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/radius_snmp.h>
29 #include <freeradius-devel/rad_assert.h>
31 #include <sys/resource.h>
37 #ifdef HAVE_SYS_STAT_H
43 static time_t start_time = 0;
46 * FIXME: Delete this crap!
48 extern time_t time_now;
51 * We'll use this below.
53 typedef int (*rad_listen_parse_t)(const char *, int, const CONF_SECTION *, rad_listen_t *);
54 typedef void (*rad_listen_free_t)(rad_listen_t *);
56 typedef struct rad_listen_master_t {
57 rad_listen_parse_t parse;
58 rad_listen_free_t free;
59 rad_listen_recv_t recv;
60 rad_listen_send_t send;
61 rad_listen_print_t print;
62 rad_listen_encode_t encode;
63 rad_listen_decode_t decode;
64 } rad_listen_master_t;
66 typedef struct listen_socket_t {
72 RADCLIENT_LIST *clients;
75 typedef struct listen_detail_t {
81 lrad_ipaddr_t client_ip;
88 * Find a per-socket client.
90 static RADCLIENT *client_listener_find(const rad_listen_t *listener,
91 const lrad_ipaddr_t *ipaddr)
93 const RADCLIENT_LIST *clients;
95 rad_assert(listener != NULL);
96 rad_assert(ipaddr != NULL);
98 rad_assert((listener->type == RAD_LISTEN_AUTH) ||
99 (listener->type == RAD_LISTEN_ACCT));
101 clients = ((listen_socket_t *)listener->data)->clients;
102 if (!clients) clients = mainconfig.clients;
104 rad_assert(clients != NULL);
106 return client_find(clients, ipaddr);
109 static int listen_bind(rad_listen_t *this);
112 * FIXME: have the detail reader use another config "exit when done",
113 * so that it can be used as a one-off tool to update stuff.
117 * Process and reply to a server-status request.
118 * Like rad_authenticate and rad_accounting this should
119 * live in it's own file but it's so small we don't bother.
121 static int rad_status_server(REQUEST *request)
123 switch (request->listener->type) {
124 case RAD_LISTEN_AUTH:
125 request->reply->code = PW_AUTHENTICATION_ACK;
128 * Commented out for now.
137 * Reply with an ACK. We might want to add some more
138 * interesting reply attributes, such as server uptime.
140 t = request->timestamp - start_time;
141 sprintf(reply_msg, "FreeRADIUS STUFF %d day%s, %02d:%02d",
142 (int)(t / 86400), (t / 86400) == 1 ? "" : "s",
143 (int)((t / 3600) % 24), (int)(t / 60) % 60);
145 vp = pairmake("Reply-Message", reply_msg, T_OP_SET);
146 pairadd(&request->reply->vps, vp); /* don't need to check if !vp */
151 case RAD_LISTEN_ACCT:
152 request->reply->code = PW_ACCOUNTING_RESPONSE;
163 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
166 listen_socket_t *sock = this->data;
168 if ((sock->ipaddr.af == AF_INET) &&
169 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
172 ip_ntoh(&sock->ipaddr, buffer, bufsize);
175 len = strlen(buffer);
177 return len + snprintf(buffer + len, bufsize - len, " port %d",
183 * Parse an authentication or accounting socket.
185 static int common_socket_parse(const char *filename, int lineno,
186 const CONF_SECTION *cs, rad_listen_t *this)
190 lrad_ipaddr_t ipaddr;
191 listen_socket_t *sock = this->data;
192 const char *section_name = NULL;
193 CONF_SECTION *client_cs;
198 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
199 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
200 &ipaddr.ipaddr.ip4addr, NULL);
201 if (rcode < 0) return -1;
203 if (rcode == 0) { /* successfully parsed IPv4 */
206 } else { /* maybe IPv6? */
207 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
208 &ipaddr.ipaddr.ip6addr, NULL);
209 if (rcode < 0) return -1;
212 radlog(L_ERR, "%s[%d]: No address specified in listen section",
216 ipaddr.af = AF_INET6;
219 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
221 if (rcode < 0) return -1;
223 sock->ipaddr = ipaddr;
224 sock->port = listen_port;
227 * And bind it to the port.
229 if (listen_bind(this) < 0) {
231 radlog(L_CONS|L_ERR, "%s[%d]: Error binding to port for %s port %d",
232 filename, cf_section_lineno(cs),
233 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
239 * If we can bind to interfaces, do so,
242 if (cf_pair_find(cs, "interface")) {
243 #ifndef SO_BINDTODEVICE
244 radlog(L_CONS|L_ERR, "%s[%d]: System does not support binding to interfaces, delete this line from the configuration file.",
245 filename, cf_section_lineno(cs));
249 const CONF_PAIR *cp = cf_pair_find(cs, "interface");
252 rad_assert(cp != NULL);
253 value = cf_pair_value(cp);
254 rad_assert(value != NULL);
256 strcpy(ifreq.ifr_name, value);
258 if (setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
259 (char *)&ifreq, sizeof(ifreq)) < 0) {
260 radlog(L_CONS|L_ERR, "%s[%d]: Failed binding to interface %s: %s",
261 filename, cf_section_lineno(cs),
262 value, strerror(errno));
264 } /* else it worked. */
269 * Look for the name of a section that holds a list
272 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
273 §ion_name, NULL);
274 if (rcode < 0) return -1; /* bad string */
275 if (rcode > 0) return 0; /* non-existent is OK. */
277 client_cs = cf_section_find(section_name);
280 radlog(L_CONS|L_ERR, "%s[%d]: Failed to find client section %s",
281 filename, cf_section_lineno(cs), section_name);
285 sock->clients = clients_parse_section(filename, client_cs);
286 if (!sock->clients) {
294 * Send an authentication response packet
296 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
298 rad_assert(request->listener == listener);
299 rad_assert(listener->send == auth_socket_send);
301 return rad_send(request->reply, request->packet,
302 request->client->secret);
307 * Send an accounting response packet (or not)
309 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
311 rad_assert(request->listener == listener);
312 rad_assert(listener->send == acct_socket_send);
315 * Accounting reject's are silently dropped.
317 * We do it here to avoid polluting the rest of the
318 * code with this knowledge
320 if (request->reply->code == 0) return 0;
322 return rad_send(request->reply, request->packet,
323 request->client->secret);
328 * Send a packet to a home server.
330 * FIXME: have different code for proxy auth & acct!
332 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
334 listen_socket_t *sock = listener->data;
336 rad_assert(request->proxy_listener == listener);
337 rad_assert(listener->send == proxy_socket_send);
339 request->proxy->src_ipaddr = sock->ipaddr;
340 request->proxy->src_port = sock->port;
342 return rad_send(request->proxy, request->packet,
343 request->home_server->secret);
348 * Check if an incoming request is "ok"
350 * It takes packets, not requests. It sees if the packet looks
351 * OK. If so, it does a number of sanity checks on it.
353 static int auth_socket_recv(rad_listen_t *listener,
354 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
358 RADIUS_PACKET *packet;
359 RAD_REQUEST_FUNP fun = NULL;
362 lrad_ipaddr_t src_ipaddr;
364 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
365 if (rcode < 0) return 0;
367 RAD_SNMP_TYPE_INC(listener, total_requests);
369 if (rcode < 20) { /* AUTH_HDR_LEN */
370 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
374 if ((client = client_listener_find(listener,
375 &src_ipaddr)) == NULL) {
376 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
379 * This is debugging rather than logging, so that
380 * DoS attacks don't affect us.
382 DEBUG("Ignoring request from unknown client %s port %d",
383 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
384 buffer, sizeof(buffer)), src_port);
389 * Some sanity checks, based on the packet code.
392 case PW_AUTHENTICATION_REQUEST:
393 RAD_SNMP_CLIENT_INC(listener, client, requests);
394 fun = rad_authenticate;
397 case PW_STATUS_SERVER:
398 if (!mainconfig.status_server) {
399 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
400 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
401 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
404 fun = rad_status_server;
408 RAD_SNMP_INC(rad_snmp.auth.total_unknown_types);
409 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
411 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
412 code, client->shortname, src_port);
415 } /* switch over packet types */
418 * Now that we've sanity checked everything, receive the
421 packet = rad_recv(listener->fd);
423 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
424 radlog(L_ERR, "%s", librad_errstr);
428 if (!received_request(listener, packet, prequest, client)) {
429 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
430 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
441 * Receive packets from an accounting socket
443 static int acct_socket_recv(rad_listen_t *listener,
444 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
448 RADIUS_PACKET *packet;
449 RAD_REQUEST_FUNP fun = NULL;
452 lrad_ipaddr_t src_ipaddr;
454 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
455 if (rcode < 0) return 0;
457 RAD_SNMP_TYPE_INC(listener, total_requests);
459 if (rcode < 20) { /* AUTH_HDR_LEN */
460 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
464 if ((client = client_listener_find(listener,
465 &src_ipaddr)) == NULL) {
466 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
469 * This is debugging rather than logging, so that
470 * DoS attacks don't affect us.
472 DEBUG("Ignoring request from unknown client %s port %d",
473 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
474 buffer, sizeof(buffer)), src_port);
479 * Some sanity checks, based on the packet code.
481 switch(packet->code) {
482 case PW_ACCOUNTING_REQUEST:
483 RAD_SNMP_CLIENT_INC(listener, client, requests);
484 fun = rad_accounting;
487 case PW_STATUS_SERVER:
488 if (!mainconfig.status_server) {
489 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
490 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
492 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
495 fun = rad_status_server;
500 * FIXME: Update MIB for packet types?
502 RAD_SNMP_TYPE_INC(listener, total_unknown_types);
503 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
505 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
506 code, client->shortname, src_port);
508 } /* switch over packet types */
511 * Now that we've sanity checked everything, receive the
514 packet = rad_recv(listener->fd);
516 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
517 radlog(L_ERR, "%s", librad_errstr);
522 * There can be no duplicate accounting packets.
524 if (!received_request(listener, packet, prequest, client)) {
525 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
526 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
537 * Recieve packets from a proxy socket.
539 static int proxy_socket_recv(rad_listen_t *listener,
540 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
543 RADIUS_PACKET *packet;
544 RAD_REQUEST_FUNP fun = NULL;
547 packet = rad_recv(listener->fd);
549 radlog(L_ERR, "%s", librad_errstr);
554 * FIXME: Client MIB updates?
556 switch(packet->code) {
557 case PW_AUTHENTICATION_ACK:
558 case PW_ACCESS_CHALLENGE:
559 case PW_AUTHENTICATION_REJECT:
560 fun = rad_authenticate;
563 case PW_ACCOUNTING_RESPONSE:
564 fun = rad_accounting;
569 * FIXME: Update MIB for packet types?
571 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
572 "from home server %s port %d - ID %d : IGNORED",
574 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
575 packet->src_port, packet->id);
580 request = received_proxy_response(packet);
585 rad_assert(fun != NULL);
593 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
595 if (!request->reply->code) return 0;
597 rad_encode(request->reply, request->packet,
598 request->client->secret);
599 rad_sign(request->reply, request->packet,
600 request->client->secret);
606 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
608 if (rad_verify(request->packet, NULL,
609 request->client->secret) < 0) {
613 return rad_decode(request->packet, NULL,
614 request->client->secret);
617 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
619 rad_encode(request->proxy, NULL, request->home_server->secret);
620 rad_sign(request->proxy, NULL, request->home_server->secret);
626 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
628 if (rad_verify(request->proxy_reply, request->proxy,
629 request->home_server->secret) < 0) {
633 return rad_decode(request->proxy_reply, request->proxy,
634 request->home_server->secret);
638 #define STATE_UNOPENED (0)
639 #define STATE_UNLOCKED (1)
640 #define STATE_HEADER (2)
641 #define STATE_READING (3)
642 #define STATE_DONE (4)
643 #define STATE_WAITING (5)
646 * If we're limiting outstanding packets, then mark the response
649 static int detail_send(rad_listen_t *listener, REQUEST *request)
651 listen_detail_t *data = listener->data;
653 rad_assert(request->listener == listener);
654 rad_assert(listener->send == detail_send);
656 if (request->simul_max >= 0) {
657 rad_assert(data->outstanding != NULL);
658 rad_assert(request->simul_max < data->max_outstanding);
660 data->outstanding[request->simul_max] = 0;
668 * Open the detail file..
670 * FIXME: create it, if it's not already there, so that the main
671 * server select() will wake us up if there's anything to read.
673 static int detail_open(rad_listen_t *this)
677 listen_detail_t *data = this->data;
679 rad_assert(data->state == STATE_UNOPENED);
680 snprintf(buffer, sizeof(buffer), "%s.work", data->detail);
683 * FIXME: Have "one-shot" configuration, where it
684 * will read the detail file, and exit once it's
687 * FIXME: Try harder to open the detail file.
688 * Maybe sleep for X usecs if it doesn't exist?
692 * Open detail.work first, so we don't lose
693 * accounting packets. It's probably better to
694 * duplicate them than to lose them.
696 * Note that we're not writing to the file, but
697 * we've got to open it for writing in order to
698 * establish the lock, to prevent rlm_detail from
701 this->fd = open(buffer, O_RDWR);
704 * Try reading the detail file. If it
705 * doesn't exist, we can't do anything.
707 * Doing the stat will tell us if the file
708 * exists, even if we don't have permissions
711 if (stat(data->detail, &st) < 0) {
716 * Open it BEFORE we rename it, just to
719 this->fd = open(data->detail, O_RDWR);
721 radlog(L_ERR, "Failed to open %s: %s",
722 data->detail, strerror(errno));
727 * Rename detail to detail.work
729 if (rename(data->detail, buffer) < 0) {
734 } /* else detail.work existed, and we opened it */
736 rad_assert(data->vps == NULL);
738 rad_assert(data->fp == NULL);
739 data->fp = fdopen(this->fd, "r");
741 radlog(L_ERR, "Failed to re-open %s: %s",
742 data->detail, strerror(errno));
746 data->state = STATE_UNLOCKED;
748 data->client_ip.af = AF_UNSPEC;
755 * This is a bad hack, just so complaints have meaningful text.
757 static const RADCLIENT detail_client = {
772 static int detail_recv(rad_listen_t *listener,
773 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
776 char key[256], value[1024];
777 VALUE_PAIR *vp, **tail;
778 RADIUS_PACKET *packet;
780 listen_detail_t *data = listener->data;
782 if (data->state == STATE_UNOPENED) {
783 rad_assert(listener->fd < 0);
784 if (!detail_open(listener)) return 0;
786 rad_assert(listener->fd >= 0);
789 * Try to lock fd. If we can't, return. If we can,
790 * continue. This means that the server doesn't block
791 * while waiting for the lock to open...
793 if (data->state == STATE_UNLOCKED) {
795 * Note that we do NOT block waiting for the
796 * lock. We've re-named the file above, so we've
797 * already guaranteed that any *new* detail
798 * writer will not be opening this file. The
799 * only purpose of the lock is to catch a race
800 * condition where the execution "ping-pongs"
801 * between radiusd & radrelay.
803 if (rad_lockfd_nonblock(listener->fd, 0) < 0) {
807 * Look for the header
809 data->state = STATE_HEADER;
813 * Catch an out of memory condition which will most likely
816 if (data->state == STATE_DONE) goto alloc_packet;
819 * If we're in another state, then it means that we read
820 * a partial packet, which is bad.
822 rad_assert(data->state == STATE_HEADER);
823 rad_assert(data->vps == NULL);
826 * We read the last packet, and returned it for
827 * processing. We later come back here to shut
828 * everything down, and unlink the file.
830 if (feof(data->fp)) {
831 rad_assert(data->state == STATE_HEADER);
834 * Don't unlink the file until we've received
835 * all of the responses.
837 if (data->max_outstanding > 0) {
840 for (i = 0; i < data->max_outstanding; i++) {
842 * FIXME: close the file?
844 if (data->outstanding[i]) {
845 data->state = STATE_WAITING;
852 rad_assert(data->vps == NULL);
854 snprintf(buffer, sizeof(buffer), "%s.work", data->detail);
856 fclose(data->fp); /* closes listener->fd */
859 data->state = STATE_UNOPENED;
862 * Try to open "detail" again. If we're on a
863 * busy RADIUS server, odds are that it will
866 detail_open(listener);
875 while (fgets(buffer, sizeof(buffer), data->fp)) {
879 if (!strchr(buffer, '\n')) {
880 pairfree(&data->vps);
885 * We've read a header, possibly packet contents,
886 * and are now at the end of the packet.
888 if ((data->state == STATE_READING) &&
889 (buffer[0] == '\n')) {
890 data->state = STATE_DONE;
895 * Look for date/time header, and read VP's if
896 * found. If not, keep reading lines until we
899 if (data->state == STATE_HEADER) {
902 if (sscanf(buffer, "%*s %*s %*d %*d:%*d:%*d %d", &y)) {
903 data->state = STATE_READING;
909 * We have a full "attribute = value" line.
910 * If it doesn't look reasonable, skip it.
912 if (sscanf(buffer, "%255s = %1023s", key, value) != 2) {
917 * Skip non-protocol attributes.
919 if (!strcasecmp(key, "Request-Authenticator")) continue;
922 * Set the original client IP address, based on
923 * what's in the detail file.
925 * Hmm... we don't set the server IP address.
928 if (!strcasecmp(key, "Client-IP-Address")) {
929 data->client_ip.af = AF_INET;
930 ip_hton(value, AF_INET, &data->client_ip);
935 * The original time at which we received the
936 * packet. We need this to properly calculate
939 if (!strcasecmp(key, "Timestamp")) {
940 data->timestamp = atoi(value);
947 * FIXME: do we want to check for non-protocol
948 * attributes like radsqlrelay does?
951 if ((userparse(buffer, &vp) > 0) &&
959 * We got to EOF, If we're in STATE_HEADER, it's OK.
960 * Otherwise it's a problem. In any case, nuke the file
961 * and start over from scratch,
963 if (feof(data->fp)) {
968 * FIXME: Do load management.
972 * If we're not done, then there's a problem. The checks
975 rad_assert(data->state == STATE_DONE);
978 * The packet we read was empty, re-set the state to look
979 * for a header, and don't return anything.
982 data->state = STATE_HEADER;
987 * Allocate the packet. If we fail, it's a serious
992 * If we keep track of the outstanding requests, do so
993 * here. Note that to minimize potential work, we do
994 * so only once the file is opened & locked.
996 if (data->max_outstanding) {
999 for (i = 0; i < data->max_outstanding; i++) {
1000 if (!data->outstanding[i]) {
1007 * All of the slots are full. Put the LF back,
1008 * so select will say that data is ready, and set
1009 * the state back to reading. Then, return so
1010 * that we don't overload the server.
1012 if (free_slot < 0) {
1013 ungetc('\n', data->fp);
1014 data->state = STATE_READING;
1019 packet = rad_alloc(1);
1021 return 0; /* maybe memory will magically free up... */
1024 memset(packet, 0, sizeof(*packet));
1025 packet->sockfd = -1;
1026 packet->src_ipaddr.af = AF_INET;
1027 packet->src_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1028 packet->code = PW_ACCOUNTING_REQUEST;
1029 packet->timestamp = time(NULL);
1032 * Look for Acct-Delay-Time, and update
1033 * based on Acct-Delay-Time += (time(NULL) - timestamp)
1035 vp = pairfind(packet->vps, PW_ACCT_DELAY_TIME);
1037 vp = paircreate(PW_ACCT_DELAY_TIME, PW_TYPE_INTEGER);
1038 rad_assert(vp != NULL);
1040 if (data->timestamp != 0) {
1041 vp->vp_integer += time(NULL) - data->timestamp;
1045 * Remember where it came from, so that we don't
1046 * proxy it to the place it came from...
1048 if (data->client_ip.af != AF_UNSPEC) {
1049 packet->src_ipaddr = data->client_ip;
1052 vp = pairfind(packet->vps, PW_PACKET_SRC_IP_ADDRESS);
1054 packet->src_ipaddr.af = AF_INET;
1055 packet->src_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
1057 vp = pairfind(packet->vps, PW_PACKET_SRC_IPV6_ADDRESS);
1059 packet->src_ipaddr.af = AF_INET6;
1060 memcpy(&packet->src_ipaddr.ipaddr.ip6addr,
1061 &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
1065 vp = pairfind(packet->vps, PW_PACKET_DST_IP_ADDRESS);
1067 packet->dst_ipaddr.af = AF_INET;
1068 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
1070 vp = pairfind(packet->vps, PW_PACKET_DST_IPV6_ADDRESS);
1072 packet->dst_ipaddr.af = AF_INET6;
1073 memcpy(&packet->dst_ipaddr.ipaddr.ip6addr,
1074 &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
1079 * We've got to give SOME value for Id & ports, so that
1080 * the packets can be added to the request queue.
1081 * However, we don't want to keep track of used/unused
1082 * id's and ports, as that's a lot of work. This hack
1083 * ensures that (if we have real random numbers), that
1084 * there will be a collision on every 2^(16+15+15+24 - 1)
1085 * packets, on average. That means we can read 2^37
1086 * packets before having a collision, which means it's
1087 * effectively impossible.
1089 packet->id = lrad_rand() & 0xffff;
1090 packet->src_port = 1024 + (lrad_rand() & 0x7fff);
1091 packet->dst_port = 1024 + (lrad_rand() & 0x7fff);
1093 packet->dst_ipaddr.af = AF_INET;
1094 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl((INADDR_LOOPBACK & ~0xffffff) | (lrad_rand() & 0xffffff));
1096 packet->vps = data->vps;
1102 data->state = STATE_HEADER;
1105 * Keep track of free slots, as a hack, in an otherwise
1108 (*prequest)->simul_max = free_slot;
1109 data->outstanding[free_slot] = 1;
1111 *pfun = rad_accounting;
1114 printf("detail_recv: Read packet from %s\n", data->detail);
1115 for (vp = packet->vps; vp; vp = vp->next) {
1117 vp_print(stdout, vp);
1123 * FIXME: many of these checks may not be necessary when
1124 * reading from the detail file.
1126 if (!received_request(listener, packet, prequest, &detail_client)) {
1136 * Free detail-specific stuff.
1138 static void detail_free(rad_listen_t *this)
1140 listen_detail_t *data = this->data;
1143 pairfree(&data->vps);
1144 free(data->outstanding);
1146 if (data->fp != NULL) fclose(data->fp);
1150 static int detail_print(rad_listen_t *this, char *buffer, size_t bufsize)
1152 return snprintf(buffer, bufsize, "%s",
1153 ((listen_detail_t *)(this->data))->detail);
1156 static int detail_encode(UNUSED rad_listen_t *this, UNUSED REQUEST *request)
1159 * We never encode responses "sent to" the detail file.
1164 static int detail_decode(UNUSED rad_listen_t *this, UNUSED REQUEST *request)
1167 * We never decode responses read from the detail file.
1173 static const CONF_PARSER detail_config[] = {
1174 { "detail", PW_TYPE_STRING_PTR,
1175 offsetof(listen_detail_t, detail), NULL, NULL },
1176 { "max_outstanding", PW_TYPE_INTEGER,
1177 offsetof(listen_detail_t, max_outstanding), NULL, "100" },
1179 { NULL, -1, 0, NULL, NULL } /* end the list */
1184 * Parse a detail section.
1186 static int detail_parse(const char *filename, int lineno,
1187 const CONF_SECTION *cs, rad_listen_t *this)
1190 listen_detail_t *data;
1194 rcode = cf_section_parse(cs, data, detail_config);
1196 radlog(L_ERR, "%s[%d]: Failed parsing listen section",
1201 if (!data->detail) {
1202 radlog(L_ERR, "%s[%d]: No detail file specified in listen section",
1209 data->state = STATE_UNOPENED;
1211 if (data->max_outstanding > 32768) data->max_outstanding = 32768;
1213 if (data->max_outstanding > 0) {
1214 data->outstanding = rad_malloc(sizeof(int) * data->max_outstanding);
1224 static int radius_snmp_recv(rad_listen_t *listener,
1225 UNUSED RAD_REQUEST_FUNP *pfun,
1226 UNUSED REQUEST **prequest)
1228 if (!mainconfig.do_snmp) return 0;
1230 if ((rad_snmp.smux_fd >= 0) &&
1231 (rad_snmp.smux_event == SMUX_READ)) {
1236 * If we've got to re-connect, then do so now,
1237 * before calling select again.
1239 if (rad_snmp.smux_event == SMUX_CONNECT) {
1244 * Reset this every time, as the smux connect may have
1245 * opened a new socket.
1247 listener->fd = rad_snmp.smux_fd;
1253 static int radius_snmp_print(rad_listen_t *this, char *buffer, size_t bufsize)
1255 return snprintf(buffer, bufsize, "SMUX with OID .1.3.6.1.4.1.3317.1.3.1");
1260 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1261 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1263 /* authentication */
1264 { common_socket_parse, NULL,
1265 auth_socket_recv, auth_socket_send,
1266 socket_print, client_socket_encode, client_socket_decode },
1269 { common_socket_parse, NULL,
1270 acct_socket_recv, acct_socket_send,
1271 socket_print, client_socket_encode, client_socket_decode},
1275 proxy_socket_recv, proxy_socket_send,
1276 socket_print, proxy_socket_encode, proxy_socket_decode },
1279 { detail_parse, detail_free,
1280 detail_recv, detail_send,
1281 detail_print, detail_encode, detail_decode },
1283 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_SNMP */
1289 * Binds a listener to a socket.
1291 static int listen_bind(rad_listen_t *this)
1293 rad_listen_t **last;
1294 listen_socket_t *sock = this->data;
1297 * If the port is zero, then it means the appropriate
1298 * thing from /etc/services.
1300 if (sock->port == 0) {
1301 struct servent *svp;
1303 switch (this->type) {
1304 case RAD_LISTEN_AUTH:
1305 svp = getservbyname ("radius", "udp");
1307 sock->port = ntohs(svp->s_port);
1309 sock->port = PW_AUTH_UDP_PORT;
1313 case RAD_LISTEN_ACCT:
1314 svp = getservbyname ("radacct", "udp");
1316 sock->port = ntohs(svp->s_port);
1318 sock->port = PW_ACCT_UDP_PORT;
1323 radlog(L_ERR|L_CONS, "ERROR: Non-fatal internal sanity check failed in bind.");
1329 * Find it in the old list, AFTER updating the port. If
1330 * it's there, use that, rather than creating a new
1331 * socket. This allows HUP's to re-use the old sockets,
1332 * which means that packets waiting in the socket queue
1335 for (last = &mainconfig.listen;
1337 last = &((*last)->next)) {
1338 if ((this->type == (*last)->type) &&
1339 (sock->port == ((listen_socket_t *)((*last)->data))->port) &&
1340 (sock->ipaddr.af == ((listen_socket_t *)((*last)->data))->ipaddr.af)) {
1343 if (sock->ipaddr.af == AF_INET) {
1344 equal = (sock->ipaddr.ipaddr.ip4addr.s_addr == ((listen_socket_t *)((*last)->data))->ipaddr.ipaddr.ip4addr.s_addr);
1345 } else if (sock->ipaddr.af == AF_INET6) {
1346 equal = IN6_ARE_ADDR_EQUAL(&(sock->ipaddr.ipaddr.ip6addr), &(((listen_socket_t *)((*last)->data))->ipaddr.ipaddr.ip6addr));
1352 this->fd = (*last)->fd;
1359 this->fd = lrad_socket(&sock->ipaddr, sock->port);
1361 radlog(L_ERR|L_CONS, "ERROR: Failed to open socket: %s",
1368 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
1369 radlog(L_ERR, "Failure in fcntl: %s)\n", strerror(errno));
1373 flags |= O_NONBLOCK;
1374 if( fcntl(this->fd, F_SETFL, flags) < 0) {
1375 radlog(L_ERR, "Failure in fcntl: %s)\n", strerror(errno));
1386 * Allocate & initialize a new listener.
1388 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
1392 this = rad_malloc(sizeof(*this));
1393 memset(this, 0, sizeof(*this));
1396 this->recv = master_listen[this->type].recv;
1397 this->send = master_listen[this->type].send;
1398 this->print = master_listen[this->type].print;
1399 this->encode = master_listen[this->type].encode;
1400 this->decode = master_listen[this->type].decode;
1403 case RAD_LISTEN_AUTH:
1404 case RAD_LISTEN_ACCT:
1405 case RAD_LISTEN_PROXY:
1406 this->data = rad_malloc(sizeof(listen_socket_t));
1407 memset(this->data, 0, sizeof(listen_socket_t));
1410 case RAD_LISTEN_DETAIL:
1411 this->data = rad_malloc(sizeof(listen_detail_t));
1412 memset(this->data, 0, sizeof(listen_detail_t));
1423 * Externally visible function for creating a new proxy LISTENER.
1425 * For now, don't take ipaddr or port.
1427 * Not thread-safe, but all calls to it are protected by the
1428 * proxy mutex in request_list.c
1430 rad_listen_t *proxy_new_listener()
1432 int last_proxy_port, port;
1433 rad_listen_t *this, *tmp, **last;
1434 listen_socket_t *sock, *old;
1436 this = listen_alloc(RAD_LISTEN_PROXY);
1439 * Find an existing proxy socket to copy.
1441 * FIXME: Make it per-realm, or per-home server!
1443 last_proxy_port = 0;
1445 last = &mainconfig.listen;
1446 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1447 if (tmp->type == RAD_LISTEN_PROXY) {
1449 if (sock->port > last_proxy_port) {
1450 last_proxy_port = sock->port + 1;
1452 if (!old) old = sock;
1455 last = &(tmp->next);
1458 if (!old) return NULL; /* This is a serious error. */
1461 * FIXME: find a new IP address to listen on?
1464 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1467 * Keep going until we find an unused port.
1469 for (port = last_proxy_port; port < 64000; port++) {
1471 if (listen_bind(this) == 0) {
1473 * Add the new listener to the list of
1485 static const LRAD_NAME_NUMBER listen_compare[] = {
1486 { "auth", RAD_LISTEN_AUTH },
1487 { "acct", RAD_LISTEN_ACCT },
1488 { "detail", RAD_LISTEN_DETAIL },
1494 * Generate a list of listeners. Takes an input list of
1495 * listeners, too, so we don't close sockets with waiting packets.
1497 int listen_init(const char *filename, rad_listen_t **head)
1501 rad_listen_t **last;
1503 lrad_ipaddr_t server_ipaddr;
1507 * We shouldn't be called with a pre-existing list.
1509 rad_assert(head && (*head == NULL));
1511 if (start_time != 0) start_time = time(NULL);
1514 server_ipaddr.af = AF_UNSPEC;
1517 * If the port is specified on the command-line,
1518 * it over-rides the configuration file.
1520 if (mainconfig.port >= 0) {
1521 auth_port = mainconfig.port;
1523 rcode = cf_item_parse(mainconfig.config, "port",
1524 PW_TYPE_INTEGER, &auth_port,
1525 Stringify(PW_AUTH_UDP_PORT));
1526 if (rcode < 0) return -1; /* error parsing it */
1529 radlog(L_INFO, "WARNING: The directive 'port' is deprecated, and will be removed in future versions of FreeRADIUS. Please edit the configuration files to use the directive 'listen'.");
1533 * If the IP address was configured on the command-line,
1534 * use that as the "bind_address"
1536 if (mainconfig.myip.af != AF_UNSPEC) {
1537 memcpy(&server_ipaddr, &mainconfig.myip,
1538 sizeof(server_ipaddr));
1543 * Else look for bind_address and/or listen sections.
1545 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1546 rcode = cf_item_parse(mainconfig.config, "bind_address",
1548 &server_ipaddr.ipaddr.ip4addr, NULL);
1549 if (rcode < 0) return -1; /* error parsing it */
1551 if (rcode == 0) { /* successfully parsed IPv4 */
1552 listen_socket_t *sock;
1553 server_ipaddr.af = AF_INET;
1555 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'.");
1558 this = listen_alloc(RAD_LISTEN_AUTH);
1561 sock->ipaddr = server_ipaddr;
1562 sock->port = auth_port;
1564 if (listen_bind(this) < 0) {
1567 radlog(L_CONS|L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1570 auth_port = sock->port; /* may have been updated in listen_bind */
1572 last = &(this->next);
1575 * Open Accounting Socket.
1577 * If we haven't already gotten acct_port from
1578 * /etc/services, then make it auth_port + 1.
1580 this = listen_alloc(RAD_LISTEN_ACCT);
1584 * Create the accounting socket.
1586 * The accounting port is always the
1587 * authentication port + 1
1589 sock->ipaddr = server_ipaddr;
1590 sock->port = auth_port + 1;
1592 if (listen_bind(this) < 0) {
1595 radlog(L_CONS|L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1600 last = &(this->next);
1602 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1603 radlog(L_CONS|L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1609 * They specified an IP on the command-line, ignore
1610 * all listen sections.
1612 if (mainconfig.myip.af != AF_UNSPEC) goto do_proxy;
1615 * Walk through the "listen" sections, if they exist.
1617 for (cs = cf_subsection_find_next(mainconfig.config, NULL, "listen");
1619 cs = cf_subsection_find_next(mainconfig.config, cs, "listen")) {
1621 char *listen_type, *identity;
1622 int lineno = cf_section_lineno(cs);
1624 listen_type = identity = NULL;
1626 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1628 if (rcode < 0) return -1;
1632 radlog(L_ERR, "%s[%d]: No type specified in listen section",
1638 * See if there's an identity.
1640 rcode = cf_item_parse(cs, "identity", PW_TYPE_STRING_PTR,
1648 type = lrad_str2int(listen_compare, listen_type,
1651 if (type == RAD_LISTEN_NONE) {
1653 radlog(L_CONS|L_ERR, "%s[%d]: Invalid type in listen section.",
1659 * Set up cross-type data.
1661 this = listen_alloc(type);
1662 this->identity = identity;
1666 * Call per-type parser.
1668 if (master_listen[type].parse(filename, lineno,
1676 last = &(this->next);
1680 * If we're proxying requests, open the proxy FD.
1681 * Otherwise, don't do anything.
1684 if (mainconfig.proxy_requests == TRUE) {
1686 listen_socket_t *sock = NULL;
1689 * No sockets to receive packets, therefore
1690 * proxying is pointless.
1692 if (!*head) return -1;
1695 * If we previously had proxy sockets, copy them
1696 * to the new config.
1698 if (mainconfig.listen != NULL) {
1699 rad_listen_t *old, *next, **tail;
1701 tail = &mainconfig.listen;
1702 for (old = mainconfig.listen;
1707 if (old->type != RAD_LISTEN_PROXY) {
1708 tail = &((*tail)->next);
1715 last = &(old->next);
1722 * Find the first authentication port,
1725 for (this = *head; this != NULL; this = this->next) {
1726 if (this->type == RAD_LISTEN_AUTH) {
1728 if (server_ipaddr.af == AF_UNSPEC) {
1729 server_ipaddr = sock->ipaddr;
1731 port = sock->port + 2; /* skip acct port */
1736 if (port < 0) port = 1024 + (lrad_rand() & 0x1ff);
1739 * Address is still unspecified, use IPv4.
1741 if (server_ipaddr.af == AF_UNSPEC) {
1742 server_ipaddr.af = AF_INET;
1743 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1746 this = listen_alloc(RAD_LISTEN_PROXY);
1750 * Create the first proxy socket.
1752 sock->ipaddr = server_ipaddr;
1755 * Try to find a proxy port (value doesn't matter)
1757 for (sock->port = port;
1760 if (listen_bind(this) == 0) {
1762 last = &(this->next); /* just in case */
1767 if (sock->port >= 64000) {
1770 radlog(L_ERR|L_CONS, "Failed to open socket for proxying");
1777 if (mainconfig.do_snmp) {
1781 * Forget about the old one.
1783 for (this = mainconfig.listen;
1785 this = this->next) {
1786 if (this->type != RAD_LISTEN_SNMP) continue;
1790 this = rad_malloc(sizeof(*this));
1791 memset(this, 0, sizeof(*this));
1793 this->type = RAD_LISTEN_SNMP;
1794 this->fd = rad_snmp.smux_fd;
1796 this->recv = radius_snmp_recv;
1797 this->print = radius_snmp_print;
1800 last = &(this->next);
1805 * Sanity check the configuration.
1808 if (mainconfig.listen != NULL) for (this = *head;
1810 this = this->next) {
1811 if (((this->type == RAD_LISTEN_ACCT) &&
1812 (rcode == RAD_LISTEN_DETAIL)) ||
1813 ((this->type == RAD_LISTEN_DETAIL) &&
1814 (rcode == RAD_LISTEN_ACCT))) {
1815 rad_assert(0 == 1); /* FIXME: configuration error */
1818 if (rcode != 0) continue;
1820 if ((this->type == RAD_LISTEN_ACCT) ||
1821 (this->type == RAD_LISTEN_DETAIL)) {
1831 * Free a linked list of listeners;
1833 void listen_free(rad_listen_t **head)
1837 if (!head || !*head) return;
1841 rad_listen_t *next = this->next;
1843 free(this->identity);
1846 * Other code may have eaten the FD.
1848 if (this->fd >= 0) close(this->fd);
1850 if (master_listen[this->type].free) {
1851 master_listen[this->type].free(this);