2 * listen.c Handle socket stuff
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2005,2006 The FreeRADIUS server project
21 * Copyright 2005 Alan DeKok <aland@ox.org>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29 #include <freeradius-devel/modpriv.h>
30 #include <freeradius-devel/rad_assert.h>
33 #include <freeradius-devel/udpfromto.h>
36 #ifdef HAVE_SYS_RESOURCE_H
37 #include <sys/resource.h>
49 * Find a per-socket client.
51 RADCLIENT *client_listener_find(const rad_listen_t *listener,
52 const fr_ipaddr_t *ipaddr, int src_port)
54 #ifdef WITH_DYNAMIC_CLIENTS
61 RADCLIENT_LIST *clients;
62 listen_socket_t *sock;
64 rad_assert(listener != NULL);
65 rad_assert(ipaddr != NULL);
67 sock = listener->data;
68 clients = sock->clients;
71 * This HAS to have been initialized previously.
73 rad_assert(clients != NULL);
75 client = client_find(clients, ipaddr,sock->proto);
77 char name[256], buffer[128];
79 #ifdef WITH_DYNAMIC_CLIENTS
80 unknown: /* used only for dynamic clients */
84 * DoS attack quenching, but only in daemon mode.
85 * If they're running in debug mode, show them
88 if (debug_flag == 0) {
89 static time_t last_printed = 0;
92 if (last_printed == now) return NULL;
97 listener->print(listener, name, sizeof(name));
99 radlog(L_ERR, "Ignoring request to %s from unknown client %s port %d"
103 , name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
104 buffer, sizeof(buffer)), src_port
106 , (sock->proto == IPPROTO_UDP) ? "udp" : "tcp"
112 #ifndef WITH_DYNAMIC_CLIENTS
113 return client; /* return the found client. */
117 * No server defined, and it's not dynamic. Return it.
119 if (!client->client_server && !client->dynamic) return client;
124 * It's a dynamically generated client, check it.
126 if (client->dynamic && (src_port != 0)) {
128 * Lives forever. Return it.
130 if (client->lifetime == 0) return client;
133 * Rate-limit the deletion of known clients.
134 * This makes them last a little longer, but
135 * prevents the server from melting down if (say)
136 * 10k clients all expire at once.
138 if (now == client->last_new_client) return client;
141 * It's not dead yet. Return it.
143 if ((client->created + client->lifetime) > now) return client;
146 * This really puts them onto a queue for later
149 client_delete(clients, client);
152 * Go find the enclosing network again.
154 client = client_find(clients, ipaddr, sock->proto);
159 if (!client) goto unknown;
160 if (!client->client_server) goto unknown;
163 * At this point, 'client' is the enclosing
164 * network that configures where dynamic clients
167 rad_assert(client->dynamic == 0);
170 * The IP is unknown, so we've found an enclosing
171 * network. Enable DoS protection. We only
172 * allow one new client per second. Known
173 * clients aren't subject to this restriction.
175 if (now == client->last_new_client) goto unknown;
178 client->last_new_client = now;
180 request = request_alloc();
181 if (!request) goto unknown;
183 request->listener = listener;
184 request->client = client;
185 request->packet = rad_recv(listener->fd, 0x02); /* MSG_PEEK */
186 if (!request->packet) { /* badly formed, etc */
187 request_free(&request);
190 request->reply = rad_alloc_reply(request->packet);
191 if (!request->reply) {
192 request_free(&request);
195 request->packet->timestamp = request->timestamp;
197 request->priority = listener->type;
198 request->server = client->client_server;
199 request->root = &mainconfig;
202 * Run a fake request through the given virtual server.
203 * Look for FreeRADIUS-Client-IP-Address
204 * FreeRADIUS-Client-Secret
207 * and create the RADCLIENT structure from that.
209 DEBUG("server %s {", request->server);
211 rcode = module_authorize(0, request);
213 DEBUG("} # server %s", request->server);
215 if (rcode != RLM_MODULE_OK) {
216 request_free(&request);
221 * If the client was updated by rlm_dynamic_clients,
222 * don't create the client from attribute-value pairs.
224 if (request->client == client) {
225 created = client_create(clients, request);
227 created = request->client;
230 * This frees the client if it isn't valid.
232 if (!client_validate(clients, client, created)) goto unknown;
234 request_free(&request);
236 if (!created) goto unknown;
242 static int listen_bind(rad_listen_t *this);
245 int listen_socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
248 listen_socket_t *sock = this->data;
250 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
251 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
253 ADDSTRING(this->frs->name);
255 #ifdef SO_BINDTODEVICE
256 if (sock->interface) {
257 ADDSTRING(" interface ");
258 ADDSTRING(sock->interface);
263 if (this->recv == auth_tcp_accept) {
264 ADDSTRING(" proto tcp");
270 * TCP sockets get printed a little differently, to make
271 * it clear what's going on.
274 ADDSTRING(" from client (");
275 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
279 snprintf(buffer, bufsize, "%d", sock->other_port);
283 if ((sock->my_ipaddr.af == AF_INET) &&
284 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
285 strlcpy(buffer, "*", bufsize);
287 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
292 snprintf(buffer, bufsize, "%d", sock->my_port);
296 ADDSTRING(", virtual-server=");
297 ADDSTRING(this->server);
306 * Maybe it's a socket that we opened to a home server.
308 if ((sock->proto == IPPROTO_TCP) &&
309 (this->type == RAD_LISTEN_PROXY)) {
311 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
315 snprintf(buffer, bufsize, "%d", sock->my_port);
317 ADDSTRING(") -> home_server (");
319 if ((sock->other_ipaddr.af == AF_INET) &&
320 (sock->other_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
321 strlcpy(buffer, "*", bufsize);
323 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
328 snprintf(buffer, bufsize, "%d", sock->other_port);
337 ADDSTRING(" address ");
339 if ((sock->my_ipaddr.af == AF_INET) &&
340 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
341 strlcpy(buffer, "*", bufsize);
343 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
348 snprintf(buffer, bufsize, "%d", sock->my_port);
352 ADDSTRING(" as server ");
353 ADDSTRING(this->server);
364 * Parse an authentication or accounting socket.
366 int listen_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
371 listen_socket_t *sock = this->data;
372 char *section_name = NULL;
373 CONF_SECTION *client_cs, *parentcs;
378 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
379 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
380 &ipaddr.ipaddr.ip4addr, NULL);
381 if (rcode < 0) return -1;
383 if (rcode == 0) { /* successfully parsed IPv4 */
386 } else { /* maybe IPv6? */
387 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
388 &ipaddr.ipaddr.ip6addr, NULL);
389 if (rcode < 0) return -1;
392 cf_log_err(cf_sectiontoitem(cs),
393 "No address specified in listen section");
396 ipaddr.af = AF_INET6;
399 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
401 if (rcode < 0) return -1;
403 if ((listen_port < 0) || (listen_port > 65535)) {
404 cf_log_err(cf_sectiontoitem(cs),
405 "Invalid value for \"port\"");
409 sock->proto = IPPROTO_UDP;
411 if (cf_pair_find(cs, "proto")) {
413 cf_log_err(cf_sectiontoitem(cs),
414 "System does not support the TCP protocol. Delete this line from the configuration file.");
420 rcode = cf_item_parse(cs, "proto", PW_TYPE_STRING_PTR,
422 if (rcode < 0) return -1;
424 if (strcmp(proto, "udp") == 0) {
425 sock->proto = IPPROTO_UDP;
427 } else if (strcmp(proto, "tcp") == 0) {
428 sock->proto = IPPROTO_TCP;
430 rcode = cf_item_parse(cs, "max_connections", PW_TYPE_INTEGER,
431 &sock->max_connections, "64");
432 if (rcode < 0) return -1;
435 cf_log_err(cf_sectiontoitem(cs),
436 "Unknown proto name \"%s\"", proto);
443 * TCP requires a destination IP for sockets.
444 * UDP doesn't, so it's allowed.
446 if ((this->type == RAD_LISTEN_PROXY) &&
447 (sock->proto != IPPROTO_UDP)) {
448 cf_log_err(cf_sectiontoitem(cs),
449 "Proxy listeners can only listen on proto = udp");
455 sock->my_ipaddr = ipaddr;
456 sock->my_port = listen_port;
459 * If we can bind to interfaces, do so,
462 if (cf_pair_find(cs, "interface")) {
463 #ifndef SO_BINDTODEVICE
464 cf_log_err(cf_sectiontoitem(cs),
465 "System does not support binding to interfaces. Delete this line from the configuration file.");
469 CONF_PAIR *cp = cf_pair_find(cs, "interface");
471 rad_assert(cp != NULL);
472 value = cf_pair_value(cp);
474 cf_log_err(cf_sectiontoitem(cs),
475 "No interface name given");
478 sock->interface = value;
484 * If we can do broadcasts..
486 if (cf_pair_find(cs, "broadcast")) {
488 cf_log_err(cf_sectiontoitem(cs),
489 "System does not support broadcast sockets. Delete this line from the configuration file.");
493 CONF_PAIR *cp = cf_pair_find(cs, "broadcast");
495 if (this->type != RAD_LISTEN_DHCP) {
496 cf_log_err(cf_pairtoitem(cp),
497 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file.");
501 rad_assert(cp != NULL);
502 value = cf_pair_value(cp);
504 cf_log_err(cf_sectiontoitem(cs),
505 "No broadcast value given");
510 * Hack... whatever happened to cf_section_parse?
512 sock->broadcast = (strcmp(value, "yes") == 0);
518 * And bind it to the port.
520 if (listen_bind(this) < 0) {
522 cf_log_err(cf_sectiontoitem(cs),
523 "Error binding to port for %s port %d",
524 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
531 * Proxy sockets don't have clients.
533 if (this->type == RAD_LISTEN_PROXY) return 0;
537 * The more specific configurations are preferred to more
541 parentcs = cf_top_section(cs);
542 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
543 §ion_name, NULL);
544 if (rcode < 0) return -1; /* bad string */
547 * Explicit list given: use it.
549 client_cs = cf_section_sub_find_name2(parentcs,
553 client_cs = cf_section_find(section_name);
556 cf_log_err(cf_sectiontoitem(cs),
557 "Failed to find clients %s {...}",
563 } /* else there was no "clients = " entry. */
566 CONF_SECTION *server_cs;
568 server_cs = cf_section_sub_find_name2(parentcs,
572 * Found a "server foo" section. If there are clients
576 (cf_section_sub_find(server_cs, "client") != NULL)) {
577 client_cs = server_cs;
582 * Still nothing. Look for global clients.
584 if (!client_cs) client_cs = parentcs;
586 sock->clients = clients_parse_section(client_cs);
587 if (!sock->clients) {
588 cf_log_err(cf_sectiontoitem(cs),
589 "Failed to load clients for this listen section");
594 if (sock->proto == IPPROTO_TCP) {
596 * Re-write the listener receive function to
597 * allow us to accept the socket.
599 this->recv = auth_tcp_accept;
608 * Binds a listener to a socket.
610 static int listen_bind(rad_listen_t *this)
613 struct sockaddr_storage salocal;
615 listen_socket_t *sock = this->data;
617 #define proto_for_port "udp"
618 #define sock_type SOCK_DGRAM
620 const char *proto_for_port = "udp";
621 int sock_type = SOCK_DGRAM;
623 if (sock->proto == IPPROTO_TCP) {
625 if (this->type == RAD_LISTEN_VQP) {
626 radlog(L_ERR, "VQP does not support TCP transport");
631 proto_for_port = "tcp";
632 sock_type = SOCK_STREAM;
637 * If the port is zero, then it means the appropriate
638 * thing from /etc/services.
640 if (sock->my_port == 0) {
643 switch (this->type) {
644 case RAD_LISTEN_AUTH:
645 svp = getservbyname ("radius", proto_for_port);
647 sock->my_port = ntohs(svp->s_port);
649 sock->my_port = PW_AUTH_UDP_PORT;
653 #ifdef WITH_ACCOUNTING
654 case RAD_LISTEN_ACCT:
655 svp = getservbyname ("radacct", proto_for_port);
657 sock->my_port = ntohs(svp->s_port);
659 sock->my_port = PW_ACCT_UDP_PORT;
665 case RAD_LISTEN_PROXY:
666 /* leave it at zero */
672 sock->my_port = 1589;
678 sock->my_port = PW_COA_UDP_PORT;
682 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
688 * Copy fr_socket() here, as we may need to bind to a device.
690 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
692 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
696 #ifdef SO_BINDTODEVICE
698 * Bind to a device BEFORE touching IP addresses.
700 if (sock->interface) {
702 strcpy(ifreq.ifr_name, sock->interface);
705 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
706 (char *)&ifreq, sizeof(ifreq));
710 radlog(L_ERR, "Failed binding to interface %s: %s",
711 sock->interface, strerror(errno));
713 } /* else it worked. */
718 if (sock->proto == IPPROTO_TCP) {
721 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
723 radlog(L_ERR, "Failed to reuse address: %s", strerror(errno));
729 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
730 else /* UDP sockets get UDPfromto */
733 #ifdef WITH_UDPFROMTO
735 * Initialize udpfromto for all sockets.
737 if (udpfromto_init(this->fd) != 0) {
744 * Set up sockaddr stuff.
746 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
751 #ifdef HAVE_STRUCT_SOCKADDR_IN6
752 if (sock->my_ipaddr.af == AF_INET6) {
754 * Listening on '::' does NOT get you IPv4 to
755 * IPv6 mapping. You've got to listen on an IPv4
756 * address, too. This makes the rest of the server
757 * design a little simpler.
761 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
764 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
765 (char *)&on, sizeof(on));
767 #endif /* IPV6_V6ONLY */
769 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
771 if (sock->my_ipaddr.af == AF_INET) {
774 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
776 * Disable PMTU discovery. On Linux, this
777 * also makes sure that the "don't fragment"
780 flag = IP_PMTUDISC_DONT;
781 setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
782 &flag, sizeof(flag));
785 #if defined(IP_DONTFRAG)
787 * Ensure that the "don't fragment" flag is zero.
790 setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
791 &flag, sizeof(flag));
797 if (sock->broadcast) {
800 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
801 radlog(L_ERR, "Can't set broadcast option: %s\n",
810 * May be binding to priviledged ports.
812 if (sock->my_port != 0) {
816 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
817 radlog(L_ERR, "Can't set re-use address option: %s\n",
824 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
830 this->print(this, buffer, sizeof(buffer));
831 radlog(L_ERR, "Failed binding to %s: %s\n",
832 buffer, strerror(errno));
837 * FreeBSD jail issues. We bind to 0.0.0.0, but the
838 * kernel instead binds us to a 1.2.3.4. If this
839 * happens, notice, and remember our real IP.
842 struct sockaddr_storage src;
843 socklen_t sizeof_src = sizeof(src);
845 memset(&src, 0, sizeof_src);
846 if (getsockname(this->fd, (struct sockaddr *) &src,
848 radlog(L_ERR, "Failed getting socket name: %s",
853 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
854 &sock->my_ipaddr, &sock->my_port)) {
855 radlog(L_ERR, "Socket has unsupported address family");
862 if (sock->proto == IPPROTO_TCP) {
863 if (listen(this->fd, 8) < 0) {
865 radlog(L_ERR, "Failed in listen(): %s", strerror(errno));
871 if (fr_nonblock(this->fd) < 0) {
873 radlog(L_ERR, "Failed setting non-blocking on socket: %s",
879 * Mostly for proxy sockets.
881 sock->other_ipaddr.af = sock->my_ipaddr.af;
884 * Don't screw up other people.
886 #undef proto_for_port
892 #define MAX_HANDLES (32)
893 static int frs_init = 0;
894 static lt_dlhandle frs_modules[MAX_HANDLES];
898 * Allocate & initialize a new listener.
900 rad_listen_t *listen_alloc(const char *type_name)
904 const frs_module_t *frs;
907 this = rad_malloc(sizeof(*this));
908 memset(this, 0, sizeof(*this));
910 snprintf(buffer, sizeof(buffer), "frs_%s", type_name);
913 memset(frs_modules, 0, sizeof(frs_modules));
918 for (i = 0; i < MAX_HANDLES; i++) {
919 if (!frs_modules[i]) continue;
921 frs = lt_dlsym(frs_modules[i], buffer);
928 handle = lt_dlopenext(buffer);
930 radlog(L_ERR, "Failed opening %s: %s",
931 buffer, lt_dlerror());
935 frs = lt_dlsym(handle, buffer);
938 radlog(L_ERR, "Failed linking to %s: %s",
939 buffer, lt_dlerror());
943 if (frs->magic != FRS_MODULE_MAGIC_NUMBER) {
945 radlog(L_ERR, "Invalid version in %s\n",
950 for (i = 0; i < MAX_HANDLES; i++) {
951 if (!frs_modules[i]) {
952 frs_modules[i] = handle;
955 } /* if we run out, we leak the handle. Too bad. */
958 this->type = frs->type;
960 this->recv = frs->recv;
961 this->send = frs->send;
962 this->print = frs->print;
963 this->encode = frs->encode;
964 this->decode = frs->decode;
966 switch (this->type) {
968 case RAD_LISTEN_NONE:
970 case RAD_LISTEN_AUTH:
971 #ifdef WITH_ACCOUNTING
972 case RAD_LISTEN_ACCT:
975 case RAD_LISTEN_PROXY:
983 this->data = rad_malloc(sizeof(listen_socket_t));
984 memset(this->data, 0, sizeof(listen_socket_t));
988 case RAD_LISTEN_DHCP:
989 this->data = rad_malloc(sizeof(dhcp_socket_t));
990 memset(this->data, 0, sizeof(dhcp_socket_t));
995 case RAD_LISTEN_DETAIL:
1000 #ifdef WITH_COMMAND_SOCKET
1002 * The data here is allocate in the "parse" section.
1004 case RAD_LISTEN_COMMAND:
1009 rad_assert("Unsupported option!" == NULL);
1018 * Externally visible function for creating a new proxy LISTENER.
1020 * Not thread-safe, but all calls to it are protected by the
1021 * proxy mutex in event.c
1023 int proxy_new_listener(home_server *home, int src_port)
1026 listen_socket_t *sock;
1028 if (!home) return 0;
1030 if ((home->max_connections > 0) &&
1031 (home->num_connections >= home->max_connections)) {
1032 DEBUG("WARNING: Home server has too many open connections (%d)",
1033 home->max_connections);
1037 this = listen_alloc(RAD_LISTEN_PROXY);
1040 sock->other_ipaddr = home->ipaddr;
1041 sock->other_port = home->port;
1044 sock->my_ipaddr = home->src_ipaddr;
1045 sock->my_port = src_port;
1046 sock->proto = home->proto;
1049 sock->last_packet = time(NULL);
1051 if (home->proto == IPPROTO_TCP) {
1052 this->recv = proxy_socket_tcp_recv;
1055 * FIXME: connect() is blocking!
1056 * We do this with the proxy mutex locked, which may
1057 * cause large delays!
1059 * http://www.developerweb.net/forum/showthread.php?p=13486
1061 this->fd = fr_tcp_client_socket(&home->src_ipaddr,
1062 &home->ipaddr, home->port);
1065 this->fd = fr_socket(&home->src_ipaddr, src_port);
1068 DEBUG("Failed opening client socket: %s", fr_strerror());
1074 * Figure out which port we were bound to.
1076 if (sock->my_port == 0) {
1077 struct sockaddr_storage src;
1078 socklen_t sizeof_src = sizeof(src);
1080 memset(&src, 0, sizeof_src);
1081 if (getsockname(this->fd, (struct sockaddr *) &src,
1083 radlog(L_ERR, "Failed getting socket name: %s",
1089 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
1090 &sock->my_ipaddr, &sock->my_port)) {
1091 radlog(L_ERR, "Socket has unsupported address family");
1098 * Tell the event loop that we have a new FD
1107 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1115 cf_log_info(cs, "listen {");
1117 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1119 if (rcode < 0) return NULL;
1122 cf_log_err(cf_sectiontoitem(cs),
1123 "No type specified in listen section");
1128 * Allow listen sections in the default config to
1129 * refer to a server.
1132 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1134 if (rcode == 1) { /* compatiblity with 2.0-pre */
1135 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1146 * We were passed a virtual server, so the caller is
1147 * defining a proxy listener inside of a virtual server.
1148 * This isn't allowed right now.
1150 else if (type == RAD_LISTEN_PROXY) {
1151 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
1157 * Set up cross-type data.
1159 this = listen_alloc(listen_type);
1160 if (!this) return NULL;
1163 this->server = server;
1167 * Call per-type parser.
1169 if (this->frs->parse(cs, this) < 0) {
1174 cf_log_info(cs, "}");
1180 * Generate a list of listeners. Takes an input list of
1181 * listeners, too, so we don't close sockets with waiting packets.
1183 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1185 int override = FALSE;
1187 CONF_SECTION *cs = NULL;
1188 rad_listen_t **last;
1190 fr_ipaddr_t server_ipaddr;
1193 int defined_proxy = 0;
1197 * We shouldn't be called with a pre-existing list.
1199 rad_assert(head && (*head == NULL));
1202 server_ipaddr.af = AF_UNSPEC;
1205 * If the port is specified on the command-line,
1206 * it over-rides the configuration file.
1208 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1210 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1213 * If the IP address was configured on the command-line,
1214 * use that as the "bind_address"
1216 if (mainconfig.myip.af != AF_UNSPEC) {
1217 memcpy(&server_ipaddr, &mainconfig.myip,
1218 sizeof(server_ipaddr));
1224 * Else look for bind_address and/or listen sections.
1226 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1227 rcode = cf_item_parse(config, "bind_address",
1229 &server_ipaddr.ipaddr.ip4addr, NULL);
1230 if (rcode < 0) return -1; /* error parsing it */
1232 if (rcode == 0) { /* successfully parsed IPv4 */
1233 listen_socket_t *sock;
1234 server_ipaddr.af = AF_INET;
1236 radlog(L_INFO, "WARNING: The directive 'bind_adress' is deprecated, and will be removed in future versions of FreeRADIUS. Please edit the configuration files to use the directive 'listen'.");
1240 if (strcmp(progname, "vmpsd") == 0) {
1241 this = listen_alloc("vmps");
1242 if (!this) return 0; /* FIXME: memleak? */
1243 if (!auth_port) auth_port = 1589;
1247 this = listen_alloc("auth");
1248 if (!this) return 0; /* FIXME: memleak? */
1253 sock->my_ipaddr = server_ipaddr;
1254 sock->my_port = auth_port;
1256 sock->clients = clients_parse_section(config);
1257 if (!sock->clients) {
1258 cf_log_err(cf_sectiontoitem(config),
1259 "Failed to find any clients for this listen section");
1264 if (listen_bind(this) < 0) {
1266 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
1270 auth_port = sock->my_port; /* may have been updated in listen_bind */
1272 cs = cf_section_sub_find_name2(config, "server",
1274 if (cs) this->server = mainconfig.name;
1278 last = &(this->next);
1284 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
1287 #ifdef WITH_ACCOUNTING
1289 * Open Accounting Socket.
1291 * If we haven't already gotten acct_port from
1292 * /etc/services, then make it auth_port + 1.
1294 this = listen_alloc("acct");
1295 if (!this) return 0; /* FIXME: memleak? */
1299 * Create the accounting socket.
1301 * The accounting port is always the
1302 * authentication port + 1
1304 sock->my_ipaddr = server_ipaddr;
1305 sock->my_port = auth_port + 1;
1307 sock->clients = clients_parse_section(config);
1308 if (!sock->clients) {
1309 cf_log_err(cf_sectiontoitem(config),
1310 "Failed to find any clients for this listen section");
1314 if (listen_bind(this) < 0) {
1317 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
1322 cs = cf_section_sub_find_name2(config, "server",
1324 if (cs) this->server = mainconfig.name;
1328 last = &(this->next);
1330 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1331 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1337 * They specified an IP on the command-line, ignore
1338 * all listen sections except the one in '-n'.
1340 if (mainconfig.myip.af != AF_UNSPEC) {
1341 CONF_SECTION *subcs;
1342 const char *name2 = cf_section_name2(cs);
1344 cs = cf_section_sub_find_name2(config, "server",
1346 if (!cs) goto add_sockets;
1349 * Should really abstract this code...
1351 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1353 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1354 this = listen_parse(subcs, name2);
1361 last = &(this->next);
1362 } /* loop over "listen" directives in server <foo> */
1368 * Walk through the "listen" sections, if they exist.
1370 for (cs = cf_subsection_find_next(config, NULL, "listen");
1372 cs = cf_subsection_find_next(config, cs, "listen")) {
1373 this = listen_parse(cs, NULL);
1380 last = &(this->next);
1384 * Check virtual servers for "listen" sections, too.
1386 * FIXME: Move to virtual server init?
1388 for (cs = cf_subsection_find_next(config, NULL, "server");
1390 cs = cf_subsection_find_next(config, cs, "server")) {
1391 CONF_SECTION *subcs;
1392 const char *name2 = cf_section_name2(cs);
1394 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1396 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1397 this = listen_parse(subcs, name2);
1404 last = &(this->next);
1405 } /* loop over "listen" directives in virtual servers */
1406 } /* loop over virtual servers */
1410 * Print out which sockets we're listening on, and
1411 * add them to the event list.
1413 for (this = *head; this != NULL; this = this->next) {
1415 if (this->type == RAD_LISTEN_PROXY) {
1424 * If we're proxying requests, open the proxy FD.
1425 * Otherwise, don't do anything.
1428 if ((mainconfig.proxy_requests == TRUE) &&
1429 (*head != NULL) && !defined_proxy) {
1430 listen_socket_t *sock = NULL;
1434 memset(&home, 0, sizeof(home));
1439 home.proto = IPPROTO_UDP;
1440 home.src_ipaddr = server_ipaddr;
1443 * Find the first authentication port,
1446 for (this = *head; this != NULL; this = this->next) {
1447 if (this->type == RAD_LISTEN_AUTH) {
1449 if (home.src_ipaddr.af == AF_UNSPEC) {
1450 home.src_ipaddr = sock->my_ipaddr;
1452 port = sock->my_port + 2;
1456 if (this->type == RAD_LISTEN_ACCT) {
1458 if (home.src_ipaddr.af == AF_UNSPEC) {
1459 home.src_ipaddr = sock->my_ipaddr;
1461 port = sock->my_port + 1;
1468 * Address is still unspecified, use IPv4.
1470 if (home.src_ipaddr.af == AF_UNSPEC) {
1471 home.src_ipaddr.af = AF_INET;
1472 /* everything else is already set to zero */
1475 this = listen_alloc("proxy");
1476 if (!this) return 0; /* FIXME: memleak? */
1478 home.ipaddr.af = home.src_ipaddr.af;
1479 /* everything else is already set to zero */
1481 if (!proxy_new_listener(&home, port)) {
1489 * Haven't defined any sockets. Die.
1491 if (!*head) return -1;
1498 * Free a linked list of listeners;
1500 void listen_free(rad_listen_t **head)
1504 if (!head || !*head) return;
1508 rad_listen_t *next = this->next;
1511 * Other code may have eaten the FD.
1513 if (this->fd >= 0) close(this->fd);
1515 if (this->frs->free) {
1516 this->frs->free(this);
1520 if ((this->type == RAD_LISTEN_AUTH) ||
1522 (this->type == RAD_LISTEN_ACCT) ||
1525 (this->type == RAD_LISTEN_PROXY)
1528 listen_socket_t *sock = this->data;
1529 rad_free(&sock->packet);
1542 fr_tcp_radius_t *fr_listen2tcp(rad_listen_t *this)
1544 listen_socket_t *sock;
1546 if (!this || (this->type != RAD_LISTEN_PROXY) || !this->data) {
1554 rad_listen_t *proxy_new_tcp_listener(home_server *home)
1557 fr_tcp_radius_t *tcp;
1558 struct sockaddr_storage src;
1559 socklen_t sizeof_src = sizeof(src);
1561 listen_socket_t *sock;
1564 ((home->max_connections > 0) &&
1565 (home->num_connections >= home->max_connections))) {
1566 DEBUG("WARNING: Home server has too many open connections (%d)",
1567 home->max_connections);
1574 * FIXME: Move to RBTrees.
1576 for (i = 0; i < home->max_connections; i++) {
1577 if (home->listeners[i]) continue;
1579 this = home->listeners[i] = listen_alloc(RAD_LISTEN_PROXY);
1581 DEBUG("WARNING: Failed allocating memory");
1588 DEBUG("WARNING: Failed to find a free connection slot");
1593 tcp = sock->tcp = rad_malloc(sizeof(*tcp));
1594 memset(tcp, 0, sizeof(*tcp));
1599 * Open a new socket...
1603 tcp->dst_ipaddr = home->ipaddr;
1604 tcp->dst_port = home->port;
1605 tcp->lifetime = home->lifetime;
1606 tcp->opened = time(NULL);
1609 * FIXME: connect() is blocking!
1610 * We do this with the proxy mutex locked, which may
1611 * cause large delays!
1613 * http://www.developerweb.net/forum/showthread.php?p=13486
1615 this->fd = tcp->fd = fr_tcp_client_socket(&tcp->dst_ipaddr, tcp->dst_port);
1618 DEBUG("WARNING: Failed opening socket to home server");
1621 memset(&src, 0, sizeof_src);
1622 if (getsockname(tcp->fd, (struct sockaddr *) &src, &sizeof_src) < 0) {
1628 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
1629 &tcp->src_ipaddr, &tcp->src_port)) {
1636 * Fill in socket information.
1638 sock->proto = IPPROTO_TCP;
1641 sock->ipaddr = tcp->src_ipaddr;
1642 sock->port = tcp->src_port;
1645 * Don't ask. Just don't ask.
1647 sock->src_ipaddr = tcp->dst_ipaddr;
1648 sock->src_port = tcp->dst_port;
1650 sock->home->num_connections++;
1653 this->recv = proxy_socket_tcp_recv;
1657 * Tell the event handler about the new socket.
1659 * It keeps track of "this", so we don't have to insert
1660 * it into the main list of listeners.
1667 void proxy_close_tcp_listener(rad_listen_t *listener)
1670 listen_socket_t *sock = listener->data;
1673 * This is the second time around for the socket. Free
1676 if (listener->status != RAD_LISTEN_STATUS_KNOWN) {
1677 listen_free(&listener);
1681 listener->status = RAD_LISTEN_STATUS_CLOSED;
1682 event_new_fd(listener);
1685 * Find the home server, and mark this listener as
1686 * no longer being active.
1688 for (i = 0; i < sock->home->max_connections; i++) {
1689 if (sock->home->listeners[i] == listener) {
1690 sock->home->listeners[i] = NULL;
1691 sock->home->num_connections--;
1697 * There are still one or more requests using this socket.
1698 * leave it marked as "closed", but don't free it. When the
1699 * last requeast using it is cleaned up, it will be deleted.
1701 if (sock->tcp->used > 0) return;
1703 listen_free(&listener);
1708 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
1712 for (this = mainconfig.listen; this != NULL; this = this->next) {
1713 listen_socket_t *sock;
1716 * FIXME: For TCP, ignore the *secondary*
1717 * listeners associated with the main socket.
1719 if ((this->type != RAD_LISTEN_AUTH) &&
1720 (this->type != RAD_LISTEN_ACCT)) continue;
1724 if ((sock->my_port == port) &&
1725 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
1729 if ((sock->my_port == port) &&
1730 ((sock->my_ipaddr.af == AF_INET) &&
1731 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == INADDR_ANY))) {
1735 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1736 if ((sock->my_port == port) &&
1737 (sock->my_ipaddr.af == AF_INET6) &&
1738 (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr))) {