2 * listen.c Handle socket stuff
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2005,2006 The FreeRADIUS server project
21 * Copyright 2005 Alan DeKok <aland@ox.org>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29 #include <freeradius-devel/rad_assert.h>
30 #include <freeradius-devel/vqp.h>
31 #include <freeradius-devel/dhcp.h>
33 #include <freeradius-devel/vmps.h>
34 #include <freeradius-devel/detail.h>
36 #ifdef HAVE_SYS_RESOURCE_H
37 #include <sys/resource.h>
50 * We'll use this below.
52 typedef int (*rad_listen_parse_t)(CONF_SECTION *, rad_listen_t *);
53 typedef void (*rad_listen_free_t)(rad_listen_t *);
55 typedef struct rad_listen_master_t {
56 rad_listen_parse_t parse;
57 rad_listen_free_t free;
58 rad_listen_recv_t recv;
59 rad_listen_send_t send;
60 rad_listen_print_t print;
61 rad_listen_encode_t encode;
62 rad_listen_decode_t decode;
63 } rad_listen_master_t;
65 typedef struct listen_socket_t {
71 #ifdef SO_BINDTODEVICE
72 const char *interface;
74 RADCLIENT_LIST *clients;
78 * Find a per-socket client.
80 RADCLIENT *client_listener_find(const rad_listen_t *listener,
81 const fr_ipaddr_t *ipaddr)
83 #ifdef WITH_DYNAMIC_CLIENTS
86 listen_socket_t *sock;
88 RADCLIENT *client, *created;
90 RADCLIENT_LIST *clients;
92 rad_assert(listener != NULL);
93 rad_assert(ipaddr != NULL);
95 rad_assert((listener->type == RAD_LISTEN_AUTH) ||
97 (listener->type == RAD_LISTEN_NONE) ||
99 #ifdef WITH_ACCOUNTING
100 (listener->type == RAD_LISTEN_ACCT) ||
103 (listener->type == RAD_LISTEN_VQP) ||
105 (listener->type == RAD_LISTEN_DHCP));
107 clients = ((listen_socket_t *)listener->data)->clients;
110 * This HAS to have been initialized previously.
112 rad_assert(clients != NULL);
114 #ifdef WITH_DYNAMIC_CLIENTS
115 client = client_find(clients, ipaddr);
116 if (!client) return NULL;
119 * No server defined, and it's not dynamic. Return it.
121 if (!client->client_server && !client->dynamic) return client;
126 * It's a dynamically generated client, check it.
128 if (client->dynamic) {
130 * Lives forever. Return it.
132 if (client->lifetime == 0) return client;
135 * Rate-limit the deletion of known clients.
136 * This makes them last a little longer, but
137 * prevents the server from melting down if (say)
138 * 10k clients all expire at once.
140 if (now == client->last_new_client) return client;
143 * It's not dead yet. Return it.
145 if ((client->created + client->lifetime) > now) return client;
148 * It's dead, Jim. Delete it.
150 client_delete(clients, client);
153 * Go find the enclosing network again.
155 client = client_find(clients, ipaddr);
160 if (!client) return NULL;
161 if (!client->client_server) return NULL;
164 * At this point, 'client' is the enclosing
165 * network that configures where dynamic clients
168 rad_assert(client->dynamic == 0);
171 * The IP is unknown, so we've found an enclosing
172 * network. Enable DoS protection. We only
173 * allow one new client per second. Known
174 * clients aren't subject to this restriction.
176 if (now == client->last_new_client) return NULL;
179 client->last_new_client = now;
181 request = request_alloc();
182 if (!request) return NULL;
184 request->listener = listener;
185 request->client = client;
186 request->packet = rad_alloc(0);
187 if (!request->packet) {
188 request_free(&request);
191 request->reply = rad_alloc(0);
192 if (!request->reply) {
193 request_free(&request);
196 request->packet->timestamp = request->timestamp;
198 request->priority = listener->type;
199 request->server = client->client_server;
200 request->root = &mainconfig;
203 * Run a fake request through the given virtual server.
204 * Look for FreeRADIUS-Client-IP-Address
205 * FreeRADIUS-Client-Secret
208 * and create the RADCLIENT structure from that.
211 sock = listener->data;
212 request->packet->sockfd = listener->fd;
213 request->packet->src_ipaddr = *ipaddr;
214 request->packet->src_port = 0; /* who cares... */
215 request->packet->dst_ipaddr = sock->ipaddr;
216 request->packet->dst_port = sock->port;
218 request->reply->sockfd = request->packet->sockfd;
219 request->reply->dst_ipaddr = request->packet->src_ipaddr;
220 request->reply->src_ipaddr = request->packet->dst_ipaddr;
221 request->reply->dst_port = request->packet->src_port;
222 request->reply->src_port = request->packet->dst_port;
223 request->reply->id = request->packet->id;
224 request->reply->code = 0; /* UNKNOWN code */
227 DEBUG("server %s {", request->server);
229 rcode = module_authorize(0, request);
231 DEBUG("} # server %s", request->server);
233 if (rcode != RLM_MODULE_OK) {
234 request_free(&request);
238 created = client_create(clients, request);
240 request_free(&request);
242 return created; /* may be NULL */
244 return client_find(clients, ipaddr);
248 static int listen_bind(rad_listen_t *this);
252 * Process and reply to a server-status request.
253 * Like rad_authenticate and rad_accounting this should
254 * live in it's own file but it's so small we don't bother.
256 static int rad_status_server(REQUEST *request)
258 int rcode = RLM_MODULE_OK;
261 switch (request->listener->type) {
263 case RAD_LISTEN_NONE:
265 case RAD_LISTEN_AUTH:
266 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
268 rcode = module_authorize(dval->value, request);
270 rcode = RLM_MODULE_OK;
275 case RLM_MODULE_UPDATED:
276 request->reply->code = PW_AUTHENTICATION_ACK;
279 case RLM_MODULE_FAIL:
280 case RLM_MODULE_HANDLED:
281 request->reply->code = 0; /* don't reply */
285 case RLM_MODULE_REJECT:
286 request->reply->code = PW_AUTHENTICATION_REJECT;
291 #ifdef WITH_ACCOUNTING
292 case RAD_LISTEN_ACCT:
293 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
295 rcode = module_accounting(dval->value, request);
297 rcode = RLM_MODULE_OK;
302 case RLM_MODULE_UPDATED:
303 request->reply->code = PW_ACCOUNTING_RESPONSE;
307 request->reply->code = 0; /* don't reply */
319 * Full statistics are available only on a statistics
322 if (request->listener->type == RAD_LISTEN_NONE) {
323 request_stats_reply(request);
331 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
334 listen_socket_t *sock = this->data;
337 switch (this->type) {
339 case RAD_LISTEN_NONE: /* what a hack... */
344 case RAD_LISTEN_AUTH:
345 name = "authentication";
348 #ifdef WITH_ACCOUNTING
349 case RAD_LISTEN_ACCT:
355 case RAD_LISTEN_PROXY:
367 case RAD_LISTEN_DHCP:
377 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
378 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
382 #ifdef SO_BINDTODEVICE
383 if (sock->interface) {
384 ADDSTRING(" interface ");
385 ADDSTRING(sock->interface);
389 ADDSTRING(" address ");
391 if ((sock->ipaddr.af == AF_INET) &&
392 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
393 strlcpy(buffer, "*", bufsize);
395 ip_ntoh(&sock->ipaddr, buffer, bufsize);
400 snprintf(buffer, bufsize, "%d", sock->port);
404 ADDSTRING(" as server ");
405 ADDSTRING(this->server);
416 * Parse an authentication or accounting socket.
418 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
423 listen_socket_t *sock = this->data;
424 char *section_name = NULL;
425 CONF_SECTION *client_cs, *parentcs;
430 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
431 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
432 &ipaddr.ipaddr.ip4addr, NULL);
433 if (rcode < 0) return -1;
435 if (rcode == 0) { /* successfully parsed IPv4 */
438 } else { /* maybe IPv6? */
439 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
440 &ipaddr.ipaddr.ip6addr, NULL);
441 if (rcode < 0) return -1;
444 cf_log_err(cf_sectiontoitem(cs),
445 "No address specified in listen section");
448 ipaddr.af = AF_INET6;
451 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
453 if (rcode < 0) return -1;
455 if ((listen_port < 0) || (listen_port > 65500)) {
456 cf_log_err(cf_sectiontoitem(cs),
457 "Invalid value for \"port\"");
461 sock->ipaddr = ipaddr;
462 sock->port = listen_port;
465 * If we can bind to interfaces, do so,
468 if (cf_pair_find(cs, "interface")) {
469 #ifndef SO_BINDTODEVICE
470 cf_log_err(cf_sectiontoitem(cs),
471 "System does not support binding to interfaces. Delete this line from the configuration file.");
475 CONF_PAIR *cp = cf_pair_find(cs, "interface");
477 rad_assert(cp != NULL);
478 value = cf_pair_value(cp);
480 cf_log_err(cf_sectiontoitem(cs),
481 "No interface name given");
484 sock->interface = value;
489 * And bind it to the port.
491 if (listen_bind(this) < 0) {
493 cf_log_err(cf_sectiontoitem(cs),
494 "Error binding to port for %s port %d",
495 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
502 * Proxy sockets don't have clients.
504 if (this->type == RAD_LISTEN_PROXY) return 0;
508 * The more specific configurations are preferred to more
512 parentcs = cf_top_section(cs);
513 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
514 §ion_name, NULL);
515 if (rcode < 0) return -1; /* bad string */
518 * Explicit list given: use it.
520 client_cs = cf_section_sub_find_name2(parentcs,
524 client_cs = cf_section_find(section_name);
527 cf_log_err(cf_sectiontoitem(cs),
528 "Failed to find clients %s {...}",
534 } /* else there was no "clients = " entry. */
537 CONF_SECTION *server_cs;
539 server_cs = cf_section_sub_find_name2(parentcs,
543 * Found a "server foo" section. If there are clients
547 (cf_section_sub_find(server_cs, "client") != NULL)) {
548 client_cs = server_cs;
553 * Still nothing. Look for global clients.
555 if (!client_cs) client_cs = parentcs;
557 sock->clients = clients_parse_section(client_cs);
558 if (!sock->clients) {
559 cf_log_err(cf_sectiontoitem(cs),
560 "Failed to load clients for this listen section");
568 * Send an authentication response packet
570 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
572 rad_assert(request->listener == listener);
573 rad_assert(listener->send == auth_socket_send);
575 return rad_send(request->reply, request->packet,
576 request->client->secret);
580 #ifdef WITH_ACCOUNTING
582 * Send an accounting response packet (or not)
584 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
586 rad_assert(request->listener == listener);
587 rad_assert(listener->send == acct_socket_send);
590 * Accounting reject's are silently dropped.
592 * We do it here to avoid polluting the rest of the
593 * code with this knowledge
595 if (request->reply->code == 0) return 0;
597 return rad_send(request->reply, request->packet,
598 request->client->secret);
604 * Send a packet to a home server.
606 * FIXME: have different code for proxy auth & acct!
608 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
610 listen_socket_t *sock = listener->data;
612 rad_assert(request->proxy_listener == listener);
613 rad_assert(listener->send == proxy_socket_send);
615 request->proxy->src_ipaddr = sock->ipaddr;
616 request->proxy->src_port = sock->port;
618 return rad_send(request->proxy, request->packet,
619 request->home_server->secret);
625 * Check if an incoming request is "ok"
627 * It takes packets, not requests. It sees if the packet looks
628 * OK. If so, it does a number of sanity checks on it.
630 static int stats_socket_recv(rad_listen_t *listener,
631 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
635 RADIUS_PACKET *packet;
638 fr_ipaddr_t src_ipaddr;
640 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
641 if (rcode < 0) return 0;
643 RAD_STATS_TYPE_INC(listener, total_requests);
645 if (rcode < 20) { /* AUTH_HDR_LEN */
646 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
650 if ((client = client_listener_find(listener,
651 &src_ipaddr)) == NULL) {
652 rad_recv_discard(listener->fd);
653 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
655 if (debug_flag > 0) {
658 listener->print(listener, name, sizeof(name));
661 * This is debugging rather than logging, so that
662 * DoS attacks don't affect us.
664 DEBUG("Ignoring request to %s from unknown client %s port %d",
666 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
667 buffer, sizeof(buffer)), src_port);
674 * We only understand Status-Server on this socket.
676 if (code != PW_STATUS_SERVER) {
677 DEBUG("Ignoring packet code %d sent to Status-Server port",
679 rad_recv_discard(listener->fd);
680 RAD_STATS_TYPE_INC(listener, total_unknown_types);
681 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
686 * Now that we've sanity checked everything, receive the
689 packet = rad_recv(listener->fd, 1); /* require message authenticator */
691 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
692 DEBUG("%s", librad_errstr);
696 if (!received_request(listener, packet, prequest, client)) {
697 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
698 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
703 *pfun = rad_status_server;
710 * Check if an incoming request is "ok"
712 * It takes packets, not requests. It sees if the packet looks
713 * OK. If so, it does a number of sanity checks on it.
715 static int auth_socket_recv(rad_listen_t *listener,
716 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
720 RADIUS_PACKET *packet;
721 RAD_REQUEST_FUNP fun = NULL;
724 fr_ipaddr_t src_ipaddr;
726 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
727 if (rcode < 0) return 0;
729 RAD_STATS_TYPE_INC(listener, total_requests);
731 if (rcode < 20) { /* AUTH_HDR_LEN */
732 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
736 if ((client = client_listener_find(listener,
737 &src_ipaddr)) == NULL) {
738 rad_recv_discard(listener->fd);
739 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
741 if (debug_flag > 0) {
744 listener->print(listener, name, sizeof(name));
747 * This is debugging rather than logging, so that
748 * DoS attacks don't affect us.
750 DEBUG("Ignoring request to %s from unknown client %s port %d",
752 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
753 buffer, sizeof(buffer)), src_port);
760 * Some sanity checks, based on the packet code.
763 case PW_AUTHENTICATION_REQUEST:
764 RAD_STATS_CLIENT_INC(listener, client, total_requests);
765 fun = rad_authenticate;
768 case PW_STATUS_SERVER:
769 if (!mainconfig.status_server) {
770 rad_recv_discard(listener->fd);
771 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
772 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
773 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
776 fun = rad_status_server;
780 rad_recv_discard(listener->fd);
781 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
782 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
784 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
785 code, client->shortname, src_port);
788 } /* switch over packet types */
791 * Now that we've sanity checked everything, receive the
794 packet = rad_recv(listener->fd, client->message_authenticator);
796 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
797 DEBUG("%s", librad_errstr);
801 if (!received_request(listener, packet, prequest, client)) {
802 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
803 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
813 #ifdef WITH_ACCOUNTING
815 * Receive packets from an accounting socket
817 static int acct_socket_recv(rad_listen_t *listener,
818 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
822 RADIUS_PACKET *packet;
823 RAD_REQUEST_FUNP fun = NULL;
826 fr_ipaddr_t src_ipaddr;
828 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
829 if (rcode < 0) return 0;
831 RAD_STATS_TYPE_INC(listener, total_requests);
833 if (rcode < 20) { /* AUTH_HDR_LEN */
834 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
838 if ((client = client_listener_find(listener,
839 &src_ipaddr)) == NULL) {
840 rad_recv_discard(listener->fd);
841 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
844 * This is debugging rather than logging, so that
845 * DoS attacks don't affect us.
847 if (debug_flag > 0) {
850 listener->print(listener, name, sizeof(name));
852 DEBUG("Ignoring request to %s from unknown client %s port %d",
854 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
855 buffer, sizeof(buffer)), src_port);
862 * Some sanity checks, based on the packet code.
865 case PW_ACCOUNTING_REQUEST:
866 RAD_STATS_CLIENT_INC(listener, client, total_requests);
867 fun = rad_accounting;
870 case PW_STATUS_SERVER:
871 if (!mainconfig.status_server) {
872 rad_recv_discard(listener->fd);
873 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
874 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
876 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
879 fun = rad_status_server;
883 rad_recv_discard(listener->fd);
884 RAD_STATS_TYPE_INC(listener, total_unknown_types);
885 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
887 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
888 code, client->shortname, src_port);
890 } /* switch over packet types */
893 * Now that we've sanity checked everything, receive the
896 packet = rad_recv(listener->fd, 0);
898 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
899 radlog(L_ERR, "%s", librad_errstr);
904 * There can be no duplicate accounting packets.
906 if (!received_request(listener, packet, prequest, client)) {
907 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
908 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
920 * Recieve packets from a proxy socket.
922 static int proxy_socket_recv(rad_listen_t *listener,
923 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
926 RADIUS_PACKET *packet;
927 RAD_REQUEST_FUNP fun = NULL;
930 packet = rad_recv(listener->fd, 0);
932 radlog(L_ERR, "%s", librad_errstr);
937 * FIXME: Client MIB updates?
939 switch(packet->code) {
940 case PW_AUTHENTICATION_ACK:
941 case PW_ACCESS_CHALLENGE:
942 case PW_AUTHENTICATION_REJECT:
943 fun = rad_authenticate;
946 #ifdef WITH_ACCOUNTING
947 case PW_ACCOUNTING_RESPONSE:
948 fun = rad_accounting;
954 * FIXME: Update MIB for packet types?
956 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
957 "from home server %s port %d - ID %d : IGNORED",
959 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
960 packet->src_port, packet->id);
965 request = received_proxy_response(packet);
970 rad_assert(fun != NULL);
979 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
981 if (!request->reply->code) return 0;
983 rad_encode(request->reply, request->packet,
984 request->client->secret);
985 rad_sign(request->reply, request->packet,
986 request->client->secret);
992 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
994 if (rad_verify(request->packet, NULL,
995 request->client->secret) < 0) {
999 return rad_decode(request->packet, NULL,
1000 request->client->secret);
1004 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1006 rad_encode(request->proxy, NULL, request->home_server->secret);
1007 rad_sign(request->proxy, NULL, request->home_server->secret);
1013 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1015 if (rad_verify(request->proxy_reply, request->proxy,
1016 request->home_server->secret) < 0) {
1020 return rad_decode(request->proxy_reply, request->proxy,
1021 request->home_server->secret);
1027 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1029 { common_socket_parse, NULL,
1030 stats_socket_recv, auth_socket_send,
1031 socket_print, client_socket_encode, client_socket_decode },
1033 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1038 { common_socket_parse, NULL,
1039 proxy_socket_recv, proxy_socket_send,
1040 socket_print, proxy_socket_encode, proxy_socket_decode },
1042 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1045 /* authentication */
1046 { common_socket_parse, NULL,
1047 auth_socket_recv, auth_socket_send,
1048 socket_print, client_socket_encode, client_socket_decode },
1050 #ifdef WITH_ACCOUNTING
1052 { common_socket_parse, NULL,
1053 acct_socket_recv, acct_socket_send,
1054 socket_print, client_socket_encode, client_socket_decode},
1056 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1061 { detail_parse, detail_free,
1062 detail_recv, detail_send,
1063 detail_print, detail_encode, detail_decode },
1065 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1069 /* vlan query protocol */
1070 { common_socket_parse, NULL,
1071 vqp_socket_recv, vqp_socket_send,
1072 socket_print, vqp_socket_encode, vqp_socket_decode },
1074 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1078 /* dhcp query protocol */
1079 { dhcp_socket_parse, NULL,
1080 dhcp_socket_recv, dhcp_socket_send,
1081 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1083 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1091 * Binds a listener to a socket.
1093 static int listen_bind(rad_listen_t *this)
1095 struct sockaddr_storage salocal;
1097 listen_socket_t *sock = this->data;
1100 * If the port is zero, then it means the appropriate
1101 * thing from /etc/services.
1103 if (sock->port == 0) {
1104 struct servent *svp;
1106 switch (this->type) {
1107 case RAD_LISTEN_AUTH:
1108 svp = getservbyname ("radius", "udp");
1110 sock->port = ntohs(svp->s_port);
1112 sock->port = PW_AUTH_UDP_PORT;
1116 #ifdef WITH_ACCOUNTING
1117 case RAD_LISTEN_ACCT:
1118 svp = getservbyname ("radacct", "udp");
1120 sock->port = ntohs(svp->s_port);
1122 sock->port = PW_ACCT_UDP_PORT;
1128 case RAD_LISTEN_PROXY:
1134 case RAD_LISTEN_VQP:
1140 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1146 * Copy fr_socket() here, as we may need to bind to a device.
1148 this->fd = socket(sock->ipaddr.af, SOCK_DGRAM, 0);
1150 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
1154 #ifdef SO_BINDTODEVICE
1156 * Bind to a device BEFORE touching IP addresses.
1158 if (sock->interface) {
1160 strcpy(ifreq.ifr_name, sock->interface);
1162 if (setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1163 (char *)&ifreq, sizeof(ifreq)) < 0) {
1165 radlog(L_ERR, "Failed opening to interface %s: %s",
1166 sock->interface, strerror(errno));
1168 } /* else it worked. */
1172 #ifdef WITH_UDPFROMTO
1174 * Initialize udpfromto for all sockets.
1176 if (udpfromto_init(this->fd) != 0) {
1183 * Set up sockaddr stuff.
1185 if (!fr_ipaddr2sockaddr(&sock->ipaddr, sock->port, &salocal, &salen)) {
1190 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1191 if (sock->ipaddr.af == AF_INET6) {
1193 * Listening on '::' does NOT get you IPv4 to
1194 * IPv6 mapping. You've got to listen on an IPv4
1195 * address, too. This makes the rest of the server
1196 * design a little simpler.
1200 if (IN6_IS_ADDR_UNSPECIFIED(&sock->ipaddr.ipaddr.ip6addr)) {
1203 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
1204 (char *)&on, sizeof(on));
1206 #endif /* IPV6_V6ONLY */
1207 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
1210 if (bind(this->fd, (struct sockaddr *) &salocal, salen) < 0) {
1212 radlog(L_ERR, "Failed binding to socket: %s\n",
1218 * FreeBSD jail issues. We bind to 0.0.0.0, but the
1219 * kernel instead binds us to a 1.2.3.4. If this
1220 * happens, notice, and remember our real IP.
1223 struct sockaddr_storage src;
1224 socklen_t sizeof_src = sizeof(src);
1226 memset(&src, 0, sizeof_src);
1227 if (getsockname(this->fd, (struct sockaddr *) &src,
1229 radlog(L_ERR, "Failed getting socket name: %s",
1234 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
1235 &sock->ipaddr, &sock->port)) {
1236 radlog(L_ERR, "Socket has unsupported address family");
1245 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
1246 radlog(L_ERR, "Failure getting socket flags: %s)\n",
1251 flags |= O_NONBLOCK;
1252 if( fcntl(this->fd, F_SETFL, flags) < 0) {
1253 radlog(L_ERR, "Failure setting socket flags: %s)\n",
1265 * Allocate & initialize a new listener.
1267 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
1271 this = rad_malloc(sizeof(*this));
1272 memset(this, 0, sizeof(*this));
1275 this->recv = master_listen[this->type].recv;
1276 this->send = master_listen[this->type].send;
1277 this->print = master_listen[this->type].print;
1278 this->encode = master_listen[this->type].encode;
1279 this->decode = master_listen[this->type].decode;
1283 case RAD_LISTEN_NONE:
1285 case RAD_LISTEN_AUTH:
1286 case RAD_LISTEN_ACCT:
1287 case RAD_LISTEN_PROXY:
1288 case RAD_LISTEN_VQP:
1289 case RAD_LISTEN_DHCP:
1290 this->data = rad_malloc(sizeof(listen_socket_t));
1291 memset(this->data, 0, sizeof(listen_socket_t));
1295 case RAD_LISTEN_DETAIL:
1301 rad_assert("Unsupported option!" == NULL);
1311 * Externally visible function for creating a new proxy LISTENER.
1313 * For now, don't take ipaddr or port.
1315 * Not thread-safe, but all calls to it are protected by the
1316 * proxy mutex in request_list.c
1318 rad_listen_t *proxy_new_listener()
1320 int last_proxy_port, port;
1321 rad_listen_t *this, *tmp, **last;
1322 listen_socket_t *sock, *old;
1324 this = listen_alloc(RAD_LISTEN_PROXY);
1327 * Find an existing proxy socket to copy.
1329 * FIXME: Make it per-realm, or per-home server!
1331 last_proxy_port = 0;
1333 last = &mainconfig.listen;
1334 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1335 if (tmp->type == RAD_LISTEN_PROXY) {
1337 if (sock->port > last_proxy_port) {
1338 last_proxy_port = sock->port + 1;
1340 if (!old) old = sock;
1343 last = &(tmp->next);
1348 return NULL; /* This is a serious error. */
1352 * FIXME: find a new IP address to listen on?
1354 * This could likely be done in the "home server"
1355 * configuration, to have per-home-server source IP's.
1358 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1361 * Keep going until we find an unused port.
1363 for (port = last_proxy_port; port < 64000; port++) {
1365 if (listen_bind(this) == 0) {
1367 * Add the new listener to the list of
1380 static const FR_NAME_NUMBER listen_compare[] = {
1382 { "status", RAD_LISTEN_NONE },
1384 { "auth", RAD_LISTEN_AUTH },
1385 #ifdef WITH_ACCOUNTING
1386 { "acct", RAD_LISTEN_ACCT },
1389 { "detail", RAD_LISTEN_DETAIL },
1392 { "proxy", RAD_LISTEN_PROXY },
1395 { "vmps", RAD_LISTEN_VQP },
1398 { "dhcp", RAD_LISTEN_DHCP },
1404 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1412 cf_log_info(cs, "listen {");
1414 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1416 if (rcode < 0) return NULL;
1419 cf_log_err(cf_sectiontoitem(cs),
1420 "No type specified in listen section");
1424 type = fr_str2int(listen_compare, listen_type, -1);
1426 cf_log_err(cf_sectiontoitem(cs),
1427 "Invalid type \"%s\" in listen section.",
1435 * Allow listen sections in the default config to
1436 * refer to a server.
1439 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1441 if (rcode == 1) { /* compatiblity with 2.0-pre */
1442 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1445 if (rcode < 0) return NULL;
1449 * Set up cross-type data.
1451 this = listen_alloc(type);
1452 this->server = server;
1456 * Call per-type parser.
1458 if (master_listen[type].parse(cs, this) < 0) {
1463 cf_log_info(cs, "}");
1469 * Generate a list of listeners. Takes an input list of
1470 * listeners, too, so we don't close sockets with waiting packets.
1472 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1474 int override = FALSE;
1476 CONF_SECTION *cs = NULL;
1477 rad_listen_t **last;
1479 fr_ipaddr_t server_ipaddr;
1482 int defined_proxy = 0;
1486 * We shouldn't be called with a pre-existing list.
1488 rad_assert(head && (*head == NULL));
1491 server_ipaddr.af = AF_UNSPEC;
1494 * If the port is specified on the command-line,
1495 * it over-rides the configuration file.
1497 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1499 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1502 * If the IP address was configured on the command-line,
1503 * use that as the "bind_address"
1505 if (mainconfig.myip.af != AF_UNSPEC) {
1506 memcpy(&server_ipaddr, &mainconfig.myip,
1507 sizeof(server_ipaddr));
1513 * Else look for bind_address and/or listen sections.
1515 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1516 rcode = cf_item_parse(config, "bind_address",
1518 &server_ipaddr.ipaddr.ip4addr, NULL);
1519 if (rcode < 0) return -1; /* error parsing it */
1521 if (rcode == 0) { /* successfully parsed IPv4 */
1522 listen_socket_t *sock;
1523 server_ipaddr.af = AF_INET;
1525 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'.");
1529 if (strcmp(progname, "vmpsd") == 0) {
1530 this = listen_alloc(RAD_LISTEN_VQP);
1531 if (!auth_port) auth_port = 1589;
1534 this = listen_alloc(RAD_LISTEN_AUTH);
1538 sock->ipaddr = server_ipaddr;
1539 sock->port = auth_port;
1541 sock->clients = clients_parse_section(config);
1542 if (!sock->clients) {
1543 cf_log_err(cf_sectiontoitem(config),
1544 "Failed to find any clients for this listen section");
1548 if (listen_bind(this) < 0) {
1551 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1554 auth_port = sock->port; /* may have been updated in listen_bind */
1556 cs = cf_section_sub_find_name2(config, "server",
1558 if (cs) this->server = mainconfig.name;
1562 last = &(this->next);
1568 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
1571 #ifdef WITH_ACCOUNTING
1573 * Open Accounting Socket.
1575 * If we haven't already gotten acct_port from
1576 * /etc/services, then make it auth_port + 1.
1578 this = listen_alloc(RAD_LISTEN_ACCT);
1582 * Create the accounting socket.
1584 * The accounting port is always the
1585 * authentication port + 1
1587 sock->ipaddr = server_ipaddr;
1588 sock->port = auth_port + 1;
1590 sock->clients = clients_parse_section(config);
1591 if (!sock->clients) {
1592 cf_log_err(cf_sectiontoitem(config),
1593 "Failed to find any clients for this listen section");
1597 if (listen_bind(this) < 0) {
1600 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1605 cs = cf_section_sub_find_name2(config, "server",
1607 if (cs) this->server = mainconfig.name;
1611 last = &(this->next);
1613 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1614 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1620 * They specified an IP on the command-line, ignore
1621 * all listen sections except the one in '-n'.
1623 if (mainconfig.myip.af != AF_UNSPEC) {
1624 CONF_SECTION *subcs;
1625 const char *name2 = cf_section_name2(cs);
1627 cs = cf_section_sub_find_name2(config, "server",
1629 if (!cs) goto do_proxy;
1632 * Should really abstract this code...
1634 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1636 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1637 this = listen_parse(subcs, name2);
1644 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1648 last = &(this->next);
1649 } /* loop over "listen" directives in server <foo> */
1655 * Walk through the "listen" sections, if they exist.
1657 for (cs = cf_subsection_find_next(config, NULL, "listen");
1659 cs = cf_subsection_find_next(config, cs, "listen")) {
1660 this = listen_parse(cs, NULL);
1667 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1671 last = &(this->next);
1675 * Check virtual servers for "listen" sections, too.
1677 * FIXME: Move to virtual server init?
1679 for (cs = cf_subsection_find_next(config, NULL, "server");
1681 cs = cf_subsection_find_next(config, cs, "server")) {
1682 CONF_SECTION *subcs;
1683 const char *name2 = cf_section_name2(cs);
1685 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1687 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1688 this = listen_parse(subcs, name2);
1695 if (this->type == RAD_LISTEN_PROXY) {
1696 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
1703 last = &(this->next);
1704 } /* loop over "listen" directives in virtual servers */
1705 } /* loop over virtual servers */
1708 * If we're proxying requests, open the proxy FD.
1709 * Otherwise, don't do anything.
1713 if (mainconfig.proxy_requests == TRUE) {
1715 listen_socket_t *sock = NULL;
1718 * No sockets to receive packets, therefore
1719 * proxying is pointless.
1721 if (!*head) return -1;
1723 if (defined_proxy) goto done;
1726 * Find the first authentication port,
1729 for (this = *head; this != NULL; this = this->next) {
1730 if (this->type == RAD_LISTEN_AUTH) {
1732 if (server_ipaddr.af == AF_UNSPEC) {
1733 server_ipaddr = sock->ipaddr;
1735 port = sock->port + 2; /* skip acct port */
1738 if (this->type == RAD_LISTEN_VQP) {
1740 if (server_ipaddr.af == AF_UNSPEC) {
1741 server_ipaddr = sock->ipaddr;
1743 port = sock->port + 1;
1748 if (port < 0) port = 1024 + (fr_rand() & 0x1ff);
1751 * Address is still unspecified, use IPv4.
1753 if (server_ipaddr.af == AF_UNSPEC) {
1754 server_ipaddr.af = AF_INET;
1755 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1758 this = listen_alloc(RAD_LISTEN_PROXY);
1762 * Create the first proxy socket.
1764 sock->ipaddr = server_ipaddr;
1767 * Try to find a proxy port (value doesn't matter)
1769 for (sock->port = port;
1772 if (listen_bind(this) == 0) {
1774 last = &(this->next); /* just in case */
1779 if (sock->port >= 64000) {
1782 radlog(L_ERR, "Failed to open socket for proxying");
1787 done: /* used only in proxy code. */
1794 * Free a linked list of listeners;
1796 void listen_free(rad_listen_t **head)
1800 if (!head || !*head) return;
1804 rad_listen_t *next = this->next;
1807 * Other code may have eaten the FD.
1809 if (this->fd >= 0) close(this->fd);
1811 if (master_listen[this->type].free) {
1812 master_listen[this->type].free(this);
1824 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
1829 for (this = mainconfig.listen; this != NULL; this = this->next) {
1830 listen_socket_t *sock;
1832 if ((this->type != RAD_LISTEN_AUTH) &&
1833 (this->type != RAD_LISTEN_ACCT)) continue;
1837 if ((sock->port == port) &&
1838 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {
1839 return sock->clients;
1846 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
1850 for (this = mainconfig.listen; this != NULL; this = this->next) {
1851 listen_socket_t *sock;
1853 if ((this->type != RAD_LISTEN_AUTH) &&
1854 (this->type != RAD_LISTEN_ACCT)) continue;
1858 if ((sock->port == port) &&
1859 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {