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);
238 created = client_create(clients, request);
240 request_free(&request);
242 return created; /* may be NULL */
244 return client_find(clients, ipaddr);
248 static int listen_bind(rad_listen_t *this);
252 * Process and reply to a server-status request.
253 * Like rad_authenticate and rad_accounting this should
254 * live in it's own file but it's so small we don't bother.
256 static int rad_status_server(REQUEST *request)
258 int rcode = RLM_MODULE_OK;
261 switch (request->listener->type) {
263 case RAD_LISTEN_NONE:
265 case RAD_LISTEN_AUTH:
266 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
268 rcode = module_authorize(dval->value, request);
270 rcode = RLM_MODULE_OK;
275 case RLM_MODULE_UPDATED:
276 request->reply->code = PW_AUTHENTICATION_ACK;
279 case RLM_MODULE_FAIL:
280 case RLM_MODULE_HANDLED:
281 request->reply->code = 0; /* don't reply */
285 case RLM_MODULE_REJECT:
286 request->reply->code = PW_AUTHENTICATION_REJECT;
291 #ifdef WITH_ACCOUNTING
292 case RAD_LISTEN_ACCT:
293 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
295 rcode = module_accounting(dval->value, request);
297 rcode = RLM_MODULE_OK;
302 case RLM_MODULE_UPDATED:
303 request->reply->code = PW_ACCOUNTING_RESPONSE;
307 request->reply->code = 0; /* don't reply */
318 request_stats_reply(request);
325 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
328 listen_socket_t *sock = this->data;
331 switch (this->type) {
333 case RAD_LISTEN_NONE: /* what a hack... */
338 case RAD_LISTEN_AUTH:
339 name = "authentication";
342 #ifdef WITH_ACCOUNTING
343 case RAD_LISTEN_ACCT:
349 case RAD_LISTEN_PROXY:
361 case RAD_LISTEN_DHCP:
371 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
372 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
376 #ifdef SO_BINDTODEVICE
377 if (sock->interface) {
378 ADDSTRING(" interface ");
379 ADDSTRING(sock->interface);
383 ADDSTRING(" address ");
385 if ((sock->ipaddr.af == AF_INET) &&
386 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
387 strlcpy(buffer, "*", bufsize);
389 ip_ntoh(&sock->ipaddr, buffer, bufsize);
394 snprintf(buffer, bufsize, "%d", sock->port);
398 ADDSTRING(" as server ");
399 ADDSTRING(this->server);
410 * Parse an authentication or accounting socket.
412 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
417 listen_socket_t *sock = this->data;
418 char *section_name = NULL;
419 CONF_SECTION *client_cs, *parentcs;
424 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
425 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
426 &ipaddr.ipaddr.ip4addr, NULL);
427 if (rcode < 0) return -1;
429 if (rcode == 0) { /* successfully parsed IPv4 */
432 } else { /* maybe IPv6? */
433 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
434 &ipaddr.ipaddr.ip6addr, NULL);
435 if (rcode < 0) return -1;
438 cf_log_err(cf_sectiontoitem(cs),
439 "No address specified in listen section");
442 ipaddr.af = AF_INET6;
445 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
447 if (rcode < 0) return -1;
449 if ((listen_port < 0) || (listen_port > 65500)) {
450 cf_log_err(cf_sectiontoitem(cs),
451 "Invalid value for \"port\"");
455 sock->ipaddr = ipaddr;
456 sock->port = listen_port;
459 * If we can bind to interfaces, do so,
462 if (cf_pair_find(cs, "interface")) {
463 #ifndef SO_BINDTODEVICE
464 cf_log_err(cf_sectiontoitem(cs),
465 "System does not support binding to interfaces. Delete this line from the configuration file.");
469 CONF_PAIR *cp = cf_pair_find(cs, "interface");
471 rad_assert(cp != NULL);
472 value = cf_pair_value(cp);
474 cf_log_err(cf_sectiontoitem(cs),
475 "No interface name given");
478 sock->interface = value;
483 * And bind it to the port.
485 if (listen_bind(this) < 0) {
487 cf_log_err(cf_sectiontoitem(cs),
488 "Error binding to port for %s port %d",
489 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
496 * Proxy sockets don't have clients.
498 if (this->type == RAD_LISTEN_PROXY) return 0;
502 * The more specific configurations are preferred to more
506 parentcs = cf_top_section(cs);
507 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
508 §ion_name, NULL);
509 if (rcode < 0) return -1; /* bad string */
512 * Explicit list given: use it.
514 client_cs = cf_section_sub_find_name2(parentcs,
518 client_cs = cf_section_find(section_name);
521 cf_log_err(cf_sectiontoitem(cs),
522 "Failed to find clients %s {...}",
528 } /* else there was no "clients = " entry. */
531 CONF_SECTION *server_cs;
533 server_cs = cf_section_sub_find_name2(parentcs,
537 * Found a "server foo" section. If there are clients
541 (cf_section_sub_find(server_cs, "client") != NULL)) {
542 client_cs = server_cs;
547 * Still nothing. Look for global clients.
549 if (!client_cs) client_cs = parentcs;
551 sock->clients = clients_parse_section(client_cs);
552 if (!sock->clients) {
553 cf_log_err(cf_sectiontoitem(cs),
554 "Failed to load clients for this listen section");
562 * Send an authentication response packet
564 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
566 rad_assert(request->listener == listener);
567 rad_assert(listener->send == auth_socket_send);
569 return rad_send(request->reply, request->packet,
570 request->client->secret);
574 #ifdef WITH_ACCOUNTING
576 * Send an accounting response packet (or not)
578 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
580 rad_assert(request->listener == listener);
581 rad_assert(listener->send == acct_socket_send);
584 * Accounting reject's are silently dropped.
586 * We do it here to avoid polluting the rest of the
587 * code with this knowledge
589 if (request->reply->code == 0) return 0;
591 return rad_send(request->reply, request->packet,
592 request->client->secret);
598 * Send a packet to a home server.
600 * FIXME: have different code for proxy auth & acct!
602 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
604 listen_socket_t *sock = listener->data;
606 rad_assert(request->proxy_listener == listener);
607 rad_assert(listener->send == proxy_socket_send);
609 request->proxy->src_ipaddr = sock->ipaddr;
610 request->proxy->src_port = sock->port;
612 return rad_send(request->proxy, request->packet,
613 request->home_server->secret);
619 * Check if an incoming request is "ok"
621 * It takes packets, not requests. It sees if the packet looks
622 * OK. If so, it does a number of sanity checks on it.
624 static int stats_socket_recv(rad_listen_t *listener,
625 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
629 RADIUS_PACKET *packet;
632 fr_ipaddr_t src_ipaddr;
634 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
635 if (rcode < 0) return 0;
637 RAD_STATS_TYPE_INC(listener, total_requests);
639 if (rcode < 20) { /* AUTH_HDR_LEN */
640 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
645 * We only understand Status-Server on this socket.
647 if (code != PW_STATUS_SERVER) {
648 DEBUG("Ignoring packet code %d sent to Status-Server port",
650 rad_recv_discard(listener->fd);
651 RAD_STATS_TYPE_INC(listener, total_unknown_types);
652 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
656 if ((client = client_listener_find(listener,
657 &src_ipaddr)) == NULL) {
658 rad_recv_discard(listener->fd);
659 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
661 if (debug_flag > 0) {
664 listener->print(listener, name, sizeof(name));
667 * This is debugging rather than logging, so that
668 * DoS attacks don't affect us.
670 DEBUG("Ignoring request to %s from unknown client %s port %d",
672 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
673 buffer, sizeof(buffer)), src_port);
680 * Now that we've sanity checked everything, receive the
683 packet = rad_recv(listener->fd, 1); /* require message authenticator */
685 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
686 DEBUG("%s", librad_errstr);
690 if (!received_request(listener, packet, prequest, client)) {
691 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
692 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
697 *pfun = rad_status_server;
704 * Check if an incoming request is "ok"
706 * It takes packets, not requests. It sees if the packet looks
707 * OK. If so, it does a number of sanity checks on it.
709 static int auth_socket_recv(rad_listen_t *listener,
710 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
714 RADIUS_PACKET *packet;
715 RAD_REQUEST_FUNP fun = NULL;
718 fr_ipaddr_t src_ipaddr;
720 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
721 if (rcode < 0) return 0;
723 RAD_STATS_TYPE_INC(listener, total_requests);
725 if (rcode < 20) { /* AUTH_HDR_LEN */
726 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
730 if ((client = client_listener_find(listener,
731 &src_ipaddr)) == NULL) {
732 rad_recv_discard(listener->fd);
733 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
735 if (debug_flag > 0) {
738 listener->print(listener, name, sizeof(name));
741 * This is debugging rather than logging, so that
742 * DoS attacks don't affect us.
744 DEBUG("Ignoring request to %s from unknown client %s port %d",
746 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
747 buffer, sizeof(buffer)), src_port);
754 * Some sanity checks, based on the packet code.
757 case PW_AUTHENTICATION_REQUEST:
758 RAD_STATS_CLIENT_INC(listener, client, total_requests);
759 fun = rad_authenticate;
762 case PW_STATUS_SERVER:
763 if (!mainconfig.status_server) {
764 rad_recv_discard(listener->fd);
765 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
766 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
767 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
770 fun = rad_status_server;
774 rad_recv_discard(listener->fd);
775 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
776 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
778 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
779 code, client->shortname, src_port);
782 } /* switch over packet types */
785 * Now that we've sanity checked everything, receive the
788 packet = rad_recv(listener->fd, client->message_authenticator);
790 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
791 DEBUG("%s", librad_errstr);
795 if (!received_request(listener, packet, prequest, client)) {
796 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
797 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
807 #ifdef WITH_ACCOUNTING
809 * Receive packets from an accounting socket
811 static int acct_socket_recv(rad_listen_t *listener,
812 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
816 RADIUS_PACKET *packet;
817 RAD_REQUEST_FUNP fun = NULL;
820 fr_ipaddr_t src_ipaddr;
822 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
823 if (rcode < 0) return 0;
825 RAD_STATS_TYPE_INC(listener, total_requests);
827 if (rcode < 20) { /* AUTH_HDR_LEN */
828 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
832 if ((client = client_listener_find(listener,
833 &src_ipaddr)) == NULL) {
834 rad_recv_discard(listener->fd);
835 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
838 * This is debugging rather than logging, so that
839 * DoS attacks don't affect us.
841 if (debug_flag > 0) {
844 listener->print(listener, name, sizeof(name));
846 DEBUG("Ignoring request to %s from unknown client %s port %d",
848 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
849 buffer, sizeof(buffer)), src_port);
856 * Some sanity checks, based on the packet code.
859 case PW_ACCOUNTING_REQUEST:
860 RAD_STATS_CLIENT_INC(listener, client, total_requests);
861 fun = rad_accounting;
864 case PW_STATUS_SERVER:
865 if (!mainconfig.status_server) {
866 rad_recv_discard(listener->fd);
867 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
868 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
870 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
873 fun = rad_status_server;
877 rad_recv_discard(listener->fd);
878 RAD_STATS_TYPE_INC(listener, total_unknown_types);
879 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
881 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
882 code, client->shortname, src_port);
884 } /* switch over packet types */
887 * Now that we've sanity checked everything, receive the
890 packet = rad_recv(listener->fd, 0);
892 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
893 radlog(L_ERR, "%s", librad_errstr);
898 * There can be no duplicate accounting packets.
900 if (!received_request(listener, packet, prequest, client)) {
901 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
902 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
914 * Recieve packets from a proxy socket.
916 static int proxy_socket_recv(rad_listen_t *listener,
917 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
920 RADIUS_PACKET *packet;
921 RAD_REQUEST_FUNP fun = NULL;
924 packet = rad_recv(listener->fd, 0);
926 radlog(L_ERR, "%s", librad_errstr);
931 * FIXME: Client MIB updates?
933 switch(packet->code) {
934 case PW_AUTHENTICATION_ACK:
935 case PW_ACCESS_CHALLENGE:
936 case PW_AUTHENTICATION_REJECT:
937 fun = rad_authenticate;
940 #ifdef WITH_ACCOUNTING
941 case PW_ACCOUNTING_RESPONSE:
942 fun = rad_accounting;
948 * FIXME: Update MIB for packet types?
950 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
951 "from home server %s port %d - ID %d : IGNORED",
953 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
954 packet->src_port, packet->id);
959 request = received_proxy_response(packet);
964 rad_assert(fun != NULL);
973 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
975 if (!request->reply->code) return 0;
977 rad_encode(request->reply, request->packet,
978 request->client->secret);
979 rad_sign(request->reply, request->packet,
980 request->client->secret);
986 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
988 if (rad_verify(request->packet, NULL,
989 request->client->secret) < 0) {
993 return rad_decode(request->packet, NULL,
994 request->client->secret);
998 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1000 rad_encode(request->proxy, NULL, request->home_server->secret);
1001 rad_sign(request->proxy, NULL, request->home_server->secret);
1007 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1009 if (rad_verify(request->proxy_reply, request->proxy,
1010 request->home_server->secret) < 0) {
1014 return rad_decode(request->proxy_reply, request->proxy,
1015 request->home_server->secret);
1021 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1023 { common_socket_parse, NULL,
1024 stats_socket_recv, auth_socket_send,
1025 socket_print, client_socket_encode, client_socket_decode },
1027 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1032 { common_socket_parse, NULL,
1033 proxy_socket_recv, proxy_socket_send,
1034 socket_print, proxy_socket_encode, proxy_socket_decode },
1036 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1039 /* authentication */
1040 { common_socket_parse, NULL,
1041 auth_socket_recv, auth_socket_send,
1042 socket_print, client_socket_encode, client_socket_decode },
1044 #ifdef WITH_ACCOUNTING
1046 { common_socket_parse, NULL,
1047 acct_socket_recv, acct_socket_send,
1048 socket_print, client_socket_encode, client_socket_decode},
1050 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1055 { detail_parse, detail_free,
1056 detail_recv, detail_send,
1057 detail_print, detail_encode, detail_decode },
1059 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1063 /* vlan query protocol */
1064 { common_socket_parse, NULL,
1065 vqp_socket_recv, vqp_socket_send,
1066 socket_print, vqp_socket_encode, vqp_socket_decode },
1068 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1072 /* dhcp query protocol */
1073 { dhcp_socket_parse, NULL,
1074 dhcp_socket_recv, dhcp_socket_send,
1075 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1077 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1085 * Binds a listener to a socket.
1087 static int listen_bind(rad_listen_t *this)
1089 struct sockaddr_storage salocal;
1091 listen_socket_t *sock = this->data;
1094 * If the port is zero, then it means the appropriate
1095 * thing from /etc/services.
1097 if (sock->port == 0) {
1098 struct servent *svp;
1100 switch (this->type) {
1101 case RAD_LISTEN_AUTH:
1102 svp = getservbyname ("radius", "udp");
1104 sock->port = ntohs(svp->s_port);
1106 sock->port = PW_AUTH_UDP_PORT;
1110 #ifdef WITH_ACCOUNTING
1111 case RAD_LISTEN_ACCT:
1112 svp = getservbyname ("radacct", "udp");
1114 sock->port = ntohs(svp->s_port);
1116 sock->port = PW_ACCT_UDP_PORT;
1122 case RAD_LISTEN_PROXY:
1128 case RAD_LISTEN_VQP:
1134 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1140 * Copy fr_socket() here, as we may need to bind to a device.
1142 this->fd = socket(sock->ipaddr.af, SOCK_DGRAM, 0);
1144 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
1148 #ifdef SO_BINDTODEVICE
1150 * Bind to a device BEFORE touching IP addresses.
1152 if (sock->interface) {
1154 strcpy(ifreq.ifr_name, sock->interface);
1156 if (setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1157 (char *)&ifreq, sizeof(ifreq)) < 0) {
1159 radlog(L_ERR, "Failed opening to interface %s: %s",
1160 sock->interface, strerror(errno));
1162 } /* else it worked. */
1166 #ifdef WITH_UDPFROMTO
1168 * Initialize udpfromto for all sockets.
1170 if (udpfromto_init(this->fd) != 0) {
1177 * Set up sockaddr stuff.
1179 if (!fr_ipaddr2sockaddr(&sock->ipaddr, sock->port, &salocal, &salen)) {
1184 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1185 if (sock->ipaddr.af == AF_INET6) {
1187 * Listening on '::' does NOT get you IPv4 to
1188 * IPv6 mapping. You've got to listen on an IPv4
1189 * address, too. This makes the rest of the server
1190 * design a little simpler.
1194 if (IN6_IS_ADDR_UNSPECIFIED(&sock->ipaddr.ipaddr.ip6addr)) {
1197 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
1198 (char *)&on, sizeof(on));
1200 #endif /* IPV6_V6ONLY */
1201 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
1204 if (bind(this->fd, (struct sockaddr *) &salocal, salen) < 0) {
1206 radlog(L_ERR, "Failed binding to socket: %s\n",
1212 * FreeBSD jail issues. We bind to 0.0.0.0, but the
1213 * kernel instead binds us to a 1.2.3.4. If this
1214 * happens, notice, and remember our real IP.
1217 struct sockaddr_storage src;
1218 socklen_t sizeof_src = sizeof(src);
1220 memset(&src, 0, sizeof_src);
1221 if (getsockname(this->fd, (struct sockaddr *) &src,
1223 radlog(L_ERR, "Failed getting socket name: %s",
1228 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
1229 &sock->ipaddr, &sock->port)) {
1230 radlog(L_ERR, "Socket has unsupported address family");
1239 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
1240 radlog(L_ERR, "Failure getting socket flags: %s)\n",
1245 flags |= O_NONBLOCK;
1246 if( fcntl(this->fd, F_SETFL, flags) < 0) {
1247 radlog(L_ERR, "Failure setting socket flags: %s)\n",
1259 * Allocate & initialize a new listener.
1261 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
1265 this = rad_malloc(sizeof(*this));
1266 memset(this, 0, sizeof(*this));
1269 this->recv = master_listen[this->type].recv;
1270 this->send = master_listen[this->type].send;
1271 this->print = master_listen[this->type].print;
1272 this->encode = master_listen[this->type].encode;
1273 this->decode = master_listen[this->type].decode;
1277 case RAD_LISTEN_NONE:
1279 case RAD_LISTEN_AUTH:
1280 case RAD_LISTEN_ACCT:
1281 case RAD_LISTEN_PROXY:
1282 case RAD_LISTEN_VQP:
1283 case RAD_LISTEN_DHCP:
1284 this->data = rad_malloc(sizeof(listen_socket_t));
1285 memset(this->data, 0, sizeof(listen_socket_t));
1289 case RAD_LISTEN_DETAIL:
1295 rad_assert("Unsupported option!" == NULL);
1305 * Externally visible function for creating a new proxy LISTENER.
1307 * For now, don't take ipaddr or port.
1309 * Not thread-safe, but all calls to it are protected by the
1310 * proxy mutex in request_list.c
1312 rad_listen_t *proxy_new_listener()
1314 int last_proxy_port, port;
1315 rad_listen_t *this, *tmp, **last;
1316 listen_socket_t *sock, *old;
1318 this = listen_alloc(RAD_LISTEN_PROXY);
1321 * Find an existing proxy socket to copy.
1323 * FIXME: Make it per-realm, or per-home server!
1325 last_proxy_port = 0;
1327 last = &mainconfig.listen;
1328 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1329 if (tmp->type == RAD_LISTEN_PROXY) {
1331 if (sock->port > last_proxy_port) {
1332 last_proxy_port = sock->port + 1;
1334 if (!old) old = sock;
1337 last = &(tmp->next);
1342 return NULL; /* This is a serious error. */
1346 * FIXME: find a new IP address to listen on?
1348 * This could likely be done in the "home server"
1349 * configuration, to have per-home-server source IP's.
1352 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1355 * Keep going until we find an unused port.
1357 for (port = last_proxy_port; port < 64000; port++) {
1359 if (listen_bind(this) == 0) {
1361 * Add the new listener to the list of
1374 static const FR_NAME_NUMBER listen_compare[] = {
1376 { "status", RAD_LISTEN_NONE },
1378 { "auth", RAD_LISTEN_AUTH },
1379 #ifdef WITH_ACCOUNTING
1380 { "acct", RAD_LISTEN_ACCT },
1383 { "detail", RAD_LISTEN_DETAIL },
1386 { "proxy", RAD_LISTEN_PROXY },
1389 { "vmps", RAD_LISTEN_VQP },
1392 { "dhcp", RAD_LISTEN_DHCP },
1398 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1406 cf_log_info(cs, "listen {");
1408 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1410 if (rcode < 0) return NULL;
1413 cf_log_err(cf_sectiontoitem(cs),
1414 "No type specified in listen section");
1418 type = fr_str2int(listen_compare, listen_type, -1);
1420 cf_log_err(cf_sectiontoitem(cs),
1421 "Invalid type \"%s\" in listen section.",
1429 * Allow listen sections in the default config to
1430 * refer to a server.
1433 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1435 if (rcode == 1) { /* compatiblity with 2.0-pre */
1436 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1439 if (rcode < 0) return NULL;
1443 * Set up cross-type data.
1445 this = listen_alloc(type);
1446 this->server = server;
1450 * Call per-type parser.
1452 if (master_listen[type].parse(cs, this) < 0) {
1457 cf_log_info(cs, "}");
1463 * Generate a list of listeners. Takes an input list of
1464 * listeners, too, so we don't close sockets with waiting packets.
1466 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1468 int override = FALSE;
1471 rad_listen_t **last;
1473 fr_ipaddr_t server_ipaddr;
1476 int defined_proxy = 0;
1480 * We shouldn't be called with a pre-existing list.
1482 rad_assert(head && (*head == NULL));
1485 server_ipaddr.af = AF_UNSPEC;
1488 * If the port is specified on the command-line,
1489 * it over-rides the configuration file.
1491 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1493 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1496 * If the IP address was configured on the command-line,
1497 * use that as the "bind_address"
1499 if (mainconfig.myip.af != AF_UNSPEC) {
1500 memcpy(&server_ipaddr, &mainconfig.myip,
1501 sizeof(server_ipaddr));
1507 * Else look for bind_address and/or listen sections.
1509 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1510 rcode = cf_item_parse(config, "bind_address",
1512 &server_ipaddr.ipaddr.ip4addr, NULL);
1513 if (rcode < 0) return -1; /* error parsing it */
1515 if (rcode == 0) { /* successfully parsed IPv4 */
1516 listen_socket_t *sock;
1517 server_ipaddr.af = AF_INET;
1519 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'.");
1523 if (strcmp(progname, "vmpsd") == 0) {
1524 this = listen_alloc(RAD_LISTEN_VQP);
1525 if (!auth_port) auth_port = 1589;
1528 this = listen_alloc(RAD_LISTEN_AUTH);
1532 sock->ipaddr = server_ipaddr;
1533 sock->port = auth_port;
1535 sock->clients = clients_parse_section(config);
1536 if (!sock->clients) {
1537 cf_log_err(cf_sectiontoitem(config),
1538 "Failed to find any clients for this listen section");
1542 if (listen_bind(this) < 0) {
1545 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1548 auth_port = sock->port; /* may have been updated in listen_bind */
1550 cs = cf_section_sub_find_name2(config, "server",
1552 if (cs) this->server = mainconfig.name;
1556 last = &(this->next);
1562 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
1565 #ifdef WITH_ACCOUNTING
1567 * Open Accounting Socket.
1569 * If we haven't already gotten acct_port from
1570 * /etc/services, then make it auth_port + 1.
1572 this = listen_alloc(RAD_LISTEN_ACCT);
1576 * Create the accounting socket.
1578 * The accounting port is always the
1579 * authentication port + 1
1581 sock->ipaddr = server_ipaddr;
1582 sock->port = auth_port + 1;
1584 sock->clients = clients_parse_section(config);
1585 if (!sock->clients) {
1586 cf_log_err(cf_sectiontoitem(config),
1587 "Failed to find any clients for this listen section");
1591 if (listen_bind(this) < 0) {
1594 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1599 cs = cf_section_sub_find_name2(config, "server",
1601 if (cs) this->server = mainconfig.name;
1605 last = &(this->next);
1607 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1608 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1614 * They specified an IP on the command-line, ignore
1615 * all listen sections except the one in '-n'.
1617 if (mainconfig.myip.af != AF_UNSPEC) {
1618 CONF_SECTION *subcs;
1619 const char *name2 = cf_section_name2(cs);
1621 cs = cf_section_sub_find_name2(config, "server",
1623 if (!cs) goto do_proxy;
1626 * Should really abstract this code...
1628 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1630 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1631 this = listen_parse(subcs, name2);
1638 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1642 last = &(this->next);
1643 } /* loop over "listen" directives in server <foo> */
1649 * Walk through the "listen" sections, if they exist.
1651 for (cs = cf_subsection_find_next(config, NULL, "listen");
1653 cs = cf_subsection_find_next(config, cs, "listen")) {
1654 this = listen_parse(cs, NULL);
1661 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1665 last = &(this->next);
1669 * Check virtual servers for "listen" sections, too.
1671 * FIXME: Move to virtual server init?
1673 for (cs = cf_subsection_find_next(config, NULL, "server");
1675 cs = cf_subsection_find_next(config, cs, "server")) {
1676 CONF_SECTION *subcs;
1677 const char *name2 = cf_section_name2(cs);
1679 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1681 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1682 this = listen_parse(subcs, name2);
1689 if (this->type == RAD_LISTEN_PROXY) {
1690 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
1697 last = &(this->next);
1698 } /* loop over "listen" directives in virtual servers */
1699 } /* loop over virtual servers */
1702 * If we're proxying requests, open the proxy FD.
1703 * Otherwise, don't do anything.
1707 if (mainconfig.proxy_requests == TRUE) {
1709 listen_socket_t *sock = NULL;
1712 * No sockets to receive packets, therefore
1713 * proxying is pointless.
1715 if (!*head) return -1;
1717 if (defined_proxy) goto done;
1720 * Find the first authentication port,
1723 for (this = *head; this != NULL; this = this->next) {
1724 if (this->type == RAD_LISTEN_AUTH) {
1726 if (server_ipaddr.af == AF_UNSPEC) {
1727 server_ipaddr = sock->ipaddr;
1729 port = sock->port + 2; /* skip acct port */
1732 if (this->type == RAD_LISTEN_VQP) {
1734 if (server_ipaddr.af == AF_UNSPEC) {
1735 server_ipaddr = sock->ipaddr;
1737 port = sock->port + 1;
1742 if (port < 0) port = 1024 + (fr_rand() & 0x1ff);
1745 * Address is still unspecified, use IPv4.
1747 if (server_ipaddr.af == AF_UNSPEC) {
1748 server_ipaddr.af = AF_INET;
1749 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1752 this = listen_alloc(RAD_LISTEN_PROXY);
1756 * Create the first proxy socket.
1758 sock->ipaddr = server_ipaddr;
1761 * Try to find a proxy port (value doesn't matter)
1763 for (sock->port = port;
1766 if (listen_bind(this) == 0) {
1768 last = &(this->next); /* just in case */
1773 if (sock->port >= 64000) {
1776 radlog(L_ERR, "Failed to open socket for proxying");
1781 done: /* used only in proxy code. */
1788 * Free a linked list of listeners;
1790 void listen_free(rad_listen_t **head)
1794 if (!head || !*head) return;
1798 rad_listen_t *next = this->next;
1801 * Other code may have eaten the FD.
1803 if (this->fd >= 0) close(this->fd);
1805 if (master_listen[this->type].free) {
1806 master_listen[this->type].free(this);
1818 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
1823 for (this = mainconfig.listen; this != NULL; this = this->next) {
1824 listen_socket_t *sock;
1826 if ((this->type != RAD_LISTEN_AUTH) &&
1827 (this->type != RAD_LISTEN_ACCT)) continue;
1831 if ((sock->port == port) &&
1832 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {
1833 return sock->clients;
1840 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
1844 for (this = mainconfig.listen; this != NULL; this = this->next) {
1845 listen_socket_t *sock;
1847 if ((this->type != RAD_LISTEN_AUTH) &&
1848 (this->type != RAD_LISTEN_ACCT)) continue;
1852 if ((sock->port == port) &&
1853 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {