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