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);
1330 request = received_proxy_tcp_response(packet,
1331 fr_listen2tcp(listener));
1336 rad_assert(fun != NULL);
1338 *prequest = request;
1346 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1348 if (!request->reply->code) return 0;
1350 rad_encode(request->reply, request->packet,
1351 request->client->secret);
1352 rad_sign(request->reply, request->packet,
1353 request->client->secret);
1359 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1361 if (rad_verify(request->packet, NULL,
1362 request->client->secret) < 0) {
1366 return rad_decode(request->packet, NULL,
1367 request->client->secret);
1371 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1373 rad_encode(request->proxy, NULL, request->home_server->secret);
1374 rad_sign(request->proxy, NULL, request->home_server->secret);
1380 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1383 * rad_verify is run in event.c, received_proxy_response()
1386 return rad_decode(request->proxy_reply, request->proxy,
1387 request->home_server->secret);
1393 #include "command.c"
1395 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1397 { common_socket_parse, NULL,
1398 stats_socket_recv, auth_socket_send,
1399 socket_print, client_socket_encode, client_socket_decode },
1402 * This always gets defined.
1404 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1409 { common_socket_parse, NULL,
1410 proxy_socket_recv, proxy_socket_send,
1411 socket_print, proxy_socket_encode, proxy_socket_decode },
1414 /* authentication */
1415 { common_socket_parse, NULL,
1416 auth_socket_recv, auth_socket_send,
1417 socket_print, client_socket_encode, client_socket_decode },
1419 #ifdef WITH_ACCOUNTING
1421 { common_socket_parse, NULL,
1422 acct_socket_recv, acct_socket_send,
1423 socket_print, client_socket_encode, client_socket_decode},
1428 { detail_parse, detail_free,
1429 detail_recv, detail_send,
1430 detail_print, detail_encode, detail_decode },
1434 /* vlan query protocol */
1435 { common_socket_parse, NULL,
1436 vqp_socket_recv, vqp_socket_send,
1437 socket_print, vqp_socket_encode, vqp_socket_decode },
1441 /* dhcp query protocol */
1442 { dhcp_socket_parse, NULL,
1443 dhcp_socket_recv, dhcp_socket_send,
1444 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1447 #ifdef WITH_COMMAND_SOCKET
1448 /* TCP command socket */
1449 { command_socket_parse, NULL,
1450 command_domain_accept, command_domain_send,
1451 command_socket_print, command_socket_encode, command_socket_decode },
1455 /* Change of Authorization */
1456 { common_socket_parse, NULL,
1457 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
1458 socket_print, client_socket_encode, client_socket_decode },
1466 * Binds a listener to a socket.
1468 static int listen_bind(rad_listen_t *this)
1471 struct sockaddr_storage salocal;
1473 listen_socket_t *sock = this->data;
1476 * If the port is zero, then it means the appropriate
1477 * thing from /etc/services.
1479 if (sock->port == 0) {
1480 struct servent *svp;
1482 switch (this->type) {
1483 case RAD_LISTEN_AUTH:
1484 svp = getservbyname ("radius", "udp");
1486 sock->port = ntohs(svp->s_port);
1488 sock->port = PW_AUTH_UDP_PORT;
1492 #ifdef WITH_ACCOUNTING
1493 case RAD_LISTEN_ACCT:
1494 svp = getservbyname ("radacct", "udp");
1496 sock->port = ntohs(svp->s_port);
1498 sock->port = PW_ACCT_UDP_PORT;
1504 case RAD_LISTEN_PROXY:
1510 case RAD_LISTEN_VQP:
1516 case RAD_LISTEN_COA:
1517 sock->port = PW_COA_UDP_PORT;
1522 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1528 * Copy fr_socket() here, as we may need to bind to a device.
1530 this->fd = socket(sock->ipaddr.af, SOCK_DGRAM, 0);
1532 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
1536 #ifdef SO_BINDTODEVICE
1538 * Bind to a device BEFORE touching IP addresses.
1540 if (sock->interface) {
1542 strcpy(ifreq.ifr_name, sock->interface);
1545 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1546 (char *)&ifreq, sizeof(ifreq));
1550 radlog(L_ERR, "Failed binding to interface %s: %s",
1551 sock->interface, strerror(errno));
1553 } /* else it worked. */
1557 #ifdef WITH_UDPFROMTO
1559 * Initialize udpfromto for all sockets.
1561 if (udpfromto_init(this->fd) != 0) {
1568 * Set up sockaddr stuff.
1570 if (!fr_ipaddr2sockaddr(&sock->ipaddr, sock->port, &salocal, &salen)) {
1575 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1576 if (sock->ipaddr.af == AF_INET6) {
1578 * Listening on '::' does NOT get you IPv4 to
1579 * IPv6 mapping. You've got to listen on an IPv4
1580 * address, too. This makes the rest of the server
1581 * design a little simpler.
1585 if (IN6_IS_ADDR_UNSPECIFIED(&sock->ipaddr.ipaddr.ip6addr)) {
1588 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
1589 (char *)&on, sizeof(on));
1591 #endif /* IPV6_V6ONLY */
1593 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
1596 if (sock->ipaddr.af == AF_INET) {
1599 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
1601 * Disable PMTU discovery. On Linux, this
1602 * also makes sure that the "don't fragment"
1605 flag = IP_PMTUDISC_DONT;
1606 setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
1607 &flag, sizeof(flag));
1610 #if defined(IP_DONTFRAG)
1612 * Ensure that the "don't fragment" flag is zero.
1615 setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
1616 &flag, sizeof(flag));
1621 * May be binding to priviledged ports.
1624 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
1630 this->print(this, buffer, sizeof(buffer));
1631 radlog(L_ERR, "Failed binding to %s: %s\n",
1632 buffer, strerror(errno));
1637 * FreeBSD jail issues. We bind to 0.0.0.0, but the
1638 * kernel instead binds us to a 1.2.3.4. If this
1639 * happens, notice, and remember our real IP.
1642 struct sockaddr_storage src;
1643 socklen_t sizeof_src = sizeof(src);
1645 memset(&src, 0, sizeof_src);
1646 if (getsockname(this->fd, (struct sockaddr *) &src,
1648 radlog(L_ERR, "Failed getting socket name: %s",
1653 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
1654 &sock->ipaddr, &sock->port)) {
1655 radlog(L_ERR, "Socket has unsupported address family");
1664 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
1665 radlog(L_ERR, "Failure getting socket flags: %s)\n",
1670 flags |= O_NONBLOCK;
1671 if( fcntl(this->fd, F_SETFL, flags) < 0) {
1672 radlog(L_ERR, "Failure setting socket flags: %s)\n",
1684 * Allocate & initialize a new listener.
1686 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
1690 this = rad_malloc(sizeof(*this));
1691 memset(this, 0, sizeof(*this));
1694 this->recv = master_listen[this->type].recv;
1695 this->send = master_listen[this->type].send;
1696 this->print = master_listen[this->type].print;
1697 this->encode = master_listen[this->type].encode;
1698 this->decode = master_listen[this->type].decode;
1702 case RAD_LISTEN_NONE:
1704 case RAD_LISTEN_AUTH:
1705 #ifdef WITH_ACCOUNTING
1706 case RAD_LISTEN_ACCT:
1709 case RAD_LISTEN_PROXY:
1712 case RAD_LISTEN_VQP:
1715 case RAD_LISTEN_COA:
1717 this->data = rad_malloc(sizeof(listen_socket_t));
1718 memset(this->data, 0, sizeof(listen_socket_t));
1722 case RAD_LISTEN_DHCP:
1723 this->data = rad_malloc(sizeof(dhcp_socket_t));
1724 memset(this->data, 0, sizeof(dhcp_socket_t));
1729 case RAD_LISTEN_DETAIL:
1734 #ifdef WITH_COMMAND_SOCKET
1735 case RAD_LISTEN_COMMAND:
1736 this->data = rad_malloc(sizeof(fr_command_socket_t));
1737 memset(this->data, 0, sizeof(fr_command_socket_t));
1742 rad_assert("Unsupported option!" == NULL);
1752 * Externally visible function for creating a new proxy LISTENER.
1754 * Not thread-safe, but all calls to it are protected by the
1755 * proxy mutex in event.c
1757 rad_listen_t *proxy_new_listener(fr_ipaddr_t *ipaddr, int exists)
1759 int last_proxy_port, port;
1760 rad_listen_t *this, *tmp, **last;
1761 listen_socket_t *sock, *old;
1764 * Find an existing proxy socket to copy.
1766 last_proxy_port = 0;
1768 last = &mainconfig.listen;
1769 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1771 * Not proxy, ignore it.
1773 if (tmp->type != RAD_LISTEN_PROXY) continue;
1778 * If we were asked to copy a specific one, do
1779 * so. If we're just finding one that already
1780 * exists, return a pointer to it. Otherwise,
1781 * create ANOTHER one with the same IP address.
1783 if ((ipaddr->af != AF_UNSPEC) &&
1784 (fr_ipaddr_cmp(&sock->ipaddr, ipaddr) != 0)) {
1785 if (exists) return tmp;
1789 if (sock->port > last_proxy_port) {
1790 last_proxy_port = sock->port + 1;
1792 if (!old) old = sock;
1794 last = &(tmp->next);
1799 * The socket MUST already exist if we're binding
1800 * to an address while proxying.
1802 * If we're initializing the server, it's OK for the
1803 * socket to NOT exist.
1805 if (!exists) return NULL;
1807 this = listen_alloc(RAD_LISTEN_PROXY);
1810 sock->ipaddr = *ipaddr;
1813 this = listen_alloc(RAD_LISTEN_PROXY);
1816 sock->ipaddr = old->ipaddr;
1820 * Keep going until we find an unused port.
1822 for (port = last_proxy_port; port < 64000; port++) {
1827 rcode = listen_bind(this);
1834 * Add the new listener to the list of
1846 static const FR_NAME_NUMBER listen_compare[] = {
1848 { "status", RAD_LISTEN_NONE },
1850 { "auth", RAD_LISTEN_AUTH },
1851 #ifdef WITH_ACCOUNTING
1852 { "acct", RAD_LISTEN_ACCT },
1855 { "detail", RAD_LISTEN_DETAIL },
1858 { "proxy", RAD_LISTEN_PROXY },
1861 { "vmps", RAD_LISTEN_VQP },
1864 { "dhcp", RAD_LISTEN_DHCP },
1866 #ifdef WITH_COMMAND_SOCKET
1867 { "control", RAD_LISTEN_COMMAND },
1870 { "coa", RAD_LISTEN_COA },
1876 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1884 cf_log_info(cs, "listen {");
1886 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1888 if (rcode < 0) return NULL;
1891 cf_log_err(cf_sectiontoitem(cs),
1892 "No type specified in listen section");
1896 type = fr_str2int(listen_compare, listen_type, -1);
1898 cf_log_err(cf_sectiontoitem(cs),
1899 "Invalid type \"%s\" in listen section.",
1907 * Allow listen sections in the default config to
1908 * refer to a server.
1911 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1913 if (rcode == 1) { /* compatiblity with 2.0-pre */
1914 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1917 if (rcode < 0) return NULL;
1921 * Set up cross-type data.
1923 this = listen_alloc(type);
1924 this->server = server;
1928 * Call per-type parser.
1930 if (master_listen[type].parse(cs, this) < 0) {
1935 cf_log_info(cs, "}");
1941 * Generate a list of listeners. Takes an input list of
1942 * listeners, too, so we don't close sockets with waiting packets.
1944 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1946 int override = FALSE;
1948 CONF_SECTION *cs = NULL;
1949 rad_listen_t **last;
1951 fr_ipaddr_t server_ipaddr;
1954 int defined_proxy = 0;
1958 * We shouldn't be called with a pre-existing list.
1960 rad_assert(head && (*head == NULL));
1963 server_ipaddr.af = AF_UNSPEC;
1966 * If the port is specified on the command-line,
1967 * it over-rides the configuration file.
1969 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1971 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1974 * If the IP address was configured on the command-line,
1975 * use that as the "bind_address"
1977 if (mainconfig.myip.af != AF_UNSPEC) {
1978 memcpy(&server_ipaddr, &mainconfig.myip,
1979 sizeof(server_ipaddr));
1985 * Else look for bind_address and/or listen sections.
1987 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1988 rcode = cf_item_parse(config, "bind_address",
1990 &server_ipaddr.ipaddr.ip4addr, NULL);
1991 if (rcode < 0) return -1; /* error parsing it */
1993 if (rcode == 0) { /* successfully parsed IPv4 */
1994 listen_socket_t *sock;
1995 server_ipaddr.af = AF_INET;
1997 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'.");
2001 if (strcmp(progname, "vmpsd") == 0) {
2002 this = listen_alloc(RAD_LISTEN_VQP);
2003 if (!auth_port) auth_port = 1589;
2006 this = listen_alloc(RAD_LISTEN_AUTH);
2010 sock->ipaddr = server_ipaddr;
2011 sock->port = auth_port;
2013 sock->clients = clients_parse_section(config);
2014 if (!sock->clients) {
2015 cf_log_err(cf_sectiontoitem(config),
2016 "Failed to find any clients for this listen section");
2021 if (listen_bind(this) < 0) {
2023 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
2027 auth_port = sock->port; /* may have been updated in listen_bind */
2029 cs = cf_section_sub_find_name2(config, "server",
2031 if (cs) this->server = mainconfig.name;
2035 last = &(this->next);
2041 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
2044 #ifdef WITH_ACCOUNTING
2046 * Open Accounting Socket.
2048 * If we haven't already gotten acct_port from
2049 * /etc/services, then make it auth_port + 1.
2051 this = listen_alloc(RAD_LISTEN_ACCT);
2055 * Create the accounting socket.
2057 * The accounting port is always the
2058 * authentication port + 1
2060 sock->ipaddr = server_ipaddr;
2061 sock->port = auth_port + 1;
2063 sock->clients = clients_parse_section(config);
2064 if (!sock->clients) {
2065 cf_log_err(cf_sectiontoitem(config),
2066 "Failed to find any clients for this listen section");
2070 if (listen_bind(this) < 0) {
2073 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
2078 cs = cf_section_sub_find_name2(config, "server",
2080 if (cs) this->server = mainconfig.name;
2084 last = &(this->next);
2086 } else if (mainconfig.port > 0) { /* no bind address, but a port */
2087 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
2093 * They specified an IP on the command-line, ignore
2094 * all listen sections except the one in '-n'.
2096 if (mainconfig.myip.af != AF_UNSPEC) {
2097 CONF_SECTION *subcs;
2098 const char *name2 = cf_section_name2(cs);
2100 cs = cf_section_sub_find_name2(config, "server",
2102 if (!cs) goto do_proxy;
2105 * Should really abstract this code...
2107 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2109 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2110 this = listen_parse(subcs, name2);
2117 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
2121 last = &(this->next);
2122 } /* loop over "listen" directives in server <foo> */
2128 * Walk through the "listen" sections, if they exist.
2130 for (cs = cf_subsection_find_next(config, NULL, "listen");
2132 cs = cf_subsection_find_next(config, cs, "listen")) {
2133 this = listen_parse(cs, NULL);
2140 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
2144 last = &(this->next);
2148 * Check virtual servers for "listen" sections, too.
2150 * FIXME: Move to virtual server init?
2152 for (cs = cf_subsection_find_next(config, NULL, "server");
2154 cs = cf_subsection_find_next(config, cs, "server")) {
2155 CONF_SECTION *subcs;
2156 const char *name2 = cf_section_name2(cs);
2158 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2160 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2161 this = listen_parse(subcs, name2);
2168 if (this->type == RAD_LISTEN_PROXY) {
2169 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
2176 last = &(this->next);
2177 } /* loop over "listen" directives in virtual servers */
2178 } /* loop over virtual servers */
2181 * If we're proxying requests, open the proxy FD.
2182 * Otherwise, don't do anything.
2186 if (mainconfig.proxy_requests == TRUE) {
2188 listen_socket_t *sock = NULL;
2191 * No sockets to receive packets, therefore
2192 * proxying is pointless.
2194 if (!*head) return -1;
2196 if (defined_proxy) goto check_home_servers;
2199 * Find the first authentication port,
2202 for (this = *head; this != NULL; this = this->next) {
2203 if (this->type == RAD_LISTEN_AUTH) {
2205 if (server_ipaddr.af == AF_UNSPEC) {
2206 server_ipaddr = sock->ipaddr;
2208 port = sock->port + 2; /* skip acct port */
2212 if (this->type == RAD_LISTEN_VQP) {
2214 if (server_ipaddr.af == AF_UNSPEC) {
2215 server_ipaddr = sock->ipaddr;
2217 port = sock->port + 1;
2223 if (port < 0) port = 1024 + (fr_rand() & 0x1ff);
2226 * Address is still unspecified, use IPv4.
2228 if (server_ipaddr.af == AF_UNSPEC) {
2229 server_ipaddr.af = AF_INET;
2230 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
2233 this = listen_alloc(RAD_LISTEN_PROXY);
2237 * Create the first proxy socket.
2239 sock->ipaddr = server_ipaddr;
2242 * Try to find a proxy port (value doesn't matter)
2244 for (sock->port = port;
2247 if (listen_bind(this) == 0) {
2249 last = &(this->next); /* just in case */
2254 if (sock->port >= 64000) {
2257 radlog(L_ERR, "Failed to open socket for proxying");
2262 * Create *additional* proxy listeners, based
2263 * on their src_ipaddr.
2266 if (home_server_create_listeners(*head) != 0) return -1;
2274 * Free a linked list of listeners;
2276 void listen_free(rad_listen_t **head)
2280 if (!head || !*head) return;
2284 rad_listen_t *next = this->next;
2287 * Other code may have eaten the FD.
2289 if (this->fd >= 0) close(this->fd);
2291 if (master_listen[this->type].free) {
2292 master_listen[this->type].free(this);
2304 fr_tcp_radius_t *fr_listen2tcp(rad_listen_t *this)
2306 listen_socket_t *sock;
2308 if (!this || (this->type != RAD_LISTEN_PROXY) || !this->data) {
2316 rad_listen_t *proxy_new_tcp_listener(home_server *home)
2319 fr_tcp_radius_t *tcp;
2320 struct sockaddr_storage src;
2321 socklen_t sizeof_src = sizeof(src);
2323 listen_socket_t *sock;
2326 ((home->max_connections > 0) &&
2327 (home->num_connections >= home->max_connections))) {
2328 DEBUG("WARNING: Home server has too many open connections (%d)",
2329 home->max_connections);
2336 * FIXME: Move to RBTrees.
2338 for (i = 0; i < home->max_connections; i++) {
2339 if (home->listeners[i]) continue;
2341 this = home->listeners[i] = listen_alloc(RAD_LISTEN_PROXY);
2343 DEBUG("WARNING: Failed allocating memory");
2350 DEBUG("WARNING: Failed to find a free connection slot");
2355 tcp = sock->tcp = rad_malloc(sizeof(*tcp));
2356 memset(tcp, 0, sizeof(*tcp));
2361 * Open a new socket...
2365 tcp->dst_ipaddr = home->ipaddr;
2366 tcp->dst_port = home->port;
2367 tcp->lifetime = home->lifetime;
2368 tcp->opened = time(NULL);
2371 * FIXME: connect() is blocking!
2372 * We do this with the proxy mutex locked, which may
2373 * cause large delays!
2375 * http://www.developerweb.net/forum/showthread.php?p=13486
2377 this->fd = tcp->fd = fr_tcp_client_socket(&tcp->dst_ipaddr, tcp->dst_port);
2380 DEBUG("WARNING: Failed opening socket to home server");
2383 memset(&src, 0, sizeof_src);
2384 if (getsockname(tcp->fd, (struct sockaddr *) &src, &sizeof_src) < 0) {
2390 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2391 &tcp->src_ipaddr, &tcp->src_port)) {
2398 * Fill in socket information.
2400 sock->proto = IPPROTO_TCP;
2403 sock->ipaddr = tcp->src_ipaddr;
2404 sock->port = tcp->src_port;
2407 * Don't ask. Just don't ask.
2409 sock->src_ipaddr = tcp->dst_ipaddr;
2410 sock->src_port = tcp->dst_port;
2412 sock->home->num_connections++;
2414 this->recv = proxy_socket_tcp_recv;
2417 * Tell the event handler about the new socket.
2419 * It keeps track of "this", so we don't have to insert
2420 * it into the main list of listeners.
2427 void proxy_close_tcp_listener(rad_listen_t *listener)
2430 listen_socket_t *sock = listener->data;
2433 * This is the second time around for the socket. Free
2436 if (listener->status != RAD_LISTEN_STATUS_KNOWN) {
2437 listen_free(&listener);
2441 listener->status = RAD_LISTEN_STATUS_CLOSED;
2442 event_new_fd(listener);
2445 * Find the home server, and mark this listener as
2446 * no longer being active.
2448 for (i = 0; i < sock->home->max_connections; i++) {
2449 if (sock->home->listeners[i] == listener) {
2450 sock->home->listeners[i] = NULL;
2451 sock->home->num_connections--;
2457 * There are still one or more requests using this socket.
2458 * leave it marked as "closed", but don't free it. When the
2459 * last requeast using it is cleaned up, it will be deleted.
2461 if (sock->tcp->used > 0) return;
2463 listen_free(&listener);
2468 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
2473 for (this = mainconfig.listen; this != NULL; this = this->next) {
2474 listen_socket_t *sock;
2476 if ((this->type != RAD_LISTEN_AUTH) &&
2477 (this->type != RAD_LISTEN_ACCT)) continue;
2481 if ((sock->port == port) &&
2482 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {
2483 return sock->clients;
2491 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
2495 for (this = mainconfig.listen; this != NULL; this = this->next) {
2496 listen_socket_t *sock;
2499 * FIXME: For TCP, ignore the *secondary*
2500 * listeners associated with the main socket.
2502 if ((this->type != RAD_LISTEN_AUTH) &&
2503 (this->type != RAD_LISTEN_ACCT)) continue;
2507 if ((sock->port == port) &&
2508 (fr_ipaddr_cmp(ipaddr, &sock->ipaddr) == 0)) {
2512 if ((sock->port == port) &&
2513 ((sock->ipaddr.af == AF_INET) &&
2514 (sock->ipaddr.ipaddr.ip4addr.s_addr == INADDR_ANY))) {
2518 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2519 if ((sock->port == port) &&
2520 (sock->ipaddr.af == AF_INET6) &&
2521 (IN6_IS_ADDR_UNSPECIFIED(&sock->ipaddr.ipaddr.ip6addr))) {