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;
77 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type);
80 * Find a per-socket client.
82 RADCLIENT *client_listener_find(const rad_listen_t *listener,
83 const fr_ipaddr_t *ipaddr)
85 #ifdef WITH_DYNAMIC_CLIENTS
88 listen_socket_t *sock;
90 RADCLIENT *client, *created;
92 RADCLIENT_LIST *clients;
94 rad_assert(listener != NULL);
95 rad_assert(ipaddr != NULL);
97 rad_assert((listener->type == RAD_LISTEN_AUTH)
99 || (listener->type == RAD_LISTEN_NONE)
101 #ifdef WITH_ACCOUNTING
102 || (listener->type == RAD_LISTEN_ACCT)
105 || (listener->type == RAD_LISTEN_VQP)
108 || (listener->type == RAD_LISTEN_DHCP)
112 clients = ((listen_socket_t *)listener->data)->clients;
115 * This HAS to have been initialized previously.
117 rad_assert(clients != NULL);
119 #ifdef WITH_DYNAMIC_CLIENTS
120 client = client_find(clients, ipaddr);
121 if (!client) return NULL;
124 * No server defined, and it's not dynamic. Return it.
126 if (!client->client_server && !client->dynamic) return client;
131 * It's a dynamically generated client, check it.
133 if (client->dynamic) {
135 * Lives forever. Return it.
137 if (client->lifetime == 0) return client;
140 * Rate-limit the deletion of known clients.
141 * This makes them last a little longer, but
142 * prevents the server from melting down if (say)
143 * 10k clients all expire at once.
145 if (now == client->last_new_client) return client;
148 * It's not dead yet. Return it.
150 if ((client->created + client->lifetime) > now) return client;
153 * It's dead, Jim. Delete it.
155 client_delete(clients, client);
158 * Go find the enclosing network again.
160 client = client_find(clients, ipaddr);
165 if (!client) return NULL;
166 if (!client->client_server) return NULL;
169 * At this point, 'client' is the enclosing
170 * network that configures where dynamic clients
173 rad_assert(client->dynamic == 0);
176 * The IP is unknown, so we've found an enclosing
177 * network. Enable DoS protection. We only
178 * allow one new client per second. Known
179 * clients aren't subject to this restriction.
181 if (now == client->last_new_client) return NULL;
184 client->last_new_client = now;
186 request = request_alloc();
187 if (!request) return NULL;
189 request->listener = listener;
190 request->client = client;
191 request->packet = rad_alloc(0);
192 if (!request->packet) {
193 request_free(&request);
196 request->reply = rad_alloc(0);
197 if (!request->reply) {
198 request_free(&request);
201 request->packet->timestamp = request->timestamp;
203 request->priority = listener->type;
204 request->server = client->client_server;
205 request->root = &mainconfig;
208 * Run a fake request through the given virtual server.
209 * Look for FreeRADIUS-Client-IP-Address
210 * FreeRADIUS-Client-Secret
213 * and create the RADCLIENT structure from that.
216 sock = listener->data;
217 request->packet->sockfd = listener->fd;
218 request->packet->src_ipaddr = *ipaddr;
219 request->packet->src_port = 0; /* who cares... */
220 request->packet->dst_ipaddr = sock->ipaddr;
221 request->packet->dst_port = sock->port;
223 request->reply->sockfd = request->packet->sockfd;
224 request->reply->dst_ipaddr = request->packet->src_ipaddr;
225 request->reply->src_ipaddr = request->packet->dst_ipaddr;
226 request->reply->dst_port = request->packet->src_port;
227 request->reply->src_port = request->packet->dst_port;
228 request->reply->id = request->packet->id;
229 request->reply->code = 0; /* UNKNOWN code */
232 DEBUG("server %s {", request->server);
234 rcode = module_authorize(0, request);
236 DEBUG("} # server %s", request->server);
238 if (rcode != RLM_MODULE_OK) {
239 request_free(&request);
244 * If the client was updated by rlm_dynamic_clients,
245 * don't create the client from attribute-value pairs.
247 if (request->client == client) {
248 created = client_create(clients, request);
250 created = request->client;
253 * This frees the client if it isn't valid.
255 if (!client_validate(clients, client, created)) {
259 request_free(&request);
261 return created; /* may be NULL */
263 return client_find(clients, ipaddr);
267 static int listen_bind(rad_listen_t *this);
271 * Process and reply to a server-status request.
272 * Like rad_authenticate and rad_accounting this should
273 * live in it's own file but it's so small we don't bother.
275 static int rad_status_server(REQUEST *request)
277 int rcode = RLM_MODULE_OK;
280 switch (request->listener->type) {
282 case RAD_LISTEN_NONE:
284 case RAD_LISTEN_AUTH:
285 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
287 rcode = module_authorize(dval->value, request);
289 rcode = RLM_MODULE_OK;
294 case RLM_MODULE_UPDATED:
295 request->reply->code = PW_AUTHENTICATION_ACK;
298 case RLM_MODULE_FAIL:
299 case RLM_MODULE_HANDLED:
300 request->reply->code = 0; /* don't reply */
304 case RLM_MODULE_REJECT:
305 request->reply->code = PW_AUTHENTICATION_REJECT;
310 #ifdef WITH_ACCOUNTING
311 case RAD_LISTEN_ACCT:
312 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
314 rcode = module_accounting(dval->value, request);
316 rcode = RLM_MODULE_OK;
321 case RLM_MODULE_UPDATED:
322 request->reply->code = PW_ACCOUNTING_RESPONSE;
326 request->reply->code = 0; /* don't reply */
338 * Full statistics are available only on a statistics
341 if (request->listener->type == RAD_LISTEN_NONE) {
342 request_stats_reply(request);
350 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
353 listen_socket_t *sock = this->data;
356 switch (this->type) {
358 case RAD_LISTEN_NONE: /* what a hack... */
363 case RAD_LISTEN_AUTH:
364 name = "authentication";
367 #ifdef WITH_ACCOUNTING
368 case RAD_LISTEN_ACCT:
374 case RAD_LISTEN_PROXY:
386 case RAD_LISTEN_DHCP:
396 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
397 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
401 #ifdef SO_BINDTODEVICE
402 if (sock->interface) {
403 ADDSTRING(" interface ");
404 ADDSTRING(sock->interface);
408 ADDSTRING(" address ");
410 if ((sock->ipaddr.af == AF_INET) &&
411 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
412 strlcpy(buffer, "*", bufsize);
414 ip_ntoh(&sock->ipaddr, buffer, bufsize);
419 snprintf(buffer, bufsize, "%d", sock->port);
423 ADDSTRING(" as server ");
424 ADDSTRING(this->server);
435 * Parse an authentication or accounting socket.
437 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
442 listen_socket_t *sock = this->data;
443 char *section_name = NULL;
444 CONF_SECTION *client_cs, *parentcs;
449 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
450 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
451 &ipaddr.ipaddr.ip4addr, NULL);
452 if (rcode < 0) return -1;
454 if (rcode == 0) { /* successfully parsed IPv4 */
457 } else { /* maybe IPv6? */
458 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
459 &ipaddr.ipaddr.ip6addr, NULL);
460 if (rcode < 0) return -1;
463 cf_log_err(cf_sectiontoitem(cs),
464 "No address specified in listen section");
467 ipaddr.af = AF_INET6;
470 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
472 if (rcode < 0) return -1;
474 if ((listen_port < 0) || (listen_port > 65500)) {
475 cf_log_err(cf_sectiontoitem(cs),
476 "Invalid value for \"port\"");
480 sock->ipaddr = ipaddr;
481 sock->port = listen_port;
484 * If we can bind to interfaces, do so,
487 if (cf_pair_find(cs, "interface")) {
488 #ifndef SO_BINDTODEVICE
489 cf_log_err(cf_sectiontoitem(cs),
490 "System does not support binding to interfaces. Delete this line from the configuration file.");
494 CONF_PAIR *cp = cf_pair_find(cs, "interface");
496 rad_assert(cp != NULL);
497 value = cf_pair_value(cp);
499 cf_log_err(cf_sectiontoitem(cs),
500 "No interface name given");
503 sock->interface = value;
508 * And bind it to the port.
510 if (listen_bind(this) < 0) {
512 cf_log_err(cf_sectiontoitem(cs),
513 "Error binding to port for %s port %d",
514 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
521 * Proxy sockets don't have clients.
523 if (this->type == RAD_LISTEN_PROXY) return 0;
527 * The more specific configurations are preferred to more
531 parentcs = cf_top_section(cs);
532 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
533 §ion_name, NULL);
534 if (rcode < 0) return -1; /* bad string */
537 * Explicit list given: use it.
539 client_cs = cf_section_sub_find_name2(parentcs,
543 client_cs = cf_section_find(section_name);
546 cf_log_err(cf_sectiontoitem(cs),
547 "Failed to find clients %s {...}",
553 } /* else there was no "clients = " entry. */
556 CONF_SECTION *server_cs;
558 server_cs = cf_section_sub_find_name2(parentcs,
562 * Found a "server foo" section. If there are clients
566 (cf_section_sub_find(server_cs, "client") != NULL)) {
567 client_cs = server_cs;
572 * Still nothing. Look for global clients.
574 if (!client_cs) client_cs = parentcs;
576 sock->clients = clients_parse_section(client_cs);
577 if (!sock->clients) {
578 cf_log_err(cf_sectiontoitem(cs),
579 "Failed to load clients for this listen section");
587 * Send an authentication response packet
589 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
591 rad_assert(request->listener == listener);
592 rad_assert(listener->send == auth_socket_send);
594 return rad_send(request->reply, request->packet,
595 request->client->secret);
599 #ifdef WITH_ACCOUNTING
601 * Send an accounting response packet (or not)
603 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
605 rad_assert(request->listener == listener);
606 rad_assert(listener->send == acct_socket_send);
609 * Accounting reject's are silently dropped.
611 * We do it here to avoid polluting the rest of the
612 * code with this knowledge
614 if (request->reply->code == 0) return 0;
616 return rad_send(request->reply, request->packet,
617 request->client->secret);
623 * Send a packet to a home server.
625 * FIXME: have different code for proxy auth & acct!
627 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
629 listen_socket_t *sock = listener->data;
631 rad_assert(request->proxy_listener == listener);
632 rad_assert(listener->send == proxy_socket_send);
634 request->proxy->src_ipaddr = sock->ipaddr;
635 request->proxy->src_port = sock->port;
637 return rad_send(request->proxy, request->packet,
638 request->home_server->secret);
644 * Check if an incoming request is "ok"
646 * It takes packets, not requests. It sees if the packet looks
647 * OK. If so, it does a number of sanity checks on it.
649 static int stats_socket_recv(rad_listen_t *listener,
650 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
654 RADIUS_PACKET *packet;
657 fr_ipaddr_t src_ipaddr;
659 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
660 if (rcode < 0) return 0;
662 RAD_STATS_TYPE_INC(listener, total_requests);
664 if (rcode < 20) { /* AUTH_HDR_LEN */
665 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
669 if ((client = client_listener_find(listener,
670 &src_ipaddr)) == NULL) {
671 rad_recv_discard(listener->fd);
672 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
674 if (debug_flag > 0) {
677 listener->print(listener, name, sizeof(name));
680 * This is debugging rather than logging, so that
681 * DoS attacks don't affect us.
683 DEBUG("Ignoring request to %s from unknown client %s port %d",
685 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
686 buffer, sizeof(buffer)), src_port);
693 * We only understand Status-Server on this socket.
695 if (code != PW_STATUS_SERVER) {
696 DEBUG("Ignoring packet code %d sent to Status-Server port",
698 rad_recv_discard(listener->fd);
699 RAD_STATS_TYPE_INC(listener, total_unknown_types);
700 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
705 * Now that we've sanity checked everything, receive the
708 packet = rad_recv(listener->fd, 1); /* require message authenticator */
710 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
711 DEBUG("%s", fr_strerror());
715 if (!received_request(listener, packet, prequest, client)) {
716 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
717 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
722 *pfun = rad_status_server;
729 * Check if an incoming request is "ok"
731 * It takes packets, not requests. It sees if the packet looks
732 * OK. If so, it does a number of sanity checks on it.
734 static int auth_socket_recv(rad_listen_t *listener,
735 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
739 RADIUS_PACKET *packet;
740 RAD_REQUEST_FUNP fun = NULL;
743 fr_ipaddr_t src_ipaddr;
745 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
746 if (rcode < 0) return 0;
748 RAD_STATS_TYPE_INC(listener, total_requests);
750 if (rcode < 20) { /* AUTH_HDR_LEN */
751 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
755 if ((client = client_listener_find(listener,
756 &src_ipaddr)) == NULL) {
757 rad_recv_discard(listener->fd);
758 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
760 if (debug_flag > 0) {
763 listener->print(listener, name, sizeof(name));
766 * This is debugging rather than logging, so that
767 * DoS attacks don't affect us.
769 DEBUG("Ignoring request to %s from unknown client %s port %d",
771 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
772 buffer, sizeof(buffer)), src_port);
779 * Some sanity checks, based on the packet code.
782 case PW_AUTHENTICATION_REQUEST:
783 RAD_STATS_CLIENT_INC(listener, client, total_requests);
784 fun = rad_authenticate;
787 case PW_STATUS_SERVER:
788 if (!mainconfig.status_server) {
789 rad_recv_discard(listener->fd);
790 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
791 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
792 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
795 fun = rad_status_server;
799 rad_recv_discard(listener->fd);
800 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
801 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
803 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
804 code, client->shortname, src_port);
807 } /* switch over packet types */
810 * Now that we've sanity checked everything, receive the
813 packet = rad_recv(listener->fd, client->message_authenticator);
815 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
816 DEBUG("%s", fr_strerror());
820 if (!received_request(listener, packet, prequest, client)) {
821 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
822 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
832 #ifdef WITH_ACCOUNTING
834 * Receive packets from an accounting socket
836 static int acct_socket_recv(rad_listen_t *listener,
837 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
841 RADIUS_PACKET *packet;
842 RAD_REQUEST_FUNP fun = NULL;
845 fr_ipaddr_t src_ipaddr;
847 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
848 if (rcode < 0) return 0;
850 RAD_STATS_TYPE_INC(listener, total_requests);
852 if (rcode < 20) { /* AUTH_HDR_LEN */
853 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
857 if ((client = client_listener_find(listener,
858 &src_ipaddr)) == NULL) {
859 rad_recv_discard(listener->fd);
860 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
863 * This is debugging rather than logging, so that
864 * DoS attacks don't affect us.
866 if (debug_flag > 0) {
869 listener->print(listener, name, sizeof(name));
871 DEBUG("Ignoring request to %s from unknown client %s port %d",
873 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
874 buffer, sizeof(buffer)), src_port);
881 * Some sanity checks, based on the packet code.
884 case PW_ACCOUNTING_REQUEST:
885 RAD_STATS_CLIENT_INC(listener, client, total_requests);
886 fun = rad_accounting;
889 case PW_STATUS_SERVER:
890 if (!mainconfig.status_server) {
891 rad_recv_discard(listener->fd);
892 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
893 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
895 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
898 fun = rad_status_server;
902 rad_recv_discard(listener->fd);
903 RAD_STATS_TYPE_INC(listener, total_unknown_types);
904 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
906 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
907 code, client->shortname, src_port);
909 } /* switch over packet types */
912 * Now that we've sanity checked everything, receive the
915 packet = rad_recv(listener->fd, 0);
917 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
918 radlog(L_ERR, "%s", fr_strerror());
923 * There can be no duplicate accounting packets.
925 if (!received_request(listener, packet, prequest, client)) {
926 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
927 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
939 * Recieve packets from a proxy socket.
941 static int proxy_socket_recv(rad_listen_t *listener,
942 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
945 RADIUS_PACKET *packet;
946 RAD_REQUEST_FUNP fun = NULL;
949 packet = rad_recv(listener->fd, 0);
951 radlog(L_ERR, "%s", fr_strerror());
956 * FIXME: Client MIB updates?
958 switch(packet->code) {
959 case PW_AUTHENTICATION_ACK:
960 case PW_ACCESS_CHALLENGE:
961 case PW_AUTHENTICATION_REJECT:
962 fun = rad_authenticate;
965 #ifdef WITH_ACCOUNTING
966 case PW_ACCOUNTING_RESPONSE:
967 fun = rad_accounting;
973 * FIXME: Update MIB for packet types?
975 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
976 "from home server %s port %d - ID %d : IGNORED",
978 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
979 packet->src_port, packet->id);
984 request = received_proxy_response(packet);
989 rad_assert(fun != NULL);
998 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1000 if (!request->reply->code) return 0;
1002 rad_encode(request->reply, request->packet,
1003 request->client->secret);
1004 rad_sign(request->reply, request->packet,
1005 request->client->secret);
1011 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1013 if (rad_verify(request->packet, NULL,
1014 request->client->secret) < 0) {
1018 return rad_decode(request->packet, NULL,
1019 request->client->secret);
1023 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1025 rad_encode(request->proxy, NULL, request->home_server->secret);
1026 rad_sign(request->proxy, NULL, request->home_server->secret);
1032 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1034 if (rad_verify(request->proxy_reply, request->proxy,
1035 request->home_server->secret) < 0) {
1039 return rad_decode(request->proxy_reply, request->proxy,
1040 request->home_server->secret);
1046 #include "command.c"
1048 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1050 { common_socket_parse, NULL,
1051 stats_socket_recv, auth_socket_send,
1052 socket_print, client_socket_encode, client_socket_decode },
1055 * This always gets defined.
1057 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1062 { common_socket_parse, NULL,
1063 proxy_socket_recv, proxy_socket_send,
1064 socket_print, proxy_socket_encode, proxy_socket_decode },
1067 /* authentication */
1068 { common_socket_parse, NULL,
1069 auth_socket_recv, auth_socket_send,
1070 socket_print, client_socket_encode, client_socket_decode },
1072 #ifdef WITH_ACCOUNTING
1074 { common_socket_parse, NULL,
1075 acct_socket_recv, acct_socket_send,
1076 socket_print, client_socket_encode, client_socket_decode},
1081 { detail_parse, detail_free,
1082 detail_recv, detail_send,
1083 detail_print, detail_encode, detail_decode },
1087 /* vlan query protocol */
1088 { common_socket_parse, NULL,
1089 vqp_socket_recv, vqp_socket_send,
1090 socket_print, vqp_socket_encode, vqp_socket_decode },
1094 /* dhcp query protocol */
1095 { dhcp_socket_parse, NULL,
1096 dhcp_socket_recv, dhcp_socket_send,
1097 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1100 #ifdef WITH_COMMAND_SOCKET
1101 /* TCP command socket */
1102 { command_socket_parse, NULL,
1103 command_domain_accept, command_domain_send,
1104 command_socket_print, command_socket_encode, command_socket_decode },
1112 * Binds a listener to a socket.
1114 static int listen_bind(rad_listen_t *this)
1116 struct sockaddr_storage salocal;
1118 listen_socket_t *sock = this->data;
1121 * If the port is zero, then it means the appropriate
1122 * thing from /etc/services.
1124 if (sock->port == 0) {
1125 struct servent *svp;
1127 switch (this->type) {
1128 case RAD_LISTEN_AUTH:
1129 svp = getservbyname ("radius", "udp");
1131 sock->port = ntohs(svp->s_port);
1133 sock->port = PW_AUTH_UDP_PORT;
1137 #ifdef WITH_ACCOUNTING
1138 case RAD_LISTEN_ACCT:
1139 svp = getservbyname ("radacct", "udp");
1141 sock->port = ntohs(svp->s_port);
1143 sock->port = PW_ACCT_UDP_PORT;
1149 case RAD_LISTEN_PROXY:
1155 case RAD_LISTEN_VQP:
1161 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1167 * Copy fr_socket() here, as we may need to bind to a device.
1169 this->fd = socket(sock->ipaddr.af, SOCK_DGRAM, 0);
1171 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
1175 #ifdef SO_BINDTODEVICE
1177 * Bind to a device BEFORE touching IP addresses.
1179 if (sock->interface) {
1181 strcpy(ifreq.ifr_name, sock->interface);
1183 if (setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1184 (char *)&ifreq, sizeof(ifreq)) < 0) {
1186 radlog(L_ERR, "Failed opening to interface %s: %s",
1187 sock->interface, strerror(errno));
1189 } /* else it worked. */
1193 #ifdef WITH_UDPFROMTO
1195 * Initialize udpfromto for all sockets.
1197 if (udpfromto_init(this->fd) != 0) {
1204 * Set up sockaddr stuff.
1206 if (!fr_ipaddr2sockaddr(&sock->ipaddr, sock->port, &salocal, &salen)) {
1211 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1212 if (sock->ipaddr.af == AF_INET6) {
1214 * Listening on '::' does NOT get you IPv4 to
1215 * IPv6 mapping. You've got to listen on an IPv4
1216 * address, too. This makes the rest of the server
1217 * design a little simpler.
1221 if (IN6_IS_ADDR_UNSPECIFIED(&sock->ipaddr.ipaddr.ip6addr)) {
1224 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
1225 (char *)&on, sizeof(on));
1227 #endif /* IPV6_V6ONLY */
1228 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
1231 if (bind(this->fd, (struct sockaddr *) &salocal, salen) < 0) {
1233 radlog(L_ERR, "Failed binding to socket: %s\n",
1239 * FreeBSD jail issues. We bind to 0.0.0.0, but the
1240 * kernel instead binds us to a 1.2.3.4. If this
1241 * happens, notice, and remember our real IP.
1244 struct sockaddr_storage src;
1245 socklen_t sizeof_src = sizeof(src);
1247 memset(&src, 0, sizeof_src);
1248 if (getsockname(this->fd, (struct sockaddr *) &src,
1250 radlog(L_ERR, "Failed getting socket name: %s",
1255 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
1256 &sock->ipaddr, &sock->port)) {
1257 radlog(L_ERR, "Socket has unsupported address family");
1266 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
1267 radlog(L_ERR, "Failure getting socket flags: %s)\n",
1272 flags |= O_NONBLOCK;
1273 if( fcntl(this->fd, F_SETFL, flags) < 0) {
1274 radlog(L_ERR, "Failure setting socket flags: %s)\n",
1286 * Allocate & initialize a new listener.
1288 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
1292 this = rad_malloc(sizeof(*this));
1293 memset(this, 0, sizeof(*this));
1296 this->recv = master_listen[this->type].recv;
1297 this->send = master_listen[this->type].send;
1298 this->print = master_listen[this->type].print;
1299 this->encode = master_listen[this->type].encode;
1300 this->decode = master_listen[this->type].decode;
1304 case RAD_LISTEN_NONE:
1306 case RAD_LISTEN_AUTH:
1307 #ifdef WITH_ACCOUNTING
1308 case RAD_LISTEN_ACCT:
1311 case RAD_LISTEN_PROXY:
1314 case RAD_LISTEN_VQP:
1317 case RAD_LISTEN_DHCP:
1319 this->data = rad_malloc(sizeof(listen_socket_t));
1320 memset(this->data, 0, sizeof(listen_socket_t));
1324 case RAD_LISTEN_DETAIL:
1329 #ifdef WITH_COMMAND_SOCKET
1330 case RAD_LISTEN_COMMAND:
1331 this->data = rad_malloc(sizeof(fr_command_socket_t));
1332 memset(this->data, 0, sizeof(fr_command_socket_t));
1337 rad_assert("Unsupported option!" == NULL);
1347 * Externally visible function for creating a new proxy LISTENER.
1349 * For now, don't take ipaddr or port.
1351 * Not thread-safe, but all calls to it are protected by the
1352 * proxy mutex in request_list.c
1354 rad_listen_t *proxy_new_listener()
1356 int last_proxy_port, port;
1357 rad_listen_t *this, *tmp, **last;
1358 listen_socket_t *sock, *old;
1360 this = listen_alloc(RAD_LISTEN_PROXY);
1363 * Find an existing proxy socket to copy.
1365 * FIXME: Make it per-realm, or per-home server!
1367 last_proxy_port = 0;
1369 last = &mainconfig.listen;
1370 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1371 if (tmp->type == RAD_LISTEN_PROXY) {
1373 if (sock->port > last_proxy_port) {
1374 last_proxy_port = sock->port + 1;
1376 if (!old) old = sock;
1379 last = &(tmp->next);
1384 return NULL; /* This is a serious error. */
1388 * FIXME: find a new IP address to listen on?
1390 * This could likely be done in the "home server"
1391 * configuration, to have per-home-server source IP's.
1394 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1397 * Keep going until we find an unused port.
1399 for (port = last_proxy_port; port < 64000; port++) {
1401 if (listen_bind(this) == 0) {
1403 * Add the new listener to the list of
1416 static const FR_NAME_NUMBER listen_compare[] = {
1418 { "status", RAD_LISTEN_NONE },
1420 { "auth", RAD_LISTEN_AUTH },
1421 #ifdef WITH_ACCOUNTING
1422 { "acct", RAD_LISTEN_ACCT },
1425 { "detail", RAD_LISTEN_DETAIL },
1428 { "proxy", RAD_LISTEN_PROXY },
1431 { "vmps", RAD_LISTEN_VQP },
1434 { "dhcp", RAD_LISTEN_DHCP },
1436 #ifdef WITH_COMMAND_SOCKET
1437 { "control", RAD_LISTEN_COMMAND },
1443 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1451 cf_log_info(cs, "listen {");
1453 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1455 if (rcode < 0) return NULL;
1458 cf_log_err(cf_sectiontoitem(cs),
1459 "No type specified in listen section");
1463 type = fr_str2int(listen_compare, listen_type, -1);
1465 cf_log_err(cf_sectiontoitem(cs),
1466 "Invalid type \"%s\" in listen section.",
1474 * Allow listen sections in the default config to
1475 * refer to a server.
1478 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1480 if (rcode == 1) { /* compatiblity with 2.0-pre */
1481 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1484 if (rcode < 0) return NULL;
1488 * Set up cross-type data.
1490 this = listen_alloc(type);
1491 this->server = server;
1495 * Call per-type parser.
1497 if (master_listen[type].parse(cs, this) < 0) {
1502 cf_log_info(cs, "}");
1508 * Generate a list of listeners. Takes an input list of
1509 * listeners, too, so we don't close sockets with waiting packets.
1511 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1513 int override = FALSE;
1515 CONF_SECTION *cs = NULL;
1516 rad_listen_t **last;
1518 fr_ipaddr_t server_ipaddr;
1521 int defined_proxy = 0;
1525 * We shouldn't be called with a pre-existing list.
1527 rad_assert(head && (*head == NULL));
1530 server_ipaddr.af = AF_UNSPEC;
1533 * If the port is specified on the command-line,
1534 * it over-rides the configuration file.
1536 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1538 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1541 * If the IP address was configured on the command-line,
1542 * use that as the "bind_address"
1544 if (mainconfig.myip.af != AF_UNSPEC) {
1545 memcpy(&server_ipaddr, &mainconfig.myip,
1546 sizeof(server_ipaddr));
1552 * Else look for bind_address and/or listen sections.
1554 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1555 rcode = cf_item_parse(config, "bind_address",
1557 &server_ipaddr.ipaddr.ip4addr, NULL);
1558 if (rcode < 0) return -1; /* error parsing it */
1560 if (rcode == 0) { /* successfully parsed IPv4 */
1561 listen_socket_t *sock;
1562 server_ipaddr.af = AF_INET;
1564 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'.");
1568 if (strcmp(progname, "vmpsd") == 0) {
1569 this = listen_alloc(RAD_LISTEN_VQP);
1570 if (!auth_port) auth_port = 1589;
1573 this = listen_alloc(RAD_LISTEN_AUTH);
1577 sock->ipaddr = server_ipaddr;
1578 sock->port = auth_port;
1580 sock->clients = clients_parse_section(config);
1581 if (!sock->clients) {
1582 cf_log_err(cf_sectiontoitem(config),
1583 "Failed to find any clients for this listen section");
1587 if (listen_bind(this) < 0) {
1590 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1593 auth_port = sock->port; /* may have been updated in listen_bind */
1595 cs = cf_section_sub_find_name2(config, "server",
1597 if (cs) this->server = mainconfig.name;
1601 last = &(this->next);
1607 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
1610 #ifdef WITH_ACCOUNTING
1612 * Open Accounting Socket.
1614 * If we haven't already gotten acct_port from
1615 * /etc/services, then make it auth_port + 1.
1617 this = listen_alloc(RAD_LISTEN_ACCT);
1621 * Create the accounting socket.
1623 * The accounting port is always the
1624 * authentication port + 1
1626 sock->ipaddr = server_ipaddr;
1627 sock->port = auth_port + 1;
1629 sock->clients = clients_parse_section(config);
1630 if (!sock->clients) {
1631 cf_log_err(cf_sectiontoitem(config),
1632 "Failed to find any clients for this listen section");
1636 if (listen_bind(this) < 0) {
1639 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1644 cs = cf_section_sub_find_name2(config, "server",
1646 if (cs) this->server = mainconfig.name;
1650 last = &(this->next);
1652 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1653 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1659 * They specified an IP on the command-line, ignore
1660 * all listen sections except the one in '-n'.
1662 if (mainconfig.myip.af != AF_UNSPEC) {
1663 CONF_SECTION *subcs;
1664 const char *name2 = cf_section_name2(cs);
1666 cs = cf_section_sub_find_name2(config, "server",
1668 if (!cs) goto do_proxy;
1671 * Should really abstract this code...
1673 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1675 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1676 this = listen_parse(subcs, name2);
1683 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1687 last = &(this->next);
1688 } /* loop over "listen" directives in server <foo> */
1694 * Walk through the "listen" sections, if they exist.
1696 for (cs = cf_subsection_find_next(config, NULL, "listen");
1698 cs = cf_subsection_find_next(config, cs, "listen")) {
1699 this = listen_parse(cs, NULL);
1706 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1710 last = &(this->next);
1714 * Check virtual servers for "listen" sections, too.
1716 * FIXME: Move to virtual server init?
1718 for (cs = cf_subsection_find_next(config, NULL, "server");
1720 cs = cf_subsection_find_next(config, cs, "server")) {
1721 CONF_SECTION *subcs;
1722 const char *name2 = cf_section_name2(cs);
1724 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1726 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1727 this = listen_parse(subcs, name2);
1734 if (this->type == RAD_LISTEN_PROXY) {
1735 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
1742 last = &(this->next);
1743 } /* loop over "listen" directives in virtual servers */
1744 } /* loop over virtual servers */
1747 * If we're proxying requests, open the proxy FD.
1748 * Otherwise, don't do anything.
1752 if (mainconfig.proxy_requests == TRUE) {
1754 listen_socket_t *sock = NULL;
1757 * No sockets to receive packets, therefore
1758 * proxying is pointless.
1760 if (!*head) return -1;
1762 if (defined_proxy) goto done;
1765 * Find the first authentication port,
1768 for (this = *head; this != NULL; this = this->next) {
1769 if (this->type == RAD_LISTEN_AUTH) {
1771 if (server_ipaddr.af == AF_UNSPEC) {
1772 server_ipaddr = sock->ipaddr;
1774 port = sock->port + 2; /* skip acct port */
1777 if (this->type == RAD_LISTEN_VQP) {
1779 if (server_ipaddr.af == AF_UNSPEC) {
1780 server_ipaddr = sock->ipaddr;
1782 port = sock->port + 1;
1787 if (port < 0) port = 1024 + (fr_rand() & 0x1ff);
1790 * Address is still unspecified, use IPv4.
1792 if (server_ipaddr.af == AF_UNSPEC) {
1793 server_ipaddr.af = AF_INET;
1794 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1797 this = listen_alloc(RAD_LISTEN_PROXY);
1801 * Create the first proxy socket.
1803 sock->ipaddr = server_ipaddr;
1806 * Try to find a proxy port (value doesn't matter)
1808 for (sock->port = port;
1811 if (listen_bind(this) == 0) {
1813 last = &(this->next); /* just in case */
1818 if (sock->port >= 64000) {
1821 radlog(L_ERR, "Failed to open socket for proxying");
1826 done: /* used only in proxy code. */
1833 * Free a linked list of listeners;
1835 void listen_free(rad_listen_t **head)
1839 if (!head || !*head) return;
1843 rad_listen_t *next = this->next;
1846 * Other code may have eaten the FD.
1848 if (this->fd >= 0) close(this->fd);
1850 if (master_listen[this->type].free) {
1851 master_listen[this->type].free(this);
1863 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
1868 for (this = mainconfig.listen; this != NULL; this = this->next) {
1869 listen_socket_t *sock;
1871 if ((this->type != RAD_LISTEN_AUTH) &&
1872 (this->type != RAD_LISTEN_ACCT)) continue;
1876 if ((sock->port == port) &&
1877 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {
1878 return sock->clients;
1885 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
1889 for (this = mainconfig.listen; this != NULL; this = this->next) {
1890 listen_socket_t *sock;
1892 if ((this->type != RAD_LISTEN_AUTH) &&
1893 (this->type != RAD_LISTEN_ACCT)) continue;
1897 if ((sock->port == port) &&
1898 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {