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 #include <sys/resource.h>
43 * We'll use this below.
45 typedef int (*rad_listen_parse_t)(CONF_SECTION *, rad_listen_t *);
46 typedef void (*rad_listen_free_t)(rad_listen_t *);
48 typedef struct rad_listen_master_t {
49 rad_listen_parse_t parse;
50 rad_listen_free_t free;
51 rad_listen_recv_t recv;
52 rad_listen_send_t send;
53 rad_listen_print_t print;
54 rad_listen_encode_t encode;
55 rad_listen_decode_t decode;
56 } rad_listen_master_t;
58 typedef struct listen_socket_t {
64 RADCLIENT_LIST *clients;
68 * Find a per-socket client.
70 RADCLIENT *client_listener_find(const rad_listen_t *listener,
71 const lrad_ipaddr_t *ipaddr)
73 const RADCLIENT_LIST *clients;
75 rad_assert(listener != NULL);
76 rad_assert(ipaddr != NULL);
78 rad_assert((listener->type == RAD_LISTEN_AUTH) ||
79 (listener->type == RAD_LISTEN_ACCT) ||
80 (listener->type == RAD_LISTEN_VQP));
82 clients = ((listen_socket_t *)listener->data)->clients;
85 * This HAS to have been initialized previously.
87 rad_assert(clients != NULL);
89 return client_find(clients, ipaddr);
92 static int listen_bind(rad_listen_t *this);
96 * Process and reply to a server-status request.
97 * Like rad_authenticate and rad_accounting this should
98 * live in it's own file but it's so small we don't bother.
100 static int rad_status_server(REQUEST *request)
102 int rcode = RLM_MODULE_OK;
105 switch (request->listener->type) {
106 case RAD_LISTEN_AUTH:
107 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
109 rcode = module_authorize(dval->value, request);
111 rcode = RLM_MODULE_OK;
116 case RLM_MODULE_UPDATED:
117 request->reply->code = PW_AUTHENTICATION_ACK;
120 case RLM_MODULE_FAIL:
121 case RLM_MODULE_HANDLED:
122 request->reply->code = 0; /* don't reply */
126 case RLM_MODULE_REJECT:
127 request->reply->code = PW_AUTHENTICATION_REJECT;
132 case RAD_LISTEN_ACCT:
133 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
135 rcode = module_accounting(dval->value, request);
137 rcode = RLM_MODULE_OK;
142 case RLM_MODULE_UPDATED:
143 request->reply->code = PW_ACCOUNTING_RESPONSE;
147 request->reply->code = 0; /* don't reply */
160 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
162 listen_socket_t *sock = this->data;
166 if ((sock->ipaddr.af == AF_INET) &&
167 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
170 ip_ntoh(&sock->ipaddr, ip_buf, sizeof(ip_buf));
173 switch (this->type) {
174 case RAD_LISTEN_AUTH:
175 name = "authentication";
178 case RAD_LISTEN_ACCT:
182 case RAD_LISTEN_PROXY:
198 return snprintf(buffer, bufsize, "%s address %s port %d",
199 name, ip_buf, sock->port);
202 return snprintf(buffer, bufsize, "%s address %s port %d as server %s",
203 name, ip_buf, sock->port, this->server);
208 * Parse an authentication or accounting socket.
210 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
214 lrad_ipaddr_t ipaddr;
215 listen_socket_t *sock = this->data;
216 const char *section_name = NULL;
217 CONF_SECTION *client_cs, *parentcs;
222 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
223 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
224 &ipaddr.ipaddr.ip4addr, NULL);
225 if (rcode < 0) return -1;
227 if (rcode == 0) { /* successfully parsed IPv4 */
230 } else { /* maybe IPv6? */
231 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
232 &ipaddr.ipaddr.ip6addr, NULL);
233 if (rcode < 0) return -1;
236 cf_log_err(cf_sectiontoitem(cs),
237 "No address specified in listen section");
240 ipaddr.af = AF_INET6;
243 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
245 if (rcode < 0) return -1;
247 sock->ipaddr = ipaddr;
248 sock->port = listen_port;
251 * And bind it to the port.
253 if (listen_bind(this) < 0) {
255 cf_log_err(cf_sectiontoitem(cs),
256 "Error binding to port for %s port %d",
257 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
263 * If we can bind to interfaces, do so,
266 if (cf_pair_find(cs, "interface")) {
267 #ifndef SO_BINDTODEVICE
268 cf_log_err(cf_sectiontoitem(cs),
269 "System does not support binding to interfaces. Delete this line from the configuration file.");
273 CONF_PAIR *cp = cf_pair_find(cs, "interface");
276 rad_assert(cp != NULL);
277 value = cf_pair_value(cp);
279 cf_log_err(cf_sectiontoitem(cs),
280 "No interface name given");
284 strcpy(ifreq.ifr_name, value);
286 if (setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
287 (char *)&ifreq, sizeof(ifreq)) < 0) {
288 cf_log_err(cf_sectiontoitem(cs),
289 "Failed binding to interface %s: %s",
290 value, strerror(errno));
292 } /* else it worked. */
297 * The more specific configurations are preferred to more
301 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
302 §ion_name, NULL);
303 if (rcode < 0) return -1; /* bad string */
306 * Explicit list given: use it.
308 client_cs = cf_section_find(section_name);
311 cf_log_err(cf_sectiontoitem(cs),
312 "Failed to find clients %s {...}",
316 } /* else there was no "clients = " entry. */
318 parentcs = cf_top_section(cs);
320 CONF_SECTION *server_cs;
322 server_cs = cf_section_sub_find_name2(parentcs,
326 * Found a "server foo" section. If there are clients
330 (cf_section_sub_find(server_cs, "client") != NULL)) {
331 client_cs = server_cs;
336 * Still nothing. Look for global clients.
338 if (!client_cs) client_cs = parentcs;
340 sock->clients = clients_parse_section(client_cs);
341 if (!sock->clients) {
342 cf_log_err(cf_sectiontoitem(cs),
343 "Failed to find any clients for this listen section");
351 * Send an authentication response packet
353 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
355 rad_assert(request->listener == listener);
356 rad_assert(listener->send == auth_socket_send);
358 return rad_send(request->reply, request->packet,
359 request->client->secret);
364 * Send an accounting response packet (or not)
366 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
368 rad_assert(request->listener == listener);
369 rad_assert(listener->send == acct_socket_send);
372 * Accounting reject's are silently dropped.
374 * We do it here to avoid polluting the rest of the
375 * code with this knowledge
377 if (request->reply->code == 0) return 0;
379 return rad_send(request->reply, request->packet,
380 request->client->secret);
385 * Send a packet to a home server.
387 * FIXME: have different code for proxy auth & acct!
389 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
391 listen_socket_t *sock = listener->data;
393 rad_assert(request->proxy_listener == listener);
394 rad_assert(listener->send == proxy_socket_send);
396 request->proxy->src_ipaddr = sock->ipaddr;
397 request->proxy->src_port = sock->port;
399 return rad_send(request->proxy, request->packet,
400 request->home_server->secret);
405 * Check if an incoming request is "ok"
407 * It takes packets, not requests. It sees if the packet looks
408 * OK. If so, it does a number of sanity checks on it.
410 static int auth_socket_recv(rad_listen_t *listener,
411 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
415 RADIUS_PACKET *packet;
416 RAD_REQUEST_FUNP fun = NULL;
419 lrad_ipaddr_t src_ipaddr;
421 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
422 if (rcode < 0) return 0;
424 RAD_SNMP_TYPE_INC(listener, total_requests);
426 if (rcode < 20) { /* AUTH_HDR_LEN */
427 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
431 if ((client = client_listener_find(listener,
432 &src_ipaddr)) == NULL) {
433 rad_recv_discard(listener->fd);
434 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
436 if (debug_flag > 0) {
439 listener->print(listener, name, sizeof(name));
442 * This is debugging rather than logging, so that
443 * DoS attacks don't affect us.
445 DEBUG("Ignoring request to %s from unknown client %s port %d",
447 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
448 buffer, sizeof(buffer)), src_port);
455 * Some sanity checks, based on the packet code.
458 case PW_AUTHENTICATION_REQUEST:
459 RAD_SNMP_CLIENT_INC(listener, client, requests);
460 fun = rad_authenticate;
463 case PW_STATUS_SERVER:
464 if (!mainconfig.status_server) {
465 rad_recv_discard(listener->fd);
466 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
467 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
468 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
471 fun = rad_status_server;
475 rad_recv_discard(listener->fd);
476 RAD_SNMP_INC(rad_snmp.auth.total_unknown_types);
477 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
479 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
480 code, client->shortname, src_port);
483 } /* switch over packet types */
486 * Now that we've sanity checked everything, receive the
489 packet = rad_recv(listener->fd);
491 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
492 radlog(L_ERR, "%s", librad_errstr);
496 if (!received_request(listener, packet, prequest, client)) {
497 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
498 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
509 * Receive packets from an accounting socket
511 static int acct_socket_recv(rad_listen_t *listener,
512 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
516 RADIUS_PACKET *packet;
517 RAD_REQUEST_FUNP fun = NULL;
520 lrad_ipaddr_t src_ipaddr;
522 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
523 if (rcode < 0) return 0;
525 RAD_SNMP_TYPE_INC(listener, total_requests);
527 if (rcode < 20) { /* AUTH_HDR_LEN */
528 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
532 if ((client = client_listener_find(listener,
533 &src_ipaddr)) == NULL) {
534 rad_recv_discard(listener->fd);
535 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
538 * This is debugging rather than logging, so that
539 * DoS attacks don't affect us.
541 if (debug_flag > 0) {
544 listener->print(listener, name, sizeof(name));
546 DEBUG("Ignoring request to %s from unknown client %s port %d",
548 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
549 buffer, sizeof(buffer)), src_port);
556 * Some sanity checks, based on the packet code.
559 case PW_ACCOUNTING_REQUEST:
560 RAD_SNMP_CLIENT_INC(listener, client, requests);
561 fun = rad_accounting;
564 case PW_STATUS_SERVER:
565 if (!mainconfig.status_server) {
566 rad_recv_discard(listener->fd);
567 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
568 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
570 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
573 fun = rad_status_server;
577 rad_recv_discard(listener->fd);
578 RAD_SNMP_TYPE_INC(listener, total_unknown_types);
579 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
581 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
582 code, client->shortname, src_port);
584 } /* switch over packet types */
587 * Now that we've sanity checked everything, receive the
590 packet = rad_recv(listener->fd);
592 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
593 radlog(L_ERR, "%s", librad_errstr);
598 * There can be no duplicate accounting packets.
600 if (!received_request(listener, packet, prequest, client)) {
601 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
602 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
613 * Recieve packets from a proxy socket.
615 static int proxy_socket_recv(rad_listen_t *listener,
616 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
619 RADIUS_PACKET *packet;
620 RAD_REQUEST_FUNP fun = NULL;
623 packet = rad_recv(listener->fd);
625 radlog(L_ERR, "%s", librad_errstr);
630 * FIXME: Client MIB updates?
632 switch(packet->code) {
633 case PW_AUTHENTICATION_ACK:
634 case PW_ACCESS_CHALLENGE:
635 case PW_AUTHENTICATION_REJECT:
636 fun = rad_authenticate;
639 case PW_ACCOUNTING_RESPONSE:
640 fun = rad_accounting;
645 * FIXME: Update MIB for packet types?
647 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
648 "from home server %s port %d - ID %d : IGNORED",
650 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
651 packet->src_port, packet->id);
656 request = received_proxy_response(packet);
661 rad_assert(fun != NULL);
669 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
671 if (!request->reply->code) return 0;
673 rad_encode(request->reply, request->packet,
674 request->client->secret);
675 rad_sign(request->reply, request->packet,
676 request->client->secret);
682 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
684 if (rad_verify(request->packet, NULL,
685 request->client->secret) < 0) {
689 return rad_decode(request->packet, NULL,
690 request->client->secret);
693 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
695 rad_encode(request->proxy, NULL, request->home_server->secret);
696 rad_sign(request->proxy, NULL, request->home_server->secret);
702 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
704 if (rad_verify(request->proxy_reply, request->proxy,
705 request->home_server->secret) < 0) {
709 return rad_decode(request->proxy_reply, request->proxy,
710 request->home_server->secret);
715 static int radius_snmp_recv(rad_listen_t *listener,
716 UNUSED RAD_REQUEST_FUNP *pfun,
717 UNUSED REQUEST **prequest)
719 if ((rad_snmp.smux_fd >= 0) &&
720 (rad_snmp.smux_event == SMUX_READ)) {
725 * If we've got to re-connect, then do so now,
726 * before calling select again.
728 if (rad_snmp.smux_event == SMUX_CONNECT) {
733 * Reset this every time, as the smux connect may have
734 * opened a new socket.
736 listener->fd = rad_snmp.smux_fd;
742 static int radius_snmp_print(UNUSED rad_listen_t *this, char *buffer, size_t bufsize)
744 return snprintf(buffer, bufsize, "SMUX with OID .1.3.6.1.4.1.11344.1.1.1");
749 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
750 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
754 proxy_socket_recv, proxy_socket_send,
755 socket_print, proxy_socket_encode, proxy_socket_decode },
758 { common_socket_parse, NULL,
759 auth_socket_recv, auth_socket_send,
760 socket_print, client_socket_encode, client_socket_decode },
763 { common_socket_parse, NULL,
764 acct_socket_recv, acct_socket_send,
765 socket_print, client_socket_encode, client_socket_decode},
768 { detail_parse, detail_free,
769 detail_recv, detail_send,
770 detail_print, detail_encode, detail_decode },
773 /* vlan query protocol */
774 { common_socket_parse, NULL,
775 vqp_socket_recv, vqp_socket_send,
776 socket_print, vqp_socket_encode, vqp_socket_decode },
778 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
781 { NULL, NULL, NULL, NULL, NULL, NULL, NULL} /* RAD_LISTEN_SNMP */
787 * Binds a listener to a socket.
789 static int listen_bind(rad_listen_t *this)
792 listen_socket_t *sock = this->data;
795 * If the port is zero, then it means the appropriate
796 * thing from /etc/services.
798 if (sock->port == 0) {
801 switch (this->type) {
802 case RAD_LISTEN_AUTH:
803 svp = getservbyname ("radius", "udp");
805 sock->port = ntohs(svp->s_port);
807 sock->port = PW_AUTH_UDP_PORT;
811 case RAD_LISTEN_ACCT:
812 svp = getservbyname ("radacct", "udp");
814 sock->port = ntohs(svp->s_port);
816 sock->port = PW_ACCT_UDP_PORT;
827 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
833 * Find it in the old list, AFTER updating the port. If
834 * it's there, use that, rather than creating a new
835 * socket. This allows HUP's to re-use the old sockets,
836 * which means that packets waiting in the socket queue
839 for (last = &mainconfig.listen;
841 last = &((*last)->next)) {
842 listen_socket_t *other;
844 if (this->type != (*last)->type) continue;
846 if ((this->type == RAD_LISTEN_DETAIL) ||
847 (this->type == RAD_LISTEN_SNMP)) continue;
849 other = (listen_socket_t *)((*last)->data);
851 if ((sock->port == other->port) &&
852 (sock->ipaddr.af == other->ipaddr.af) &&
853 (lrad_ipaddr_cmp(&sock->ipaddr, &other->ipaddr) == 0)) {
854 this->fd = (*last)->fd;
860 this->fd = lrad_socket(&sock->ipaddr, sock->port);
862 radlog(L_ERR, "ERROR: Failed to open socket: %s",
868 struct sockaddr_storage src;
869 socklen_t sizeof_src = sizeof(src);
871 memset(&src, 0, sizeof_src);
872 if (getsockname(this->fd, (struct sockaddr *) &src,
877 if (src.ss_family == AF_INET) {
878 struct sockaddr_in *s4;
880 s4 = (struct sockaddr_in *)&src;
881 sock->ipaddr.ipaddr.ip4addr = s4->sin_addr;
883 #ifdef HAVE_STRUCT_SOCKADDR_IN6
884 } else if (src.ss_family == AF_INET6) {
885 struct sockaddr_in6 *s6;
887 s6 = (struct sockaddr_in6 *)&src;
888 sock->ipaddr.ipaddr.ip6addr = s6->sin6_addr;
897 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
898 radlog(L_ERR, "Failure in fcntl: %s)\n", strerror(errno));
903 if( fcntl(this->fd, F_SETFL, flags) < 0) {
904 radlog(L_ERR, "Failure in fcntl: %s)\n", strerror(errno));
915 * Allocate & initialize a new listener.
917 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
921 this = rad_malloc(sizeof(*this));
922 memset(this, 0, sizeof(*this));
925 this->recv = master_listen[this->type].recv;
926 this->send = master_listen[this->type].send;
927 this->print = master_listen[this->type].print;
928 this->encode = master_listen[this->type].encode;
929 this->decode = master_listen[this->type].decode;
932 case RAD_LISTEN_AUTH:
933 case RAD_LISTEN_ACCT:
934 case RAD_LISTEN_PROXY:
936 this->data = rad_malloc(sizeof(listen_socket_t));
937 memset(this->data, 0, sizeof(listen_socket_t));
940 case RAD_LISTEN_DETAIL:
945 rad_assert("Unsupported option!" == NULL);
954 * Externally visible function for creating a new proxy LISTENER.
956 * For now, don't take ipaddr or port.
958 * Not thread-safe, but all calls to it are protected by the
959 * proxy mutex in request_list.c
961 rad_listen_t *proxy_new_listener()
963 int last_proxy_port, port;
964 rad_listen_t *this, *tmp, **last;
965 listen_socket_t *sock, *old;
967 this = listen_alloc(RAD_LISTEN_PROXY);
970 * Find an existing proxy socket to copy.
972 * FIXME: Make it per-realm, or per-home server!
976 last = &mainconfig.listen;
977 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
978 if (tmp->type == RAD_LISTEN_PROXY) {
980 if (sock->port > last_proxy_port) {
981 last_proxy_port = sock->port + 1;
983 if (!old) old = sock;
989 if (!old) return NULL; /* This is a serious error. */
992 * FIXME: find a new IP address to listen on?
994 * This could likely be done in the "home server"
995 * configuration, to have per-home-server source IP's.
998 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1001 * Keep going until we find an unused port.
1003 for (port = last_proxy_port; port < 64000; port++) {
1005 if (listen_bind(this) == 0) {
1007 * Add the new listener to the list of
1019 static const LRAD_NAME_NUMBER listen_compare[] = {
1020 { "auth", RAD_LISTEN_AUTH },
1021 { "acct", RAD_LISTEN_ACCT },
1022 { "detail", RAD_LISTEN_DETAIL },
1024 { "vmps", RAD_LISTEN_VQP },
1030 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1040 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1042 if (rcode < 0) return NULL;
1045 cf_log_err(cf_sectiontoitem(cs),
1046 "No type specified in listen section");
1050 type = lrad_str2int(listen_compare, listen_type,
1052 if (type == RAD_LISTEN_NONE) {
1053 cf_log_err(cf_sectiontoitem(cs),
1054 "Invalid type \"%s\" in listen section.",
1062 * Allow listen sections in the default config to
1063 * refer to a server.
1066 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1068 if (rcode == 1) { /* compatiblity with 2.0-pre */
1069 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1072 if (rcode < 0) return NULL;
1076 * Set up cross-type data.
1078 this = listen_alloc(type);
1079 this->server = server;
1083 * Call per-type parser.
1085 if (master_listen[type].parse(cs, this) < 0) {
1096 * Generate a list of listeners. Takes an input list of
1097 * listeners, too, so we don't close sockets with waiting packets.
1099 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1103 rad_listen_t **last;
1105 lrad_ipaddr_t server_ipaddr;
1109 * We shouldn't be called with a pre-existing list.
1111 rad_assert(head && (*head == NULL));
1114 server_ipaddr.af = AF_UNSPEC;
1117 * If the port is specified on the command-line,
1118 * it over-rides the configuration file.
1120 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1122 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1125 * If the IP address was configured on the command-line,
1126 * use that as the "bind_address"
1128 if (mainconfig.myip.af != AF_UNSPEC) {
1129 memcpy(&server_ipaddr, &mainconfig.myip,
1130 sizeof(server_ipaddr));
1135 * Else look for bind_address and/or listen sections.
1137 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1138 rcode = cf_item_parse(config, "bind_address",
1140 &server_ipaddr.ipaddr.ip4addr, NULL);
1141 if (rcode < 0) return -1; /* error parsing it */
1143 if (rcode == 0) { /* successfully parsed IPv4 */
1144 listen_socket_t *sock;
1145 server_ipaddr.af = AF_INET;
1147 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'.");
1151 if (strcmp(progname, "vmpsd") == 0) {
1152 this = listen_alloc(RAD_LISTEN_VQP);
1153 if (!auth_port) auth_port = 1589;
1156 this = listen_alloc(RAD_LISTEN_AUTH);
1160 sock->ipaddr = server_ipaddr;
1161 sock->port = auth_port;
1163 if (listen_bind(this) < 0) {
1166 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1169 auth_port = sock->port; /* may have been updated in listen_bind */
1171 last = &(this->next);
1177 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
1181 * Open Accounting Socket.
1183 * If we haven't already gotten acct_port from
1184 * /etc/services, then make it auth_port + 1.
1186 this = listen_alloc(RAD_LISTEN_ACCT);
1190 * Create the accounting socket.
1192 * The accounting port is always the
1193 * authentication port + 1
1195 sock->ipaddr = server_ipaddr;
1196 sock->port = auth_port + 1;
1198 if (listen_bind(this) < 0) {
1201 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1206 last = &(this->next);
1208 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1209 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1215 * They specified an IP on the command-line, ignore
1216 * all listen sections.
1218 if (mainconfig.myip.af != AF_UNSPEC) goto do_proxy;
1221 * Walk through the "listen" sections, if they exist.
1223 for (cs = cf_subsection_find_next(config, NULL, "listen");
1225 cs = cf_subsection_find_next(config, cs, "listen")) {
1226 this = listen_parse(cs, NULL);
1233 last = &(this->next);
1237 * Check virtual servers for "listen" sections, too.
1239 * FIXME: Move to virtual server init?
1241 for (cs = cf_subsection_find_next(config, NULL, "server");
1243 cs = cf_subsection_find_next(config, cs, "server")) {
1244 CONF_SECTION *subcs;
1245 const char *name2 = cf_section_name2(cs);
1247 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1249 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1250 this = listen_parse(subcs, name2);
1257 last = &(this->next);
1258 } /* loop over "listen" directives in virtual servers */
1259 } /* loop over virtual servers */
1262 * If we're proxying requests, open the proxy FD.
1263 * Otherwise, don't do anything.
1266 if (mainconfig.proxy_requests == TRUE) {
1268 listen_socket_t *sock = NULL;
1271 * No sockets to receive packets, therefore
1272 * proxying is pointless.
1274 if (!*head) return -1;
1277 * If we previously had proxy sockets, copy them
1278 * to the new config.
1280 if (mainconfig.listen != NULL) {
1281 rad_listen_t *old, *next, **tail;
1283 tail = &mainconfig.listen;
1284 for (old = mainconfig.listen;
1289 if (old->type != RAD_LISTEN_PROXY) {
1290 tail = &((*tail)->next);
1297 last = &(old->next);
1304 * Find the first authentication port,
1307 for (this = *head; this != NULL; this = this->next) {
1308 if (this->type == RAD_LISTEN_AUTH) {
1310 if (server_ipaddr.af == AF_UNSPEC) {
1311 server_ipaddr = sock->ipaddr;
1313 port = sock->port + 2; /* skip acct port */
1316 if (this->type == RAD_LISTEN_VQP) {
1318 if (server_ipaddr.af == AF_UNSPEC) {
1319 server_ipaddr = sock->ipaddr;
1321 port = sock->port + 1;
1326 if (port < 0) port = 1024 + (lrad_rand() & 0x1ff);
1329 * Address is still unspecified, use IPv4.
1331 if (server_ipaddr.af == AF_UNSPEC) {
1332 server_ipaddr.af = AF_INET;
1333 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1336 this = listen_alloc(RAD_LISTEN_PROXY);
1340 * Create the first proxy socket.
1342 sock->ipaddr = server_ipaddr;
1345 * Try to find a proxy port (value doesn't matter)
1347 for (sock->port = port;
1350 if (listen_bind(this) == 0) {
1352 last = &(this->next); /* just in case */
1357 if (sock->port >= 64000) {
1360 radlog(L_ERR, "Failed to open socket for proxying");
1367 if (radius_snmp_init(config)) {
1369 * Forget about the old one.
1371 for (this = mainconfig.listen;
1373 this = this->next) {
1374 if (this->type != RAD_LISTEN_SNMP) continue;
1378 this = rad_malloc(sizeof(*this));
1379 memset(this, 0, sizeof(*this));
1381 this->type = RAD_LISTEN_SNMP;
1382 this->fd = rad_snmp.smux_fd;
1384 this->recv = radius_snmp_recv;
1385 this->print = radius_snmp_print;
1388 last = &(this->next);
1396 * Free a linked list of listeners;
1398 void listen_free(rad_listen_t **head)
1402 if (!head || !*head) return;
1406 rad_listen_t *next = this->next;
1409 * Other code may have eaten the FD.
1411 if (this->fd >= 0) close(this->fd);
1413 if (master_listen[this->type].free) {
1414 master_listen[this->type].free(this);