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