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;
410 * Grab the packet currently being processed.
412 packet = sock->packet;
415 rcode = fr_tcp_read_packet(packet, 0);
418 * Still only a partial packet. Put it back, and return,
419 * so that we'll read more data when it's ready.
422 sock->packet = packet;
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 */
437 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
440 * Decrement the number of connections.
442 if (sock->parent->num_connections > 0) {
443 sock->parent->num_connections--;
445 if (sock->client->num_connections > 0) {
446 sock->client->num_connections--;
450 * Tell the event handler that an FD has disappeared.
452 DEBUG("Client has closed connection");
453 event_new_fd(listener);
456 * Do NOT free the listener here. It's in use by
457 * a request, and will need to hang around until
458 * all of the requests are done.
460 * It is instead free'd in remove_from_request_hash()
466 RAD_STATS_TYPE_INC(listener, total_requests);
469 * Some sanity checks, based on the packet code.
471 switch(packet->code) {
472 case PW_AUTHENTICATION_REQUEST:
473 RAD_STATS_CLIENT_INC(listener, client, total_requests);
474 fun = rad_authenticate;
477 case PW_STATUS_SERVER:
478 if (!mainconfig.status_server) {
480 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
481 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
482 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
485 fun = rad_status_server;
490 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
491 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
493 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
494 packet->code, client->shortname, packet->src_port);
497 } /* switch over packet types */
499 if (!received_request(listener, packet, prequest, sock->client)) {
500 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
501 RAD_STATS_CLIENT_INC(listener, sock->client, total_packets_dropped);
506 packet->dst_ipaddr = sock->my_ipaddr;
507 packet->dst_port = sock->my_port;
513 static int auth_tcp_accept(rad_listen_t *listener,
514 UNUSED RAD_REQUEST_FUNP *pfun,
515 UNUSED REQUEST **prequest)
520 struct sockaddr_storage src;
521 listen_socket_t *sock;
522 fr_ipaddr_t src_ipaddr;
527 DEBUG2(" ... new connection request on TCP socket.");
529 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
532 * Non-blocking sockets must handle this.
534 if (errno == EWOULDBLOCK) {
538 DEBUG2(" ... failed to accept connection.");
542 if (!fr_sockaddr2ipaddr(&src, salen, &src_ipaddr, &src_port)) {
543 DEBUG2(" ... unknown address family.");
548 * Enforce client IP address checks on accept, not on
551 if ((client = client_listener_find(listener,
552 &src_ipaddr, src_port)) == NULL) {
554 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
559 * Enforce max_connectionsx on client && listen section.
561 if ((client->max_connections != 0) &&
562 (client->max_connections == client->num_connections)) {
564 * FIXME: Print client IP/port, and server IP/port.
566 radlog(L_INFO, "Ignoring new connection due to client max_connections (%d)", client->max_connections);
571 sock = listener->data;
572 if ((sock->max_connections != 0) &&
573 (sock->max_connections == sock->num_connections)) {
575 * FIXME: Print client IP/port, and server IP/port.
577 radlog(L_INFO, "Ignoring new connection due to socket max_connections");
581 client->num_connections++;
582 sock->num_connections++;
585 * Add the new listener.
587 this = listen_alloc(listener->type);
588 if (!this) return -1;
591 * Copy everything, including the pointer to the socket
595 memcpy(this->data, listener->data, sizeof(*sock));
596 memcpy(this, listener, sizeof(*this));
598 this->data = sock; /* fix it back */
600 sock->parent = listener->data;
601 sock->other_ipaddr = src_ipaddr;
602 sock->other_port = src_port;
603 sock->client = client;
606 this->status = RAD_LISTEN_STATUS_INIT;
607 this->recv = auth_tcp_recv;
610 * FIXME: set O_NONBLOCK on the accept'd fd.
611 * See djb's portability rants for details.
615 * Tell the event loop that we have a new FD.
616 * This can be called from a child thread...
626 * This function is stupid and complicated.
628 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
631 listen_socket_t *sock = this->data;
634 switch (this->type) {
636 case RAD_LISTEN_NONE: /* what a hack... */
641 case RAD_LISTEN_AUTH:
642 name = "authentication";
645 #ifdef WITH_ACCOUNTING
646 case RAD_LISTEN_ACCT:
652 case RAD_LISTEN_PROXY:
664 case RAD_LISTEN_DHCP:
680 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
681 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
685 #ifdef SO_BINDTODEVICE
686 if (sock->interface) {
687 ADDSTRING(" interface ");
688 ADDSTRING(sock->interface);
693 if (this->recv == auth_tcp_accept) {
694 ADDSTRING(" proto tcp");
700 * TCP sockets get printed a little differently, to make
701 * it clear what's going on.
704 ADDSTRING(" from client (");
705 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
709 snprintf(buffer, bufsize, "%d", sock->other_port);
713 if ((sock->my_ipaddr.af == AF_INET) &&
714 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
715 strlcpy(buffer, "*", bufsize);
717 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
722 snprintf(buffer, bufsize, "%d", sock->my_port);
726 ADDSTRING(", virtual-server=");
727 ADDSTRING(this->server);
736 * Maybe it's a socket that we opened to a home server.
738 if ((sock->proto == IPPROTO_TCP) &&
739 (this->type == RAD_LISTEN_PROXY)) {
741 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
745 snprintf(buffer, bufsize, "%d", sock->my_port);
747 ADDSTRING(") -> home_server (");
749 if ((sock->other_ipaddr.af == AF_INET) &&
750 (sock->other_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
751 strlcpy(buffer, "*", bufsize);
753 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
758 snprintf(buffer, bufsize, "%d", sock->other_port);
767 ADDSTRING(" address ");
769 if ((sock->my_ipaddr.af == AF_INET) &&
770 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
771 strlcpy(buffer, "*", bufsize);
773 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
778 snprintf(buffer, bufsize, "%d", sock->my_port);
782 ADDSTRING(" as server ");
783 ADDSTRING(this->server);
794 * Parse an authentication or accounting socket.
796 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
801 listen_socket_t *sock = this->data;
802 char *section_name = NULL;
803 CONF_SECTION *client_cs, *parentcs;
808 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
809 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
810 &ipaddr.ipaddr.ip4addr, NULL);
811 if (rcode < 0) return -1;
813 if (rcode == 0) { /* successfully parsed IPv4 */
816 } else { /* maybe IPv6? */
817 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
818 &ipaddr.ipaddr.ip6addr, NULL);
819 if (rcode < 0) return -1;
822 cf_log_err(cf_sectiontoitem(cs),
823 "No address specified in listen section");
826 ipaddr.af = AF_INET6;
829 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
831 if (rcode < 0) return -1;
833 if ((listen_port < 0) || (listen_port > 65535)) {
834 cf_log_err(cf_sectiontoitem(cs),
835 "Invalid value for \"port\"");
839 sock->proto = IPPROTO_UDP;
841 if (cf_pair_find(cs, "proto")) {
843 cf_log_err(cf_sectiontoitem(cs),
844 "System does not support the TCP protocol. Delete this line from the configuration file.");
850 rcode = cf_item_parse(cs, "proto", PW_TYPE_STRING_PTR,
852 if (rcode < 0) return -1;
854 if (strcmp(proto, "udp") == 0) {
855 sock->proto = IPPROTO_UDP;
857 } else if (strcmp(proto, "tcp") == 0) {
858 sock->proto = IPPROTO_TCP;
860 rcode = cf_item_parse(cs, "max_connections", PW_TYPE_INTEGER,
861 &sock->max_connections, "64");
862 if (rcode < 0) return -1;
865 cf_log_err(cf_sectiontoitem(cs),
866 "Unknown proto name \"%s\"", proto);
873 * TCP requires a destination IP for sockets.
874 * UDP doesn't, so it's allowed.
876 if ((this->type == RAD_LISTEN_PROXY) &&
877 (sock->proto != IPPROTO_UDP)) {
878 cf_log_err(cf_sectiontoitem(cs),
879 "Proxy listeners can only listen on proto = udp");
885 sock->my_ipaddr = ipaddr;
886 sock->my_port = listen_port;
889 * If we can bind to interfaces, do so,
892 if (cf_pair_find(cs, "interface")) {
893 #ifndef SO_BINDTODEVICE
894 cf_log_err(cf_sectiontoitem(cs),
895 "System does not support binding to interfaces. Delete this line from the configuration file.");
899 CONF_PAIR *cp = cf_pair_find(cs, "interface");
901 rad_assert(cp != NULL);
902 value = cf_pair_value(cp);
904 cf_log_err(cf_sectiontoitem(cs),
905 "No interface name given");
908 sock->interface = value;
914 * If we can do broadcasts..
916 if (cf_pair_find(cs, "broadcast")) {
918 cf_log_err(cf_sectiontoitem(cs),
919 "System does not support broadcast sockets. Delete this line from the configuration file.");
923 CONF_PAIR *cp = cf_pair_find(cs, "broadcast");
925 if (this->type != RAD_LISTEN_DHCP) {
926 cf_log_err(cf_pairtoitem(cp),
927 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file.");
931 rad_assert(cp != NULL);
932 value = cf_pair_value(cp);
934 cf_log_err(cf_sectiontoitem(cs),
935 "No broadcast value given");
940 * Hack... whatever happened to cf_section_parse?
942 sock->broadcast = (strcmp(value, "yes") == 0);
948 * And bind it to the port.
950 if (listen_bind(this) < 0) {
952 cf_log_err(cf_sectiontoitem(cs),
953 "Error binding to port for %s port %d",
954 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
961 * Proxy sockets don't have clients.
963 if (this->type == RAD_LISTEN_PROXY) return 0;
967 * The more specific configurations are preferred to more
971 parentcs = cf_top_section(cs);
972 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
973 §ion_name, NULL);
974 if (rcode < 0) return -1; /* bad string */
977 * Explicit list given: use it.
979 client_cs = cf_section_sub_find_name2(parentcs,
983 client_cs = cf_section_find(section_name);
986 cf_log_err(cf_sectiontoitem(cs),
987 "Failed to find clients %s {...}",
993 } /* else there was no "clients = " entry. */
996 CONF_SECTION *server_cs;
998 server_cs = cf_section_sub_find_name2(parentcs,
1002 * Found a "server foo" section. If there are clients
1006 (cf_section_sub_find(server_cs, "client") != NULL)) {
1007 client_cs = server_cs;
1012 * Still nothing. Look for global clients.
1014 if (!client_cs) client_cs = parentcs;
1016 sock->clients = clients_parse_section(client_cs);
1017 if (!sock->clients) {
1018 cf_log_err(cf_sectiontoitem(cs),
1019 "Failed to load clients for this listen section");
1024 if (sock->proto == IPPROTO_TCP) {
1026 * Re-write the listener receive function to
1027 * allow us to accept the socket.
1029 this->recv = auth_tcp_accept;
1037 * Send an authentication response packet
1039 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1041 rad_assert(request->listener == listener);
1042 rad_assert(listener->send == auth_socket_send);
1044 return rad_send(request->reply, request->packet,
1045 request->client->secret);
1049 #ifdef WITH_ACCOUNTING
1051 * Send an accounting response packet (or not)
1053 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1055 rad_assert(request->listener == listener);
1056 rad_assert(listener->send == acct_socket_send);
1059 * Accounting reject's are silently dropped.
1061 * We do it here to avoid polluting the rest of the
1062 * code with this knowledge
1064 if (request->reply->code == 0) return 0;
1066 return rad_send(request->reply, request->packet,
1067 request->client->secret);
1073 * Send a packet to a home server.
1075 * FIXME: have different code for proxy auth & acct!
1077 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1079 rad_assert(request->proxy_listener == listener);
1080 rad_assert(listener->send == proxy_socket_send);
1082 return rad_send(request->proxy, request->packet,
1083 request->home_server->secret);
1089 * Check if an incoming request is "ok"
1091 * It takes packets, not requests. It sees if the packet looks
1092 * OK. If so, it does a number of sanity checks on it.
1094 static int stats_socket_recv(rad_listen_t *listener,
1095 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1099 RADIUS_PACKET *packet;
1101 fr_ipaddr_t src_ipaddr;
1103 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1104 if (rcode < 0) return 0;
1106 RAD_STATS_TYPE_INC(listener, total_requests);
1108 if (rcode < 20) { /* AUTH_HDR_LEN */
1109 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1113 if ((client = client_listener_find(listener,
1114 &src_ipaddr, src_port)) == NULL) {
1115 rad_recv_discard(listener->fd);
1116 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1121 * We only understand Status-Server on this socket.
1123 if (code != PW_STATUS_SERVER) {
1124 DEBUG("Ignoring packet code %d sent to Status-Server port",
1126 rad_recv_discard(listener->fd);
1127 RAD_STATS_TYPE_INC(listener, total_unknown_types);
1128 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1133 * Now that we've sanity checked everything, receive the
1136 packet = rad_recv(listener->fd, 1); /* require message authenticator */
1138 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1139 DEBUG("%s", fr_strerror());
1143 if (!received_request(listener, packet, prequest, client)) {
1144 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1145 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1150 *pfun = rad_status_server;
1157 * Check if an incoming request is "ok"
1159 * It takes packets, not requests. It sees if the packet looks
1160 * OK. If so, it does a number of sanity checks on it.
1162 static int auth_socket_recv(rad_listen_t *listener,
1163 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1167 RADIUS_PACKET *packet;
1168 RAD_REQUEST_FUNP fun = NULL;
1170 fr_ipaddr_t src_ipaddr;
1172 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1173 if (rcode < 0) return 0;
1175 RAD_STATS_TYPE_INC(listener, total_requests);
1177 if (rcode < 20) { /* AUTH_HDR_LEN */
1178 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1182 if ((client = client_listener_find(listener,
1183 &src_ipaddr, src_port)) == NULL) {
1184 rad_recv_discard(listener->fd);
1185 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1190 * Some sanity checks, based on the packet code.
1193 case PW_AUTHENTICATION_REQUEST:
1194 RAD_STATS_CLIENT_INC(listener, client, total_requests);
1195 fun = rad_authenticate;
1198 case PW_STATUS_SERVER:
1199 if (!mainconfig.status_server) {
1200 rad_recv_discard(listener->fd);
1201 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1202 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1203 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1206 fun = rad_status_server;
1210 rad_recv_discard(listener->fd);
1211 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
1212 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1214 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
1215 code, client->shortname, src_port);
1218 } /* switch over packet types */
1221 * Now that we've sanity checked everything, receive the
1224 packet = rad_recv(listener->fd, client->message_authenticator);
1226 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1227 DEBUG("%s", fr_strerror());
1231 if (!received_request(listener, packet, prequest, client)) {
1232 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1233 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1243 #ifdef WITH_ACCOUNTING
1245 * Receive packets from an accounting socket
1247 static int acct_socket_recv(rad_listen_t *listener,
1248 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1252 RADIUS_PACKET *packet;
1253 RAD_REQUEST_FUNP fun = NULL;
1255 fr_ipaddr_t src_ipaddr;
1257 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1258 if (rcode < 0) return 0;
1260 RAD_STATS_TYPE_INC(listener, total_requests);
1262 if (rcode < 20) { /* AUTH_HDR_LEN */
1263 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1267 if ((client = client_listener_find(listener,
1268 &src_ipaddr, src_port)) == NULL) {
1269 rad_recv_discard(listener->fd);
1270 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1275 * Some sanity checks, based on the packet code.
1278 case PW_ACCOUNTING_REQUEST:
1279 RAD_STATS_CLIENT_INC(listener, client, total_requests);
1280 fun = rad_accounting;
1283 case PW_STATUS_SERVER:
1284 if (!mainconfig.status_server) {
1285 rad_recv_discard(listener->fd);
1286 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1287 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1289 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1292 fun = rad_status_server;
1296 rad_recv_discard(listener->fd);
1297 RAD_STATS_TYPE_INC(listener, total_unknown_types);
1298 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1300 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1301 code, client->shortname, src_port);
1303 } /* switch over packet types */
1306 * Now that we've sanity checked everything, receive the
1309 packet = rad_recv(listener->fd, 0);
1311 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1312 radlog(L_ERR, "%s", fr_strerror());
1317 * There can be no duplicate accounting packets.
1319 if (!received_request(listener, packet, prequest, client)) {
1320 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1321 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1334 * For now, all CoA requests are *only* originated, and not
1335 * proxied. So all of the necessary work is done in the
1336 * post-proxy section, which is automatically handled by event.c.
1337 * As a result, we don't have to do anything here.
1339 static int rad_coa_reply(REQUEST *request)
1341 VALUE_PAIR *s1, *s2;
1344 * Inform the user about RFC requirements.
1346 s1 = pairfind(request->proxy->vps, PW_STATE);
1348 s2 = pairfind(request->proxy_reply->vps, PW_STATE);
1351 DEBUG("WARNING: Client was sent State in CoA, and did not respond with State.");
1353 } else if ((s1->length != s2->length) ||
1354 (memcmp(s1->vp_octets, s2->vp_octets,
1355 s1->length) != 0)) {
1356 DEBUG("WARNING: Client was sent State in CoA, and did not respond with the same State.");
1360 return RLM_MODULE_OK;
1364 * Receive a CoA packet.
1366 static int rad_coa_recv(REQUEST *request)
1368 int rcode = RLM_MODULE_OK;
1373 * Get the correct response
1375 switch (request->packet->code) {
1376 case PW_COA_REQUEST:
1381 case PW_DISCONNECT_REQUEST:
1382 ack = PW_DISCONNECT_ACK;
1383 nak = PW_DISCONNECT_NAK;
1386 default: /* shouldn't happen */
1387 return RLM_MODULE_FAIL;
1391 #define WAS_PROXIED (request->proxy)
1393 #define WAS_PROXIED (0)
1398 * RFC 5176 Section 3.3. If we have a CoA-Request
1399 * with Service-Type = Authorize-Only, it MUST
1400 * have a State attribute in it.
1402 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE);
1403 if (request->packet->code == PW_COA_REQUEST) {
1404 if (vp && (vp->vp_integer == 17)) {
1405 vp = pairfind(request->packet->vps, PW_STATE);
1406 if (!vp || (vp->length == 0)) {
1407 RDEBUG("ERROR: CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1408 request->reply->code = PW_COA_NAK;
1409 return RLM_MODULE_FAIL;
1414 * RFC 5176, Section 3.2.
1416 RDEBUG("ERROR: Disconnect-Request MUST NOT contain a Service-Type attribute");
1417 request->reply->code = PW_DISCONNECT_NAK;
1418 return RLM_MODULE_FAIL;
1421 rcode = module_recv_coa(0, request);
1423 case RLM_MODULE_FAIL:
1424 case RLM_MODULE_INVALID:
1425 case RLM_MODULE_REJECT:
1426 case RLM_MODULE_USERLOCK:
1428 request->reply->code = nak;
1431 case RLM_MODULE_HANDLED:
1434 case RLM_MODULE_NOOP:
1435 case RLM_MODULE_NOTFOUND:
1437 case RLM_MODULE_UPDATED:
1438 request->reply->code = ack;
1443 * Start the reply code with the proxy reply
1446 request->reply->code = request->proxy_reply->code;
1450 * Copy State from the request to the reply.
1451 * See RFC 5176 Section 3.3.
1453 vp = paircopy2(request->packet->vps, PW_STATE);
1454 if (vp) pairadd(&request->reply->vps, vp);
1457 * We may want to over-ride the reply.
1459 rcode = module_send_coa(0, request);
1462 * We need to send CoA-NAK back if Service-Type
1463 * is Authorize-Only. Rely on the user's policy
1464 * to do that. We're not a real NAS, so this
1465 * restriction doesn't (ahem) apply to us.
1467 case RLM_MODULE_FAIL:
1468 case RLM_MODULE_INVALID:
1469 case RLM_MODULE_REJECT:
1470 case RLM_MODULE_USERLOCK:
1473 * Over-ride an ACK with a NAK
1475 request->reply->code = nak;
1478 case RLM_MODULE_HANDLED:
1481 case RLM_MODULE_NOOP:
1482 case RLM_MODULE_NOTFOUND:
1484 case RLM_MODULE_UPDATED:
1486 * Do NOT over-ride a previously set value.
1487 * Otherwise an "ok" here will re-write a
1490 if (request->reply->code == 0) {
1491 request->reply->code = ack;
1497 return RLM_MODULE_OK;
1502 * Check if an incoming request is "ok"
1504 * It takes packets, not requests. It sees if the packet looks
1505 * OK. If so, it does a number of sanity checks on it.
1507 static int coa_socket_recv(rad_listen_t *listener,
1508 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1512 RADIUS_PACKET *packet;
1513 RAD_REQUEST_FUNP fun = NULL;
1515 fr_ipaddr_t src_ipaddr;
1517 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1518 if (rcode < 0) return 0;
1520 RAD_STATS_TYPE_INC(listener, total_requests);
1522 if (rcode < 20) { /* AUTH_HDR_LEN */
1523 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1527 if ((client = client_listener_find(listener,
1528 &src_ipaddr, src_port)) == NULL) {
1529 rad_recv_discard(listener->fd);
1530 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1535 * Some sanity checks, based on the packet code.
1538 case PW_COA_REQUEST:
1539 case PW_DISCONNECT_REQUEST:
1544 rad_recv_discard(listener->fd);
1545 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1546 code, client->shortname, src_port);
1549 } /* switch over packet types */
1552 * Now that we've sanity checked everything, receive the
1555 packet = rad_recv(listener->fd, client->message_authenticator);
1557 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1558 DEBUG("%s", fr_strerror());
1562 if (!received_request(listener, packet, prequest, client)) {
1574 * Recieve packets from a proxy socket.
1576 static int proxy_socket_recv(rad_listen_t *listener,
1577 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1580 RADIUS_PACKET *packet;
1581 RAD_REQUEST_FUNP fun = NULL;
1584 packet = rad_recv(listener->fd, 0);
1586 radlog(L_ERR, "%s", fr_strerror());
1591 * FIXME: Client MIB updates?
1593 switch(packet->code) {
1594 case PW_AUTHENTICATION_ACK:
1595 case PW_ACCESS_CHALLENGE:
1596 case PW_AUTHENTICATION_REJECT:
1597 fun = rad_authenticate;
1600 #ifdef WITH_ACCOUNTING
1601 case PW_ACCOUNTING_RESPONSE:
1602 fun = rad_accounting;
1607 case PW_DISCONNECT_ACK:
1608 case PW_DISCONNECT_NAK:
1611 fun = rad_coa_reply;
1617 * FIXME: Update MIB for packet types?
1619 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1620 "from home server %s port %d - ID %d : IGNORED",
1622 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1623 packet->src_port, packet->id);
1628 request = received_proxy_response(packet);
1636 * Distinguish proxied CoA requests from ones we
1639 if ((fun == rad_coa_reply) &&
1640 (request->packet->code == request->proxy->code)) {
1645 rad_assert(fun != NULL);
1647 *prequest = request;
1654 * Recieve packets from a proxy socket.
1656 static int proxy_socket_tcp_recv(rad_listen_t *listener,
1657 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1660 RADIUS_PACKET *packet;
1661 RAD_REQUEST_FUNP fun = NULL;
1662 listen_socket_t *sock = listener->data;
1665 packet = fr_tcp_recv(listener->fd, 0);
1667 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
1668 event_new_fd(listener);
1673 * FIXME: Client MIB updates?
1675 switch(packet->code) {
1676 case PW_AUTHENTICATION_ACK:
1677 case PW_ACCESS_CHALLENGE:
1678 case PW_AUTHENTICATION_REJECT:
1679 fun = rad_authenticate;
1682 #ifdef WITH_ACCOUNTING
1683 case PW_ACCOUNTING_RESPONSE:
1684 fun = rad_accounting;
1690 * FIXME: Update MIB for packet types?
1692 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1693 "from home server %s port %d - ID %d : IGNORED",
1695 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1696 packet->src_port, packet->id);
1701 packet->src_ipaddr = sock->other_ipaddr;
1702 packet->src_port = sock->other_port;
1703 packet->dst_ipaddr = sock->my_ipaddr;
1704 packet->dst_port = sock->my_port;
1707 * FIXME: Have it return an indication of packets that
1708 * are OK to ignore (dups, too late), versus ones that
1709 * aren't OK to ignore (unknown response, spoofed, etc.)
1711 * Close the socket on bad packets...
1713 request = received_proxy_response(packet);
1718 rad_assert(fun != NULL);
1719 sock->opened = sock->last_packet = request->timestamp;
1722 *prequest = request;
1730 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1732 if (!request->reply->code) return 0;
1734 rad_encode(request->reply, request->packet,
1735 request->client->secret);
1736 rad_sign(request->reply, request->packet,
1737 request->client->secret);
1743 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1745 if (rad_verify(request->packet, NULL,
1746 request->client->secret) < 0) {
1750 return rad_decode(request->packet, NULL,
1751 request->client->secret);
1755 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1757 rad_encode(request->proxy, NULL, request->home_server->secret);
1758 rad_sign(request->proxy, NULL, request->home_server->secret);
1764 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1767 * rad_verify is run in event.c, received_proxy_response()
1770 return rad_decode(request->proxy_reply, request->proxy,
1771 request->home_server->secret);
1777 #include "command.c"
1779 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1781 { common_socket_parse, NULL,
1782 stats_socket_recv, auth_socket_send,
1783 socket_print, client_socket_encode, client_socket_decode },
1786 * This always gets defined.
1788 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1793 { common_socket_parse, NULL,
1794 proxy_socket_recv, proxy_socket_send,
1795 socket_print, proxy_socket_encode, proxy_socket_decode },
1798 /* authentication */
1799 { common_socket_parse, NULL,
1800 auth_socket_recv, auth_socket_send,
1801 socket_print, client_socket_encode, client_socket_decode },
1803 #ifdef WITH_ACCOUNTING
1805 { common_socket_parse, NULL,
1806 acct_socket_recv, acct_socket_send,
1807 socket_print, client_socket_encode, client_socket_decode},
1812 { detail_parse, detail_free,
1813 detail_recv, detail_send,
1814 detail_print, detail_encode, detail_decode },
1818 /* vlan query protocol */
1819 { common_socket_parse, NULL,
1820 vqp_socket_recv, vqp_socket_send,
1821 socket_print, vqp_socket_encode, vqp_socket_decode },
1825 /* dhcp query protocol */
1826 { dhcp_socket_parse, NULL,
1827 dhcp_socket_recv, dhcp_socket_send,
1828 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1831 #ifdef WITH_COMMAND_SOCKET
1832 /* TCP command socket */
1833 { command_socket_parse, NULL,
1834 command_domain_accept, command_domain_send,
1835 command_socket_print, command_socket_encode, command_socket_decode },
1839 /* Change of Authorization */
1840 { common_socket_parse, NULL,
1841 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
1842 socket_print, client_socket_encode, client_socket_decode },
1850 * Binds a listener to a socket.
1852 static int listen_bind(rad_listen_t *this)
1855 struct sockaddr_storage salocal;
1857 listen_socket_t *sock = this->data;
1859 #define proto_for_port "udp"
1860 #define sock_type SOCK_DGRAM
1862 const char *proto_for_port = "udp";
1863 int sock_type = SOCK_DGRAM;
1865 if (sock->proto == IPPROTO_TCP) {
1867 if (this->type == RAD_LISTEN_VQP) {
1868 radlog(L_ERR, "VQP does not support TCP transport");
1873 proto_for_port = "tcp";
1874 sock_type = SOCK_STREAM;
1879 * If the port is zero, then it means the appropriate
1880 * thing from /etc/services.
1882 if (sock->my_port == 0) {
1883 struct servent *svp;
1885 switch (this->type) {
1886 case RAD_LISTEN_AUTH:
1887 svp = getservbyname ("radius", proto_for_port);
1889 sock->my_port = ntohs(svp->s_port);
1891 sock->my_port = PW_AUTH_UDP_PORT;
1895 #ifdef WITH_ACCOUNTING
1896 case RAD_LISTEN_ACCT:
1897 svp = getservbyname ("radacct", proto_for_port);
1899 sock->my_port = ntohs(svp->s_port);
1901 sock->my_port = PW_ACCT_UDP_PORT;
1907 case RAD_LISTEN_PROXY:
1908 /* leave it at zero */
1913 case RAD_LISTEN_VQP:
1914 sock->my_port = 1589;
1919 case RAD_LISTEN_COA:
1920 sock->my_port = PW_COA_UDP_PORT;
1925 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1931 * Copy fr_socket() here, as we may need to bind to a device.
1933 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
1935 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
1939 #ifdef SO_BINDTODEVICE
1941 * Bind to a device BEFORE touching IP addresses.
1943 if (sock->interface) {
1945 strcpy(ifreq.ifr_name, sock->interface);
1948 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1949 (char *)&ifreq, sizeof(ifreq));
1953 radlog(L_ERR, "Failed binding to interface %s: %s",
1954 sock->interface, strerror(errno));
1956 } /* else it worked. */
1961 if (sock->proto == IPPROTO_TCP) {
1964 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
1966 radlog(L_ERR, "Failed to reuse address: %s", strerror(errno));
1972 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
1973 else /* UDP sockets get UDPfromto */
1976 #ifdef WITH_UDPFROMTO
1978 * Initialize udpfromto for all sockets.
1980 if (udpfromto_init(this->fd) != 0) {
1987 * Set up sockaddr stuff.
1989 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
1994 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1995 if (sock->my_ipaddr.af == AF_INET6) {
1997 * Listening on '::' does NOT get you IPv4 to
1998 * IPv6 mapping. You've got to listen on an IPv4
1999 * address, too. This makes the rest of the server
2000 * design a little simpler.
2004 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2007 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2008 (char *)&on, sizeof(on));
2010 #endif /* IPV6_V6ONLY */
2012 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2014 if (sock->my_ipaddr.af == AF_INET) {
2017 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2019 * Disable PMTU discovery. On Linux, this
2020 * also makes sure that the "don't fragment"
2023 flag = IP_PMTUDISC_DONT;
2024 setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2025 &flag, sizeof(flag));
2028 #if defined(IP_DONTFRAG)
2030 * Ensure that the "don't fragment" flag is zero.
2033 setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2034 &flag, sizeof(flag));
2040 if (sock->broadcast) {
2043 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2044 radlog(L_ERR, "Can't set broadcast option: %s\n",
2053 * May be binding to priviledged ports.
2055 if (sock->my_port != 0) {
2059 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2060 radlog(L_ERR, "Can't set re-use address option: %s\n",
2067 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2073 this->print(this, buffer, sizeof(buffer));
2074 radlog(L_ERR, "Failed binding to %s: %s\n",
2075 buffer, strerror(errno));
2080 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2081 * kernel instead binds us to a 1.2.3.4. If this
2082 * happens, notice, and remember our real IP.
2085 struct sockaddr_storage src;
2086 socklen_t sizeof_src = sizeof(src);
2088 memset(&src, 0, sizeof_src);
2089 if (getsockname(this->fd, (struct sockaddr *) &src,
2091 radlog(L_ERR, "Failed getting socket name: %s",
2096 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2097 &sock->my_ipaddr, &sock->my_port)) {
2098 radlog(L_ERR, "Socket has unsupported address family");
2105 if (sock->proto == IPPROTO_TCP) {
2106 if (listen(this->fd, 8) < 0) {
2108 radlog(L_ERR, "Failed in listen(): %s", strerror(errno));
2114 if (fr_nonblock(this->fd) < 0) {
2116 radlog(L_ERR, "Failed setting non-blocking on socket: %s",
2122 * Mostly for proxy sockets.
2124 sock->other_ipaddr.af = sock->my_ipaddr.af;
2127 * Don't screw up other people.
2129 #undef proto_for_port
2137 * Allocate & initialize a new listener.
2139 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
2143 this = rad_malloc(sizeof(*this));
2144 memset(this, 0, sizeof(*this));
2147 this->recv = master_listen[this->type].recv;
2148 this->send = master_listen[this->type].send;
2149 this->print = master_listen[this->type].print;
2150 this->encode = master_listen[this->type].encode;
2151 this->decode = master_listen[this->type].decode;
2155 case RAD_LISTEN_NONE:
2157 case RAD_LISTEN_AUTH:
2158 #ifdef WITH_ACCOUNTING
2159 case RAD_LISTEN_ACCT:
2162 case RAD_LISTEN_PROXY:
2165 case RAD_LISTEN_VQP:
2168 case RAD_LISTEN_COA:
2170 this->data = rad_malloc(sizeof(listen_socket_t));
2171 memset(this->data, 0, sizeof(listen_socket_t));
2175 case RAD_LISTEN_DHCP:
2176 this->data = rad_malloc(sizeof(dhcp_socket_t));
2177 memset(this->data, 0, sizeof(dhcp_socket_t));
2182 case RAD_LISTEN_DETAIL:
2187 #ifdef WITH_COMMAND_SOCKET
2188 case RAD_LISTEN_COMMAND:
2189 this->data = rad_malloc(sizeof(fr_command_socket_t));
2190 memset(this->data, 0, sizeof(fr_command_socket_t));
2195 rad_assert("Unsupported option!" == NULL);
2204 * Externally visible function for creating a new proxy LISTENER.
2206 * Not thread-safe, but all calls to it are protected by the
2207 * proxy mutex in event.c
2209 int proxy_new_listener(home_server *home, int src_port)
2212 listen_socket_t *sock;
2214 if (!home) return 0;
2216 if ((home->max_connections > 0) &&
2217 (home->num_connections >= home->max_connections)) {
2218 DEBUG("WARNING: Home server has too many open connections (%d)",
2219 home->max_connections);
2223 this = listen_alloc(RAD_LISTEN_PROXY);
2226 sock->other_ipaddr = home->ipaddr;
2227 sock->other_port = home->port;
2230 sock->my_ipaddr = home->src_ipaddr;
2231 sock->my_port = src_port;
2232 sock->proto = home->proto;
2235 sock->last_packet = time(NULL);
2237 if (home->proto == IPPROTO_TCP) {
2238 this->recv = proxy_socket_tcp_recv;
2241 * FIXME: connect() is blocking!
2242 * We do this with the proxy mutex locked, which may
2243 * cause large delays!
2245 * http://www.developerweb.net/forum/showthread.php?p=13486
2247 this->fd = fr_tcp_client_socket(&home->src_ipaddr,
2248 &home->ipaddr, home->port);
2251 this->fd = fr_socket(&home->src_ipaddr, src_port);
2254 DEBUG("Failed opening client socket: %s", fr_strerror());
2260 * Figure out which port we were bound to.
2262 if (sock->my_port == 0) {
2263 struct sockaddr_storage src;
2264 socklen_t sizeof_src = sizeof(src);
2266 memset(&src, 0, sizeof_src);
2267 if (getsockname(this->fd, (struct sockaddr *) &src,
2269 radlog(L_ERR, "Failed getting socket name: %s",
2275 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2276 &sock->my_ipaddr, &sock->my_port)) {
2277 radlog(L_ERR, "Socket has unsupported address family");
2284 * Tell the event loop that we have a new FD
2293 static const FR_NAME_NUMBER listen_compare[] = {
2295 { "status", RAD_LISTEN_NONE },
2297 { "auth", RAD_LISTEN_AUTH },
2298 #ifdef WITH_ACCOUNTING
2299 { "acct", RAD_LISTEN_ACCT },
2302 { "detail", RAD_LISTEN_DETAIL },
2305 { "proxy", RAD_LISTEN_PROXY },
2308 { "vmps", RAD_LISTEN_VQP },
2311 { "dhcp", RAD_LISTEN_DHCP },
2313 #ifdef WITH_COMMAND_SOCKET
2314 { "control", RAD_LISTEN_COMMAND },
2317 { "coa", RAD_LISTEN_COA },
2323 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
2331 cf_log_info(cs, "listen {");
2333 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
2335 if (rcode < 0) return NULL;
2338 cf_log_err(cf_sectiontoitem(cs),
2339 "No type specified in listen section");
2343 type = fr_str2int(listen_compare, listen_type, -1);
2345 cf_log_err(cf_sectiontoitem(cs),
2346 "Invalid type \"%s\" in listen section.",
2354 * Allow listen sections in the default config to
2355 * refer to a server.
2358 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
2360 if (rcode == 1) { /* compatiblity with 2.0-pre */
2361 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
2364 if (rcode < 0) return NULL;
2369 * We were passed a virtual server, so the caller is
2370 * defining a proxy listener inside of a virtual server.
2371 * This isn't allowed right now.
2373 else if (this->type == RAD_LISTEN_PROXY) {
2374 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
2380 * Set up cross-type data.
2382 this = listen_alloc(type);
2383 this->server = server;
2387 * Call per-type parser.
2389 if (master_listen[type].parse(cs, this) < 0) {
2394 cf_log_info(cs, "}");
2400 * Generate a list of listeners. Takes an input list of
2401 * listeners, too, so we don't close sockets with waiting packets.
2403 int listen_init(CONF_SECTION *config, rad_listen_t **head)
2405 int override = FALSE;
2407 CONF_SECTION *cs = NULL;
2408 rad_listen_t **last;
2410 fr_ipaddr_t server_ipaddr;
2413 int defined_proxy = 0;
2417 * We shouldn't be called with a pre-existing list.
2419 rad_assert(head && (*head == NULL));
2422 server_ipaddr.af = AF_UNSPEC;
2425 * If the port is specified on the command-line,
2426 * it over-rides the configuration file.
2428 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
2430 if (mainconfig.port >= 0) auth_port = mainconfig.port;
2433 * If the IP address was configured on the command-line,
2434 * use that as the "bind_address"
2436 if (mainconfig.myip.af != AF_UNSPEC) {
2437 memcpy(&server_ipaddr, &mainconfig.myip,
2438 sizeof(server_ipaddr));
2444 * Else look for bind_address and/or listen sections.
2446 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
2447 rcode = cf_item_parse(config, "bind_address",
2449 &server_ipaddr.ipaddr.ip4addr, NULL);
2450 if (rcode < 0) return -1; /* error parsing it */
2452 if (rcode == 0) { /* successfully parsed IPv4 */
2453 listen_socket_t *sock;
2454 server_ipaddr.af = AF_INET;
2456 radlog(L_INFO, "WARNING: The directive 'bind_adress' is deprecated, and will be removed in future versions of FreeRADIUS. Please edit the configuration files to use the directive 'listen'.");
2460 if (strcmp(progname, "vmpsd") == 0) {
2461 this = listen_alloc(RAD_LISTEN_VQP);
2462 if (!auth_port) auth_port = 1589;
2465 this = listen_alloc(RAD_LISTEN_AUTH);
2469 sock->my_ipaddr = server_ipaddr;
2470 sock->my_port = auth_port;
2472 sock->clients = clients_parse_section(config);
2473 if (!sock->clients) {
2474 cf_log_err(cf_sectiontoitem(config),
2475 "Failed to find any clients for this listen section");
2480 if (listen_bind(this) < 0) {
2482 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
2486 auth_port = sock->my_port; /* may have been updated in listen_bind */
2488 cs = cf_section_sub_find_name2(config, "server",
2490 if (cs) this->server = mainconfig.name;
2494 last = &(this->next);
2500 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
2503 #ifdef WITH_ACCOUNTING
2505 * Open Accounting Socket.
2507 * If we haven't already gotten acct_port from
2508 * /etc/services, then make it auth_port + 1.
2510 this = listen_alloc(RAD_LISTEN_ACCT);
2514 * Create the accounting socket.
2516 * The accounting port is always the
2517 * authentication port + 1
2519 sock->my_ipaddr = server_ipaddr;
2520 sock->my_port = auth_port + 1;
2522 sock->clients = clients_parse_section(config);
2523 if (!sock->clients) {
2524 cf_log_err(cf_sectiontoitem(config),
2525 "Failed to find any clients for this listen section");
2529 if (listen_bind(this) < 0) {
2532 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
2537 cs = cf_section_sub_find_name2(config, "server",
2539 if (cs) this->server = mainconfig.name;
2543 last = &(this->next);
2545 } else if (mainconfig.port > 0) { /* no bind address, but a port */
2546 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
2552 * They specified an IP on the command-line, ignore
2553 * all listen sections except the one in '-n'.
2555 if (mainconfig.myip.af != AF_UNSPEC) {
2556 CONF_SECTION *subcs;
2557 const char *name2 = cf_section_name2(cs);
2559 cs = cf_section_sub_find_name2(config, "server",
2561 if (!cs) goto add_sockets;
2564 * Should really abstract this code...
2566 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2568 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2569 this = listen_parse(subcs, name2);
2576 last = &(this->next);
2577 } /* loop over "listen" directives in server <foo> */
2583 * Walk through the "listen" sections, if they exist.
2585 for (cs = cf_subsection_find_next(config, NULL, "listen");
2587 cs = cf_subsection_find_next(config, cs, "listen")) {
2588 this = listen_parse(cs, NULL);
2595 last = &(this->next);
2599 * Check virtual servers for "listen" sections, too.
2601 * FIXME: Move to virtual server init?
2603 for (cs = cf_subsection_find_next(config, NULL, "server");
2605 cs = cf_subsection_find_next(config, cs, "server")) {
2606 CONF_SECTION *subcs;
2607 const char *name2 = cf_section_name2(cs);
2609 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2611 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2612 this = listen_parse(subcs, name2);
2619 last = &(this->next);
2620 } /* loop over "listen" directives in virtual servers */
2621 } /* loop over virtual servers */
2625 * Print out which sockets we're listening on, and
2626 * add them to the event list.
2628 for (this = *head; this != NULL; this = this->next) {
2630 if (this->type == RAD_LISTEN_PROXY) {
2639 * If we're proxying requests, open the proxy FD.
2640 * Otherwise, don't do anything.
2643 if ((mainconfig.proxy_requests == TRUE) &&
2644 (*head != NULL) && !defined_proxy) {
2645 listen_socket_t *sock = NULL;
2649 memset(&home, 0, sizeof(home));
2655 home.proto = IPPROTO_UDP;
2658 home.src_ipaddr = server_ipaddr;
2661 * Find the first authentication port,
2664 for (this = *head; this != NULL; this = this->next) {
2665 if (this->type == RAD_LISTEN_AUTH) {
2667 if (home.src_ipaddr.af == AF_UNSPEC) {
2668 home.src_ipaddr = sock->my_ipaddr;
2670 port = sock->my_port + 2;
2674 if (this->type == RAD_LISTEN_ACCT) {
2676 if (home.src_ipaddr.af == AF_UNSPEC) {
2677 home.src_ipaddr = sock->my_ipaddr;
2679 port = sock->my_port + 1;
2686 * Address is still unspecified, use IPv4.
2688 if (home.src_ipaddr.af == AF_UNSPEC) {
2689 home.src_ipaddr.af = AF_INET;
2690 /* everything else is already set to zero */
2693 home.ipaddr.af = home.src_ipaddr.af;
2694 /* everything else is already set to zero */
2696 if (!proxy_new_listener(&home, port)) {
2704 * Haven't defined any sockets. Die.
2706 if (!*head) return -1;
2713 * Free a linked list of listeners;
2715 void listen_free(rad_listen_t **head)
2719 if (!head || !*head) return;
2723 rad_listen_t *next = this->next;
2726 * Other code may have eaten the FD.
2728 if (this->fd >= 0) close(this->fd);
2730 if (master_listen[this->type].free) {
2731 master_listen[this->type].free(this);
2735 if ((this->type == RAD_LISTEN_AUTH) ||
2737 (this->type == RAD_LISTEN_ACCT) ||
2740 (this->type == RAD_LISTEN_PROXY)
2743 listen_socket_t *sock = this->data;
2744 rad_free(&sock->packet);
2758 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
2763 for (this = mainconfig.listen; this != NULL; this = this->next) {
2764 listen_socket_t *sock;
2766 if ((this->type != RAD_LISTEN_AUTH) &&
2767 (this->type != RAD_LISTEN_ACCT)) continue;
2771 if ((sock->my_port == port) &&
2772 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
2773 return sock->clients;
2781 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
2785 for (this = mainconfig.listen; this != NULL; this = this->next) {
2786 listen_socket_t *sock;
2789 * FIXME: For TCP, ignore the *secondary*
2790 * listeners associated with the main socket.
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)) {
2802 if ((sock->my_port == port) &&
2803 ((sock->my_ipaddr.af == AF_INET) &&
2804 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == INADDR_ANY))) {
2808 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2809 if ((sock->my_port == port) &&
2810 (sock->my_ipaddr.af == AF_INET6) &&
2811 (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr))) {