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 || !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, int 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 request_free(&request);
250 request->reply = rad_alloc_reply(request, request->packet);
251 if (!request->reply) {
252 request_free(&request);
255 gettimeofday(&request->packet->timestamp, NULL);
257 request->priority = listener->type;
258 request->server = client->client_server;
259 request->root = &mainconfig;
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 request_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 request_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_AUTHENTICATION_ACK;
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_AUTHENTICATION_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, 0);
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 event_new_fd(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_AUTHENTICATION_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 (!mainconfig.status_server) {
505 FR_STATS_INC(auth, total_unknown_types);
506 WDEBUG("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)
538 struct sockaddr_storage src;
539 listen_socket_t *sock;
540 fr_ipaddr_t src_ipaddr;
541 RADCLIENT *client = NULL;
545 DEBUG2(" ... new connection request on TCP socket.");
547 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
550 * Non-blocking sockets must handle this.
553 if (errno == EWOULDBLOCK) {
558 DEBUG2(" ... failed to accept connection.");
562 if (!fr_sockaddr2ipaddr(&src, salen, &src_ipaddr, &src_port)) {
564 DEBUG2(" ... unknown address family.");
569 * Enforce client IP address checks on accept, not on
572 if ((client = client_listener_find(listener,
573 &src_ipaddr, src_port)) == NULL) {
575 FR_STATS_INC(auth, total_invalid_requests);
581 * Enforce security restrictions.
583 * This shouldn't be necessary in practice. However, it
584 * serves as a double-check on configurations. Marking a
585 * client as "tls required" means that any accidental
586 * exposure of the client to non-TLS traffic is
589 if (client->tls_required && !listener->tls) {
590 INFO("Ignoring connection to TLS socket from non-TLS client");
597 * Enforce max_connections on client && listen section.
599 if ((client->limit.max_connections != 0) &&
600 (client->limit.max_connections == client->limit.num_connections)) {
602 * FIXME: Print client IP/port, and server IP/port.
604 INFO("Ignoring new connection due to client max_connections (%d)", client->limit.max_connections);
609 sock = listener->data;
610 if ((sock->limit.max_connections != 0) &&
611 (sock->limit.max_connections == sock->limit.num_connections)) {
613 * FIXME: Print client IP/port, and server IP/port.
615 INFO("Ignoring new connection due to socket max_connections");
619 client->limit.num_connections++;
620 sock->limit.num_connections++;
623 * Add the new listener.
625 this = listen_alloc(listener, listener->type);
626 if (!this) return -1;
629 * Copy everything, including the pointer to the socket
633 memcpy(this->data, listener->data, sizeof(*sock));
634 memcpy(this, listener, sizeof(*this));
636 this->data = sock; /* fix it back */
638 sock->parent = listener->data;
639 sock->other_ipaddr = src_ipaddr;
640 sock->other_port = src_port;
641 sock->client = client;
642 sock->opened = sock->last_packet = time(NULL);
645 * Set the limits. The defaults are the parent limits.
646 * Client limits on max_connections are enforced dynamically.
647 * Set the MINIMUM of client/socket idle timeout or lifetime.
649 memcpy(&sock->limit, &sock->parent->limit, sizeof(sock->limit));
651 if (client->limit.idle_timeout &&
652 ((sock->limit.idle_timeout == 0) ||
653 (client->limit.idle_timeout < sock->limit.idle_timeout))) {
654 sock->limit.idle_timeout = client->limit.idle_timeout;
657 if (client->limit.lifetime &&
658 ((sock->limit.lifetime == 0) ||
659 (client->limit.lifetime < sock->limit.lifetime))) {
660 sock->limit.lifetime = client->limit.lifetime;
664 this->status = RAD_LISTEN_STATUS_INIT;
667 #ifdef WITH_COMMAND_SOCKET
668 if (this->type == RAD_LISTEN_COMMAND) {
669 this->recv = command_tcp_recv;
670 this->send = command_tcp_send;
671 command_write_magic(this->fd, sock);
676 this->recv = dual_tcp_recv;
680 this->recv = dual_tls_recv;
681 this->send = dual_tls_send;
687 * FIXME: set O_NONBLOCK on the accept'd fd.
688 * See djb's portability rants for details.
692 * Tell the event loop that we have a new FD.
693 * This can be called from a child thread...
702 * Ensure that we always keep the correct counters.
705 static void common_socket_free(rad_listen_t *this)
707 listen_socket_t *sock = this->data;
709 if (sock->proto != IPPROTO_TCP) return;
711 if (!sock->parent) return;
714 * Decrement the number of connections.
716 if (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", PW_TYPE_BOOLEAN,
869 offsetof(rad_listen_t, nodup), NULL, NULL },
871 { "synchronous", PW_TYPE_BOOLEAN,
872 offsetof(rad_listen_t, synchronous), NULL, NULL },
874 { "workers", PW_TYPE_INTEGER,
875 offsetof(rad_listen_t, workers), NULL, NULL },
877 { NULL, -1, 0, NULL, NULL } /* end the list */
881 static CONF_PARSER limit_config[] = {
882 { "max_pps", PW_TYPE_INTEGER,
883 offsetof(listen_socket_t, max_rate), NULL, NULL },
886 { "max_connections", PW_TYPE_INTEGER,
887 offsetof(listen_socket_t, limit.max_connections), NULL, "16" },
889 { "lifetime", PW_TYPE_INTEGER,
890 offsetof(listen_socket_t, limit.lifetime), NULL, "0" },
892 { "idle_timeout", PW_TYPE_INTEGER,
893 offsetof(listen_socket_t, limit.idle_timeout), NULL, "30" },
896 { NULL, -1, 0, NULL, NULL } /* end the list */
900 * Parse an authentication or accounting socket.
902 int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
907 listen_socket_t *sock = this->data;
908 char *section_name = NULL;
909 CONF_SECTION *client_cs, *parentcs;
917 memset(&ipaddr, 0, sizeof(ipaddr));
918 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
919 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
920 &ipaddr.ipaddr.ip4addr, NULL);
921 if (rcode < 0) return -1;
923 if (rcode == 0) { /* successfully parsed IPv4 */
926 } else { /* maybe IPv6? */
927 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
928 &ipaddr.ipaddr.ip6addr, NULL);
929 if (rcode < 0) return -1;
933 "No address specified in listen section");
936 ipaddr.af = AF_INET6;
939 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
941 if (rcode < 0) return -1;
943 if ((listen_port < 0) || (listen_port > 65535)) {
945 "Invalid value for \"port\"");
949 sock->proto = IPPROTO_UDP;
951 if (cf_pair_find(cs, "proto")) {
954 "System does not support the TCP protocol. Delete this line from the configuration file.");
962 rcode = cf_item_parse(cs, "proto", PW_TYPE_STRING_PTR,
964 if (rcode < 0) return -1;
966 if (strcmp(proto, "udp") == 0) {
967 sock->proto = IPPROTO_UDP;
969 } else if (strcmp(proto, "tcp") == 0) {
970 sock->proto = IPPROTO_TCP;
973 "Unknown proto name \"%s\"", proto);
978 * TCP requires a destination IP for sockets.
979 * UDP doesn't, so it's allowed.
982 if ((this->type == RAD_LISTEN_PROXY) &&
983 (sock->proto != IPPROTO_UDP)) {
985 "Proxy listeners can only listen on proto = udp");
988 #endif /* WITH_PROXY */
991 tls = cf_section_sub_find(cs, "tls");
995 * Don't allow TLS configurations for UDP sockets.
997 if (sock->proto != IPPROTO_TCP) {
999 "TLS transport is not available for UDP sockets.");
1004 * If unset, set to default.
1006 if (listen_port == 0) listen_port = PW_RADIUS_TLS_PORT;
1008 this->tls = tls_server_conf_parse(tls);
1013 #ifdef HAVE_PTRHEAD_H
1014 if (pthread_mutex_init(&sock->mutex, NULL) < 0) {
1022 #else /* WITH_TLS */
1024 * Built without TLS. Disallow it.
1026 if (cf_section_sub_find(cs, "tls")) {
1028 "TLS transport is not available in this executable.");
1031 #endif /* WITH_TLS */
1033 #endif /* WITH_TCP */
1036 * No "proto" field. Disallow TLS.
1038 } else if (cf_section_sub_find(cs, "tls")) {
1040 "TLS transport is not available in this \"listen\" section.");
1045 * Magical tuning methods!
1047 subcs = cf_section_sub_find(cs, "performance");
1049 rcode = cf_section_parse(subcs, this,
1050 performance_config);
1051 if (rcode < 0) return -1;
1053 if (this->synchronous && sock->max_rate) {
1054 WARN("Setting 'max_pps' is incompatible with 'synchronous'. Disabling 'max_pps'");
1058 if (!this->synchronous && this->workers) {
1059 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
1064 subcs = cf_section_sub_find(cs, "limit");
1066 rcode = cf_section_parse(subcs, sock,
1068 if (rcode < 0) return -1;
1070 if (sock->max_rate && ((sock->max_rate < 10) || (sock->max_rate > 1000000))) {
1072 "Invalid value for \"max_pps\"");
1077 if ((sock->limit.idle_timeout > 0) && (sock->limit.idle_timeout < 5)) {
1078 WARN("Setting idle_timeout to 5");
1079 sock->limit.idle_timeout = 5;
1082 if ((sock->limit.lifetime > 0) && (sock->limit.lifetime < 5)) {
1083 WARN("Setting lifetime to 5");
1084 sock->limit.lifetime = 5;
1087 if ((sock->limit.lifetime > 0) && (sock->limit.idle_timeout > sock->limit.lifetime)) {
1088 WARN("Setting idle_timeout to 0");
1089 sock->limit.idle_timeout = 0;
1093 * Force no duplicate detection for TCP sockets.
1095 if (sock->proto == IPPROTO_TCP) {
1100 sock->limit.max_connections = 60;
1101 sock->limit.idle_timeout = 30;
1102 sock->limit.lifetime = 0;
1106 sock->my_ipaddr = ipaddr;
1107 sock->my_port = listen_port;
1112 * Until there is a side effects free way of forwarding a
1113 * request to another virtual server, this check is invalid,
1114 * and should be left disabled.
1117 if (home_server_find(&sock->my_ipaddr, sock->my_port, sock->proto)) {
1120 EDEBUG("We have been asked to listen on %s port %d, which is also listed as a "
1121 "home server. This can create a proxy loop",
1122 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)), sock->my_port);
1126 return 0; /* don't do anything */
1131 * If we can bind to interfaces, do so,
1134 if (cf_pair_find(cs, "interface")) {
1136 CONF_PAIR *cp = cf_pair_find(cs, "interface");
1138 rad_assert(cp != NULL);
1139 value = cf_pair_value(cp);
1142 "No interface name given");
1145 sock->interface = value;
1150 * If we can do broadcasts..
1152 if (cf_pair_find(cs, "broadcast")) {
1153 #ifndef SO_BROADCAST
1155 "System does not support broadcast sockets. Delete this line from the configuration file.");
1159 CONF_PAIR *cp = cf_pair_find(cs, "broadcast");
1161 if (this->type != RAD_LISTEN_DHCP) {
1163 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file.");
1167 rad_assert(cp != NULL);
1168 value = cf_pair_value(cp);
1171 "No broadcast value given");
1176 * Hack... whatever happened to cf_section_parse?
1178 sock->broadcast = (strcmp(value, "yes") == 0);
1184 * And bind it to the port.
1186 if (listen_bind(this) < 0) {
1189 "Error binding to port for %s port %d",
1190 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
1197 * Proxy sockets don't have clients.
1199 if (this->type == RAD_LISTEN_PROXY) return 0;
1203 * The more specific configurations are preferred to more
1207 parentcs = cf_top_section(cs);
1208 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
1209 §ion_name, NULL);
1210 if (rcode < 0) return -1; /* bad string */
1213 * Explicit list given: use it.
1215 client_cs = cf_section_sub_find_name2(parentcs,
1219 client_cs = cf_section_find(section_name);
1223 "Failed to find clients %s {...}",
1227 } /* else there was no "clients = " entry. */
1230 CONF_SECTION *server_cs;
1232 server_cs = cf_section_sub_find_name2(parentcs,
1236 * Found a "server foo" section. If there are clients
1240 (cf_section_sub_find(server_cs, "client") != NULL)) {
1241 client_cs = server_cs;
1246 * Still nothing. Look for global clients.
1248 if (!client_cs) client_cs = parentcs;
1251 sock->clients = clients_parse_section(client_cs, (this->tls != NULL));
1253 sock->clients = clients_parse_section(client_cs, false);
1255 if (!sock->clients) {
1257 "Failed to load clients for this listen section");
1262 if (sock->proto == IPPROTO_TCP) {
1264 * Re-write the listener receive function to
1265 * allow us to accept the socket.
1267 this->recv = dual_tcp_accept;
1275 * Send an authentication response packet
1277 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1279 rad_assert(request->listener == listener);
1280 rad_assert(listener->send == auth_socket_send);
1282 #ifdef WITH_UDPFROMTO
1284 * Overwrite the src ip address on the outbound packet
1285 * with the one specified by the client.
1286 * This is useful to work around broken DSR implementations
1287 * and other routing issues.
1289 if (request->client->src_ipaddr.af != AF_UNSPEC) {
1290 request->reply->src_ipaddr = request->client->src_ipaddr;
1294 if (rad_send(request->reply, request->packet,
1295 request->client->secret) < 0) {
1296 RERROR("Failed sending reply: %s",
1305 #ifdef WITH_ACCOUNTING
1307 * Send an accounting response packet (or not)
1309 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1311 rad_assert(request->listener == listener);
1312 rad_assert(listener->send == acct_socket_send);
1315 * Accounting reject's are silently dropped.
1317 * We do it here to avoid polluting the rest of the
1318 * code with this knowledge
1320 if (request->reply->code == 0) return 0;
1322 #ifdef WITH_UDPFROMTO
1324 * Overwrite the src ip address on the outbound packet
1325 * with the one specified by the client.
1326 * This is useful to work around broken DSR implementations
1327 * and other routing issues.
1329 if (request->client->src_ipaddr.af != AF_UNSPEC) {
1330 request->reply->src_ipaddr = request->client->src_ipaddr;
1334 if (rad_send(request->reply, request->packet,
1335 request->client->secret) < 0) {
1336 RERROR("Failed sending reply: %s",
1347 * Send a packet to a home server.
1349 * FIXME: have different code for proxy auth & acct!
1351 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1353 rad_assert(request->proxy_listener == listener);
1354 rad_assert(listener->send == proxy_socket_send);
1356 if (rad_send(request->proxy, NULL,
1357 request->home_server->secret) < 0) {
1358 RERROR("Failed sending proxied request: %s",
1369 * Check if an incoming request is "ok"
1371 * It takes packets, not requests. It sees if the packet looks
1372 * OK. If so, it does a number of sanity checks on it.
1374 static int stats_socket_recv(rad_listen_t *listener)
1378 RADIUS_PACKET *packet;
1379 RADCLIENT *client = NULL;
1380 fr_ipaddr_t src_ipaddr;
1382 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1383 if (rcode < 0) return 0;
1385 FR_STATS_INC(auth, total_requests);
1387 if (rcode < 20) { /* AUTH_HDR_LEN */
1388 FR_STATS_INC(auth, total_malformed_requests);
1392 if ((client = client_listener_find(listener,
1393 &src_ipaddr, src_port)) == NULL) {
1394 rad_recv_discard(listener->fd);
1395 FR_STATS_INC(auth, total_invalid_requests);
1399 FR_STATS_TYPE_INC(client->auth.total_requests);
1402 * We only understand Status-Server on this socket.
1404 if (code != PW_CODE_STATUS_SERVER) {
1405 DEBUG("Ignoring packet code %d sent to Status-Server port",
1407 rad_recv_discard(listener->fd);
1408 FR_STATS_INC(auth, total_unknown_types);
1413 * Now that we've sanity checked everything, receive the
1416 packet = rad_recv(listener->fd, 1); /* require message authenticator */
1418 FR_STATS_INC(auth, total_malformed_requests);
1419 DEBUG("%s", fr_strerror());
1423 if (!request_receive(listener, packet, client, rad_status_server)) {
1424 FR_STATS_INC(auth, total_packets_dropped);
1435 * Check if an incoming request is "ok"
1437 * It takes packets, not requests. It sees if the packet looks
1438 * OK. If so, it does a number of sanity checks on it.
1440 static int auth_socket_recv(rad_listen_t *listener)
1444 RADIUS_PACKET *packet;
1445 RAD_REQUEST_FUNP fun = NULL;
1446 RADCLIENT *client = NULL;
1447 fr_ipaddr_t src_ipaddr;
1449 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1450 if (rcode < 0) return 0;
1452 FR_STATS_INC(auth, total_requests);
1454 if (rcode < 20) { /* AUTH_HDR_LEN */
1455 FR_STATS_INC(auth, total_malformed_requests);
1459 if ((client = client_listener_find(listener,
1460 &src_ipaddr, src_port)) == NULL) {
1461 rad_recv_discard(listener->fd);
1462 FR_STATS_INC(auth, total_invalid_requests);
1466 FR_STATS_TYPE_INC(client->auth.total_requests);
1469 * Some sanity checks, based on the packet code.
1472 case PW_CODE_AUTHENTICATION_REQUEST:
1473 fun = rad_authenticate;
1476 case PW_CODE_STATUS_SERVER:
1477 if (!mainconfig.status_server) {
1478 rad_recv_discard(listener->fd);
1479 FR_STATS_INC(auth, total_unknown_types);
1480 WDEBUG("Ignoring Status-Server request due to security configuration");
1483 fun = rad_status_server;
1487 rad_recv_discard(listener->fd);
1488 FR_STATS_INC(auth,total_unknown_types);
1490 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
1491 code, client->shortname, src_port);
1494 } /* switch over packet types */
1497 * Now that we've sanity checked everything, receive the
1500 packet = rad_recv(listener->fd, client->message_authenticator);
1502 FR_STATS_INC(auth, total_malformed_requests);
1503 DEBUG("%s", fr_strerror());
1508 #ifdef WITH_UDPFROMTO
1510 * This is a NICE Mac OSX bug. Create an interface with
1511 * two IP address, and then configure one listener for
1512 * each IP address. Send thousands of packets to one
1513 * address, and some will show up on the OTHER socket.
1515 * This hack works ONLY if the clients are global. If
1516 * each listener has the same client IP, but with
1517 * different secrets, then it will fail the rad_recv()
1518 * check above, and there's nothing you can do.
1521 listen_socket_t *sock = listener->data;
1522 rad_listen_t *other;
1524 other = listener_find_byipaddr(&packet->dst_ipaddr,
1525 packet->dst_port, sock->proto);
1526 if (other) listener = other;
1532 if (!request_receive(listener, packet, client, fun)) {
1533 FR_STATS_INC(auth, total_packets_dropped);
1542 #ifdef WITH_ACCOUNTING
1544 * Receive packets from an accounting socket
1546 static int acct_socket_recv(rad_listen_t *listener)
1550 RADIUS_PACKET *packet;
1551 RAD_REQUEST_FUNP fun = NULL;
1552 RADCLIENT *client = NULL;
1553 fr_ipaddr_t src_ipaddr;
1555 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1556 if (rcode < 0) return 0;
1558 FR_STATS_INC(acct, total_requests);
1560 if (rcode < 20) { /* AUTH_HDR_LEN */
1561 FR_STATS_INC(acct, total_malformed_requests);
1565 if ((client = client_listener_find(listener,
1566 &src_ipaddr, src_port)) == NULL) {
1567 rad_recv_discard(listener->fd);
1568 FR_STATS_INC(acct, total_invalid_requests);
1572 FR_STATS_TYPE_INC(client->acct.total_requests);
1575 * Some sanity checks, based on the packet code.
1578 case PW_CODE_ACCOUNTING_REQUEST:
1579 fun = rad_accounting;
1582 case PW_CODE_STATUS_SERVER:
1583 if (!mainconfig.status_server) {
1584 rad_recv_discard(listener->fd);
1585 FR_STATS_INC(acct, total_unknown_types);
1587 WDEBUG("Ignoring Status-Server request due to security configuration");
1590 fun = rad_status_server;
1594 rad_recv_discard(listener->fd);
1595 FR_STATS_INC(acct, total_unknown_types);
1597 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1598 code, client->shortname, src_port);
1600 } /* switch over packet types */
1603 * Now that we've sanity checked everything, receive the
1606 packet = rad_recv(listener->fd, 0);
1608 FR_STATS_INC(acct, total_malformed_requests);
1609 ERROR("%s", fr_strerror());
1614 * There can be no duplicate accounting packets.
1616 if (!request_receive(listener, packet, client, fun)) {
1617 FR_STATS_INC(acct, total_packets_dropped);
1628 static int do_proxy(REQUEST *request)
1632 if (request->in_proxy_hash ||
1633 (request->proxy_reply && (request->proxy_reply->code != 0))) {
1637 vp = pairfind(request->config_items, PW_HOME_SERVER_POOL, 0, TAG_ANY);
1640 if (!home_pool_byname(vp->vp_strvalue, HOME_TYPE_COA)) {
1641 REDEBUG2("Cannot proxy to unknown pool %s",
1650 * Receive a CoA packet.
1652 static int rad_coa_recv(REQUEST *request)
1654 int rcode = RLM_MODULE_OK;
1659 * Get the correct response
1661 switch (request->packet->code) {
1662 case PW_CODE_COA_REQUEST:
1663 ack = PW_CODE_COA_ACK;
1664 nak = PW_CODE_COA_NAK;
1667 case PW_CODE_DISCONNECT_REQUEST:
1668 ack = PW_CODE_DISCONNECT_ACK;
1669 nak = PW_CODE_DISCONNECT_NAK;
1672 default: /* shouldn't happen */
1673 return RLM_MODULE_FAIL;
1677 #define WAS_PROXIED (request->proxy)
1679 #define WAS_PROXIED (0)
1684 * RFC 5176 Section 3.3. If we have a CoA-Request
1685 * with Service-Type = Authorize-Only, it MUST
1686 * have a State attribute in it.
1688 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE, 0, TAG_ANY);
1689 if (request->packet->code == PW_CODE_COA_REQUEST) {
1690 if (vp && (vp->vp_integer == 17)) {
1691 vp = pairfind(request->packet->vps, PW_STATE, 0, TAG_ANY);
1692 if (!vp || (vp->length == 0)) {
1693 REDEBUG("CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1694 request->reply->code = PW_CODE_COA_NAK;
1695 return RLM_MODULE_FAIL;
1700 * RFC 5176, Section 3.2.
1702 REDEBUG("Disconnect-Request MUST NOT contain a Service-Type attribute");
1703 request->reply->code = PW_CODE_DISCONNECT_NAK;
1704 return RLM_MODULE_FAIL;
1707 rcode = process_recv_coa(0, request);
1709 case RLM_MODULE_FAIL:
1710 case RLM_MODULE_INVALID:
1711 case RLM_MODULE_REJECT:
1712 case RLM_MODULE_USERLOCK:
1714 request->reply->code = nak;
1717 case RLM_MODULE_HANDLED:
1720 case RLM_MODULE_NOOP:
1721 case RLM_MODULE_NOTFOUND:
1723 case RLM_MODULE_UPDATED:
1724 if (do_proxy(request)) return RLM_MODULE_OK;
1725 request->reply->code = ack;
1728 } else if (request->proxy_reply) {
1730 * Start the reply code with the proxy reply
1733 request->reply->code = request->proxy_reply->code;
1737 * Copy State from the request to the reply.
1738 * See RFC 5176 Section 3.3.
1740 vp = paircopy2(request->reply, request->packet->vps, PW_STATE, 0, TAG_ANY);
1741 if (vp) pairadd(&request->reply->vps, vp);
1744 * We may want to over-ride the reply.
1746 if (request->reply->code) {
1747 rcode = process_send_coa(0, request);
1750 * We need to send CoA-NAK back if Service-Type
1751 * is Authorize-Only. Rely on the user's policy
1752 * to do that. We're not a real NAS, so this
1753 * restriction doesn't (ahem) apply to us.
1755 case RLM_MODULE_FAIL:
1756 case RLM_MODULE_INVALID:
1757 case RLM_MODULE_REJECT:
1758 case RLM_MODULE_USERLOCK:
1761 * Over-ride an ACK with a NAK
1763 request->reply->code = nak;
1766 case RLM_MODULE_HANDLED:
1769 case RLM_MODULE_NOOP:
1770 case RLM_MODULE_NOTFOUND:
1772 case RLM_MODULE_UPDATED:
1774 * Do NOT over-ride a previously set value.
1775 * Otherwise an "ok" here will re-write a
1778 if (request->reply->code == 0) {
1779 request->reply->code = ack;
1785 return RLM_MODULE_OK;
1790 * Check if an incoming request is "ok"
1792 * It takes packets, not requests. It sees if the packet looks
1793 * OK. If so, it does a number of sanity checks on it.
1795 static int coa_socket_recv(rad_listen_t *listener)
1799 RADIUS_PACKET *packet;
1800 RAD_REQUEST_FUNP fun = NULL;
1801 RADCLIENT *client = NULL;
1802 fr_ipaddr_t src_ipaddr;
1804 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1805 if (rcode < 0) return 0;
1807 if (rcode < 20) { /* AUTH_HDR_LEN */
1808 FR_STATS_INC(coa, total_requests);
1809 FR_STATS_INC(coa, total_malformed_requests);
1813 if ((client = client_listener_find(listener,
1814 &src_ipaddr, src_port)) == NULL) {
1815 rad_recv_discard(listener->fd);
1816 FR_STATS_INC(coa, total_requests);
1817 FR_STATS_INC(coa, total_invalid_requests);
1822 * Some sanity checks, based on the packet code.
1825 case PW_CODE_COA_REQUEST:
1826 FR_STATS_INC(coa, total_requests);
1830 case PW_CODE_DISCONNECT_REQUEST:
1831 FR_STATS_INC(dsc, total_requests);
1836 rad_recv_discard(listener->fd);
1837 FR_STATS_INC(coa, total_unknown_types);
1838 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1839 code, client->shortname, src_port);
1841 } /* switch over packet types */
1844 * Now that we've sanity checked everything, receive the
1847 packet = rad_recv(listener->fd, client->message_authenticator);
1849 FR_STATS_INC(coa, total_malformed_requests);
1850 DEBUG("%s", fr_strerror());
1854 if (!request_receive(listener, packet, client, fun)) {
1855 FR_STATS_INC(coa, total_packets_dropped);
1866 * Recieve packets from a proxy socket.
1868 static int proxy_socket_recv(rad_listen_t *listener)
1870 RADIUS_PACKET *packet;
1873 packet = rad_recv(listener->fd, 0);
1875 ERROR("%s", fr_strerror());
1880 * FIXME: Client MIB updates?
1882 switch(packet->code) {
1883 case PW_CODE_AUTHENTICATION_ACK:
1884 case PW_CODE_ACCESS_CHALLENGE:
1885 case PW_CODE_AUTHENTICATION_REJECT:
1888 #ifdef WITH_ACCOUNTING
1889 case PW_CODE_ACCOUNTING_RESPONSE:
1894 case PW_CODE_DISCONNECT_ACK:
1895 case PW_CODE_DISCONNECT_NAK:
1896 case PW_CODE_COA_ACK:
1897 case PW_CODE_COA_NAK:
1903 * FIXME: Update MIB for packet types?
1905 ERROR("Invalid packet code %d sent to a proxy port "
1906 "from home server %s port %d - ID %d : IGNORED",
1908 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1909 packet->src_port, packet->id);
1914 if (!request_proxy_reply(packet)) {
1924 * Recieve packets from a proxy socket.
1926 static int proxy_socket_tcp_recv(rad_listen_t *listener)
1928 RADIUS_PACKET *packet;
1929 listen_socket_t *sock = listener->data;
1932 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return 0;
1934 packet = fr_tcp_recv(listener->fd, 0);
1936 listener->status = RAD_LISTEN_STATUS_EOL;
1937 event_new_fd(listener);
1942 * FIXME: Client MIB updates?
1944 switch(packet->code) {
1945 case PW_CODE_AUTHENTICATION_ACK:
1946 case PW_CODE_ACCESS_CHALLENGE:
1947 case PW_CODE_AUTHENTICATION_REJECT:
1950 #ifdef WITH_ACCOUNTING
1951 case PW_CODE_ACCOUNTING_RESPONSE:
1957 * FIXME: Update MIB for packet types?
1959 ERROR("Invalid packet code %d sent to a proxy port "
1960 "from home server %s port %d - ID %d : IGNORED",
1962 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1963 packet->src_port, packet->id);
1968 packet->src_ipaddr = sock->other_ipaddr;
1969 packet->src_port = sock->other_port;
1970 packet->dst_ipaddr = sock->my_ipaddr;
1971 packet->dst_port = sock->my_port;
1974 * FIXME: Have it return an indication of packets that
1975 * are OK to ignore (dups, too late), versus ones that
1976 * aren't OK to ignore (unknown response, spoofed, etc.)
1978 * Close the socket on bad packets...
1980 if (!request_proxy_reply(packet)) {
1985 sock->opened = sock->last_packet = time(NULL);
1993 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1995 if (!request->reply->code) return 0;
1997 if (rad_encode(request->reply, request->packet,
1998 request->client->secret) < 0) {
1999 RERROR("Failed encoding packet: %s",
2004 if (rad_sign(request->reply, request->packet,
2005 request->client->secret) < 0) {
2006 RERROR("Failed signing packet: %s",
2015 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
2017 if (rad_verify(request->packet, NULL,
2018 request->client->secret) < 0) {
2022 return rad_decode(request->packet, NULL,
2023 request->client->secret);
2027 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
2029 if (rad_encode(request->proxy, NULL, request->home_server->secret) < 0) {
2030 RERROR("Failed encoding proxied packet: %s",
2035 if (rad_sign(request->proxy, NULL, request->home_server->secret) < 0) {
2036 RERROR("Failed signing proxied packet: %s",
2045 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
2048 * rad_verify is run in event.c, received_proxy_response()
2051 return rad_decode(request->proxy_reply, request->proxy,
2052 request->home_server->secret);
2056 #include "command.c"
2059 * Temporarily NOT const!
2061 static fr_protocol_t master_listen[RAD_LISTEN_MAX] = {
2063 { RLM_MODULE_INIT, "status", sizeof(listen_socket_t), NULL,
2064 common_socket_parse, NULL,
2065 stats_socket_recv, auth_socket_send,
2066 common_socket_print, client_socket_encode, client_socket_decode },
2069 * This always gets defined.
2071 { RLM_MODULE_INIT, "status", 0, NULL,
2072 NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
2077 { RLM_MODULE_INIT, "proxy", sizeof(listen_socket_t), NULL,
2078 common_socket_parse, NULL,
2079 proxy_socket_recv, proxy_socket_send,
2080 common_socket_print, proxy_socket_encode, proxy_socket_decode },
2083 /* authentication */
2084 { RLM_MODULE_INIT, "auth", sizeof(listen_socket_t), NULL,
2085 common_socket_parse, common_socket_free,
2086 auth_socket_recv, auth_socket_send,
2087 common_socket_print, client_socket_encode, client_socket_decode },
2089 #ifdef WITH_ACCOUNTING
2091 { RLM_MODULE_INIT, "acct", sizeof(listen_socket_t), NULL,
2092 common_socket_parse, common_socket_free,
2093 acct_socket_recv, acct_socket_send,
2094 common_socket_print, client_socket_encode, client_socket_decode},
2099 { RLM_MODULE_INIT, "detail", sizeof(listen_detail_t), NULL,
2100 detail_parse, detail_free,
2101 detail_recv, detail_send,
2102 detail_print, detail_encode, detail_decode },
2106 /* vlan query protocol */
2107 { 0, "vmps", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
2111 /* dhcp query protocol */
2112 { 0, "dhcp", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
2115 #ifdef WITH_COMMAND_SOCKET
2116 /* TCP command socket */
2117 { RLM_MODULE_INIT, "control", sizeof(fr_command_socket_t), NULL,
2118 command_socket_parse, command_socket_free,
2119 command_domain_accept, command_domain_send,
2120 command_socket_print, command_socket_encode, command_socket_decode },
2124 /* Change of Authorization */
2125 { RLM_MODULE_INIT, "coa", sizeof(listen_socket_t), NULL,
2126 common_socket_parse, NULL,
2127 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
2128 common_socket_print, client_socket_encode, client_socket_decode },
2136 * Binds a listener to a socket.
2138 static int listen_bind(rad_listen_t *this)
2141 struct sockaddr_storage salocal;
2143 listen_socket_t *sock = this->data;
2145 #define proto_for_port "udp"
2146 #define sock_type SOCK_DGRAM
2148 char const *proto_for_port = "udp";
2149 int sock_type = SOCK_DGRAM;
2151 if (sock->proto == IPPROTO_TCP) {
2153 if (this->type == RAD_LISTEN_VQP) {
2154 ERROR("VQP does not support TCP transport");
2159 proto_for_port = "tcp";
2160 sock_type = SOCK_STREAM;
2165 * If the port is zero, then it means the appropriate
2166 * thing from /etc/services.
2168 if (sock->my_port == 0) {
2169 struct servent *svp;
2171 switch (this->type) {
2172 case RAD_LISTEN_AUTH:
2173 svp = getservbyname ("radius", proto_for_port);
2175 sock->my_port = ntohs(svp->s_port);
2177 sock->my_port = PW_AUTH_UDP_PORT;
2181 #ifdef WITH_ACCOUNTING
2182 case RAD_LISTEN_ACCT:
2183 svp = getservbyname ("radacct", proto_for_port);
2185 sock->my_port = ntohs(svp->s_port);
2187 sock->my_port = PW_ACCT_UDP_PORT;
2193 case RAD_LISTEN_PROXY:
2194 /* leave it at zero */
2199 case RAD_LISTEN_VQP:
2200 sock->my_port = 1589;
2204 #ifdef WITH_COMMAND_SOCKET
2205 case RAD_LISTEN_COMMAND:
2206 sock->my_port = PW_RADMIN_PORT;
2211 case RAD_LISTEN_COA:
2212 svp = getservbyname ("radius-dynauth", "udp");
2214 sock->my_port = ntohs(svp->s_port);
2216 sock->my_port = PW_COA_UDP_PORT;
2222 WDEBUG("Internal sanity check failed in binding to socket. Ignoring problem.");
2228 * Don't open sockets if we're checking the config.
2236 * Copy fr_socket() here, as we may need to bind to a device.
2238 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
2242 this->print(this, buffer, sizeof(buffer));
2244 ERROR("Failed opening %s: %s", buffer, fr_syserror(errno));
2250 * We don't want child processes inheriting these
2253 rcode = fcntl(this->fd, F_GETFD);
2255 if (fcntl(this->fd, F_SETFD, rcode | FD_CLOEXEC) < 0) {
2257 ERROR("Failed setting close on exec: %s", fr_syserror(errno));
2264 * Bind to a device BEFORE touching IP addresses.
2266 if (sock->interface) {
2267 #ifdef SO_BINDTODEVICE
2270 memset(&ifreq, 0, sizeof(ifreq));
2271 strlcpy(ifreq.ifr_name, sock->interface, sizeof(ifreq.ifr_name));
2274 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
2275 (char *)&ifreq, sizeof(ifreq));
2279 ERROR("Failed binding to interface %s: %s",
2280 sock->interface, fr_syserror(errno));
2282 } /* else it worked. */
2284 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2285 #ifdef HAVE_NET_IF_H
2287 * Odds are that any system supporting "bind to
2288 * device" also supports IPv6, so this next bit
2289 * isn't necessary. But it's here for
2292 * If we're doing IPv6, and the scope hasn't yet
2293 * been defined, set the scope to the scope of
2296 if (sock->my_ipaddr.af == AF_INET6) {
2297 if (sock->my_ipaddr.scope == 0) {
2298 sock->my_ipaddr.scope = if_nametoindex(sock->interface);
2299 if (sock->my_ipaddr.scope == 0) {
2301 ERROR("Failed finding interface %s: %s",
2302 sock->interface, fr_syserror(errno));
2305 } /* else scope was defined: we're OK. */
2310 * IPv4: no link local addresses,
2311 * and no bind to device.
2315 ERROR("Failed binding to interface %s: \"bind to device\" is unsupported", sock->interface);
2322 if (sock->proto == IPPROTO_TCP) {
2325 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2327 ERROR("Failed to reuse address: %s", fr_syserror(errno));
2333 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
2334 else /* UDP sockets get UDPfromto */
2337 #ifdef WITH_UDPFROMTO
2339 * Initialize udpfromto for all sockets.
2341 if (udpfromto_init(this->fd) != 0) {
2342 ERROR("Failed initializing udpfromto: %s",
2343 fr_syserror(errno));
2350 * Set up sockaddr stuff.
2352 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
2357 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2358 if (sock->my_ipaddr.af == AF_INET6) {
2360 * Listening on '::' does NOT get you IPv4 to
2361 * IPv6 mapping. You've got to listen on an IPv4
2362 * address, too. This makes the rest of the server
2363 * design a little simpler.
2367 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2370 if (setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2371 (char *)&on, sizeof(on)) < 0) {
2372 ERROR("Failed setting socket to IPv6 "
2373 "only: %s", fr_syserror(errno));
2379 #endif /* IPV6_V6ONLY */
2381 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2383 if (sock->my_ipaddr.af == AF_INET) {
2386 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2388 * Disable PMTU discovery. On Linux, this
2389 * also makes sure that the "don't fragment"
2392 flag = IP_PMTUDISC_DONT;
2393 if (setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2394 &flag, sizeof(flag)) < 0) {
2395 ERROR("Failed disabling PMTU discovery: %s",
2396 fr_syserror(errno));
2403 #if defined(IP_DONTFRAG)
2405 * Ensure that the "don't fragment" flag is zero.
2408 if (setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2409 &flag, sizeof(flag)) < 0) {
2410 ERROR("Failed setting don't fragment flag: %s",
2411 fr_syserror(errno));
2421 if (sock->broadcast) {
2424 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2425 ERROR("Can't set broadcast option: %s",
2426 fr_syserror(errno));
2434 * May be binding to priviledged ports.
2436 if (sock->my_port != 0) {
2440 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2441 ERROR("Can't set re-use address option: %s",
2442 fr_syserror(errno));
2448 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2454 this->print(this, buffer, sizeof(buffer));
2455 ERROR("Failed binding to %s: %s\n",
2456 buffer, fr_syserror(errno));
2461 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2462 * kernel instead binds us to a 1.2.3.4. If this
2463 * happens, notice, and remember our real IP.
2466 struct sockaddr_storage src;
2467 socklen_t sizeof_src = sizeof(src);
2469 memset(&src, 0, sizeof_src);
2470 if (getsockname(this->fd, (struct sockaddr *) &src,
2472 ERROR("Failed getting socket name: %s",
2473 fr_syserror(errno));
2477 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2478 &sock->my_ipaddr, &sock->my_port)) {
2479 ERROR("Socket has unsupported address family");
2486 if (sock->proto == IPPROTO_TCP) {
2488 * Allow a backlog of 8 listeners
2490 if (listen(this->fd, 8) < 0) {
2492 ERROR("Failed in listen(): %s", fr_syserror(errno));
2497 * If there are hard-coded worker threads, they're blocking.
2499 * Otherwise, they're non-blocking.
2501 if (!this->workers && fr_nonblock(this->fd) < 0) {
2503 ERROR("Failed setting non-blocking on socket: %s",
2504 fr_syserror(errno));
2511 * Mostly for proxy sockets.
2513 sock->other_ipaddr.af = sock->my_ipaddr.af;
2516 * Don't screw up other people.
2518 #undef proto_for_port
2525 static int listener_free(void *ctx)
2529 this = talloc_get_type_abort(ctx, rad_listen_t);
2532 * Other code may have eaten the FD.
2534 if (this->fd >= 0) close(this->fd);
2536 if (master_listen[this->type].free) {
2537 master_listen[this->type].free(this);
2541 if ((this->type == RAD_LISTEN_AUTH)
2543 || (this->type == RAD_LISTEN_ACCT)
2546 || (this->type == RAD_LISTEN_PROXY)
2548 #ifdef WITH_COMMAND_SOCKET
2549 || ((this->type == RAD_LISTEN_COMMAND) &&
2550 (((fr_command_socket_t *) this->data)->magic != COMMAND_SOCKET_MAGIC))
2553 listen_socket_t *sock = this->data;
2555 rad_free(&sock->packet);
2557 rad_assert(sock->ev == NULL);
2561 * Note that we do NOT free this->tls, as the
2562 * pointer is parented by its CONF_SECTION. It
2563 * may be used by multiple listeners.
2566 if (sock->ssn) session_free(sock->ssn);
2567 request_free(&sock->request);
2568 #ifdef HAVE_PTHREAD_H
2569 pthread_mutex_destroy(&(sock->mutex));
2572 #endif /* WITH_TLS */
2574 #endif /* WITH_TCP */
2580 * Allocate & initialize a new listener.
2582 static rad_listen_t *listen_alloc(TALLOC_CTX *ctx, RAD_LISTEN_TYPE type)
2586 this = talloc_zero(ctx, rad_listen_t);
2589 this->recv = master_listen[this->type].recv;
2590 this->send = master_listen[this->type].send;
2591 this->print = master_listen[this->type].print;
2592 this->encode = master_listen[this->type].encode;
2593 this->decode = master_listen[this->type].decode;
2595 talloc_set_destructor((void *) this, listener_free);
2597 this->data = talloc_zero_array(this, uint8_t, master_listen[this->type].inst_size);
2604 * Externally visible function for creating a new proxy LISTENER.
2606 * Not thread-safe, but all calls to it are protected by the
2607 * proxy mutex in event.c
2609 rad_listen_t *proxy_new_listener(home_server_t *home, int src_port)
2613 listen_socket_t *sock;
2616 if (!home) return NULL;
2618 if ((home->limit.max_connections > 0) &&
2619 (home->limit.num_connections >= home->limit.max_connections)) {
2620 WDEBUG("Home server has too many open connections (%d)",
2621 home->limit.max_connections);
2626 if (home->last_failed_open == now) {
2627 WDEBUG("Suppressing attempt to open socket to 'down' home server");
2631 this = listen_alloc(mainconfig.config, RAD_LISTEN_PROXY);
2634 sock->other_ipaddr = home->ipaddr;
2635 sock->other_port = home->port;
2638 sock->my_ipaddr = home->src_ipaddr;
2639 sock->my_port = src_port;
2640 sock->proto = home->proto;
2643 * For error messages.
2645 this->print(this, buffer, sizeof(buffer));
2647 if (debug_flag >= 2) {
2648 DEBUG("Opening new proxy socket '%s'", buffer);
2652 sock->opened = sock->last_packet = now;
2654 if (home->proto == IPPROTO_TCP) {
2655 this->recv = proxy_socket_tcp_recv;
2658 * FIXME: connect() is blocking!
2659 * We do this with the proxy mutex locked, which may
2660 * cause large delays!
2662 * http://www.developerweb.net/forum/showthread.php?p=13486
2664 this->fd = fr_tcp_client_socket(&home->src_ipaddr,
2665 &home->ipaddr, home->port);
2668 this->fd = fr_socket(&home->src_ipaddr, src_port);
2671 this->print(this, buffer,sizeof(buffer));
2672 ERROR("Failed opening proxy socket '%s' : %s",
2673 buffer, fr_strerror());
2674 home->last_failed_open = now;
2682 if ((home->proto == IPPROTO_TCP) && home->tls) {
2683 DEBUG("Trying SSL to port %d\n", home->port);
2684 sock->ssn = tls_new_client_session(home->tls, this->fd);
2686 ERROR("Failed starting SSL to '%s'", buffer);
2687 home->last_failed_open = now;
2692 this->recv = proxy_tls_recv;
2693 this->send = proxy_tls_send;
2698 * Figure out which port we were bound to.
2700 if (sock->my_port == 0) {
2701 struct sockaddr_storage src;
2702 socklen_t sizeof_src = sizeof(src);
2704 memset(&src, 0, sizeof_src);
2705 if (getsockname(this->fd, (struct sockaddr *) &src,
2707 ERROR("Failed getting socket name for '%s': %s",
2708 buffer, fr_syserror(errno));
2709 home->last_failed_open = now;
2714 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2715 &sock->my_ipaddr, &sock->my_port)) {
2716 ERROR("Socket has unsupported address family for '%s'", buffer);
2717 home->last_failed_open = now;
2723 home->limit.num_connections++;
2729 static const FR_NAME_NUMBER listen_compare[] = {
2731 { "status", RAD_LISTEN_NONE },
2733 { "auth", RAD_LISTEN_AUTH },
2734 #ifdef WITH_ACCOUNTING
2735 { "acct", RAD_LISTEN_ACCT },
2736 { "auth+acct", RAD_LISTEN_AUTH },
2739 { "detail", RAD_LISTEN_DETAIL },
2742 { "proxy", RAD_LISTEN_PROXY },
2745 { "vmps", RAD_LISTEN_VQP },
2748 { "dhcp", RAD_LISTEN_DHCP },
2750 #ifdef WITH_COMMAND_SOCKET
2751 { "control", RAD_LISTEN_COMMAND },
2754 { "coa", RAD_LISTEN_COA },
2759 static int _free_proto_handle(lt_dlhandle *handle)
2765 static rad_listen_t *listen_parse(CONF_SECTION *cs, char const *server)
2775 cp = cf_pair_find(cs, "type");
2778 "No type specified in listen section");
2782 value = cf_pair_value(cp);
2785 "Type cannot be empty");
2789 snprintf(buffer, sizeof(buffer), "proto_%s", value);
2790 handle = lt_dlopenext(buffer);
2792 fr_protocol_t *proto;
2793 lt_dlhandle *marker;
2795 proto = dlsym(handle, buffer);
2798 "Failed linking to protocol %s : %s\n",
2804 type = fr_str2int(listen_compare, value, -1);
2805 rad_assert(type >= 0); /* shouldn't be able to compile an invalid type */
2807 memcpy(&master_listen[type], proto, sizeof(*proto));
2810 * Ensure handle gets closed if config section gets freed
2812 marker = talloc(cs, lt_dlhandle);
2814 talloc_set_destructor(marker, _free_proto_handle);
2816 if (master_listen[type].magic != RLM_MODULE_INIT) {
2817 ERROR("Failed to load protocol '%s' due to internal sanity check problem",
2818 master_listen[type].name);
2823 cf_log_info(cs, "listen {");
2826 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
2828 if (rcode < 0) return NULL;
2831 "No type specified in listen section");
2835 type = fr_str2int(listen_compare, listen_type, -1);
2838 "Invalid type \"%s\" in listen section.",
2844 * Allow listen sections in the default config to
2845 * refer to a server.
2848 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
2850 if (rcode == 1) { /* compatiblity with 2.0-pre */
2851 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
2854 if (rcode < 0) return NULL;
2859 * We were passed a virtual server, so the caller is
2860 * defining a proxy listener inside of a virtual server.
2861 * This isn't allowed right now.
2863 else if (type == RAD_LISTEN_PROXY) {
2864 ERROR("Error: listen type \"proxy\" Cannot appear in a virtual server section");
2870 * Set up cross-type data.
2872 this = listen_alloc(cs, type);
2873 this->server = server;
2878 * Special-case '+' for "auth+acct".
2880 if (strchr(listen_type, '+') != NULL) {
2886 * Call per-type parser.
2888 if (master_listen[type].parse(cs, this) < 0) {
2893 cf_log_info(cs, "}");
2898 #ifdef HAVE_PTHREAD_H
2900 * A child thread which does NOTHING other than read and process
2903 static void *recv_thread(void *arg)
2905 rad_listen_t *this = arg;
2918 * Generate a list of listeners. Takes an input list of
2919 * listeners, too, so we don't close sockets with waiting packets.
2921 int listen_init(CONF_SECTION *config, rad_listen_t **head,
2925 UNUSED bool spawn_flag
2930 bool override = false;
2931 CONF_SECTION *cs = NULL;
2932 rad_listen_t **last;
2934 fr_ipaddr_t server_ipaddr;
2937 bool defined_proxy = false;
2941 * We shouldn't be called with a pre-existing list.
2943 rad_assert(head && (*head == NULL));
2945 memset(&server_ipaddr, 0, sizeof(server_ipaddr));
2948 server_ipaddr.af = AF_UNSPEC;
2951 * If the port is specified on the command-line,
2952 * it over-rides the configuration file.
2954 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
2956 if (mainconfig.port >= 0) {
2957 auth_port = mainconfig.port;
2960 * -p X but no -i Y on the command-line.
2962 if ((mainconfig.port > 0) &&
2963 (mainconfig.myip.af == AF_UNSPEC)) {
2964 ERROR("The command-line says \"-p %d\", but there is no associated IP address to use",
2971 * If the IP address was configured on the command-line,
2972 * use that as the "bind_address"
2974 if (mainconfig.myip.af != AF_UNSPEC) {
2975 listen_socket_t *sock;
2977 memcpy(&server_ipaddr, &mainconfig.myip,
2978 sizeof(server_ipaddr));
2982 if (strcmp(progname, "vmpsd") == 0) {
2983 this = listen_alloc(config, RAD_LISTEN_VQP);
2984 if (!auth_port) auth_port = 1589;
2987 this = listen_alloc(config, RAD_LISTEN_AUTH);
2991 sock->my_ipaddr = server_ipaddr;
2992 sock->my_port = auth_port;
2994 sock->clients = clients_parse_section(config, false);
2995 if (!sock->clients) {
2996 cf_log_err_cs(config,
2997 "Failed to find any clients for this listen section");
3002 if (listen_bind(this) < 0) {
3004 ERROR("There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
3008 auth_port = sock->my_port; /* may have been updated in listen_bind */
3010 cs = cf_section_sub_find_name2(config, "server",
3012 if (cs) this->server = mainconfig.name;
3016 last = &(this->next);
3022 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
3025 #ifdef WITH_ACCOUNTING
3027 * Open Accounting Socket.
3029 * If we haven't already gotten acct_port from
3030 * /etc/services, then make it auth_port + 1.
3032 this = listen_alloc(config, RAD_LISTEN_ACCT);
3036 * Create the accounting socket.
3038 * The accounting port is always the
3039 * authentication port + 1
3041 sock->my_ipaddr = server_ipaddr;
3042 sock->my_port = auth_port + 1;
3044 sock->clients = clients_parse_section(config, false);
3045 if (!sock->clients) {
3046 cf_log_err_cs(config,
3047 "Failed to find any clients for this listen section");
3051 if (listen_bind(this) < 0) {
3054 ERROR("There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
3059 cs = cf_section_sub_find_name2(config, "server",
3061 if (cs) this->server = mainconfig.name;
3065 last = &(this->next);
3070 * They specified an IP on the command-line, ignore
3071 * all listen sections except the one in '-n'.
3073 if (mainconfig.myip.af != AF_UNSPEC) {
3074 CONF_SECTION *subcs;
3075 char const *name2 = cf_section_name2(cs);
3077 cs = cf_section_sub_find_name2(config, "server",
3079 if (!cs) goto add_sockets;
3082 * Should really abstract this code...
3084 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
3086 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
3087 this = listen_parse(subcs, name2);
3094 last = &(this->next);
3095 } /* loop over "listen" directives in server <foo> */
3101 * Walk through the "listen" sections, if they exist.
3103 for (cs = cf_subsection_find_next(config, NULL, "listen");
3105 cs = cf_subsection_find_next(config, cs, "listen")) {
3106 this = listen_parse(cs, NULL);
3113 last = &(this->next);
3117 * Check virtual servers for "listen" sections, too.
3119 * FIXME: Move to virtual server init?
3121 for (cs = cf_subsection_find_next(config, NULL, "server");
3123 cs = cf_subsection_find_next(config, cs, "server")) {
3124 CONF_SECTION *subcs;
3125 char const *name2 = cf_section_name2(cs);
3127 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
3129 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
3130 this = listen_parse(subcs, name2);
3137 last = &(this->next);
3138 } /* loop over "listen" directives in virtual servers */
3139 } /* loop over virtual servers */
3143 * No sockets to receive packets, this is an error.
3144 * proxying is pointless.
3147 ERROR("The server is not configured to listen on any ports. Cannot start.");
3152 * Print out which sockets we're listening on, and
3153 * add them to the event list.
3155 for (this = *head; this != NULL; this = this->next) {
3157 if (this->type == RAD_LISTEN_PROXY) {
3158 defined_proxy = true;
3164 if (!check_config && !spawn_flag && this->tls) {
3165 cf_log_err_cs(this->cs, "Threading must be enabled for TLS sockets to function properly");
3166 cf_log_err_cs(this->cs, "You probably need to do '%s -fxx -l stdout' for debugging",
3171 if (!check_config) {
3172 if (this->workers && !spawn_flag) {
3173 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
3177 if (this->workers) {
3178 #ifndef HAVE_PTHREAD_H
3179 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
3185 this->print(this, buffer, sizeof(buffer));
3187 for (i = 0; i < this->workers; i++) {
3191 * FIXME: create detached?
3193 rcode = pthread_create(&id, 0, recv_thread, this);
3195 ERROR("Thread create failed: %s",
3196 fr_syserror(rcode));
3200 DEBUG("Thread %d for %s\n", i, buffer);
3211 * If we're proxying requests, open the proxy FD.
3212 * Otherwise, don't do anything.
3215 if ((mainconfig.proxy_requests == true) &&
3217 (*head != NULL) && !defined_proxy) {
3221 memset(&home, 0, sizeof(home));
3226 home.proto = IPPROTO_UDP;
3227 home.src_ipaddr = server_ipaddr;
3231 * Address is still unspecified, use IPv4.
3233 if (home.src_ipaddr.af == AF_UNSPEC) {
3234 home.src_ipaddr.af = AF_INET;
3235 /* everything else is already set to zero */
3238 home.ipaddr.af = home.src_ipaddr.af;
3239 /* everything else is already set to zero */
3241 this = proxy_new_listener(&home, port);
3247 if (!event_new_fd(this)) {
3256 * Haven't defined any sockets. Die.
3258 if (!*head) return -1;
3260 xlat_register("listen", xlat_listen, NULL, NULL);
3266 * Free a linked list of listeners;
3268 void listen_free(rad_listen_t **head)
3272 if (!head || !*head) return;
3276 rad_listen_t *next = this->next;
3285 RADCLIENT_LIST *listener_find_client_list(fr_ipaddr_t const *ipaddr,
3290 for (this = mainconfig.listen; this != NULL; this = this->next) {
3291 listen_socket_t *sock;
3293 if ((this->type != RAD_LISTEN_AUTH)
3294 #ifdef WITH_ACCOUNTING
3295 && (this->type != RAD_LISTEN_ACCT)
3301 if ((sock->my_port == port) &&
3302 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
3303 return sock->clients;
3311 rad_listen_t *listener_find_byipaddr(fr_ipaddr_t const *ipaddr, int port, int proto)
3315 for (this = mainconfig.listen; this != NULL; this = this->next) {
3316 listen_socket_t *sock;
3320 if (sock->my_port != port) continue;
3321 if (sock->proto != proto) continue;
3322 if (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) != 0) continue;
3328 * Failed to find a specific one. Find INADDR_ANY
3330 for (this = mainconfig.listen; this != NULL; this = this->next) {
3331 listen_socket_t *sock;
3335 if (sock->my_port != port) continue;
3336 if (sock->proto != proto) continue;
3337 if (!fr_inaddr_any(&sock->my_ipaddr)) continue;