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