2 * listen.c Handle socket stuff
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2005,2006 The FreeRADIUS server project
21 * Copyright 2005 Alan DeKok <aland@ox.org>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29 #include <freeradius-devel/rad_assert.h>
30 #include <freeradius-devel/vqp.h>
31 #include <freeradius-devel/dhcp.h>
33 #include <freeradius-devel/vmps.h>
34 #include <freeradius-devel/detail.h>
37 #include <freeradius-devel/udpfromto.h>
40 #ifdef HAVE_SYS_RESOURCE_H
41 #include <sys/resource.h>
53 void print_packet(RADIUS_PACKET *packet)
55 char src[256], dst[256];
57 ip_ntoh(&packet->src_ipaddr, src, sizeof(src));
58 ip_ntoh(&packet->dst_ipaddr, dst, sizeof(dst));
60 fprintf(stderr, "ID %d: %s %d -> %s %d\n", packet->id,
61 src, packet->src_port, dst, packet->dst_port);
67 * We'll use this below.
69 typedef int (*rad_listen_parse_t)(CONF_SECTION *, rad_listen_t *);
70 typedef void (*rad_listen_free_t)(rad_listen_t *);
72 typedef struct rad_listen_master_t {
73 rad_listen_parse_t parse;
74 rad_listen_free_t free;
75 rad_listen_recv_t recv;
76 rad_listen_send_t send;
77 rad_listen_print_t print;
78 rad_listen_encode_t encode;
79 rad_listen_decode_t decode;
80 } rad_listen_master_t;
82 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type);
85 * Find a per-socket client.
87 RADCLIENT *client_listener_find(const rad_listen_t *listener,
88 const fr_ipaddr_t *ipaddr, int src_port)
90 #ifdef WITH_DYNAMIC_CLIENTS
97 RADCLIENT_LIST *clients;
98 listen_socket_t *sock;
100 rad_assert(listener != NULL);
101 rad_assert(ipaddr != NULL);
103 sock = listener->data;
104 clients = sock->clients;
107 * This HAS to have been initialized previously.
109 rad_assert(clients != NULL);
111 client = client_find(clients, ipaddr,sock->proto);
113 char name[256], buffer[128];
115 #ifdef WITH_DYNAMIC_CLIENTS
116 unknown: /* used only for dynamic clients */
120 * DoS attack quenching, but only in daemon mode.
121 * If they're running in debug mode, show them
124 if (debug_flag == 0) {
125 static time_t last_printed = 0;
128 if (last_printed == now) return NULL;
133 listener->print(listener, name, sizeof(name));
135 radlog(L_ERR, "Ignoring request to %s from unknown client %s port %d"
139 , name, inet_ntop(ipaddr->af, &ipaddr->ipaddr,
140 buffer, sizeof(buffer)), src_port
142 , (sock->proto == IPPROTO_UDP) ? "udp" : "tcp"
148 #ifndef WITH_DYNAMIC_CLIENTS
149 return client; /* return the found client. */
153 * No server defined, and it's not dynamic. Return it.
155 if (!client->client_server && !client->dynamic) return client;
160 * It's a dynamically generated client, check it.
162 if (client->dynamic && (src_port != 0)) {
164 * Lives forever. Return it.
166 if (client->lifetime == 0) return client;
169 * Rate-limit the deletion of known clients.
170 * This makes them last a little longer, but
171 * prevents the server from melting down if (say)
172 * 10k clients all expire at once.
174 if (now == client->last_new_client) return client;
177 * It's not dead yet. Return it.
179 if ((client->created + client->lifetime) > now) return client;
182 * This really puts them onto a queue for later
185 client_delete(clients, client);
188 * Go find the enclosing network again.
190 client = client_find(clients, ipaddr, sock->proto);
195 if (!client) goto unknown;
196 if (!client->client_server) goto unknown;
199 * At this point, 'client' is the enclosing
200 * network that configures where dynamic clients
203 rad_assert(client->dynamic == 0);
205 } else if (!client->dynamic && client->rate_limit) {
207 * The IP is unknown, so we've found an enclosing
208 * network. Enable DoS protection. We only
209 * allow one new client per second. Known
210 * clients aren't subject to this restriction.
212 if (now == client->last_new_client) goto unknown;
215 client->last_new_client = now;
217 request = request_alloc();
218 if (!request) goto unknown;
220 request->listener = listener;
221 request->client = client;
222 request->packet = rad_recv(listener->fd, 0x02); /* MSG_PEEK */
223 if (!request->packet) { /* badly formed, etc */
224 request_free(&request);
227 request->reply = rad_alloc_reply(request->packet);
228 if (!request->reply) {
229 request_free(&request);
232 request->packet->timestamp = request->timestamp;
234 request->priority = listener->type;
235 request->server = client->client_server;
236 request->root = &mainconfig;
239 * Run a fake request through the given virtual server.
240 * Look for FreeRADIUS-Client-IP-Address
241 * FreeRADIUS-Client-Secret
244 * and create the RADCLIENT structure from that.
246 DEBUG("server %s {", request->server);
248 rcode = module_authorize(0, request);
250 DEBUG("} # server %s", request->server);
252 if (rcode != RLM_MODULE_OK) {
253 request_free(&request);
258 * If the client was updated by rlm_dynamic_clients,
259 * don't create the client from attribute-value pairs.
261 if (request->client == client) {
262 created = client_create(clients, request);
264 created = request->client;
267 * This frees the client if it isn't valid.
269 if (!client_validate(clients, client, created)) goto unknown;
271 request_free(&request);
273 if (!created) goto unknown;
279 static int listen_bind(rad_listen_t *this);
283 * Process and reply to a server-status request.
284 * Like rad_authenticate and rad_accounting this should
285 * live in it's own file but it's so small we don't bother.
287 static int rad_status_server(REQUEST *request)
289 int rcode = RLM_MODULE_OK;
292 switch (request->listener->type) {
294 case RAD_LISTEN_NONE:
296 case RAD_LISTEN_AUTH:
297 dval = dict_valbyname(PW_AUTZ_TYPE, 0, "Status-Server");
299 rcode = module_authorize(dval->value, request);
301 rcode = RLM_MODULE_OK;
306 case RLM_MODULE_UPDATED:
307 request->reply->code = PW_AUTHENTICATION_ACK;
310 case RLM_MODULE_FAIL:
311 case RLM_MODULE_HANDLED:
312 request->reply->code = 0; /* don't reply */
316 case RLM_MODULE_REJECT:
317 request->reply->code = PW_AUTHENTICATION_REJECT;
322 #ifdef WITH_ACCOUNTING
323 case RAD_LISTEN_ACCT:
324 dval = dict_valbyname(PW_ACCT_TYPE, 0, "Status-Server");
326 rcode = module_accounting(dval->value, request);
328 rcode = RLM_MODULE_OK;
333 case RLM_MODULE_UPDATED:
334 request->reply->code = PW_ACCOUNTING_RESPONSE;
338 request->reply->code = 0; /* don't reply */
346 * This is a vendor extension. Suggested by Glen
347 * Zorn in IETF 72, and rejected by the rest of
348 * the WG. We like it, so it goes in here.
351 dval = dict_valbyname(PW_RECV_COA_TYPE, 0, "Status-Server");
353 rcode = module_recv_coa(dval->value, request);
355 rcode = RLM_MODULE_OK;
360 case RLM_MODULE_UPDATED:
361 request->reply->code = PW_COA_ACK;
365 request->reply->code = 0; /* don't reply */
377 * Full statistics are available only on a statistics
380 if (request->listener->type == RAD_LISTEN_NONE) {
381 request_stats_reply(request);
389 static int auth_tcp_recv(rad_listen_t *listener,
390 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
393 RADIUS_PACKET *packet;
394 RAD_REQUEST_FUNP fun = NULL;
395 listen_socket_t *sock = listener->data;
396 RADCLIENT *client = sock->client;
399 * Allocate a packet for partial reads.
402 sock->packet = rad_alloc(0);
403 if (!sock->packet) return 0;
405 sock->packet->sockfd = listener->fd;
406 sock->packet->src_ipaddr = sock->other_ipaddr;
407 sock->packet->src_port = sock->other_port;
408 sock->packet->dst_ipaddr = sock->my_ipaddr;
409 sock->packet->dst_port = sock->my_port;
413 * Grab the packet currently being processed.
415 packet = sock->packet;
417 rcode = fr_tcp_read_packet(packet, 0);
420 * Still only a partial packet. Put it back, and return,
421 * so that we'll read more data when it's ready.
427 if (rcode == -1) { /* error reading packet */
430 radlog(L_ERR, "Invalid packet from %s port %d: closing socket",
431 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
435 if (rcode < 0) { /* error or connection reset */
436 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
439 * Decrement the number of connections.
441 if (sock->parent->num_connections > 0) {
442 sock->parent->num_connections--;
444 if (sock->client->num_connections > 0) {
445 sock->client->num_connections--;
449 * Tell the event handler that an FD has disappeared.
451 DEBUG("Client has closed connection");
452 event_new_fd(listener);
455 * Do NOT free the listener here. It's in use by
456 * a request, and will need to hang around until
457 * all of the requests are done.
459 * It is instead free'd in remove_from_request_hash()
464 RAD_STATS_TYPE_INC(listener, total_requests);
467 * Some sanity checks, based on the packet code.
469 switch(packet->code) {
470 case PW_AUTHENTICATION_REQUEST:
471 RAD_STATS_CLIENT_INC(listener, client, total_requests);
472 fun = rad_authenticate;
475 case PW_STATUS_SERVER:
476 if (!mainconfig.status_server) {
477 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
478 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
479 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
480 rad_free(&sock->packet);
483 fun = rad_status_server;
487 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
488 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
490 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
491 packet->code, client->shortname, packet->src_port);
492 rad_free(&sock->packet);
494 } /* switch over packet types */
496 if (!received_request(listener, packet, prequest, sock->client)) {
497 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
498 RAD_STATS_CLIENT_INC(listener, sock->client, total_packets_dropped);
499 rad_free(&sock->packet);
504 sock->packet = NULL; /* we have no need for more partial reads */
508 static int auth_tcp_accept(rad_listen_t *listener,
509 UNUSED RAD_REQUEST_FUNP *pfun,
510 UNUSED REQUEST **prequest)
515 struct sockaddr_storage src;
516 listen_socket_t *sock;
517 fr_ipaddr_t src_ipaddr;
522 DEBUG2(" ... new connection request on TCP socket.");
524 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
527 * Non-blocking sockets must handle this.
529 if (errno == EWOULDBLOCK) {
533 DEBUG2(" ... failed to accept connection.");
537 if (!fr_sockaddr2ipaddr(&src, salen, &src_ipaddr, &src_port)) {
538 DEBUG2(" ... unknown address family.");
543 * Enforce client IP address checks on accept, not on
546 if ((client = client_listener_find(listener,
547 &src_ipaddr, src_port)) == NULL) {
549 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
554 * Enforce max_connectionsx on client && listen section.
556 if ((client->max_connections != 0) &&
557 (client->max_connections == client->num_connections)) {
559 * FIXME: Print client IP/port, and server IP/port.
561 radlog(L_INFO, "Ignoring new connection due to client max_connections (%d)", client->max_connections);
566 sock = listener->data;
567 if ((sock->max_connections != 0) &&
568 (sock->max_connections == sock->num_connections)) {
570 * FIXME: Print client IP/port, and server IP/port.
572 radlog(L_INFO, "Ignoring new connection due to socket max_connections");
576 client->num_connections++;
577 sock->num_connections++;
580 * Add the new listener.
582 this = listen_alloc(listener->type);
583 if (!this) return -1;
586 * Copy everything, including the pointer to the socket
590 memcpy(this->data, listener->data, sizeof(*sock));
591 memcpy(this, listener, sizeof(*this));
593 this->data = sock; /* fix it back */
595 sock->parent = listener->data;
596 sock->other_ipaddr = src_ipaddr;
597 sock->other_port = src_port;
598 sock->client = client;
601 this->status = RAD_LISTEN_STATUS_INIT;
602 this->recv = auth_tcp_recv;
605 * FIXME: set O_NONBLOCK on the accept'd fd.
606 * See djb's portability rants for details.
610 * Tell the event loop that we have a new FD.
611 * This can be called from a child thread...
621 * This function is stupid and complicated.
623 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
626 listen_socket_t *sock = this->data;
629 switch (this->type) {
631 case RAD_LISTEN_NONE: /* what a hack... */
636 case RAD_LISTEN_AUTH:
637 name = "authentication";
640 #ifdef WITH_ACCOUNTING
641 case RAD_LISTEN_ACCT:
647 case RAD_LISTEN_PROXY:
659 case RAD_LISTEN_DHCP:
675 #define FORWARD len = strlen(buffer); if (len >= (bufsize + 1)) return 0;buffer += len;bufsize -= len
676 #define ADDSTRING(_x) strlcpy(buffer, _x, bufsize);FORWARD
680 if (sock->interface) {
681 ADDSTRING(" interface ");
682 ADDSTRING(sock->interface);
686 if (this->recv == auth_tcp_accept) {
687 ADDSTRING(" proto tcp");
693 * TCP sockets get printed a little differently, to make
694 * it clear what's going on.
697 ADDSTRING(" from client (");
698 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
702 snprintf(buffer, bufsize, "%d", sock->other_port);
706 if ((sock->my_ipaddr.af == AF_INET) &&
707 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
708 strlcpy(buffer, "*", bufsize);
710 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
715 snprintf(buffer, bufsize, "%d", sock->my_port);
719 ADDSTRING(", virtual-server=");
720 ADDSTRING(this->server);
729 * Maybe it's a socket that we opened to a home server.
731 if ((sock->proto == IPPROTO_TCP) &&
732 (this->type == RAD_LISTEN_PROXY)) {
734 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
738 snprintf(buffer, bufsize, "%d", sock->my_port);
740 ADDSTRING(") -> home_server (");
742 if ((sock->other_ipaddr.af == AF_INET) &&
743 (sock->other_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
744 strlcpy(buffer, "*", bufsize);
746 ip_ntoh(&sock->other_ipaddr, buffer, bufsize);
751 snprintf(buffer, bufsize, "%d", sock->other_port);
760 ADDSTRING(" address ");
762 if ((sock->my_ipaddr.af == AF_INET) &&
763 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
764 strlcpy(buffer, "*", bufsize);
766 ip_ntoh(&sock->my_ipaddr, buffer, bufsize);
771 snprintf(buffer, bufsize, "%d", sock->my_port);
775 ADDSTRING(" as server ");
776 ADDSTRING(this->server);
787 * Parse an authentication or accounting socket.
789 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
794 listen_socket_t *sock = this->data;
795 char *section_name = NULL;
796 CONF_SECTION *client_cs, *parentcs;
801 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
802 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
803 &ipaddr.ipaddr.ip4addr, NULL);
804 if (rcode < 0) return -1;
806 if (rcode == 0) { /* successfully parsed IPv4 */
809 } else { /* maybe IPv6? */
810 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
811 &ipaddr.ipaddr.ip6addr, NULL);
812 if (rcode < 0) return -1;
815 cf_log_err(cf_sectiontoitem(cs),
816 "No address specified in listen section");
819 ipaddr.af = AF_INET6;
822 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
824 if (rcode < 0) return -1;
826 if ((listen_port < 0) || (listen_port > 65535)) {
827 cf_log_err(cf_sectiontoitem(cs),
828 "Invalid value for \"port\"");
832 sock->proto = IPPROTO_UDP;
834 if (cf_pair_find(cs, "proto")) {
836 cf_log_err(cf_sectiontoitem(cs),
837 "System does not support the TCP protocol. Delete this line from the configuration file.");
843 rcode = cf_item_parse(cs, "proto", PW_TYPE_STRING_PTR,
845 if (rcode < 0) return -1;
847 if (strcmp(proto, "udp") == 0) {
848 sock->proto = IPPROTO_UDP;
850 } else if (strcmp(proto, "tcp") == 0) {
851 sock->proto = IPPROTO_TCP;
853 rcode = cf_item_parse(cs, "max_connections", PW_TYPE_INTEGER,
854 &sock->max_connections, "64");
855 if (rcode < 0) return -1;
858 cf_log_err(cf_sectiontoitem(cs),
859 "Unknown proto name \"%s\"", proto);
866 * TCP requires a destination IP for sockets.
867 * UDP doesn't, so it's allowed.
869 if ((this->type == RAD_LISTEN_PROXY) &&
870 (sock->proto != IPPROTO_UDP)) {
871 cf_log_err(cf_sectiontoitem(cs),
872 "Proxy listeners can only listen on proto = udp");
878 sock->my_ipaddr = ipaddr;
879 sock->my_port = listen_port;
882 * If we can bind to interfaces, do so,
885 if (cf_pair_find(cs, "interface")) {
887 CONF_PAIR *cp = cf_pair_find(cs, "interface");
889 rad_assert(cp != NULL);
890 value = cf_pair_value(cp);
892 cf_log_err(cf_sectiontoitem(cs),
893 "No interface name given");
896 sock->interface = value;
901 * If we can do broadcasts..
903 if (cf_pair_find(cs, "broadcast")) {
905 cf_log_err(cf_sectiontoitem(cs),
906 "System does not support broadcast sockets. Delete this line from the configuration file.");
910 CONF_PAIR *cp = cf_pair_find(cs, "broadcast");
912 if (this->type != RAD_LISTEN_DHCP) {
913 cf_log_err(cf_pairtoitem(cp),
914 "Broadcast can only be set for DHCP listeners. Delete this line from the configuration file.");
918 rad_assert(cp != NULL);
919 value = cf_pair_value(cp);
921 cf_log_err(cf_sectiontoitem(cs),
922 "No broadcast value given");
927 * Hack... whatever happened to cf_section_parse?
929 sock->broadcast = (strcmp(value, "yes") == 0);
935 * And bind it to the port.
937 if (listen_bind(this) < 0) {
939 cf_log_err(cf_sectiontoitem(cs),
940 "Error binding to port for %s port %d",
941 ip_ntoh(&sock->my_ipaddr, buffer, sizeof(buffer)),
948 * Proxy sockets don't have clients.
950 if (this->type == RAD_LISTEN_PROXY) return 0;
954 * The more specific configurations are preferred to more
958 parentcs = cf_top_section(cs);
959 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
960 §ion_name, NULL);
961 if (rcode < 0) return -1; /* bad string */
964 * Explicit list given: use it.
966 client_cs = cf_section_sub_find_name2(parentcs,
970 client_cs = cf_section_find(section_name);
973 cf_log_err(cf_sectiontoitem(cs),
974 "Failed to find clients %s {...}",
980 } /* else there was no "clients = " entry. */
983 CONF_SECTION *server_cs;
985 server_cs = cf_section_sub_find_name2(parentcs,
989 * Found a "server foo" section. If there are clients
993 (cf_section_sub_find(server_cs, "client") != NULL)) {
994 client_cs = server_cs;
999 * Still nothing. Look for global clients.
1001 if (!client_cs) client_cs = parentcs;
1003 sock->clients = clients_parse_section(client_cs);
1004 if (!sock->clients) {
1005 cf_log_err(cf_sectiontoitem(cs),
1006 "Failed to load clients for this listen section");
1011 if (sock->proto == IPPROTO_TCP) {
1013 * Re-write the listener receive function to
1014 * allow us to accept the socket.
1016 this->recv = auth_tcp_accept;
1024 * Send an authentication response packet
1026 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
1028 rad_assert(request->listener == listener);
1029 rad_assert(listener->send == auth_socket_send);
1031 return rad_send(request->reply, request->packet,
1032 request->client->secret);
1036 #ifdef WITH_ACCOUNTING
1038 * Send an accounting response packet (or not)
1040 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
1042 rad_assert(request->listener == listener);
1043 rad_assert(listener->send == acct_socket_send);
1046 * Accounting reject's are silently dropped.
1048 * We do it here to avoid polluting the rest of the
1049 * code with this knowledge
1051 if (request->reply->code == 0) return 0;
1053 return rad_send(request->reply, request->packet,
1054 request->client->secret);
1060 * Send a packet to a home server.
1062 * FIXME: have different code for proxy auth & acct!
1064 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
1066 rad_assert(request->proxy_listener == listener);
1067 rad_assert(listener->send == proxy_socket_send);
1069 return rad_send(request->proxy, request->packet,
1070 request->home_server->secret);
1076 * Check if an incoming request is "ok"
1078 * It takes packets, not requests. It sees if the packet looks
1079 * OK. If so, it does a number of sanity checks on it.
1081 static int stats_socket_recv(rad_listen_t *listener,
1082 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1086 RADIUS_PACKET *packet;
1088 fr_ipaddr_t src_ipaddr;
1090 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1091 if (rcode < 0) return 0;
1093 RAD_STATS_TYPE_INC(listener, total_requests);
1095 if (rcode < 20) { /* AUTH_HDR_LEN */
1096 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1100 if ((client = client_listener_find(listener,
1101 &src_ipaddr, src_port)) == NULL) {
1102 rad_recv_discard(listener->fd);
1103 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1108 * We only understand Status-Server on this socket.
1110 if (code != PW_STATUS_SERVER) {
1111 DEBUG("Ignoring packet code %d sent to Status-Server port",
1113 rad_recv_discard(listener->fd);
1114 RAD_STATS_TYPE_INC(listener, total_unknown_types);
1115 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1120 * Now that we've sanity checked everything, receive the
1123 packet = rad_recv(listener->fd, 1); /* require message authenticator */
1125 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1126 DEBUG("%s", fr_strerror());
1130 if (!received_request(listener, packet, prequest, client)) {
1131 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1132 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1137 *pfun = rad_status_server;
1144 * Check if an incoming request is "ok"
1146 * It takes packets, not requests. It sees if the packet looks
1147 * OK. If so, it does a number of sanity checks on it.
1149 static int auth_socket_recv(rad_listen_t *listener,
1150 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1154 RADIUS_PACKET *packet;
1155 RAD_REQUEST_FUNP fun = NULL;
1157 fr_ipaddr_t src_ipaddr;
1159 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1160 if (rcode < 0) return 0;
1162 RAD_STATS_TYPE_INC(listener, total_requests);
1164 if (rcode < 20) { /* AUTH_HDR_LEN */
1165 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1169 if ((client = client_listener_find(listener,
1170 &src_ipaddr, src_port)) == NULL) {
1171 rad_recv_discard(listener->fd);
1172 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1177 * Some sanity checks, based on the packet code.
1180 case PW_AUTHENTICATION_REQUEST:
1181 RAD_STATS_CLIENT_INC(listener, client, total_requests);
1182 fun = rad_authenticate;
1185 case PW_STATUS_SERVER:
1186 if (!mainconfig.status_server) {
1187 rad_recv_discard(listener->fd);
1188 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1189 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1190 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1193 fun = rad_status_server;
1197 rad_recv_discard(listener->fd);
1198 RAD_STATS_INC(radius_auth_stats.total_unknown_types);
1199 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1201 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
1202 code, client->shortname, src_port);
1205 } /* switch over packet types */
1208 * Now that we've sanity checked everything, receive the
1211 packet = rad_recv(listener->fd, client->message_authenticator);
1213 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1214 DEBUG("%s", fr_strerror());
1218 if (!received_request(listener, packet, prequest, client)) {
1219 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1220 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1230 #ifdef WITH_ACCOUNTING
1232 * Receive packets from an accounting socket
1234 static int acct_socket_recv(rad_listen_t *listener,
1235 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1239 RADIUS_PACKET *packet;
1240 RAD_REQUEST_FUNP fun = NULL;
1242 fr_ipaddr_t src_ipaddr;
1244 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1245 if (rcode < 0) return 0;
1247 RAD_STATS_TYPE_INC(listener, total_requests);
1249 if (rcode < 20) { /* AUTH_HDR_LEN */
1250 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1254 if ((client = client_listener_find(listener,
1255 &src_ipaddr, src_port)) == NULL) {
1256 rad_recv_discard(listener->fd);
1257 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1262 * Some sanity checks, based on the packet code.
1265 case PW_ACCOUNTING_REQUEST:
1266 RAD_STATS_CLIENT_INC(listener, client, total_requests);
1267 fun = rad_accounting;
1270 case PW_STATUS_SERVER:
1271 if (!mainconfig.status_server) {
1272 rad_recv_discard(listener->fd);
1273 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1274 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1276 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
1279 fun = rad_status_server;
1283 rad_recv_discard(listener->fd);
1284 RAD_STATS_TYPE_INC(listener, total_unknown_types);
1285 RAD_STATS_CLIENT_INC(listener, client, total_unknown_types);
1287 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
1288 code, client->shortname, src_port);
1290 } /* switch over packet types */
1293 * Now that we've sanity checked everything, receive the
1296 packet = rad_recv(listener->fd, 0);
1298 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1299 radlog(L_ERR, "%s", fr_strerror());
1304 * There can be no duplicate accounting packets.
1306 if (!received_request(listener, packet, prequest, client)) {
1307 RAD_STATS_TYPE_INC(listener, total_packets_dropped);
1308 RAD_STATS_CLIENT_INC(listener, client, total_packets_dropped);
1321 * For now, all CoA requests are *only* originated, and not
1322 * proxied. So all of the necessary work is done in the
1323 * post-proxy section, which is automatically handled by event.c.
1324 * As a result, we don't have to do anything here.
1326 static int rad_coa_reply(REQUEST *request)
1328 VALUE_PAIR *s1, *s2;
1331 * Inform the user about RFC requirements.
1333 s1 = pairfind(request->proxy->vps, PW_STATE, 0);
1335 s2 = pairfind(request->proxy_reply->vps, PW_STATE, 0);
1338 DEBUG("WARNING: Client was sent State in CoA, and did not respond with State.");
1340 } else if ((s1->length != s2->length) ||
1341 (memcmp(s1->vp_octets, s2->vp_octets,
1342 s1->length) != 0)) {
1343 DEBUG("WARNING: Client was sent State in CoA, and did not respond with the same State.");
1347 return RLM_MODULE_OK;
1351 * Receive a CoA packet.
1353 static int rad_coa_recv(REQUEST *request)
1355 int rcode = RLM_MODULE_OK;
1360 * Get the correct response
1362 switch (request->packet->code) {
1363 case PW_COA_REQUEST:
1368 case PW_DISCONNECT_REQUEST:
1369 ack = PW_DISCONNECT_ACK;
1370 nak = PW_DISCONNECT_NAK;
1373 default: /* shouldn't happen */
1374 return RLM_MODULE_FAIL;
1378 #define WAS_PROXIED (request->proxy)
1380 #define WAS_PROXIED (0)
1385 * RFC 5176 Section 3.3. If we have a CoA-Request
1386 * with Service-Type = Authorize-Only, it MUST
1387 * have a State attribute in it.
1389 vp = pairfind(request->packet->vps, PW_SERVICE_TYPE, 0);
1390 if (request->packet->code == PW_COA_REQUEST) {
1391 if (vp && (vp->vp_integer == 17)) {
1392 vp = pairfind(request->packet->vps, PW_STATE, 0);
1393 if (!vp || (vp->length == 0)) {
1394 RDEBUG("ERROR: CoA-Request with Service-Type = Authorize-Only MUST contain a State attribute");
1395 request->reply->code = PW_COA_NAK;
1396 return RLM_MODULE_FAIL;
1401 * RFC 5176, Section 3.2.
1403 RDEBUG("ERROR: Disconnect-Request MUST NOT contain a Service-Type attribute");
1404 request->reply->code = PW_DISCONNECT_NAK;
1405 return RLM_MODULE_FAIL;
1408 rcode = module_recv_coa(0, request);
1410 case RLM_MODULE_FAIL:
1411 case RLM_MODULE_INVALID:
1412 case RLM_MODULE_REJECT:
1413 case RLM_MODULE_USERLOCK:
1415 request->reply->code = nak;
1418 case RLM_MODULE_HANDLED:
1421 case RLM_MODULE_NOOP:
1422 case RLM_MODULE_NOTFOUND:
1424 case RLM_MODULE_UPDATED:
1425 request->reply->code = ack;
1430 * Start the reply code with the proxy reply
1433 request->reply->code = request->proxy_reply->code;
1437 * Copy State from the request to the reply.
1438 * See RFC 5176 Section 3.3.
1440 vp = paircopy2(request->packet->vps, PW_STATE, 0);
1441 if (vp) pairadd(&request->reply->vps, vp);
1444 * We may want to over-ride the reply.
1446 rcode = module_send_coa(0, request);
1449 * We need to send CoA-NAK back if Service-Type
1450 * is Authorize-Only. Rely on the user's policy
1451 * to do that. We're not a real NAS, so this
1452 * restriction doesn't (ahem) apply to us.
1454 case RLM_MODULE_FAIL:
1455 case RLM_MODULE_INVALID:
1456 case RLM_MODULE_REJECT:
1457 case RLM_MODULE_USERLOCK:
1460 * Over-ride an ACK with a NAK
1462 request->reply->code = nak;
1465 case RLM_MODULE_HANDLED:
1468 case RLM_MODULE_NOOP:
1469 case RLM_MODULE_NOTFOUND:
1471 case RLM_MODULE_UPDATED:
1473 * Do NOT over-ride a previously set value.
1474 * Otherwise an "ok" here will re-write a
1477 if (request->reply->code == 0) {
1478 request->reply->code = ack;
1484 return RLM_MODULE_OK;
1489 * Check if an incoming request is "ok"
1491 * It takes packets, not requests. It sees if the packet looks
1492 * OK. If so, it does a number of sanity checks on it.
1494 static int coa_socket_recv(rad_listen_t *listener,
1495 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1499 RADIUS_PACKET *packet;
1500 RAD_REQUEST_FUNP fun = NULL;
1502 fr_ipaddr_t src_ipaddr;
1504 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
1505 if (rcode < 0) return 0;
1507 RAD_STATS_TYPE_INC(listener, total_requests);
1509 if (rcode < 20) { /* AUTH_HDR_LEN */
1510 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1514 if ((client = client_listener_find(listener,
1515 &src_ipaddr, src_port)) == NULL) {
1516 rad_recv_discard(listener->fd);
1517 RAD_STATS_TYPE_INC(listener, total_invalid_requests);
1522 * Some sanity checks, based on the packet code.
1525 case PW_COA_REQUEST:
1526 case PW_DISCONNECT_REQUEST:
1531 rad_recv_discard(listener->fd);
1532 DEBUG("Invalid packet code %d sent to coa port from client %s port %d : IGNORED",
1533 code, client->shortname, src_port);
1536 } /* switch over packet types */
1539 * Now that we've sanity checked everything, receive the
1542 packet = rad_recv(listener->fd, client->message_authenticator);
1544 RAD_STATS_TYPE_INC(listener, total_malformed_requests);
1545 DEBUG("%s", fr_strerror());
1549 if (!received_request(listener, packet, prequest, client)) {
1561 * Recieve packets from a proxy socket.
1563 static int proxy_socket_recv(rad_listen_t *listener,
1564 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1567 RADIUS_PACKET *packet;
1568 RAD_REQUEST_FUNP fun = NULL;
1571 packet = rad_recv(listener->fd, 0);
1573 radlog(L_ERR, "%s", fr_strerror());
1578 * FIXME: Client MIB updates?
1580 switch(packet->code) {
1581 case PW_AUTHENTICATION_ACK:
1582 case PW_ACCESS_CHALLENGE:
1583 case PW_AUTHENTICATION_REJECT:
1584 fun = rad_authenticate;
1587 #ifdef WITH_ACCOUNTING
1588 case PW_ACCOUNTING_RESPONSE:
1589 fun = rad_accounting;
1594 case PW_DISCONNECT_ACK:
1595 case PW_DISCONNECT_NAK:
1598 fun = rad_coa_reply;
1604 * FIXME: Update MIB for packet types?
1606 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1607 "from home server %s port %d - ID %d : IGNORED",
1609 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1610 packet->src_port, packet->id);
1615 request = received_proxy_response(packet);
1623 * Distinguish proxied CoA requests from ones we
1626 if ((fun == rad_coa_reply) &&
1627 (request->packet->code == request->proxy->code)) {
1632 rad_assert(fun != NULL);
1634 *prequest = request;
1641 * Recieve packets from a proxy socket.
1643 static int proxy_socket_tcp_recv(rad_listen_t *listener,
1644 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1647 RADIUS_PACKET *packet;
1648 RAD_REQUEST_FUNP fun = NULL;
1649 listen_socket_t *sock = listener->data;
1652 packet = fr_tcp_recv(listener->fd, 0);
1654 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
1655 event_new_fd(listener);
1660 * FIXME: Client MIB updates?
1662 switch(packet->code) {
1663 case PW_AUTHENTICATION_ACK:
1664 case PW_ACCESS_CHALLENGE:
1665 case PW_AUTHENTICATION_REJECT:
1666 fun = rad_authenticate;
1669 #ifdef WITH_ACCOUNTING
1670 case PW_ACCOUNTING_RESPONSE:
1671 fun = rad_accounting;
1677 * FIXME: Update MIB for packet types?
1679 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
1680 "from home server %s port %d - ID %d : IGNORED",
1682 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
1683 packet->src_port, packet->id);
1688 packet->src_ipaddr = sock->other_ipaddr;
1689 packet->src_port = sock->other_port;
1690 packet->dst_ipaddr = sock->my_ipaddr;
1691 packet->dst_port = sock->my_port;
1694 * FIXME: Have it return an indication of packets that
1695 * are OK to ignore (dups, too late), versus ones that
1696 * aren't OK to ignore (unknown response, spoofed, etc.)
1698 * Close the socket on bad packets...
1700 request = received_proxy_response(packet);
1705 rad_assert(fun != NULL);
1706 sock->opened = sock->last_packet = request->timestamp;
1709 *prequest = request;
1717 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1719 if (!request->reply->code) return 0;
1721 rad_encode(request->reply, request->packet,
1722 request->client->secret);
1723 rad_sign(request->reply, request->packet,
1724 request->client->secret);
1730 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1732 if (rad_verify(request->packet, NULL,
1733 request->client->secret) < 0) {
1737 return rad_decode(request->packet, NULL,
1738 request->client->secret);
1742 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
1744 rad_encode(request->proxy, NULL, request->home_server->secret);
1745 rad_sign(request->proxy, NULL, request->home_server->secret);
1751 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
1754 * rad_verify is run in event.c, received_proxy_response()
1757 return rad_decode(request->proxy_reply, request->proxy,
1758 request->home_server->secret);
1764 #include "command.c"
1766 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1768 { common_socket_parse, NULL,
1769 stats_socket_recv, auth_socket_send,
1770 socket_print, client_socket_encode, client_socket_decode },
1773 * This always gets defined.
1775 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1780 { common_socket_parse, NULL,
1781 proxy_socket_recv, proxy_socket_send,
1782 socket_print, proxy_socket_encode, proxy_socket_decode },
1785 /* authentication */
1786 { common_socket_parse, NULL,
1787 auth_socket_recv, auth_socket_send,
1788 socket_print, client_socket_encode, client_socket_decode },
1790 #ifdef WITH_ACCOUNTING
1792 { common_socket_parse, NULL,
1793 acct_socket_recv, acct_socket_send,
1794 socket_print, client_socket_encode, client_socket_decode},
1799 { detail_parse, detail_free,
1800 detail_recv, detail_send,
1801 detail_print, detail_encode, detail_decode },
1805 /* vlan query protocol */
1806 { common_socket_parse, NULL,
1807 vqp_socket_recv, vqp_socket_send,
1808 socket_print, vqp_socket_encode, vqp_socket_decode },
1812 /* dhcp query protocol */
1813 { dhcp_socket_parse, NULL,
1814 dhcp_socket_recv, dhcp_socket_send,
1815 socket_print, dhcp_socket_encode, dhcp_socket_decode },
1818 #ifdef WITH_COMMAND_SOCKET
1819 /* TCP command socket */
1820 { command_socket_parse, command_socket_free,
1821 command_domain_accept, command_domain_send,
1822 command_socket_print, command_socket_encode, command_socket_decode },
1826 /* Change of Authorization */
1827 { common_socket_parse, NULL,
1828 coa_socket_recv, auth_socket_send, /* CoA packets are same as auth */
1829 socket_print, client_socket_encode, client_socket_decode },
1837 * Binds a listener to a socket.
1839 static int listen_bind(rad_listen_t *this)
1842 struct sockaddr_storage salocal;
1844 listen_socket_t *sock = this->data;
1846 #define proto_for_port "udp"
1847 #define sock_type SOCK_DGRAM
1849 const char *proto_for_port = "udp";
1850 int sock_type = SOCK_DGRAM;
1852 if (sock->proto == IPPROTO_TCP) {
1854 if (this->type == RAD_LISTEN_VQP) {
1855 radlog(L_ERR, "VQP does not support TCP transport");
1860 proto_for_port = "tcp";
1861 sock_type = SOCK_STREAM;
1866 * If the port is zero, then it means the appropriate
1867 * thing from /etc/services.
1869 if (sock->my_port == 0) {
1870 struct servent *svp;
1872 switch (this->type) {
1873 case RAD_LISTEN_AUTH:
1874 svp = getservbyname ("radius", proto_for_port);
1876 sock->my_port = ntohs(svp->s_port);
1878 sock->my_port = PW_AUTH_UDP_PORT;
1882 #ifdef WITH_ACCOUNTING
1883 case RAD_LISTEN_ACCT:
1884 svp = getservbyname ("radacct", proto_for_port);
1886 sock->my_port = ntohs(svp->s_port);
1888 sock->my_port = PW_ACCT_UDP_PORT;
1894 case RAD_LISTEN_PROXY:
1895 /* leave it at zero */
1900 case RAD_LISTEN_VQP:
1901 sock->my_port = 1589;
1906 case RAD_LISTEN_COA:
1907 svp = getservbyname ("radius-dynauth", "udp");
1909 sock->my_port = ntohs(svp->s_port);
1911 sock->my_port = PW_COA_UDP_PORT;
1917 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
1923 * Copy fr_socket() here, as we may need to bind to a device.
1925 this->fd = socket(sock->my_ipaddr.af, sock_type, 0);
1927 radlog(L_ERR, "Failed opening socket: %s", strerror(errno));
1932 * Bind to a device BEFORE touching IP addresses.
1934 if (sock->interface) {
1935 #ifdef SO_BINDTODEVICE
1937 strcpy(ifreq.ifr_name, sock->interface);
1940 rcode = setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
1941 (char *)&ifreq, sizeof(ifreq));
1945 radlog(L_ERR, "Failed binding to interface %s: %s",
1946 sock->interface, strerror(errno));
1948 } /* else it worked. */
1950 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1951 #ifdef HAVE_NET_IF_H
1953 * Odds are that any system supporting "bind to
1954 * device" also supports IPv6, so this next bit
1955 * isn't necessary. But it's here for
1958 * If we're doing IPv6, and the scope hasn't yet
1959 * been defined, set the scope to the scope of
1962 if (sock->my_ipaddr.af == AF_INET6) {
1963 if (sock->my_ipaddr.scope == 0) {
1964 sock->my_ipaddr.scope = if_nametoindex(sock->interface);
1965 if (sock->my_ipaddr.scope == 0) {
1967 radlog(L_ERR, "Failed finding interface %s: %s",
1968 sock->interface, strerror(errno));
1971 } /* else scope was defined: we're OK. */
1976 * IPv4: no link local addresses,
1977 * and no bind to device.
1981 radlog(L_ERR, "Failed binding to interface %s: \"bind to device\" is unsupported", sock->interface);
1988 if (sock->proto == IPPROTO_TCP) {
1991 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
1993 radlog(L_ERR, "Failed to reuse address: %s", strerror(errno));
1999 #if defined(WITH_TCP) && defined(WITH_UDPFROMTO)
2000 else /* UDP sockets get UDPfromto */
2003 #ifdef WITH_UDPFROMTO
2005 * Initialize udpfromto for all sockets.
2007 if (udpfromto_init(this->fd) != 0) {
2014 * Set up sockaddr stuff.
2016 if (!fr_ipaddr2sockaddr(&sock->my_ipaddr, sock->my_port, &salocal, &salen)) {
2021 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2022 if (sock->my_ipaddr.af == AF_INET6) {
2024 * Listening on '::' does NOT get you IPv4 to
2025 * IPv6 mapping. You've got to listen on an IPv4
2026 * address, too. This makes the rest of the server
2027 * design a little simpler.
2031 if (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr)) {
2034 setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2035 (char *)&on, sizeof(on));
2037 #endif /* IPV6_V6ONLY */
2039 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
2041 if (sock->my_ipaddr.af == AF_INET) {
2044 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2046 * Disable PMTU discovery. On Linux, this
2047 * also makes sure that the "don't fragment"
2050 flag = IP_PMTUDISC_DONT;
2051 setsockopt(this->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2052 &flag, sizeof(flag));
2055 #if defined(IP_DONTFRAG)
2057 * Ensure that the "don't fragment" flag is zero.
2060 setsockopt(this->fd, IPPROTO_IP, IP_DONTFRAG,
2061 &flag, sizeof(flag));
2067 if (sock->broadcast) {
2070 if (setsockopt(this->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
2071 radlog(L_ERR, "Can't set broadcast option: %s\n",
2080 * May be binding to priviledged ports.
2082 if (sock->my_port != 0) {
2086 if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
2087 radlog(L_ERR, "Can't set re-use address option: %s\n",
2094 rcode = bind(this->fd, (struct sockaddr *) &salocal, salen);
2100 this->print(this, buffer, sizeof(buffer));
2101 radlog(L_ERR, "Failed binding to %s: %s\n",
2102 buffer, strerror(errno));
2107 * FreeBSD jail issues. We bind to 0.0.0.0, but the
2108 * kernel instead binds us to a 1.2.3.4. If this
2109 * happens, notice, and remember our real IP.
2112 struct sockaddr_storage src;
2113 socklen_t sizeof_src = sizeof(src);
2115 memset(&src, 0, sizeof_src);
2116 if (getsockname(this->fd, (struct sockaddr *) &src,
2118 radlog(L_ERR, "Failed getting socket name: %s",
2123 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2124 &sock->my_ipaddr, &sock->my_port)) {
2125 radlog(L_ERR, "Socket has unsupported address family");
2132 if (sock->proto == IPPROTO_TCP) {
2133 if (listen(this->fd, 8) < 0) {
2135 radlog(L_ERR, "Failed in listen(): %s", strerror(errno));
2141 if (fr_nonblock(this->fd) < 0) {
2143 radlog(L_ERR, "Failed setting non-blocking on socket: %s",
2149 * Mostly for proxy sockets.
2151 sock->other_ipaddr.af = sock->my_ipaddr.af;
2154 * Don't screw up other people.
2156 #undef proto_for_port
2164 * Allocate & initialize a new listener.
2166 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
2170 this = rad_malloc(sizeof(*this));
2171 memset(this, 0, sizeof(*this));
2174 this->recv = master_listen[this->type].recv;
2175 this->send = master_listen[this->type].send;
2176 this->print = master_listen[this->type].print;
2177 this->encode = master_listen[this->type].encode;
2178 this->decode = master_listen[this->type].decode;
2182 case RAD_LISTEN_NONE:
2184 case RAD_LISTEN_AUTH:
2185 #ifdef WITH_ACCOUNTING
2186 case RAD_LISTEN_ACCT:
2189 case RAD_LISTEN_PROXY:
2192 case RAD_LISTEN_VQP:
2195 case RAD_LISTEN_COA:
2197 this->data = rad_malloc(sizeof(listen_socket_t));
2198 memset(this->data, 0, sizeof(listen_socket_t));
2202 case RAD_LISTEN_DHCP:
2203 this->data = rad_malloc(sizeof(dhcp_socket_t));
2204 memset(this->data, 0, sizeof(dhcp_socket_t));
2209 case RAD_LISTEN_DETAIL:
2214 #ifdef WITH_COMMAND_SOCKET
2215 case RAD_LISTEN_COMMAND:
2216 this->data = rad_malloc(sizeof(fr_command_socket_t));
2217 memset(this->data, 0, sizeof(fr_command_socket_t));
2222 rad_assert("Unsupported option!" == NULL);
2231 * Externally visible function for creating a new proxy LISTENER.
2233 * Not thread-safe, but all calls to it are protected by the
2234 * proxy mutex in event.c
2236 int proxy_new_listener(home_server *home, int src_port)
2239 listen_socket_t *sock;
2241 if (!home) return 0;
2243 if ((home->max_connections > 0) &&
2244 (home->num_connections >= home->max_connections)) {
2245 DEBUG("WARNING: Home server has too many open connections (%d)",
2246 home->max_connections);
2250 this = listen_alloc(RAD_LISTEN_PROXY);
2253 sock->other_ipaddr = home->ipaddr;
2254 sock->other_port = home->port;
2257 sock->my_ipaddr = home->src_ipaddr;
2258 sock->my_port = src_port;
2259 sock->proto = home->proto;
2262 sock->last_packet = time(NULL);
2264 if (home->proto == IPPROTO_TCP) {
2265 this->recv = proxy_socket_tcp_recv;
2268 * FIXME: connect() is blocking!
2269 * We do this with the proxy mutex locked, which may
2270 * cause large delays!
2272 * http://www.developerweb.net/forum/showthread.php?p=13486
2274 this->fd = fr_tcp_client_socket(&home->src_ipaddr,
2275 &home->ipaddr, home->port);
2278 this->fd = fr_socket(&home->src_ipaddr, src_port);
2281 DEBUG("Failed opening client socket: %s", fr_strerror());
2287 * Figure out which port we were bound to.
2289 if (sock->my_port == 0) {
2290 struct sockaddr_storage src;
2291 socklen_t sizeof_src = sizeof(src);
2293 memset(&src, 0, sizeof_src);
2294 if (getsockname(this->fd, (struct sockaddr *) &src,
2296 radlog(L_ERR, "Failed getting socket name: %s",
2302 if (!fr_sockaddr2ipaddr(&src, sizeof_src,
2303 &sock->my_ipaddr, &sock->my_port)) {
2304 radlog(L_ERR, "Socket has unsupported address family");
2311 * Tell the event loop that we have a new FD
2313 if (!event_new_fd(this)) {
2323 static const FR_NAME_NUMBER listen_compare[] = {
2325 { "status", RAD_LISTEN_NONE },
2327 { "auth", RAD_LISTEN_AUTH },
2328 #ifdef WITH_ACCOUNTING
2329 { "acct", RAD_LISTEN_ACCT },
2332 { "detail", RAD_LISTEN_DETAIL },
2335 { "proxy", RAD_LISTEN_PROXY },
2338 { "vmps", RAD_LISTEN_VQP },
2341 { "dhcp", RAD_LISTEN_DHCP },
2343 #ifdef WITH_COMMAND_SOCKET
2344 { "control", RAD_LISTEN_COMMAND },
2347 { "coa", RAD_LISTEN_COA },
2353 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
2361 cf_log_info(cs, "listen {");
2363 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
2365 if (rcode < 0) return NULL;
2368 cf_log_err(cf_sectiontoitem(cs),
2369 "No type specified in listen section");
2373 type = fr_str2int(listen_compare, listen_type, -1);
2375 cf_log_err(cf_sectiontoitem(cs),
2376 "Invalid type \"%s\" in listen section.",
2384 * Allow listen sections in the default config to
2385 * refer to a server.
2388 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
2390 if (rcode == 1) { /* compatiblity with 2.0-pre */
2391 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
2394 if (rcode < 0) return NULL;
2399 * We were passed a virtual server, so the caller is
2400 * defining a proxy listener inside of a virtual server.
2401 * This isn't allowed right now.
2403 else if (type == RAD_LISTEN_PROXY) {
2404 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
2410 * Set up cross-type data.
2412 this = listen_alloc(type);
2413 this->server = server;
2417 * Call per-type parser.
2419 if (master_listen[type].parse(cs, this) < 0) {
2424 cf_log_info(cs, "}");
2430 * Generate a list of listeners. Takes an input list of
2431 * listeners, too, so we don't close sockets with waiting packets.
2433 int listen_init(CONF_SECTION *config, rad_listen_t **head)
2435 int override = FALSE;
2437 CONF_SECTION *cs = NULL;
2438 rad_listen_t **last;
2440 fr_ipaddr_t server_ipaddr;
2443 int defined_proxy = 0;
2447 * We shouldn't be called with a pre-existing list.
2449 rad_assert(head && (*head == NULL));
2452 server_ipaddr.af = AF_UNSPEC;
2455 * If the port is specified on the command-line,
2456 * it over-rides the configuration file.
2458 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
2460 if (mainconfig.port >= 0) auth_port = mainconfig.port;
2463 * If the IP address was configured on the command-line,
2464 * use that as the "bind_address"
2466 if (mainconfig.myip.af != AF_UNSPEC) {
2467 memcpy(&server_ipaddr, &mainconfig.myip,
2468 sizeof(server_ipaddr));
2474 * Else look for bind_address and/or listen sections.
2476 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
2477 rcode = cf_item_parse(config, "bind_address",
2479 &server_ipaddr.ipaddr.ip4addr, NULL);
2480 if (rcode < 0) return -1; /* error parsing it */
2482 if (rcode == 0) { /* successfully parsed IPv4 */
2483 listen_socket_t *sock;
2484 server_ipaddr.af = AF_INET;
2486 radlog(L_INFO, "WARNING: The directive 'bind_address' is deprecated, and will be removed in future versions of FreeRADIUS. Please edit the configuration files to use the directive 'listen'.");
2490 if (strcmp(progname, "vmpsd") == 0) {
2491 this = listen_alloc(RAD_LISTEN_VQP);
2492 if (!auth_port) auth_port = 1589;
2495 this = listen_alloc(RAD_LISTEN_AUTH);
2499 sock->my_ipaddr = server_ipaddr;
2500 sock->my_port = auth_port;
2502 sock->clients = clients_parse_section(config);
2503 if (!sock->clients) {
2504 cf_log_err(cf_sectiontoitem(config),
2505 "Failed to find any clients for this listen section");
2510 if (listen_bind(this) < 0) {
2512 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->my_port);
2516 auth_port = sock->my_port; /* may have been updated in listen_bind */
2518 cs = cf_section_sub_find_name2(config, "server",
2520 if (cs) this->server = mainconfig.name;
2524 last = &(this->next);
2530 if (strcmp(progname, "vmpsd") == 0) goto add_sockets;
2533 #ifdef WITH_ACCOUNTING
2535 * Open Accounting Socket.
2537 * If we haven't already gotten acct_port from
2538 * /etc/services, then make it auth_port + 1.
2540 this = listen_alloc(RAD_LISTEN_ACCT);
2544 * Create the accounting socket.
2546 * The accounting port is always the
2547 * authentication port + 1
2549 sock->my_ipaddr = server_ipaddr;
2550 sock->my_port = auth_port + 1;
2552 sock->clients = clients_parse_section(config);
2553 if (!sock->clients) {
2554 cf_log_err(cf_sectiontoitem(config),
2555 "Failed to find any clients for this listen section");
2559 if (listen_bind(this) < 0) {
2562 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->my_port);
2567 cs = cf_section_sub_find_name2(config, "server",
2569 if (cs) this->server = mainconfig.name;
2573 last = &(this->next);
2575 } else if (mainconfig.port > 0) { /* no bind address, but a port */
2576 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
2582 * They specified an IP on the command-line, ignore
2583 * all listen sections except the one in '-n'.
2585 if (mainconfig.myip.af != AF_UNSPEC) {
2586 CONF_SECTION *subcs;
2587 const char *name2 = cf_section_name2(cs);
2589 cs = cf_section_sub_find_name2(config, "server",
2591 if (!cs) goto add_sockets;
2594 * Should really abstract this code...
2596 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2598 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2599 this = listen_parse(subcs, name2);
2606 last = &(this->next);
2607 } /* loop over "listen" directives in server <foo> */
2613 * Walk through the "listen" sections, if they exist.
2615 for (cs = cf_subsection_find_next(config, NULL, "listen");
2617 cs = cf_subsection_find_next(config, cs, "listen")) {
2618 this = listen_parse(cs, NULL);
2625 last = &(this->next);
2629 * Check virtual servers for "listen" sections, too.
2631 * FIXME: Move to virtual server init?
2633 for (cs = cf_subsection_find_next(config, NULL, "server");
2635 cs = cf_subsection_find_next(config, cs, "server")) {
2636 CONF_SECTION *subcs;
2637 const char *name2 = cf_section_name2(cs);
2639 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
2641 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
2642 this = listen_parse(subcs, name2);
2649 last = &(this->next);
2650 } /* loop over "listen" directives in virtual servers */
2651 } /* loop over virtual servers */
2655 * Print out which sockets we're listening on, and
2656 * add them to the event list.
2658 for (this = *head; this != NULL; this = this->next) {
2660 if (this->type == RAD_LISTEN_PROXY) {
2669 * If we're proxying requests, open the proxy FD.
2670 * Otherwise, don't do anything.
2673 if ((mainconfig.proxy_requests == TRUE) &&
2674 (*head != NULL) && !defined_proxy) {
2675 listen_socket_t *sock = NULL;
2679 memset(&home, 0, sizeof(home));
2684 home.proto = IPPROTO_UDP;
2685 home.src_ipaddr = server_ipaddr;
2688 * Find the first authentication port,
2691 for (this = *head; this != NULL; this = this->next) {
2692 if (this->type == RAD_LISTEN_AUTH) {
2694 if (home.src_ipaddr.af == AF_UNSPEC) {
2695 home.src_ipaddr = sock->my_ipaddr;
2697 port = sock->my_port + 2;
2701 if (this->type == RAD_LISTEN_ACCT) {
2703 if (home.src_ipaddr.af == AF_UNSPEC) {
2704 home.src_ipaddr = sock->my_ipaddr;
2706 port = sock->my_port + 1;
2713 * Address is still unspecified, use IPv4.
2715 if (home.src_ipaddr.af == AF_UNSPEC) {
2716 home.src_ipaddr.af = AF_INET;
2717 /* everything else is already set to zero */
2720 home.ipaddr.af = home.src_ipaddr.af;
2721 /* everything else is already set to zero */
2723 if (!proxy_new_listener(&home, port)) {
2731 * Haven't defined any sockets. Die.
2733 if (!*head) return -1;
2740 * Free a linked list of listeners;
2742 void listen_free(rad_listen_t **head)
2746 if (!head || !*head) return;
2750 rad_listen_t *next = this->next;
2753 * Other code may have eaten the FD.
2755 if (this->fd >= 0) close(this->fd);
2757 if (master_listen[this->type].free) {
2758 master_listen[this->type].free(this);
2762 if ((this->type == RAD_LISTEN_AUTH) ||
2764 (this->type == RAD_LISTEN_ACCT) ||
2767 (this->type == RAD_LISTEN_PROXY)
2770 listen_socket_t *sock = this->data;
2771 rad_free(&sock->packet);
2785 RADCLIENT_LIST *listener_find_client_list(const fr_ipaddr_t *ipaddr,
2790 for (this = mainconfig.listen; this != NULL; this = this->next) {
2791 listen_socket_t *sock;
2793 if ((this->type != RAD_LISTEN_AUTH) &&
2794 (this->type != RAD_LISTEN_ACCT)) continue;
2798 if ((sock->my_port == port) &&
2799 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
2800 return sock->clients;
2808 rad_listen_t *listener_find_byipaddr(const fr_ipaddr_t *ipaddr, int port)
2812 for (this = mainconfig.listen; this != NULL; this = this->next) {
2813 listen_socket_t *sock;
2816 * FIXME: For TCP, ignore the *secondary*
2817 * listeners associated with the main socket.
2819 if ((this->type != RAD_LISTEN_AUTH) &&
2820 (this->type != RAD_LISTEN_ACCT)) continue;
2824 if ((sock->my_port == port) &&
2825 (fr_ipaddr_cmp(ipaddr, &sock->my_ipaddr) == 0)) {
2829 if ((sock->my_port == port) &&
2830 ((sock->my_ipaddr.af == AF_INET) &&
2831 (sock->my_ipaddr.ipaddr.ip4addr.s_addr == INADDR_ANY))) {
2835 #ifdef HAVE_STRUCT_SOCKADDR_IN6
2836 if ((sock->my_port == port) &&
2837 (sock->my_ipaddr.af == AF_INET6) &&
2838 (IN6_IS_ADDR_UNSPECIFIED(&sock->my_ipaddr.ipaddr.ip6addr))) {