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