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