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