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