2 // $Id: l2tpns.h,v 1.78 2005/06/12 06:10:29 bodea Exp $
7 #include <netinet/in.h>
12 #include <netinet/in.h>
13 #include <sys/socket.h>
14 #include <arpa/inet.h>
15 #include <sys/types.h>
18 #define VERSION "2.1.1"
21 #define MAXTUNNEL 500 // could be up to 65535
22 #define MAXSESSION 60000 // could be up to 65535
23 #define MAXTBFS 6000 // Maximum token bucket filters. Might need up to 2 * session.
25 #define RADIUS_SHIFT 6
26 #define RADIUS_FDS (1 << RADIUS_SHIFT)
27 #define RADIUS_MASK ((1 << RADIUS_SHIFT) - 1)
28 #define MAXRADIUS (1 << (8 + RADIUS_SHIFT))
30 #define T_UNDEF (0xffff) // A tunnel ID that won't ever be used. Mark session as undefined.
31 #define T_FREE (0) // A tunnel ID that won't ever be used. Mark session as free.
33 #define MAXCONTROL 1000 // max length control message we ever send...
34 #define MAXETHER (1500+18) // max packet we try sending to tun
35 #define MAXTEL 96 // telephone number
36 #define MAXUSER 128 // username
37 #define MAXPASS 128 // password
38 #define MAXPLUGINS 20 // maximum number of plugins to load
39 #define MAXRADSERVER 10 // max radius servers
40 #define MAXROUTE 10 // max static routes per session
41 #define MAXIPPOOL 131072 // max number of ip addresses in pool
42 #define RINGBUFFER_SIZE 10000 // Number of ringbuffer entries to allocate
43 #define MAX_LOG_LENGTH 512 // Maximum size of log message
44 #define ECHO_TIMEOUT 60 // Time between last packet sent and LCP ECHO generation
45 #define IDLE_TIMEOUT 240 // Time between last packet sent and LCP ECHO generation
46 #define BUSY_WAIT_TIME 3000 // 5 minutes in 1/10th seconds to wait for radius to cleanup on shutdown
47 #define DEFAULT_MRU 1452 // maximum packet size to avoid fragmentation when LNS ethernet MTU is 1500
51 #define ETCDIR "/etc/l2tpns"
55 #define LIBDIR "/usr/lib/l2tpns"
59 #define PLUGINDIR LIBDIR // Plugins
63 #define PLUGINCONF ETCDIR // Plugin config dir
67 #define FLASHDIR ETCDIR
70 #define TUNDEVICE "/dev/net/tun"
71 #define RANDOMDEVICE "/dev/urandom" // default, not as secure as /dev/random but non-blocking
72 #define CONFIGFILE FLASHDIR "/startup-config" // Configuration file
73 #define CLIUSERS FLASHDIR "/users" // CLI Users file
74 #define IPPOOLFILE FLASHDIR "/ip_pool" // Address pool configuration
75 #define ACCT_TIME 3000 // 5 minute accounting interval
76 #define ACCT_SHUT_TIME 600 // 1 minute for counters of shutdown sessions
77 #define L2TPPORT 1701 // L2TP port
78 #define RADPORT 1645 // old radius port...
79 #define PKTARP 0x0806 // ARP packet type
80 #define PKTIP 0x0800 // IPv4 packet type
81 #define PKTIPV6 0x86DD // IPv6 packet type
83 #define PPPCHAP 0xC223
85 #define PPPIPCP 0x8021
86 #define PPPIPV6CP 0x8057
89 #define PPPIPV6 0x0057
91 #define MIN_IP_SIZE 0x19
118 typedef uint16_t sessionidt
;
119 typedef uint16_t tunnelidt
;
120 typedef uint32_t clockt
;
121 typedef uint8_t hasht
[16];
124 struct cli_session_actions
{
134 #define CLI_SESS_KILL 0x01
135 #define CLI_SESS_SNOOP 0x02
136 #define CLI_SESS_NOSNOOP 0x04
137 #define CLI_SESS_THROTTLE 0x08
138 #define CLI_SESS_NOTHROTTLE 0x10
139 #define CLI_SESS_FILTER 0x20
140 #define CLI_SESS_NOFILTER 0x40
142 struct cli_tunnel_actions
{
146 #define CLI_TUN_KILL 0x01
149 typedef struct // route
156 typedef struct controls
// control message
158 struct controls
*next
; // next in queue
159 uint16_t length
; // length
160 uint8_t buf
[MAXCONTROL
];
166 sessionidt next
; // next session in linked list
167 sessionidt far
; // far end session ID
168 tunnelidt tunnel
; // near end tunnel ID
169 uint8_t l2tp_flags
; // various bit flags from the ICCN on the l2tp tunnel.
170 uint8_t flags
; // Various session flags.
171 in_addr_t ip
; // IP of session set by RADIUS response (host byte order).
172 int ip_pool_index
; // index to IP pool
173 uint32_t unique_id
; // unique session id
174 uint16_t nr
; // next receive
175 uint16_t ns
; // next send
176 uint32_t magic
; // ppp magic number
177 uint32_t pin
, pout
; // packet counts
178 uint32_t cin
, cout
; // byte counts
179 uint32_t cin_wrap
, cout_wrap
; // byte counter wrap count (RADIUS accounting giagawords)
180 uint32_t cin_delta
, cout_delta
; // byte count changes (for dump_session())
181 uint16_t throttle_in
; // upstream throttle rate (kbps)
182 uint16_t throttle_out
; // downstream throttle rate
183 uint8_t filter_in
; // input filter index (to ip_filters[N-1]; 0 if none)
184 uint8_t filter_out
; // output filter index
185 uint16_t mru
; // maximum receive unit
186 clockt opened
; // when started
187 clockt die
; // being closed, when to finally free
188 time_t last_packet
; // Last packet from the user (used for idle timeouts)
189 in_addr_t dns1
, dns2
; // DNS servers
190 routet route
[MAXROUTE
]; // static routes
191 uint16_t tbf_in
; // filter bucket for throttling in from the user.
192 uint16_t tbf_out
; // filter bucket for throttling out to the user.
193 int random_vector_length
;
194 char random_vector
[MAXTEL
];
195 char user
[MAXUSER
]; // user (needed in seesion for radius stop messages)
196 char called
[MAXTEL
]; // called number
197 char calling
[MAXTEL
]; // calling number
198 uint32_t tx_connect_speed
;
199 uint32_t rx_connect_speed
;
200 in_addr_t snoop_ip
; // Interception destination IP
201 uint16_t snoop_port
; // Interception destination port
202 uint8_t walled_garden
; // is this session gardened?
203 uint8_t ipv6prefixlen
; // IPv6 route prefix length
204 struct in6_addr ipv6route
; // Static IPv6 route
205 char reserved
[16]; // Space to expand structure without changing HB_VERSION
209 #define SF_IPCP_ACKED 1 // Has this session seen an IPCP Ack?
210 #define SF_LCP_ACKED 2 // LCP negotiated
211 #define SF_CCP_ACKED 4 // CCP negotiated
212 #define SF_IPV6CP_ACKED 8 // IPv6 negotiated
213 #define SF_IPV6_NACKED 16 // IPv6 rejected
214 #define SF_IPV6_ROUTED 32 // advertised v6 route
216 #define AUTHPAP 1 // allow PAP
217 #define AUTHCHAP 2 // allow CHAP
230 clockt last_packet_out
;
231 uint32_t packets_out
;
232 uint32_t packets_dropped
;
234 // RADIUS session in use
241 #define SESSIONPFC 1 // PFC negotiated flags
242 #define SESSIONACFC 2 // ACFC negotiated flags
244 // 168 bytes per tunnel
247 tunnelidt far
; // far end tunnel ID
248 in_addr_t ip
; // Ip for far end
249 uint16_t port
; // port for far end
250 uint16_t window
; // Rx window
251 uint16_t nr
; // next receive
252 uint16_t ns
; // next send
253 int state
; // current state (tunnelstate enum)
254 clockt last
; // when last control message sent (used for resend timeout)
255 clockt retry
; // when to try resenting pending control
256 clockt die
; // being closed, when to finally free
257 clockt lastrec
; // when the last control message was received
258 char hostname
[128]; // tunnel hostname
259 char vendor
[128]; // LAC vendor
260 uint8_t try; // number of retrys on a control message
261 uint16_t controlc
; // outstaind messages in queue
262 controlt
*controls
; // oldest message
263 controlt
*controle
; // newest message
267 // 180 bytes per radius session
268 typedef struct // outstanding RADIUS requests
270 sessionidt session
; // which session this applies to
271 hasht auth
; // request authenticator
272 clockt retry
; // when to try next
273 char calling
[MAXTEL
]; // calling number
274 char pass
[129]; // password
275 uint8_t id
; // ID for PPP response
276 uint8_t try; // which try we are on
277 uint8_t state
; // state of radius requests
278 uint8_t chap
; // set if CHAP used (is CHAP identifier)
284 in_addr_t address
; // Host byte order..
285 char assigned
; // 1 if assigned, 0 if free
287 clockt last
; // last used
288 char user
[129]; // user (try to have ip addresses persistent)
299 char message
[MAX_LOG_LENGTH
];
300 } buffer
[RINGBUFFER_SIZE
];
307 * Possible tunnel states
308 * TUNNELFREE -> TUNNELOPEN -> TUNNELDIE -> TUNNELFREE
312 TUNNELFREE
, // Not in use
313 TUNNELOPEN
, // Active tunnel
314 TUNNELDIE
, // Currently closing
315 TUNNELOPENING
, // Busy opening
316 TUNNELUNDEF
, // Undefined
321 RADIUSNULL
, // Not in use
322 RADIUSCHAP
, // sending CHAP down PPP
323 RADIUSAUTH
, // sending auth to RADIUS server
324 RADIUSIPCP
, // sending IPCP to end user
325 RADIUSSTART
, // sending start accounting to RADIUS server
326 RADIUSSTOP
, // sending stop accounting to RADIUS server
327 RADIUSINTERIM
, // sending interim accounting to RADIUS server
328 RADIUSWAIT
, // waiting timeout before available, in case delayed replies
336 uint32_t tun_rx_packets
;
337 uint32_t tun_tx_packets
;
338 uint32_t tun_rx_bytes
;
339 uint32_t tun_tx_bytes
;
340 uint32_t tun_rx_errors
;
341 uint32_t tun_tx_errors
;
342 uint32_t tun_rx_dropped
;
344 uint32_t tunnel_rx_packets
;
345 uint32_t tunnel_tx_packets
;
346 uint32_t tunnel_rx_bytes
;
347 uint32_t tunnel_tx_bytes
;
348 uint32_t tunnel_rx_errors
;
349 uint32_t tunnel_tx_errors
;
351 uint32_t tunnel_retries
;
352 uint32_t radius_retries
;
356 uint32_t packets_snooped
;
358 uint32_t tunnel_created
;
359 uint32_t session_created
;
360 uint32_t tunnel_timeout
;
361 uint32_t session_timeout
;
362 uint32_t radius_timeout
;
363 uint32_t radius_overflow
;
364 uint32_t tunnel_overflow
;
365 uint32_t session_overflow
;
367 uint32_t ip_allocated
;
370 uint32_t c_forwarded
;
371 uint32_t recv_forward
;
373 uint32_t select_called
;
374 uint32_t multi_read_used
;
375 uint32_t multi_read_exceeded
;
378 uint32_t call_processtun
;
379 uint32_t call_processipout
;
380 uint32_t call_processipv6out
;
381 uint32_t call_processudp
;
382 uint32_t call_sessionbyip
;
383 uint32_t call_sessionbyipv6
;
384 uint32_t call_sessionbyuser
;
385 uint32_t call_sendarp
;
386 uint32_t call_sendipcp
;
387 uint32_t call_processipv6cp
;
388 uint32_t call_tunnelsend
;
389 uint32_t call_sessionkill
;
390 uint32_t call_sessionshutdown
;
391 uint32_t call_tunnelkill
;
392 uint32_t call_tunnelshutdown
;
393 uint32_t call_assign_ip_address
;
394 uint32_t call_free_ip_address
;
395 uint32_t call_dump_acct_info
;
396 uint32_t call_sessionsetup
;
397 uint32_t call_processpap
;
398 uint32_t call_processchap
;
399 uint32_t call_processlcp
;
400 uint32_t call_processipcp
;
401 uint32_t call_processipin
;
402 uint32_t call_processipv6in
;
403 uint32_t call_processccp
;
404 uint32_t call_sendchap
;
405 uint32_t call_processrad
;
406 uint32_t call_radiussend
;
407 uint32_t call_radiusretry
;
408 uint32_t call_random_data
;
415 #define CSTAT(x) STAT(call_ ## x)
420 #define STAT(x) (_statistics->x++)
421 #define INC_STAT(x,y) (_statistics->x += (y))
422 #define GET_STAT(x) (_statistics->x)
423 #define SET_STAT(x, y) (_statistics->x = (y))
427 #define INC_STAT(x,y)
428 #define GET_STAT(x) 0
429 #define SET_STAT(x, y)
434 int debug
; // debugging level
435 time_t start_time
; // time when l2tpns was started
436 char bandwidth
[256]; // current bandwidth
437 char pid_file
[256]; // file to write PID to on startup
439 clockt current_time
; // 1/10ths of a second since the process started.
440 // means that we can only run a given process
441 // for 13 years without re-starting!
443 char config_file
[128];
444 int reload_config
; // flag to re-read config (set by cli)
445 int multi_read_count
; // amount of packets to read per fd in processing loop
447 char tundevice
[10]; // tun device name
448 char log_filename
[128];
451 char random_device
[256]; // random device path, defaults to RANDOMDEVICE
453 char radiussecret
[64];
454 int radius_accounting
;
456 in_addr_t radiusserver
[MAXRADSERVER
]; // radius servers
457 uint16_t radiusport
[MAXRADSERVER
]; // radius base ports
458 uint8_t numradiusservers
; // radius server count
460 char radius_authtypes_s
[32]; // list of valid authentication types (chap, pap) in order of preference
461 int radius_authtypes
;
462 int radius_authprefer
;
464 int allow_duplicate_users
; // allow multiple logins with the same username
466 in_addr_t default_dns1
, default_dns2
;
468 unsigned long rl_rate
; // default throttle rate
469 int num_tbfs
; // number of throttle buckets
471 char accounting_dir
[128];
472 in_addr_t bind_address
;
473 in_addr_t peer_address
;
474 int send_garp
; // Set to true to garp for vip address on startup
478 char plugins
[64][MAXPLUGINS
];
479 char old_plugins
[64][MAXPLUGINS
];
481 int next_tbf
; // Next HTB id available to use
482 int scheduler_fifo
; // If the system has multiple CPUs, use FIFO scheduling
483 // policy for this process.
484 int lock_pages
; // Lock pages into memory.
485 int icmp_rate
; // Max number of ICMP unreachable per second to send
486 int max_packets
; // DoS prevention: per session limit of packets/0.1s
488 in_addr_t cluster_address
; // Multicast address of cluster.
489 // Send to this address to have everyone hear.
490 char cluster_interface
[64]; // Which interface to listen for multicast on.
491 int cluster_iam_master
; // Are we the cluster master???
492 int cluster_iam_uptodate
; // Set if we've got a full set of state from the master.
493 in_addr_t cluster_master_address
; // The network address of the cluster master.
494 // Zero if i am the cluster master.
495 int cluster_seq_number
; // Sequence number of the next heartbeat we'll send out
496 // (or the seq number we're next expecting if we're a slave).
497 int cluster_undefined_sessions
; // How many sessions we're yet to receive from the master.
498 int cluster_undefined_tunnels
; // How many tunnels we're yet to receive from the master.
499 int cluster_highest_sessionid
;
500 int cluster_highest_tunnelid
;
501 clockt cluster_last_hb
; // Last time we saw a heartbeat from the master.
502 int cluster_last_hb_ver
; // Heartbeat version last seen from master
503 int cluster_num_changes
; // Number of changes queued.
505 int cluster_hb_interval
; // How often to send a heartbeat.
506 int cluster_hb_timeout
; // How many missed heartbeats trigger an election.
507 uint64_t cluster_table_version
; // # state changes processed by cluster
509 struct in6_addr ipv6_prefix
; // Our IPv6 network pool.
512 int cluster_master_min_adv
; // Master advertises routes while the number of up to date
513 // slaves is less than this value.
516 #define BGP_NUM_PEERS 2
523 } neighbour
[BGP_NUM_PEERS
];
527 enum config_typet
{ INT
, STRING
, UNSIGNED_LONG
, SHORT
, BOOL
, IPv4
, IPv6
, MAC
};
533 enum config_typet type
;
538 uint8_t op
; // operation
539 #define FILTER_PORT_OP_NONE 0 // all ports match
540 #define FILTER_PORT_OP_EQ 1
541 #define FILTER_PORT_OP_NEQ 2
542 #define FILTER_PORT_OP_GT 3
543 #define FILTER_PORT_OP_LT 4
544 #define FILTER_PORT_OP_RANGE 5
545 uint16_t port
; // port (host byte order)
546 uint16_t port2
; // range
551 int action
; // permit/deny
552 #define FILTER_ACTION_DENY 1
553 #define FILTER_ACTION_PERMIT 2
554 uint8_t proto
; // protocol: IPPROTO_* (netinet/in.h)
555 in_addr_t src_ip
; // source ip (network byte order)
557 ip_filter_portt src_ports
;
558 in_addr_t dst_ip
; // dest ip
560 ip_filter_portt dst_ports
;
561 uint8_t frag
; // apply to non-initial fragments
562 uint8_t tcp_flag_op
; // match type: any, all, established
563 #define FILTER_FLAG_OP_ANY 1
564 #define FILTER_FLAG_OP_ALL 2
565 #define FILTER_FLAG_OP_EST 3
566 uint8_t tcp_sflags
; // flags set
567 uint8_t tcp_cflags
; // flags clear
568 uint32_t counter
; // match count
571 #define TCP_FLAG_FIN 0x01
572 #define TCP_FLAG_SYN 0x02
573 #define TCP_FLAG_RST 0x04
574 #define TCP_FLAG_PSH 0x08
575 #define TCP_FLAG_ACK 0x10
576 #define TCP_FLAG_URG 0x20
579 #define MAXFILTER_RULES 32
582 char name
[32]; // ACL name
583 int extended
; // type: 0 = standard, 1 = extended
584 ip_filter_rulet rules
[MAXFILTER_RULES
];
585 int used
; // session ref count
589 void sendarp(int ifr_idx
, const unsigned char* mac
, in_addr_t ip
);
593 void processpap(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
594 void processchap(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
595 void processlcp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
596 void processipcp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
597 void processipv6cp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
598 void processipin(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
599 void processipv6in(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
600 void processccp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
601 void sendchap(tunnelidt t
, sessionidt s
);
602 uint8_t *makeppp(uint8_t *b
, int size
, uint8_t *p
, int l
, tunnelidt t
, sessionidt s
, uint16_t mtype
);
603 void sendlcp(tunnelidt t
, sessionidt s
, int authtype
);
604 void send_ipin(sessionidt s
, uint8_t *buf
, int len
);
609 void radiussend(uint16_t r
, uint8_t state
);
610 void processrad(uint8_t *buf
, int len
, char socket_index
);
611 void radiusretry(uint16_t r
);
612 uint16_t radiusnew(sessionidt s
);
613 void radiusclear(uint16_t r
, sessionidt s
);
617 clockt
backoff(uint8_t try);
618 void send_ipv6_ra(tunnelidt t
, sessionidt s
, struct in6_addr
*ip
);
619 void route6set(sessionidt s
, struct in6_addr ip
, int prefixlen
, int add
);
620 sessionidt
sessionbyip(in_addr_t ip
);
621 sessionidt
sessionbyipv6(struct in6_addr ip
);
622 sessionidt
sessionbyuser(char *username
);
623 void increment_counter(uint32_t *counter
, uint32_t *wrap
, uint32_t delta
);
624 void random_data(uint8_t *buf
, int len
);
625 void sessionkill(sessionidt s
, char *reason
);
626 void sessionshutdown(sessionidt s
, char *reason
, int result
, int error
);
627 void send_garp(in_addr_t ip
);
628 void tunnelsend(uint8_t *buf
, uint16_t l
, tunnelidt t
);
629 void sendipcp(tunnelidt t
, sessionidt s
);
630 void processudp(uint8_t *buf
, int len
, struct sockaddr_in
*addr
);
631 void snoop_send_packet(char *packet
, uint16_t size
, in_addr_t destination
, uint16_t port
);
632 int ip_filter(uint8_t *buf
, int len
, uint8_t filter
);
633 int cmd_show_ipcache(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
634 int cmd_show_hist_idle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
635 int cmd_show_hist_open(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
639 #define LOG(D, s, t, f, ...) ({ if (D <= config->debug) _log(D, s, t, f, ## __VA_ARGS__); })
640 #define LOG_HEX(D, t, d, s) ({ if (D <= config->debug) _log_hex(D, t, d, s); })
642 void _log(int level
, sessionidt s
, tunnelidt t
, const char *format
, ...) __attribute__((format (printf
, 4, 5)));
643 void _log_hex(int level
, const char *title
, const char *data
, int maxsize
);
645 int sessionsetup(tunnelidt t
, sessionidt s
);
646 int run_plugins(int plugin_type
, void *data
);
647 void rebuild_address_pool(void);
648 void throttle_session(sessionidt s
, int rate_in
, int rate_out
);
649 int load_session(sessionidt
, sessiont
*);
650 void become_master(void); // We're the master; kick off any required master initializations.
654 void init_cli(char *hostname
);
655 void cli_do_file(FILE *fh
);
656 void cli_do(int sockfd
);
657 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...);
661 void host_unreachable(in_addr_t destination
, uint16_t id
, in_addr_t source
, char *packet
, int packet_len
);
664 extern tunnelt
*tunnel
;
665 extern sessiont
*session
;
666 extern sessionlocalt
*sess_local
;
667 extern ippoolt
*ip_address_pool
;
668 #define sessionfree (session[0].next)
670 #define log_backtrace(count, max) \
671 if (count++ < max) { \
675 LOG(0, 0, t, "Backtrace follows:\n"); \
676 size = backtrace(array, 10); \
677 strings = backtrace_symbols(array, size); \
678 if (strings) for (i = 0; i < size; i++) \
680 LOG(0, 0, t, " %s\n", strings[i]); \
686 extern configt
*config
;
687 extern time_t basetime
; // Time when this process started.
688 extern time_t time_now
; // Seconds since EPOCH.
689 extern char main_quit
;
690 extern uint32_t last_id
;
691 extern struct Tstats
*_statistics
;
692 extern in_addr_t my_address
;
693 extern int tun_write(uint8_t *data
, int size
);
707 int index
; // for RADIUS, BGP
710 #define TIME (config->current_time)
712 // macros for handling help in cli commands
713 #define CLI_HELP_REQUESTED (argc > 0 && argv[argc-1][strlen(argv[argc-1])-1] == '?')
714 #define CLI_HELP_NO_ARGS (argc > 1 || argv[0][1]) ? CLI_OK : cli_arg_help(cli, 1, NULL)
716 // CVS identifiers (for "show version file")
717 extern char const *cvs_id_arp
;
718 extern char const *cvs_id_cli
;
719 extern char const *cvs_id_cluster
;
720 extern char const *cvs_id_constants
;
721 extern char const *cvs_id_control
;
722 extern char const *cvs_id_icmp
;
723 extern char const *cvs_id_l2tpns
;
724 extern char const *cvs_id_ll
;
725 extern char const *cvs_id_md5
;
726 extern char const *cvs_id_ppp
;
727 extern char const *cvs_id_radius
;
728 extern char const *cvs_id_tbf
;
729 extern char const *cvs_id_util
;
731 #endif /* __L2TPNS_H__ */