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 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type);
85 * Xlat for %{listen:foo}
87 static size_t xlat_listen(UNUSED void *instance, REQUEST *request,
90 UNUSED RADIUS_ESCAPE_STRING func)
92 const char *value = NULL;
95 if (!fmt || !out || (outlen < 1)) return 0;
97 if (!request || !request->listener) {
102 cp = cf_pair_find(request->listener->cs, fmt);
103 if (!cp || !(value = cf_pair_value(cp))) {
108 strlcpy(out, value, outlen);
114 * Find a per-socket client.
116 RADCLIENT *client_listener_find(rad_listen_t *listener,
117 const fr_ipaddr_t *ipaddr, int src_port)
119 #ifdef WITH_DYNAMIC_CLIENTS
126 RADCLIENT_LIST *clients;
127 listen_socket_t *sock;
129 rad_assert(listener != NULL);
130 rad_assert(ipaddr != NULL);
132 sock = listener->data;
133 clients = sock->clients;
136 * This HAS to have been initialized previously.
138 rad_assert(clients != NULL);
140 client = client_find(clients, ipaddr,sock->proto);
142 char name[256], buffer[128];
144 #ifdef WITH_DYNAMIC_CLIENTS
145 unknown: /* used only for dynamic clients */
149 * DoS attack quenching, but only in daemon mode.
150 * If they're running in debug mode, show them
153 if (debug_flag == 0) {
154 static time_t last_printed = 0;
157 if (last_printed == now) return NULL;
162 listener->print(listener, name, sizeof(name));
164 radlog(L_ERR, "Ignoring request to %s from unknown client %s port %d"
168 , name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
169 buffer, sizeof(buffer)), src_port
171 , (sock->proto == IPPROTO_UDP) ? "udp" : "tcp"
177 #ifndef WITH_DYNAMIC_CLIENTS
178 return client; /* return the found client. */
182 * No server defined, and it's not dynamic. Return it.
184 if (!client->client_server && !client->dynamic) return client;
189 * It's a dynamically generated client, check it.
191 if (client->dynamic && (src_port != 0)) {
193 * Lives forever. Return it.
195 if (client->lifetime == 0) return client;
198 * Rate-limit the deletion of known clients.
199 * This makes them last a little longer, but
200 * prevents the server from melting down if (say)
201 * 10k clients all expire at once.
203 if (now == client->last_new_client) return client;
206 * It's not dead yet. Return it.
208 if ((client->created + client->lifetime) > now) return client;
211 * This really puts them onto a queue for later
214 client_delete(clients, client);
217 * Go find the enclosing network again.
219 client = client_find(clients, ipaddr, sock->proto);
224 if (!client) goto unknown;
225 if (!client->client_server) goto unknown;
228 * At this point, 'client' is the enclosing
229 * network that configures where dynamic clients
232 rad_assert(client->dynamic == 0);
234 } else if (!client->dynamic && client->rate_limit) {
236 * The IP is unknown, so we've found an enclosing
237 * network. Enable DoS protection. We only
238 * allow one new client per second. Known
239 * clients aren't subject to this restriction.
241 if (now == client->last_new_client) goto unknown;
244 client->last_new_client = now;
246 request = request_alloc();
247 if (!request) goto unknown;
249 request->listener = listener;
250 request->client = client;
251 request->packet = rad_recv(listener->fd, 0x02); /* MSG_PEEK */
252 if (!request->packet) { /* badly formed, etc */
253 request_free(&request);
256 request->reply = rad_alloc_reply(request->packet);
257 if (!request->reply) {
258 request_free(&request);
261 request->packet->timestamp = request->timestamp;
263 request->priority = listener->type;
264 request->server = client->client_server;
265 request->root = &mainconfig;
268 * Run a fake request through the given virtual server.
269 * Look for FreeRADIUS-Client-IP-Address
270 * FreeRADIUS-Client-Secret
273 * and create the RADCLIENT structure from that.
275 DEBUG("server %s {", request->server);
277 rcode = module_authorize(0, request);
279 DEBUG("} # server %s", request->server);
281 if (rcode != RLM_MODULE_OK) {
282 request_free(&request);
287 * If the client was updated by rlm_dynamic_clients,
288 * don't create the client from attribute-value pairs.
290 if (request->client == client) {
291 created = client_create(clients, request);
293 created = request->client;
296 * This frees the client if it isn't valid.
298 if (!client_validate(clients, client, created)) goto unknown;
300 request_free(&request);
302 if (!created) goto unknown;
308 static int listen_bind(rad_listen_t *this);
312 * Process and reply to a server-status request.
313 * Like rad_authenticate and rad_accounting this should
314 * live in it's own file but it's so small we don't bother.
316 static int rad_status_server(REQUEST *request)
318 int rcode = RLM_MODULE_OK;
321 switch (request->listener->type) {
323 case RAD_LISTEN_NONE:
325 case RAD_LISTEN_AUTH:
326 dval = dict_valbyname(PW_AUTZ_TYPE, 0, "Status-Server");
328 rcode = module_authorize(dval->value, request);
330 rcode = RLM_MODULE_OK;
335 case RLM_MODULE_UPDATED:
336 request->reply->code = PW_AUTHENTICATION_ACK;
339 case RLM_MODULE_FAIL:
340 case RLM_MODULE_HANDLED:
341 request->reply->code = 0; /* don't reply */
345 case RLM_MODULE_REJECT:
346 request->reply->code = PW_AUTHENTICATION_REJECT;
351 #ifdef WITH_ACCOUNTING
352 case RAD_LISTEN_ACCT:
353 dval = dict_valbyname(PW_ACCT_TYPE, 0, "Status-Server");
355 rcode = module_accounting(dval->value, request);
357 rcode = RLM_MODULE_OK;
362 case RLM_MODULE_UPDATED:
363 request->reply->code = PW_ACCOUNTING_RESPONSE;
367 request->reply->code = 0; /* don't reply */
375 * This is a vendor extension. Suggested by Glen
376 * Zorn in IETF 72, and rejected by the rest of
377 * the WG. We like it, so it goes in here.
380 dval = dict_valbyname(PW_RECV_COA_TYPE, 0, "Status-Server");
382 rcode = module_recv_coa(dval->value, request);
384 rcode = RLM_MODULE_OK;
389 case RLM_MODULE_UPDATED:
390 request->reply->code = PW_COA_ACK;
394 request->reply->code = 0; /* don't reply */
406 * Full statistics are available only on a statistics
409 if (request->listener->type == RAD_LISTEN_NONE) {
410 request_stats_reply(request);
418 static int auth_tcp_recv(rad_listen_t *listener,
419 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
422 RADIUS_PACKET *packet;
423 RAD_REQUEST_FUNP fun = NULL;
424 listen_socket_t *sock = listener->data;
425 RADCLIENT *client = sock->client;
428 * Allocate a packet for partial reads.
431 sock->packet = rad_alloc(0);
432 if (!sock->packet) return 0;
434 sock->packet->sockfd = listener->fd;
435 sock->packet->src_ipaddr = sock->other_ipaddr;
436 sock->packet->src_port = sock->other_port;
437 sock->packet->dst_ipaddr = sock->my_ipaddr;
438 sock->packet->dst_port = sock->my_port;
442 * Grab the packet currently being processed.
444 packet = sock->packet;
446 rcode = fr_tcp_read_packet(packet, 0);
449 * Still only a partial packet. Put it back, and return,
450 * so that we'll read more data when it's ready.
456 if (rcode == -1) { /* error reading packet */
459 radlog(L_ERR, "Invalid packet from %s port %d: closing socket",
460 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
464 if (rcode < 0) { /* error or connection reset */
465 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
468 * Decrement the number of connections.
470 if (sock->parent->num_connections > 0) {
471 sock->parent->num_connections--;
473 if (sock->client->num_connections > 0) {
474 sock->client->num_connections--;
478 * Tell the event handler that an FD has disappeared.
480 DEBUG("Client has closed connection");
481 event_new_fd(listener);
484 * Do NOT free the listener here. It's in use by
485 * a request, and will need to hang around until
486 * all of the requests are done.
488 * It is instead free'd in remove_from_request_hash()
493 RAD_STATS_TYPE_INC(listener, total_requests);
496 * Some sanity checks, based on the packet code.
498 switch(packet->code) {
499 case PW_AUTHENTICATION_REQUEST:
500 RAD_STATS_CLIENT_INC(listener, client, total_requests);
501 fun = rad_authenticate;
504 case PW_STATUS_SERVER:
505 if (!mainconfig.status_server) {
506 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
507 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
508 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
509 rad_free(&sock->packet);
512 fun = rad_status_server;
516 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
517 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
519 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
520 packet->code, client->shortname, packet->src_port);
521 rad_free(&sock->packet);
523 } /* switch over packet types */
525 if (!received_request(listener, packet, prequest, sock->client)) {
526 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
527 RAD_STATS_CLIENT_INC(listener, sock->client, total_packets_dropped);
528 rad_free(&sock->packet);
533 sock->packet = NULL; /* we have no need for more partial reads */
537 static int auth_tcp_accept(rad_listen_t *listener,
538 UNUSED RAD_REQUEST_FUNP *pfun,
539 UNUSED REQUEST **prequest)
544 struct sockaddr_storage src;
545 listen_socket_t *sock;
546 fr_ipaddr_t src_ipaddr;
551 DEBUG2(" ... new connection request on TCP socket.");
553 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
556 * Non-blocking sockets must handle this.
558 if (errno == EWOULDBLOCK) {
562 DEBUG2(" ... failed to accept connection.");
566 if (!fr_sockaddr2ipaddr(&src, salen, &src_ipaddr, &src_port)) {
567 DEBUG2(" ... unknown address family.");
572 * Enforce client IP address checks on accept, not on
575 if ((client = client_listener_find(listener,
576 &src_ipaddr, src_port)) == NULL) {
578 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
583 * Enforce max_connectionsx on client && listen section.
585 if ((client->max_connections != 0) &&
586 (client->max_connections == client->num_connections)) {
588 * FIXME: Print client IP/port, and server IP/port.
590 radlog(L_INFO, "Ignoring new connection due to client max_connections (%d)", client->max_connections);
595 sock = listener->data;
596 if ((sock->max_connections != 0) &&
597 (sock->max_connections == sock->num_connections)) {
599 * FIXME: Print client IP/port, and server IP/port.
601 radlog(L_INFO, "Ignoring new connection due to socket max_connections");
605 client->num_connections++;
606 sock->num_connections++;
609 * Add the new listener.
611 this = listen_alloc(listener->type);
612 if (!this) return -1;
615 * Copy everything, including the pointer to the socket
619 memcpy(this->data, listener->data, sizeof(*sock));
620 memcpy(this, listener, sizeof(*this));
622 this->data = sock; /* fix it back */
624 sock->parent = listener->data;
625 sock->other_ipaddr = src_ipaddr;
626 sock->other_port = src_port;
627 sock->client = client;
630 this->status = RAD_LISTEN_STATUS_INIT;
631 this->recv = auth_tcp_recv;
634 * FIXME: set O_NONBLOCK on the accept'd fd.
635 * See djb's portability rants for details.
639 * Tell the event loop that we have a new FD.
640 * This can be called from a child thread...
650 * This function is stupid and complicated.
652 static int socket_print(const rad_listen_t *this, char *buffer, size_t bufsize)
655 listen_socket_t *sock = this->data;
658 switch (this->type) {
660 case RAD_LISTEN_NONE: /* what a hack... */
665 case RAD_LISTEN_AUTH:
666 name = "authentication";
669 #ifdef WITH_ACCOUNTING
670 case RAD_LISTEN_ACCT:
676 case RAD_LISTEN_PROXY:
688 case RAD_LISTEN_DHCP:
704 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
705 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
709 if (sock->interface) {
710 ADDSTRING(" interface ");
711 ADDSTRING(sock->interface);
715 if (this->recv == auth_tcp_accept) {
716 ADDSTRING(" proto tcp");
722 * TCP sockets get printed a little differently, to make
723 * it clear what's going on.
726 ADDSTRING(" from client (");
727 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
731 snprintf(buffer, bufsize, "%d", sock->other_port);
735 if ((sock->my_ipaddr.af == AF_INET) &&
736 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
737 strlcpy(buffer, "*", bufsize);
739 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
744 snprintf(buffer, bufsize, "%d", sock->my_port);
748 ADDSTRING(", virtual-server=");
749 ADDSTRING(this->server);
759 * Maybe it's a socket that we opened to a home server.
761 if ((sock->proto == IPPROTO_TCP) &&
762 (this->type == RAD_LISTEN_PROXY)) {
764 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
768 snprintf(buffer, bufsize, "%d", sock->my_port);
770 ADDSTRING(") -> home_server (");
772 if ((sock->other_ipaddr.af == AF_INET) &&
773 (sock->other_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
774 strlcpy(buffer, "*", bufsize);
776 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
781 snprintf(buffer, bufsize, "%d", sock->other_port);
788 #endif /* WITH_PROXY */
789 #endif /* WITH_TCP */
791 ADDSTRING(" address ");
793 if ((sock->my_ipaddr.af == AF_INET) &&
794 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
795 strlcpy(buffer, "*", bufsize);
797 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
802 snprintf(buffer, bufsize, "%d", sock->my_port);
806 ADDSTRING(" as server ");
807 ADDSTRING(this->server);
816 extern int check_config; /* radiusd.c */
820 * Parse an authentication or accounting socket.
822 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
827 listen_socket_t *sock = this->data;
828 char *section_name = NULL;
829 CONF_SECTION *client_cs, *parentcs;
836 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
837 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
838 &ipaddr.ipaddr.ip4addr, NULL);
839 if (rcode < 0) return -1;
841 if (rcode == 0) { /* successfully parsed IPv4 */
844 } else { /* maybe IPv6? */
845 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
846 &ipaddr.ipaddr.ip6addr, NULL);
847 if (rcode < 0) return -1;
850 cf_log_err(cf_sectiontoitem(cs),
851 "No address specified in listen section");
854 ipaddr.af = AF_INET6;
857 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
859 if (rcode < 0) return -1;
861 if ((listen_port < 0) || (listen_port > 65535)) {
862 cf_log_err(cf_sectiontoitem(cs),
863 "Invalid value for \"port\"");
867 sock->proto = IPPROTO_UDP;
869 if (cf_pair_find(cs, "proto")) {
871 cf_log_err(cf_sectiontoitem(cs),
872 "System does not support the TCP protocol. Delete this line from the configuration file.");
878 rcode = cf_item_parse(cs, "proto", PW_TYPE_STRING_PTR,
880 if (rcode < 0) return -1;
882 if (strcmp(proto, "udp") == 0) {
883 sock->proto = IPPROTO_UDP;
885 } else if (strcmp(proto, "tcp") == 0) {
886 sock->proto = IPPROTO_TCP;
888 rcode = cf_item_parse(cs, "max_connections", PW_TYPE_INTEGER,
889 &sock->max_connections, "64");
890 if (rcode < 0) return -1;
893 cf_log_err(cf_sectiontoitem(cs),
894 "Unknown proto name \"%s\"", proto);
901 * TCP requires a destination IP for sockets.
902 * UDP doesn't, so it's allowed.
905 if ((this->type == RAD_LISTEN_PROXY) &&
906 (sock->proto != IPPROTO_UDP)) {
907 cf_log_err(cf_sectiontoitem(cs),
908 "Proxy listeners can only listen on proto = udp");
911 #endif /* WITH_PROXY */
912 #endif /* WITH_TCP */
915 sock->my_ipaddr = ipaddr;
916 sock->my_port = listen_port;
920 if (home_server_find(&sock->my_ipaddr, sock->my_port, sock->proto)) {
923 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.",
924 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
929 return 0; /* don't do anything */
934 * If we can bind to interfaces, do so,
937 if (cf_pair_find(cs, "interface")) {
939 CONF_PAIR *cp = cf_pair_find(cs, "interface");
941 rad_assert(cp != NULL);
942 value = cf_pair_value(cp);
944 cf_log_err(cf_sectiontoitem(cs),
945 "No interface name given");
948 sock->interface = value;
953 * If we can do broadcasts..
955 if (cf_pair_find(cs, "broadcast")) {
957 cf_log_err(cf_sectiontoitem(cs),
958 "System does not support broadcast sockets. Delete this line from the configuration file.");
962 CONF_PAIR *cp = cf_pair_find(cs, "broadcast");
964 if (this->type != RAD_LISTEN_DHCP) {
965 cf_log_err(cf_pairtoitem(cp),
966 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file.");
970 rad_assert(cp != NULL);
971 value = cf_pair_value(cp);
973 cf_log_err(cf_sectiontoitem(cs),
974 "No broadcast value given");
979 * Hack... whatever happened to cf_section_parse?
981 sock->broadcast = (strcmp(value, "yes") == 0);
987 * And bind it to the port.
989 if (listen_bind(this) < 0) {
991 cf_log_err(cf_sectiontoitem(cs),
992 "Error binding to port for %s port %d",
993 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
1000 * Proxy sockets don't have clients.
1002 if (this->type == RAD_LISTEN_PROXY) return 0;
1006 * The more specific configurations are preferred to more
1010 parentcs = cf_top_section(cs);
1011 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
1012 §ion_name, NULL);
1013 if (rcode < 0) return -1; /* bad string */
1016 * Explicit list given: use it.
1018 client_cs = cf_section_sub_find_name2(parentcs,
1022 client_cs = cf_section_find(section_name);
1025 cf_log_err(cf_sectiontoitem(cs),
1026 "Failed to find clients %s {...}",
1032 } /* else there was no "clients = " entry. */
1035 CONF_SECTION *server_cs;
1037 server_cs = cf_section_sub_find_name2(parentcs,
1041 * Found a "server foo" section. If there are clients
1045 (cf_section_sub_find(server_cs, "client") != NULL)) {
1046 client_cs = server_cs;
1051 * Still nothing. Look for global clients.
1053 if (!client_cs) client_cs = parentcs;
1055 sock->clients = clients_parse_section(client_cs);
1056 if (!sock->clients) {
1057 cf_log_err(cf_sectiontoitem(cs),
1058 "Failed to load clients for this listen section");
1063 if (sock->proto == IPPROTO_TCP) {
1065 * Re-write the listener receive function to
1066 * allow us to accept the socket.
1068 this->recv = auth_tcp_accept;
1076 * Send an authentication response packet
1078 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1080 rad_assert(request->listener == listener);
1081 rad_assert(listener->send == auth_socket_send);
1083 if (rad_send(request->reply, request->packet,
1084 request->client->secret) < 0) {
1085 radlog_request(L_ERR, 0, request, "Failed sending reply: %s",
1094 #ifdef WITH_ACCOUNTING
1096 * Send an accounting response packet (or not)
1098 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1100 rad_assert(request->listener == listener);
1101 rad_assert(listener->send == acct_socket_send);
1104 * Accounting reject's are silently dropped.
1106 * We do it here to avoid polluting the rest of the
1107 * code with this knowledge
1109 if (request->reply->code == 0) return 0;
1111 if (rad_send(request->reply, request->packet,
1112 request->client->secret) < 0) {
1113 radlog_request(L_ERR, 0, request, "Failed sending reply: %s",
1124 * Send a packet to a home server.
1126 * FIXME: have different code for proxy auth & acct!
1128 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1130 rad_assert(request->proxy_listener == listener);
1131 rad_assert(listener->send == proxy_socket_send);
1133 if (rad_send(request->proxy, request->packet,
1134 request->home_server->secret) < 0) {
1135 radlog_request(L_ERR, 0, request, "Failed sending proxied request: %s",
1146 * Check if an incoming request is "ok"
1148 * It takes packets, not requests. It sees if the packet looks
1149 * OK. If so, it does a number of sanity checks on it.
1151 static int stats_socket_recv(rad_listen_t *listener,
1152 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1156 RADIUS_PACKET *packet;
1158 fr_ipaddr_t src_ipaddr;
1160 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1161 if (rcode < 0) return 0;
1163 RAD_STATS_TYPE_INC(listener, total_requests);
1165 if (rcode < 20) { /* AUTH_HDR_LEN */
1166 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1170 if ((client = client_listener_find(listener,
1171 &src_ipaddr, src_port)) == NULL) {
1172 rad_recv_discard(listener->fd);
1173 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1178 * We only understand Status-Server on this socket.
1180 if (code != PW_STATUS_SERVER) {
1181 DEBUG("Ignoring packet code %d sent to Status-Server port",
1183 rad_recv_discard(listener->fd);
1184 RAD_STATS_TYPE_INC(listener, total_unknown_types);
1185 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1190 * Now that we've sanity checked everything, receive the
1193 packet = rad_recv(listener->fd, 1); /* require message authenticator */
1195 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1196 DEBUG("%s", fr_strerror());
1200 if (!received_request(listener, packet, prequest, client)) {
1201 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1202 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1207 *pfun = rad_status_server;
1214 * Check if an incoming request is "ok"
1216 * It takes packets, not requests. It sees if the packet looks
1217 * OK. If so, it does a number of sanity checks on it.
1219 static int auth_socket_recv(rad_listen_t *listener,
1220 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1224 RADIUS_PACKET *packet;
1225 RAD_REQUEST_FUNP fun = NULL;
1227 fr_ipaddr_t src_ipaddr;
1229 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1230 if (rcode < 0) return 0;
1232 RAD_STATS_TYPE_INC(listener, total_requests);
1234 if (rcode < 20) { /* AUTH_HDR_LEN */
1235 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1239 if ((client = client_listener_find(listener,
1240 &src_ipaddr, src_port)) == NULL) {
1241 rad_recv_discard(listener->fd);
1242 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1247 * Some sanity checks, based on the packet code.
1250 case PW_AUTHENTICATION_REQUEST:
1251 RAD_STATS_CLIENT_INC(listener, client, total_requests);
1252 fun = rad_authenticate;
1255 case PW_STATUS_SERVER:
1256 if (!mainconfig.status_server) {
1257 rad_recv_discard(listener->fd);
1258 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1259 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1260 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1263 fun = rad_status_server;
1267 rad_recv_discard(listener->fd);
1268 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
1269 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1271 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
1272 code, client->shortname, src_port);
1275 } /* switch over packet types */
1278 * Now that we've sanity checked everything, receive the
1281 packet = rad_recv(listener->fd, client->message_authenticator);
1283 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1284 DEBUG("%s", fr_strerror());
1288 if (!received_request(listener, packet, prequest, client)) {
1289 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1290 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1300 #ifdef WITH_ACCOUNTING
1302 * Receive packets from an accounting socket
1304 static int acct_socket_recv(rad_listen_t *listener,
1305 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1309 RADIUS_PACKET *packet;
1310 RAD_REQUEST_FUNP fun = NULL;
1312 fr_ipaddr_t src_ipaddr;
1314 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1315 if (rcode < 0) return 0;
1317 RAD_STATS_TYPE_INC(listener, total_requests);
1319 if (rcode < 20) { /* AUTH_HDR_LEN */
1320 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1324 if ((client = client_listener_find(listener,
1325 &src_ipaddr, src_port)) == NULL) {
1326 rad_recv_discard(listener->fd);
1327 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1332 * Some sanity checks, based on the packet code.
1335 case PW_ACCOUNTING_REQUEST:
1336 RAD_STATS_CLIENT_INC(listener, client, total_requests);
1337 fun = rad_accounting;
1340 case PW_STATUS_SERVER:
1341 if (!mainconfig.status_server) {
1342 rad_recv_discard(listener->fd);
1343 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1344 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1346 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1349 fun = rad_status_server;
1353 rad_recv_discard(listener->fd);
1354 RAD_STATS_TYPE_INC(listener, total_unknown_types);
1355 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1357 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1358 code, client->shortname, src_port);
1360 } /* switch over packet types */
1363 * Now that we've sanity checked everything, receive the
1366 packet = rad_recv(listener->fd, 0);
1368 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1369 radlog(L_ERR, "%s", fr_strerror());
1374 * There can be no duplicate accounting packets.
1376 if (!received_request(listener, packet, prequest, client)) {
1377 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1378 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1391 * For now, all CoA requests are *only* originated, and not
1392 * proxied. So all of the necessary work is done in the
1393 * post-proxy section, which is automatically handled by event.c.
1394 * As a result, we don't have to do anything here.
1396 static int rad_coa_reply(REQUEST *request)
1398 VALUE_PAIR *s1, *s2;
1401 * Inform the user about RFC requirements.
1403 s1 = pairfind(request->proxy->vps, PW_STATE, 0);
1405 s2 = pairfind(request->proxy_reply->vps, PW_STATE, 0);
1408 DEBUG("WARNING: Client was sent State in CoA, and did not respond with State.");
1410 } else if ((s1->length != s2->length) ||
1411 (memcmp(s1->vp_octets, s2->vp_octets,
1412 s1->length) != 0)) {
1413 DEBUG("WARNING: Client was sent State in CoA, and did not respond with the same State.");
1417 return RLM_MODULE_OK;
1420 static int do_proxy(REQUEST *request)
1424 if (request->in_proxy_hash ||
1425 (request->proxy_reply && (request->proxy_reply->code != 0))) {
1429 vp = pairfind(request->config_items, PW_HOME_SERVER_POOL);
1432 if (!home_pool_byname(vp->vp_strvalue, HOME_TYPE_COA)) {
1433 RDEBUG2("ERROR: Cannot proxy to unknown pool %s",
1442 * Receive a CoA packet.
1444 static int rad_coa_recv(REQUEST *request)
1446 int rcode = RLM_MODULE_OK;
1451 * Get the correct response
1453 switch (request->packet->code) {
1454 case PW_COA_REQUEST:
1459 case PW_DISCONNECT_REQUEST:
1460 ack = PW_DISCONNECT_ACK;
1461 nak = PW_DISCONNECT_NAK;
1464 default: /* shouldn't happen */
1465 return RLM_MODULE_FAIL;
1469 #define WAS_PROXIED (request->proxy)
1471 #define WAS_PROXIED (0)
1476 * RFC 5176 Section 3.3. If we have a CoA-Request
1477 * with Service-Type = Authorize-Only, it MUST
1478 * have a State attribute in it.
1480 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE, 0);
1481 if (request->packet->code == PW_COA_REQUEST) {
1482 if (vp && (vp->vp_integer == 17)) {
1483 vp = pairfind(request->packet->vps, PW_STATE, 0);
1484 if (!vp || (vp->length == 0)) {
1485 RDEBUG("ERROR: CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1486 request->reply->code = PW_COA_NAK;
1487 return RLM_MODULE_FAIL;
1492 * RFC 5176, Section 3.2.
1494 RDEBUG("ERROR: Disconnect-Request MUST NOT contain a Service-Type attribute");
1495 request->reply->code = PW_DISCONNECT_NAK;
1496 return RLM_MODULE_FAIL;
1499 rcode = module_recv_coa(0, request);
1501 case RLM_MODULE_FAIL:
1502 case RLM_MODULE_INVALID:
1503 case RLM_MODULE_REJECT:
1504 case RLM_MODULE_USERLOCK:
1506 request->reply->code = nak;
1509 case RLM_MODULE_HANDLED:
1512 case RLM_MODULE_NOOP:
1513 case RLM_MODULE_NOTFOUND:
1515 case RLM_MODULE_UPDATED:
1516 if (do_proxy(request)) return RLM_MODULE_OK;
1517 request->reply->code = ack;
1522 * Start the reply code with the proxy reply
1525 request->reply->code = request->proxy_reply->code;
1529 * Copy State from the request to the reply.
1530 * See RFC 5176 Section 3.3.
1532 vp = paircopy2(request->packet->vps, PW_STATE, 0);
1533 if (vp) pairadd(&request->reply->vps, vp);
1536 * We may want to over-ride the reply.
1538 rcode = module_send_coa(0, request);
1541 * We need to send CoA-NAK back if Service-Type
1542 * is Authorize-Only. Rely on the user's policy
1543 * to do that. We're not a real NAS, so this
1544 * restriction doesn't (ahem) apply to us.
1546 case RLM_MODULE_FAIL:
1547 case RLM_MODULE_INVALID:
1548 case RLM_MODULE_REJECT:
1549 case RLM_MODULE_USERLOCK:
1552 * Over-ride an ACK with a NAK
1554 request->reply->code = nak;
1557 case RLM_MODULE_HANDLED:
1560 case RLM_MODULE_NOOP:
1561 case RLM_MODULE_NOTFOUND:
1563 case RLM_MODULE_UPDATED:
1565 * Do NOT over-ride a previously set value.
1566 * Otherwise an "ok" here will re-write a
1569 if (request->reply->code == 0) {
1570 request->reply->code = ack;
1576 return RLM_MODULE_OK;
1581 * Check if an incoming request is "ok"
1583 * It takes packets, not requests. It sees if the packet looks
1584 * OK. If so, it does a number of sanity checks on it.
1586 static int coa_socket_recv(rad_listen_t *listener,
1587 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1591 RADIUS_PACKET *packet;
1592 RAD_REQUEST_FUNP fun = NULL;
1594 fr_ipaddr_t src_ipaddr;
1596 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1597 if (rcode < 0) return 0;
1599 RAD_STATS_TYPE_INC(listener, total_requests);
1601 if (rcode < 20) { /* AUTH_HDR_LEN */
1602 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1606 if ((client = client_listener_find(listener,
1607 &src_ipaddr, src_port)) == NULL) {
1608 rad_recv_discard(listener->fd);
1609 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1614 * Some sanity checks, based on the packet code.
1617 case PW_COA_REQUEST:
1618 case PW_DISCONNECT_REQUEST:
1623 rad_recv_discard(listener->fd);
1624 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1625 code, client->shortname, src_port);
1628 } /* switch over packet types */
1631 * Now that we've sanity checked everything, receive the
1634 packet = rad_recv(listener->fd, client->message_authenticator);
1636 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1637 DEBUG("%s", fr_strerror());
1641 if (!received_request(listener, packet, prequest, client)) {
1653 * Recieve packets from a proxy socket.
1655 static int proxy_socket_recv(rad_listen_t *listener,
1656 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1659 RADIUS_PACKET *packet;
1660 RAD_REQUEST_FUNP fun = NULL;
1663 packet = rad_recv(listener->fd, 0);
1665 radlog(L_ERR, "%s", fr_strerror());
1670 * FIXME: Client MIB updates?
1672 switch(packet->code) {
1673 case PW_AUTHENTICATION_ACK:
1674 case PW_ACCESS_CHALLENGE:
1675 case PW_AUTHENTICATION_REJECT:
1676 fun = rad_authenticate;
1679 #ifdef WITH_ACCOUNTING
1680 case PW_ACCOUNTING_RESPONSE:
1681 fun = rad_accounting;
1686 case PW_DISCONNECT_ACK:
1687 case PW_DISCONNECT_NAK:
1690 fun = rad_coa_reply;
1696 * FIXME: Update MIB for packet types?
1698 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1699 "from home server %s port %d - ID %d : IGNORED",
1701 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1702 packet->src_port, packet->id);
1707 request = received_proxy_response(packet);
1715 * Distinguish proxied CoA requests from ones we
1718 if ((fun == rad_coa_reply) &&
1719 (request->packet->code == request->proxy->code)) {
1724 rad_assert(fun != NULL);
1726 *prequest = request;
1733 * Recieve packets from a proxy socket.
1735 static int proxy_socket_tcp_recv(rad_listen_t *listener,
1736 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1739 RADIUS_PACKET *packet;
1740 RAD_REQUEST_FUNP fun = NULL;
1741 listen_socket_t *sock = listener->data;
1744 packet = fr_tcp_recv(listener->fd, 0);
1746 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
1747 event_new_fd(listener);
1752 * FIXME: Client MIB updates?
1754 switch(packet->code) {
1755 case PW_AUTHENTICATION_ACK:
1756 case PW_ACCESS_CHALLENGE:
1757 case PW_AUTHENTICATION_REJECT:
1758 fun = rad_authenticate;
1761 #ifdef WITH_ACCOUNTING
1762 case PW_ACCOUNTING_RESPONSE:
1763 fun = rad_accounting;
1769 * FIXME: Update MIB for packet types?
1771 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1772 "from home server %s port %d - ID %d : IGNORED",
1774 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1775 packet->src_port, packet->id);
1780 packet->src_ipaddr = sock->other_ipaddr;
1781 packet->src_port = sock->other_port;
1782 packet->dst_ipaddr = sock->my_ipaddr;
1783 packet->dst_port = sock->my_port;
1786 * FIXME: Have it return an indication of packets that
1787 * are OK to ignore (dups, too late), versus ones that
1788 * aren't OK to ignore (unknown response, spoofed, etc.)
1790 * Close the socket on bad packets...
1792 request = received_proxy_response(packet);
1797 rad_assert(fun != NULL);
1798 sock->opened = sock->last_packet = request->timestamp;
1801 *prequest = request;
1809 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1811 if (!request->reply->code) return 0;
1813 if (rad_encode(request->reply, request->packet,
1814 request->client->secret) < 0) {
1815 radlog_request(L_ERR, 0, request, "Failed encoding packet: %s",
1820 if (rad_sign(request->reply, request->packet,
1821 request->client->secret) < 0) {
1822 radlog_request(L_ERR, 0, request, "Failed signing packet: %s",
1831 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1833 if (rad_verify(request->packet, NULL,
1834 request->client->secret) < 0) {
1838 return rad_decode(request->packet, NULL,
1839 request->client->secret);
1843 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1845 if (rad_encode(request->proxy, NULL, request->home_server->secret) < 0) {
1846 radlog_request(L_ERR, 0, request, "Failed encoding proxied packet: %s",
1851 if (rad_sign(request->proxy, NULL, request->home_server->secret) < 0) {
1852 radlog_request(L_ERR, 0, request, "Failed signing proxied packet: %s",
1861 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1864 * rad_verify is run in event.c, received_proxy_response()
1867 return rad_decode(request->proxy_reply, request->proxy,
1868 request->home_server->secret);
1874 #include "command.c"
1876 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1878 { common_socket_parse, NULL,
1879 stats_socket_recv, auth_socket_send,
1880 socket_print, client_socket_encode, client_socket_decode },
1883 * This always gets defined.
1885 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1890 { common_socket_parse, NULL,
1891 proxy_socket_recv, proxy_socket_send,
1892 socket_print, proxy_socket_encode, proxy_socket_decode },
1895 /* authentication */
1896 { common_socket_parse, NULL,
1897 auth_socket_recv, auth_socket_send,
1898 socket_print, client_socket_encode, client_socket_decode },
1900 #ifdef WITH_ACCOUNTING
1902 { common_socket_parse, NULL,
1903 acct_socket_recv, acct_socket_send,
1904 socket_print, client_socket_encode, client_socket_decode},
1909 { detail_parse, detail_free,
1910 detail_recv, detail_send,
1911 detail_print, detail_encode, detail_decode },
1915 /* vlan query protocol */
1916 { common_socket_parse, NULL,
1917 vqp_socket_recv, vqp_socket_send,
1918 socket_print, vqp_socket_encode, vqp_socket_decode },
1922 /* dhcp query protocol */
1923 { dhcp_socket_parse, NULL,
1924 dhcp_socket_recv, dhcp_socket_send,
1925 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1928 #ifdef WITH_COMMAND_SOCKET
1929 /* TCP command socket */
1930 { command_socket_parse, command_socket_free,
1931 command_domain_accept, command_domain_send,
1932 command_socket_print, command_socket_encode, command_socket_decode },
1936 /* Change of Authorization */
1937 { common_socket_parse, NULL,
1938 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
1939 socket_print, client_socket_encode, client_socket_decode },
1947 * Binds a listener to a socket.
1949 static int listen_bind(rad_listen_t *this)
1952 struct sockaddr_storage salocal;
1954 listen_socket_t *sock = this->data;
1956 #define proto_for_port "udp"
1957 #define sock_type SOCK_DGRAM
1959 const char *proto_for_port = "udp";
1960 int sock_type = SOCK_DGRAM;
1962 if (sock->proto == IPPROTO_TCP) {
1964 if (this->type == RAD_LISTEN_VQP) {
1965 radlog(L_ERR, "VQP does not support TCP transport");
1970 proto_for_port = "tcp";
1971 sock_type = SOCK_STREAM;
1976 * If the port is zero, then it means the appropriate
1977 * thing from /etc/services.
1979 if (sock->my_port == 0) {
1980 struct servent *svp;
1982 switch (this->type) {
1983 case RAD_LISTEN_AUTH:
1984 svp = getservbyname ("radius", proto_for_port);
1986 sock->my_port = ntohs(svp->s_port);
1988 sock->my_port = PW_AUTH_UDP_PORT;
1992 #ifdef WITH_ACCOUNTING
1993 case RAD_LISTEN_ACCT:
1994 svp = getservbyname ("radacct", proto_for_port);
1996 sock->my_port = ntohs(svp->s_port);
1998 sock->my_port = PW_ACCT_UDP_PORT;
2004 case RAD_LISTEN_PROXY:
2005 /* leave it at zero */
2010 case RAD_LISTEN_VQP:
2011 sock->my_port = 1589;
2016 case RAD_LISTEN_COA:
2017 svp = getservbyname ("radius-dynauth", "udp");
2019 sock->my_port = ntohs(svp->s_port);
2021 sock->my_port = PW_COA_UDP_PORT;
2027 DEBUG("WARNING: Internal sanity check failed in binding to socket. Ignoring problem.");
2033 * Copy fr_socket() here, as we may need to bind to a device.
2035 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
2039 this->print(this, buffer, sizeof(buffer));
2041 radlog(L_ERR, "Failed opening %s: %s", buffer, strerror(errno));
2046 * Bind to a device BEFORE touching IP addresses.
2048 if (sock->interface) {
2049 #ifdef SO_BINDTODEVICE
2051 strcpy(ifreq.ifr_name, sock->interface);
2054 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
2055 (char *)&ifreq, sizeof(ifreq));
2059 radlog(L_ERR, "Failed binding to interface %s: %s",
2060 sock->interface, strerror(errno));
2062 } /* else it worked. */
2064 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2065 #ifdef HAVE_NET_IF_H
2067 * Odds are that any system supporting "bind to
2068 * device" also supports IPv6, so this next bit
2069 * isn't necessary. But it's here for
2072 * If we're doing IPv6, and the scope hasn't yet
2073 * been defined, set the scope to the scope of
2076 if (sock->my_ipaddr.af == AF_INET6) {
2077 if (sock->my_ipaddr.scope == 0) {
2078 sock->my_ipaddr.scope = if_nametoindex(sock->interface);
2079 if (sock->my_ipaddr.scope == 0) {
2081 radlog(L_ERR, "Failed finding interface %s: %s",
2082 sock->interface, strerror(errno));
2085 } /* else scope was defined: we're OK. */
2090 * IPv4: no link local addresses,
2091 * and no bind to device.
2095 radlog(L_ERR, "Failed binding to interface %s: \"bind to device\" is unsupported", sock->interface);
2102 if (sock->proto == IPPROTO_TCP) {
2105 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2107 radlog(L_ERR, "Failed to reuse address: %s", strerror(errno));
2113 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
2114 else /* UDP sockets get UDPfromto */
2117 #ifdef WITH_UDPFROMTO
2119 * Initialize udpfromto for all sockets.
2121 if (udpfromto_init(this->fd) != 0) {
2122 radlog(L_ERR, "Failed initializing udpfromto: %s",
2130 * Set up sockaddr stuff.
2132 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
2137 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2138 if (sock->my_ipaddr.af == AF_INET6) {
2140 * Listening on '::' does NOT get you IPv4 to
2141 * IPv6 mapping. You've got to listen on an IPv4
2142 * address, too. This makes the rest of the server
2143 * design a little simpler.
2147 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2150 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2151 (char *)&on, sizeof(on));
2153 #endif /* IPV6_V6ONLY */
2155 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2157 if (sock->my_ipaddr.af == AF_INET) {
2160 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2162 * Disable PMTU discovery. On Linux, this
2163 * also makes sure that the "don't fragment"
2166 flag = IP_PMTUDISC_DONT;
2167 setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2168 &flag, sizeof(flag));
2171 #if defined(IP_DONTFRAG)
2173 * Ensure that the "don't fragment" flag is zero.
2176 setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2177 &flag, sizeof(flag));
2183 if (sock->broadcast) {
2186 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2187 radlog(L_ERR, "Can't set broadcast option: %s\n",
2196 * May be binding to priviledged ports.
2198 if (sock->my_port != 0) {
2202 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2203 radlog(L_ERR, "Can't set re-use address option: %s\n",
2210 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2216 this->print(this, buffer, sizeof(buffer));
2217 radlog(L_ERR, "Failed binding to %s: %s\n",
2218 buffer, strerror(errno));
2223 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2224 * kernel instead binds us to a 1.2.3.4. If this
2225 * happens, notice, and remember our real IP.
2228 struct sockaddr_storage src;
2229 socklen_t sizeof_src = sizeof(src);
2231 memset(&src, 0, sizeof_src);
2232 if (getsockname(this->fd, (struct sockaddr *) &src,
2234 radlog(L_ERR, "Failed getting socket name: %s",
2239 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2240 &sock->my_ipaddr, &sock->my_port)) {
2241 radlog(L_ERR, "Socket has unsupported address family");
2248 if (sock->proto == IPPROTO_TCP) {
2249 if (listen(this->fd, 8) < 0) {
2251 radlog(L_ERR, "Failed in listen(): %s", strerror(errno));
2257 if (fr_nonblock(this->fd) < 0) {
2259 radlog(L_ERR, "Failed setting non-blocking on socket: %s",
2265 * Mostly for proxy sockets.
2267 sock->other_ipaddr.af = sock->my_ipaddr.af;
2270 * Don't screw up other people.
2272 #undef proto_for_port
2280 * Allocate & initialize a new listener.
2282 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
2286 this = rad_malloc(sizeof(*this));
2287 memset(this, 0, sizeof(*this));
2290 this->recv = master_listen[this->type].recv;
2291 this->send = master_listen[this->type].send;
2292 this->print = master_listen[this->type].print;
2293 this->encode = master_listen[this->type].encode;
2294 this->decode = master_listen[this->type].decode;
2298 case RAD_LISTEN_NONE:
2300 case RAD_LISTEN_AUTH:
2301 #ifdef WITH_ACCOUNTING
2302 case RAD_LISTEN_ACCT:
2305 case RAD_LISTEN_PROXY:
2308 case RAD_LISTEN_VQP:
2311 case RAD_LISTEN_COA:
2313 this->data = rad_malloc(sizeof(listen_socket_t));
2314 memset(this->data, 0, sizeof(listen_socket_t));
2318 case RAD_LISTEN_DHCP:
2319 this->data = rad_malloc(sizeof(dhcp_socket_t));
2320 memset(this->data, 0, sizeof(dhcp_socket_t));
2325 case RAD_LISTEN_DETAIL:
2330 #ifdef WITH_COMMAND_SOCKET
2331 case RAD_LISTEN_COMMAND:
2332 this->data = rad_malloc(sizeof(fr_command_socket_t));
2333 memset(this->data, 0, sizeof(fr_command_socket_t));
2338 rad_assert("Unsupported option!" == NULL);
2347 * Externally visible function for creating a new proxy LISTENER.
2349 * Not thread-safe, but all calls to it are protected by the
2350 * proxy mutex in event.c
2352 int proxy_new_listener(home_server *home, int src_port)
2355 listen_socket_t *sock;
2357 if (!home) return 0;
2359 if ((home->max_connections > 0) &&
2360 (home->num_connections >= home->max_connections)) {
2361 DEBUG("WARNING: Home server has too many open connections (%d)",
2362 home->max_connections);
2366 this = listen_alloc(RAD_LISTEN_PROXY);
2369 sock->other_ipaddr = home->ipaddr;
2370 sock->other_port = home->port;
2373 sock->my_ipaddr = home->src_ipaddr;
2374 sock->my_port = src_port;
2375 sock->proto = home->proto;
2378 sock->last_packet = time(NULL);
2380 if (home->proto == IPPROTO_TCP) {
2381 this->recv = proxy_socket_tcp_recv;
2384 * FIXME: connect() is blocking!
2385 * We do this with the proxy mutex locked, which may
2386 * cause large delays!
2388 * http://www.developerweb.net/forum/showthread.php?p=13486
2390 this->fd = fr_tcp_client_socket(&home->src_ipaddr,
2391 &home->ipaddr, home->port);
2394 this->fd = fr_socket(&home->src_ipaddr, src_port);
2397 DEBUG("Failed opening client socket: %s", fr_strerror());
2403 * Figure out which port we were bound to.
2405 if (sock->my_port == 0) {
2406 struct sockaddr_storage src;
2407 socklen_t sizeof_src = sizeof(src);
2409 memset(&src, 0, sizeof_src);
2410 if (getsockname(this->fd, (struct sockaddr *) &src,
2412 radlog(L_ERR, "Failed getting socket name: %s",
2418 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2419 &sock->my_ipaddr, &sock->my_port)) {
2420 radlog(L_ERR, "Socket has unsupported address family");
2427 * Tell the event loop that we have a new FD
2429 if (!event_new_fd(this)) {
2439 static const FR_NAME_NUMBER listen_compare[] = {
2441 { "status", RAD_LISTEN_NONE },
2443 { "auth", RAD_LISTEN_AUTH },
2444 #ifdef WITH_ACCOUNTING
2445 { "acct", RAD_LISTEN_ACCT },
2448 { "detail", RAD_LISTEN_DETAIL },
2451 { "proxy", RAD_LISTEN_PROXY },
2454 { "vmps", RAD_LISTEN_VQP },
2457 { "dhcp", RAD_LISTEN_DHCP },
2459 #ifdef WITH_COMMAND_SOCKET
2460 { "control", RAD_LISTEN_COMMAND },
2463 { "coa", RAD_LISTEN_COA },
2469 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
2477 cf_log_info(cs, "listen {");
2479 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
2481 if (rcode < 0) return NULL;
2484 cf_log_err(cf_sectiontoitem(cs),
2485 "No type specified in listen section");
2489 type = fr_str2int(listen_compare, listen_type, -1);
2491 cf_log_err(cf_sectiontoitem(cs),
2492 "Invalid type \"%s\" in listen section.",
2500 * Allow listen sections in the default config to
2501 * refer to a server.
2504 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
2506 if (rcode == 1) { /* compatiblity with 2.0-pre */
2507 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
2510 if (rcode < 0) return NULL;
2515 * We were passed a virtual server, so the caller is
2516 * defining a proxy listener inside of a virtual server.
2517 * This isn't allowed right now.
2519 else if (type == RAD_LISTEN_PROXY) {
2520 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
2526 * Set up cross-type data.
2528 this = listen_alloc(type);
2529 this->server = server;
2533 * Call per-type parser.
2535 if (master_listen[type].parse(cs, this) < 0) {
2540 cf_log_info(cs, "}");
2546 static int is_loopback(const fr_ipaddr_t *ipaddr)
2549 * We shouldn't proxy on loopback.
2551 if ((ipaddr->af == AF_INET) &&
2552 (ipaddr->ipaddr.ip4addr.s_addr == htonl(INADDR_LOOPBACK))) {
2556 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2557 if ((ipaddr->af == AF_INET6) &&
2558 (IN6_IS_ADDR_LINKLOCAL(&ipaddr->ipaddr.ip6addr))) {
2568 * Generate a list of listeners. Takes an input list of
2569 * listeners, too, so we don't close sockets with waiting packets.
2571 int listen_init(CONF_SECTION *config, rad_listen_t **head)
2573 int override = FALSE;
2575 CONF_SECTION *cs = NULL;
2576 rad_listen_t **last;
2578 fr_ipaddr_t server_ipaddr;
2581 int defined_proxy = 0;
2585 * We shouldn't be called with a pre-existing list.
2587 rad_assert(head && (*head == NULL));
2590 server_ipaddr.af = AF_UNSPEC;
2593 * If the port is specified on the command-line,
2594 * it over-rides the configuration file.
2596 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
2598 if (mainconfig.port >= 0) auth_port = mainconfig.port;
2601 * If the IP address was configured on the command-line,
2602 * use that as the "bind_address"
2604 if (mainconfig.myip.af != AF_UNSPEC) {
2605 memcpy(&server_ipaddr, &mainconfig.myip,
2606 sizeof(server_ipaddr));
2612 * Else look for bind_address and/or listen sections.
2614 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
2615 rcode = cf_item_parse(config, "bind_address",
2617 &server_ipaddr.ipaddr.ip4addr, NULL);
2618 if (rcode < 0) return -1; /* error parsing it */
2620 if (rcode == 0) { /* successfully parsed IPv4 */
2621 listen_socket_t *sock;
2622 server_ipaddr.af = AF_INET;
2624 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'.");
2628 if (strcmp(progname, "vmpsd") == 0) {
2629 this = listen_alloc(RAD_LISTEN_VQP);
2630 if (!auth_port) auth_port = 1589;
2633 this = listen_alloc(RAD_LISTEN_AUTH);
2637 sock->my_ipaddr = server_ipaddr;
2638 sock->my_port = auth_port;
2640 sock->clients = clients_parse_section(config);
2641 if (!sock->clients) {
2642 cf_log_err(cf_sectiontoitem(config),
2643 "Failed to find any clients for this listen section");
2648 if (listen_bind(this) < 0) {
2650 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
2654 auth_port = sock->my_port; /* may have been updated in listen_bind */
2656 cs = cf_section_sub_find_name2(config, "server",
2658 if (cs) this->server = mainconfig.name;
2662 last = &(this->next);
2668 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
2671 #ifdef WITH_ACCOUNTING
2673 * Open Accounting Socket.
2675 * If we haven't already gotten acct_port from
2676 * /etc/services, then make it auth_port + 1.
2678 this = listen_alloc(RAD_LISTEN_ACCT);
2682 * Create the accounting socket.
2684 * The accounting port is always the
2685 * authentication port + 1
2687 sock->my_ipaddr = server_ipaddr;
2688 sock->my_port = auth_port + 1;
2690 sock->clients = clients_parse_section(config);
2691 if (!sock->clients) {
2692 cf_log_err(cf_sectiontoitem(config),
2693 "Failed to find any clients for this listen section");
2697 if (listen_bind(this) < 0) {
2700 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
2705 cs = cf_section_sub_find_name2(config, "server",
2707 if (cs) this->server = mainconfig.name;
2711 last = &(this->next);
2713 } else if (mainconfig.port > 0) { /* no bind address, but a port */
2714 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
2720 * They specified an IP on the command-line, ignore
2721 * all listen sections except the one in '-n'.
2723 if (mainconfig.myip.af != AF_UNSPEC) {
2724 CONF_SECTION *subcs;
2725 const char *name2 = cf_section_name2(cs);
2727 cs = cf_section_sub_find_name2(config, "server",
2729 if (!cs) goto add_sockets;
2732 * Should really abstract this code...
2734 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2736 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2737 this = listen_parse(subcs, name2);
2744 last = &(this->next);
2745 } /* loop over "listen" directives in server <foo> */
2751 * Walk through the "listen" sections, if they exist.
2753 for (cs = cf_subsection_find_next(config, NULL, "listen");
2755 cs = cf_subsection_find_next(config, cs, "listen")) {
2756 this = listen_parse(cs, NULL);
2763 last = &(this->next);
2767 * Check virtual servers for "listen" sections, too.
2769 * FIXME: Move to virtual server init?
2771 for (cs = cf_subsection_find_next(config, NULL, "server");
2773 cs = cf_subsection_find_next(config, cs, "server")) {
2774 CONF_SECTION *subcs;
2775 const char *name2 = cf_section_name2(cs);
2777 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2779 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2780 this = listen_parse(subcs, name2);
2787 last = &(this->next);
2788 } /* loop over "listen" directives in virtual servers */
2789 } /* loop over virtual servers */
2793 * No sockets to receive packets, this is an error.
2794 * proxying is pointless.
2797 radlog(L_ERR, "The server is not configured to listen on any ports. Cannot start.");
2802 * Print out which sockets we're listening on, and
2803 * add them to the event list.
2805 for (this = *head; this != NULL; this = this->next) {
2807 if (this->type == RAD_LISTEN_PROXY) {
2816 * If we're proxying requests, open the proxy FD.
2817 * Otherwise, don't do anything.
2820 if ((mainconfig.proxy_requests == TRUE) &&
2821 (*head != NULL) && !defined_proxy) {
2822 listen_socket_t *sock = NULL;
2826 memset(&home, 0, sizeof(home));
2831 home.proto = IPPROTO_UDP;
2832 home.src_ipaddr = server_ipaddr;
2835 * Find the first authentication port,
2838 for (this = *head; this != NULL; this = this->next) {
2839 if (this->type == RAD_LISTEN_AUTH) {
2842 if (is_loopback(&sock->my_ipaddr)) continue;
2844 if (home.src_ipaddr.af == AF_UNSPEC) {
2845 home.src_ipaddr = sock->my_ipaddr;
2847 port = sock->my_port + 2;
2851 if (this->type == RAD_LISTEN_ACCT) {
2854 if (is_loopback(&sock->my_ipaddr)) continue;
2856 if (home.src_ipaddr.af == AF_UNSPEC) {
2857 home.src_ipaddr = sock->my_ipaddr;
2859 port = sock->my_port + 1;
2866 * Address is still unspecified, use IPv4.
2868 if (home.src_ipaddr.af == AF_UNSPEC) {
2869 home.src_ipaddr.af = AF_INET;
2870 /* everything else is already set to zero */
2873 home.ipaddr.af = home.src_ipaddr.af;
2874 /* everything else is already set to zero */
2876 if (!proxy_new_listener(&home, port)) {
2884 * Haven't defined any sockets. Die.
2886 if (!*head) return -1;
2888 xlat_register("listen", xlat_listen, NULL);
2894 * Free a linked list of listeners;
2896 void listen_free(rad_listen_t **head)
2900 if (!head || !*head) return;
2904 rad_listen_t *next = this->next;
2907 * Other code may have eaten the FD.
2909 if (this->fd >= 0) close(this->fd);
2911 if (master_listen[this->type].free) {
2912 master_listen[this->type].free(this);
2916 if ((this->type == RAD_LISTEN_AUTH)
2918 || (this->type == RAD_LISTEN_ACCT)
2921 || (this->type == RAD_LISTEN_PROXY)
2924 listen_socket_t *sock = this->data;
2925 rad_free(&sock->packet);
2939 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
2944 for (this = mainconfig.listen; this != NULL; this = this->next) {
2945 listen_socket_t *sock;
2947 if ((this->type != RAD_LISTEN_AUTH)
2948 #ifdef WITH_ACCOUNTING
2949 && (this->type != RAD_LISTEN_ACCT)
2955 if ((sock->my_port == port) &&
2956 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
2957 return sock->clients;
2965 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
2969 for (this = mainconfig.listen; this != NULL; this = this->next) {
2970 listen_socket_t *sock;
2973 * FIXME: For TCP, ignore the *secondary*
2974 * listeners associated with the main socket.
2976 if ((this->type != RAD_LISTEN_AUTH)
2977 #ifdef WITH_ACCOUNTING
2978 && (this->type != RAD_LISTEN_ACCT)
2984 if ((sock->my_port == port) &&
2985 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
2989 if ((sock->my_port == port) &&
2990 fr_inaddr_any(&sock->my_ipaddr)) {