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 #ifdef SO_BINDTODEVICE
73 const char *interface;
75 RADCLIENT_LIST *clients;
79 * Find a per-socket client.
81 RADCLIENT *client_listener_find(const rad_listen_t *listener,
82 const fr_ipaddr_t *ipaddr)
84 #ifdef WITH_DYNAMIC_CLIENTS
87 listen_socket_t *sock;
89 RADCLIENT *client, *created;
91 RADCLIENT_LIST *clients;
93 rad_assert(listener != NULL);
94 rad_assert(ipaddr != NULL);
96 rad_assert((listener->type == RAD_LISTEN_AUTH) ||
98 (listener->type == RAD_LISTEN_NONE) ||
100 #ifdef WITH_ACCOUNTING
101 (listener->type == RAD_LISTEN_ACCT) ||
104 (listener->type == RAD_LISTEN_VQP) ||
106 (listener->type == RAD_LISTEN_DHCP));
108 clients = ((listen_socket_t *)listener->data)->clients;
111 * This HAS to have been initialized previously.
113 rad_assert(clients != NULL);
115 #ifdef WITH_DYNAMIC_CLIENTS
116 client = client_find(clients, ipaddr);
117 if (!client) return NULL;
120 * No server defined, and it's not dynamic. Return it.
122 if (!client->client_server && !client->dynamic) return client;
127 * It's a dynamically generated client, check it.
129 if (client->dynamic) {
131 * Lives forever. Return it.
133 if (client->lifetime == 0) return client;
136 * Rate-limit the deletion of known clients.
137 * This makes them last a little longer, but
138 * prevents the server from melting down if (say)
139 * 10k clients all expire at once.
141 if (now == client->last_new_client) return client;
144 * It's not dead yet. Return it.
146 if ((client->created + client->lifetime) > now) return client;
149 * It's dead, Jim. Delete it.
151 client_delete(clients, client);
154 * Go find the enclosing network again.
156 client = client_find(clients, ipaddr);
161 if (!client) return NULL;
162 if (!client->client_server) return NULL;
165 * At this point, 'client' is the enclosing
166 * network that configures where dynamic clients
169 rad_assert(client->dynamic == 0);
172 * The IP is unknown, so we've found an enclosing
173 * network. Enable DoS protection. We only
174 * allow one new client per second. Known
175 * clients aren't subject to this restriction.
177 if (now == client->last_new_client) return NULL;
180 client->last_new_client = now;
182 request = request_alloc();
183 if (!request) return NULL;
185 request->listener = listener;
186 request->client = client;
187 request->packet = rad_alloc(0);
188 if (!request->packet) {
189 request_free(&request);
192 request->reply = rad_alloc(0);
193 if (!request->reply) {
194 request_free(&request);
197 request->packet->timestamp = request->timestamp;
199 request->priority = listener->type;
200 request->server = client->client_server;
201 request->root = &mainconfig;
204 * Run a fake request through the given virtual server.
205 * Look for FreeRADIUS-Client-IP-Address
206 * FreeRADIUS-Client-Secret
209 * and create the RADCLIENT structure from that.
212 sock = listener->data;
213 request->packet->sockfd = listener->fd;
214 request->packet->src_ipaddr = *ipaddr;
215 request->packet->src_port = 0; /* who cares... */
216 request->packet->dst_ipaddr = sock->ipaddr;
217 request->packet->dst_port = sock->port;
219 request->reply->sockfd = request->packet->sockfd;
220 request->reply->dst_ipaddr = request->packet->src_ipaddr;
221 request->reply->src_ipaddr = request->packet->dst_ipaddr;
222 request->reply->dst_port = request->packet->src_port;
223 request->reply->src_port = request->packet->dst_port;
224 request->reply->id = request->packet->id;
225 request->reply->code = 0; /* UNKNOWN code */
228 DEBUG("server %s {", request->server);
230 rcode = module_authorize(0, request);
232 DEBUG("} # server %s", request->server);
234 if (rcode != RLM_MODULE_OK) {
235 request_free(&request);
239 created = client_create(clients, request);
241 request_free(&request);
243 return created; /* may be NULL */
245 return client_find(clients, ipaddr);
249 static int listen_bind(rad_listen_t *this);
253 * Process and reply to a server-status request.
254 * Like rad_authenticate and rad_accounting this should
255 * live in it's own file but it's so small we don't bother.
257 static int rad_status_server(REQUEST *request)
259 int rcode = RLM_MODULE_OK;
262 switch (request->listener->type) {
264 case RAD_LISTEN_NONE:
266 case RAD_LISTEN_AUTH:
267 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
269 rcode = module_authorize(dval->value, request);
271 rcode = RLM_MODULE_OK;
276 case RLM_MODULE_UPDATED:
277 request->reply->code = PW_AUTHENTICATION_ACK;
280 case RLM_MODULE_FAIL:
281 case RLM_MODULE_HANDLED:
282 request->reply->code = 0; /* don't reply */
286 case RLM_MODULE_REJECT:
287 request->reply->code = PW_AUTHENTICATION_REJECT;
292 #ifdef WITH_ACCOUNTING
293 case RAD_LISTEN_ACCT:
294 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
296 rcode = module_accounting(dval->value, request);
298 rcode = RLM_MODULE_OK;
303 case RLM_MODULE_UPDATED:
304 request->reply->code = PW_ACCOUNTING_RESPONSE;
308 request->reply->code = 0; /* don't reply */
319 request_stats_reply(request);
326 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
329 listen_socket_t *sock = this->data;
332 switch (this->type) {
334 case RAD_LISTEN_NONE: /* what a hack... */
339 case RAD_LISTEN_AUTH:
340 name = "authentication";
343 #ifdef WITH_ACCOUNTING
344 case RAD_LISTEN_ACCT:
350 case RAD_LISTEN_PROXY:
362 case RAD_LISTEN_DHCP:
372 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
373 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
377 #ifdef SO_BINDTODEVICE
378 if (sock->interface) {
379 ADDSTRING(" interface ");
380 ADDSTRING(sock->interface);
384 ADDSTRING(" address ");
386 if ((sock->ipaddr.af == AF_INET) &&
387 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
388 strlcpy(buffer, "*", bufsize);
390 ip_ntoh(&sock->ipaddr, buffer, bufsize);
395 snprintf(buffer, bufsize, "%d", sock->port);
399 ADDSTRING(" as server ");
400 ADDSTRING(this->server);
411 * Parse an authentication or accounting socket.
413 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
418 listen_socket_t *sock = this->data;
419 char *section_name = NULL;
420 CONF_SECTION *client_cs, *parentcs;
425 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
426 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
427 &ipaddr.ipaddr.ip4addr, NULL);
428 if (rcode < 0) return -1;
430 if (rcode == 0) { /* successfully parsed IPv4 */
433 } else { /* maybe IPv6? */
434 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
435 &ipaddr.ipaddr.ip6addr, NULL);
436 if (rcode < 0) return -1;
439 cf_log_err(cf_sectiontoitem(cs),
440 "No address specified in listen section");
443 ipaddr.af = AF_INET6;
446 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
448 if (rcode < 0) return -1;
450 if ((listen_port < 0) || (listen_port > 65500)) {
451 cf_log_err(cf_sectiontoitem(cs),
452 "Invalid value for \"port\"");
456 sock->ipaddr = ipaddr;
457 sock->port = listen_port;
460 * If we can bind to interfaces, do so,
463 if (cf_pair_find(cs, "interface")) {
464 #ifndef SO_BINDTODEVICE
465 cf_log_err(cf_sectiontoitem(cs),
466 "System does not support binding to interfaces. Delete this line from the configuration file.");
470 CONF_PAIR *cp = cf_pair_find(cs, "interface");
472 rad_assert(cp != NULL);
473 value = cf_pair_value(cp);
475 cf_log_err(cf_sectiontoitem(cs),
476 "No interface name given");
479 sock->interface = value;
484 * And bind it to the port.
486 if (listen_bind(this) < 0) {
488 cf_log_err(cf_sectiontoitem(cs),
489 "Error binding to port for %s port %d",
490 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
497 * Proxy sockets don't have clients.
499 if (this->type == RAD_LISTEN_PROXY) return 0;
503 * The more specific configurations are preferred to more
507 parentcs = cf_top_section(cs);
508 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
509 §ion_name, NULL);
510 if (rcode < 0) return -1; /* bad string */
513 * Explicit list given: use it.
515 client_cs = cf_section_sub_find_name2(parentcs,
519 client_cs = cf_section_find(section_name);
522 cf_log_err(cf_sectiontoitem(cs),
523 "Failed to find clients %s {...}",
529 } /* else there was no "clients = " entry. */
532 CONF_SECTION *server_cs;
534 server_cs = cf_section_sub_find_name2(parentcs,
538 * Found a "server foo" section. If there are clients
542 (cf_section_sub_find(server_cs, "client") != NULL)) {
543 client_cs = server_cs;
548 * Still nothing. Look for global clients.
550 if (!client_cs) client_cs = parentcs;
552 sock->clients = clients_parse_section(client_cs);
553 if (!sock->clients) {
554 cf_log_err(cf_sectiontoitem(cs),
555 "Failed to find any clients for this listen section");
563 * Send an authentication response packet
565 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
567 rad_assert(request->listener == listener);
568 rad_assert(listener->send == auth_socket_send);
570 return rad_send(request->reply, request->packet,
571 request->client->secret);
575 #ifdef WITH_ACCOUNTING
577 * Send an accounting response packet (or not)
579 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
581 rad_assert(request->listener == listener);
582 rad_assert(listener->send == acct_socket_send);
585 * Accounting reject's are silently dropped.
587 * We do it here to avoid polluting the rest of the
588 * code with this knowledge
590 if (request->reply->code == 0) return 0;
592 return rad_send(request->reply, request->packet,
593 request->client->secret);
599 * Send a packet to a home server.
601 * FIXME: have different code for proxy auth & acct!
603 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
605 listen_socket_t *sock = listener->data;
607 rad_assert(request->proxy_listener == listener);
608 rad_assert(listener->send == proxy_socket_send);
610 request->proxy->src_ipaddr = sock->ipaddr;
611 request->proxy->src_port = sock->port;
613 return rad_send(request->proxy, request->packet,
614 request->home_server->secret);
620 * Check if an incoming request is "ok"
622 * It takes packets, not requests. It sees if the packet looks
623 * OK. If so, it does a number of sanity checks on it.
625 static int stats_socket_recv(rad_listen_t *listener,
626 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
630 RADIUS_PACKET *packet;
633 fr_ipaddr_t src_ipaddr;
635 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
636 if (rcode < 0) return 0;
638 RAD_STATS_TYPE_INC(listener, total_requests);
640 if (rcode < 20) { /* AUTH_HDR_LEN */
641 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
646 * We only understand Status-Server on this socket.
648 if (code != PW_STATUS_SERVER) {
649 DEBUG("Ignoring packet code %d sent to Status-Server port",
651 rad_recv_discard(listener->fd);
652 RAD_STATS_TYPE_INC(listener, total_unknown_types);
653 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
657 if ((client = client_listener_find(listener,
658 &src_ipaddr)) == NULL) {
659 rad_recv_discard(listener->fd);
660 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
662 if (debug_flag > 0) {
665 listener->print(listener, name, sizeof(name));
668 * This is debugging rather than logging, so that
669 * DoS attacks don't affect us.
671 DEBUG("Ignoring request to %s from unknown client %s port %d",
673 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
674 buffer, sizeof(buffer)), src_port);
681 * Now that we've sanity checked everything, receive the
684 packet = rad_recv(listener->fd, 1); /* require message authenticator */
686 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
687 DEBUG("%s", librad_errstr);
691 if (!received_request(listener, packet, prequest, client)) {
692 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
693 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
698 *pfun = rad_status_server;
705 * Check if an incoming request is "ok"
707 * It takes packets, not requests. It sees if the packet looks
708 * OK. If so, it does a number of sanity checks on it.
710 static int auth_socket_recv(rad_listen_t *listener,
711 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
715 RADIUS_PACKET *packet;
716 RAD_REQUEST_FUNP fun = NULL;
719 fr_ipaddr_t src_ipaddr;
721 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
722 if (rcode < 0) return 0;
724 RAD_STATS_TYPE_INC(listener, total_requests);
726 if (rcode < 20) { /* AUTH_HDR_LEN */
727 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
731 if ((client = client_listener_find(listener,
732 &src_ipaddr)) == NULL) {
733 rad_recv_discard(listener->fd);
734 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
736 if (debug_flag > 0) {
739 listener->print(listener, name, sizeof(name));
742 * This is debugging rather than logging, so that
743 * DoS attacks don't affect us.
745 DEBUG("Ignoring request to %s from unknown client %s port %d",
747 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
748 buffer, sizeof(buffer)), src_port);
755 * Some sanity checks, based on the packet code.
758 case PW_AUTHENTICATION_REQUEST:
759 RAD_STATS_CLIENT_INC(listener, client, total_requests);
760 fun = rad_authenticate;
763 case PW_STATUS_SERVER:
764 if (!mainconfig.status_server) {
765 rad_recv_discard(listener->fd);
766 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
767 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
768 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
771 fun = rad_status_server;
775 rad_recv_discard(listener->fd);
776 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
777 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
779 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
780 code, client->shortname, src_port);
783 } /* switch over packet types */
786 * Now that we've sanity checked everything, receive the
789 packet = rad_recv(listener->fd, client->message_authenticator);
791 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
792 DEBUG("%s", librad_errstr);
796 if (!received_request(listener, packet, prequest, client)) {
797 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
798 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
808 #ifdef WITH_ACCOUNTING
810 * Receive packets from an accounting socket
812 static int acct_socket_recv(rad_listen_t *listener,
813 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
817 RADIUS_PACKET *packet;
818 RAD_REQUEST_FUNP fun = NULL;
821 fr_ipaddr_t src_ipaddr;
823 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
824 if (rcode < 0) return 0;
826 RAD_STATS_TYPE_INC(listener, total_requests);
828 if (rcode < 20) { /* AUTH_HDR_LEN */
829 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
833 if ((client = client_listener_find(listener,
834 &src_ipaddr)) == NULL) {
835 rad_recv_discard(listener->fd);
836 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
839 * This is debugging rather than logging, so that
840 * DoS attacks don't affect us.
842 if (debug_flag > 0) {
845 listener->print(listener, name, sizeof(name));
847 DEBUG("Ignoring request to %s from unknown client %s port %d",
849 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
850 buffer, sizeof(buffer)), src_port);
857 * Some sanity checks, based on the packet code.
860 case PW_ACCOUNTING_REQUEST:
861 RAD_STATS_CLIENT_INC(listener, client, total_requests);
862 fun = rad_accounting;
865 case PW_STATUS_SERVER:
866 if (!mainconfig.status_server) {
867 rad_recv_discard(listener->fd);
868 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
869 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
871 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
874 fun = rad_status_server;
878 rad_recv_discard(listener->fd);
879 RAD_STATS_TYPE_INC(listener, total_unknown_types);
880 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
882 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
883 code, client->shortname, src_port);
885 } /* switch over packet types */
888 * Now that we've sanity checked everything, receive the
891 packet = rad_recv(listener->fd, 0);
893 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
894 radlog(L_ERR, "%s", librad_errstr);
899 * There can be no duplicate accounting packets.
901 if (!received_request(listener, packet, prequest, client)) {
902 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
903 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
915 * Recieve packets from a proxy socket.
917 static int proxy_socket_recv(rad_listen_t *listener,
918 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
921 RADIUS_PACKET *packet;
922 RAD_REQUEST_FUNP fun = NULL;
925 packet = rad_recv(listener->fd, 0);
927 radlog(L_ERR, "%s", librad_errstr);
932 * FIXME: Client MIB updates?
934 switch(packet->code) {
935 case PW_AUTHENTICATION_ACK:
936 case PW_ACCESS_CHALLENGE:
937 case PW_AUTHENTICATION_REJECT:
938 fun = rad_authenticate;
941 #ifdef WITH_ACCOUNTING
942 case PW_ACCOUNTING_RESPONSE:
943 fun = rad_accounting;
949 * FIXME: Update MIB for packet types?
951 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
952 "from home server %s port %d - ID %d : IGNORED",
954 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
955 packet->src_port, packet->id);
960 request = received_proxy_response(packet);
965 rad_assert(fun != NULL);
974 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
976 if (!request->reply->code) return 0;
978 rad_encode(request->reply, request->packet,
979 request->client->secret);
980 rad_sign(request->reply, request->packet,
981 request->client->secret);
987 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
989 if (rad_verify(request->packet, NULL,
990 request->client->secret) < 0) {
994 return rad_decode(request->packet, NULL,
995 request->client->secret);
999 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1001 rad_encode(request->proxy, NULL, request->home_server->secret);
1002 rad_sign(request->proxy, NULL, request->home_server->secret);
1008 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1010 if (rad_verify(request->proxy_reply, request->proxy,
1011 request->home_server->secret) < 0) {
1015 return rad_decode(request->proxy_reply, request->proxy,
1016 request->home_server->secret);
1021 static int radius_snmp_recv(rad_listen_t *listener,
1022 UNUSED RAD_REQUEST_FUNP *pfun,
1023 UNUSED REQUEST **prequest)
1025 if ((rad_snmp.smux_fd >= 0) &&
1026 (rad_snmp.smux_event == SMUX_READ)) {
1031 * If we've got to re-connect, then do so now,
1032 * before calling select again.
1034 if (rad_snmp.smux_event == SMUX_CONNECT) {
1039 * Reset this every time, as the smux connect may have
1040 * opened a new socket.
1042 listener->fd = rad_snmp.smux_fd;
1048 static int radius_snmp_print(UNUSED rad_listen_t *this, char *buffer, size_t bufsize)
1050 return snprintf(buffer, bufsize, "SMUX with OID .1.3.6.1.4.1.11344.1.1.1");
1057 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1059 { common_socket_parse, NULL,
1060 stats_socket_recv, auth_socket_send,
1061 socket_print, client_socket_encode, client_socket_decode },
1063 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1068 { common_socket_parse, NULL,
1069 proxy_socket_recv, proxy_socket_send,
1070 socket_print, proxy_socket_encode, proxy_socket_decode },
1072 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1075 /* authentication */
1076 { common_socket_parse, NULL,
1077 auth_socket_recv, auth_socket_send,
1078 socket_print, client_socket_encode, client_socket_decode },
1080 #ifdef WITH_ACCOUNTING
1082 { common_socket_parse, NULL,
1083 acct_socket_recv, acct_socket_send,
1084 socket_print, client_socket_encode, client_socket_decode},
1086 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1091 { detail_parse, detail_free,
1092 detail_recv, detail_send,
1093 detail_print, detail_encode, detail_decode },
1095 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1099 /* vlan query protocol */
1100 { common_socket_parse, NULL,
1101 vqp_socket_recv, vqp_socket_send,
1102 socket_print, vqp_socket_encode, vqp_socket_decode },
1104 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1108 /* dhcp query protocol */
1109 { dhcp_socket_parse, NULL,
1110 dhcp_socket_recv, dhcp_socket_send,
1111 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1113 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
1116 { NULL, NULL, NULL, NULL, NULL, NULL, NULL} /* RAD_LISTEN_SNMP */
1122 * Binds a listener to a socket.
1124 static int listen_bind(rad_listen_t *this)
1126 struct sockaddr_storage salocal;
1128 listen_socket_t *sock = this->data;
1131 * If the port is zero, then it means the appropriate
1132 * thing from /etc/services.
1134 if (sock->port == 0) {
1135 struct servent *svp;
1137 switch (this->type) {
1138 case RAD_LISTEN_AUTH:
1139 svp = getservbyname ("radius", "udp");
1141 sock->port = ntohs(svp->s_port);
1143 sock->port = PW_AUTH_UDP_PORT;
1147 #ifdef WITH_ACCOUNTING
1148 case RAD_LISTEN_ACCT:
1149 svp = getservbyname ("radacct", "udp");
1151 sock->port = ntohs(svp->s_port);
1153 sock->port = PW_ACCT_UDP_PORT;
1159 case RAD_LISTEN_PROXY:
1165 case RAD_LISTEN_VQP:
1171 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1177 * Copy fr_socket() here, as we may need to bind to a device.
1179 this->fd = socket(sock->ipaddr.af, SOCK_DGRAM, 0);
1181 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
1185 #ifdef SO_BINDTODEVICE
1187 * Bind to a device BEFORE touching IP addresses.
1189 if (sock->interface) {
1191 strcpy(ifreq.ifr_name, sock->interface);
1193 if (setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1194 (char *)&ifreq, sizeof(ifreq)) < 0) {
1196 radlog(L_ERR, "Failed opening to interface %s: %s",
1197 sock->interface, strerror(errno));
1199 } /* else it worked. */
1203 #ifdef WITH_UDPFROMTO
1205 * Initialize udpfromto for all sockets.
1207 if (udpfromto_init(this->fd) != 0) {
1214 * Set up sockaddr stuff.
1216 if (!fr_ipaddr2sockaddr(&sock->ipaddr, sock->port, &salocal, &salen)) {
1221 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1222 if (sock->ipaddr.af == AF_INET6) {
1224 * Listening on '::' does NOT get you IPv4 to
1225 * IPv6 mapping. You've got to listen on an IPv4
1226 * address, too. This makes the rest of the server
1227 * design a little simpler.
1231 if (IN6_IS_ADDR_UNSPECIFIED(&sock->ipaddr.ipaddr.ip6addr)) {
1234 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
1235 (char *)&on, sizeof(on));
1237 #endif /* IPV6_V6ONLY */
1238 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
1241 if (bind(this->fd, (struct sockaddr *) &salocal, salen) < 0) {
1243 radlog(L_ERR, "Failed binding to socket: %s\n",
1249 * FreeBSD jail issues. We bind to 0.0.0.0, but the
1250 * kernel instead binds us to a 1.2.3.4. If this
1251 * happens, notice, and remember our real IP.
1254 struct sockaddr_storage src;
1255 socklen_t sizeof_src = sizeof(src);
1257 memset(&src, 0, sizeof_src);
1258 if (getsockname(this->fd, (struct sockaddr *) &src,
1260 radlog(L_ERR, "Failed getting socket name: %s",
1265 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
1266 &sock->ipaddr, &sock->port)) {
1267 radlog(L_ERR, "Socket has unsupported address family");
1276 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
1277 radlog(L_ERR, "Failure getting socket flags: %s)\n",
1282 flags |= O_NONBLOCK;
1283 if( fcntl(this->fd, F_SETFL, flags) < 0) {
1284 radlog(L_ERR, "Failure setting socket flags: %s)\n",
1296 * Allocate & initialize a new listener.
1298 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
1302 this = rad_malloc(sizeof(*this));
1303 memset(this, 0, sizeof(*this));
1306 this->recv = master_listen[this->type].recv;
1307 this->send = master_listen[this->type].send;
1308 this->print = master_listen[this->type].print;
1309 this->encode = master_listen[this->type].encode;
1310 this->decode = master_listen[this->type].decode;
1314 case RAD_LISTEN_NONE:
1316 case RAD_LISTEN_AUTH:
1317 case RAD_LISTEN_ACCT:
1318 case RAD_LISTEN_PROXY:
1319 case RAD_LISTEN_VQP:
1320 case RAD_LISTEN_DHCP:
1321 this->data = rad_malloc(sizeof(listen_socket_t));
1322 memset(this->data, 0, sizeof(listen_socket_t));
1326 case RAD_LISTEN_DETAIL:
1332 rad_assert("Unsupported option!" == NULL);
1342 * Externally visible function for creating a new proxy LISTENER.
1344 * For now, don't take ipaddr or port.
1346 * Not thread-safe, but all calls to it are protected by the
1347 * proxy mutex in request_list.c
1349 rad_listen_t *proxy_new_listener()
1351 int last_proxy_port, port;
1352 rad_listen_t *this, *tmp, **last;
1353 listen_socket_t *sock, *old;
1355 this = listen_alloc(RAD_LISTEN_PROXY);
1358 * Find an existing proxy socket to copy.
1360 * FIXME: Make it per-realm, or per-home server!
1362 last_proxy_port = 0;
1364 last = &mainconfig.listen;
1365 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1366 if (tmp->type == RAD_LISTEN_PROXY) {
1368 if (sock->port > last_proxy_port) {
1369 last_proxy_port = sock->port + 1;
1371 if (!old) old = sock;
1374 last = &(tmp->next);
1379 return NULL; /* This is a serious error. */
1383 * FIXME: find a new IP address to listen on?
1385 * This could likely be done in the "home server"
1386 * configuration, to have per-home-server source IP's.
1389 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1392 * Keep going until we find an unused port.
1394 for (port = last_proxy_port; port < 64000; port++) {
1396 if (listen_bind(this) == 0) {
1398 * Add the new listener to the list of
1411 static const FR_NAME_NUMBER listen_compare[] = {
1413 { "status", RAD_LISTEN_NONE },
1415 { "auth", RAD_LISTEN_AUTH },
1416 #ifdef WITH_ACCOUNTING
1417 { "acct", RAD_LISTEN_ACCT },
1420 { "detail", RAD_LISTEN_DETAIL },
1423 { "proxy", RAD_LISTEN_PROXY },
1426 { "vmps", RAD_LISTEN_VQP },
1429 { "dhcp", RAD_LISTEN_DHCP },
1435 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1443 cf_log_info(cs, "listen {");
1445 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1447 if (rcode < 0) return NULL;
1450 cf_log_err(cf_sectiontoitem(cs),
1451 "No type specified in listen section");
1455 type = fr_str2int(listen_compare, listen_type, -1);
1457 cf_log_err(cf_sectiontoitem(cs),
1458 "Invalid type \"%s\" in listen section.",
1466 * Allow listen sections in the default config to
1467 * refer to a server.
1470 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1472 if (rcode == 1) { /* compatiblity with 2.0-pre */
1473 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1476 if (rcode < 0) return NULL;
1480 * Set up cross-type data.
1482 this = listen_alloc(type);
1483 this->server = server;
1487 * Call per-type parser.
1489 if (master_listen[type].parse(cs, this) < 0) {
1494 cf_log_info(cs, "}");
1500 * Generate a list of listeners. Takes an input list of
1501 * listeners, too, so we don't close sockets with waiting packets.
1503 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1505 int override = FALSE;
1508 rad_listen_t **last;
1510 fr_ipaddr_t server_ipaddr;
1513 int defined_proxy = 0;
1517 * We shouldn't be called with a pre-existing list.
1519 rad_assert(head && (*head == NULL));
1522 server_ipaddr.af = AF_UNSPEC;
1525 * If the port is specified on the command-line,
1526 * it over-rides the configuration file.
1528 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1530 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1533 * If the IP address was configured on the command-line,
1534 * use that as the "bind_address"
1536 if (mainconfig.myip.af != AF_UNSPEC) {
1537 memcpy(&server_ipaddr, &mainconfig.myip,
1538 sizeof(server_ipaddr));
1544 * Else look for bind_address and/or listen sections.
1546 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1547 rcode = cf_item_parse(config, "bind_address",
1549 &server_ipaddr.ipaddr.ip4addr, NULL);
1550 if (rcode < 0) return -1; /* error parsing it */
1552 if (rcode == 0) { /* successfully parsed IPv4 */
1553 listen_socket_t *sock;
1554 server_ipaddr.af = AF_INET;
1556 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'.");
1560 if (strcmp(progname, "vmpsd") == 0) {
1561 this = listen_alloc(RAD_LISTEN_VQP);
1562 if (!auth_port) auth_port = 1589;
1565 this = listen_alloc(RAD_LISTEN_AUTH);
1569 sock->ipaddr = server_ipaddr;
1570 sock->port = auth_port;
1572 sock->clients = clients_parse_section(config);
1573 if (!sock->clients) {
1574 cf_log_err(cf_sectiontoitem(config),
1575 "Failed to find any clients for this listen section");
1579 if (listen_bind(this) < 0) {
1582 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1585 auth_port = sock->port; /* may have been updated in listen_bind */
1587 cs = cf_section_sub_find_name2(config, "server",
1589 if (cs) this->server = mainconfig.name;
1593 last = &(this->next);
1599 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
1602 #ifdef WITH_ACCOUNTING
1604 * Open Accounting Socket.
1606 * If we haven't already gotten acct_port from
1607 * /etc/services, then make it auth_port + 1.
1609 this = listen_alloc(RAD_LISTEN_ACCT);
1613 * Create the accounting socket.
1615 * The accounting port is always the
1616 * authentication port + 1
1618 sock->ipaddr = server_ipaddr;
1619 sock->port = auth_port + 1;
1621 sock->clients = clients_parse_section(config);
1622 if (!sock->clients) {
1623 cf_log_err(cf_sectiontoitem(config),
1624 "Failed to find any clients for this listen section");
1628 if (listen_bind(this) < 0) {
1631 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1636 cs = cf_section_sub_find_name2(config, "server",
1638 if (cs) this->server = mainconfig.name;
1642 last = &(this->next);
1644 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1645 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1651 * They specified an IP on the command-line, ignore
1652 * all listen sections except the one in '-n'.
1654 if (mainconfig.myip.af != AF_UNSPEC) {
1655 CONF_SECTION *subcs;
1656 const char *name2 = cf_section_name2(cs);
1658 cs = cf_section_sub_find_name2(config, "server",
1660 if (!cs) goto do_proxy;
1663 * Should really abstract this code...
1665 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1667 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1668 this = listen_parse(subcs, name2);
1675 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1679 last = &(this->next);
1680 } /* loop over "listen" directives in server <foo> */
1686 * Walk through the "listen" sections, if they exist.
1688 for (cs = cf_subsection_find_next(config, NULL, "listen");
1690 cs = cf_subsection_find_next(config, cs, "listen")) {
1691 this = listen_parse(cs, NULL);
1698 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1702 last = &(this->next);
1706 * Check virtual servers for "listen" sections, too.
1708 * FIXME: Move to virtual server init?
1710 for (cs = cf_subsection_find_next(config, NULL, "server");
1712 cs = cf_subsection_find_next(config, cs, "server")) {
1713 CONF_SECTION *subcs;
1714 const char *name2 = cf_section_name2(cs);
1716 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1718 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1719 this = listen_parse(subcs, name2);
1726 if (this->type == RAD_LISTEN_PROXY) {
1727 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
1734 last = &(this->next);
1735 } /* loop over "listen" directives in virtual servers */
1736 } /* loop over virtual servers */
1739 * If we're proxying requests, open the proxy FD.
1740 * Otherwise, don't do anything.
1744 if (mainconfig.proxy_requests == TRUE) {
1746 listen_socket_t *sock = NULL;
1749 * No sockets to receive packets, therefore
1750 * proxying is pointless.
1752 if (!*head) return -1;
1754 if (defined_proxy) goto do_snmp;
1757 * Find the first authentication port,
1760 for (this = *head; this != NULL; this = this->next) {
1761 if (this->type == RAD_LISTEN_AUTH) {
1763 if (server_ipaddr.af == AF_UNSPEC) {
1764 server_ipaddr = sock->ipaddr;
1766 port = sock->port + 2; /* skip acct port */
1769 if (this->type == RAD_LISTEN_VQP) {
1771 if (server_ipaddr.af == AF_UNSPEC) {
1772 server_ipaddr = sock->ipaddr;
1774 port = sock->port + 1;
1779 if (port < 0) port = 1024 + (fr_rand() & 0x1ff);
1782 * Address is still unspecified, use IPv4.
1784 if (server_ipaddr.af == AF_UNSPEC) {
1785 server_ipaddr.af = AF_INET;
1786 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1789 this = listen_alloc(RAD_LISTEN_PROXY);
1793 * Create the first proxy socket.
1795 sock->ipaddr = server_ipaddr;
1798 * Try to find a proxy port (value doesn't matter)
1800 for (sock->port = port;
1803 if (listen_bind(this) == 0) {
1805 last = &(this->next); /* just in case */
1810 if (sock->port >= 64000) {
1813 radlog(L_ERR, "Failed to open socket for proxying");
1818 do_snmp: /* used only in proxy code. */
1822 if (radius_snmp_init(config)) {
1823 this = rad_malloc(sizeof(*this));
1824 memset(this, 0, sizeof(*this));
1826 this->type = RAD_LISTEN_SNMP;
1827 this->fd = rad_snmp.smux_fd;
1829 this->recv = radius_snmp_recv;
1830 this->print = radius_snmp_print;
1833 last = &(this->next);
1841 * Free a linked list of listeners;
1843 void listen_free(rad_listen_t **head)
1847 if (!head || !*head) return;
1851 rad_listen_t *next = this->next;
1854 * Other code may have eaten the FD.
1856 if (this->fd >= 0) close(this->fd);
1858 if (master_listen[this->type].free) {
1859 master_listen[this->type].free(this);
1871 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
1876 for (this = mainconfig.listen; this != NULL; this = this->next) {
1877 listen_socket_t *sock;
1879 if ((this->type != RAD_LISTEN_AUTH) &&
1880 (this->type != RAD_LISTEN_ACCT)) continue;
1884 if ((sock->port == port) &&
1885 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {
1886 return sock->clients;
1893 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
1897 for (this = mainconfig.listen; this != NULL; this = this->next) {
1898 listen_socket_t *sock;
1900 if ((this->type != RAD_LISTEN_AUTH) &&
1901 (this->type != RAD_LISTEN_ACCT)) continue;
1905 if ((sock->port == port) &&
1906 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {