2 // $Id: l2tpns.h,v 1.50 2004-12-18 01:20:05 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 50000 // could be up to 65535
23 #define MAXTBFS 6000 // Maximum token bucket filters. Might need up to 2 * session.
25 #define RADIUS_SHIFT 5
26 #define RADIUS_MASK ((unsigned short)(((unsigned short)~0) >> (16 - RADIUS_SHIFT)))
27 #define MAXRADIUS ((unsigned long)(1L << RADIUS_SHIFT) * 255)
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 MAXPLUGINS 20 // maximum number of plugins to load
36 #define MAXRADSERVER 10 // max radius servers
37 #define MAXROUTE 10 // max static routes per session
38 #define MAXIPPOOL 131072 // max number of ip addresses in pool
39 #define RINGBUFFER_SIZE 10000 // Number of ringbuffer entries to allocate
40 #define MAX_LOG_LENGTH 512 // Maximum size of log message
41 #define ECHO_TIMEOUT 60 // Time between last packet sent and LCP ECHO generation
42 #define IDLE_TIMEOUT 240 // Time between last packet sent and LCP ECHO generation
43 #define BUSY_WAIT_TIME 3000 // 5 minutes in 1/10th seconds to wait for radius to cleanup on shutdown
47 #define ETCDIR "/etc/l2tpns"
51 #define LIBDIR "/usr/lib/l2tpns"
55 #define STATEDIR "/var/lib/l2tpns"
59 #define PLUGINDIR LIBDIR // Plugins
63 #define PLUGINCONF ETCDIR // Plugin config dir
67 #define FLASHDIR ETCDIR
71 #define DATADIR STATEDIR
74 #define TUNDEVICE "/dev/net/tun"
75 #define STATEFILE DATADIR "/state.dump" // State dump file
76 #define CONFIGFILE FLASHDIR "/startup-config" // Configuration file
77 #define CLIUSERS FLASHDIR "/users" // CLI Users file
78 #define IPPOOLFILE FLASHDIR "/ip_pool" // Address pool configuration
79 #define ACCT_TIME 3000 // 5 minute accounting interval
80 #define ACCT_SHUT_TIME 600 // 1 minute for counters of shutdown sessions
81 #define L2TPPORT 1701 // L2TP port
82 #define RADPORT 1645 // old radius port...
83 #define PKTARP 0x0806 // ARP packet type
84 #define PKTIP 0x0800 // IPv4 packet type
85 #define PKTIPV6 0x86DD // IPv6 packet type
87 #define PPPCHAP 0xC223
89 #define PPPIPCP 0x8021
90 #define PPPIPV6CP 0x8057
93 #define PPPIPV6 0x0057
95 #define MIN_IP_SIZE 0x19
113 typedef uint16_t sessionidt
;
114 typedef uint16_t tunnelidt
;
115 typedef uint32_t clockt
;
116 typedef uint8_t hasht
[16];
119 struct cli_session_actions
{
129 #define CLI_SESS_KILL 0x01
130 #define CLI_SESS_SNOOP 0x02
131 #define CLI_SESS_NOSNOOP 0x04
132 #define CLI_SESS_THROTTLE 0x08
133 #define CLI_SESS_NOTHROTTLE 0x10
134 #define CLI_SESS_FILTER 0x20
135 #define CLI_SESS_NOFILTER 0x40
137 struct cli_tunnel_actions
{
141 #define CLI_TUN_KILL 0x01
143 // dump header: update number if internal format changes
144 #define DUMP_MAGIC "L2TPNS#" VERSION "#"
147 typedef struct // route
154 typedef struct controls
// control message
156 struct controls
*next
; // next in queue
157 uint16_t length
; // length
158 uint8_t buf
[MAXCONTROL
];
164 sessionidt next
; // next session in linked list
165 sessionidt far
; // far end session ID
166 tunnelidt tunnel
; // near end tunnel ID
167 in_addr_t ip
; // IP of session set by RADIUS response (host byte order).
168 int ip_pool_index
; // index to IP pool
169 unsigned long unique_id
; // unique session id
170 uint16_t nr
; // next receive
171 uint16_t ns
; // next send
172 uint32_t magic
; // ppp magic number
173 uint32_t cin
, cout
; // byte counts
174 uint32_t pin
, pout
; // packet counts
175 uint32_t total_cin
; // This counter is never reset while a session is open
176 uint32_t total_cout
; // This counter is never reset while a session is open
177 uint32_t id
; // session id
178 uint16_t throttle_in
; // upstream throttle rate (kbps)
179 uint16_t throttle_out
; // downstream throttle rate
180 clockt opened
; // when started
181 clockt die
; // being closed, when to finally free
182 time_t last_packet
; // Last packet from the user (used for idle timeouts)
183 in_addr_t dns1
, dns2
; // DNS servers
184 routet route
[MAXROUTE
]; // static routes
185 uint16_t radius
; // which radius session is being used (0 for not waiting on authentication)
186 uint16_t mru
; // maximum receive unit
187 uint16_t tbf_in
; // filter bucket for throttling in from the user.
188 uint16_t tbf_out
; // filter bucket for throttling out to the user.
189 uint8_t l2tp_flags
; // various bit flags from the ICCN on the l2tp tunnel.
190 uint8_t reserved_old_snoop
; // No longer used - remove at some time
191 uint8_t walled_garden
; // is this session gardened?
192 uint8_t flags1
; // additional flags (currently unused);
193 char random_vector
[MAXTEL
];
194 int random_vector_length
;
195 char user
[129]; // user (needed in seesion for radius stop messages) (can we reduce this? --mo)
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 uint32_t flags
; // Various session flags.
201 in_addr_t snoop_ip
; // Interception destination IP
202 uint16_t snoop_port
; // Interception destination port
203 uint16_t sid
; // near end session id.
204 uint8_t filter_in
; // input filter index (to ip_filters[N-1]; 0 if none)
205 uint8_t filter_out
; // output filter index
206 char reserved
[18]; // Space to expand structure without changing HB_VERSION
210 #define SF_IPCP_ACKED 1 // Has this session seen an IPCP Ack?
211 #define SF_LCP_ACKED 2 // LCP negotiated
212 #define SF_CCP_ACKED 4 // CCP negotiated
213 #define SF_IPV6CP_ACKED 8 // IPv6 negotiated
214 #define SF_IPV6_NACKED 16 // IPv6 rejected
222 #define SESSIONPFC 1 // PFC negotiated flags
223 #define SESSIONACFC 2 // ACFC negotiated flags
225 // 168 bytes per tunnel
228 tunnelidt far
; // far end tunnel ID
229 in_addr_t ip
; // Ip for far end
230 uint16_t port
; // port for far end
231 uint16_t window
; // Rx window
232 uint16_t nr
; // next receive
233 uint16_t ns
; // next send
234 int state
; // current state (tunnelstate enum)
235 clockt last
; // when last control message sent (used for resend timeout)
236 clockt retry
; // when to try resenting pending control
237 clockt die
; // being closed, when to finally free
238 clockt lastrec
; // when the last control message was received
239 char hostname
[128]; // tunnel hostname
240 char vendor
[128]; // LAC vendor
241 uint8_t try; // number of retrys on a control message
242 uint16_t controlc
; // outstaind messages in queue
243 controlt
*controls
; // oldest message
244 controlt
*controle
; // newest message
248 // 180 bytes per radius session
249 typedef struct // outstanding RADIUS requests
251 sessionidt session
; // which session this applies to
252 hasht auth
; // request authenticator
253 clockt retry
; // when to try next
254 char calling
[MAXTEL
]; // calling number
255 char pass
[129]; // password
256 uint8_t id
; // ID for PPP response
257 uint8_t try; // which try we are on
258 uint8_t state
; // state of radius requests
259 uint8_t chap
; // set if CHAP used (is CHAP identifier)
265 in_addr_t address
; // Host byte order..
266 char assigned
; // 1 if assigned, 0 if free
268 clockt last
; // last used
269 char user
[129]; // user (try to have ip addresses persistent)
280 char message
[MAX_LOG_LENGTH
];
281 } buffer
[RINGBUFFER_SIZE
];
288 * Possible tunnel states
289 * TUNNELFREE -> TUNNELOPEN -> TUNNELDIE -> TUNNELFREE
293 TUNNELFREE
, // Not in use
294 TUNNELOPEN
, // Active tunnel
295 TUNNELDIE
, // Currently closing
296 TUNNELOPENING
, // Busy opening
297 TUNNELUNDEF
, // Undefined
302 RADIUSNULL
, // Not in use
303 RADIUSCHAP
, // sending CHAP down PPP
304 RADIUSAUTH
, // sending auth to RADIUS server
305 RADIUSIPCP
, // sending IPCP to end user
306 RADIUSSTART
, // sending start accounting to RADIUS server
307 RADIUSSTOP
, // sending stop accounting to RADIUS server
308 RADIUSWAIT
, // waiting timeout before available, in case delayed replies
309 RADIUSDEAD
, // errored while talking to radius server.
317 uint32_t tun_rx_packets
;
318 uint32_t tun_tx_packets
;
319 uint32_t tun_rx_bytes
;
320 uint32_t tun_tx_bytes
;
321 uint32_t tun_rx_errors
;
322 uint32_t tun_tx_errors
;
324 uint32_t tunnel_rx_packets
;
325 uint32_t tunnel_tx_packets
;
326 uint32_t tunnel_rx_bytes
;
327 uint32_t tunnel_tx_bytes
;
328 uint32_t tunnel_rx_errors
;
329 uint32_t tunnel_tx_errors
;
331 uint32_t tunnel_retries
;
332 uint32_t radius_retries
;
336 uint32_t packets_snooped
;
338 uint32_t tunnel_created
;
339 uint32_t session_created
;
340 uint32_t tunnel_timeout
;
341 uint32_t session_timeout
;
342 uint32_t radius_timeout
;
343 uint32_t radius_overflow
;
344 uint32_t tunnel_overflow
;
345 uint32_t session_overflow
;
347 uint32_t ip_allocated
;
350 uint32_t c_forwarded
;
351 uint32_t recv_forward
;
353 uint32_t select_called
;
354 uint32_t multi_read_used
;
355 uint32_t multi_read_exceeded
;
358 uint32_t call_processtun
;
359 uint32_t call_processipout
;
360 uint32_t call_processudp
;
361 uint32_t call_sessionbyip
;
362 uint32_t call_sessionbyuser
;
363 uint32_t call_sendarp
;
364 uint32_t call_sendipcp
;
365 uint32_t call_tunnelsend
;
366 uint32_t call_sessionkill
;
367 uint32_t call_sessionshutdown
;
368 uint32_t call_tunnelkill
;
369 uint32_t call_tunnelshutdown
;
370 uint32_t call_assign_ip_address
;
371 uint32_t call_free_ip_address
;
372 uint32_t call_dump_acct_info
;
373 uint32_t call_sessionsetup
;
374 uint32_t call_processpap
;
375 uint32_t call_processchap
;
376 uint32_t call_processlcp
;
377 uint32_t call_processipcp
;
378 uint32_t call_processipin
;
379 uint32_t call_processccp
;
380 uint32_t call_sendchap
;
381 uint32_t call_processrad
;
382 uint32_t call_radiussend
;
383 uint32_t call_radiusretry
;
390 #define CSTAT(x) STAT(x)
395 #define STAT(x) (_statistics->x++)
396 #define INC_STAT(x,y) (_statistics->x += (y))
397 #define GET_STAT(x) (_statistics->x)
398 #define SET_STAT(x, y) (_statistics->x = (y))
402 #define INC_STAT(x,y)
403 #define GET_STAT(x) 0
404 #define SET_STAT(x, y)
409 int debug
; // debugging level
410 time_t start_time
; // time when l2tpns was started
411 char bandwidth
[256]; // current bandwidth
412 char pid_file
[256]; // file to write PID to on startup
414 clockt current_time
; // 1/10ths of a second since the process started.
415 // means that we can only run a given process
416 // for 13 years without re-starting!
418 char config_file
[128];
419 int reload_config
; // flag to re-read config (set by cli)
420 int cleanup_interval
; // interval between regular cleanups (in seconds)
421 int multi_read_count
; // amount of packets to read per fd in processing loop
423 char tundevice
[10]; // tun device name
424 char log_filename
[128];
427 char radiussecret
[64];
428 int radius_accounting
;
429 in_addr_t radiusserver
[MAXRADSERVER
]; // radius servers
430 uint16_t radiusport
[MAXRADSERVER
]; // radius base ports
431 uint8_t numradiusservers
; // radius server count
432 short num_radfds
; // Number of radius filehandles allocated
434 in_addr_t default_dns1
, default_dns2
;
436 unsigned long rl_rate
; // default throttle rate
437 int num_tbfs
; // number of throttle buckets
440 char accounting_dir
[128];
441 in_addr_t bind_address
;
442 in_addr_t peer_address
;
443 int send_garp
; // Set to true to garp for vip address on startup
447 char plugins
[64][MAXPLUGINS
];
448 char old_plugins
[64][MAXPLUGINS
];
450 int next_tbf
; // Next HTB id available to use
451 int scheduler_fifo
; // If the system has multiple CPUs, use FIFO scheduling policy for this process.
452 int lock_pages
; // Lock pages into memory.
453 int icmp_rate
; // Max number of ICMP unreachable per second to send>
455 in_addr_t cluster_address
; // Multicast address of cluster.
456 // Send to this address to have everyone hear.
457 char cluster_interface
[64]; // Which interface to listen for multicast on.
458 int cluster_iam_master
; // Are we the cluster master???
459 int cluster_iam_uptodate
; // Set if we've got a full set of state from the master.
460 in_addr_t cluster_master_address
; // The network address of the cluster master.
461 // Zero if i am the cluster master.
462 int cluster_seq_number
; // Sequence number of the next heartbeat we'll send out
463 // (or the seq number we're next expecting if we're a slave).
464 int cluster_undefined_sessions
; // How many sessions we're yet to receive from the master.
465 int cluster_undefined_tunnels
; // How many tunnels we're yet to receive from the master.
466 int cluster_highest_sessionid
;
467 int cluster_highest_tunnelid
;
468 clockt cluster_last_hb
; // Last time we saw a heartbeat from the master.
469 int cluster_num_changes
; // Number of changes queued.
471 int cluster_hb_interval
; // How often to send a heartbeat.
472 int cluster_hb_timeout
; // How many missed heartbeats trigger an election.
473 uint64_t cluster_table_version
; // # state changes processed by cluster
476 #define BGP_NUM_PEERS 2
483 } neighbour
[BGP_NUM_PEERS
];
487 enum config_typet
{ INT
, STRING
, UNSIGNED_LONG
, SHORT
, BOOL
, IPv4
, IPv6
, MAC
};
493 enum config_typet type
;
498 uint8_t op
; // operation
499 #define FILTER_PORT_OP_NONE 0 // all ports match
500 #define FILTER_PORT_OP_EQ 1
501 #define FILTER_PORT_OP_NEQ 2
502 #define FILTER_PORT_OP_GT 3
503 #define FILTER_PORT_OP_LT 4
504 #define FILTER_PORT_OP_RANGE 5
505 uint16_t port
; // port (host byte order)
506 uint16_t port2
; // range
511 int action
; // permit/deny
512 #define FILTER_ACTION_DENY 1
513 #define FILTER_ACTION_PERMIT 2
514 uint8_t proto
; // protocol: IPPROTO_* (netinet/in.h)
515 in_addr_t src_ip
; // source ip (network byte order)
517 ip_filter_portt src_ports
;
518 in_addr_t dst_ip
; // dest ip
520 ip_filter_portt dst_ports
;
521 uint8_t frag
; // apply to non-initial fragments
522 uint8_t tcp_flag_op
; // match type: any, all, established
523 #define FILTER_FLAG_OP_ANY 1
524 #define FILTER_FLAG_OP_ALL 2
525 #define FILTER_FLAG_OP_EST 3
526 uint8_t tcp_sflags
; // flags set
527 uint8_t tcp_cflags
; // flags clear
528 uint32_t counter
; // match count
531 #define TCP_FLAG_FIN 0x01
532 #define TCP_FLAG_SYN 0x02
533 #define TCP_FLAG_RST 0x04
534 #define TCP_FLAG_PSH 0x08
535 #define TCP_FLAG_ACK 0x10
536 #define TCP_FLAG_URG 0x20
539 #define MAXFILTER_RULES 32
542 char name
[32]; // ACL name
543 int extended
; // type: 0 = standard, 1 = extended
544 ip_filter_rulet rules
[MAXFILTER_RULES
];
545 int used
; // session ref count
549 void sendarp(int ifr_idx
, const unsigned char* mac
, in_addr_t ip
);
553 void processpap(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
554 void processchap(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
555 void processlcp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
556 void processipcp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
557 void processipin(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
558 void processccp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
559 void sendchap(tunnelidt t
, sessionidt s
);
560 uint8_t *makeppp(uint8_t *b
, int size
, uint8_t *p
, int l
, tunnelidt t
, sessionidt s
, uint16_t mtype
);
561 void initlcp(tunnelidt t
, sessionidt s
);
562 void send_ipin(sessionidt s
, uint8_t *buf
, int len
);
567 void radiussend(uint16_t r
, uint8_t state
);
568 void processrad(uint8_t *buf
, int len
, char socket_index
);
569 void radiusretry(uint16_t r
);
570 uint16_t radiusnew(sessionidt s
);
571 void radiusclear(uint16_t r
, sessionidt s
);
575 clockt
backoff(uint8_t try);
576 sessionidt
sessionbyip(in_addr_t ip
);
577 sessionidt
sessionbyuser(char *username
);
578 void sessionshutdown(sessionidt s
, char *reason
);
579 void send_garp(in_addr_t ip
);
580 void tunnelsend(uint8_t *buf
, uint16_t l
, tunnelidt t
);
581 void sendipcp(tunnelidt t
, sessionidt s
);
582 void processudp(uint8_t *buf
, int len
, struct sockaddr_in
*addr
);
583 void snoop_send_packet(char *packet
, uint16_t size
, in_addr_t destination
, uint16_t port
);
584 int ip_filter(uint8_t *buf
, int len
, uint8_t filter
);
585 int cmd_show_ipcache(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
586 int cmd_show_hist_idle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
587 int cmd_show_hist_open(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
591 #define LOG(D, s, t, f, ...) ({ if (D <= config->debug) _log(D, s, t, f, ## __VA_ARGS__); })
592 #define LOG_HEX(D, t, d, s) ({ if (D <= config->debug) _log_hex(D, t, d, s); })
594 void _log(int level
, sessionidt s
, tunnelidt t
, const char *format
, ...) __attribute__((format (printf
, 4, 5)));
595 void _log_hex(int level
, const char *title
, const char *data
, int maxsize
);
597 int sessionsetup(tunnelidt t
, sessionidt s
);
598 int run_plugins(int plugin_type
, void *data
);
599 void rebuild_address_pool(void);
600 void throttle_session(sessionidt s
, int rate_in
, int rate_out
);
601 int load_session(sessionidt
, sessiont
*);
602 void become_master(void); // We're the master; kick off any required master initializations.
606 void init_cli(char *hostname
);
607 void cli_do_file(FILE *fh
);
608 void cli_do(int sockfd
);
609 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...);
613 void host_unreachable(in_addr_t destination
, uint16_t id
, in_addr_t source
, char *packet
, int packet_len
);
616 extern tunnelt
*tunnel
;
617 extern sessiont
*session
;
618 extern sessioncountt
*sess_count
;
619 extern ippoolt
*ip_address_pool
;
620 #define sessionfree (session[0].next)
622 #define log_backtrace(count, max) \
623 if (count++ < max) { \
627 LOG(0, 0, t, "Backtrace follows"); \
628 size = backtrace(array, 10); \
629 strings = backtrace_symbols(array, size); \
630 if (strings) for (i = 0; i < size; i++) \
632 LOG(0, 0, t, "%s\n", strings[i]); \
638 extern configt
*config
;
639 extern time_t basetime
; // Time when this process started.
640 extern time_t time_now
; // Seconds since EPOCH.
641 extern uint32_t last_id
;
642 extern struct Tstats
*_statistics
;
643 extern in_addr_t my_address
;
644 extern int tun_write(uint8_t *data
, int size
);
648 #define TIME (config->current_time)
650 // macros for handling help in cli commands
651 #define CLI_HELP_REQUESTED (argc > 0 && argv[argc-1][strlen(argv[argc-1])-1] == '?')
652 #define CLI_HELP_NO_ARGS (argc > 1 || argv[0][1]) ? CLI_OK : cli_arg_help(cli, 1, NULL)
654 // CVS identifiers (for "show version file")
655 extern char const *cvs_id_arp
;
656 extern char const *cvs_id_cli
;
657 extern char const *cvs_id_cluster
;
658 extern char const *cvs_id_constants
;
659 extern char const *cvs_id_control
;
660 extern char const *cvs_id_icmp
;
661 extern char const *cvs_id_l2tpns
;
662 extern char const *cvs_id_ll
;
663 extern char const *cvs_id_md5
;
664 extern char const *cvs_id_ppp
;
665 extern char const *cvs_id_radius
;
666 extern char const *cvs_id_tbf
;
667 extern char const *cvs_id_util
;
669 #endif /* __L2TPNS_H__ */