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