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>
37 #include <freeradius-devel/udpfromto.h>
40 #ifdef HAVE_SYS_RESOURCE_H
41 #include <sys/resource.h>
53 void print_packet(RADIUS_PACKET *packet)
55 char src[256], dst[256];
57 ip_ntoh(&packet->src_ipaddr, src, sizeof(src));
58 ip_ntoh(&packet->dst_ipaddr, dst, sizeof(dst));
60 fprintf(stderr, "ID %d: %s %d -> %s %d\n", packet->id,
61 src, packet->src_port, dst, packet->dst_port);
67 * We'll use this below.
69 typedef int (*rad_listen_parse_t)(CONF_SECTION *, rad_listen_t *);
70 typedef void (*rad_listen_free_t)(rad_listen_t *);
72 typedef struct rad_listen_master_t {
73 rad_listen_parse_t parse;
74 rad_listen_free_t free;
75 rad_listen_recv_t recv;
76 rad_listen_send_t send;
77 rad_listen_print_t print;
78 rad_listen_encode_t encode;
79 rad_listen_decode_t decode;
80 } rad_listen_master_t;
82 typedef struct listen_socket_t {
88 const char *interface;
89 RADCLIENT_LIST *clients;
92 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type);
95 * Find a per-socket client.
97 RADCLIENT *client_listener_find(const rad_listen_t *listener,
98 const fr_ipaddr_t *ipaddr, int src_port)
100 #ifdef WITH_DYNAMIC_CLIENTS
107 RADCLIENT_LIST *clients;
108 listen_socket_t *sock;
110 rad_assert(listener != NULL);
111 rad_assert(ipaddr != NULL);
113 sock = listener->data;
114 clients = sock->clients;
117 * This HAS to have been initialized previously.
119 rad_assert(clients != NULL);
121 client = client_find(clients, ipaddr,sock->proto);
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 daemon mode.
131 * If they're running in debug mode, show them
134 if (debug_flag == 0) {
135 static time_t last_printed = 0;
138 if (last_printed == now) return NULL;
143 listener->print(listener, name, sizeof(name));
145 radlog(L_ERR, "Ignoring request to %s from unknown client %s port %d"
149 , name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
150 buffer, sizeof(buffer)), src_port
152 , (sock->proto == IPPROTO_UDP) ? "udp" : "tcp"
158 #ifndef WITH_DYNAMIC_CLIENTS
159 return client; /* return the found client. */
163 * No server defined, and it's not dynamic. Return it.
165 if (!client->client_server && !client->dynamic) return client;
170 * It's a dynamically generated client, check it.
172 if (client->dynamic && (src_port != 0)) {
174 * Lives forever. Return it.
176 if (client->lifetime == 0) return client;
179 * Rate-limit the deletion of known clients.
180 * This makes them last a little longer, but
181 * prevents the server from melting down if (say)
182 * 10k clients all expire at once.
184 if (now == client->last_new_client) return client;
187 * It's not dead yet. Return it.
189 if ((client->created + client->lifetime) > now) return client;
192 * This really puts them onto a queue for later
195 client_delete(clients, client);
198 * Go find the enclosing network again.
200 client = client_find(clients, ipaddr, sock->proto);
205 if (!client) goto unknown;
206 if (!client->client_server) goto unknown;
209 * At this point, 'client' is the enclosing
210 * network that configures where dynamic clients
213 rad_assert(client->dynamic == 0);
216 * The IP is unknown, so we've found an enclosing
217 * network. Enable DoS protection. We only
218 * allow one new client per second. Known
219 * clients aren't subject to this restriction.
221 if (now == client->last_new_client) goto unknown;
224 client->last_new_client = now;
226 request = request_alloc();
227 if (!request) goto unknown;
229 request->listener = listener;
230 request->client = client;
231 request->packet = rad_recv(listener->fd, 0x02); /* MSG_PEEK */
232 if (!request->packet) { /* badly formed, etc */
233 request_free(&request);
236 request->reply = rad_alloc_reply(request->packet);
237 if (!request->reply) {
238 request_free(&request);
241 request->packet->timestamp = request->timestamp;
243 request->priority = listener->type;
244 request->server = client->client_server;
245 request->root = &mainconfig;
248 * Run a fake request through the given virtual server.
249 * Look for FreeRADIUS-Client-IP-Address
250 * FreeRADIUS-Client-Secret
253 * and create the RADCLIENT structure from that.
255 DEBUG("server %s {", request->server);
257 rcode = module_authorize(0, request);
259 DEBUG("} # server %s", request->server);
261 if (rcode != RLM_MODULE_OK) {
262 request_free(&request);
267 * If the client was updated by rlm_dynamic_clients,
268 * don't create the client from attribute-value pairs.
270 if (request->client == client) {
271 created = client_create(clients, request);
273 created = request->client;
276 * This frees the client if it isn't valid.
278 if (!client_validate(clients, client, created)) goto unknown;
280 request_free(&request);
282 if (!created) goto unknown;
288 static int listen_bind(rad_listen_t *this);
292 * Process and reply to a server-status request.
293 * Like rad_authenticate and rad_accounting this should
294 * live in it's own file but it's so small we don't bother.
296 static int rad_status_server(REQUEST *request)
298 int rcode = RLM_MODULE_OK;
301 switch (request->listener->type) {
303 case RAD_LISTEN_NONE:
305 case RAD_LISTEN_AUTH:
306 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
308 rcode = module_authorize(dval->value, request);
310 rcode = RLM_MODULE_OK;
315 case RLM_MODULE_UPDATED:
316 request->reply->code = PW_AUTHENTICATION_ACK;
319 case RLM_MODULE_FAIL:
320 case RLM_MODULE_HANDLED:
321 request->reply->code = 0; /* don't reply */
325 case RLM_MODULE_REJECT:
326 request->reply->code = PW_AUTHENTICATION_REJECT;
331 #ifdef WITH_ACCOUNTING
332 case RAD_LISTEN_ACCT:
333 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
335 rcode = module_accounting(dval->value, request);
337 rcode = RLM_MODULE_OK;
342 case RLM_MODULE_UPDATED:
343 request->reply->code = PW_ACCOUNTING_RESPONSE;
347 request->reply->code = 0; /* don't reply */
355 * This is a vendor extension. Suggested by Glen
356 * Zorn in IETF 72, and rejected by the rest of
357 * the WG. We like it, so it goes in here.
360 dval = dict_valbyname(PW_RECV_COA_TYPE, "Status-Server");
362 rcode = module_recv_coa(dval->value, request);
364 rcode = RLM_MODULE_OK;
369 case RLM_MODULE_UPDATED:
370 request->reply->code = PW_COA_ACK;
374 request->reply->code = 0; /* don't reply */
386 * Full statistics are available only on a statistics
389 if (request->listener->type == RAD_LISTEN_NONE) {
390 request_stats_reply(request);
398 static int auth_tcp_recv(rad_listen_t *listener,
399 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
402 RADIUS_PACKET *packet;
403 RAD_REQUEST_FUNP fun = NULL;
404 listen_socket_t *sock = listener->data;
405 RADCLIENT *client = sock->client;
408 * Allocate a packet for partial reads.
411 sock->packet = rad_alloc(0);
412 if (!sock->packet) return 0;
414 sock->packet->sockfd = listener->fd;
415 sock->packet->src_ipaddr = sock->other_ipaddr;
416 sock->packet->src_port = sock->other_port;
417 sock->packet->dst_ipaddr = sock->my_ipaddr;
418 sock->packet->dst_port = sock->my_port;
422 * Grab the packet currently being processed.
424 packet = sock->packet;
426 rcode = fr_tcp_read_packet(packet, 0);
429 * Still only a partial packet. Put it back, and return,
430 * so that we'll read more data when it's ready.
436 if (rcode == -1) { /* error reading packet */
439 radlog(L_ERR, "Invalid packet from %s port %d: closing socket",
440 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
444 if (rcode < 0) { /* error or connection reset */
445 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
448 * Decrement the number of connections.
450 if (sock->parent->num_connections > 0) {
451 sock->parent->num_connections--;
453 if (sock->client->num_connections > 0) {
454 sock->client->num_connections--;
458 * Tell the event handler that an FD has disappeared.
460 DEBUG("Client has closed connection");
461 event_new_fd(listener);
464 * Do NOT free the listener here. It's in use by
465 * a request, and will need to hang around until
466 * all of the requests are done.
468 * It is instead free'd in remove_from_request_hash()
473 RAD_STATS_TYPE_INC(listener, total_requests);
476 * Some sanity checks, based on the packet code.
478 switch(packet->code) {
479 case PW_AUTHENTICATION_REQUEST:
480 RAD_STATS_CLIENT_INC(listener, client, total_requests);
481 fun = rad_authenticate;
484 case PW_STATUS_SERVER:
485 if (!mainconfig.status_server) {
486 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
487 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
488 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
489 rad_free(&sock->packet);
492 fun = rad_status_server;
496 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
497 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
499 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
500 packet->code, client->shortname, packet->src_port);
501 rad_free(&sock->packet);
503 } /* switch over packet types */
505 if (!received_request(listener, packet, prequest, sock->client)) {
506 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
507 RAD_STATS_CLIENT_INC(listener, sock->client, total_packets_dropped);
508 rad_free(&sock->packet);
513 sock->packet = NULL; /* we have no need for more partial reads */
517 static int auth_tcp_accept(rad_listen_t *listener,
518 UNUSED RAD_REQUEST_FUNP *pfun,
519 UNUSED REQUEST **prequest)
524 struct sockaddr_storage src;
525 listen_socket_t *sock;
526 fr_ipaddr_t src_ipaddr;
531 DEBUG2(" ... new connection request on TCP socket.");
533 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
536 * Non-blocking sockets must handle this.
538 if (errno == EWOULDBLOCK) {
542 DEBUG2(" ... failed to accept connection.");
546 if (!fr_sockaddr2ipaddr(&src, salen, &src_ipaddr, &src_port)) {
547 DEBUG2(" ... unknown address family.");
552 * Enforce client IP address checks on accept, not on
555 if ((client = client_listener_find(listener,
556 &src_ipaddr, src_port)) == NULL) {
558 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
563 * Enforce max_connectionsx on client && listen section.
565 if ((client->max_connections != 0) &&
566 (client->max_connections == client->num_connections)) {
568 * FIXME: Print client IP/port, and server IP/port.
570 radlog(L_INFO, "Ignoring new connection due to client max_connections (%d)", client->max_connections);
575 sock = listener->data;
576 if ((sock->max_connections != 0) &&
577 (sock->max_connections == sock->num_connections)) {
579 * FIXME: Print client IP/port, and server IP/port.
581 radlog(L_INFO, "Ignoring new connection due to socket max_connections");
585 client->num_connections++;
586 sock->num_connections++;
589 * Add the new listener.
591 this = listen_alloc(listener->type);
592 if (!this) return -1;
595 * Copy everything, including the pointer to the socket
599 memcpy(this->data, listener->data, sizeof(*sock));
600 memcpy(this, listener, sizeof(*this));
602 this->data = sock; /* fix it back */
604 sock->parent = listener->data;
605 sock->other_ipaddr = src_ipaddr;
606 sock->other_port = src_port;
607 sock->client = client;
610 this->status = RAD_LISTEN_STATUS_INIT;
611 this->recv = auth_tcp_recv;
614 * FIXME: set O_NONBLOCK on the accept'd fd.
615 * See djb's portability rants for details.
619 * Tell the event loop that we have a new FD.
620 * This can be called from a child thread...
630 * This function is stupid and complicated.
632 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
635 listen_socket_t *sock = this->data;
638 switch (this->type) {
640 case RAD_LISTEN_NONE: /* what a hack... */
645 case RAD_LISTEN_AUTH:
646 name = "authentication";
649 #ifdef WITH_ACCOUNTING
650 case RAD_LISTEN_ACCT:
656 case RAD_LISTEN_PROXY:
668 case RAD_LISTEN_DHCP:
684 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
685 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
689 if (sock->interface) {
690 ADDSTRING(" interface ");
691 ADDSTRING(sock->interface);
695 if (this->recv == auth_tcp_accept) {
696 ADDSTRING(" proto tcp");
702 * TCP sockets get printed a little differently, to make
703 * it clear what's going on.
706 ADDSTRING(" from client (");
707 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
711 snprintf(buffer, bufsize, "%d", sock->other_port);
715 if ((sock->my_ipaddr.af == AF_INET) &&
716 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
717 strlcpy(buffer, "*", bufsize);
719 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
724 snprintf(buffer, bufsize, "%d", sock->my_port);
728 ADDSTRING(", virtual-server=");
729 ADDSTRING(this->server);
738 * Maybe it's a socket that we opened to a home server.
740 if ((sock->proto == IPPROTO_TCP) &&
741 (this->type == RAD_LISTEN_PROXY)) {
743 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
747 snprintf(buffer, bufsize, "%d", sock->my_port);
749 ADDSTRING(") -> home_server (");
751 if ((sock->other_ipaddr.af == AF_INET) &&
752 (sock->other_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
753 strlcpy(buffer, "*", bufsize);
755 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
760 snprintf(buffer, bufsize, "%d", sock->other_port);
769 ADDSTRING(" address ");
771 if ((sock->my_ipaddr.af == AF_INET) &&
772 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
773 strlcpy(buffer, "*", bufsize);
775 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
780 snprintf(buffer, bufsize, "%d", sock->my_port);
784 ADDSTRING(" as server ");
785 ADDSTRING(this->server);
796 * Parse an authentication or accounting socket.
798 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
803 listen_socket_t *sock = this->data;
804 char *section_name = NULL;
805 CONF_SECTION *client_cs, *parentcs;
810 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
811 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
812 &ipaddr.ipaddr.ip4addr, NULL);
813 if (rcode < 0) return -1;
815 if (rcode == 0) { /* successfully parsed IPv4 */
818 } else { /* maybe IPv6? */
819 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
820 &ipaddr.ipaddr.ip6addr, NULL);
821 if (rcode < 0) return -1;
824 cf_log_err(cf_sectiontoitem(cs),
825 "No address specified in listen section");
828 ipaddr.af = AF_INET6;
831 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
833 if (rcode < 0) return -1;
835 if ((listen_port < 0) || (listen_port > 65535)) {
836 cf_log_err(cf_sectiontoitem(cs),
837 "Invalid value for \"port\"");
841 sock->proto = IPPROTO_UDP;
843 if (cf_pair_find(cs, "proto")) {
845 cf_log_err(cf_sectiontoitem(cs),
846 "System does not support the TCP protocol. Delete this line from the configuration file.");
852 rcode = cf_item_parse(cs, "proto", PW_TYPE_STRING_PTR,
854 if (rcode < 0) return -1;
856 if (strcmp(proto, "udp") == 0) {
857 sock->proto = IPPROTO_UDP;
859 } else if (strcmp(proto, "tcp") == 0) {
860 sock->proto = IPPROTO_TCP;
862 rcode = cf_item_parse(cs, "max_connections", PW_TYPE_INTEGER,
863 &sock->max_connections, "64");
864 if (rcode < 0) return -1;
867 cf_log_err(cf_sectiontoitem(cs),
868 "Unknown proto name \"%s\"", proto);
875 * TCP requires a destination IP for sockets.
876 * UDP doesn't, so it's allowed.
878 if ((this->type == RAD_LISTEN_PROXY) &&
879 (sock->proto != IPPROTO_UDP)) {
880 cf_log_err(cf_sectiontoitem(cs),
881 "Proxy listeners can only listen on proto = udp");
887 sock->my_ipaddr = ipaddr;
888 sock->my_port = listen_port;
891 * If we can bind to interfaces, do so,
894 if (cf_pair_find(cs, "interface")) {
896 CONF_PAIR *cp = cf_pair_find(cs, "interface");
898 rad_assert(cp != NULL);
899 value = cf_pair_value(cp);
901 cf_log_err(cf_sectiontoitem(cs),
902 "No interface name given");
905 sock->interface = value;
910 * If we can do broadcasts..
912 if (cf_pair_find(cs, "broadcast")) {
914 cf_log_err(cf_sectiontoitem(cs),
915 "System does not support broadcast sockets. Delete this line from the configuration file.");
919 CONF_PAIR *cp = cf_pair_find(cs, "broadcast");
921 if (this->type != RAD_LISTEN_DHCP) {
922 cf_log_err(cf_pairtoitem(cp),
923 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file.");
927 rad_assert(cp != NULL);
928 value = cf_pair_value(cp);
930 cf_log_err(cf_sectiontoitem(cs),
931 "No broadcast value given");
936 * Hack... whatever happened to cf_section_parse?
938 sock->broadcast = (strcmp(value, "yes") == 0);
944 * And bind it to the port.
946 if (listen_bind(this) < 0) {
948 cf_log_err(cf_sectiontoitem(cs),
949 "Error binding to port for %s port %d",
950 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
957 * Proxy sockets don't have clients.
959 if (this->type == RAD_LISTEN_PROXY) return 0;
963 * The more specific configurations are preferred to more
967 parentcs = cf_top_section(cs);
968 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
969 §ion_name, NULL);
970 if (rcode < 0) return -1; /* bad string */
973 * Explicit list given: use it.
975 client_cs = cf_section_sub_find_name2(parentcs,
979 client_cs = cf_section_find(section_name);
982 cf_log_err(cf_sectiontoitem(cs),
983 "Failed to find clients %s {...}",
989 } /* else there was no "clients = " entry. */
992 CONF_SECTION *server_cs;
994 server_cs = cf_section_sub_find_name2(parentcs,
998 * Found a "server foo" section. If there are clients
1002 (cf_section_sub_find(server_cs, "client") != NULL)) {
1003 client_cs = server_cs;
1008 * Still nothing. Look for global clients.
1010 if (!client_cs) client_cs = parentcs;
1012 sock->clients = clients_parse_section(client_cs);
1013 if (!sock->clients) {
1014 cf_log_err(cf_sectiontoitem(cs),
1015 "Failed to load clients for this listen section");
1020 if (sock->proto == IPPROTO_TCP) {
1022 * Re-write the listener receive function to
1023 * allow us to accept the socket.
1025 this->recv = auth_tcp_accept;
1033 * Send an authentication response packet
1035 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1037 rad_assert(request->listener == listener);
1038 rad_assert(listener->send == auth_socket_send);
1040 return rad_send(request->reply, request->packet,
1041 request->client->secret);
1045 #ifdef WITH_ACCOUNTING
1047 * Send an accounting response packet (or not)
1049 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1051 rad_assert(request->listener == listener);
1052 rad_assert(listener->send == acct_socket_send);
1055 * Accounting reject's are silently dropped.
1057 * We do it here to avoid polluting the rest of the
1058 * code with this knowledge
1060 if (request->reply->code == 0) return 0;
1062 return rad_send(request->reply, request->packet,
1063 request->client->secret);
1069 * Send a packet to a home server.
1071 * FIXME: have different code for proxy auth & acct!
1073 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1075 rad_assert(request->proxy_listener == listener);
1076 rad_assert(listener->send == proxy_socket_send);
1078 return rad_send(request->proxy, request->packet,
1079 request->home_server->secret);
1085 * Check if an incoming request is "ok"
1087 * It takes packets, not requests. It sees if the packet looks
1088 * OK. If so, it does a number of sanity checks on it.
1090 static int stats_socket_recv(rad_listen_t *listener,
1091 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1095 RADIUS_PACKET *packet;
1097 fr_ipaddr_t src_ipaddr;
1099 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1100 if (rcode < 0) return 0;
1102 RAD_STATS_TYPE_INC(listener, total_requests);
1104 if (rcode < 20) { /* AUTH_HDR_LEN */
1105 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1109 if ((client = client_listener_find(listener,
1110 &src_ipaddr, src_port)) == NULL) {
1111 rad_recv_discard(listener->fd);
1112 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1117 * We only understand Status-Server on this socket.
1119 if (code != PW_STATUS_SERVER) {
1120 DEBUG("Ignoring packet code %d sent to Status-Server port",
1122 rad_recv_discard(listener->fd);
1123 RAD_STATS_TYPE_INC(listener, total_unknown_types);
1124 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1129 * Now that we've sanity checked everything, receive the
1132 packet = rad_recv(listener->fd, 1); /* require message authenticator */
1134 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1135 DEBUG("%s", fr_strerror());
1139 if (!received_request(listener, packet, prequest, client)) {
1140 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1141 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1146 *pfun = rad_status_server;
1153 * Check if an incoming request is "ok"
1155 * It takes packets, not requests. It sees if the packet looks
1156 * OK. If so, it does a number of sanity checks on it.
1158 static int auth_socket_recv(rad_listen_t *listener,
1159 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1163 RADIUS_PACKET *packet;
1164 RAD_REQUEST_FUNP fun = NULL;
1166 fr_ipaddr_t src_ipaddr;
1168 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1169 if (rcode < 0) return 0;
1171 RAD_STATS_TYPE_INC(listener, total_requests);
1173 if (rcode < 20) { /* AUTH_HDR_LEN */
1174 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1178 if ((client = client_listener_find(listener,
1179 &src_ipaddr, src_port)) == NULL) {
1180 rad_recv_discard(listener->fd);
1181 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1186 * Some sanity checks, based on the packet code.
1189 case PW_AUTHENTICATION_REQUEST:
1190 RAD_STATS_CLIENT_INC(listener, client, total_requests);
1191 fun = rad_authenticate;
1194 case PW_STATUS_SERVER:
1195 if (!mainconfig.status_server) {
1196 rad_recv_discard(listener->fd);
1197 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1198 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1199 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1202 fun = rad_status_server;
1206 rad_recv_discard(listener->fd);
1207 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
1208 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1210 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
1211 code, client->shortname, src_port);
1214 } /* switch over packet types */
1217 * Now that we've sanity checked everything, receive the
1220 packet = rad_recv(listener->fd, client->message_authenticator);
1222 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1223 DEBUG("%s", fr_strerror());
1227 if (!received_request(listener, packet, prequest, client)) {
1228 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1229 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1239 #ifdef WITH_ACCOUNTING
1241 * Receive packets from an accounting socket
1243 static int acct_socket_recv(rad_listen_t *listener,
1244 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1248 RADIUS_PACKET *packet;
1249 RAD_REQUEST_FUNP fun = NULL;
1251 fr_ipaddr_t src_ipaddr;
1253 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1254 if (rcode < 0) return 0;
1256 RAD_STATS_TYPE_INC(listener, total_requests);
1258 if (rcode < 20) { /* AUTH_HDR_LEN */
1259 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1263 if ((client = client_listener_find(listener,
1264 &src_ipaddr, src_port)) == NULL) {
1265 rad_recv_discard(listener->fd);
1266 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1271 * Some sanity checks, based on the packet code.
1274 case PW_ACCOUNTING_REQUEST:
1275 RAD_STATS_CLIENT_INC(listener, client, total_requests);
1276 fun = rad_accounting;
1279 case PW_STATUS_SERVER:
1280 if (!mainconfig.status_server) {
1281 rad_recv_discard(listener->fd);
1282 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1283 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1285 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1288 fun = rad_status_server;
1292 rad_recv_discard(listener->fd);
1293 RAD_STATS_TYPE_INC(listener, total_unknown_types);
1294 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1296 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1297 code, client->shortname, src_port);
1299 } /* switch over packet types */
1302 * Now that we've sanity checked everything, receive the
1305 packet = rad_recv(listener->fd, 0);
1307 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1308 radlog(L_ERR, "%s", fr_strerror());
1313 * There can be no duplicate accounting packets.
1315 if (!received_request(listener, packet, prequest, client)) {
1316 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1317 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1330 * For now, all CoA requests are *only* originated, and not
1331 * proxied. So all of the necessary work is done in the
1332 * post-proxy section, which is automatically handled by event.c.
1333 * As a result, we don't have to do anything here.
1335 static int rad_coa_reply(REQUEST *request)
1337 VALUE_PAIR *s1, *s2;
1340 * Inform the user about RFC requirements.
1342 s1 = pairfind(request->proxy->vps, PW_STATE);
1344 s2 = pairfind(request->proxy_reply->vps, PW_STATE);
1347 DEBUG("WARNING: Client was sent State in CoA, and did not respond with State.");
1349 } else if ((s1->length != s2->length) ||
1350 (memcmp(s1->vp_octets, s2->vp_octets,
1351 s1->length) != 0)) {
1352 DEBUG("WARNING: Client was sent State in CoA, and did not respond with the same State.");
1356 return RLM_MODULE_OK;
1360 * Receive a CoA packet.
1362 static int rad_coa_recv(REQUEST *request)
1364 int rcode = RLM_MODULE_OK;
1369 * Get the correct response
1371 switch (request->packet->code) {
1372 case PW_COA_REQUEST:
1377 case PW_DISCONNECT_REQUEST:
1378 ack = PW_DISCONNECT_ACK;
1379 nak = PW_DISCONNECT_NAK;
1382 default: /* shouldn't happen */
1383 return RLM_MODULE_FAIL;
1387 #define WAS_PROXIED (request->proxy)
1389 #define WAS_PROXIED (0)
1394 * RFC 5176 Section 3.3. If we have a CoA-Request
1395 * with Service-Type = Authorize-Only, it MUST
1396 * have a State attribute in it.
1398 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE);
1399 if (request->packet->code == PW_COA_REQUEST) {
1400 if (vp && (vp->vp_integer == 17)) {
1401 vp = pairfind(request->packet->vps, PW_STATE);
1402 if (!vp || (vp->length == 0)) {
1403 RDEBUG("ERROR: CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1404 request->reply->code = PW_COA_NAK;
1405 return RLM_MODULE_FAIL;
1410 * RFC 5176, Section 3.2.
1412 RDEBUG("ERROR: Disconnect-Request MUST NOT contain a Service-Type attribute");
1413 request->reply->code = PW_DISCONNECT_NAK;
1414 return RLM_MODULE_FAIL;
1417 rcode = module_recv_coa(0, request);
1419 case RLM_MODULE_FAIL:
1420 case RLM_MODULE_INVALID:
1421 case RLM_MODULE_REJECT:
1422 case RLM_MODULE_USERLOCK:
1424 request->reply->code = nak;
1427 case RLM_MODULE_HANDLED:
1430 case RLM_MODULE_NOOP:
1431 case RLM_MODULE_NOTFOUND:
1433 case RLM_MODULE_UPDATED:
1434 request->reply->code = ack;
1439 * Start the reply code with the proxy reply
1442 request->reply->code = request->proxy_reply->code;
1446 * Copy State from the request to the reply.
1447 * See RFC 5176 Section 3.3.
1449 vp = paircopy2(request->packet->vps, PW_STATE);
1450 if (vp) pairadd(&request->reply->vps, vp);
1453 * We may want to over-ride the reply.
1455 rcode = module_send_coa(0, request);
1458 * We need to send CoA-NAK back if Service-Type
1459 * is Authorize-Only. Rely on the user's policy
1460 * to do that. We're not a real NAS, so this
1461 * restriction doesn't (ahem) apply to us.
1463 case RLM_MODULE_FAIL:
1464 case RLM_MODULE_INVALID:
1465 case RLM_MODULE_REJECT:
1466 case RLM_MODULE_USERLOCK:
1469 * Over-ride an ACK with a NAK
1471 request->reply->code = nak;
1474 case RLM_MODULE_HANDLED:
1477 case RLM_MODULE_NOOP:
1478 case RLM_MODULE_NOTFOUND:
1480 case RLM_MODULE_UPDATED:
1482 * Do NOT over-ride a previously set value.
1483 * Otherwise an "ok" here will re-write a
1486 if (request->reply->code == 0) {
1487 request->reply->code = ack;
1493 return RLM_MODULE_OK;
1498 * Check if an incoming request is "ok"
1500 * It takes packets, not requests. It sees if the packet looks
1501 * OK. If so, it does a number of sanity checks on it.
1503 static int coa_socket_recv(rad_listen_t *listener,
1504 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1508 RADIUS_PACKET *packet;
1509 RAD_REQUEST_FUNP fun = NULL;
1511 fr_ipaddr_t src_ipaddr;
1513 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1514 if (rcode < 0) return 0;
1516 RAD_STATS_TYPE_INC(listener, total_requests);
1518 if (rcode < 20) { /* AUTH_HDR_LEN */
1519 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1523 if ((client = client_listener_find(listener,
1524 &src_ipaddr, src_port)) == NULL) {
1525 rad_recv_discard(listener->fd);
1526 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1531 * Some sanity checks, based on the packet code.
1534 case PW_COA_REQUEST:
1535 case PW_DISCONNECT_REQUEST:
1540 rad_recv_discard(listener->fd);
1541 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1542 code, client->shortname, src_port);
1545 } /* switch over packet types */
1548 * Now that we've sanity checked everything, receive the
1551 packet = rad_recv(listener->fd, client->message_authenticator);
1553 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1554 DEBUG("%s", fr_strerror());
1558 if (!received_request(listener, packet, prequest, client)) {
1570 * Recieve packets from a proxy socket.
1572 static int proxy_socket_recv(rad_listen_t *listener,
1573 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1576 RADIUS_PACKET *packet;
1577 RAD_REQUEST_FUNP fun = NULL;
1580 packet = rad_recv(listener->fd, 0);
1582 radlog(L_ERR, "%s", fr_strerror());
1587 * FIXME: Client MIB updates?
1589 switch(packet->code) {
1590 case PW_AUTHENTICATION_ACK:
1591 case PW_ACCESS_CHALLENGE:
1592 case PW_AUTHENTICATION_REJECT:
1593 fun = rad_authenticate;
1596 #ifdef WITH_ACCOUNTING
1597 case PW_ACCOUNTING_RESPONSE:
1598 fun = rad_accounting;
1603 case PW_DISCONNECT_ACK:
1604 case PW_DISCONNECT_NAK:
1607 fun = rad_coa_reply;
1613 * FIXME: Update MIB for packet types?
1615 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1616 "from home server %s port %d - ID %d : IGNORED",
1618 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1619 packet->src_port, packet->id);
1624 request = received_proxy_response(packet);
1632 * Distinguish proxied CoA requests from ones we
1635 if ((fun == rad_coa_reply) &&
1636 (request->packet->code == request->proxy->code)) {
1641 rad_assert(fun != NULL);
1643 *prequest = request;
1650 * Recieve packets from a proxy socket.
1652 static int proxy_socket_tcp_recv(rad_listen_t *listener,
1653 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1656 RADIUS_PACKET *packet;
1657 RAD_REQUEST_FUNP fun = NULL;
1658 listen_socket_t *sock = listener->data;
1661 packet = fr_tcp_recv(listener->fd, 0);
1663 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
1664 event_new_fd(listener);
1669 * FIXME: Client MIB updates?
1671 switch(packet->code) {
1672 case PW_AUTHENTICATION_ACK:
1673 case PW_ACCESS_CHALLENGE:
1674 case PW_AUTHENTICATION_REJECT:
1675 fun = rad_authenticate;
1678 #ifdef WITH_ACCOUNTING
1679 case PW_ACCOUNTING_RESPONSE:
1680 fun = rad_accounting;
1686 * FIXME: Update MIB for packet types?
1688 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1689 "from home server %s port %d - ID %d : IGNORED",
1691 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1692 packet->src_port, packet->id);
1697 packet->src_ipaddr = sock->other_ipaddr;
1698 packet->src_port = sock->other_port;
1699 packet->dst_ipaddr = sock->my_ipaddr;
1700 packet->dst_port = sock->my_port;
1703 * FIXME: Have it return an indication of packets that
1704 * are OK to ignore (dups, too late), versus ones that
1705 * aren't OK to ignore (unknown response, spoofed, etc.)
1707 * Close the socket on bad packets...
1709 request = received_proxy_response(packet);
1714 rad_assert(fun != NULL);
1715 sock->opened = sock->last_packet = request->timestamp;
1718 *prequest = request;
1726 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1728 if (!request->reply->code) return 0;
1730 rad_encode(request->reply, request->packet,
1731 request->client->secret);
1732 rad_sign(request->reply, request->packet,
1733 request->client->secret);
1739 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1741 if (rad_verify(request->packet, NULL,
1742 request->client->secret) < 0) {
1746 return rad_decode(request->packet, NULL,
1747 request->client->secret);
1751 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1753 rad_encode(request->proxy, NULL, request->home_server->secret);
1754 rad_sign(request->proxy, NULL, request->home_server->secret);
1760 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1763 * rad_verify is run in event.c, received_proxy_response()
1766 return rad_decode(request->proxy_reply, request->proxy,
1767 request->home_server->secret);
1773 #include "command.c"
1775 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1777 { common_socket_parse, NULL,
1778 stats_socket_recv, auth_socket_send,
1779 socket_print, client_socket_encode, client_socket_decode },
1782 * This always gets defined.
1784 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1789 { common_socket_parse, NULL,
1790 proxy_socket_recv, proxy_socket_send,
1791 socket_print, proxy_socket_encode, proxy_socket_decode },
1794 /* authentication */
1795 { common_socket_parse, NULL,
1796 auth_socket_recv, auth_socket_send,
1797 socket_print, client_socket_encode, client_socket_decode },
1799 #ifdef WITH_ACCOUNTING
1801 { common_socket_parse, NULL,
1802 acct_socket_recv, acct_socket_send,
1803 socket_print, client_socket_encode, client_socket_decode},
1808 { detail_parse, detail_free,
1809 detail_recv, detail_send,
1810 detail_print, detail_encode, detail_decode },
1814 /* vlan query protocol */
1815 { common_socket_parse, NULL,
1816 vqp_socket_recv, vqp_socket_send,
1817 socket_print, vqp_socket_encode, vqp_socket_decode },
1821 /* dhcp query protocol */
1822 { dhcp_socket_parse, NULL,
1823 dhcp_socket_recv, dhcp_socket_send,
1824 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1827 #ifdef WITH_COMMAND_SOCKET
1828 /* TCP command socket */
1829 { command_socket_parse, NULL,
1830 command_domain_accept, command_domain_send,
1831 command_socket_print, command_socket_encode, command_socket_decode },
1835 /* Change of Authorization */
1836 { common_socket_parse, NULL,
1837 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
1838 socket_print, client_socket_encode, client_socket_decode },
1846 * Binds a listener to a socket.
1848 static int listen_bind(rad_listen_t *this)
1851 struct sockaddr_storage salocal;
1853 listen_socket_t *sock = this->data;
1855 #define proto_for_port "udp"
1856 #define sock_type SOCK_DGRAM
1858 const char *proto_for_port = "udp";
1859 int sock_type = SOCK_DGRAM;
1861 if (sock->proto == IPPROTO_TCP) {
1863 if (this->type == RAD_LISTEN_VQP) {
1864 radlog(L_ERR, "VQP does not support TCP transport");
1869 proto_for_port = "tcp";
1870 sock_type = SOCK_STREAM;
1875 * If the port is zero, then it means the appropriate
1876 * thing from /etc/services.
1878 if (sock->my_port == 0) {
1879 struct servent *svp;
1881 switch (this->type) {
1882 case RAD_LISTEN_AUTH:
1883 svp = getservbyname ("radius", proto_for_port);
1885 sock->my_port = ntohs(svp->s_port);
1887 sock->my_port = PW_AUTH_UDP_PORT;
1891 #ifdef WITH_ACCOUNTING
1892 case RAD_LISTEN_ACCT:
1893 svp = getservbyname ("radacct", proto_for_port);
1895 sock->my_port = ntohs(svp->s_port);
1897 sock->my_port = PW_ACCT_UDP_PORT;
1903 case RAD_LISTEN_PROXY:
1904 /* leave it at zero */
1909 case RAD_LISTEN_VQP:
1910 sock->my_port = 1589;
1915 case RAD_LISTEN_COA:
1916 svp = getservbyname ("radius-dynauth", "udp");
1918 sock->port = ntohs(svp->s_port);
1920 sock->port = PW_COA_UDP_PORT;
1926 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1932 * Copy fr_socket() here, as we may need to bind to a device.
1934 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
1936 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
1941 * Bind to a device BEFORE touching IP addresses.
1943 if (sock->interface) {
1944 #ifdef SO_BINDTODEVICE
1946 strcpy(ifreq.ifr_name, sock->interface);
1949 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1950 (char *)&ifreq, sizeof(ifreq));
1954 radlog(L_ERR, "Failed binding to interface %s: %s",
1955 sock->interface, strerror(errno));
1957 } /* else it worked. */
1959 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1960 #ifdef HAVE_NET_IF_H
1962 * Odds are that any system supporting "bind to
1963 * device" also supports IPv6, so this next bit
1964 * isn't necessary. But it's here for
1967 * If we're doing IPv6, and the scope hasn't yet
1968 * been defined, set the scope to the scope of
1971 if (sock->ipaddr.af == AF_INET6) {
1972 if (sock->ipaddr.scope == 0) {
1973 sock->ipaddr.scope = if_nametoindex(sock->interface);
1974 if (sock->ipaddr.scope == 0) {
1976 radlog(L_ERR, "Failed finding interface %s: %s",
1977 sock->interface, strerror(errno));
1980 } /* else scope was defined: we're OK. */
1985 * IPv4: no link local addresses,
1986 * and no bind to device.
1990 radlog(L_ERR, "Failed binding to interface %s: \"bind to device\" is unsupported", sock->interface);
1997 if (sock->proto == IPPROTO_TCP) {
2000 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2002 radlog(L_ERR, "Failed to reuse address: %s", strerror(errno));
2008 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
2009 else /* UDP sockets get UDPfromto */
2012 #ifdef WITH_UDPFROMTO
2014 * Initialize udpfromto for all sockets.
2016 if (udpfromto_init(this->fd) != 0) {
2023 * Set up sockaddr stuff.
2025 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
2030 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2031 if (sock->my_ipaddr.af == AF_INET6) {
2033 * Listening on '::' does NOT get you IPv4 to
2034 * IPv6 mapping. You've got to listen on an IPv4
2035 * address, too. This makes the rest of the server
2036 * design a little simpler.
2040 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2043 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2044 (char *)&on, sizeof(on));
2046 #endif /* IPV6_V6ONLY */
2048 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2050 if (sock->my_ipaddr.af == AF_INET) {
2053 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2055 * Disable PMTU discovery. On Linux, this
2056 * also makes sure that the "don't fragment"
2059 flag = IP_PMTUDISC_DONT;
2060 setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2061 &flag, sizeof(flag));
2064 #if defined(IP_DONTFRAG)
2066 * Ensure that the "don't fragment" flag is zero.
2069 setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2070 &flag, sizeof(flag));
2076 if (sock->broadcast) {
2079 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2080 radlog(L_ERR, "Can't set broadcast option: %s\n",
2089 * May be binding to priviledged ports.
2091 if (sock->my_port != 0) {
2095 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2096 radlog(L_ERR, "Can't set re-use address option: %s\n",
2103 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2109 this->print(this, buffer, sizeof(buffer));
2110 radlog(L_ERR, "Failed binding to %s: %s\n",
2111 buffer, strerror(errno));
2116 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2117 * kernel instead binds us to a 1.2.3.4. If this
2118 * happens, notice, and remember our real IP.
2121 struct sockaddr_storage src;
2122 socklen_t sizeof_src = sizeof(src);
2124 memset(&src, 0, sizeof_src);
2125 if (getsockname(this->fd, (struct sockaddr *) &src,
2127 radlog(L_ERR, "Failed getting socket name: %s",
2132 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2133 &sock->my_ipaddr, &sock->my_port)) {
2134 radlog(L_ERR, "Socket has unsupported address family");
2141 if (sock->proto == IPPROTO_TCP) {
2142 if (listen(this->fd, 8) < 0) {
2144 radlog(L_ERR, "Failed in listen(): %s", strerror(errno));
2150 if (fr_nonblock(this->fd) < 0) {
2152 radlog(L_ERR, "Failed setting non-blocking on socket: %s",
2158 * Mostly for proxy sockets.
2160 sock->other_ipaddr.af = sock->my_ipaddr.af;
2163 * Don't screw up other people.
2165 #undef proto_for_port
2173 * Allocate & initialize a new listener.
2175 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
2179 this = rad_malloc(sizeof(*this));
2180 memset(this, 0, sizeof(*this));
2183 this->recv = master_listen[this->type].recv;
2184 this->send = master_listen[this->type].send;
2185 this->print = master_listen[this->type].print;
2186 this->encode = master_listen[this->type].encode;
2187 this->decode = master_listen[this->type].decode;
2191 case RAD_LISTEN_NONE:
2193 case RAD_LISTEN_AUTH:
2194 #ifdef WITH_ACCOUNTING
2195 case RAD_LISTEN_ACCT:
2198 case RAD_LISTEN_PROXY:
2201 case RAD_LISTEN_VQP:
2204 case RAD_LISTEN_COA:
2206 this->data = rad_malloc(sizeof(listen_socket_t));
2207 memset(this->data, 0, sizeof(listen_socket_t));
2211 case RAD_LISTEN_DHCP:
2212 this->data = rad_malloc(sizeof(dhcp_socket_t));
2213 memset(this->data, 0, sizeof(dhcp_socket_t));
2218 case RAD_LISTEN_DETAIL:
2223 #ifdef WITH_COMMAND_SOCKET
2224 case RAD_LISTEN_COMMAND:
2225 this->data = rad_malloc(sizeof(fr_command_socket_t));
2226 memset(this->data, 0, sizeof(fr_command_socket_t));
2231 rad_assert("Unsupported option!" == NULL);
2240 * Externally visible function for creating a new proxy LISTENER.
2242 * Not thread-safe, but all calls to it are protected by the
2243 * proxy mutex in event.c
2245 int proxy_new_listener(home_server *home, int src_port)
2248 listen_socket_t *sock;
2250 if (!home) return 0;
2252 if ((home->max_connections > 0) &&
2253 (home->num_connections >= home->max_connections)) {
2254 DEBUG("WARNING: Home server has too many open connections (%d)",
2255 home->max_connections);
2259 this = listen_alloc(RAD_LISTEN_PROXY);
2262 sock->other_ipaddr = home->ipaddr;
2263 sock->other_port = home->port;
2266 sock->my_ipaddr = home->src_ipaddr;
2267 sock->my_port = src_port;
2268 sock->proto = home->proto;
2271 sock->last_packet = time(NULL);
2273 if (home->proto == IPPROTO_TCP) {
2274 this->recv = proxy_socket_tcp_recv;
2277 * FIXME: connect() is blocking!
2278 * We do this with the proxy mutex locked, which may
2279 * cause large delays!
2281 * http://www.developerweb.net/forum/showthread.php?p=13486
2283 this->fd = fr_tcp_client_socket(&home->src_ipaddr,
2284 &home->ipaddr, home->port);
2287 this->fd = fr_socket(&home->src_ipaddr, src_port);
2290 DEBUG("Failed opening client socket: %s", fr_strerror());
2296 * Figure out which port we were bound to.
2298 if (sock->my_port == 0) {
2299 struct sockaddr_storage src;
2300 socklen_t sizeof_src = sizeof(src);
2302 memset(&src, 0, sizeof_src);
2303 if (getsockname(this->fd, (struct sockaddr *) &src,
2305 radlog(L_ERR, "Failed getting socket name: %s",
2311 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2312 &sock->my_ipaddr, &sock->my_port)) {
2313 radlog(L_ERR, "Socket has unsupported address family");
2320 * Tell the event loop that we have a new FD
2322 if (!event_new_fd(this)) {
2332 static const FR_NAME_NUMBER listen_compare[] = {
2334 { "status", RAD_LISTEN_NONE },
2336 { "auth", RAD_LISTEN_AUTH },
2337 #ifdef WITH_ACCOUNTING
2338 { "acct", RAD_LISTEN_ACCT },
2341 { "detail", RAD_LISTEN_DETAIL },
2344 { "proxy", RAD_LISTEN_PROXY },
2347 { "vmps", RAD_LISTEN_VQP },
2350 { "dhcp", RAD_LISTEN_DHCP },
2352 #ifdef WITH_COMMAND_SOCKET
2353 { "control", RAD_LISTEN_COMMAND },
2356 { "coa", RAD_LISTEN_COA },
2362 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
2370 cf_log_info(cs, "listen {");
2372 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
2374 if (rcode < 0) return NULL;
2377 cf_log_err(cf_sectiontoitem(cs),
2378 "No type specified in listen section");
2382 type = fr_str2int(listen_compare, listen_type, -1);
2384 cf_log_err(cf_sectiontoitem(cs),
2385 "Invalid type \"%s\" in listen section.",
2393 * Allow listen sections in the default config to
2394 * refer to a server.
2397 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
2399 if (rcode == 1) { /* compatiblity with 2.0-pre */
2400 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
2403 if (rcode < 0) return NULL;
2408 * We were passed a virtual server, so the caller is
2409 * defining a proxy listener inside of a virtual server.
2410 * This isn't allowed right now.
2412 else if (type == RAD_LISTEN_PROXY) {
2413 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
2419 * Set up cross-type data.
2421 this = listen_alloc(type);
2422 this->server = server;
2426 * Call per-type parser.
2428 if (master_listen[type].parse(cs, this) < 0) {
2433 cf_log_info(cs, "}");
2439 * Generate a list of listeners. Takes an input list of
2440 * listeners, too, so we don't close sockets with waiting packets.
2442 int listen_init(CONF_SECTION *config, rad_listen_t **head)
2444 int override = FALSE;
2446 CONF_SECTION *cs = NULL;
2447 rad_listen_t **last;
2449 fr_ipaddr_t server_ipaddr;
2452 int defined_proxy = 0;
2456 * We shouldn't be called with a pre-existing list.
2458 rad_assert(head && (*head == NULL));
2461 server_ipaddr.af = AF_UNSPEC;
2464 * If the port is specified on the command-line,
2465 * it over-rides the configuration file.
2467 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
2469 if (mainconfig.port >= 0) auth_port = mainconfig.port;
2472 * If the IP address was configured on the command-line,
2473 * use that as the "bind_address"
2475 if (mainconfig.myip.af != AF_UNSPEC) {
2476 memcpy(&server_ipaddr, &mainconfig.myip,
2477 sizeof(server_ipaddr));
2483 * Else look for bind_address and/or listen sections.
2485 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
2486 rcode = cf_item_parse(config, "bind_address",
2488 &server_ipaddr.ipaddr.ip4addr, NULL);
2489 if (rcode < 0) return -1; /* error parsing it */
2491 if (rcode == 0) { /* successfully parsed IPv4 */
2492 listen_socket_t *sock;
2493 server_ipaddr.af = AF_INET;
2495 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'.");
2499 if (strcmp(progname, "vmpsd") == 0) {
2500 this = listen_alloc(RAD_LISTEN_VQP);
2501 if (!auth_port) auth_port = 1589;
2504 this = listen_alloc(RAD_LISTEN_AUTH);
2508 sock->my_ipaddr = server_ipaddr;
2509 sock->my_port = auth_port;
2511 sock->clients = clients_parse_section(config);
2512 if (!sock->clients) {
2513 cf_log_err(cf_sectiontoitem(config),
2514 "Failed to find any clients for this listen section");
2519 if (listen_bind(this) < 0) {
2521 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
2525 auth_port = sock->my_port; /* may have been updated in listen_bind */
2527 cs = cf_section_sub_find_name2(config, "server",
2529 if (cs) this->server = mainconfig.name;
2533 last = &(this->next);
2539 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
2542 #ifdef WITH_ACCOUNTING
2544 * Open Accounting Socket.
2546 * If we haven't already gotten acct_port from
2547 * /etc/services, then make it auth_port + 1.
2549 this = listen_alloc(RAD_LISTEN_ACCT);
2553 * Create the accounting socket.
2555 * The accounting port is always the
2556 * authentication port + 1
2558 sock->my_ipaddr = server_ipaddr;
2559 sock->my_port = auth_port + 1;
2561 sock->clients = clients_parse_section(config);
2562 if (!sock->clients) {
2563 cf_log_err(cf_sectiontoitem(config),
2564 "Failed to find any clients for this listen section");
2568 if (listen_bind(this) < 0) {
2571 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
2576 cs = cf_section_sub_find_name2(config, "server",
2578 if (cs) this->server = mainconfig.name;
2582 last = &(this->next);
2584 } else if (mainconfig.port > 0) { /* no bind address, but a port */
2585 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
2591 * They specified an IP on the command-line, ignore
2592 * all listen sections except the one in '-n'.
2594 if (mainconfig.myip.af != AF_UNSPEC) {
2595 CONF_SECTION *subcs;
2596 const char *name2 = cf_section_name2(cs);
2598 cs = cf_section_sub_find_name2(config, "server",
2600 if (!cs) goto add_sockets;
2603 * Should really abstract this code...
2605 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2607 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2608 this = listen_parse(subcs, name2);
2615 last = &(this->next);
2616 } /* loop over "listen" directives in server <foo> */
2622 * Walk through the "listen" sections, if they exist.
2624 for (cs = cf_subsection_find_next(config, NULL, "listen");
2626 cs = cf_subsection_find_next(config, cs, "listen")) {
2627 this = listen_parse(cs, NULL);
2634 last = &(this->next);
2638 * Check virtual servers for "listen" sections, too.
2640 * FIXME: Move to virtual server init?
2642 for (cs = cf_subsection_find_next(config, NULL, "server");
2644 cs = cf_subsection_find_next(config, cs, "server")) {
2645 CONF_SECTION *subcs;
2646 const char *name2 = cf_section_name2(cs);
2648 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2650 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2651 this = listen_parse(subcs, name2);
2658 last = &(this->next);
2659 } /* loop over "listen" directives in virtual servers */
2660 } /* loop over virtual servers */
2664 * Print out which sockets we're listening on, and
2665 * add them to the event list.
2667 for (this = *head; this != NULL; this = this->next) {
2669 if (this->type == RAD_LISTEN_PROXY) {
2678 * If we're proxying requests, open the proxy FD.
2679 * Otherwise, don't do anything.
2682 if ((mainconfig.proxy_requests == TRUE) &&
2683 (*head != NULL) && !defined_proxy) {
2684 listen_socket_t *sock = NULL;
2688 memset(&home, 0, sizeof(home));
2693 home.proto = IPPROTO_UDP;
2694 home.src_ipaddr = server_ipaddr;
2697 * Find the first authentication port,
2700 for (this = *head; this != NULL; this = this->next) {
2701 if (this->type == RAD_LISTEN_AUTH) {
2703 if (home.src_ipaddr.af == AF_UNSPEC) {
2704 home.src_ipaddr = sock->my_ipaddr;
2706 port = sock->my_port + 2;
2710 if (this->type == RAD_LISTEN_ACCT) {
2712 if (home.src_ipaddr.af == AF_UNSPEC) {
2713 home.src_ipaddr = sock->my_ipaddr;
2715 port = sock->my_port + 1;
2722 * Address is still unspecified, use IPv4.
2724 if (home.src_ipaddr.af == AF_UNSPEC) {
2725 home.src_ipaddr.af = AF_INET;
2726 /* everything else is already set to zero */
2729 home.ipaddr.af = home.src_ipaddr.af;
2730 /* everything else is already set to zero */
2732 if (!proxy_new_listener(&home, port)) {
2740 * Haven't defined any sockets. Die.
2742 if (!*head) return -1;
2749 * Free a linked list of listeners;
2751 void listen_free(rad_listen_t **head)
2755 if (!head || !*head) return;
2759 rad_listen_t *next = this->next;
2762 * Other code may have eaten the FD.
2764 if (this->fd >= 0) close(this->fd);
2766 if (master_listen[this->type].free) {
2767 master_listen[this->type].free(this);
2771 if ((this->type == RAD_LISTEN_AUTH) ||
2773 (this->type == RAD_LISTEN_ACCT) ||
2776 (this->type == RAD_LISTEN_PROXY)
2779 listen_socket_t *sock = this->data;
2780 rad_free(&sock->packet);
2794 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
2799 for (this = mainconfig.listen; this != NULL; this = this->next) {
2800 listen_socket_t *sock;
2802 if ((this->type != RAD_LISTEN_AUTH) &&
2803 (this->type != RAD_LISTEN_ACCT)) continue;
2807 if ((sock->my_port == port) &&
2808 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
2809 return sock->clients;
2817 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
2821 for (this = mainconfig.listen; this != NULL; this = this->next) {
2822 listen_socket_t *sock;
2825 * FIXME: For TCP, ignore the *secondary*
2826 * listeners associated with the main socket.
2828 if ((this->type != RAD_LISTEN_AUTH) &&
2829 (this->type != RAD_LISTEN_ACCT)) continue;
2833 if ((sock->my_port == port) &&
2834 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
2838 if ((sock->my_port == port) &&
2839 ((sock->my_ipaddr.af == AF_INET) &&
2840 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == INADDR_ANY))) {
2844 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2845 if ((sock->my_port == port) &&
2846 (sock->my_ipaddr.af == AF_INET6) &&
2847 (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr))) {