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