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>
32 #include <freeradius-devel/detail.h>
35 #include <freeradius-devel/udpfromto.h>
38 #ifdef HAVE_SYS_RESOURCE_H
39 #include <sys/resource.h>
51 #ifdef DEBUG_PRINT_PACKET
52 static void print_packet(RADIUS_PACKET *packet)
54 char src[256], dst[256];
56 ip_ntoh(&packet->src_ipaddr, src, sizeof(src));
57 ip_ntoh(&packet->dst_ipaddr, dst, sizeof(dst));
59 fprintf(stderr, "ID %d: %s %d -> %s %d\n", packet->id,
60 src, packet->src_port, dst, packet->dst_port);
66 static rad_listen_t *listen_alloc(TALLOC_CTX *ctx, RAD_LISTEN_TYPE type);
68 #ifdef WITH_COMMAND_SOCKET
69 static int command_tcp_recv(rad_listen_t *listener);
70 static int command_tcp_send(rad_listen_t *listener, REQUEST *request);
71 static int command_write_magic(int newfd, listen_socket_t *sock);
74 static fr_protocol_t master_listen[RAD_LISTEN_MAX];
77 * Xlat for %{listen:foo}
79 static ssize_t xlat_listen(UNUSED void *instance, REQUEST *request,
80 char const *fmt, char *out,
83 char const *value = NULL;
86 if (!fmt || !out || (outlen < 1)) return 0;
88 if (!request || !request->listener) {
89 RWDEBUG("No listener associated with this request");
94 cp = cf_pair_find(request->listener->cs, fmt);
95 if (!cp || !(value = cf_pair_value(cp))) {
96 RDEBUG("Listener does not contain config item \"%s\"", fmt);
101 strlcpy(out, value, outlen);
107 * Find a per-socket client.
109 RADCLIENT *client_listener_find(rad_listen_t *listener,
110 fr_ipaddr_t const *ipaddr, int src_port)
112 #ifdef WITH_DYNAMIC_CLIENTS
119 RADCLIENT_LIST *clients;
120 listen_socket_t *sock;
122 rad_assert(listener != NULL);
123 rad_assert(ipaddr != NULL);
125 sock = listener->data;
126 clients = sock->clients;
129 * This HAS to have been initialized previously.
131 rad_assert(clients != NULL);
133 client = client_find(clients, ipaddr,sock->proto);
135 char name[256], buffer[128];
137 #ifdef WITH_DYNAMIC_CLIENTS
138 unknown: /* used only for dynamic clients */
142 * DoS attack quenching, but only in daemon mode.
143 * If they're running in debug mode, show them
146 if (debug_flag == 0) {
147 static time_t last_printed = 0;
150 if (last_printed == now) return NULL;
155 listener->print(listener, name, sizeof(name));
157 ERROR("Ignoring request to %s from unknown client %s port %d"
161 , name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
162 buffer, sizeof(buffer)), src_port
164 , (sock->proto == IPPROTO_UDP) ? "udp" : "tcp"
170 #ifndef WITH_DYNAMIC_CLIENTS
171 return client; /* return the found client. */
175 * No server defined, and it's not dynamic. Return it.
177 if (!client->client_server && !client->dynamic) return client;
182 * It's a dynamically generated client, check it.
184 if (client->dynamic && (src_port != 0)) {
186 * Lives forever. Return it.
188 if (client->lifetime == 0) return client;
191 * Rate-limit the deletion of known clients.
192 * This makes them last a little longer, but
193 * prevents the server from melting down if (say)
194 * 10k clients all expire at once.
196 if (now == client->last_new_client) return client;
199 * It's not dead yet. Return it.
201 if ((client->created + client->lifetime) > now) return client;
204 * This really puts them onto a queue for later
207 client_delete(clients, client);
210 * Go find the enclosing network again.
212 client = client_find(clients, ipaddr, sock->proto);
217 if (!client) goto unknown;
218 if (!client->client_server) goto unknown;
221 * At this point, 'client' is the enclosing
222 * network that configures where dynamic clients
225 rad_assert(client->dynamic == 0);
227 } else if (!client->dynamic && client->rate_limit) {
229 * The IP is unknown, so we've found an enclosing
230 * network. Enable DoS protection. We only
231 * allow one new client per second. Known
232 * clients aren't subject to this restriction.
234 if (now == client->last_new_client) goto unknown;
237 client->last_new_client = now;
239 request = request_alloc(NULL);
240 if (!request) goto unknown;
242 request->listener = listener;
243 request->client = client;
244 request->packet = rad_recv(listener->fd, 0x02); /* MSG_PEEK */
245 if (!request->packet) { /* badly formed, etc */
246 request_free(&request);
249 request->reply = rad_alloc_reply(request, request->packet);
250 if (!request->reply) {
251 request_free(&request);
254 gettimeofday(&request->packet->timestamp, NULL);
256 request->priority = listener->type;
257 request->server = client->client_server;
258 request->root = &mainconfig;
261 * Run a fake request through the given virtual server.
262 * Look for FreeRADIUS-Client-IP-Address
263 * FreeRADIUS-Client-Secret
266 * and create the RADCLIENT structure from that.
268 DEBUG("server %s {", request->server);
270 rcode = process_authorize(0, request);
272 DEBUG("} # server %s", request->server);
274 if (rcode != RLM_MODULE_OK) {
275 request_free(&request);
280 * If the client was updated by rlm_dynamic_clients,
281 * don't create the client from attribute-value pairs.
283 if (request->client == client) {
284 created = client_from_request(clients, request);
286 created = request->client;
289 * This frees the client if it isn't valid.
291 if (!client_validate(clients, client, created)) goto unknown;
294 request->server = client->server;
295 exec_trigger(request, NULL, "server.client.add", false);
297 request_free(&request);
299 if (!created) goto unknown;
305 static int listen_bind(rad_listen_t *this);
309 * Process and reply to a server-status request.
310 * Like rad_authenticate and rad_accounting this should
311 * live in it's own file but it's so small we don't bother.
313 int rad_status_server(REQUEST *request)
315 int rcode = RLM_MODULE_OK;
318 switch (request->listener->type) {
320 case RAD_LISTEN_NONE:
322 case RAD_LISTEN_AUTH:
323 dval = dict_valbyname(PW_AUTZ_TYPE, 0, "Status-Server");
325 rcode = process_authorize(dval->value, request);
327 rcode = RLM_MODULE_OK;
332 case RLM_MODULE_UPDATED:
333 request->reply->code = PW_AUTHENTICATION_ACK;
336 case RLM_MODULE_FAIL:
337 case RLM_MODULE_HANDLED:
338 request->reply->code = 0; /* don't reply */
342 case RLM_MODULE_REJECT:
343 request->reply->code = PW_AUTHENTICATION_REJECT;
348 #ifdef WITH_ACCOUNTING
349 case RAD_LISTEN_ACCT:
350 dval = dict_valbyname(PW_ACCT_TYPE, 0, "Status-Server");
352 rcode = process_accounting(dval->value, request);
354 rcode = RLM_MODULE_OK;
359 case RLM_MODULE_UPDATED:
360 request->reply->code = PW_ACCOUNTING_RESPONSE;
364 request->reply->code = 0; /* don't reply */
372 * This is a vendor extension. Suggested by Glen
373 * Zorn in IETF 72, and rejected by the rest of
374 * the WG. We like it, so it goes in here.
377 dval = dict_valbyname(PW_RECV_COA_TYPE, 0, "Status-Server");
379 rcode = process_recv_coa(dval->value, request);
381 rcode = RLM_MODULE_OK;
386 case RLM_MODULE_UPDATED:
387 request->reply->code = PW_COA_ACK;
391 request->reply->code = 0; /* don't reply */
403 * Full statistics are available only on a statistics
406 if (request->listener->type == RAD_LISTEN_NONE) {
407 request_stats_reply(request);
415 static int dual_tcp_recv(rad_listen_t *listener)
418 RADIUS_PACKET *packet;
419 RAD_REQUEST_FUNP fun = NULL;
420 listen_socket_t *sock = listener->data;
421 RADCLIENT *client = sock->client;
423 rad_assert(client != NULL);
426 * Allocate a packet for partial reads.
429 sock->packet = rad_alloc(sock, 0);
430 if (!sock->packet) return 0;
432 sock->packet->sockfd = listener->fd;
433 sock->packet->src_ipaddr = sock->other_ipaddr;
434 sock->packet->src_port = sock->other_port;
435 sock->packet->dst_ipaddr = sock->my_ipaddr;
436 sock->packet->dst_port = sock->my_port;
437 sock->packet->proto = sock->proto;
441 * Grab the packet currently being processed.
443 packet = sock->packet;
445 rcode = fr_tcp_read_packet(packet, 0);
448 * Still only a partial packet. Put it back, and return,
449 * so that we'll read more data when it's ready.
455 if (rcode == -1) { /* error reading packet */
458 ERROR("Invalid packet from %s port %d, closing socket: %s",
459 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
460 packet->src_port, fr_strerror());
463 if (rcode < 0) { /* error or connection reset */
464 listener->status = RAD_LISTEN_STATUS_REMOVE_NOW;
467 * Tell the event handler that an FD has disappeared.
469 DEBUG("Client has closed connection");
470 event_new_fd(listener);
473 * Do NOT free the listener here. It's in use by
474 * a request, and will need to hang around until
475 * all of the requests are done.
477 * It is instead free'd in remove_from_request_hash()
483 * Some sanity checks, based on the packet code.
485 switch(packet->code) {
486 case PW_AUTHENTICATION_REQUEST:
487 if (listener->type != RAD_LISTEN_AUTH) goto bad_packet;
488 FR_STATS_INC(auth, total_requests);
489 fun = rad_authenticate;
492 #ifdef WITH_ACCOUNTING
493 case PW_ACCOUNTING_REQUEST:
494 if (listener->type != RAD_LISTEN_ACCT) goto bad_packet;
495 FR_STATS_INC(acct, total_requests);
496 fun = rad_accounting;
500 case PW_STATUS_SERVER:
501 if (!mainconfig.status_server) {
502 FR_STATS_INC(auth, total_unknown_types);
503 WDEBUG("Ignoring Status-Server request due to security configuration");
504 rad_free(&sock->packet);
507 fun = rad_status_server;
512 FR_STATS_INC(auth, total_unknown_types);
514 DEBUG("Invalid packet code %d sent from client %s port %d : IGNORED",
515 packet->code, client->shortname, packet->src_port);
516 rad_free(&sock->packet);
518 } /* switch over packet types */
520 if (!request_receive(listener, packet, client, fun)) {
521 FR_STATS_INC(auth, total_packets_dropped);
522 rad_free(&sock->packet);
526 sock->packet = NULL; /* we have no need for more partial reads */
530 static int dual_tcp_accept(rad_listen_t *listener)
535 struct sockaddr_storage src;
536 listen_socket_t *sock;
537 fr_ipaddr_t src_ipaddr;
538 RADCLIENT *client = NULL;
542 DEBUG2(" ... new connection request on TCP socket.");
544 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
547 * Non-blocking sockets must handle this.
550 if (errno == EWOULDBLOCK) {
555 DEBUG2(" ... failed to accept connection.");
559 if (!fr_sockaddr2ipaddr(&src, salen, &src_ipaddr, &src_port)) {
561 DEBUG2(" ... unknown address family.");
566 * Enforce client IP address checks on accept, not on
569 if ((client = client_listener_find(listener,
570 &src_ipaddr, src_port)) == NULL) {
572 FR_STATS_INC(auth, total_invalid_requests);
578 * Enforce security restrictions.
580 * This shouldn't be necessary in practice. However, it
581 * serves as a double-check on configurations. Marking a
582 * client as "tls required" means that any accidental
583 * exposure of the client to non-TLS traffic is
586 if (client->tls_required && !listener->tls) {
587 INFO("Ignoring connection to TLS socket from non-TLS client");
594 * Enforce max_connections on client && listen section.
596 if ((client->limit.max_connections != 0) &&
597 (client->limit.max_connections == client->limit.num_connections)) {
599 * FIXME: Print client IP/port, and server IP/port.
601 INFO("Ignoring new connection due to client max_connections (%d)", client->limit.max_connections);
606 sock = listener->data;
607 if ((sock->limit.max_connections != 0) &&
608 (sock->limit.max_connections == sock->limit.num_connections)) {
610 * FIXME: Print client IP/port, and server IP/port.
612 INFO("Ignoring new connection due to socket max_connections");
616 client->limit.num_connections++;
617 sock->limit.num_connections++;
620 * Add the new listener.
622 this = listen_alloc(listener, listener->type);
623 if (!this) return -1;
626 * Copy everything, including the pointer to the socket
630 memcpy(this->data, listener->data, sizeof(*sock));
631 memcpy(this, listener, sizeof(*this));
633 this->data = sock; /* fix it back */
635 sock->parent = listener->data;
636 sock->other_ipaddr = src_ipaddr;
637 sock->other_port = src_port;
638 sock->client = client;
639 sock->opened = sock->last_packet = time(NULL);
642 * Set the limits. The defaults are the parent limits.
643 * Client limits on max_connections are enforced dynamically.
644 * Set the MINIMUM of client/socket idle timeout or lifetime.
646 memcpy(&sock->limit, &sock->parent->limit, sizeof(sock->limit));
648 if (client->limit.idle_timeout &&
649 ((sock->limit.idle_timeout == 0) ||
650 (client->limit.idle_timeout < sock->limit.idle_timeout))) {
651 sock->limit.idle_timeout = client->limit.idle_timeout;
654 if (client->limit.lifetime &&
655 ((sock->limit.lifetime == 0) ||
656 (client->limit.lifetime < sock->limit.lifetime))) {
657 sock->limit.lifetime = client->limit.lifetime;
661 this->status = RAD_LISTEN_STATUS_INIT;
664 #ifdef WITH_COMMAND_SOCKET
665 if (this->type == RAD_LISTEN_COMMAND) {
666 this->recv = command_tcp_recv;
667 this->send = command_tcp_send;
668 command_write_magic(this->fd, sock);
673 this->recv = dual_tcp_recv;
677 this->recv = dual_tls_recv;
678 this->send = dual_tls_send;
684 * FIXME: set O_NONBLOCK on the accept'd fd.
685 * See djb's portability rants for details.
689 * Tell the event loop that we have a new FD.
690 * This can be called from a child thread...
699 * Ensure that we always keep the correct counters.
702 static void common_socket_free(rad_listen_t *this)
704 listen_socket_t *sock = this->data;
706 if (sock->proto != IPPROTO_TCP) return;
708 if (!sock->parent) return;
711 * Decrement the number of connections.
713 if (sock->parent->limit.num_connections > 0) {
714 sock->parent->limit.num_connections--;
716 if (sock->client->limit.num_connections > 0) {
717 sock->client->limit.num_connections--;
721 static void common_socket_free(UNUSED rad_listen_t *this)
728 * This function is stupid and complicated.
730 int common_socket_print(rad_listen_t const *this, char *buffer, size_t bufsize)
733 listen_socket_t *sock = this->data;
734 char const *name = master_listen[this->type].name;
736 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
737 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
745 if (sock->interface) {
746 ADDSTRING(" interface ");
747 ADDSTRING(sock->interface);
751 if (this->recv == dual_tcp_accept) {
752 ADDSTRING(" proto tcp");
758 * TCP sockets get printed a little differently, to make
759 * it clear what's going on.
762 ADDSTRING(" from client (");
763 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
767 snprintf(buffer, bufsize, "%d", sock->other_port);
771 if ((sock->my_ipaddr.af == AF_INET) &&
772 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
773 strlcpy(buffer, "*", bufsize);
775 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
780 snprintf(buffer, bufsize, "%d", sock->my_port);
784 ADDSTRING(", virtual-server=");
785 ADDSTRING(this->server);
795 * Maybe it's a socket that we opened to a home server.
797 if ((sock->proto == IPPROTO_TCP) &&
798 (this->type == RAD_LISTEN_PROXY)) {
800 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
804 snprintf(buffer, bufsize, "%d", sock->my_port);
806 ADDSTRING(") -> home_server (");
808 if ((sock->other_ipaddr.af == AF_INET) &&
809 (sock->other_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
810 strlcpy(buffer, "*", bufsize);
812 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
817 snprintf(buffer, bufsize, "%d", sock->other_port);
824 #endif /* WITH_PROXY */
825 #endif /* WITH_TCP */
827 ADDSTRING(" address ");
829 if ((sock->my_ipaddr.af == AF_INET) &&
830 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
831 strlcpy(buffer, "*", bufsize);
833 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
838 snprintf(buffer, bufsize, "%d", sock->my_port);
849 ADDSTRING(" as server ");
850 ADDSTRING(this->server);
859 extern bool check_config; /* radiusd.c */
861 static CONF_PARSER performance_config[] = {
862 { "skip_duplicate_checks", PW_TYPE_BOOLEAN,
863 offsetof(rad_listen_t, nodup), NULL, NULL },
865 { "synchronous", PW_TYPE_BOOLEAN,
866 offsetof(rad_listen_t, synchronous), NULL, NULL },
868 { "workers", PW_TYPE_INTEGER,
869 offsetof(rad_listen_t, workers), NULL, NULL },
871 { NULL, -1, 0, NULL, NULL } /* end the list */
875 static CONF_PARSER limit_config[] = {
876 { "max_pps", PW_TYPE_INTEGER,
877 offsetof(listen_socket_t, max_rate), NULL, NULL },
880 { "max_connections", PW_TYPE_INTEGER,
881 offsetof(listen_socket_t, limit.max_connections), NULL, "16" },
883 { "lifetime", PW_TYPE_INTEGER,
884 offsetof(listen_socket_t, limit.lifetime), NULL, "0" },
886 { "idle_timeout", PW_TYPE_INTEGER,
887 offsetof(listen_socket_t, limit.idle_timeout), NULL, "30" },
890 { NULL, -1, 0, NULL, NULL } /* end the list */
894 * Parse an authentication or accounting socket.
896 int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
901 listen_socket_t *sock = this->data;
902 char *section_name = NULL;
903 CONF_SECTION *client_cs, *parentcs;
911 memset(&ipaddr, 0, sizeof(ipaddr));
912 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
913 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
914 &ipaddr.ipaddr.ip4addr, NULL);
915 if (rcode < 0) return -1;
917 if (rcode == 0) { /* successfully parsed IPv4 */
920 } else { /* maybe IPv6? */
921 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
922 &ipaddr.ipaddr.ip6addr, NULL);
923 if (rcode < 0) return -1;
927 "No address specified in listen section");
930 ipaddr.af = AF_INET6;
933 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
935 if (rcode < 0) return -1;
937 if ((listen_port < 0) || (listen_port > 65535)) {
939 "Invalid value for \"port\"");
943 sock->proto = IPPROTO_UDP;
945 if (cf_pair_find(cs, "proto")) {
948 "System does not support the TCP protocol. Delete this line from the configuration file.");
956 rcode = cf_item_parse(cs, "proto", PW_TYPE_STRING_PTR,
958 if (rcode < 0) return -1;
960 if (strcmp(proto, "udp") == 0) {
961 sock->proto = IPPROTO_UDP;
963 } else if (strcmp(proto, "tcp") == 0) {
964 sock->proto = IPPROTO_TCP;
967 "Unknown proto name \"%s\"", proto);
972 * TCP requires a destination IP for sockets.
973 * UDP doesn't, so it's allowed.
976 if ((this->type == RAD_LISTEN_PROXY) &&
977 (sock->proto != IPPROTO_UDP)) {
979 "Proxy listeners can only listen on proto = udp");
982 #endif /* WITH_PROXY */
985 tls = cf_section_sub_find(cs, "tls");
988 * Don't allow TLS configurations for UDP sockets.
990 if (sock->proto != IPPROTO_TCP) {
992 "TLS transport is not available for UDP sockets.");
998 * FIXME: Make this better.
1000 if (listen_port == 0) listen_port = 2083;
1002 this->tls = tls_server_conf_parse(tls);
1007 #ifdef HAVE_PTRHEAD_H
1008 if (pthread_mutex_init(&sock->mutex, NULL) < 0) {
1016 #else /* WITH_TLS */
1018 * Built without TLS. Disallow it.
1020 if (cf_section_sub_find(cs, "tls")) {
1022 "TLS transport is not available in this executable.");
1025 #endif /* WITH_TLS */
1027 #endif /* WITH_TCP */
1030 * No "proto" field. Disallow TLS.
1032 } else if (cf_section_sub_find(cs, "tls")) {
1034 "TLS transport is not available in this \"listen\" section.");
1039 * Magical tuning methods!
1041 subcs = cf_section_sub_find(cs, "performance");
1043 rcode = cf_section_parse(subcs, this,
1044 performance_config);
1045 if (rcode < 0) return -1;
1047 if (this->synchronous && sock->max_rate) {
1048 WARN("Setting 'max_pps' is incompatible with 'synchronous'. Disabling 'max_pps'");
1052 if (!this->synchronous && this->workers) {
1053 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
1058 subcs = cf_section_sub_find(cs, "limit");
1060 rcode = cf_section_parse(subcs, sock,
1062 if (rcode < 0) return -1;
1064 if (sock->max_rate && ((sock->max_rate < 10) || (sock->max_rate > 1000000))) {
1066 "Invalid value for \"max_pps\"");
1071 if ((sock->limit.idle_timeout > 0) && (sock->limit.idle_timeout < 5)) {
1072 WARN("Setting idle_timeout to 5");
1073 sock->limit.idle_timeout = 5;
1076 if ((sock->limit.lifetime > 0) && (sock->limit.lifetime < 5)) {
1077 WARN("Setting lifetime to 5");
1078 sock->limit.lifetime = 5;
1081 if ((sock->limit.lifetime > 0) && (sock->limit.idle_timeout > sock->limit.lifetime)) {
1082 WARN("Setting idle_timeout to 0");
1083 sock->limit.idle_timeout = 0;
1087 * Force no duplicate detection for TCP sockets.
1089 if (sock->proto == IPPROTO_TCP) {
1094 sock->limit.max_connections = 60;
1095 sock->limit.idle_timeout = 30;
1096 sock->limit.lifetime = 0;
1100 sock->my_ipaddr = ipaddr;
1101 sock->my_port = listen_port;
1106 * Until there is a side effects free way of forwarding a
1107 * request to another virtual server, this check is invalid,
1108 * and should be left disabled.
1111 if (home_server_find(&sock->my_ipaddr, sock->my_port, sock->proto)) {
1114 EDEBUG("We have been asked to listen on %s port %d, which is also listed as a "
1115 "home server. This can create a proxy loop",
1116 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)), sock->my_port);
1120 return 0; /* don't do anything */
1125 * If we can bind to interfaces, do so,
1128 if (cf_pair_find(cs, "interface")) {
1130 CONF_PAIR *cp = cf_pair_find(cs, "interface");
1132 rad_assert(cp != NULL);
1133 value = cf_pair_value(cp);
1136 "No interface name given");
1139 sock->interface = value;
1144 * If we can do broadcasts..
1146 if (cf_pair_find(cs, "broadcast")) {
1147 #ifndef SO_BROADCAST
1149 "System does not support broadcast sockets. Delete this line from the configuration file.");
1153 CONF_PAIR *cp = cf_pair_find(cs, "broadcast");
1155 if (this->type != RAD_LISTEN_DHCP) {
1157 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file.");
1161 rad_assert(cp != NULL);
1162 value = cf_pair_value(cp);
1165 "No broadcast value given");
1170 * Hack... whatever happened to cf_section_parse?
1172 sock->broadcast = (strcmp(value, "yes") == 0);
1178 * And bind it to the port.
1180 if (listen_bind(this) < 0) {
1183 "Error binding to port for %s port %d",
1184 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
1191 * Proxy sockets don't have clients.
1193 if (this->type == RAD_LISTEN_PROXY) return 0;
1197 * The more specific configurations are preferred to more
1201 parentcs = cf_top_section(cs);
1202 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
1203 §ion_name, NULL);
1204 if (rcode < 0) return -1; /* bad string */
1207 * Explicit list given: use it.
1209 client_cs = cf_section_sub_find_name2(parentcs,
1213 client_cs = cf_section_find(section_name);
1217 "Failed to find clients %s {...}",
1221 } /* else there was no "clients = " entry. */
1224 CONF_SECTION *server_cs;
1226 server_cs = cf_section_sub_find_name2(parentcs,
1230 * Found a "server foo" section. If there are clients
1234 (cf_section_sub_find(server_cs, "client") != NULL)) {
1235 client_cs = server_cs;
1240 * Still nothing. Look for global clients.
1242 if (!client_cs) client_cs = parentcs;
1245 sock->clients = clients_parse_section(client_cs, (this->tls != NULL));
1247 sock->clients = clients_parse_section(client_cs, false);
1249 if (!sock->clients) {
1251 "Failed to load clients for this listen section");
1256 if (sock->proto == IPPROTO_TCP) {
1258 * Re-write the listener receive function to
1259 * allow us to accept the socket.
1261 this->recv = dual_tcp_accept;
1269 * Send an authentication response packet
1271 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1273 rad_assert(request->listener == listener);
1274 rad_assert(listener->send == auth_socket_send);
1276 #ifdef WITH_UDPFROMTO
1278 * Overwrite the src ip address on the outbound packet
1279 * with the one specified by the client.
1280 * This is useful to work around broken DSR implementations
1281 * and other routing issues.
1283 if (request->client->src_ipaddr.af != AF_UNSPEC) {
1284 request->reply->src_ipaddr = request->client->src_ipaddr;
1288 if (rad_send(request->reply, request->packet,
1289 request->client->secret) < 0) {
1290 RERROR("Failed sending reply: %s",
1299 #ifdef WITH_ACCOUNTING
1301 * Send an accounting response packet (or not)
1303 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1305 rad_assert(request->listener == listener);
1306 rad_assert(listener->send == acct_socket_send);
1309 * Accounting reject's are silently dropped.
1311 * We do it here to avoid polluting the rest of the
1312 * code with this knowledge
1314 if (request->reply->code == 0) return 0;
1316 #ifdef WITH_UDPFROMTO
1318 * Overwrite the src ip address on the outbound packet
1319 * with the one specified by the client.
1320 * This is useful to work around broken DSR implementations
1321 * and other routing issues.
1323 if (request->client->src_ipaddr.af != AF_UNSPEC) {
1324 request->reply->src_ipaddr = request->client->src_ipaddr;
1328 if (rad_send(request->reply, request->packet,
1329 request->client->secret) < 0) {
1330 RERROR("Failed sending reply: %s",
1341 * Send a packet to a home server.
1343 * FIXME: have different code for proxy auth & acct!
1345 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1347 rad_assert(request->proxy_listener == listener);
1348 rad_assert(listener->send == proxy_socket_send);
1350 if (rad_send(request->proxy, NULL,
1351 request->home_server->secret) < 0) {
1352 RERROR("Failed sending proxied request: %s",
1363 * Check if an incoming request is "ok"
1365 * It takes packets, not requests. It sees if the packet looks
1366 * OK. If so, it does a number of sanity checks on it.
1368 static int stats_socket_recv(rad_listen_t *listener)
1372 RADIUS_PACKET *packet;
1373 RADCLIENT *client = NULL;
1374 fr_ipaddr_t src_ipaddr;
1376 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1377 if (rcode < 0) return 0;
1379 FR_STATS_INC(auth, total_requests);
1381 if (rcode < 20) { /* AUTH_HDR_LEN */
1382 FR_STATS_INC(auth, total_malformed_requests);
1386 if ((client = client_listener_find(listener,
1387 &src_ipaddr, src_port)) == NULL) {
1388 rad_recv_discard(listener->fd);
1389 FR_STATS_INC(auth, total_invalid_requests);
1393 FR_STATS_TYPE_INC(client->auth.total_requests);
1396 * We only understand Status-Server on this socket.
1398 if (code != PW_STATUS_SERVER) {
1399 DEBUG("Ignoring packet code %d sent to Status-Server port",
1401 rad_recv_discard(listener->fd);
1402 FR_STATS_INC(auth, total_unknown_types);
1407 * Now that we've sanity checked everything, receive the
1410 packet = rad_recv(listener->fd, 1); /* require message authenticator */
1412 FR_STATS_INC(auth, total_malformed_requests);
1413 DEBUG("%s", fr_strerror());
1417 if (!request_receive(listener, packet, client, rad_status_server)) {
1418 FR_STATS_INC(auth, total_packets_dropped);
1429 * Check if an incoming request is "ok"
1431 * It takes packets, not requests. It sees if the packet looks
1432 * OK. If so, it does a number of sanity checks on it.
1434 static int auth_socket_recv(rad_listen_t *listener)
1438 RADIUS_PACKET *packet;
1439 RAD_REQUEST_FUNP fun = NULL;
1440 RADCLIENT *client = NULL;
1441 fr_ipaddr_t src_ipaddr;
1443 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1444 if (rcode < 0) return 0;
1446 FR_STATS_INC(auth, total_requests);
1448 if (rcode < 20) { /* AUTH_HDR_LEN */
1449 FR_STATS_INC(auth, total_malformed_requests);
1453 if ((client = client_listener_find(listener,
1454 &src_ipaddr, src_port)) == NULL) {
1455 rad_recv_discard(listener->fd);
1456 FR_STATS_INC(auth, total_invalid_requests);
1460 FR_STATS_TYPE_INC(client->auth.total_requests);
1463 * Some sanity checks, based on the packet code.
1466 case PW_AUTHENTICATION_REQUEST:
1467 fun = rad_authenticate;
1470 case PW_STATUS_SERVER:
1471 if (!mainconfig.status_server) {
1472 rad_recv_discard(listener->fd);
1473 FR_STATS_INC(auth, total_unknown_types);
1474 WDEBUG("Ignoring Status-Server request due to security configuration");
1477 fun = rad_status_server;
1481 rad_recv_discard(listener->fd);
1482 FR_STATS_INC(auth,total_unknown_types);
1484 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
1485 code, client->shortname, src_port);
1488 } /* switch over packet types */
1491 * Now that we've sanity checked everything, receive the
1494 packet = rad_recv(listener->fd, client->message_authenticator);
1496 FR_STATS_INC(auth, total_malformed_requests);
1497 DEBUG("%s", fr_strerror());
1502 #ifdef WITH_UDPFROMTO
1504 * This is a NICE Mac OSX bug. Create an interface with
1505 * two IP address, and then configure one listener for
1506 * each IP address. Send thousands of packets to one
1507 * address, and some will show up on the OTHER socket.
1509 * This hack works ONLY if the clients are global. If
1510 * each listener has the same client IP, but with
1511 * different secrets, then it will fail the rad_recv()
1512 * check above, and there's nothing you can do.
1515 listen_socket_t *sock = listener->data;
1516 rad_listen_t *other;
1518 other = listener_find_byipaddr(&packet->dst_ipaddr,
1519 packet->dst_port, sock->proto);
1520 if (other) listener = other;
1526 if (!request_receive(listener, packet, client, fun)) {
1527 FR_STATS_INC(auth, total_packets_dropped);
1536 #ifdef WITH_ACCOUNTING
1538 * Receive packets from an accounting socket
1540 static int acct_socket_recv(rad_listen_t *listener)
1544 RADIUS_PACKET *packet;
1545 RAD_REQUEST_FUNP fun = NULL;
1546 RADCLIENT *client = NULL;
1547 fr_ipaddr_t src_ipaddr;
1549 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1550 if (rcode < 0) return 0;
1552 FR_STATS_INC(acct, total_requests);
1554 if (rcode < 20) { /* AUTH_HDR_LEN */
1555 FR_STATS_INC(acct, total_malformed_requests);
1559 if ((client = client_listener_find(listener,
1560 &src_ipaddr, src_port)) == NULL) {
1561 rad_recv_discard(listener->fd);
1562 FR_STATS_INC(acct, total_invalid_requests);
1566 FR_STATS_TYPE_INC(client->acct.total_requests);
1569 * Some sanity checks, based on the packet code.
1572 case PW_ACCOUNTING_REQUEST:
1573 fun = rad_accounting;
1576 case PW_STATUS_SERVER:
1577 if (!mainconfig.status_server) {
1578 rad_recv_discard(listener->fd);
1579 FR_STATS_INC(acct, total_unknown_types);
1581 WDEBUG("Ignoring Status-Server request due to security configuration");
1584 fun = rad_status_server;
1588 rad_recv_discard(listener->fd);
1589 FR_STATS_INC(acct, total_unknown_types);
1591 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1592 code, client->shortname, src_port);
1594 } /* switch over packet types */
1597 * Now that we've sanity checked everything, receive the
1600 packet = rad_recv(listener->fd, 0);
1602 FR_STATS_INC(acct, total_malformed_requests);
1603 ERROR("%s", fr_strerror());
1608 * There can be no duplicate accounting packets.
1610 if (!request_receive(listener, packet, client, fun)) {
1611 FR_STATS_INC(acct, total_packets_dropped);
1622 static int do_proxy(REQUEST *request)
1626 if (request->in_proxy_hash ||
1627 (request->proxy_reply && (request->proxy_reply->code != 0))) {
1631 vp = pairfind(request->config_items, PW_HOME_SERVER_POOL, 0, TAG_ANY);
1634 if (!home_pool_byname(vp->vp_strvalue, HOME_TYPE_COA)) {
1635 REDEBUG2("Cannot proxy to unknown pool %s",
1644 * Receive a CoA packet.
1646 static int rad_coa_recv(REQUEST *request)
1648 int rcode = RLM_MODULE_OK;
1653 * Get the correct response
1655 switch (request->packet->code) {
1656 case PW_COA_REQUEST:
1661 case PW_DISCONNECT_REQUEST:
1662 ack = PW_DISCONNECT_ACK;
1663 nak = PW_DISCONNECT_NAK;
1666 default: /* shouldn't happen */
1667 return RLM_MODULE_FAIL;
1671 #define WAS_PROXIED (request->proxy)
1673 #define WAS_PROXIED (0)
1678 * RFC 5176 Section 3.3. If we have a CoA-Request
1679 * with Service-Type = Authorize-Only, it MUST
1680 * have a State attribute in it.
1682 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE, 0, TAG_ANY);
1683 if (request->packet->code == PW_COA_REQUEST) {
1684 if (vp && (vp->vp_integer == 17)) {
1685 vp = pairfind(request->packet->vps, PW_STATE, 0, TAG_ANY);
1686 if (!vp || (vp->length == 0)) {
1687 REDEBUG("CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1688 request->reply->code = PW_COA_NAK;
1689 return RLM_MODULE_FAIL;
1694 * RFC 5176, Section 3.2.
1696 REDEBUG("Disconnect-Request MUST NOT contain a Service-Type attribute");
1697 request->reply->code = PW_DISCONNECT_NAK;
1698 return RLM_MODULE_FAIL;
1701 rcode = process_recv_coa(0, request);
1703 case RLM_MODULE_FAIL:
1704 case RLM_MODULE_INVALID:
1705 case RLM_MODULE_REJECT:
1706 case RLM_MODULE_USERLOCK:
1708 request->reply->code = nak;
1711 case RLM_MODULE_HANDLED:
1714 case RLM_MODULE_NOOP:
1715 case RLM_MODULE_NOTFOUND:
1717 case RLM_MODULE_UPDATED:
1718 if (do_proxy(request)) return RLM_MODULE_OK;
1719 request->reply->code = ack;
1722 } else if (request->proxy_reply) {
1724 * Start the reply code with the proxy reply
1727 request->reply->code = request->proxy_reply->code;
1731 * Copy State from the request to the reply.
1732 * See RFC 5176 Section 3.3.
1734 vp = paircopy2(request->reply, request->packet->vps, PW_STATE, 0, TAG_ANY);
1735 if (vp) pairadd(&request->reply->vps, vp);
1738 * We may want to over-ride the reply.
1740 if (request->reply->code) {
1741 rcode = process_send_coa(0, request);
1744 * We need to send CoA-NAK back if Service-Type
1745 * is Authorize-Only. Rely on the user's policy
1746 * to do that. We're not a real NAS, so this
1747 * restriction doesn't (ahem) apply to us.
1749 case RLM_MODULE_FAIL:
1750 case RLM_MODULE_INVALID:
1751 case RLM_MODULE_REJECT:
1752 case RLM_MODULE_USERLOCK:
1755 * Over-ride an ACK with a NAK
1757 request->reply->code = nak;
1760 case RLM_MODULE_HANDLED:
1763 case RLM_MODULE_NOOP:
1764 case RLM_MODULE_NOTFOUND:
1766 case RLM_MODULE_UPDATED:
1768 * Do NOT over-ride a previously set value.
1769 * Otherwise an "ok" here will re-write a
1772 if (request->reply->code == 0) {
1773 request->reply->code = ack;
1779 return RLM_MODULE_OK;
1784 * Check if an incoming request is "ok"
1786 * It takes packets, not requests. It sees if the packet looks
1787 * OK. If so, it does a number of sanity checks on it.
1789 static int coa_socket_recv(rad_listen_t *listener)
1793 RADIUS_PACKET *packet;
1794 RAD_REQUEST_FUNP fun = NULL;
1795 RADCLIENT *client = NULL;
1796 fr_ipaddr_t src_ipaddr;
1798 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1799 if (rcode < 0) return 0;
1801 if (rcode < 20) { /* AUTH_HDR_LEN */
1802 FR_STATS_INC(coa, total_requests);
1803 FR_STATS_INC(coa, total_malformed_requests);
1807 if ((client = client_listener_find(listener,
1808 &src_ipaddr, src_port)) == NULL) {
1809 rad_recv_discard(listener->fd);
1810 FR_STATS_INC(coa, total_requests);
1811 FR_STATS_INC(coa, total_invalid_requests);
1816 * Some sanity checks, based on the packet code.
1819 case PW_COA_REQUEST:
1820 FR_STATS_INC(coa, total_requests);
1824 case PW_DISCONNECT_REQUEST:
1825 FR_STATS_INC(dsc, total_requests);
1830 rad_recv_discard(listener->fd);
1831 FR_STATS_INC(coa, total_unknown_types);
1832 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1833 code, client->shortname, src_port);
1835 } /* switch over packet types */
1838 * Now that we've sanity checked everything, receive the
1841 packet = rad_recv(listener->fd, client->message_authenticator);
1843 FR_STATS_INC(coa, total_malformed_requests);
1844 DEBUG("%s", fr_strerror());
1848 if (!request_receive(listener, packet, client, fun)) {
1849 FR_STATS_INC(coa, total_packets_dropped);
1860 * Recieve packets from a proxy socket.
1862 static int proxy_socket_recv(rad_listen_t *listener)
1864 RADIUS_PACKET *packet;
1867 packet = rad_recv(listener->fd, 0);
1869 ERROR("%s", fr_strerror());
1874 * FIXME: Client MIB updates?
1876 switch(packet->code) {
1877 case PW_AUTHENTICATION_ACK:
1878 case PW_ACCESS_CHALLENGE:
1879 case PW_AUTHENTICATION_REJECT:
1882 #ifdef WITH_ACCOUNTING
1883 case PW_ACCOUNTING_RESPONSE:
1888 case PW_DISCONNECT_ACK:
1889 case PW_DISCONNECT_NAK:
1897 * FIXME: Update MIB for packet types?
1899 ERROR("Invalid packet code %d sent to a proxy port "
1900 "from home server %s port %d - ID %d : IGNORED",
1902 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1903 packet->src_port, packet->id);
1908 if (!request_proxy_reply(packet)) {
1918 * Recieve packets from a proxy socket.
1920 static int proxy_socket_tcp_recv(rad_listen_t *listener)
1922 RADIUS_PACKET *packet;
1923 listen_socket_t *sock = listener->data;
1926 packet = fr_tcp_recv(listener->fd, 0);
1928 listener->status = RAD_LISTEN_STATUS_REMOVE_NOW;
1929 event_new_fd(listener);
1934 * FIXME: Client MIB updates?
1936 switch(packet->code) {
1937 case PW_AUTHENTICATION_ACK:
1938 case PW_ACCESS_CHALLENGE:
1939 case PW_AUTHENTICATION_REJECT:
1942 #ifdef WITH_ACCOUNTING
1943 case PW_ACCOUNTING_RESPONSE:
1949 * FIXME: Update MIB for packet types?
1951 ERROR("Invalid packet code %d sent to a proxy port "
1952 "from home server %s port %d - ID %d : IGNORED",
1954 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1955 packet->src_port, packet->id);
1960 packet->src_ipaddr = sock->other_ipaddr;
1961 packet->src_port = sock->other_port;
1962 packet->dst_ipaddr = sock->my_ipaddr;
1963 packet->dst_port = sock->my_port;
1966 * FIXME: Have it return an indication of packets that
1967 * are OK to ignore (dups, too late), versus ones that
1968 * aren't OK to ignore (unknown response, spoofed, etc.)
1970 * Close the socket on bad packets...
1972 if (!request_proxy_reply(packet)) {
1977 sock->opened = sock->last_packet = time(NULL);
1985 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1987 if (!request->reply->code) return 0;
1989 if (rad_encode(request->reply, request->packet,
1990 request->client->secret) < 0) {
1991 RERROR("Failed encoding packet: %s",
1996 if (rad_sign(request->reply, request->packet,
1997 request->client->secret) < 0) {
1998 RERROR("Failed signing packet: %s",
2007 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
2009 if (rad_verify(request->packet, NULL,
2010 request->client->secret) < 0) {
2014 return rad_decode(request->packet, NULL,
2015 request->client->secret);
2019 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
2021 if (rad_encode(request->proxy, NULL, request->home_server->secret) < 0) {
2022 RERROR("Failed encoding proxied packet: %s",
2027 if (rad_sign(request->proxy, NULL, request->home_server->secret) < 0) {
2028 RERROR("Failed signing proxied packet: %s",
2037 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
2040 * rad_verify is run in event.c, received_proxy_response()
2043 return rad_decode(request->proxy_reply, request->proxy,
2044 request->home_server->secret);
2048 #include "command.c"
2051 * Temporarily NOT const!
2053 static fr_protocol_t master_listen[RAD_LISTEN_MAX] = {
2055 { RLM_MODULE_INIT, "status", sizeof(listen_socket_t), NULL,
2056 common_socket_parse, NULL,
2057 stats_socket_recv, auth_socket_send,
2058 common_socket_print, client_socket_encode, client_socket_decode },
2061 * This always gets defined.
2063 { RLM_MODULE_INIT, "status", 0, NULL,
2064 NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
2069 { RLM_MODULE_INIT, "proxy", sizeof(listen_socket_t), NULL,
2070 common_socket_parse, NULL,
2071 proxy_socket_recv, proxy_socket_send,
2072 common_socket_print, proxy_socket_encode, proxy_socket_decode },
2075 /* authentication */
2076 { RLM_MODULE_INIT, "auth", sizeof(listen_socket_t), NULL,
2077 common_socket_parse, common_socket_free,
2078 auth_socket_recv, auth_socket_send,
2079 common_socket_print, client_socket_encode, client_socket_decode },
2081 #ifdef WITH_ACCOUNTING
2083 { RLM_MODULE_INIT, "acct", sizeof(listen_socket_t), NULL,
2084 common_socket_parse, common_socket_free,
2085 acct_socket_recv, acct_socket_send,
2086 common_socket_print, client_socket_encode, client_socket_decode},
2091 { RLM_MODULE_INIT, "detail", sizeof(listen_detail_t), NULL,
2092 detail_parse, detail_free,
2093 detail_recv, detail_send,
2094 detail_print, detail_encode, detail_decode },
2098 /* vlan query protocol */
2099 { 0, "vmps", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
2103 /* dhcp query protocol */
2104 { 0, "dhcp", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
2107 #ifdef WITH_COMMAND_SOCKET
2108 /* TCP command socket */
2109 { RLM_MODULE_INIT, "control", sizeof(fr_command_socket_t), NULL,
2110 command_socket_parse, command_socket_free,
2111 command_domain_accept, command_domain_send,
2112 command_socket_print, command_socket_encode, command_socket_decode },
2116 /* Change of Authorization */
2117 { RLM_MODULE_INIT, "coa", sizeof(listen_socket_t), NULL,
2118 common_socket_parse, NULL,
2119 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
2120 common_socket_print, client_socket_encode, client_socket_decode },
2128 * Binds a listener to a socket.
2130 static int listen_bind(rad_listen_t *this)
2133 struct sockaddr_storage salocal;
2135 listen_socket_t *sock = this->data;
2137 #define proto_for_port "udp"
2138 #define sock_type SOCK_DGRAM
2140 char const *proto_for_port = "udp";
2141 int sock_type = SOCK_DGRAM;
2143 if (sock->proto == IPPROTO_TCP) {
2145 if (this->type == RAD_LISTEN_VQP) {
2146 ERROR("VQP does not support TCP transport");
2151 proto_for_port = "tcp";
2152 sock_type = SOCK_STREAM;
2157 * If the port is zero, then it means the appropriate
2158 * thing from /etc/services.
2160 if (sock->my_port == 0) {
2161 struct servent *svp;
2163 switch (this->type) {
2164 case RAD_LISTEN_AUTH:
2165 svp = getservbyname ("radius", proto_for_port);
2167 sock->my_port = ntohs(svp->s_port);
2169 sock->my_port = PW_AUTH_UDP_PORT;
2173 #ifdef WITH_ACCOUNTING
2174 case RAD_LISTEN_ACCT:
2175 svp = getservbyname ("radacct", proto_for_port);
2177 sock->my_port = ntohs(svp->s_port);
2179 sock->my_port = PW_ACCT_UDP_PORT;
2185 case RAD_LISTEN_PROXY:
2186 /* leave it at zero */
2191 case RAD_LISTEN_VQP:
2192 sock->my_port = 1589;
2196 #ifdef WITH_COMMAND_SOCKET
2197 case RAD_LISTEN_COMMAND:
2198 sock->my_port = PW_RADMIN_PORT;
2203 case RAD_LISTEN_COA:
2204 svp = getservbyname ("radius-dynauth", "udp");
2206 sock->my_port = ntohs(svp->s_port);
2208 sock->my_port = PW_COA_UDP_PORT;
2214 WDEBUG("Internal sanity check failed in binding to socket. Ignoring problem.");
2220 * Don't open sockets if we're checking the config.
2228 * Copy fr_socket() here, as we may need to bind to a device.
2230 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
2234 this->print(this, buffer, sizeof(buffer));
2236 ERROR("Failed opening %s: %s", buffer, fr_syserror(errno));
2242 * We don't want child processes inheriting these
2245 rcode = fcntl(this->fd, F_GETFD);
2247 if (fcntl(this->fd, F_SETFD, rcode | FD_CLOEXEC) < 0) {
2249 ERROR("Failed setting close on exec: %s", fr_syserror(errno));
2256 * Bind to a device BEFORE touching IP addresses.
2258 if (sock->interface) {
2259 #ifdef SO_BINDTODEVICE
2262 memset(&ifreq, 0, sizeof(ifreq));
2263 strlcpy(ifreq.ifr_name, sock->interface, sizeof(ifreq.ifr_name));
2266 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
2267 (char *)&ifreq, sizeof(ifreq));
2271 ERROR("Failed binding to interface %s: %s",
2272 sock->interface, fr_syserror(errno));
2274 } /* else it worked. */
2276 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2277 #ifdef HAVE_NET_IF_H
2279 * Odds are that any system supporting "bind to
2280 * device" also supports IPv6, so this next bit
2281 * isn't necessary. But it's here for
2284 * If we're doing IPv6, and the scope hasn't yet
2285 * been defined, set the scope to the scope of
2288 if (sock->my_ipaddr.af == AF_INET6) {
2289 if (sock->my_ipaddr.scope == 0) {
2290 sock->my_ipaddr.scope = if_nametoindex(sock->interface);
2291 if (sock->my_ipaddr.scope == 0) {
2293 ERROR("Failed finding interface %s: %s",
2294 sock->interface, fr_syserror(errno));
2297 } /* else scope was defined: we're OK. */
2302 * IPv4: no link local addresses,
2303 * and no bind to device.
2307 ERROR("Failed binding to interface %s: \"bind to device\" is unsupported", sock->interface);
2314 if (sock->proto == IPPROTO_TCP) {
2317 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2319 ERROR("Failed to reuse address: %s", fr_syserror(errno));
2325 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
2326 else /* UDP sockets get UDPfromto */
2329 #ifdef WITH_UDPFROMTO
2331 * Initialize udpfromto for all sockets.
2333 if (udpfromto_init(this->fd) != 0) {
2334 ERROR("Failed initializing udpfromto: %s",
2335 fr_syserror(errno));
2342 * Set up sockaddr stuff.
2344 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
2349 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2350 if (sock->my_ipaddr.af == AF_INET6) {
2352 * Listening on '::' does NOT get you IPv4 to
2353 * IPv6 mapping. You've got to listen on an IPv4
2354 * address, too. This makes the rest of the server
2355 * design a little simpler.
2359 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2362 if (setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2363 (char *)&on, sizeof(on)) < 0) {
2364 ERROR("Failed setting socket to IPv6 "
2365 "only: %s", fr_syserror(errno));
2371 #endif /* IPV6_V6ONLY */
2373 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2375 if (sock->my_ipaddr.af == AF_INET) {
2378 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2380 * Disable PMTU discovery. On Linux, this
2381 * also makes sure that the "don't fragment"
2384 flag = IP_PMTUDISC_DONT;
2385 if (setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2386 &flag, sizeof(flag)) < 0) {
2387 ERROR("Failed disabling PMTU discovery: %s",
2388 fr_syserror(errno));
2395 #if defined(IP_DONTFRAG)
2397 * Ensure that the "don't fragment" flag is zero.
2400 if (setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2401 &flag, sizeof(flag)) < 0) {
2402 ERROR("Failed setting don't fragment flag: %s",
2403 fr_syserror(errno));
2413 if (sock->broadcast) {
2416 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2417 ERROR("Can't set broadcast option: %s",
2418 fr_syserror(errno));
2426 * May be binding to priviledged ports.
2428 if (sock->my_port != 0) {
2432 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2433 ERROR("Can't set re-use address option: %s",
2434 fr_syserror(errno));
2440 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2446 this->print(this, buffer, sizeof(buffer));
2447 ERROR("Failed binding to %s: %s\n",
2448 buffer, fr_syserror(errno));
2453 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2454 * kernel instead binds us to a 1.2.3.4. If this
2455 * happens, notice, and remember our real IP.
2458 struct sockaddr_storage src;
2459 socklen_t sizeof_src = sizeof(src);
2461 memset(&src, 0, sizeof_src);
2462 if (getsockname(this->fd, (struct sockaddr *) &src,
2464 ERROR("Failed getting socket name: %s",
2465 fr_syserror(errno));
2469 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2470 &sock->my_ipaddr, &sock->my_port)) {
2471 ERROR("Socket has unsupported address family");
2478 if (sock->proto == IPPROTO_TCP) {
2480 * Allow a backlog of 8 listeners
2482 if (listen(this->fd, 8) < 0) {
2484 ERROR("Failed in listen(): %s", fr_syserror(errno));
2489 * If there are hard-coded worker threads, they're blocking.
2491 * Otherwise, they're non-blocking.
2493 if (!this->workers && fr_nonblock(this->fd) < 0) {
2495 ERROR("Failed setting non-blocking on socket: %s",
2496 fr_syserror(errno));
2503 * Mostly for proxy sockets.
2505 sock->other_ipaddr.af = sock->my_ipaddr.af;
2508 * Don't screw up other people.
2510 #undef proto_for_port
2517 static int listener_free(void *ctx)
2521 this = talloc_get_type_abort(ctx, rad_listen_t);
2524 * Other code may have eaten the FD.
2526 if (this->fd >= 0) close(this->fd);
2528 if (master_listen[this->type].free) {
2529 master_listen[this->type].free(this);
2533 if ((this->type == RAD_LISTEN_AUTH)
2535 || (this->type == RAD_LISTEN_ACCT)
2538 || (this->type == RAD_LISTEN_PROXY)
2540 #ifdef WITH_COMMAND_SOCKET
2541 || ((this->type == RAD_LISTEN_COMMAND) &&
2542 (((fr_command_socket_t *) this->data)->magic != COMMAND_SOCKET_MAGIC))
2545 listen_socket_t *sock = this->data;
2547 rad_free(&sock->packet);
2551 if (sock->ssn) session_free(sock->ssn);
2552 request_free(&sock->request);
2553 #ifdef HAVE_PTHREAD_H
2554 pthread_mutex_destroy(&(sock->mutex));
2557 #endif /* WITH_TLS */
2559 #endif /* WITH_TCP */
2565 * Allocate & initialize a new listener.
2567 static rad_listen_t *listen_alloc(TALLOC_CTX *ctx, RAD_LISTEN_TYPE type)
2571 this = talloc_zero(ctx, rad_listen_t);
2574 this->recv = master_listen[this->type].recv;
2575 this->send = master_listen[this->type].send;
2576 this->print = master_listen[this->type].print;
2577 this->encode = master_listen[this->type].encode;
2578 this->decode = master_listen[this->type].decode;
2580 talloc_set_destructor((void *) this, listener_free);
2582 this->data = talloc_zero_array(this, uint8_t, master_listen[this->type].inst_size);
2589 * Externally visible function for creating a new proxy LISTENER.
2591 * Not thread-safe, but all calls to it are protected by the
2592 * proxy mutex in event.c
2594 rad_listen_t *proxy_new_listener(home_server_t *home, int src_port)
2598 listen_socket_t *sock;
2601 if (!home) return NULL;
2603 if ((home->limit.max_connections > 0) &&
2604 (home->limit.num_connections >= home->limit.max_connections)) {
2605 WDEBUG("Home server has too many open connections (%d)",
2606 home->limit.max_connections);
2611 if (home->last_failed_open == now) {
2612 WDEBUG("Suppressing attempt to open socket to 'down' home server");
2616 this = listen_alloc(mainconfig.config, RAD_LISTEN_PROXY);
2619 sock->other_ipaddr = home->ipaddr;
2620 sock->other_port = home->port;
2623 sock->my_ipaddr = home->src_ipaddr;
2624 sock->my_port = src_port;
2625 sock->proto = home->proto;
2627 if (debug_flag >= 2) {
2628 this->print(this, buffer, sizeof(buffer));
2629 DEBUG("Opening new %s", buffer);
2633 sock->opened = sock->last_packet = now;
2635 if (home->proto == IPPROTO_TCP) {
2636 this->recv = proxy_socket_tcp_recv;
2639 * FIXME: connect() is blocking!
2640 * We do this with the proxy mutex locked, which may
2641 * cause large delays!
2643 * http://www.developerweb.net/forum/showthread.php?p=13486
2645 this->fd = fr_tcp_client_socket(&home->src_ipaddr,
2646 &home->ipaddr, home->port);
2648 DEBUG("Trying SSL to port %d\n", home->port);
2649 sock->ssn = tls_new_client_session(home->tls, this->fd);
2651 ERROR("Failed starting SSL to %s", buffer);
2652 home->last_failed_open = now;
2657 this->recv = proxy_tls_recv;
2658 this->send = proxy_tls_send;
2662 this->fd = fr_socket(&home->src_ipaddr, src_port);
2665 this->print(this, buffer,sizeof(buffer));
2666 ERROR("Failed opening client socket %s : %s",
2667 buffer, fr_strerror());
2668 home->last_failed_open = now;
2674 * Figure out which port we were bound to.
2676 if (sock->my_port == 0) {
2677 struct sockaddr_storage src;
2678 socklen_t sizeof_src = sizeof(src);
2680 memset(&src, 0, sizeof_src);
2681 if (getsockname(this->fd, (struct sockaddr *) &src,
2683 ERROR("Failed getting socket name: %s",
2684 fr_syserror(errno));
2685 home->last_failed_open = now;
2690 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2691 &sock->my_ipaddr, &sock->my_port)) {
2692 ERROR("Socket has unsupported address family");
2693 home->last_failed_open = now;
2704 static const FR_NAME_NUMBER listen_compare[] = {
2706 { "status", RAD_LISTEN_NONE },
2708 { "auth", RAD_LISTEN_AUTH },
2709 #ifdef WITH_ACCOUNTING
2710 { "acct", RAD_LISTEN_ACCT },
2711 { "auth+acct", RAD_LISTEN_AUTH },
2714 { "detail", RAD_LISTEN_DETAIL },
2717 { "proxy", RAD_LISTEN_PROXY },
2720 { "vmps", RAD_LISTEN_VQP },
2723 { "dhcp", RAD_LISTEN_DHCP },
2725 #ifdef WITH_COMMAND_SOCKET
2726 { "control", RAD_LISTEN_COMMAND },
2729 { "coa", RAD_LISTEN_COA },
2735 static rad_listen_t *listen_parse(CONF_SECTION *cs, char const *server)
2745 cp = cf_pair_find(cs, "type");
2748 "No type specified in listen section");
2752 value = cf_pair_value(cp);
2755 "Type cannot be empty");
2759 snprintf(buffer, sizeof(buffer), "proto_%s", value);
2760 handle = lt_dlopenext(buffer);
2762 fr_protocol_t *proto;
2764 proto = dlsym(handle, buffer);
2767 "Failed linking to protocol %s : %s\n",
2773 type = fr_str2int(listen_compare, value, -1);
2774 rad_assert(type >= 0); /* shouldn't be able to compile an invalid type */
2776 memcpy(&master_listen[type], proto, sizeof(*proto));
2779 * And throw away the handle.
2780 * @todo: fix it later
2783 if (master_listen[type].magic != RLM_MODULE_INIT) {
2784 ERROR("Failed to load protocol '%s' due to internal sanity check problem",
2785 master_listen[type].name);
2790 cf_log_info(cs, "listen {");
2793 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
2795 if (rcode < 0) return NULL;
2798 "No type specified in listen section");
2802 type = fr_str2int(listen_compare, listen_type, -1);
2805 "Invalid type \"%s\" in listen section.",
2811 * Allow listen sections in the default config to
2812 * refer to a server.
2815 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
2817 if (rcode == 1) { /* compatiblity with 2.0-pre */
2818 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
2821 if (rcode < 0) return NULL;
2826 * We were passed a virtual server, so the caller is
2827 * defining a proxy listener inside of a virtual server.
2828 * This isn't allowed right now.
2830 else if (type == RAD_LISTEN_PROXY) {
2831 ERROR("Error: listen type \"proxy\" Cannot appear in a virtual server section");
2837 * Set up cross-type data.
2839 this = listen_alloc(cs, type);
2840 this->server = server;
2845 * Special-case '+' for "auth+acct".
2847 if (strchr(listen_type, '+') != NULL) {
2853 * Call per-type parser.
2855 if (master_listen[type].parse(cs, this) < 0) {
2860 cf_log_info(cs, "}");
2866 static int is_loopback(fr_ipaddr_t const *ipaddr)
2869 * We shouldn't proxy on loopback.
2871 if ((ipaddr->af == AF_INET) &&
2872 (ipaddr->ipaddr.ip4addr.s_addr == htonl(INADDR_LOOPBACK))) {
2876 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2877 if ((ipaddr->af == AF_INET6) &&
2878 (IN6_IS_ADDR_LINKLOCAL(&ipaddr->ipaddr.ip6addr))) {
2888 #ifdef HAVE_PTHREAD_H
2890 * A child thread which does NOTHING other than read and process
2893 static void *recv_thread(void *arg)
2895 rad_listen_t *this = arg;
2908 * Generate a list of listeners. Takes an input list of
2909 * listeners, too, so we don't close sockets with waiting packets.
2911 int listen_init(CONF_SECTION *config, rad_listen_t **head,
2915 UNUSED int spawn_flag
2920 int override = false;
2921 CONF_SECTION *cs = NULL;
2922 rad_listen_t **last;
2924 fr_ipaddr_t server_ipaddr;
2927 int defined_proxy = 0;
2931 * We shouldn't be called with a pre-existing list.
2933 rad_assert(head && (*head == NULL));
2935 memset(&server_ipaddr, 0, sizeof(server_ipaddr));
2938 server_ipaddr.af = AF_UNSPEC;
2941 * If the port is specified on the command-line,
2942 * it over-rides the configuration file.
2944 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
2946 if (mainconfig.port >= 0) {
2947 auth_port = mainconfig.port;
2950 * -p X but no -i Y on the command-line.
2952 if ((mainconfig.port > 0) &&
2953 (mainconfig.myip.af == AF_UNSPEC)) {
2954 ERROR("The command-line says \"-p %d\", but there is no associated IP address to use",
2961 * If the IP address was configured on the command-line,
2962 * use that as the "bind_address"
2964 if (mainconfig.myip.af != AF_UNSPEC) {
2965 listen_socket_t *sock;
2967 memcpy(&server_ipaddr, &mainconfig.myip,
2968 sizeof(server_ipaddr));
2972 if (strcmp(progname, "vmpsd") == 0) {
2973 this = listen_alloc(config, RAD_LISTEN_VQP);
2974 if (!auth_port) auth_port = 1589;
2977 this = listen_alloc(config, RAD_LISTEN_AUTH);
2981 sock->my_ipaddr = server_ipaddr;
2982 sock->my_port = auth_port;
2984 sock->clients = clients_parse_section(config, false);
2985 if (!sock->clients) {
2986 cf_log_err_cs(config,
2987 "Failed to find any clients for this listen section");
2992 if (listen_bind(this) < 0) {
2994 ERROR("There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
2998 auth_port = sock->my_port; /* may have been updated in listen_bind */
3000 cs = cf_section_sub_find_name2(config, "server",
3002 if (cs) this->server = mainconfig.name;
3006 last = &(this->next);
3012 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
3015 #ifdef WITH_ACCOUNTING
3017 * Open Accounting Socket.
3019 * If we haven't already gotten acct_port from
3020 * /etc/services, then make it auth_port + 1.
3022 this = listen_alloc(config, RAD_LISTEN_ACCT);
3026 * Create the accounting socket.
3028 * The accounting port is always the
3029 * authentication port + 1
3031 sock->my_ipaddr = server_ipaddr;
3032 sock->my_port = auth_port + 1;
3034 sock->clients = clients_parse_section(config, false);
3035 if (!sock->clients) {
3036 cf_log_err_cs(config,
3037 "Failed to find any clients for this listen section");
3041 if (listen_bind(this) < 0) {
3044 ERROR("There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
3049 cs = cf_section_sub_find_name2(config, "server",
3051 if (cs) this->server = mainconfig.name;
3055 last = &(this->next);
3060 * They specified an IP on the command-line, ignore
3061 * all listen sections except the one in '-n'.
3063 if (mainconfig.myip.af != AF_UNSPEC) {
3064 CONF_SECTION *subcs;
3065 char const *name2 = cf_section_name2(cs);
3067 cs = cf_section_sub_find_name2(config, "server",
3069 if (!cs) goto add_sockets;
3072 * Should really abstract this code...
3074 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
3076 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
3077 this = listen_parse(subcs, name2);
3084 last = &(this->next);
3085 } /* loop over "listen" directives in server <foo> */
3091 * Walk through the "listen" sections, if they exist.
3093 for (cs = cf_subsection_find_next(config, NULL, "listen");
3095 cs = cf_subsection_find_next(config, cs, "listen")) {
3096 this = listen_parse(cs, NULL);
3103 last = &(this->next);
3107 * Check virtual servers for "listen" sections, too.
3109 * FIXME: Move to virtual server init?
3111 for (cs = cf_subsection_find_next(config, NULL, "server");
3113 cs = cf_subsection_find_next(config, cs, "server")) {
3114 CONF_SECTION *subcs;
3115 char const *name2 = cf_section_name2(cs);
3117 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
3119 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
3120 this = listen_parse(subcs, name2);
3127 last = &(this->next);
3128 } /* loop over "listen" directives in virtual servers */
3129 } /* loop over virtual servers */
3133 * No sockets to receive packets, this is an error.
3134 * proxying is pointless.
3137 ERROR("The server is not configured to listen on any ports. Cannot start.");
3142 * Print out which sockets we're listening on, and
3143 * add them to the event list.
3145 for (this = *head; this != NULL; this = this->next) {
3147 if (this->type == RAD_LISTEN_PROXY) {
3154 if (!check_config && !spawn_flag && this->tls) {
3155 cf_log_err_cs(this->cs, "Threading must be enabled for TLS sockets to function properly");
3156 cf_log_err_cs(this->cs, "You probably need to do '%s -fxx -l stdout' for debugging",
3161 if (!check_config) {
3162 if (this->workers && !spawn_flag) {
3163 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
3167 if (this->workers) {
3168 #ifndef HAVE_PTHREAD_H
3169 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
3175 this->print(this, buffer, sizeof(buffer));
3177 for (i = 0; i < this->workers; i++) {
3181 * FIXME: create detached?
3183 rcode = pthread_create(&id, 0, recv_thread, this);
3185 ERROR("Thread create failed: %s",
3186 fr_syserror(rcode));
3190 DEBUG("Thread %d for %s\n", i, buffer);
3200 * If we're proxying requests, open the proxy FD.
3201 * Otherwise, don't do anything.
3204 if ((mainconfig.proxy_requests == true) &&
3206 (*head != NULL) && !defined_proxy) {
3207 listen_socket_t *sock = NULL;
3211 memset(&home, 0, sizeof(home));
3216 home.proto = IPPROTO_UDP;
3217 home.src_ipaddr = server_ipaddr;
3220 * Find the first authentication port,
3223 for (this = *head; this != NULL; this = this->next) {
3224 switch (this->type) {
3225 case RAD_LISTEN_AUTH:
3228 if (is_loopback(&sock->my_ipaddr)) continue;
3230 if (home.src_ipaddr.af == AF_UNSPEC) {
3231 home.src_ipaddr = sock->my_ipaddr;
3233 port = sock->my_port + 2;
3236 case RAD_LISTEN_ACCT:
3239 if (is_loopback(&sock->my_ipaddr)) continue;
3241 if (home.src_ipaddr.af == AF_UNSPEC) {
3242 home.src_ipaddr = sock->my_ipaddr;
3244 port = sock->my_port + 1;
3253 * Address is still unspecified, use IPv4.
3255 if (home.src_ipaddr.af == AF_UNSPEC) {
3256 home.src_ipaddr.af = AF_INET;
3257 /* everything else is already set to zero */
3260 home.ipaddr.af = home.src_ipaddr.af;
3261 /* everything else is already set to zero */
3263 this = proxy_new_listener(&home, port);
3269 if (!event_new_fd(this)) {
3278 * Haven't defined any sockets. Die.
3280 if (!*head) return -1;
3282 xlat_register("listen", xlat_listen, NULL, NULL);
3288 * Free a linked list of listeners;
3290 void listen_free(rad_listen_t **head)
3294 if (!head || !*head) return;
3298 rad_listen_t *next = this->next;
3307 RADCLIENT_LIST *listener_find_client_list(fr_ipaddr_t const *ipaddr,
3312 for (this = mainconfig.listen; this != NULL; this = this->next) {
3313 listen_socket_t *sock;
3315 if ((this->type != RAD_LISTEN_AUTH)
3316 #ifdef WITH_ACCOUNTING
3317 && (this->type != RAD_LISTEN_ACCT)
3323 if ((sock->my_port == port) &&
3324 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
3325 return sock->clients;
3333 rad_listen_t *listener_find_byipaddr(fr_ipaddr_t const *ipaddr, int port, int proto)
3337 for (this = mainconfig.listen; this != NULL; this = this->next) {
3338 listen_socket_t *sock;
3342 if (sock->my_port != port) continue;
3343 if (sock->proto != proto) continue;
3344 if (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) != 0) continue;
3350 * Failed to find a specific one. Find INADDR_ANY
3352 for (this = mainconfig.listen; this != NULL; this = this->next) {
3353 listen_socket_t *sock;
3357 if (sock->my_port != port) continue;
3358 if (sock->proto != proto) continue;
3359 if (!fr_inaddr_any(&sock->my_ipaddr)) continue;