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