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>
32 #include <freeradius-devel/process.h>
34 #include <freeradius-devel/vmps.h>
35 #include <freeradius-devel/detail.h>
38 #include <freeradius-devel/udpfromto.h>
41 #ifdef HAVE_SYS_RESOURCE_H
42 #include <sys/resource.h>
54 static void print_packet(RADIUS_PACKET *packet)
56 char src[256], dst[256];
58 ip_ntoh(&packet->src_ipaddr, src, sizeof(src));
59 ip_ntoh(&packet->dst_ipaddr, dst, sizeof(dst));
61 fprintf(stderr, "ID %d: %s %d -> %s %d\n", packet->id,
62 src, packet->src_port, dst, packet->dst_port);
68 * We'll use this below.
70 typedef int (*rad_listen_parse_t)(CONF_SECTION *, rad_listen_t *);
71 typedef void (*rad_listen_free_t)(rad_listen_t *);
73 typedef struct rad_listen_master_t {
74 rad_listen_parse_t parse;
75 rad_listen_free_t free;
76 rad_listen_recv_t recv;
77 rad_listen_send_t send;
78 rad_listen_print_t print;
79 rad_listen_encode_t encode;
80 rad_listen_decode_t decode;
81 } rad_listen_master_t;
83 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type);
86 * Xlat for %{listen:foo}
88 static size_t xlat_listen(UNUSED void *instance, REQUEST *request,
91 UNUSED RADIUS_ESCAPE_STRING func)
93 const char *value = NULL;
96 if (!fmt || !out || (outlen < 1)) return 0;
98 if (!request || !request->listener) {
103 cp = cf_pair_find(request->listener->cs, fmt);
104 if (!cp || !(value = cf_pair_value(cp))) {
109 strlcpy(out, value, outlen);
115 * Find a per-socket client.
117 RADCLIENT *client_listener_find(rad_listen_t *listener,
118 const fr_ipaddr_t *ipaddr, int src_port)
120 #ifdef WITH_DYNAMIC_CLIENTS
127 RADCLIENT_LIST *clients;
128 listen_socket_t *sock;
130 rad_assert(listener != NULL);
131 rad_assert(ipaddr != NULL);
133 sock = listener->data;
134 clients = sock->clients;
137 * This HAS to have been initialized previously.
139 rad_assert(clients != NULL);
141 client = client_find(clients, ipaddr,sock->proto);
143 char name[256], buffer[128];
145 #ifdef WITH_DYNAMIC_CLIENTS
146 unknown: /* used only for dynamic clients */
150 * DoS attack quenching, but only in daemon mode.
151 * If they're running in debug mode, show them
154 if (debug_flag == 0) {
155 static time_t last_printed = 0;
158 if (last_printed == now) return NULL;
163 listener->print(listener, name, sizeof(name));
165 radlog(L_ERR, "Ignoring request to %s from unknown client %s port %d"
169 , name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
170 buffer, sizeof(buffer)), src_port
172 , (sock->proto == IPPROTO_UDP) ? "udp" : "tcp"
178 #ifndef WITH_DYNAMIC_CLIENTS
179 return client; /* return the found client. */
183 * No server defined, and it's not dynamic. Return it.
185 if (!client->client_server && !client->dynamic) return client;
190 * It's a dynamically generated client, check it.
192 if (client->dynamic && (src_port != 0)) {
194 * Lives forever. Return it.
196 if (client->lifetime == 0) return client;
199 * Rate-limit the deletion of known clients.
200 * This makes them last a little longer, but
201 * prevents the server from melting down if (say)
202 * 10k clients all expire at once.
204 if (now == client->last_new_client) return client;
207 * It's not dead yet. Return it.
209 if ((client->created + client->lifetime) > now) return client;
212 * This really puts them onto a queue for later
215 client_delete(clients, client);
218 * Go find the enclosing network again.
220 client = client_find(clients, ipaddr, sock->proto);
225 if (!client) goto unknown;
226 if (!client->client_server) goto unknown;
229 * At this point, 'client' is the enclosing
230 * network that configures where dynamic clients
233 rad_assert(client->dynamic == 0);
235 } else if (!client->dynamic && client->rate_limit) {
237 * The IP is unknown, so we've found an enclosing
238 * network. Enable DoS protection. We only
239 * allow one new client per second. Known
240 * clients aren't subject to this restriction.
242 if (now == client->last_new_client) goto unknown;
245 client->last_new_client = now;
247 request = request_alloc();
248 if (!request) goto unknown;
250 request->listener = listener;
251 request->client = client;
252 request->packet = rad_recv(listener->fd, 0x02); /* MSG_PEEK */
253 if (!request->packet) { /* badly formed, etc */
254 request_free(&request);
257 request->reply = rad_alloc_reply(request->packet);
258 if (!request->reply) {
259 request_free(&request);
262 request->packet->timestamp = request->timestamp;
264 request->priority = listener->type;
265 request->server = client->client_server;
266 request->root = &mainconfig;
269 * Run a fake request through the given virtual server.
270 * Look for FreeRADIUS-Client-IP-Address
271 * FreeRADIUS-Client-Secret
274 * and create the RADCLIENT structure from that.
276 DEBUG("server %s {", request->server);
278 rcode = module_authorize(0, request);
280 DEBUG("} # server %s", request->server);
282 if (rcode != RLM_MODULE_OK) {
283 request_free(&request);
288 * If the client was updated by rlm_dynamic_clients,
289 * don't create the client from attribute-value pairs.
291 if (request->client == client) {
292 created = client_create(clients, request);
294 created = request->client;
297 * This frees the client if it isn't valid.
299 if (!client_validate(clients, client, created)) goto unknown;
302 request->server = client->server;
303 exec_trigger(request, NULL, "server.client.add");
305 request_free(&request);
307 if (!created) goto unknown;
313 static int listen_bind(rad_listen_t *this);
317 * Process and reply to a server-status request.
318 * Like rad_authenticate and rad_accounting this should
319 * live in it's own file but it's so small we don't bother.
321 static int rad_status_server(REQUEST *request)
323 int rcode = RLM_MODULE_OK;
326 switch (request->listener->type) {
328 case RAD_LISTEN_NONE:
330 case RAD_LISTEN_AUTH:
331 dval = dict_valbyname(PW_AUTZ_TYPE, 0, "Status-Server");
333 rcode = module_authorize(dval->value, request);
335 rcode = RLM_MODULE_OK;
340 case RLM_MODULE_UPDATED:
341 request->reply->code = PW_AUTHENTICATION_ACK;
344 case RLM_MODULE_FAIL:
345 case RLM_MODULE_HANDLED:
346 request->reply->code = 0; /* don't reply */
350 case RLM_MODULE_REJECT:
351 request->reply->code = PW_AUTHENTICATION_REJECT;
356 #ifdef WITH_ACCOUNTING
357 case RAD_LISTEN_ACCT:
358 dval = dict_valbyname(PW_ACCT_TYPE, 0, "Status-Server");
360 rcode = module_accounting(dval->value, request);
362 rcode = RLM_MODULE_OK;
367 case RLM_MODULE_UPDATED:
368 request->reply->code = PW_ACCOUNTING_RESPONSE;
372 request->reply->code = 0; /* don't reply */
380 * This is a vendor extension. Suggested by Glen
381 * Zorn in IETF 72, and rejected by the rest of
382 * the WG. We like it, so it goes in here.
385 dval = dict_valbyname(PW_RECV_COA_TYPE, 0, "Status-Server");
387 rcode = module_recv_coa(dval->value, request);
389 rcode = RLM_MODULE_OK;
394 case RLM_MODULE_UPDATED:
395 request->reply->code = PW_COA_ACK;
399 request->reply->code = 0; /* don't reply */
411 * Full statistics are available only on a statistics
414 if (request->listener->type == RAD_LISTEN_NONE) {
415 request_stats_reply(request);
423 static int dual_tcp_recv(rad_listen_t *listener)
426 RADIUS_PACKET *packet;
427 RAD_REQUEST_FUNP fun = NULL;
428 listen_socket_t *sock = listener->data;
429 RADCLIENT *client = sock->client;
432 * Allocate a packet for partial reads.
435 sock->packet = rad_alloc(0);
436 if (!sock->packet) return 0;
438 sock->packet->sockfd = listener->fd;
439 sock->packet->src_ipaddr = sock->other_ipaddr;
440 sock->packet->src_port = sock->other_port;
441 sock->packet->dst_ipaddr = sock->my_ipaddr;
442 sock->packet->dst_port = sock->my_port;
446 * Grab the packet currently being processed.
448 packet = sock->packet;
450 rcode = fr_tcp_read_packet(packet, 0);
453 * Still only a partial packet. Put it back, and return,
454 * so that we'll read more data when it's ready.
460 if (rcode == -1) { /* error reading packet */
463 radlog(L_ERR, "Invalid packet from %s port %d: closing socket",
464 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
468 if (rcode < 0) { /* error or connection reset */
469 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
472 * Decrement the number of connections.
474 if (sock->parent->num_connections > 0) {
475 sock->parent->num_connections--;
477 if (sock->client->num_connections > 0) {
478 sock->client->num_connections--;
482 * Tell the event handler that an FD has disappeared.
484 DEBUG("Client has closed connection");
485 event_new_fd(listener);
488 * Do NOT free the listener here. It's in use by
489 * a request, and will need to hang around until
490 * all of the requests are done.
492 * It is instead free'd in remove_from_request_hash()
497 RAD_STATS_TYPE_INC(listener, total_requests);
500 * Some sanity checks, based on the packet code.
502 switch(packet->code) {
503 case PW_AUTHENTICATION_REQUEST:
504 if (listener->type != RAD_LISTEN_AUTH) goto bad_packet;
505 RAD_STATS_CLIENT_INC(listener, client, total_requests);
506 fun = rad_authenticate;
509 #ifdef WITH_ACCOUNTING
510 case PW_ACCOUNTING_REQUEST:
511 if (listener->type != RAD_LISTEN_ACCT) goto bad_packet;
512 RAD_STATS_CLIENT_INC(listener, client, total_requests);
513 fun = rad_accounting;
517 case PW_STATUS_SERVER:
518 if (!mainconfig.status_server) {
519 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
520 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
521 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
522 rad_free(&sock->packet);
525 fun = rad_status_server;
530 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
531 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
533 DEBUG("Invalid packet code %d sent from client %s port %d : IGNORED",
534 packet->code, client->shortname, packet->src_port);
535 rad_free(&sock->packet);
537 } /* switch over packet types */
539 if (!request_receive(listener, packet, client, fun)) {
540 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
541 RAD_STATS_CLIENT_INC(listener, sock->client, total_packets_dropped);
542 rad_free(&sock->packet);
546 sock->packet = NULL; /* we have no need for more partial reads */
550 static int dual_tcp_accept(rad_listen_t *listener)
555 struct sockaddr_storage src;
556 listen_socket_t *sock;
557 fr_ipaddr_t src_ipaddr;
562 DEBUG2(" ... new connection request on TCP socket.");
564 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
567 * Non-blocking sockets must handle this.
569 if (errno == EWOULDBLOCK) {
573 DEBUG2(" ... failed to accept connection.");
577 if (!fr_sockaddr2ipaddr(&src, salen, &src_ipaddr, &src_port)) {
578 DEBUG2(" ... unknown address family.");
583 * Enforce client IP address checks on accept, not on
586 if ((client = client_listener_find(listener,
587 &src_ipaddr, src_port)) == NULL) {
589 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
594 * Enforce max_connectionsx on client && listen section.
596 if ((client->max_connections != 0) &&
597 (client->max_connections == client->num_connections)) {
599 * FIXME: Print client IP/port, and server IP/port.
601 radlog(L_INFO, "Ignoring new connection due to client max_connections (%d)", client->max_connections);
606 sock = listener->data;
607 if ((sock->max_connections != 0) &&
608 (sock->max_connections == sock->num_connections)) {
610 * FIXME: Print client IP/port, and server IP/port.
612 radlog(L_INFO, "Ignoring new connection due to socket max_connections");
616 client->num_connections++;
617 sock->num_connections++;
620 * Add the new listener.
622 this = listen_alloc(listener->type);
623 if (!this) return -1;
626 * Copy everything, including the pointer to the socket
630 memcpy(this->data, listener->data, sizeof(*sock));
631 memcpy(this, listener, sizeof(*this));
633 this->data = sock; /* fix it back */
635 sock->parent = listener->data;
636 sock->other_ipaddr = src_ipaddr;
637 sock->other_port = src_port;
638 sock->client = client;
641 this->status = RAD_LISTEN_STATUS_INIT;
642 this->recv = dual_tcp_recv;
645 * FIXME: set O_NONBLOCK on the accept'd fd.
646 * See djb's portability rants for details.
650 * Tell the event loop that we have a new FD.
651 * This can be called from a child thread...
661 * This function is stupid and complicated.
663 static int socket_print(const rad_listen_t *this, char *buffer, size_t bufsize)
666 listen_socket_t *sock = this->data;
669 switch (this->type) {
671 case RAD_LISTEN_NONE: /* what a hack... */
676 case RAD_LISTEN_AUTH:
677 name = "authentication";
680 #ifdef WITH_ACCOUNTING
681 case RAD_LISTEN_ACCT:
687 case RAD_LISTEN_PROXY:
699 case RAD_LISTEN_DHCP:
715 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
716 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
720 if (sock->interface) {
721 ADDSTRING(" interface ");
722 ADDSTRING(sock->interface);
726 if (this->recv == dual_tcp_accept) {
727 ADDSTRING(" proto tcp");
733 * TCP sockets get printed a little differently, to make
734 * it clear what's going on.
737 ADDSTRING(" from client (");
738 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
742 snprintf(buffer, bufsize, "%d", sock->other_port);
746 if ((sock->my_ipaddr.af == AF_INET) &&
747 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
748 strlcpy(buffer, "*", bufsize);
750 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
755 snprintf(buffer, bufsize, "%d", sock->my_port);
759 ADDSTRING(", virtual-server=");
760 ADDSTRING(this->server);
770 * Maybe it's a socket that we opened to a home server.
772 if ((sock->proto == IPPROTO_TCP) &&
773 (this->type == RAD_LISTEN_PROXY)) {
775 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
779 snprintf(buffer, bufsize, "%d", sock->my_port);
781 ADDSTRING(") -> home_server (");
783 if ((sock->other_ipaddr.af == AF_INET) &&
784 (sock->other_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
785 strlcpy(buffer, "*", bufsize);
787 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
792 snprintf(buffer, bufsize, "%d", sock->other_port);
799 #endif /* WITH_PROXY */
800 #endif /* WITH_TCP */
802 ADDSTRING(" address ");
804 if ((sock->my_ipaddr.af == AF_INET) &&
805 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
806 strlcpy(buffer, "*", bufsize);
808 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
813 snprintf(buffer, bufsize, "%d", sock->my_port);
817 ADDSTRING(" as server ");
818 ADDSTRING(this->server);
827 extern int check_config; /* radiusd.c */
831 * Parse an authentication or accounting socket.
833 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
838 listen_socket_t *sock = this->data;
839 char *section_name = NULL;
840 CONF_SECTION *client_cs, *parentcs;
847 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
848 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
849 &ipaddr.ipaddr.ip4addr, NULL);
850 if (rcode < 0) return -1;
852 if (rcode == 0) { /* successfully parsed IPv4 */
855 } else { /* maybe IPv6? */
856 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
857 &ipaddr.ipaddr.ip6addr, NULL);
858 if (rcode < 0) return -1;
861 cf_log_err(cf_sectiontoitem(cs),
862 "No address specified in listen section");
865 ipaddr.af = AF_INET6;
868 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
870 if (rcode < 0) return -1;
872 if ((listen_port < 0) || (listen_port > 65535)) {
873 cf_log_err(cf_sectiontoitem(cs),
874 "Invalid value for \"port\"");
878 sock->proto = IPPROTO_UDP;
880 if (cf_pair_find(cs, "proto")) {
882 cf_log_err(cf_sectiontoitem(cs),
883 "System does not support the TCP protocol. Delete this line from the configuration file.");
889 rcode = cf_item_parse(cs, "proto", PW_TYPE_STRING_PTR,
891 if (rcode < 0) return -1;
893 if (strcmp(proto, "udp") == 0) {
894 sock->proto = IPPROTO_UDP;
896 } else if (strcmp(proto, "tcp") == 0) {
897 sock->proto = IPPROTO_TCP;
899 rcode = cf_item_parse(cs, "max_connections", PW_TYPE_INTEGER,
900 &sock->max_connections, "64");
901 if (rcode < 0) return -1;
904 cf_log_err(cf_sectiontoitem(cs),
905 "Unknown proto name \"%s\"", proto);
912 * TCP requires a destination IP for sockets.
913 * UDP doesn't, so it's allowed.
916 if ((this->type == RAD_LISTEN_PROXY) &&
917 (sock->proto != IPPROTO_UDP)) {
918 cf_log_err(cf_sectiontoitem(cs),
919 "Proxy listeners can only listen on proto = udp");
922 #endif /* WITH_PROXY */
923 #endif /* WITH_TCP */
926 sock->my_ipaddr = ipaddr;
927 sock->my_port = listen_port;
931 if (home_server_find(&sock->my_ipaddr, sock->my_port, sock->proto)) {
934 DEBUG("ERROR: We have been asked to listen on %s port %d, which is also listed as a home server. This can create a proxy loop.",
935 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
940 return 0; /* don't do anything */
945 * If we can bind to interfaces, do so,
948 if (cf_pair_find(cs, "interface")) {
950 CONF_PAIR *cp = cf_pair_find(cs, "interface");
952 rad_assert(cp != NULL);
953 value = cf_pair_value(cp);
955 cf_log_err(cf_sectiontoitem(cs),
956 "No interface name given");
959 sock->interface = value;
964 * If we can do broadcasts..
966 if (cf_pair_find(cs, "broadcast")) {
968 cf_log_err(cf_sectiontoitem(cs),
969 "System does not support broadcast sockets. Delete this line from the configuration file.");
973 CONF_PAIR *cp = cf_pair_find(cs, "broadcast");
975 if (this->type != RAD_LISTEN_DHCP) {
976 cf_log_err(cf_pairtoitem(cp),
977 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file.");
981 rad_assert(cp != NULL);
982 value = cf_pair_value(cp);
984 cf_log_err(cf_sectiontoitem(cs),
985 "No broadcast value given");
990 * Hack... whatever happened to cf_section_parse?
992 sock->broadcast = (strcmp(value, "yes") == 0);
998 * And bind it to the port.
1000 if (listen_bind(this) < 0) {
1002 cf_log_err(cf_sectiontoitem(cs),
1003 "Error binding to port for %s port %d",
1004 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
1011 * Proxy sockets don't have clients.
1013 if (this->type == RAD_LISTEN_PROXY) return 0;
1017 * The more specific configurations are preferred to more
1021 parentcs = cf_top_section(cs);
1022 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
1023 §ion_name, NULL);
1024 if (rcode < 0) return -1; /* bad string */
1027 * Explicit list given: use it.
1029 client_cs = cf_section_sub_find_name2(parentcs,
1033 client_cs = cf_section_find(section_name);
1036 cf_log_err(cf_sectiontoitem(cs),
1037 "Failed to find clients %s {...}",
1043 } /* else there was no "clients = " entry. */
1046 CONF_SECTION *server_cs;
1048 server_cs = cf_section_sub_find_name2(parentcs,
1052 * Found a "server foo" section. If there are clients
1056 (cf_section_sub_find(server_cs, "client") != NULL)) {
1057 client_cs = server_cs;
1062 * Still nothing. Look for global clients.
1064 if (!client_cs) client_cs = parentcs;
1066 sock->clients = clients_parse_section(client_cs);
1067 if (!sock->clients) {
1068 cf_log_err(cf_sectiontoitem(cs),
1069 "Failed to load clients for this listen section");
1074 if (sock->proto == IPPROTO_TCP) {
1076 * Re-write the listener receive function to
1077 * allow us to accept the socket.
1079 this->recv = dual_tcp_accept;
1087 * Send an authentication response packet
1089 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1091 rad_assert(request->listener == listener);
1092 rad_assert(listener->send == auth_socket_send);
1094 if (rad_send(request->reply, request->packet,
1095 request->client->secret) < 0) {
1096 radlog_request(L_ERR, 0, request, "Failed sending reply: %s",
1105 #ifdef WITH_ACCOUNTING
1107 * Send an accounting response packet (or not)
1109 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1111 rad_assert(request->listener == listener);
1112 rad_assert(listener->send == acct_socket_send);
1115 * Accounting reject's are silently dropped.
1117 * We do it here to avoid polluting the rest of the
1118 * code with this knowledge
1120 if (request->reply->code == 0) return 0;
1122 if (rad_send(request->reply, request->packet,
1123 request->client->secret) < 0) {
1124 radlog_request(L_ERR, 0, request, "Failed sending reply: %s",
1135 * Send a packet to a home server.
1137 * FIXME: have different code for proxy auth & acct!
1139 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1141 rad_assert(request->proxy_listener == listener);
1142 rad_assert(listener->send == proxy_socket_send);
1144 if (rad_send(request->proxy, request->packet,
1145 request->home_server->secret) < 0) {
1146 radlog_request(L_ERR, 0, request, "Failed sending proxied request: %s",
1157 * Check if an incoming request is "ok"
1159 * It takes packets, not requests. It sees if the packet looks
1160 * OK. If so, it does a number of sanity checks on it.
1162 static int stats_socket_recv(rad_listen_t *listener)
1166 RADIUS_PACKET *packet;
1168 fr_ipaddr_t src_ipaddr;
1170 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1171 if (rcode < 0) return 0;
1173 RAD_STATS_TYPE_INC(listener, total_requests);
1175 if (rcode < 20) { /* AUTH_HDR_LEN */
1176 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1180 if ((client = client_listener_find(listener,
1181 &src_ipaddr, src_port)) == NULL) {
1182 rad_recv_discard(listener->fd);
1183 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1188 * We only understand Status-Server on this socket.
1190 if (code != PW_STATUS_SERVER) {
1191 DEBUG("Ignoring packet code %d sent to Status-Server port",
1193 rad_recv_discard(listener->fd);
1194 RAD_STATS_TYPE_INC(listener, total_unknown_types);
1195 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1200 * Now that we've sanity checked everything, receive the
1203 packet = rad_recv(listener->fd, 1); /* require message authenticator */
1205 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1206 DEBUG("%s", fr_strerror());
1210 if (!request_receive(listener, packet, client, rad_status_server)) {
1211 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1212 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1223 * Check if an incoming request is "ok"
1225 * It takes packets, not requests. It sees if the packet looks
1226 * OK. If so, it does a number of sanity checks on it.
1228 static int auth_socket_recv(rad_listen_t *listener)
1232 RADIUS_PACKET *packet;
1233 RAD_REQUEST_FUNP fun = NULL;
1235 fr_ipaddr_t src_ipaddr;
1237 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1238 if (rcode < 0) return 0;
1240 RAD_STATS_TYPE_INC(listener, total_requests);
1242 if (rcode < 20) { /* AUTH_HDR_LEN */
1243 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1247 if ((client = client_listener_find(listener,
1248 &src_ipaddr, src_port)) == NULL) {
1249 rad_recv_discard(listener->fd);
1250 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1255 * Some sanity checks, based on the packet code.
1258 case PW_AUTHENTICATION_REQUEST:
1259 RAD_STATS_CLIENT_INC(listener, client, total_requests);
1260 fun = rad_authenticate;
1263 case PW_STATUS_SERVER:
1264 if (!mainconfig.status_server) {
1265 rad_recv_discard(listener->fd);
1266 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1267 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1268 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1271 fun = rad_status_server;
1275 rad_recv_discard(listener->fd);
1276 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
1277 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1279 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
1280 code, client->shortname, src_port);
1283 } /* switch over packet types */
1286 * Now that we've sanity checked everything, receive the
1289 packet = rad_recv(listener->fd, client->message_authenticator);
1291 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1292 DEBUG("%s", fr_strerror());
1296 if (!request_receive(listener, packet, client, fun)) {
1297 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1298 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1307 #ifdef WITH_ACCOUNTING
1309 * Receive packets from an accounting socket
1311 static int acct_socket_recv(rad_listen_t *listener)
1315 RADIUS_PACKET *packet;
1316 RAD_REQUEST_FUNP fun = NULL;
1318 fr_ipaddr_t src_ipaddr;
1320 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1321 if (rcode < 0) return 0;
1323 RAD_STATS_TYPE_INC(listener, total_requests);
1325 if (rcode < 20) { /* AUTH_HDR_LEN */
1326 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1330 if ((client = client_listener_find(listener,
1331 &src_ipaddr, src_port)) == NULL) {
1332 rad_recv_discard(listener->fd);
1333 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1338 * Some sanity checks, based on the packet code.
1341 case PW_ACCOUNTING_REQUEST:
1342 RAD_STATS_CLIENT_INC(listener, client, total_requests);
1343 fun = rad_accounting;
1346 case PW_STATUS_SERVER:
1347 if (!mainconfig.status_server) {
1348 rad_recv_discard(listener->fd);
1349 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1350 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1352 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1355 fun = rad_status_server;
1359 rad_recv_discard(listener->fd);
1360 RAD_STATS_TYPE_INC(listener, total_unknown_types);
1361 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1363 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1364 code, client->shortname, src_port);
1366 } /* switch over packet types */
1369 * Now that we've sanity checked everything, receive the
1372 packet = rad_recv(listener->fd, 0);
1374 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1375 radlog(L_ERR, "%s", fr_strerror());
1380 * There can be no duplicate accounting packets.
1382 if (!request_receive(listener, packet, client, fun)) {
1383 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1384 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1395 static int do_proxy(REQUEST *request)
1399 if (request->in_proxy_hash ||
1400 (request->proxy_reply && (request->proxy_reply->code != 0))) {
1404 vp = pairfind(request->config_items, PW_HOME_SERVER_POOL, 0);
1407 if (!home_pool_byname(vp->vp_strvalue, HOME_TYPE_COA)) {
1408 RDEBUG2("ERROR: Cannot proxy to unknown pool %s",
1417 * Receive a CoA packet.
1419 static int rad_coa_recv(REQUEST *request)
1421 int rcode = RLM_MODULE_OK;
1426 * Get the correct response
1428 switch (request->packet->code) {
1429 case PW_COA_REQUEST:
1434 case PW_DISCONNECT_REQUEST:
1435 ack = PW_DISCONNECT_ACK;
1436 nak = PW_DISCONNECT_NAK;
1439 default: /* shouldn't happen */
1440 return RLM_MODULE_FAIL;
1444 #define WAS_PROXIED (request->proxy)
1446 #define WAS_PROXIED (0)
1451 * RFC 5176 Section 3.3. If we have a CoA-Request
1452 * with Service-Type = Authorize-Only, it MUST
1453 * have a State attribute in it.
1455 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE, 0);
1456 if (request->packet->code == PW_COA_REQUEST) {
1457 if (vp && (vp->vp_integer == 17)) {
1458 vp = pairfind(request->packet->vps, PW_STATE, 0);
1459 if (!vp || (vp->length == 0)) {
1460 RDEBUG("ERROR: CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1461 request->reply->code = PW_COA_NAK;
1462 return RLM_MODULE_FAIL;
1467 * RFC 5176, Section 3.2.
1469 RDEBUG("ERROR: Disconnect-Request MUST NOT contain a Service-Type attribute");
1470 request->reply->code = PW_DISCONNECT_NAK;
1471 return RLM_MODULE_FAIL;
1474 rcode = module_recv_coa(0, request);
1476 case RLM_MODULE_FAIL:
1477 case RLM_MODULE_INVALID:
1478 case RLM_MODULE_REJECT:
1479 case RLM_MODULE_USERLOCK:
1481 request->reply->code = nak;
1484 case RLM_MODULE_HANDLED:
1487 case RLM_MODULE_NOOP:
1488 case RLM_MODULE_NOTFOUND:
1490 case RLM_MODULE_UPDATED:
1491 if (do_proxy(request)) return RLM_MODULE_OK;
1492 request->reply->code = ack;
1495 } else if (request->proxy_reply) {
1497 * Start the reply code with the proxy reply
1500 request->reply->code = request->proxy_reply->code;
1504 * Copy State from the request to the reply.
1505 * See RFC 5176 Section 3.3.
1507 vp = paircopy2(request->packet->vps, PW_STATE, 0);
1508 if (vp) pairadd(&request->reply->vps, vp);
1511 * We may want to over-ride the reply.
1513 if (request->reply->code) {
1514 rcode = module_send_coa(0, request);
1517 * We need to send CoA-NAK back if Service-Type
1518 * is Authorize-Only. Rely on the user's policy
1519 * to do that. We're not a real NAS, so this
1520 * restriction doesn't (ahem) apply to us.
1522 case RLM_MODULE_FAIL:
1523 case RLM_MODULE_INVALID:
1524 case RLM_MODULE_REJECT:
1525 case RLM_MODULE_USERLOCK:
1528 * Over-ride an ACK with a NAK
1530 request->reply->code = nak;
1533 case RLM_MODULE_HANDLED:
1536 case RLM_MODULE_NOOP:
1537 case RLM_MODULE_NOTFOUND:
1539 case RLM_MODULE_UPDATED:
1541 * Do NOT over-ride a previously set value.
1542 * Otherwise an "ok" here will re-write a
1545 if (request->reply->code == 0) {
1546 request->reply->code = ack;
1552 return RLM_MODULE_OK;
1557 * Check if an incoming request is "ok"
1559 * It takes packets, not requests. It sees if the packet looks
1560 * OK. If so, it does a number of sanity checks on it.
1562 static int coa_socket_recv(rad_listen_t *listener)
1566 RADIUS_PACKET *packet;
1567 RAD_REQUEST_FUNP fun = NULL;
1569 fr_ipaddr_t src_ipaddr;
1571 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1572 if (rcode < 0) return 0;
1574 RAD_STATS_TYPE_INC(listener, total_requests);
1576 if (rcode < 20) { /* AUTH_HDR_LEN */
1577 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1581 if ((client = client_listener_find(listener,
1582 &src_ipaddr, src_port)) == NULL) {
1583 rad_recv_discard(listener->fd);
1584 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1589 * Some sanity checks, based on the packet code.
1592 case PW_COA_REQUEST:
1593 case PW_DISCONNECT_REQUEST:
1598 rad_recv_discard(listener->fd);
1599 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1600 code, client->shortname, src_port);
1603 } /* switch over packet types */
1606 * Now that we've sanity checked everything, receive the
1609 packet = rad_recv(listener->fd, client->message_authenticator);
1611 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1612 DEBUG("%s", fr_strerror());
1616 if (!request_receive(listener, packet, client, fun)) {
1627 * Recieve packets from a proxy socket.
1629 static int proxy_socket_recv(rad_listen_t *listener)
1631 RADIUS_PACKET *packet;
1634 packet = rad_recv(listener->fd, 0);
1636 radlog(L_ERR, "%s", fr_strerror());
1641 * FIXME: Client MIB updates?
1643 switch(packet->code) {
1644 case PW_AUTHENTICATION_ACK:
1645 case PW_ACCESS_CHALLENGE:
1646 case PW_AUTHENTICATION_REJECT:
1649 #ifdef WITH_ACCOUNTING
1650 case PW_ACCOUNTING_RESPONSE:
1655 case PW_DISCONNECT_ACK:
1656 case PW_DISCONNECT_NAK:
1664 * FIXME: Update MIB for packet types?
1666 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1667 "from home server %s port %d - ID %d : IGNORED",
1669 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1670 packet->src_port, packet->id);
1675 if (!request_proxy_reply(packet)) {
1685 * Recieve packets from a proxy socket.
1687 static int proxy_socket_tcp_recv(rad_listen_t *listener)
1689 RADIUS_PACKET *packet;
1690 listen_socket_t *sock = listener->data;
1693 packet = fr_tcp_recv(listener->fd, 0);
1695 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
1696 event_new_fd(listener);
1701 * FIXME: Client MIB updates?
1703 switch(packet->code) {
1704 case PW_AUTHENTICATION_ACK:
1705 case PW_ACCESS_CHALLENGE:
1706 case PW_AUTHENTICATION_REJECT:
1709 #ifdef WITH_ACCOUNTING
1710 case PW_ACCOUNTING_RESPONSE:
1716 * FIXME: Update MIB for packet types?
1718 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1719 "from home server %s port %d - ID %d : IGNORED",
1721 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1722 packet->src_port, packet->id);
1727 packet->src_ipaddr = sock->other_ipaddr;
1728 packet->src_port = sock->other_port;
1729 packet->dst_ipaddr = sock->my_ipaddr;
1730 packet->dst_port = sock->my_port;
1733 * FIXME: Have it return an indication of packets that
1734 * are OK to ignore (dups, too late), versus ones that
1735 * aren't OK to ignore (unknown response, spoofed, etc.)
1737 * Close the socket on bad packets...
1739 if (!request_proxy_reply(packet)) {
1744 sock->opened = sock->last_packet = time(NULL);
1752 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1754 if (!request->reply->code) return 0;
1756 if (rad_encode(request->reply, request->packet,
1757 request->client->secret) < 0) {
1758 radlog_request(L_ERR, 0, request, "Failed encoding packet: %s",
1763 if (rad_sign(request->reply, request->packet,
1764 request->client->secret) < 0) {
1765 radlog_request(L_ERR, 0, request, "Failed signing packet: %s",
1774 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1776 if (rad_verify(request->packet, NULL,
1777 request->client->secret) < 0) {
1781 return rad_decode(request->packet, NULL,
1782 request->client->secret);
1786 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1788 if (rad_encode(request->proxy, NULL, request->home_server->secret) < 0) {
1789 radlog_request(L_ERR, 0, request, "Failed encoding proxied packet: %s",
1794 if (rad_sign(request->proxy, NULL, request->home_server->secret) < 0) {
1795 radlog_request(L_ERR, 0, request, "Failed signing proxied packet: %s",
1804 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1807 * rad_verify is run in event.c, received_proxy_response()
1810 return rad_decode(request->proxy_reply, request->proxy,
1811 request->home_server->secret);
1817 #include "command.c"
1819 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1821 { common_socket_parse, NULL,
1822 stats_socket_recv, auth_socket_send,
1823 socket_print, client_socket_encode, client_socket_decode },
1826 * This always gets defined.
1828 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1833 { common_socket_parse, NULL,
1834 proxy_socket_recv, proxy_socket_send,
1835 socket_print, proxy_socket_encode, proxy_socket_decode },
1838 /* authentication */
1839 { common_socket_parse, NULL,
1840 auth_socket_recv, auth_socket_send,
1841 socket_print, client_socket_encode, client_socket_decode },
1843 #ifdef WITH_ACCOUNTING
1845 { common_socket_parse, NULL,
1846 acct_socket_recv, acct_socket_send,
1847 socket_print, client_socket_encode, client_socket_decode},
1852 { detail_parse, detail_free,
1853 detail_recv, detail_send,
1854 detail_print, detail_encode, detail_decode },
1858 /* vlan query protocol */
1859 { common_socket_parse, NULL,
1860 vqp_socket_recv, vqp_socket_send,
1861 socket_print, vqp_socket_encode, vqp_socket_decode },
1865 /* dhcp query protocol */
1866 { dhcp_socket_parse, NULL,
1867 dhcp_socket_recv, dhcp_socket_send,
1868 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1871 #ifdef WITH_COMMAND_SOCKET
1872 /* TCP command socket */
1873 { command_socket_parse, command_socket_free,
1874 command_domain_accept, command_domain_send,
1875 command_socket_print, command_socket_encode, command_socket_decode },
1879 /* Change of Authorization */
1880 { common_socket_parse, NULL,
1881 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
1882 socket_print, client_socket_encode, client_socket_decode },
1890 * Binds a listener to a socket.
1892 static int listen_bind(rad_listen_t *this)
1895 struct sockaddr_storage salocal;
1897 listen_socket_t *sock = this->data;
1899 #define proto_for_port "udp"
1900 #define sock_type SOCK_DGRAM
1902 const char *proto_for_port = "udp";
1903 int sock_type = SOCK_DGRAM;
1905 if (sock->proto == IPPROTO_TCP) {
1907 if (this->type == RAD_LISTEN_VQP) {
1908 radlog(L_ERR, "VQP does not support TCP transport");
1913 proto_for_port = "tcp";
1914 sock_type = SOCK_STREAM;
1919 * If the port is zero, then it means the appropriate
1920 * thing from /etc/services.
1922 if (sock->my_port == 0) {
1923 struct servent *svp;
1925 switch (this->type) {
1926 case RAD_LISTEN_AUTH:
1927 svp = getservbyname ("radius", proto_for_port);
1929 sock->my_port = ntohs(svp->s_port);
1931 sock->my_port = PW_AUTH_UDP_PORT;
1935 #ifdef WITH_ACCOUNTING
1936 case RAD_LISTEN_ACCT:
1937 svp = getservbyname ("radacct", proto_for_port);
1939 sock->my_port = ntohs(svp->s_port);
1941 sock->my_port = PW_ACCT_UDP_PORT;
1947 case RAD_LISTEN_PROXY:
1948 /* leave it at zero */
1953 case RAD_LISTEN_VQP:
1954 sock->my_port = 1589;
1959 case RAD_LISTEN_COA:
1960 svp = getservbyname ("radius-dynauth", "udp");
1962 sock->my_port = ntohs(svp->s_port);
1964 sock->my_port = PW_COA_UDP_PORT;
1970 DEBUG("WARNING: Internal sanity check failed in binding to socket. Ignoring problem.");
1976 * Copy fr_socket() here, as we may need to bind to a device.
1978 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
1982 this->print(this, buffer, sizeof(buffer));
1984 radlog(L_ERR, "Failed opening %s: %s", buffer, strerror(errno));
1989 * Bind to a device BEFORE touching IP addresses.
1991 if (sock->interface) {
1992 #ifdef SO_BINDTODEVICE
1994 strcpy(ifreq.ifr_name, sock->interface);
1997 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1998 (char *)&ifreq, sizeof(ifreq));
2002 radlog(L_ERR, "Failed binding to interface %s: %s",
2003 sock->interface, strerror(errno));
2005 } /* else it worked. */
2007 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2008 #ifdef HAVE_NET_IF_H
2010 * Odds are that any system supporting "bind to
2011 * device" also supports IPv6, so this next bit
2012 * isn't necessary. But it's here for
2015 * If we're doing IPv6, and the scope hasn't yet
2016 * been defined, set the scope to the scope of
2019 if (sock->my_ipaddr.af == AF_INET6) {
2020 if (sock->my_ipaddr.scope == 0) {
2021 sock->my_ipaddr.scope = if_nametoindex(sock->interface);
2022 if (sock->my_ipaddr.scope == 0) {
2024 radlog(L_ERR, "Failed finding interface %s: %s",
2025 sock->interface, strerror(errno));
2028 } /* else scope was defined: we're OK. */
2033 * IPv4: no link local addresses,
2034 * and no bind to device.
2038 radlog(L_ERR, "Failed binding to interface %s: \"bind to device\" is unsupported", sock->interface);
2045 if (sock->proto == IPPROTO_TCP) {
2048 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2050 radlog(L_ERR, "Failed to reuse address: %s", strerror(errno));
2056 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
2057 else /* UDP sockets get UDPfromto */
2060 #ifdef WITH_UDPFROMTO
2062 * Initialize udpfromto for all sockets.
2064 if (udpfromto_init(this->fd) != 0) {
2065 radlog(L_ERR, "Failed initializing udpfromto: %s",
2073 * Set up sockaddr stuff.
2075 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
2080 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2081 if (sock->my_ipaddr.af == AF_INET6) {
2083 * Listening on '::' does NOT get you IPv4 to
2084 * IPv6 mapping. You've got to listen on an IPv4
2085 * address, too. This makes the rest of the server
2086 * design a little simpler.
2090 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2093 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2094 (char *)&on, sizeof(on));
2096 #endif /* IPV6_V6ONLY */
2098 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2100 if (sock->my_ipaddr.af == AF_INET) {
2103 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2105 * Disable PMTU discovery. On Linux, this
2106 * also makes sure that the "don't fragment"
2109 flag = IP_PMTUDISC_DONT;
2110 setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2111 &flag, sizeof(flag));
2114 #if defined(IP_DONTFRAG)
2116 * Ensure that the "don't fragment" flag is zero.
2119 setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2120 &flag, sizeof(flag));
2126 if (sock->broadcast) {
2129 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2130 radlog(L_ERR, "Can't set broadcast option: %s\n",
2139 * May be binding to priviledged ports.
2141 if (sock->my_port != 0) {
2145 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2146 radlog(L_ERR, "Can't set re-use address option: %s\n",
2153 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2159 this->print(this, buffer, sizeof(buffer));
2160 radlog(L_ERR, "Failed binding to %s: %s\n",
2161 buffer, strerror(errno));
2166 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2167 * kernel instead binds us to a 1.2.3.4. If this
2168 * happens, notice, and remember our real IP.
2171 struct sockaddr_storage src;
2172 socklen_t sizeof_src = sizeof(src);
2174 memset(&src, 0, sizeof_src);
2175 if (getsockname(this->fd, (struct sockaddr *) &src,
2177 radlog(L_ERR, "Failed getting socket name: %s",
2182 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2183 &sock->my_ipaddr, &sock->my_port)) {
2184 radlog(L_ERR, "Socket has unsupported address family");
2191 if (sock->proto == IPPROTO_TCP) {
2192 if (listen(this->fd, 8) < 0) {
2194 radlog(L_ERR, "Failed in listen(): %s", strerror(errno));
2200 if (fr_nonblock(this->fd) < 0) {
2202 radlog(L_ERR, "Failed setting non-blocking on socket: %s",
2208 * Mostly for proxy sockets.
2210 sock->other_ipaddr.af = sock->my_ipaddr.af;
2213 * Don't screw up other people.
2215 #undef proto_for_port
2223 * Allocate & initialize a new listener.
2225 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
2229 this = rad_malloc(sizeof(*this));
2230 memset(this, 0, sizeof(*this));
2233 this->recv = master_listen[this->type].recv;
2234 this->send = master_listen[this->type].send;
2235 this->print = master_listen[this->type].print;
2236 this->encode = master_listen[this->type].encode;
2237 this->decode = master_listen[this->type].decode;
2241 case RAD_LISTEN_NONE:
2243 case RAD_LISTEN_AUTH:
2244 #ifdef WITH_ACCOUNTING
2245 case RAD_LISTEN_ACCT:
2248 case RAD_LISTEN_PROXY:
2251 case RAD_LISTEN_VQP:
2254 case RAD_LISTEN_COA:
2256 this->data = rad_malloc(sizeof(listen_socket_t));
2257 memset(this->data, 0, sizeof(listen_socket_t));
2261 case RAD_LISTEN_DHCP:
2262 this->data = rad_malloc(sizeof(dhcp_socket_t));
2263 memset(this->data, 0, sizeof(dhcp_socket_t));
2268 case RAD_LISTEN_DETAIL:
2273 #ifdef WITH_COMMAND_SOCKET
2274 case RAD_LISTEN_COMMAND:
2275 this->data = rad_malloc(sizeof(fr_command_socket_t));
2276 memset(this->data, 0, sizeof(fr_command_socket_t));
2281 rad_assert("Unsupported option!" == NULL);
2290 * Externally visible function for creating a new proxy LISTENER.
2292 * Not thread-safe, but all calls to it are protected by the
2293 * proxy mutex in event.c
2295 int proxy_new_listener(home_server *home, int src_port)
2298 listen_socket_t *sock;
2300 if (!home) return 0;
2302 if ((home->max_connections > 0) &&
2303 (home->num_connections >= home->max_connections)) {
2304 DEBUG("WARNING: Home server has too many open connections (%d)",
2305 home->max_connections);
2309 this = listen_alloc(RAD_LISTEN_PROXY);
2312 sock->other_ipaddr = home->ipaddr;
2313 sock->other_port = home->port;
2316 sock->my_ipaddr = home->src_ipaddr;
2317 sock->my_port = src_port;
2318 sock->proto = home->proto;
2321 sock->last_packet = time(NULL);
2323 if (home->proto == IPPROTO_TCP) {
2324 this->recv = proxy_socket_tcp_recv;
2327 * FIXME: connect() is blocking!
2328 * We do this with the proxy mutex locked, which may
2329 * cause large delays!
2331 * http://www.developerweb.net/forum/showthread.php?p=13486
2333 this->fd = fr_tcp_client_socket(&home->src_ipaddr,
2334 &home->ipaddr, home->port);
2337 this->fd = fr_socket(&home->src_ipaddr, src_port);
2340 DEBUG("Failed opening client socket: %s", fr_strerror());
2346 * Figure out which port we were bound to.
2348 if (sock->my_port == 0) {
2349 struct sockaddr_storage src;
2350 socklen_t sizeof_src = sizeof(src);
2352 memset(&src, 0, sizeof_src);
2353 if (getsockname(this->fd, (struct sockaddr *) &src,
2355 radlog(L_ERR, "Failed getting socket name: %s",
2361 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2362 &sock->my_ipaddr, &sock->my_port)) {
2363 radlog(L_ERR, "Socket has unsupported address family");
2370 * Tell the event loop that we have a new FD
2372 if (!event_new_fd(this)) {
2382 static const FR_NAME_NUMBER listen_compare[] = {
2384 { "status", RAD_LISTEN_NONE },
2386 { "auth", RAD_LISTEN_AUTH },
2387 #ifdef WITH_ACCOUNTING
2388 { "acct", RAD_LISTEN_ACCT },
2391 { "detail", RAD_LISTEN_DETAIL },
2394 { "proxy", RAD_LISTEN_PROXY },
2397 { "vmps", RAD_LISTEN_VQP },
2400 { "dhcp", RAD_LISTEN_DHCP },
2402 #ifdef WITH_COMMAND_SOCKET
2403 { "control", RAD_LISTEN_COMMAND },
2406 { "coa", RAD_LISTEN_COA },
2412 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
2420 cf_log_info(cs, "listen {");
2422 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
2424 if (rcode < 0) return NULL;
2427 cf_log_err(cf_sectiontoitem(cs),
2428 "No type specified in listen section");
2432 type = fr_str2int(listen_compare, listen_type, -1);
2434 cf_log_err(cf_sectiontoitem(cs),
2435 "Invalid type \"%s\" in listen section.",
2443 * Allow listen sections in the default config to
2444 * refer to a server.
2447 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
2449 if (rcode == 1) { /* compatiblity with 2.0-pre */
2450 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
2453 if (rcode < 0) return NULL;
2458 * We were passed a virtual server, so the caller is
2459 * defining a proxy listener inside of a virtual server.
2460 * This isn't allowed right now.
2462 else if (type == RAD_LISTEN_PROXY) {
2463 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
2469 * Set up cross-type data.
2471 this = listen_alloc(type);
2472 this->server = server;
2476 * Call per-type parser.
2478 if (master_listen[type].parse(cs, this) < 0) {
2483 cf_log_info(cs, "}");
2489 static int is_loopback(const fr_ipaddr_t *ipaddr)
2492 * We shouldn't proxy on loopback.
2494 if ((ipaddr->af == AF_INET) &&
2495 (ipaddr->ipaddr.ip4addr.s_addr == htonl(INADDR_LOOPBACK))) {
2499 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2500 if ((ipaddr->af == AF_INET6) &&
2501 (IN6_IS_ADDR_LINKLOCAL(&ipaddr->ipaddr.ip6addr))) {
2511 * Generate a list of listeners. Takes an input list of
2512 * listeners, too, so we don't close sockets with waiting packets.
2514 int listen_init(CONF_SECTION *config, rad_listen_t **head)
2516 int override = FALSE;
2518 CONF_SECTION *cs = NULL;
2519 rad_listen_t **last;
2521 fr_ipaddr_t server_ipaddr;
2524 int defined_proxy = 0;
2528 * We shouldn't be called with a pre-existing list.
2530 rad_assert(head && (*head == NULL));
2533 server_ipaddr.af = AF_UNSPEC;
2536 * If the port is specified on the command-line,
2537 * it over-rides the configuration file.
2539 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
2541 if (mainconfig.port >= 0) auth_port = mainconfig.port;
2544 * If the IP address was configured on the command-line,
2545 * use that as the "bind_address"
2547 if (mainconfig.myip.af != AF_UNSPEC) {
2548 memcpy(&server_ipaddr, &mainconfig.myip,
2549 sizeof(server_ipaddr));
2555 * Else look for bind_address and/or listen sections.
2557 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
2558 rcode = cf_item_parse(config, "bind_address",
2560 &server_ipaddr.ipaddr.ip4addr, NULL);
2561 if (rcode < 0) return -1; /* error parsing it */
2563 if (rcode == 0) { /* successfully parsed IPv4 */
2564 listen_socket_t *sock;
2565 server_ipaddr.af = AF_INET;
2567 radlog(L_INFO, "WARNING: The directive 'bind_address' is deprecated, and will be removed in future versions of FreeRADIUS. Please edit the configuration files to use the directive 'listen'.");
2571 if (strcmp(progname, "vmpsd") == 0) {
2572 this = listen_alloc(RAD_LISTEN_VQP);
2573 if (!auth_port) auth_port = 1589;
2576 this = listen_alloc(RAD_LISTEN_AUTH);
2580 sock->my_ipaddr = server_ipaddr;
2581 sock->my_port = auth_port;
2583 sock->clients = clients_parse_section(config);
2584 if (!sock->clients) {
2585 cf_log_err(cf_sectiontoitem(config),
2586 "Failed to find any clients for this listen section");
2591 if (listen_bind(this) < 0) {
2593 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
2597 auth_port = sock->my_port; /* may have been updated in listen_bind */
2599 cs = cf_section_sub_find_name2(config, "server",
2601 if (cs) this->server = mainconfig.name;
2605 last = &(this->next);
2611 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
2614 #ifdef WITH_ACCOUNTING
2616 * Open Accounting Socket.
2618 * If we haven't already gotten acct_port from
2619 * /etc/services, then make it auth_port + 1.
2621 this = listen_alloc(RAD_LISTEN_ACCT);
2625 * Create the accounting socket.
2627 * The accounting port is always the
2628 * authentication port + 1
2630 sock->my_ipaddr = server_ipaddr;
2631 sock->my_port = auth_port + 1;
2633 sock->clients = clients_parse_section(config);
2634 if (!sock->clients) {
2635 cf_log_err(cf_sectiontoitem(config),
2636 "Failed to find any clients for this listen section");
2640 if (listen_bind(this) < 0) {
2643 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
2648 cs = cf_section_sub_find_name2(config, "server",
2650 if (cs) this->server = mainconfig.name;
2654 last = &(this->next);
2656 } else if (mainconfig.port > 0) { /* no bind address, but a port */
2657 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
2663 * They specified an IP on the command-line, ignore
2664 * all listen sections except the one in '-n'.
2666 if (mainconfig.myip.af != AF_UNSPEC) {
2667 CONF_SECTION *subcs;
2668 const char *name2 = cf_section_name2(cs);
2670 cs = cf_section_sub_find_name2(config, "server",
2672 if (!cs) goto add_sockets;
2675 * Should really abstract this code...
2677 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2679 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2680 this = listen_parse(subcs, name2);
2687 last = &(this->next);
2688 } /* loop over "listen" directives in server <foo> */
2694 * Walk through the "listen" sections, if they exist.
2696 for (cs = cf_subsection_find_next(config, NULL, "listen");
2698 cs = cf_subsection_find_next(config, cs, "listen")) {
2699 this = listen_parse(cs, NULL);
2706 last = &(this->next);
2710 * Check virtual servers for "listen" sections, too.
2712 * FIXME: Move to virtual server init?
2714 for (cs = cf_subsection_find_next(config, NULL, "server");
2716 cs = cf_subsection_find_next(config, cs, "server")) {
2717 CONF_SECTION *subcs;
2718 const char *name2 = cf_section_name2(cs);
2720 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2722 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2723 this = listen_parse(subcs, name2);
2730 last = &(this->next);
2731 } /* loop over "listen" directives in virtual servers */
2732 } /* loop over virtual servers */
2736 * No sockets to receive packets, this is an error.
2737 * proxying is pointless.
2740 radlog(L_ERR, "The server is not configured to listen on any ports. Cannot start.");
2745 * Print out which sockets we're listening on, and
2746 * add them to the event list.
2748 for (this = *head; this != NULL; this = this->next) {
2750 if (this->type == RAD_LISTEN_PROXY) {
2759 * If we're proxying requests, open the proxy FD.
2760 * Otherwise, don't do anything.
2763 if ((mainconfig.proxy_requests == TRUE) &&
2764 (*head != NULL) && !defined_proxy) {
2765 listen_socket_t *sock = NULL;
2769 memset(&home, 0, sizeof(home));
2774 home.proto = IPPROTO_UDP;
2775 home.src_ipaddr = server_ipaddr;
2778 * Find the first authentication port,
2781 for (this = *head; this != NULL; this = this->next) {
2782 if (this->type == RAD_LISTEN_AUTH) {
2785 if (is_loopback(&sock->my_ipaddr)) continue;
2787 if (home.src_ipaddr.af == AF_UNSPEC) {
2788 home.src_ipaddr = sock->my_ipaddr;
2790 port = sock->my_port + 2;
2794 if (this->type == RAD_LISTEN_ACCT) {
2797 if (is_loopback(&sock->my_ipaddr)) continue;
2799 if (home.src_ipaddr.af == AF_UNSPEC) {
2800 home.src_ipaddr = sock->my_ipaddr;
2802 port = sock->my_port + 1;
2809 * Address is still unspecified, use IPv4.
2811 if (home.src_ipaddr.af == AF_UNSPEC) {
2812 home.src_ipaddr.af = AF_INET;
2813 /* everything else is already set to zero */
2816 home.ipaddr.af = home.src_ipaddr.af;
2817 /* everything else is already set to zero */
2819 if (!proxy_new_listener(&home, port)) {
2827 * Haven't defined any sockets. Die.
2829 if (!*head) return -1;
2831 xlat_register("listen", xlat_listen, NULL);
2837 * Free a linked list of listeners;
2839 void listen_free(rad_listen_t **head)
2843 if (!head || !*head) return;
2847 rad_listen_t *next = this->next;
2850 * Other code may have eaten the FD.
2852 if (this->fd >= 0) close(this->fd);
2854 if (master_listen[this->type].free) {
2855 master_listen[this->type].free(this);
2859 if ((this->type == RAD_LISTEN_AUTH)
2861 || (this->type == RAD_LISTEN_ACCT)
2864 || (this->type == RAD_LISTEN_PROXY)
2867 listen_socket_t *sock = this->data;
2868 rad_free(&sock->packet);
2882 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
2887 for (this = mainconfig.listen; this != NULL; this = this->next) {
2888 listen_socket_t *sock;
2890 if ((this->type != RAD_LISTEN_AUTH)
2891 #ifdef WITH_ACCOUNTING
2892 && (this->type != RAD_LISTEN_ACCT)
2898 if ((sock->my_port == port) &&
2899 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
2900 return sock->clients;
2908 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
2912 for (this = mainconfig.listen; this != NULL; this = this->next) {
2913 listen_socket_t *sock;
2916 * FIXME: For TCP, ignore the *secondary*
2917 * listeners associated with the main socket.
2919 if ((this->type != RAD_LISTEN_AUTH)
2920 #ifdef WITH_ACCOUNTING
2921 && (this->type != RAD_LISTEN_ACCT)
2927 if ((sock->my_port == port) &&
2928 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
2932 if ((sock->my_port == port) &&
2933 fr_inaddr_any(&sock->my_ipaddr)) {