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>
37 #include <freeradius-devel/udpfromto.h>
40 #ifdef HAVE_SYS_RESOURCE_H
41 #include <sys/resource.h>
54 * We'll use this below.
56 typedef int (*rad_listen_parse_t)(CONF_SECTION *, rad_listen_t *);
57 typedef void (*rad_listen_free_t)(rad_listen_t *);
59 typedef struct rad_listen_master_t {
60 rad_listen_parse_t parse;
61 rad_listen_free_t free;
62 rad_listen_recv_t recv;
63 rad_listen_send_t send;
64 rad_listen_print_t print;
65 rad_listen_encode_t encode;
66 rad_listen_decode_t decode;
67 } rad_listen_master_t;
69 typedef struct listen_socket_t {
75 #ifdef SO_BINDTODEVICE
76 const char *interface;
78 RADCLIENT_LIST *clients;
81 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type);
84 * Find a per-socket client.
86 RADCLIENT *client_listener_find(const rad_listen_t *listener,
87 const fr_ipaddr_t *ipaddr, int src_port)
89 #ifdef WITH_DYNAMIC_CLIENTS
91 listen_socket_t *sock;
97 RADCLIENT_LIST *clients;
99 rad_assert(listener != NULL);
100 rad_assert(ipaddr != NULL);
102 clients = ((listen_socket_t *)listener->data)->clients;
105 * This HAS to have been initialized previously.
107 rad_assert(clients != NULL);
109 client = client_find(clients, ipaddr);
111 static time_t last_printed = 0;
112 char name[256], buffer[128];
114 #ifdef WITH_DYNAMIC_CLIENTS
115 unknown: /* used only for dynamic clients */
119 * DoS attack quenching, but only in debug mode.
120 * If they're running in debug mode, show them
123 if (debug_flag == 0) {
125 if (last_printed == now) return NULL;
130 listener->print(listener, name, sizeof(name));
132 radlog(L_ERR, "Ignoring request to %s from unknown client %s port %d",
133 name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
134 buffer, sizeof(buffer)),
139 #ifndef WITH_DYNAMIC_CLIENTS
140 return client; /* return the found client. */
144 * No server defined, and it's not dynamic. Return it.
146 if (!client->client_server && !client->dynamic) return client;
151 * It's a dynamically generated client, check it.
153 if (client->dynamic && (src_port != 0)) {
155 * Lives forever. Return it.
157 if (client->lifetime == 0) return client;
160 * Rate-limit the deletion of known clients.
161 * This makes them last a little longer, but
162 * prevents the server from melting down if (say)
163 * 10k clients all expire at once.
165 if (now == client->last_new_client) return client;
168 * It's not dead yet. Return it.
170 if ((client->created + client->lifetime) > now) return client;
173 * This really puts them onto a queue for later
176 client_delete(clients, client);
179 * Go find the enclosing network again.
181 client = client_find(clients, ipaddr);
186 if (!client) goto unknown;
187 if (!client->client_server) goto unknown;
190 * At this point, 'client' is the enclosing
191 * network that configures where dynamic clients
194 rad_assert(client->dynamic == 0);
197 * The IP is unknown, so we've found an enclosing
198 * network. Enable DoS protection. We only
199 * allow one new client per second. Known
200 * clients aren't subject to this restriction.
202 if (now == client->last_new_client) goto unknown;
205 client->last_new_client = now;
207 request = request_alloc();
208 if (!request) goto unknown;
210 request->listener = listener;
211 request->client = client;
212 request->packet = rad_alloc(0);
213 if (!request->packet) {
214 request_free(&request);
217 request->reply = rad_alloc(0);
218 if (!request->reply) {
219 request_free(&request);
222 request->packet->timestamp = request->timestamp;
224 request->priority = listener->type;
225 request->server = client->client_server;
226 request->root = &mainconfig;
229 * Run a fake request through the given virtual server.
230 * Look for FreeRADIUS-Client-IP-Address
231 * FreeRADIUS-Client-Secret
234 * and create the RADCLIENT structure from that.
237 sock = listener->data;
238 request->packet->sockfd = listener->fd;
239 request->packet->src_ipaddr = *ipaddr;
240 request->packet->src_port = 0; /* who cares... */
241 request->packet->dst_ipaddr = sock->ipaddr;
242 request->packet->dst_port = sock->port;
244 request->reply->sockfd = request->packet->sockfd;
245 request->reply->dst_ipaddr = request->packet->src_ipaddr;
246 request->reply->src_ipaddr = request->packet->dst_ipaddr;
247 request->reply->dst_port = request->packet->src_port;
248 request->reply->src_port = request->packet->dst_port;
249 request->reply->id = request->packet->id;
250 request->reply->code = 0; /* UNKNOWN code */
253 DEBUG("server %s {", request->server);
255 rcode = module_authorize(0, request);
257 DEBUG("} # server %s", request->server);
259 if (rcode != RLM_MODULE_OK) {
260 request_free(&request);
265 * If the client was updated by rlm_dynamic_clients,
266 * don't create the client from attribute-value pairs.
268 if (request->client == client) {
269 created = client_create(clients, request);
271 created = request->client;
274 * This frees the client if it isn't valid.
276 if (!client_validate(clients, client, created)) goto unknown;
278 request_free(&request);
280 if (!created) goto unknown;
286 static int listen_bind(rad_listen_t *this);
290 * Process and reply to a server-status request.
291 * Like rad_authenticate and rad_accounting this should
292 * live in it's own file but it's so small we don't bother.
294 static int rad_status_server(REQUEST *request)
296 int rcode = RLM_MODULE_OK;
299 switch (request->listener->type) {
301 case RAD_LISTEN_NONE:
303 case RAD_LISTEN_AUTH:
304 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
306 rcode = module_authorize(dval->value, request);
308 rcode = RLM_MODULE_OK;
313 case RLM_MODULE_UPDATED:
314 request->reply->code = PW_AUTHENTICATION_ACK;
317 case RLM_MODULE_FAIL:
318 case RLM_MODULE_HANDLED:
319 request->reply->code = 0; /* don't reply */
323 case RLM_MODULE_REJECT:
324 request->reply->code = PW_AUTHENTICATION_REJECT;
329 #ifdef WITH_ACCOUNTING
330 case RAD_LISTEN_ACCT:
331 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
333 rcode = module_accounting(dval->value, request);
335 rcode = RLM_MODULE_OK;
340 case RLM_MODULE_UPDATED:
341 request->reply->code = PW_ACCOUNTING_RESPONSE;
345 request->reply->code = 0; /* don't reply */
357 * Full statistics are available only on a statistics
360 if (request->listener->type == RAD_LISTEN_NONE) {
361 request_stats_reply(request);
369 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
372 listen_socket_t *sock = this->data;
375 switch (this->type) {
377 case RAD_LISTEN_NONE: /* what a hack... */
382 case RAD_LISTEN_AUTH:
383 name = "authentication";
386 #ifdef WITH_ACCOUNTING
387 case RAD_LISTEN_ACCT:
393 case RAD_LISTEN_PROXY:
405 case RAD_LISTEN_DHCP:
415 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
416 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
420 #ifdef SO_BINDTODEVICE
421 if (sock->interface) {
422 ADDSTRING(" interface ");
423 ADDSTRING(sock->interface);
427 ADDSTRING(" address ");
429 if ((sock->ipaddr.af == AF_INET) &&
430 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
431 strlcpy(buffer, "*", bufsize);
433 ip_ntoh(&sock->ipaddr, buffer, bufsize);
438 snprintf(buffer, bufsize, "%d", sock->port);
442 ADDSTRING(" as server ");
443 ADDSTRING(this->server);
454 * Parse an authentication or accounting socket.
456 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
461 listen_socket_t *sock = this->data;
462 char *section_name = NULL;
463 CONF_SECTION *client_cs, *parentcs;
468 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
469 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
470 &ipaddr.ipaddr.ip4addr, NULL);
471 if (rcode < 0) return -1;
473 if (rcode == 0) { /* successfully parsed IPv4 */
476 } else { /* maybe IPv6? */
477 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
478 &ipaddr.ipaddr.ip6addr, NULL);
479 if (rcode < 0) return -1;
482 cf_log_err(cf_sectiontoitem(cs),
483 "No address specified in listen section");
486 ipaddr.af = AF_INET6;
489 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
491 if (rcode < 0) return -1;
493 if ((listen_port < 0) || (listen_port > 65535)) {
494 cf_log_err(cf_sectiontoitem(cs),
495 "Invalid value for \"port\"");
499 sock->ipaddr = ipaddr;
500 sock->port = listen_port;
503 * If we can bind to interfaces, do so,
506 if (cf_pair_find(cs, "interface")) {
507 #ifndef SO_BINDTODEVICE
508 cf_log_err(cf_sectiontoitem(cs),
509 "System does not support binding to interfaces. Delete this line from the configuration file.");
513 CONF_PAIR *cp = cf_pair_find(cs, "interface");
515 rad_assert(cp != NULL);
516 value = cf_pair_value(cp);
518 cf_log_err(cf_sectiontoitem(cs),
519 "No interface name given");
522 sock->interface = value;
527 * And bind it to the port.
529 if (listen_bind(this) < 0) {
531 cf_log_err(cf_sectiontoitem(cs),
532 "Error binding to port for %s port %d",
533 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
540 * Proxy sockets don't have clients.
542 if (this->type == RAD_LISTEN_PROXY) return 0;
546 * The more specific configurations are preferred to more
550 parentcs = cf_top_section(cs);
551 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
552 §ion_name, NULL);
553 if (rcode < 0) return -1; /* bad string */
556 * Explicit list given: use it.
558 client_cs = cf_section_sub_find_name2(parentcs,
562 client_cs = cf_section_find(section_name);
565 cf_log_err(cf_sectiontoitem(cs),
566 "Failed to find clients %s {...}",
572 } /* else there was no "clients = " entry. */
575 CONF_SECTION *server_cs;
577 server_cs = cf_section_sub_find_name2(parentcs,
581 * Found a "server foo" section. If there are clients
585 (cf_section_sub_find(server_cs, "client") != NULL)) {
586 client_cs = server_cs;
591 * Still nothing. Look for global clients.
593 if (!client_cs) client_cs = parentcs;
595 sock->clients = clients_parse_section(client_cs);
596 if (!sock->clients) {
597 cf_log_err(cf_sectiontoitem(cs),
598 "Failed to load clients for this listen section");
606 * Send an authentication response packet
608 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
610 rad_assert(request->listener == listener);
611 rad_assert(listener->send == auth_socket_send);
613 return rad_send(request->reply, request->packet,
614 request->client->secret);
618 #ifdef WITH_ACCOUNTING
620 * Send an accounting response packet (or not)
622 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
624 rad_assert(request->listener == listener);
625 rad_assert(listener->send == acct_socket_send);
628 * Accounting reject's are silently dropped.
630 * We do it here to avoid polluting the rest of the
631 * code with this knowledge
633 if (request->reply->code == 0) return 0;
635 return rad_send(request->reply, request->packet,
636 request->client->secret);
642 * Send a packet to a home server.
644 * FIXME: have different code for proxy auth & acct!
646 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
648 listen_socket_t *sock = listener->data;
650 rad_assert(request->proxy_listener == listener);
651 rad_assert(listener->send == proxy_socket_send);
653 request->proxy->src_ipaddr = sock->ipaddr;
654 request->proxy->src_port = sock->port;
656 return rad_send(request->proxy, request->packet,
657 request->home_server->secret);
663 * Check if an incoming request is "ok"
665 * It takes packets, not requests. It sees if the packet looks
666 * OK. If so, it does a number of sanity checks on it.
668 static int stats_socket_recv(rad_listen_t *listener,
669 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
673 RADIUS_PACKET *packet;
675 fr_ipaddr_t src_ipaddr;
677 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
678 if (rcode < 0) return 0;
680 RAD_STATS_TYPE_INC(listener, total_requests);
682 if (rcode < 20) { /* AUTH_HDR_LEN */
683 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
687 if ((client = client_listener_find(listener,
688 &src_ipaddr, src_port)) == NULL) {
689 rad_recv_discard(listener->fd);
690 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
695 * We only understand Status-Server on this socket.
697 if (code != PW_STATUS_SERVER) {
698 DEBUG("Ignoring packet code %d sent to Status-Server port",
700 rad_recv_discard(listener->fd);
701 RAD_STATS_TYPE_INC(listener, total_unknown_types);
702 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
707 * Now that we've sanity checked everything, receive the
710 packet = rad_recv(listener->fd, 1); /* require message authenticator */
712 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
713 DEBUG("%s", fr_strerror());
717 if (!received_request(listener, packet, prequest, client)) {
718 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
719 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
724 *pfun = rad_status_server;
731 * Check if an incoming request is "ok"
733 * It takes packets, not requests. It sees if the packet looks
734 * OK. If so, it does a number of sanity checks on it.
736 static int auth_socket_recv(rad_listen_t *listener,
737 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
741 RADIUS_PACKET *packet;
742 RAD_REQUEST_FUNP fun = NULL;
744 fr_ipaddr_t src_ipaddr;
746 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
747 if (rcode < 0) return 0;
749 RAD_STATS_TYPE_INC(listener, total_requests);
751 if (rcode < 20) { /* AUTH_HDR_LEN */
752 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
756 if ((client = client_listener_find(listener,
757 &src_ipaddr, src_port)) == NULL) {
758 rad_recv_discard(listener->fd);
759 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
764 * Some sanity checks, based on the packet code.
767 case PW_AUTHENTICATION_REQUEST:
768 RAD_STATS_CLIENT_INC(listener, client, total_requests);
769 fun = rad_authenticate;
772 case PW_STATUS_SERVER:
773 if (!mainconfig.status_server) {
774 rad_recv_discard(listener->fd);
775 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
776 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
777 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
780 fun = rad_status_server;
784 rad_recv_discard(listener->fd);
785 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
786 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
788 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
789 code, client->shortname, src_port);
792 } /* switch over packet types */
795 * Now that we've sanity checked everything, receive the
798 packet = rad_recv(listener->fd, client->message_authenticator);
800 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
801 DEBUG("%s", fr_strerror());
805 if (!received_request(listener, packet, prequest, client)) {
806 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
807 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
817 #ifdef WITH_ACCOUNTING
819 * Receive packets from an accounting socket
821 static int acct_socket_recv(rad_listen_t *listener,
822 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
826 RADIUS_PACKET *packet;
827 RAD_REQUEST_FUNP fun = NULL;
829 fr_ipaddr_t src_ipaddr;
831 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
832 if (rcode < 0) return 0;
834 RAD_STATS_TYPE_INC(listener, total_requests);
836 if (rcode < 20) { /* AUTH_HDR_LEN */
837 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
841 if ((client = client_listener_find(listener,
842 &src_ipaddr, src_port)) == NULL) {
843 rad_recv_discard(listener->fd);
844 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
849 * Some sanity checks, based on the packet code.
852 case PW_ACCOUNTING_REQUEST:
853 RAD_STATS_CLIENT_INC(listener, client, total_requests);
854 fun = rad_accounting;
857 case PW_STATUS_SERVER:
858 if (!mainconfig.status_server) {
859 rad_recv_discard(listener->fd);
860 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
861 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
863 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
866 fun = rad_status_server;
870 rad_recv_discard(listener->fd);
871 RAD_STATS_TYPE_INC(listener, total_unknown_types);
872 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
874 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
875 code, client->shortname, src_port);
877 } /* switch over packet types */
880 * Now that we've sanity checked everything, receive the
883 packet = rad_recv(listener->fd, 0);
885 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
886 radlog(L_ERR, "%s", fr_strerror());
891 * There can be no duplicate accounting packets.
893 if (!received_request(listener, packet, prequest, client)) {
894 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
895 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
908 * For now, all CoA requests are *only* originated, and not
909 * proxied. So all of the necessary work is done in the
910 * post-proxy section, which is automatically handled by event.c.
911 * As a result, we don't have to do anything here.
913 static int rad_coa_reply(REQUEST *request)
918 * Inform the user about RFC requirements.
920 s1 = pairfind(request->proxy->vps, PW_STATE);
922 s2 = pairfind(request->proxy_reply->vps, PW_STATE);
925 DEBUG("WARNING: Client was sent State in CoA, and did not respond with State.");
927 } else if ((s1->length != s2->length) ||
928 (memcmp(s1->vp_octets, s2->vp_octets,
930 DEBUG("WARNING: Client was sent State in CoA, and did not respond with the same State.");
934 return RLM_MODULE_OK;
940 * Recieve packets from a proxy socket.
942 static int proxy_socket_recv(rad_listen_t *listener,
943 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
946 RADIUS_PACKET *packet;
947 RAD_REQUEST_FUNP fun = NULL;
950 packet = rad_recv(listener->fd, 0);
952 radlog(L_ERR, "%s", fr_strerror());
957 * FIXME: Client MIB updates?
959 switch(packet->code) {
960 case PW_AUTHENTICATION_ACK:
961 case PW_ACCESS_CHALLENGE:
962 case PW_AUTHENTICATION_REJECT:
963 fun = rad_authenticate;
966 #ifdef WITH_ACCOUNTING
967 case PW_ACCOUNTING_RESPONSE:
968 fun = rad_accounting;
973 case PW_DISCONNECT_ACK:
974 case PW_DISCONNECT_NAK:
983 * FIXME: Update MIB for packet types?
985 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
986 "from home server %s port %d - ID %d : IGNORED",
988 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
989 packet->src_port, packet->id);
994 request = received_proxy_response(packet);
1000 rad_assert(fun != NULL);
1002 *prequest = request;
1009 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1011 if (!request->reply->code) return 0;
1013 rad_encode(request->reply, request->packet,
1014 request->client->secret);
1015 rad_sign(request->reply, request->packet,
1016 request->client->secret);
1022 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1024 if (rad_verify(request->packet, NULL,
1025 request->client->secret) < 0) {
1029 return rad_decode(request->packet, NULL,
1030 request->client->secret);
1034 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1036 rad_encode(request->proxy, NULL, request->home_server->secret);
1037 rad_sign(request->proxy, NULL, request->home_server->secret);
1043 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1046 * rad_verify is run in event.c, received_proxy_response()
1049 return rad_decode(request->proxy_reply, request->proxy,
1050 request->home_server->secret);
1056 #include "command.c"
1058 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1060 { common_socket_parse, NULL,
1061 stats_socket_recv, auth_socket_send,
1062 socket_print, client_socket_encode, client_socket_decode },
1065 * This always gets defined.
1067 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1072 { common_socket_parse, NULL,
1073 proxy_socket_recv, proxy_socket_send,
1074 socket_print, proxy_socket_encode, proxy_socket_decode },
1077 /* authentication */
1078 { common_socket_parse, NULL,
1079 auth_socket_recv, auth_socket_send,
1080 socket_print, client_socket_encode, client_socket_decode },
1082 #ifdef WITH_ACCOUNTING
1084 { common_socket_parse, NULL,
1085 acct_socket_recv, acct_socket_send,
1086 socket_print, client_socket_encode, client_socket_decode},
1091 { detail_parse, detail_free,
1092 detail_recv, detail_send,
1093 detail_print, detail_encode, detail_decode },
1097 /* vlan query protocol */
1098 { common_socket_parse, NULL,
1099 vqp_socket_recv, vqp_socket_send,
1100 socket_print, vqp_socket_encode, vqp_socket_decode },
1104 /* dhcp query protocol */
1105 { dhcp_socket_parse, NULL,
1106 dhcp_socket_recv, dhcp_socket_send,
1107 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1110 #ifdef WITH_COMMAND_SOCKET
1111 /* TCP command socket */
1112 { command_socket_parse, NULL,
1113 command_domain_accept, command_domain_send,
1114 command_socket_print, command_socket_encode, command_socket_decode },
1122 * Binds a listener to a socket.
1124 static int listen_bind(rad_listen_t *this)
1127 struct sockaddr_storage salocal;
1129 listen_socket_t *sock = this->data;
1132 * If the port is zero, then it means the appropriate
1133 * thing from /etc/services.
1135 if (sock->port == 0) {
1136 struct servent *svp;
1138 switch (this->type) {
1139 case RAD_LISTEN_AUTH:
1140 svp = getservbyname ("radius", "udp");
1142 sock->port = ntohs(svp->s_port);
1144 sock->port = PW_AUTH_UDP_PORT;
1148 #ifdef WITH_ACCOUNTING
1149 case RAD_LISTEN_ACCT:
1150 svp = getservbyname ("radacct", "udp");
1152 sock->port = ntohs(svp->s_port);
1154 sock->port = PW_ACCT_UDP_PORT;
1160 case RAD_LISTEN_PROXY:
1166 case RAD_LISTEN_VQP:
1172 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1178 * Copy fr_socket() here, as we may need to bind to a device.
1180 this->fd = socket(sock->ipaddr.af, SOCK_DGRAM, 0);
1182 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
1186 #ifdef SO_BINDTODEVICE
1188 * Bind to a device BEFORE touching IP addresses.
1190 if (sock->interface) {
1192 strcpy(ifreq.ifr_name, sock->interface);
1195 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1196 (char *)&ifreq, sizeof(ifreq));
1200 radlog(L_ERR, "Failed binding to interface %s: %s",
1201 sock->interface, strerror(errno));
1203 } /* else it worked. */
1207 #ifdef WITH_UDPFROMTO
1209 * Initialize udpfromto for all sockets.
1211 if (udpfromto_init(this->fd) != 0) {
1218 * Set up sockaddr stuff.
1220 if (!fr_ipaddr2sockaddr(&sock->ipaddr, sock->port, &salocal, &salen)) {
1225 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1226 if (sock->ipaddr.af == AF_INET6) {
1228 * Listening on '::' does NOT get you IPv4 to
1229 * IPv6 mapping. You've got to listen on an IPv4
1230 * address, too. This makes the rest of the server
1231 * design a little simpler.
1235 if (IN6_IS_ADDR_UNSPECIFIED(&sock->ipaddr.ipaddr.ip6addr)) {
1238 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
1239 (char *)&on, sizeof(on));
1241 #endif /* IPV6_V6ONLY */
1243 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
1246 * May be binding to priviledged ports.
1249 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
1253 radlog(L_ERR, "Failed binding to socket: %s\n",
1259 * FreeBSD jail issues. We bind to 0.0.0.0, but the
1260 * kernel instead binds us to a 1.2.3.4. If this
1261 * happens, notice, and remember our real IP.
1264 struct sockaddr_storage src;
1265 socklen_t sizeof_src = sizeof(src);
1267 memset(&src, 0, sizeof_src);
1268 if (getsockname(this->fd, (struct sockaddr *) &src,
1270 radlog(L_ERR, "Failed getting socket name: %s",
1275 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
1276 &sock->ipaddr, &sock->port)) {
1277 radlog(L_ERR, "Socket has unsupported address family");
1286 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
1287 radlog(L_ERR, "Failure getting socket flags: %s)\n",
1292 flags |= O_NONBLOCK;
1293 if( fcntl(this->fd, F_SETFL, flags) < 0) {
1294 radlog(L_ERR, "Failure setting socket flags: %s)\n",
1306 * Allocate & initialize a new listener.
1308 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
1312 this = rad_malloc(sizeof(*this));
1313 memset(this, 0, sizeof(*this));
1316 this->recv = master_listen[this->type].recv;
1317 this->send = master_listen[this->type].send;
1318 this->print = master_listen[this->type].print;
1319 this->encode = master_listen[this->type].encode;
1320 this->decode = master_listen[this->type].decode;
1324 case RAD_LISTEN_NONE:
1326 case RAD_LISTEN_AUTH:
1327 #ifdef WITH_ACCOUNTING
1328 case RAD_LISTEN_ACCT:
1331 case RAD_LISTEN_PROXY:
1334 case RAD_LISTEN_VQP:
1337 case RAD_LISTEN_DHCP:
1339 this->data = rad_malloc(sizeof(listen_socket_t));
1340 memset(this->data, 0, sizeof(listen_socket_t));
1344 case RAD_LISTEN_DETAIL:
1349 #ifdef WITH_COMMAND_SOCKET
1350 case RAD_LISTEN_COMMAND:
1351 this->data = rad_malloc(sizeof(fr_command_socket_t));
1352 memset(this->data, 0, sizeof(fr_command_socket_t));
1357 rad_assert("Unsupported option!" == NULL);
1367 * Externally visible function for creating a new proxy LISTENER.
1369 * For now, don't take ipaddr or port.
1371 * Not thread-safe, but all calls to it are protected by the
1372 * proxy mutex in request_list.c
1374 rad_listen_t *proxy_new_listener()
1376 int last_proxy_port, port;
1377 rad_listen_t *this, *tmp, **last;
1378 listen_socket_t *sock, *old;
1380 this = listen_alloc(RAD_LISTEN_PROXY);
1383 * Find an existing proxy socket to copy.
1385 * FIXME: Make it per-realm, or per-home server!
1387 last_proxy_port = 0;
1389 last = &mainconfig.listen;
1390 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1391 if (tmp->type == RAD_LISTEN_PROXY) {
1393 if (sock->port > last_proxy_port) {
1394 last_proxy_port = sock->port + 1;
1396 if (!old) old = sock;
1399 last = &(tmp->next);
1404 return NULL; /* This is a serious error. */
1408 * FIXME: find a new IP address to listen on?
1410 * This could likely be done in the "home server"
1411 * configuration, to have per-home-server source IP's.
1414 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1417 * Keep going until we find an unused port.
1419 for (port = last_proxy_port; port < 64000; port++) {
1421 if (listen_bind(this) == 0) {
1423 * Add the new listener to the list of
1436 static const FR_NAME_NUMBER listen_compare[] = {
1438 { "status", RAD_LISTEN_NONE },
1440 { "auth", RAD_LISTEN_AUTH },
1441 #ifdef WITH_ACCOUNTING
1442 { "acct", RAD_LISTEN_ACCT },
1445 { "detail", RAD_LISTEN_DETAIL },
1448 { "proxy", RAD_LISTEN_PROXY },
1451 { "vmps", RAD_LISTEN_VQP },
1454 { "dhcp", RAD_LISTEN_DHCP },
1456 #ifdef WITH_COMMAND_SOCKET
1457 { "control", RAD_LISTEN_COMMAND },
1463 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1471 cf_log_info(cs, "listen {");
1473 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1475 if (rcode < 0) return NULL;
1478 cf_log_err(cf_sectiontoitem(cs),
1479 "No type specified in listen section");
1483 type = fr_str2int(listen_compare, listen_type, -1);
1485 cf_log_err(cf_sectiontoitem(cs),
1486 "Invalid type \"%s\" in listen section.",
1494 * Allow listen sections in the default config to
1495 * refer to a server.
1498 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1500 if (rcode == 1) { /* compatiblity with 2.0-pre */
1501 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1504 if (rcode < 0) return NULL;
1508 * Set up cross-type data.
1510 this = listen_alloc(type);
1511 this->server = server;
1515 * Call per-type parser.
1517 if (master_listen[type].parse(cs, this) < 0) {
1522 cf_log_info(cs, "}");
1528 * Generate a list of listeners. Takes an input list of
1529 * listeners, too, so we don't close sockets with waiting packets.
1531 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1533 int override = FALSE;
1535 CONF_SECTION *cs = NULL;
1536 rad_listen_t **last;
1538 fr_ipaddr_t server_ipaddr;
1541 int defined_proxy = 0;
1545 * We shouldn't be called with a pre-existing list.
1547 rad_assert(head && (*head == NULL));
1550 server_ipaddr.af = AF_UNSPEC;
1553 * If the port is specified on the command-line,
1554 * it over-rides the configuration file.
1556 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1558 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1561 * If the IP address was configured on the command-line,
1562 * use that as the "bind_address"
1564 if (mainconfig.myip.af != AF_UNSPEC) {
1565 memcpy(&server_ipaddr, &mainconfig.myip,
1566 sizeof(server_ipaddr));
1572 * Else look for bind_address and/or listen sections.
1574 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1575 rcode = cf_item_parse(config, "bind_address",
1577 &server_ipaddr.ipaddr.ip4addr, NULL);
1578 if (rcode < 0) return -1; /* error parsing it */
1580 if (rcode == 0) { /* successfully parsed IPv4 */
1581 listen_socket_t *sock;
1582 server_ipaddr.af = AF_INET;
1584 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'.");
1588 if (strcmp(progname, "vmpsd") == 0) {
1589 this = listen_alloc(RAD_LISTEN_VQP);
1590 if (!auth_port) auth_port = 1589;
1593 this = listen_alloc(RAD_LISTEN_AUTH);
1597 sock->ipaddr = server_ipaddr;
1598 sock->port = auth_port;
1600 sock->clients = clients_parse_section(config);
1601 if (!sock->clients) {
1602 cf_log_err(cf_sectiontoitem(config),
1603 "Failed to find any clients for this listen section");
1608 if (listen_bind(this) < 0) {
1610 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1614 auth_port = sock->port; /* may have been updated in listen_bind */
1616 cs = cf_section_sub_find_name2(config, "server",
1618 if (cs) this->server = mainconfig.name;
1622 last = &(this->next);
1628 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
1631 #ifdef WITH_ACCOUNTING
1633 * Open Accounting Socket.
1635 * If we haven't already gotten acct_port from
1636 * /etc/services, then make it auth_port + 1.
1638 this = listen_alloc(RAD_LISTEN_ACCT);
1642 * Create the accounting socket.
1644 * The accounting port is always the
1645 * authentication port + 1
1647 sock->ipaddr = server_ipaddr;
1648 sock->port = auth_port + 1;
1650 sock->clients = clients_parse_section(config);
1651 if (!sock->clients) {
1652 cf_log_err(cf_sectiontoitem(config),
1653 "Failed to find any clients for this listen section");
1657 if (listen_bind(this) < 0) {
1660 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1665 cs = cf_section_sub_find_name2(config, "server",
1667 if (cs) this->server = mainconfig.name;
1671 last = &(this->next);
1673 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1674 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1680 * They specified an IP on the command-line, ignore
1681 * all listen sections except the one in '-n'.
1683 if (mainconfig.myip.af != AF_UNSPEC) {
1684 CONF_SECTION *subcs;
1685 const char *name2 = cf_section_name2(cs);
1687 cs = cf_section_sub_find_name2(config, "server",
1689 if (!cs) goto do_proxy;
1692 * Should really abstract this code...
1694 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1696 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1697 this = listen_parse(subcs, name2);
1704 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1708 last = &(this->next);
1709 } /* loop over "listen" directives in server <foo> */
1715 * Walk through the "listen" sections, if they exist.
1717 for (cs = cf_subsection_find_next(config, NULL, "listen");
1719 cs = cf_subsection_find_next(config, cs, "listen")) {
1720 this = listen_parse(cs, NULL);
1727 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1731 last = &(this->next);
1735 * Check virtual servers for "listen" sections, too.
1737 * FIXME: Move to virtual server init?
1739 for (cs = cf_subsection_find_next(config, NULL, "server");
1741 cs = cf_subsection_find_next(config, cs, "server")) {
1742 CONF_SECTION *subcs;
1743 const char *name2 = cf_section_name2(cs);
1745 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1747 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1748 this = listen_parse(subcs, name2);
1755 if (this->type == RAD_LISTEN_PROXY) {
1756 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
1763 last = &(this->next);
1764 } /* loop over "listen" directives in virtual servers */
1765 } /* loop over virtual servers */
1768 * If we're proxying requests, open the proxy FD.
1769 * Otherwise, don't do anything.
1773 if (mainconfig.proxy_requests == TRUE) {
1775 listen_socket_t *sock = NULL;
1778 * No sockets to receive packets, therefore
1779 * proxying is pointless.
1781 if (!*head) return -1;
1783 if (defined_proxy) goto done;
1786 * Find the first authentication port,
1789 for (this = *head; this != NULL; this = this->next) {
1790 if (this->type == RAD_LISTEN_AUTH) {
1792 if (server_ipaddr.af == AF_UNSPEC) {
1793 server_ipaddr = sock->ipaddr;
1795 port = sock->port + 2; /* skip acct port */
1799 if (this->type == RAD_LISTEN_VQP) {
1801 if (server_ipaddr.af == AF_UNSPEC) {
1802 server_ipaddr = sock->ipaddr;
1804 port = sock->port + 1;
1810 if (port < 0) port = 1024 + (fr_rand() & 0x1ff);
1813 * Address is still unspecified, use IPv4.
1815 if (server_ipaddr.af == AF_UNSPEC) {
1816 server_ipaddr.af = AF_INET;
1817 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1820 this = listen_alloc(RAD_LISTEN_PROXY);
1824 * Create the first proxy socket.
1826 sock->ipaddr = server_ipaddr;
1829 * Try to find a proxy port (value doesn't matter)
1831 for (sock->port = port;
1834 if (listen_bind(this) == 0) {
1836 last = &(this->next); /* just in case */
1841 if (sock->port >= 64000) {
1844 radlog(L_ERR, "Failed to open socket for proxying");
1849 done: /* used only in proxy code. */
1856 * Free a linked list of listeners;
1858 void listen_free(rad_listen_t **head)
1862 if (!head || !*head) return;
1866 rad_listen_t *next = this->next;
1869 * Other code may have eaten the FD.
1871 if (this->fd >= 0) close(this->fd);
1873 if (master_listen[this->type].free) {
1874 master_listen[this->type].free(this);
1886 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
1891 for (this = mainconfig.listen; this != NULL; this = this->next) {
1892 listen_socket_t *sock;
1894 if ((this->type != RAD_LISTEN_AUTH) &&
1895 (this->type != RAD_LISTEN_ACCT)) continue;
1899 if ((sock->port == port) &&
1900 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {
1901 return sock->clients;
1909 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
1913 for (this = mainconfig.listen; this != NULL; this = this->next) {
1914 listen_socket_t *sock;
1917 * FIXME: For TCP, ignore the *secondary*
1918 * listeners associated with the main socket.
1920 if ((this->type != RAD_LISTEN_AUTH) &&
1921 (this->type != RAD_LISTEN_ACCT)) continue;
1925 if ((sock->port == port) &&
1926 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {
1930 if ((sock->port == port) &&
1931 ((sock->ipaddr.af == AF_INET) &&
1932 (sock->ipaddr.ipaddr.ip4addr.s_addr == INADDR_ANY))) {
1936 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1937 if ((sock->port == port) &&
1938 (sock->ipaddr.af == AF_INET6) &&
1939 (IN6_IS_ADDR_UNSPECIFIED(&sock->ipaddr.ipaddr.ip6addr))) {