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