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>
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/modules.h>
28 #include <freeradius-devel/rad_assert.h>
29 #include <freeradius-devel/vqp.h>
30 #include <freeradius-devel/dhcp.h>
31 #include <freeradius-devel/process.h>
33 #include <freeradius-devel/vmps.h>
34 #include <freeradius-devel/detail.h>
37 #include <freeradius-devel/udpfromto.h>
40 #ifdef HAVE_SYS_RESOURCE_H
41 #include <sys/resource.h>
53 #ifdef DEBUG_PRINT_PACKET
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(TALLOC_CTX *ctx, RAD_LISTEN_TYPE type);
85 #ifdef WITH_COMMAND_SOCKET
86 static int command_tcp_recv(rad_listen_t *listener);
87 static int command_tcp_send(rad_listen_t *listener, REQUEST *request);
88 static int command_write_magic(int newfd, listen_socket_t *sock);
92 * Xlat for %{listen:foo}
94 static size_t xlat_listen(UNUSED void *instance, REQUEST *request,
95 const char *fmt, char *out,
98 const char *value = NULL;
101 if (!fmt || !out || (outlen < 1)) return 0;
103 if (!request || !request->listener) {
108 cp = cf_pair_find(request->listener->cs, fmt);
109 if (!cp || !(value = cf_pair_value(cp))) {
114 strlcpy(out, value, outlen);
120 * Find a per-socket client.
122 RADCLIENT *client_listener_find(rad_listen_t *listener,
123 const fr_ipaddr_t *ipaddr, int src_port)
125 #ifdef WITH_DYNAMIC_CLIENTS
132 RADCLIENT_LIST *clients;
133 listen_socket_t *sock;
135 rad_assert(listener != NULL);
136 rad_assert(ipaddr != NULL);
138 sock = listener->data;
139 clients = sock->clients;
142 * This HAS to have been initialized previously.
144 rad_assert(clients != NULL);
146 client = client_find(clients, ipaddr,sock->proto);
148 char name[256], buffer[128];
150 #ifdef WITH_DYNAMIC_CLIENTS
151 unknown: /* used only for dynamic clients */
155 * DoS attack quenching, but only in daemon mode.
156 * If they're running in debug mode, show them
159 if (debug_flag == 0) {
160 static time_t last_printed = 0;
163 if (last_printed == now) return NULL;
168 listener->print(listener, name, sizeof(name));
170 radlog(L_ERR, "Ignoring request to %s from unknown client %s port %d"
174 , name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
175 buffer, sizeof(buffer)), src_port
177 , (sock->proto == IPPROTO_UDP) ? "udp" : "tcp"
183 #ifndef WITH_DYNAMIC_CLIENTS
184 return client; /* return the found client. */
188 * No server defined, and it's not dynamic. Return it.
190 if (!client->client_server && !client->dynamic) return client;
195 * It's a dynamically generated client, check it.
197 if (client->dynamic && (src_port != 0)) {
199 * Lives forever. Return it.
201 if (client->lifetime == 0) return client;
204 * Rate-limit the deletion of known clients.
205 * This makes them last a little longer, but
206 * prevents the server from melting down if (say)
207 * 10k clients all expire at once.
209 if (now == client->last_new_client) return client;
212 * It's not dead yet. Return it.
214 if ((client->created + client->lifetime) > now) return client;
217 * This really puts them onto a queue for later
220 client_delete(clients, client);
223 * Go find the enclosing network again.
225 client = client_find(clients, ipaddr, sock->proto);
230 if (!client) goto unknown;
231 if (!client->client_server) goto unknown;
234 * At this point, 'client' is the enclosing
235 * network that configures where dynamic clients
238 rad_assert(client->dynamic == 0);
240 } else if (!client->dynamic && client->rate_limit) {
242 * The IP is unknown, so we've found an enclosing
243 * network. Enable DoS protection. We only
244 * allow one new client per second. Known
245 * clients aren't subject to this restriction.
247 if (now == client->last_new_client) goto unknown;
250 client->last_new_client = now;
252 request = request_alloc();
253 if (!request) goto unknown;
255 request->listener = listener;
256 request->client = client;
257 request->packet = rad_recv(listener->fd, 0x02); /* MSG_PEEK */
258 if (!request->packet) { /* badly formed, etc */
259 request_free(&request);
262 request->reply = rad_alloc_reply(request, request->packet);
263 if (!request->reply) {
264 request_free(&request);
267 gettimeofday(&request->packet->timestamp, NULL);
269 request->priority = listener->type;
270 request->server = client->client_server;
271 request->root = &mainconfig;
274 * Run a fake request through the given virtual server.
275 * Look for FreeRADIUS-Client-IP-Address
276 * FreeRADIUS-Client-Secret
279 * and create the RADCLIENT structure from that.
281 DEBUG("server %s {", request->server);
283 rcode = process_authorize(0, request);
285 DEBUG("} # server %s", request->server);
287 if (rcode != RLM_MODULE_OK) {
288 request_free(&request);
293 * If the client was updated by rlm_dynamic_clients,
294 * don't create the client from attribute-value pairs.
296 if (request->client == client) {
297 created = client_create(clients, request);
299 created = request->client;
302 * This frees the client if it isn't valid.
304 if (!client_validate(clients, client, created)) goto unknown;
307 request->server = client->server;
308 exec_trigger(request, NULL, "server.client.add", FALSE);
310 request_free(&request);
312 if (!created) goto unknown;
318 static int listen_bind(rad_listen_t *this);
322 * Process and reply to a server-status request.
323 * Like rad_authenticate and rad_accounting this should
324 * live in it's own file but it's so small we don't bother.
326 int rad_status_server(REQUEST *request)
328 int rcode = RLM_MODULE_OK;
331 switch (request->listener->type) {
333 case RAD_LISTEN_NONE:
335 case RAD_LISTEN_AUTH:
336 dval = dict_valbyname(PW_AUTZ_TYPE, 0, "Status-Server");
338 rcode = process_authorize(dval->value, request);
340 rcode = RLM_MODULE_OK;
345 case RLM_MODULE_UPDATED:
346 request->reply->code = PW_AUTHENTICATION_ACK;
349 case RLM_MODULE_FAIL:
350 case RLM_MODULE_HANDLED:
351 request->reply->code = 0; /* don't reply */
355 case RLM_MODULE_REJECT:
356 request->reply->code = PW_AUTHENTICATION_REJECT;
361 #ifdef WITH_ACCOUNTING
362 case RAD_LISTEN_ACCT:
363 dval = dict_valbyname(PW_ACCT_TYPE, 0, "Status-Server");
365 rcode = process_accounting(dval->value, request);
367 rcode = RLM_MODULE_OK;
372 case RLM_MODULE_UPDATED:
373 request->reply->code = PW_ACCOUNTING_RESPONSE;
377 request->reply->code = 0; /* don't reply */
385 * This is a vendor extension. Suggested by Glen
386 * Zorn in IETF 72, and rejected by the rest of
387 * the WG. We like it, so it goes in here.
390 dval = dict_valbyname(PW_RECV_COA_TYPE, 0, "Status-Server");
392 rcode = process_recv_coa(dval->value, request);
394 rcode = RLM_MODULE_OK;
399 case RLM_MODULE_UPDATED:
400 request->reply->code = PW_COA_ACK;
404 request->reply->code = 0; /* don't reply */
416 * Full statistics are available only on a statistics
419 if (request->listener->type == RAD_LISTEN_NONE) {
420 request_stats_reply(request);
428 static int dual_tcp_recv(rad_listen_t *listener)
431 RADIUS_PACKET *packet;
432 RAD_REQUEST_FUNP fun = NULL;
433 listen_socket_t *sock = listener->data;
434 RADCLIENT *client = sock->client;
437 * Allocate a packet for partial reads.
440 sock->packet = rad_alloc(NULL, 0);
441 if (!sock->packet) return 0;
443 sock->packet->sockfd = listener->fd;
444 sock->packet->src_ipaddr = sock->other_ipaddr;
445 sock->packet->src_port = sock->other_port;
446 sock->packet->dst_ipaddr = sock->my_ipaddr;
447 sock->packet->dst_port = sock->my_port;
451 * Grab the packet currently being processed.
453 packet = sock->packet;
455 rcode = fr_tcp_read_packet(packet, 0);
458 * Still only a partial packet. Put it back, and return,
459 * so that we'll read more data when it's ready.
465 if (rcode == -1) { /* error reading packet */
468 radlog(L_ERR, "Invalid packet from %s port %d: closing socket",
469 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
473 if (rcode < 0) { /* error or connection reset */
474 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
477 * Decrement the number of connections.
479 if (sock->parent->limit.num_connections > 0) {
480 sock->parent->limit.num_connections--;
482 if (sock->client->limit.num_connections > 0) {
483 sock->client->limit.num_connections--;
487 * Tell the event handler that an FD has disappeared.
489 DEBUG("Client has closed connection");
490 event_new_fd(listener);
493 * Do NOT free the listener here. It's in use by
494 * a request, and will need to hang around until
495 * all of the requests are done.
497 * It is instead free'd in remove_from_request_hash()
503 * Some sanity checks, based on the packet code.
505 switch(packet->code) {
506 case PW_AUTHENTICATION_REQUEST:
507 if (listener->type != RAD_LISTEN_AUTH) goto bad_packet;
508 FR_STATS_INC(auth, total_requests);
509 fun = rad_authenticate;
512 #ifdef WITH_ACCOUNTING
513 case PW_ACCOUNTING_REQUEST:
514 if (listener->type != RAD_LISTEN_ACCT) goto bad_packet;
515 FR_STATS_INC(acct, total_requests);
516 fun = rad_accounting;
520 case PW_STATUS_SERVER:
521 if (!mainconfig.status_server) {
522 FR_STATS_INC(auth, total_unknown_types);
523 DEBUGW("Ignoring Status-Server request due to security configuration");
524 rad_free(&sock->packet);
527 fun = rad_status_server;
532 FR_STATS_INC(auth, total_unknown_types);
534 DEBUG("Invalid packet code %d sent from client %s port %d : IGNORED",
535 packet->code, client->shortname, packet->src_port);
536 rad_free(&sock->packet);
538 } /* switch over packet types */
540 if (!request_receive(listener, packet, client, fun)) {
541 FR_STATS_INC(auth, total_packets_dropped);
542 rad_free(&sock->packet);
546 sock->packet = NULL; /* we have no need for more partial reads */
550 static int dual_tcp_accept(rad_listen_t *listener)
555 struct sockaddr_storage src;
556 listen_socket_t *sock;
557 fr_ipaddr_t src_ipaddr;
558 RADCLIENT *client = NULL;
562 DEBUG2(" ... new connection request on TCP socket.");
564 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
567 * Non-blocking sockets must handle this.
570 if (errno == EWOULDBLOCK) {
575 DEBUG2(" ... failed to accept connection.");
579 if (!fr_sockaddr2ipaddr(&src, salen, &src_ipaddr, &src_port)) {
581 DEBUG2(" ... unknown address family.");
586 * Enforce client IP address checks on accept, not on
589 if ((client = client_listener_find(listener,
590 &src_ipaddr, src_port)) == NULL) {
592 FR_STATS_INC(auth, total_invalid_requests);
597 * Enforce max_connections on client && listen section.
599 if ((client->limit.max_connections != 0) &&
600 (client->limit.max_connections == client->limit.num_connections)) {
602 * FIXME: Print client IP/port, and server IP/port.
604 radlog(L_INFO, "Ignoring new connection due to client max_connections (%d)", client->limit.max_connections);
609 sock = listener->data;
610 if ((sock->limit.max_connections != 0) &&
611 (sock->limit.max_connections == sock->limit.num_connections)) {
613 * FIXME: Print client IP/port, and server IP/port.
615 radlog(L_INFO, "Ignoring new connection due to socket max_connections");
619 client->limit.num_connections++;
620 sock->limit.num_connections++;
623 * Add the new listener.
625 this = listen_alloc(listener, listener->type);
626 if (!this) return -1;
629 * Copy everything, including the pointer to the socket
633 memcpy(this->data, listener->data, sizeof(*sock));
634 memcpy(this, listener, sizeof(*this));
636 this->data = sock; /* fix it back */
638 sock->parent = listener->data;
639 sock->other_ipaddr = src_ipaddr;
640 sock->other_port = src_port;
641 sock->client = client;
642 sock->opened = sock->last_packet = time(NULL);
645 * Set the limits. The defaults are the parent limits.
646 * Client limits on max_connections are enforced dynamically.
647 * Set the MINIMUM of client/socket idle timeout or lifetime.
649 memcpy(&sock->limit, &sock->parent->limit, sizeof(sock->limit));
651 if (client->limit.idle_timeout &&
652 ((sock->limit.idle_timeout == 0) ||
653 (client->limit.idle_timeout < sock->limit.idle_timeout))) {
654 sock->limit.idle_timeout = client->limit.idle_timeout;
657 if (client->limit.lifetime &&
658 ((sock->limit.lifetime == 0) ||
659 (client->limit.lifetime < sock->limit.lifetime))) {
660 sock->limit.lifetime = client->limit.lifetime;
664 this->status = RAD_LISTEN_STATUS_INIT;
667 #ifdef WITH_COMMAND_SOCKET
668 if (this->type == RAD_LISTEN_COMMAND) {
669 this->recv = command_tcp_recv;
670 this->send = command_tcp_send;
671 command_write_magic(this->fd, sock);
676 this->recv = dual_tcp_recv;
680 this->recv = dual_tls_recv;
681 this->send = dual_tls_send;
687 * FIXME: set O_NONBLOCK on the accept'd fd.
688 * See djb's portability rants for details.
692 * Tell the event loop that we have a new FD.
693 * This can be called from a child thread...
703 * This function is stupid and complicated.
705 static int socket_print(const rad_listen_t *this, char *buffer, size_t bufsize)
708 listen_socket_t *sock = this->data;
711 switch (this->type) {
713 case RAD_LISTEN_NONE: /* what a hack... */
718 case RAD_LISTEN_AUTH:
719 name = "authentication";
722 #ifdef WITH_ACCOUNTING
723 case RAD_LISTEN_ACCT:
729 case RAD_LISTEN_PROXY:
741 case RAD_LISTEN_DHCP:
752 #ifdef WITH_COMMAND_SOCKET
753 case RAD_LISTEN_COMMAND:
763 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
764 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
768 if (sock->interface) {
769 ADDSTRING(" interface ");
770 ADDSTRING(sock->interface);
774 if (this->recv == dual_tcp_accept) {
775 ADDSTRING(" proto tcp");
781 * TCP sockets get printed a little differently, to make
782 * it clear what's going on.
785 ADDSTRING(" from client (");
786 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
790 snprintf(buffer, bufsize, "%d", sock->other_port);
794 if ((sock->my_ipaddr.af == AF_INET) &&
795 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
796 strlcpy(buffer, "*", bufsize);
798 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
803 snprintf(buffer, bufsize, "%d", sock->my_port);
807 ADDSTRING(", virtual-server=");
808 ADDSTRING(this->server);
818 * Maybe it's a socket that we opened to a home server.
820 if ((sock->proto == IPPROTO_TCP) &&
821 (this->type == RAD_LISTEN_PROXY)) {
823 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
827 snprintf(buffer, bufsize, "%d", sock->my_port);
829 ADDSTRING(") -> home_server (");
831 if ((sock->other_ipaddr.af == AF_INET) &&
832 (sock->other_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
833 strlcpy(buffer, "*", bufsize);
835 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
840 snprintf(buffer, bufsize, "%d", sock->other_port);
847 #endif /* WITH_PROXY */
848 #endif /* WITH_TCP */
850 ADDSTRING(" address ");
852 if ((sock->my_ipaddr.af == AF_INET) &&
853 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
854 strlcpy(buffer, "*", bufsize);
856 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
861 snprintf(buffer, bufsize, "%d", sock->my_port);
872 ADDSTRING(" as server ");
873 ADDSTRING(this->server);
882 extern int check_config; /* radiusd.c */
885 static CONF_PARSER limit_config[] = {
886 { "max_connections", PW_TYPE_INTEGER,
887 offsetof(listen_socket_t, limit.max_connections), NULL, "16" },
889 { "lifetime", PW_TYPE_INTEGER,
890 offsetof(listen_socket_t, limit.lifetime), NULL, "0" },
892 { "idle_timeout", PW_TYPE_INTEGER,
893 offsetof(listen_socket_t, limit.idle_timeout), NULL, "30" },
895 { NULL, -1, 0, NULL, NULL } /* end the list */
900 * Parse an authentication or accounting socket.
902 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
905 int listen_port, max_pps;
907 listen_socket_t *sock = this->data;
908 char *section_name = NULL;
909 CONF_SECTION *client_cs, *parentcs;
916 memset(&ipaddr, 0, sizeof(ipaddr));
917 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
918 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
919 &ipaddr.ipaddr.ip4addr, NULL);
920 if (rcode < 0) return -1;
922 if (rcode == 0) { /* successfully parsed IPv4 */
925 } else { /* maybe IPv6? */
926 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
927 &ipaddr.ipaddr.ip6addr, NULL);
928 if (rcode < 0) return -1;
932 "No address specified in listen section");
935 ipaddr.af = AF_INET6;
938 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
940 if (rcode < 0) return -1;
942 if ((listen_port < 0) || (listen_port > 65535)) {
944 "Invalid value for \"port\"");
948 rcode = cf_item_parse(cs, "max_pps", PW_TYPE_INTEGER,
950 if (rcode < 0) return -1;
952 if (max_pps && ((max_pps < 10) || (max_pps > 1000000))) {
954 "Invalid value for \"max_pps\"");
958 sock->proto = IPPROTO_UDP;
960 if (cf_pair_find(cs, "proto")) {
963 "System does not support the TCP protocol. Delete this line from the configuration file.");
971 rcode = cf_item_parse(cs, "proto", PW_TYPE_STRING_PTR,
973 if (rcode < 0) return -1;
975 if (strcmp(proto, "udp") == 0) {
976 sock->proto = IPPROTO_UDP;
978 } else if (strcmp(proto, "tcp") == 0) {
979 sock->proto = IPPROTO_TCP;
982 limit = cf_section_sub_find(cs, "limit");
984 rcode = cf_section_parse(limit, sock,
986 if (rcode < 0) return -1;
988 sock->limit.max_connections = 60;
989 sock->limit.idle_timeout = 30;
990 sock->limit.lifetime = 0;
993 if ((sock->limit.idle_timeout > 0) && (sock->limit.idle_timeout < 5))
994 sock->limit.idle_timeout = 5;
995 if ((sock->limit.lifetime > 0) && (sock->limit.lifetime < 5))
996 sock->limit.lifetime = 5;
997 if ((sock->limit.lifetime > 0) && (sock->limit.idle_timeout > sock->limit.lifetime))
998 sock->limit.idle_timeout = 0;
1001 "Unknown proto name \"%s\"", proto);
1006 * TCP requires a destination IP for sockets.
1007 * UDP doesn't, so it's allowed.
1010 if ((this->type == RAD_LISTEN_PROXY) &&
1011 (sock->proto != IPPROTO_UDP)) {
1013 "Proxy listeners can only listen on proto = udp");
1016 #endif /* WITH_PROXY */
1019 tls = cf_section_sub_find(cs, "tls");
1022 * Don't allow TLS configurations for UDP sockets.
1024 if (sock->proto != IPPROTO_TCP) {
1026 "TLS transport is not available for UDP sockets.");
1032 * FIXME: Make this better.
1034 if (listen_port == 0) listen_port = 2083;
1036 this->tls = tls_server_conf_parse(tls);
1041 #ifdef HAVE_PTRHEAD_H
1042 if (pthread_mutex_init(&sock->mutex, NULL) < 0) {
1050 #else /* WITH_TLS */
1052 * Built without TLS. Disallow it.
1054 if (cf_section_sub_find(cs, "tls")) {
1056 "TLS transport is not available in this executable.");
1059 #endif /* WITH_TLS */
1061 #endif /* WITH_TCP */
1064 * No "proto" field. Disallow TLS.
1066 } else if (cf_section_sub_find(cs, "tls")) {
1068 "TLS transport is not available in this \"listen\" section.");
1072 sock->my_ipaddr = ipaddr;
1073 sock->my_port = listen_port;
1074 sock->max_rate = max_pps;
1078 if (home_server_find(&sock->my_ipaddr, sock->my_port, sock->proto)) {
1081 DEBUGE("We have been asked to listen on %s port %d, which is also listed as a home server. This can create a proxy loop.",
1082 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
1087 return 0; /* don't do anything */
1092 * If we can bind to interfaces, do so,
1095 if (cf_pair_find(cs, "interface")) {
1097 CONF_PAIR *cp = cf_pair_find(cs, "interface");
1099 rad_assert(cp != NULL);
1100 value = cf_pair_value(cp);
1103 "No interface name given");
1106 sock->interface = value;
1111 * If we can do broadcasts..
1113 if (cf_pair_find(cs, "broadcast")) {
1114 #ifndef SO_BROADCAST
1116 "System does not support broadcast sockets. Delete this line from the configuration file.");
1120 CONF_PAIR *cp = cf_pair_find(cs, "broadcast");
1122 if (this->type != RAD_LISTEN_DHCP) {
1124 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file.");
1128 rad_assert(cp != NULL);
1129 value = cf_pair_value(cp);
1132 "No broadcast value given");
1137 * Hack... whatever happened to cf_section_parse?
1139 sock->broadcast = (strcmp(value, "yes") == 0);
1145 * And bind it to the port.
1147 if (listen_bind(this) < 0) {
1150 "Error binding to port for %s port %d",
1151 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
1158 * Proxy sockets don't have clients.
1160 if (this->type == RAD_LISTEN_PROXY) return 0;
1164 * The more specific configurations are preferred to more
1168 parentcs = cf_top_section(cs);
1169 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
1170 §ion_name, NULL);
1171 if (rcode < 0) return -1; /* bad string */
1174 * Explicit list given: use it.
1176 client_cs = cf_section_sub_find_name2(parentcs,
1180 client_cs = cf_section_find(section_name);
1184 "Failed to find clients %s {...}",
1188 } /* else there was no "clients = " entry. */
1191 CONF_SECTION *server_cs;
1193 server_cs = cf_section_sub_find_name2(parentcs,
1197 * Found a "server foo" section. If there are clients
1201 (cf_section_sub_find(server_cs, "client") != NULL)) {
1202 client_cs = server_cs;
1207 * Still nothing. Look for global clients.
1209 if (!client_cs) client_cs = parentcs;
1211 sock->clients = clients_parse_section(client_cs);
1212 if (!sock->clients) {
1214 "Failed to load clients for this listen section");
1219 if (sock->proto == IPPROTO_TCP) {
1221 * Re-write the listener receive function to
1222 * allow us to accept the socket.
1224 this->recv = dual_tcp_accept;
1232 * Send an authentication response packet
1234 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1236 rad_assert(request->listener == listener);
1237 rad_assert(listener->send == auth_socket_send);
1239 #ifdef WITH_UDPFROMTO
1241 * Overwrite the src ip address on the outbound packet
1242 * with the one specified by the client.
1243 * This is useful to work around broken DSR implementations
1244 * and other routing issues.
1246 if (request->client->src_ipaddr.af != AF_UNSPEC) {
1247 request->reply->src_ipaddr = request->client->src_ipaddr;
1251 if (rad_send(request->reply, request->packet,
1252 request->client->secret) < 0) {
1253 radlog_request(L_ERR, 0, request, "Failed sending reply: %s",
1262 #ifdef WITH_ACCOUNTING
1264 * Send an accounting response packet (or not)
1266 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1268 rad_assert(request->listener == listener);
1269 rad_assert(listener->send == acct_socket_send);
1272 * Accounting reject's are silently dropped.
1274 * We do it here to avoid polluting the rest of the
1275 * code with this knowledge
1277 if (request->reply->code == 0) return 0;
1279 #ifdef WITH_UDPFROMTO
1281 * Overwrite the src ip address on the outbound packet
1282 * with the one specified by the client.
1283 * This is useful to work around broken DSR implementations
1284 * and other routing issues.
1286 if (request->client->src_ipaddr.af != AF_UNSPEC) {
1287 request->reply->src_ipaddr = request->client->src_ipaddr;
1291 if (rad_send(request->reply, request->packet,
1292 request->client->secret) < 0) {
1293 radlog_request(L_ERR, 0, request, "Failed sending reply: %s",
1304 * Send a packet to a home server.
1306 * FIXME: have different code for proxy auth & acct!
1308 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1310 rad_assert(request->proxy_listener == listener);
1311 rad_assert(listener->send == proxy_socket_send);
1313 if (rad_send(request->proxy, NULL,
1314 request->home_server->secret) < 0) {
1315 radlog_request(L_ERR, 0, request, "Failed sending proxied request: %s",
1326 * Check if an incoming request is "ok"
1328 * It takes packets, not requests. It sees if the packet looks
1329 * OK. If so, it does a number of sanity checks on it.
1331 static int stats_socket_recv(rad_listen_t *listener)
1335 RADIUS_PACKET *packet;
1336 RADCLIENT *client = NULL;
1337 fr_ipaddr_t src_ipaddr;
1339 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1340 if (rcode < 0) return 0;
1342 FR_STATS_INC(auth, total_requests);
1344 if (rcode < 20) { /* AUTH_HDR_LEN */
1345 FR_STATS_INC(auth, total_malformed_requests);
1349 if ((client = client_listener_find(listener,
1350 &src_ipaddr, src_port)) == NULL) {
1351 rad_recv_discard(listener->fd);
1352 FR_STATS_INC(auth, total_invalid_requests);
1356 FR_STATS_TYPE_INC(client->auth.total_requests);
1359 * We only understand Status-Server on this socket.
1361 if (code != PW_STATUS_SERVER) {
1362 DEBUG("Ignoring packet code %d sent to Status-Server port",
1364 rad_recv_discard(listener->fd);
1365 FR_STATS_INC(auth, total_unknown_types);
1370 * Now that we've sanity checked everything, receive the
1373 packet = rad_recv(listener->fd, 1); /* require message authenticator */
1375 FR_STATS_INC(auth, total_malformed_requests);
1376 DEBUG("%s", fr_strerror());
1380 if (!request_receive(listener, packet, client, rad_status_server)) {
1381 FR_STATS_INC(auth, total_packets_dropped);
1392 * Check if an incoming request is "ok"
1394 * It takes packets, not requests. It sees if the packet looks
1395 * OK. If so, it does a number of sanity checks on it.
1397 static int auth_socket_recv(rad_listen_t *listener)
1401 RADIUS_PACKET *packet;
1402 RAD_REQUEST_FUNP fun = NULL;
1403 RADCLIENT *client = NULL;
1404 fr_ipaddr_t src_ipaddr;
1406 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1407 if (rcode < 0) return 0;
1409 FR_STATS_INC(auth, total_requests);
1411 if (rcode < 20) { /* AUTH_HDR_LEN */
1412 FR_STATS_INC(auth, total_malformed_requests);
1416 if ((client = client_listener_find(listener,
1417 &src_ipaddr, src_port)) == NULL) {
1418 rad_recv_discard(listener->fd);
1419 FR_STATS_INC(auth, total_invalid_requests);
1423 FR_STATS_TYPE_INC(client->auth.total_requests);
1426 * Some sanity checks, based on the packet code.
1429 case PW_AUTHENTICATION_REQUEST:
1430 fun = rad_authenticate;
1433 case PW_STATUS_SERVER:
1434 if (!mainconfig.status_server) {
1435 rad_recv_discard(listener->fd);
1436 FR_STATS_INC(auth, total_unknown_types);
1437 DEBUGW("Ignoring Status-Server request due to security configuration");
1440 fun = rad_status_server;
1444 rad_recv_discard(listener->fd);
1445 FR_STATS_INC(auth,total_unknown_types);
1447 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
1448 code, client->shortname, src_port);
1451 } /* switch over packet types */
1454 * Now that we've sanity checked everything, receive the
1457 packet = rad_recv(listener->fd, client->message_authenticator);
1459 FR_STATS_INC(auth, total_malformed_requests);
1460 DEBUG("%s", fr_strerror());
1464 if (!request_receive(listener, packet, client, fun)) {
1465 FR_STATS_INC(auth, total_packets_dropped);
1474 #ifdef WITH_ACCOUNTING
1476 * Receive packets from an accounting socket
1478 static int acct_socket_recv(rad_listen_t *listener)
1482 RADIUS_PACKET *packet;
1483 RAD_REQUEST_FUNP fun = NULL;
1484 RADCLIENT *client = NULL;
1485 fr_ipaddr_t src_ipaddr;
1487 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1488 if (rcode < 0) return 0;
1490 FR_STATS_INC(acct, total_requests);
1492 if (rcode < 20) { /* AUTH_HDR_LEN */
1493 FR_STATS_INC(acct, total_malformed_requests);
1497 if ((client = client_listener_find(listener,
1498 &src_ipaddr, src_port)) == NULL) {
1499 rad_recv_discard(listener->fd);
1500 FR_STATS_INC(acct, total_invalid_requests);
1504 FR_STATS_TYPE_INC(client->acct.total_requests);
1507 * Some sanity checks, based on the packet code.
1510 case PW_ACCOUNTING_REQUEST:
1511 fun = rad_accounting;
1514 case PW_STATUS_SERVER:
1515 if (!mainconfig.status_server) {
1516 rad_recv_discard(listener->fd);
1517 FR_STATS_INC(acct, total_unknown_types);
1519 DEBUGW("Ignoring Status-Server request due to security configuration");
1522 fun = rad_status_server;
1526 rad_recv_discard(listener->fd);
1527 FR_STATS_INC(acct, total_unknown_types);
1529 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1530 code, client->shortname, src_port);
1532 } /* switch over packet types */
1535 * Now that we've sanity checked everything, receive the
1538 packet = rad_recv(listener->fd, 0);
1540 FR_STATS_INC(acct, total_malformed_requests);
1541 radlog(L_ERR, "%s", fr_strerror());
1546 * There can be no duplicate accounting packets.
1548 if (!request_receive(listener, packet, client, fun)) {
1549 FR_STATS_INC(acct, total_packets_dropped);
1560 static int do_proxy(REQUEST *request)
1564 if (request->in_proxy_hash ||
1565 (request->proxy_reply && (request->proxy_reply->code != 0))) {
1569 vp = pairfind(request->config_items, PW_HOME_SERVER_POOL, 0, TAG_ANY);
1572 if (!home_pool_byname(vp->vp_strvalue, HOME_TYPE_COA)) {
1573 RDEBUG2E("Cannot proxy to unknown pool %s",
1582 * Receive a CoA packet.
1584 static int rad_coa_recv(REQUEST *request)
1586 int rcode = RLM_MODULE_OK;
1591 * Get the correct response
1593 switch (request->packet->code) {
1594 case PW_COA_REQUEST:
1599 case PW_DISCONNECT_REQUEST:
1600 ack = PW_DISCONNECT_ACK;
1601 nak = PW_DISCONNECT_NAK;
1604 default: /* shouldn't happen */
1605 return RLM_MODULE_FAIL;
1609 #define WAS_PROXIED (request->proxy)
1611 #define WAS_PROXIED (0)
1616 * RFC 5176 Section 3.3. If we have a CoA-Request
1617 * with Service-Type = Authorize-Only, it MUST
1618 * have a State attribute in it.
1620 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE, 0, TAG_ANY);
1621 if (request->packet->code == PW_COA_REQUEST) {
1622 if (vp && (vp->vp_integer == 17)) {
1623 vp = pairfind(request->packet->vps, PW_STATE, 0, TAG_ANY);
1624 if (!vp || (vp->length == 0)) {
1625 RDEBUGE("CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1626 request->reply->code = PW_COA_NAK;
1627 return RLM_MODULE_FAIL;
1632 * RFC 5176, Section 3.2.
1634 RDEBUGE("Disconnect-Request MUST NOT contain a Service-Type attribute");
1635 request->reply->code = PW_DISCONNECT_NAK;
1636 return RLM_MODULE_FAIL;
1639 rcode = process_recv_coa(0, request);
1641 case RLM_MODULE_FAIL:
1642 case RLM_MODULE_INVALID:
1643 case RLM_MODULE_REJECT:
1644 case RLM_MODULE_USERLOCK:
1646 request->reply->code = nak;
1649 case RLM_MODULE_HANDLED:
1652 case RLM_MODULE_NOOP:
1653 case RLM_MODULE_NOTFOUND:
1655 case RLM_MODULE_UPDATED:
1656 if (do_proxy(request)) return RLM_MODULE_OK;
1657 request->reply->code = ack;
1660 } else if (request->proxy_reply) {
1662 * Start the reply code with the proxy reply
1665 request->reply->code = request->proxy_reply->code;
1669 * Copy State from the request to the reply.
1670 * See RFC 5176 Section 3.3.
1672 vp = paircopy2(request->reply, request->packet->vps, PW_STATE, 0, TAG_ANY);
1673 if (vp) pairadd(&request->reply->vps, vp);
1676 * We may want to over-ride the reply.
1678 if (request->reply->code) {
1679 rcode = process_send_coa(0, request);
1682 * We need to send CoA-NAK back if Service-Type
1683 * is Authorize-Only. Rely on the user's policy
1684 * to do that. We're not a real NAS, so this
1685 * restriction doesn't (ahem) apply to us.
1687 case RLM_MODULE_FAIL:
1688 case RLM_MODULE_INVALID:
1689 case RLM_MODULE_REJECT:
1690 case RLM_MODULE_USERLOCK:
1693 * Over-ride an ACK with a NAK
1695 request->reply->code = nak;
1698 case RLM_MODULE_HANDLED:
1701 case RLM_MODULE_NOOP:
1702 case RLM_MODULE_NOTFOUND:
1704 case RLM_MODULE_UPDATED:
1706 * Do NOT over-ride a previously set value.
1707 * Otherwise an "ok" here will re-write a
1710 if (request->reply->code == 0) {
1711 request->reply->code = ack;
1717 return RLM_MODULE_OK;
1722 * Check if an incoming request is "ok"
1724 * It takes packets, not requests. It sees if the packet looks
1725 * OK. If so, it does a number of sanity checks on it.
1727 static int coa_socket_recv(rad_listen_t *listener)
1731 RADIUS_PACKET *packet;
1732 RAD_REQUEST_FUNP fun = NULL;
1733 RADCLIENT *client = NULL;
1734 fr_ipaddr_t src_ipaddr;
1736 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1737 if (rcode < 0) return 0;
1739 if (rcode < 20) { /* AUTH_HDR_LEN */
1740 FR_STATS_INC(coa, total_requests);
1741 FR_STATS_INC(coa, total_malformed_requests);
1745 if ((client = client_listener_find(listener,
1746 &src_ipaddr, src_port)) == NULL) {
1747 rad_recv_discard(listener->fd);
1748 FR_STATS_INC(coa, total_requests);
1749 FR_STATS_INC(coa, total_invalid_requests);
1754 * Some sanity checks, based on the packet code.
1757 case PW_COA_REQUEST:
1758 FR_STATS_INC(coa, total_requests);
1762 case PW_DISCONNECT_REQUEST:
1763 FR_STATS_INC(dsc, total_requests);
1768 rad_recv_discard(listener->fd);
1769 FR_STATS_INC(coa, total_unknown_types);
1770 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1771 code, client->shortname, src_port);
1773 } /* switch over packet types */
1776 * Now that we've sanity checked everything, receive the
1779 packet = rad_recv(listener->fd, client->message_authenticator);
1781 FR_STATS_INC(coa, total_malformed_requests);
1782 DEBUG("%s", fr_strerror());
1786 if (!request_receive(listener, packet, client, fun)) {
1787 FR_STATS_INC(coa, total_packets_dropped);
1798 * Recieve packets from a proxy socket.
1800 static int proxy_socket_recv(rad_listen_t *listener)
1802 RADIUS_PACKET *packet;
1805 packet = rad_recv(listener->fd, 0);
1807 radlog(L_ERR, "%s", fr_strerror());
1812 * FIXME: Client MIB updates?
1814 switch(packet->code) {
1815 case PW_AUTHENTICATION_ACK:
1816 case PW_ACCESS_CHALLENGE:
1817 case PW_AUTHENTICATION_REJECT:
1820 #ifdef WITH_ACCOUNTING
1821 case PW_ACCOUNTING_RESPONSE:
1826 case PW_DISCONNECT_ACK:
1827 case PW_DISCONNECT_NAK:
1835 * FIXME: Update MIB for packet types?
1837 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1838 "from home server %s port %d - ID %d : IGNORED",
1840 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1841 packet->src_port, packet->id);
1846 if (!request_proxy_reply(packet)) {
1856 * Recieve packets from a proxy socket.
1858 static int proxy_socket_tcp_recv(rad_listen_t *listener)
1860 RADIUS_PACKET *packet;
1861 listen_socket_t *sock = listener->data;
1864 packet = fr_tcp_recv(listener->fd, 0);
1866 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
1867 event_new_fd(listener);
1872 * FIXME: Client MIB updates?
1874 switch(packet->code) {
1875 case PW_AUTHENTICATION_ACK:
1876 case PW_ACCESS_CHALLENGE:
1877 case PW_AUTHENTICATION_REJECT:
1880 #ifdef WITH_ACCOUNTING
1881 case PW_ACCOUNTING_RESPONSE:
1887 * FIXME: Update MIB for packet types?
1889 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1890 "from home server %s port %d - ID %d : IGNORED",
1892 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1893 packet->src_port, packet->id);
1898 packet->src_ipaddr = sock->other_ipaddr;
1899 packet->src_port = sock->other_port;
1900 packet->dst_ipaddr = sock->my_ipaddr;
1901 packet->dst_port = sock->my_port;
1904 * FIXME: Have it return an indication of packets that
1905 * are OK to ignore (dups, too late), versus ones that
1906 * aren't OK to ignore (unknown response, spoofed, etc.)
1908 * Close the socket on bad packets...
1910 if (!request_proxy_reply(packet)) {
1915 sock->opened = sock->last_packet = time(NULL);
1923 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1925 if (!request->reply->code) return 0;
1927 if (rad_encode(request->reply, request->packet,
1928 request->client->secret) < 0) {
1929 radlog_request(L_ERR, 0, request, "Failed encoding packet: %s",
1934 if (rad_sign(request->reply, request->packet,
1935 request->client->secret) < 0) {
1936 radlog_request(L_ERR, 0, request, "Failed signing packet: %s",
1945 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1947 if (rad_verify(request->packet, NULL,
1948 request->client->secret) < 0) {
1952 return rad_decode(request->packet, NULL,
1953 request->client->secret);
1957 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1959 if (rad_encode(request->proxy, NULL, request->home_server->secret) < 0) {
1960 radlog_request(L_ERR, 0, request, "Failed encoding proxied packet: %s",
1965 if (rad_sign(request->proxy, NULL, request->home_server->secret) < 0) {
1966 radlog_request(L_ERR, 0, request, "Failed signing proxied packet: %s",
1975 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1978 * rad_verify is run in event.c, received_proxy_response()
1981 return rad_decode(request->proxy_reply, request->proxy,
1982 request->home_server->secret);
1988 #include "command.c"
1990 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1992 { common_socket_parse, NULL,
1993 stats_socket_recv, auth_socket_send,
1994 socket_print, client_socket_encode, client_socket_decode },
1997 * This always gets defined.
1999 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
2004 { common_socket_parse, NULL,
2005 proxy_socket_recv, proxy_socket_send,
2006 socket_print, proxy_socket_encode, proxy_socket_decode },
2009 /* authentication */
2010 { common_socket_parse, NULL,
2011 auth_socket_recv, auth_socket_send,
2012 socket_print, client_socket_encode, client_socket_decode },
2014 #ifdef WITH_ACCOUNTING
2016 { common_socket_parse, NULL,
2017 acct_socket_recv, acct_socket_send,
2018 socket_print, client_socket_encode, client_socket_decode},
2023 { detail_parse, detail_free,
2024 detail_recv, detail_send,
2025 detail_print, detail_encode, detail_decode },
2029 /* vlan query protocol */
2030 { common_socket_parse, NULL,
2031 vqp_socket_recv, vqp_socket_send,
2032 socket_print, vqp_socket_encode, vqp_socket_decode },
2036 /* dhcp query protocol */
2037 { dhcp_socket_parse, NULL,
2038 dhcp_socket_recv, dhcp_socket_send,
2039 socket_print, dhcp_socket_encode, dhcp_socket_decode },
2042 #ifdef WITH_COMMAND_SOCKET
2043 /* TCP command socket */
2044 { command_socket_parse, command_socket_free,
2045 command_domain_accept, command_domain_send,
2046 command_socket_print, command_socket_encode, command_socket_decode },
2050 /* Change of Authorization */
2051 { common_socket_parse, NULL,
2052 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
2053 socket_print, client_socket_encode, client_socket_decode },
2061 * Binds a listener to a socket.
2063 static int listen_bind(rad_listen_t *this)
2066 struct sockaddr_storage salocal;
2068 listen_socket_t *sock = this->data;
2070 #define proto_for_port "udp"
2071 #define sock_type SOCK_DGRAM
2073 const char *proto_for_port = "udp";
2074 int sock_type = SOCK_DGRAM;
2076 if (sock->proto == IPPROTO_TCP) {
2078 if (this->type == RAD_LISTEN_VQP) {
2079 radlog(L_ERR, "VQP does not support TCP transport");
2084 proto_for_port = "tcp";
2085 sock_type = SOCK_STREAM;
2090 * If the port is zero, then it means the appropriate
2091 * thing from /etc/services.
2093 if (sock->my_port == 0) {
2094 struct servent *svp;
2096 switch (this->type) {
2097 case RAD_LISTEN_AUTH:
2098 svp = getservbyname ("radius", proto_for_port);
2100 sock->my_port = ntohs(svp->s_port);
2102 sock->my_port = PW_AUTH_UDP_PORT;
2106 #ifdef WITH_ACCOUNTING
2107 case RAD_LISTEN_ACCT:
2108 svp = getservbyname ("radacct", proto_for_port);
2110 sock->my_port = ntohs(svp->s_port);
2112 sock->my_port = PW_ACCT_UDP_PORT;
2118 case RAD_LISTEN_PROXY:
2119 /* leave it at zero */
2124 case RAD_LISTEN_VQP:
2125 sock->my_port = 1589;
2129 #ifdef WITH_COMMAND_SOCKET
2130 case RAD_LISTEN_COMMAND:
2131 sock->my_port = PW_RADMIN_PORT;
2136 case RAD_LISTEN_COA:
2137 svp = getservbyname ("radius-dynauth", "udp");
2139 sock->my_port = ntohs(svp->s_port);
2141 sock->my_port = PW_COA_UDP_PORT;
2147 DEBUGW("Internal sanity check failed in binding to socket. Ignoring problem.");
2153 * Don't open sockets if we're checking the config.
2161 * Copy fr_socket() here, as we may need to bind to a device.
2163 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
2167 this->print(this, buffer, sizeof(buffer));
2169 radlog(L_ERR, "Failed opening %s: %s", buffer, strerror(errno));
2175 * We don't want child processes inheriting these
2178 rcode = fcntl(this->fd, F_GETFD);
2180 if (fcntl(this->fd, F_SETFD, rcode | FD_CLOEXEC) < 0) {
2182 radlog(L_ERR, "Failed setting close on exec: %s", strerror(errno));
2189 * Bind to a device BEFORE touching IP addresses.
2191 if (sock->interface) {
2192 #ifdef SO_BINDTODEVICE
2195 memset(&ifreq, 0, sizeof(ifreq));
2196 strlcpy(ifreq.ifr_name, sock->interface, sizeof(ifreq.ifr_name));
2199 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
2200 (char *)&ifreq, sizeof(ifreq));
2204 radlog(L_ERR, "Failed binding to interface %s: %s",
2205 sock->interface, strerror(errno));
2207 } /* else it worked. */
2209 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2210 #ifdef HAVE_NET_IF_H
2212 * Odds are that any system supporting "bind to
2213 * device" also supports IPv6, so this next bit
2214 * isn't necessary. But it's here for
2217 * If we're doing IPv6, and the scope hasn't yet
2218 * been defined, set the scope to the scope of
2221 if (sock->my_ipaddr.af == AF_INET6) {
2222 if (sock->my_ipaddr.scope == 0) {
2223 sock->my_ipaddr.scope = if_nametoindex(sock->interface);
2224 if (sock->my_ipaddr.scope == 0) {
2226 radlog(L_ERR, "Failed finding interface %s: %s",
2227 sock->interface, strerror(errno));
2230 } /* else scope was defined: we're OK. */
2235 * IPv4: no link local addresses,
2236 * and no bind to device.
2240 radlog(L_ERR, "Failed binding to interface %s: \"bind to device\" is unsupported", sock->interface);
2247 if (sock->proto == IPPROTO_TCP) {
2250 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2252 radlog(L_ERR, "Failed to reuse address: %s", strerror(errno));
2258 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
2259 else /* UDP sockets get UDPfromto */
2262 #ifdef WITH_UDPFROMTO
2264 * Initialize udpfromto for all sockets.
2266 if (udpfromto_init(this->fd) != 0) {
2267 radlog(L_ERR, "Failed initializing udpfromto: %s",
2275 * Set up sockaddr stuff.
2277 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
2282 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2283 if (sock->my_ipaddr.af == AF_INET6) {
2285 * Listening on '::' does NOT get you IPv4 to
2286 * IPv6 mapping. You've got to listen on an IPv4
2287 * address, too. This makes the rest of the server
2288 * design a little simpler.
2292 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2295 if (setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2296 (char *)&on, sizeof(on)) < 0) {
2297 radlog(L_ERR, "Failed setting socket to IPv6 "
2298 "only: %s", strerror(errno));
2304 #endif /* IPV6_V6ONLY */
2306 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2308 if (sock->my_ipaddr.af == AF_INET) {
2311 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2313 * Disable PMTU discovery. On Linux, this
2314 * also makes sure that the "don't fragment"
2317 flag = IP_PMTUDISC_DONT;
2318 if (setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2319 &flag, sizeof(flag)) < 0) {
2320 radlog(L_ERR, "Failed disabling PMTU discovery: %s",
2328 #if defined(IP_DONTFRAG)
2330 * Ensure that the "don't fragment" flag is zero.
2333 if (setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2334 &flag, sizeof(flag)) < 0) {
2335 radlog(L_ERR, "Failed setting don't fragment flag: %s",
2346 if (sock->broadcast) {
2349 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2350 radlog(L_ERR, "Can't set broadcast option: %s",
2359 * May be binding to priviledged ports.
2361 if (sock->my_port != 0) {
2365 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2366 radlog(L_ERR, "Can't set re-use address option: %s",
2373 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2379 this->print(this, buffer, sizeof(buffer));
2380 radlog(L_ERR, "Failed binding to %s: %s\n",
2381 buffer, strerror(errno));
2386 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2387 * kernel instead binds us to a 1.2.3.4. If this
2388 * happens, notice, and remember our real IP.
2391 struct sockaddr_storage src;
2392 socklen_t sizeof_src = sizeof(src);
2394 memset(&src, 0, sizeof_src);
2395 if (getsockname(this->fd, (struct sockaddr *) &src,
2397 radlog(L_ERR, "Failed getting socket name: %s",
2402 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2403 &sock->my_ipaddr, &sock->my_port)) {
2404 radlog(L_ERR, "Socket has unsupported address family");
2411 if (sock->proto == IPPROTO_TCP) {
2412 if (listen(this->fd, 8) < 0) {
2414 radlog(L_ERR, "Failed in listen(): %s", strerror(errno));
2420 if (fr_nonblock(this->fd) < 0) {
2422 radlog(L_ERR, "Failed setting non-blocking on socket: %s",
2428 * Mostly for proxy sockets.
2430 sock->other_ipaddr.af = sock->my_ipaddr.af;
2433 * Don't screw up other people.
2435 #undef proto_for_port
2442 static int listener_free(void *ctx)
2446 this = talloc_get_type_abort(ctx, rad_listen_t);
2449 * Other code may have eaten the FD.
2451 if (this->fd >= 0) close(this->fd);
2453 if (master_listen[this->type].free) {
2454 master_listen[this->type].free(this);
2458 if ((this->type == RAD_LISTEN_AUTH)
2460 || (this->type == RAD_LISTEN_ACCT)
2463 || (this->type == RAD_LISTEN_PROXY)
2465 #ifdef WITH_COMMAND_SOCKET
2466 || ((this->type == RAD_LISTEN_COMMAND) &&
2467 (((fr_command_socket_t *) this->data)->magic != COMMAND_SOCKET_MAGIC))
2470 listen_socket_t *sock = this->data;
2473 if (sock->request) {
2474 pthread_mutex_destroy(&(sock->mutex));
2475 request_free(&sock->request);
2476 sock->packet = NULL;
2478 if (sock->ssn) session_free(sock->ssn);
2479 request_free(&sock->request);
2482 rad_free(&sock->packet);
2484 #endif /* WITH_TCP */
2490 * Allocate & initialize a new listener.
2492 static rad_listen_t *listen_alloc(TALLOC_CTX *ctx, RAD_LISTEN_TYPE type)
2496 this = talloc_zero(ctx, rad_listen_t);
2499 this->recv = master_listen[this->type].recv;
2500 this->send = master_listen[this->type].send;
2501 this->print = master_listen[this->type].print;
2502 this->encode = master_listen[this->type].encode;
2503 this->decode = master_listen[this->type].decode;
2505 talloc_set_destructor((void *) this, listener_free);
2509 case RAD_LISTEN_NONE:
2511 case RAD_LISTEN_AUTH:
2512 #ifdef WITH_ACCOUNTING
2513 case RAD_LISTEN_ACCT:
2516 case RAD_LISTEN_PROXY:
2519 case RAD_LISTEN_VQP:
2522 case RAD_LISTEN_COA:
2524 this->data = talloc_zero(this, listen_socket_t);
2528 case RAD_LISTEN_DHCP:
2529 this->data = talloc_zero(this, dhcp_socket_t);
2534 case RAD_LISTEN_DETAIL:
2539 #ifdef WITH_COMMAND_SOCKET
2540 case RAD_LISTEN_COMMAND:
2541 this->data = talloc_zero(this, fr_command_socket_t);
2546 rad_assert("Unsupported option!" == NULL);
2555 * Externally visible function for creating a new proxy LISTENER.
2557 * Not thread-safe, but all calls to it are protected by the
2558 * proxy mutex in event.c
2560 rad_listen_t *proxy_new_listener(home_server *home, int src_port)
2563 listen_socket_t *sock;
2566 if (!home) return NULL;
2568 if ((home->limit.max_connections > 0) &&
2569 (home->limit.num_connections >= home->limit.max_connections)) {
2570 DEBUGW("Home server has too many open connections (%d)",
2571 home->limit.max_connections);
2575 this = listen_alloc(mainconfig.config, RAD_LISTEN_PROXY);
2578 sock->other_ipaddr = home->ipaddr;
2579 sock->other_port = home->port;
2582 sock->my_ipaddr = home->src_ipaddr;
2583 sock->my_port = src_port;
2584 sock->proto = home->proto;
2586 if (debug_flag >= 2) {
2587 this->print(this, buffer, sizeof(buffer));
2588 DEBUG("Opening new %s", buffer);
2592 sock->opened = sock->last_packet = time(NULL);
2594 if (home->proto == IPPROTO_TCP) {
2595 this->recv = proxy_socket_tcp_recv;
2598 * FIXME: connect() is blocking!
2599 * We do this with the proxy mutex locked, which may
2600 * cause large delays!
2602 * http://www.developerweb.net/forum/showthread.php?p=13486
2604 this->fd = fr_tcp_client_socket(&home->src_ipaddr,
2605 &home->ipaddr, home->port);
2608 DEBUG("Trying SSL to port %d\n", home->port);
2609 sock->ssn = tls_new_client_session(home->tls, this->fd);
2615 this->recv = proxy_tls_recv;
2616 this->send = proxy_tls_send;
2621 this->fd = fr_socket(&home->src_ipaddr, src_port);
2624 this->print(this, buffer,sizeof(buffer));
2625 DEBUG("Failed opening client socket ::%s:: : %s",
2626 buffer, fr_strerror());
2632 * Figure out which port we were bound to.
2634 if (sock->my_port == 0) {
2635 struct sockaddr_storage src;
2636 socklen_t sizeof_src = sizeof(src);
2638 memset(&src, 0, sizeof_src);
2639 if (getsockname(this->fd, (struct sockaddr *) &src,
2641 radlog(L_ERR, "Failed getting socket name: %s",
2647 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2648 &sock->my_ipaddr, &sock->my_port)) {
2649 radlog(L_ERR, "Socket has unsupported address family");
2660 static const FR_NAME_NUMBER listen_compare[] = {
2662 { "status", RAD_LISTEN_NONE },
2664 { "auth", RAD_LISTEN_AUTH },
2665 #ifdef WITH_ACCOUNTING
2666 { "acct", RAD_LISTEN_ACCT },
2669 { "detail", RAD_LISTEN_DETAIL },
2672 { "proxy", RAD_LISTEN_PROXY },
2675 { "vmps", RAD_LISTEN_VQP },
2678 { "dhcp", RAD_LISTEN_DHCP },
2680 #ifdef WITH_COMMAND_SOCKET
2681 { "control", RAD_LISTEN_COMMAND },
2684 { "coa", RAD_LISTEN_COA },
2690 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
2698 cf_log_info(cs, "listen {");
2700 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
2702 if (rcode < 0) return NULL;
2705 "No type specified in listen section");
2709 type = fr_str2int(listen_compare, listen_type, -1);
2712 "Invalid type \"%s\" in listen section.",
2718 * Allow listen sections in the default config to
2719 * refer to a server.
2722 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
2724 if (rcode == 1) { /* compatiblity with 2.0-pre */
2725 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
2728 if (rcode < 0) return NULL;
2733 * We were passed a virtual server, so the caller is
2734 * defining a proxy listener inside of a virtual server.
2735 * This isn't allowed right now.
2737 else if (type == RAD_LISTEN_PROXY) {
2738 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
2744 * Set up cross-type data.
2746 this = listen_alloc(cs, type);
2747 this->server = server;
2751 * Call per-type parser.
2753 if (master_listen[type].parse(cs, this) < 0) {
2758 cf_log_info(cs, "}");
2764 static int is_loopback(const fr_ipaddr_t *ipaddr)
2767 * We shouldn't proxy on loopback.
2769 if ((ipaddr->af == AF_INET) &&
2770 (ipaddr->ipaddr.ip4addr.s_addr == htonl(INADDR_LOOPBACK))) {
2774 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2775 if ((ipaddr->af == AF_INET6) &&
2776 (IN6_IS_ADDR_LINKLOCAL(&ipaddr->ipaddr.ip6addr))) {
2786 * Generate a list of listeners. Takes an input list of
2787 * listeners, too, so we don't close sockets with waiting packets.
2789 int listen_init(CONF_SECTION *config, rad_listen_t **head,
2793 UNUSED int spawn_flag
2798 int override = FALSE;
2799 CONF_SECTION *cs = NULL;
2800 rad_listen_t **last;
2802 fr_ipaddr_t server_ipaddr;
2805 int defined_proxy = 0;
2809 * We shouldn't be called with a pre-existing list.
2811 rad_assert(head && (*head == NULL));
2814 server_ipaddr.af = AF_UNSPEC;
2817 * If the port is specified on the command-line,
2818 * it over-rides the configuration file.
2820 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
2822 if (mainconfig.port >= 0) {
2823 auth_port = mainconfig.port;
2826 * -p X but no -i Y on the command-line.
2828 if ((mainconfig.port > 0) &&
2829 (mainconfig.myip.af == AF_UNSPEC)) {
2830 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
2837 * If the IP address was configured on the command-line,
2838 * use that as the "bind_address"
2840 if (mainconfig.myip.af != AF_UNSPEC) {
2841 listen_socket_t *sock;
2843 memcpy(&server_ipaddr, &mainconfig.myip,
2844 sizeof(server_ipaddr));
2848 if (strcmp(progname, "vmpsd") == 0) {
2849 this = listen_alloc(config, RAD_LISTEN_VQP);
2850 if (!auth_port) auth_port = 1589;
2853 this = listen_alloc(config, RAD_LISTEN_AUTH);
2857 sock->my_ipaddr = server_ipaddr;
2858 sock->my_port = auth_port;
2860 sock->clients = clients_parse_section(config);
2861 if (!sock->clients) {
2862 cf_log_err_cs(config,
2863 "Failed to find any clients for this listen section");
2868 if (listen_bind(this) < 0) {
2870 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
2874 auth_port = sock->my_port; /* may have been updated in listen_bind */
2876 cs = cf_section_sub_find_name2(config, "server",
2878 if (cs) this->server = mainconfig.name;
2882 last = &(this->next);
2888 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
2891 #ifdef WITH_ACCOUNTING
2893 * Open Accounting Socket.
2895 * If we haven't already gotten acct_port from
2896 * /etc/services, then make it auth_port + 1.
2898 this = listen_alloc(config, RAD_LISTEN_ACCT);
2902 * Create the accounting socket.
2904 * The accounting port is always the
2905 * authentication port + 1
2907 sock->my_ipaddr = server_ipaddr;
2908 sock->my_port = auth_port + 1;
2910 sock->clients = clients_parse_section(config);
2911 if (!sock->clients) {
2912 cf_log_err_cs(config,
2913 "Failed to find any clients for this listen section");
2917 if (listen_bind(this) < 0) {
2920 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
2925 cs = cf_section_sub_find_name2(config, "server",
2927 if (cs) this->server = mainconfig.name;
2931 last = &(this->next);
2936 * They specified an IP on the command-line, ignore
2937 * all listen sections except the one in '-n'.
2939 if (mainconfig.myip.af != AF_UNSPEC) {
2940 CONF_SECTION *subcs;
2941 const char *name2 = cf_section_name2(cs);
2943 cs = cf_section_sub_find_name2(config, "server",
2945 if (!cs) goto add_sockets;
2948 * Should really abstract this code...
2950 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2952 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2953 this = listen_parse(subcs, name2);
2960 last = &(this->next);
2961 } /* loop over "listen" directives in server <foo> */
2967 * Walk through the "listen" sections, if they exist.
2969 for (cs = cf_subsection_find_next(config, NULL, "listen");
2971 cs = cf_subsection_find_next(config, cs, "listen")) {
2972 this = listen_parse(cs, NULL);
2979 last = &(this->next);
2983 * Check virtual servers for "listen" sections, too.
2985 * FIXME: Move to virtual server init?
2987 for (cs = cf_subsection_find_next(config, NULL, "server");
2989 cs = cf_subsection_find_next(config, cs, "server")) {
2990 CONF_SECTION *subcs;
2991 const char *name2 = cf_section_name2(cs);
2993 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2995 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2996 this = listen_parse(subcs, name2);
3003 last = &(this->next);
3004 } /* loop over "listen" directives in virtual servers */
3005 } /* loop over virtual servers */
3009 * No sockets to receive packets, this is an error.
3010 * proxying is pointless.
3013 radlog(L_ERR, "The server is not configured to listen on any ports. Cannot start.");
3018 * Print out which sockets we're listening on, and
3019 * add them to the event list.
3021 for (this = *head; this != NULL; this = this->next) {
3023 if (this->type == RAD_LISTEN_PROXY) {
3030 if (!spawn_flag && this->tls) {
3031 cf_log_err_cs(this->cs, "Threading must be enabled for TLS sockets to function properly.");
3032 cf_log_err_cs(this->cs, "You probably need to do 'radiusd -fxx -l stdout' for debugging");
3036 if (!check_config) event_new_fd(this);
3040 * If we're proxying requests, open the proxy FD.
3041 * Otherwise, don't do anything.
3044 if ((mainconfig.proxy_requests == TRUE) &&
3046 (*head != NULL) && !defined_proxy) {
3047 listen_socket_t *sock = NULL;
3051 memset(&home, 0, sizeof(home));
3056 home.proto = IPPROTO_UDP;
3057 home.src_ipaddr = server_ipaddr;
3060 * Find the first authentication port,
3063 for (this = *head; this != NULL; this = this->next) {
3064 if (this->type == RAD_LISTEN_AUTH) {
3067 if (is_loopback(&sock->my_ipaddr)) continue;
3069 if (home.src_ipaddr.af == AF_UNSPEC) {
3070 home.src_ipaddr = sock->my_ipaddr;
3072 port = sock->my_port + 2;
3076 if (this->type == RAD_LISTEN_ACCT) {
3079 if (is_loopback(&sock->my_ipaddr)) continue;
3081 if (home.src_ipaddr.af == AF_UNSPEC) {
3082 home.src_ipaddr = sock->my_ipaddr;
3084 port = sock->my_port + 1;
3091 * Address is still unspecified, use IPv4.
3093 if (home.src_ipaddr.af == AF_UNSPEC) {
3094 home.src_ipaddr.af = AF_INET;
3095 /* everything else is already set to zero */
3098 home.ipaddr.af = home.src_ipaddr.af;
3099 /* everything else is already set to zero */
3101 this = proxy_new_listener(&home, port);
3107 if (!event_new_fd(this)) {
3116 * Haven't defined any sockets. Die.
3118 if (!*head) return -1;
3120 xlat_register("listen", xlat_listen, NULL);
3126 * Free a linked list of listeners;
3128 void listen_free(rad_listen_t **head)
3132 if (!head || !*head) return;
3136 rad_listen_t *next = this->next;
3145 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
3150 for (this = mainconfig.listen; this != NULL; this = this->next) {
3151 listen_socket_t *sock;
3153 if ((this->type != RAD_LISTEN_AUTH)
3154 #ifdef WITH_ACCOUNTING
3155 && (this->type != RAD_LISTEN_ACCT)
3161 if ((sock->my_port == port) &&
3162 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
3163 return sock->clients;
3171 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port, int proto)
3175 for (this = mainconfig.listen; this != NULL; this = this->next) {
3176 listen_socket_t *sock;
3180 if (sock->my_port != port) continue;
3181 if (sock->proto != proto) continue;
3182 if (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) != 0) continue;
3188 * Failed to find a specific one. Find INADDR_ANY
3190 for (this = mainconfig.listen; this != NULL; this = this->next) {
3191 listen_socket_t *sock;
3195 if (sock->my_port != port) continue;
3196 if (sock->proto != proto) continue;
3197 if (!fr_inaddr_any(&sock->my_ipaddr)) continue;