2 // $Id: l2tpns.h,v 1.42 2004/11/30 19:37:46 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.0.10"
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 L2TPPORT 1701 // L2TP port
81 #define RADPORT 1645 // old radius port...
82 #define PKTARP 0x0806 // ARP packet type
83 #define PKTIP 0x0800 // IP packet type
84 #define PSEUDOMAC 0x0200 // pseudo MAC prefix (local significant MAC)
86 #define PPPCHAP 0xC223
88 #define PPPIPCP 0x8021
92 #define MIN_IP_SIZE 0x19
110 typedef unsigned short u16
;
111 typedef unsigned int u32
;
112 typedef unsigned char u8
;
115 typedef u16 sessionidt
;
116 typedef u16 tunnelidt
;
118 typedef u8 hasht
[16];
121 struct cli_session_actions
{
131 #define CLI_SESS_KILL 0x01
132 #define CLI_SESS_SNOOP 0x02
133 #define CLI_SESS_NOSNOOP 0x04
134 #define CLI_SESS_THROTTLE 0x08
135 #define CLI_SESS_NOTHROTTLE 0x10
136 #define CLI_SESS_FILTER 0x20
137 #define CLI_SESS_NOFILTER 0x40
139 struct cli_tunnel_actions
{
143 #define CLI_TUN_KILL 0x01
145 // dump header: update number if internal format changes
146 #define DUMP_MAGIC "L2TPNS#" VERSION "#"
149 typedef struct // route
156 typedef struct controls
// control message
158 struct controls
*next
; // next in queue
159 u16 length
; // length
166 sessionidt next
; // next session in linked list
167 sessionidt far
; // far end session ID
168 tunnelidt tunnel
; // near end tunnel ID
169 ipt ip
; // IP of session set by RADIUS response (host byte order).
170 int ip_pool_index
; // index to IP pool
171 unsigned long unique_id
; // unique session id
172 u16 nr
; // next receive
174 u32 magic
; // ppp magic number
175 u32 cin
, cout
; // byte counts
176 u32 pin
, pout
; // packet counts
177 u32 total_cin
; // This counter is never reset while a session is open
178 u32 total_cout
; // This counter is never reset while a session is open
179 u32 id
; // session id
180 u16 throttle_in
; // upstream throttle rate (kbps)
181 u16 throttle_out
; // downstream throttle rate
182 clockt opened
; // when started
183 clockt die
; // being closed, when to finally free
184 time_t last_packet
; // Last packet from the user (used for idle timeouts)
185 ipt dns1
, dns2
; // DNS servers
186 routet route
[MAXROUTE
]; // static routes
187 u16 radius
; // which radius session is being used (0 for not waiting on authentication)
188 u16 mru
; // maximum receive unit
189 u16 tbf_in
; // filter bucket for throttling in from the user.
190 u16 tbf_out
; // filter bucket for throttling out to the user.
191 u8 l2tp_flags
; // various bit flags from the ICCN on the l2tp tunnel.
192 u8 reserved_old_snoop
; // No longer used - remove at some time
193 u8 walled_garden
; // is this session gardened?
194 u8 flags1
; // additional flags (currently unused);
195 char random_vector
[MAXTEL
];
196 int random_vector_length
;
197 char user
[129]; // user (needed in seesion for radius stop messages) (can we reduce this? --mo)
198 char called
[MAXTEL
]; // called number
199 char calling
[MAXTEL
]; // calling number
200 u32 tx_connect_speed
;
201 u32 rx_connect_speed
;
202 u32 flags
; // Various session flags.
203 ipt snoop_ip
; // Interception destination IP
204 u16 snoop_port
; // Interception destination port
205 u16 sid
; // near end session id.
206 u8 filter_in
; // input filter index (to ip_filters[N-1]; 0 if none)
207 u8 filter_out
; // output filter index
208 char reserved
[18]; // Space to expand structure without changing HB_VERSION
212 #define SF_IPCP_ACKED 1 // Has this session seen an IPCP Ack?
213 #define SF_LCP_ACKED 2 // LCP negotiated
214 #define SF_CCP_ACKED 4 // CCP negotiated
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 ipt ip
; // Ip for far end
230 portt port
; // port for far end
231 u16 window
; // Rx window
232 u16 nr
; // next receive
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 u8
try; // number of retrys on a control message
242 u16 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 u8 id
; // ID for PPP response
257 u8
try; // which try we are on
258 u8 state
; // state of radius requests
259 u8 chap
; // set if CHAP used (is CHAP identifier)
265 ipt 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 unsigned long tun_rx_packets
;
318 unsigned long tun_tx_packets
;
319 unsigned long tun_rx_bytes
;
320 unsigned long tun_tx_bytes
;
321 unsigned long tun_rx_errors
;
322 unsigned long tun_tx_errors
;
324 unsigned long tunnel_rx_packets
;
325 unsigned long tunnel_tx_packets
;
326 unsigned long tunnel_rx_bytes
;
327 unsigned long tunnel_tx_bytes
;
328 unsigned long tunnel_rx_errors
;
329 unsigned long tunnel_tx_errors
;
331 unsigned long tunnel_retries
;
332 unsigned long radius_retries
;
334 unsigned long arp_sent
;
336 unsigned long packets_snooped
;
338 unsigned long tunnel_created
;
339 unsigned long session_created
;
340 unsigned long tunnel_timeout
;
341 unsigned long session_timeout
;
342 unsigned long radius_timeout
;
343 unsigned long radius_overflow
;
344 unsigned long tunnel_overflow
;
345 unsigned long session_overflow
;
347 unsigned long ip_allocated
;
348 unsigned long ip_freed
;
350 unsigned long c_forwarded
;
351 unsigned long recv_forward
;
353 unsigned long call_processtun
;
354 unsigned long call_processipout
;
355 unsigned long call_processudp
;
356 unsigned long call_sessionbyip
;
357 unsigned long call_sessionbyuser
;
358 unsigned long call_sendarp
;
359 unsigned long call_sendipcp
;
360 unsigned long call_tunnelsend
;
361 unsigned long call_sessionkill
;
362 unsigned long call_sessionshutdown
;
363 unsigned long call_tunnelkill
;
364 unsigned long call_tunnelshutdown
;
365 unsigned long call_assign_ip_address
;
366 unsigned long call_free_ip_address
;
367 unsigned long call_dump_acct_info
;
368 unsigned long call_sessionsetup
;
369 unsigned long call_processpap
;
370 unsigned long call_processchap
;
371 unsigned long call_processlcp
;
372 unsigned long call_processipcp
;
373 unsigned long call_processipin
;
374 unsigned long call_processccp
;
375 unsigned long call_sendchap
;
376 unsigned long call_processrad
;
377 unsigned long call_radiussend
;
378 unsigned long call_radiusretry
;
385 #define CSTAT(x) STAT(x)
390 #define STAT(x) (_statistics->x++)
391 #define INC_STAT(x,y) (_statistics->x += (y))
392 #define GET_STAT(x) (_statistics->x)
393 #define SET_STAT(x, y) (_statistics->x = (y))
397 #define INC_STAT(x,y)
398 #define GET_STAT(x) 0
399 #define SET_STAT(x, y)
404 int debug
; // debugging level
405 time_t start_time
; // time when l2tpns was started
406 char bandwidth
[256]; // current bandwidth
407 char pid_file
[256]; // file to write PID to on startup
409 clockt current_time
; // 1/10ths of a second since the process started.
410 // means that we can only run a given process
411 // for 13 years without re-starting!
413 char config_file
[128];
414 int reload_config
; // flag to re-read config (set by cli)
415 int cleanup_interval
; // interval between regular cleanups (in seconds)
416 int multi_read_count
; // amount of packets to read per fd in processing loop
418 char tundevice
[10]; // tun device name
419 char log_filename
[128];
422 char radiussecret
[64];
423 int radius_accounting
;
424 ipt radiusserver
[MAXRADSERVER
]; // radius servers
425 u16 radiusport
[MAXRADSERVER
]; // radius base ports
426 u8 numradiusservers
; // radius server count
427 short num_radfds
; // Number of radius filehandles allocated
429 ipt default_dns1
, default_dns2
;
431 unsigned long rl_rate
; // default throttle rate
432 int num_tbfs
; // number of throttle buckets
435 char accounting_dir
[128];
438 int send_garp
; // Set to true to garp for vip address on startup
442 char plugins
[64][MAXPLUGINS
];
443 char old_plugins
[64][MAXPLUGINS
];
445 int next_tbf
; // Next HTB id available to use
446 int scheduler_fifo
; // If the system has multiple CPUs, use FIFO scheduling policy for this process.
447 int lock_pages
; // Lock pages into memory.
448 int icmp_rate
; // Max number of ICMP unreachable per second to send>
450 u32 cluster_address
; // Multicast address of cluster.
451 // Send to this address to have everyone hear.
452 char cluster_interface
[64]; // Which interface to listen for multicast on.
453 int cluster_iam_master
; // Are we the cluster master???
454 int cluster_iam_uptodate
; // Set if we've got a full set of state from the master.
455 u32 cluster_master_address
; // The network address of the cluster master.
456 // Zero if i am the cluster master.
457 int cluster_seq_number
; // Sequence number of the next heartbeat we'll send out
458 // (or the seq number we're next expecting if we're a slave).
459 int cluster_undefined_sessions
; // How many sessions we're yet to receive from the master.
460 int cluster_undefined_tunnels
; // How many tunnels we're yet to receive from the master.
461 int cluster_highest_sessionid
;
462 int cluster_highest_tunnelid
;
463 clockt cluster_last_hb
; // Last time we saw a heartbeat from the master.
464 int cluster_num_changes
; // Number of changes queued.
466 int cluster_hb_interval
; // How often to send a heartbeat.
467 int cluster_hb_timeout
; // How many missed heartbeats trigger an election.
470 #define BGP_NUM_PEERS 2
477 } neighbour
[BGP_NUM_PEERS
];
481 enum config_typet
{ INT
, STRING
, UNSIGNED_LONG
, SHORT
, BOOL
, IP
, MAC
};
487 enum config_typet type
;
493 #define FILTER_PORT_OP_NONE 0 // all ports match
494 #define FILTER_PORT_OP_EQ 1
495 #define FILTER_PORT_OP_NEQ 2
496 #define FILTER_PORT_OP_GT 3
497 #define FILTER_PORT_OP_LT 4
498 #define FILTER_PORT_OP_RANGE 5
499 portt port
; // port (host byte order)
500 portt port2
; // range
505 int action
; // permit/deny
506 #define FILTER_ACTION_DENY 1
507 #define FILTER_ACTION_PERMIT 2
508 u8 proto
; // protocol: IPPROTO_* (netinet/in.h)
509 ipt src_ip
; // source ip (network byte order)
511 ip_filter_portt src_ports
;
512 ipt dst_ip
; // dest ip
514 ip_filter_portt dst_ports
;
515 u8 frag
; // apply to non-initial fragments
516 u8 tcp_flag_op
; // match type: any, all, established
517 #define FILTER_FLAG_OP_ANY 1
518 #define FILTER_FLAG_OP_ALL 2
519 #define FILTER_FLAG_OP_EST 3
520 u8 tcp_sflags
; // flags set
521 u8 tcp_cflags
; // flags clear
522 u32 counter
; // match count
525 #define TCP_FLAG_FIN 0x01
526 #define TCP_FLAG_SYN 0x02
527 #define TCP_FLAG_RST 0x04
528 #define TCP_FLAG_PSH 0x08
529 #define TCP_FLAG_ACK 0x10
530 #define TCP_FLAG_URG 0x20
533 #define MAXFILTER_RULES 32
536 char name
[32]; // ACL name
537 int extended
; // type: 0 = standard, 1 = extended
538 ip_filter_rulet rules
[MAXFILTER_RULES
];
539 int used
; // session ref count
543 void sendarp(int ifr_idx
, const unsigned char* mac
, ipt ip
);
547 void processpap(tunnelidt t
, sessionidt s
, u8
* p
, u16 l
);
548 void processchap(tunnelidt t
, sessionidt s
, u8
* p
, u16 l
);
549 void processlcp(tunnelidt t
, sessionidt s
, u8
* p
, u16 l
);
550 void processipcp(tunnelidt t
, sessionidt s
, u8
* p
, u16 l
);
551 void processipin(tunnelidt t
, sessionidt s
, u8
* p
, u16 l
);
552 void processccp(tunnelidt t
, sessionidt s
, u8
* p
, u16 l
);
553 void sendchap(tunnelidt t
, sessionidt s
);
554 u8
*makeppp(u8
* b
, int size
, u8
* p
, int l
, tunnelidt t
, sessionidt s
, u16 mtype
);
555 void initlcp(tunnelidt t
, sessionidt s
);
556 void send_ipin(sessionidt s
, u8
* buf
, int len
);
561 void radiussend(u16 r
, u8 state
);
562 void processrad(u8
*buf
, int len
, char socket_index
);
563 void radiusretry(u16 r
);
564 u16
radiusnew(sessionidt s
);
565 void radiusclear(u16 r
, sessionidt s
);
569 clockt
backoff(u8
try);
570 sessionidt
sessionbyip(ipt ip
);
571 sessionidt
sessionbyuser(char *username
);
572 void sessionshutdown(sessionidt s
, char *reason
);
573 void send_garp(ipt ip
);
574 void tunnelsend(u8
* buf
, u16 l
, tunnelidt t
);
575 void sendipcp(tunnelidt t
, sessionidt s
);
576 void processudp(u8
* buf
, int len
, struct sockaddr_in
*addr
);
577 void snoop_send_packet(char *packet
, u16 size
, ipt destination
, u16 port
);
578 int ip_filter(u8
*buf
, int len
, u8 filter
);
579 int cmd_show_ipcache(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
580 int cmd_show_hist_idle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
581 int cmd_show_hist_open(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
585 #define LOG(D, s, t, f, ...) ({ if (D <= config->debug) _log(D, s, t, f, ## __VA_ARGS__); })
586 #define LOG_HEX(D, t, d, s) ({ if (D <= config->debug) _log_hex(D, t, d, s); })
588 void _log(int level
, sessionidt s
, tunnelidt t
, const char *format
, ...) __attribute__((format (printf
, 4, 5)));
589 void _log_hex(int level
, const char *title
, const char *data
, int maxsize
);
591 int sessionsetup(tunnelidt t
, sessionidt s
);
592 int run_plugins(int plugin_type
, void *data
);
593 void rebuild_address_pool(void);
594 void throttle_session(sessionidt s
, int rate_in
, int rate_out
);
595 int load_session(sessionidt
, sessiont
*);
596 void become_master(void); // We're the master; kick off any required master initializations.
600 void init_cli(char *hostname
);
601 void cli_do_file(FILE *fh
);
602 void cli_do(int sockfd
);
603 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...);
607 void host_unreachable(ipt destination
, u16 id
, ipt source
, char *packet
, int packet_len
);
610 extern tunnelt
*tunnel
;
611 extern sessiont
*session
;
612 extern sessioncountt
*sess_count
;
613 extern ippoolt
*ip_address_pool
;
614 #define sessionfree (session[0].next)
616 #define log_backtrace(count, max) \
617 if (count++ < max) { \
621 LOG(0, 0, t, "Backtrace follows"); \
622 size = backtrace(array, 10); \
623 strings = backtrace_symbols(array, size); \
624 if (strings) for (i = 0; i < size; i++) \
626 LOG(0, 0, t, "%s\n", strings[i]); \
632 extern configt
*config
;
633 extern time_t basetime
; // Time when this process started.
634 extern time_t time_now
; // Seconds since EPOCH.
636 extern struct Tstats
*_statistics
;
637 extern ipt my_address
;
638 extern int tun_write(u8
*data
, int size
);
642 #define TIME (config->current_time)
644 // macros for handling help in cli commands
645 #define CLI_HELP_REQUESTED (argc > 0 && argv[argc-1][strlen(argv[argc-1])-1] == '?')
646 #define CLI_HELP_NO_ARGS (argc > 1 || argv[0][1]) ? CLI_OK : cli_arg_help(cli, 1, NULL)
648 // CVS identifiers (for "show version file")
649 extern char const *cvs_id_arp
;
650 extern char const *cvs_id_cli
;
651 extern char const *cvs_id_cluster
;
652 extern char const *cvs_id_constants
;
653 extern char const *cvs_id_control
;
654 extern char const *cvs_id_icmp
;
655 extern char const *cvs_id_l2tpns
;
656 extern char const *cvs_id_ll
;
657 extern char const *cvs_id_md5
;
658 extern char const *cvs_id_ppp
;
659 extern char const *cvs_id_radius
;
660 extern char const *cvs_id_tbf
;
661 extern char const *cvs_id_util
;
663 #endif /* __L2TPNS_H__ */