2 * listen.c Handle socket stuff
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2005,2006 The FreeRADIUS server project
21 * Copyright 2005 Alan DeKok <aland@ox.org>
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/modules.h>
28 #include <freeradius-devel/rad_assert.h>
29 #include <freeradius-devel/process.h>
30 #include <freeradius-devel/protocol.h>
31 #include <freeradius-devel/modpriv.h>
33 #include <freeradius-devel/detail.h>
36 #include <freeradius-devel/udpfromto.h>
39 #ifdef HAVE_SYS_RESOURCE_H
40 #include <sys/resource.h>
52 #ifdef DEBUG_PRINT_PACKET
53 static void print_packet(RADIUS_PACKET *packet)
55 char src[256], dst[256];
57 ip_ntoh(&packet->src_ipaddr, src, sizeof(src));
58 ip_ntoh(&packet->dst_ipaddr, dst, sizeof(dst));
60 fprintf(stderr, "ID %d: %s %d -> %s %d\n", packet->id,
61 src, packet->src_port, dst, packet->dst_port);
67 static rad_listen_t *listen_alloc(TALLOC_CTX *ctx, RAD_LISTEN_TYPE type);
69 #ifdef WITH_COMMAND_SOCKET
70 static int command_tcp_recv(rad_listen_t *listener);
71 static int command_tcp_send(rad_listen_t *listener, REQUEST *request);
72 static int command_write_magic(int newfd, listen_socket_t *sock);
75 static fr_protocol_t master_listen[RAD_LISTEN_MAX];
78 * Xlat for %{listen:foo}
80 static ssize_t xlat_listen(UNUSED void *instance, REQUEST *request,
81 char const *fmt, char *out,
84 char const *value = NULL;
87 if (!fmt || !out || (outlen < 1)) return 0;
89 if (!request->listener) {
90 RWDEBUG("No listener associated with this request");
95 cp = cf_pair_find(request->listener->cs, fmt);
96 if (!cp || !(value = cf_pair_value(cp))) {
97 RDEBUG("Listener does not contain config item \"%s\"", fmt);
102 strlcpy(out, value, outlen);
108 * Find a per-socket client.
110 RADCLIENT *client_listener_find(rad_listen_t *listener,
111 fr_ipaddr_t const *ipaddr, uint16_t src_port)
113 #ifdef WITH_DYNAMIC_CLIENTS
120 RADCLIENT_LIST *clients;
121 listen_socket_t *sock;
123 rad_assert(listener != NULL);
124 rad_assert(ipaddr != NULL);
126 sock = listener->data;
127 clients = sock->clients;
130 * This HAS to have been initialized previously.
132 rad_assert(clients != NULL);
134 client = client_find(clients, ipaddr,sock->proto);
136 char name[256], buffer[128];
138 #ifdef WITH_DYNAMIC_CLIENTS
139 unknown: /* used only for dynamic clients */
143 * DoS attack quenching, but only in daemon mode.
144 * If they're running in debug mode, show them
147 if (debug_flag == 0) {
148 static time_t last_printed = 0;
151 if (last_printed == now) return NULL;
156 listener->print(listener, name, sizeof(name));
158 ERROR("Ignoring request to %s from unknown client %s port %d"
162 , name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
163 buffer, sizeof(buffer)), src_port
165 , (sock->proto == IPPROTO_UDP) ? "udp" : "tcp"
171 #ifndef WITH_DYNAMIC_CLIENTS
172 return client; /* return the found client. */
176 * No server defined, and it's not dynamic. Return it.
178 if (!client->client_server && !client->dynamic) return client;
183 * It's a dynamically generated client, check it.
185 if (client->dynamic && (src_port != 0)) {
187 * Lives forever. Return it.
189 if (client->lifetime == 0) return client;
192 * Rate-limit the deletion of known clients.
193 * This makes them last a little longer, but
194 * prevents the server from melting down if (say)
195 * 10k clients all expire at once.
197 if (now == client->last_new_client) return client;
200 * It's not dead yet. Return it.
202 if ((client->created + client->lifetime) > now) return client;
205 * This really puts them onto a queue for later
208 client_delete(clients, client);
211 * Go find the enclosing network again.
213 client = client_find(clients, ipaddr, sock->proto);
218 if (!client) goto unknown;
219 if (!client->client_server) goto unknown;
222 * At this point, 'client' is the enclosing
223 * network that configures where dynamic clients
226 rad_assert(client->dynamic == 0);
228 } else if (!client->dynamic && client->rate_limit) {
230 * The IP is unknown, so we've found an enclosing
231 * network. Enable DoS protection. We only
232 * allow one new client per second. Known
233 * clients aren't subject to this restriction.
235 if (now == client->last_new_client) goto unknown;
238 client->last_new_client = now;
240 request = request_alloc(NULL);
241 if (!request) goto unknown;
243 request->listener = listener;
244 request->client = client;
245 request->packet = rad_recv(listener->fd, 0x02); /* MSG_PEEK */
246 if (!request->packet) { /* badly formed, etc */
247 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 = &main_config;
262 * Run a fake request through the given virtual server.
263 * Look for FreeRADIUS-Client-IP-Address
264 * FreeRADIUS-Client-Secret
267 * and create the RADCLIENT structure from that.
269 DEBUG("server %s {", request->server);
271 rcode = process_authorize(0, request);
273 DEBUG("} # server %s", request->server);
275 if (rcode != RLM_MODULE_OK) {
276 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 radius_update_listener(listener);
476 * Do NOT free the listener here. It's in use by
477 * a request, and will need to hang around until
478 * all of the requests are done.
480 * It is instead free'd in remove_from_request_hash()
486 * Some sanity checks, based on the packet code.
488 switch(packet->code) {
489 case PW_CODE_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 (!main_config.status_server) {
505 FR_STATS_INC(auth, total_unknown_types);
506 WARN("Ignoring Status-Server request due to security configuration");
507 rad_free(&sock->packet);
510 fun = rad_status_server;
515 FR_STATS_INC(auth, total_unknown_types);
517 DEBUG("Invalid packet code %d sent from client %s port %d : IGNORED",
518 packet->code, client->shortname, packet->src_port);
519 rad_free(&sock->packet);
521 } /* switch over packet types */
523 if (!request_receive(listener, packet, client, fun)) {
524 FR_STATS_INC(auth, total_packets_dropped);
525 rad_free(&sock->packet);
529 sock->packet = NULL; /* we have no need for more partial reads */
533 static int dual_tcp_accept(rad_listen_t *listener)
539 struct sockaddr_storage src;
540 listen_socket_t *sock;
541 fr_ipaddr_t src_ipaddr;
542 RADCLIENT *client = NULL;
546 DEBUG2(" ... new connection request on TCP socket");
548 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
551 * Non-blocking sockets must handle this.
554 if (errno == EWOULDBLOCK) {
559 DEBUG2(" ... failed to accept connection");
563 if (!fr_sockaddr2ipaddr(&src, salen, &src_ipaddr, &src_port)) {
565 DEBUG2(" ... unknown address family");
570 * Enforce client IP address checks on accept, not on
573 if ((client = client_listener_find(listener,
574 &src_ipaddr, src_port)) == NULL) {
576 FR_STATS_INC(auth, total_invalid_requests);
582 * Enforce security restrictions.
584 * This shouldn't be necessary in practice. However, it
585 * serves as a double-check on configurations. Marking a
586 * client as "tls required" means that any accidental
587 * exposure of the client to non-TLS traffic is
590 if (client->tls_required && !listener->tls) {
591 INFO("Ignoring connection to TLS socket from non-TLS client");
598 * Enforce max_connections on client && listen section.
600 if ((client->limit.max_connections != 0) &&
601 (client->limit.max_connections == client->limit.num_connections)) {
603 * FIXME: Print client IP/port, and server IP/port.
605 INFO("Ignoring new connection due to client max_connections (%d)", client->limit.max_connections);
610 sock = listener->data;
611 if ((sock->limit.max_connections != 0) &&
612 (sock->limit.max_connections == sock->limit.num_connections)) {
614 * FIXME: Print client IP/port, and server IP/port.
616 INFO("Ignoring new connection due to socket max_connections");
620 client->limit.num_connections++;
621 sock->limit.num_connections++;
624 * Add the new listener.
626 this = listen_alloc(listener, listener->type);
627 if (!this) return -1;
630 * Copy everything, including the pointer to the socket
634 memcpy(this->data, listener->data, sizeof(*sock));
635 memcpy(this, listener, sizeof(*this));
637 this->data = sock; /* fix it back */
639 sock->parent = listener->data;
640 sock->other_ipaddr = src_ipaddr;
641 sock->other_port = src_port;
642 sock->client = client;
643 sock->opened = sock->last_packet = time(NULL);
646 * Set the limits. The defaults are the parent limits.
647 * Client limits on max_connections are enforced dynamically.
648 * Set the MINIMUM of client/socket idle timeout or lifetime.
650 memcpy(&sock->limit, &sock->parent->limit, sizeof(sock->limit));
652 if (client->limit.idle_timeout &&
653 ((sock->limit.idle_timeout == 0) ||
654 (client->limit.idle_timeout < sock->limit.idle_timeout))) {
655 sock->limit.idle_timeout = client->limit.idle_timeout;
658 if (client->limit.lifetime &&
659 ((sock->limit.lifetime == 0) ||
660 (client->limit.lifetime < sock->limit.lifetime))) {
661 sock->limit.lifetime = client->limit.lifetime;
665 this->status = RAD_LISTEN_STATUS_INIT;
668 #ifdef WITH_COMMAND_SOCKET
669 if (this->type == RAD_LISTEN_COMMAND) {
670 this->recv = command_tcp_recv;
671 this->send = command_tcp_send;
672 command_write_magic(this->fd, sock);
677 this->recv = dual_tcp_recv;
681 this->recv = dual_tls_recv;
682 this->send = dual_tls_send;
688 * FIXME: set O_NONBLOCK on the accept'd fd.
689 * See djb's portability rants for details.
693 * Tell the event loop that we have a new FD.
694 * This can be called from a child thread...
696 radius_update_listener(this);
703 * Ensure that we always keep the correct counters.
706 static void common_socket_free(rad_listen_t *this)
708 listen_socket_t *sock = this->data;
710 if (sock->proto != IPPROTO_TCP) return;
713 * Decrement the number of connections.
715 if (sock->parent && (sock->parent->limit.num_connections > 0)) {
716 sock->parent->limit.num_connections--;
718 if (sock->client && sock->client->limit.num_connections > 0) {
719 sock->client->limit.num_connections--;
721 if (sock->home && sock->home->limit.num_connections > 0) {
722 sock->home->limit.num_connections--;
726 static void common_socket_free(UNUSED rad_listen_t *this)
733 * This function is stupid and complicated.
735 int common_socket_print(rad_listen_t const *this, char *buffer, size_t bufsize)
738 listen_socket_t *sock = this->data;
739 char const *name = master_listen[this->type].name;
741 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
742 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
750 if (sock->interface) {
751 ADDSTRING(" interface ");
752 ADDSTRING(sock->interface);
756 if (this->recv == dual_tcp_accept) {
757 ADDSTRING(" proto tcp");
763 * TCP sockets get printed a little differently, to make
764 * it clear what's going on.
767 ADDSTRING(" from client (");
768 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
772 snprintf(buffer, bufsize, "%d", sock->other_port);
776 if ((sock->my_ipaddr.af == AF_INET) &&
777 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
778 strlcpy(buffer, "*", bufsize);
780 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
785 snprintf(buffer, bufsize, "%d", sock->my_port);
789 ADDSTRING(", virtual-server=");
790 ADDSTRING(this->server);
800 * Maybe it's a socket that we opened to a home server.
802 if ((sock->proto == IPPROTO_TCP) &&
803 (this->type == RAD_LISTEN_PROXY)) {
805 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
809 snprintf(buffer, bufsize, "%d", sock->my_port);
811 ADDSTRING(") -> home_server (");
813 if ((sock->other_ipaddr.af == AF_INET) &&
814 (sock->other_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
815 strlcpy(buffer, "*", bufsize);
817 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
822 snprintf(buffer, bufsize, "%d", sock->other_port);
829 #endif /* WITH_PROXY */
830 #endif /* WITH_TCP */
832 ADDSTRING(" address ");
834 if ((sock->my_ipaddr.af == AF_INET) &&
835 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
836 strlcpy(buffer, "*", bufsize);
838 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
843 snprintf(buffer, bufsize, "%d", sock->my_port);
854 ADDSTRING(" as server ");
855 ADDSTRING(this->server);
864 extern bool check_config; /* radiusd.c */
866 static CONF_PARSER performance_config[] = {
867 { "skip_duplicate_checks", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rad_listen_t, nodup), NULL },
869 { "synchronous", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rad_listen_t, synchronous), NULL },
871 { "workers", FR_CONF_OFFSET(PW_TYPE_INTEGER, rad_listen_t, workers), NULL },
873 { NULL, -1, 0, NULL, NULL } /* end the list */
877 static CONF_PARSER limit_config[] = {
878 { "max_pps", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_socket_t, max_rate), NULL },
881 { "max_connections", PW_TYPE_INTEGER,
882 offsetof(listen_socket_t, limit.max_connections), NULL, "16" },
884 { "lifetime", PW_TYPE_INTEGER,
885 offsetof(listen_socket_t, limit.lifetime), NULL, "0" },
887 { "idle_timeout", PW_TYPE_INTEGER,
888 offsetof(listen_socket_t, limit.idle_timeout), NULL, "30" },
891 { NULL, -1, 0, NULL, NULL } /* end the list */
895 * Parse an authentication or accounting socket.
897 int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
900 uint16_t listen_port;
902 listen_socket_t *sock = this->data;
903 char const *section_name = NULL;
904 CONF_SECTION *client_cs, *parentcs;
912 memset(&ipaddr, 0, sizeof(ipaddr));
913 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
914 rcode = cf_item_parse(cs, "ipaddr", FR_ITEM_POINTER(PW_TYPE_IPADDR, &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", FR_ITEM_POINTER(PW_TYPE_IPV6ADDR, &ipaddr.ipaddr.ip6addr), NULL);
922 if (rcode < 0) return -1;
926 "No address specified in listen section");
929 ipaddr.af = AF_INET6;
932 rcode = cf_item_parse(cs, "port", FR_ITEM_POINTER(PW_TYPE_SHORT, &listen_port), "0");
933 if (rcode < 0) return -1;
935 sock->proto = IPPROTO_UDP;
937 if (cf_pair_find(cs, "proto")) {
940 "System does not support the TCP protocol. Delete this line from the configuration file");
943 char const *proto = NULL;
948 rcode = cf_item_parse(cs, "proto", FR_ITEM_POINTER(PW_TYPE_STRING, &proto), "udp");
949 if (rcode < 0) return -1;
951 if (strcmp(proto, "udp") == 0) {
952 sock->proto = IPPROTO_UDP;
954 } else if (strcmp(proto, "tcp") == 0) {
955 sock->proto = IPPROTO_TCP;
958 "Unknown proto name \"%s\"", proto);
963 * TCP requires a destination IP for sockets.
964 * UDP doesn't, so it's allowed.
967 if ((this->type == RAD_LISTEN_PROXY) &&
968 (sock->proto != IPPROTO_UDP)) {
970 "Proxy listeners can only listen on proto = udp");
973 #endif /* WITH_PROXY */
976 tls = cf_section_sub_find(cs, "tls");
980 * Don't allow TLS configurations for UDP sockets.
982 if (sock->proto != IPPROTO_TCP) {
984 "TLS transport is not available for UDP sockets");
989 * If unset, set to default.
991 if (listen_port == 0) listen_port = PW_RADIUS_TLS_PORT;
993 this->tls = tls_server_conf_parse(tls);
998 #ifdef HAVE_PTRHEAD_H
999 if (pthread_mutex_init(&sock->mutex, NULL) < 0) {
1007 #else /* WITH_TLS */
1009 * Built without TLS. Disallow it.
1011 if (cf_section_sub_find(cs, "tls")) {
1013 "TLS transport is not available in this executable");
1016 #endif /* WITH_TLS */
1018 #endif /* WITH_TCP */
1021 * No "proto" field. Disallow TLS.
1023 } else if (cf_section_sub_find(cs, "tls")) {
1025 "TLS transport is not available in this \"listen\" section");
1030 * Magical tuning methods!
1032 subcs = cf_section_sub_find(cs, "performance");
1034 rcode = cf_section_parse(subcs, this,
1035 performance_config);
1036 if (rcode < 0) return -1;
1038 if (this->synchronous && sock->max_rate) {
1039 WARN("Setting 'max_pps' is incompatible with 'synchronous'. Disabling 'max_pps'");
1043 if (!this->synchronous && this->workers) {
1044 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
1049 subcs = cf_section_sub_find(cs, "limit");
1051 rcode = cf_section_parse(subcs, sock,
1053 if (rcode < 0) return -1;
1055 if (sock->max_rate && ((sock->max_rate < 10) || (sock->max_rate > 1000000))) {
1057 "Invalid value for \"max_pps\"");
1062 if ((sock->limit.idle_timeout > 0) && (sock->limit.idle_timeout < 5)) {
1063 WARN("Setting idle_timeout to 5");
1064 sock->limit.idle_timeout = 5;
1067 if ((sock->limit.lifetime > 0) && (sock->limit.lifetime < 5)) {
1068 WARN("Setting lifetime to 5");
1069 sock->limit.lifetime = 5;
1072 if ((sock->limit.lifetime > 0) && (sock->limit.idle_timeout > sock->limit.lifetime)) {
1073 WARN("Setting idle_timeout to 0");
1074 sock->limit.idle_timeout = 0;
1078 * Force no duplicate detection for TCP sockets.
1080 if (sock->proto == IPPROTO_TCP) {
1085 sock->limit.max_connections = 60;
1086 sock->limit.idle_timeout = 30;
1087 sock->limit.lifetime = 0;
1091 sock->my_ipaddr = ipaddr;
1092 sock->my_port = listen_port;
1097 * Until there is a side effects free way of forwarding a
1098 * request to another virtual server, this check is invalid,
1099 * and should be left disabled.
1102 if (home_server_find(&sock->my_ipaddr, sock->my_port, sock->proto)) {
1105 ERROR("We have been asked to listen on %s port %d, which is also listed as a "
1106 "home server. This can create a proxy loop",
1107 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)), sock->my_port);
1111 return 0; /* don't do anything */
1116 * If we can bind to interfaces, do so,
1119 if (cf_pair_find(cs, "interface")) {
1121 CONF_PAIR *cp = cf_pair_find(cs, "interface");
1123 rad_assert(cp != NULL);
1124 value = cf_pair_value(cp);
1127 "No interface name given");
1130 sock->interface = value;
1135 * If we can do broadcasts..
1137 if (cf_pair_find(cs, "broadcast")) {
1138 #ifndef SO_BROADCAST
1140 "System does not support broadcast sockets. Delete this line from the configuration file");
1144 CONF_PAIR *cp = cf_pair_find(cs, "broadcast");
1146 if (this->type != RAD_LISTEN_DHCP) {
1148 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file");
1152 rad_assert(cp != NULL);
1153 value = cf_pair_value(cp);
1156 "No broadcast value given");
1161 * Hack... whatever happened to cf_section_parse?
1163 sock->broadcast = (strcmp(value, "yes") == 0);
1169 * And bind it to the port.
1171 if (listen_bind(this) < 0) {
1174 "Error binding to port for %s port %d",
1175 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
1182 * Proxy sockets don't have clients.
1184 if (this->type == RAD_LISTEN_PROXY) return 0;
1188 * The more specific configurations are preferred to more
1192 parentcs = cf_top_section(cs);
1193 rcode = cf_item_parse(cs, "clients", FR_ITEM_POINTER(PW_TYPE_STRING, §ion_name), NULL);
1194 if (rcode < 0) return -1; /* bad string */
1197 * Explicit list given: use it.
1199 client_cs = cf_section_sub_find_name2(parentcs, "clients", section_name);
1201 client_cs = cf_section_find(section_name);
1205 "Failed to find clients %s {...}",
1209 } /* else there was no "clients = " entry. */
1212 CONF_SECTION *server_cs;
1214 server_cs = cf_section_sub_find_name2(parentcs,
1218 * Found a "server foo" section. If there are clients
1222 (cf_section_sub_find(server_cs, "client") != NULL)) {
1223 client_cs = server_cs;
1228 * Still nothing. Look for global clients.
1230 if (!client_cs) client_cs = parentcs;
1233 sock->clients = clients_parse_section(client_cs, (this->tls != NULL));
1235 sock->clients = clients_parse_section(client_cs, false);
1237 if (!sock->clients) {
1239 "Failed to load clients for this listen section");
1244 if (sock->proto == IPPROTO_TCP) {
1246 * Re-write the listener receive function to
1247 * allow us to accept the socket.
1249 this->recv = dual_tcp_accept;
1257 * Send an authentication response packet
1259 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1261 rad_assert(request->listener == listener);
1262 rad_assert(listener->send == auth_socket_send);
1264 if (request->reply->code == 0) return 0;
1266 #ifdef WITH_UDPFROMTO
1268 * Overwrite the src ip address on the outbound packet
1269 * with the one specified by the client.
1270 * This is useful to work around broken DSR implementations
1271 * and other routing issues.
1273 if (request->client->src_ipaddr.af != AF_UNSPEC) {
1274 request->reply->src_ipaddr = request->client->src_ipaddr;
1278 if (rad_send(request->reply, request->packet,
1279 request->client->secret) < 0) {
1280 RERROR("Failed sending reply: %s",
1289 #ifdef WITH_ACCOUNTING
1291 * Send an accounting response packet (or not)
1293 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1295 rad_assert(request->listener == listener);
1296 rad_assert(listener->send == acct_socket_send);
1299 * Accounting reject's are silently dropped.
1301 * We do it here to avoid polluting the rest of the
1302 * code with this knowledge
1304 if (request->reply->code == 0) return 0;
1306 #ifdef WITH_UDPFROMTO
1308 * Overwrite the src ip address on the outbound packet
1309 * with the one specified by the client.
1310 * This is useful to work around broken DSR implementations
1311 * and other routing issues.
1313 if (request->client->src_ipaddr.af != AF_UNSPEC) {
1314 request->reply->src_ipaddr = request->client->src_ipaddr;
1318 if (rad_send(request->reply, request->packet,
1319 request->client->secret) < 0) {
1320 RERROR("Failed sending reply: %s",
1331 * Send a packet to a home server.
1333 * FIXME: have different code for proxy auth & acct!
1335 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1337 rad_assert(request->proxy_listener == listener);
1338 rad_assert(listener->send == proxy_socket_send);
1340 if (rad_send(request->proxy, NULL,
1341 request->home_server->secret) < 0) {
1342 RERROR("Failed sending proxied request: %s",
1353 * Check if an incoming request is "ok"
1355 * It takes packets, not requests. It sees if the packet looks
1356 * OK. If so, it does a number of sanity checks on it.
1358 static int stats_socket_recv(rad_listen_t *listener)
1363 RADIUS_PACKET *packet;
1364 RADCLIENT *client = NULL;
1365 fr_ipaddr_t src_ipaddr;
1367 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1368 if (rcode < 0) return 0;
1370 FR_STATS_INC(auth, total_requests);
1372 if (rcode < 20) { /* AUTH_HDR_LEN */
1373 FR_STATS_INC(auth, total_malformed_requests);
1377 if ((client = client_listener_find(listener,
1378 &src_ipaddr, src_port)) == NULL) {
1379 rad_recv_discard(listener->fd);
1380 FR_STATS_INC(auth, total_invalid_requests);
1384 FR_STATS_TYPE_INC(client->auth.total_requests);
1387 * We only understand Status-Server on this socket.
1389 if (code != PW_CODE_STATUS_SERVER) {
1390 DEBUG("Ignoring packet code %d sent to Status-Server port",
1392 rad_recv_discard(listener->fd);
1393 FR_STATS_INC(auth, total_unknown_types);
1398 * Now that we've sanity checked everything, receive the
1401 packet = rad_recv(listener->fd, 1); /* require message authenticator */
1403 FR_STATS_INC(auth, total_malformed_requests);
1404 DEBUG("%s", fr_strerror());
1408 if (!request_receive(listener, packet, client, rad_status_server)) {
1409 FR_STATS_INC(auth, total_packets_dropped);
1420 * Check if an incoming request is "ok"
1422 * It takes packets, not requests. It sees if the packet looks
1423 * OK. If so, it does a number of sanity checks on it.
1425 static int auth_socket_recv(rad_listen_t *listener)
1430 RADIUS_PACKET *packet;
1431 RAD_REQUEST_FUNP fun = NULL;
1432 RADCLIENT *client = NULL;
1433 fr_ipaddr_t src_ipaddr;
1435 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1436 if (rcode < 0) return 0;
1438 FR_STATS_INC(auth, total_requests);
1440 if (rcode < 20) { /* AUTH_HDR_LEN */
1441 FR_STATS_INC(auth, total_malformed_requests);
1445 if ((client = client_listener_find(listener,
1446 &src_ipaddr, src_port)) == NULL) {
1447 rad_recv_discard(listener->fd);
1448 FR_STATS_INC(auth, total_invalid_requests);
1452 FR_STATS_TYPE_INC(client->auth.total_requests);
1455 * Some sanity checks, based on the packet code.
1458 case PW_CODE_AUTHENTICATION_REQUEST:
1459 fun = rad_authenticate;
1462 case PW_CODE_STATUS_SERVER:
1463 if (!main_config.status_server) {
1464 rad_recv_discard(listener->fd);
1465 FR_STATS_INC(auth, total_unknown_types);
1466 WARN("Ignoring Status-Server request due to security configuration");
1469 fun = rad_status_server;
1473 rad_recv_discard(listener->fd);
1474 FR_STATS_INC(auth,total_unknown_types);
1476 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
1477 code, client->shortname, src_port);
1480 } /* switch over packet types */
1483 * Now that we've sanity checked everything, receive the
1486 packet = rad_recv(listener->fd, client->message_authenticator);
1488 FR_STATS_INC(auth, total_malformed_requests);
1489 DEBUG("%s", fr_strerror());
1494 #ifdef WITH_UDPFROMTO
1496 * This is a NICE Mac OSX bug. Create an interface with
1497 * two IP address, and then configure one listener for
1498 * each IP address. Send thousands of packets to one
1499 * address, and some will show up on the OTHER socket.
1501 * This hack works ONLY if the clients are global. If
1502 * each listener has the same client IP, but with
1503 * different secrets, then it will fail the rad_recv()
1504 * check above, and there's nothing you can do.
1507 listen_socket_t *sock = listener->data;
1508 rad_listen_t *other;
1510 other = listener_find_byipaddr(&packet->dst_ipaddr,
1511 packet->dst_port, sock->proto);
1512 if (other) listener = other;
1518 if (!request_receive(listener, packet, client, fun)) {
1519 FR_STATS_INC(auth, total_packets_dropped);
1528 #ifdef WITH_ACCOUNTING
1530 * Receive packets from an accounting socket
1532 static int acct_socket_recv(rad_listen_t *listener)
1537 RADIUS_PACKET *packet;
1538 RAD_REQUEST_FUNP fun = NULL;
1539 RADCLIENT *client = NULL;
1540 fr_ipaddr_t src_ipaddr;
1542 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1543 if (rcode < 0) return 0;
1545 FR_STATS_INC(acct, total_requests);
1547 if (rcode < 20) { /* AUTH_HDR_LEN */
1548 FR_STATS_INC(acct, total_malformed_requests);
1552 if ((client = client_listener_find(listener,
1553 &src_ipaddr, src_port)) == NULL) {
1554 rad_recv_discard(listener->fd);
1555 FR_STATS_INC(acct, total_invalid_requests);
1559 FR_STATS_TYPE_INC(client->acct.total_requests);
1562 * Some sanity checks, based on the packet code.
1565 case PW_CODE_ACCOUNTING_REQUEST:
1566 fun = rad_accounting;
1569 case PW_CODE_STATUS_SERVER:
1570 if (!main_config.status_server) {
1571 rad_recv_discard(listener->fd);
1572 FR_STATS_INC(acct, total_unknown_types);
1574 WARN("Ignoring Status-Server request due to security configuration");
1577 fun = rad_status_server;
1581 rad_recv_discard(listener->fd);
1582 FR_STATS_INC(acct, total_unknown_types);
1584 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1585 code, client->shortname, src_port);
1587 } /* switch over packet types */
1590 * Now that we've sanity checked everything, receive the
1593 packet = rad_recv(listener->fd, 0);
1595 FR_STATS_INC(acct, total_malformed_requests);
1596 ERROR("%s", fr_strerror());
1601 * There can be no duplicate accounting packets.
1603 if (!request_receive(listener, packet, client, fun)) {
1604 FR_STATS_INC(acct, total_packets_dropped);
1615 static int do_proxy(REQUEST *request)
1619 if (request->in_proxy_hash ||
1620 (request->proxy_reply && (request->proxy_reply->code != 0))) {
1624 vp = pairfind(request->config_items, PW_HOME_SERVER_POOL, 0, TAG_ANY);
1627 if (!home_pool_byname(vp->vp_strvalue, HOME_TYPE_COA)) {
1628 REDEBUG2("Cannot proxy to unknown pool %s",
1637 * Receive a CoA packet.
1639 static int rad_coa_recv(REQUEST *request)
1641 int rcode = RLM_MODULE_OK;
1646 * Get the correct response
1648 switch (request->packet->code) {
1649 case PW_CODE_COA_REQUEST:
1650 ack = PW_CODE_COA_ACK;
1651 nak = PW_CODE_COA_NAK;
1654 case PW_CODE_DISCONNECT_REQUEST:
1655 ack = PW_CODE_DISCONNECT_ACK;
1656 nak = PW_CODE_DISCONNECT_NAK;
1659 default: /* shouldn't happen */
1660 return RLM_MODULE_FAIL;
1664 #define WAS_PROXIED (request->proxy)
1666 #define WAS_PROXIED (0)
1671 * RFC 5176 Section 3.3. If we have a CoA-Request
1672 * with Service-Type = Authorize-Only, it MUST
1673 * have a State attribute in it.
1675 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE, 0, TAG_ANY);
1676 if (request->packet->code == PW_CODE_COA_REQUEST) {
1677 if (vp && (vp->vp_integer == 17)) {
1678 vp = pairfind(request->packet->vps, PW_STATE, 0, TAG_ANY);
1679 if (!vp || (vp->length == 0)) {
1680 REDEBUG("CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1681 request->reply->code = PW_CODE_COA_NAK;
1682 return RLM_MODULE_FAIL;
1687 * RFC 5176, Section 3.2.
1689 REDEBUG("Disconnect-Request MUST NOT contain a Service-Type attribute");
1690 request->reply->code = PW_CODE_DISCONNECT_NAK;
1691 return RLM_MODULE_FAIL;
1694 rcode = process_recv_coa(0, request);
1696 case RLM_MODULE_FAIL:
1697 case RLM_MODULE_INVALID:
1698 case RLM_MODULE_REJECT:
1699 case RLM_MODULE_USERLOCK:
1701 request->reply->code = nak;
1704 case RLM_MODULE_HANDLED:
1707 case RLM_MODULE_NOOP:
1708 case RLM_MODULE_NOTFOUND:
1710 case RLM_MODULE_UPDATED:
1711 if (do_proxy(request)) return RLM_MODULE_OK;
1712 request->reply->code = ack;
1715 } else if (request->proxy_reply) {
1717 * Start the reply code with the proxy reply
1720 request->reply->code = request->proxy_reply->code;
1724 * Copy State from the request to the reply.
1725 * See RFC 5176 Section 3.3.
1727 vp = paircopy2(request->reply, request->packet->vps, PW_STATE, 0, TAG_ANY);
1728 if (vp) pairadd(&request->reply->vps, vp);
1731 * We may want to over-ride the reply.
1733 if (request->reply->code) {
1734 rcode = process_send_coa(0, request);
1737 * We need to send CoA-NAK back if Service-Type
1738 * is Authorize-Only. Rely on the user's policy
1739 * to do that. We're not a real NAS, so this
1740 * restriction doesn't (ahem) apply to us.
1742 case RLM_MODULE_FAIL:
1743 case RLM_MODULE_INVALID:
1744 case RLM_MODULE_REJECT:
1745 case RLM_MODULE_USERLOCK:
1748 * Over-ride an ACK with a NAK
1750 request->reply->code = nak;
1753 case RLM_MODULE_HANDLED:
1756 case RLM_MODULE_NOOP:
1757 case RLM_MODULE_NOTFOUND:
1759 case RLM_MODULE_UPDATED:
1761 * Do NOT over-ride a previously set value.
1762 * Otherwise an "ok" here will re-write a
1765 if (request->reply->code == 0) {
1766 request->reply->code = ack;
1772 return RLM_MODULE_OK;
1777 * Check if an incoming request is "ok"
1779 * It takes packets, not requests. It sees if the packet looks
1780 * OK. If so, it does a number of sanity checks on it.
1782 static int coa_socket_recv(rad_listen_t *listener)
1787 RADIUS_PACKET *packet;
1788 RAD_REQUEST_FUNP fun = NULL;
1789 RADCLIENT *client = NULL;
1790 fr_ipaddr_t src_ipaddr;
1792 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1793 if (rcode < 0) return 0;
1795 if (rcode < 20) { /* AUTH_HDR_LEN */
1796 FR_STATS_INC(coa, total_requests);
1797 FR_STATS_INC(coa, total_malformed_requests);
1801 if ((client = client_listener_find(listener,
1802 &src_ipaddr, src_port)) == NULL) {
1803 rad_recv_discard(listener->fd);
1804 FR_STATS_INC(coa, total_requests);
1805 FR_STATS_INC(coa, total_invalid_requests);
1810 * Some sanity checks, based on the packet code.
1813 case PW_CODE_COA_REQUEST:
1814 FR_STATS_INC(coa, total_requests);
1818 case PW_CODE_DISCONNECT_REQUEST:
1819 FR_STATS_INC(dsc, total_requests);
1824 rad_recv_discard(listener->fd);
1825 FR_STATS_INC(coa, total_unknown_types);
1826 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1827 code, client->shortname, src_port);
1829 } /* switch over packet types */
1832 * Now that we've sanity checked everything, receive the
1835 packet = rad_recv(listener->fd, client->message_authenticator);
1837 FR_STATS_INC(coa, total_malformed_requests);
1838 DEBUG("%s", fr_strerror());
1842 if (!request_receive(listener, packet, client, fun)) {
1843 FR_STATS_INC(coa, total_packets_dropped);
1854 * Recieve packets from a proxy socket.
1856 static int proxy_socket_recv(rad_listen_t *listener)
1858 RADIUS_PACKET *packet;
1861 packet = rad_recv(listener->fd, 0);
1863 ERROR("%s", fr_strerror());
1868 * FIXME: Client MIB updates?
1870 switch(packet->code) {
1871 case PW_CODE_AUTHENTICATION_ACK:
1872 case PW_CODE_ACCESS_CHALLENGE:
1873 case PW_CODE_AUTHENTICATION_REJECT:
1876 #ifdef WITH_ACCOUNTING
1877 case PW_CODE_ACCOUNTING_RESPONSE:
1882 case PW_CODE_DISCONNECT_ACK:
1883 case PW_CODE_DISCONNECT_NAK:
1884 case PW_CODE_COA_ACK:
1885 case PW_CODE_COA_NAK:
1891 * FIXME: Update MIB for packet types?
1893 ERROR("Invalid packet code %d sent to a proxy port "
1894 "from home server %s port %d - ID %d : IGNORED",
1896 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1897 packet->src_port, packet->id);
1902 if (!request_proxy_reply(packet)) {
1912 * Recieve packets from a proxy socket.
1914 static int proxy_socket_tcp_recv(rad_listen_t *listener)
1916 RADIUS_PACKET *packet;
1917 listen_socket_t *sock = listener->data;
1920 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return 0;
1922 packet = fr_tcp_recv(listener->fd, 0);
1924 listener->status = RAD_LISTEN_STATUS_EOL;
1925 radius_update_listener(listener);
1930 * FIXME: Client MIB updates?
1932 switch(packet->code) {
1933 case PW_CODE_AUTHENTICATION_ACK:
1934 case PW_CODE_ACCESS_CHALLENGE:
1935 case PW_CODE_AUTHENTICATION_REJECT:
1938 #ifdef WITH_ACCOUNTING
1939 case PW_CODE_ACCOUNTING_RESPONSE:
1945 * FIXME: Update MIB for packet types?
1947 ERROR("Invalid packet code %d sent to a proxy port "
1948 "from home server %s port %d - ID %d : IGNORED",
1950 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1951 packet->src_port, packet->id);
1956 packet->src_ipaddr = sock->other_ipaddr;
1957 packet->src_port = sock->other_port;
1958 packet->dst_ipaddr = sock->my_ipaddr;
1959 packet->dst_port = sock->my_port;
1962 * FIXME: Have it return an indication of packets that
1963 * are OK to ignore (dups, too late), versus ones that
1964 * aren't OK to ignore (unknown response, spoofed, etc.)
1966 * Close the socket on bad packets...
1968 if (!request_proxy_reply(packet)) {
1973 sock->opened = sock->last_packet = time(NULL);
1981 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1983 if (!request->reply->code) return 0;
1985 if (rad_encode(request->reply, request->packet,
1986 request->client->secret) < 0) {
1987 RERROR("Failed encoding packet: %s",
1992 if (rad_sign(request->reply, request->packet,
1993 request->client->secret) < 0) {
1994 RERROR("Failed signing packet: %s",
2003 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
2005 if (rad_verify(request->packet, NULL,
2006 request->client->secret) < 0) {
2010 return rad_decode(request->packet, NULL,
2011 request->client->secret);
2015 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
2017 if (rad_encode(request->proxy, NULL, request->home_server->secret) < 0) {
2018 RERROR("Failed encoding proxied packet: %s",
2023 if (rad_sign(request->proxy, NULL, request->home_server->secret) < 0) {
2024 RERROR("Failed signing proxied packet: %s",
2033 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
2036 * rad_verify is run in event.c, received_proxy_response()
2039 return rad_decode(request->proxy_reply, request->proxy,
2040 request->home_server->secret);
2044 #include "command.c"
2047 * Temporarily NOT const!
2049 static fr_protocol_t master_listen[RAD_LISTEN_MAX] = {
2051 { RLM_MODULE_INIT, "status", sizeof(listen_socket_t), NULL,
2052 common_socket_parse, NULL,
2053 stats_socket_recv, auth_socket_send,
2054 common_socket_print, client_socket_encode, client_socket_decode },
2057 * This always gets defined.
2059 { RLM_MODULE_INIT, "status", 0, NULL,
2060 NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
2065 { RLM_MODULE_INIT, "proxy", sizeof(listen_socket_t), NULL,
2066 common_socket_parse, NULL,
2067 proxy_socket_recv, proxy_socket_send,
2068 common_socket_print, proxy_socket_encode, proxy_socket_decode },
2071 /* authentication */
2072 { RLM_MODULE_INIT, "auth", sizeof(listen_socket_t), NULL,
2073 common_socket_parse, common_socket_free,
2074 auth_socket_recv, auth_socket_send,
2075 common_socket_print, client_socket_encode, client_socket_decode },
2077 #ifdef WITH_ACCOUNTING
2079 { RLM_MODULE_INIT, "acct", sizeof(listen_socket_t), NULL,
2080 common_socket_parse, common_socket_free,
2081 acct_socket_recv, acct_socket_send,
2082 common_socket_print, client_socket_encode, client_socket_decode},
2087 { RLM_MODULE_INIT, "detail", sizeof(listen_detail_t), NULL,
2088 detail_parse, detail_free,
2089 detail_recv, detail_send,
2090 detail_print, detail_encode, detail_decode },
2094 /* vlan query protocol */
2095 { 0, "vmps", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
2099 /* dhcp query protocol */
2100 { 0, "dhcp", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
2103 #ifdef WITH_COMMAND_SOCKET
2104 /* TCP command socket */
2105 { RLM_MODULE_INIT, "control", sizeof(fr_command_socket_t), NULL,
2106 command_socket_parse, command_socket_free,
2107 command_domain_accept, command_domain_send,
2108 command_socket_print, command_socket_encode, command_socket_decode },
2112 /* Change of Authorization */
2113 { RLM_MODULE_INIT, "coa", sizeof(listen_socket_t), NULL,
2114 common_socket_parse, NULL,
2115 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
2116 common_socket_print, client_socket_encode, client_socket_decode },
2124 * Binds a listener to a socket.
2126 static int listen_bind(rad_listen_t *this)
2129 struct sockaddr_storage salocal;
2131 listen_socket_t *sock = this->data;
2133 #define proto_for_port "udp"
2134 #define sock_type SOCK_DGRAM
2136 char const *proto_for_port = "udp";
2137 int sock_type = SOCK_DGRAM;
2139 if (sock->proto == IPPROTO_TCP) {
2141 if (this->type == RAD_LISTEN_VQP) {
2142 ERROR("VQP does not support TCP transport");
2147 proto_for_port = "tcp";
2148 sock_type = SOCK_STREAM;
2153 * If the port is zero, then it means the appropriate
2154 * thing from /etc/services.
2156 if (sock->my_port == 0) {
2157 struct servent *svp;
2159 switch (this->type) {
2160 case RAD_LISTEN_AUTH:
2161 svp = getservbyname ("radius", proto_for_port);
2163 sock->my_port = ntohs(svp->s_port);
2165 sock->my_port = PW_AUTH_UDP_PORT;
2169 #ifdef WITH_ACCOUNTING
2170 case RAD_LISTEN_ACCT:
2171 svp = getservbyname ("radacct", proto_for_port);
2173 sock->my_port = ntohs(svp->s_port);
2175 sock->my_port = PW_ACCT_UDP_PORT;
2181 case RAD_LISTEN_PROXY:
2182 /* leave it at zero */
2187 case RAD_LISTEN_VQP:
2188 sock->my_port = 1589;
2192 #ifdef WITH_COMMAND_SOCKET
2193 case RAD_LISTEN_COMMAND:
2194 sock->my_port = PW_RADMIN_PORT;
2199 case RAD_LISTEN_COA:
2200 svp = getservbyname ("radius-dynauth", "udp");
2202 sock->my_port = ntohs(svp->s_port);
2204 sock->my_port = PW_COA_UDP_PORT;
2210 WARN("Internal sanity check failed in binding to socket. Ignoring problem");
2216 * Don't open sockets if we're checking the config.
2224 * Copy fr_socket() here, as we may need to bind to a device.
2226 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
2230 this->print(this, buffer, sizeof(buffer));
2232 ERROR("Failed opening %s: %s", buffer, fr_syserror(errno));
2238 * We don't want child processes inheriting these
2241 rcode = fcntl(this->fd, F_GETFD);
2243 if (fcntl(this->fd, F_SETFD, rcode | FD_CLOEXEC) < 0) {
2245 ERROR("Failed setting close on exec: %s", fr_syserror(errno));
2252 * Bind to a device BEFORE touching IP addresses.
2254 if (sock->interface) {
2255 #ifdef SO_BINDTODEVICE
2258 memset(&ifreq, 0, sizeof(ifreq));
2259 strlcpy(ifreq.ifr_name, sock->interface, sizeof(ifreq.ifr_name));
2262 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
2263 (char *)&ifreq, sizeof(ifreq));
2267 ERROR("Failed binding to interface %s: %s",
2268 sock->interface, fr_syserror(errno));
2270 } /* else it worked. */
2272 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2273 #ifdef HAVE_NET_IF_H
2275 * Odds are that any system supporting "bind to
2276 * device" also supports IPv6, so this next bit
2277 * isn't necessary. But it's here for
2280 * If we're doing IPv6, and the scope hasn't yet
2281 * been defined, set the scope to the scope of
2284 if (sock->my_ipaddr.af == AF_INET6) {
2285 if (sock->my_ipaddr.scope == 0) {
2286 sock->my_ipaddr.scope = if_nametoindex(sock->interface);
2287 if (sock->my_ipaddr.scope == 0) {
2289 ERROR("Failed finding interface %s: %s",
2290 sock->interface, fr_syserror(errno));
2293 } /* else scope was defined: we're OK. */
2298 * IPv4: no link local addresses,
2299 * and no bind to device.
2303 ERROR("Failed binding to interface %s: \"bind to device\" is unsupported", sock->interface);
2310 if (sock->proto == IPPROTO_TCP) {
2313 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2315 ERROR("Failed to reuse address: %s", fr_syserror(errno));
2321 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
2322 else /* UDP sockets get UDPfromto */
2325 #ifdef WITH_UDPFROMTO
2327 * Initialize udpfromto for all sockets.
2329 if (udpfromto_init(this->fd) != 0) {
2330 ERROR("Failed initializing udpfromto: %s",
2331 fr_syserror(errno));
2338 * Set up sockaddr stuff.
2340 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
2345 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2346 if (sock->my_ipaddr.af == AF_INET6) {
2348 * Listening on '::' does NOT get you IPv4 to
2349 * IPv6 mapping. You've got to listen on an IPv4
2350 * address, too. This makes the rest of the server
2351 * design a little simpler.
2355 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2358 if (setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2359 (char *)&on, sizeof(on)) < 0) {
2360 ERROR("Failed setting socket to IPv6 "
2361 "only: %s", fr_syserror(errno));
2367 #endif /* IPV6_V6ONLY */
2369 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2371 if (sock->my_ipaddr.af == AF_INET) {
2374 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2376 * Disable PMTU discovery. On Linux, this
2377 * also makes sure that the "don't fragment"
2380 flag = IP_PMTUDISC_DONT;
2381 if (setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2382 &flag, sizeof(flag)) < 0) {
2383 ERROR("Failed disabling PMTU discovery: %s",
2384 fr_syserror(errno));
2391 #if defined(IP_DONTFRAG)
2393 * Ensure that the "don't fragment" flag is zero.
2396 if (setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2397 &flag, sizeof(flag)) < 0) {
2398 ERROR("Failed setting don't fragment flag: %s",
2399 fr_syserror(errno));
2409 if (sock->broadcast) {
2412 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2413 ERROR("Can't set broadcast option: %s",
2414 fr_syserror(errno));
2422 * May be binding to priviledged ports.
2424 if (sock->my_port != 0) {
2426 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2432 this->print(this, buffer, sizeof(buffer));
2433 ERROR("Failed binding to %s: %s\n",
2434 buffer, fr_syserror(errno));
2439 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2440 * kernel instead binds us to a 1.2.3.4. If this
2441 * happens, notice, and remember our real IP.
2444 struct sockaddr_storage src;
2445 socklen_t sizeof_src = sizeof(src);
2447 memset(&src, 0, sizeof_src);
2448 if (getsockname(this->fd, (struct sockaddr *) &src,
2450 ERROR("Failed getting socket name: %s",
2451 fr_syserror(errno));
2455 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2456 &sock->my_ipaddr, &sock->my_port)) {
2457 ERROR("Socket has unsupported address family");
2464 if (sock->proto == IPPROTO_TCP) {
2466 * If there are hard-coded worker threads, they're blocking.
2468 * Otherwise, they're non-blocking.
2470 if (!this->workers && fr_nonblock(this->fd) < 0) {
2472 ERROR("Failed setting non-blocking on socket: %s",
2473 fr_syserror(errno));
2478 * Allow a backlog of 8 listeners
2480 if (listen(this->fd, 8) < 0) {
2482 ERROR("Failed in listen(): %s", fr_syserror(errno));
2489 * Mostly for proxy sockets.
2491 sock->other_ipaddr.af = sock->my_ipaddr.af;
2494 * Don't screw up other people.
2496 #undef proto_for_port
2503 static int listener_free(void *ctx)
2507 this = talloc_get_type_abort(ctx, rad_listen_t);
2510 * Other code may have eaten the FD.
2512 if (this->fd >= 0) close(this->fd);
2514 if (master_listen[this->type].free) {
2515 master_listen[this->type].free(this);
2519 if ((this->type == RAD_LISTEN_AUTH)
2521 || (this->type == RAD_LISTEN_ACCT)
2524 || (this->type == RAD_LISTEN_PROXY)
2526 #ifdef WITH_COMMAND_SOCKET
2527 || ((this->type == RAD_LISTEN_COMMAND) &&
2528 (((fr_command_socket_t *) this->data)->magic != COMMAND_SOCKET_MAGIC))
2531 listen_socket_t *sock = this->data;
2533 rad_free(&sock->packet);
2535 rad_assert(sock->ev == NULL);
2539 * Note that we do NOT free this->tls, as the
2540 * pointer is parented by its CONF_SECTION. It
2541 * may be used by multiple listeners.
2544 if (sock->ssn) session_free(sock->ssn);
2545 request_free(&sock->request);
2546 #ifdef HAVE_PTHREAD_H
2547 pthread_mutex_destroy(&(sock->mutex));
2550 #endif /* WITH_TLS */
2552 #endif /* WITH_TCP */
2558 * Allocate & initialize a new listener.
2560 static rad_listen_t *listen_alloc(TALLOC_CTX *ctx, RAD_LISTEN_TYPE type)
2564 this = talloc_zero(ctx, rad_listen_t);
2567 this->recv = master_listen[this->type].recv;
2568 this->send = master_listen[this->type].send;
2569 this->print = master_listen[this->type].print;
2570 this->encode = master_listen[this->type].encode;
2571 this->decode = master_listen[this->type].decode;
2573 talloc_set_destructor((void *) this, listener_free);
2575 this->data = talloc_zero_array(this, uint8_t, master_listen[this->type].inst_size);
2582 * Externally visible function for creating a new proxy LISTENER.
2584 * Not thread-safe, but all calls to it are protected by the
2585 * proxy mutex in event.c
2587 rad_listen_t *proxy_new_listener(home_server_t *home, uint16_t src_port)
2591 listen_socket_t *sock;
2594 if (!home) return NULL;
2596 rad_assert(home->server == NULL); /* we only open real sockets */
2598 if ((home->limit.max_connections > 0) &&
2599 (home->limit.num_connections >= home->limit.max_connections)) {
2600 RATE_LIMIT(INFO("Home server %s has too many open connections (%d)",
2601 home->name, home->limit.max_connections));
2606 if (home->last_failed_open == now) {
2607 WARN("Suppressing attempt to open socket to 'down' home server");
2611 this = listen_alloc(main_config.config, RAD_LISTEN_PROXY);
2614 sock->other_ipaddr = home->ipaddr;
2615 sock->other_port = home->port;
2618 sock->my_ipaddr = home->src_ipaddr;
2619 sock->my_port = src_port;
2620 sock->proto = home->proto;
2623 * For error messages.
2625 this->print(this, buffer, sizeof(buffer));
2627 if (debug_flag >= 2) {
2628 DEBUG("Opening new proxy socket '%s'", buffer);
2632 sock->opened = sock->last_packet = now;
2634 if (home->proto == IPPROTO_TCP) {
2635 this->recv = proxy_socket_tcp_recv;
2638 * FIXME: connect() is blocking!
2639 * We do this with the proxy mutex locked, which may
2640 * cause large delays!
2642 * http://www.developerweb.net/forum/showthread.php?p=13486
2644 this->fd = fr_tcp_client_socket(&home->src_ipaddr,
2645 &home->ipaddr, home->port);
2648 this->fd = fr_socket(&home->src_ipaddr, src_port);
2651 this->print(this, buffer,sizeof(buffer));
2652 ERROR("Failed opening proxy socket '%s' : %s",
2653 buffer, fr_strerror());
2654 home->last_failed_open = now;
2662 if ((home->proto == IPPROTO_TCP) && home->tls) {
2663 DEBUG("Trying SSL to port %d\n", home->port);
2664 sock->ssn = tls_new_client_session(home->tls, this->fd);
2666 ERROR("Failed starting SSL to '%s'", buffer);
2667 home->last_failed_open = now;
2672 this->recv = proxy_tls_recv;
2673 this->send = proxy_tls_send;
2678 * Figure out which port we were bound to.
2680 if (sock->my_port == 0) {
2681 struct sockaddr_storage src;
2682 socklen_t sizeof_src = sizeof(src);
2684 memset(&src, 0, sizeof_src);
2685 if (getsockname(this->fd, (struct sockaddr *) &src,
2687 ERROR("Failed getting socket name for '%s': %s",
2688 buffer, fr_syserror(errno));
2689 home->last_failed_open = now;
2694 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2695 &sock->my_ipaddr, &sock->my_port)) {
2696 ERROR("Socket has unsupported address family for '%s'", buffer);
2697 home->last_failed_open = now;
2703 home->limit.num_connections++;
2709 static const FR_NAME_NUMBER listen_compare[] = {
2711 { "status", RAD_LISTEN_NONE },
2713 { "auth", RAD_LISTEN_AUTH },
2714 #ifdef WITH_ACCOUNTING
2715 { "acct", RAD_LISTEN_ACCT },
2716 { "auth+acct", RAD_LISTEN_AUTH },
2719 { "detail", RAD_LISTEN_DETAIL },
2722 { "proxy", RAD_LISTEN_PROXY },
2725 { "vmps", RAD_LISTEN_VQP },
2728 { "dhcp", RAD_LISTEN_DHCP },
2730 #ifdef WITH_COMMAND_SOCKET
2731 { "control", RAD_LISTEN_COMMAND },
2734 { "coa", RAD_LISTEN_COA },
2739 static int _free_proto_handle(lt_dlhandle *handle)
2745 static rad_listen_t *listen_parse(CONF_SECTION *cs, char const *server)
2748 char const *listen_type;
2755 cp = cf_pair_find(cs, "type");
2758 "No type specified in listen section");
2762 value = cf_pair_value(cp);
2765 "Type cannot be empty");
2769 snprintf(buffer, sizeof(buffer), "proto_%s", value);
2770 handle = lt_dlopenext(buffer);
2772 fr_protocol_t *proto;
2773 lt_dlhandle *marker;
2775 proto = dlsym(handle, buffer);
2778 "Failed linking to protocol %s : %s\n",
2784 type = fr_str2int(listen_compare, value, -1);
2785 rad_assert(type >= 0); /* shouldn't be able to compile an invalid type */
2787 memcpy(&master_listen[type], proto, sizeof(*proto));
2790 * Ensure handle gets closed if config section gets freed
2792 marker = talloc(cs, lt_dlhandle);
2794 talloc_set_destructor(marker, _free_proto_handle);
2796 if (master_listen[type].magic != RLM_MODULE_INIT) {
2797 ERROR("Failed to load protocol '%s' due to internal sanity check problem",
2798 master_listen[type].name);
2803 cf_log_info(cs, "listen {");
2806 rcode = cf_item_parse(cs, "type", FR_ITEM_POINTER(PW_TYPE_STRING, &listen_type), "");
2807 if (rcode < 0) return NULL;
2810 "No type specified in listen section");
2814 type = fr_str2int(listen_compare, listen_type, -1);
2817 "Invalid type \"%s\" in listen section.",
2823 * Allow listen sections in the default config to
2824 * refer to a server.
2827 rcode = cf_item_parse(cs, "virtual_server", FR_ITEM_POINTER(PW_TYPE_STRING, &server), NULL);
2828 if (rcode < 0) return NULL;
2833 * We were passed a virtual server, so the caller is
2834 * defining a proxy listener inside of a virtual server.
2835 * This isn't allowed right now.
2837 else if (type == RAD_LISTEN_PROXY) {
2838 ERROR("Error: listen type \"proxy\" Cannot appear in a virtual server section");
2844 * Set up cross-type data.
2846 this = listen_alloc(cs, type);
2847 this->server = server;
2852 * Special-case '+' for "auth+acct".
2854 if (strchr(listen_type, '+') != NULL) {
2860 * Call per-type parser.
2862 if (master_listen[type].parse(cs, this) < 0) {
2867 cf_log_info(cs, "}");
2872 #ifdef HAVE_PTHREAD_H
2874 * A child thread which does NOTHING other than read and process
2877 static void *recv_thread(void *arg)
2879 rad_listen_t *this = arg;
2892 * Generate a list of listeners. Takes an input list of
2893 * listeners, too, so we don't close sockets with waiting packets.
2895 int listen_init(CONF_SECTION *config, rad_listen_t **head,
2899 UNUSED bool spawn_flag
2904 bool override = false;
2905 CONF_SECTION *cs = NULL;
2906 rad_listen_t **last;
2908 fr_ipaddr_t server_ipaddr;
2909 uint16_t auth_port = 0;
2911 bool defined_proxy = false;
2915 * We shouldn't be called with a pre-existing list.
2917 rad_assert(head && (*head == NULL));
2919 memset(&server_ipaddr, 0, sizeof(server_ipaddr));
2922 server_ipaddr.af = AF_UNSPEC;
2925 * If the port is specified on the command-line,
2926 * it over-rides the configuration file.
2928 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
2930 if (main_config.port > 0) {
2931 auth_port = main_config.port;
2934 * -p X but no -i Y on the command-line.
2936 if (main_config.myip.af == AF_UNSPEC) {
2937 ERROR("The command-line says \"-p %d\", but there is no associated IP address to use",
2944 * If the IP address was configured on the command-line,
2945 * use that as the "bind_address"
2947 if (main_config.myip.af != AF_UNSPEC) {
2948 listen_socket_t *sock;
2950 memcpy(&server_ipaddr, &main_config.myip,
2951 sizeof(server_ipaddr));
2955 if (strcmp(progname, "vmpsd") == 0) {
2956 this = listen_alloc(config, RAD_LISTEN_VQP);
2957 if (!auth_port) auth_port = 1589;
2960 this = listen_alloc(config, RAD_LISTEN_AUTH);
2964 sock->my_ipaddr = server_ipaddr;
2965 sock->my_port = auth_port;
2967 sock->clients = clients_parse_section(config, false);
2968 if (!sock->clients) {
2969 cf_log_err_cs(config,
2970 "Failed to find any clients for this listen section");
2975 if (listen_bind(this) < 0) {
2977 ERROR("There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
2981 auth_port = sock->my_port; /* may have been updated in listen_bind */
2983 cs = cf_section_sub_find_name2(config, "server",
2985 if (cs) this->server = main_config.name;
2989 last = &(this->next);
2995 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
2998 #ifdef WITH_ACCOUNTING
3000 * Open Accounting Socket.
3002 * If we haven't already gotten acct_port from
3003 * /etc/services, then make it auth_port + 1.
3005 this = listen_alloc(config, RAD_LISTEN_ACCT);
3009 * Create the accounting socket.
3011 * The accounting port is always the
3012 * authentication port + 1
3014 sock->my_ipaddr = server_ipaddr;
3015 sock->my_port = auth_port + 1;
3017 sock->clients = clients_parse_section(config, false);
3018 if (!sock->clients) {
3019 cf_log_err_cs(config,
3020 "Failed to find any clients for this listen section");
3024 if (listen_bind(this) < 0) {
3027 ERROR("There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
3032 cs = cf_section_sub_find_name2(config, "server",
3034 if (cs) this->server = main_config.name;
3038 last = &(this->next);
3043 * They specified an IP on the command-line, ignore
3044 * all listen sections except the one in '-n'.
3046 if (main_config.myip.af != AF_UNSPEC) {
3047 CONF_SECTION *subcs;
3048 char const *name2 = cf_section_name2(cs);
3050 cs = cf_section_sub_find_name2(config, "server",
3052 if (!cs) goto add_sockets;
3055 * Should really abstract this code...
3057 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
3059 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
3060 this = listen_parse(subcs, name2);
3067 last = &(this->next);
3068 } /* loop over "listen" directives in server <foo> */
3074 * Walk through the "listen" sections, if they exist.
3076 for (cs = cf_subsection_find_next(config, NULL, "listen");
3078 cs = cf_subsection_find_next(config, cs, "listen")) {
3079 this = listen_parse(cs, NULL);
3086 last = &(this->next);
3090 * Check virtual servers for "listen" sections, too.
3092 * FIXME: Move to virtual server init?
3094 for (cs = cf_subsection_find_next(config, NULL, "server");
3096 cs = cf_subsection_find_next(config, cs, "server")) {
3097 CONF_SECTION *subcs;
3098 char const *name2 = cf_section_name2(cs);
3100 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
3102 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
3103 this = listen_parse(subcs, name2);
3110 last = &(this->next);
3111 } /* loop over "listen" directives in virtual servers */
3112 } /* loop over virtual servers */
3116 * No sockets to receive packets, this is an error.
3117 * proxying is pointless.
3120 ERROR("The server is not configured to listen on any ports. Cannot start");
3125 * Print out which sockets we're listening on, and
3126 * add them to the event list.
3128 for (this = *head; this != NULL; this = this->next) {
3130 if (this->type == RAD_LISTEN_PROXY) {
3131 defined_proxy = true;
3137 if (!check_config && !spawn_flag && this->tls) {
3138 cf_log_err_cs(this->cs, "Threading must be enabled for TLS sockets to function properly");
3139 cf_log_err_cs(this->cs, "You probably need to do '%s -fxx -l stdout' for debugging",
3144 if (!check_config) {
3145 if (this->workers && !spawn_flag) {
3146 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
3150 if (this->workers) {
3151 #ifdef HAVE_PTHREAD_H
3156 this->print(this, buffer, sizeof(buffer));
3158 for (i = 0; i < this->workers; i++) {
3162 * FIXME: create detached?
3164 rcode = pthread_create(&id, 0, recv_thread, this);
3166 ERROR("Thread create failed: %s",
3167 fr_syserror(rcode));
3171 DEBUG("Thread %d for %s\n", i, buffer);
3174 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
3179 radius_update_listener(this);
3186 * If we're proxying requests, open the proxy FD.
3187 * Otherwise, don't do anything.
3190 if ((main_config.proxy_requests == true) &&
3192 (*head != NULL) && !defined_proxy) {
3196 memset(&home, 0, sizeof(home));
3201 home.proto = IPPROTO_UDP;
3202 home.src_ipaddr = server_ipaddr;
3206 * Address is still unspecified, use IPv4.
3208 if (home.src_ipaddr.af == AF_UNSPEC) {
3209 home.src_ipaddr.af = AF_INET;
3210 /* everything else is already set to zero */
3213 home.ipaddr.af = home.src_ipaddr.af;
3214 /* everything else is already set to zero */
3216 this = proxy_new_listener(&home, port);
3222 radius_update_listener(this);
3227 * Haven't defined any sockets. Die.
3229 if (!*head) return -1;
3231 xlat_register("listen", xlat_listen, NULL, NULL);
3237 * Free a linked list of listeners;
3239 void listen_free(rad_listen_t **head)
3243 if (!head || !*head) return;
3247 rad_listen_t *next = this->next;
3256 RADCLIENT_LIST *listener_find_client_list(fr_ipaddr_t const *ipaddr, uint16_t port)
3260 for (this = main_config.listen; this != NULL; this = this->next) {
3261 listen_socket_t *sock;
3263 if ((this->type != RAD_LISTEN_AUTH)
3264 #ifdef WITH_ACCOUNTING
3265 && (this->type != RAD_LISTEN_ACCT)
3271 if ((sock->my_port == port) &&
3272 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
3273 return sock->clients;
3281 rad_listen_t *listener_find_byipaddr(fr_ipaddr_t const *ipaddr, uint16_t port, int proto)
3285 for (this = main_config.listen; this != NULL; this = this->next) {
3286 listen_socket_t *sock;
3290 if (sock->my_port != port) continue;
3291 if (sock->proto != proto) continue;
3292 if (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) != 0) continue;
3298 * Failed to find a specific one. Find INADDR_ANY
3300 for (this = main_config.listen; this != NULL; this = this->next) {
3301 listen_socket_t *sock;
3305 if (sock->my_port != port) continue;
3306 if (sock->proto != proto) continue;
3307 if (!fr_inaddr_any(&sock->my_ipaddr)) continue;