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, int src_port)
85 #ifdef WITH_DYNAMIC_CLIENTS
87 listen_socket_t *sock;
93 RADCLIENT_LIST *clients;
95 rad_assert(listener != NULL);
96 rad_assert(ipaddr != NULL);
98 rad_assert((listener->type == RAD_LISTEN_AUTH)
100 || (listener->type == RAD_LISTEN_NONE)
102 #ifdef WITH_ACCOUNTING
103 || (listener->type == RAD_LISTEN_ACCT)
106 || (listener->type == RAD_LISTEN_VQP)
109 || (listener->type == RAD_LISTEN_DHCP)
113 clients = ((listen_socket_t *)listener->data)->clients;
116 * This HAS to have been initialized previously.
118 rad_assert(clients != NULL);
120 client = client_find(clients, ipaddr);
122 static time_t last_printed = 0;
123 char name[256], buffer[128];
125 #ifdef WITH_DYNAMIC_CLIENTS
126 unknown: /* used only for dynamic clients */
130 * DoS attack quenching, but only in debug mode.
131 * If they're running in debug mode, show them
134 if (debug_flag == 0) {
136 if (last_printed == now) return NULL;
141 listener->print(listener, name, sizeof(name));
143 radlog(L_ERR, "Ignoring request to %s from unknown client %s port %d",
144 name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
145 buffer, sizeof(buffer)),
150 #ifndef WITH_DYNAMIC_CLIENTS
151 return client; /* return the found client. */
155 * No server defined, and it's not dynamic. Return it.
157 if (!client->client_server && !client->dynamic) return client;
162 * It's a dynamically generated client, check it.
164 if (client->dynamic) {
166 * Lives forever. Return it.
168 if (client->lifetime == 0) return client;
171 * Rate-limit the deletion of known clients.
172 * This makes them last a little longer, but
173 * prevents the server from melting down if (say)
174 * 10k clients all expire at once.
176 if (now == client->last_new_client) return client;
179 * It's not dead yet. Return it.
181 if ((client->created + client->lifetime) > now) return client;
184 * It's dead, Jim. Delete it.
186 client_delete(clients, client);
189 * Go find the enclosing network again.
191 client = client_find(clients, ipaddr);
196 if (!client) goto unknown;
197 if (!client->client_server) goto unknown;
200 * At this point, 'client' is the enclosing
201 * network that configures where dynamic clients
204 rad_assert(client->dynamic == 0);
207 * The IP is unknown, so we've found an enclosing
208 * network. Enable DoS protection. We only
209 * allow one new client per second. Known
210 * clients aren't subject to this restriction.
212 if (now == client->last_new_client) goto unknown;
215 client->last_new_client = now;
217 request = request_alloc();
218 if (!request) goto unknown;
220 request->listener = listener;
221 request->client = client;
222 request->packet = rad_alloc(0);
223 if (!request->packet) {
224 request_free(&request);
227 request->reply = rad_alloc(0);
228 if (!request->reply) {
229 request_free(&request);
232 request->packet->timestamp = request->timestamp;
234 request->priority = listener->type;
235 request->server = client->client_server;
236 request->root = &mainconfig;
239 * Run a fake request through the given virtual server.
240 * Look for FreeRADIUS-Client-IP-Address
241 * FreeRADIUS-Client-Secret
244 * and create the RADCLIENT structure from that.
247 sock = listener->data;
248 request->packet->sockfd = listener->fd;
249 request->packet->src_ipaddr = *ipaddr;
250 request->packet->src_port = 0; /* who cares... */
251 request->packet->dst_ipaddr = sock->ipaddr;
252 request->packet->dst_port = sock->port;
254 request->reply->sockfd = request->packet->sockfd;
255 request->reply->dst_ipaddr = request->packet->src_ipaddr;
256 request->reply->src_ipaddr = request->packet->dst_ipaddr;
257 request->reply->dst_port = request->packet->src_port;
258 request->reply->src_port = request->packet->dst_port;
259 request->reply->id = request->packet->id;
260 request->reply->code = 0; /* UNKNOWN code */
263 DEBUG("server %s {", request->server);
265 rcode = module_authorize(0, request);
267 DEBUG("} # server %s", request->server);
269 if (rcode != RLM_MODULE_OK) {
270 request_free(&request);
275 * If the client was updated by rlm_dynamic_clients,
276 * don't create the client from attribute-value pairs.
278 if (request->client == client) {
279 created = client_create(clients, request);
281 created = request->client;
284 * This frees the client if it isn't valid.
286 if (!client_validate(clients, client, created)) goto unknown;
288 request_free(&request);
290 if (!created) goto unknown;
296 static int listen_bind(rad_listen_t *this);
300 * Process and reply to a server-status request.
301 * Like rad_authenticate and rad_accounting this should
302 * live in it's own file but it's so small we don't bother.
304 static int rad_status_server(REQUEST *request)
306 int rcode = RLM_MODULE_OK;
309 switch (request->listener->type) {
311 case RAD_LISTEN_NONE:
313 case RAD_LISTEN_AUTH:
314 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
316 rcode = module_authorize(dval->value, request);
318 rcode = RLM_MODULE_OK;
323 case RLM_MODULE_UPDATED:
324 request->reply->code = PW_AUTHENTICATION_ACK;
327 case RLM_MODULE_FAIL:
328 case RLM_MODULE_HANDLED:
329 request->reply->code = 0; /* don't reply */
333 case RLM_MODULE_REJECT:
334 request->reply->code = PW_AUTHENTICATION_REJECT;
339 #ifdef WITH_ACCOUNTING
340 case RAD_LISTEN_ACCT:
341 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
343 rcode = module_accounting(dval->value, request);
345 rcode = RLM_MODULE_OK;
350 case RLM_MODULE_UPDATED:
351 request->reply->code = PW_ACCOUNTING_RESPONSE;
355 request->reply->code = 0; /* don't reply */
367 * Full statistics are available only on a statistics
370 if (request->listener->type == RAD_LISTEN_NONE) {
371 request_stats_reply(request);
379 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
382 listen_socket_t *sock = this->data;
385 switch (this->type) {
387 case RAD_LISTEN_NONE: /* what a hack... */
392 case RAD_LISTEN_AUTH:
393 name = "authentication";
396 #ifdef WITH_ACCOUNTING
397 case RAD_LISTEN_ACCT:
403 case RAD_LISTEN_PROXY:
415 case RAD_LISTEN_DHCP:
425 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
426 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
430 #ifdef SO_BINDTODEVICE
431 if (sock->interface) {
432 ADDSTRING(" interface ");
433 ADDSTRING(sock->interface);
437 ADDSTRING(" address ");
439 if ((sock->ipaddr.af == AF_INET) &&
440 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
441 strlcpy(buffer, "*", bufsize);
443 ip_ntoh(&sock->ipaddr, buffer, bufsize);
448 snprintf(buffer, bufsize, "%d", sock->port);
452 ADDSTRING(" as server ");
453 ADDSTRING(this->server);
464 * Parse an authentication or accounting socket.
466 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
471 listen_socket_t *sock = this->data;
472 char *section_name = NULL;
473 CONF_SECTION *client_cs, *parentcs;
478 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
479 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
480 &ipaddr.ipaddr.ip4addr, NULL);
481 if (rcode < 0) return -1;
483 if (rcode == 0) { /* successfully parsed IPv4 */
486 } else { /* maybe IPv6? */
487 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
488 &ipaddr.ipaddr.ip6addr, NULL);
489 if (rcode < 0) return -1;
492 cf_log_err(cf_sectiontoitem(cs),
493 "No address specified in listen section");
496 ipaddr.af = AF_INET6;
499 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
501 if (rcode < 0) return -1;
503 if ((listen_port < 0) || (listen_port > 65535)) {
504 cf_log_err(cf_sectiontoitem(cs),
505 "Invalid value for \"port\"");
509 sock->ipaddr = ipaddr;
510 sock->port = listen_port;
513 * If we can bind to interfaces, do so,
516 if (cf_pair_find(cs, "interface")) {
517 #ifndef SO_BINDTODEVICE
518 cf_log_err(cf_sectiontoitem(cs),
519 "System does not support binding to interfaces. Delete this line from the configuration file.");
523 CONF_PAIR *cp = cf_pair_find(cs, "interface");
525 rad_assert(cp != NULL);
526 value = cf_pair_value(cp);
528 cf_log_err(cf_sectiontoitem(cs),
529 "No interface name given");
532 sock->interface = value;
537 * And bind it to the port.
539 if (listen_bind(this) < 0) {
541 cf_log_err(cf_sectiontoitem(cs),
542 "Error binding to port for %s port %d",
543 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
550 * Proxy sockets don't have clients.
552 if (this->type == RAD_LISTEN_PROXY) return 0;
556 * The more specific configurations are preferred to more
560 parentcs = cf_top_section(cs);
561 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
562 §ion_name, NULL);
563 if (rcode < 0) return -1; /* bad string */
566 * Explicit list given: use it.
568 client_cs = cf_section_sub_find_name2(parentcs,
572 client_cs = cf_section_find(section_name);
575 cf_log_err(cf_sectiontoitem(cs),
576 "Failed to find clients %s {...}",
582 } /* else there was no "clients = " entry. */
585 CONF_SECTION *server_cs;
587 server_cs = cf_section_sub_find_name2(parentcs,
591 * Found a "server foo" section. If there are clients
595 (cf_section_sub_find(server_cs, "client") != NULL)) {
596 client_cs = server_cs;
601 * Still nothing. Look for global clients.
603 if (!client_cs) client_cs = parentcs;
605 sock->clients = clients_parse_section(client_cs);
606 if (!sock->clients) {
607 cf_log_err(cf_sectiontoitem(cs),
608 "Failed to load clients for this listen section");
616 * Send an authentication response packet
618 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
620 rad_assert(request->listener == listener);
621 rad_assert(listener->send == auth_socket_send);
623 return rad_send(request->reply, request->packet,
624 request->client->secret);
628 #ifdef WITH_ACCOUNTING
630 * Send an accounting response packet (or not)
632 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
634 rad_assert(request->listener == listener);
635 rad_assert(listener->send == acct_socket_send);
638 * Accounting reject's are silently dropped.
640 * We do it here to avoid polluting the rest of the
641 * code with this knowledge
643 if (request->reply->code == 0) return 0;
645 return rad_send(request->reply, request->packet,
646 request->client->secret);
652 * Send a packet to a home server.
654 * FIXME: have different code for proxy auth & acct!
656 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
658 listen_socket_t *sock = listener->data;
660 rad_assert(request->proxy_listener == listener);
661 rad_assert(listener->send == proxy_socket_send);
663 request->proxy->src_ipaddr = sock->ipaddr;
664 request->proxy->src_port = sock->port;
666 return rad_send(request->proxy, request->packet,
667 request->home_server->secret);
673 * Check if an incoming request is "ok"
675 * It takes packets, not requests. It sees if the packet looks
676 * OK. If so, it does a number of sanity checks on it.
678 static int stats_socket_recv(rad_listen_t *listener,
679 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
683 RADIUS_PACKET *packet;
685 fr_ipaddr_t src_ipaddr;
687 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
688 if (rcode < 0) return 0;
690 RAD_STATS_TYPE_INC(listener, total_requests);
692 if (rcode < 20) { /* AUTH_HDR_LEN */
693 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
697 if ((client = client_listener_find(listener,
698 &src_ipaddr, src_port)) == NULL) {
699 rad_recv_discard(listener->fd);
700 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
705 * We only understand Status-Server on this socket.
707 if (code != PW_STATUS_SERVER) {
708 DEBUG("Ignoring packet code %d sent to Status-Server port",
710 rad_recv_discard(listener->fd);
711 RAD_STATS_TYPE_INC(listener, total_unknown_types);
712 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
717 * Now that we've sanity checked everything, receive the
720 packet = rad_recv(listener->fd, 1); /* require message authenticator */
722 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
723 DEBUG("%s", fr_strerror());
727 if (!received_request(listener, packet, prequest, client)) {
728 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
729 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
734 *pfun = rad_status_server;
741 * Check if an incoming request is "ok"
743 * It takes packets, not requests. It sees if the packet looks
744 * OK. If so, it does a number of sanity checks on it.
746 static int auth_socket_recv(rad_listen_t *listener,
747 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
751 RADIUS_PACKET *packet;
752 RAD_REQUEST_FUNP fun = NULL;
754 fr_ipaddr_t src_ipaddr;
756 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
757 if (rcode < 0) return 0;
759 RAD_STATS_TYPE_INC(listener, total_requests);
761 if (rcode < 20) { /* AUTH_HDR_LEN */
762 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
766 if ((client = client_listener_find(listener,
767 &src_ipaddr, src_port)) == NULL) {
768 rad_recv_discard(listener->fd);
769 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
774 * Some sanity checks, based on the packet code.
777 case PW_AUTHENTICATION_REQUEST:
778 RAD_STATS_CLIENT_INC(listener, client, total_requests);
779 fun = rad_authenticate;
782 case PW_STATUS_SERVER:
783 if (!mainconfig.status_server) {
784 rad_recv_discard(listener->fd);
785 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
786 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
787 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
790 fun = rad_status_server;
794 rad_recv_discard(listener->fd);
795 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
796 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
798 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
799 code, client->shortname, src_port);
802 } /* switch over packet types */
805 * Now that we've sanity checked everything, receive the
808 packet = rad_recv(listener->fd, client->message_authenticator);
810 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
811 DEBUG("%s", fr_strerror());
815 if (!received_request(listener, packet, prequest, client)) {
816 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
817 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
827 #ifdef WITH_ACCOUNTING
829 * Receive packets from an accounting socket
831 static int acct_socket_recv(rad_listen_t *listener,
832 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
836 RADIUS_PACKET *packet;
837 RAD_REQUEST_FUNP fun = NULL;
839 fr_ipaddr_t src_ipaddr;
841 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
842 if (rcode < 0) return 0;
844 RAD_STATS_TYPE_INC(listener, total_requests);
846 if (rcode < 20) { /* AUTH_HDR_LEN */
847 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
851 if ((client = client_listener_find(listener,
852 &src_ipaddr, src_port)) == NULL) {
853 rad_recv_discard(listener->fd);
854 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
859 * Some sanity checks, based on the packet code.
862 case PW_ACCOUNTING_REQUEST:
863 RAD_STATS_CLIENT_INC(listener, client, total_requests);
864 fun = rad_accounting;
867 case PW_STATUS_SERVER:
868 if (!mainconfig.status_server) {
869 rad_recv_discard(listener->fd);
870 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
871 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
873 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
876 fun = rad_status_server;
880 rad_recv_discard(listener->fd);
881 RAD_STATS_TYPE_INC(listener, total_unknown_types);
882 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
884 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
885 code, client->shortname, src_port);
887 } /* switch over packet types */
890 * Now that we've sanity checked everything, receive the
893 packet = rad_recv(listener->fd, 0);
895 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
896 radlog(L_ERR, "%s", fr_strerror());
901 * There can be no duplicate accounting packets.
903 if (!received_request(listener, packet, prequest, client)) {
904 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
905 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
917 * Recieve packets from a proxy socket.
919 static int proxy_socket_recv(rad_listen_t *listener,
920 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
923 RADIUS_PACKET *packet;
924 RAD_REQUEST_FUNP fun = NULL;
927 packet = rad_recv(listener->fd, 0);
929 radlog(L_ERR, "%s", fr_strerror());
934 * FIXME: Client MIB updates?
936 switch(packet->code) {
937 case PW_AUTHENTICATION_ACK:
938 case PW_ACCESS_CHALLENGE:
939 case PW_AUTHENTICATION_REJECT:
940 fun = rad_authenticate;
943 #ifdef WITH_ACCOUNTING
944 case PW_ACCOUNTING_RESPONSE:
945 fun = rad_accounting;
951 * FIXME: Update MIB for packet types?
953 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
954 "from home server %s port %d - ID %d : IGNORED",
956 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
957 packet->src_port, packet->id);
962 request = received_proxy_response(packet);
967 rad_assert(fun != NULL);
976 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
978 if (!request->reply->code) return 0;
980 rad_encode(request->reply, request->packet,
981 request->client->secret);
982 rad_sign(request->reply, request->packet,
983 request->client->secret);
989 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
991 if (rad_verify(request->packet, NULL,
992 request->client->secret) < 0) {
996 return rad_decode(request->packet, NULL,
997 request->client->secret);
1001 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1003 rad_encode(request->proxy, NULL, request->home_server->secret);
1004 rad_sign(request->proxy, NULL, request->home_server->secret);
1010 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1012 if (rad_verify(request->proxy_reply, request->proxy,
1013 request->home_server->secret) < 0) {
1017 return rad_decode(request->proxy_reply, request->proxy,
1018 request->home_server->secret);
1024 #include "command.c"
1026 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1028 { common_socket_parse, NULL,
1029 stats_socket_recv, auth_socket_send,
1030 socket_print, client_socket_encode, client_socket_decode },
1033 * This always gets defined.
1035 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1040 { common_socket_parse, NULL,
1041 proxy_socket_recv, proxy_socket_send,
1042 socket_print, proxy_socket_encode, proxy_socket_decode },
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},
1059 { detail_parse, detail_free,
1060 detail_recv, detail_send,
1061 detail_print, detail_encode, detail_decode },
1065 /* vlan query protocol */
1066 { common_socket_parse, NULL,
1067 vqp_socket_recv, vqp_socket_send,
1068 socket_print, vqp_socket_encode, vqp_socket_decode },
1072 /* dhcp query protocol */
1073 { dhcp_socket_parse, NULL,
1074 dhcp_socket_recv, dhcp_socket_send,
1075 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1078 #ifdef WITH_COMMAND_SOCKET
1079 /* TCP command socket */
1080 { command_socket_parse, NULL,
1081 command_domain_accept, command_domain_send,
1082 command_socket_print, command_socket_encode, command_socket_decode },
1090 * Binds a listener to a socket.
1092 static int listen_bind(rad_listen_t *this)
1094 struct sockaddr_storage salocal;
1096 listen_socket_t *sock = this->data;
1099 * If the port is zero, then it means the appropriate
1100 * thing from /etc/services.
1102 if (sock->port == 0) {
1103 struct servent *svp;
1105 switch (this->type) {
1106 case RAD_LISTEN_AUTH:
1107 svp = getservbyname ("radius", "udp");
1109 sock->port = ntohs(svp->s_port);
1111 sock->port = PW_AUTH_UDP_PORT;
1115 #ifdef WITH_ACCOUNTING
1116 case RAD_LISTEN_ACCT:
1117 svp = getservbyname ("radacct", "udp");
1119 sock->port = ntohs(svp->s_port);
1121 sock->port = PW_ACCT_UDP_PORT;
1127 case RAD_LISTEN_PROXY:
1133 case RAD_LISTEN_VQP:
1139 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1145 * Copy fr_socket() here, as we may need to bind to a device.
1147 this->fd = socket(sock->ipaddr.af, SOCK_DGRAM, 0);
1149 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
1153 #ifdef SO_BINDTODEVICE
1155 * Bind to a device BEFORE touching IP addresses.
1157 if (sock->interface) {
1159 strcpy(ifreq.ifr_name, sock->interface);
1161 if (setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1162 (char *)&ifreq, sizeof(ifreq)) < 0) {
1164 radlog(L_ERR, "Failed opening to interface %s: %s",
1165 sock->interface, strerror(errno));
1167 } /* else it worked. */
1171 #ifdef WITH_UDPFROMTO
1173 * Initialize udpfromto for all sockets.
1175 if (udpfromto_init(this->fd) != 0) {
1182 * Set up sockaddr stuff.
1184 if (!fr_ipaddr2sockaddr(&sock->ipaddr, sock->port, &salocal, &salen)) {
1189 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1190 if (sock->ipaddr.af == AF_INET6) {
1192 * Listening on '::' does NOT get you IPv4 to
1193 * IPv6 mapping. You've got to listen on an IPv4
1194 * address, too. This makes the rest of the server
1195 * design a little simpler.
1199 if (IN6_IS_ADDR_UNSPECIFIED(&sock->ipaddr.ipaddr.ip6addr)) {
1202 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
1203 (char *)&on, sizeof(on));
1205 #endif /* IPV6_V6ONLY */
1207 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
1209 if (bind(this->fd, (struct sockaddr *) &salocal, salen) < 0) {
1211 radlog(L_ERR, "Failed binding to socket: %s\n",
1217 * FreeBSD jail issues. We bind to 0.0.0.0, but the
1218 * kernel instead binds us to a 1.2.3.4. If this
1219 * happens, notice, and remember our real IP.
1222 struct sockaddr_storage src;
1223 socklen_t sizeof_src = sizeof(src);
1225 memset(&src, 0, sizeof_src);
1226 if (getsockname(this->fd, (struct sockaddr *) &src,
1228 radlog(L_ERR, "Failed getting socket name: %s",
1233 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
1234 &sock->ipaddr, &sock->port)) {
1235 radlog(L_ERR, "Socket has unsupported address family");
1244 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
1245 radlog(L_ERR, "Failure getting socket flags: %s)\n",
1250 flags |= O_NONBLOCK;
1251 if( fcntl(this->fd, F_SETFL, flags) < 0) {
1252 radlog(L_ERR, "Failure setting socket flags: %s)\n",
1264 * Allocate & initialize a new listener.
1266 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
1270 this = rad_malloc(sizeof(*this));
1271 memset(this, 0, sizeof(*this));
1274 this->recv = master_listen[this->type].recv;
1275 this->send = master_listen[this->type].send;
1276 this->print = master_listen[this->type].print;
1277 this->encode = master_listen[this->type].encode;
1278 this->decode = master_listen[this->type].decode;
1282 case RAD_LISTEN_NONE:
1284 case RAD_LISTEN_AUTH:
1285 #ifdef WITH_ACCOUNTING
1286 case RAD_LISTEN_ACCT:
1289 case RAD_LISTEN_PROXY:
1292 case RAD_LISTEN_VQP:
1295 case RAD_LISTEN_DHCP:
1297 this->data = rad_malloc(sizeof(listen_socket_t));
1298 memset(this->data, 0, sizeof(listen_socket_t));
1302 case RAD_LISTEN_DETAIL:
1307 #ifdef WITH_COMMAND_SOCKET
1308 case RAD_LISTEN_COMMAND:
1309 this->data = rad_malloc(sizeof(fr_command_socket_t));
1310 memset(this->data, 0, sizeof(fr_command_socket_t));
1315 rad_assert("Unsupported option!" == NULL);
1325 * Externally visible function for creating a new proxy LISTENER.
1327 * For now, don't take ipaddr or port.
1329 * Not thread-safe, but all calls to it are protected by the
1330 * proxy mutex in request_list.c
1332 rad_listen_t *proxy_new_listener()
1334 int last_proxy_port, port;
1335 rad_listen_t *this, *tmp, **last;
1336 listen_socket_t *sock, *old;
1338 this = listen_alloc(RAD_LISTEN_PROXY);
1341 * Find an existing proxy socket to copy.
1343 * FIXME: Make it per-realm, or per-home server!
1345 last_proxy_port = 0;
1347 last = &mainconfig.listen;
1348 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1349 if (tmp->type == RAD_LISTEN_PROXY) {
1351 if (sock->port > last_proxy_port) {
1352 last_proxy_port = sock->port + 1;
1354 if (!old) old = sock;
1357 last = &(tmp->next);
1362 return NULL; /* This is a serious error. */
1366 * FIXME: find a new IP address to listen on?
1368 * This could likely be done in the "home server"
1369 * configuration, to have per-home-server source IP's.
1372 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1375 * Keep going until we find an unused port.
1377 for (port = last_proxy_port; port < 64000; port++) {
1379 if (listen_bind(this) == 0) {
1381 * Add the new listener to the list of
1394 static const FR_NAME_NUMBER listen_compare[] = {
1396 { "status", RAD_LISTEN_NONE },
1398 { "auth", RAD_LISTEN_AUTH },
1399 #ifdef WITH_ACCOUNTING
1400 { "acct", RAD_LISTEN_ACCT },
1403 { "detail", RAD_LISTEN_DETAIL },
1406 { "proxy", RAD_LISTEN_PROXY },
1409 { "vmps", RAD_LISTEN_VQP },
1412 { "dhcp", RAD_LISTEN_DHCP },
1414 #ifdef WITH_COMMAND_SOCKET
1415 { "control", RAD_LISTEN_COMMAND },
1421 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1429 cf_log_info(cs, "listen {");
1431 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1433 if (rcode < 0) return NULL;
1436 cf_log_err(cf_sectiontoitem(cs),
1437 "No type specified in listen section");
1441 type = fr_str2int(listen_compare, listen_type, -1);
1443 cf_log_err(cf_sectiontoitem(cs),
1444 "Invalid type \"%s\" in listen section.",
1452 * Allow listen sections in the default config to
1453 * refer to a server.
1456 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1458 if (rcode == 1) { /* compatiblity with 2.0-pre */
1459 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1462 if (rcode < 0) return NULL;
1466 * Set up cross-type data.
1468 this = listen_alloc(type);
1469 this->server = server;
1473 * Call per-type parser.
1475 if (master_listen[type].parse(cs, this) < 0) {
1480 cf_log_info(cs, "}");
1486 * Generate a list of listeners. Takes an input list of
1487 * listeners, too, so we don't close sockets with waiting packets.
1489 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1491 int override = FALSE;
1493 CONF_SECTION *cs = NULL;
1494 rad_listen_t **last;
1496 fr_ipaddr_t server_ipaddr;
1499 int defined_proxy = 0;
1503 * We shouldn't be called with a pre-existing list.
1505 rad_assert(head && (*head == NULL));
1508 server_ipaddr.af = AF_UNSPEC;
1511 * If the port is specified on the command-line,
1512 * it over-rides the configuration file.
1514 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1516 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1519 * If the IP address was configured on the command-line,
1520 * use that as the "bind_address"
1522 if (mainconfig.myip.af != AF_UNSPEC) {
1523 memcpy(&server_ipaddr, &mainconfig.myip,
1524 sizeof(server_ipaddr));
1530 * Else look for bind_address and/or listen sections.
1532 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1533 rcode = cf_item_parse(config, "bind_address",
1535 &server_ipaddr.ipaddr.ip4addr, NULL);
1536 if (rcode < 0) return -1; /* error parsing it */
1538 if (rcode == 0) { /* successfully parsed IPv4 */
1539 listen_socket_t *sock;
1540 server_ipaddr.af = AF_INET;
1542 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'.");
1546 if (strcmp(progname, "vmpsd") == 0) {
1547 this = listen_alloc(RAD_LISTEN_VQP);
1548 if (!auth_port) auth_port = 1589;
1551 this = listen_alloc(RAD_LISTEN_AUTH);
1555 sock->ipaddr = server_ipaddr;
1556 sock->port = auth_port;
1558 sock->clients = clients_parse_section(config);
1559 if (!sock->clients) {
1560 cf_log_err(cf_sectiontoitem(config),
1561 "Failed to find any clients for this listen section");
1566 if (listen_bind(this) < 0) {
1568 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1572 auth_port = sock->port; /* may have been updated in listen_bind */
1574 cs = cf_section_sub_find_name2(config, "server",
1576 if (cs) this->server = mainconfig.name;
1580 last = &(this->next);
1586 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
1589 #ifdef WITH_ACCOUNTING
1591 * Open Accounting Socket.
1593 * If we haven't already gotten acct_port from
1594 * /etc/services, then make it auth_port + 1.
1596 this = listen_alloc(RAD_LISTEN_ACCT);
1600 * Create the accounting socket.
1602 * The accounting port is always the
1603 * authentication port + 1
1605 sock->ipaddr = server_ipaddr;
1606 sock->port = auth_port + 1;
1608 sock->clients = clients_parse_section(config);
1609 if (!sock->clients) {
1610 cf_log_err(cf_sectiontoitem(config),
1611 "Failed to find any clients for this listen section");
1615 if (listen_bind(this) < 0) {
1618 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1623 cs = cf_section_sub_find_name2(config, "server",
1625 if (cs) this->server = mainconfig.name;
1629 last = &(this->next);
1631 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1632 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1638 * They specified an IP on the command-line, ignore
1639 * all listen sections except the one in '-n'.
1641 if (mainconfig.myip.af != AF_UNSPEC) {
1642 CONF_SECTION *subcs;
1643 const char *name2 = cf_section_name2(cs);
1645 cs = cf_section_sub_find_name2(config, "server",
1647 if (!cs) goto do_proxy;
1650 * Should really abstract this code...
1652 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1654 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1655 this = listen_parse(subcs, name2);
1662 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1666 last = &(this->next);
1667 } /* loop over "listen" directives in server <foo> */
1673 * Walk through the "listen" sections, if they exist.
1675 for (cs = cf_subsection_find_next(config, NULL, "listen");
1677 cs = cf_subsection_find_next(config, cs, "listen")) {
1678 this = listen_parse(cs, NULL);
1685 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1689 last = &(this->next);
1693 * Check virtual servers for "listen" sections, too.
1695 * FIXME: Move to virtual server init?
1697 for (cs = cf_subsection_find_next(config, NULL, "server");
1699 cs = cf_subsection_find_next(config, cs, "server")) {
1700 CONF_SECTION *subcs;
1701 const char *name2 = cf_section_name2(cs);
1703 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1705 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1706 this = listen_parse(subcs, name2);
1713 if (this->type == RAD_LISTEN_PROXY) {
1714 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
1721 last = &(this->next);
1722 } /* loop over "listen" directives in virtual servers */
1723 } /* loop over virtual servers */
1726 * If we're proxying requests, open the proxy FD.
1727 * Otherwise, don't do anything.
1731 if (mainconfig.proxy_requests == TRUE) {
1733 listen_socket_t *sock = NULL;
1736 * No sockets to receive packets, therefore
1737 * proxying is pointless.
1739 if (!*head) return -1;
1741 if (defined_proxy) goto done;
1744 * Find the first authentication port,
1747 for (this = *head; this != NULL; this = this->next) {
1748 if (this->type == RAD_LISTEN_AUTH) {
1750 if (server_ipaddr.af == AF_UNSPEC) {
1751 server_ipaddr = sock->ipaddr;
1753 port = sock->port + 2; /* skip acct port */
1756 if (this->type == RAD_LISTEN_VQP) {
1758 if (server_ipaddr.af == AF_UNSPEC) {
1759 server_ipaddr = sock->ipaddr;
1761 port = sock->port + 1;
1766 if (port < 0) port = 1024 + (fr_rand() & 0x1ff);
1769 * Address is still unspecified, use IPv4.
1771 if (server_ipaddr.af == AF_UNSPEC) {
1772 server_ipaddr.af = AF_INET;
1773 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1776 this = listen_alloc(RAD_LISTEN_PROXY);
1780 * Create the first proxy socket.
1782 sock->ipaddr = server_ipaddr;
1785 * Try to find a proxy port (value doesn't matter)
1787 for (sock->port = port;
1790 if (listen_bind(this) == 0) {
1792 last = &(this->next); /* just in case */
1797 if (sock->port >= 64000) {
1800 radlog(L_ERR, "Failed to open socket for proxying");
1805 done: /* used only in proxy code. */
1812 * Free a linked list of listeners;
1814 void listen_free(rad_listen_t **head)
1818 if (!head || !*head) return;
1822 rad_listen_t *next = this->next;
1825 * Other code may have eaten the FD.
1827 if (this->fd >= 0) close(this->fd);
1829 if (master_listen[this->type].free) {
1830 master_listen[this->type].free(this);
1842 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
1847 for (this = mainconfig.listen; this != NULL; this = this->next) {
1848 listen_socket_t *sock;
1850 if ((this->type != RAD_LISTEN_AUTH) &&
1851 (this->type != RAD_LISTEN_ACCT)) continue;
1855 if ((sock->port == port) &&
1856 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {
1857 return sock->clients;
1864 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
1868 for (this = mainconfig.listen; this != NULL; this = this->next) {
1869 listen_socket_t *sock;
1871 if ((this->type != RAD_LISTEN_AUTH) &&
1872 (this->type != RAD_LISTEN_ACCT)) continue;
1876 if ((sock->port == port) &&
1877 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {