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 * Find a per-socket client.
87 RADCLIENT *client_listener_find(const rad_listen_t *listener,
88 const fr_ipaddr_t *ipaddr, int src_port)
90 #ifdef WITH_DYNAMIC_CLIENTS
97 RADCLIENT_LIST *clients;
98 listen_socket_t *sock;
100 rad_assert(listener != NULL);
101 rad_assert(ipaddr != NULL);
103 sock = listener->data;
104 clients = sock->clients;
107 * This HAS to have been initialized previously.
109 rad_assert(clients != NULL);
111 client = client_find(clients, ipaddr,sock->proto);
113 char name[256], buffer[128];
115 #ifdef WITH_DYNAMIC_CLIENTS
116 unknown: /* used only for dynamic clients */
120 * DoS attack quenching, but only in daemon mode.
121 * If they're running in debug mode, show them
124 if (debug_flag == 0) {
125 static time_t last_printed = 0;
128 if (last_printed == now) return NULL;
133 listener->print(listener, name, sizeof(name));
135 radlog(L_ERR, "Ignoring request to %s from unknown client %s port %d"
139 , name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
140 buffer, sizeof(buffer)), src_port
142 , (sock->proto == IPPROTO_UDP) ? "udp" : "tcp"
148 #ifndef WITH_DYNAMIC_CLIENTS
149 return client; /* return the found client. */
153 * No server defined, and it's not dynamic. Return it.
155 if (!client->client_server && !client->dynamic) return client;
160 * It's a dynamically generated client, check it.
162 if (client->dynamic && (src_port != 0)) {
164 * Lives forever. Return it.
166 if (client->lifetime == 0) return client;
169 * Rate-limit the deletion of known clients.
170 * This makes them last a little longer, but
171 * prevents the server from melting down if (say)
172 * 10k clients all expire at once.
174 if (now == client->last_new_client) return client;
177 * It's not dead yet. Return it.
179 if ((client->created + client->lifetime) > now) return client;
182 * This really puts them onto a queue for later
185 client_delete(clients, client);
188 * Go find the enclosing network again.
190 client = client_find(clients, ipaddr, sock->proto);
195 if (!client) goto unknown;
196 if (!client->client_server) goto unknown;
199 * At this point, 'client' is the enclosing
200 * network that configures where dynamic clients
203 rad_assert(client->dynamic == 0);
205 } else if (!client->dynamic && client->rate_limit) {
207 * The IP is unknown, so we've found an enclosing
208 * network. Enable DoS protection. We only
209 * allow one new client per second. Known
210 * clients aren't subject to this restriction.
212 if (now == client->last_new_client) goto unknown;
215 client->last_new_client = now;
217 request = request_alloc();
218 if (!request) goto unknown;
220 request->listener = listener;
221 request->client = client;
222 request->packet = rad_recv(listener->fd, 0x02); /* MSG_PEEK */
223 if (!request->packet) { /* badly formed, etc */
224 request_free(&request);
227 request->reply = rad_alloc_reply(request->packet);
228 if (!request->reply) {
229 request_free(&request);
232 request->packet->timestamp = request->timestamp;
234 request->priority = listener->type;
235 request->server = client->client_server;
236 request->root = &mainconfig;
239 * Run a fake request through the given virtual server.
240 * Look for FreeRADIUS-Client-IP-Address
241 * FreeRADIUS-Client-Secret
244 * and create the RADCLIENT structure from that.
246 DEBUG("server %s {", request->server);
248 rcode = module_authorize(0, request);
250 DEBUG("} # server %s", request->server);
252 if (rcode != RLM_MODULE_OK) {
253 request_free(&request);
258 * If the client was updated by rlm_dynamic_clients,
259 * don't create the client from attribute-value pairs.
261 if (request->client == client) {
262 created = client_create(clients, request);
264 created = request->client;
267 * This frees the client if it isn't valid.
269 if (!client_validate(clients, client, created)) goto unknown;
271 request_free(&request);
273 if (!created) goto unknown;
279 static int listen_bind(rad_listen_t *this);
283 * Process and reply to a server-status request.
284 * Like rad_authenticate and rad_accounting this should
285 * live in it's own file but it's so small we don't bother.
287 static int rad_status_server(REQUEST *request)
289 int rcode = RLM_MODULE_OK;
292 switch (request->listener->type) {
294 case RAD_LISTEN_NONE:
296 case RAD_LISTEN_AUTH:
297 dval = dict_valbyname(PW_AUTZ_TYPE, 0, "Status-Server");
299 rcode = module_authorize(dval->value, request);
301 rcode = RLM_MODULE_OK;
306 case RLM_MODULE_UPDATED:
307 request->reply->code = PW_AUTHENTICATION_ACK;
310 case RLM_MODULE_FAIL:
311 case RLM_MODULE_HANDLED:
312 request->reply->code = 0; /* don't reply */
316 case RLM_MODULE_REJECT:
317 request->reply->code = PW_AUTHENTICATION_REJECT;
322 #ifdef WITH_ACCOUNTING
323 case RAD_LISTEN_ACCT:
324 dval = dict_valbyname(PW_ACCT_TYPE, 0, "Status-Server");
326 rcode = module_accounting(dval->value, request);
328 rcode = RLM_MODULE_OK;
333 case RLM_MODULE_UPDATED:
334 request->reply->code = PW_ACCOUNTING_RESPONSE;
338 request->reply->code = 0; /* don't reply */
346 * This is a vendor extension. Suggested by Glen
347 * Zorn in IETF 72, and rejected by the rest of
348 * the WG. We like it, so it goes in here.
351 dval = dict_valbyname(PW_RECV_COA_TYPE, 0, "Status-Server");
353 rcode = module_recv_coa(dval->value, request);
355 rcode = RLM_MODULE_OK;
360 case RLM_MODULE_UPDATED:
361 request->reply->code = PW_COA_ACK;
365 request->reply->code = 0; /* don't reply */
377 * Full statistics are available only on a statistics
380 if (request->listener->type == RAD_LISTEN_NONE) {
381 request_stats_reply(request);
389 static int auth_tcp_recv(rad_listen_t *listener,
390 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
393 RADIUS_PACKET *packet;
394 RAD_REQUEST_FUNP fun = NULL;
395 listen_socket_t *sock = listener->data;
396 RADCLIENT *client = sock->client;
399 * Allocate a packet for partial reads.
402 sock->packet = rad_alloc(0);
403 if (!sock->packet) return 0;
405 sock->packet->sockfd = listener->fd;
406 sock->packet->src_ipaddr = sock->other_ipaddr;
407 sock->packet->src_port = sock->other_port;
408 sock->packet->dst_ipaddr = sock->my_ipaddr;
409 sock->packet->dst_port = sock->my_port;
413 * Grab the packet currently being processed.
415 packet = sock->packet;
417 rcode = fr_tcp_read_packet(packet, 0);
420 * Still only a partial packet. Put it back, and return,
421 * so that we'll read more data when it's ready.
427 if (rcode == -1) { /* error reading packet */
430 radlog(L_ERR, "Invalid packet from %s port %d: closing socket",
431 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
435 if (rcode < 0) { /* error or connection reset */
436 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
439 * Decrement the number of connections.
441 if (sock->parent->num_connections > 0) {
442 sock->parent->num_connections--;
444 if (sock->client->num_connections > 0) {
445 sock->client->num_connections--;
449 * Tell the event handler that an FD has disappeared.
451 DEBUG("Client has closed connection");
452 event_new_fd(listener);
455 * Do NOT free the listener here. It's in use by
456 * a request, and will need to hang around until
457 * all of the requests are done.
459 * It is instead free'd in remove_from_request_hash()
464 RAD_STATS_TYPE_INC(listener, total_requests);
467 * Some sanity checks, based on the packet code.
469 switch(packet->code) {
470 case PW_AUTHENTICATION_REQUEST:
471 RAD_STATS_CLIENT_INC(listener, client, total_requests);
472 fun = rad_authenticate;
475 case PW_STATUS_SERVER:
476 if (!mainconfig.status_server) {
477 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
478 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
479 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
480 rad_free(&sock->packet);
483 fun = rad_status_server;
487 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
488 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
490 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
491 packet->code, client->shortname, packet->src_port);
492 rad_free(&sock->packet);
494 } /* switch over packet types */
496 if (!received_request(listener, packet, prequest, sock->client)) {
497 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
498 RAD_STATS_CLIENT_INC(listener, sock->client, total_packets_dropped);
499 rad_free(&sock->packet);
504 sock->packet = NULL; /* we have no need for more partial reads */
508 static int auth_tcp_accept(rad_listen_t *listener,
509 UNUSED RAD_REQUEST_FUNP *pfun,
510 UNUSED REQUEST **prequest)
515 struct sockaddr_storage src;
516 listen_socket_t *sock;
517 fr_ipaddr_t src_ipaddr;
522 DEBUG2(" ... new connection request on TCP socket.");
524 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
527 * Non-blocking sockets must handle this.
529 if (errno == EWOULDBLOCK) {
533 DEBUG2(" ... failed to accept connection.");
537 if (!fr_sockaddr2ipaddr(&src, salen, &src_ipaddr, &src_port)) {
538 DEBUG2(" ... unknown address family.");
543 * Enforce client IP address checks on accept, not on
546 if ((client = client_listener_find(listener,
547 &src_ipaddr, src_port)) == NULL) {
549 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
554 * Enforce max_connectionsx on client && listen section.
556 if ((client->max_connections != 0) &&
557 (client->max_connections == client->num_connections)) {
559 * FIXME: Print client IP/port, and server IP/port.
561 radlog(L_INFO, "Ignoring new connection due to client max_connections (%d)", client->max_connections);
566 sock = listener->data;
567 if ((sock->max_connections != 0) &&
568 (sock->max_connections == sock->num_connections)) {
570 * FIXME: Print client IP/port, and server IP/port.
572 radlog(L_INFO, "Ignoring new connection due to socket max_connections");
576 client->num_connections++;
577 sock->num_connections++;
580 * Add the new listener.
582 this = listen_alloc(listener->type);
583 if (!this) return -1;
586 * Copy everything, including the pointer to the socket
590 memcpy(this->data, listener->data, sizeof(*sock));
591 memcpy(this, listener, sizeof(*this));
593 this->data = sock; /* fix it back */
595 sock->parent = listener->data;
596 sock->other_ipaddr = src_ipaddr;
597 sock->other_port = src_port;
598 sock->client = client;
601 this->status = RAD_LISTEN_STATUS_INIT;
602 this->recv = auth_tcp_recv;
605 * FIXME: set O_NONBLOCK on the accept'd fd.
606 * See djb's portability rants for details.
610 * Tell the event loop that we have a new FD.
611 * This can be called from a child thread...
621 * This function is stupid and complicated.
623 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
626 listen_socket_t *sock = this->data;
629 switch (this->type) {
631 case RAD_LISTEN_NONE: /* what a hack... */
636 case RAD_LISTEN_AUTH:
637 name = "authentication";
640 #ifdef WITH_ACCOUNTING
641 case RAD_LISTEN_ACCT:
647 case RAD_LISTEN_PROXY:
659 case RAD_LISTEN_DHCP:
675 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
676 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
680 if (sock->interface) {
681 ADDSTRING(" interface ");
682 ADDSTRING(sock->interface);
686 if (this->recv == auth_tcp_accept) {
687 ADDSTRING(" proto tcp");
693 * TCP sockets get printed a little differently, to make
694 * it clear what's going on.
697 ADDSTRING(" from client (");
698 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
702 snprintf(buffer, bufsize, "%d", sock->other_port);
706 if ((sock->my_ipaddr.af == AF_INET) &&
707 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
708 strlcpy(buffer, "*", bufsize);
710 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
715 snprintf(buffer, bufsize, "%d", sock->my_port);
719 ADDSTRING(", virtual-server=");
720 ADDSTRING(this->server);
729 * Maybe it's a socket that we opened to a home server.
731 if ((sock->proto == IPPROTO_TCP) &&
732 (this->type == RAD_LISTEN_PROXY)) {
734 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
738 snprintf(buffer, bufsize, "%d", sock->my_port);
740 ADDSTRING(") -> home_server (");
742 if ((sock->other_ipaddr.af == AF_INET) &&
743 (sock->other_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
744 strlcpy(buffer, "*", bufsize);
746 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
751 snprintf(buffer, bufsize, "%d", sock->other_port);
760 ADDSTRING(" address ");
762 if ((sock->my_ipaddr.af == AF_INET) &&
763 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
764 strlcpy(buffer, "*", bufsize);
766 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
771 snprintf(buffer, bufsize, "%d", sock->my_port);
775 ADDSTRING(" as server ");
776 ADDSTRING(this->server);
785 extern int check_config; /* radiusd.c */
789 * Parse an authentication or accounting socket.
791 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
796 listen_socket_t *sock = this->data;
797 char *section_name = NULL;
798 CONF_SECTION *client_cs, *parentcs;
803 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
804 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
805 &ipaddr.ipaddr.ip4addr, NULL);
806 if (rcode < 0) return -1;
808 if (rcode == 0) { /* successfully parsed IPv4 */
811 } else { /* maybe IPv6? */
812 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
813 &ipaddr.ipaddr.ip6addr, NULL);
814 if (rcode < 0) return -1;
817 cf_log_err(cf_sectiontoitem(cs),
818 "No address specified in listen section");
821 ipaddr.af = AF_INET6;
824 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
826 if (rcode < 0) return -1;
828 if ((listen_port < 0) || (listen_port > 65535)) {
829 cf_log_err(cf_sectiontoitem(cs),
830 "Invalid value for \"port\"");
834 sock->proto = IPPROTO_UDP;
836 if (cf_pair_find(cs, "proto")) {
838 cf_log_err(cf_sectiontoitem(cs),
839 "System does not support the TCP protocol. Delete this line from the configuration file.");
845 rcode = cf_item_parse(cs, "proto", PW_TYPE_STRING_PTR,
847 if (rcode < 0) return -1;
849 if (strcmp(proto, "udp") == 0) {
850 sock->proto = IPPROTO_UDP;
852 } else if (strcmp(proto, "tcp") == 0) {
853 sock->proto = IPPROTO_TCP;
855 rcode = cf_item_parse(cs, "max_connections", PW_TYPE_INTEGER,
856 &sock->max_connections, "64");
857 if (rcode < 0) return -1;
860 cf_log_err(cf_sectiontoitem(cs),
861 "Unknown proto name \"%s\"", proto);
868 * TCP requires a destination IP for sockets.
869 * UDP doesn't, so it's allowed.
871 if ((this->type == RAD_LISTEN_PROXY) &&
872 (sock->proto != IPPROTO_UDP)) {
873 cf_log_err(cf_sectiontoitem(cs),
874 "Proxy listeners can only listen on proto = udp");
880 sock->my_ipaddr = ipaddr;
881 sock->my_port = listen_port;
884 if (home_server_find(&sock->ipaddr, sock->port)) {
887 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.",
888 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
893 return 0; /* don't do anything */
897 * If we can bind to interfaces, do so,
900 if (cf_pair_find(cs, "interface")) {
902 CONF_PAIR *cp = cf_pair_find(cs, "interface");
904 rad_assert(cp != NULL);
905 value = cf_pair_value(cp);
907 cf_log_err(cf_sectiontoitem(cs),
908 "No interface name given");
911 sock->interface = value;
916 * If we can do broadcasts..
918 if (cf_pair_find(cs, "broadcast")) {
920 cf_log_err(cf_sectiontoitem(cs),
921 "System does not support broadcast sockets. Delete this line from the configuration file.");
925 CONF_PAIR *cp = cf_pair_find(cs, "broadcast");
927 if (this->type != RAD_LISTEN_DHCP) {
928 cf_log_err(cf_pairtoitem(cp),
929 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file.");
933 rad_assert(cp != NULL);
934 value = cf_pair_value(cp);
936 cf_log_err(cf_sectiontoitem(cs),
937 "No broadcast value given");
942 * Hack... whatever happened to cf_section_parse?
944 sock->broadcast = (strcmp(value, "yes") == 0);
950 * And bind it to the port.
952 if (listen_bind(this) < 0) {
954 cf_log_err(cf_sectiontoitem(cs),
955 "Error binding to port for %s port %d",
956 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
963 * Proxy sockets don't have clients.
965 if (this->type == RAD_LISTEN_PROXY) return 0;
969 * The more specific configurations are preferred to more
973 parentcs = cf_top_section(cs);
974 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
975 §ion_name, NULL);
976 if (rcode < 0) return -1; /* bad string */
979 * Explicit list given: use it.
981 client_cs = cf_section_sub_find_name2(parentcs,
985 client_cs = cf_section_find(section_name);
988 cf_log_err(cf_sectiontoitem(cs),
989 "Failed to find clients %s {...}",
995 } /* else there was no "clients = " entry. */
998 CONF_SECTION *server_cs;
1000 server_cs = cf_section_sub_find_name2(parentcs,
1004 * Found a "server foo" section. If there are clients
1008 (cf_section_sub_find(server_cs, "client") != NULL)) {
1009 client_cs = server_cs;
1014 * Still nothing. Look for global clients.
1016 if (!client_cs) client_cs = parentcs;
1018 sock->clients = clients_parse_section(client_cs);
1019 if (!sock->clients) {
1020 cf_log_err(cf_sectiontoitem(cs),
1021 "Failed to load clients for this listen section");
1026 if (sock->proto == IPPROTO_TCP) {
1028 * Re-write the listener receive function to
1029 * allow us to accept the socket.
1031 this->recv = auth_tcp_accept;
1039 * Send an authentication response packet
1041 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1043 rad_assert(request->listener == listener);
1044 rad_assert(listener->send == auth_socket_send);
1046 return rad_send(request->reply, request->packet,
1047 request->client->secret);
1051 #ifdef WITH_ACCOUNTING
1053 * Send an accounting response packet (or not)
1055 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1057 rad_assert(request->listener == listener);
1058 rad_assert(listener->send == acct_socket_send);
1061 * Accounting reject's are silently dropped.
1063 * We do it here to avoid polluting the rest of the
1064 * code with this knowledge
1066 if (request->reply->code == 0) return 0;
1068 return rad_send(request->reply, request->packet,
1069 request->client->secret);
1075 * Send a packet to a home server.
1077 * FIXME: have different code for proxy auth & acct!
1079 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1081 rad_assert(request->proxy_listener == listener);
1082 rad_assert(listener->send == proxy_socket_send);
1084 return rad_send(request->proxy, request->packet,
1085 request->home_server->secret);
1091 * Check if an incoming request is "ok"
1093 * It takes packets, not requests. It sees if the packet looks
1094 * OK. If so, it does a number of sanity checks on it.
1096 static int stats_socket_recv(rad_listen_t *listener,
1097 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1101 RADIUS_PACKET *packet;
1103 fr_ipaddr_t src_ipaddr;
1105 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1106 if (rcode < 0) return 0;
1108 RAD_STATS_TYPE_INC(listener, total_requests);
1110 if (rcode < 20) { /* AUTH_HDR_LEN */
1111 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1115 if ((client = client_listener_find(listener,
1116 &src_ipaddr, src_port)) == NULL) {
1117 rad_recv_discard(listener->fd);
1118 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1123 * We only understand Status-Server on this socket.
1125 if (code != PW_STATUS_SERVER) {
1126 DEBUG("Ignoring packet code %d sent to Status-Server port",
1128 rad_recv_discard(listener->fd);
1129 RAD_STATS_TYPE_INC(listener, total_unknown_types);
1130 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1135 * Now that we've sanity checked everything, receive the
1138 packet = rad_recv(listener->fd, 1); /* require message authenticator */
1140 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1141 DEBUG("%s", fr_strerror());
1145 if (!received_request(listener, packet, prequest, client)) {
1146 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1147 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1152 *pfun = rad_status_server;
1159 * Check if an incoming request is "ok"
1161 * It takes packets, not requests. It sees if the packet looks
1162 * OK. If so, it does a number of sanity checks on it.
1164 static int auth_socket_recv(rad_listen_t *listener,
1165 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1169 RADIUS_PACKET *packet;
1170 RAD_REQUEST_FUNP fun = NULL;
1172 fr_ipaddr_t src_ipaddr;
1174 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1175 if (rcode < 0) return 0;
1177 RAD_STATS_TYPE_INC(listener, total_requests);
1179 if (rcode < 20) { /* AUTH_HDR_LEN */
1180 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1184 if ((client = client_listener_find(listener,
1185 &src_ipaddr, src_port)) == NULL) {
1186 rad_recv_discard(listener->fd);
1187 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1192 * Some sanity checks, based on the packet code.
1195 case PW_AUTHENTICATION_REQUEST:
1196 RAD_STATS_CLIENT_INC(listener, client, total_requests);
1197 fun = rad_authenticate;
1200 case PW_STATUS_SERVER:
1201 if (!mainconfig.status_server) {
1202 rad_recv_discard(listener->fd);
1203 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1204 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1205 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1208 fun = rad_status_server;
1212 rad_recv_discard(listener->fd);
1213 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
1214 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1216 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
1217 code, client->shortname, src_port);
1220 } /* switch over packet types */
1223 * Now that we've sanity checked everything, receive the
1226 packet = rad_recv(listener->fd, client->message_authenticator);
1228 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1229 DEBUG("%s", fr_strerror());
1233 if (!received_request(listener, packet, prequest, client)) {
1234 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1235 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1245 #ifdef WITH_ACCOUNTING
1247 * Receive packets from an accounting socket
1249 static int acct_socket_recv(rad_listen_t *listener,
1250 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1254 RADIUS_PACKET *packet;
1255 RAD_REQUEST_FUNP fun = NULL;
1257 fr_ipaddr_t src_ipaddr;
1259 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1260 if (rcode < 0) return 0;
1262 RAD_STATS_TYPE_INC(listener, total_requests);
1264 if (rcode < 20) { /* AUTH_HDR_LEN */
1265 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1269 if ((client = client_listener_find(listener,
1270 &src_ipaddr, src_port)) == NULL) {
1271 rad_recv_discard(listener->fd);
1272 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1277 * Some sanity checks, based on the packet code.
1280 case PW_ACCOUNTING_REQUEST:
1281 RAD_STATS_CLIENT_INC(listener, client, total_requests);
1282 fun = rad_accounting;
1285 case PW_STATUS_SERVER:
1286 if (!mainconfig.status_server) {
1287 rad_recv_discard(listener->fd);
1288 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1289 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1291 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1294 fun = rad_status_server;
1298 rad_recv_discard(listener->fd);
1299 RAD_STATS_TYPE_INC(listener, total_unknown_types);
1300 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1302 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1303 code, client->shortname, src_port);
1305 } /* switch over packet types */
1308 * Now that we've sanity checked everything, receive the
1311 packet = rad_recv(listener->fd, 0);
1313 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1314 radlog(L_ERR, "%s", fr_strerror());
1319 * There can be no duplicate accounting packets.
1321 if (!received_request(listener, packet, prequest, client)) {
1322 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1323 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1336 * For now, all CoA requests are *only* originated, and not
1337 * proxied. So all of the necessary work is done in the
1338 * post-proxy section, which is automatically handled by event.c.
1339 * As a result, we don't have to do anything here.
1341 static int rad_coa_reply(REQUEST *request)
1343 VALUE_PAIR *s1, *s2;
1346 * Inform the user about RFC requirements.
1348 s1 = pairfind(request->proxy->vps, PW_STATE, 0);
1350 s2 = pairfind(request->proxy_reply->vps, PW_STATE, 0);
1353 DEBUG("WARNING: Client was sent State in CoA, and did not respond with State.");
1355 } else if ((s1->length != s2->length) ||
1356 (memcmp(s1->vp_octets, s2->vp_octets,
1357 s1->length) != 0)) {
1358 DEBUG("WARNING: Client was sent State in CoA, and did not respond with the same State.");
1362 return RLM_MODULE_OK;
1366 * Receive a CoA packet.
1368 static int rad_coa_recv(REQUEST *request)
1370 int rcode = RLM_MODULE_OK;
1375 * Get the correct response
1377 switch (request->packet->code) {
1378 case PW_COA_REQUEST:
1383 case PW_DISCONNECT_REQUEST:
1384 ack = PW_DISCONNECT_ACK;
1385 nak = PW_DISCONNECT_NAK;
1388 default: /* shouldn't happen */
1389 return RLM_MODULE_FAIL;
1393 #define WAS_PROXIED (request->proxy)
1395 #define WAS_PROXIED (0)
1400 * RFC 5176 Section 3.3. If we have a CoA-Request
1401 * with Service-Type = Authorize-Only, it MUST
1402 * have a State attribute in it.
1404 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE, 0);
1405 if (request->packet->code == PW_COA_REQUEST) {
1406 if (vp && (vp->vp_integer == 17)) {
1407 vp = pairfind(request->packet->vps, PW_STATE, 0);
1408 if (!vp || (vp->length == 0)) {
1409 RDEBUG("ERROR: CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1410 request->reply->code = PW_COA_NAK;
1411 return RLM_MODULE_FAIL;
1416 * RFC 5176, Section 3.2.
1418 RDEBUG("ERROR: Disconnect-Request MUST NOT contain a Service-Type attribute");
1419 request->reply->code = PW_DISCONNECT_NAK;
1420 return RLM_MODULE_FAIL;
1423 rcode = module_recv_coa(0, request);
1425 case RLM_MODULE_FAIL:
1426 case RLM_MODULE_INVALID:
1427 case RLM_MODULE_REJECT:
1428 case RLM_MODULE_USERLOCK:
1430 request->reply->code = nak;
1433 case RLM_MODULE_HANDLED:
1436 case RLM_MODULE_NOOP:
1437 case RLM_MODULE_NOTFOUND:
1439 case RLM_MODULE_UPDATED:
1440 request->reply->code = ack;
1445 * Start the reply code with the proxy reply
1448 request->reply->code = request->proxy_reply->code;
1452 * Copy State from the request to the reply.
1453 * See RFC 5176 Section 3.3.
1455 vp = paircopy2(request->packet->vps, PW_STATE, 0);
1456 if (vp) pairadd(&request->reply->vps, vp);
1459 * We may want to over-ride the reply.
1461 rcode = module_send_coa(0, request);
1464 * We need to send CoA-NAK back if Service-Type
1465 * is Authorize-Only. Rely on the user's policy
1466 * to do that. We're not a real NAS, so this
1467 * restriction doesn't (ahem) apply to us.
1469 case RLM_MODULE_FAIL:
1470 case RLM_MODULE_INVALID:
1471 case RLM_MODULE_REJECT:
1472 case RLM_MODULE_USERLOCK:
1475 * Over-ride an ACK with a NAK
1477 request->reply->code = nak;
1480 case RLM_MODULE_HANDLED:
1483 case RLM_MODULE_NOOP:
1484 case RLM_MODULE_NOTFOUND:
1486 case RLM_MODULE_UPDATED:
1488 * Do NOT over-ride a previously set value.
1489 * Otherwise an "ok" here will re-write a
1492 if (request->reply->code == 0) {
1493 request->reply->code = ack;
1499 return RLM_MODULE_OK;
1504 * Check if an incoming request is "ok"
1506 * It takes packets, not requests. It sees if the packet looks
1507 * OK. If so, it does a number of sanity checks on it.
1509 static int coa_socket_recv(rad_listen_t *listener,
1510 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1514 RADIUS_PACKET *packet;
1515 RAD_REQUEST_FUNP fun = NULL;
1517 fr_ipaddr_t src_ipaddr;
1519 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1520 if (rcode < 0) return 0;
1522 RAD_STATS_TYPE_INC(listener, total_requests);
1524 if (rcode < 20) { /* AUTH_HDR_LEN */
1525 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1529 if ((client = client_listener_find(listener,
1530 &src_ipaddr, src_port)) == NULL) {
1531 rad_recv_discard(listener->fd);
1532 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1537 * Some sanity checks, based on the packet code.
1540 case PW_COA_REQUEST:
1541 case PW_DISCONNECT_REQUEST:
1546 rad_recv_discard(listener->fd);
1547 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1548 code, client->shortname, src_port);
1551 } /* switch over packet types */
1554 * Now that we've sanity checked everything, receive the
1557 packet = rad_recv(listener->fd, client->message_authenticator);
1559 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1560 DEBUG("%s", fr_strerror());
1564 if (!received_request(listener, packet, prequest, client)) {
1576 * Recieve packets from a proxy socket.
1578 static int proxy_socket_recv(rad_listen_t *listener,
1579 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1582 RADIUS_PACKET *packet;
1583 RAD_REQUEST_FUNP fun = NULL;
1586 packet = rad_recv(listener->fd, 0);
1588 radlog(L_ERR, "%s", fr_strerror());
1593 * FIXME: Client MIB updates?
1595 switch(packet->code) {
1596 case PW_AUTHENTICATION_ACK:
1597 case PW_ACCESS_CHALLENGE:
1598 case PW_AUTHENTICATION_REJECT:
1599 fun = rad_authenticate;
1602 #ifdef WITH_ACCOUNTING
1603 case PW_ACCOUNTING_RESPONSE:
1604 fun = rad_accounting;
1609 case PW_DISCONNECT_ACK:
1610 case PW_DISCONNECT_NAK:
1613 fun = rad_coa_reply;
1619 * FIXME: Update MIB for packet types?
1621 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1622 "from home server %s port %d - ID %d : IGNORED",
1624 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1625 packet->src_port, packet->id);
1630 request = received_proxy_response(packet);
1638 * Distinguish proxied CoA requests from ones we
1641 if ((fun == rad_coa_reply) &&
1642 (request->packet->code == request->proxy->code)) {
1647 rad_assert(fun != NULL);
1649 *prequest = request;
1656 * Recieve packets from a proxy socket.
1658 static int proxy_socket_tcp_recv(rad_listen_t *listener,
1659 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1662 RADIUS_PACKET *packet;
1663 RAD_REQUEST_FUNP fun = NULL;
1664 listen_socket_t *sock = listener->data;
1667 packet = fr_tcp_recv(listener->fd, 0);
1669 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
1670 event_new_fd(listener);
1675 * FIXME: Client MIB updates?
1677 switch(packet->code) {
1678 case PW_AUTHENTICATION_ACK:
1679 case PW_ACCESS_CHALLENGE:
1680 case PW_AUTHENTICATION_REJECT:
1681 fun = rad_authenticate;
1684 #ifdef WITH_ACCOUNTING
1685 case PW_ACCOUNTING_RESPONSE:
1686 fun = rad_accounting;
1692 * FIXME: Update MIB for packet types?
1694 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1695 "from home server %s port %d - ID %d : IGNORED",
1697 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1698 packet->src_port, packet->id);
1703 packet->src_ipaddr = sock->other_ipaddr;
1704 packet->src_port = sock->other_port;
1705 packet->dst_ipaddr = sock->my_ipaddr;
1706 packet->dst_port = sock->my_port;
1709 * FIXME: Have it return an indication of packets that
1710 * are OK to ignore (dups, too late), versus ones that
1711 * aren't OK to ignore (unknown response, spoofed, etc.)
1713 * Close the socket on bad packets...
1715 request = received_proxy_response(packet);
1720 rad_assert(fun != NULL);
1721 sock->opened = sock->last_packet = request->timestamp;
1724 *prequest = request;
1732 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1734 if (!request->reply->code) return 0;
1736 rad_encode(request->reply, request->packet,
1737 request->client->secret);
1738 rad_sign(request->reply, request->packet,
1739 request->client->secret);
1745 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1747 if (rad_verify(request->packet, NULL,
1748 request->client->secret) < 0) {
1752 return rad_decode(request->packet, NULL,
1753 request->client->secret);
1757 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1759 rad_encode(request->proxy, NULL, request->home_server->secret);
1760 rad_sign(request->proxy, NULL, request->home_server->secret);
1766 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1769 * rad_verify is run in event.c, received_proxy_response()
1772 return rad_decode(request->proxy_reply, request->proxy,
1773 request->home_server->secret);
1779 #include "command.c"
1781 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1783 { common_socket_parse, NULL,
1784 stats_socket_recv, auth_socket_send,
1785 socket_print, client_socket_encode, client_socket_decode },
1788 * This always gets defined.
1790 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1795 { common_socket_parse, NULL,
1796 proxy_socket_recv, proxy_socket_send,
1797 socket_print, proxy_socket_encode, proxy_socket_decode },
1800 /* authentication */
1801 { common_socket_parse, NULL,
1802 auth_socket_recv, auth_socket_send,
1803 socket_print, client_socket_encode, client_socket_decode },
1805 #ifdef WITH_ACCOUNTING
1807 { common_socket_parse, NULL,
1808 acct_socket_recv, acct_socket_send,
1809 socket_print, client_socket_encode, client_socket_decode},
1814 { detail_parse, detail_free,
1815 detail_recv, detail_send,
1816 detail_print, detail_encode, detail_decode },
1820 /* vlan query protocol */
1821 { common_socket_parse, NULL,
1822 vqp_socket_recv, vqp_socket_send,
1823 socket_print, vqp_socket_encode, vqp_socket_decode },
1827 /* dhcp query protocol */
1828 { dhcp_socket_parse, NULL,
1829 dhcp_socket_recv, dhcp_socket_send,
1830 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1833 #ifdef WITH_COMMAND_SOCKET
1834 /* TCP command socket */
1835 { command_socket_parse, command_socket_free,
1836 command_domain_accept, command_domain_send,
1837 command_socket_print, command_socket_encode, command_socket_decode },
1841 /* Change of Authorization */
1842 { common_socket_parse, NULL,
1843 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
1844 socket_print, client_socket_encode, client_socket_decode },
1852 * Binds a listener to a socket.
1854 static int listen_bind(rad_listen_t *this)
1857 struct sockaddr_storage salocal;
1859 listen_socket_t *sock = this->data;
1861 #define proto_for_port "udp"
1862 #define sock_type SOCK_DGRAM
1864 const char *proto_for_port = "udp";
1865 int sock_type = SOCK_DGRAM;
1867 if (sock->proto == IPPROTO_TCP) {
1869 if (this->type == RAD_LISTEN_VQP) {
1870 radlog(L_ERR, "VQP does not support TCP transport");
1875 proto_for_port = "tcp";
1876 sock_type = SOCK_STREAM;
1881 * If the port is zero, then it means the appropriate
1882 * thing from /etc/services.
1884 if (sock->my_port == 0) {
1885 struct servent *svp;
1887 switch (this->type) {
1888 case RAD_LISTEN_AUTH:
1889 svp = getservbyname ("radius", proto_for_port);
1891 sock->my_port = ntohs(svp->s_port);
1893 sock->my_port = PW_AUTH_UDP_PORT;
1897 #ifdef WITH_ACCOUNTING
1898 case RAD_LISTEN_ACCT:
1899 svp = getservbyname ("radacct", proto_for_port);
1901 sock->my_port = ntohs(svp->s_port);
1903 sock->my_port = PW_ACCT_UDP_PORT;
1909 case RAD_LISTEN_PROXY:
1910 /* leave it at zero */
1915 case RAD_LISTEN_VQP:
1916 sock->my_port = 1589;
1921 case RAD_LISTEN_COA:
1922 svp = getservbyname ("radius-dynauth", "udp");
1924 sock->my_port = ntohs(svp->s_port);
1926 sock->my_port = PW_COA_UDP_PORT;
1932 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1938 * Copy fr_socket() here, as we may need to bind to a device.
1940 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
1942 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
1947 * Bind to a device BEFORE touching IP addresses.
1949 if (sock->interface) {
1950 #ifdef SO_BINDTODEVICE
1952 strcpy(ifreq.ifr_name, sock->interface);
1955 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1956 (char *)&ifreq, sizeof(ifreq));
1960 radlog(L_ERR, "Failed binding to interface %s: %s",
1961 sock->interface, strerror(errno));
1963 } /* else it worked. */
1965 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1966 #ifdef HAVE_NET_IF_H
1968 * Odds are that any system supporting "bind to
1969 * device" also supports IPv6, so this next bit
1970 * isn't necessary. But it's here for
1973 * If we're doing IPv6, and the scope hasn't yet
1974 * been defined, set the scope to the scope of
1977 if (sock->my_ipaddr.af == AF_INET6) {
1978 if (sock->my_ipaddr.scope == 0) {
1979 sock->my_ipaddr.scope = if_nametoindex(sock->interface);
1980 if (sock->my_ipaddr.scope == 0) {
1982 radlog(L_ERR, "Failed finding interface %s: %s",
1983 sock->interface, strerror(errno));
1986 } /* else scope was defined: we're OK. */
1991 * IPv4: no link local addresses,
1992 * and no bind to device.
1996 radlog(L_ERR, "Failed binding to interface %s: \"bind to device\" is unsupported", sock->interface);
2003 if (sock->proto == IPPROTO_TCP) {
2006 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2008 radlog(L_ERR, "Failed to reuse address: %s", strerror(errno));
2014 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
2015 else /* UDP sockets get UDPfromto */
2018 #ifdef WITH_UDPFROMTO
2020 * Initialize udpfromto for all sockets.
2022 if (udpfromto_init(this->fd) != 0) {
2029 * Set up sockaddr stuff.
2031 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
2036 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2037 if (sock->my_ipaddr.af == AF_INET6) {
2039 * Listening on '::' does NOT get you IPv4 to
2040 * IPv6 mapping. You've got to listen on an IPv4
2041 * address, too. This makes the rest of the server
2042 * design a little simpler.
2046 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2049 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2050 (char *)&on, sizeof(on));
2052 #endif /* IPV6_V6ONLY */
2054 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2056 if (sock->my_ipaddr.af == AF_INET) {
2059 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2061 * Disable PMTU discovery. On Linux, this
2062 * also makes sure that the "don't fragment"
2065 flag = IP_PMTUDISC_DONT;
2066 setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2067 &flag, sizeof(flag));
2070 #if defined(IP_DONTFRAG)
2072 * Ensure that the "don't fragment" flag is zero.
2075 setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2076 &flag, sizeof(flag));
2082 if (sock->broadcast) {
2085 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2086 radlog(L_ERR, "Can't set broadcast option: %s\n",
2095 * May be binding to priviledged ports.
2097 if (sock->my_port != 0) {
2101 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2102 radlog(L_ERR, "Can't set re-use address option: %s\n",
2109 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2115 this->print(this, buffer, sizeof(buffer));
2116 radlog(L_ERR, "Failed binding to %s: %s\n",
2117 buffer, strerror(errno));
2122 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2123 * kernel instead binds us to a 1.2.3.4. If this
2124 * happens, notice, and remember our real IP.
2127 struct sockaddr_storage src;
2128 socklen_t sizeof_src = sizeof(src);
2130 memset(&src, 0, sizeof_src);
2131 if (getsockname(this->fd, (struct sockaddr *) &src,
2133 radlog(L_ERR, "Failed getting socket name: %s",
2138 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2139 &sock->my_ipaddr, &sock->my_port)) {
2140 radlog(L_ERR, "Socket has unsupported address family");
2147 if (sock->proto == IPPROTO_TCP) {
2148 if (listen(this->fd, 8) < 0) {
2150 radlog(L_ERR, "Failed in listen(): %s", strerror(errno));
2156 if (fr_nonblock(this->fd) < 0) {
2158 radlog(L_ERR, "Failed setting non-blocking on socket: %s",
2164 * Mostly for proxy sockets.
2166 sock->other_ipaddr.af = sock->my_ipaddr.af;
2169 * Don't screw up other people.
2171 #undef proto_for_port
2179 * Allocate & initialize a new listener.
2181 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
2185 this = rad_malloc(sizeof(*this));
2186 memset(this, 0, sizeof(*this));
2189 this->recv = master_listen[this->type].recv;
2190 this->send = master_listen[this->type].send;
2191 this->print = master_listen[this->type].print;
2192 this->encode = master_listen[this->type].encode;
2193 this->decode = master_listen[this->type].decode;
2197 case RAD_LISTEN_NONE:
2199 case RAD_LISTEN_AUTH:
2200 #ifdef WITH_ACCOUNTING
2201 case RAD_LISTEN_ACCT:
2204 case RAD_LISTEN_PROXY:
2207 case RAD_LISTEN_VQP:
2210 case RAD_LISTEN_COA:
2212 this->data = rad_malloc(sizeof(listen_socket_t));
2213 memset(this->data, 0, sizeof(listen_socket_t));
2217 case RAD_LISTEN_DHCP:
2218 this->data = rad_malloc(sizeof(dhcp_socket_t));
2219 memset(this->data, 0, sizeof(dhcp_socket_t));
2224 case RAD_LISTEN_DETAIL:
2229 #ifdef WITH_COMMAND_SOCKET
2230 case RAD_LISTEN_COMMAND:
2231 this->data = rad_malloc(sizeof(fr_command_socket_t));
2232 memset(this->data, 0, sizeof(fr_command_socket_t));
2237 rad_assert("Unsupported option!" == NULL);
2246 * Externally visible function for creating a new proxy LISTENER.
2248 * Not thread-safe, but all calls to it are protected by the
2249 * proxy mutex in event.c
2251 int proxy_new_listener(home_server *home, int src_port)
2254 listen_socket_t *sock;
2256 if (!home) return 0;
2258 if ((home->max_connections > 0) &&
2259 (home->num_connections >= home->max_connections)) {
2260 DEBUG("WARNING: Home server has too many open connections (%d)",
2261 home->max_connections);
2265 this = listen_alloc(RAD_LISTEN_PROXY);
2268 sock->other_ipaddr = home->ipaddr;
2269 sock->other_port = home->port;
2272 sock->my_ipaddr = home->src_ipaddr;
2273 sock->my_port = src_port;
2274 sock->proto = home->proto;
2277 sock->last_packet = time(NULL);
2279 if (home->proto == IPPROTO_TCP) {
2280 this->recv = proxy_socket_tcp_recv;
2283 * FIXME: connect() is blocking!
2284 * We do this with the proxy mutex locked, which may
2285 * cause large delays!
2287 * http://www.developerweb.net/forum/showthread.php?p=13486
2289 this->fd = fr_tcp_client_socket(&home->src_ipaddr,
2290 &home->ipaddr, home->port);
2293 this->fd = fr_socket(&home->src_ipaddr, src_port);
2296 DEBUG("Failed opening client socket: %s", fr_strerror());
2302 * Figure out which port we were bound to.
2304 if (sock->my_port == 0) {
2305 struct sockaddr_storage src;
2306 socklen_t sizeof_src = sizeof(src);
2308 memset(&src, 0, sizeof_src);
2309 if (getsockname(this->fd, (struct sockaddr *) &src,
2311 radlog(L_ERR, "Failed getting socket name: %s",
2317 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2318 &sock->my_ipaddr, &sock->my_port)) {
2319 radlog(L_ERR, "Socket has unsupported address family");
2326 * Tell the event loop that we have a new FD
2328 if (!event_new_fd(this)) {
2338 static const FR_NAME_NUMBER listen_compare[] = {
2340 { "status", RAD_LISTEN_NONE },
2342 { "auth", RAD_LISTEN_AUTH },
2343 #ifdef WITH_ACCOUNTING
2344 { "acct", RAD_LISTEN_ACCT },
2347 { "detail", RAD_LISTEN_DETAIL },
2350 { "proxy", RAD_LISTEN_PROXY },
2353 { "vmps", RAD_LISTEN_VQP },
2356 { "dhcp", RAD_LISTEN_DHCP },
2358 #ifdef WITH_COMMAND_SOCKET
2359 { "control", RAD_LISTEN_COMMAND },
2362 { "coa", RAD_LISTEN_COA },
2368 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
2376 cf_log_info(cs, "listen {");
2378 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
2380 if (rcode < 0) return NULL;
2383 cf_log_err(cf_sectiontoitem(cs),
2384 "No type specified in listen section");
2388 type = fr_str2int(listen_compare, listen_type, -1);
2390 cf_log_err(cf_sectiontoitem(cs),
2391 "Invalid type \"%s\" in listen section.",
2399 * Allow listen sections in the default config to
2400 * refer to a server.
2403 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
2405 if (rcode == 1) { /* compatiblity with 2.0-pre */
2406 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
2409 if (rcode < 0) return NULL;
2414 * We were passed a virtual server, so the caller is
2415 * defining a proxy listener inside of a virtual server.
2416 * This isn't allowed right now.
2418 else if (type == RAD_LISTEN_PROXY) {
2419 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
2425 * Set up cross-type data.
2427 this = listen_alloc(type);
2428 this->server = server;
2432 * Call per-type parser.
2434 if (master_listen[type].parse(cs, this) < 0) {
2439 cf_log_info(cs, "}");
2445 * Generate a list of listeners. Takes an input list of
2446 * listeners, too, so we don't close sockets with waiting packets.
2448 int listen_init(CONF_SECTION *config, rad_listen_t **head)
2450 int override = FALSE;
2452 CONF_SECTION *cs = NULL;
2453 rad_listen_t **last;
2455 fr_ipaddr_t server_ipaddr;
2458 int defined_proxy = 0;
2462 * We shouldn't be called with a pre-existing list.
2464 rad_assert(head && (*head == NULL));
2467 server_ipaddr.af = AF_UNSPEC;
2470 * If the port is specified on the command-line,
2471 * it over-rides the configuration file.
2473 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
2475 if (mainconfig.port >= 0) auth_port = mainconfig.port;
2478 * If the IP address was configured on the command-line,
2479 * use that as the "bind_address"
2481 if (mainconfig.myip.af != AF_UNSPEC) {
2482 memcpy(&server_ipaddr, &mainconfig.myip,
2483 sizeof(server_ipaddr));
2489 * Else look for bind_address and/or listen sections.
2491 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
2492 rcode = cf_item_parse(config, "bind_address",
2494 &server_ipaddr.ipaddr.ip4addr, NULL);
2495 if (rcode < 0) return -1; /* error parsing it */
2497 if (rcode == 0) { /* successfully parsed IPv4 */
2498 listen_socket_t *sock;
2499 server_ipaddr.af = AF_INET;
2501 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'.");
2505 if (strcmp(progname, "vmpsd") == 0) {
2506 this = listen_alloc(RAD_LISTEN_VQP);
2507 if (!auth_port) auth_port = 1589;
2510 this = listen_alloc(RAD_LISTEN_AUTH);
2514 sock->my_ipaddr = server_ipaddr;
2515 sock->my_port = auth_port;
2517 sock->clients = clients_parse_section(config);
2518 if (!sock->clients) {
2519 cf_log_err(cf_sectiontoitem(config),
2520 "Failed to find any clients for this listen section");
2525 if (listen_bind(this) < 0) {
2527 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
2531 auth_port = sock->my_port; /* may have been updated in listen_bind */
2533 cs = cf_section_sub_find_name2(config, "server",
2535 if (cs) this->server = mainconfig.name;
2539 last = &(this->next);
2545 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
2548 #ifdef WITH_ACCOUNTING
2550 * Open Accounting Socket.
2552 * If we haven't already gotten acct_port from
2553 * /etc/services, then make it auth_port + 1.
2555 this = listen_alloc(RAD_LISTEN_ACCT);
2559 * Create the accounting socket.
2561 * The accounting port is always the
2562 * authentication port + 1
2564 sock->my_ipaddr = server_ipaddr;
2565 sock->my_port = auth_port + 1;
2567 sock->clients = clients_parse_section(config);
2568 if (!sock->clients) {
2569 cf_log_err(cf_sectiontoitem(config),
2570 "Failed to find any clients for this listen section");
2574 if (listen_bind(this) < 0) {
2577 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
2582 cs = cf_section_sub_find_name2(config, "server",
2584 if (cs) this->server = mainconfig.name;
2588 last = &(this->next);
2590 } else if (mainconfig.port > 0) { /* no bind address, but a port */
2591 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
2597 * They specified an IP on the command-line, ignore
2598 * all listen sections except the one in '-n'.
2600 if (mainconfig.myip.af != AF_UNSPEC) {
2601 CONF_SECTION *subcs;
2602 const char *name2 = cf_section_name2(cs);
2604 cs = cf_section_sub_find_name2(config, "server",
2606 if (!cs) goto add_sockets;
2609 * Should really abstract this code...
2611 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2613 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2614 this = listen_parse(subcs, name2);
2621 last = &(this->next);
2622 } /* loop over "listen" directives in server <foo> */
2628 * Walk through the "listen" sections, if they exist.
2630 for (cs = cf_subsection_find_next(config, NULL, "listen");
2632 cs = cf_subsection_find_next(config, cs, "listen")) {
2633 this = listen_parse(cs, NULL);
2640 last = &(this->next);
2644 * Check virtual servers for "listen" sections, too.
2646 * FIXME: Move to virtual server init?
2648 for (cs = cf_subsection_find_next(config, NULL, "server");
2650 cs = cf_subsection_find_next(config, cs, "server")) {
2651 CONF_SECTION *subcs;
2652 const char *name2 = cf_section_name2(cs);
2654 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2656 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2657 this = listen_parse(subcs, name2);
2664 last = &(this->next);
2665 } /* loop over "listen" directives in virtual servers */
2666 } /* loop over virtual servers */
2670 * Print out which sockets we're listening on, and
2671 * add them to the event list.
2673 for (this = *head; this != NULL; this = this->next) {
2675 if (this->type == RAD_LISTEN_PROXY) {
2684 * If we're proxying requests, open the proxy FD.
2685 * Otherwise, don't do anything.
2688 if ((mainconfig.proxy_requests == TRUE) &&
2689 (*head != NULL) && !defined_proxy) {
2690 listen_socket_t *sock = NULL;
2694 memset(&home, 0, sizeof(home));
2699 home.proto = IPPROTO_UDP;
2700 home.src_ipaddr = server_ipaddr;
2703 * Find the first authentication port,
2706 for (this = *head; this != NULL; this = this->next) {
2707 if (this->type == RAD_LISTEN_AUTH) {
2709 if (home.src_ipaddr.af == AF_UNSPEC) {
2710 home.src_ipaddr = sock->my_ipaddr;
2712 port = sock->my_port + 2;
2716 if (this->type == RAD_LISTEN_ACCT) {
2718 if (home.src_ipaddr.af == AF_UNSPEC) {
2719 home.src_ipaddr = sock->my_ipaddr;
2721 port = sock->my_port + 1;
2728 * Address is still unspecified, use IPv4.
2730 if (home.src_ipaddr.af == AF_UNSPEC) {
2731 home.src_ipaddr.af = AF_INET;
2732 /* everything else is already set to zero */
2735 home.ipaddr.af = home.src_ipaddr.af;
2736 /* everything else is already set to zero */
2738 if (!proxy_new_listener(&home, port)) {
2746 * Haven't defined any sockets. Die.
2748 if (!*head) return -1;
2755 * Free a linked list of listeners;
2757 void listen_free(rad_listen_t **head)
2761 if (!head || !*head) return;
2765 rad_listen_t *next = this->next;
2768 * Other code may have eaten the FD.
2770 if (this->fd >= 0) close(this->fd);
2772 if (master_listen[this->type].free) {
2773 master_listen[this->type].free(this);
2777 if ((this->type == RAD_LISTEN_AUTH) ||
2779 (this->type == RAD_LISTEN_ACCT) ||
2782 (this->type == RAD_LISTEN_PROXY)
2785 listen_socket_t *sock = this->data;
2786 rad_free(&sock->packet);
2800 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
2805 for (this = mainconfig.listen; this != NULL; this = this->next) {
2806 listen_socket_t *sock;
2808 if ((this->type != RAD_LISTEN_AUTH) &&
2809 (this->type != RAD_LISTEN_ACCT)) continue;
2813 if ((sock->my_port == port) &&
2814 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
2815 return sock->clients;
2823 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
2827 for (this = mainconfig.listen; this != NULL; this = this->next) {
2828 listen_socket_t *sock;
2831 * FIXME: For TCP, ignore the *secondary*
2832 * listeners associated with the main socket.
2834 if ((this->type != RAD_LISTEN_AUTH) &&
2835 (this->type != RAD_LISTEN_ACCT)) continue;
2839 if ((sock->my_port == port) &&
2840 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
2844 if ((sock->my_port == port) &&
2845 ((sock->my_ipaddr.af == AF_INET) &&
2846 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == INADDR_ANY))) {
2850 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2851 if ((sock->my_port == port) &&
2852 (sock->my_ipaddr.af == AF_INET6) &&
2853 (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr))) {