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