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