2 * listen.c Handle socket stuff
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2005,2006 The FreeRADIUS server project
21 * Copyright 2005 Alan DeKok <aland@ox.org>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29 #include <freeradius-devel/rad_assert.h>
30 #include <freeradius-devel/vqp.h>
31 #include <freeradius-devel/dhcp.h>
32 #include <freeradius-devel/process.h>
34 #include <freeradius-devel/vmps.h>
35 #include <freeradius-devel/detail.h>
38 #include <freeradius-devel/udpfromto.h>
41 #ifdef HAVE_SYS_RESOURCE_H
42 #include <sys/resource.h>
54 static void print_packet(RADIUS_PACKET *packet)
56 char src[256], dst[256];
58 ip_ntoh(&packet->src_ipaddr, src, sizeof(src));
59 ip_ntoh(&packet->dst_ipaddr, dst, sizeof(dst));
61 fprintf(stderr, "ID %d: %s %d -> %s %d\n", packet->id,
62 src, packet->src_port, dst, packet->dst_port);
68 * We'll use this below.
70 typedef int (*rad_listen_parse_t)(CONF_SECTION *, rad_listen_t *);
71 typedef void (*rad_listen_free_t)(rad_listen_t *);
73 typedef struct rad_listen_master_t {
74 rad_listen_parse_t parse;
75 rad_listen_free_t free;
76 rad_listen_recv_t recv;
77 rad_listen_send_t send;
78 rad_listen_print_t print;
79 rad_listen_encode_t encode;
80 rad_listen_decode_t decode;
81 } rad_listen_master_t;
83 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type);
86 * Xlat for %{listen:foo}
88 static size_t xlat_listen(UNUSED void *instance, REQUEST *request,
91 UNUSED RADIUS_ESCAPE_STRING func)
93 const char *value = NULL;
96 if (!fmt || !out || (outlen < 1)) return 0;
98 if (!request || !request->listener) {
103 cp = cf_pair_find(request->listener->cs, fmt);
104 if (!cp || !(value = cf_pair_value(cp))) {
109 strlcpy(out, value, outlen);
115 * Find a per-socket client.
117 RADCLIENT *client_listener_find(rad_listen_t *listener,
118 const fr_ipaddr_t *ipaddr, int src_port)
120 #ifdef WITH_DYNAMIC_CLIENTS
127 RADCLIENT_LIST *clients;
128 listen_socket_t *sock;
130 rad_assert(listener != NULL);
131 rad_assert(ipaddr != NULL);
133 sock = listener->data;
134 clients = sock->clients;
137 * This HAS to have been initialized previously.
139 rad_assert(clients != NULL);
141 client = client_find(clients, ipaddr,sock->proto);
143 char name[256], buffer[128];
145 #ifdef WITH_DYNAMIC_CLIENTS
146 unknown: /* used only for dynamic clients */
150 * DoS attack quenching, but only in daemon mode.
151 * If they're running in debug mode, show them
154 if (debug_flag == 0) {
155 static time_t last_printed = 0;
158 if (last_printed == now) return NULL;
163 listener->print(listener, name, sizeof(name));
165 radlog(L_ERR, "Ignoring request to %s from unknown client %s port %d"
169 , name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
170 buffer, sizeof(buffer)), src_port
172 , (sock->proto == IPPROTO_UDP) ? "udp" : "tcp"
178 #ifndef WITH_DYNAMIC_CLIENTS
179 return client; /* return the found client. */
183 * No server defined, and it's not dynamic. Return it.
185 if (!client->client_server && !client->dynamic) return client;
190 * It's a dynamically generated client, check it.
192 if (client->dynamic && (src_port != 0)) {
194 * Lives forever. Return it.
196 if (client->lifetime == 0) return client;
199 * Rate-limit the deletion of known clients.
200 * This makes them last a little longer, but
201 * prevents the server from melting down if (say)
202 * 10k clients all expire at once.
204 if (now == client->last_new_client) return client;
207 * It's not dead yet. Return it.
209 if ((client->created + client->lifetime) > now) return client;
212 * This really puts them onto a queue for later
215 client_delete(clients, client);
218 * Go find the enclosing network again.
220 client = client_find(clients, ipaddr, sock->proto);
225 if (!client) goto unknown;
226 if (!client->client_server) goto unknown;
229 * At this point, 'client' is the enclosing
230 * network that configures where dynamic clients
233 rad_assert(client->dynamic == 0);
235 } else if (!client->dynamic && client->rate_limit) {
237 * The IP is unknown, so we've found an enclosing
238 * network. Enable DoS protection. We only
239 * allow one new client per second. Known
240 * clients aren't subject to this restriction.
242 if (now == client->last_new_client) goto unknown;
245 client->last_new_client = now;
247 request = request_alloc();
248 if (!request) goto unknown;
250 request->listener = listener;
251 request->client = client;
252 request->packet = rad_recv(listener->fd, 0x02); /* MSG_PEEK */
253 if (!request->packet) { /* badly formed, etc */
254 request_free(&request);
257 request->reply = rad_alloc_reply(request->packet);
258 if (!request->reply) {
259 request_free(&request);
262 gettimeofday(&request->packet->timestamp, NULL);
264 request->priority = listener->type;
265 request->server = client->client_server;
266 request->root = &mainconfig;
269 * Run a fake request through the given virtual server.
270 * Look for FreeRADIUS-Client-IP-Address
271 * FreeRADIUS-Client-Secret
274 * and create the RADCLIENT structure from that.
276 DEBUG("server %s {", request->server);
278 rcode = module_authorize(0, request);
280 DEBUG("} # server %s", request->server);
282 if (rcode != RLM_MODULE_OK) {
283 request_free(&request);
288 * If the client was updated by rlm_dynamic_clients,
289 * don't create the client from attribute-value pairs.
291 if (request->client == client) {
292 created = client_create(clients, request);
294 created = request->client;
297 * This frees the client if it isn't valid.
299 if (!client_validate(clients, client, created)) goto unknown;
302 request->server = client->server;
303 exec_trigger(request, NULL, "server.client.add");
305 request_free(&request);
307 if (!created) goto unknown;
313 static int listen_bind(rad_listen_t *this);
317 * Process and reply to a server-status request.
318 * Like rad_authenticate and rad_accounting this should
319 * live in it's own file but it's so small we don't bother.
321 int rad_status_server(REQUEST *request)
323 int rcode = RLM_MODULE_OK;
326 switch (request->listener->type) {
328 case RAD_LISTEN_NONE:
330 case RAD_LISTEN_AUTH:
331 dval = dict_valbyname(PW_AUTZ_TYPE, 0, "Status-Server");
333 rcode = module_authorize(dval->value, request);
335 rcode = RLM_MODULE_OK;
340 case RLM_MODULE_UPDATED:
341 request->reply->code = PW_AUTHENTICATION_ACK;
344 case RLM_MODULE_FAIL:
345 case RLM_MODULE_HANDLED:
346 request->reply->code = 0; /* don't reply */
350 case RLM_MODULE_REJECT:
351 request->reply->code = PW_AUTHENTICATION_REJECT;
356 #ifdef WITH_ACCOUNTING
357 case RAD_LISTEN_ACCT:
358 dval = dict_valbyname(PW_ACCT_TYPE, 0, "Status-Server");
360 rcode = module_accounting(dval->value, request);
362 rcode = RLM_MODULE_OK;
367 case RLM_MODULE_UPDATED:
368 request->reply->code = PW_ACCOUNTING_RESPONSE;
372 request->reply->code = 0; /* don't reply */
380 * This is a vendor extension. Suggested by Glen
381 * Zorn in IETF 72, and rejected by the rest of
382 * the WG. We like it, so it goes in here.
385 dval = dict_valbyname(PW_RECV_COA_TYPE, 0, "Status-Server");
387 rcode = module_recv_coa(dval->value, request);
389 rcode = RLM_MODULE_OK;
394 case RLM_MODULE_UPDATED:
395 request->reply->code = PW_COA_ACK;
399 request->reply->code = 0; /* don't reply */
411 * Full statistics are available only on a statistics
414 if (request->listener->type == RAD_LISTEN_NONE) {
415 request_stats_reply(request);
423 static int dual_tcp_recv(rad_listen_t *listener)
426 RADIUS_PACKET *packet;
427 RAD_REQUEST_FUNP fun = NULL;
428 listen_socket_t *sock = listener->data;
429 RADCLIENT *client = sock->client;
432 * Allocate a packet for partial reads.
435 sock->packet = rad_alloc(0);
436 if (!sock->packet) return 0;
438 sock->packet->sockfd = listener->fd;
439 sock->packet->src_ipaddr = sock->other_ipaddr;
440 sock->packet->src_port = sock->other_port;
441 sock->packet->dst_ipaddr = sock->my_ipaddr;
442 sock->packet->dst_port = sock->my_port;
446 * Grab the packet currently being processed.
448 packet = sock->packet;
450 rcode = fr_tcp_read_packet(packet, 0);
453 * Still only a partial packet. Put it back, and return,
454 * so that we'll read more data when it's ready.
460 if (rcode == -1) { /* error reading packet */
463 radlog(L_ERR, "Invalid packet from %s port %d: closing socket",
464 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
468 if (rcode < 0) { /* error or connection reset */
469 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
472 * Decrement the number of connections.
474 if (sock->parent->num_connections > 0) {
475 sock->parent->num_connections--;
477 if (sock->client->num_connections > 0) {
478 sock->client->num_connections--;
482 * Tell the event handler that an FD has disappeared.
484 DEBUG("Client has closed connection");
485 event_new_fd(listener);
488 * Do NOT free the listener here. It's in use by
489 * a request, and will need to hang around until
490 * all of the requests are done.
492 * It is instead free'd in remove_from_request_hash()
498 * Some sanity checks, based on the packet code.
500 switch(packet->code) {
501 case PW_AUTHENTICATION_REQUEST:
502 if (listener->type != RAD_LISTEN_AUTH) goto bad_packet;
503 FR_STATS_INC(auth, total_requests);
504 fun = rad_authenticate;
507 #ifdef WITH_ACCOUNTING
508 case PW_ACCOUNTING_REQUEST:
509 if (listener->type != RAD_LISTEN_ACCT) goto bad_packet;
510 FR_STATS_INC(acct, total_requests);
511 fun = rad_accounting;
515 case PW_STATUS_SERVER:
516 if (!mainconfig.status_server) {
517 FR_STATS_INC(auth, total_unknown_types);
518 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
519 rad_free(&sock->packet);
522 fun = rad_status_server;
527 FR_STATS_INC(auth, total_unknown_types);
529 DEBUG("Invalid packet code %d sent from client %s port %d : IGNORED",
530 packet->code, client->shortname, packet->src_port);
531 rad_free(&sock->packet);
533 } /* switch over packet types */
535 if (!request_receive(listener, packet, client, fun)) {
536 FR_STATS_INC(auth, total_packets_dropped);
537 rad_free(&sock->packet);
541 sock->packet = NULL; /* we have no need for more partial reads */
545 static int dual_tcp_accept(rad_listen_t *listener)
550 struct sockaddr_storage src;
551 listen_socket_t *sock;
552 fr_ipaddr_t src_ipaddr;
553 RADCLIENT *client = NULL;
557 DEBUG2(" ... new connection request on TCP socket.");
559 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
562 * Non-blocking sockets must handle this.
565 if (errno == EWOULDBLOCK) {
570 DEBUG2(" ... failed to accept connection.");
574 if (!fr_sockaddr2ipaddr(&src, salen, &src_ipaddr, &src_port)) {
575 DEBUG2(" ... unknown address family.");
580 * Enforce client IP address checks on accept, not on
583 if ((client = client_listener_find(listener,
584 &src_ipaddr, src_port)) == NULL) {
586 FR_STATS_INC(auth, total_invalid_requests);
591 * Enforce max_connectionsx on client && listen section.
593 if ((client->max_connections != 0) &&
594 (client->max_connections == client->num_connections)) {
596 * FIXME: Print client IP/port, and server IP/port.
598 radlog(L_INFO, "Ignoring new connection due to client max_connections (%d)", client->max_connections);
603 sock = listener->data;
604 if ((sock->max_connections != 0) &&
605 (sock->max_connections == sock->num_connections)) {
607 * FIXME: Print client IP/port, and server IP/port.
609 radlog(L_INFO, "Ignoring new connection due to socket max_connections");
613 client->num_connections++;
614 sock->num_connections++;
617 * Add the new listener.
619 this = listen_alloc(listener->type);
620 if (!this) return -1;
623 * Copy everything, including the pointer to the socket
627 memcpy(this->data, listener->data, sizeof(*sock));
628 memcpy(this, listener, sizeof(*this));
630 this->data = sock; /* fix it back */
632 sock->parent = listener->data;
633 sock->other_ipaddr = src_ipaddr;
634 sock->other_port = src_port;
635 sock->client = client;
636 sock->opened = sock->last_packet = time(NULL);
639 this->status = RAD_LISTEN_STATUS_INIT;
640 this->recv = dual_tcp_recv;
644 this->recv = dual_tls_recv;
645 this->send = dual_tls_send;
650 * FIXME: set O_NONBLOCK on the accept'd fd.
651 * See djb's portability rants for details.
655 * Tell the event loop that we have a new FD.
656 * This can be called from a child thread...
666 * This function is stupid and complicated.
668 static int socket_print(const rad_listen_t *this, char *buffer, size_t bufsize)
671 listen_socket_t *sock = this->data;
674 switch (this->type) {
676 case RAD_LISTEN_NONE: /* what a hack... */
681 case RAD_LISTEN_AUTH:
682 name = "authentication";
685 #ifdef WITH_ACCOUNTING
686 case RAD_LISTEN_ACCT:
692 case RAD_LISTEN_PROXY:
704 case RAD_LISTEN_DHCP:
720 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
721 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
725 if (sock->interface) {
726 ADDSTRING(" interface ");
727 ADDSTRING(sock->interface);
731 if (this->recv == dual_tcp_accept) {
732 ADDSTRING(" proto tcp");
738 * TCP sockets get printed a little differently, to make
739 * it clear what's going on.
742 ADDSTRING(" from client (");
743 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
747 snprintf(buffer, bufsize, "%d", sock->other_port);
751 if ((sock->my_ipaddr.af == AF_INET) &&
752 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
753 strlcpy(buffer, "*", bufsize);
755 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
760 snprintf(buffer, bufsize, "%d", sock->my_port);
764 ADDSTRING(", virtual-server=");
765 ADDSTRING(this->server);
775 * Maybe it's a socket that we opened to a home server.
777 if ((sock->proto == IPPROTO_TCP) &&
778 (this->type == RAD_LISTEN_PROXY)) {
780 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
784 snprintf(buffer, bufsize, "%d", sock->my_port);
786 ADDSTRING(") -> home_server (");
788 if ((sock->other_ipaddr.af == AF_INET) &&
789 (sock->other_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
790 strlcpy(buffer, "*", bufsize);
792 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
797 snprintf(buffer, bufsize, "%d", sock->other_port);
804 #endif /* WITH_PROXY */
805 #endif /* WITH_TCP */
807 ADDSTRING(" address ");
809 if ((sock->my_ipaddr.af == AF_INET) &&
810 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
811 strlcpy(buffer, "*", bufsize);
813 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
818 snprintf(buffer, bufsize, "%d", sock->my_port);
829 ADDSTRING(" as server ");
830 ADDSTRING(this->server);
839 extern int check_config; /* radiusd.c */
843 * Parse an authentication or accounting socket.
845 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
850 listen_socket_t *sock = this->data;
851 char *section_name = NULL;
852 CONF_SECTION *client_cs, *parentcs;
859 memset(&ipaddr, 0, sizeof(ipaddr));
860 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
861 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
862 &ipaddr.ipaddr.ip4addr, NULL);
863 if (rcode < 0) return -1;
865 if (rcode == 0) { /* successfully parsed IPv4 */
868 } else { /* maybe IPv6? */
869 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
870 &ipaddr.ipaddr.ip6addr, NULL);
871 if (rcode < 0) return -1;
874 cf_log_err(cf_sectiontoitem(cs),
875 "No address specified in listen section");
878 ipaddr.af = AF_INET6;
881 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
883 if (rcode < 0) return -1;
885 if ((listen_port < 0) || (listen_port > 65535)) {
886 cf_log_err(cf_sectiontoitem(cs),
887 "Invalid value for \"port\"");
891 sock->proto = IPPROTO_UDP;
893 if (cf_pair_find(cs, "proto")) {
895 cf_log_err(cf_sectiontoitem(cs),
896 "System does not support the TCP protocol. Delete this line from the configuration file.");
904 rcode = cf_item_parse(cs, "proto", PW_TYPE_STRING_PTR,
906 if (rcode < 0) return -1;
908 if (strcmp(proto, "udp") == 0) {
909 sock->proto = IPPROTO_UDP;
911 } else if (strcmp(proto, "tcp") == 0) {
912 sock->proto = IPPROTO_TCP;
914 rcode = cf_item_parse(cs, "max_connections", PW_TYPE_INTEGER,
915 &sock->max_connections, "64");
916 if (rcode < 0) return -1;
919 cf_log_err(cf_sectiontoitem(cs),
920 "Unknown proto name \"%s\"", proto);
927 * TCP requires a destination IP for sockets.
928 * UDP doesn't, so it's allowed.
931 if ((this->type == RAD_LISTEN_PROXY) &&
932 (sock->proto != IPPROTO_UDP)) {
933 cf_log_err(cf_sectiontoitem(cs),
934 "Proxy listeners can only listen on proto = udp");
937 #endif /* WITH_PROXY */
940 tls = cf_section_sub_find(cs, "tls");
943 * Don't allow TLS configurations for UDP sockets.
945 if (sock->proto != IPPROTO_TCP) {
946 cf_log_err(cf_sectiontoitem(cs),
947 "TLS transport is not available for UDP sockets.");
953 * FIXME: Make this better.
955 if (listen_port == 0) listen_port = 2083;
957 this->tls = tls_server_conf_parse(tls);
962 #ifdef HAVE_PTRHEAD_H
963 if (pthread_mutex_init(&sock->mutex, NULL) < 0) {
973 * Built without TLS. Disallow it.
975 if (cf_section_sub_find(cs, "tls")) {
976 cf_log_err(cf_sectiontoitem(cs),
977 "TLS transport is not available in this executable.");
980 #endif /* WITH_TLS */
982 #endif /* WITH_TCP */
985 * No "proto" field. Disallow TLS.
987 } else if (cf_section_sub_find(cs, "tls")) {
988 cf_log_err(cf_sectiontoitem(cs),
989 "TLS transport is not available in this \"listen\" section.");
993 sock->my_ipaddr = ipaddr;
994 sock->my_port = listen_port;
998 if (home_server_find(&sock->my_ipaddr, sock->my_port, sock->proto)) {
1001 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.",
1002 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
1007 return 0; /* don't do anything */
1012 * If we can bind to interfaces, do so,
1015 if (cf_pair_find(cs, "interface")) {
1017 CONF_PAIR *cp = cf_pair_find(cs, "interface");
1019 rad_assert(cp != NULL);
1020 value = cf_pair_value(cp);
1022 cf_log_err(cf_sectiontoitem(cs),
1023 "No interface name given");
1026 sock->interface = value;
1031 * If we can do broadcasts..
1033 if (cf_pair_find(cs, "broadcast")) {
1034 #ifndef SO_BROADCAST
1035 cf_log_err(cf_sectiontoitem(cs),
1036 "System does not support broadcast sockets. Delete this line from the configuration file.");
1040 CONF_PAIR *cp = cf_pair_find(cs, "broadcast");
1042 if (this->type != RAD_LISTEN_DHCP) {
1043 cf_log_err(cf_pairtoitem(cp),
1044 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file.");
1048 rad_assert(cp != NULL);
1049 value = cf_pair_value(cp);
1051 cf_log_err(cf_sectiontoitem(cs),
1052 "No broadcast value given");
1057 * Hack... whatever happened to cf_section_parse?
1059 sock->broadcast = (strcmp(value, "yes") == 0);
1065 * And bind it to the port.
1067 if (listen_bind(this) < 0) {
1069 cf_log_err(cf_sectiontoitem(cs),
1070 "Error binding to port for %s port %d",
1071 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
1078 * Proxy sockets don't have clients.
1080 if (this->type == RAD_LISTEN_PROXY) return 0;
1084 * The more specific configurations are preferred to more
1088 parentcs = cf_top_section(cs);
1089 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
1090 §ion_name, NULL);
1091 if (rcode < 0) return -1; /* bad string */
1094 * Explicit list given: use it.
1096 client_cs = cf_section_sub_find_name2(parentcs,
1100 client_cs = cf_section_find(section_name);
1103 cf_log_err(cf_sectiontoitem(cs),
1104 "Failed to find clients %s {...}",
1110 } /* else there was no "clients = " entry. */
1113 CONF_SECTION *server_cs;
1115 server_cs = cf_section_sub_find_name2(parentcs,
1119 * Found a "server foo" section. If there are clients
1123 (cf_section_sub_find(server_cs, "client") != NULL)) {
1124 client_cs = server_cs;
1129 * Still nothing. Look for global clients.
1131 if (!client_cs) client_cs = parentcs;
1133 sock->clients = clients_parse_section(client_cs);
1134 if (!sock->clients) {
1135 cf_log_err(cf_sectiontoitem(cs),
1136 "Failed to load clients for this listen section");
1141 if (sock->proto == IPPROTO_TCP) {
1143 * Re-write the listener receive function to
1144 * allow us to accept the socket.
1146 this->recv = dual_tcp_accept;
1154 * Send an authentication response packet
1156 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1158 rad_assert(request->listener == listener);
1159 rad_assert(listener->send == auth_socket_send);
1161 if (rad_send(request->reply, request->packet,
1162 request->client->secret) < 0) {
1163 radlog_request(L_ERR, 0, request, "Failed sending reply: %s",
1172 #ifdef WITH_ACCOUNTING
1174 * Send an accounting response packet (or not)
1176 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1178 rad_assert(request->listener == listener);
1179 rad_assert(listener->send == acct_socket_send);
1182 * Accounting reject's are silently dropped.
1184 * We do it here to avoid polluting the rest of the
1185 * code with this knowledge
1187 if (request->reply->code == 0) return 0;
1189 if (rad_send(request->reply, request->packet,
1190 request->client->secret) < 0) {
1191 radlog_request(L_ERR, 0, request, "Failed sending reply: %s",
1202 * Send a packet to a home server.
1204 * FIXME: have different code for proxy auth & acct!
1206 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1208 rad_assert(request->proxy_listener == listener);
1209 rad_assert(listener->send == proxy_socket_send);
1211 if (rad_send(request->proxy, NULL,
1212 request->home_server->secret) < 0) {
1213 radlog_request(L_ERR, 0, request, "Failed sending proxied request: %s",
1224 * Check if an incoming request is "ok"
1226 * It takes packets, not requests. It sees if the packet looks
1227 * OK. If so, it does a number of sanity checks on it.
1229 static int stats_socket_recv(rad_listen_t *listener)
1233 RADIUS_PACKET *packet;
1234 RADCLIENT *client = NULL;
1235 fr_ipaddr_t src_ipaddr;
1237 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1238 if (rcode < 0) return 0;
1240 FR_STATS_INC(auth, total_requests);
1242 if (rcode < 20) { /* AUTH_HDR_LEN */
1243 FR_STATS_INC(auth, total_malformed_requests);
1247 if ((client = client_listener_find(listener,
1248 &src_ipaddr, src_port)) == NULL) {
1249 rad_recv_discard(listener->fd);
1250 FR_STATS_INC(auth, total_invalid_requests);
1254 FR_STATS_TYPE_INC(client->auth.total_requests);
1257 * We only understand Status-Server on this socket.
1259 if (code != PW_STATUS_SERVER) {
1260 DEBUG("Ignoring packet code %d sent to Status-Server port",
1262 rad_recv_discard(listener->fd);
1263 FR_STATS_INC(auth, total_unknown_types);
1268 * Now that we've sanity checked everything, receive the
1271 packet = rad_recv(listener->fd, 1); /* require message authenticator */
1273 FR_STATS_INC(auth, total_malformed_requests);
1274 DEBUG("%s", fr_strerror());
1278 if (!request_receive(listener, packet, client, rad_status_server)) {
1279 FR_STATS_INC(auth, total_packets_dropped);
1290 * Check if an incoming request is "ok"
1292 * It takes packets, not requests. It sees if the packet looks
1293 * OK. If so, it does a number of sanity checks on it.
1295 static int auth_socket_recv(rad_listen_t *listener)
1299 RADIUS_PACKET *packet;
1300 RAD_REQUEST_FUNP fun = NULL;
1301 RADCLIENT *client = NULL;
1302 fr_ipaddr_t src_ipaddr;
1304 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1305 if (rcode < 0) return 0;
1307 FR_STATS_INC(auth, total_requests);
1309 if (rcode < 20) { /* AUTH_HDR_LEN */
1310 FR_STATS_INC(auth, total_malformed_requests);
1314 if ((client = client_listener_find(listener,
1315 &src_ipaddr, src_port)) == NULL) {
1316 rad_recv_discard(listener->fd);
1317 FR_STATS_INC(auth, total_invalid_requests);
1321 FR_STATS_TYPE_INC(client->auth.total_requests);
1324 * Some sanity checks, based on the packet code.
1327 case PW_AUTHENTICATION_REQUEST:
1328 fun = rad_authenticate;
1331 case PW_STATUS_SERVER:
1332 if (!mainconfig.status_server) {
1333 rad_recv_discard(listener->fd);
1334 FR_STATS_INC(auth, total_unknown_types);
1335 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1338 fun = rad_status_server;
1342 rad_recv_discard(listener->fd);
1343 FR_STATS_INC(auth,total_unknown_types);
1345 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
1346 code, client->shortname, src_port);
1349 } /* switch over packet types */
1352 * Now that we've sanity checked everything, receive the
1355 packet = rad_recv(listener->fd, client->message_authenticator);
1357 FR_STATS_INC(auth, total_malformed_requests);
1358 DEBUG("%s", fr_strerror());
1362 if (!request_receive(listener, packet, client, fun)) {
1363 FR_STATS_INC(auth, total_packets_dropped);
1372 #ifdef WITH_ACCOUNTING
1374 * Receive packets from an accounting socket
1376 static int acct_socket_recv(rad_listen_t *listener)
1380 RADIUS_PACKET *packet;
1381 RAD_REQUEST_FUNP fun = NULL;
1382 RADCLIENT *client = NULL;
1383 fr_ipaddr_t src_ipaddr;
1385 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1386 if (rcode < 0) return 0;
1388 FR_STATS_INC(acct, total_requests);
1390 if (rcode < 20) { /* AUTH_HDR_LEN */
1391 FR_STATS_INC(acct, total_malformed_requests);
1395 if ((client = client_listener_find(listener,
1396 &src_ipaddr, src_port)) == NULL) {
1397 rad_recv_discard(listener->fd);
1398 FR_STATS_INC(acct, total_invalid_requests);
1402 FR_STATS_TYPE_INC(client->acct.total_requests);
1405 * Some sanity checks, based on the packet code.
1408 case PW_ACCOUNTING_REQUEST:
1409 fun = rad_accounting;
1412 case PW_STATUS_SERVER:
1413 if (!mainconfig.status_server) {
1414 rad_recv_discard(listener->fd);
1415 FR_STATS_INC(acct, total_unknown_types);
1417 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1420 fun = rad_status_server;
1424 rad_recv_discard(listener->fd);
1425 FR_STATS_INC(acct, total_unknown_types);
1427 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1428 code, client->shortname, src_port);
1430 } /* switch over packet types */
1433 * Now that we've sanity checked everything, receive the
1436 packet = rad_recv(listener->fd, 0);
1438 FR_STATS_INC(acct, total_malformed_requests);
1439 radlog(L_ERR, "%s", fr_strerror());
1444 * There can be no duplicate accounting packets.
1446 if (!request_receive(listener, packet, client, fun)) {
1447 FR_STATS_INC(acct, total_packets_dropped);
1458 static int do_proxy(REQUEST *request)
1462 if (request->in_proxy_hash ||
1463 (request->proxy_reply && (request->proxy_reply->code != 0))) {
1467 vp = pairfind(request->config_items, PW_HOME_SERVER_POOL, 0);
1470 if (!home_pool_byname(vp->vp_strvalue, HOME_TYPE_COA)) {
1471 RDEBUG2("ERROR: Cannot proxy to unknown pool %s",
1480 * Receive a CoA packet.
1482 static int rad_coa_recv(REQUEST *request)
1484 int rcode = RLM_MODULE_OK;
1489 * Get the correct response
1491 switch (request->packet->code) {
1492 case PW_COA_REQUEST:
1497 case PW_DISCONNECT_REQUEST:
1498 ack = PW_DISCONNECT_ACK;
1499 nak = PW_DISCONNECT_NAK;
1502 default: /* shouldn't happen */
1503 return RLM_MODULE_FAIL;
1507 #define WAS_PROXIED (request->proxy)
1509 #define WAS_PROXIED (0)
1514 * RFC 5176 Section 3.3. If we have a CoA-Request
1515 * with Service-Type = Authorize-Only, it MUST
1516 * have a State attribute in it.
1518 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE, 0);
1519 if (request->packet->code == PW_COA_REQUEST) {
1520 if (vp && (vp->vp_integer == 17)) {
1521 vp = pairfind(request->packet->vps, PW_STATE, 0);
1522 if (!vp || (vp->length == 0)) {
1523 RDEBUG("ERROR: CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1524 request->reply->code = PW_COA_NAK;
1525 return RLM_MODULE_FAIL;
1530 * RFC 5176, Section 3.2.
1532 RDEBUG("ERROR: Disconnect-Request MUST NOT contain a Service-Type attribute");
1533 request->reply->code = PW_DISCONNECT_NAK;
1534 return RLM_MODULE_FAIL;
1537 rcode = module_recv_coa(0, request);
1539 case RLM_MODULE_FAIL:
1540 case RLM_MODULE_INVALID:
1541 case RLM_MODULE_REJECT:
1542 case RLM_MODULE_USERLOCK:
1544 request->reply->code = nak;
1547 case RLM_MODULE_HANDLED:
1550 case RLM_MODULE_NOOP:
1551 case RLM_MODULE_NOTFOUND:
1553 case RLM_MODULE_UPDATED:
1554 if (do_proxy(request)) return RLM_MODULE_OK;
1555 request->reply->code = ack;
1558 } else if (request->proxy_reply) {
1560 * Start the reply code with the proxy reply
1563 request->reply->code = request->proxy_reply->code;
1567 * Copy State from the request to the reply.
1568 * See RFC 5176 Section 3.3.
1570 vp = paircopy2(request->packet->vps, PW_STATE, 0);
1571 if (vp) pairadd(&request->reply->vps, vp);
1574 * We may want to over-ride the reply.
1576 if (request->reply->code) {
1577 rcode = module_send_coa(0, request);
1580 * We need to send CoA-NAK back if Service-Type
1581 * is Authorize-Only. Rely on the user's policy
1582 * to do that. We're not a real NAS, so this
1583 * restriction doesn't (ahem) apply to us.
1585 case RLM_MODULE_FAIL:
1586 case RLM_MODULE_INVALID:
1587 case RLM_MODULE_REJECT:
1588 case RLM_MODULE_USERLOCK:
1591 * Over-ride an ACK with a NAK
1593 request->reply->code = nak;
1596 case RLM_MODULE_HANDLED:
1599 case RLM_MODULE_NOOP:
1600 case RLM_MODULE_NOTFOUND:
1602 case RLM_MODULE_UPDATED:
1604 * Do NOT over-ride a previously set value.
1605 * Otherwise an "ok" here will re-write a
1608 if (request->reply->code == 0) {
1609 request->reply->code = ack;
1615 return RLM_MODULE_OK;
1620 * Check if an incoming request is "ok"
1622 * It takes packets, not requests. It sees if the packet looks
1623 * OK. If so, it does a number of sanity checks on it.
1625 static int coa_socket_recv(rad_listen_t *listener)
1629 RADIUS_PACKET *packet;
1630 RAD_REQUEST_FUNP fun = NULL;
1631 RADCLIENT *client = NULL;
1632 fr_ipaddr_t src_ipaddr;
1634 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1635 if (rcode < 0) return 0;
1637 if (rcode < 20) { /* AUTH_HDR_LEN */
1638 FR_STATS_INC(coa, total_requests);
1639 FR_STATS_INC(coa, total_malformed_requests);
1643 if ((client = client_listener_find(listener,
1644 &src_ipaddr, src_port)) == NULL) {
1645 rad_recv_discard(listener->fd);
1646 FR_STATS_INC(coa, total_requests);
1647 FR_STATS_INC(coa, total_invalid_requests);
1652 * Some sanity checks, based on the packet code.
1655 case PW_COA_REQUEST:
1656 FR_STATS_INC(coa, total_requests);
1660 case PW_DISCONNECT_REQUEST:
1661 FR_STATS_INC(dsc, total_requests);
1666 rad_recv_discard(listener->fd);
1667 FR_STATS_INC(coa, total_unknown_types);
1668 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1669 code, client->shortname, src_port);
1671 } /* switch over packet types */
1674 * Now that we've sanity checked everything, receive the
1677 packet = rad_recv(listener->fd, client->message_authenticator);
1679 FR_STATS_INC(coa, total_malformed_requests);
1680 DEBUG("%s", fr_strerror());
1684 if (!request_receive(listener, packet, client, fun)) {
1685 FR_STATS_INC(coa, total_packets_dropped);
1696 * Recieve packets from a proxy socket.
1698 static int proxy_socket_recv(rad_listen_t *listener)
1700 RADIUS_PACKET *packet;
1703 packet = rad_recv(listener->fd, 0);
1705 radlog(L_ERR, "%s", fr_strerror());
1710 * FIXME: Client MIB updates?
1712 switch(packet->code) {
1713 case PW_AUTHENTICATION_ACK:
1714 case PW_ACCESS_CHALLENGE:
1715 case PW_AUTHENTICATION_REJECT:
1718 #ifdef WITH_ACCOUNTING
1719 case PW_ACCOUNTING_RESPONSE:
1724 case PW_DISCONNECT_ACK:
1725 case PW_DISCONNECT_NAK:
1733 * FIXME: Update MIB for packet types?
1735 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1736 "from home server %s port %d - ID %d : IGNORED",
1738 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1739 packet->src_port, packet->id);
1744 if (!request_proxy_reply(packet)) {
1754 * Recieve packets from a proxy socket.
1756 static int proxy_socket_tcp_recv(rad_listen_t *listener)
1758 RADIUS_PACKET *packet;
1759 listen_socket_t *sock = listener->data;
1762 packet = fr_tcp_recv(listener->fd, 0);
1764 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
1765 event_new_fd(listener);
1770 * FIXME: Client MIB updates?
1772 switch(packet->code) {
1773 case PW_AUTHENTICATION_ACK:
1774 case PW_ACCESS_CHALLENGE:
1775 case PW_AUTHENTICATION_REJECT:
1778 #ifdef WITH_ACCOUNTING
1779 case PW_ACCOUNTING_RESPONSE:
1785 * FIXME: Update MIB for packet types?
1787 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1788 "from home server %s port %d - ID %d : IGNORED",
1790 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1791 packet->src_port, packet->id);
1796 packet->src_ipaddr = sock->other_ipaddr;
1797 packet->src_port = sock->other_port;
1798 packet->dst_ipaddr = sock->my_ipaddr;
1799 packet->dst_port = sock->my_port;
1802 * FIXME: Have it return an indication of packets that
1803 * are OK to ignore (dups, too late), versus ones that
1804 * aren't OK to ignore (unknown response, spoofed, etc.)
1806 * Close the socket on bad packets...
1808 if (!request_proxy_reply(packet)) {
1813 sock->opened = sock->last_packet = time(NULL);
1821 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1823 if (!request->reply->code) return 0;
1825 if (rad_encode(request->reply, request->packet,
1826 request->client->secret) < 0) {
1827 radlog_request(L_ERR, 0, request, "Failed encoding packet: %s",
1832 if (rad_sign(request->reply, request->packet,
1833 request->client->secret) < 0) {
1834 radlog_request(L_ERR, 0, request, "Failed signing packet: %s",
1843 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1845 if (rad_verify(request->packet, NULL,
1846 request->client->secret) < 0) {
1850 return rad_decode(request->packet, NULL,
1851 request->client->secret);
1855 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1857 if (rad_encode(request->proxy, NULL, request->home_server->secret) < 0) {
1858 radlog_request(L_ERR, 0, request, "Failed encoding proxied packet: %s",
1863 if (rad_sign(request->proxy, NULL, request->home_server->secret) < 0) {
1864 radlog_request(L_ERR, 0, request, "Failed signing proxied packet: %s",
1873 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1876 * rad_verify is run in event.c, received_proxy_response()
1879 return rad_decode(request->proxy_reply, request->proxy,
1880 request->home_server->secret);
1886 #include "command.c"
1888 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1890 { common_socket_parse, NULL,
1891 stats_socket_recv, auth_socket_send,
1892 socket_print, client_socket_encode, client_socket_decode },
1895 * This always gets defined.
1897 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1902 { common_socket_parse, NULL,
1903 proxy_socket_recv, proxy_socket_send,
1904 socket_print, proxy_socket_encode, proxy_socket_decode },
1907 /* authentication */
1908 { common_socket_parse, NULL,
1909 auth_socket_recv, auth_socket_send,
1910 socket_print, client_socket_encode, client_socket_decode },
1912 #ifdef WITH_ACCOUNTING
1914 { common_socket_parse, NULL,
1915 acct_socket_recv, acct_socket_send,
1916 socket_print, client_socket_encode, client_socket_decode},
1921 { detail_parse, detail_free,
1922 detail_recv, detail_send,
1923 detail_print, detail_encode, detail_decode },
1927 /* vlan query protocol */
1928 { common_socket_parse, NULL,
1929 vqp_socket_recv, vqp_socket_send,
1930 socket_print, vqp_socket_encode, vqp_socket_decode },
1934 /* dhcp query protocol */
1935 { dhcp_socket_parse, NULL,
1936 dhcp_socket_recv, dhcp_socket_send,
1937 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1940 #ifdef WITH_COMMAND_SOCKET
1941 /* TCP command socket */
1942 { command_socket_parse, command_socket_free,
1943 command_domain_accept, command_domain_send,
1944 command_socket_print, command_socket_encode, command_socket_decode },
1948 /* Change of Authorization */
1949 { common_socket_parse, NULL,
1950 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
1951 socket_print, client_socket_encode, client_socket_decode },
1959 * Binds a listener to a socket.
1961 static int listen_bind(rad_listen_t *this)
1964 struct sockaddr_storage salocal;
1966 listen_socket_t *sock = this->data;
1968 #define proto_for_port "udp"
1969 #define sock_type SOCK_DGRAM
1971 const char *proto_for_port = "udp";
1972 int sock_type = SOCK_DGRAM;
1974 if (sock->proto == IPPROTO_TCP) {
1976 if (this->type == RAD_LISTEN_VQP) {
1977 radlog(L_ERR, "VQP does not support TCP transport");
1982 proto_for_port = "tcp";
1983 sock_type = SOCK_STREAM;
1988 * If the port is zero, then it means the appropriate
1989 * thing from /etc/services.
1991 if (sock->my_port == 0) {
1992 struct servent *svp;
1994 switch (this->type) {
1995 case RAD_LISTEN_AUTH:
1996 svp = getservbyname ("radius", proto_for_port);
1998 sock->my_port = ntohs(svp->s_port);
2000 sock->my_port = PW_AUTH_UDP_PORT;
2004 #ifdef WITH_ACCOUNTING
2005 case RAD_LISTEN_ACCT:
2006 svp = getservbyname ("radacct", proto_for_port);
2008 sock->my_port = ntohs(svp->s_port);
2010 sock->my_port = PW_ACCT_UDP_PORT;
2016 case RAD_LISTEN_PROXY:
2017 /* leave it at zero */
2022 case RAD_LISTEN_VQP:
2023 sock->my_port = 1589;
2028 case RAD_LISTEN_COA:
2029 svp = getservbyname ("radius-dynauth", "udp");
2031 sock->my_port = ntohs(svp->s_port);
2033 sock->my_port = PW_COA_UDP_PORT;
2039 DEBUG("WARNING: Internal sanity check failed in binding to socket. Ignoring problem.");
2045 * Don't open sockets if we're checking the config.
2053 * Copy fr_socket() here, as we may need to bind to a device.
2055 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
2059 this->print(this, buffer, sizeof(buffer));
2061 radlog(L_ERR, "Failed opening %s: %s", buffer, strerror(errno));
2066 * Bind to a device BEFORE touching IP addresses.
2068 if (sock->interface) {
2069 #ifdef SO_BINDTODEVICE
2072 memset(&ifreq, 0, sizeof(ifreq));
2073 strlcpy(ifreq.ifr_name, sock->interface, sizeof(ifreq.ifr_name));
2076 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
2077 (char *)&ifreq, sizeof(ifreq));
2081 radlog(L_ERR, "Failed binding to interface %s: %s",
2082 sock->interface, strerror(errno));
2084 } /* else it worked. */
2086 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2087 #ifdef HAVE_NET_IF_H
2089 * Odds are that any system supporting "bind to
2090 * device" also supports IPv6, so this next bit
2091 * isn't necessary. But it's here for
2094 * If we're doing IPv6, and the scope hasn't yet
2095 * been defined, set the scope to the scope of
2098 if (sock->my_ipaddr.af == AF_INET6) {
2099 if (sock->my_ipaddr.scope == 0) {
2100 sock->my_ipaddr.scope = if_nametoindex(sock->interface);
2101 if (sock->my_ipaddr.scope == 0) {
2103 radlog(L_ERR, "Failed finding interface %s: %s",
2104 sock->interface, strerror(errno));
2107 } /* else scope was defined: we're OK. */
2112 * IPv4: no link local addresses,
2113 * and no bind to device.
2117 radlog(L_ERR, "Failed binding to interface %s: \"bind to device\" is unsupported", sock->interface);
2124 if (sock->proto == IPPROTO_TCP) {
2127 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2129 radlog(L_ERR, "Failed to reuse address: %s", strerror(errno));
2135 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
2136 else /* UDP sockets get UDPfromto */
2139 #ifdef WITH_UDPFROMTO
2141 * Initialize udpfromto for all sockets.
2143 if (udpfromto_init(this->fd) != 0) {
2144 radlog(L_ERR, "Failed initializing udpfromto: %s",
2152 * Set up sockaddr stuff.
2154 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
2159 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2160 if (sock->my_ipaddr.af == AF_INET6) {
2162 * Listening on '::' does NOT get you IPv4 to
2163 * IPv6 mapping. You've got to listen on an IPv4
2164 * address, too. This makes the rest of the server
2165 * design a little simpler.
2169 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2172 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2173 (char *)&on, sizeof(on));
2175 #endif /* IPV6_V6ONLY */
2177 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2179 if (sock->my_ipaddr.af == AF_INET) {
2182 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2184 * Disable PMTU discovery. On Linux, this
2185 * also makes sure that the "don't fragment"
2188 flag = IP_PMTUDISC_DONT;
2189 setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2190 &flag, sizeof(flag));
2193 #if defined(IP_DONTFRAG)
2195 * Ensure that the "don't fragment" flag is zero.
2198 setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2199 &flag, sizeof(flag));
2205 if (sock->broadcast) {
2208 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2209 radlog(L_ERR, "Can't set broadcast option: %s\n",
2218 * May be binding to priviledged ports.
2220 if (sock->my_port != 0) {
2224 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2225 radlog(L_ERR, "Can't set re-use address option: %s\n",
2232 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2238 this->print(this, buffer, sizeof(buffer));
2239 radlog(L_ERR, "Failed binding to %s: %s\n",
2240 buffer, strerror(errno));
2245 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2246 * kernel instead binds us to a 1.2.3.4. If this
2247 * happens, notice, and remember our real IP.
2250 struct sockaddr_storage src;
2251 socklen_t sizeof_src = sizeof(src);
2253 memset(&src, 0, sizeof_src);
2254 if (getsockname(this->fd, (struct sockaddr *) &src,
2256 radlog(L_ERR, "Failed getting socket name: %s",
2261 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2262 &sock->my_ipaddr, &sock->my_port)) {
2263 radlog(L_ERR, "Socket has unsupported address family");
2270 if (sock->proto == IPPROTO_TCP) {
2271 if (listen(this->fd, 8) < 0) {
2273 radlog(L_ERR, "Failed in listen(): %s", strerror(errno));
2279 if (fr_nonblock(this->fd) < 0) {
2281 radlog(L_ERR, "Failed setting non-blocking on socket: %s",
2287 * Mostly for proxy sockets.
2289 sock->other_ipaddr.af = sock->my_ipaddr.af;
2292 * Don't screw up other people.
2294 #undef proto_for_port
2302 * Allocate & initialize a new listener.
2304 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
2308 this = rad_malloc(sizeof(*this));
2309 memset(this, 0, sizeof(*this));
2312 this->recv = master_listen[this->type].recv;
2313 this->send = master_listen[this->type].send;
2314 this->print = master_listen[this->type].print;
2315 this->encode = master_listen[this->type].encode;
2316 this->decode = master_listen[this->type].decode;
2320 case RAD_LISTEN_NONE:
2322 case RAD_LISTEN_AUTH:
2323 #ifdef WITH_ACCOUNTING
2324 case RAD_LISTEN_ACCT:
2327 case RAD_LISTEN_PROXY:
2330 case RAD_LISTEN_VQP:
2333 case RAD_LISTEN_COA:
2335 this->data = rad_malloc(sizeof(listen_socket_t));
2336 memset(this->data, 0, sizeof(listen_socket_t));
2340 case RAD_LISTEN_DHCP:
2341 this->data = rad_malloc(sizeof(dhcp_socket_t));
2342 memset(this->data, 0, sizeof(dhcp_socket_t));
2347 case RAD_LISTEN_DETAIL:
2352 #ifdef WITH_COMMAND_SOCKET
2353 case RAD_LISTEN_COMMAND:
2354 this->data = rad_malloc(sizeof(fr_command_socket_t));
2355 memset(this->data, 0, sizeof(fr_command_socket_t));
2360 rad_assert("Unsupported option!" == NULL);
2369 * Externally visible function for creating a new proxy LISTENER.
2371 * Not thread-safe, but all calls to it are protected by the
2372 * proxy mutex in event.c
2374 int proxy_new_listener(home_server *home, int src_port)
2377 listen_socket_t *sock;
2382 if (!home) return 0;
2384 if ((home->max_connections > 0) &&
2385 (home->num_connections >= home->max_connections)) {
2386 DEBUG("WARNING: Home server has too many open connections (%d)",
2387 home->max_connections);
2391 this = listen_alloc(RAD_LISTEN_PROXY);
2394 sock->other_ipaddr = home->ipaddr;
2395 sock->other_port = home->port;
2398 sock->my_ipaddr = home->src_ipaddr;
2399 sock->my_port = src_port;
2400 sock->proto = home->proto;
2402 if (debug_flag >= 2) {
2403 this->print(this, buffer, sizeof(buffer));
2404 DEBUG("Opening new %s", buffer);
2408 sock->opened = sock->last_packet = time(NULL);
2410 if (home->proto == IPPROTO_TCP) {
2411 this->recv = proxy_socket_tcp_recv;
2414 * FIXME: connect() is blocking!
2415 * We do this with the proxy mutex locked, which may
2416 * cause large delays!
2418 * http://www.developerweb.net/forum/showthread.php?p=13486
2420 this->fd = fr_tcp_client_socket(&home->src_ipaddr,
2421 &home->ipaddr, home->port);
2424 DEBUG("Trying SSL to port %d\n", home->port);
2425 sock->ssn = tls_new_client_session(home->tls, this->fd);
2431 this->recv = proxy_tls_recv;
2432 this->send = proxy_tls_send;
2437 this->fd = fr_socket(&home->src_ipaddr, src_port);
2440 this->print(this, buffer,sizeof(buffer));
2441 DEBUG("Failed opening client socket ::%s:: : %s",
2442 buffer, fr_strerror());
2448 * Figure out which port we were bound to.
2450 if (sock->my_port == 0) {
2451 struct sockaddr_storage src;
2452 socklen_t sizeof_src = sizeof(src);
2454 memset(&src, 0, sizeof_src);
2455 if (getsockname(this->fd, (struct sockaddr *) &src,
2457 radlog(L_ERR, "Failed getting socket name: %s",
2463 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2464 &sock->my_ipaddr, &sock->my_port)) {
2465 radlog(L_ERR, "Socket has unsupported address family");
2472 * Tell the event loop that we have a new FD
2474 if (!event_new_fd(this)) {
2484 static const FR_NAME_NUMBER listen_compare[] = {
2486 { "status", RAD_LISTEN_NONE },
2488 { "auth", RAD_LISTEN_AUTH },
2489 #ifdef WITH_ACCOUNTING
2490 { "acct", RAD_LISTEN_ACCT },
2493 { "detail", RAD_LISTEN_DETAIL },
2496 { "proxy", RAD_LISTEN_PROXY },
2499 { "vmps", RAD_LISTEN_VQP },
2502 { "dhcp", RAD_LISTEN_DHCP },
2504 #ifdef WITH_COMMAND_SOCKET
2505 { "control", RAD_LISTEN_COMMAND },
2508 { "coa", RAD_LISTEN_COA },
2514 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
2522 cf_log_info(cs, "listen {");
2524 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
2526 if (rcode < 0) return NULL;
2529 cf_log_err(cf_sectiontoitem(cs),
2530 "No type specified in listen section");
2534 type = fr_str2int(listen_compare, listen_type, -1);
2536 cf_log_err(cf_sectiontoitem(cs),
2537 "Invalid type \"%s\" in listen section.",
2545 * Allow listen sections in the default config to
2546 * refer to a server.
2549 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
2551 if (rcode == 1) { /* compatiblity with 2.0-pre */
2552 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
2555 if (rcode < 0) return NULL;
2560 * We were passed a virtual server, so the caller is
2561 * defining a proxy listener inside of a virtual server.
2562 * This isn't allowed right now.
2564 else if (type == RAD_LISTEN_PROXY) {
2565 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
2571 * Set up cross-type data.
2573 this = listen_alloc(type);
2574 this->server = server;
2578 * Call per-type parser.
2580 if (master_listen[type].parse(cs, this) < 0) {
2585 cf_log_info(cs, "}");
2591 static int is_loopback(const fr_ipaddr_t *ipaddr)
2594 * We shouldn't proxy on loopback.
2596 if ((ipaddr->af == AF_INET) &&
2597 (ipaddr->ipaddr.ip4addr.s_addr == htonl(INADDR_LOOPBACK))) {
2601 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2602 if ((ipaddr->af == AF_INET6) &&
2603 (IN6_IS_ADDR_LINKLOCAL(&ipaddr->ipaddr.ip6addr))) {
2613 * Generate a list of listeners. Takes an input list of
2614 * listeners, too, so we don't close sockets with waiting packets.
2616 int listen_init(CONF_SECTION *config, rad_listen_t **head, int spawn_flag)
2618 int override = FALSE;
2620 CONF_SECTION *cs = NULL;
2621 rad_listen_t **last;
2623 fr_ipaddr_t server_ipaddr;
2626 int defined_proxy = 0;
2629 spawn_flag = spawn_flag; /* -Wunused */
2633 * We shouldn't be called with a pre-existing list.
2635 rad_assert(head && (*head == NULL));
2638 server_ipaddr.af = AF_UNSPEC;
2641 * If the port is specified on the command-line,
2642 * it over-rides the configuration file.
2644 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
2646 if (mainconfig.port >= 0) auth_port = mainconfig.port;
2649 * If the IP address was configured on the command-line,
2650 * use that as the "bind_address"
2652 if (mainconfig.myip.af != AF_UNSPEC) {
2653 memcpy(&server_ipaddr, &mainconfig.myip,
2654 sizeof(server_ipaddr));
2660 * Else look for bind_address and/or listen sections.
2662 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
2663 rcode = cf_item_parse(config, "bind_address",
2665 &server_ipaddr.ipaddr.ip4addr, NULL);
2666 if (rcode < 0) return -1; /* error parsing it */
2668 if (rcode == 0) { /* successfully parsed IPv4 */
2669 listen_socket_t *sock;
2670 server_ipaddr.af = AF_INET;
2672 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'.");
2676 if (strcmp(progname, "vmpsd") == 0) {
2677 this = listen_alloc(RAD_LISTEN_VQP);
2678 if (!auth_port) auth_port = 1589;
2681 this = listen_alloc(RAD_LISTEN_AUTH);
2685 sock->my_ipaddr = server_ipaddr;
2686 sock->my_port = auth_port;
2688 sock->clients = clients_parse_section(config);
2689 if (!sock->clients) {
2690 cf_log_err(cf_sectiontoitem(config),
2691 "Failed to find any clients for this listen section");
2696 if (listen_bind(this) < 0) {
2698 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
2702 auth_port = sock->my_port; /* may have been updated in listen_bind */
2704 cs = cf_section_sub_find_name2(config, "server",
2706 if (cs) this->server = mainconfig.name;
2710 last = &(this->next);
2716 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
2719 #ifdef WITH_ACCOUNTING
2721 * Open Accounting Socket.
2723 * If we haven't already gotten acct_port from
2724 * /etc/services, then make it auth_port + 1.
2726 this = listen_alloc(RAD_LISTEN_ACCT);
2730 * Create the accounting socket.
2732 * The accounting port is always the
2733 * authentication port + 1
2735 sock->my_ipaddr = server_ipaddr;
2736 sock->my_port = auth_port + 1;
2738 sock->clients = clients_parse_section(config);
2739 if (!sock->clients) {
2740 cf_log_err(cf_sectiontoitem(config),
2741 "Failed to find any clients for this listen section");
2745 if (listen_bind(this) < 0) {
2748 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
2753 cs = cf_section_sub_find_name2(config, "server",
2755 if (cs) this->server = mainconfig.name;
2759 last = &(this->next);
2761 } else if (mainconfig.port > 0) { /* no bind address, but a port */
2762 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
2768 * They specified an IP on the command-line, ignore
2769 * all listen sections except the one in '-n'.
2771 if (mainconfig.myip.af != AF_UNSPEC) {
2772 CONF_SECTION *subcs;
2773 const char *name2 = cf_section_name2(cs);
2775 cs = cf_section_sub_find_name2(config, "server",
2777 if (!cs) goto add_sockets;
2780 * Should really abstract this code...
2782 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2784 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2785 this = listen_parse(subcs, name2);
2792 last = &(this->next);
2793 } /* loop over "listen" directives in server <foo> */
2799 * Walk through the "listen" sections, if they exist.
2801 for (cs = cf_subsection_find_next(config, NULL, "listen");
2803 cs = cf_subsection_find_next(config, cs, "listen")) {
2804 this = listen_parse(cs, NULL);
2811 last = &(this->next);
2815 * Check virtual servers for "listen" sections, too.
2817 * FIXME: Move to virtual server init?
2819 for (cs = cf_subsection_find_next(config, NULL, "server");
2821 cs = cf_subsection_find_next(config, cs, "server")) {
2822 CONF_SECTION *subcs;
2823 const char *name2 = cf_section_name2(cs);
2825 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2827 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2828 this = listen_parse(subcs, name2);
2835 last = &(this->next);
2836 } /* loop over "listen" directives in virtual servers */
2837 } /* loop over virtual servers */
2841 * No sockets to receive packets, this is an error.
2842 * proxying is pointless.
2845 radlog(L_ERR, "The server is not configured to listen on any ports. Cannot start.");
2850 * Print out which sockets we're listening on, and
2851 * add them to the event list.
2853 for (this = *head; this != NULL; this = this->next) {
2855 if (this->type == RAD_LISTEN_PROXY) {
2862 if (!spawn_flag && this->tls) {
2863 cf_log_err(cf_sectiontoitem(this->cs), "Threading must be enabled for TLS sockets to function properly.");
2864 cf_log_err(cf_sectiontoitem(this->cs), "You probably need to do 'radiusd -fxx -l stdout' for debugging");
2868 if (!check_config) event_new_fd(this);
2872 * If we're proxying requests, open the proxy FD.
2873 * Otherwise, don't do anything.
2876 if ((mainconfig.proxy_requests == TRUE) &&
2878 (*head != NULL) && !defined_proxy) {
2879 listen_socket_t *sock = NULL;
2883 memset(&home, 0, sizeof(home));
2888 home.proto = IPPROTO_UDP;
2889 home.src_ipaddr = server_ipaddr;
2892 * Find the first authentication port,
2895 for (this = *head; this != NULL; this = this->next) {
2896 if (this->type == RAD_LISTEN_AUTH) {
2899 if (is_loopback(&sock->my_ipaddr)) continue;
2901 if (home.src_ipaddr.af == AF_UNSPEC) {
2902 home.src_ipaddr = sock->my_ipaddr;
2904 port = sock->my_port + 2;
2908 if (this->type == RAD_LISTEN_ACCT) {
2911 if (is_loopback(&sock->my_ipaddr)) continue;
2913 if (home.src_ipaddr.af == AF_UNSPEC) {
2914 home.src_ipaddr = sock->my_ipaddr;
2916 port = sock->my_port + 1;
2923 * Address is still unspecified, use IPv4.
2925 if (home.src_ipaddr.af == AF_UNSPEC) {
2926 home.src_ipaddr.af = AF_INET;
2927 /* everything else is already set to zero */
2930 home.ipaddr.af = home.src_ipaddr.af;
2931 /* everything else is already set to zero */
2933 if (!proxy_new_listener(&home, port)) {
2941 * Haven't defined any sockets. Die.
2943 if (!*head) return -1;
2945 xlat_register("listen", xlat_listen, NULL);
2951 * Free a linked list of listeners;
2953 void listen_free(rad_listen_t **head)
2957 if (!head || !*head) return;
2961 rad_listen_t *next = this->next;
2964 * Other code may have eaten the FD.
2966 if (this->fd >= 0) close(this->fd);
2968 if (master_listen[this->type].free) {
2969 master_listen[this->type].free(this);
2973 if (this->tls) tls_server_conf_free(this->tls);
2977 if ((this->type == RAD_LISTEN_AUTH)
2979 || (this->type == RAD_LISTEN_ACCT)
2982 || (this->type == RAD_LISTEN_PROXY)
2985 listen_socket_t *sock = this->data;
2988 if (sock->request) {
2989 pthread_mutex_destroy(&(sock->mutex));
2990 request_free(&sock->request);
2991 sock->packet = NULL;
2993 if (sock->ssn) session_free(sock->ssn);
2994 request_free(&sock->request);
2997 rad_free(&sock->packet);
3000 #endif /* WITH_TCP */
3012 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
3017 for (this = mainconfig.listen; this != NULL; this = this->next) {
3018 listen_socket_t *sock;
3020 if ((this->type != RAD_LISTEN_AUTH)
3021 #ifdef WITH_ACCOUNTING
3022 && (this->type != RAD_LISTEN_ACCT)
3028 if ((sock->my_port == port) &&
3029 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
3030 return sock->clients;
3038 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port, int proto)
3042 for (this = mainconfig.listen; this != NULL; this = this->next) {
3043 listen_socket_t *sock;
3047 if (sock->my_port != port) continue;
3048 if (sock->proto != proto) continue;
3049 if (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) != 0) continue;
3055 * Failed to find a specific one. Find INADDR_ANY
3057 for (this = mainconfig.listen; this != NULL; this = this->next) {
3058 listen_socket_t *sock;
3062 if (sock->my_port != port) continue;
3063 if (sock->proto != proto) continue;
3064 if (!fr_inaddr_any(&sock->my_ipaddr)) continue;