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;
83 if (!clients) clients = mainconfig.clients;
85 rad_assert(clients != NULL);
87 return client_find(clients, ipaddr);
90 static int listen_bind(rad_listen_t *this);
93 * Process and reply to a server-status request.
94 * Like rad_authenticate and rad_accounting this should
95 * live in it's own file but it's so small we don't bother.
97 static int rad_status_server(REQUEST *request)
99 int rcode = RLM_MODULE_OK;
102 switch (request->listener->type) {
103 case RAD_LISTEN_AUTH:
104 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
106 rcode = module_authorize(dval->value, request);
108 rcode = RLM_MODULE_OK;
113 case RLM_MODULE_UPDATED:
114 request->reply->code = PW_AUTHENTICATION_ACK;
117 case RLM_MODULE_FAIL:
118 case RLM_MODULE_HANDLED:
119 request->reply->code = 0; /* don't reply */
123 case RLM_MODULE_REJECT:
124 request->reply->code = PW_AUTHENTICATION_REJECT;
129 case RAD_LISTEN_ACCT:
130 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
132 rcode = module_accounting(dval->value, request);
134 rcode = RLM_MODULE_OK;
139 case RLM_MODULE_UPDATED:
140 request->reply->code = PW_ACCOUNTING_RESPONSE;
144 request->reply->code = 0; /* don't reply */
157 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
159 listen_socket_t *sock = this->data;
163 if ((sock->ipaddr.af == AF_INET) &&
164 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
167 ip_ntoh(&sock->ipaddr, ip_buf, sizeof(ip_buf));
170 switch (this->type) {
171 case RAD_LISTEN_AUTH:
172 name = "authentication";
175 case RAD_LISTEN_ACCT:
179 case RAD_LISTEN_PROXY:
195 return snprintf(buffer, bufsize, "%s address %s port %d",
196 name, ip_buf, sock->port);
199 return snprintf(buffer, bufsize, "%s address %s port %d as server %s",
200 name, ip_buf, sock->port, this->server);
205 * Parse an authentication or accounting socket.
207 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
211 lrad_ipaddr_t ipaddr;
212 listen_socket_t *sock = this->data;
213 const char *section_name = NULL;
214 CONF_SECTION *client_cs;
219 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
220 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
221 &ipaddr.ipaddr.ip4addr, NULL);
222 if (rcode < 0) return -1;
224 if (rcode == 0) { /* successfully parsed IPv4 */
227 } else { /* maybe IPv6? */
228 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
229 &ipaddr.ipaddr.ip6addr, NULL);
230 if (rcode < 0) return -1;
233 cf_log_err(cf_sectiontoitem(cs),
234 "No address specified in listen section");
237 ipaddr.af = AF_INET6;
240 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
242 if (rcode < 0) return -1;
244 sock->ipaddr = ipaddr;
245 sock->port = listen_port;
248 * And bind it to the port.
250 if (listen_bind(this) < 0) {
252 cf_log_err(cf_sectiontoitem(cs),
253 "Error binding to port for %s port %d",
254 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
260 * If we can bind to interfaces, do so,
263 if (cf_pair_find(cs, "interface")) {
264 #ifndef SO_BINDTODEVICE
265 cf_log_err(cf_sectiontoitem(cs),
266 "System does not support binding to interfaces. Delete this line from the configuration file.");
270 CONF_PAIR *cp = cf_pair_find(cs, "interface");
273 rad_assert(cp != NULL);
274 value = cf_pair_value(cp);
276 cf_log_err(cf_sectiontoitem(cs),
277 "No interface name given");
281 strcpy(ifreq.ifr_name, value);
283 if (setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
284 (char *)&ifreq, sizeof(ifreq)) < 0) {
285 cf_log_err(cf_sectiontoitem(cs),
286 "Failed binding to interface %s: %s",
287 value, strerror(errno));
289 } /* else it worked. */
294 * The more specific configurations are preferred to more
298 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
299 §ion_name, NULL);
300 if (rcode < 0) return -1; /* bad string */
303 * Explicit list given: use it.
305 client_cs = cf_section_find(section_name);
308 cf_log_err(cf_sectiontoitem(cs),
309 "Failed to find clients %s {...}",
313 } /* else there was no "clients = " entry. */
316 CONF_SECTION *server_cs;
318 server_cs = cf_section_sub_find_name2(mainconfig.config,
322 * Found a "server foo" section. If there are clients
326 (cf_section_sub_find(server_cs, "client") != NULL)) {
327 client_cs = server_cs;
332 * Still nothing. Look for global clients.
334 if (!client_cs) client_cs = mainconfig.config;
336 sock->clients = clients_parse_section(client_cs);
337 if (!sock->clients) {
338 cf_log_err(cf_sectiontoitem(cs),
339 "Failed to find any clients for this listen section");
347 * Send an authentication response packet
349 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
351 rad_assert(request->listener == listener);
352 rad_assert(listener->send == auth_socket_send);
354 return rad_send(request->reply, request->packet,
355 request->client->secret);
360 * Send an accounting response packet (or not)
362 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
364 rad_assert(request->listener == listener);
365 rad_assert(listener->send == acct_socket_send);
368 * Accounting reject's are silently dropped.
370 * We do it here to avoid polluting the rest of the
371 * code with this knowledge
373 if (request->reply->code == 0) return 0;
375 return rad_send(request->reply, request->packet,
376 request->client->secret);
381 * Send a packet to a home server.
383 * FIXME: have different code for proxy auth & acct!
385 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
387 listen_socket_t *sock = listener->data;
389 rad_assert(request->proxy_listener == listener);
390 rad_assert(listener->send == proxy_socket_send);
392 request->proxy->src_ipaddr = sock->ipaddr;
393 request->proxy->src_port = sock->port;
395 return rad_send(request->proxy, request->packet,
396 request->home_server->secret);
401 * Check if an incoming request is "ok"
403 * It takes packets, not requests. It sees if the packet looks
404 * OK. If so, it does a number of sanity checks on it.
406 static int auth_socket_recv(rad_listen_t *listener,
407 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
411 RADIUS_PACKET *packet;
412 RAD_REQUEST_FUNP fun = NULL;
415 lrad_ipaddr_t src_ipaddr;
417 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
418 if (rcode < 0) return 0;
420 RAD_SNMP_TYPE_INC(listener, total_requests);
422 if (rcode < 20) { /* AUTH_HDR_LEN */
423 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
427 if ((client = client_listener_find(listener,
428 &src_ipaddr)) == NULL) {
429 rad_recv_discard(listener->fd);
430 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
432 if (debug_flag > 0) {
435 listener->print(listener, name, sizeof(name));
438 * This is debugging rather than logging, so that
439 * DoS attacks don't affect us.
441 DEBUG("Ignoring request to %s from unknown client %s port %d",
443 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
444 buffer, sizeof(buffer)), src_port);
451 * Some sanity checks, based on the packet code.
454 case PW_AUTHENTICATION_REQUEST:
455 RAD_SNMP_CLIENT_INC(listener, client, requests);
456 fun = rad_authenticate;
459 case PW_STATUS_SERVER:
460 if (!mainconfig.status_server) {
461 rad_recv_discard(listener->fd);
462 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
463 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
464 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
467 fun = rad_status_server;
471 rad_recv_discard(listener->fd);
472 RAD_SNMP_INC(rad_snmp.auth.total_unknown_types);
473 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
475 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
476 code, client->shortname, src_port);
479 } /* switch over packet types */
482 * Now that we've sanity checked everything, receive the
485 packet = rad_recv(listener->fd);
487 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
488 radlog(L_ERR, "%s", librad_errstr);
492 if (!received_request(listener, packet, prequest, client)) {
493 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
494 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
505 * Receive packets from an accounting socket
507 static int acct_socket_recv(rad_listen_t *listener,
508 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
512 RADIUS_PACKET *packet;
513 RAD_REQUEST_FUNP fun = NULL;
516 lrad_ipaddr_t src_ipaddr;
518 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
519 if (rcode < 0) return 0;
521 RAD_SNMP_TYPE_INC(listener, total_requests);
523 if (rcode < 20) { /* AUTH_HDR_LEN */
524 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
528 if ((client = client_listener_find(listener,
529 &src_ipaddr)) == NULL) {
530 rad_recv_discard(listener->fd);
531 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
534 * This is debugging rather than logging, so that
535 * DoS attacks don't affect us.
537 if (debug_flag > 0) {
540 listener->print(listener, name, sizeof(name));
542 DEBUG("Ignoring request to %s from unknown client %s port %d",
544 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
545 buffer, sizeof(buffer)), src_port);
552 * Some sanity checks, based on the packet code.
555 case PW_ACCOUNTING_REQUEST:
556 RAD_SNMP_CLIENT_INC(listener, client, requests);
557 fun = rad_accounting;
560 case PW_STATUS_SERVER:
561 if (!mainconfig.status_server) {
562 rad_recv_discard(listener->fd);
563 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
564 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
566 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
569 fun = rad_status_server;
573 rad_recv_discard(listener->fd);
574 RAD_SNMP_TYPE_INC(listener, total_unknown_types);
575 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
577 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
578 code, client->shortname, src_port);
580 } /* switch over packet types */
583 * Now that we've sanity checked everything, receive the
586 packet = rad_recv(listener->fd);
588 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
589 radlog(L_ERR, "%s", librad_errstr);
594 * There can be no duplicate accounting packets.
596 if (!received_request(listener, packet, prequest, client)) {
597 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
598 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
609 * Recieve packets from a proxy socket.
611 static int proxy_socket_recv(rad_listen_t *listener,
612 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
615 RADIUS_PACKET *packet;
616 RAD_REQUEST_FUNP fun = NULL;
619 packet = rad_recv(listener->fd);
621 radlog(L_ERR, "%s", librad_errstr);
626 * FIXME: Client MIB updates?
628 switch(packet->code) {
629 case PW_AUTHENTICATION_ACK:
630 case PW_ACCESS_CHALLENGE:
631 case PW_AUTHENTICATION_REJECT:
632 fun = rad_authenticate;
635 case PW_ACCOUNTING_RESPONSE:
636 fun = rad_accounting;
641 * FIXME: Update MIB for packet types?
643 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
644 "from home server %s port %d - ID %d : IGNORED",
646 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
647 packet->src_port, packet->id);
652 request = received_proxy_response(packet);
657 rad_assert(fun != NULL);
665 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
667 if (!request->reply->code) return 0;
669 rad_encode(request->reply, request->packet,
670 request->client->secret);
671 rad_sign(request->reply, request->packet,
672 request->client->secret);
678 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
680 if (rad_verify(request->packet, NULL,
681 request->client->secret) < 0) {
685 return rad_decode(request->packet, NULL,
686 request->client->secret);
689 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
691 rad_encode(request->proxy, NULL, request->home_server->secret);
692 rad_sign(request->proxy, NULL, request->home_server->secret);
698 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
700 if (rad_verify(request->proxy_reply, request->proxy,
701 request->home_server->secret) < 0) {
705 return rad_decode(request->proxy_reply, request->proxy,
706 request->home_server->secret);
711 static int radius_snmp_recv(rad_listen_t *listener,
712 UNUSED RAD_REQUEST_FUNP *pfun,
713 UNUSED REQUEST **prequest)
715 if ((rad_snmp.smux_fd >= 0) &&
716 (rad_snmp.smux_event == SMUX_READ)) {
721 * If we've got to re-connect, then do so now,
722 * before calling select again.
724 if (rad_snmp.smux_event == SMUX_CONNECT) {
729 * Reset this every time, as the smux connect may have
730 * opened a new socket.
732 listener->fd = rad_snmp.smux_fd;
738 static int radius_snmp_print(UNUSED rad_listen_t *this, char *buffer, size_t bufsize)
740 return snprintf(buffer, bufsize, "SMUX with OID .1.3.6.1.4.1.11344.1.1.1");
745 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
746 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
750 proxy_socket_recv, proxy_socket_send,
751 socket_print, proxy_socket_encode, proxy_socket_decode },
754 { common_socket_parse, NULL,
755 auth_socket_recv, auth_socket_send,
756 socket_print, client_socket_encode, client_socket_decode },
759 { common_socket_parse, NULL,
760 acct_socket_recv, acct_socket_send,
761 socket_print, client_socket_encode, client_socket_decode},
764 { detail_parse, detail_free,
765 detail_recv, detail_send,
766 detail_print, detail_encode, detail_decode },
769 /* vlan query protocol */
770 { common_socket_parse, NULL,
771 vqp_socket_recv, vqp_socket_send,
772 socket_print, vqp_socket_encode, vqp_socket_decode },
774 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
777 { NULL, NULL, NULL, NULL, NULL, NULL, NULL} /* RAD_LISTEN_SNMP */
783 * Binds a listener to a socket.
785 static int listen_bind(rad_listen_t *this)
788 listen_socket_t *sock = this->data;
791 * If the port is zero, then it means the appropriate
792 * thing from /etc/services.
794 if (sock->port == 0) {
797 switch (this->type) {
798 case RAD_LISTEN_AUTH:
799 svp = getservbyname ("radius", "udp");
801 sock->port = ntohs(svp->s_port);
803 sock->port = PW_AUTH_UDP_PORT;
807 case RAD_LISTEN_ACCT:
808 svp = getservbyname ("radacct", "udp");
810 sock->port = ntohs(svp->s_port);
812 sock->port = PW_ACCT_UDP_PORT;
823 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
829 * Find it in the old list, AFTER updating the port. If
830 * it's there, use that, rather than creating a new
831 * socket. This allows HUP's to re-use the old sockets,
832 * which means that packets waiting in the socket queue
835 for (last = &mainconfig.listen;
837 last = &((*last)->next)) {
838 listen_socket_t *other;
840 if (this->type != (*last)->type) continue;
842 if ((this->type == RAD_LISTEN_DETAIL) ||
843 (this->type == RAD_LISTEN_SNMP)) continue;
845 other = (listen_socket_t *)((*last)->data);
847 if ((sock->port == other->port) &&
848 (sock->ipaddr.af == other->ipaddr.af) &&
849 (lrad_ipaddr_cmp(&sock->ipaddr, &other->ipaddr) == 0)) {
850 this->fd = (*last)->fd;
856 this->fd = lrad_socket(&sock->ipaddr, sock->port);
858 radlog(L_ERR, "ERROR: Failed to open socket: %s",
864 struct sockaddr_storage src;
865 socklen_t sizeof_src = sizeof(src);
867 memset(&src, 0, sizeof_src);
868 if (getsockname(this->fd, (struct sockaddr *) &src,
873 if (src.ss_family == AF_INET) {
874 struct sockaddr_in *s4;
876 s4 = (struct sockaddr_in *)&src;
877 sock->ipaddr.ipaddr.ip4addr = s4->sin_addr;
879 #ifdef HAVE_STRUCT_SOCKADDR_IN6
880 } else if (src.ss_family == AF_INET6) {
881 struct sockaddr_in6 *s6;
883 s6 = (struct sockaddr_in6 *)&src;
884 sock->ipaddr.ipaddr.ip6addr = s6->sin6_addr;
893 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
894 radlog(L_ERR, "Failure in fcntl: %s)\n", strerror(errno));
899 if( fcntl(this->fd, F_SETFL, flags) < 0) {
900 radlog(L_ERR, "Failure in fcntl: %s)\n", strerror(errno));
911 * Allocate & initialize a new listener.
913 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
917 this = rad_malloc(sizeof(*this));
918 memset(this, 0, sizeof(*this));
921 this->recv = master_listen[this->type].recv;
922 this->send = master_listen[this->type].send;
923 this->print = master_listen[this->type].print;
924 this->encode = master_listen[this->type].encode;
925 this->decode = master_listen[this->type].decode;
928 case RAD_LISTEN_AUTH:
929 case RAD_LISTEN_ACCT:
930 case RAD_LISTEN_PROXY:
932 this->data = rad_malloc(sizeof(listen_socket_t));
933 memset(this->data, 0, sizeof(listen_socket_t));
936 case RAD_LISTEN_DETAIL:
941 rad_assert("Unsupported option!" == NULL);
950 * Externally visible function for creating a new proxy LISTENER.
952 * For now, don't take ipaddr or port.
954 * Not thread-safe, but all calls to it are protected by the
955 * proxy mutex in request_list.c
957 rad_listen_t *proxy_new_listener()
959 int last_proxy_port, port;
960 rad_listen_t *this, *tmp, **last;
961 listen_socket_t *sock, *old;
963 this = listen_alloc(RAD_LISTEN_PROXY);
966 * Find an existing proxy socket to copy.
968 * FIXME: Make it per-realm, or per-home server!
972 last = &mainconfig.listen;
973 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
974 if (tmp->type == RAD_LISTEN_PROXY) {
976 if (sock->port > last_proxy_port) {
977 last_proxy_port = sock->port + 1;
979 if (!old) old = sock;
985 if (!old) return NULL; /* This is a serious error. */
988 * FIXME: find a new IP address to listen on?
990 * This could likely be done in the "home server"
991 * configuration, to have per-home-server source IP's.
994 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
997 * Keep going until we find an unused port.
999 for (port = last_proxy_port; port < 64000; port++) {
1001 if (listen_bind(this) == 0) {
1003 * Add the new listener to the list of
1015 static const LRAD_NAME_NUMBER listen_compare[] = {
1016 { "auth", RAD_LISTEN_AUTH },
1017 { "acct", RAD_LISTEN_ACCT },
1018 { "detail", RAD_LISTEN_DETAIL },
1020 { "vmps", RAD_LISTEN_VQP },
1026 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1034 DEBUG2(" listen {");
1036 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1038 if (rcode < 0) return NULL;
1041 cf_log_err(cf_sectiontoitem(cs),
1042 "No type specified in listen section");
1046 type = lrad_str2int(listen_compare, listen_type,
1048 if (type == RAD_LISTEN_NONE) {
1049 cf_log_err(cf_sectiontoitem(cs),
1050 "Invalid type \"%s\" in listen section.",
1058 * Allow listen sections in the default config to
1059 * refer to a server.
1062 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1064 if (rcode < 0) return NULL;
1068 * Set up cross-type data.
1070 this = listen_alloc(type);
1071 this->server = server;
1075 * Call per-type parser.
1077 if (master_listen[type].parse(cs, this) < 0) {
1088 * Generate a list of listeners. Takes an input list of
1089 * listeners, too, so we don't close sockets with waiting packets.
1091 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1095 rad_listen_t **last;
1097 lrad_ipaddr_t server_ipaddr;
1101 * We shouldn't be called with a pre-existing list.
1103 rad_assert(head && (*head == NULL));
1106 server_ipaddr.af = AF_UNSPEC;
1109 * If the port is specified on the command-line,
1110 * it over-rides the configuration file.
1112 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1114 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1117 * If the IP address was configured on the command-line,
1118 * use that as the "bind_address"
1120 if (mainconfig.myip.af != AF_UNSPEC) {
1121 memcpy(&server_ipaddr, &mainconfig.myip,
1122 sizeof(server_ipaddr));
1127 * Else look for bind_address and/or listen sections.
1129 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1130 rcode = cf_item_parse(config, "bind_address",
1132 &server_ipaddr.ipaddr.ip4addr, NULL);
1133 if (rcode < 0) return -1; /* error parsing it */
1135 if (rcode == 0) { /* successfully parsed IPv4 */
1136 listen_socket_t *sock;
1137 server_ipaddr.af = AF_INET;
1139 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'.");
1143 if (strcmp(progname, "vmpsd") == 0) {
1144 this = listen_alloc(RAD_LISTEN_VQP);
1145 if (!auth_port) auth_port = 1589;
1148 this = listen_alloc(RAD_LISTEN_AUTH);
1152 sock->ipaddr = server_ipaddr;
1153 sock->port = auth_port;
1155 if (listen_bind(this) < 0) {
1158 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1161 auth_port = sock->port; /* may have been updated in listen_bind */
1163 last = &(this->next);
1169 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
1173 * Open Accounting Socket.
1175 * If we haven't already gotten acct_port from
1176 * /etc/services, then make it auth_port + 1.
1178 this = listen_alloc(RAD_LISTEN_ACCT);
1182 * Create the accounting socket.
1184 * The accounting port is always the
1185 * authentication port + 1
1187 sock->ipaddr = server_ipaddr;
1188 sock->port = auth_port + 1;
1190 if (listen_bind(this) < 0) {
1193 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1198 last = &(this->next);
1200 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1201 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1207 * They specified an IP on the command-line, ignore
1208 * all listen sections.
1210 if (mainconfig.myip.af != AF_UNSPEC) goto do_proxy;
1213 * Walk through the "listen" sections, if they exist.
1215 for (cs = cf_subsection_find_next(config, NULL, "listen");
1217 cs = cf_subsection_find_next(config, cs, "listen")) {
1218 this = listen_parse(cs, NULL);
1225 last = &(this->next);
1229 * Check virtual servers for "listen" sections, too.
1231 * FIXME: Move to virtual server init?
1233 for (cs = cf_subsection_find_next(config, NULL, "server");
1235 cs = cf_subsection_find_next(config, cs, "server")) {
1236 CONF_SECTION *subcs;
1237 const char *name2 = cf_section_name2(cs);
1239 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1241 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1242 this = listen_parse(subcs, name2);
1249 last = &(this->next);
1250 } /* loop over "listen" directives in virtual servers */
1251 } /* loop over virtual servers */
1254 * If we're proxying requests, open the proxy FD.
1255 * Otherwise, don't do anything.
1258 if (mainconfig.proxy_requests == TRUE) {
1260 listen_socket_t *sock = NULL;
1263 * No sockets to receive packets, therefore
1264 * proxying is pointless.
1266 if (!*head) return -1;
1269 * If we previously had proxy sockets, copy them
1270 * to the new config.
1272 if (mainconfig.listen != NULL) {
1273 rad_listen_t *old, *next, **tail;
1275 tail = &mainconfig.listen;
1276 for (old = mainconfig.listen;
1281 if (old->type != RAD_LISTEN_PROXY) {
1282 tail = &((*tail)->next);
1289 last = &(old->next);
1296 * Find the first authentication port,
1299 for (this = *head; this != NULL; this = this->next) {
1300 if (this->type == RAD_LISTEN_AUTH) {
1302 if (server_ipaddr.af == AF_UNSPEC) {
1303 server_ipaddr = sock->ipaddr;
1305 port = sock->port + 2; /* skip acct port */
1308 if (this->type == RAD_LISTEN_VQP) {
1310 if (server_ipaddr.af == AF_UNSPEC) {
1311 server_ipaddr = sock->ipaddr;
1313 port = sock->port + 1;
1318 if (port < 0) port = 1024 + (lrad_rand() & 0x1ff);
1321 * Address is still unspecified, use IPv4.
1323 if (server_ipaddr.af == AF_UNSPEC) {
1324 server_ipaddr.af = AF_INET;
1325 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1328 this = listen_alloc(RAD_LISTEN_PROXY);
1332 * Create the first proxy socket.
1334 sock->ipaddr = server_ipaddr;
1337 * Try to find a proxy port (value doesn't matter)
1339 for (sock->port = port;
1342 if (listen_bind(this) == 0) {
1344 last = &(this->next); /* just in case */
1349 if (sock->port >= 64000) {
1352 radlog(L_ERR, "Failed to open socket for proxying");
1359 if (radius_snmp_init(config)) {
1361 * Forget about the old one.
1363 for (this = mainconfig.listen;
1365 this = this->next) {
1366 if (this->type != RAD_LISTEN_SNMP) continue;
1370 this = rad_malloc(sizeof(*this));
1371 memset(this, 0, sizeof(*this));
1373 this->type = RAD_LISTEN_SNMP;
1374 this->fd = rad_snmp.smux_fd;
1376 this->recv = radius_snmp_recv;
1377 this->print = radius_snmp_print;
1380 last = &(this->next);
1388 * Free a linked list of listeners;
1390 void listen_free(rad_listen_t **head)
1394 if (!head || !*head) return;
1398 rad_listen_t *next = this->next;
1401 * Other code may have eaten the FD.
1403 if (this->fd >= 0) close(this->fd);
1405 if (master_listen[this->type].free) {
1406 master_listen[this->type].free(this);