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