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 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 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 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 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 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 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 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;
1982 packet = rad_recv(NULL, listener->fd, 0);
1984 ERROR("Receive - %s", fr_strerror());
1988 switch (packet->code) {
1989 case PW_CODE_ACCESS_ACCEPT:
1990 case PW_CODE_ACCESS_CHALLENGE:
1991 case PW_CODE_ACCESS_REJECT:
1994 #ifdef WITH_ACCOUNTING
1995 case PW_CODE_ACCOUNTING_RESPONSE:
2000 case PW_CODE_DISCONNECT_ACK:
2001 case PW_CODE_DISCONNECT_NAK:
2002 case PW_CODE_COA_ACK:
2003 case PW_CODE_COA_NAK:
2009 * FIXME: Update MIB for packet types?
2011 ERROR("Invalid packet code %d sent to a proxy port "
2012 "from home server %s port %d - ID %d : IGNORED",
2014 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
2015 packet->src_port, packet->id);
2017 listener->stats.total_unknown_types++;
2023 if (!request_proxy_reply(packet)) {
2025 listener->stats.total_packets_dropped++;
2036 * Recieve packets from a proxy socket.
2038 static int proxy_socket_tcp_recv(rad_listen_t *listener)
2040 RADIUS_PACKET *packet;
2041 listen_socket_t *sock = listener->data;
2044 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return 0;
2046 packet = fr_tcp_recv(listener->fd, 0);
2048 listener->status = RAD_LISTEN_STATUS_EOL;
2049 radius_update_listener(listener);
2054 * FIXME: Client MIB updates?
2056 switch (packet->code) {
2057 case PW_CODE_ACCESS_ACCEPT:
2058 case PW_CODE_ACCESS_CHALLENGE:
2059 case PW_CODE_ACCESS_REJECT:
2062 #ifdef WITH_ACCOUNTING
2063 case PW_CODE_ACCOUNTING_RESPONSE:
2069 * FIXME: Update MIB for packet types?
2071 ERROR("Invalid packet code %d sent to a proxy port "
2072 "from home server %s port %d - ID %d : IGNORED",
2074 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
2075 packet->src_port, packet->id);
2080 packet->src_ipaddr = sock->other_ipaddr;
2081 packet->src_port = sock->other_port;
2082 packet->dst_ipaddr = sock->my_ipaddr;
2083 packet->dst_port = sock->my_port;
2086 * FIXME: Have it return an indication of packets that
2087 * are OK to ignore (dups, too late), versus ones that
2088 * aren't OK to ignore (unknown response, spoofed, etc.)
2090 * Close the socket on bad packets...
2092 if (!request_proxy_reply(packet)) {
2097 sock->opened = sock->last_packet = time(NULL);
2105 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
2107 if (!request->reply->code) return 0;
2109 if (rad_encode(request->reply, request->packet, request->client->secret) < 0) {
2110 RERROR("Failed encoding packet: %s", fr_strerror());
2115 if (rad_sign(request->reply, request->packet, request->client->secret) < 0) {
2116 RERROR("Failed signing packet: %s", fr_strerror());
2125 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
2128 listen_socket_t *sock;
2131 if (rad_verify(request->packet, NULL,
2132 request->client->secret) < 0) {
2137 sock = request->listener->data;
2138 rad_assert(sock != NULL);
2141 * FIXME: Add the rest of the TLS parameters, too? But
2142 * how do we separate EAP-TLS parameters from RADIUS/TLS
2145 if (sock->ssn && sock->ssn->ssl) {
2146 #ifdef PSK_MAX_IDENTITY_LEN
2147 const char *identity = SSL_get_psk_identity(sock->ssn->ssl);
2149 RDEBUG("Retrieved psk identity: %s", identity);
2150 pair_make_request("TLS-PSK-Identity", identity, T_OP_SET);
2156 return rad_decode(request->packet, NULL,
2157 request->client->secret);
2161 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
2163 if (rad_encode(request->proxy, NULL, request->home_server->secret) < 0) {
2164 RERROR("Failed encoding proxied packet: %s", fr_strerror());
2169 if (rad_sign(request->proxy, NULL, request->home_server->secret) < 0) {
2170 RERROR("Failed signing proxied packet: %s", fr_strerror());
2179 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
2182 * rad_verify is run in event.c, received_proxy_response()
2185 return rad_decode(request->proxy_reply, request->proxy,
2186 request->home_server->secret);
2190 #include "command.c"
2193 * Temporarily NOT const!
2195 static fr_protocol_t master_listen[RAD_LISTEN_MAX] = {
2197 { RLM_MODULE_INIT, "status", sizeof(listen_socket_t), NULL,
2198 common_socket_parse, NULL,
2199 stats_socket_recv, auth_socket_send,
2200 common_socket_print, client_socket_encode, client_socket_decode },
2203 * This always gets defined.
2205 { RLM_MODULE_INIT, "status", 0, NULL,
2206 NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
2211 { RLM_MODULE_INIT, "proxy", sizeof(listen_socket_t), NULL,
2212 common_socket_parse, common_socket_free,
2213 proxy_socket_recv, proxy_socket_send,
2214 common_socket_print, proxy_socket_encode, proxy_socket_decode },
2217 /* authentication */
2218 { RLM_MODULE_INIT, "auth", sizeof(listen_socket_t), NULL,
2219 common_socket_parse, common_socket_free,
2220 auth_socket_recv, auth_socket_send,
2221 common_socket_print, client_socket_encode, client_socket_decode },
2223 #ifdef WITH_ACCOUNTING
2225 { RLM_MODULE_INIT, "acct", sizeof(listen_socket_t), NULL,
2226 common_socket_parse, common_socket_free,
2227 acct_socket_recv, acct_socket_send,
2228 common_socket_print, client_socket_encode, client_socket_decode},
2233 { RLM_MODULE_INIT, "detail", sizeof(listen_detail_t), NULL,
2234 detail_parse, detail_free,
2235 detail_recv, detail_send,
2236 detail_print, detail_encode, detail_decode },
2240 /* vlan query protocol */
2241 { 0, "vmps", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
2245 /* dhcp query protocol */
2246 { 0, "dhcp", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
2249 #ifdef WITH_COMMAND_SOCKET
2250 /* TCP command socket */
2251 { RLM_MODULE_INIT, "control", sizeof(fr_command_socket_t), NULL,
2252 command_socket_parse, command_socket_free,
2253 command_domain_accept, command_domain_send,
2254 command_socket_print, command_socket_encode, command_socket_decode },
2258 /* Change of Authorization */
2259 { RLM_MODULE_INIT, "coa", sizeof(listen_socket_t), NULL,
2260 common_socket_parse, NULL,
2261 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
2262 common_socket_print, client_socket_encode, client_socket_decode },
2270 * Binds a listener to a socket.
2272 static int listen_bind(rad_listen_t *this)
2275 struct sockaddr_storage salocal;
2277 listen_socket_t *sock = this->data;
2279 #define proto_for_port "udp"
2280 #define sock_type SOCK_DGRAM
2282 char const *proto_for_port = "udp";
2283 int sock_type = SOCK_DGRAM;
2285 if (sock->proto == IPPROTO_TCP) {
2287 if (this->type == RAD_LISTEN_VQP) {
2288 ERROR("VQP does not support TCP transport");
2293 proto_for_port = "tcp";
2294 sock_type = SOCK_STREAM;
2299 * If the port is zero, then it means the appropriate
2300 * thing from /etc/services.
2302 if (sock->my_port == 0) {
2303 struct servent *svp;
2305 switch (this->type) {
2306 case RAD_LISTEN_AUTH:
2307 svp = getservbyname ("radius", proto_for_port);
2309 sock->my_port = ntohs(svp->s_port);
2311 sock->my_port = PW_AUTH_UDP_PORT;
2315 #ifdef WITH_ACCOUNTING
2316 case RAD_LISTEN_ACCT:
2317 svp = getservbyname ("radacct", proto_for_port);
2319 sock->my_port = ntohs(svp->s_port);
2321 sock->my_port = PW_ACCT_UDP_PORT;
2327 case RAD_LISTEN_PROXY:
2328 /* leave it at zero */
2333 case RAD_LISTEN_VQP:
2334 sock->my_port = 1589;
2338 #ifdef WITH_COMMAND_SOCKET
2339 case RAD_LISTEN_COMMAND:
2340 sock->my_port = PW_RADMIN_PORT;
2345 case RAD_LISTEN_COA:
2346 svp = getservbyname ("radius-dynauth", "udp");
2348 sock->my_port = ntohs(svp->s_port);
2350 sock->my_port = PW_COA_UDP_PORT;
2356 WARN("Internal sanity check failed in binding to socket. Ignoring problem");
2362 * Don't open sockets if we're checking the config.
2370 * Copy fr_socket() here, as we may need to bind to a device.
2372 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
2376 this->print(this, buffer, sizeof(buffer));
2378 ERROR("Failed opening %s: %s", buffer, fr_syserror(errno));
2384 * We don't want child processes inheriting these
2387 rcode = fcntl(this->fd, F_GETFD);
2389 if (fcntl(this->fd, F_SETFD, rcode | FD_CLOEXEC) < 0) {
2391 ERROR("Failed setting close on exec: %s", fr_syserror(errno));
2398 * Bind to a device BEFORE touching IP addresses.
2400 if (sock->interface) {
2401 #ifdef SO_BINDTODEVICE
2404 memset(&ifreq, 0, sizeof(ifreq));
2405 strlcpy(ifreq.ifr_name, sock->interface, sizeof(ifreq.ifr_name));
2408 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
2409 (char *)&ifreq, sizeof(ifreq));
2413 ERROR("Failed binding to interface %s: %s",
2414 sock->interface, fr_syserror(errno));
2416 } /* else it worked. */
2418 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2419 #ifdef HAVE_NET_IF_H
2421 * Odds are that any system supporting "bind to
2422 * device" also supports IPv6, so this next bit
2423 * isn't necessary. But it's here for
2426 * If we're doing IPv6, and the scope hasn't yet
2427 * been defined, set the scope to the scope of
2430 if (sock->my_ipaddr.af == AF_INET6) {
2431 if (sock->my_ipaddr.scope == 0) {
2432 sock->my_ipaddr.scope = if_nametoindex(sock->interface);
2433 if (sock->my_ipaddr.scope == 0) {
2435 ERROR("Failed finding interface %s: %s",
2436 sock->interface, fr_syserror(errno));
2439 } /* else scope was defined: we're OK. */
2444 * IPv4: no link local addresses,
2445 * and no bind to device.
2449 ERROR("Failed binding to interface %s: \"bind to device\" is unsupported", sock->interface);
2456 if (sock->proto == IPPROTO_TCP) {
2459 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2461 ERROR("Failed to reuse address: %s", fr_syserror(errno));
2467 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
2468 else /* UDP sockets get UDPfromto */
2471 #ifdef WITH_UDPFROMTO
2473 * Initialize udpfromto for all sockets.
2475 if (udpfromto_init(this->fd) != 0) {
2476 ERROR("Failed initializing udpfromto: %s",
2477 fr_syserror(errno));
2484 * Set up sockaddr stuff.
2486 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
2491 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2492 if (sock->my_ipaddr.af == AF_INET6) {
2494 * Listening on '::' does NOT get you IPv4 to
2495 * IPv6 mapping. You've got to listen on an IPv4
2496 * address, too. This makes the rest of the server
2497 * design a little simpler.
2501 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2504 if (setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2505 (char *)&on, sizeof(on)) < 0) {
2506 ERROR("Failed setting socket to IPv6 "
2507 "only: %s", fr_syserror(errno));
2513 #endif /* IPV6_V6ONLY */
2515 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2517 if (sock->my_ipaddr.af == AF_INET) {
2518 #if (defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)) || defined(IP_DONTFRAG)
2522 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2525 * Disable PMTU discovery. On Linux, this
2526 * also makes sure that the "don't fragment"
2529 flag = IP_PMTUDISC_DONT;
2530 if (setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2531 &flag, sizeof(flag)) < 0) {
2532 ERROR("Failed disabling PMTU discovery: %s",
2533 fr_syserror(errno));
2540 #if defined(IP_DONTFRAG)
2542 * Ensure that the "don't fragment" flag is zero.
2545 if (setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2546 &flag, sizeof(flag)) < 0) {
2547 ERROR("Failed setting don't fragment flag: %s",
2548 fr_syserror(errno));
2558 if (sock->broadcast) {
2561 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2562 ERROR("Can't set broadcast option: %s",
2563 fr_syserror(errno));
2571 * May be binding to priviledged ports.
2573 if (sock->my_port != 0) {
2575 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2581 this->print(this, buffer, sizeof(buffer));
2582 ERROR("Failed binding to %s: %s\n",
2583 buffer, fr_syserror(errno));
2588 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2589 * kernel instead binds us to a 1.2.3.4. If this
2590 * happens, notice, and remember our real IP.
2593 struct sockaddr_storage src;
2594 socklen_t sizeof_src = sizeof(src);
2596 memset(&src, 0, sizeof_src);
2597 if (getsockname(this->fd, (struct sockaddr *) &src,
2599 ERROR("Failed getting socket name: %s",
2600 fr_syserror(errno));
2604 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2605 &sock->my_ipaddr, &sock->my_port)) {
2606 ERROR("Socket has unsupported address family");
2613 if (sock->proto == IPPROTO_TCP) {
2615 * If there are hard-coded worker threads, OR
2616 * it's a TLS connection, it's blocking.
2618 * Otherwise, they're non-blocking.
2623 && (this->type == RAD_LISTEN_PROXY) && !this->tls
2627 if (fr_nonblock(this->fd) < 0) {
2629 ERROR("Failed setting non-blocking on socket: %s",
2630 fr_syserror(errno));
2636 * Allow a backlog of 8 listeners, but only for incoming interfaces.
2639 if (this->type != RAD_LISTEN_PROXY)
2641 if (listen(this->fd, 8) < 0) {
2643 ERROR("Failed in listen(): %s", fr_syserror(errno));
2650 * Mostly for proxy sockets.
2652 sock->other_ipaddr.af = sock->my_ipaddr.af;
2655 * Don't screw up other people.
2657 #undef proto_for_port
2664 static int _listener_free(rad_listen_t *this)
2667 * Other code may have eaten the FD.
2669 if (this->fd >= 0) close(this->fd);
2671 if (master_listen[this->type].free) {
2672 master_listen[this->type].free(this);
2676 if ((this->type == RAD_LISTEN_AUTH)
2678 || (this->type == RAD_LISTEN_ACCT)
2681 || (this->type == RAD_LISTEN_PROXY)
2683 #ifdef WITH_COMMAND_SOCKET
2684 || ((this->type == RAD_LISTEN_COMMAND) &&
2685 (((fr_command_socket_t *) this->data)->magic != COMMAND_SOCKET_MAGIC))
2688 listen_socket_t *sock = this->data;
2690 rad_assert(talloc_parent(sock) == this);
2691 rad_assert(sock->ev == NULL);
2694 * Remove the child from the parent tree.
2697 rbtree_deletebydata(this->parent->children, this);
2701 * Delete / close all of the children, too!
2703 if (this->children) {
2704 rbtree_walk(this->children, RBTREE_DELETE_ORDER, listener_unlink, this);
2709 * Note that we do NOT free this->tls, as the
2710 * pointer is parented by its CONF_SECTION. It
2711 * may be used by multiple listeners.
2714 rad_assert(!sock->ssn || (talloc_parent(sock->ssn) == sock));
2715 rad_assert(!sock->request || (talloc_parent(sock->request) == sock));
2716 #ifdef HAVE_PTHREAD_H
2717 pthread_mutex_destroy(&(sock->mutex));
2720 #endif /* WITH_TLS */
2722 #endif /* WITH_TCP */
2729 * Allocate & initialize a new listener.
2731 static rad_listen_t *listen_alloc(TALLOC_CTX *ctx, RAD_LISTEN_TYPE type)
2735 this = talloc_zero(ctx, rad_listen_t);
2738 this->recv = master_listen[this->type].recv;
2739 this->send = master_listen[this->type].send;
2740 this->print = master_listen[this->type].print;
2741 this->encode = master_listen[this->type].encode;
2742 this->decode = master_listen[this->type].decode;
2744 talloc_set_destructor(this, _listener_free);
2746 this->data = talloc_zero_array(this, uint8_t, master_listen[this->type].inst_size);
2753 * Externally visible function for creating a new proxy LISTENER.
2755 * Not thread-safe, but all calls to it are protected by the
2756 * proxy mutex in event.c
2758 rad_listen_t *proxy_new_listener(TALLOC_CTX *ctx, home_server_t *home, uint16_t src_port)
2762 listen_socket_t *sock;
2765 if (!home) return NULL;
2767 rad_assert(home->server == NULL); /* we only open real sockets */
2769 if ((home->limit.max_connections > 0) &&
2770 (home->limit.num_connections >= home->limit.max_connections)) {
2771 RATE_LIMIT(INFO("Home server %s has too many open connections (%d)",
2772 home->log_name, home->limit.max_connections));
2777 if (home->last_failed_open == now) {
2778 WARN("Suppressing attempt to open socket to 'down' home server");
2782 this = listen_alloc(ctx, RAD_LISTEN_PROXY);
2785 sock->other_ipaddr = home->ipaddr;
2786 sock->other_port = home->port;
2789 sock->my_ipaddr = home->src_ipaddr;
2790 sock->my_port = src_port;
2791 sock->proto = home->proto;
2794 * For error messages.
2796 this->print(this, buffer, sizeof(buffer));
2798 if (rad_debug_lvl >= 2) {
2799 DEBUG("Opening new proxy socket '%s'", buffer);
2803 sock->opened = sock->last_packet = now;
2805 if (home->proto == IPPROTO_TCP) {
2806 this->recv = proxy_socket_tcp_recv;
2809 * FIXME: connect() is blocking!
2810 * We do this with the proxy mutex locked, which may
2811 * cause large delays!
2813 * http://www.developerweb.net/forum/showthread.php?p=13486
2815 this->fd = fr_socket_client_tcp(&home->src_ipaddr,
2816 &home->ipaddr, home->port, false);
2819 this->fd = fr_socket(&home->src_ipaddr, src_port);
2822 this->print(this, buffer,sizeof(buffer));
2823 ERROR("Failed opening proxy socket '%s' : %s",
2824 buffer, fr_strerror());
2825 home->last_failed_open = now;
2833 if ((home->proto == IPPROTO_TCP) && home->tls) {
2834 DEBUG("Trying SSL to port %d\n", home->port);
2835 sock->ssn = tls_new_client_session(sock, home->tls, this->fd);
2837 ERROR("Failed starting SSL to '%s'", buffer);
2838 home->last_failed_open = now;
2843 this->recv = proxy_tls_recv;
2844 this->send = proxy_tls_send;
2849 * Figure out which port we were bound to.
2851 if (sock->my_port == 0) {
2852 struct sockaddr_storage src;
2853 socklen_t sizeof_src = sizeof(src);
2855 memset(&src, 0, sizeof_src);
2856 if (getsockname(this->fd, (struct sockaddr *) &src,
2858 ERROR("Failed getting socket name for '%s': %s",
2859 buffer, fr_syserror(errno));
2860 home->last_failed_open = now;
2865 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2866 &sock->my_ipaddr, &sock->my_port)) {
2867 ERROR("Socket has unsupported address family for '%s'", buffer);
2868 home->last_failed_open = now;
2874 home->limit.num_connections++;
2880 static const FR_NAME_NUMBER listen_compare[] = {
2882 { "status", RAD_LISTEN_NONE },
2884 { "auth", RAD_LISTEN_AUTH },
2885 #ifdef WITH_ACCOUNTING
2886 { "acct", RAD_LISTEN_ACCT },
2887 { "auth+acct", RAD_LISTEN_AUTH },
2890 { "detail", RAD_LISTEN_DETAIL },
2893 { "proxy", RAD_LISTEN_PROXY },
2896 { "vmps", RAD_LISTEN_VQP },
2899 { "dhcp", RAD_LISTEN_DHCP },
2901 #ifdef WITH_COMMAND_SOCKET
2902 { "control", RAD_LISTEN_COMMAND },
2905 { "coa", RAD_LISTEN_COA },
2910 static int _free_proto_handle(lt_dlhandle *handle)
2916 static rad_listen_t *listen_parse(CONF_SECTION *cs, char const *server)
2919 char const *listen_type;
2924 CONF_SECTION *server_cs;
2927 cp = cf_pair_find(cs, "type");
2930 "No type specified in listen section");
2934 value = cf_pair_value(cp);
2937 "Type cannot be empty");
2941 snprintf(buffer, sizeof(buffer), "proto_%s", value);
2942 handle = lt_dlopenext(buffer);
2944 fr_protocol_t *proto;
2945 lt_dlhandle *marker;
2947 proto = dlsym(handle, buffer);
2950 "Failed linking to protocol %s : %s\n",
2956 type = fr_str2int(listen_compare, value, -1);
2957 rad_assert(type >= 0); /* shouldn't be able to compile an invalid type */
2959 memcpy(&master_listen[type], proto, sizeof(*proto));
2962 * Ensure handle gets closed if config section gets freed
2964 marker = talloc(cs, lt_dlhandle);
2966 talloc_set_destructor(marker, _free_proto_handle);
2968 if (master_listen[type].magic != RLM_MODULE_INIT) {
2969 ERROR("Failed to load protocol '%s', it has the wrong version.",
2970 master_listen[type].name);
2975 cf_log_info(cs, "listen {");
2978 rcode = cf_item_parse(cs, "type", FR_ITEM_POINTER(PW_TYPE_STRING, &listen_type), "");
2979 if (rcode < 0) return NULL;
2982 "No type specified in listen section");
2986 type = fr_str2int(listen_compare, listen_type, -1);
2989 "Invalid type \"%s\" in listen section.",
2995 * DHCP and VMPS *must* be loaded dynamically.
2997 if (master_listen[type].magic != RLM_MODULE_INIT) {
2998 ERROR("Cannot load protocol '%s', as the required library does not exist",
2999 master_listen[type].name);
3004 * Allow listen sections in the default config to
3005 * refer to a server.
3008 rcode = cf_item_parse(cs, "virtual_server", FR_ITEM_POINTER(PW_TYPE_STRING, &server), NULL);
3009 if (rcode < 0) return NULL;
3014 * We were passed a virtual server, so the caller is
3015 * defining a proxy listener inside of a virtual server.
3016 * This isn't allowed right now.
3018 else if (type == RAD_LISTEN_PROXY) {
3019 ERROR("Error: listen type \"proxy\" Cannot appear in a virtual server section");
3025 * Set up cross-type data.
3027 this = listen_alloc(cs, type);
3028 this->server = server;
3033 * Special-case '+' for "auth+acct".
3035 if (strchr(listen_type, '+') != NULL) {
3041 * Call per-type parser.
3043 if (master_listen[type].parse(cs, this) < 0) {
3048 server_cs = cf_section_sub_find_name2(main_config.config, "server",
3050 if (!server_cs && this->server) {
3051 cf_log_err_cs(cs, "No such server \"%s\"", this->server);
3056 cf_log_info(cs, "}");
3061 #ifdef HAVE_PTHREAD_H
3063 * A child thread which does NOTHING other than read and process
3066 static void *recv_thread(void *arg)
3068 rad_listen_t *this = arg;
3081 * Generate a list of listeners. Takes an input list of
3082 * listeners, too, so we don't close sockets with waiting packets.
3084 int listen_init(CONF_SECTION *config, rad_listen_t **head, bool spawn_flag)
3086 bool override = false;
3087 CONF_SECTION *cs = NULL;
3088 rad_listen_t **last;
3090 fr_ipaddr_t server_ipaddr;
3091 uint16_t auth_port = 0;
3093 bool defined_proxy = false;
3097 * We shouldn't be called with a pre-existing list.
3099 rad_assert(head && (*head == NULL));
3101 memset(&server_ipaddr, 0, sizeof(server_ipaddr));
3104 server_ipaddr.af = AF_UNSPEC;
3107 * If the port is specified on the command-line,
3108 * it over-rides the configuration file.
3110 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
3112 if (main_config.port > 0) {
3113 auth_port = main_config.port;
3116 * -p X but no -i Y on the command-line.
3118 if (main_config.myip.af == AF_UNSPEC) {
3119 ERROR("The command-line says \"-p %d\", but there is no associated IP address to use",
3126 * If the IP address was configured on the command-line,
3127 * use that as the "bind_address"
3129 if (main_config.myip.af != AF_UNSPEC) {
3130 listen_socket_t *sock;
3132 memcpy(&server_ipaddr, &main_config.myip,
3133 sizeof(server_ipaddr));
3137 if (strcmp(progname, "vmpsd") == 0) {
3138 this = listen_alloc(config, RAD_LISTEN_VQP);
3139 if (!auth_port) auth_port = 1589;
3142 this = listen_alloc(config, RAD_LISTEN_AUTH);
3146 sock->my_ipaddr = server_ipaddr;
3147 sock->my_port = auth_port;
3149 sock->clients = client_list_parse_section(config, false);
3150 if (!sock->clients) {
3151 cf_log_err_cs(config,
3152 "Failed to find any clients for this listen section");
3157 if (listen_bind(this) < 0) {
3159 ERROR("There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
3163 auth_port = sock->my_port; /* may have been updated in listen_bind */
3165 cs = cf_section_sub_find_name2(config, "server",
3167 if (cs) this->server = main_config.name;
3171 last = &(this->next);
3177 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
3180 #ifdef WITH_ACCOUNTING
3182 * Open Accounting Socket.
3184 * If we haven't already gotten acct_port from
3185 * /etc/services, then make it auth_port + 1.
3187 this = listen_alloc(config, RAD_LISTEN_ACCT);
3191 * Create the accounting socket.
3193 * The accounting port is always the
3194 * authentication port + 1
3196 sock->my_ipaddr = server_ipaddr;
3197 sock->my_port = auth_port + 1;
3199 sock->clients = client_list_parse_section(config, false);
3200 if (!sock->clients) {
3201 cf_log_err_cs(config,
3202 "Failed to find any clients for this listen section");
3206 if (listen_bind(this) < 0) {
3209 ERROR("There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
3214 cs = cf_section_sub_find_name2(config, "server",
3216 if (cs) this->server = main_config.name;
3220 last = &(this->next);
3225 * They specified an IP on the command-line, ignore
3226 * all listen sections except the one in '-n'.
3228 if (main_config.myip.af != AF_UNSPEC) {
3229 CONF_SECTION *subcs;
3230 char const *name2 = cf_section_name2(cs);
3232 cs = cf_section_sub_find_name2(config, "server",
3234 if (!cs) goto add_sockets;
3237 * Should really abstract this code...
3239 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
3241 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
3242 this = listen_parse(subcs, name2);
3249 last = &(this->next);
3250 } /* loop over "listen" directives in server <foo> */
3256 * Walk through the "listen" sections, if they exist.
3258 for (cs = cf_subsection_find_next(config, NULL, "listen");
3260 cs = cf_subsection_find_next(config, cs, "listen")) {
3261 this = listen_parse(cs, NULL);
3268 last = &(this->next);
3272 * Check virtual servers for "listen" sections, too.
3274 * FIXME: Move to virtual server init?
3276 for (cs = cf_subsection_find_next(config, NULL, "server");
3278 cs = cf_subsection_find_next(config, cs, "server")) {
3279 CONF_SECTION *subcs;
3280 char const *name2 = cf_section_name2(cs);
3282 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
3284 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
3285 this = listen_parse(subcs, name2);
3292 last = &(this->next);
3293 } /* loop over "listen" directives in virtual servers */
3294 } /* loop over virtual servers */
3298 * No sockets to receive packets, this is an error.
3299 * proxying is pointless.
3302 ERROR("The server is not configured to listen on any ports. Cannot start");
3307 * Print out which sockets we're listening on, and
3308 * add them to the event list.
3310 for (this = *head; this != NULL; this = this->next) {
3312 if (this->type == RAD_LISTEN_PROXY) {
3313 defined_proxy = true;
3319 if (!check_config && !spawn_flag && this->tls) {
3320 cf_log_err_cs(this->cs, "Threading must be enabled for TLS sockets to function properly");
3321 cf_log_err_cs(this->cs, "You probably need to do '%s -fxx -l stdout' for debugging",
3326 if (!check_config) {
3327 if (this->workers && !spawn_flag) {
3328 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
3332 if (this->workers) {
3333 #ifdef HAVE_PTHREAD_H
3338 this->print(this, buffer, sizeof(buffer));
3340 for (i = 0; i < this->workers; i++) {
3344 * FIXME: create detached?
3346 rcode = pthread_create(&id, 0, recv_thread, this);
3348 ERROR("Thread create failed: %s",
3349 fr_syserror(rcode));
3353 DEBUG("Thread %d for %s\n", i, buffer);
3356 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
3361 radius_update_listener(this);
3368 if (!home_servers_udp) defined_proxy = true;
3372 * If we're proxying requests, open the proxy FD.
3373 * Otherwise, don't do anything.
3376 if ((main_config.proxy_requests == true) &&
3378 (*head != NULL) && !defined_proxy) {
3382 memset(&home, 0, sizeof(home));
3385 * Open a default UDP port
3387 home.proto = IPPROTO_UDP;
3388 home.src_ipaddr = server_ipaddr;
3392 * Address is still unspecified, use IPv4.
3394 if (home.src_ipaddr.af == AF_UNSPEC) {
3395 home.src_ipaddr.af = AF_INET;
3396 /* everything else is already set to zero */
3399 home.ipaddr.af = home.src_ipaddr.af;
3400 /* everything else is already set to zero */
3403 * It's OK to allocate a UDP listener from the
3404 * main config. The listener will never be
3405 * deleted until the server stops and the config
3408 this = proxy_new_listener(config, &home, port);
3414 radius_update_listener(this);
3419 * Haven't defined any sockets. Die.
3421 if (!*head) return -1;
3423 xlat_register("listen", xlat_listen, NULL, NULL);
3429 * Free a linked list of listeners;
3431 void listen_free(rad_listen_t **head)
3435 if (!head || !*head) return;
3439 rad_listen_t *next = this->next;
3448 RADCLIENT_LIST *listener_find_client_list(fr_ipaddr_t const *ipaddr, uint16_t port, int proto)
3452 for (this = main_config.listen; this != NULL; this = this->next) {
3453 listen_socket_t *sock;
3455 if ((this->type != RAD_LISTEN_AUTH)
3456 #ifdef WITH_ACCOUNTING
3457 && (this->type != RAD_LISTEN_ACCT)
3460 && (this->type != RAD_LISTEN_COA)
3466 if (sock->my_port != port) continue;
3467 if (sock->proto != proto) continue;
3468 if (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) != 0) continue;
3470 return sock->clients;
3477 rad_listen_t *listener_find_byipaddr(fr_ipaddr_t const *ipaddr, uint16_t port, int proto)
3481 for (this = main_config.listen; this != NULL; this = this->next) {
3482 listen_socket_t *sock;
3486 if (sock->my_port != port) continue;
3487 if (sock->proto != proto) continue;
3488 if (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) != 0) continue;
3494 * Failed to find a specific one. Find INADDR_ANY
3496 for (this = main_config.listen; this != NULL; this = this->next) {
3497 listen_socket_t *sock;
3501 if (sock->my_port != port) continue;
3502 if (sock->proto != proto) continue;
3503 if (!fr_inaddr_any(&sock->my_ipaddr)) continue;