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