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 static void common_socket_free(UNUSED rad_listen_t *this)
734 * This function is stupid and complicated.
736 int common_socket_print(rad_listen_t const *this, char *buffer, size_t bufsize)
739 listen_socket_t *sock = this->data;
740 char const *name = master_listen[this->type].name;
742 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
743 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
751 if (sock->interface) {
752 ADDSTRING(" interface ");
753 ADDSTRING(sock->interface);
757 if (this->recv == dual_tcp_accept) {
758 ADDSTRING(" proto tcp");
764 * TCP sockets get printed a little differently, to make
765 * it clear what's going on.
768 ADDSTRING(" from client (");
769 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
773 snprintf(buffer, bufsize, "%d", sock->other_port);
777 if ((sock->my_ipaddr.af == AF_INET) &&
778 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
779 strlcpy(buffer, "*", bufsize);
781 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
786 snprintf(buffer, bufsize, "%d", sock->my_port);
790 ADDSTRING(", virtual-server=");
791 ADDSTRING(this->server);
801 * Maybe it's a socket that we opened to a home server.
803 if ((sock->proto == IPPROTO_TCP) &&
804 (this->type == RAD_LISTEN_PROXY)) {
806 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
810 snprintf(buffer, bufsize, "%d", sock->my_port);
812 ADDSTRING(") -> home_server (");
814 if ((sock->other_ipaddr.af == AF_INET) &&
815 (sock->other_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
816 strlcpy(buffer, "*", bufsize);
818 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
823 snprintf(buffer, bufsize, "%d", sock->other_port);
830 #endif /* WITH_PROXY */
831 #endif /* WITH_TCP */
833 ADDSTRING(" address ");
835 if ((sock->my_ipaddr.af == AF_INET) &&
836 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
837 strlcpy(buffer, "*", bufsize);
839 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
844 snprintf(buffer, bufsize, "%d", sock->my_port);
855 ADDSTRING(" as server ");
856 ADDSTRING(this->server);
865 extern bool check_config; /* radiusd.c */
867 static CONF_PARSER performance_config[] = {
868 { "skip_duplicate_checks", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rad_listen_t, nodup), NULL },
870 { "synchronous", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rad_listen_t, synchronous), NULL },
872 { "workers", FR_CONF_OFFSET(PW_TYPE_INTEGER, rad_listen_t, workers), NULL },
874 { NULL, -1, 0, NULL, NULL } /* end the list */
878 static CONF_PARSER limit_config[] = {
879 { "max_pps", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_socket_t, max_rate), NULL },
882 { "max_connections", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_socket_t, limit.max_connections), "16" },
883 { "lifetime", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_socket_t, limit.lifetime), "0" },
884 { "idle_timeout", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_socket_t, limit.idle_timeout), STRINGIFY(30) },
887 { NULL, -1, 0, NULL, NULL } /* end the list */
891 * Parse an authentication or accounting socket.
893 int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
896 uint16_t listen_port;
898 listen_socket_t *sock = this->data;
899 char const *section_name = NULL;
900 CONF_SECTION *client_cs, *parentcs;
908 memset(&ipaddr, 0, sizeof(ipaddr));
909 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
911 rcode = cf_item_parse(cs, "ipaddr", FR_ITEM_POINTER(PW_TYPE_IP_ADDR, &ipaddr), NULL);
912 if (rcode < 0) return -1;
913 if (rcode != 0) rcode = cf_item_parse(cs, "ipv4addr", FR_ITEM_POINTER(PW_TYPE_IPV4_ADDR, &ipaddr), NULL);
914 if (rcode < 0) return -1;
915 if (rcode != 0) rcode = cf_item_parse(cs, "ipv6addr", FR_ITEM_POINTER(PW_TYPE_IPV6_ADDR, &ipaddr), NULL);
916 if (rcode < 0) return -1;
918 cf_log_err_cs(cs, "No address specified in listen section");
922 rcode = cf_item_parse(cs, "port", FR_ITEM_POINTER(PW_TYPE_SHORT, &listen_port), "0");
923 if (rcode < 0) return -1;
925 sock->proto = IPPROTO_UDP;
927 if (cf_pair_find(cs, "proto")) {
930 "System does not support the TCP protocol. Delete this line from the configuration file");
933 char const *proto = NULL;
938 rcode = cf_item_parse(cs, "proto", FR_ITEM_POINTER(PW_TYPE_STRING, &proto), "udp");
939 if (rcode < 0) return -1;
941 if (strcmp(proto, "udp") == 0) {
942 sock->proto = IPPROTO_UDP;
944 } else if (strcmp(proto, "tcp") == 0) {
945 sock->proto = IPPROTO_TCP;
948 "Unknown proto name \"%s\"", proto);
953 * TCP requires a destination IP for sockets.
954 * UDP doesn't, so it's allowed.
957 if ((this->type == RAD_LISTEN_PROXY) &&
958 (sock->proto != IPPROTO_UDP)) {
960 "Proxy listeners can only listen on proto = udp");
963 #endif /* WITH_PROXY */
966 tls = cf_section_sub_find(cs, "tls");
970 * Don't allow TLS configurations for UDP sockets.
972 if (sock->proto != IPPROTO_TCP) {
974 "TLS transport is not available for UDP sockets");
979 * If unset, set to default.
981 if (listen_port == 0) listen_port = PW_RADIUS_TLS_PORT;
983 this->tls = tls_server_conf_parse(tls);
988 #ifdef HAVE_PTRHEAD_H
989 if (pthread_mutex_init(&sock->mutex, NULL) < 0) {
999 * Built without TLS. Disallow it.
1001 if (cf_section_sub_find(cs, "tls")) {
1003 "TLS transport is not available in this executable");
1006 #endif /* WITH_TLS */
1008 #endif /* WITH_TCP */
1011 * No "proto" field. Disallow TLS.
1013 } else if (cf_section_sub_find(cs, "tls")) {
1015 "TLS transport is not available in this \"listen\" section");
1020 * Magical tuning methods!
1022 subcs = cf_section_sub_find(cs, "performance");
1024 rcode = cf_section_parse(subcs, this,
1025 performance_config);
1026 if (rcode < 0) return -1;
1028 if (this->synchronous && sock->max_rate) {
1029 WARN("Setting 'max_pps' is incompatible with 'synchronous'. Disabling 'max_pps'");
1033 if (!this->synchronous && this->workers) {
1034 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
1039 subcs = cf_section_sub_find(cs, "limit");
1041 rcode = cf_section_parse(subcs, sock,
1043 if (rcode < 0) return -1;
1045 if (sock->max_rate && ((sock->max_rate < 10) || (sock->max_rate > 1000000))) {
1047 "Invalid value for \"max_pps\"");
1052 if ((sock->limit.idle_timeout > 0) && (sock->limit.idle_timeout < 5)) {
1053 WARN("Setting idle_timeout to 5");
1054 sock->limit.idle_timeout = 5;
1057 if ((sock->limit.lifetime > 0) && (sock->limit.lifetime < 5)) {
1058 WARN("Setting lifetime to 5");
1059 sock->limit.lifetime = 5;
1062 if ((sock->limit.lifetime > 0) && (sock->limit.idle_timeout > sock->limit.lifetime)) {
1063 WARN("Setting idle_timeout to 0");
1064 sock->limit.idle_timeout = 0;
1068 * Force no duplicate detection for TCP sockets.
1070 if (sock->proto == IPPROTO_TCP) {
1075 sock->limit.max_connections = 60;
1076 sock->limit.idle_timeout = 30;
1077 sock->limit.lifetime = 0;
1081 sock->my_ipaddr = ipaddr;
1082 sock->my_port = listen_port;
1087 * Until there is a side effects free way of forwarding a
1088 * request to another virtual server, this check is invalid,
1089 * and should be left disabled.
1092 if (home_server_find(&sock->my_ipaddr, sock->my_port, sock->proto)) {
1095 ERROR("We have been asked to listen on %s port %d, which is also listed as a "
1096 "home server. This can create a proxy loop",
1097 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)), sock->my_port);
1101 return 0; /* don't do anything */
1106 * If we can bind to interfaces, do so,
1109 if (cf_pair_find(cs, "interface")) {
1111 CONF_PAIR *cp = cf_pair_find(cs, "interface");
1113 rad_assert(cp != NULL);
1114 value = cf_pair_value(cp);
1117 "No interface name given");
1120 sock->interface = value;
1125 * If we can do broadcasts..
1127 if (cf_pair_find(cs, "broadcast")) {
1128 #ifndef SO_BROADCAST
1130 "System does not support broadcast sockets. Delete this line from the configuration file");
1134 CONF_PAIR *cp = cf_pair_find(cs, "broadcast");
1136 if (this->type != RAD_LISTEN_DHCP) {
1138 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file");
1142 rad_assert(cp != NULL);
1143 value = cf_pair_value(cp);
1146 "No broadcast value given");
1151 * Hack... whatever happened to cf_section_parse?
1153 sock->broadcast = (strcmp(value, "yes") == 0);
1159 * And bind it to the port.
1161 if (listen_bind(this) < 0) {
1164 "Error binding to port for %s port %d",
1165 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
1172 * Proxy sockets don't have clients.
1174 if (this->type == RAD_LISTEN_PROXY) return 0;
1178 * The more specific configurations are preferred to more
1182 parentcs = cf_top_section(cs);
1183 rcode = cf_item_parse(cs, "clients", FR_ITEM_POINTER(PW_TYPE_STRING, §ion_name), NULL);
1184 if (rcode < 0) return -1; /* bad string */
1187 * Explicit list given: use it.
1189 client_cs = cf_section_sub_find_name2(parentcs, "clients", section_name);
1191 client_cs = cf_section_find(section_name);
1195 "Failed to find clients %s {...}",
1199 } /* else there was no "clients = " entry. */
1202 CONF_SECTION *server_cs;
1204 server_cs = cf_section_sub_find_name2(parentcs,
1208 * Found a "server foo" section. If there are clients
1212 (cf_section_sub_find(server_cs, "client") != NULL)) {
1213 client_cs = server_cs;
1218 * Still nothing. Look for global clients.
1220 if (!client_cs) client_cs = parentcs;
1223 sock->clients = clients_parse_section(client_cs, (this->tls != NULL));
1225 sock->clients = clients_parse_section(client_cs, false);
1227 if (!sock->clients) {
1229 "Failed to load clients for this listen section");
1234 if (sock->proto == IPPROTO_TCP) {
1236 * Re-write the listener receive function to
1237 * allow us to accept the socket.
1239 this->recv = dual_tcp_accept;
1247 * Send an authentication response packet
1249 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1251 rad_assert(request->listener == listener);
1252 rad_assert(listener->send == auth_socket_send);
1254 if (request->reply->code == 0) return 0;
1256 #ifdef WITH_UDPFROMTO
1258 * Overwrite the src ip address on the outbound packet
1259 * with the one specified by the client.
1260 * This is useful to work around broken DSR implementations
1261 * and other routing issues.
1263 if (request->client->src_ipaddr.af != AF_UNSPEC) {
1264 request->reply->src_ipaddr = request->client->src_ipaddr;
1268 if (rad_send(request->reply, request->packet,
1269 request->client->secret) < 0) {
1270 RERROR("Failed sending reply: %s",
1279 #ifdef WITH_ACCOUNTING
1281 * Send an accounting response packet (or not)
1283 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1285 rad_assert(request->listener == listener);
1286 rad_assert(listener->send == acct_socket_send);
1289 * Accounting reject's are silently dropped.
1291 * We do it here to avoid polluting the rest of the
1292 * code with this knowledge
1294 if (request->reply->code == 0) return 0;
1296 #ifdef WITH_UDPFROMTO
1298 * Overwrite the src ip address on the outbound packet
1299 * with the one specified by the client.
1300 * This is useful to work around broken DSR implementations
1301 * and other routing issues.
1303 if (request->client->src_ipaddr.af != AF_UNSPEC) {
1304 request->reply->src_ipaddr = request->client->src_ipaddr;
1308 if (rad_send(request->reply, request->packet,
1309 request->client->secret) < 0) {
1310 RERROR("Failed sending reply: %s",
1321 * Send a packet to a home server.
1323 * FIXME: have different code for proxy auth & acct!
1325 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1327 rad_assert(request->proxy_listener == listener);
1328 rad_assert(listener->send == proxy_socket_send);
1330 if (rad_send(request->proxy, NULL,
1331 request->home_server->secret) < 0) {
1332 RERROR("Failed sending proxied request: %s",
1343 * Check if an incoming request is "ok"
1345 * It takes packets, not requests. It sees if the packet looks
1346 * OK. If so, it does a number of sanity checks on it.
1348 static int stats_socket_recv(rad_listen_t *listener)
1353 RADIUS_PACKET *packet;
1354 RADCLIENT *client = NULL;
1355 fr_ipaddr_t src_ipaddr;
1357 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1358 if (rcode < 0) return 0;
1360 FR_STATS_INC(auth, total_requests);
1362 if (rcode < 20) { /* AUTH_HDR_LEN */
1363 FR_STATS_INC(auth, total_malformed_requests);
1367 if ((client = client_listener_find(listener,
1368 &src_ipaddr, src_port)) == NULL) {
1369 rad_recv_discard(listener->fd);
1370 FR_STATS_INC(auth, total_invalid_requests);
1374 FR_STATS_TYPE_INC(client->auth.total_requests);
1377 * We only understand Status-Server on this socket.
1379 if (code != PW_CODE_STATUS_SERVER) {
1380 DEBUG("Ignoring packet code %d sent to Status-Server port",
1382 rad_recv_discard(listener->fd);
1383 FR_STATS_INC(auth, total_unknown_types);
1388 * Now that we've sanity checked everything, receive the
1391 packet = rad_recv(listener->fd, 1); /* require message authenticator */
1393 FR_STATS_INC(auth, total_malformed_requests);
1394 DEBUG("%s", fr_strerror());
1398 if (!request_receive(listener, packet, client, rad_status_server)) {
1399 FR_STATS_INC(auth, total_packets_dropped);
1410 * Check if an incoming request is "ok"
1412 * It takes packets, not requests. It sees if the packet looks
1413 * OK. If so, it does a number of sanity checks on it.
1415 static int auth_socket_recv(rad_listen_t *listener)
1420 RADIUS_PACKET *packet;
1421 RAD_REQUEST_FUNP fun = NULL;
1422 RADCLIENT *client = NULL;
1423 fr_ipaddr_t src_ipaddr;
1425 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1426 if (rcode < 0) return 0;
1428 FR_STATS_INC(auth, total_requests);
1430 if (rcode < 20) { /* AUTH_HDR_LEN */
1431 FR_STATS_INC(auth, total_malformed_requests);
1435 if ((client = client_listener_find(listener,
1436 &src_ipaddr, src_port)) == NULL) {
1437 rad_recv_discard(listener->fd);
1438 FR_STATS_INC(auth, total_invalid_requests);
1442 FR_STATS_TYPE_INC(client->auth.total_requests);
1445 * Some sanity checks, based on the packet code.
1448 case PW_CODE_ACCESS_REQUEST:
1449 fun = rad_authenticate;
1452 case PW_CODE_STATUS_SERVER:
1453 if (!main_config.status_server) {
1454 rad_recv_discard(listener->fd);
1455 FR_STATS_INC(auth, total_unknown_types);
1456 WARN("Ignoring Status-Server request due to security configuration");
1459 fun = rad_status_server;
1463 rad_recv_discard(listener->fd);
1464 FR_STATS_INC(auth,total_unknown_types);
1466 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
1467 code, client->shortname, src_port);
1470 } /* switch over packet types */
1473 * Now that we've sanity checked everything, receive the
1476 packet = rad_recv(listener->fd, client->message_authenticator);
1478 FR_STATS_INC(auth, total_malformed_requests);
1479 DEBUG("%s", fr_strerror());
1484 #ifdef WITH_UDPFROMTO
1486 * This is a NICE Mac OSX bug. Create an interface with
1487 * two IP address, and then configure one listener for
1488 * each IP address. Send thousands of packets to one
1489 * address, and some will show up on the OTHER socket.
1491 * This hack works ONLY if the clients are global. If
1492 * each listener has the same client IP, but with
1493 * different secrets, then it will fail the rad_recv()
1494 * check above, and there's nothing you can do.
1497 listen_socket_t *sock = listener->data;
1498 rad_listen_t *other;
1500 other = listener_find_byipaddr(&packet->dst_ipaddr,
1501 packet->dst_port, sock->proto);
1502 if (other) listener = other;
1508 if (!request_receive(listener, packet, client, fun)) {
1509 FR_STATS_INC(auth, total_packets_dropped);
1518 #ifdef WITH_ACCOUNTING
1520 * Receive packets from an accounting socket
1522 static int acct_socket_recv(rad_listen_t *listener)
1527 RADIUS_PACKET *packet;
1528 RAD_REQUEST_FUNP fun = NULL;
1529 RADCLIENT *client = NULL;
1530 fr_ipaddr_t src_ipaddr;
1532 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1533 if (rcode < 0) return 0;
1535 FR_STATS_INC(acct, total_requests);
1537 if (rcode < 20) { /* AUTH_HDR_LEN */
1538 FR_STATS_INC(acct, total_malformed_requests);
1542 if ((client = client_listener_find(listener,
1543 &src_ipaddr, src_port)) == NULL) {
1544 rad_recv_discard(listener->fd);
1545 FR_STATS_INC(acct, total_invalid_requests);
1549 FR_STATS_TYPE_INC(client->acct.total_requests);
1552 * Some sanity checks, based on the packet code.
1555 case PW_CODE_ACCOUNTING_REQUEST:
1556 fun = rad_accounting;
1559 case PW_CODE_STATUS_SERVER:
1560 if (!main_config.status_server) {
1561 rad_recv_discard(listener->fd);
1562 FR_STATS_INC(acct, total_unknown_types);
1564 WARN("Ignoring Status-Server request due to security configuration");
1567 fun = rad_status_server;
1571 rad_recv_discard(listener->fd);
1572 FR_STATS_INC(acct, total_unknown_types);
1574 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1575 code, client->shortname, src_port);
1577 } /* switch over packet types */
1580 * Now that we've sanity checked everything, receive the
1583 packet = rad_recv(listener->fd, 0);
1585 FR_STATS_INC(acct, total_malformed_requests);
1586 ERROR("%s", fr_strerror());
1591 * There can be no duplicate accounting packets.
1593 if (!request_receive(listener, packet, client, fun)) {
1594 FR_STATS_INC(acct, total_packets_dropped);
1605 static int do_proxy(REQUEST *request)
1609 if (request->in_proxy_hash ||
1610 (request->proxy_reply && (request->proxy_reply->code != 0))) {
1614 vp = pairfind(request->config_items, PW_HOME_SERVER_POOL, 0, TAG_ANY);
1617 if (!home_pool_byname(vp->vp_strvalue, HOME_TYPE_COA)) {
1618 REDEBUG2("Cannot proxy to unknown pool %s",
1627 * Receive a CoA packet.
1629 static int rad_coa_recv(REQUEST *request)
1631 int rcode = RLM_MODULE_OK;
1636 * Get the correct response
1638 switch (request->packet->code) {
1639 case PW_CODE_COA_REQUEST:
1640 ack = PW_CODE_COA_ACK;
1641 nak = PW_CODE_COA_NAK;
1644 case PW_CODE_DISCONNECT_REQUEST:
1645 ack = PW_CODE_DISCONNECT_ACK;
1646 nak = PW_CODE_DISCONNECT_NAK;
1649 default: /* shouldn't happen */
1650 return RLM_MODULE_FAIL;
1654 #define WAS_PROXIED (request->proxy)
1656 #define WAS_PROXIED (0)
1661 * RFC 5176 Section 3.3. If we have a CoA-Request
1662 * with Service-Type = Authorize-Only, it MUST
1663 * have a State attribute in it.
1665 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE, 0, TAG_ANY);
1666 if (request->packet->code == PW_CODE_COA_REQUEST) {
1667 if (vp && (vp->vp_integer == 17)) {
1668 vp = pairfind(request->packet->vps, PW_STATE, 0, TAG_ANY);
1669 if (!vp || (vp->length == 0)) {
1670 REDEBUG("CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1671 request->reply->code = PW_CODE_COA_NAK;
1672 return RLM_MODULE_FAIL;
1677 * RFC 5176, Section 3.2.
1679 REDEBUG("Disconnect-Request MUST NOT contain a Service-Type attribute");
1680 request->reply->code = PW_CODE_DISCONNECT_NAK;
1681 return RLM_MODULE_FAIL;
1684 rcode = process_recv_coa(0, request);
1686 case RLM_MODULE_FAIL:
1687 case RLM_MODULE_INVALID:
1688 case RLM_MODULE_REJECT:
1689 case RLM_MODULE_USERLOCK:
1691 request->reply->code = nak;
1694 case RLM_MODULE_HANDLED:
1697 case RLM_MODULE_NOOP:
1698 case RLM_MODULE_NOTFOUND:
1700 case RLM_MODULE_UPDATED:
1701 if (do_proxy(request)) return RLM_MODULE_OK;
1702 request->reply->code = ack;
1705 } else if (request->proxy_reply) {
1707 * Start the reply code with the proxy reply
1710 request->reply->code = request->proxy_reply->code;
1714 * Copy State from the request to the reply.
1715 * See RFC 5176 Section 3.3.
1717 vp = paircopy2(request->reply, request->packet->vps, PW_STATE, 0, TAG_ANY);
1718 if (vp) pairadd(&request->reply->vps, vp);
1721 * We may want to over-ride the reply.
1723 if (request->reply->code) {
1724 rcode = process_send_coa(0, request);
1727 * We need to send CoA-NAK back if Service-Type
1728 * is Authorize-Only. Rely on the user's policy
1729 * to do that. We're not a real NAS, so this
1730 * restriction doesn't (ahem) apply to us.
1732 case RLM_MODULE_FAIL:
1733 case RLM_MODULE_INVALID:
1734 case RLM_MODULE_REJECT:
1735 case RLM_MODULE_USERLOCK:
1738 * Over-ride an ACK with a NAK
1740 request->reply->code = nak;
1743 case RLM_MODULE_HANDLED:
1746 case RLM_MODULE_NOOP:
1747 case RLM_MODULE_NOTFOUND:
1749 case RLM_MODULE_UPDATED:
1751 * Do NOT over-ride a previously set value.
1752 * Otherwise an "ok" here will re-write a
1755 if (request->reply->code == 0) {
1756 request->reply->code = ack;
1762 return RLM_MODULE_OK;
1767 * Check if an incoming request is "ok"
1769 * It takes packets, not requests. It sees if the packet looks
1770 * OK. If so, it does a number of sanity checks on it.
1772 static int coa_socket_recv(rad_listen_t *listener)
1777 RADIUS_PACKET *packet;
1778 RAD_REQUEST_FUNP fun = NULL;
1779 RADCLIENT *client = NULL;
1780 fr_ipaddr_t src_ipaddr;
1782 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1783 if (rcode < 0) return 0;
1785 if (rcode < 20) { /* AUTH_HDR_LEN */
1786 FR_STATS_INC(coa, total_requests);
1787 FR_STATS_INC(coa, total_malformed_requests);
1791 if ((client = client_listener_find(listener,
1792 &src_ipaddr, src_port)) == NULL) {
1793 rad_recv_discard(listener->fd);
1794 FR_STATS_INC(coa, total_requests);
1795 FR_STATS_INC(coa, total_invalid_requests);
1800 * Some sanity checks, based on the packet code.
1803 case PW_CODE_COA_REQUEST:
1804 FR_STATS_INC(coa, total_requests);
1808 case PW_CODE_DISCONNECT_REQUEST:
1809 FR_STATS_INC(dsc, total_requests);
1814 rad_recv_discard(listener->fd);
1815 FR_STATS_INC(coa, total_unknown_types);
1816 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1817 code, client->shortname, src_port);
1819 } /* switch over packet types */
1822 * Now that we've sanity checked everything, receive the
1825 packet = rad_recv(listener->fd, client->message_authenticator);
1827 FR_STATS_INC(coa, total_malformed_requests);
1828 DEBUG("%s", fr_strerror());
1832 if (!request_receive(listener, packet, client, fun)) {
1833 FR_STATS_INC(coa, total_packets_dropped);
1844 * Recieve packets from a proxy socket.
1846 static int proxy_socket_recv(rad_listen_t *listener)
1848 RADIUS_PACKET *packet;
1851 packet = rad_recv(listener->fd, 0);
1853 ERROR("%s", fr_strerror());
1858 * FIXME: Client MIB updates?
1860 switch(packet->code) {
1861 case PW_CODE_ACCESS_ACCEPT:
1862 case PW_CODE_ACCESS_CHALLENGE:
1863 case PW_CODE_ACCESS_REJECT:
1866 #ifdef WITH_ACCOUNTING
1867 case PW_CODE_ACCOUNTING_RESPONSE:
1872 case PW_CODE_DISCONNECT_ACK:
1873 case PW_CODE_DISCONNECT_NAK:
1874 case PW_CODE_COA_ACK:
1875 case PW_CODE_COA_NAK:
1881 * FIXME: Update MIB for packet types?
1883 ERROR("Invalid packet code %d sent to a proxy port "
1884 "from home server %s port %d - ID %d : IGNORED",
1886 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1887 packet->src_port, packet->id);
1892 if (!request_proxy_reply(packet)) {
1902 * Recieve packets from a proxy socket.
1904 static int proxy_socket_tcp_recv(rad_listen_t *listener)
1906 RADIUS_PACKET *packet;
1907 listen_socket_t *sock = listener->data;
1910 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return 0;
1912 packet = fr_tcp_recv(listener->fd, 0);
1914 listener->status = RAD_LISTEN_STATUS_EOL;
1915 radius_update_listener(listener);
1920 * FIXME: Client MIB updates?
1922 switch(packet->code) {
1923 case PW_CODE_ACCESS_ACCEPT:
1924 case PW_CODE_ACCESS_CHALLENGE:
1925 case PW_CODE_ACCESS_REJECT:
1928 #ifdef WITH_ACCOUNTING
1929 case PW_CODE_ACCOUNTING_RESPONSE:
1935 * FIXME: Update MIB for packet types?
1937 ERROR("Invalid packet code %d sent to a proxy port "
1938 "from home server %s port %d - ID %d : IGNORED",
1940 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1941 packet->src_port, packet->id);
1946 packet->src_ipaddr = sock->other_ipaddr;
1947 packet->src_port = sock->other_port;
1948 packet->dst_ipaddr = sock->my_ipaddr;
1949 packet->dst_port = sock->my_port;
1952 * FIXME: Have it return an indication of packets that
1953 * are OK to ignore (dups, too late), versus ones that
1954 * aren't OK to ignore (unknown response, spoofed, etc.)
1956 * Close the socket on bad packets...
1958 if (!request_proxy_reply(packet)) {
1963 sock->opened = sock->last_packet = time(NULL);
1971 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1973 if (!request->reply->code) return 0;
1975 if (rad_encode(request->reply, request->packet,
1976 request->client->secret) < 0) {
1977 RERROR("Failed encoding packet: %s",
1982 if (rad_sign(request->reply, request->packet,
1983 request->client->secret) < 0) {
1984 RERROR("Failed signing packet: %s",
1993 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1995 if (rad_verify(request->packet, NULL,
1996 request->client->secret) < 0) {
2000 return rad_decode(request->packet, NULL,
2001 request->client->secret);
2005 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
2007 if (rad_encode(request->proxy, NULL, request->home_server->secret) < 0) {
2008 RERROR("Failed encoding proxied packet: %s",
2013 if (rad_sign(request->proxy, NULL, request->home_server->secret) < 0) {
2014 RERROR("Failed signing proxied packet: %s",
2023 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
2026 * rad_verify is run in event.c, received_proxy_response()
2029 return rad_decode(request->proxy_reply, request->proxy,
2030 request->home_server->secret);
2034 #include "command.c"
2037 * Temporarily NOT const!
2039 static fr_protocol_t master_listen[RAD_LISTEN_MAX] = {
2041 { RLM_MODULE_INIT, "status", sizeof(listen_socket_t), NULL,
2042 common_socket_parse, NULL,
2043 stats_socket_recv, auth_socket_send,
2044 common_socket_print, client_socket_encode, client_socket_decode },
2047 * This always gets defined.
2049 { RLM_MODULE_INIT, "status", 0, NULL,
2050 NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
2055 { RLM_MODULE_INIT, "proxy", sizeof(listen_socket_t), NULL,
2056 common_socket_parse, NULL,
2057 proxy_socket_recv, proxy_socket_send,
2058 common_socket_print, proxy_socket_encode, proxy_socket_decode },
2061 /* authentication */
2062 { RLM_MODULE_INIT, "auth", sizeof(listen_socket_t), NULL,
2063 common_socket_parse, common_socket_free,
2064 auth_socket_recv, auth_socket_send,
2065 common_socket_print, client_socket_encode, client_socket_decode },
2067 #ifdef WITH_ACCOUNTING
2069 { RLM_MODULE_INIT, "acct", sizeof(listen_socket_t), NULL,
2070 common_socket_parse, common_socket_free,
2071 acct_socket_recv, acct_socket_send,
2072 common_socket_print, client_socket_encode, client_socket_decode},
2077 { RLM_MODULE_INIT, "detail", sizeof(listen_detail_t), NULL,
2078 detail_parse, detail_free,
2079 detail_recv, detail_send,
2080 detail_print, detail_encode, detail_decode },
2084 /* vlan query protocol */
2085 { 0, "vmps", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
2089 /* dhcp query protocol */
2090 { 0, "dhcp", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
2093 #ifdef WITH_COMMAND_SOCKET
2094 /* TCP command socket */
2095 { RLM_MODULE_INIT, "control", sizeof(fr_command_socket_t), NULL,
2096 command_socket_parse, command_socket_free,
2097 command_domain_accept, command_domain_send,
2098 command_socket_print, command_socket_encode, command_socket_decode },
2102 /* Change of Authorization */
2103 { RLM_MODULE_INIT, "coa", sizeof(listen_socket_t), NULL,
2104 common_socket_parse, NULL,
2105 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
2106 common_socket_print, client_socket_encode, client_socket_decode },
2114 * Binds a listener to a socket.
2116 static int listen_bind(rad_listen_t *this)
2119 struct sockaddr_storage salocal;
2121 listen_socket_t *sock = this->data;
2123 #define proto_for_port "udp"
2124 #define sock_type SOCK_DGRAM
2126 char const *proto_for_port = "udp";
2127 int sock_type = SOCK_DGRAM;
2129 if (sock->proto == IPPROTO_TCP) {
2131 if (this->type == RAD_LISTEN_VQP) {
2132 ERROR("VQP does not support TCP transport");
2137 proto_for_port = "tcp";
2138 sock_type = SOCK_STREAM;
2143 * If the port is zero, then it means the appropriate
2144 * thing from /etc/services.
2146 if (sock->my_port == 0) {
2147 struct servent *svp;
2149 switch (this->type) {
2150 case RAD_LISTEN_AUTH:
2151 svp = getservbyname ("radius", proto_for_port);
2153 sock->my_port = ntohs(svp->s_port);
2155 sock->my_port = PW_AUTH_UDP_PORT;
2159 #ifdef WITH_ACCOUNTING
2160 case RAD_LISTEN_ACCT:
2161 svp = getservbyname ("radacct", proto_for_port);
2163 sock->my_port = ntohs(svp->s_port);
2165 sock->my_port = PW_ACCT_UDP_PORT;
2171 case RAD_LISTEN_PROXY:
2172 /* leave it at zero */
2177 case RAD_LISTEN_VQP:
2178 sock->my_port = 1589;
2182 #ifdef WITH_COMMAND_SOCKET
2183 case RAD_LISTEN_COMMAND:
2184 sock->my_port = PW_RADMIN_PORT;
2189 case RAD_LISTEN_COA:
2190 svp = getservbyname ("radius-dynauth", "udp");
2192 sock->my_port = ntohs(svp->s_port);
2194 sock->my_port = PW_COA_UDP_PORT;
2200 WARN("Internal sanity check failed in binding to socket. Ignoring problem");
2206 * Don't open sockets if we're checking the config.
2214 * Copy fr_socket() here, as we may need to bind to a device.
2216 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
2220 this->print(this, buffer, sizeof(buffer));
2222 ERROR("Failed opening %s: %s", buffer, fr_syserror(errno));
2228 * We don't want child processes inheriting these
2231 rcode = fcntl(this->fd, F_GETFD);
2233 if (fcntl(this->fd, F_SETFD, rcode | FD_CLOEXEC) < 0) {
2235 ERROR("Failed setting close on exec: %s", fr_syserror(errno));
2242 * Bind to a device BEFORE touching IP addresses.
2244 if (sock->interface) {
2245 #ifdef SO_BINDTODEVICE
2248 memset(&ifreq, 0, sizeof(ifreq));
2249 strlcpy(ifreq.ifr_name, sock->interface, sizeof(ifreq.ifr_name));
2252 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
2253 (char *)&ifreq, sizeof(ifreq));
2257 ERROR("Failed binding to interface %s: %s",
2258 sock->interface, fr_syserror(errno));
2260 } /* else it worked. */
2262 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2263 #ifdef HAVE_NET_IF_H
2265 * Odds are that any system supporting "bind to
2266 * device" also supports IPv6, so this next bit
2267 * isn't necessary. But it's here for
2270 * If we're doing IPv6, and the scope hasn't yet
2271 * been defined, set the scope to the scope of
2274 if (sock->my_ipaddr.af == AF_INET6) {
2275 if (sock->my_ipaddr.scope == 0) {
2276 sock->my_ipaddr.scope = if_nametoindex(sock->interface);
2277 if (sock->my_ipaddr.scope == 0) {
2279 ERROR("Failed finding interface %s: %s",
2280 sock->interface, fr_syserror(errno));
2283 } /* else scope was defined: we're OK. */
2288 * IPv4: no link local addresses,
2289 * and no bind to device.
2293 ERROR("Failed binding to interface %s: \"bind to device\" is unsupported", sock->interface);
2300 if (sock->proto == IPPROTO_TCP) {
2303 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2305 ERROR("Failed to reuse address: %s", fr_syserror(errno));
2311 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
2312 else /* UDP sockets get UDPfromto */
2315 #ifdef WITH_UDPFROMTO
2317 * Initialize udpfromto for all sockets.
2319 if (udpfromto_init(this->fd) != 0) {
2320 ERROR("Failed initializing udpfromto: %s",
2321 fr_syserror(errno));
2328 * Set up sockaddr stuff.
2330 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
2335 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2336 if (sock->my_ipaddr.af == AF_INET6) {
2338 * Listening on '::' does NOT get you IPv4 to
2339 * IPv6 mapping. You've got to listen on an IPv4
2340 * address, too. This makes the rest of the server
2341 * design a little simpler.
2345 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2348 if (setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2349 (char *)&on, sizeof(on)) < 0) {
2350 ERROR("Failed setting socket to IPv6 "
2351 "only: %s", fr_syserror(errno));
2357 #endif /* IPV6_V6ONLY */
2359 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2361 if (sock->my_ipaddr.af == AF_INET) {
2364 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2366 * Disable PMTU discovery. On Linux, this
2367 * also makes sure that the "don't fragment"
2370 flag = IP_PMTUDISC_DONT;
2371 if (setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2372 &flag, sizeof(flag)) < 0) {
2373 ERROR("Failed disabling PMTU discovery: %s",
2374 fr_syserror(errno));
2381 #if defined(IP_DONTFRAG)
2383 * Ensure that the "don't fragment" flag is zero.
2386 if (setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2387 &flag, sizeof(flag)) < 0) {
2388 ERROR("Failed setting don't fragment flag: %s",
2389 fr_syserror(errno));
2399 if (sock->broadcast) {
2402 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2403 ERROR("Can't set broadcast option: %s",
2404 fr_syserror(errno));
2412 * May be binding to priviledged ports.
2414 if (sock->my_port != 0) {
2416 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2422 this->print(this, buffer, sizeof(buffer));
2423 ERROR("Failed binding to %s: %s\n",
2424 buffer, fr_syserror(errno));
2429 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2430 * kernel instead binds us to a 1.2.3.4. If this
2431 * happens, notice, and remember our real IP.
2434 struct sockaddr_storage src;
2435 socklen_t sizeof_src = sizeof(src);
2437 memset(&src, 0, sizeof_src);
2438 if (getsockname(this->fd, (struct sockaddr *) &src,
2440 ERROR("Failed getting socket name: %s",
2441 fr_syserror(errno));
2445 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2446 &sock->my_ipaddr, &sock->my_port)) {
2447 ERROR("Socket has unsupported address family");
2454 if (sock->proto == IPPROTO_TCP) {
2456 * If there are hard-coded worker threads, OR
2457 * it's a TLS connection, it's blocking.
2459 * Otherwise, they're non-blocking.
2464 && (this->type == RAD_LISTEN_PROXY) && !this->tls
2468 if (fr_nonblock(this->fd) < 0) {
2470 ERROR("Failed setting non-blocking on socket: %s",
2471 fr_syserror(errno));
2477 * Allow a backlog of 8 listeners, but only for incoming interfaces.
2480 if (this->type != RAD_LISTEN_PROXY)
2482 if (listen(this->fd, 8) < 0) {
2484 ERROR("Failed in listen(): %s", fr_syserror(errno));
2491 * Mostly for proxy sockets.
2493 sock->other_ipaddr.af = sock->my_ipaddr.af;
2496 * Don't screw up other people.
2498 #undef proto_for_port
2505 static int _listener_free(rad_listen_t *this)
2508 * Other code may have eaten the FD.
2510 if (this->fd >= 0) close(this->fd);
2512 if (master_listen[this->type].free) {
2513 master_listen[this->type].free(this);
2517 if ((this->type == RAD_LISTEN_AUTH)
2519 || (this->type == RAD_LISTEN_ACCT)
2522 || (this->type == RAD_LISTEN_PROXY)
2524 #ifdef WITH_COMMAND_SOCKET
2525 || ((this->type == RAD_LISTEN_COMMAND) &&
2526 (((fr_command_socket_t *) this->data)->magic != COMMAND_SOCKET_MAGIC))
2529 listen_socket_t *sock = this->data;
2531 rad_free(&sock->packet);
2533 rad_assert(sock->ev == NULL);
2537 * Note that we do NOT free this->tls, as the
2538 * pointer is parented by its CONF_SECTION. It
2539 * may be used by multiple listeners.
2542 TALLOC_FREE(sock->ssn);
2543 TALLOC_FREE(sock->request);
2544 #ifdef HAVE_PTHREAD_H
2545 pthread_mutex_destroy(&(sock->mutex));
2548 #endif /* WITH_TLS */
2550 #endif /* WITH_TCP */
2556 * Allocate & initialize a new listener.
2558 static rad_listen_t *listen_alloc(TALLOC_CTX *ctx, RAD_LISTEN_TYPE type)
2562 this = talloc_zero(ctx, rad_listen_t);
2565 this->recv = master_listen[this->type].recv;
2566 this->send = master_listen[this->type].send;
2567 this->print = master_listen[this->type].print;
2568 this->encode = master_listen[this->type].encode;
2569 this->decode = master_listen[this->type].decode;
2571 talloc_set_destructor(this, _listener_free);
2573 this->data = talloc_zero_array(this, uint8_t, master_listen[this->type].inst_size);
2580 * Externally visible function for creating a new proxy LISTENER.
2582 * Not thread-safe, but all calls to it are protected by the
2583 * proxy mutex in event.c
2585 rad_listen_t *proxy_new_listener(home_server_t *home, uint16_t src_port)
2589 listen_socket_t *sock;
2592 if (!home) return NULL;
2594 rad_assert(home->server == NULL); /* we only open real sockets */
2596 if ((home->limit.max_connections > 0) &&
2597 (home->limit.num_connections >= home->limit.max_connections)) {
2598 RATE_LIMIT(INFO("Home server %s has too many open connections (%d)",
2599 home->name, home->limit.max_connections));
2604 if (home->last_failed_open == now) {
2605 WARN("Suppressing attempt to open socket to 'down' home server");
2609 this = listen_alloc(main_config.config, RAD_LISTEN_PROXY);
2612 sock->other_ipaddr = home->ipaddr;
2613 sock->other_port = home->port;
2616 sock->my_ipaddr = home->src_ipaddr;
2617 sock->my_port = src_port;
2618 sock->proto = home->proto;
2621 * For error messages.
2623 this->print(this, buffer, sizeof(buffer));
2625 if (debug_flag >= 2) {
2626 DEBUG("Opening new proxy socket '%s'", buffer);
2630 sock->opened = sock->last_packet = now;
2632 if (home->proto == IPPROTO_TCP) {
2633 this->recv = proxy_socket_tcp_recv;
2636 * FIXME: connect() is blocking!
2637 * We do this with the proxy mutex locked, which may
2638 * cause large delays!
2640 * http://www.developerweb.net/forum/showthread.php?p=13486
2642 this->fd = fr_tcp_client_socket(&home->src_ipaddr,
2643 &home->ipaddr, home->port);
2646 this->fd = fr_socket(&home->src_ipaddr, src_port);
2649 this->print(this, buffer,sizeof(buffer));
2650 ERROR("Failed opening proxy socket '%s' : %s",
2651 buffer, fr_strerror());
2652 home->last_failed_open = now;
2660 if ((home->proto == IPPROTO_TCP) && home->tls) {
2661 DEBUG("Trying SSL to port %d\n", home->port);
2662 sock->ssn = tls_new_client_session(home->tls, this->fd);
2664 ERROR("Failed starting SSL to '%s'", buffer);
2665 home->last_failed_open = now;
2670 this->recv = proxy_tls_recv;
2671 this->send = proxy_tls_send;
2676 * Figure out which port we were bound to.
2678 if (sock->my_port == 0) {
2679 struct sockaddr_storage src;
2680 socklen_t sizeof_src = sizeof(src);
2682 memset(&src, 0, sizeof_src);
2683 if (getsockname(this->fd, (struct sockaddr *) &src,
2685 ERROR("Failed getting socket name for '%s': %s",
2686 buffer, fr_syserror(errno));
2687 home->last_failed_open = now;
2692 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2693 &sock->my_ipaddr, &sock->my_port)) {
2694 ERROR("Socket has unsupported address family for '%s'", buffer);
2695 home->last_failed_open = now;
2701 home->limit.num_connections++;
2707 static const FR_NAME_NUMBER listen_compare[] = {
2709 { "status", RAD_LISTEN_NONE },
2711 { "auth", RAD_LISTEN_AUTH },
2712 #ifdef WITH_ACCOUNTING
2713 { "acct", RAD_LISTEN_ACCT },
2714 { "auth+acct", RAD_LISTEN_AUTH },
2717 { "detail", RAD_LISTEN_DETAIL },
2720 { "proxy", RAD_LISTEN_PROXY },
2723 { "vmps", RAD_LISTEN_VQP },
2726 { "dhcp", RAD_LISTEN_DHCP },
2728 #ifdef WITH_COMMAND_SOCKET
2729 { "control", RAD_LISTEN_COMMAND },
2732 { "coa", RAD_LISTEN_COA },
2737 static int _free_proto_handle(lt_dlhandle *handle)
2743 static rad_listen_t *listen_parse(CONF_SECTION *cs, char const *server)
2746 char const *listen_type;
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) {
2865 cf_log_info(cs, "}");
2870 #ifdef HAVE_PTHREAD_H
2872 * A child thread which does NOTHING other than read and process
2875 static void *recv_thread(void *arg)
2877 rad_listen_t *this = arg;
2890 * Generate a list of listeners. Takes an input list of
2891 * listeners, too, so we don't close sockets with waiting packets.
2893 int listen_init(CONF_SECTION *config, rad_listen_t **head,
2897 UNUSED bool spawn_flag
2902 bool override = false;
2903 CONF_SECTION *cs = NULL;
2904 rad_listen_t **last;
2906 fr_ipaddr_t server_ipaddr;
2907 uint16_t auth_port = 0;
2909 bool defined_proxy = false;
2913 * We shouldn't be called with a pre-existing list.
2915 rad_assert(head && (*head == NULL));
2917 memset(&server_ipaddr, 0, sizeof(server_ipaddr));
2920 server_ipaddr.af = AF_UNSPEC;
2923 * If the port is specified on the command-line,
2924 * it over-rides the configuration file.
2926 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
2928 if (main_config.port > 0) {
2929 auth_port = main_config.port;
2932 * -p X but no -i Y on the command-line.
2934 if (main_config.myip.af == AF_UNSPEC) {
2935 ERROR("The command-line says \"-p %d\", but there is no associated IP address to use",
2942 * If the IP address was configured on the command-line,
2943 * use that as the "bind_address"
2945 if (main_config.myip.af != AF_UNSPEC) {
2946 listen_socket_t *sock;
2948 memcpy(&server_ipaddr, &main_config.myip,
2949 sizeof(server_ipaddr));
2953 if (strcmp(progname, "vmpsd") == 0) {
2954 this = listen_alloc(config, RAD_LISTEN_VQP);
2955 if (!auth_port) auth_port = 1589;
2958 this = listen_alloc(config, RAD_LISTEN_AUTH);
2962 sock->my_ipaddr = server_ipaddr;
2963 sock->my_port = auth_port;
2965 sock->clients = clients_parse_section(config, false);
2966 if (!sock->clients) {
2967 cf_log_err_cs(config,
2968 "Failed to find any clients for this listen section");
2973 if (listen_bind(this) < 0) {
2975 ERROR("There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
2979 auth_port = sock->my_port; /* may have been updated in listen_bind */
2981 cs = cf_section_sub_find_name2(config, "server",
2983 if (cs) this->server = main_config.name;
2987 last = &(this->next);
2993 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
2996 #ifdef WITH_ACCOUNTING
2998 * Open Accounting Socket.
3000 * If we haven't already gotten acct_port from
3001 * /etc/services, then make it auth_port + 1.
3003 this = listen_alloc(config, RAD_LISTEN_ACCT);
3007 * Create the accounting socket.
3009 * The accounting port is always the
3010 * authentication port + 1
3012 sock->my_ipaddr = server_ipaddr;
3013 sock->my_port = auth_port + 1;
3015 sock->clients = clients_parse_section(config, false);
3016 if (!sock->clients) {
3017 cf_log_err_cs(config,
3018 "Failed to find any clients for this listen section");
3022 if (listen_bind(this) < 0) {
3025 ERROR("There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
3030 cs = cf_section_sub_find_name2(config, "server",
3032 if (cs) this->server = main_config.name;
3036 last = &(this->next);
3041 * They specified an IP on the command-line, ignore
3042 * all listen sections except the one in '-n'.
3044 if (main_config.myip.af != AF_UNSPEC) {
3045 CONF_SECTION *subcs;
3046 char const *name2 = cf_section_name2(cs);
3048 cs = cf_section_sub_find_name2(config, "server",
3050 if (!cs) goto add_sockets;
3053 * Should really abstract this code...
3055 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
3057 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
3058 this = listen_parse(subcs, name2);
3065 last = &(this->next);
3066 } /* loop over "listen" directives in server <foo> */
3072 * Walk through the "listen" sections, if they exist.
3074 for (cs = cf_subsection_find_next(config, NULL, "listen");
3076 cs = cf_subsection_find_next(config, cs, "listen")) {
3077 this = listen_parse(cs, NULL);
3084 last = &(this->next);
3088 * Check virtual servers for "listen" sections, too.
3090 * FIXME: Move to virtual server init?
3092 for (cs = cf_subsection_find_next(config, NULL, "server");
3094 cs = cf_subsection_find_next(config, cs, "server")) {
3095 CONF_SECTION *subcs;
3096 char const *name2 = cf_section_name2(cs);
3098 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
3100 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
3101 this = listen_parse(subcs, name2);
3108 last = &(this->next);
3109 } /* loop over "listen" directives in virtual servers */
3110 } /* loop over virtual servers */
3114 * No sockets to receive packets, this is an error.
3115 * proxying is pointless.
3118 ERROR("The server is not configured to listen on any ports. Cannot start");
3123 * Print out which sockets we're listening on, and
3124 * add them to the event list.
3126 for (this = *head; this != NULL; this = this->next) {
3128 if (this->type == RAD_LISTEN_PROXY) {
3129 defined_proxy = true;
3135 if (!check_config && !spawn_flag && this->tls) {
3136 cf_log_err_cs(this->cs, "Threading must be enabled for TLS sockets to function properly");
3137 cf_log_err_cs(this->cs, "You probably need to do '%s -fxx -l stdout' for debugging",
3142 if (!check_config) {
3143 if (this->workers && !spawn_flag) {
3144 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
3148 if (this->workers) {
3149 #ifdef HAVE_PTHREAD_H
3154 this->print(this, buffer, sizeof(buffer));
3156 for (i = 0; i < this->workers; i++) {
3160 * FIXME: create detached?
3162 rcode = pthread_create(&id, 0, recv_thread, this);
3164 ERROR("Thread create failed: %s",
3165 fr_syserror(rcode));
3169 DEBUG("Thread %d for %s\n", i, buffer);
3172 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
3177 radius_update_listener(this);
3184 * If we're proxying requests, open the proxy FD.
3185 * Otherwise, don't do anything.
3188 if ((main_config.proxy_requests == true) &&
3190 (*head != NULL) && !defined_proxy) {
3194 memset(&home, 0, sizeof(home));
3199 home.proto = IPPROTO_UDP;
3200 home.src_ipaddr = server_ipaddr;
3204 * Address is still unspecified, use IPv4.
3206 if (home.src_ipaddr.af == AF_UNSPEC) {
3207 home.src_ipaddr.af = AF_INET;
3208 /* everything else is already set to zero */
3211 home.ipaddr.af = home.src_ipaddr.af;
3212 /* everything else is already set to zero */
3214 this = proxy_new_listener(&home, port);
3220 radius_update_listener(this);
3225 * Haven't defined any sockets. Die.
3227 if (!*head) return -1;
3229 xlat_register("listen", xlat_listen, NULL, NULL);
3235 * Free a linked list of listeners;
3237 void listen_free(rad_listen_t **head)
3241 if (!head || !*head) return;
3245 rad_listen_t *next = this->next;
3254 RADCLIENT_LIST *listener_find_client_list(fr_ipaddr_t const *ipaddr, uint16_t port)
3258 for (this = main_config.listen; this != NULL; this = this->next) {
3259 listen_socket_t *sock;
3261 if ((this->type != RAD_LISTEN_AUTH)
3262 #ifdef WITH_ACCOUNTING
3263 && (this->type != RAD_LISTEN_ACCT)
3269 if ((sock->my_port == port) &&
3270 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
3271 return sock->clients;
3279 rad_listen_t *listener_find_byipaddr(fr_ipaddr_t const *ipaddr, uint16_t port, int proto)
3283 for (this = main_config.listen; this != NULL; this = this->next) {
3284 listen_socket_t *sock;
3288 if (sock->my_port != port) continue;
3289 if (sock->proto != proto) continue;
3290 if (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) != 0) continue;
3296 * Failed to find a specific one. Find INADDR_ANY
3298 for (this = main_config.listen; this != NULL; this = this->next) {
3299 listen_socket_t *sock;
3303 if (sock->my_port != port) continue;
3304 if (sock->proto != proto) continue;
3305 if (!fr_inaddr_any(&sock->my_ipaddr)) continue;