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 request->reply = rad_alloc_reply(request, request->packet);
251 if (!request->reply) {
252 talloc_free(request);
255 gettimeofday(&request->packet->timestamp, NULL);
257 request->priority = listener->type;
258 request->server = client->client_server;
259 request->root = &main_config;
262 * Run a fake request through the given virtual server.
263 * Look for FreeRADIUS-Client-IP-Address
264 * FreeRADIUS-Client-Secret
267 * and create the RADCLIENT structure from that.
269 DEBUG("server %s {", request->server);
271 rcode = process_authorize(0, request);
273 DEBUG("} # server %s", request->server);
275 if (rcode != RLM_MODULE_OK) {
276 talloc_free(request);
281 * If the client was updated by rlm_dynamic_clients,
282 * don't create the client from attribute-value pairs.
284 if (request->client == client) {
285 created = client_from_request(clients, request);
287 created = request->client;
290 * This frees the client if it isn't valid.
292 if (!client_validate(clients, client, created)) goto unknown;
295 request->server = client->server;
296 exec_trigger(request, NULL, "server.client.add", false);
298 talloc_free(request);
300 if (!created) goto unknown;
306 static int listen_bind(rad_listen_t *this);
310 * Process and reply to a server-status request.
311 * Like rad_authenticate and rad_accounting this should
312 * live in it's own file but it's so small we don't bother.
314 int rad_status_server(REQUEST *request)
316 int rcode = RLM_MODULE_OK;
319 switch (request->listener->type) {
321 case RAD_LISTEN_NONE:
323 case RAD_LISTEN_AUTH:
324 dval = dict_valbyname(PW_AUTZ_TYPE, 0, "Status-Server");
326 rcode = process_authorize(dval->value, request);
328 rcode = RLM_MODULE_OK;
333 case RLM_MODULE_UPDATED:
334 request->reply->code = PW_CODE_ACCESS_ACCEPT;
337 case RLM_MODULE_FAIL:
338 case RLM_MODULE_HANDLED:
339 request->reply->code = 0; /* don't reply */
343 case RLM_MODULE_REJECT:
344 request->reply->code = PW_CODE_ACCESS_REJECT;
349 #ifdef WITH_ACCOUNTING
350 case RAD_LISTEN_ACCT:
351 dval = dict_valbyname(PW_ACCT_TYPE, 0, "Status-Server");
353 rcode = process_accounting(dval->value, request);
355 rcode = RLM_MODULE_OK;
360 case RLM_MODULE_UPDATED:
361 request->reply->code = PW_CODE_ACCOUNTING_RESPONSE;
365 request->reply->code = 0; /* don't reply */
373 * This is a vendor extension. Suggested by Glen
374 * Zorn in IETF 72, and rejected by the rest of
375 * the WG. We like it, so it goes in here.
378 dval = dict_valbyname(PW_RECV_COA_TYPE, 0, "Status-Server");
380 rcode = process_recv_coa(dval->value, request);
382 rcode = RLM_MODULE_OK;
387 case RLM_MODULE_UPDATED:
388 request->reply->code = PW_CODE_COA_ACK;
392 request->reply->code = 0; /* don't reply */
404 * Full statistics are available only on a statistics
407 if (request->listener->type == RAD_LISTEN_NONE) {
408 request_stats_reply(request);
416 static int dual_tcp_recv(rad_listen_t *listener)
419 RADIUS_PACKET *packet;
420 RAD_REQUEST_FUNP fun = NULL;
421 listen_socket_t *sock = listener->data;
422 RADCLIENT *client = sock->client;
424 rad_assert(client != NULL);
426 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return 0;
429 * Allocate a packet for partial reads.
432 sock->packet = rad_alloc(sock, false);
433 if (!sock->packet) return 0;
435 sock->packet->sockfd = listener->fd;
436 sock->packet->src_ipaddr = sock->other_ipaddr;
437 sock->packet->src_port = sock->other_port;
438 sock->packet->dst_ipaddr = sock->my_ipaddr;
439 sock->packet->dst_port = sock->my_port;
440 sock->packet->proto = sock->proto;
444 * Grab the packet currently being processed.
446 packet = sock->packet;
448 rcode = fr_tcp_read_packet(packet, 0);
451 * Still only a partial packet. Put it back, and return,
452 * so that we'll read more data when it's ready.
458 if (rcode == -1) { /* error reading packet */
461 ERROR("Invalid packet from %s port %d, closing socket: %s",
462 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
463 packet->src_port, fr_strerror());
466 if (rcode < 0) { /* error or connection reset */
467 listener->status = RAD_LISTEN_STATUS_EOL;
470 * Tell the event handler that an FD has disappeared.
472 DEBUG("Client has closed connection");
473 radius_update_listener(listener);
476 * Do NOT free the listener here. It's in use by
477 * a request, and will need to hang around until
478 * all of the requests are done.
480 * It is instead free'd in remove_from_request_hash()
486 * Some sanity checks, based on the packet code.
488 switch(packet->code) {
489 case PW_CODE_ACCESS_REQUEST:
490 if (listener->type != RAD_LISTEN_AUTH) goto bad_packet;
491 FR_STATS_INC(auth, total_requests);
492 fun = rad_authenticate;
495 #ifdef WITH_ACCOUNTING
496 case PW_CODE_ACCOUNTING_REQUEST:
497 if (listener->type != RAD_LISTEN_ACCT) goto bad_packet;
498 FR_STATS_INC(acct, total_requests);
499 fun = rad_accounting;
503 case PW_CODE_STATUS_SERVER:
504 if (!main_config.status_server) {
505 FR_STATS_INC(auth, total_unknown_types);
506 WARN("Ignoring Status-Server request due to security configuration");
507 rad_free(&sock->packet);
510 fun = rad_status_server;
515 FR_STATS_INC(auth, total_unknown_types);
517 DEBUG("Invalid packet code %d sent from client %s port %d : IGNORED",
518 packet->code, client->shortname, packet->src_port);
519 rad_free(&sock->packet);
521 } /* switch over packet types */
523 if (!request_receive(listener, packet, client, fun)) {
524 FR_STATS_INC(auth, total_packets_dropped);
525 rad_free(&sock->packet);
529 sock->packet = NULL; /* we have no need for more partial reads */
533 static int dual_tcp_accept(rad_listen_t *listener)
539 struct sockaddr_storage src;
540 listen_socket_t *sock;
541 fr_ipaddr_t src_ipaddr;
542 RADCLIENT *client = NULL;
546 DEBUG2(" ... new connection request on TCP socket");
548 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
551 * Non-blocking sockets must handle this.
554 if (errno == EWOULDBLOCK) {
559 DEBUG2(" ... failed to accept connection");
563 if (!fr_sockaddr2ipaddr(&src, salen, &src_ipaddr, &src_port)) {
565 DEBUG2(" ... unknown address family");
570 * Enforce client IP address checks on accept, not on
573 if ((client = client_listener_find(listener,
574 &src_ipaddr, src_port)) == NULL) {
576 FR_STATS_INC(auth, total_invalid_requests);
582 * Enforce security restrictions.
584 * This shouldn't be necessary in practice. However, it
585 * serves as a double-check on configurations. Marking a
586 * client as "tls required" means that any accidental
587 * exposure of the client to non-TLS traffic is
590 if (client->tls_required && !listener->tls) {
591 INFO("Ignoring connection to TLS socket from non-TLS client");
598 * Enforce max_connections on client && listen section.
600 if ((client->limit.max_connections != 0) &&
601 (client->limit.max_connections == client->limit.num_connections)) {
603 * FIXME: Print client IP/port, and server IP/port.
605 INFO("Ignoring new connection due to client max_connections (%d)", client->limit.max_connections);
610 sock = listener->data;
611 if ((sock->limit.max_connections != 0) &&
612 (sock->limit.max_connections == sock->limit.num_connections)) {
614 * FIXME: Print client IP/port, and server IP/port.
616 INFO("Ignoring new connection due to socket max_connections");
620 client->limit.num_connections++;
621 sock->limit.num_connections++;
624 * Add the new listener.
626 this = listen_alloc(listener, listener->type);
627 if (!this) return -1;
630 * Copy everything, including the pointer to the socket
634 memcpy(this->data, listener->data, sizeof(*sock));
635 memcpy(this, listener, sizeof(*this));
637 this->data = sock; /* fix it back */
639 sock->parent = listener->data;
640 sock->other_ipaddr = src_ipaddr;
641 sock->other_port = src_port;
642 sock->client = client;
643 sock->opened = sock->last_packet = time(NULL);
646 * Set the limits. The defaults are the parent limits.
647 * Client limits on max_connections are enforced dynamically.
648 * Set the MINIMUM of client/socket idle timeout or lifetime.
650 memcpy(&sock->limit, &sock->parent->limit, sizeof(sock->limit));
652 if (client->limit.idle_timeout &&
653 ((sock->limit.idle_timeout == 0) ||
654 (client->limit.idle_timeout < sock->limit.idle_timeout))) {
655 sock->limit.idle_timeout = client->limit.idle_timeout;
658 if (client->limit.lifetime &&
659 ((sock->limit.lifetime == 0) ||
660 (client->limit.lifetime < sock->limit.lifetime))) {
661 sock->limit.lifetime = client->limit.lifetime;
665 this->status = RAD_LISTEN_STATUS_INIT;
668 #ifdef WITH_COMMAND_SOCKET
669 if (this->type == RAD_LISTEN_COMMAND) {
670 this->recv = command_tcp_recv;
671 this->send = command_tcp_send;
672 command_write_magic(this->fd, sock);
677 this->recv = dual_tcp_recv;
681 this->recv = dual_tls_recv;
682 this->send = dual_tls_send;
688 * FIXME: set O_NONBLOCK on the accept'd fd.
689 * See djb's portability rants for details.
693 * Tell the event loop that we have a new FD.
694 * This can be called from a child thread...
696 radius_update_listener(this);
703 * Ensure that we always keep the correct counters.
706 static void common_socket_free(rad_listen_t *this)
708 listen_socket_t *sock = this->data;
710 if (sock->proto != IPPROTO_TCP) return;
713 * Decrement the number of connections.
715 if (sock->parent && (sock->parent->limit.num_connections > 0)) {
716 sock->parent->limit.num_connections--;
718 if (sock->client && sock->client->limit.num_connections > 0) {
719 sock->client->limit.num_connections--;
721 if (sock->home && sock->home->limit.num_connections > 0) {
722 sock->home->limit.num_connections--;
726 static void common_socket_free(UNUSED rad_listen_t *this)
733 * This function is stupid and complicated.
735 int common_socket_print(rad_listen_t const *this, char *buffer, size_t bufsize)
738 listen_socket_t *sock = this->data;
739 char const *name = master_listen[this->type].name;
741 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
742 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
750 if (sock->interface) {
751 ADDSTRING(" interface ");
752 ADDSTRING(sock->interface);
756 if (this->recv == dual_tcp_accept) {
757 ADDSTRING(" proto tcp");
763 * TCP sockets get printed a little differently, to make
764 * it clear what's going on.
767 ADDSTRING(" from client (");
768 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
772 snprintf(buffer, bufsize, "%d", sock->other_port);
776 if ((sock->my_ipaddr.af == AF_INET) &&
777 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
778 strlcpy(buffer, "*", bufsize);
780 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
785 snprintf(buffer, bufsize, "%d", sock->my_port);
789 ADDSTRING(", virtual-server=");
790 ADDSTRING(this->server);
800 * Maybe it's a socket that we opened to a home server.
802 if ((sock->proto == IPPROTO_TCP) &&
803 (this->type == RAD_LISTEN_PROXY)) {
805 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
809 snprintf(buffer, bufsize, "%d", sock->my_port);
811 ADDSTRING(") -> home_server (");
813 if ((sock->other_ipaddr.af == AF_INET) &&
814 (sock->other_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
815 strlcpy(buffer, "*", bufsize);
817 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
822 snprintf(buffer, bufsize, "%d", sock->other_port);
829 #endif /* WITH_PROXY */
830 #endif /* WITH_TCP */
832 ADDSTRING(" address ");
834 if ((sock->my_ipaddr.af == AF_INET) &&
835 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
836 strlcpy(buffer, "*", bufsize);
838 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
843 snprintf(buffer, bufsize, "%d", sock->my_port);
854 ADDSTRING(" as server ");
855 ADDSTRING(this->server);
864 extern bool check_config; /* radiusd.c */
866 static CONF_PARSER performance_config[] = {
867 { "skip_duplicate_checks", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rad_listen_t, nodup), NULL },
869 { "synchronous", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rad_listen_t, synchronous), NULL },
871 { "workers", FR_CONF_OFFSET(PW_TYPE_INTEGER, rad_listen_t, workers), NULL },
873 { NULL, -1, 0, NULL, NULL } /* end the list */
877 static CONF_PARSER limit_config[] = {
878 { "max_pps", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_socket_t, max_rate), NULL },
881 { "max_connections", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_socket_t, limit.max_connections), "16" },
882 { "lifetime", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_socket_t, limit.lifetime), "0" },
883 { "idle_timeout", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_socket_t, limit.idle_timeout), STRINGIFY(30) },
886 { NULL, -1, 0, NULL, NULL } /* end the list */
890 * Parse an authentication or accounting socket.
892 int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
895 uint16_t listen_port;
897 listen_socket_t *sock = this->data;
898 char const *section_name = NULL;
899 CONF_SECTION *client_cs, *parentcs;
907 memset(&ipaddr, 0, sizeof(ipaddr));
908 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
910 rcode = cf_item_parse(cs, "ipaddr", FR_ITEM_POINTER(PW_TYPE_IP_ADDR, &ipaddr), NULL);
911 if (rcode < 0) return -1;
912 if (rcode != 0) rcode = cf_item_parse(cs, "ipv4addr", FR_ITEM_POINTER(PW_TYPE_IPV4_ADDR, &ipaddr), NULL);
913 if (rcode < 0) return -1;
914 if (rcode != 0) rcode = cf_item_parse(cs, "ipv6addr", FR_ITEM_POINTER(PW_TYPE_IPV6_ADDR, &ipaddr), NULL);
915 if (rcode < 0) return -1;
917 cf_log_err_cs(cs, "No address specified in listen section");
921 rcode = cf_item_parse(cs, "port", FR_ITEM_POINTER(PW_TYPE_SHORT, &listen_port), "0");
922 if (rcode < 0) return -1;
924 sock->proto = IPPROTO_UDP;
926 if (cf_pair_find(cs, "proto")) {
929 "System does not support the TCP protocol. Delete this line from the configuration file");
932 char const *proto = NULL;
937 rcode = cf_item_parse(cs, "proto", FR_ITEM_POINTER(PW_TYPE_STRING, &proto), "udp");
938 if (rcode < 0) return -1;
940 if (strcmp(proto, "udp") == 0) {
941 sock->proto = IPPROTO_UDP;
943 } else if (strcmp(proto, "tcp") == 0) {
944 sock->proto = IPPROTO_TCP;
947 "Unknown proto name \"%s\"", proto);
952 * TCP requires a destination IP for sockets.
953 * UDP doesn't, so it's allowed.
956 if ((this->type == RAD_LISTEN_PROXY) &&
957 (sock->proto != IPPROTO_UDP)) {
959 "Proxy listeners can only listen on proto = udp");
962 #endif /* WITH_PROXY */
965 tls = cf_section_sub_find(cs, "tls");
969 * Don't allow TLS configurations for UDP sockets.
971 if (sock->proto != IPPROTO_TCP) {
973 "TLS transport is not available for UDP sockets");
978 * If unset, set to default.
980 if (listen_port == 0) listen_port = PW_RADIUS_TLS_PORT;
982 this->tls = tls_server_conf_parse(tls);
987 #ifdef HAVE_PTRHEAD_H
988 if (pthread_mutex_init(&sock->mutex, NULL) < 0) {
998 * Built without TLS. Disallow it.
1000 if (cf_section_sub_find(cs, "tls")) {
1002 "TLS transport is not available in this executable");
1005 #endif /* WITH_TLS */
1007 #endif /* WITH_TCP */
1010 * No "proto" field. Disallow TLS.
1012 } else if (cf_section_sub_find(cs, "tls")) {
1014 "TLS transport is not available in this \"listen\" section");
1019 * Magical tuning methods!
1021 subcs = cf_section_sub_find(cs, "performance");
1023 rcode = cf_section_parse(subcs, this,
1024 performance_config);
1025 if (rcode < 0) return -1;
1027 if (this->synchronous && sock->max_rate) {
1028 WARN("Setting 'max_pps' is incompatible with 'synchronous'. Disabling 'max_pps'");
1032 if (!this->synchronous && this->workers) {
1033 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
1038 subcs = cf_section_sub_find(cs, "limit");
1040 rcode = cf_section_parse(subcs, sock,
1042 if (rcode < 0) return -1;
1044 if (sock->max_rate && ((sock->max_rate < 10) || (sock->max_rate > 1000000))) {
1046 "Invalid value for \"max_pps\"");
1051 if ((sock->limit.idle_timeout > 0) && (sock->limit.idle_timeout < 5)) {
1052 WARN("Setting idle_timeout to 5");
1053 sock->limit.idle_timeout = 5;
1056 if ((sock->limit.lifetime > 0) && (sock->limit.lifetime < 5)) {
1057 WARN("Setting lifetime to 5");
1058 sock->limit.lifetime = 5;
1061 if ((sock->limit.lifetime > 0) && (sock->limit.idle_timeout > sock->limit.lifetime)) {
1062 WARN("Setting idle_timeout to 0");
1063 sock->limit.idle_timeout = 0;
1067 * Force no duplicate detection for TCP sockets.
1069 if (sock->proto == IPPROTO_TCP) {
1074 sock->limit.max_connections = 60;
1075 sock->limit.idle_timeout = 30;
1076 sock->limit.lifetime = 0;
1080 sock->my_ipaddr = ipaddr;
1081 sock->my_port = listen_port;
1086 * Until there is a side effects free way of forwarding a
1087 * request to another virtual server, this check is invalid,
1088 * and should be left disabled.
1091 if (home_server_find(&sock->my_ipaddr, sock->my_port, sock->proto)) {
1094 ERROR("We have been asked to listen on %s port %d, which is also listed as a "
1095 "home server. This can create a proxy loop",
1096 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)), sock->my_port);
1100 return 0; /* don't do anything */
1105 * If we can bind to interfaces, do so,
1108 if (cf_pair_find(cs, "interface")) {
1110 CONF_PAIR *cp = cf_pair_find(cs, "interface");
1112 rad_assert(cp != NULL);
1113 value = cf_pair_value(cp);
1116 "No interface name given");
1119 sock->interface = value;
1124 * If we can do broadcasts..
1126 if (cf_pair_find(cs, "broadcast")) {
1127 #ifndef SO_BROADCAST
1129 "System does not support broadcast sockets. Delete this line from the configuration file");
1133 CONF_PAIR *cp = cf_pair_find(cs, "broadcast");
1135 if (this->type != RAD_LISTEN_DHCP) {
1137 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file");
1141 rad_assert(cp != NULL);
1142 value = cf_pair_value(cp);
1145 "No broadcast value given");
1150 * Hack... whatever happened to cf_section_parse?
1152 sock->broadcast = (strcmp(value, "yes") == 0);
1158 * And bind it to the port.
1160 if (listen_bind(this) < 0) {
1163 "Error binding to port for %s port %d",
1164 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
1171 * Proxy sockets don't have clients.
1173 if (this->type == RAD_LISTEN_PROXY) return 0;
1177 * The more specific configurations are preferred to more
1181 parentcs = cf_top_section(cs);
1182 rcode = cf_item_parse(cs, "clients", FR_ITEM_POINTER(PW_TYPE_STRING, §ion_name), NULL);
1183 if (rcode < 0) return -1; /* bad string */
1186 * Explicit list given: use it.
1188 client_cs = cf_section_sub_find_name2(parentcs, "clients", section_name);
1190 client_cs = cf_section_find(section_name);
1194 "Failed to find clients %s {...}",
1198 } /* else there was no "clients = " entry. */
1201 CONF_SECTION *server_cs;
1203 server_cs = cf_section_sub_find_name2(parentcs,
1207 * Found a "server foo" section. If there are clients
1211 (cf_section_sub_find(server_cs, "client") != NULL)) {
1212 client_cs = server_cs;
1217 * Still nothing. Look for global clients.
1219 if (!client_cs) client_cs = parentcs;
1222 sock->clients = clients_parse_section(client_cs, (this->tls != NULL));
1224 sock->clients = clients_parse_section(client_cs, false);
1226 if (!sock->clients) {
1228 "Failed to load clients for this listen section");
1233 if (sock->proto == IPPROTO_TCP) {
1235 * Re-write the listener receive function to
1236 * allow us to accept the socket.
1238 this->recv = dual_tcp_accept;
1246 * Send an authentication response packet
1248 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1250 rad_assert(request->listener == listener);
1251 rad_assert(listener->send == auth_socket_send);
1253 if (request->reply->code == 0) return 0;
1255 #ifdef WITH_UDPFROMTO
1257 * Overwrite the src ip address on the outbound packet
1258 * with the one specified by the client.
1259 * This is useful to work around broken DSR implementations
1260 * and other routing issues.
1262 if (request->client->src_ipaddr.af != AF_UNSPEC) {
1263 request->reply->src_ipaddr = request->client->src_ipaddr;
1267 if (rad_send(request->reply, request->packet,
1268 request->client->secret) < 0) {
1269 RERROR("Failed sending reply: %s",
1278 #ifdef WITH_ACCOUNTING
1280 * Send an accounting response packet (or not)
1282 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1284 rad_assert(request->listener == listener);
1285 rad_assert(listener->send == acct_socket_send);
1288 * Accounting reject's are silently dropped.
1290 * We do it here to avoid polluting the rest of the
1291 * code with this knowledge
1293 if (request->reply->code == 0) return 0;
1295 #ifdef WITH_UDPFROMTO
1297 * Overwrite the src ip address on the outbound packet
1298 * with the one specified by the client.
1299 * This is useful to work around broken DSR implementations
1300 * and other routing issues.
1302 if (request->client->src_ipaddr.af != AF_UNSPEC) {
1303 request->reply->src_ipaddr = request->client->src_ipaddr;
1307 if (rad_send(request->reply, request->packet,
1308 request->client->secret) < 0) {
1309 RERROR("Failed sending reply: %s",
1320 * Send a packet to a home server.
1322 * FIXME: have different code for proxy auth & acct!
1324 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1326 rad_assert(request->proxy_listener == listener);
1327 rad_assert(listener->send == proxy_socket_send);
1329 if (rad_send(request->proxy, NULL,
1330 request->home_server->secret) < 0) {
1331 RERROR("Failed sending proxied request: %s",
1342 * Check if an incoming request is "ok"
1344 * It takes packets, not requests. It sees if the packet looks
1345 * OK. If so, it does a number of sanity checks on it.
1347 static int stats_socket_recv(rad_listen_t *listener)
1352 RADIUS_PACKET *packet;
1353 RADCLIENT *client = NULL;
1354 fr_ipaddr_t src_ipaddr;
1356 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1357 if (rcode < 0) return 0;
1359 FR_STATS_INC(auth, total_requests);
1361 if (rcode < 20) { /* AUTH_HDR_LEN */
1362 FR_STATS_INC(auth, total_malformed_requests);
1366 if ((client = client_listener_find(listener,
1367 &src_ipaddr, src_port)) == NULL) {
1368 rad_recv_discard(listener->fd);
1369 FR_STATS_INC(auth, total_invalid_requests);
1373 FR_STATS_TYPE_INC(client->auth.total_requests);
1376 * We only understand Status-Server on this socket.
1378 if (code != PW_CODE_STATUS_SERVER) {
1379 DEBUG("Ignoring packet code %d sent to Status-Server port",
1381 rad_recv_discard(listener->fd);
1382 FR_STATS_INC(auth, total_unknown_types);
1387 * Now that we've sanity checked everything, receive the
1390 packet = rad_recv(listener->fd, 1); /* require message authenticator */
1392 FR_STATS_INC(auth, total_malformed_requests);
1393 DEBUG("%s", fr_strerror());
1397 if (!request_receive(listener, packet, client, rad_status_server)) {
1398 FR_STATS_INC(auth, total_packets_dropped);
1409 * Check if an incoming request is "ok"
1411 * It takes packets, not requests. It sees if the packet looks
1412 * OK. If so, it does a number of sanity checks on it.
1414 static int auth_socket_recv(rad_listen_t *listener)
1419 RADIUS_PACKET *packet;
1420 RAD_REQUEST_FUNP fun = NULL;
1421 RADCLIENT *client = NULL;
1422 fr_ipaddr_t src_ipaddr;
1424 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1425 if (rcode < 0) return 0;
1427 FR_STATS_INC(auth, total_requests);
1429 if (rcode < 20) { /* AUTH_HDR_LEN */
1430 FR_STATS_INC(auth, total_malformed_requests);
1434 if ((client = client_listener_find(listener,
1435 &src_ipaddr, src_port)) == NULL) {
1436 rad_recv_discard(listener->fd);
1437 FR_STATS_INC(auth, total_invalid_requests);
1441 FR_STATS_TYPE_INC(client->auth.total_requests);
1444 * Some sanity checks, based on the packet code.
1447 case PW_CODE_ACCESS_REQUEST:
1448 fun = rad_authenticate;
1451 case PW_CODE_STATUS_SERVER:
1452 if (!main_config.status_server) {
1453 rad_recv_discard(listener->fd);
1454 FR_STATS_INC(auth, total_unknown_types);
1455 WARN("Ignoring Status-Server request due to security configuration");
1458 fun = rad_status_server;
1462 rad_recv_discard(listener->fd);
1463 FR_STATS_INC(auth,total_unknown_types);
1465 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
1466 code, client->shortname, src_port);
1469 } /* switch over packet types */
1472 * Now that we've sanity checked everything, receive the
1475 packet = rad_recv(listener->fd, client->message_authenticator);
1477 FR_STATS_INC(auth, total_malformed_requests);
1478 DEBUG("%s", fr_strerror());
1483 #ifdef WITH_UDPFROMTO
1485 * This is a NICE Mac OSX bug. Create an interface with
1486 * two IP address, and then configure one listener for
1487 * each IP address. Send thousands of packets to one
1488 * address, and some will show up on the OTHER socket.
1490 * This hack works ONLY if the clients are global. If
1491 * each listener has the same client IP, but with
1492 * different secrets, then it will fail the rad_recv()
1493 * check above, and there's nothing you can do.
1496 listen_socket_t *sock = listener->data;
1497 rad_listen_t *other;
1499 other = listener_find_byipaddr(&packet->dst_ipaddr,
1500 packet->dst_port, sock->proto);
1501 if (other) listener = other;
1507 if (!request_receive(listener, packet, client, fun)) {
1508 FR_STATS_INC(auth, total_packets_dropped);
1517 #ifdef WITH_ACCOUNTING
1519 * Receive packets from an accounting socket
1521 static int acct_socket_recv(rad_listen_t *listener)
1526 RADIUS_PACKET *packet;
1527 RAD_REQUEST_FUNP fun = NULL;
1528 RADCLIENT *client = NULL;
1529 fr_ipaddr_t src_ipaddr;
1531 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1532 if (rcode < 0) return 0;
1534 FR_STATS_INC(acct, total_requests);
1536 if (rcode < 20) { /* AUTH_HDR_LEN */
1537 FR_STATS_INC(acct, total_malformed_requests);
1541 if ((client = client_listener_find(listener,
1542 &src_ipaddr, src_port)) == NULL) {
1543 rad_recv_discard(listener->fd);
1544 FR_STATS_INC(acct, total_invalid_requests);
1548 FR_STATS_TYPE_INC(client->acct.total_requests);
1551 * Some sanity checks, based on the packet code.
1554 case PW_CODE_ACCOUNTING_REQUEST:
1555 fun = rad_accounting;
1558 case PW_CODE_STATUS_SERVER:
1559 if (!main_config.status_server) {
1560 rad_recv_discard(listener->fd);
1561 FR_STATS_INC(acct, total_unknown_types);
1563 WARN("Ignoring Status-Server request due to security configuration");
1566 fun = rad_status_server;
1570 rad_recv_discard(listener->fd);
1571 FR_STATS_INC(acct, total_unknown_types);
1573 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1574 code, client->shortname, src_port);
1576 } /* switch over packet types */
1579 * Now that we've sanity checked everything, receive the
1582 packet = rad_recv(listener->fd, 0);
1584 FR_STATS_INC(acct, total_malformed_requests);
1585 ERROR("%s", fr_strerror());
1590 * There can be no duplicate accounting packets.
1592 if (!request_receive(listener, packet, client, fun)) {
1593 FR_STATS_INC(acct, total_packets_dropped);
1604 static int do_proxy(REQUEST *request)
1608 if (request->in_proxy_hash ||
1609 (request->proxy_reply && (request->proxy_reply->code != 0))) {
1613 vp = pairfind(request->config_items, PW_HOME_SERVER_POOL, 0, TAG_ANY);
1616 if (!home_pool_byname(vp->vp_strvalue, HOME_TYPE_COA)) {
1617 REDEBUG2("Cannot proxy to unknown pool %s",
1626 * Receive a CoA packet.
1628 static int rad_coa_recv(REQUEST *request)
1630 int rcode = RLM_MODULE_OK;
1635 * Get the correct response
1637 switch (request->packet->code) {
1638 case PW_CODE_COA_REQUEST:
1639 ack = PW_CODE_COA_ACK;
1640 nak = PW_CODE_COA_NAK;
1643 case PW_CODE_DISCONNECT_REQUEST:
1644 ack = PW_CODE_DISCONNECT_ACK;
1645 nak = PW_CODE_DISCONNECT_NAK;
1648 default: /* shouldn't happen */
1649 return RLM_MODULE_FAIL;
1653 #define WAS_PROXIED (request->proxy)
1655 #define WAS_PROXIED (0)
1660 * RFC 5176 Section 3.3. If we have a CoA-Request
1661 * with Service-Type = Authorize-Only, it MUST
1662 * have a State attribute in it.
1664 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE, 0, TAG_ANY);
1665 if (request->packet->code == PW_CODE_COA_REQUEST) {
1666 if (vp && (vp->vp_integer == 17)) {
1667 vp = pairfind(request->packet->vps, PW_STATE, 0, TAG_ANY);
1668 if (!vp || (vp->length == 0)) {
1669 REDEBUG("CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1670 request->reply->code = PW_CODE_COA_NAK;
1671 return RLM_MODULE_FAIL;
1676 * RFC 5176, Section 3.2.
1678 REDEBUG("Disconnect-Request MUST NOT contain a Service-Type attribute");
1679 request->reply->code = PW_CODE_DISCONNECT_NAK;
1680 return RLM_MODULE_FAIL;
1683 rcode = process_recv_coa(0, request);
1685 case RLM_MODULE_FAIL:
1686 case RLM_MODULE_INVALID:
1687 case RLM_MODULE_REJECT:
1688 case RLM_MODULE_USERLOCK:
1690 request->reply->code = nak;
1693 case RLM_MODULE_HANDLED:
1696 case RLM_MODULE_NOOP:
1697 case RLM_MODULE_NOTFOUND:
1699 case RLM_MODULE_UPDATED:
1700 if (do_proxy(request)) return RLM_MODULE_OK;
1701 request->reply->code = ack;
1704 } else if (request->proxy_reply) {
1706 * Start the reply code with the proxy reply
1709 request->reply->code = request->proxy_reply->code;
1713 * Copy State from the request to the reply.
1714 * See RFC 5176 Section 3.3.
1716 vp = paircopy2(request->reply, request->packet->vps, PW_STATE, 0, TAG_ANY);
1717 if (vp) pairadd(&request->reply->vps, vp);
1720 * We may want to over-ride the reply.
1722 if (request->reply->code) {
1723 rcode = process_send_coa(0, request);
1726 * We need to send CoA-NAK back if Service-Type
1727 * is Authorize-Only. Rely on the user's policy
1728 * to do that. We're not a real NAS, so this
1729 * restriction doesn't (ahem) apply to us.
1731 case RLM_MODULE_FAIL:
1732 case RLM_MODULE_INVALID:
1733 case RLM_MODULE_REJECT:
1734 case RLM_MODULE_USERLOCK:
1737 * Over-ride an ACK with a NAK
1739 request->reply->code = nak;
1742 case RLM_MODULE_HANDLED:
1745 case RLM_MODULE_NOOP:
1746 case RLM_MODULE_NOTFOUND:
1748 case RLM_MODULE_UPDATED:
1750 * Do NOT over-ride a previously set value.
1751 * Otherwise an "ok" here will re-write a
1754 if (request->reply->code == 0) {
1755 request->reply->code = ack;
1761 return RLM_MODULE_OK;
1766 * Check if an incoming request is "ok"
1768 * It takes packets, not requests. It sees if the packet looks
1769 * OK. If so, it does a number of sanity checks on it.
1771 static int coa_socket_recv(rad_listen_t *listener)
1776 RADIUS_PACKET *packet;
1777 RAD_REQUEST_FUNP fun = NULL;
1778 RADCLIENT *client = NULL;
1779 fr_ipaddr_t src_ipaddr;
1781 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1782 if (rcode < 0) return 0;
1784 if (rcode < 20) { /* AUTH_HDR_LEN */
1785 FR_STATS_INC(coa, total_requests);
1786 FR_STATS_INC(coa, total_malformed_requests);
1790 if ((client = client_listener_find(listener,
1791 &src_ipaddr, src_port)) == NULL) {
1792 rad_recv_discard(listener->fd);
1793 FR_STATS_INC(coa, total_requests);
1794 FR_STATS_INC(coa, total_invalid_requests);
1799 * Some sanity checks, based on the packet code.
1802 case PW_CODE_COA_REQUEST:
1803 FR_STATS_INC(coa, total_requests);
1807 case PW_CODE_DISCONNECT_REQUEST:
1808 FR_STATS_INC(dsc, total_requests);
1813 rad_recv_discard(listener->fd);
1814 FR_STATS_INC(coa, total_unknown_types);
1815 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1816 code, client->shortname, src_port);
1818 } /* switch over packet types */
1821 * Now that we've sanity checked everything, receive the
1824 packet = rad_recv(listener->fd, client->message_authenticator);
1826 FR_STATS_INC(coa, total_malformed_requests);
1827 DEBUG("%s", fr_strerror());
1831 if (!request_receive(listener, packet, client, fun)) {
1832 FR_STATS_INC(coa, total_packets_dropped);
1843 * Recieve packets from a proxy socket.
1845 static int proxy_socket_recv(rad_listen_t *listener)
1847 RADIUS_PACKET *packet;
1850 packet = rad_recv(listener->fd, 0);
1852 ERROR("%s", fr_strerror());
1857 * FIXME: Client MIB updates?
1859 switch(packet->code) {
1860 case PW_CODE_ACCESS_ACCEPT:
1861 case PW_CODE_ACCESS_CHALLENGE:
1862 case PW_CODE_ACCESS_REJECT:
1865 #ifdef WITH_ACCOUNTING
1866 case PW_CODE_ACCOUNTING_RESPONSE:
1871 case PW_CODE_DISCONNECT_ACK:
1872 case PW_CODE_DISCONNECT_NAK:
1873 case PW_CODE_COA_ACK:
1874 case PW_CODE_COA_NAK:
1880 * FIXME: Update MIB for packet types?
1882 ERROR("Invalid packet code %d sent to a proxy port "
1883 "from home server %s port %d - ID %d : IGNORED",
1885 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1886 packet->src_port, packet->id);
1891 if (!request_proxy_reply(packet)) {
1901 * Recieve packets from a proxy socket.
1903 static int proxy_socket_tcp_recv(rad_listen_t *listener)
1905 RADIUS_PACKET *packet;
1906 listen_socket_t *sock = listener->data;
1909 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return 0;
1911 packet = fr_tcp_recv(listener->fd, 0);
1913 listener->status = RAD_LISTEN_STATUS_EOL;
1914 radius_update_listener(listener);
1919 * FIXME: Client MIB updates?
1921 switch(packet->code) {
1922 case PW_CODE_ACCESS_ACCEPT:
1923 case PW_CODE_ACCESS_CHALLENGE:
1924 case PW_CODE_ACCESS_REJECT:
1927 #ifdef WITH_ACCOUNTING
1928 case PW_CODE_ACCOUNTING_RESPONSE:
1934 * FIXME: Update MIB for packet types?
1936 ERROR("Invalid packet code %d sent to a proxy port "
1937 "from home server %s port %d - ID %d : IGNORED",
1939 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1940 packet->src_port, packet->id);
1945 packet->src_ipaddr = sock->other_ipaddr;
1946 packet->src_port = sock->other_port;
1947 packet->dst_ipaddr = sock->my_ipaddr;
1948 packet->dst_port = sock->my_port;
1951 * FIXME: Have it return an indication of packets that
1952 * are OK to ignore (dups, too late), versus ones that
1953 * aren't OK to ignore (unknown response, spoofed, etc.)
1955 * Close the socket on bad packets...
1957 if (!request_proxy_reply(packet)) {
1962 sock->opened = sock->last_packet = time(NULL);
1970 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1972 if (!request->reply->code) return 0;
1974 if (rad_encode(request->reply, request->packet,
1975 request->client->secret) < 0) {
1976 RERROR("Failed encoding packet: %s",
1981 if (rad_sign(request->reply, request->packet,
1982 request->client->secret) < 0) {
1983 RERROR("Failed signing packet: %s",
1992 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1994 if (rad_verify(request->packet, NULL,
1995 request->client->secret) < 0) {
1999 return rad_decode(request->packet, NULL,
2000 request->client->secret);
2004 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
2006 if (rad_encode(request->proxy, NULL, request->home_server->secret) < 0) {
2007 RERROR("Failed encoding proxied packet: %s",
2012 if (rad_sign(request->proxy, NULL, request->home_server->secret) < 0) {
2013 RERROR("Failed signing proxied packet: %s",
2022 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
2025 * rad_verify is run in event.c, received_proxy_response()
2028 return rad_decode(request->proxy_reply, request->proxy,
2029 request->home_server->secret);
2033 #include "command.c"
2036 * Temporarily NOT const!
2038 static fr_protocol_t master_listen[RAD_LISTEN_MAX] = {
2040 { RLM_MODULE_INIT, "status", sizeof(listen_socket_t), NULL,
2041 common_socket_parse, NULL,
2042 stats_socket_recv, auth_socket_send,
2043 common_socket_print, client_socket_encode, client_socket_decode },
2046 * This always gets defined.
2048 { RLM_MODULE_INIT, "status", 0, NULL,
2049 NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
2054 { RLM_MODULE_INIT, "proxy", sizeof(listen_socket_t), NULL,
2055 common_socket_parse, NULL,
2056 proxy_socket_recv, proxy_socket_send,
2057 common_socket_print, proxy_socket_encode, proxy_socket_decode },
2060 /* authentication */
2061 { RLM_MODULE_INIT, "auth", sizeof(listen_socket_t), NULL,
2062 common_socket_parse, common_socket_free,
2063 auth_socket_recv, auth_socket_send,
2064 common_socket_print, client_socket_encode, client_socket_decode },
2066 #ifdef WITH_ACCOUNTING
2068 { RLM_MODULE_INIT, "acct", sizeof(listen_socket_t), NULL,
2069 common_socket_parse, common_socket_free,
2070 acct_socket_recv, acct_socket_send,
2071 common_socket_print, client_socket_encode, client_socket_decode},
2076 { RLM_MODULE_INIT, "detail", sizeof(listen_detail_t), NULL,
2077 detail_parse, detail_free,
2078 detail_recv, detail_send,
2079 detail_print, detail_encode, detail_decode },
2083 /* vlan query protocol */
2084 { 0, "vmps", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
2088 /* dhcp query protocol */
2089 { 0, "dhcp", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
2092 #ifdef WITH_COMMAND_SOCKET
2093 /* TCP command socket */
2094 { RLM_MODULE_INIT, "control", sizeof(fr_command_socket_t), NULL,
2095 command_socket_parse, command_socket_free,
2096 command_domain_accept, command_domain_send,
2097 command_socket_print, command_socket_encode, command_socket_decode },
2101 /* Change of Authorization */
2102 { RLM_MODULE_INIT, "coa", sizeof(listen_socket_t), NULL,
2103 common_socket_parse, NULL,
2104 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
2105 common_socket_print, client_socket_encode, client_socket_decode },
2113 * Binds a listener to a socket.
2115 static int listen_bind(rad_listen_t *this)
2118 struct sockaddr_storage salocal;
2120 listen_socket_t *sock = this->data;
2122 #define proto_for_port "udp"
2123 #define sock_type SOCK_DGRAM
2125 char const *proto_for_port = "udp";
2126 int sock_type = SOCK_DGRAM;
2128 if (sock->proto == IPPROTO_TCP) {
2130 if (this->type == RAD_LISTEN_VQP) {
2131 ERROR("VQP does not support TCP transport");
2136 proto_for_port = "tcp";
2137 sock_type = SOCK_STREAM;
2142 * If the port is zero, then it means the appropriate
2143 * thing from /etc/services.
2145 if (sock->my_port == 0) {
2146 struct servent *svp;
2148 switch (this->type) {
2149 case RAD_LISTEN_AUTH:
2150 svp = getservbyname ("radius", proto_for_port);
2152 sock->my_port = ntohs(svp->s_port);
2154 sock->my_port = PW_AUTH_UDP_PORT;
2158 #ifdef WITH_ACCOUNTING
2159 case RAD_LISTEN_ACCT:
2160 svp = getservbyname ("radacct", proto_for_port);
2162 sock->my_port = ntohs(svp->s_port);
2164 sock->my_port = PW_ACCT_UDP_PORT;
2170 case RAD_LISTEN_PROXY:
2171 /* leave it at zero */
2176 case RAD_LISTEN_VQP:
2177 sock->my_port = 1589;
2181 #ifdef WITH_COMMAND_SOCKET
2182 case RAD_LISTEN_COMMAND:
2183 sock->my_port = PW_RADMIN_PORT;
2188 case RAD_LISTEN_COA:
2189 svp = getservbyname ("radius-dynauth", "udp");
2191 sock->my_port = ntohs(svp->s_port);
2193 sock->my_port = PW_COA_UDP_PORT;
2199 WARN("Internal sanity check failed in binding to socket. Ignoring problem");
2205 * Don't open sockets if we're checking the config.
2213 * Copy fr_socket() here, as we may need to bind to a device.
2215 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
2219 this->print(this, buffer, sizeof(buffer));
2221 ERROR("Failed opening %s: %s", buffer, fr_syserror(errno));
2227 * We don't want child processes inheriting these
2230 rcode = fcntl(this->fd, F_GETFD);
2232 if (fcntl(this->fd, F_SETFD, rcode | FD_CLOEXEC) < 0) {
2234 ERROR("Failed setting close on exec: %s", fr_syserror(errno));
2241 * Bind to a device BEFORE touching IP addresses.
2243 if (sock->interface) {
2244 #ifdef SO_BINDTODEVICE
2247 memset(&ifreq, 0, sizeof(ifreq));
2248 strlcpy(ifreq.ifr_name, sock->interface, sizeof(ifreq.ifr_name));
2251 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
2252 (char *)&ifreq, sizeof(ifreq));
2256 ERROR("Failed binding to interface %s: %s",
2257 sock->interface, fr_syserror(errno));
2259 } /* else it worked. */
2261 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2262 #ifdef HAVE_NET_IF_H
2264 * Odds are that any system supporting "bind to
2265 * device" also supports IPv6, so this next bit
2266 * isn't necessary. But it's here for
2269 * If we're doing IPv6, and the scope hasn't yet
2270 * been defined, set the scope to the scope of
2273 if (sock->my_ipaddr.af == AF_INET6) {
2274 if (sock->my_ipaddr.scope == 0) {
2275 sock->my_ipaddr.scope = if_nametoindex(sock->interface);
2276 if (sock->my_ipaddr.scope == 0) {
2278 ERROR("Failed finding interface %s: %s",
2279 sock->interface, fr_syserror(errno));
2282 } /* else scope was defined: we're OK. */
2287 * IPv4: no link local addresses,
2288 * and no bind to device.
2292 ERROR("Failed binding to interface %s: \"bind to device\" is unsupported", sock->interface);
2299 if (sock->proto == IPPROTO_TCP) {
2302 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2304 ERROR("Failed to reuse address: %s", fr_syserror(errno));
2310 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
2311 else /* UDP sockets get UDPfromto */
2314 #ifdef WITH_UDPFROMTO
2316 * Initialize udpfromto for all sockets.
2318 if (udpfromto_init(this->fd) != 0) {
2319 ERROR("Failed initializing udpfromto: %s",
2320 fr_syserror(errno));
2327 * Set up sockaddr stuff.
2329 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
2334 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2335 if (sock->my_ipaddr.af == AF_INET6) {
2337 * Listening on '::' does NOT get you IPv4 to
2338 * IPv6 mapping. You've got to listen on an IPv4
2339 * address, too. This makes the rest of the server
2340 * design a little simpler.
2344 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2347 if (setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2348 (char *)&on, sizeof(on)) < 0) {
2349 ERROR("Failed setting socket to IPv6 "
2350 "only: %s", fr_syserror(errno));
2356 #endif /* IPV6_V6ONLY */
2358 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2360 if (sock->my_ipaddr.af == AF_INET) {
2363 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2365 * Disable PMTU discovery. On Linux, this
2366 * also makes sure that the "don't fragment"
2369 flag = IP_PMTUDISC_DONT;
2370 if (setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2371 &flag, sizeof(flag)) < 0) {
2372 ERROR("Failed disabling PMTU discovery: %s",
2373 fr_syserror(errno));
2380 #if defined(IP_DONTFRAG)
2382 * Ensure that the "don't fragment" flag is zero.
2385 if (setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2386 &flag, sizeof(flag)) < 0) {
2387 ERROR("Failed setting don't fragment flag: %s",
2388 fr_syserror(errno));
2398 if (sock->broadcast) {
2401 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2402 ERROR("Can't set broadcast option: %s",
2403 fr_syserror(errno));
2411 * May be binding to priviledged ports.
2413 if (sock->my_port != 0) {
2415 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2421 this->print(this, buffer, sizeof(buffer));
2422 ERROR("Failed binding to %s: %s\n",
2423 buffer, fr_syserror(errno));
2428 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2429 * kernel instead binds us to a 1.2.3.4. If this
2430 * happens, notice, and remember our real IP.
2433 struct sockaddr_storage src;
2434 socklen_t sizeof_src = sizeof(src);
2436 memset(&src, 0, sizeof_src);
2437 if (getsockname(this->fd, (struct sockaddr *) &src,
2439 ERROR("Failed getting socket name: %s",
2440 fr_syserror(errno));
2444 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2445 &sock->my_ipaddr, &sock->my_port)) {
2446 ERROR("Socket has unsupported address family");
2453 if (sock->proto == IPPROTO_TCP) {
2455 * If there are hard-coded worker threads, OR
2456 * it's a TLS connection, it's blocking.
2458 * Otherwise, they're non-blocking.
2463 && (this->type == RAD_LISTEN_PROXY) && !this->tls
2467 if (fr_nonblock(this->fd) < 0) {
2469 ERROR("Failed setting non-blocking on socket: %s",
2470 fr_syserror(errno));
2476 * Allow a backlog of 8 listeners, but only for incoming interfaces.
2479 if (this->type != RAD_LISTEN_PROXY)
2481 if (listen(this->fd, 8) < 0) {
2483 ERROR("Failed in listen(): %s", fr_syserror(errno));
2490 * Mostly for proxy sockets.
2492 sock->other_ipaddr.af = sock->my_ipaddr.af;
2495 * Don't screw up other people.
2497 #undef proto_for_port
2504 static int _listener_free(rad_listen_t *this)
2507 * Other code may have eaten the FD.
2509 if (this->fd >= 0) close(this->fd);
2511 if (master_listen[this->type].free) {
2512 master_listen[this->type].free(this);
2516 if ((this->type == RAD_LISTEN_AUTH)
2518 || (this->type == RAD_LISTEN_ACCT)
2521 || (this->type == RAD_LISTEN_PROXY)
2523 #ifdef WITH_COMMAND_SOCKET
2524 || ((this->type == RAD_LISTEN_COMMAND) &&
2525 (((fr_command_socket_t *) this->data)->magic != COMMAND_SOCKET_MAGIC))
2528 listen_socket_t *sock = this->data;
2530 rad_free(&sock->packet);
2532 rad_assert(sock->ev == NULL);
2536 * Note that we do NOT free this->tls, as the
2537 * pointer is parented by its CONF_SECTION. It
2538 * may be used by multiple listeners.
2541 TALLOC_FREE(sock->ssn);
2542 TALLOC_FREE(sock->request);
2543 #ifdef HAVE_PTHREAD_H
2544 pthread_mutex_destroy(&(sock->mutex));
2547 #endif /* WITH_TLS */
2549 #endif /* WITH_TCP */
2555 * Allocate & initialize a new listener.
2557 static rad_listen_t *listen_alloc(TALLOC_CTX *ctx, RAD_LISTEN_TYPE type)
2561 this = talloc_zero(ctx, rad_listen_t);
2564 this->recv = master_listen[this->type].recv;
2565 this->send = master_listen[this->type].send;
2566 this->print = master_listen[this->type].print;
2567 this->encode = master_listen[this->type].encode;
2568 this->decode = master_listen[this->type].decode;
2570 talloc_set_destructor(this, _listener_free);
2572 this->data = talloc_zero_array(this, uint8_t, master_listen[this->type].inst_size);
2579 * Externally visible function for creating a new proxy LISTENER.
2581 * Not thread-safe, but all calls to it are protected by the
2582 * proxy mutex in event.c
2584 rad_listen_t *proxy_new_listener(home_server_t *home, uint16_t src_port)
2588 listen_socket_t *sock;
2591 if (!home) return NULL;
2593 rad_assert(home->server == NULL); /* we only open real sockets */
2595 if ((home->limit.max_connections > 0) &&
2596 (home->limit.num_connections >= home->limit.max_connections)) {
2597 RATE_LIMIT(INFO("Home server %s has too many open connections (%d)",
2598 home->name, home->limit.max_connections));
2603 if (home->last_failed_open == now) {
2604 WARN("Suppressing attempt to open socket to 'down' home server");
2608 this = listen_alloc(main_config.config, RAD_LISTEN_PROXY);
2611 sock->other_ipaddr = home->ipaddr;
2612 sock->other_port = home->port;
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;
2752 cp = cf_pair_find(cs, "type");
2755 "No type specified in listen section");
2759 value = cf_pair_value(cp);
2762 "Type cannot be empty");
2766 snprintf(buffer, sizeof(buffer), "proto_%s", value);
2767 handle = lt_dlopenext(buffer);
2769 fr_protocol_t *proto;
2770 lt_dlhandle *marker;
2772 proto = dlsym(handle, buffer);
2775 "Failed linking to protocol %s : %s\n",
2781 type = fr_str2int(listen_compare, value, -1);
2782 rad_assert(type >= 0); /* shouldn't be able to compile an invalid type */
2784 memcpy(&master_listen[type], proto, sizeof(*proto));
2787 * Ensure handle gets closed if config section gets freed
2789 marker = talloc(cs, lt_dlhandle);
2791 talloc_set_destructor(marker, _free_proto_handle);
2793 if (master_listen[type].magic != RLM_MODULE_INIT) {
2794 ERROR("Failed to load protocol '%s' due to internal sanity check problem",
2795 master_listen[type].name);
2800 cf_log_info(cs, "listen {");
2803 rcode = cf_item_parse(cs, "type", FR_ITEM_POINTER(PW_TYPE_STRING, &listen_type), "");
2804 if (rcode < 0) return NULL;
2807 "No type specified in listen section");
2811 type = fr_str2int(listen_compare, listen_type, -1);
2814 "Invalid type \"%s\" in listen section.",
2820 * Allow listen sections in the default config to
2821 * refer to a server.
2824 rcode = cf_item_parse(cs, "virtual_server", FR_ITEM_POINTER(PW_TYPE_STRING, &server), NULL);
2825 if (rcode < 0) return NULL;
2830 * We were passed a virtual server, so the caller is
2831 * defining a proxy listener inside of a virtual server.
2832 * This isn't allowed right now.
2834 else if (type == RAD_LISTEN_PROXY) {
2835 ERROR("Error: listen type \"proxy\" Cannot appear in a virtual server section");
2841 * Set up cross-type data.
2843 this = listen_alloc(cs, type);
2844 this->server = server;
2849 * Special-case '+' for "auth+acct".
2851 if (strchr(listen_type, '+') != NULL) {
2857 * Call per-type parser.
2859 if (master_listen[type].parse(cs, this) < 0) {
2864 cf_log_info(cs, "}");
2869 #ifdef HAVE_PTHREAD_H
2871 * A child thread which does NOTHING other than read and process
2874 static void *recv_thread(void *arg)
2876 rad_listen_t *this = arg;
2889 * Generate a list of listeners. Takes an input list of
2890 * listeners, too, so we don't close sockets with waiting packets.
2892 int listen_init(CONF_SECTION *config, rad_listen_t **head,
2896 UNUSED bool spawn_flag
2901 bool override = false;
2902 CONF_SECTION *cs = NULL;
2903 rad_listen_t **last;
2905 fr_ipaddr_t server_ipaddr;
2906 uint16_t auth_port = 0;
2908 bool defined_proxy = false;
2912 * We shouldn't be called with a pre-existing list.
2914 rad_assert(head && (*head == NULL));
2916 memset(&server_ipaddr, 0, sizeof(server_ipaddr));
2919 server_ipaddr.af = AF_UNSPEC;
2922 * If the port is specified on the command-line,
2923 * it over-rides the configuration file.
2925 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
2927 if (main_config.port > 0) {
2928 auth_port = main_config.port;
2931 * -p X but no -i Y on the command-line.
2933 if (main_config.myip.af == AF_UNSPEC) {
2934 ERROR("The command-line says \"-p %d\", but there is no associated IP address to use",
2941 * If the IP address was configured on the command-line,
2942 * use that as the "bind_address"
2944 if (main_config.myip.af != AF_UNSPEC) {
2945 listen_socket_t *sock;
2947 memcpy(&server_ipaddr, &main_config.myip,
2948 sizeof(server_ipaddr));
2952 if (strcmp(progname, "vmpsd") == 0) {
2953 this = listen_alloc(config, RAD_LISTEN_VQP);
2954 if (!auth_port) auth_port = 1589;
2957 this = listen_alloc(config, RAD_LISTEN_AUTH);
2961 sock->my_ipaddr = server_ipaddr;
2962 sock->my_port = auth_port;
2964 sock->clients = clients_parse_section(config, false);
2965 if (!sock->clients) {
2966 cf_log_err_cs(config,
2967 "Failed to find any clients for this listen section");
2972 if (listen_bind(this) < 0) {
2974 ERROR("There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
2978 auth_port = sock->my_port; /* may have been updated in listen_bind */
2980 cs = cf_section_sub_find_name2(config, "server",
2982 if (cs) this->server = main_config.name;
2986 last = &(this->next);
2992 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
2995 #ifdef WITH_ACCOUNTING
2997 * Open Accounting Socket.
2999 * If we haven't already gotten acct_port from
3000 * /etc/services, then make it auth_port + 1.
3002 this = listen_alloc(config, RAD_LISTEN_ACCT);
3006 * Create the accounting socket.
3008 * The accounting port is always the
3009 * authentication port + 1
3011 sock->my_ipaddr = server_ipaddr;
3012 sock->my_port = auth_port + 1;
3014 sock->clients = clients_parse_section(config, false);
3015 if (!sock->clients) {
3016 cf_log_err_cs(config,
3017 "Failed to find any clients for this listen section");
3021 if (listen_bind(this) < 0) {
3024 ERROR("There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
3029 cs = cf_section_sub_find_name2(config, "server",
3031 if (cs) this->server = main_config.name;
3035 last = &(this->next);
3040 * They specified an IP on the command-line, ignore
3041 * all listen sections except the one in '-n'.
3043 if (main_config.myip.af != AF_UNSPEC) {
3044 CONF_SECTION *subcs;
3045 char const *name2 = cf_section_name2(cs);
3047 cs = cf_section_sub_find_name2(config, "server",
3049 if (!cs) goto add_sockets;
3052 * Should really abstract this code...
3054 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
3056 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
3057 this = listen_parse(subcs, name2);
3064 last = &(this->next);
3065 } /* loop over "listen" directives in server <foo> */
3071 * Walk through the "listen" sections, if they exist.
3073 for (cs = cf_subsection_find_next(config, NULL, "listen");
3075 cs = cf_subsection_find_next(config, cs, "listen")) {
3076 this = listen_parse(cs, NULL);
3083 last = &(this->next);
3087 * Check virtual servers for "listen" sections, too.
3089 * FIXME: Move to virtual server init?
3091 for (cs = cf_subsection_find_next(config, NULL, "server");
3093 cs = cf_subsection_find_next(config, cs, "server")) {
3094 CONF_SECTION *subcs;
3095 char const *name2 = cf_section_name2(cs);
3097 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
3099 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
3100 this = listen_parse(subcs, name2);
3107 last = &(this->next);
3108 } /* loop over "listen" directives in virtual servers */
3109 } /* loop over virtual servers */
3113 * No sockets to receive packets, this is an error.
3114 * proxying is pointless.
3117 ERROR("The server is not configured to listen on any ports. Cannot start");
3122 * Print out which sockets we're listening on, and
3123 * add them to the event list.
3125 for (this = *head; this != NULL; this = this->next) {
3127 if (this->type == RAD_LISTEN_PROXY) {
3128 defined_proxy = true;
3134 if (!check_config && !spawn_flag && this->tls) {
3135 cf_log_err_cs(this->cs, "Threading must be enabled for TLS sockets to function properly");
3136 cf_log_err_cs(this->cs, "You probably need to do '%s -fxx -l stdout' for debugging",
3141 if (!check_config) {
3142 if (this->workers && !spawn_flag) {
3143 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
3147 if (this->workers) {
3148 #ifdef HAVE_PTHREAD_H
3153 this->print(this, buffer, sizeof(buffer));
3155 for (i = 0; i < this->workers; i++) {
3159 * FIXME: create detached?
3161 rcode = pthread_create(&id, 0, recv_thread, this);
3163 ERROR("Thread create failed: %s",
3164 fr_syserror(rcode));
3168 DEBUG("Thread %d for %s\n", i, buffer);
3171 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
3176 radius_update_listener(this);
3183 * If we're proxying requests, open the proxy FD.
3184 * Otherwise, don't do anything.
3187 if ((main_config.proxy_requests == true) &&
3189 (*head != NULL) && !defined_proxy) {
3193 memset(&home, 0, sizeof(home));
3198 home.proto = IPPROTO_UDP;
3199 home.src_ipaddr = server_ipaddr;
3203 * Address is still unspecified, use IPv4.
3205 if (home.src_ipaddr.af == AF_UNSPEC) {
3206 home.src_ipaddr.af = AF_INET;
3207 /* everything else is already set to zero */
3210 home.ipaddr.af = home.src_ipaddr.af;
3211 /* everything else is already set to zero */
3213 this = proxy_new_listener(&home, port);
3219 radius_update_listener(this);
3224 * Haven't defined any sockets. Die.
3226 if (!*head) return -1;
3228 xlat_register("listen", xlat_listen, NULL, NULL);
3234 * Free a linked list of listeners;
3236 void listen_free(rad_listen_t **head)
3240 if (!head || !*head) return;
3244 rad_listen_t *next = this->next;
3253 RADCLIENT_LIST *listener_find_client_list(fr_ipaddr_t const *ipaddr, uint16_t port)
3257 for (this = main_config.listen; this != NULL; this = this->next) {
3258 listen_socket_t *sock;
3260 if ((this->type != RAD_LISTEN_AUTH)
3261 #ifdef WITH_ACCOUNTING
3262 && (this->type != RAD_LISTEN_ACCT)
3268 if ((sock->my_port == port) &&
3269 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
3270 return sock->clients;
3278 rad_listen_t *listener_find_byipaddr(fr_ipaddr_t const *ipaddr, uint16_t port, int proto)
3282 for (this = main_config.listen; this != NULL; this = this->next) {
3283 listen_socket_t *sock;
3287 if (sock->my_port != port) continue;
3288 if (sock->proto != proto) continue;
3289 if (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) != 0) continue;
3295 * Failed to find a specific one. Find INADDR_ANY
3297 for (this = main_config.listen; this != NULL; this = this->next) {
3298 listen_socket_t *sock;
3302 if (sock->my_port != port) continue;
3303 if (sock->proto != proto) continue;
3304 if (!fr_inaddr_any(&sock->my_ipaddr)) continue;