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