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