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>
33 #include <freeradius-devel/vmps.h>
34 #include <freeradius-devel/detail.h>
36 #ifdef HAVE_SYS_RESOURCE_H
37 #include <sys/resource.h>
50 * We'll use this below.
52 typedef int (*rad_listen_parse_t)(CONF_SECTION *, rad_listen_t *);
53 typedef void (*rad_listen_free_t)(rad_listen_t *);
55 typedef struct rad_listen_master_t {
56 rad_listen_parse_t parse;
57 rad_listen_free_t free;
58 rad_listen_recv_t recv;
59 rad_listen_send_t send;
60 rad_listen_print_t print;
61 rad_listen_encode_t encode;
62 rad_listen_decode_t decode;
63 } rad_listen_master_t;
65 typedef struct listen_socket_t {
71 RADCLIENT_LIST *clients;
75 * Find a per-socket client.
77 RADCLIENT *client_listener_find(const rad_listen_t *listener,
78 const fr_ipaddr_t *ipaddr)
80 const RADCLIENT_LIST *clients;
82 rad_assert(listener != NULL);
83 rad_assert(ipaddr != NULL);
85 rad_assert((listener->type == RAD_LISTEN_AUTH) ||
86 (listener->type == RAD_LISTEN_ACCT) ||
87 (listener->type == RAD_LISTEN_VQP));
89 clients = ((listen_socket_t *)listener->data)->clients;
92 * This HAS to have been initialized previously.
94 rad_assert(clients != NULL);
96 return client_find(clients, ipaddr);
99 static int listen_bind(rad_listen_t *this);
103 * Process and reply to a server-status request.
104 * Like rad_authenticate and rad_accounting this should
105 * live in it's own file but it's so small we don't bother.
107 static int rad_status_server(REQUEST *request)
109 int rcode = RLM_MODULE_OK;
112 switch (request->listener->type) {
113 case RAD_LISTEN_AUTH:
114 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
116 rcode = module_authorize(dval->value, request);
118 rcode = RLM_MODULE_OK;
123 case RLM_MODULE_UPDATED:
124 request->reply->code = PW_AUTHENTICATION_ACK;
127 case RLM_MODULE_FAIL:
128 case RLM_MODULE_HANDLED:
129 request->reply->code = 0; /* don't reply */
133 case RLM_MODULE_REJECT:
134 request->reply->code = PW_AUTHENTICATION_REJECT;
139 case RAD_LISTEN_ACCT:
140 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
142 rcode = module_accounting(dval->value, request);
144 rcode = RLM_MODULE_OK;
149 case RLM_MODULE_UPDATED:
150 request->reply->code = PW_ACCOUNTING_RESPONSE;
154 request->reply->code = 0; /* don't reply */
167 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
169 listen_socket_t *sock = this->data;
173 if ((sock->ipaddr.af == AF_INET) &&
174 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
177 ip_ntoh(&sock->ipaddr, ip_buf, sizeof(ip_buf));
180 switch (this->type) {
181 case RAD_LISTEN_AUTH:
182 name = "authentication";
185 case RAD_LISTEN_ACCT:
189 case RAD_LISTEN_PROXY:
205 return snprintf(buffer, bufsize, "%s address %s port %d",
206 name, ip_buf, sock->port);
209 return snprintf(buffer, bufsize, "%s address %s port %d as server %s",
210 name, ip_buf, sock->port, this->server);
215 * Parse an authentication or accounting socket.
217 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
222 listen_socket_t *sock = this->data;
223 char *section_name = NULL;
224 CONF_SECTION *client_cs, *parentcs;
229 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
230 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
231 &ipaddr.ipaddr.ip4addr, NULL);
232 if (rcode < 0) return -1;
234 if (rcode == 0) { /* successfully parsed IPv4 */
237 } else { /* maybe IPv6? */
238 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
239 &ipaddr.ipaddr.ip6addr, NULL);
240 if (rcode < 0) return -1;
243 cf_log_err(cf_sectiontoitem(cs),
244 "No address specified in listen section");
247 ipaddr.af = AF_INET6;
250 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
252 if (rcode < 0) return -1;
254 sock->ipaddr = ipaddr;
255 sock->port = listen_port;
258 * And bind it to the port.
260 if (listen_bind(this) < 0) {
262 cf_log_err(cf_sectiontoitem(cs),
263 "Error binding to port for %s port %d",
264 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
270 * If we can bind to interfaces, do so,
273 if (cf_pair_find(cs, "interface")) {
274 #ifndef SO_BINDTODEVICE
275 cf_log_err(cf_sectiontoitem(cs),
276 "System does not support binding to interfaces. Delete this line from the configuration file.");
280 CONF_PAIR *cp = cf_pair_find(cs, "interface");
283 rad_assert(cp != NULL);
284 value = cf_pair_value(cp);
286 cf_log_err(cf_sectiontoitem(cs),
287 "No interface name given");
291 strcpy(ifreq.ifr_name, value);
293 if (setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
294 (char *)&ifreq, sizeof(ifreq)) < 0) {
295 cf_log_err(cf_sectiontoitem(cs),
296 "Failed binding to interface %s: %s",
297 value, strerror(errno));
299 } /* else it worked. */
304 * The more specific configurations are preferred to more
308 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
309 §ion_name, NULL);
310 if (rcode < 0) return -1; /* bad string */
313 * Explicit list given: use it.
315 client_cs = cf_section_find(section_name);
317 cf_log_err(cf_sectiontoitem(cs),
318 "Failed to find clients %s {...}",
324 } /* else there was no "clients = " entry. */
326 parentcs = cf_top_section(cs);
328 CONF_SECTION *server_cs;
330 server_cs = cf_section_sub_find_name2(parentcs,
334 * Found a "server foo" section. If there are clients
338 (cf_section_sub_find(server_cs, "client") != NULL)) {
339 client_cs = server_cs;
344 * Still nothing. Look for global clients.
346 if (!client_cs) client_cs = parentcs;
348 sock->clients = clients_parse_section(client_cs);
349 if (!sock->clients) {
350 cf_log_err(cf_sectiontoitem(cs),
351 "Failed to find any clients for this listen section");
359 * Send an authentication response packet
361 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
363 rad_assert(request->listener == listener);
364 rad_assert(listener->send == auth_socket_send);
366 return rad_send(request->reply, request->packet,
367 request->client->secret);
372 * Send an accounting response packet (or not)
374 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
376 rad_assert(request->listener == listener);
377 rad_assert(listener->send == acct_socket_send);
380 * Accounting reject's are silently dropped.
382 * We do it here to avoid polluting the rest of the
383 * code with this knowledge
385 if (request->reply->code == 0) return 0;
387 return rad_send(request->reply, request->packet,
388 request->client->secret);
393 * Send a packet to a home server.
395 * FIXME: have different code for proxy auth & acct!
397 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
399 listen_socket_t *sock = listener->data;
401 rad_assert(request->proxy_listener == listener);
402 rad_assert(listener->send == proxy_socket_send);
404 request->proxy->src_ipaddr = sock->ipaddr;
405 request->proxy->src_port = sock->port;
407 return rad_send(request->proxy, request->packet,
408 request->home_server->secret);
413 * Check if an incoming request is "ok"
415 * It takes packets, not requests. It sees if the packet looks
416 * OK. If so, it does a number of sanity checks on it.
418 static int auth_socket_recv(rad_listen_t *listener,
419 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
423 RADIUS_PACKET *packet;
424 RAD_REQUEST_FUNP fun = NULL;
427 fr_ipaddr_t src_ipaddr;
429 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
430 if (rcode < 0) return 0;
432 RAD_SNMP_TYPE_INC(listener, total_requests);
434 if (rcode < 20) { /* AUTH_HDR_LEN */
435 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
439 if ((client = client_listener_find(listener,
440 &src_ipaddr)) == NULL) {
441 rad_recv_discard(listener->fd);
442 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
444 if (debug_flag > 0) {
447 listener->print(listener, name, sizeof(name));
450 * This is debugging rather than logging, so that
451 * DoS attacks don't affect us.
453 DEBUG("Ignoring request to %s from unknown client %s port %d",
455 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
456 buffer, sizeof(buffer)), src_port);
463 * Some sanity checks, based on the packet code.
466 case PW_AUTHENTICATION_REQUEST:
467 RAD_SNMP_CLIENT_INC(listener, client, requests);
468 fun = rad_authenticate;
471 case PW_STATUS_SERVER:
472 if (!mainconfig.status_server) {
473 rad_recv_discard(listener->fd);
474 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
475 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
476 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
479 fun = rad_status_server;
483 rad_recv_discard(listener->fd);
484 RAD_SNMP_INC(rad_snmp.auth.total_unknown_types);
485 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
487 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
488 code, client->shortname, src_port);
491 } /* switch over packet types */
494 * Now that we've sanity checked everything, receive the
497 packet = rad_recv(listener->fd);
499 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
500 radlog(L_ERR, "%s", librad_errstr);
504 if (!received_request(listener, packet, prequest, client)) {
505 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
506 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
517 * Receive packets from an accounting socket
519 static int acct_socket_recv(rad_listen_t *listener,
520 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
524 RADIUS_PACKET *packet;
525 RAD_REQUEST_FUNP fun = NULL;
528 fr_ipaddr_t src_ipaddr;
530 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
531 if (rcode < 0) return 0;
533 RAD_SNMP_TYPE_INC(listener, total_requests);
535 if (rcode < 20) { /* AUTH_HDR_LEN */
536 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
540 if ((client = client_listener_find(listener,
541 &src_ipaddr)) == NULL) {
542 rad_recv_discard(listener->fd);
543 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
546 * This is debugging rather than logging, so that
547 * DoS attacks don't affect us.
549 if (debug_flag > 0) {
552 listener->print(listener, name, sizeof(name));
554 DEBUG("Ignoring request to %s from unknown client %s port %d",
556 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
557 buffer, sizeof(buffer)), src_port);
564 * Some sanity checks, based on the packet code.
567 case PW_ACCOUNTING_REQUEST:
568 RAD_SNMP_CLIENT_INC(listener, client, requests);
569 fun = rad_accounting;
572 case PW_STATUS_SERVER:
573 if (!mainconfig.status_server) {
574 rad_recv_discard(listener->fd);
575 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
576 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
578 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
581 fun = rad_status_server;
585 rad_recv_discard(listener->fd);
586 RAD_SNMP_TYPE_INC(listener, total_unknown_types);
587 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
589 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
590 code, client->shortname, src_port);
592 } /* switch over packet types */
595 * Now that we've sanity checked everything, receive the
598 packet = rad_recv(listener->fd);
600 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
601 radlog(L_ERR, "%s", librad_errstr);
606 * There can be no duplicate accounting packets.
608 if (!received_request(listener, packet, prequest, client)) {
609 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
610 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
621 * Recieve packets from a proxy socket.
623 static int proxy_socket_recv(rad_listen_t *listener,
624 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
627 RADIUS_PACKET *packet;
628 RAD_REQUEST_FUNP fun = NULL;
631 packet = rad_recv(listener->fd);
633 radlog(L_ERR, "%s", librad_errstr);
638 * FIXME: Client MIB updates?
640 switch(packet->code) {
641 case PW_AUTHENTICATION_ACK:
642 case PW_ACCESS_CHALLENGE:
643 case PW_AUTHENTICATION_REJECT:
644 fun = rad_authenticate;
647 case PW_ACCOUNTING_RESPONSE:
648 fun = rad_accounting;
653 * FIXME: Update MIB for packet types?
655 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
656 "from home server %s port %d - ID %d : IGNORED",
658 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
659 packet->src_port, packet->id);
664 request = received_proxy_response(packet);
669 rad_assert(fun != NULL);
677 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
679 if (!request->reply->code) return 0;
681 rad_encode(request->reply, request->packet,
682 request->client->secret);
683 rad_sign(request->reply, request->packet,
684 request->client->secret);
690 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
692 if (rad_verify(request->packet, NULL,
693 request->client->secret) < 0) {
697 return rad_decode(request->packet, NULL,
698 request->client->secret);
701 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
703 rad_encode(request->proxy, NULL, request->home_server->secret);
704 rad_sign(request->proxy, NULL, request->home_server->secret);
710 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
712 if (rad_verify(request->proxy_reply, request->proxy,
713 request->home_server->secret) < 0) {
717 return rad_decode(request->proxy_reply, request->proxy,
718 request->home_server->secret);
723 static int radius_snmp_recv(rad_listen_t *listener,
724 UNUSED RAD_REQUEST_FUNP *pfun,
725 UNUSED REQUEST **prequest)
727 if ((rad_snmp.smux_fd >= 0) &&
728 (rad_snmp.smux_event == SMUX_READ)) {
733 * If we've got to re-connect, then do so now,
734 * before calling select again.
736 if (rad_snmp.smux_event == SMUX_CONNECT) {
741 * Reset this every time, as the smux connect may have
742 * opened a new socket.
744 listener->fd = rad_snmp.smux_fd;
750 static int radius_snmp_print(UNUSED rad_listen_t *this, char *buffer, size_t bufsize)
752 return snprintf(buffer, bufsize, "SMUX with OID .1.3.6.1.4.1.11344.1.1.1");
757 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
758 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
762 proxy_socket_recv, proxy_socket_send,
763 socket_print, proxy_socket_encode, proxy_socket_decode },
766 { common_socket_parse, NULL,
767 auth_socket_recv, auth_socket_send,
768 socket_print, client_socket_encode, client_socket_decode },
771 { common_socket_parse, NULL,
772 acct_socket_recv, acct_socket_send,
773 socket_print, client_socket_encode, client_socket_decode},
776 { detail_parse, detail_free,
777 detail_recv, detail_send,
778 detail_print, detail_encode, detail_decode },
781 /* vlan query protocol */
782 { common_socket_parse, NULL,
783 vqp_socket_recv, vqp_socket_send,
784 socket_print, vqp_socket_encode, vqp_socket_decode },
786 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
789 { NULL, NULL, NULL, NULL, NULL, NULL, NULL} /* RAD_LISTEN_SNMP */
795 * Binds a listener to a socket.
797 static int listen_bind(rad_listen_t *this)
800 listen_socket_t *sock = this->data;
803 * If the port is zero, then it means the appropriate
804 * thing from /etc/services.
806 if (sock->port == 0) {
809 switch (this->type) {
810 case RAD_LISTEN_AUTH:
811 svp = getservbyname ("radius", "udp");
813 sock->port = ntohs(svp->s_port);
815 sock->port = PW_AUTH_UDP_PORT;
819 case RAD_LISTEN_ACCT:
820 svp = getservbyname ("radacct", "udp");
822 sock->port = ntohs(svp->s_port);
824 sock->port = PW_ACCT_UDP_PORT;
835 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
841 * Find it in the old list, AFTER updating the port. If
842 * it's there, use that, rather than creating a new
843 * socket. This allows HUP's to re-use the old sockets,
844 * which means that packets waiting in the socket queue
847 for (last = &mainconfig.listen;
849 last = &((*last)->next)) {
850 listen_socket_t *other;
852 if (this->type != (*last)->type) continue;
854 if ((this->type == RAD_LISTEN_DETAIL) ||
855 (this->type == RAD_LISTEN_SNMP)) continue;
857 other = (listen_socket_t *)((*last)->data);
859 if ((sock->port == other->port) &&
860 (sock->ipaddr.af == other->ipaddr.af) &&
861 (fr_ipaddr_cmp(&sock->ipaddr, &other->ipaddr) == 0)) {
862 this->fd = (*last)->fd;
868 this->fd = fr_socket(&sock->ipaddr, sock->port);
870 radlog(L_ERR, "ERROR: Failed to open socket: %s",
876 * FreeBSD jail issues. We bind to 0.0.0.0, but the
877 * kernel instead binds us to a 1.2.3.4. If this
878 * happens, notice, and remember our real IP.
881 struct sockaddr_storage src;
882 socklen_t sizeof_src = sizeof(src);
884 memset(&src, 0, sizeof_src);
885 if (getsockname(this->fd, (struct sockaddr *) &src,
890 if (src.ss_family == AF_INET) {
891 struct sockaddr_in *s4;
893 s4 = (struct sockaddr_in *)&src;
894 sock->ipaddr.ipaddr.ip4addr = s4->sin_addr;
896 #ifdef HAVE_STRUCT_SOCKADDR_IN6
897 } else if (src.ss_family == AF_INET6) {
898 struct sockaddr_in6 *s6;
900 s6 = (struct sockaddr_in6 *)&src;
901 sock->ipaddr.ipaddr.ip6addr = s6->sin6_addr;
912 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
913 radlog(L_ERR, "Failure getting socket flags: %s)\n",
919 if( fcntl(this->fd, F_SETFL, flags) < 0) {
920 radlog(L_ERR, "Failure setting socket flags: %s)\n",
932 * Allocate & initialize a new listener.
934 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
938 this = rad_malloc(sizeof(*this));
939 memset(this, 0, sizeof(*this));
942 this->recv = master_listen[this->type].recv;
943 this->send = master_listen[this->type].send;
944 this->print = master_listen[this->type].print;
945 this->encode = master_listen[this->type].encode;
946 this->decode = master_listen[this->type].decode;
949 case RAD_LISTEN_AUTH:
950 case RAD_LISTEN_ACCT:
951 case RAD_LISTEN_PROXY:
953 this->data = rad_malloc(sizeof(listen_socket_t));
954 memset(this->data, 0, sizeof(listen_socket_t));
957 case RAD_LISTEN_DETAIL:
962 rad_assert("Unsupported option!" == NULL);
971 * Externally visible function for creating a new proxy LISTENER.
973 * For now, don't take ipaddr or port.
975 * Not thread-safe, but all calls to it are protected by the
976 * proxy mutex in request_list.c
978 rad_listen_t *proxy_new_listener()
980 int last_proxy_port, port;
981 rad_listen_t *this, *tmp, **last;
982 listen_socket_t *sock, *old;
984 this = listen_alloc(RAD_LISTEN_PROXY);
987 * Find an existing proxy socket to copy.
989 * FIXME: Make it per-realm, or per-home server!
993 last = &mainconfig.listen;
994 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
995 if (tmp->type == RAD_LISTEN_PROXY) {
997 if (sock->port > last_proxy_port) {
998 last_proxy_port = sock->port + 1;
1000 if (!old) old = sock;
1003 last = &(tmp->next);
1008 return NULL; /* This is a serious error. */
1012 * FIXME: find a new IP address to listen on?
1014 * This could likely be done in the "home server"
1015 * configuration, to have per-home-server source IP's.
1018 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1021 * Keep going until we find an unused port.
1023 for (port = last_proxy_port; port < 64000; port++) {
1025 if (listen_bind(this) == 0) {
1027 * Add the new listener to the list of
1040 static const FR_NAME_NUMBER listen_compare[] = {
1041 { "auth", RAD_LISTEN_AUTH },
1042 { "acct", RAD_LISTEN_ACCT },
1043 { "detail", RAD_LISTEN_DETAIL },
1045 { "vmps", RAD_LISTEN_VQP },
1051 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1059 cf_log_info(cs, "listen {");
1061 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1063 if (rcode < 0) return NULL;
1066 cf_log_err(cf_sectiontoitem(cs),
1067 "No type specified in listen section");
1071 type = fr_str2int(listen_compare, listen_type,
1073 if (type == RAD_LISTEN_NONE) {
1074 cf_log_err(cf_sectiontoitem(cs),
1075 "Invalid type \"%s\" in listen section.",
1083 * Allow listen sections in the default config to
1084 * refer to a server.
1087 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1089 if (rcode == 1) { /* compatiblity with 2.0-pre */
1090 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1093 if (rcode < 0) return NULL;
1097 * Set up cross-type data.
1099 this = listen_alloc(type);
1100 this->server = server;
1104 * Call per-type parser.
1106 if (master_listen[type].parse(cs, this) < 0) {
1111 cf_log_info(cs, "}");
1117 * Generate a list of listeners. Takes an input list of
1118 * listeners, too, so we don't close sockets with waiting packets.
1120 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1124 rad_listen_t **last;
1126 fr_ipaddr_t server_ipaddr;
1130 * We shouldn't be called with a pre-existing list.
1132 rad_assert(head && (*head == NULL));
1135 server_ipaddr.af = AF_UNSPEC;
1138 * If the port is specified on the command-line,
1139 * it over-rides the configuration file.
1141 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1143 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1146 * If the IP address was configured on the command-line,
1147 * use that as the "bind_address"
1149 if (mainconfig.myip.af != AF_UNSPEC) {
1150 memcpy(&server_ipaddr, &mainconfig.myip,
1151 sizeof(server_ipaddr));
1156 * Else look for bind_address and/or listen sections.
1158 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1159 rcode = cf_item_parse(config, "bind_address",
1161 &server_ipaddr.ipaddr.ip4addr, NULL);
1162 if (rcode < 0) return -1; /* error parsing it */
1164 if (rcode == 0) { /* successfully parsed IPv4 */
1165 listen_socket_t *sock;
1166 server_ipaddr.af = AF_INET;
1168 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'.");
1172 if (strcmp(progname, "vmpsd") == 0) {
1173 this = listen_alloc(RAD_LISTEN_VQP);
1174 if (!auth_port) auth_port = 1589;
1177 this = listen_alloc(RAD_LISTEN_AUTH);
1181 sock->ipaddr = server_ipaddr;
1182 sock->port = auth_port;
1184 if (listen_bind(this) < 0) {
1187 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1190 auth_port = sock->port; /* may have been updated in listen_bind */
1192 last = &(this->next);
1198 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
1202 * Open Accounting Socket.
1204 * If we haven't already gotten acct_port from
1205 * /etc/services, then make it auth_port + 1.
1207 this = listen_alloc(RAD_LISTEN_ACCT);
1211 * Create the accounting socket.
1213 * The accounting port is always the
1214 * authentication port + 1
1216 sock->ipaddr = server_ipaddr;
1217 sock->port = auth_port + 1;
1219 if (listen_bind(this) < 0) {
1222 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1227 last = &(this->next);
1229 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1230 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1236 * They specified an IP on the command-line, ignore
1237 * all listen sections.
1239 if (mainconfig.myip.af != AF_UNSPEC) goto do_proxy;
1242 * Walk through the "listen" sections, if they exist.
1244 for (cs = cf_subsection_find_next(config, NULL, "listen");
1246 cs = cf_subsection_find_next(config, cs, "listen")) {
1247 this = listen_parse(cs, NULL);
1254 last = &(this->next);
1258 * Check virtual servers for "listen" sections, too.
1260 * FIXME: Move to virtual server init?
1262 for (cs = cf_subsection_find_next(config, NULL, "server");
1264 cs = cf_subsection_find_next(config, cs, "server")) {
1265 CONF_SECTION *subcs;
1266 const char *name2 = cf_section_name2(cs);
1268 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1270 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1271 this = listen_parse(subcs, name2);
1278 last = &(this->next);
1279 } /* loop over "listen" directives in virtual servers */
1280 } /* loop over virtual servers */
1283 * If we're proxying requests, open the proxy FD.
1284 * Otherwise, don't do anything.
1287 if (mainconfig.proxy_requests == TRUE) {
1289 listen_socket_t *sock = NULL;
1292 * No sockets to receive packets, therefore
1293 * proxying is pointless.
1295 if (!*head) return -1;
1298 * If we previously had proxy sockets, copy them
1299 * to the new config.
1301 if (mainconfig.listen != NULL) {
1302 rad_listen_t *old, *next, **tail;
1304 tail = &mainconfig.listen;
1305 for (old = mainconfig.listen;
1310 if (old->type != RAD_LISTEN_PROXY) {
1311 tail = &((*tail)->next);
1318 last = &(old->next);
1325 * Find the first authentication port,
1328 for (this = *head; this != NULL; this = this->next) {
1329 if (this->type == RAD_LISTEN_AUTH) {
1331 if (server_ipaddr.af == AF_UNSPEC) {
1332 server_ipaddr = sock->ipaddr;
1334 port = sock->port + 2; /* skip acct port */
1337 if (this->type == RAD_LISTEN_VQP) {
1339 if (server_ipaddr.af == AF_UNSPEC) {
1340 server_ipaddr = sock->ipaddr;
1342 port = sock->port + 1;
1347 if (port < 0) port = 1024 + (fr_rand() & 0x1ff);
1350 * Address is still unspecified, use IPv4.
1352 if (server_ipaddr.af == AF_UNSPEC) {
1353 server_ipaddr.af = AF_INET;
1354 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1357 this = listen_alloc(RAD_LISTEN_PROXY);
1361 * Create the first proxy socket.
1363 sock->ipaddr = server_ipaddr;
1366 * Try to find a proxy port (value doesn't matter)
1368 for (sock->port = port;
1371 if (listen_bind(this) == 0) {
1373 last = &(this->next); /* just in case */
1378 if (sock->port >= 64000) {
1381 radlog(L_ERR, "Failed to open socket for proxying");
1388 if (radius_snmp_init(config)) {
1390 * Forget about the old one.
1392 for (this = mainconfig.listen;
1394 this = this->next) {
1395 if (this->type != RAD_LISTEN_SNMP) continue;
1399 this = rad_malloc(sizeof(*this));
1400 memset(this, 0, sizeof(*this));
1402 this->type = RAD_LISTEN_SNMP;
1403 this->fd = rad_snmp.smux_fd;
1405 this->recv = radius_snmp_recv;
1406 this->print = radius_snmp_print;
1409 last = &(this->next);
1417 * Free a linked list of listeners;
1419 void listen_free(rad_listen_t **head)
1423 if (!head || !*head) return;
1427 rad_listen_t *next = this->next;
1430 * Other code may have eaten the FD.
1432 if (this->fd >= 0) close(this->fd);
1434 if (master_listen[this->type].free) {
1435 master_listen[this->type].free(this);