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