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 case RAD_LISTEN_ACCT:
142 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
144 rcode = module_accounting(dval->value, request);
146 rcode = RLM_MODULE_OK;
151 case RLM_MODULE_UPDATED:
152 request->reply->code = PW_ACCOUNTING_RESPONSE;
156 request->reply->code = 0; /* don't reply */
169 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
171 listen_socket_t *sock = this->data;
175 if ((sock->ipaddr.af == AF_INET) &&
176 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
179 ip_ntoh(&sock->ipaddr, ip_buf, sizeof(ip_buf));
182 switch (this->type) {
183 case RAD_LISTEN_AUTH:
184 name = "authentication";
187 case RAD_LISTEN_ACCT:
191 case RAD_LISTEN_PROXY:
202 case RAD_LISTEN_DHCP:
213 return snprintf(buffer, bufsize, "%s address %s port %d",
214 name, ip_buf, sock->port);
217 return snprintf(buffer, bufsize, "%s address %s port %d as server %s",
218 name, ip_buf, sock->port, this->server);
223 * Parse an authentication or accounting socket.
225 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
230 listen_socket_t *sock = this->data;
231 char *section_name = NULL;
232 CONF_SECTION *client_cs, *parentcs;
237 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
238 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
239 &ipaddr.ipaddr.ip4addr, NULL);
240 if (rcode < 0) return -1;
242 if (rcode == 0) { /* successfully parsed IPv4 */
245 } else { /* maybe IPv6? */
246 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
247 &ipaddr.ipaddr.ip6addr, NULL);
248 if (rcode < 0) return -1;
251 cf_log_err(cf_sectiontoitem(cs),
252 "No address specified in listen section");
255 ipaddr.af = AF_INET6;
258 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
260 if (rcode < 0) return -1;
262 if ((listen_port < 0) || (listen_port > 65500)) {
263 cf_log_err(cf_sectiontoitem(cs),
264 "Invalid value for \"port\"");
268 sock->ipaddr = ipaddr;
269 sock->port = listen_port;
272 * And bind it to the port.
274 if (listen_bind(this) < 0) {
276 cf_log_err(cf_sectiontoitem(cs),
277 "Error binding to port for %s port %d",
278 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
284 * If we can bind to interfaces, do so,
287 if (cf_pair_find(cs, "interface")) {
288 #ifndef SO_BINDTODEVICE
289 cf_log_err(cf_sectiontoitem(cs),
290 "System does not support binding to interfaces. Delete this line from the configuration file.");
294 CONF_PAIR *cp = cf_pair_find(cs, "interface");
297 rad_assert(cp != NULL);
298 value = cf_pair_value(cp);
300 cf_log_err(cf_sectiontoitem(cs),
301 "No interface name given");
305 strcpy(ifreq.ifr_name, value);
307 if (setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
308 (char *)&ifreq, sizeof(ifreq)) < 0) {
309 cf_log_err(cf_sectiontoitem(cs),
310 "Failed binding to interface %s: %s",
311 value, strerror(errno));
313 } /* else it worked. */
318 * Proxy sockets don't have clients.
320 if (this->type == RAD_LISTEN_PROXY) return 0;
323 * The more specific configurations are preferred to more
327 parentcs = cf_top_section(cs);
328 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
329 §ion_name, NULL);
330 if (rcode < 0) return -1; /* bad string */
333 * Explicit list given: use it.
335 client_cs = cf_section_sub_find_name2(parentcs,
339 client_cs = cf_section_find(section_name);
342 cf_log_err(cf_sectiontoitem(cs),
343 "Failed to find clients %s {...}",
349 } /* else there was no "clients = " entry. */
352 CONF_SECTION *server_cs;
354 server_cs = cf_section_sub_find_name2(parentcs,
358 * Found a "server foo" section. If there are clients
362 (cf_section_sub_find(server_cs, "client") != NULL)) {
363 client_cs = server_cs;
368 * Still nothing. Look for global clients.
370 if (!client_cs) client_cs = parentcs;
372 sock->clients = clients_parse_section(client_cs);
373 if (!sock->clients) {
374 cf_log_err(cf_sectiontoitem(cs),
375 "Failed to find any clients for this listen section");
383 * Send an authentication response packet
385 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
387 rad_assert(request->listener == listener);
388 rad_assert(listener->send == auth_socket_send);
390 return rad_send(request->reply, request->packet,
391 request->client->secret);
396 * Send an accounting response packet (or not)
398 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
400 rad_assert(request->listener == listener);
401 rad_assert(listener->send == acct_socket_send);
404 * Accounting reject's are silently dropped.
406 * We do it here to avoid polluting the rest of the
407 * code with this knowledge
409 if (request->reply->code == 0) return 0;
411 return rad_send(request->reply, request->packet,
412 request->client->secret);
417 * Send a packet to a home server.
419 * FIXME: have different code for proxy auth & acct!
421 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
423 listen_socket_t *sock = listener->data;
425 rad_assert(request->proxy_listener == listener);
426 rad_assert(listener->send == proxy_socket_send);
428 request->proxy->src_ipaddr = sock->ipaddr;
429 request->proxy->src_port = sock->port;
431 return rad_send(request->proxy, request->packet,
432 request->home_server->secret);
437 * Check if an incoming request is "ok"
439 * It takes packets, not requests. It sees if the packet looks
440 * OK. If so, it does a number of sanity checks on it.
442 static int auth_socket_recv(rad_listen_t *listener,
443 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
447 RADIUS_PACKET *packet;
448 RAD_REQUEST_FUNP fun = NULL;
451 fr_ipaddr_t src_ipaddr;
453 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
454 if (rcode < 0) return 0;
456 RAD_SNMP_TYPE_INC(listener, total_requests);
458 if (rcode < 20) { /* AUTH_HDR_LEN */
459 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
463 if ((client = client_listener_find(listener,
464 &src_ipaddr)) == NULL) {
465 rad_recv_discard(listener->fd);
466 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
468 if (debug_flag > 0) {
471 listener->print(listener, name, sizeof(name));
474 * This is debugging rather than logging, so that
475 * DoS attacks don't affect us.
477 DEBUG("Ignoring request to %s from unknown client %s port %d",
479 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
480 buffer, sizeof(buffer)), src_port);
487 * Some sanity checks, based on the packet code.
490 case PW_AUTHENTICATION_REQUEST:
491 RAD_SNMP_CLIENT_INC(listener, client, requests);
492 fun = rad_authenticate;
495 case PW_STATUS_SERVER:
496 if (!mainconfig.status_server) {
497 rad_recv_discard(listener->fd);
498 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
499 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
500 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
503 fun = rad_status_server;
507 rad_recv_discard(listener->fd);
508 RAD_SNMP_INC(rad_snmp.auth.total_unknown_types);
509 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
511 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
512 code, client->shortname, src_port);
515 } /* switch over packet types */
518 * Now that we've sanity checked everything, receive the
521 packet = rad_recv(listener->fd, client->message_authenticator);
523 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
524 DEBUG("%s", librad_errstr);
528 if (!received_request(listener, packet, prequest, client)) {
529 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
530 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
541 * Receive packets from an accounting socket
543 static int acct_socket_recv(rad_listen_t *listener,
544 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
548 RADIUS_PACKET *packet;
549 RAD_REQUEST_FUNP fun = NULL;
552 fr_ipaddr_t src_ipaddr;
554 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
555 if (rcode < 0) return 0;
557 RAD_SNMP_TYPE_INC(listener, total_requests);
559 if (rcode < 20) { /* AUTH_HDR_LEN */
560 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
564 if ((client = client_listener_find(listener,
565 &src_ipaddr)) == NULL) {
566 rad_recv_discard(listener->fd);
567 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
570 * This is debugging rather than logging, so that
571 * DoS attacks don't affect us.
573 if (debug_flag > 0) {
576 listener->print(listener, name, sizeof(name));
578 DEBUG("Ignoring request to %s from unknown client %s port %d",
580 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
581 buffer, sizeof(buffer)), src_port);
588 * Some sanity checks, based on the packet code.
591 case PW_ACCOUNTING_REQUEST:
592 RAD_SNMP_CLIENT_INC(listener, client, requests);
593 fun = rad_accounting;
596 case PW_STATUS_SERVER:
597 if (!mainconfig.status_server) {
598 rad_recv_discard(listener->fd);
599 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
600 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
602 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
605 fun = rad_status_server;
609 rad_recv_discard(listener->fd);
610 RAD_SNMP_TYPE_INC(listener, total_unknown_types);
611 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
613 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
614 code, client->shortname, src_port);
616 } /* switch over packet types */
619 * Now that we've sanity checked everything, receive the
622 packet = rad_recv(listener->fd, 0);
624 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
625 radlog(L_ERR, "%s", librad_errstr);
630 * There can be no duplicate accounting packets.
632 if (!received_request(listener, packet, prequest, client)) {
633 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
634 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
645 * Recieve packets from a proxy socket.
647 static int proxy_socket_recv(rad_listen_t *listener,
648 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
651 RADIUS_PACKET *packet;
652 RAD_REQUEST_FUNP fun = NULL;
655 packet = rad_recv(listener->fd, 0);
657 radlog(L_ERR, "%s", librad_errstr);
662 * FIXME: Client MIB updates?
664 switch(packet->code) {
665 case PW_AUTHENTICATION_ACK:
666 case PW_ACCESS_CHALLENGE:
667 case PW_AUTHENTICATION_REJECT:
668 fun = rad_authenticate;
671 case PW_ACCOUNTING_RESPONSE:
672 fun = rad_accounting;
677 * FIXME: Update MIB for packet types?
679 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
680 "from home server %s port %d - ID %d : IGNORED",
682 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
683 packet->src_port, packet->id);
688 request = received_proxy_response(packet);
693 rad_assert(fun != NULL);
701 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
703 if (!request->reply->code) return 0;
705 rad_encode(request->reply, request->packet,
706 request->client->secret);
707 rad_sign(request->reply, request->packet,
708 request->client->secret);
714 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
716 if (rad_verify(request->packet, NULL,
717 request->client->secret) < 0) {
721 return rad_decode(request->packet, NULL,
722 request->client->secret);
725 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
727 rad_encode(request->proxy, NULL, request->home_server->secret);
728 rad_sign(request->proxy, NULL, request->home_server->secret);
734 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
736 if (rad_verify(request->proxy_reply, request->proxy,
737 request->home_server->secret) < 0) {
741 return rad_decode(request->proxy_reply, request->proxy,
742 request->home_server->secret);
746 static int radius_snmp_recv(rad_listen_t *listener,
747 UNUSED RAD_REQUEST_FUNP *pfun,
748 UNUSED REQUEST **prequest)
750 if ((rad_snmp.smux_fd >= 0) &&
751 (rad_snmp.smux_event == SMUX_READ)) {
756 * If we've got to re-connect, then do so now,
757 * before calling select again.
759 if (rad_snmp.smux_event == SMUX_CONNECT) {
764 * Reset this every time, as the smux connect may have
765 * opened a new socket.
767 listener->fd = rad_snmp.smux_fd;
773 static int radius_snmp_print(UNUSED rad_listen_t *this, char *buffer, size_t bufsize)
775 return snprintf(buffer, bufsize, "SMUX with OID .1.3.6.1.4.1.11344.1.1.1");
782 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
783 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
786 { common_socket_parse, NULL,
787 proxy_socket_recv, proxy_socket_send,
788 socket_print, proxy_socket_encode, proxy_socket_decode },
791 { common_socket_parse, NULL,
792 auth_socket_recv, auth_socket_send,
793 socket_print, client_socket_encode, client_socket_decode },
796 { common_socket_parse, NULL,
797 acct_socket_recv, acct_socket_send,
798 socket_print, client_socket_encode, client_socket_decode},
801 { detail_parse, detail_free,
802 detail_recv, detail_send,
803 detail_print, detail_encode, detail_decode },
806 /* vlan query protocol */
807 { common_socket_parse, NULL,
808 vqp_socket_recv, vqp_socket_send,
809 socket_print, vqp_socket_encode, vqp_socket_decode },
811 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
815 /* dhcp query protocol */
816 { dhcp_socket_parse, NULL,
817 dhcp_socket_recv, dhcp_socket_send,
818 socket_print, dhcp_socket_encode, dhcp_socket_decode },
820 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
823 { NULL, NULL, NULL, NULL, NULL, NULL, NULL} /* RAD_LISTEN_SNMP */
829 * Binds a listener to a socket.
831 static int listen_bind(rad_listen_t *this)
833 listen_socket_t *sock = this->data;
836 * If the port is zero, then it means the appropriate
837 * thing from /etc/services.
839 if (sock->port == 0) {
842 switch (this->type) {
843 case RAD_LISTEN_AUTH:
844 svp = getservbyname ("radius", "udp");
846 sock->port = ntohs(svp->s_port);
848 sock->port = PW_AUTH_UDP_PORT;
852 case RAD_LISTEN_ACCT:
853 svp = getservbyname ("radacct", "udp");
855 sock->port = ntohs(svp->s_port);
857 sock->port = PW_ACCT_UDP_PORT;
861 case RAD_LISTEN_PROXY:
872 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
877 this->fd = fr_socket(&sock->ipaddr, sock->port);
879 radlog(L_ERR, "ERROR: Failed to open socket: %s",
885 * FreeBSD jail issues. We bind to 0.0.0.0, but the
886 * kernel instead binds us to a 1.2.3.4. If this
887 * happens, notice, and remember our real IP.
890 struct sockaddr_storage src;
891 socklen_t sizeof_src = sizeof(src);
893 memset(&src, 0, sizeof_src);
894 if (getsockname(this->fd, (struct sockaddr *) &src,
896 radlog(L_ERR, "Failed getting socket name: %s",
901 if (src.ss_family == AF_INET) {
902 struct sockaddr_in *s4;
904 s4 = (struct sockaddr_in *)&src;
905 sock->ipaddr.ipaddr.ip4addr = s4->sin_addr;
906 sock->port = ntohs(s4->sin_port);
908 #ifdef HAVE_STRUCT_SOCKADDR_IN6
909 } else if (src.ss_family == AF_INET6) {
910 struct sockaddr_in6 *s6;
912 s6 = (struct sockaddr_in6 *)&src;
913 sock->ipaddr.ipaddr.ip6addr = s6->sin6_addr;
914 sock->port = ntohs(s6->sin6_port);
917 radlog(L_ERR, "Socket has unsupported address family");
926 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
927 radlog(L_ERR, "Failure getting socket flags: %s)\n",
933 if( fcntl(this->fd, F_SETFL, flags) < 0) {
934 radlog(L_ERR, "Failure setting socket flags: %s)\n",
946 * Allocate & initialize a new listener.
948 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
952 this = rad_malloc(sizeof(*this));
953 memset(this, 0, sizeof(*this));
956 this->recv = master_listen[this->type].recv;
957 this->send = master_listen[this->type].send;
958 this->print = master_listen[this->type].print;
959 this->encode = master_listen[this->type].encode;
960 this->decode = master_listen[this->type].decode;
963 case RAD_LISTEN_AUTH:
964 case RAD_LISTEN_ACCT:
965 case RAD_LISTEN_PROXY:
967 case RAD_LISTEN_DHCP:
968 this->data = rad_malloc(sizeof(listen_socket_t));
969 memset(this->data, 0, sizeof(listen_socket_t));
972 case RAD_LISTEN_DETAIL:
977 rad_assert("Unsupported option!" == NULL);
986 * Externally visible function for creating a new proxy LISTENER.
988 * For now, don't take ipaddr or port.
990 * Not thread-safe, but all calls to it are protected by the
991 * proxy mutex in request_list.c
993 rad_listen_t *proxy_new_listener()
995 int last_proxy_port, port;
996 rad_listen_t *this, *tmp, **last;
997 listen_socket_t *sock, *old;
999 this = listen_alloc(RAD_LISTEN_PROXY);
1002 * Find an existing proxy socket to copy.
1004 * FIXME: Make it per-realm, or per-home server!
1006 last_proxy_port = 0;
1008 last = &mainconfig.listen;
1009 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1010 if (tmp->type == RAD_LISTEN_PROXY) {
1012 if (sock->port > last_proxy_port) {
1013 last_proxy_port = sock->port + 1;
1015 if (!old) old = sock;
1018 last = &(tmp->next);
1023 return NULL; /* This is a serious error. */
1027 * FIXME: find a new IP address to listen on?
1029 * This could likely be done in the "home server"
1030 * configuration, to have per-home-server source IP's.
1033 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1036 * Keep going until we find an unused port.
1038 for (port = last_proxy_port; port < 64000; port++) {
1040 if (listen_bind(this) == 0) {
1042 * Add the new listener to the list of
1055 static const FR_NAME_NUMBER listen_compare[] = {
1056 { "auth", RAD_LISTEN_AUTH },
1057 { "acct", RAD_LISTEN_ACCT },
1058 { "detail", RAD_LISTEN_DETAIL },
1059 { "proxy", RAD_LISTEN_PROXY },
1061 { "vmps", RAD_LISTEN_VQP },
1064 { "dhcp", RAD_LISTEN_DHCP },
1066 { "dhcp", RAD_LISTEN_NONE },
1072 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1080 cf_log_info(cs, "listen {");
1082 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1084 if (rcode < 0) return NULL;
1087 cf_log_err(cf_sectiontoitem(cs),
1088 "No type specified in listen section");
1092 type = fr_str2int(listen_compare, listen_type,
1094 if (type == RAD_LISTEN_NONE) {
1095 cf_log_err(cf_sectiontoitem(cs),
1096 "Invalid type \"%s\" in listen section.",
1104 * Allow listen sections in the default config to
1105 * refer to a server.
1108 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1110 if (rcode == 1) { /* compatiblity with 2.0-pre */
1111 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1114 if (rcode < 0) return NULL;
1118 * Set up cross-type data.
1120 this = listen_alloc(type);
1121 this->server = server;
1125 * Call per-type parser.
1127 if (master_listen[type].parse(cs, this) < 0) {
1132 cf_log_info(cs, "}");
1138 * Generate a list of listeners. Takes an input list of
1139 * listeners, too, so we don't close sockets with waiting packets.
1141 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1143 int override = FALSE;
1146 rad_listen_t **last;
1148 fr_ipaddr_t server_ipaddr;
1150 int defined_proxy = 0;
1153 * We shouldn't be called with a pre-existing list.
1155 rad_assert(head && (*head == NULL));
1158 server_ipaddr.af = AF_UNSPEC;
1161 * If the port is specified on the command-line,
1162 * it over-rides the configuration file.
1164 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1166 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1169 * If the IP address was configured on the command-line,
1170 * use that as the "bind_address"
1172 if (mainconfig.myip.af != AF_UNSPEC) {
1173 memcpy(&server_ipaddr, &mainconfig.myip,
1174 sizeof(server_ipaddr));
1180 * Else look for bind_address and/or listen sections.
1182 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1183 rcode = cf_item_parse(config, "bind_address",
1185 &server_ipaddr.ipaddr.ip4addr, NULL);
1186 if (rcode < 0) return -1; /* error parsing it */
1188 if (rcode == 0) { /* successfully parsed IPv4 */
1189 listen_socket_t *sock;
1190 server_ipaddr.af = AF_INET;
1192 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'.");
1196 if (strcmp(progname, "vmpsd") == 0) {
1197 this = listen_alloc(RAD_LISTEN_VQP);
1198 if (!auth_port) auth_port = 1589;
1201 this = listen_alloc(RAD_LISTEN_AUTH);
1205 sock->ipaddr = server_ipaddr;
1206 sock->port = auth_port;
1208 sock->clients = clients_parse_section(config);
1209 if (!sock->clients) {
1210 cf_log_err(cf_sectiontoitem(config),
1211 "Failed to find any clients for this listen section");
1215 if (listen_bind(this) < 0) {
1218 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1221 auth_port = sock->port; /* may have been updated in listen_bind */
1223 cs = cf_section_sub_find_name2(config, "server",
1225 if (cs) this->server = mainconfig.name;
1229 last = &(this->next);
1235 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
1239 * Open Accounting Socket.
1241 * If we haven't already gotten acct_port from
1242 * /etc/services, then make it auth_port + 1.
1244 this = listen_alloc(RAD_LISTEN_ACCT);
1248 * Create the accounting socket.
1250 * The accounting port is always the
1251 * authentication port + 1
1253 sock->ipaddr = server_ipaddr;
1254 sock->port = auth_port + 1;
1256 sock->clients = clients_parse_section(config);
1257 if (!sock->clients) {
1258 cf_log_err(cf_sectiontoitem(config),
1259 "Failed to find any clients for this listen section");
1263 if (listen_bind(this) < 0) {
1266 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1271 cs = cf_section_sub_find_name2(config, "server",
1273 if (cs) this->server = mainconfig.name;
1277 last = &(this->next);
1279 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1280 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1286 * They specified an IP on the command-line, ignore
1287 * all listen sections except the one in '-n'.
1289 if (mainconfig.myip.af != AF_UNSPEC) {
1290 CONF_SECTION *subcs;
1291 const char *name2 = cf_section_name2(cs);
1293 cs = cf_section_sub_find_name2(config, "server",
1295 if (!cs) goto do_proxy;
1298 * Should really abstract this code...
1300 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1302 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1303 this = listen_parse(subcs, name2);
1309 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1312 last = &(this->next);
1313 } /* loop over "listen" directives in server <foo> */
1319 * Walk through the "listen" sections, if they exist.
1321 for (cs = cf_subsection_find_next(config, NULL, "listen");
1323 cs = cf_subsection_find_next(config, cs, "listen")) {
1324 this = listen_parse(cs, NULL);
1330 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1333 last = &(this->next);
1337 * Check virtual servers for "listen" sections, too.
1339 * FIXME: Move to virtual server init?
1341 for (cs = cf_subsection_find_next(config, NULL, "server");
1343 cs = cf_subsection_find_next(config, cs, "server")) {
1344 CONF_SECTION *subcs;
1345 const char *name2 = cf_section_name2(cs);
1347 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1349 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1350 this = listen_parse(subcs, name2);
1356 if (this->type == RAD_LISTEN_PROXY) {
1357 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
1363 last = &(this->next);
1364 } /* loop over "listen" directives in virtual servers */
1365 } /* loop over virtual servers */
1368 * If we're proxying requests, open the proxy FD.
1369 * Otherwise, don't do anything.
1372 if (mainconfig.proxy_requests == TRUE) {
1374 listen_socket_t *sock = NULL;
1377 * No sockets to receive packets, therefore
1378 * proxying is pointless.
1380 if (!*head) return -1;
1382 if (defined_proxy) goto do_snmp;
1385 * Find the first authentication port,
1388 for (this = *head; this != NULL; this = this->next) {
1389 if (this->type == RAD_LISTEN_AUTH) {
1391 if (server_ipaddr.af == AF_UNSPEC) {
1392 server_ipaddr = sock->ipaddr;
1394 port = sock->port + 2; /* skip acct port */
1397 if (this->type == RAD_LISTEN_VQP) {
1399 if (server_ipaddr.af == AF_UNSPEC) {
1400 server_ipaddr = sock->ipaddr;
1402 port = sock->port + 1;
1407 if (port < 0) port = 1024 + (fr_rand() & 0x1ff);
1410 * Address is still unspecified, use IPv4.
1412 if (server_ipaddr.af == AF_UNSPEC) {
1413 server_ipaddr.af = AF_INET;
1414 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1417 this = listen_alloc(RAD_LISTEN_PROXY);
1421 * Create the first proxy socket.
1423 sock->ipaddr = server_ipaddr;
1426 * Try to find a proxy port (value doesn't matter)
1428 for (sock->port = port;
1431 if (listen_bind(this) == 0) {
1433 last = &(this->next); /* just in case */
1438 if (sock->port >= 64000) {
1441 radlog(L_ERR, "Failed to open socket for proxying");
1448 if (radius_snmp_init(config)) {
1449 this = rad_malloc(sizeof(*this));
1450 memset(this, 0, sizeof(*this));
1452 this->type = RAD_LISTEN_SNMP;
1453 this->fd = rad_snmp.smux_fd;
1455 this->recv = radius_snmp_recv;
1456 this->print = radius_snmp_print;
1459 last = &(this->next);
1467 * Free a linked list of listeners;
1469 void listen_free(rad_listen_t **head)
1473 if (!head || !*head) return;
1477 rad_listen_t *next = this->next;
1480 * Other code may have eaten the FD.
1482 if (this->fd >= 0) close(this->fd);
1484 if (master_listen[this->type].free) {
1485 master_listen[this->type].free(this);