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