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) {
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 * FIXME: Don't delete it until after the last
170 * request using it has expired. i.e. keep a
171 * "use counter" (ugh). Why can't we use a
172 * language with garbage collection?
175 client_delete(clients, client);
178 * Go find the enclosing network again.
180 client = client_find(clients, ipaddr);
185 if (!client) goto unknown;
186 if (!client->client_server) goto unknown;
189 * At this point, 'client' is the enclosing
190 * network that configures where dynamic clients
193 rad_assert(client->dynamic == 0);
196 * The IP is unknown, so we've found an enclosing
197 * network. Enable DoS protection. We only
198 * allow one new client per second. Known
199 * clients aren't subject to this restriction.
201 if (now == client->last_new_client) goto unknown;
204 client->last_new_client = now;
206 request = request_alloc();
207 if (!request) goto unknown;
209 request->listener = listener;
210 request->client = client;
211 request->packet = rad_alloc(0);
212 if (!request->packet) {
213 request_free(&request);
216 request->reply = rad_alloc(0);
217 if (!request->reply) {
218 request_free(&request);
221 request->packet->timestamp = request->timestamp;
223 request->priority = listener->type;
224 request->server = client->client_server;
225 request->root = &mainconfig;
228 * Run a fake request through the given virtual server.
229 * Look for FreeRADIUS-Client-IP-Address
230 * FreeRADIUS-Client-Secret
233 * and create the RADCLIENT structure from that.
236 sock = listener->data;
237 request->packet->sockfd = listener->fd;
238 request->packet->src_ipaddr = *ipaddr;
239 request->packet->src_port = 0; /* who cares... */
240 request->packet->dst_ipaddr = sock->ipaddr;
241 request->packet->dst_port = sock->port;
243 request->reply->sockfd = request->packet->sockfd;
244 request->reply->dst_ipaddr = request->packet->src_ipaddr;
245 request->reply->src_ipaddr = request->packet->dst_ipaddr;
246 request->reply->dst_port = request->packet->src_port;
247 request->reply->src_port = request->packet->dst_port;
248 request->reply->id = request->packet->id;
249 request->reply->code = 0; /* UNKNOWN code */
252 DEBUG("server %s {", request->server);
254 rcode = module_authorize(0, request);
256 DEBUG("} # server %s", request->server);
258 if (rcode != RLM_MODULE_OK) {
259 request_free(&request);
264 * If the client was updated by rlm_dynamic_clients,
265 * don't create the client from attribute-value pairs.
267 if (request->client == client) {
268 created = client_create(clients, request);
270 created = request->client;
273 * This frees the client if it isn't valid.
275 if (!client_validate(clients, client, created)) goto unknown;
277 request_free(&request);
279 if (!created) goto unknown;
285 static int listen_bind(rad_listen_t *this);
289 * Process and reply to a server-status request.
290 * Like rad_authenticate and rad_accounting this should
291 * live in it's own file but it's so small we don't bother.
293 static int rad_status_server(REQUEST *request)
295 int rcode = RLM_MODULE_OK;
298 switch (request->listener->type) {
300 case RAD_LISTEN_NONE:
302 case RAD_LISTEN_AUTH:
303 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
305 rcode = module_authorize(dval->value, request);
307 rcode = RLM_MODULE_OK;
312 case RLM_MODULE_UPDATED:
313 request->reply->code = PW_AUTHENTICATION_ACK;
316 case RLM_MODULE_FAIL:
317 case RLM_MODULE_HANDLED:
318 request->reply->code = 0; /* don't reply */
322 case RLM_MODULE_REJECT:
323 request->reply->code = PW_AUTHENTICATION_REJECT;
328 #ifdef WITH_ACCOUNTING
329 case RAD_LISTEN_ACCT:
330 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
332 rcode = module_accounting(dval->value, request);
334 rcode = RLM_MODULE_OK;
339 case RLM_MODULE_UPDATED:
340 request->reply->code = PW_ACCOUNTING_RESPONSE;
344 request->reply->code = 0; /* don't reply */
356 * Full statistics are available only on a statistics
359 if (request->listener->type == RAD_LISTEN_NONE) {
360 request_stats_reply(request);
368 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
371 listen_socket_t *sock = this->data;
374 switch (this->type) {
376 case RAD_LISTEN_NONE: /* what a hack... */
381 case RAD_LISTEN_AUTH:
382 name = "authentication";
385 #ifdef WITH_ACCOUNTING
386 case RAD_LISTEN_ACCT:
392 case RAD_LISTEN_PROXY:
404 case RAD_LISTEN_DHCP:
414 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
415 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
419 #ifdef SO_BINDTODEVICE
420 if (sock->interface) {
421 ADDSTRING(" interface ");
422 ADDSTRING(sock->interface);
426 ADDSTRING(" address ");
428 if ((sock->ipaddr.af == AF_INET) &&
429 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
430 strlcpy(buffer, "*", bufsize);
432 ip_ntoh(&sock->ipaddr, buffer, bufsize);
437 snprintf(buffer, bufsize, "%d", sock->port);
441 ADDSTRING(" as server ");
442 ADDSTRING(this->server);
453 * Parse an authentication or accounting socket.
455 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
460 listen_socket_t *sock = this->data;
461 char *section_name = NULL;
462 CONF_SECTION *client_cs, *parentcs;
467 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
468 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
469 &ipaddr.ipaddr.ip4addr, NULL);
470 if (rcode < 0) return -1;
472 if (rcode == 0) { /* successfully parsed IPv4 */
475 } else { /* maybe IPv6? */
476 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
477 &ipaddr.ipaddr.ip6addr, NULL);
478 if (rcode < 0) return -1;
481 cf_log_err(cf_sectiontoitem(cs),
482 "No address specified in listen section");
485 ipaddr.af = AF_INET6;
488 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
490 if (rcode < 0) return -1;
492 if ((listen_port < 0) || (listen_port > 65535)) {
493 cf_log_err(cf_sectiontoitem(cs),
494 "Invalid value for \"port\"");
498 sock->ipaddr = ipaddr;
499 sock->port = listen_port;
502 * If we can bind to interfaces, do so,
505 if (cf_pair_find(cs, "interface")) {
506 #ifndef SO_BINDTODEVICE
507 cf_log_err(cf_sectiontoitem(cs),
508 "System does not support binding to interfaces. Delete this line from the configuration file.");
512 CONF_PAIR *cp = cf_pair_find(cs, "interface");
514 rad_assert(cp != NULL);
515 value = cf_pair_value(cp);
517 cf_log_err(cf_sectiontoitem(cs),
518 "No interface name given");
521 sock->interface = value;
526 * And bind it to the port.
528 if (listen_bind(this) < 0) {
530 cf_log_err(cf_sectiontoitem(cs),
531 "Error binding to port for %s port %d",
532 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
539 * Proxy sockets don't have clients.
541 if (this->type == RAD_LISTEN_PROXY) return 0;
545 * The more specific configurations are preferred to more
549 parentcs = cf_top_section(cs);
550 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
551 §ion_name, NULL);
552 if (rcode < 0) return -1; /* bad string */
555 * Explicit list given: use it.
557 client_cs = cf_section_sub_find_name2(parentcs,
561 client_cs = cf_section_find(section_name);
564 cf_log_err(cf_sectiontoitem(cs),
565 "Failed to find clients %s {...}",
571 } /* else there was no "clients = " entry. */
574 CONF_SECTION *server_cs;
576 server_cs = cf_section_sub_find_name2(parentcs,
580 * Found a "server foo" section. If there are clients
584 (cf_section_sub_find(server_cs, "client") != NULL)) {
585 client_cs = server_cs;
590 * Still nothing. Look for global clients.
592 if (!client_cs) client_cs = parentcs;
594 sock->clients = clients_parse_section(client_cs);
595 if (!sock->clients) {
596 cf_log_err(cf_sectiontoitem(cs),
597 "Failed to load clients for this listen section");
605 * Send an authentication response packet
607 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
609 rad_assert(request->listener == listener);
610 rad_assert(listener->send == auth_socket_send);
612 return rad_send(request->reply, request->packet,
613 request->client->secret);
617 #ifdef WITH_ACCOUNTING
619 * Send an accounting response packet (or not)
621 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
623 rad_assert(request->listener == listener);
624 rad_assert(listener->send == acct_socket_send);
627 * Accounting reject's are silently dropped.
629 * We do it here to avoid polluting the rest of the
630 * code with this knowledge
632 if (request->reply->code == 0) return 0;
634 return rad_send(request->reply, request->packet,
635 request->client->secret);
641 * Send a packet to a home server.
643 * FIXME: have different code for proxy auth & acct!
645 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
647 listen_socket_t *sock = listener->data;
649 rad_assert(request->proxy_listener == listener);
650 rad_assert(listener->send == proxy_socket_send);
652 request->proxy->src_ipaddr = sock->ipaddr;
653 request->proxy->src_port = sock->port;
655 return rad_send(request->proxy, request->packet,
656 request->home_server->secret);
662 * Check if an incoming request is "ok"
664 * It takes packets, not requests. It sees if the packet looks
665 * OK. If so, it does a number of sanity checks on it.
667 static int stats_socket_recv(rad_listen_t *listener,
668 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
672 RADIUS_PACKET *packet;
674 fr_ipaddr_t src_ipaddr;
676 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
677 if (rcode < 0) return 0;
679 RAD_STATS_TYPE_INC(listener, total_requests);
681 if (rcode < 20) { /* AUTH_HDR_LEN */
682 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
686 if ((client = client_listener_find(listener,
687 &src_ipaddr, src_port)) == NULL) {
688 rad_recv_discard(listener->fd);
689 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
694 * We only understand Status-Server on this socket.
696 if (code != PW_STATUS_SERVER) {
697 DEBUG("Ignoring packet code %d sent to Status-Server port",
699 rad_recv_discard(listener->fd);
700 RAD_STATS_TYPE_INC(listener, total_unknown_types);
701 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
706 * Now that we've sanity checked everything, receive the
709 packet = rad_recv(listener->fd, 1); /* require message authenticator */
711 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
712 DEBUG("%s", fr_strerror());
716 if (!received_request(listener, packet, prequest, client)) {
717 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
718 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
723 *pfun = rad_status_server;
730 * Check if an incoming request is "ok"
732 * It takes packets, not requests. It sees if the packet looks
733 * OK. If so, it does a number of sanity checks on it.
735 static int auth_socket_recv(rad_listen_t *listener,
736 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
740 RADIUS_PACKET *packet;
741 RAD_REQUEST_FUNP fun = NULL;
743 fr_ipaddr_t src_ipaddr;
745 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
746 if (rcode < 0) return 0;
748 RAD_STATS_TYPE_INC(listener, total_requests);
750 if (rcode < 20) { /* AUTH_HDR_LEN */
751 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
755 if ((client = client_listener_find(listener,
756 &src_ipaddr, src_port)) == NULL) {
757 rad_recv_discard(listener->fd);
758 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
763 * Some sanity checks, based on the packet code.
766 case PW_AUTHENTICATION_REQUEST:
767 RAD_STATS_CLIENT_INC(listener, client, total_requests);
768 fun = rad_authenticate;
771 case PW_STATUS_SERVER:
772 if (!mainconfig.status_server) {
773 rad_recv_discard(listener->fd);
774 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
775 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
776 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
779 fun = rad_status_server;
783 rad_recv_discard(listener->fd);
784 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
785 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
787 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
788 code, client->shortname, src_port);
791 } /* switch over packet types */
794 * Now that we've sanity checked everything, receive the
797 packet = rad_recv(listener->fd, client->message_authenticator);
799 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
800 DEBUG("%s", fr_strerror());
804 if (!received_request(listener, packet, prequest, client)) {
805 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
806 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
816 #ifdef WITH_ACCOUNTING
818 * Receive packets from an accounting socket
820 static int acct_socket_recv(rad_listen_t *listener,
821 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
825 RADIUS_PACKET *packet;
826 RAD_REQUEST_FUNP fun = NULL;
828 fr_ipaddr_t src_ipaddr;
830 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
831 if (rcode < 0) return 0;
833 RAD_STATS_TYPE_INC(listener, total_requests);
835 if (rcode < 20) { /* AUTH_HDR_LEN */
836 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
840 if ((client = client_listener_find(listener,
841 &src_ipaddr, src_port)) == NULL) {
842 rad_recv_discard(listener->fd);
843 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
848 * Some sanity checks, based on the packet code.
851 case PW_ACCOUNTING_REQUEST:
852 RAD_STATS_CLIENT_INC(listener, client, total_requests);
853 fun = rad_accounting;
856 case PW_STATUS_SERVER:
857 if (!mainconfig.status_server) {
858 rad_recv_discard(listener->fd);
859 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
860 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
862 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
865 fun = rad_status_server;
869 rad_recv_discard(listener->fd);
870 RAD_STATS_TYPE_INC(listener, total_unknown_types);
871 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
873 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
874 code, client->shortname, src_port);
876 } /* switch over packet types */
879 * Now that we've sanity checked everything, receive the
882 packet = rad_recv(listener->fd, 0);
884 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
885 radlog(L_ERR, "%s", fr_strerror());
890 * There can be no duplicate accounting packets.
892 if (!received_request(listener, packet, prequest, client)) {
893 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
894 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
907 * For now, all CoA requests are *only* originated, and not
908 * proxied. So all of the necessary work is done in the
909 * post-proxy section, which is automatically handled by event.c.
910 * As a result, we don't have to do anything here.
912 static int rad_coa_reply(REQUEST *request)
917 * Inform the user about RFC requirements.
919 s1 = pairfind(request->proxy->vps, PW_STATE);
921 s2 = pairfind(request->proxy_reply->vps, PW_STATE);
924 DEBUG("WARNING: Client was sent State in CoA, and did not respond with State.");
926 } else if ((s1->length != s2->length) ||
927 (memcmp(s1->vp_octets, s2->vp_octets,
929 DEBUG("WARNING: Client was sent State in CoA, and did not respond with the same State.");
933 return RLM_MODULE_OK;
939 * Recieve packets from a proxy socket.
941 static int proxy_socket_recv(rad_listen_t *listener,
942 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
945 RADIUS_PACKET *packet;
946 RAD_REQUEST_FUNP fun = NULL;
949 packet = rad_recv(listener->fd, 0);
951 radlog(L_ERR, "%s", fr_strerror());
956 * FIXME: Client MIB updates?
958 switch(packet->code) {
959 case PW_AUTHENTICATION_ACK:
960 case PW_ACCESS_CHALLENGE:
961 case PW_AUTHENTICATION_REJECT:
962 fun = rad_authenticate;
965 #ifdef WITH_ACCOUNTING
966 case PW_ACCOUNTING_RESPONSE:
967 fun = rad_accounting;
972 case PW_DISCONNECT_ACK:
973 case PW_DISCONNECT_NAK:
982 * FIXME: Update MIB for packet types?
984 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
985 "from home server %s port %d - ID %d : IGNORED",
987 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
988 packet->src_port, packet->id);
993 request = received_proxy_response(packet);
999 rad_assert(fun != NULL);
1001 *prequest = request;
1008 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1010 if (!request->reply->code) return 0;
1012 rad_encode(request->reply, request->packet,
1013 request->client->secret);
1014 rad_sign(request->reply, request->packet,
1015 request->client->secret);
1021 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1023 if (rad_verify(request->packet, NULL,
1024 request->client->secret) < 0) {
1028 return rad_decode(request->packet, NULL,
1029 request->client->secret);
1033 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1035 rad_encode(request->proxy, NULL, request->home_server->secret);
1036 rad_sign(request->proxy, NULL, request->home_server->secret);
1042 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1044 if (rad_verify(request->proxy_reply, request->proxy,
1045 request->home_server->secret) < 0) {
1049 return rad_decode(request->proxy_reply, request->proxy,
1050 request->home_server->secret);
1056 #include "command.c"
1058 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1060 { common_socket_parse, NULL,
1061 stats_socket_recv, auth_socket_send,
1062 socket_print, client_socket_encode, client_socket_decode },
1065 * This always gets defined.
1067 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1072 { common_socket_parse, NULL,
1073 proxy_socket_recv, proxy_socket_send,
1074 socket_print, proxy_socket_encode, proxy_socket_decode },
1077 /* authentication */
1078 { common_socket_parse, NULL,
1079 auth_socket_recv, auth_socket_send,
1080 socket_print, client_socket_encode, client_socket_decode },
1082 #ifdef WITH_ACCOUNTING
1084 { common_socket_parse, NULL,
1085 acct_socket_recv, acct_socket_send,
1086 socket_print, client_socket_encode, client_socket_decode},
1091 { detail_parse, detail_free,
1092 detail_recv, detail_send,
1093 detail_print, detail_encode, detail_decode },
1097 /* vlan query protocol */
1098 { common_socket_parse, NULL,
1099 vqp_socket_recv, vqp_socket_send,
1100 socket_print, vqp_socket_encode, vqp_socket_decode },
1104 /* dhcp query protocol */
1105 { dhcp_socket_parse, NULL,
1106 dhcp_socket_recv, dhcp_socket_send,
1107 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1110 #ifdef WITH_COMMAND_SOCKET
1111 /* TCP command socket */
1112 { command_socket_parse, NULL,
1113 command_domain_accept, command_domain_send,
1114 command_socket_print, command_socket_encode, command_socket_decode },
1122 * Binds a listener to a socket.
1124 static int listen_bind(rad_listen_t *this)
1127 struct sockaddr_storage salocal;
1129 listen_socket_t *sock = this->data;
1132 * If the port is zero, then it means the appropriate
1133 * thing from /etc/services.
1135 if (sock->port == 0) {
1136 struct servent *svp;
1138 switch (this->type) {
1139 case RAD_LISTEN_AUTH:
1140 svp = getservbyname ("radius", "udp");
1142 sock->port = ntohs(svp->s_port);
1144 sock->port = PW_AUTH_UDP_PORT;
1148 #ifdef WITH_ACCOUNTING
1149 case RAD_LISTEN_ACCT:
1150 svp = getservbyname ("radacct", "udp");
1152 sock->port = ntohs(svp->s_port);
1154 sock->port = PW_ACCT_UDP_PORT;
1160 case RAD_LISTEN_PROXY:
1166 case RAD_LISTEN_VQP:
1172 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1178 * Copy fr_socket() here, as we may need to bind to a device.
1180 this->fd = socket(sock->ipaddr.af, SOCK_DGRAM, 0);
1182 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
1186 #ifdef SO_BINDTODEVICE
1188 * Bind to a device BEFORE touching IP addresses.
1190 if (sock->interface) {
1192 strcpy(ifreq.ifr_name, sock->interface);
1195 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1196 (char *)&ifreq, sizeof(ifreq));
1200 radlog(L_ERR, "Failed binding to interface %s: %s",
1201 sock->interface, strerror(errno));
1203 } /* else it worked. */
1207 #ifdef WITH_UDPFROMTO
1209 * Initialize udpfromto for all sockets.
1211 if (udpfromto_init(this->fd) != 0) {
1218 * Set up sockaddr stuff.
1220 if (!fr_ipaddr2sockaddr(&sock->ipaddr, sock->port, &salocal, &salen)) {
1225 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1226 if (sock->ipaddr.af == AF_INET6) {
1228 * Listening on '::' does NOT get you IPv4 to
1229 * IPv6 mapping. You've got to listen on an IPv4
1230 * address, too. This makes the rest of the server
1231 * design a little simpler.
1235 if (IN6_IS_ADDR_UNSPECIFIED(&sock->ipaddr.ipaddr.ip6addr)) {
1238 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
1239 (char *)&on, sizeof(on));
1241 #endif /* IPV6_V6ONLY */
1243 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
1246 * May be binding to priviledged ports.
1249 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
1253 radlog(L_ERR, "Failed binding to socket: %s\n",
1259 * FreeBSD jail issues. We bind to 0.0.0.0, but the
1260 * kernel instead binds us to a 1.2.3.4. If this
1261 * happens, notice, and remember our real IP.
1264 struct sockaddr_storage src;
1265 socklen_t sizeof_src = sizeof(src);
1267 memset(&src, 0, sizeof_src);
1268 if (getsockname(this->fd, (struct sockaddr *) &src,
1270 radlog(L_ERR, "Failed getting socket name: %s",
1275 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
1276 &sock->ipaddr, &sock->port)) {
1277 radlog(L_ERR, "Socket has unsupported address family");
1286 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
1287 radlog(L_ERR, "Failure getting socket flags: %s)\n",
1292 flags |= O_NONBLOCK;
1293 if( fcntl(this->fd, F_SETFL, flags) < 0) {
1294 radlog(L_ERR, "Failure setting socket flags: %s)\n",
1306 * Allocate & initialize a new listener.
1308 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
1312 this = rad_malloc(sizeof(*this));
1313 memset(this, 0, sizeof(*this));
1316 this->recv = master_listen[this->type].recv;
1317 this->send = master_listen[this->type].send;
1318 this->print = master_listen[this->type].print;
1319 this->encode = master_listen[this->type].encode;
1320 this->decode = master_listen[this->type].decode;
1324 case RAD_LISTEN_NONE:
1326 case RAD_LISTEN_AUTH:
1327 #ifdef WITH_ACCOUNTING
1328 case RAD_LISTEN_ACCT:
1331 case RAD_LISTEN_PROXY:
1334 case RAD_LISTEN_VQP:
1337 case RAD_LISTEN_DHCP:
1339 this->data = rad_malloc(sizeof(listen_socket_t));
1340 memset(this->data, 0, sizeof(listen_socket_t));
1344 case RAD_LISTEN_DETAIL:
1349 #ifdef WITH_COMMAND_SOCKET
1350 case RAD_LISTEN_COMMAND:
1351 this->data = rad_malloc(sizeof(fr_command_socket_t));
1352 memset(this->data, 0, sizeof(fr_command_socket_t));
1357 rad_assert("Unsupported option!" == NULL);
1367 * Externally visible function for creating a new proxy LISTENER.
1369 * For now, don't take ipaddr or port.
1371 * Not thread-safe, but all calls to it are protected by the
1372 * proxy mutex in request_list.c
1374 rad_listen_t *proxy_new_listener()
1376 int last_proxy_port, port;
1377 rad_listen_t *this, *tmp, **last;
1378 listen_socket_t *sock, *old;
1380 this = listen_alloc(RAD_LISTEN_PROXY);
1383 * Find an existing proxy socket to copy.
1385 * FIXME: Make it per-realm, or per-home server!
1387 last_proxy_port = 0;
1389 last = &mainconfig.listen;
1390 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1391 if (tmp->type == RAD_LISTEN_PROXY) {
1393 if (sock->port > last_proxy_port) {
1394 last_proxy_port = sock->port + 1;
1396 if (!old) old = sock;
1399 last = &(tmp->next);
1404 return NULL; /* This is a serious error. */
1408 * FIXME: find a new IP address to listen on?
1410 * This could likely be done in the "home server"
1411 * configuration, to have per-home-server source IP's.
1414 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1417 * Keep going until we find an unused port.
1419 for (port = last_proxy_port; port < 64000; port++) {
1421 if (listen_bind(this) == 0) {
1423 * Add the new listener to the list of
1436 static const FR_NAME_NUMBER listen_compare[] = {
1438 { "status", RAD_LISTEN_NONE },
1440 { "auth", RAD_LISTEN_AUTH },
1441 #ifdef WITH_ACCOUNTING
1442 { "acct", RAD_LISTEN_ACCT },
1445 { "detail", RAD_LISTEN_DETAIL },
1448 { "proxy", RAD_LISTEN_PROXY },
1451 { "vmps", RAD_LISTEN_VQP },
1454 { "dhcp", RAD_LISTEN_DHCP },
1456 #ifdef WITH_COMMAND_SOCKET
1457 { "control", RAD_LISTEN_COMMAND },
1463 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1471 cf_log_info(cs, "listen {");
1473 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1475 if (rcode < 0) return NULL;
1478 cf_log_err(cf_sectiontoitem(cs),
1479 "No type specified in listen section");
1483 type = fr_str2int(listen_compare, listen_type, -1);
1485 cf_log_err(cf_sectiontoitem(cs),
1486 "Invalid type \"%s\" in listen section.",
1494 * Allow listen sections in the default config to
1495 * refer to a server.
1498 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1500 if (rcode == 1) { /* compatiblity with 2.0-pre */
1501 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1504 if (rcode < 0) return NULL;
1508 * Set up cross-type data.
1510 this = listen_alloc(type);
1511 this->server = server;
1515 * Call per-type parser.
1517 if (master_listen[type].parse(cs, this) < 0) {
1522 cf_log_info(cs, "}");
1528 * Generate a list of listeners. Takes an input list of
1529 * listeners, too, so we don't close sockets with waiting packets.
1531 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1533 int override = FALSE;
1535 CONF_SECTION *cs = NULL;
1536 rad_listen_t **last;
1538 fr_ipaddr_t server_ipaddr;
1541 int defined_proxy = 0;
1545 * We shouldn't be called with a pre-existing list.
1547 rad_assert(head && (*head == NULL));
1550 server_ipaddr.af = AF_UNSPEC;
1553 * If the port is specified on the command-line,
1554 * it over-rides the configuration file.
1556 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1558 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1561 * If the IP address was configured on the command-line,
1562 * use that as the "bind_address"
1564 if (mainconfig.myip.af != AF_UNSPEC) {
1565 memcpy(&server_ipaddr, &mainconfig.myip,
1566 sizeof(server_ipaddr));
1572 * Else look for bind_address and/or listen sections.
1574 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1575 rcode = cf_item_parse(config, "bind_address",
1577 &server_ipaddr.ipaddr.ip4addr, NULL);
1578 if (rcode < 0) return -1; /* error parsing it */
1580 if (rcode == 0) { /* successfully parsed IPv4 */
1581 listen_socket_t *sock;
1582 server_ipaddr.af = AF_INET;
1584 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'.");
1588 if (strcmp(progname, "vmpsd") == 0) {
1589 this = listen_alloc(RAD_LISTEN_VQP);
1590 if (!auth_port) auth_port = 1589;
1593 this = listen_alloc(RAD_LISTEN_AUTH);
1597 sock->ipaddr = server_ipaddr;
1598 sock->port = auth_port;
1600 sock->clients = clients_parse_section(config);
1601 if (!sock->clients) {
1602 cf_log_err(cf_sectiontoitem(config),
1603 "Failed to find any clients for this listen section");
1608 if (listen_bind(this) < 0) {
1610 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1614 auth_port = sock->port; /* may have been updated in listen_bind */
1616 cs = cf_section_sub_find_name2(config, "server",
1618 if (cs) this->server = mainconfig.name;
1622 last = &(this->next);
1628 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
1631 #ifdef WITH_ACCOUNTING
1633 * Open Accounting Socket.
1635 * If we haven't already gotten acct_port from
1636 * /etc/services, then make it auth_port + 1.
1638 this = listen_alloc(RAD_LISTEN_ACCT);
1642 * Create the accounting socket.
1644 * The accounting port is always the
1645 * authentication port + 1
1647 sock->ipaddr = server_ipaddr;
1648 sock->port = auth_port + 1;
1650 sock->clients = clients_parse_section(config);
1651 if (!sock->clients) {
1652 cf_log_err(cf_sectiontoitem(config),
1653 "Failed to find any clients for this listen section");
1657 if (listen_bind(this) < 0) {
1660 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1665 cs = cf_section_sub_find_name2(config, "server",
1667 if (cs) this->server = mainconfig.name;
1671 last = &(this->next);
1673 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1674 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1680 * They specified an IP on the command-line, ignore
1681 * all listen sections except the one in '-n'.
1683 if (mainconfig.myip.af != AF_UNSPEC) {
1684 CONF_SECTION *subcs;
1685 const char *name2 = cf_section_name2(cs);
1687 cs = cf_section_sub_find_name2(config, "server",
1689 if (!cs) goto do_proxy;
1692 * Should really abstract this code...
1694 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1696 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1697 this = listen_parse(subcs, name2);
1704 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1708 last = &(this->next);
1709 } /* loop over "listen" directives in server <foo> */
1715 * Walk through the "listen" sections, if they exist.
1717 for (cs = cf_subsection_find_next(config, NULL, "listen");
1719 cs = cf_subsection_find_next(config, cs, "listen")) {
1720 this = listen_parse(cs, NULL);
1727 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1731 last = &(this->next);
1735 * Check virtual servers for "listen" sections, too.
1737 * FIXME: Move to virtual server init?
1739 for (cs = cf_subsection_find_next(config, NULL, "server");
1741 cs = cf_subsection_find_next(config, cs, "server")) {
1742 CONF_SECTION *subcs;
1743 const char *name2 = cf_section_name2(cs);
1745 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1747 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1748 this = listen_parse(subcs, name2);
1755 if (this->type == RAD_LISTEN_PROXY) {
1756 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
1763 last = &(this->next);
1764 } /* loop over "listen" directives in virtual servers */
1765 } /* loop over virtual servers */
1768 * If we're proxying requests, open the proxy FD.
1769 * Otherwise, don't do anything.
1773 if (mainconfig.proxy_requests == TRUE) {
1775 listen_socket_t *sock = NULL;
1778 * No sockets to receive packets, therefore
1779 * proxying is pointless.
1781 if (!*head) return -1;
1783 if (defined_proxy) goto done;
1786 * Find the first authentication port,
1789 for (this = *head; this != NULL; this = this->next) {
1790 if (this->type == RAD_LISTEN_AUTH) {
1792 if (server_ipaddr.af == AF_UNSPEC) {
1793 server_ipaddr = sock->ipaddr;
1795 port = sock->port + 2; /* skip acct port */
1798 if (this->type == RAD_LISTEN_VQP) {
1800 if (server_ipaddr.af == AF_UNSPEC) {
1801 server_ipaddr = sock->ipaddr;
1803 port = sock->port + 1;
1808 if (port < 0) port = 1024 + (fr_rand() & 0x1ff);
1811 * Address is still unspecified, use IPv4.
1813 if (server_ipaddr.af == AF_UNSPEC) {
1814 server_ipaddr.af = AF_INET;
1815 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1818 this = listen_alloc(RAD_LISTEN_PROXY);
1822 * Create the first proxy socket.
1824 sock->ipaddr = server_ipaddr;
1827 * Try to find a proxy port (value doesn't matter)
1829 for (sock->port = port;
1832 if (listen_bind(this) == 0) {
1834 last = &(this->next); /* just in case */
1839 if (sock->port >= 64000) {
1842 radlog(L_ERR, "Failed to open socket for proxying");
1847 done: /* used only in proxy code. */
1854 * Free a linked list of listeners;
1856 void listen_free(rad_listen_t **head)
1860 if (!head || !*head) return;
1864 rad_listen_t *next = this->next;
1867 * Other code may have eaten the FD.
1869 if (this->fd >= 0) close(this->fd);
1871 if (master_listen[this->type].free) {
1872 master_listen[this->type].free(this);
1884 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
1889 for (this = mainconfig.listen; this != NULL; this = this->next) {
1890 listen_socket_t *sock;
1892 if ((this->type != RAD_LISTEN_AUTH) &&
1893 (this->type != RAD_LISTEN_ACCT)) continue;
1897 if ((sock->port == port) &&
1898 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {
1899 return sock->clients;
1906 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
1910 for (this = mainconfig.listen; this != NULL; this = this->next) {
1911 listen_socket_t *sock;
1913 if ((this->type != RAD_LISTEN_AUTH) &&
1914 (this->type != RAD_LISTEN_ACCT)) continue;
1918 if ((sock->port == port) &&
1919 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {