3 // Copyright (c) 2003, 2004, 2005, 2006 Optus Internet Engineering
4 // Copyright (c) 2002 FireBrick (Andrews & Arnold Ltd / Watchfront Ltd) - GPL licenced
11 #include <linux/if_tun.h>
15 #include <net/route.h>
18 #include <netinet/in.h>
19 #include <netinet/ip6.h>
25 #include <sys/ioctl.h>
26 #include <sys/socket.h>
29 #include <sys/resource.h>
37 #include <sys/sysinfo.h>
39 #include <linux/netlink.h>
40 #include <linux/rtnetlink.h>
47 #include "constants.h"
61 char * Vendor_name
= "Linux L2TPNS";
62 uint32_t call_serial_number
= 0;
66 configt
*config
= NULL
; // all configuration
67 int nlfd
= -1; // netlink socket
68 int tunfd
= -1; // tun interface file handle. (network device)
69 int udpfd
= -1; // UDP file handle
71 int udplacfd
= -1; // UDP LAC file handle
73 int controlfd
= -1; // Control signal handle
74 int clifd
= -1; // Socket listening for CLI connections.
75 int daefd
= -1; // Socket listening for DAE connections.
76 int snoopfd
= -1; // UDP file handle for sending out intercept data
77 int *radfds
= NULL
; // RADIUS requests file handles
78 int rand_fd
= -1; // Random data source
79 int cluster_sockfd
= -1; // Intra-cluster communications socket.
80 int epollfd
= -1; // event polling
81 time_t basetime
= 0; // base clock
82 char hostname
[MAXHOSTNAME
] = ""; // us.
83 static int tunidx
; // ifr_ifindex of tun device
84 int nlseqnum
= 0; // netlink sequence number
85 int min_initok_nlseqnum
= 0; // minimun seq number for messages after init is ok
86 static int syslog_log
= 0; // are we logging to syslog
87 static FILE *log_stream
= 0; // file handle for direct logging (i.e. direct into file, not via syslog).
88 uint32_t last_id
= 0; // Unique ID for radius accounting
90 char guest_users
[10][32]; // Array of guest users
91 int guest_accounts_num
= 0; // Number of guest users
93 // calculated from config->l2tp_mtu
94 uint16_t MRU
= 0; // PPP MRU
95 uint16_t MSS
= 0; // TCP MSS
97 struct cli_session_actions
*cli_session_actions
= NULL
; // Pending session changes requested by CLI
98 struct cli_tunnel_actions
*cli_tunnel_actions
= NULL
; // Pending tunnel changes required by CLI
103 } ip_hash
[256]; // Mapping from IP address to session structures.
107 struct ipv6radix
*branch
;
108 } ipv6_hash
[256]; // Mapping from IPv6 address to session structures.
111 static uint32_t udp_rx
= 0, udp_rx_pkt
= 0, udp_tx
= 0;
112 static uint32_t eth_rx
= 0, eth_rx_pkt
= 0;
115 static uint32_t ip_pool_size
= 1; // Size of the pool of addresses used for dynamic address allocation.
116 time_t time_now
= 0; // Current time in seconds since epoch.
117 uint64_t time_now_ms
= 0; // Current time in milliseconds since epoch.
118 static char time_now_string
[64] = {0}; // Current time as a string.
119 static int time_changed
= 0; // time_now changed
120 char main_quit
= 0; // True if we're in the process of exiting.
121 static char main_reload
= 0; // Re-load pending
122 linked_list
*loaded_plugins
;
123 linked_list
*plugins
[MAX_PLUGIN_TYPES
];
125 #define membersize(STRUCT, MEMBER) sizeof(((STRUCT *)0)->MEMBER)
126 #define CONFIG(NAME, MEMBER, TYPE) { NAME, offsetof(configt, MEMBER), membersize(configt, MEMBER), TYPE }
128 config_descriptt config_values
[] = {
129 CONFIG("debug", debug
, INT
),
130 CONFIG("log_file", log_filename
, STRING
),
131 CONFIG("pid_file", pid_file
, STRING
),
132 CONFIG("random_device", random_device
, STRING
),
133 CONFIG("l2tp_secret", l2tp_secret
, STRING
),
134 CONFIG("l2tp_mtu", l2tp_mtu
, INT
),
135 CONFIG("ppp_restart_time", ppp_restart_time
, INT
),
136 CONFIG("ppp_max_configure", ppp_max_configure
, INT
),
137 CONFIG("ppp_max_failure", ppp_max_failure
, INT
),
138 CONFIG("primary_dns", default_dns1
, IPv4
),
139 CONFIG("secondary_dns", default_dns2
, IPv4
),
140 CONFIG("primary_radius", radiusserver
[0], IPv4
),
141 CONFIG("secondary_radius", radiusserver
[1], IPv4
),
142 CONFIG("primary_radius_port", radiusport
[0], SHORT
),
143 CONFIG("secondary_radius_port", radiusport
[1], SHORT
),
144 CONFIG("radius_accounting", radius_accounting
, BOOL
),
145 CONFIG("radius_interim", radius_interim
, INT
),
146 CONFIG("radius_secret", radiussecret
, STRING
),
147 CONFIG("radius_authtypes", radius_authtypes_s
, STRING
),
148 CONFIG("radius_dae_port", radius_dae_port
, SHORT
),
149 CONFIG("radius_bind_min", radius_bind_min
, SHORT
),
150 CONFIG("radius_bind_max", radius_bind_max
, SHORT
),
151 CONFIG("allow_duplicate_users", allow_duplicate_users
, BOOL
),
152 CONFIG("kill_timedout_sessions", kill_timedout_sessions
, BOOL
),
153 CONFIG("guest_account", guest_user
, STRING
),
154 CONFIG("bind_address", bind_address
, IPv4
),
155 CONFIG("peer_address", peer_address
, IPv4
),
156 CONFIG("send_garp", send_garp
, BOOL
),
157 CONFIG("throttle_speed", rl_rate
, UNSIGNED_LONG
),
158 CONFIG("throttle_buckets", num_tbfs
, INT
),
159 CONFIG("accounting_dir", accounting_dir
, STRING
),
160 CONFIG("dump_speed", dump_speed
, BOOL
),
161 CONFIG("multi_read_count", multi_read_count
, INT
),
162 CONFIG("scheduler_fifo", scheduler_fifo
, BOOL
),
163 CONFIG("lock_pages", lock_pages
, BOOL
),
164 CONFIG("icmp_rate", icmp_rate
, INT
),
165 CONFIG("packet_limit", max_packets
, INT
),
166 CONFIG("cluster_address", cluster_address
, IPv4
),
167 CONFIG("cluster_interface", cluster_interface
, STRING
),
168 CONFIG("cluster_mcast_ttl", cluster_mcast_ttl
, INT
),
169 CONFIG("cluster_hb_interval", cluster_hb_interval
, INT
),
170 CONFIG("cluster_hb_timeout", cluster_hb_timeout
, INT
),
171 CONFIG("cluster_master_min_adv", cluster_master_min_adv
, INT
),
172 CONFIG("ipv6_prefix", ipv6_prefix
, IPv6
),
173 CONFIG("cli_bind_address", cli_bind_address
, IPv4
),
174 CONFIG("hostname", hostname
, STRING
),
176 CONFIG("nexthop_address", nexthop_address
, IPv4
),
177 CONFIG("nexthop6_address", nexthop6_address
, IPv6
),
179 CONFIG("echo_timeout", echo_timeout
, INT
),
180 CONFIG("idle_echo_timeout", idle_echo_timeout
, INT
),
181 CONFIG("iftun_address", iftun_address
, IPv4
),
183 CONFIG("disable_lac_func", disable_lac_func
, BOOL
),
184 CONFIG("bind_address_remotelns", bind_address_remotelns
, IPv4
),
185 CONFIG("bind_portremotelns", bind_portremotelns
, SHORT
),
190 static char *plugin_functions
[] = {
195 "plugin_new_session",
196 "plugin_kill_session",
198 "plugin_radius_response",
199 "plugin_radius_reset",
200 "plugin_radius_account",
201 "plugin_become_master",
202 "plugin_new_session_master",
205 #define max_plugin_functions (sizeof(plugin_functions) / sizeof(char *))
207 // Counters for shutdown sessions
208 static sessiont shut_acct
[8192];
209 static sessionidt shut_acct_n
= 0;
211 tunnelt
*tunnel
= NULL
; // Array of tunnel structures.
212 bundlet
*bundle
= NULL
; // Array of bundle structures.
213 fragmentationt
*frag
= NULL
; // Array of fragmentation structures.
214 sessiont
*session
= NULL
; // Array of session structures.
215 sessionlocalt
*sess_local
= NULL
; // Array of local per-session counters.
216 radiust
*radius
= NULL
; // Array of radius structures.
217 ippoolt
*ip_address_pool
= NULL
; // Array of dynamic IP addresses.
218 ip_filtert
*ip_filters
= NULL
; // Array of named filters.
219 static controlt
*controlfree
= 0;
220 struct Tstats
*_statistics
= NULL
;
222 struct Tringbuffer
*ringbuffer
= NULL
;
225 static ssize_t
netlink_send(struct nlmsghdr
*nh
);
226 static void netlink_addattr(struct nlmsghdr
*nh
, int type
, const void *data
, int alen
);
227 static void cache_ipmap(in_addr_t ip
, sessionidt s
);
228 static void uncache_ipmap(in_addr_t ip
);
229 static void cache_ipv6map(struct in6_addr ip
, int prefixlen
, sessionidt s
);
230 static void free_ip_address(sessionidt s
);
231 static void dump_acct_info(int all
);
232 static void sighup_handler(int sig
);
233 static void shutdown_handler(int sig
);
234 static void sigchild_handler(int sig
);
235 static void build_chap_response(uint8_t *challenge
, uint8_t id
, uint16_t challenge_length
, uint8_t **challenge_response
);
236 static void update_config(void);
237 static void read_config_file(void);
238 static void initplugins(void);
239 static int add_plugin(char *plugin_name
);
240 static int remove_plugin(char *plugin_name
);
241 static void plugins_done(void);
242 static void processcontrol(uint8_t *buf
, int len
, struct sockaddr_in
*addr
, int alen
, struct in_addr
*local
);
243 static tunnelidt
new_tunnel(void);
244 static void unhide_value(uint8_t *value
, size_t len
, uint16_t type
, uint8_t *vector
, size_t vec_len
);
245 static void bundleclear(bundleidt b
);
247 // return internal time (10ths since process startup), set f if given
248 // as a side-effect sets time_now, and time_changed
249 static clockt
now(double *f
)
253 if (f
) *f
= t
.tv_sec
+ t
.tv_usec
/ 1000000.0;
254 if (t
.tv_sec
!= time_now
)
260 // Time in milliseconds
261 // TODO FOR MLPPP DEV
262 //time_now_ms = (t.tv_sec * 1000) + (t.tv_usec/1000);
264 return (t
.tv_sec
- basetime
) * 10 + t
.tv_usec
/ 100000 + 1;
267 // work out a retry time based on try number
268 // This is a straight bounded exponential backoff.
269 // Maximum re-try time is 32 seconds. (2^5).
270 clockt
backoff(uint8_t try)
272 if (try > 5) try = 5; // max backoff
273 return now(NULL
) + 10 * (1 << try);
278 // Log a debug message. Typically called via the LOG macro
280 void _log(int level
, sessionidt s
, tunnelidt t
, const char *format
, ...)
282 static char message
[65536] = {0};
288 if (++ringbuffer
->tail
>= RINGBUFFER_SIZE
)
289 ringbuffer
->tail
= 0;
290 if (ringbuffer
->tail
== ringbuffer
->head
)
291 if (++ringbuffer
->head
>= RINGBUFFER_SIZE
)
292 ringbuffer
->head
= 0;
294 ringbuffer
->buffer
[ringbuffer
->tail
].level
= level
;
295 ringbuffer
->buffer
[ringbuffer
->tail
].session
= s
;
296 ringbuffer
->buffer
[ringbuffer
->tail
].tunnel
= t
;
297 va_start(ap
, format
);
298 vsnprintf(ringbuffer
->buffer
[ringbuffer
->tail
].message
, MAX_LOG_LENGTH
, format
, ap
);
303 if (config
->debug
< level
) return;
305 va_start(ap
, format
);
306 vsnprintf(message
, sizeof(message
), format
, ap
);
309 fprintf(log_stream
, "%s %02d/%02d %s", time_now_string
, t
, s
, message
);
311 syslog(level
+ 2, "%02d/%02d %s", t
, s
, message
); // We don't need LOG_EMERG or LOG_ALERT
316 void _log_hex(int level
, const char *title
, const uint8_t *data
, int maxsize
)
319 const uint8_t *d
= data
;
321 if (config
->debug
< level
) return;
323 // No support for _log_hex to syslog
326 _log(level
, 0, 0, "%s (%d bytes):\n", title
, maxsize
);
327 setvbuf(log_stream
, NULL
, _IOFBF
, 16384);
329 for (i
= 0; i
< maxsize
; )
331 fprintf(log_stream
, "%4X: ", i
);
332 for (j
= i
; j
< maxsize
&& j
< (i
+ 16); j
++)
334 fprintf(log_stream
, "%02X ", d
[j
]);
336 fputs(": ", log_stream
);
339 for (; j
< i
+ 16; j
++)
341 fputs(" ", log_stream
);
343 fputs(": ", log_stream
);
346 fputs(" ", log_stream
);
347 for (j
= i
; j
< maxsize
&& j
< (i
+ 16); j
++)
349 if (d
[j
] >= 0x20 && d
[j
] < 0x7f && d
[j
] != 0x20)
350 fputc(d
[j
], log_stream
);
352 fputc('.', log_stream
);
355 fputs(" ", log_stream
);
359 fputs("\n", log_stream
);
363 setbuf(log_stream
, NULL
);
367 // update a counter, accumulating 2^32 wraps
368 void increment_counter(uint32_t *counter
, uint32_t *wrap
, uint32_t delta
)
370 uint32_t new = *counter
+ delta
;
377 // initialise the random generator
378 static void initrandom(char *source
)
380 static char path
[sizeof(config
->random_device
)] = "*undefined*";
382 // reinitialise only if we are forced to do so or if the config has changed
383 if (source
&& !strncmp(path
, source
, sizeof(path
)))
386 // close previous source, if any
395 snprintf(path
, sizeof(path
), "%s", source
);
399 rand_fd
= open(path
, O_RDONLY
|O_NONBLOCK
);
401 LOG(0, 0, 0, "Error opening the random device %s: %s\n",
402 path
, strerror(errno
));
407 // fill buffer with random data
408 void random_data(uint8_t *buf
, int len
)
415 n
= read(rand_fd
, buf
, len
);
416 if (n
>= len
) return;
421 LOG(0, 0, 0, "Error reading from random source: %s\n",
424 // fall back to rand()
432 // append missing data
434 // not using the low order bits from the prng stream
435 buf
[n
++] = (rand() >> 4) & 0xff;
440 // This adds it to the routing table, advertises it
441 // via BGP if enabled, and stuffs it into the
442 // 'sessionbyip' cache.
444 // 'ip' must be in _host_ order.
446 static void routeset(sessionidt s
, in_addr_t ip
, int prefixlen
, in_addr_t gw
, int add
)
456 if (!prefixlen
) prefixlen
= 32;
458 ip
&= 0xffffffff << (32 - prefixlen
);; // Force the ip to be the first one in the route.
460 memset(&req
, 0, sizeof(req
));
464 req
.nh
.nlmsg_type
= RTM_NEWROUTE
;
465 req
.nh
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_REPLACE
;
469 req
.nh
.nlmsg_type
= RTM_DELROUTE
;
470 req
.nh
.nlmsg_flags
= NLM_F_REQUEST
;
473 req
.nh
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.rt
));
475 req
.rt
.rtm_family
= AF_INET
;
476 req
.rt
.rtm_dst_len
= prefixlen
;
477 req
.rt
.rtm_table
= RT_TABLE_MAIN
;
478 req
.rt
.rtm_protocol
= 42;
479 req
.rt
.rtm_scope
= RT_SCOPE_LINK
;
480 req
.rt
.rtm_type
= RTN_UNICAST
;
482 netlink_addattr(&req
.nh
, RTA_OIF
, &tunidx
, sizeof(int));
484 netlink_addattr(&req
.nh
, RTA_DST
, &n_ip
, sizeof(n_ip
));
488 netlink_addattr(&req
.nh
, RTA_GATEWAY
, &n_ip
, sizeof(n_ip
));
491 LOG(1, s
, session
[s
].tunnel
, "Route %s %s/%d%s%s\n", add
? "add" : "del",
492 fmtaddr(htonl(ip
), 0), prefixlen
,
493 gw
? " via" : "", gw
? fmtaddr(htonl(gw
), 2) : "");
495 if (netlink_send(&req
.nh
) < 0)
496 LOG(0, 0, 0, "routeset() error in sending netlink message: %s\n", strerror(errno
));
500 bgp_add_route(htonl(ip
), prefixlen
);
502 bgp_del_route(htonl(ip
), prefixlen
);
505 // Add/Remove the IPs to the 'sessionbyip' cache.
506 // Note that we add the zero address in the case of
507 // a network route. Roll on CIDR.
509 // Note that 's == 0' implies this is the address pool.
510 // We still cache it here, because it will pre-fill
511 // the malloc'ed tree.
515 if (!add
) // Are we deleting a route?
516 s
= 0; // Caching the session as '0' is the same as uncaching.
518 for (i
= ip
; i
< ip
+(1<<(32-prefixlen
)) ; ++i
)
523 void route6set(sessionidt s
, struct in6_addr ip
, int prefixlen
, int add
)
531 char ipv6addr
[INET6_ADDRSTRLEN
];
533 if (!config
->ipv6_prefix
.s6_addr
[0])
535 LOG(0, 0, 0, "Asked to set IPv6 route, but IPv6 not setup.\n");
539 memset(&req
, 0, sizeof(req
));
543 req
.nh
.nlmsg_type
= RTM_NEWROUTE
;
544 req
.nh
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_REPLACE
;
548 req
.nh
.nlmsg_type
= RTM_DELROUTE
;
549 req
.nh
.nlmsg_flags
= NLM_F_REQUEST
;
552 req
.nh
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.rt
));
554 req
.rt
.rtm_family
= AF_INET6
;
555 req
.rt
.rtm_dst_len
= prefixlen
;
556 req
.rt
.rtm_table
= RT_TABLE_MAIN
;
557 req
.rt
.rtm_protocol
= 42;
558 req
.rt
.rtm_scope
= RT_SCOPE_LINK
;
559 req
.rt
.rtm_type
= RTN_UNICAST
;
561 netlink_addattr(&req
.nh
, RTA_OIF
, &tunidx
, sizeof(int));
562 netlink_addattr(&req
.nh
, RTA_DST
, &ip
, sizeof(ip
));
564 netlink_addattr(&req
.nh
, RTA_METRICS
, &metric
, sizeof(metric
));
566 LOG(1, s
, session
[s
].tunnel
, "Route %s %s/%d\n",
568 inet_ntop(AF_INET6
, &ip
, ipv6addr
, INET6_ADDRSTRLEN
),
571 if (netlink_send(&req
.nh
) < 0)
572 LOG(0, 0, 0, "route6set() error in sending netlink message: %s\n", strerror(errno
));
576 bgp_add_route6(ip
, prefixlen
);
578 bgp_del_route6(ip
, prefixlen
);
583 if (!add
) // Are we deleting a route?
584 s
= 0; // Caching the session as '0' is the same as uncaching.
586 cache_ipv6map(ip
, prefixlen
, s
);
593 // Set up netlink socket
594 static void initnetlink(void)
596 struct sockaddr_nl nladdr
;
598 nlfd
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
601 LOG(0, 0, 0, "Can't create netlink socket: %s\n", strerror(errno
));
605 memset(&nladdr
, 0, sizeof(nladdr
));
606 nladdr
.nl_family
= AF_NETLINK
;
607 nladdr
.nl_pid
= getpid();
609 if (bind(nlfd
, (struct sockaddr
*)&nladdr
, sizeof(nladdr
)) < 0)
611 LOG(0, 0, 0, "Can't bind netlink socket: %s\n", strerror(errno
));
616 static ssize_t
netlink_send(struct nlmsghdr
*nh
)
618 struct sockaddr_nl nladdr
;
622 nh
->nlmsg_pid
= getpid();
623 nh
->nlmsg_seq
= ++nlseqnum
;
625 // set kernel address
626 memset(&nladdr
, 0, sizeof(nladdr
));
627 nladdr
.nl_family
= AF_NETLINK
;
629 iov
= (struct iovec
){ (void *)nh
, nh
->nlmsg_len
};
630 msg
= (struct msghdr
){ (void *)&nladdr
, sizeof(nladdr
), &iov
, 1, NULL
, 0, 0 };
632 return sendmsg(nlfd
, &msg
, 0);
635 static ssize_t
netlink_recv(void *buf
, ssize_t len
)
637 struct sockaddr_nl nladdr
;
641 // set kernel address
642 memset(&nladdr
, 0, sizeof(nladdr
));
643 nladdr
.nl_family
= AF_NETLINK
;
645 iov
= (struct iovec
){ buf
, len
};
646 msg
= (struct msghdr
){ (void *)&nladdr
, sizeof(nladdr
), &iov
, 1, NULL
, 0, 0 };
648 return recvmsg(nlfd
, &msg
, 0);
651 /* adapted from iproute2 */
652 static void netlink_addattr(struct nlmsghdr
*nh
, int type
, const void *data
, int alen
)
654 int len
= RTA_LENGTH(alen
);
657 rta
= (struct rtattr
*)(((void *)nh
) + NLMSG_ALIGN(nh
->nlmsg_len
));
658 rta
->rta_type
= type
;
660 memcpy(RTA_DATA(rta
), data
, alen
);
661 nh
->nlmsg_len
= NLMSG_ALIGN(nh
->nlmsg_len
) + RTA_ALIGN(len
);
664 // messages corresponding to different phases seq number
665 static char *tun_nl_phase_msg
[] = {
667 "getting tun interface index",
668 "setting tun interface parameters",
669 "setting tun IPv4 address",
670 "setting tun LL IPv6 address",
671 "setting tun global IPv6 address",
675 // Set up TUN interface
676 static void inittun(void)
680 memset(&ifr
, 0, sizeof(ifr
));
681 ifr
.ifr_flags
= IFF_TUN
;
683 tunfd
= open(TUNDEVICE
, O_RDWR
);
686 LOG(0, 0, 0, "Can't open %s: %s\n", TUNDEVICE
, strerror(errno
));
690 int flags
= fcntl(tunfd
, F_GETFL
, 0);
691 fcntl(tunfd
, F_SETFL
, flags
| O_NONBLOCK
);
693 if (ioctl(tunfd
, TUNSETIFF
, (void *) &ifr
) < 0)
695 LOG(0, 0, 0, "Can't set tun interface: %s\n", strerror(errno
));
698 assert(strlen(ifr
.ifr_name
) < sizeof(config
->tundevice
) - 1);
699 strncpy(config
->tundevice
, ifr
.ifr_name
, sizeof(config
->tundevice
));
701 tunidx
= if_nametoindex(config
->tundevice
);
704 LOG(0, 0, 0, "Can't get tun interface index\n");
713 struct ifinfomsg ifinfo
;
714 struct ifaddrmsg ifaddr
;
716 char rtdata
[32]; // 32 should be enough
718 uint32_t txqlen
, mtu
;
721 memset(&req
, 0, sizeof(req
));
723 req
.nh
.nlmsg_type
= RTM_NEWLINK
;
724 req
.nh
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_MULTI
;
725 req
.nh
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.ifmsg
.ifinfo
));
727 req
.ifmsg
.ifinfo
.ifi_family
= AF_UNSPEC
;
728 req
.ifmsg
.ifinfo
.ifi_index
= tunidx
;
729 req
.ifmsg
.ifinfo
.ifi_flags
|= IFF_UP
; // set interface up
730 req
.ifmsg
.ifinfo
.ifi_change
= IFF_UP
; // only change this flag
732 /* Bump up the qlen to deal with bursts from the network */
734 netlink_addattr(&req
.nh
, IFLA_TXQLEN
, &txqlen
, sizeof(txqlen
));
735 /* set MTU to modem MRU */
737 netlink_addattr(&req
.nh
, IFLA_MTU
, &mtu
, sizeof(mtu
));
739 if (netlink_send(&req
.nh
) < 0)
742 memset(&req
, 0, sizeof(req
));
744 req
.nh
.nlmsg_type
= RTM_NEWADDR
;
745 req
.nh
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_REPLACE
| NLM_F_MULTI
;
746 req
.nh
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.ifmsg
.ifaddr
));
748 req
.ifmsg
.ifaddr
.ifa_family
= AF_INET
;
749 req
.ifmsg
.ifaddr
.ifa_prefixlen
= 32;
750 req
.ifmsg
.ifaddr
.ifa_scope
= RT_SCOPE_UNIVERSE
;
751 req
.ifmsg
.ifaddr
.ifa_index
= tunidx
;
753 if (config
->iftun_address
)
754 ip
= config
->iftun_address
;
756 ip
= 0x01010101; // 1.1.1.1
757 netlink_addattr(&req
.nh
, IFA_LOCAL
, &ip
, sizeof(ip
));
759 if (netlink_send(&req
.nh
) < 0)
762 // Only setup IPv6 on the tun device if we have a configured prefix
763 if (config
->ipv6_prefix
.s6_addr
[0]) {
766 memset(&req
, 0, sizeof(req
));
768 req
.nh
.nlmsg_type
= RTM_NEWADDR
;
769 req
.nh
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_REPLACE
| NLM_F_MULTI
;
770 req
.nh
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.ifmsg
.ifaddr
));
772 req
.ifmsg
.ifaddr
.ifa_family
= AF_INET6
;
773 req
.ifmsg
.ifaddr
.ifa_prefixlen
= 64;
774 req
.ifmsg
.ifaddr
.ifa_scope
= RT_SCOPE_LINK
;
775 req
.ifmsg
.ifaddr
.ifa_index
= tunidx
;
777 // Link local address is FE80::1
778 memset(&ip6
, 0, sizeof(ip6
));
779 ip6
.s6_addr
[0] = 0xFE;
780 ip6
.s6_addr
[1] = 0x80;
782 netlink_addattr(&req
.nh
, IFA_LOCAL
, &ip6
, sizeof(ip6
));
784 if (netlink_send(&req
.nh
) < 0)
787 memset(&req
, 0, sizeof(req
));
789 req
.nh
.nlmsg_type
= RTM_NEWADDR
;
790 req
.nh
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_REPLACE
| NLM_F_MULTI
;
791 req
.nh
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.ifmsg
.ifaddr
));
793 req
.ifmsg
.ifaddr
.ifa_family
= AF_INET6
;
794 req
.ifmsg
.ifaddr
.ifa_prefixlen
= 64;
795 req
.ifmsg
.ifaddr
.ifa_scope
= RT_SCOPE_UNIVERSE
;
796 req
.ifmsg
.ifaddr
.ifa_index
= tunidx
;
798 // Global address is prefix::1
799 ip6
= config
->ipv6_prefix
;
801 netlink_addattr(&req
.nh
, IFA_LOCAL
, &ip6
, sizeof(ip6
));
803 if (netlink_send(&req
.nh
) < 0)
807 memset(&req
, 0, sizeof(req
));
809 req
.nh
.nlmsg_type
= NLMSG_DONE
;
810 req
.nh
.nlmsg_len
= NLMSG_LENGTH(0);
812 if (netlink_send(&req
.nh
) < 0)
815 // if we get an error for seqnum < min_initok_nlseqnum,
816 // we must exit as initialization went wrong
817 if (config
->ipv6_prefix
.s6_addr
[0])
818 min_initok_nlseqnum
= 5 + 1; // idx + if + addr + 2*addr6
820 min_initok_nlseqnum
= 3 + 1; // idx + if + addr
826 LOG(0, 0, 0, "Error while setting up tun device: %s\n", strerror(errno
));
831 static void initudp(void)
834 struct sockaddr_in addr
;
837 memset(&addr
, 0, sizeof(addr
));
838 addr
.sin_family
= AF_INET
;
839 addr
.sin_port
= htons(L2TPPORT
);
840 addr
.sin_addr
.s_addr
= config
->bind_address
;
841 udpfd
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
842 setsockopt(udpfd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
844 int flags
= fcntl(udpfd
, F_GETFL
, 0);
845 fcntl(udpfd
, F_SETFL
, flags
| O_NONBLOCK
);
847 if (bind(udpfd
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0)
849 LOG(0, 0, 0, "Error in UDP bind: %s\n", strerror(errno
));
854 memset(&addr
, 0, sizeof(addr
));
855 addr
.sin_family
= AF_INET
;
856 addr
.sin_port
= htons(NSCTL_PORT
);
857 controlfd
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
858 setsockopt(controlfd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
859 setsockopt(controlfd
, SOL_IP
, IP_PKTINFO
, &on
, sizeof(on
)); // recvfromto
860 if (bind(controlfd
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0)
862 LOG(0, 0, 0, "Error in control bind: %s\n", strerror(errno
));
866 // Dynamic Authorization Extensions to RADIUS
867 memset(&addr
, 0, sizeof(addr
));
868 addr
.sin_family
= AF_INET
;
869 addr
.sin_port
= htons(config
->radius_dae_port
);
870 daefd
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
871 setsockopt(daefd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
872 setsockopt(daefd
, SOL_IP
, IP_PKTINFO
, &on
, sizeof(on
)); // recvfromto
873 if (bind(daefd
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0)
875 LOG(0, 0, 0, "Error in DAE bind: %s\n", strerror(errno
));
880 // Tunnel to Remote LNS
881 memset(&addr
, 0, sizeof(addr
));
882 addr
.sin_family
= AF_INET
;
883 addr
.sin_port
= htons(config
->bind_portremotelns
);
884 addr
.sin_addr
.s_addr
= config
->bind_address_remotelns
;
885 udplacfd
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
886 setsockopt(udplacfd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
888 int flags
= fcntl(udplacfd
, F_GETFL
, 0);
889 fcntl(udplacfd
, F_SETFL
, flags
| O_NONBLOCK
);
891 if (bind(udplacfd
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0)
893 LOG(0, 0, 0, "Error in UDP REMOTE LNS bind: %s\n", strerror(errno
));
899 snoopfd
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
903 // Find session by IP, < 1 for not found
905 // Confusingly enough, this 'ip' must be
906 // in _network_ order. This being the common
907 // case when looking it up from IP packet headers.
909 // We actually use this cache for two things.
910 // #1. For used IP addresses, this maps to the
911 // session ID that it's used by.
912 // #2. For un-used IP addresses, this maps to the
913 // index into the pool table that contains that
917 static sessionidt
lookup_ipmap(in_addr_t ip
)
919 uint8_t *a
= (uint8_t *) &ip
;
920 union iphash
*h
= ip_hash
;
922 if (!(h
= h
[*a
++].idx
)) return 0;
923 if (!(h
= h
[*a
++].idx
)) return 0;
924 if (!(h
= h
[*a
++].idx
)) return 0;
929 static sessionidt
lookup_ipv6map(struct in6_addr ip
)
931 struct ipv6radix
*curnode
;
934 char ipv6addr
[INET6_ADDRSTRLEN
];
936 curnode
= &ipv6_hash
[ip
.s6_addr
[0]];
940 while (s
== 0 && i
< 15 && curnode
->branch
!= NULL
)
942 curnode
= &curnode
->branch
[ip
.s6_addr
[i
]];
947 LOG(4, s
, session
[s
].tunnel
, "Looking up address %s and got %d\n",
948 inet_ntop(AF_INET6
, &ip
, ipv6addr
,
955 sessionidt
sessionbyip(in_addr_t ip
)
957 sessionidt s
= lookup_ipmap(ip
);
960 if (s
> 0 && s
< MAXSESSION
&& session
[s
].opened
)
966 sessionidt
sessionbyipv6(struct in6_addr ip
)
969 CSTAT(sessionbyipv6
);
971 if (!memcmp(&config
->ipv6_prefix
, &ip
, 8) ||
972 (ip
.s6_addr
[0] == 0xFE &&
973 ip
.s6_addr
[1] == 0x80 &&
974 ip
.s6_addr16
[1] == 0 &&
975 ip
.s6_addr16
[2] == 0 &&
976 ip
.s6_addr16
[3] == 0)) {
977 s
= lookup_ipmap(*(in_addr_t
*) &ip
.s6_addr
[8]);
979 s
= lookup_ipv6map(ip
);
982 if (s
> 0 && s
< MAXSESSION
&& session
[s
].opened
)
989 // Take an IP address in HOST byte order and
990 // add it to the sessionid by IP cache.
992 // (It's actually cached in network order)
994 static void cache_ipmap(in_addr_t ip
, sessionidt s
)
996 in_addr_t nip
= htonl(ip
); // MUST be in network order. I.e. MSB must in be ((char *) (&ip))[0]
997 uint8_t *a
= (uint8_t *) &nip
;
998 union iphash
*h
= ip_hash
;
1001 for (i
= 0; i
< 3; i
++)
1003 if (!(h
[a
[i
]].idx
|| (h
[a
[i
]].idx
= calloc(256, sizeof(union iphash
)))))
1012 LOG(4, s
, session
[s
].tunnel
, "Caching ip address %s\n", fmtaddr(nip
, 0));
1015 LOG(4, 0, 0, "Un-caching ip address %s\n", fmtaddr(nip
, 0));
1016 // else a map to an ip pool index.
1019 static void uncache_ipmap(in_addr_t ip
)
1021 cache_ipmap(ip
, 0); // Assign it to the NULL session.
1024 static void cache_ipv6map(struct in6_addr ip
, int prefixlen
, sessionidt s
)
1028 struct ipv6radix
*curnode
;
1029 char ipv6addr
[INET6_ADDRSTRLEN
];
1031 curnode
= &ipv6_hash
[ip
.s6_addr
[0]];
1033 bytes
= prefixlen
>> 3;
1036 if (curnode
->branch
== NULL
)
1038 if (!(curnode
->branch
= calloc(256,
1039 sizeof (struct ipv6radix
))))
1042 curnode
= &curnode
->branch
[ip
.s6_addr
[i
]];
1049 LOG(4, s
, session
[s
].tunnel
, "Caching ip address %s/%d\n",
1050 inet_ntop(AF_INET6
, &ip
, ipv6addr
,
1054 LOG(4, 0, 0, "Un-caching ip address %s/%d\n",
1055 inet_ntop(AF_INET6
, &ip
, ipv6addr
,
1061 // CLI list to dump current ipcache.
1063 int cmd_show_ipcache(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1065 union iphash
*d
= ip_hash
, *e
, *f
, *g
;
1069 if (CLI_HELP_REQUESTED
)
1070 return CLI_HELP_NO_ARGS
;
1072 cli_print(cli
, "%7s %s", "Sess#", "IP Address");
1074 for (i
= 0; i
< 256; ++i
)
1080 for (j
= 0; j
< 256; ++j
)
1086 for (k
= 0; k
< 256; ++k
)
1092 for (l
= 0; l
< 256; ++l
)
1097 cli_print(cli
, "%7d %d.%d.%d.%d", g
[l
].sess
, i
, j
, k
, l
);
1103 cli_print(cli
, "%d entries in cache", count
);
1108 // Find session by username, 0 for not found
1109 // walled garden users aren't authenticated, so the username is
1110 // reasonably useless. Ignore them to avoid incorrect actions
1112 // This is VERY inefficent. Don't call it often. :)
1114 sessionidt
sessionbyuser(char *username
)
1117 CSTAT(sessionbyuser
);
1119 for (s
= 1; s
<= config
->cluster_highest_sessionid
; ++s
)
1121 if (!session
[s
].opened
)
1124 if (session
[s
].walled_garden
)
1125 continue; // Skip walled garden users.
1127 if (!strncmp(session
[s
].user
, username
, 128))
1131 return 0; // Not found.
1134 void send_garp(in_addr_t ip
)
1140 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
1143 LOG(0, 0, 0, "Error creating socket for GARP: %s\n", strerror(errno
));
1146 memset(&ifr
, 0, sizeof(ifr
));
1147 strncpy(ifr
.ifr_name
, "eth0", sizeof(ifr
.ifr_name
) - 1);
1148 if (ioctl(s
, SIOCGIFHWADDR
, &ifr
) < 0)
1150 LOG(0, 0, 0, "Error getting eth0 hardware address for GARP: %s\n", strerror(errno
));
1154 memcpy(mac
, &ifr
.ifr_hwaddr
.sa_data
, 6*sizeof(char));
1155 if (ioctl(s
, SIOCGIFINDEX
, &ifr
) < 0)
1157 LOG(0, 0, 0, "Error getting eth0 interface index for GARP: %s\n", strerror(errno
));
1162 sendarp(ifr
.ifr_ifindex
, mac
, ip
);
1165 static sessiont
*sessiontbysessionidt(sessionidt s
)
1167 if (!s
|| s
>= MAXSESSION
) return NULL
;
1171 static sessionidt
sessionidtbysessiont(sessiont
*s
)
1173 sessionidt val
= s
-session
;
1174 if (s
< session
|| val
>= MAXSESSION
) return 0;
1178 // actually send a control message for a specific tunnel
1179 void tunnelsend(uint8_t * buf
, uint16_t l
, tunnelidt t
)
1181 struct sockaddr_in addr
;
1187 LOG(0, 0, t
, "tunnelsend called with 0 as tunnel id\n");
1188 STAT(tunnel_tx_errors
);
1194 LOG(1, 0, t
, "Error sending data out tunnel: no remote endpoint (tunnel not set up)\n");
1195 STAT(tunnel_tx_errors
);
1199 memset(&addr
, 0, sizeof(addr
));
1200 addr
.sin_family
= AF_INET
;
1201 *(uint32_t *) & addr
.sin_addr
= htonl(tunnel
[t
].ip
);
1202 addr
.sin_port
= htons(tunnel
[t
].port
);
1204 // sequence expected, if sequence in message
1205 if (*buf
& 0x08) *(uint16_t *) (buf
+ ((*buf
& 0x40) ? 10 : 8)) = htons(tunnel
[t
].nr
);
1207 // If this is a control message, deal with retries
1210 tunnel
[t
].last
= time_now
; // control message sent
1211 tunnel
[t
].retry
= backoff(tunnel
[t
].try); // when to resend
1214 STAT(tunnel_retries
);
1215 LOG(3, 0, t
, "Control message resend try %d\n", tunnel
[t
].try);
1219 if (sendto((tunnel
[t
].isremotelns
?udplacfd
:udpfd
), buf
, l
, 0, (void *) &addr
, sizeof(addr
)) < 0)
1221 if (sendto(udpfd
, buf
, l
, 0, (void *) &addr
, sizeof(addr
)) < 0)
1224 LOG(0, ntohs((*(uint16_t *) (buf
+ 6))), t
, "Error sending data out tunnel: %s (udpfd=%d, buf=%p, len=%d, dest=%s)\n",
1225 strerror(errno
), udpfd
, buf
, l
, inet_ntoa(addr
.sin_addr
));
1226 STAT(tunnel_tx_errors
);
1230 LOG_HEX(5, "Send Tunnel Data", buf
, l
);
1231 STAT(tunnel_tx_packets
);
1232 INC_STAT(tunnel_tx_bytes
, l
);
1236 // Tiny helper function to write data to
1237 // the 'tun' device.
1239 int tun_write(uint8_t * data
, int size
)
1241 return write(tunfd
, data
, size
);
1244 // adjust tcp mss to avoid fragmentation (called only for tcp packets with syn set)
1245 void adjust_tcp_mss(sessionidt s
, tunnelidt t
, uint8_t *buf
, int len
, uint8_t *tcp
)
1247 int d
= (tcp
[12] >> 4) * 4;
1254 if ((tcp
[13] & 0x3f) & ~(TCP_FLAG_SYN
|TCP_FLAG_ACK
)) // only want SYN and SYN,ACK
1257 if (tcp
+ d
> buf
+ len
) // short?
1265 if (*opts
== 2 && opts
[1] == 4) // mss option (2), length 4
1268 if (mss
+ 2 > data
) return; // short?
1272 if (*opts
== 0) return; // end of options
1273 if (*opts
== 1 || !opts
[1]) // no op (one byte), or no length (prevent loop)
1276 opts
+= opts
[1]; // skip over option
1279 if (!mss
) return; // not found
1280 orig
= ntohs(*(uint16_t *) mss
);
1282 if (orig
<= MSS
) return; // mss OK
1284 LOG(5, s
, t
, "TCP: %s:%u -> %s:%u SYN%s: adjusted mss from %u to %u\n",
1285 fmtaddr(*(in_addr_t
*) (buf
+ 12), 0), ntohs(*(uint16_t *) tcp
),
1286 fmtaddr(*(in_addr_t
*) (buf
+ 16), 1), ntohs(*(uint16_t *) (tcp
+ 2)),
1287 (tcp
[13] & TCP_FLAG_ACK
) ? ",ACK" : "", orig
, MSS
);
1290 *(int16_t *) mss
= htons(MSS
);
1292 // adjust checksum (see rfc1141)
1293 sum
= orig
+ (~MSS
& 0xffff);
1294 sum
+= ntohs(*(uint16_t *) (tcp
+ 16));
1295 sum
= (sum
& 0xffff) + (sum
>> 16);
1296 *(uint16_t *) (tcp
+ 16) = htons(sum
+ (sum
>> 16));
1299 void processmpframe(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
, uint8_t extra
)
1303 // Skip the four extra bytes
1315 proto
= ntohs(*(uint16_t *) p
);
1323 LOG(4, s
, t
, "MPPP: Session %d is closing. Don't process PPP packets\n", s
);
1324 return; // closing session, PPP not processed
1326 session
[s
].last_packet
= session
[s
].last_data
= time_now
;
1327 processipin(s
, t
, p
, l
);
1329 else if (proto
== PPPIPV6
&& config
->ipv6_prefix
.s6_addr
[0])
1333 LOG(4, s
, t
, "MPPP: Session %d is closing. Don't process PPP packets\n", s
);
1334 return; // closing session, PPP not processed
1337 session
[s
].last_packet
= session
[s
].last_data
= time_now
;
1338 processipv6in(s
, t
, p
, l
);
1340 else if (proto
== PPPIPCP
)
1342 session
[s
].last_packet
= session
[s
].last_data
= time_now
;
1343 processipcp(s
, t
, p
, l
);
1345 else if (proto
== PPPCCP
)
1347 session
[s
].last_packet
= session
[s
].last_data
= time_now
;
1348 processccp(s
, t
, p
, l
);
1352 LOG(2, s
, t
, "MPPP: Unsupported MP protocol 0x%04X received\n",proto
);
1356 static void update_session_out_stat(sessionidt s
, sessiont
*sp
, int len
)
1358 increment_counter(&sp
->cout
, &sp
->cout_wrap
, len
); // byte count
1359 sp
->cout_delta
+= len
;
1361 sp
->last_data
= time_now
;
1363 sess_local
[s
].cout
+= len
; // To send to master..
1364 sess_local
[s
].pout
++;
1367 // process outgoing (to tunnel) IP
1369 // (i.e. this routine writes to data[-8]).
1370 void processipout(uint8_t *buf
, int len
)
1377 uint8_t *data
= buf
; // Keep a copy of the originals.
1380 uint8_t fragbuf
[MAXETHER
+ 20];
1382 CSTAT(processipout
);
1384 if (len
< MIN_IP_SIZE
)
1386 LOG(1, 0, 0, "Short IP, %d bytes\n", len
);
1387 STAT(tun_rx_errors
);
1390 if (len
>= MAXETHER
)
1392 LOG(1, 0, 0, "Oversize IP packet %d bytes\n", len
);
1393 STAT(tun_rx_errors
);
1397 // Skip the tun header
1401 // Got an IP header now
1402 if (*(uint8_t *)(buf
) >> 4 != 4)
1404 LOG(1, 0, 0, "IP: Don't understand anything except IPv4\n");
1408 ip
= *(uint32_t *)(buf
+ 16);
1409 if (!(s
= sessionbyip(ip
)))
1411 // Is this a packet for a session that doesn't exist?
1412 static int rate
= 0; // Number of ICMP packets we've sent this second.
1413 static int last
= 0; // Last time we reset the ICMP packet counter 'rate'.
1415 if (last
!= time_now
)
1421 if (rate
++ < config
->icmp_rate
) // Only send a max of icmp_rate per second.
1423 LOG(4, 0, 0, "IP: Sending ICMP host unreachable to %s\n", fmtaddr(*(in_addr_t
*)(buf
+ 12), 0));
1424 host_unreachable(*(in_addr_t
*)(buf
+ 12), *(uint16_t *)(buf
+ 4),
1425 config
->bind_address
? config
->bind_address
: my_address
, buf
, len
);
1430 t
= session
[s
].tunnel
;
1431 if (len
> session
[s
].mru
|| (session
[s
].mrru
&& len
> session
[s
].mrru
))
1433 LOG(3, s
, t
, "Packet size more than session MRU\n");
1439 // DoS prevention: enforce a maximum number of packets per 0.1s for a session
1440 if (config
->max_packets
> 0)
1442 if (sess_local
[s
].last_packet_out
== TIME
)
1444 int max
= config
->max_packets
;
1446 // All packets for throttled sessions are handled by the
1447 // master, so further limit by using the throttle rate.
1448 // A bit of a kludge, since throttle rate is in kbps,
1449 // but should still be generous given our average DSL
1450 // packet size is 200 bytes: a limit of 28kbps equates
1451 // to around 180 packets per second.
1452 if (!config
->cluster_iam_master
&& sp
->throttle_out
&& sp
->throttle_out
< max
)
1453 max
= sp
->throttle_out
;
1455 if (++sess_local
[s
].packets_out
> max
)
1457 sess_local
[s
].packets_dropped
++;
1463 if (sess_local
[s
].packets_dropped
)
1465 INC_STAT(tun_rx_dropped
, sess_local
[s
].packets_dropped
);
1466 LOG(3, s
, t
, "Dropped %u/%u packets to %s for %suser %s\n",
1467 sess_local
[s
].packets_dropped
, sess_local
[s
].packets_out
,
1468 fmtaddr(ip
, 0), sp
->throttle_out
? "throttled " : "",
1472 sess_local
[s
].last_packet_out
= TIME
;
1473 sess_local
[s
].packets_out
= 1;
1474 sess_local
[s
].packets_dropped
= 0;
1478 // run access-list if any
1479 if (session
[s
].filter_out
&& !ip_filter(buf
, len
, session
[s
].filter_out
- 1))
1482 // adjust MSS on SYN and SYN,ACK packets with options
1483 if ((ntohs(*(uint16_t *) (buf
+ 6)) & 0x1fff) == 0 && buf
[9] == IPPROTO_TCP
) // first tcp fragment
1485 int ihl
= (buf
[0] & 0xf) * 4; // length of IP header
1486 if (len
>= ihl
+ 20 && (buf
[ihl
+ 13] & TCP_FLAG_SYN
) && ((buf
[ihl
+ 12] >> 4) > 5))
1487 adjust_tcp_mss(s
, t
, buf
, len
, buf
+ ihl
);
1492 // Are we throttling this session?
1493 if (config
->cluster_iam_master
)
1494 tbf_queue_packet(sp
->tbf_out
, data
, size
);
1496 master_throttle_packet(sp
->tbf_out
, data
, size
);
1500 if (sp
->walled_garden
&& !config
->cluster_iam_master
)
1502 // We are walled-gardening this
1503 master_garden_packet(s
, data
, size
);
1507 if(session
[s
].bundle
!= 0 && bundle
[session
[s
].bundle
].num_of_links
> 1)
1510 if (!config
->cluster_iam_master
)
1512 // The MPPP packets must be managed by the Master.
1513 master_forward_mppp_packet(s
, data
, size
);
1517 // Add on L2TP header
1518 sessionidt members
[MAXBUNDLESES
];
1519 bundleidt bid
= session
[s
].bundle
;
1520 bundlet
*b
= &bundle
[bid
];
1521 uint32_t num_of_links
, nb_opened
;
1524 num_of_links
= b
->num_of_links
;
1526 for (i
= 0;i
< num_of_links
;i
++)
1529 if (session
[s
].ppp
.lcp
== Opened
)
1531 members
[nb_opened
] = s
;
1538 LOG(3, s
, t
, "MPPP: PROCESSIPOUT ERROR, no session opened in bundle:%d\n", bid
);
1542 num_of_links
= nb_opened
;
1543 b
->current_ses
= (b
->current_ses
+ 1) % num_of_links
;
1544 s
= members
[b
->current_ses
];
1545 t
= session
[s
].tunnel
;
1547 LOG(4, s
, t
, "MPPP: (1)Session number becomes: %d\n", s
);
1549 if (num_of_links
> 1)
1551 if(len
> MINFRAGLEN
)
1553 //for rotate traffic among the member links
1554 uint32_t divisor
= num_of_links
;
1556 divisor
= divisor
/2 + (divisor
& 1);
1558 // Partition the packet to "num_of_links" fragments
1559 uint32_t fraglen
= len
/ divisor
;
1560 uint32_t last_fraglen
= fraglen
+ len
% divisor
;
1561 uint32_t remain
= len
;
1563 // send the first packet
1564 uint8_t *p
= makeppp(fragbuf
, sizeof(fragbuf
), buf
, fraglen
, s
, t
, PPPIP
, 0, bid
, MP_BEGIN
);
1566 tunnelsend(fragbuf
, fraglen
+ (p
-fragbuf
), t
); // send it...
1569 update_session_out_stat(s
, sp
, fraglen
);
1572 while (remain
> last_fraglen
)
1574 b
->current_ses
= (b
->current_ses
+ 1) % num_of_links
;
1575 s
= members
[b
->current_ses
];
1576 t
= session
[s
].tunnel
;
1578 LOG(4, s
, t
, "MPPP: (2)Session number becomes: %d\n", s
);
1579 p
= makeppp(fragbuf
, sizeof(fragbuf
), buf
+(len
- remain
), fraglen
, s
, t
, PPPIP
, 0, bid
, 0);
1581 tunnelsend(fragbuf
, fraglen
+ (p
-fragbuf
), t
); // send it...
1582 update_session_out_stat(s
, sp
, fraglen
);
1585 // send the last fragment
1586 b
->current_ses
= (b
->current_ses
+ 1) % num_of_links
;
1587 s
= members
[b
->current_ses
];
1588 t
= session
[s
].tunnel
;
1590 LOG(4, s
, t
, "MPPP: (2)Session number becomes: %d\n", s
);
1591 p
= makeppp(fragbuf
, sizeof(fragbuf
), buf
+(len
- remain
), remain
, s
, t
, PPPIP
, 0, bid
, MP_END
);
1593 tunnelsend(fragbuf
, remain
+ (p
-fragbuf
), t
); // send it...
1594 update_session_out_stat(s
, sp
, remain
);
1595 if (remain
!= last_fraglen
)
1596 LOG(3, s
, t
, "PROCESSIPOUT ERROR REMAIN != LAST_FRAGLEN, %d != %d\n", remain
, last_fraglen
);
1600 // Send it as one frame
1601 uint8_t *p
= makeppp(fragbuf
, sizeof(fragbuf
), buf
, len
, s
, t
, PPPIP
, 0, bid
, MP_BOTH_BITS
);
1603 tunnelsend(fragbuf
, len
+ (p
-fragbuf
), t
); // send it...
1604 LOG(4, s
, t
, "MPPP: packet sent as one frame\n");
1605 update_session_out_stat(s
, sp
, len
);
1610 // Send it as one frame (NO MPPP Frame)
1611 uint8_t *p
= makeppp(fragbuf
, sizeof(fragbuf
), buf
, len
, s
, t
, PPPIP
, 0, 0, 0);
1613 tunnelsend(fragbuf
, len
+ (p
-fragbuf
), t
); // send it...
1614 update_session_out_stat(s
, sp
, len
);
1619 uint8_t *p
= makeppp(fragbuf
, sizeof(fragbuf
), buf
, len
, s
, t
, PPPIP
, 0, 0, 0);
1621 tunnelsend(fragbuf
, len
+ (p
-fragbuf
), t
); // send it...
1622 update_session_out_stat(s
, sp
, len
);
1625 // Snooping this session, send it to intercept box
1626 if (sp
->snoop_ip
&& sp
->snoop_port
)
1627 snoop_send_packet(buf
, len
, sp
->snoop_ip
, sp
->snoop_port
);
1632 // process outgoing (to tunnel) IPv6
1634 static void processipv6out(uint8_t * buf
, int len
)
1640 struct in6_addr ip6
;
1642 uint8_t *data
= buf
; // Keep a copy of the originals.
1645 uint8_t b
[MAXETHER
+ 20];
1647 CSTAT(processipv6out
);
1649 if (len
< MIN_IP_SIZE
)
1651 LOG(1, 0, 0, "Short IPv6, %d bytes\n", len
);
1652 STAT(tunnel_tx_errors
);
1655 if (len
>= MAXETHER
)
1657 LOG(1, 0, 0, "Oversize IPv6 packet %d bytes\n", len
);
1658 STAT(tunnel_tx_errors
);
1662 // Skip the tun header
1666 // Got an IP header now
1667 if (*(uint8_t *)(buf
) >> 4 != 6)
1669 LOG(1, 0, 0, "IP: Don't understand anything except IPv6\n");
1673 ip6
= *(struct in6_addr
*)(buf
+24);
1674 s
= sessionbyipv6(ip6
);
1678 ip
= *(uint32_t *)(buf
+ 32);
1679 s
= sessionbyip(ip
);
1684 // Is this a packet for a session that doesn't exist?
1685 static int rate
= 0; // Number of ICMP packets we've sent this second.
1686 static int last
= 0; // Last time we reset the ICMP packet counter 'rate'.
1688 if (last
!= time_now
)
1694 if (rate
++ < config
->icmp_rate
) // Only send a max of icmp_rate per second.
1696 // FIXME: Should send icmp6 host unreachable
1700 if (session
[s
].bundle
&& bundle
[session
[s
].bundle
].num_of_links
> 1)
1702 bundleidt bid
= session
[s
].bundle
;
1703 bundlet
*b
= &bundle
[bid
];
1705 b
->current_ses
= (b
->current_ses
+ 1) % b
->num_of_links
;
1706 s
= b
->members
[b
->current_ses
];
1707 LOG(3, s
, session
[s
].tunnel
, "MPPP: Session number becomes: %u\n", s
);
1709 t
= session
[s
].tunnel
;
1711 sp
->last_data
= time_now
;
1713 // FIXME: add DoS prevention/filters?
1717 // Are we throttling this session?
1718 if (config
->cluster_iam_master
)
1719 tbf_queue_packet(sp
->tbf_out
, data
, size
);
1721 master_throttle_packet(sp
->tbf_out
, data
, size
);
1724 else if (sp
->walled_garden
&& !config
->cluster_iam_master
)
1726 // We are walled-gardening this
1727 master_garden_packet(s
, data
, size
);
1731 LOG(5, s
, t
, "Ethernet -> Tunnel (%d bytes)\n", len
);
1733 // Add on L2TP header
1735 uint8_t *p
= makeppp(b
, sizeof(b
), buf
, len
, s
, t
, PPPIPV6
, 0, 0, 0);
1737 tunnelsend(b
, len
+ (p
-b
), t
); // send it...
1740 // Snooping this session, send it to intercept box
1741 if (sp
->snoop_ip
&& sp
->snoop_port
)
1742 snoop_send_packet(buf
, len
, sp
->snoop_ip
, sp
->snoop_port
);
1744 increment_counter(&sp
->cout
, &sp
->cout_wrap
, len
); // byte count
1745 sp
->cout_delta
+= len
;
1749 sess_local
[s
].cout
+= len
; // To send to master..
1750 sess_local
[s
].pout
++;
1754 // Helper routine for the TBF filters.
1755 // Used to send queued data in to the user!
1757 static void send_ipout(sessionidt s
, uint8_t *buf
, int len
)
1762 uint8_t b
[MAXETHER
+ 20];
1764 if (len
< 0 || len
> MAXETHER
)
1766 LOG(1, 0, 0, "Odd size IP packet: %d bytes\n", len
);
1770 // Skip the tun header
1777 t
= session
[s
].tunnel
;
1780 LOG(5, s
, t
, "Ethernet -> Tunnel (%d bytes)\n", len
);
1782 // Add on L2TP header
1784 uint8_t *p
= makeppp(b
, sizeof(b
), buf
, len
, s
, t
, PPPIP
, 0, 0, 0);
1786 tunnelsend(b
, len
+ (p
-b
), t
); // send it...
1789 // Snooping this session.
1790 if (sp
->snoop_ip
&& sp
->snoop_port
)
1791 snoop_send_packet(buf
, len
, sp
->snoop_ip
, sp
->snoop_port
);
1793 increment_counter(&sp
->cout
, &sp
->cout_wrap
, len
); // byte count
1794 sp
->cout_delta
+= len
;
1798 sess_local
[s
].cout
+= len
; // To send to master..
1799 sess_local
[s
].pout
++;
1802 // add an AVP (16 bit)
1803 static void control16(controlt
* c
, uint16_t avp
, uint16_t val
, uint8_t m
)
1805 uint16_t l
= (m
? 0x8008 : 0x0008);
1806 c
->buf16
[c
->length
/2 + 0] = htons(l
);
1807 c
->buf16
[c
->length
/2 + 1] = htons(0);
1808 c
->buf16
[c
->length
/2 + 2] = htons(avp
);
1809 c
->buf16
[c
->length
/2 + 3] = htons(val
);
1813 // add an AVP (32 bit)
1814 static void control32(controlt
* c
, uint16_t avp
, uint32_t val
, uint8_t m
)
1816 uint16_t l
= (m
? 0x800A : 0x000A);
1817 c
->buf16
[c
->length
/2 + 0] = htons(l
);
1818 c
->buf16
[c
->length
/2 + 1] = htons(0);
1819 c
->buf16
[c
->length
/2 + 2] = htons(avp
);
1820 *(uint32_t *) &c
->buf
[c
->length
+ 6] = htonl(val
);
1824 // add an AVP (string)
1825 static void controls(controlt
* c
, uint16_t avp
, char *val
, uint8_t m
)
1827 uint16_t l
= ((m
? 0x8000 : 0) + strlen(val
) + 6);
1828 c
->buf16
[c
->length
/2 + 0] = htons(l
);
1829 c
->buf16
[c
->length
/2 + 1] = htons(0);
1830 c
->buf16
[c
->length
/2 + 2] = htons(avp
);
1831 memcpy(&c
->buf
[c
->length
+ 6], val
, strlen(val
));
1832 c
->length
+= 6 + strlen(val
);
1836 static void controlb(controlt
* c
, uint16_t avp
, uint8_t *val
, unsigned int len
, uint8_t m
)
1838 uint16_t l
= ((m
? 0x8000 : 0) + len
+ 6);
1839 c
->buf16
[c
->length
/2 + 0] = htons(l
);
1840 c
->buf16
[c
->length
/2 + 1] = htons(0);
1841 c
->buf16
[c
->length
/2 + 2] = htons(avp
);
1842 memcpy(&c
->buf
[c
->length
+ 6], val
, len
);
1843 c
->length
+= 6 + len
;
1846 // new control connection
1847 static controlt
*controlnew(uint16_t mtype
)
1851 c
= malloc(sizeof(controlt
));
1855 controlfree
= c
->next
;
1859 c
->buf16
[0] = htons(0xC802); // flags/ver
1861 control16(c
, 0, mtype
, 1);
1865 // send zero block if nothing is waiting
1867 static void controlnull(tunnelidt t
)
1870 if (tunnel
[t
].controlc
) // Messages queued; They will carry the ack.
1873 buf
[0] = htons(0xC802); // flags/ver
1874 buf
[1] = htons(12); // length
1875 buf
[2] = htons(tunnel
[t
].far
); // tunnel
1876 buf
[3] = htons(0); // session
1877 buf
[4] = htons(tunnel
[t
].ns
); // sequence
1878 buf
[5] = htons(tunnel
[t
].nr
); // sequence
1879 tunnelsend((uint8_t *)buf
, 12, t
);
1882 // add a control message to a tunnel, and send if within window
1883 static void controladd(controlt
*c
, sessionidt far
, tunnelidt t
)
1885 c
->buf16
[1] = htons(c
->length
); // length
1886 c
->buf16
[2] = htons(tunnel
[t
].far
); // tunnel
1887 c
->buf16
[3] = htons(far
); // session
1888 c
->buf16
[4] = htons(tunnel
[t
].ns
); // sequence
1889 tunnel
[t
].ns
++; // advance sequence
1890 // link in message in to queue
1891 if (tunnel
[t
].controlc
)
1892 tunnel
[t
].controle
->next
= c
;
1894 tunnel
[t
].controls
= c
;
1896 tunnel
[t
].controle
= c
;
1897 tunnel
[t
].controlc
++;
1899 // send now if space in window
1900 if (tunnel
[t
].controlc
<= tunnel
[t
].window
)
1902 tunnel
[t
].try = 0; // first send
1903 tunnelsend(c
->buf
, c
->length
, t
);
1908 // Throttle or Unthrottle a session
1910 // Throttle the data from/to through a session to no more than
1911 // 'rate_in' kbit/sec in (from user) or 'rate_out' kbit/sec out (to
1914 // If either value is -1, the current value is retained for that
1917 void throttle_session(sessionidt s
, int rate_in
, int rate_out
)
1919 if (!session
[s
].opened
)
1920 return; // No-one home.
1922 if (!*session
[s
].user
)
1923 return; // User not logged in
1927 int bytes
= rate_in
* 1024 / 8; // kbits to bytes
1928 if (session
[s
].tbf_in
)
1929 free_tbf(session
[s
].tbf_in
);
1932 session
[s
].tbf_in
= new_tbf(s
, bytes
* 2, bytes
, send_ipin
);
1934 session
[s
].tbf_in
= 0;
1936 session
[s
].throttle_in
= rate_in
;
1941 int bytes
= rate_out
* 1024 / 8;
1942 if (session
[s
].tbf_out
)
1943 free_tbf(session
[s
].tbf_out
);
1946 session
[s
].tbf_out
= new_tbf(s
, bytes
* 2, bytes
, send_ipout
);
1948 session
[s
].tbf_out
= 0;
1950 session
[s
].throttle_out
= rate_out
;
1954 // add/remove filters from session (-1 = no change)
1955 void filter_session(sessionidt s
, int filter_in
, int filter_out
)
1957 if (!session
[s
].opened
)
1958 return; // No-one home.
1960 if (!*session
[s
].user
)
1961 return; // User not logged in
1964 if (filter_in
> MAXFILTER
) filter_in
= -1;
1965 if (filter_out
> MAXFILTER
) filter_out
= -1;
1966 if (session
[s
].filter_in
> MAXFILTER
) session
[s
].filter_in
= 0;
1967 if (session
[s
].filter_out
> MAXFILTER
) session
[s
].filter_out
= 0;
1971 if (session
[s
].filter_in
)
1972 ip_filters
[session
[s
].filter_in
- 1].used
--;
1975 ip_filters
[filter_in
- 1].used
++;
1977 session
[s
].filter_in
= filter_in
;
1980 if (filter_out
>= 0)
1982 if (session
[s
].filter_out
)
1983 ip_filters
[session
[s
].filter_out
- 1].used
--;
1986 ip_filters
[filter_out
- 1].used
++;
1988 session
[s
].filter_out
= filter_out
;
1992 // start tidy shutdown of session
1993 void sessionshutdown(sessionidt s
, char const *reason
, int cdn_result
, int cdn_error
, int term_cause
)
1995 int walled_garden
= session
[s
].walled_garden
;
1996 bundleidt b
= session
[s
].bundle
;
1997 //delete routes only for last session in bundle (in case of MPPP)
1998 int del_routes
= !b
|| (bundle
[b
].num_of_links
== 1);
2000 CSTAT(sessionshutdown
);
2002 if (!session
[s
].opened
)
2004 LOG(3, s
, session
[s
].tunnel
, "Called sessionshutdown on an unopened session.\n");
2005 return; // not a live session
2008 if (!session
[s
].die
)
2010 struct param_kill_session data
= { &tunnel
[session
[s
].tunnel
], &session
[s
] };
2011 LOG(2, s
, session
[s
].tunnel
, "Shutting down session %u: %s\n", s
, reason
);
2012 run_plugins(PLUGIN_KILL_SESSION
, &data
);
2013 session
[s
].die
= TIME
+ 150; // Clean up in 15 seconds
2016 if (session
[s
].ip
&& !walled_garden
&& !session
[s
].die
)
2018 // RADIUS Stop message
2019 uint16_t r
= radiusnew(s
);
2022 // stop, if not already trying
2023 if (radius
[r
].state
!= RADIUSSTOP
)
2025 radius
[r
].term_cause
= term_cause
;
2026 radius
[r
].term_msg
= reason
;
2027 radiussend(r
, RADIUSSTOP
);
2031 LOG(1, s
, session
[s
].tunnel
, "No free RADIUS sessions for Stop message\n");
2033 // Save counters to dump to accounting file
2034 if (*config
->accounting_dir
&& shut_acct_n
< sizeof(shut_acct
) / sizeof(*shut_acct
))
2035 memcpy(&shut_acct
[shut_acct_n
++], &session
[s
], sizeof(session
[s
]));
2039 { // IP allocated, clear and unroute
2042 for (r
= 0; r
< MAXROUTE
&& session
[s
].route
[r
].ip
; r
++)
2044 if ((session
[s
].ip
>> (32-session
[s
].route
[r
].prefixlen
)) ==
2045 (session
[s
].route
[r
].ip
>> (32-session
[s
].route
[r
].prefixlen
)))
2048 if (del_routes
) routeset(s
, session
[s
].route
[r
].ip
, session
[s
].route
[r
].prefixlen
, 0, 0);
2049 session
[s
].route
[r
].ip
= 0;
2052 if (session
[s
].ip_pool_index
== -1) // static ip
2054 if (!routed
&& del_routes
) routeset(s
, session
[s
].ip
, 0, 0, 0);
2060 // unroute IPv6, if setup
2061 if (session
[s
].ppp
.ipv6cp
== Opened
&& session
[s
].ipv6prefixlen
&& del_routes
)
2062 route6set(s
, session
[s
].ipv6route
, session
[s
].ipv6prefixlen
, 0);
2066 // This session was part of a bundle
2067 bundle
[b
].num_of_links
--;
2068 LOG(3, s
, session
[s
].tunnel
, "MPPP: Dropping member link: %d from bundle %d\n",s
,b
);
2069 if(bundle
[b
].num_of_links
== 0)
2072 LOG(3, s
, session
[s
].tunnel
, "MPPP: Kill bundle: %d (No remaing member links)\n",b
);
2076 // Adjust the members array to accomodate the new change
2077 uint8_t mem_num
= 0;
2078 // It should be here num_of_links instead of num_of_links-1 (previous instruction "num_of_links--")
2079 if(bundle
[b
].members
[bundle
[b
].num_of_links
] != s
)
2082 for(ml
= 0; ml
<bundle
[b
].num_of_links
; ml
++)
2083 if(bundle
[b
].members
[ml
] == s
)
2088 bundle
[b
].members
[mem_num
] = bundle
[b
].members
[bundle
[b
].num_of_links
];
2089 LOG(3, s
, session
[s
].tunnel
, "MPPP: Adjusted member links array\n");
2091 // If the killed session is the first of the bundle,
2092 // the new first session must be stored in the cache_ipmap
2093 // else the function sessionbyip return 0 and the sending not work any more (processipout).
2096 sessionidt new_s
= bundle
[b
].members
[0];
2099 // Add the route for this session.
2100 for (r
= 0; r
< MAXROUTE
&& session
[new_s
].route
[r
].ip
; r
++)
2105 prefixlen
= session
[new_s
].route
[r
].prefixlen
;
2106 ip
= session
[new_s
].route
[r
].ip
;
2108 if (!prefixlen
) prefixlen
= 32;
2109 ip
&= 0xffffffff << (32 - prefixlen
); // Force the ip to be the first one in the route.
2111 for (i
= ip
; i
< ip
+(1<<(32-prefixlen
)) ; ++i
)
2112 cache_ipmap(i
, new_s
);
2114 cache_ipmap(session
[new_s
].ip
, new_s
);
2117 if (session
[new_s
].ipv6prefixlen
)
2118 cache_ipv6map(session
[new_s
].ipv6route
, session
[new_s
].ipv6prefixlen
, new_s
);
2123 cluster_send_bundle(b
);
2127 if (session
[s
].throttle_in
|| session
[s
].throttle_out
) // Unthrottle if throttled.
2128 throttle_session(s
, 0, 0);
2132 controlt
*c
= controlnew(14); // sending CDN
2136 buf
[0] = htons(cdn_result
);
2137 buf
[1] = htons(cdn_error
);
2138 controlb(c
, 1, (uint8_t *)buf
, 4, 1);
2141 control16(c
, 1, cdn_result
, 1);
2143 control16(c
, 14, s
, 1); // assigned session (our end)
2144 controladd(c
, session
[s
].far
, session
[s
].tunnel
); // send the message
2147 // update filter refcounts
2148 if (session
[s
].filter_in
) ip_filters
[session
[s
].filter_in
- 1].used
--;
2149 if (session
[s
].filter_out
) ip_filters
[session
[s
].filter_out
- 1].used
--;
2152 memset(&session
[s
].ppp
, 0, sizeof(session
[s
].ppp
));
2153 sess_local
[s
].lcp
.restart
= 0;
2154 sess_local
[s
].ipcp
.restart
= 0;
2155 sess_local
[s
].ipv6cp
.restart
= 0;
2156 sess_local
[s
].ccp
.restart
= 0;
2158 cluster_send_session(s
);
2161 void sendipcp(sessionidt s
, tunnelidt t
)
2163 uint8_t buf
[MAXETHER
];
2167 LOG(3, s
, t
, "IPCP: send ConfigReq\n");
2169 if (!session
[s
].unique_id
)
2171 if (!++last_id
) ++last_id
; // skip zero
2172 session
[s
].unique_id
= last_id
;
2175 q
= makeppp(buf
, sizeof(buf
), 0, 0, s
, t
, PPPIPCP
, 0, 0, 0);
2179 q
[1] = session
[s
].unique_id
& 0xf; // ID, dont care, we only send one type of request
2180 *(uint16_t *) (q
+ 2) = htons(10); // packet length
2181 q
[4] = 3; // ip address option
2182 q
[5] = 6; // option length
2183 *(in_addr_t
*) (q
+ 6) = config
->peer_address
? config
->peer_address
:
2184 config
->iftun_address
? config
->iftun_address
:
2185 my_address
; // send my IP
2187 tunnelsend(buf
, 10 + (q
- buf
), t
); // send it
2188 restart_timer(s
, ipcp
);
2191 void sendipv6cp(sessionidt s
, tunnelidt t
)
2193 uint8_t buf
[MAXETHER
];
2197 LOG(3, s
, t
, "IPV6CP: send ConfigReq\n");
2199 q
= makeppp(buf
, sizeof(buf
), 0, 0, s
, t
, PPPIPV6CP
, 0, 0, 0);
2203 q
[1] = session
[s
].unique_id
& 0xf; // ID, don't care, we
2204 // only send one type
2206 *(uint16_t *) (q
+ 2) = htons(14);
2207 q
[4] = 1; // interface identifier option
2208 q
[5] = 10; // option length
2209 *(uint32_t *) (q
+ 6) = 0; // We'll be prefix::1
2210 *(uint32_t *) (q
+ 10) = 0;
2213 tunnelsend(buf
, 14 + (q
- buf
), t
); // send it
2214 restart_timer(s
, ipv6cp
);
2217 static void sessionclear(sessionidt s
)
2219 memset(&session
[s
], 0, sizeof(session
[s
]));
2220 memset(&sess_local
[s
], 0, sizeof(sess_local
[s
]));
2221 memset(&cli_session_actions
[s
], 0, sizeof(cli_session_actions
[s
]));
2223 session
[s
].tunnel
= T_FREE
; // Mark it as free.
2224 session
[s
].next
= sessionfree
;
2228 // kill a session now
2229 void sessionkill(sessionidt s
, char *reason
)
2233 if (!session
[s
].opened
) // not alive
2236 if (session
[s
].next
)
2238 LOG(0, s
, session
[s
].tunnel
, "Tried to kill a session with next pointer set (%u)\n", session
[s
].next
);
2242 if (!session
[s
].die
)
2243 sessionshutdown(s
, reason
, CDN_ADMIN_DISC
, TERM_ADMIN_RESET
); // close radius/routes, etc.
2245 if (sess_local
[s
].radius
)
2246 radiusclear(sess_local
[s
].radius
, s
); // cant send clean accounting data, session is killed
2249 if (session
[s
].forwardtosession
)
2251 sessionidt sess
= session
[s
].forwardtosession
;
2252 if (session
[sess
].forwardtosession
== s
)
2254 // Shutdown the linked session also.
2255 sessionshutdown(sess
, reason
, CDN_ADMIN_DISC
, TERM_ADMIN_RESET
);
2260 LOG(2, s
, session
[s
].tunnel
, "Kill session %d (%s): %s\n", s
, session
[s
].user
, reason
);
2262 cluster_send_session(s
);
2265 static void tunnelclear(tunnelidt t
)
2268 memset(&tunnel
[t
], 0, sizeof(tunnel
[t
]));
2269 tunnel
[t
].state
= TUNNELFREE
;
2272 static void bundleclear(bundleidt b
)
2275 memset(&bundle
[b
], 0, sizeof(bundle
[b
]));
2276 bundle
[b
].state
= BUNDLEFREE
;
2279 // kill a tunnel now
2280 static void tunnelkill(tunnelidt t
, char *reason
)
2287 tunnel
[t
].state
= TUNNELDIE
;
2289 // free control messages
2290 while ((c
= tunnel
[t
].controls
))
2292 controlt
* n
= c
->next
;
2293 tunnel
[t
].controls
= n
;
2294 tunnel
[t
].controlc
--;
2295 c
->next
= controlfree
;
2299 for (s
= 1; s
<= config
->cluster_highest_sessionid
; ++s
)
2300 if (session
[s
].tunnel
== t
)
2301 sessionkill(s
, reason
);
2305 LOG(1, 0, t
, "Kill tunnel %u: %s\n", t
, reason
);
2306 cli_tunnel_actions
[t
].action
= 0;
2307 cluster_send_tunnel(t
);
2310 // shut down a tunnel cleanly
2311 static void tunnelshutdown(tunnelidt t
, char *reason
, int result
, int error
, char *msg
)
2315 CSTAT(tunnelshutdown
);
2317 if (!tunnel
[t
].last
|| !tunnel
[t
].far
|| tunnel
[t
].state
== TUNNELFREE
)
2319 // never set up, can immediately kill
2320 tunnelkill(t
, reason
);
2323 LOG(1, 0, t
, "Shutting down tunnel %u (%s)\n", t
, reason
);
2326 for (s
= 1; s
<= config
->cluster_highest_sessionid
; ++s
)
2327 if (session
[s
].tunnel
== t
)
2328 sessionshutdown(s
, reason
, CDN_NONE
, TERM_ADMIN_RESET
);
2330 tunnel
[t
].state
= TUNNELDIE
;
2331 tunnel
[t
].die
= TIME
+ 700; // Clean up in 70 seconds
2332 cluster_send_tunnel(t
);
2333 // TBA - should we wait for sessions to stop?
2336 controlt
*c
= controlnew(4); // sending StopCCN
2341 buf
[0] = htons(result
);
2342 buf
[1] = htons(error
);
2345 int m
= strlen(msg
);
2346 if (m
+ 4 > sizeof(buf
))
2347 m
= sizeof(buf
) - 4;
2349 memcpy(buf
+2, msg
, m
);
2353 controlb(c
, 1, (uint8_t *)buf
, l
, 1);
2356 control16(c
, 1, result
, 1);
2358 control16(c
, 9, t
, 1); // assigned tunnel (our end)
2359 controladd(c
, 0, t
); // send the message
2363 // read and process packet on tunnel (UDP)
2364 void processudp(uint8_t *buf
, int len
, struct sockaddr_in
*addr
)
2366 uint8_t *chapresponse
= NULL
;
2367 uint16_t l
= len
, t
= 0, s
= 0, ns
= 0, nr
= 0;
2368 uint8_t *p
= buf
+ 2;
2375 LOG_HEX(5, "UDP Data", buf
, len
);
2376 STAT(tunnel_rx_packets
);
2377 INC_STAT(tunnel_rx_bytes
, len
);
2380 LOG(1, 0, 0, "Short UDP, %d bytes\n", len
);
2381 STAT(tunnel_rx_errors
);
2384 if ((buf
[1] & 0x0F) != 2)
2386 LOG(1, 0, 0, "Bad L2TP ver %d\n", buf
[1] & 0x0F);
2387 STAT(tunnel_rx_errors
);
2392 l
= ntohs(*(uint16_t *) p
);
2395 t
= ntohs(*(uint16_t *) p
);
2397 s
= ntohs(*(uint16_t *) p
);
2399 if (s
>= MAXSESSION
)
2401 LOG(1, s
, t
, "Received UDP packet with invalid session ID\n");
2402 STAT(tunnel_rx_errors
);
2407 LOG(1, s
, t
, "Received UDP packet with invalid tunnel ID\n");
2408 STAT(tunnel_rx_errors
);
2413 ns
= ntohs(*(uint16_t *) p
);
2415 nr
= ntohs(*(uint16_t *) p
);
2420 uint16_t o
= ntohs(*(uint16_t *) p
);
2425 LOG(1, s
, t
, "Bad length %d>%d\n", (int) (p
- buf
), l
);
2426 STAT(tunnel_rx_errors
);
2431 // used to time out old tunnels
2432 if (t
&& tunnel
[t
].state
== TUNNELOPEN
)
2433 tunnel
[t
].lastrec
= time_now
;
2437 uint16_t message
= 0xFFFF; // message type
2439 uint8_t mandatory
= 0;
2440 uint16_t asession
= 0; // assigned session
2441 uint32_t amagic
= 0; // magic number
2442 uint8_t aflags
= 0; // flags from last LCF
2443 uint16_t version
= 0x0100; // protocol version (we handle 0.0 as well and send that back just in case)
2444 char called
[MAXTEL
] = ""; // called number
2445 char calling
[MAXTEL
] = ""; // calling number
2447 if (!config
->cluster_iam_master
)
2449 master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
);
2453 // control messages must have bits 0x80|0x40|0x08
2454 // (type, length and sequence) set, and bits 0x02|0x01
2455 // (offset and priority) clear
2456 if ((*buf
& 0xCB) != 0xC8)
2458 LOG(1, s
, t
, "Bad control header %02X\n", *buf
);
2459 STAT(tunnel_rx_errors
);
2463 // check for duplicate tunnel open message
2469 // Is this a duplicate of the first packet? (SCCRQ)
2471 for (i
= 1; i
<= config
->cluster_highest_tunnelid
; ++i
)
2473 if (tunnel
[i
].state
!= TUNNELOPENING
||
2474 tunnel
[i
].ip
!= ntohl(*(in_addr_t
*) & addr
->sin_addr
) ||
2475 tunnel
[i
].port
!= ntohs(addr
->sin_port
) )
2478 LOG(3, s
, t
, "Duplicate SCCRQ?\n");
2483 LOG(3, s
, t
, "Control message (%d bytes): (unacked %d) l-ns %u l-nr %u r-ns %u r-nr %u\n",
2484 l
, tunnel
[t
].controlc
, tunnel
[t
].ns
, tunnel
[t
].nr
, ns
, nr
);
2486 // if no tunnel specified, assign one
2489 if (!(t
= new_tunnel()))
2491 LOG(1, 0, 0, "No more tunnels\n");
2492 STAT(tunnel_overflow
);
2496 tunnel
[t
].ip
= ntohl(*(in_addr_t
*) & addr
->sin_addr
);
2497 tunnel
[t
].port
= ntohs(addr
->sin_port
);
2498 tunnel
[t
].window
= 4; // default window
2499 STAT(tunnel_created
);
2500 LOG(1, 0, t
, " New tunnel from %s:%u ID %u\n",
2501 fmtaddr(htonl(tunnel
[t
].ip
), 0), tunnel
[t
].port
, t
);
2504 // If the 'ns' just received is not the 'nr' we're
2505 // expecting, just send an ack and drop it.
2507 // if 'ns' is less, then we got a retransmitted packet.
2508 // if 'ns' is greater than missed a packet. Either way
2509 // we should ignore it.
2510 if (ns
!= tunnel
[t
].nr
)
2512 // is this the sequence we were expecting?
2513 STAT(tunnel_rx_errors
);
2514 LOG(1, 0, t
, " Out of sequence tunnel %u, (%u is not the expected %u)\n",
2515 t
, ns
, tunnel
[t
].nr
);
2517 if (l
) // Is this not a ZLB?
2522 // check sequence of this message
2524 int skip
= tunnel
[t
].window
; // track how many in-window packets are still in queue
2525 // some to clear maybe?
2526 while (tunnel
[t
].controlc
> 0 && (((tunnel
[t
].ns
- tunnel
[t
].controlc
) - nr
) & 0x8000))
2528 controlt
*c
= tunnel
[t
].controls
;
2529 tunnel
[t
].controls
= c
->next
;
2530 tunnel
[t
].controlc
--;
2531 c
->next
= controlfree
;
2534 tunnel
[t
].try = 0; // we have progress
2537 // receiver advance (do here so quoted correctly in any sends below)
2538 if (l
) tunnel
[t
].nr
= (ns
+ 1);
2539 if (skip
< 0) skip
= 0;
2540 if (skip
< tunnel
[t
].controlc
)
2542 // some control packets can now be sent that were previous stuck out of window
2543 int tosend
= tunnel
[t
].window
- skip
;
2544 controlt
*c
= tunnel
[t
].controls
;
2552 tunnel
[t
].try = 0; // first send
2553 tunnelsend(c
->buf
, c
->length
, t
);
2558 if (!tunnel
[t
].controlc
)
2559 tunnel
[t
].retry
= 0; // caught up
2562 { // if not a null message
2567 // Default disconnect cause/message on receipt of CDN. Set to
2568 // more specific value from attribute 1 (result code) or 46
2569 // (disconnect cause) if present below.
2570 int disc_cause_set
= 0;
2571 int disc_cause
= TERM_NAS_REQUEST
;
2572 char const *disc_reason
= "Closed (Received CDN).";
2575 while (l
&& !(fatal
& 0x80)) // 0x80 = mandatory AVP
2577 uint16_t n
= (ntohs(*(uint16_t *) p
) & 0x3FF);
2584 LOG(1, s
, t
, "Invalid length in AVP\n");
2585 STAT(tunnel_rx_errors
);
2590 if (flags
& 0x3C) // reserved bits, should be clear
2592 LOG(1, s
, t
, "Unrecognised AVP flags %02X\n", *b
);
2594 result
= 2; // general error
2595 error
= 3; // reserved field non-zero
2600 if (*(uint16_t *) (b
))
2602 LOG(2, s
, t
, "Unknown AVP vendor %u\n", ntohs(*(uint16_t *) (b
)));
2604 result
= 2; // general error
2605 error
= 6; // generic vendor-specific error
2606 msg
= "unsupported vendor-specific";
2610 mtype
= ntohs(*(uint16_t *) (b
));
2618 // handle hidden AVPs
2619 if (!*config
->l2tp_secret
)
2621 LOG(1, s
, t
, "Hidden AVP requested, but no L2TP secret.\n");
2623 result
= 2; // general error
2624 error
= 6; // generic vendor-specific error
2625 msg
= "secret not specified";
2628 if (!session
[s
].random_vector_length
)
2630 LOG(1, s
, t
, "Hidden AVP requested, but no random vector.\n");
2632 result
= 2; // general error
2633 error
= 6; // generic
2634 msg
= "no random vector";
2639 LOG(2, s
, t
, "Short hidden AVP.\n");
2641 result
= 2; // general error
2642 error
= 2; // length is wrong
2648 unhide_value(b
, n
, mtype
, session
[s
].random_vector
, session
[s
].random_vector_length
);
2650 orig_len
= ntohs(*(uint16_t *) b
);
2651 if (orig_len
> n
+ 2)
2653 LOG(1, s
, t
, "Original length %d too long in hidden AVP of length %d; wrong secret?\n",
2657 result
= 2; // general error
2658 error
= 2; // length is wrong
2667 LOG(4, s
, t
, " AVP %u (%s) len %d%s%s\n", mtype
, l2tp_avp_name(mtype
), n
,
2668 flags
& 0x40 ? ", hidden" : "", flags
& 0x80 ? ", mandatory" : "");
2672 case 0: // message type
2673 message
= ntohs(*(uint16_t *) b
);
2674 mandatory
= flags
& 0x80;
2675 LOG(4, s
, t
, " Message type = %u (%s)\n", message
, l2tp_code(message
));
2677 case 1: // result code
2679 uint16_t rescode
= ntohs(*(uint16_t *) b
);
2680 char const *resdesc
= "(unknown)";
2681 char const *errdesc
= NULL
;
2686 resdesc
= l2tp_stopccn_result_code(rescode
);
2687 cause
= TERM_LOST_SERVICE
;
2689 else if (message
== 14)
2691 resdesc
= l2tp_cdn_result_code(rescode
);
2693 cause
= TERM_LOST_CARRIER
;
2695 cause
= TERM_ADMIN_RESET
;
2698 LOG(4, s
, t
, " Result Code %u: %s\n", rescode
, resdesc
);
2701 uint16_t errcode
= ntohs(*(uint16_t *)(b
+ 2));
2702 errdesc
= l2tp_error_code(errcode
);
2703 LOG(4, s
, t
, " Error Code %u: %s\n", errcode
, errdesc
);
2706 LOG(4, s
, t
, " Error String: %.*s\n", n
-4, b
+4);
2708 if (cause
&& disc_cause_set
< mtype
) // take cause from attrib 46 in preference
2710 disc_cause_set
= mtype
;
2711 disc_reason
= errdesc
? errdesc
: resdesc
;
2718 case 2: // protocol version
2720 version
= ntohs(*(uint16_t *) (b
));
2721 LOG(4, s
, t
, " Protocol version = %u\n", version
);
2722 if (version
&& version
!= 0x0100)
2723 { // allow 0.0 and 1.0
2724 LOG(1, s
, t
, " Bad protocol version %04X\n", version
);
2726 result
= 5; // unspported protocol version
2727 error
= 0x0100; // supported version
2733 case 3: // framing capabilities
2735 case 4: // bearer capabilities
2737 case 5: // tie breaker
2738 // We never open tunnels, so we don't care about tie breakers
2740 case 6: // firmware revision
2742 case 7: // host name
2743 memset(tunnel
[t
].hostname
, 0, sizeof(tunnel
[t
].hostname
));
2744 memcpy(tunnel
[t
].hostname
, b
, (n
< sizeof(tunnel
[t
].hostname
)) ? n
: sizeof(tunnel
[t
].hostname
) - 1);
2745 LOG(4, s
, t
, " Tunnel hostname = \"%s\"\n", tunnel
[t
].hostname
);
2746 // TBA - to send to RADIUS
2748 case 8: // vendor name
2749 memset(tunnel
[t
].vendor
, 0, sizeof(tunnel
[t
].vendor
));
2750 memcpy(tunnel
[t
].vendor
, b
, (n
< sizeof(tunnel
[t
].vendor
)) ? n
: sizeof(tunnel
[t
].vendor
) - 1);
2751 LOG(4, s
, t
, " Vendor name = \"%s\"\n", tunnel
[t
].vendor
);
2753 case 9: // assigned tunnel
2754 tunnel
[t
].far
= ntohs(*(uint16_t *) (b
));
2755 LOG(4, s
, t
, " Remote tunnel id = %u\n", tunnel
[t
].far
);
2757 case 10: // rx window
2758 tunnel
[t
].window
= ntohs(*(uint16_t *) (b
));
2759 if (!tunnel
[t
].window
)
2760 tunnel
[t
].window
= 1; // window of 0 is silly
2761 LOG(4, s
, t
, " rx window = %u\n", tunnel
[t
].window
);
2763 case 11: // Challenge
2765 LOG(4, s
, t
, " LAC requested CHAP authentication for tunnel\n");
2766 build_chap_response(b
, 2, n
, &chapresponse
);
2769 case 13: // Response
2771 if (tunnel
[t
].isremotelns
)
2773 chapresponse
= calloc(17, 1);
2774 memcpy(chapresponse
, b
, (n
< 17) ? n
: 16);
2775 LOG(3, s
, t
, "received challenge response from REMOTE LNS\n");
2779 // Why did they send a response? We never challenge.
2780 LOG(2, s
, t
, " received unexpected challenge response\n");
2783 case 14: // assigned session
2784 asession
= session
[s
].far
= ntohs(*(uint16_t *) (b
));
2785 LOG(4, s
, t
, " assigned session = %u\n", asession
);
2787 case 15: // call serial number
2788 LOG(4, s
, t
, " call serial number = %u\n", ntohl(*(uint32_t *)b
));
2790 case 18: // bearer type
2791 LOG(4, s
, t
, " bearer type = %u\n", ntohl(*(uint32_t *)b
));
2794 case 19: // framing type
2795 LOG(4, s
, t
, " framing type = %u\n", ntohl(*(uint32_t *)b
));
2798 case 21: // called number
2799 memset(called
, 0, sizeof(called
));
2800 memcpy(called
, b
, (n
< sizeof(called
)) ? n
: sizeof(called
) - 1);
2801 LOG(4, s
, t
, " Called <%s>\n", called
);
2803 case 22: // calling number
2804 memset(calling
, 0, sizeof(calling
));
2805 memcpy(calling
, b
, (n
< sizeof(calling
)) ? n
: sizeof(calling
) - 1);
2806 LOG(4, s
, t
, " Calling <%s>\n", calling
);
2810 case 24: // tx connect speed
2813 session
[s
].tx_connect_speed
= ntohl(*(uint32_t *)b
);
2817 // AS5300s send connect speed as a string
2819 memset(tmp
, 0, sizeof(tmp
));
2820 memcpy(tmp
, b
, (n
< sizeof(tmp
)) ? n
: sizeof(tmp
) - 1);
2821 session
[s
].tx_connect_speed
= atol(tmp
);
2823 LOG(4, s
, t
, " TX connect speed <%u>\n", session
[s
].tx_connect_speed
);
2825 case 38: // rx connect speed
2828 session
[s
].rx_connect_speed
= ntohl(*(uint32_t *)b
);
2832 // AS5300s send connect speed as a string
2834 memset(tmp
, 0, sizeof(tmp
));
2835 memcpy(tmp
, b
, (n
< sizeof(tmp
)) ? n
: sizeof(tmp
) - 1);
2836 session
[s
].rx_connect_speed
= atol(tmp
);
2838 LOG(4, s
, t
, " RX connect speed <%u>\n", session
[s
].rx_connect_speed
);
2840 case 25: // Physical Channel ID
2842 uint32_t tmp
= ntohl(*(uint32_t *) b
);
2843 LOG(4, s
, t
, " Physical Channel ID <%X>\n", tmp
);
2846 case 29: // Proxy Authentication Type
2848 uint16_t atype
= ntohs(*(uint16_t *)b
);
2849 LOG(4, s
, t
, " Proxy Auth Type %u (%s)\n", atype
, ppp_auth_type(atype
));
2852 case 30: // Proxy Authentication Name
2855 memset(authname
, 0, sizeof(authname
));
2856 memcpy(authname
, b
, (n
< sizeof(authname
)) ? n
: sizeof(authname
) - 1);
2857 LOG(4, s
, t
, " Proxy Auth Name (%s)\n",
2861 case 31: // Proxy Authentication Challenge
2863 LOG(4, s
, t
, " Proxy Auth Challenge\n");
2866 case 32: // Proxy Authentication ID
2868 uint16_t authid
= ntohs(*(uint16_t *)(b
));
2869 LOG(4, s
, t
, " Proxy Auth ID (%u)\n", authid
);
2872 case 33: // Proxy Authentication Response
2873 LOG(4, s
, t
, " Proxy Auth Response\n");
2875 case 27: // last sent lcp
2876 { // find magic number
2877 uint8_t *p
= b
, *e
= p
+ n
;
2878 while (p
+ 1 < e
&& p
[1] && p
+ p
[1] <= e
)
2880 if (*p
== 5 && p
[1] == 6) // Magic-Number
2881 amagic
= ntohl(*(uint32_t *) (p
+ 2));
2882 else if (*p
== 7) // Protocol-Field-Compression
2883 aflags
|= SESSION_PFC
;
2884 else if (*p
== 8) // Address-and-Control-Field-Compression
2885 aflags
|= SESSION_ACFC
;
2890 case 28: // last recv lcp confreq
2892 case 26: // Initial Received LCP CONFREQ
2894 case 39: // seq required - we control it as an LNS anyway...
2896 case 36: // Random Vector
2897 LOG(4, s
, t
, " Random Vector received. Enabled AVP Hiding.\n");
2898 memset(session
[s
].random_vector
, 0, sizeof(session
[s
].random_vector
));
2899 if (n
> sizeof(session
[s
].random_vector
))
2900 n
= sizeof(session
[s
].random_vector
);
2901 memcpy(session
[s
].random_vector
, b
, n
);
2902 session
[s
].random_vector_length
= n
;
2904 case 46: // ppp disconnect cause
2907 uint16_t code
= ntohs(*(uint16_t *) b
);
2908 uint16_t proto
= ntohs(*(uint16_t *) (b
+ 2));
2909 uint8_t dir
= *(b
+ 4);
2911 LOG(4, s
, t
, " PPP disconnect cause "
2912 "(code=%u, proto=%04X, dir=%u, msg=\"%.*s\")\n",
2913 code
, proto
, dir
, n
- 5, b
+ 5);
2915 disc_cause_set
= mtype
;
2919 case 1: // admin disconnect
2920 disc_cause
= TERM_ADMIN_RESET
;
2921 disc_reason
= "Administrative disconnect";
2923 case 3: // lcp terminate
2924 if (dir
!= 2) break; // 1=peer (LNS), 2=local (LAC)
2925 disc_cause
= TERM_USER_REQUEST
;
2926 disc_reason
= "Normal disconnection";
2928 case 4: // compulsory encryption unavailable
2929 if (dir
!= 1) break; // 1=refused by peer, 2=local
2930 disc_cause
= TERM_USER_ERROR
;
2931 disc_reason
= "Compulsory encryption refused";
2933 case 5: // lcp: fsm timeout
2934 disc_cause
= TERM_PORT_ERROR
;
2935 disc_reason
= "LCP: FSM timeout";
2937 case 6: // lcp: no recognisable lcp packets received
2938 disc_cause
= TERM_PORT_ERROR
;
2939 disc_reason
= "LCP: no recognisable LCP packets";
2941 case 7: // lcp: magic-no error (possibly looped back)
2942 disc_cause
= TERM_PORT_ERROR
;
2943 disc_reason
= "LCP: magic-no error (possible loop)";
2945 case 8: // lcp: echo request timeout
2946 disc_cause
= TERM_PORT_ERROR
;
2947 disc_reason
= "LCP: echo request timeout";
2949 case 13: // auth: fsm timeout
2950 disc_cause
= TERM_SERVICE_UNAVAILABLE
;
2951 disc_reason
= "Authentication: FSM timeout";
2953 case 15: // auth: unacceptable auth protocol
2954 disc_cause
= TERM_SERVICE_UNAVAILABLE
;
2955 disc_reason
= "Unacceptable authentication protocol";
2957 case 16: // auth: authentication failed
2958 disc_cause
= TERM_SERVICE_UNAVAILABLE
;
2959 disc_reason
= "Authentication failed";
2961 case 17: // ncp: fsm timeout
2962 disc_cause
= TERM_SERVICE_UNAVAILABLE
;
2963 disc_reason
= "NCP: FSM timeout";
2965 case 18: // ncp: no ncps available
2966 disc_cause
= TERM_SERVICE_UNAVAILABLE
;
2967 disc_reason
= "NCP: no NCPs available";
2969 case 19: // ncp: failure to converge on acceptable address
2970 disc_cause
= TERM_SERVICE_UNAVAILABLE
;
2971 disc_reason
= (dir
== 1)
2972 ? "NCP: too many Configure-Naks received from peer"
2973 : "NCP: too many Configure-Naks sent to peer";
2975 case 20: // ncp: user not permitted to use any address
2976 disc_cause
= TERM_SERVICE_UNAVAILABLE
;
2977 disc_reason
= (dir
== 1)
2978 ? "NCP: local link address not acceptable to peer"
2979 : "NCP: remote link address not acceptable";
2986 static char e
[] = "unknown AVP 0xXXXX";
2987 LOG(2, s
, t
, " Unknown AVP type %u\n", mtype
);
2989 result
= 2; // general error
2990 error
= 8; // unknown mandatory AVP
2991 sprintf((msg
= e
) + 14, "%04x", mtype
);
2998 tunnelshutdown(t
, "Invalid mandatory AVP", result
, error
, msg
);
3002 case 1: // SCCRQ - Start Control Connection Request
3003 tunnel
[t
].state
= TUNNELOPENING
;
3004 LOG(3, s
, t
, "Received SCCRQ\n");
3005 if (main_quit
!= QUIT_SHUTDOWN
)
3007 LOG(3, s
, t
, "sending SCCRP\n");
3008 controlt
*c
= controlnew(2); // sending SCCRP
3009 control16(c
, 2, version
, 1); // protocol version
3010 control32(c
, 3, 3, 1); // framing
3011 controls(c
, 7, hostname
, 1); // host name
3012 if (chapresponse
) controlb(c
, 13, chapresponse
, 16, 1); // Challenge response
3013 control16(c
, 9, t
, 1); // assigned tunnel
3014 controladd(c
, 0, t
); // send the resply
3018 tunnelshutdown(t
, "Shutting down", 6, 0, 0);
3022 tunnel
[t
].state
= TUNNELOPEN
;
3023 tunnel
[t
].lastrec
= time_now
;
3025 LOG(3, s
, t
, "Received SCCRP\n");
3026 if (main_quit
!= QUIT_SHUTDOWN
)
3028 if (tunnel
[t
].isremotelns
&& chapresponse
)
3032 lac_calc_rlns_auth(t
, 2, hash
); // id = 2 (SCCRP)
3033 // check authenticator
3034 if (memcmp(hash
, chapresponse
, 16) == 0)
3036 LOG(3, s
, t
, "sending SCCCN to REMOTE LNS\n");
3037 controlt
*c
= controlnew(3); // sending SCCCN
3038 controls(c
, 7, hostname
, 1); // host name
3039 controls(c
, 8, Vendor_name
, 1); // Vendor name
3040 control16(c
, 2, version
, 1); // protocol version
3041 control32(c
, 3, 3, 1); // framing Capabilities
3042 control16(c
, 9, t
, 1); // assigned tunnel
3043 controladd(c
, 0, t
); // send
3047 tunnelshutdown(t
, "Bad chap response from REMOTE LNS", 4, 0, 0);
3053 tunnelshutdown(t
, "Shutting down", 6, 0, 0);
3058 LOG(3, s
, t
, "Received SCCN\n");
3059 tunnel
[t
].state
= TUNNELOPEN
;
3060 tunnel
[t
].lastrec
= time_now
;
3061 controlnull(t
); // ack
3064 LOG(3, s
, t
, "Received StopCCN\n");
3065 controlnull(t
); // ack
3066 tunnelshutdown(t
, "Stopped", 0, 0, 0); // Shut down cleanly
3069 LOG(3, s
, t
, "Received HELLO\n");
3070 controlnull(t
); // simply ACK
3074 LOG(3, s
, t
, "Received OCRQ\n");
3078 LOG(3, s
, t
, "Received OCRO\n");
3082 LOG(3, s
, t
, "Received OCCN\n");
3085 LOG(3, s
, t
, "Received ICRQ\n");
3086 if (sessionfree
&& main_quit
!= QUIT_SHUTDOWN
)
3088 controlt
*c
= controlnew(11); // ICRP
3090 LOG(3, s
, t
, "Sending ICRP\n");
3093 sessionfree
= session
[s
].next
;
3094 memset(&session
[s
], 0, sizeof(session
[s
]));
3096 if (s
> config
->cluster_highest_sessionid
)
3097 config
->cluster_highest_sessionid
= s
;
3099 session
[s
].opened
= time_now
;
3100 session
[s
].tunnel
= t
;
3101 session
[s
].far
= asession
;
3102 session
[s
].last_packet
= session
[s
].last_data
= time_now
;
3103 LOG(3, s
, t
, "New session (%u/%u)\n", tunnel
[t
].far
, session
[s
].far
);
3104 control16(c
, 14, s
, 1); // assigned session
3105 controladd(c
, asession
, t
); // send the reply
3107 strncpy(session
[s
].called
, called
, sizeof(session
[s
].called
) - 1);
3108 strncpy(session
[s
].calling
, calling
, sizeof(session
[s
].calling
) - 1);
3110 session
[s
].ppp
.phase
= Establish
;
3111 session
[s
].ppp
.lcp
= Starting
;
3113 STAT(session_created
);
3118 controlt
*c
= controlnew(14); // CDN
3119 LOG(3, s
, t
, "Sending CDN\n");
3122 STAT(session_overflow
);
3123 LOG(1, 0, t
, "No free sessions\n");
3124 control16(c
, 1, 4, 0); // temporary lack of resources
3127 control16(c
, 1, 2, 7); // shutting down, try another
3129 controladd(c
, asession
, t
); // send the message
3134 LOG(3, s
, t
, "Received ICRP\n");
3135 if (session
[s
].forwardtosession
)
3137 controlt
*c
= controlnew(12); // ICCN
3139 session
[s
].opened
= time_now
;
3140 session
[s
].tunnel
= t
;
3141 session
[s
].far
= asession
;
3142 session
[s
].last_packet
= session
[s
].last_data
= time_now
;
3144 control32(c
, 19, 1, 1); // Framing Type
3145 control32(c
, 24, 10000000, 1); // Tx Connect Speed
3146 controladd(c
, asession
, t
); // send the message
3147 LOG(3, s
, t
, "Sending ICCN\n");
3152 LOG(3, s
, t
, "Received ICCN\n");
3153 if (amagic
== 0) amagic
= time_now
;
3154 session
[s
].magic
= amagic
; // set magic number
3155 session
[s
].flags
= aflags
; // set flags received
3156 session
[s
].mru
= PPPoE_MRU
; // default
3157 controlnull(t
); // ack
3160 sess_local
[s
].lcp_authtype
= config
->radius_authprefer
;
3161 sess_local
[s
].ppp_mru
= MRU
;
3163 // Set multilink options before sending initial LCP packet
3164 sess_local
[s
].mp_mrru
= 1614;
3165 sess_local
[s
].mp_epdis
= ntohl(config
->iftun_address
? config
->iftun_address
: my_address
);
3168 change_state(s
, lcp
, RequestSent
);
3172 LOG(3, s
, t
, "Received CDN\n");
3173 controlnull(t
); // ack
3174 sessionshutdown(s
, disc_reason
, CDN_NONE
, disc_cause
);
3177 LOG(1, s
, t
, "Missing message type\n");
3180 STAT(tunnel_rx_errors
);
3182 tunnelshutdown(t
, "Unknown message type", 2, 6, "unknown message type");
3184 LOG(1, s
, t
, "Unknown message type %u\n", message
);
3187 if (chapresponse
) free(chapresponse
);
3188 cluster_send_tunnel(t
);
3192 LOG(4, s
, t
, " Got a ZLB ack\n");
3199 LOG_HEX(5, "Receive Tunnel Data", p
, l
);
3200 if (l
> 2 && p
[0] == 0xFF && p
[1] == 0x03)
3201 { // HDLC address header, discard
3207 LOG(1, s
, t
, "Short ppp length %d\n", l
);
3208 STAT(tunnel_rx_errors
);
3218 proto
= ntohs(*(uint16_t *) p
);
3224 if (session
[s
].forwardtosession
)
3226 LOG(5, s
, t
, "Forwarding data session to session %u\n", session
[s
].forwardtosession
);
3227 // Forward to LAC or Remote LNS session
3228 lac_session_forward(buf
, len
, s
, proto
, addr
->sin_addr
.s_addr
, addr
->sin_port
);
3233 if (s
&& !session
[s
].opened
) // Is something wrong??
3235 if (!config
->cluster_iam_master
)
3237 // Pass it off to the master to deal with..
3238 master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
);
3242 LOG(1, s
, t
, "UDP packet contains session which is not opened. Dropping packet.\n");
3243 STAT(tunnel_rx_errors
);
3247 if (proto
== PPPPAP
)
3249 session
[s
].last_packet
= time_now
;
3250 if (!config
->cluster_iam_master
) { master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
); return; }
3251 processpap(s
, t
, p
, l
);
3253 else if (proto
== PPPCHAP
)
3255 session
[s
].last_packet
= time_now
;
3256 if (!config
->cluster_iam_master
) { master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
); return; }
3257 processchap(s
, t
, p
, l
);
3259 else if (proto
== PPPLCP
)
3261 session
[s
].last_packet
= time_now
;
3262 if (!config
->cluster_iam_master
) { master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
); return; }
3263 processlcp(s
, t
, p
, l
);
3265 else if (proto
== PPPIPCP
)
3267 session
[s
].last_packet
= time_now
;
3268 if (!config
->cluster_iam_master
) { master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
); return; }
3269 processipcp(s
, t
, p
, l
);
3271 else if (proto
== PPPIPV6CP
&& config
->ipv6_prefix
.s6_addr
[0])
3273 session
[s
].last_packet
= time_now
;
3274 if (!config
->cluster_iam_master
) { master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
); return; }
3275 processipv6cp(s
, t
, p
, l
);
3277 else if (proto
== PPPCCP
)
3279 session
[s
].last_packet
= time_now
;
3280 if (!config
->cluster_iam_master
) { master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
); return; }
3281 processccp(s
, t
, p
, l
);
3283 else if (proto
== PPPIP
)
3287 LOG(4, s
, t
, "Session %u is closing. Don't process PPP packets\n", s
);
3288 return; // closing session, PPP not processed
3291 session
[s
].last_packet
= session
[s
].last_data
= time_now
;
3292 if (session
[s
].walled_garden
&& !config
->cluster_iam_master
)
3294 master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
);
3298 processipin(s
, t
, p
, l
);
3300 else if (proto
== PPPMP
)
3304 LOG(4, s
, t
, "Session %u is closing. Don't process PPP packets\n", s
);
3305 return; // closing session, PPP not processed
3308 session
[s
].last_packet
= session
[s
].last_data
= time_now
;
3309 if (!config
->cluster_iam_master
)
3311 // The fragments reconstruction is managed by the Master.
3312 master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
);
3316 processmpin(s
, t
, p
, l
);
3318 else if (proto
== PPPIPV6
&& config
->ipv6_prefix
.s6_addr
[0])
3322 LOG(4, s
, t
, "Session %u is closing. Don't process PPP packets\n", s
);
3323 return; // closing session, PPP not processed
3326 session
[s
].last_packet
= session
[s
].last_data
= time_now
;
3327 if (session
[s
].walled_garden
&& !config
->cluster_iam_master
)
3329 master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
);
3333 processipv6in(s
, t
, p
, l
);
3335 else if (session
[s
].ppp
.lcp
== Opened
)
3337 session
[s
].last_packet
= time_now
;
3338 if (!config
->cluster_iam_master
) { master_forward_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
); return; }
3339 protoreject(s
, t
, p
, l
, proto
);
3343 LOG(2, s
, t
, "Unknown PPP protocol 0x%04X received in LCP %s state\n",
3344 proto
, ppp_state(session
[s
].ppp
.lcp
));
3349 // read and process packet on tun
3350 // (i.e. this routine writes to buf[-8]).
3351 static void processtun(uint8_t * buf
, int len
)
3353 LOG_HEX(5, "Receive TUN Data", buf
, len
);
3354 STAT(tun_rx_packets
);
3355 INC_STAT(tun_rx_bytes
, len
);
3363 LOG(1, 0, 0, "Short tun packet %d bytes\n", len
);
3364 STAT(tun_rx_errors
);
3368 if (*(uint16_t *) (buf
+ 2) == htons(PKTIP
)) // IPv4
3369 processipout(buf
, len
);
3370 else if (*(uint16_t *) (buf
+ 2) == htons(PKTIPV6
) // IPV6
3371 && config
->ipv6_prefix
.s6_addr
[0])
3372 processipv6out(buf
, len
);
3377 // Handle retries, timeouts. Runs every 1/10th sec, want to ensure
3378 // that we look at the whole of the tunnel, radius and session tables
3380 static void regular_cleanups(double period
)
3382 // Next tunnel, radius and session to check for actions on.
3383 static tunnelidt t
= 0;
3385 static sessionidt s
= 0;
3398 // divide up tables into slices based on the last run
3399 t_slice
= config
->cluster_highest_tunnelid
* period
;
3400 r_slice
= (MAXRADIUS
- 1) * period
;
3401 s_slice
= config
->cluster_highest_sessionid
* period
;
3405 else if (t_slice
> config
->cluster_highest_tunnelid
)
3406 t_slice
= config
->cluster_highest_tunnelid
;
3410 else if (r_slice
> (MAXRADIUS
- 1))
3411 r_slice
= MAXRADIUS
- 1;
3415 else if (s_slice
> config
->cluster_highest_sessionid
)
3416 s_slice
= config
->cluster_highest_sessionid
;
3418 LOG(4, 0, 0, "Begin regular cleanup (last %f seconds ago)\n", period
);
3420 for (i
= 0; i
< t_slice
; i
++)
3423 if (t
> config
->cluster_highest_tunnelid
)
3426 // check for expired tunnels
3427 if (tunnel
[t
].die
&& tunnel
[t
].die
<= TIME
)
3429 STAT(tunnel_timeout
);
3430 tunnelkill(t
, "Expired");
3434 // check for message resend
3435 if (tunnel
[t
].retry
&& tunnel
[t
].controlc
)
3437 // resend pending messages as timeout on reply
3438 if (tunnel
[t
].retry
<= TIME
)
3440 controlt
*c
= tunnel
[t
].controls
;
3441 uint16_t w
= tunnel
[t
].window
;
3442 tunnel
[t
].try++; // another try
3443 if (tunnel
[t
].try > 5)
3444 tunnelkill(t
, "Timeout on control message"); // game over
3448 tunnelsend(c
->buf
, c
->length
, t
);
3456 if (tunnel
[t
].state
== TUNNELOPEN
&& !tunnel
[t
].controlc
&& (time_now
- tunnel
[t
].lastrec
) > 60)
3458 controlt
*c
= controlnew(6); // sending HELLO
3459 controladd(c
, 0, t
); // send the message
3460 LOG(3, 0, t
, "Sending HELLO message\n");
3464 // Check for tunnel changes requested from the CLI
3465 if ((a
= cli_tunnel_actions
[t
].action
))
3467 cli_tunnel_actions
[t
].action
= 0;
3468 if (a
& CLI_TUN_KILL
)
3470 LOG(2, 0, t
, "Dropping tunnel by CLI\n");
3471 tunnelshutdown(t
, "Requested by administrator", 1, 0, 0);
3477 for (i
= 0; i
< r_slice
; i
++)
3483 if (!radius
[r
].state
)
3486 if (radius
[r
].retry
<= TIME
)
3493 for (i
= 0; i
< s_slice
; i
++)
3496 if (s
> config
->cluster_highest_sessionid
)
3499 if (!session
[s
].opened
) // Session isn't in use
3502 // check for expired sessions
3505 if (session
[s
].die
<= TIME
)
3507 sessionkill(s
, "Expired");
3514 if (sess_local
[s
].lcp
.restart
<= time_now
)
3516 int next_state
= session
[s
].ppp
.lcp
;
3517 switch (session
[s
].ppp
.lcp
)
3521 next_state
= RequestSent
;
3524 if (sess_local
[s
].lcp
.conf_sent
< config
->ppp_max_configure
)
3526 LOG(3, s
, session
[s
].tunnel
, "No ACK for LCP ConfigReq... resending\n");
3527 sendlcp(s
, session
[s
].tunnel
);
3528 change_state(s
, lcp
, next_state
);
3532 sessionshutdown(s
, "No response to LCP ConfigReq.", CDN_ADMIN_DISC
, TERM_LOST_SERVICE
);
3533 STAT(session_timeout
);
3543 if (sess_local
[s
].ipcp
.restart
<= time_now
)
3545 int next_state
= session
[s
].ppp
.ipcp
;
3546 switch (session
[s
].ppp
.ipcp
)
3550 next_state
= RequestSent
;
3553 if (sess_local
[s
].ipcp
.conf_sent
< config
->ppp_max_configure
)
3555 LOG(3, s
, session
[s
].tunnel
, "No ACK for IPCP ConfigReq... resending\n");
3556 sendipcp(s
, session
[s
].tunnel
);
3557 change_state(s
, ipcp
, next_state
);
3561 sessionshutdown(s
, "No response to IPCP ConfigReq.", CDN_ADMIN_DISC
, TERM_LOST_SERVICE
);
3562 STAT(session_timeout
);
3572 if (sess_local
[s
].ipv6cp
.restart
<= time_now
)
3574 int next_state
= session
[s
].ppp
.ipv6cp
;
3575 switch (session
[s
].ppp
.ipv6cp
)
3579 next_state
= RequestSent
;
3582 if (sess_local
[s
].ipv6cp
.conf_sent
< config
->ppp_max_configure
)
3584 LOG(3, s
, session
[s
].tunnel
, "No ACK for IPV6CP ConfigReq... resending\n");
3585 sendipv6cp(s
, session
[s
].tunnel
);
3586 change_state(s
, ipv6cp
, next_state
);
3590 LOG(3, s
, session
[s
].tunnel
, "No ACK for IPV6CP ConfigReq\n");
3591 change_state(s
, ipv6cp
, Stopped
);
3598 if (sess_local
[s
].ccp
.restart
<= time_now
)
3600 int next_state
= session
[s
].ppp
.ccp
;
3601 switch (session
[s
].ppp
.ccp
)
3605 next_state
= RequestSent
;
3608 if (sess_local
[s
].ccp
.conf_sent
< config
->ppp_max_configure
)
3610 LOG(3, s
, session
[s
].tunnel
, "No ACK for CCP ConfigReq... resending\n");
3611 sendccp(s
, session
[s
].tunnel
);
3612 change_state(s
, ccp
, next_state
);
3616 LOG(3, s
, session
[s
].tunnel
, "No ACK for CCP ConfigReq\n");
3617 change_state(s
, ccp
, Stopped
);
3624 // Drop sessions who have not responded within IDLE_ECHO_TIMEOUT seconds
3625 if (session
[s
].last_packet
&& (time_now
- session
[s
].last_packet
>= config
->idle_echo_timeout
))
3627 sessionshutdown(s
, "No response to LCP ECHO requests.", CDN_ADMIN_DISC
, TERM_LOST_SERVICE
);
3628 STAT(session_timeout
);
3633 // No data in ECHO_TIMEOUT seconds, send LCP ECHO
3634 if (session
[s
].ppp
.phase
>= Establish
&& (time_now
- session
[s
].last_packet
>= config
->echo_timeout
) &&
3635 (time_now
- sess_local
[s
].last_echo
>= ECHO_TIMEOUT
))
3637 uint8_t b
[MAXETHER
];
3639 uint8_t *q
= makeppp(b
, sizeof(b
), 0, 0, s
, session
[s
].tunnel
, PPPLCP
, 1, 0, 0);
3643 *(uint8_t *)(q
+ 1) = (time_now
% 255); // ID
3644 *(uint16_t *)(q
+ 2) = htons(8); // Length
3645 *(uint32_t *)(q
+ 4) = session
[s
].ppp
.lcp
== Opened
? htonl(session
[s
].magic
) : 0; // Magic Number
3647 LOG(4, s
, session
[s
].tunnel
, "No data in %d seconds, sending LCP ECHO\n",
3648 (int)(time_now
- session
[s
].last_packet
));
3649 tunnelsend(b
, 24, session
[s
].tunnel
); // send it
3650 sess_local
[s
].last_echo
= time_now
;
3654 // Drop sessions who have reached session_timeout seconds
3655 if (session
[s
].session_timeout
)
3657 bundleidt bid
= session
[s
].bundle
;
3660 if (time_now
- bundle
[bid
].last_check
>= 1)
3662 bundle
[bid
].online_time
+= (time_now
- bundle
[bid
].last_check
) * bundle
[bid
].num_of_links
;
3663 bundle
[bid
].last_check
= time_now
;
3664 if (bundle
[bid
].online_time
>= session
[s
].session_timeout
)
3667 for (ses
= bundle
[bid
].num_of_links
- 1; ses
>= 0; ses
--)
3669 sessionshutdown(bundle
[bid
].members
[ses
], "Session timeout", CDN_ADMIN_DISC
, TERM_SESSION_TIMEOUT
);
3676 else if (time_now
- session
[s
].opened
>= session
[s
].session_timeout
)
3678 sessionshutdown(s
, "Session timeout", CDN_ADMIN_DISC
, TERM_SESSION_TIMEOUT
);
3684 // Drop sessions who have reached idle_timeout seconds
3685 if (session
[s
].last_data
&& session
[s
].idle_timeout
&& (time_now
- session
[s
].last_data
>= session
[s
].idle_timeout
))
3687 sessionshutdown(s
, "Idle Timeout Reached", CDN_ADMIN_DISC
, TERM_IDLE_TIMEOUT
);
3688 STAT(session_timeout
);
3693 // Check for actions requested from the CLI
3694 if ((a
= cli_session_actions
[s
].action
))
3698 cli_session_actions
[s
].action
= 0;
3699 if (a
& CLI_SESS_KILL
)
3701 LOG(2, s
, session
[s
].tunnel
, "Dropping session by CLI\n");
3702 sessionshutdown(s
, "Requested by administrator.", CDN_ADMIN_DISC
, TERM_ADMIN_RESET
);
3703 a
= 0; // dead, no need to check for other actions
3707 if (a
& CLI_SESS_NOSNOOP
)
3709 LOG(2, s
, session
[s
].tunnel
, "Unsnooping session by CLI\n");
3710 session
[s
].snoop_ip
= 0;
3711 session
[s
].snoop_port
= 0;
3715 else if (a
& CLI_SESS_SNOOP
)
3717 LOG(2, s
, session
[s
].tunnel
, "Snooping session by CLI (to %s:%u)\n",
3718 fmtaddr(cli_session_actions
[s
].snoop_ip
, 0),
3719 cli_session_actions
[s
].snoop_port
);
3721 session
[s
].snoop_ip
= cli_session_actions
[s
].snoop_ip
;
3722 session
[s
].snoop_port
= cli_session_actions
[s
].snoop_port
;
3727 if (a
& CLI_SESS_NOTHROTTLE
)
3729 LOG(2, s
, session
[s
].tunnel
, "Un-throttling session by CLI\n");
3730 throttle_session(s
, 0, 0);
3734 else if (a
& CLI_SESS_THROTTLE
)
3736 LOG(2, s
, session
[s
].tunnel
, "Throttling session by CLI (to %dkb/s up and %dkb/s down)\n",
3737 cli_session_actions
[s
].throttle_in
,
3738 cli_session_actions
[s
].throttle_out
);
3740 throttle_session(s
, cli_session_actions
[s
].throttle_in
, cli_session_actions
[s
].throttle_out
);
3745 if (a
& CLI_SESS_NOFILTER
)
3747 LOG(2, s
, session
[s
].tunnel
, "Un-filtering session by CLI\n");
3748 filter_session(s
, 0, 0);
3752 else if (a
& CLI_SESS_FILTER
)
3754 LOG(2, s
, session
[s
].tunnel
, "Filtering session by CLI (in=%d, out=%d)\n",
3755 cli_session_actions
[s
].filter_in
,
3756 cli_session_actions
[s
].filter_out
);
3758 filter_session(s
, cli_session_actions
[s
].filter_in
, cli_session_actions
[s
].filter_out
);
3764 cluster_send_session(s
);
3767 // RADIUS interim accounting
3768 if (config
->radius_accounting
&& config
->radius_interim
> 0
3769 && session
[s
].ip
&& !session
[s
].walled_garden
3770 && !sess_local
[s
].radius
// RADIUS already in progress
3771 && time_now
- sess_local
[s
].last_interim
>= config
->radius_interim
3772 && session
[s
].flags
& SESSION_STARTED
)
3774 int rad
= radiusnew(s
);
3777 LOG(1, s
, session
[s
].tunnel
, "No free RADIUS sessions for Interim message\n");
3778 STAT(radius_overflow
);
3782 LOG(3, s
, session
[s
].tunnel
, "Sending RADIUS Interim for %s (%u)\n",
3783 session
[s
].user
, session
[s
].unique_id
);
3785 radiussend(rad
, RADIUSINTERIM
);
3786 sess_local
[s
].last_interim
= time_now
;
3791 LOG(4, 0, 0, "End regular cleanup: checked %d/%d/%d tunnels/radius/sessions; %d/%d/%d actions\n",
3792 t_slice
, r_slice
, s_slice
, t_actions
, r_actions
, s_actions
);
3796 // Are we in the middle of a tunnel update, or radius
3799 static int still_busy(void)
3802 static clockt last_talked
= 0;
3803 static clockt start_busy_wait
= 0;
3806 static time_t stopped_bgp
= 0;
3811 LOG(1, 0, 0, "Shutting down in %d seconds, stopping BGP...\n", QUIT_DELAY
);
3813 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
3814 if (bgp_peers
[i
].state
== Established
)
3815 bgp_stop(&bgp_peers
[i
]);
3817 stopped_bgp
= time_now
;
3819 if (!config
->cluster_iam_master
)
3821 // we don't want to become master
3822 cluster_send_ping(0);
3828 if (!config
->cluster_iam_master
&& time_now
< (stopped_bgp
+ QUIT_DELAY
))
3833 if (!config
->cluster_iam_master
)
3836 if (main_quit
== QUIT_SHUTDOWN
)
3838 static int dropped
= 0;
3843 LOG(1, 0, 0, "Dropping sessions and tunnels\n");
3844 for (i
= 1; i
< MAXTUNNEL
; i
++)
3845 if (tunnel
[i
].ip
|| tunnel
[i
].state
)
3846 tunnelshutdown(i
, "L2TPNS Closing", 6, 0, 0);
3852 if (start_busy_wait
== 0)
3853 start_busy_wait
= TIME
;
3855 for (i
= config
->cluster_highest_tunnelid
; i
> 0 ; --i
)
3857 if (!tunnel
[i
].controlc
)
3860 if (last_talked
!= TIME
)
3862 LOG(2, 0, 0, "Tunnel %u still has un-acked control messages.\n", i
);
3868 // We stop waiting for radius after BUSY_WAIT_TIME 1/10th seconds
3869 if (abs(TIME
- start_busy_wait
) > BUSY_WAIT_TIME
)
3871 LOG(1, 0, 0, "Giving up waiting for RADIUS to be empty. Shutting down anyway.\n");
3875 for (i
= 1; i
< MAXRADIUS
; i
++)
3877 if (radius
[i
].state
== RADIUSNULL
)
3879 if (radius
[i
].state
== RADIUSWAIT
)
3882 if (last_talked
!= TIME
)
3884 LOG(2, 0, 0, "Radius session %u is still busy (sid %u)\n", i
, radius
[i
].session
);
3894 # include <sys/epoll.h>
3896 # define FAKE_EPOLL_IMPLEMENTATION /* include the functions */
3897 # include "fake_epoll.h"
3901 // the base set of fds polled: cli, cluster, tun, udp, control, dae, netlink, udplac
3904 // the base set of fds polled: cli, cluster, tun, udp, control, dae, netlink
3908 // additional polled fds
3910 # define EXTRA_FDS BGP_NUM_PEERS
3912 # define EXTRA_FDS 0
3915 // main loop - gets packets on tun or udp and processes them
3916 static void mainloop(void)
3920 uint8_t *p
= buf
+ 8; // for the hearder of the forwarded MPPP packet (see C_MPPP_FORWARD)
3921 int size_bufp
= sizeof(buf
) - 8;
3922 clockt next_cluster_ping
= 0; // send initial ping immediately
3923 struct epoll_event events
[BASE_FDS
+ RADIUS_FDS
+ EXTRA_FDS
];
3924 int maxevent
= sizeof(events
)/sizeof(*events
);
3926 if ((epollfd
= epoll_create(maxevent
)) < 0)
3928 LOG(0, 0, 0, "epoll_create failed: %s\n", strerror(errno
));
3933 LOG(4, 0, 0, "Beginning of main loop. clifd=%d, cluster_sockfd=%d, tunfd=%d, udpfd=%d, controlfd=%d, daefd=%d, nlfd=%d , udplacfd=%d\n",
3934 clifd
, cluster_sockfd
, tunfd
, udpfd
, controlfd
, daefd
, nlfd
, udplacfd
);
3936 LOG(4, 0, 0, "Beginning of main loop. clifd=%d, cluster_sockfd=%d, tunfd=%d, udpfd=%d, controlfd=%d, daefd=%d, nlfd=%d\n",
3937 clifd
, cluster_sockfd
, tunfd
, udpfd
, controlfd
, daefd
, nlfd
);
3940 /* setup our fds to poll for input */
3942 static struct event_data d
[BASE_FDS
];
3943 struct epoll_event e
;
3950 d
[i
].type
= FD_TYPE_CLI
;
3951 e
.data
.ptr
= &d
[i
++];
3952 epoll_ctl(epollfd
, EPOLL_CTL_ADD
, clifd
, &e
);
3955 d
[i
].type
= FD_TYPE_CLUSTER
;
3956 e
.data
.ptr
= &d
[i
++];
3957 epoll_ctl(epollfd
, EPOLL_CTL_ADD
, cluster_sockfd
, &e
);
3959 d
[i
].type
= FD_TYPE_TUN
;
3960 e
.data
.ptr
= &d
[i
++];
3961 epoll_ctl(epollfd
, EPOLL_CTL_ADD
, tunfd
, &e
);
3963 d
[i
].type
= FD_TYPE_UDP
;
3964 e
.data
.ptr
= &d
[i
++];
3965 epoll_ctl(epollfd
, EPOLL_CTL_ADD
, udpfd
, &e
);
3967 d
[i
].type
= FD_TYPE_CONTROL
;
3968 e
.data
.ptr
= &d
[i
++];
3969 epoll_ctl(epollfd
, EPOLL_CTL_ADD
, controlfd
, &e
);
3971 d
[i
].type
= FD_TYPE_DAE
;
3972 e
.data
.ptr
= &d
[i
++];
3973 epoll_ctl(epollfd
, EPOLL_CTL_ADD
, daefd
, &e
);
3975 d
[i
].type
= FD_TYPE_NETLINK
;
3976 e
.data
.ptr
= &d
[i
++];
3977 epoll_ctl(epollfd
, EPOLL_CTL_ADD
, nlfd
, &e
);
3980 d
[i
].type
= FD_TYPE_UDPLAC
;
3981 e
.data
.ptr
= &d
[i
++];
3982 epoll_ctl(epollfd
, EPOLL_CTL_ADD
, udplacfd
, &e
);
3987 signal(SIGPIPE
, SIG_IGN
);
3988 bgp_setup(config
->as_number
);
3989 if (config
->bind_address
)
3990 bgp_add_route(config
->bind_address
, 0xffffffff);
3992 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
3994 if (config
->neighbour
[i
].name
[0])
3995 bgp_start(&bgp_peers
[i
], config
->neighbour
[i
].name
,
3996 config
->neighbour
[i
].as
, config
->neighbour
[i
].keepalive
,
3997 config
->neighbour
[i
].hold
, config
->neighbour
[i
].update_source
,
3998 0); /* 0 = routing disabled */
4002 while (!main_quit
|| still_busy())
4012 config
->reload_config
++;
4015 if (config
->reload_config
)
4017 config
->reload_config
= 0;
4025 n
= epoll_wait(epollfd
, events
, maxevent
, 100); // timeout 100ms (1/10th sec)
4026 STAT(select_called
);
4031 if (errno
== EINTR
||
4032 errno
== ECHILD
) // EINTR was clobbered by sigchild_handler()
4035 LOG(0, 0, 0, "Error returned from select(): %s\n", strerror(errno
));
4041 struct sockaddr_in addr
;
4042 struct in_addr local
;
4047 int udplac_ready
= 0;
4048 int udplac_pkts
= 0;
4051 int cluster_ready
= 0;
4054 int cluster_pkts
= 0;
4056 uint32_t bgp_events
[BGP_NUM_PEERS
];
4057 memset(bgp_events
, 0, sizeof(bgp_events
));
4060 for (c
= n
, i
= 0; i
< c
; i
++)
4062 struct event_data
*d
= events
[i
].data
.ptr
;
4066 case FD_TYPE_CLI
: // CLI connections
4070 alen
= sizeof(addr
);
4071 if ((cli
= accept(clifd
, (struct sockaddr
*)&addr
, &alen
)) >= 0)
4077 LOG(0, 0, 0, "accept error: %s\n", strerror(errno
));
4083 // these are handled below, with multiple interleaved reads
4084 case FD_TYPE_CLUSTER
: cluster_ready
++; break;
4085 case FD_TYPE_TUN
: tun_ready
++; break;
4086 case FD_TYPE_UDP
: udp_ready
++; break;
4088 case FD_TYPE_UDPLAC
: udplac_ready
++; break;
4090 case FD_TYPE_CONTROL
: // nsctl commands
4091 alen
= sizeof(addr
);
4092 s
= recvfromto(controlfd
, buf
, sizeof(buf
), MSG_WAITALL
, (struct sockaddr
*) &addr
, &alen
, &local
);
4093 if (s
> 0) processcontrol(buf
, s
, &addr
, alen
, &local
);
4097 case FD_TYPE_DAE
: // DAE requests
4098 alen
= sizeof(addr
);
4099 s
= recvfromto(daefd
, buf
, sizeof(buf
), MSG_WAITALL
, (struct sockaddr
*) &addr
, &alen
, &local
);
4100 if (s
> 0) processdae(buf
, s
, &addr
, alen
, &local
);
4104 case FD_TYPE_RADIUS
: // RADIUS response
4105 alen
= sizeof(addr
);
4106 s
= recvfrom(radfds
[d
->index
], buf
, sizeof(buf
), MSG_WAITALL
, (struct sockaddr
*) &addr
, &alen
);
4107 if (s
>= 0 && config
->cluster_iam_master
)
4109 if (addr
.sin_addr
.s_addr
== config
->radiusserver
[0] ||
4110 addr
.sin_addr
.s_addr
== config
->radiusserver
[1])
4111 processrad(buf
, s
, d
->index
);
4113 LOG(3, 0, 0, "Dropping RADIUS packet from unknown source %s\n",
4114 fmtaddr(addr
.sin_addr
.s_addr
, 0));
4122 bgp_events
[d
->index
] = events
[i
].events
;
4127 case FD_TYPE_NETLINK
:
4129 struct nlmsghdr
*nh
= (struct nlmsghdr
*)buf
;
4130 s
= netlink_recv(buf
, sizeof(buf
));
4131 if (nh
->nlmsg_type
== NLMSG_ERROR
)
4133 struct nlmsgerr
*errmsg
= NLMSG_DATA(nh
);
4136 if (errmsg
->msg
.nlmsg_seq
< min_initok_nlseqnum
)
4138 LOG(0, 0, 0, "Got a fatal netlink error (while %s): %s\n", tun_nl_phase_msg
[nh
->nlmsg_seq
], strerror(-errmsg
->error
));
4143 LOG(0, 0, 0, "Got a netlink error: %s\n", strerror(-errmsg
->error
));
4148 LOG(1, 0, 0, "Got a unknown netlink message: type %d seq %d flags %d\n", nh
->nlmsg_type
, nh
->nlmsg_seq
, nh
->nlmsg_flags
);
4154 LOG(0, 0, 0, "Unexpected fd type returned from epoll_wait: %d\n", d
->type
);
4159 bgp_process(bgp_events
);
4162 for (c
= 0; n
&& c
< config
->multi_read_count
; c
++)
4167 alen
= sizeof(addr
);
4168 if ((s
= recvfrom(udpfd
, buf
, sizeof(buf
), 0, (void *) &addr
, &alen
)) > 0)
4170 processudp(buf
, s
, &addr
);
4183 alen
= sizeof(addr
);
4184 if ((s
= recvfrom(udplacfd
, buf
, sizeof(buf
), 0, (void *) &addr
, &alen
)) > 0)
4186 if (!config
->disable_lac_func
)
4187 processudp(buf
, s
, &addr
);
4201 if ((s
= read(tunfd
, p
, size_bufp
)) > 0)
4216 alen
= sizeof(addr
);
4217 if ((s
= recvfrom(cluster_sockfd
, buf
, sizeof(buf
), MSG_WAITALL
, (void *) &addr
, &alen
)) > 0)
4219 processcluster(buf
, s
, addr
.sin_addr
.s_addr
);
4230 if (udp_pkts
> 1 || tun_pkts
> 1 || cluster_pkts
> 1)
4231 STAT(multi_read_used
);
4233 if (c
>= config
->multi_read_count
)
4236 LOG(3, 0, 0, "Reached multi_read_count (%d); processed %d udp, %d tun %d cluster and %d rmlns packets\n",
4237 config
->multi_read_count
, udp_pkts
, tun_pkts
, cluster_pkts
, udplac_pkts
);
4239 LOG(3, 0, 0, "Reached multi_read_count (%d); processed %d udp, %d tun and %d cluster packets\n",
4240 config
->multi_read_count
, udp_pkts
, tun_pkts
, cluster_pkts
);
4242 STAT(multi_read_exceeded
);
4248 /* no event received, but timers could still have expired */
4249 bgp_process_peers_timers();
4254 double Mbps
= 1024.0 * 1024.0 / 8 * time_changed
;
4256 // Log current traffic stats
4257 snprintf(config
->bandwidth
, sizeof(config
->bandwidth
),
4258 "UDP-ETH:%1.0f/%1.0f ETH-UDP:%1.0f/%1.0f TOTAL:%0.1f IN:%u OUT:%u",
4259 (udp_rx
/ Mbps
), (eth_tx
/ Mbps
), (eth_rx
/ Mbps
), (udp_tx
/ Mbps
),
4260 ((udp_tx
+ udp_rx
+ eth_tx
+ eth_rx
) / Mbps
),
4261 udp_rx_pkt
/ time_changed
, eth_rx_pkt
/ time_changed
);
4263 udp_tx
= udp_rx
= 0;
4264 udp_rx_pkt
= eth_rx_pkt
= 0;
4265 eth_tx
= eth_rx
= 0;
4268 if (config
->dump_speed
)
4269 printf("%s\n", config
->bandwidth
);
4271 // Update the internal time counter
4272 strftime(time_now_string
, sizeof(time_now_string
), "%Y-%m-%d %H:%M:%S", localtime(&time_now
));
4276 struct param_timer p
= { time_now
};
4277 run_plugins(PLUGIN_TIMER
, &p
);
4281 // Runs on every machine (master and slaves).
4282 if (next_cluster_ping
<= TIME
)
4284 // Check to see which of the cluster is still alive..
4286 cluster_send_ping(basetime
); // Only does anything if we're a slave
4287 cluster_check_master(); // ditto.
4289 cluster_heartbeat(); // Only does anything if we're a master.
4290 cluster_check_slaves(); // ditto.
4292 master_update_counts(); // If we're a slave, send our byte counters to our master.
4294 if (config
->cluster_iam_master
&& !config
->cluster_iam_uptodate
)
4295 next_cluster_ping
= TIME
+ 1; // out-of-date slaves, do fast updates
4297 next_cluster_ping
= TIME
+ config
->cluster_hb_interval
;
4300 if (!config
->cluster_iam_master
)
4303 // Run token bucket filtering queue..
4304 // Only run it every 1/10th of a second.
4306 static clockt last_run
= 0;
4307 if (last_run
!= TIME
)
4314 // Handle timeouts, retries etc.
4316 static double last_clean
= 0;
4320 TIME
= now(&this_clean
);
4321 diff
= this_clean
- last_clean
;
4323 // Run during idle time (after we've handled
4324 // all incoming packets) or every 1/10th sec
4325 if (!more
|| diff
> 0.1)
4327 regular_cleanups(diff
);
4328 last_clean
= this_clean
;
4332 if (*config
->accounting_dir
)
4334 static clockt next_acct
= 0;
4335 static clockt next_shut_acct
= 0;
4337 if (next_acct
<= TIME
)
4339 // Dump accounting data
4340 next_acct
= TIME
+ ACCT_TIME
;
4341 next_shut_acct
= TIME
+ ACCT_SHUT_TIME
;
4344 else if (next_shut_acct
<= TIME
)
4346 // Dump accounting data for shutdown sessions
4347 next_shut_acct
= TIME
+ ACCT_SHUT_TIME
;
4354 // Are we the master and shutting down??
4355 if (config
->cluster_iam_master
)
4356 cluster_heartbeat(); // Flush any queued changes..
4358 // Ok. Notify everyone we're shutting down. If we're
4359 // the master, this will force an election.
4360 cluster_send_ping(0);
4363 // Important!!! We MUST not process any packets past this point!
4364 LOG(1, 0, 0, "Shutdown complete\n");
4367 static void stripdomain(char *host
)
4371 if ((p
= strchr(host
, '.')))
4377 FILE *resolv
= fopen("/etc/resolv.conf", "r");
4383 while (fgets(buf
, sizeof(buf
), resolv
))
4385 if (strncmp(buf
, "domain", 6) && strncmp(buf
, "search", 6))
4388 if (!isspace(buf
[6]))
4392 while (isspace(*b
)) b
++;
4397 while (*b
&& !isspace(*b
)) b
++;
4399 if (buf
[0] == 'd') // domain is canonical
4405 // first search line
4408 // hold, may be subsequent domain line
4409 strncpy(_domain
, d
, sizeof(_domain
))[sizeof(_domain
)-1] = 0;
4420 int hl
= strlen(host
);
4421 int dl
= strlen(domain
);
4422 if (dl
< hl
&& host
[hl
- dl
- 1] == '.' && !strcmp(host
+ hl
- dl
, domain
))
4423 host
[hl
-dl
- 1] = 0;
4427 *p
= 0; // everything after first dot
4432 // Init data structures
4433 static void initdata(int optdebug
, char *optconfig
)
4437 if (!(config
= shared_malloc(sizeof(configt
))))
4439 fprintf(stderr
, "Error doing malloc for configuration: %s\n", strerror(errno
));
4443 memset(config
, 0, sizeof(configt
));
4444 time(&config
->start_time
);
4445 strncpy(config
->config_file
, optconfig
, strlen(optconfig
));
4446 config
->debug
= optdebug
;
4447 config
->num_tbfs
= MAXTBFS
;
4448 config
->rl_rate
= 28; // 28kbps
4449 config
->cluster_mcast_ttl
= 1;
4450 config
->cluster_master_min_adv
= 1;
4451 config
->ppp_restart_time
= 3;
4452 config
->ppp_max_configure
= 10;
4453 config
->ppp_max_failure
= 5;
4454 config
->kill_timedout_sessions
= 1;
4455 strcpy(config
->random_device
, RANDOMDEVICE
);
4456 // Set default value echo_timeout and idle_echo_timeout
4457 config
->echo_timeout
= ECHO_TIMEOUT
;
4458 config
->idle_echo_timeout
= IDLE_ECHO_TIMEOUT
;
4460 log_stream
= stderr
;
4463 if (!(ringbuffer
= shared_malloc(sizeof(struct Tringbuffer
))))
4465 LOG(0, 0, 0, "Error doing malloc for ringbuffer: %s\n", strerror(errno
));
4468 memset(ringbuffer
, 0, sizeof(struct Tringbuffer
));
4471 if (!(_statistics
= shared_malloc(sizeof(struct Tstats
))))
4473 LOG(0, 0, 0, "Error doing malloc for _statistics: %s\n", strerror(errno
));
4476 if (!(tunnel
= shared_malloc(sizeof(tunnelt
) * MAXTUNNEL
)))
4478 LOG(0, 0, 0, "Error doing malloc for tunnels: %s\n", strerror(errno
));
4481 if (!(bundle
= shared_malloc(sizeof(bundlet
) * MAXBUNDLE
)))
4483 LOG(0, 0, 0, "Error doing malloc for bundles: %s\n", strerror(errno
));
4486 if (!(frag
= shared_malloc(sizeof(fragmentationt
) * MAXBUNDLE
)))
4488 LOG(0, 0, 0, "Error doing malloc for fragmentations: %s\n", strerror(errno
));
4491 if (!(session
= shared_malloc(sizeof(sessiont
) * MAXSESSION
)))
4493 LOG(0, 0, 0, "Error doing malloc for sessions: %s\n", strerror(errno
));
4497 if (!(sess_local
= shared_malloc(sizeof(sessionlocalt
) * MAXSESSION
)))
4499 LOG(0, 0, 0, "Error doing malloc for sess_local: %s\n", strerror(errno
));
4503 if (!(radius
= shared_malloc(sizeof(radiust
) * MAXRADIUS
)))
4505 LOG(0, 0, 0, "Error doing malloc for radius: %s\n", strerror(errno
));
4509 if (!(ip_address_pool
= shared_malloc(sizeof(ippoolt
) * MAXIPPOOL
)))
4511 LOG(0, 0, 0, "Error doing malloc for ip_address_pool: %s\n", strerror(errno
));
4515 if (!(ip_filters
= shared_malloc(sizeof(ip_filtert
) * MAXFILTER
)))
4517 LOG(0, 0, 0, "Error doing malloc for ip_filters: %s\n", strerror(errno
));
4520 memset(ip_filters
, 0, sizeof(ip_filtert
) * MAXFILTER
);
4522 if (!(cli_session_actions
= shared_malloc(sizeof(struct cli_session_actions
) * MAXSESSION
)))
4524 LOG(0, 0, 0, "Error doing malloc for cli session actions: %s\n", strerror(errno
));
4527 memset(cli_session_actions
, 0, sizeof(struct cli_session_actions
) * MAXSESSION
);
4529 if (!(cli_tunnel_actions
= shared_malloc(sizeof(struct cli_tunnel_actions
) * MAXSESSION
)))
4531 LOG(0, 0, 0, "Error doing malloc for cli tunnel actions: %s\n", strerror(errno
));
4534 memset(cli_tunnel_actions
, 0, sizeof(struct cli_tunnel_actions
) * MAXSESSION
);
4536 memset(tunnel
, 0, sizeof(tunnelt
) * MAXTUNNEL
);
4537 memset(bundle
, 0, sizeof(bundlet
) * MAXBUNDLE
);
4538 memset(session
, 0, sizeof(sessiont
) * MAXSESSION
);
4539 memset(radius
, 0, sizeof(radiust
) * MAXRADIUS
);
4540 memset(ip_address_pool
, 0, sizeof(ippoolt
) * MAXIPPOOL
);
4542 // Put all the sessions on the free list marked as undefined.
4543 for (i
= 1; i
< MAXSESSION
; i
++)
4545 session
[i
].next
= i
+ 1;
4546 session
[i
].tunnel
= T_UNDEF
; // mark it as not filled in.
4548 session
[MAXSESSION
- 1].next
= 0;
4551 // Mark all the tunnels as undefined (waiting to be filled in by a download).
4552 for (i
= 1; i
< MAXTUNNEL
; i
++)
4553 tunnel
[i
].state
= TUNNELUNDEF
; // mark it as not filled in.
4555 for (i
= 1; i
< MAXBUNDLE
; i
++) {
4556 bundle
[i
].state
= BUNDLEUNDEF
;
4561 // Grab my hostname unless it's been specified
4562 gethostname(hostname
, sizeof(hostname
));
4563 stripdomain(hostname
);
4566 _statistics
->start_time
= _statistics
->last_reset
= time(NULL
);
4569 if (!(bgp_peers
= shared_malloc(sizeof(struct bgp_peer
) * BGP_NUM_PEERS
)))
4571 LOG(0, 0, 0, "Error doing malloc for bgp: %s\n", strerror(errno
));
4577 lac_initremotelnsdata();
4581 static int assign_ip_address(sessionidt s
)
4585 time_t best_time
= time_now
;
4586 char *u
= session
[s
].user
;
4590 CSTAT(assign_ip_address
);
4592 for (i
= 1; i
< ip_pool_size
; i
++)
4594 if (!ip_address_pool
[i
].address
|| ip_address_pool
[i
].assigned
)
4597 if (!session
[s
].walled_garden
&& ip_address_pool
[i
].user
[0] && !strcmp(u
, ip_address_pool
[i
].user
))
4604 if (ip_address_pool
[i
].last
< best_time
)
4607 if (!(best_time
= ip_address_pool
[i
].last
))
4608 break; // never used, grab this one
4614 LOG(0, s
, session
[s
].tunnel
, "assign_ip_address(): out of addresses\n");
4618 session
[s
].ip
= ip_address_pool
[best
].address
;
4619 session
[s
].ip_pool_index
= best
;
4620 ip_address_pool
[best
].assigned
= 1;
4621 ip_address_pool
[best
].last
= time_now
;
4622 ip_address_pool
[best
].session
= s
;
4623 if (session
[s
].walled_garden
)
4624 /* Don't track addresses of users in walled garden (note: this
4625 means that their address isn't "sticky" even if they get
4627 ip_address_pool
[best
].user
[0] = 0;
4629 strncpy(ip_address_pool
[best
].user
, u
, sizeof(ip_address_pool
[best
].user
) - 1);
4632 LOG(4, s
, session
[s
].tunnel
, "assign_ip_address(): %s ip address %d from pool\n",
4633 reuse
? "Reusing" : "Allocating", best
);
4638 static void free_ip_address(sessionidt s
)
4640 int i
= session
[s
].ip_pool_index
;
4643 CSTAT(free_ip_address
);
4646 return; // what the?
4648 if (i
< 0) // Is this actually part of the ip pool?
4652 cache_ipmap(session
[s
].ip
, -i
); // Change the mapping to point back to the ip pool index.
4654 ip_address_pool
[i
].assigned
= 0;
4655 ip_address_pool
[i
].session
= 0;
4656 ip_address_pool
[i
].last
= time_now
;
4660 // Fsck the address pool against the session table.
4661 // Normally only called when we become a master.
4663 // This isn't perfect: We aren't keep tracking of which
4664 // users used to have an IP address.
4666 void rebuild_address_pool(void)
4671 // Zero the IP pool allocation, and build
4672 // a map from IP address to pool index.
4673 for (i
= 1; i
< MAXIPPOOL
; ++i
)
4675 ip_address_pool
[i
].assigned
= 0;
4676 ip_address_pool
[i
].session
= 0;
4677 if (!ip_address_pool
[i
].address
)
4680 cache_ipmap(ip_address_pool
[i
].address
, -i
); // Map pool IP to pool index.
4683 for (i
= 0; i
< MAXSESSION
; ++i
)
4686 if (!(session
[i
].opened
&& session
[i
].ip
))
4689 ipid
= - lookup_ipmap(htonl(session
[i
].ip
));
4691 if (session
[i
].ip_pool_index
< 0)
4693 // Not allocated out of the pool.
4694 if (ipid
< 1) // Not found in the pool either? good.
4697 LOG(0, i
, 0, "Session %u has an IP address (%s) that was marked static, but is in the pool (%d)!\n",
4698 i
, fmtaddr(session
[i
].ip
, 0), ipid
);
4700 // Fall through and process it as part of the pool.
4704 if (ipid
> MAXIPPOOL
|| ipid
< 0)
4706 LOG(0, i
, 0, "Session %u has a pool IP that's not found in the pool! (%d)\n", i
, ipid
);
4708 session
[i
].ip_pool_index
= ipid
;
4712 ip_address_pool
[ipid
].assigned
= 1;
4713 ip_address_pool
[ipid
].session
= i
;
4714 ip_address_pool
[ipid
].last
= time_now
;
4715 strncpy(ip_address_pool
[ipid
].user
, session
[i
].user
, sizeof(ip_address_pool
[ipid
].user
) - 1);
4716 session
[i
].ip_pool_index
= ipid
;
4717 cache_ipmap(session
[i
].ip
, i
); // Fix the ip map.
4722 // Fix the address pool to match a changed session.
4723 // (usually when the master sends us an update).
4724 static void fix_address_pool(int sid
)
4728 ipid
= session
[sid
].ip_pool_index
;
4730 if (ipid
> ip_pool_size
)
4731 return; // Ignore it. rebuild_address_pool will fix it up.
4733 if (ip_address_pool
[ipid
].address
!= session
[sid
].ip
)
4734 return; // Just ignore it. rebuild_address_pool will take care of it.
4736 ip_address_pool
[ipid
].assigned
= 1;
4737 ip_address_pool
[ipid
].session
= sid
;
4738 ip_address_pool
[ipid
].last
= time_now
;
4739 strncpy(ip_address_pool
[ipid
].user
, session
[sid
].user
, sizeof(ip_address_pool
[ipid
].user
) - 1);
4743 // Add a block of addresses to the IP pool to hand out.
4745 static void add_to_ip_pool(in_addr_t addr
, int prefixlen
)
4749 prefixlen
= 32; // Host route only.
4751 addr
&= 0xffffffff << (32 - prefixlen
);
4753 if (ip_pool_size
>= MAXIPPOOL
) // Pool is full!
4756 for (i
= addr
; i
< addr
+(1<<(32-prefixlen
)); ++i
)
4758 if ((i
& 0xff) == 0 || (i
&0xff) == 255)
4759 continue; // Skip 0 and broadcast addresses.
4761 ip_address_pool
[ip_pool_size
].address
= i
;
4762 ip_address_pool
[ip_pool_size
].assigned
= 0;
4764 if (ip_pool_size
>= MAXIPPOOL
)
4766 LOG(0, 0, 0, "Overflowed IP pool adding %s\n", fmtaddr(htonl(addr
), 0));
4772 // Initialize the IP address pool
4773 static void initippool()
4778 memset(ip_address_pool
, 0, sizeof(ip_address_pool
));
4780 if (!(f
= fopen(IPPOOLFILE
, "r")))
4782 LOG(0, 0, 0, "Can't load pool file " IPPOOLFILE
": %s\n", strerror(errno
));
4786 while (ip_pool_size
< MAXIPPOOL
&& fgets(buf
, 4096, f
))
4789 buf
[4095] = 0; // Force it to be zero terminated/
4791 if (*buf
== '#' || *buf
== '\n')
4792 continue; // Skip comments / blank lines
4793 if ((p
= (char *)strrchr(buf
, '\n'))) *p
= 0;
4794 if ((p
= (char *)strchr(buf
, ':')))
4798 src
= inet_addr(buf
);
4799 if (src
== INADDR_NONE
)
4801 LOG(0, 0, 0, "Invalid address pool IP %s\n", buf
);
4804 // This entry is for a specific IP only
4805 if (src
!= config
->bind_address
)
4810 if ((p
= (char *)strchr(pool
, '/')))
4814 in_addr_t start
= 0;
4816 LOG(2, 0, 0, "Adding IP address range %s\n", buf
);
4818 if (!*p
|| !(numbits
= atoi(p
)))
4820 LOG(0, 0, 0, "Invalid pool range %s\n", buf
);
4823 start
= ntohl(inet_addr(pool
));
4825 // Add a static route for this pool
4826 LOG(5, 0, 0, "Adding route for address pool %s/%d\n",
4827 fmtaddr(htonl(start
), 0), numbits
);
4829 routeset(0, start
, numbits
, 0, 1);
4831 add_to_ip_pool(start
, numbits
);
4835 // It's a single ip address
4836 add_to_ip_pool(ntohl(inet_addr(pool
)), 0);
4840 LOG(1, 0, 0, "IP address pool is %d addresses\n", ip_pool_size
- 1);
4843 void snoop_send_packet(uint8_t *packet
, uint16_t size
, in_addr_t destination
, uint16_t port
)
4845 struct sockaddr_in snoop_addr
= {0};
4846 if (!destination
|| !port
|| snoopfd
<= 0 || size
<= 0 || !packet
)
4849 snoop_addr
.sin_family
= AF_INET
;
4850 snoop_addr
.sin_addr
.s_addr
= destination
;
4851 snoop_addr
.sin_port
= ntohs(port
);
4853 LOG(5, 0, 0, "Snooping %d byte packet to %s:%u\n", size
,
4854 fmtaddr(snoop_addr
.sin_addr
.s_addr
, 0),
4855 htons(snoop_addr
.sin_port
));
4857 if (sendto(snoopfd
, packet
, size
, MSG_DONTWAIT
| MSG_NOSIGNAL
, (void *) &snoop_addr
, sizeof(snoop_addr
)) < 0)
4858 LOG(0, 0, 0, "Error sending intercept packet: %s\n", strerror(errno
));
4860 STAT(packets_snooped
);
4863 static int dump_session(FILE **f
, sessiont
*s
)
4866 if (!s
->opened
|| (!s
->ip
&& !s
->forwardtosession
) || !(s
->cin_delta
|| s
->cout_delta
) || !*s
->user
|| s
->walled_garden
)
4868 if (!s
->opened
|| !s
->ip
|| !(s
->cin_delta
|| s
->cout_delta
) || !*s
->user
|| s
->walled_garden
)
4874 char filename
[1024];
4876 time_t now
= time(NULL
);
4878 strftime(timestr
, sizeof(timestr
), "%Y%m%d%H%M%S", localtime(&now
));
4879 snprintf(filename
, sizeof(filename
), "%s/%s", config
->accounting_dir
, timestr
);
4881 if (!(*f
= fopen(filename
, "w")))
4883 LOG(0, 0, 0, "Can't write accounting info to %s: %s\n", filename
, strerror(errno
));
4887 LOG(3, 0, 0, "Dumping accounting information to %s\n", filename
);
4888 fprintf(*f
, "# dslwatch.pl dump file V1.01\n"
4893 "# format: username ip qos uptxoctets downrxoctets\n",
4895 fmtaddr(config
->iftun_address
? config
->iftun_address
: my_address
, 0),
4900 LOG(4, 0, 0, "Dumping accounting information for %s\n", s
->user
);
4901 fprintf(*f
, "%s %s %d %u %u\n",
4902 s
->user
, // username
4903 fmtaddr(htonl(s
->ip
), 0), // ip
4904 (s
->throttle_in
|| s
->throttle_out
) ? 2 : 1, // qos
4905 (uint32_t) s
->cin_delta
, // uptxoctets
4906 (uint32_t) s
->cout_delta
); // downrxoctets
4908 s
->cin_delta
= s
->cout_delta
= 0;
4913 static void dump_acct_info(int all
)
4919 CSTAT(dump_acct_info
);
4923 for (i
= 0; i
< shut_acct_n
; i
++)
4924 dump_session(&f
, &shut_acct
[i
]);
4930 for (i
= 1; i
<= config
->cluster_highest_sessionid
; i
++)
4931 dump_session(&f
, &session
[i
]);
4938 int main(int argc
, char *argv
[])
4942 char *optconfig
= CONFIGFILE
;
4944 time(&basetime
); // start clock
4947 while ((i
= getopt(argc
, argv
, "dvc:h:")) >= 0)
4952 if (fork()) exit(0);
4954 freopen("/dev/null", "r", stdin
);
4955 freopen("/dev/null", "w", stdout
);
4956 freopen("/dev/null", "w", stderr
);
4965 snprintf(hostname
, sizeof(hostname
), "%s", optarg
);
4968 printf("Args are:\n"
4969 "\t-d\t\tDetach from terminal\n"
4970 "\t-c <file>\tConfig file\n"
4971 "\t-h <hostname>\tForce hostname\n"
4979 // Start the timer routine off
4981 strftime(time_now_string
, sizeof(time_now_string
), "%Y-%m-%d %H:%M:%S", localtime(&time_now
));
4984 initdata(optdebug
, optconfig
);
4988 /* set hostname /after/ having read the config file */
4989 if (*config
->hostname
)
4990 strcpy(hostname
, config
->hostname
);
4991 cli_init_complete(hostname
);
4993 init_tbf(config
->num_tbfs
);
4995 LOG(0, 0, 0, "L2TPNS version " VERSION
"\n");
4996 LOG(0, 0, 0, "Copyright (c) 2003, 2004, 2005, 2006 Optus Internet Engineering\n");
4997 LOG(0, 0, 0, "Copyright (c) 2002 FireBrick (Andrews & Arnold Ltd / Watchfront Ltd) - GPL licenced\n");
5000 rlim
.rlim_cur
= RLIM_INFINITY
;
5001 rlim
.rlim_max
= RLIM_INFINITY
;
5002 // Remove the maximum core size
5003 if (setrlimit(RLIMIT_CORE
, &rlim
) < 0)
5004 LOG(0, 0, 0, "Can't set ulimit: %s\n", strerror(errno
));
5006 // Make core dumps go to /tmp
5010 if (config
->scheduler_fifo
)
5013 struct sched_param params
= {0};
5014 params
.sched_priority
= 1;
5016 if (get_nprocs() < 2)
5018 LOG(0, 0, 0, "Not using FIFO scheduler, there is only 1 processor in the system.\n");
5019 config
->scheduler_fifo
= 0;
5023 if ((ret
= sched_setscheduler(0, SCHED_FIFO
, ¶ms
)) == 0)
5025 LOG(1, 0, 0, "Using FIFO scheduler. Say goodbye to any other processes running\n");
5029 LOG(0, 0, 0, "Error setting scheduler to FIFO: %s\n", strerror(errno
));
5030 config
->scheduler_fifo
= 0;
5037 /* Set up the cluster communications port. */
5038 if (cluster_init() < 0)
5042 LOG(1, 0, 0, "Set up on interface %s\n", config
->tundevice
);
5050 unsigned seed
= time_now
^ getpid();
5051 LOG(4, 0, 0, "Seeding the pseudo random generator: %u\n", seed
);
5055 signal(SIGHUP
, sighup_handler
);
5056 signal(SIGCHLD
, sigchild_handler
);
5057 signal(SIGTERM
, shutdown_handler
);
5058 signal(SIGINT
, shutdown_handler
);
5059 signal(SIGQUIT
, shutdown_handler
);
5061 // Prevent us from getting paged out
5062 if (config
->lock_pages
)
5064 if (!mlockall(MCL_CURRENT
))
5065 LOG(1, 0, 0, "Locking pages into memory\n");
5067 LOG(0, 0, 0, "Can't lock pages: %s\n", strerror(errno
));
5072 /* remove plugins (so cleanup code gets run) */
5075 // Remove the PID file if we wrote it
5076 if (config
->wrote_pid
&& *config
->pid_file
== '/')
5077 unlink(config
->pid_file
);
5079 /* kill CLI children */
5080 signal(SIGTERM
, SIG_IGN
);
5085 static void sighup_handler(int sig
)
5090 static void shutdown_handler(int sig
)
5092 main_quit
= (sig
== SIGQUIT
) ? QUIT_SHUTDOWN
: QUIT_FAILOVER
;
5095 static void sigchild_handler(int sig
)
5097 while (waitpid(-1, NULL
, WNOHANG
) > 0)
5101 static void build_chap_response(uint8_t *challenge
, uint8_t id
, uint16_t challenge_length
, uint8_t **challenge_response
)
5104 *challenge_response
= NULL
;
5106 if (!*config
->l2tp_secret
)
5108 LOG(0, 0, 0, "LNS requested CHAP authentication, but no l2tp secret is defined\n");
5112 LOG(4, 0, 0, " Building challenge response for CHAP request\n");
5114 *challenge_response
= calloc(17, 1);
5117 MD5_Update(&ctx
, &id
, 1);
5118 MD5_Update(&ctx
, config
->l2tp_secret
, strlen(config
->l2tp_secret
));
5119 MD5_Update(&ctx
, challenge
, challenge_length
);
5120 MD5_Final(*challenge_response
, &ctx
);
5125 static int facility_value(char *name
)
5128 for (i
= 0; facilitynames
[i
].c_name
; i
++)
5130 if (strcmp(facilitynames
[i
].c_name
, name
) == 0)
5131 return facilitynames
[i
].c_val
;
5136 static void update_config()
5140 static int timeout
= 0;
5141 static int interval
= 0;
5148 if (log_stream
!= stderr
)
5154 if (*config
->log_filename
)
5156 if (strstr(config
->log_filename
, "syslog:") == config
->log_filename
)
5158 char *p
= config
->log_filename
+ 7;
5161 openlog("l2tpns", LOG_PID
, facility_value(p
));
5165 else if (strchr(config
->log_filename
, '/') == config
->log_filename
)
5167 if ((log_stream
= fopen((char *)(config
->log_filename
), "a")))
5169 fseek(log_stream
, 0, SEEK_END
);
5170 setbuf(log_stream
, NULL
);
5174 log_stream
= stderr
;
5175 setbuf(log_stream
, NULL
);
5181 log_stream
= stderr
;
5182 setbuf(log_stream
, NULL
);
5185 #define L2TP_HDRS (20+8+6+4) // L2TP data encaptulation: ip + udp + l2tp (data) + ppp (inc hdlc)
5186 #define TCP_HDRS (20+20) // TCP encapsulation: ip + tcp
5188 if (config
->l2tp_mtu
<= 0) config
->l2tp_mtu
= 1500; // ethernet default
5189 else if (config
->l2tp_mtu
< MINMTU
) config
->l2tp_mtu
= MINMTU
;
5190 else if (config
->l2tp_mtu
> MAXMTU
) config
->l2tp_mtu
= MAXMTU
;
5192 // reset MRU/MSS globals
5193 MRU
= config
->l2tp_mtu
- L2TP_HDRS
;
5194 if (MRU
> PPPoE_MRU
)
5197 MSS
= MRU
- TCP_HDRS
;
5200 config
->numradiusservers
= 0;
5201 for (i
= 0; i
< MAXRADSERVER
; i
++)
5202 if (config
->radiusserver
[i
])
5204 config
->numradiusservers
++;
5205 // Set radius port: if not set, take the port from the
5206 // first radius server. For the first radius server,
5207 // take the #defined default value from l2tpns.h
5209 // test twice, In case someone works with
5210 // a secondary radius server without defining
5211 // a primary one, this will work even then.
5212 if (i
> 0 && !config
->radiusport
[i
])
5213 config
->radiusport
[i
] = config
->radiusport
[i
-1];
5214 if (!config
->radiusport
[i
])
5215 config
->radiusport
[i
] = RADPORT
;
5218 if (!config
->numradiusservers
)
5219 LOG(0, 0, 0, "No RADIUS servers defined!\n");
5221 // parse radius_authtypes_s
5222 config
->radius_authtypes
= config
->radius_authprefer
= 0;
5223 p
= config
->radius_authtypes_s
;
5226 char *s
= strpbrk(p
, " \t,");
5232 while (*s
== ' ' || *s
== '\t')
5239 if (!strncasecmp("chap", p
, strlen(p
)))
5241 else if (!strncasecmp("pap", p
, strlen(p
)))
5244 LOG(0, 0, 0, "Invalid RADIUS authentication type \"%s\"\n", p
);
5246 config
->radius_authtypes
|= type
;
5247 if (!config
->radius_authprefer
)
5248 config
->radius_authprefer
= type
;
5253 if (!config
->radius_authtypes
)
5255 LOG(0, 0, 0, "Defaulting to PAP authentication\n");
5256 config
->radius_authtypes
= config
->radius_authprefer
= AUTHPAP
;
5259 // normalise radius_authtypes_s
5260 if (config
->radius_authprefer
== AUTHPAP
)
5262 strcpy(config
->radius_authtypes_s
, "pap");
5263 if (config
->radius_authtypes
& AUTHCHAP
)
5264 strcat(config
->radius_authtypes_s
, ", chap");
5268 strcpy(config
->radius_authtypes_s
, "chap");
5269 if (config
->radius_authtypes
& AUTHPAP
)
5270 strcat(config
->radius_authtypes_s
, ", pap");
5273 if (!config
->radius_dae_port
)
5274 config
->radius_dae_port
= DAEPORT
;
5277 if(!config
->bind_portremotelns
)
5278 config
->bind_portremotelns
= L2TPLACPORT
;
5279 if(!config
->bind_address_remotelns
)
5280 config
->bind_address_remotelns
= INADDR_ANY
;
5282 if(!config
->iftun_address
)
5283 config
->iftun_address
= config
->bind_address
;
5285 // re-initialise the random number source
5286 initrandom(config
->random_device
);
5289 for (i
= 0; i
< MAXPLUGINS
; i
++)
5291 if (strcmp(config
->plugins
[i
], config
->old_plugins
[i
]) == 0)
5294 if (*config
->plugins
[i
])
5297 add_plugin(config
->plugins
[i
]);
5299 else if (*config
->old_plugins
[i
])
5302 remove_plugin(config
->old_plugins
[i
]);
5307 guest_accounts_num
= 0;
5308 char *p2
= config
->guest_user
;
5311 char *s
= strpbrk(p2
, " \t,");
5315 while (*s
== ' ' || *s
== '\t')
5322 strcpy(guest_users
[guest_accounts_num
], p2
);
5323 LOG(1, 0, 0, "Guest account[%d]: %s\n", guest_accounts_num
, guest_users
[guest_accounts_num
]);
5324 guest_accounts_num
++;
5327 // Rebuild the guest_user array
5328 strcpy(config
->guest_user
, "");
5330 for (ui
=0; ui
<guest_accounts_num
; ui
++)
5332 strcat(config
->guest_user
, guest_users
[ui
]);
5333 if (ui
<guest_accounts_num
-1)
5335 strcat(config
->guest_user
, ",");
5340 memcpy(config
->old_plugins
, config
->plugins
, sizeof(config
->plugins
));
5341 if (!config
->multi_read_count
) config
->multi_read_count
= 10;
5342 if (!config
->cluster_address
) config
->cluster_address
= inet_addr(DEFAULT_MCAST_ADDR
);
5343 if (!*config
->cluster_interface
)
5344 strncpy(config
->cluster_interface
, DEFAULT_MCAST_INTERFACE
, sizeof(config
->cluster_interface
) - 1);
5346 if (!config
->cluster_hb_interval
)
5347 config
->cluster_hb_interval
= PING_INTERVAL
; // Heartbeat every 0.5 seconds.
5349 if (!config
->cluster_hb_timeout
)
5350 config
->cluster_hb_timeout
= HB_TIMEOUT
; // 10 missed heartbeat triggers an election.
5352 if (interval
!= config
->cluster_hb_interval
|| timeout
!= config
->cluster_hb_timeout
)
5354 // Paranoia: cluster_check_master() treats 2 x interval + 1 sec as
5355 // late, ensure we're sufficiently larger than that
5356 int t
= 4 * config
->cluster_hb_interval
+ 11;
5358 if (config
->cluster_hb_timeout
< t
)
5360 LOG(0, 0, 0, "Heartbeat timeout %d too low, adjusting to %d\n", config
->cluster_hb_timeout
, t
);
5361 config
->cluster_hb_timeout
= t
;
5364 // Push timing changes to the slaves immediately if we're the master
5365 if (config
->cluster_iam_master
)
5366 cluster_heartbeat();
5368 interval
= config
->cluster_hb_interval
;
5369 timeout
= config
->cluster_hb_timeout
;
5373 if (*config
->pid_file
== '/' && !config
->wrote_pid
)
5376 if ((f
= fopen(config
->pid_file
, "w")))
5378 fprintf(f
, "%d\n", getpid());
5380 config
->wrote_pid
= 1;
5384 LOG(0, 0, 0, "Can't write to PID file %s: %s\n", config
->pid_file
, strerror(errno
));
5389 static void read_config_file()
5393 if (!config
->config_file
) return;
5394 if (!(f
= fopen(config
->config_file
, "r")))
5396 fprintf(stderr
, "Can't open config file %s: %s\n", config
->config_file
, strerror(errno
));
5400 LOG(3, 0, 0, "Reading config file %s\n", config
->config_file
);
5402 LOG(3, 0, 0, "Done reading config file\n");
5406 int sessionsetup(sessionidt s
, tunnelidt t
)
5408 // A session now exists, set it up
5414 CSTAT(sessionsetup
);
5416 LOG(3, s
, t
, "Doing session setup for session\n");
5418 // Join a bundle if the MRRU option is accepted
5419 if(session
[s
].mrru
> 0 && session
[s
].bundle
== 0)
5421 LOG(3, s
, t
, "This session can be part of multilink bundle\n");
5422 if (join_bundle(s
) > 0)
5423 cluster_send_bundle(session
[s
].bundle
);
5426 LOG(0, s
, t
, "MPPP: Mismaching mssf option with other sessions in bundle\n");
5427 sessionshutdown(s
, "Mismaching mssf option.", CDN_NONE
, TERM_SERVICE_UNAVAILABLE
);
5434 assign_ip_address(s
);
5437 LOG(0, s
, t
, " No IP allocated. The IP address pool is FULL!\n");
5438 sessionshutdown(s
, "No IP addresses available.", CDN_TRY_ANOTHER
, TERM_SERVICE_UNAVAILABLE
);
5441 LOG(3, s
, t
, " No IP allocated. Assigned %s from pool\n",
5442 fmtaddr(htonl(session
[s
].ip
), 0));
5445 // Make sure this is right
5446 session
[s
].tunnel
= t
;
5448 // zap old sessions with same IP and/or username
5449 // Don't kill gardened sessions - doing so leads to a DoS
5450 // from someone who doesn't need to know the password
5453 user
= session
[s
].user
;
5454 for (i
= 1; i
<= config
->cluster_highest_sessionid
; i
++)
5456 if (i
== s
) continue;
5457 if (!session
[s
].opened
) break;
5458 // Allow duplicate sessions for multilink ones of the same bundle.
5459 if (session
[s
].bundle
&& session
[i
].bundle
&& session
[s
].bundle
== session
[i
].bundle
) continue;
5461 if (ip
== session
[i
].ip
)
5463 sessionkill(i
, "Duplicate IP address");
5464 cluster_listinvert_session(s
, i
);
5468 if (config
->allow_duplicate_users
) continue;
5469 if (session
[s
].walled_garden
|| session
[i
].walled_garden
) continue;
5473 for (gu
= 0; gu
< guest_accounts_num
; gu
++)
5475 if (!strcasecmp(user
, guest_users
[gu
]))
5481 if (found
) continue;
5483 // Drop the new session in case of duplicate sessionss, not the old one.
5484 if (!strcasecmp(user
, session
[i
].user
))
5485 sessionkill(i
, "Duplicate session for users");
5489 // no need to set a route for the same IP address of the bundle
5490 if (!session
[s
].bundle
|| (bundle
[session
[s
].bundle
].num_of_links
== 1))
5494 // Add the route for this session.
5495 for (r
= 0; r
< MAXROUTE
&& session
[s
].route
[r
].ip
; r
++)
5497 if ((session
[s
].ip
>> (32-session
[s
].route
[r
].prefixlen
)) ==
5498 (session
[s
].route
[r
].ip
>> (32-session
[s
].route
[r
].prefixlen
)))
5501 routeset(s
, session
[s
].route
[r
].ip
, session
[s
].route
[r
].prefixlen
, 0, 1);
5504 // Static IPs need to be routed if not already
5505 // convered by a Framed-Route. Anything else is part
5506 // of the IP address pool and is already routed, it
5507 // just needs to be added to the IP cache.
5508 // IPv6 route setup is done in ppp.c, when IPV6CP is acked.
5509 if (session
[s
].ip_pool_index
== -1) // static ip
5511 if (!routed
) routeset(s
, session
[s
].ip
, 0, 0, 1);
5514 cache_ipmap(session
[s
].ip
, s
);
5517 sess_local
[s
].lcp_authtype
= 0; // RADIUS authentication complete
5518 lcp_open(s
, t
); // transition to Network phase and send initial IPCP
5520 // Run the plugin's against this new session.
5522 struct param_new_session data
= { &tunnel
[t
], &session
[s
] };
5523 run_plugins(PLUGIN_NEW_SESSION
, &data
);
5526 // Allocate TBFs if throttled
5527 if (session
[s
].throttle_in
|| session
[s
].throttle_out
)
5528 throttle_session(s
, session
[s
].throttle_in
, session
[s
].throttle_out
);
5530 session
[s
].last_packet
= session
[s
].last_data
= time_now
;
5532 LOG(2, s
, t
, "Login by %s at %s from %s (%s)\n", session
[s
].user
,
5533 fmtaddr(htonl(session
[s
].ip
), 0),
5534 fmtaddr(htonl(tunnel
[t
].ip
), 1), tunnel
[t
].hostname
);
5536 cluster_send_session(s
); // Mark it as dirty, and needing to the flooded to the cluster.
5538 return 1; // RADIUS OK and IP allocated, done...
5542 // This session just got dropped on us by the master or something.
5543 // Make sure our tables up up to date...
5545 int load_session(sessionidt s
, sessiont
*new)
5551 if (new->ip_pool_index
>= MAXIPPOOL
||
5552 new->tunnel
>= MAXTUNNEL
)
5554 LOG(0, s
, 0, "Strange session update received!\n");
5555 // FIXME! What to do here?
5560 // Ok. All sanity checks passed. Now we're committed to
5561 // loading the new session.
5564 session
[s
].tunnel
= new->tunnel
; // For logging in cache_ipmap
5566 // See if routes/ip cache need updating
5567 if (new->ip
!= session
[s
].ip
)
5570 for (i
= 0; !newip
&& i
< MAXROUTE
&& (session
[s
].route
[i
].ip
|| new->route
[i
].ip
); i
++)
5571 if (new->route
[i
].ip
!= session
[s
].route
[i
].ip
||
5572 new->route
[i
].prefixlen
!= session
[s
].route
[i
].prefixlen
)
5580 // remove old routes...
5581 for (i
= 0; i
< MAXROUTE
&& session
[s
].route
[i
].ip
; i
++)
5583 if ((session
[s
].ip
>> (32-session
[s
].route
[i
].prefixlen
)) ==
5584 (session
[s
].route
[i
].ip
>> (32-session
[s
].route
[i
].prefixlen
)))
5587 routeset(s
, session
[s
].route
[i
].ip
, session
[s
].route
[i
].prefixlen
, 0, 0);
5593 if (session
[s
].ip_pool_index
== -1) // static IP
5595 if (!routed
) routeset(s
, session
[s
].ip
, 0, 0, 0);
5597 else // It's part of the IP pool, remove it manually.
5598 uncache_ipmap(session
[s
].ip
);
5603 // add new routes...
5604 for (i
= 0; i
< MAXROUTE
&& new->route
[i
].ip
; i
++)
5606 if ((new->ip
>> (32-new->route
[i
].prefixlen
)) ==
5607 (new->route
[i
].ip
>> (32-new->route
[i
].prefixlen
)))
5610 routeset(s
, new->route
[i
].ip
, new->route
[i
].prefixlen
, 0, 1);
5616 // If there's a new one, add it.
5617 if (new->ip_pool_index
== -1)
5619 if (!routed
) routeset(s
, new->ip
, 0, 0, 1);
5622 cache_ipmap(new->ip
, s
);
5627 if (new->ipv6prefixlen
&& new->ppp
.ipv6cp
== Opened
&& session
[s
].ppp
.ipv6cp
!= Opened
)
5628 route6set(s
, new->ipv6route
, new->ipv6prefixlen
, 1);
5631 if (new->filter_in
&& (new->filter_in
> MAXFILTER
|| !ip_filters
[new->filter_in
- 1].name
[0]))
5633 LOG(2, s
, session
[s
].tunnel
, "Dropping invalid input filter %u\n", (int) new->filter_in
);
5637 if (new->filter_out
&& (new->filter_out
> MAXFILTER
|| !ip_filters
[new->filter_out
- 1].name
[0]))
5639 LOG(2, s
, session
[s
].tunnel
, "Dropping invalid output filter %u\n", (int) new->filter_out
);
5640 new->filter_out
= 0;
5643 if (new->filter_in
!= session
[s
].filter_in
)
5645 if (session
[s
].filter_in
) ip_filters
[session
[s
].filter_in
- 1].used
--;
5646 if (new->filter_in
) ip_filters
[new->filter_in
- 1].used
++;
5649 if (new->filter_out
!= session
[s
].filter_out
)
5651 if (session
[s
].filter_out
) ip_filters
[session
[s
].filter_out
- 1].used
--;
5652 if (new->filter_out
) ip_filters
[new->filter_out
- 1].used
++;
5655 if (new->tunnel
&& s
> config
->cluster_highest_sessionid
) // Maintain this in the slave. It's used
5656 // for walking the sessions to forward byte counts to the master.
5657 config
->cluster_highest_sessionid
= s
;
5659 memcpy(&session
[s
], new, sizeof(session
[s
])); // Copy over..
5661 // Do fixups into address pool.
5662 if (new->ip_pool_index
!= -1)
5663 fix_address_pool(s
);
5668 static void initplugins()
5672 loaded_plugins
= ll_init();
5673 // Initialize the plugins to nothing
5674 for (i
= 0; i
< MAX_PLUGIN_TYPES
; i
++)
5675 plugins
[i
] = ll_init();
5678 static void *open_plugin(char *plugin_name
, int load
)
5680 char path
[256] = "";
5682 snprintf(path
, 256, PLUGINDIR
"/%s.so", plugin_name
);
5683 LOG(2, 0, 0, "%soading plugin from %s\n", load
? "L" : "Un-l", path
);
5684 return dlopen(path
, RTLD_NOW
);
5687 // plugin callback to get a config value
5688 static void *getconfig(char *key
, enum config_typet type
)
5692 for (i
= 0; config_values
[i
].key
; i
++)
5694 if (!strcmp(config_values
[i
].key
, key
))
5696 if (config_values
[i
].type
== type
)
5697 return ((void *) config
) + config_values
[i
].offset
;
5699 LOG(1, 0, 0, "plugin requested config item \"%s\" expecting type %d, have type %d\n",
5700 key
, type
, config_values
[i
].type
);
5706 LOG(1, 0, 0, "plugin requested unknown config item \"%s\"\n", key
);
5710 static int add_plugin(char *plugin_name
)
5712 static struct pluginfuncs funcs
= {
5717 sessiontbysessionidt
,
5718 sessionidtbysessiont
,
5725 cluster_send_session
,
5728 void *p
= open_plugin(plugin_name
, 1);
5729 int (*initfunc
)(struct pluginfuncs
*);
5734 LOG(1, 0, 0, " Plugin load failed: %s\n", dlerror());
5738 if (ll_contains(loaded_plugins
, p
))
5741 return 0; // already loaded
5745 int *v
= dlsym(p
, "plugin_api_version");
5746 if (!v
|| *v
!= PLUGIN_API_VERSION
)
5748 LOG(1, 0, 0, " Plugin load failed: API version mismatch: %s\n", dlerror());
5754 if ((initfunc
= dlsym(p
, "plugin_init")))
5756 if (!initfunc(&funcs
))
5758 LOG(1, 0, 0, " Plugin load failed: plugin_init() returned FALSE: %s\n", dlerror());
5764 ll_push(loaded_plugins
, p
);
5766 for (i
= 0; i
< max_plugin_functions
; i
++)
5769 if (plugin_functions
[i
] && (x
= dlsym(p
, plugin_functions
[i
])))
5771 LOG(3, 0, 0, " Supports function \"%s\"\n", plugin_functions
[i
]);
5772 ll_push(plugins
[i
], x
);
5776 LOG(2, 0, 0, " Loaded plugin %s\n", plugin_name
);
5780 static void run_plugin_done(void *plugin
)
5782 int (*donefunc
)(void) = dlsym(plugin
, "plugin_done");
5788 static int remove_plugin(char *plugin_name
)
5790 void *p
= open_plugin(plugin_name
, 0);
5796 if (ll_contains(loaded_plugins
, p
))
5799 for (i
= 0; i
< max_plugin_functions
; i
++)
5802 if (plugin_functions
[i
] && (x
= dlsym(p
, plugin_functions
[i
])))
5803 ll_delete(plugins
[i
], x
);
5806 ll_delete(loaded_plugins
, p
);
5812 LOG(2, 0, 0, "Removed plugin %s\n", plugin_name
);
5816 int run_plugins(int plugin_type
, void *data
)
5818 int (*func
)(void *data
);
5820 if (!plugins
[plugin_type
] || plugin_type
> max_plugin_functions
)
5821 return PLUGIN_RET_ERROR
;
5823 ll_reset(plugins
[plugin_type
]);
5824 while ((func
= ll_next(plugins
[plugin_type
])))
5828 if (r
!= PLUGIN_RET_OK
)
5829 return r
; // stop here
5832 return PLUGIN_RET_OK
;
5835 static void plugins_done()
5839 ll_reset(loaded_plugins
);
5840 while ((p
= ll_next(loaded_plugins
)))
5844 static void processcontrol(uint8_t *buf
, int len
, struct sockaddr_in
*addr
, int alen
, struct in_addr
*local
)
5846 struct nsctl request
;
5847 struct nsctl response
;
5848 int type
= unpack_control(&request
, buf
, len
);
5852 if (log_stream
&& config
->debug
>= 4)
5856 LOG(4, 0, 0, "Bogus control message from %s (%d)\n",
5857 fmtaddr(addr
->sin_addr
.s_addr
, 0), type
);
5861 LOG(4, 0, 0, "Received [%s] ", fmtaddr(addr
->sin_addr
.s_addr
, 0));
5862 dump_control(&request
, log_stream
);
5868 case NSCTL_REQ_LOAD
:
5869 if (request
.argc
!= 1)
5871 response
.type
= NSCTL_RES_ERR
;
5873 response
.argv
[0] = "name of plugin required";
5875 else if ((r
= add_plugin(request
.argv
[0])) < 1)
5877 response
.type
= NSCTL_RES_ERR
;
5879 response
.argv
[0] = !r
5880 ? "plugin already loaded"
5881 : "error loading plugin";
5885 response
.type
= NSCTL_RES_OK
;
5891 case NSCTL_REQ_UNLOAD
:
5892 if (request
.argc
!= 1)
5894 response
.type
= NSCTL_RES_ERR
;
5896 response
.argv
[0] = "name of plugin required";
5898 else if ((r
= remove_plugin(request
.argv
[0])) < 1)
5900 response
.type
= NSCTL_RES_ERR
;
5902 response
.argv
[0] = !r
5903 ? "plugin not loaded"
5904 : "plugin not found";
5908 response
.type
= NSCTL_RES_OK
;
5914 case NSCTL_REQ_HELP
:
5915 response
.type
= NSCTL_RES_OK
;
5918 ll_reset(loaded_plugins
);
5919 while ((p
= ll_next(loaded_plugins
)))
5921 char **help
= dlsym(p
, "plugin_control_help");
5922 while (response
.argc
< 0xff && help
&& *help
)
5923 response
.argv
[response
.argc
++] = *help
++;
5928 case NSCTL_REQ_CONTROL
:
5930 struct param_control param
= {
5931 config
->cluster_iam_master
,
5938 int r
= run_plugins(PLUGIN_CONTROL
, ¶m
);
5940 if (r
== PLUGIN_RET_ERROR
)
5942 response
.type
= NSCTL_RES_ERR
;
5944 response
.argv
[0] = param
.additional
5946 : "error returned by plugin";
5948 else if (r
== PLUGIN_RET_NOTMASTER
)
5950 static char msg
[] = "must be run on master: 000.000.000.000";
5952 response
.type
= NSCTL_RES_ERR
;
5954 if (config
->cluster_master_address
)
5956 strcpy(msg
+ 23, fmtaddr(config
->cluster_master_address
, 0));
5957 response
.argv
[0] = msg
;
5961 response
.argv
[0] = "must be run on master: none elected";
5964 else if (!(param
.response
& NSCTL_RESPONSE
))
5966 response
.type
= NSCTL_RES_ERR
;
5968 response
.argv
[0] = param
.response
5969 ? "unrecognised response value from plugin"
5970 : "unhandled action";
5974 response
.type
= param
.response
;
5976 if (param
.additional
)
5979 response
.argv
[0] = param
.additional
;
5987 response
.type
= NSCTL_RES_ERR
;
5989 response
.argv
[0] = "error unpacking control packet";
5992 buf
= calloc(NSCTL_MAX_PKT_SZ
, 1);
5995 LOG(2, 0, 0, "Failed to allocate nsctl response\n");
5999 r
= pack_control(buf
, NSCTL_MAX_PKT_SZ
, response
.type
, response
.argc
, response
.argv
);
6002 sendtofrom(controlfd
, buf
, r
, 0, (const struct sockaddr
*) addr
, alen
, local
);
6003 if (log_stream
&& config
->debug
>= 4)
6005 LOG(4, 0, 0, "Sent [%s] ", fmtaddr(addr
->sin_addr
.s_addr
, 0));
6006 dump_control(&response
, log_stream
);
6010 LOG(2, 0, 0, "Failed to pack nsctl response for %s (%d)\n",
6011 fmtaddr(addr
->sin_addr
.s_addr
, 0), r
);
6016 static tunnelidt
new_tunnel()
6019 for (i
= 1; i
< MAXTUNNEL
; i
++)
6021 if (tunnel
[i
].state
== TUNNELFREE
)
6023 LOG(4, 0, i
, "Assigning tunnel ID %u\n", i
);
6024 if (i
> config
->cluster_highest_tunnelid
)
6025 config
->cluster_highest_tunnelid
= i
;
6029 LOG(0, 0, 0, "Can't find a free tunnel! There shouldn't be this many in use!\n");
6034 // We're becoming the master. Do any required setup..
6036 // This is principally telling all the plugins that we're
6037 // now a master, and telling them about all the sessions
6038 // that are active too..
6040 void become_master(void)
6043 static struct event_data d
[RADIUS_FDS
];
6044 struct epoll_event e
;
6046 run_plugins(PLUGIN_BECOME_MASTER
, NULL
);
6048 // running a bunch of iptables commands is slow and can cause
6049 // the master to drop tunnels on takeover--kludge around the
6050 // problem by forking for the moment (note: race)
6051 if (!fork_and_close())
6053 for (s
= 1; s
<= config
->cluster_highest_sessionid
; ++s
)
6055 if (!session
[s
].opened
) // Not an in-use session.
6058 run_plugins(PLUGIN_NEW_SESSION_MASTER
, &session
[s
]);
6065 for (i
= 0; i
< RADIUS_FDS
; i
++)
6067 d
[i
].type
= FD_TYPE_RADIUS
;
6071 epoll_ctl(epollfd
, EPOLL_CTL_ADD
, radfds
[i
], &e
);
6075 int cmd_show_hist_idle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
6081 if (CLI_HELP_REQUESTED
)
6082 return CLI_HELP_NO_ARGS
;
6085 for (i
= 0; i
< 64;++i
) buckets
[i
] = 0;
6087 for (s
= 1; s
<= config
->cluster_highest_sessionid
; ++s
)
6090 if (!session
[s
].opened
)
6093 idle
= time_now
- session
[s
].last_data
;
6094 idle
/= 5 ; // In multiples of 5 seconds.
6104 for (i
= 0; i
< 63; ++i
)
6106 cli_print(cli
, "%3d seconds : %7.2f%% (%6d)", i
* 5, (double) buckets
[i
] * 100.0 / count
, buckets
[i
]);
6108 cli_print(cli
, "lots of secs : %7.2f%% (%6d)", (double) buckets
[63] * 100.0 / count
, buckets
[i
]);
6109 cli_print(cli
, "%d total sessions open.", count
);
6113 int cmd_show_hist_open(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
6119 if (CLI_HELP_REQUESTED
)
6120 return CLI_HELP_NO_ARGS
;
6123 for (i
= 0; i
< 64;++i
) buckets
[i
] = 0;
6125 for (s
= 1; s
<= config
->cluster_highest_sessionid
; ++s
)
6128 if (!session
[s
].opened
)
6131 d
= time_now
- session
[s
].opened
;
6134 while (d
> 1 && open
< 32)
6144 for (i
= 0; i
< 30; ++i
)
6146 cli_print(cli
, " < %8d seconds : %7.2f%% (%6d)", s
, (double) buckets
[i
] * 100.0 / count
, buckets
[i
]);
6149 cli_print(cli
, "%d total sessions open.", count
);
6155 * This unencodes the AVP using the L2TP secret and the previously
6156 * stored random vector. It overwrites the hidden data with the
6157 * unhidden AVP subformat.
6159 static void unhide_value(uint8_t *value
, size_t len
, uint16_t type
, uint8_t *vector
, size_t vec_len
)
6165 uint16_t m
= htons(type
);
6167 // Compute initial pad
6169 MD5_Update(&ctx
, (unsigned char *) &m
, 2);
6170 MD5_Update(&ctx
, config
->l2tp_secret
, strlen(config
->l2tp_secret
));
6171 MD5_Update(&ctx
, vector
, vec_len
);
6172 MD5_Final(digest
, &ctx
);
6174 // pointer to last decoded 16 octets
6179 // calculate a new pad based on the last decoded block
6180 if (d
>= sizeof(digest
))
6183 MD5_Update(&ctx
, config
->l2tp_secret
, strlen(config
->l2tp_secret
));
6184 MD5_Update(&ctx
, last
, sizeof(digest
));
6185 MD5_Final(digest
, &ctx
);
6191 *value
++ ^= digest
[d
++];
6196 int find_filter(char const *name
, size_t len
)
6201 for (i
= 0; i
< MAXFILTER
; i
++)
6203 if (!*ip_filters
[i
].name
)
6211 if (strlen(ip_filters
[i
].name
) != len
)
6214 if (!strncmp(ip_filters
[i
].name
, name
, len
))
6221 static int ip_filter_port(ip_filter_portt
*p
, uint16_t port
)
6225 case FILTER_PORT_OP_EQ
: return port
== p
->port
;
6226 case FILTER_PORT_OP_NEQ
: return port
!= p
->port
;
6227 case FILTER_PORT_OP_GT
: return port
> p
->port
;
6228 case FILTER_PORT_OP_LT
: return port
< p
->port
;
6229 case FILTER_PORT_OP_RANGE
: return port
>= p
->port
&& port
<= p
->port2
;
6235 static int ip_filter_flag(uint8_t op
, uint8_t sflags
, uint8_t cflags
, uint8_t flags
)
6239 case FILTER_FLAG_OP_ANY
:
6240 return (flags
& sflags
) || (~flags
& cflags
);
6242 case FILTER_FLAG_OP_ALL
:
6243 return (flags
& sflags
) == sflags
&& (~flags
& cflags
) == cflags
;
6245 case FILTER_FLAG_OP_EST
:
6246 return (flags
& (TCP_FLAG_ACK
|TCP_FLAG_RST
)) && (~flags
& TCP_FLAG_SYN
);
6252 int ip_filter(uint8_t *buf
, int len
, uint8_t filter
)
6254 uint16_t frag_offset
;
6258 uint16_t src_port
= 0;
6259 uint16_t dst_port
= 0;
6261 ip_filter_rulet
*rule
;
6263 if (len
< 20) // up to end of destination address
6266 if ((*buf
>> 4) != 4) // IPv4
6269 frag_offset
= ntohs(*(uint16_t *) (buf
+ 6)) & 0x1fff;
6271 src_ip
= *(in_addr_t
*) (buf
+ 12);
6272 dst_ip
= *(in_addr_t
*) (buf
+ 16);
6274 if (frag_offset
== 0 && (proto
== IPPROTO_TCP
|| proto
== IPPROTO_UDP
))
6276 int l
= (buf
[0] & 0xf) * 4; // length of IP header
6277 if (len
< l
+ 4) // ports
6280 src_port
= ntohs(*(uint16_t *) (buf
+ l
));
6281 dst_port
= ntohs(*(uint16_t *) (buf
+ l
+ 2));
6282 if (proto
== IPPROTO_TCP
)
6284 if (len
< l
+ 14) // flags
6287 flags
= buf
[l
+ 13] & 0x3f;
6291 for (rule
= ip_filters
[filter
].rules
; rule
->action
; rule
++)
6293 if (rule
->proto
!= IPPROTO_IP
&& proto
!= rule
->proto
)
6296 if (rule
->src_wild
!= INADDR_BROADCAST
&&
6297 (src_ip
& ~rule
->src_wild
) != (rule
->src_ip
& ~rule
->src_wild
))
6300 if (rule
->dst_wild
!= INADDR_BROADCAST
&&
6301 (dst_ip
& ~rule
->dst_wild
) != (rule
->dst_ip
& ~rule
->dst_wild
))
6306 // layer 4 deny rules are skipped
6307 if (rule
->action
== FILTER_ACTION_DENY
&&
6308 (rule
->src_ports
.op
|| rule
->dst_ports
.op
|| rule
->tcp_flag_op
))
6316 if (proto
== IPPROTO_TCP
|| proto
== IPPROTO_UDP
)
6318 if (rule
->src_ports
.op
&& !ip_filter_port(&rule
->src_ports
, src_port
))
6321 if (rule
->dst_ports
.op
&& !ip_filter_port(&rule
->dst_ports
, dst_port
))
6324 if (proto
== IPPROTO_TCP
&& rule
->tcp_flag_op
&&
6325 !ip_filter_flag(rule
->tcp_flag_op
, rule
->tcp_sflags
, rule
->tcp_cflags
, flags
))
6332 return rule
->action
== FILTER_ACTION_PERMIT
;
6341 tunnelidt
lac_new_tunnel()
6343 return new_tunnel();
6346 void lac_tunnelclear(tunnelidt t
)
6351 void lac_send_SCCRQ(tunnelidt t
, uint8_t * auth
, unsigned int auth_len
)
6353 uint16_t version
= 0x0100; // protocol version
6355 tunnel
[t
].state
= TUNNELOPENING
;
6357 // Sent SCCRQ - Start Control Connection Request
6358 controlt
*c
= controlnew(1); // sending SCCRQ
6359 controls(c
, 7, hostname
, 1); // host name
6360 controls(c
, 8, Vendor_name
, 1); // Vendor name
6361 control16(c
, 2, version
, 1); // protocol version
6362 control32(c
, 3, 3, 1); // framing Capabilities
6363 control16(c
, 9, t
, 1); // assigned tunnel
6364 controlb(c
, 11, (uint8_t *) auth
, auth_len
, 1); // CHAP Challenge
6365 LOG(3, 0, t
, "Sent SCCRQ to REMOTE LNS\n");
6366 controladd(c
, 0, t
); // send
6369 void lac_send_ICRQ(tunnelidt t
, sessionidt s
)
6371 // Sent ICRQ Incoming-call-request
6372 controlt
*c
= controlnew(10); // ICRQ
6374 control16(c
, 14, s
, 1); // assigned sesion
6375 call_serial_number
++;
6376 control32(c
, 15, call_serial_number
, 1); // call serial number
6377 LOG(3, s
, t
, "Sent ICRQ to REMOTE LNS (far ID %u)\n", tunnel
[t
].far
);
6378 controladd(c
, 0, t
); // send
6381 void lac_tunnelshutdown(tunnelidt t
, char *reason
, int result
, int error
, char *msg
)
6383 tunnelshutdown(t
, reason
, result
, error
, msg
);