2 // $Id: l2tpns.h,v 1.49 2004-12-17 00:28:00 bodea Exp $
7 #include <netinet/in.h>
12 #include <netinet/in.h>
13 #include <sys/socket.h>
14 #include <arpa/inet.h>
15 #include <sys/types.h>
18 #define VERSION "2.0.13"
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.
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)
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.
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
47 #define ETCDIR "/etc/l2tpns"
51 #define LIBDIR "/usr/lib/l2tpns"
55 #define STATEDIR "/var/lib/l2tpns"
59 #define PLUGINDIR LIBDIR // Plugins
63 #define PLUGINCONF ETCDIR // Plugin config dir
67 #define FLASHDIR ETCDIR
71 #define DATADIR STATEDIR
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 ACCT_SHUT_TIME 600 // 1 minute for counters of shutdown sessions
81 #define L2TPPORT 1701 // L2TP port
82 #define RADPORT 1645 // old radius port...
83 #define PKTARP 0x0806 // ARP packet type
84 #define PKTIP 0x0800 // IP packet type
85 #define PSEUDOMAC 0x0200 // pseudo MAC prefix (local significant MAC)
87 #define PPPCHAP 0xC223
89 #define PPPIPCP 0x8021
93 #define MIN_IP_SIZE 0x19
111 typedef uint16_t sessionidt
;
112 typedef uint16_t tunnelidt
;
113 typedef uint32_t clockt
;
114 typedef uint8_t hasht
[16];
117 struct cli_session_actions
{
127 #define CLI_SESS_KILL 0x01
128 #define CLI_SESS_SNOOP 0x02
129 #define CLI_SESS_NOSNOOP 0x04
130 #define CLI_SESS_THROTTLE 0x08
131 #define CLI_SESS_NOTHROTTLE 0x10
132 #define CLI_SESS_FILTER 0x20
133 #define CLI_SESS_NOFILTER 0x40
135 struct cli_tunnel_actions
{
139 #define CLI_TUN_KILL 0x01
141 // dump header: update number if internal format changes
142 #define DUMP_MAGIC "L2TPNS#" VERSION "#"
145 typedef struct // route
152 typedef struct controls
// control message
154 struct controls
*next
; // next in queue
155 uint16_t length
; // length
156 uint8_t buf
[MAXCONTROL
];
162 sessionidt next
; // next session in linked list
163 sessionidt far
; // far end session ID
164 tunnelidt tunnel
; // near end tunnel ID
165 in_addr_t 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 uint16_t nr
; // next receive
169 uint16_t ns
; // next send
170 uint32_t magic
; // ppp magic number
171 uint32_t cin
, cout
; // byte counts
172 uint32_t pin
, pout
; // packet counts
173 uint32_t total_cin
; // This counter is never reset while a session is open
174 uint32_t total_cout
; // This counter is never reset while a session is open
175 uint32_t id
; // session id
176 uint16_t throttle_in
; // upstream throttle rate (kbps)
177 uint16_t 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 in_addr_t dns1
, dns2
; // DNS servers
182 routet route
[MAXROUTE
]; // static routes
183 uint16_t radius
; // which radius session is being used (0 for not waiting on authentication)
184 uint16_t mru
; // maximum receive unit
185 uint16_t tbf_in
; // filter bucket for throttling in from the user.
186 uint16_t tbf_out
; // filter bucket for throttling out to the user.
187 uint8_t l2tp_flags
; // various bit flags from the ICCN on the l2tp tunnel.
188 uint8_t reserved_old_snoop
; // No longer used - remove at some time
189 uint8_t walled_garden
; // is this session gardened?
190 uint8_t 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 uint32_t tx_connect_speed
;
197 uint32_t rx_connect_speed
;
198 uint32_t flags
; // Various session flags.
199 in_addr_t snoop_ip
; // Interception destination IP
200 uint16_t snoop_port
; // Interception destination port
201 uint16_t sid
; // near end session id.
202 uint8_t filter_in
; // input filter index (to ip_filters[N-1]; 0 if none)
203 uint8_t filter_out
; // output filter index
204 char reserved
[18]; // Space to expand structure without changing HB_VERSION
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
218 #define SESSIONPFC 1 // PFC negotiated flags
219 #define SESSIONACFC 2 // ACFC negotiated flags
221 // 168 bytes per tunnel
224 tunnelidt far
; // far end tunnel ID
225 in_addr_t ip
; // Ip for far end
226 uint16_t port
; // port for far end
227 uint16_t window
; // Rx window
228 uint16_t nr
; // next receive
229 uint16_t 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 uint8_t try; // number of retrys on a control message
238 uint16_t controlc
; // outstaind messages in queue
239 controlt
*controls
; // oldest message
240 controlt
*controle
; // newest message
244 // 180 bytes per radius session
245 typedef struct // outstanding RADIUS requests
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 uint8_t id
; // ID for PPP response
253 uint8_t try; // which try we are on
254 uint8_t state
; // state of radius requests
255 uint8_t chap
; // set if CHAP used (is CHAP identifier)
261 in_addr_t address
; // Host byte order..
262 char assigned
; // 1 if assigned, 0 if free
264 clockt last
; // last used
265 char user
[129]; // user (try to have ip addresses persistent)
276 char message
[MAX_LOG_LENGTH
];
277 } buffer
[RINGBUFFER_SIZE
];
284 * Possible tunnel states
285 * TUNNELFREE -> TUNNELOPEN -> TUNNELDIE -> TUNNELFREE
289 TUNNELFREE
, // Not in use
290 TUNNELOPEN
, // Active tunnel
291 TUNNELDIE
, // Currently closing
292 TUNNELOPENING
, // Busy opening
293 TUNNELUNDEF
, // Undefined
298 RADIUSNULL
, // Not in use
299 RADIUSCHAP
, // sending CHAP down PPP
300 RADIUSAUTH
, // sending auth to RADIUS server
301 RADIUSIPCP
, // sending IPCP to end user
302 RADIUSSTART
, // sending start accounting to RADIUS server
303 RADIUSSTOP
, // sending stop accounting to RADIUS server
304 RADIUSWAIT
, // waiting timeout before available, in case delayed replies
305 RADIUSDEAD
, // errored while talking to radius server.
313 uint32_t tun_rx_packets
;
314 uint32_t tun_tx_packets
;
315 uint32_t tun_rx_bytes
;
316 uint32_t tun_tx_bytes
;
317 uint32_t tun_rx_errors
;
318 uint32_t tun_tx_errors
;
320 uint32_t tunnel_rx_packets
;
321 uint32_t tunnel_tx_packets
;
322 uint32_t tunnel_rx_bytes
;
323 uint32_t tunnel_tx_bytes
;
324 uint32_t tunnel_rx_errors
;
325 uint32_t tunnel_tx_errors
;
327 uint32_t tunnel_retries
;
328 uint32_t radius_retries
;
332 uint32_t packets_snooped
;
334 uint32_t tunnel_created
;
335 uint32_t session_created
;
336 uint32_t tunnel_timeout
;
337 uint32_t session_timeout
;
338 uint32_t radius_timeout
;
339 uint32_t radius_overflow
;
340 uint32_t tunnel_overflow
;
341 uint32_t session_overflow
;
343 uint32_t ip_allocated
;
346 uint32_t c_forwarded
;
347 uint32_t recv_forward
;
349 uint32_t select_called
;
350 uint32_t multi_read_used
;
351 uint32_t multi_read_exceeded
;
354 uint32_t call_processtun
;
355 uint32_t call_processipout
;
356 uint32_t call_processudp
;
357 uint32_t call_sessionbyip
;
358 uint32_t call_sessionbyuser
;
359 uint32_t call_sendarp
;
360 uint32_t call_sendipcp
;
361 uint32_t call_tunnelsend
;
362 uint32_t call_sessionkill
;
363 uint32_t call_sessionshutdown
;
364 uint32_t call_tunnelkill
;
365 uint32_t call_tunnelshutdown
;
366 uint32_t call_assign_ip_address
;
367 uint32_t call_free_ip_address
;
368 uint32_t call_dump_acct_info
;
369 uint32_t call_sessionsetup
;
370 uint32_t call_processpap
;
371 uint32_t call_processchap
;
372 uint32_t call_processlcp
;
373 uint32_t call_processipcp
;
374 uint32_t call_processipin
;
375 uint32_t call_processccp
;
376 uint32_t call_sendchap
;
377 uint32_t call_processrad
;
378 uint32_t call_radiussend
;
379 uint32_t call_radiusretry
;
386 #define CSTAT(x) STAT(x)
391 #define STAT(x) (_statistics->x++)
392 #define INC_STAT(x,y) (_statistics->x += (y))
393 #define GET_STAT(x) (_statistics->x)
394 #define SET_STAT(x, y) (_statistics->x = (y))
398 #define INC_STAT(x,y)
399 #define GET_STAT(x) 0
400 #define SET_STAT(x, y)
405 int debug
; // debugging level
406 time_t start_time
; // time when l2tpns was started
407 char bandwidth
[256]; // current bandwidth
408 char pid_file
[256]; // file to write PID to on startup
410 clockt current_time
; // 1/10ths of a second since the process started.
411 // means that we can only run a given process
412 // for 13 years without re-starting!
414 char config_file
[128];
415 int reload_config
; // flag to re-read config (set by cli)
416 int cleanup_interval
; // interval between regular cleanups (in seconds)
417 int multi_read_count
; // amount of packets to read per fd in processing loop
419 char tundevice
[10]; // tun device name
420 char log_filename
[128];
423 char radiussecret
[64];
424 int radius_accounting
;
425 in_addr_t radiusserver
[MAXRADSERVER
]; // radius servers
426 uint16_t radiusport
[MAXRADSERVER
]; // radius base ports
427 uint8_t numradiusservers
; // radius server count
428 short num_radfds
; // Number of radius filehandles allocated
430 in_addr_t default_dns1
, default_dns2
;
432 unsigned long rl_rate
; // default throttle rate
433 int num_tbfs
; // number of throttle buckets
436 char accounting_dir
[128];
437 in_addr_t bind_address
;
438 in_addr_t peer_address
;
439 int send_garp
; // Set to true to garp for vip address on startup
443 char plugins
[64][MAXPLUGINS
];
444 char old_plugins
[64][MAXPLUGINS
];
446 int next_tbf
; // Next HTB id available to use
447 int scheduler_fifo
; // If the system has multiple CPUs, use FIFO scheduling policy for this process.
448 int lock_pages
; // Lock pages into memory.
449 int icmp_rate
; // Max number of ICMP unreachable per second to send>
451 in_addr_t cluster_address
; // Multicast address of cluster.
452 // Send to this address to have everyone hear.
453 char cluster_interface
[64]; // Which interface to listen for multicast on.
454 int cluster_iam_master
; // Are we the cluster master???
455 int cluster_iam_uptodate
; // Set if we've got a full set of state from the master.
456 in_addr_t cluster_master_address
; // The network address of the cluster master.
457 // Zero if i am the cluster master.
458 int cluster_seq_number
; // Sequence number of the next heartbeat we'll send out
459 // (or the seq number we're next expecting if we're a slave).
460 int cluster_undefined_sessions
; // How many sessions we're yet to receive from the master.
461 int cluster_undefined_tunnels
; // How many tunnels we're yet to receive from the master.
462 int cluster_highest_sessionid
;
463 int cluster_highest_tunnelid
;
464 clockt cluster_last_hb
; // Last time we saw a heartbeat from the master.
465 int cluster_num_changes
; // Number of changes queued.
467 int cluster_hb_interval
; // How often to send a heartbeat.
468 int cluster_hb_timeout
; // How many missed heartbeats trigger an election.
469 uint64_t cluster_table_version
; // # state changes processed by cluster
472 #define BGP_NUM_PEERS 2
479 } neighbour
[BGP_NUM_PEERS
];
483 enum config_typet
{ INT
, STRING
, UNSIGNED_LONG
, SHORT
, BOOL
, IP
, MAC
};
489 enum config_typet type
;
494 uint8_t op
; // operation
495 #define FILTER_PORT_OP_NONE 0 // all ports match
496 #define FILTER_PORT_OP_EQ 1
497 #define FILTER_PORT_OP_NEQ 2
498 #define FILTER_PORT_OP_GT 3
499 #define FILTER_PORT_OP_LT 4
500 #define FILTER_PORT_OP_RANGE 5
501 uint16_t port
; // port (host byte order)
502 uint16_t port2
; // range
507 int action
; // permit/deny
508 #define FILTER_ACTION_DENY 1
509 #define FILTER_ACTION_PERMIT 2
510 uint8_t proto
; // protocol: IPPROTO_* (netinet/in.h)
511 in_addr_t src_ip
; // source ip (network byte order)
513 ip_filter_portt src_ports
;
514 in_addr_t dst_ip
; // dest ip
516 ip_filter_portt dst_ports
;
517 uint8_t frag
; // apply to non-initial fragments
518 uint8_t tcp_flag_op
; // match type: any, all, established
519 #define FILTER_FLAG_OP_ANY 1
520 #define FILTER_FLAG_OP_ALL 2
521 #define FILTER_FLAG_OP_EST 3
522 uint8_t tcp_sflags
; // flags set
523 uint8_t tcp_cflags
; // flags clear
524 uint32_t counter
; // match count
527 #define TCP_FLAG_FIN 0x01
528 #define TCP_FLAG_SYN 0x02
529 #define TCP_FLAG_RST 0x04
530 #define TCP_FLAG_PSH 0x08
531 #define TCP_FLAG_ACK 0x10
532 #define TCP_FLAG_URG 0x20
535 #define MAXFILTER_RULES 32
538 char name
[32]; // ACL name
539 int extended
; // type: 0 = standard, 1 = extended
540 ip_filter_rulet rules
[MAXFILTER_RULES
];
541 int used
; // session ref count
545 void sendarp(int ifr_idx
, const unsigned char* mac
, in_addr_t ip
);
549 void processpap(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
550 void processchap(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
551 void processlcp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
552 void processipcp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
553 void processipin(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
554 void processccp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
);
555 void sendchap(tunnelidt t
, sessionidt s
);
556 uint8_t *makeppp(uint8_t *b
, int size
, uint8_t *p
, int l
, tunnelidt t
, sessionidt s
, uint16_t mtype
);
557 void initlcp(tunnelidt t
, sessionidt s
);
558 void send_ipin(sessionidt s
, uint8_t *buf
, int len
);
563 void radiussend(uint16_t r
, uint8_t state
);
564 void processrad(uint8_t *buf
, int len
, char socket_index
);
565 void radiusretry(uint16_t r
);
566 uint16_t radiusnew(sessionidt s
);
567 void radiusclear(uint16_t r
, sessionidt s
);
571 clockt
backoff(uint8_t try);
572 sessionidt
sessionbyip(in_addr_t ip
);
573 sessionidt
sessionbyuser(char *username
);
574 void sessionshutdown(sessionidt s
, char *reason
);
575 void send_garp(in_addr_t ip
);
576 void tunnelsend(uint8_t *buf
, uint16_t l
, tunnelidt t
);
577 void sendipcp(tunnelidt t
, sessionidt s
);
578 void processudp(uint8_t *buf
, int len
, struct sockaddr_in
*addr
);
579 void snoop_send_packet(char *packet
, uint16_t size
, in_addr_t destination
, uint16_t port
);
580 int ip_filter(uint8_t *buf
, int len
, uint8_t filter
);
581 int cmd_show_ipcache(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
582 int cmd_show_hist_idle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
583 int cmd_show_hist_open(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
587 #define LOG(D, s, t, f, ...) ({ if (D <= config->debug) _log(D, s, t, f, ## __VA_ARGS__); })
588 #define LOG_HEX(D, t, d, s) ({ if (D <= config->debug) _log_hex(D, t, d, s); })
590 void _log(int level
, sessionidt s
, tunnelidt t
, const char *format
, ...) __attribute__((format (printf
, 4, 5)));
591 void _log_hex(int level
, const char *title
, const char *data
, int maxsize
);
593 int sessionsetup(tunnelidt t
, sessionidt s
);
594 int run_plugins(int plugin_type
, void *data
);
595 void rebuild_address_pool(void);
596 void throttle_session(sessionidt s
, int rate_in
, int rate_out
);
597 int load_session(sessionidt
, sessiont
*);
598 void become_master(void); // We're the master; kick off any required master initializations.
602 void init_cli(char *hostname
);
603 void cli_do_file(FILE *fh
);
604 void cli_do(int sockfd
);
605 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...);
609 void host_unreachable(in_addr_t destination
, uint16_t id
, in_addr_t source
, char *packet
, int packet_len
);
612 extern tunnelt
*tunnel
;
613 extern sessiont
*session
;
614 extern sessioncountt
*sess_count
;
615 extern ippoolt
*ip_address_pool
;
616 #define sessionfree (session[0].next)
618 #define log_backtrace(count, max) \
619 if (count++ < max) { \
623 LOG(0, 0, t, "Backtrace follows"); \
624 size = backtrace(array, 10); \
625 strings = backtrace_symbols(array, size); \
626 if (strings) for (i = 0; i < size; i++) \
628 LOG(0, 0, t, "%s\n", strings[i]); \
634 extern configt
*config
;
635 extern time_t basetime
; // Time when this process started.
636 extern time_t time_now
; // Seconds since EPOCH.
637 extern uint32_t last_id
;
638 extern struct Tstats
*_statistics
;
639 extern in_addr_t my_address
;
640 extern int tun_write(uint8_t *data
, int size
);
644 #define TIME (config->current_time)
646 // macros for handling help in cli commands
647 #define CLI_HELP_REQUESTED (argc > 0 && argv[argc-1][strlen(argv[argc-1])-1] == '?')
648 #define CLI_HELP_NO_ARGS (argc > 1 || argv[0][1]) ? CLI_OK : cli_arg_help(cli, 1, NULL)
650 // CVS identifiers (for "show version file")
651 extern char const *cvs_id_arp
;
652 extern char const *cvs_id_cli
;
653 extern char const *cvs_id_cluster
;
654 extern char const *cvs_id_constants
;
655 extern char const *cvs_id_control
;
656 extern char const *cvs_id_icmp
;
657 extern char const *cvs_id_l2tpns
;
658 extern char const *cvs_id_ll
;
659 extern char const *cvs_id_md5
;
660 extern char const *cvs_id_ppp
;
661 extern char const *cvs_id_radius
;
662 extern char const *cvs_id_tbf
;
663 extern char const *cvs_id_util
;
665 #endif /* __L2TPNS_H__ */