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