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>
51 #ifdef HAVE_SYS_STAT_H
55 #ifdef DEBUG_PRINT_PACKET
56 static void print_packet(RADIUS_PACKET *packet)
58 char src[256], dst[256];
60 ip_ntoh(&packet->src_ipaddr, src, sizeof(src));
61 ip_ntoh(&packet->dst_ipaddr, dst, sizeof(dst));
63 fprintf(stderr, "ID %d: %s %d -> %s %d\n", packet->id,
64 src, packet->src_port, dst, packet->dst_port);
70 static rad_listen_t *listen_alloc(TALLOC_CTX *ctx, RAD_LISTEN_TYPE type);
72 #ifdef WITH_COMMAND_SOCKET
73 static int command_tcp_recv(rad_listen_t *listener);
74 static int command_tcp_send(rad_listen_t *listener, REQUEST *request);
75 static int command_write_magic(int newfd, listen_socket_t *sock);
78 static fr_protocol_t master_listen[];
81 * Xlat for %{listen:foo}
83 static ssize_t xlat_listen(UNUSED void *instance, REQUEST *request,
84 char const *fmt, char *out,
87 char const *value = NULL;
90 if (!fmt || !out || (outlen < 1)) return 0;
92 if (!request->listener) {
93 RWDEBUG("No listener associated with this request");
98 cp = cf_pair_find(request->listener->cs, fmt);
99 if (!cp || !(value = cf_pair_value(cp))) {
100 RDEBUG("Listener does not contain config item \"%s\"", fmt);
105 strlcpy(out, value, outlen);
111 * Find a per-socket client.
113 RADCLIENT *client_listener_find(rad_listen_t *listener,
114 fr_ipaddr_t const *ipaddr, uint16_t src_port)
116 #ifdef WITH_DYNAMIC_CLIENTS
123 RADCLIENT_LIST *clients;
124 listen_socket_t *sock;
126 rad_assert(listener != NULL);
127 rad_assert(ipaddr != NULL);
129 sock = listener->data;
130 clients = sock->clients;
133 * This HAS to have been initialized previously.
135 rad_assert(clients != NULL);
137 client = client_find(clients, ipaddr, sock->proto);
139 char name[256], buffer[128];
141 #ifdef WITH_DYNAMIC_CLIENTS
142 unknown: /* used only for dynamic clients */
146 * DoS attack quenching, but only in daemon mode.
147 * If they're running in debug mode, show them
150 if (rad_debug_lvl == 0) {
151 static time_t last_printed = 0;
154 if (last_printed == now) return NULL;
159 listener->print(listener, name, sizeof(name));
161 radlog(L_ERR, "Ignoring request to %s from unknown client %s port %d"
165 , name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
166 buffer, sizeof(buffer)), src_port
168 , (sock->proto == IPPROTO_UDP) ? "udp" : "tcp"
174 #ifndef WITH_DYNAMIC_CLIENTS
175 return client; /* return the found client. */
179 * No server defined, and it's not dynamic. Return it.
181 if (!client->client_server && !client->dynamic) return client;
186 * It's a dynamically generated client, check it.
188 if (client->dynamic && (src_port != 0)) {
189 #ifdef HAVE_SYS_STAT_H
190 char const *filename;
194 * Lives forever. Return it.
196 if (client->lifetime == 0) return client;
199 * Rate-limit the deletion of known clients.
200 * This makes them last a little longer, but
201 * prevents the server from melting down if (say)
202 * 10k clients all expire at once.
204 if (now == client->last_new_client) return client;
207 * It's not dead yet. Return it.
209 if ((client->created + client->lifetime) > now) return client;
211 #ifdef HAVE_SYS_STAT_H
213 * The client was read from a file, and the file
214 * hasn't changed since the client was created.
215 * Just renew the creation time, and continue.
216 * We don't need to re-load the same information.
219 (filename = cf_section_filename(client->cs)) != NULL) {
222 if ((stat(filename, &buf) >= 0) &&
223 (buf.st_mtime < client->created)) {
224 client->created = now;
232 * This really puts them onto a queue for later
235 client_delete(clients, client);
238 * Go find the enclosing network again.
240 client = client_find(clients, ipaddr, sock->proto);
245 if (!client) goto unknown;
246 if (!client->client_server) goto unknown;
249 * At this point, 'client' is the enclosing
250 * network that configures where dynamic clients
253 rad_assert(client->dynamic == 0);
255 } else if (!client->dynamic && client->rate_limit) {
257 * The IP is unknown, so we've found an enclosing
258 * network. Enable DoS protection. We only
259 * allow one new client per second. Known
260 * clients aren't subject to this restriction.
262 if (now == client->last_new_client) goto unknown;
265 client->last_new_client = now;
267 request = request_alloc(NULL);
268 if (!request) goto unknown;
270 request->listener = listener;
271 request->client = client;
272 request->packet = rad_recv(NULL, listener->fd, 0x02); /* MSG_PEEK */
273 if (!request->packet) { /* badly formed, etc */
274 talloc_free(request);
275 if (DEBUG_ENABLED) ERROR("Receive - %s", fr_strerror());
278 (void) talloc_steal(request, request->packet);
279 request->reply = rad_alloc_reply(request, request->packet);
280 if (!request->reply) {
281 talloc_free(request);
284 gettimeofday(&request->packet->timestamp, NULL);
286 request->priority = listener->type;
287 request->server = client->client_server;
288 request->root = &main_config;
291 * Run a fake request through the given virtual server.
292 * Look for FreeRADIUS-Client-IP-Address
293 * FreeRADIUS-Client-Secret
296 * and create the RADCLIENT structure from that.
298 RDEBUG("server %s {", request->server);
300 rcode = process_authorize(0, request);
302 RDEBUG("} # server %s", request->server);
306 case RLM_MODULE_UPDATED:
310 * Likely a fatal error we want to warn the user about
312 case RLM_MODULE_INVALID:
313 case RLM_MODULE_FAIL:
314 ERROR("Virtual-Server %s returned %s, creating dynamic client failed", request->server,
315 fr_int2str(mod_rcode_table, rcode, "<INVALID>"));
316 talloc_free(request);
320 * Probably the result of policy, or the client not existing.
323 DEBUG("Virtual-Server %s returned %s, ignoring client", request->server,
324 fr_int2str(mod_rcode_table, rcode, "<INVALID>"));
325 talloc_free(request);
330 * If the client was updated by rlm_dynamic_clients,
331 * don't create the client from attribute-value pairs.
333 if (request->client == client) {
334 created = client_afrom_request(clients, request);
336 created = request->client;
339 * This frees the client if it isn't valid.
341 if (!client_add_dynamic(clients, client, created)) goto unknown;
344 request->server = client->server;
345 exec_trigger(request, NULL, "server.client.add", false);
347 talloc_free(request);
349 if (!created) goto unknown;
355 static int listen_bind(rad_listen_t *this);
359 * Process and reply to a server-status request.
360 * Like rad_authenticate and rad_accounting this should
361 * live in it's own file but it's so small we don't bother.
363 int rad_status_server(REQUEST *request)
365 int rcode = RLM_MODULE_OK;
368 switch (request->listener->type) {
370 case RAD_LISTEN_NONE:
372 case RAD_LISTEN_AUTH:
373 dval = dict_valbyname(PW_AUTZ_TYPE, 0, "Status-Server");
375 rcode = process_authorize(dval->value, request);
377 rcode = RLM_MODULE_OK;
382 case RLM_MODULE_UPDATED:
383 request->reply->code = PW_CODE_ACCESS_ACCEPT;
386 case RLM_MODULE_FAIL:
387 case RLM_MODULE_HANDLED:
388 request->reply->code = 0; /* don't reply */
392 case RLM_MODULE_REJECT:
393 request->reply->code = PW_CODE_ACCESS_REJECT;
398 #ifdef WITH_ACCOUNTING
399 case RAD_LISTEN_ACCT:
400 dval = dict_valbyname(PW_ACCT_TYPE, 0, "Status-Server");
402 rcode = process_accounting(dval->value, request);
404 rcode = RLM_MODULE_OK;
409 case RLM_MODULE_UPDATED:
410 request->reply->code = PW_CODE_ACCOUNTING_RESPONSE;
414 request->reply->code = 0; /* don't reply */
422 * This is a vendor extension. Suggested by Glen
423 * Zorn in IETF 72, and rejected by the rest of
424 * the WG. We like it, so it goes in here.
427 dval = dict_valbyname(PW_RECV_COA_TYPE, 0, "Status-Server");
429 rcode = process_recv_coa(dval->value, request);
431 rcode = RLM_MODULE_OK;
436 case RLM_MODULE_UPDATED:
437 request->reply->code = PW_CODE_COA_ACK;
441 request->reply->code = 0; /* don't reply */
453 * Full statistics are available only on a statistics
456 if (request->listener->type == RAD_LISTEN_NONE) {
457 request_stats_reply(request);
465 static int dual_tcp_recv(rad_listen_t *listener)
468 RADIUS_PACKET *packet;
469 RAD_REQUEST_FUNP fun = NULL;
470 listen_socket_t *sock = listener->data;
471 RADCLIENT *client = sock->client;
473 rad_assert(client != NULL);
475 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return 0;
478 * Allocate a packet for partial reads.
481 sock->packet = rad_alloc(sock, false);
482 if (!sock->packet) return 0;
484 sock->packet->sockfd = listener->fd;
485 sock->packet->src_ipaddr = sock->other_ipaddr;
486 sock->packet->src_port = sock->other_port;
487 sock->packet->dst_ipaddr = sock->my_ipaddr;
488 sock->packet->dst_port = sock->my_port;
489 sock->packet->proto = sock->proto;
493 * Grab the packet currently being processed.
495 packet = sock->packet;
497 rcode = fr_tcp_read_packet(packet, 0);
500 * Still only a partial packet. Put it back, and return,
501 * so that we'll read more data when it's ready.
507 if (rcode == -1) { /* error reading packet */
510 ERROR("Invalid packet from %s port %d, closing socket: %s",
511 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
512 packet->src_port, fr_strerror());
515 if (rcode < 0) { /* error or connection reset */
516 listener->status = RAD_LISTEN_STATUS_EOL;
519 * Tell the event handler that an FD has disappeared.
521 DEBUG("Client has closed connection");
522 radius_update_listener(listener);
525 * Do NOT free the listener here. It's in use by
526 * a request, and will need to hang around until
527 * all of the requests are done.
529 * It is instead free'd in remove_from_request_hash()
535 * Some sanity checks, based on the packet code.
537 switch (packet->code) {
538 case PW_CODE_ACCESS_REQUEST:
539 if (listener->type != RAD_LISTEN_AUTH) goto bad_packet;
540 FR_STATS_INC(auth, total_requests);
541 fun = rad_authenticate;
544 #ifdef WITH_ACCOUNTING
545 case PW_CODE_ACCOUNTING_REQUEST:
546 if (listener->type != RAD_LISTEN_ACCT) goto bad_packet;
547 FR_STATS_INC(acct, total_requests);
548 fun = rad_accounting;
552 case PW_CODE_STATUS_SERVER:
553 if (!main_config.status_server) {
554 FR_STATS_INC(auth, total_unknown_types);
555 WARN("Ignoring Status-Server request due to security configuration");
556 rad_free(&sock->packet);
559 fun = rad_status_server;
564 FR_STATS_INC(auth, total_unknown_types);
566 DEBUG("Invalid packet code %d sent from client %s port %d : IGNORED",
567 packet->code, client->shortname, packet->src_port);
568 rad_free(&sock->packet);
570 } /* switch over packet types */
572 if (!request_receive(NULL, listener, packet, client, fun)) {
573 FR_STATS_INC(auth, total_packets_dropped);
574 rad_free(&sock->packet);
578 sock->packet = NULL; /* we have no need for more partial reads */
583 static int dual_tcp_accept(rad_listen_t *listener)
589 struct sockaddr_storage src;
590 listen_socket_t *sock;
591 fr_ipaddr_t src_ipaddr;
592 RADCLIENT *client = NULL;
596 DEBUG2(" ... new connection request on TCP socket");
598 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
601 * Non-blocking sockets must handle this.
604 if (errno == EWOULDBLOCK) {
609 DEBUG2(" ... failed to accept connection");
613 if (!fr_sockaddr2ipaddr(&src, salen, &src_ipaddr, &src_port)) {
615 DEBUG2(" ... unknown address family");
620 * Enforce client IP address checks on accept, not on
623 if ((client = client_listener_find(listener,
624 &src_ipaddr, src_port)) == NULL) {
626 FR_STATS_INC(auth, total_invalid_requests);
632 * Enforce security restrictions.
634 * This shouldn't be necessary in practice. However, it
635 * serves as a double-check on configurations. Marking a
636 * client as "tls required" means that any accidental
637 * exposure of the client to non-TLS traffic is
640 if (client->tls_required && !listener->tls) {
641 INFO("Ignoring connection to TLS socket from non-TLS client");
648 * Enforce max_connections on client && listen section.
650 if ((client->limit.max_connections != 0) &&
651 (client->limit.max_connections == client->limit.num_connections)) {
653 * FIXME: Print client IP/port, and server IP/port.
655 INFO("Ignoring new connection due to client max_connections (%d)", client->limit.max_connections);
660 sock = listener->data;
661 if ((sock->limit.max_connections != 0) &&
662 (sock->limit.max_connections == sock->limit.num_connections)) {
664 * FIXME: Print client IP/port, and server IP/port.
666 INFO("Ignoring new connection due to socket max_connections");
670 client->limit.num_connections++;
671 sock->limit.num_connections++;
674 * Add the new listener. We require a new context here,
675 * because the allocations for the packet, etc. in the
676 * child listener will be done in a child thread.
678 this = listen_alloc(NULL, listener->type);
679 if (!this) return -1;
682 * Copy everything, including the pointer to the socket
686 memcpy(this->data, listener->data, sizeof(*sock));
687 memcpy(this, listener, sizeof(*this));
689 this->data = sock; /* fix it back */
691 sock->parent = listener->data;
692 sock->other_ipaddr = src_ipaddr;
693 sock->other_port = src_port;
694 sock->client = client;
695 sock->opened = sock->last_packet = time(NULL);
698 * Set the limits. The defaults are the parent limits.
699 * Client limits on max_connections are enforced dynamically.
700 * Set the MINIMUM of client/socket idle timeout or lifetime.
702 memcpy(&sock->limit, &sock->parent->limit, sizeof(sock->limit));
704 if (client->limit.idle_timeout &&
705 ((sock->limit.idle_timeout == 0) ||
706 (client->limit.idle_timeout < sock->limit.idle_timeout))) {
707 sock->limit.idle_timeout = client->limit.idle_timeout;
710 if (client->limit.lifetime &&
711 ((sock->limit.lifetime == 0) ||
712 (client->limit.lifetime < sock->limit.lifetime))) {
713 sock->limit.lifetime = client->limit.lifetime;
717 this->status = RAD_LISTEN_STATUS_INIT;
719 this->parent = listener;
720 if (!rbtree_insert(listener->children, this)) {
721 ERROR("Failed inserting TCP socket into parent list.");
724 #ifdef WITH_COMMAND_SOCKET
725 if (this->type == RAD_LISTEN_COMMAND) {
726 this->recv = command_tcp_recv;
727 this->send = command_tcp_send;
728 command_write_magic(this->fd, sock);
733 this->recv = dual_tcp_recv;
737 this->recv = dual_tls_recv;
738 this->send = dual_tls_send;
744 * FIXME: set O_NONBLOCK on the accept'd fd.
745 * See djb's portability rants for details.
749 * Tell the event loop that we have a new FD.
750 * This can be called from a child thread...
752 radius_update_listener(this);
759 * Ensure that we always keep the correct counters.
762 static void common_socket_free(rad_listen_t *this)
764 listen_socket_t *sock = this->data;
766 if (sock->proto != IPPROTO_TCP) return;
769 * Decrement the number of connections.
771 if (sock->parent && (sock->parent->limit.num_connections > 0)) {
772 sock->parent->limit.num_connections--;
774 if (sock->client && sock->client->limit.num_connections > 0) {
775 sock->client->limit.num_connections--;
777 if (sock->home && sock->home->limit.num_connections > 0) {
778 sock->home->limit.num_connections--;
782 #define common_socket_free NULL
786 * This function is stupid and complicated.
788 int common_socket_print(rad_listen_t const *this, char *buffer, size_t bufsize)
791 listen_socket_t *sock = this->data;
792 char const *name = master_listen[this->type].name;
794 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
795 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
803 if (sock->interface) {
804 ADDSTRING(" interface ");
805 ADDSTRING(sock->interface);
809 if (this->recv == dual_tcp_accept) {
810 ADDSTRING(" proto tcp");
816 * TCP sockets get printed a little differently, to make
817 * it clear what's going on.
820 ADDSTRING(" from client (");
821 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
825 snprintf(buffer, bufsize, "%d", sock->other_port);
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);
842 ADDSTRING(", virtual-server=");
843 ADDSTRING(this->server);
853 * Maybe it's a socket that we opened to a home server.
855 if ((sock->proto == IPPROTO_TCP) &&
856 (this->type == RAD_LISTEN_PROXY)) {
858 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
862 snprintf(buffer, bufsize, "%d", sock->my_port);
864 ADDSTRING(") -> home_server (");
866 if ((sock->other_ipaddr.af == AF_INET) &&
867 (sock->other_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
868 strlcpy(buffer, "*", bufsize);
870 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
875 snprintf(buffer, bufsize, "%d", sock->other_port);
882 #endif /* WITH_PROXY */
883 #endif /* WITH_TCP */
885 ADDSTRING(" address ");
887 if ((sock->my_ipaddr.af == AF_INET) &&
888 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
889 strlcpy(buffer, "*", bufsize);
891 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
896 snprintf(buffer, bufsize, "%d", sock->my_port);
907 ADDSTRING(" bound to server ");
908 strlcpy(buffer, this->server, bufsize);
917 static CONF_PARSER performance_config[] = {
918 { "skip_duplicate_checks", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rad_listen_t, nodup), NULL },
920 { "synchronous", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rad_listen_t, synchronous), NULL },
922 { "workers", FR_CONF_OFFSET(PW_TYPE_INTEGER, rad_listen_t, workers), NULL },
924 { NULL, -1, 0, NULL, NULL } /* end the list */
928 static CONF_PARSER limit_config[] = {
929 { "max_pps", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_socket_t, max_rate), NULL },
932 { "max_connections", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_socket_t, limit.max_connections), "16" },
933 { "lifetime", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_socket_t, limit.lifetime), "0" },
934 { "idle_timeout", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_socket_t, limit.idle_timeout), STRINGIFY(30) },
937 { NULL, -1, 0, NULL, NULL } /* end the list */
943 * TLS requires child threads to handle the listeners. Which
944 * means that we need a separate talloc context per child thread.
945 * Which means that we need to manually clean up the child
946 * listeners. Which means we need to manually track them.
948 * All child thread linking/unlinking is done in the master
949 * thread. If we care, we can later add a mutex for the parent
952 static int listener_cmp(void const *one, void const *two)
954 if (one < two) return -1;
955 if (one > two) return +1;
959 static int listener_unlink(UNUSED void *ctx, UNUSED void *data)
961 return 2; /* unlink this node from the tree */
967 * Parse an authentication or accounting socket.
969 int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
972 uint16_t listen_port;
974 listen_socket_t *sock = this->data;
975 char const *section_name = NULL;
976 CONF_SECTION *client_cs, *parentcs;
985 memset(&ipaddr, 0, sizeof(ipaddr));
986 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
988 rcode = cf_item_parse(cs, "ipaddr", FR_ITEM_POINTER(PW_TYPE_COMBO_IP_ADDR, &ipaddr), NULL);
989 if (rcode < 0) return -1;
990 if (rcode != 0) rcode = cf_item_parse(cs, "ipv4addr", FR_ITEM_POINTER(PW_TYPE_IPV4_ADDR, &ipaddr), NULL);
991 if (rcode < 0) return -1;
992 if (rcode != 0) rcode = cf_item_parse(cs, "ipv6addr", FR_ITEM_POINTER(PW_TYPE_IPV6_ADDR, &ipaddr), NULL);
993 if (rcode < 0) return -1;
995 cf_log_err_cs(cs, "No address specified in listen section");
999 rcode = cf_item_parse(cs, "port", FR_ITEM_POINTER(PW_TYPE_SHORT, &listen_port), "0");
1000 if (rcode < 0) return -1;
1002 sock->proto = IPPROTO_UDP;
1004 if (cf_pair_find(cs, "proto")) {
1007 "System does not support the TCP protocol. Delete this line from the configuration file");
1010 char const *proto = NULL;
1015 rcode = cf_item_parse(cs, "proto", FR_ITEM_POINTER(PW_TYPE_STRING, &proto), "udp");
1016 if (rcode < 0) return -1;
1018 if (!proto || strcmp(proto, "udp") == 0) {
1019 sock->proto = IPPROTO_UDP;
1021 } else if (strcmp(proto, "tcp") == 0) {
1022 sock->proto = IPPROTO_TCP;
1025 "Unknown proto name \"%s\"", proto);
1030 * TCP requires a destination IP for sockets.
1031 * UDP doesn't, so it's allowed.
1034 if ((this->type == RAD_LISTEN_PROXY) &&
1035 (sock->proto != IPPROTO_UDP)) {
1037 "Proxy listeners can only listen on proto = udp");
1040 #endif /* WITH_PROXY */
1043 tls = cf_section_sub_find(cs, "tls");
1047 * Don't allow TLS configurations for UDP sockets.
1049 if (sock->proto != IPPROTO_TCP) {
1051 "TLS transport is not available for UDP sockets");
1056 * If unset, set to default.
1058 if (listen_port == 0) listen_port = PW_RADIUS_TLS_PORT;
1060 this->tls = tls_server_conf_parse(tls);
1065 #ifdef HAVE_PTRHEAD_H
1066 if (pthread_mutex_init(&sock->mutex, NULL) < 0) {
1074 #else /* WITH_TLS */
1076 * Built without TLS. Disallow it.
1078 if (cf_section_sub_find(cs, "tls")) {
1080 "TLS transport is not available in this executable");
1083 #endif /* WITH_TLS */
1085 #endif /* WITH_TCP */
1088 * No "proto" field. Disallow TLS.
1090 } else if (cf_section_sub_find(cs, "tls")) {
1092 "TLS transport is not available in this \"listen\" section");
1097 * Magical tuning methods!
1099 subcs = cf_section_sub_find(cs, "performance");
1101 rcode = cf_section_parse(subcs, this,
1102 performance_config);
1103 if (rcode < 0) return -1;
1105 if (this->synchronous && sock->max_rate) {
1106 WARN("Setting 'max_pps' is incompatible with 'synchronous'. Disabling 'max_pps'");
1110 if (!this->synchronous && this->workers) {
1111 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
1116 subcs = cf_section_sub_find(cs, "limit");
1118 rcode = cf_section_parse(subcs, sock,
1120 if (rcode < 0) return -1;
1122 if (sock->max_rate && ((sock->max_rate < 10) || (sock->max_rate > 1000000))) {
1124 "Invalid value for \"max_pps\"");
1129 if ((sock->limit.idle_timeout > 0) && (sock->limit.idle_timeout < 5)) {
1130 WARN("Setting idle_timeout to 5");
1131 sock->limit.idle_timeout = 5;
1134 if ((sock->limit.lifetime > 0) && (sock->limit.lifetime < 5)) {
1135 WARN("Setting lifetime to 5");
1136 sock->limit.lifetime = 5;
1139 if ((sock->limit.lifetime > 0) && (sock->limit.idle_timeout > sock->limit.lifetime)) {
1140 WARN("Setting idle_timeout to 0");
1141 sock->limit.idle_timeout = 0;
1145 * Force no duplicate detection for TCP sockets.
1147 if (sock->proto == IPPROTO_TCP) {
1152 sock->limit.max_connections = 60;
1153 sock->limit.idle_timeout = 30;
1154 sock->limit.lifetime = 0;
1158 sock->my_ipaddr = ipaddr;
1159 sock->my_port = listen_port;
1164 * Until there is a side effects free way of forwarding a
1165 * request to another virtual server, this check is invalid,
1166 * and should be left disabled.
1169 if (home_server_find(&sock->my_ipaddr, sock->my_port, sock->proto)) {
1172 ERROR("We have been asked to listen on %s port %d, which is also listed as a "
1173 "home server. This can create a proxy loop",
1174 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)), sock->my_port);
1178 return 0; /* don't do anything */
1183 * If we can bind to interfaces, do so,
1186 cp = cf_pair_find(cs, "interface");
1188 char const *value = cf_pair_value(cp);
1191 "No interface name given");
1194 sock->interface = value;
1199 * If we can do broadcasts..
1201 cp = cf_pair_find(cs, "broadcast");
1203 #ifndef SO_BROADCAST
1205 "System does not support broadcast sockets. Delete this line from the configuration file");
1208 if (this->type != RAD_LISTEN_DHCP) {
1210 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file");
1214 char const *value = cf_pair_value(cp);
1217 "No broadcast value given");
1222 * Hack... whatever happened to cf_section_parse?
1224 sock->broadcast = (strcmp(value, "yes") == 0);
1230 * And bind it to the port.
1232 if (listen_bind(this) < 0) {
1235 "Error binding to port for %s port %d",
1236 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
1243 * Proxy sockets don't have clients.
1245 if (this->type == RAD_LISTEN_PROXY) return 0;
1249 * The more specific configurations are preferred to more
1253 parentcs = cf_top_section(cs);
1254 rcode = cf_item_parse(cs, "clients", FR_ITEM_POINTER(PW_TYPE_STRING, §ion_name), NULL);
1255 if (rcode < 0) return -1; /* bad string */
1258 * Explicit list given: use it.
1260 client_cs = cf_section_sub_find_name2(parentcs, "clients", section_name);
1262 client_cs = cf_section_find(section_name);
1266 "Failed to find clients %s {...}",
1270 } /* else there was no "clients = " entry. */
1273 * The "listen" section wasn't given an explicit client list.
1274 * Look for (a) clients in this virtual server, or
1275 * (b) the global client list.
1278 CONF_SECTION *server_cs;
1280 server_cs = cf_section_sub_find_name2(parentcs,
1284 * Found a "server foo" section. If there are clients
1288 (cf_section_sub_find(server_cs, "client") != NULL)) {
1289 client_cs = server_cs;
1294 * Still nothing. Look for global clients.
1296 if (!client_cs) client_cs = parentcs;
1299 sock->clients = client_list_parse_section(client_cs, (this->tls != NULL));
1301 sock->clients = client_list_parse_section(client_cs, false);
1303 if (!sock->clients) {
1305 "Failed to load clients for this listen section");
1310 if (sock->proto == IPPROTO_TCP) {
1312 * Re-write the listener receive function to
1313 * allow us to accept the socket.
1315 this->recv = dual_tcp_accept;
1317 this->children = rbtree_create(this, listener_cmp, NULL, 0);
1318 if (!this->children) {
1319 cf_log_err_cs(cs, "Failed to create child list for TCP socket.");
1329 * Send an authentication response packet
1331 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1333 rad_assert(request->listener == listener);
1334 rad_assert(listener->send == auth_socket_send);
1336 if (request->reply->code == 0) return 0;
1338 #ifdef WITH_UDPFROMTO
1340 * Overwrite the src ip address on the outbound packet
1341 * with the one specified by the client.
1342 * This is useful to work around broken DSR implementations
1343 * and other routing issues.
1345 if (request->client->src_ipaddr.af != AF_UNSPEC) {
1346 request->reply->src_ipaddr = request->client->src_ipaddr;
1350 if (rad_send(request->reply, request->packet,
1351 request->client->secret) < 0) {
1352 RERROR("Failed sending reply: %s",
1361 #ifdef WITH_ACCOUNTING
1363 * Send an accounting response packet (or not)
1365 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1367 rad_assert(request->listener == listener);
1368 rad_assert(listener->send == acct_socket_send);
1371 * Accounting reject's are silently dropped.
1373 * We do it here to avoid polluting the rest of the
1374 * code with this knowledge
1376 if (request->reply->code == 0) return 0;
1378 #ifdef WITH_UDPFROMTO
1380 * Overwrite the src ip address on the outbound packet
1381 * with the one specified by the client.
1382 * This is useful to work around broken DSR implementations
1383 * and other routing issues.
1385 if (request->client->src_ipaddr.af != AF_UNSPEC) {
1386 request->reply->src_ipaddr = request->client->src_ipaddr;
1390 if (rad_send(request->reply, request->packet,
1391 request->client->secret) < 0) {
1392 RERROR("Failed sending reply: %s",
1403 * Send a packet to a home server.
1405 * FIXME: have different code for proxy auth & acct!
1407 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1409 rad_assert(request->proxy_listener == listener);
1410 rad_assert(listener->send == proxy_socket_send);
1412 if (rad_send(request->proxy, NULL,
1413 request->home_server->secret) < 0) {
1414 RERROR("Failed sending proxied request: %s",
1425 * Check if an incoming request is "ok"
1427 * It takes packets, not requests. It sees if the packet looks
1428 * OK. If so, it does a number of sanity checks on it.
1430 static int stats_socket_recv(rad_listen_t *listener)
1435 RADIUS_PACKET *packet;
1436 RADCLIENT *client = NULL;
1437 fr_ipaddr_t src_ipaddr;
1439 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1440 if (rcode < 0) return 0;
1442 FR_STATS_INC(auth, total_requests);
1444 if (rcode < 20) { /* RADIUS_HDR_LEN */
1445 if (DEBUG_ENABLED) ERROR("Receive - %s", fr_strerror());
1446 FR_STATS_INC(auth, total_malformed_requests);
1450 if ((client = client_listener_find(listener,
1451 &src_ipaddr, src_port)) == NULL) {
1452 rad_recv_discard(listener->fd);
1453 FR_STATS_INC(auth, total_invalid_requests);
1457 FR_STATS_TYPE_INC(client->auth.total_requests);
1460 * We only understand Status-Server on this socket.
1462 if (code != PW_CODE_STATUS_SERVER) {
1463 DEBUG("Ignoring packet code %d sent to Status-Server port",
1465 rad_recv_discard(listener->fd);
1466 FR_STATS_INC(auth, total_unknown_types);
1471 * Now that we've sanity checked everything, receive the
1474 packet = rad_recv(NULL, listener->fd, 1); /* require message authenticator */
1476 FR_STATS_INC(auth, total_malformed_requests);
1477 if (DEBUG_ENABLED) ERROR("Receive - %s", fr_strerror());
1481 if (!request_receive(NULL, listener, packet, client, rad_status_server)) {
1482 FR_STATS_INC(auth, total_packets_dropped);
1493 * Check if an incoming request is "ok"
1495 * It takes packets, not requests. It sees if the packet looks
1496 * OK. If so, it does a number of sanity checks on it.
1498 static int auth_socket_recv(rad_listen_t *listener)
1503 RADIUS_PACKET *packet;
1504 RAD_REQUEST_FUNP fun = NULL;
1505 RADCLIENT *client = NULL;
1506 fr_ipaddr_t src_ipaddr;
1509 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1510 if (rcode < 0) return 0;
1512 FR_STATS_INC(auth, total_requests);
1514 if (rcode < 20) { /* RADIUS_HDR_LEN */
1515 if (DEBUG_ENABLED) ERROR("Receive - %s", fr_strerror());
1516 FR_STATS_INC(auth, total_malformed_requests);
1520 if ((client = client_listener_find(listener,
1521 &src_ipaddr, src_port)) == NULL) {
1522 rad_recv_discard(listener->fd);
1523 FR_STATS_INC(auth, total_invalid_requests);
1527 FR_STATS_TYPE_INC(client->auth.total_requests);
1530 * Some sanity checks, based on the packet code.
1533 case PW_CODE_ACCESS_REQUEST:
1534 fun = rad_authenticate;
1537 case PW_CODE_STATUS_SERVER:
1538 if (!main_config.status_server) {
1539 rad_recv_discard(listener->fd);
1540 FR_STATS_INC(auth, total_unknown_types);
1541 WARN("Ignoring Status-Server request due to security configuration");
1544 fun = rad_status_server;
1548 rad_recv_discard(listener->fd);
1549 FR_STATS_INC(auth, total_unknown_types);
1551 if (DEBUG_ENABLED) ERROR("Receive - Invalid packet code %d sent to authentication port from "
1552 "client %s port %d", code, client->shortname, src_port);
1554 } /* switch over packet types */
1556 ctx = talloc_pool(NULL, main_config.talloc_pool_size);
1558 rad_recv_discard(listener->fd);
1559 FR_STATS_INC(auth, total_packets_dropped);
1562 talloc_set_name_const(ctx, "auth_listener_pool");
1565 * Now that we've sanity checked everything, receive the
1568 packet = rad_recv(ctx, listener->fd, client->message_authenticator);
1570 FR_STATS_INC(auth, total_malformed_requests);
1571 if (DEBUG_ENABLED) ERROR("Receive - %s", fr_strerror());
1577 #ifdef WITH_UDPFROMTO
1579 * This is a NICE Mac OSX bug. Create an interface with
1580 * two IP address, and then configure one listener for
1581 * each IP address. Send thousands of packets to one
1582 * address, and some will show up on the OTHER socket.
1584 * This hack works ONLY if the clients are global. If
1585 * each listener has the same client IP, but with
1586 * different secrets, then it will fail the rad_recv()
1587 * check above, and there's nothing you can do.
1590 listen_socket_t *sock = listener->data;
1591 rad_listen_t *other;
1593 other = listener_find_byipaddr(&packet->dst_ipaddr,
1594 packet->dst_port, sock->proto);
1595 if (other) listener = other;
1600 if (!request_receive(ctx, listener, packet, client, fun)) {
1601 FR_STATS_INC(auth, total_packets_dropped);
1610 #ifdef WITH_ACCOUNTING
1612 * Receive packets from an accounting socket
1614 static int acct_socket_recv(rad_listen_t *listener)
1619 RADIUS_PACKET *packet;
1620 RAD_REQUEST_FUNP fun = NULL;
1621 RADCLIENT *client = NULL;
1622 fr_ipaddr_t src_ipaddr;
1625 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1626 if (rcode < 0) return 0;
1628 FR_STATS_INC(acct, total_requests);
1630 if (rcode < 20) { /* RADIUS_HDR_LEN */
1631 if (DEBUG_ENABLED) ERROR("Receive - %s", fr_strerror());
1632 FR_STATS_INC(acct, total_malformed_requests);
1636 if ((client = client_listener_find(listener,
1637 &src_ipaddr, src_port)) == NULL) {
1638 rad_recv_discard(listener->fd);
1639 FR_STATS_INC(acct, total_invalid_requests);
1643 FR_STATS_TYPE_INC(client->acct.total_requests);
1646 * Some sanity checks, based on the packet code.
1649 case PW_CODE_ACCOUNTING_REQUEST:
1650 fun = rad_accounting;
1653 case PW_CODE_STATUS_SERVER:
1654 if (!main_config.status_server) {
1655 rad_recv_discard(listener->fd);
1656 FR_STATS_INC(acct, total_unknown_types);
1658 WARN("Ignoring Status-Server request due to security configuration");
1661 fun = rad_status_server;
1665 rad_recv_discard(listener->fd);
1666 FR_STATS_INC(acct, total_unknown_types);
1668 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1669 code, client->shortname, src_port);
1671 } /* switch over packet types */
1673 ctx = talloc_pool(NULL, main_config.talloc_pool_size);
1675 rad_recv_discard(listener->fd);
1676 FR_STATS_INC(acct, total_packets_dropped);
1679 talloc_set_name_const(ctx, "acct_listener_pool");
1682 * Now that we've sanity checked everything, receive the
1685 packet = rad_recv(ctx, listener->fd, 0);
1687 FR_STATS_INC(acct, total_malformed_requests);
1688 if (DEBUG_ENABLED) ERROR("Receive - %s", fr_strerror());
1694 * There can be no duplicate accounting packets.
1696 if (!request_receive(ctx, listener, packet, client, fun)) {
1697 FR_STATS_INC(acct, total_packets_dropped);
1709 static int do_proxy(REQUEST *request)
1713 if (request->in_proxy_hash ||
1714 (request->proxy_reply && (request->proxy_reply->code != 0))) {
1718 vp = fr_pair_find_by_num(request->config, PW_HOME_SERVER_POOL, 0, TAG_ANY);
1721 if (!home_pool_byname(vp->vp_strvalue, HOME_TYPE_COA)) {
1722 REDEBUG2("Cannot proxy to unknown pool %s",
1731 * We have a destination IP address. It will (later) proxied.
1733 vp = fr_pair_find_by_num(request->config, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY);
1734 if (!vp) vp = fr_pair_find_by_num(request->config, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY);
1742 * Receive a CoA packet.
1744 int rad_coa_recv(REQUEST *request)
1746 int rcode = RLM_MODULE_OK;
1751 * Get the correct response
1753 switch (request->packet->code) {
1754 case PW_CODE_COA_REQUEST:
1755 ack = PW_CODE_COA_ACK;
1756 nak = PW_CODE_COA_NAK;
1759 case PW_CODE_DISCONNECT_REQUEST:
1760 ack = PW_CODE_DISCONNECT_ACK;
1761 nak = PW_CODE_DISCONNECT_NAK;
1764 default: /* shouldn't happen */
1765 return RLM_MODULE_FAIL;
1769 #define WAS_PROXIED (request->proxy)
1771 #define WAS_PROXIED (0)
1776 * RFC 5176 Section 3.3. If we have a CoA-Request
1777 * with Service-Type = Authorize-Only, it MUST
1778 * have a State attribute in it.
1780 vp = fr_pair_find_by_num(request->packet->vps, PW_SERVICE_TYPE, 0, TAG_ANY);
1781 if (request->packet->code == PW_CODE_COA_REQUEST) {
1782 if (vp && (vp->vp_integer == PW_AUTHORIZE_ONLY)) {
1783 vp = fr_pair_find_by_num(request->packet->vps, PW_STATE, 0, TAG_ANY);
1784 if (!vp || (vp->vp_length == 0)) {
1785 REDEBUG("CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1786 request->reply->code = PW_CODE_COA_NAK;
1787 return RLM_MODULE_FAIL;
1792 * RFC 5176, Section 3.2.
1794 REDEBUG("Disconnect-Request MUST NOT contain a Service-Type attribute");
1795 request->reply->code = PW_CODE_DISCONNECT_NAK;
1796 return RLM_MODULE_FAIL;
1799 rcode = process_recv_coa(0, request);
1801 case RLM_MODULE_FAIL:
1802 case RLM_MODULE_INVALID:
1803 case RLM_MODULE_REJECT:
1804 case RLM_MODULE_USERLOCK:
1806 request->reply->code = nak;
1809 case RLM_MODULE_HANDLED:
1812 case RLM_MODULE_NOOP:
1813 case RLM_MODULE_NOTFOUND:
1815 case RLM_MODULE_UPDATED:
1816 if (do_proxy(request)) return RLM_MODULE_OK;
1817 request->reply->code = ack;
1824 else if (request->proxy_reply) {
1826 * Start the reply code with the proxy reply
1829 request->reply->code = request->proxy_reply->code;
1834 * Copy State from the request to the reply.
1835 * See RFC 5176 Section 3.3.
1837 vp = fr_pair_list_copy_by_num(request->reply, request->packet->vps, PW_STATE, 0, TAG_ANY);
1838 if (vp) fr_pair_add(&request->reply->vps, vp);
1841 * We may want to over-ride the reply.
1843 if (request->reply->code) {
1844 rcode = process_send_coa(0, request);
1847 * We need to send CoA-NAK back if Service-Type
1848 * is Authorize-Only. Rely on the user's policy
1849 * to do that. We're not a real NAS, so this
1850 * restriction doesn't (ahem) apply to us.
1852 case RLM_MODULE_FAIL:
1853 case RLM_MODULE_INVALID:
1854 case RLM_MODULE_REJECT:
1855 case RLM_MODULE_USERLOCK:
1858 * Over-ride an ACK with a NAK
1860 request->reply->code = nak;
1863 case RLM_MODULE_HANDLED:
1866 case RLM_MODULE_NOOP:
1867 case RLM_MODULE_NOTFOUND:
1869 case RLM_MODULE_UPDATED:
1871 * Do NOT over-ride a previously set value.
1872 * Otherwise an "ok" here will re-write a
1875 if (request->reply->code == 0) {
1876 request->reply->code = ack;
1882 return RLM_MODULE_OK;
1887 * Check if an incoming request is "ok"
1889 * It takes packets, not requests. It sees if the packet looks
1890 * OK. If so, it does a number of sanity checks on it.
1892 static int coa_socket_recv(rad_listen_t *listener)
1897 RADIUS_PACKET *packet;
1898 RAD_REQUEST_FUNP fun = NULL;
1899 RADCLIENT *client = NULL;
1900 fr_ipaddr_t src_ipaddr;
1903 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1904 if (rcode < 0) return 0;
1906 if (rcode < 20) { /* RADIUS_HDR_LEN */
1907 if (DEBUG_ENABLED) ERROR("Receive - %s", fr_strerror());
1908 FR_STATS_INC(coa, total_malformed_requests);
1912 if ((client = client_listener_find(listener,
1913 &src_ipaddr, src_port)) == NULL) {
1914 rad_recv_discard(listener->fd);
1915 FR_STATS_INC(coa, total_requests);
1916 FR_STATS_INC(coa, total_invalid_requests);
1921 * Some sanity checks, based on the packet code.
1924 case PW_CODE_COA_REQUEST:
1925 FR_STATS_INC(coa, total_requests);
1929 case PW_CODE_DISCONNECT_REQUEST:
1930 FR_STATS_INC(dsc, total_requests);
1935 rad_recv_discard(listener->fd);
1936 FR_STATS_INC(coa, total_unknown_types);
1937 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1938 code, client->shortname, src_port);
1940 } /* switch over packet types */
1942 ctx = talloc_pool(NULL, main_config.talloc_pool_size);
1944 rad_recv_discard(listener->fd);
1945 FR_STATS_INC(coa, total_packets_dropped);
1948 talloc_set_name_const(ctx, "coa_socket_recv_pool");
1951 * Now that we've sanity checked everything, receive the
1954 packet = rad_recv(ctx, listener->fd, client->message_authenticator);
1956 FR_STATS_INC(coa, total_malformed_requests);
1957 if (DEBUG_ENABLED) ERROR("Receive - %s", fr_strerror());
1962 if (!request_receive(ctx, listener, packet, client, fun)) {
1963 FR_STATS_INC(coa, total_packets_dropped);
1975 * Recieve packets from a proxy socket.
1977 static int proxy_socket_recv(rad_listen_t *listener)
1979 RADIUS_PACKET *packet;
1981 listen_socket_t *sock;
1985 packet = rad_recv(NULL, listener->fd, 0);
1987 if (DEBUG_ENABLED) ERROR("Receive - %s", fr_strerror());
1991 switch (packet->code) {
1992 case PW_CODE_ACCESS_ACCEPT:
1993 case PW_CODE_ACCESS_CHALLENGE:
1994 case PW_CODE_ACCESS_REJECT:
1997 #ifdef WITH_ACCOUNTING
1998 case PW_CODE_ACCOUNTING_RESPONSE:
2003 case PW_CODE_DISCONNECT_ACK:
2004 case PW_CODE_DISCONNECT_NAK:
2005 case PW_CODE_COA_ACK:
2006 case PW_CODE_COA_NAK:
2012 * FIXME: Update MIB for packet types?
2014 ERROR("Invalid packet code %d sent to a proxy port "
2015 "from home server %s port %d - ID %d : IGNORED",
2017 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
2018 packet->src_port, packet->id);
2020 listener->stats.total_unknown_types++;
2027 sock = listener->data;
2028 packet->proto = sock->proto;
2031 if (!request_proxy_reply(packet)) {
2033 listener->stats.total_packets_dropped++;
2044 * Recieve packets from a proxy socket.
2046 static int proxy_socket_tcp_recv(rad_listen_t *listener)
2048 RADIUS_PACKET *packet;
2049 listen_socket_t *sock = listener->data;
2052 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return 0;
2054 packet = fr_tcp_recv(listener->fd, 0);
2056 listener->status = RAD_LISTEN_STATUS_EOL;
2057 radius_update_listener(listener);
2062 * FIXME: Client MIB updates?
2064 switch (packet->code) {
2065 case PW_CODE_ACCESS_ACCEPT:
2066 case PW_CODE_ACCESS_CHALLENGE:
2067 case PW_CODE_ACCESS_REJECT:
2070 #ifdef WITH_ACCOUNTING
2071 case PW_CODE_ACCOUNTING_RESPONSE:
2077 * FIXME: Update MIB for packet types?
2079 ERROR("Invalid packet code %d sent to a proxy port "
2080 "from home server %s port %d - ID %d : IGNORED",
2082 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
2083 packet->src_port, packet->id);
2088 packet->src_ipaddr = sock->other_ipaddr;
2089 packet->src_port = sock->other_port;
2090 packet->dst_ipaddr = sock->my_ipaddr;
2091 packet->dst_port = sock->my_port;
2094 * FIXME: Have it return an indication of packets that
2095 * are OK to ignore (dups, too late), versus ones that
2096 * aren't OK to ignore (unknown response, spoofed, etc.)
2098 * Close the socket on bad packets...
2100 if (!request_proxy_reply(packet)) {
2105 sock->opened = sock->last_packet = time(NULL);
2113 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
2115 if (!request->reply->code) return 0;
2117 if (rad_encode(request->reply, request->packet, request->client->secret) < 0) {
2118 RERROR("Failed encoding packet: %s", fr_strerror());
2123 if (rad_sign(request->reply, request->packet, request->client->secret) < 0) {
2124 RERROR("Failed signing packet: %s", fr_strerror());
2133 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
2136 listen_socket_t *sock;
2139 if (rad_verify(request->packet, NULL,
2140 request->client->secret) < 0) {
2145 sock = request->listener->data;
2146 rad_assert(sock != NULL);
2149 * FIXME: Add the rest of the TLS parameters, too? But
2150 * how do we separate EAP-TLS parameters from RADIUS/TLS
2153 if (sock->ssn && sock->ssn->ssl) {
2154 #ifdef PSK_MAX_IDENTITY_LEN
2155 const char *identity = SSL_get_psk_identity(sock->ssn->ssl);
2157 RDEBUG("Retrieved psk identity: %s", identity);
2158 pair_make_request("TLS-PSK-Identity", identity, T_OP_SET);
2164 return rad_decode(request->packet, NULL,
2165 request->client->secret);
2169 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
2171 if (rad_encode(request->proxy, NULL, request->home_server->secret) < 0) {
2172 RERROR("Failed encoding proxied packet: %s", fr_strerror());
2177 if (rad_sign(request->proxy, NULL, request->home_server->secret) < 0) {
2178 RERROR("Failed signing proxied packet: %s", fr_strerror());
2187 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
2190 * rad_verify is run in event.c, received_proxy_response()
2193 return rad_decode(request->proxy_reply, request->proxy,
2194 request->home_server->secret);
2198 #include "command.c"
2201 * Temporarily NOT const!
2203 static fr_protocol_t master_listen[RAD_LISTEN_MAX] = {
2205 { RLM_MODULE_INIT, "status", sizeof(listen_socket_t), NULL,
2206 common_socket_parse, NULL,
2207 stats_socket_recv, auth_socket_send,
2208 common_socket_print, client_socket_encode, client_socket_decode },
2211 * This always gets defined.
2213 { RLM_MODULE_INIT, "status", 0, NULL,
2214 NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
2219 { RLM_MODULE_INIT, "proxy", sizeof(listen_socket_t), NULL,
2220 common_socket_parse, common_socket_free,
2221 proxy_socket_recv, proxy_socket_send,
2222 common_socket_print, proxy_socket_encode, proxy_socket_decode },
2225 /* authentication */
2226 { RLM_MODULE_INIT, "auth", sizeof(listen_socket_t), NULL,
2227 common_socket_parse, common_socket_free,
2228 auth_socket_recv, auth_socket_send,
2229 common_socket_print, client_socket_encode, client_socket_decode },
2231 #ifdef WITH_ACCOUNTING
2233 { RLM_MODULE_INIT, "acct", sizeof(listen_socket_t), NULL,
2234 common_socket_parse, common_socket_free,
2235 acct_socket_recv, acct_socket_send,
2236 common_socket_print, client_socket_encode, client_socket_decode},
2241 { RLM_MODULE_INIT, "detail", sizeof(listen_detail_t), NULL,
2242 detail_parse, detail_free,
2243 detail_recv, detail_send,
2244 detail_print, detail_encode, detail_decode },
2248 /* vlan query protocol */
2249 { 0, "vmps", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
2253 /* dhcp query protocol */
2254 { 0, "dhcp", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
2257 #ifdef WITH_COMMAND_SOCKET
2258 /* TCP command socket */
2259 { RLM_MODULE_INIT, "control", sizeof(fr_command_socket_t), NULL,
2260 command_socket_parse, command_socket_free,
2261 command_domain_accept, command_domain_send,
2262 command_socket_print, command_socket_encode, command_socket_decode },
2266 /* Change of Authorization */
2267 { RLM_MODULE_INIT, "coa", sizeof(listen_socket_t), NULL,
2268 common_socket_parse, NULL,
2269 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
2270 common_socket_print, client_socket_encode, client_socket_decode },
2278 * Binds a listener to a socket.
2280 static int listen_bind(rad_listen_t *this)
2283 struct sockaddr_storage salocal;
2285 listen_socket_t *sock = this->data;
2287 #define proto_for_port "udp"
2288 #define sock_type SOCK_DGRAM
2290 char const *proto_for_port = "udp";
2291 int sock_type = SOCK_DGRAM;
2293 if (sock->proto == IPPROTO_TCP) {
2295 if (this->type == RAD_LISTEN_VQP) {
2296 ERROR("VQP does not support TCP transport");
2301 proto_for_port = "tcp";
2302 sock_type = SOCK_STREAM;
2307 * If the port is zero, then it means the appropriate
2308 * thing from /etc/services.
2310 if (sock->my_port == 0) {
2311 struct servent *svp;
2313 switch (this->type) {
2314 case RAD_LISTEN_AUTH:
2315 svp = getservbyname ("radius", proto_for_port);
2317 sock->my_port = ntohs(svp->s_port);
2319 sock->my_port = PW_AUTH_UDP_PORT;
2323 #ifdef WITH_ACCOUNTING
2324 case RAD_LISTEN_ACCT:
2325 svp = getservbyname ("radacct", proto_for_port);
2327 sock->my_port = ntohs(svp->s_port);
2329 sock->my_port = PW_ACCT_UDP_PORT;
2335 case RAD_LISTEN_PROXY:
2336 /* leave it at zero */
2341 case RAD_LISTEN_VQP:
2342 sock->my_port = 1589;
2346 #ifdef WITH_COMMAND_SOCKET
2347 case RAD_LISTEN_COMMAND:
2348 sock->my_port = PW_RADMIN_PORT;
2353 case RAD_LISTEN_COA:
2354 svp = getservbyname ("radius-dynauth", "udp");
2356 sock->my_port = ntohs(svp->s_port);
2358 sock->my_port = PW_COA_UDP_PORT;
2364 WARN("Internal sanity check failed in binding to socket. Ignoring problem");
2370 * Don't open sockets if we're checking the config.
2378 * Copy fr_socket() here, as we may need to bind to a device.
2380 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
2384 this->print(this, buffer, sizeof(buffer));
2386 ERROR("Failed opening %s: %s", buffer, fr_syserror(errno));
2392 * We don't want child processes inheriting these
2395 rcode = fcntl(this->fd, F_GETFD);
2397 if (fcntl(this->fd, F_SETFD, rcode | FD_CLOEXEC) < 0) {
2399 ERROR("Failed setting close on exec: %s", fr_syserror(errno));
2406 * Bind to a device BEFORE touching IP addresses.
2408 if (sock->interface) {
2409 #ifdef SO_BINDTODEVICE
2412 memset(&ifreq, 0, sizeof(ifreq));
2413 strlcpy(ifreq.ifr_name, sock->interface, sizeof(ifreq.ifr_name));
2416 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
2417 (char *)&ifreq, sizeof(ifreq));
2421 ERROR("Failed binding to interface %s: %s",
2422 sock->interface, fr_syserror(errno));
2424 } /* else it worked. */
2426 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2427 #ifdef HAVE_NET_IF_H
2429 * Odds are that any system supporting "bind to
2430 * device" also supports IPv6, so this next bit
2431 * isn't necessary. But it's here for
2434 * If we're doing IPv6, and the scope hasn't yet
2435 * been defined, set the scope to the scope of
2438 if (sock->my_ipaddr.af == AF_INET6) {
2439 if (sock->my_ipaddr.scope == 0) {
2440 sock->my_ipaddr.scope = if_nametoindex(sock->interface);
2441 if (sock->my_ipaddr.scope == 0) {
2443 ERROR("Failed finding interface %s: %s",
2444 sock->interface, fr_syserror(errno));
2447 } /* else scope was defined: we're OK. */
2452 * IPv4: no link local addresses,
2453 * and no bind to device.
2457 ERROR("Failed binding to interface %s: \"bind to device\" is unsupported", sock->interface);
2464 if (sock->proto == IPPROTO_TCP) {
2467 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2469 ERROR("Failed to reuse address: %s", fr_syserror(errno));
2475 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
2476 else /* UDP sockets get UDPfromto */
2479 #ifdef WITH_UDPFROMTO
2481 * Initialize udpfromto for all sockets.
2483 if (udpfromto_init(this->fd) != 0) {
2484 ERROR("Failed initializing udpfromto: %s",
2485 fr_syserror(errno));
2492 * Set up sockaddr stuff.
2494 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
2499 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2500 if (sock->my_ipaddr.af == AF_INET6) {
2502 * Listening on '::' does NOT get you IPv4 to
2503 * IPv6 mapping. You've got to listen on an IPv4
2504 * address, too. This makes the rest of the server
2505 * design a little simpler.
2509 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2512 if (setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2513 (char *)&on, sizeof(on)) < 0) {
2514 ERROR("Failed setting socket to IPv6 "
2515 "only: %s", fr_syserror(errno));
2521 #endif /* IPV6_V6ONLY */
2523 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2525 if (sock->my_ipaddr.af == AF_INET) {
2526 #if (defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)) || defined(IP_DONTFRAG)
2530 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2533 * Disable PMTU discovery. On Linux, this
2534 * also makes sure that the "don't fragment"
2537 flag = IP_PMTUDISC_DONT;
2538 if (setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2539 &flag, sizeof(flag)) < 0) {
2540 ERROR("Failed disabling PMTU discovery: %s",
2541 fr_syserror(errno));
2548 #if defined(IP_DONTFRAG)
2550 * Ensure that the "don't fragment" flag is zero.
2553 if (setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2554 &flag, sizeof(flag)) < 0) {
2555 ERROR("Failed setting don't fragment flag: %s",
2556 fr_syserror(errno));
2566 if (sock->broadcast) {
2569 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2570 ERROR("Can't set broadcast option: %s",
2571 fr_syserror(errno));
2579 * May be binding to priviledged ports.
2581 if (sock->my_port != 0) {
2583 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2589 this->print(this, buffer, sizeof(buffer));
2590 ERROR("Failed binding to %s: %s\n",
2591 buffer, fr_syserror(errno));
2596 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2597 * kernel instead binds us to a 1.2.3.4. If this
2598 * happens, notice, and remember our real IP.
2601 struct sockaddr_storage src;
2602 socklen_t sizeof_src = sizeof(src);
2604 memset(&src, 0, sizeof_src);
2605 if (getsockname(this->fd, (struct sockaddr *) &src,
2607 ERROR("Failed getting socket name: %s",
2608 fr_syserror(errno));
2612 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2613 &sock->my_ipaddr, &sock->my_port)) {
2614 ERROR("Socket has unsupported address family");
2621 if (sock->proto == IPPROTO_TCP) {
2623 * If there are hard-coded worker threads, OR
2624 * it's a TLS connection, it's blocking.
2626 * Otherwise, they're non-blocking.
2631 && (this->type == RAD_LISTEN_PROXY) && !this->tls
2635 if (fr_nonblock(this->fd) < 0) {
2637 ERROR("Failed setting non-blocking on socket: %s",
2638 fr_syserror(errno));
2644 * Allow a backlog of 8 listeners, but only for incoming interfaces.
2647 if (this->type != RAD_LISTEN_PROXY)
2649 if (listen(this->fd, 8) < 0) {
2651 ERROR("Failed in listen(): %s", fr_syserror(errno));
2658 * Mostly for proxy sockets.
2660 sock->other_ipaddr.af = sock->my_ipaddr.af;
2663 * Don't screw up other people.
2665 #undef proto_for_port
2672 static int _listener_free(rad_listen_t *this)
2675 * Other code may have eaten the FD.
2677 if (this->fd >= 0) close(this->fd);
2679 if (master_listen[this->type].free) {
2680 master_listen[this->type].free(this);
2684 if ((this->type == RAD_LISTEN_AUTH)
2686 || (this->type == RAD_LISTEN_ACCT)
2689 || (this->type == RAD_LISTEN_PROXY)
2691 #ifdef WITH_COMMAND_SOCKET
2692 || ((this->type == RAD_LISTEN_COMMAND) &&
2693 (((fr_command_socket_t *) this->data)->magic != COMMAND_SOCKET_MAGIC))
2696 listen_socket_t *sock = this->data;
2698 rad_assert(talloc_parent(sock) == this);
2699 rad_assert(sock->ev == NULL);
2702 * Remove the child from the parent tree.
2705 rbtree_deletebydata(this->parent->children, this);
2709 * Delete / close all of the children, too!
2711 if (this->children) {
2712 rbtree_walk(this->children, RBTREE_DELETE_ORDER, listener_unlink, this);
2717 * Note that we do NOT free this->tls, as the
2718 * pointer is parented by its CONF_SECTION. It
2719 * may be used by multiple listeners.
2722 rad_assert(!sock->ssn || (talloc_parent(sock->ssn) == sock));
2723 rad_assert(!sock->request || (talloc_parent(sock->request) == sock));
2724 #ifdef HAVE_PTHREAD_H
2725 pthread_mutex_destroy(&(sock->mutex));
2728 #endif /* WITH_TLS */
2730 #endif /* WITH_TCP */
2737 * Allocate & initialize a new listener.
2739 static rad_listen_t *listen_alloc(TALLOC_CTX *ctx, RAD_LISTEN_TYPE type)
2743 this = talloc_zero(ctx, rad_listen_t);
2746 this->recv = master_listen[this->type].recv;
2747 this->send = master_listen[this->type].send;
2748 this->print = master_listen[this->type].print;
2749 this->encode = master_listen[this->type].encode;
2750 this->decode = master_listen[this->type].decode;
2752 talloc_set_destructor(this, _listener_free);
2754 this->data = talloc_zero_array(this, uint8_t, master_listen[this->type].inst_size);
2761 * Externally visible function for creating a new proxy LISTENER.
2763 * Not thread-safe, but all calls to it are protected by the
2764 * proxy mutex in event.c
2766 rad_listen_t *proxy_new_listener(TALLOC_CTX *ctx, home_server_t *home, uint16_t src_port)
2770 listen_socket_t *sock;
2773 if (!home) return NULL;
2775 rad_assert(home->server == NULL); /* we only open real sockets */
2777 if ((home->limit.max_connections > 0) &&
2778 (home->limit.num_connections >= home->limit.max_connections)) {
2779 RATE_LIMIT(INFO("Home server %s has too many open connections (%d)",
2780 home->log_name, home->limit.max_connections));
2785 if (home->last_failed_open == now) {
2786 WARN("Suppressing attempt to open socket to 'down' home server");
2790 this = listen_alloc(ctx, RAD_LISTEN_PROXY);
2793 sock->other_ipaddr = home->ipaddr;
2794 sock->other_port = home->port;
2797 sock->my_ipaddr = home->src_ipaddr;
2798 sock->my_port = src_port;
2799 sock->proto = home->proto;
2802 * For error messages.
2804 this->print(this, buffer, sizeof(buffer));
2806 if (rad_debug_lvl >= 2) {
2807 DEBUG("Opening new proxy socket '%s'", buffer);
2811 sock->opened = sock->last_packet = now;
2813 if (home->proto == IPPROTO_TCP) {
2814 this->recv = proxy_socket_tcp_recv;
2817 * FIXME: connect() is blocking!
2818 * We do this with the proxy mutex locked, which may
2819 * cause large delays!
2821 * http://www.developerweb.net/forum/showthread.php?p=13486
2823 this->fd = fr_socket_client_tcp(&home->src_ipaddr,
2824 &home->ipaddr, home->port, false);
2827 this->fd = fr_socket(&home->src_ipaddr, src_port);
2830 this->print(this, buffer,sizeof(buffer));
2831 ERROR("Failed opening proxy socket '%s' : %s",
2832 buffer, fr_strerror());
2833 home->last_failed_open = now;
2841 if ((home->proto == IPPROTO_TCP) && home->tls) {
2842 DEBUG("Trying SSL to port %d\n", home->port);
2843 sock->ssn = tls_new_client_session(sock, home->tls, this->fd);
2845 ERROR("Failed starting SSL to '%s'", buffer);
2846 home->last_failed_open = now;
2851 this->recv = proxy_tls_recv;
2852 this->send = proxy_tls_send;
2857 * Figure out which port we were bound to.
2859 if (sock->my_port == 0) {
2860 struct sockaddr_storage src;
2861 socklen_t sizeof_src = sizeof(src);
2863 memset(&src, 0, sizeof_src);
2864 if (getsockname(this->fd, (struct sockaddr *) &src,
2866 ERROR("Failed getting socket name for '%s': %s",
2867 buffer, fr_syserror(errno));
2868 home->last_failed_open = now;
2873 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2874 &sock->my_ipaddr, &sock->my_port)) {
2875 ERROR("Socket has unsupported address family for '%s'", buffer);
2876 home->last_failed_open = now;
2882 home->limit.num_connections++;
2888 static const FR_NAME_NUMBER listen_compare[] = {
2890 { "status", RAD_LISTEN_NONE },
2892 { "auth", RAD_LISTEN_AUTH },
2893 #ifdef WITH_ACCOUNTING
2894 { "acct", RAD_LISTEN_ACCT },
2895 { "auth+acct", RAD_LISTEN_AUTH },
2898 { "detail", RAD_LISTEN_DETAIL },
2901 { "proxy", RAD_LISTEN_PROXY },
2904 { "vmps", RAD_LISTEN_VQP },
2907 { "dhcp", RAD_LISTEN_DHCP },
2909 #ifdef WITH_COMMAND_SOCKET
2910 { "control", RAD_LISTEN_COMMAND },
2913 { "coa", RAD_LISTEN_COA },
2918 static int _free_proto_handle(lt_dlhandle *handle)
2924 static rad_listen_t *listen_parse(CONF_SECTION *cs, char const *server)
2927 char const *listen_type;
2932 CONF_SECTION *server_cs;
2935 cp = cf_pair_find(cs, "type");
2938 "No type specified in listen section");
2942 value = cf_pair_value(cp);
2945 "Type cannot be empty");
2949 snprintf(buffer, sizeof(buffer), "proto_%s", value);
2950 handle = lt_dlopenext(buffer);
2952 fr_protocol_t *proto;
2953 lt_dlhandle *marker;
2955 proto = dlsym(handle, buffer);
2958 "Failed linking to protocol %s : %s\n",
2964 type = fr_str2int(listen_compare, value, -1);
2965 rad_assert(type >= 0); /* shouldn't be able to compile an invalid type */
2967 memcpy(&master_listen[type], proto, sizeof(*proto));
2970 * Ensure handle gets closed if config section gets freed
2972 marker = talloc(cs, lt_dlhandle);
2974 talloc_set_destructor(marker, _free_proto_handle);
2976 if (master_listen[type].magic != RLM_MODULE_INIT) {
2977 ERROR("Failed to load protocol '%s', it has the wrong version.",
2978 master_listen[type].name);
2983 cf_log_info(cs, "listen {");
2986 rcode = cf_item_parse(cs, "type", FR_ITEM_POINTER(PW_TYPE_STRING, &listen_type), "");
2987 if (rcode < 0) return NULL;
2990 "No type specified in listen section");
2994 type = fr_str2int(listen_compare, listen_type, -1);
2997 "Invalid type \"%s\" in listen section.",
3003 * DHCP and VMPS *must* be loaded dynamically.
3005 if (master_listen[type].magic != RLM_MODULE_INIT) {
3006 ERROR("Cannot load protocol '%s', as the required library does not exist",
3007 master_listen[type].name);
3012 * Allow listen sections in the default config to
3013 * refer to a server.
3016 rcode = cf_item_parse(cs, "virtual_server", FR_ITEM_POINTER(PW_TYPE_STRING, &server), NULL);
3017 if (rcode < 0) return NULL;
3022 * We were passed a virtual server, so the caller is
3023 * defining a proxy listener inside of a virtual server.
3024 * This isn't allowed right now.
3026 else if (type == RAD_LISTEN_PROXY) {
3027 ERROR("Error: listen type \"proxy\" Cannot appear in a virtual server section");
3033 * Set up cross-type data.
3035 this = listen_alloc(cs, type);
3036 this->server = server;
3041 * Special-case '+' for "auth+acct".
3043 if (strchr(listen_type, '+') != NULL) {
3049 * Call per-type parser.
3051 if (master_listen[type].parse(cs, this) < 0) {
3056 server_cs = cf_section_sub_find_name2(main_config.config, "server",
3058 if (!server_cs && this->server) {
3059 cf_log_err_cs(cs, "No such server \"%s\"", this->server);
3064 cf_log_info(cs, "}");
3069 #ifdef HAVE_PTHREAD_H
3071 * A child thread which does NOTHING other than read and process
3074 static void *recv_thread(void *arg)
3076 rad_listen_t *this = arg;
3089 * Generate a list of listeners. Takes an input list of
3090 * listeners, too, so we don't close sockets with waiting packets.
3092 int listen_init(CONF_SECTION *config, rad_listen_t **head, bool spawn_flag)
3094 bool override = false;
3095 CONF_SECTION *cs = NULL;
3096 rad_listen_t **last;
3098 fr_ipaddr_t server_ipaddr;
3099 uint16_t auth_port = 0;
3101 bool defined_proxy = false;
3105 * We shouldn't be called with a pre-existing list.
3107 rad_assert(head && (*head == NULL));
3109 memset(&server_ipaddr, 0, sizeof(server_ipaddr));
3112 server_ipaddr.af = AF_UNSPEC;
3115 * If the port is specified on the command-line,
3116 * it over-rides the configuration file.
3118 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
3120 if (main_config.port > 0) {
3121 auth_port = main_config.port;
3124 * -p X but no -i Y on the command-line.
3126 if (main_config.myip.af == AF_UNSPEC) {
3127 ERROR("The command-line says \"-p %d\", but there is no associated IP address to use",
3134 * If the IP address was configured on the command-line,
3135 * use that as the "bind_address"
3137 if (main_config.myip.af != AF_UNSPEC) {
3138 listen_socket_t *sock;
3140 memcpy(&server_ipaddr, &main_config.myip,
3141 sizeof(server_ipaddr));
3145 if (strcmp(progname, "vmpsd") == 0) {
3146 this = listen_alloc(config, RAD_LISTEN_VQP);
3147 if (!auth_port) auth_port = 1589;
3150 this = listen_alloc(config, RAD_LISTEN_AUTH);
3154 sock->my_ipaddr = server_ipaddr;
3155 sock->my_port = auth_port;
3157 sock->clients = client_list_parse_section(config, false);
3158 if (!sock->clients) {
3159 cf_log_err_cs(config,
3160 "Failed to find any clients for this listen section");
3165 if (listen_bind(this) < 0) {
3167 ERROR("There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
3171 auth_port = sock->my_port; /* may have been updated in listen_bind */
3173 cs = cf_section_sub_find_name2(config, "server",
3175 if (cs) this->server = main_config.name;
3179 last = &(this->next);
3185 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
3188 #ifdef WITH_ACCOUNTING
3190 * Open Accounting Socket.
3192 * If we haven't already gotten acct_port from
3193 * /etc/services, then make it auth_port + 1.
3195 this = listen_alloc(config, RAD_LISTEN_ACCT);
3199 * Create the accounting socket.
3201 * The accounting port is always the
3202 * authentication port + 1
3204 sock->my_ipaddr = server_ipaddr;
3205 sock->my_port = auth_port + 1;
3207 sock->clients = client_list_parse_section(config, false);
3208 if (!sock->clients) {
3209 cf_log_err_cs(config,
3210 "Failed to find any clients for this listen section");
3214 if (listen_bind(this) < 0) {
3217 ERROR("There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
3222 cs = cf_section_sub_find_name2(config, "server",
3224 if (cs) this->server = main_config.name;
3228 last = &(this->next);
3233 * They specified an IP on the command-line, ignore
3234 * all listen sections except the one in '-n'.
3236 if (main_config.myip.af != AF_UNSPEC) {
3237 CONF_SECTION *subcs;
3238 char const *name2 = cf_section_name2(cs);
3240 cs = cf_section_sub_find_name2(config, "server",
3242 if (!cs) goto add_sockets;
3245 * Should really abstract this code...
3247 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
3249 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
3250 this = listen_parse(subcs, name2);
3257 last = &(this->next);
3258 } /* loop over "listen" directives in server <foo> */
3264 * Walk through the "listen" sections, if they exist.
3266 for (cs = cf_subsection_find_next(config, NULL, "listen");
3268 cs = cf_subsection_find_next(config, cs, "listen")) {
3269 this = listen_parse(cs, NULL);
3276 last = &(this->next);
3280 * Check virtual servers for "listen" sections, too.
3282 * FIXME: Move to virtual server init?
3284 for (cs = cf_subsection_find_next(config, NULL, "server");
3286 cs = cf_subsection_find_next(config, cs, "server")) {
3287 CONF_SECTION *subcs;
3288 char const *name2 = cf_section_name2(cs);
3290 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
3292 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
3293 this = listen_parse(subcs, name2);
3300 last = &(this->next);
3301 } /* loop over "listen" directives in virtual servers */
3302 } /* loop over virtual servers */
3306 * No sockets to receive packets, this is an error.
3307 * proxying is pointless.
3310 ERROR("The server is not configured to listen on any ports. Cannot start");
3315 * Print out which sockets we're listening on, and
3316 * add them to the event list.
3318 for (this = *head; this != NULL; this = this->next) {
3320 if (this->type == RAD_LISTEN_PROXY) {
3321 defined_proxy = true;
3327 if (!check_config && !spawn_flag && this->tls) {
3328 cf_log_err_cs(this->cs, "Threading must be enabled for TLS sockets to function properly");
3329 cf_log_err_cs(this->cs, "You probably need to do '%s -fxx -l stdout' for debugging",
3334 if (!check_config) {
3335 if (this->workers && !spawn_flag) {
3336 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
3340 if (this->workers) {
3341 #ifdef HAVE_PTHREAD_H
3346 this->print(this, buffer, sizeof(buffer));
3348 for (i = 0; i < this->workers; i++) {
3352 * FIXME: create detached?
3354 rcode = pthread_create(&id, 0, recv_thread, this);
3356 ERROR("Thread create failed: %s",
3357 fr_syserror(rcode));
3361 DEBUG("Thread %d for %s\n", i, buffer);
3364 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
3369 radius_update_listener(this);
3376 if (!home_servers_udp) defined_proxy = true;
3380 * If we're proxying requests, open the proxy FD.
3381 * Otherwise, don't do anything.
3384 if ((main_config.proxy_requests == true) &&
3386 (*head != NULL) && !defined_proxy) {
3390 memset(&home, 0, sizeof(home));
3393 * Open a default UDP port
3395 home.proto = IPPROTO_UDP;
3396 home.src_ipaddr = server_ipaddr;
3400 * Address is still unspecified, use IPv4.
3402 if (home.src_ipaddr.af == AF_UNSPEC) {
3403 home.src_ipaddr.af = AF_INET;
3404 /* everything else is already set to zero */
3407 home.ipaddr.af = home.src_ipaddr.af;
3408 /* everything else is already set to zero */
3411 * It's OK to allocate a UDP listener from the
3412 * main config. The listener will never be
3413 * deleted until the server stops and the config
3416 this = proxy_new_listener(config, &home, port);
3422 radius_update_listener(this);
3427 * Haven't defined any sockets. Die.
3429 if (!*head) return -1;
3431 xlat_register("listen", xlat_listen, NULL, NULL);
3437 * Free a linked list of listeners;
3439 void listen_free(rad_listen_t **head)
3443 if (!head || !*head) return;
3447 rad_listen_t *next = this->next;
3456 RADCLIENT_LIST *listener_find_client_list(fr_ipaddr_t const *ipaddr, uint16_t port, int proto)
3460 for (this = main_config.listen; this != NULL; this = this->next) {
3461 listen_socket_t *sock;
3463 if ((this->type != RAD_LISTEN_AUTH)
3464 #ifdef WITH_ACCOUNTING
3465 && (this->type != RAD_LISTEN_ACCT)
3468 && (this->type != RAD_LISTEN_COA)
3474 if (sock->my_port != port) continue;
3475 if (sock->proto != proto) continue;
3476 if (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) != 0) continue;
3478 return sock->clients;
3485 rad_listen_t *listener_find_byipaddr(fr_ipaddr_t const *ipaddr, uint16_t port, int proto)
3489 for (this = main_config.listen; this != NULL; this = this->next) {
3490 listen_socket_t *sock;
3494 if (sock->my_port != port) continue;
3495 if (sock->proto != proto) continue;
3496 if (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) != 0) continue;
3502 * Failed to find a specific one. Find INADDR_ANY
3504 for (this = main_config.listen; this != NULL; this = this->next) {
3505 listen_socket_t *sock;
3509 if (sock->my_port != port) continue;
3510 if (sock->proto != proto) continue;
3511 if (!fr_inaddr_any(&sock->my_ipaddr)) continue;