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>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29 #include <freeradius-devel/rad_assert.h>
30 #include <freeradius-devel/vqp.h>
31 #include <freeradius-devel/dhcp.h>
33 #include <freeradius-devel/vmps.h>
34 #include <freeradius-devel/detail.h>
36 #ifdef HAVE_SYS_RESOURCE_H
37 #include <sys/resource.h>
50 * We'll use this below.
52 typedef int (*rad_listen_parse_t)(CONF_SECTION *, rad_listen_t *);
53 typedef void (*rad_listen_free_t)(rad_listen_t *);
55 typedef struct rad_listen_master_t {
56 rad_listen_parse_t parse;
57 rad_listen_free_t free;
58 rad_listen_recv_t recv;
59 rad_listen_send_t send;
60 rad_listen_print_t print;
61 rad_listen_encode_t encode;
62 rad_listen_decode_t decode;
63 } rad_listen_master_t;
65 typedef struct listen_socket_t {
71 #ifdef SO_BINDTODEVICE
72 const char *interface;
74 RADCLIENT_LIST *clients;
78 * Find a per-socket client.
80 RADCLIENT *client_listener_find(const rad_listen_t *listener,
81 const fr_ipaddr_t *ipaddr)
83 #ifdef WITH_DYNAMIC_CLIENTS
86 listen_socket_t *sock;
88 RADCLIENT *client, *created;
90 RADCLIENT_LIST *clients;
92 rad_assert(listener != NULL);
93 rad_assert(ipaddr != NULL);
95 rad_assert((listener->type == RAD_LISTEN_AUTH) ||
97 (listener->type == RAD_LISTEN_NONE) ||
99 #ifdef WITH_ACCOUNTING
100 (listener->type == RAD_LISTEN_ACCT) ||
103 (listener->type == RAD_LISTEN_VQP) ||
105 (listener->type == RAD_LISTEN_DHCP));
107 clients = ((listen_socket_t *)listener->data)->clients;
110 * This HAS to have been initialized previously.
112 rad_assert(clients != NULL);
114 #ifdef WITH_DYNAMIC_CLIENTS
115 client = client_find(clients, ipaddr);
116 if (!client) return NULL;
119 * No server defined, and it's not dynamic. Return it.
121 if (!client->client_server && !client->dynamic) return client;
126 * It's a dynamically generated client, check it.
128 if (client->dynamic) {
130 * Lives forever. Return it.
132 if (client->lifetime == 0) return client;
135 * Rate-limit the deletion of known clients.
136 * This makes them last a little longer, but
137 * prevents the server from melting down if (say)
138 * 10k clients all expire at once.
140 if (now == client->last_new_client) return client;
143 * It's not dead yet. Return it.
145 if ((client->created + client->lifetime) > now) return client;
148 * It's dead, Jim. Delete it.
150 client_delete(clients, client);
153 * Go find the enclosing network again.
155 client = client_find(clients, ipaddr);
160 if (!client) return NULL;
161 if (!client->client_server) return NULL;
164 * At this point, 'client' is the enclosing
165 * network that configures where dynamic clients
168 rad_assert(client->dynamic == 0);
171 * The IP is unknown, so we've found an enclosing
172 * network. Enable DoS protection. We only
173 * allow one new client per second. Known
174 * clients aren't subject to this restriction.
176 if (now == client->last_new_client) return NULL;
179 client->last_new_client = now;
181 request = request_alloc();
182 if (!request) return NULL;
184 request->listener = listener;
185 request->client = client;
186 request->packet = rad_alloc(0);
187 if (!request->packet) {
188 request_free(&request);
191 request->reply = rad_alloc(0);
192 if (!request->reply) {
193 request_free(&request);
196 request->packet->timestamp = request->timestamp;
198 request->priority = listener->type;
199 request->server = client->client_server;
200 request->root = &mainconfig;
203 * Run a fake request through the given virtual server.
204 * Look for FreeRADIUS-Client-IP-Address
205 * FreeRADIUS-Client-Secret
208 * and create the RADCLIENT structure from that.
211 sock = listener->data;
212 request->packet->sockfd = listener->fd;
213 request->packet->src_ipaddr = *ipaddr;
214 request->packet->src_port = 0; /* who cares... */
215 request->packet->dst_ipaddr = sock->ipaddr;
216 request->packet->dst_port = sock->port;
218 request->reply->sockfd = request->packet->sockfd;
219 request->reply->dst_ipaddr = request->packet->src_ipaddr;
220 request->reply->src_ipaddr = request->packet->dst_ipaddr;
221 request->reply->dst_port = request->packet->src_port;
222 request->reply->src_port = request->packet->dst_port;
223 request->reply->id = request->packet->id;
224 request->reply->code = 0; /* UNKNOWN code */
227 DEBUG("server %s {", request->server);
229 rcode = module_authorize(0, request);
231 DEBUG("} # server %s", request->server);
233 if (rcode != RLM_MODULE_OK) {
234 request_free(&request);
239 * If the client was updated by rlm_dynamic_clients,
240 * don't create the client from attribute-value pairs.
242 if (request->client == client) {
243 created = client_create(clients, request);
245 created = request->client;
248 * This frees the client if it isn't valid.
250 if (!client_validate(clients, request, created)) {
254 request_free(&request);
256 return created; /* may be NULL */
258 return client_find(clients, ipaddr);
262 static int listen_bind(rad_listen_t *this);
266 * Process and reply to a server-status request.
267 * Like rad_authenticate and rad_accounting this should
268 * live in it's own file but it's so small we don't bother.
270 static int rad_status_server(REQUEST *request)
272 int rcode = RLM_MODULE_OK;
275 switch (request->listener->type) {
277 case RAD_LISTEN_NONE:
279 case RAD_LISTEN_AUTH:
280 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
282 rcode = module_authorize(dval->value, request);
284 rcode = RLM_MODULE_OK;
289 case RLM_MODULE_UPDATED:
290 request->reply->code = PW_AUTHENTICATION_ACK;
293 case RLM_MODULE_FAIL:
294 case RLM_MODULE_HANDLED:
295 request->reply->code = 0; /* don't reply */
299 case RLM_MODULE_REJECT:
300 request->reply->code = PW_AUTHENTICATION_REJECT;
305 #ifdef WITH_ACCOUNTING
306 case RAD_LISTEN_ACCT:
307 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
309 rcode = module_accounting(dval->value, request);
311 rcode = RLM_MODULE_OK;
316 case RLM_MODULE_UPDATED:
317 request->reply->code = PW_ACCOUNTING_RESPONSE;
321 request->reply->code = 0; /* don't reply */
333 * Full statistics are available only on a statistics
336 if (request->listener->type == RAD_LISTEN_NONE) {
337 request_stats_reply(request);
345 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
348 listen_socket_t *sock = this->data;
351 switch (this->type) {
353 case RAD_LISTEN_NONE: /* what a hack... */
358 case RAD_LISTEN_AUTH:
359 name = "authentication";
362 #ifdef WITH_ACCOUNTING
363 case RAD_LISTEN_ACCT:
369 case RAD_LISTEN_PROXY:
381 case RAD_LISTEN_DHCP:
391 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
392 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
396 #ifdef SO_BINDTODEVICE
397 if (sock->interface) {
398 ADDSTRING(" interface ");
399 ADDSTRING(sock->interface);
403 ADDSTRING(" address ");
405 if ((sock->ipaddr.af == AF_INET) &&
406 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
407 strlcpy(buffer, "*", bufsize);
409 ip_ntoh(&sock->ipaddr, buffer, bufsize);
414 snprintf(buffer, bufsize, "%d", sock->port);
418 ADDSTRING(" as server ");
419 ADDSTRING(this->server);
430 * Parse an authentication or accounting socket.
432 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
437 listen_socket_t *sock = this->data;
438 char *section_name = NULL;
439 CONF_SECTION *client_cs, *parentcs;
444 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
445 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
446 &ipaddr.ipaddr.ip4addr, NULL);
447 if (rcode < 0) return -1;
449 if (rcode == 0) { /* successfully parsed IPv4 */
452 } else { /* maybe IPv6? */
453 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
454 &ipaddr.ipaddr.ip6addr, NULL);
455 if (rcode < 0) return -1;
458 cf_log_err(cf_sectiontoitem(cs),
459 "No address specified in listen section");
462 ipaddr.af = AF_INET6;
465 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
467 if (rcode < 0) return -1;
469 if ((listen_port < 0) || (listen_port > 65500)) {
470 cf_log_err(cf_sectiontoitem(cs),
471 "Invalid value for \"port\"");
475 sock->ipaddr = ipaddr;
476 sock->port = listen_port;
479 * If we can bind to interfaces, do so,
482 if (cf_pair_find(cs, "interface")) {
483 #ifndef SO_BINDTODEVICE
484 cf_log_err(cf_sectiontoitem(cs),
485 "System does not support binding to interfaces. Delete this line from the configuration file.");
489 CONF_PAIR *cp = cf_pair_find(cs, "interface");
491 rad_assert(cp != NULL);
492 value = cf_pair_value(cp);
494 cf_log_err(cf_sectiontoitem(cs),
495 "No interface name given");
498 sock->interface = value;
503 * And bind it to the port.
505 if (listen_bind(this) < 0) {
507 cf_log_err(cf_sectiontoitem(cs),
508 "Error binding to port for %s port %d",
509 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
516 * Proxy sockets don't have clients.
518 if (this->type == RAD_LISTEN_PROXY) return 0;
522 * The more specific configurations are preferred to more
526 parentcs = cf_top_section(cs);
527 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
528 §ion_name, NULL);
529 if (rcode < 0) return -1; /* bad string */
532 * Explicit list given: use it.
534 client_cs = cf_section_sub_find_name2(parentcs,
538 client_cs = cf_section_find(section_name);
541 cf_log_err(cf_sectiontoitem(cs),
542 "Failed to find clients %s {...}",
548 } /* else there was no "clients = " entry. */
551 CONF_SECTION *server_cs;
553 server_cs = cf_section_sub_find_name2(parentcs,
557 * Found a "server foo" section. If there are clients
561 (cf_section_sub_find(server_cs, "client") != NULL)) {
562 client_cs = server_cs;
567 * Still nothing. Look for global clients.
569 if (!client_cs) client_cs = parentcs;
571 sock->clients = clients_parse_section(client_cs);
572 if (!sock->clients) {
573 cf_log_err(cf_sectiontoitem(cs),
574 "Failed to load clients for this listen section");
582 * Send an authentication response packet
584 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
586 rad_assert(request->listener == listener);
587 rad_assert(listener->send == auth_socket_send);
589 return rad_send(request->reply, request->packet,
590 request->client->secret);
594 #ifdef WITH_ACCOUNTING
596 * Send an accounting response packet (or not)
598 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
600 rad_assert(request->listener == listener);
601 rad_assert(listener->send == acct_socket_send);
604 * Accounting reject's are silently dropped.
606 * We do it here to avoid polluting the rest of the
607 * code with this knowledge
609 if (request->reply->code == 0) return 0;
611 return rad_send(request->reply, request->packet,
612 request->client->secret);
618 * Send a packet to a home server.
620 * FIXME: have different code for proxy auth & acct!
622 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
624 listen_socket_t *sock = listener->data;
626 rad_assert(request->proxy_listener == listener);
627 rad_assert(listener->send == proxy_socket_send);
629 request->proxy->src_ipaddr = sock->ipaddr;
630 request->proxy->src_port = sock->port;
632 return rad_send(request->proxy, request->packet,
633 request->home_server->secret);
639 * Check if an incoming request is "ok"
641 * It takes packets, not requests. It sees if the packet looks
642 * OK. If so, it does a number of sanity checks on it.
644 static int stats_socket_recv(rad_listen_t *listener,
645 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
649 RADIUS_PACKET *packet;
652 fr_ipaddr_t src_ipaddr;
654 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
655 if (rcode < 0) return 0;
657 RAD_STATS_TYPE_INC(listener, total_requests);
659 if (rcode < 20) { /* AUTH_HDR_LEN */
660 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
664 if ((client = client_listener_find(listener,
665 &src_ipaddr)) == NULL) {
666 rad_recv_discard(listener->fd);
667 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
669 if (debug_flag > 0) {
672 listener->print(listener, name, sizeof(name));
675 * This is debugging rather than logging, so that
676 * DoS attacks don't affect us.
678 DEBUG("Ignoring request to %s from unknown client %s port %d",
680 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
681 buffer, sizeof(buffer)), src_port);
688 * We only understand Status-Server on this socket.
690 if (code != PW_STATUS_SERVER) {
691 DEBUG("Ignoring packet code %d sent to Status-Server port",
693 rad_recv_discard(listener->fd);
694 RAD_STATS_TYPE_INC(listener, total_unknown_types);
695 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
700 * Now that we've sanity checked everything, receive the
703 packet = rad_recv(listener->fd, 1); /* require message authenticator */
705 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
706 DEBUG("%s", fr_strerror());
710 if (!received_request(listener, packet, prequest, client)) {
711 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
712 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
717 *pfun = rad_status_server;
724 * Check if an incoming request is "ok"
726 * It takes packets, not requests. It sees if the packet looks
727 * OK. If so, it does a number of sanity checks on it.
729 static int auth_socket_recv(rad_listen_t *listener,
730 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
734 RADIUS_PACKET *packet;
735 RAD_REQUEST_FUNP fun = NULL;
738 fr_ipaddr_t src_ipaddr;
740 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
741 if (rcode < 0) return 0;
743 RAD_STATS_TYPE_INC(listener, total_requests);
745 if (rcode < 20) { /* AUTH_HDR_LEN */
746 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
750 if ((client = client_listener_find(listener,
751 &src_ipaddr)) == NULL) {
752 rad_recv_discard(listener->fd);
753 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
755 if (debug_flag > 0) {
758 listener->print(listener, name, sizeof(name));
761 * This is debugging rather than logging, so that
762 * DoS attacks don't affect us.
764 DEBUG("Ignoring request to %s from unknown client %s port %d",
766 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
767 buffer, sizeof(buffer)), src_port);
774 * Some sanity checks, based on the packet code.
777 case PW_AUTHENTICATION_REQUEST:
778 RAD_STATS_CLIENT_INC(listener, client, total_requests);
779 fun = rad_authenticate;
782 case PW_STATUS_SERVER:
783 if (!mainconfig.status_server) {
784 rad_recv_discard(listener->fd);
785 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
786 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
787 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
790 fun = rad_status_server;
794 rad_recv_discard(listener->fd);
795 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
796 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
798 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
799 code, client->shortname, src_port);
802 } /* switch over packet types */
805 * Now that we've sanity checked everything, receive the
808 packet = rad_recv(listener->fd, client->message_authenticator);
810 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
811 DEBUG("%s", fr_strerror());
815 if (!received_request(listener, packet, prequest, client)) {
816 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
817 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
827 #ifdef WITH_ACCOUNTING
829 * Receive packets from an accounting socket
831 static int acct_socket_recv(rad_listen_t *listener,
832 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
836 RADIUS_PACKET *packet;
837 RAD_REQUEST_FUNP fun = NULL;
840 fr_ipaddr_t src_ipaddr;
842 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
843 if (rcode < 0) return 0;
845 RAD_STATS_TYPE_INC(listener, total_requests);
847 if (rcode < 20) { /* AUTH_HDR_LEN */
848 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
852 if ((client = client_listener_find(listener,
853 &src_ipaddr)) == NULL) {
854 rad_recv_discard(listener->fd);
855 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
858 * This is debugging rather than logging, so that
859 * DoS attacks don't affect us.
861 if (debug_flag > 0) {
864 listener->print(listener, name, sizeof(name));
866 DEBUG("Ignoring request to %s from unknown client %s port %d",
868 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
869 buffer, sizeof(buffer)), src_port);
876 * Some sanity checks, based on the packet code.
879 case PW_ACCOUNTING_REQUEST:
880 RAD_STATS_CLIENT_INC(listener, client, total_requests);
881 fun = rad_accounting;
884 case PW_STATUS_SERVER:
885 if (!mainconfig.status_server) {
886 rad_recv_discard(listener->fd);
887 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
888 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
890 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
893 fun = rad_status_server;
897 rad_recv_discard(listener->fd);
898 RAD_STATS_TYPE_INC(listener, total_unknown_types);
899 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
901 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
902 code, client->shortname, src_port);
904 } /* switch over packet types */
907 * Now that we've sanity checked everything, receive the
910 packet = rad_recv(listener->fd, 0);
912 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
913 radlog(L_ERR, "%s", fr_strerror());
918 * There can be no duplicate accounting packets.
920 if (!received_request(listener, packet, prequest, client)) {
921 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
922 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
934 * Recieve packets from a proxy socket.
936 static int proxy_socket_recv(rad_listen_t *listener,
937 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
940 RADIUS_PACKET *packet;
941 RAD_REQUEST_FUNP fun = NULL;
944 packet = rad_recv(listener->fd, 0);
946 radlog(L_ERR, "%s", fr_strerror());
951 * FIXME: Client MIB updates?
953 switch(packet->code) {
954 case PW_AUTHENTICATION_ACK:
955 case PW_ACCESS_CHALLENGE:
956 case PW_AUTHENTICATION_REJECT:
957 fun = rad_authenticate;
960 #ifdef WITH_ACCOUNTING
961 case PW_ACCOUNTING_RESPONSE:
962 fun = rad_accounting;
968 * FIXME: Update MIB for packet types?
970 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
971 "from home server %s port %d - ID %d : IGNORED",
973 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
974 packet->src_port, packet->id);
979 request = received_proxy_response(packet);
984 rad_assert(fun != NULL);
993 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
995 if (!request->reply->code) return 0;
997 rad_encode(request->reply, request->packet,
998 request->client->secret);
999 rad_sign(request->reply, request->packet,
1000 request->client->secret);
1006 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1008 if (rad_verify(request->packet, NULL,
1009 request->client->secret) < 0) {
1013 return rad_decode(request->packet, NULL,
1014 request->client->secret);
1018 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1020 rad_encode(request->proxy, NULL, request->home_server->secret);
1021 rad_sign(request->proxy, NULL, request->home_server->secret);
1027 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1029 if (rad_verify(request->proxy_reply, request->proxy,
1030 request->home_server->secret) < 0) {
1034 return rad_decode(request->proxy_reply, request->proxy,
1035 request->home_server->secret);
1041 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1043 { common_socket_parse, NULL,
1044 stats_socket_recv, auth_socket_send,
1045 socket_print, client_socket_encode, client_socket_decode },
1047 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1052 { common_socket_parse, NULL,
1053 proxy_socket_recv, proxy_socket_send,
1054 socket_print, proxy_socket_encode, proxy_socket_decode },
1056 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1059 /* authentication */
1060 { common_socket_parse, NULL,
1061 auth_socket_recv, auth_socket_send,
1062 socket_print, client_socket_encode, client_socket_decode },
1064 #ifdef WITH_ACCOUNTING
1066 { common_socket_parse, NULL,
1067 acct_socket_recv, acct_socket_send,
1068 socket_print, client_socket_encode, client_socket_decode},
1070 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1075 { detail_parse, detail_free,
1076 detail_recv, detail_send,
1077 detail_print, detail_encode, detail_decode },
1079 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1083 /* vlan query protocol */
1084 { common_socket_parse, NULL,
1085 vqp_socket_recv, vqp_socket_send,
1086 socket_print, vqp_socket_encode, vqp_socket_decode },
1088 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1092 /* dhcp query protocol */
1093 { dhcp_socket_parse, NULL,
1094 dhcp_socket_recv, dhcp_socket_send,
1095 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1097 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1105 * Binds a listener to a socket.
1107 static int listen_bind(rad_listen_t *this)
1109 struct sockaddr_storage salocal;
1111 listen_socket_t *sock = this->data;
1114 * If the port is zero, then it means the appropriate
1115 * thing from /etc/services.
1117 if (sock->port == 0) {
1118 struct servent *svp;
1120 switch (this->type) {
1121 case RAD_LISTEN_AUTH:
1122 svp = getservbyname ("radius", "udp");
1124 sock->port = ntohs(svp->s_port);
1126 sock->port = PW_AUTH_UDP_PORT;
1130 #ifdef WITH_ACCOUNTING
1131 case RAD_LISTEN_ACCT:
1132 svp = getservbyname ("radacct", "udp");
1134 sock->port = ntohs(svp->s_port);
1136 sock->port = PW_ACCT_UDP_PORT;
1142 case RAD_LISTEN_PROXY:
1148 case RAD_LISTEN_VQP:
1154 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1160 * Copy fr_socket() here, as we may need to bind to a device.
1162 this->fd = socket(sock->ipaddr.af, SOCK_DGRAM, 0);
1164 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
1168 #ifdef SO_BINDTODEVICE
1170 * Bind to a device BEFORE touching IP addresses.
1172 if (sock->interface) {
1174 strcpy(ifreq.ifr_name, sock->interface);
1176 if (setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1177 (char *)&ifreq, sizeof(ifreq)) < 0) {
1179 radlog(L_ERR, "Failed opening to interface %s: %s",
1180 sock->interface, strerror(errno));
1182 } /* else it worked. */
1186 #ifdef WITH_UDPFROMTO
1188 * Initialize udpfromto for all sockets.
1190 if (udpfromto_init(this->fd) != 0) {
1197 * Set up sockaddr stuff.
1199 if (!fr_ipaddr2sockaddr(&sock->ipaddr, sock->port, &salocal, &salen)) {
1204 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1205 if (sock->ipaddr.af == AF_INET6) {
1207 * Listening on '::' does NOT get you IPv4 to
1208 * IPv6 mapping. You've got to listen on an IPv4
1209 * address, too. This makes the rest of the server
1210 * design a little simpler.
1214 if (IN6_IS_ADDR_UNSPECIFIED(&sock->ipaddr.ipaddr.ip6addr)) {
1217 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
1218 (char *)&on, sizeof(on));
1220 #endif /* IPV6_V6ONLY */
1221 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
1224 if (bind(this->fd, (struct sockaddr *) &salocal, salen) < 0) {
1226 radlog(L_ERR, "Failed binding to socket: %s\n",
1232 * FreeBSD jail issues. We bind to 0.0.0.0, but the
1233 * kernel instead binds us to a 1.2.3.4. If this
1234 * happens, notice, and remember our real IP.
1237 struct sockaddr_storage src;
1238 socklen_t sizeof_src = sizeof(src);
1240 memset(&src, 0, sizeof_src);
1241 if (getsockname(this->fd, (struct sockaddr *) &src,
1243 radlog(L_ERR, "Failed getting socket name: %s",
1248 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
1249 &sock->ipaddr, &sock->port)) {
1250 radlog(L_ERR, "Socket has unsupported address family");
1259 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
1260 radlog(L_ERR, "Failure getting socket flags: %s)\n",
1265 flags |= O_NONBLOCK;
1266 if( fcntl(this->fd, F_SETFL, flags) < 0) {
1267 radlog(L_ERR, "Failure setting socket flags: %s)\n",
1279 * Allocate & initialize a new listener.
1281 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
1285 this = rad_malloc(sizeof(*this));
1286 memset(this, 0, sizeof(*this));
1289 this->recv = master_listen[this->type].recv;
1290 this->send = master_listen[this->type].send;
1291 this->print = master_listen[this->type].print;
1292 this->encode = master_listen[this->type].encode;
1293 this->decode = master_listen[this->type].decode;
1297 case RAD_LISTEN_NONE:
1299 case RAD_LISTEN_AUTH:
1300 case RAD_LISTEN_ACCT:
1301 case RAD_LISTEN_PROXY:
1302 case RAD_LISTEN_VQP:
1303 case RAD_LISTEN_DHCP:
1304 this->data = rad_malloc(sizeof(listen_socket_t));
1305 memset(this->data, 0, sizeof(listen_socket_t));
1309 case RAD_LISTEN_DETAIL:
1315 rad_assert("Unsupported option!" == NULL);
1325 * Externally visible function for creating a new proxy LISTENER.
1327 * For now, don't take ipaddr or port.
1329 * Not thread-safe, but all calls to it are protected by the
1330 * proxy mutex in request_list.c
1332 rad_listen_t *proxy_new_listener()
1334 int last_proxy_port, port;
1335 rad_listen_t *this, *tmp, **last;
1336 listen_socket_t *sock, *old;
1338 this = listen_alloc(RAD_LISTEN_PROXY);
1341 * Find an existing proxy socket to copy.
1343 * FIXME: Make it per-realm, or per-home server!
1345 last_proxy_port = 0;
1347 last = &mainconfig.listen;
1348 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1349 if (tmp->type == RAD_LISTEN_PROXY) {
1351 if (sock->port > last_proxy_port) {
1352 last_proxy_port = sock->port + 1;
1354 if (!old) old = sock;
1357 last = &(tmp->next);
1362 return NULL; /* This is a serious error. */
1366 * FIXME: find a new IP address to listen on?
1368 * This could likely be done in the "home server"
1369 * configuration, to have per-home-server source IP's.
1372 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1375 * Keep going until we find an unused port.
1377 for (port = last_proxy_port; port < 64000; port++) {
1379 if (listen_bind(this) == 0) {
1381 * Add the new listener to the list of
1394 static const FR_NAME_NUMBER listen_compare[] = {
1396 { "status", RAD_LISTEN_NONE },
1398 { "auth", RAD_LISTEN_AUTH },
1399 #ifdef WITH_ACCOUNTING
1400 { "acct", RAD_LISTEN_ACCT },
1403 { "detail", RAD_LISTEN_DETAIL },
1406 { "proxy", RAD_LISTEN_PROXY },
1409 { "vmps", RAD_LISTEN_VQP },
1412 { "dhcp", RAD_LISTEN_DHCP },
1418 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1426 cf_log_info(cs, "listen {");
1428 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1430 if (rcode < 0) return NULL;
1433 cf_log_err(cf_sectiontoitem(cs),
1434 "No type specified in listen section");
1438 type = fr_str2int(listen_compare, listen_type, -1);
1440 cf_log_err(cf_sectiontoitem(cs),
1441 "Invalid type \"%s\" in listen section.",
1449 * Allow listen sections in the default config to
1450 * refer to a server.
1453 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1455 if (rcode == 1) { /* compatiblity with 2.0-pre */
1456 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1459 if (rcode < 0) return NULL;
1463 * Set up cross-type data.
1465 this = listen_alloc(type);
1466 this->server = server;
1470 * Call per-type parser.
1472 if (master_listen[type].parse(cs, this) < 0) {
1477 cf_log_info(cs, "}");
1483 * Generate a list of listeners. Takes an input list of
1484 * listeners, too, so we don't close sockets with waiting packets.
1486 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1488 int override = FALSE;
1490 CONF_SECTION *cs = NULL;
1491 rad_listen_t **last;
1493 fr_ipaddr_t server_ipaddr;
1496 int defined_proxy = 0;
1500 * We shouldn't be called with a pre-existing list.
1502 rad_assert(head && (*head == NULL));
1505 server_ipaddr.af = AF_UNSPEC;
1508 * If the port is specified on the command-line,
1509 * it over-rides the configuration file.
1511 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1513 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1516 * If the IP address was configured on the command-line,
1517 * use that as the "bind_address"
1519 if (mainconfig.myip.af != AF_UNSPEC) {
1520 memcpy(&server_ipaddr, &mainconfig.myip,
1521 sizeof(server_ipaddr));
1527 * Else look for bind_address and/or listen sections.
1529 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1530 rcode = cf_item_parse(config, "bind_address",
1532 &server_ipaddr.ipaddr.ip4addr, NULL);
1533 if (rcode < 0) return -1; /* error parsing it */
1535 if (rcode == 0) { /* successfully parsed IPv4 */
1536 listen_socket_t *sock;
1537 server_ipaddr.af = AF_INET;
1539 radlog(L_INFO, "WARNING: The directive 'bind_adress' is deprecated, and will be removed in future versions of FreeRADIUS. Please edit the configuration files to use the directive 'listen'.");
1543 if (strcmp(progname, "vmpsd") == 0) {
1544 this = listen_alloc(RAD_LISTEN_VQP);
1545 if (!auth_port) auth_port = 1589;
1548 this = listen_alloc(RAD_LISTEN_AUTH);
1552 sock->ipaddr = server_ipaddr;
1553 sock->port = auth_port;
1555 sock->clients = clients_parse_section(config);
1556 if (!sock->clients) {
1557 cf_log_err(cf_sectiontoitem(config),
1558 "Failed to find any clients for this listen section");
1562 if (listen_bind(this) < 0) {
1565 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1568 auth_port = sock->port; /* may have been updated in listen_bind */
1570 cs = cf_section_sub_find_name2(config, "server",
1572 if (cs) this->server = mainconfig.name;
1576 last = &(this->next);
1582 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
1585 #ifdef WITH_ACCOUNTING
1587 * Open Accounting Socket.
1589 * If we haven't already gotten acct_port from
1590 * /etc/services, then make it auth_port + 1.
1592 this = listen_alloc(RAD_LISTEN_ACCT);
1596 * Create the accounting socket.
1598 * The accounting port is always the
1599 * authentication port + 1
1601 sock->ipaddr = server_ipaddr;
1602 sock->port = auth_port + 1;
1604 sock->clients = clients_parse_section(config);
1605 if (!sock->clients) {
1606 cf_log_err(cf_sectiontoitem(config),
1607 "Failed to find any clients for this listen section");
1611 if (listen_bind(this) < 0) {
1614 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1619 cs = cf_section_sub_find_name2(config, "server",
1621 if (cs) this->server = mainconfig.name;
1625 last = &(this->next);
1627 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1628 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1634 * They specified an IP on the command-line, ignore
1635 * all listen sections except the one in '-n'.
1637 if (mainconfig.myip.af != AF_UNSPEC) {
1638 CONF_SECTION *subcs;
1639 const char *name2 = cf_section_name2(cs);
1641 cs = cf_section_sub_find_name2(config, "server",
1643 if (!cs) goto do_proxy;
1646 * Should really abstract this code...
1648 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1650 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1651 this = listen_parse(subcs, name2);
1658 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1662 last = &(this->next);
1663 } /* loop over "listen" directives in server <foo> */
1669 * Walk through the "listen" sections, if they exist.
1671 for (cs = cf_subsection_find_next(config, NULL, "listen");
1673 cs = cf_subsection_find_next(config, cs, "listen")) {
1674 this = listen_parse(cs, NULL);
1681 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1685 last = &(this->next);
1689 * Check virtual servers for "listen" sections, too.
1691 * FIXME: Move to virtual server init?
1693 for (cs = cf_subsection_find_next(config, NULL, "server");
1695 cs = cf_subsection_find_next(config, cs, "server")) {
1696 CONF_SECTION *subcs;
1697 const char *name2 = cf_section_name2(cs);
1699 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1701 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1702 this = listen_parse(subcs, name2);
1709 if (this->type == RAD_LISTEN_PROXY) {
1710 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
1717 last = &(this->next);
1718 } /* loop over "listen" directives in virtual servers */
1719 } /* loop over virtual servers */
1722 * If we're proxying requests, open the proxy FD.
1723 * Otherwise, don't do anything.
1727 if (mainconfig.proxy_requests == TRUE) {
1729 listen_socket_t *sock = NULL;
1732 * No sockets to receive packets, therefore
1733 * proxying is pointless.
1735 if (!*head) return -1;
1737 if (defined_proxy) goto done;
1740 * Find the first authentication port,
1743 for (this = *head; this != NULL; this = this->next) {
1744 if (this->type == RAD_LISTEN_AUTH) {
1746 if (server_ipaddr.af == AF_UNSPEC) {
1747 server_ipaddr = sock->ipaddr;
1749 port = sock->port + 2; /* skip acct port */
1752 if (this->type == RAD_LISTEN_VQP) {
1754 if (server_ipaddr.af == AF_UNSPEC) {
1755 server_ipaddr = sock->ipaddr;
1757 port = sock->port + 1;
1762 if (port < 0) port = 1024 + (fr_rand() & 0x1ff);
1765 * Address is still unspecified, use IPv4.
1767 if (server_ipaddr.af == AF_UNSPEC) {
1768 server_ipaddr.af = AF_INET;
1769 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1772 this = listen_alloc(RAD_LISTEN_PROXY);
1776 * Create the first proxy socket.
1778 sock->ipaddr = server_ipaddr;
1781 * Try to find a proxy port (value doesn't matter)
1783 for (sock->port = port;
1786 if (listen_bind(this) == 0) {
1788 last = &(this->next); /* just in case */
1793 if (sock->port >= 64000) {
1796 radlog(L_ERR, "Failed to open socket for proxying");
1801 done: /* used only in proxy code. */
1808 * Free a linked list of listeners;
1810 void listen_free(rad_listen_t **head)
1814 if (!head || !*head) return;
1818 rad_listen_t *next = this->next;
1821 * Other code may have eaten the FD.
1823 if (this->fd >= 0) close(this->fd);
1825 if (master_listen[this->type].free) {
1826 master_listen[this->type].free(this);
1838 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
1843 for (this = mainconfig.listen; this != NULL; this = this->next) {
1844 listen_socket_t *sock;
1846 if ((this->type != RAD_LISTEN_AUTH) &&
1847 (this->type != RAD_LISTEN_ACCT)) continue;
1851 if ((sock->port == port) &&
1852 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {
1853 return sock->clients;
1860 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
1864 for (this = mainconfig.listen; this != NULL; this = this->next) {
1865 listen_socket_t *sock;
1867 if ((this->type != RAD_LISTEN_AUTH) &&
1868 (this->type != RAD_LISTEN_ACCT)) continue;
1872 if ((sock->port == port) &&
1873 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {