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