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 rad_assert((listener->type == RAD_LISTEN_AUTH)
100 || (listener->type == RAD_LISTEN_NONE)
102 #ifdef WITH_ACCOUNTING
103 || (listener->type == RAD_LISTEN_ACCT)
106 || (listener->type == RAD_LISTEN_VQP)
109 || (listener->type == RAD_LISTEN_DHCP)
113 clients = ((listen_socket_t *)listener->data)->clients;
116 * This HAS to have been initialized previously.
118 rad_assert(clients != NULL);
120 client = client_find(clients, ipaddr);
122 static time_t last_printed = 0;
123 char name[256], buffer[128];
125 #ifdef WITH_DYNAMIC_CLIENTS
126 unknown: /* used only for dynamic clients */
130 * DoS attack quenching, but only in debug mode.
131 * If they're running in debug mode, show them
134 if (debug_flag == 0) {
136 if (last_printed == now) return NULL;
141 listener->print(listener, name, sizeof(name));
143 radlog(L_ERR, "Ignoring request to %s from unknown client %s port %d",
144 name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
145 buffer, sizeof(buffer)),
150 #ifndef WITH_DYNAMIC_CLIENTS
151 return client; /* return the found client. */
155 * No server defined, and it's not dynamic. Return it.
157 if (!client->client_server && !client->dynamic) return client;
162 * It's a dynamically generated client, check it.
164 if (client->dynamic) {
166 * Lives forever. Return it.
168 if (client->lifetime == 0) return client;
171 * Rate-limit the deletion of known clients.
172 * This makes them last a little longer, but
173 * prevents the server from melting down if (say)
174 * 10k clients all expire at once.
176 if (now == client->last_new_client) return client;
179 * It's not dead yet. Return it.
181 if ((client->created + client->lifetime) > now) return client;
184 * FIXME: Don't delete it until after the last
185 * request using it has expired. i.e. keep a
186 * "use counter" (ugh). Why can't we use a
187 * language with garbage collection?
190 client_delete(clients, client);
193 * Go find the enclosing network again.
195 client = client_find(clients, ipaddr);
200 if (!client) goto unknown;
201 if (!client->client_server) goto unknown;
204 * At this point, 'client' is the enclosing
205 * network that configures where dynamic clients
208 rad_assert(client->dynamic == 0);
211 * The IP is unknown, so we've found an enclosing
212 * network. Enable DoS protection. We only
213 * allow one new client per second. Known
214 * clients aren't subject to this restriction.
216 if (now == client->last_new_client) goto unknown;
219 client->last_new_client = now;
221 request = request_alloc();
222 if (!request) goto unknown;
224 request->listener = listener;
225 request->client = client;
226 request->packet = rad_alloc(0);
227 if (!request->packet) {
228 request_free(&request);
231 request->reply = rad_alloc(0);
232 if (!request->reply) {
233 request_free(&request);
236 request->packet->timestamp = request->timestamp;
238 request->priority = listener->type;
239 request->server = client->client_server;
240 request->root = &mainconfig;
243 * Run a fake request through the given virtual server.
244 * Look for FreeRADIUS-Client-IP-Address
245 * FreeRADIUS-Client-Secret
248 * and create the RADCLIENT structure from that.
251 sock = listener->data;
252 request->packet->sockfd = listener->fd;
253 request->packet->src_ipaddr = *ipaddr;
254 request->packet->src_port = 0; /* who cares... */
255 request->packet->dst_ipaddr = sock->ipaddr;
256 request->packet->dst_port = sock->port;
258 request->reply->sockfd = request->packet->sockfd;
259 request->reply->dst_ipaddr = request->packet->src_ipaddr;
260 request->reply->src_ipaddr = request->packet->dst_ipaddr;
261 request->reply->dst_port = request->packet->src_port;
262 request->reply->src_port = request->packet->dst_port;
263 request->reply->id = request->packet->id;
264 request->reply->code = 0; /* UNKNOWN code */
267 DEBUG("server %s {", request->server);
269 rcode = module_authorize(0, request);
271 DEBUG("} # server %s", request->server);
273 if (rcode != RLM_MODULE_OK) {
274 request_free(&request);
279 * If the client was updated by rlm_dynamic_clients,
280 * don't create the client from attribute-value pairs.
282 if (request->client == client) {
283 created = client_create(clients, request);
285 created = request->client;
288 * This frees the client if it isn't valid.
290 if (!client_validate(clients, client, created)) goto unknown;
292 request_free(&request);
294 if (!created) goto unknown;
300 static int listen_bind(rad_listen_t *this);
304 * Process and reply to a server-status request.
305 * Like rad_authenticate and rad_accounting this should
306 * live in it's own file but it's so small we don't bother.
308 static int rad_status_server(REQUEST *request)
310 int rcode = RLM_MODULE_OK;
313 switch (request->listener->type) {
315 case RAD_LISTEN_NONE:
317 case RAD_LISTEN_AUTH:
318 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
320 rcode = module_authorize(dval->value, request);
322 rcode = RLM_MODULE_OK;
327 case RLM_MODULE_UPDATED:
328 request->reply->code = PW_AUTHENTICATION_ACK;
331 case RLM_MODULE_FAIL:
332 case RLM_MODULE_HANDLED:
333 request->reply->code = 0; /* don't reply */
337 case RLM_MODULE_REJECT:
338 request->reply->code = PW_AUTHENTICATION_REJECT;
343 #ifdef WITH_ACCOUNTING
344 case RAD_LISTEN_ACCT:
345 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
347 rcode = module_accounting(dval->value, request);
349 rcode = RLM_MODULE_OK;
354 case RLM_MODULE_UPDATED:
355 request->reply->code = PW_ACCOUNTING_RESPONSE;
359 request->reply->code = 0; /* don't reply */
371 * Full statistics are available only on a statistics
374 if (request->listener->type == RAD_LISTEN_NONE) {
375 request_stats_reply(request);
383 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
386 listen_socket_t *sock = this->data;
389 switch (this->type) {
391 case RAD_LISTEN_NONE: /* what a hack... */
396 case RAD_LISTEN_AUTH:
397 name = "authentication";
400 #ifdef WITH_ACCOUNTING
401 case RAD_LISTEN_ACCT:
407 case RAD_LISTEN_PROXY:
419 case RAD_LISTEN_DHCP:
429 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
430 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
434 #ifdef SO_BINDTODEVICE
435 if (sock->interface) {
436 ADDSTRING(" interface ");
437 ADDSTRING(sock->interface);
441 ADDSTRING(" address ");
443 if ((sock->ipaddr.af == AF_INET) &&
444 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
445 strlcpy(buffer, "*", bufsize);
447 ip_ntoh(&sock->ipaddr, buffer, bufsize);
452 snprintf(buffer, bufsize, "%d", sock->port);
456 ADDSTRING(" as server ");
457 ADDSTRING(this->server);
468 * Parse an authentication or accounting socket.
470 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
475 listen_socket_t *sock = this->data;
476 char *section_name = NULL;
477 CONF_SECTION *client_cs, *parentcs;
482 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
483 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
484 &ipaddr.ipaddr.ip4addr, NULL);
485 if (rcode < 0) return -1;
487 if (rcode == 0) { /* successfully parsed IPv4 */
490 } else { /* maybe IPv6? */
491 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
492 &ipaddr.ipaddr.ip6addr, NULL);
493 if (rcode < 0) return -1;
496 cf_log_err(cf_sectiontoitem(cs),
497 "No address specified in listen section");
500 ipaddr.af = AF_INET6;
503 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
505 if (rcode < 0) return -1;
507 if ((listen_port < 0) || (listen_port > 65535)) {
508 cf_log_err(cf_sectiontoitem(cs),
509 "Invalid value for \"port\"");
513 sock->ipaddr = ipaddr;
514 sock->port = listen_port;
517 * If we can bind to interfaces, do so,
520 if (cf_pair_find(cs, "interface")) {
521 #ifndef SO_BINDTODEVICE
522 cf_log_err(cf_sectiontoitem(cs),
523 "System does not support binding to interfaces. Delete this line from the configuration file.");
527 CONF_PAIR *cp = cf_pair_find(cs, "interface");
529 rad_assert(cp != NULL);
530 value = cf_pair_value(cp);
532 cf_log_err(cf_sectiontoitem(cs),
533 "No interface name given");
536 sock->interface = value;
541 * And bind it to the port.
543 if (listen_bind(this) < 0) {
545 cf_log_err(cf_sectiontoitem(cs),
546 "Error binding to port for %s port %d",
547 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
554 * Proxy sockets don't have clients.
556 if (this->type == RAD_LISTEN_PROXY) return 0;
560 * The more specific configurations are preferred to more
564 parentcs = cf_top_section(cs);
565 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
566 §ion_name, NULL);
567 if (rcode < 0) return -1; /* bad string */
570 * Explicit list given: use it.
572 client_cs = cf_section_sub_find_name2(parentcs,
576 client_cs = cf_section_find(section_name);
579 cf_log_err(cf_sectiontoitem(cs),
580 "Failed to find clients %s {...}",
586 } /* else there was no "clients = " entry. */
589 CONF_SECTION *server_cs;
591 server_cs = cf_section_sub_find_name2(parentcs,
595 * Found a "server foo" section. If there are clients
599 (cf_section_sub_find(server_cs, "client") != NULL)) {
600 client_cs = server_cs;
605 * Still nothing. Look for global clients.
607 if (!client_cs) client_cs = parentcs;
609 sock->clients = clients_parse_section(client_cs);
610 if (!sock->clients) {
611 cf_log_err(cf_sectiontoitem(cs),
612 "Failed to load clients for this listen section");
620 * Send an authentication response packet
622 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
624 rad_assert(request->listener == listener);
625 rad_assert(listener->send == auth_socket_send);
627 return rad_send(request->reply, request->packet,
628 request->client->secret);
632 #ifdef WITH_ACCOUNTING
634 * Send an accounting response packet (or not)
636 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
638 rad_assert(request->listener == listener);
639 rad_assert(listener->send == acct_socket_send);
642 * Accounting reject's are silently dropped.
644 * We do it here to avoid polluting the rest of the
645 * code with this knowledge
647 if (request->reply->code == 0) return 0;
649 return rad_send(request->reply, request->packet,
650 request->client->secret);
656 * Send a packet to a home server.
658 * FIXME: have different code for proxy auth & acct!
660 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
662 listen_socket_t *sock = listener->data;
664 rad_assert(request->proxy_listener == listener);
665 rad_assert(listener->send == proxy_socket_send);
667 request->proxy->src_ipaddr = sock->ipaddr;
668 request->proxy->src_port = sock->port;
670 return rad_send(request->proxy, request->packet,
671 request->home_server->secret);
677 * Check if an incoming request is "ok"
679 * It takes packets, not requests. It sees if the packet looks
680 * OK. If so, it does a number of sanity checks on it.
682 static int stats_socket_recv(rad_listen_t *listener,
683 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
687 RADIUS_PACKET *packet;
689 fr_ipaddr_t src_ipaddr;
691 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
692 if (rcode < 0) return 0;
694 RAD_STATS_TYPE_INC(listener, total_requests);
696 if (rcode < 20) { /* AUTH_HDR_LEN */
697 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
701 if ((client = client_listener_find(listener,
702 &src_ipaddr, src_port)) == NULL) {
703 rad_recv_discard(listener->fd);
704 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
709 * We only understand Status-Server on this socket.
711 if (code != PW_STATUS_SERVER) {
712 DEBUG("Ignoring packet code %d sent to Status-Server port",
714 rad_recv_discard(listener->fd);
715 RAD_STATS_TYPE_INC(listener, total_unknown_types);
716 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
721 * Now that we've sanity checked everything, receive the
724 packet = rad_recv(listener->fd, 1); /* require message authenticator */
726 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
727 DEBUG("%s", fr_strerror());
731 if (!received_request(listener, packet, prequest, client)) {
732 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
733 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
738 *pfun = rad_status_server;
745 * Check if an incoming request is "ok"
747 * It takes packets, not requests. It sees if the packet looks
748 * OK. If so, it does a number of sanity checks on it.
750 static int auth_socket_recv(rad_listen_t *listener,
751 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
755 RADIUS_PACKET *packet;
756 RAD_REQUEST_FUNP fun = NULL;
758 fr_ipaddr_t src_ipaddr;
760 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
761 if (rcode < 0) return 0;
763 RAD_STATS_TYPE_INC(listener, total_requests);
765 if (rcode < 20) { /* AUTH_HDR_LEN */
766 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
770 if ((client = client_listener_find(listener,
771 &src_ipaddr, src_port)) == NULL) {
772 rad_recv_discard(listener->fd);
773 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
778 * Some sanity checks, based on the packet code.
781 case PW_AUTHENTICATION_REQUEST:
782 RAD_STATS_CLIENT_INC(listener, client, total_requests);
783 fun = rad_authenticate;
786 case PW_STATUS_SERVER:
787 if (!mainconfig.status_server) {
788 rad_recv_discard(listener->fd);
789 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
790 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
791 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
794 fun = rad_status_server;
798 rad_recv_discard(listener->fd);
799 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
800 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
802 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
803 code, client->shortname, src_port);
806 } /* switch over packet types */
809 * Now that we've sanity checked everything, receive the
812 packet = rad_recv(listener->fd, client->message_authenticator);
814 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
815 DEBUG("%s", fr_strerror());
819 if (!received_request(listener, packet, prequest, client)) {
820 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
821 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
831 #ifdef WITH_ACCOUNTING
833 * Receive packets from an accounting socket
835 static int acct_socket_recv(rad_listen_t *listener,
836 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
840 RADIUS_PACKET *packet;
841 RAD_REQUEST_FUNP fun = NULL;
843 fr_ipaddr_t src_ipaddr;
845 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
846 if (rcode < 0) return 0;
848 RAD_STATS_TYPE_INC(listener, total_requests);
850 if (rcode < 20) { /* AUTH_HDR_LEN */
851 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
855 if ((client = client_listener_find(listener,
856 &src_ipaddr, src_port)) == NULL) {
857 rad_recv_discard(listener->fd);
858 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
863 * Some sanity checks, based on the packet code.
866 case PW_ACCOUNTING_REQUEST:
867 RAD_STATS_CLIENT_INC(listener, client, total_requests);
868 fun = rad_accounting;
871 case PW_STATUS_SERVER:
872 if (!mainconfig.status_server) {
873 rad_recv_discard(listener->fd);
874 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
875 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
877 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
880 fun = rad_status_server;
884 rad_recv_discard(listener->fd);
885 RAD_STATS_TYPE_INC(listener, total_unknown_types);
886 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
888 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
889 code, client->shortname, src_port);
891 } /* switch over packet types */
894 * Now that we've sanity checked everything, receive the
897 packet = rad_recv(listener->fd, 0);
899 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
900 radlog(L_ERR, "%s", fr_strerror());
905 * There can be no duplicate accounting packets.
907 if (!received_request(listener, packet, prequest, client)) {
908 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
909 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
922 * For now, all CoA requests are *only* originated, and not
923 * proxied. So all of the necessary work is done in the
924 * post-proxy section, which is automatically handled by event.c.
925 * As a result, we don't have to do anything here.
927 static int rad_coa_reply(REQUEST *request)
932 * Inform the user about RFC requirements.
934 s1 = pairfind(request->proxy->vps, PW_STATE);
936 s2 = pairfind(request->proxy_reply->vps, PW_STATE);
939 DEBUG("WARNING: Client was sent State in CoA, and did not respond with State.");
941 } else if ((s1->length != s2->length) ||
942 (memcmp(s1->vp_octets, s2->vp_octets,
944 DEBUG("WARNING: Client was sent State in CoA, and did not respond with the same State.");
948 return RLM_MODULE_OK;
954 * Recieve packets from a proxy socket.
956 static int proxy_socket_recv(rad_listen_t *listener,
957 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
960 RADIUS_PACKET *packet;
961 RAD_REQUEST_FUNP fun = NULL;
964 packet = rad_recv(listener->fd, 0);
966 radlog(L_ERR, "%s", fr_strerror());
971 * FIXME: Client MIB updates?
973 switch(packet->code) {
974 case PW_AUTHENTICATION_ACK:
975 case PW_ACCESS_CHALLENGE:
976 case PW_AUTHENTICATION_REJECT:
977 fun = rad_authenticate;
980 #ifdef WITH_ACCOUNTING
981 case PW_ACCOUNTING_RESPONSE:
982 fun = rad_accounting;
987 case PW_DISCONNECT_ACK:
988 case PW_DISCONNECT_NAK:
997 * FIXME: Update MIB for packet types?
999 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1000 "from home server %s port %d - ID %d : IGNORED",
1002 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1003 packet->src_port, packet->id);
1008 request = received_proxy_response(packet);
1014 rad_assert(fun != NULL);
1016 *prequest = request;
1023 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1025 if (!request->reply->code) return 0;
1027 rad_encode(request->reply, request->packet,
1028 request->client->secret);
1029 rad_sign(request->reply, request->packet,
1030 request->client->secret);
1036 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1038 if (rad_verify(request->packet, NULL,
1039 request->client->secret) < 0) {
1043 return rad_decode(request->packet, NULL,
1044 request->client->secret);
1048 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1050 rad_encode(request->proxy, NULL, request->home_server->secret);
1051 rad_sign(request->proxy, NULL, request->home_server->secret);
1057 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1059 if (rad_verify(request->proxy_reply, request->proxy,
1060 request->home_server->secret) < 0) {
1064 return rad_decode(request->proxy_reply, request->proxy,
1065 request->home_server->secret);
1071 #include "command.c"
1073 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1075 { common_socket_parse, NULL,
1076 stats_socket_recv, auth_socket_send,
1077 socket_print, client_socket_encode, client_socket_decode },
1080 * This always gets defined.
1082 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1087 { common_socket_parse, NULL,
1088 proxy_socket_recv, proxy_socket_send,
1089 socket_print, proxy_socket_encode, proxy_socket_decode },
1092 /* authentication */
1093 { common_socket_parse, NULL,
1094 auth_socket_recv, auth_socket_send,
1095 socket_print, client_socket_encode, client_socket_decode },
1097 #ifdef WITH_ACCOUNTING
1099 { common_socket_parse, NULL,
1100 acct_socket_recv, acct_socket_send,
1101 socket_print, client_socket_encode, client_socket_decode},
1106 { detail_parse, detail_free,
1107 detail_recv, detail_send,
1108 detail_print, detail_encode, detail_decode },
1112 /* vlan query protocol */
1113 { common_socket_parse, NULL,
1114 vqp_socket_recv, vqp_socket_send,
1115 socket_print, vqp_socket_encode, vqp_socket_decode },
1119 /* dhcp query protocol */
1120 { dhcp_socket_parse, NULL,
1121 dhcp_socket_recv, dhcp_socket_send,
1122 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1125 #ifdef WITH_COMMAND_SOCKET
1126 /* TCP command socket */
1127 { command_socket_parse, NULL,
1128 command_domain_accept, command_domain_send,
1129 command_socket_print, command_socket_encode, command_socket_decode },
1137 * Binds a listener to a socket.
1139 static int listen_bind(rad_listen_t *this)
1142 struct sockaddr_storage salocal;
1144 listen_socket_t *sock = this->data;
1147 * If the port is zero, then it means the appropriate
1148 * thing from /etc/services.
1150 if (sock->port == 0) {
1151 struct servent *svp;
1153 switch (this->type) {
1154 case RAD_LISTEN_AUTH:
1155 svp = getservbyname ("radius", "udp");
1157 sock->port = ntohs(svp->s_port);
1159 sock->port = PW_AUTH_UDP_PORT;
1163 #ifdef WITH_ACCOUNTING
1164 case RAD_LISTEN_ACCT:
1165 svp = getservbyname ("radacct", "udp");
1167 sock->port = ntohs(svp->s_port);
1169 sock->port = PW_ACCT_UDP_PORT;
1175 case RAD_LISTEN_PROXY:
1181 case RAD_LISTEN_VQP:
1187 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1193 * Copy fr_socket() here, as we may need to bind to a device.
1195 this->fd = socket(sock->ipaddr.af, SOCK_DGRAM, 0);
1197 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
1201 #ifdef SO_BINDTODEVICE
1203 * Bind to a device BEFORE touching IP addresses.
1205 if (sock->interface) {
1207 strcpy(ifreq.ifr_name, sock->interface);
1210 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1211 (char *)&ifreq, sizeof(ifreq));
1215 radlog(L_ERR, "Failed binding to interface %s: %s",
1216 sock->interface, strerror(errno));
1218 } /* else it worked. */
1222 #ifdef WITH_UDPFROMTO
1224 * Initialize udpfromto for all sockets.
1226 if (udpfromto_init(this->fd) != 0) {
1233 * Set up sockaddr stuff.
1235 if (!fr_ipaddr2sockaddr(&sock->ipaddr, sock->port, &salocal, &salen)) {
1240 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1241 if (sock->ipaddr.af == AF_INET6) {
1243 * Listening on '::' does NOT get you IPv4 to
1244 * IPv6 mapping. You've got to listen on an IPv4
1245 * address, too. This makes the rest of the server
1246 * design a little simpler.
1250 if (IN6_IS_ADDR_UNSPECIFIED(&sock->ipaddr.ipaddr.ip6addr)) {
1253 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
1254 (char *)&on, sizeof(on));
1256 #endif /* IPV6_V6ONLY */
1258 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
1261 * May be binding to priviledged ports.
1264 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
1268 radlog(L_ERR, "Failed binding to socket: %s\n",
1274 * FreeBSD jail issues. We bind to 0.0.0.0, but the
1275 * kernel instead binds us to a 1.2.3.4. If this
1276 * happens, notice, and remember our real IP.
1279 struct sockaddr_storage src;
1280 socklen_t sizeof_src = sizeof(src);
1282 memset(&src, 0, sizeof_src);
1283 if (getsockname(this->fd, (struct sockaddr *) &src,
1285 radlog(L_ERR, "Failed getting socket name: %s",
1290 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
1291 &sock->ipaddr, &sock->port)) {
1292 radlog(L_ERR, "Socket has unsupported address family");
1301 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
1302 radlog(L_ERR, "Failure getting socket flags: %s)\n",
1307 flags |= O_NONBLOCK;
1308 if( fcntl(this->fd, F_SETFL, flags) < 0) {
1309 radlog(L_ERR, "Failure setting socket flags: %s)\n",
1321 * Allocate & initialize a new listener.
1323 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
1327 this = rad_malloc(sizeof(*this));
1328 memset(this, 0, sizeof(*this));
1331 this->recv = master_listen[this->type].recv;
1332 this->send = master_listen[this->type].send;
1333 this->print = master_listen[this->type].print;
1334 this->encode = master_listen[this->type].encode;
1335 this->decode = master_listen[this->type].decode;
1339 case RAD_LISTEN_NONE:
1341 case RAD_LISTEN_AUTH:
1342 #ifdef WITH_ACCOUNTING
1343 case RAD_LISTEN_ACCT:
1346 case RAD_LISTEN_PROXY:
1349 case RAD_LISTEN_VQP:
1352 case RAD_LISTEN_DHCP:
1354 this->data = rad_malloc(sizeof(listen_socket_t));
1355 memset(this->data, 0, sizeof(listen_socket_t));
1359 case RAD_LISTEN_DETAIL:
1364 #ifdef WITH_COMMAND_SOCKET
1365 case RAD_LISTEN_COMMAND:
1366 this->data = rad_malloc(sizeof(fr_command_socket_t));
1367 memset(this->data, 0, sizeof(fr_command_socket_t));
1372 rad_assert("Unsupported option!" == NULL);
1382 * Externally visible function for creating a new proxy LISTENER.
1384 * For now, don't take ipaddr or port.
1386 * Not thread-safe, but all calls to it are protected by the
1387 * proxy mutex in request_list.c
1389 rad_listen_t *proxy_new_listener()
1391 int last_proxy_port, port;
1392 rad_listen_t *this, *tmp, **last;
1393 listen_socket_t *sock, *old;
1395 this = listen_alloc(RAD_LISTEN_PROXY);
1398 * Find an existing proxy socket to copy.
1400 * FIXME: Make it per-realm, or per-home server!
1402 last_proxy_port = 0;
1404 last = &mainconfig.listen;
1405 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1406 if (tmp->type == RAD_LISTEN_PROXY) {
1408 if (sock->port > last_proxy_port) {
1409 last_proxy_port = sock->port + 1;
1411 if (!old) old = sock;
1414 last = &(tmp->next);
1419 return NULL; /* This is a serious error. */
1423 * FIXME: find a new IP address to listen on?
1425 * This could likely be done in the "home server"
1426 * configuration, to have per-home-server source IP's.
1429 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1432 * Keep going until we find an unused port.
1434 for (port = last_proxy_port; port < 64000; port++) {
1436 if (listen_bind(this) == 0) {
1438 * Add the new listener to the list of
1451 static const FR_NAME_NUMBER listen_compare[] = {
1453 { "status", RAD_LISTEN_NONE },
1455 { "auth", RAD_LISTEN_AUTH },
1456 #ifdef WITH_ACCOUNTING
1457 { "acct", RAD_LISTEN_ACCT },
1460 { "detail", RAD_LISTEN_DETAIL },
1463 { "proxy", RAD_LISTEN_PROXY },
1466 { "vmps", RAD_LISTEN_VQP },
1469 { "dhcp", RAD_LISTEN_DHCP },
1471 #ifdef WITH_COMMAND_SOCKET
1472 { "control", RAD_LISTEN_COMMAND },
1478 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1486 cf_log_info(cs, "listen {");
1488 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1490 if (rcode < 0) return NULL;
1493 cf_log_err(cf_sectiontoitem(cs),
1494 "No type specified in listen section");
1498 type = fr_str2int(listen_compare, listen_type, -1);
1500 cf_log_err(cf_sectiontoitem(cs),
1501 "Invalid type \"%s\" in listen section.",
1509 * Allow listen sections in the default config to
1510 * refer to a server.
1513 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1515 if (rcode == 1) { /* compatiblity with 2.0-pre */
1516 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1519 if (rcode < 0) return NULL;
1523 * Set up cross-type data.
1525 this = listen_alloc(type);
1526 this->server = server;
1530 * Call per-type parser.
1532 if (master_listen[type].parse(cs, this) < 0) {
1537 cf_log_info(cs, "}");
1543 * Generate a list of listeners. Takes an input list of
1544 * listeners, too, so we don't close sockets with waiting packets.
1546 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1548 int override = FALSE;
1550 CONF_SECTION *cs = NULL;
1551 rad_listen_t **last;
1553 fr_ipaddr_t server_ipaddr;
1556 int defined_proxy = 0;
1560 * We shouldn't be called with a pre-existing list.
1562 rad_assert(head && (*head == NULL));
1565 server_ipaddr.af = AF_UNSPEC;
1568 * If the port is specified on the command-line,
1569 * it over-rides the configuration file.
1571 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1573 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1576 * If the IP address was configured on the command-line,
1577 * use that as the "bind_address"
1579 if (mainconfig.myip.af != AF_UNSPEC) {
1580 memcpy(&server_ipaddr, &mainconfig.myip,
1581 sizeof(server_ipaddr));
1587 * Else look for bind_address and/or listen sections.
1589 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1590 rcode = cf_item_parse(config, "bind_address",
1592 &server_ipaddr.ipaddr.ip4addr, NULL);
1593 if (rcode < 0) return -1; /* error parsing it */
1595 if (rcode == 0) { /* successfully parsed IPv4 */
1596 listen_socket_t *sock;
1597 server_ipaddr.af = AF_INET;
1599 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'.");
1603 if (strcmp(progname, "vmpsd") == 0) {
1604 this = listen_alloc(RAD_LISTEN_VQP);
1605 if (!auth_port) auth_port = 1589;
1608 this = listen_alloc(RAD_LISTEN_AUTH);
1612 sock->ipaddr = server_ipaddr;
1613 sock->port = auth_port;
1615 sock->clients = clients_parse_section(config);
1616 if (!sock->clients) {
1617 cf_log_err(cf_sectiontoitem(config),
1618 "Failed to find any clients for this listen section");
1623 if (listen_bind(this) < 0) {
1625 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1629 auth_port = sock->port; /* may have been updated in listen_bind */
1631 cs = cf_section_sub_find_name2(config, "server",
1633 if (cs) this->server = mainconfig.name;
1637 last = &(this->next);
1643 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
1646 #ifdef WITH_ACCOUNTING
1648 * Open Accounting Socket.
1650 * If we haven't already gotten acct_port from
1651 * /etc/services, then make it auth_port + 1.
1653 this = listen_alloc(RAD_LISTEN_ACCT);
1657 * Create the accounting socket.
1659 * The accounting port is always the
1660 * authentication port + 1
1662 sock->ipaddr = server_ipaddr;
1663 sock->port = auth_port + 1;
1665 sock->clients = clients_parse_section(config);
1666 if (!sock->clients) {
1667 cf_log_err(cf_sectiontoitem(config),
1668 "Failed to find any clients for this listen section");
1672 if (listen_bind(this) < 0) {
1675 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1680 cs = cf_section_sub_find_name2(config, "server",
1682 if (cs) this->server = mainconfig.name;
1686 last = &(this->next);
1688 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1689 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1695 * They specified an IP on the command-line, ignore
1696 * all listen sections except the one in '-n'.
1698 if (mainconfig.myip.af != AF_UNSPEC) {
1699 CONF_SECTION *subcs;
1700 const char *name2 = cf_section_name2(cs);
1702 cs = cf_section_sub_find_name2(config, "server",
1704 if (!cs) goto do_proxy;
1707 * Should really abstract this code...
1709 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1711 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1712 this = listen_parse(subcs, name2);
1719 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1723 last = &(this->next);
1724 } /* loop over "listen" directives in server <foo> */
1730 * Walk through the "listen" sections, if they exist.
1732 for (cs = cf_subsection_find_next(config, NULL, "listen");
1734 cs = cf_subsection_find_next(config, cs, "listen")) {
1735 this = listen_parse(cs, NULL);
1742 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1746 last = &(this->next);
1750 * Check virtual servers for "listen" sections, too.
1752 * FIXME: Move to virtual server init?
1754 for (cs = cf_subsection_find_next(config, NULL, "server");
1756 cs = cf_subsection_find_next(config, cs, "server")) {
1757 CONF_SECTION *subcs;
1758 const char *name2 = cf_section_name2(cs);
1760 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1762 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1763 this = listen_parse(subcs, name2);
1770 if (this->type == RAD_LISTEN_PROXY) {
1771 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
1778 last = &(this->next);
1779 } /* loop over "listen" directives in virtual servers */
1780 } /* loop over virtual servers */
1783 * If we're proxying requests, open the proxy FD.
1784 * Otherwise, don't do anything.
1788 if (mainconfig.proxy_requests == TRUE) {
1790 listen_socket_t *sock = NULL;
1793 * No sockets to receive packets, therefore
1794 * proxying is pointless.
1796 if (!*head) return -1;
1798 if (defined_proxy) goto done;
1801 * Find the first authentication port,
1804 for (this = *head; this != NULL; this = this->next) {
1805 if (this->type == RAD_LISTEN_AUTH) {
1807 if (server_ipaddr.af == AF_UNSPEC) {
1808 server_ipaddr = sock->ipaddr;
1810 port = sock->port + 2; /* skip acct port */
1813 if (this->type == RAD_LISTEN_VQP) {
1815 if (server_ipaddr.af == AF_UNSPEC) {
1816 server_ipaddr = sock->ipaddr;
1818 port = sock->port + 1;
1823 if (port < 0) port = 1024 + (fr_rand() & 0x1ff);
1826 * Address is still unspecified, use IPv4.
1828 if (server_ipaddr.af == AF_UNSPEC) {
1829 server_ipaddr.af = AF_INET;
1830 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1833 this = listen_alloc(RAD_LISTEN_PROXY);
1837 * Create the first proxy socket.
1839 sock->ipaddr = server_ipaddr;
1842 * Try to find a proxy port (value doesn't matter)
1844 for (sock->port = port;
1847 if (listen_bind(this) == 0) {
1849 last = &(this->next); /* just in case */
1854 if (sock->port >= 64000) {
1857 radlog(L_ERR, "Failed to open socket for proxying");
1862 done: /* used only in proxy code. */
1869 * Free a linked list of listeners;
1871 void listen_free(rad_listen_t **head)
1875 if (!head || !*head) return;
1879 rad_listen_t *next = this->next;
1882 * Other code may have eaten the FD.
1884 if (this->fd >= 0) close(this->fd);
1886 if (master_listen[this->type].free) {
1887 master_listen[this->type].free(this);
1899 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
1904 for (this = mainconfig.listen; this != NULL; this = this->next) {
1905 listen_socket_t *sock;
1907 if ((this->type != RAD_LISTEN_AUTH) &&
1908 (this->type != RAD_LISTEN_ACCT)) continue;
1912 if ((sock->port == port) &&
1913 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {
1914 return sock->clients;
1921 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
1925 for (this = mainconfig.listen; this != NULL; this = this->next) {
1926 listen_socket_t *sock;
1928 if ((this->type != RAD_LISTEN_AUTH) &&
1929 (this->type != RAD_LISTEN_ACCT)) continue;
1933 if ((sock->port == port) &&
1934 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {