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(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);
205 } else if (!client->dynamic && client->rate_limit) {
207 * The IP is unknown, so we've found an enclosing
208 * network. Enable DoS protection. We only
209 * allow one new client per second. Known
210 * clients aren't subject to this restriction.
212 if (now == client->last_new_client) goto unknown;
215 client->last_new_client = now;
217 request = request_alloc();
218 if (!request) goto unknown;
220 request->listener = listener;
221 request->client = client;
222 request->packet = rad_recv(listener->fd, 0x02); /* MSG_PEEK */
223 if (!request->packet) { /* badly formed, etc */
224 request_free(&request);
227 request->reply = rad_alloc_reply(request->packet);
228 if (!request->reply) {
229 request_free(&request);
232 request->packet->timestamp = request->timestamp;
234 request->priority = listener->type;
235 request->server = client->client_server;
236 request->root = &mainconfig;
239 * Run a fake request through the given virtual server.
240 * Look for FreeRADIUS-Client-IP-Address
241 * FreeRADIUS-Client-Secret
244 * and create the RADCLIENT structure from that.
246 DEBUG("server %s {", request->server);
248 rcode = module_authorize(0, request);
250 DEBUG("} # server %s", request->server);
252 if (rcode != RLM_MODULE_OK) {
253 request_free(&request);
258 * If the client was updated by rlm_dynamic_clients,
259 * don't create the client from attribute-value pairs.
261 if (request->client == client) {
262 created = client_create(clients, request);
264 created = request->client;
267 * This frees the client if it isn't valid.
269 if (!client_validate(clients, client, created)) goto unknown;
271 request_free(&request);
273 if (!created) goto unknown;
279 static int listen_bind(rad_listen_t *this);
283 * Process and reply to a server-status request.
284 * Like rad_authenticate and rad_accounting this should
285 * live in it's own file but it's so small we don't bother.
287 static int rad_status_server(REQUEST *request)
289 int rcode = RLM_MODULE_OK;
292 switch (request->listener->type) {
294 case RAD_LISTEN_NONE:
296 case RAD_LISTEN_AUTH:
297 dval = dict_valbyname(PW_AUTZ_TYPE, 0, "Status-Server");
299 rcode = module_authorize(dval->value, request);
301 rcode = RLM_MODULE_OK;
306 case RLM_MODULE_UPDATED:
307 request->reply->code = PW_AUTHENTICATION_ACK;
310 case RLM_MODULE_FAIL:
311 case RLM_MODULE_HANDLED:
312 request->reply->code = 0; /* don't reply */
316 case RLM_MODULE_REJECT:
317 request->reply->code = PW_AUTHENTICATION_REJECT;
322 #ifdef WITH_ACCOUNTING
323 case RAD_LISTEN_ACCT:
324 dval = dict_valbyname(PW_ACCT_TYPE, 0, "Status-Server");
326 rcode = module_accounting(dval->value, request);
328 rcode = RLM_MODULE_OK;
333 case RLM_MODULE_UPDATED:
334 request->reply->code = PW_ACCOUNTING_RESPONSE;
338 request->reply->code = 0; /* don't reply */
346 * This is a vendor extension. Suggested by Glen
347 * Zorn in IETF 72, and rejected by the rest of
348 * the WG. We like it, so it goes in here.
351 dval = dict_valbyname(PW_RECV_COA_TYPE, 0, "Status-Server");
353 rcode = module_recv_coa(dval->value, request);
355 rcode = RLM_MODULE_OK;
360 case RLM_MODULE_UPDATED:
361 request->reply->code = PW_COA_ACK;
365 request->reply->code = 0; /* don't reply */
377 * Full statistics are available only on a statistics
380 if (request->listener->type == RAD_LISTEN_NONE) {
381 request_stats_reply(request);
389 static int auth_tcp_recv(rad_listen_t *listener,
390 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
393 RADIUS_PACKET *packet;
394 RAD_REQUEST_FUNP fun = NULL;
395 listen_socket_t *sock = listener->data;
396 RADCLIENT *client = sock->client;
399 * Allocate a packet for partial reads.
402 sock->packet = rad_alloc(0);
403 if (!sock->packet) return 0;
405 sock->packet->sockfd = listener->fd;
406 sock->packet->src_ipaddr = sock->other_ipaddr;
407 sock->packet->src_port = sock->other_port;
408 sock->packet->dst_ipaddr = sock->my_ipaddr;
409 sock->packet->dst_port = sock->my_port;
413 * Grab the packet currently being processed.
415 packet = sock->packet;
417 rcode = fr_tcp_read_packet(packet, 0);
420 * Still only a partial packet. Put it back, and return,
421 * so that we'll read more data when it's ready.
427 if (rcode == -1) { /* error reading packet */
430 radlog(L_ERR, "Invalid packet from %s port %d: closing socket",
431 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
435 if (rcode < 0) { /* error or connection reset */
436 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
439 * Decrement the number of connections.
441 if (sock->parent->num_connections > 0) {
442 sock->parent->num_connections--;
444 if (sock->client->num_connections > 0) {
445 sock->client->num_connections--;
449 * Tell the event handler that an FD has disappeared.
451 DEBUG("Client has closed connection");
452 event_new_fd(listener);
455 * Do NOT free the listener here. It's in use by
456 * a request, and will need to hang around until
457 * all of the requests are done.
459 * It is instead free'd in remove_from_request_hash()
464 RAD_STATS_TYPE_INC(listener, total_requests);
467 * Some sanity checks, based on the packet code.
469 switch(packet->code) {
470 case PW_AUTHENTICATION_REQUEST:
471 RAD_STATS_CLIENT_INC(listener, client, total_requests);
472 fun = rad_authenticate;
475 case PW_STATUS_SERVER:
476 if (!mainconfig.status_server) {
477 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
478 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
479 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
480 rad_free(&sock->packet);
483 fun = rad_status_server;
487 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
488 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
490 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
491 packet->code, client->shortname, packet->src_port);
492 rad_free(&sock->packet);
494 } /* switch over packet types */
496 if (!received_request(listener, packet, prequest, sock->client)) {
497 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
498 RAD_STATS_CLIENT_INC(listener, sock->client, total_packets_dropped);
499 rad_free(&sock->packet);
504 sock->packet = NULL; /* we have no need for more partial reads */
508 static int auth_tcp_accept(rad_listen_t *listener,
509 UNUSED RAD_REQUEST_FUNP *pfun,
510 UNUSED REQUEST **prequest)
515 struct sockaddr_storage src;
516 listen_socket_t *sock;
517 fr_ipaddr_t src_ipaddr;
522 DEBUG2(" ... new connection request on TCP socket.");
524 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
527 * Non-blocking sockets must handle this.
529 if (errno == EWOULDBLOCK) {
533 DEBUG2(" ... failed to accept connection.");
537 if (!fr_sockaddr2ipaddr(&src, salen, &src_ipaddr, &src_port)) {
538 DEBUG2(" ... unknown address family.");
543 * Enforce client IP address checks on accept, not on
546 if ((client = client_listener_find(listener,
547 &src_ipaddr, src_port)) == NULL) {
549 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
554 * Enforce max_connectionsx on client && listen section.
556 if ((client->max_connections != 0) &&
557 (client->max_connections == client->num_connections)) {
559 * FIXME: Print client IP/port, and server IP/port.
561 radlog(L_INFO, "Ignoring new connection due to client max_connections (%d)", client->max_connections);
566 sock = listener->data;
567 if ((sock->max_connections != 0) &&
568 (sock->max_connections == sock->num_connections)) {
570 * FIXME: Print client IP/port, and server IP/port.
572 radlog(L_INFO, "Ignoring new connection due to socket max_connections");
576 client->num_connections++;
577 sock->num_connections++;
580 * Add the new listener.
582 this = listen_alloc(listener->type);
583 if (!this) return -1;
586 * Copy everything, including the pointer to the socket
590 memcpy(this->data, listener->data, sizeof(*sock));
591 memcpy(this, listener, sizeof(*this));
593 this->data = sock; /* fix it back */
595 sock->parent = listener->data;
596 sock->other_ipaddr = src_ipaddr;
597 sock->other_port = src_port;
598 sock->client = client;
601 this->status = RAD_LISTEN_STATUS_INIT;
602 this->recv = auth_tcp_recv;
605 * FIXME: set O_NONBLOCK on the accept'd fd.
606 * See djb's portability rants for details.
610 * Tell the event loop that we have a new FD.
611 * This can be called from a child thread...
621 * This function is stupid and complicated.
623 static int socket_print(const rad_listen_t *this, char *buffer, size_t bufsize)
626 listen_socket_t *sock = this->data;
629 switch (this->type) {
631 case RAD_LISTEN_NONE: /* what a hack... */
636 case RAD_LISTEN_AUTH:
637 name = "authentication";
640 #ifdef WITH_ACCOUNTING
641 case RAD_LISTEN_ACCT:
647 case RAD_LISTEN_PROXY:
659 case RAD_LISTEN_DHCP:
675 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
676 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
680 if (sock->interface) {
681 ADDSTRING(" interface ");
682 ADDSTRING(sock->interface);
686 if (this->recv == auth_tcp_accept) {
687 ADDSTRING(" proto tcp");
693 * TCP sockets get printed a little differently, to make
694 * it clear what's going on.
697 ADDSTRING(" from client (");
698 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
702 snprintf(buffer, bufsize, "%d", sock->other_port);
706 if ((sock->my_ipaddr.af == AF_INET) &&
707 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
708 strlcpy(buffer, "*", bufsize);
710 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
715 snprintf(buffer, bufsize, "%d", sock->my_port);
719 ADDSTRING(", virtual-server=");
720 ADDSTRING(this->server);
730 * Maybe it's a socket that we opened to a home server.
732 if ((sock->proto == IPPROTO_TCP) &&
733 (this->type == RAD_LISTEN_PROXY)) {
735 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
739 snprintf(buffer, bufsize, "%d", sock->my_port);
741 ADDSTRING(") -> home_server (");
743 if ((sock->other_ipaddr.af == AF_INET) &&
744 (sock->other_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
745 strlcpy(buffer, "*", bufsize);
747 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
752 snprintf(buffer, bufsize, "%d", sock->other_port);
759 #endif /* WITH_PROXY */
760 #endif /* WITH_TCP */
762 ADDSTRING(" address ");
764 if ((sock->my_ipaddr.af == AF_INET) &&
765 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
766 strlcpy(buffer, "*", bufsize);
768 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
773 snprintf(buffer, bufsize, "%d", sock->my_port);
777 ADDSTRING(" as server ");
778 ADDSTRING(this->server);
787 extern int check_config; /* radiusd.c */
791 * Parse an authentication or accounting socket.
793 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
798 listen_socket_t *sock = this->data;
799 char *section_name = NULL;
800 CONF_SECTION *client_cs, *parentcs;
805 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
806 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
807 &ipaddr.ipaddr.ip4addr, NULL);
808 if (rcode < 0) return -1;
810 if (rcode == 0) { /* successfully parsed IPv4 */
813 } else { /* maybe IPv6? */
814 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
815 &ipaddr.ipaddr.ip6addr, NULL);
816 if (rcode < 0) return -1;
819 cf_log_err(cf_sectiontoitem(cs),
820 "No address specified in listen section");
823 ipaddr.af = AF_INET6;
826 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
828 if (rcode < 0) return -1;
830 if ((listen_port < 0) || (listen_port > 65535)) {
831 cf_log_err(cf_sectiontoitem(cs),
832 "Invalid value for \"port\"");
836 sock->proto = IPPROTO_UDP;
838 if (cf_pair_find(cs, "proto")) {
840 cf_log_err(cf_sectiontoitem(cs),
841 "System does not support the TCP protocol. Delete this line from the configuration file.");
847 rcode = cf_item_parse(cs, "proto", PW_TYPE_STRING_PTR,
849 if (rcode < 0) return -1;
851 if (strcmp(proto, "udp") == 0) {
852 sock->proto = IPPROTO_UDP;
854 } else if (strcmp(proto, "tcp") == 0) {
855 sock->proto = IPPROTO_TCP;
857 rcode = cf_item_parse(cs, "max_connections", PW_TYPE_INTEGER,
858 &sock->max_connections, "64");
859 if (rcode < 0) return -1;
862 cf_log_err(cf_sectiontoitem(cs),
863 "Unknown proto name \"%s\"", proto);
870 * TCP requires a destination IP for sockets.
871 * UDP doesn't, so it's allowed.
874 if ((this->type == RAD_LISTEN_PROXY) &&
875 (sock->proto != IPPROTO_UDP)) {
876 cf_log_err(cf_sectiontoitem(cs),
877 "Proxy listeners can only listen on proto = udp");
880 #endif /* WITH_PROXY */
881 #endif /* WITH_TCP */
884 sock->my_ipaddr = ipaddr;
885 sock->my_port = listen_port;
889 if (home_server_find(&sock->my_ipaddr, sock->my_port, sock->proto)) {
892 DEBUG("ERROR: We have been asked to listen on %s port %d, which is also listed as a home server. This can create a proxy loop.",
893 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
898 return 0; /* don't do anything */
903 * If we can bind to interfaces, do so,
906 if (cf_pair_find(cs, "interface")) {
908 CONF_PAIR *cp = cf_pair_find(cs, "interface");
910 rad_assert(cp != NULL);
911 value = cf_pair_value(cp);
913 cf_log_err(cf_sectiontoitem(cs),
914 "No interface name given");
917 sock->interface = value;
922 * If we can do broadcasts..
924 if (cf_pair_find(cs, "broadcast")) {
926 cf_log_err(cf_sectiontoitem(cs),
927 "System does not support broadcast sockets. Delete this line from the configuration file.");
931 CONF_PAIR *cp = cf_pair_find(cs, "broadcast");
933 if (this->type != RAD_LISTEN_DHCP) {
934 cf_log_err(cf_pairtoitem(cp),
935 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file.");
939 rad_assert(cp != NULL);
940 value = cf_pair_value(cp);
942 cf_log_err(cf_sectiontoitem(cs),
943 "No broadcast value given");
948 * Hack... whatever happened to cf_section_parse?
950 sock->broadcast = (strcmp(value, "yes") == 0);
956 * And bind it to the port.
958 if (listen_bind(this) < 0) {
960 cf_log_err(cf_sectiontoitem(cs),
961 "Error binding to port for %s port %d",
962 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
969 * Proxy sockets don't have clients.
971 if (this->type == RAD_LISTEN_PROXY) return 0;
975 * The more specific configurations are preferred to more
979 parentcs = cf_top_section(cs);
980 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
981 §ion_name, NULL);
982 if (rcode < 0) return -1; /* bad string */
985 * Explicit list given: use it.
987 client_cs = cf_section_sub_find_name2(parentcs,
991 client_cs = cf_section_find(section_name);
994 cf_log_err(cf_sectiontoitem(cs),
995 "Failed to find clients %s {...}",
1001 } /* else there was no "clients = " entry. */
1004 CONF_SECTION *server_cs;
1006 server_cs = cf_section_sub_find_name2(parentcs,
1010 * Found a "server foo" section. If there are clients
1014 (cf_section_sub_find(server_cs, "client") != NULL)) {
1015 client_cs = server_cs;
1020 * Still nothing. Look for global clients.
1022 if (!client_cs) client_cs = parentcs;
1024 sock->clients = clients_parse_section(client_cs);
1025 if (!sock->clients) {
1026 cf_log_err(cf_sectiontoitem(cs),
1027 "Failed to load clients for this listen section");
1032 if (sock->proto == IPPROTO_TCP) {
1034 * Re-write the listener receive function to
1035 * allow us to accept the socket.
1037 this->recv = auth_tcp_accept;
1045 * Send an authentication response packet
1047 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1049 rad_assert(request->listener == listener);
1050 rad_assert(listener->send == auth_socket_send);
1052 return rad_send(request->reply, request->packet,
1053 request->client->secret);
1057 #ifdef WITH_ACCOUNTING
1059 * Send an accounting response packet (or not)
1061 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1063 rad_assert(request->listener == listener);
1064 rad_assert(listener->send == acct_socket_send);
1067 * Accounting reject's are silently dropped.
1069 * We do it here to avoid polluting the rest of the
1070 * code with this knowledge
1072 if (request->reply->code == 0) return 0;
1074 return rad_send(request->reply, request->packet,
1075 request->client->secret);
1081 * Send a packet to a home server.
1083 * FIXME: have different code for proxy auth & acct!
1085 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1087 rad_assert(request->proxy_listener == listener);
1088 rad_assert(listener->send == proxy_socket_send);
1090 return rad_send(request->proxy, request->packet,
1091 request->home_server->secret);
1097 * Check if an incoming request is "ok"
1099 * It takes packets, not requests. It sees if the packet looks
1100 * OK. If so, it does a number of sanity checks on it.
1102 static int stats_socket_recv(rad_listen_t *listener,
1103 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1107 RADIUS_PACKET *packet;
1109 fr_ipaddr_t src_ipaddr;
1111 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1112 if (rcode < 0) return 0;
1114 RAD_STATS_TYPE_INC(listener, total_requests);
1116 if (rcode < 20) { /* AUTH_HDR_LEN */
1117 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1121 if ((client = client_listener_find(listener,
1122 &src_ipaddr, src_port)) == NULL) {
1123 rad_recv_discard(listener->fd);
1124 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1129 * We only understand Status-Server on this socket.
1131 if (code != PW_STATUS_SERVER) {
1132 DEBUG("Ignoring packet code %d sent to Status-Server port",
1134 rad_recv_discard(listener->fd);
1135 RAD_STATS_TYPE_INC(listener, total_unknown_types);
1136 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1141 * Now that we've sanity checked everything, receive the
1144 packet = rad_recv(listener->fd, 1); /* require message authenticator */
1146 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1147 DEBUG("%s", fr_strerror());
1151 if (!received_request(listener, packet, prequest, client)) {
1152 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1153 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1158 *pfun = rad_status_server;
1165 * Check if an incoming request is "ok"
1167 * It takes packets, not requests. It sees if the packet looks
1168 * OK. If so, it does a number of sanity checks on it.
1170 static int auth_socket_recv(rad_listen_t *listener,
1171 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1175 RADIUS_PACKET *packet;
1176 RAD_REQUEST_FUNP fun = NULL;
1178 fr_ipaddr_t src_ipaddr;
1180 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1181 if (rcode < 0) return 0;
1183 RAD_STATS_TYPE_INC(listener, total_requests);
1185 if (rcode < 20) { /* AUTH_HDR_LEN */
1186 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1190 if ((client = client_listener_find(listener,
1191 &src_ipaddr, src_port)) == NULL) {
1192 rad_recv_discard(listener->fd);
1193 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1198 * Some sanity checks, based on the packet code.
1201 case PW_AUTHENTICATION_REQUEST:
1202 RAD_STATS_CLIENT_INC(listener, client, total_requests);
1203 fun = rad_authenticate;
1206 case PW_STATUS_SERVER:
1207 if (!mainconfig.status_server) {
1208 rad_recv_discard(listener->fd);
1209 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1210 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1211 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1214 fun = rad_status_server;
1218 rad_recv_discard(listener->fd);
1219 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
1220 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1222 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
1223 code, client->shortname, src_port);
1226 } /* switch over packet types */
1229 * Now that we've sanity checked everything, receive the
1232 packet = rad_recv(listener->fd, client->message_authenticator);
1234 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1235 DEBUG("%s", fr_strerror());
1239 if (!received_request(listener, packet, prequest, client)) {
1240 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1241 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1251 #ifdef WITH_ACCOUNTING
1253 * Receive packets from an accounting socket
1255 static int acct_socket_recv(rad_listen_t *listener,
1256 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1260 RADIUS_PACKET *packet;
1261 RAD_REQUEST_FUNP fun = NULL;
1263 fr_ipaddr_t src_ipaddr;
1265 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1266 if (rcode < 0) return 0;
1268 RAD_STATS_TYPE_INC(listener, total_requests);
1270 if (rcode < 20) { /* AUTH_HDR_LEN */
1271 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1275 if ((client = client_listener_find(listener,
1276 &src_ipaddr, src_port)) == NULL) {
1277 rad_recv_discard(listener->fd);
1278 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1283 * Some sanity checks, based on the packet code.
1286 case PW_ACCOUNTING_REQUEST:
1287 RAD_STATS_CLIENT_INC(listener, client, total_requests);
1288 fun = rad_accounting;
1291 case PW_STATUS_SERVER:
1292 if (!mainconfig.status_server) {
1293 rad_recv_discard(listener->fd);
1294 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1295 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1297 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1300 fun = rad_status_server;
1304 rad_recv_discard(listener->fd);
1305 RAD_STATS_TYPE_INC(listener, total_unknown_types);
1306 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1308 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1309 code, client->shortname, src_port);
1311 } /* switch over packet types */
1314 * Now that we've sanity checked everything, receive the
1317 packet = rad_recv(listener->fd, 0);
1319 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1320 radlog(L_ERR, "%s", fr_strerror());
1325 * There can be no duplicate accounting packets.
1327 if (!received_request(listener, packet, prequest, client)) {
1328 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1329 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1342 * For now, all CoA requests are *only* originated, and not
1343 * proxied. So all of the necessary work is done in the
1344 * post-proxy section, which is automatically handled by event.c.
1345 * As a result, we don't have to do anything here.
1347 static int rad_coa_reply(REQUEST *request)
1349 VALUE_PAIR *s1, *s2;
1352 * Inform the user about RFC requirements.
1354 s1 = pairfind(request->proxy->vps, PW_STATE, 0);
1356 s2 = pairfind(request->proxy_reply->vps, PW_STATE, 0);
1359 DEBUG("WARNING: Client was sent State in CoA, and did not respond with State.");
1361 } else if ((s1->length != s2->length) ||
1362 (memcmp(s1->vp_octets, s2->vp_octets,
1363 s1->length) != 0)) {
1364 DEBUG("WARNING: Client was sent State in CoA, and did not respond with the same State.");
1368 return RLM_MODULE_OK;
1372 * Receive a CoA packet.
1374 static int rad_coa_recv(REQUEST *request)
1376 int rcode = RLM_MODULE_OK;
1381 * Get the correct response
1383 switch (request->packet->code) {
1384 case PW_COA_REQUEST:
1389 case PW_DISCONNECT_REQUEST:
1390 ack = PW_DISCONNECT_ACK;
1391 nak = PW_DISCONNECT_NAK;
1394 default: /* shouldn't happen */
1395 return RLM_MODULE_FAIL;
1399 #define WAS_PROXIED (request->proxy)
1401 #define WAS_PROXIED (0)
1406 * RFC 5176 Section 3.3. If we have a CoA-Request
1407 * with Service-Type = Authorize-Only, it MUST
1408 * have a State attribute in it.
1410 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE, 0);
1411 if (request->packet->code == PW_COA_REQUEST) {
1412 if (vp && (vp->vp_integer == 17)) {
1413 vp = pairfind(request->packet->vps, PW_STATE, 0);
1414 if (!vp || (vp->length == 0)) {
1415 RDEBUG("ERROR: CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1416 request->reply->code = PW_COA_NAK;
1417 return RLM_MODULE_FAIL;
1422 * RFC 5176, Section 3.2.
1424 RDEBUG("ERROR: Disconnect-Request MUST NOT contain a Service-Type attribute");
1425 request->reply->code = PW_DISCONNECT_NAK;
1426 return RLM_MODULE_FAIL;
1429 rcode = module_recv_coa(0, request);
1431 case RLM_MODULE_FAIL:
1432 case RLM_MODULE_INVALID:
1433 case RLM_MODULE_REJECT:
1434 case RLM_MODULE_USERLOCK:
1436 request->reply->code = nak;
1439 case RLM_MODULE_HANDLED:
1442 case RLM_MODULE_NOOP:
1443 case RLM_MODULE_NOTFOUND:
1445 case RLM_MODULE_UPDATED:
1446 request->reply->code = ack;
1451 * Start the reply code with the proxy reply
1454 request->reply->code = request->proxy_reply->code;
1458 * Copy State from the request to the reply.
1459 * See RFC 5176 Section 3.3.
1461 vp = paircopy2(request->packet->vps, PW_STATE, 0);
1462 if (vp) pairadd(&request->reply->vps, vp);
1465 * We may want to over-ride the reply.
1467 rcode = module_send_coa(0, request);
1470 * We need to send CoA-NAK back if Service-Type
1471 * is Authorize-Only. Rely on the user's policy
1472 * to do that. We're not a real NAS, so this
1473 * restriction doesn't (ahem) apply to us.
1475 case RLM_MODULE_FAIL:
1476 case RLM_MODULE_INVALID:
1477 case RLM_MODULE_REJECT:
1478 case RLM_MODULE_USERLOCK:
1481 * Over-ride an ACK with a NAK
1483 request->reply->code = nak;
1486 case RLM_MODULE_HANDLED:
1489 case RLM_MODULE_NOOP:
1490 case RLM_MODULE_NOTFOUND:
1492 case RLM_MODULE_UPDATED:
1494 * Do NOT over-ride a previously set value.
1495 * Otherwise an "ok" here will re-write a
1498 if (request->reply->code == 0) {
1499 request->reply->code = ack;
1505 return RLM_MODULE_OK;
1510 * Check if an incoming request is "ok"
1512 * It takes packets, not requests. It sees if the packet looks
1513 * OK. If so, it does a number of sanity checks on it.
1515 static int coa_socket_recv(rad_listen_t *listener,
1516 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1520 RADIUS_PACKET *packet;
1521 RAD_REQUEST_FUNP fun = NULL;
1523 fr_ipaddr_t src_ipaddr;
1525 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1526 if (rcode < 0) return 0;
1528 RAD_STATS_TYPE_INC(listener, total_requests);
1530 if (rcode < 20) { /* AUTH_HDR_LEN */
1531 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1535 if ((client = client_listener_find(listener,
1536 &src_ipaddr, src_port)) == NULL) {
1537 rad_recv_discard(listener->fd);
1538 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1543 * Some sanity checks, based on the packet code.
1546 case PW_COA_REQUEST:
1547 case PW_DISCONNECT_REQUEST:
1552 rad_recv_discard(listener->fd);
1553 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1554 code, client->shortname, src_port);
1557 } /* switch over packet types */
1560 * Now that we've sanity checked everything, receive the
1563 packet = rad_recv(listener->fd, client->message_authenticator);
1565 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1566 DEBUG("%s", fr_strerror());
1570 if (!received_request(listener, packet, prequest, client)) {
1582 * Recieve packets from a proxy socket.
1584 static int proxy_socket_recv(rad_listen_t *listener,
1585 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1588 RADIUS_PACKET *packet;
1589 RAD_REQUEST_FUNP fun = NULL;
1592 packet = rad_recv(listener->fd, 0);
1594 radlog(L_ERR, "%s", fr_strerror());
1599 * FIXME: Client MIB updates?
1601 switch(packet->code) {
1602 case PW_AUTHENTICATION_ACK:
1603 case PW_ACCESS_CHALLENGE:
1604 case PW_AUTHENTICATION_REJECT:
1605 fun = rad_authenticate;
1608 #ifdef WITH_ACCOUNTING
1609 case PW_ACCOUNTING_RESPONSE:
1610 fun = rad_accounting;
1615 case PW_DISCONNECT_ACK:
1616 case PW_DISCONNECT_NAK:
1619 fun = rad_coa_reply;
1625 * FIXME: Update MIB for packet types?
1627 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1628 "from home server %s port %d - ID %d : IGNORED",
1630 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1631 packet->src_port, packet->id);
1636 request = received_proxy_response(packet);
1644 * Distinguish proxied CoA requests from ones we
1647 if ((fun == rad_coa_reply) &&
1648 (request->packet->code == request->proxy->code)) {
1653 rad_assert(fun != NULL);
1655 *prequest = request;
1662 * Recieve packets from a proxy socket.
1664 static int proxy_socket_tcp_recv(rad_listen_t *listener,
1665 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1668 RADIUS_PACKET *packet;
1669 RAD_REQUEST_FUNP fun = NULL;
1670 listen_socket_t *sock = listener->data;
1673 packet = fr_tcp_recv(listener->fd, 0);
1675 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
1676 event_new_fd(listener);
1681 * FIXME: Client MIB updates?
1683 switch(packet->code) {
1684 case PW_AUTHENTICATION_ACK:
1685 case PW_ACCESS_CHALLENGE:
1686 case PW_AUTHENTICATION_REJECT:
1687 fun = rad_authenticate;
1690 #ifdef WITH_ACCOUNTING
1691 case PW_ACCOUNTING_RESPONSE:
1692 fun = rad_accounting;
1698 * FIXME: Update MIB for packet types?
1700 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1701 "from home server %s port %d - ID %d : IGNORED",
1703 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1704 packet->src_port, packet->id);
1709 packet->src_ipaddr = sock->other_ipaddr;
1710 packet->src_port = sock->other_port;
1711 packet->dst_ipaddr = sock->my_ipaddr;
1712 packet->dst_port = sock->my_port;
1715 * FIXME: Have it return an indication of packets that
1716 * are OK to ignore (dups, too late), versus ones that
1717 * aren't OK to ignore (unknown response, spoofed, etc.)
1719 * Close the socket on bad packets...
1721 request = received_proxy_response(packet);
1726 rad_assert(fun != NULL);
1727 sock->opened = sock->last_packet = request->timestamp;
1730 *prequest = request;
1738 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1740 if (!request->reply->code) return 0;
1742 rad_encode(request->reply, request->packet,
1743 request->client->secret);
1744 rad_sign(request->reply, request->packet,
1745 request->client->secret);
1751 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1753 if (rad_verify(request->packet, NULL,
1754 request->client->secret) < 0) {
1758 return rad_decode(request->packet, NULL,
1759 request->client->secret);
1763 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1765 rad_encode(request->proxy, NULL, request->home_server->secret);
1766 rad_sign(request->proxy, NULL, request->home_server->secret);
1772 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1775 * rad_verify is run in event.c, received_proxy_response()
1778 return rad_decode(request->proxy_reply, request->proxy,
1779 request->home_server->secret);
1785 #include "command.c"
1787 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1789 { common_socket_parse, NULL,
1790 stats_socket_recv, auth_socket_send,
1791 socket_print, client_socket_encode, client_socket_decode },
1794 * This always gets defined.
1796 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1801 { common_socket_parse, NULL,
1802 proxy_socket_recv, proxy_socket_send,
1803 socket_print, proxy_socket_encode, proxy_socket_decode },
1806 /* authentication */
1807 { common_socket_parse, NULL,
1808 auth_socket_recv, auth_socket_send,
1809 socket_print, client_socket_encode, client_socket_decode },
1811 #ifdef WITH_ACCOUNTING
1813 { common_socket_parse, NULL,
1814 acct_socket_recv, acct_socket_send,
1815 socket_print, client_socket_encode, client_socket_decode},
1820 { detail_parse, detail_free,
1821 detail_recv, detail_send,
1822 detail_print, detail_encode, detail_decode },
1826 /* vlan query protocol */
1827 { common_socket_parse, NULL,
1828 vqp_socket_recv, vqp_socket_send,
1829 socket_print, vqp_socket_encode, vqp_socket_decode },
1833 /* dhcp query protocol */
1834 { dhcp_socket_parse, NULL,
1835 dhcp_socket_recv, dhcp_socket_send,
1836 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1839 #ifdef WITH_COMMAND_SOCKET
1840 /* TCP command socket */
1841 { command_socket_parse, command_socket_free,
1842 command_domain_accept, command_domain_send,
1843 command_socket_print, command_socket_encode, command_socket_decode },
1847 /* Change of Authorization */
1848 { common_socket_parse, NULL,
1849 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
1850 socket_print, client_socket_encode, client_socket_decode },
1858 * Binds a listener to a socket.
1860 static int listen_bind(rad_listen_t *this)
1863 struct sockaddr_storage salocal;
1865 listen_socket_t *sock = this->data;
1867 #define proto_for_port "udp"
1868 #define sock_type SOCK_DGRAM
1870 const char *proto_for_port = "udp";
1871 int sock_type = SOCK_DGRAM;
1873 if (sock->proto == IPPROTO_TCP) {
1875 if (this->type == RAD_LISTEN_VQP) {
1876 radlog(L_ERR, "VQP does not support TCP transport");
1881 proto_for_port = "tcp";
1882 sock_type = SOCK_STREAM;
1887 * If the port is zero, then it means the appropriate
1888 * thing from /etc/services.
1890 if (sock->my_port == 0) {
1891 struct servent *svp;
1893 switch (this->type) {
1894 case RAD_LISTEN_AUTH:
1895 svp = getservbyname ("radius", proto_for_port);
1897 sock->my_port = ntohs(svp->s_port);
1899 sock->my_port = PW_AUTH_UDP_PORT;
1903 #ifdef WITH_ACCOUNTING
1904 case RAD_LISTEN_ACCT:
1905 svp = getservbyname ("radacct", proto_for_port);
1907 sock->my_port = ntohs(svp->s_port);
1909 sock->my_port = PW_ACCT_UDP_PORT;
1915 case RAD_LISTEN_PROXY:
1916 /* leave it at zero */
1921 case RAD_LISTEN_VQP:
1922 sock->my_port = 1589;
1927 case RAD_LISTEN_COA:
1928 svp = getservbyname ("radius-dynauth", "udp");
1930 sock->my_port = ntohs(svp->s_port);
1932 sock->my_port = PW_COA_UDP_PORT;
1938 DEBUG("WARNING: Internal sanity check failed in binding to socket. Ignoring problem.");
1944 * Copy fr_socket() here, as we may need to bind to a device.
1946 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
1950 this->print(this, buffer, sizeof(buffer));
1952 radlog(L_ERR, "Failed opening %s: %s", buffer, strerror(errno));
1957 * Bind to a device BEFORE touching IP addresses.
1959 if (sock->interface) {
1960 #ifdef SO_BINDTODEVICE
1962 strcpy(ifreq.ifr_name, sock->interface);
1965 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1966 (char *)&ifreq, sizeof(ifreq));
1970 radlog(L_ERR, "Failed binding to interface %s: %s",
1971 sock->interface, strerror(errno));
1973 } /* else it worked. */
1975 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1976 #ifdef HAVE_NET_IF_H
1978 * Odds are that any system supporting "bind to
1979 * device" also supports IPv6, so this next bit
1980 * isn't necessary. But it's here for
1983 * If we're doing IPv6, and the scope hasn't yet
1984 * been defined, set the scope to the scope of
1987 if (sock->my_ipaddr.af == AF_INET6) {
1988 if (sock->my_ipaddr.scope == 0) {
1989 sock->my_ipaddr.scope = if_nametoindex(sock->interface);
1990 if (sock->my_ipaddr.scope == 0) {
1992 radlog(L_ERR, "Failed finding interface %s: %s",
1993 sock->interface, strerror(errno));
1996 } /* else scope was defined: we're OK. */
2001 * IPv4: no link local addresses,
2002 * and no bind to device.
2006 radlog(L_ERR, "Failed binding to interface %s: \"bind to device\" is unsupported", sock->interface);
2013 if (sock->proto == IPPROTO_TCP) {
2016 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2018 radlog(L_ERR, "Failed to reuse address: %s", strerror(errno));
2024 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
2025 else /* UDP sockets get UDPfromto */
2028 #ifdef WITH_UDPFROMTO
2030 * Initialize udpfromto for all sockets.
2032 if (udpfromto_init(this->fd) != 0) {
2039 * Set up sockaddr stuff.
2041 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
2046 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2047 if (sock->my_ipaddr.af == AF_INET6) {
2049 * Listening on '::' does NOT get you IPv4 to
2050 * IPv6 mapping. You've got to listen on an IPv4
2051 * address, too. This makes the rest of the server
2052 * design a little simpler.
2056 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2059 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2060 (char *)&on, sizeof(on));
2062 #endif /* IPV6_V6ONLY */
2064 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2066 if (sock->my_ipaddr.af == AF_INET) {
2069 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2071 * Disable PMTU discovery. On Linux, this
2072 * also makes sure that the "don't fragment"
2075 flag = IP_PMTUDISC_DONT;
2076 setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2077 &flag, sizeof(flag));
2080 #if defined(IP_DONTFRAG)
2082 * Ensure that the "don't fragment" flag is zero.
2085 setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2086 &flag, sizeof(flag));
2092 if (sock->broadcast) {
2095 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2096 radlog(L_ERR, "Can't set broadcast option: %s\n",
2105 * May be binding to priviledged ports.
2107 if (sock->my_port != 0) {
2111 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2112 radlog(L_ERR, "Can't set re-use address option: %s\n",
2119 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2125 this->print(this, buffer, sizeof(buffer));
2126 radlog(L_ERR, "Failed binding to %s: %s\n",
2127 buffer, strerror(errno));
2132 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2133 * kernel instead binds us to a 1.2.3.4. If this
2134 * happens, notice, and remember our real IP.
2137 struct sockaddr_storage src;
2138 socklen_t sizeof_src = sizeof(src);
2140 memset(&src, 0, sizeof_src);
2141 if (getsockname(this->fd, (struct sockaddr *) &src,
2143 radlog(L_ERR, "Failed getting socket name: %s",
2148 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2149 &sock->my_ipaddr, &sock->my_port)) {
2150 radlog(L_ERR, "Socket has unsupported address family");
2157 if (sock->proto == IPPROTO_TCP) {
2158 if (listen(this->fd, 8) < 0) {
2160 radlog(L_ERR, "Failed in listen(): %s", strerror(errno));
2166 if (fr_nonblock(this->fd) < 0) {
2168 radlog(L_ERR, "Failed setting non-blocking on socket: %s",
2174 * Mostly for proxy sockets.
2176 sock->other_ipaddr.af = sock->my_ipaddr.af;
2179 * Don't screw up other people.
2181 #undef proto_for_port
2189 * Allocate & initialize a new listener.
2191 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
2195 this = rad_malloc(sizeof(*this));
2196 memset(this, 0, sizeof(*this));
2199 this->recv = master_listen[this->type].recv;
2200 this->send = master_listen[this->type].send;
2201 this->print = master_listen[this->type].print;
2202 this->encode = master_listen[this->type].encode;
2203 this->decode = master_listen[this->type].decode;
2207 case RAD_LISTEN_NONE:
2209 case RAD_LISTEN_AUTH:
2210 #ifdef WITH_ACCOUNTING
2211 case RAD_LISTEN_ACCT:
2214 case RAD_LISTEN_PROXY:
2217 case RAD_LISTEN_VQP:
2220 case RAD_LISTEN_COA:
2222 this->data = rad_malloc(sizeof(listen_socket_t));
2223 memset(this->data, 0, sizeof(listen_socket_t));
2227 case RAD_LISTEN_DHCP:
2228 this->data = rad_malloc(sizeof(dhcp_socket_t));
2229 memset(this->data, 0, sizeof(dhcp_socket_t));
2234 case RAD_LISTEN_DETAIL:
2239 #ifdef WITH_COMMAND_SOCKET
2240 case RAD_LISTEN_COMMAND:
2241 this->data = rad_malloc(sizeof(fr_command_socket_t));
2242 memset(this->data, 0, sizeof(fr_command_socket_t));
2247 rad_assert("Unsupported option!" == NULL);
2256 * Externally visible function for creating a new proxy LISTENER.
2258 * Not thread-safe, but all calls to it are protected by the
2259 * proxy mutex in event.c
2261 int proxy_new_listener(home_server *home, int src_port)
2264 listen_socket_t *sock;
2266 if (!home) return 0;
2268 if ((home->max_connections > 0) &&
2269 (home->num_connections >= home->max_connections)) {
2270 DEBUG("WARNING: Home server has too many open connections (%d)",
2271 home->max_connections);
2275 this = listen_alloc(RAD_LISTEN_PROXY);
2278 sock->other_ipaddr = home->ipaddr;
2279 sock->other_port = home->port;
2282 sock->my_ipaddr = home->src_ipaddr;
2283 sock->my_port = src_port;
2284 sock->proto = home->proto;
2287 sock->last_packet = time(NULL);
2289 if (home->proto == IPPROTO_TCP) {
2290 this->recv = proxy_socket_tcp_recv;
2293 * FIXME: connect() is blocking!
2294 * We do this with the proxy mutex locked, which may
2295 * cause large delays!
2297 * http://www.developerweb.net/forum/showthread.php?p=13486
2299 this->fd = fr_tcp_client_socket(&home->src_ipaddr,
2300 &home->ipaddr, home->port);
2303 this->fd = fr_socket(&home->src_ipaddr, src_port);
2306 DEBUG("Failed opening client socket: %s", fr_strerror());
2312 * Figure out which port we were bound to.
2314 if (sock->my_port == 0) {
2315 struct sockaddr_storage src;
2316 socklen_t sizeof_src = sizeof(src);
2318 memset(&src, 0, sizeof_src);
2319 if (getsockname(this->fd, (struct sockaddr *) &src,
2321 radlog(L_ERR, "Failed getting socket name: %s",
2327 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2328 &sock->my_ipaddr, &sock->my_port)) {
2329 radlog(L_ERR, "Socket has unsupported address family");
2336 * Tell the event loop that we have a new FD
2338 if (!event_new_fd(this)) {
2348 static const FR_NAME_NUMBER listen_compare[] = {
2350 { "status", RAD_LISTEN_NONE },
2352 { "auth", RAD_LISTEN_AUTH },
2353 #ifdef WITH_ACCOUNTING
2354 { "acct", RAD_LISTEN_ACCT },
2357 { "detail", RAD_LISTEN_DETAIL },
2360 { "proxy", RAD_LISTEN_PROXY },
2363 { "vmps", RAD_LISTEN_VQP },
2366 { "dhcp", RAD_LISTEN_DHCP },
2368 #ifdef WITH_COMMAND_SOCKET
2369 { "control", RAD_LISTEN_COMMAND },
2372 { "coa", RAD_LISTEN_COA },
2378 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
2386 cf_log_info(cs, "listen {");
2388 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
2390 if (rcode < 0) return NULL;
2393 cf_log_err(cf_sectiontoitem(cs),
2394 "No type specified in listen section");
2398 type = fr_str2int(listen_compare, listen_type, -1);
2400 cf_log_err(cf_sectiontoitem(cs),
2401 "Invalid type \"%s\" in listen section.",
2409 * Allow listen sections in the default config to
2410 * refer to a server.
2413 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
2415 if (rcode == 1) { /* compatiblity with 2.0-pre */
2416 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
2419 if (rcode < 0) return NULL;
2424 * We were passed a virtual server, so the caller is
2425 * defining a proxy listener inside of a virtual server.
2426 * This isn't allowed right now.
2428 else if (type == RAD_LISTEN_PROXY) {
2429 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
2435 * Set up cross-type data.
2437 this = listen_alloc(type);
2438 this->server = server;
2442 * Call per-type parser.
2444 if (master_listen[type].parse(cs, this) < 0) {
2449 cf_log_info(cs, "}");
2455 static int is_loopback(const fr_ipaddr_t *ipaddr)
2458 * We shouldn't proxy on loopback.
2460 if ((ipaddr->af == AF_INET) &&
2461 (ipaddr->ipaddr.ip4addr.s_addr == htonl(INADDR_LOOPBACK))) {
2465 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2466 if ((ipaddr->af == AF_INET6) &&
2467 (IN6_IS_ADDR_LINKLOCAL(&ipaddr->ipaddr.ip6addr))) {
2477 * Generate a list of listeners. Takes an input list of
2478 * listeners, too, so we don't close sockets with waiting packets.
2480 int listen_init(CONF_SECTION *config, rad_listen_t **head)
2482 int override = FALSE;
2484 CONF_SECTION *cs = NULL;
2485 rad_listen_t **last;
2487 fr_ipaddr_t server_ipaddr;
2490 int defined_proxy = 0;
2494 * We shouldn't be called with a pre-existing list.
2496 rad_assert(head && (*head == NULL));
2499 server_ipaddr.af = AF_UNSPEC;
2502 * If the port is specified on the command-line,
2503 * it over-rides the configuration file.
2505 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
2507 if (mainconfig.port >= 0) auth_port = mainconfig.port;
2510 * If the IP address was configured on the command-line,
2511 * use that as the "bind_address"
2513 if (mainconfig.myip.af != AF_UNSPEC) {
2514 memcpy(&server_ipaddr, &mainconfig.myip,
2515 sizeof(server_ipaddr));
2521 * Else look for bind_address and/or listen sections.
2523 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
2524 rcode = cf_item_parse(config, "bind_address",
2526 &server_ipaddr.ipaddr.ip4addr, NULL);
2527 if (rcode < 0) return -1; /* error parsing it */
2529 if (rcode == 0) { /* successfully parsed IPv4 */
2530 listen_socket_t *sock;
2531 server_ipaddr.af = AF_INET;
2533 radlog(L_INFO, "WARNING: The directive 'bind_address' is deprecated, and will be removed in future versions of FreeRADIUS. Please edit the configuration files to use the directive 'listen'.");
2537 if (strcmp(progname, "vmpsd") == 0) {
2538 this = listen_alloc(RAD_LISTEN_VQP);
2539 if (!auth_port) auth_port = 1589;
2542 this = listen_alloc(RAD_LISTEN_AUTH);
2546 sock->my_ipaddr = server_ipaddr;
2547 sock->my_port = auth_port;
2549 sock->clients = clients_parse_section(config);
2550 if (!sock->clients) {
2551 cf_log_err(cf_sectiontoitem(config),
2552 "Failed to find any clients for this listen section");
2557 if (listen_bind(this) < 0) {
2559 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
2563 auth_port = sock->my_port; /* may have been updated in listen_bind */
2565 cs = cf_section_sub_find_name2(config, "server",
2567 if (cs) this->server = mainconfig.name;
2571 last = &(this->next);
2577 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
2580 #ifdef WITH_ACCOUNTING
2582 * Open Accounting Socket.
2584 * If we haven't already gotten acct_port from
2585 * /etc/services, then make it auth_port + 1.
2587 this = listen_alloc(RAD_LISTEN_ACCT);
2591 * Create the accounting socket.
2593 * The accounting port is always the
2594 * authentication port + 1
2596 sock->my_ipaddr = server_ipaddr;
2597 sock->my_port = auth_port + 1;
2599 sock->clients = clients_parse_section(config);
2600 if (!sock->clients) {
2601 cf_log_err(cf_sectiontoitem(config),
2602 "Failed to find any clients for this listen section");
2606 if (listen_bind(this) < 0) {
2609 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
2614 cs = cf_section_sub_find_name2(config, "server",
2616 if (cs) this->server = mainconfig.name;
2620 last = &(this->next);
2622 } else if (mainconfig.port > 0) { /* no bind address, but a port */
2623 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
2629 * They specified an IP on the command-line, ignore
2630 * all listen sections except the one in '-n'.
2632 if (mainconfig.myip.af != AF_UNSPEC) {
2633 CONF_SECTION *subcs;
2634 const char *name2 = cf_section_name2(cs);
2636 cs = cf_section_sub_find_name2(config, "server",
2638 if (!cs) goto add_sockets;
2641 * Should really abstract this code...
2643 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2645 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2646 this = listen_parse(subcs, name2);
2653 last = &(this->next);
2654 } /* loop over "listen" directives in server <foo> */
2660 * Walk through the "listen" sections, if they exist.
2662 for (cs = cf_subsection_find_next(config, NULL, "listen");
2664 cs = cf_subsection_find_next(config, cs, "listen")) {
2665 this = listen_parse(cs, NULL);
2672 last = &(this->next);
2676 * Check virtual servers for "listen" sections, too.
2678 * FIXME: Move to virtual server init?
2680 for (cs = cf_subsection_find_next(config, NULL, "server");
2682 cs = cf_subsection_find_next(config, cs, "server")) {
2683 CONF_SECTION *subcs;
2684 const char *name2 = cf_section_name2(cs);
2686 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2688 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2689 this = listen_parse(subcs, name2);
2696 last = &(this->next);
2697 } /* loop over "listen" directives in virtual servers */
2698 } /* loop over virtual servers */
2702 * No sockets to receive packets, this is an error.
2703 * proxying is pointless.
2706 radlog(L_ERR, "The server is not configured to listen on any ports. Cannot start.");
2711 * Print out which sockets we're listening on, and
2712 * add them to the event list.
2714 for (this = *head; this != NULL; this = this->next) {
2716 if (this->type == RAD_LISTEN_PROXY) {
2725 * If we're proxying requests, open the proxy FD.
2726 * Otherwise, don't do anything.
2729 if ((mainconfig.proxy_requests == TRUE) &&
2730 (*head != NULL) && !defined_proxy) {
2731 listen_socket_t *sock = NULL;
2735 memset(&home, 0, sizeof(home));
2740 home.proto = IPPROTO_UDP;
2741 home.src_ipaddr = server_ipaddr;
2744 * Find the first authentication port,
2747 for (this = *head; this != NULL; this = this->next) {
2748 if (this->type == RAD_LISTEN_AUTH) {
2751 if (is_loopback(&sock->my_ipaddr)) continue;
2753 if (home.src_ipaddr.af == AF_UNSPEC) {
2754 home.src_ipaddr = sock->my_ipaddr;
2756 port = sock->my_port + 2;
2760 if (this->type == RAD_LISTEN_ACCT) {
2763 if (is_loopback(&sock->my_ipaddr)) continue;
2765 if (home.src_ipaddr.af == AF_UNSPEC) {
2766 home.src_ipaddr = sock->my_ipaddr;
2768 port = sock->my_port + 1;
2775 * Address is still unspecified, use IPv4.
2777 if (home.src_ipaddr.af == AF_UNSPEC) {
2778 home.src_ipaddr.af = AF_INET;
2779 /* everything else is already set to zero */
2782 home.ipaddr.af = home.src_ipaddr.af;
2783 /* everything else is already set to zero */
2785 if (!proxy_new_listener(&home, port)) {
2793 * Haven't defined any sockets. Die.
2795 if (!*head) return -1;
2802 * Free a linked list of listeners;
2804 void listen_free(rad_listen_t **head)
2808 if (!head || !*head) return;
2812 rad_listen_t *next = this->next;
2815 * Other code may have eaten the FD.
2817 if (this->fd >= 0) close(this->fd);
2819 if (master_listen[this->type].free) {
2820 master_listen[this->type].free(this);
2824 if ((this->type == RAD_LISTEN_AUTH)
2826 || (this->type == RAD_LISTEN_ACCT)
2829 || (this->type == RAD_LISTEN_PROXY)
2832 listen_socket_t *sock = this->data;
2833 rad_free(&sock->packet);
2847 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
2852 for (this = mainconfig.listen; this != NULL; this = this->next) {
2853 listen_socket_t *sock;
2855 if ((this->type != RAD_LISTEN_AUTH)
2856 #ifdef WITH_ACCOUNTING
2857 && (this->type != RAD_LISTEN_ACCT)
2863 if ((sock->my_port == port) &&
2864 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
2865 return sock->clients;
2873 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
2877 for (this = mainconfig.listen; this != NULL; this = this->next) {
2878 listen_socket_t *sock;
2881 * FIXME: For TCP, ignore the *secondary*
2882 * listeners associated with the main socket.
2884 if ((this->type != RAD_LISTEN_AUTH)
2885 #ifdef WITH_ACCOUNTING
2886 && (this->type != RAD_LISTEN_ACCT)
2892 if ((sock->my_port == port) &&
2893 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
2897 if ((sock->my_port == port) &&
2898 fr_inaddr_any(&sock->my_ipaddr)) {