3 // Copyright (c) 2003, 2004 Optus Internet Engineering
4 // Copyright (c) 2002 FireBrick (Andrews & Arnold Ltd / Watchfront Ltd) - GPL licenced
7 char const *cvs_id_l2tpns
= "$Id: l2tpns.c,v 1.10 2004/07/02 07:31:23 bodea Exp $";
13 #include <linux/if_tun.h>
18 #include <net/route.h>
21 #include <netinet/in.h>
27 #include <sys/ioctl.h>
28 #include <sys/socket.h>
31 #include <sys/resource.h>
39 #include <sys/sysinfo.h>
47 #include "constants.h"
53 struct configt
*config
= NULL
; // all configuration
54 int tapfd
= -1; // tap interface file handle
55 int udpfd
= -1; // UDP file handle
56 int controlfd
= -1; // Control signal handle
57 int snoopfd
= -1; // UDP file handle for sending out intercept data
58 int *radfds
= NULL
; // RADIUS requests file handles
59 int ifrfd
= -1; // File descriptor for routing, etc
60 time_t basetime
= 0; // base clock
61 char hostname
[1000] = ""; // us.
62 u16 tapmac
[3]; // MAC of tap interface
63 int tapidx
; // ifr_ifindex of tap device
64 u32 sessionid
= 0; // session id for radius accounting
65 int syslog_log
= 0; // are we logging to syslog
66 FILE *log_stream
= NULL
;
67 struct sockaddr_in snoop_addr
= {0};
68 extern int cluster_sockfd
;
71 sessionidt
*cli_session_kill
= NULL
;
72 tunnelidt
*cli_tunnel_kill
= NULL
;
73 static void *ip_hash
[256];
74 u32 udp_tx
= 0, udp_rx
= 0, udp_rx_pkt
= 0;
75 u32 eth_tx
= 0, eth_rx
= 0, eth_rx_pkt
= 0;
78 char time_now_string
[64] = {0};
80 char *_program_name
= NULL
;
81 linked_list
*loaded_plugins
;
82 linked_list
*plugins
[MAX_PLUGIN_TYPES
];
86 struct bgp_peer
*bgp_peers
= 0;
87 struct bgp_route_list
*bgp_routes
= 0;
88 int bgp_configured
= 0;
91 #define membersize(STRUCT, MEMBER) sizeof(((STRUCT *)0)->MEMBER)
92 #define CONFIG(NAME, MEMBER, TYPE) { NAME, offsetof(struct configt, MEMBER), membersize(struct configt, MEMBER), TYPE }
94 struct config_descriptt config_values
[] = {
95 CONFIG("debug", debug
, INT
),
96 CONFIG("log_file", log_filename
, STRING
),
97 CONFIG("l2tp_secret", l2tpsecret
, STRING
),
98 CONFIG("primary_dns", default_dns1
, IP
),
99 CONFIG("secondary_dns", default_dns2
, IP
),
100 CONFIG("save_state", save_state
, BOOL
),
101 CONFIG("primary_radius", radiusserver
[0], IP
),
102 CONFIG("secondary_radius", radiusserver
[1], IP
),
103 CONFIG("radius_accounting", radius_accounting
, BOOL
),
104 CONFIG("radius_secret", radiussecret
, STRING
),
105 CONFIG("bind_address", bind_address
, IP
),
106 CONFIG("send_garp", send_garp
, BOOL
),
107 CONFIG("throttle_speed", rl_rate
, UNSIGNED_LONG
),
108 CONFIG("accounting_dir", accounting_dir
, STRING
),
109 CONFIG("setuid", target_uid
, INT
),
110 CONFIG("dump_speed", dump_speed
, BOOL
),
111 CONFIG("cleanup_interval", cleanup_interval
, INT
),
112 CONFIG("multi_read_count", multi_read_count
, INT
),
113 CONFIG("scheduler_fifo", scheduler_fifo
, BOOL
),
114 CONFIG("icmp_rate", icmp_rate
, INT
),
115 CONFIG("cluster_address", cluster_address
, IP
),
116 CONFIG("cluster_interface", cluster_interface
, STRING
),
117 CONFIG("cluster_hb_interval", cluster_hb_interval
, INT
),
118 CONFIG("cluster_hb_timeout", cluster_hb_timeout
, INT
),
120 CONFIG("as_number", as_number
, SHORT
),
121 CONFIG("bgp_peer1", bgp_peer
[0], STRING
),
122 CONFIG("bgp_peer1_as", bgp_peer_as
[0], SHORT
),
123 CONFIG("bgp_peer2", bgp_peer
[1], STRING
),
124 CONFIG("bgp_peer2_as", bgp_peer_as
[1], SHORT
),
129 char *plugin_functions
[] = {
136 "plugin_new_session",
137 "plugin_kill_session",
139 "plugin_radius_response",
140 "plugin_become_master",
141 "plugin_new_session_master",
144 #define max_plugin_functions (sizeof(plugin_functions) / sizeof(char *))
146 tunnelt
*tunnel
= NULL
; // 1000 * 45 = 45000 = 45k
147 sessiont
*session
= NULL
; // 5000 * 213 = 1065000 = 1 Mb
148 sessioncountt
*sess_count
= NULL
;
149 radiust
*radius
= NULL
;
150 ippoolt
*ip_address_pool
= NULL
;
151 controlt
*controlfree
= 0;
152 struct Tstats
*_statistics
= NULL
;
154 struct Tringbuffer
*ringbuffer
= NULL
;
157 void sigalrm_handler(int);
158 void sighup_handler(int);
159 void sigterm_handler(int);
160 void sigquit_handler(int);
161 void sigchild_handler(int);
162 void read_config_file();
166 tunnelidt
new_tunnel();
167 void update_config();
169 static void cache_ipmap(ipt ip
, int s
);
170 static void uncache_ipmap(ipt ip
);
172 // return internal time (10ths since run)
177 return (t
.tv_sec
- basetime
) * 10 + t
.tv_usec
/ 100000 + 1;
180 // work out a retry time based on try number
181 clockt
backoff(u8
try)
183 if (try > 5) try = 5; // max backoff
184 return now() + 10 * (1 << try);
187 void _log(int level
, ipt address
, sessionidt s
, tunnelidt t
, const char *format
, ...)
189 static char message
[65536] = {0};
190 static char message2
[65536] = {0};
196 if (++ringbuffer
->tail
>= RINGBUFFER_SIZE
)
197 ringbuffer
->tail
= 0;
198 if (ringbuffer
->tail
== ringbuffer
->head
)
199 if (++ringbuffer
->head
>= RINGBUFFER_SIZE
)
200 ringbuffer
->head
= 0;
202 ringbuffer
->buffer
[ringbuffer
->tail
].level
= level
;
203 ringbuffer
->buffer
[ringbuffer
->tail
].address
= address
;
204 ringbuffer
->buffer
[ringbuffer
->tail
].session
= s
;
205 ringbuffer
->buffer
[ringbuffer
->tail
].tunnel
= t
;
206 va_start(ap
, format
);
207 vsnprintf(ringbuffer
->buffer
[ringbuffer
->tail
].message
, 4095, format
, ap
);
212 if (config
->debug
< level
) return;
214 va_start(ap
, format
);
217 vsnprintf(message2
, 65535, format
, ap
);
218 snprintf(message
, 65535, "%s %02d/%02d %s", time_now_string
, t
, s
, message2
);
219 fprintf(log_stream
, "%s", message
);
223 vsnprintf(message2
, 65535, format
, ap
);
224 snprintf(message
, 65535, "%02d/%02d %s", t
, s
, message2
);
225 syslog(level
+ 2, message
); // We don't need LOG_EMERG or LOG_ALERT
230 void _log_hex(int level
, ipt address
, sessionidt s
, tunnelidt t
, const char *title
, const char *data
, int maxsize
)
233 const u8
*d
= (const u8
*)data
;
235 if (config
->debug
< level
) return;
237 // No support for log_hex to syslog
240 log(level
, address
, s
, t
, "%s (%d bytes):\n", title
, maxsize
);
241 setvbuf(log_stream
, NULL
, _IOFBF
, 16384);
243 for (i
= 0; i
< maxsize
; )
245 fprintf(log_stream
, "%4X: ", i
);
246 for (j
= i
; j
< maxsize
&& j
< (i
+ 16); j
++)
248 fprintf(log_stream
, "%02X ", d
[j
]);
250 fputs(": ", log_stream
);
253 for (; j
< i
+ 16; j
++)
255 fputs(" ", log_stream
);
257 fputs(": ", log_stream
);
260 fputs(" ", log_stream
);
261 for (j
= i
; j
< maxsize
&& j
< (i
+ 16); j
++)
263 if (d
[j
] >= 0x20 && d
[j
] < 0x7f && d
[j
] != 0x20)
264 fputc(d
[j
], log_stream
);
266 fputc('.', log_stream
);
269 fputs(" ", log_stream
);
273 fputs("\n", log_stream
);
277 setbuf(log_stream
, NULL
);
284 // This adds it to the routing table, advertises it
285 // via iBGP if enabled, and stuffs it into the
286 // 'sessionbyip' cache.
288 // 'ip' and 'mask' must be in _host_ order.
290 void routeset(sessionidt s
, ipt ip
, ipt mask
, ipt gw
, u8 add
)
295 if (!mask
) mask
= 0xffffffff;
297 ip
&= mask
; // Force the ip to be the first one in the route.
299 memset(&r
, 0, sizeof(r
));
300 r
.rt_dev
= config
->tapdevice
;
301 r
.rt_dst
.sa_family
= AF_INET
;
302 *(u32
*) & (((struct sockaddr_in
*) &r
.rt_dst
)->sin_addr
.s_addr
) = htonl(ip
);
303 r
.rt_gateway
.sa_family
= AF_INET
;
304 *(u32
*) & (((struct sockaddr_in
*) &r
.rt_gateway
)->sin_addr
.s_addr
) = htonl(gw
);
305 r
.rt_genmask
.sa_family
= AF_INET
;
306 *(u32
*) & (((struct sockaddr_in
*) &r
.rt_genmask
)->sin_addr
.s_addr
) = htonl(mask
);
307 r
.rt_flags
= (RTF_UP
| RTF_STATIC
);
309 r
.rt_flags
|= RTF_GATEWAY
;
310 else if (mask
== 0xffffffff)
311 r
.rt_flags
|= RTF_HOST
;
313 log(1, ip
, 0, 0, "Route %s %u.%u.%u.%u/%u.%u.%u.%u %u.%u.%u.%u\n",
315 ip
>> 24, ip
>> 16 & 0xff, ip
>> 8 & 0xff, ip
& 0xff,
316 mask
>> 24, mask
>> 16 & 0xff, mask
>> 8 & 0xff, mask
& 0xff,
317 gw
>> 24, gw
>> 16 & 0xff, gw
>> 8 & 0xff, gw
& 0xff);
319 if (ioctl(ifrfd
, add
? SIOCADDRT
: SIOCDELRT
, (void *) &r
) < 0)
320 log(0, 0, 0, 0, "routeset() error in ioctl: %s\n", strerror(errno
));
324 bgp_add_route(htonl(ip
), htonl(mask
));
326 bgp_del_route(htonl(ip
), htonl(mask
));
329 // Add/Remove the IPs to the 'sessionbyip' cache.
330 // Note that we add the zero address in the case of
331 // a network route. Roll on CIDR.
333 // Note that 's == 0' implies this is the address pool.
334 // We still cache it here, because it will pre-fill
335 // the malloc'ed tree.
339 if (!add
) // Are we deleting a route?
340 s
= 0; // Caching the session as '0' is the same as uncaching.
342 for (i
= ip
; (i
&mask
) == (ip
&mask
) ; ++i
)
347 // Set up TAP interface
351 struct sockaddr_in sin
= {0};
352 memset(&ifr
, 0, sizeof(ifr
));
353 ifr
.ifr_flags
= IFF_TUN
;
355 tapfd
= open(TAPDEVICE
, O_RDWR
);
358 log(0, 0, 0, 0, "Can't open %s: %s\n", TAPDEVICE
, strerror(errno
));
362 int flags
= fcntl(tapfd
, F_GETFL
, 0);
363 fcntl(tapfd
, F_SETFL
, flags
| O_NONBLOCK
);
365 if (ioctl(tapfd
, TUNSETIFF
, (void *) &ifr
) < 0)
367 log(0, 0, 0, 0, "Can't set tap interface: %s\n", strerror(errno
));
370 assert(strlen(ifr
.ifr_name
) < sizeof(config
->tapdevice
));
371 strncpy(config
->tapdevice
, ifr
.ifr_name
, sizeof(config
->tapdevice
) - 1);
372 ifrfd
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
374 sin
.sin_family
= AF_INET
;
375 sin
.sin_addr
.s_addr
= config
->bind_address
? config
->bind_address
: 0x01010101; // 1.1.1.1
376 memcpy(&ifr
.ifr_addr
, &sin
, sizeof(struct sockaddr
));
378 if (ioctl(ifrfd
, SIOCSIFADDR
, (void *) &ifr
) < 0)
380 log(0, 0, 0, 0, "Error setting tun address: %s\n", strerror(errno
));
383 /* Bump up the qlen to deal with bursts from the network */
385 if (ioctl(ifrfd
, SIOCSIFTXQLEN
, (void *) &ifr
) < 0)
387 log(0, 0, 0, 0, "Error setting tun queue length: %s\n", strerror(errno
));
390 ifr
.ifr_flags
= IFF_UP
;
391 if (ioctl(ifrfd
, SIOCSIFFLAGS
, (void *) &ifr
) < 0)
393 log(0, 0, 0, 0, "Error setting tun flags: %s\n", strerror(errno
));
396 if (ioctl(ifrfd
, SIOCGIFHWADDR
, (void *) &ifr
) < 0)
398 log(0, 0, 0, 0, "Error setting tun hardware address: %s\n", strerror(errno
));
401 memcpy(&tapmac
, 2 + (u8
*) & ifr
.ifr_hwaddr
, 6);
402 if (ioctl(ifrfd
, SIOCGIFINDEX
, (void *) &ifr
) < 0)
404 log(0, 0, 0, 0, "Error setting tun ifindex: %s\n", strerror(errno
));
407 tapidx
= ifr
.ifr_ifindex
;
414 struct sockaddr_in addr
;
417 memset(&addr
, 0, sizeof(addr
));
418 addr
.sin_family
= AF_INET
;
419 addr
.sin_port
= htons(L2TPPORT
);
420 addr
.sin_addr
.s_addr
= config
->bind_address
;
421 udpfd
= socket(AF_INET
, SOCK_DGRAM
, UDP
);
422 setsockopt(udpfd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
424 int flags
= fcntl(udpfd
, F_GETFL
, 0);
425 fcntl(udpfd
, F_SETFL
, flags
| O_NONBLOCK
);
427 if (bind(udpfd
, (void *) &addr
, sizeof(addr
)) < 0)
429 log(0, 0, 0, 0, "Error in UDP bind: %s\n", strerror(errno
));
432 snoopfd
= socket(AF_INET
, SOCK_DGRAM
, UDP
);
433 snoop_addr
.sin_family
= AF_INET
;
436 memset(&addr
, 0, sizeof(addr
));
437 addr
.sin_family
= AF_INET
;
438 addr
.sin_port
= htons(1702);
439 controlfd
= socket(AF_INET
, SOCK_DGRAM
, 17);
440 setsockopt(controlfd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
441 if (bind(controlfd
, (void *) &addr
, sizeof(addr
)) < 0)
443 log(0, 0, 0, 0, "Error in control bind: %s\n", strerror(errno
));
449 // Find session by IP, < 1 for not found
451 // Confusingly enough, this 'ip' must be
452 // in _network_ order. This being the common
453 // case when looking it up from IP packet headers.
455 // We actually use this cache for two things.
456 // #1. For used IP addresses, this maps to the
457 // session ID that it's used by.
458 // #2. For un-used IP addresses, this maps to the
459 // index into the pool table that contains that
463 int lookup_ipmap(ipt ip
)
466 char **d
= (char **) ip_hash
;
469 if (!(d
= (char **) d
[(size_t) *a
++])) return 0;
470 if (!(d
= (char **) d
[(size_t) *a
++])) return 0;
471 if (!(d
= (char **) d
[(size_t) *a
++])) return 0;
473 s
= (ipt
) d
[(size_t) *a
];
477 sessionidt
sessionbyip(ipt ip
)
479 int s
= lookup_ipmap(ip
);
480 CSTAT(call_sessionbyip
);
482 if (s
> 0 && s
< MAXSESSION
&& session
[s
].tunnel
)
488 // Take an IP address in HOST byte order and
489 // add it to the sessionid by IP cache.
491 // (It's actually cached in network order)
493 static void cache_ipmap(ipt ip
, int s
)
495 ipt nip
= htonl(ip
); // MUST be in network order. I.e. MSB must in be ((char*)(&ip))[0]
497 char **d
= (char **) ip_hash
;
500 for (i
= 0; i
< 3; i
++)
502 if (!d
[(size_t) a
[i
]])
504 if (!(d
[(size_t) a
[i
]] = calloc(256, sizeof (void *))))
508 d
= (char **) d
[(size_t) a
[i
]];
511 d
[(size_t) a
[3]] = (char *)((int)s
);
514 log(4, ip
, s
, session
[s
].tunnel
, "Caching ip address %s\n", inet_toa(nip
));
516 log(4, ip
, 0, 0, "Un-caching ip address %s\n", inet_toa(nip
));
517 // else a map to an ip pool index.
520 static void uncache_ipmap(ipt ip
)
522 cache_ipmap(ip
, 0); // Assign it to the NULL session.
526 // CLI list to dump current ipcache.
528 int cmd_show_ipcache(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
530 char **d
= (char **) ip_hash
, **e
, **f
, **g
;
534 if (CLI_HELP_REQUESTED
)
535 return CLI_HELP_NO_ARGS
;
537 cli_print(cli
, "%7s %s", "Sess#", "IP Address");
539 for (i
= 0; i
< 256; ++i
) {
542 for (j
= 0; j
< 256; ++j
) {
545 for (k
= 0; k
< 256; ++k
) {
548 for (l
= 0; l
< 256; ++l
) {
550 cli_print(cli
, "%7d %d.%d.%d.%d", (int) g
[l
], i
, j
, k
, l
);
556 cli_print(cli
, "%d entries in cache", count
);
561 // Find session by username, 0 for not found
562 // walled garden users aren't authenticated, so the username is
563 // reasonably useless. Ignore them to avoid incorrect actions
565 // This is VERY inefficent. Don't call it often. :)
567 sessionidt
sessionbyuser(char *username
)
570 CSTAT(call_sessionbyuser
);
572 for (s
= 1; s
< MAXSESSION
; ++s
) {
573 if (session
[s
].walled_garden
)
574 continue; // Skip walled garden users.
576 if (!strncmp(session
[s
].user
, username
, 128))
580 return 0; // Not found.
583 void send_garp(ipt ip
)
589 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
592 log(0, 0, 0, 0, "Error creating socket for GARP: %s\n", strerror(errno
));
595 memset(&ifr
, 0, sizeof(ifr
));
596 strncpy(ifr
.ifr_name
, "eth0", sizeof(ifr
.ifr_name
) - 1);
597 if (ioctl(s
, SIOCGIFHWADDR
, &ifr
) < 0)
599 log(0, 0, 0, 0, "Error getting eth0 hardware address for GARP: %s\n", strerror(errno
));
603 memcpy(mac
, &ifr
.ifr_hwaddr
.sa_data
, 6*sizeof(char));
604 if (ioctl(s
, SIOCGIFINDEX
, &ifr
) < 0)
606 log(0, 0, 0, 0, "Error getting eth0 interface index for GARP: %s\n", strerror(errno
));
611 sendarp(ifr
.ifr_ifindex
, mac
, ip
);
614 // Find session by username, 0 for not found
615 sessiont
*sessiontbysessionidt(sessionidt s
)
617 if (!s
|| s
> MAXSESSION
) return NULL
;
621 sessionidt
sessionidtbysessiont(sessiont
*s
)
623 sessionidt val
= s
-session
;
624 if (s
< session
|| val
> MAXSESSION
) return 0;
628 // Handle ARP requests
629 void processarp(u8
* buf
, int len
)
634 CSTAT(call_processarp
);
639 log(0, 0, 0, 0, "Unexpected length ARP %d bytes\n", len
);
643 if (*(u16
*) (buf
+ 16) != htons(PKTARP
))
645 log(0, 0, 0, 0, "Unexpected ARP type %04X\n", ntohs(*(u16
*) (buf
+ 16)));
649 if (*(u16
*) (buf
+ 18) != htons(0x0001))
651 log(0, 0, 0, 0, "Unexpected ARP hard type %04X\n", ntohs(*(u16
*) (buf
+ 18)));
655 if (*(u16
*) (buf
+ 20) != htons(PKTIP
))
657 log(0, 0, 0, 0, "Unexpected ARP prot type %04X\n", ntohs(*(u16
*) (buf
+ 20)));
663 log(0, 0, 0, 0, "Unexpected ARP hard len %d\n", buf
[22]);
669 log(0, 0, 0, 0, "Unexpected ARP prot len %d\n", buf
[23]);
673 if (*(u16
*) (buf
+ 24) != htons(0x0001))
675 log(0, 0, 0, 0, "Unexpected ARP op %04X\n", ntohs(*(u16
*) (buf
+ 24)));
679 ip
= *(u32
*) (buf
+ 42);
684 log(3, ntohl(ip
), s
, session
[s
].tunnel
, "ARP reply for %s\n", inet_toa(ip
));
685 memcpy(buf
+ 4, buf
+ 10, 6); // set destination as source
686 *(u16
*) (buf
+ 10) = htons(tapmac
[0]); // set source address
687 *(u16
*) (buf
+ 12) = htons(tapmac
[1]);
688 *(u16
*) (buf
+ 14) = htons(tapmac
[2]);
689 *(u16
*) (buf
+ 24) = htons(0x0002); // ARP reply
690 memcpy(buf
+ 26, buf
+ 10, 6); // sender ethernet
691 memcpy(buf
+ 36, buf
+ 4, 6); // target ethernet
692 *(u32
*) (buf
+ 42) = *(u32
*) (buf
+ 32); // target IP
693 *(u32
*) (buf
+ 32) = ip
; // sender IP
694 write(tapfd
, buf
, len
);
699 log(3, ntohl(ip
), 0, 0, "ARP request for unknown IP %s\n", inet_toa(ip
));
704 // actually send a control message for a specific tunnel
705 void tunnelsend(u8
* buf
, u16 l
, tunnelidt t
)
707 struct sockaddr_in addr
;
709 CSTAT(call_tunnelsend
);
713 static int backtrace_count
= 0;
714 log(0, 0, 0, t
, "tunnelsend called with 0 as tunnel id\n");
715 STAT(tunnel_tx_errors
);
716 log_backtrace(backtrace_count
, 5)
722 static int backtrace_count
= 0;
723 log(1, 0, 0, t
, "Error sending data out tunnel: no remote endpoint (tunnel not set up)\n");
724 log_backtrace(backtrace_count
, 5)
725 STAT(tunnel_tx_errors
);
728 memset(&addr
, 0, sizeof(addr
));
729 addr
.sin_family
= AF_INET
;
730 *(u32
*) & addr
.sin_addr
= htonl(tunnel
[t
].ip
);
731 addr
.sin_port
= htons(tunnel
[t
].port
);
733 // sequence expected, if sequence in message
734 if (*buf
& 0x08) *(u16
*) (buf
+ ((*buf
& 0x40) ? 10 : 8)) = htons(tunnel
[t
].nr
);
736 // If this is a control message, deal with retries
739 tunnel
[t
].last
= time_now
; // control message sent
740 tunnel
[t
].retry
= backoff(tunnel
[t
].try); // when to resend
741 if (tunnel
[t
].try > 1)
743 STAT(tunnel_retries
);
744 log(3, tunnel
[t
].ip
, 0, t
, "Control message resend try %d\n", tunnel
[t
].try);
748 if (sendto(udpfd
, buf
, l
, 0, (void *) &addr
, sizeof(addr
)) < 0)
750 log(0, tunnel
[t
].ip
, ntohs((*(u16
*) (buf
+ 6))), t
, "Error sending data out tunnel: %s (udpfd=%d, buf=%p, len=%d, dest=%s)\n",
751 strerror(errno
), udpfd
, buf
, l
, inet_ntoa(addr
.sin_addr
));
752 STAT(tunnel_tx_errors
);
756 log_hex(5, "Send Tunnel Data", buf
, l
);
757 STAT(tunnel_tx_packets
);
758 INC_STAT(tunnel_tx_bytes
, l
);
762 // Tiny helper function to write data to
765 int tun_write(u8
* data
, int size
)
767 return write(tapfd
, data
, size
);
770 // process outgoing (to tunnel) IP
772 void processipout(u8
* buf
, int len
)
779 char * data
= buf
; // Keep a copy of the originals.
784 CSTAT(call_processipout
);
786 if (len
< MIN_IP_SIZE
)
788 log(1, 0, 0, 0, "Short IP, %d bytes\n", len
);
789 STAT(tunnel_tx_errors
);
794 log(1, 0, 0, 0, "Oversize IP packet %d bytes\n", len
);
795 STAT(tunnel_tx_errors
);
799 // Skip the tun header
803 // Got an IP header now
804 if (*(u8
*)(buf
) >> 4 != 4)
806 log(1, 0, 0, 0, "IP: Don't understand anything except IPv4\n");
810 ip
= *(u32
*)(buf
+ 16);
811 if (!(s
= sessionbyip(ip
)))
813 // Is this a packet for a session that doesn't exist?
814 static int rate
= 0; // Number of ICMP packets we've sent this second.
815 static int last
= 0; // Last time we reset the ICMP packet counter 'rate'.
817 if (last
!= time_now
) {
822 if (rate
++ < config
->icmp_rate
) // Only send a max of icmp_rate per second.
824 log(4, 0, 0, 0, "IP: Sending ICMP host unreachable to %s\n", inet_toa(*(u32
*)(buf
+ 12)));
825 host_unreachable(*(u32
*)(buf
+ 12), *(u16
*)(buf
+ 4), ip
, buf
, (len
< 64) ? 64 : len
);
829 t
= session
[s
].tunnel
;
834 // Are we throttling this session?
835 if (config
->cluster_iam_master
)
836 tbf_queue_packet(sp
->tbf_out
, data
, size
);
838 master_throttle_packet(sp
->tbf_out
, data
, size
);
841 else if (sp
->walled_garden
&& !config
->cluster_iam_master
)
843 // We are walled-gardening this
844 master_garden_packet(s
, data
, size
);
848 // Snooping this session, send it to intercept box
849 if (sp
->snoop_ip
&& sp
->snoop_port
)
850 snoop_send_packet(buf
, len
, sp
->snoop_ip
, sp
->snoop_port
);
852 log(5, session
[s
].ip
, s
, t
, "Ethernet -> Tunnel (%d bytes)\n", len
);
854 // Add on L2TP header
856 u8
*p
= makeppp(b
, sizeof(b
), buf
, len
, t
, s
, PPPIP
);
858 log(3, session
[s
].ip
, s
, t
, "failed to send packet in processipout.\n");
861 tunnelsend(b
, len
+ (p
-b
), t
); // send it...
864 sp
->cout
+= len
; // byte count
865 sp
->total_cout
+= len
; // byte count
868 sess_count
[s
].cout
+= len
; // To send to master..
872 // Helper routine for the TBF filters.
873 // Used to send queued data in to the user!
875 void send_ipout(sessionidt s
, u8
*buf
, int len
)
883 if (len
< 0 || len
> MAXETHER
) {
884 log(1,0,0,0, "Odd size IP packet: %d bytes\n", len
);
888 // Skip the tun header
892 ip
= *(u32
*)(buf
+ 16);
896 t
= session
[s
].tunnel
;
899 log(5, session
[s
].ip
, s
, t
, "Ethernet -> Tunnel (%d bytes)\n", len
);
901 // Snooping this session, send it to intercept box
902 if (sp
->snoop_ip
&& sp
->snoop_port
)
903 snoop_send_packet(buf
, len
, sp
->snoop_ip
, sp
->snoop_port
);
905 // Add on L2TP header
907 u8
*p
= makeppp(b
, sizeof(b
), buf
, len
, t
, s
, PPPIP
);
909 log(3, session
[s
].ip
, s
, t
, "failed to send packet in send_ipout.\n");
912 tunnelsend(b
, len
+ (p
-b
), t
); // send it...
914 sp
->cout
+= len
; // byte count
915 sp
->total_cout
+= len
; // byte count
918 sess_count
[s
].cout
+= len
; // To send to master..
921 // add an AVP (16 bit)
922 void control16(controlt
* c
, u16 avp
, u16 val
, u8 m
)
924 u16 l
= (m
? 0x8008 : 0x0008);
925 *(u16
*) (c
->buf
+ c
->length
+ 0) = htons(l
);
926 *(u16
*) (c
->buf
+ c
->length
+ 2) = htons(0);
927 *(u16
*) (c
->buf
+ c
->length
+ 4) = htons(avp
);
928 *(u16
*) (c
->buf
+ c
->length
+ 6) = htons(val
);
932 // add an AVP (32 bit)
933 void control32(controlt
* c
, u16 avp
, u32 val
, u8 m
)
935 u16 l
= (m
? 0x800A : 0x000A);
936 *(u16
*) (c
->buf
+ c
->length
+ 0) = htons(l
);
937 *(u16
*) (c
->buf
+ c
->length
+ 2) = htons(0);
938 *(u16
*) (c
->buf
+ c
->length
+ 4) = htons(avp
);
939 *(u32
*) (c
->buf
+ c
->length
+ 6) = htonl(val
);
943 // add an AVP (32 bit)
944 void controls(controlt
* c
, u16 avp
, char *val
, u8 m
)
946 u16 l
= ((m
? 0x8000 : 0) + strlen(val
) + 6);
947 *(u16
*) (c
->buf
+ c
->length
+ 0) = htons(l
);
948 *(u16
*) (c
->buf
+ c
->length
+ 2) = htons(0);
949 *(u16
*) (c
->buf
+ c
->length
+ 4) = htons(avp
);
950 memcpy(c
->buf
+ c
->length
+ 6, val
, strlen(val
));
951 c
->length
+= 6 + strlen(val
);
955 void controlb(controlt
* c
, u16 avp
, char *val
, unsigned int len
, u8 m
)
957 u16 l
= ((m
? 0x8000 : 0) + len
+ 6);
958 *(u16
*) (c
->buf
+ c
->length
+ 0) = htons(l
);
959 *(u16
*) (c
->buf
+ c
->length
+ 2) = htons(0);
960 *(u16
*) (c
->buf
+ c
->length
+ 4) = htons(avp
);
961 memcpy(c
->buf
+ c
->length
+ 6, val
, len
);
962 c
->length
+= 6 + len
;
965 // new control connection
966 controlt
*controlnew(u16 mtype
)
970 c
= malloc(sizeof(controlt
));
974 controlfree
= c
->next
;
978 *(u16
*) (c
->buf
+ 0) = htons(0xC802); // flags/ver
980 control16(c
, 0, mtype
, 1);
984 // send zero block if nothing is waiting
986 void controlnull(tunnelidt t
)
989 if (tunnel
[t
].controlc
) // Messages queued; They will carry the ack.
992 *(u16
*) (buf
+ 0) = htons(0xC802); // flags/ver
993 *(u16
*) (buf
+ 2) = htons(12); // length
994 *(u16
*) (buf
+ 4) = htons(tunnel
[t
].far
); // tunnel
995 *(u16
*) (buf
+ 6) = htons(0); // session
996 *(u16
*) (buf
+ 8) = htons(tunnel
[t
].ns
); // sequence
997 *(u16
*) (buf
+ 10) = htons(tunnel
[t
].nr
); // sequence
998 tunnelsend(buf
, 12, t
);
1001 // add a control message to a tunnel, and send if within window
1002 void controladd(controlt
* c
, tunnelidt t
, sessionidt s
)
1004 *(u16
*) (c
->buf
+ 2) = htons(c
->length
); // length
1005 *(u16
*) (c
->buf
+ 4) = htons(tunnel
[t
].far
); // tunnel
1006 *(u16
*) (c
->buf
+ 6) = htons(s
? session
[s
].far
: 0); // session
1007 *(u16
*) (c
->buf
+ 8) = htons(tunnel
[t
].ns
); // sequence
1008 tunnel
[t
].ns
++; // advance sequence
1009 // link in message in to queue
1010 if (tunnel
[t
].controlc
)
1011 tunnel
[t
].controle
->next
= c
;
1013 tunnel
[t
].controls
= c
;
1014 tunnel
[t
].controle
= c
;
1015 tunnel
[t
].controlc
++;
1016 // send now if space in window
1017 if (tunnel
[t
].controlc
<= tunnel
[t
].window
)
1019 tunnel
[t
].try = 0; // first send
1020 tunnelsend(c
->buf
, c
->length
, t
);
1025 // Throttle or Unthrottle a session
1027 // Throttle the data folling through a session
1028 // to be no more than 'throttle' kbit/sec each way.
1030 int throttle_session(sessionidt s
, int throttle
)
1032 if (!session
[s
].tunnel
)
1033 return 0; // No-one home.
1035 if (!*session
[s
].user
)
1036 return 0; // User not logged in
1039 if (session
[s
].tbf_in
|| session
[s
].tbf_out
) {
1040 if (throttle
== session
[s
].throttle
)
1043 // Currently throttled but the rate is changing.
1045 free_tbf(session
[s
].tbf_in
);
1046 free_tbf(session
[s
].tbf_out
);
1049 session
[s
].tbf_in
= new_tbf(s
, throttle
*1024/4, throttle
*1024/8, send_ipin
);
1050 session
[s
].tbf_out
= new_tbf(s
, throttle
*1024/4, throttle
*1024/8, send_ipout
);
1052 if (throttle
!= session
[s
].throttle
) { // Changed. Flood to slaves.
1053 session
[s
].throttle
= throttle
;
1054 cluster_send_session(s
);
1060 // else Unthrottling.
1062 if (!session
[s
].tbf_in
&& !session
[s
].tbf_out
&& !session
[s
].throttle
)
1065 free_tbf(session
[s
].tbf_in
);
1066 session
[s
].tbf_in
= 0;
1068 free_tbf(session
[s
].tbf_out
);
1069 session
[s
].tbf_out
= 0;
1071 if (throttle
!= session
[s
].throttle
) { // Changed. Flood to slaves.
1072 session
[s
].throttle
= throttle
;
1073 cluster_send_session(s
);
1079 // start tidy shutdown of session
1080 void sessionshutdown(sessionidt s
, char *reason
)
1082 int dead
= session
[s
].die
;
1083 int walled_garden
= session
[s
].walled_garden
;
1086 CSTAT(call_sessionshutdown
);
1088 if (!session
[s
].tunnel
)
1090 log(3, session
[s
].ip
, s
, session
[s
].tunnel
, "Called sessionshutdown on a session with no tunnel.\n");
1091 return; // not a live session
1094 if (!session
[s
].die
)
1095 log(2, 0, s
, session
[s
].tunnel
, "Shutting down session %d: %s\n", s
, reason
);
1097 session
[s
].die
= now() + 150; // Clean up in 15 seconds
1100 struct param_kill_session data
= { &tunnel
[session
[s
].tunnel
], &session
[s
] };
1101 run_plugins(PLUGIN_KILL_SESSION
, &data
);
1104 // RADIUS Stop message
1105 if (session
[s
].opened
&& !walled_garden
&& !dead
) {
1106 u16 r
= session
[s
].radius
;
1109 if (!(r
= radiusnew(s
)))
1111 log(1, 0, s
, session
[s
].tunnel
, "No free RADIUS sessions for Stop message\n");
1112 STAT(radius_overflow
);
1117 for (n
= 0; n
< 15; n
++)
1118 radius
[r
].auth
[n
] = rand();
1121 if (r
&& radius
[r
].state
!= RADIUSSTOP
)
1122 radiussend(r
, RADIUSSTOP
); // stop, if not already trying
1126 { // IP allocated, clear and unroute
1128 for (r
= 0; r
< MAXROUTE
&& session
[s
].route
[r
].ip
; r
++)
1130 routeset(s
, session
[s
].route
[r
].ip
, session
[s
].route
[r
].mask
, session
[s
].ip
, 0);
1131 session
[s
].route
[r
].ip
= 0;
1134 if (session
[s
].ip_pool_index
== -1) // static ip
1136 routeset(s
, session
[s
].ip
, 0, 0, 0); // Delete route.
1142 if (session
[s
].throttle
) // Unthrottle if throttled.
1143 throttle_session(s
, 0);
1146 controlt
*c
= controlnew(14); // sending CDN
1147 control16(c
, 1, 3, 1); // result code (admin reasons - TBA make error, general error, add message
1148 control16(c
, 14, s
, 1); // assigned session (our end)
1149 controladd(c
, session
[s
].tunnel
, s
); // send the message
1151 cluster_send_session(s
);
1154 void sendipcp(tunnelidt t
, sessionidt s
)
1157 u16 r
= session
[s
].radius
;
1160 CSTAT(call_sendipcp
);
1164 if (radius
[r
].state
!= RADIUSIPCP
)
1166 radius
[r
].state
= RADIUSIPCP
;
1169 radius
[r
].retry
= backoff(radius
[r
].try++);
1170 if (radius
[r
].try > 10)
1172 sessionshutdown(s
, "No reply on IPCP");
1176 q
= makeppp(buf
,sizeof(buf
), 0, 0, t
, s
, PPPIPCP
);
1178 log(3, session
[s
].ip
, s
, t
, "failed to send packet in sendipcp.\n");
1183 q
[1] = r
<< RADIUS_SHIFT
; // ID, dont care, we only send one type of request
1184 *(u16
*) (q
+ 2) = htons(10);
1187 *(u32
*) (q
+ 6) = config
->bind_address
; // send my IP
1188 tunnelsend(buf
, 10 + (q
- buf
), t
); // send it
1189 session
[s
].flags
&= ~SF_IPCP_ACKED
; // Clear flag.
1192 // kill a session now
1193 void sessionkill(sessionidt s
, char *reason
)
1196 CSTAT(call_sessionkill
);
1198 sessionshutdown(s
, reason
); // close radius/routes, etc.
1199 if (session
[s
].radius
)
1200 radiusclear(session
[s
].radius
, 0); // cant send clean accounting data, session is killed
1201 log(2, 0, s
, session
[s
].tunnel
, "Kill session %d (%s): %s\n", s
, session
[s
].user
, reason
);
1203 throttle_session(s
, 0); // Force session to be un-throttle. Free'ing TBF structures.
1205 memset(&session
[s
], 0, sizeof(session
[s
]));
1206 session
[s
].tunnel
= T_FREE
; // Mark it as free.
1207 session
[s
].next
= sessionfree
;
1209 cluster_send_session(s
);
1212 // kill a tunnel now
1213 void tunnelkill(tunnelidt t
, char *reason
)
1218 CSTAT(call_tunnelkill
);
1220 tunnel
[t
].state
= TUNNELDIE
;
1222 // free control messages
1223 while ((c
= tunnel
[t
].controls
))
1225 controlt
* n
= c
->next
;
1226 tunnel
[t
].controls
= n
;
1227 tunnel
[t
].controlc
--;
1228 c
->next
= controlfree
;
1232 for (s
= 1; s
< MAXSESSION
; s
++)
1233 if (session
[s
].tunnel
== t
)
1234 sessionkill(s
, reason
);
1238 cluster_send_tunnel(t
);
1239 log(1, 0, 0, t
, "Kill tunnel %d: %s\n", t
, reason
);
1241 tunnel
[t
].state
= TUNNELFREE
;
1244 // shut down a tunnel cleanly
1245 void tunnelshutdown(tunnelidt t
, char *reason
)
1249 CSTAT(call_tunnelshutdown
);
1251 if (!tunnel
[t
].last
|| !tunnel
[t
].far
|| tunnel
[t
].state
== TUNNELFREE
)
1253 // never set up, can immediately kill
1254 tunnelkill(t
, reason
);
1257 log(1, 0, 0, t
, "Shutting down tunnel %d (%s)\n", t
, reason
);
1260 for (s
= 1; s
< MAXSESSION
; s
++)
1261 if (session
[s
].tunnel
== t
)
1262 sessionkill(s
, reason
);
1264 tunnel
[t
].state
= TUNNELDIE
;
1265 tunnel
[t
].die
= now() + 700; // Clean up in 70 seconds
1266 cluster_send_tunnel(t
);
1267 // TBA - should we wait for sessions to stop?
1269 controlt
*c
= controlnew(4); // sending StopCCN
1270 control16(c
, 1, 1, 1); // result code (admin reasons - TBA make error, general error, add message
1271 control16(c
, 9, t
, 1); // assigned tunnel (our end)
1272 controladd(c
, t
, 0); // send the message
1276 // read and process packet on tunnel (UDP)
1277 void processudp(u8
* buf
, int len
, struct sockaddr_in
*addr
)
1279 char *chapresponse
= NULL
;
1280 u16 l
= len
, t
= 0, s
= 0, ns
= 0, nr
= 0;
1284 CSTAT(call_processudp
);
1288 log_hex(5, "UDP Data", buf
, len
);
1289 STAT(tunnel_rx_packets
);
1290 INC_STAT(tunnel_rx_bytes
, len
);
1293 log(1, ntohl(addr
->sin_addr
.s_addr
), 0, 0, "Short UDP, %d bytes\n", len
);
1294 STAT(tunnel_rx_errors
);
1297 if ((buf
[1] & 0x0F) != 2)
1299 log(1, ntohl(addr
->sin_addr
.s_addr
), 0, 0, "Bad L2TP ver %d\n", (buf
[1] & 0x0F) != 2);
1300 STAT(tunnel_rx_errors
);
1305 l
= ntohs(*(u16
*) p
);
1308 t
= ntohs(*(u16
*) p
);
1310 s
= ntohs(*(u16
*) p
);
1312 if (s
>= MAXSESSION
)
1314 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Received UDP packet with invalid session ID\n");
1315 STAT(tunnel_rx_errors
);
1320 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Received UDP packet with invalid tunnel ID\n");
1321 STAT(tunnel_rx_errors
);
1326 ns
= ntohs(*(u16
*) p
);
1328 nr
= ntohs(*(u16
*) p
);
1333 u16 o
= ntohs(*(u16
*) p
);
1338 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Bad length %d>%d\n", (p
- buf
), l
);
1339 STAT(tunnel_rx_errors
);
1345 u16 message
= 0xFFFF; // message type
1347 u8 mandatorymessage
= 0;
1348 u8 chap
= 0; // if CHAP being used
1349 u16 asession
= 0; // assigned session
1350 u32 amagic
= 0; // magic number
1351 u8 aflags
= 0; // flags from last LCF
1352 u16 version
= 0x0100; // protocol version (we handle 0.0 as well and send that back just in case)
1353 int requestchap
= 0; // do we request PAP instead of original CHAP request?
1354 char called
[MAXTEL
] = ""; // called number
1355 char calling
[MAXTEL
] = ""; // calling number
1357 if (!config
->cluster_iam_master
) {
1358 master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
);
1362 if ((*buf
& 0xCA) != 0xC8)
1364 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Bad control header %02X\n", *buf
);
1365 STAT(tunnel_rx_errors
);
1368 log(3, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Control message (%d bytes): (unacked %d) l-ns %d l-nr %d r-ns %d r-nr %d\n",
1369 l
, tunnel
[t
].controlc
, tunnel
[t
].ns
, tunnel
[t
].nr
, ns
, nr
);
1370 // if no tunnel specified, assign one
1376 // Is this a duplicate of the first packet? (SCCRQ)
1378 for ( i
= 1; i
<= config
->cluster_highest_tunnelid
; ++i
) {
1379 if (tunnel
[t
].state
!= TUNNELOPENING
||
1380 tunnel
[t
].ip
!= ntohl(*(ipt
*) & addr
->sin_addr
) ||
1381 tunnel
[t
].port
!= ntohs(addr
->sin_port
) )
1389 if (!(t
= new_tunnel()))
1391 log(1, ntohl(addr
->sin_addr
.s_addr
), 0, 0, "No more tunnels\n");
1392 STAT(tunnel_overflow
);
1396 tunnel
[t
].ip
= ntohl(*(ipt
*) & addr
->sin_addr
);
1397 tunnel
[t
].port
= ntohs(addr
->sin_port
);
1398 tunnel
[t
].window
= 4; // default window
1399 log(1, ntohl(addr
->sin_addr
.s_addr
), 0, t
, " New tunnel from %u.%u.%u.%u/%u ID %d\n", tunnel
[t
].ip
>> 24, tunnel
[t
].ip
>> 16 & 255, tunnel
[t
].ip
>> 8 & 255, tunnel
[t
].ip
& 255, tunnel
[t
].port
, t
);
1400 STAT(tunnel_created
);
1403 // This is used to time out old tunnels
1404 tunnel
[t
].lastrec
= time_now
;
1406 // check sequence of this message
1408 int skip
= tunnel
[t
].window
; // track how many in-window packets are still in queue
1409 // some to clear maybe?
1410 while (tunnel
[t
].controlc
&& (((tunnel
[t
].ns
- tunnel
[t
].controlc
) - nr
) & 0x8000))
1412 controlt
*c
= tunnel
[t
].controls
;
1413 tunnel
[t
].controls
= c
->next
;
1414 tunnel
[t
].controlc
--;
1415 c
->next
= controlfree
;
1418 tunnel
[t
].try = 0; // we have progress
1421 // If the 'ns' just received is not the 'nr' we're
1422 // expecting, just send an ack and drop it.
1424 // if 'ns' is less, then we got a retransmitted packet.
1425 // if 'ns' is greater than missed a packet. Either way
1426 // we should ignore it.
1427 if (ns
!= tunnel
[t
].nr
)
1429 // is this the sequence we were expecting?
1430 log(1, ntohl(addr
->sin_addr
.s_addr
), 0, t
, " Out of sequence tunnel %d, (%d is not the expected %d)\n", t
, ns
, tunnel
[t
].nr
);
1431 STAT(tunnel_rx_errors
);
1433 if (l
) // Is this not a ZLB?
1437 // receiver advance (do here so quoted correctly in any sends below)
1438 if (l
) tunnel
[t
].nr
= (ns
+ 1);
1439 if (skip
< 0) skip
= 0;
1440 if (skip
< tunnel
[t
].controlc
)
1442 // some control packets can now be sent that were previous stuck out of window
1443 int tosend
= tunnel
[t
].window
- skip
;
1444 controlt
*c
= tunnel
[t
].controls
;
1452 tunnel
[t
].try = 0; // first send
1453 tunnelsend(c
->buf
, c
->length
, t
);
1458 if (!tunnel
[t
].controlc
)
1459 tunnel
[t
].retry
= 0; // caught up
1462 { // if not a null message
1464 while (l
&& !(fatal
& 0x80))
1466 u16 n
= (ntohs(*(u16
*) p
) & 0x3FF);
1473 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Invalid length in AVP\n");
1474 STAT(tunnel_rx_errors
);
1481 // handle hidden AVPs
1482 if (!*config
->l2tpsecret
)
1484 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Hidden AVP requested, but no L2TP secret.\n");
1488 if (!session
[s
].random_vector_length
)
1490 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Hidden AVP requested, but no random vector.\n");
1494 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Hidden AVP\n");
1498 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Unrecognised AVP flags %02X\n", *b
);
1505 log(2, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Unknown AVP vendor %d\n", ntohs(*(u16
*) (b
)));
1510 mtype
= ntohs(*(u16
*) (b
));
1514 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " AVP %d (%s) len %d\n", mtype
, avpnames
[mtype
], n
);
1517 case 0: // message type
1518 message
= ntohs(*(u16
*) b
);
1519 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Message type = %d (%s)\n", *b
,
1520 l2tp_message_types
[message
]);
1521 mandatorymessage
= flags
;
1523 case 1: // result code
1525 u16 rescode
= ntohs(*(u16
*)(b
));
1526 const char* resdesc
= "(unknown)";
1527 if (message
== 4) { /* StopCCN */
1528 if (rescode
<= MAX_STOPCCN_RESULT_CODE
)
1529 resdesc
= stopccn_result_codes
[rescode
];
1530 } else if (message
== 14) { /* CDN */
1531 if (rescode
<= MAX_CDN_RESULT_CODE
)
1532 resdesc
= cdn_result_codes
[rescode
];
1535 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Result Code %d: %s\n",
1538 u16 errcode
= ntohs(*(u16
*)(b
+ 2));
1539 const char* errdesc
= "(unknown)";
1540 if (errcode
<= MAX_ERROR_CODE
)
1541 errdesc
= error_codes
[errcode
];
1542 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Error Code %d: %s\n",
1546 /* %*s doesn't work?? */
1547 char *buf
= (char *)strndup(b
+4, n
-4);
1548 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Error String: %s\n",
1555 case 2: // protocol version
1557 version
= ntohs(*(u16
*) (b
));
1558 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Protocol version = %d\n", version
);
1559 if (version
&& version
!= 0x0100)
1560 { // allow 0.0 and 1.0
1561 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Bad protocol version %04X\n",
1568 case 3: // framing capabilities
1569 // log(4, ntohl(addr->sin_addr.s_addr), s, t, "Framing capabilities\n");
1571 case 4: // bearer capabilities
1572 // log(4, ntohl(addr->sin_addr.s_addr), s, t, "Bearer capabilities\n");
1574 case 5: // tie breaker
1575 // We never open tunnels, so we don't care about tie breakers
1576 // log(4, ntohl(addr->sin_addr.s_addr), s, t, "Tie breaker\n");
1578 case 6: // firmware revision
1579 // log(4, ntohl(addr->sin_addr.s_addr), s, t, "Firmware revision\n");
1581 case 7: // host name
1582 memset(tunnel
[t
].hostname
, 0, 128);
1583 memcpy(tunnel
[t
].hostname
, b
, (n
>= 127) ? 127 : n
);
1584 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Tunnel hostname = \"%s\"\n", tunnel
[t
].hostname
);
1585 // TBA - to send to RADIUS
1587 case 8: // vendor name
1588 memset(tunnel
[t
].vendor
, 0, 128);
1589 memcpy(tunnel
[t
].vendor
, b
, (n
>= 127) ? 127 : n
);
1590 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Vendor name = \"%s\"\n", tunnel
[t
].vendor
);
1592 case 9: // assigned tunnel
1593 tunnel
[t
].far
= ntohs(*(u16
*) (b
));
1594 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Remote tunnel id = %d\n", tunnel
[t
].far
);
1596 case 10: // rx window
1597 tunnel
[t
].window
= ntohs(*(u16
*) (b
));
1598 if (!tunnel
[t
].window
)
1599 tunnel
[t
].window
= 1; // window of 0 is silly
1600 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " rx window = %d\n", tunnel
[t
].window
);
1602 case 11: // Challenge
1604 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " LAC requested CHAP authentication for tunnel\n");
1605 build_chap_response(b
, 2, n
, &chapresponse
);
1608 case 14: // assigned session
1609 asession
= session
[s
].far
= ntohs(*(u16
*) (b
));
1610 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " assigned session = %d\n", asession
);
1612 case 15: // call serial number
1613 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " call serial number = %d\n", ntohl(*(u32
*)b
));
1615 case 18: // bearer type
1616 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " bearer type = %d\n", ntohl(*(u32
*)b
));
1619 case 19: // framing type
1620 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " framing type = %d\n", ntohl(*(u32
*)b
));
1623 case 21: // called number
1624 memset(called
, 0, MAXTEL
);
1625 memcpy(called
, b
, (n
>= MAXTEL
) ? (MAXTEL
-1) : n
);
1626 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Called <%s>\n", called
);
1628 case 22: // calling number
1629 memset(calling
, 0, MAXTEL
);
1630 memcpy(calling
, b
, (n
>= MAXTEL
) ? (MAXTEL
-1) : n
);
1631 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Calling <%s>\n", calling
);
1633 case 24: // tx connect speed
1636 session
[s
].tx_connect_speed
= ntohl(*(u32
*)b
);
1640 // AS5300s send connect speed as a string
1642 memcpy(tmp
, b
, (n
>= 30) ? 30 : n
);
1643 session
[s
].tx_connect_speed
= atol(tmp
);
1645 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " TX connect speed <%u>\n",
1646 session
[s
].tx_connect_speed
);
1648 case 38: // rx connect speed
1651 session
[s
].rx_connect_speed
= ntohl(*(u32
*)b
);
1655 // AS5300s send connect speed as a string
1657 memcpy(tmp
, b
, (n
>= 30) ? 30 : n
);
1658 session
[s
].rx_connect_speed
= atol(tmp
);
1660 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " RX connect speed <%u>\n",
1661 session
[s
].rx_connect_speed
);
1663 case 25: // Physical Channel ID
1665 u32 tmp
= ntohl(*(u32
*)b
);
1666 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Physical Channel ID <%X>\n", tmp
);
1669 case 29: // Proxy Authentication Type
1671 u16 authtype
= ntohs(*(u16
*)b
);
1672 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Proxy Auth Type %d (%s)\n",
1673 authtype
, authtypes
[authtype
]);
1674 requestchap
= (authtype
== 2);
1677 case 30: // Proxy Authentication Name
1679 char authname
[64] = {0};
1680 memcpy(authname
, b
, (n
> 63) ? 63 : n
);
1681 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Proxy Auth Name (%s)\n",
1685 case 31: // Proxy Authentication Challenge
1687 memcpy(radius
[session
[s
].radius
].auth
, b
, 16);
1688 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Proxy Auth Challenge\n");
1691 case 32: // Proxy Authentication ID
1693 u16 authid
= ntohs(*(u16
*)(b
));
1694 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Proxy Auth ID (%d)\n",
1696 if (session
[s
].radius
)
1697 radius
[session
[s
].radius
].id
= authid
;
1700 case 33: // Proxy Authentication Response
1702 char authresp
[64] = {0};
1703 memcpy(authresp
, b
, (n
> 63) ? 63 : n
);
1704 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Proxy Auth Response\n");
1707 case 27: // last send lcp
1708 { // find magic number
1709 u8
*p
= b
, *e
= p
+ n
;
1710 while (p
< e
&& p
[1])
1712 if (*p
== 5 && p
[1] == 6)
1713 amagic
= ntohl(*(u32
*) (p
+ 2));
1714 else if (*p
== 3 && p
[1] == 5 && *(u16
*) (p
+ 2) == htons(PPPCHAP
) && p
[4] == 5)
1717 aflags
|= SESSIONPFC
;
1719 aflags
|= SESSIONACFC
;
1724 char tmp
[500] = {0};
1726 memcpy((tmp
+ 1), b
, n
);
1730 case 28: // last recv lcp confreq
1732 char tmp
[500] = {0};
1734 memcpy((tmp
+ 1), b
, n
);
1737 case 26: // Initial Received LCP CONFREQ
1739 char tmp
[500] = {0};
1741 memcpy((tmp
+ 1), b
, n
);
1744 case 39: // seq required - we control it as an LNS anyway...
1746 case 36: // Random Vector
1747 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Random Vector received. Enabled AVP Hiding.\n");
1748 memset(session
[s
].random_vector
, 0, sizeof(session
[s
].random_vector
));
1749 memcpy(session
[s
].random_vector
, b
, n
);
1750 session
[s
].random_vector_length
= n
;
1753 log(2, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Unknown AVP type %d\n", mtype
);
1760 tunnelshutdown(t
, "Unknown Mandatory AVP");
1764 case 1: // SCCRQ - Start Control Connection Request
1766 controlt
*c
= controlnew(2); // sending SCCRP
1767 control16(c
, 2, version
, 1); // protocol version
1768 control32(c
, 3, 3, 1); // framing
1769 controls(c
, 7, tunnel
[t
].hostname
, 1); // host name (TBA)
1770 if (chapresponse
) controlb(c
, 13, chapresponse
, 16, 1); // Challenge response
1771 control16(c
, 9, t
, 1); // assigned tunnel
1772 controladd(c
, t
, s
); // send the resply
1774 tunnel
[t
].state
= TUNNELOPENING
;
1777 tunnel
[t
].state
= TUNNELOPEN
;
1780 tunnel
[t
].state
= TUNNELOPEN
;
1781 controlnull(t
); // ack
1784 controlnull(t
); // ack
1785 tunnelshutdown(t
, "Stopped"); // Shut down cleanly
1786 tunnelkill(t
, "Stopped"); // Immediately force everything dead
1789 controlnull(t
); // simply ACK
1803 STAT(session_overflow
);
1804 tunnelshutdown(t
, "No free sessions");
1812 sessionfree
= session
[s
].next
;
1813 memset(&session
[s
], 0, sizeof(session
[s
]));
1815 if (s
> config
->cluster_highest_sessionid
)
1816 config
->cluster_highest_sessionid
= s
;
1818 // make a RADIUS session
1819 if (!(r
= radiusnew(s
)))
1821 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "No free RADIUS sessions for ICRQ\n");
1822 // sessionkill(s, "no free RADIUS sesions");
1826 c
= controlnew(11); // sending ICRP
1827 session
[s
].id
= sessionid
++;
1828 session
[s
].opened
= time(NULL
);
1829 session
[s
].tunnel
= t
;
1830 session
[s
].far
= asession
;
1831 session
[s
].last_packet
= time_now
;
1832 log(3, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "New session (%d/%d)\n", tunnel
[t
].far
, session
[s
].far
);
1833 control16(c
, 14, s
, 1); // assigned session
1834 controladd(c
, t
, s
); // send the reply
1836 // Generate a random challenge
1838 for (n
= 0; n
< 15; n
++)
1839 radius
[r
].auth
[n
] = rand();
1841 strncpy(radius
[r
].calling
, calling
, sizeof(radius
[r
].calling
) - 1);
1842 strncpy(session
[s
].called
, called
, sizeof(session
[s
].called
) - 1);
1843 strncpy(session
[s
].calling
, calling
, sizeof(session
[s
].calling
) - 1);
1844 STAT(session_created
);
1851 session
[s
].magic
= amagic
; // set magic number
1852 session
[s
].l2tp_flags
= aflags
; // set flags received
1853 log(3, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Magic %X Flags %X\n", amagic
, aflags
);
1854 controlnull(t
); // ack
1855 // In CHAP state, request PAP instead
1860 controlnull(t
); // ack
1861 sessionshutdown(s
, "Closed (Received CDN)");
1864 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Missing message type\n");
1867 STAT(tunnel_rx_errors
);
1868 if (mandatorymessage
& 0x80)
1869 tunnelshutdown(t
, "Unknown message");
1871 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Unknown message type %d\n", message
);
1874 if (chapresponse
) free(chapresponse
);
1875 cluster_send_tunnel(t
);
1879 log(4, 0, s
, t
, " Got a ZLB ack\n");
1886 log_hex(5, "Receive Tunnel Data", p
, l
);
1887 if (l
> 2 && p
[0] == 0xFF && p
[1] == 0x03)
1888 { // HDLC address header, discard
1894 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Short ppp length %d\n", l
);
1895 STAT(tunnel_rx_errors
);
1905 prot
= ntohs(*(u16
*) p
);
1910 if (s
&& !session
[s
].tunnel
) // Is something wrong??
1912 if (!config
->cluster_iam_master
)
1914 // Pass it off to the master to deal with..
1915 master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
);
1920 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "UDP packet contains session %d "
1921 "but no session[%d].tunnel exists (LAC said"
1922 " tunnel = %d). Dropping packet.\n", s
, s
, t
);
1923 STAT(tunnel_rx_errors
);
1929 log(3, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Session %d is closing. Don't process PPP packets\n", s
);
1930 // I'm pretty sure this isn't right -- mo.
1931 // return; // closing session, PPP not processed
1935 session
[s
].last_packet
= time_now
;
1936 if (!config
->cluster_iam_master
) { master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
); return; }
1937 processpap(t
, s
, p
, l
);
1939 else if (prot
== PPPCHAP
)
1941 session
[s
].last_packet
= time_now
;
1942 if (!config
->cluster_iam_master
) { master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
); return; }
1943 processchap(t
, s
, p
, l
);
1945 else if (prot
== PPPLCP
)
1947 session
[s
].last_packet
= time_now
;
1948 if (!config
->cluster_iam_master
) { master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
); return; }
1949 processlcp(t
, s
, p
, l
);
1951 else if (prot
== PPPIPCP
)
1953 session
[s
].last_packet
= time_now
;
1954 if (!config
->cluster_iam_master
) { master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
); return; }
1955 processipcp(t
, s
, p
, l
);
1957 else if (prot
== PPPCCP
)
1959 session
[s
].last_packet
= time_now
;
1960 if (!config
->cluster_iam_master
) { master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
); return; }
1961 processccp(t
, s
, p
, l
);
1963 else if (prot
== PPPIP
)
1965 if (!config
->cluster_iam_master
)
1967 // We're a slave. Should we forward this packet to the master?
1969 // Is this a walled garden session, or something that needs it's
1970 // idle time updated??
1972 // Maintain the idle timeouts on the master. If this would
1973 // significantly reset the idletimeout, run it via the master
1974 // to refresh the master's idle timer.
1975 // Not sure this is ideal: It may re-order packets.
1977 if (session
[s
].walled_garden
|| (session
[s
].last_packet
+ (ECHO_TIMEOUT
/2)) < time_now
)
1979 master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
);
1980 session
[s
].last_packet
= time_now
;
1983 // fall through to processipin.
1985 session
[s
].last_packet
= time_now
;
1986 processipin(t
, s
, p
, l
);
1990 STAT(tunnel_rx_errors
);
1991 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Unknown PPP protocol %04X\n", prot
);
1996 // read and process packet on tap
1997 void processtap(u8
* buf
, int len
)
1999 log_hex(5, "Receive TAP Data", buf
, len
);
2000 STAT(tap_rx_packets
);
2001 INC_STAT(tap_rx_bytes
, len
);
2003 CSTAT(call_processtap
);
2009 log(1, 0, 0, 0, "Short tap packet %d bytes\n", len
);
2010 STAT(tap_rx_errors
);
2013 if (*(u16
*) (buf
+ 2) == htons(PKTARP
)) // ARP
2014 processarp(buf
, len
);
2015 else if (*(u16
*) (buf
+ 2) == htons(PKTIP
)) // IP
2016 processipout(buf
, len
);
2021 // Maximum number of actions to complete.
2022 // This is to avoid sending out too many packets
2024 #define MAX_ACTIONS 500
2026 int regular_cleanups(void)
2028 static sessionidt s
= 0; // Next session to check for actions on.
2032 static clockt next_acct
= 0;
2034 log(3, 0, 0, 0, "Begin regular cleanup\n");
2036 for (r
= 1; r
< MAXRADIUS
; r
++)
2038 if (!radius
[r
].state
)
2040 if (radius
[r
].retry
)
2042 if (radius
[r
].retry
<= TIME
)
2045 radius
[r
].retry
= backoff(radius
[r
].try+1); // Is this really needed? --mo
2047 for (t
= 1; t
< config
->cluster_highest_tunnelid
; t
++)
2049 // check for expired tunnels
2050 if (tunnel
[t
].die
&& tunnel
[t
].die
<= TIME
)
2052 STAT(tunnel_timeout
);
2053 tunnelkill(t
, "Expired");
2056 // check for message resend
2057 if (tunnel
[t
].retry
&& tunnel
[t
].controlc
)
2059 // resend pending messages as timeout on reply
2060 if (tunnel
[t
].retry
<= TIME
)
2062 controlt
*c
= tunnel
[t
].controls
;
2063 u8 w
= tunnel
[t
].window
;
2064 tunnel
[t
].try++; // another try
2065 if (tunnel
[t
].try > 5)
2066 tunnelkill(t
, "Timeout on control message"); // game over
2070 tunnelsend(c
->buf
, c
->length
, t
);
2076 if (tunnel
[t
].state
== TUNNELOPEN
&& tunnel
[t
].lastrec
< TIME
+ 600)
2078 controlt
*c
= controlnew(6); // sending HELLO
2079 controladd(c
, t
, 0); // send the message
2080 log(3, tunnel
[t
].ip
, 0, t
, "Sending HELLO message\n");
2084 // Check for sessions that have been killed from the CLI
2085 if (cli_session_kill
[0])
2088 for (i
= 0; i
< MAXSESSION
&& cli_session_kill
[i
]; i
++)
2090 log(2, 0, cli_session_kill
[i
], 0, "Dropping session by CLI\n");
2091 sessionshutdown(cli_session_kill
[i
], "Requested by administrator");
2092 cli_session_kill
[i
] = 0;
2095 // Check for tunnels that have been killed from the CLI
2096 if (cli_tunnel_kill
[0])
2099 for (i
= 1; i
< MAXTUNNEL
&& cli_tunnel_kill
[i
]; i
++)
2101 log(2, 0, cli_tunnel_kill
[i
], 0, "Dropping tunnel by CLI\n");
2102 tunnelshutdown(cli_tunnel_kill
[i
], "Requested by administrator");
2103 cli_tunnel_kill
[i
] = 0;
2108 for (i
= 1; i
<= config
->cluster_highest_sessionid
; i
++)
2112 if (s
> config
->cluster_highest_sessionid
)
2115 if (!session
[s
].tunnel
) // Session isn't in use
2118 if (!session
[s
].die
&& session
[s
].ip
&& !(session
[s
].flags
& SF_IPCP_ACKED
) )
2120 // IPCP has not completed yet. Resend
2121 log(3, session
[s
].ip
, s
, session
[s
].tunnel
, "No ACK for initial IPCP ConfigReq... resending\n");
2122 sendipcp(session
[s
].tunnel
, s
);
2125 // check for expired sessions
2126 if (session
[s
].die
&& session
[s
].die
<= TIME
)
2128 sessionkill(s
, "Expired");
2129 if (++count
>= MAX_ACTIONS
) break;
2133 // Drop sessions who have not responded within IDLE_TIMEOUT seconds
2134 if (session
[s
].last_packet
&& (time_now
- session
[s
].last_packet
>= IDLE_TIMEOUT
))
2136 sessionkill(s
, "No response to LCP ECHO requests");
2137 STAT(session_timeout
);
2138 if (++count
>= MAX_ACTIONS
) break;
2142 // No data in IDLE_TIMEOUT seconds, send LCP ECHO
2143 if (session
[s
].user
[0] && (time_now
- session
[s
].last_packet
>= ECHO_TIMEOUT
))
2145 u8 b
[MAXCONTROL
] = {0};
2147 u8
*q
= makeppp(b
, sizeof(b
), 0, 0, session
[s
].tunnel
, s
, PPPLCP
);
2149 log(3, session
[s
].ip
, s
, t
, "failed to send ECHO packet.\n");
2154 *(u8
*)(q
+ 1) = (time_now
% 255); // ID
2155 *(u16
*)(q
+ 2) = htons(8); // Length
2156 *(u32
*)(q
+ 4) = 0; // Magic Number (not supported)
2158 log(4, session
[s
].ip
, s
, session
[s
].tunnel
, "No data in %d seconds, sending LCP ECHO\n",
2159 (int)(time_now
- session
[s
].last_packet
));
2160 tunnelsend(b
, 24, session
[s
].tunnel
); // send it
2161 if (++count
>= MAX_ACTIONS
) break;
2165 if (config
->accounting_dir
&& next_acct
<= TIME
)
2167 // Dump accounting data
2168 next_acct
= TIME
+ ACCT_TIME
;
2172 if (count
>= MAX_ACTIONS
)
2173 return 1; // Didn't finish!
2175 log(3, 0, 0, 0, "End regular cleanup (%d actions), next in %d seconds\n", count
, config
->cleanup_interval
);
2181 // Are we in the middle of a tunnel update, or radius
2184 int still_busy(void)
2187 static int last_talked
= 0;
2188 for (i
= config
->cluster_highest_tunnelid
; i
> 0 ; --i
) {
2189 if (!tunnel
[i
].controlc
)
2192 if (last_talked
!= TIME
) {
2193 log(2,0,0,0, "Tunnel %d still has un-acked control messages.\n", i
);
2199 for (i
= 1; i
< MAXRADIUS
; i
++)
2201 if (radius
[i
].state
== RADIUSNULL
)
2203 if (radius
[i
].state
== RADIUSWAIT
)
2206 if (last_talked
!= TIME
) {
2207 log(2,0,0,0, "Radius session %d is still busy (sid %d)\n", i
, radius
[i
].session
);
2216 // main loop - gets packets on tap or udp and processes them
2223 time_t next_cluster_ping
= 0; // default 1 second pings.
2224 clockt next_clean
= time_now
+ config
->cleanup_interval
;
2226 log(4, 0, 0, 0, "Beginning of main loop. udpfd=%d, tapfd=%d, cluster_sockfd=%d, controlfd=%d\n",
2227 udpfd
, tapfd
, cluster_sockfd
, controlfd
);
2232 FD_SET(controlfd
, &cr
);
2234 if (cluster_sockfd
) FD_SET(cluster_sockfd
, &cr
);
2236 if (cn
< tapfd
) cn
= tapfd
;
2237 if (cn
< controlfd
) cn
= controlfd
;
2238 if (cn
< clifd
) cn
= clifd
;
2239 if (cn
< cluster_sockfd
) cn
= cluster_sockfd
;
2240 for (i
= 0; i
< config
->num_radfds
; i
++)
2242 if (!radfds
[i
]) continue;
2243 FD_SET(radfds
[i
], &cr
);
2248 while (!main_quit
|| still_busy())
2254 int bgp_set
[BGP_NUM_PEERS
];
2257 if (config
->reload_config
)
2259 // Update the config state based on config settings
2263 memcpy(&r
, &cr
, sizeof(fd_set
));
2265 to
.tv_usec
= 100000; // 1/10th of a second.
2269 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2271 bgp_set
[i
] = bgp_select_state(&bgp_peers
[i
]);
2274 FD_SET(bgp_peers
[i
].sock
, &r
);
2275 if (bgp_peers
[i
].sock
> n
)
2276 n
= bgp_peers
[i
].sock
;
2281 FD_SET(bgp_peers
[i
].sock
, &w
);
2282 if (bgp_peers
[i
].sock
> n
)
2283 n
= bgp_peers
[i
].sock
;
2287 n
= select(n
+ 1, &r
, &w
, 0, &to
);
2289 n
= select(n
+ 1, &r
, 0, 0, &to
);
2298 log(0, 0, 0, 0, "Error returned from select(): %s\n", strerror(errno
));
2304 struct sockaddr_in addr
;
2305 int alen
= sizeof(addr
);
2306 if (FD_ISSET(udpfd
, &r
))
2309 for (c
= 0; c
< config
->multi_read_count
; c
++)
2311 if ((n
= recvfrom(udpfd
, buf
, sizeof(buf
), 0, (void *) &addr
, &alen
)) > 0)
2312 processudp(buf
, n
, &addr
);
2317 if (FD_ISSET(tapfd
, &r
))
2320 for (c
= 0; c
< config
->multi_read_count
; c
++)
2322 if ((n
= read(tapfd
, buf
, sizeof(buf
))) > 0)
2328 for (i
= 0; i
< config
->num_radfds
; i
++)
2329 if (FD_ISSET(radfds
[i
], &r
))
2330 processrad(buf
, recv(radfds
[i
], buf
, sizeof(buf
), 0), i
);
2331 if (FD_ISSET(cluster_sockfd
, &r
)) {
2333 size
= recvfrom(cluster_sockfd
, buf
, sizeof(buf
), MSG_WAITALL
, (void *) &addr
, &alen
);
2334 processcluster(buf
, size
, addr
.sin_addr
.s_addr
);
2336 if (FD_ISSET(controlfd
, &r
))
2337 processcontrol(buf
, recvfrom(controlfd
, buf
, sizeof(buf
), MSG_WAITALL
, (void *) &addr
, &alen
), &addr
);
2338 if (FD_ISSET(clifd
, &r
))
2340 struct sockaddr_in addr
;
2342 int len
= sizeof(addr
);
2344 if ((sockfd
= accept(clifd
, (struct sockaddr
*)&addr
, &len
)) <= 0)
2346 log(0, 0, 0, 0, "accept error: %s\n", strerror(errno
));
2357 // Runs on every machine (master and slaves).
2358 if (cluster_sockfd
&& next_cluster_ping
<= time_now
)
2360 // Check to see which of the cluster is still alive..
2361 next_cluster_ping
= time_now
+ 1;
2362 cluster_send_ping(basetime
);
2364 cluster_check_master();
2366 cluster_heartbeat(config
->cluster_highest_sessionid
, sessionfree
, config
->cluster_highest_tunnelid
); // Only does anything if we're a master.
2367 master_update_counts(); // If we're a slave, send our byte counters to our master.
2370 // Run token bucket filtering queue..
2371 // Only run it every 1/10th of a second.
2372 // Runs on all machines both master and slave.
2374 static clockt last_run
= 0;
2375 if (last_run
!= TIME
) {
2381 /* Handle timeouts. Make sure that this gets run anyway, even if there was
2382 * something to read, else under load this will never actually run....
2385 if (config
->cluster_iam_master
&& next_clean
<= time_now
) {
2386 if (regular_cleanups()) { // Did it finish?
2387 next_clean
= time_now
+ 1 ; // Didn't finish. Check quickly.
2389 next_clean
= time_now
+ config
->cleanup_interval
; // Did. Move to next interval.
2394 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2396 bgp_process(&bgp_peers
[i
],
2397 bgp_set
[i
] ? FD_ISSET(bgp_peers
[i
].sock
, &r
) : 0,
2398 bgp_set
[i
] ? FD_ISSET(bgp_peers
[i
].sock
, &w
) : 0);
2403 // Are we the master and shutting down??
2404 if (config
->cluster_iam_master
) {
2406 cluster_heartbeat(config
->cluster_highest_sessionid
, sessionfree
,
2407 config
->cluster_highest_tunnelid
); // Flush any queued changes..
2410 // Ok. Notify everyone we're shutting down. If we're
2411 // the master, this will force an election.
2412 cluster_send_ping(0);
2415 // Important!!! We MUST not process any packets past this point!
2418 // Init data structures
2423 _statistics
= mmap(NULL
, sizeof(struct Tstats
), PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
2424 if (_statistics
== MAP_FAILED
)
2426 log(0, 0, 0, 0, "Error doing mmap for _statistics: %s\n", strerror(errno
));
2429 config
= mmap(NULL
, sizeof(struct configt
), PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
2430 if (config
== MAP_FAILED
)
2432 log(0, 0, 0, 0, "Error doing mmap for configuration: %s\n", strerror(errno
));
2435 memset(config
, 0, sizeof(struct configt
));
2436 time(&config
->start_time
);
2437 strncpy(config
->config_file
, CONFIGFILE
, sizeof(config
->config_file
) - 1);
2438 tunnel
= mmap(NULL
, sizeof(tunnelt
) * MAXTUNNEL
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
2439 if (tunnel
== MAP_FAILED
)
2441 log(0, 0, 0, 0, "Error doing mmap for tunnels: %s\n", strerror(errno
));
2444 session
= mmap(NULL
, sizeof(sessiont
) * MAXSESSION
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
2445 if (session
== MAP_FAILED
)
2447 log(0, 0, 0, 0, "Error doing mmap for sessions: %s\n", strerror(errno
));
2451 sess_count
= mmap(NULL
, sizeof(sessioncountt
) * MAXSESSION
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
2452 if (sess_count
== MAP_FAILED
)
2454 log(0, 0, 0, 0, "Error doing mmap for sessions_count: %s\n", strerror(errno
));
2458 radius
= mmap(NULL
, sizeof(radiust
) * MAXRADIUS
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
2459 if (radius
== MAP_FAILED
)
2461 log(0, 0, 0, 0, "Error doing mmap for radius: %s\n", strerror(errno
));
2464 ip_address_pool
= mmap(NULL
, sizeof(ippoolt
) * MAXIPPOOL
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
2465 if (ip_address_pool
== MAP_FAILED
)
2467 log(0, 0, 0, 0, "Error doing mmap for radius: %s\n", strerror(errno
));
2471 ringbuffer
= mmap(NULL
, sizeof(struct Tringbuffer
), PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
2472 if (ringbuffer
== MAP_FAILED
)
2474 log(0, 0, 0, 0, "Error doing mmap for radius: %s\n", strerror(errno
));
2477 memset(ringbuffer
, 0, sizeof(struct Tringbuffer
));
2480 cli_session_kill
= mmap(NULL
, sizeof(sessionidt
) * MAXSESSION
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
2481 if (cli_session_kill
== MAP_FAILED
)
2483 log(0, 0, 0, 0, "Error doing mmap for cli session kill: %s\n", strerror(errno
));
2486 memset(cli_session_kill
, 0, sizeof(sessionidt
) * MAXSESSION
);
2487 cli_tunnel_kill
= mmap(NULL
, sizeof(tunnelidt
) * MAXSESSION
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
2488 if (cli_tunnel_kill
== MAP_FAILED
)
2490 log(0, 0, 0, 0, "Error doing mmap for cli tunnel kill: %s\n", strerror(errno
));
2493 memset(cli_tunnel_kill
, 0, sizeof(tunnelidt
) * MAXSESSION
);
2495 memset(tunnel
, 0, sizeof(tunnelt
) * MAXTUNNEL
);
2496 memset(session
, 0, sizeof(sessiont
) * MAXSESSION
);
2497 memset(radius
, 0, sizeof(radiust
) * MAXRADIUS
);
2498 memset(ip_address_pool
, 0, sizeof(ippoolt
) * MAXIPPOOL
);
2500 // Put all the sessions on the free list marked as undefined.
2501 for (i
= 1; i
< MAXSESSION
- 1; i
++) {
2502 session
[i
].next
= i
+ 1;
2503 session
[i
].tunnel
= T_UNDEF
; // mark it as not filled in.
2505 session
[MAXSESSION
- 1].next
= 0;
2508 // Mark all the tunnels as undefined (waiting to be filled in by a download).
2509 for (i
= 1; i
< MAXTUNNEL
- 1; i
++) {
2510 tunnel
[i
].state
= TUNNELUNDEF
; // mark it as not filled in.
2515 // Grab my hostname unless it's been specified
2516 gethostname(hostname
, sizeof(hostname
));
2518 _statistics
->start_time
= _statistics
->last_reset
= time(NULL
);
2521 bgp_peers
= mmap(NULL
, sizeof(struct bgp_peer
) * BGP_NUM_PEERS
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
2522 if (bgp_peers
== MAP_FAILED
)
2524 log(0, 0, 0, 0, "Error doing mmap for bgp: %s\n", strerror(errno
));
2530 void initiptables(void)
2532 /* Flush the tables here so that we have a clean slate */
2534 // Not needed. 'nat' is setup by garden.c
2535 // mangle isn't used (as throttling is done by tbf inhouse).
2538 int assign_ip_address(sessionidt s
)
2542 clockt best_time
= time_now
;
2543 char *u
= session
[s
].user
;
2547 CSTAT(call_assign_ip_address
);
2549 for (i
= 1; i
< ip_pool_size
; i
++)
2551 if (!ip_address_pool
[i
].address
|| ip_address_pool
[i
].assigned
)
2554 if (!session
[s
].walled_garden
&& ip_address_pool
[i
].user
[0] && !strcmp(u
, ip_address_pool
[i
].user
))
2561 if (ip_address_pool
[i
].last
< best_time
)
2564 if (!(best_time
= ip_address_pool
[i
].last
))
2565 break; // never used, grab this one
2571 log(0, 0, s
, session
[s
].tunnel
, "assign_ip_address(): out of addresses\n");
2575 session
[s
].ip
= ip_address_pool
[best
].address
;
2576 session
[s
].ip_pool_index
= best
;
2577 ip_address_pool
[best
].assigned
= 1;
2578 ip_address_pool
[best
].last
= time_now
;
2579 ip_address_pool
[best
].session
= s
;
2580 if (session
[s
].walled_garden
)
2581 /* Don't track addresses of users in walled garden (note: this
2582 means that their address isn't "sticky" even if they get
2584 ip_address_pool
[best
].user
[0] = 0;
2586 strncpy(ip_address_pool
[best
].user
, u
, sizeof(ip_address_pool
[best
].user
) - 1);
2589 log(4, ip_address_pool
[best
].address
, s
, session
[s
].tunnel
,
2590 "assign_ip_address(): %s ip address %d from pool\n", reuse
? "Reusing" : "Allocating", best
);
2595 void free_ip_address(sessionidt s
)
2597 int i
= session
[s
].ip_pool_index
;
2600 return; // what the?
2602 if (i
< 0) // Is this actually part of the ip pool?
2606 cache_ipmap(session
[s
].ip
, -i
); // Change the mapping to point back to the ip pool index.
2608 ip_address_pool
[i
].assigned
= 0;
2609 ip_address_pool
[i
].session
= 0;
2610 ip_address_pool
[i
].last
= time_now
;
2613 CSTAT(call_free_ip_address
);
2618 // Fsck the address pool against the session table.
2619 // Normally only called when we become a master.
2621 // This isn't perfect: We aren't keep tracking of which
2622 // users used to have an IP address.
2624 void rebuild_address_pool(void)
2629 // Zero the IP pool allocation, and build
2630 // a map from IP address to pool index.
2631 for (i
= 1; i
< MAXIPPOOL
; ++i
) {
2632 ip_address_pool
[i
].assigned
= 0;
2633 ip_address_pool
[i
].session
= 0;
2634 if (!ip_address_pool
[i
].address
)
2637 cache_ipmap(ip_address_pool
[i
].address
, -i
); // Map pool IP to pool index.
2640 for (i
= 0; i
< MAXSESSION
; ++i
) {
2642 if (!session
[i
].ip
|| !session
[i
].tunnel
)
2644 ipid
= - lookup_ipmap(htonl(session
[i
].ip
));
2646 if (session
[i
].ip_pool_index
< 0) { // Not allocated out of the pool.
2647 if (ipid
< 1) // Not found in the pool either? good.
2650 log(0, 0, i
, 0, "Session %d has an IP address (%s) that was marked static, but is in the pool (%d)!\n",
2651 i
, inet_toa(session
[i
].ip
), ipid
);
2653 // Fall through and process it as part of the pool.
2657 if (ipid
> MAXIPPOOL
|| ipid
< 0) {
2658 log(0, 0, i
, 0, "Session %d has a pool IP that's not found in the pool! (%d)\n", i
, ipid
);
2660 session
[i
].ip_pool_index
= ipid
;
2664 ip_address_pool
[ipid
].assigned
= 1;
2665 ip_address_pool
[ipid
].session
= i
;
2666 ip_address_pool
[ipid
].last
= time_now
;
2667 strncpy(ip_address_pool
[ipid
].user
, session
[i
].user
, sizeof(ip_address_pool
[ipid
].user
) - 1);
2668 session
[i
].ip_pool_index
= ipid
;
2669 cache_ipmap(session
[i
].ip
, i
); // Fix the ip map.
2674 // Fix the address pool to match a changed session.
2675 // (usually when the master sends us an update).
2676 void fix_address_pool(int sid
)
2680 ipid
= session
[sid
].ip_pool_index
;
2682 if (ipid
> ip_pool_size
)
2683 return; // Ignore it. rebuild_address_pool will fix it up.
2685 if (ip_address_pool
[ipid
].address
!= session
[sid
].ip
)
2686 return; // Just ignore it. rebuild_address_pool will take care of it.
2688 ip_address_pool
[ipid
].assigned
= 1;
2689 ip_address_pool
[ipid
].session
= sid
;
2690 ip_address_pool
[ipid
].last
= time_now
;
2691 strncpy(ip_address_pool
[ipid
].user
, session
[sid
].user
, sizeof(ip_address_pool
[ipid
].user
) - 1);
2695 // Add a block of addresses to the IP pool to hand out.
2697 void add_to_ip_pool(u32 addr
, u32 mask
)
2701 mask
= 0xffffffff; // Host route only.
2705 if (ip_pool_size
>= MAXIPPOOL
) // Pool is full!
2708 for (i
= addr
;(i
& mask
) == addr
; ++i
)
2710 if ((i
& 0xff) == 0 || (i
&0xff) == 255)
2711 continue; // Skip 0 and broadcast addresses.
2713 ip_address_pool
[ip_pool_size
].address
= i
;
2714 ip_address_pool
[ip_pool_size
].assigned
= 0;
2716 if (ip_pool_size
>= MAXIPPOOL
)
2718 log(0,0,0,0, "Overflowed IP pool adding %s\n", inet_toa(htonl(addr
)) );
2724 // Initialize the IP address pool
2730 memset(ip_address_pool
, 0, sizeof(ip_address_pool
));
2732 if (!(f
= fopen(IPPOOLFILE
, "r")))
2734 log(0, 0, 0, 0, "Can't load pool file " IPPOOLFILE
": %s\n", strerror(errno
));
2738 while (ip_pool_size
< MAXIPPOOL
&& fgets(buf
, 4096, f
))
2741 buf
[4095] = 0; // Force it to be zero terminated/
2743 if (*buf
== '#' || *buf
== '\n')
2744 continue; // Skip comments / blank lines
2745 if ((p
= (char *)strrchr(buf
, '\n'))) *p
= 0;
2746 if ((p
= (char *)strchr(buf
, ':')))
2750 src
= inet_addr(buf
);
2751 if (src
== INADDR_NONE
)
2753 log(0, 0, 0, 0, "Invalid address pool IP %s\n", buf
);
2756 // This entry is for a specific IP only
2757 if (src
!= config
->bind_address
)
2762 if ((p
= (char *)strchr(pool
, '/')))
2766 u32 start
= 0, mask
= 0;
2768 log(2, 0, 0, 0, "Adding IP address range %s\n", buf
);
2770 if (!*p
|| !(numbits
= atoi(p
)))
2772 log(0, 0, 0, 0, "Invalid pool range %s\n", buf
);
2775 start
= ntohl(inet_addr(pool
));
2776 mask
= (u32
)(pow(2, numbits
) - 1) << (32 - numbits
);
2778 // Add a static route for this pool
2779 log(5, 0, 0, 0, "Adding route for address pool %s/%u\n", inet_toa(htonl(start
)), 32 + mask
);
2780 routeset(0, start
, mask
, 0, 1);
2782 add_to_ip_pool(start
, mask
);
2786 // It's a single ip address
2787 add_to_ip_pool(inet_addr(pool
), 0);
2791 log(1, 0, 0, 0, "IP address pool is %d addresses\n", ip_pool_size
- 1);
2794 void snoop_send_packet(char *packet
, u16 size
, ipt destination
, u16 port
)
2796 if (!destination
|| !port
|| snoopfd
<= 0 || size
<= 0 || !packet
)
2799 snoop_addr
.sin_addr
.s_addr
= destination
;
2800 snoop_addr
.sin_port
= ntohs(port
);
2802 log(5, 0, 0, 0, "Snooping packet at %p (%d bytes) to %s:%d\n",
2803 packet
, size
, inet_toa(snoop_addr
.sin_addr
.s_addr
), htons(snoop_addr
.sin_port
));
2804 if (sendto(snoopfd
, packet
, size
, MSG_DONTWAIT
| MSG_NOSIGNAL
, (void *) &snoop_addr
, sizeof(snoop_addr
)) < 0)
2805 log(0, 0, 0, 0, "Error sending intercept packet: %s\n", strerror(errno
));
2806 STAT(packets_snooped
);
2809 void dump_acct_info()
2811 char filename
[1024];
2813 time_t t
= time(NULL
);
2818 CSTAT(call_dump_acct_info
);
2820 strftime(timestr
, 64, "%Y%m%d%H%M%S", localtime(&t
));
2821 snprintf(filename
, 1024, "%s/%s", config
->accounting_dir
, timestr
);
2823 for (i
= 0; i
< MAXSESSION
; i
++)
2825 if (!session
[i
].opened
|| !session
[i
].ip
|| !session
[i
].cin
|| !session
[i
].cout
|| !*session
[i
].user
|| session
[i
].walled_garden
)
2829 time_t now
= time(NULL
);
2830 if (!(f
= fopen(filename
, "w")))
2832 log(0, 0, 0, 0, "Can't write accounting info to %s: %s\n", filename
, strerror(errno
));
2835 log(3, 0, 0, 0, "Dumping accounting information to %s\n", filename
);
2836 fprintf(f
, "# dslwatch.pl dump file V1.01\n"
2840 "# format: username ip qos uptxoctets downrxoctets\n",
2846 log(4, 0, 0, 0, "Dumping accounting information for %s\n", session
[i
].user
);
2847 fprintf(f
, "%s %s %d %u %u\n",
2848 session
[i
].user
, // username
2849 inet_toa(htonl(session
[i
].ip
)), // ip
2850 (session
[i
].throttle
) ? 2 : 1, // qos
2851 (u32
)session
[i
].cin
, // uptxoctets
2852 (u32
)session
[i
].cout
); // downrxoctets
2854 session
[i
].pin
= session
[i
].cin
= 0;
2855 session
[i
].pout
= session
[i
].cout
= 0;
2862 int main(int argc
, char *argv
[])
2866 _program_name
= strdup(argv
[0]);
2868 time(&basetime
); // start clock
2871 while ((o
= getopt(argc
, argv
, "vc:h:a:")) >= 0)
2876 // Double fork to detach from terminal
2877 if (fork()) exit(0);
2878 if (fork()) exit(0);
2884 strncpy(hostname
, optarg
, 999);
2888 printf("Args are:\n"
2889 "\t-d\tDetach from terminal\n"
2890 "\t-c <file>\tConfig file\n"
2891 "\t-h <hostname>\tForce hostname\n"
2892 "\t-a <address>\tUse specific address\n"
2900 // Start the timer routine off
2902 strftime(time_now_string
, 64, "%Y-%m-%d %H:%M:%S", localtime(&time_now
));
2903 signal(SIGALRM
, sigalrm_handler
);
2904 siginterrupt(SIGALRM
, 0);
2913 log(0, 0, 0, 0, "L2TPNS version " VERSION
"\n");
2914 log(0, 0, 0, 0, "Copyright (c) 2003, 2004 Optus Internet Engineering\n");
2915 log(0, 0, 0, 0, "Copyright (c) 2002 FireBrick (Andrews & Arnold Ltd / Watchfront Ltd) - GPL licenced\n");
2918 rlim
.rlim_cur
= RLIM_INFINITY
;
2919 rlim
.rlim_max
= RLIM_INFINITY
;
2920 // Remove the maximum core size
2921 if (setrlimit(RLIMIT_CORE
, &rlim
) < 0)
2922 log(0, 0, 0, 0, "Can't set ulimit: %s\n", strerror(errno
));
2923 // Make core dumps go to /tmp
2927 if (config
->scheduler_fifo
)
2930 struct sched_param params
= {0};
2931 params
.sched_priority
= 1;
2933 if (get_nprocs() < 2)
2935 log(0, 0, 0, 0, "Not using FIFO scheduler, there is only 1 processor in the system.\n");
2936 config
->scheduler_fifo
= 0;
2940 if ((ret
= sched_setscheduler(0, SCHED_FIFO
, ¶ms
)) == 0)
2942 log(1, 0, 0, 0, "Using FIFO scheduler. Say goodbye to any other processes running\n");
2946 log(0, 0, 0, 0, "Error setting scheduler to FIFO: %s\n", strerror(errno
));
2947 config
->scheduler_fifo
= 0;
2952 /* Set up the cluster communications port. */
2953 if (cluster_init(config
->bind_address
) < 0)
2957 signal(SIGPIPE
, SIG_IGN
);
2958 bgp_setup(config
->as_number
);
2959 bgp_add_route(config
->bind_address
, 0xffffffff);
2960 if (*config
->bgp_peer
[0])
2961 bgp_start(&bgp_peers
[0], config
->bgp_peer
[0],
2962 config
->bgp_peer_as
[0], 0); /* 0 = routing disabled */
2964 if (*config
->bgp_peer
[1])
2965 bgp_start(&bgp_peers
[1], config
->bgp_peer
[1],
2966 config
->bgp_peer_as
[1], 0);
2970 log(1, 0, 0, 0, "Set up on interface %s\n", config
->tapdevice
);
2978 signal(SIGHUP
, sighup_handler
);
2979 signal(SIGTERM
, sigterm_handler
);
2980 signal(SIGINT
, sigterm_handler
);
2981 signal(SIGQUIT
, sigquit_handler
);
2982 signal(SIGCHLD
, sigchild_handler
);
2986 // Drop privileges here
2987 if (config
->target_uid
> 0 && geteuid() == 0)
2988 setuid(config
->target_uid
);
2993 /* try to shut BGP down cleanly; with luck the sockets will be
2994 writable since we're out of the select */
2997 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2998 if (bgp_peers
[i
].state
== Established
)
2999 bgp_stop(&bgp_peers
[i
]);
3003 /* remove plugins (so cleanup code gets run) */
3006 /* kill CLI children */
3007 signal(SIGTERM
, SIG_IGN
);
3012 void sighup_handler(int junk
)
3014 if (log_stream
&& log_stream
!= stderr
)
3023 void sigalrm_handler(int junk
)
3025 // Log current traffic stats
3027 snprintf(config
->bandwidth
, sizeof(config
->bandwidth
),
3028 "UDP-ETH:%1.0f/%1.0f ETH-UDP:%1.0f/%1.0f TOTAL:%0.1f IN:%u OUT:%u",
3029 (udp_rx
/ 1024.0 / 1024.0 * 8),
3030 (eth_tx
/ 1024.0 / 1024.0 * 8),
3031 (eth_rx
/ 1024.0 / 1024.0 * 8),
3032 (udp_tx
/ 1024.0 / 1024.0 * 8),
3033 ((udp_tx
+ udp_rx
+ eth_tx
+ eth_rx
) / 1024.0 / 1024.0 * 8),
3034 udp_rx_pkt
, eth_rx_pkt
);
3036 udp_tx
= udp_rx
= 0;
3037 udp_rx_pkt
= eth_rx_pkt
= 0;
3038 eth_tx
= eth_rx
= 0;
3040 if (config
->dump_speed
)
3041 printf("%s\n", config
->bandwidth
);
3043 // Update the internal time counter
3045 strftime(time_now_string
, 64, "%Y-%m-%d %H:%M:%S", localtime(&time_now
));
3050 struct param_timer p
= { time_now
};
3051 run_plugins(PLUGIN_TIMER
, &p
);
3056 void sigterm_handler(int junk
)
3058 log(1, 0, 0, 0, "Shutting down cleanly\n");
3059 if (config
->save_state
)
3065 void sigquit_handler(int junk
)
3069 log(1, 0, 0, 0, "Shutting down without saving sessions\n");
3070 for (i
= 1; i
< MAXSESSION
; i
++)
3072 if (session
[i
].opened
)
3073 sessionkill(i
, "L2TPNS Closing");
3075 for (i
= 1; i
< MAXTUNNEL
; i
++)
3077 if (tunnel
[i
].ip
|| tunnel
[i
].state
)
3078 tunnelshutdown(i
, "L2TPNS Closing");
3084 void sigchild_handler(int signal
)
3086 while (waitpid(-1, NULL
, WNOHANG
) > 0)
3096 char magic
[sizeof(DUMP_MAGIC
)-1];
3099 if (!config
->save_state
)
3105 if (stat(STATEFILE
, &sb
) < 0)
3111 if (sb
.st_mtime
< (time(NULL
) - 60))
3113 log(0, 0, 0, 0, "State file is too old to read, ignoring\n");
3118 f
= fopen(STATEFILE
, "r");
3123 log(0, 0, 0, 0, "Can't read state file: %s\n", strerror(errno
));
3127 if (fread(magic
, sizeof(magic
), 1, f
) != 1 || strncmp(magic
, DUMP_MAGIC
, sizeof(magic
)))
3129 log(0, 0, 0, 0, "Bad state file magic\n");
3133 log(1, 0, 0, 0, "Reading state information\n");
3134 if (fread(buf
, sizeof(buf
), 1, f
) != 1 || buf
[0] > MAXIPPOOL
|| buf
[1] != sizeof(ippoolt
))
3136 log(0, 0, 0, 0, "Error/mismatch reading ip pool header from state file\n");
3140 if (buf
[0] > ip_pool_size
)
3142 log(0, 0, 0, 0, "ip pool has shrunk! state = %d, current = %d\n", buf
[0], ip_pool_size
);
3146 log(2, 0, 0, 0, "Loading %u ip addresses\n", buf
[0]);
3147 for (i
= 0; i
< buf
[0]; i
++)
3149 if (fread(&itmp
, sizeof(itmp
), 1, f
) != 1)
3151 log(0, 0, 0, 0, "Error reading ip %d from state file: %s\n", i
, strerror(errno
));
3155 if (itmp
.address
!= ip_address_pool
[i
].address
)
3157 log(0, 0, 0, 0, "Mismatched ip %d from state file: pool may only be extended\n", i
);
3161 memcpy(&ip_address_pool
[i
], &itmp
, sizeof(itmp
));
3164 if (fread(buf
, sizeof(buf
), 1, f
) != 1 || buf
[0] != MAXTUNNEL
|| buf
[1] != sizeof(tunnelt
))
3166 log(0, 0, 0, 0, "Error/mismatch reading tunnel header from state file\n");
3170 log(2, 0, 0, 0, "Loading %u tunnels\n", MAXTUNNEL
);
3171 if (fread(tunnel
, sizeof(tunnelt
), MAXTUNNEL
, f
) != MAXTUNNEL
)
3173 log(0, 0, 0, 0, "Error reading tunnel data from state file\n");
3177 for (i
= 0; i
< MAXTUNNEL
; i
++)
3179 tunnel
[i
].controlc
= 0;
3180 tunnel
[i
].controls
= NULL
;
3181 tunnel
[i
].controle
= NULL
;
3182 if (*tunnel
[i
].hostname
)
3183 log(3, 0, 0, 0, "Created tunnel for %s\n", tunnel
[i
].hostname
);
3186 if (fread(buf
, sizeof(buf
), 1, f
) != 1 || buf
[0] != MAXSESSION
|| buf
[1] != sizeof(sessiont
))
3188 log(0, 0, 0, 0, "Error/mismatch reading session header from state file\n");
3192 log(2, 0, 0, 0, "Loading %u sessions\n", MAXSESSION
);
3193 if (fread(session
, sizeof(sessiont
), MAXSESSION
, f
) != MAXSESSION
)
3195 log(0, 0, 0, 0, "Error reading session data from state file\n");
3199 for (i
= 0; i
< MAXSESSION
; i
++)
3201 session
[i
].tbf_in
= 0;
3202 session
[i
].tbf_out
= 0;
3203 if (session
[i
].opened
)
3205 log(2, 0, i
, 0, "Loaded active session for user %s\n", session
[i
].user
);
3207 sessionsetup(session
[i
].tunnel
, i
);
3212 log(0, 0, 0, 0, "Loaded saved state information\n");
3220 if (!config
->save_state
)
3224 if (!(f
= fopen(STATEFILE
, "w")))
3227 log(1, 0, 0, 0, "Dumping state information\n");
3229 if (fwrite(DUMP_MAGIC
, sizeof(DUMP_MAGIC
)-1, 1, f
) != 1) break;
3231 log(2, 0, 0, 0, "Dumping %u ip addresses\n", ip_pool_size
);
3232 buf
[0] = ip_pool_size
;
3233 buf
[1] = sizeof(ippoolt
);
3234 if (fwrite(buf
, sizeof(buf
), 1, f
) != 1) break;
3235 if (fwrite(ip_address_pool
, sizeof(ippoolt
), ip_pool_size
, f
) != ip_pool_size
) break;
3237 log(2, 0, 0, 0, "Dumping %u tunnels\n", MAXTUNNEL
);
3239 buf
[1] = sizeof(tunnelt
);
3240 if (fwrite(buf
, sizeof(buf
), 1, f
) != 1) break;
3241 if (fwrite(tunnel
, sizeof(tunnelt
), MAXTUNNEL
, f
) != MAXTUNNEL
) break;
3243 log(2, 0, 0, 0, "Dumping %u sessions\n", MAXSESSION
);
3244 buf
[0] = MAXSESSION
;
3245 buf
[1] = sizeof(sessiont
);
3246 if (fwrite(buf
, sizeof(buf
), 1, f
) != 1) break;
3247 if (fwrite(session
, sizeof(sessiont
), MAXSESSION
, f
) != MAXSESSION
) break;
3249 if (fclose(f
) == 0) return; // OK
3252 log(0, 0, 0, 0, "Can't write state information: %s\n", strerror(errno
));
3256 void build_chap_response(char *challenge
, u8 id
, u16 challenge_length
, char **challenge_response
)
3259 *challenge_response
= NULL
;
3261 if (!*config
->l2tpsecret
)
3263 log(0, 0, 0, 0, "LNS requested CHAP authentication, but no l2tp secret is defined\n");
3267 log(4, 0, 0, 0, " Building challenge response for CHAP request\n");
3269 *challenge_response
= (char *)calloc(17, 1);
3272 MD5Update(&ctx
, &id
, 1);
3273 MD5Update(&ctx
, config
->l2tpsecret
, strlen(config
->l2tpsecret
));
3274 MD5Update(&ctx
, challenge
, challenge_length
);
3275 MD5Final(*challenge_response
, &ctx
);
3280 static int facility_value(char *name
)
3283 for (i
= 0; facilitynames
[i
].c_name
; i
++)
3285 if (strcmp(facilitynames
[i
].c_name
, name
) == 0)
3286 return facilitynames
[i
].c_val
;
3291 void update_config()
3303 if (*config
->log_filename
)
3305 if (strstr(config
->log_filename
, "syslog:") == config
->log_filename
)
3307 char *p
= config
->log_filename
+ 7;
3310 openlog("l2tpns", LOG_PID
, facility_value(p
));
3314 else if (strchr(config
->log_filename
, '/') == config
->log_filename
)
3316 if ((log_stream
= fopen((char *)(config
->log_filename
), "a")))
3318 fseek(log_stream
, 0, SEEK_END
);
3319 setbuf(log_stream
, NULL
);
3323 log_stream
= stderr
;
3324 setbuf(log_stream
, NULL
);
3330 log_stream
= stderr
;
3331 setbuf(log_stream
, NULL
);
3336 config
->numradiusservers
= 0;
3337 for (i
= 0; i
< MAXRADSERVER
; i
++)
3338 if (config
->radiusserver
[i
]) config
->numradiusservers
++;
3340 if (!config
->numradiusservers
)
3342 log(0, 0, 0, 0, "No RADIUS servers defined!\n");
3345 config
->num_radfds
= 2 << RADIUS_SHIFT
;
3348 for (i
= 0; i
< MAXPLUGINS
; i
++)
3350 if (strcmp(config
->plugins
[i
], config
->old_plugins
[i
]) == 0)
3352 if (*config
->plugins
[i
])
3355 add_plugin(config
->plugins
[i
]);
3357 else if (*config
->old_plugins
[i
])
3360 remove_plugin(config
->old_plugins
[i
]);
3363 memcpy(config
->old_plugins
, config
->plugins
, sizeof(config
->plugins
));
3364 if (!config
->cleanup_interval
) config
->cleanup_interval
= 10;
3365 if (!config
->multi_read_count
) config
->multi_read_count
= 10;
3366 if (!config
->cluster_address
) config
->cluster_address
= inet_addr(DEFAULT_MCAST_ADDR
);
3367 if (!*config
->cluster_interface
)
3368 strncpy(config
->cluster_interface
, DEFAULT_MCAST_INTERFACE
, sizeof(config
->cluster_interface
) - 1);
3370 if (!config
->cluster_hb_interval
)
3371 config
->cluster_hb_interval
= PING_INTERVAL
; // Heartbeat every 0.5 seconds.
3373 if (!config
->cluster_hb_timeout
)
3374 config
->cluster_hb_timeout
= HB_TIMEOUT
; // 10 missed heartbeat triggers an election.
3376 config
->reload_config
= 0;
3379 void read_config_file()
3383 if (!config
->config_file
) return;
3384 if (!(f
= fopen(config
->config_file
, "r"))) {
3385 fprintf(stderr
, "Can't open config file %s: %s\n", config
->config_file
, strerror(errno
));
3389 log(3, 0, 0, 0, "Reading config file %s\n", config
->config_file
);
3391 log(3, 0, 0, 0, "Done reading config file\n");
3396 int sessionsetup(tunnelidt t
, sessionidt s
)
3398 // A session now exists, set it up
3404 CSTAT(call_sessionsetup
);
3407 log(3, session
[s
].ip
, s
, t
, "Doing session setup for session\n");
3409 if (!session
[s
].ip
|| session
[s
].ip
== 0xFFFFFFFE)
3411 assign_ip_address(s
);
3413 log(3, 0, s
, t
, " No IP allocated. Assigned %s from pool\n",
3414 inet_toa(htonl(session
[s
].ip
)));
3416 log(0, 0, s
, t
, " No IP allocated. The IP address pool is FULL!\n");
3420 // Make sure this is right
3421 session
[s
].tunnel
= t
;
3423 // zap old sessions with same IP and/or username
3424 // Don't kill gardened sessions - doing so leads to a DoS
3425 // from someone who doesn't need to know the password
3428 user
= session
[s
].user
;
3429 for (i
= 1; i
<= config
->cluster_highest_sessionid
; i
++)
3431 if (i
== s
) continue;
3432 if (ip
== session
[i
].ip
) sessionkill(i
, "Duplicate IP address");
3433 if (!session
[s
].walled_garden
&& !session
[i
].walled_garden
&& strcasecmp(user
, session
[i
].user
) == 0)
3434 sessionkill(i
, "Duplicate session for users");
3438 // Add the route for this session.
3440 // Static IPs need to be routed. Anything else
3441 // is part of the IP address pool and is already routed,
3442 // it just needs to be added to the IP cache.
3443 if (session
[s
].ip_pool_index
== -1) // static ip
3444 routeset(s
, session
[s
].ip
, 0, 0, 1);
3446 cache_ipmap(session
[s
].ip
, s
);
3448 for (r
= 0; r
< MAXROUTE
&& session
[s
].route
[r
].ip
; r
++)
3449 routeset(s
, session
[s
].route
[r
].ip
, session
[s
].route
[r
].mask
, session
[s
].ip
, 1);
3451 if (!session
[s
].sid
) { // did this session just finish radius?
3452 log(3, session
[s
].ip
, s
, t
, "Sending initial IPCP to client\n");
3454 session
[s
].sid
= ++last_sid
;
3457 // Run the plugin's against this new session.
3459 struct param_new_session data
= { &tunnel
[t
], &session
[s
] };
3460 run_plugins(PLUGIN_NEW_SESSION
, &data
);
3463 // Force throttling on or off (Actually : refresh the current throttling status)
3464 // This has the advantage of cleaning up after another throttled user who may have left
3465 // firewall rules lying around
3466 throttle_session(s
, session
[s
].throttle
);
3468 session
[s
].last_packet
= time_now
;
3471 char *sessionip
, *tunnelip
;
3472 sessionip
= strdup(inet_toa(htonl(session
[s
].ip
)));
3473 tunnelip
= strdup(inet_toa(htonl(tunnel
[t
].ip
)));
3474 log(2, session
[s
].ip
, s
, t
, "Login by %s at %s from %s (%s)\n",
3475 session
[s
].user
, sessionip
, tunnelip
, tunnel
[t
].hostname
);
3476 if (sessionip
) free(sessionip
);
3477 if (tunnelip
) free(tunnelip
);
3480 cluster_send_session(s
); // Mark it as dirty, and needing to the flooded to the cluster.
3482 return 1; // RADIUS OK and IP allocated, done...
3486 // This session just got dropped on us by the master or something.
3487 // Make sure our tables up up to date...
3489 int load_session(sessionidt s
, sessiont
*new)
3494 if (new->ip_pool_index
>= MAXIPPOOL
||
3495 new->tunnel
>= MAXTUNNEL
) {
3496 log(0,0,s
,0, "Strange session update received!\n");
3497 // FIXME! What to do here?
3502 // Ok. All sanity checks passed. Now we're committed to
3503 // loading the new session.
3506 session
[s
].tunnel
= new->tunnel
; // For logging in cache_ipmap
3509 if (new->ip
!= session
[s
].ip
) // Changed ip. fix up hash tables.
3511 if (session
[s
].ip
) // If there's an old one, remove it.
3513 // Remove any routes if the IP has changed
3514 for (i
= 0; i
< MAXROUTE
&& session
[s
].route
[i
].ip
; i
++)
3516 routeset(s
, session
[s
].route
[i
].ip
, session
[s
].route
[i
].mask
, session
[s
].ip
, 0);
3517 session
[s
].route
[i
].ip
= 0;
3520 if (session
[s
].ip_pool_index
== -1) // static IP
3521 routeset(s
, session
[s
].ip
, 0, 0, 0);
3522 else // It's part of the IP pool, remove it manually.
3523 uncache_ipmap(session
[s
].ip
);
3526 if (new->ip
) { // If there's a new one, add it.
3527 if (new->ip_pool_index
== -1)
3528 routeset(s
, new->ip
, 0, 0, 1);
3530 cache_ipmap(new->ip
, s
);
3534 // Update routed networks
3535 for (i
= 0; i
< MAXROUTE
&& (session
[s
].route
[i
].ip
|| new->route
[i
].ip
); i
++)
3537 if (new->route
[i
].ip
== session
[s
].route
[i
].ip
&&
3538 new->route
[i
].mask
== session
[s
].route
[i
].mask
)
3541 if (session
[s
].route
[i
].ip
) // Remove the old one if it exists.
3542 routeset(s
, session
[s
].route
[i
].ip
, session
[s
].route
[i
].mask
, session
[s
].ip
, 0);
3544 if (new->route
[i
].ip
) // Add the new one if it exists.
3545 routeset(s
, new->route
[i
].ip
, new->route
[i
].mask
, new->ip
, 1);
3548 if (new->tunnel
&& s
> config
->cluster_highest_sessionid
) // Maintain this in the slave. It's used
3549 // for walking the sessions to forward byte counts to the master.
3550 config
->cluster_highest_sessionid
= s
;
3552 memcpy(&session
[s
], new, sizeof(session
[s
])); // Copy over..
3554 // Do fixups into address pool.
3555 if (new->ip_pool_index
!= -1)
3556 fix_address_pool(s
);
3562 void ringbuffer_dump(FILE *stream
)
3564 int i
= ringbuffer
->head
;
3566 while (i
!= ringbuffer
->tail
)
3568 if (*ringbuffer
->buffer
[i
].message
)
3569 fprintf(stream
, "%d-%s", ringbuffer
->buffer
[i
].level
, ringbuffer
->buffer
[i
].message
);
3570 if (++i
== ringbuffer
->tail
) break;
3571 if (i
== RINGBUFFER_SIZE
) i
= 0;
3580 loaded_plugins
= ll_init();
3581 // Initialize the plugins to nothing
3582 for (i
= 0; i
< MAX_PLUGIN_TYPES
; i
++)
3583 plugins
[i
] = ll_init();
3586 static void *open_plugin(char *plugin_name
, int load
)
3588 char path
[256] = "";
3590 snprintf(path
, 256, PLUGINDIR
"/%s.so", plugin_name
);
3591 log(2, 0, 0, 0, "%soading plugin from %s\n", load
? "L" : "Un-l", path
);
3592 return dlopen(path
, RTLD_NOW
);
3595 void add_plugin(char *plugin_name
)
3597 static struct pluginfuncs funcs
= {
3602 sessiontbysessionidt
,
3603 sessionidtbysessiont
,
3609 void *p
= open_plugin(plugin_name
, 1);
3610 int (*initfunc
)(struct pluginfuncs
*);
3615 log(1, 0, 0, 0, " Plugin load failed: %s\n", dlerror());
3619 if (ll_contains(loaded_plugins
, p
))
3626 int *v
= dlsym(p
, "__plugin_api_version");
3627 if (!v
|| *v
!= PLUGIN_API_VERSION
)
3629 log(1, 0, 0, 0, " Plugin load failed: API version mismatch: %s\n", dlerror());
3635 if ((initfunc
= dlsym(p
, "plugin_init")))
3637 if (!initfunc(&funcs
))
3639 log(1, 0, 0, 0, " Plugin load failed: plugin_init() returned FALSE: %s\n", dlerror());
3645 ll_push(loaded_plugins
, p
);
3647 for (i
= 0; i
< max_plugin_functions
; i
++)
3650 if (plugin_functions
[i
] && (x
= dlsym(p
, plugin_functions
[i
])))
3652 log(3, 0, 0, 0, " Supports function \"%s\"\n", plugin_functions
[i
]);
3653 ll_push(plugins
[i
], x
);
3657 log(2, 0, 0, 0, " Loaded plugin %s\n", plugin_name
);
3660 static void run_plugin_done(void *plugin
)
3662 int (*donefunc
)(void) = dlsym(plugin
, "plugin_done");
3668 void remove_plugin(char *plugin_name
)
3670 void *p
= open_plugin(plugin_name
, 0);
3676 for (i
= 0; i
< max_plugin_functions
; i
++)
3679 if (plugin_functions
[i
] && (x
= dlsym(p
, plugin_functions
[i
])))
3680 ll_delete(plugins
[i
], x
);
3683 if (ll_contains(loaded_plugins
, p
))
3685 ll_delete(loaded_plugins
, p
);
3690 log(2, 0, 0, 0, "Removed plugin %s\n", plugin_name
);
3693 int run_plugins(int plugin_type
, void *data
)
3695 int (*func
)(void *data
);
3696 if (!plugins
[plugin_type
] || plugin_type
> max_plugin_functions
) return 1;
3698 ll_reset(plugins
[plugin_type
]);
3699 while ((func
= ll_next(plugins
[plugin_type
])))
3703 if (rc
== PLUGIN_RET_STOP
) return 1;
3704 if (rc
== PLUGIN_RET_ERROR
) return 0;
3713 ll_reset(loaded_plugins
);
3714 while ((p
= ll_next(loaded_plugins
)))
3718 void processcontrol(u8
* buf
, int len
, struct sockaddr_in
*addr
)
3722 struct param_control param
= { buf
, len
, ntohl(addr
->sin_addr
.s_addr
), ntohs(addr
->sin_port
), NULL
, 0, 0 };
3725 if (log_stream
&& config
->debug
>= 4)
3727 log(4, ntohl(addr
->sin_addr
.s_addr
), 0, 0, "Received ");
3728 dump_packet(buf
, log_stream
);
3731 resp
= calloc(1400, 1);
3732 l
= new_packet(PKT_RESP_ERROR
, resp
);
3733 *(int *)(resp
+ 6) = *(int *)(buf
+ 6);
3735 param
.type
= ntohs(*(short *)(buf
+ 2));
3736 param
.id
= ntohl(*(int *)(buf
+ 6));
3737 param
.data_length
= ntohs(*(short *)(buf
+ 4)) - 10;
3738 param
.data
= (param
.data_length
> 0) ? (char *)(buf
+ 10) : NULL
;
3739 param
.response
= resp
;
3740 param
.response_length
= l
;
3742 run_plugins(PLUGIN_CONTROL
, ¶m
);
3744 if (param
.send_response
)
3746 send_packet(controlfd
, ntohl(addr
->sin_addr
.s_addr
), ntohs(addr
->sin_port
), param
.response
, param
.response_length
);
3747 log(4, ntohl(addr
->sin_addr
.s_addr
), 0, 0, "Sent Control packet response\n");
3755 * Go through all of the tunnels and do some cleanups
3761 log(1, 0, 0, 0, "Cleaning tunnels array\n");
3763 for (i
= 1; i
< MAXTUNNEL
; i
++)
3766 || !*tunnel
[i
].hostname
3767 || (tunnel
[i
].state
== TUNNELDIE
&& tunnel
[i
].die
>= time_now
))
3774 void tunnelclear(tunnelidt t
)
3777 memset(&tunnel
[t
], 0, sizeof(tunnel
[t
]));
3778 tunnel
[t
].state
= TUNNELFREE
;
3781 tunnelidt
new_tunnel()
3784 for (i
= 1; i
< MAXTUNNEL
; i
++)
3786 if (tunnel
[i
].state
== TUNNELFREE
)
3788 log(4, 0, 0, i
, "Assigning tunnel ID %d\n", i
);
3789 if (i
> config
->cluster_highest_tunnelid
)
3790 config
->cluster_highest_tunnelid
= i
;
3794 log(0, 0, 0, 0, "Can't find a free tunnel! There shouldn't be this many in use!\n");
3799 // We're becoming the master. Do any required setup..
3801 // This is principally telling all the plugins that we're
3802 // now a master, and telling them about all the sessions
3803 // that are active too..
3805 void become_master(void)
3808 run_plugins(PLUGIN_BECOME_MASTER
, NULL
);
3810 for (s
= 0; s
< config
->cluster_highest_sessionid
; ++s
) {
3811 if (!session
[s
].tunnel
) // Not an in-use session.
3814 run_plugins(PLUGIN_NEW_SESSION_MASTER
, &session
[s
]);
3820 int cmd_show_hist_idle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3826 if (CLI_HELP_REQUESTED
)
3827 return CLI_HELP_NO_ARGS
;
3830 for (i
= 0; i
< 64;++i
) buckets
[i
] = 0;
3832 for (s
= 0; s
< config
->cluster_highest_sessionid
; ++s
) {
3834 if (!session
[s
].tunnel
)
3837 idle
= time_now
- session
[s
].last_packet
;
3838 idle
/= 5 ; // In multiples of 5 seconds.
3848 for (i
= 0; i
< 63; ++i
) {
3849 cli_print(cli
, "%3d seconds : %7.2f%% (%6d)", i
* 5, (double) buckets
[i
] * 100.0 / count
, buckets
[i
]);
3851 cli_print(cli
, "lots of secs : %7.2f%% (%6d)", (double) buckets
[63] * 100.0 / count
, buckets
[i
]);
3852 cli_print(cli
, "%d total sessions open.", count
);
3856 int cmd_show_hist_open(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3862 if (CLI_HELP_REQUESTED
)
3863 return CLI_HELP_NO_ARGS
;
3866 for (i
= 0; i
< 64;++i
) buckets
[i
] = 0;
3868 for (s
= 0; s
< config
->cluster_highest_sessionid
; ++s
) {
3870 if (!session
[s
].tunnel
)
3873 d
= time_now
- session
[s
].opened
;
3876 while (d
> 1 && open
< 32) {
3885 for (i
= 0; i
< 30; ++i
) {
3886 cli_print(cli
, " < %8d seconds : %7.2f%% (%6d)", s
, (double) buckets
[i
] * 100.0 / count
, buckets
[i
]);
3889 cli_print(cli
, "%d total sessions open.", count
);