2 * listen.c Handle socket stuff
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2005,2006 The FreeRADIUS server project
21 * Copyright 2005 Alan DeKok <aland@ox.org>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29 #include <freeradius-devel/rad_assert.h>
30 #include <freeradius-devel/vqp.h>
31 #include <freeradius-devel/dhcp.h>
33 #include <freeradius-devel/vmps.h>
34 #include <freeradius-devel/detail.h>
37 #include <freeradius-devel/udpfromto.h>
40 #ifdef HAVE_SYS_RESOURCE_H
41 #include <sys/resource.h>
53 void print_packet(RADIUS_PACKET *packet)
55 char src[256], dst[256];
57 ip_ntoh(&packet->src_ipaddr, src, sizeof(src));
58 ip_ntoh(&packet->dst_ipaddr, dst, sizeof(dst));
60 fprintf(stderr, "ID %d: %s %d -> %s %d\n", packet->id,
61 src, packet->src_port, dst, packet->dst_port);
67 * We'll use this below.
69 typedef int (*rad_listen_parse_t)(CONF_SECTION *, rad_listen_t *);
70 typedef void (*rad_listen_free_t)(rad_listen_t *);
72 typedef struct rad_listen_master_t {
73 rad_listen_parse_t parse;
74 rad_listen_free_t free;
75 rad_listen_recv_t recv;
76 rad_listen_send_t send;
77 rad_listen_print_t print;
78 rad_listen_encode_t encode;
79 rad_listen_decode_t decode;
80 } rad_listen_master_t;
82 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type);
85 * Find a per-socket client.
87 RADCLIENT *client_listener_find(const rad_listen_t *listener,
88 const fr_ipaddr_t *ipaddr, int src_port)
90 #ifdef WITH_DYNAMIC_CLIENTS
97 RADCLIENT_LIST *clients;
98 listen_socket_t *sock;
100 rad_assert(listener != NULL);
101 rad_assert(ipaddr != NULL);
103 sock = listener->data;
104 clients = sock->clients;
107 * This HAS to have been initialized previously.
109 rad_assert(clients != NULL);
111 client = client_find(clients, ipaddr,sock->proto);
113 char name[256], buffer[128];
115 #ifdef WITH_DYNAMIC_CLIENTS
116 unknown: /* used only for dynamic clients */
120 * DoS attack quenching, but only in daemon mode.
121 * If they're running in debug mode, show them
124 if (debug_flag == 0) {
125 static time_t last_printed = 0;
128 if (last_printed == now) return NULL;
133 listener->print(listener, name, sizeof(name));
135 radlog(L_ERR, "Ignoring request to %s from unknown client %s port %d"
139 , name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
140 buffer, sizeof(buffer)), src_port
142 , (sock->proto == IPPROTO_UDP) ? "udp" : "tcp"
148 #ifndef WITH_DYNAMIC_CLIENTS
149 return client; /* return the found client. */
153 * No server defined, and it's not dynamic. Return it.
155 if (!client->client_server && !client->dynamic) return client;
160 * It's a dynamically generated client, check it.
162 if (client->dynamic && (src_port != 0)) {
164 * Lives forever. Return it.
166 if (client->lifetime == 0) return client;
169 * Rate-limit the deletion of known clients.
170 * This makes them last a little longer, but
171 * prevents the server from melting down if (say)
172 * 10k clients all expire at once.
174 if (now == client->last_new_client) return client;
177 * It's not dead yet. Return it.
179 if ((client->created + client->lifetime) > now) return client;
182 * This really puts them onto a queue for later
185 client_delete(clients, client);
188 * Go find the enclosing network again.
190 client = client_find(clients, ipaddr, sock->proto);
195 if (!client) goto unknown;
196 if (!client->client_server) goto unknown;
199 * At this point, 'client' is the enclosing
200 * network that configures where dynamic clients
203 rad_assert(client->dynamic == 0);
206 * The IP is unknown, so we've found an enclosing
207 * network. Enable DoS protection. We only
208 * allow one new client per second. Known
209 * clients aren't subject to this restriction.
211 if (now == client->last_new_client) goto unknown;
214 client->last_new_client = now;
216 request = request_alloc();
217 if (!request) goto unknown;
219 request->listener = listener;
220 request->client = client;
221 request->packet = rad_recv(listener->fd, 0x02); /* MSG_PEEK */
222 if (!request->packet) { /* badly formed, etc */
223 request_free(&request);
226 request->reply = rad_alloc_reply(request->packet);
227 if (!request->reply) {
228 request_free(&request);
231 request->packet->timestamp = request->timestamp;
233 request->priority = listener->type;
234 request->server = client->client_server;
235 request->root = &mainconfig;
238 * Run a fake request through the given virtual server.
239 * Look for FreeRADIUS-Client-IP-Address
240 * FreeRADIUS-Client-Secret
243 * and create the RADCLIENT structure from that.
245 DEBUG("server %s {", request->server);
247 rcode = module_authorize(0, request);
249 DEBUG("} # server %s", request->server);
251 if (rcode != RLM_MODULE_OK) {
252 request_free(&request);
257 * If the client was updated by rlm_dynamic_clients,
258 * don't create the client from attribute-value pairs.
260 if (request->client == client) {
261 created = client_create(clients, request);
263 created = request->client;
266 * This frees the client if it isn't valid.
268 if (!client_validate(clients, client, created)) goto unknown;
270 request_free(&request);
272 if (!created) goto unknown;
278 static int listen_bind(rad_listen_t *this);
282 * Process and reply to a server-status request.
283 * Like rad_authenticate and rad_accounting this should
284 * live in it's own file but it's so small we don't bother.
286 static int rad_status_server(REQUEST *request)
288 int rcode = RLM_MODULE_OK;
291 switch (request->listener->type) {
293 case RAD_LISTEN_NONE:
295 case RAD_LISTEN_AUTH:
296 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
298 rcode = module_authorize(dval->value, request);
300 rcode = RLM_MODULE_OK;
305 case RLM_MODULE_UPDATED:
306 request->reply->code = PW_AUTHENTICATION_ACK;
309 case RLM_MODULE_FAIL:
310 case RLM_MODULE_HANDLED:
311 request->reply->code = 0; /* don't reply */
315 case RLM_MODULE_REJECT:
316 request->reply->code = PW_AUTHENTICATION_REJECT;
321 #ifdef WITH_ACCOUNTING
322 case RAD_LISTEN_ACCT:
323 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
325 rcode = module_accounting(dval->value, request);
327 rcode = RLM_MODULE_OK;
332 case RLM_MODULE_UPDATED:
333 request->reply->code = PW_ACCOUNTING_RESPONSE;
337 request->reply->code = 0; /* don't reply */
345 * This is a vendor extension. Suggested by Glen
346 * Zorn in IETF 72, and rejected by the rest of
347 * the WG. We like it, so it goes in here.
350 dval = dict_valbyname(PW_RECV_COA_TYPE, "Status-Server");
352 rcode = module_recv_coa(dval->value, request);
354 rcode = RLM_MODULE_OK;
359 case RLM_MODULE_UPDATED:
360 request->reply->code = PW_COA_ACK;
364 request->reply->code = 0; /* don't reply */
376 * Full statistics are available only on a statistics
379 if (request->listener->type == RAD_LISTEN_NONE) {
380 request_stats_reply(request);
388 static int auth_tcp_recv(rad_listen_t *listener,
389 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
392 RADIUS_PACKET *packet;
393 RAD_REQUEST_FUNP fun = NULL;
394 listen_socket_t *sock = listener->data;
395 RADCLIENT *client = sock->client;
398 * Allocate a packet for partial reads.
401 sock->packet = rad_alloc(0);
402 if (!sock->packet) return 0;
404 sock->packet->sockfd = listener->fd;
405 sock->packet->src_ipaddr = sock->other_ipaddr;
406 sock->packet->src_port = sock->other_port;
407 sock->packet->dst_ipaddr = sock->my_ipaddr;
408 sock->packet->dst_port = sock->my_port;
412 * Grab the packet currently being processed.
414 packet = sock->packet;
416 rcode = fr_tcp_read_packet(packet, 0);
419 * Still only a partial packet. Put it back, and return,
420 * so that we'll read more data when it's ready.
426 if (rcode == -1) { /* error reading packet */
429 radlog(L_ERR, "Invalid packet from %s port %d: closing socket",
430 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
434 if (rcode < 0) { /* error or connection reset */
435 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
438 * Decrement the number of connections.
440 if (sock->parent->num_connections > 0) {
441 sock->parent->num_connections--;
443 if (sock->client->num_connections > 0) {
444 sock->client->num_connections--;
448 * Tell the event handler that an FD has disappeared.
450 DEBUG("Client has closed connection");
451 event_new_fd(listener);
454 * Do NOT free the listener here. It's in use by
455 * a request, and will need to hang around until
456 * all of the requests are done.
458 * It is instead free'd in remove_from_request_hash()
463 RAD_STATS_TYPE_INC(listener, total_requests);
466 * Some sanity checks, based on the packet code.
468 switch(packet->code) {
469 case PW_AUTHENTICATION_REQUEST:
470 RAD_STATS_CLIENT_INC(listener, client, total_requests);
471 fun = rad_authenticate;
474 case PW_STATUS_SERVER:
475 if (!mainconfig.status_server) {
476 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
477 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
478 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
479 rad_free(&sock->packet);
482 fun = rad_status_server;
486 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
487 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
489 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
490 packet->code, client->shortname, packet->src_port);
491 rad_free(&sock->packet);
493 } /* switch over packet types */
495 if (!received_request(listener, packet, prequest, sock->client)) {
496 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
497 RAD_STATS_CLIENT_INC(listener, sock->client, total_packets_dropped);
498 rad_free(&sock->packet);
503 sock->packet = NULL; /* we have no need for more partial reads */
507 static int auth_tcp_accept(rad_listen_t *listener,
508 UNUSED RAD_REQUEST_FUNP *pfun,
509 UNUSED REQUEST **prequest)
514 struct sockaddr_storage src;
515 listen_socket_t *sock;
516 fr_ipaddr_t src_ipaddr;
521 DEBUG2(" ... new connection request on TCP socket.");
523 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
526 * Non-blocking sockets must handle this.
528 if (errno == EWOULDBLOCK) {
532 DEBUG2(" ... failed to accept connection.");
536 if (!fr_sockaddr2ipaddr(&src, salen, &src_ipaddr, &src_port)) {
537 DEBUG2(" ... unknown address family.");
542 * Enforce client IP address checks on accept, not on
545 if ((client = client_listener_find(listener,
546 &src_ipaddr, src_port)) == NULL) {
548 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
553 * Enforce max_connectionsx on client && listen section.
555 if ((client->max_connections != 0) &&
556 (client->max_connections == client->num_connections)) {
558 * FIXME: Print client IP/port, and server IP/port.
560 radlog(L_INFO, "Ignoring new connection due to client max_connections (%d)", client->max_connections);
565 sock = listener->data;
566 if ((sock->max_connections != 0) &&
567 (sock->max_connections == sock->num_connections)) {
569 * FIXME: Print client IP/port, and server IP/port.
571 radlog(L_INFO, "Ignoring new connection due to socket max_connections");
575 client->num_connections++;
576 sock->num_connections++;
579 * Add the new listener.
581 this = listen_alloc(listener->type);
582 if (!this) return -1;
585 * Copy everything, including the pointer to the socket
589 memcpy(this->data, listener->data, sizeof(*sock));
590 memcpy(this, listener, sizeof(*this));
592 this->data = sock; /* fix it back */
594 sock->parent = listener->data;
595 sock->other_ipaddr = src_ipaddr;
596 sock->other_port = src_port;
597 sock->client = client;
600 this->status = RAD_LISTEN_STATUS_INIT;
601 this->recv = auth_tcp_recv;
604 * FIXME: set O_NONBLOCK on the accept'd fd.
605 * See djb's portability rants for details.
609 * Tell the event loop that we have a new FD.
610 * This can be called from a child thread...
620 * This function is stupid and complicated.
622 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
625 listen_socket_t *sock = this->data;
628 switch (this->type) {
630 case RAD_LISTEN_NONE: /* what a hack... */
635 case RAD_LISTEN_AUTH:
636 name = "authentication";
639 #ifdef WITH_ACCOUNTING
640 case RAD_LISTEN_ACCT:
646 case RAD_LISTEN_PROXY:
658 case RAD_LISTEN_DHCP:
674 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
675 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
679 #ifdef SO_BINDTODEVICE
680 if (sock->interface) {
681 ADDSTRING(" interface ");
682 ADDSTRING(sock->interface);
687 if (this->recv == auth_tcp_accept) {
688 ADDSTRING(" proto tcp");
694 * TCP sockets get printed a little differently, to make
695 * it clear what's going on.
698 ADDSTRING(" from client (");
699 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
703 snprintf(buffer, bufsize, "%d", sock->other_port);
707 if ((sock->my_ipaddr.af == AF_INET) &&
708 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
709 strlcpy(buffer, "*", bufsize);
711 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
716 snprintf(buffer, bufsize, "%d", sock->my_port);
720 ADDSTRING(", virtual-server=");
721 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);
761 ADDSTRING(" address ");
763 if ((sock->my_ipaddr.af == AF_INET) &&
764 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
765 strlcpy(buffer, "*", bufsize);
767 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
772 snprintf(buffer, bufsize, "%d", sock->my_port);
776 ADDSTRING(" as server ");
777 ADDSTRING(this->server);
788 * Parse an authentication or accounting socket.
790 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
795 listen_socket_t *sock = this->data;
796 char *section_name = NULL;
797 CONF_SECTION *client_cs, *parentcs;
802 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
803 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
804 &ipaddr.ipaddr.ip4addr, NULL);
805 if (rcode < 0) return -1;
807 if (rcode == 0) { /* successfully parsed IPv4 */
810 } else { /* maybe IPv6? */
811 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
812 &ipaddr.ipaddr.ip6addr, NULL);
813 if (rcode < 0) return -1;
816 cf_log_err(cf_sectiontoitem(cs),
817 "No address specified in listen section");
820 ipaddr.af = AF_INET6;
823 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
825 if (rcode < 0) return -1;
827 if ((listen_port < 0) || (listen_port > 65535)) {
828 cf_log_err(cf_sectiontoitem(cs),
829 "Invalid value for \"port\"");
833 sock->proto = IPPROTO_UDP;
835 if (cf_pair_find(cs, "proto")) {
837 cf_log_err(cf_sectiontoitem(cs),
838 "System does not support the TCP protocol. Delete this line from the configuration file.");
844 rcode = cf_item_parse(cs, "proto", PW_TYPE_STRING_PTR,
846 if (rcode < 0) return -1;
848 if (strcmp(proto, "udp") == 0) {
849 sock->proto = IPPROTO_UDP;
851 } else if (strcmp(proto, "tcp") == 0) {
852 sock->proto = IPPROTO_TCP;
854 rcode = cf_item_parse(cs, "max_connections", PW_TYPE_INTEGER,
855 &sock->max_connections, "64");
856 if (rcode < 0) return -1;
859 cf_log_err(cf_sectiontoitem(cs),
860 "Unknown proto name \"%s\"", proto);
867 * TCP requires a destination IP for sockets.
868 * UDP doesn't, so it's allowed.
870 if ((this->type == RAD_LISTEN_PROXY) &&
871 (sock->proto != IPPROTO_UDP)) {
872 cf_log_err(cf_sectiontoitem(cs),
873 "Proxy listeners can only listen on proto = udp");
879 sock->my_ipaddr = ipaddr;
880 sock->my_port = listen_port;
883 * If we can bind to interfaces, do so,
886 if (cf_pair_find(cs, "interface")) {
887 #ifndef SO_BINDTODEVICE
888 cf_log_err(cf_sectiontoitem(cs),
889 "System does not support binding to interfaces. Delete this line from the configuration file.");
893 CONF_PAIR *cp = cf_pair_find(cs, "interface");
895 rad_assert(cp != NULL);
896 value = cf_pair_value(cp);
898 cf_log_err(cf_sectiontoitem(cs),
899 "No interface name given");
902 sock->interface = value;
908 * If we can do broadcasts..
910 if (cf_pair_find(cs, "broadcast")) {
912 cf_log_err(cf_sectiontoitem(cs),
913 "System does not support broadcast sockets. Delete this line from the configuration file.");
917 CONF_PAIR *cp = cf_pair_find(cs, "broadcast");
919 if (this->type != RAD_LISTEN_DHCP) {
920 cf_log_err(cf_pairtoitem(cp),
921 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file.");
925 rad_assert(cp != NULL);
926 value = cf_pair_value(cp);
928 cf_log_err(cf_sectiontoitem(cs),
929 "No broadcast value given");
934 * Hack... whatever happened to cf_section_parse?
936 sock->broadcast = (strcmp(value, "yes") == 0);
942 * And bind it to the port.
944 if (listen_bind(this) < 0) {
946 cf_log_err(cf_sectiontoitem(cs),
947 "Error binding to port for %s port %d",
948 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
955 * Proxy sockets don't have clients.
957 if (this->type == RAD_LISTEN_PROXY) return 0;
961 * The more specific configurations are preferred to more
965 parentcs = cf_top_section(cs);
966 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
967 §ion_name, NULL);
968 if (rcode < 0) return -1; /* bad string */
971 * Explicit list given: use it.
973 client_cs = cf_section_sub_find_name2(parentcs,
977 client_cs = cf_section_find(section_name);
980 cf_log_err(cf_sectiontoitem(cs),
981 "Failed to find clients %s {...}",
987 } /* else there was no "clients = " entry. */
990 CONF_SECTION *server_cs;
992 server_cs = cf_section_sub_find_name2(parentcs,
996 * Found a "server foo" section. If there are clients
1000 (cf_section_sub_find(server_cs, "client") != NULL)) {
1001 client_cs = server_cs;
1006 * Still nothing. Look for global clients.
1008 if (!client_cs) client_cs = parentcs;
1010 sock->clients = clients_parse_section(client_cs);
1011 if (!sock->clients) {
1012 cf_log_err(cf_sectiontoitem(cs),
1013 "Failed to load clients for this listen section");
1018 if (sock->proto == IPPROTO_TCP) {
1020 * Re-write the listener receive function to
1021 * allow us to accept the socket.
1023 this->recv = auth_tcp_accept;
1031 * Send an authentication response packet
1033 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1035 rad_assert(request->listener == listener);
1036 rad_assert(listener->send == auth_socket_send);
1038 return rad_send(request->reply, request->packet,
1039 request->client->secret);
1043 #ifdef WITH_ACCOUNTING
1045 * Send an accounting response packet (or not)
1047 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1049 rad_assert(request->listener == listener);
1050 rad_assert(listener->send == acct_socket_send);
1053 * Accounting reject's are silently dropped.
1055 * We do it here to avoid polluting the rest of the
1056 * code with this knowledge
1058 if (request->reply->code == 0) return 0;
1060 return rad_send(request->reply, request->packet,
1061 request->client->secret);
1067 * Send a packet to a home server.
1069 * FIXME: have different code for proxy auth & acct!
1071 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1073 rad_assert(request->proxy_listener == listener);
1074 rad_assert(listener->send == proxy_socket_send);
1076 return rad_send(request->proxy, request->packet,
1077 request->home_server->secret);
1083 * Check if an incoming request is "ok"
1085 * It takes packets, not requests. It sees if the packet looks
1086 * OK. If so, it does a number of sanity checks on it.
1088 static int stats_socket_recv(rad_listen_t *listener,
1089 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1093 RADIUS_PACKET *packet;
1095 fr_ipaddr_t src_ipaddr;
1097 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1098 if (rcode < 0) return 0;
1100 RAD_STATS_TYPE_INC(listener, total_requests);
1102 if (rcode < 20) { /* AUTH_HDR_LEN */
1103 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1107 if ((client = client_listener_find(listener,
1108 &src_ipaddr, src_port)) == NULL) {
1109 rad_recv_discard(listener->fd);
1110 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1115 * We only understand Status-Server on this socket.
1117 if (code != PW_STATUS_SERVER) {
1118 DEBUG("Ignoring packet code %d sent to Status-Server port",
1120 rad_recv_discard(listener->fd);
1121 RAD_STATS_TYPE_INC(listener, total_unknown_types);
1122 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1127 * Now that we've sanity checked everything, receive the
1130 packet = rad_recv(listener->fd, 1); /* require message authenticator */
1132 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1133 DEBUG("%s", fr_strerror());
1137 if (!received_request(listener, packet, prequest, client)) {
1138 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1139 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1144 *pfun = rad_status_server;
1151 * Check if an incoming request is "ok"
1153 * It takes packets, not requests. It sees if the packet looks
1154 * OK. If so, it does a number of sanity checks on it.
1156 static int auth_socket_recv(rad_listen_t *listener,
1157 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1161 RADIUS_PACKET *packet;
1162 RAD_REQUEST_FUNP fun = NULL;
1164 fr_ipaddr_t src_ipaddr;
1166 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1167 if (rcode < 0) return 0;
1169 RAD_STATS_TYPE_INC(listener, total_requests);
1171 if (rcode < 20) { /* AUTH_HDR_LEN */
1172 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1176 if ((client = client_listener_find(listener,
1177 &src_ipaddr, src_port)) == NULL) {
1178 rad_recv_discard(listener->fd);
1179 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1184 * Some sanity checks, based on the packet code.
1187 case PW_AUTHENTICATION_REQUEST:
1188 RAD_STATS_CLIENT_INC(listener, client, total_requests);
1189 fun = rad_authenticate;
1192 case PW_STATUS_SERVER:
1193 if (!mainconfig.status_server) {
1194 rad_recv_discard(listener->fd);
1195 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1196 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1197 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1200 fun = rad_status_server;
1204 rad_recv_discard(listener->fd);
1205 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
1206 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1208 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
1209 code, client->shortname, src_port);
1212 } /* switch over packet types */
1215 * Now that we've sanity checked everything, receive the
1218 packet = rad_recv(listener->fd, client->message_authenticator);
1220 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1221 DEBUG("%s", fr_strerror());
1225 if (!received_request(listener, packet, prequest, client)) {
1226 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1227 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1237 #ifdef WITH_ACCOUNTING
1239 * Receive packets from an accounting socket
1241 static int acct_socket_recv(rad_listen_t *listener,
1242 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1246 RADIUS_PACKET *packet;
1247 RAD_REQUEST_FUNP fun = NULL;
1249 fr_ipaddr_t src_ipaddr;
1251 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1252 if (rcode < 0) return 0;
1254 RAD_STATS_TYPE_INC(listener, total_requests);
1256 if (rcode < 20) { /* AUTH_HDR_LEN */
1257 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1261 if ((client = client_listener_find(listener,
1262 &src_ipaddr, src_port)) == NULL) {
1263 rad_recv_discard(listener->fd);
1264 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1269 * Some sanity checks, based on the packet code.
1272 case PW_ACCOUNTING_REQUEST:
1273 RAD_STATS_CLIENT_INC(listener, client, total_requests);
1274 fun = rad_accounting;
1277 case PW_STATUS_SERVER:
1278 if (!mainconfig.status_server) {
1279 rad_recv_discard(listener->fd);
1280 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1281 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1283 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1286 fun = rad_status_server;
1290 rad_recv_discard(listener->fd);
1291 RAD_STATS_TYPE_INC(listener, total_unknown_types);
1292 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1294 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1295 code, client->shortname, src_port);
1297 } /* switch over packet types */
1300 * Now that we've sanity checked everything, receive the
1303 packet = rad_recv(listener->fd, 0);
1305 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1306 radlog(L_ERR, "%s", fr_strerror());
1311 * There can be no duplicate accounting packets.
1313 if (!received_request(listener, packet, prequest, client)) {
1314 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1315 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1328 * For now, all CoA requests are *only* originated, and not
1329 * proxied. So all of the necessary work is done in the
1330 * post-proxy section, which is automatically handled by event.c.
1331 * As a result, we don't have to do anything here.
1333 static int rad_coa_reply(REQUEST *request)
1335 VALUE_PAIR *s1, *s2;
1338 * Inform the user about RFC requirements.
1340 s1 = pairfind(request->proxy->vps, PW_STATE);
1342 s2 = pairfind(request->proxy_reply->vps, PW_STATE);
1345 DEBUG("WARNING: Client was sent State in CoA, and did not respond with State.");
1347 } else if ((s1->length != s2->length) ||
1348 (memcmp(s1->vp_octets, s2->vp_octets,
1349 s1->length) != 0)) {
1350 DEBUG("WARNING: Client was sent State in CoA, and did not respond with the same State.");
1354 return RLM_MODULE_OK;
1358 * Receive a CoA packet.
1360 static int rad_coa_recv(REQUEST *request)
1362 int rcode = RLM_MODULE_OK;
1367 * Get the correct response
1369 switch (request->packet->code) {
1370 case PW_COA_REQUEST:
1375 case PW_DISCONNECT_REQUEST:
1376 ack = PW_DISCONNECT_ACK;
1377 nak = PW_DISCONNECT_NAK;
1380 default: /* shouldn't happen */
1381 return RLM_MODULE_FAIL;
1385 #define WAS_PROXIED (request->proxy)
1387 #define WAS_PROXIED (0)
1392 * RFC 5176 Section 3.3. If we have a CoA-Request
1393 * with Service-Type = Authorize-Only, it MUST
1394 * have a State attribute in it.
1396 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE);
1397 if (request->packet->code == PW_COA_REQUEST) {
1398 if (vp && (vp->vp_integer == 17)) {
1399 vp = pairfind(request->packet->vps, PW_STATE);
1400 if (!vp || (vp->length == 0)) {
1401 RDEBUG("ERROR: CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1402 request->reply->code = PW_COA_NAK;
1403 return RLM_MODULE_FAIL;
1408 * RFC 5176, Section 3.2.
1410 RDEBUG("ERROR: Disconnect-Request MUST NOT contain a Service-Type attribute");
1411 request->reply->code = PW_DISCONNECT_NAK;
1412 return RLM_MODULE_FAIL;
1415 rcode = module_recv_coa(0, request);
1417 case RLM_MODULE_FAIL:
1418 case RLM_MODULE_INVALID:
1419 case RLM_MODULE_REJECT:
1420 case RLM_MODULE_USERLOCK:
1422 request->reply->code = nak;
1425 case RLM_MODULE_HANDLED:
1428 case RLM_MODULE_NOOP:
1429 case RLM_MODULE_NOTFOUND:
1431 case RLM_MODULE_UPDATED:
1432 request->reply->code = ack;
1437 * Start the reply code with the proxy reply
1440 request->reply->code = request->proxy_reply->code;
1444 * Copy State from the request to the reply.
1445 * See RFC 5176 Section 3.3.
1447 vp = paircopy2(request->packet->vps, PW_STATE);
1448 if (vp) pairadd(&request->reply->vps, vp);
1451 * We may want to over-ride the reply.
1453 rcode = module_send_coa(0, request);
1456 * We need to send CoA-NAK back if Service-Type
1457 * is Authorize-Only. Rely on the user's policy
1458 * to do that. We're not a real NAS, so this
1459 * restriction doesn't (ahem) apply to us.
1461 case RLM_MODULE_FAIL:
1462 case RLM_MODULE_INVALID:
1463 case RLM_MODULE_REJECT:
1464 case RLM_MODULE_USERLOCK:
1467 * Over-ride an ACK with a NAK
1469 request->reply->code = nak;
1472 case RLM_MODULE_HANDLED:
1475 case RLM_MODULE_NOOP:
1476 case RLM_MODULE_NOTFOUND:
1478 case RLM_MODULE_UPDATED:
1480 * Do NOT over-ride a previously set value.
1481 * Otherwise an "ok" here will re-write a
1484 if (request->reply->code == 0) {
1485 request->reply->code = ack;
1491 return RLM_MODULE_OK;
1496 * Check if an incoming request is "ok"
1498 * It takes packets, not requests. It sees if the packet looks
1499 * OK. If so, it does a number of sanity checks on it.
1501 static int coa_socket_recv(rad_listen_t *listener,
1502 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1506 RADIUS_PACKET *packet;
1507 RAD_REQUEST_FUNP fun = NULL;
1509 fr_ipaddr_t src_ipaddr;
1511 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1512 if (rcode < 0) return 0;
1514 RAD_STATS_TYPE_INC(listener, total_requests);
1516 if (rcode < 20) { /* AUTH_HDR_LEN */
1517 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1521 if ((client = client_listener_find(listener,
1522 &src_ipaddr, src_port)) == NULL) {
1523 rad_recv_discard(listener->fd);
1524 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1529 * Some sanity checks, based on the packet code.
1532 case PW_COA_REQUEST:
1533 case PW_DISCONNECT_REQUEST:
1538 rad_recv_discard(listener->fd);
1539 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1540 code, client->shortname, src_port);
1543 } /* switch over packet types */
1546 * Now that we've sanity checked everything, receive the
1549 packet = rad_recv(listener->fd, client->message_authenticator);
1551 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1552 DEBUG("%s", fr_strerror());
1556 if (!received_request(listener, packet, prequest, client)) {
1568 * Recieve packets from a proxy socket.
1570 static int proxy_socket_recv(rad_listen_t *listener,
1571 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1574 RADIUS_PACKET *packet;
1575 RAD_REQUEST_FUNP fun = NULL;
1578 packet = rad_recv(listener->fd, 0);
1580 radlog(L_ERR, "%s", fr_strerror());
1585 * FIXME: Client MIB updates?
1587 switch(packet->code) {
1588 case PW_AUTHENTICATION_ACK:
1589 case PW_ACCESS_CHALLENGE:
1590 case PW_AUTHENTICATION_REJECT:
1591 fun = rad_authenticate;
1594 #ifdef WITH_ACCOUNTING
1595 case PW_ACCOUNTING_RESPONSE:
1596 fun = rad_accounting;
1601 case PW_DISCONNECT_ACK:
1602 case PW_DISCONNECT_NAK:
1605 fun = rad_coa_reply;
1611 * FIXME: Update MIB for packet types?
1613 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1614 "from home server %s port %d - ID %d : IGNORED",
1616 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1617 packet->src_port, packet->id);
1622 request = received_proxy_response(packet);
1630 * Distinguish proxied CoA requests from ones we
1633 if ((fun == rad_coa_reply) &&
1634 (request->packet->code == request->proxy->code)) {
1639 rad_assert(fun != NULL);
1641 *prequest = request;
1648 * Recieve packets from a proxy socket.
1650 static int proxy_socket_tcp_recv(rad_listen_t *listener,
1651 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1654 RADIUS_PACKET *packet;
1655 RAD_REQUEST_FUNP fun = NULL;
1656 listen_socket_t *sock = listener->data;
1659 packet = fr_tcp_recv(listener->fd, 0);
1661 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
1662 event_new_fd(listener);
1667 * FIXME: Client MIB updates?
1669 switch(packet->code) {
1670 case PW_AUTHENTICATION_ACK:
1671 case PW_ACCESS_CHALLENGE:
1672 case PW_AUTHENTICATION_REJECT:
1673 fun = rad_authenticate;
1676 #ifdef WITH_ACCOUNTING
1677 case PW_ACCOUNTING_RESPONSE:
1678 fun = rad_accounting;
1684 * FIXME: Update MIB for packet types?
1686 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1687 "from home server %s port %d - ID %d : IGNORED",
1689 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1690 packet->src_port, packet->id);
1695 packet->src_ipaddr = sock->other_ipaddr;
1696 packet->src_port = sock->other_port;
1697 packet->dst_ipaddr = sock->my_ipaddr;
1698 packet->dst_port = sock->my_port;
1701 * FIXME: Have it return an indication of packets that
1702 * are OK to ignore (dups, too late), versus ones that
1703 * aren't OK to ignore (unknown response, spoofed, etc.)
1705 * Close the socket on bad packets...
1707 request = received_proxy_response(packet);
1712 rad_assert(fun != NULL);
1713 sock->opened = sock->last_packet = request->timestamp;
1716 *prequest = request;
1724 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1726 if (!request->reply->code) return 0;
1728 rad_encode(request->reply, request->packet,
1729 request->client->secret);
1730 rad_sign(request->reply, request->packet,
1731 request->client->secret);
1737 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1739 if (rad_verify(request->packet, NULL,
1740 request->client->secret) < 0) {
1744 return rad_decode(request->packet, NULL,
1745 request->client->secret);
1749 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1751 rad_encode(request->proxy, NULL, request->home_server->secret);
1752 rad_sign(request->proxy, NULL, request->home_server->secret);
1758 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1761 * rad_verify is run in event.c, received_proxy_response()
1764 return rad_decode(request->proxy_reply, request->proxy,
1765 request->home_server->secret);
1771 #include "command.c"
1773 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1775 { common_socket_parse, NULL,
1776 stats_socket_recv, auth_socket_send,
1777 socket_print, client_socket_encode, client_socket_decode },
1780 * This always gets defined.
1782 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1787 { common_socket_parse, NULL,
1788 proxy_socket_recv, proxy_socket_send,
1789 socket_print, proxy_socket_encode, proxy_socket_decode },
1792 /* authentication */
1793 { common_socket_parse, NULL,
1794 auth_socket_recv, auth_socket_send,
1795 socket_print, client_socket_encode, client_socket_decode },
1797 #ifdef WITH_ACCOUNTING
1799 { common_socket_parse, NULL,
1800 acct_socket_recv, acct_socket_send,
1801 socket_print, client_socket_encode, client_socket_decode},
1806 { detail_parse, detail_free,
1807 detail_recv, detail_send,
1808 detail_print, detail_encode, detail_decode },
1812 /* vlan query protocol */
1813 { common_socket_parse, NULL,
1814 vqp_socket_recv, vqp_socket_send,
1815 socket_print, vqp_socket_encode, vqp_socket_decode },
1819 /* dhcp query protocol */
1820 { dhcp_socket_parse, NULL,
1821 dhcp_socket_recv, dhcp_socket_send,
1822 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1825 #ifdef WITH_COMMAND_SOCKET
1826 /* TCP command socket */
1827 { command_socket_parse, NULL,
1828 command_domain_accept, command_domain_send,
1829 command_socket_print, command_socket_encode, command_socket_decode },
1833 /* Change of Authorization */
1834 { common_socket_parse, NULL,
1835 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
1836 socket_print, client_socket_encode, client_socket_decode },
1844 * Binds a listener to a socket.
1846 static int listen_bind(rad_listen_t *this)
1849 struct sockaddr_storage salocal;
1851 listen_socket_t *sock = this->data;
1853 #define proto_for_port "udp"
1854 #define sock_type SOCK_DGRAM
1856 const char *proto_for_port = "udp";
1857 int sock_type = SOCK_DGRAM;
1859 if (sock->proto == IPPROTO_TCP) {
1861 if (this->type == RAD_LISTEN_VQP) {
1862 radlog(L_ERR, "VQP does not support TCP transport");
1867 proto_for_port = "tcp";
1868 sock_type = SOCK_STREAM;
1873 * If the port is zero, then it means the appropriate
1874 * thing from /etc/services.
1876 if (sock->my_port == 0) {
1877 struct servent *svp;
1879 switch (this->type) {
1880 case RAD_LISTEN_AUTH:
1881 svp = getservbyname ("radius", proto_for_port);
1883 sock->my_port = ntohs(svp->s_port);
1885 sock->my_port = PW_AUTH_UDP_PORT;
1889 #ifdef WITH_ACCOUNTING
1890 case RAD_LISTEN_ACCT:
1891 svp = getservbyname ("radacct", proto_for_port);
1893 sock->my_port = ntohs(svp->s_port);
1895 sock->my_port = PW_ACCT_UDP_PORT;
1901 case RAD_LISTEN_PROXY:
1902 /* leave it at zero */
1907 case RAD_LISTEN_VQP:
1908 sock->my_port = 1589;
1913 case RAD_LISTEN_COA:
1914 sock->my_port = PW_COA_UDP_PORT;
1919 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1925 * Copy fr_socket() here, as we may need to bind to a device.
1927 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
1929 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
1933 #ifdef SO_BINDTODEVICE
1935 * Bind to a device BEFORE touching IP addresses.
1937 if (sock->interface) {
1939 strcpy(ifreq.ifr_name, sock->interface);
1942 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1943 (char *)&ifreq, sizeof(ifreq));
1947 radlog(L_ERR, "Failed binding to interface %s: %s",
1948 sock->interface, strerror(errno));
1950 } /* else it worked. */
1955 if (sock->proto == IPPROTO_TCP) {
1958 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
1960 radlog(L_ERR, "Failed to reuse address: %s", strerror(errno));
1966 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
1967 else /* UDP sockets get UDPfromto */
1970 #ifdef WITH_UDPFROMTO
1972 * Initialize udpfromto for all sockets.
1974 if (udpfromto_init(this->fd) != 0) {
1981 * Set up sockaddr stuff.
1983 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
1988 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1989 if (sock->my_ipaddr.af == AF_INET6) {
1991 * Listening on '::' does NOT get you IPv4 to
1992 * IPv6 mapping. You've got to listen on an IPv4
1993 * address, too. This makes the rest of the server
1994 * design a little simpler.
1998 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2001 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2002 (char *)&on, sizeof(on));
2004 #endif /* IPV6_V6ONLY */
2006 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2008 if (sock->my_ipaddr.af == AF_INET) {
2011 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2013 * Disable PMTU discovery. On Linux, this
2014 * also makes sure that the "don't fragment"
2017 flag = IP_PMTUDISC_DONT;
2018 setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2019 &flag, sizeof(flag));
2022 #if defined(IP_DONTFRAG)
2024 * Ensure that the "don't fragment" flag is zero.
2027 setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2028 &flag, sizeof(flag));
2034 if (sock->broadcast) {
2037 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2038 radlog(L_ERR, "Can't set broadcast option: %s\n",
2047 * May be binding to priviledged ports.
2049 if (sock->my_port != 0) {
2053 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2054 radlog(L_ERR, "Can't set re-use address option: %s\n",
2061 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2067 this->print(this, buffer, sizeof(buffer));
2068 radlog(L_ERR, "Failed binding to %s: %s\n",
2069 buffer, strerror(errno));
2074 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2075 * kernel instead binds us to a 1.2.3.4. If this
2076 * happens, notice, and remember our real IP.
2079 struct sockaddr_storage src;
2080 socklen_t sizeof_src = sizeof(src);
2082 memset(&src, 0, sizeof_src);
2083 if (getsockname(this->fd, (struct sockaddr *) &src,
2085 radlog(L_ERR, "Failed getting socket name: %s",
2090 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2091 &sock->my_ipaddr, &sock->my_port)) {
2092 radlog(L_ERR, "Socket has unsupported address family");
2099 if (sock->proto == IPPROTO_TCP) {
2100 if (listen(this->fd, 8) < 0) {
2102 radlog(L_ERR, "Failed in listen(): %s", strerror(errno));
2108 if (fr_nonblock(this->fd) < 0) {
2110 radlog(L_ERR, "Failed setting non-blocking on socket: %s",
2116 * Mostly for proxy sockets.
2118 sock->other_ipaddr.af = sock->my_ipaddr.af;
2121 * Don't screw up other people.
2123 #undef proto_for_port
2131 * Allocate & initialize a new listener.
2133 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
2137 this = rad_malloc(sizeof(*this));
2138 memset(this, 0, sizeof(*this));
2141 this->recv = master_listen[this->type].recv;
2142 this->send = master_listen[this->type].send;
2143 this->print = master_listen[this->type].print;
2144 this->encode = master_listen[this->type].encode;
2145 this->decode = master_listen[this->type].decode;
2149 case RAD_LISTEN_NONE:
2151 case RAD_LISTEN_AUTH:
2152 #ifdef WITH_ACCOUNTING
2153 case RAD_LISTEN_ACCT:
2156 case RAD_LISTEN_PROXY:
2159 case RAD_LISTEN_VQP:
2162 case RAD_LISTEN_COA:
2164 this->data = rad_malloc(sizeof(listen_socket_t));
2165 memset(this->data, 0, sizeof(listen_socket_t));
2169 case RAD_LISTEN_DHCP:
2170 this->data = rad_malloc(sizeof(dhcp_socket_t));
2171 memset(this->data, 0, sizeof(dhcp_socket_t));
2176 case RAD_LISTEN_DETAIL:
2181 #ifdef WITH_COMMAND_SOCKET
2182 case RAD_LISTEN_COMMAND:
2183 this->data = rad_malloc(sizeof(fr_command_socket_t));
2184 memset(this->data, 0, sizeof(fr_command_socket_t));
2189 rad_assert("Unsupported option!" == NULL);
2198 * Externally visible function for creating a new proxy LISTENER.
2200 * Not thread-safe, but all calls to it are protected by the
2201 * proxy mutex in event.c
2203 int proxy_new_listener(home_server *home, int src_port)
2206 listen_socket_t *sock;
2208 if (!home) return 0;
2210 if ((home->max_connections > 0) &&
2211 (home->num_connections >= home->max_connections)) {
2212 DEBUG("WARNING: Home server has too many open connections (%d)",
2213 home->max_connections);
2217 this = listen_alloc(RAD_LISTEN_PROXY);
2220 sock->other_ipaddr = home->ipaddr;
2221 sock->other_port = home->port;
2224 sock->my_ipaddr = home->src_ipaddr;
2225 sock->my_port = src_port;
2226 sock->proto = home->proto;
2229 sock->last_packet = time(NULL);
2231 if (home->proto == IPPROTO_TCP) {
2232 this->recv = proxy_socket_tcp_recv;
2235 * FIXME: connect() is blocking!
2236 * We do this with the proxy mutex locked, which may
2237 * cause large delays!
2239 * http://www.developerweb.net/forum/showthread.php?p=13486
2241 this->fd = fr_tcp_client_socket(&home->src_ipaddr,
2242 &home->ipaddr, home->port);
2245 this->fd = fr_socket(&home->src_ipaddr, src_port);
2248 DEBUG("Failed opening client socket: %s", fr_strerror());
2254 * Figure out which port we were bound to.
2256 if (sock->my_port == 0) {
2257 struct sockaddr_storage src;
2258 socklen_t sizeof_src = sizeof(src);
2260 memset(&src, 0, sizeof_src);
2261 if (getsockname(this->fd, (struct sockaddr *) &src,
2263 radlog(L_ERR, "Failed getting socket name: %s",
2269 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2270 &sock->my_ipaddr, &sock->my_port)) {
2271 radlog(L_ERR, "Socket has unsupported address family");
2278 * Tell the event loop that we have a new FD
2280 if (!event_new_fd(this)) {
2290 static const FR_NAME_NUMBER listen_compare[] = {
2292 { "status", RAD_LISTEN_NONE },
2294 { "auth", RAD_LISTEN_AUTH },
2295 #ifdef WITH_ACCOUNTING
2296 { "acct", RAD_LISTEN_ACCT },
2299 { "detail", RAD_LISTEN_DETAIL },
2302 { "proxy", RAD_LISTEN_PROXY },
2305 { "vmps", RAD_LISTEN_VQP },
2308 { "dhcp", RAD_LISTEN_DHCP },
2310 #ifdef WITH_COMMAND_SOCKET
2311 { "control", RAD_LISTEN_COMMAND },
2314 { "coa", RAD_LISTEN_COA },
2320 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
2328 cf_log_info(cs, "listen {");
2330 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
2332 if (rcode < 0) return NULL;
2335 cf_log_err(cf_sectiontoitem(cs),
2336 "No type specified in listen section");
2340 type = fr_str2int(listen_compare, listen_type, -1);
2342 cf_log_err(cf_sectiontoitem(cs),
2343 "Invalid type \"%s\" in listen section.",
2351 * Allow listen sections in the default config to
2352 * refer to a server.
2355 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
2357 if (rcode == 1) { /* compatiblity with 2.0-pre */
2358 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
2361 if (rcode < 0) return NULL;
2366 * We were passed a virtual server, so the caller is
2367 * defining a proxy listener inside of a virtual server.
2368 * This isn't allowed right now.
2370 else if (type == RAD_LISTEN_PROXY) {
2371 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
2377 * Set up cross-type data.
2379 this = listen_alloc(type);
2380 this->server = server;
2384 * Call per-type parser.
2386 if (master_listen[type].parse(cs, this) < 0) {
2391 cf_log_info(cs, "}");
2397 * Generate a list of listeners. Takes an input list of
2398 * listeners, too, so we don't close sockets with waiting packets.
2400 int listen_init(CONF_SECTION *config, rad_listen_t **head)
2402 int override = FALSE;
2404 CONF_SECTION *cs = NULL;
2405 rad_listen_t **last;
2407 fr_ipaddr_t server_ipaddr;
2410 int defined_proxy = 0;
2414 * We shouldn't be called with a pre-existing list.
2416 rad_assert(head && (*head == NULL));
2419 server_ipaddr.af = AF_UNSPEC;
2422 * If the port is specified on the command-line,
2423 * it over-rides the configuration file.
2425 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
2427 if (mainconfig.port >= 0) auth_port = mainconfig.port;
2430 * If the IP address was configured on the command-line,
2431 * use that as the "bind_address"
2433 if (mainconfig.myip.af != AF_UNSPEC) {
2434 memcpy(&server_ipaddr, &mainconfig.myip,
2435 sizeof(server_ipaddr));
2441 * Else look for bind_address and/or listen sections.
2443 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
2444 rcode = cf_item_parse(config, "bind_address",
2446 &server_ipaddr.ipaddr.ip4addr, NULL);
2447 if (rcode < 0) return -1; /* error parsing it */
2449 if (rcode == 0) { /* successfully parsed IPv4 */
2450 listen_socket_t *sock;
2451 server_ipaddr.af = AF_INET;
2453 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'.");
2457 if (strcmp(progname, "vmpsd") == 0) {
2458 this = listen_alloc(RAD_LISTEN_VQP);
2459 if (!auth_port) auth_port = 1589;
2462 this = listen_alloc(RAD_LISTEN_AUTH);
2466 sock->my_ipaddr = server_ipaddr;
2467 sock->my_port = auth_port;
2469 sock->clients = clients_parse_section(config);
2470 if (!sock->clients) {
2471 cf_log_err(cf_sectiontoitem(config),
2472 "Failed to find any clients for this listen section");
2477 if (listen_bind(this) < 0) {
2479 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
2483 auth_port = sock->my_port; /* may have been updated in listen_bind */
2485 cs = cf_section_sub_find_name2(config, "server",
2487 if (cs) this->server = mainconfig.name;
2491 last = &(this->next);
2497 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
2500 #ifdef WITH_ACCOUNTING
2502 * Open Accounting Socket.
2504 * If we haven't already gotten acct_port from
2505 * /etc/services, then make it auth_port + 1.
2507 this = listen_alloc(RAD_LISTEN_ACCT);
2511 * Create the accounting socket.
2513 * The accounting port is always the
2514 * authentication port + 1
2516 sock->my_ipaddr = server_ipaddr;
2517 sock->my_port = auth_port + 1;
2519 sock->clients = clients_parse_section(config);
2520 if (!sock->clients) {
2521 cf_log_err(cf_sectiontoitem(config),
2522 "Failed to find any clients for this listen section");
2526 if (listen_bind(this) < 0) {
2529 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
2534 cs = cf_section_sub_find_name2(config, "server",
2536 if (cs) this->server = mainconfig.name;
2540 last = &(this->next);
2542 } else if (mainconfig.port > 0) { /* no bind address, but a port */
2543 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
2549 * They specified an IP on the command-line, ignore
2550 * all listen sections except the one in '-n'.
2552 if (mainconfig.myip.af != AF_UNSPEC) {
2553 CONF_SECTION *subcs;
2554 const char *name2 = cf_section_name2(cs);
2556 cs = cf_section_sub_find_name2(config, "server",
2558 if (!cs) goto add_sockets;
2561 * Should really abstract this code...
2563 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2565 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2566 this = listen_parse(subcs, name2);
2573 last = &(this->next);
2574 } /* loop over "listen" directives in server <foo> */
2580 * Walk through the "listen" sections, if they exist.
2582 for (cs = cf_subsection_find_next(config, NULL, "listen");
2584 cs = cf_subsection_find_next(config, cs, "listen")) {
2585 this = listen_parse(cs, NULL);
2592 last = &(this->next);
2596 * Check virtual servers for "listen" sections, too.
2598 * FIXME: Move to virtual server init?
2600 for (cs = cf_subsection_find_next(config, NULL, "server");
2602 cs = cf_subsection_find_next(config, cs, "server")) {
2603 CONF_SECTION *subcs;
2604 const char *name2 = cf_section_name2(cs);
2606 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2608 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2609 this = listen_parse(subcs, name2);
2616 last = &(this->next);
2617 } /* loop over "listen" directives in virtual servers */
2618 } /* loop over virtual servers */
2622 * Print out which sockets we're listening on, and
2623 * add them to the event list.
2625 for (this = *head; this != NULL; this = this->next) {
2627 if (this->type == RAD_LISTEN_PROXY) {
2636 * If we're proxying requests, open the proxy FD.
2637 * Otherwise, don't do anything.
2640 if ((mainconfig.proxy_requests == TRUE) &&
2641 (*head != NULL) && !defined_proxy) {
2642 listen_socket_t *sock = NULL;
2646 memset(&home, 0, sizeof(home));
2651 home.proto = IPPROTO_UDP;
2652 home.src_ipaddr = server_ipaddr;
2655 * Find the first authentication port,
2658 for (this = *head; this != NULL; this = this->next) {
2659 if (this->type == RAD_LISTEN_AUTH) {
2661 if (home.src_ipaddr.af == AF_UNSPEC) {
2662 home.src_ipaddr = sock->my_ipaddr;
2664 port = sock->my_port + 2;
2668 if (this->type == RAD_LISTEN_ACCT) {
2670 if (home.src_ipaddr.af == AF_UNSPEC) {
2671 home.src_ipaddr = sock->my_ipaddr;
2673 port = sock->my_port + 1;
2680 * Address is still unspecified, use IPv4.
2682 if (home.src_ipaddr.af == AF_UNSPEC) {
2683 home.src_ipaddr.af = AF_INET;
2684 /* everything else is already set to zero */
2687 home.ipaddr.af = home.src_ipaddr.af;
2688 /* everything else is already set to zero */
2690 if (!proxy_new_listener(&home, port)) {
2698 * Haven't defined any sockets. Die.
2700 if (!*head) return -1;
2707 * Free a linked list of listeners;
2709 void listen_free(rad_listen_t **head)
2713 if (!head || !*head) return;
2717 rad_listen_t *next = this->next;
2720 * Other code may have eaten the FD.
2722 if (this->fd >= 0) close(this->fd);
2724 if (master_listen[this->type].free) {
2725 master_listen[this->type].free(this);
2729 if ((this->type == RAD_LISTEN_AUTH) ||
2731 (this->type == RAD_LISTEN_ACCT) ||
2734 (this->type == RAD_LISTEN_PROXY)
2737 listen_socket_t *sock = this->data;
2738 rad_free(&sock->packet);
2752 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
2757 for (this = mainconfig.listen; this != NULL; this = this->next) {
2758 listen_socket_t *sock;
2760 if ((this->type != RAD_LISTEN_AUTH) &&
2761 (this->type != RAD_LISTEN_ACCT)) continue;
2765 if ((sock->my_port == port) &&
2766 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
2767 return sock->clients;
2775 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
2779 for (this = mainconfig.listen; this != NULL; this = this->next) {
2780 listen_socket_t *sock;
2783 * FIXME: For TCP, ignore the *secondary*
2784 * listeners associated with the main socket.
2786 if ((this->type != RAD_LISTEN_AUTH) &&
2787 (this->type != RAD_LISTEN_ACCT)) continue;
2791 if ((sock->my_port == port) &&
2792 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
2796 if ((sock->my_port == port) &&
2797 ((sock->my_ipaddr.af == AF_INET) &&
2798 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == INADDR_ANY))) {
2802 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2803 if ((sock->my_port == port) &&
2804 (sock->my_ipaddr.af == AF_INET6) &&
2805 (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr))) {