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>
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/modules.h>
28 #include <freeradius-devel/rad_assert.h>
29 #include <freeradius-devel/process.h>
30 #include <freeradius-devel/protocol.h>
31 #include <freeradius-devel/modpriv.h>
33 #include <freeradius-devel/detail.h>
36 #include <freeradius-devel/udpfromto.h>
39 #ifdef HAVE_SYS_RESOURCE_H
40 #include <sys/resource.h>
52 #ifdef DEBUG_PRINT_PACKET
53 static void print_packet(RADIUS_PACKET *packet)
55 char src[256], dst[256];
57 ip_ntoh(&packet->src_ipaddr, src, sizeof(src));
58 ip_ntoh(&packet->dst_ipaddr, dst, sizeof(dst));
60 fprintf(stderr, "ID %d: %s %d -> %s %d\n", packet->id,
61 src, packet->src_port, dst, packet->dst_port);
67 static rad_listen_t *listen_alloc(TALLOC_CTX *ctx, RAD_LISTEN_TYPE type);
69 #ifdef WITH_COMMAND_SOCKET
70 static int command_tcp_recv(rad_listen_t *listener);
71 static int command_tcp_send(rad_listen_t *listener, REQUEST *request);
72 static int command_write_magic(int newfd, listen_socket_t *sock);
75 static fr_protocol_t master_listen[RAD_LISTEN_MAX];
78 * Xlat for %{listen:foo}
80 static ssize_t xlat_listen(UNUSED void *instance, REQUEST *request,
81 char const *fmt, char *out,
84 char const *value = NULL;
87 if (!fmt || !out || (outlen < 1)) return 0;
89 if (!request->listener) {
90 RWDEBUG("No listener associated with this request");
95 cp = cf_pair_find(request->listener->cs, fmt);
96 if (!cp || !(value = cf_pair_value(cp))) {
97 RDEBUG("Listener does not contain config item \"%s\"", fmt);
102 strlcpy(out, value, outlen);
108 * Find a per-socket client.
110 RADCLIENT *client_listener_find(rad_listen_t *listener,
111 fr_ipaddr_t const *ipaddr, uint16_t src_port)
113 #ifdef WITH_DYNAMIC_CLIENTS
120 RADCLIENT_LIST *clients;
121 listen_socket_t *sock;
123 rad_assert(listener != NULL);
124 rad_assert(ipaddr != NULL);
126 sock = listener->data;
127 clients = sock->clients;
130 * This HAS to have been initialized previously.
132 rad_assert(clients != NULL);
134 client = client_find(clients, ipaddr,sock->proto);
136 char name[256], buffer[128];
138 #ifdef WITH_DYNAMIC_CLIENTS
139 unknown: /* used only for dynamic clients */
143 * DoS attack quenching, but only in daemon mode.
144 * If they're running in debug mode, show them
147 if (debug_flag == 0) {
148 static time_t last_printed = 0;
151 if (last_printed == now) return NULL;
156 listener->print(listener, name, sizeof(name));
158 ERROR("Ignoring request to %s from unknown client %s port %d"
162 , name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
163 buffer, sizeof(buffer)), src_port
165 , (sock->proto == IPPROTO_UDP) ? "udp" : "tcp"
171 #ifndef WITH_DYNAMIC_CLIENTS
172 return client; /* return the found client. */
176 * No server defined, and it's not dynamic. Return it.
178 if (!client->client_server && !client->dynamic) return client;
183 * It's a dynamically generated client, check it.
185 if (client->dynamic && (src_port != 0)) {
187 * Lives forever. Return it.
189 if (client->lifetime == 0) return client;
192 * Rate-limit the deletion of known clients.
193 * This makes them last a little longer, but
194 * prevents the server from melting down if (say)
195 * 10k clients all expire at once.
197 if (now == client->last_new_client) return client;
200 * It's not dead yet. Return it.
202 if ((client->created + client->lifetime) > now) return client;
205 * This really puts them onto a queue for later
208 client_delete(clients, client);
211 * Go find the enclosing network again.
213 client = client_find(clients, ipaddr, sock->proto);
218 if (!client) goto unknown;
219 if (!client->client_server) goto unknown;
222 * At this point, 'client' is the enclosing
223 * network that configures where dynamic clients
226 rad_assert(client->dynamic == 0);
228 } else if (!client->dynamic && client->rate_limit) {
230 * The IP is unknown, so we've found an enclosing
231 * network. Enable DoS protection. We only
232 * allow one new client per second. Known
233 * clients aren't subject to this restriction.
235 if (now == client->last_new_client) goto unknown;
238 client->last_new_client = now;
240 request = request_alloc(NULL);
241 if (!request) goto unknown;
243 request->listener = listener;
244 request->client = client;
245 request->packet = rad_recv(listener->fd, 0x02); /* MSG_PEEK */
246 if (!request->packet) { /* badly formed, etc */
247 talloc_free(request);
250 (void) talloc_steal(request, request->packet);
251 request->reply = rad_alloc_reply(request, request->packet);
252 if (!request->reply) {
253 talloc_free(request);
256 gettimeofday(&request->packet->timestamp, NULL);
258 request->priority = listener->type;
259 request->server = client->client_server;
260 request->root = &main_config;
263 * Run a fake request through the given virtual server.
264 * Look for FreeRADIUS-Client-IP-Address
265 * FreeRADIUS-Client-Secret
268 * and create the RADCLIENT structure from that.
270 DEBUG("server %s {", request->server);
272 rcode = process_authorize(0, request);
274 DEBUG("} # server %s", request->server);
276 if (rcode != RLM_MODULE_OK) {
277 talloc_free(request);
282 * If the client was updated by rlm_dynamic_clients,
283 * don't create the client from attribute-value pairs.
285 if (request->client == client) {
286 created = client_from_request(clients, request);
288 created = request->client;
291 * This frees the client if it isn't valid.
293 if (!client_validate(clients, client, created)) goto unknown;
296 request->server = client->server;
297 exec_trigger(request, NULL, "server.client.add", false);
299 talloc_free(request);
301 if (!created) goto unknown;
307 static int listen_bind(rad_listen_t *this);
311 * Process and reply to a server-status request.
312 * Like rad_authenticate and rad_accounting this should
313 * live in it's own file but it's so small we don't bother.
315 int rad_status_server(REQUEST *request)
317 int rcode = RLM_MODULE_OK;
320 switch (request->listener->type) {
322 case RAD_LISTEN_NONE:
324 case RAD_LISTEN_AUTH:
325 dval = dict_valbyname(PW_AUTZ_TYPE, 0, "Status-Server");
327 rcode = process_authorize(dval->value, request);
329 rcode = RLM_MODULE_OK;
334 case RLM_MODULE_UPDATED:
335 request->reply->code = PW_CODE_ACCESS_ACCEPT;
338 case RLM_MODULE_FAIL:
339 case RLM_MODULE_HANDLED:
340 request->reply->code = 0; /* don't reply */
344 case RLM_MODULE_REJECT:
345 request->reply->code = PW_CODE_ACCESS_REJECT;
350 #ifdef WITH_ACCOUNTING
351 case RAD_LISTEN_ACCT:
352 dval = dict_valbyname(PW_ACCT_TYPE, 0, "Status-Server");
354 rcode = process_accounting(dval->value, request);
356 rcode = RLM_MODULE_OK;
361 case RLM_MODULE_UPDATED:
362 request->reply->code = PW_CODE_ACCOUNTING_RESPONSE;
366 request->reply->code = 0; /* don't reply */
374 * This is a vendor extension. Suggested by Glen
375 * Zorn in IETF 72, and rejected by the rest of
376 * the WG. We like it, so it goes in here.
379 dval = dict_valbyname(PW_RECV_COA_TYPE, 0, "Status-Server");
381 rcode = process_recv_coa(dval->value, request);
383 rcode = RLM_MODULE_OK;
388 case RLM_MODULE_UPDATED:
389 request->reply->code = PW_CODE_COA_ACK;
393 request->reply->code = 0; /* don't reply */
405 * Full statistics are available only on a statistics
408 if (request->listener->type == RAD_LISTEN_NONE) {
409 request_stats_reply(request);
417 static int dual_tcp_recv(rad_listen_t *listener)
420 RADIUS_PACKET *packet;
421 RAD_REQUEST_FUNP fun = NULL;
422 listen_socket_t *sock = listener->data;
423 RADCLIENT *client = sock->client;
425 rad_assert(client != NULL);
427 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return 0;
430 * Allocate a packet for partial reads.
433 sock->packet = rad_alloc(sock, false);
434 if (!sock->packet) return 0;
436 sock->packet->sockfd = listener->fd;
437 sock->packet->src_ipaddr = sock->other_ipaddr;
438 sock->packet->src_port = sock->other_port;
439 sock->packet->dst_ipaddr = sock->my_ipaddr;
440 sock->packet->dst_port = sock->my_port;
441 sock->packet->proto = sock->proto;
445 * Grab the packet currently being processed.
447 packet = sock->packet;
449 rcode = fr_tcp_read_packet(packet, 0);
452 * Still only a partial packet. Put it back, and return,
453 * so that we'll read more data when it's ready.
459 if (rcode == -1) { /* error reading packet */
462 ERROR("Invalid packet from %s port %d, closing socket: %s",
463 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
464 packet->src_port, fr_strerror());
467 if (rcode < 0) { /* error or connection reset */
468 listener->status = RAD_LISTEN_STATUS_EOL;
471 * Tell the event handler that an FD has disappeared.
473 DEBUG("Client has closed connection");
474 radius_update_listener(listener);
477 * Do NOT free the listener here. It's in use by
478 * a request, and will need to hang around until
479 * all of the requests are done.
481 * It is instead free'd in remove_from_request_hash()
487 * Some sanity checks, based on the packet code.
489 switch(packet->code) {
490 case PW_CODE_ACCESS_REQUEST:
491 if (listener->type != RAD_LISTEN_AUTH) goto bad_packet;
492 FR_STATS_INC(auth, total_requests);
493 fun = rad_authenticate;
496 #ifdef WITH_ACCOUNTING
497 case PW_CODE_ACCOUNTING_REQUEST:
498 if (listener->type != RAD_LISTEN_ACCT) goto bad_packet;
499 FR_STATS_INC(acct, total_requests);
500 fun = rad_accounting;
504 case PW_CODE_STATUS_SERVER:
505 if (!main_config.status_server) {
506 FR_STATS_INC(auth, total_unknown_types);
507 WARN("Ignoring Status-Server request due to security configuration");
508 rad_free(&sock->packet);
511 fun = rad_status_server;
516 FR_STATS_INC(auth, total_unknown_types);
518 DEBUG("Invalid packet code %d sent from client %s port %d : IGNORED",
519 packet->code, client->shortname, packet->src_port);
520 rad_free(&sock->packet);
522 } /* switch over packet types */
524 if (!request_receive(listener, packet, client, fun)) {
525 FR_STATS_INC(auth, total_packets_dropped);
526 rad_free(&sock->packet);
530 sock->packet = NULL; /* we have no need for more partial reads */
534 static int dual_tcp_accept(rad_listen_t *listener)
540 struct sockaddr_storage src;
541 listen_socket_t *sock;
542 fr_ipaddr_t src_ipaddr;
543 RADCLIENT *client = NULL;
547 DEBUG2(" ... new connection request on TCP socket");
549 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
552 * Non-blocking sockets must handle this.
555 if (errno == EWOULDBLOCK) {
560 DEBUG2(" ... failed to accept connection");
564 if (!fr_sockaddr2ipaddr(&src, salen, &src_ipaddr, &src_port)) {
566 DEBUG2(" ... unknown address family");
571 * Enforce client IP address checks on accept, not on
574 if ((client = client_listener_find(listener,
575 &src_ipaddr, src_port)) == NULL) {
577 FR_STATS_INC(auth, total_invalid_requests);
583 * Enforce security restrictions.
585 * This shouldn't be necessary in practice. However, it
586 * serves as a double-check on configurations. Marking a
587 * client as "tls required" means that any accidental
588 * exposure of the client to non-TLS traffic is
591 if (client->tls_required && !listener->tls) {
592 INFO("Ignoring connection to TLS socket from non-TLS client");
599 * Enforce max_connections on client && listen section.
601 if ((client->limit.max_connections != 0) &&
602 (client->limit.max_connections == client->limit.num_connections)) {
604 * FIXME: Print client IP/port, and server IP/port.
606 INFO("Ignoring new connection due to client max_connections (%d)", client->limit.max_connections);
611 sock = listener->data;
612 if ((sock->limit.max_connections != 0) &&
613 (sock->limit.max_connections == sock->limit.num_connections)) {
615 * FIXME: Print client IP/port, and server IP/port.
617 INFO("Ignoring new connection due to socket max_connections");
621 client->limit.num_connections++;
622 sock->limit.num_connections++;
625 * Add the new listener.
627 this = listen_alloc(listener, listener->type);
628 if (!this) return -1;
631 * Copy everything, including the pointer to the socket
635 memcpy(this->data, listener->data, sizeof(*sock));
636 memcpy(this, listener, sizeof(*this));
638 this->data = sock; /* fix it back */
640 sock->parent = listener->data;
641 sock->other_ipaddr = src_ipaddr;
642 sock->other_port = src_port;
643 sock->client = client;
644 sock->opened = sock->last_packet = time(NULL);
647 * Set the limits. The defaults are the parent limits.
648 * Client limits on max_connections are enforced dynamically.
649 * Set the MINIMUM of client/socket idle timeout or lifetime.
651 memcpy(&sock->limit, &sock->parent->limit, sizeof(sock->limit));
653 if (client->limit.idle_timeout &&
654 ((sock->limit.idle_timeout == 0) ||
655 (client->limit.idle_timeout < sock->limit.idle_timeout))) {
656 sock->limit.idle_timeout = client->limit.idle_timeout;
659 if (client->limit.lifetime &&
660 ((sock->limit.lifetime == 0) ||
661 (client->limit.lifetime < sock->limit.lifetime))) {
662 sock->limit.lifetime = client->limit.lifetime;
666 this->status = RAD_LISTEN_STATUS_INIT;
669 #ifdef WITH_COMMAND_SOCKET
670 if (this->type == RAD_LISTEN_COMMAND) {
671 this->recv = command_tcp_recv;
672 this->send = command_tcp_send;
673 command_write_magic(this->fd, sock);
678 this->recv = dual_tcp_recv;
682 this->recv = dual_tls_recv;
683 this->send = dual_tls_send;
689 * FIXME: set O_NONBLOCK on the accept'd fd.
690 * See djb's portability rants for details.
694 * Tell the event loop that we have a new FD.
695 * This can be called from a child thread...
697 radius_update_listener(this);
704 * Ensure that we always keep the correct counters.
707 static void common_socket_free(rad_listen_t *this)
709 listen_socket_t *sock = this->data;
711 if (sock->proto != IPPROTO_TCP) return;
714 * Decrement the number of connections.
716 if (sock->parent && (sock->parent->limit.num_connections > 0)) {
717 sock->parent->limit.num_connections--;
719 if (sock->client && sock->client->limit.num_connections > 0) {
720 sock->client->limit.num_connections--;
722 if (sock->home && sock->home->limit.num_connections > 0) {
723 sock->home->limit.num_connections--;
727 #define common_socket_free NULL
731 * This function is stupid and complicated.
733 int common_socket_print(rad_listen_t const *this, char *buffer, size_t bufsize)
736 listen_socket_t *sock = this->data;
737 char const *name = master_listen[this->type].name;
739 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
740 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
748 if (sock->interface) {
749 ADDSTRING(" interface ");
750 ADDSTRING(sock->interface);
754 if (this->recv == dual_tcp_accept) {
755 ADDSTRING(" proto tcp");
761 * TCP sockets get printed a little differently, to make
762 * it clear what's going on.
765 ADDSTRING(" from client (");
766 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
770 snprintf(buffer, bufsize, "%d", sock->other_port);
774 if ((sock->my_ipaddr.af == AF_INET) &&
775 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
776 strlcpy(buffer, "*", bufsize);
778 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
783 snprintf(buffer, bufsize, "%d", sock->my_port);
787 ADDSTRING(", virtual-server=");
788 ADDSTRING(this->server);
798 * Maybe it's a socket that we opened to a home server.
800 if ((sock->proto == IPPROTO_TCP) &&
801 (this->type == RAD_LISTEN_PROXY)) {
803 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
807 snprintf(buffer, bufsize, "%d", sock->my_port);
809 ADDSTRING(") -> home_server (");
811 if ((sock->other_ipaddr.af == AF_INET) &&
812 (sock->other_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
813 strlcpy(buffer, "*", bufsize);
815 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
820 snprintf(buffer, bufsize, "%d", sock->other_port);
827 #endif /* WITH_PROXY */
828 #endif /* WITH_TCP */
830 ADDSTRING(" address ");
832 if ((sock->my_ipaddr.af == AF_INET) &&
833 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
834 strlcpy(buffer, "*", bufsize);
836 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
841 snprintf(buffer, bufsize, "%d", sock->my_port);
852 ADDSTRING(" as server ");
853 ADDSTRING(this->server);
862 extern bool check_config; /* radiusd.c */
864 static CONF_PARSER performance_config[] = {
865 { "skip_duplicate_checks", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rad_listen_t, nodup), NULL },
867 { "synchronous", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rad_listen_t, synchronous), NULL },
869 { "workers", FR_CONF_OFFSET(PW_TYPE_INTEGER, rad_listen_t, workers), NULL },
871 { NULL, -1, 0, NULL, NULL } /* end the list */
875 static CONF_PARSER limit_config[] = {
876 { "max_pps", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_socket_t, max_rate), NULL },
879 { "max_connections", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_socket_t, limit.max_connections), "16" },
880 { "lifetime", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_socket_t, limit.lifetime), "0" },
881 { "idle_timeout", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_socket_t, limit.idle_timeout), STRINGIFY(30) },
884 { NULL, -1, 0, NULL, NULL } /* end the list */
888 * Parse an authentication or accounting socket.
890 int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
893 uint16_t listen_port;
895 listen_socket_t *sock = this->data;
896 char const *section_name = NULL;
897 CONF_SECTION *client_cs, *parentcs;
905 memset(&ipaddr, 0, sizeof(ipaddr));
906 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
908 rcode = cf_item_parse(cs, "ipaddr", FR_ITEM_POINTER(PW_TYPE_IP_ADDR, &ipaddr), NULL);
909 if (rcode < 0) return -1;
910 if (rcode != 0) rcode = cf_item_parse(cs, "ipv4addr", FR_ITEM_POINTER(PW_TYPE_IPV4_ADDR, &ipaddr), NULL);
911 if (rcode < 0) return -1;
912 if (rcode != 0) rcode = cf_item_parse(cs, "ipv6addr", FR_ITEM_POINTER(PW_TYPE_IPV6_ADDR, &ipaddr), NULL);
913 if (rcode < 0) return -1;
915 cf_log_err_cs(cs, "No address specified in listen section");
919 rcode = cf_item_parse(cs, "port", FR_ITEM_POINTER(PW_TYPE_SHORT, &listen_port), "0");
920 if (rcode < 0) return -1;
922 sock->proto = IPPROTO_UDP;
924 if (cf_pair_find(cs, "proto")) {
927 "System does not support the TCP protocol. Delete this line from the configuration file");
930 char const *proto = NULL;
935 rcode = cf_item_parse(cs, "proto", FR_ITEM_POINTER(PW_TYPE_STRING, &proto), "udp");
936 if (rcode < 0) return -1;
938 if (strcmp(proto, "udp") == 0) {
939 sock->proto = IPPROTO_UDP;
941 } else if (strcmp(proto, "tcp") == 0) {
942 sock->proto = IPPROTO_TCP;
945 "Unknown proto name \"%s\"", proto);
950 * TCP requires a destination IP for sockets.
951 * UDP doesn't, so it's allowed.
954 if ((this->type == RAD_LISTEN_PROXY) &&
955 (sock->proto != IPPROTO_UDP)) {
957 "Proxy listeners can only listen on proto = udp");
960 #endif /* WITH_PROXY */
963 tls = cf_section_sub_find(cs, "tls");
967 * Don't allow TLS configurations for UDP sockets.
969 if (sock->proto != IPPROTO_TCP) {
971 "TLS transport is not available for UDP sockets");
976 * If unset, set to default.
978 if (listen_port == 0) listen_port = PW_RADIUS_TLS_PORT;
980 this->tls = tls_server_conf_parse(tls);
985 #ifdef HAVE_PTRHEAD_H
986 if (pthread_mutex_init(&sock->mutex, NULL) < 0) {
996 * Built without TLS. Disallow it.
998 if (cf_section_sub_find(cs, "tls")) {
1000 "TLS transport is not available in this executable");
1003 #endif /* WITH_TLS */
1005 #endif /* WITH_TCP */
1008 * No "proto" field. Disallow TLS.
1010 } else if (cf_section_sub_find(cs, "tls")) {
1012 "TLS transport is not available in this \"listen\" section");
1017 * Magical tuning methods!
1019 subcs = cf_section_sub_find(cs, "performance");
1021 rcode = cf_section_parse(subcs, this,
1022 performance_config);
1023 if (rcode < 0) return -1;
1025 if (this->synchronous && sock->max_rate) {
1026 WARN("Setting 'max_pps' is incompatible with 'synchronous'. Disabling 'max_pps'");
1030 if (!this->synchronous && this->workers) {
1031 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
1036 subcs = cf_section_sub_find(cs, "limit");
1038 rcode = cf_section_parse(subcs, sock,
1040 if (rcode < 0) return -1;
1042 if (sock->max_rate && ((sock->max_rate < 10) || (sock->max_rate > 1000000))) {
1044 "Invalid value for \"max_pps\"");
1049 if ((sock->limit.idle_timeout > 0) && (sock->limit.idle_timeout < 5)) {
1050 WARN("Setting idle_timeout to 5");
1051 sock->limit.idle_timeout = 5;
1054 if ((sock->limit.lifetime > 0) && (sock->limit.lifetime < 5)) {
1055 WARN("Setting lifetime to 5");
1056 sock->limit.lifetime = 5;
1059 if ((sock->limit.lifetime > 0) && (sock->limit.idle_timeout > sock->limit.lifetime)) {
1060 WARN("Setting idle_timeout to 0");
1061 sock->limit.idle_timeout = 0;
1065 * Force no duplicate detection for TCP sockets.
1067 if (sock->proto == IPPROTO_TCP) {
1072 sock->limit.max_connections = 60;
1073 sock->limit.idle_timeout = 30;
1074 sock->limit.lifetime = 0;
1078 sock->my_ipaddr = ipaddr;
1079 sock->my_port = listen_port;
1084 * Until there is a side effects free way of forwarding a
1085 * request to another virtual server, this check is invalid,
1086 * and should be left disabled.
1089 if (home_server_find(&sock->my_ipaddr, sock->my_port, sock->proto)) {
1092 ERROR("We have been asked to listen on %s port %d, which is also listed as a "
1093 "home server. This can create a proxy loop",
1094 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)), sock->my_port);
1098 return 0; /* don't do anything */
1103 * If we can bind to interfaces, do so,
1106 if (cf_pair_find(cs, "interface")) {
1108 CONF_PAIR *cp = cf_pair_find(cs, "interface");
1110 rad_assert(cp != NULL);
1111 value = cf_pair_value(cp);
1114 "No interface name given");
1117 sock->interface = value;
1122 * If we can do broadcasts..
1124 if (cf_pair_find(cs, "broadcast")) {
1125 #ifndef SO_BROADCAST
1127 "System does not support broadcast sockets. Delete this line from the configuration file");
1131 CONF_PAIR *cp = cf_pair_find(cs, "broadcast");
1133 if (this->type != RAD_LISTEN_DHCP) {
1135 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file");
1139 rad_assert(cp != NULL);
1140 value = cf_pair_value(cp);
1143 "No broadcast value given");
1148 * Hack... whatever happened to cf_section_parse?
1150 sock->broadcast = (strcmp(value, "yes") == 0);
1156 * And bind it to the port.
1158 if (listen_bind(this) < 0) {
1161 "Error binding to port for %s port %d",
1162 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
1169 * Proxy sockets don't have clients.
1171 if (this->type == RAD_LISTEN_PROXY) return 0;
1175 * The more specific configurations are preferred to more
1179 parentcs = cf_top_section(cs);
1180 rcode = cf_item_parse(cs, "clients", FR_ITEM_POINTER(PW_TYPE_STRING, §ion_name), NULL);
1181 if (rcode < 0) return -1; /* bad string */
1184 * Explicit list given: use it.
1186 client_cs = cf_section_sub_find_name2(parentcs, "clients", section_name);
1188 client_cs = cf_section_find(section_name);
1192 "Failed to find clients %s {...}",
1196 } /* else there was no "clients = " entry. */
1199 CONF_SECTION *server_cs;
1201 server_cs = cf_section_sub_find_name2(parentcs,
1205 * Found a "server foo" section. If there are clients
1209 (cf_section_sub_find(server_cs, "client") != NULL)) {
1210 client_cs = server_cs;
1215 * Still nothing. Look for global clients.
1217 if (!client_cs) client_cs = parentcs;
1220 sock->clients = clients_parse_section(client_cs, (this->tls != NULL));
1222 sock->clients = clients_parse_section(client_cs, false);
1224 if (!sock->clients) {
1226 "Failed to load clients for this listen section");
1231 if (sock->proto == IPPROTO_TCP) {
1233 * Re-write the listener receive function to
1234 * allow us to accept the socket.
1236 this->recv = dual_tcp_accept;
1244 * Send an authentication response packet
1246 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1248 rad_assert(request->listener == listener);
1249 rad_assert(listener->send == auth_socket_send);
1251 if (request->reply->code == 0) return 0;
1253 #ifdef WITH_UDPFROMTO
1255 * Overwrite the src ip address on the outbound packet
1256 * with the one specified by the client.
1257 * This is useful to work around broken DSR implementations
1258 * and other routing issues.
1260 if (request->client->src_ipaddr.af != AF_UNSPEC) {
1261 request->reply->src_ipaddr = request->client->src_ipaddr;
1265 if (rad_send(request->reply, request->packet,
1266 request->client->secret) < 0) {
1267 RERROR("Failed sending reply: %s",
1276 #ifdef WITH_ACCOUNTING
1278 * Send an accounting response packet (or not)
1280 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1282 rad_assert(request->listener == listener);
1283 rad_assert(listener->send == acct_socket_send);
1286 * Accounting reject's are silently dropped.
1288 * We do it here to avoid polluting the rest of the
1289 * code with this knowledge
1291 if (request->reply->code == 0) return 0;
1293 #ifdef WITH_UDPFROMTO
1295 * Overwrite the src ip address on the outbound packet
1296 * with the one specified by the client.
1297 * This is useful to work around broken DSR implementations
1298 * and other routing issues.
1300 if (request->client->src_ipaddr.af != AF_UNSPEC) {
1301 request->reply->src_ipaddr = request->client->src_ipaddr;
1305 if (rad_send(request->reply, request->packet,
1306 request->client->secret) < 0) {
1307 RERROR("Failed sending reply: %s",
1318 * Send a packet to a home server.
1320 * FIXME: have different code for proxy auth & acct!
1322 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1324 rad_assert(request->proxy_listener == listener);
1325 rad_assert(listener->send == proxy_socket_send);
1327 if (rad_send(request->proxy, NULL,
1328 request->home_server->secret) < 0) {
1329 RERROR("Failed sending proxied request: %s",
1340 * Check if an incoming request is "ok"
1342 * It takes packets, not requests. It sees if the packet looks
1343 * OK. If so, it does a number of sanity checks on it.
1345 static int stats_socket_recv(rad_listen_t *listener)
1350 RADIUS_PACKET *packet;
1351 RADCLIENT *client = NULL;
1352 fr_ipaddr_t src_ipaddr;
1354 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1355 if (rcode < 0) return 0;
1357 FR_STATS_INC(auth, total_requests);
1359 if (rcode < 20) { /* AUTH_HDR_LEN */
1360 FR_STATS_INC(auth, total_malformed_requests);
1364 if ((client = client_listener_find(listener,
1365 &src_ipaddr, src_port)) == NULL) {
1366 rad_recv_discard(listener->fd);
1367 FR_STATS_INC(auth, total_invalid_requests);
1371 FR_STATS_TYPE_INC(client->auth.total_requests);
1374 * We only understand Status-Server on this socket.
1376 if (code != PW_CODE_STATUS_SERVER) {
1377 DEBUG("Ignoring packet code %d sent to Status-Server port",
1379 rad_recv_discard(listener->fd);
1380 FR_STATS_INC(auth, total_unknown_types);
1385 * Now that we've sanity checked everything, receive the
1388 packet = rad_recv(listener->fd, 1); /* require message authenticator */
1390 FR_STATS_INC(auth, total_malformed_requests);
1391 DEBUG("%s", fr_strerror());
1395 if (!request_receive(listener, packet, client, rad_status_server)) {
1396 FR_STATS_INC(auth, total_packets_dropped);
1407 * Check if an incoming request is "ok"
1409 * It takes packets, not requests. It sees if the packet looks
1410 * OK. If so, it does a number of sanity checks on it.
1412 static int auth_socket_recv(rad_listen_t *listener)
1417 RADIUS_PACKET *packet;
1418 RAD_REQUEST_FUNP fun = NULL;
1419 RADCLIENT *client = NULL;
1420 fr_ipaddr_t src_ipaddr;
1422 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1423 if (rcode < 0) return 0;
1425 FR_STATS_INC(auth, total_requests);
1427 if (rcode < 20) { /* AUTH_HDR_LEN */
1428 FR_STATS_INC(auth, total_malformed_requests);
1432 if ((client = client_listener_find(listener,
1433 &src_ipaddr, src_port)) == NULL) {
1434 rad_recv_discard(listener->fd);
1435 FR_STATS_INC(auth, total_invalid_requests);
1439 FR_STATS_TYPE_INC(client->auth.total_requests);
1442 * Some sanity checks, based on the packet code.
1445 case PW_CODE_ACCESS_REQUEST:
1446 fun = rad_authenticate;
1449 case PW_CODE_STATUS_SERVER:
1450 if (!main_config.status_server) {
1451 rad_recv_discard(listener->fd);
1452 FR_STATS_INC(auth, total_unknown_types);
1453 WARN("Ignoring Status-Server request due to security configuration");
1456 fun = rad_status_server;
1460 rad_recv_discard(listener->fd);
1461 FR_STATS_INC(auth,total_unknown_types);
1463 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
1464 code, client->shortname, src_port);
1467 } /* switch over packet types */
1470 * Now that we've sanity checked everything, receive the
1473 packet = rad_recv(listener->fd, client->message_authenticator);
1475 FR_STATS_INC(auth, total_malformed_requests);
1476 DEBUG("%s", fr_strerror());
1481 #ifdef WITH_UDPFROMTO
1483 * This is a NICE Mac OSX bug. Create an interface with
1484 * two IP address, and then configure one listener for
1485 * each IP address. Send thousands of packets to one
1486 * address, and some will show up on the OTHER socket.
1488 * This hack works ONLY if the clients are global. If
1489 * each listener has the same client IP, but with
1490 * different secrets, then it will fail the rad_recv()
1491 * check above, and there's nothing you can do.
1494 listen_socket_t *sock = listener->data;
1495 rad_listen_t *other;
1497 other = listener_find_byipaddr(&packet->dst_ipaddr,
1498 packet->dst_port, sock->proto);
1499 if (other) listener = other;
1505 if (!request_receive(listener, packet, client, fun)) {
1506 FR_STATS_INC(auth, total_packets_dropped);
1515 #ifdef WITH_ACCOUNTING
1517 * Receive packets from an accounting socket
1519 static int acct_socket_recv(rad_listen_t *listener)
1524 RADIUS_PACKET *packet;
1525 RAD_REQUEST_FUNP fun = NULL;
1526 RADCLIENT *client = NULL;
1527 fr_ipaddr_t src_ipaddr;
1529 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1530 if (rcode < 0) return 0;
1532 FR_STATS_INC(acct, total_requests);
1534 if (rcode < 20) { /* AUTH_HDR_LEN */
1535 FR_STATS_INC(acct, total_malformed_requests);
1539 if ((client = client_listener_find(listener,
1540 &src_ipaddr, src_port)) == NULL) {
1541 rad_recv_discard(listener->fd);
1542 FR_STATS_INC(acct, total_invalid_requests);
1546 FR_STATS_TYPE_INC(client->acct.total_requests);
1549 * Some sanity checks, based on the packet code.
1552 case PW_CODE_ACCOUNTING_REQUEST:
1553 fun = rad_accounting;
1556 case PW_CODE_STATUS_SERVER:
1557 if (!main_config.status_server) {
1558 rad_recv_discard(listener->fd);
1559 FR_STATS_INC(acct, total_unknown_types);
1561 WARN("Ignoring Status-Server request due to security configuration");
1564 fun = rad_status_server;
1568 rad_recv_discard(listener->fd);
1569 FR_STATS_INC(acct, total_unknown_types);
1571 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1572 code, client->shortname, src_port);
1574 } /* switch over packet types */
1577 * Now that we've sanity checked everything, receive the
1580 packet = rad_recv(listener->fd, 0);
1582 FR_STATS_INC(acct, total_malformed_requests);
1583 ERROR("%s", fr_strerror());
1588 * There can be no duplicate accounting packets.
1590 if (!request_receive(listener, packet, client, fun)) {
1591 FR_STATS_INC(acct, total_packets_dropped);
1602 static int do_proxy(REQUEST *request)
1606 if (request->in_proxy_hash ||
1607 (request->proxy_reply && (request->proxy_reply->code != 0))) {
1611 vp = pairfind(request->config_items, PW_HOME_SERVER_POOL, 0, TAG_ANY);
1614 if (!home_pool_byname(vp->vp_strvalue, HOME_TYPE_COA)) {
1615 REDEBUG2("Cannot proxy to unknown pool %s",
1624 * Receive a CoA packet.
1626 static int rad_coa_recv(REQUEST *request)
1628 int rcode = RLM_MODULE_OK;
1633 * Get the correct response
1635 switch (request->packet->code) {
1636 case PW_CODE_COA_REQUEST:
1637 ack = PW_CODE_COA_ACK;
1638 nak = PW_CODE_COA_NAK;
1641 case PW_CODE_DISCONNECT_REQUEST:
1642 ack = PW_CODE_DISCONNECT_ACK;
1643 nak = PW_CODE_DISCONNECT_NAK;
1646 default: /* shouldn't happen */
1647 return RLM_MODULE_FAIL;
1651 #define WAS_PROXIED (request->proxy)
1653 #define WAS_PROXIED (0)
1658 * RFC 5176 Section 3.3. If we have a CoA-Request
1659 * with Service-Type = Authorize-Only, it MUST
1660 * have a State attribute in it.
1662 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE, 0, TAG_ANY);
1663 if (request->packet->code == PW_CODE_COA_REQUEST) {
1664 if (vp && (vp->vp_integer == 17)) {
1665 vp = pairfind(request->packet->vps, PW_STATE, 0, TAG_ANY);
1666 if (!vp || (vp->length == 0)) {
1667 REDEBUG("CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1668 request->reply->code = PW_CODE_COA_NAK;
1669 return RLM_MODULE_FAIL;
1674 * RFC 5176, Section 3.2.
1676 REDEBUG("Disconnect-Request MUST NOT contain a Service-Type attribute");
1677 request->reply->code = PW_CODE_DISCONNECT_NAK;
1678 return RLM_MODULE_FAIL;
1681 rcode = process_recv_coa(0, request);
1683 case RLM_MODULE_FAIL:
1684 case RLM_MODULE_INVALID:
1685 case RLM_MODULE_REJECT:
1686 case RLM_MODULE_USERLOCK:
1688 request->reply->code = nak;
1691 case RLM_MODULE_HANDLED:
1694 case RLM_MODULE_NOOP:
1695 case RLM_MODULE_NOTFOUND:
1697 case RLM_MODULE_UPDATED:
1698 if (do_proxy(request)) return RLM_MODULE_OK;
1699 request->reply->code = ack;
1702 } else if (request->proxy_reply) {
1704 * Start the reply code with the proxy reply
1707 request->reply->code = request->proxy_reply->code;
1711 * Copy State from the request to the reply.
1712 * See RFC 5176 Section 3.3.
1714 vp = paircopy2(request->reply, request->packet->vps, PW_STATE, 0, TAG_ANY);
1715 if (vp) pairadd(&request->reply->vps, vp);
1718 * We may want to over-ride the reply.
1720 if (request->reply->code) {
1721 rcode = process_send_coa(0, request);
1724 * We need to send CoA-NAK back if Service-Type
1725 * is Authorize-Only. Rely on the user's policy
1726 * to do that. We're not a real NAS, so this
1727 * restriction doesn't (ahem) apply to us.
1729 case RLM_MODULE_FAIL:
1730 case RLM_MODULE_INVALID:
1731 case RLM_MODULE_REJECT:
1732 case RLM_MODULE_USERLOCK:
1735 * Over-ride an ACK with a NAK
1737 request->reply->code = nak;
1740 case RLM_MODULE_HANDLED:
1743 case RLM_MODULE_NOOP:
1744 case RLM_MODULE_NOTFOUND:
1746 case RLM_MODULE_UPDATED:
1748 * Do NOT over-ride a previously set value.
1749 * Otherwise an "ok" here will re-write a
1752 if (request->reply->code == 0) {
1753 request->reply->code = ack;
1759 return RLM_MODULE_OK;
1764 * Check if an incoming request is "ok"
1766 * It takes packets, not requests. It sees if the packet looks
1767 * OK. If so, it does a number of sanity checks on it.
1769 static int coa_socket_recv(rad_listen_t *listener)
1774 RADIUS_PACKET *packet;
1775 RAD_REQUEST_FUNP fun = NULL;
1776 RADCLIENT *client = NULL;
1777 fr_ipaddr_t src_ipaddr;
1779 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1780 if (rcode < 0) return 0;
1782 if (rcode < 20) { /* AUTH_HDR_LEN */
1783 FR_STATS_INC(coa, total_requests);
1784 FR_STATS_INC(coa, total_malformed_requests);
1788 if ((client = client_listener_find(listener,
1789 &src_ipaddr, src_port)) == NULL) {
1790 rad_recv_discard(listener->fd);
1791 FR_STATS_INC(coa, total_requests);
1792 FR_STATS_INC(coa, total_invalid_requests);
1797 * Some sanity checks, based on the packet code.
1800 case PW_CODE_COA_REQUEST:
1801 FR_STATS_INC(coa, total_requests);
1805 case PW_CODE_DISCONNECT_REQUEST:
1806 FR_STATS_INC(dsc, total_requests);
1811 rad_recv_discard(listener->fd);
1812 FR_STATS_INC(coa, total_unknown_types);
1813 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1814 code, client->shortname, src_port);
1816 } /* switch over packet types */
1819 * Now that we've sanity checked everything, receive the
1822 packet = rad_recv(listener->fd, client->message_authenticator);
1824 FR_STATS_INC(coa, total_malformed_requests);
1825 DEBUG("%s", fr_strerror());
1829 if (!request_receive(listener, packet, client, fun)) {
1830 FR_STATS_INC(coa, total_packets_dropped);
1841 * Recieve packets from a proxy socket.
1843 static int proxy_socket_recv(rad_listen_t *listener)
1845 RADIUS_PACKET *packet;
1848 packet = rad_recv(listener->fd, 0);
1850 ERROR("%s", fr_strerror());
1855 * FIXME: Client MIB updates?
1857 switch(packet->code) {
1858 case PW_CODE_ACCESS_ACCEPT:
1859 case PW_CODE_ACCESS_CHALLENGE:
1860 case PW_CODE_ACCESS_REJECT:
1863 #ifdef WITH_ACCOUNTING
1864 case PW_CODE_ACCOUNTING_RESPONSE:
1869 case PW_CODE_DISCONNECT_ACK:
1870 case PW_CODE_DISCONNECT_NAK:
1871 case PW_CODE_COA_ACK:
1872 case PW_CODE_COA_NAK:
1878 * FIXME: Update MIB for packet types?
1880 ERROR("Invalid packet code %d sent to a proxy port "
1881 "from home server %s port %d - ID %d : IGNORED",
1883 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1884 packet->src_port, packet->id);
1889 if (!request_proxy_reply(packet)) {
1899 * Recieve packets from a proxy socket.
1901 static int proxy_socket_tcp_recv(rad_listen_t *listener)
1903 RADIUS_PACKET *packet;
1904 listen_socket_t *sock = listener->data;
1907 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return 0;
1909 packet = fr_tcp_recv(listener->fd, 0);
1911 listener->status = RAD_LISTEN_STATUS_EOL;
1912 radius_update_listener(listener);
1917 * FIXME: Client MIB updates?
1919 switch(packet->code) {
1920 case PW_CODE_ACCESS_ACCEPT:
1921 case PW_CODE_ACCESS_CHALLENGE:
1922 case PW_CODE_ACCESS_REJECT:
1925 #ifdef WITH_ACCOUNTING
1926 case PW_CODE_ACCOUNTING_RESPONSE:
1932 * FIXME: Update MIB for packet types?
1934 ERROR("Invalid packet code %d sent to a proxy port "
1935 "from home server %s port %d - ID %d : IGNORED",
1937 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1938 packet->src_port, packet->id);
1943 packet->src_ipaddr = sock->other_ipaddr;
1944 packet->src_port = sock->other_port;
1945 packet->dst_ipaddr = sock->my_ipaddr;
1946 packet->dst_port = sock->my_port;
1949 * FIXME: Have it return an indication of packets that
1950 * are OK to ignore (dups, too late), versus ones that
1951 * aren't OK to ignore (unknown response, spoofed, etc.)
1953 * Close the socket on bad packets...
1955 if (!request_proxy_reply(packet)) {
1960 sock->opened = sock->last_packet = time(NULL);
1968 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1970 if (!request->reply->code) return 0;
1972 if (rad_encode(request->reply, request->packet,
1973 request->client->secret) < 0) {
1974 RERROR("Failed encoding packet: %s",
1979 if (rad_sign(request->reply, request->packet,
1980 request->client->secret) < 0) {
1981 RERROR("Failed signing packet: %s",
1990 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1992 if (rad_verify(request->packet, NULL,
1993 request->client->secret) < 0) {
1997 return rad_decode(request->packet, NULL,
1998 request->client->secret);
2002 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
2004 if (rad_encode(request->proxy, NULL, request->home_server->secret) < 0) {
2005 RERROR("Failed encoding proxied packet: %s",
2010 if (rad_sign(request->proxy, NULL, request->home_server->secret) < 0) {
2011 RERROR("Failed signing proxied packet: %s",
2020 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
2023 * rad_verify is run in event.c, received_proxy_response()
2026 return rad_decode(request->proxy_reply, request->proxy,
2027 request->home_server->secret);
2031 #include "command.c"
2034 * Temporarily NOT const!
2036 static fr_protocol_t master_listen[RAD_LISTEN_MAX] = {
2038 { RLM_MODULE_INIT, "status", sizeof(listen_socket_t), NULL,
2039 common_socket_parse, NULL,
2040 stats_socket_recv, auth_socket_send,
2041 common_socket_print, client_socket_encode, client_socket_decode },
2044 * This always gets defined.
2046 { RLM_MODULE_INIT, "status", 0, NULL,
2047 NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
2052 { RLM_MODULE_INIT, "proxy", sizeof(listen_socket_t), NULL,
2053 common_socket_parse, common_socket_free,
2054 proxy_socket_recv, proxy_socket_send,
2055 common_socket_print, proxy_socket_encode, proxy_socket_decode },
2058 /* authentication */
2059 { RLM_MODULE_INIT, "auth", sizeof(listen_socket_t), NULL,
2060 common_socket_parse, common_socket_free,
2061 auth_socket_recv, auth_socket_send,
2062 common_socket_print, client_socket_encode, client_socket_decode },
2064 #ifdef WITH_ACCOUNTING
2066 { RLM_MODULE_INIT, "acct", sizeof(listen_socket_t), NULL,
2067 common_socket_parse, common_socket_free,
2068 acct_socket_recv, acct_socket_send,
2069 common_socket_print, client_socket_encode, client_socket_decode},
2074 { RLM_MODULE_INIT, "detail", sizeof(listen_detail_t), NULL,
2075 detail_parse, detail_free,
2076 detail_recv, detail_send,
2077 detail_print, detail_encode, detail_decode },
2081 /* vlan query protocol */
2082 { 0, "vmps", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
2086 /* dhcp query protocol */
2087 { 0, "dhcp", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
2090 #ifdef WITH_COMMAND_SOCKET
2091 /* TCP command socket */
2092 { RLM_MODULE_INIT, "control", sizeof(fr_command_socket_t), NULL,
2093 command_socket_parse, command_socket_free,
2094 command_domain_accept, command_domain_send,
2095 command_socket_print, command_socket_encode, command_socket_decode },
2099 /* Change of Authorization */
2100 { RLM_MODULE_INIT, "coa", sizeof(listen_socket_t), NULL,
2101 common_socket_parse, NULL,
2102 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
2103 common_socket_print, client_socket_encode, client_socket_decode },
2111 * Binds a listener to a socket.
2113 static int listen_bind(rad_listen_t *this)
2116 struct sockaddr_storage salocal;
2118 listen_socket_t *sock = this->data;
2120 #define proto_for_port "udp"
2121 #define sock_type SOCK_DGRAM
2123 char const *proto_for_port = "udp";
2124 int sock_type = SOCK_DGRAM;
2126 if (sock->proto == IPPROTO_TCP) {
2128 if (this->type == RAD_LISTEN_VQP) {
2129 ERROR("VQP does not support TCP transport");
2134 proto_for_port = "tcp";
2135 sock_type = SOCK_STREAM;
2140 * If the port is zero, then it means the appropriate
2141 * thing from /etc/services.
2143 if (sock->my_port == 0) {
2144 struct servent *svp;
2146 switch (this->type) {
2147 case RAD_LISTEN_AUTH:
2148 svp = getservbyname ("radius", proto_for_port);
2150 sock->my_port = ntohs(svp->s_port);
2152 sock->my_port = PW_AUTH_UDP_PORT;
2156 #ifdef WITH_ACCOUNTING
2157 case RAD_LISTEN_ACCT:
2158 svp = getservbyname ("radacct", proto_for_port);
2160 sock->my_port = ntohs(svp->s_port);
2162 sock->my_port = PW_ACCT_UDP_PORT;
2168 case RAD_LISTEN_PROXY:
2169 /* leave it at zero */
2174 case RAD_LISTEN_VQP:
2175 sock->my_port = 1589;
2179 #ifdef WITH_COMMAND_SOCKET
2180 case RAD_LISTEN_COMMAND:
2181 sock->my_port = PW_RADMIN_PORT;
2186 case RAD_LISTEN_COA:
2187 svp = getservbyname ("radius-dynauth", "udp");
2189 sock->my_port = ntohs(svp->s_port);
2191 sock->my_port = PW_COA_UDP_PORT;
2197 WARN("Internal sanity check failed in binding to socket. Ignoring problem");
2203 * Don't open sockets if we're checking the config.
2211 * Copy fr_socket() here, as we may need to bind to a device.
2213 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
2217 this->print(this, buffer, sizeof(buffer));
2219 ERROR("Failed opening %s: %s", buffer, fr_syserror(errno));
2225 * We don't want child processes inheriting these
2228 rcode = fcntl(this->fd, F_GETFD);
2230 if (fcntl(this->fd, F_SETFD, rcode | FD_CLOEXEC) < 0) {
2232 ERROR("Failed setting close on exec: %s", fr_syserror(errno));
2239 * Bind to a device BEFORE touching IP addresses.
2241 if (sock->interface) {
2242 #ifdef SO_BINDTODEVICE
2245 memset(&ifreq, 0, sizeof(ifreq));
2246 strlcpy(ifreq.ifr_name, sock->interface, sizeof(ifreq.ifr_name));
2249 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
2250 (char *)&ifreq, sizeof(ifreq));
2254 ERROR("Failed binding to interface %s: %s",
2255 sock->interface, fr_syserror(errno));
2257 } /* else it worked. */
2259 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2260 #ifdef HAVE_NET_IF_H
2262 * Odds are that any system supporting "bind to
2263 * device" also supports IPv6, so this next bit
2264 * isn't necessary. But it's here for
2267 * If we're doing IPv6, and the scope hasn't yet
2268 * been defined, set the scope to the scope of
2271 if (sock->my_ipaddr.af == AF_INET6) {
2272 if (sock->my_ipaddr.scope == 0) {
2273 sock->my_ipaddr.scope = if_nametoindex(sock->interface);
2274 if (sock->my_ipaddr.scope == 0) {
2276 ERROR("Failed finding interface %s: %s",
2277 sock->interface, fr_syserror(errno));
2280 } /* else scope was defined: we're OK. */
2285 * IPv4: no link local addresses,
2286 * and no bind to device.
2290 ERROR("Failed binding to interface %s: \"bind to device\" is unsupported", sock->interface);
2297 if (sock->proto == IPPROTO_TCP) {
2300 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2302 ERROR("Failed to reuse address: %s", fr_syserror(errno));
2308 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
2309 else /* UDP sockets get UDPfromto */
2312 #ifdef WITH_UDPFROMTO
2314 * Initialize udpfromto for all sockets.
2316 if (udpfromto_init(this->fd) != 0) {
2317 ERROR("Failed initializing udpfromto: %s",
2318 fr_syserror(errno));
2325 * Set up sockaddr stuff.
2327 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
2332 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2333 if (sock->my_ipaddr.af == AF_INET6) {
2335 * Listening on '::' does NOT get you IPv4 to
2336 * IPv6 mapping. You've got to listen on an IPv4
2337 * address, too. This makes the rest of the server
2338 * design a little simpler.
2342 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2345 if (setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2346 (char *)&on, sizeof(on)) < 0) {
2347 ERROR("Failed setting socket to IPv6 "
2348 "only: %s", fr_syserror(errno));
2354 #endif /* IPV6_V6ONLY */
2356 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2358 if (sock->my_ipaddr.af == AF_INET) {
2361 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2363 * Disable PMTU discovery. On Linux, this
2364 * also makes sure that the "don't fragment"
2367 flag = IP_PMTUDISC_DONT;
2368 if (setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2369 &flag, sizeof(flag)) < 0) {
2370 ERROR("Failed disabling PMTU discovery: %s",
2371 fr_syserror(errno));
2378 #if defined(IP_DONTFRAG)
2380 * Ensure that the "don't fragment" flag is zero.
2383 if (setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2384 &flag, sizeof(flag)) < 0) {
2385 ERROR("Failed setting don't fragment flag: %s",
2386 fr_syserror(errno));
2396 if (sock->broadcast) {
2399 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2400 ERROR("Can't set broadcast option: %s",
2401 fr_syserror(errno));
2409 * May be binding to priviledged ports.
2411 if (sock->my_port != 0) {
2413 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2419 this->print(this, buffer, sizeof(buffer));
2420 ERROR("Failed binding to %s: %s\n",
2421 buffer, fr_syserror(errno));
2426 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2427 * kernel instead binds us to a 1.2.3.4. If this
2428 * happens, notice, and remember our real IP.
2431 struct sockaddr_storage src;
2432 socklen_t sizeof_src = sizeof(src);
2434 memset(&src, 0, sizeof_src);
2435 if (getsockname(this->fd, (struct sockaddr *) &src,
2437 ERROR("Failed getting socket name: %s",
2438 fr_syserror(errno));
2442 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2443 &sock->my_ipaddr, &sock->my_port)) {
2444 ERROR("Socket has unsupported address family");
2451 if (sock->proto == IPPROTO_TCP) {
2453 * If there are hard-coded worker threads, OR
2454 * it's a TLS connection, it's blocking.
2456 * Otherwise, they're non-blocking.
2461 && (this->type == RAD_LISTEN_PROXY) && !this->tls
2465 if (fr_nonblock(this->fd) < 0) {
2467 ERROR("Failed setting non-blocking on socket: %s",
2468 fr_syserror(errno));
2474 * Allow a backlog of 8 listeners, but only for incoming interfaces.
2477 if (this->type != RAD_LISTEN_PROXY)
2479 if (listen(this->fd, 8) < 0) {
2481 ERROR("Failed in listen(): %s", fr_syserror(errno));
2488 * Mostly for proxy sockets.
2490 sock->other_ipaddr.af = sock->my_ipaddr.af;
2493 * Don't screw up other people.
2495 #undef proto_for_port
2502 static int _listener_free(rad_listen_t *this)
2505 * Other code may have eaten the FD.
2507 if (this->fd >= 0) close(this->fd);
2509 if (master_listen[this->type].free) {
2510 master_listen[this->type].free(this);
2514 if ((this->type == RAD_LISTEN_AUTH)
2516 || (this->type == RAD_LISTEN_ACCT)
2519 || (this->type == RAD_LISTEN_PROXY)
2521 #ifdef WITH_COMMAND_SOCKET
2522 || ((this->type == RAD_LISTEN_COMMAND) &&
2523 (((fr_command_socket_t *) this->data)->magic != COMMAND_SOCKET_MAGIC))
2526 listen_socket_t *sock = this->data;
2528 rad_free(&sock->packet);
2530 rad_assert(sock->ev == NULL);
2534 * Note that we do NOT free this->tls, as the
2535 * pointer is parented by its CONF_SECTION. It
2536 * may be used by multiple listeners.
2539 TALLOC_FREE(sock->ssn);
2540 TALLOC_FREE(sock->request);
2541 #ifdef HAVE_PTHREAD_H
2542 pthread_mutex_destroy(&(sock->mutex));
2545 #endif /* WITH_TLS */
2547 #endif /* WITH_TCP */
2553 * Allocate & initialize a new listener.
2555 static rad_listen_t *listen_alloc(TALLOC_CTX *ctx, RAD_LISTEN_TYPE type)
2559 this = talloc_zero(ctx, rad_listen_t);
2562 this->recv = master_listen[this->type].recv;
2563 this->send = master_listen[this->type].send;
2564 this->print = master_listen[this->type].print;
2565 this->encode = master_listen[this->type].encode;
2566 this->decode = master_listen[this->type].decode;
2568 talloc_set_destructor(this, _listener_free);
2570 this->data = talloc_zero_array(this, uint8_t, master_listen[this->type].inst_size);
2577 * Externally visible function for creating a new proxy LISTENER.
2579 * Not thread-safe, but all calls to it are protected by the
2580 * proxy mutex in event.c
2582 rad_listen_t *proxy_new_listener(home_server_t *home, uint16_t src_port)
2586 listen_socket_t *sock;
2589 if (!home) return NULL;
2591 rad_assert(home->server == NULL); /* we only open real sockets */
2593 if ((home->limit.max_connections > 0) &&
2594 (home->limit.num_connections >= home->limit.max_connections)) {
2595 RATE_LIMIT(INFO("Home server %s has too many open connections (%d)",
2596 home->name, home->limit.max_connections));
2601 if (home->last_failed_open == now) {
2602 WARN("Suppressing attempt to open socket to 'down' home server");
2606 this = listen_alloc(main_config.config, RAD_LISTEN_PROXY);
2609 sock->other_ipaddr = home->ipaddr;
2610 sock->other_port = home->port;
2612 if (home->name) /*Not the default proxy listener*/
2613 talloc_reference(sock, sock->home); /*In case dynamic home server is freed*/
2615 sock->my_ipaddr = home->src_ipaddr;
2616 sock->my_port = src_port;
2617 sock->proto = home->proto;
2620 * For error messages.
2622 this->print(this, buffer, sizeof(buffer));
2624 if (debug_flag >= 2) {
2625 DEBUG("Opening new proxy socket '%s'", buffer);
2629 sock->opened = sock->last_packet = now;
2631 if (home->proto == IPPROTO_TCP) {
2632 this->recv = proxy_socket_tcp_recv;
2635 * FIXME: connect() is blocking!
2636 * We do this with the proxy mutex locked, which may
2637 * cause large delays!
2639 * http://www.developerweb.net/forum/showthread.php?p=13486
2641 this->fd = fr_tcp_client_socket(&home->src_ipaddr,
2642 &home->ipaddr, home->port);
2645 this->fd = fr_socket(&home->src_ipaddr, src_port);
2648 this->print(this, buffer,sizeof(buffer));
2649 ERROR("Failed opening proxy socket '%s' : %s",
2650 buffer, fr_strerror());
2651 home->last_failed_open = now;
2659 if ((home->proto == IPPROTO_TCP) && home->tls) {
2660 DEBUG("Trying SSL to port %d\n", home->port);
2661 sock->ssn = tls_new_client_session(home->tls, this->fd);
2663 ERROR("Failed starting SSL to '%s'", buffer);
2664 home->last_failed_open = now;
2669 this->recv = proxy_tls_recv;
2670 this->send = proxy_tls_send;
2675 * Figure out which port we were bound to.
2677 if (sock->my_port == 0) {
2678 struct sockaddr_storage src;
2679 socklen_t sizeof_src = sizeof(src);
2681 memset(&src, 0, sizeof_src);
2682 if (getsockname(this->fd, (struct sockaddr *) &src,
2684 ERROR("Failed getting socket name for '%s': %s",
2685 buffer, fr_syserror(errno));
2686 home->last_failed_open = now;
2691 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2692 &sock->my_ipaddr, &sock->my_port)) {
2693 ERROR("Socket has unsupported address family for '%s'", buffer);
2694 home->last_failed_open = now;
2700 home->limit.num_connections++;
2706 static const FR_NAME_NUMBER listen_compare[] = {
2708 { "status", RAD_LISTEN_NONE },
2710 { "auth", RAD_LISTEN_AUTH },
2711 #ifdef WITH_ACCOUNTING
2712 { "acct", RAD_LISTEN_ACCT },
2713 { "auth+acct", RAD_LISTEN_AUTH },
2716 { "detail", RAD_LISTEN_DETAIL },
2719 { "proxy", RAD_LISTEN_PROXY },
2722 { "vmps", RAD_LISTEN_VQP },
2725 { "dhcp", RAD_LISTEN_DHCP },
2727 #ifdef WITH_COMMAND_SOCKET
2728 { "control", RAD_LISTEN_COMMAND },
2731 { "coa", RAD_LISTEN_COA },
2736 static int _free_proto_handle(lt_dlhandle *handle)
2742 static rad_listen_t *listen_parse(CONF_SECTION *cs, char const *server)
2745 char const *listen_type;
2750 CONF_SECTION *server_cs;
2753 cp = cf_pair_find(cs, "type");
2756 "No type specified in listen section");
2760 value = cf_pair_value(cp);
2763 "Type cannot be empty");
2767 snprintf(buffer, sizeof(buffer), "proto_%s", value);
2768 handle = lt_dlopenext(buffer);
2770 fr_protocol_t *proto;
2771 lt_dlhandle *marker;
2773 proto = dlsym(handle, buffer);
2776 "Failed linking to protocol %s : %s\n",
2782 type = fr_str2int(listen_compare, value, -1);
2783 rad_assert(type >= 0); /* shouldn't be able to compile an invalid type */
2785 memcpy(&master_listen[type], proto, sizeof(*proto));
2788 * Ensure handle gets closed if config section gets freed
2790 marker = talloc(cs, lt_dlhandle);
2792 talloc_set_destructor(marker, _free_proto_handle);
2794 if (master_listen[type].magic != RLM_MODULE_INIT) {
2795 ERROR("Failed to load protocol '%s' due to internal sanity check problem",
2796 master_listen[type].name);
2801 cf_log_info(cs, "listen {");
2804 rcode = cf_item_parse(cs, "type", FR_ITEM_POINTER(PW_TYPE_STRING, &listen_type), "");
2805 if (rcode < 0) return NULL;
2808 "No type specified in listen section");
2812 type = fr_str2int(listen_compare, listen_type, -1);
2815 "Invalid type \"%s\" in listen section.",
2821 * Allow listen sections in the default config to
2822 * refer to a server.
2825 rcode = cf_item_parse(cs, "virtual_server", FR_ITEM_POINTER(PW_TYPE_STRING, &server), NULL);
2826 if (rcode < 0) return NULL;
2831 * We were passed a virtual server, so the caller is
2832 * defining a proxy listener inside of a virtual server.
2833 * This isn't allowed right now.
2835 else if (type == RAD_LISTEN_PROXY) {
2836 ERROR("Error: listen type \"proxy\" Cannot appear in a virtual server section");
2842 * Set up cross-type data.
2844 this = listen_alloc(cs, type);
2845 this->server = server;
2850 * Special-case '+' for "auth+acct".
2852 if (strchr(listen_type, '+') != NULL) {
2858 * Call per-type parser.
2860 if (master_listen[type].parse(cs, this) < 0) {
2866 server_cs = cf_section_sub_find_name2(main_config.config, "server",
2869 cf_log_err_cs(cs, "No such server \"%s\"", this->server);
2874 cf_log_info(cs, "}");
2879 #ifdef HAVE_PTHREAD_H
2881 * A child thread which does NOTHING other than read and process
2884 static void *recv_thread(void *arg)
2886 rad_listen_t *this = arg;
2899 * Generate a list of listeners. Takes an input list of
2900 * listeners, too, so we don't close sockets with waiting packets.
2902 int listen_init(CONF_SECTION *config, rad_listen_t **head,
2906 UNUSED bool spawn_flag
2911 bool override = false;
2912 CONF_SECTION *cs = NULL;
2913 rad_listen_t **last;
2915 fr_ipaddr_t server_ipaddr;
2916 uint16_t auth_port = 0;
2918 bool defined_proxy = false;
2922 * We shouldn't be called with a pre-existing list.
2924 rad_assert(head && (*head == NULL));
2926 memset(&server_ipaddr, 0, sizeof(server_ipaddr));
2929 server_ipaddr.af = AF_UNSPEC;
2932 * If the port is specified on the command-line,
2933 * it over-rides the configuration file.
2935 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
2937 if (main_config.port > 0) {
2938 auth_port = main_config.port;
2941 * -p X but no -i Y on the command-line.
2943 if (main_config.myip.af == AF_UNSPEC) {
2944 ERROR("The command-line says \"-p %d\", but there is no associated IP address to use",
2951 * If the IP address was configured on the command-line,
2952 * use that as the "bind_address"
2954 if (main_config.myip.af != AF_UNSPEC) {
2955 listen_socket_t *sock;
2957 memcpy(&server_ipaddr, &main_config.myip,
2958 sizeof(server_ipaddr));
2962 if (strcmp(progname, "vmpsd") == 0) {
2963 this = listen_alloc(config, RAD_LISTEN_VQP);
2964 if (!auth_port) auth_port = 1589;
2967 this = listen_alloc(config, RAD_LISTEN_AUTH);
2971 sock->my_ipaddr = server_ipaddr;
2972 sock->my_port = auth_port;
2974 sock->clients = clients_parse_section(config, false);
2975 if (!sock->clients) {
2976 cf_log_err_cs(config,
2977 "Failed to find any clients for this listen section");
2982 if (listen_bind(this) < 0) {
2984 ERROR("There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
2988 auth_port = sock->my_port; /* may have been updated in listen_bind */
2990 cs = cf_section_sub_find_name2(config, "server",
2992 if (cs) this->server = main_config.name;
2996 last = &(this->next);
3002 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
3005 #ifdef WITH_ACCOUNTING
3007 * Open Accounting Socket.
3009 * If we haven't already gotten acct_port from
3010 * /etc/services, then make it auth_port + 1.
3012 this = listen_alloc(config, RAD_LISTEN_ACCT);
3016 * Create the accounting socket.
3018 * The accounting port is always the
3019 * authentication port + 1
3021 sock->my_ipaddr = server_ipaddr;
3022 sock->my_port = auth_port + 1;
3024 sock->clients = clients_parse_section(config, false);
3025 if (!sock->clients) {
3026 cf_log_err_cs(config,
3027 "Failed to find any clients for this listen section");
3031 if (listen_bind(this) < 0) {
3034 ERROR("There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
3039 cs = cf_section_sub_find_name2(config, "server",
3041 if (cs) this->server = main_config.name;
3045 last = &(this->next);
3050 * They specified an IP on the command-line, ignore
3051 * all listen sections except the one in '-n'.
3053 if (main_config.myip.af != AF_UNSPEC) {
3054 CONF_SECTION *subcs;
3055 char const *name2 = cf_section_name2(cs);
3057 cs = cf_section_sub_find_name2(config, "server",
3059 if (!cs) goto add_sockets;
3062 * Should really abstract this code...
3064 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
3066 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
3067 this = listen_parse(subcs, name2);
3074 last = &(this->next);
3075 } /* loop over "listen" directives in server <foo> */
3081 * Walk through the "listen" sections, if they exist.
3083 for (cs = cf_subsection_find_next(config, NULL, "listen");
3085 cs = cf_subsection_find_next(config, cs, "listen")) {
3086 this = listen_parse(cs, NULL);
3093 last = &(this->next);
3097 * Check virtual servers for "listen" sections, too.
3099 * FIXME: Move to virtual server init?
3101 for (cs = cf_subsection_find_next(config, NULL, "server");
3103 cs = cf_subsection_find_next(config, cs, "server")) {
3104 CONF_SECTION *subcs;
3105 char const *name2 = cf_section_name2(cs);
3107 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
3109 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
3110 this = listen_parse(subcs, name2);
3117 last = &(this->next);
3118 } /* loop over "listen" directives in virtual servers */
3119 } /* loop over virtual servers */
3123 * No sockets to receive packets, this is an error.
3124 * proxying is pointless.
3127 ERROR("The server is not configured to listen on any ports. Cannot start");
3132 * Print out which sockets we're listening on, and
3133 * add them to the event list.
3135 for (this = *head; this != NULL; this = this->next) {
3137 if (this->type == RAD_LISTEN_PROXY) {
3138 defined_proxy = true;
3144 if (!check_config && !spawn_flag && this->tls) {
3145 cf_log_err_cs(this->cs, "Threading must be enabled for TLS sockets to function properly");
3146 cf_log_err_cs(this->cs, "You probably need to do '%s -fxx -l stdout' for debugging",
3151 if (!check_config) {
3152 if (this->workers && !spawn_flag) {
3153 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
3157 if (this->workers) {
3158 #ifdef HAVE_PTHREAD_H
3163 this->print(this, buffer, sizeof(buffer));
3165 for (i = 0; i < this->workers; i++) {
3169 * FIXME: create detached?
3171 rcode = pthread_create(&id, 0, recv_thread, this);
3173 ERROR("Thread create failed: %s",
3174 fr_syserror(rcode));
3178 DEBUG("Thread %d for %s\n", i, buffer);
3181 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
3186 radius_update_listener(this);
3193 * If we're proxying requests, open the proxy FD.
3194 * Otherwise, don't do anything.
3197 if ((main_config.proxy_requests == true) &&
3199 (*head != NULL) && !defined_proxy) {
3203 memset(&home, 0, sizeof(home));
3208 home.proto = IPPROTO_UDP;
3209 home.src_ipaddr = server_ipaddr;
3213 * Address is still unspecified, use IPv4.
3215 if (home.src_ipaddr.af == AF_UNSPEC) {
3216 home.src_ipaddr.af = AF_INET;
3217 /* everything else is already set to zero */
3220 home.ipaddr.af = home.src_ipaddr.af;
3221 /* everything else is already set to zero */
3223 this = proxy_new_listener(&home, port);
3229 radius_update_listener(this);
3234 * Haven't defined any sockets. Die.
3236 if (!*head) return -1;
3238 xlat_register("listen", xlat_listen, NULL, NULL);
3244 * Free a linked list of listeners;
3246 void listen_free(rad_listen_t **head)
3250 if (!head || !*head) return;
3254 rad_listen_t *next = this->next;
3263 RADCLIENT_LIST *listener_find_client_list(fr_ipaddr_t const *ipaddr, uint16_t port)
3267 for (this = main_config.listen; this != NULL; this = this->next) {
3268 listen_socket_t *sock;
3270 if ((this->type != RAD_LISTEN_AUTH)
3271 #ifdef WITH_ACCOUNTING
3272 && (this->type != RAD_LISTEN_ACCT)
3278 if ((sock->my_port == port) &&
3279 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
3280 return sock->clients;
3288 rad_listen_t *listener_find_byipaddr(fr_ipaddr_t const *ipaddr, uint16_t port, int proto)
3292 for (this = main_config.listen; this != NULL; this = this->next) {
3293 listen_socket_t *sock;
3297 if (sock->my_port != port) continue;
3298 if (sock->proto != proto) continue;
3299 if (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) != 0) continue;
3305 * Failed to find a specific one. Find INADDR_ANY
3307 for (this = main_config.listen; this != NULL; this = this->next) {
3308 listen_socket_t *sock;
3312 if (sock->my_port != port) continue;
3313 if (sock->proto != proto) continue;
3314 if (!fr_inaddr_any(&sock->my_ipaddr)) continue;