2 // $Id: l2tpns.h,v 1.7 2004-06-23 03:52:24 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 // 336 bytes per session
129 typedef struct sessions
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
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
172 #define SF_IPCP_ACKED (1<<0) // Has this session seen an IPCP Ack?
179 #define SESSIONPFC 1 // PFC negotiated flags
180 #define SESSIONACFC 2 // ACFC negotiated flags
182 // 168 bytes per tunnel
183 typedef struct tunnels
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
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
205 // 180 bytes per radius session
206 typedef struct radiuss
// outstanding RADIUS requests
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)
222 ipt address
; // Host byte order..
223 char assigned
; // 1 if assigned, 0 if free
225 clockt last
; // last used
226 char user
[129]; // user (try to have ip addresses persistent)
238 char message
[MAX_LOG_LENGTH
];
239 } buffer
[RINGBUFFER_SIZE
];
246 * Possible tunnel states
247 * TUNNELFREE -> TUNNELOPEN -> TUNNELDIE -> TUNNELFREE
251 TUNNELFREE
, // Not in use
252 TUNNELOPEN
, // Active tunnel
253 TUNNELDIE
, // Currently closing
254 TUNNELOPENING
, // Busy opening
255 TUNNELUNDEF
, // Undefined
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.
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
;
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
;
289 unsigned long tunnel_retries
;
290 unsigned long radius_retries
;
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
;
298 unsigned long packets_snooped
;
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
;
309 unsigned long ip_allocated
;
310 unsigned long ip_freed
;
312 unsigned long c_forwarded
;
313 unsigned long recv_forward
;
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
;
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
352 #define INC_STAT(x,y)
353 #define GET_STAT(x) 0
354 #define SET_STAT(x, y)
359 int debug
; // debugging level
360 time_t start_time
; // time when l2tpns was started
361 char bandwidth
[256]; // current bandwidth
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
369 char tapdevice
[10]; // tap device name
370 char log_filename
[128];
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
379 ipt default_dns1
, default_dns2
;
381 unsigned long rl_rate
;
383 char accounting_dir
[128];
385 int send_garp
; // Set to true to garp for vip address on startup
389 char plugins
[64][MAXPLUGINS
];
390 char old_plugins
[64][MAXPLUGINS
];
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
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.
415 char bgp_peer
[2][64];
420 struct config_descriptt
425 enum { INT
, STRING
, UNSIGNED_LONG
, SHORT
, BOOL
, IP
} type
;
429 void sendarp(int ifr_idx
, const unsigned char* mac
, ipt ip
);
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
);
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
);
455 int throttle_session(sessionidt s
, int throttle
);
462 void rl_done_tbf(u16 t
);
463 void rl_destroy_tbf(u16 t
);
468 clockt
backoff(u8
try);
469 void routeset(sessionidt
, ipt ip
, ipt mask
, ipt gw
, u8 add
);
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();
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
);
507 #define log_hex(a,b,c,d) do{if (a <= config->debug) _log_hex(a,0,0,0,b,c,d);}while (0)
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();
517 void cli_do_file(FILE *fh
);
518 void cli_do(int sockfd
);
520 void ringbuffer_dump(FILE *stream
);
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)
541 #define log_backtrace(count, max) \
542 if (count++ < max) { \
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++) \
551 log(0, 0, 0, t, "%s\n", strings[i]); \
557 extern struct configt
*config
;
558 extern time_t basetime
; // Time when this process started.
559 extern time_t time_now
; // Seconds since EPOCH.
561 extern struct Tstats
*_statistics
;
562 extern ipt my_address
;
563 extern int tun_write(u8
*data
, int size
);
564 #endif /* __L2TPNS_H__ */