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
117 char name[256], buffer[128];
119 #ifdef WITH_DYNAMIC_CLIENTS
120 unknown: /* used only for dynamic clients */
124 * DoS attack quenching, but only in daemon mode.
125 * If they're running in debug mode, show them
128 if (debug_flag == 0) {
129 static time_t last_printed = 0;
132 if (last_printed == now) return NULL;
137 listener->print(listener, name, sizeof(name));
139 radlog(L_ERR, "Ignoring request to %s from unknown client %s port %d"
143 , name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
144 buffer, sizeof(buffer)), src_port
146 , (sock->proto == IPPROTO_UDP) ? "udp" : "tcp"
152 #ifndef WITH_DYNAMIC_CLIENTS
153 return client; /* return the found client. */
157 * No server defined, and it's not dynamic. Return it.
159 if (!client->client_server && !client->dynamic) return client;
164 * It's a dynamically generated client, check it.
166 if (client->dynamic && (src_port != 0)) {
168 * Lives forever. Return it.
170 if (client->lifetime == 0) return client;
173 * Rate-limit the deletion of known clients.
174 * This makes them last a little longer, but
175 * prevents the server from melting down if (say)
176 * 10k clients all expire at once.
178 if (now == client->last_new_client) return client;
181 * It's not dead yet. Return it.
183 if ((client->created + client->lifetime) > now) return client;
186 * This really puts them onto a queue for later
189 client_delete(clients, client);
192 * Go find the enclosing network again.
194 client = client_find(clients, ipaddr
203 if (!client) goto unknown;
204 if (!client->client_server) goto unknown;
207 * At this point, 'client' is the enclosing
208 * network that configures where dynamic clients
211 rad_assert(client->dynamic == 0);
214 * The IP is unknown, so we've found an enclosing
215 * network. Enable DoS protection. We only
216 * allow one new client per second. Known
217 * clients aren't subject to this restriction.
219 if (now == client->last_new_client) goto unknown;
222 client->last_new_client = now;
224 request = request_alloc();
225 if (!request) goto unknown;
227 request->listener = listener;
228 request->client = client;
229 request->packet = rad_recv(listener->fd, 0x02); /* MSG_PEEK */
230 if (!request->packet) { /* badly formed, etc */
231 request_free(&request);
234 request->reply = rad_alloc_reply(request->packet);
235 if (!request->reply) {
236 request_free(&request);
239 request->packet->timestamp = request->timestamp;
241 request->priority = listener->type;
242 request->server = client->client_server;
243 request->root = &mainconfig;
246 * Run a fake request through the given virtual server.
247 * Look for FreeRADIUS-Client-IP-Address
248 * FreeRADIUS-Client-Secret
251 * and create the RADCLIENT structure from that.
253 DEBUG("server %s {", request->server);
255 rcode = module_authorize(0, request);
257 DEBUG("} # server %s", request->server);
259 if (rcode != RLM_MODULE_OK) {
260 request_free(&request);
265 * If the client was updated by rlm_dynamic_clients,
266 * don't create the client from attribute-value pairs.
268 if (request->client == client) {
269 created = client_create(clients, request);
271 created = request->client;
274 * This frees the client if it isn't valid.
276 if (!client_validate(clients, client, created)) goto unknown;
278 request_free(&request);
280 if (!created) goto unknown;
286 static int listen_bind(rad_listen_t *this);
290 * Process and reply to a server-status request.
291 * Like rad_authenticate and rad_accounting this should
292 * live in it's own file but it's so small we don't bother.
294 static int rad_status_server(REQUEST *request)
296 int rcode = RLM_MODULE_OK;
299 switch (request->listener->type) {
301 case RAD_LISTEN_NONE:
303 case RAD_LISTEN_AUTH:
304 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
306 rcode = module_authorize(dval->value, request);
308 rcode = RLM_MODULE_OK;
313 case RLM_MODULE_UPDATED:
314 request->reply->code = PW_AUTHENTICATION_ACK;
317 case RLM_MODULE_FAIL:
318 case RLM_MODULE_HANDLED:
319 request->reply->code = 0; /* don't reply */
323 case RLM_MODULE_REJECT:
324 request->reply->code = PW_AUTHENTICATION_REJECT;
329 #ifdef WITH_ACCOUNTING
330 case RAD_LISTEN_ACCT:
331 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
333 rcode = module_accounting(dval->value, request);
335 rcode = RLM_MODULE_OK;
340 case RLM_MODULE_UPDATED:
341 request->reply->code = PW_ACCOUNTING_RESPONSE;
345 request->reply->code = 0; /* don't reply */
353 * This is a vendor extension. Suggested by Glen
354 * Zorn in IETF 72, and rejected by the rest of
355 * the WG. We like it, so it goes in here.
358 dval = dict_valbyname(PW_RECV_COA_TYPE, "Status-Server");
360 rcode = module_recv_coa(dval->value, request);
362 rcode = RLM_MODULE_OK;
367 case RLM_MODULE_UPDATED:
368 request->reply->code = PW_COA_ACK;
372 request->reply->code = 0; /* don't reply */
384 * Full statistics are available only on a statistics
387 if (request->listener->type == RAD_LISTEN_NONE) {
388 request_stats_reply(request);
396 static int auth_tcp_recv(rad_listen_t *listener,
397 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
400 RADIUS_PACKET *packet;
401 RAD_REQUEST_FUNP fun = NULL;
402 listen_socket_t *sock = listener->data;
403 RADCLIENT *client = sock->client;
406 * Allocate a packet for partial reads.
409 sock->packet = rad_alloc(0);
410 if (!sock->packet) return 0;
412 sock->packet->sockfd = listener->fd;
413 sock->packet->src_ipaddr = sock->other_ipaddr;
414 sock->packet->src_port = sock->other_port;
418 * Grab the packet currently being processed.
420 packet = sock->packet;
423 rcode = fr_tcp_read_packet(packet, 0);
426 * Still only a partial packet. Put it back, and return,
427 * so that we'll read more data when it's ready.
430 sock->packet = packet;
434 if (rcode == -1) { /* error reading packet */
437 radlog(L_ERR, "Invalid packet from %s port %d: closing socket",
438 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
442 if (rcode < 0) { /* error or connection reset */
445 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
448 * Decrement the number of connections.
450 if (sock->parent->num_connections > 0) {
451 sock->parent->num_connections--;
453 if (sock->client->num_connections > 0) {
454 sock->client->num_connections--;
458 * Tell the event handler that an FD has disappeared.
460 DEBUG("Client has closed connection");
461 event_new_fd(listener);
464 * Do NOT free the listener here. It's in use by
465 * a request, and will need to hang around until
466 * all of the requests are done.
468 * It is instead free'd in remove_from_request_hash()
474 RAD_STATS_TYPE_INC(listener, total_requests);
477 * Some sanity checks, based on the packet code.
479 switch(packet->code) {
480 case PW_AUTHENTICATION_REQUEST:
481 RAD_STATS_CLIENT_INC(listener, client, total_requests);
482 fun = rad_authenticate;
485 case PW_STATUS_SERVER:
486 if (!mainconfig.status_server) {
488 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
489 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
490 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
493 fun = rad_status_server;
498 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
499 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
501 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
502 packet->code, client->shortname, packet->src_port);
505 } /* switch over packet types */
507 if (!received_request(listener, packet, prequest, sock->client)) {
508 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
509 RAD_STATS_CLIENT_INC(listener, sock->client, total_packets_dropped);
514 packet->dst_ipaddr = sock->my_ipaddr;
515 packet->dst_port = sock->my_port;
521 static int auth_tcp_accept(rad_listen_t *listener,
522 UNUSED RAD_REQUEST_FUNP *pfun,
523 UNUSED REQUEST **prequest)
528 struct sockaddr_storage src;
529 listen_socket_t *sock;
530 fr_ipaddr_t src_ipaddr;
535 DEBUG2(" ... new connection request on TCP socket.");
537 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
540 * Non-blocking sockets must handle this.
542 if (errno == EWOULDBLOCK) {
546 DEBUG2(" ... failed to accept connection.");
550 if (!fr_sockaddr2ipaddr(&src, salen, &src_ipaddr, &src_port)) {
551 DEBUG2(" ... unknown address family.");
556 * Enforce client IP address checks on accept, not on
559 if ((client = client_listener_find(listener,
560 &src_ipaddr, src_port)) == NULL) {
562 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
567 * Enforce max_connectionsx on client && listen section.
569 if ((client->max_connections != 0) &&
570 (client->max_connections == client->num_connections)) {
572 * FIXME: Print client IP/port, and server IP/port.
574 radlog(L_INFO, "Ignoring new connection due to client max_connections (%d)", client->max_connections);
579 sock = listener->data;
580 if ((sock->max_connections != 0) &&
581 (sock->max_connections == sock->num_connections)) {
583 * FIXME: Print client IP/port, and server IP/port.
585 radlog(L_INFO, "Ignoring new connection due to socket max_connections");
589 client->num_connections++;
590 sock->num_connections++;
593 * Add the new listener.
595 this = listen_alloc(listener->type);
596 if (!this) return -1;
599 * Copy everything, including the pointer to the socket
603 memcpy(this->data, listener->data, sizeof(*sock));
604 memcpy(this, listener, sizeof(*this));
606 this->data = sock; /* fix it back */
608 sock->parent = listener->data;
609 sock->other_ipaddr = src_ipaddr;
610 sock->other_port = src_port;
611 sock->client = client;
614 this->status = RAD_LISTEN_STATUS_INIT;
615 this->recv = auth_tcp_recv;
618 * FIXME: set O_NONBLOCK on the accept'd fd.
619 * See djb's portability rants for details.
623 * Tell the event loop that we have a new FD.
624 * This can be called from a child thread...
634 * This function is stupid and complicated.
636 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
639 listen_socket_t *sock = this->data;
642 switch (this->type) {
644 case RAD_LISTEN_NONE: /* what a hack... */
649 case RAD_LISTEN_AUTH:
650 name = "authentication";
653 #ifdef WITH_ACCOUNTING
654 case RAD_LISTEN_ACCT:
660 case RAD_LISTEN_PROXY:
672 case RAD_LISTEN_DHCP:
688 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
689 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
693 #ifdef SO_BINDTODEVICE
694 if (sock->interface) {
695 ADDSTRING(" interface ");
696 ADDSTRING(sock->interface);
701 if (this->recv == auth_tcp_accept) {
702 ADDSTRING(" proto tcp");
708 * TCP sockets get printed a little differently, to make
709 * it clear what's going on.
712 ADDSTRING(" from client (");
713 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
717 snprintf(buffer, bufsize, "%d", sock->other_port);
721 if ((sock->my_ipaddr.af == AF_INET) &&
722 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
723 strlcpy(buffer, "*", bufsize);
725 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
730 snprintf(buffer, bufsize, "%d", sock->my_port);
734 ADDSTRING(", virtual-server=");
735 ADDSTRING(this->server);
744 * Maybe it's a socket that we opened to a home server.
746 if ((sock->proto == IPPROTO_TCP) &&
747 (this->type == RAD_LISTEN_PROXY)) {
749 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
753 snprintf(buffer, bufsize, "%d", sock->my_port);
755 ADDSTRING(") -> home_server (");
757 if ((sock->other_ipaddr.af == AF_INET) &&
758 (sock->other_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
759 strlcpy(buffer, "*", bufsize);
761 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
766 snprintf(buffer, bufsize, "%d", sock->other_port);
775 ADDSTRING(" address ");
777 if ((sock->my_ipaddr.af == AF_INET) &&
778 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
779 strlcpy(buffer, "*", bufsize);
781 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
786 snprintf(buffer, bufsize, "%d", sock->my_port);
790 ADDSTRING(" as server ");
791 ADDSTRING(this->server);
802 * Parse an authentication or accounting socket.
804 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
809 listen_socket_t *sock = this->data;
810 char *section_name = NULL;
811 CONF_SECTION *client_cs, *parentcs;
816 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
817 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
818 &ipaddr.ipaddr.ip4addr, NULL);
819 if (rcode < 0) return -1;
821 if (rcode == 0) { /* successfully parsed IPv4 */
824 } else { /* maybe IPv6? */
825 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
826 &ipaddr.ipaddr.ip6addr, NULL);
827 if (rcode < 0) return -1;
830 cf_log_err(cf_sectiontoitem(cs),
831 "No address specified in listen section");
834 ipaddr.af = AF_INET6;
837 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
839 if (rcode < 0) return -1;
841 if ((listen_port < 0) || (listen_port > 65535)) {
842 cf_log_err(cf_sectiontoitem(cs),
843 "Invalid value for \"port\"");
848 sock->proto = IPPROTO_UDP;
851 if (cf_pair_find(cs, "proto")) {
853 cf_log_err(cf_sectiontoitem(cs),
854 "System does not support the TCP protocol. Delete this line from the configuration file.");
860 rcode = cf_item_parse(cs, "proto", PW_TYPE_STRING_PTR,
862 if (rcode < 0) return -1;
864 if (strcmp(proto, "udp") == 0) {
865 sock->proto = IPPROTO_UDP;
867 } else if (strcmp(proto, "tcp") == 0) {
868 sock->proto = IPPROTO_TCP;
870 rcode = cf_item_parse(cs, "max_connections", PW_TYPE_INTEGER,
871 &sock->max_connections, "64");
872 if (rcode < 0) return -1;
875 cf_log_err(cf_sectiontoitem(cs),
876 "Unknown proto name \"%s\"", proto);
883 * TCP requires a destination IP for sockets.
884 * UDP doesn't, so it's allowed.
886 if ((this->type == RAD_LISTEN_PROXY) &&
887 (sock->proto != IPPROTO_UDP)) {
888 cf_log_err(cf_sectiontoitem(cs),
889 "Proxy listeners can only listen on proto = udp");
895 sock->my_ipaddr = ipaddr;
896 sock->my_port = listen_port;
899 * If we can bind to interfaces, do so,
902 if (cf_pair_find(cs, "interface")) {
903 #ifndef SO_BINDTODEVICE
904 cf_log_err(cf_sectiontoitem(cs),
905 "System does not support binding to interfaces. Delete this line from the configuration file.");
909 CONF_PAIR *cp = cf_pair_find(cs, "interface");
911 rad_assert(cp != NULL);
912 value = cf_pair_value(cp);
914 cf_log_err(cf_sectiontoitem(cs),
915 "No interface name given");
918 sock->interface = value;
923 * And bind it to the port.
925 if (listen_bind(this) < 0) {
927 cf_log_err(cf_sectiontoitem(cs),
928 "Error binding to port for %s port %d",
929 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
936 * Proxy sockets don't have clients.
938 if (this->type == RAD_LISTEN_PROXY) return 0;
942 * The more specific configurations are preferred to more
946 parentcs = cf_top_section(cs);
947 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
948 §ion_name, NULL);
949 if (rcode < 0) return -1; /* bad string */
952 * Explicit list given: use it.
954 client_cs = cf_section_sub_find_name2(parentcs,
958 client_cs = cf_section_find(section_name);
961 cf_log_err(cf_sectiontoitem(cs),
962 "Failed to find clients %s {...}",
968 } /* else there was no "clients = " entry. */
971 CONF_SECTION *server_cs;
973 server_cs = cf_section_sub_find_name2(parentcs,
977 * Found a "server foo" section. If there are clients
981 (cf_section_sub_find(server_cs, "client") != NULL)) {
982 client_cs = server_cs;
987 * Still nothing. Look for global clients.
989 if (!client_cs) client_cs = parentcs;
991 sock->clients = clients_parse_section(client_cs);
992 if (!sock->clients) {
993 cf_log_err(cf_sectiontoitem(cs),
994 "Failed to load clients for this listen section");
999 if (sock->proto == IPPROTO_TCP) {
1001 * Re-write the listener receive function to
1002 * allow us to accept the socket.
1004 this->recv = auth_tcp_accept;
1012 * Send an authentication response packet
1014 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1016 rad_assert(request->listener == listener);
1017 rad_assert(listener->send == auth_socket_send);
1019 return rad_send(request->reply, request->packet,
1020 request->client->secret);
1024 #ifdef WITH_ACCOUNTING
1026 * Send an accounting response packet (or not)
1028 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1030 rad_assert(request->listener == listener);
1031 rad_assert(listener->send == acct_socket_send);
1034 * Accounting reject's are silently dropped.
1036 * We do it here to avoid polluting the rest of the
1037 * code with this knowledge
1039 if (request->reply->code == 0) return 0;
1041 return rad_send(request->reply, request->packet,
1042 request->client->secret);
1048 * Send a packet to a home server.
1050 * FIXME: have different code for proxy auth & acct!
1052 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1054 rad_assert(request->proxy_listener == listener);
1055 rad_assert(listener->send == proxy_socket_send);
1057 return rad_send(request->proxy, request->packet,
1058 request->home_server->secret);
1064 * Check if an incoming request is "ok"
1066 * It takes packets, not requests. It sees if the packet looks
1067 * OK. If so, it does a number of sanity checks on it.
1069 static int stats_socket_recv(rad_listen_t *listener,
1070 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1074 RADIUS_PACKET *packet;
1076 fr_ipaddr_t src_ipaddr;
1078 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1079 if (rcode < 0) return 0;
1081 RAD_STATS_TYPE_INC(listener, total_requests);
1083 if (rcode < 20) { /* AUTH_HDR_LEN */
1084 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1088 if ((client = client_listener_find(listener,
1089 &src_ipaddr, src_port)) == NULL) {
1090 rad_recv_discard(listener->fd);
1091 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1096 * We only understand Status-Server on this socket.
1098 if (code != PW_STATUS_SERVER) {
1099 DEBUG("Ignoring packet code %d sent to Status-Server port",
1101 rad_recv_discard(listener->fd);
1102 RAD_STATS_TYPE_INC(listener, total_unknown_types);
1103 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1108 * Now that we've sanity checked everything, receive the
1111 packet = rad_recv(listener->fd, 1); /* require message authenticator */
1113 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1114 DEBUG("%s", fr_strerror());
1118 if (!received_request(listener, packet, prequest, client)) {
1119 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1120 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1125 *pfun = rad_status_server;
1132 * Check if an incoming request is "ok"
1134 * It takes packets, not requests. It sees if the packet looks
1135 * OK. If so, it does a number of sanity checks on it.
1137 static int auth_socket_recv(rad_listen_t *listener,
1138 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1142 RADIUS_PACKET *packet;
1143 RAD_REQUEST_FUNP fun = NULL;
1145 fr_ipaddr_t src_ipaddr;
1147 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1148 if (rcode < 0) return 0;
1150 RAD_STATS_TYPE_INC(listener, total_requests);
1152 if (rcode < 20) { /* AUTH_HDR_LEN */
1153 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1157 if ((client = client_listener_find(listener,
1158 &src_ipaddr, src_port)) == NULL) {
1159 rad_recv_discard(listener->fd);
1160 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1165 * Some sanity checks, based on the packet code.
1168 case PW_AUTHENTICATION_REQUEST:
1169 RAD_STATS_CLIENT_INC(listener, client, total_requests);
1170 fun = rad_authenticate;
1173 case PW_STATUS_SERVER:
1174 if (!mainconfig.status_server) {
1175 rad_recv_discard(listener->fd);
1176 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1177 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1178 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1181 fun = rad_status_server;
1185 rad_recv_discard(listener->fd);
1186 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
1187 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1189 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
1190 code, client->shortname, src_port);
1193 } /* switch over packet types */
1196 * Now that we've sanity checked everything, receive the
1199 packet = rad_recv(listener->fd, client->message_authenticator);
1201 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1202 DEBUG("%s", fr_strerror());
1206 if (!received_request(listener, packet, prequest, client)) {
1207 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1208 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1218 #ifdef WITH_ACCOUNTING
1220 * Receive packets from an accounting socket
1222 static int acct_socket_recv(rad_listen_t *listener,
1223 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1227 RADIUS_PACKET *packet;
1228 RAD_REQUEST_FUNP fun = NULL;
1230 fr_ipaddr_t src_ipaddr;
1232 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1233 if (rcode < 0) return 0;
1235 RAD_STATS_TYPE_INC(listener, total_requests);
1237 if (rcode < 20) { /* AUTH_HDR_LEN */
1238 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1242 if ((client = client_listener_find(listener,
1243 &src_ipaddr, src_port)) == NULL) {
1244 rad_recv_discard(listener->fd);
1245 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1250 * Some sanity checks, based on the packet code.
1253 case PW_ACCOUNTING_REQUEST:
1254 RAD_STATS_CLIENT_INC(listener, client, total_requests);
1255 fun = rad_accounting;
1258 case PW_STATUS_SERVER:
1259 if (!mainconfig.status_server) {
1260 rad_recv_discard(listener->fd);
1261 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1262 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1264 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1267 fun = rad_status_server;
1271 rad_recv_discard(listener->fd);
1272 RAD_STATS_TYPE_INC(listener, total_unknown_types);
1273 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1275 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1276 code, client->shortname, src_port);
1278 } /* switch over packet types */
1281 * Now that we've sanity checked everything, receive the
1284 packet = rad_recv(listener->fd, 0);
1286 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1287 radlog(L_ERR, "%s", fr_strerror());
1292 * There can be no duplicate accounting packets.
1294 if (!received_request(listener, packet, prequest, client)) {
1295 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1296 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1309 * For now, all CoA requests are *only* originated, and not
1310 * proxied. So all of the necessary work is done in the
1311 * post-proxy section, which is automatically handled by event.c.
1312 * As a result, we don't have to do anything here.
1314 static int rad_coa_reply(REQUEST *request)
1316 VALUE_PAIR *s1, *s2;
1319 * Inform the user about RFC requirements.
1321 s1 = pairfind(request->proxy->vps, PW_STATE);
1323 s2 = pairfind(request->proxy_reply->vps, PW_STATE);
1326 DEBUG("WARNING: Client was sent State in CoA, and did not respond with State.");
1328 } else if ((s1->length != s2->length) ||
1329 (memcmp(s1->vp_octets, s2->vp_octets,
1330 s1->length) != 0)) {
1331 DEBUG("WARNING: Client was sent State in CoA, and did not respond with the same State.");
1335 return RLM_MODULE_OK;
1339 * Receive a CoA packet.
1341 static int rad_coa_recv(REQUEST *request)
1343 int rcode = RLM_MODULE_OK;
1348 * Get the correct response
1350 switch (request->packet->code) {
1351 case PW_COA_REQUEST:
1356 case PW_DISCONNECT_REQUEST:
1357 ack = PW_DISCONNECT_ACK;
1358 nak = PW_DISCONNECT_NAK;
1361 default: /* shouldn't happen */
1362 return RLM_MODULE_FAIL;
1366 #define WAS_PROXIED (request->proxy)
1368 #define WAS_PROXIED (0)
1373 * RFC 5176 Section 3.3. If we have a CoA-Request
1374 * with Service-Type = Authorize-Only, it MUST
1375 * have a State attribute in it.
1377 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE);
1378 if (request->packet->code == PW_COA_REQUEST) {
1379 if (vp && (vp->vp_integer == 17)) {
1380 vp = pairfind(request->packet->vps, PW_STATE);
1381 if (!vp || (vp->length == 0)) {
1382 RDEBUG("ERROR: CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1383 request->reply->code = PW_COA_NAK;
1384 return RLM_MODULE_FAIL;
1389 * RFC 5176, Section 3.2.
1391 RDEBUG("ERROR: Disconnect-Request MUST NOT contain a Service-Type attribute");
1392 request->reply->code = PW_DISCONNECT_NAK;
1393 return RLM_MODULE_FAIL;
1396 rcode = module_recv_coa(0, request);
1398 case RLM_MODULE_FAIL:
1399 case RLM_MODULE_INVALID:
1400 case RLM_MODULE_REJECT:
1401 case RLM_MODULE_USERLOCK:
1403 request->reply->code = nak;
1406 case RLM_MODULE_HANDLED:
1409 case RLM_MODULE_NOOP:
1410 case RLM_MODULE_NOTFOUND:
1412 case RLM_MODULE_UPDATED:
1413 request->reply->code = ack;
1418 * Start the reply code with the proxy reply
1421 request->reply->code = request->proxy_reply->code;
1425 * Copy State from the request to the reply.
1426 * See RFC 5176 Section 3.3.
1428 vp = paircopy2(request->packet->vps, PW_STATE);
1429 if (vp) pairadd(&request->reply->vps, vp);
1432 * We may want to over-ride the reply.
1434 rcode = module_send_coa(0, request);
1437 * We need to send CoA-NAK back if Service-Type
1438 * is Authorize-Only. Rely on the user's policy
1439 * to do that. We're not a real NAS, so this
1440 * restriction doesn't (ahem) apply to us.
1442 case RLM_MODULE_FAIL:
1443 case RLM_MODULE_INVALID:
1444 case RLM_MODULE_REJECT:
1445 case RLM_MODULE_USERLOCK:
1448 * Over-ride an ACK with a NAK
1450 request->reply->code = nak;
1453 case RLM_MODULE_HANDLED:
1456 case RLM_MODULE_NOOP:
1457 case RLM_MODULE_NOTFOUND:
1459 case RLM_MODULE_UPDATED:
1461 * Do NOT over-ride a previously set value.
1462 * Otherwise an "ok" here will re-write a
1465 if (request->reply->code == 0) {
1466 request->reply->code = ack;
1472 return RLM_MODULE_OK;
1477 * Check if an incoming request is "ok"
1479 * It takes packets, not requests. It sees if the packet looks
1480 * OK. If so, it does a number of sanity checks on it.
1482 static int coa_socket_recv(rad_listen_t *listener,
1483 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1487 RADIUS_PACKET *packet;
1488 RAD_REQUEST_FUNP fun = NULL;
1490 fr_ipaddr_t src_ipaddr;
1492 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1493 if (rcode < 0) return 0;
1495 RAD_STATS_TYPE_INC(listener, total_requests);
1497 if (rcode < 20) { /* AUTH_HDR_LEN */
1498 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1502 if ((client = client_listener_find(listener,
1503 &src_ipaddr, src_port)) == NULL) {
1504 rad_recv_discard(listener->fd);
1505 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1510 * Some sanity checks, based on the packet code.
1513 case PW_COA_REQUEST:
1514 case PW_DISCONNECT_REQUEST:
1519 rad_recv_discard(listener->fd);
1520 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1521 code, client->shortname, src_port);
1524 } /* switch over packet types */
1527 * Now that we've sanity checked everything, receive the
1530 packet = rad_recv(listener->fd, client->message_authenticator);
1532 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1533 DEBUG("%s", fr_strerror());
1537 if (!received_request(listener, packet, prequest, client)) {
1549 * Recieve packets from a proxy socket.
1551 static int proxy_socket_recv(rad_listen_t *listener,
1552 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1555 RADIUS_PACKET *packet;
1556 RAD_REQUEST_FUNP fun = NULL;
1559 packet = rad_recv(listener->fd, 0);
1561 radlog(L_ERR, "%s", fr_strerror());
1566 * FIXME: Client MIB updates?
1568 switch(packet->code) {
1569 case PW_AUTHENTICATION_ACK:
1570 case PW_ACCESS_CHALLENGE:
1571 case PW_AUTHENTICATION_REJECT:
1572 fun = rad_authenticate;
1575 #ifdef WITH_ACCOUNTING
1576 case PW_ACCOUNTING_RESPONSE:
1577 fun = rad_accounting;
1582 case PW_DISCONNECT_ACK:
1583 case PW_DISCONNECT_NAK:
1586 fun = rad_coa_reply;
1592 * FIXME: Update MIB for packet types?
1594 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1595 "from home server %s port %d - ID %d : IGNORED",
1597 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1598 packet->src_port, packet->id);
1603 request = received_proxy_response(packet);
1611 * Distinguish proxied CoA requests from ones we
1614 if ((fun == rad_coa_reply) &&
1615 (request->packet->code == request->proxy->code)) {
1620 rad_assert(fun != NULL);
1622 *prequest = request;
1629 * Recieve packets from a proxy socket.
1631 static int proxy_socket_tcp_recv(rad_listen_t *listener,
1632 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1635 RADIUS_PACKET *packet;
1636 RAD_REQUEST_FUNP fun = NULL;
1637 listen_socket_t *sock = listener->data;
1640 packet = fr_tcp_recv(listener->fd, 0);
1642 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
1643 event_new_fd(listener);
1648 * FIXME: Client MIB updates?
1650 switch(packet->code) {
1651 case PW_AUTHENTICATION_ACK:
1652 case PW_ACCESS_CHALLENGE:
1653 case PW_AUTHENTICATION_REJECT:
1654 fun = rad_authenticate;
1657 #ifdef WITH_ACCOUNTING
1658 case PW_ACCOUNTING_RESPONSE:
1659 fun = rad_accounting;
1665 * FIXME: Update MIB for packet types?
1667 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1668 "from home server %s port %d - ID %d : IGNORED",
1670 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1671 packet->src_port, packet->id);
1676 packet->src_ipaddr = sock->other_ipaddr;
1677 packet->src_port = sock->other_port;
1678 packet->dst_ipaddr = sock->my_ipaddr;
1679 packet->dst_port = sock->my_port;
1682 * FIXME: Have it return an indication of packets that
1683 * are OK to ignore (dups, too late), versus ones that
1684 * aren't OK to ignore (unknown response, spoofed, etc.)
1686 * Close the socket on bad packets...
1688 request = received_proxy_response(packet);
1693 rad_assert(fun != NULL);
1694 sock->opened = sock->last_packet = request->timestamp;
1697 *prequest = request;
1705 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1707 if (!request->reply->code) return 0;
1709 rad_encode(request->reply, request->packet,
1710 request->client->secret);
1711 rad_sign(request->reply, request->packet,
1712 request->client->secret);
1718 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1720 if (rad_verify(request->packet, NULL,
1721 request->client->secret) < 0) {
1725 return rad_decode(request->packet, NULL,
1726 request->client->secret);
1730 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1732 rad_encode(request->proxy, NULL, request->home_server->secret);
1733 rad_sign(request->proxy, NULL, request->home_server->secret);
1739 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1742 * rad_verify is run in event.c, received_proxy_response()
1745 return rad_decode(request->proxy_reply, request->proxy,
1746 request->home_server->secret);
1752 #include "command.c"
1754 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1756 { common_socket_parse, NULL,
1757 stats_socket_recv, auth_socket_send,
1758 socket_print, client_socket_encode, client_socket_decode },
1761 * This always gets defined.
1763 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1768 { common_socket_parse, NULL,
1769 proxy_socket_recv, proxy_socket_send,
1770 socket_print, proxy_socket_encode, proxy_socket_decode },
1773 /* authentication */
1774 { common_socket_parse, NULL,
1775 auth_socket_recv, auth_socket_send,
1776 socket_print, client_socket_encode, client_socket_decode },
1778 #ifdef WITH_ACCOUNTING
1780 { common_socket_parse, NULL,
1781 acct_socket_recv, acct_socket_send,
1782 socket_print, client_socket_encode, client_socket_decode},
1787 { detail_parse, detail_free,
1788 detail_recv, detail_send,
1789 detail_print, detail_encode, detail_decode },
1793 /* vlan query protocol */
1794 { common_socket_parse, NULL,
1795 vqp_socket_recv, vqp_socket_send,
1796 socket_print, vqp_socket_encode, vqp_socket_decode },
1800 /* dhcp query protocol */
1801 { dhcp_socket_parse, NULL,
1802 dhcp_socket_recv, dhcp_socket_send,
1803 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1806 #ifdef WITH_COMMAND_SOCKET
1807 /* TCP command socket */
1808 { command_socket_parse, NULL,
1809 command_domain_accept, command_domain_send,
1810 command_socket_print, command_socket_encode, command_socket_decode },
1814 /* Change of Authorization */
1815 { common_socket_parse, NULL,
1816 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
1817 socket_print, client_socket_encode, client_socket_decode },
1825 * Binds a listener to a socket.
1827 static int listen_bind(rad_listen_t *this)
1830 struct sockaddr_storage salocal;
1832 listen_socket_t *sock = this->data;
1834 #define proto_for_port "udp"
1835 #define sock_type SOCK_DGRAM
1837 const char *proto_for_port = "udp";
1838 int sock_type = SOCK_DGRAM;
1840 if (sock->proto == IPPROTO_TCP) {
1842 if (this->type == RAD_LISTEN_VQP) {
1843 radlog(L_ERR, "VQP does not support TCP transport");
1848 proto_for_port = "tcp";
1849 sock_type = SOCK_STREAM;
1854 * If the port is zero, then it means the appropriate
1855 * thing from /etc/services.
1857 if (sock->my_port == 0) {
1858 struct servent *svp;
1860 switch (this->type) {
1861 case RAD_LISTEN_AUTH:
1862 svp = getservbyname ("radius", proto_for_port);
1864 sock->my_port = ntohs(svp->s_port);
1866 sock->my_port = PW_AUTH_UDP_PORT;
1870 #ifdef WITH_ACCOUNTING
1871 case RAD_LISTEN_ACCT:
1872 svp = getservbyname ("radacct", proto_for_port);
1874 sock->my_port = ntohs(svp->s_port);
1876 sock->my_port = PW_ACCT_UDP_PORT;
1882 case RAD_LISTEN_PROXY:
1883 /* leave it at zero */
1888 case RAD_LISTEN_VQP:
1889 sock->my_port = 1589;
1894 case RAD_LISTEN_COA:
1895 sock->my_port = PW_COA_UDP_PORT;
1900 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1906 * Copy fr_socket() here, as we may need to bind to a device.
1908 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
1910 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
1914 #ifdef SO_BINDTODEVICE
1916 * Bind to a device BEFORE touching IP addresses.
1918 if (sock->interface) {
1920 strcpy(ifreq.ifr_name, sock->interface);
1923 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1924 (char *)&ifreq, sizeof(ifreq));
1928 radlog(L_ERR, "Failed binding to interface %s: %s",
1929 sock->interface, strerror(errno));
1931 } /* else it worked. */
1936 if (sock->proto == IPPROTO_TCP) {
1939 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
1941 radlog(L_ERR, "Failed to reuse address: %s", strerror(errno));
1947 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
1948 else /* UDP sockets get UDPfromto */
1951 #ifdef WITH_UDPFROMTO
1953 * Initialize udpfromto for all sockets.
1955 if (udpfromto_init(this->fd) != 0) {
1962 * Set up sockaddr stuff.
1964 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
1969 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1970 if (sock->my_ipaddr.af == AF_INET6) {
1972 * Listening on '::' does NOT get you IPv4 to
1973 * IPv6 mapping. You've got to listen on an IPv4
1974 * address, too. This makes the rest of the server
1975 * design a little simpler.
1979 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
1982 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
1983 (char *)&on, sizeof(on));
1985 #endif /* IPV6_V6ONLY */
1987 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
1989 if (sock->my_ipaddr.af == AF_INET) {
1992 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
1994 * Disable PMTU discovery. On Linux, this
1995 * also makes sure that the "don't fragment"
1998 flag = IP_PMTUDISC_DONT;
1999 setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2000 &flag, sizeof(flag));
2003 #if defined(IP_DONTFRAG)
2005 * Ensure that the "don't fragment" flag is zero.
2008 setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2009 &flag, sizeof(flag));
2014 * May be binding to priviledged ports.
2016 if (sock->my_port != 0) {
2018 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2024 this->print(this, buffer, sizeof(buffer));
2025 radlog(L_ERR, "Failed binding to %s: %s\n",
2026 buffer, strerror(errno));
2031 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2032 * kernel instead binds us to a 1.2.3.4. If this
2033 * happens, notice, and remember our real IP.
2036 struct sockaddr_storage src;
2037 socklen_t sizeof_src = sizeof(src);
2039 memset(&src, 0, sizeof_src);
2040 if (getsockname(this->fd, (struct sockaddr *) &src,
2042 radlog(L_ERR, "Failed getting socket name: %s",
2047 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2048 &sock->my_ipaddr, &sock->my_port)) {
2049 radlog(L_ERR, "Socket has unsupported address family");
2056 if (sock->proto == IPPROTO_TCP) {
2057 if (listen(this->fd, 8) < 0) {
2059 radlog(L_ERR, "Failed in listen(): %s", strerror(errno));
2065 if (fr_nonblock(this->fd) < 0) {
2067 radlog(L_ERR, "Failed setting non-blocking on socket: %s",
2073 * Mostly for proxy sockets.
2075 sock->other_ipaddr.af = sock->my_ipaddr.af;
2078 * Don't screw up other people.
2080 #undef proto_for_port
2088 * Allocate & initialize a new listener.
2090 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
2094 this = rad_malloc(sizeof(*this));
2095 memset(this, 0, sizeof(*this));
2098 this->recv = master_listen[this->type].recv;
2099 this->send = master_listen[this->type].send;
2100 this->print = master_listen[this->type].print;
2101 this->encode = master_listen[this->type].encode;
2102 this->decode = master_listen[this->type].decode;
2106 case RAD_LISTEN_NONE:
2108 case RAD_LISTEN_AUTH:
2109 #ifdef WITH_ACCOUNTING
2110 case RAD_LISTEN_ACCT:
2113 case RAD_LISTEN_PROXY:
2116 case RAD_LISTEN_VQP:
2119 case RAD_LISTEN_COA:
2121 this->data = rad_malloc(sizeof(listen_socket_t));
2122 memset(this->data, 0, sizeof(listen_socket_t));
2126 case RAD_LISTEN_DHCP:
2127 this->data = rad_malloc(sizeof(dhcp_socket_t));
2128 memset(this->data, 0, sizeof(dhcp_socket_t));
2133 case RAD_LISTEN_DETAIL:
2138 #ifdef WITH_COMMAND_SOCKET
2139 case RAD_LISTEN_COMMAND:
2140 this->data = rad_malloc(sizeof(fr_command_socket_t));
2141 memset(this->data, 0, sizeof(fr_command_socket_t));
2146 rad_assert("Unsupported option!" == NULL);
2155 * Externally visible function for creating a new proxy LISTENER.
2157 * Not thread-safe, but all calls to it are protected by the
2158 * proxy mutex in event.c
2160 int proxy_new_listener(home_server *home, int src_port)
2163 listen_socket_t *sock;
2165 if (!home) return 0;
2167 if ((home->max_connections > 0) &&
2168 (home->num_connections >= home->max_connections)) {
2169 DEBUG("WARNING: Home server has too many open connections (%d)",
2170 home->max_connections);
2174 this = listen_alloc(RAD_LISTEN_PROXY);
2177 sock->other_ipaddr = home->ipaddr;
2178 sock->other_port = home->port;
2181 sock->my_ipaddr = home->src_ipaddr;
2182 sock->my_port = src_port;
2185 sock->proto = home->proto;
2186 sock->last_packet = time(NULL);
2188 if (home->proto == IPPROTO_TCP) {
2189 this->recv = proxy_socket_tcp_recv;
2192 * FIXME: connect() is blocking!
2193 * We do this with the proxy mutex locked, which may
2194 * cause large delays!
2196 * http://www.developerweb.net/forum/showthread.php?p=13486
2198 this->fd = fr_tcp_client_socket(&home->src_ipaddr,
2199 &home->ipaddr, home->port);
2202 this->fd = fr_socket(&home->src_ipaddr, src_port);
2205 DEBUG("Failed opening client socket: %s", fr_strerror());
2211 * Figure out which port we were bound to.
2213 if (sock->my_port == 0) {
2214 struct sockaddr_storage src;
2215 socklen_t sizeof_src = sizeof(src);
2217 memset(&src, 0, sizeof_src);
2218 if (getsockname(this->fd, (struct sockaddr *) &src,
2220 radlog(L_ERR, "Failed getting socket name: %s",
2226 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2227 &sock->my_ipaddr, &sock->my_port)) {
2228 radlog(L_ERR, "Socket has unsupported address family");
2235 * Tell the event loop that we have a new FD
2244 static const FR_NAME_NUMBER listen_compare[] = {
2246 { "status", RAD_LISTEN_NONE },
2248 { "auth", RAD_LISTEN_AUTH },
2249 #ifdef WITH_ACCOUNTING
2250 { "acct", RAD_LISTEN_ACCT },
2253 { "detail", RAD_LISTEN_DETAIL },
2256 { "proxy", RAD_LISTEN_PROXY },
2259 { "vmps", RAD_LISTEN_VQP },
2262 { "dhcp", RAD_LISTEN_DHCP },
2264 #ifdef WITH_COMMAND_SOCKET
2265 { "control", RAD_LISTEN_COMMAND },
2268 { "coa", RAD_LISTEN_COA },
2274 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
2282 cf_log_info(cs, "listen {");
2284 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
2286 if (rcode < 0) return NULL;
2289 cf_log_err(cf_sectiontoitem(cs),
2290 "No type specified in listen section");
2294 type = fr_str2int(listen_compare, listen_type, -1);
2296 cf_log_err(cf_sectiontoitem(cs),
2297 "Invalid type \"%s\" in listen section.",
2305 * Allow listen sections in the default config to
2306 * refer to a server.
2309 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
2311 if (rcode == 1) { /* compatiblity with 2.0-pre */
2312 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
2315 if (rcode < 0) return NULL;
2320 * We were passed a virtual server, so the caller is
2321 * defining a proxy listener inside of a virtual server.
2322 * This isn't allowed right now.
2324 else if (this->type == RAD_LISTEN_PROXY) {
2325 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
2331 * Set up cross-type data.
2333 this = listen_alloc(type);
2334 this->server = server;
2338 * Call per-type parser.
2340 if (master_listen[type].parse(cs, this) < 0) {
2345 cf_log_info(cs, "}");
2351 * Generate a list of listeners. Takes an input list of
2352 * listeners, too, so we don't close sockets with waiting packets.
2354 int listen_init(CONF_SECTION *config, rad_listen_t **head)
2356 int override = FALSE;
2358 CONF_SECTION *cs = NULL;
2359 rad_listen_t **last;
2361 fr_ipaddr_t server_ipaddr;
2364 int defined_proxy = 0;
2368 * We shouldn't be called with a pre-existing list.
2370 rad_assert(head && (*head == NULL));
2373 server_ipaddr.af = AF_UNSPEC;
2376 * If the port is specified on the command-line,
2377 * it over-rides the configuration file.
2379 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
2381 if (mainconfig.port >= 0) auth_port = mainconfig.port;
2384 * If the IP address was configured on the command-line,
2385 * use that as the "bind_address"
2387 if (mainconfig.myip.af != AF_UNSPEC) {
2388 memcpy(&server_ipaddr, &mainconfig.myip,
2389 sizeof(server_ipaddr));
2395 * Else look for bind_address and/or listen sections.
2397 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
2398 rcode = cf_item_parse(config, "bind_address",
2400 &server_ipaddr.ipaddr.ip4addr, NULL);
2401 if (rcode < 0) return -1; /* error parsing it */
2403 if (rcode == 0) { /* successfully parsed IPv4 */
2404 listen_socket_t *sock;
2405 server_ipaddr.af = AF_INET;
2407 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'.");
2411 if (strcmp(progname, "vmpsd") == 0) {
2412 this = listen_alloc(RAD_LISTEN_VQP);
2413 if (!auth_port) auth_port = 1589;
2416 this = listen_alloc(RAD_LISTEN_AUTH);
2420 sock->my_ipaddr = server_ipaddr;
2421 sock->my_port = auth_port;
2423 sock->clients = clients_parse_section(config);
2424 if (!sock->clients) {
2425 cf_log_err(cf_sectiontoitem(config),
2426 "Failed to find any clients for this listen section");
2431 if (listen_bind(this) < 0) {
2433 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
2437 auth_port = sock->my_port; /* may have been updated in listen_bind */
2439 cs = cf_section_sub_find_name2(config, "server",
2441 if (cs) this->server = mainconfig.name;
2445 last = &(this->next);
2451 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
2454 #ifdef WITH_ACCOUNTING
2456 * Open Accounting Socket.
2458 * If we haven't already gotten acct_port from
2459 * /etc/services, then make it auth_port + 1.
2461 this = listen_alloc(RAD_LISTEN_ACCT);
2465 * Create the accounting socket.
2467 * The accounting port is always the
2468 * authentication port + 1
2470 sock->my_ipaddr = server_ipaddr;
2471 sock->my_port = auth_port + 1;
2473 sock->clients = clients_parse_section(config);
2474 if (!sock->clients) {
2475 cf_log_err(cf_sectiontoitem(config),
2476 "Failed to find any clients for this listen section");
2480 if (listen_bind(this) < 0) {
2483 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
2488 cs = cf_section_sub_find_name2(config, "server",
2490 if (cs) this->server = mainconfig.name;
2494 last = &(this->next);
2496 } else if (mainconfig.port > 0) { /* no bind address, but a port */
2497 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
2503 * They specified an IP on the command-line, ignore
2504 * all listen sections except the one in '-n'.
2506 if (mainconfig.myip.af != AF_UNSPEC) {
2507 CONF_SECTION *subcs;
2508 const char *name2 = cf_section_name2(cs);
2510 cs = cf_section_sub_find_name2(config, "server",
2512 if (!cs) goto add_sockets;
2515 * Should really abstract this code...
2517 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2519 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2520 this = listen_parse(subcs, name2);
2527 last = &(this->next);
2528 } /* loop over "listen" directives in server <foo> */
2534 * Walk through the "listen" sections, if they exist.
2536 for (cs = cf_subsection_find_next(config, NULL, "listen");
2538 cs = cf_subsection_find_next(config, cs, "listen")) {
2539 this = listen_parse(cs, NULL);
2546 last = &(this->next);
2550 * Check virtual servers for "listen" sections, too.
2552 * FIXME: Move to virtual server init?
2554 for (cs = cf_subsection_find_next(config, NULL, "server");
2556 cs = cf_subsection_find_next(config, cs, "server")) {
2557 CONF_SECTION *subcs;
2558 const char *name2 = cf_section_name2(cs);
2560 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2562 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2563 this = listen_parse(subcs, name2);
2570 last = &(this->next);
2571 } /* loop over "listen" directives in virtual servers */
2572 } /* loop over virtual servers */
2576 * Print out which sockets we're listening on, and
2577 * add them to the event list.
2579 for (this = *head; this != NULL; this = this->next) {
2581 if (this->type == RAD_LISTEN_PROXY) {
2590 * If we're proxying requests, open the proxy FD.
2591 * Otherwise, don't do anything.
2594 if ((mainconfig.proxy_requests == TRUE) &&
2595 (*head != NULL) && !defined_proxy) {
2596 listen_socket_t *sock = NULL;
2600 memset(&home, 0, sizeof(home));
2606 home.proto = IPPROTO_UDP;
2609 home.src_ipaddr = server_ipaddr;
2612 * Find the first authentication port,
2615 for (this = *head; this != NULL; this = this->next) {
2616 if (this->type == RAD_LISTEN_AUTH) {
2618 if (home.src_ipaddr.af == AF_UNSPEC) {
2619 home.src_ipaddr = sock->my_ipaddr;
2621 port = sock->my_port + 2;
2625 if (this->type == RAD_LISTEN_ACCT) {
2627 if (home.src_ipaddr.af == AF_UNSPEC) {
2628 home.src_ipaddr = sock->my_ipaddr;
2630 port = sock->my_port + 1;
2637 * Address is still unspecified, use IPv4.
2639 if (home.src_ipaddr.af == AF_UNSPEC) {
2640 home.src_ipaddr.af = AF_INET;
2641 /* everything else is already set to zero */
2644 home.ipaddr.af = home.src_ipaddr.af;
2645 /* everything else is already set to zero */
2647 if (!proxy_new_listener(&home, port)) {
2655 * Haven't defined any sockets. Die.
2657 if (!*head) return -1;
2664 * Free a linked list of listeners;
2666 void listen_free(rad_listen_t **head)
2670 if (!head || !*head) return;
2674 rad_listen_t *next = this->next;
2677 * Other code may have eaten the FD.
2679 if (this->fd >= 0) close(this->fd);
2681 if (master_listen[this->type].free) {
2682 master_listen[this->type].free(this);
2686 if ((this->type == RAD_LISTEN_AUTH) ||
2688 (this->type == RAD_LISTEN_ACCT) ||
2691 (this->type == RAD_LISTEN_PROXY)
2694 listen_socket_t *sock = this->data;
2695 rad_free(&sock->packet);
2709 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
2714 for (this = mainconfig.listen; this != NULL; this = this->next) {
2715 listen_socket_t *sock;
2717 if ((this->type != RAD_LISTEN_AUTH) &&
2718 (this->type != RAD_LISTEN_ACCT)) continue;
2722 if ((sock->my_port == port) &&
2723 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
2724 return sock->clients;
2732 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
2736 for (this = mainconfig.listen; this != NULL; this = this->next) {
2737 listen_socket_t *sock;
2740 * FIXME: For TCP, ignore the *secondary*
2741 * listeners associated with the main socket.
2743 if ((this->type != RAD_LISTEN_AUTH) &&
2744 (this->type != RAD_LISTEN_ACCT)) continue;
2748 if ((sock->my_port == port) &&
2749 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
2753 if ((sock->my_port == port) &&
2754 ((sock->my_ipaddr.af == AF_INET) &&
2755 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == INADDR_ANY))) {
2759 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2760 if ((sock->my_port == port) &&
2761 (sock->my_ipaddr.af == AF_INET6) &&
2762 (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr))) {