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