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