2 * listen.c Handle socket stuff
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2005,2006 The FreeRADIUS server project
21 * Copyright 2005 Alan DeKok <aland@ox.org>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29 #include <freeradius-devel/rad_assert.h>
30 #include <freeradius-devel/vqp.h>
31 #include <freeradius-devel/dhcp.h>
33 #include <freeradius-devel/vmps.h>
34 #include <freeradius-devel/detail.h>
37 #include <freeradius-devel/udpfromto.h>
40 #ifdef HAVE_SYS_RESOURCE_H
41 #include <sys/resource.h>
53 void print_packet(RADIUS_PACKET *packet)
55 char src[256], dst[256];
57 ip_ntoh(&packet->src_ipaddr, src, sizeof(src));
58 ip_ntoh(&packet->dst_ipaddr, dst, sizeof(dst));
60 fprintf(stderr, "ID %d: %s %d -> %s %d\n", packet->id,
61 src, packet->src_port, dst, packet->dst_port);
67 * We'll use this below.
69 typedef int (*rad_listen_parse_t)(CONF_SECTION *, rad_listen_t *);
70 typedef void (*rad_listen_free_t)(rad_listen_t *);
72 typedef struct rad_listen_master_t {
73 rad_listen_parse_t parse;
74 rad_listen_free_t free;
75 rad_listen_recv_t recv;
76 rad_listen_send_t send;
77 rad_listen_print_t print;
78 rad_listen_encode_t encode;
79 rad_listen_decode_t decode;
80 } rad_listen_master_t;
82 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type);
85 * Find a per-socket client.
87 RADCLIENT *client_listener_find(const rad_listen_t *listener,
88 const fr_ipaddr_t *ipaddr, int src_port)
90 #ifdef WITH_DYNAMIC_CLIENTS
97 RADCLIENT_LIST *clients;
98 listen_socket_t *sock;
100 rad_assert(listener != NULL);
101 rad_assert(ipaddr != NULL);
103 sock = listener->data;
104 clients = sock->clients;
107 * This HAS to have been initialized previously.
109 rad_assert(clients != NULL);
111 client = client_find(clients, ipaddr,sock->proto);
113 char name[256], buffer[128];
115 #ifdef WITH_DYNAMIC_CLIENTS
116 unknown: /* used only for dynamic clients */
120 * DoS attack quenching, but only in daemon mode.
121 * If they're running in debug mode, show them
124 if (debug_flag == 0) {
125 static time_t last_printed = 0;
128 if (last_printed == now) return NULL;
133 listener->print(listener, name, sizeof(name));
135 radlog(L_ERR, "Ignoring request to %s from unknown client %s port %d"
139 , name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
140 buffer, sizeof(buffer)), src_port
142 , (sock->proto == IPPROTO_UDP) ? "udp" : "tcp"
148 #ifndef WITH_DYNAMIC_CLIENTS
149 return client; /* return the found client. */
153 * No server defined, and it's not dynamic. Return it.
155 if (!client->client_server && !client->dynamic) return client;
160 * It's a dynamically generated client, check it.
162 if (client->dynamic && (src_port != 0)) {
164 * Lives forever. Return it.
166 if (client->lifetime == 0) return client;
169 * Rate-limit the deletion of known clients.
170 * This makes them last a little longer, but
171 * prevents the server from melting down if (say)
172 * 10k clients all expire at once.
174 if (now == client->last_new_client) return client;
177 * It's not dead yet. Return it.
179 if ((client->created + client->lifetime) > now) return client;
182 * This really puts them onto a queue for later
185 client_delete(clients, client);
188 * Go find the enclosing network again.
190 client = client_find(clients, ipaddr, sock->proto);
195 if (!client) goto unknown;
196 if (!client->client_server) goto unknown;
199 * At this point, 'client' is the enclosing
200 * network that configures where dynamic clients
203 rad_assert(client->dynamic == 0);
206 * The IP is unknown, so we've found an enclosing
207 * network. Enable DoS protection. We only
208 * allow one new client per second. Known
209 * clients aren't subject to this restriction.
211 if (now == client->last_new_client) goto unknown;
214 client->last_new_client = now;
216 request = request_alloc();
217 if (!request) goto unknown;
219 request->listener = listener;
220 request->client = client;
221 request->packet = rad_recv(listener->fd, 0x02); /* MSG_PEEK */
222 if (!request->packet) { /* badly formed, etc */
223 request_free(&request);
226 request->reply = rad_alloc_reply(request->packet);
227 if (!request->reply) {
228 request_free(&request);
231 request->packet->timestamp = request->timestamp;
233 request->priority = listener->type;
234 request->server = client->client_server;
235 request->root = &mainconfig;
238 * Run a fake request through the given virtual server.
239 * Look for FreeRADIUS-Client-IP-Address
240 * FreeRADIUS-Client-Secret
243 * and create the RADCLIENT structure from that.
245 DEBUG("server %s {", request->server);
247 rcode = module_authorize(0, request);
249 DEBUG("} # server %s", request->server);
251 if (rcode != RLM_MODULE_OK) {
252 request_free(&request);
257 * If the client was updated by rlm_dynamic_clients,
258 * don't create the client from attribute-value pairs.
260 if (request->client == client) {
261 created = client_create(clients, request);
263 created = request->client;
266 * This frees the client if it isn't valid.
268 if (!client_validate(clients, client, created)) goto unknown;
270 request_free(&request);
272 if (!created) goto unknown;
278 static int listen_bind(rad_listen_t *this);
282 * Process and reply to a server-status request.
283 * Like rad_authenticate and rad_accounting this should
284 * live in it's own file but it's so small we don't bother.
286 static int rad_status_server(REQUEST *request)
288 int rcode = RLM_MODULE_OK;
291 switch (request->listener->type) {
293 case RAD_LISTEN_NONE:
295 case RAD_LISTEN_AUTH:
296 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
298 rcode = module_authorize(dval->value, request);
300 rcode = RLM_MODULE_OK;
305 case RLM_MODULE_UPDATED:
306 request->reply->code = PW_AUTHENTICATION_ACK;
309 case RLM_MODULE_FAIL:
310 case RLM_MODULE_HANDLED:
311 request->reply->code = 0; /* don't reply */
315 case RLM_MODULE_REJECT:
316 request->reply->code = PW_AUTHENTICATION_REJECT;
321 #ifdef WITH_ACCOUNTING
322 case RAD_LISTEN_ACCT:
323 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
325 rcode = module_accounting(dval->value, request);
327 rcode = RLM_MODULE_OK;
332 case RLM_MODULE_UPDATED:
333 request->reply->code = PW_ACCOUNTING_RESPONSE;
337 request->reply->code = 0; /* don't reply */
345 * This is a vendor extension. Suggested by Glen
346 * Zorn in IETF 72, and rejected by the rest of
347 * the WG. We like it, so it goes in here.
350 dval = dict_valbyname(PW_RECV_COA_TYPE, "Status-Server");
352 rcode = module_recv_coa(dval->value, request);
354 rcode = RLM_MODULE_OK;
359 case RLM_MODULE_UPDATED:
360 request->reply->code = PW_COA_ACK;
364 request->reply->code = 0; /* don't reply */
376 * Full statistics are available only on a statistics
379 if (request->listener->type == RAD_LISTEN_NONE) {
380 request_stats_reply(request);
388 static int auth_tcp_recv(rad_listen_t *listener,
389 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
392 RADIUS_PACKET *packet;
393 RAD_REQUEST_FUNP fun = NULL;
394 listen_socket_t *sock = listener->data;
395 RADCLIENT *client = sock->client;
398 * Allocate a packet for partial reads.
401 sock->packet = rad_alloc(0);
402 if (!sock->packet) return 0;
404 sock->packet->sockfd = listener->fd;
405 sock->packet->src_ipaddr = sock->other_ipaddr;
406 sock->packet->src_port = sock->other_port;
410 * Grab the packet currently being processed.
412 packet = sock->packet;
415 rcode = fr_tcp_read_packet(packet, 0);
418 * Still only a partial packet. Put it back, and return,
419 * so that we'll read more data when it's ready.
422 sock->packet = packet;
426 if (rcode == -1) { /* error reading packet */
429 radlog(L_ERR, "Invalid packet from %s port %d: closing socket",
430 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
434 if (rcode < 0) { /* error or connection reset */
437 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
440 * Decrement the number of connections.
442 if (sock->parent->num_connections > 0) {
443 sock->parent->num_connections--;
445 if (sock->client->num_connections > 0) {
446 sock->client->num_connections--;
450 * Tell the event handler that an FD has disappeared.
452 DEBUG("Client has closed connection");
453 event_new_fd(listener);
456 * Do NOT free the listener here. It's in use by
457 * a request, and will need to hang around until
458 * all of the requests are done.
460 * It is instead free'd in remove_from_request_hash()
466 RAD_STATS_TYPE_INC(listener, total_requests);
469 * Some sanity checks, based on the packet code.
471 switch(packet->code) {
472 case PW_AUTHENTICATION_REQUEST:
473 RAD_STATS_CLIENT_INC(listener, client, total_requests);
474 fun = rad_authenticate;
477 case PW_STATUS_SERVER:
478 if (!mainconfig.status_server) {
480 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
481 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
482 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
485 fun = rad_status_server;
490 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
491 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
493 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
494 packet->code, client->shortname, packet->src_port);
497 } /* switch over packet types */
499 if (!received_request(listener, packet, prequest, sock->client)) {
500 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
501 RAD_STATS_CLIENT_INC(listener, sock->client, total_packets_dropped);
506 packet->dst_ipaddr = sock->my_ipaddr;
507 packet->dst_port = sock->my_port;
513 static int auth_tcp_accept(rad_listen_t *listener,
514 UNUSED RAD_REQUEST_FUNP *pfun,
515 UNUSED REQUEST **prequest)
520 struct sockaddr_storage src;
521 listen_socket_t *sock;
522 fr_ipaddr_t src_ipaddr;
527 DEBUG2(" ... new connection request on TCP socket.");
529 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
532 * Non-blocking sockets must handle this.
534 if (errno == EWOULDBLOCK) {
538 DEBUG2(" ... failed to accept connection.");
542 if (!fr_sockaddr2ipaddr(&src, salen, &src_ipaddr, &src_port)) {
543 DEBUG2(" ... unknown address family.");
548 * Enforce client IP address checks on accept, not on
551 if ((client = client_listener_find(listener,
552 &src_ipaddr, src_port)) == NULL) {
554 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
559 * Enforce max_connectionsx on client && listen section.
561 if ((client->max_connections != 0) &&
562 (client->max_connections == client->num_connections)) {
564 * FIXME: Print client IP/port, and server IP/port.
566 radlog(L_INFO, "Ignoring new connection due to client max_connections (%d)", client->max_connections);
571 sock = listener->data;
572 if ((sock->max_connections != 0) &&
573 (sock->max_connections == sock->num_connections)) {
575 * FIXME: Print client IP/port, and server IP/port.
577 radlog(L_INFO, "Ignoring new connection due to socket max_connections");
581 client->num_connections++;
582 sock->num_connections++;
585 * Add the new listener.
587 this = listen_alloc(listener->type);
588 if (!this) return -1;
591 * Copy everything, including the pointer to the socket
595 memcpy(this->data, listener->data, sizeof(*sock));
596 memcpy(this, listener, sizeof(*this));
598 this->data = sock; /* fix it back */
600 sock->parent = listener->data;
601 sock->other_ipaddr = src_ipaddr;
602 sock->other_port = src_port;
603 sock->client = client;
606 this->status = RAD_LISTEN_STATUS_INIT;
607 this->recv = auth_tcp_recv;
610 * FIXME: set O_NONBLOCK on the accept'd fd.
611 * See djb's portability rants for details.
615 * Tell the event loop that we have a new FD.
616 * This can be called from a child thread...
626 * This function is stupid and complicated.
628 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
631 listen_socket_t *sock = this->data;
634 switch (this->type) {
636 case RAD_LISTEN_NONE: /* what a hack... */
641 case RAD_LISTEN_AUTH:
642 name = "authentication";
645 #ifdef WITH_ACCOUNTING
646 case RAD_LISTEN_ACCT:
652 case RAD_LISTEN_PROXY:
664 case RAD_LISTEN_DHCP:
680 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
681 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
685 #ifdef SO_BINDTODEVICE
686 if (sock->interface) {
687 ADDSTRING(" interface ");
688 ADDSTRING(sock->interface);
693 if (this->recv == auth_tcp_accept) {
694 ADDSTRING(" proto tcp");
700 * TCP sockets get printed a little differently, to make
701 * it clear what's going on.
704 ADDSTRING(" from client (");
705 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
709 snprintf(buffer, bufsize, "%d", sock->other_port);
713 if ((sock->my_ipaddr.af == AF_INET) &&
714 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
715 strlcpy(buffer, "*", bufsize);
717 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
722 snprintf(buffer, bufsize, "%d", sock->my_port);
726 ADDSTRING(", virtual-server=");
727 ADDSTRING(this->server);
736 * Maybe it's a socket that we opened to a home server.
738 if ((sock->proto == IPPROTO_TCP) &&
739 (this->type == RAD_LISTEN_PROXY)) {
741 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
745 snprintf(buffer, bufsize, "%d", sock->my_port);
747 ADDSTRING(") -> home_server (");
749 if ((sock->other_ipaddr.af == AF_INET) &&
750 (sock->other_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
751 strlcpy(buffer, "*", bufsize);
753 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
758 snprintf(buffer, bufsize, "%d", sock->other_port);
767 ADDSTRING(" address ");
769 if ((sock->my_ipaddr.af == AF_INET) &&
770 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
771 strlcpy(buffer, "*", bufsize);
773 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
778 snprintf(buffer, bufsize, "%d", sock->my_port);
782 ADDSTRING(" as server ");
783 ADDSTRING(this->server);
794 * Parse an authentication or accounting socket.
796 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
801 listen_socket_t *sock = this->data;
802 char *section_name = NULL;
803 CONF_SECTION *client_cs, *parentcs;
808 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
809 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
810 &ipaddr.ipaddr.ip4addr, NULL);
811 if (rcode < 0) return -1;
813 if (rcode == 0) { /* successfully parsed IPv4 */
816 } else { /* maybe IPv6? */
817 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
818 &ipaddr.ipaddr.ip6addr, NULL);
819 if (rcode < 0) return -1;
822 cf_log_err(cf_sectiontoitem(cs),
823 "No address specified in listen section");
826 ipaddr.af = AF_INET6;
829 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
831 if (rcode < 0) return -1;
833 if ((listen_port < 0) || (listen_port > 65535)) {
834 cf_log_err(cf_sectiontoitem(cs),
835 "Invalid value for \"port\"");
839 sock->proto = IPPROTO_UDP;
841 if (cf_pair_find(cs, "proto")) {
843 cf_log_err(cf_sectiontoitem(cs),
844 "System does not support the TCP protocol. Delete this line from the configuration file.");
850 rcode = cf_item_parse(cs, "proto", PW_TYPE_STRING_PTR,
852 if (rcode < 0) return -1;
854 if (strcmp(proto, "udp") == 0) {
855 sock->proto = IPPROTO_UDP;
857 } else if (strcmp(proto, "tcp") == 0) {
858 sock->proto = IPPROTO_TCP;
860 rcode = cf_item_parse(cs, "max_connections", PW_TYPE_INTEGER,
861 &sock->max_connections, "64");
862 if (rcode < 0) return -1;
865 cf_log_err(cf_sectiontoitem(cs),
866 "Unknown proto name \"%s\"", proto);
873 * TCP requires a destination IP for sockets.
874 * UDP doesn't, so it's allowed.
876 if ((this->type == RAD_LISTEN_PROXY) &&
877 (sock->proto != IPPROTO_UDP)) {
878 cf_log_err(cf_sectiontoitem(cs),
879 "Proxy listeners can only listen on proto = udp");
885 sock->my_ipaddr = ipaddr;
886 sock->my_port = listen_port;
889 * If we can bind to interfaces, do so,
892 if (cf_pair_find(cs, "interface")) {
893 #ifndef SO_BINDTODEVICE
894 cf_log_err(cf_sectiontoitem(cs),
895 "System does not support binding to interfaces. Delete this line from the configuration file.");
899 CONF_PAIR *cp = cf_pair_find(cs, "interface");
901 rad_assert(cp != NULL);
902 value = cf_pair_value(cp);
904 cf_log_err(cf_sectiontoitem(cs),
905 "No interface name given");
908 sock->interface = value;
913 * And bind it to the port.
915 if (listen_bind(this) < 0) {
917 cf_log_err(cf_sectiontoitem(cs),
918 "Error binding to port for %s port %d",
919 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
926 * Proxy sockets don't have clients.
928 if (this->type == RAD_LISTEN_PROXY) return 0;
932 * The more specific configurations are preferred to more
936 parentcs = cf_top_section(cs);
937 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
938 §ion_name, NULL);
939 if (rcode < 0) return -1; /* bad string */
942 * Explicit list given: use it.
944 client_cs = cf_section_sub_find_name2(parentcs,
948 client_cs = cf_section_find(section_name);
951 cf_log_err(cf_sectiontoitem(cs),
952 "Failed to find clients %s {...}",
958 } /* else there was no "clients = " entry. */
961 CONF_SECTION *server_cs;
963 server_cs = cf_section_sub_find_name2(parentcs,
967 * Found a "server foo" section. If there are clients
971 (cf_section_sub_find(server_cs, "client") != NULL)) {
972 client_cs = server_cs;
977 * Still nothing. Look for global clients.
979 if (!client_cs) client_cs = parentcs;
981 sock->clients = clients_parse_section(client_cs);
982 if (!sock->clients) {
983 cf_log_err(cf_sectiontoitem(cs),
984 "Failed to load clients for this listen section");
989 if (sock->proto == IPPROTO_TCP) {
991 * Re-write the listener receive function to
992 * allow us to accept the socket.
994 this->recv = auth_tcp_accept;
1002 * Send an authentication response packet
1004 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1006 rad_assert(request->listener == listener);
1007 rad_assert(listener->send == auth_socket_send);
1009 return rad_send(request->reply, request->packet,
1010 request->client->secret);
1014 #ifdef WITH_ACCOUNTING
1016 * Send an accounting response packet (or not)
1018 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1020 rad_assert(request->listener == listener);
1021 rad_assert(listener->send == acct_socket_send);
1024 * Accounting reject's are silently dropped.
1026 * We do it here to avoid polluting the rest of the
1027 * code with this knowledge
1029 if (request->reply->code == 0) return 0;
1031 return rad_send(request->reply, request->packet,
1032 request->client->secret);
1038 * Send a packet to a home server.
1040 * FIXME: have different code for proxy auth & acct!
1042 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1044 rad_assert(request->proxy_listener == listener);
1045 rad_assert(listener->send == proxy_socket_send);
1047 return rad_send(request->proxy, request->packet,
1048 request->home_server->secret);
1054 * Check if an incoming request is "ok"
1056 * It takes packets, not requests. It sees if the packet looks
1057 * OK. If so, it does a number of sanity checks on it.
1059 static int stats_socket_recv(rad_listen_t *listener,
1060 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1064 RADIUS_PACKET *packet;
1066 fr_ipaddr_t src_ipaddr;
1068 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1069 if (rcode < 0) return 0;
1071 RAD_STATS_TYPE_INC(listener, total_requests);
1073 if (rcode < 20) { /* AUTH_HDR_LEN */
1074 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1078 if ((client = client_listener_find(listener,
1079 &src_ipaddr, src_port)) == NULL) {
1080 rad_recv_discard(listener->fd);
1081 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1086 * We only understand Status-Server on this socket.
1088 if (code != PW_STATUS_SERVER) {
1089 DEBUG("Ignoring packet code %d sent to Status-Server port",
1091 rad_recv_discard(listener->fd);
1092 RAD_STATS_TYPE_INC(listener, total_unknown_types);
1093 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1098 * Now that we've sanity checked everything, receive the
1101 packet = rad_recv(listener->fd, 1); /* require message authenticator */
1103 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1104 DEBUG("%s", fr_strerror());
1108 if (!received_request(listener, packet, prequest, client)) {
1109 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1110 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1115 *pfun = rad_status_server;
1122 * Check if an incoming request is "ok"
1124 * It takes packets, not requests. It sees if the packet looks
1125 * OK. If so, it does a number of sanity checks on it.
1127 static int auth_socket_recv(rad_listen_t *listener,
1128 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1132 RADIUS_PACKET *packet;
1133 RAD_REQUEST_FUNP fun = NULL;
1135 fr_ipaddr_t src_ipaddr;
1137 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1138 if (rcode < 0) return 0;
1140 RAD_STATS_TYPE_INC(listener, total_requests);
1142 if (rcode < 20) { /* AUTH_HDR_LEN */
1143 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1147 if ((client = client_listener_find(listener,
1148 &src_ipaddr, src_port)) == NULL) {
1149 rad_recv_discard(listener->fd);
1150 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1155 * Some sanity checks, based on the packet code.
1158 case PW_AUTHENTICATION_REQUEST:
1159 RAD_STATS_CLIENT_INC(listener, client, total_requests);
1160 fun = rad_authenticate;
1163 case PW_STATUS_SERVER:
1164 if (!mainconfig.status_server) {
1165 rad_recv_discard(listener->fd);
1166 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1167 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1168 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1171 fun = rad_status_server;
1175 rad_recv_discard(listener->fd);
1176 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
1177 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1179 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
1180 code, client->shortname, src_port);
1183 } /* switch over packet types */
1186 * Now that we've sanity checked everything, receive the
1189 packet = rad_recv(listener->fd, client->message_authenticator);
1191 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1192 DEBUG("%s", fr_strerror());
1196 if (!received_request(listener, packet, prequest, client)) {
1197 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1198 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1208 #ifdef WITH_ACCOUNTING
1210 * Receive packets from an accounting socket
1212 static int acct_socket_recv(rad_listen_t *listener,
1213 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1217 RADIUS_PACKET *packet;
1218 RAD_REQUEST_FUNP fun = NULL;
1220 fr_ipaddr_t src_ipaddr;
1222 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1223 if (rcode < 0) return 0;
1225 RAD_STATS_TYPE_INC(listener, total_requests);
1227 if (rcode < 20) { /* AUTH_HDR_LEN */
1228 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1232 if ((client = client_listener_find(listener,
1233 &src_ipaddr, src_port)) == NULL) {
1234 rad_recv_discard(listener->fd);
1235 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1240 * Some sanity checks, based on the packet code.
1243 case PW_ACCOUNTING_REQUEST:
1244 RAD_STATS_CLIENT_INC(listener, client, total_requests);
1245 fun = rad_accounting;
1248 case PW_STATUS_SERVER:
1249 if (!mainconfig.status_server) {
1250 rad_recv_discard(listener->fd);
1251 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1252 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1254 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1257 fun = rad_status_server;
1261 rad_recv_discard(listener->fd);
1262 RAD_STATS_TYPE_INC(listener, total_unknown_types);
1263 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1265 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1266 code, client->shortname, src_port);
1268 } /* switch over packet types */
1271 * Now that we've sanity checked everything, receive the
1274 packet = rad_recv(listener->fd, 0);
1276 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1277 radlog(L_ERR, "%s", fr_strerror());
1282 * There can be no duplicate accounting packets.
1284 if (!received_request(listener, packet, prequest, client)) {
1285 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1286 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1299 * For now, all CoA requests are *only* originated, and not
1300 * proxied. So all of the necessary work is done in the
1301 * post-proxy section, which is automatically handled by event.c.
1302 * As a result, we don't have to do anything here.
1304 static int rad_coa_reply(REQUEST *request)
1306 VALUE_PAIR *s1, *s2;
1309 * Inform the user about RFC requirements.
1311 s1 = pairfind(request->proxy->vps, PW_STATE);
1313 s2 = pairfind(request->proxy_reply->vps, PW_STATE);
1316 DEBUG("WARNING: Client was sent State in CoA, and did not respond with State.");
1318 } else if ((s1->length != s2->length) ||
1319 (memcmp(s1->vp_octets, s2->vp_octets,
1320 s1->length) != 0)) {
1321 DEBUG("WARNING: Client was sent State in CoA, and did not respond with the same State.");
1325 return RLM_MODULE_OK;
1329 * Receive a CoA packet.
1331 static int rad_coa_recv(REQUEST *request)
1333 int rcode = RLM_MODULE_OK;
1338 * Get the correct response
1340 switch (request->packet->code) {
1341 case PW_COA_REQUEST:
1346 case PW_DISCONNECT_REQUEST:
1347 ack = PW_DISCONNECT_ACK;
1348 nak = PW_DISCONNECT_NAK;
1351 default: /* shouldn't happen */
1352 return RLM_MODULE_FAIL;
1356 #define WAS_PROXIED (request->proxy)
1358 #define WAS_PROXIED (0)
1363 * RFC 5176 Section 3.3. If we have a CoA-Request
1364 * with Service-Type = Authorize-Only, it MUST
1365 * have a State attribute in it.
1367 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE);
1368 if (request->packet->code == PW_COA_REQUEST) {
1369 if (vp && (vp->vp_integer == 17)) {
1370 vp = pairfind(request->packet->vps, PW_STATE);
1371 if (!vp || (vp->length == 0)) {
1372 RDEBUG("ERROR: CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1373 request->reply->code = PW_COA_NAK;
1374 return RLM_MODULE_FAIL;
1379 * RFC 5176, Section 3.2.
1381 RDEBUG("ERROR: Disconnect-Request MUST NOT contain a Service-Type attribute");
1382 request->reply->code = PW_DISCONNECT_NAK;
1383 return RLM_MODULE_FAIL;
1386 rcode = module_recv_coa(0, request);
1388 case RLM_MODULE_FAIL:
1389 case RLM_MODULE_INVALID:
1390 case RLM_MODULE_REJECT:
1391 case RLM_MODULE_USERLOCK:
1393 request->reply->code = nak;
1396 case RLM_MODULE_HANDLED:
1399 case RLM_MODULE_NOOP:
1400 case RLM_MODULE_NOTFOUND:
1402 case RLM_MODULE_UPDATED:
1403 request->reply->code = ack;
1408 * Start the reply code with the proxy reply
1411 request->reply->code = request->proxy_reply->code;
1415 * Copy State from the request to the reply.
1416 * See RFC 5176 Section 3.3.
1418 vp = paircopy2(request->packet->vps, PW_STATE);
1419 if (vp) pairadd(&request->reply->vps, vp);
1422 * We may want to over-ride the reply.
1424 rcode = module_send_coa(0, request);
1427 * We need to send CoA-NAK back if Service-Type
1428 * is Authorize-Only. Rely on the user's policy
1429 * to do that. We're not a real NAS, so this
1430 * restriction doesn't (ahem) apply to us.
1432 case RLM_MODULE_FAIL:
1433 case RLM_MODULE_INVALID:
1434 case RLM_MODULE_REJECT:
1435 case RLM_MODULE_USERLOCK:
1438 * Over-ride an ACK with a NAK
1440 request->reply->code = nak;
1443 case RLM_MODULE_HANDLED:
1446 case RLM_MODULE_NOOP:
1447 case RLM_MODULE_NOTFOUND:
1449 case RLM_MODULE_UPDATED:
1451 * Do NOT over-ride a previously set value.
1452 * Otherwise an "ok" here will re-write a
1455 if (request->reply->code == 0) {
1456 request->reply->code = ack;
1462 return RLM_MODULE_OK;
1467 * Check if an incoming request is "ok"
1469 * It takes packets, not requests. It sees if the packet looks
1470 * OK. If so, it does a number of sanity checks on it.
1472 static int coa_socket_recv(rad_listen_t *listener,
1473 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1477 RADIUS_PACKET *packet;
1478 RAD_REQUEST_FUNP fun = NULL;
1480 fr_ipaddr_t src_ipaddr;
1482 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1483 if (rcode < 0) return 0;
1485 RAD_STATS_TYPE_INC(listener, total_requests);
1487 if (rcode < 20) { /* AUTH_HDR_LEN */
1488 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1492 if ((client = client_listener_find(listener,
1493 &src_ipaddr, src_port)) == NULL) {
1494 rad_recv_discard(listener->fd);
1495 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1500 * Some sanity checks, based on the packet code.
1503 case PW_COA_REQUEST:
1504 case PW_DISCONNECT_REQUEST:
1509 rad_recv_discard(listener->fd);
1510 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1511 code, client->shortname, src_port);
1514 } /* switch over packet types */
1517 * Now that we've sanity checked everything, receive the
1520 packet = rad_recv(listener->fd, client->message_authenticator);
1522 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1523 DEBUG("%s", fr_strerror());
1527 if (!received_request(listener, packet, prequest, client)) {
1539 * Recieve packets from a proxy socket.
1541 static int proxy_socket_recv(rad_listen_t *listener,
1542 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1545 RADIUS_PACKET *packet;
1546 RAD_REQUEST_FUNP fun = NULL;
1549 packet = rad_recv(listener->fd, 0);
1551 radlog(L_ERR, "%s", fr_strerror());
1556 * FIXME: Client MIB updates?
1558 switch(packet->code) {
1559 case PW_AUTHENTICATION_ACK:
1560 case PW_ACCESS_CHALLENGE:
1561 case PW_AUTHENTICATION_REJECT:
1562 fun = rad_authenticate;
1565 #ifdef WITH_ACCOUNTING
1566 case PW_ACCOUNTING_RESPONSE:
1567 fun = rad_accounting;
1572 case PW_DISCONNECT_ACK:
1573 case PW_DISCONNECT_NAK:
1576 fun = rad_coa_reply;
1582 * FIXME: Update MIB for packet types?
1584 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1585 "from home server %s port %d - ID %d : IGNORED",
1587 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1588 packet->src_port, packet->id);
1593 request = received_proxy_response(packet);
1601 * Distinguish proxied CoA requests from ones we
1604 if ((fun == rad_coa_reply) &&
1605 (request->packet->code == request->proxy->code)) {
1610 rad_assert(fun != NULL);
1612 *prequest = request;
1619 * Recieve packets from a proxy socket.
1621 static int proxy_socket_tcp_recv(rad_listen_t *listener,
1622 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1625 RADIUS_PACKET *packet;
1626 RAD_REQUEST_FUNP fun = NULL;
1627 listen_socket_t *sock = listener->data;
1630 packet = fr_tcp_recv(listener->fd, 0);
1632 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
1633 event_new_fd(listener);
1638 * FIXME: Client MIB updates?
1640 switch(packet->code) {
1641 case PW_AUTHENTICATION_ACK:
1642 case PW_ACCESS_CHALLENGE:
1643 case PW_AUTHENTICATION_REJECT:
1644 fun = rad_authenticate;
1647 #ifdef WITH_ACCOUNTING
1648 case PW_ACCOUNTING_RESPONSE:
1649 fun = rad_accounting;
1655 * FIXME: Update MIB for packet types?
1657 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1658 "from home server %s port %d - ID %d : IGNORED",
1660 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1661 packet->src_port, packet->id);
1666 packet->src_ipaddr = sock->other_ipaddr;
1667 packet->src_port = sock->other_port;
1668 packet->dst_ipaddr = sock->my_ipaddr;
1669 packet->dst_port = sock->my_port;
1672 * FIXME: Have it return an indication of packets that
1673 * are OK to ignore (dups, too late), versus ones that
1674 * aren't OK to ignore (unknown response, spoofed, etc.)
1676 * Close the socket on bad packets...
1678 request = received_proxy_response(packet);
1683 rad_assert(fun != NULL);
1684 sock->opened = sock->last_packet = request->timestamp;
1687 *prequest = request;
1695 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1697 if (!request->reply->code) return 0;
1699 rad_encode(request->reply, request->packet,
1700 request->client->secret);
1701 rad_sign(request->reply, request->packet,
1702 request->client->secret);
1708 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1710 if (rad_verify(request->packet, NULL,
1711 request->client->secret) < 0) {
1715 return rad_decode(request->packet, NULL,
1716 request->client->secret);
1720 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1722 rad_encode(request->proxy, NULL, request->home_server->secret);
1723 rad_sign(request->proxy, NULL, request->home_server->secret);
1729 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1732 * rad_verify is run in event.c, received_proxy_response()
1735 return rad_decode(request->proxy_reply, request->proxy,
1736 request->home_server->secret);
1742 #include "command.c"
1744 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1746 { common_socket_parse, NULL,
1747 stats_socket_recv, auth_socket_send,
1748 socket_print, client_socket_encode, client_socket_decode },
1751 * This always gets defined.
1753 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1758 { common_socket_parse, NULL,
1759 proxy_socket_recv, proxy_socket_send,
1760 socket_print, proxy_socket_encode, proxy_socket_decode },
1763 /* authentication */
1764 { common_socket_parse, NULL,
1765 auth_socket_recv, auth_socket_send,
1766 socket_print, client_socket_encode, client_socket_decode },
1768 #ifdef WITH_ACCOUNTING
1770 { common_socket_parse, NULL,
1771 acct_socket_recv, acct_socket_send,
1772 socket_print, client_socket_encode, client_socket_decode},
1777 { detail_parse, detail_free,
1778 detail_recv, detail_send,
1779 detail_print, detail_encode, detail_decode },
1783 /* vlan query protocol */
1784 { common_socket_parse, NULL,
1785 vqp_socket_recv, vqp_socket_send,
1786 socket_print, vqp_socket_encode, vqp_socket_decode },
1790 /* dhcp query protocol */
1791 { dhcp_socket_parse, NULL,
1792 dhcp_socket_recv, dhcp_socket_send,
1793 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1796 #ifdef WITH_COMMAND_SOCKET
1797 /* TCP command socket */
1798 { command_socket_parse, NULL,
1799 command_domain_accept, command_domain_send,
1800 command_socket_print, command_socket_encode, command_socket_decode },
1804 /* Change of Authorization */
1805 { common_socket_parse, NULL,
1806 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
1807 socket_print, client_socket_encode, client_socket_decode },
1815 * Binds a listener to a socket.
1817 static int listen_bind(rad_listen_t *this)
1820 struct sockaddr_storage salocal;
1822 listen_socket_t *sock = this->data;
1824 #define proto_for_port "udp"
1825 #define sock_type SOCK_DGRAM
1827 const char *proto_for_port = "udp";
1828 int sock_type = SOCK_DGRAM;
1830 if (sock->proto == IPPROTO_TCP) {
1832 if (this->type == RAD_LISTEN_VQP) {
1833 radlog(L_ERR, "VQP does not support TCP transport");
1838 proto_for_port = "tcp";
1839 sock_type = SOCK_STREAM;
1844 * If the port is zero, then it means the appropriate
1845 * thing from /etc/services.
1847 if (sock->my_port == 0) {
1848 struct servent *svp;
1850 switch (this->type) {
1851 case RAD_LISTEN_AUTH:
1852 svp = getservbyname ("radius", proto_for_port);
1854 sock->my_port = ntohs(svp->s_port);
1856 sock->my_port = PW_AUTH_UDP_PORT;
1860 #ifdef WITH_ACCOUNTING
1861 case RAD_LISTEN_ACCT:
1862 svp = getservbyname ("radacct", proto_for_port);
1864 sock->my_port = ntohs(svp->s_port);
1866 sock->my_port = PW_ACCT_UDP_PORT;
1872 case RAD_LISTEN_PROXY:
1873 /* leave it at zero */
1878 case RAD_LISTEN_VQP:
1879 sock->my_port = 1589;
1884 case RAD_LISTEN_COA:
1885 sock->my_port = PW_COA_UDP_PORT;
1890 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1896 * Copy fr_socket() here, as we may need to bind to a device.
1898 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
1900 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
1904 #ifdef SO_BINDTODEVICE
1906 * Bind to a device BEFORE touching IP addresses.
1908 if (sock->interface) {
1910 strcpy(ifreq.ifr_name, sock->interface);
1913 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1914 (char *)&ifreq, sizeof(ifreq));
1918 radlog(L_ERR, "Failed binding to interface %s: %s",
1919 sock->interface, strerror(errno));
1921 } /* else it worked. */
1926 if (sock->proto == IPPROTO_TCP) {
1929 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
1931 radlog(L_ERR, "Failed to reuse address: %s", strerror(errno));
1937 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
1938 else /* UDP sockets get UDPfromto */
1941 #ifdef WITH_UDPFROMTO
1943 * Initialize udpfromto for all sockets.
1945 if (udpfromto_init(this->fd) != 0) {
1952 * Set up sockaddr stuff.
1954 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
1959 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1960 if (sock->my_ipaddr.af == AF_INET6) {
1962 * Listening on '::' does NOT get you IPv4 to
1963 * IPv6 mapping. You've got to listen on an IPv4
1964 * address, too. This makes the rest of the server
1965 * design a little simpler.
1969 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
1972 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
1973 (char *)&on, sizeof(on));
1975 #endif /* IPV6_V6ONLY */
1977 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
1979 if (sock->my_ipaddr.af == AF_INET) {
1982 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
1984 * Disable PMTU discovery. On Linux, this
1985 * also makes sure that the "don't fragment"
1988 flag = IP_PMTUDISC_DONT;
1989 setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
1990 &flag, sizeof(flag));
1993 #if defined(IP_DONTFRAG)
1995 * Ensure that the "don't fragment" flag is zero.
1998 setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
1999 &flag, sizeof(flag));
2004 * May be binding to priviledged ports.
2006 if (sock->my_port != 0) {
2008 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2014 this->print(this, buffer, sizeof(buffer));
2015 radlog(L_ERR, "Failed binding to %s: %s\n",
2016 buffer, strerror(errno));
2021 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2022 * kernel instead binds us to a 1.2.3.4. If this
2023 * happens, notice, and remember our real IP.
2026 struct sockaddr_storage src;
2027 socklen_t sizeof_src = sizeof(src);
2029 memset(&src, 0, sizeof_src);
2030 if (getsockname(this->fd, (struct sockaddr *) &src,
2032 radlog(L_ERR, "Failed getting socket name: %s",
2037 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2038 &sock->my_ipaddr, &sock->my_port)) {
2039 radlog(L_ERR, "Socket has unsupported address family");
2046 if (sock->proto == IPPROTO_TCP) {
2047 if (listen(this->fd, 8) < 0) {
2049 radlog(L_ERR, "Failed in listen(): %s", strerror(errno));
2055 if (fr_nonblock(this->fd) < 0) {
2057 radlog(L_ERR, "Failed setting non-blocking on socket: %s",
2063 * Mostly for proxy sockets.
2065 sock->other_ipaddr.af = sock->my_ipaddr.af;
2068 * Don't screw up other people.
2070 #undef proto_for_port
2078 * Allocate & initialize a new listener.
2080 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
2084 this = rad_malloc(sizeof(*this));
2085 memset(this, 0, sizeof(*this));
2088 this->recv = master_listen[this->type].recv;
2089 this->send = master_listen[this->type].send;
2090 this->print = master_listen[this->type].print;
2091 this->encode = master_listen[this->type].encode;
2092 this->decode = master_listen[this->type].decode;
2096 case RAD_LISTEN_NONE:
2098 case RAD_LISTEN_AUTH:
2099 #ifdef WITH_ACCOUNTING
2100 case RAD_LISTEN_ACCT:
2103 case RAD_LISTEN_PROXY:
2106 case RAD_LISTEN_VQP:
2109 case RAD_LISTEN_COA:
2111 this->data = rad_malloc(sizeof(listen_socket_t));
2112 memset(this->data, 0, sizeof(listen_socket_t));
2116 case RAD_LISTEN_DHCP:
2117 this->data = rad_malloc(sizeof(dhcp_socket_t));
2118 memset(this->data, 0, sizeof(dhcp_socket_t));
2123 case RAD_LISTEN_DETAIL:
2128 #ifdef WITH_COMMAND_SOCKET
2129 case RAD_LISTEN_COMMAND:
2130 this->data = rad_malloc(sizeof(fr_command_socket_t));
2131 memset(this->data, 0, sizeof(fr_command_socket_t));
2136 rad_assert("Unsupported option!" == NULL);
2145 * Externally visible function for creating a new proxy LISTENER.
2147 * Not thread-safe, but all calls to it are protected by the
2148 * proxy mutex in event.c
2150 int proxy_new_listener(home_server *home, int src_port)
2153 listen_socket_t *sock;
2155 if (!home) return 0;
2157 if ((home->max_connections > 0) &&
2158 (home->num_connections >= home->max_connections)) {
2159 DEBUG("WARNING: Home server has too many open connections (%d)",
2160 home->max_connections);
2164 this = listen_alloc(RAD_LISTEN_PROXY);
2167 sock->other_ipaddr = home->ipaddr;
2168 sock->other_port = home->port;
2171 sock->my_ipaddr = home->src_ipaddr;
2172 sock->my_port = src_port;
2173 sock->proto = home->proto;
2176 sock->last_packet = time(NULL);
2178 if (home->proto == IPPROTO_TCP) {
2179 this->recv = proxy_socket_tcp_recv;
2182 * FIXME: connect() is blocking!
2183 * We do this with the proxy mutex locked, which may
2184 * cause large delays!
2186 * http://www.developerweb.net/forum/showthread.php?p=13486
2188 this->fd = fr_tcp_client_socket(&home->src_ipaddr,
2189 &home->ipaddr, home->port);
2192 this->fd = fr_socket(&home->src_ipaddr, src_port);
2195 DEBUG("Failed opening client socket: %s", fr_strerror());
2201 * Figure out which port we were bound to.
2203 if (sock->my_port == 0) {
2204 struct sockaddr_storage src;
2205 socklen_t sizeof_src = sizeof(src);
2207 memset(&src, 0, sizeof_src);
2208 if (getsockname(this->fd, (struct sockaddr *) &src,
2210 radlog(L_ERR, "Failed getting socket name: %s",
2216 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2217 &sock->my_ipaddr, &sock->my_port)) {
2218 radlog(L_ERR, "Socket has unsupported address family");
2225 * Tell the event loop that we have a new FD
2234 static const FR_NAME_NUMBER listen_compare[] = {
2236 { "status", RAD_LISTEN_NONE },
2238 { "auth", RAD_LISTEN_AUTH },
2239 #ifdef WITH_ACCOUNTING
2240 { "acct", RAD_LISTEN_ACCT },
2243 { "detail", RAD_LISTEN_DETAIL },
2246 { "proxy", RAD_LISTEN_PROXY },
2249 { "vmps", RAD_LISTEN_VQP },
2252 { "dhcp", RAD_LISTEN_DHCP },
2254 #ifdef WITH_COMMAND_SOCKET
2255 { "control", RAD_LISTEN_COMMAND },
2258 { "coa", RAD_LISTEN_COA },
2264 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
2272 cf_log_info(cs, "listen {");
2274 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
2276 if (rcode < 0) return NULL;
2279 cf_log_err(cf_sectiontoitem(cs),
2280 "No type specified in listen section");
2284 type = fr_str2int(listen_compare, listen_type, -1);
2286 cf_log_err(cf_sectiontoitem(cs),
2287 "Invalid type \"%s\" in listen section.",
2295 * Allow listen sections in the default config to
2296 * refer to a server.
2299 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
2301 if (rcode == 1) { /* compatiblity with 2.0-pre */
2302 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
2305 if (rcode < 0) return NULL;
2310 * We were passed a virtual server, so the caller is
2311 * defining a proxy listener inside of a virtual server.
2312 * This isn't allowed right now.
2314 else if (this->type == RAD_LISTEN_PROXY) {
2315 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
2321 * Set up cross-type data.
2323 this = listen_alloc(type);
2324 this->server = server;
2328 * Call per-type parser.
2330 if (master_listen[type].parse(cs, this) < 0) {
2335 cf_log_info(cs, "}");
2341 * Generate a list of listeners. Takes an input list of
2342 * listeners, too, so we don't close sockets with waiting packets.
2344 int listen_init(CONF_SECTION *config, rad_listen_t **head)
2346 int override = FALSE;
2348 CONF_SECTION *cs = NULL;
2349 rad_listen_t **last;
2351 fr_ipaddr_t server_ipaddr;
2354 int defined_proxy = 0;
2358 * We shouldn't be called with a pre-existing list.
2360 rad_assert(head && (*head == NULL));
2363 server_ipaddr.af = AF_UNSPEC;
2366 * If the port is specified on the command-line,
2367 * it over-rides the configuration file.
2369 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
2371 if (mainconfig.port >= 0) auth_port = mainconfig.port;
2374 * If the IP address was configured on the command-line,
2375 * use that as the "bind_address"
2377 if (mainconfig.myip.af != AF_UNSPEC) {
2378 memcpy(&server_ipaddr, &mainconfig.myip,
2379 sizeof(server_ipaddr));
2385 * Else look for bind_address and/or listen sections.
2387 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
2388 rcode = cf_item_parse(config, "bind_address",
2390 &server_ipaddr.ipaddr.ip4addr, NULL);
2391 if (rcode < 0) return -1; /* error parsing it */
2393 if (rcode == 0) { /* successfully parsed IPv4 */
2394 listen_socket_t *sock;
2395 server_ipaddr.af = AF_INET;
2397 radlog(L_INFO, "WARNING: The directive 'bind_adress' is deprecated, and will be removed in future versions of FreeRADIUS. Please edit the configuration files to use the directive 'listen'.");
2401 if (strcmp(progname, "vmpsd") == 0) {
2402 this = listen_alloc(RAD_LISTEN_VQP);
2403 if (!auth_port) auth_port = 1589;
2406 this = listen_alloc(RAD_LISTEN_AUTH);
2410 sock->my_ipaddr = server_ipaddr;
2411 sock->my_port = auth_port;
2413 sock->clients = clients_parse_section(config);
2414 if (!sock->clients) {
2415 cf_log_err(cf_sectiontoitem(config),
2416 "Failed to find any clients for this listen section");
2421 if (listen_bind(this) < 0) {
2423 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
2427 auth_port = sock->my_port; /* may have been updated in listen_bind */
2429 cs = cf_section_sub_find_name2(config, "server",
2431 if (cs) this->server = mainconfig.name;
2435 last = &(this->next);
2441 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
2444 #ifdef WITH_ACCOUNTING
2446 * Open Accounting Socket.
2448 * If we haven't already gotten acct_port from
2449 * /etc/services, then make it auth_port + 1.
2451 this = listen_alloc(RAD_LISTEN_ACCT);
2455 * Create the accounting socket.
2457 * The accounting port is always the
2458 * authentication port + 1
2460 sock->my_ipaddr = server_ipaddr;
2461 sock->my_port = auth_port + 1;
2463 sock->clients = clients_parse_section(config);
2464 if (!sock->clients) {
2465 cf_log_err(cf_sectiontoitem(config),
2466 "Failed to find any clients for this listen section");
2470 if (listen_bind(this) < 0) {
2473 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
2478 cs = cf_section_sub_find_name2(config, "server",
2480 if (cs) this->server = mainconfig.name;
2484 last = &(this->next);
2486 } else if (mainconfig.port > 0) { /* no bind address, but a port */
2487 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
2493 * They specified an IP on the command-line, ignore
2494 * all listen sections except the one in '-n'.
2496 if (mainconfig.myip.af != AF_UNSPEC) {
2497 CONF_SECTION *subcs;
2498 const char *name2 = cf_section_name2(cs);
2500 cs = cf_section_sub_find_name2(config, "server",
2502 if (!cs) goto add_sockets;
2505 * Should really abstract this code...
2507 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2509 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2510 this = listen_parse(subcs, name2);
2517 last = &(this->next);
2518 } /* loop over "listen" directives in server <foo> */
2524 * Walk through the "listen" sections, if they exist.
2526 for (cs = cf_subsection_find_next(config, NULL, "listen");
2528 cs = cf_subsection_find_next(config, cs, "listen")) {
2529 this = listen_parse(cs, NULL);
2536 last = &(this->next);
2540 * Check virtual servers for "listen" sections, too.
2542 * FIXME: Move to virtual server init?
2544 for (cs = cf_subsection_find_next(config, NULL, "server");
2546 cs = cf_subsection_find_next(config, cs, "server")) {
2547 CONF_SECTION *subcs;
2548 const char *name2 = cf_section_name2(cs);
2550 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2552 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2553 this = listen_parse(subcs, name2);
2560 last = &(this->next);
2561 } /* loop over "listen" directives in virtual servers */
2562 } /* loop over virtual servers */
2566 * Print out which sockets we're listening on, and
2567 * add them to the event list.
2569 for (this = *head; this != NULL; this = this->next) {
2571 if (this->type == RAD_LISTEN_PROXY) {
2580 * If we're proxying requests, open the proxy FD.
2581 * Otherwise, don't do anything.
2584 if ((mainconfig.proxy_requests == TRUE) &&
2585 (*head != NULL) && !defined_proxy) {
2586 listen_socket_t *sock = NULL;
2590 memset(&home, 0, sizeof(home));
2596 home.proto = IPPROTO_UDP;
2599 home.src_ipaddr = server_ipaddr;
2602 * Find the first authentication port,
2605 for (this = *head; this != NULL; this = this->next) {
2606 if (this->type == RAD_LISTEN_AUTH) {
2608 if (home.src_ipaddr.af == AF_UNSPEC) {
2609 home.src_ipaddr = sock->my_ipaddr;
2611 port = sock->my_port + 2;
2615 if (this->type == RAD_LISTEN_ACCT) {
2617 if (home.src_ipaddr.af == AF_UNSPEC) {
2618 home.src_ipaddr = sock->my_ipaddr;
2620 port = sock->my_port + 1;
2627 * Address is still unspecified, use IPv4.
2629 if (home.src_ipaddr.af == AF_UNSPEC) {
2630 home.src_ipaddr.af = AF_INET;
2631 /* everything else is already set to zero */
2634 home.ipaddr.af = home.src_ipaddr.af;
2635 /* everything else is already set to zero */
2637 if (!proxy_new_listener(&home, port)) {
2645 * Haven't defined any sockets. Die.
2647 if (!*head) return -1;
2654 * Free a linked list of listeners;
2656 void listen_free(rad_listen_t **head)
2660 if (!head || !*head) return;
2664 rad_listen_t *next = this->next;
2667 * Other code may have eaten the FD.
2669 if (this->fd >= 0) close(this->fd);
2671 if (master_listen[this->type].free) {
2672 master_listen[this->type].free(this);
2676 if ((this->type == RAD_LISTEN_AUTH) ||
2678 (this->type == RAD_LISTEN_ACCT) ||
2681 (this->type == RAD_LISTEN_PROXY)
2684 listen_socket_t *sock = this->data;
2685 rad_free(&sock->packet);
2699 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
2704 for (this = mainconfig.listen; this != NULL; this = this->next) {
2705 listen_socket_t *sock;
2707 if ((this->type != RAD_LISTEN_AUTH) &&
2708 (this->type != RAD_LISTEN_ACCT)) continue;
2712 if ((sock->my_port == port) &&
2713 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
2714 return sock->clients;
2722 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
2726 for (this = mainconfig.listen; this != NULL; this = this->next) {
2727 listen_socket_t *sock;
2730 * FIXME: For TCP, ignore the *secondary*
2731 * listeners associated with the main socket.
2733 if ((this->type != RAD_LISTEN_AUTH) &&
2734 (this->type != RAD_LISTEN_ACCT)) continue;
2738 if ((sock->my_port == port) &&
2739 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
2743 if ((sock->my_port == port) &&
2744 ((sock->my_ipaddr.af == AF_INET) &&
2745 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == INADDR_ANY))) {
2749 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2750 if ((sock->my_port == port) &&
2751 (sock->my_ipaddr.af == AF_INET6) &&
2752 (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr))) {