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