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 const RADCLIENT_LIST *clients;
83 rad_assert(listener != NULL);
84 rad_assert(ipaddr != NULL);
86 rad_assert((listener->type == RAD_LISTEN_AUTH) ||
87 (listener->type == RAD_LISTEN_ACCT) ||
88 (listener->type == RAD_LISTEN_VQP) ||
89 (listener->type == RAD_LISTEN_DHCP));
91 clients = ((listen_socket_t *)listener->data)->clients;
94 * This HAS to have been initialized previously.
96 rad_assert(clients != NULL);
98 return client_find(clients, ipaddr);
101 static int listen_bind(rad_listen_t *this);
105 * Process and reply to a server-status request.
106 * Like rad_authenticate and rad_accounting this should
107 * live in it's own file but it's so small we don't bother.
109 static int rad_status_server(REQUEST *request)
111 int rcode = RLM_MODULE_OK;
114 switch (request->listener->type) {
115 case RAD_LISTEN_AUTH:
116 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
118 rcode = module_authorize(dval->value, request);
120 rcode = RLM_MODULE_OK;
125 case RLM_MODULE_UPDATED:
126 request->reply->code = PW_AUTHENTICATION_ACK;
129 case RLM_MODULE_FAIL:
130 case RLM_MODULE_HANDLED:
131 request->reply->code = 0; /* don't reply */
135 case RLM_MODULE_REJECT:
136 request->reply->code = PW_AUTHENTICATION_REJECT;
141 #ifdef WITH_ACCOUNTING
142 case RAD_LISTEN_ACCT:
143 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
145 rcode = module_accounting(dval->value, request);
147 rcode = RLM_MODULE_OK;
152 case RLM_MODULE_UPDATED:
153 request->reply->code = PW_ACCOUNTING_RESPONSE;
157 request->reply->code = 0; /* don't reply */
171 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
173 listen_socket_t *sock = this->data;
177 if ((sock->ipaddr.af == AF_INET) &&
178 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
181 ip_ntoh(&sock->ipaddr, ip_buf, sizeof(ip_buf));
184 switch (this->type) {
185 case RAD_LISTEN_AUTH:
186 name = "authentication";
189 #ifdef WITH_ACCOUNTING
190 case RAD_LISTEN_ACCT:
196 case RAD_LISTEN_PROXY:
208 case RAD_LISTEN_DHCP:
219 return snprintf(buffer, bufsize, "%s address %s port %d",
220 name, ip_buf, sock->port);
223 return snprintf(buffer, bufsize, "%s address %s port %d as server %s",
224 name, ip_buf, sock->port, this->server);
229 * Parse an authentication or accounting socket.
231 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
236 listen_socket_t *sock = this->data;
237 char *section_name = NULL;
238 CONF_SECTION *client_cs, *parentcs;
243 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
244 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
245 &ipaddr.ipaddr.ip4addr, NULL);
246 if (rcode < 0) return -1;
248 if (rcode == 0) { /* successfully parsed IPv4 */
251 } else { /* maybe IPv6? */
252 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
253 &ipaddr.ipaddr.ip6addr, NULL);
254 if (rcode < 0) return -1;
257 cf_log_err(cf_sectiontoitem(cs),
258 "No address specified in listen section");
261 ipaddr.af = AF_INET6;
264 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
266 if (rcode < 0) return -1;
268 if ((listen_port < 0) || (listen_port > 65500)) {
269 cf_log_err(cf_sectiontoitem(cs),
270 "Invalid value for \"port\"");
274 sock->ipaddr = ipaddr;
275 sock->port = listen_port;
278 * And bind it to the port.
280 if (listen_bind(this) < 0) {
282 cf_log_err(cf_sectiontoitem(cs),
283 "Error binding to port for %s port %d",
284 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
290 * If we can bind to interfaces, do so,
293 if (cf_pair_find(cs, "interface")) {
294 #ifndef SO_BINDTODEVICE
295 cf_log_err(cf_sectiontoitem(cs),
296 "System does not support binding to interfaces. Delete this line from the configuration file.");
300 CONF_PAIR *cp = cf_pair_find(cs, "interface");
303 rad_assert(cp != NULL);
304 value = cf_pair_value(cp);
306 cf_log_err(cf_sectiontoitem(cs),
307 "No interface name given");
311 strcpy(ifreq.ifr_name, value);
313 if (setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
314 (char *)&ifreq, sizeof(ifreq)) < 0) {
315 cf_log_err(cf_sectiontoitem(cs),
316 "Failed binding to interface %s: %s",
317 value, strerror(errno));
319 } /* else it worked. */
325 * Proxy sockets don't have clients.
327 if (this->type == RAD_LISTEN_PROXY) return 0;
331 * The more specific configurations are preferred to more
335 parentcs = cf_top_section(cs);
336 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
337 §ion_name, NULL);
338 if (rcode < 0) return -1; /* bad string */
341 * Explicit list given: use it.
343 client_cs = cf_section_sub_find_name2(parentcs,
347 client_cs = cf_section_find(section_name);
350 cf_log_err(cf_sectiontoitem(cs),
351 "Failed to find clients %s {...}",
357 } /* else there was no "clients = " entry. */
360 CONF_SECTION *server_cs;
362 server_cs = cf_section_sub_find_name2(parentcs,
366 * Found a "server foo" section. If there are clients
370 (cf_section_sub_find(server_cs, "client") != NULL)) {
371 client_cs = server_cs;
376 * Still nothing. Look for global clients.
378 if (!client_cs) client_cs = parentcs;
380 sock->clients = clients_parse_section(client_cs);
381 if (!sock->clients) {
382 cf_log_err(cf_sectiontoitem(cs),
383 "Failed to find any clients for this listen section");
391 * Send an authentication response packet
393 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
395 rad_assert(request->listener == listener);
396 rad_assert(listener->send == auth_socket_send);
398 return rad_send(request->reply, request->packet,
399 request->client->secret);
403 #ifdef WITH_ACCOUNTING
405 * Send an accounting response packet (or not)
407 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
409 rad_assert(request->listener == listener);
410 rad_assert(listener->send == acct_socket_send);
413 * Accounting reject's are silently dropped.
415 * We do it here to avoid polluting the rest of the
416 * code with this knowledge
418 if (request->reply->code == 0) return 0;
420 return rad_send(request->reply, request->packet,
421 request->client->secret);
427 * Send a packet to a home server.
429 * FIXME: have different code for proxy auth & acct!
431 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
433 listen_socket_t *sock = listener->data;
435 rad_assert(request->proxy_listener == listener);
436 rad_assert(listener->send == proxy_socket_send);
438 request->proxy->src_ipaddr = sock->ipaddr;
439 request->proxy->src_port = sock->port;
441 return rad_send(request->proxy, request->packet,
442 request->home_server->secret);
448 * Check if an incoming request is "ok"
450 * It takes packets, not requests. It sees if the packet looks
451 * OK. If so, it does a number of sanity checks on it.
453 static int auth_socket_recv(rad_listen_t *listener,
454 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
458 RADIUS_PACKET *packet;
459 RAD_REQUEST_FUNP fun = NULL;
462 fr_ipaddr_t src_ipaddr;
464 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
465 if (rcode < 0) return 0;
467 RAD_SNMP_TYPE_INC(listener, total_requests);
469 if (rcode < 20) { /* AUTH_HDR_LEN */
470 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
474 if ((client = client_listener_find(listener,
475 &src_ipaddr)) == NULL) {
476 rad_recv_discard(listener->fd);
477 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
479 if (debug_flag > 0) {
482 listener->print(listener, name, sizeof(name));
485 * This is debugging rather than logging, so that
486 * DoS attacks don't affect us.
488 DEBUG("Ignoring request to %s from unknown client %s port %d",
490 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
491 buffer, sizeof(buffer)), src_port);
498 * Some sanity checks, based on the packet code.
501 case PW_AUTHENTICATION_REQUEST:
502 RAD_SNMP_CLIENT_INC(listener, client, requests);
503 fun = rad_authenticate;
506 case PW_STATUS_SERVER:
507 if (!mainconfig.status_server) {
508 rad_recv_discard(listener->fd);
509 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
510 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
511 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
514 fun = rad_status_server;
518 rad_recv_discard(listener->fd);
519 RAD_SNMP_INC(rad_snmp.auth.total_unknown_types);
520 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
522 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
523 code, client->shortname, src_port);
526 } /* switch over packet types */
529 * Now that we've sanity checked everything, receive the
532 packet = rad_recv(listener->fd, client->message_authenticator);
534 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
535 DEBUG("%s", librad_errstr);
539 if (!received_request(listener, packet, prequest, client)) {
540 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
541 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
551 #ifdef WITH_ACCOUNTING
553 * Receive packets from an accounting socket
555 static int acct_socket_recv(rad_listen_t *listener,
556 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
560 RADIUS_PACKET *packet;
561 RAD_REQUEST_FUNP fun = NULL;
564 fr_ipaddr_t src_ipaddr;
566 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
567 if (rcode < 0) return 0;
569 RAD_SNMP_TYPE_INC(listener, total_requests);
571 if (rcode < 20) { /* AUTH_HDR_LEN */
572 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
576 if ((client = client_listener_find(listener,
577 &src_ipaddr)) == NULL) {
578 rad_recv_discard(listener->fd);
579 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
582 * This is debugging rather than logging, so that
583 * DoS attacks don't affect us.
585 if (debug_flag > 0) {
588 listener->print(listener, name, sizeof(name));
590 DEBUG("Ignoring request to %s from unknown client %s port %d",
592 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
593 buffer, sizeof(buffer)), src_port);
600 * Some sanity checks, based on the packet code.
603 case PW_ACCOUNTING_REQUEST:
604 RAD_SNMP_CLIENT_INC(listener, client, requests);
605 fun = rad_accounting;
608 case PW_STATUS_SERVER:
609 if (!mainconfig.status_server) {
610 rad_recv_discard(listener->fd);
611 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
612 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
614 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
617 fun = rad_status_server;
621 rad_recv_discard(listener->fd);
622 RAD_SNMP_TYPE_INC(listener, total_unknown_types);
623 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
625 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
626 code, client->shortname, src_port);
628 } /* switch over packet types */
631 * Now that we've sanity checked everything, receive the
634 packet = rad_recv(listener->fd, 0);
636 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
637 radlog(L_ERR, "%s", librad_errstr);
642 * There can be no duplicate accounting packets.
644 if (!received_request(listener, packet, prequest, client)) {
645 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
646 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
658 * Recieve packets from a proxy socket.
660 static int proxy_socket_recv(rad_listen_t *listener,
661 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
664 RADIUS_PACKET *packet;
665 RAD_REQUEST_FUNP fun = NULL;
668 packet = rad_recv(listener->fd, 0);
670 radlog(L_ERR, "%s", librad_errstr);
675 * FIXME: Client MIB updates?
677 switch(packet->code) {
678 case PW_AUTHENTICATION_ACK:
679 case PW_ACCESS_CHALLENGE:
680 case PW_AUTHENTICATION_REJECT:
681 fun = rad_authenticate;
684 #ifdef WITH_ACCOUNTING
685 case PW_ACCOUNTING_RESPONSE:
686 fun = rad_accounting;
692 * FIXME: Update MIB for packet types?
694 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
695 "from home server %s port %d - ID %d : IGNORED",
697 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
698 packet->src_port, packet->id);
703 request = received_proxy_response(packet);
708 rad_assert(fun != NULL);
717 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
719 if (!request->reply->code) return 0;
721 rad_encode(request->reply, request->packet,
722 request->client->secret);
723 rad_sign(request->reply, request->packet,
724 request->client->secret);
730 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
732 if (rad_verify(request->packet, NULL,
733 request->client->secret) < 0) {
737 return rad_decode(request->packet, NULL,
738 request->client->secret);
742 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
744 rad_encode(request->proxy, NULL, request->home_server->secret);
745 rad_sign(request->proxy, NULL, request->home_server->secret);
751 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
753 if (rad_verify(request->proxy_reply, request->proxy,
754 request->home_server->secret) < 0) {
758 return rad_decode(request->proxy_reply, request->proxy,
759 request->home_server->secret);
764 static int radius_snmp_recv(rad_listen_t *listener,
765 UNUSED RAD_REQUEST_FUNP *pfun,
766 UNUSED REQUEST **prequest)
768 if ((rad_snmp.smux_fd >= 0) &&
769 (rad_snmp.smux_event == SMUX_READ)) {
774 * If we've got to re-connect, then do so now,
775 * before calling select again.
777 if (rad_snmp.smux_event == SMUX_CONNECT) {
782 * Reset this every time, as the smux connect may have
783 * opened a new socket.
785 listener->fd = rad_snmp.smux_fd;
791 static int radius_snmp_print(UNUSED rad_listen_t *this, char *buffer, size_t bufsize)
793 return snprintf(buffer, bufsize, "SMUX with OID .1.3.6.1.4.1.11344.1.1.1");
800 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
801 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
805 { common_socket_parse, NULL,
806 proxy_socket_recv, proxy_socket_send,
807 socket_print, proxy_socket_encode, proxy_socket_decode },
809 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
813 { common_socket_parse, NULL,
814 auth_socket_recv, auth_socket_send,
815 socket_print, client_socket_encode, client_socket_decode },
817 #ifdef WITH_ACCOUNTING
819 { common_socket_parse, NULL,
820 acct_socket_recv, acct_socket_send,
821 socket_print, client_socket_encode, client_socket_decode},
823 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
828 { detail_parse, detail_free,
829 detail_recv, detail_send,
830 detail_print, detail_encode, detail_decode },
832 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
836 /* vlan query protocol */
837 { common_socket_parse, NULL,
838 vqp_socket_recv, vqp_socket_send,
839 socket_print, vqp_socket_encode, vqp_socket_decode },
841 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
845 /* dhcp query protocol */
846 { dhcp_socket_parse, NULL,
847 dhcp_socket_recv, dhcp_socket_send,
848 socket_print, dhcp_socket_encode, dhcp_socket_decode },
850 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
853 { NULL, NULL, NULL, NULL, NULL, NULL, NULL} /* RAD_LISTEN_SNMP */
859 * Binds a listener to a socket.
861 static int listen_bind(rad_listen_t *this)
863 listen_socket_t *sock = this->data;
866 * If the port is zero, then it means the appropriate
867 * thing from /etc/services.
869 if (sock->port == 0) {
872 switch (this->type) {
873 case RAD_LISTEN_AUTH:
874 svp = getservbyname ("radius", "udp");
876 sock->port = ntohs(svp->s_port);
878 sock->port = PW_AUTH_UDP_PORT;
882 #ifdef WITH_ACCOUNTING
883 case RAD_LISTEN_ACCT:
884 svp = getservbyname ("radacct", "udp");
886 sock->port = ntohs(svp->s_port);
888 sock->port = PW_ACCT_UDP_PORT;
894 case RAD_LISTEN_PROXY:
906 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
911 this->fd = fr_socket(&sock->ipaddr, sock->port);
913 radlog(L_ERR, "ERROR: Failed to open socket: %s",
919 * FreeBSD jail issues. We bind to 0.0.0.0, but the
920 * kernel instead binds us to a 1.2.3.4. If this
921 * happens, notice, and remember our real IP.
924 struct sockaddr_storage src;
925 socklen_t sizeof_src = sizeof(src);
927 memset(&src, 0, sizeof_src);
928 if (getsockname(this->fd, (struct sockaddr *) &src,
930 radlog(L_ERR, "Failed getting socket name: %s",
935 if (src.ss_family == AF_INET) {
936 struct sockaddr_in s4;
938 memcpy(&s4, &src, sizeof(s4));
939 sock->ipaddr.ipaddr.ip4addr = s4.sin_addr;
940 sock->port = ntohs(s4.sin_port);
942 #ifdef HAVE_STRUCT_SOCKADDR_IN6
943 } else if (src.ss_family == AF_INET6) {
944 struct sockaddr_in6 s6;
946 memcpy(&s6, &src, sizeof(s6));
947 sock->ipaddr.ipaddr.ip6addr = s6.sin6_addr;
948 sock->port = ntohs(s6.sin6_port);
951 radlog(L_ERR, "Socket has unsupported address family");
960 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
961 radlog(L_ERR, "Failure getting socket flags: %s)\n",
967 if( fcntl(this->fd, F_SETFL, flags) < 0) {
968 radlog(L_ERR, "Failure setting socket flags: %s)\n",
980 * Allocate & initialize a new listener.
982 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
986 this = rad_malloc(sizeof(*this));
987 memset(this, 0, sizeof(*this));
990 this->recv = master_listen[this->type].recv;
991 this->send = master_listen[this->type].send;
992 this->print = master_listen[this->type].print;
993 this->encode = master_listen[this->type].encode;
994 this->decode = master_listen[this->type].decode;
997 case RAD_LISTEN_AUTH:
998 case RAD_LISTEN_ACCT:
999 case RAD_LISTEN_PROXY:
1000 case RAD_LISTEN_VQP:
1001 case RAD_LISTEN_DHCP:
1002 this->data = rad_malloc(sizeof(listen_socket_t));
1003 memset(this->data, 0, sizeof(listen_socket_t));
1007 case RAD_LISTEN_DETAIL:
1013 rad_assert("Unsupported option!" == NULL);
1023 * Externally visible function for creating a new proxy LISTENER.
1025 * For now, don't take ipaddr or port.
1027 * Not thread-safe, but all calls to it are protected by the
1028 * proxy mutex in request_list.c
1030 rad_listen_t *proxy_new_listener()
1032 int last_proxy_port, port;
1033 rad_listen_t *this, *tmp, **last;
1034 listen_socket_t *sock, *old;
1036 this = listen_alloc(RAD_LISTEN_PROXY);
1039 * Find an existing proxy socket to copy.
1041 * FIXME: Make it per-realm, or per-home server!
1043 last_proxy_port = 0;
1045 last = &mainconfig.listen;
1046 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1047 if (tmp->type == RAD_LISTEN_PROXY) {
1049 if (sock->port > last_proxy_port) {
1050 last_proxy_port = sock->port + 1;
1052 if (!old) old = sock;
1055 last = &(tmp->next);
1060 return NULL; /* This is a serious error. */
1064 * FIXME: find a new IP address to listen on?
1066 * This could likely be done in the "home server"
1067 * configuration, to have per-home-server source IP's.
1070 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1073 * Keep going until we find an unused port.
1075 for (port = last_proxy_port; port < 64000; port++) {
1077 if (listen_bind(this) == 0) {
1079 * Add the new listener to the list of
1092 static const FR_NAME_NUMBER listen_compare[] = {
1093 { "auth", RAD_LISTEN_AUTH },
1094 #ifdef WITH_ACCOUNTING
1095 { "acct", RAD_LISTEN_ACCT },
1098 { "detail", RAD_LISTEN_DETAIL },
1101 { "proxy", RAD_LISTEN_PROXY },
1104 { "vmps", RAD_LISTEN_VQP },
1107 { "dhcp", RAD_LISTEN_DHCP },
1109 { "dhcp", RAD_LISTEN_NONE },
1115 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1123 cf_log_info(cs, "listen {");
1125 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1127 if (rcode < 0) return NULL;
1130 cf_log_err(cf_sectiontoitem(cs),
1131 "No type specified in listen section");
1135 type = fr_str2int(listen_compare, listen_type,
1137 if (type == RAD_LISTEN_NONE) {
1138 cf_log_err(cf_sectiontoitem(cs),
1139 "Invalid type \"%s\" in listen section.",
1147 * Allow listen sections in the default config to
1148 * refer to a server.
1151 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1153 if (rcode == 1) { /* compatiblity with 2.0-pre */
1154 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1157 if (rcode < 0) return NULL;
1161 * Set up cross-type data.
1163 this = listen_alloc(type);
1164 this->server = server;
1168 * Call per-type parser.
1170 if (master_listen[type].parse(cs, this) < 0) {
1175 cf_log_info(cs, "}");
1181 * Generate a list of listeners. Takes an input list of
1182 * listeners, too, so we don't close sockets with waiting packets.
1184 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1186 int override = FALSE;
1189 rad_listen_t **last;
1191 fr_ipaddr_t server_ipaddr;
1194 int defined_proxy = 0;
1198 * We shouldn't be called with a pre-existing list.
1200 rad_assert(head && (*head == NULL));
1203 server_ipaddr.af = AF_UNSPEC;
1206 * If the port is specified on the command-line,
1207 * it over-rides the configuration file.
1209 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1211 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1214 * If the IP address was configured on the command-line,
1215 * use that as the "bind_address"
1217 if (mainconfig.myip.af != AF_UNSPEC) {
1218 memcpy(&server_ipaddr, &mainconfig.myip,
1219 sizeof(server_ipaddr));
1225 * Else look for bind_address and/or listen sections.
1227 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1228 rcode = cf_item_parse(config, "bind_address",
1230 &server_ipaddr.ipaddr.ip4addr, NULL);
1231 if (rcode < 0) return -1; /* error parsing it */
1233 if (rcode == 0) { /* successfully parsed IPv4 */
1234 listen_socket_t *sock;
1235 server_ipaddr.af = AF_INET;
1237 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'.");
1241 if (strcmp(progname, "vmpsd") == 0) {
1242 this = listen_alloc(RAD_LISTEN_VQP);
1243 if (!auth_port) auth_port = 1589;
1246 this = listen_alloc(RAD_LISTEN_AUTH);
1250 sock->ipaddr = server_ipaddr;
1251 sock->port = auth_port;
1253 sock->clients = clients_parse_section(config);
1254 if (!sock->clients) {
1255 cf_log_err(cf_sectiontoitem(config),
1256 "Failed to find any clients for this listen section");
1260 if (listen_bind(this) < 0) {
1263 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1266 auth_port = sock->port; /* may have been updated in listen_bind */
1268 cs = cf_section_sub_find_name2(config, "server",
1270 if (cs) this->server = mainconfig.name;
1274 last = &(this->next);
1280 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
1283 #ifdef WITH_ACCOUNTING
1285 * Open Accounting Socket.
1287 * If we haven't already gotten acct_port from
1288 * /etc/services, then make it auth_port + 1.
1290 this = listen_alloc(RAD_LISTEN_ACCT);
1294 * Create the accounting socket.
1296 * The accounting port is always the
1297 * authentication port + 1
1299 sock->ipaddr = server_ipaddr;
1300 sock->port = auth_port + 1;
1302 sock->clients = clients_parse_section(config);
1303 if (!sock->clients) {
1304 cf_log_err(cf_sectiontoitem(config),
1305 "Failed to find any clients for this listen section");
1309 if (listen_bind(this) < 0) {
1312 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1317 cs = cf_section_sub_find_name2(config, "server",
1319 if (cs) this->server = mainconfig.name;
1323 last = &(this->next);
1325 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1326 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1332 * They specified an IP on the command-line, ignore
1333 * all listen sections except the one in '-n'.
1335 if (mainconfig.myip.af != AF_UNSPEC) {
1336 CONF_SECTION *subcs;
1337 const char *name2 = cf_section_name2(cs);
1339 cs = cf_section_sub_find_name2(config, "server",
1341 if (!cs) goto do_proxy;
1344 * Should really abstract this code...
1346 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1348 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1349 this = listen_parse(subcs, name2);
1356 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1360 last = &(this->next);
1361 } /* loop over "listen" directives in server <foo> */
1367 * Walk through the "listen" sections, if they exist.
1369 for (cs = cf_subsection_find_next(config, NULL, "listen");
1371 cs = cf_subsection_find_next(config, cs, "listen")) {
1372 this = listen_parse(cs, NULL);
1379 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1383 last = &(this->next);
1387 * Check virtual servers for "listen" sections, too.
1389 * FIXME: Move to virtual server init?
1391 for (cs = cf_subsection_find_next(config, NULL, "server");
1393 cs = cf_subsection_find_next(config, cs, "server")) {
1394 CONF_SECTION *subcs;
1395 const char *name2 = cf_section_name2(cs);
1397 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1399 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1400 this = listen_parse(subcs, name2);
1407 if (this->type == RAD_LISTEN_PROXY) {
1408 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
1415 last = &(this->next);
1416 } /* loop over "listen" directives in virtual servers */
1417 } /* loop over virtual servers */
1420 * If we're proxying requests, open the proxy FD.
1421 * Otherwise, don't do anything.
1425 if (mainconfig.proxy_requests == TRUE) {
1427 listen_socket_t *sock = NULL;
1430 * No sockets to receive packets, therefore
1431 * proxying is pointless.
1433 if (!*head) return -1;
1435 if (defined_proxy) goto do_snmp;
1438 * Find the first authentication port,
1441 for (this = *head; this != NULL; this = this->next) {
1442 if (this->type == RAD_LISTEN_AUTH) {
1444 if (server_ipaddr.af == AF_UNSPEC) {
1445 server_ipaddr = sock->ipaddr;
1447 port = sock->port + 2; /* skip acct port */
1450 if (this->type == RAD_LISTEN_VQP) {
1452 if (server_ipaddr.af == AF_UNSPEC) {
1453 server_ipaddr = sock->ipaddr;
1455 port = sock->port + 1;
1460 if (port < 0) port = 1024 + (fr_rand() & 0x1ff);
1463 * Address is still unspecified, use IPv4.
1465 if (server_ipaddr.af == AF_UNSPEC) {
1466 server_ipaddr.af = AF_INET;
1467 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1470 this = listen_alloc(RAD_LISTEN_PROXY);
1474 * Create the first proxy socket.
1476 sock->ipaddr = server_ipaddr;
1479 * Try to find a proxy port (value doesn't matter)
1481 for (sock->port = port;
1484 if (listen_bind(this) == 0) {
1486 last = &(this->next); /* just in case */
1491 if (sock->port >= 64000) {
1494 radlog(L_ERR, "Failed to open socket for proxying");
1499 do_snmp: /* used only in proxy code. */
1503 if (radius_snmp_init(config)) {
1504 this = rad_malloc(sizeof(*this));
1505 memset(this, 0, sizeof(*this));
1507 this->type = RAD_LISTEN_SNMP;
1508 this->fd = rad_snmp.smux_fd;
1510 this->recv = radius_snmp_recv;
1511 this->print = radius_snmp_print;
1514 last = &(this->next);
1522 * Free a linked list of listeners;
1524 void listen_free(rad_listen_t **head)
1528 if (!head || !*head) return;
1532 rad_listen_t *next = this->next;
1535 * Other code may have eaten the FD.
1537 if (this->fd >= 0) close(this->fd);
1539 if (master_listen[this->type].free) {
1540 master_listen[this->type].free(this);