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/vqp.h>
30 #include <freeradius-devel/process.h>
31 #include <freeradius-devel/protocol.h>
33 #include <freeradius-devel/detail.h>
36 #include <freeradius-devel/udpfromto.h>
39 #ifdef HAVE_SYS_RESOURCE_H
40 #include <sys/resource.h>
52 #ifdef DEBUG_PRINT_PACKET
53 static void print_packet(RADIUS_PACKET *packet)
55 char src[256], dst[256];
57 ip_ntoh(&packet->src_ipaddr, src, sizeof(src));
58 ip_ntoh(&packet->dst_ipaddr, dst, sizeof(dst));
60 fprintf(stderr, "ID %d: %s %d -> %s %d\n", packet->id,
61 src, packet->src_port, dst, packet->dst_port);
67 static rad_listen_t *listen_alloc(TALLOC_CTX *ctx, RAD_LISTEN_TYPE type);
69 #ifdef WITH_COMMAND_SOCKET
70 static int command_tcp_recv(rad_listen_t *listener);
71 static int command_tcp_send(rad_listen_t *listener, REQUEST *request);
72 static int command_write_magic(int newfd, listen_socket_t *sock);
75 static fr_protocol_t master_listen[RAD_LISTEN_MAX];
78 * Xlat for %{listen:foo}
80 static size_t xlat_listen(UNUSED void *instance, REQUEST *request,
81 const char *fmt, char *out,
84 const char *value = NULL;
87 if (!fmt || !out || (outlen < 1)) return 0;
89 if (!request || !request->listener) {
94 cp = cf_pair_find(request->listener->cs, fmt);
95 if (!cp || !(value = cf_pair_value(cp))) {
100 strlcpy(out, value, outlen);
106 * Find a per-socket client.
108 RADCLIENT *client_listener_find(rad_listen_t *listener,
109 const fr_ipaddr_t *ipaddr, int src_port)
111 #ifdef WITH_DYNAMIC_CLIENTS
118 RADCLIENT_LIST *clients;
119 listen_socket_t *sock;
121 rad_assert(listener != NULL);
122 rad_assert(ipaddr != NULL);
124 sock = listener->data;
125 clients = sock->clients;
128 * This HAS to have been initialized previously.
130 rad_assert(clients != NULL);
132 client = client_find(clients, ipaddr,sock->proto);
134 char name[256], buffer[128];
136 #ifdef WITH_DYNAMIC_CLIENTS
137 unknown: /* used only for dynamic clients */
141 * DoS attack quenching, but only in daemon mode.
142 * If they're running in debug mode, show them
145 if (debug_flag == 0) {
146 static time_t last_printed = 0;
149 if (last_printed == now) return NULL;
154 listener->print(listener, name, sizeof(name));
156 radlog(L_ERR, "Ignoring request to %s from unknown client %s port %d"
160 , name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
161 buffer, sizeof(buffer)), src_port
163 , (sock->proto == IPPROTO_UDP) ? "udp" : "tcp"
169 #ifndef WITH_DYNAMIC_CLIENTS
170 return client; /* return the found client. */
174 * No server defined, and it's not dynamic. Return it.
176 if (!client->client_server && !client->dynamic) return client;
181 * It's a dynamically generated client, check it.
183 if (client->dynamic && (src_port != 0)) {
185 * Lives forever. Return it.
187 if (client->lifetime == 0) return client;
190 * Rate-limit the deletion of known clients.
191 * This makes them last a little longer, but
192 * prevents the server from melting down if (say)
193 * 10k clients all expire at once.
195 if (now == client->last_new_client) return client;
198 * It's not dead yet. Return it.
200 if ((client->created + client->lifetime) > now) return client;
203 * This really puts them onto a queue for later
206 client_delete(clients, client);
209 * Go find the enclosing network again.
211 client = client_find(clients, ipaddr, sock->proto);
216 if (!client) goto unknown;
217 if (!client->client_server) goto unknown;
220 * At this point, 'client' is the enclosing
221 * network that configures where dynamic clients
224 rad_assert(client->dynamic == 0);
226 } else if (!client->dynamic && client->rate_limit) {
228 * The IP is unknown, so we've found an enclosing
229 * network. Enable DoS protection. We only
230 * allow one new client per second. Known
231 * clients aren't subject to this restriction.
233 if (now == client->last_new_client) goto unknown;
236 client->last_new_client = now;
238 request = request_alloc();
239 if (!request) goto unknown;
241 request->listener = listener;
242 request->client = client;
243 request->packet = rad_recv(listener->fd, 0x02); /* MSG_PEEK */
244 if (!request->packet) { /* badly formed, etc */
245 request_free(&request);
248 request->reply = rad_alloc_reply(request, request->packet);
249 if (!request->reply) {
250 request_free(&request);
253 gettimeofday(&request->packet->timestamp, NULL);
255 request->priority = listener->type;
256 request->server = client->client_server;
257 request->root = &mainconfig;
260 * Run a fake request through the given virtual server.
261 * Look for FreeRADIUS-Client-IP-Address
262 * FreeRADIUS-Client-Secret
265 * and create the RADCLIENT structure from that.
267 DEBUG("server %s {", request->server);
269 rcode = process_authorize(0, request);
271 DEBUG("} # server %s", request->server);
273 if (rcode != RLM_MODULE_OK) {
274 request_free(&request);
279 * If the client was updated by rlm_dynamic_clients,
280 * don't create the client from attribute-value pairs.
282 if (request->client == client) {
283 created = client_create(clients, request);
285 created = request->client;
288 * This frees the client if it isn't valid.
290 if (!client_validate(clients, client, created)) goto unknown;
293 request->server = client->server;
294 exec_trigger(request, NULL, "server.client.add", FALSE);
296 request_free(&request);
298 if (!created) goto unknown;
304 static int listen_bind(rad_listen_t *this);
308 * Process and reply to a server-status request.
309 * Like rad_authenticate and rad_accounting this should
310 * live in it's own file but it's so small we don't bother.
312 int rad_status_server(REQUEST *request)
314 int rcode = RLM_MODULE_OK;
317 switch (request->listener->type) {
319 case RAD_LISTEN_NONE:
321 case RAD_LISTEN_AUTH:
322 dval = dict_valbyname(PW_AUTZ_TYPE, 0, "Status-Server");
324 rcode = process_authorize(dval->value, request);
326 rcode = RLM_MODULE_OK;
331 case RLM_MODULE_UPDATED:
332 request->reply->code = PW_AUTHENTICATION_ACK;
335 case RLM_MODULE_FAIL:
336 case RLM_MODULE_HANDLED:
337 request->reply->code = 0; /* don't reply */
341 case RLM_MODULE_REJECT:
342 request->reply->code = PW_AUTHENTICATION_REJECT;
347 #ifdef WITH_ACCOUNTING
348 case RAD_LISTEN_ACCT:
349 dval = dict_valbyname(PW_ACCT_TYPE, 0, "Status-Server");
351 rcode = process_accounting(dval->value, request);
353 rcode = RLM_MODULE_OK;
358 case RLM_MODULE_UPDATED:
359 request->reply->code = PW_ACCOUNTING_RESPONSE;
363 request->reply->code = 0; /* don't reply */
371 * This is a vendor extension. Suggested by Glen
372 * Zorn in IETF 72, and rejected by the rest of
373 * the WG. We like it, so it goes in here.
376 dval = dict_valbyname(PW_RECV_COA_TYPE, 0, "Status-Server");
378 rcode = process_recv_coa(dval->value, request);
380 rcode = RLM_MODULE_OK;
385 case RLM_MODULE_UPDATED:
386 request->reply->code = PW_COA_ACK;
390 request->reply->code = 0; /* don't reply */
402 * Full statistics are available only on a statistics
405 if (request->listener->type == RAD_LISTEN_NONE) {
406 request_stats_reply(request);
414 static int dual_tcp_recv(rad_listen_t *listener)
417 RADIUS_PACKET *packet;
418 RAD_REQUEST_FUNP fun = NULL;
419 listen_socket_t *sock = listener->data;
420 RADCLIENT *client = sock->client;
423 * Allocate a packet for partial reads.
426 sock->packet = rad_alloc(NULL, 0);
427 if (!sock->packet) return 0;
429 sock->packet->sockfd = listener->fd;
430 sock->packet->src_ipaddr = sock->other_ipaddr;
431 sock->packet->src_port = sock->other_port;
432 sock->packet->dst_ipaddr = sock->my_ipaddr;
433 sock->packet->dst_port = sock->my_port;
437 * Grab the packet currently being processed.
439 packet = sock->packet;
441 rcode = fr_tcp_read_packet(packet, 0);
444 * Still only a partial packet. Put it back, and return,
445 * so that we'll read more data when it's ready.
451 if (rcode == -1) { /* error reading packet */
454 radlog(L_ERR, "Invalid packet from %s port %d: closing socket",
455 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
459 if (rcode < 0) { /* error or connection reset */
460 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
463 * Decrement the number of connections.
465 if (sock->parent->limit.num_connections > 0) {
466 sock->parent->limit.num_connections--;
468 if (sock->client->limit.num_connections > 0) {
469 sock->client->limit.num_connections--;
473 * Tell the event handler that an FD has disappeared.
475 DEBUG("Client has closed connection");
476 event_new_fd(listener);
479 * Do NOT free the listener here. It's in use by
480 * a request, and will need to hang around until
481 * all of the requests are done.
483 * It is instead free'd in remove_from_request_hash()
489 * Some sanity checks, based on the packet code.
491 switch(packet->code) {
492 case PW_AUTHENTICATION_REQUEST:
493 if (listener->type != RAD_LISTEN_AUTH) goto bad_packet;
494 FR_STATS_INC(auth, total_requests);
495 fun = rad_authenticate;
498 #ifdef WITH_ACCOUNTING
499 case PW_ACCOUNTING_REQUEST:
500 if (listener->type != RAD_LISTEN_ACCT) goto bad_packet;
501 FR_STATS_INC(acct, total_requests);
502 fun = rad_accounting;
506 case PW_STATUS_SERVER:
507 if (!mainconfig.status_server) {
508 FR_STATS_INC(auth, total_unknown_types);
509 DEBUGW("Ignoring Status-Server request due to security configuration");
510 rad_free(&sock->packet);
513 fun = rad_status_server;
518 FR_STATS_INC(auth, total_unknown_types);
520 DEBUG("Invalid packet code %d sent from client %s port %d : IGNORED",
521 packet->code, client->shortname, packet->src_port);
522 rad_free(&sock->packet);
524 } /* switch over packet types */
526 if (!request_receive(listener, packet, client, fun)) {
527 FR_STATS_INC(auth, total_packets_dropped);
528 rad_free(&sock->packet);
532 sock->packet = NULL; /* we have no need for more partial reads */
536 static int dual_tcp_accept(rad_listen_t *listener)
541 struct sockaddr_storage src;
542 listen_socket_t *sock;
543 fr_ipaddr_t src_ipaddr;
544 RADCLIENT *client = NULL;
548 DEBUG2(" ... new connection request on TCP socket.");
550 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
553 * Non-blocking sockets must handle this.
556 if (errno == EWOULDBLOCK) {
561 DEBUG2(" ... failed to accept connection.");
565 if (!fr_sockaddr2ipaddr(&src, salen, &src_ipaddr, &src_port)) {
567 DEBUG2(" ... unknown address family.");
572 * Enforce client IP address checks on accept, not on
575 if ((client = client_listener_find(listener,
576 &src_ipaddr, src_port)) == NULL) {
578 FR_STATS_INC(auth, total_invalid_requests);
583 * Enforce max_connections on client && listen section.
585 if ((client->limit.max_connections != 0) &&
586 (client->limit.max_connections == client->limit.num_connections)) {
588 * FIXME: Print client IP/port, and server IP/port.
590 radlog(L_INFO, "Ignoring new connection due to client max_connections (%d)", client->limit.max_connections);
595 sock = listener->data;
596 if ((sock->limit.max_connections != 0) &&
597 (sock->limit.max_connections == sock->limit.num_connections)) {
599 * FIXME: Print client IP/port, and server IP/port.
601 radlog(L_INFO, "Ignoring new connection due to socket max_connections");
605 client->limit.num_connections++;
606 sock->limit.num_connections++;
609 * Add the new listener.
611 this = listen_alloc(listener, listener->type);
612 if (!this) return -1;
615 * Copy everything, including the pointer to the socket
619 memcpy(this->data, listener->data, sizeof(*sock));
620 memcpy(this, listener, sizeof(*this));
622 this->data = sock; /* fix it back */
624 sock->parent = listener->data;
625 sock->other_ipaddr = src_ipaddr;
626 sock->other_port = src_port;
627 sock->client = client;
628 sock->opened = sock->last_packet = time(NULL);
631 * Set the limits. The defaults are the parent limits.
632 * Client limits on max_connections are enforced dynamically.
633 * Set the MINIMUM of client/socket idle timeout or lifetime.
635 memcpy(&sock->limit, &sock->parent->limit, sizeof(sock->limit));
637 if (client->limit.idle_timeout &&
638 ((sock->limit.idle_timeout == 0) ||
639 (client->limit.idle_timeout < sock->limit.idle_timeout))) {
640 sock->limit.idle_timeout = client->limit.idle_timeout;
643 if (client->limit.lifetime &&
644 ((sock->limit.lifetime == 0) ||
645 (client->limit.lifetime < sock->limit.lifetime))) {
646 sock->limit.lifetime = client->limit.lifetime;
650 this->status = RAD_LISTEN_STATUS_INIT;
653 #ifdef WITH_COMMAND_SOCKET
654 if (this->type == RAD_LISTEN_COMMAND) {
655 this->recv = command_tcp_recv;
656 this->send = command_tcp_send;
657 command_write_magic(this->fd, sock);
662 this->recv = dual_tcp_recv;
666 this->recv = dual_tls_recv;
667 this->send = dual_tls_send;
673 * FIXME: set O_NONBLOCK on the accept'd fd.
674 * See djb's portability rants for details.
678 * Tell the event loop that we have a new FD.
679 * This can be called from a child thread...
689 * This function is stupid and complicated.
691 int common_socket_print(const rad_listen_t *this, char *buffer, size_t bufsize)
694 listen_socket_t *sock = this->data;
695 const char *name = master_listen[this->type].name;
697 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
698 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
702 if (sock->interface) {
703 ADDSTRING(" interface ");
704 ADDSTRING(sock->interface);
708 if (this->recv == dual_tcp_accept) {
709 ADDSTRING(" proto tcp");
715 * TCP sockets get printed a little differently, to make
716 * it clear what's going on.
719 ADDSTRING(" from client (");
720 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
724 snprintf(buffer, bufsize, "%d", sock->other_port);
728 if ((sock->my_ipaddr.af == AF_INET) &&
729 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
730 strlcpy(buffer, "*", bufsize);
732 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
737 snprintf(buffer, bufsize, "%d", sock->my_port);
741 ADDSTRING(", virtual-server=");
742 ADDSTRING(this->server);
752 * Maybe it's a socket that we opened to a home server.
754 if ((sock->proto == IPPROTO_TCP) &&
755 (this->type == RAD_LISTEN_PROXY)) {
757 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
761 snprintf(buffer, bufsize, "%d", sock->my_port);
763 ADDSTRING(") -> home_server (");
765 if ((sock->other_ipaddr.af == AF_INET) &&
766 (sock->other_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
767 strlcpy(buffer, "*", bufsize);
769 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
774 snprintf(buffer, bufsize, "%d", sock->other_port);
781 #endif /* WITH_PROXY */
782 #endif /* WITH_TCP */
784 ADDSTRING(" address ");
786 if ((sock->my_ipaddr.af == AF_INET) &&
787 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
788 strlcpy(buffer, "*", bufsize);
790 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
795 snprintf(buffer, bufsize, "%d", sock->my_port);
806 ADDSTRING(" as server ");
807 ADDSTRING(this->server);
816 extern int check_config; /* radiusd.c */
819 static CONF_PARSER limit_config[] = {
820 { "max_connections", PW_TYPE_INTEGER,
821 offsetof(listen_socket_t, limit.max_connections), NULL, "16" },
823 { "lifetime", PW_TYPE_INTEGER,
824 offsetof(listen_socket_t, limit.lifetime), NULL, "0" },
826 { "idle_timeout", PW_TYPE_INTEGER,
827 offsetof(listen_socket_t, limit.idle_timeout), NULL, "30" },
829 { NULL, -1, 0, NULL, NULL } /* end the list */
834 * Parse an authentication or accounting socket.
836 int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
839 int listen_port, max_pps;
841 listen_socket_t *sock = this->data;
842 char *section_name = NULL;
843 CONF_SECTION *client_cs, *parentcs;
850 memset(&ipaddr, 0, sizeof(ipaddr));
851 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
852 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
853 &ipaddr.ipaddr.ip4addr, NULL);
854 if (rcode < 0) return -1;
856 if (rcode == 0) { /* successfully parsed IPv4 */
859 } else { /* maybe IPv6? */
860 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
861 &ipaddr.ipaddr.ip6addr, NULL);
862 if (rcode < 0) return -1;
866 "No address specified in listen section");
869 ipaddr.af = AF_INET6;
872 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
874 if (rcode < 0) return -1;
876 if ((listen_port < 0) || (listen_port > 65535)) {
878 "Invalid value for \"port\"");
882 rcode = cf_item_parse(cs, "max_pps", PW_TYPE_INTEGER,
884 if (rcode < 0) return -1;
886 if (max_pps && ((max_pps < 10) || (max_pps > 1000000))) {
888 "Invalid value for \"max_pps\"");
892 sock->proto = IPPROTO_UDP;
894 if (cf_pair_find(cs, "proto")) {
897 "System does not support the TCP protocol. Delete this line from the configuration file.");
905 rcode = cf_item_parse(cs, "proto", PW_TYPE_STRING_PTR,
907 if (rcode < 0) return -1;
909 if (strcmp(proto, "udp") == 0) {
910 sock->proto = IPPROTO_UDP;
912 } else if (strcmp(proto, "tcp") == 0) {
913 sock->proto = IPPROTO_TCP;
916 limit = cf_section_sub_find(cs, "limit");
918 rcode = cf_section_parse(limit, sock,
920 if (rcode < 0) return -1;
922 sock->limit.max_connections = 60;
923 sock->limit.idle_timeout = 30;
924 sock->limit.lifetime = 0;
927 if ((sock->limit.idle_timeout > 0) && (sock->limit.idle_timeout < 5))
928 sock->limit.idle_timeout = 5;
929 if ((sock->limit.lifetime > 0) && (sock->limit.lifetime < 5))
930 sock->limit.lifetime = 5;
931 if ((sock->limit.lifetime > 0) && (sock->limit.idle_timeout > sock->limit.lifetime))
932 sock->limit.idle_timeout = 0;
935 "Unknown proto name \"%s\"", proto);
940 * TCP requires a destination IP for sockets.
941 * UDP doesn't, so it's allowed.
944 if ((this->type == RAD_LISTEN_PROXY) &&
945 (sock->proto != IPPROTO_UDP)) {
947 "Proxy listeners can only listen on proto = udp");
950 #endif /* WITH_PROXY */
953 tls = cf_section_sub_find(cs, "tls");
956 * Don't allow TLS configurations for UDP sockets.
958 if (sock->proto != IPPROTO_TCP) {
960 "TLS transport is not available for UDP sockets.");
966 * FIXME: Make this better.
968 if (listen_port == 0) listen_port = 2083;
970 this->tls = tls_server_conf_parse(tls);
975 #ifdef HAVE_PTRHEAD_H
976 if (pthread_mutex_init(&sock->mutex, NULL) < 0) {
986 * Built without TLS. Disallow it.
988 if (cf_section_sub_find(cs, "tls")) {
990 "TLS transport is not available in this executable.");
993 #endif /* WITH_TLS */
995 #endif /* WITH_TCP */
998 * No "proto" field. Disallow TLS.
1000 } else if (cf_section_sub_find(cs, "tls")) {
1002 "TLS transport is not available in this \"listen\" section.");
1006 sock->my_ipaddr = ipaddr;
1007 sock->my_port = listen_port;
1008 sock->max_rate = max_pps;
1012 if (home_server_find(&sock->my_ipaddr, sock->my_port, sock->proto)) {
1015 DEBUGE("We have been asked to listen on %s port %d, which is also listed as a home server. This can create a proxy loop.",
1016 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
1021 return 0; /* don't do anything */
1026 * If we can bind to interfaces, do so,
1029 if (cf_pair_find(cs, "interface")) {
1031 CONF_PAIR *cp = cf_pair_find(cs, "interface");
1033 rad_assert(cp != NULL);
1034 value = cf_pair_value(cp);
1037 "No interface name given");
1040 sock->interface = value;
1045 * If we can do broadcasts..
1047 if (cf_pair_find(cs, "broadcast")) {
1048 #ifndef SO_BROADCAST
1050 "System does not support broadcast sockets. Delete this line from the configuration file.");
1054 CONF_PAIR *cp = cf_pair_find(cs, "broadcast");
1056 if (this->type != RAD_LISTEN_DHCP) {
1058 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file.");
1062 rad_assert(cp != NULL);
1063 value = cf_pair_value(cp);
1066 "No broadcast value given");
1071 * Hack... whatever happened to cf_section_parse?
1073 sock->broadcast = (strcmp(value, "yes") == 0);
1079 * And bind it to the port.
1081 if (listen_bind(this) < 0) {
1084 "Error binding to port for %s port %d",
1085 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
1092 * Proxy sockets don't have clients.
1094 if (this->type == RAD_LISTEN_PROXY) return 0;
1098 * The more specific configurations are preferred to more
1102 parentcs = cf_top_section(cs);
1103 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
1104 §ion_name, NULL);
1105 if (rcode < 0) return -1; /* bad string */
1108 * Explicit list given: use it.
1110 client_cs = cf_section_sub_find_name2(parentcs,
1114 client_cs = cf_section_find(section_name);
1118 "Failed to find clients %s {...}",
1122 } /* else there was no "clients = " entry. */
1125 CONF_SECTION *server_cs;
1127 server_cs = cf_section_sub_find_name2(parentcs,
1131 * Found a "server foo" section. If there are clients
1135 (cf_section_sub_find(server_cs, "client") != NULL)) {
1136 client_cs = server_cs;
1141 * Still nothing. Look for global clients.
1143 if (!client_cs) client_cs = parentcs;
1145 sock->clients = clients_parse_section(client_cs);
1146 if (!sock->clients) {
1148 "Failed to load clients for this listen section");
1153 if (sock->proto == IPPROTO_TCP) {
1155 * Re-write the listener receive function to
1156 * allow us to accept the socket.
1158 this->recv = dual_tcp_accept;
1166 * Send an authentication response packet
1168 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1170 rad_assert(request->listener == listener);
1171 rad_assert(listener->send == auth_socket_send);
1173 #ifdef WITH_UDPFROMTO
1175 * Overwrite the src ip address on the outbound packet
1176 * with the one specified by the client.
1177 * This is useful to work around broken DSR implementations
1178 * and other routing issues.
1180 if (request->client->src_ipaddr.af != AF_UNSPEC) {
1181 request->reply->src_ipaddr = request->client->src_ipaddr;
1185 if (rad_send(request->reply, request->packet,
1186 request->client->secret) < 0) {
1187 radlog_request(L_ERR, 0, request, "Failed sending reply: %s",
1196 #ifdef WITH_ACCOUNTING
1198 * Send an accounting response packet (or not)
1200 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1202 rad_assert(request->listener == listener);
1203 rad_assert(listener->send == acct_socket_send);
1206 * Accounting reject's are silently dropped.
1208 * We do it here to avoid polluting the rest of the
1209 * code with this knowledge
1211 if (request->reply->code == 0) return 0;
1213 #ifdef WITH_UDPFROMTO
1215 * Overwrite the src ip address on the outbound packet
1216 * with the one specified by the client.
1217 * This is useful to work around broken DSR implementations
1218 * and other routing issues.
1220 if (request->client->src_ipaddr.af != AF_UNSPEC) {
1221 request->reply->src_ipaddr = request->client->src_ipaddr;
1225 if (rad_send(request->reply, request->packet,
1226 request->client->secret) < 0) {
1227 radlog_request(L_ERR, 0, request, "Failed sending reply: %s",
1238 * Send a packet to a home server.
1240 * FIXME: have different code for proxy auth & acct!
1242 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1244 rad_assert(request->proxy_listener == listener);
1245 rad_assert(listener->send == proxy_socket_send);
1247 if (rad_send(request->proxy, NULL,
1248 request->home_server->secret) < 0) {
1249 radlog_request(L_ERR, 0, request, "Failed sending proxied request: %s",
1260 * Check if an incoming request is "ok"
1262 * It takes packets, not requests. It sees if the packet looks
1263 * OK. If so, it does a number of sanity checks on it.
1265 static int stats_socket_recv(rad_listen_t *listener)
1269 RADIUS_PACKET *packet;
1270 RADCLIENT *client = NULL;
1271 fr_ipaddr_t src_ipaddr;
1273 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1274 if (rcode < 0) return 0;
1276 FR_STATS_INC(auth, total_requests);
1278 if (rcode < 20) { /* AUTH_HDR_LEN */
1279 FR_STATS_INC(auth, total_malformed_requests);
1283 if ((client = client_listener_find(listener,
1284 &src_ipaddr, src_port)) == NULL) {
1285 rad_recv_discard(listener->fd);
1286 FR_STATS_INC(auth, total_invalid_requests);
1290 FR_STATS_TYPE_INC(client->auth.total_requests);
1293 * We only understand Status-Server on this socket.
1295 if (code != PW_STATUS_SERVER) {
1296 DEBUG("Ignoring packet code %d sent to Status-Server port",
1298 rad_recv_discard(listener->fd);
1299 FR_STATS_INC(auth, total_unknown_types);
1304 * Now that we've sanity checked everything, receive the
1307 packet = rad_recv(listener->fd, 1); /* require message authenticator */
1309 FR_STATS_INC(auth, total_malformed_requests);
1310 DEBUG("%s", fr_strerror());
1314 if (!request_receive(listener, packet, client, rad_status_server)) {
1315 FR_STATS_INC(auth, total_packets_dropped);
1326 * Check if an incoming request is "ok"
1328 * It takes packets, not requests. It sees if the packet looks
1329 * OK. If so, it does a number of sanity checks on it.
1331 static int auth_socket_recv(rad_listen_t *listener)
1335 RADIUS_PACKET *packet;
1336 RAD_REQUEST_FUNP fun = NULL;
1337 RADCLIENT *client = NULL;
1338 fr_ipaddr_t src_ipaddr;
1340 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1341 if (rcode < 0) return 0;
1343 FR_STATS_INC(auth, total_requests);
1345 if (rcode < 20) { /* AUTH_HDR_LEN */
1346 FR_STATS_INC(auth, total_malformed_requests);
1350 if ((client = client_listener_find(listener,
1351 &src_ipaddr, src_port)) == NULL) {
1352 rad_recv_discard(listener->fd);
1353 FR_STATS_INC(auth, total_invalid_requests);
1357 FR_STATS_TYPE_INC(client->auth.total_requests);
1360 * Some sanity checks, based on the packet code.
1363 case PW_AUTHENTICATION_REQUEST:
1364 fun = rad_authenticate;
1367 case PW_STATUS_SERVER:
1368 if (!mainconfig.status_server) {
1369 rad_recv_discard(listener->fd);
1370 FR_STATS_INC(auth, total_unknown_types);
1371 DEBUGW("Ignoring Status-Server request due to security configuration");
1374 fun = rad_status_server;
1378 rad_recv_discard(listener->fd);
1379 FR_STATS_INC(auth,total_unknown_types);
1381 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
1382 code, client->shortname, src_port);
1385 } /* switch over packet types */
1388 * Now that we've sanity checked everything, receive the
1391 packet = rad_recv(listener->fd, client->message_authenticator);
1393 FR_STATS_INC(auth, total_malformed_requests);
1394 DEBUG("%s", fr_strerror());
1398 if (!request_receive(listener, packet, client, fun)) {
1399 FR_STATS_INC(auth, total_packets_dropped);
1408 #ifdef WITH_ACCOUNTING
1410 * Receive packets from an accounting socket
1412 static int acct_socket_recv(rad_listen_t *listener)
1416 RADIUS_PACKET *packet;
1417 RAD_REQUEST_FUNP fun = NULL;
1418 RADCLIENT *client = NULL;
1419 fr_ipaddr_t src_ipaddr;
1421 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1422 if (rcode < 0) return 0;
1424 FR_STATS_INC(acct, total_requests);
1426 if (rcode < 20) { /* AUTH_HDR_LEN */
1427 FR_STATS_INC(acct, total_malformed_requests);
1431 if ((client = client_listener_find(listener,
1432 &src_ipaddr, src_port)) == NULL) {
1433 rad_recv_discard(listener->fd);
1434 FR_STATS_INC(acct, total_invalid_requests);
1438 FR_STATS_TYPE_INC(client->acct.total_requests);
1441 * Some sanity checks, based on the packet code.
1444 case PW_ACCOUNTING_REQUEST:
1445 fun = rad_accounting;
1448 case PW_STATUS_SERVER:
1449 if (!mainconfig.status_server) {
1450 rad_recv_discard(listener->fd);
1451 FR_STATS_INC(acct, total_unknown_types);
1453 DEBUGW("Ignoring Status-Server request due to security configuration");
1456 fun = rad_status_server;
1460 rad_recv_discard(listener->fd);
1461 FR_STATS_INC(acct, total_unknown_types);
1463 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1464 code, client->shortname, src_port);
1466 } /* switch over packet types */
1469 * Now that we've sanity checked everything, receive the
1472 packet = rad_recv(listener->fd, 0);
1474 FR_STATS_INC(acct, total_malformed_requests);
1475 radlog(L_ERR, "%s", fr_strerror());
1480 * There can be no duplicate accounting packets.
1482 if (!request_receive(listener, packet, client, fun)) {
1483 FR_STATS_INC(acct, total_packets_dropped);
1494 static int do_proxy(REQUEST *request)
1498 if (request->in_proxy_hash ||
1499 (request->proxy_reply && (request->proxy_reply->code != 0))) {
1503 vp = pairfind(request->config_items, PW_HOME_SERVER_POOL, 0, TAG_ANY);
1506 if (!home_pool_byname(vp->vp_strvalue, HOME_TYPE_COA)) {
1507 RDEBUG2E("Cannot proxy to unknown pool %s",
1516 * Receive a CoA packet.
1518 static int rad_coa_recv(REQUEST *request)
1520 int rcode = RLM_MODULE_OK;
1525 * Get the correct response
1527 switch (request->packet->code) {
1528 case PW_COA_REQUEST:
1533 case PW_DISCONNECT_REQUEST:
1534 ack = PW_DISCONNECT_ACK;
1535 nak = PW_DISCONNECT_NAK;
1538 default: /* shouldn't happen */
1539 return RLM_MODULE_FAIL;
1543 #define WAS_PROXIED (request->proxy)
1545 #define WAS_PROXIED (0)
1550 * RFC 5176 Section 3.3. If we have a CoA-Request
1551 * with Service-Type = Authorize-Only, it MUST
1552 * have a State attribute in it.
1554 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE, 0, TAG_ANY);
1555 if (request->packet->code == PW_COA_REQUEST) {
1556 if (vp && (vp->vp_integer == 17)) {
1557 vp = pairfind(request->packet->vps, PW_STATE, 0, TAG_ANY);
1558 if (!vp || (vp->length == 0)) {
1559 RDEBUGE("CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1560 request->reply->code = PW_COA_NAK;
1561 return RLM_MODULE_FAIL;
1566 * RFC 5176, Section 3.2.
1568 RDEBUGE("Disconnect-Request MUST NOT contain a Service-Type attribute");
1569 request->reply->code = PW_DISCONNECT_NAK;
1570 return RLM_MODULE_FAIL;
1573 rcode = process_recv_coa(0, request);
1575 case RLM_MODULE_FAIL:
1576 case RLM_MODULE_INVALID:
1577 case RLM_MODULE_REJECT:
1578 case RLM_MODULE_USERLOCK:
1580 request->reply->code = nak;
1583 case RLM_MODULE_HANDLED:
1586 case RLM_MODULE_NOOP:
1587 case RLM_MODULE_NOTFOUND:
1589 case RLM_MODULE_UPDATED:
1590 if (do_proxy(request)) return RLM_MODULE_OK;
1591 request->reply->code = ack;
1594 } else if (request->proxy_reply) {
1596 * Start the reply code with the proxy reply
1599 request->reply->code = request->proxy_reply->code;
1603 * Copy State from the request to the reply.
1604 * See RFC 5176 Section 3.3.
1606 vp = paircopy2(request->reply, request->packet->vps, PW_STATE, 0, TAG_ANY);
1607 if (vp) pairadd(&request->reply->vps, vp);
1610 * We may want to over-ride the reply.
1612 if (request->reply->code) {
1613 rcode = process_send_coa(0, request);
1616 * We need to send CoA-NAK back if Service-Type
1617 * is Authorize-Only. Rely on the user's policy
1618 * to do that. We're not a real NAS, so this
1619 * restriction doesn't (ahem) apply to us.
1621 case RLM_MODULE_FAIL:
1622 case RLM_MODULE_INVALID:
1623 case RLM_MODULE_REJECT:
1624 case RLM_MODULE_USERLOCK:
1627 * Over-ride an ACK with a NAK
1629 request->reply->code = nak;
1632 case RLM_MODULE_HANDLED:
1635 case RLM_MODULE_NOOP:
1636 case RLM_MODULE_NOTFOUND:
1638 case RLM_MODULE_UPDATED:
1640 * Do NOT over-ride a previously set value.
1641 * Otherwise an "ok" here will re-write a
1644 if (request->reply->code == 0) {
1645 request->reply->code = ack;
1651 return RLM_MODULE_OK;
1656 * Check if an incoming request is "ok"
1658 * It takes packets, not requests. It sees if the packet looks
1659 * OK. If so, it does a number of sanity checks on it.
1661 static int coa_socket_recv(rad_listen_t *listener)
1665 RADIUS_PACKET *packet;
1666 RAD_REQUEST_FUNP fun = NULL;
1667 RADCLIENT *client = NULL;
1668 fr_ipaddr_t src_ipaddr;
1670 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1671 if (rcode < 0) return 0;
1673 if (rcode < 20) { /* AUTH_HDR_LEN */
1674 FR_STATS_INC(coa, total_requests);
1675 FR_STATS_INC(coa, total_malformed_requests);
1679 if ((client = client_listener_find(listener,
1680 &src_ipaddr, src_port)) == NULL) {
1681 rad_recv_discard(listener->fd);
1682 FR_STATS_INC(coa, total_requests);
1683 FR_STATS_INC(coa, total_invalid_requests);
1688 * Some sanity checks, based on the packet code.
1691 case PW_COA_REQUEST:
1692 FR_STATS_INC(coa, total_requests);
1696 case PW_DISCONNECT_REQUEST:
1697 FR_STATS_INC(dsc, total_requests);
1702 rad_recv_discard(listener->fd);
1703 FR_STATS_INC(coa, total_unknown_types);
1704 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1705 code, client->shortname, src_port);
1707 } /* switch over packet types */
1710 * Now that we've sanity checked everything, receive the
1713 packet = rad_recv(listener->fd, client->message_authenticator);
1715 FR_STATS_INC(coa, total_malformed_requests);
1716 DEBUG("%s", fr_strerror());
1720 if (!request_receive(listener, packet, client, fun)) {
1721 FR_STATS_INC(coa, total_packets_dropped);
1732 * Recieve packets from a proxy socket.
1734 static int proxy_socket_recv(rad_listen_t *listener)
1736 RADIUS_PACKET *packet;
1739 packet = rad_recv(listener->fd, 0);
1741 radlog(L_ERR, "%s", fr_strerror());
1746 * FIXME: Client MIB updates?
1748 switch(packet->code) {
1749 case PW_AUTHENTICATION_ACK:
1750 case PW_ACCESS_CHALLENGE:
1751 case PW_AUTHENTICATION_REJECT:
1754 #ifdef WITH_ACCOUNTING
1755 case PW_ACCOUNTING_RESPONSE:
1760 case PW_DISCONNECT_ACK:
1761 case PW_DISCONNECT_NAK:
1769 * FIXME: Update MIB for packet types?
1771 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1772 "from home server %s port %d - ID %d : IGNORED",
1774 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1775 packet->src_port, packet->id);
1780 if (!request_proxy_reply(packet)) {
1790 * Recieve packets from a proxy socket.
1792 static int proxy_socket_tcp_recv(rad_listen_t *listener)
1794 RADIUS_PACKET *packet;
1795 listen_socket_t *sock = listener->data;
1798 packet = fr_tcp_recv(listener->fd, 0);
1800 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
1801 event_new_fd(listener);
1806 * FIXME: Client MIB updates?
1808 switch(packet->code) {
1809 case PW_AUTHENTICATION_ACK:
1810 case PW_ACCESS_CHALLENGE:
1811 case PW_AUTHENTICATION_REJECT:
1814 #ifdef WITH_ACCOUNTING
1815 case PW_ACCOUNTING_RESPONSE:
1821 * FIXME: Update MIB for packet types?
1823 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1824 "from home server %s port %d - ID %d : IGNORED",
1826 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1827 packet->src_port, packet->id);
1832 packet->src_ipaddr = sock->other_ipaddr;
1833 packet->src_port = sock->other_port;
1834 packet->dst_ipaddr = sock->my_ipaddr;
1835 packet->dst_port = sock->my_port;
1838 * FIXME: Have it return an indication of packets that
1839 * are OK to ignore (dups, too late), versus ones that
1840 * aren't OK to ignore (unknown response, spoofed, etc.)
1842 * Close the socket on bad packets...
1844 if (!request_proxy_reply(packet)) {
1849 sock->opened = sock->last_packet = time(NULL);
1857 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1859 if (!request->reply->code) return 0;
1861 if (rad_encode(request->reply, request->packet,
1862 request->client->secret) < 0) {
1863 radlog_request(L_ERR, 0, request, "Failed encoding packet: %s",
1868 if (rad_sign(request->reply, request->packet,
1869 request->client->secret) < 0) {
1870 radlog_request(L_ERR, 0, request, "Failed signing packet: %s",
1879 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1881 if (rad_verify(request->packet, NULL,
1882 request->client->secret) < 0) {
1886 return rad_decode(request->packet, NULL,
1887 request->client->secret);
1891 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1893 if (rad_encode(request->proxy, NULL, request->home_server->secret) < 0) {
1894 radlog_request(L_ERR, 0, request, "Failed encoding proxied packet: %s",
1899 if (rad_sign(request->proxy, NULL, request->home_server->secret) < 0) {
1900 radlog_request(L_ERR, 0, request, "Failed signing proxied packet: %s",
1909 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1912 * rad_verify is run in event.c, received_proxy_response()
1915 return rad_decode(request->proxy_reply, request->proxy,
1916 request->home_server->secret);
1920 #include "command.c"
1923 * Temporarily NOT const!
1925 static fr_protocol_t master_listen[RAD_LISTEN_MAX] = {
1927 { RLM_MODULE_INIT, "status", sizeof(listen_socket_t), NULL,
1928 common_socket_parse, NULL,
1929 stats_socket_recv, auth_socket_send,
1930 common_socket_print, client_socket_encode, client_socket_decode },
1933 * This always gets defined.
1935 { RLM_MODULE_INIT, "status", 0, NULL,
1936 NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1941 { RLM_MODULE_INIT, "proxy", sizeof(listen_socket_t), NULL,
1942 common_socket_parse, NULL,
1943 proxy_socket_recv, proxy_socket_send,
1944 common_socket_print, proxy_socket_encode, proxy_socket_decode },
1947 /* authentication */
1948 { RLM_MODULE_INIT, "auth", sizeof(listen_socket_t), NULL,
1949 common_socket_parse, NULL,
1950 auth_socket_recv, auth_socket_send,
1951 common_socket_print, client_socket_encode, client_socket_decode },
1953 #ifdef WITH_ACCOUNTING
1955 { RLM_MODULE_INIT, "acct", sizeof(listen_socket_t), NULL,
1956 common_socket_parse, NULL,
1957 acct_socket_recv, acct_socket_send,
1958 common_socket_print, client_socket_encode, client_socket_decode},
1963 { RLM_MODULE_INIT, "detail", sizeof(listen_detail_t), NULL,
1964 detail_parse, detail_free,
1965 detail_recv, detail_send,
1966 detail_print, detail_encode, detail_decode },
1970 /* vlan query protocol */
1971 { 0, "vmps", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
1975 /* dhcp query protocol */
1976 { 0, "dhcp", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
1979 #ifdef WITH_COMMAND_SOCKET
1980 /* TCP command socket */
1981 { RLM_MODULE_INIT, "control", sizeof(fr_command_socket_t), NULL,
1982 command_socket_parse, command_socket_free,
1983 command_domain_accept, command_domain_send,
1984 command_socket_print, command_socket_encode, command_socket_decode },
1988 /* Change of Authorization */
1989 { RLM_MODULE_INIT, "coa", sizeof(listen_socket_t), NULL,
1990 common_socket_parse, NULL,
1991 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
1992 common_socket_print, client_socket_encode, client_socket_decode },
2000 * Binds a listener to a socket.
2002 static int listen_bind(rad_listen_t *this)
2005 struct sockaddr_storage salocal;
2007 listen_socket_t *sock = this->data;
2009 #define proto_for_port "udp"
2010 #define sock_type SOCK_DGRAM
2012 const char *proto_for_port = "udp";
2013 int sock_type = SOCK_DGRAM;
2015 if (sock->proto == IPPROTO_TCP) {
2017 if (this->type == RAD_LISTEN_VQP) {
2018 radlog(L_ERR, "VQP does not support TCP transport");
2023 proto_for_port = "tcp";
2024 sock_type = SOCK_STREAM;
2029 * If the port is zero, then it means the appropriate
2030 * thing from /etc/services.
2032 if (sock->my_port == 0) {
2033 struct servent *svp;
2035 switch (this->type) {
2036 case RAD_LISTEN_AUTH:
2037 svp = getservbyname ("radius", proto_for_port);
2039 sock->my_port = ntohs(svp->s_port);
2041 sock->my_port = PW_AUTH_UDP_PORT;
2045 #ifdef WITH_ACCOUNTING
2046 case RAD_LISTEN_ACCT:
2047 svp = getservbyname ("radacct", proto_for_port);
2049 sock->my_port = ntohs(svp->s_port);
2051 sock->my_port = PW_ACCT_UDP_PORT;
2057 case RAD_LISTEN_PROXY:
2058 /* leave it at zero */
2063 case RAD_LISTEN_VQP:
2064 sock->my_port = 1589;
2068 #ifdef WITH_COMMAND_SOCKET
2069 case RAD_LISTEN_COMMAND:
2070 sock->my_port = PW_RADMIN_PORT;
2075 case RAD_LISTEN_COA:
2076 svp = getservbyname ("radius-dynauth", "udp");
2078 sock->my_port = ntohs(svp->s_port);
2080 sock->my_port = PW_COA_UDP_PORT;
2086 DEBUGW("Internal sanity check failed in binding to socket. Ignoring problem.");
2092 * Don't open sockets if we're checking the config.
2100 * Copy fr_socket() here, as we may need to bind to a device.
2102 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
2106 this->print(this, buffer, sizeof(buffer));
2108 radlog(L_ERR, "Failed opening %s: %s", buffer, strerror(errno));
2114 * We don't want child processes inheriting these
2117 rcode = fcntl(this->fd, F_GETFD);
2119 if (fcntl(this->fd, F_SETFD, rcode | FD_CLOEXEC) < 0) {
2121 radlog(L_ERR, "Failed setting close on exec: %s", strerror(errno));
2128 * Bind to a device BEFORE touching IP addresses.
2130 if (sock->interface) {
2131 #ifdef SO_BINDTODEVICE
2134 memset(&ifreq, 0, sizeof(ifreq));
2135 strlcpy(ifreq.ifr_name, sock->interface, sizeof(ifreq.ifr_name));
2138 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
2139 (char *)&ifreq, sizeof(ifreq));
2143 radlog(L_ERR, "Failed binding to interface %s: %s",
2144 sock->interface, strerror(errno));
2146 } /* else it worked. */
2148 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2149 #ifdef HAVE_NET_IF_H
2151 * Odds are that any system supporting "bind to
2152 * device" also supports IPv6, so this next bit
2153 * isn't necessary. But it's here for
2156 * If we're doing IPv6, and the scope hasn't yet
2157 * been defined, set the scope to the scope of
2160 if (sock->my_ipaddr.af == AF_INET6) {
2161 if (sock->my_ipaddr.scope == 0) {
2162 sock->my_ipaddr.scope = if_nametoindex(sock->interface);
2163 if (sock->my_ipaddr.scope == 0) {
2165 radlog(L_ERR, "Failed finding interface %s: %s",
2166 sock->interface, strerror(errno));
2169 } /* else scope was defined: we're OK. */
2174 * IPv4: no link local addresses,
2175 * and no bind to device.
2179 radlog(L_ERR, "Failed binding to interface %s: \"bind to device\" is unsupported", sock->interface);
2186 if (sock->proto == IPPROTO_TCP) {
2189 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2191 radlog(L_ERR, "Failed to reuse address: %s", strerror(errno));
2197 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
2198 else /* UDP sockets get UDPfromto */
2201 #ifdef WITH_UDPFROMTO
2203 * Initialize udpfromto for all sockets.
2205 if (udpfromto_init(this->fd) != 0) {
2206 radlog(L_ERR, "Failed initializing udpfromto: %s",
2214 * Set up sockaddr stuff.
2216 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
2221 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2222 if (sock->my_ipaddr.af == AF_INET6) {
2224 * Listening on '::' does NOT get you IPv4 to
2225 * IPv6 mapping. You've got to listen on an IPv4
2226 * address, too. This makes the rest of the server
2227 * design a little simpler.
2231 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2234 if (setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2235 (char *)&on, sizeof(on)) < 0) {
2236 radlog(L_ERR, "Failed setting socket to IPv6 "
2237 "only: %s", strerror(errno));
2243 #endif /* IPV6_V6ONLY */
2245 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2247 if (sock->my_ipaddr.af == AF_INET) {
2250 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2252 * Disable PMTU discovery. On Linux, this
2253 * also makes sure that the "don't fragment"
2256 flag = IP_PMTUDISC_DONT;
2257 if (setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2258 &flag, sizeof(flag)) < 0) {
2259 radlog(L_ERR, "Failed disabling PMTU discovery: %s",
2267 #if defined(IP_DONTFRAG)
2269 * Ensure that the "don't fragment" flag is zero.
2272 if (setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2273 &flag, sizeof(flag)) < 0) {
2274 radlog(L_ERR, "Failed setting don't fragment flag: %s",
2285 if (sock->broadcast) {
2288 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2289 radlog(L_ERR, "Can't set broadcast option: %s",
2298 * May be binding to priviledged ports.
2300 if (sock->my_port != 0) {
2304 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2305 radlog(L_ERR, "Can't set re-use address option: %s",
2312 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2318 this->print(this, buffer, sizeof(buffer));
2319 radlog(L_ERR, "Failed binding to %s: %s\n",
2320 buffer, strerror(errno));
2325 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2326 * kernel instead binds us to a 1.2.3.4. If this
2327 * happens, notice, and remember our real IP.
2330 struct sockaddr_storage src;
2331 socklen_t sizeof_src = sizeof(src);
2333 memset(&src, 0, sizeof_src);
2334 if (getsockname(this->fd, (struct sockaddr *) &src,
2336 radlog(L_ERR, "Failed getting socket name: %s",
2341 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2342 &sock->my_ipaddr, &sock->my_port)) {
2343 radlog(L_ERR, "Socket has unsupported address family");
2350 if (sock->proto == IPPROTO_TCP) {
2351 if (listen(this->fd, 8) < 0) {
2353 radlog(L_ERR, "Failed in listen(): %s", strerror(errno));
2359 if (fr_nonblock(this->fd) < 0) {
2361 radlog(L_ERR, "Failed setting non-blocking on socket: %s",
2367 * Mostly for proxy sockets.
2369 sock->other_ipaddr.af = sock->my_ipaddr.af;
2372 * Don't screw up other people.
2374 #undef proto_for_port
2381 static int listener_free(void *ctx)
2385 this = talloc_get_type_abort(ctx, rad_listen_t);
2388 * Other code may have eaten the FD.
2390 if (this->fd >= 0) close(this->fd);
2392 if (master_listen[this->type].free) {
2393 master_listen[this->type].free(this);
2397 if ((this->type == RAD_LISTEN_AUTH)
2399 || (this->type == RAD_LISTEN_ACCT)
2402 || (this->type == RAD_LISTEN_PROXY)
2404 #ifdef WITH_COMMAND_SOCKET
2405 || ((this->type == RAD_LISTEN_COMMAND) &&
2406 (((fr_command_socket_t *) this->data)->magic != COMMAND_SOCKET_MAGIC))
2409 listen_socket_t *sock = this->data;
2412 if (sock->request) {
2413 pthread_mutex_destroy(&(sock->mutex));
2414 request_free(&sock->request);
2415 sock->packet = NULL;
2417 if (sock->ssn) session_free(sock->ssn);
2418 request_free(&sock->request);
2421 rad_free(&sock->packet);
2423 #endif /* WITH_TCP */
2429 * Allocate & initialize a new listener.
2431 static rad_listen_t *listen_alloc(TALLOC_CTX *ctx, RAD_LISTEN_TYPE type)
2435 this = talloc_zero(ctx, rad_listen_t);
2438 this->recv = master_listen[this->type].recv;
2439 this->send = master_listen[this->type].send;
2440 this->print = master_listen[this->type].print;
2441 this->encode = master_listen[this->type].encode;
2442 this->decode = master_listen[this->type].decode;
2444 talloc_set_destructor((void *) this, listener_free);
2446 this->data = talloc_zero_array(this, uint8_t, master_listen[this->type].inst_size);
2453 * Externally visible function for creating a new proxy LISTENER.
2455 * Not thread-safe, but all calls to it are protected by the
2456 * proxy mutex in event.c
2458 rad_listen_t *proxy_new_listener(home_server *home, int src_port)
2461 listen_socket_t *sock;
2464 if (!home) return NULL;
2466 if ((home->limit.max_connections > 0) &&
2467 (home->limit.num_connections >= home->limit.max_connections)) {
2468 DEBUGW("Home server has too many open connections (%d)",
2469 home->limit.max_connections);
2473 this = listen_alloc(mainconfig.config, RAD_LISTEN_PROXY);
2476 sock->other_ipaddr = home->ipaddr;
2477 sock->other_port = home->port;
2480 sock->my_ipaddr = home->src_ipaddr;
2481 sock->my_port = src_port;
2482 sock->proto = home->proto;
2484 if (debug_flag >= 2) {
2485 this->print(this, buffer, sizeof(buffer));
2486 DEBUG("Opening new %s", buffer);
2490 sock->opened = sock->last_packet = time(NULL);
2492 if (home->proto == IPPROTO_TCP) {
2493 this->recv = proxy_socket_tcp_recv;
2496 * FIXME: connect() is blocking!
2497 * We do this with the proxy mutex locked, which may
2498 * cause large delays!
2500 * http://www.developerweb.net/forum/showthread.php?p=13486
2502 this->fd = fr_tcp_client_socket(&home->src_ipaddr,
2503 &home->ipaddr, home->port);
2506 DEBUG("Trying SSL to port %d\n", home->port);
2507 sock->ssn = tls_new_client_session(home->tls, this->fd);
2513 this->recv = proxy_tls_recv;
2514 this->send = proxy_tls_send;
2519 this->fd = fr_socket(&home->src_ipaddr, src_port);
2522 this->print(this, buffer,sizeof(buffer));
2523 DEBUG("Failed opening client socket ::%s:: : %s",
2524 buffer, fr_strerror());
2530 * Figure out which port we were bound to.
2532 if (sock->my_port == 0) {
2533 struct sockaddr_storage src;
2534 socklen_t sizeof_src = sizeof(src);
2536 memset(&src, 0, sizeof_src);
2537 if (getsockname(this->fd, (struct sockaddr *) &src,
2539 radlog(L_ERR, "Failed getting socket name: %s",
2545 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2546 &sock->my_ipaddr, &sock->my_port)) {
2547 radlog(L_ERR, "Socket has unsupported address family");
2558 static const FR_NAME_NUMBER listen_compare[] = {
2560 { "status", RAD_LISTEN_NONE },
2562 { "auth", RAD_LISTEN_AUTH },
2563 #ifdef WITH_ACCOUNTING
2564 { "acct", RAD_LISTEN_ACCT },
2567 { "detail", RAD_LISTEN_DETAIL },
2570 { "proxy", RAD_LISTEN_PROXY },
2573 { "vmps", RAD_LISTEN_VQP },
2576 { "dhcp", RAD_LISTEN_DHCP },
2578 #ifdef WITH_COMMAND_SOCKET
2579 { "control", RAD_LISTEN_COMMAND },
2582 { "coa", RAD_LISTEN_COA },
2588 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
2598 cp = cf_pair_find(cs, "type");
2601 "No type specified in listen section");
2605 value = cf_pair_value(cp);
2608 "Type cannot be empty");
2612 snprintf(buffer, sizeof(buffer), "proto_%s", value);
2613 handle = lt_dlopenext(buffer);
2615 fr_protocol_t *proto;
2617 proto = dlsym(handle, buffer);
2620 "Failed linking to protocol %s : %s\n",
2626 type = fr_str2int(listen_compare, value, -1);
2627 rad_assert(type >= 0); /* shouldn't be able to compile an invalid type */
2629 memcpy(&master_listen[type], proto, sizeof(*proto));
2632 * And throw away the handle.
2633 * @todo: fix it later
2637 if (master_listen[type].magic != RLM_MODULE_INIT) {
2638 radlog(L_ERR, "Failed to load protocol '%s' due to internal sanity check problem",
2639 master_listen[type].name);
2643 cf_log_info(cs, "listen {");
2646 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
2648 if (rcode < 0) return NULL;
2651 "No type specified in listen section");
2655 type = fr_str2int(listen_compare, listen_type, -1);
2658 "Invalid type \"%s\" in listen section.",
2664 * Allow listen sections in the default config to
2665 * refer to a server.
2668 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
2670 if (rcode == 1) { /* compatiblity with 2.0-pre */
2671 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
2674 if (rcode < 0) return NULL;
2679 * We were passed a virtual server, so the caller is
2680 * defining a proxy listener inside of a virtual server.
2681 * This isn't allowed right now.
2683 else if (type == RAD_LISTEN_PROXY) {
2684 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
2690 * Set up cross-type data.
2692 this = listen_alloc(cs, type);
2693 this->server = server;
2697 * Call per-type parser.
2699 if (master_listen[type].parse(cs, this) < 0) {
2704 cf_log_info(cs, "}");
2710 static int is_loopback(const fr_ipaddr_t *ipaddr)
2713 * We shouldn't proxy on loopback.
2715 if ((ipaddr->af == AF_INET) &&
2716 (ipaddr->ipaddr.ip4addr.s_addr == htonl(INADDR_LOOPBACK))) {
2720 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2721 if ((ipaddr->af == AF_INET6) &&
2722 (IN6_IS_ADDR_LINKLOCAL(&ipaddr->ipaddr.ip6addr))) {
2732 * Generate a list of listeners. Takes an input list of
2733 * listeners, too, so we don't close sockets with waiting packets.
2735 int listen_init(CONF_SECTION *config, rad_listen_t **head,
2739 UNUSED int spawn_flag
2744 int override = FALSE;
2745 CONF_SECTION *cs = NULL;
2746 rad_listen_t **last;
2748 fr_ipaddr_t server_ipaddr;
2751 int defined_proxy = 0;
2755 * We shouldn't be called with a pre-existing list.
2757 rad_assert(head && (*head == NULL));
2760 server_ipaddr.af = AF_UNSPEC;
2763 * If the port is specified on the command-line,
2764 * it over-rides the configuration file.
2766 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
2768 if (mainconfig.port >= 0) {
2769 auth_port = mainconfig.port;
2772 * -p X but no -i Y on the command-line.
2774 if ((mainconfig.port > 0) &&
2775 (mainconfig.myip.af == AF_UNSPEC)) {
2776 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
2783 * If the IP address was configured on the command-line,
2784 * use that as the "bind_address"
2786 if (mainconfig.myip.af != AF_UNSPEC) {
2787 listen_socket_t *sock;
2789 memcpy(&server_ipaddr, &mainconfig.myip,
2790 sizeof(server_ipaddr));
2794 if (strcmp(progname, "vmpsd") == 0) {
2795 this = listen_alloc(config, RAD_LISTEN_VQP);
2796 if (!auth_port) auth_port = 1589;
2799 this = listen_alloc(config, RAD_LISTEN_AUTH);
2803 sock->my_ipaddr = server_ipaddr;
2804 sock->my_port = auth_port;
2806 sock->clients = clients_parse_section(config);
2807 if (!sock->clients) {
2808 cf_log_err_cs(config,
2809 "Failed to find any clients for this listen section");
2814 if (listen_bind(this) < 0) {
2816 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
2820 auth_port = sock->my_port; /* may have been updated in listen_bind */
2822 cs = cf_section_sub_find_name2(config, "server",
2824 if (cs) this->server = mainconfig.name;
2828 last = &(this->next);
2834 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
2837 #ifdef WITH_ACCOUNTING
2839 * Open Accounting Socket.
2841 * If we haven't already gotten acct_port from
2842 * /etc/services, then make it auth_port + 1.
2844 this = listen_alloc(config, RAD_LISTEN_ACCT);
2848 * Create the accounting socket.
2850 * The accounting port is always the
2851 * authentication port + 1
2853 sock->my_ipaddr = server_ipaddr;
2854 sock->my_port = auth_port + 1;
2856 sock->clients = clients_parse_section(config);
2857 if (!sock->clients) {
2858 cf_log_err_cs(config,
2859 "Failed to find any clients for this listen section");
2863 if (listen_bind(this) < 0) {
2866 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
2871 cs = cf_section_sub_find_name2(config, "server",
2873 if (cs) this->server = mainconfig.name;
2877 last = &(this->next);
2882 * They specified an IP on the command-line, ignore
2883 * all listen sections except the one in '-n'.
2885 if (mainconfig.myip.af != AF_UNSPEC) {
2886 CONF_SECTION *subcs;
2887 const char *name2 = cf_section_name2(cs);
2889 cs = cf_section_sub_find_name2(config, "server",
2891 if (!cs) goto add_sockets;
2894 * Should really abstract this code...
2896 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2898 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2899 this = listen_parse(subcs, name2);
2906 last = &(this->next);
2907 } /* loop over "listen" directives in server <foo> */
2913 * Walk through the "listen" sections, if they exist.
2915 for (cs = cf_subsection_find_next(config, NULL, "listen");
2917 cs = cf_subsection_find_next(config, cs, "listen")) {
2918 this = listen_parse(cs, NULL);
2925 last = &(this->next);
2929 * Check virtual servers for "listen" sections, too.
2931 * FIXME: Move to virtual server init?
2933 for (cs = cf_subsection_find_next(config, NULL, "server");
2935 cs = cf_subsection_find_next(config, cs, "server")) {
2936 CONF_SECTION *subcs;
2937 const char *name2 = cf_section_name2(cs);
2939 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2941 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2942 this = listen_parse(subcs, name2);
2949 last = &(this->next);
2950 } /* loop over "listen" directives in virtual servers */
2951 } /* loop over virtual servers */
2955 * No sockets to receive packets, this is an error.
2956 * proxying is pointless.
2959 radlog(L_ERR, "The server is not configured to listen on any ports. Cannot start.");
2964 * Print out which sockets we're listening on, and
2965 * add them to the event list.
2967 for (this = *head; this != NULL; this = this->next) {
2969 if (this->type == RAD_LISTEN_PROXY) {
2976 if (!spawn_flag && this->tls) {
2977 cf_log_err_cs(this->cs, "Threading must be enabled for TLS sockets to function properly.");
2978 cf_log_err_cs(this->cs, "You probably need to do 'radiusd -fxx -l stdout' for debugging");
2982 if (!check_config) event_new_fd(this);
2986 * If we're proxying requests, open the proxy FD.
2987 * Otherwise, don't do anything.
2990 if ((mainconfig.proxy_requests == TRUE) &&
2992 (*head != NULL) && !defined_proxy) {
2993 listen_socket_t *sock = NULL;
2997 memset(&home, 0, sizeof(home));
3002 home.proto = IPPROTO_UDP;
3003 home.src_ipaddr = server_ipaddr;
3006 * Find the first authentication port,
3009 for (this = *head; this != NULL; this = this->next) {
3010 if (this->type == RAD_LISTEN_AUTH) {
3013 if (is_loopback(&sock->my_ipaddr)) continue;
3015 if (home.src_ipaddr.af == AF_UNSPEC) {
3016 home.src_ipaddr = sock->my_ipaddr;
3018 port = sock->my_port + 2;
3022 if (this->type == RAD_LISTEN_ACCT) {
3025 if (is_loopback(&sock->my_ipaddr)) continue;
3027 if (home.src_ipaddr.af == AF_UNSPEC) {
3028 home.src_ipaddr = sock->my_ipaddr;
3030 port = sock->my_port + 1;
3037 * Address is still unspecified, use IPv4.
3039 if (home.src_ipaddr.af == AF_UNSPEC) {
3040 home.src_ipaddr.af = AF_INET;
3041 /* everything else is already set to zero */
3044 home.ipaddr.af = home.src_ipaddr.af;
3045 /* everything else is already set to zero */
3047 this = proxy_new_listener(&home, port);
3053 if (!event_new_fd(this)) {
3062 * Haven't defined any sockets. Die.
3064 if (!*head) return -1;
3066 xlat_register("listen", xlat_listen, NULL);
3072 * Free a linked list of listeners;
3074 void listen_free(rad_listen_t **head)
3078 if (!head || !*head) return;
3082 rad_listen_t *next = this->next;
3091 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
3096 for (this = mainconfig.listen; this != NULL; this = this->next) {
3097 listen_socket_t *sock;
3099 if ((this->type != RAD_LISTEN_AUTH)
3100 #ifdef WITH_ACCOUNTING
3101 && (this->type != RAD_LISTEN_ACCT)
3107 if ((sock->my_port == port) &&
3108 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
3109 return sock->clients;
3117 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port, int proto)
3121 for (this = mainconfig.listen; this != NULL; this = this->next) {
3122 listen_socket_t *sock;
3126 if (sock->my_port != port) continue;
3127 if (sock->proto != proto) continue;
3128 if (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) != 0) continue;
3134 * Failed to find a specific one. Find INADDR_ANY
3136 for (this = mainconfig.listen; this != NULL; this = this->next) {
3137 listen_socket_t *sock;
3141 if (sock->my_port != port) continue;
3142 if (sock->proto != proto) continue;
3143 if (!fr_inaddr_any(&sock->my_ipaddr)) continue;