roll in Michael's "limp along" fix for when a slave drops temporarily from the mcast...
[l2tpns.git] / l2tpns.h
1 // L2TPNS Global Stuff
2 // $Id: l2tpns.h,v 1.9 2004-07-02 07:31:23 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.0.0"
19
20 // Limits
21 #define MAXTUNNEL 500 // could be up to 65535
22 #define MAXSESSION 50000 // 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 ((2 << (RADIUS_SHIFT - 1)) * 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 tap
34 #define MAXTEL 96 // telephone number
35 #define MAXPLUGINS 20 // maximum number of plugins to load
36 #define MAXRADSERVER 10 // max radius servers
37 #define MAXROUTE 10 // max static routes per session
38 #define MAXIPPOOL 131072 // max number of ip addresses in pool
39 #define RINGBUFFER_SIZE 10000 // Number of ringbuffer entries to allocate
40 #define MAX_LOG_LENGTH 512 // Maximum size of log message
41 #define ECHO_TIMEOUT 60 // Time between last packet sent and LCP ECHO generation
42 #define IDLE_TIMEOUT 240 // Time between last packet sent and LCP ECHO generation
43
44 // Constants
45 #include "config.h"
46 #ifndef PLUGINDIR
47 #define PLUGINDIR LIBDIR // Plugins
48 #endif
49
50 #ifndef PLUGINCONF
51 #define PLUGINCONF ETCDIR // Plugin config dir
52 #endif
53
54 #ifndef DATADIR
55 #define DATADIR "/tmp"
56 #endif
57
58 #ifndef FLASHDIR
59 #define FLASHDIR ETCDIR
60 #endif
61
62 #define TAPDEVICE "/dev/net/tun"
63 #define UDP 17
64 #define STATEFILE DATADIR "/state.dump" // State dump file
65 #define CONFIGFILE FLASHDIR "/startup-config" // Configuration file
66 #define CLIUSERS FLASHDIR "/users" // CLI Users file
67 #define IPPOOLFILE FLASHDIR "/ip_pool" // Address pool configuration
68 #define ACCT_TIME 3000 // 5 minute accounting interval
69 #define L2TPPORT 1701 // L2TP port
70 #define RADPORT 1645 // old radius port...
71 #define RADAPORT 1646 // old radius accounting port
72 #define PKTARP 0x0806 // ARP packet type
73 #define PKTIP 0x0800 // IP packet type
74 #define PSEUDOMAC 0x0200 // pseudo MAC prefix (local significant MAC)
75 #define PPPPAP 0xC023
76 #define PPPCHAP 0xC223
77 #define PPPLCP 0xC021
78 #define PPPIPCP 0x8021
79 #define PPPCCP 0x80FD
80 #define PPPIP 0x0021
81 #define PPPMP 0x003D
82 #define MIN_IP_SIZE 0x19
83 enum
84 {
85 ConfigReq = 1,
86 ConfigAck,
87 ConfigNak,
88 ConfigRej,
89 TerminateReq,
90 TerminateAck,
91 CodeRej,
92 ProtocolRej,
93 EchoReq,
94 EchoReply,
95 DiscardRequest
96 };
97
98 // Types
99 typedef unsigned short u16;
100 typedef unsigned int u32;
101 typedef unsigned char u8;
102 typedef u32 ipt;
103 typedef u16 portt;
104 typedef u16 sessionidt;
105 typedef u16 tunnelidt;
106 typedef u32 clockt;
107 typedef u8 hasht[16];
108
109 // dump header: update number if internal format changes
110 #define DUMP_MAGIC "L2TPNS#" VERSION "#"
111
112 // structures
113 typedef struct routes // route
114 {
115 ipt ip;
116 ipt mask;
117 }
118 routet;
119
120 typedef struct controls // control message
121 {
122 struct controls *next; // next in queue
123 u16 length; // length
124 u8 buf[MAXCONTROL];
125 }
126 controlt;
127
128 typedef struct sessions
129 {
130 sessionidt next; // next session in linked list
131 sessionidt far; // far end session ID
132 tunnelidt tunnel; // tunnel ID
133 ipt ip; // IP of session set by RADIUS response (host byte order).
134 int ip_pool_index; // index to IP pool
135 unsigned long sid; // session id for hsddb
136 u16 nr; // next receive
137 u16 ns; // next send
138 u32 magic; // ppp magic number
139 u32 cin, cout; // byte counts
140 u32 pin, pout; // packet counts
141 u32 total_cin; // This counter is never reset while a session is open
142 u32 total_cout; // This counter is never reset while a session is open
143 u32 id; // session id
144 u32 throttle; // non-zero if this session is throttled.
145 clockt opened; // when started
146 clockt die; // being closed, when to finally free
147 time_t last_packet; // Last packet from the user (used for idle timeouts)
148 ipt dns1, dns2; // DNS servers
149 routet route[MAXROUTE]; // static routes
150 u16 radius; // which radius session is being used (0 for not waiting on authentication)
151 u16 mru; // maximum receive unit
152 u16 tbf_in; // filter bucket for throttling in from the user.
153 u16 tbf_out; // filter bucket for throttling out to the user.
154 u8 l2tp_flags; // various bit flags from the ICCN on the l2tp tunnel.
155 u8 walled_garden; // is this session gardened?
156 u8 flags1; // additional flags (currently unused);
157 char random_vector[MAXTEL];
158 int random_vector_length;
159 char user[129]; // user (needed in seesion for radius stop messages) (can we reduce this? --mo)
160 char called[MAXTEL]; // called number
161 char calling[MAXTEL]; // calling number
162 u32 tx_connect_speed;
163 u32 rx_connect_speed;
164 u32 flags; // Various session flags.
165 ipt snoop_ip; // Interception destination IP
166 u16 snoop_port; // Interception destination port
167 char reserved[28]; // Space to expand structure without changing HB_VERSION
168 }
169 sessiont;
170
171 #define SF_IPCP_ACKED (1<<0) // Has this session seen an IPCP Ack?
172
173 typedef struct {
174 u32 cin;
175 u32 cout;
176 } sessioncountt;
177
178 #define SESSIONPFC 1 // PFC negotiated flags
179 #define SESSIONACFC 2 // ACFC negotiated flags
180
181 // 168 bytes per tunnel
182 typedef struct tunnels
183 {
184 tunnelidt far; // far end tunnel ID
185 ipt ip; // Ip for far end
186 portt port; // port for far end
187 u16 window; // Rx window
188 u16 nr; // next receive
189 u16 ns; // next send
190 int state; // current state (tunnelstate enum)
191 clockt last; // when last control message sent (used for resend timeout)
192 clockt retry; // when to try resenting pending control
193 clockt die; // being closed, when to finally free
194 clockt lastrec; // when the last control message was received
195 char hostname[128]; // tunnel hostname
196 char vendor[128]; // LAC vendor
197 u8 try; // number of retrys on a control message
198 u16 controlc; // outstaind messages in queue
199 controlt *controls; // oldest message
200 controlt *controle; // newest message
201 }
202 tunnelt;
203
204 // 180 bytes per radius session
205 typedef struct radiuss // outstanding RADIUS requests
206 {
207 sessionidt session; // which session this applies to
208 hasht auth; // request authenticator
209 clockt retry; // when to try next
210 char calling[MAXTEL]; // calling number
211 char pass[129]; // password
212 u8 id; // ID for PPP response
213 u8 try; // which try we are on
214 u8 state; // state of radius requests
215 u8 chap; // set if CHAP used (is CHAP identifier)
216 }
217 radiust;
218
219 typedef struct
220 {
221 ipt address; // Host byte order..
222 char assigned; // 1 if assigned, 0 if free
223 sessionidt session;
224 clockt last; // last used
225 char user[129]; // user (try to have ip addresses persistent)
226 }
227 ippoolt;
228
229 #ifdef RINGBUFFER
230 struct Tringbuffer
231 {
232 struct {
233 char level;
234 sessionidt session;
235 tunnelidt tunnel;
236 ipt address;
237 char message[MAX_LOG_LENGTH];
238 } buffer[RINGBUFFER_SIZE];
239 int head;
240 int tail;
241 };
242 #endif
243
244 /*
245 * Possible tunnel states
246 * TUNNELFREE -> TUNNELOPEN -> TUNNELDIE -> TUNNELFREE
247 */
248 enum
249 {
250 TUNNELFREE, // Not in use
251 TUNNELOPEN, // Active tunnel
252 TUNNELDIE, // Currently closing
253 TUNNELOPENING, // Busy opening
254 TUNNELUNDEF, // Undefined
255 };
256
257 enum
258 {
259 RADIUSNULL, // Not in use
260 RADIUSCHAP, // sending CHAP down PPP
261 RADIUSAUTH, // sending auth to RADIUS server
262 RADIUSIPCP, // sending IPCP to end user
263 RADIUSSTART, // sending start accounting to RADIUS server
264 RADIUSSTOP, // sending stop accounting to RADIUS server
265 RADIUSWAIT, // waiting timeout before available, in case delayed replies
266 RADIUSDEAD, // errored while talking to radius server.
267 };
268
269 struct Tstats
270 {
271 time_t start_time;
272 time_t last_reset;
273
274 unsigned long tap_rx_packets;
275 unsigned long tap_tx_packets;
276 unsigned long tap_rx_bytes;
277 unsigned long tap_tx_bytes;
278 unsigned long tap_rx_errors;
279 unsigned long tap_tx_errors;
280
281 unsigned long tunnel_rx_packets;
282 unsigned long tunnel_tx_packets;
283 unsigned long tunnel_rx_bytes;
284 unsigned long tunnel_tx_bytes;
285 unsigned long tunnel_rx_errors;
286 unsigned long tunnel_tx_errors;
287
288 unsigned long tunnel_retries;
289 unsigned long radius_retries;
290
291 unsigned long arp_errors;
292 unsigned long arp_replies;
293 unsigned long arp_discarded;
294 unsigned long arp_sent;
295 unsigned long arp_recv;
296
297 unsigned long packets_snooped;
298
299 unsigned long tunnel_created;
300 unsigned long session_created;
301 unsigned long tunnel_timeout;
302 unsigned long session_timeout;
303 unsigned long radius_timeout;
304 unsigned long radius_overflow;
305 unsigned long tunnel_overflow;
306 unsigned long session_overflow;
307
308 unsigned long ip_allocated;
309 unsigned long ip_freed;
310
311 unsigned long c_forwarded;
312 unsigned long recv_forward;
313 #ifdef STATISTICS
314 unsigned long call_processtap;
315 unsigned long call_processarp;
316 unsigned long call_processipout;
317 unsigned long call_processudp;
318 unsigned long call_sessionbyip;
319 unsigned long call_sessionbyuser;
320 unsigned long call_sendarp;
321 unsigned long call_sendipcp;
322 unsigned long call_tunnelsend;
323 unsigned long call_sessionkill;
324 unsigned long call_sessionshutdown;
325 unsigned long call_tunnelkill;
326 unsigned long call_tunnelshutdown;
327 unsigned long call_assign_ip_address;
328 unsigned long call_free_ip_address;
329 unsigned long call_dump_acct_info;
330 unsigned long call_sessionsetup;
331 unsigned long call_processpap;
332 unsigned long call_processchap;
333 unsigned long call_processlcp;
334 unsigned long call_processipcp;
335 unsigned long call_processipin;
336 unsigned long call_processccp;
337 unsigned long call_sendchap;
338 unsigned long call_processrad;
339 unsigned long call_radiussend;
340 unsigned long call_radiusretry;
341 #endif
342 };
343
344 #ifdef STATISTICS
345
346 #ifdef STAT_CALLS
347 #define CSTAT(x) STAT(x)
348 #else
349 #define CSTAT(x)
350 #endif
351
352 #define STAT(x) (_statistics->x++)
353 #define INC_STAT(x,y) (_statistics->x += (y))
354 #define GET_STAT(x) (_statistics->x)
355 #define SET_STAT(x, y) (_statistics->x = (y))
356 #else
357 #define CSTAT(x)
358 #define STAT(x)
359 #define INC_STAT(x,y)
360 #define GET_STAT(x) 0
361 #define SET_STAT(x, y)
362 #endif
363
364 struct configt
365 {
366 int debug; // debugging level
367 time_t start_time; // time when l2tpns was started
368 char bandwidth[256]; // current bandwidth
369 clockt current_time; // 1/10ths of a second since the process started.
370 // means that we can only run a given process
371 // for 13 years without re-starting!
372
373 char config_file[128];
374 int reload_config; // flag to re-read config (set by cli)
375 int cleanup_interval; // interval between regular cleanups (in seconds)
376 int multi_read_count; // amount of packets to read per fd in processing loop
377
378 char tapdevice[10]; // tap device name
379 char log_filename[128];
380 char l2tpsecret[64];
381
382 char radiussecret[64];
383 int radius_accounting;
384 ipt radiusserver[MAXRADSERVER]; // radius servers
385 u8 numradiusservers; // radius server count
386 short num_radfds; // Number of radius filehandles allocated
387
388 ipt default_dns1, default_dns2;
389
390 unsigned long rl_rate;
391 int save_state;
392 char accounting_dir[128];
393 ipt bind_address;
394 int send_garp; // Set to true to garp for vip address on startup
395
396 int target_uid;
397 int dump_speed;
398 char plugins[64][MAXPLUGINS];
399 char old_plugins[64][MAXPLUGINS];
400
401 int next_tbf; // Next HTB id available to use
402 int scheduler_fifo; // If 1, will force scheduler to use SCHED_FIFO.
403 // Don't use this unless you have a dual processor machine!
404 int icmp_rate; // Max number of ICMP unreachable per second to send
405
406 u32 cluster_address; // Multicast address of cluster.
407 // Send to this address to have everyone hear.
408 char cluster_interface[64]; // Which interface to listen for multicast on.
409 int cluster_iam_master; // Are we the cluster master???
410 int cluster_iam_uptodate; // Set if we've got a full set of state from the master.
411 u32 cluster_master_address; // The network address of the cluster master.
412 // Zero if i am the cluster master.
413 int cluster_seq_number; // Sequence number of the next heartbeat we'll send out
414 // (or the seq number we're next expecting if we're a slave).
415 int cluster_undefined_sessions; // How many sessions we're yet to receive from the master.
416 int cluster_undefined_tunnels; // How many tunnels we're yet to receive from the master.
417 int cluster_highest_sessionid;
418 int cluster_highest_tunnelid;
419 clockt cluster_last_hb; // Last time we saw a heartbeat from the master.
420 int cluster_num_changes; // Number of changes queued.
421
422 int cluster_hb_interval; // How often to send a heartbeat.
423 int cluster_hb_timeout; // How many missed heartbeats trigger an election.
424
425 #ifdef BGP
426 u16 as_number;
427 char bgp_peer[2][64];
428 u16 bgp_peer_as[2];
429 #endif
430 };
431
432 struct config_descriptt
433 {
434 char *key;
435 int offset;
436 int size;
437 enum { INT, STRING, UNSIGNED_LONG, SHORT, BOOL, IP } type;
438 };
439
440 // arp.c
441 void sendarp(int ifr_idx, const unsigned char* mac, ipt ip);
442
443
444 // ppp.c
445 void processpap(tunnelidt t, sessionidt s, u8 * p, u16 l);
446 void processchap(tunnelidt t, sessionidt s, u8 * p, u16 l);
447 void processlcp(tunnelidt t, sessionidt s, u8 * p, u16 l);
448 void processipcp(tunnelidt t, sessionidt s, u8 * p, u16 l);
449 void processipin(tunnelidt t, sessionidt s, u8 * p, u16 l);
450 void processccp(tunnelidt t, sessionidt s, u8 * p, u16 l);
451 void sendchap(tunnelidt t, sessionidt s);
452 u8 *makeppp(u8 * b, int size, u8 * p, int l, tunnelidt t, sessionidt s, u16 mtype);
453 u8 *findppp(u8 * b, u8 mtype);
454 void initlcp(tunnelidt t, sessionidt s);
455 void dumplcp(u8 *p, int l);
456
457
458 // radius.c
459 void initrad(void);
460 void radiussend(u16 r, u8 state);
461 void processrad(u8 *buf, int len, char socket_index);
462 void radiusretry(u16 r);
463 u16 radiusnew(sessionidt s);
464 void radiusclear(u16 r, sessionidt s);
465
466 // throttle.c
467 int throttle_session(sessionidt s, int throttle);
468
469
470 // rl.c
471 void init_rl();
472 u16 rl_create_tbf();
473 u16 rl_get_tbf();
474 void rl_done_tbf(u16 t);
475 void rl_destroy_tbf(u16 t);
476
477
478 // l2tpns.c
479 clockt now(void);
480 clockt backoff(u8 try);
481 void routeset(sessionidt, ipt ip, ipt mask, ipt gw, u8 add);
482 void inittap(void);
483 void initudp(void);
484 void initdata(void);
485 void initippool();
486 sessionidt sessionbyip(ipt ip);
487 sessionidt sessionbyuser(char *username);
488 void sessionshutdown(sessionidt s, char *reason);
489 void sessionsendarp(sessionidt s);
490 void send_garp(ipt ip);
491 void sessionkill(sessionidt s, char *reason);
492 void control16(controlt * c, u16 avp, u16 val, u8 m);
493 void control32(controlt * c, u16 avp, u32 val, u8 m);
494 void controls(controlt * c, u16 avp, char *val, u8 m);
495 void controlb(controlt * c, u16 avp, char *val, unsigned int len, u8 m);
496 controlt *controlnew(u16 mtype);
497 void controlnull(tunnelidt t);
498 void controladd(controlt * c, tunnelidt t, sessionidt s);
499 void tunnelsend(u8 * buf, u16 l, tunnelidt t);
500 void tunnelkill(tunnelidt t, char *reason);
501 void tunnelshutdown(tunnelidt t, char *reason);
502 void sendipcp(tunnelidt t, sessionidt s);
503 void processipout(u8 * buf, int len);
504 void processarp(u8 * buf, int len);
505 void processudp(u8 * buf, int len, struct sockaddr_in *addr);
506 void processtap(u8 * buf, int len);
507 void processcontrol(u8 * buf, int len, struct sockaddr_in *addr);
508 int assign_ip_address(sessionidt s);
509 void free_ip_address(sessionidt s);
510 void snoop_send_packet(char *packet, u16 size, ipt destination, u16 port);
511 void dump_acct_info();
512 void mainloop(void);
513 int cmd_show_ipcache(struct cli_def *cli, char *command, char **argv, int argc);
514 int cmd_show_hist_idle(struct cli_def *cli, char *command, char **argv, int argc);
515 int cmd_show_hist_open(struct cli_def *cli, char *command, char **argv, int argc);
516
517 #define log _log
518 #ifndef log_hex
519 #define log_hex(a,b,c,d) do{if (a <= config->debug) _log_hex(a,0,0,0,b,c,d);}while (0)
520 #endif
521 void _log(int level, ipt address, sessionidt s, tunnelidt t, const char *format, ...) __attribute__((format (printf, 5, 6)));
522 void _log_hex(int level, ipt address, sessionidt s, tunnelidt t, const char *title, const char *data, int maxsize);
523 void build_chap_response(char *challenge, u8 id, u16 challenge_length, char **challenge_response);
524 int sessionsetup(tunnelidt t, sessionidt s);
525 int cluster_send_session(int s);
526 int cluster_send_tunnel(int t);
527 int cluster_send_goodbye();
528 void init_cli();
529 void cli_do_file(FILE *fh);
530 void cli_do(int sockfd);
531 int cli_arg_help(struct cli_def *cli, int cr_ok, char *entry, ...);
532 #ifdef RINGBUFFER
533 void ringbuffer_dump(FILE *stream);
534 #endif
535 void initplugins(void);
536 int run_plugins(int plugin_type, void *data);
537 void add_plugin(char *plugin_name);
538 void remove_plugin(char *plugin_name);
539 void plugins_done(void);
540 void tunnelclear(tunnelidt t);
541 void host_unreachable(ipt destination, u16 id, ipt source, char *packet, int packet_len);
542 void fix_address_pool(int sid);
543 void rebuild_address_pool(void);
544 void send_ipin(sessionidt s, u8 * buf, int len);
545 int throttle_session(sessionidt s, int throttle);
546 int load_session(sessionidt, sessiont *);
547 void become_master(void); // We're the master; kick off any required master initializations.
548 extern tunnelt *tunnel;
549 extern sessiont *session;
550 extern sessioncountt *sess_count;
551 extern ippoolt *ip_address_pool;
552 #define sessionfree (session[0].next)
553
554 #define log_backtrace(count, max) \
555 if (count++ < max) { \
556 void *array[20]; \
557 char **strings; \
558 int size, i; \
559 log(0, 0, 0, t, "Backtrace follows"); \
560 size = backtrace(array, 10); \
561 strings = backtrace_symbols(array, size); \
562 if (strings) for (i = 0; i < size; i++) \
563 { \
564 log(0, 0, 0, t, "%s\n", strings[i]); \
565 } \
566 free(strings); \
567 }
568
569
570 extern struct configt *config;
571 extern time_t basetime; // Time when this process started.
572 extern time_t time_now; // Seconds since EPOCH.
573 extern u32 last_sid;
574 extern struct Tstats *_statistics;
575 extern ipt my_address;
576 extern int tun_write(u8 *data, int size);
577
578
579 #define TIME (config->current_time)
580
581 // macros for handling help in cli commands
582 #define CLI_HELP_REQUESTED (argc > 0 && argv[argc-1][strlen(argv[argc-1])-1] == '?')
583 #define CLI_HELP_NO_ARGS (argc > 1 || argv[0][1]) ? CLI_OK : cli_arg_help(cli, 1, NULL)
584
585 // CVS identifiers (for "show version file")
586 extern char const *cvs_id_arp;
587 extern char const *cvs_id_cli;
588 extern char const *cvs_id_cluster;
589 extern char const *cvs_id_constants;
590 extern char const *cvs_id_control;
591 extern char const *cvs_id_icmp;
592 extern char const *cvs_id_l2tpns;
593 extern char const *cvs_id_ll;
594 extern char const *cvs_id_md5;
595 extern char const *cvs_id_ppp;
596 extern char const *cvs_id_radius;
597 extern char const *cvs_id_tbf;
598 extern char const *cvs_id_util;
599
600 #endif /* __L2TPNS_H__ */