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.13 2004/07/11 07:57:35 bodea Exp $";
13 #include <linux/if_tun.h>
18 #include <net/route.h>
21 #include <netinet/in.h>
26 #include <sys/ioctl.h>
27 #include <sys/socket.h>
30 #include <sys/resource.h>
38 #include <sys/sysinfo.h>
46 #include "constants.h"
52 struct configt
*config
= NULL
; // all configuration
53 int tunfd
= -1; // tun interface file handle. (network device)
54 int udpfd
= -1; // UDP file handle
55 int controlfd
= -1; // Control signal handle
56 int snoopfd
= -1; // UDP file handle for sending out intercept data
57 int *radfds
= NULL
; // RADIUS requests file handles
58 int ifrfd
= -1; // File descriptor for routing, etc
59 time_t basetime
= 0; // base clock
60 char hostname
[1000] = ""; // us.
61 int tunidx
; // ifr_ifindex of tun device
62 u32 sessionid
= 0; // session id for radius accounting
63 int syslog_log
= 0; // are we logging to syslog
64 FILE *log_stream
= NULL
; // file handle for direct logging (i.e. direct into file, not via syslog).
65 extern int cluster_sockfd
; // Intra-cluster communications socket.
66 u32 last_sid
= 0; // Last used PPP SID. Can I kill this?? -- mo
67 int clifd
= 0; // Socket listening for CLI connections.
69 struct cli_session_actions
*cli_session_actions
= NULL
; // Pending session changes requested by CLI
70 struct cli_tunnel_actions
*cli_tunnel_actions
= NULL
; // Pending tunnel changes required by CLI
72 static void *ip_hash
[256]; // Mapping from IP address to session structures.
74 u32 udp_tx
= 0, udp_rx
= 0, udp_rx_pkt
= 0; // Global traffic counters.
75 u32 eth_tx
= 0, eth_rx
= 0, eth_rx_pkt
= 0;
76 u32 ip_pool_size
= 1; // Size of the pool of addresses used for dynamic address allocation.
77 time_t time_now
= 0; // Current time in seconds since epoch.
78 char time_now_string
[64] = {0}; // Current time as a string.
79 char main_quit
= 0; // True if we're in the process of exiting.
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
; // Array of tunnel structures.
147 sessiont
*session
= NULL
; // Array of session structures.
148 sessioncountt
*sess_count
= NULL
; // Array of partial per-session traffic counters.
149 radiust
*radius
= NULL
; // Array of radius structures.
150 ippoolt
*ip_address_pool
= NULL
; // Array of dynamic IP addresses.
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 process startup)
177 return (t
.tv_sec
- basetime
) * 10 + t
.tv_usec
/ 100000 + 1;
180 // work out a retry time based on try number
181 // This is a straight bounded exponential backoff.
182 // Maximum re-try time is 32 seconds. (2^5).
183 clockt
backoff(u8
try)
185 if (try > 5) try = 5; // max backoff
186 return now() + 10 * (1 << try);
191 // Log a debug message.
193 void _log(int level
, ipt address
, sessionidt s
, tunnelidt t
, const char *format
, ...)
195 static char message
[65536] = {0};
196 static char message2
[65536] = {0};
202 if (++ringbuffer
->tail
>= RINGBUFFER_SIZE
)
203 ringbuffer
->tail
= 0;
204 if (ringbuffer
->tail
== ringbuffer
->head
)
205 if (++ringbuffer
->head
>= RINGBUFFER_SIZE
)
206 ringbuffer
->head
= 0;
208 ringbuffer
->buffer
[ringbuffer
->tail
].level
= level
;
209 ringbuffer
->buffer
[ringbuffer
->tail
].address
= address
;
210 ringbuffer
->buffer
[ringbuffer
->tail
].session
= s
;
211 ringbuffer
->buffer
[ringbuffer
->tail
].tunnel
= t
;
212 va_start(ap
, format
);
213 vsnprintf(ringbuffer
->buffer
[ringbuffer
->tail
].message
, 4095, format
, ap
);
218 if (config
->debug
< level
) return;
220 va_start(ap
, format
);
223 vsnprintf(message2
, 65535, format
, ap
);
224 snprintf(message
, 65535, "%s %02d/%02d %s", time_now_string
, t
, s
, message2
);
225 fprintf(log_stream
, "%s", message
);
229 vsnprintf(message2
, 65535, format
, ap
);
230 snprintf(message
, 65535, "%02d/%02d %s", t
, s
, message2
);
231 syslog(level
+ 2, message
); // We don't need LOG_EMERG or LOG_ALERT
236 void _log_hex(int level
, ipt address
, sessionidt s
, tunnelidt t
, const char *title
, const char *data
, int maxsize
)
239 const u8
*d
= (const u8
*)data
;
241 if (config
->debug
< level
) return;
243 // No support for log_hex to syslog
246 log(level
, address
, s
, t
, "%s (%d bytes):\n", title
, maxsize
);
247 setvbuf(log_stream
, NULL
, _IOFBF
, 16384);
249 for (i
= 0; i
< maxsize
; )
251 fprintf(log_stream
, "%4X: ", i
);
252 for (j
= i
; j
< maxsize
&& j
< (i
+ 16); j
++)
254 fprintf(log_stream
, "%02X ", d
[j
]);
256 fputs(": ", log_stream
);
259 for (; j
< i
+ 16; j
++)
261 fputs(" ", log_stream
);
263 fputs(": ", log_stream
);
266 fputs(" ", log_stream
);
267 for (j
= i
; j
< maxsize
&& j
< (i
+ 16); j
++)
269 if (d
[j
] >= 0x20 && d
[j
] < 0x7f && d
[j
] != 0x20)
270 fputc(d
[j
], log_stream
);
272 fputc('.', log_stream
);
275 fputs(" ", log_stream
);
279 fputs("\n", log_stream
);
283 setbuf(log_stream
, NULL
);
290 // This adds it to the routing table, advertises it
291 // via iBGP if enabled, and stuffs it into the
292 // 'sessionbyip' cache.
294 // 'ip' and 'mask' must be in _host_ order.
296 void routeset(sessionidt s
, ipt ip
, ipt mask
, ipt gw
, u8 add
)
301 if (!mask
) mask
= 0xffffffff;
303 ip
&= mask
; // Force the ip to be the first one in the route.
305 memset(&r
, 0, sizeof(r
));
306 r
.rt_dev
= config
->tundevice
;
307 r
.rt_dst
.sa_family
= AF_INET
;
308 *(u32
*) & (((struct sockaddr_in
*) &r
.rt_dst
)->sin_addr
.s_addr
) = htonl(ip
);
309 r
.rt_gateway
.sa_family
= AF_INET
;
310 *(u32
*) & (((struct sockaddr_in
*) &r
.rt_gateway
)->sin_addr
.s_addr
) = htonl(gw
);
311 r
.rt_genmask
.sa_family
= AF_INET
;
312 *(u32
*) & (((struct sockaddr_in
*) &r
.rt_genmask
)->sin_addr
.s_addr
) = htonl(mask
);
313 r
.rt_flags
= (RTF_UP
| RTF_STATIC
);
315 r
.rt_flags
|= RTF_GATEWAY
;
316 else if (mask
== 0xffffffff)
317 r
.rt_flags
|= RTF_HOST
;
319 log(1, ip
, 0, 0, "Route %s %u.%u.%u.%u/%u.%u.%u.%u %u.%u.%u.%u\n",
321 ip
>> 24, ip
>> 16 & 0xff, ip
>> 8 & 0xff, ip
& 0xff,
322 mask
>> 24, mask
>> 16 & 0xff, mask
>> 8 & 0xff, mask
& 0xff,
323 gw
>> 24, gw
>> 16 & 0xff, gw
>> 8 & 0xff, gw
& 0xff);
325 if (ioctl(ifrfd
, add
? SIOCADDRT
: SIOCDELRT
, (void *) &r
) < 0)
326 log(0, 0, 0, 0, "routeset() error in ioctl: %s\n", strerror(errno
));
330 bgp_add_route(htonl(ip
), htonl(mask
));
332 bgp_del_route(htonl(ip
), htonl(mask
));
335 // Add/Remove the IPs to the 'sessionbyip' cache.
336 // Note that we add the zero address in the case of
337 // a network route. Roll on CIDR.
339 // Note that 's == 0' implies this is the address pool.
340 // We still cache it here, because it will pre-fill
341 // the malloc'ed tree.
345 if (!add
) // Are we deleting a route?
346 s
= 0; // Caching the session as '0' is the same as uncaching.
348 for (i
= ip
; (i
&mask
) == (ip
&mask
) ; ++i
)
354 // Set up TUN interface
358 struct sockaddr_in sin
= {0};
359 memset(&ifr
, 0, sizeof(ifr
));
360 ifr
.ifr_flags
= IFF_TUN
;
362 tunfd
= open(TUNDEVICE
, O_RDWR
);
365 log(0, 0, 0, 0, "Can't open %s: %s\n", TUNDEVICE
, strerror(errno
));
369 int flags
= fcntl(tunfd
, F_GETFL
, 0);
370 fcntl(tunfd
, F_SETFL
, flags
| O_NONBLOCK
);
372 if (ioctl(tunfd
, TUNSETIFF
, (void *) &ifr
) < 0)
374 log(0, 0, 0, 0, "Can't set tun interface: %s\n", strerror(errno
));
377 assert(strlen(ifr
.ifr_name
) < sizeof(config
->tundevice
));
378 strncpy(config
->tundevice
, ifr
.ifr_name
, sizeof(config
->tundevice
) - 1);
379 ifrfd
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
381 sin
.sin_family
= AF_INET
;
382 sin
.sin_addr
.s_addr
= config
->bind_address
? config
->bind_address
: 0x01010101; // 1.1.1.1
383 memcpy(&ifr
.ifr_addr
, &sin
, sizeof(struct sockaddr
));
385 if (ioctl(ifrfd
, SIOCSIFADDR
, (void *) &ifr
) < 0)
387 log(0, 0, 0, 0, "Error setting tun address: %s\n", strerror(errno
));
390 /* Bump up the qlen to deal with bursts from the network */
392 if (ioctl(ifrfd
, SIOCSIFTXQLEN
, (void *) &ifr
) < 0)
394 log(0, 0, 0, 0, "Error setting tun queue length: %s\n", strerror(errno
));
397 ifr
.ifr_flags
= IFF_UP
;
398 if (ioctl(ifrfd
, SIOCSIFFLAGS
, (void *) &ifr
) < 0)
400 log(0, 0, 0, 0, "Error setting tun flags: %s\n", strerror(errno
));
403 if (ioctl(ifrfd
, SIOCGIFINDEX
, (void *) &ifr
) < 0)
405 log(0, 0, 0, 0, "Error setting tun ifindex: %s\n", strerror(errno
));
408 tunidx
= ifr
.ifr_ifindex
;
415 struct sockaddr_in addr
;
418 memset(&addr
, 0, sizeof(addr
));
419 addr
.sin_family
= AF_INET
;
420 addr
.sin_port
= htons(L2TPPORT
);
421 addr
.sin_addr
.s_addr
= config
->bind_address
;
422 udpfd
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
423 setsockopt(udpfd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
425 int flags
= fcntl(udpfd
, F_GETFL
, 0);
426 fcntl(udpfd
, F_SETFL
, flags
| O_NONBLOCK
);
428 if (bind(udpfd
, (void *) &addr
, sizeof(addr
)) < 0)
430 log(0, 0, 0, 0, "Error in UDP bind: %s\n", strerror(errno
));
433 snoopfd
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
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 // actually send a control message for a specific tunnel
629 void tunnelsend(u8
* buf
, u16 l
, tunnelidt t
)
631 struct sockaddr_in addr
;
633 CSTAT(call_tunnelsend
);
637 static int backtrace_count
= 0;
638 log(0, 0, 0, t
, "tunnelsend called with 0 as tunnel id\n");
639 STAT(tunnel_tx_errors
);
640 log_backtrace(backtrace_count
, 5)
646 static int backtrace_count
= 0;
647 log(1, 0, 0, t
, "Error sending data out tunnel: no remote endpoint (tunnel not set up)\n");
648 log_backtrace(backtrace_count
, 5)
649 STAT(tunnel_tx_errors
);
652 memset(&addr
, 0, sizeof(addr
));
653 addr
.sin_family
= AF_INET
;
654 *(u32
*) & addr
.sin_addr
= htonl(tunnel
[t
].ip
);
655 addr
.sin_port
= htons(tunnel
[t
].port
);
657 // sequence expected, if sequence in message
658 if (*buf
& 0x08) *(u16
*) (buf
+ ((*buf
& 0x40) ? 10 : 8)) = htons(tunnel
[t
].nr
);
660 // If this is a control message, deal with retries
663 tunnel
[t
].last
= time_now
; // control message sent
664 tunnel
[t
].retry
= backoff(tunnel
[t
].try); // when to resend
665 if (tunnel
[t
].try > 1)
667 STAT(tunnel_retries
);
668 log(3, tunnel
[t
].ip
, 0, t
, "Control message resend try %d\n", tunnel
[t
].try);
672 if (sendto(udpfd
, buf
, l
, 0, (void *) &addr
, sizeof(addr
)) < 0)
674 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",
675 strerror(errno
), udpfd
, buf
, l
, inet_ntoa(addr
.sin_addr
));
676 STAT(tunnel_tx_errors
);
680 log_hex(5, "Send Tunnel Data", buf
, l
);
681 STAT(tunnel_tx_packets
);
682 INC_STAT(tunnel_tx_bytes
, l
);
686 // Tiny helper function to write data to
689 int tun_write(u8
* data
, int size
)
691 return write(tunfd
, data
, size
);
694 // process outgoing (to tunnel) IP
696 void processipout(u8
* buf
, int len
)
703 char * data
= buf
; // Keep a copy of the originals.
708 CSTAT(call_processipout
);
710 if (len
< MIN_IP_SIZE
)
712 log(1, 0, 0, 0, "Short IP, %d bytes\n", len
);
713 STAT(tunnel_tx_errors
);
718 log(1, 0, 0, 0, "Oversize IP packet %d bytes\n", len
);
719 STAT(tunnel_tx_errors
);
723 // Skip the tun header
727 // Got an IP header now
728 if (*(u8
*)(buf
) >> 4 != 4)
730 log(1, 0, 0, 0, "IP: Don't understand anything except IPv4\n");
734 ip
= *(u32
*)(buf
+ 16);
735 if (!(s
= sessionbyip(ip
)))
737 // Is this a packet for a session that doesn't exist?
738 static int rate
= 0; // Number of ICMP packets we've sent this second.
739 static int last
= 0; // Last time we reset the ICMP packet counter 'rate'.
741 if (last
!= time_now
) {
746 if (rate
++ < config
->icmp_rate
) // Only send a max of icmp_rate per second.
748 log(4, 0, 0, 0, "IP: Sending ICMP host unreachable to %s\n", inet_toa(*(u32
*)(buf
+ 12)));
749 host_unreachable(*(u32
*)(buf
+ 12), *(u16
*)(buf
+ 4), ip
, buf
, (len
< 64) ? 64 : len
);
753 t
= session
[s
].tunnel
;
758 // Are we throttling this session?
759 if (config
->cluster_iam_master
)
760 tbf_queue_packet(sp
->tbf_out
, data
, size
);
762 master_throttle_packet(sp
->tbf_out
, data
, size
);
765 else if (sp
->walled_garden
&& !config
->cluster_iam_master
)
767 // We are walled-gardening this
768 master_garden_packet(s
, data
, size
);
772 // Snooping this session, send it to intercept box
773 if (sp
->snoop_ip
&& sp
->snoop_port
)
774 snoop_send_packet(buf
, len
, sp
->snoop_ip
, sp
->snoop_port
);
776 log(5, session
[s
].ip
, s
, t
, "Ethernet -> Tunnel (%d bytes)\n", len
);
778 // Add on L2TP header
780 u8
*p
= makeppp(b
, sizeof(b
), buf
, len
, t
, s
, PPPIP
);
782 log(3, session
[s
].ip
, s
, t
, "failed to send packet in processipout.\n");
785 tunnelsend(b
, len
+ (p
-b
), t
); // send it...
788 sp
->cout
+= len
; // byte count
789 sp
->total_cout
+= len
; // byte count
792 sess_count
[s
].cout
+= len
; // To send to master..
796 // Helper routine for the TBF filters.
797 // Used to send queued data in to the user!
799 void send_ipout(sessionidt s
, u8
*buf
, int len
)
807 if (len
< 0 || len
> MAXETHER
) {
808 log(1,0,0,0, "Odd size IP packet: %d bytes\n", len
);
812 // Skip the tun header
816 ip
= *(u32
*)(buf
+ 16);
820 t
= session
[s
].tunnel
;
823 log(5, session
[s
].ip
, s
, t
, "Ethernet -> Tunnel (%d bytes)\n", len
);
825 // Snooping this session.
826 if (sp
->snoop_ip
&& sp
->snoop_port
)
827 snoop_send_packet(buf
, len
, sp
->snoop_ip
, sp
->snoop_port
);
829 // Add on L2TP header
831 u8
*p
= makeppp(b
, sizeof(b
), buf
, len
, t
, s
, PPPIP
);
833 log(3, session
[s
].ip
, s
, t
, "failed to send packet in send_ipout.\n");
836 tunnelsend(b
, len
+ (p
-b
), t
); // send it...
838 sp
->cout
+= len
; // byte count
839 sp
->total_cout
+= len
; // byte count
842 sess_count
[s
].cout
+= len
; // To send to master..
845 // add an AVP (16 bit)
846 void control16(controlt
* c
, u16 avp
, u16 val
, u8 m
)
848 u16 l
= (m
? 0x8008 : 0x0008);
849 *(u16
*) (c
->buf
+ c
->length
+ 0) = htons(l
);
850 *(u16
*) (c
->buf
+ c
->length
+ 2) = htons(0);
851 *(u16
*) (c
->buf
+ c
->length
+ 4) = htons(avp
);
852 *(u16
*) (c
->buf
+ c
->length
+ 6) = htons(val
);
856 // add an AVP (32 bit)
857 void control32(controlt
* c
, u16 avp
, u32 val
, u8 m
)
859 u16 l
= (m
? 0x800A : 0x000A);
860 *(u16
*) (c
->buf
+ c
->length
+ 0) = htons(l
);
861 *(u16
*) (c
->buf
+ c
->length
+ 2) = htons(0);
862 *(u16
*) (c
->buf
+ c
->length
+ 4) = htons(avp
);
863 *(u32
*) (c
->buf
+ c
->length
+ 6) = htonl(val
);
867 // add an AVP (32 bit)
868 void controls(controlt
* c
, u16 avp
, char *val
, u8 m
)
870 u16 l
= ((m
? 0x8000 : 0) + strlen(val
) + 6);
871 *(u16
*) (c
->buf
+ c
->length
+ 0) = htons(l
);
872 *(u16
*) (c
->buf
+ c
->length
+ 2) = htons(0);
873 *(u16
*) (c
->buf
+ c
->length
+ 4) = htons(avp
);
874 memcpy(c
->buf
+ c
->length
+ 6, val
, strlen(val
));
875 c
->length
+= 6 + strlen(val
);
879 void controlb(controlt
* c
, u16 avp
, char *val
, unsigned int len
, u8 m
)
881 u16 l
= ((m
? 0x8000 : 0) + len
+ 6);
882 *(u16
*) (c
->buf
+ c
->length
+ 0) = htons(l
);
883 *(u16
*) (c
->buf
+ c
->length
+ 2) = htons(0);
884 *(u16
*) (c
->buf
+ c
->length
+ 4) = htons(avp
);
885 memcpy(c
->buf
+ c
->length
+ 6, val
, len
);
886 c
->length
+= 6 + len
;
889 // new control connection
890 controlt
*controlnew(u16 mtype
)
894 c
= malloc(sizeof(controlt
));
898 controlfree
= c
->next
;
902 *(u16
*) (c
->buf
+ 0) = htons(0xC802); // flags/ver
904 control16(c
, 0, mtype
, 1);
908 // send zero block if nothing is waiting
910 void controlnull(tunnelidt t
)
913 if (tunnel
[t
].controlc
) // Messages queued; They will carry the ack.
916 *(u16
*) (buf
+ 0) = htons(0xC802); // flags/ver
917 *(u16
*) (buf
+ 2) = htons(12); // length
918 *(u16
*) (buf
+ 4) = htons(tunnel
[t
].far
); // tunnel
919 *(u16
*) (buf
+ 6) = htons(0); // session
920 *(u16
*) (buf
+ 8) = htons(tunnel
[t
].ns
); // sequence
921 *(u16
*) (buf
+ 10) = htons(tunnel
[t
].nr
); // sequence
922 tunnelsend(buf
, 12, t
);
925 // add a control message to a tunnel, and send if within window
926 void controladd(controlt
* c
, tunnelidt t
, sessionidt s
)
928 *(u16
*) (c
->buf
+ 2) = htons(c
->length
); // length
929 *(u16
*) (c
->buf
+ 4) = htons(tunnel
[t
].far
); // tunnel
930 *(u16
*) (c
->buf
+ 6) = htons(s
? session
[s
].far
: 0); // session
931 *(u16
*) (c
->buf
+ 8) = htons(tunnel
[t
].ns
); // sequence
932 tunnel
[t
].ns
++; // advance sequence
933 // link in message in to queue
934 if (tunnel
[t
].controlc
)
935 tunnel
[t
].controle
->next
= c
;
937 tunnel
[t
].controls
= c
;
938 tunnel
[t
].controle
= c
;
939 tunnel
[t
].controlc
++;
940 // send now if space in window
941 if (tunnel
[t
].controlc
<= tunnel
[t
].window
)
943 tunnel
[t
].try = 0; // first send
944 tunnelsend(c
->buf
, c
->length
, t
);
949 // Throttle or Unthrottle a session
951 // Throttle the data folling through a session
952 // to be no more than 'throttle' kbit/sec each way.
954 int throttle_session(sessionidt s
, int throttle
)
956 if (!session
[s
].tunnel
)
957 return 0; // No-one home.
959 if (!*session
[s
].user
)
960 return 0; // User not logged in
963 if (session
[s
].tbf_in
|| session
[s
].tbf_out
) {
964 if (throttle
== session
[s
].throttle
)
967 // Currently throttled but the rate is changing.
969 free_tbf(session
[s
].tbf_in
);
970 free_tbf(session
[s
].tbf_out
);
973 session
[s
].tbf_in
= new_tbf(s
, throttle
*1024/4, throttle
*1024/8, send_ipin
);
974 session
[s
].tbf_out
= new_tbf(s
, throttle
*1024/4, throttle
*1024/8, send_ipout
);
976 if (throttle
!= session
[s
].throttle
) { // Changed. Flood to slaves.
977 session
[s
].throttle
= throttle
;
978 cluster_send_session(s
);
984 // else Unthrottling.
986 if (!session
[s
].tbf_in
&& !session
[s
].tbf_out
&& !session
[s
].throttle
)
989 free_tbf(session
[s
].tbf_in
);
990 session
[s
].tbf_in
= 0;
992 free_tbf(session
[s
].tbf_out
);
993 session
[s
].tbf_out
= 0;
995 if (throttle
!= session
[s
].throttle
) { // Changed. Flood to slaves.
996 session
[s
].throttle
= throttle
;
997 cluster_send_session(s
);
1003 // start tidy shutdown of session
1004 void sessionshutdown(sessionidt s
, char *reason
)
1006 int dead
= session
[s
].die
;
1007 int walled_garden
= session
[s
].walled_garden
;
1010 CSTAT(call_sessionshutdown
);
1012 if (!session
[s
].tunnel
)
1014 log(3, session
[s
].ip
, s
, session
[s
].tunnel
, "Called sessionshutdown on a session with no tunnel.\n");
1015 return; // not a live session
1019 log(2, 0, s
, session
[s
].tunnel
, "Shutting down session %d: %s\n", s
, reason
);
1021 session
[s
].die
= now() + 150; // Clean up in 15 seconds
1024 struct param_kill_session data
= { &tunnel
[session
[s
].tunnel
], &session
[s
] };
1025 run_plugins(PLUGIN_KILL_SESSION
, &data
);
1028 // RADIUS Stop message
1029 if (session
[s
].opened
&& !walled_garden
&& !dead
) {
1030 u16 r
= session
[s
].radius
;
1033 if (!(r
= radiusnew(s
)))
1035 log(1, 0, s
, session
[s
].tunnel
, "No free RADIUS sessions for Stop message\n");
1036 STAT(radius_overflow
);
1041 for (n
= 0; n
< 15; n
++)
1042 radius
[r
].auth
[n
] = rand();
1045 if (r
&& radius
[r
].state
!= RADIUSSTOP
)
1046 radiussend(r
, RADIUSSTOP
); // stop, if not already trying
1050 { // IP allocated, clear and unroute
1052 for (r
= 0; r
< MAXROUTE
&& session
[s
].route
[r
].ip
; r
++)
1054 routeset(s
, session
[s
].route
[r
].ip
, session
[s
].route
[r
].mask
, session
[s
].ip
, 0);
1055 session
[s
].route
[r
].ip
= 0;
1058 if (session
[s
].ip_pool_index
== -1) // static ip
1060 routeset(s
, session
[s
].ip
, 0, 0, 0); // Delete route.
1066 if (session
[s
].throttle
) // Unthrottle if throttled.
1067 throttle_session(s
, 0);
1070 controlt
*c
= controlnew(14); // sending CDN
1071 control16(c
, 1, 3, 1); // result code (admin reasons - TBA make error, general error, add message
1072 control16(c
, 14, s
, 1); // assigned session (our end)
1073 controladd(c
, session
[s
].tunnel
, s
); // send the message
1075 cluster_send_session(s
);
1078 void sendipcp(tunnelidt t
, sessionidt s
)
1081 u16 r
= session
[s
].radius
;
1084 CSTAT(call_sendipcp
);
1089 if (radius
[r
].state
!= RADIUSIPCP
)
1091 radius
[r
].state
= RADIUSIPCP
;
1095 radius
[r
].retry
= backoff(radius
[r
].try++);
1096 if (radius
[r
].try > 10)
1098 radiusclear(r
, s
); // Clear radius session.
1099 sessionshutdown(s
, "No reply on IPCP");
1103 q
= makeppp(buf
,sizeof(buf
), 0, 0, t
, s
, PPPIPCP
);
1105 log(3, session
[s
].ip
, s
, t
, "failed to send packet in sendipcp.\n");
1110 q
[1] = r
<< RADIUS_SHIFT
; // ID, dont care, we only send one type of request
1111 *(u16
*) (q
+ 2) = htons(10);
1114 *(u32
*) (q
+ 6) = config
->bind_address
; // send my IP
1115 tunnelsend(buf
, 10 + (q
- buf
), t
); // send it
1116 session
[s
].flags
&= ~SF_IPCP_ACKED
; // Clear flag.
1119 // kill a session now
1120 void sessionkill(sessionidt s
, char *reason
)
1123 CSTAT(call_sessionkill
);
1125 session
[s
].die
= now();
1126 sessionshutdown(s
, reason
); // close radius/routes, etc.
1127 if (session
[s
].radius
)
1128 radiusclear(session
[s
].radius
, s
); // cant send clean accounting data, session is killed
1130 log(2, 0, s
, session
[s
].tunnel
, "Kill session %d (%s): %s\n", s
, session
[s
].user
, reason
);
1132 throttle_session(s
, 0); // Force session to be un-throttle. Free'ing TBF structures.
1134 memset(&session
[s
], 0, sizeof(session
[s
]));
1135 session
[s
].tunnel
= T_FREE
; // Mark it as free.
1136 session
[s
].next
= sessionfree
;
1138 cli_session_actions
[s
].action
= 0;
1139 cluster_send_session(s
);
1142 // kill a tunnel now
1143 void tunnelkill(tunnelidt t
, char *reason
)
1148 CSTAT(call_tunnelkill
);
1150 tunnel
[t
].state
= TUNNELDIE
;
1152 // free control messages
1153 while ((c
= tunnel
[t
].controls
))
1155 controlt
* n
= c
->next
;
1156 tunnel
[t
].controls
= n
;
1157 tunnel
[t
].controlc
--;
1158 c
->next
= controlfree
;
1162 for (s
= 1; s
< MAXSESSION
; s
++)
1163 if (session
[s
].tunnel
== t
)
1164 sessionkill(s
, reason
);
1168 log(1, 0, 0, t
, "Kill tunnel %d: %s\n", t
, reason
);
1169 cli_tunnel_actions
[s
].action
= 0;
1170 cluster_send_tunnel(t
);
1173 // shut down a tunnel cleanly
1174 void tunnelshutdown(tunnelidt t
, char *reason
)
1178 CSTAT(call_tunnelshutdown
);
1180 if (!tunnel
[t
].last
|| !tunnel
[t
].far
|| tunnel
[t
].state
== TUNNELFREE
)
1182 // never set up, can immediately kill
1183 tunnelkill(t
, reason
);
1186 log(1, 0, 0, t
, "Shutting down tunnel %d (%s)\n", t
, reason
);
1189 for (s
= 1; s
< MAXSESSION
; s
++)
1190 if (session
[s
].tunnel
== t
)
1191 sessionkill(s
, reason
);
1193 tunnel
[t
].state
= TUNNELDIE
;
1194 tunnel
[t
].die
= now() + 700; // Clean up in 70 seconds
1195 cluster_send_tunnel(t
);
1196 // TBA - should we wait for sessions to stop?
1198 controlt
*c
= controlnew(4); // sending StopCCN
1199 control16(c
, 1, 1, 1); // result code (admin reasons - TBA make error, general error, add message
1200 control16(c
, 9, t
, 1); // assigned tunnel (our end)
1201 controladd(c
, t
, 0); // send the message
1205 // read and process packet on tunnel (UDP)
1206 void processudp(u8
* buf
, int len
, struct sockaddr_in
*addr
)
1208 char *chapresponse
= NULL
;
1209 u16 l
= len
, t
= 0, s
= 0, ns
= 0, nr
= 0;
1213 CSTAT(call_processudp
);
1217 log_hex(5, "UDP Data", buf
, len
);
1218 STAT(tunnel_rx_packets
);
1219 INC_STAT(tunnel_rx_bytes
, len
);
1222 log(1, ntohl(addr
->sin_addr
.s_addr
), 0, 0, "Short UDP, %d bytes\n", len
);
1223 STAT(tunnel_rx_errors
);
1226 if ((buf
[1] & 0x0F) != 2)
1228 log(1, ntohl(addr
->sin_addr
.s_addr
), 0, 0, "Bad L2TP ver %d\n", (buf
[1] & 0x0F) != 2);
1229 STAT(tunnel_rx_errors
);
1234 l
= ntohs(*(u16
*) p
);
1237 t
= ntohs(*(u16
*) p
);
1239 s
= ntohs(*(u16
*) p
);
1241 if (s
>= MAXSESSION
)
1243 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Received UDP packet with invalid session ID\n");
1244 STAT(tunnel_rx_errors
);
1249 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Received UDP packet with invalid tunnel ID\n");
1250 STAT(tunnel_rx_errors
);
1255 ns
= ntohs(*(u16
*) p
);
1257 nr
= ntohs(*(u16
*) p
);
1262 u16 o
= ntohs(*(u16
*) p
);
1267 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Bad length %d>%d\n", (p
- buf
), l
);
1268 STAT(tunnel_rx_errors
);
1274 u16 message
= 0xFFFF; // message type
1276 u8 mandatorymessage
= 0;
1277 u8 chap
= 0; // if CHAP being used
1278 u16 asession
= 0; // assigned session
1279 u32 amagic
= 0; // magic number
1280 u8 aflags
= 0; // flags from last LCF
1281 u16 version
= 0x0100; // protocol version (we handle 0.0 as well and send that back just in case)
1282 int requestchap
= 0; // do we request PAP instead of original CHAP request?
1283 char called
[MAXTEL
] = ""; // called number
1284 char calling
[MAXTEL
] = ""; // calling number
1286 if (!config
->cluster_iam_master
) {
1287 master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
);
1291 if ((*buf
& 0xCA) != 0xC8)
1293 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Bad control header %02X\n", *buf
);
1294 STAT(tunnel_rx_errors
);
1297 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",
1298 l
, tunnel
[t
].controlc
, tunnel
[t
].ns
, tunnel
[t
].nr
, ns
, nr
);
1299 // if no tunnel specified, assign one
1305 // Is this a duplicate of the first packet? (SCCRQ)
1307 for ( i
= 1; i
<= config
->cluster_highest_tunnelid
; ++i
) {
1308 if (tunnel
[i
].state
!= TUNNELOPENING
||
1309 tunnel
[i
].ip
!= ntohl(*(ipt
*) & addr
->sin_addr
) ||
1310 tunnel
[i
].port
!= ntohs(addr
->sin_port
) )
1318 if (!(t
= new_tunnel()))
1320 log(1, ntohl(addr
->sin_addr
.s_addr
), 0, 0, "No more tunnels\n");
1321 STAT(tunnel_overflow
);
1325 tunnel
[t
].ip
= ntohl(*(ipt
*) & addr
->sin_addr
);
1326 tunnel
[t
].port
= ntohs(addr
->sin_port
);
1327 tunnel
[t
].window
= 4; // default window
1328 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
);
1329 STAT(tunnel_created
);
1332 // This is used to time out old tunnels
1333 tunnel
[t
].lastrec
= time_now
;
1335 // check sequence of this message
1337 int skip
= tunnel
[t
].window
; // track how many in-window packets are still in queue
1338 // some to clear maybe?
1339 while (tunnel
[t
].controlc
&& (((tunnel
[t
].ns
- tunnel
[t
].controlc
) - nr
) & 0x8000))
1341 controlt
*c
= tunnel
[t
].controls
;
1342 tunnel
[t
].controls
= c
->next
;
1343 tunnel
[t
].controlc
--;
1344 c
->next
= controlfree
;
1347 tunnel
[t
].try = 0; // we have progress
1350 // If the 'ns' just received is not the 'nr' we're
1351 // expecting, just send an ack and drop it.
1353 // if 'ns' is less, then we got a retransmitted packet.
1354 // if 'ns' is greater than missed a packet. Either way
1355 // we should ignore it.
1356 if (ns
!= tunnel
[t
].nr
)
1358 // is this the sequence we were expecting?
1359 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
);
1360 STAT(tunnel_rx_errors
);
1362 if (l
) // Is this not a ZLB?
1366 // receiver advance (do here so quoted correctly in any sends below)
1367 if (l
) tunnel
[t
].nr
= (ns
+ 1);
1368 if (skip
< 0) skip
= 0;
1369 if (skip
< tunnel
[t
].controlc
)
1371 // some control packets can now be sent that were previous stuck out of window
1372 int tosend
= tunnel
[t
].window
- skip
;
1373 controlt
*c
= tunnel
[t
].controls
;
1381 tunnel
[t
].try = 0; // first send
1382 tunnelsend(c
->buf
, c
->length
, t
);
1387 if (!tunnel
[t
].controlc
)
1388 tunnel
[t
].retry
= 0; // caught up
1391 { // if not a null message
1393 while (l
&& !(fatal
& 0x80))
1395 u16 n
= (ntohs(*(u16
*) p
) & 0x3FF);
1402 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Invalid length in AVP\n");
1403 STAT(tunnel_rx_errors
);
1410 // handle hidden AVPs
1411 if (!*config
->l2tpsecret
)
1413 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Hidden AVP requested, but no L2TP secret.\n");
1417 if (!session
[s
].random_vector_length
)
1419 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Hidden AVP requested, but no random vector.\n");
1423 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Hidden AVP\n");
1427 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Unrecognised AVP flags %02X\n", *b
);
1434 log(2, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Unknown AVP vendor %d\n", ntohs(*(u16
*) (b
)));
1439 mtype
= ntohs(*(u16
*) (b
));
1443 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " AVP %d (%s) len %d\n", mtype
, avpnames
[mtype
], n
);
1446 case 0: // message type
1447 message
= ntohs(*(u16
*) b
);
1448 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Message type = %d (%s)\n", *b
,
1449 l2tp_message_types
[message
]);
1450 mandatorymessage
= flags
;
1452 case 1: // result code
1454 u16 rescode
= ntohs(*(u16
*)(b
));
1455 const char* resdesc
= "(unknown)";
1456 if (message
== 4) { /* StopCCN */
1457 if (rescode
<= MAX_STOPCCN_RESULT_CODE
)
1458 resdesc
= stopccn_result_codes
[rescode
];
1459 } else if (message
== 14) { /* CDN */
1460 if (rescode
<= MAX_CDN_RESULT_CODE
)
1461 resdesc
= cdn_result_codes
[rescode
];
1464 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Result Code %d: %s\n",
1467 u16 errcode
= ntohs(*(u16
*)(b
+ 2));
1468 const char* errdesc
= "(unknown)";
1469 if (errcode
<= MAX_ERROR_CODE
)
1470 errdesc
= error_codes
[errcode
];
1471 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Error Code %d: %s\n",
1475 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Error String: %.*s\n",
1481 case 2: // protocol version
1483 version
= ntohs(*(u16
*) (b
));
1484 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Protocol version = %d\n", version
);
1485 if (version
&& version
!= 0x0100)
1486 { // allow 0.0 and 1.0
1487 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Bad protocol version %04X\n",
1494 case 3: // framing capabilities
1495 // log(4, ntohl(addr->sin_addr.s_addr), s, t, "Framing capabilities\n");
1497 case 4: // bearer capabilities
1498 // log(4, ntohl(addr->sin_addr.s_addr), s, t, "Bearer capabilities\n");
1500 case 5: // tie breaker
1501 // We never open tunnels, so we don't care about tie breakers
1502 // log(4, ntohl(addr->sin_addr.s_addr), s, t, "Tie breaker\n");
1504 case 6: // firmware revision
1505 // log(4, ntohl(addr->sin_addr.s_addr), s, t, "Firmware revision\n");
1507 case 7: // host name
1508 memset(tunnel
[t
].hostname
, 0, 128);
1509 memcpy(tunnel
[t
].hostname
, b
, (n
>= 127) ? 127 : n
);
1510 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Tunnel hostname = \"%s\"\n", tunnel
[t
].hostname
);
1511 // TBA - to send to RADIUS
1513 case 8: // vendor name
1514 memset(tunnel
[t
].vendor
, 0, sizeof(tunnel
[t
].vendor
));
1515 memcpy(tunnel
[t
].vendor
, b
, (n
>= sizeof(tunnel
[t
].vendor
) - 1) ? sizeof(tunnel
[t
].vendor
) - 1 : n
);
1516 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Vendor name = \"%s\"\n", tunnel
[t
].vendor
);
1518 case 9: // assigned tunnel
1519 tunnel
[t
].far
= ntohs(*(u16
*) (b
));
1520 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Remote tunnel id = %d\n", tunnel
[t
].far
);
1522 case 10: // rx window
1523 tunnel
[t
].window
= ntohs(*(u16
*) (b
));
1524 if (!tunnel
[t
].window
)
1525 tunnel
[t
].window
= 1; // window of 0 is silly
1526 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " rx window = %d\n", tunnel
[t
].window
);
1528 case 11: // Challenge
1530 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " LAC requested CHAP authentication for tunnel\n");
1531 build_chap_response(b
, 2, n
, &chapresponse
);
1534 case 14: // assigned session
1535 asession
= session
[s
].far
= ntohs(*(u16
*) (b
));
1536 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " assigned session = %d\n", asession
);
1538 case 15: // call serial number
1539 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " call serial number = %d\n", ntohl(*(u32
*)b
));
1541 case 18: // bearer type
1542 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " bearer type = %d\n", ntohl(*(u32
*)b
));
1545 case 19: // framing type
1546 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " framing type = %d\n", ntohl(*(u32
*)b
));
1549 case 21: // called number
1550 memset(called
, 0, MAXTEL
);
1551 memcpy(called
, b
, (n
>= MAXTEL
) ? (MAXTEL
-1) : n
);
1552 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Called <%s>\n", called
);
1554 case 22: // calling number
1555 memset(calling
, 0, MAXTEL
);
1556 memcpy(calling
, b
, (n
>= MAXTEL
) ? (MAXTEL
-1) : n
);
1557 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Calling <%s>\n", calling
);
1559 case 24: // tx connect speed
1562 session
[s
].tx_connect_speed
= ntohl(*(u32
*)b
);
1566 // AS5300s send connect speed as a string
1568 memcpy(tmp
, b
, (n
>= 30) ? 30 : n
);
1569 session
[s
].tx_connect_speed
= atol(tmp
);
1571 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " TX connect speed <%u>\n",
1572 session
[s
].tx_connect_speed
);
1574 case 38: // rx connect speed
1577 session
[s
].rx_connect_speed
= ntohl(*(u32
*)b
);
1581 // AS5300s send connect speed as a string
1583 memcpy(tmp
, b
, (n
>= 30) ? 30 : n
);
1584 session
[s
].rx_connect_speed
= atol(tmp
);
1586 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " RX connect speed <%u>\n",
1587 session
[s
].rx_connect_speed
);
1589 case 25: // Physical Channel ID
1591 u32 tmp
= ntohl(*(u32
*)b
);
1592 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Physical Channel ID <%X>\n", tmp
);
1595 case 29: // Proxy Authentication Type
1597 u16 authtype
= ntohs(*(u16
*)b
);
1598 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Proxy Auth Type %d (%s)\n",
1599 authtype
, authtypes
[authtype
]);
1600 requestchap
= (authtype
== 2);
1603 case 30: // Proxy Authentication Name
1605 char authname
[64] = {0};
1606 memcpy(authname
, b
, (n
> 63) ? 63 : n
);
1607 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Proxy Auth Name (%s)\n",
1611 case 31: // Proxy Authentication Challenge
1613 memcpy(radius
[session
[s
].radius
].auth
, b
, 16);
1614 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Proxy Auth Challenge\n");
1617 case 32: // Proxy Authentication ID
1619 u16 authid
= ntohs(*(u16
*)(b
));
1620 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Proxy Auth ID (%d)\n",
1622 if (session
[s
].radius
)
1623 radius
[session
[s
].radius
].id
= authid
;
1626 case 33: // Proxy Authentication Response
1628 char authresp
[64] = {0};
1629 memcpy(authresp
, b
, (n
> 63) ? 63 : n
);
1630 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Proxy Auth Response\n");
1633 case 27: // last send lcp
1634 { // find magic number
1635 u8
*p
= b
, *e
= p
+ n
;
1636 while (p
< e
&& p
[1])
1638 if (*p
== 5 && p
[1] == 6)
1639 amagic
= ntohl(*(u32
*) (p
+ 2));
1640 else if (*p
== 3 && p
[1] == 5 && *(u16
*) (p
+ 2) == htons(PPPCHAP
) && p
[4] == 5)
1643 aflags
|= SESSIONPFC
;
1645 aflags
|= SESSIONACFC
;
1650 char tmp
[500] = {0};
1652 memcpy((tmp
+ 1), b
, n
);
1656 case 28: // last recv lcp confreq
1658 char tmp
[500] = {0};
1660 memcpy((tmp
+ 1), b
, n
);
1663 case 26: // Initial Received LCP CONFREQ
1665 char tmp
[500] = {0};
1667 memcpy((tmp
+ 1), b
, n
);
1670 case 39: // seq required - we control it as an LNS anyway...
1672 case 36: // Random Vector
1673 log(4, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Random Vector received. Enabled AVP Hiding.\n");
1674 memset(session
[s
].random_vector
, 0, sizeof(session
[s
].random_vector
));
1675 memcpy(session
[s
].random_vector
, b
, n
);
1676 session
[s
].random_vector_length
= n
;
1679 log(2, ntohl(addr
->sin_addr
.s_addr
), s
, t
, " Unknown AVP type %d\n", mtype
);
1686 tunnelshutdown(t
, "Unknown Mandatory AVP");
1690 case 1: // SCCRQ - Start Control Connection Request
1692 controlt
*c
= controlnew(2); // sending SCCRP
1693 control16(c
, 2, version
, 1); // protocol version
1694 control32(c
, 3, 3, 1); // framing
1695 controls(c
, 7, tunnel
[t
].hostname
, 1); // host name (TBA)
1696 if (chapresponse
) controlb(c
, 13, chapresponse
, 16, 1); // Challenge response
1697 control16(c
, 9, t
, 1); // assigned tunnel
1698 controladd(c
, t
, s
); // send the resply
1700 tunnel
[t
].state
= TUNNELOPENING
;
1703 tunnel
[t
].state
= TUNNELOPEN
;
1706 tunnel
[t
].state
= TUNNELOPEN
;
1707 controlnull(t
); // ack
1710 controlnull(t
); // ack
1711 tunnelshutdown(t
, "Stopped"); // Shut down cleanly
1712 tunnelkill(t
, "Stopped"); // Immediately force everything dead
1715 controlnull(t
); // simply ACK
1729 STAT(session_overflow
);
1730 tunnelshutdown(t
, "No free sessions");
1738 sessionfree
= session
[s
].next
;
1739 memset(&session
[s
], 0, sizeof(session
[s
]));
1741 if (s
> config
->cluster_highest_sessionid
)
1742 config
->cluster_highest_sessionid
= s
;
1744 // make a RADIUS session
1745 if (!(r
= radiusnew(s
)))
1747 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "No free RADIUS sessions for ICRQ\n");
1748 sessionkill(s
, "no free RADIUS sesions");
1752 c
= controlnew(11); // sending ICRP
1753 session
[s
].id
= sessionid
++;
1754 session
[s
].opened
= time(NULL
);
1755 session
[s
].tunnel
= t
;
1756 session
[s
].far
= asession
;
1757 session
[s
].last_packet
= time_now
;
1758 log(3, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "New session (%d/%d)\n", tunnel
[t
].far
, session
[s
].far
);
1759 control16(c
, 14, s
, 1); // assigned session
1760 controladd(c
, t
, s
); // send the reply
1762 // Generate a random challenge
1764 for (n
= 0; n
< 15; n
++)
1765 radius
[r
].auth
[n
] = rand();
1767 strncpy(radius
[r
].calling
, calling
, sizeof(radius
[r
].calling
) - 1);
1768 strncpy(session
[s
].called
, called
, sizeof(session
[s
].called
) - 1);
1769 strncpy(session
[s
].calling
, calling
, sizeof(session
[s
].calling
) - 1);
1770 STAT(session_created
);
1777 session
[s
].magic
= amagic
; // set magic number
1778 session
[s
].l2tp_flags
= aflags
; // set flags received
1779 log(3, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Magic %X Flags %X\n", amagic
, aflags
);
1780 controlnull(t
); // ack
1781 // In CHAP state, request PAP instead
1786 controlnull(t
); // ack
1787 sessionshutdown(s
, "Closed (Received CDN)");
1790 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Missing message type\n");
1793 STAT(tunnel_rx_errors
);
1794 if (mandatorymessage
& 0x80)
1795 tunnelshutdown(t
, "Unknown message");
1797 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Unknown message type %d\n", message
);
1800 if (chapresponse
) free(chapresponse
);
1801 cluster_send_tunnel(t
);
1805 log(4, 0, s
, t
, " Got a ZLB ack\n");
1812 log_hex(5, "Receive Tunnel Data", p
, l
);
1813 if (l
> 2 && p
[0] == 0xFF && p
[1] == 0x03)
1814 { // HDLC address header, discard
1820 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Short ppp length %d\n", l
);
1821 STAT(tunnel_rx_errors
);
1831 prot
= ntohs(*(u16
*) p
);
1836 if (s
&& !session
[s
].tunnel
) // Is something wrong??
1838 if (!config
->cluster_iam_master
)
1840 // Pass it off to the master to deal with..
1841 master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
);
1846 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "UDP packet contains session %d "
1847 "but no session[%d].tunnel exists (LAC said"
1848 " tunnel = %d). Dropping packet.\n", s
, s
, t
);
1849 STAT(tunnel_rx_errors
);
1855 log(3, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Session %d is closing. Don't process PPP packets\n", s
);
1856 // I'm pretty sure this isn't right -- mo.
1857 // return; // closing session, PPP not processed
1861 session
[s
].last_packet
= time_now
;
1862 if (!config
->cluster_iam_master
) { master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
); return; }
1863 processpap(t
, s
, p
, l
);
1865 else if (prot
== PPPCHAP
)
1867 session
[s
].last_packet
= time_now
;
1868 if (!config
->cluster_iam_master
) { master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
); return; }
1869 processchap(t
, s
, p
, l
);
1871 else if (prot
== PPPLCP
)
1873 session
[s
].last_packet
= time_now
;
1874 if (!config
->cluster_iam_master
) { master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
); return; }
1875 processlcp(t
, s
, p
, l
);
1877 else if (prot
== PPPIPCP
)
1879 session
[s
].last_packet
= time_now
;
1880 if (!config
->cluster_iam_master
) { master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
); return; }
1881 processipcp(t
, s
, p
, l
);
1883 else if (prot
== PPPCCP
)
1885 session
[s
].last_packet
= time_now
;
1886 if (!config
->cluster_iam_master
) { master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
); return; }
1887 processccp(t
, s
, p
, l
);
1889 else if (prot
== PPPIP
)
1891 if (!config
->cluster_iam_master
)
1893 // We're a slave. Should we forward this packet to the master?
1895 // Is this a walled garden session, or something that needs it's
1896 // idle time updated??
1898 // Maintain the idle timeouts on the master. If this would
1899 // significantly reset the idletimeout, run it via the master
1900 // to refresh the master's idle timer.
1901 // Not sure this is ideal: It may re-order packets.
1903 if (session
[s
].walled_garden
|| (session
[s
].last_packet
+ (ECHO_TIMEOUT
/2)) < time_now
)
1905 master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
);
1906 session
[s
].last_packet
= time_now
;
1909 // fall through to processipin.
1911 session
[s
].last_packet
= time_now
;
1912 processipin(t
, s
, p
, l
);
1916 STAT(tunnel_rx_errors
);
1917 log(1, ntohl(addr
->sin_addr
.s_addr
), s
, t
, "Unknown PPP protocol %04X\n", prot
);
1922 // read and process packet on tun
1923 void processtun(u8
* buf
, int len
)
1925 log_hex(5, "Receive TUN Data", buf
, len
);
1926 STAT(tun_rx_packets
);
1927 INC_STAT(tun_rx_bytes
, len
);
1929 CSTAT(call_processtun
);
1935 log(1, 0, 0, 0, "Short tun packet %d bytes\n", len
);
1936 STAT(tun_rx_errors
);
1940 if (*(u16
*) (buf
+ 2) == htons(PKTIP
)) // IP
1941 processipout(buf
, len
);
1946 // Maximum number of actions to complete.
1947 // This is to avoid sending out too many packets
1949 #define MAX_ACTIONS 500
1951 int regular_cleanups(void)
1953 static sessionidt s
= 0; // Next session to check for actions on.
1957 static clockt next_acct
= 0;
1960 log(3, 0, 0, 0, "Begin regular cleanup\n");
1962 for (r
= 1; r
< MAXRADIUS
; r
++)
1964 if (!radius
[r
].state
)
1966 if (radius
[r
].retry
)
1968 if (radius
[r
].retry
<= TIME
)
1971 radius
[r
].retry
= backoff(radius
[r
].try+1); // Is this really needed? --mo
1973 for (t
= 1; t
<= config
->cluster_highest_tunnelid
; t
++)
1975 // check for expired tunnels
1976 if (tunnel
[t
].die
&& tunnel
[t
].die
<= TIME
)
1978 STAT(tunnel_timeout
);
1979 tunnelkill(t
, "Expired");
1982 // check for message resend
1983 if (tunnel
[t
].retry
&& tunnel
[t
].controlc
)
1985 // resend pending messages as timeout on reply
1986 if (tunnel
[t
].retry
<= TIME
)
1988 controlt
*c
= tunnel
[t
].controls
;
1989 u8 w
= tunnel
[t
].window
;
1990 tunnel
[t
].try++; // another try
1991 if (tunnel
[t
].try > 5)
1992 tunnelkill(t
, "Timeout on control message"); // game over
1996 tunnelsend(c
->buf
, c
->length
, t
);
2002 if (tunnel
[t
].state
== TUNNELOPEN
&& tunnel
[t
].lastrec
< TIME
+ 600)
2004 controlt
*c
= controlnew(6); // sending HELLO
2005 controladd(c
, t
, 0); // send the message
2006 log(3, tunnel
[t
].ip
, 0, t
, "Sending HELLO message\n");
2009 // Check for tunnel changes requested from the CLI
2010 if ((a
= cli_tunnel_actions
[t
].action
))
2012 cli_tunnel_actions
[t
].action
= 0;
2013 if (a
& CLI_TUN_KILL
)
2015 log(2, tunnel
[t
].ip
, 0, t
, "Dropping tunnel by CLI\n");
2016 tunnelshutdown(t
, "Requested by administrator");
2023 for (i
= 1; i
<= config
->cluster_highest_sessionid
; i
++)
2026 if (s
> config
->cluster_highest_sessionid
)
2029 if (!session
[s
].tunnel
) // Session isn't in use
2032 if (!session
[s
].die
&& session
[s
].ip
&& !(session
[s
].flags
& SF_IPCP_ACKED
) )
2034 // IPCP has not completed yet. Resend
2035 log(3, session
[s
].ip
, s
, session
[s
].tunnel
, "No ACK for initial IPCP ConfigReq... resending\n");
2036 sendipcp(session
[s
].tunnel
, s
);
2039 // check for expired sessions
2040 if (session
[s
].die
&& session
[s
].die
<= TIME
)
2042 sessionkill(s
, "Expired");
2043 if (++count
>= MAX_ACTIONS
) break;
2047 // Drop sessions who have not responded within IDLE_TIMEOUT seconds
2048 if (session
[s
].last_packet
&& (time_now
- session
[s
].last_packet
>= IDLE_TIMEOUT
))
2050 sessionkill(s
, "No response to LCP ECHO requests");
2051 STAT(session_timeout
);
2052 if (++count
>= MAX_ACTIONS
) break;
2056 // No data in IDLE_TIMEOUT seconds, send LCP ECHO
2057 if (session
[s
].user
[0] && (time_now
- session
[s
].last_packet
>= ECHO_TIMEOUT
))
2059 u8 b
[MAXCONTROL
] = {0};
2061 u8
*q
= makeppp(b
, sizeof(b
), 0, 0, session
[s
].tunnel
, s
, PPPLCP
);
2063 log(3, session
[s
].ip
, s
, t
, "failed to send ECHO packet.\n");
2068 *(u8
*)(q
+ 1) = (time_now
% 255); // ID
2069 *(u16
*)(q
+ 2) = htons(8); // Length
2070 *(u32
*)(q
+ 4) = 0; // Magic Number (not supported)
2072 log(4, session
[s
].ip
, s
, session
[s
].tunnel
, "No data in %d seconds, sending LCP ECHO\n",
2073 (int)(time_now
- session
[s
].last_packet
));
2074 tunnelsend(b
, 24, session
[s
].tunnel
); // send it
2075 if (++count
>= MAX_ACTIONS
) break;
2079 // Check for actions requested from the CLI
2080 if ((a
= cli_session_actions
[s
].action
))
2084 cli_session_actions
[s
].action
= 0;
2085 if (a
& CLI_SESS_KILL
)
2087 log(2, 0, s
, session
[s
].tunnel
, "Dropping session by CLI\n");
2088 sessionshutdown(s
, "Requested by administrator");
2089 a
= 0; // dead, no need to check for other actions
2092 if (a
& CLI_SESS_SNOOP
)
2094 log(2, 0, s
, session
[s
].tunnel
, "Snooping session by CLI (to %s:%d)\n",
2095 inet_toa(cli_session_actions
[s
].snoop_ip
), cli_session_actions
[s
].snoop_port
);
2097 session
[s
].snoop_ip
= cli_session_actions
[s
].snoop_ip
;
2098 session
[s
].snoop_port
= cli_session_actions
[s
].snoop_port
;
2102 if (a
& CLI_SESS_NOSNOOP
)
2104 log(2, 0, s
, session
[s
].tunnel
, "Unsnooping session by CLI\n");
2105 session
[s
].snoop_ip
= 0;
2106 session
[s
].snoop_port
= 0;
2110 if (a
& CLI_SESS_THROTTLE
)
2112 log(2, 0, s
, session
[s
].tunnel
, "Throttling session by CLI (to %d)\n",
2113 cli_session_actions
[s
].throttle
);
2115 throttle_session(s
, cli_session_actions
[s
].throttle
);
2118 if (a
& CLI_SESS_NOTHROTTLE
)
2120 log(2, 0, s
, session
[s
].tunnel
, "Un-throttling session by CLI\n");
2121 throttle_session(s
, 0);
2125 cluster_send_session(s
);
2127 if (++count
>= MAX_ACTIONS
) break;
2132 if (config
->accounting_dir
&& next_acct
<= TIME
)
2134 // Dump accounting data
2135 next_acct
= TIME
+ ACCT_TIME
;
2139 if (count
>= MAX_ACTIONS
)
2140 return 1; // Didn't finish!
2142 log(3, 0, 0, 0, "End regular cleanup (%d actions), next in %d seconds\n", count
, config
->cleanup_interval
);
2148 // Are we in the middle of a tunnel update, or radius
2151 int still_busy(void)
2154 static int last_talked
= 0;
2155 for (i
= config
->cluster_highest_tunnelid
; i
> 0 ; --i
) {
2156 if (!tunnel
[i
].controlc
)
2159 if (last_talked
!= TIME
) {
2160 log(2,0,0,0, "Tunnel %d still has un-acked control messages.\n", i
);
2166 for (i
= 1; i
< MAXRADIUS
; i
++)
2168 if (radius
[i
].state
== RADIUSNULL
)
2170 if (radius
[i
].state
== RADIUSWAIT
)
2173 if (last_talked
!= TIME
) {
2174 log(2,0,0,0, "Radius session %d is still busy (sid %d)\n", i
, radius
[i
].session
);
2183 // main loop - gets packets on tun or udp and processes them
2190 clockt next_cluster_ping
= 0; // send initial ping immediately
2191 time_t next_clean
= time_now
+ config
->cleanup_interval
;
2193 log(4, 0, 0, 0, "Beginning of main loop. udpfd=%d, tunfd=%d, cluster_sockfd=%d, controlfd=%d\n",
2194 udpfd
, tunfd
, cluster_sockfd
, controlfd
);
2199 FD_SET(controlfd
, &cr
);
2201 if (cluster_sockfd
) FD_SET(cluster_sockfd
, &cr
);
2203 if (cn
< tunfd
) cn
= tunfd
;
2204 if (cn
< controlfd
) cn
= controlfd
;
2205 if (cn
< clifd
) cn
= clifd
;
2206 if (cn
< cluster_sockfd
) cn
= cluster_sockfd
;
2207 for (i
= 0; i
< config
->num_radfds
; i
++)
2209 if (!radfds
[i
]) continue;
2210 FD_SET(radfds
[i
], &cr
);
2215 while (!main_quit
|| still_busy())
2221 int bgp_set
[BGP_NUM_PEERS
];
2224 if (config
->reload_config
)
2226 // Update the config state based on config settings
2230 memcpy(&r
, &cr
, sizeof(fd_set
));
2232 to
.tv_usec
= 100000; // 1/10th of a second.
2236 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2238 bgp_set
[i
] = bgp_select_state(&bgp_peers
[i
]);
2241 FD_SET(bgp_peers
[i
].sock
, &r
);
2242 if (bgp_peers
[i
].sock
> n
)
2243 n
= bgp_peers
[i
].sock
;
2248 FD_SET(bgp_peers
[i
].sock
, &w
);
2249 if (bgp_peers
[i
].sock
> n
)
2250 n
= bgp_peers
[i
].sock
;
2254 n
= select(n
+ 1, &r
, &w
, 0, &to
);
2256 n
= select(n
+ 1, &r
, 0, 0, &to
);
2265 log(0, 0, 0, 0, "Error returned from select(): %s\n", strerror(errno
));
2271 struct sockaddr_in addr
;
2272 int alen
= sizeof(addr
);
2273 if (FD_ISSET(udpfd
, &r
))
2276 for (c
= 0; c
< config
->multi_read_count
; c
++)
2278 if ((n
= recvfrom(udpfd
, buf
, sizeof(buf
), 0, (void *) &addr
, &alen
)) > 0)
2279 processudp(buf
, n
, &addr
);
2284 if (FD_ISSET(tunfd
, &r
))
2287 for (c
= 0; c
< config
->multi_read_count
; c
++)
2289 if ((n
= read(tunfd
, buf
, sizeof(buf
))) > 0)
2295 for (i
= 0; i
< config
->num_radfds
; i
++)
2296 if (FD_ISSET(radfds
[i
], &r
))
2297 processrad(buf
, recv(radfds
[i
], buf
, sizeof(buf
), 0), i
);
2298 if (FD_ISSET(cluster_sockfd
, &r
)) {
2300 size
= recvfrom(cluster_sockfd
, buf
, sizeof(buf
), MSG_WAITALL
, (void *) &addr
, &alen
);
2301 processcluster(buf
, size
, addr
.sin_addr
.s_addr
);
2303 if (FD_ISSET(controlfd
, &r
))
2304 processcontrol(buf
, recvfrom(controlfd
, buf
, sizeof(buf
), MSG_WAITALL
, (void *) &addr
, &alen
), &addr
);
2305 if (FD_ISSET(clifd
, &r
))
2307 struct sockaddr_in addr
;
2309 int len
= sizeof(addr
);
2311 if ((sockfd
= accept(clifd
, (struct sockaddr
*)&addr
, &len
)) <= 0)
2313 log(0, 0, 0, 0, "accept error: %s\n", strerror(errno
));
2324 // Runs on every machine (master and slaves).
2325 if (cluster_sockfd
&& next_cluster_ping
<= TIME
)
2327 // Check to see which of the cluster is still alive..
2329 cluster_send_ping(basetime
); // Only does anything if we're a slave
2330 cluster_check_master(); // ditto.
2332 cluster_heartbeat(); // Only does anything if we're a master.
2333 cluster_check_slaves(); // ditto.
2335 master_update_counts(); // If we're a slave, send our byte counters to our master.
2337 if (config
->cluster_iam_master
&& !config
->cluster_iam_uptodate
)
2338 next_cluster_ping
= TIME
+ 1; // out-of-date slaves, do fast updates
2340 next_cluster_ping
= TIME
+ config
->cluster_hb_interval
;
2343 // Run token bucket filtering queue..
2344 // Only run it every 1/10th of a second.
2345 // Runs on all machines both master and slave.
2347 static clockt last_run
= 0;
2348 if (last_run
!= TIME
) {
2354 /* Handle timeouts. Make sure that this gets run anyway, even if there was
2355 * something to read, else under load this will never actually run....
2358 if (config
->cluster_iam_master
&& next_clean
<= time_now
) {
2359 if (regular_cleanups()) { // Did it finish?
2360 next_clean
= time_now
+ 1 ; // Didn't finish. Check quickly.
2362 next_clean
= time_now
+ config
->cleanup_interval
; // Did. Move to next interval.
2367 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2369 bgp_process(&bgp_peers
[i
],
2370 bgp_set
[i
] ? FD_ISSET(bgp_peers
[i
].sock
, &r
) : 0,
2371 bgp_set
[i
] ? FD_ISSET(bgp_peers
[i
].sock
, &w
) : 0);
2376 // Are we the master and shutting down??
2377 if (config
->cluster_iam_master
)
2378 cluster_heartbeat(); // Flush any queued changes..
2380 // Ok. Notify everyone we're shutting down. If we're
2381 // the master, this will force an election.
2382 cluster_send_ping(0);
2385 // Important!!! We MUST not process any packets past this point!
2388 // Init data structures
2393 _statistics
= mmap(NULL
, sizeof(struct Tstats
), PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
2394 if (_statistics
== MAP_FAILED
)
2396 log(0, 0, 0, 0, "Error doing mmap for _statistics: %s\n", strerror(errno
));
2399 config
= mmap(NULL
, sizeof(struct configt
), PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
2400 if (config
== MAP_FAILED
)
2402 log(0, 0, 0, 0, "Error doing mmap for configuration: %s\n", strerror(errno
));
2405 memset(config
, 0, sizeof(struct configt
));
2406 time(&config
->start_time
);
2407 strncpy(config
->config_file
, CONFIGFILE
, sizeof(config
->config_file
) - 1);
2408 tunnel
= mmap(NULL
, sizeof(tunnelt
) * MAXTUNNEL
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
2409 if (tunnel
== MAP_FAILED
)
2411 log(0, 0, 0, 0, "Error doing mmap for tunnels: %s\n", strerror(errno
));
2414 session
= mmap(NULL
, sizeof(sessiont
) * MAXSESSION
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
2415 if (session
== MAP_FAILED
)
2417 log(0, 0, 0, 0, "Error doing mmap for sessions: %s\n", strerror(errno
));
2421 sess_count
= mmap(NULL
, sizeof(sessioncountt
) * MAXSESSION
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
2422 if (sess_count
== MAP_FAILED
)
2424 log(0, 0, 0, 0, "Error doing mmap for sessions_count: %s\n", strerror(errno
));
2428 radius
= mmap(NULL
, sizeof(radiust
) * MAXRADIUS
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
2429 if (radius
== MAP_FAILED
)
2431 log(0, 0, 0, 0, "Error doing mmap for radius: %s\n", strerror(errno
));
2434 ip_address_pool
= mmap(NULL
, sizeof(ippoolt
) * MAXIPPOOL
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
2435 if (ip_address_pool
== MAP_FAILED
)
2437 log(0, 0, 0, 0, "Error doing mmap for ip_address_pool: %s\n", strerror(errno
));
2441 ringbuffer
= mmap(NULL
, sizeof(struct Tringbuffer
), PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
2442 if (ringbuffer
== MAP_FAILED
)
2444 log(0, 0, 0, 0, "Error doing mmap for ringbuffer: %s\n", strerror(errno
));
2447 memset(ringbuffer
, 0, sizeof(struct Tringbuffer
));
2450 cli_session_actions
= mmap(NULL
, sizeof(struct cli_session_actions
) * MAXSESSION
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
2451 if (cli_session_actions
== MAP_FAILED
)
2453 log(0, 0, 0, 0, "Error doing mmap for cli session actions: %s\n", strerror(errno
));
2456 memset(cli_session_actions
, 0, sizeof(struct cli_session_actions
) * MAXSESSION
);
2457 cli_tunnel_actions
= mmap(NULL
, sizeof(struct cli_tunnel_actions
) * MAXSESSION
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
2458 if (cli_tunnel_actions
== MAP_FAILED
)
2460 log(0, 0, 0, 0, "Error doing mmap for cli tunnel actions: %s\n", strerror(errno
));
2463 memset(cli_tunnel_actions
, 0, sizeof(struct cli_tunnel_actions
) * MAXSESSION
);
2465 memset(tunnel
, 0, sizeof(tunnelt
) * MAXTUNNEL
);
2466 memset(session
, 0, sizeof(sessiont
) * MAXSESSION
);
2467 memset(radius
, 0, sizeof(radiust
) * MAXRADIUS
);
2468 memset(ip_address_pool
, 0, sizeof(ippoolt
) * MAXIPPOOL
);
2470 // Put all the sessions on the free list marked as undefined.
2471 for (i
= 1; i
< MAXSESSION
- 1; i
++) {
2472 session
[i
].next
= i
+ 1;
2473 session
[i
].tunnel
= T_UNDEF
; // mark it as not filled in.
2475 session
[MAXSESSION
- 1].next
= 0;
2478 // Mark all the tunnels as undefined (waiting to be filled in by a download).
2479 for (i
= 1; i
< MAXTUNNEL
- 1; i
++) {
2480 tunnel
[i
].state
= TUNNELUNDEF
; // mark it as not filled in.
2486 // Grab my hostname unless it's been specified
2487 gethostname(hostname
, sizeof(hostname
));
2488 if ((p
= strchr(hostname
, '.'))) *p
= 0;
2490 _statistics
->start_time
= _statistics
->last_reset
= time(NULL
);
2493 bgp_peers
= mmap(NULL
, sizeof(struct bgp_peer
) * BGP_NUM_PEERS
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
2494 if (bgp_peers
== MAP_FAILED
)
2496 log(0, 0, 0, 0, "Error doing mmap for bgp: %s\n", strerror(errno
));
2502 void initiptables(void)
2504 /* Flush the tables here so that we have a clean slate */
2506 // Not needed. 'nat' is setup by garden.c
2507 // mangle isn't used (as throttling is done by tbf inhouse).
2510 int assign_ip_address(sessionidt s
)
2514 time_t best_time
= time_now
;
2515 char *u
= session
[s
].user
;
2519 CSTAT(call_assign_ip_address
);
2521 for (i
= 1; i
< ip_pool_size
; i
++)
2523 if (!ip_address_pool
[i
].address
|| ip_address_pool
[i
].assigned
)
2526 if (!session
[s
].walled_garden
&& ip_address_pool
[i
].user
[0] && !strcmp(u
, ip_address_pool
[i
].user
))
2533 if (ip_address_pool
[i
].last
< best_time
)
2536 if (!(best_time
= ip_address_pool
[i
].last
))
2537 break; // never used, grab this one
2543 log(0, 0, s
, session
[s
].tunnel
, "assign_ip_address(): out of addresses\n");
2547 session
[s
].ip
= ip_address_pool
[best
].address
;
2548 session
[s
].ip_pool_index
= best
;
2549 ip_address_pool
[best
].assigned
= 1;
2550 ip_address_pool
[best
].last
= time_now
;
2551 ip_address_pool
[best
].session
= s
;
2552 if (session
[s
].walled_garden
)
2553 /* Don't track addresses of users in walled garden (note: this
2554 means that their address isn't "sticky" even if they get
2556 ip_address_pool
[best
].user
[0] = 0;
2558 strncpy(ip_address_pool
[best
].user
, u
, sizeof(ip_address_pool
[best
].user
) - 1);
2561 log(4, ip_address_pool
[best
].address
, s
, session
[s
].tunnel
,
2562 "assign_ip_address(): %s ip address %d from pool\n", reuse
? "Reusing" : "Allocating", best
);
2567 void free_ip_address(sessionidt s
)
2569 int i
= session
[s
].ip_pool_index
;
2572 return; // what the?
2574 if (i
< 0) // Is this actually part of the ip pool?
2578 cache_ipmap(session
[s
].ip
, -i
); // Change the mapping to point back to the ip pool index.
2580 ip_address_pool
[i
].assigned
= 0;
2581 ip_address_pool
[i
].session
= 0;
2582 ip_address_pool
[i
].last
= time_now
;
2585 CSTAT(call_free_ip_address
);
2590 // Fsck the address pool against the session table.
2591 // Normally only called when we become a master.
2593 // This isn't perfect: We aren't keep tracking of which
2594 // users used to have an IP address.
2596 void rebuild_address_pool(void)
2601 // Zero the IP pool allocation, and build
2602 // a map from IP address to pool index.
2603 for (i
= 1; i
< MAXIPPOOL
; ++i
) {
2604 ip_address_pool
[i
].assigned
= 0;
2605 ip_address_pool
[i
].session
= 0;
2606 if (!ip_address_pool
[i
].address
)
2609 cache_ipmap(ip_address_pool
[i
].address
, -i
); // Map pool IP to pool index.
2612 for (i
= 0; i
< MAXSESSION
; ++i
) {
2614 if (!session
[i
].ip
|| !session
[i
].tunnel
)
2616 ipid
= - lookup_ipmap(htonl(session
[i
].ip
));
2618 if (session
[i
].ip_pool_index
< 0) { // Not allocated out of the pool.
2619 if (ipid
< 1) // Not found in the pool either? good.
2622 log(0, 0, i
, 0, "Session %d has an IP address (%s) that was marked static, but is in the pool (%d)!\n",
2623 i
, inet_toa(session
[i
].ip
), ipid
);
2625 // Fall through and process it as part of the pool.
2629 if (ipid
> MAXIPPOOL
|| ipid
< 0) {
2630 log(0, 0, i
, 0, "Session %d has a pool IP that's not found in the pool! (%d)\n", i
, ipid
);
2632 session
[i
].ip_pool_index
= ipid
;
2636 ip_address_pool
[ipid
].assigned
= 1;
2637 ip_address_pool
[ipid
].session
= i
;
2638 ip_address_pool
[ipid
].last
= time_now
;
2639 strncpy(ip_address_pool
[ipid
].user
, session
[i
].user
, sizeof(ip_address_pool
[ipid
].user
) - 1);
2640 session
[i
].ip_pool_index
= ipid
;
2641 cache_ipmap(session
[i
].ip
, i
); // Fix the ip map.
2646 // Fix the address pool to match a changed session.
2647 // (usually when the master sends us an update).
2648 void fix_address_pool(int sid
)
2652 ipid
= session
[sid
].ip_pool_index
;
2654 if (ipid
> ip_pool_size
)
2655 return; // Ignore it. rebuild_address_pool will fix it up.
2657 if (ip_address_pool
[ipid
].address
!= session
[sid
].ip
)
2658 return; // Just ignore it. rebuild_address_pool will take care of it.
2660 ip_address_pool
[ipid
].assigned
= 1;
2661 ip_address_pool
[ipid
].session
= sid
;
2662 ip_address_pool
[ipid
].last
= time_now
;
2663 strncpy(ip_address_pool
[ipid
].user
, session
[sid
].user
, sizeof(ip_address_pool
[ipid
].user
) - 1);
2667 // Add a block of addresses to the IP pool to hand out.
2669 void add_to_ip_pool(u32 addr
, u32 mask
)
2673 mask
= 0xffffffff; // Host route only.
2677 if (ip_pool_size
>= MAXIPPOOL
) // Pool is full!
2680 for (i
= addr
;(i
& mask
) == addr
; ++i
)
2682 if ((i
& 0xff) == 0 || (i
&0xff) == 255)
2683 continue; // Skip 0 and broadcast addresses.
2685 ip_address_pool
[ip_pool_size
].address
= i
;
2686 ip_address_pool
[ip_pool_size
].assigned
= 0;
2688 if (ip_pool_size
>= MAXIPPOOL
)
2690 log(0,0,0,0, "Overflowed IP pool adding %s\n", inet_toa(htonl(addr
)) );
2696 // Initialize the IP address pool
2702 memset(ip_address_pool
, 0, sizeof(ip_address_pool
));
2704 if (!(f
= fopen(IPPOOLFILE
, "r")))
2706 log(0, 0, 0, 0, "Can't load pool file " IPPOOLFILE
": %s\n", strerror(errno
));
2710 while (ip_pool_size
< MAXIPPOOL
&& fgets(buf
, 4096, f
))
2713 buf
[4095] = 0; // Force it to be zero terminated/
2715 if (*buf
== '#' || *buf
== '\n')
2716 continue; // Skip comments / blank lines
2717 if ((p
= (char *)strrchr(buf
, '\n'))) *p
= 0;
2718 if ((p
= (char *)strchr(buf
, ':')))
2722 src
= inet_addr(buf
);
2723 if (src
== INADDR_NONE
)
2725 log(0, 0, 0, 0, "Invalid address pool IP %s\n", buf
);
2728 // This entry is for a specific IP only
2729 if (src
!= config
->bind_address
)
2734 if ((p
= (char *)strchr(pool
, '/')))
2738 u32 start
= 0, mask
= 0;
2740 log(2, 0, 0, 0, "Adding IP address range %s\n", buf
);
2742 if (!*p
|| !(numbits
= atoi(p
)))
2744 log(0, 0, 0, 0, "Invalid pool range %s\n", buf
);
2747 start
= ntohl(inet_addr(pool
));
2748 mask
= (u32
)(pow(2, numbits
) - 1) << (32 - numbits
);
2750 // Add a static route for this pool
2751 log(5, 0, 0, 0, "Adding route for address pool %s/%u\n", inet_toa(htonl(start
)), 32 + mask
);
2752 routeset(0, start
, mask
, 0, 1);
2754 add_to_ip_pool(start
, mask
);
2758 // It's a single ip address
2759 add_to_ip_pool(inet_addr(pool
), 0);
2763 log(1, 0, 0, 0, "IP address pool is %d addresses\n", ip_pool_size
- 1);
2766 void snoop_send_packet(char *packet
, u16 size
, ipt destination
, u16 port
)
2768 struct sockaddr_in snoop_addr
= {0};
2769 if (!destination
|| !port
|| snoopfd
<= 0 || size
<= 0 || !packet
)
2772 snoop_addr
.sin_family
= AF_INET
;
2773 snoop_addr
.sin_addr
.s_addr
= destination
;
2774 snoop_addr
.sin_port
= ntohs(port
);
2776 log(5, 0, 0, 0, "Snooping packet at %p (%d bytes) to %s:%d\n",
2777 packet
, size
, inet_toa(snoop_addr
.sin_addr
.s_addr
), htons(snoop_addr
.sin_port
));
2778 if (sendto(snoopfd
, packet
, size
, MSG_DONTWAIT
| MSG_NOSIGNAL
, (void *) &snoop_addr
, sizeof(snoop_addr
)) < 0)
2779 log(0, 0, 0, 0, "Error sending intercept packet: %s\n", strerror(errno
));
2780 STAT(packets_snooped
);
2783 void dump_acct_info()
2785 char filename
[1024];
2787 time_t t
= time(NULL
);
2792 CSTAT(call_dump_acct_info
);
2794 strftime(timestr
, 64, "%Y%m%d%H%M%S", localtime(&t
));
2795 snprintf(filename
, 1024, "%s/%s", config
->accounting_dir
, timestr
);
2797 for (i
= 0; i
< MAXSESSION
; i
++)
2799 if (!session
[i
].opened
|| !session
[i
].ip
|| !session
[i
].cin
|| !session
[i
].cout
|| !*session
[i
].user
|| session
[i
].walled_garden
)
2803 time_t now
= time(NULL
);
2804 if (!(f
= fopen(filename
, "w")))
2806 log(0, 0, 0, 0, "Can't write accounting info to %s: %s\n", filename
, strerror(errno
));
2809 log(3, 0, 0, 0, "Dumping accounting information to %s\n", filename
);
2810 fprintf(f
, "# dslwatch.pl dump file V1.01\n"
2814 "# format: username ip qos uptxoctets downrxoctets\n",
2820 log(4, 0, 0, 0, "Dumping accounting information for %s\n", session
[i
].user
);
2821 fprintf(f
, "%s %s %d %u %u\n",
2822 session
[i
].user
, // username
2823 inet_toa(htonl(session
[i
].ip
)), // ip
2824 (session
[i
].throttle
) ? 2 : 1, // qos
2825 (u32
)session
[i
].cin
, // uptxoctets
2826 (u32
)session
[i
].cout
); // downrxoctets
2828 session
[i
].pin
= session
[i
].cin
= 0;
2829 session
[i
].pout
= session
[i
].cout
= 0;
2836 int main(int argc
, char *argv
[])
2840 _program_name
= strdup(argv
[0]);
2842 time(&basetime
); // start clock
2845 while ((o
= getopt(argc
, argv
, "vc:h:a:")) >= 0)
2850 // Double fork to detach from terminal
2851 if (fork()) exit(0);
2852 if (fork()) exit(0);
2858 snprintf(hostname
, sizeof(hostname
), "%s", optarg
);
2862 printf("Args are:\n"
2863 "\t-d\tDetach from terminal\n"
2864 "\t-c <file>\tConfig file\n"
2865 "\t-h <hostname>\tForce hostname\n"
2866 "\t-a <address>\tUse specific address\n"
2874 // Start the timer routine off
2876 strftime(time_now_string
, sizeof(time_now_string
), "%Y-%m-%d %H:%M:%S", localtime(&time_now
));
2877 signal(SIGALRM
, sigalrm_handler
);
2878 siginterrupt(SIGALRM
, 0);
2887 log(0, 0, 0, 0, "L2TPNS version " VERSION
"\n");
2888 log(0, 0, 0, 0, "Copyright (c) 2003, 2004 Optus Internet Engineering\n");
2889 log(0, 0, 0, 0, "Copyright (c) 2002 FireBrick (Andrews & Arnold Ltd / Watchfront Ltd) - GPL licenced\n");
2892 rlim
.rlim_cur
= RLIM_INFINITY
;
2893 rlim
.rlim_max
= RLIM_INFINITY
;
2894 // Remove the maximum core size
2895 if (setrlimit(RLIMIT_CORE
, &rlim
) < 0)
2896 log(0, 0, 0, 0, "Can't set ulimit: %s\n", strerror(errno
));
2897 // Make core dumps go to /tmp
2901 if (config
->scheduler_fifo
)
2904 struct sched_param params
= {0};
2905 params
.sched_priority
= 1;
2907 if (get_nprocs() < 2)
2909 log(0, 0, 0, 0, "Not using FIFO scheduler, there is only 1 processor in the system.\n");
2910 config
->scheduler_fifo
= 0;
2914 if ((ret
= sched_setscheduler(0, SCHED_FIFO
, ¶ms
)) == 0)
2916 log(1, 0, 0, 0, "Using FIFO scheduler. Say goodbye to any other processes running\n");
2920 log(0, 0, 0, 0, "Error setting scheduler to FIFO: %s\n", strerror(errno
));
2921 config
->scheduler_fifo
= 0;
2926 /* Set up the cluster communications port. */
2927 if (cluster_init(config
->bind_address
) < 0)
2931 signal(SIGPIPE
, SIG_IGN
);
2932 bgp_setup(config
->as_number
);
2933 bgp_add_route(config
->bind_address
, 0xffffffff);
2934 if (*config
->bgp_peer
[0])
2935 bgp_start(&bgp_peers
[0], config
->bgp_peer
[0],
2936 config
->bgp_peer_as
[0], 0); /* 0 = routing disabled */
2938 if (*config
->bgp_peer
[1])
2939 bgp_start(&bgp_peers
[1], config
->bgp_peer
[1],
2940 config
->bgp_peer_as
[1], 0);
2944 log(1, 0, 0, 0, "Set up on interface %s\n", config
->tundevice
);
2952 signal(SIGHUP
, sighup_handler
);
2953 signal(SIGTERM
, sigterm_handler
);
2954 signal(SIGINT
, sigterm_handler
);
2955 signal(SIGQUIT
, sigquit_handler
);
2956 signal(SIGCHLD
, sigchild_handler
);
2960 // Drop privileges here
2961 if (config
->target_uid
> 0 && geteuid() == 0)
2962 setuid(config
->target_uid
);
2967 /* try to shut BGP down cleanly; with luck the sockets will be
2968 writable since we're out of the select */
2971 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2972 if (bgp_peers
[i
].state
== Established
)
2973 bgp_stop(&bgp_peers
[i
]);
2977 /* remove plugins (so cleanup code gets run) */
2980 /* kill CLI children */
2981 signal(SIGTERM
, SIG_IGN
);
2986 void sighup_handler(int junk
)
2988 if (log_stream
&& log_stream
!= stderr
)
2997 void sigalrm_handler(int junk
)
2999 // Log current traffic stats
3001 snprintf(config
->bandwidth
, sizeof(config
->bandwidth
),
3002 "UDP-ETH:%1.0f/%1.0f ETH-UDP:%1.0f/%1.0f TOTAL:%0.1f IN:%u OUT:%u",
3003 (udp_rx
/ 1024.0 / 1024.0 * 8),
3004 (eth_tx
/ 1024.0 / 1024.0 * 8),
3005 (eth_rx
/ 1024.0 / 1024.0 * 8),
3006 (udp_tx
/ 1024.0 / 1024.0 * 8),
3007 ((udp_tx
+ udp_rx
+ eth_tx
+ eth_rx
) / 1024.0 / 1024.0 * 8),
3008 udp_rx_pkt
, eth_rx_pkt
);
3010 udp_tx
= udp_rx
= 0;
3011 udp_rx_pkt
= eth_rx_pkt
= 0;
3012 eth_tx
= eth_rx
= 0;
3014 if (config
->dump_speed
)
3015 printf("%s\n", config
->bandwidth
);
3017 // Update the internal time counter
3019 strftime(time_now_string
, sizeof(time_now_string
), "%Y-%m-%d %H:%M:%S", localtime(&time_now
));
3024 struct param_timer p
= { time_now
};
3025 run_plugins(PLUGIN_TIMER
, &p
);
3030 void sigterm_handler(int junk
)
3032 log(1, 0, 0, 0, "Shutting down cleanly\n");
3033 if (config
->save_state
)
3039 void sigquit_handler(int junk
)
3043 log(1, 0, 0, 0, "Shutting down without saving sessions\n");
3044 for (i
= 1; i
< MAXSESSION
; i
++)
3046 if (session
[i
].opened
)
3047 sessionkill(i
, "L2TPNS Closing");
3049 for (i
= 1; i
< MAXTUNNEL
; i
++)
3051 if (tunnel
[i
].ip
|| tunnel
[i
].state
)
3052 tunnelshutdown(i
, "L2TPNS Closing");
3058 void sigchild_handler(int signal
)
3060 while (waitpid(-1, NULL
, WNOHANG
) > 0)
3070 char magic
[sizeof(DUMP_MAGIC
)-1];
3073 if (!config
->save_state
)
3079 if (stat(STATEFILE
, &sb
) < 0)
3085 if (sb
.st_mtime
< (time(NULL
) - 60))
3087 log(0, 0, 0, 0, "State file is too old to read, ignoring\n");
3092 f
= fopen(STATEFILE
, "r");
3097 log(0, 0, 0, 0, "Can't read state file: %s\n", strerror(errno
));
3101 if (fread(magic
, sizeof(magic
), 1, f
) != 1 || strncmp(magic
, DUMP_MAGIC
, sizeof(magic
)))
3103 log(0, 0, 0, 0, "Bad state file magic\n");
3107 log(1, 0, 0, 0, "Reading state information\n");
3108 if (fread(buf
, sizeof(buf
), 1, f
) != 1 || buf
[0] > MAXIPPOOL
|| buf
[1] != sizeof(ippoolt
))
3110 log(0, 0, 0, 0, "Error/mismatch reading ip pool header from state file\n");
3114 if (buf
[0] > ip_pool_size
)
3116 log(0, 0, 0, 0, "ip pool has shrunk! state = %d, current = %d\n", buf
[0], ip_pool_size
);
3120 log(2, 0, 0, 0, "Loading %u ip addresses\n", buf
[0]);
3121 for (i
= 0; i
< buf
[0]; i
++)
3123 if (fread(&itmp
, sizeof(itmp
), 1, f
) != 1)
3125 log(0, 0, 0, 0, "Error reading ip %d from state file: %s\n", i
, strerror(errno
));
3129 if (itmp
.address
!= ip_address_pool
[i
].address
)
3131 log(0, 0, 0, 0, "Mismatched ip %d from state file: pool may only be extended\n", i
);
3135 memcpy(&ip_address_pool
[i
], &itmp
, sizeof(itmp
));
3138 if (fread(buf
, sizeof(buf
), 1, f
) != 1 || buf
[0] != MAXTUNNEL
|| buf
[1] != sizeof(tunnelt
))
3140 log(0, 0, 0, 0, "Error/mismatch reading tunnel header from state file\n");
3144 log(2, 0, 0, 0, "Loading %u tunnels\n", MAXTUNNEL
);
3145 if (fread(tunnel
, sizeof(tunnelt
), MAXTUNNEL
, f
) != MAXTUNNEL
)
3147 log(0, 0, 0, 0, "Error reading tunnel data from state file\n");
3151 for (i
= 0; i
< MAXTUNNEL
; i
++)
3153 tunnel
[i
].controlc
= 0;
3154 tunnel
[i
].controls
= NULL
;
3155 tunnel
[i
].controle
= NULL
;
3156 if (*tunnel
[i
].hostname
)
3157 log(3, 0, 0, 0, "Created tunnel for %s\n", tunnel
[i
].hostname
);
3160 if (fread(buf
, sizeof(buf
), 1, f
) != 1 || buf
[0] != MAXSESSION
|| buf
[1] != sizeof(sessiont
))
3162 log(0, 0, 0, 0, "Error/mismatch reading session header from state file\n");
3166 log(2, 0, 0, 0, "Loading %u sessions\n", MAXSESSION
);
3167 if (fread(session
, sizeof(sessiont
), MAXSESSION
, f
) != MAXSESSION
)
3169 log(0, 0, 0, 0, "Error reading session data from state file\n");
3173 for (i
= 0; i
< MAXSESSION
; i
++)
3175 session
[i
].tbf_in
= 0;
3176 session
[i
].tbf_out
= 0;
3177 if (session
[i
].opened
)
3179 log(2, 0, i
, 0, "Loaded active session for user %s\n", session
[i
].user
);
3181 sessionsetup(session
[i
].tunnel
, i
);
3186 log(0, 0, 0, 0, "Loaded saved state information\n");
3194 if (!config
->save_state
)
3198 if (!(f
= fopen(STATEFILE
, "w")))
3201 log(1, 0, 0, 0, "Dumping state information\n");
3203 if (fwrite(DUMP_MAGIC
, sizeof(DUMP_MAGIC
)-1, 1, f
) != 1) break;
3205 log(2, 0, 0, 0, "Dumping %u ip addresses\n", ip_pool_size
);
3206 buf
[0] = ip_pool_size
;
3207 buf
[1] = sizeof(ippoolt
);
3208 if (fwrite(buf
, sizeof(buf
), 1, f
) != 1) break;
3209 if (fwrite(ip_address_pool
, sizeof(ippoolt
), ip_pool_size
, f
) != ip_pool_size
) break;
3211 log(2, 0, 0, 0, "Dumping %u tunnels\n", MAXTUNNEL
);
3213 buf
[1] = sizeof(tunnelt
);
3214 if (fwrite(buf
, sizeof(buf
), 1, f
) != 1) break;
3215 if (fwrite(tunnel
, sizeof(tunnelt
), MAXTUNNEL
, f
) != MAXTUNNEL
) break;
3217 log(2, 0, 0, 0, "Dumping %u sessions\n", MAXSESSION
);
3218 buf
[0] = MAXSESSION
;
3219 buf
[1] = sizeof(sessiont
);
3220 if (fwrite(buf
, sizeof(buf
), 1, f
) != 1) break;
3221 if (fwrite(session
, sizeof(sessiont
), MAXSESSION
, f
) != MAXSESSION
) break;
3223 if (fclose(f
) == 0) return; // OK
3226 log(0, 0, 0, 0, "Can't write state information: %s\n", strerror(errno
));
3230 void build_chap_response(char *challenge
, u8 id
, u16 challenge_length
, char **challenge_response
)
3233 *challenge_response
= NULL
;
3235 if (!*config
->l2tpsecret
)
3237 log(0, 0, 0, 0, "LNS requested CHAP authentication, but no l2tp secret is defined\n");
3241 log(4, 0, 0, 0, " Building challenge response for CHAP request\n");
3243 *challenge_response
= (char *)calloc(17, 1);
3246 MD5Update(&ctx
, &id
, 1);
3247 MD5Update(&ctx
, config
->l2tpsecret
, strlen(config
->l2tpsecret
));
3248 MD5Update(&ctx
, challenge
, challenge_length
);
3249 MD5Final(*challenge_response
, &ctx
);
3254 static int facility_value(char *name
)
3257 for (i
= 0; facilitynames
[i
].c_name
; i
++)
3259 if (strcmp(facilitynames
[i
].c_name
, name
) == 0)
3260 return facilitynames
[i
].c_val
;
3265 void update_config()
3268 static int timeout
= 0;
3269 static int interval
= 0;
3279 if (*config
->log_filename
)
3281 if (strstr(config
->log_filename
, "syslog:") == config
->log_filename
)
3283 char *p
= config
->log_filename
+ 7;
3286 openlog("l2tpns", LOG_PID
, facility_value(p
));
3290 else if (strchr(config
->log_filename
, '/') == config
->log_filename
)
3292 if ((log_stream
= fopen((char *)(config
->log_filename
), "a")))
3294 fseek(log_stream
, 0, SEEK_END
);
3295 setbuf(log_stream
, NULL
);
3299 log_stream
= stderr
;
3300 setbuf(log_stream
, NULL
);
3306 log_stream
= stderr
;
3307 setbuf(log_stream
, NULL
);
3312 config
->numradiusservers
= 0;
3313 for (i
= 0; i
< MAXRADSERVER
; i
++)
3314 if (config
->radiusserver
[i
]) config
->numradiusservers
++;
3316 if (!config
->numradiusservers
)
3318 log(0, 0, 0, 0, "No RADIUS servers defined!\n");
3321 config
->num_radfds
= 2 << RADIUS_SHIFT
;
3324 for (i
= 0; i
< MAXPLUGINS
; i
++)
3326 if (strcmp(config
->plugins
[i
], config
->old_plugins
[i
]) == 0)
3328 if (*config
->plugins
[i
])
3331 add_plugin(config
->plugins
[i
]);
3333 else if (*config
->old_plugins
[i
])
3336 remove_plugin(config
->old_plugins
[i
]);
3339 memcpy(config
->old_plugins
, config
->plugins
, sizeof(config
->plugins
));
3340 if (!config
->cleanup_interval
) config
->cleanup_interval
= 10;
3341 if (!config
->multi_read_count
) config
->multi_read_count
= 10;
3342 if (!config
->cluster_address
) config
->cluster_address
= inet_addr(DEFAULT_MCAST_ADDR
);
3343 if (!*config
->cluster_interface
)
3344 strncpy(config
->cluster_interface
, DEFAULT_MCAST_INTERFACE
, sizeof(config
->cluster_interface
) - 1);
3346 if (!config
->cluster_hb_interval
)
3347 config
->cluster_hb_interval
= PING_INTERVAL
; // Heartbeat every 0.5 seconds.
3349 if (!config
->cluster_hb_timeout
)
3350 config
->cluster_hb_timeout
= HB_TIMEOUT
; // 10 missed heartbeat triggers an election.
3352 if (interval
!= config
->cluster_hb_interval
|| timeout
!= config
->cluster_hb_timeout
)
3354 // Paranoia: cluster_check_master() treats 2 x interval + 1 sec as
3355 // late, ensure we're sufficiently larger than that
3356 int t
= 4 * config
->cluster_hb_interval
+ 11;
3358 if (config
->cluster_hb_timeout
< t
)
3360 log(0,0,0,0, "Heartbeat timeout %d too low, adjusting to %d\n", config
->cluster_hb_timeout
, t
);
3361 config
->cluster_hb_timeout
= t
;
3364 // Push timing changes to the slaves immediately if we're the master
3365 if (config
->cluster_iam_master
)
3366 cluster_heartbeat();
3368 interval
= config
->cluster_hb_interval
;
3369 timeout
= config
->cluster_hb_timeout
;
3372 config
->reload_config
= 0;
3375 void read_config_file()
3379 if (!config
->config_file
) return;
3380 if (!(f
= fopen(config
->config_file
, "r"))) {
3381 fprintf(stderr
, "Can't open config file %s: %s\n", config
->config_file
, strerror(errno
));
3385 log(3, 0, 0, 0, "Reading config file %s\n", config
->config_file
);
3387 log(3, 0, 0, 0, "Done reading config file\n");
3392 int sessionsetup(tunnelidt t
, sessionidt s
)
3394 // A session now exists, set it up
3400 CSTAT(call_sessionsetup
);
3403 log(3, session
[s
].ip
, s
, t
, "Doing session setup for session\n");
3405 if (!session
[s
].ip
|| session
[s
].ip
== 0xFFFFFFFE)
3407 assign_ip_address(s
);
3409 log(3, 0, s
, t
, " No IP allocated. Assigned %s from pool\n",
3410 inet_toa(htonl(session
[s
].ip
)));
3412 log(0, 0, s
, t
, " No IP allocated. The IP address pool is FULL!\n");
3416 // Make sure this is right
3417 session
[s
].tunnel
= t
;
3419 // zap old sessions with same IP and/or username
3420 // Don't kill gardened sessions - doing so leads to a DoS
3421 // from someone who doesn't need to know the password
3424 user
= session
[s
].user
;
3425 for (i
= 1; i
<= config
->cluster_highest_sessionid
; i
++)
3427 if (i
== s
) continue;
3428 if (ip
== session
[i
].ip
) sessionkill(i
, "Duplicate IP address");
3429 if (!session
[s
].walled_garden
&& !session
[i
].walled_garden
&& strcasecmp(user
, session
[i
].user
) == 0)
3430 sessionkill(i
, "Duplicate session for users");
3434 // Add the route for this session.
3436 // Static IPs need to be routed. Anything else
3437 // is part of the IP address pool and is already routed,
3438 // it just needs to be added to the IP cache.
3439 if (session
[s
].ip_pool_index
== -1) // static ip
3440 routeset(s
, session
[s
].ip
, 0, 0, 1);
3442 cache_ipmap(session
[s
].ip
, s
);
3444 for (r
= 0; r
< MAXROUTE
&& session
[s
].route
[r
].ip
; r
++)
3445 routeset(s
, session
[s
].route
[r
].ip
, session
[s
].route
[r
].mask
, session
[s
].ip
, 1);
3447 if (!session
[s
].sid
) { // did this session just finish radius?
3448 log(3, session
[s
].ip
, s
, t
, "Sending initial IPCP to client\n");
3450 session
[s
].sid
= ++last_sid
;
3453 // Run the plugin's against this new session.
3455 struct param_new_session data
= { &tunnel
[t
], &session
[s
] };
3456 run_plugins(PLUGIN_NEW_SESSION
, &data
);
3459 // Force throttling on or off (Actually : refresh the current throttling status)
3460 // This has the advantage of cleaning up after another throttled user who may have left
3461 // firewall rules lying around
3462 throttle_session(s
, session
[s
].throttle
);
3464 session
[s
].last_packet
= time_now
;
3467 char *sessionip
, *tunnelip
;
3468 sessionip
= strdup(inet_toa(htonl(session
[s
].ip
)));
3469 tunnelip
= strdup(inet_toa(htonl(tunnel
[t
].ip
)));
3470 log(2, session
[s
].ip
, s
, t
, "Login by %s at %s from %s (%s)\n",
3471 session
[s
].user
, sessionip
, tunnelip
, tunnel
[t
].hostname
);
3472 if (sessionip
) free(sessionip
);
3473 if (tunnelip
) free(tunnelip
);
3476 cluster_send_session(s
); // Mark it as dirty, and needing to the flooded to the cluster.
3478 return 1; // RADIUS OK and IP allocated, done...
3482 // This session just got dropped on us by the master or something.
3483 // Make sure our tables up up to date...
3485 int load_session(sessionidt s
, sessiont
*new)
3490 if (new->ip_pool_index
>= MAXIPPOOL
||
3491 new->tunnel
>= MAXTUNNEL
) {
3492 log(0,0,s
,0, "Strange session update received!\n");
3493 // FIXME! What to do here?
3498 // Ok. All sanity checks passed. Now we're committed to
3499 // loading the new session.
3502 session
[s
].tunnel
= new->tunnel
; // For logging in cache_ipmap
3505 if (new->ip
!= session
[s
].ip
) // Changed ip. fix up hash tables.
3507 if (session
[s
].ip
) // If there's an old one, remove it.
3509 // Remove any routes if the IP has changed
3510 for (i
= 0; i
< MAXROUTE
&& session
[s
].route
[i
].ip
; i
++)
3512 routeset(s
, session
[s
].route
[i
].ip
, session
[s
].route
[i
].mask
, session
[s
].ip
, 0);
3513 session
[s
].route
[i
].ip
= 0;
3516 if (session
[s
].ip_pool_index
== -1) // static IP
3517 routeset(s
, session
[s
].ip
, 0, 0, 0);
3518 else // It's part of the IP pool, remove it manually.
3519 uncache_ipmap(session
[s
].ip
);
3522 if (new->ip
) { // If there's a new one, add it.
3523 if (new->ip_pool_index
== -1)
3524 routeset(s
, new->ip
, 0, 0, 1);
3526 cache_ipmap(new->ip
, s
);
3530 // Update routed networks
3531 for (i
= 0; i
< MAXROUTE
&& (session
[s
].route
[i
].ip
|| new->route
[i
].ip
); i
++)
3533 if (new->route
[i
].ip
== session
[s
].route
[i
].ip
&&
3534 new->route
[i
].mask
== session
[s
].route
[i
].mask
)
3537 if (session
[s
].route
[i
].ip
) // Remove the old one if it exists.
3538 routeset(s
, session
[s
].route
[i
].ip
, session
[s
].route
[i
].mask
, session
[s
].ip
, 0);
3540 if (new->route
[i
].ip
) // Add the new one if it exists.
3541 routeset(s
, new->route
[i
].ip
, new->route
[i
].mask
, new->ip
, 1);
3544 if (new->tunnel
&& s
> config
->cluster_highest_sessionid
) // Maintain this in the slave. It's used
3545 // for walking the sessions to forward byte counts to the master.
3546 config
->cluster_highest_sessionid
= s
;
3548 memcpy(&session
[s
], new, sizeof(session
[s
])); // Copy over..
3550 // Do fixups into address pool.
3551 if (new->ip_pool_index
!= -1)
3552 fix_address_pool(s
);
3558 void ringbuffer_dump(FILE *stream
)
3560 int i
= ringbuffer
->head
;
3562 while (i
!= ringbuffer
->tail
)
3564 if (*ringbuffer
->buffer
[i
].message
)
3565 fprintf(stream
, "%d-%s", ringbuffer
->buffer
[i
].level
, ringbuffer
->buffer
[i
].message
);
3566 if (++i
== ringbuffer
->tail
) break;
3567 if (i
== RINGBUFFER_SIZE
) i
= 0;
3576 loaded_plugins
= ll_init();
3577 // Initialize the plugins to nothing
3578 for (i
= 0; i
< MAX_PLUGIN_TYPES
; i
++)
3579 plugins
[i
] = ll_init();
3582 static void *open_plugin(char *plugin_name
, int load
)
3584 char path
[256] = "";
3586 snprintf(path
, 256, PLUGINDIR
"/%s.so", plugin_name
);
3587 log(2, 0, 0, 0, "%soading plugin from %s\n", load
? "L" : "Un-l", path
);
3588 return dlopen(path
, RTLD_NOW
);
3591 void add_plugin(char *plugin_name
)
3593 static struct pluginfuncs funcs
= {
3598 sessiontbysessionidt
,
3599 sessionidtbysessiont
,
3605 void *p
= open_plugin(plugin_name
, 1);
3606 int (*initfunc
)(struct pluginfuncs
*);
3611 log(1, 0, 0, 0, " Plugin load failed: %s\n", dlerror());
3615 if (ll_contains(loaded_plugins
, p
))
3622 int *v
= dlsym(p
, "__plugin_api_version");
3623 if (!v
|| *v
!= PLUGIN_API_VERSION
)
3625 log(1, 0, 0, 0, " Plugin load failed: API version mismatch: %s\n", dlerror());
3631 if ((initfunc
= dlsym(p
, "plugin_init")))
3633 if (!initfunc(&funcs
))
3635 log(1, 0, 0, 0, " Plugin load failed: plugin_init() returned FALSE: %s\n", dlerror());
3641 ll_push(loaded_plugins
, p
);
3643 for (i
= 0; i
< max_plugin_functions
; i
++)
3646 if (plugin_functions
[i
] && (x
= dlsym(p
, plugin_functions
[i
])))
3648 log(3, 0, 0, 0, " Supports function \"%s\"\n", plugin_functions
[i
]);
3649 ll_push(plugins
[i
], x
);
3653 log(2, 0, 0, 0, " Loaded plugin %s\n", plugin_name
);
3656 static void run_plugin_done(void *plugin
)
3658 int (*donefunc
)(void) = dlsym(plugin
, "plugin_done");
3664 void remove_plugin(char *plugin_name
)
3666 void *p
= open_plugin(plugin_name
, 0);
3672 for (i
= 0; i
< max_plugin_functions
; i
++)
3675 if (plugin_functions
[i
] && (x
= dlsym(p
, plugin_functions
[i
])))
3676 ll_delete(plugins
[i
], x
);
3679 if (ll_contains(loaded_plugins
, p
))
3681 ll_delete(loaded_plugins
, p
);
3686 log(2, 0, 0, 0, "Removed plugin %s\n", plugin_name
);
3689 int run_plugins(int plugin_type
, void *data
)
3691 int (*func
)(void *data
);
3692 if (!plugins
[plugin_type
] || plugin_type
> max_plugin_functions
) return 1;
3694 ll_reset(plugins
[plugin_type
]);
3695 while ((func
= ll_next(plugins
[plugin_type
])))
3699 if (rc
== PLUGIN_RET_STOP
) return 1;
3700 if (rc
== PLUGIN_RET_ERROR
) return 0;
3709 ll_reset(loaded_plugins
);
3710 while ((p
= ll_next(loaded_plugins
)))
3714 void processcontrol(u8
* buf
, int len
, struct sockaddr_in
*addr
)
3718 struct param_control param
= { buf
, len
, ntohl(addr
->sin_addr
.s_addr
), ntohs(addr
->sin_port
), NULL
, 0, 0 };
3721 if (log_stream
&& config
->debug
>= 4)
3723 log(4, ntohl(addr
->sin_addr
.s_addr
), 0, 0, "Received ");
3724 dump_packet(buf
, log_stream
);
3727 resp
= calloc(1400, 1);
3728 l
= new_packet(PKT_RESP_ERROR
, resp
);
3729 *(int *)(resp
+ 6) = *(int *)(buf
+ 6);
3731 param
.type
= ntohs(*(short *)(buf
+ 2));
3732 param
.id
= ntohl(*(int *)(buf
+ 6));
3733 param
.data_length
= ntohs(*(short *)(buf
+ 4)) - 10;
3734 param
.data
= (param
.data_length
> 0) ? (char *)(buf
+ 10) : NULL
;
3735 param
.response
= resp
;
3736 param
.response_length
= l
;
3738 run_plugins(PLUGIN_CONTROL
, ¶m
);
3740 if (param
.send_response
)
3742 send_packet(controlfd
, ntohl(addr
->sin_addr
.s_addr
), ntohs(addr
->sin_port
), param
.response
, param
.response_length
);
3743 log(4, ntohl(addr
->sin_addr
.s_addr
), 0, 0, "Sent Control packet response\n");
3751 * Go through all of the tunnels and do some cleanups
3757 log(1, 0, 0, 0, "Cleaning tunnels array\n");
3759 for (i
= 1; i
< MAXTUNNEL
; i
++)
3762 || !*tunnel
[i
].hostname
3763 || (tunnel
[i
].state
== TUNNELDIE
&& tunnel
[i
].die
>= time_now
))
3770 void tunnelclear(tunnelidt t
)
3773 memset(&tunnel
[t
], 0, sizeof(tunnel
[t
]));
3774 tunnel
[t
].state
= TUNNELFREE
;
3777 tunnelidt
new_tunnel()
3780 for (i
= 1; i
< MAXTUNNEL
; i
++)
3782 if (tunnel
[i
].state
== TUNNELFREE
)
3784 log(4, 0, 0, i
, "Assigning tunnel ID %d\n", i
);
3785 if (i
> config
->cluster_highest_tunnelid
)
3786 config
->cluster_highest_tunnelid
= i
;
3790 log(0, 0, 0, 0, "Can't find a free tunnel! There shouldn't be this many in use!\n");
3795 // We're becoming the master. Do any required setup..
3797 // This is principally telling all the plugins that we're
3798 // now a master, and telling them about all the sessions
3799 // that are active too..
3801 void become_master(void)
3804 run_plugins(PLUGIN_BECOME_MASTER
, NULL
);
3806 for (s
= 1; s
<= config
->cluster_highest_sessionid
; ++s
) {
3807 if (!session
[s
].tunnel
) // Not an in-use session.
3810 run_plugins(PLUGIN_NEW_SESSION_MASTER
, &session
[s
]);
3816 int cmd_show_hist_idle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3822 if (CLI_HELP_REQUESTED
)
3823 return CLI_HELP_NO_ARGS
;
3826 for (i
= 0; i
< 64;++i
) buckets
[i
] = 0;
3828 for (s
= 1; s
<= config
->cluster_highest_sessionid
; ++s
) {
3830 if (!session
[s
].tunnel
)
3833 idle
= time_now
- session
[s
].last_packet
;
3834 idle
/= 5 ; // In multiples of 5 seconds.
3844 for (i
= 0; i
< 63; ++i
) {
3845 cli_print(cli
, "%3d seconds : %7.2f%% (%6d)", i
* 5, (double) buckets
[i
] * 100.0 / count
, buckets
[i
]);
3847 cli_print(cli
, "lots of secs : %7.2f%% (%6d)", (double) buckets
[63] * 100.0 / count
, buckets
[i
]);
3848 cli_print(cli
, "%d total sessions open.", count
);
3852 int cmd_show_hist_open(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3858 if (CLI_HELP_REQUESTED
)
3859 return CLI_HELP_NO_ARGS
;
3862 for (i
= 0; i
< 64;++i
) buckets
[i
] = 0;
3864 for (s
= 1; s
<= config
->cluster_highest_sessionid
; ++s
) {
3866 if (!session
[s
].tunnel
)
3869 d
= time_now
- session
[s
].opened
;
3872 while (d
> 1 && open
< 32) {
3881 for (i
= 0; i
< 30; ++i
) {
3882 cli_print(cli
, " < %8d seconds : %7.2f%% (%6d)", s
, (double) buckets
[i
] * 100.0 / count
, buckets
[i
]);
3885 cli_print(cli
, "%d total sessions open.", count
);