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) ||
107 (listener->type == RAD_LISTEN_DHCP));
109 clients = ((listen_socket_t *)listener->data)->clients;
112 * This HAS to have been initialized previously.
114 rad_assert(clients != NULL);
116 #ifdef WITH_DYNAMIC_CLIENTS
117 client = client_find(clients, ipaddr);
118 if (!client) return NULL;
121 * No server defined, and it's not dynamic. Return it.
123 if (!client->client_server && !client->dynamic) return client;
128 * It's a dynamically generated client, check it.
130 if (client->dynamic) {
132 * Lives forever. Return it.
134 if (client->lifetime == 0) return client;
137 * Rate-limit the deletion of known clients.
138 * This makes them last a little longer, but
139 * prevents the server from melting down if (say)
140 * 10k clients all expire at once.
142 if (now == client->last_new_client) return client;
145 * It's not dead yet. Return it.
147 if ((client->created + client->lifetime) > now) return client;
150 * It's dead, Jim. Delete it.
152 client_delete(clients, client);
155 * Go find the enclosing network again.
157 client = client_find(clients, ipaddr);
162 if (!client) return NULL;
163 if (!client->client_server) return NULL;
166 * At this point, 'client' is the enclosing
167 * network that configures where dynamic clients
170 rad_assert(client->dynamic == 0);
173 * The IP is unknown, so we've found an enclosing
174 * network. Enable DoS protection. We only
175 * allow one new client per second. Known
176 * clients aren't subject to this restriction.
178 if (now == client->last_new_client) return NULL;
181 client->last_new_client = now;
183 request = request_alloc();
184 if (!request) return NULL;
186 request->listener = listener;
187 request->client = client;
188 request->packet = rad_alloc(0);
189 if (!request->packet) {
190 request_free(&request);
193 request->reply = rad_alloc(0);
194 if (!request->reply) {
195 request_free(&request);
198 request->packet->timestamp = request->timestamp;
200 request->priority = listener->type;
201 request->server = client->client_server;
202 request->root = &mainconfig;
205 * Run a fake request through the given virtual server.
206 * Look for FreeRADIUS-Client-IP-Address
207 * FreeRADIUS-Client-Secret
210 * and create the RADCLIENT structure from that.
213 sock = listener->data;
214 request->packet->sockfd = listener->fd;
215 request->packet->src_ipaddr = *ipaddr;
216 request->packet->src_port = 0; /* who cares... */
217 request->packet->dst_ipaddr = sock->ipaddr;
218 request->packet->dst_port = sock->port;
220 request->reply->sockfd = request->packet->sockfd;
221 request->reply->dst_ipaddr = request->packet->src_ipaddr;
222 request->reply->src_ipaddr = request->packet->dst_ipaddr;
223 request->reply->dst_port = request->packet->src_port;
224 request->reply->src_port = request->packet->dst_port;
225 request->reply->id = request->packet->id;
226 request->reply->code = 0; /* UNKNOWN code */
229 DEBUG("server %s {", request->server);
231 rcode = module_authorize(0, request);
233 DEBUG("} # server %s", request->server);
235 if (rcode != RLM_MODULE_OK) {
236 request_free(&request);
241 * If the client was updated by rlm_dynamic_clients,
242 * don't create the client from attribute-value pairs.
244 if (request->client == client) {
245 created = client_create(clients, request);
247 created = request->client;
250 * This frees the client if it isn't valid.
252 if (!client_validate(clients, client, created)) {
256 request_free(&request);
258 return created; /* may be NULL */
260 return client_find(clients, ipaddr);
264 static int listen_bind(rad_listen_t *this);
268 * Process and reply to a server-status request.
269 * Like rad_authenticate and rad_accounting this should
270 * live in it's own file but it's so small we don't bother.
272 static int rad_status_server(REQUEST *request)
274 int rcode = RLM_MODULE_OK;
277 switch (request->listener->type) {
279 case RAD_LISTEN_NONE:
281 case RAD_LISTEN_AUTH:
282 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
284 rcode = module_authorize(dval->value, request);
286 rcode = RLM_MODULE_OK;
291 case RLM_MODULE_UPDATED:
292 request->reply->code = PW_AUTHENTICATION_ACK;
295 case RLM_MODULE_FAIL:
296 case RLM_MODULE_HANDLED:
297 request->reply->code = 0; /* don't reply */
301 case RLM_MODULE_REJECT:
302 request->reply->code = PW_AUTHENTICATION_REJECT;
307 #ifdef WITH_ACCOUNTING
308 case RAD_LISTEN_ACCT:
309 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
311 rcode = module_accounting(dval->value, request);
313 rcode = RLM_MODULE_OK;
318 case RLM_MODULE_UPDATED:
319 request->reply->code = PW_ACCOUNTING_RESPONSE;
323 request->reply->code = 0; /* don't reply */
335 * Full statistics are available only on a statistics
338 if (request->listener->type == RAD_LISTEN_NONE) {
339 request_stats_reply(request);
347 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
350 listen_socket_t *sock = this->data;
353 switch (this->type) {
355 case RAD_LISTEN_NONE: /* what a hack... */
360 case RAD_LISTEN_AUTH:
361 name = "authentication";
364 #ifdef WITH_ACCOUNTING
365 case RAD_LISTEN_ACCT:
371 case RAD_LISTEN_PROXY:
383 case RAD_LISTEN_DHCP:
393 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
394 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
398 #ifdef SO_BINDTODEVICE
399 if (sock->interface) {
400 ADDSTRING(" interface ");
401 ADDSTRING(sock->interface);
405 ADDSTRING(" address ");
407 if ((sock->ipaddr.af == AF_INET) &&
408 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
409 strlcpy(buffer, "*", bufsize);
411 ip_ntoh(&sock->ipaddr, buffer, bufsize);
416 snprintf(buffer, bufsize, "%d", sock->port);
420 ADDSTRING(" as server ");
421 ADDSTRING(this->server);
432 * Parse an authentication or accounting socket.
434 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
439 listen_socket_t *sock = this->data;
440 char *section_name = NULL;
441 CONF_SECTION *client_cs, *parentcs;
446 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
447 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
448 &ipaddr.ipaddr.ip4addr, NULL);
449 if (rcode < 0) return -1;
451 if (rcode == 0) { /* successfully parsed IPv4 */
454 } else { /* maybe IPv6? */
455 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
456 &ipaddr.ipaddr.ip6addr, NULL);
457 if (rcode < 0) return -1;
460 cf_log_err(cf_sectiontoitem(cs),
461 "No address specified in listen section");
464 ipaddr.af = AF_INET6;
467 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
469 if (rcode < 0) return -1;
471 if ((listen_port < 0) || (listen_port > 65500)) {
472 cf_log_err(cf_sectiontoitem(cs),
473 "Invalid value for \"port\"");
477 sock->ipaddr = ipaddr;
478 sock->port = listen_port;
481 * If we can bind to interfaces, do so,
484 if (cf_pair_find(cs, "interface")) {
485 #ifndef SO_BINDTODEVICE
486 cf_log_err(cf_sectiontoitem(cs),
487 "System does not support binding to interfaces. Delete this line from the configuration file.");
491 CONF_PAIR *cp = cf_pair_find(cs, "interface");
493 rad_assert(cp != NULL);
494 value = cf_pair_value(cp);
496 cf_log_err(cf_sectiontoitem(cs),
497 "No interface name given");
500 sock->interface = value;
505 * And bind it to the port.
507 if (listen_bind(this) < 0) {
509 cf_log_err(cf_sectiontoitem(cs),
510 "Error binding to port for %s port %d",
511 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
518 * Proxy sockets don't have clients.
520 if (this->type == RAD_LISTEN_PROXY) return 0;
524 * The more specific configurations are preferred to more
528 parentcs = cf_top_section(cs);
529 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
530 §ion_name, NULL);
531 if (rcode < 0) return -1; /* bad string */
534 * Explicit list given: use it.
536 client_cs = cf_section_sub_find_name2(parentcs,
540 client_cs = cf_section_find(section_name);
543 cf_log_err(cf_sectiontoitem(cs),
544 "Failed to find clients %s {...}",
550 } /* else there was no "clients = " entry. */
553 CONF_SECTION *server_cs;
555 server_cs = cf_section_sub_find_name2(parentcs,
559 * Found a "server foo" section. If there are clients
563 (cf_section_sub_find(server_cs, "client") != NULL)) {
564 client_cs = server_cs;
569 * Still nothing. Look for global clients.
571 if (!client_cs) client_cs = parentcs;
573 sock->clients = clients_parse_section(client_cs);
574 if (!sock->clients) {
575 cf_log_err(cf_sectiontoitem(cs),
576 "Failed to load clients for this listen section");
584 * Send an authentication response packet
586 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
588 rad_assert(request->listener == listener);
589 rad_assert(listener->send == auth_socket_send);
591 return rad_send(request->reply, request->packet,
592 request->client->secret);
596 #ifdef WITH_ACCOUNTING
598 * Send an accounting response packet (or not)
600 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
602 rad_assert(request->listener == listener);
603 rad_assert(listener->send == acct_socket_send);
606 * Accounting reject's are silently dropped.
608 * We do it here to avoid polluting the rest of the
609 * code with this knowledge
611 if (request->reply->code == 0) return 0;
613 return rad_send(request->reply, request->packet,
614 request->client->secret);
620 * Send a packet to a home server.
622 * FIXME: have different code for proxy auth & acct!
624 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
626 listen_socket_t *sock = listener->data;
628 rad_assert(request->proxy_listener == listener);
629 rad_assert(listener->send == proxy_socket_send);
631 request->proxy->src_ipaddr = sock->ipaddr;
632 request->proxy->src_port = sock->port;
634 return rad_send(request->proxy, request->packet,
635 request->home_server->secret);
641 * Check if an incoming request is "ok"
643 * It takes packets, not requests. It sees if the packet looks
644 * OK. If so, it does a number of sanity checks on it.
646 static int stats_socket_recv(rad_listen_t *listener,
647 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
651 RADIUS_PACKET *packet;
654 fr_ipaddr_t src_ipaddr;
656 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
657 if (rcode < 0) return 0;
659 RAD_STATS_TYPE_INC(listener, total_requests);
661 if (rcode < 20) { /* AUTH_HDR_LEN */
662 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
666 if ((client = client_listener_find(listener,
667 &src_ipaddr)) == NULL) {
668 rad_recv_discard(listener->fd);
669 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
671 if (debug_flag > 0) {
674 listener->print(listener, name, sizeof(name));
677 * This is debugging rather than logging, so that
678 * DoS attacks don't affect us.
680 DEBUG("Ignoring request to %s from unknown client %s port %d",
682 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
683 buffer, sizeof(buffer)), src_port);
690 * We only understand Status-Server on this socket.
692 if (code != PW_STATUS_SERVER) {
693 DEBUG("Ignoring packet code %d sent to Status-Server port",
695 rad_recv_discard(listener->fd);
696 RAD_STATS_TYPE_INC(listener, total_unknown_types);
697 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
702 * Now that we've sanity checked everything, receive the
705 packet = rad_recv(listener->fd, 1); /* require message authenticator */
707 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
708 DEBUG("%s", fr_strerror());
712 if (!received_request(listener, packet, prequest, client)) {
713 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
714 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
719 *pfun = rad_status_server;
726 * Check if an incoming request is "ok"
728 * It takes packets, not requests. It sees if the packet looks
729 * OK. If so, it does a number of sanity checks on it.
731 static int auth_socket_recv(rad_listen_t *listener,
732 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
736 RADIUS_PACKET *packet;
737 RAD_REQUEST_FUNP fun = NULL;
740 fr_ipaddr_t src_ipaddr;
742 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
743 if (rcode < 0) return 0;
745 RAD_STATS_TYPE_INC(listener, total_requests);
747 if (rcode < 20) { /* AUTH_HDR_LEN */
748 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
752 if ((client = client_listener_find(listener,
753 &src_ipaddr)) == NULL) {
754 rad_recv_discard(listener->fd);
755 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
757 if (debug_flag > 0) {
760 listener->print(listener, name, sizeof(name));
763 * This is debugging rather than logging, so that
764 * DoS attacks don't affect us.
766 DEBUG("Ignoring request to %s from unknown client %s port %d",
768 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
769 buffer, sizeof(buffer)), src_port);
776 * Some sanity checks, based on the packet code.
779 case PW_AUTHENTICATION_REQUEST:
780 RAD_STATS_CLIENT_INC(listener, client, total_requests);
781 fun = rad_authenticate;
784 case PW_STATUS_SERVER:
785 if (!mainconfig.status_server) {
786 rad_recv_discard(listener->fd);
787 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
788 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
789 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
792 fun = rad_status_server;
796 rad_recv_discard(listener->fd);
797 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
798 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
800 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
801 code, client->shortname, src_port);
804 } /* switch over packet types */
807 * Now that we've sanity checked everything, receive the
810 packet = rad_recv(listener->fd, client->message_authenticator);
812 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
813 DEBUG("%s", fr_strerror());
817 if (!received_request(listener, packet, prequest, client)) {
818 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
819 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
829 #ifdef WITH_ACCOUNTING
831 * Receive packets from an accounting socket
833 static int acct_socket_recv(rad_listen_t *listener,
834 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
838 RADIUS_PACKET *packet;
839 RAD_REQUEST_FUNP fun = NULL;
842 fr_ipaddr_t src_ipaddr;
844 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
845 if (rcode < 0) return 0;
847 RAD_STATS_TYPE_INC(listener, total_requests);
849 if (rcode < 20) { /* AUTH_HDR_LEN */
850 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
854 if ((client = client_listener_find(listener,
855 &src_ipaddr)) == NULL) {
856 rad_recv_discard(listener->fd);
857 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
860 * This is debugging rather than logging, so that
861 * DoS attacks don't affect us.
863 if (debug_flag > 0) {
866 listener->print(listener, name, sizeof(name));
868 DEBUG("Ignoring request to %s from unknown client %s port %d",
870 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
871 buffer, sizeof(buffer)), src_port);
878 * Some sanity checks, based on the packet code.
881 case PW_ACCOUNTING_REQUEST:
882 RAD_STATS_CLIENT_INC(listener, client, total_requests);
883 fun = rad_accounting;
886 case PW_STATUS_SERVER:
887 if (!mainconfig.status_server) {
888 rad_recv_discard(listener->fd);
889 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
890 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
892 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
895 fun = rad_status_server;
899 rad_recv_discard(listener->fd);
900 RAD_STATS_TYPE_INC(listener, total_unknown_types);
901 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
903 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
904 code, client->shortname, src_port);
906 } /* switch over packet types */
909 * Now that we've sanity checked everything, receive the
912 packet = rad_recv(listener->fd, 0);
914 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
915 radlog(L_ERR, "%s", fr_strerror());
920 * There can be no duplicate accounting packets.
922 if (!received_request(listener, packet, prequest, client)) {
923 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
924 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
936 * Recieve packets from a proxy socket.
938 static int proxy_socket_recv(rad_listen_t *listener,
939 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
942 RADIUS_PACKET *packet;
943 RAD_REQUEST_FUNP fun = NULL;
946 packet = rad_recv(listener->fd, 0);
948 radlog(L_ERR, "%s", fr_strerror());
953 * FIXME: Client MIB updates?
955 switch(packet->code) {
956 case PW_AUTHENTICATION_ACK:
957 case PW_ACCESS_CHALLENGE:
958 case PW_AUTHENTICATION_REJECT:
959 fun = rad_authenticate;
962 #ifdef WITH_ACCOUNTING
963 case PW_ACCOUNTING_RESPONSE:
964 fun = rad_accounting;
970 * FIXME: Update MIB for packet types?
972 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
973 "from home server %s port %d - ID %d : IGNORED",
975 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
976 packet->src_port, packet->id);
981 request = received_proxy_response(packet);
986 rad_assert(fun != NULL);
995 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
997 if (!request->reply->code) return 0;
999 rad_encode(request->reply, request->packet,
1000 request->client->secret);
1001 rad_sign(request->reply, request->packet,
1002 request->client->secret);
1008 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1010 if (rad_verify(request->packet, NULL,
1011 request->client->secret) < 0) {
1015 return rad_decode(request->packet, NULL,
1016 request->client->secret);
1020 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1022 rad_encode(request->proxy, NULL, request->home_server->secret);
1023 rad_sign(request->proxy, NULL, request->home_server->secret);
1029 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1031 if (rad_verify(request->proxy_reply, request->proxy,
1032 request->home_server->secret) < 0) {
1036 return rad_decode(request->proxy_reply, request->proxy,
1037 request->home_server->secret);
1043 #include "command.c"
1045 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1047 { common_socket_parse, NULL,
1048 stats_socket_recv, auth_socket_send,
1049 socket_print, client_socket_encode, client_socket_decode },
1051 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1056 { common_socket_parse, NULL,
1057 proxy_socket_recv, proxy_socket_send,
1058 socket_print, proxy_socket_encode, proxy_socket_decode },
1060 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1063 /* authentication */
1064 { common_socket_parse, NULL,
1065 auth_socket_recv, auth_socket_send,
1066 socket_print, client_socket_encode, client_socket_decode },
1068 #ifdef WITH_ACCOUNTING
1070 { common_socket_parse, NULL,
1071 acct_socket_recv, acct_socket_send,
1072 socket_print, client_socket_encode, client_socket_decode},
1074 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1079 { detail_parse, detail_free,
1080 detail_recv, detail_send,
1081 detail_print, detail_encode, detail_decode },
1083 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
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 },
1092 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1096 /* dhcp query protocol */
1097 { dhcp_socket_parse, NULL,
1098 dhcp_socket_recv, dhcp_socket_send,
1099 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1101 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1104 #ifdef WITH_COMMAND_SOCKET
1105 /* TCP command socket */
1106 { command_socket_parse, NULL,
1107 command_domain_accept, command_domain_send,
1108 command_socket_print, command_socket_encode, command_socket_decode },
1110 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1118 * Binds a listener to a socket.
1120 static int listen_bind(rad_listen_t *this)
1122 struct sockaddr_storage salocal;
1124 listen_socket_t *sock = this->data;
1127 * If the port is zero, then it means the appropriate
1128 * thing from /etc/services.
1130 if (sock->port == 0) {
1131 struct servent *svp;
1133 switch (this->type) {
1134 case RAD_LISTEN_AUTH:
1135 svp = getservbyname ("radius", "udp");
1137 sock->port = ntohs(svp->s_port);
1139 sock->port = PW_AUTH_UDP_PORT;
1143 #ifdef WITH_ACCOUNTING
1144 case RAD_LISTEN_ACCT:
1145 svp = getservbyname ("radacct", "udp");
1147 sock->port = ntohs(svp->s_port);
1149 sock->port = PW_ACCT_UDP_PORT;
1155 case RAD_LISTEN_PROXY:
1161 case RAD_LISTEN_VQP:
1167 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1173 * Copy fr_socket() here, as we may need to bind to a device.
1175 this->fd = socket(sock->ipaddr.af, SOCK_DGRAM, 0);
1177 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
1181 #ifdef SO_BINDTODEVICE
1183 * Bind to a device BEFORE touching IP addresses.
1185 if (sock->interface) {
1187 strcpy(ifreq.ifr_name, sock->interface);
1189 if (setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1190 (char *)&ifreq, sizeof(ifreq)) < 0) {
1192 radlog(L_ERR, "Failed opening to interface %s: %s",
1193 sock->interface, strerror(errno));
1195 } /* else it worked. */
1199 #ifdef WITH_UDPFROMTO
1201 * Initialize udpfromto for all sockets.
1203 if (udpfromto_init(this->fd) != 0) {
1210 * Set up sockaddr stuff.
1212 if (!fr_ipaddr2sockaddr(&sock->ipaddr, sock->port, &salocal, &salen)) {
1217 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1218 if (sock->ipaddr.af == AF_INET6) {
1220 * Listening on '::' does NOT get you IPv4 to
1221 * IPv6 mapping. You've got to listen on an IPv4
1222 * address, too. This makes the rest of the server
1223 * design a little simpler.
1227 if (IN6_IS_ADDR_UNSPECIFIED(&sock->ipaddr.ipaddr.ip6addr)) {
1230 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
1231 (char *)&on, sizeof(on));
1233 #endif /* IPV6_V6ONLY */
1234 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
1237 if (bind(this->fd, (struct sockaddr *) &salocal, salen) < 0) {
1239 radlog(L_ERR, "Failed binding to socket: %s\n",
1245 * FreeBSD jail issues. We bind to 0.0.0.0, but the
1246 * kernel instead binds us to a 1.2.3.4. If this
1247 * happens, notice, and remember our real IP.
1250 struct sockaddr_storage src;
1251 socklen_t sizeof_src = sizeof(src);
1253 memset(&src, 0, sizeof_src);
1254 if (getsockname(this->fd, (struct sockaddr *) &src,
1256 radlog(L_ERR, "Failed getting socket name: %s",
1261 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
1262 &sock->ipaddr, &sock->port)) {
1263 radlog(L_ERR, "Socket has unsupported address family");
1272 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
1273 radlog(L_ERR, "Failure getting socket flags: %s)\n",
1278 flags |= O_NONBLOCK;
1279 if( fcntl(this->fd, F_SETFL, flags) < 0) {
1280 radlog(L_ERR, "Failure setting socket flags: %s)\n",
1292 * Allocate & initialize a new listener.
1294 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
1298 this = rad_malloc(sizeof(*this));
1299 memset(this, 0, sizeof(*this));
1302 this->recv = master_listen[this->type].recv;
1303 this->send = master_listen[this->type].send;
1304 this->print = master_listen[this->type].print;
1305 this->encode = master_listen[this->type].encode;
1306 this->decode = master_listen[this->type].decode;
1310 case RAD_LISTEN_NONE:
1312 case RAD_LISTEN_AUTH:
1313 case RAD_LISTEN_ACCT:
1314 case RAD_LISTEN_PROXY:
1315 case RAD_LISTEN_VQP:
1316 case RAD_LISTEN_DHCP:
1317 this->data = rad_malloc(sizeof(listen_socket_t));
1318 memset(this->data, 0, sizeof(listen_socket_t));
1322 case RAD_LISTEN_DETAIL:
1327 #ifdef WITH_COMMAND_SOCKET
1328 case RAD_LISTEN_COMMAND:
1329 this->data = rad_malloc(sizeof(fr_command_socket_t));
1330 memset(this->data, 0, sizeof(fr_command_socket_t));
1335 rad_assert("Unsupported option!" == NULL);
1345 * Externally visible function for creating a new proxy LISTENER.
1347 * For now, don't take ipaddr or port.
1349 * Not thread-safe, but all calls to it are protected by the
1350 * proxy mutex in request_list.c
1352 rad_listen_t *proxy_new_listener()
1354 int last_proxy_port, port;
1355 rad_listen_t *this, *tmp, **last;
1356 listen_socket_t *sock, *old;
1358 this = listen_alloc(RAD_LISTEN_PROXY);
1361 * Find an existing proxy socket to copy.
1363 * FIXME: Make it per-realm, or per-home server!
1365 last_proxy_port = 0;
1367 last = &mainconfig.listen;
1368 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1369 if (tmp->type == RAD_LISTEN_PROXY) {
1371 if (sock->port > last_proxy_port) {
1372 last_proxy_port = sock->port + 1;
1374 if (!old) old = sock;
1377 last = &(tmp->next);
1382 return NULL; /* This is a serious error. */
1386 * FIXME: find a new IP address to listen on?
1388 * This could likely be done in the "home server"
1389 * configuration, to have per-home-server source IP's.
1392 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1395 * Keep going until we find an unused port.
1397 for (port = last_proxy_port; port < 64000; port++) {
1399 if (listen_bind(this) == 0) {
1401 * Add the new listener to the list of
1414 static const FR_NAME_NUMBER listen_compare[] = {
1416 { "status", RAD_LISTEN_NONE },
1418 { "auth", RAD_LISTEN_AUTH },
1419 #ifdef WITH_ACCOUNTING
1420 { "acct", RAD_LISTEN_ACCT },
1423 { "detail", RAD_LISTEN_DETAIL },
1426 { "proxy", RAD_LISTEN_PROXY },
1429 { "vmps", RAD_LISTEN_VQP },
1432 { "dhcp", RAD_LISTEN_DHCP },
1434 #ifdef WITH_COMMAND_SOCKET
1435 { "control", RAD_LISTEN_COMMAND },
1441 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1449 cf_log_info(cs, "listen {");
1451 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1453 if (rcode < 0) return NULL;
1456 cf_log_err(cf_sectiontoitem(cs),
1457 "No type specified in listen section");
1461 type = fr_str2int(listen_compare, listen_type, -1);
1463 cf_log_err(cf_sectiontoitem(cs),
1464 "Invalid type \"%s\" in listen section.",
1472 * Allow listen sections in the default config to
1473 * refer to a server.
1476 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1478 if (rcode == 1) { /* compatiblity with 2.0-pre */
1479 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1482 if (rcode < 0) return NULL;
1486 * Set up cross-type data.
1488 this = listen_alloc(type);
1489 this->server = server;
1493 * Call per-type parser.
1495 if (master_listen[type].parse(cs, this) < 0) {
1500 cf_log_info(cs, "}");
1506 * Generate a list of listeners. Takes an input list of
1507 * listeners, too, so we don't close sockets with waiting packets.
1509 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1511 int override = FALSE;
1513 CONF_SECTION *cs = NULL;
1514 rad_listen_t **last;
1516 fr_ipaddr_t server_ipaddr;
1519 int defined_proxy = 0;
1523 * We shouldn't be called with a pre-existing list.
1525 rad_assert(head && (*head == NULL));
1528 server_ipaddr.af = AF_UNSPEC;
1531 * If the port is specified on the command-line,
1532 * it over-rides the configuration file.
1534 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1536 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1539 * If the IP address was configured on the command-line,
1540 * use that as the "bind_address"
1542 if (mainconfig.myip.af != AF_UNSPEC) {
1543 memcpy(&server_ipaddr, &mainconfig.myip,
1544 sizeof(server_ipaddr));
1550 * Else look for bind_address and/or listen sections.
1552 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1553 rcode = cf_item_parse(config, "bind_address",
1555 &server_ipaddr.ipaddr.ip4addr, NULL);
1556 if (rcode < 0) return -1; /* error parsing it */
1558 if (rcode == 0) { /* successfully parsed IPv4 */
1559 listen_socket_t *sock;
1560 server_ipaddr.af = AF_INET;
1562 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'.");
1566 if (strcmp(progname, "vmpsd") == 0) {
1567 this = listen_alloc(RAD_LISTEN_VQP);
1568 if (!auth_port) auth_port = 1589;
1571 this = listen_alloc(RAD_LISTEN_AUTH);
1575 sock->ipaddr = server_ipaddr;
1576 sock->port = auth_port;
1578 sock->clients = clients_parse_section(config);
1579 if (!sock->clients) {
1580 cf_log_err(cf_sectiontoitem(config),
1581 "Failed to find any clients for this listen section");
1585 if (listen_bind(this) < 0) {
1588 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1591 auth_port = sock->port; /* may have been updated in listen_bind */
1593 cs = cf_section_sub_find_name2(config, "server",
1595 if (cs) this->server = mainconfig.name;
1599 last = &(this->next);
1605 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
1608 #ifdef WITH_ACCOUNTING
1610 * Open Accounting Socket.
1612 * If we haven't already gotten acct_port from
1613 * /etc/services, then make it auth_port + 1.
1615 this = listen_alloc(RAD_LISTEN_ACCT);
1619 * Create the accounting socket.
1621 * The accounting port is always the
1622 * authentication port + 1
1624 sock->ipaddr = server_ipaddr;
1625 sock->port = auth_port + 1;
1627 sock->clients = clients_parse_section(config);
1628 if (!sock->clients) {
1629 cf_log_err(cf_sectiontoitem(config),
1630 "Failed to find any clients for this listen section");
1634 if (listen_bind(this) < 0) {
1637 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1642 cs = cf_section_sub_find_name2(config, "server",
1644 if (cs) this->server = mainconfig.name;
1648 last = &(this->next);
1650 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1651 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1657 * They specified an IP on the command-line, ignore
1658 * all listen sections except the one in '-n'.
1660 if (mainconfig.myip.af != AF_UNSPEC) {
1661 CONF_SECTION *subcs;
1662 const char *name2 = cf_section_name2(cs);
1664 cs = cf_section_sub_find_name2(config, "server",
1666 if (!cs) goto do_proxy;
1669 * Should really abstract this code...
1671 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1673 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1674 this = listen_parse(subcs, name2);
1681 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1685 last = &(this->next);
1686 } /* loop over "listen" directives in server <foo> */
1692 * Walk through the "listen" sections, if they exist.
1694 for (cs = cf_subsection_find_next(config, NULL, "listen");
1696 cs = cf_subsection_find_next(config, cs, "listen")) {
1697 this = listen_parse(cs, NULL);
1704 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1708 last = &(this->next);
1712 * Check virtual servers for "listen" sections, too.
1714 * FIXME: Move to virtual server init?
1716 for (cs = cf_subsection_find_next(config, NULL, "server");
1718 cs = cf_subsection_find_next(config, cs, "server")) {
1719 CONF_SECTION *subcs;
1720 const char *name2 = cf_section_name2(cs);
1722 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1724 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1725 this = listen_parse(subcs, name2);
1732 if (this->type == RAD_LISTEN_PROXY) {
1733 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
1740 last = &(this->next);
1741 } /* loop over "listen" directives in virtual servers */
1742 } /* loop over virtual servers */
1745 * If we're proxying requests, open the proxy FD.
1746 * Otherwise, don't do anything.
1750 if (mainconfig.proxy_requests == TRUE) {
1752 listen_socket_t *sock = NULL;
1755 * No sockets to receive packets, therefore
1756 * proxying is pointless.
1758 if (!*head) return -1;
1760 if (defined_proxy) goto done;
1763 * Find the first authentication port,
1766 for (this = *head; this != NULL; this = this->next) {
1767 if (this->type == RAD_LISTEN_AUTH) {
1769 if (server_ipaddr.af == AF_UNSPEC) {
1770 server_ipaddr = sock->ipaddr;
1772 port = sock->port + 2; /* skip acct port */
1775 if (this->type == RAD_LISTEN_VQP) {
1777 if (server_ipaddr.af == AF_UNSPEC) {
1778 server_ipaddr = sock->ipaddr;
1780 port = sock->port + 1;
1785 if (port < 0) port = 1024 + (fr_rand() & 0x1ff);
1788 * Address is still unspecified, use IPv4.
1790 if (server_ipaddr.af == AF_UNSPEC) {
1791 server_ipaddr.af = AF_INET;
1792 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1795 this = listen_alloc(RAD_LISTEN_PROXY);
1799 * Create the first proxy socket.
1801 sock->ipaddr = server_ipaddr;
1804 * Try to find a proxy port (value doesn't matter)
1806 for (sock->port = port;
1809 if (listen_bind(this) == 0) {
1811 last = &(this->next); /* just in case */
1816 if (sock->port >= 64000) {
1819 radlog(L_ERR, "Failed to open socket for proxying");
1824 done: /* used only in proxy code. */
1831 * Free a linked list of listeners;
1833 void listen_free(rad_listen_t **head)
1837 if (!head || !*head) return;
1841 rad_listen_t *next = this->next;
1844 * Other code may have eaten the FD.
1846 if (this->fd >= 0) close(this->fd);
1848 if (master_listen[this->type].free) {
1849 master_listen[this->type].free(this);
1861 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
1866 for (this = mainconfig.listen; this != NULL; this = this->next) {
1867 listen_socket_t *sock;
1869 if ((this->type != RAD_LISTEN_AUTH) &&
1870 (this->type != RAD_LISTEN_ACCT)) continue;
1874 if ((sock->port == port) &&
1875 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {
1876 return sock->clients;
1883 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
1887 for (this = mainconfig.listen; this != NULL; this = this->next) {
1888 listen_socket_t *sock;
1890 if ((this->type != RAD_LISTEN_AUTH) &&
1891 (this->type != RAD_LISTEN_ACCT)) continue;
1895 if ((sock->port == port) &&
1896 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {