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