2 // $Id: l2tpns.h,v 1.71 2005/05/16 05:56:33 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.0"
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_MASK ((1 << RADIUS_SHIFT) - 1)
27 #define MAXRADIUS (1 << (8 + RADIUS_SHIFT))
29 #define T_UNDEF (0xffff) // A tunnel ID that won't ever be used. Mark session as undefined.
30 #define T_FREE (0) // A tunnel ID that won't ever be used. Mark session as free.
32 #define MAXCONTROL 1000 // max length control message we ever send...
33 #define MAXETHER (1500+18) // max packet we try sending to tun
34 #define MAXTEL 96 // telephone number
35 #define MAXUSER 128 // username
36 #define MAXPASS 128 // password
37 #define MAXPLUGINS 20 // maximum number of plugins to load
38 #define MAXRADSERVER 10 // max radius servers
39 #define MAXROUTE 10 // max static routes per session
40 #define MAXIPPOOL 131072 // max number of ip addresses in pool
41 #define RINGBUFFER_SIZE 10000 // Number of ringbuffer entries to allocate
42 #define MAX_LOG_LENGTH 512 // Maximum size of log message
43 #define ECHO_TIMEOUT 60 // Time between last packet sent and LCP ECHO generation
44 #define IDLE_TIMEOUT 240 // Time between last packet sent and LCP ECHO generation
45 #define BUSY_WAIT_TIME 3000 // 5 minutes in 1/10th seconds to wait for radius to cleanup on shutdown
46 #define DEFAULT_MRU 1458 // maximum packet size to avoid fragmentation when LNS ethernet MTU is 1500
50 #define ETCDIR "/etc/l2tpns"
54 #define LIBDIR "/usr/lib/l2tpns"
58 #define PLUGINDIR LIBDIR // Plugins
62 #define PLUGINCONF ETCDIR // Plugin config dir
66 #define FLASHDIR ETCDIR
69 #define TUNDEVICE "/dev/net/tun"
70 #define RANDOMDEVICE "/dev/urandom" // default, not as secure as /dev/random but non-blocking
71 #define CONFIGFILE FLASHDIR "/startup-config" // Configuration file
72 #define CLIUSERS FLASHDIR "/users" // CLI Users file
73 #define IPPOOLFILE FLASHDIR "/ip_pool" // Address pool configuration
74 #define ACCT_TIME 3000 // 5 minute accounting interval
75 #define ACCT_SHUT_TIME 600 // 1 minute for counters of shutdown sessions
76 #define L2TPPORT 1701 // L2TP port
77 #define RADPORT 1645 // old radius port...
78 #define PKTARP 0x0806 // ARP packet type
79 #define PKTIP 0x0800 // IPv4 packet type
80 #define PKTIPV6 0x86DD // IPv6 packet type
82 #define PPPCHAP 0xC223
84 #define PPPIPCP 0x8021
85 #define PPPIPV6CP 0x8057
88 #define PPPIPV6 0x0057
90 #define MIN_IP_SIZE 0x19
117 typedef uint16_t sessionidt
;
118 typedef uint16_t tunnelidt
;
119 typedef uint32_t clockt
;
120 typedef uint8_t hasht
[16];
123 struct cli_session_actions
{
133 #define CLI_SESS_KILL 0x01
134 #define CLI_SESS_SNOOP 0x02
135 #define CLI_SESS_NOSNOOP 0x04
136 #define CLI_SESS_THROTTLE 0x08
137 #define CLI_SESS_NOTHROTTLE 0x10
138 #define CLI_SESS_FILTER 0x20
139 #define CLI_SESS_NOFILTER 0x40
141 struct cli_tunnel_actions
{
145 #define CLI_TUN_KILL 0x01
148 typedef struct // route
155 typedef struct controls
// control message
157 struct controls
*next
; // next in queue
158 uint16_t length
; // length
159 uint8_t buf
[MAXCONTROL
];
165 sessionidt next
; // next session in linked list
166 sessionidt far
; // far end session ID
167 tunnelidt tunnel
; // near end tunnel ID
168 uint8_t l2tp_flags
; // various bit flags from the ICCN on the l2tp tunnel.
169 uint8_t flags
; // Various session flags.
170 in_addr_t ip
; // IP of session set by RADIUS response (host byte order).
171 int ip_pool_index
; // index to IP pool
172 uint32_t unique_id
; // unique session id
173 uint16_t nr
; // next receive
174 uint16_t ns
; // next send
175 uint32_t magic
; // ppp magic number
176 uint32_t cin
, cout
; // byte counts
177 uint32_t pin
, pout
; // packet counts
178 uint32_t total_cin
; // This counter is never reset while a session is open
179 uint32_t total_cout
; // This counter is never reset while a session is open
180 uint16_t throttle_in
; // upstream throttle rate (kbps)
181 uint16_t throttle_out
; // downstream throttle rate
182 uint8_t filter_in
; // input filter index (to ip_filters[N-1]; 0 if none)
183 uint8_t filter_out
; // output filter index
184 uint16_t mru
; // maximum receive unit
185 clockt opened
; // when started
186 clockt die
; // being closed, when to finally free
187 time_t last_packet
; // Last packet from the user (used for idle timeouts)
188 in_addr_t dns1
, dns2
; // DNS servers
189 routet route
[MAXROUTE
]; // static routes
190 uint16_t tbf_in
; // filter bucket for throttling in from the user.
191 uint16_t tbf_out
; // filter bucket for throttling out to the user.
192 int random_vector_length
;
193 char random_vector
[MAXTEL
];
194 char user
[MAXUSER
]; // user (needed in seesion for radius stop messages)
195 char called
[MAXTEL
]; // called number
196 char calling
[MAXTEL
]; // calling number
197 uint32_t tx_connect_speed
;
198 uint32_t rx_connect_speed
;
199 in_addr_t snoop_ip
; // Interception destination IP
200 uint16_t snoop_port
; // Interception destination port
201 uint8_t walled_garden
; // is this session gardened?
202 uint8_t ipv6prefixlen
; // IPv6 route prefix length
203 struct in6_addr ipv6route
; // Static IPv6 route
204 char reserved
[24]; // Space to expand structure without changing HB_VERSION
208 #define SF_IPCP_ACKED 1 // Has this session seen an IPCP Ack?
209 #define SF_LCP_ACKED 2 // LCP negotiated
210 #define SF_CCP_ACKED 4 // CCP negotiated
211 #define SF_IPV6CP_ACKED 8 // IPv6 negotiated
212 #define SF_IPV6_NACKED 16 // IPv6 rejected
213 #define SF_IPV6_ROUTED 32 // advertised v6 route
215 #define AUTHPAP 1 // allow PAP
216 #define AUTHCHAP 2 // allow CHAP
225 clockt last_packet_out
;
226 uint32_t packets_out
;
227 uint32_t packets_dropped
;
229 // RADIUS session in use
236 #define SESSIONPFC 1 // PFC negotiated flags
237 #define SESSIONACFC 2 // ACFC negotiated flags
239 // 168 bytes per tunnel
242 tunnelidt far
; // far end tunnel ID
243 in_addr_t ip
; // Ip for far end
244 uint16_t port
; // port for far end
245 uint16_t window
; // Rx window
246 uint16_t nr
; // next receive
247 uint16_t ns
; // next send
248 int state
; // current state (tunnelstate enum)
249 clockt last
; // when last control message sent (used for resend timeout)
250 clockt retry
; // when to try resenting pending control
251 clockt die
; // being closed, when to finally free
252 clockt lastrec
; // when the last control message was received
253 char hostname
[128]; // tunnel hostname
254 char vendor
[128]; // LAC vendor
255 uint8_t try; // number of retrys on a control message
256 uint16_t controlc
; // outstaind messages in queue
257 controlt
*controls
; // oldest message
258 controlt
*controle
; // newest message
262 // 180 bytes per radius session
263 typedef struct // outstanding RADIUS requests
265 sessionidt session
; // which session this applies to
266 hasht auth
; // request authenticator
267 clockt retry
; // when to try next
268 char calling
[MAXTEL
]; // calling number
269 char pass
[129]; // password
270 uint8_t id
; // ID for PPP response
271 uint8_t try; // which try we are on
272 uint8_t state
; // state of radius requests
273 uint8_t chap
; // set if CHAP used (is CHAP identifier)
279 in_addr_t address
; // Host byte order..
280 char assigned
; // 1 if assigned, 0 if free
282 clockt last
; // last used
283 char user
[129]; // user (try to have ip addresses persistent)
294 char message
[MAX_LOG_LENGTH
];
295 } buffer
[RINGBUFFER_SIZE
];
302 * Possible tunnel states
303 * TUNNELFREE -> TUNNELOPEN -> TUNNELDIE -> TUNNELFREE
307 TUNNELFREE
, // Not in use
308 TUNNELOPEN
, // Active tunnel
309 TUNNELDIE
, // Currently closing
310 TUNNELOPENING
, // Busy opening
311 TUNNELUNDEF
, // Undefined
316 RADIUSNULL
, // Not in use
317 RADIUSCHAP
, // sending CHAP down PPP
318 RADIUSAUTH
, // sending auth to RADIUS server
319 RADIUSIPCP
, // sending IPCP to end user
320 RADIUSSTART
, // sending start accounting to RADIUS server
321 RADIUSSTOP
, // sending stop accounting to RADIUS server
322 RADIUSINTERIM
, // sending interim accounting to RADIUS server
323 RADIUSWAIT
, // waiting timeout before available, in case delayed replies
331 uint32_t tun_rx_packets
;
332 uint32_t tun_tx_packets
;
333 uint32_t tun_rx_bytes
;
334 uint32_t tun_tx_bytes
;
335 uint32_t tun_rx_errors
;
336 uint32_t tun_tx_errors
;
337 uint32_t tun_rx_dropped
;
339 uint32_t tunnel_rx_packets
;
340 uint32_t tunnel_tx_packets
;
341 uint32_t tunnel_rx_bytes
;
342 uint32_t tunnel_tx_bytes
;
343 uint32_t tunnel_rx_errors
;
344 uint32_t tunnel_tx_errors
;
346 uint32_t tunnel_retries
;
347 uint32_t radius_retries
;
351 uint32_t packets_snooped
;
353 uint32_t tunnel_created
;
354 uint32_t session_created
;
355 uint32_t tunnel_timeout
;
356 uint32_t session_timeout
;
357 uint32_t radius_timeout
;
358 uint32_t radius_overflow
;
359 uint32_t tunnel_overflow
;
360 uint32_t session_overflow
;
362 uint32_t ip_allocated
;
365 uint32_t c_forwarded
;
366 uint32_t recv_forward
;
368 uint32_t select_called
;
369 uint32_t multi_read_used
;
370 uint32_t multi_read_exceeded
;
373 uint32_t call_processtun
;
374 uint32_t call_processipout
;
375 uint32_t call_processipv6out
;
376 uint32_t call_processudp
;
377 uint32_t call_sessionbyip
;
378 uint32_t call_sessionbyipv6
;
379 uint32_t call_sessionbyuser
;
380 uint32_t call_sendarp
;
381 uint32_t call_sendipcp
;
382 uint32_t call_processipv6cp
;
383 uint32_t call_tunnelsend
;
384 uint32_t call_sessionkill
;
385 uint32_t call_sessionshutdown
;
386 uint32_t call_tunnelkill
;
387 uint32_t call_tunnelshutdown
;
388 uint32_t call_assign_ip_address
;
389 uint32_t call_free_ip_address
;
390 uint32_t call_dump_acct_info
;
391 uint32_t call_sessionsetup
;
392 uint32_t call_processpap
;
393 uint32_t call_processchap
;
394 uint32_t call_processlcp
;
395 uint32_t call_processipcp
;
396 uint32_t call_processipin
;
397 uint32_t call_processipv6in
;
398 uint32_t call_processccp
;
399 uint32_t call_sendchap
;
400 uint32_t call_processrad
;
401 uint32_t call_radiussend
;
402 uint32_t call_radiusretry
;
403 uint32_t call_random_data
;
410 #define CSTAT(x) STAT(call_ ## x)
415 #define STAT(x) (_statistics->x++)
416 #define INC_STAT(x,y) (_statistics->x += (y))
417 #define GET_STAT(x) (_statistics->x)
418 #define SET_STAT(x, y) (_statistics->x = (y))
422 #define INC_STAT(x,y)
423 #define GET_STAT(x) 0
424 #define SET_STAT(x, y)
429 int debug
; // debugging level
430 time_t start_time
; // time when l2tpns was started
431 char bandwidth
[256]; // current bandwidth
432 char pid_file
[256]; // file to write PID to on startup
434 clockt current_time
; // 1/10ths of a second since the process started.
435 // means that we can only run a given process
436 // for 13 years without re-starting!
438 char config_file
[128];
439 int reload_config
; // flag to re-read config (set by cli)
440 int cleanup_interval
; // interval between regular cleanups (in seconds)
441 int multi_read_count
; // amount of packets to read per fd in processing loop
443 char tundevice
[10]; // tun device name
444 char log_filename
[128];
447 char random_device
[256]; // random device path, defaults to RANDOMDEVICE
449 char radiussecret
[64];
450 int radius_accounting
;
452 in_addr_t radiusserver
[MAXRADSERVER
]; // radius servers
453 uint16_t radiusport
[MAXRADSERVER
]; // radius base ports
454 uint8_t numradiusservers
; // radius server count
455 short num_radfds
; // Number of radius filehandles allocated
457 char radius_authtypes_s
[32]; // list of valid authentication types (chap, pap) in order of preference
458 int radius_authtypes
;
459 int radius_authprefer
;
461 in_addr_t default_dns1
, default_dns2
;
463 unsigned long rl_rate
; // default throttle rate
464 int num_tbfs
; // number of throttle buckets
466 char accounting_dir
[128];
467 in_addr_t bind_address
;
468 in_addr_t peer_address
;
469 int send_garp
; // Set to true to garp for vip address on startup
473 char plugins
[64][MAXPLUGINS
];
474 char old_plugins
[64][MAXPLUGINS
];
476 int next_tbf
; // Next HTB id available to use
477 int scheduler_fifo
; // If the system has multiple CPUs, use FIFO scheduling policy for this process.
478 int lock_pages
; // Lock pages into memory.
479 int icmp_rate
; // Max number of ICMP unreachable per second to send
480 int max_packets
; // DoS prevention: per session limit of packets/0.1s
482 in_addr_t cluster_address
; // Multicast address of cluster.
483 // Send to this address to have everyone hear.
484 char cluster_interface
[64]; // Which interface to listen for multicast on.
485 int cluster_iam_master
; // Are we the cluster master???
486 int cluster_iam_uptodate
; // Set if we've got a full set of state from the master.
487 in_addr_t cluster_master_address
; // The network address of the cluster master.
488 // Zero if i am the cluster master.
489 int cluster_seq_number
; // Sequence number of the next heartbeat we'll send out
490 // (or the seq number we're next expecting if we're a slave).
491 int cluster_undefined_sessions
; // How many sessions we're yet to receive from the master.
492 int cluster_undefined_tunnels
; // How many tunnels we're yet to receive from the master.
493 int cluster_highest_sessionid
;
494 int cluster_highest_tunnelid
;
495 clockt cluster_last_hb
; // Last time we saw a heartbeat from the master.
496 int cluster_num_changes
; // Number of changes queued.
498 int cluster_hb_interval
; // How often to send a heartbeat.
499 int cluster_hb_timeout
; // How many missed heartbeats trigger an election.
500 uint64_t cluster_table_version
; // # state changes processed by cluster
502 struct in6_addr ipv6_prefix
; // Our IPv6 network pool.
505 #define BGP_NUM_PEERS 2
512 } neighbour
[BGP_NUM_PEERS
];
516 enum config_typet
{ INT
, STRING
, UNSIGNED_LONG
, SHORT
, BOOL
, IPv4
, IPv6
, MAC
};
522 enum config_typet type
;
527 uint8_t op
; // operation
528 #define FILTER_PORT_OP_NONE 0 // all ports match
529 #define FILTER_PORT_OP_EQ 1
530 #define FILTER_PORT_OP_NEQ 2
531 #define FILTER_PORT_OP_GT 3
532 #define FILTER_PORT_OP_LT 4
533 #define FILTER_PORT_OP_RANGE 5
534 uint16_t port
; // port (host byte order)
535 uint16_t port2
; // range
540 int action
; // permit/deny
541 #define FILTER_ACTION_DENY 1
542 #define FILTER_ACTION_PERMIT 2
543 uint8_t proto
; // protocol: IPPROTO_* (netinet/in.h)
544 in_addr_t src_ip
; // source ip (network byte order)
546 ip_filter_portt src_ports
;
547 in_addr_t dst_ip
; // dest ip
549 ip_filter_portt dst_ports
;
550 uint8_t frag
; // apply to non-initial fragments
551 uint8_t tcp_flag_op
; // match type: any, all, established
552 #define FILTER_FLAG_OP_ANY 1
553 #define FILTER_FLAG_OP_ALL 2
554 #define FILTER_FLAG_OP_EST 3
555 uint8_t tcp_sflags
; // flags set
556 uint8_t tcp_cflags
; // flags clear
557 uint32_t counter
; // match count
560 #define TCP_FLAG_FIN 0x01
561 #define TCP_FLAG_SYN 0x02
562 #define TCP_FLAG_RST 0x04
563 #define TCP_FLAG_PSH 0x08
564 #define TCP_FLAG_ACK 0x10
565 #define TCP_FLAG_URG 0x20
568 #define MAXFILTER_RULES 32
571 char name
[32]; // ACL name
572 int extended
; // type: 0 = standard, 1 = extended
573 ip_filter_rulet rules
[MAXFILTER_RULES
];
574 int used
; // session ref count
578 void sendarp(int ifr_idx
, const unsigned char* mac
, in_addr_t ip
);
582 void processpap(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
583 void processchap(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
584 void processlcp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
585 void processipcp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
586 void processipv6cp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
587 void processipin(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
588 void processipv6in(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
589 void processccp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
590 void sendchap(tunnelidt t
, sessionidt s
);
591 uint8_t *makeppp(uint8_t *b
, int size
, uint8_t *p
, int l
, tunnelidt t
, sessionidt s
, uint16_t mtype
);
592 void sendlcp(tunnelidt t
, sessionidt s
, int authtype
);
593 void send_ipin(sessionidt s
, uint8_t *buf
, int len
);
598 void radiussend(uint16_t r
, uint8_t state
);
599 void processrad(uint8_t *buf
, int len
, char socket_index
);
600 void radiusretry(uint16_t r
);
601 uint16_t radiusnew(sessionidt s
);
602 void radiusclear(uint16_t r
, sessionidt s
);
606 clockt
backoff(uint8_t try);
607 void send_ipv6_ra(tunnelidt t
, sessionidt s
, struct in6_addr
*ip
);
608 void route6set(sessionidt s
, struct in6_addr ip
, int prefixlen
, int add
);
609 sessionidt
sessionbyip(in_addr_t ip
);
610 sessionidt
sessionbyipv6(struct in6_addr ip
);
611 sessionidt
sessionbyuser(char *username
);
612 void random_data(uint8_t *buf
, int len
);
613 void sessionkill(sessionidt s
, char *reason
);
614 void sessionshutdown(sessionidt s
, char *reason
, int result
, int error
);
615 void send_garp(in_addr_t ip
);
616 void tunnelsend(uint8_t *buf
, uint16_t l
, tunnelidt t
);
617 void sendipcp(tunnelidt t
, sessionidt s
);
618 void processudp(uint8_t *buf
, int len
, struct sockaddr_in
*addr
);
619 void snoop_send_packet(char *packet
, uint16_t size
, in_addr_t destination
, uint16_t port
);
620 int ip_filter(uint8_t *buf
, int len
, uint8_t filter
);
621 int cmd_show_ipcache(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
622 int cmd_show_hist_idle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
623 int cmd_show_hist_open(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
627 #define LOG(D, s, t, f, ...) ({ if (D <= config->debug) _log(D, s, t, f, ## __VA_ARGS__); })
628 #define LOG_HEX(D, t, d, s) ({ if (D <= config->debug) _log_hex(D, t, d, s); })
630 void _log(int level
, sessionidt s
, tunnelidt t
, const char *format
, ...) __attribute__((format (printf
, 4, 5)));
631 void _log_hex(int level
, const char *title
, const char *data
, int maxsize
);
633 int sessionsetup(tunnelidt t
, sessionidt s
);
634 int run_plugins(int plugin_type
, void *data
);
635 void rebuild_address_pool(void);
636 void throttle_session(sessionidt s
, int rate_in
, int rate_out
);
637 int load_session(sessionidt
, sessiont
*);
638 void become_master(void); // We're the master; kick off any required master initializations.
642 void init_cli(char *hostname
);
643 void cli_do_file(FILE *fh
);
644 void cli_do(int sockfd
);
645 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...);
649 void host_unreachable(in_addr_t destination
, uint16_t id
, in_addr_t source
, char *packet
, int packet_len
);
652 extern tunnelt
*tunnel
;
653 extern sessiont
*session
;
654 extern sessionlocalt
*sess_local
;
655 extern ippoolt
*ip_address_pool
;
656 #define sessionfree (session[0].next)
658 #define log_backtrace(count, max) \
659 if (count++ < max) { \
663 LOG(0, 0, t, "Backtrace follows"); \
664 size = backtrace(array, 10); \
665 strings = backtrace_symbols(array, size); \
666 if (strings) for (i = 0; i < size; i++) \
668 LOG(0, 0, t, "%s\n", strings[i]); \
674 extern configt
*config
;
675 extern time_t basetime
; // Time when this process started.
676 extern time_t time_now
; // Seconds since EPOCH.
677 extern uint32_t last_id
;
678 extern struct Tstats
*_statistics
;
679 extern in_addr_t my_address
;
680 extern int tun_write(uint8_t *data
, int size
);
684 #define TIME (config->current_time)
686 // macros for handling help in cli commands
687 #define CLI_HELP_REQUESTED (argc > 0 && argv[argc-1][strlen(argv[argc-1])-1] == '?')
688 #define CLI_HELP_NO_ARGS (argc > 1 || argv[0][1]) ? CLI_OK : cli_arg_help(cli, 1, NULL)
690 // CVS identifiers (for "show version file")
691 extern char const *cvs_id_arp
;
692 extern char const *cvs_id_cli
;
693 extern char const *cvs_id_cluster
;
694 extern char const *cvs_id_constants
;
695 extern char const *cvs_id_control
;
696 extern char const *cvs_id_icmp
;
697 extern char const *cvs_id_l2tpns
;
698 extern char const *cvs_id_ll
;
699 extern char const *cvs_id_md5
;
700 extern char const *cvs_id_ppp
;
701 extern char const *cvs_id_radius
;
702 extern char const *cvs_id_tbf
;
703 extern char const *cvs_id_util
;
705 #endif /* __L2TPNS_H__ */