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