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