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 RADCLIENT_LIST *clients;
76 * Find a per-socket client.
78 RADCLIENT *client_listener_find(const rad_listen_t *listener,
79 const fr_ipaddr_t *ipaddr)
81 #ifdef WITH_DYNAMIC_CLIENTS
84 listen_socket_t *sock;
86 RADCLIENT *client, *created;
88 RADCLIENT_LIST *clients;
90 rad_assert(listener != NULL);
91 rad_assert(ipaddr != NULL);
93 rad_assert((listener->type == RAD_LISTEN_AUTH) ||
94 (listener->type == RAD_LISTEN_ACCT) ||
95 (listener->type == RAD_LISTEN_VQP) ||
96 (listener->type == RAD_LISTEN_DHCP));
98 clients = ((listen_socket_t *)listener->data)->clients;
101 * This HAS to have been initialized previously.
103 rad_assert(clients != NULL);
105 #ifdef WITH_DYNAMIC_CLIENTS
106 client = client_find(clients, ipaddr);
107 if (!client) return NULL;
110 * No server defined, and it's not dynamic. Return it.
112 if (!client->client_server && !client->dynamic) return client;
117 * It's a dynamically generated client, check it.
119 if (client->dynamic) {
121 * Lives forever. Return it.
123 if (client->lifetime == 0) return client;
126 * Rate-limit the deletion of known clients.
127 * This makes them last a little longer, but
128 * prevents the server from melting down if (say)
129 * 10k clients all expire at once.
131 if (now == client->last_new_client) return client;
134 * It's not dead yet. Return it.
136 if ((client->created + client->lifetime) > now) return client;
139 * It's dead, Jim. Delete it.
141 client_delete(clients, client);
144 * Go find the enclosing network again.
146 client = client_find(clients, ipaddr);
151 if (!client) return NULL;
152 if (!client->client_server) return NULL;
155 * At this point, 'client' is the enclosing
156 * network that configures where dynamic clients
159 rad_assert(client->dynamic == 0);
162 * The IP is unknown, so we've found an enclosing
163 * network. Enable DoS protection. We only
164 * allow one new client per second. Known
165 * clients aren't subject to this restriction.
167 if (now == client->last_new_client) return NULL;
170 client->last_new_client = now;
172 request = request_alloc();
173 if (!request) return NULL;
175 request->listener = listener;
176 request->client = client;
177 request->packet = rad_alloc(0);
178 if (!request->packet) {
179 request_free(&request);
182 request->reply = rad_alloc(0);
183 if (!request->reply) {
184 request_free(&request);
187 request->packet->timestamp = request->timestamp;
189 request->priority = listener->type;
190 request->server = client->client_server;
191 request->root = &mainconfig;
194 * Run a fake request through the given virtual server.
195 * Look for FreeRADIUS-Client-IP-Address
196 * FreeRADIUS-Client-Secret
199 * and create the RADCLIENT structure from that.
202 sock = listener->data;
203 request->packet->sockfd = listener->fd;
204 request->packet->src_ipaddr = *ipaddr;
205 request->packet->src_port = 0; /* who cares... */
206 request->packet->dst_ipaddr = sock->ipaddr;
207 request->packet->dst_port = sock->port;
209 request->reply->sockfd = request->packet->sockfd;
210 request->reply->dst_ipaddr = request->packet->src_ipaddr;
211 request->reply->src_ipaddr = request->packet->dst_ipaddr;
212 request->reply->dst_port = request->packet->src_port;
213 request->reply->src_port = request->packet->dst_port;
214 request->reply->id = request->packet->id;
215 request->reply->code = 0; /* UNKNOWN code */
218 DEBUG("server %s {", request->server);
220 rcode = module_authorize(0, request);
222 DEBUG("} # server %s", request->server);
224 if (rcode != RLM_MODULE_OK) {
225 request_free(&request);
229 created = client_create(clients, request);
231 request_free(&request);
233 return created; /* may be NULL */
235 return client_find(clients, ipaddr);
239 static int listen_bind(rad_listen_t *this);
243 * Process and reply to a server-status request.
244 * Like rad_authenticate and rad_accounting this should
245 * live in it's own file but it's so small we don't bother.
247 static int rad_status_server(REQUEST *request)
249 int rcode = RLM_MODULE_OK;
252 switch (request->listener->type) {
253 case RAD_LISTEN_AUTH:
254 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
256 rcode = module_authorize(dval->value, request);
258 rcode = RLM_MODULE_OK;
263 case RLM_MODULE_UPDATED:
264 request->reply->code = PW_AUTHENTICATION_ACK;
267 case RLM_MODULE_FAIL:
268 case RLM_MODULE_HANDLED:
269 request->reply->code = 0; /* don't reply */
273 case RLM_MODULE_REJECT:
274 request->reply->code = PW_AUTHENTICATION_REJECT;
279 #ifdef WITH_ACCOUNTING
280 case RAD_LISTEN_ACCT:
281 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
283 rcode = module_accounting(dval->value, request);
285 rcode = RLM_MODULE_OK;
290 case RLM_MODULE_UPDATED:
291 request->reply->code = PW_ACCOUNTING_RESPONSE;
295 request->reply->code = 0; /* don't reply */
309 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
311 listen_socket_t *sock = this->data;
315 if ((sock->ipaddr.af == AF_INET) &&
316 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
319 ip_ntoh(&sock->ipaddr, ip_buf, sizeof(ip_buf));
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:
357 return snprintf(buffer, bufsize, "%s address %s port %d",
358 name, ip_buf, sock->port);
361 return snprintf(buffer, bufsize, "%s address %s port %d as server %s",
362 name, ip_buf, sock->port, this->server);
367 * Parse an authentication or accounting socket.
369 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
374 listen_socket_t *sock = this->data;
375 char *section_name = NULL;
376 CONF_SECTION *client_cs, *parentcs;
381 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
382 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
383 &ipaddr.ipaddr.ip4addr, NULL);
384 if (rcode < 0) return -1;
386 if (rcode == 0) { /* successfully parsed IPv4 */
389 } else { /* maybe IPv6? */
390 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
391 &ipaddr.ipaddr.ip6addr, NULL);
392 if (rcode < 0) return -1;
395 cf_log_err(cf_sectiontoitem(cs),
396 "No address specified in listen section");
399 ipaddr.af = AF_INET6;
402 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
404 if (rcode < 0) return -1;
406 if ((listen_port < 0) || (listen_port > 65500)) {
407 cf_log_err(cf_sectiontoitem(cs),
408 "Invalid value for \"port\"");
412 sock->ipaddr = ipaddr;
413 sock->port = listen_port;
416 * And bind it to the port.
418 if (listen_bind(this) < 0) {
420 cf_log_err(cf_sectiontoitem(cs),
421 "Error binding to port for %s port %d",
422 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
428 * If we can bind to interfaces, do so,
431 if (cf_pair_find(cs, "interface")) {
432 #ifndef SO_BINDTODEVICE
433 cf_log_err(cf_sectiontoitem(cs),
434 "System does not support binding to interfaces. Delete this line from the configuration file.");
438 CONF_PAIR *cp = cf_pair_find(cs, "interface");
441 rad_assert(cp != NULL);
442 value = cf_pair_value(cp);
444 cf_log_err(cf_sectiontoitem(cs),
445 "No interface name given");
449 strcpy(ifreq.ifr_name, value);
451 if (setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
452 (char *)&ifreq, sizeof(ifreq)) < 0) {
453 cf_log_err(cf_sectiontoitem(cs),
454 "Failed binding to interface %s: %s",
455 value, strerror(errno));
457 } /* else it worked. */
463 * Proxy sockets don't have clients.
465 if (this->type == RAD_LISTEN_PROXY) return 0;
469 * The more specific configurations are preferred to more
473 parentcs = cf_top_section(cs);
474 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
475 §ion_name, NULL);
476 if (rcode < 0) return -1; /* bad string */
479 * Explicit list given: use it.
481 client_cs = cf_section_sub_find_name2(parentcs,
485 client_cs = cf_section_find(section_name);
488 cf_log_err(cf_sectiontoitem(cs),
489 "Failed to find clients %s {...}",
495 } /* else there was no "clients = " entry. */
498 CONF_SECTION *server_cs;
500 server_cs = cf_section_sub_find_name2(parentcs,
504 * Found a "server foo" section. If there are clients
508 (cf_section_sub_find(server_cs, "client") != NULL)) {
509 client_cs = server_cs;
514 * Still nothing. Look for global clients.
516 if (!client_cs) client_cs = parentcs;
518 sock->clients = clients_parse_section(client_cs);
519 if (!sock->clients) {
520 cf_log_err(cf_sectiontoitem(cs),
521 "Failed to find any clients for this listen section");
529 * Send an authentication response packet
531 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
533 rad_assert(request->listener == listener);
534 rad_assert(listener->send == auth_socket_send);
536 return rad_send(request->reply, request->packet,
537 request->client->secret);
541 #ifdef WITH_ACCOUNTING
543 * Send an accounting response packet (or not)
545 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
547 rad_assert(request->listener == listener);
548 rad_assert(listener->send == acct_socket_send);
551 * Accounting reject's are silently dropped.
553 * We do it here to avoid polluting the rest of the
554 * code with this knowledge
556 if (request->reply->code == 0) return 0;
558 return rad_send(request->reply, request->packet,
559 request->client->secret);
565 * Send a packet to a home server.
567 * FIXME: have different code for proxy auth & acct!
569 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
571 listen_socket_t *sock = listener->data;
573 rad_assert(request->proxy_listener == listener);
574 rad_assert(listener->send == proxy_socket_send);
576 request->proxy->src_ipaddr = sock->ipaddr;
577 request->proxy->src_port = sock->port;
579 return rad_send(request->proxy, request->packet,
580 request->home_server->secret);
586 * Check if an incoming request is "ok"
588 * It takes packets, not requests. It sees if the packet looks
589 * OK. If so, it does a number of sanity checks on it.
591 static int auth_socket_recv(rad_listen_t *listener,
592 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
596 RADIUS_PACKET *packet;
597 RAD_REQUEST_FUNP fun = NULL;
600 fr_ipaddr_t src_ipaddr;
602 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
603 if (rcode < 0) return 0;
605 RAD_SNMP_TYPE_INC(listener, total_requests);
607 if (rcode < 20) { /* AUTH_HDR_LEN */
608 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
612 if ((client = client_listener_find(listener,
613 &src_ipaddr)) == NULL) {
614 rad_recv_discard(listener->fd);
615 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
617 if (debug_flag > 0) {
620 listener->print(listener, name, sizeof(name));
623 * This is debugging rather than logging, so that
624 * DoS attacks don't affect us.
626 DEBUG("Ignoring request to %s from unknown client %s port %d",
628 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
629 buffer, sizeof(buffer)), src_port);
636 * Some sanity checks, based on the packet code.
639 case PW_AUTHENTICATION_REQUEST:
640 RAD_SNMP_CLIENT_INC(listener, client, requests);
641 fun = rad_authenticate;
644 case PW_STATUS_SERVER:
645 if (!mainconfig.status_server) {
646 rad_recv_discard(listener->fd);
647 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
648 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
649 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
652 fun = rad_status_server;
656 rad_recv_discard(listener->fd);
657 RAD_SNMP_INC(rad_snmp.auth.total_unknown_types);
658 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
660 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
661 code, client->shortname, src_port);
664 } /* switch over packet types */
667 * Now that we've sanity checked everything, receive the
670 packet = rad_recv(listener->fd, client->message_authenticator);
672 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
673 DEBUG("%s", librad_errstr);
677 if (!received_request(listener, packet, prequest, client)) {
678 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
679 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
689 #ifdef WITH_ACCOUNTING
691 * Receive packets from an accounting socket
693 static int acct_socket_recv(rad_listen_t *listener,
694 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
698 RADIUS_PACKET *packet;
699 RAD_REQUEST_FUNP fun = NULL;
702 fr_ipaddr_t src_ipaddr;
704 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
705 if (rcode < 0) return 0;
707 RAD_SNMP_TYPE_INC(listener, total_requests);
709 if (rcode < 20) { /* AUTH_HDR_LEN */
710 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
714 if ((client = client_listener_find(listener,
715 &src_ipaddr)) == NULL) {
716 rad_recv_discard(listener->fd);
717 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
720 * This is debugging rather than logging, so that
721 * DoS attacks don't affect us.
723 if (debug_flag > 0) {
726 listener->print(listener, name, sizeof(name));
728 DEBUG("Ignoring request to %s from unknown client %s port %d",
730 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
731 buffer, sizeof(buffer)), src_port);
738 * Some sanity checks, based on the packet code.
741 case PW_ACCOUNTING_REQUEST:
742 RAD_SNMP_CLIENT_INC(listener, client, requests);
743 fun = rad_accounting;
746 case PW_STATUS_SERVER:
747 if (!mainconfig.status_server) {
748 rad_recv_discard(listener->fd);
749 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
750 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
752 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
755 fun = rad_status_server;
759 rad_recv_discard(listener->fd);
760 RAD_SNMP_TYPE_INC(listener, total_unknown_types);
761 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
763 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
764 code, client->shortname, src_port);
766 } /* switch over packet types */
769 * Now that we've sanity checked everything, receive the
772 packet = rad_recv(listener->fd, 0);
774 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
775 radlog(L_ERR, "%s", librad_errstr);
780 * There can be no duplicate accounting packets.
782 if (!received_request(listener, packet, prequest, client)) {
783 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
784 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
796 * Recieve packets from a proxy socket.
798 static int proxy_socket_recv(rad_listen_t *listener,
799 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
802 RADIUS_PACKET *packet;
803 RAD_REQUEST_FUNP fun = NULL;
806 packet = rad_recv(listener->fd, 0);
808 radlog(L_ERR, "%s", librad_errstr);
813 * FIXME: Client MIB updates?
815 switch(packet->code) {
816 case PW_AUTHENTICATION_ACK:
817 case PW_ACCESS_CHALLENGE:
818 case PW_AUTHENTICATION_REJECT:
819 fun = rad_authenticate;
822 #ifdef WITH_ACCOUNTING
823 case PW_ACCOUNTING_RESPONSE:
824 fun = rad_accounting;
830 * FIXME: Update MIB for packet types?
832 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
833 "from home server %s port %d - ID %d : IGNORED",
835 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
836 packet->src_port, packet->id);
841 request = received_proxy_response(packet);
846 rad_assert(fun != NULL);
855 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
857 if (!request->reply->code) return 0;
859 rad_encode(request->reply, request->packet,
860 request->client->secret);
861 rad_sign(request->reply, request->packet,
862 request->client->secret);
868 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
870 if (rad_verify(request->packet, NULL,
871 request->client->secret) < 0) {
875 return rad_decode(request->packet, NULL,
876 request->client->secret);
880 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
882 rad_encode(request->proxy, NULL, request->home_server->secret);
883 rad_sign(request->proxy, NULL, request->home_server->secret);
889 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
891 if (rad_verify(request->proxy_reply, request->proxy,
892 request->home_server->secret) < 0) {
896 return rad_decode(request->proxy_reply, request->proxy,
897 request->home_server->secret);
902 static int radius_snmp_recv(rad_listen_t *listener,
903 UNUSED RAD_REQUEST_FUNP *pfun,
904 UNUSED REQUEST **prequest)
906 if ((rad_snmp.smux_fd >= 0) &&
907 (rad_snmp.smux_event == SMUX_READ)) {
912 * If we've got to re-connect, then do so now,
913 * before calling select again.
915 if (rad_snmp.smux_event == SMUX_CONNECT) {
920 * Reset this every time, as the smux connect may have
921 * opened a new socket.
923 listener->fd = rad_snmp.smux_fd;
929 static int radius_snmp_print(UNUSED rad_listen_t *this, char *buffer, size_t bufsize)
931 return snprintf(buffer, bufsize, "SMUX with OID .1.3.6.1.4.1.11344.1.1.1");
938 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
939 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
943 { common_socket_parse, NULL,
944 proxy_socket_recv, proxy_socket_send,
945 socket_print, proxy_socket_encode, proxy_socket_decode },
947 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
951 { common_socket_parse, NULL,
952 auth_socket_recv, auth_socket_send,
953 socket_print, client_socket_encode, client_socket_decode },
955 #ifdef WITH_ACCOUNTING
957 { common_socket_parse, NULL,
958 acct_socket_recv, acct_socket_send,
959 socket_print, client_socket_encode, client_socket_decode},
961 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
966 { detail_parse, detail_free,
967 detail_recv, detail_send,
968 detail_print, detail_encode, detail_decode },
970 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
974 /* vlan query protocol */
975 { common_socket_parse, NULL,
976 vqp_socket_recv, vqp_socket_send,
977 socket_print, vqp_socket_encode, vqp_socket_decode },
979 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
983 /* dhcp query protocol */
984 { dhcp_socket_parse, NULL,
985 dhcp_socket_recv, dhcp_socket_send,
986 socket_print, dhcp_socket_encode, dhcp_socket_decode },
988 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
991 { NULL, NULL, NULL, NULL, NULL, NULL, NULL} /* RAD_LISTEN_SNMP */
997 * Binds a listener to a socket.
999 static int listen_bind(rad_listen_t *this)
1001 listen_socket_t *sock = this->data;
1004 * If the port is zero, then it means the appropriate
1005 * thing from /etc/services.
1007 if (sock->port == 0) {
1008 struct servent *svp;
1010 switch (this->type) {
1011 case RAD_LISTEN_AUTH:
1012 svp = getservbyname ("radius", "udp");
1014 sock->port = ntohs(svp->s_port);
1016 sock->port = PW_AUTH_UDP_PORT;
1020 #ifdef WITH_ACCOUNTING
1021 case RAD_LISTEN_ACCT:
1022 svp = getservbyname ("radacct", "udp");
1024 sock->port = ntohs(svp->s_port);
1026 sock->port = PW_ACCT_UDP_PORT;
1032 case RAD_LISTEN_PROXY:
1038 case RAD_LISTEN_VQP:
1044 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1049 this->fd = fr_socket(&sock->ipaddr, sock->port);
1051 radlog(L_ERR, "ERROR: Failed to open socket: %s",
1057 * FreeBSD jail issues. We bind to 0.0.0.0, but the
1058 * kernel instead binds us to a 1.2.3.4. If this
1059 * happens, notice, and remember our real IP.
1062 struct sockaddr_storage src;
1063 socklen_t sizeof_src = sizeof(src);
1065 memset(&src, 0, sizeof_src);
1066 if (getsockname(this->fd, (struct sockaddr *) &src,
1068 radlog(L_ERR, "Failed getting socket name: %s",
1073 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
1074 &sock->ipaddr, &sock->port)) {
1075 radlog(L_ERR, "Socket has unsupported address family");
1084 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
1085 radlog(L_ERR, "Failure getting socket flags: %s)\n",
1090 flags |= O_NONBLOCK;
1091 if( fcntl(this->fd, F_SETFL, flags) < 0) {
1092 radlog(L_ERR, "Failure setting socket flags: %s)\n",
1104 * Allocate & initialize a new listener.
1106 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
1110 this = rad_malloc(sizeof(*this));
1111 memset(this, 0, sizeof(*this));
1114 this->recv = master_listen[this->type].recv;
1115 this->send = master_listen[this->type].send;
1116 this->print = master_listen[this->type].print;
1117 this->encode = master_listen[this->type].encode;
1118 this->decode = master_listen[this->type].decode;
1121 case RAD_LISTEN_AUTH:
1122 case RAD_LISTEN_ACCT:
1123 case RAD_LISTEN_PROXY:
1124 case RAD_LISTEN_VQP:
1125 case RAD_LISTEN_DHCP:
1126 this->data = rad_malloc(sizeof(listen_socket_t));
1127 memset(this->data, 0, sizeof(listen_socket_t));
1131 case RAD_LISTEN_DETAIL:
1137 rad_assert("Unsupported option!" == NULL);
1147 * Externally visible function for creating a new proxy LISTENER.
1149 * For now, don't take ipaddr or port.
1151 * Not thread-safe, but all calls to it are protected by the
1152 * proxy mutex in request_list.c
1154 rad_listen_t *proxy_new_listener()
1156 int last_proxy_port, port;
1157 rad_listen_t *this, *tmp, **last;
1158 listen_socket_t *sock, *old;
1160 this = listen_alloc(RAD_LISTEN_PROXY);
1163 * Find an existing proxy socket to copy.
1165 * FIXME: Make it per-realm, or per-home server!
1167 last_proxy_port = 0;
1169 last = &mainconfig.listen;
1170 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1171 if (tmp->type == RAD_LISTEN_PROXY) {
1173 if (sock->port > last_proxy_port) {
1174 last_proxy_port = sock->port + 1;
1176 if (!old) old = sock;
1179 last = &(tmp->next);
1184 return NULL; /* This is a serious error. */
1188 * FIXME: find a new IP address to listen on?
1190 * This could likely be done in the "home server"
1191 * configuration, to have per-home-server source IP's.
1194 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1197 * Keep going until we find an unused port.
1199 for (port = last_proxy_port; port < 64000; port++) {
1201 if (listen_bind(this) == 0) {
1203 * Add the new listener to the list of
1216 static const FR_NAME_NUMBER listen_compare[] = {
1217 { "auth", RAD_LISTEN_AUTH },
1218 #ifdef WITH_ACCOUNTING
1219 { "acct", RAD_LISTEN_ACCT },
1222 { "detail", RAD_LISTEN_DETAIL },
1225 { "proxy", RAD_LISTEN_PROXY },
1228 { "vmps", RAD_LISTEN_VQP },
1231 { "dhcp", RAD_LISTEN_DHCP },
1233 { "dhcp", RAD_LISTEN_NONE },
1239 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1247 cf_log_info(cs, "listen {");
1249 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1251 if (rcode < 0) return NULL;
1254 cf_log_err(cf_sectiontoitem(cs),
1255 "No type specified in listen section");
1259 type = fr_str2int(listen_compare, listen_type,
1261 if (type == RAD_LISTEN_NONE) {
1262 cf_log_err(cf_sectiontoitem(cs),
1263 "Invalid type \"%s\" in listen section.",
1271 * Allow listen sections in the default config to
1272 * refer to a server.
1275 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1277 if (rcode == 1) { /* compatiblity with 2.0-pre */
1278 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1281 if (rcode < 0) return NULL;
1285 * Set up cross-type data.
1287 this = listen_alloc(type);
1288 this->server = server;
1292 * Call per-type parser.
1294 if (master_listen[type].parse(cs, this) < 0) {
1299 cf_log_info(cs, "}");
1305 * Generate a list of listeners. Takes an input list of
1306 * listeners, too, so we don't close sockets with waiting packets.
1308 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1310 int override = FALSE;
1313 rad_listen_t **last;
1315 fr_ipaddr_t server_ipaddr;
1318 int defined_proxy = 0;
1322 * We shouldn't be called with a pre-existing list.
1324 rad_assert(head && (*head == NULL));
1327 server_ipaddr.af = AF_UNSPEC;
1330 * If the port is specified on the command-line,
1331 * it over-rides the configuration file.
1333 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1335 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1338 * If the IP address was configured on the command-line,
1339 * use that as the "bind_address"
1341 if (mainconfig.myip.af != AF_UNSPEC) {
1342 memcpy(&server_ipaddr, &mainconfig.myip,
1343 sizeof(server_ipaddr));
1349 * Else look for bind_address and/or listen sections.
1351 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1352 rcode = cf_item_parse(config, "bind_address",
1354 &server_ipaddr.ipaddr.ip4addr, NULL);
1355 if (rcode < 0) return -1; /* error parsing it */
1357 if (rcode == 0) { /* successfully parsed IPv4 */
1358 listen_socket_t *sock;
1359 server_ipaddr.af = AF_INET;
1361 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'.");
1365 if (strcmp(progname, "vmpsd") == 0) {
1366 this = listen_alloc(RAD_LISTEN_VQP);
1367 if (!auth_port) auth_port = 1589;
1370 this = listen_alloc(RAD_LISTEN_AUTH);
1374 sock->ipaddr = server_ipaddr;
1375 sock->port = auth_port;
1377 sock->clients = clients_parse_section(config);
1378 if (!sock->clients) {
1379 cf_log_err(cf_sectiontoitem(config),
1380 "Failed to find any clients for this listen section");
1384 if (listen_bind(this) < 0) {
1387 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1390 auth_port = sock->port; /* may have been updated in listen_bind */
1392 cs = cf_section_sub_find_name2(config, "server",
1394 if (cs) this->server = mainconfig.name;
1398 last = &(this->next);
1404 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
1407 #ifdef WITH_ACCOUNTING
1409 * Open Accounting Socket.
1411 * If we haven't already gotten acct_port from
1412 * /etc/services, then make it auth_port + 1.
1414 this = listen_alloc(RAD_LISTEN_ACCT);
1418 * Create the accounting socket.
1420 * The accounting port is always the
1421 * authentication port + 1
1423 sock->ipaddr = server_ipaddr;
1424 sock->port = auth_port + 1;
1426 sock->clients = clients_parse_section(config);
1427 if (!sock->clients) {
1428 cf_log_err(cf_sectiontoitem(config),
1429 "Failed to find any clients for this listen section");
1433 if (listen_bind(this) < 0) {
1436 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1441 cs = cf_section_sub_find_name2(config, "server",
1443 if (cs) this->server = mainconfig.name;
1447 last = &(this->next);
1449 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1450 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1456 * They specified an IP on the command-line, ignore
1457 * all listen sections except the one in '-n'.
1459 if (mainconfig.myip.af != AF_UNSPEC) {
1460 CONF_SECTION *subcs;
1461 const char *name2 = cf_section_name2(cs);
1463 cs = cf_section_sub_find_name2(config, "server",
1465 if (!cs) goto do_proxy;
1468 * Should really abstract this code...
1470 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1472 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1473 this = listen_parse(subcs, name2);
1480 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1484 last = &(this->next);
1485 } /* loop over "listen" directives in server <foo> */
1491 * Walk through the "listen" sections, if they exist.
1493 for (cs = cf_subsection_find_next(config, NULL, "listen");
1495 cs = cf_subsection_find_next(config, cs, "listen")) {
1496 this = listen_parse(cs, NULL);
1503 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1507 last = &(this->next);
1511 * Check virtual servers for "listen" sections, too.
1513 * FIXME: Move to virtual server init?
1515 for (cs = cf_subsection_find_next(config, NULL, "server");
1517 cs = cf_subsection_find_next(config, cs, "server")) {
1518 CONF_SECTION *subcs;
1519 const char *name2 = cf_section_name2(cs);
1521 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1523 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1524 this = listen_parse(subcs, name2);
1531 if (this->type == RAD_LISTEN_PROXY) {
1532 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
1539 last = &(this->next);
1540 } /* loop over "listen" directives in virtual servers */
1541 } /* loop over virtual servers */
1544 * If we're proxying requests, open the proxy FD.
1545 * Otherwise, don't do anything.
1549 if (mainconfig.proxy_requests == TRUE) {
1551 listen_socket_t *sock = NULL;
1554 * No sockets to receive packets, therefore
1555 * proxying is pointless.
1557 if (!*head) return -1;
1559 if (defined_proxy) goto do_snmp;
1562 * Find the first authentication port,
1565 for (this = *head; this != NULL; this = this->next) {
1566 if (this->type == RAD_LISTEN_AUTH) {
1568 if (server_ipaddr.af == AF_UNSPEC) {
1569 server_ipaddr = sock->ipaddr;
1571 port = sock->port + 2; /* skip acct port */
1574 if (this->type == RAD_LISTEN_VQP) {
1576 if (server_ipaddr.af == AF_UNSPEC) {
1577 server_ipaddr = sock->ipaddr;
1579 port = sock->port + 1;
1584 if (port < 0) port = 1024 + (fr_rand() & 0x1ff);
1587 * Address is still unspecified, use IPv4.
1589 if (server_ipaddr.af == AF_UNSPEC) {
1590 server_ipaddr.af = AF_INET;
1591 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1594 this = listen_alloc(RAD_LISTEN_PROXY);
1598 * Create the first proxy socket.
1600 sock->ipaddr = server_ipaddr;
1603 * Try to find a proxy port (value doesn't matter)
1605 for (sock->port = port;
1608 if (listen_bind(this) == 0) {
1610 last = &(this->next); /* just in case */
1615 if (sock->port >= 64000) {
1618 radlog(L_ERR, "Failed to open socket for proxying");
1623 do_snmp: /* used only in proxy code. */
1627 if (radius_snmp_init(config)) {
1628 this = rad_malloc(sizeof(*this));
1629 memset(this, 0, sizeof(*this));
1631 this->type = RAD_LISTEN_SNMP;
1632 this->fd = rad_snmp.smux_fd;
1634 this->recv = radius_snmp_recv;
1635 this->print = radius_snmp_print;
1638 last = &(this->next);
1646 * Free a linked list of listeners;
1648 void listen_free(rad_listen_t **head)
1652 if (!head || !*head) return;
1656 rad_listen_t *next = this->next;
1659 * Other code may have eaten the FD.
1661 if (this->fd >= 0) close(this->fd);
1663 if (master_listen[this->type].free) {
1664 master_listen[this->type].free(this);