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/radius_snmp.h>
29 #include <freeradius-devel/modules.h>
30 #include <freeradius-devel/rad_assert.h>
31 #include <freeradius-devel/vqp.h>
32 #include <freeradius-devel/dhcp.h>
34 #include <freeradius-devel/vmps.h>
35 #include <freeradius-devel/detail.h>
37 #ifdef HAVE_SYS_RESOURCE_H
38 #include <sys/resource.h>
51 * We'll use this below.
53 typedef int (*rad_listen_parse_t)(CONF_SECTION *, rad_listen_t *);
54 typedef void (*rad_listen_free_t)(rad_listen_t *);
56 typedef struct rad_listen_master_t {
57 rad_listen_parse_t parse;
58 rad_listen_free_t free;
59 rad_listen_recv_t recv;
60 rad_listen_send_t send;
61 rad_listen_print_t print;
62 rad_listen_encode_t encode;
63 rad_listen_decode_t decode;
64 } rad_listen_master_t;
66 typedef struct listen_socket_t {
72 #ifdef SO_BINDTODEVICE
73 const char *interface;
75 RADCLIENT_LIST *clients;
79 * Find a per-socket client.
81 RADCLIENT *client_listener_find(const rad_listen_t *listener,
82 const fr_ipaddr_t *ipaddr)
84 #ifdef WITH_DYNAMIC_CLIENTS
87 listen_socket_t *sock;
89 RADCLIENT *client, *created;
91 RADCLIENT_LIST *clients;
93 rad_assert(listener != NULL);
94 rad_assert(ipaddr != NULL);
96 rad_assert((listener->type == RAD_LISTEN_AUTH) ||
97 (listener->type == RAD_LISTEN_ACCT) ||
98 (listener->type == RAD_LISTEN_VQP) ||
99 (listener->type == RAD_LISTEN_DHCP));
101 clients = ((listen_socket_t *)listener->data)->clients;
104 * This HAS to have been initialized previously.
106 rad_assert(clients != NULL);
108 #ifdef WITH_DYNAMIC_CLIENTS
109 client = client_find(clients, ipaddr);
110 if (!client) return NULL;
113 * No server defined, and it's not dynamic. Return it.
115 if (!client->client_server && !client->dynamic) return client;
120 * It's a dynamically generated client, check it.
122 if (client->dynamic) {
124 * Lives forever. Return it.
126 if (client->lifetime == 0) return client;
129 * Rate-limit the deletion of known clients.
130 * This makes them last a little longer, but
131 * prevents the server from melting down if (say)
132 * 10k clients all expire at once.
134 if (now == client->last_new_client) return client;
137 * It's not dead yet. Return it.
139 if ((client->created + client->lifetime) > now) return client;
142 * It's dead, Jim. Delete it.
144 client_delete(clients, client);
147 * Go find the enclosing network again.
149 client = client_find(clients, ipaddr);
154 if (!client) return NULL;
155 if (!client->client_server) return NULL;
158 * At this point, 'client' is the enclosing
159 * network that configures where dynamic clients
162 rad_assert(client->dynamic == 0);
165 * The IP is unknown, so we've found an enclosing
166 * network. Enable DoS protection. We only
167 * allow one new client per second. Known
168 * clients aren't subject to this restriction.
170 if (now == client->last_new_client) return NULL;
173 client->last_new_client = now;
175 request = request_alloc();
176 if (!request) return NULL;
178 request->listener = listener;
179 request->client = client;
180 request->packet = rad_alloc(0);
181 if (!request->packet) {
182 request_free(&request);
185 request->reply = rad_alloc(0);
186 if (!request->reply) {
187 request_free(&request);
190 request->packet->timestamp = request->timestamp;
192 request->priority = listener->type;
193 request->server = client->client_server;
194 request->root = &mainconfig;
197 * Run a fake request through the given virtual server.
198 * Look for FreeRADIUS-Client-IP-Address
199 * FreeRADIUS-Client-Secret
202 * and create the RADCLIENT structure from that.
205 sock = listener->data;
206 request->packet->sockfd = listener->fd;
207 request->packet->src_ipaddr = *ipaddr;
208 request->packet->src_port = 0; /* who cares... */
209 request->packet->dst_ipaddr = sock->ipaddr;
210 request->packet->dst_port = sock->port;
212 request->reply->sockfd = request->packet->sockfd;
213 request->reply->dst_ipaddr = request->packet->src_ipaddr;
214 request->reply->src_ipaddr = request->packet->dst_ipaddr;
215 request->reply->dst_port = request->packet->src_port;
216 request->reply->src_port = request->packet->dst_port;
217 request->reply->id = request->packet->id;
218 request->reply->code = 0; /* UNKNOWN code */
221 DEBUG("server %s {", request->server);
223 rcode = module_authorize(0, request);
225 DEBUG("} # server %s", request->server);
227 if (rcode != RLM_MODULE_OK) {
228 request_free(&request);
232 created = client_create(clients, request);
234 request_free(&request);
236 return created; /* may be NULL */
238 return client_find(clients, ipaddr);
242 static int listen_bind(rad_listen_t *this);
246 * Process and reply to a server-status request.
247 * Like rad_authenticate and rad_accounting this should
248 * live in it's own file but it's so small we don't bother.
250 static int rad_status_server(REQUEST *request)
252 int rcode = RLM_MODULE_OK;
255 switch (request->listener->type) {
256 case RAD_LISTEN_AUTH:
257 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
259 rcode = module_authorize(dval->value, request);
261 rcode = RLM_MODULE_OK;
266 case RLM_MODULE_UPDATED:
267 request->reply->code = PW_AUTHENTICATION_ACK;
270 case RLM_MODULE_FAIL:
271 case RLM_MODULE_HANDLED:
272 request->reply->code = 0; /* don't reply */
276 case RLM_MODULE_REJECT:
277 request->reply->code = PW_AUTHENTICATION_REJECT;
282 #ifdef WITH_ACCOUNTING
283 case RAD_LISTEN_ACCT:
284 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
286 rcode = module_accounting(dval->value, request);
288 rcode = RLM_MODULE_OK;
293 case RLM_MODULE_UPDATED:
294 request->reply->code = PW_ACCOUNTING_RESPONSE;
298 request->reply->code = 0; /* don't reply */
309 request_stats_reply(request);
316 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
319 listen_socket_t *sock = this->data;
322 switch (this->type) {
323 case RAD_LISTEN_AUTH:
324 name = "authentication";
327 #ifdef WITH_ACCOUNTING
328 case RAD_LISTEN_ACCT:
334 case RAD_LISTEN_PROXY:
346 case RAD_LISTEN_DHCP:
356 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
357 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
361 #ifdef SO_BINDTODEVICE
362 if (sock->interface) {
363 ADDSTRING(" interface ");
364 ADDSTRING(sock->interface);
368 ADDSTRING(" address ");
370 if ((sock->ipaddr.af == AF_INET) &&
371 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
372 strlcpy(buffer, "*", bufsize);
374 ip_ntoh(&sock->ipaddr, buffer, bufsize);
379 snprintf(buffer, bufsize, "%d", sock->port);
383 ADDSTRING(" as server ");
384 ADDSTRING(this->server);
395 * Parse an authentication or accounting socket.
397 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
402 listen_socket_t *sock = this->data;
403 char *section_name = NULL;
404 CONF_SECTION *client_cs, *parentcs;
409 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
410 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
411 &ipaddr.ipaddr.ip4addr, NULL);
412 if (rcode < 0) return -1;
414 if (rcode == 0) { /* successfully parsed IPv4 */
417 } else { /* maybe IPv6? */
418 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
419 &ipaddr.ipaddr.ip6addr, NULL);
420 if (rcode < 0) return -1;
423 cf_log_err(cf_sectiontoitem(cs),
424 "No address specified in listen section");
427 ipaddr.af = AF_INET6;
430 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
432 if (rcode < 0) return -1;
434 if ((listen_port < 0) || (listen_port > 65500)) {
435 cf_log_err(cf_sectiontoitem(cs),
436 "Invalid value for \"port\"");
440 sock->ipaddr = ipaddr;
441 sock->port = listen_port;
444 * If we can bind to interfaces, do so,
447 if (cf_pair_find(cs, "interface")) {
448 #ifndef SO_BINDTODEVICE
449 cf_log_err(cf_sectiontoitem(cs),
450 "System does not support binding to interfaces. Delete this line from the configuration file.");
454 CONF_PAIR *cp = cf_pair_find(cs, "interface");
456 rad_assert(cp != NULL);
457 value = cf_pair_value(cp);
459 cf_log_err(cf_sectiontoitem(cs),
460 "No interface name given");
463 sock->interface = value;
468 * And bind it to the port.
470 if (listen_bind(this) < 0) {
472 cf_log_err(cf_sectiontoitem(cs),
473 "Error binding to port for %s port %d",
474 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
481 * Proxy sockets don't have clients.
483 if (this->type == RAD_LISTEN_PROXY) return 0;
487 * The more specific configurations are preferred to more
491 parentcs = cf_top_section(cs);
492 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
493 §ion_name, NULL);
494 if (rcode < 0) return -1; /* bad string */
497 * Explicit list given: use it.
499 client_cs = cf_section_sub_find_name2(parentcs,
503 client_cs = cf_section_find(section_name);
506 cf_log_err(cf_sectiontoitem(cs),
507 "Failed to find clients %s {...}",
513 } /* else there was no "clients = " entry. */
516 CONF_SECTION *server_cs;
518 server_cs = cf_section_sub_find_name2(parentcs,
522 * Found a "server foo" section. If there are clients
526 (cf_section_sub_find(server_cs, "client") != NULL)) {
527 client_cs = server_cs;
532 * Still nothing. Look for global clients.
534 if (!client_cs) client_cs = parentcs;
536 sock->clients = clients_parse_section(client_cs);
537 if (!sock->clients) {
538 cf_log_err(cf_sectiontoitem(cs),
539 "Failed to find any clients for this listen section");
547 * Send an authentication response packet
549 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
551 rad_assert(request->listener == listener);
552 rad_assert(listener->send == auth_socket_send);
554 return rad_send(request->reply, request->packet,
555 request->client->secret);
559 #ifdef WITH_ACCOUNTING
561 * Send an accounting response packet (or not)
563 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
565 rad_assert(request->listener == listener);
566 rad_assert(listener->send == acct_socket_send);
569 * Accounting reject's are silently dropped.
571 * We do it here to avoid polluting the rest of the
572 * code with this knowledge
574 if (request->reply->code == 0) return 0;
576 return rad_send(request->reply, request->packet,
577 request->client->secret);
583 * Send a packet to a home server.
585 * FIXME: have different code for proxy auth & acct!
587 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
589 listen_socket_t *sock = listener->data;
591 rad_assert(request->proxy_listener == listener);
592 rad_assert(listener->send == proxy_socket_send);
594 request->proxy->src_ipaddr = sock->ipaddr;
595 request->proxy->src_port = sock->port;
597 return rad_send(request->proxy, request->packet,
598 request->home_server->secret);
604 * Check if an incoming request is "ok"
606 * It takes packets, not requests. It sees if the packet looks
607 * OK. If so, it does a number of sanity checks on it.
609 static int auth_socket_recv(rad_listen_t *listener,
610 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
614 RADIUS_PACKET *packet;
615 RAD_REQUEST_FUNP fun = NULL;
618 fr_ipaddr_t src_ipaddr;
620 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
621 if (rcode < 0) return 0;
623 RAD_STATS_TYPE_INC(listener, total_requests);
625 if (rcode < 20) { /* AUTH_HDR_LEN */
626 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
630 if ((client = client_listener_find(listener,
631 &src_ipaddr)) == NULL) {
632 rad_recv_discard(listener->fd);
633 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
635 if (debug_flag > 0) {
638 listener->print(listener, name, sizeof(name));
641 * This is debugging rather than logging, so that
642 * DoS attacks don't affect us.
644 DEBUG("Ignoring request to %s from unknown client %s port %d",
646 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
647 buffer, sizeof(buffer)), src_port);
654 * Some sanity checks, based on the packet code.
657 case PW_AUTHENTICATION_REQUEST:
658 RAD_STATS_CLIENT_INC(listener, client, requests);
659 fun = rad_authenticate;
662 case PW_STATUS_SERVER:
663 if (!mainconfig.status_server) {
664 rad_recv_discard(listener->fd);
665 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
666 RAD_STATS_CLIENT_INC(listener, client, packets_dropped);
667 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
670 fun = rad_status_server;
674 rad_recv_discard(listener->fd);
675 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
676 RAD_STATS_CLIENT_INC(listener, client, unknown_types);
678 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
679 code, client->shortname, src_port);
682 } /* switch over packet types */
685 * Now that we've sanity checked everything, receive the
688 packet = rad_recv(listener->fd, client->message_authenticator);
690 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
691 DEBUG("%s", librad_errstr);
695 if (!received_request(listener, packet, prequest, client)) {
696 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
697 RAD_STATS_CLIENT_INC(listener, client, packets_dropped);
707 #ifdef WITH_ACCOUNTING
709 * Receive packets from an accounting socket
711 static int acct_socket_recv(rad_listen_t *listener,
712 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
716 RADIUS_PACKET *packet;
717 RAD_REQUEST_FUNP fun = NULL;
720 fr_ipaddr_t src_ipaddr;
722 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
723 if (rcode < 0) return 0;
725 RAD_STATS_TYPE_INC(listener, total_requests);
727 if (rcode < 20) { /* AUTH_HDR_LEN */
728 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
732 if ((client = client_listener_find(listener,
733 &src_ipaddr)) == NULL) {
734 rad_recv_discard(listener->fd);
735 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
738 * This is debugging rather than logging, so that
739 * DoS attacks don't affect us.
741 if (debug_flag > 0) {
744 listener->print(listener, name, sizeof(name));
746 DEBUG("Ignoring request to %s from unknown client %s port %d",
748 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
749 buffer, sizeof(buffer)), src_port);
756 * Some sanity checks, based on the packet code.
759 case PW_ACCOUNTING_REQUEST:
760 RAD_STATS_CLIENT_INC(listener, client, requests);
761 fun = rad_accounting;
764 case PW_STATUS_SERVER:
765 if (!mainconfig.status_server) {
766 rad_recv_discard(listener->fd);
767 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
768 RAD_STATS_CLIENT_INC(listener, client, unknown_types);
770 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
773 fun = rad_status_server;
777 rad_recv_discard(listener->fd);
778 RAD_STATS_TYPE_INC(listener, total_unknown_types);
779 RAD_STATS_CLIENT_INC(listener, client, unknown_types);
781 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
782 code, client->shortname, src_port);
784 } /* switch over packet types */
787 * Now that we've sanity checked everything, receive the
790 packet = rad_recv(listener->fd, 0);
792 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
793 radlog(L_ERR, "%s", librad_errstr);
798 * There can be no duplicate accounting packets.
800 if (!received_request(listener, packet, prequest, client)) {
801 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
802 RAD_STATS_CLIENT_INC(listener, client, packets_dropped);
814 * Recieve packets from a proxy socket.
816 static int proxy_socket_recv(rad_listen_t *listener,
817 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
820 RADIUS_PACKET *packet;
821 RAD_REQUEST_FUNP fun = NULL;
824 packet = rad_recv(listener->fd, 0);
826 radlog(L_ERR, "%s", librad_errstr);
831 * FIXME: Client MIB updates?
833 switch(packet->code) {
834 case PW_AUTHENTICATION_ACK:
835 case PW_ACCESS_CHALLENGE:
836 case PW_AUTHENTICATION_REJECT:
837 fun = rad_authenticate;
840 #ifdef WITH_ACCOUNTING
841 case PW_ACCOUNTING_RESPONSE:
842 fun = rad_accounting;
848 * FIXME: Update MIB for packet types?
850 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
851 "from home server %s port %d - ID %d : IGNORED",
853 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
854 packet->src_port, packet->id);
859 request = received_proxy_response(packet);
864 rad_assert(fun != NULL);
873 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
875 if (!request->reply->code) return 0;
877 rad_encode(request->reply, request->packet,
878 request->client->secret);
879 rad_sign(request->reply, request->packet,
880 request->client->secret);
886 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
888 if (rad_verify(request->packet, NULL,
889 request->client->secret) < 0) {
893 return rad_decode(request->packet, NULL,
894 request->client->secret);
898 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
900 rad_encode(request->proxy, NULL, request->home_server->secret);
901 rad_sign(request->proxy, NULL, request->home_server->secret);
907 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
909 if (rad_verify(request->proxy_reply, request->proxy,
910 request->home_server->secret) < 0) {
914 return rad_decode(request->proxy_reply, request->proxy,
915 request->home_server->secret);
920 static int radius_snmp_recv(rad_listen_t *listener,
921 UNUSED RAD_REQUEST_FUNP *pfun,
922 UNUSED REQUEST **prequest)
924 if ((rad_snmp.smux_fd >= 0) &&
925 (rad_snmp.smux_event == SMUX_READ)) {
930 * If we've got to re-connect, then do so now,
931 * before calling select again.
933 if (rad_snmp.smux_event == SMUX_CONNECT) {
938 * Reset this every time, as the smux connect may have
939 * opened a new socket.
941 listener->fd = rad_snmp.smux_fd;
947 static int radius_snmp_print(UNUSED rad_listen_t *this, char *buffer, size_t bufsize)
949 return snprintf(buffer, bufsize, "SMUX with OID .1.3.6.1.4.1.11344.1.1.1");
956 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
957 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
961 { common_socket_parse, NULL,
962 proxy_socket_recv, proxy_socket_send,
963 socket_print, proxy_socket_encode, proxy_socket_decode },
965 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
969 { common_socket_parse, NULL,
970 auth_socket_recv, auth_socket_send,
971 socket_print, client_socket_encode, client_socket_decode },
973 #ifdef WITH_ACCOUNTING
975 { common_socket_parse, NULL,
976 acct_socket_recv, acct_socket_send,
977 socket_print, client_socket_encode, client_socket_decode},
979 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
984 { detail_parse, detail_free,
985 detail_recv, detail_send,
986 detail_print, detail_encode, detail_decode },
988 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
992 /* vlan query protocol */
993 { common_socket_parse, NULL,
994 vqp_socket_recv, vqp_socket_send,
995 socket_print, vqp_socket_encode, vqp_socket_decode },
997 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1001 /* dhcp query protocol */
1002 { dhcp_socket_parse, NULL,
1003 dhcp_socket_recv, dhcp_socket_send,
1004 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1006 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1009 { NULL, NULL, NULL, NULL, NULL, NULL, NULL} /* RAD_LISTEN_SNMP */
1015 * Binds a listener to a socket.
1017 static int listen_bind(rad_listen_t *this)
1019 struct sockaddr_storage salocal;
1021 listen_socket_t *sock = this->data;
1024 * If the port is zero, then it means the appropriate
1025 * thing from /etc/services.
1027 if (sock->port == 0) {
1028 struct servent *svp;
1030 switch (this->type) {
1031 case RAD_LISTEN_AUTH:
1032 svp = getservbyname ("radius", "udp");
1034 sock->port = ntohs(svp->s_port);
1036 sock->port = PW_AUTH_UDP_PORT;
1040 #ifdef WITH_ACCOUNTING
1041 case RAD_LISTEN_ACCT:
1042 svp = getservbyname ("radacct", "udp");
1044 sock->port = ntohs(svp->s_port);
1046 sock->port = PW_ACCT_UDP_PORT;
1052 case RAD_LISTEN_PROXY:
1058 case RAD_LISTEN_VQP:
1064 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1070 * Copy fr_socket() here, as we may need to bind to a device.
1072 this->fd = socket(sock->ipaddr.af, SOCK_DGRAM, 0);
1074 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
1078 #ifdef SO_BINDTODEVICE
1080 * Bind to a device BEFORE touching IP addresses.
1082 if (sock->interface) {
1084 strcpy(ifreq.ifr_name, sock->interface);
1086 if (setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1087 (char *)&ifreq, sizeof(ifreq)) < 0) {
1089 radlog(L_ERR, "Failed opening to interface %s: %s",
1090 sock->interface, strerror(errno));
1092 } /* else it worked. */
1096 #ifdef WITH_UDPFROMTO
1098 * Initialize udpfromto for all sockets.
1100 if (udpfromto_init(this->fd) != 0) {
1107 * Set up sockaddr stuff.
1109 if (!fr_ipaddr2sockaddr(&sock->ipaddr, sock->port, &salocal, &salen)) {
1114 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1115 if (sock->ipaddr.af == AF_INET6) {
1117 * Listening on '::' does NOT get you IPv4 to
1118 * IPv6 mapping. You've got to listen on an IPv4
1119 * address, too. This makes the rest of the server
1120 * design a little simpler.
1124 if (IN6_IS_ADDR_UNSPECIFIED(&sock->ipaddr.ipaddr.ip6addr)) {
1127 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
1128 (char *)&on, sizeof(on));
1130 #endif /* IPV6_V6ONLY */
1131 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
1134 if (bind(this->fd, (struct sockaddr *) &salocal, salen) < 0) {
1136 radlog(L_ERR, "Failed binding to socket: %s\n",
1142 * FreeBSD jail issues. We bind to 0.0.0.0, but the
1143 * kernel instead binds us to a 1.2.3.4. If this
1144 * happens, notice, and remember our real IP.
1147 struct sockaddr_storage src;
1148 socklen_t sizeof_src = sizeof(src);
1150 memset(&src, 0, sizeof_src);
1151 if (getsockname(this->fd, (struct sockaddr *) &src,
1153 radlog(L_ERR, "Failed getting socket name: %s",
1158 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
1159 &sock->ipaddr, &sock->port)) {
1160 radlog(L_ERR, "Socket has unsupported address family");
1169 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
1170 radlog(L_ERR, "Failure getting socket flags: %s)\n",
1175 flags |= O_NONBLOCK;
1176 if( fcntl(this->fd, F_SETFL, flags) < 0) {
1177 radlog(L_ERR, "Failure setting socket flags: %s)\n",
1189 * Allocate & initialize a new listener.
1191 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
1195 this = rad_malloc(sizeof(*this));
1196 memset(this, 0, sizeof(*this));
1199 this->recv = master_listen[this->type].recv;
1200 this->send = master_listen[this->type].send;
1201 this->print = master_listen[this->type].print;
1202 this->encode = master_listen[this->type].encode;
1203 this->decode = master_listen[this->type].decode;
1206 case RAD_LISTEN_AUTH:
1207 case RAD_LISTEN_ACCT:
1208 case RAD_LISTEN_PROXY:
1209 case RAD_LISTEN_VQP:
1210 case RAD_LISTEN_DHCP:
1211 this->data = rad_malloc(sizeof(listen_socket_t));
1212 memset(this->data, 0, sizeof(listen_socket_t));
1216 case RAD_LISTEN_DETAIL:
1222 rad_assert("Unsupported option!" == NULL);
1232 * Externally visible function for creating a new proxy LISTENER.
1234 * For now, don't take ipaddr or port.
1236 * Not thread-safe, but all calls to it are protected by the
1237 * proxy mutex in request_list.c
1239 rad_listen_t *proxy_new_listener()
1241 int last_proxy_port, port;
1242 rad_listen_t *this, *tmp, **last;
1243 listen_socket_t *sock, *old;
1245 this = listen_alloc(RAD_LISTEN_PROXY);
1248 * Find an existing proxy socket to copy.
1250 * FIXME: Make it per-realm, or per-home server!
1252 last_proxy_port = 0;
1254 last = &mainconfig.listen;
1255 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1256 if (tmp->type == RAD_LISTEN_PROXY) {
1258 if (sock->port > last_proxy_port) {
1259 last_proxy_port = sock->port + 1;
1261 if (!old) old = sock;
1264 last = &(tmp->next);
1269 return NULL; /* This is a serious error. */
1273 * FIXME: find a new IP address to listen on?
1275 * This could likely be done in the "home server"
1276 * configuration, to have per-home-server source IP's.
1279 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1282 * Keep going until we find an unused port.
1284 for (port = last_proxy_port; port < 64000; port++) {
1286 if (listen_bind(this) == 0) {
1288 * Add the new listener to the list of
1301 static const FR_NAME_NUMBER listen_compare[] = {
1302 { "auth", RAD_LISTEN_AUTH },
1303 #ifdef WITH_ACCOUNTING
1304 { "acct", RAD_LISTEN_ACCT },
1307 { "detail", RAD_LISTEN_DETAIL },
1310 { "proxy", RAD_LISTEN_PROXY },
1313 { "vmps", RAD_LISTEN_VQP },
1316 { "dhcp", RAD_LISTEN_DHCP },
1318 { "dhcp", RAD_LISTEN_NONE },
1324 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1332 cf_log_info(cs, "listen {");
1334 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1336 if (rcode < 0) return NULL;
1339 cf_log_err(cf_sectiontoitem(cs),
1340 "No type specified in listen section");
1344 type = fr_str2int(listen_compare, listen_type,
1346 if (type == RAD_LISTEN_NONE) {
1347 cf_log_err(cf_sectiontoitem(cs),
1348 "Invalid type \"%s\" in listen section.",
1356 * Allow listen sections in the default config to
1357 * refer to a server.
1360 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1362 if (rcode == 1) { /* compatiblity with 2.0-pre */
1363 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1366 if (rcode < 0) return NULL;
1370 * Set up cross-type data.
1372 this = listen_alloc(type);
1373 this->server = server;
1377 * Call per-type parser.
1379 if (master_listen[type].parse(cs, this) < 0) {
1384 cf_log_info(cs, "}");
1390 * Generate a list of listeners. Takes an input list of
1391 * listeners, too, so we don't close sockets with waiting packets.
1393 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1395 int override = FALSE;
1398 rad_listen_t **last;
1400 fr_ipaddr_t server_ipaddr;
1403 int defined_proxy = 0;
1407 * We shouldn't be called with a pre-existing list.
1409 rad_assert(head && (*head == NULL));
1412 server_ipaddr.af = AF_UNSPEC;
1415 * If the port is specified on the command-line,
1416 * it over-rides the configuration file.
1418 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1420 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1423 * If the IP address was configured on the command-line,
1424 * use that as the "bind_address"
1426 if (mainconfig.myip.af != AF_UNSPEC) {
1427 memcpy(&server_ipaddr, &mainconfig.myip,
1428 sizeof(server_ipaddr));
1434 * Else look for bind_address and/or listen sections.
1436 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1437 rcode = cf_item_parse(config, "bind_address",
1439 &server_ipaddr.ipaddr.ip4addr, NULL);
1440 if (rcode < 0) return -1; /* error parsing it */
1442 if (rcode == 0) { /* successfully parsed IPv4 */
1443 listen_socket_t *sock;
1444 server_ipaddr.af = AF_INET;
1446 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'.");
1450 if (strcmp(progname, "vmpsd") == 0) {
1451 this = listen_alloc(RAD_LISTEN_VQP);
1452 if (!auth_port) auth_port = 1589;
1455 this = listen_alloc(RAD_LISTEN_AUTH);
1459 sock->ipaddr = server_ipaddr;
1460 sock->port = auth_port;
1462 sock->clients = clients_parse_section(config);
1463 if (!sock->clients) {
1464 cf_log_err(cf_sectiontoitem(config),
1465 "Failed to find any clients for this listen section");
1469 if (listen_bind(this) < 0) {
1472 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1475 auth_port = sock->port; /* may have been updated in listen_bind */
1477 cs = cf_section_sub_find_name2(config, "server",
1479 if (cs) this->server = mainconfig.name;
1483 last = &(this->next);
1489 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
1492 #ifdef WITH_ACCOUNTING
1494 * Open Accounting Socket.
1496 * If we haven't already gotten acct_port from
1497 * /etc/services, then make it auth_port + 1.
1499 this = listen_alloc(RAD_LISTEN_ACCT);
1503 * Create the accounting socket.
1505 * The accounting port is always the
1506 * authentication port + 1
1508 sock->ipaddr = server_ipaddr;
1509 sock->port = auth_port + 1;
1511 sock->clients = clients_parse_section(config);
1512 if (!sock->clients) {
1513 cf_log_err(cf_sectiontoitem(config),
1514 "Failed to find any clients for this listen section");
1518 if (listen_bind(this) < 0) {
1521 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1526 cs = cf_section_sub_find_name2(config, "server",
1528 if (cs) this->server = mainconfig.name;
1532 last = &(this->next);
1534 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1535 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1541 * They specified an IP on the command-line, ignore
1542 * all listen sections except the one in '-n'.
1544 if (mainconfig.myip.af != AF_UNSPEC) {
1545 CONF_SECTION *subcs;
1546 const char *name2 = cf_section_name2(cs);
1548 cs = cf_section_sub_find_name2(config, "server",
1550 if (!cs) goto do_proxy;
1553 * Should really abstract this code...
1555 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1557 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1558 this = listen_parse(subcs, name2);
1565 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1569 last = &(this->next);
1570 } /* loop over "listen" directives in server <foo> */
1576 * Walk through the "listen" sections, if they exist.
1578 for (cs = cf_subsection_find_next(config, NULL, "listen");
1580 cs = cf_subsection_find_next(config, cs, "listen")) {
1581 this = listen_parse(cs, NULL);
1588 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1592 last = &(this->next);
1596 * Check virtual servers for "listen" sections, too.
1598 * FIXME: Move to virtual server init?
1600 for (cs = cf_subsection_find_next(config, NULL, "server");
1602 cs = cf_subsection_find_next(config, cs, "server")) {
1603 CONF_SECTION *subcs;
1604 const char *name2 = cf_section_name2(cs);
1606 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1608 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1609 this = listen_parse(subcs, name2);
1616 if (this->type == RAD_LISTEN_PROXY) {
1617 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
1624 last = &(this->next);
1625 } /* loop over "listen" directives in virtual servers */
1626 } /* loop over virtual servers */
1629 * If we're proxying requests, open the proxy FD.
1630 * Otherwise, don't do anything.
1634 if (mainconfig.proxy_requests == TRUE) {
1636 listen_socket_t *sock = NULL;
1639 * No sockets to receive packets, therefore
1640 * proxying is pointless.
1642 if (!*head) return -1;
1644 if (defined_proxy) goto do_snmp;
1647 * Find the first authentication port,
1650 for (this = *head; this != NULL; this = this->next) {
1651 if (this->type == RAD_LISTEN_AUTH) {
1653 if (server_ipaddr.af == AF_UNSPEC) {
1654 server_ipaddr = sock->ipaddr;
1656 port = sock->port + 2; /* skip acct port */
1659 if (this->type == RAD_LISTEN_VQP) {
1661 if (server_ipaddr.af == AF_UNSPEC) {
1662 server_ipaddr = sock->ipaddr;
1664 port = sock->port + 1;
1669 if (port < 0) port = 1024 + (fr_rand() & 0x1ff);
1672 * Address is still unspecified, use IPv4.
1674 if (server_ipaddr.af == AF_UNSPEC) {
1675 server_ipaddr.af = AF_INET;
1676 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1679 this = listen_alloc(RAD_LISTEN_PROXY);
1683 * Create the first proxy socket.
1685 sock->ipaddr = server_ipaddr;
1688 * Try to find a proxy port (value doesn't matter)
1690 for (sock->port = port;
1693 if (listen_bind(this) == 0) {
1695 last = &(this->next); /* just in case */
1700 if (sock->port >= 64000) {
1703 radlog(L_ERR, "Failed to open socket for proxying");
1708 do_snmp: /* used only in proxy code. */
1712 if (radius_snmp_init(config)) {
1713 this = rad_malloc(sizeof(*this));
1714 memset(this, 0, sizeof(*this));
1716 this->type = RAD_LISTEN_SNMP;
1717 this->fd = rad_snmp.smux_fd;
1719 this->recv = radius_snmp_recv;
1720 this->print = radius_snmp_print;
1723 last = &(this->next);
1731 * Free a linked list of listeners;
1733 void listen_free(rad_listen_t **head)
1737 if (!head || !*head) return;
1741 rad_listen_t *next = this->next;
1744 * Other code may have eaten the FD.
1746 if (this->fd >= 0) close(this->fd);
1748 if (master_listen[this->type].free) {
1749 master_listen[this->type].free(this);