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