2 * listen.c Handle socket stuff
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2005,2006 The FreeRADIUS server project
21 * Copyright 2005 Alan DeKok <aland@ox.org>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29 #include <freeradius-devel/rad_assert.h>
30 #include <freeradius-devel/vqp.h>
31 #include <freeradius-devel/dhcp.h>
33 #include <freeradius-devel/vmps.h>
34 #include <freeradius-devel/detail.h>
37 #include <freeradius-devel/udpfromto.h>
40 #ifdef HAVE_SYS_RESOURCE_H
41 #include <sys/resource.h>
53 void print_packet(RADIUS_PACKET *packet)
55 char src[256], dst[256];
57 ip_ntoh(&packet->src_ipaddr, src, sizeof(src));
58 ip_ntoh(&packet->dst_ipaddr, dst, sizeof(dst));
60 fprintf(stderr, "ID %d: %s %d -> %s %d\n", packet->id,
61 src, packet->src_port, dst, packet->dst_port);
67 * We'll use this below.
69 typedef int (*rad_listen_parse_t)(CONF_SECTION *, rad_listen_t *);
70 typedef void (*rad_listen_free_t)(rad_listen_t *);
72 typedef struct rad_listen_master_t {
73 rad_listen_parse_t parse;
74 rad_listen_free_t free;
75 rad_listen_recv_t recv;
76 rad_listen_send_t send;
77 rad_listen_print_t print;
78 rad_listen_encode_t encode;
79 rad_listen_decode_t decode;
80 } rad_listen_master_t;
82 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type);
85 * Find a per-socket client.
87 RADCLIENT *client_listener_find(const rad_listen_t *listener,
88 const fr_ipaddr_t *ipaddr, int src_port)
90 #ifdef WITH_DYNAMIC_CLIENTS
97 RADCLIENT_LIST *clients;
98 listen_socket_t *sock;
100 rad_assert(listener != NULL);
101 rad_assert(ipaddr != NULL);
103 sock = listener->data;
104 clients = sock->clients;
107 * This HAS to have been initialized previously.
109 rad_assert(clients != NULL);
111 client = client_find(clients, ipaddr,sock->proto);
113 char name[256], buffer[128];
115 #ifdef WITH_DYNAMIC_CLIENTS
116 unknown: /* used only for dynamic clients */
120 * DoS attack quenching, but only in daemon mode.
121 * If they're running in debug mode, show them
124 if (debug_flag == 0) {
125 static time_t last_printed = 0;
128 if (last_printed == now) return NULL;
133 listener->print(listener, name, sizeof(name));
135 radlog(L_ERR, "Ignoring request to %s from unknown client %s port %d"
139 , name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
140 buffer, sizeof(buffer)), src_port
142 , (sock->proto == IPPROTO_UDP) ? "udp" : "tcp"
148 #ifndef WITH_DYNAMIC_CLIENTS
149 return client; /* return the found client. */
153 * No server defined, and it's not dynamic. Return it.
155 if (!client->client_server && !client->dynamic) return client;
160 * It's a dynamically generated client, check it.
162 if (client->dynamic && (src_port != 0)) {
164 * Lives forever. Return it.
166 if (client->lifetime == 0) return client;
169 * Rate-limit the deletion of known clients.
170 * This makes them last a little longer, but
171 * prevents the server from melting down if (say)
172 * 10k clients all expire at once.
174 if (now == client->last_new_client) return client;
177 * It's not dead yet. Return it.
179 if ((client->created + client->lifetime) > now) return client;
182 * This really puts them onto a queue for later
185 client_delete(clients, client);
188 * Go find the enclosing network again.
190 client = client_find(clients, ipaddr, sock->proto);
195 if (!client) goto unknown;
196 if (!client->client_server) goto unknown;
199 * At this point, 'client' is the enclosing
200 * network that configures where dynamic clients
203 rad_assert(client->dynamic == 0);
206 * The IP is unknown, so we've found an enclosing
207 * network. Enable DoS protection. We only
208 * allow one new client per second. Known
209 * clients aren't subject to this restriction.
211 if (now == client->last_new_client) goto unknown;
214 client->last_new_client = now;
216 request = request_alloc();
217 if (!request) goto unknown;
219 request->listener = listener;
220 request->client = client;
221 request->packet = rad_recv(listener->fd, 0x02); /* MSG_PEEK */
222 if (!request->packet) { /* badly formed, etc */
223 request_free(&request);
226 request->reply = rad_alloc_reply(request->packet);
227 if (!request->reply) {
228 request_free(&request);
231 request->packet->timestamp = request->timestamp;
233 request->priority = listener->type;
234 request->server = client->client_server;
235 request->root = &mainconfig;
238 * Run a fake request through the given virtual server.
239 * Look for FreeRADIUS-Client-IP-Address
240 * FreeRADIUS-Client-Secret
243 * and create the RADCLIENT structure from that.
245 DEBUG("server %s {", request->server);
247 rcode = module_authorize(0, request);
249 DEBUG("} # server %s", request->server);
251 if (rcode != RLM_MODULE_OK) {
252 request_free(&request);
257 * If the client was updated by rlm_dynamic_clients,
258 * don't create the client from attribute-value pairs.
260 if (request->client == client) {
261 created = client_create(clients, request);
263 created = request->client;
266 * This frees the client if it isn't valid.
268 if (!client_validate(clients, client, created)) goto unknown;
270 request_free(&request);
272 if (!created) goto unknown;
278 static int listen_bind(rad_listen_t *this);
282 * Process and reply to a server-status request.
283 * Like rad_authenticate and rad_accounting this should
284 * live in it's own file but it's so small we don't bother.
286 static int rad_status_server(REQUEST *request)
288 int rcode = RLM_MODULE_OK;
291 switch (request->listener->type) {
293 case RAD_LISTEN_NONE:
295 case RAD_LISTEN_AUTH:
296 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
298 rcode = module_authorize(dval->value, request);
300 rcode = RLM_MODULE_OK;
305 case RLM_MODULE_UPDATED:
306 request->reply->code = PW_AUTHENTICATION_ACK;
309 case RLM_MODULE_FAIL:
310 case RLM_MODULE_HANDLED:
311 request->reply->code = 0; /* don't reply */
315 case RLM_MODULE_REJECT:
316 request->reply->code = PW_AUTHENTICATION_REJECT;
321 #ifdef WITH_ACCOUNTING
322 case RAD_LISTEN_ACCT:
323 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
325 rcode = module_accounting(dval->value, request);
327 rcode = RLM_MODULE_OK;
332 case RLM_MODULE_UPDATED:
333 request->reply->code = PW_ACCOUNTING_RESPONSE;
337 request->reply->code = 0; /* don't reply */
345 * This is a vendor extension. Suggested by Glen
346 * Zorn in IETF 72, and rejected by the rest of
347 * the WG. We like it, so it goes in here.
350 dval = dict_valbyname(PW_RECV_COA_TYPE, "Status-Server");
352 rcode = module_recv_coa(dval->value, request);
354 rcode = RLM_MODULE_OK;
359 case RLM_MODULE_UPDATED:
360 request->reply->code = PW_COA_ACK;
364 request->reply->code = 0; /* don't reply */
376 * Full statistics are available only on a statistics
379 if (request->listener->type == RAD_LISTEN_NONE) {
380 request_stats_reply(request);
388 static int auth_tcp_recv(rad_listen_t *listener,
389 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
392 RADIUS_PACKET *packet;
393 RAD_REQUEST_FUNP fun = NULL;
394 listen_socket_t *sock = listener->data;
395 RADCLIENT *client = sock->client;
398 * Allocate a packet for partial reads.
401 sock->packet = rad_alloc(0);
402 if (!sock->packet) return 0;
404 sock->packet->sockfd = listener->fd;
405 sock->packet->src_ipaddr = sock->other_ipaddr;
406 sock->packet->src_port = sock->other_port;
407 sock->packet->dst_ipaddr = sock->my_ipaddr;
408 sock->packet->dst_port = sock->my_port;
412 * Grab the packet currently being processed.
414 packet = sock->packet;
416 rcode = fr_tcp_read_packet(packet, 0);
419 * Still only a partial packet. Put it back, and return,
420 * so that we'll read more data when it's ready.
426 if (rcode == -1) { /* error reading packet */
429 radlog(L_ERR, "Invalid packet from %s port %d: closing socket",
430 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
434 if (rcode < 0) { /* error or connection reset */
435 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
438 * Decrement the number of connections.
440 if (sock->parent->num_connections > 0) {
441 sock->parent->num_connections--;
443 if (sock->client->num_connections > 0) {
444 sock->client->num_connections--;
448 * Tell the event handler that an FD has disappeared.
450 DEBUG("Client has closed connection");
451 event_new_fd(listener);
454 * Do NOT free the listener here. It's in use by
455 * a request, and will need to hang around until
456 * all of the requests are done.
458 * It is instead free'd in remove_from_request_hash()
463 RAD_STATS_TYPE_INC(listener, total_requests);
466 * Some sanity checks, based on the packet code.
468 switch(packet->code) {
469 case PW_AUTHENTICATION_REQUEST:
470 RAD_STATS_CLIENT_INC(listener, client, total_requests);
471 fun = rad_authenticate;
474 case PW_STATUS_SERVER:
475 if (!mainconfig.status_server) {
476 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
477 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
478 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
479 rad_free(&sock->packet);
482 fun = rad_status_server;
486 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
487 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
489 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
490 packet->code, client->shortname, packet->src_port);
491 rad_free(&sock->packet);
493 } /* switch over packet types */
495 if (!received_request(listener, packet, prequest, sock->client)) {
496 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
497 RAD_STATS_CLIENT_INC(listener, sock->client, total_packets_dropped);
498 rad_free(&sock->packet);
503 sock->packet = NULL; /* we have no need for more partial reads */
507 static int auth_tcp_accept(rad_listen_t *listener,
508 UNUSED RAD_REQUEST_FUNP *pfun,
509 UNUSED REQUEST **prequest)
514 struct sockaddr_storage src;
515 listen_socket_t *sock;
516 fr_ipaddr_t src_ipaddr;
521 DEBUG2(" ... new connection request on TCP socket.");
523 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
526 * Non-blocking sockets must handle this.
528 if (errno == EWOULDBLOCK) {
532 DEBUG2(" ... failed to accept connection.");
536 if (!fr_sockaddr2ipaddr(&src, salen, &src_ipaddr, &src_port)) {
537 DEBUG2(" ... unknown address family.");
542 * Enforce client IP address checks on accept, not on
545 if ((client = client_listener_find(listener,
546 &src_ipaddr, src_port)) == NULL) {
548 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
553 * Enforce max_connectionsx on client && listen section.
555 if ((client->max_connections != 0) &&
556 (client->max_connections == client->num_connections)) {
558 * FIXME: Print client IP/port, and server IP/port.
560 radlog(L_INFO, "Ignoring new connection due to client max_connections (%d)", client->max_connections);
565 sock = listener->data;
566 if ((sock->max_connections != 0) &&
567 (sock->max_connections == sock->num_connections)) {
569 * FIXME: Print client IP/port, and server IP/port.
571 radlog(L_INFO, "Ignoring new connection due to socket max_connections");
575 client->num_connections++;
576 sock->num_connections++;
579 * Add the new listener.
581 this = listen_alloc(listener->type);
582 if (!this) return -1;
585 * Copy everything, including the pointer to the socket
589 memcpy(this->data, listener->data, sizeof(*sock));
590 memcpy(this, listener, sizeof(*this));
592 this->data = sock; /* fix it back */
594 sock->parent = listener->data;
595 sock->other_ipaddr = src_ipaddr;
596 sock->other_port = src_port;
597 sock->client = client;
600 this->status = RAD_LISTEN_STATUS_INIT;
601 this->recv = auth_tcp_recv;
604 * FIXME: set O_NONBLOCK on the accept'd fd.
605 * See djb's portability rants for details.
609 * Tell the event loop that we have a new FD.
610 * This can be called from a child thread...
620 * This function is stupid and complicated.
622 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
625 listen_socket_t *sock = this->data;
628 switch (this->type) {
630 case RAD_LISTEN_NONE: /* what a hack... */
635 case RAD_LISTEN_AUTH:
636 name = "authentication";
639 #ifdef WITH_ACCOUNTING
640 case RAD_LISTEN_ACCT:
646 case RAD_LISTEN_PROXY:
658 case RAD_LISTEN_DHCP:
674 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
675 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
679 if (sock->interface) {
680 ADDSTRING(" interface ");
681 ADDSTRING(sock->interface);
685 if (this->recv == auth_tcp_accept) {
686 ADDSTRING(" proto tcp");
692 * TCP sockets get printed a little differently, to make
693 * it clear what's going on.
696 ADDSTRING(" from client (");
697 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
701 snprintf(buffer, bufsize, "%d", sock->other_port);
705 if ((sock->my_ipaddr.af == AF_INET) &&
706 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
707 strlcpy(buffer, "*", bufsize);
709 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
714 snprintf(buffer, bufsize, "%d", sock->my_port);
718 ADDSTRING(", virtual-server=");
719 ADDSTRING(this->server);
728 * Maybe it's a socket that we opened to a home server.
730 if ((sock->proto == IPPROTO_TCP) &&
731 (this->type == RAD_LISTEN_PROXY)) {
733 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
737 snprintf(buffer, bufsize, "%d", sock->my_port);
739 ADDSTRING(") -> home_server (");
741 if ((sock->other_ipaddr.af == AF_INET) &&
742 (sock->other_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
743 strlcpy(buffer, "*", bufsize);
745 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
750 snprintf(buffer, bufsize, "%d", sock->other_port);
759 ADDSTRING(" address ");
761 if ((sock->my_ipaddr.af == AF_INET) &&
762 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
763 strlcpy(buffer, "*", bufsize);
765 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
770 snprintf(buffer, bufsize, "%d", sock->my_port);
774 ADDSTRING(" as server ");
775 ADDSTRING(this->server);
786 * Parse an authentication or accounting socket.
788 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
793 listen_socket_t *sock = this->data;
794 char *section_name = NULL;
795 CONF_SECTION *client_cs, *parentcs;
800 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
801 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
802 &ipaddr.ipaddr.ip4addr, NULL);
803 if (rcode < 0) return -1;
805 if (rcode == 0) { /* successfully parsed IPv4 */
808 } else { /* maybe IPv6? */
809 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
810 &ipaddr.ipaddr.ip6addr, NULL);
811 if (rcode < 0) return -1;
814 cf_log_err(cf_sectiontoitem(cs),
815 "No address specified in listen section");
818 ipaddr.af = AF_INET6;
821 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
823 if (rcode < 0) return -1;
825 if ((listen_port < 0) || (listen_port > 65535)) {
826 cf_log_err(cf_sectiontoitem(cs),
827 "Invalid value for \"port\"");
831 sock->proto = IPPROTO_UDP;
833 if (cf_pair_find(cs, "proto")) {
835 cf_log_err(cf_sectiontoitem(cs),
836 "System does not support the TCP protocol. Delete this line from the configuration file.");
842 rcode = cf_item_parse(cs, "proto", PW_TYPE_STRING_PTR,
844 if (rcode < 0) return -1;
846 if (strcmp(proto, "udp") == 0) {
847 sock->proto = IPPROTO_UDP;
849 } else if (strcmp(proto, "tcp") == 0) {
850 sock->proto = IPPROTO_TCP;
852 rcode = cf_item_parse(cs, "max_connections", PW_TYPE_INTEGER,
853 &sock->max_connections, "64");
854 if (rcode < 0) return -1;
857 cf_log_err(cf_sectiontoitem(cs),
858 "Unknown proto name \"%s\"", proto);
865 * TCP requires a destination IP for sockets.
866 * UDP doesn't, so it's allowed.
868 if ((this->type == RAD_LISTEN_PROXY) &&
869 (sock->proto != IPPROTO_UDP)) {
870 cf_log_err(cf_sectiontoitem(cs),
871 "Proxy listeners can only listen on proto = udp");
877 sock->my_ipaddr = ipaddr;
878 sock->my_port = listen_port;
881 * If we can bind to interfaces, do so,
884 if (cf_pair_find(cs, "interface")) {
886 CONF_PAIR *cp = cf_pair_find(cs, "interface");
888 rad_assert(cp != NULL);
889 value = cf_pair_value(cp);
891 cf_log_err(cf_sectiontoitem(cs),
892 "No interface name given");
895 sock->interface = value;
900 * If we can do broadcasts..
902 if (cf_pair_find(cs, "broadcast")) {
904 cf_log_err(cf_sectiontoitem(cs),
905 "System does not support broadcast sockets. Delete this line from the configuration file.");
909 CONF_PAIR *cp = cf_pair_find(cs, "broadcast");
911 if (this->type != RAD_LISTEN_DHCP) {
912 cf_log_err(cf_pairtoitem(cp),
913 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file.");
917 rad_assert(cp != NULL);
918 value = cf_pair_value(cp);
920 cf_log_err(cf_sectiontoitem(cs),
921 "No broadcast value given");
926 * Hack... whatever happened to cf_section_parse?
928 sock->broadcast = (strcmp(value, "yes") == 0);
934 * And bind it to the port.
936 if (listen_bind(this) < 0) {
938 cf_log_err(cf_sectiontoitem(cs),
939 "Error binding to port for %s port %d",
940 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
947 * Proxy sockets don't have clients.
949 if (this->type == RAD_LISTEN_PROXY) return 0;
953 * The more specific configurations are preferred to more
957 parentcs = cf_top_section(cs);
958 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
959 §ion_name, NULL);
960 if (rcode < 0) return -1; /* bad string */
963 * Explicit list given: use it.
965 client_cs = cf_section_sub_find_name2(parentcs,
969 client_cs = cf_section_find(section_name);
972 cf_log_err(cf_sectiontoitem(cs),
973 "Failed to find clients %s {...}",
979 } /* else there was no "clients = " entry. */
982 CONF_SECTION *server_cs;
984 server_cs = cf_section_sub_find_name2(parentcs,
988 * Found a "server foo" section. If there are clients
992 (cf_section_sub_find(server_cs, "client") != NULL)) {
993 client_cs = server_cs;
998 * Still nothing. Look for global clients.
1000 if (!client_cs) client_cs = parentcs;
1002 sock->clients = clients_parse_section(client_cs);
1003 if (!sock->clients) {
1004 cf_log_err(cf_sectiontoitem(cs),
1005 "Failed to load clients for this listen section");
1010 if (sock->proto == IPPROTO_TCP) {
1012 * Re-write the listener receive function to
1013 * allow us to accept the socket.
1015 this->recv = auth_tcp_accept;
1023 * Send an authentication response packet
1025 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1027 rad_assert(request->listener == listener);
1028 rad_assert(listener->send == auth_socket_send);
1030 return rad_send(request->reply, request->packet,
1031 request->client->secret);
1035 #ifdef WITH_ACCOUNTING
1037 * Send an accounting response packet (or not)
1039 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1041 rad_assert(request->listener == listener);
1042 rad_assert(listener->send == acct_socket_send);
1045 * Accounting reject's are silently dropped.
1047 * We do it here to avoid polluting the rest of the
1048 * code with this knowledge
1050 if (request->reply->code == 0) return 0;
1052 return rad_send(request->reply, request->packet,
1053 request->client->secret);
1059 * Send a packet to a home server.
1061 * FIXME: have different code for proxy auth & acct!
1063 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1065 rad_assert(request->proxy_listener == listener);
1066 rad_assert(listener->send == proxy_socket_send);
1068 return rad_send(request->proxy, request->packet,
1069 request->home_server->secret);
1075 * Check if an incoming request is "ok"
1077 * It takes packets, not requests. It sees if the packet looks
1078 * OK. If so, it does a number of sanity checks on it.
1080 static int stats_socket_recv(rad_listen_t *listener,
1081 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1085 RADIUS_PACKET *packet;
1087 fr_ipaddr_t src_ipaddr;
1089 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1090 if (rcode < 0) return 0;
1092 RAD_STATS_TYPE_INC(listener, total_requests);
1094 if (rcode < 20) { /* AUTH_HDR_LEN */
1095 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1099 if ((client = client_listener_find(listener,
1100 &src_ipaddr, src_port)) == NULL) {
1101 rad_recv_discard(listener->fd);
1102 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1107 * We only understand Status-Server on this socket.
1109 if (code != PW_STATUS_SERVER) {
1110 DEBUG("Ignoring packet code %d sent to Status-Server port",
1112 rad_recv_discard(listener->fd);
1113 RAD_STATS_TYPE_INC(listener, total_unknown_types);
1114 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1119 * Now that we've sanity checked everything, receive the
1122 packet = rad_recv(listener->fd, 1); /* require message authenticator */
1124 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1125 DEBUG("%s", fr_strerror());
1129 if (!received_request(listener, packet, prequest, client)) {
1130 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1131 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1136 *pfun = rad_status_server;
1143 * Check if an incoming request is "ok"
1145 * It takes packets, not requests. It sees if the packet looks
1146 * OK. If so, it does a number of sanity checks on it.
1148 static int auth_socket_recv(rad_listen_t *listener,
1149 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1153 RADIUS_PACKET *packet;
1154 RAD_REQUEST_FUNP fun = NULL;
1156 fr_ipaddr_t src_ipaddr;
1158 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1159 if (rcode < 0) return 0;
1161 RAD_STATS_TYPE_INC(listener, total_requests);
1163 if (rcode < 20) { /* AUTH_HDR_LEN */
1164 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1168 if ((client = client_listener_find(listener,
1169 &src_ipaddr, src_port)) == NULL) {
1170 rad_recv_discard(listener->fd);
1171 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1176 * Some sanity checks, based on the packet code.
1179 case PW_AUTHENTICATION_REQUEST:
1180 RAD_STATS_CLIENT_INC(listener, client, total_requests);
1181 fun = rad_authenticate;
1184 case PW_STATUS_SERVER:
1185 if (!mainconfig.status_server) {
1186 rad_recv_discard(listener->fd);
1187 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1188 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1189 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1192 fun = rad_status_server;
1196 rad_recv_discard(listener->fd);
1197 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
1198 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1200 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
1201 code, client->shortname, src_port);
1204 } /* switch over packet types */
1207 * Now that we've sanity checked everything, receive the
1210 packet = rad_recv(listener->fd, client->message_authenticator);
1212 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1213 DEBUG("%s", fr_strerror());
1217 if (!received_request(listener, packet, prequest, client)) {
1218 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1219 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1229 #ifdef WITH_ACCOUNTING
1231 * Receive packets from an accounting socket
1233 static int acct_socket_recv(rad_listen_t *listener,
1234 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1238 RADIUS_PACKET *packet;
1239 RAD_REQUEST_FUNP fun = NULL;
1241 fr_ipaddr_t src_ipaddr;
1243 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1244 if (rcode < 0) return 0;
1246 RAD_STATS_TYPE_INC(listener, total_requests);
1248 if (rcode < 20) { /* AUTH_HDR_LEN */
1249 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1253 if ((client = client_listener_find(listener,
1254 &src_ipaddr, src_port)) == NULL) {
1255 rad_recv_discard(listener->fd);
1256 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1261 * Some sanity checks, based on the packet code.
1264 case PW_ACCOUNTING_REQUEST:
1265 RAD_STATS_CLIENT_INC(listener, client, total_requests);
1266 fun = rad_accounting;
1269 case PW_STATUS_SERVER:
1270 if (!mainconfig.status_server) {
1271 rad_recv_discard(listener->fd);
1272 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1273 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1275 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1278 fun = rad_status_server;
1282 rad_recv_discard(listener->fd);
1283 RAD_STATS_TYPE_INC(listener, total_unknown_types);
1284 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1286 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1287 code, client->shortname, src_port);
1289 } /* switch over packet types */
1292 * Now that we've sanity checked everything, receive the
1295 packet = rad_recv(listener->fd, 0);
1297 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1298 radlog(L_ERR, "%s", fr_strerror());
1303 * There can be no duplicate accounting packets.
1305 if (!received_request(listener, packet, prequest, client)) {
1306 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1307 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1320 * For now, all CoA requests are *only* originated, and not
1321 * proxied. So all of the necessary work is done in the
1322 * post-proxy section, which is automatically handled by event.c.
1323 * As a result, we don't have to do anything here.
1325 static int rad_coa_reply(REQUEST *request)
1327 VALUE_PAIR *s1, *s2;
1330 * Inform the user about RFC requirements.
1332 s1 = pairfind(request->proxy->vps, PW_STATE);
1334 s2 = pairfind(request->proxy_reply->vps, PW_STATE);
1337 DEBUG("WARNING: Client was sent State in CoA, and did not respond with State.");
1339 } else if ((s1->length != s2->length) ||
1340 (memcmp(s1->vp_octets, s2->vp_octets,
1341 s1->length) != 0)) {
1342 DEBUG("WARNING: Client was sent State in CoA, and did not respond with the same State.");
1346 return RLM_MODULE_OK;
1350 * Receive a CoA packet.
1352 static int rad_coa_recv(REQUEST *request)
1354 int rcode = RLM_MODULE_OK;
1359 * Get the correct response
1361 switch (request->packet->code) {
1362 case PW_COA_REQUEST:
1367 case PW_DISCONNECT_REQUEST:
1368 ack = PW_DISCONNECT_ACK;
1369 nak = PW_DISCONNECT_NAK;
1372 default: /* shouldn't happen */
1373 return RLM_MODULE_FAIL;
1377 #define WAS_PROXIED (request->proxy)
1379 #define WAS_PROXIED (0)
1384 * RFC 5176 Section 3.3. If we have a CoA-Request
1385 * with Service-Type = Authorize-Only, it MUST
1386 * have a State attribute in it.
1388 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE);
1389 if (request->packet->code == PW_COA_REQUEST) {
1390 if (vp && (vp->vp_integer == 17)) {
1391 vp = pairfind(request->packet->vps, PW_STATE);
1392 if (!vp || (vp->length == 0)) {
1393 RDEBUG("ERROR: CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1394 request->reply->code = PW_COA_NAK;
1395 return RLM_MODULE_FAIL;
1400 * RFC 5176, Section 3.2.
1402 RDEBUG("ERROR: Disconnect-Request MUST NOT contain a Service-Type attribute");
1403 request->reply->code = PW_DISCONNECT_NAK;
1404 return RLM_MODULE_FAIL;
1407 rcode = module_recv_coa(0, request);
1409 case RLM_MODULE_FAIL:
1410 case RLM_MODULE_INVALID:
1411 case RLM_MODULE_REJECT:
1412 case RLM_MODULE_USERLOCK:
1414 request->reply->code = nak;
1417 case RLM_MODULE_HANDLED:
1420 case RLM_MODULE_NOOP:
1421 case RLM_MODULE_NOTFOUND:
1423 case RLM_MODULE_UPDATED:
1424 request->reply->code = ack;
1429 * Start the reply code with the proxy reply
1432 request->reply->code = request->proxy_reply->code;
1436 * Copy State from the request to the reply.
1437 * See RFC 5176 Section 3.3.
1439 vp = paircopy2(request->packet->vps, PW_STATE);
1440 if (vp) pairadd(&request->reply->vps, vp);
1443 * We may want to over-ride the reply.
1445 rcode = module_send_coa(0, request);
1448 * We need to send CoA-NAK back if Service-Type
1449 * is Authorize-Only. Rely on the user's policy
1450 * to do that. We're not a real NAS, so this
1451 * restriction doesn't (ahem) apply to us.
1453 case RLM_MODULE_FAIL:
1454 case RLM_MODULE_INVALID:
1455 case RLM_MODULE_REJECT:
1456 case RLM_MODULE_USERLOCK:
1459 * Over-ride an ACK with a NAK
1461 request->reply->code = nak;
1464 case RLM_MODULE_HANDLED:
1467 case RLM_MODULE_NOOP:
1468 case RLM_MODULE_NOTFOUND:
1470 case RLM_MODULE_UPDATED:
1472 * Do NOT over-ride a previously set value.
1473 * Otherwise an "ok" here will re-write a
1476 if (request->reply->code == 0) {
1477 request->reply->code = ack;
1483 return RLM_MODULE_OK;
1488 * Check if an incoming request is "ok"
1490 * It takes packets, not requests. It sees if the packet looks
1491 * OK. If so, it does a number of sanity checks on it.
1493 static int coa_socket_recv(rad_listen_t *listener,
1494 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1498 RADIUS_PACKET *packet;
1499 RAD_REQUEST_FUNP fun = NULL;
1501 fr_ipaddr_t src_ipaddr;
1503 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1504 if (rcode < 0) return 0;
1506 RAD_STATS_TYPE_INC(listener, total_requests);
1508 if (rcode < 20) { /* AUTH_HDR_LEN */
1509 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1513 if ((client = client_listener_find(listener,
1514 &src_ipaddr, src_port)) == NULL) {
1515 rad_recv_discard(listener->fd);
1516 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1521 * Some sanity checks, based on the packet code.
1524 case PW_COA_REQUEST:
1525 case PW_DISCONNECT_REQUEST:
1530 rad_recv_discard(listener->fd);
1531 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1532 code, client->shortname, src_port);
1535 } /* switch over packet types */
1538 * Now that we've sanity checked everything, receive the
1541 packet = rad_recv(listener->fd, client->message_authenticator);
1543 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1544 DEBUG("%s", fr_strerror());
1548 if (!received_request(listener, packet, prequest, client)) {
1560 * Recieve packets from a proxy socket.
1562 static int proxy_socket_recv(rad_listen_t *listener,
1563 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1566 RADIUS_PACKET *packet;
1567 RAD_REQUEST_FUNP fun = NULL;
1570 packet = rad_recv(listener->fd, 0);
1572 radlog(L_ERR, "%s", fr_strerror());
1577 * FIXME: Client MIB updates?
1579 switch(packet->code) {
1580 case PW_AUTHENTICATION_ACK:
1581 case PW_ACCESS_CHALLENGE:
1582 case PW_AUTHENTICATION_REJECT:
1583 fun = rad_authenticate;
1586 #ifdef WITH_ACCOUNTING
1587 case PW_ACCOUNTING_RESPONSE:
1588 fun = rad_accounting;
1593 case PW_DISCONNECT_ACK:
1594 case PW_DISCONNECT_NAK:
1597 fun = rad_coa_reply;
1603 * FIXME: Update MIB for packet types?
1605 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1606 "from home server %s port %d - ID %d : IGNORED",
1608 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1609 packet->src_port, packet->id);
1614 request = received_proxy_response(packet);
1622 * Distinguish proxied CoA requests from ones we
1625 if ((fun == rad_coa_reply) &&
1626 (request->packet->code == request->proxy->code)) {
1631 rad_assert(fun != NULL);
1633 *prequest = request;
1640 * Recieve packets from a proxy socket.
1642 static int proxy_socket_tcp_recv(rad_listen_t *listener,
1643 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1646 RADIUS_PACKET *packet;
1647 RAD_REQUEST_FUNP fun = NULL;
1648 listen_socket_t *sock = listener->data;
1651 packet = fr_tcp_recv(listener->fd, 0);
1653 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
1654 event_new_fd(listener);
1659 * FIXME: Client MIB updates?
1661 switch(packet->code) {
1662 case PW_AUTHENTICATION_ACK:
1663 case PW_ACCESS_CHALLENGE:
1664 case PW_AUTHENTICATION_REJECT:
1665 fun = rad_authenticate;
1668 #ifdef WITH_ACCOUNTING
1669 case PW_ACCOUNTING_RESPONSE:
1670 fun = rad_accounting;
1676 * FIXME: Update MIB for packet types?
1678 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1679 "from home server %s port %d - ID %d : IGNORED",
1681 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1682 packet->src_port, packet->id);
1687 packet->src_ipaddr = sock->other_ipaddr;
1688 packet->src_port = sock->other_port;
1689 packet->dst_ipaddr = sock->my_ipaddr;
1690 packet->dst_port = sock->my_port;
1693 * FIXME: Have it return an indication of packets that
1694 * are OK to ignore (dups, too late), versus ones that
1695 * aren't OK to ignore (unknown response, spoofed, etc.)
1697 * Close the socket on bad packets...
1699 request = received_proxy_response(packet);
1704 rad_assert(fun != NULL);
1705 sock->opened = sock->last_packet = request->timestamp;
1708 *prequest = request;
1716 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1718 if (!request->reply->code) return 0;
1720 rad_encode(request->reply, request->packet,
1721 request->client->secret);
1722 rad_sign(request->reply, request->packet,
1723 request->client->secret);
1729 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1731 if (rad_verify(request->packet, NULL,
1732 request->client->secret) < 0) {
1736 return rad_decode(request->packet, NULL,
1737 request->client->secret);
1741 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1743 rad_encode(request->proxy, NULL, request->home_server->secret);
1744 rad_sign(request->proxy, NULL, request->home_server->secret);
1750 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1753 * rad_verify is run in event.c, received_proxy_response()
1756 return rad_decode(request->proxy_reply, request->proxy,
1757 request->home_server->secret);
1763 #include "command.c"
1765 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1767 { common_socket_parse, NULL,
1768 stats_socket_recv, auth_socket_send,
1769 socket_print, client_socket_encode, client_socket_decode },
1772 * This always gets defined.
1774 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1779 { common_socket_parse, NULL,
1780 proxy_socket_recv, proxy_socket_send,
1781 socket_print, proxy_socket_encode, proxy_socket_decode },
1784 /* authentication */
1785 { common_socket_parse, NULL,
1786 auth_socket_recv, auth_socket_send,
1787 socket_print, client_socket_encode, client_socket_decode },
1789 #ifdef WITH_ACCOUNTING
1791 { common_socket_parse, NULL,
1792 acct_socket_recv, acct_socket_send,
1793 socket_print, client_socket_encode, client_socket_decode},
1798 { detail_parse, detail_free,
1799 detail_recv, detail_send,
1800 detail_print, detail_encode, detail_decode },
1804 /* vlan query protocol */
1805 { common_socket_parse, NULL,
1806 vqp_socket_recv, vqp_socket_send,
1807 socket_print, vqp_socket_encode, vqp_socket_decode },
1811 /* dhcp query protocol */
1812 { dhcp_socket_parse, NULL,
1813 dhcp_socket_recv, dhcp_socket_send,
1814 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1817 #ifdef WITH_COMMAND_SOCKET
1818 /* TCP command socket */
1819 { command_socket_parse, NULL,
1820 command_domain_accept, command_domain_send,
1821 command_socket_print, command_socket_encode, command_socket_decode },
1825 /* Change of Authorization */
1826 { common_socket_parse, NULL,
1827 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
1828 socket_print, client_socket_encode, client_socket_decode },
1836 * Binds a listener to a socket.
1838 static int listen_bind(rad_listen_t *this)
1841 struct sockaddr_storage salocal;
1843 listen_socket_t *sock = this->data;
1845 #define proto_for_port "udp"
1846 #define sock_type SOCK_DGRAM
1848 const char *proto_for_port = "udp";
1849 int sock_type = SOCK_DGRAM;
1851 if (sock->proto == IPPROTO_TCP) {
1853 if (this->type == RAD_LISTEN_VQP) {
1854 radlog(L_ERR, "VQP does not support TCP transport");
1859 proto_for_port = "tcp";
1860 sock_type = SOCK_STREAM;
1865 * If the port is zero, then it means the appropriate
1866 * thing from /etc/services.
1868 if (sock->my_port == 0) {
1869 struct servent *svp;
1871 switch (this->type) {
1872 case RAD_LISTEN_AUTH:
1873 svp = getservbyname ("radius", proto_for_port);
1875 sock->my_port = ntohs(svp->s_port);
1877 sock->my_port = PW_AUTH_UDP_PORT;
1881 #ifdef WITH_ACCOUNTING
1882 case RAD_LISTEN_ACCT:
1883 svp = getservbyname ("radacct", proto_for_port);
1885 sock->my_port = ntohs(svp->s_port);
1887 sock->my_port = PW_ACCT_UDP_PORT;
1893 case RAD_LISTEN_PROXY:
1894 /* leave it at zero */
1899 case RAD_LISTEN_VQP:
1900 sock->my_port = 1589;
1905 case RAD_LISTEN_COA:
1906 svp = getservbyname ("radius-dynauth", "udp");
1908 sock->my_port = ntohs(svp->s_port);
1910 sock->my_port = PW_COA_UDP_PORT;
1916 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1922 * Copy fr_socket() here, as we may need to bind to a device.
1924 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
1926 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
1931 * Bind to a device BEFORE touching IP addresses.
1933 if (sock->interface) {
1934 #ifdef SO_BINDTODEVICE
1936 strcpy(ifreq.ifr_name, sock->interface);
1939 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1940 (char *)&ifreq, sizeof(ifreq));
1944 radlog(L_ERR, "Failed binding to interface %s: %s",
1945 sock->interface, strerror(errno));
1947 } /* else it worked. */
1949 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1950 #ifdef HAVE_NET_IF_H
1952 * Odds are that any system supporting "bind to
1953 * device" also supports IPv6, so this next bit
1954 * isn't necessary. But it's here for
1957 * If we're doing IPv6, and the scope hasn't yet
1958 * been defined, set the scope to the scope of
1961 if (sock->my_ipaddr.af == AF_INET6) {
1962 if (sock->my_ipaddr.scope == 0) {
1963 sock->my_ipaddr.scope = if_nametoindex(sock->interface);
1964 if (sock->my_ipaddr.scope == 0) {
1966 radlog(L_ERR, "Failed finding interface %s: %s",
1967 sock->interface, strerror(errno));
1970 } /* else scope was defined: we're OK. */
1975 * IPv4: no link local addresses,
1976 * and no bind to device.
1980 radlog(L_ERR, "Failed binding to interface %s: \"bind to device\" is unsupported", sock->interface);
1987 if (sock->proto == IPPROTO_TCP) {
1990 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
1992 radlog(L_ERR, "Failed to reuse address: %s", strerror(errno));
1998 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
1999 else /* UDP sockets get UDPfromto */
2002 #ifdef WITH_UDPFROMTO
2004 * Initialize udpfromto for all sockets.
2006 if (udpfromto_init(this->fd) != 0) {
2013 * Set up sockaddr stuff.
2015 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
2020 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2021 if (sock->my_ipaddr.af == AF_INET6) {
2023 * Listening on '::' does NOT get you IPv4 to
2024 * IPv6 mapping. You've got to listen on an IPv4
2025 * address, too. This makes the rest of the server
2026 * design a little simpler.
2030 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2033 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2034 (char *)&on, sizeof(on));
2036 #endif /* IPV6_V6ONLY */
2038 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2040 if (sock->my_ipaddr.af == AF_INET) {
2043 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2045 * Disable PMTU discovery. On Linux, this
2046 * also makes sure that the "don't fragment"
2049 flag = IP_PMTUDISC_DONT;
2050 setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2051 &flag, sizeof(flag));
2054 #if defined(IP_DONTFRAG)
2056 * Ensure that the "don't fragment" flag is zero.
2059 setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2060 &flag, sizeof(flag));
2066 if (sock->broadcast) {
2069 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2070 radlog(L_ERR, "Can't set broadcast option: %s\n",
2079 * May be binding to priviledged ports.
2081 if (sock->my_port != 0) {
2085 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2086 radlog(L_ERR, "Can't set re-use address option: %s\n",
2093 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2099 this->print(this, buffer, sizeof(buffer));
2100 radlog(L_ERR, "Failed binding to %s: %s\n",
2101 buffer, strerror(errno));
2106 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2107 * kernel instead binds us to a 1.2.3.4. If this
2108 * happens, notice, and remember our real IP.
2111 struct sockaddr_storage src;
2112 socklen_t sizeof_src = sizeof(src);
2114 memset(&src, 0, sizeof_src);
2115 if (getsockname(this->fd, (struct sockaddr *) &src,
2117 radlog(L_ERR, "Failed getting socket name: %s",
2122 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2123 &sock->my_ipaddr, &sock->my_port)) {
2124 radlog(L_ERR, "Socket has unsupported address family");
2131 if (sock->proto == IPPROTO_TCP) {
2132 if (listen(this->fd, 8) < 0) {
2134 radlog(L_ERR, "Failed in listen(): %s", strerror(errno));
2140 if (fr_nonblock(this->fd) < 0) {
2142 radlog(L_ERR, "Failed setting non-blocking on socket: %s",
2148 * Mostly for proxy sockets.
2150 sock->other_ipaddr.af = sock->my_ipaddr.af;
2153 * Don't screw up other people.
2155 #undef proto_for_port
2163 * Allocate & initialize a new listener.
2165 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
2169 this = rad_malloc(sizeof(*this));
2170 memset(this, 0, sizeof(*this));
2173 this->recv = master_listen[this->type].recv;
2174 this->send = master_listen[this->type].send;
2175 this->print = master_listen[this->type].print;
2176 this->encode = master_listen[this->type].encode;
2177 this->decode = master_listen[this->type].decode;
2181 case RAD_LISTEN_NONE:
2183 case RAD_LISTEN_AUTH:
2184 #ifdef WITH_ACCOUNTING
2185 case RAD_LISTEN_ACCT:
2188 case RAD_LISTEN_PROXY:
2191 case RAD_LISTEN_VQP:
2194 case RAD_LISTEN_COA:
2196 this->data = rad_malloc(sizeof(listen_socket_t));
2197 memset(this->data, 0, sizeof(listen_socket_t));
2201 case RAD_LISTEN_DHCP:
2202 this->data = rad_malloc(sizeof(dhcp_socket_t));
2203 memset(this->data, 0, sizeof(dhcp_socket_t));
2208 case RAD_LISTEN_DETAIL:
2213 #ifdef WITH_COMMAND_SOCKET
2214 case RAD_LISTEN_COMMAND:
2215 this->data = rad_malloc(sizeof(fr_command_socket_t));
2216 memset(this->data, 0, sizeof(fr_command_socket_t));
2221 rad_assert("Unsupported option!" == NULL);
2230 * Externally visible function for creating a new proxy LISTENER.
2232 * Not thread-safe, but all calls to it are protected by the
2233 * proxy mutex in event.c
2235 int proxy_new_listener(home_server *home, int src_port)
2238 listen_socket_t *sock;
2240 if (!home) return 0;
2242 if ((home->max_connections > 0) &&
2243 (home->num_connections >= home->max_connections)) {
2244 DEBUG("WARNING: Home server has too many open connections (%d)",
2245 home->max_connections);
2249 this = listen_alloc(RAD_LISTEN_PROXY);
2252 sock->other_ipaddr = home->ipaddr;
2253 sock->other_port = home->port;
2256 sock->my_ipaddr = home->src_ipaddr;
2257 sock->my_port = src_port;
2258 sock->proto = home->proto;
2261 sock->last_packet = time(NULL);
2263 if (home->proto == IPPROTO_TCP) {
2264 this->recv = proxy_socket_tcp_recv;
2267 * FIXME: connect() is blocking!
2268 * We do this with the proxy mutex locked, which may
2269 * cause large delays!
2271 * http://www.developerweb.net/forum/showthread.php?p=13486
2273 this->fd = fr_tcp_client_socket(&home->src_ipaddr,
2274 &home->ipaddr, home->port);
2277 this->fd = fr_socket(&home->src_ipaddr, src_port);
2280 DEBUG("Failed opening client socket: %s", fr_strerror());
2286 * Figure out which port we were bound to.
2288 if (sock->my_port == 0) {
2289 struct sockaddr_storage src;
2290 socklen_t sizeof_src = sizeof(src);
2292 memset(&src, 0, sizeof_src);
2293 if (getsockname(this->fd, (struct sockaddr *) &src,
2295 radlog(L_ERR, "Failed getting socket name: %s",
2301 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2302 &sock->my_ipaddr, &sock->my_port)) {
2303 radlog(L_ERR, "Socket has unsupported address family");
2310 * Tell the event loop that we have a new FD
2312 if (!event_new_fd(this)) {
2322 static const FR_NAME_NUMBER listen_compare[] = {
2324 { "status", RAD_LISTEN_NONE },
2326 { "auth", RAD_LISTEN_AUTH },
2327 #ifdef WITH_ACCOUNTING
2328 { "acct", RAD_LISTEN_ACCT },
2331 { "detail", RAD_LISTEN_DETAIL },
2334 { "proxy", RAD_LISTEN_PROXY },
2337 { "vmps", RAD_LISTEN_VQP },
2340 { "dhcp", RAD_LISTEN_DHCP },
2342 #ifdef WITH_COMMAND_SOCKET
2343 { "control", RAD_LISTEN_COMMAND },
2346 { "coa", RAD_LISTEN_COA },
2352 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
2360 cf_log_info(cs, "listen {");
2362 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
2364 if (rcode < 0) return NULL;
2367 cf_log_err(cf_sectiontoitem(cs),
2368 "No type specified in listen section");
2372 type = fr_str2int(listen_compare, listen_type, -1);
2374 cf_log_err(cf_sectiontoitem(cs),
2375 "Invalid type \"%s\" in listen section.",
2383 * Allow listen sections in the default config to
2384 * refer to a server.
2387 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
2389 if (rcode == 1) { /* compatiblity with 2.0-pre */
2390 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
2393 if (rcode < 0) return NULL;
2398 * We were passed a virtual server, so the caller is
2399 * defining a proxy listener inside of a virtual server.
2400 * This isn't allowed right now.
2402 else if (type == RAD_LISTEN_PROXY) {
2403 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
2409 * Set up cross-type data.
2411 this = listen_alloc(type);
2412 this->server = server;
2416 * Call per-type parser.
2418 if (master_listen[type].parse(cs, this) < 0) {
2423 cf_log_info(cs, "}");
2429 * Generate a list of listeners. Takes an input list of
2430 * listeners, too, so we don't close sockets with waiting packets.
2432 int listen_init(CONF_SECTION *config, rad_listen_t **head)
2434 int override = FALSE;
2436 CONF_SECTION *cs = NULL;
2437 rad_listen_t **last;
2439 fr_ipaddr_t server_ipaddr;
2442 int defined_proxy = 0;
2446 * We shouldn't be called with a pre-existing list.
2448 rad_assert(head && (*head == NULL));
2451 server_ipaddr.af = AF_UNSPEC;
2454 * If the port is specified on the command-line,
2455 * it over-rides the configuration file.
2457 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
2459 if (mainconfig.port >= 0) auth_port = mainconfig.port;
2462 * If the IP address was configured on the command-line,
2463 * use that as the "bind_address"
2465 if (mainconfig.myip.af != AF_UNSPEC) {
2466 memcpy(&server_ipaddr, &mainconfig.myip,
2467 sizeof(server_ipaddr));
2473 * Else look for bind_address and/or listen sections.
2475 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
2476 rcode = cf_item_parse(config, "bind_address",
2478 &server_ipaddr.ipaddr.ip4addr, NULL);
2479 if (rcode < 0) return -1; /* error parsing it */
2481 if (rcode == 0) { /* successfully parsed IPv4 */
2482 listen_socket_t *sock;
2483 server_ipaddr.af = AF_INET;
2485 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'.");
2489 if (strcmp(progname, "vmpsd") == 0) {
2490 this = listen_alloc(RAD_LISTEN_VQP);
2491 if (!auth_port) auth_port = 1589;
2494 this = listen_alloc(RAD_LISTEN_AUTH);
2498 sock->my_ipaddr = server_ipaddr;
2499 sock->my_port = auth_port;
2501 sock->clients = clients_parse_section(config);
2502 if (!sock->clients) {
2503 cf_log_err(cf_sectiontoitem(config),
2504 "Failed to find any clients for this listen section");
2509 if (listen_bind(this) < 0) {
2511 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
2515 auth_port = sock->my_port; /* may have been updated in listen_bind */
2517 cs = cf_section_sub_find_name2(config, "server",
2519 if (cs) this->server = mainconfig.name;
2523 last = &(this->next);
2529 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
2532 #ifdef WITH_ACCOUNTING
2534 * Open Accounting Socket.
2536 * If we haven't already gotten acct_port from
2537 * /etc/services, then make it auth_port + 1.
2539 this = listen_alloc(RAD_LISTEN_ACCT);
2543 * Create the accounting socket.
2545 * The accounting port is always the
2546 * authentication port + 1
2548 sock->my_ipaddr = server_ipaddr;
2549 sock->my_port = auth_port + 1;
2551 sock->clients = clients_parse_section(config);
2552 if (!sock->clients) {
2553 cf_log_err(cf_sectiontoitem(config),
2554 "Failed to find any clients for this listen section");
2558 if (listen_bind(this) < 0) {
2561 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
2566 cs = cf_section_sub_find_name2(config, "server",
2568 if (cs) this->server = mainconfig.name;
2572 last = &(this->next);
2574 } else if (mainconfig.port > 0) { /* no bind address, but a port */
2575 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
2581 * They specified an IP on the command-line, ignore
2582 * all listen sections except the one in '-n'.
2584 if (mainconfig.myip.af != AF_UNSPEC) {
2585 CONF_SECTION *subcs;
2586 const char *name2 = cf_section_name2(cs);
2588 cs = cf_section_sub_find_name2(config, "server",
2590 if (!cs) goto add_sockets;
2593 * Should really abstract this code...
2595 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2597 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2598 this = listen_parse(subcs, name2);
2605 last = &(this->next);
2606 } /* loop over "listen" directives in server <foo> */
2612 * Walk through the "listen" sections, if they exist.
2614 for (cs = cf_subsection_find_next(config, NULL, "listen");
2616 cs = cf_subsection_find_next(config, cs, "listen")) {
2617 this = listen_parse(cs, NULL);
2624 last = &(this->next);
2628 * Check virtual servers for "listen" sections, too.
2630 * FIXME: Move to virtual server init?
2632 for (cs = cf_subsection_find_next(config, NULL, "server");
2634 cs = cf_subsection_find_next(config, cs, "server")) {
2635 CONF_SECTION *subcs;
2636 const char *name2 = cf_section_name2(cs);
2638 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2640 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2641 this = listen_parse(subcs, name2);
2648 last = &(this->next);
2649 } /* loop over "listen" directives in virtual servers */
2650 } /* loop over virtual servers */
2654 * Print out which sockets we're listening on, and
2655 * add them to the event list.
2657 for (this = *head; this != NULL; this = this->next) {
2659 if (this->type == RAD_LISTEN_PROXY) {
2668 * If we're proxying requests, open the proxy FD.
2669 * Otherwise, don't do anything.
2672 if ((mainconfig.proxy_requests == TRUE) &&
2673 (*head != NULL) && !defined_proxy) {
2674 listen_socket_t *sock = NULL;
2678 memset(&home, 0, sizeof(home));
2683 home.proto = IPPROTO_UDP;
2684 home.src_ipaddr = server_ipaddr;
2687 * Find the first authentication port,
2690 for (this = *head; this != NULL; this = this->next) {
2691 if (this->type == RAD_LISTEN_AUTH) {
2693 if (home.src_ipaddr.af == AF_UNSPEC) {
2694 home.src_ipaddr = sock->my_ipaddr;
2696 port = sock->my_port + 2;
2700 if (this->type == RAD_LISTEN_ACCT) {
2702 if (home.src_ipaddr.af == AF_UNSPEC) {
2703 home.src_ipaddr = sock->my_ipaddr;
2705 port = sock->my_port + 1;
2712 * Address is still unspecified, use IPv4.
2714 if (home.src_ipaddr.af == AF_UNSPEC) {
2715 home.src_ipaddr.af = AF_INET;
2716 /* everything else is already set to zero */
2719 home.ipaddr.af = home.src_ipaddr.af;
2720 /* everything else is already set to zero */
2722 if (!proxy_new_listener(&home, port)) {
2730 * Haven't defined any sockets. Die.
2732 if (!*head) return -1;
2739 * Free a linked list of listeners;
2741 void listen_free(rad_listen_t **head)
2745 if (!head || !*head) return;
2749 rad_listen_t *next = this->next;
2752 * Other code may have eaten the FD.
2754 if (this->fd >= 0) close(this->fd);
2756 if (master_listen[this->type].free) {
2757 master_listen[this->type].free(this);
2761 if ((this->type == RAD_LISTEN_AUTH) ||
2763 (this->type == RAD_LISTEN_ACCT) ||
2766 (this->type == RAD_LISTEN_PROXY)
2769 listen_socket_t *sock = this->data;
2770 rad_free(&sock->packet);
2784 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
2789 for (this = mainconfig.listen; this != NULL; this = this->next) {
2790 listen_socket_t *sock;
2792 if ((this->type != RAD_LISTEN_AUTH) &&
2793 (this->type != RAD_LISTEN_ACCT)) continue;
2797 if ((sock->my_port == port) &&
2798 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
2799 return sock->clients;
2807 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
2811 for (this = mainconfig.listen; this != NULL; this = this->next) {
2812 listen_socket_t *sock;
2815 * FIXME: For TCP, ignore the *secondary*
2816 * listeners associated with the main socket.
2818 if ((this->type != RAD_LISTEN_AUTH) &&
2819 (this->type != RAD_LISTEN_ACCT)) continue;
2823 if ((sock->my_port == port) &&
2824 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
2828 if ((sock->my_port == port) &&
2829 ((sock->my_ipaddr.af == AF_INET) &&
2830 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == INADDR_ANY))) {
2834 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2835 if ((sock->my_port == port) &&
2836 (sock->my_ipaddr.af == AF_INET6) &&
2837 (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr))) {