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