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>
54 * We'll use this below.
56 typedef int (*rad_listen_parse_t)(CONF_SECTION *, rad_listen_t *);
57 typedef void (*rad_listen_free_t)(rad_listen_t *);
59 typedef struct rad_listen_master_t {
60 rad_listen_parse_t parse;
61 rad_listen_free_t free;
62 rad_listen_recv_t recv;
63 rad_listen_send_t send;
64 rad_listen_print_t print;
65 rad_listen_encode_t encode;
66 rad_listen_decode_t decode;
67 } rad_listen_master_t;
69 typedef struct listen_socket_t {
76 #ifdef SO_BINDTODEVICE
77 const char *interface;
85 struct listen_socket_t *parent;
87 fr_ipaddr_t src_ipaddr;
89 RADCLIENT *client; /* for server sockets */
91 fr_tcp_radius_t *tcp; /* for RAD_LISTEN_PROXY */
93 RADIUS_PACKET *packet; /* for reading partial packets */
95 RADCLIENT_LIST *clients;
98 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type);
101 * Find a per-socket client.
103 RADCLIENT *client_listener_find(const rad_listen_t *listener,
104 const fr_ipaddr_t *ipaddr, int src_port)
106 #ifdef WITH_DYNAMIC_CLIENTS
113 RADCLIENT_LIST *clients;
115 rad_assert(listener != NULL);
116 rad_assert(ipaddr != NULL);
118 clients = ((listen_socket_t *)listener->data)->clients;
121 * This HAS to have been initialized previously.
123 rad_assert(clients != NULL);
125 client = client_find(clients, ipaddr
131 static time_t last_printed = 0;
132 char name[256], buffer[128];
134 #ifdef WITH_DYNAMIC_CLIENTS
135 unknown: /* used only for dynamic clients */
139 * DoS attack quenching, but only in debug mode.
140 * If they're running in debug mode, show them
143 if (debug_flag == 0) {
145 if (last_printed == now) return NULL;
150 listener->print(listener, name, sizeof(name));
152 radlog(L_ERR, "Ignoring request to %s from unknown client %s port %d",
153 name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
154 buffer, sizeof(buffer)),
159 #ifndef WITH_DYNAMIC_CLIENTS
160 return client; /* return the found client. */
164 * No server defined, and it's not dynamic. Return it.
166 if (!client->client_server && !client->dynamic) return client;
171 * It's a dynamically generated client, check it.
173 if (client->dynamic && (src_port != 0)) {
175 * Lives forever. Return it.
177 if (client->lifetime == 0) return client;
180 * Rate-limit the deletion of known clients.
181 * This makes them last a little longer, but
182 * prevents the server from melting down if (say)
183 * 10k clients all expire at once.
185 if (now == client->last_new_client) return client;
188 * It's not dead yet. Return it.
190 if ((client->created + client->lifetime) > now) return client;
193 * This really puts them onto a queue for later
196 client_delete(clients, client);
199 * Go find the enclosing network again.
201 client = client_find(clients, ipaddr
210 if (!client) goto unknown;
211 if (!client->client_server) goto unknown;
214 * At this point, 'client' is the enclosing
215 * network that configures where dynamic clients
218 rad_assert(client->dynamic == 0);
221 * The IP is unknown, so we've found an enclosing
222 * network. Enable DoS protection. We only
223 * allow one new client per second. Known
224 * clients aren't subject to this restriction.
226 if (now == client->last_new_client) goto unknown;
229 client->last_new_client = now;
231 request = request_alloc();
232 if (!request) goto unknown;
234 request->listener = listener;
235 request->client = client;
236 request->packet = rad_recv(listener->fd, 0x02); /* MSG_PEEK */
237 if (!request->packet) { /* badly formed, etc */
238 request_free(&request);
241 request->reply = rad_alloc_reply(request->packet);
242 if (!request->reply) {
243 request_free(&request);
246 request->packet->timestamp = request->timestamp;
248 request->priority = listener->type;
249 request->server = client->client_server;
250 request->root = &mainconfig;
253 * Run a fake request through the given virtual server.
254 * Look for FreeRADIUS-Client-IP-Address
255 * FreeRADIUS-Client-Secret
258 * and create the RADCLIENT structure from that.
260 DEBUG("server %s {", request->server);
262 rcode = module_authorize(0, request);
264 DEBUG("} # server %s", request->server);
266 if (rcode != RLM_MODULE_OK) {
267 request_free(&request);
272 * If the client was updated by rlm_dynamic_clients,
273 * don't create the client from attribute-value pairs.
275 if (request->client == client) {
276 created = client_create(clients, request);
278 created = request->client;
281 * This frees the client if it isn't valid.
283 if (!client_validate(clients, client, created)) goto unknown;
285 request_free(&request);
287 if (!created) goto unknown;
293 static int listen_bind(rad_listen_t *this);
297 * Process and reply to a server-status request.
298 * Like rad_authenticate and rad_accounting this should
299 * live in it's own file but it's so small we don't bother.
301 static int rad_status_server(REQUEST *request)
303 int rcode = RLM_MODULE_OK;
306 switch (request->listener->type) {
308 case RAD_LISTEN_NONE:
310 case RAD_LISTEN_AUTH:
311 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
313 rcode = module_authorize(dval->value, request);
315 rcode = RLM_MODULE_OK;
320 case RLM_MODULE_UPDATED:
321 request->reply->code = PW_AUTHENTICATION_ACK;
324 case RLM_MODULE_FAIL:
325 case RLM_MODULE_HANDLED:
326 request->reply->code = 0; /* don't reply */
330 case RLM_MODULE_REJECT:
331 request->reply->code = PW_AUTHENTICATION_REJECT;
336 #ifdef WITH_ACCOUNTING
337 case RAD_LISTEN_ACCT:
338 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
340 rcode = module_accounting(dval->value, request);
342 rcode = RLM_MODULE_OK;
347 case RLM_MODULE_UPDATED:
348 request->reply->code = PW_ACCOUNTING_RESPONSE;
352 request->reply->code = 0; /* don't reply */
360 * This is a vendor extension. Suggested by Glen
361 * Zorn in IETF 72, and rejected by the rest of
362 * the WG. We like it, so it goes in here.
365 dval = dict_valbyname(PW_RECV_COA_TYPE, "Status-Server");
367 rcode = module_recv_coa(dval->value, request);
369 rcode = RLM_MODULE_OK;
374 case RLM_MODULE_UPDATED:
375 request->reply->code = PW_COA_ACK;
379 request->reply->code = 0; /* don't reply */
391 * Full statistics are available only on a statistics
394 if (request->listener->type == RAD_LISTEN_NONE) {
395 request_stats_reply(request);
403 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
406 listen_socket_t *sock = this->data;
409 switch (this->type) {
411 case RAD_LISTEN_NONE: /* what a hack... */
416 case RAD_LISTEN_AUTH:
417 name = "authentication";
420 #ifdef WITH_ACCOUNTING
421 case RAD_LISTEN_ACCT:
427 case RAD_LISTEN_PROXY:
439 case RAD_LISTEN_DHCP:
455 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
456 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
460 #ifdef SO_BINDTODEVICE
461 if (sock->interface) {
462 ADDSTRING(" interface ");
463 ADDSTRING(sock->interface);
467 ADDSTRING(" address ");
469 if ((sock->ipaddr.af == AF_INET) &&
470 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
471 strlcpy(buffer, "*", bufsize);
473 ip_ntoh(&sock->ipaddr, buffer, bufsize);
478 snprintf(buffer, bufsize, "%d", sock->port);
482 ADDSTRING(" as server ");
483 ADDSTRING(this->server);
494 * Parse an authentication or accounting socket.
496 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
501 listen_socket_t *sock = this->data;
502 char *section_name = NULL;
503 CONF_SECTION *client_cs, *parentcs;
508 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
509 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
510 &ipaddr.ipaddr.ip4addr, NULL);
511 if (rcode < 0) return -1;
513 if (rcode == 0) { /* successfully parsed IPv4 */
516 } else { /* maybe IPv6? */
517 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
518 &ipaddr.ipaddr.ip6addr, NULL);
519 if (rcode < 0) return -1;
522 cf_log_err(cf_sectiontoitem(cs),
523 "No address specified in listen section");
526 ipaddr.af = AF_INET6;
529 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
531 if (rcode < 0) return -1;
533 if ((listen_port < 0) || (listen_port > 65535)) {
534 cf_log_err(cf_sectiontoitem(cs),
535 "Invalid value for \"port\"");
539 sock->ipaddr = ipaddr;
540 sock->port = listen_port;
543 * If we can bind to interfaces, do so,
546 if (cf_pair_find(cs, "interface")) {
547 #ifndef SO_BINDTODEVICE
548 cf_log_err(cf_sectiontoitem(cs),
549 "System does not support binding to interfaces. Delete this line from the configuration file.");
553 CONF_PAIR *cp = cf_pair_find(cs, "interface");
555 rad_assert(cp != NULL);
556 value = cf_pair_value(cp);
558 cf_log_err(cf_sectiontoitem(cs),
559 "No interface name given");
562 sock->interface = value;
567 * And bind it to the port.
569 if (listen_bind(this) < 0) {
571 cf_log_err(cf_sectiontoitem(cs),
572 "Error binding to port for %s port %d",
573 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
580 * Proxy sockets don't have clients.
582 if (this->type == RAD_LISTEN_PROXY) return 0;
586 * The more specific configurations are preferred to more
590 parentcs = cf_top_section(cs);
591 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
592 §ion_name, NULL);
593 if (rcode < 0) return -1; /* bad string */
596 * Explicit list given: use it.
598 client_cs = cf_section_sub_find_name2(parentcs,
602 client_cs = cf_section_find(section_name);
605 cf_log_err(cf_sectiontoitem(cs),
606 "Failed to find clients %s {...}",
612 } /* else there was no "clients = " entry. */
615 CONF_SECTION *server_cs;
617 server_cs = cf_section_sub_find_name2(parentcs,
621 * Found a "server foo" section. If there are clients
625 (cf_section_sub_find(server_cs, "client") != NULL)) {
626 client_cs = server_cs;
631 * Still nothing. Look for global clients.
633 if (!client_cs) client_cs = parentcs;
635 sock->clients = clients_parse_section(client_cs);
636 if (!sock->clients) {
637 cf_log_err(cf_sectiontoitem(cs),
638 "Failed to load clients for this listen section");
646 * Send an authentication response packet
648 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
650 rad_assert(request->listener == listener);
651 rad_assert(listener->send == auth_socket_send);
653 return rad_send(request->reply, request->packet,
654 request->client->secret);
658 #ifdef WITH_ACCOUNTING
660 * Send an accounting response packet (or not)
662 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
664 rad_assert(request->listener == listener);
665 rad_assert(listener->send == acct_socket_send);
668 * Accounting reject's are silently dropped.
670 * We do it here to avoid polluting the rest of the
671 * code with this knowledge
673 if (request->reply->code == 0) return 0;
675 return rad_send(request->reply, request->packet,
676 request->client->secret);
682 * Send a packet to a home server.
684 * FIXME: have different code for proxy auth & acct!
686 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
688 listen_socket_t *sock = listener->data;
690 rad_assert(request->proxy_listener == listener);
691 rad_assert(listener->send == proxy_socket_send);
693 request->proxy->src_ipaddr = sock->ipaddr;
694 request->proxy->src_port = sock->port;
696 return rad_send(request->proxy, request->packet,
697 request->home_server->secret);
703 * Check if an incoming request is "ok"
705 * It takes packets, not requests. It sees if the packet looks
706 * OK. If so, it does a number of sanity checks on it.
708 static int stats_socket_recv(rad_listen_t *listener,
709 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
713 RADIUS_PACKET *packet;
715 fr_ipaddr_t src_ipaddr;
717 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
718 if (rcode < 0) return 0;
720 RAD_STATS_TYPE_INC(listener, total_requests);
722 if (rcode < 20) { /* AUTH_HDR_LEN */
723 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
727 if ((client = client_listener_find(listener,
728 &src_ipaddr, src_port)) == NULL) {
729 rad_recv_discard(listener->fd);
730 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
735 * We only understand Status-Server on this socket.
737 if (code != PW_STATUS_SERVER) {
738 DEBUG("Ignoring packet code %d sent to Status-Server port",
740 rad_recv_discard(listener->fd);
741 RAD_STATS_TYPE_INC(listener, total_unknown_types);
742 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
747 * Now that we've sanity checked everything, receive the
750 packet = rad_recv(listener->fd, 1); /* require message authenticator */
752 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
753 DEBUG("%s", fr_strerror());
757 if (!received_request(listener, packet, prequest, client)) {
758 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
759 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
764 *pfun = rad_status_server;
771 * Check if an incoming request is "ok"
773 * It takes packets, not requests. It sees if the packet looks
774 * OK. If so, it does a number of sanity checks on it.
776 static int auth_socket_recv(rad_listen_t *listener,
777 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
781 RADIUS_PACKET *packet;
782 RAD_REQUEST_FUNP fun = NULL;
784 fr_ipaddr_t src_ipaddr;
786 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
787 if (rcode < 0) return 0;
789 RAD_STATS_TYPE_INC(listener, total_requests);
791 if (rcode < 20) { /* AUTH_HDR_LEN */
792 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
796 if ((client = client_listener_find(listener,
797 &src_ipaddr, src_port)) == NULL) {
798 rad_recv_discard(listener->fd);
799 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
804 * Some sanity checks, based on the packet code.
807 case PW_AUTHENTICATION_REQUEST:
808 RAD_STATS_CLIENT_INC(listener, client, total_requests);
809 fun = rad_authenticate;
812 case PW_STATUS_SERVER:
813 if (!mainconfig.status_server) {
814 rad_recv_discard(listener->fd);
815 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
816 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
817 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
820 fun = rad_status_server;
824 rad_recv_discard(listener->fd);
825 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
826 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
828 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
829 code, client->shortname, src_port);
832 } /* switch over packet types */
835 * Now that we've sanity checked everything, receive the
838 packet = rad_recv(listener->fd, client->message_authenticator);
840 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
841 DEBUG("%s", fr_strerror());
845 if (!received_request(listener, packet, prequest, client)) {
846 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
847 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
857 #ifdef WITH_ACCOUNTING
859 * Receive packets from an accounting socket
861 static int acct_socket_recv(rad_listen_t *listener,
862 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
866 RADIUS_PACKET *packet;
867 RAD_REQUEST_FUNP fun = NULL;
869 fr_ipaddr_t src_ipaddr;
871 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
872 if (rcode < 0) return 0;
874 RAD_STATS_TYPE_INC(listener, total_requests);
876 if (rcode < 20) { /* AUTH_HDR_LEN */
877 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
881 if ((client = client_listener_find(listener,
882 &src_ipaddr, src_port)) == NULL) {
883 rad_recv_discard(listener->fd);
884 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
889 * Some sanity checks, based on the packet code.
892 case PW_ACCOUNTING_REQUEST:
893 RAD_STATS_CLIENT_INC(listener, client, total_requests);
894 fun = rad_accounting;
897 case PW_STATUS_SERVER:
898 if (!mainconfig.status_server) {
899 rad_recv_discard(listener->fd);
900 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
901 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
903 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
906 fun = rad_status_server;
910 rad_recv_discard(listener->fd);
911 RAD_STATS_TYPE_INC(listener, total_unknown_types);
912 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
914 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
915 code, client->shortname, src_port);
917 } /* switch over packet types */
920 * Now that we've sanity checked everything, receive the
923 packet = rad_recv(listener->fd, 0);
925 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
926 radlog(L_ERR, "%s", fr_strerror());
931 * There can be no duplicate accounting packets.
933 if (!received_request(listener, packet, prequest, client)) {
934 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
935 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
948 * For now, all CoA requests are *only* originated, and not
949 * proxied. So all of the necessary work is done in the
950 * post-proxy section, which is automatically handled by event.c.
951 * As a result, we don't have to do anything here.
953 static int rad_coa_reply(REQUEST *request)
958 * Inform the user about RFC requirements.
960 s1 = pairfind(request->proxy->vps, PW_STATE);
962 s2 = pairfind(request->proxy_reply->vps, PW_STATE);
965 DEBUG("WARNING: Client was sent State in CoA, and did not respond with State.");
967 } else if ((s1->length != s2->length) ||
968 (memcmp(s1->vp_octets, s2->vp_octets,
970 DEBUG("WARNING: Client was sent State in CoA, and did not respond with the same State.");
974 return RLM_MODULE_OK;
978 * Receive a CoA packet.
980 static int rad_coa_recv(REQUEST *request)
982 int rcode = RLM_MODULE_OK;
987 * Get the correct response
989 switch (request->packet->code) {
995 case PW_DISCONNECT_REQUEST:
996 ack = PW_DISCONNECT_ACK;
997 nak = PW_DISCONNECT_NAK;
1000 default: /* shouldn't happen */
1001 return RLM_MODULE_FAIL;
1005 #define WAS_PROXIED (request->proxy)
1007 #define WAS_PROXIED (0)
1012 * RFC 5176 Section 3.3. If we have a CoA-Request
1013 * with Service-Type = Authorize-Only, it MUST
1014 * have a State attribute in it.
1016 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE);
1017 if (request->packet->code == PW_COA_REQUEST) {
1018 if (vp && (vp->vp_integer == 17)) {
1019 vp = pairfind(request->packet->vps, PW_STATE);
1020 if (!vp || (vp->length == 0)) {
1021 RDEBUG("ERROR: CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1022 request->reply->code = PW_COA_NAK;
1023 return RLM_MODULE_FAIL;
1028 * RFC 5176, Section 3.2.
1030 RDEBUG("ERROR: Disconnect-Request MUST NOT contain a Service-Type attribute");
1031 request->reply->code = PW_DISCONNECT_NAK;
1032 return RLM_MODULE_FAIL;
1035 rcode = module_recv_coa(0, request);
1037 case RLM_MODULE_FAIL:
1038 case RLM_MODULE_INVALID:
1039 case RLM_MODULE_REJECT:
1040 case RLM_MODULE_USERLOCK:
1042 request->reply->code = nak;
1045 case RLM_MODULE_HANDLED:
1048 case RLM_MODULE_NOOP:
1049 case RLM_MODULE_NOTFOUND:
1051 case RLM_MODULE_UPDATED:
1052 request->reply->code = ack;
1057 * Start the reply code with the proxy reply
1060 request->reply->code = request->proxy_reply->code;
1064 * Copy State from the request to the reply.
1065 * See RFC 5176 Section 3.3.
1067 vp = paircopy2(request->packet->vps, PW_STATE);
1068 if (vp) pairadd(&request->reply->vps, vp);
1071 * We may want to over-ride the reply.
1073 rcode = module_send_coa(0, request);
1076 * We need to send CoA-NAK back if Service-Type
1077 * is Authorize-Only. Rely on the user's policy
1078 * to do that. We're not a real NAS, so this
1079 * restriction doesn't (ahem) apply to us.
1081 case RLM_MODULE_FAIL:
1082 case RLM_MODULE_INVALID:
1083 case RLM_MODULE_REJECT:
1084 case RLM_MODULE_USERLOCK:
1087 * Over-ride an ACK with a NAK
1089 request->reply->code = nak;
1092 case RLM_MODULE_HANDLED:
1095 case RLM_MODULE_NOOP:
1096 case RLM_MODULE_NOTFOUND:
1098 case RLM_MODULE_UPDATED:
1100 * Do NOT over-ride a previously set value.
1101 * Otherwise an "ok" here will re-write a
1104 if (request->reply->code == 0) {
1105 request->reply->code = ack;
1111 return RLM_MODULE_OK;
1116 * Check if an incoming request is "ok"
1118 * It takes packets, not requests. It sees if the packet looks
1119 * OK. If so, it does a number of sanity checks on it.
1121 static int coa_socket_recv(rad_listen_t *listener,
1122 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1126 RADIUS_PACKET *packet;
1127 RAD_REQUEST_FUNP fun = NULL;
1130 fr_ipaddr_t src_ipaddr;
1132 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1133 if (rcode < 0) return 0;
1135 RAD_STATS_TYPE_INC(listener, total_requests);
1137 if (rcode < 20) { /* AUTH_HDR_LEN */
1138 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1142 if ((client = client_listener_find(listener,
1143 &src_ipaddr, src_port)) == NULL) {
1144 rad_recv_discard(listener->fd);
1145 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1147 if (debug_flag > 0) {
1150 listener->print(listener, name, sizeof(name));
1153 * This is debugging rather than logging, so that
1154 * DoS attacks don't affect us.
1156 DEBUG("Ignoring request to %s from unknown client %s port %d",
1158 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
1159 buffer, sizeof(buffer)), src_port);
1166 * Some sanity checks, based on the packet code.
1169 case PW_COA_REQUEST:
1170 case PW_DISCONNECT_REQUEST:
1175 rad_recv_discard(listener->fd);
1176 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1177 code, client->shortname, src_port);
1180 } /* switch over packet types */
1183 * Now that we've sanity checked everything, receive the
1186 packet = rad_recv(listener->fd, client->message_authenticator);
1188 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1189 DEBUG("%s", fr_strerror());
1193 if (!received_request(listener, packet, prequest, client)) {
1205 * Recieve packets from a proxy socket.
1207 static int proxy_socket_recv(rad_listen_t *listener,
1208 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1211 RADIUS_PACKET *packet;
1212 RAD_REQUEST_FUNP fun = NULL;
1215 packet = rad_recv(listener->fd, 0);
1217 radlog(L_ERR, "%s", fr_strerror());
1222 * FIXME: Client MIB updates?
1224 switch(packet->code) {
1225 case PW_AUTHENTICATION_ACK:
1226 case PW_ACCESS_CHALLENGE:
1227 case PW_AUTHENTICATION_REJECT:
1228 fun = rad_authenticate;
1231 #ifdef WITH_ACCOUNTING
1232 case PW_ACCOUNTING_RESPONSE:
1233 fun = rad_accounting;
1238 case PW_DISCONNECT_ACK:
1239 case PW_DISCONNECT_NAK:
1242 fun = rad_coa_reply;
1248 * FIXME: Update MIB for packet types?
1250 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1251 "from home server %s port %d - ID %d : IGNORED",
1253 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1254 packet->src_port, packet->id);
1259 request = received_proxy_response(packet);
1267 * Distinguish proxied CoA requests from ones we
1270 if ((fun == rad_coa_reply) &&
1271 (request->packet->code == request->proxy->code)) {
1276 rad_assert(fun != NULL);
1278 *prequest = request;
1285 * Recieve packets from a proxy socket.
1287 static int proxy_socket_tcp_recv(rad_listen_t *listener,
1288 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1291 RADIUS_PACKET *packet;
1292 RAD_REQUEST_FUNP fun = NULL;
1295 packet = fr_tcp_recv(listener->fd, 0);
1297 proxy_close_tcp_listener(listener);
1302 * FIXME: Client MIB updates?
1304 switch(packet->code) {
1305 case PW_AUTHENTICATION_ACK:
1306 case PW_ACCESS_CHALLENGE:
1307 case PW_AUTHENTICATION_REJECT:
1308 fun = rad_authenticate;
1311 #ifdef WITH_ACCOUNTING
1312 case PW_ACCOUNTING_RESPONSE:
1313 fun = rad_accounting;
1319 * FIXME: Update MIB for packet types?
1321 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1322 "from home server %s port %d - ID %d : IGNORED",
1324 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1325 packet->src_port, packet->id);
1332 * Commented out until the rest of the code is added.
1334 request = received_proxy_tcp_response(packet,
1335 fr_listen2tcp(listener));
1341 rad_assert(fun != NULL);
1343 *prequest = request;
1351 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1353 if (!request->reply->code) return 0;
1355 rad_encode(request->reply, request->packet,
1356 request->client->secret);
1357 rad_sign(request->reply, request->packet,
1358 request->client->secret);
1364 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1366 if (rad_verify(request->packet, NULL,
1367 request->client->secret) < 0) {
1371 return rad_decode(request->packet, NULL,
1372 request->client->secret);
1376 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1378 rad_encode(request->proxy, NULL, request->home_server->secret);
1379 rad_sign(request->proxy, NULL, request->home_server->secret);
1385 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1388 * rad_verify is run in event.c, received_proxy_response()
1391 return rad_decode(request->proxy_reply, request->proxy,
1392 request->home_server->secret);
1398 #include "command.c"
1400 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1402 { common_socket_parse, NULL,
1403 stats_socket_recv, auth_socket_send,
1404 socket_print, client_socket_encode, client_socket_decode },
1407 * This always gets defined.
1409 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1414 { common_socket_parse, NULL,
1415 proxy_socket_recv, proxy_socket_send,
1416 socket_print, proxy_socket_encode, proxy_socket_decode },
1419 /* authentication */
1420 { common_socket_parse, NULL,
1421 auth_socket_recv, auth_socket_send,
1422 socket_print, client_socket_encode, client_socket_decode },
1424 #ifdef WITH_ACCOUNTING
1426 { common_socket_parse, NULL,
1427 acct_socket_recv, acct_socket_send,
1428 socket_print, client_socket_encode, client_socket_decode},
1433 { detail_parse, detail_free,
1434 detail_recv, detail_send,
1435 detail_print, detail_encode, detail_decode },
1439 /* vlan query protocol */
1440 { common_socket_parse, NULL,
1441 vqp_socket_recv, vqp_socket_send,
1442 socket_print, vqp_socket_encode, vqp_socket_decode },
1446 /* dhcp query protocol */
1447 { dhcp_socket_parse, NULL,
1448 dhcp_socket_recv, dhcp_socket_send,
1449 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1452 #ifdef WITH_COMMAND_SOCKET
1453 /* TCP command socket */
1454 { command_socket_parse, NULL,
1455 command_domain_accept, command_domain_send,
1456 command_socket_print, command_socket_encode, command_socket_decode },
1460 /* Change of Authorization */
1461 { common_socket_parse, NULL,
1462 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
1463 socket_print, client_socket_encode, client_socket_decode },
1471 * Binds a listener to a socket.
1473 static int listen_bind(rad_listen_t *this)
1476 struct sockaddr_storage salocal;
1478 listen_socket_t *sock = this->data;
1481 * If the port is zero, then it means the appropriate
1482 * thing from /etc/services.
1484 if (sock->port == 0) {
1485 struct servent *svp;
1487 switch (this->type) {
1488 case RAD_LISTEN_AUTH:
1489 svp = getservbyname ("radius", "udp");
1491 sock->port = ntohs(svp->s_port);
1493 sock->port = PW_AUTH_UDP_PORT;
1497 #ifdef WITH_ACCOUNTING
1498 case RAD_LISTEN_ACCT:
1499 svp = getservbyname ("radacct", "udp");
1501 sock->port = ntohs(svp->s_port);
1503 sock->port = PW_ACCT_UDP_PORT;
1509 case RAD_LISTEN_PROXY:
1515 case RAD_LISTEN_VQP:
1521 case RAD_LISTEN_COA:
1522 sock->port = PW_COA_UDP_PORT;
1527 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1533 * Copy fr_socket() here, as we may need to bind to a device.
1535 this->fd = socket(sock->ipaddr.af, SOCK_DGRAM, 0);
1537 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
1541 #ifdef SO_BINDTODEVICE
1543 * Bind to a device BEFORE touching IP addresses.
1545 if (sock->interface) {
1547 strcpy(ifreq.ifr_name, sock->interface);
1550 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1551 (char *)&ifreq, sizeof(ifreq));
1555 radlog(L_ERR, "Failed binding to interface %s: %s",
1556 sock->interface, strerror(errno));
1558 } /* else it worked. */
1562 #ifdef WITH_UDPFROMTO
1564 * Initialize udpfromto for all sockets.
1566 if (udpfromto_init(this->fd) != 0) {
1573 * Set up sockaddr stuff.
1575 if (!fr_ipaddr2sockaddr(&sock->ipaddr, sock->port, &salocal, &salen)) {
1580 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1581 if (sock->ipaddr.af == AF_INET6) {
1583 * Listening on '::' does NOT get you IPv4 to
1584 * IPv6 mapping. You've got to listen on an IPv4
1585 * address, too. This makes the rest of the server
1586 * design a little simpler.
1590 if (IN6_IS_ADDR_UNSPECIFIED(&sock->ipaddr.ipaddr.ip6addr)) {
1593 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
1594 (char *)&on, sizeof(on));
1596 #endif /* IPV6_V6ONLY */
1598 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
1601 if (sock->ipaddr.af == AF_INET) {
1604 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
1606 * Disable PMTU discovery. On Linux, this
1607 * also makes sure that the "don't fragment"
1610 flag = IP_PMTUDISC_DONT;
1611 setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
1612 &flag, sizeof(flag));
1615 #if defined(IP_DONTFRAG)
1617 * Ensure that the "don't fragment" flag is zero.
1620 setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
1621 &flag, sizeof(flag));
1626 * May be binding to priviledged ports.
1629 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
1635 this->print(this, buffer, sizeof(buffer));
1636 radlog(L_ERR, "Failed binding to %s: %s\n",
1637 buffer, strerror(errno));
1642 * FreeBSD jail issues. We bind to 0.0.0.0, but the
1643 * kernel instead binds us to a 1.2.3.4. If this
1644 * happens, notice, and remember our real IP.
1647 struct sockaddr_storage src;
1648 socklen_t sizeof_src = sizeof(src);
1650 memset(&src, 0, sizeof_src);
1651 if (getsockname(this->fd, (struct sockaddr *) &src,
1653 radlog(L_ERR, "Failed getting socket name: %s",
1658 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
1659 &sock->ipaddr, &sock->port)) {
1660 radlog(L_ERR, "Socket has unsupported address family");
1669 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
1670 radlog(L_ERR, "Failure getting socket flags: %s)\n",
1675 flags |= O_NONBLOCK;
1676 if( fcntl(this->fd, F_SETFL, flags) < 0) {
1677 radlog(L_ERR, "Failure setting socket flags: %s)\n",
1689 * Allocate & initialize a new listener.
1691 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
1695 this = rad_malloc(sizeof(*this));
1696 memset(this, 0, sizeof(*this));
1699 this->recv = master_listen[this->type].recv;
1700 this->send = master_listen[this->type].send;
1701 this->print = master_listen[this->type].print;
1702 this->encode = master_listen[this->type].encode;
1703 this->decode = master_listen[this->type].decode;
1707 case RAD_LISTEN_NONE:
1709 case RAD_LISTEN_AUTH:
1710 #ifdef WITH_ACCOUNTING
1711 case RAD_LISTEN_ACCT:
1714 case RAD_LISTEN_PROXY:
1717 case RAD_LISTEN_VQP:
1720 case RAD_LISTEN_COA:
1722 this->data = rad_malloc(sizeof(listen_socket_t));
1723 memset(this->data, 0, sizeof(listen_socket_t));
1727 case RAD_LISTEN_DHCP:
1728 this->data = rad_malloc(sizeof(dhcp_socket_t));
1729 memset(this->data, 0, sizeof(dhcp_socket_t));
1734 case RAD_LISTEN_DETAIL:
1739 #ifdef WITH_COMMAND_SOCKET
1740 case RAD_LISTEN_COMMAND:
1741 this->data = rad_malloc(sizeof(fr_command_socket_t));
1742 memset(this->data, 0, sizeof(fr_command_socket_t));
1747 rad_assert("Unsupported option!" == NULL);
1757 * Externally visible function for creating a new proxy LISTENER.
1759 * Not thread-safe, but all calls to it are protected by the
1760 * proxy mutex in event.c
1762 rad_listen_t *proxy_new_listener(fr_ipaddr_t *ipaddr, int exists)
1764 int last_proxy_port, port;
1765 rad_listen_t *this, *tmp, **last;
1766 listen_socket_t *sock, *old;
1769 * Find an existing proxy socket to copy.
1771 last_proxy_port = 0;
1773 last = &mainconfig.listen;
1774 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1776 * Not proxy, ignore it.
1778 if (tmp->type != RAD_LISTEN_PROXY) continue;
1783 * If we were asked to copy a specific one, do
1784 * so. If we're just finding one that already
1785 * exists, return a pointer to it. Otherwise,
1786 * create ANOTHER one with the same IP address.
1788 if ((ipaddr->af != AF_UNSPEC) &&
1789 (fr_ipaddr_cmp(&sock->ipaddr, ipaddr) != 0)) {
1790 if (exists) return tmp;
1794 if (sock->port > last_proxy_port) {
1795 last_proxy_port = sock->port + 1;
1797 if (!old) old = sock;
1799 last = &(tmp->next);
1804 * The socket MUST already exist if we're binding
1805 * to an address while proxying.
1807 * If we're initializing the server, it's OK for the
1808 * socket to NOT exist.
1810 if (!exists) return NULL;
1812 this = listen_alloc(RAD_LISTEN_PROXY);
1815 sock->ipaddr = *ipaddr;
1818 this = listen_alloc(RAD_LISTEN_PROXY);
1821 sock->ipaddr = old->ipaddr;
1825 * Keep going until we find an unused port.
1827 for (port = last_proxy_port; port < 64000; port++) {
1832 rcode = listen_bind(this);
1839 * Add the new listener to the list of
1851 static const FR_NAME_NUMBER listen_compare[] = {
1853 { "status", RAD_LISTEN_NONE },
1855 { "auth", RAD_LISTEN_AUTH },
1856 #ifdef WITH_ACCOUNTING
1857 { "acct", RAD_LISTEN_ACCT },
1860 { "detail", RAD_LISTEN_DETAIL },
1863 { "proxy", RAD_LISTEN_PROXY },
1866 { "vmps", RAD_LISTEN_VQP },
1869 { "dhcp", RAD_LISTEN_DHCP },
1871 #ifdef WITH_COMMAND_SOCKET
1872 { "control", RAD_LISTEN_COMMAND },
1875 { "coa", RAD_LISTEN_COA },
1881 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1889 cf_log_info(cs, "listen {");
1891 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1893 if (rcode < 0) return NULL;
1896 cf_log_err(cf_sectiontoitem(cs),
1897 "No type specified in listen section");
1901 type = fr_str2int(listen_compare, listen_type, -1);
1903 cf_log_err(cf_sectiontoitem(cs),
1904 "Invalid type \"%s\" in listen section.",
1912 * Allow listen sections in the default config to
1913 * refer to a server.
1916 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1918 if (rcode == 1) { /* compatiblity with 2.0-pre */
1919 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1922 if (rcode < 0) return NULL;
1926 * Set up cross-type data.
1928 this = listen_alloc(type);
1929 this->server = server;
1933 * Call per-type parser.
1935 if (master_listen[type].parse(cs, this) < 0) {
1940 cf_log_info(cs, "}");
1946 * Generate a list of listeners. Takes an input list of
1947 * listeners, too, so we don't close sockets with waiting packets.
1949 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1951 int override = FALSE;
1953 CONF_SECTION *cs = NULL;
1954 rad_listen_t **last;
1956 fr_ipaddr_t server_ipaddr;
1959 int defined_proxy = 0;
1963 * We shouldn't be called with a pre-existing list.
1965 rad_assert(head && (*head == NULL));
1968 server_ipaddr.af = AF_UNSPEC;
1971 * If the port is specified on the command-line,
1972 * it over-rides the configuration file.
1974 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1976 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1979 * If the IP address was configured on the command-line,
1980 * use that as the "bind_address"
1982 if (mainconfig.myip.af != AF_UNSPEC) {
1983 memcpy(&server_ipaddr, &mainconfig.myip,
1984 sizeof(server_ipaddr));
1990 * Else look for bind_address and/or listen sections.
1992 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1993 rcode = cf_item_parse(config, "bind_address",
1995 &server_ipaddr.ipaddr.ip4addr, NULL);
1996 if (rcode < 0) return -1; /* error parsing it */
1998 if (rcode == 0) { /* successfully parsed IPv4 */
1999 listen_socket_t *sock;
2000 server_ipaddr.af = AF_INET;
2002 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'.");
2006 if (strcmp(progname, "vmpsd") == 0) {
2007 this = listen_alloc(RAD_LISTEN_VQP);
2008 if (!auth_port) auth_port = 1589;
2011 this = listen_alloc(RAD_LISTEN_AUTH);
2015 sock->ipaddr = server_ipaddr;
2016 sock->port = auth_port;
2018 sock->clients = clients_parse_section(config);
2019 if (!sock->clients) {
2020 cf_log_err(cf_sectiontoitem(config),
2021 "Failed to find any clients for this listen section");
2026 if (listen_bind(this) < 0) {
2028 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
2032 auth_port = sock->port; /* may have been updated in listen_bind */
2034 cs = cf_section_sub_find_name2(config, "server",
2036 if (cs) this->server = mainconfig.name;
2040 last = &(this->next);
2046 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
2049 #ifdef WITH_ACCOUNTING
2051 * Open Accounting Socket.
2053 * If we haven't already gotten acct_port from
2054 * /etc/services, then make it auth_port + 1.
2056 this = listen_alloc(RAD_LISTEN_ACCT);
2060 * Create the accounting socket.
2062 * The accounting port is always the
2063 * authentication port + 1
2065 sock->ipaddr = server_ipaddr;
2066 sock->port = auth_port + 1;
2068 sock->clients = clients_parse_section(config);
2069 if (!sock->clients) {
2070 cf_log_err(cf_sectiontoitem(config),
2071 "Failed to find any clients for this listen section");
2075 if (listen_bind(this) < 0) {
2078 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
2083 cs = cf_section_sub_find_name2(config, "server",
2085 if (cs) this->server = mainconfig.name;
2089 last = &(this->next);
2091 } else if (mainconfig.port > 0) { /* no bind address, but a port */
2092 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
2098 * They specified an IP on the command-line, ignore
2099 * all listen sections except the one in '-n'.
2101 if (mainconfig.myip.af != AF_UNSPEC) {
2102 CONF_SECTION *subcs;
2103 const char *name2 = cf_section_name2(cs);
2105 cs = cf_section_sub_find_name2(config, "server",
2107 if (!cs) goto do_proxy;
2110 * Should really abstract this code...
2112 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2114 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2115 this = listen_parse(subcs, name2);
2122 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
2126 last = &(this->next);
2127 } /* loop over "listen" directives in server <foo> */
2133 * Walk through the "listen" sections, if they exist.
2135 for (cs = cf_subsection_find_next(config, NULL, "listen");
2137 cs = cf_subsection_find_next(config, cs, "listen")) {
2138 this = listen_parse(cs, NULL);
2145 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
2149 last = &(this->next);
2153 * Check virtual servers for "listen" sections, too.
2155 * FIXME: Move to virtual server init?
2157 for (cs = cf_subsection_find_next(config, NULL, "server");
2159 cs = cf_subsection_find_next(config, cs, "server")) {
2160 CONF_SECTION *subcs;
2161 const char *name2 = cf_section_name2(cs);
2163 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2165 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2166 this = listen_parse(subcs, name2);
2173 if (this->type == RAD_LISTEN_PROXY) {
2174 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
2181 last = &(this->next);
2182 } /* loop over "listen" directives in virtual servers */
2183 } /* loop over virtual servers */
2186 * If we're proxying requests, open the proxy FD.
2187 * Otherwise, don't do anything.
2191 if (mainconfig.proxy_requests == TRUE) {
2193 listen_socket_t *sock = NULL;
2196 * No sockets to receive packets, therefore
2197 * proxying is pointless.
2199 if (!*head) return -1;
2201 if (defined_proxy) goto check_home_servers;
2204 * Find the first authentication port,
2207 for (this = *head; this != NULL; this = this->next) {
2208 if (this->type == RAD_LISTEN_AUTH) {
2210 if (server_ipaddr.af == AF_UNSPEC) {
2211 server_ipaddr = sock->ipaddr;
2213 port = sock->port + 2; /* skip acct port */
2217 if (this->type == RAD_LISTEN_VQP) {
2219 if (server_ipaddr.af == AF_UNSPEC) {
2220 server_ipaddr = sock->ipaddr;
2222 port = sock->port + 1;
2228 if (port < 0) port = 1024 + (fr_rand() & 0x1ff);
2231 * Address is still unspecified, use IPv4.
2233 if (server_ipaddr.af == AF_UNSPEC) {
2234 server_ipaddr.af = AF_INET;
2235 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
2238 this = listen_alloc(RAD_LISTEN_PROXY);
2242 * Create the first proxy socket.
2244 sock->ipaddr = server_ipaddr;
2247 * Try to find a proxy port (value doesn't matter)
2249 for (sock->port = port;
2252 if (listen_bind(this) == 0) {
2254 last = &(this->next); /* just in case */
2259 if (sock->port >= 64000) {
2262 radlog(L_ERR, "Failed to open socket for proxying");
2267 * Create *additional* proxy listeners, based
2268 * on their src_ipaddr.
2271 if (home_server_create_listeners(*head) != 0) return -1;
2279 * Free a linked list of listeners;
2281 void listen_free(rad_listen_t **head)
2285 if (!head || !*head) return;
2289 rad_listen_t *next = this->next;
2292 * Other code may have eaten the FD.
2294 if (this->fd >= 0) close(this->fd);
2296 if (master_listen[this->type].free) {
2297 master_listen[this->type].free(this);
2309 fr_tcp_radius_t *fr_listen2tcp(rad_listen_t *this)
2311 listen_socket_t *sock;
2313 if (!this || (this->type != RAD_LISTEN_PROXY) || !this->data) {
2321 rad_listen_t *proxy_new_tcp_listener(home_server *home)
2324 fr_tcp_radius_t *tcp;
2325 struct sockaddr_storage src;
2326 socklen_t sizeof_src = sizeof(src);
2328 listen_socket_t *sock;
2331 ((home->max_connections > 0) &&
2332 (home->num_connections >= home->max_connections))) {
2333 DEBUG("WARNING: Home server has too many open connections (%d)",
2334 home->max_connections);
2341 * FIXME: Move to RBTrees.
2343 for (i = 0; i < home->max_connections; i++) {
2344 if (home->listeners[i]) continue;
2346 this = home->listeners[i] = listen_alloc(RAD_LISTEN_PROXY);
2348 DEBUG("WARNING: Failed allocating memory");
2355 DEBUG("WARNING: Failed to find a free connection slot");
2360 tcp = sock->tcp = rad_malloc(sizeof(*tcp));
2361 memset(tcp, 0, sizeof(*tcp));
2366 * Open a new socket...
2370 tcp->dst_ipaddr = home->ipaddr;
2371 tcp->dst_port = home->port;
2372 tcp->lifetime = home->lifetime;
2373 tcp->opened = time(NULL);
2376 * FIXME: connect() is blocking!
2377 * We do this with the proxy mutex locked, which may
2378 * cause large delays!
2380 * http://www.developerweb.net/forum/showthread.php?p=13486
2382 this->fd = tcp->fd = fr_tcp_client_socket(&tcp->dst_ipaddr, tcp->dst_port);
2385 DEBUG("WARNING: Failed opening socket to home server");
2388 memset(&src, 0, sizeof_src);
2389 if (getsockname(tcp->fd, (struct sockaddr *) &src, &sizeof_src) < 0) {
2395 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2396 &tcp->src_ipaddr, &tcp->src_port)) {
2403 * Fill in socket information.
2405 sock->proto = IPPROTO_TCP;
2408 sock->ipaddr = tcp->src_ipaddr;
2409 sock->port = tcp->src_port;
2412 * Don't ask. Just don't ask.
2414 sock->src_ipaddr = tcp->dst_ipaddr;
2415 sock->src_port = tcp->dst_port;
2417 sock->home->num_connections++;
2419 this->recv = proxy_socket_tcp_recv;
2422 * Tell the event handler about the new socket.
2424 * It keeps track of "this", so we don't have to insert
2425 * it into the main list of listeners.
2432 void proxy_close_tcp_listener(rad_listen_t *listener)
2435 listen_socket_t *sock = listener->data;
2438 * This is the second time around for the socket. Free
2441 if (listener->status != RAD_LISTEN_STATUS_KNOWN) {
2442 listen_free(&listener);
2446 listener->status = RAD_LISTEN_STATUS_CLOSED;
2447 event_new_fd(listener);
2450 * Find the home server, and mark this listener as
2451 * no longer being active.
2453 for (i = 0; i < sock->home->max_connections; i++) {
2454 if (sock->home->listeners[i] == listener) {
2455 sock->home->listeners[i] = NULL;
2456 sock->home->num_connections--;
2462 * There are still one or more requests using this socket.
2463 * leave it marked as "closed", but don't free it. When the
2464 * last requeast using it is cleaned up, it will be deleted.
2466 if (sock->tcp->used > 0) return;
2468 listen_free(&listener);
2473 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
2478 for (this = mainconfig.listen; this != NULL; this = this->next) {
2479 listen_socket_t *sock;
2481 if ((this->type != RAD_LISTEN_AUTH) &&
2482 (this->type != RAD_LISTEN_ACCT)) continue;
2486 if ((sock->port == port) &&
2487 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {
2488 return sock->clients;
2496 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
2500 for (this = mainconfig.listen; this != NULL; this = this->next) {
2501 listen_socket_t *sock;
2504 * FIXME: For TCP, ignore the *secondary*
2505 * listeners associated with the main socket.
2507 if ((this->type != RAD_LISTEN_AUTH) &&
2508 (this->type != RAD_LISTEN_ACCT)) continue;
2512 if ((sock->port == port) &&
2513 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {
2517 if ((sock->port == port) &&
2518 ((sock->ipaddr.af == AF_INET) &&
2519 (sock->ipaddr.ipaddr.ip4addr.s_addr == INADDR_ANY))) {
2523 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2524 if ((sock->port == port) &&
2525 (sock->ipaddr.af == AF_INET6) &&
2526 (IN6_IS_ADDR_UNSPECIFIED(&sock->ipaddr.ipaddr.ip6addr))) {