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