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.
564 if (errno == EWOULDBLOCK) {
568 DEBUG2(" ... failed to accept connection.");
572 if (!fr_sockaddr2ipaddr(&src, salen, &src_ipaddr, &src_port)) {
573 DEBUG2(" ... unknown address family.");
578 * Enforce client IP address checks on accept, not on
581 if ((client = client_listener_find(listener,
582 &src_ipaddr, src_port)) == NULL) {
584 FR_STATS_INC(auth, total_invalid_requests);
589 * Enforce max_connectionsx on client && listen section.
591 if ((client->max_connections != 0) &&
592 (client->max_connections == client->num_connections)) {
594 * FIXME: Print client IP/port, and server IP/port.
596 radlog(L_INFO, "Ignoring new connection due to client max_connections (%d)", client->max_connections);
601 sock = listener->data;
602 if ((sock->max_connections != 0) &&
603 (sock->max_connections == sock->num_connections)) {
605 * FIXME: Print client IP/port, and server IP/port.
607 radlog(L_INFO, "Ignoring new connection due to socket max_connections");
611 client->num_connections++;
612 sock->num_connections++;
615 * Add the new listener.
617 this = listen_alloc(listener->type);
618 if (!this) return -1;
621 * Copy everything, including the pointer to the socket
625 memcpy(this->data, listener->data, sizeof(*sock));
626 memcpy(this, listener, sizeof(*this));
628 this->data = sock; /* fix it back */
630 sock->parent = listener->data;
631 sock->other_ipaddr = src_ipaddr;
632 sock->other_port = src_port;
633 sock->client = client;
636 this->status = RAD_LISTEN_STATUS_INIT;
637 this->recv = dual_tcp_recv;
641 this->recv = dual_tls_recv;
642 this->send = dual_tls_send;
647 * FIXME: set O_NONBLOCK on the accept'd fd.
648 * See djb's portability rants for details.
652 * Tell the event loop that we have a new FD.
653 * This can be called from a child thread...
663 * This function is stupid and complicated.
665 static int socket_print(const rad_listen_t *this, char *buffer, size_t bufsize)
668 listen_socket_t *sock = this->data;
671 switch (this->type) {
673 case RAD_LISTEN_NONE: /* what a hack... */
678 case RAD_LISTEN_AUTH:
679 name = "authentication";
682 #ifdef WITH_ACCOUNTING
683 case RAD_LISTEN_ACCT:
689 case RAD_LISTEN_PROXY:
701 case RAD_LISTEN_DHCP:
717 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
718 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
722 if (sock->interface) {
723 ADDSTRING(" interface ");
724 ADDSTRING(sock->interface);
728 if (this->recv == dual_tcp_accept) {
729 ADDSTRING(" proto tcp");
735 * TCP sockets get printed a little differently, to make
736 * it clear what's going on.
739 ADDSTRING(" from client (");
740 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
744 snprintf(buffer, bufsize, "%d", sock->other_port);
748 if ((sock->my_ipaddr.af == AF_INET) &&
749 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
750 strlcpy(buffer, "*", bufsize);
752 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
757 snprintf(buffer, bufsize, "%d", sock->my_port);
761 ADDSTRING(", virtual-server=");
762 ADDSTRING(this->server);
772 * Maybe it's a socket that we opened to a home server.
774 if ((sock->proto == IPPROTO_TCP) &&
775 (this->type == RAD_LISTEN_PROXY)) {
777 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
781 snprintf(buffer, bufsize, "%d", sock->my_port);
783 ADDSTRING(") -> home_server (");
785 if ((sock->other_ipaddr.af == AF_INET) &&
786 (sock->other_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
787 strlcpy(buffer, "*", bufsize);
789 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
794 snprintf(buffer, bufsize, "%d", sock->other_port);
801 #endif /* WITH_PROXY */
802 #endif /* WITH_TCP */
804 ADDSTRING(" address ");
806 if ((sock->my_ipaddr.af == AF_INET) &&
807 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
808 strlcpy(buffer, "*", bufsize);
810 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
815 snprintf(buffer, bufsize, "%d", sock->my_port);
824 ADDSTRING(" as server ");
825 ADDSTRING(this->server);
834 extern int check_config; /* radiusd.c */
838 * Parse an authentication or accounting socket.
840 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
845 listen_socket_t *sock = this->data;
846 char *section_name = NULL;
847 CONF_SECTION *client_cs, *parentcs;
854 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
855 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
856 &ipaddr.ipaddr.ip4addr, NULL);
857 if (rcode < 0) return -1;
859 if (rcode == 0) { /* successfully parsed IPv4 */
862 } else { /* maybe IPv6? */
863 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
864 &ipaddr.ipaddr.ip6addr, NULL);
865 if (rcode < 0) return -1;
868 cf_log_err(cf_sectiontoitem(cs),
869 "No address specified in listen section");
872 ipaddr.af = AF_INET6;
875 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
877 if (rcode < 0) return -1;
879 if ((listen_port < 0) || (listen_port > 65535)) {
880 cf_log_err(cf_sectiontoitem(cs),
881 "Invalid value for \"port\"");
885 sock->proto = IPPROTO_UDP;
887 if (cf_pair_find(cs, "proto")) {
889 cf_log_err(cf_sectiontoitem(cs),
890 "System does not support the TCP protocol. Delete this line from the configuration file.");
898 rcode = cf_item_parse(cs, "proto", PW_TYPE_STRING_PTR,
900 if (rcode < 0) return -1;
902 if (strcmp(proto, "udp") == 0) {
903 sock->proto = IPPROTO_UDP;
905 } else if (strcmp(proto, "tcp") == 0) {
906 sock->proto = IPPROTO_TCP;
908 rcode = cf_item_parse(cs, "max_connections", PW_TYPE_INTEGER,
909 &sock->max_connections, "64");
910 if (rcode < 0) return -1;
913 cf_log_err(cf_sectiontoitem(cs),
914 "Unknown proto name \"%s\"", proto);
921 * TCP requires a destination IP for sockets.
922 * UDP doesn't, so it's allowed.
925 if ((this->type == RAD_LISTEN_PROXY) &&
926 (sock->proto != IPPROTO_UDP)) {
927 cf_log_err(cf_sectiontoitem(cs),
928 "Proxy listeners can only listen on proto = udp");
931 #endif /* WITH_PROXY */
934 tls = cf_section_sub_find(cs, "tls");
937 * Don't allow TLS configurations for UDP sockets.
939 if (sock->proto != IPPROTO_TCP) {
940 cf_log_err(cf_sectiontoitem(cs),
941 "TLS transport is not available for UDP sockets.");
947 * FIXME: Make this better.
949 if (listen_port == 0) listen_port = 2083;
951 this->tls = tls_server_conf_parse(tls);
956 #ifdef HAVE_PTRHEAD_H
957 if (pthread_mutex_init(&sock->mutex, NULL) < 0) {
967 * Built without TLS. Disallow it.
969 if (cf_section_sub_find(cs, "tls")) {
970 cf_log_err(cf_sectiontoitem(cs),
971 "TLS transport is not available in this executable.");
974 #endif /* WITH_TLS */
976 #endif /* WITH_TCP */
979 * No "proto" field. Disallow TLS.
981 } else if (cf_section_sub_find(cs, "tls")) {
982 cf_log_err(cf_sectiontoitem(cs),
983 "TLS transport is not available in this \"listen\" section.");
987 sock->my_ipaddr = ipaddr;
988 sock->my_port = listen_port;
992 if (home_server_find(&sock->my_ipaddr, sock->my_port, sock->proto)) {
995 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.",
996 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
1001 return 0; /* don't do anything */
1006 * If we can bind to interfaces, do so,
1009 if (cf_pair_find(cs, "interface")) {
1011 CONF_PAIR *cp = cf_pair_find(cs, "interface");
1013 rad_assert(cp != NULL);
1014 value = cf_pair_value(cp);
1016 cf_log_err(cf_sectiontoitem(cs),
1017 "No interface name given");
1020 sock->interface = value;
1025 * If we can do broadcasts..
1027 if (cf_pair_find(cs, "broadcast")) {
1028 #ifndef SO_BROADCAST
1029 cf_log_err(cf_sectiontoitem(cs),
1030 "System does not support broadcast sockets. Delete this line from the configuration file.");
1034 CONF_PAIR *cp = cf_pair_find(cs, "broadcast");
1036 if (this->type != RAD_LISTEN_DHCP) {
1037 cf_log_err(cf_pairtoitem(cp),
1038 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file.");
1042 rad_assert(cp != NULL);
1043 value = cf_pair_value(cp);
1045 cf_log_err(cf_sectiontoitem(cs),
1046 "No broadcast value given");
1051 * Hack... whatever happened to cf_section_parse?
1053 sock->broadcast = (strcmp(value, "yes") == 0);
1059 * And bind it to the port.
1061 if (listen_bind(this) < 0) {
1063 cf_log_err(cf_sectiontoitem(cs),
1064 "Error binding to port for %s port %d",
1065 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
1072 * Proxy sockets don't have clients.
1074 if (this->type == RAD_LISTEN_PROXY) return 0;
1078 * The more specific configurations are preferred to more
1082 parentcs = cf_top_section(cs);
1083 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
1084 §ion_name, NULL);
1085 if (rcode < 0) return -1; /* bad string */
1088 * Explicit list given: use it.
1090 client_cs = cf_section_sub_find_name2(parentcs,
1094 client_cs = cf_section_find(section_name);
1097 cf_log_err(cf_sectiontoitem(cs),
1098 "Failed to find clients %s {...}",
1104 } /* else there was no "clients = " entry. */
1107 CONF_SECTION *server_cs;
1109 server_cs = cf_section_sub_find_name2(parentcs,
1113 * Found a "server foo" section. If there are clients
1117 (cf_section_sub_find(server_cs, "client") != NULL)) {
1118 client_cs = server_cs;
1123 * Still nothing. Look for global clients.
1125 if (!client_cs) client_cs = parentcs;
1127 sock->clients = clients_parse_section(client_cs);
1128 if (!sock->clients) {
1129 cf_log_err(cf_sectiontoitem(cs),
1130 "Failed to load clients for this listen section");
1135 if (sock->proto == IPPROTO_TCP) {
1137 * Re-write the listener receive function to
1138 * allow us to accept the socket.
1140 this->recv = dual_tcp_accept;
1148 * Send an authentication response packet
1150 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1152 rad_assert(request->listener == listener);
1153 rad_assert(listener->send == auth_socket_send);
1155 if (rad_send(request->reply, request->packet,
1156 request->client->secret) < 0) {
1157 radlog_request(L_ERR, 0, request, "Failed sending reply: %s",
1166 #ifdef WITH_ACCOUNTING
1168 * Send an accounting response packet (or not)
1170 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1172 rad_assert(request->listener == listener);
1173 rad_assert(listener->send == acct_socket_send);
1176 * Accounting reject's are silently dropped.
1178 * We do it here to avoid polluting the rest of the
1179 * code with this knowledge
1181 if (request->reply->code == 0) return 0;
1183 if (rad_send(request->reply, request->packet,
1184 request->client->secret) < 0) {
1185 radlog_request(L_ERR, 0, request, "Failed sending reply: %s",
1196 * Send a packet to a home server.
1198 * FIXME: have different code for proxy auth & acct!
1200 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1202 rad_assert(request->proxy_listener == listener);
1203 rad_assert(listener->send == proxy_socket_send);
1205 if (rad_send(request->proxy, request->packet,
1206 request->home_server->secret) < 0) {
1207 radlog_request(L_ERR, 0, request, "Failed sending proxied request: %s",
1218 * Check if an incoming request is "ok"
1220 * It takes packets, not requests. It sees if the packet looks
1221 * OK. If so, it does a number of sanity checks on it.
1223 static int stats_socket_recv(rad_listen_t *listener)
1227 RADIUS_PACKET *packet;
1228 RADCLIENT *client = NULL;
1229 fr_ipaddr_t src_ipaddr;
1231 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1232 if (rcode < 0) return 0;
1234 FR_STATS_INC(auth, total_requests);
1236 if (rcode < 20) { /* AUTH_HDR_LEN */
1237 FR_STATS_INC(auth, total_malformed_requests);
1241 if ((client = client_listener_find(listener,
1242 &src_ipaddr, src_port)) == NULL) {
1243 rad_recv_discard(listener->fd);
1244 FR_STATS_INC(auth, total_invalid_requests);
1248 FR_STATS_TYPE_INC(client->auth->total_requests);
1251 * We only understand Status-Server on this socket.
1253 if (code != PW_STATUS_SERVER) {
1254 DEBUG("Ignoring packet code %d sent to Status-Server port",
1256 rad_recv_discard(listener->fd);
1257 FR_STATS_INC(auth, total_unknown_types);
1262 * Now that we've sanity checked everything, receive the
1265 packet = rad_recv(listener->fd, 1); /* require message authenticator */
1267 FR_STATS_INC(auth, total_malformed_requests);
1268 DEBUG("%s", fr_strerror());
1272 if (!request_receive(listener, packet, client, rad_status_server)) {
1273 FR_STATS_INC(auth, total_packets_dropped);
1284 * Check if an incoming request is "ok"
1286 * It takes packets, not requests. It sees if the packet looks
1287 * OK. If so, it does a number of sanity checks on it.
1289 static int auth_socket_recv(rad_listen_t *listener)
1293 RADIUS_PACKET *packet;
1294 RAD_REQUEST_FUNP fun = NULL;
1295 RADCLIENT *client = NULL;
1296 fr_ipaddr_t src_ipaddr;
1298 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1299 if (rcode < 0) return 0;
1301 FR_STATS_INC(auth, total_requests);
1303 if (rcode < 20) { /* AUTH_HDR_LEN */
1304 FR_STATS_INC(auth, total_malformed_requests);
1308 if ((client = client_listener_find(listener,
1309 &src_ipaddr, src_port)) == NULL) {
1310 rad_recv_discard(listener->fd);
1311 FR_STATS_INC(auth, total_invalid_requests);
1315 FR_STATS_TYPE_INC(client->auth->total_requests);
1318 * Some sanity checks, based on the packet code.
1321 case PW_AUTHENTICATION_REQUEST:
1322 fun = rad_authenticate;
1325 case PW_STATUS_SERVER:
1326 if (!mainconfig.status_server) {
1327 rad_recv_discard(listener->fd);
1328 FR_STATS_INC(auth, total_unknown_types);
1329 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1332 fun = rad_status_server;
1336 rad_recv_discard(listener->fd);
1337 FR_STATS_INC(auth,total_unknown_types);
1339 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
1340 code, client->shortname, src_port);
1343 } /* switch over packet types */
1346 * Now that we've sanity checked everything, receive the
1349 packet = rad_recv(listener->fd, client->message_authenticator);
1351 FR_STATS_INC(auth, total_malformed_requests);
1352 DEBUG("%s", fr_strerror());
1356 if (!request_receive(listener, packet, client, fun)) {
1357 FR_STATS_INC(auth, total_packets_dropped);
1366 #ifdef WITH_ACCOUNTING
1368 * Receive packets from an accounting socket
1370 static int acct_socket_recv(rad_listen_t *listener)
1374 RADIUS_PACKET *packet;
1375 RAD_REQUEST_FUNP fun = NULL;
1376 RADCLIENT *client = NULL;
1377 fr_ipaddr_t src_ipaddr;
1379 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1380 if (rcode < 0) return 0;
1382 FR_STATS_INC(acct, total_requests);
1384 if (rcode < 20) { /* AUTH_HDR_LEN */
1385 FR_STATS_INC(acct, total_malformed_requests);
1389 if ((client = client_listener_find(listener,
1390 &src_ipaddr, src_port)) == NULL) {
1391 rad_recv_discard(listener->fd);
1392 FR_STATS_INC(acct, total_invalid_requests);
1396 FR_STATS_TYPE_INC(client->acct->total_requests);
1399 * Some sanity checks, based on the packet code.
1402 case PW_ACCOUNTING_REQUEST:
1403 fun = rad_accounting;
1406 case PW_STATUS_SERVER:
1407 if (!mainconfig.status_server) {
1408 rad_recv_discard(listener->fd);
1409 FR_STATS_INC(acct, total_unknown_types);
1411 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1414 fun = rad_status_server;
1418 rad_recv_discard(listener->fd);
1419 FR_STATS_INC(acct, total_unknown_types);
1421 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1422 code, client->shortname, src_port);
1424 } /* switch over packet types */
1427 * Now that we've sanity checked everything, receive the
1430 packet = rad_recv(listener->fd, 0);
1432 FR_STATS_INC(acct, total_malformed_requests);
1433 radlog(L_ERR, "%s", fr_strerror());
1438 * There can be no duplicate accounting packets.
1440 if (!request_receive(listener, packet, client, fun)) {
1441 FR_STATS_INC(acct, total_packets_dropped);
1452 static int do_proxy(REQUEST *request)
1456 if (request->in_proxy_hash ||
1457 (request->proxy_reply && (request->proxy_reply->code != 0))) {
1461 vp = pairfind(request->config_items, PW_HOME_SERVER_POOL, 0);
1464 if (!home_pool_byname(vp->vp_strvalue, HOME_TYPE_COA)) {
1465 RDEBUG2("ERROR: Cannot proxy to unknown pool %s",
1474 * Receive a CoA packet.
1476 static int rad_coa_recv(REQUEST *request)
1478 int rcode = RLM_MODULE_OK;
1483 * Get the correct response
1485 switch (request->packet->code) {
1486 case PW_COA_REQUEST:
1491 case PW_DISCONNECT_REQUEST:
1492 ack = PW_DISCONNECT_ACK;
1493 nak = PW_DISCONNECT_NAK;
1496 default: /* shouldn't happen */
1497 return RLM_MODULE_FAIL;
1501 #define WAS_PROXIED (request->proxy)
1503 #define WAS_PROXIED (0)
1508 * RFC 5176 Section 3.3. If we have a CoA-Request
1509 * with Service-Type = Authorize-Only, it MUST
1510 * have a State attribute in it.
1512 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE, 0);
1513 if (request->packet->code == PW_COA_REQUEST) {
1514 if (vp && (vp->vp_integer == 17)) {
1515 vp = pairfind(request->packet->vps, PW_STATE, 0);
1516 if (!vp || (vp->length == 0)) {
1517 RDEBUG("ERROR: CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1518 request->reply->code = PW_COA_NAK;
1519 return RLM_MODULE_FAIL;
1524 * RFC 5176, Section 3.2.
1526 RDEBUG("ERROR: Disconnect-Request MUST NOT contain a Service-Type attribute");
1527 request->reply->code = PW_DISCONNECT_NAK;
1528 return RLM_MODULE_FAIL;
1531 rcode = module_recv_coa(0, request);
1533 case RLM_MODULE_FAIL:
1534 case RLM_MODULE_INVALID:
1535 case RLM_MODULE_REJECT:
1536 case RLM_MODULE_USERLOCK:
1538 request->reply->code = nak;
1541 case RLM_MODULE_HANDLED:
1544 case RLM_MODULE_NOOP:
1545 case RLM_MODULE_NOTFOUND:
1547 case RLM_MODULE_UPDATED:
1548 if (do_proxy(request)) return RLM_MODULE_OK;
1549 request->reply->code = ack;
1552 } else if (request->proxy_reply) {
1554 * Start the reply code with the proxy reply
1557 request->reply->code = request->proxy_reply->code;
1561 * Copy State from the request to the reply.
1562 * See RFC 5176 Section 3.3.
1564 vp = paircopy2(request->packet->vps, PW_STATE, 0);
1565 if (vp) pairadd(&request->reply->vps, vp);
1568 * We may want to over-ride the reply.
1570 if (request->reply->code) {
1571 rcode = module_send_coa(0, request);
1574 * We need to send CoA-NAK back if Service-Type
1575 * is Authorize-Only. Rely on the user's policy
1576 * to do that. We're not a real NAS, so this
1577 * restriction doesn't (ahem) apply to us.
1579 case RLM_MODULE_FAIL:
1580 case RLM_MODULE_INVALID:
1581 case RLM_MODULE_REJECT:
1582 case RLM_MODULE_USERLOCK:
1585 * Over-ride an ACK with a NAK
1587 request->reply->code = nak;
1590 case RLM_MODULE_HANDLED:
1593 case RLM_MODULE_NOOP:
1594 case RLM_MODULE_NOTFOUND:
1596 case RLM_MODULE_UPDATED:
1598 * Do NOT over-ride a previously set value.
1599 * Otherwise an "ok" here will re-write a
1602 if (request->reply->code == 0) {
1603 request->reply->code = ack;
1609 return RLM_MODULE_OK;
1614 * Check if an incoming request is "ok"
1616 * It takes packets, not requests. It sees if the packet looks
1617 * OK. If so, it does a number of sanity checks on it.
1619 static int coa_socket_recv(rad_listen_t *listener)
1623 RADIUS_PACKET *packet;
1624 RAD_REQUEST_FUNP fun = NULL;
1625 RADCLIENT *client = NULL;
1626 fr_ipaddr_t src_ipaddr;
1628 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1629 if (rcode < 0) return 0;
1631 if (rcode < 20) { /* AUTH_HDR_LEN */
1632 FR_STATS_INC(coa, total_requests);
1633 FR_STATS_INC(coa, total_malformed_requests);
1637 if ((client = client_listener_find(listener,
1638 &src_ipaddr, src_port)) == NULL) {
1639 rad_recv_discard(listener->fd);
1640 FR_STATS_INC(coa, total_requests);
1641 FR_STATS_INC(coa, total_invalid_requests);
1646 * Some sanity checks, based on the packet code.
1649 case PW_COA_REQUEST:
1650 FR_STATS_INC(coa, total_requests);
1654 case PW_DISCONNECT_REQUEST:
1655 FR_STATS_INC(dsc, total_requests);
1660 rad_recv_discard(listener->fd);
1661 FR_STATS_INC(coa, total_unknown_types);
1662 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1663 code, client->shortname, src_port);
1665 } /* switch over packet types */
1668 * Now that we've sanity checked everything, receive the
1671 packet = rad_recv(listener->fd, client->message_authenticator);
1673 FR_STATS_INC(coa, total_malformed_requests);
1674 DEBUG("%s", fr_strerror());
1678 if (!request_receive(listener, packet, client, fun)) {
1679 FR_STATS_INC(coa, total_packets_dropped);
1690 * Recieve packets from a proxy socket.
1692 static int proxy_socket_recv(rad_listen_t *listener)
1694 RADIUS_PACKET *packet;
1697 packet = rad_recv(listener->fd, 0);
1699 radlog(L_ERR, "%s", fr_strerror());
1704 * FIXME: Client MIB updates?
1706 switch(packet->code) {
1707 case PW_AUTHENTICATION_ACK:
1708 case PW_ACCESS_CHALLENGE:
1709 case PW_AUTHENTICATION_REJECT:
1712 #ifdef WITH_ACCOUNTING
1713 case PW_ACCOUNTING_RESPONSE:
1718 case PW_DISCONNECT_ACK:
1719 case PW_DISCONNECT_NAK:
1727 * FIXME: Update MIB for packet types?
1729 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1730 "from home server %s port %d - ID %d : IGNORED",
1732 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1733 packet->src_port, packet->id);
1738 if (!request_proxy_reply(packet)) {
1748 * Recieve packets from a proxy socket.
1750 static int proxy_socket_tcp_recv(rad_listen_t *listener)
1752 RADIUS_PACKET *packet;
1753 listen_socket_t *sock = listener->data;
1756 packet = fr_tcp_recv(listener->fd, 0);
1758 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
1759 event_new_fd(listener);
1764 * FIXME: Client MIB updates?
1766 switch(packet->code) {
1767 case PW_AUTHENTICATION_ACK:
1768 case PW_ACCESS_CHALLENGE:
1769 case PW_AUTHENTICATION_REJECT:
1772 #ifdef WITH_ACCOUNTING
1773 case PW_ACCOUNTING_RESPONSE:
1779 * FIXME: Update MIB for packet types?
1781 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1782 "from home server %s port %d - ID %d : IGNORED",
1784 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1785 packet->src_port, packet->id);
1790 packet->src_ipaddr = sock->other_ipaddr;
1791 packet->src_port = sock->other_port;
1792 packet->dst_ipaddr = sock->my_ipaddr;
1793 packet->dst_port = sock->my_port;
1796 * FIXME: Have it return an indication of packets that
1797 * are OK to ignore (dups, too late), versus ones that
1798 * aren't OK to ignore (unknown response, spoofed, etc.)
1800 * Close the socket on bad packets...
1802 if (!request_proxy_reply(packet)) {
1807 sock->opened = sock->last_packet = time(NULL);
1815 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1817 if (!request->reply->code) return 0;
1819 if (rad_encode(request->reply, request->packet,
1820 request->client->secret) < 0) {
1821 radlog_request(L_ERR, 0, request, "Failed encoding packet: %s",
1826 if (rad_sign(request->reply, request->packet,
1827 request->client->secret) < 0) {
1828 radlog_request(L_ERR, 0, request, "Failed signing packet: %s",
1837 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1839 if (rad_verify(request->packet, NULL,
1840 request->client->secret) < 0) {
1844 return rad_decode(request->packet, NULL,
1845 request->client->secret);
1849 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1851 if (rad_encode(request->proxy, NULL, request->home_server->secret) < 0) {
1852 radlog_request(L_ERR, 0, request, "Failed encoding proxied packet: %s",
1857 if (rad_sign(request->proxy, NULL, request->home_server->secret) < 0) {
1858 radlog_request(L_ERR, 0, request, "Failed signing proxied packet: %s",
1867 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1870 * rad_verify is run in event.c, received_proxy_response()
1873 return rad_decode(request->proxy_reply, request->proxy,
1874 request->home_server->secret);
1880 #include "command.c"
1882 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1884 { common_socket_parse, NULL,
1885 stats_socket_recv, auth_socket_send,
1886 socket_print, client_socket_encode, client_socket_decode },
1889 * This always gets defined.
1891 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1896 { common_socket_parse, NULL,
1897 proxy_socket_recv, proxy_socket_send,
1898 socket_print, proxy_socket_encode, proxy_socket_decode },
1901 /* authentication */
1902 { common_socket_parse, NULL,
1903 auth_socket_recv, auth_socket_send,
1904 socket_print, client_socket_encode, client_socket_decode },
1906 #ifdef WITH_ACCOUNTING
1908 { common_socket_parse, NULL,
1909 acct_socket_recv, acct_socket_send,
1910 socket_print, client_socket_encode, client_socket_decode},
1915 { detail_parse, detail_free,
1916 detail_recv, detail_send,
1917 detail_print, detail_encode, detail_decode },
1921 /* vlan query protocol */
1922 { common_socket_parse, NULL,
1923 vqp_socket_recv, vqp_socket_send,
1924 socket_print, vqp_socket_encode, vqp_socket_decode },
1928 /* dhcp query protocol */
1929 { dhcp_socket_parse, NULL,
1930 dhcp_socket_recv, dhcp_socket_send,
1931 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1934 #ifdef WITH_COMMAND_SOCKET
1935 /* TCP command socket */
1936 { command_socket_parse, command_socket_free,
1937 command_domain_accept, command_domain_send,
1938 command_socket_print, command_socket_encode, command_socket_decode },
1942 /* Change of Authorization */
1943 { common_socket_parse, NULL,
1944 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
1945 socket_print, client_socket_encode, client_socket_decode },
1953 * Binds a listener to a socket.
1955 static int listen_bind(rad_listen_t *this)
1958 struct sockaddr_storage salocal;
1960 listen_socket_t *sock = this->data;
1962 #define proto_for_port "udp"
1963 #define sock_type SOCK_DGRAM
1965 const char *proto_for_port = "udp";
1966 int sock_type = SOCK_DGRAM;
1968 if (sock->proto == IPPROTO_TCP) {
1970 if (this->type == RAD_LISTEN_VQP) {
1971 radlog(L_ERR, "VQP does not support TCP transport");
1976 proto_for_port = "tcp";
1977 sock_type = SOCK_STREAM;
1982 * If the port is zero, then it means the appropriate
1983 * thing from /etc/services.
1985 if (sock->my_port == 0) {
1986 struct servent *svp;
1988 switch (this->type) {
1989 case RAD_LISTEN_AUTH:
1990 svp = getservbyname ("radius", proto_for_port);
1992 sock->my_port = ntohs(svp->s_port);
1994 sock->my_port = PW_AUTH_UDP_PORT;
1998 #ifdef WITH_ACCOUNTING
1999 case RAD_LISTEN_ACCT:
2000 svp = getservbyname ("radacct", proto_for_port);
2002 sock->my_port = ntohs(svp->s_port);
2004 sock->my_port = PW_ACCT_UDP_PORT;
2010 case RAD_LISTEN_PROXY:
2011 /* leave it at zero */
2016 case RAD_LISTEN_VQP:
2017 sock->my_port = 1589;
2022 case RAD_LISTEN_COA:
2023 svp = getservbyname ("radius-dynauth", "udp");
2025 sock->my_port = ntohs(svp->s_port);
2027 sock->my_port = PW_COA_UDP_PORT;
2033 DEBUG("WARNING: Internal sanity check failed in binding to socket. Ignoring problem.");
2039 * Copy fr_socket() here, as we may need to bind to a device.
2041 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
2045 this->print(this, buffer, sizeof(buffer));
2047 radlog(L_ERR, "Failed opening %s: %s", buffer, strerror(errno));
2052 * Bind to a device BEFORE touching IP addresses.
2054 if (sock->interface) {
2055 #ifdef SO_BINDTODEVICE
2057 strcpy(ifreq.ifr_name, sock->interface);
2060 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
2061 (char *)&ifreq, sizeof(ifreq));
2065 radlog(L_ERR, "Failed binding to interface %s: %s",
2066 sock->interface, strerror(errno));
2068 } /* else it worked. */
2070 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2071 #ifdef HAVE_NET_IF_H
2073 * Odds are that any system supporting "bind to
2074 * device" also supports IPv6, so this next bit
2075 * isn't necessary. But it's here for
2078 * If we're doing IPv6, and the scope hasn't yet
2079 * been defined, set the scope to the scope of
2082 if (sock->my_ipaddr.af == AF_INET6) {
2083 if (sock->my_ipaddr.scope == 0) {
2084 sock->my_ipaddr.scope = if_nametoindex(sock->interface);
2085 if (sock->my_ipaddr.scope == 0) {
2087 radlog(L_ERR, "Failed finding interface %s: %s",
2088 sock->interface, strerror(errno));
2091 } /* else scope was defined: we're OK. */
2096 * IPv4: no link local addresses,
2097 * and no bind to device.
2101 radlog(L_ERR, "Failed binding to interface %s: \"bind to device\" is unsupported", sock->interface);
2108 if (sock->proto == IPPROTO_TCP) {
2111 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2113 radlog(L_ERR, "Failed to reuse address: %s", strerror(errno));
2119 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
2120 else /* UDP sockets get UDPfromto */
2123 #ifdef WITH_UDPFROMTO
2125 * Initialize udpfromto for all sockets.
2127 if (udpfromto_init(this->fd) != 0) {
2128 radlog(L_ERR, "Failed initializing udpfromto: %s",
2136 * Set up sockaddr stuff.
2138 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
2143 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2144 if (sock->my_ipaddr.af == AF_INET6) {
2146 * Listening on '::' does NOT get you IPv4 to
2147 * IPv6 mapping. You've got to listen on an IPv4
2148 * address, too. This makes the rest of the server
2149 * design a little simpler.
2153 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2156 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2157 (char *)&on, sizeof(on));
2159 #endif /* IPV6_V6ONLY */
2161 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2163 if (sock->my_ipaddr.af == AF_INET) {
2166 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2168 * Disable PMTU discovery. On Linux, this
2169 * also makes sure that the "don't fragment"
2172 flag = IP_PMTUDISC_DONT;
2173 setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2174 &flag, sizeof(flag));
2177 #if defined(IP_DONTFRAG)
2179 * Ensure that the "don't fragment" flag is zero.
2182 setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2183 &flag, sizeof(flag));
2189 if (sock->broadcast) {
2192 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2193 radlog(L_ERR, "Can't set broadcast option: %s\n",
2202 * May be binding to priviledged ports.
2204 if (sock->my_port != 0) {
2208 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2209 radlog(L_ERR, "Can't set re-use address option: %s\n",
2216 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2222 this->print(this, buffer, sizeof(buffer));
2223 radlog(L_ERR, "Failed binding to %s: %s\n",
2224 buffer, strerror(errno));
2229 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2230 * kernel instead binds us to a 1.2.3.4. If this
2231 * happens, notice, and remember our real IP.
2234 struct sockaddr_storage src;
2235 socklen_t sizeof_src = sizeof(src);
2237 memset(&src, 0, sizeof_src);
2238 if (getsockname(this->fd, (struct sockaddr *) &src,
2240 radlog(L_ERR, "Failed getting socket name: %s",
2245 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2246 &sock->my_ipaddr, &sock->my_port)) {
2247 radlog(L_ERR, "Socket has unsupported address family");
2254 if (sock->proto == IPPROTO_TCP) {
2255 if (listen(this->fd, 8) < 0) {
2257 radlog(L_ERR, "Failed in listen(): %s", strerror(errno));
2263 if (fr_nonblock(this->fd) < 0) {
2265 radlog(L_ERR, "Failed setting non-blocking on socket: %s",
2271 * Mostly for proxy sockets.
2273 sock->other_ipaddr.af = sock->my_ipaddr.af;
2276 * Don't screw up other people.
2278 #undef proto_for_port
2286 * Allocate & initialize a new listener.
2288 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
2292 this = rad_malloc(sizeof(*this));
2293 memset(this, 0, sizeof(*this));
2296 this->recv = master_listen[this->type].recv;
2297 this->send = master_listen[this->type].send;
2298 this->print = master_listen[this->type].print;
2299 this->encode = master_listen[this->type].encode;
2300 this->decode = master_listen[this->type].decode;
2304 case RAD_LISTEN_NONE:
2306 case RAD_LISTEN_AUTH:
2307 #ifdef WITH_ACCOUNTING
2308 case RAD_LISTEN_ACCT:
2311 case RAD_LISTEN_PROXY:
2314 case RAD_LISTEN_VQP:
2317 case RAD_LISTEN_COA:
2319 this->data = rad_malloc(sizeof(listen_socket_t));
2320 memset(this->data, 0, sizeof(listen_socket_t));
2324 case RAD_LISTEN_DHCP:
2325 this->data = rad_malloc(sizeof(dhcp_socket_t));
2326 memset(this->data, 0, sizeof(dhcp_socket_t));
2331 case RAD_LISTEN_DETAIL:
2336 #ifdef WITH_COMMAND_SOCKET
2337 case RAD_LISTEN_COMMAND:
2338 this->data = rad_malloc(sizeof(fr_command_socket_t));
2339 memset(this->data, 0, sizeof(fr_command_socket_t));
2344 rad_assert("Unsupported option!" == NULL);
2353 * Externally visible function for creating a new proxy LISTENER.
2355 * Not thread-safe, but all calls to it are protected by the
2356 * proxy mutex in event.c
2358 int proxy_new_listener(home_server *home, int src_port)
2361 listen_socket_t *sock;
2366 if (!home) return 0;
2368 if ((home->max_connections > 0) &&
2369 (home->num_connections >= home->max_connections)) {
2370 DEBUG("WARNING: Home server has too many open connections (%d)",
2371 home->max_connections);
2375 this = listen_alloc(RAD_LISTEN_PROXY);
2378 sock->other_ipaddr = home->ipaddr;
2379 sock->other_port = home->port;
2382 sock->my_ipaddr = home->src_ipaddr;
2383 sock->my_port = src_port;
2384 sock->proto = home->proto;
2386 if (debug_flag >= 2) {
2387 this->print(this, buffer, sizeof(buffer));
2388 DEBUG("Opening new %s", buffer);
2392 sock->opened = sock->last_packet = time(NULL);
2394 if (home->proto == IPPROTO_TCP) {
2395 this->recv = proxy_socket_tcp_recv;
2398 * FIXME: connect() is blocking!
2399 * We do this with the proxy mutex locked, which may
2400 * cause large delays!
2402 * http://www.developerweb.net/forum/showthread.php?p=13486
2404 this->fd = fr_tcp_client_socket(&home->src_ipaddr,
2405 &home->ipaddr, home->port);
2408 DEBUG("Trying SSL to port %d\n", home->port);
2409 sock->ssn = tls_new_client_session(home->tls, this->fd);
2415 this->recv = proxy_tls_recv;
2416 this->send = proxy_tls_send;
2421 this->fd = fr_socket(&home->src_ipaddr, src_port);
2424 this->print(this, buffer,sizeof(buffer));
2425 DEBUG("Failed opening client socket ::%s:: : %s",
2426 buffer, fr_strerror());
2432 * Figure out which port we were bound to.
2434 if (sock->my_port == 0) {
2435 struct sockaddr_storage src;
2436 socklen_t sizeof_src = sizeof(src);
2438 memset(&src, 0, sizeof_src);
2439 if (getsockname(this->fd, (struct sockaddr *) &src,
2441 radlog(L_ERR, "Failed getting socket name: %s",
2447 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2448 &sock->my_ipaddr, &sock->my_port)) {
2449 radlog(L_ERR, "Socket has unsupported address family");
2456 * Tell the event loop that we have a new FD
2458 if (!event_new_fd(this)) {
2468 static const FR_NAME_NUMBER listen_compare[] = {
2470 { "status", RAD_LISTEN_NONE },
2472 { "auth", RAD_LISTEN_AUTH },
2473 #ifdef WITH_ACCOUNTING
2474 { "acct", RAD_LISTEN_ACCT },
2477 { "detail", RAD_LISTEN_DETAIL },
2480 { "proxy", RAD_LISTEN_PROXY },
2483 { "vmps", RAD_LISTEN_VQP },
2486 { "dhcp", RAD_LISTEN_DHCP },
2488 #ifdef WITH_COMMAND_SOCKET
2489 { "control", RAD_LISTEN_COMMAND },
2492 { "coa", RAD_LISTEN_COA },
2498 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
2506 cf_log_info(cs, "listen {");
2508 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
2510 if (rcode < 0) return NULL;
2513 cf_log_err(cf_sectiontoitem(cs),
2514 "No type specified in listen section");
2518 type = fr_str2int(listen_compare, listen_type, -1);
2520 cf_log_err(cf_sectiontoitem(cs),
2521 "Invalid type \"%s\" in listen section.",
2529 * Allow listen sections in the default config to
2530 * refer to a server.
2533 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
2535 if (rcode == 1) { /* compatiblity with 2.0-pre */
2536 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
2539 if (rcode < 0) return NULL;
2544 * We were passed a virtual server, so the caller is
2545 * defining a proxy listener inside of a virtual server.
2546 * This isn't allowed right now.
2548 else if (type == RAD_LISTEN_PROXY) {
2549 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
2555 * Set up cross-type data.
2557 this = listen_alloc(type);
2558 this->server = server;
2562 * Call per-type parser.
2564 if (master_listen[type].parse(cs, this) < 0) {
2569 cf_log_info(cs, "}");
2575 static int is_loopback(const fr_ipaddr_t *ipaddr)
2578 * We shouldn't proxy on loopback.
2580 if ((ipaddr->af == AF_INET) &&
2581 (ipaddr->ipaddr.ip4addr.s_addr == htonl(INADDR_LOOPBACK))) {
2585 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2586 if ((ipaddr->af == AF_INET6) &&
2587 (IN6_IS_ADDR_LINKLOCAL(&ipaddr->ipaddr.ip6addr))) {
2597 * Generate a list of listeners. Takes an input list of
2598 * listeners, too, so we don't close sockets with waiting packets.
2600 int listen_init(CONF_SECTION *config, rad_listen_t **head, int spawn_flag)
2602 int override = FALSE;
2604 CONF_SECTION *cs = NULL;
2605 rad_listen_t **last;
2607 fr_ipaddr_t server_ipaddr;
2610 int defined_proxy = 0;
2614 * We shouldn't be called with a pre-existing list.
2616 rad_assert(head && (*head == NULL));
2619 server_ipaddr.af = AF_UNSPEC;
2622 * If the port is specified on the command-line,
2623 * it over-rides the configuration file.
2625 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
2627 if (mainconfig.port >= 0) auth_port = mainconfig.port;
2630 * If the IP address was configured on the command-line,
2631 * use that as the "bind_address"
2633 if (mainconfig.myip.af != AF_UNSPEC) {
2634 memcpy(&server_ipaddr, &mainconfig.myip,
2635 sizeof(server_ipaddr));
2641 * Else look for bind_address and/or listen sections.
2643 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
2644 rcode = cf_item_parse(config, "bind_address",
2646 &server_ipaddr.ipaddr.ip4addr, NULL);
2647 if (rcode < 0) return -1; /* error parsing it */
2649 if (rcode == 0) { /* successfully parsed IPv4 */
2650 listen_socket_t *sock;
2651 server_ipaddr.af = AF_INET;
2653 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'.");
2657 if (strcmp(progname, "vmpsd") == 0) {
2658 this = listen_alloc(RAD_LISTEN_VQP);
2659 if (!auth_port) auth_port = 1589;
2662 this = listen_alloc(RAD_LISTEN_AUTH);
2666 sock->my_ipaddr = server_ipaddr;
2667 sock->my_port = auth_port;
2669 sock->clients = clients_parse_section(config);
2670 if (!sock->clients) {
2671 cf_log_err(cf_sectiontoitem(config),
2672 "Failed to find any clients for this listen section");
2677 if (listen_bind(this) < 0) {
2679 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
2683 auth_port = sock->my_port; /* may have been updated in listen_bind */
2685 cs = cf_section_sub_find_name2(config, "server",
2687 if (cs) this->server = mainconfig.name;
2691 if (!spawn_flag && this->tls) {
2693 cf_log_err(cf_sectiontoitem(cs), "Threading must be enabled for TLS sockets to function properly.");
2700 last = &(this->next);
2706 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
2709 #ifdef WITH_ACCOUNTING
2711 * Open Accounting Socket.
2713 * If we haven't already gotten acct_port from
2714 * /etc/services, then make it auth_port + 1.
2716 this = listen_alloc(RAD_LISTEN_ACCT);
2720 * Create the accounting socket.
2722 * The accounting port is always the
2723 * authentication port + 1
2725 sock->my_ipaddr = server_ipaddr;
2726 sock->my_port = auth_port + 1;
2728 sock->clients = clients_parse_section(config);
2729 if (!sock->clients) {
2730 cf_log_err(cf_sectiontoitem(config),
2731 "Failed to find any clients for this listen section");
2735 if (listen_bind(this) < 0) {
2738 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
2743 cs = cf_section_sub_find_name2(config, "server",
2745 if (cs) this->server = mainconfig.name;
2749 last = &(this->next);
2751 } else if (mainconfig.port > 0) { /* no bind address, but a port */
2752 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
2758 * They specified an IP on the command-line, ignore
2759 * all listen sections except the one in '-n'.
2761 if (mainconfig.myip.af != AF_UNSPEC) {
2762 CONF_SECTION *subcs;
2763 const char *name2 = cf_section_name2(cs);
2765 cs = cf_section_sub_find_name2(config, "server",
2767 if (!cs) goto add_sockets;
2770 * Should really abstract this code...
2772 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2774 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2775 this = listen_parse(subcs, name2);
2782 if (!spawn_flag && this->tls) goto tls_error;
2786 last = &(this->next);
2787 } /* loop over "listen" directives in server <foo> */
2793 * Walk through the "listen" sections, if they exist.
2795 for (cs = cf_subsection_find_next(config, NULL, "listen");
2797 cs = cf_subsection_find_next(config, cs, "listen")) {
2798 this = listen_parse(cs, NULL);
2805 last = &(this->next);
2809 * Check virtual servers for "listen" sections, too.
2811 * FIXME: Move to virtual server init?
2813 for (cs = cf_subsection_find_next(config, NULL, "server");
2815 cs = cf_subsection_find_next(config, cs, "server")) {
2816 CONF_SECTION *subcs;
2817 const char *name2 = cf_section_name2(cs);
2819 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2821 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2822 this = listen_parse(subcs, name2);
2829 last = &(this->next);
2830 } /* loop over "listen" directives in virtual servers */
2831 } /* loop over virtual servers */
2835 * No sockets to receive packets, this is an error.
2836 * proxying is pointless.
2839 radlog(L_ERR, "The server is not configured to listen on any ports. Cannot start.");
2844 * Print out which sockets we're listening on, and
2845 * add them to the event list.
2847 for (this = *head; this != NULL; this = this->next) {
2849 if (this->type == RAD_LISTEN_PROXY) {
2858 * If we're proxying requests, open the proxy FD.
2859 * Otherwise, don't do anything.
2862 if ((mainconfig.proxy_requests == TRUE) &&
2863 (*head != NULL) && !defined_proxy) {
2864 listen_socket_t *sock = NULL;
2868 memset(&home, 0, sizeof(home));
2873 home.proto = IPPROTO_UDP;
2874 home.src_ipaddr = server_ipaddr;
2877 * Find the first authentication port,
2880 for (this = *head; this != NULL; this = this->next) {
2881 if (this->type == RAD_LISTEN_AUTH) {
2884 if (is_loopback(&sock->my_ipaddr)) continue;
2886 if (home.src_ipaddr.af == AF_UNSPEC) {
2887 home.src_ipaddr = sock->my_ipaddr;
2889 port = sock->my_port + 2;
2893 if (this->type == RAD_LISTEN_ACCT) {
2896 if (is_loopback(&sock->my_ipaddr)) continue;
2898 if (home.src_ipaddr.af == AF_UNSPEC) {
2899 home.src_ipaddr = sock->my_ipaddr;
2901 port = sock->my_port + 1;
2908 * Address is still unspecified, use IPv4.
2910 if (home.src_ipaddr.af == AF_UNSPEC) {
2911 home.src_ipaddr.af = AF_INET;
2912 /* everything else is already set to zero */
2915 home.ipaddr.af = home.src_ipaddr.af;
2916 /* everything else is already set to zero */
2918 if (!proxy_new_listener(&home, port)) {
2926 * Haven't defined any sockets. Die.
2928 if (!*head) return -1;
2930 xlat_register("listen", xlat_listen, NULL);
2936 * Free a linked list of listeners;
2938 void listen_free(rad_listen_t **head)
2942 if (!head || !*head) return;
2946 rad_listen_t *next = this->next;
2949 * Other code may have eaten the FD.
2951 if (this->fd >= 0) close(this->fd);
2953 if (master_listen[this->type].free) {
2954 master_listen[this->type].free(this);
2958 if (this->tls) tls_server_conf_free(this->tls);
2962 if ((this->type == RAD_LISTEN_AUTH)
2964 || (this->type == RAD_LISTEN_ACCT)
2967 || (this->type == RAD_LISTEN_PROXY)
2970 listen_socket_t *sock = this->data;
2973 if (sock->request) {
2974 pthread_mutex_destroy(&(sock->mutex));
2975 request_free(&sock->request);
2976 sock->packet = NULL;
2978 if (sock->ssn) session_free(sock->ssn);
2979 request_free(&sock->request);
2982 rad_free(&sock->packet);
2985 #endif /* WITH_TCP */
2997 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
3002 for (this = mainconfig.listen; this != NULL; this = this->next) {
3003 listen_socket_t *sock;
3005 if ((this->type != RAD_LISTEN_AUTH)
3006 #ifdef WITH_ACCOUNTING
3007 && (this->type != RAD_LISTEN_ACCT)
3013 if ((sock->my_port == port) &&
3014 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
3015 return sock->clients;
3023 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port, int proto)
3027 for (this = mainconfig.listen; this != NULL; this = this->next) {
3028 listen_socket_t *sock;
3032 if (sock->my_port != port) continue;
3033 if (sock->proto != proto) continue;
3034 if (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) != 0) continue;
3040 * Failed to find a specific one. Find INADDR_ANY
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_inaddr_any(&sock->my_ipaddr)) continue;