2 * listen.c Handle socket stuff
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2005,2006 The FreeRADIUS server project
21 * Copyright 2005 Alan DeKok <aland@ox.org>
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/modules.h>
28 #include <freeradius-devel/rad_assert.h>
29 #include <freeradius-devel/process.h>
30 #include <freeradius-devel/protocol.h>
32 #include <freeradius-devel/detail.h>
35 #include <freeradius-devel/udpfromto.h>
38 #ifdef HAVE_SYS_RESOURCE_H
39 #include <sys/resource.h>
51 #ifdef DEBUG_PRINT_PACKET
52 static void print_packet(RADIUS_PACKET *packet)
54 char src[256], dst[256];
56 ip_ntoh(&packet->src_ipaddr, src, sizeof(src));
57 ip_ntoh(&packet->dst_ipaddr, dst, sizeof(dst));
59 fprintf(stderr, "ID %d: %s %d -> %s %d\n", packet->id,
60 src, packet->src_port, dst, packet->dst_port);
66 static rad_listen_t *listen_alloc(TALLOC_CTX *ctx, RAD_LISTEN_TYPE type);
68 #ifdef WITH_COMMAND_SOCKET
69 static int command_tcp_recv(rad_listen_t *listener);
70 static int command_tcp_send(rad_listen_t *listener, REQUEST *request);
71 static int command_write_magic(int newfd, listen_socket_t *sock);
74 static fr_protocol_t master_listen[RAD_LISTEN_MAX];
77 * Xlat for %{listen:foo}
79 static size_t xlat_listen(UNUSED void *instance, REQUEST *request,
80 char const *fmt, char *out,
83 char const *value = NULL;
86 if (!fmt || !out || (outlen < 1)) return 0;
88 if (!request || !request->listener) {
93 cp = cf_pair_find(request->listener->cs, fmt);
94 if (!cp || !(value = cf_pair_value(cp))) {
99 strlcpy(out, value, outlen);
105 * Find a per-socket client.
107 RADCLIENT *client_listener_find(rad_listen_t *listener,
108 fr_ipaddr_t const *ipaddr, int src_port)
110 #ifdef WITH_DYNAMIC_CLIENTS
117 RADCLIENT_LIST *clients;
118 listen_socket_t *sock;
120 rad_assert(listener != NULL);
121 rad_assert(ipaddr != NULL);
123 sock = listener->data;
124 clients = sock->clients;
127 * This HAS to have been initialized previously.
129 rad_assert(clients != NULL);
131 client = client_find(clients, ipaddr,sock->proto);
133 char name[256], buffer[128];
135 #ifdef WITH_DYNAMIC_CLIENTS
136 unknown: /* used only for dynamic clients */
140 * DoS attack quenching, but only in daemon mode.
141 * If they're running in debug mode, show them
144 if (debug_flag == 0) {
145 static time_t last_printed = 0;
148 if (last_printed == now) return NULL;
153 listener->print(listener, name, sizeof(name));
155 ERROR("Ignoring request to %s from unknown client %s port %d"
159 , name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
160 buffer, sizeof(buffer)), src_port
162 , (sock->proto == IPPROTO_UDP) ? "udp" : "tcp"
168 #ifndef WITH_DYNAMIC_CLIENTS
169 return client; /* return the found client. */
173 * No server defined, and it's not dynamic. Return it.
175 if (!client->client_server && !client->dynamic) return client;
180 * It's a dynamically generated client, check it.
182 if (client->dynamic && (src_port != 0)) {
184 * Lives forever. Return it.
186 if (client->lifetime == 0) return client;
189 * Rate-limit the deletion of known clients.
190 * This makes them last a little longer, but
191 * prevents the server from melting down if (say)
192 * 10k clients all expire at once.
194 if (now == client->last_new_client) return client;
197 * It's not dead yet. Return it.
199 if ((client->created + client->lifetime) > now) return client;
202 * This really puts them onto a queue for later
205 client_delete(clients, client);
208 * Go find the enclosing network again.
210 client = client_find(clients, ipaddr, sock->proto);
215 if (!client) goto unknown;
216 if (!client->client_server) goto unknown;
219 * At this point, 'client' is the enclosing
220 * network that configures where dynamic clients
223 rad_assert(client->dynamic == 0);
225 } else if (!client->dynamic && client->rate_limit) {
227 * The IP is unknown, so we've found an enclosing
228 * network. Enable DoS protection. We only
229 * allow one new client per second. Known
230 * clients aren't subject to this restriction.
232 if (now == client->last_new_client) goto unknown;
235 client->last_new_client = now;
237 request = request_alloc();
238 if (!request) goto unknown;
240 request->listener = listener;
241 request->client = client;
242 request->packet = rad_recv(listener->fd, 0x02); /* MSG_PEEK */
243 if (!request->packet) { /* badly formed, etc */
244 request_free(&request);
247 request->reply = rad_alloc_reply(request, request->packet);
248 if (!request->reply) {
249 request_free(&request);
252 gettimeofday(&request->packet->timestamp, NULL);
254 request->priority = listener->type;
255 request->server = client->client_server;
256 request->root = &mainconfig;
259 * Run a fake request through the given virtual server.
260 * Look for FreeRADIUS-Client-IP-Address
261 * FreeRADIUS-Client-Secret
264 * and create the RADCLIENT structure from that.
266 DEBUG("server %s {", request->server);
268 rcode = process_authorize(0, request);
270 DEBUG("} # server %s", request->server);
272 if (rcode != RLM_MODULE_OK) {
273 request_free(&request);
278 * If the client was updated by rlm_dynamic_clients,
279 * don't create the client from attribute-value pairs.
281 if (request->client == client) {
282 created = client_create(clients, request);
284 created = request->client;
287 * This frees the client if it isn't valid.
289 if (!client_validate(clients, client, created)) goto unknown;
292 request->server = client->server;
293 exec_trigger(request, NULL, "server.client.add", false);
295 request_free(&request);
297 if (!created) goto unknown;
303 static int listen_bind(rad_listen_t *this);
307 * Process and reply to a server-status request.
308 * Like rad_authenticate and rad_accounting this should
309 * live in it's own file but it's so small we don't bother.
311 int rad_status_server(REQUEST *request)
313 int rcode = RLM_MODULE_OK;
316 switch (request->listener->type) {
318 case RAD_LISTEN_NONE:
320 case RAD_LISTEN_AUTH:
321 dval = dict_valbyname(PW_AUTZ_TYPE, 0, "Status-Server");
323 rcode = process_authorize(dval->value, request);
325 rcode = RLM_MODULE_OK;
330 case RLM_MODULE_UPDATED:
331 request->reply->code = PW_AUTHENTICATION_ACK;
334 case RLM_MODULE_FAIL:
335 case RLM_MODULE_HANDLED:
336 request->reply->code = 0; /* don't reply */
340 case RLM_MODULE_REJECT:
341 request->reply->code = PW_AUTHENTICATION_REJECT;
346 #ifdef WITH_ACCOUNTING
347 case RAD_LISTEN_ACCT:
348 dval = dict_valbyname(PW_ACCT_TYPE, 0, "Status-Server");
350 rcode = process_accounting(dval->value, request);
352 rcode = RLM_MODULE_OK;
357 case RLM_MODULE_UPDATED:
358 request->reply->code = PW_ACCOUNTING_RESPONSE;
362 request->reply->code = 0; /* don't reply */
370 * This is a vendor extension. Suggested by Glen
371 * Zorn in IETF 72, and rejected by the rest of
372 * the WG. We like it, so it goes in here.
375 dval = dict_valbyname(PW_RECV_COA_TYPE, 0, "Status-Server");
377 rcode = process_recv_coa(dval->value, request);
379 rcode = RLM_MODULE_OK;
384 case RLM_MODULE_UPDATED:
385 request->reply->code = PW_COA_ACK;
389 request->reply->code = 0; /* don't reply */
401 * Full statistics are available only on a statistics
404 if (request->listener->type == RAD_LISTEN_NONE) {
405 request_stats_reply(request);
413 static int dual_tcp_recv(rad_listen_t *listener)
416 RADIUS_PACKET *packet;
417 RAD_REQUEST_FUNP fun = NULL;
418 listen_socket_t *sock = listener->data;
419 RADCLIENT *client = sock->client;
422 * Allocate a packet for partial reads.
425 sock->packet = rad_alloc(NULL, 0);
426 if (!sock->packet) return 0;
428 sock->packet->sockfd = listener->fd;
429 sock->packet->src_ipaddr = sock->other_ipaddr;
430 sock->packet->src_port = sock->other_port;
431 sock->packet->dst_ipaddr = sock->my_ipaddr;
432 sock->packet->dst_port = sock->my_port;
436 * Grab the packet currently being processed.
438 packet = sock->packet;
440 rcode = fr_tcp_read_packet(packet, 0);
443 * Still only a partial packet. Put it back, and return,
444 * so that we'll read more data when it's ready.
450 if (rcode == -1) { /* error reading packet */
453 ERROR("Invalid packet from %s port %d: closing socket",
454 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
458 if (rcode < 0) { /* error or connection reset */
459 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
462 * Decrement the number of connections.
464 if (sock->parent->limit.num_connections > 0) {
465 sock->parent->limit.num_connections--;
467 if (sock->client->limit.num_connections > 0) {
468 sock->client->limit.num_connections--;
472 * Tell the event handler that an FD has disappeared.
474 DEBUG("Client has closed connection");
475 event_new_fd(listener);
478 * Do NOT free the listener here. It's in use by
479 * a request, and will need to hang around until
480 * all of the requests are done.
482 * It is instead free'd in remove_from_request_hash()
488 * Some sanity checks, based on the packet code.
490 switch(packet->code) {
491 case PW_AUTHENTICATION_REQUEST:
492 if (listener->type != RAD_LISTEN_AUTH) goto bad_packet;
493 FR_STATS_INC(auth, total_requests);
494 fun = rad_authenticate;
497 #ifdef WITH_ACCOUNTING
498 case PW_ACCOUNTING_REQUEST:
499 if (listener->type != RAD_LISTEN_ACCT) goto bad_packet;
500 FR_STATS_INC(acct, total_requests);
501 fun = rad_accounting;
505 case PW_STATUS_SERVER:
506 if (!mainconfig.status_server) {
507 FR_STATS_INC(auth, total_unknown_types);
508 WDEBUG("Ignoring Status-Server request due to security configuration");
509 rad_free(&sock->packet);
512 fun = rad_status_server;
517 FR_STATS_INC(auth, total_unknown_types);
519 DEBUG("Invalid packet code %d sent from client %s port %d : IGNORED",
520 packet->code, client->shortname, packet->src_port);
521 rad_free(&sock->packet);
523 } /* switch over packet types */
525 if (!request_receive(listener, packet, client, fun)) {
526 FR_STATS_INC(auth, total_packets_dropped);
527 rad_free(&sock->packet);
531 sock->packet = NULL; /* we have no need for more partial reads */
535 static int dual_tcp_accept(rad_listen_t *listener)
540 struct sockaddr_storage src;
541 listen_socket_t *sock;
542 fr_ipaddr_t src_ipaddr;
543 RADCLIENT *client = NULL;
547 DEBUG2(" ... new connection request on TCP socket.");
549 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
552 * Non-blocking sockets must handle this.
555 if (errno == EWOULDBLOCK) {
560 DEBUG2(" ... failed to accept connection.");
564 if (!fr_sockaddr2ipaddr(&src, salen, &src_ipaddr, &src_port)) {
566 DEBUG2(" ... unknown address family.");
571 * Enforce client IP address checks on accept, not on
574 if ((client = client_listener_find(listener,
575 &src_ipaddr, src_port)) == NULL) {
577 FR_STATS_INC(auth, total_invalid_requests);
582 * Enforce max_connections on client && listen section.
584 if ((client->limit.max_connections != 0) &&
585 (client->limit.max_connections == client->limit.num_connections)) {
587 * FIXME: Print client IP/port, and server IP/port.
589 INFO("Ignoring new connection due to client max_connections (%d)", client->limit.max_connections);
594 sock = listener->data;
595 if ((sock->limit.max_connections != 0) &&
596 (sock->limit.max_connections == sock->limit.num_connections)) {
598 * FIXME: Print client IP/port, and server IP/port.
600 INFO("Ignoring new connection due to socket max_connections");
604 client->limit.num_connections++;
605 sock->limit.num_connections++;
608 * Add the new listener.
610 this = listen_alloc(listener, listener->type);
611 if (!this) return -1;
614 * Copy everything, including the pointer to the socket
618 memcpy(this->data, listener->data, sizeof(*sock));
619 memcpy(this, listener, sizeof(*this));
621 this->data = sock; /* fix it back */
623 sock->parent = listener->data;
624 sock->other_ipaddr = src_ipaddr;
625 sock->other_port = src_port;
626 sock->client = client;
627 sock->opened = sock->last_packet = time(NULL);
630 * Set the limits. The defaults are the parent limits.
631 * Client limits on max_connections are enforced dynamically.
632 * Set the MINIMUM of client/socket idle timeout or lifetime.
634 memcpy(&sock->limit, &sock->parent->limit, sizeof(sock->limit));
636 if (client->limit.idle_timeout &&
637 ((sock->limit.idle_timeout == 0) ||
638 (client->limit.idle_timeout < sock->limit.idle_timeout))) {
639 sock->limit.idle_timeout = client->limit.idle_timeout;
642 if (client->limit.lifetime &&
643 ((sock->limit.lifetime == 0) ||
644 (client->limit.lifetime < sock->limit.lifetime))) {
645 sock->limit.lifetime = client->limit.lifetime;
649 this->status = RAD_LISTEN_STATUS_INIT;
652 #ifdef WITH_COMMAND_SOCKET
653 if (this->type == RAD_LISTEN_COMMAND) {
654 this->recv = command_tcp_recv;
655 this->send = command_tcp_send;
656 command_write_magic(this->fd, sock);
661 this->recv = dual_tcp_recv;
665 this->recv = dual_tls_recv;
666 this->send = dual_tls_send;
672 * FIXME: set O_NONBLOCK on the accept'd fd.
673 * See djb's portability rants for details.
677 * Tell the event loop that we have a new FD.
678 * This can be called from a child thread...
688 * This function is stupid and complicated.
690 int common_socket_print(rad_listen_t const *this, char *buffer, size_t bufsize)
693 listen_socket_t *sock = this->data;
694 char const *name = master_listen[this->type].name;
696 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
697 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
701 if (sock->interface) {
702 ADDSTRING(" interface ");
703 ADDSTRING(sock->interface);
707 if (this->recv == dual_tcp_accept) {
708 ADDSTRING(" proto tcp");
714 * TCP sockets get printed a little differently, to make
715 * it clear what's going on.
718 ADDSTRING(" from client (");
719 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
723 snprintf(buffer, bufsize, "%d", sock->other_port);
727 if ((sock->my_ipaddr.af == AF_INET) &&
728 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
729 strlcpy(buffer, "*", bufsize);
731 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
736 snprintf(buffer, bufsize, "%d", sock->my_port);
740 ADDSTRING(", virtual-server=");
741 ADDSTRING(this->server);
751 * Maybe it's a socket that we opened to a home server.
753 if ((sock->proto == IPPROTO_TCP) &&
754 (this->type == RAD_LISTEN_PROXY)) {
756 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
760 snprintf(buffer, bufsize, "%d", sock->my_port);
762 ADDSTRING(") -> home_server (");
764 if ((sock->other_ipaddr.af == AF_INET) &&
765 (sock->other_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
766 strlcpy(buffer, "*", bufsize);
768 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
773 snprintf(buffer, bufsize, "%d", sock->other_port);
780 #endif /* WITH_PROXY */
781 #endif /* WITH_TCP */
783 ADDSTRING(" address ");
785 if ((sock->my_ipaddr.af == AF_INET) &&
786 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
787 strlcpy(buffer, "*", bufsize);
789 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
794 snprintf(buffer, bufsize, "%d", sock->my_port);
805 ADDSTRING(" as server ");
806 ADDSTRING(this->server);
815 extern int check_config; /* radiusd.c */
818 static CONF_PARSER limit_config[] = {
819 { "max_connections", PW_TYPE_INTEGER,
820 offsetof(listen_socket_t, limit.max_connections), NULL, "16" },
822 { "lifetime", PW_TYPE_INTEGER,
823 offsetof(listen_socket_t, limit.lifetime), NULL, "0" },
825 { "idle_timeout", PW_TYPE_INTEGER,
826 offsetof(listen_socket_t, limit.idle_timeout), NULL, "30" },
828 { NULL, -1, 0, NULL, NULL } /* end the list */
833 * Parse an authentication or accounting socket.
835 int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
838 int listen_port, max_pps;
840 listen_socket_t *sock = this->data;
841 char *section_name = NULL;
842 CONF_SECTION *client_cs, *parentcs;
849 memset(&ipaddr, 0, sizeof(ipaddr));
850 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
851 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
852 &ipaddr.ipaddr.ip4addr, NULL);
853 if (rcode < 0) return -1;
855 if (rcode == 0) { /* successfully parsed IPv4 */
858 } else { /* maybe IPv6? */
859 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
860 &ipaddr.ipaddr.ip6addr, NULL);
861 if (rcode < 0) return -1;
865 "No address specified in listen section");
868 ipaddr.af = AF_INET6;
871 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
873 if (rcode < 0) return -1;
875 if ((listen_port < 0) || (listen_port > 65535)) {
877 "Invalid value for \"port\"");
881 rcode = cf_item_parse(cs, "max_pps", PW_TYPE_INTEGER,
883 if (rcode < 0) return -1;
885 if (max_pps && ((max_pps < 10) || (max_pps > 1000000))) {
887 "Invalid value for \"max_pps\"");
891 sock->proto = IPPROTO_UDP;
893 if (cf_pair_find(cs, "proto")) {
896 "System does not support the TCP protocol. Delete this line from the configuration file.");
904 rcode = cf_item_parse(cs, "proto", PW_TYPE_STRING_PTR,
906 if (rcode < 0) return -1;
908 if (strcmp(proto, "udp") == 0) {
909 sock->proto = IPPROTO_UDP;
911 } else if (strcmp(proto, "tcp") == 0) {
912 sock->proto = IPPROTO_TCP;
915 limit = cf_section_sub_find(cs, "limit");
917 rcode = cf_section_parse(limit, sock,
919 if (rcode < 0) return -1;
921 sock->limit.max_connections = 60;
922 sock->limit.idle_timeout = 30;
923 sock->limit.lifetime = 0;
926 if ((sock->limit.idle_timeout > 0) && (sock->limit.idle_timeout < 5))
927 sock->limit.idle_timeout = 5;
928 if ((sock->limit.lifetime > 0) && (sock->limit.lifetime < 5))
929 sock->limit.lifetime = 5;
930 if ((sock->limit.lifetime > 0) && (sock->limit.idle_timeout > sock->limit.lifetime))
931 sock->limit.idle_timeout = 0;
934 "Unknown proto name \"%s\"", proto);
939 * TCP requires a destination IP for sockets.
940 * UDP doesn't, so it's allowed.
943 if ((this->type == RAD_LISTEN_PROXY) &&
944 (sock->proto != IPPROTO_UDP)) {
946 "Proxy listeners can only listen on proto = udp");
949 #endif /* WITH_PROXY */
952 tls = cf_section_sub_find(cs, "tls");
955 * Don't allow TLS configurations for UDP sockets.
957 if (sock->proto != IPPROTO_TCP) {
959 "TLS transport is not available for UDP sockets.");
965 * FIXME: Make this better.
967 if (listen_port == 0) listen_port = 2083;
969 this->tls = tls_server_conf_parse(tls);
974 #ifdef HAVE_PTRHEAD_H
975 if (pthread_mutex_init(&sock->mutex, NULL) < 0) {
985 * Built without TLS. Disallow it.
987 if (cf_section_sub_find(cs, "tls")) {
989 "TLS transport is not available in this executable.");
992 #endif /* WITH_TLS */
994 #endif /* WITH_TCP */
997 * No "proto" field. Disallow TLS.
999 } else if (cf_section_sub_find(cs, "tls")) {
1001 "TLS transport is not available in this \"listen\" section.");
1005 sock->my_ipaddr = ipaddr;
1006 sock->my_port = listen_port;
1007 sock->max_rate = max_pps;
1011 if (home_server_find(&sock->my_ipaddr, sock->my_port, sock->proto)) {
1014 EDEBUG("We have been asked to listen on %s port %d, which is also listed as a home server. This can create a proxy loop.",
1015 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
1020 return 0; /* don't do anything */
1025 * If we can bind to interfaces, do so,
1028 if (cf_pair_find(cs, "interface")) {
1030 CONF_PAIR *cp = cf_pair_find(cs, "interface");
1032 rad_assert(cp != NULL);
1033 value = cf_pair_value(cp);
1036 "No interface name given");
1039 sock->interface = value;
1044 * If we can do broadcasts..
1046 if (cf_pair_find(cs, "broadcast")) {
1047 #ifndef SO_BROADCAST
1049 "System does not support broadcast sockets. Delete this line from the configuration file.");
1053 CONF_PAIR *cp = cf_pair_find(cs, "broadcast");
1055 if (this->type != RAD_LISTEN_DHCP) {
1057 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file.");
1061 rad_assert(cp != NULL);
1062 value = cf_pair_value(cp);
1065 "No broadcast value given");
1070 * Hack... whatever happened to cf_section_parse?
1072 sock->broadcast = (strcmp(value, "yes") == 0);
1078 * And bind it to the port.
1080 if (listen_bind(this) < 0) {
1083 "Error binding to port for %s port %d",
1084 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
1091 * Proxy sockets don't have clients.
1093 if (this->type == RAD_LISTEN_PROXY) return 0;
1097 * The more specific configurations are preferred to more
1101 parentcs = cf_top_section(cs);
1102 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
1103 §ion_name, NULL);
1104 if (rcode < 0) return -1; /* bad string */
1107 * Explicit list given: use it.
1109 client_cs = cf_section_sub_find_name2(parentcs,
1113 client_cs = cf_section_find(section_name);
1117 "Failed to find clients %s {...}",
1121 } /* else there was no "clients = " entry. */
1124 CONF_SECTION *server_cs;
1126 server_cs = cf_section_sub_find_name2(parentcs,
1130 * Found a "server foo" section. If there are clients
1134 (cf_section_sub_find(server_cs, "client") != NULL)) {
1135 client_cs = server_cs;
1140 * Still nothing. Look for global clients.
1142 if (!client_cs) client_cs = parentcs;
1144 sock->clients = clients_parse_section(client_cs);
1145 if (!sock->clients) {
1147 "Failed to load clients for this listen section");
1152 if (sock->proto == IPPROTO_TCP) {
1154 * Re-write the listener receive function to
1155 * allow us to accept the socket.
1157 this->recv = dual_tcp_accept;
1165 * Send an authentication response packet
1167 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1169 rad_assert(request->listener == listener);
1170 rad_assert(listener->send == auth_socket_send);
1172 #ifdef WITH_UDPFROMTO
1174 * Overwrite the src ip address on the outbound packet
1175 * with the one specified by the client.
1176 * This is useful to work around broken DSR implementations
1177 * and other routing issues.
1179 if (request->client->src_ipaddr.af != AF_UNSPEC) {
1180 request->reply->src_ipaddr = request->client->src_ipaddr;
1184 if (rad_send(request->reply, request->packet,
1185 request->client->secret) < 0) {
1186 RERROR("Failed sending reply: %s",
1195 #ifdef WITH_ACCOUNTING
1197 * Send an accounting response packet (or not)
1199 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1201 rad_assert(request->listener == listener);
1202 rad_assert(listener->send == acct_socket_send);
1205 * Accounting reject's are silently dropped.
1207 * We do it here to avoid polluting the rest of the
1208 * code with this knowledge
1210 if (request->reply->code == 0) return 0;
1212 #ifdef WITH_UDPFROMTO
1214 * Overwrite the src ip address on the outbound packet
1215 * with the one specified by the client.
1216 * This is useful to work around broken DSR implementations
1217 * and other routing issues.
1219 if (request->client->src_ipaddr.af != AF_UNSPEC) {
1220 request->reply->src_ipaddr = request->client->src_ipaddr;
1224 if (rad_send(request->reply, request->packet,
1225 request->client->secret) < 0) {
1226 RERROR("Failed sending reply: %s",
1237 * Send a packet to a home server.
1239 * FIXME: have different code for proxy auth & acct!
1241 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1243 rad_assert(request->proxy_listener == listener);
1244 rad_assert(listener->send == proxy_socket_send);
1246 if (rad_send(request->proxy, NULL,
1247 request->home_server->secret) < 0) {
1248 RERROR("Failed sending proxied request: %s",
1259 * Check if an incoming request is "ok"
1261 * It takes packets, not requests. It sees if the packet looks
1262 * OK. If so, it does a number of sanity checks on it.
1264 static int stats_socket_recv(rad_listen_t *listener)
1268 RADIUS_PACKET *packet;
1269 RADCLIENT *client = NULL;
1270 fr_ipaddr_t src_ipaddr;
1272 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1273 if (rcode < 0) return 0;
1275 FR_STATS_INC(auth, total_requests);
1277 if (rcode < 20) { /* AUTH_HDR_LEN */
1278 FR_STATS_INC(auth, total_malformed_requests);
1282 if ((client = client_listener_find(listener,
1283 &src_ipaddr, src_port)) == NULL) {
1284 rad_recv_discard(listener->fd);
1285 FR_STATS_INC(auth, total_invalid_requests);
1289 FR_STATS_TYPE_INC(client->auth.total_requests);
1292 * We only understand Status-Server on this socket.
1294 if (code != PW_STATUS_SERVER) {
1295 DEBUG("Ignoring packet code %d sent to Status-Server port",
1297 rad_recv_discard(listener->fd);
1298 FR_STATS_INC(auth, total_unknown_types);
1303 * Now that we've sanity checked everything, receive the
1306 packet = rad_recv(listener->fd, 1); /* require message authenticator */
1308 FR_STATS_INC(auth, total_malformed_requests);
1309 DEBUG("%s", fr_strerror());
1313 if (!request_receive(listener, packet, client, rad_status_server)) {
1314 FR_STATS_INC(auth, total_packets_dropped);
1325 * Check if an incoming request is "ok"
1327 * It takes packets, not requests. It sees if the packet looks
1328 * OK. If so, it does a number of sanity checks on it.
1330 static int auth_socket_recv(rad_listen_t *listener)
1334 RADIUS_PACKET *packet;
1335 RAD_REQUEST_FUNP fun = NULL;
1336 RADCLIENT *client = NULL;
1337 fr_ipaddr_t src_ipaddr;
1339 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1340 if (rcode < 0) return 0;
1342 FR_STATS_INC(auth, total_requests);
1344 if (rcode < 20) { /* AUTH_HDR_LEN */
1345 FR_STATS_INC(auth, total_malformed_requests);
1349 if ((client = client_listener_find(listener,
1350 &src_ipaddr, src_port)) == NULL) {
1351 rad_recv_discard(listener->fd);
1352 FR_STATS_INC(auth, total_invalid_requests);
1356 FR_STATS_TYPE_INC(client->auth.total_requests);
1359 * Some sanity checks, based on the packet code.
1362 case PW_AUTHENTICATION_REQUEST:
1363 fun = rad_authenticate;
1366 case PW_STATUS_SERVER:
1367 if (!mainconfig.status_server) {
1368 rad_recv_discard(listener->fd);
1369 FR_STATS_INC(auth, total_unknown_types);
1370 WDEBUG("Ignoring Status-Server request due to security configuration");
1373 fun = rad_status_server;
1377 rad_recv_discard(listener->fd);
1378 FR_STATS_INC(auth,total_unknown_types);
1380 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
1381 code, client->shortname, src_port);
1384 } /* switch over packet types */
1387 * Now that we've sanity checked everything, receive the
1390 packet = rad_recv(listener->fd, client->message_authenticator);
1392 FR_STATS_INC(auth, total_malformed_requests);
1393 DEBUG("%s", fr_strerror());
1397 if (!request_receive(listener, packet, client, fun)) {
1398 FR_STATS_INC(auth, total_packets_dropped);
1407 #ifdef WITH_ACCOUNTING
1409 * Receive packets from an accounting socket
1411 static int acct_socket_recv(rad_listen_t *listener)
1415 RADIUS_PACKET *packet;
1416 RAD_REQUEST_FUNP fun = NULL;
1417 RADCLIENT *client = NULL;
1418 fr_ipaddr_t src_ipaddr;
1420 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1421 if (rcode < 0) return 0;
1423 FR_STATS_INC(acct, total_requests);
1425 if (rcode < 20) { /* AUTH_HDR_LEN */
1426 FR_STATS_INC(acct, total_malformed_requests);
1430 if ((client = client_listener_find(listener,
1431 &src_ipaddr, src_port)) == NULL) {
1432 rad_recv_discard(listener->fd);
1433 FR_STATS_INC(acct, total_invalid_requests);
1437 FR_STATS_TYPE_INC(client->acct.total_requests);
1440 * Some sanity checks, based on the packet code.
1443 case PW_ACCOUNTING_REQUEST:
1444 fun = rad_accounting;
1447 case PW_STATUS_SERVER:
1448 if (!mainconfig.status_server) {
1449 rad_recv_discard(listener->fd);
1450 FR_STATS_INC(acct, total_unknown_types);
1452 WDEBUG("Ignoring Status-Server request due to security configuration");
1455 fun = rad_status_server;
1459 rad_recv_discard(listener->fd);
1460 FR_STATS_INC(acct, total_unknown_types);
1462 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1463 code, client->shortname, src_port);
1465 } /* switch over packet types */
1468 * Now that we've sanity checked everything, receive the
1471 packet = rad_recv(listener->fd, 0);
1473 FR_STATS_INC(acct, total_malformed_requests);
1474 ERROR("%s", fr_strerror());
1479 * There can be no duplicate accounting packets.
1481 if (!request_receive(listener, packet, client, fun)) {
1482 FR_STATS_INC(acct, total_packets_dropped);
1493 static int do_proxy(REQUEST *request)
1497 if (request->in_proxy_hash ||
1498 (request->proxy_reply && (request->proxy_reply->code != 0))) {
1502 vp = pairfind(request->config_items, PW_HOME_SERVER_POOL, 0, TAG_ANY);
1505 if (!home_pool_byname(vp->vp_strvalue, HOME_TYPE_COA)) {
1506 REDEBUG2("Cannot proxy to unknown pool %s",
1515 * Receive a CoA packet.
1517 static int rad_coa_recv(REQUEST *request)
1519 int rcode = RLM_MODULE_OK;
1524 * Get the correct response
1526 switch (request->packet->code) {
1527 case PW_COA_REQUEST:
1532 case PW_DISCONNECT_REQUEST:
1533 ack = PW_DISCONNECT_ACK;
1534 nak = PW_DISCONNECT_NAK;
1537 default: /* shouldn't happen */
1538 return RLM_MODULE_FAIL;
1542 #define WAS_PROXIED (request->proxy)
1544 #define WAS_PROXIED (0)
1549 * RFC 5176 Section 3.3. If we have a CoA-Request
1550 * with Service-Type = Authorize-Only, it MUST
1551 * have a State attribute in it.
1553 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE, 0, TAG_ANY);
1554 if (request->packet->code == PW_COA_REQUEST) {
1555 if (vp && (vp->vp_integer == 17)) {
1556 vp = pairfind(request->packet->vps, PW_STATE, 0, TAG_ANY);
1557 if (!vp || (vp->length == 0)) {
1558 REDEBUG("CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1559 request->reply->code = PW_COA_NAK;
1560 return RLM_MODULE_FAIL;
1565 * RFC 5176, Section 3.2.
1567 REDEBUG("Disconnect-Request MUST NOT contain a Service-Type attribute");
1568 request->reply->code = PW_DISCONNECT_NAK;
1569 return RLM_MODULE_FAIL;
1572 rcode = process_recv_coa(0, request);
1574 case RLM_MODULE_FAIL:
1575 case RLM_MODULE_INVALID:
1576 case RLM_MODULE_REJECT:
1577 case RLM_MODULE_USERLOCK:
1579 request->reply->code = nak;
1582 case RLM_MODULE_HANDLED:
1585 case RLM_MODULE_NOOP:
1586 case RLM_MODULE_NOTFOUND:
1588 case RLM_MODULE_UPDATED:
1589 if (do_proxy(request)) return RLM_MODULE_OK;
1590 request->reply->code = ack;
1593 } else if (request->proxy_reply) {
1595 * Start the reply code with the proxy reply
1598 request->reply->code = request->proxy_reply->code;
1602 * Copy State from the request to the reply.
1603 * See RFC 5176 Section 3.3.
1605 vp = paircopy2(request->reply, request->packet->vps, PW_STATE, 0, TAG_ANY);
1606 if (vp) pairadd(&request->reply->vps, vp);
1609 * We may want to over-ride the reply.
1611 if (request->reply->code) {
1612 rcode = process_send_coa(0, request);
1615 * We need to send CoA-NAK back if Service-Type
1616 * is Authorize-Only. Rely on the user's policy
1617 * to do that. We're not a real NAS, so this
1618 * restriction doesn't (ahem) apply to us.
1620 case RLM_MODULE_FAIL:
1621 case RLM_MODULE_INVALID:
1622 case RLM_MODULE_REJECT:
1623 case RLM_MODULE_USERLOCK:
1626 * Over-ride an ACK with a NAK
1628 request->reply->code = nak;
1631 case RLM_MODULE_HANDLED:
1634 case RLM_MODULE_NOOP:
1635 case RLM_MODULE_NOTFOUND:
1637 case RLM_MODULE_UPDATED:
1639 * Do NOT over-ride a previously set value.
1640 * Otherwise an "ok" here will re-write a
1643 if (request->reply->code == 0) {
1644 request->reply->code = ack;
1650 return RLM_MODULE_OK;
1655 * Check if an incoming request is "ok"
1657 * It takes packets, not requests. It sees if the packet looks
1658 * OK. If so, it does a number of sanity checks on it.
1660 static int coa_socket_recv(rad_listen_t *listener)
1664 RADIUS_PACKET *packet;
1665 RAD_REQUEST_FUNP fun = NULL;
1666 RADCLIENT *client = NULL;
1667 fr_ipaddr_t src_ipaddr;
1669 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1670 if (rcode < 0) return 0;
1672 if (rcode < 20) { /* AUTH_HDR_LEN */
1673 FR_STATS_INC(coa, total_requests);
1674 FR_STATS_INC(coa, total_malformed_requests);
1678 if ((client = client_listener_find(listener,
1679 &src_ipaddr, src_port)) == NULL) {
1680 rad_recv_discard(listener->fd);
1681 FR_STATS_INC(coa, total_requests);
1682 FR_STATS_INC(coa, total_invalid_requests);
1687 * Some sanity checks, based on the packet code.
1690 case PW_COA_REQUEST:
1691 FR_STATS_INC(coa, total_requests);
1695 case PW_DISCONNECT_REQUEST:
1696 FR_STATS_INC(dsc, total_requests);
1701 rad_recv_discard(listener->fd);
1702 FR_STATS_INC(coa, total_unknown_types);
1703 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1704 code, client->shortname, src_port);
1706 } /* switch over packet types */
1709 * Now that we've sanity checked everything, receive the
1712 packet = rad_recv(listener->fd, client->message_authenticator);
1714 FR_STATS_INC(coa, total_malformed_requests);
1715 DEBUG("%s", fr_strerror());
1719 if (!request_receive(listener, packet, client, fun)) {
1720 FR_STATS_INC(coa, total_packets_dropped);
1731 * Recieve packets from a proxy socket.
1733 static int proxy_socket_recv(rad_listen_t *listener)
1735 RADIUS_PACKET *packet;
1738 packet = rad_recv(listener->fd, 0);
1740 ERROR("%s", fr_strerror());
1745 * FIXME: Client MIB updates?
1747 switch(packet->code) {
1748 case PW_AUTHENTICATION_ACK:
1749 case PW_ACCESS_CHALLENGE:
1750 case PW_AUTHENTICATION_REJECT:
1753 #ifdef WITH_ACCOUNTING
1754 case PW_ACCOUNTING_RESPONSE:
1759 case PW_DISCONNECT_ACK:
1760 case PW_DISCONNECT_NAK:
1768 * FIXME: Update MIB for packet types?
1770 ERROR("Invalid packet code %d sent to a proxy port "
1771 "from home server %s port %d - ID %d : IGNORED",
1773 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1774 packet->src_port, packet->id);
1779 if (!request_proxy_reply(packet)) {
1789 * Recieve packets from a proxy socket.
1791 static int proxy_socket_tcp_recv(rad_listen_t *listener)
1793 RADIUS_PACKET *packet;
1794 listen_socket_t *sock = listener->data;
1797 packet = fr_tcp_recv(listener->fd, 0);
1799 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
1800 event_new_fd(listener);
1805 * FIXME: Client MIB updates?
1807 switch(packet->code) {
1808 case PW_AUTHENTICATION_ACK:
1809 case PW_ACCESS_CHALLENGE:
1810 case PW_AUTHENTICATION_REJECT:
1813 #ifdef WITH_ACCOUNTING
1814 case PW_ACCOUNTING_RESPONSE:
1820 * FIXME: Update MIB for packet types?
1822 ERROR("Invalid packet code %d sent to a proxy port "
1823 "from home server %s port %d - ID %d : IGNORED",
1825 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1826 packet->src_port, packet->id);
1831 packet->src_ipaddr = sock->other_ipaddr;
1832 packet->src_port = sock->other_port;
1833 packet->dst_ipaddr = sock->my_ipaddr;
1834 packet->dst_port = sock->my_port;
1837 * FIXME: Have it return an indication of packets that
1838 * are OK to ignore (dups, too late), versus ones that
1839 * aren't OK to ignore (unknown response, spoofed, etc.)
1841 * Close the socket on bad packets...
1843 if (!request_proxy_reply(packet)) {
1848 sock->opened = sock->last_packet = time(NULL);
1856 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1858 if (!request->reply->code) return 0;
1860 if (rad_encode(request->reply, request->packet,
1861 request->client->secret) < 0) {
1862 RERROR("Failed encoding packet: %s",
1867 if (rad_sign(request->reply, request->packet,
1868 request->client->secret) < 0) {
1869 RERROR("Failed signing packet: %s",
1878 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1880 if (rad_verify(request->packet, NULL,
1881 request->client->secret) < 0) {
1885 return rad_decode(request->packet, NULL,
1886 request->client->secret);
1890 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1892 if (rad_encode(request->proxy, NULL, request->home_server->secret) < 0) {
1893 RERROR("Failed encoding proxied packet: %s",
1898 if (rad_sign(request->proxy, NULL, request->home_server->secret) < 0) {
1899 RERROR("Failed signing proxied packet: %s",
1908 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1911 * rad_verify is run in event.c, received_proxy_response()
1914 return rad_decode(request->proxy_reply, request->proxy,
1915 request->home_server->secret);
1919 #include "command.c"
1922 * Temporarily NOT const!
1924 static fr_protocol_t master_listen[RAD_LISTEN_MAX] = {
1926 { RLM_MODULE_INIT, "status", sizeof(listen_socket_t), NULL,
1927 common_socket_parse, NULL,
1928 stats_socket_recv, auth_socket_send,
1929 common_socket_print, client_socket_encode, client_socket_decode },
1932 * This always gets defined.
1934 { RLM_MODULE_INIT, "status", 0, NULL,
1935 NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1940 { RLM_MODULE_INIT, "proxy", sizeof(listen_socket_t), NULL,
1941 common_socket_parse, NULL,
1942 proxy_socket_recv, proxy_socket_send,
1943 common_socket_print, proxy_socket_encode, proxy_socket_decode },
1946 /* authentication */
1947 { RLM_MODULE_INIT, "auth", sizeof(listen_socket_t), NULL,
1948 common_socket_parse, NULL,
1949 auth_socket_recv, auth_socket_send,
1950 common_socket_print, client_socket_encode, client_socket_decode },
1952 #ifdef WITH_ACCOUNTING
1954 { RLM_MODULE_INIT, "acct", sizeof(listen_socket_t), NULL,
1955 common_socket_parse, NULL,
1956 acct_socket_recv, acct_socket_send,
1957 common_socket_print, client_socket_encode, client_socket_decode},
1962 { RLM_MODULE_INIT, "detail", sizeof(listen_detail_t), NULL,
1963 detail_parse, detail_free,
1964 detail_recv, detail_send,
1965 detail_print, detail_encode, detail_decode },
1969 /* vlan query protocol */
1970 { 0, "vmps", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
1974 /* dhcp query protocol */
1975 { 0, "dhcp", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
1978 #ifdef WITH_COMMAND_SOCKET
1979 /* TCP command socket */
1980 { RLM_MODULE_INIT, "control", sizeof(fr_command_socket_t), NULL,
1981 command_socket_parse, command_socket_free,
1982 command_domain_accept, command_domain_send,
1983 command_socket_print, command_socket_encode, command_socket_decode },
1987 /* Change of Authorization */
1988 { RLM_MODULE_INIT, "coa", sizeof(listen_socket_t), NULL,
1989 common_socket_parse, NULL,
1990 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
1991 common_socket_print, client_socket_encode, client_socket_decode },
1999 * Binds a listener to a socket.
2001 static int listen_bind(rad_listen_t *this)
2004 struct sockaddr_storage salocal;
2006 listen_socket_t *sock = this->data;
2008 #define proto_for_port "udp"
2009 #define sock_type SOCK_DGRAM
2011 char const *proto_for_port = "udp";
2012 int sock_type = SOCK_DGRAM;
2014 if (sock->proto == IPPROTO_TCP) {
2016 if (this->type == RAD_LISTEN_VQP) {
2017 ERROR("VQP does not support TCP transport");
2022 proto_for_port = "tcp";
2023 sock_type = SOCK_STREAM;
2028 * If the port is zero, then it means the appropriate
2029 * thing from /etc/services.
2031 if (sock->my_port == 0) {
2032 struct servent *svp;
2034 switch (this->type) {
2035 case RAD_LISTEN_AUTH:
2036 svp = getservbyname ("radius", proto_for_port);
2038 sock->my_port = ntohs(svp->s_port);
2040 sock->my_port = PW_AUTH_UDP_PORT;
2044 #ifdef WITH_ACCOUNTING
2045 case RAD_LISTEN_ACCT:
2046 svp = getservbyname ("radacct", proto_for_port);
2048 sock->my_port = ntohs(svp->s_port);
2050 sock->my_port = PW_ACCT_UDP_PORT;
2056 case RAD_LISTEN_PROXY:
2057 /* leave it at zero */
2062 case RAD_LISTEN_VQP:
2063 sock->my_port = 1589;
2067 #ifdef WITH_COMMAND_SOCKET
2068 case RAD_LISTEN_COMMAND:
2069 sock->my_port = PW_RADMIN_PORT;
2074 case RAD_LISTEN_COA:
2075 svp = getservbyname ("radius-dynauth", "udp");
2077 sock->my_port = ntohs(svp->s_port);
2079 sock->my_port = PW_COA_UDP_PORT;
2085 WDEBUG("Internal sanity check failed in binding to socket. Ignoring problem.");
2091 * Don't open sockets if we're checking the config.
2099 * Copy fr_socket() here, as we may need to bind to a device.
2101 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
2105 this->print(this, buffer, sizeof(buffer));
2107 ERROR("Failed opening %s: %s", buffer, strerror(errno));
2113 * We don't want child processes inheriting these
2116 rcode = fcntl(this->fd, F_GETFD);
2118 if (fcntl(this->fd, F_SETFD, rcode | FD_CLOEXEC) < 0) {
2120 ERROR("Failed setting close on exec: %s", strerror(errno));
2127 * Bind to a device BEFORE touching IP addresses.
2129 if (sock->interface) {
2130 #ifdef SO_BINDTODEVICE
2133 memset(&ifreq, 0, sizeof(ifreq));
2134 strlcpy(ifreq.ifr_name, sock->interface, sizeof(ifreq.ifr_name));
2137 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
2138 (char *)&ifreq, sizeof(ifreq));
2142 ERROR("Failed binding to interface %s: %s",
2143 sock->interface, strerror(errno));
2145 } /* else it worked. */
2147 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2148 #ifdef HAVE_NET_IF_H
2150 * Odds are that any system supporting "bind to
2151 * device" also supports IPv6, so this next bit
2152 * isn't necessary. But it's here for
2155 * If we're doing IPv6, and the scope hasn't yet
2156 * been defined, set the scope to the scope of
2159 if (sock->my_ipaddr.af == AF_INET6) {
2160 if (sock->my_ipaddr.scope == 0) {
2161 sock->my_ipaddr.scope = if_nametoindex(sock->interface);
2162 if (sock->my_ipaddr.scope == 0) {
2164 ERROR("Failed finding interface %s: %s",
2165 sock->interface, strerror(errno));
2168 } /* else scope was defined: we're OK. */
2173 * IPv4: no link local addresses,
2174 * and no bind to device.
2178 ERROR("Failed binding to interface %s: \"bind to device\" is unsupported", sock->interface);
2185 if (sock->proto == IPPROTO_TCP) {
2188 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2190 ERROR("Failed to reuse address: %s", strerror(errno));
2196 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
2197 else /* UDP sockets get UDPfromto */
2200 #ifdef WITH_UDPFROMTO
2202 * Initialize udpfromto for all sockets.
2204 if (udpfromto_init(this->fd) != 0) {
2205 ERROR("Failed initializing udpfromto: %s",
2213 * Set up sockaddr stuff.
2215 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
2220 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2221 if (sock->my_ipaddr.af == AF_INET6) {
2223 * Listening on '::' does NOT get you IPv4 to
2224 * IPv6 mapping. You've got to listen on an IPv4
2225 * address, too. This makes the rest of the server
2226 * design a little simpler.
2230 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2233 if (setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2234 (char *)&on, sizeof(on)) < 0) {
2235 ERROR("Failed setting socket to IPv6 "
2236 "only: %s", strerror(errno));
2242 #endif /* IPV6_V6ONLY */
2244 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2246 if (sock->my_ipaddr.af == AF_INET) {
2249 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2251 * Disable PMTU discovery. On Linux, this
2252 * also makes sure that the "don't fragment"
2255 flag = IP_PMTUDISC_DONT;
2256 if (setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2257 &flag, sizeof(flag)) < 0) {
2258 ERROR("Failed disabling PMTU discovery: %s",
2266 #if defined(IP_DONTFRAG)
2268 * Ensure that the "don't fragment" flag is zero.
2271 if (setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2272 &flag, sizeof(flag)) < 0) {
2273 ERROR("Failed setting don't fragment flag: %s",
2284 if (sock->broadcast) {
2287 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2288 ERROR("Can't set broadcast option: %s",
2297 * May be binding to priviledged ports.
2299 if (sock->my_port != 0) {
2303 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2304 ERROR("Can't set re-use address option: %s",
2311 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2317 this->print(this, buffer, sizeof(buffer));
2318 ERROR("Failed binding to %s: %s\n",
2319 buffer, strerror(errno));
2324 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2325 * kernel instead binds us to a 1.2.3.4. If this
2326 * happens, notice, and remember our real IP.
2329 struct sockaddr_storage src;
2330 socklen_t sizeof_src = sizeof(src);
2332 memset(&src, 0, sizeof_src);
2333 if (getsockname(this->fd, (struct sockaddr *) &src,
2335 ERROR("Failed getting socket name: %s",
2340 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2341 &sock->my_ipaddr, &sock->my_port)) {
2342 ERROR("Socket has unsupported address family");
2349 if (sock->proto == IPPROTO_TCP) {
2350 if (listen(this->fd, 8) < 0) {
2352 ERROR("Failed in listen(): %s", strerror(errno));
2358 if (fr_nonblock(this->fd) < 0) {
2360 ERROR("Failed setting non-blocking on socket: %s",
2366 * Mostly for proxy sockets.
2368 sock->other_ipaddr.af = sock->my_ipaddr.af;
2371 * Don't screw up other people.
2373 #undef proto_for_port
2380 static int listener_free(void *ctx)
2384 this = talloc_get_type_abort(ctx, rad_listen_t);
2387 * Other code may have eaten the FD.
2389 if (this->fd >= 0) close(this->fd);
2391 if (master_listen[this->type].free) {
2392 master_listen[this->type].free(this);
2396 if ((this->type == RAD_LISTEN_AUTH)
2398 || (this->type == RAD_LISTEN_ACCT)
2401 || (this->type == RAD_LISTEN_PROXY)
2403 #ifdef WITH_COMMAND_SOCKET
2404 || ((this->type == RAD_LISTEN_COMMAND) &&
2405 (((fr_command_socket_t *) this->data)->magic != COMMAND_SOCKET_MAGIC))
2408 listen_socket_t *sock = this->data;
2411 if (sock->request) {
2412 pthread_mutex_destroy(&(sock->mutex));
2413 request_free(&sock->request);
2414 sock->packet = NULL;
2416 if (sock->ssn) session_free(sock->ssn);
2417 request_free(&sock->request);
2420 rad_free(&sock->packet);
2422 #endif /* WITH_TCP */
2428 * Allocate & initialize a new listener.
2430 static rad_listen_t *listen_alloc(TALLOC_CTX *ctx, RAD_LISTEN_TYPE type)
2434 this = talloc_zero(ctx, rad_listen_t);
2437 this->recv = master_listen[this->type].recv;
2438 this->send = master_listen[this->type].send;
2439 this->print = master_listen[this->type].print;
2440 this->encode = master_listen[this->type].encode;
2441 this->decode = master_listen[this->type].decode;
2443 talloc_set_destructor((void *) this, listener_free);
2445 this->data = talloc_zero_array(this, uint8_t, master_listen[this->type].inst_size);
2452 * Externally visible function for creating a new proxy LISTENER.
2454 * Not thread-safe, but all calls to it are protected by the
2455 * proxy mutex in event.c
2457 rad_listen_t *proxy_new_listener(home_server *home, int src_port)
2460 listen_socket_t *sock;
2463 if (!home) return NULL;
2465 if ((home->limit.max_connections > 0) &&
2466 (home->limit.num_connections >= home->limit.max_connections)) {
2467 WDEBUG("Home server has too many open connections (%d)",
2468 home->limit.max_connections);
2472 this = listen_alloc(mainconfig.config, RAD_LISTEN_PROXY);
2475 sock->other_ipaddr = home->ipaddr;
2476 sock->other_port = home->port;
2479 sock->my_ipaddr = home->src_ipaddr;
2480 sock->my_port = src_port;
2481 sock->proto = home->proto;
2483 if (debug_flag >= 2) {
2484 this->print(this, buffer, sizeof(buffer));
2485 DEBUG("Opening new %s", buffer);
2489 sock->opened = sock->last_packet = time(NULL);
2491 if (home->proto == IPPROTO_TCP) {
2492 this->recv = proxy_socket_tcp_recv;
2495 * FIXME: connect() is blocking!
2496 * We do this with the proxy mutex locked, which may
2497 * cause large delays!
2499 * http://www.developerweb.net/forum/showthread.php?p=13486
2501 this->fd = fr_tcp_client_socket(&home->src_ipaddr,
2502 &home->ipaddr, home->port);
2505 DEBUG("Trying SSL to port %d\n", home->port);
2506 sock->ssn = tls_new_client_session(home->tls, this->fd);
2512 this->recv = proxy_tls_recv;
2513 this->send = proxy_tls_send;
2518 this->fd = fr_socket(&home->src_ipaddr, src_port);
2521 this->print(this, buffer,sizeof(buffer));
2522 DEBUG("Failed opening client socket ::%s:: : %s",
2523 buffer, fr_strerror());
2529 * Figure out which port we were bound to.
2531 if (sock->my_port == 0) {
2532 struct sockaddr_storage src;
2533 socklen_t sizeof_src = sizeof(src);
2535 memset(&src, 0, sizeof_src);
2536 if (getsockname(this->fd, (struct sockaddr *) &src,
2538 ERROR("Failed getting socket name: %s",
2544 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2545 &sock->my_ipaddr, &sock->my_port)) {
2546 ERROR("Socket has unsupported address family");
2557 static const FR_NAME_NUMBER listen_compare[] = {
2559 { "status", RAD_LISTEN_NONE },
2561 { "auth", RAD_LISTEN_AUTH },
2562 #ifdef WITH_ACCOUNTING
2563 { "acct", RAD_LISTEN_ACCT },
2566 { "detail", RAD_LISTEN_DETAIL },
2569 { "proxy", RAD_LISTEN_PROXY },
2572 { "vmps", RAD_LISTEN_VQP },
2575 { "dhcp", RAD_LISTEN_DHCP },
2577 #ifdef WITH_COMMAND_SOCKET
2578 { "control", RAD_LISTEN_COMMAND },
2581 { "coa", RAD_LISTEN_COA },
2587 static rad_listen_t *listen_parse(CONF_SECTION *cs, char const *server)
2597 cp = cf_pair_find(cs, "type");
2600 "No type specified in listen section");
2604 value = cf_pair_value(cp);
2607 "Type cannot be empty");
2611 snprintf(buffer, sizeof(buffer), "proto_%s", value);
2612 handle = lt_dlopenext(buffer);
2614 fr_protocol_t *proto;
2616 proto = dlsym(handle, buffer);
2619 "Failed linking to protocol %s : %s\n",
2625 type = fr_str2int(listen_compare, value, -1);
2626 rad_assert(type >= 0); /* shouldn't be able to compile an invalid type */
2628 memcpy(&master_listen[type], proto, sizeof(*proto));
2631 * And throw away the handle.
2632 * @todo: fix it later
2635 if (master_listen[type].magic != RLM_MODULE_INIT) {
2636 ERROR("Failed to load protocol '%s' due to internal sanity check problem",
2637 master_listen[type].name);
2642 cf_log_info(cs, "listen {");
2645 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
2647 if (rcode < 0) return NULL;
2650 "No type specified in listen section");
2654 type = fr_str2int(listen_compare, listen_type, -1);
2657 "Invalid type \"%s\" in listen section.",
2663 * Allow listen sections in the default config to
2664 * refer to a server.
2667 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
2669 if (rcode == 1) { /* compatiblity with 2.0-pre */
2670 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
2673 if (rcode < 0) return NULL;
2678 * We were passed a virtual server, so the caller is
2679 * defining a proxy listener inside of a virtual server.
2680 * This isn't allowed right now.
2682 else if (type == RAD_LISTEN_PROXY) {
2683 ERROR("Error: listen type \"proxy\" Cannot appear in a virtual server section");
2689 * Set up cross-type data.
2691 this = listen_alloc(cs, type);
2692 this->server = server;
2696 * Call per-type parser.
2698 if (master_listen[type].parse(cs, this) < 0) {
2703 cf_log_info(cs, "}");
2709 static int is_loopback(fr_ipaddr_t const *ipaddr)
2712 * We shouldn't proxy on loopback.
2714 if ((ipaddr->af == AF_INET) &&
2715 (ipaddr->ipaddr.ip4addr.s_addr == htonl(INADDR_LOOPBACK))) {
2719 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2720 if ((ipaddr->af == AF_INET6) &&
2721 (IN6_IS_ADDR_LINKLOCAL(&ipaddr->ipaddr.ip6addr))) {
2731 * Generate a list of listeners. Takes an input list of
2732 * listeners, too, so we don't close sockets with waiting packets.
2734 int listen_init(CONF_SECTION *config, rad_listen_t **head,
2738 UNUSED int spawn_flag
2743 int override = false;
2744 CONF_SECTION *cs = NULL;
2745 rad_listen_t **last;
2747 fr_ipaddr_t server_ipaddr;
2750 int defined_proxy = 0;
2754 * We shouldn't be called with a pre-existing list.
2756 rad_assert(head && (*head == NULL));
2759 server_ipaddr.af = AF_UNSPEC;
2762 * If the port is specified on the command-line,
2763 * it over-rides the configuration file.
2765 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
2767 if (mainconfig.port >= 0) {
2768 auth_port = mainconfig.port;
2771 * -p X but no -i Y on the command-line.
2773 if ((mainconfig.port > 0) &&
2774 (mainconfig.myip.af == AF_UNSPEC)) {
2775 ERROR("The command-line says \"-p %d\", but there is no associated IP address to use",
2782 * If the IP address was configured on the command-line,
2783 * use that as the "bind_address"
2785 if (mainconfig.myip.af != AF_UNSPEC) {
2786 listen_socket_t *sock;
2788 memcpy(&server_ipaddr, &mainconfig.myip,
2789 sizeof(server_ipaddr));
2793 if (strcmp(progname, "vmpsd") == 0) {
2794 this = listen_alloc(config, RAD_LISTEN_VQP);
2795 if (!auth_port) auth_port = 1589;
2798 this = listen_alloc(config, RAD_LISTEN_AUTH);
2802 sock->my_ipaddr = server_ipaddr;
2803 sock->my_port = auth_port;
2805 sock->clients = clients_parse_section(config);
2806 if (!sock->clients) {
2807 cf_log_err_cs(config,
2808 "Failed to find any clients for this listen section");
2813 if (listen_bind(this) < 0) {
2815 ERROR("There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
2819 auth_port = sock->my_port; /* may have been updated in listen_bind */
2821 cs = cf_section_sub_find_name2(config, "server",
2823 if (cs) this->server = mainconfig.name;
2827 last = &(this->next);
2833 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
2836 #ifdef WITH_ACCOUNTING
2838 * Open Accounting Socket.
2840 * If we haven't already gotten acct_port from
2841 * /etc/services, then make it auth_port + 1.
2843 this = listen_alloc(config, RAD_LISTEN_ACCT);
2847 * Create the accounting socket.
2849 * The accounting port is always the
2850 * authentication port + 1
2852 sock->my_ipaddr = server_ipaddr;
2853 sock->my_port = auth_port + 1;
2855 sock->clients = clients_parse_section(config);
2856 if (!sock->clients) {
2857 cf_log_err_cs(config,
2858 "Failed to find any clients for this listen section");
2862 if (listen_bind(this) < 0) {
2865 ERROR("There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
2870 cs = cf_section_sub_find_name2(config, "server",
2872 if (cs) this->server = mainconfig.name;
2876 last = &(this->next);
2881 * They specified an IP on the command-line, ignore
2882 * all listen sections except the one in '-n'.
2884 if (mainconfig.myip.af != AF_UNSPEC) {
2885 CONF_SECTION *subcs;
2886 char const *name2 = cf_section_name2(cs);
2888 cs = cf_section_sub_find_name2(config, "server",
2890 if (!cs) goto add_sockets;
2893 * Should really abstract this code...
2895 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2897 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2898 this = listen_parse(subcs, name2);
2905 last = &(this->next);
2906 } /* loop over "listen" directives in server <foo> */
2912 * Walk through the "listen" sections, if they exist.
2914 for (cs = cf_subsection_find_next(config, NULL, "listen");
2916 cs = cf_subsection_find_next(config, cs, "listen")) {
2917 this = listen_parse(cs, NULL);
2924 last = &(this->next);
2928 * Check virtual servers for "listen" sections, too.
2930 * FIXME: Move to virtual server init?
2932 for (cs = cf_subsection_find_next(config, NULL, "server");
2934 cs = cf_subsection_find_next(config, cs, "server")) {
2935 CONF_SECTION *subcs;
2936 char const *name2 = cf_section_name2(cs);
2938 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2940 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2941 this = listen_parse(subcs, name2);
2948 last = &(this->next);
2949 } /* loop over "listen" directives in virtual servers */
2950 } /* loop over virtual servers */
2954 * No sockets to receive packets, this is an error.
2955 * proxying is pointless.
2958 ERROR("The server is not configured to listen on any ports. Cannot start.");
2963 * Print out which sockets we're listening on, and
2964 * add them to the event list.
2966 for (this = *head; this != NULL; this = this->next) {
2968 if (this->type == RAD_LISTEN_PROXY) {
2975 if (!spawn_flag && this->tls) {
2976 cf_log_err_cs(this->cs, "Threading must be enabled for TLS sockets to function properly.");
2977 cf_log_err_cs(this->cs, "You probably need to do 'radiusd -fxx -l stdout' for debugging");
2981 if (!check_config) event_new_fd(this);
2985 * If we're proxying requests, open the proxy FD.
2986 * Otherwise, don't do anything.
2989 if ((mainconfig.proxy_requests == true) &&
2991 (*head != NULL) && !defined_proxy) {
2992 listen_socket_t *sock = NULL;
2996 memset(&home, 0, sizeof(home));
3001 home.proto = IPPROTO_UDP;
3002 home.src_ipaddr = server_ipaddr;
3005 * Find the first authentication port,
3008 for (this = *head; this != NULL; this = this->next) {
3009 if (this->type == RAD_LISTEN_AUTH) {
3012 if (is_loopback(&sock->my_ipaddr)) continue;
3014 if (home.src_ipaddr.af == AF_UNSPEC) {
3015 home.src_ipaddr = sock->my_ipaddr;
3017 port = sock->my_port + 2;
3021 if (this->type == RAD_LISTEN_ACCT) {
3024 if (is_loopback(&sock->my_ipaddr)) continue;
3026 if (home.src_ipaddr.af == AF_UNSPEC) {
3027 home.src_ipaddr = sock->my_ipaddr;
3029 port = sock->my_port + 1;
3036 * Address is still unspecified, use IPv4.
3038 if (home.src_ipaddr.af == AF_UNSPEC) {
3039 home.src_ipaddr.af = AF_INET;
3040 /* everything else is already set to zero */
3043 home.ipaddr.af = home.src_ipaddr.af;
3044 /* everything else is already set to zero */
3046 this = proxy_new_listener(&home, port);
3052 if (!event_new_fd(this)) {
3061 * Haven't defined any sockets. Die.
3063 if (!*head) return -1;
3065 xlat_register("listen", xlat_listen, NULL, NULL);
3071 * Free a linked list of listeners;
3073 void listen_free(rad_listen_t **head)
3077 if (!head || !*head) return;
3081 rad_listen_t *next = this->next;
3090 RADCLIENT_LIST *listener_find_client_list(fr_ipaddr_t const *ipaddr,
3095 for (this = mainconfig.listen; this != NULL; this = this->next) {
3096 listen_socket_t *sock;
3098 if ((this->type != RAD_LISTEN_AUTH)
3099 #ifdef WITH_ACCOUNTING
3100 && (this->type != RAD_LISTEN_ACCT)
3106 if ((sock->my_port == port) &&
3107 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
3108 return sock->clients;
3116 rad_listen_t *listener_find_byipaddr(fr_ipaddr_t const *ipaddr, int port, int proto)
3120 for (this = mainconfig.listen; this != NULL; this = this->next) {
3121 listen_socket_t *sock;
3125 if (sock->my_port != port) continue;
3126 if (sock->proto != proto) continue;
3127 if (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) != 0) continue;
3133 * Failed to find a specific one. Find INADDR_ANY
3135 for (this = mainconfig.listen; this != NULL; this = this->next) {
3136 listen_socket_t *sock;
3140 if (sock->my_port != port) continue;
3141 if (sock->proto != proto) continue;
3142 if (!fr_inaddr_any(&sock->my_ipaddr)) continue;