ba9a56f70333075cac366f650a607c7adde663af
[l2tpns.git] / l2tpns.h
1 // L2TPNS Global Stuff
2 // $Id: l2tpns.h,v 1.121 2009-12-08 14:49:28 bodea Exp $
3
4 #ifndef __L2TPNS_H__
5 #define __L2TPNS_H__
6
7 #include <netinet/in.h>
8 #include <stdio.h>
9 #include <signal.h>
10 #include <stdlib.h>
11 #include <netinet/in.h>
12 #include <sys/socket.h>
13 #include <arpa/inet.h>
14 #include <sys/types.h>
15 #include <libcli.h>
16
17 #define VERSION "2.2.1"
18
19 // Limits
20 #define MAXTUNNEL 500 // could be up to 65535
21 #define MAXBUNDLE 300 // could be up to 65535
22 #define MAXBUNDLESES 12 // Maximum number of member links in bundle
23 #define MAXADDRESS 20 // Maximum length for the Endpoint Discrminiator address
24 #define MAXSESSION 60000 // could be up to 65535
25 #define MAXTBFS 6000 // Maximum token bucket filters. Might need up to 2 * session.
26
27 #define RADIUS_SHIFT 6
28 #define RADIUS_FDS (1 << RADIUS_SHIFT)
29 #define RADIUS_MASK ((1 << RADIUS_SHIFT) - 1)
30 #define MAXRADIUS (1 << (8 + RADIUS_SHIFT))
31
32 #define T_UNDEF (0xffff) // A tunnel ID that won't ever be used. Mark session as undefined.
33 #define T_FREE (0) // A tunnel ID that won't ever be used. Mark session as free.
34
35 #define MAXCONTROL 1000 // max length control message we ever send...
36 #define MINMTU 576 // minimum recommended MTU (rfc1063)
37 #define MAXMTU 2600 // arbitrary maximum MTU
38 #define PPPoE_MRU 1492 // maximum PPPoE MRU (rfc2516: 1500 less PPPoE header (6) and PPP protocol ID (2))
39 #define MAXETHER (MAXMTU+18) // max packet we try sending to tun
40 #define MAXTEL 96 // telephone number
41 #define MAXHOSTNAME 256 // hostname
42 #define MAXUSER 128 // username
43 #define MAXPASS 128 // password
44 #define MAXCLASS 128 // radius class attribute size
45 #define MAXPLUGINS 20 // maximum number of plugins to load
46 #define MAXRADSERVER 10 // max radius servers
47 #define MAXROUTE 10 // max static routes per session
48 #define MAXIPPOOL 131072 // max number of ip addresses in pool
49 #define RINGBUFFER_SIZE 10000 // Number of ringbuffer entries to allocate
50 #define MAX_LOG_LENGTH 512 // Maximum size of log message
51 #define ECHO_TIMEOUT 10 // Time between last packet sent and LCP ECHO generation
52 #define IDLE_ECHO_TIMEOUT 240 // Time between last packet seen and session shutdown
53 #define BUSY_WAIT_TIME 3000 // 5 minutes in 1/10th seconds to wait for radius to cleanup on shutdown
54
55 #define MP_BEGIN 0x80 // This value is used when (b)egin bit is set in MP header
56 #define MP_END 0x40 // This value is used when (e)nd bit is set in MP header
57 #define MP_BOTH_BITS 0xC0 // This value is used when both bits (begin and end) are set in MP header
58
59 #define MINFRAGLEN 64 // Minumum fragment length
60 #define MAXFRAGLEN 1496 // Maximum length for Multilink fragment (The multilink may contain only one link)
61 #define MAXFRAGNUM 512 // Maximum number of Multilink fragment in a bundle (must be in the form of 2^X)
62 // it's not expected to have a space for more than 10 unassembled packets = 10 * MAXBUNDLESES
63 #define MAXFRAGNUM_MASK 511 // Must be equal to MAXFRAGNUM-1
64
65 // Constants
66 #ifndef ETCDIR
67 #define ETCDIR "/etc/l2tpns"
68 #endif
69
70 #ifndef LIBDIR
71 #define LIBDIR "/usr/lib/l2tpns"
72 #endif
73
74 #ifndef PLUGINDIR
75 #define PLUGINDIR LIBDIR // Plugins
76 #endif
77
78 #ifndef PLUGINCONF
79 #define PLUGINCONF ETCDIR // Plugin config dir
80 #endif
81
82 #ifndef FLASHDIR
83 #define FLASHDIR ETCDIR
84 #endif
85
86 #define TUNDEVICE "/dev/net/tun"
87 #define RANDOMDEVICE "/dev/urandom" // default, not as secure as /dev/random but non-blocking
88 #define CONFIGFILE FLASHDIR "/startup-config" // Configuration file
89 #define CLIUSERS FLASHDIR "/users" // CLI Users file
90 #define IPPOOLFILE FLASHDIR "/ip_pool" // Address pool configuration
91 #define ACCT_TIME 3000 // 5 minute accounting interval
92 #define ACCT_SHUT_TIME 600 // 1 minute for counters of shutdown sessions
93 #define L2TPPORT 1701 // L2TP port
94 #define RADPORT 1645 // old radius port...
95 #define DAEPORT 3799 // DAE port
96 #define PKTARP 0x0806 // ARP packet type
97 #define PKTIP 0x0800 // IPv4 packet type
98 #define PKTIPV6 0x86DD // IPv6 packet type
99 #define PPPPAP 0xC023
100 #define PPPCHAP 0xC223
101 #define PPPLCP 0xC021
102 #define PPPIPCP 0x8021
103 #define PPPIPV6CP 0x8057
104 #define PPPCCP 0x80FD
105 #define PPPIP 0x0021
106 #define PPPIPV6 0x0057
107 #define PPPMP 0x003D
108 #define MIN_IP_SIZE 0x19
109
110 enum {
111 ConfigReq = 1,
112 ConfigAck,
113 ConfigNak,
114 ConfigRej,
115 TerminateReq,
116 TerminateAck,
117 CodeRej,
118 ProtocolRej,
119 EchoReq,
120 EchoReply,
121 DiscardRequest,
122 IdentRequest
123 };
124
125 enum {
126 AccessRequest = 1,
127 AccessAccept,
128 AccessReject,
129 AccountingRequest,
130 AccountingResponse,
131 AccessChallenge = 11,
132 DisconnectRequest = 40,
133 DisconnectACK,
134 DisconnectNAK,
135 CoARequest,
136 CoAACK,
137 CoANAK
138 };
139
140 // PPP phases
141 enum {
142 Dead,
143 Establish,
144 Authenticate,
145 Network,
146 Terminate
147 };
148
149 // PPP states
150 enum {
151 Initial,
152 Starting,
153 Closed,
154 Stopped,
155 Closing,
156 Stopping,
157 RequestSent,
158 AckReceived,
159 AckSent,
160 Opened
161 };
162
163 // reset state machine counters
164 #define initialise_restart_count(_s, _fsm) \
165 sess_local[_s]._fsm.conf_sent = \
166 sess_local[_s]._fsm.nak_sent = 0
167
168 // no more attempts
169 #define zero_restart_count(_s, _fsm) ({ \
170 sess_local[_s]._fsm.conf_sent = \
171 config->ppp_max_configure; \
172 sess_local[_s]._fsm.restart = \
173 time_now + config->ppp_restart_time; \
174 })
175
176 // increment ConfReq counter and reset timer
177 #define restart_timer(_s, _fsm) ({ \
178 sess_local[_s]._fsm.conf_sent++; \
179 sess_local[_s]._fsm.restart = \
180 time_now + config->ppp_restart_time; \
181 })
182
183 // stop timer on change to state where timer does not run
184 #define change_state(_s, _fsm, _new) ({ \
185 if (_new != session[_s].ppp._fsm) \
186 { \
187 switch (_new) \
188 { \
189 case Initial: \
190 case Starting: \
191 case Closed: \
192 case Stopped: \
193 case Opened: \
194 sess_local[_s]._fsm.restart = 0; \
195 initialise_restart_count(_s, _fsm); \
196 } \
197 session[_s].ppp._fsm = _new; \
198 cluster_send_session(_s); \
199 } \
200 })
201
202 // Types
203 typedef uint16_t sessionidt;
204 typedef uint16_t bundleidt;
205 typedef uint16_t tunnelidt;
206 typedef uint32_t clockt;
207 typedef uint8_t hasht[16];
208
209 // CLI actions
210 struct cli_session_actions {
211 char action;
212 in_addr_t snoop_ip;
213 uint16_t snoop_port;
214 int throttle_in;
215 int throttle_out;
216 int filter_in;
217 int filter_out;
218 };
219
220 #define CLI_SESS_KILL 0x01
221 #define CLI_SESS_SNOOP 0x02
222 #define CLI_SESS_NOSNOOP 0x04
223 #define CLI_SESS_THROTTLE 0x08
224 #define CLI_SESS_NOTHROTTLE 0x10
225 #define CLI_SESS_FILTER 0x20
226 #define CLI_SESS_NOFILTER 0x40
227
228 struct cli_tunnel_actions {
229 char action;
230 };
231
232 #define CLI_TUN_KILL 0x01
233
234 // structures
235 typedef struct // route
236 {
237 in_addr_t ip;
238 int prefixlen;
239 }
240 routet;
241
242 typedef struct controls // control message
243 {
244 struct controls *next; // next in queue
245 uint16_t length; // length
246 union {
247 uint8_t buf[MAXCONTROL];
248 uint16_t buf16[MAXCONTROL/2];
249 } __attribute__ ((__transparent_union__));
250 }
251 controlt;
252
253 typedef struct {
254 uint8_t length; // Endpoint Discriminator length
255 uint8_t addr_class; // Endpoint Discriminator class
256 uint8_t address[MAXADDRESS]; // Endpoint Discriminator address
257 } epdist;
258
259 typedef struct {
260 sessionidt sid; // Fragment originating session
261 tunnelidt tid; // Fragment originating tunnel
262 uint8_t flags; // MP frame flags
263 uint32_t seq; // fragment seq num
264 uint32_t jitteravg;
265 uint16_t length; // Fragment length
266 uint8_t data[MAXFRAGLEN]; // Fragment data
267 } fragmentt;
268
269 typedef struct
270 {
271 sessionidt next; // next session in linked list
272 sessionidt far; // far end session ID
273 tunnelidt tunnel; // near end tunnel ID
274 uint8_t flags; // session flags: see SESSION_*
275 struct {
276 uint8_t phase; // PPP phase
277 uint8_t lcp:4; // LCP state
278 uint8_t ipcp:4; // IPCP state
279 uint8_t ipv6cp:4; // IPV6CP state
280 uint8_t ccp:4; // CCP state
281 } ppp;
282 uint16_t mru; // maximum receive unit
283 in_addr_t ip; // IP of session set by RADIUS response (host byte order).
284 int ip_pool_index; // index to IP pool
285 uint32_t unique_id; // unique session id
286 uint32_t magic; // ppp magic number
287 uint32_t pin, pout; // packet counts
288 uint32_t cin, cout; // byte counts
289 uint32_t cin_wrap, cout_wrap; // byte counter wrap count (RADIUS accounting giagawords)
290 uint32_t cin_delta, cout_delta; // byte count changes (for dump_session())
291 uint16_t throttle_in; // upstream throttle rate (kbps)
292 uint16_t throttle_out; // downstream throttle rate
293 uint8_t filter_in; // input filter index (to ip_filters[N-1]; 0 if none)
294 uint8_t filter_out; // output filter index
295 uint16_t snoop_port; // Interception destination port
296 in_addr_t snoop_ip; // Interception destination IP
297 clockt opened; // when started
298 clockt die; // being closed, when to finally free
299 uint32_t session_timeout; // Maximum session time in seconds
300 uint32_t idle_timeout; // Maximum idle time in seconds
301 time_t last_packet; // Last packet from the user (used for idle timeouts)
302 time_t last_data; // Last data packet to/from the user (used for idle timeouts)
303 in_addr_t dns1, dns2; // DNS servers
304 routet route[MAXROUTE]; // static routes
305 uint16_t tbf_in; // filter bucket for throttling in from the user.
306 uint16_t tbf_out; // filter bucket for throttling out to the user.
307 int random_vector_length;
308 uint8_t random_vector[MAXTEL];
309 char user[MAXUSER]; // user (needed in session for radius stop messages)
310 char called[MAXTEL]; // called number
311 char calling[MAXTEL]; // calling number
312 uint32_t tx_connect_speed;
313 uint32_t rx_connect_speed;
314 clockt timeout; // Session timeout
315 uint32_t mrru; // Multilink Max-Receive-Reconstructed-Unit
316 epdist epdis; // Multilink Endpoint Discriminator
317 bundleidt bundle; // Multilink Bundle Identifier
318 uint8_t mssf; // Multilink Short Sequence Number Header Format
319 uint8_t walled_garden; // is this session gardened?
320 uint8_t classlen; // class (needed for radius accounting messages)
321 char class[MAXCLASS];
322 uint8_t ipv6prefixlen; // IPv6 route prefix length
323 struct in6_addr ipv6route; // Static IPv6 route
324 #ifdef LAC
325 sessionidt forwardtosession; // LNS id_session to forward
326 char reserved[10]; // Space to expand structure without changing HB_VERSION
327 #else
328 char reserved[12]; // Space to expand structure without changing HB_VERSION
329 #endif
330 }
331 sessiont;
332
333 typedef struct
334 {
335 int state; // current state (bundlestate enum)
336 uint32_t seq_num_t; // Sequence Number (transmission)
337 uint32_t timeout; // Session-Timeout for bundle
338 uint32_t max_seq; // Max value of sequence number field
339 uint8_t num_of_links; // Number of links joint to this bundle
340 uint32_t online_time; // The time this bundle is online
341 clockt last_check; // Last time the timeout is checked
342 uint32_t mrru; // Multilink Max-Receive-Reconstructed-Unit
343 uint8_t mssf; // Multilink Short Sequence Number Header Format
344 epdist epdis; // Multilink Endpoint Discriminator
345 char user[MAXUSER]; // Needed for matching member links
346 sessionidt current_ses; // Current session to use for sending (used in RR load-balancing)
347 sessionidt members[MAXBUNDLESES]; // Array for member links sessions
348 }
349 bundlet;
350
351 typedef struct
352 {
353 fragmentt fragment[MAXFRAGNUM];
354 uint8_t reassembled_frame[MAXETHER]; // The reassembled frame
355 uint16_t re_frame_len; // The reassembled frame length
356 uint16_t re_frame_begin_index, re_frame_end_index; // reassembled frame begin index, end index respectively
357 uint16_t start_index, end_index; // start and end sequence numbers available on the fragments array respectively
358 uint32_t M; // Minumum frame sequence number received over all bundle members
359 uint32_t start_seq; // Last received frame sequence number (bearing B bit)
360 }
361 fragmentationt;
362
363 #define AUTHPAP 1 // allow PAP
364 #define AUTHCHAP 2 // allow CHAP
365
366 typedef struct
367 {
368 // packet counters
369 uint32_t pin;
370 uint32_t pout;
371
372 // byte counters
373 uint32_t cin;
374 uint32_t cout;
375
376 // PPP restart timer/counters
377 struct {
378 time_t restart;
379 int conf_sent;
380 int nak_sent;
381 } lcp, ipcp, ipv6cp, ccp;
382
383 // identifier for Protocol-Reject, Code-Reject
384 uint8_t lcp_ident;
385
386 // authentication to use
387 int lcp_authtype;
388
389 // our MRU
390 uint16_t ppp_mru;
391
392 // our MRRU
393 uint16_t mp_mrru;
394
395 // our mssf
396 uint16_t mp_mssf;
397
398 // our Endpoint Discriminator
399 in_addr_t mp_epdis;
400
401 // DoS prevention
402 clockt last_packet_out;
403 uint32_t packets_out;
404 uint32_t packets_dropped;
405
406 // RADIUS session in use
407 uint16_t radius;
408
409 // interim RADIUS
410 time_t last_interim;
411
412 // last LCP Echo
413 time_t last_echo;
414
415 // Last Multilink frame sequence number received
416 uint32_t last_seq;
417
418 // jitter average of the session
419 uint32_t jitteravg;
420 // time in milliseconds of the last fragment.
421 uint64_t prev_time;
422 } sessionlocalt;
423
424 // session flags
425 #define SESSION_PFC (1 << 0) // use Protocol-Field-Compression
426 #define SESSION_ACFC (1 << 1) // use Address-and-Control-Field-Compression
427 #define SESSION_STARTED (1 << 2) // RADIUS Start record sent
428
429 // 328 bytes per tunnel
430 typedef struct
431 {
432 tunnelidt far; // far end tunnel ID
433 in_addr_t ip; // Ip for far end
434 uint16_t port; // port for far end
435 uint16_t window; // Rx window
436 uint16_t nr; // next receive
437 uint16_t ns; // next send
438 int state; // current state (tunnelstate enum)
439 clockt last; // when last control message sent (used for resend timeout)
440 clockt retry; // when to try resending pending control
441 clockt die; // being closed, when to finally free
442 clockt lastrec; // when the last control message was received
443 char hostname[128]; // tunnel hostname
444 char vendor[128]; // LAC vendor
445 uint8_t try; // number of retrys on a control message
446 uint16_t controlc; // outstaind messages in queue
447 controlt *controls; // oldest message
448 controlt *controle; // newest message
449 #ifdef LAC
450 uint16_t isremotelns; // != 0 if the tunnel is to remote LNS (== index on the conf remote lns)
451 char reserved[14]; // Space to expand structure without changing HB_VERSION
452 #else
453 char reserved[16]; // Space to expand structure without changing HB_VERSION
454 #endif
455 }
456 tunnelt;
457
458 // 164 bytes per radius session
459 typedef struct // outstanding RADIUS requests
460 {
461 sessionidt session; // which session this applies to
462 hasht auth; // request authenticator
463 clockt retry; // when to try next
464 char pass[129]; // password
465 uint8_t id; // ID for PPP response
466 uint8_t try; // which try we are on
467 uint8_t state; // state of radius requests
468 uint8_t chap; // set if CHAP used (is CHAP identifier)
469 uint8_t term_cause; // Stop record: Acct-Terminate-Cause
470 char const *term_msg; // terminate reason
471 }
472 radiust;
473
474 typedef struct
475 {
476 in_addr_t address; // Host byte order..
477 char assigned; // 1 if assigned, 0 if free
478 sessionidt session;
479 clockt last; // last used
480 char user[129]; // user (try to have ip addresses persistent)
481 }
482 ippoolt;
483
484 #ifdef RINGBUFFER
485 struct Tringbuffer
486 {
487 struct {
488 char level;
489 sessionidt session;
490 tunnelidt tunnel;
491 char message[MAX_LOG_LENGTH];
492 } buffer[RINGBUFFER_SIZE];
493 int head;
494 int tail;
495 };
496 #endif
497
498 /*
499 * Possible tunnel states
500 * TUNNELFREE -> TUNNELOPEN -> TUNNELDIE -> TUNNELFREE
501 */
502 enum
503 {
504 TUNNELFREE, // Not in use
505 TUNNELOPEN, // Active tunnel
506 TUNNELDIE, // Currently closing
507 TUNNELOPENING, // Busy opening
508 TUNNELUNDEF, // Undefined
509 };
510
511 enum
512 {
513 BUNDLEFREE, // Not in use
514 BUNDLEOPEN, // Active bundle
515 BUNDLEUNDEF, // Undefined
516 };
517
518 enum
519 {
520 NULLCLASS = 0, //End Point Discriminator classes
521 LOCALADDR,
522 IPADDR,
523 IEEEMACADDR,
524 PPPMAGIC,
525 PSNDN,
526 };
527
528 enum
529 {
530 RADIUSNULL, // Not in use
531 RADIUSCHAP, // sending CHAP down PPP
532 RADIUSAUTH, // sending auth to RADIUS server
533 RADIUSSTART, // sending start accounting to RADIUS server
534 RADIUSSTOP, // sending stop accounting to RADIUS server
535 RADIUSINTERIM, // sending interim accounting to RADIUS server
536 RADIUSWAIT, // waiting timeout before available, in case delayed replies
537 RADIUSJUSTAUTH, // sending auth to RADIUS server, just authentication, no ip assigning
538 };
539
540 struct Tstats
541 {
542 time_t start_time;
543 time_t last_reset;
544
545 uint32_t tun_rx_packets;
546 uint32_t tun_tx_packets;
547 uint32_t tun_rx_bytes;
548 uint32_t tun_tx_bytes;
549 uint32_t tun_rx_errors;
550 uint32_t tun_tx_errors;
551 uint32_t tun_rx_dropped;
552
553 uint32_t tunnel_rx_packets;
554 uint32_t tunnel_tx_packets;
555 uint32_t tunnel_rx_bytes;
556 uint32_t tunnel_tx_bytes;
557 uint32_t tunnel_rx_errors;
558 uint32_t tunnel_tx_errors;
559
560 uint32_t tunnel_retries;
561 uint32_t radius_retries;
562
563 uint32_t arp_sent;
564
565 uint32_t packets_snooped;
566
567 uint32_t tunnel_created;
568 uint32_t session_created;
569 uint32_t tunnel_timeout;
570 uint32_t session_timeout;
571 uint32_t radius_timeout;
572 uint32_t radius_overflow;
573 uint32_t tunnel_overflow;
574 uint32_t session_overflow;
575
576 uint32_t ip_allocated;
577 uint32_t ip_freed;
578
579 uint32_t c_forwarded;
580 uint32_t recv_forward;
581
582 uint32_t select_called;
583 uint32_t multi_read_used;
584 uint32_t multi_read_exceeded;
585
586 #ifdef STATISTICS
587 uint32_t call_processtun;
588 uint32_t call_processipout;
589 uint32_t call_processipv6out;
590 uint32_t call_processudp;
591 uint32_t call_sessionbyip;
592 uint32_t call_sessionbyipv6;
593 uint32_t call_sessionbyuser;
594 uint32_t call_sendarp;
595 uint32_t call_sendipcp;
596 uint32_t call_sendipv6cp;
597 uint32_t call_processipv6cp;
598 uint32_t call_tunnelsend;
599 uint32_t call_sessionkill;
600 uint32_t call_sessionshutdown;
601 uint32_t call_tunnelkill;
602 uint32_t call_tunnelshutdown;
603 uint32_t call_assign_ip_address;
604 uint32_t call_free_ip_address;
605 uint32_t call_dump_acct_info;
606 uint32_t call_sessionsetup;
607 uint32_t call_processpap;
608 uint32_t call_processchap;
609 uint32_t call_processlcp;
610 uint32_t call_processipcp;
611 uint32_t call_processipin;
612 uint32_t call_processipv6in;
613 uint32_t call_processccp;
614 uint32_t call_sendchap;
615 uint32_t call_processrad;
616 uint32_t call_radiussend;
617 uint32_t call_radiusretry;
618 uint32_t call_random_data;
619 #endif
620 };
621
622 #ifdef STATISTICS
623
624 #ifdef STAT_CALLS
625 #define CSTAT(x) STAT(call_ ## x)
626 #else
627 #define CSTAT(x)
628 #endif
629
630 #define STAT(x) (_statistics->x++)
631 #define INC_STAT(x,y) (_statistics->x += (y))
632 #define GET_STAT(x) (_statistics->x)
633 #define SET_STAT(x, y) (_statistics->x = (y))
634 #else
635 #define CSTAT(x)
636 #define STAT(x)
637 #define INC_STAT(x,y)
638 #define GET_STAT(x) 0
639 #define SET_STAT(x, y)
640 #endif
641
642 #ifndef IFNAMSIZ
643 # define IFNAMSIZ 16
644 #endif
645
646 typedef struct
647 {
648 int debug; // debugging level
649 time_t start_time; // time when l2tpns was started
650 char bandwidth[256]; // current bandwidth
651 char pid_file[256]; // file to write PID to on startup
652 int wrote_pid;
653 clockt current_time; // 1/10ths of a second since the process started.
654 // means that we can only run a given process
655 // for 13 years without re-starting!
656
657 char config_file[128];
658 int reload_config; // flag to re-read config (set by cli)
659 int multi_read_count; // amount of packets to read per fd in processing loop
660
661 char tundevicename[IFNAMSIZ]; // tun device name
662 char log_filename[128];
663
664 char l2tp_secret[64]; // L2TP shared secret
665 int l2tp_mtu; // MTU of interface used for L2TP
666
667 char random_device[256]; // random device path, defaults to RANDOMDEVICE
668
669 int ppp_restart_time; // timeout for PPP restart
670 int ppp_max_configure; // max lcp configure requests to send
671 int ppp_max_failure; // max lcp configure naks to send
672
673 char radiussecret[64];
674 int radius_accounting;
675 int radius_interim;
676 in_addr_t radiusserver[MAXRADSERVER]; // radius servers
677 uint16_t radiusport[MAXRADSERVER]; // radius base ports
678 uint8_t numradiusservers; // radius server count
679
680 uint16_t radius_dae_port; // port for radius DAE
681 uint16_t radius_bind_min; // port range for udp sockets used to send/recv radius packets
682 uint16_t radius_bind_max;
683
684 char radius_authtypes_s[32]; // list of valid authentication types (chap, pap) in order of preference
685 int radius_authtypes;
686 int radius_authprefer;
687
688 int allow_duplicate_users; // allow multiple logins with the same username
689 int kill_timedout_sessions; // kill authenticated sessions with "session_timeout == 0"
690
691 in_addr_t default_dns1, default_dns2;
692
693 unsigned long rl_rate; // default throttle rate
694 int num_tbfs; // number of throttle buckets
695
696 char accounting_dir[128];
697 in_addr_t bind_address;
698 in_addr_t peer_address;
699 int send_garp; // Set to true to garp for vip address on startup
700
701 int dump_speed;
702 char plugins[64][MAXPLUGINS];
703 char old_plugins[64][MAXPLUGINS];
704
705 int next_tbf; // Next HTB id available to use
706 int scheduler_fifo; // If the system has multiple CPUs, use FIFO scheduling
707 // policy for this process.
708 int lock_pages; // Lock pages into memory.
709 int icmp_rate; // Max number of ICMP unreachable per second to send
710 int max_packets; // DoS prevention: per session limit of packets/0.1s
711 char epdis_addr[20]; // MP Endpoint Discriminator address
712
713 in_addr_t cluster_address; // Multicast address of cluster.
714 // Send to this address to have everyone hear.
715 char cluster_interface[64]; // Which interface to listen for multicast on.
716 int cluster_iam_master; // Are we the cluster master???
717 int cluster_iam_uptodate; // Set if we've got a full set of state from the master.
718 in_addr_t cluster_master_address; // The network address of the cluster master.
719 // Zero if i am the cluster master.
720 int cluster_seq_number; // Sequence number of the next heartbeat we'll send out
721 // (or the seq number we're next expecting if we're a slave).
722 int cluster_undefined_sessions; // How many sessions we're yet to receive from the master.
723 int cluster_undefined_bundles; // How many bundles we're yet to receive from the master.
724 int cluster_undefined_tunnels; // How many tunnels we're yet to receive from the master.
725 int cluster_highest_sessionid;
726 int cluster_highest_bundleid;
727 int cluster_highest_tunnelid;
728 clockt cluster_last_hb; // Last time we saw a heartbeat from the master.
729 int cluster_last_hb_ver; // Heartbeat version last seen from master
730 int cluster_num_changes; // Number of changes queued.
731
732 int cluster_mcast_ttl; // TTL for multicast packets
733 int cluster_hb_interval; // How often to send a heartbeat.
734 int cluster_hb_timeout; // How many missed heartbeats trigger an election.
735 uint64_t cluster_table_version; // # state changes processed by cluster
736
737 struct in6_addr ipv6_prefix; // Our IPv6 network pool.
738
739
740 int cluster_master_min_adv; // Master advertises routes while the number of up to date
741 // slaves is less than this value.
742 in_addr_t cli_bind_address; // bind address for CLI
743 char hostname[MAXHOSTNAME]; // hostname (overridden by -h on command line)
744 // Guest change
745 char guest_user[MAXUSER]; // Guest account username
746
747 #ifdef BGP
748 #define BGP_NUM_PEERS 2
749 uint16_t as_number;
750 struct {
751 char name[64];
752 uint16_t as;
753 int keepalive;
754 int hold;
755 struct in_addr update_source;
756 } neighbour[BGP_NUM_PEERS];
757 in_addr_t nexthop_address;
758 struct in6_addr nexthop6_address;
759 #endif
760
761 int echo_timeout; // Time between last packet sent and LCP ECHO generation
762 int idle_echo_timeout; // Time between last packet seen and
763 // Drop sessions who have not responded within IDLE_ECHO_TIMEOUT seconds
764 in_addr_t iftun_address;
765 #ifdef LAC
766 int disable_lac_func;
767 int highest_rlnsid;
768 uint16_t bind_portremotelns;
769 in_addr_t bind_address_remotelns;
770 #endif
771 } configt;
772
773 enum config_typet { INT, STRING, UNSIGNED_LONG, SHORT, BOOL, IPv4, IPv6 };
774 typedef struct
775 {
776 char *key;
777 int offset;
778 int size;
779 enum config_typet type;
780 } config_descriptt;
781
782 typedef struct
783 {
784 uint8_t op; // operation
785 #define FILTER_PORT_OP_NONE 0 // all ports match
786 #define FILTER_PORT_OP_EQ 1
787 #define FILTER_PORT_OP_NEQ 2
788 #define FILTER_PORT_OP_GT 3
789 #define FILTER_PORT_OP_LT 4
790 #define FILTER_PORT_OP_RANGE 5
791 uint16_t port; // port (host byte order)
792 uint16_t port2; // range
793 } ip_filter_portt;
794
795 typedef struct
796 {
797 int action; // permit/deny
798 #define FILTER_ACTION_DENY 1
799 #define FILTER_ACTION_PERMIT 2
800 uint8_t proto; // protocol: IPPROTO_* (netinet/in.h)
801 in_addr_t src_ip; // source ip (network byte order)
802 in_addr_t src_wild;
803 ip_filter_portt src_ports;
804 in_addr_t dst_ip; // dest ip
805 in_addr_t dst_wild;
806 ip_filter_portt dst_ports;
807 uint8_t frag; // apply to non-initial fragments
808 uint8_t tcp_flag_op; // match type: any, all, established
809 #define FILTER_FLAG_OP_ANY 1
810 #define FILTER_FLAG_OP_ALL 2
811 #define FILTER_FLAG_OP_EST 3
812 uint8_t tcp_sflags; // flags set
813 uint8_t tcp_cflags; // flags clear
814 uint32_t counter; // match count
815 } ip_filter_rulet;
816
817 #define TCP_FLAG_FIN 0x01
818 #define TCP_FLAG_SYN 0x02
819 #define TCP_FLAG_RST 0x04
820 #define TCP_FLAG_PSH 0x08
821 #define TCP_FLAG_ACK 0x10
822 #define TCP_FLAG_URG 0x20
823
824 #define MAXFILTER 32
825 #define MAXFILTER_RULES 32
826 typedef struct
827 {
828 char name[32]; // ACL name
829 int extended; // type: 0 = standard, 1 = extended
830 ip_filter_rulet rules[MAXFILTER_RULES];
831 int used; // session ref count
832 } ip_filtert;
833
834 // CDN result/error codes
835 #define CDN_NONE 0, 0
836 #define CDN_TRY_ANOTHER 2, 7
837 #define CDN_ADMIN_DISC 3, 0
838 #define CDN_UNAVAILABLE 4, 0
839
840 // RADIUS Acct-Terminate-Cause values
841 #define TERM_USER_REQUEST 1
842 #define TERM_LOST_CARRIER 2
843 #define TERM_LOST_SERVICE 3
844 #define TERM_IDLE_TIMEOUT 4
845 #define TERM_SESSION_TIMEOUT 5
846 #define TERM_ADMIN_RESET 6
847 #define TERM_ADMIN_REBOOT 7
848 #define TERM_PORT_ERROR 8
849 #define TERM_NAS_ERROR 9
850 #define TERM_NAS_REQUEST 10
851 #define TERM_NAS_REBOOT 11
852 #define TERM_PORT_UNNEEDED 12
853 #define TERM_PORT_PREEMPTED 13
854 #define TERM_PORT_SUSPENDED 14
855 #define TERM_SERVICE_UNAVAILABLE 15
856 #define TERM_CALLBACK 16
857 #define TERM_USER_ERROR 17
858 #define TERM_HOST_REQUEST 18
859 #define TERM_SUPPLICANT_RESTART 19
860 #define TERM_REAUTHENTICATION_FAILURE 20
861 #define TERM_PORT_REINIT 21
862 #define TERM_PORT_DISABLED 22
863
864 // on slaves, alow BGP to withdraw cleanly before exiting
865 #define QUIT_DELAY 5
866
867 // quit actions (master)
868 #define QUIT_FAILOVER 1 // SIGTERM: exit when all control messages have been acked (for cluster failover)
869 #define QUIT_SHUTDOWN 2 // SIGQUIT: shutdown sessions/tunnels, reject new connections
870
871 // arp.c
872 void sendarp(int ifr_idx, const unsigned char* mac, in_addr_t ip);
873
874
875 // ppp.c
876 void processpap(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l);
877 void processchap(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l);
878 void lcp_open(sessionidt s, tunnelidt t);
879 void lcp_restart(sessionidt s);
880 void processlcp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l);
881 void processipcp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l);
882 void processipv6cp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l);
883 void processipin(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l);
884 void processmpin(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l);
885 void processmpframe(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l, uint8_t extra);
886 void processipv6in(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l);
887 void processccp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l);
888 void sendchap(sessionidt s, tunnelidt t);
889 uint8_t *makeppp(uint8_t *b, int size, uint8_t *p, int l, sessionidt s, tunnelidt t, uint16_t mtype, uint8_t prio, bundleidt bid, uint8_t mp_bits);
890 void sendlcp(sessionidt s, tunnelidt t);
891 void send_ipin(sessionidt s, uint8_t *buf, int len);
892 void sendccp(sessionidt s, tunnelidt t);
893 void protoreject(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l, uint16_t proto);
894 int join_bundle(sessionidt s);
895
896
897 // radius.c
898 void initrad(void);
899 void radiussend(uint16_t r, uint8_t state);
900 void processrad(uint8_t *buf, int len, char socket_index);
901 void radiusretry(uint16_t r);
902 uint16_t radiusnew(sessionidt s);
903 void radiusclear(uint16_t r, sessionidt s);
904 void processdae(uint8_t *buf, int len, struct sockaddr_in *addr, int alen, struct in_addr *local);
905 #ifdef LAC
906 int rad_tunnel_pwdecode(uint8_t *pl2tpsecret, size_t *pl2tpsecretlen, const char *radiussecret, const uint8_t * auth);
907 #endif
908
909 // l2tpns.c
910 clockt backoff(uint8_t try);
911 void send_ipv6_ra(sessionidt s, tunnelidt t, struct in6_addr *ip);
912 void route6set(sessionidt s, struct in6_addr ip, int prefixlen, int add);
913 sessionidt sessionbyip(in_addr_t ip);
914 sessionidt sessionbyipv6(struct in6_addr ip);
915 sessionidt sessionbyuser(char *username);
916 void increment_counter(uint32_t *counter, uint32_t *wrap, uint32_t delta);
917 void random_data(uint8_t *buf, int len);
918 void sessionkill(sessionidt s, char *reason);
919 void sessionshutdown(sessionidt s, char const *reason, int cdn_result, int cdn_error, int term_cause);
920 void filter_session(sessionidt s, int filter_in, int filter_out);
921 void send_garp(in_addr_t ip);
922 void tunnelsend(uint8_t *buf, uint16_t l, tunnelidt t);
923 int tun_write(uint8_t *data, int size);
924 void adjust_tcp_mss(sessionidt s, tunnelidt t, uint8_t *buf, int len, uint8_t *tcp);
925 void sendipcp(sessionidt s, tunnelidt t);
926 void sendipv6cp(sessionidt s, tunnelidt t);
927 void processudp(uint8_t *buf, int len, struct sockaddr_in *addr);
928 void processipout(uint8_t *buf, int len);
929 void snoop_send_packet(uint8_t *packet, uint16_t size, in_addr_t destination, uint16_t port);
930 int find_filter(char const *name, size_t len);
931 int ip_filter(uint8_t *buf, int len, uint8_t filter);
932 int cmd_show_ipcache(struct cli_def *cli, char *command, char **argv, int argc);
933 int cmd_show_hist_idle(struct cli_def *cli, char *command, char **argv, int argc);
934 int cmd_show_hist_open(struct cli_def *cli, char *command, char **argv, int argc);
935 #ifdef LAC
936 tunnelidt lac_new_tunnel();
937 void lac_tunnelclear(tunnelidt t);
938 void lac_send_SCCRQ(tunnelidt t, uint8_t * auth, unsigned int auth_len);
939 void lac_send_ICRQ(tunnelidt t, sessionidt s);
940 void lac_tunnelshutdown(tunnelidt t, char *reason, int result, int error, char *msg);
941 #endif
942
943 #undef LOG
944 #undef LOG_HEX
945 #define LOG(D, s, t, f, ...) ({ if (D <= config->debug) _log(D, s, t, f, ## __VA_ARGS__); })
946 #define LOG_HEX(D, t, d, s) ({ if (D <= config->debug) _log_hex(D, t, d, s); })
947
948 void _log(int level, sessionidt s, tunnelidt t, const char *format, ...) __attribute__((format (printf, 4, 5)));
949 void _log_hex(int level, const char *title, const uint8_t *data, int maxsize);
950
951
952 int sessionsetup(sessionidt s, tunnelidt t);
953 int run_plugins(int plugin_type, void *data);
954 void rebuild_address_pool(void);
955 void throttle_session(sessionidt s, int rate_in, int rate_out);
956 int load_session(sessionidt, sessiont *);
957 void become_master(void); // We're the master; kick off any required master initializations.
958
959
960 // cli.c
961 void init_cli();
962 void cli_init_complete(char *hostname);
963 void cli_do_file(FILE *fh);
964 void cli_do(int sockfd);
965 int cli_arg_help(struct cli_def *cli, int cr_ok, char *entry, ...);
966
967
968 // icmp.c
969 void host_unreachable(in_addr_t destination, uint16_t id, in_addr_t source, uint8_t *packet, int packet_len);
970
971
972 extern tunnelt *tunnel;
973 extern bundlet *bundle;
974 extern sessiont *session;
975 extern sessionlocalt *sess_local;
976 extern ippoolt *ip_address_pool;
977 #define sessionfree (session[0].next)
978
979
980 extern configt *config;
981 extern time_t basetime; // Time when this process started.
982 extern time_t time_now; // Seconds since EPOCH.
983 extern char main_quit;
984 extern uint32_t last_id;
985 extern struct Tstats *_statistics;
986 extern in_addr_t my_address;
987 extern int clifd;
988 extern int epollfd;
989
990 struct event_data {
991 enum {
992 FD_TYPE_CLI,
993 FD_TYPE_CLUSTER,
994 FD_TYPE_TUN,
995 FD_TYPE_UDP,
996 FD_TYPE_CONTROL,
997 FD_TYPE_DAE,
998 FD_TYPE_RADIUS,
999 FD_TYPE_BGP,
1000 FD_TYPE_NETLINK,
1001 #ifdef LAC
1002 FD_TYPE_UDPLAC,
1003 #endif
1004 } type;
1005 int index; // for RADIUS, BGP
1006 };
1007
1008 #define TIME (config->current_time)
1009
1010 extern uint16_t MRU;
1011 extern uint16_t MSS;
1012
1013 // macros for handling help in cli commands
1014 #define CLI_HELP_REQUESTED (argc > 0 && argv[argc-1][strlen(argv[argc-1])-1] == '?')
1015 #define CLI_HELP_NO_ARGS (argc > 1 || argv[0][1]) ? CLI_OK : cli_arg_help(cli, 1, NULL)
1016
1017 #endif /* __L2TPNS_H__ */