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 },
923 CONF_PARSER_TERMINATOR
927 static CONF_PARSER limit_config[] = {
928 { "max_pps", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_socket_t, max_rate), NULL },
931 { "max_connections", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_socket_t, limit.max_connections), "16" },
932 { "lifetime", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_socket_t, limit.lifetime), "0" },
933 { "idle_timeout", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_socket_t, limit.idle_timeout), STRINGIFY(30) },
935 CONF_PARSER_TERMINATOR
941 * TLS requires child threads to handle the listeners. Which
942 * means that we need a separate talloc context per child thread.
943 * Which means that we need to manually clean up the child
944 * listeners. Which means we need to manually track them.
946 * All child thread linking/unlinking is done in the master
947 * thread. If we care, we can later add a mutex for the parent
950 static int listener_cmp(void const *one, void const *two)
952 if (one < two) return -1;
953 if (one > two) return +1;
957 static int listener_unlink(UNUSED void *ctx, UNUSED void *data)
959 return 2; /* unlink this node from the tree */
965 * Parse an authentication or accounting socket.
967 int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
970 uint16_t listen_port;
972 listen_socket_t *sock = this->data;
973 char const *section_name = NULL;
974 CONF_SECTION *client_cs, *parentcs;
983 memset(&ipaddr, 0, sizeof(ipaddr));
984 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
986 rcode = cf_item_parse(cs, "ipaddr", FR_ITEM_POINTER(PW_TYPE_COMBO_IP_ADDR, &ipaddr), NULL);
987 if (rcode < 0) return -1;
988 if (rcode != 0) rcode = cf_item_parse(cs, "ipv4addr", FR_ITEM_POINTER(PW_TYPE_IPV4_ADDR, &ipaddr), NULL);
989 if (rcode < 0) return -1;
990 if (rcode != 0) rcode = cf_item_parse(cs, "ipv6addr", FR_ITEM_POINTER(PW_TYPE_IPV6_ADDR, &ipaddr), NULL);
991 if (rcode < 0) return -1;
993 cf_log_err_cs(cs, "No address specified in listen section");
997 rcode = cf_item_parse(cs, "port", FR_ITEM_POINTER(PW_TYPE_SHORT, &listen_port), "0");
998 if (rcode < 0) return -1;
1000 sock->proto = IPPROTO_UDP;
1002 if (cf_pair_find(cs, "proto")) {
1005 "System does not support the TCP protocol. Delete this line from the configuration file");
1008 char const *proto = NULL;
1013 rcode = cf_item_parse(cs, "proto", FR_ITEM_POINTER(PW_TYPE_STRING, &proto), "udp");
1014 if (rcode < 0) return -1;
1016 if (!proto || strcmp(proto, "udp") == 0) {
1017 sock->proto = IPPROTO_UDP;
1019 } else if (strcmp(proto, "tcp") == 0) {
1020 sock->proto = IPPROTO_TCP;
1023 "Unknown proto name \"%s\"", proto);
1028 * TCP requires a destination IP for sockets.
1029 * UDP doesn't, so it's allowed.
1032 if ((this->type == RAD_LISTEN_PROXY) &&
1033 (sock->proto != IPPROTO_UDP)) {
1035 "Proxy listeners can only listen on proto = udp");
1038 #endif /* WITH_PROXY */
1041 tls = cf_section_sub_find(cs, "tls");
1045 * Don't allow TLS configurations for UDP sockets.
1047 if (sock->proto != IPPROTO_TCP) {
1049 "TLS transport is not available for UDP sockets");
1054 * If unset, set to default.
1056 if (listen_port == 0) listen_port = PW_RADIUS_TLS_PORT;
1058 this->tls = tls_server_conf_parse(tls);
1063 #ifdef HAVE_PTRHEAD_H
1064 if (pthread_mutex_init(&sock->mutex, NULL) < 0) {
1072 #else /* WITH_TLS */
1074 * Built without TLS. Disallow it.
1076 if (cf_section_sub_find(cs, "tls")) {
1078 "TLS transport is not available in this executable");
1081 #endif /* WITH_TLS */
1083 #endif /* WITH_TCP */
1086 * No "proto" field. Disallow TLS.
1088 } else if (cf_section_sub_find(cs, "tls")) {
1090 "TLS transport is not available in this \"listen\" section");
1095 * Magical tuning methods!
1097 subcs = cf_section_sub_find(cs, "performance");
1099 rcode = cf_section_parse(subcs, this,
1100 performance_config);
1101 if (rcode < 0) return -1;
1103 if (this->synchronous && sock->max_rate) {
1104 WARN("Setting 'max_pps' is incompatible with 'synchronous'. Disabling 'max_pps'");
1108 if (!this->synchronous && this->workers) {
1109 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
1114 subcs = cf_section_sub_find(cs, "limit");
1116 rcode = cf_section_parse(subcs, sock,
1118 if (rcode < 0) return -1;
1120 if (sock->max_rate && ((sock->max_rate < 10) || (sock->max_rate > 1000000))) {
1122 "Invalid value for \"max_pps\"");
1127 if ((sock->limit.idle_timeout > 0) && (sock->limit.idle_timeout < 5)) {
1128 WARN("Setting idle_timeout to 5");
1129 sock->limit.idle_timeout = 5;
1132 if ((sock->limit.lifetime > 0) && (sock->limit.lifetime < 5)) {
1133 WARN("Setting lifetime to 5");
1134 sock->limit.lifetime = 5;
1137 if ((sock->limit.lifetime > 0) && (sock->limit.idle_timeout > sock->limit.lifetime)) {
1138 WARN("Setting idle_timeout to 0");
1139 sock->limit.idle_timeout = 0;
1143 * Force no duplicate detection for TCP sockets.
1145 if (sock->proto == IPPROTO_TCP) {
1150 sock->limit.max_connections = 60;
1151 sock->limit.idle_timeout = 30;
1152 sock->limit.lifetime = 0;
1156 sock->my_ipaddr = ipaddr;
1157 sock->my_port = listen_port;
1162 * Until there is a side effects free way of forwarding a
1163 * request to another virtual server, this check is invalid,
1164 * and should be left disabled.
1167 if (home_server_find(&sock->my_ipaddr, sock->my_port, sock->proto)) {
1170 ERROR("We have been asked to listen on %s port %d, which is also listed as a "
1171 "home server. This can create a proxy loop",
1172 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)), sock->my_port);
1176 return 0; /* don't do anything */
1181 * If we can bind to interfaces, do so,
1184 cp = cf_pair_find(cs, "interface");
1186 char const *value = cf_pair_value(cp);
1189 "No interface name given");
1192 sock->interface = value;
1197 * If we can do broadcasts..
1199 cp = cf_pair_find(cs, "broadcast");
1201 #ifndef SO_BROADCAST
1203 "System does not support broadcast sockets. Delete this line from the configuration file");
1206 if (this->type != RAD_LISTEN_DHCP) {
1208 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file");
1212 char const *value = cf_pair_value(cp);
1215 "No broadcast value given");
1220 * Hack... whatever happened to cf_section_parse?
1222 sock->broadcast = (strcmp(value, "yes") == 0);
1228 * And bind it to the port.
1230 if (listen_bind(this) < 0) {
1233 "Error binding to port for %s port %d",
1234 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
1241 * Proxy sockets don't have clients.
1243 if (this->type == RAD_LISTEN_PROXY) return 0;
1247 * The more specific configurations are preferred to more
1251 parentcs = cf_top_section(cs);
1252 rcode = cf_item_parse(cs, "clients", FR_ITEM_POINTER(PW_TYPE_STRING, §ion_name), NULL);
1253 if (rcode < 0) return -1; /* bad string */
1256 * Explicit list given: use it.
1258 client_cs = cf_section_sub_find_name2(parentcs, "clients", section_name);
1260 client_cs = cf_section_find(section_name);
1264 "Failed to find clients %s {...}",
1268 } /* else there was no "clients = " entry. */
1271 * The "listen" section wasn't given an explicit client list.
1272 * Look for (a) clients in this virtual server, or
1273 * (b) the global client list.
1276 CONF_SECTION *server_cs;
1278 server_cs = cf_section_sub_find_name2(parentcs,
1282 * Found a "server foo" section. If there are clients
1286 (cf_section_sub_find(server_cs, "client") != NULL)) {
1287 client_cs = server_cs;
1292 * Still nothing. Look for global clients.
1294 if (!client_cs) client_cs = parentcs;
1297 sock->clients = client_list_parse_section(client_cs, (this->tls != NULL));
1299 sock->clients = client_list_parse_section(client_cs, false);
1301 if (!sock->clients) {
1303 "Failed to load clients for this listen section");
1308 if (sock->proto == IPPROTO_TCP) {
1310 * Re-write the listener receive function to
1311 * allow us to accept the socket.
1313 this->recv = dual_tcp_accept;
1315 this->children = rbtree_create(this, listener_cmp, NULL, 0);
1316 if (!this->children) {
1317 cf_log_err_cs(cs, "Failed to create child list for TCP socket.");
1327 * Send an authentication response packet
1329 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1331 rad_assert(request->listener == listener);
1332 rad_assert(listener->send == auth_socket_send);
1334 if (request->reply->code == 0) return 0;
1336 #ifdef WITH_UDPFROMTO
1338 * Overwrite the src ip address on the outbound packet
1339 * with the one specified by the client.
1340 * This is useful to work around broken DSR implementations
1341 * and other routing issues.
1343 if (request->client->src_ipaddr.af != AF_UNSPEC) {
1344 request->reply->src_ipaddr = request->client->src_ipaddr;
1348 if (rad_send(request->reply, request->packet,
1349 request->client->secret) < 0) {
1350 RERROR("Failed sending reply: %s",
1359 #ifdef WITH_ACCOUNTING
1361 * Send an accounting response packet (or not)
1363 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1365 rad_assert(request->listener == listener);
1366 rad_assert(listener->send == acct_socket_send);
1369 * Accounting reject's are silently dropped.
1371 * We do it here to avoid polluting the rest of the
1372 * code with this knowledge
1374 if (request->reply->code == 0) return 0;
1376 #ifdef WITH_UDPFROMTO
1378 * Overwrite the src ip address on the outbound packet
1379 * with the one specified by the client.
1380 * This is useful to work around broken DSR implementations
1381 * and other routing issues.
1383 if (request->client->src_ipaddr.af != AF_UNSPEC) {
1384 request->reply->src_ipaddr = request->client->src_ipaddr;
1388 if (rad_send(request->reply, request->packet,
1389 request->client->secret) < 0) {
1390 RERROR("Failed sending reply: %s",
1401 * Send a packet to a home server.
1403 * FIXME: have different code for proxy auth & acct!
1405 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1407 rad_assert(request->proxy_listener == listener);
1408 rad_assert(listener->send == proxy_socket_send);
1410 if (rad_send(request->proxy, NULL,
1411 request->home_server->secret) < 0) {
1412 RERROR("Failed sending proxied request: %s",
1423 * Check if an incoming request is "ok"
1425 * It takes packets, not requests. It sees if the packet looks
1426 * OK. If so, it does a number of sanity checks on it.
1428 static int stats_socket_recv(rad_listen_t *listener)
1433 RADIUS_PACKET *packet;
1434 RADCLIENT *client = NULL;
1435 fr_ipaddr_t src_ipaddr;
1437 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1438 if (rcode < 0) return 0;
1440 FR_STATS_INC(auth, total_requests);
1442 if (rcode < 20) { /* RADIUS_HDR_LEN */
1443 if (DEBUG_ENABLED) ERROR("Receive - %s", fr_strerror());
1444 FR_STATS_INC(auth, total_malformed_requests);
1448 if ((client = client_listener_find(listener,
1449 &src_ipaddr, src_port)) == NULL) {
1450 rad_recv_discard(listener->fd);
1451 FR_STATS_INC(auth, total_invalid_requests);
1455 FR_STATS_TYPE_INC(client->auth.total_requests);
1458 * We only understand Status-Server on this socket.
1460 if (code != PW_CODE_STATUS_SERVER) {
1461 DEBUG("Ignoring packet code %d sent to Status-Server port",
1463 rad_recv_discard(listener->fd);
1464 FR_STATS_INC(auth, total_unknown_types);
1469 * Now that we've sanity checked everything, receive the
1472 packet = rad_recv(NULL, listener->fd, 1); /* require message authenticator */
1474 FR_STATS_INC(auth, total_malformed_requests);
1475 if (DEBUG_ENABLED) ERROR("Receive - %s", fr_strerror());
1479 if (!request_receive(NULL, listener, packet, client, rad_status_server)) {
1480 FR_STATS_INC(auth, total_packets_dropped);
1491 * Check if an incoming request is "ok"
1493 * It takes packets, not requests. It sees if the packet looks
1494 * OK. If so, it does a number of sanity checks on it.
1496 static int auth_socket_recv(rad_listen_t *listener)
1501 RADIUS_PACKET *packet;
1502 RAD_REQUEST_FUNP fun = NULL;
1503 RADCLIENT *client = NULL;
1504 fr_ipaddr_t src_ipaddr;
1507 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1508 if (rcode < 0) return 0;
1510 FR_STATS_INC(auth, total_requests);
1512 if (rcode < 20) { /* RADIUS_HDR_LEN */
1513 if (DEBUG_ENABLED) ERROR("Receive - %s", fr_strerror());
1514 FR_STATS_INC(auth, total_malformed_requests);
1518 if ((client = client_listener_find(listener,
1519 &src_ipaddr, src_port)) == NULL) {
1520 rad_recv_discard(listener->fd);
1521 FR_STATS_INC(auth, total_invalid_requests);
1525 FR_STATS_TYPE_INC(client->auth.total_requests);
1528 * Some sanity checks, based on the packet code.
1531 case PW_CODE_ACCESS_REQUEST:
1532 fun = rad_authenticate;
1535 case PW_CODE_STATUS_SERVER:
1536 if (!main_config.status_server) {
1537 rad_recv_discard(listener->fd);
1538 FR_STATS_INC(auth, total_unknown_types);
1539 WARN("Ignoring Status-Server request due to security configuration");
1542 fun = rad_status_server;
1546 rad_recv_discard(listener->fd);
1547 FR_STATS_INC(auth, total_unknown_types);
1549 if (DEBUG_ENABLED) ERROR("Receive - Invalid packet code %d sent to authentication port from "
1550 "client %s port %d", code, client->shortname, src_port);
1552 } /* switch over packet types */
1554 ctx = talloc_pool(NULL, main_config.talloc_pool_size);
1556 rad_recv_discard(listener->fd);
1557 FR_STATS_INC(auth, total_packets_dropped);
1560 talloc_set_name_const(ctx, "auth_listener_pool");
1563 * Now that we've sanity checked everything, receive the
1566 packet = rad_recv(ctx, listener->fd, client->message_authenticator);
1568 FR_STATS_INC(auth, total_malformed_requests);
1569 if (DEBUG_ENABLED) ERROR("Receive - %s", fr_strerror());
1575 #ifdef WITH_UDPFROMTO
1577 * This is a NICE Mac OSX bug. Create an interface with
1578 * two IP address, and then configure one listener for
1579 * each IP address. Send thousands of packets to one
1580 * address, and some will show up on the OTHER socket.
1582 * This hack works ONLY if the clients are global. If
1583 * each listener has the same client IP, but with
1584 * different secrets, then it will fail the rad_recv()
1585 * check above, and there's nothing you can do.
1588 listen_socket_t *sock = listener->data;
1589 rad_listen_t *other;
1591 other = listener_find_byipaddr(&packet->dst_ipaddr,
1592 packet->dst_port, sock->proto);
1593 if (other) listener = other;
1598 if (!request_receive(ctx, listener, packet, client, fun)) {
1599 FR_STATS_INC(auth, total_packets_dropped);
1608 #ifdef WITH_ACCOUNTING
1610 * Receive packets from an accounting socket
1612 static int acct_socket_recv(rad_listen_t *listener)
1617 RADIUS_PACKET *packet;
1618 RAD_REQUEST_FUNP fun = NULL;
1619 RADCLIENT *client = NULL;
1620 fr_ipaddr_t src_ipaddr;
1623 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1624 if (rcode < 0) return 0;
1626 FR_STATS_INC(acct, total_requests);
1628 if (rcode < 20) { /* RADIUS_HDR_LEN */
1629 if (DEBUG_ENABLED) ERROR("Receive - %s", fr_strerror());
1630 FR_STATS_INC(acct, total_malformed_requests);
1634 if ((client = client_listener_find(listener,
1635 &src_ipaddr, src_port)) == NULL) {
1636 rad_recv_discard(listener->fd);
1637 FR_STATS_INC(acct, total_invalid_requests);
1641 FR_STATS_TYPE_INC(client->acct.total_requests);
1644 * Some sanity checks, based on the packet code.
1647 case PW_CODE_ACCOUNTING_REQUEST:
1648 fun = rad_accounting;
1651 case PW_CODE_STATUS_SERVER:
1652 if (!main_config.status_server) {
1653 rad_recv_discard(listener->fd);
1654 FR_STATS_INC(acct, total_unknown_types);
1656 WARN("Ignoring Status-Server request due to security configuration");
1659 fun = rad_status_server;
1663 rad_recv_discard(listener->fd);
1664 FR_STATS_INC(acct, total_unknown_types);
1666 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1667 code, client->shortname, src_port);
1669 } /* switch over packet types */
1671 ctx = talloc_pool(NULL, main_config.talloc_pool_size);
1673 rad_recv_discard(listener->fd);
1674 FR_STATS_INC(acct, total_packets_dropped);
1677 talloc_set_name_const(ctx, "acct_listener_pool");
1680 * Now that we've sanity checked everything, receive the
1683 packet = rad_recv(ctx, listener->fd, 0);
1685 FR_STATS_INC(acct, total_malformed_requests);
1686 if (DEBUG_ENABLED) ERROR("Receive - %s", fr_strerror());
1692 * There can be no duplicate accounting packets.
1694 if (!request_receive(ctx, listener, packet, client, fun)) {
1695 FR_STATS_INC(acct, total_packets_dropped);
1707 static int do_proxy(REQUEST *request)
1711 if (request->in_proxy_hash ||
1712 (request->proxy_reply && (request->proxy_reply->code != 0))) {
1716 vp = fr_pair_find_by_num(request->config, PW_HOME_SERVER_POOL, 0, TAG_ANY);
1719 if (!home_pool_byname(vp->vp_strvalue, HOME_TYPE_COA)) {
1720 REDEBUG2("Cannot proxy to unknown pool %s",
1729 * We have a destination IP address. It will (later) proxied.
1731 vp = fr_pair_find_by_num(request->config, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY);
1732 if (!vp) vp = fr_pair_find_by_num(request->config, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY);
1740 * Receive a CoA packet.
1742 int rad_coa_recv(REQUEST *request)
1744 int rcode = RLM_MODULE_OK;
1749 * Get the correct response
1751 switch (request->packet->code) {
1752 case PW_CODE_COA_REQUEST:
1753 ack = PW_CODE_COA_ACK;
1754 nak = PW_CODE_COA_NAK;
1757 case PW_CODE_DISCONNECT_REQUEST:
1758 ack = PW_CODE_DISCONNECT_ACK;
1759 nak = PW_CODE_DISCONNECT_NAK;
1762 default: /* shouldn't happen */
1763 return RLM_MODULE_FAIL;
1767 #define WAS_PROXIED (request->proxy)
1769 #define WAS_PROXIED (0)
1774 * RFC 5176 Section 3.3. If we have a CoA-Request
1775 * with Service-Type = Authorize-Only, it MUST
1776 * have a State attribute in it.
1778 vp = fr_pair_find_by_num(request->packet->vps, PW_SERVICE_TYPE, 0, TAG_ANY);
1779 if (request->packet->code == PW_CODE_COA_REQUEST) {
1780 if (vp && (vp->vp_integer == PW_AUTHORIZE_ONLY)) {
1781 vp = fr_pair_find_by_num(request->packet->vps, PW_STATE, 0, TAG_ANY);
1782 if (!vp || (vp->vp_length == 0)) {
1783 REDEBUG("CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1784 request->reply->code = PW_CODE_COA_NAK;
1785 return RLM_MODULE_FAIL;
1790 * RFC 5176, Section 3.2.
1792 REDEBUG("Disconnect-Request MUST NOT contain a Service-Type attribute");
1793 request->reply->code = PW_CODE_DISCONNECT_NAK;
1794 return RLM_MODULE_FAIL;
1797 rcode = process_recv_coa(0, request);
1799 case RLM_MODULE_FAIL:
1800 case RLM_MODULE_INVALID:
1801 case RLM_MODULE_REJECT:
1802 case RLM_MODULE_USERLOCK:
1804 request->reply->code = nak;
1807 case RLM_MODULE_HANDLED:
1810 case RLM_MODULE_NOOP:
1811 case RLM_MODULE_NOTFOUND:
1813 case RLM_MODULE_UPDATED:
1814 if (do_proxy(request)) return RLM_MODULE_OK;
1815 request->reply->code = ack;
1822 else if (request->proxy_reply) {
1824 * Start the reply code with the proxy reply
1827 request->reply->code = request->proxy_reply->code;
1832 * Copy State from the request to the reply.
1833 * See RFC 5176 Section 3.3.
1835 vp = fr_pair_list_copy_by_num(request->reply, request->packet->vps, PW_STATE, 0, TAG_ANY);
1836 if (vp) fr_pair_add(&request->reply->vps, vp);
1839 * We may want to over-ride the reply.
1841 if (request->reply->code) {
1842 rcode = process_send_coa(0, request);
1845 * We need to send CoA-NAK back if Service-Type
1846 * is Authorize-Only. Rely on the user's policy
1847 * to do that. We're not a real NAS, so this
1848 * restriction doesn't (ahem) apply to us.
1850 case RLM_MODULE_FAIL:
1851 case RLM_MODULE_INVALID:
1852 case RLM_MODULE_REJECT:
1853 case RLM_MODULE_USERLOCK:
1856 * Over-ride an ACK with a NAK
1858 request->reply->code = nak;
1861 case RLM_MODULE_HANDLED:
1864 case RLM_MODULE_NOOP:
1865 case RLM_MODULE_NOTFOUND:
1867 case RLM_MODULE_UPDATED:
1869 * Do NOT over-ride a previously set value.
1870 * Otherwise an "ok" here will re-write a
1873 if (request->reply->code == 0) {
1874 request->reply->code = ack;
1880 return RLM_MODULE_OK;
1885 * Check if an incoming request is "ok"
1887 * It takes packets, not requests. It sees if the packet looks
1888 * OK. If so, it does a number of sanity checks on it.
1890 static int coa_socket_recv(rad_listen_t *listener)
1895 RADIUS_PACKET *packet;
1896 RAD_REQUEST_FUNP fun = NULL;
1897 RADCLIENT *client = NULL;
1898 fr_ipaddr_t src_ipaddr;
1901 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1902 if (rcode < 0) return 0;
1904 if (rcode < 20) { /* RADIUS_HDR_LEN */
1905 if (DEBUG_ENABLED) ERROR("Receive - %s", fr_strerror());
1906 FR_STATS_INC(coa, total_malformed_requests);
1910 if ((client = client_listener_find(listener,
1911 &src_ipaddr, src_port)) == NULL) {
1912 rad_recv_discard(listener->fd);
1913 FR_STATS_INC(coa, total_requests);
1914 FR_STATS_INC(coa, total_invalid_requests);
1919 * Some sanity checks, based on the packet code.
1922 case PW_CODE_COA_REQUEST:
1923 FR_STATS_INC(coa, total_requests);
1927 case PW_CODE_DISCONNECT_REQUEST:
1928 FR_STATS_INC(dsc, total_requests);
1933 rad_recv_discard(listener->fd);
1934 FR_STATS_INC(coa, total_unknown_types);
1935 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1936 code, client->shortname, src_port);
1938 } /* switch over packet types */
1940 ctx = talloc_pool(NULL, main_config.talloc_pool_size);
1942 rad_recv_discard(listener->fd);
1943 FR_STATS_INC(coa, total_packets_dropped);
1946 talloc_set_name_const(ctx, "coa_socket_recv_pool");
1949 * Now that we've sanity checked everything, receive the
1952 packet = rad_recv(ctx, listener->fd, client->message_authenticator);
1954 FR_STATS_INC(coa, total_malformed_requests);
1955 if (DEBUG_ENABLED) ERROR("Receive - %s", fr_strerror());
1960 if (!request_receive(ctx, listener, packet, client, fun)) {
1961 FR_STATS_INC(coa, total_packets_dropped);
1973 * Recieve packets from a proxy socket.
1975 static int proxy_socket_recv(rad_listen_t *listener)
1977 RADIUS_PACKET *packet;
1979 listen_socket_t *sock;
1983 packet = rad_recv(NULL, listener->fd, 0);
1985 if (DEBUG_ENABLED) ERROR("Receive - %s", fr_strerror());
1989 switch (packet->code) {
1990 case PW_CODE_ACCESS_ACCEPT:
1991 case PW_CODE_ACCESS_CHALLENGE:
1992 case PW_CODE_ACCESS_REJECT:
1995 #ifdef WITH_ACCOUNTING
1996 case PW_CODE_ACCOUNTING_RESPONSE:
2001 case PW_CODE_DISCONNECT_ACK:
2002 case PW_CODE_DISCONNECT_NAK:
2003 case PW_CODE_COA_ACK:
2004 case PW_CODE_COA_NAK:
2010 * FIXME: Update MIB for packet types?
2012 ERROR("Invalid packet code %d sent to a proxy port "
2013 "from home server %s port %d - ID %d : IGNORED",
2015 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
2016 packet->src_port, packet->id);
2018 listener->stats.total_unknown_types++;
2025 sock = listener->data;
2026 packet->proto = sock->proto;
2029 if (!request_proxy_reply(packet)) {
2031 listener->stats.total_packets_dropped++;
2042 * Recieve packets from a proxy socket.
2044 static int proxy_socket_tcp_recv(rad_listen_t *listener)
2046 RADIUS_PACKET *packet;
2047 listen_socket_t *sock = listener->data;
2050 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return 0;
2052 packet = fr_tcp_recv(listener->fd, 0);
2054 listener->status = RAD_LISTEN_STATUS_EOL;
2055 radius_update_listener(listener);
2060 * FIXME: Client MIB updates?
2062 switch (packet->code) {
2063 case PW_CODE_ACCESS_ACCEPT:
2064 case PW_CODE_ACCESS_CHALLENGE:
2065 case PW_CODE_ACCESS_REJECT:
2068 #ifdef WITH_ACCOUNTING
2069 case PW_CODE_ACCOUNTING_RESPONSE:
2075 * FIXME: Update MIB for packet types?
2077 ERROR("Invalid packet code %d sent to a proxy port "
2078 "from home server %s port %d - ID %d : IGNORED",
2080 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
2081 packet->src_port, packet->id);
2086 packet->src_ipaddr = sock->other_ipaddr;
2087 packet->src_port = sock->other_port;
2088 packet->dst_ipaddr = sock->my_ipaddr;
2089 packet->dst_port = sock->my_port;
2092 * FIXME: Have it return an indication of packets that
2093 * are OK to ignore (dups, too late), versus ones that
2094 * aren't OK to ignore (unknown response, spoofed, etc.)
2096 * Close the socket on bad packets...
2098 if (!request_proxy_reply(packet)) {
2103 sock->opened = sock->last_packet = time(NULL);
2111 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
2113 if (!request->reply->code) return 0;
2115 if (rad_encode(request->reply, request->packet, request->client->secret) < 0) {
2116 RERROR("Failed encoding packet: %s", fr_strerror());
2121 if (rad_sign(request->reply, request->packet, request->client->secret) < 0) {
2122 RERROR("Failed signing packet: %s", fr_strerror());
2131 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
2134 listen_socket_t *sock;
2137 if (rad_verify(request->packet, NULL,
2138 request->client->secret) < 0) {
2143 sock = request->listener->data;
2144 rad_assert(sock != NULL);
2147 * FIXME: Add the rest of the TLS parameters, too? But
2148 * how do we separate EAP-TLS parameters from RADIUS/TLS
2151 if (sock->ssn && sock->ssn->ssl) {
2152 #ifdef PSK_MAX_IDENTITY_LEN
2153 const char *identity = SSL_get_psk_identity(sock->ssn->ssl);
2155 RDEBUG("Retrieved psk identity: %s", identity);
2156 pair_make_request("TLS-PSK-Identity", identity, T_OP_SET);
2162 return rad_decode(request->packet, NULL,
2163 request->client->secret);
2167 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
2169 if (rad_encode(request->proxy, NULL, request->home_server->secret) < 0) {
2170 RERROR("Failed encoding proxied packet: %s", fr_strerror());
2175 if (rad_sign(request->proxy, NULL, request->home_server->secret) < 0) {
2176 RERROR("Failed signing proxied packet: %s", fr_strerror());
2185 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
2188 * rad_verify is run in event.c, received_proxy_response()
2191 return rad_decode(request->proxy_reply, request->proxy,
2192 request->home_server->secret);
2196 #include "command.c"
2199 * Temporarily NOT const!
2201 static fr_protocol_t master_listen[RAD_LISTEN_MAX] = {
2203 { RLM_MODULE_INIT, "status", sizeof(listen_socket_t), NULL,
2204 common_socket_parse, NULL,
2205 stats_socket_recv, auth_socket_send,
2206 common_socket_print, client_socket_encode, client_socket_decode },
2209 * This always gets defined.
2211 { RLM_MODULE_INIT, "status", 0, NULL,
2212 NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
2217 { RLM_MODULE_INIT, "proxy", sizeof(listen_socket_t), NULL,
2218 common_socket_parse, common_socket_free,
2219 proxy_socket_recv, proxy_socket_send,
2220 common_socket_print, proxy_socket_encode, proxy_socket_decode },
2223 /* authentication */
2224 { RLM_MODULE_INIT, "auth", sizeof(listen_socket_t), NULL,
2225 common_socket_parse, common_socket_free,
2226 auth_socket_recv, auth_socket_send,
2227 common_socket_print, client_socket_encode, client_socket_decode },
2229 #ifdef WITH_ACCOUNTING
2231 { RLM_MODULE_INIT, "acct", sizeof(listen_socket_t), NULL,
2232 common_socket_parse, common_socket_free,
2233 acct_socket_recv, acct_socket_send,
2234 common_socket_print, client_socket_encode, client_socket_decode},
2239 { RLM_MODULE_INIT, "detail", sizeof(listen_detail_t), NULL,
2240 detail_parse, detail_free,
2241 detail_recv, detail_send,
2242 detail_print, detail_encode, detail_decode },
2246 /* vlan query protocol */
2247 { 0, "vmps", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
2251 /* dhcp query protocol */
2252 { 0, "dhcp", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
2255 #ifdef WITH_COMMAND_SOCKET
2256 /* TCP command socket */
2257 { RLM_MODULE_INIT, "control", sizeof(fr_command_socket_t), NULL,
2258 command_socket_parse, command_socket_free,
2259 command_domain_accept, command_domain_send,
2260 command_socket_print, command_socket_encode, command_socket_decode },
2264 /* Change of Authorization */
2265 { RLM_MODULE_INIT, "coa", sizeof(listen_socket_t), NULL,
2266 common_socket_parse, NULL,
2267 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
2268 common_socket_print, client_socket_encode, client_socket_decode },
2276 * Binds a listener to a socket.
2278 static int listen_bind(rad_listen_t *this)
2281 struct sockaddr_storage salocal;
2283 listen_socket_t *sock = this->data;
2285 #define proto_for_port "udp"
2286 #define sock_type SOCK_DGRAM
2288 char const *proto_for_port = "udp";
2289 int sock_type = SOCK_DGRAM;
2291 if (sock->proto == IPPROTO_TCP) {
2293 if (this->type == RAD_LISTEN_VQP) {
2294 ERROR("VQP does not support TCP transport");
2299 proto_for_port = "tcp";
2300 sock_type = SOCK_STREAM;
2305 * If the port is zero, then it means the appropriate
2306 * thing from /etc/services.
2308 if (sock->my_port == 0) {
2309 struct servent *svp;
2311 switch (this->type) {
2312 case RAD_LISTEN_AUTH:
2313 svp = getservbyname ("radius", proto_for_port);
2315 sock->my_port = ntohs(svp->s_port);
2317 sock->my_port = PW_AUTH_UDP_PORT;
2321 #ifdef WITH_ACCOUNTING
2322 case RAD_LISTEN_ACCT:
2323 svp = getservbyname ("radacct", proto_for_port);
2325 sock->my_port = ntohs(svp->s_port);
2327 sock->my_port = PW_ACCT_UDP_PORT;
2333 case RAD_LISTEN_PROXY:
2334 /* leave it at zero */
2339 case RAD_LISTEN_VQP:
2340 sock->my_port = 1589;
2344 #ifdef WITH_COMMAND_SOCKET
2345 case RAD_LISTEN_COMMAND:
2346 sock->my_port = PW_RADMIN_PORT;
2351 case RAD_LISTEN_COA:
2352 svp = getservbyname ("radius-dynauth", "udp");
2354 sock->my_port = ntohs(svp->s_port);
2356 sock->my_port = PW_COA_UDP_PORT;
2362 WARN("Internal sanity check failed in binding to socket. Ignoring problem");
2368 * Don't open sockets if we're checking the config.
2376 * Copy fr_socket() here, as we may need to bind to a device.
2378 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
2382 this->print(this, buffer, sizeof(buffer));
2384 ERROR("Failed opening %s: %s", buffer, fr_syserror(errno));
2390 * We don't want child processes inheriting these
2393 rcode = fcntl(this->fd, F_GETFD);
2395 if (fcntl(this->fd, F_SETFD, rcode | FD_CLOEXEC) < 0) {
2397 ERROR("Failed setting close on exec: %s", fr_syserror(errno));
2404 * Bind to a device BEFORE touching IP addresses.
2406 if (sock->interface) {
2407 #ifdef SO_BINDTODEVICE
2410 memset(&ifreq, 0, sizeof(ifreq));
2411 strlcpy(ifreq.ifr_name, sock->interface, sizeof(ifreq.ifr_name));
2414 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
2415 (char *)&ifreq, sizeof(ifreq));
2419 ERROR("Failed binding to interface %s: %s",
2420 sock->interface, fr_syserror(errno));
2422 } /* else it worked. */
2424 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2425 #ifdef HAVE_NET_IF_H
2427 * Odds are that any system supporting "bind to
2428 * device" also supports IPv6, so this next bit
2429 * isn't necessary. But it's here for
2432 * If we're doing IPv6, and the scope hasn't yet
2433 * been defined, set the scope to the scope of
2436 if (sock->my_ipaddr.af == AF_INET6) {
2437 if (sock->my_ipaddr.scope == 0) {
2438 sock->my_ipaddr.scope = if_nametoindex(sock->interface);
2439 if (sock->my_ipaddr.scope == 0) {
2441 ERROR("Failed finding interface %s: %s",
2442 sock->interface, fr_syserror(errno));
2445 } /* else scope was defined: we're OK. */
2450 * IPv4: no link local addresses,
2451 * and no bind to device.
2455 ERROR("Failed binding to interface %s: \"bind to device\" is unsupported", sock->interface);
2462 if (sock->proto == IPPROTO_TCP) {
2465 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2467 ERROR("Failed to reuse address: %s", fr_syserror(errno));
2473 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
2474 else /* UDP sockets get UDPfromto */
2477 #ifdef WITH_UDPFROMTO
2479 * Initialize udpfromto for all sockets.
2481 if (udpfromto_init(this->fd) != 0) {
2482 ERROR("Failed initializing udpfromto: %s",
2483 fr_syserror(errno));
2490 * Set up sockaddr stuff.
2492 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
2497 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2498 if (sock->my_ipaddr.af == AF_INET6) {
2500 * Listening on '::' does NOT get you IPv4 to
2501 * IPv6 mapping. You've got to listen on an IPv4
2502 * address, too. This makes the rest of the server
2503 * design a little simpler.
2507 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2510 if (setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2511 (char *)&on, sizeof(on)) < 0) {
2512 ERROR("Failed setting socket to IPv6 "
2513 "only: %s", fr_syserror(errno));
2519 #endif /* IPV6_V6ONLY */
2521 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2523 if (sock->my_ipaddr.af == AF_INET) {
2524 #if (defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)) || defined(IP_DONTFRAG)
2528 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2531 * Disable PMTU discovery. On Linux, this
2532 * also makes sure that the "don't fragment"
2535 flag = IP_PMTUDISC_DONT;
2536 if (setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2537 &flag, sizeof(flag)) < 0) {
2538 ERROR("Failed disabling PMTU discovery: %s",
2539 fr_syserror(errno));
2546 #if defined(IP_DONTFRAG)
2548 * Ensure that the "don't fragment" flag is zero.
2551 if (setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2552 &flag, sizeof(flag)) < 0) {
2553 ERROR("Failed setting don't fragment flag: %s",
2554 fr_syserror(errno));
2564 if (sock->broadcast) {
2567 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2568 ERROR("Can't set broadcast option: %s",
2569 fr_syserror(errno));
2577 * May be binding to priviledged ports.
2579 if (sock->my_port != 0) {
2581 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2587 this->print(this, buffer, sizeof(buffer));
2588 ERROR("Failed binding to %s: %s\n",
2589 buffer, fr_syserror(errno));
2594 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2595 * kernel instead binds us to a 1.2.3.4. If this
2596 * happens, notice, and remember our real IP.
2599 struct sockaddr_storage src;
2600 socklen_t sizeof_src = sizeof(src);
2602 memset(&src, 0, sizeof_src);
2603 if (getsockname(this->fd, (struct sockaddr *) &src,
2605 ERROR("Failed getting socket name: %s",
2606 fr_syserror(errno));
2610 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2611 &sock->my_ipaddr, &sock->my_port)) {
2612 ERROR("Socket has unsupported address family");
2619 if (sock->proto == IPPROTO_TCP) {
2621 * If there are hard-coded worker threads, OR
2622 * it's a TLS connection, it's blocking.
2624 * Otherwise, they're non-blocking.
2629 && (this->type == RAD_LISTEN_PROXY) && !this->tls
2633 if (fr_nonblock(this->fd) < 0) {
2635 ERROR("Failed setting non-blocking on socket: %s",
2636 fr_syserror(errno));
2642 * Allow a backlog of 8 listeners, but only for incoming interfaces.
2645 if (this->type != RAD_LISTEN_PROXY)
2647 if (listen(this->fd, 8) < 0) {
2649 ERROR("Failed in listen(): %s", fr_syserror(errno));
2656 * Mostly for proxy sockets.
2658 sock->other_ipaddr.af = sock->my_ipaddr.af;
2661 * Don't screw up other people.
2663 #undef proto_for_port
2670 static int _listener_free(rad_listen_t *this)
2673 * Other code may have eaten the FD.
2675 if (this->fd >= 0) close(this->fd);
2677 if (master_listen[this->type].free) {
2678 master_listen[this->type].free(this);
2682 if ((this->type == RAD_LISTEN_AUTH)
2684 || (this->type == RAD_LISTEN_ACCT)
2687 || (this->type == RAD_LISTEN_PROXY)
2689 #ifdef WITH_COMMAND_SOCKET
2690 || ((this->type == RAD_LISTEN_COMMAND) &&
2691 (((fr_command_socket_t *) this->data)->magic != COMMAND_SOCKET_MAGIC))
2694 listen_socket_t *sock = this->data;
2696 rad_assert(talloc_parent(sock) == this);
2697 rad_assert(sock->ev == NULL);
2700 * Remove the child from the parent tree.
2703 rbtree_deletebydata(this->parent->children, this);
2707 * Delete / close all of the children, too!
2709 if (this->children) {
2710 rbtree_walk(this->children, RBTREE_DELETE_ORDER, listener_unlink, this);
2715 * Note that we do NOT free this->tls, as the
2716 * pointer is parented by its CONF_SECTION. It
2717 * may be used by multiple listeners.
2720 rad_assert(!sock->ssn || (talloc_parent(sock->ssn) == sock));
2721 rad_assert(!sock->request || (talloc_parent(sock->request) == sock));
2722 #ifdef HAVE_PTHREAD_H
2723 pthread_mutex_destroy(&(sock->mutex));
2726 #endif /* WITH_TLS */
2728 #endif /* WITH_TCP */
2735 * Allocate & initialize a new listener.
2737 static rad_listen_t *listen_alloc(TALLOC_CTX *ctx, RAD_LISTEN_TYPE type)
2741 this = talloc_zero(ctx, rad_listen_t);
2744 this->recv = master_listen[this->type].recv;
2745 this->send = master_listen[this->type].send;
2746 this->print = master_listen[this->type].print;
2747 this->encode = master_listen[this->type].encode;
2748 this->decode = master_listen[this->type].decode;
2750 talloc_set_destructor(this, _listener_free);
2752 this->data = talloc_zero_array(this, uint8_t, master_listen[this->type].inst_size);
2759 * Externally visible function for creating a new proxy LISTENER.
2761 * Not thread-safe, but all calls to it are protected by the
2762 * proxy mutex in event.c
2764 rad_listen_t *proxy_new_listener(TALLOC_CTX *ctx, home_server_t *home, uint16_t src_port)
2768 listen_socket_t *sock;
2771 if (!home) return NULL;
2773 rad_assert(home->server == NULL); /* we only open real sockets */
2775 if ((home->limit.max_connections > 0) &&
2776 (home->limit.num_connections >= home->limit.max_connections)) {
2777 RATE_LIMIT(INFO("Home server %s has too many open connections (%d)",
2778 home->log_name, home->limit.max_connections));
2783 if (home->last_failed_open == now) {
2784 WARN("Suppressing attempt to open socket to 'down' home server");
2788 this = listen_alloc(ctx, RAD_LISTEN_PROXY);
2791 sock->other_ipaddr = home->ipaddr;
2792 sock->other_port = home->port;
2795 sock->my_ipaddr = home->src_ipaddr;
2796 sock->my_port = src_port;
2797 sock->proto = home->proto;
2800 * For error messages.
2802 this->print(this, buffer, sizeof(buffer));
2804 if (rad_debug_lvl >= 2) {
2805 DEBUG("Opening new proxy socket '%s'", buffer);
2809 sock->opened = sock->last_packet = now;
2811 if (home->proto == IPPROTO_TCP) {
2812 this->recv = proxy_socket_tcp_recv;
2815 * FIXME: connect() is blocking!
2816 * We do this with the proxy mutex locked, which may
2817 * cause large delays!
2819 * http://www.developerweb.net/forum/showthread.php?p=13486
2821 this->fd = fr_socket_client_tcp(&home->src_ipaddr,
2822 &home->ipaddr, home->port, false);
2825 this->fd = fr_socket(&home->src_ipaddr, src_port);
2828 this->print(this, buffer,sizeof(buffer));
2829 ERROR("Failed opening proxy socket '%s' : %s",
2830 buffer, fr_strerror());
2831 home->last_failed_open = now;
2839 if ((home->proto == IPPROTO_TCP) && home->tls) {
2840 DEBUG("Trying SSL to port %d\n", home->port);
2841 sock->ssn = tls_new_client_session(sock, home->tls, this->fd);
2843 ERROR("Failed starting SSL to '%s'", buffer);
2844 home->last_failed_open = now;
2849 this->recv = proxy_tls_recv;
2850 this->send = proxy_tls_send;
2855 * Figure out which port we were bound to.
2857 if (sock->my_port == 0) {
2858 struct sockaddr_storage src;
2859 socklen_t sizeof_src = sizeof(src);
2861 memset(&src, 0, sizeof_src);
2862 if (getsockname(this->fd, (struct sockaddr *) &src,
2864 ERROR("Failed getting socket name for '%s': %s",
2865 buffer, fr_syserror(errno));
2866 home->last_failed_open = now;
2871 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2872 &sock->my_ipaddr, &sock->my_port)) {
2873 ERROR("Socket has unsupported address family for '%s'", buffer);
2874 home->last_failed_open = now;
2880 home->limit.num_connections++;
2886 static const FR_NAME_NUMBER listen_compare[] = {
2888 { "status", RAD_LISTEN_NONE },
2890 { "auth", RAD_LISTEN_AUTH },
2891 #ifdef WITH_ACCOUNTING
2892 { "acct", RAD_LISTEN_ACCT },
2893 { "auth+acct", RAD_LISTEN_AUTH },
2896 { "detail", RAD_LISTEN_DETAIL },
2899 { "proxy", RAD_LISTEN_PROXY },
2902 { "vmps", RAD_LISTEN_VQP },
2905 { "dhcp", RAD_LISTEN_DHCP },
2907 #ifdef WITH_COMMAND_SOCKET
2908 { "control", RAD_LISTEN_COMMAND },
2911 { "coa", RAD_LISTEN_COA },
2916 static int _free_proto_handle(lt_dlhandle *handle)
2922 static rad_listen_t *listen_parse(CONF_SECTION *cs, char const *server)
2925 char const *listen_type;
2930 CONF_SECTION *server_cs;
2933 cp = cf_pair_find(cs, "type");
2936 "No type specified in listen section");
2940 value = cf_pair_value(cp);
2943 "Type cannot be empty");
2947 snprintf(buffer, sizeof(buffer), "proto_%s", value);
2948 handle = lt_dlopenext(buffer);
2950 fr_protocol_t *proto;
2951 lt_dlhandle *marker;
2953 proto = dlsym(handle, buffer);
2956 "Failed linking to protocol %s : %s\n",
2962 type = fr_str2int(listen_compare, value, -1);
2963 rad_assert(type >= 0); /* shouldn't be able to compile an invalid type */
2965 memcpy(&master_listen[type], proto, sizeof(*proto));
2968 * Ensure handle gets closed if config section gets freed
2970 marker = talloc(cs, lt_dlhandle);
2972 talloc_set_destructor(marker, _free_proto_handle);
2974 if (master_listen[type].magic != RLM_MODULE_INIT) {
2975 ERROR("Failed to load protocol '%s', it has the wrong version.",
2976 master_listen[type].name);
2981 cf_log_info(cs, "listen {");
2984 rcode = cf_item_parse(cs, "type", FR_ITEM_POINTER(PW_TYPE_STRING, &listen_type), "");
2985 if (rcode < 0) return NULL;
2988 "No type specified in listen section");
2992 type = fr_str2int(listen_compare, listen_type, -1);
2995 "Invalid type \"%s\" in listen section.",
3001 * DHCP and VMPS *must* be loaded dynamically.
3003 if (master_listen[type].magic != RLM_MODULE_INIT) {
3004 ERROR("Cannot load protocol '%s', as the required library does not exist",
3005 master_listen[type].name);
3010 * Allow listen sections in the default config to
3011 * refer to a server.
3014 rcode = cf_item_parse(cs, "virtual_server", FR_ITEM_POINTER(PW_TYPE_STRING, &server), NULL);
3015 if (rcode < 0) return NULL;
3020 * We were passed a virtual server, so the caller is
3021 * defining a proxy listener inside of a virtual server.
3022 * This isn't allowed right now.
3024 else if (type == RAD_LISTEN_PROXY) {
3025 ERROR("Error: listen type \"proxy\" Cannot appear in a virtual server section");
3031 * Set up cross-type data.
3033 this = listen_alloc(cs, type);
3034 this->server = server;
3039 * Special-case '+' for "auth+acct".
3041 if (strchr(listen_type, '+') != NULL) {
3047 * Call per-type parser.
3049 if (master_listen[type].parse(cs, this) < 0) {
3054 server_cs = cf_section_sub_find_name2(main_config.config, "server",
3056 if (!server_cs && this->server) {
3057 cf_log_err_cs(cs, "No such server \"%s\"", this->server);
3062 cf_log_info(cs, "}");
3067 #ifdef HAVE_PTHREAD_H
3069 * A child thread which does NOTHING other than read and process
3072 static void *recv_thread(void *arg)
3074 rad_listen_t *this = arg;
3087 * Generate a list of listeners. Takes an input list of
3088 * listeners, too, so we don't close sockets with waiting packets.
3090 int listen_init(CONF_SECTION *config, rad_listen_t **head, bool spawn_flag)
3092 bool override = false;
3093 CONF_SECTION *cs = NULL;
3094 rad_listen_t **last;
3096 fr_ipaddr_t server_ipaddr;
3097 uint16_t auth_port = 0;
3099 bool defined_proxy = false;
3103 * We shouldn't be called with a pre-existing list.
3105 rad_assert(head && (*head == NULL));
3107 memset(&server_ipaddr, 0, sizeof(server_ipaddr));
3110 server_ipaddr.af = AF_UNSPEC;
3113 * If the port is specified on the command-line,
3114 * it over-rides the configuration file.
3116 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
3118 if (main_config.port > 0) {
3119 auth_port = main_config.port;
3122 * -p X but no -i Y on the command-line.
3124 if (main_config.myip.af == AF_UNSPEC) {
3125 ERROR("The command-line says \"-p %d\", but there is no associated IP address to use",
3132 * If the IP address was configured on the command-line,
3133 * use that as the "bind_address"
3135 if (main_config.myip.af != AF_UNSPEC) {
3136 listen_socket_t *sock;
3138 memcpy(&server_ipaddr, &main_config.myip,
3139 sizeof(server_ipaddr));
3143 if (strcmp(progname, "vmpsd") == 0) {
3144 this = listen_alloc(config, RAD_LISTEN_VQP);
3145 if (!auth_port) auth_port = 1589;
3148 this = listen_alloc(config, RAD_LISTEN_AUTH);
3152 sock->my_ipaddr = server_ipaddr;
3153 sock->my_port = auth_port;
3155 sock->clients = client_list_parse_section(config, false);
3156 if (!sock->clients) {
3157 cf_log_err_cs(config,
3158 "Failed to find any clients for this listen section");
3163 if (listen_bind(this) < 0) {
3165 ERROR("There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
3169 auth_port = sock->my_port; /* may have been updated in listen_bind */
3171 cs = cf_section_sub_find_name2(config, "server",
3173 if (cs) this->server = main_config.name;
3177 last = &(this->next);
3183 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
3186 #ifdef WITH_ACCOUNTING
3188 * Open Accounting Socket.
3190 * If we haven't already gotten acct_port from
3191 * /etc/services, then make it auth_port + 1.
3193 this = listen_alloc(config, RAD_LISTEN_ACCT);
3197 * Create the accounting socket.
3199 * The accounting port is always the
3200 * authentication port + 1
3202 sock->my_ipaddr = server_ipaddr;
3203 sock->my_port = auth_port + 1;
3205 sock->clients = client_list_parse_section(config, false);
3206 if (!sock->clients) {
3207 cf_log_err_cs(config,
3208 "Failed to find any clients for this listen section");
3212 if (listen_bind(this) < 0) {
3215 ERROR("There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
3220 cs = cf_section_sub_find_name2(config, "server",
3222 if (cs) this->server = main_config.name;
3226 last = &(this->next);
3231 * They specified an IP on the command-line, ignore
3232 * all listen sections except the one in '-n'.
3234 if (main_config.myip.af != AF_UNSPEC) {
3235 CONF_SECTION *subcs;
3236 char const *name2 = cf_section_name2(cs);
3238 cs = cf_section_sub_find_name2(config, "server",
3240 if (!cs) goto add_sockets;
3243 * Should really abstract this code...
3245 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
3247 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
3248 this = listen_parse(subcs, name2);
3255 last = &(this->next);
3256 } /* loop over "listen" directives in server <foo> */
3262 * Walk through the "listen" sections, if they exist.
3264 for (cs = cf_subsection_find_next(config, NULL, "listen");
3266 cs = cf_subsection_find_next(config, cs, "listen")) {
3267 this = listen_parse(cs, NULL);
3274 last = &(this->next);
3278 * Check virtual servers for "listen" sections, too.
3280 * FIXME: Move to virtual server init?
3282 for (cs = cf_subsection_find_next(config, NULL, "server");
3284 cs = cf_subsection_find_next(config, cs, "server")) {
3285 CONF_SECTION *subcs;
3286 char const *name2 = cf_section_name2(cs);
3288 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
3290 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
3291 this = listen_parse(subcs, name2);
3298 last = &(this->next);
3299 } /* loop over "listen" directives in virtual servers */
3300 } /* loop over virtual servers */
3304 * No sockets to receive packets, this is an error.
3305 * proxying is pointless.
3308 ERROR("The server is not configured to listen on any ports. Cannot start");
3313 * Print out which sockets we're listening on, and
3314 * add them to the event list.
3316 for (this = *head; this != NULL; this = this->next) {
3318 if (this->type == RAD_LISTEN_PROXY) {
3319 defined_proxy = true;
3325 if (!check_config && !spawn_flag && this->tls) {
3326 cf_log_err_cs(this->cs, "Threading must be enabled for TLS sockets to function properly");
3327 cf_log_err_cs(this->cs, "You probably need to do '%s -fxx -l stdout' for debugging",
3332 if (!check_config) {
3333 if (this->workers && !spawn_flag) {
3334 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
3338 if (this->workers) {
3339 #ifdef HAVE_PTHREAD_H
3344 this->print(this, buffer, sizeof(buffer));
3346 for (i = 0; i < this->workers; i++) {
3350 * FIXME: create detached?
3352 rcode = pthread_create(&id, 0, recv_thread, this);
3354 ERROR("Thread create failed: %s",
3355 fr_syserror(rcode));
3359 DEBUG("Thread %d for %s\n", i, buffer);
3362 WARN("Setting 'workers' requires 'synchronous'. Disabling 'workers'");
3367 radius_update_listener(this);
3374 if (!home_servers_udp) defined_proxy = true;
3378 * If we're proxying requests, open the proxy FD.
3379 * Otherwise, don't do anything.
3382 if ((main_config.proxy_requests == true) &&
3384 (*head != NULL) && !defined_proxy) {
3388 memset(&home, 0, sizeof(home));
3391 * Open a default UDP port
3393 home.proto = IPPROTO_UDP;
3394 home.src_ipaddr = server_ipaddr;
3398 * Address is still unspecified, use IPv4.
3400 if (home.src_ipaddr.af == AF_UNSPEC) {
3401 home.src_ipaddr.af = AF_INET;
3402 /* everything else is already set to zero */
3405 home.ipaddr.af = home.src_ipaddr.af;
3406 /* everything else is already set to zero */
3409 * It's OK to allocate a UDP listener from the
3410 * main config. The listener will never be
3411 * deleted until the server stops and the config
3414 this = proxy_new_listener(config, &home, port);
3420 radius_update_listener(this);
3425 * Haven't defined any sockets. Die.
3427 if (!*head) return -1;
3429 xlat_register("listen", xlat_listen, NULL, NULL);
3435 * Free a linked list of listeners;
3437 void listen_free(rad_listen_t **head)
3441 if (!head || !*head) return;
3445 rad_listen_t *next = this->next;
3454 RADCLIENT_LIST *listener_find_client_list(fr_ipaddr_t const *ipaddr, uint16_t port, int proto)
3458 for (this = main_config.listen; this != NULL; this = this->next) {
3459 listen_socket_t *sock;
3461 if ((this->type != RAD_LISTEN_AUTH)
3462 #ifdef WITH_ACCOUNTING
3463 && (this->type != RAD_LISTEN_ACCT)
3466 && (this->type != RAD_LISTEN_COA)
3472 if (sock->my_port != port) continue;
3473 if (sock->proto != proto) continue;
3474 if (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) != 0) continue;
3476 return sock->clients;
3483 rad_listen_t *listener_find_byipaddr(fr_ipaddr_t const *ipaddr, uint16_t port, int proto)
3487 for (this = main_config.listen; this != NULL; this = this->next) {
3488 listen_socket_t *sock;
3492 if (sock->my_port != port) continue;
3493 if (sock->proto != proto) continue;
3494 if (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) != 0) continue;
3500 * Failed to find a specific one. Find INADDR_ANY
3502 for (this = main_config.listen; this != NULL; this = this->next) {
3503 listen_socket_t *sock;
3507 if (sock->my_port != port) continue;
3508 if (sock->proto != proto) continue;
3509 if (!fr_inaddr_any(&sock->my_ipaddr)) continue;