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/modules.h>
29 #include <freeradius-devel/rad_assert.h>
30 #include <freeradius-devel/vqp.h>
31 #include <freeradius-devel/dhcp.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 #ifdef SO_BINDTODEVICE
72 const char *interface;
74 RADCLIENT_LIST *clients;
77 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type);
80 * Find a per-socket client.
82 RADCLIENT *client_listener_find(const rad_listen_t *listener,
83 const fr_ipaddr_t *ipaddr, int src_port)
85 #ifdef WITH_DYNAMIC_CLIENTS
87 listen_socket_t *sock;
93 RADCLIENT_LIST *clients;
95 rad_assert(listener != NULL);
96 rad_assert(ipaddr != NULL);
98 clients = ((listen_socket_t *)listener->data)->clients;
101 * This HAS to have been initialized previously.
103 rad_assert(clients != NULL);
105 client = client_find(clients, ipaddr);
107 static time_t last_printed = 0;
108 char name[256], buffer[128];
110 #ifdef WITH_DYNAMIC_CLIENTS
111 unknown: /* used only for dynamic clients */
115 * DoS attack quenching, but only in debug mode.
116 * If they're running in debug mode, show them
119 if (debug_flag == 0) {
121 if (last_printed == now) return NULL;
126 listener->print(listener, name, sizeof(name));
128 radlog(L_ERR, "Ignoring request to %s from unknown client %s port %d",
129 name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
130 buffer, sizeof(buffer)),
135 #ifndef WITH_DYNAMIC_CLIENTS
136 return client; /* return the found client. */
140 * No server defined, and it's not dynamic. Return it.
142 if (!client->client_server && !client->dynamic) return client;
147 * It's a dynamically generated client, check it.
149 if (client->dynamic && (src_port != 0)) {
151 * Lives forever. Return it.
153 if (client->lifetime == 0) return client;
156 * Rate-limit the deletion of known clients.
157 * This makes them last a little longer, but
158 * prevents the server from melting down if (say)
159 * 10k clients all expire at once.
161 if (now == client->last_new_client) return client;
164 * It's not dead yet. Return it.
166 if ((client->created + client->lifetime) > now) return client;
169 * This really puts them onto a queue for later
172 client_delete(clients, client);
175 * Go find the enclosing network again.
177 client = client_find(clients, ipaddr);
182 if (!client) goto unknown;
183 if (!client->client_server) goto unknown;
186 * At this point, 'client' is the enclosing
187 * network that configures where dynamic clients
190 rad_assert(client->dynamic == 0);
193 * The IP is unknown, so we've found an enclosing
194 * network. Enable DoS protection. We only
195 * allow one new client per second. Known
196 * clients aren't subject to this restriction.
198 if (now == client->last_new_client) goto unknown;
201 client->last_new_client = now;
203 request = request_alloc();
204 if (!request) goto unknown;
206 request->listener = listener;
207 request->client = client;
208 request->packet = rad_alloc(0);
209 if (!request->packet) {
210 request_free(&request);
213 request->reply = rad_alloc(0);
214 if (!request->reply) {
215 request_free(&request);
218 request->packet->timestamp = request->timestamp;
220 request->priority = listener->type;
221 request->server = client->client_server;
222 request->root = &mainconfig;
225 * Run a fake request through the given virtual server.
226 * Look for FreeRADIUS-Client-IP-Address
227 * FreeRADIUS-Client-Secret
230 * and create the RADCLIENT structure from that.
233 sock = listener->data;
234 request->packet->sockfd = listener->fd;
235 request->packet->src_ipaddr = *ipaddr;
236 request->packet->src_port = 0; /* who cares... */
237 request->packet->dst_ipaddr = sock->ipaddr;
238 request->packet->dst_port = sock->port;
240 request->reply->sockfd = request->packet->sockfd;
241 request->reply->dst_ipaddr = request->packet->src_ipaddr;
242 request->reply->src_ipaddr = request->packet->dst_ipaddr;
243 request->reply->dst_port = request->packet->src_port;
244 request->reply->src_port = request->packet->dst_port;
245 request->reply->id = request->packet->id;
246 request->reply->code = 0; /* UNKNOWN code */
249 DEBUG("server %s {", request->server);
251 rcode = module_authorize(0, request);
253 DEBUG("} # server %s", request->server);
255 if (rcode != RLM_MODULE_OK) {
256 request_free(&request);
261 * If the client was updated by rlm_dynamic_clients,
262 * don't create the client from attribute-value pairs.
264 if (request->client == client) {
265 created = client_create(clients, request);
267 created = request->client;
270 * This frees the client if it isn't valid.
272 if (!client_validate(clients, client, created)) goto unknown;
274 request_free(&request);
276 if (!created) goto unknown;
282 static int listen_bind(rad_listen_t *this);
286 * Process and reply to a server-status request.
287 * Like rad_authenticate and rad_accounting this should
288 * live in it's own file but it's so small we don't bother.
290 static int rad_status_server(REQUEST *request)
292 int rcode = RLM_MODULE_OK;
295 switch (request->listener->type) {
297 case RAD_LISTEN_NONE:
299 case RAD_LISTEN_AUTH:
300 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
302 rcode = module_authorize(dval->value, request);
304 rcode = RLM_MODULE_OK;
309 case RLM_MODULE_UPDATED:
310 request->reply->code = PW_AUTHENTICATION_ACK;
313 case RLM_MODULE_FAIL:
314 case RLM_MODULE_HANDLED:
315 request->reply->code = 0; /* don't reply */
319 case RLM_MODULE_REJECT:
320 request->reply->code = PW_AUTHENTICATION_REJECT;
325 #ifdef WITH_ACCOUNTING
326 case RAD_LISTEN_ACCT:
327 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
329 rcode = module_accounting(dval->value, request);
331 rcode = RLM_MODULE_OK;
336 case RLM_MODULE_UPDATED:
337 request->reply->code = PW_ACCOUNTING_RESPONSE;
341 request->reply->code = 0; /* don't reply */
353 * Full statistics are available only on a statistics
356 if (request->listener->type == RAD_LISTEN_NONE) {
357 request_stats_reply(request);
365 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
368 listen_socket_t *sock = this->data;
371 switch (this->type) {
373 case RAD_LISTEN_NONE: /* what a hack... */
378 case RAD_LISTEN_AUTH:
379 name = "authentication";
382 #ifdef WITH_ACCOUNTING
383 case RAD_LISTEN_ACCT:
389 case RAD_LISTEN_PROXY:
401 case RAD_LISTEN_DHCP:
411 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
412 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
416 #ifdef SO_BINDTODEVICE
417 if (sock->interface) {
418 ADDSTRING(" interface ");
419 ADDSTRING(sock->interface);
423 ADDSTRING(" address ");
425 if ((sock->ipaddr.af == AF_INET) &&
426 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
427 strlcpy(buffer, "*", bufsize);
429 ip_ntoh(&sock->ipaddr, buffer, bufsize);
434 snprintf(buffer, bufsize, "%d", sock->port);
438 ADDSTRING(" as server ");
439 ADDSTRING(this->server);
450 * Parse an authentication or accounting socket.
452 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
457 listen_socket_t *sock = this->data;
458 char *section_name = NULL;
459 CONF_SECTION *client_cs, *parentcs;
464 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
465 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
466 &ipaddr.ipaddr.ip4addr, NULL);
467 if (rcode < 0) return -1;
469 if (rcode == 0) { /* successfully parsed IPv4 */
472 } else { /* maybe IPv6? */
473 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
474 &ipaddr.ipaddr.ip6addr, NULL);
475 if (rcode < 0) return -1;
478 cf_log_err(cf_sectiontoitem(cs),
479 "No address specified in listen section");
482 ipaddr.af = AF_INET6;
485 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
487 if (rcode < 0) return -1;
489 if ((listen_port < 0) || (listen_port > 65535)) {
490 cf_log_err(cf_sectiontoitem(cs),
491 "Invalid value for \"port\"");
495 sock->ipaddr = ipaddr;
496 sock->port = listen_port;
499 * If we can bind to interfaces, do so,
502 if (cf_pair_find(cs, "interface")) {
503 #ifndef SO_BINDTODEVICE
504 cf_log_err(cf_sectiontoitem(cs),
505 "System does not support binding to interfaces. Delete this line from the configuration file.");
509 CONF_PAIR *cp = cf_pair_find(cs, "interface");
511 rad_assert(cp != NULL);
512 value = cf_pair_value(cp);
514 cf_log_err(cf_sectiontoitem(cs),
515 "No interface name given");
518 sock->interface = value;
523 * And bind it to the port.
525 if (listen_bind(this) < 0) {
527 cf_log_err(cf_sectiontoitem(cs),
528 "Error binding to port for %s port %d",
529 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
536 * Proxy sockets don't have clients.
538 if (this->type == RAD_LISTEN_PROXY) return 0;
542 * The more specific configurations are preferred to more
546 parentcs = cf_top_section(cs);
547 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
548 §ion_name, NULL);
549 if (rcode < 0) return -1; /* bad string */
552 * Explicit list given: use it.
554 client_cs = cf_section_sub_find_name2(parentcs,
558 client_cs = cf_section_find(section_name);
561 cf_log_err(cf_sectiontoitem(cs),
562 "Failed to find clients %s {...}",
568 } /* else there was no "clients = " entry. */
571 CONF_SECTION *server_cs;
573 server_cs = cf_section_sub_find_name2(parentcs,
577 * Found a "server foo" section. If there are clients
581 (cf_section_sub_find(server_cs, "client") != NULL)) {
582 client_cs = server_cs;
587 * Still nothing. Look for global clients.
589 if (!client_cs) client_cs = parentcs;
591 sock->clients = clients_parse_section(client_cs);
592 if (!sock->clients) {
593 cf_log_err(cf_sectiontoitem(cs),
594 "Failed to load clients for this listen section");
602 * Send an authentication response packet
604 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
606 rad_assert(request->listener == listener);
607 rad_assert(listener->send == auth_socket_send);
609 return rad_send(request->reply, request->packet,
610 request->client->secret);
614 #ifdef WITH_ACCOUNTING
616 * Send an accounting response packet (or not)
618 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
620 rad_assert(request->listener == listener);
621 rad_assert(listener->send == acct_socket_send);
624 * Accounting reject's are silently dropped.
626 * We do it here to avoid polluting the rest of the
627 * code with this knowledge
629 if (request->reply->code == 0) return 0;
631 return rad_send(request->reply, request->packet,
632 request->client->secret);
638 * Send a packet to a home server.
640 * FIXME: have different code for proxy auth & acct!
642 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
644 listen_socket_t *sock = listener->data;
646 rad_assert(request->proxy_listener == listener);
647 rad_assert(listener->send == proxy_socket_send);
649 request->proxy->src_ipaddr = sock->ipaddr;
650 request->proxy->src_port = sock->port;
652 return rad_send(request->proxy, request->packet,
653 request->home_server->secret);
659 * Check if an incoming request is "ok"
661 * It takes packets, not requests. It sees if the packet looks
662 * OK. If so, it does a number of sanity checks on it.
664 static int stats_socket_recv(rad_listen_t *listener,
665 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
669 RADIUS_PACKET *packet;
671 fr_ipaddr_t src_ipaddr;
673 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
674 if (rcode < 0) return 0;
676 RAD_STATS_TYPE_INC(listener, total_requests);
678 if (rcode < 20) { /* AUTH_HDR_LEN */
679 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
683 if ((client = client_listener_find(listener,
684 &src_ipaddr, src_port)) == NULL) {
685 rad_recv_discard(listener->fd);
686 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
691 * We only understand Status-Server on this socket.
693 if (code != PW_STATUS_SERVER) {
694 DEBUG("Ignoring packet code %d sent to Status-Server port",
696 rad_recv_discard(listener->fd);
697 RAD_STATS_TYPE_INC(listener, total_unknown_types);
698 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
703 * Now that we've sanity checked everything, receive the
706 packet = rad_recv(listener->fd, 1); /* require message authenticator */
708 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
709 DEBUG("%s", fr_strerror());
713 if (!received_request(listener, packet, prequest, client)) {
714 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
715 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
720 *pfun = rad_status_server;
727 * Check if an incoming request is "ok"
729 * It takes packets, not requests. It sees if the packet looks
730 * OK. If so, it does a number of sanity checks on it.
732 static int auth_socket_recv(rad_listen_t *listener,
733 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
737 RADIUS_PACKET *packet;
738 RAD_REQUEST_FUNP fun = NULL;
740 fr_ipaddr_t src_ipaddr;
742 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
743 if (rcode < 0) return 0;
745 RAD_STATS_TYPE_INC(listener, total_requests);
747 if (rcode < 20) { /* AUTH_HDR_LEN */
748 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
752 if ((client = client_listener_find(listener,
753 &src_ipaddr, src_port)) == NULL) {
754 rad_recv_discard(listener->fd);
755 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
760 * Some sanity checks, based on the packet code.
763 case PW_AUTHENTICATION_REQUEST:
764 RAD_STATS_CLIENT_INC(listener, client, total_requests);
765 fun = rad_authenticate;
768 case PW_STATUS_SERVER:
769 if (!mainconfig.status_server) {
770 rad_recv_discard(listener->fd);
771 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
772 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
773 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
776 fun = rad_status_server;
780 rad_recv_discard(listener->fd);
781 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
782 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
784 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
785 code, client->shortname, src_port);
788 } /* switch over packet types */
791 * Now that we've sanity checked everything, receive the
794 packet = rad_recv(listener->fd, client->message_authenticator);
796 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
797 DEBUG("%s", fr_strerror());
801 if (!received_request(listener, packet, prequest, client)) {
802 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
803 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
813 #ifdef WITH_ACCOUNTING
815 * Receive packets from an accounting socket
817 static int acct_socket_recv(rad_listen_t *listener,
818 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
822 RADIUS_PACKET *packet;
823 RAD_REQUEST_FUNP fun = NULL;
825 fr_ipaddr_t src_ipaddr;
827 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
828 if (rcode < 0) return 0;
830 RAD_STATS_TYPE_INC(listener, total_requests);
832 if (rcode < 20) { /* AUTH_HDR_LEN */
833 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
837 if ((client = client_listener_find(listener,
838 &src_ipaddr, src_port)) == NULL) {
839 rad_recv_discard(listener->fd);
840 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
845 * Some sanity checks, based on the packet code.
848 case PW_ACCOUNTING_REQUEST:
849 RAD_STATS_CLIENT_INC(listener, client, total_requests);
850 fun = rad_accounting;
853 case PW_STATUS_SERVER:
854 if (!mainconfig.status_server) {
855 rad_recv_discard(listener->fd);
856 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
857 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
859 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
862 fun = rad_status_server;
866 rad_recv_discard(listener->fd);
867 RAD_STATS_TYPE_INC(listener, total_unknown_types);
868 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
870 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
871 code, client->shortname, src_port);
873 } /* switch over packet types */
876 * Now that we've sanity checked everything, receive the
879 packet = rad_recv(listener->fd, 0);
881 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
882 radlog(L_ERR, "%s", fr_strerror());
887 * There can be no duplicate accounting packets.
889 if (!received_request(listener, packet, prequest, client)) {
890 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
891 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
904 * For now, all CoA requests are *only* originated, and not
905 * proxied. So all of the necessary work is done in the
906 * post-proxy section, which is automatically handled by event.c.
907 * As a result, we don't have to do anything here.
909 static int rad_coa_reply(REQUEST *request)
914 * Inform the user about RFC requirements.
916 s1 = pairfind(request->proxy->vps, PW_STATE);
918 s2 = pairfind(request->proxy_reply->vps, PW_STATE);
921 DEBUG("WARNING: Client was sent State in CoA, and did not respond with State.");
923 } else if ((s1->length != s2->length) ||
924 (memcmp(s1->vp_octets, s2->vp_octets,
926 DEBUG("WARNING: Client was sent State in CoA, and did not respond with the same State.");
930 return RLM_MODULE_OK;
936 * Recieve packets from a proxy socket.
938 static int proxy_socket_recv(rad_listen_t *listener,
939 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
942 RADIUS_PACKET *packet;
943 RAD_REQUEST_FUNP fun = NULL;
946 packet = rad_recv(listener->fd, 0);
948 radlog(L_ERR, "%s", fr_strerror());
953 * FIXME: Client MIB updates?
955 switch(packet->code) {
956 case PW_AUTHENTICATION_ACK:
957 case PW_ACCESS_CHALLENGE:
958 case PW_AUTHENTICATION_REJECT:
959 fun = rad_authenticate;
962 #ifdef WITH_ACCOUNTING
963 case PW_ACCOUNTING_RESPONSE:
964 fun = rad_accounting;
969 case PW_DISCONNECT_ACK:
970 case PW_DISCONNECT_NAK:
979 * FIXME: Update MIB for packet types?
981 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
982 "from home server %s port %d - ID %d : IGNORED",
984 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
985 packet->src_port, packet->id);
990 request = received_proxy_response(packet);
996 rad_assert(fun != NULL);
1005 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1007 if (!request->reply->code) return 0;
1009 rad_encode(request->reply, request->packet,
1010 request->client->secret);
1011 rad_sign(request->reply, request->packet,
1012 request->client->secret);
1018 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1020 if (rad_verify(request->packet, NULL,
1021 request->client->secret) < 0) {
1025 return rad_decode(request->packet, NULL,
1026 request->client->secret);
1030 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1032 rad_encode(request->proxy, NULL, request->home_server->secret);
1033 rad_sign(request->proxy, NULL, request->home_server->secret);
1039 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1041 if (rad_verify(request->proxy_reply, request->proxy,
1042 request->home_server->secret) < 0) {
1046 return rad_decode(request->proxy_reply, request->proxy,
1047 request->home_server->secret);
1053 #include "command.c"
1055 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1057 { common_socket_parse, NULL,
1058 stats_socket_recv, auth_socket_send,
1059 socket_print, client_socket_encode, client_socket_decode },
1062 * This always gets defined.
1064 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1069 { common_socket_parse, NULL,
1070 proxy_socket_recv, proxy_socket_send,
1071 socket_print, proxy_socket_encode, proxy_socket_decode },
1074 /* authentication */
1075 { common_socket_parse, NULL,
1076 auth_socket_recv, auth_socket_send,
1077 socket_print, client_socket_encode, client_socket_decode },
1079 #ifdef WITH_ACCOUNTING
1081 { common_socket_parse, NULL,
1082 acct_socket_recv, acct_socket_send,
1083 socket_print, client_socket_encode, client_socket_decode},
1088 { detail_parse, detail_free,
1089 detail_recv, detail_send,
1090 detail_print, detail_encode, detail_decode },
1094 /* vlan query protocol */
1095 { common_socket_parse, NULL,
1096 vqp_socket_recv, vqp_socket_send,
1097 socket_print, vqp_socket_encode, vqp_socket_decode },
1101 /* dhcp query protocol */
1102 { dhcp_socket_parse, NULL,
1103 dhcp_socket_recv, dhcp_socket_send,
1104 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1107 #ifdef WITH_COMMAND_SOCKET
1108 /* TCP command socket */
1109 { command_socket_parse, NULL,
1110 command_domain_accept, command_domain_send,
1111 command_socket_print, command_socket_encode, command_socket_decode },
1119 * Binds a listener to a socket.
1121 static int listen_bind(rad_listen_t *this)
1124 struct sockaddr_storage salocal;
1126 listen_socket_t *sock = this->data;
1129 * If the port is zero, then it means the appropriate
1130 * thing from /etc/services.
1132 if (sock->port == 0) {
1133 struct servent *svp;
1135 switch (this->type) {
1136 case RAD_LISTEN_AUTH:
1137 svp = getservbyname ("radius", "udp");
1139 sock->port = ntohs(svp->s_port);
1141 sock->port = PW_AUTH_UDP_PORT;
1145 #ifdef WITH_ACCOUNTING
1146 case RAD_LISTEN_ACCT:
1147 svp = getservbyname ("radacct", "udp");
1149 sock->port = ntohs(svp->s_port);
1151 sock->port = PW_ACCT_UDP_PORT;
1157 case RAD_LISTEN_PROXY:
1163 case RAD_LISTEN_VQP:
1169 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1175 * Copy fr_socket() here, as we may need to bind to a device.
1177 this->fd = socket(sock->ipaddr.af, SOCK_DGRAM, 0);
1179 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
1183 #ifdef SO_BINDTODEVICE
1185 * Bind to a device BEFORE touching IP addresses.
1187 if (sock->interface) {
1189 strcpy(ifreq.ifr_name, sock->interface);
1192 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1193 (char *)&ifreq, sizeof(ifreq));
1197 radlog(L_ERR, "Failed binding to interface %s: %s",
1198 sock->interface, strerror(errno));
1200 } /* else it worked. */
1204 #ifdef WITH_UDPFROMTO
1206 * Initialize udpfromto for all sockets.
1208 if (udpfromto_init(this->fd) != 0) {
1215 * Set up sockaddr stuff.
1217 if (!fr_ipaddr2sockaddr(&sock->ipaddr, sock->port, &salocal, &salen)) {
1222 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1223 if (sock->ipaddr.af == AF_INET6) {
1225 * Listening on '::' does NOT get you IPv4 to
1226 * IPv6 mapping. You've got to listen on an IPv4
1227 * address, too. This makes the rest of the server
1228 * design a little simpler.
1232 if (IN6_IS_ADDR_UNSPECIFIED(&sock->ipaddr.ipaddr.ip6addr)) {
1235 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
1236 (char *)&on, sizeof(on));
1238 #endif /* IPV6_V6ONLY */
1240 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
1243 * May be binding to priviledged ports.
1246 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
1250 radlog(L_ERR, "Failed binding to socket: %s\n",
1256 * FreeBSD jail issues. We bind to 0.0.0.0, but the
1257 * kernel instead binds us to a 1.2.3.4. If this
1258 * happens, notice, and remember our real IP.
1261 struct sockaddr_storage src;
1262 socklen_t sizeof_src = sizeof(src);
1264 memset(&src, 0, sizeof_src);
1265 if (getsockname(this->fd, (struct sockaddr *) &src,
1267 radlog(L_ERR, "Failed getting socket name: %s",
1272 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
1273 &sock->ipaddr, &sock->port)) {
1274 radlog(L_ERR, "Socket has unsupported address family");
1283 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
1284 radlog(L_ERR, "Failure getting socket flags: %s)\n",
1289 flags |= O_NONBLOCK;
1290 if( fcntl(this->fd, F_SETFL, flags) < 0) {
1291 radlog(L_ERR, "Failure setting socket flags: %s)\n",
1303 * Allocate & initialize a new listener.
1305 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
1309 this = rad_malloc(sizeof(*this));
1310 memset(this, 0, sizeof(*this));
1313 this->recv = master_listen[this->type].recv;
1314 this->send = master_listen[this->type].send;
1315 this->print = master_listen[this->type].print;
1316 this->encode = master_listen[this->type].encode;
1317 this->decode = master_listen[this->type].decode;
1321 case RAD_LISTEN_NONE:
1323 case RAD_LISTEN_AUTH:
1324 #ifdef WITH_ACCOUNTING
1325 case RAD_LISTEN_ACCT:
1328 case RAD_LISTEN_PROXY:
1331 case RAD_LISTEN_VQP:
1334 case RAD_LISTEN_DHCP:
1336 this->data = rad_malloc(sizeof(listen_socket_t));
1337 memset(this->data, 0, sizeof(listen_socket_t));
1341 case RAD_LISTEN_DETAIL:
1346 #ifdef WITH_COMMAND_SOCKET
1347 case RAD_LISTEN_COMMAND:
1348 this->data = rad_malloc(sizeof(fr_command_socket_t));
1349 memset(this->data, 0, sizeof(fr_command_socket_t));
1354 rad_assert("Unsupported option!" == NULL);
1364 * Externally visible function for creating a new proxy LISTENER.
1366 * For now, don't take ipaddr or port.
1368 * Not thread-safe, but all calls to it are protected by the
1369 * proxy mutex in request_list.c
1371 rad_listen_t *proxy_new_listener()
1373 int last_proxy_port, port;
1374 rad_listen_t *this, *tmp, **last;
1375 listen_socket_t *sock, *old;
1377 this = listen_alloc(RAD_LISTEN_PROXY);
1380 * Find an existing proxy socket to copy.
1382 * FIXME: Make it per-realm, or per-home server!
1384 last_proxy_port = 0;
1386 last = &mainconfig.listen;
1387 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1388 if (tmp->type == RAD_LISTEN_PROXY) {
1390 if (sock->port > last_proxy_port) {
1391 last_proxy_port = sock->port + 1;
1393 if (!old) old = sock;
1396 last = &(tmp->next);
1401 return NULL; /* This is a serious error. */
1405 * FIXME: find a new IP address to listen on?
1407 * This could likely be done in the "home server"
1408 * configuration, to have per-home-server source IP's.
1411 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1414 * Keep going until we find an unused port.
1416 for (port = last_proxy_port; port < 64000; port++) {
1418 if (listen_bind(this) == 0) {
1420 * Add the new listener to the list of
1433 static const FR_NAME_NUMBER listen_compare[] = {
1435 { "status", RAD_LISTEN_NONE },
1437 { "auth", RAD_LISTEN_AUTH },
1438 #ifdef WITH_ACCOUNTING
1439 { "acct", RAD_LISTEN_ACCT },
1442 { "detail", RAD_LISTEN_DETAIL },
1445 { "proxy", RAD_LISTEN_PROXY },
1448 { "vmps", RAD_LISTEN_VQP },
1451 { "dhcp", RAD_LISTEN_DHCP },
1453 #ifdef WITH_COMMAND_SOCKET
1454 { "control", RAD_LISTEN_COMMAND },
1460 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1468 cf_log_info(cs, "listen {");
1470 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1472 if (rcode < 0) return NULL;
1475 cf_log_err(cf_sectiontoitem(cs),
1476 "No type specified in listen section");
1480 type = fr_str2int(listen_compare, listen_type, -1);
1482 cf_log_err(cf_sectiontoitem(cs),
1483 "Invalid type \"%s\" in listen section.",
1491 * Allow listen sections in the default config to
1492 * refer to a server.
1495 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1497 if (rcode == 1) { /* compatiblity with 2.0-pre */
1498 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1501 if (rcode < 0) return NULL;
1505 * Set up cross-type data.
1507 this = listen_alloc(type);
1508 this->server = server;
1512 * Call per-type parser.
1514 if (master_listen[type].parse(cs, this) < 0) {
1519 cf_log_info(cs, "}");
1525 * Generate a list of listeners. Takes an input list of
1526 * listeners, too, so we don't close sockets with waiting packets.
1528 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1530 int override = FALSE;
1532 CONF_SECTION *cs = NULL;
1533 rad_listen_t **last;
1535 fr_ipaddr_t server_ipaddr;
1538 int defined_proxy = 0;
1542 * We shouldn't be called with a pre-existing list.
1544 rad_assert(head && (*head == NULL));
1547 server_ipaddr.af = AF_UNSPEC;
1550 * If the port is specified on the command-line,
1551 * it over-rides the configuration file.
1553 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1555 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1558 * If the IP address was configured on the command-line,
1559 * use that as the "bind_address"
1561 if (mainconfig.myip.af != AF_UNSPEC) {
1562 memcpy(&server_ipaddr, &mainconfig.myip,
1563 sizeof(server_ipaddr));
1569 * Else look for bind_address and/or listen sections.
1571 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1572 rcode = cf_item_parse(config, "bind_address",
1574 &server_ipaddr.ipaddr.ip4addr, NULL);
1575 if (rcode < 0) return -1; /* error parsing it */
1577 if (rcode == 0) { /* successfully parsed IPv4 */
1578 listen_socket_t *sock;
1579 server_ipaddr.af = AF_INET;
1581 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'.");
1585 if (strcmp(progname, "vmpsd") == 0) {
1586 this = listen_alloc(RAD_LISTEN_VQP);
1587 if (!auth_port) auth_port = 1589;
1590 this = listen_alloc(RAD_LISTEN_AUTH);
1594 sock->ipaddr = server_ipaddr;
1595 sock->port = auth_port;
1597 sock->clients = clients_parse_section(config);
1598 if (!sock->clients) {
1599 cf_log_err(cf_sectiontoitem(config),
1600 "Failed to find any clients for this listen section");
1605 if (listen_bind(this) < 0) {
1607 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1611 auth_port = sock->port; /* may have been updated in listen_bind */
1613 cs = cf_section_sub_find_name2(config, "server",
1615 if (cs) this->server = mainconfig.name;
1619 last = &(this->next);
1625 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
1628 #ifdef WITH_ACCOUNTING
1630 * Open Accounting Socket.
1632 * If we haven't already gotten acct_port from
1633 * /etc/services, then make it auth_port + 1.
1635 this = listen_alloc(RAD_LISTEN_ACCT);
1639 * Create the accounting socket.
1641 * The accounting port is always the
1642 * authentication port + 1
1644 sock->ipaddr = server_ipaddr;
1645 sock->port = auth_port + 1;
1647 sock->clients = clients_parse_section(config);
1648 if (!sock->clients) {
1649 cf_log_err(cf_sectiontoitem(config),
1650 "Failed to find any clients for this listen section");
1654 if (listen_bind(this) < 0) {
1657 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1662 cs = cf_section_sub_find_name2(config, "server",
1664 if (cs) this->server = mainconfig.name;
1668 last = &(this->next);
1670 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1671 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1677 * They specified an IP on the command-line, ignore
1678 * all listen sections except the one in '-n'.
1680 if (mainconfig.myip.af != AF_UNSPEC) {
1681 CONF_SECTION *subcs;
1682 const char *name2 = cf_section_name2(cs);
1684 cs = cf_section_sub_find_name2(config, "server",
1686 if (!cs) goto do_proxy;
1689 * Should really abstract this code...
1691 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1693 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1694 this = listen_parse(subcs, name2);
1701 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1705 last = &(this->next);
1706 } /* loop over "listen" directives in server <foo> */
1712 * Walk through the "listen" sections, if they exist.
1714 for (cs = cf_subsection_find_next(config, NULL, "listen");
1716 cs = cf_subsection_find_next(config, cs, "listen")) {
1717 this = listen_parse(cs, NULL);
1724 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1728 last = &(this->next);
1732 * Check virtual servers for "listen" sections, too.
1734 * FIXME: Move to virtual server init?
1736 for (cs = cf_subsection_find_next(config, NULL, "server");
1738 cs = cf_subsection_find_next(config, cs, "server")) {
1739 CONF_SECTION *subcs;
1740 const char *name2 = cf_section_name2(cs);
1742 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1744 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1745 this = listen_parse(subcs, name2);
1752 if (this->type == RAD_LISTEN_PROXY) {
1753 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
1760 last = &(this->next);
1761 } /* loop over "listen" directives in virtual servers */
1762 } /* loop over virtual servers */
1765 * If we're proxying requests, open the proxy FD.
1766 * Otherwise, don't do anything.
1770 if (mainconfig.proxy_requests == TRUE) {
1772 listen_socket_t *sock = NULL;
1775 * No sockets to receive packets, therefore
1776 * proxying is pointless.
1778 if (!*head) return -1;
1780 if (defined_proxy) goto done;
1783 * Find the first authentication port,
1786 for (this = *head; this != NULL; this = this->next) {
1787 if (this->type == RAD_LISTEN_AUTH) {
1789 if (server_ipaddr.af == AF_UNSPEC) {
1790 server_ipaddr = sock->ipaddr;
1792 port = sock->port + 2; /* skip acct port */
1795 if (this->type == RAD_LISTEN_VQP) {
1797 if (server_ipaddr.af == AF_UNSPEC) {
1798 server_ipaddr = sock->ipaddr;
1800 port = sock->port + 1;
1805 if (port < 0) port = 1024 + (fr_rand() & 0x1ff);
1808 * Address is still unspecified, use IPv4.
1810 if (server_ipaddr.af == AF_UNSPEC) {
1811 server_ipaddr.af = AF_INET;
1812 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1815 this = listen_alloc(RAD_LISTEN_PROXY);
1819 * Create the first proxy socket.
1821 sock->ipaddr = server_ipaddr;
1824 * Try to find a proxy port (value doesn't matter)
1826 for (sock->port = port;
1829 if (listen_bind(this) == 0) {
1831 last = &(this->next); /* just in case */
1836 if (sock->port >= 64000) {
1839 radlog(L_ERR, "Failed to open socket for proxying");
1844 done: /* used only in proxy code. */
1851 * Free a linked list of listeners;
1853 void listen_free(rad_listen_t **head)
1857 if (!head || !*head) return;
1861 rad_listen_t *next = this->next;
1864 * Other code may have eaten the FD.
1866 if (this->fd >= 0) close(this->fd);
1868 if (master_listen[this->type].free) {
1869 master_listen[this->type].free(this);
1881 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
1886 for (this = mainconfig.listen; this != NULL; this = this->next) {
1887 listen_socket_t *sock;
1889 if ((this->type != RAD_LISTEN_AUTH) &&
1890 (this->type != RAD_LISTEN_ACCT)) continue;
1894 if ((sock->port == port) &&
1895 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {
1896 return sock->clients;
1904 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
1908 for (this = mainconfig.listen; this != NULL; this = this->next) {
1909 listen_socket_t *sock;
1912 * FIXME: For TCP, ignore the *secondary*
1913 * listeners associated with the main socket.
1915 if ((this->type != RAD_LISTEN_AUTH) &&
1916 (this->type != RAD_LISTEN_ACCT)) continue;
1920 if ((sock->port == port) &&
1921 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {
1925 if ((sock->port == port) &&
1926 ((sock->ipaddr.af == AF_INET) &&
1927 (sock->ipaddr.ipaddr.ip4addr.s_addr == INADDR_ANY))) {
1931 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1932 if ((sock->port == port) &&
1933 (sock->ipaddr.af == AF_INET6) &&
1934 (IN6_IS_ADDR_UNSPECIFIED(&sock->ipaddr.ipaddr.ip6addr))) {