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 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, request->secret);
306 * Send an accounting response packet (or not)
308 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
310 rad_assert(request->listener == listener);
311 rad_assert(listener->send == acct_socket_send);
314 * Accounting reject's are silently dropped.
316 * We do it here to avoid polluting the rest of the
317 * code with this knowledge
319 if (request->reply->code == 0) return 0;
321 return rad_send(request->reply, request->packet, request->secret);
326 * Send a packet to a home server.
328 * FIXME: have different code for proxy auth & acct!
330 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
332 listen_socket_t *sock = listener->data;
334 rad_assert(request->proxy_listener == listener);
335 rad_assert(listener->send == proxy_socket_send);
337 request->proxy->src_ipaddr = sock->ipaddr;
338 request->proxy->src_port = sock->port;
340 return rad_send(request->proxy, request->packet,
341 request->home_server->secret);
346 * Check if an incoming request is "ok"
348 * It takes packets, not requests. It sees if the packet looks
349 * OK. If so, it does a number of sanity checks on it.
351 static int auth_socket_recv(rad_listen_t *listener,
352 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
354 RADIUS_PACKET *packet;
355 RAD_REQUEST_FUNP fun = NULL;
359 packet = rad_recv(listener->fd);
361 RAD_SNMP_TYPE_INC(listener, total_requests);
362 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
363 radlog(L_ERR, "%s", librad_errstr);
367 RAD_SNMP_TYPE_INC(listener, total_requests);
369 if ((client = client_listener_find(listener,
370 &packet->src_ipaddr)) == NULL) {
371 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
373 radlog(L_ERR, "Ignoring request from unknown client %s port %d",
374 inet_ntop(packet->src_ipaddr.af,
375 &packet->src_ipaddr.ipaddr,
376 buffer, sizeof(buffer)),
383 * Some sanity checks, based on the packet code.
385 switch(packet->code) {
386 case PW_AUTHENTICATION_REQUEST:
387 RAD_SNMP_CLIENT_INC(listener, client, requests);
388 fun = rad_authenticate;
391 case PW_STATUS_SERVER:
392 if (!mainconfig.status_server) {
393 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
394 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
395 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
399 fun = rad_status_server;
403 RAD_SNMP_INC(rad_snmp.auth.total_unknown_types);
404 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
406 radlog(L_ERR, "Invalid packet code %d sent to authentication port from client %s port %d "
408 packet->code, client->shortname,
409 packet->src_port, packet->id);
413 } /* switch over packet types */
415 if (!received_request(listener, packet, prequest, client)) {
416 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
417 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
428 * Receive packets from an accounting socket
430 static int acct_socket_recv(rad_listen_t *listener,
431 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
433 RADIUS_PACKET *packet;
434 RAD_REQUEST_FUNP fun = NULL;
438 packet = rad_recv(listener->fd);
440 RAD_SNMP_TYPE_INC(listener, total_requests);
441 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
442 radlog(L_ERR, "%s", librad_errstr);
446 RAD_SNMP_TYPE_INC(listener, total_requests);
448 if ((client = client_listener_find(listener,
449 &packet->src_ipaddr)) == NULL) {
450 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
452 radlog(L_ERR, "Ignoring request from unknown client %s port %d",
453 inet_ntop(packet->src_ipaddr.af,
454 &packet->src_ipaddr.ipaddr,
455 buffer, sizeof(buffer)),
461 switch(packet->code) {
462 case PW_ACCOUNTING_REQUEST:
463 RAD_SNMP_CLIENT_INC(listener, client, requests);
464 fun = rad_accounting;
467 case PW_STATUS_SERVER:
468 if (!mainconfig.status_server) {
469 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
470 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
472 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
476 fun = rad_status_server;
481 * FIXME: Update MIB for packet types?
483 RAD_SNMP_TYPE_INC(listener, total_unknown_types);
484 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
486 radlog(L_ERR, "Invalid packet code %d sent to a accounting port "
487 "from client %s port %d - ID %d : IGNORED",
488 packet->code, client->shortname,
489 packet->src_port, packet->id);
495 * FIXME: Accounting duplicates should be handled
496 * differently than authentication duplicates.
498 if (!received_request(listener, packet, prequest, client)) {
499 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
500 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
511 * Recieve packets from a proxy socket.
513 static int proxy_socket_recv(rad_listen_t *listener,
514 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
517 RADIUS_PACKET *packet;
518 RAD_REQUEST_FUNP fun = NULL;
521 packet = rad_recv(listener->fd);
523 radlog(L_ERR, "%s", librad_errstr);
528 * FIXME: Client MIB updates?
530 switch(packet->code) {
531 case PW_AUTHENTICATION_ACK:
532 case PW_ACCESS_CHALLENGE:
533 case PW_AUTHENTICATION_REJECT:
534 fun = rad_authenticate;
537 case PW_ACCOUNTING_RESPONSE:
538 fun = rad_accounting;
543 * FIXME: Update MIB for packet types?
545 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
546 "from home server %s port %d - ID %d : IGNORED",
548 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
549 packet->src_port, packet->id);
554 request = received_proxy_response(packet);
559 rad_assert(fun != NULL);
567 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
569 if (!request->reply->code) return 0;
571 rad_encode(request->reply, request->packet, request->secret);
572 rad_sign(request->reply, request->packet, request->secret);
578 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
580 if (rad_verify(request->packet, NULL, request->secret) < 0) {
584 return rad_decode(request->packet, NULL, request->secret);
587 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
589 rad_encode(request->proxy, NULL, request->home_server->secret);
590 rad_sign(request->proxy, NULL, request->home_server->secret);
596 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
598 if (rad_verify(request->proxy_reply, request->proxy,
599 request->home_server->secret) < 0) {
603 return rad_decode(request->proxy_reply, request->proxy,
604 request->home_server->secret);
608 #define STATE_UNOPENED (0)
609 #define STATE_UNLOCKED (1)
610 #define STATE_HEADER (2)
611 #define STATE_READING (3)
612 #define STATE_DONE (4)
613 #define STATE_WAITING (5)
616 * If we're limiting outstanding packets, then mark the response
619 static int detail_send(rad_listen_t *listener, REQUEST *request)
621 listen_detail_t *data = listener->data;
623 rad_assert(request->listener == listener);
624 rad_assert(listener->send == detail_send);
626 if (request->simul_max >= 0) {
627 rad_assert(data->outstanding != NULL);
628 rad_assert(request->simul_max < data->max_outstanding);
630 data->outstanding[request->simul_max] = 0;
638 * Open the detail file..
640 * FIXME: create it, if it's not already there, so that the main
641 * server select() will wake us up if there's anything to read.
643 static int detail_open(rad_listen_t *this)
647 listen_detail_t *data = this->data;
649 rad_assert(data->state == STATE_UNOPENED);
650 snprintf(buffer, sizeof(buffer), "%s.work", data->detail);
653 * FIXME: Have "one-shot" configuration, where it
654 * will read the detail file, and exit once it's
657 * FIXME: Try harder to open the detail file.
658 * Maybe sleep for X usecs if it doesn't exist?
662 * Open detail.work first, so we don't lose
663 * accounting packets. It's probably better to
664 * duplicate them than to lose them.
666 * Note that we're not writing to the file, but
667 * we've got to open it for writing in order to
668 * establish the lock, to prevent rlm_detail from
671 this->fd = open(buffer, O_RDWR);
674 * Try reading the detail file. If it
675 * doesn't exist, we can't do anything.
677 * Doing the stat will tell us if the file
678 * exists, even if we don't have permissions
681 if (stat(data->detail, &st) < 0) {
686 * Open it BEFORE we rename it, just to
689 this->fd = open(data->detail, O_RDWR);
691 radlog(L_ERR, "Failed to open %s: %s",
692 data->detail, strerror(errno));
697 * Rename detail to detail.work
699 if (rename(data->detail, buffer) < 0) {
704 } /* else detail.work existed, and we opened it */
706 rad_assert(data->vps == NULL);
708 rad_assert(data->fp == NULL);
709 data->fp = fdopen(this->fd, "r");
711 radlog(L_ERR, "Failed to re-open %s: %s",
712 data->detail, strerror(errno));
716 data->state = STATE_UNLOCKED;
718 data->client_ip.af = AF_UNSPEC;
725 * This is a bad hack, just so complaints have meaningful text.
727 static const RADCLIENT detail_client = {
742 static int detail_recv(rad_listen_t *listener,
743 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
746 char key[256], value[1024];
747 VALUE_PAIR *vp, **tail;
748 RADIUS_PACKET *packet;
750 listen_detail_t *data = listener->data;
752 if (data->state == STATE_UNOPENED) {
753 rad_assert(listener->fd < 0);
754 if (!detail_open(listener)) return 0;
756 rad_assert(listener->fd >= 0);
759 * Try to lock fd. If we can't, return. If we can,
760 * continue. This means that the server doesn't block
761 * while waiting for the lock to open...
763 if (data->state == STATE_UNLOCKED) {
765 * Note that we do NOT block waiting for the
766 * lock. We've re-named the file above, so we've
767 * already guaranteed that any *new* detail
768 * writer will not be opening this file. The
769 * only purpose of the lock is to catch a race
770 * condition where the execution "ping-pongs"
771 * between radiusd & radrelay.
773 if (rad_lockfd_nonblock(listener->fd, 0) < 0) {
777 * Look for the header
779 data->state = STATE_HEADER;
783 * Catch an out of memory condition which will most likely
786 if (data->state == STATE_DONE) goto alloc_packet;
789 * If we're in another state, then it means that we read
790 * a partial packet, which is bad.
792 rad_assert(data->state == STATE_HEADER);
793 rad_assert(data->vps == NULL);
796 * We read the last packet, and returned it for
797 * processing. We later come back here to shut
798 * everything down, and unlink the file.
800 if (feof(data->fp)) {
801 rad_assert(data->state == STATE_HEADER);
804 * Don't unlink the file until we've received
805 * all of the responses.
807 if (data->max_outstanding > 0) {
810 for (i = 0; i < data->max_outstanding; i++) {
812 * FIXME: close the file?
814 if (data->outstanding[i]) {
815 data->state = STATE_WAITING;
822 rad_assert(data->vps == NULL);
824 snprintf(buffer, sizeof(buffer), "%s.work", data->detail);
826 fclose(data->fp); /* closes listener->fd */
829 data->state = STATE_UNOPENED;
832 * Try to open "detail" again. If we're on a
833 * busy RADIUS server, odds are that it will
836 detail_open(listener);
845 while (fgets(buffer, sizeof(buffer), data->fp)) {
849 if (!strchr(buffer, '\n')) {
850 pairfree(&data->vps);
855 * We've read a header, possibly packet contents,
856 * and are now at the end of the packet.
858 if ((data->state == STATE_READING) &&
859 (buffer[0] == '\n')) {
860 data->state = STATE_DONE;
865 * Look for date/time header, and read VP's if
866 * found. If not, keep reading lines until we
869 if (data->state == STATE_HEADER) {
872 if (sscanf(buffer, "%*s %*s %*d %*d:%*d:%*d %d", &y)) {
873 data->state = STATE_READING;
879 * We have a full "attribute = value" line.
880 * If it doesn't look reasonable, skip it.
882 if (sscanf(buffer, "%255s = %1023s", key, value) != 2) {
887 * Skip non-protocol attributes.
889 if (!strcasecmp(key, "Request-Authenticator")) continue;
892 * Set the original client IP address, based on
893 * what's in the detail file.
895 * Hmm... we don't set the server IP address.
898 if (!strcasecmp(key, "Client-IP-Address")) {
899 data->client_ip.af = AF_INET;
900 ip_hton(value, AF_INET, &data->client_ip);
905 * The original time at which we received the
906 * packet. We need this to properly calculate
909 if (!strcasecmp(key, "Timestamp")) {
910 data->timestamp = atoi(value);
917 * FIXME: do we want to check for non-protocol
918 * attributes like radsqlrelay does?
921 if ((userparse(buffer, &vp) > 0) &&
929 * We got to EOF, If we're in STATE_HEADER, it's OK.
930 * Otherwise it's a problem. In any case, nuke the file
931 * and start over from scratch,
933 if (feof(data->fp)) {
938 * FIXME: Do load management.
942 * If we're not done, then there's a problem. The checks
945 rad_assert(data->state == STATE_DONE);
948 * The packet we read was empty, re-set the state to look
949 * for a header, and don't return anything.
952 data->state = STATE_HEADER;
957 * Allocate the packet. If we fail, it's a serious
962 * If we keep track of the outstanding requests, do so
963 * here. Note that to minimize potential work, we do
964 * so only once the file is opened & locked.
966 if (data->max_outstanding) {
969 for (i = 0; i < data->max_outstanding; i++) {
970 if (!data->outstanding[i]) {
977 * All of the slots are full. Put the LF back,
978 * so select will say that data is ready, and set
979 * the state back to reading. Then, return so
980 * that we don't overload the server.
983 ungetc('\n', data->fp);
984 data->state = STATE_READING;
989 packet = rad_alloc(1);
991 return 0; /* maybe memory will magically free up... */
994 memset(packet, 0, sizeof(*packet));
996 packet->src_ipaddr.af = AF_INET;
997 packet->src_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
998 packet->code = PW_ACCOUNTING_REQUEST;
999 packet->timestamp = time(NULL);
1002 * Look for Acct-Delay-Time, and update
1003 * based on Acct-Delay-Time += (time(NULL) - timestamp)
1005 vp = pairfind(packet->vps, PW_ACCT_DELAY_TIME);
1007 vp = paircreate(PW_ACCT_DELAY_TIME, PW_TYPE_INTEGER);
1008 rad_assert(vp != NULL);
1010 if (data->timestamp != 0) {
1011 vp->lvalue += time(NULL) - data->timestamp;
1015 * Remember where it came from, so that we don't
1016 * proxy it to the place it came from...
1018 if (data->client_ip.af != AF_UNSPEC) {
1019 packet->src_ipaddr = data->client_ip;
1022 vp = pairfind(packet->vps, PW_PACKET_SRC_IP_ADDRESS);
1024 packet->src_ipaddr.af = AF_INET;
1025 packet->src_ipaddr.ipaddr.ip4addr.s_addr = vp->lvalue;
1027 vp = pairfind(packet->vps, PW_PACKET_SRC_IPV6_ADDRESS);
1029 packet->src_ipaddr.af = AF_INET6;
1030 memcpy(&packet->src_ipaddr.ipaddr.ip6addr,
1031 &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
1035 vp = pairfind(packet->vps, PW_PACKET_DST_IP_ADDRESS);
1037 packet->dst_ipaddr.af = AF_INET;
1038 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->lvalue;
1040 vp = pairfind(packet->vps, PW_PACKET_DST_IPV6_ADDRESS);
1042 packet->dst_ipaddr.af = AF_INET6;
1043 memcpy(&packet->dst_ipaddr.ipaddr.ip6addr,
1044 &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
1049 * We've got to give SOME value for Id & ports, so that
1050 * the packets can be added to the request queue.
1051 * However, we don't want to keep track of used/unused
1052 * id's and ports, as that's a lot of work. This hack
1053 * ensures that (if we have real random numbers), that
1054 * there will be a collision on every 2^(16+15+15+24 - 1)
1055 * packets, on average. That means we can read 2^37
1056 * packets before having a collision, which means it's
1057 * effectively impossible.
1059 packet->id = lrad_rand() & 0xffff;
1060 packet->src_port = 1024 + (lrad_rand() & 0x7fff);
1061 packet->dst_port = 1024 + (lrad_rand() & 0x7fff);
1063 packet->dst_ipaddr.af = AF_INET;
1064 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl((INADDR_LOOPBACK & ~0xffffff) | (lrad_rand() & 0xffffff));
1066 packet->vps = data->vps;
1072 data->state = STATE_HEADER;
1075 * Keep track of free slots, as a hack, in an otherwise
1078 (*prequest)->simul_max = free_slot;
1079 data->outstanding[free_slot] = 1;
1081 *pfun = rad_accounting;
1084 printf("detail_recv: Read packet from %s\n", data->detail);
1085 for (vp = packet->vps; vp; vp = vp->next) {
1087 vp_print(stdout, vp);
1093 * FIXME: many of these checks may not be necessary when
1094 * reading from the detail file.
1096 if (!received_request(listener, packet, prequest, &detail_client)) {
1106 * Free detail-specific stuff.
1108 static void detail_free(rad_listen_t *this)
1110 listen_detail_t *data = this->data;
1113 pairfree(&data->vps);
1114 free(data->outstanding);
1116 if (data->fp != NULL) fclose(data->fp);
1120 static int detail_print(rad_listen_t *this, char *buffer, size_t bufsize)
1122 return snprintf(buffer, bufsize, "%s",
1123 ((listen_detail_t *)(this->data))->detail);
1126 static int detail_encode(UNUSED rad_listen_t *this, UNUSED REQUEST *request)
1129 * We never encode responses "sent to" the detail file.
1134 static int detail_decode(UNUSED rad_listen_t *this, UNUSED REQUEST *request)
1137 * We never decode responses read from the detail file.
1143 static const CONF_PARSER detail_config[] = {
1144 { "detail", PW_TYPE_STRING_PTR,
1145 offsetof(listen_detail_t, detail), NULL, NULL },
1146 { "max_outstanding", PW_TYPE_INTEGER,
1147 offsetof(listen_detail_t, max_outstanding), NULL, "100" },
1149 { NULL, -1, 0, NULL, NULL } /* end the list */
1154 * Parse a detail section.
1156 static int detail_parse(const char *filename, int lineno,
1157 const CONF_SECTION *cs, rad_listen_t *this)
1160 listen_detail_t *data;
1164 rcode = cf_section_parse(cs, data, detail_config);
1166 radlog(L_ERR, "%s[%d]: Failed parsing listen section",
1171 if (!data->detail) {
1172 radlog(L_ERR, "%s[%d]: No detail file specified in listen section",
1179 data->state = STATE_UNOPENED;
1181 if (data->max_outstanding > 32768) data->max_outstanding = 32768;
1183 if (data->max_outstanding > 0) {
1184 data->outstanding = rad_malloc(sizeof(int) * data->max_outstanding);
1194 static int radius_snmp_recv(rad_listen_t *listener,
1195 UNUSED RAD_REQUEST_FUNP *pfun,
1196 UNUSED REQUEST **prequest)
1198 if (!mainconfig.do_snmp) return 0;
1200 if ((rad_snmp.smux_fd >= 0) &&
1201 (rad_snmp.smux_event == SMUX_READ)) {
1206 * If we've got to re-connect, then do so now,
1207 * before calling select again.
1209 if (rad_snmp.smux_event == SMUX_CONNECT) {
1214 * Reset this every time, as the smux connect may have
1215 * opened a new socket.
1217 listener->fd = rad_snmp.smux_fd;
1223 static int radius_snmp_print(rad_listen_t *this, char *buffer, size_t bufsize)
1225 return snprintf(buffer, bufsize, "SMUX with OID .1.3.6.1.4.1.3317.1.3.1");
1230 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1231 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1233 /* authentication */
1234 { common_socket_parse, NULL,
1235 auth_socket_recv, auth_socket_send,
1236 socket_print, client_socket_encode, client_socket_decode },
1239 { common_socket_parse, NULL,
1240 acct_socket_recv, acct_socket_send,
1241 socket_print, client_socket_encode, client_socket_decode},
1245 proxy_socket_recv, proxy_socket_send,
1246 socket_print, proxy_socket_encode, proxy_socket_decode },
1249 { detail_parse, detail_free,
1250 detail_recv, detail_send,
1251 detail_print, detail_encode, detail_decode },
1253 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_SNMP */
1259 * Binds a listener to a socket.
1261 static int listen_bind(rad_listen_t *this)
1263 rad_listen_t **last;
1264 listen_socket_t *sock = this->data;
1267 * If the port is zero, then it means the appropriate
1268 * thing from /etc/services.
1270 if (sock->port == 0) {
1271 struct servent *svp;
1273 switch (this->type) {
1274 case RAD_LISTEN_AUTH:
1275 svp = getservbyname ("radius", "udp");
1277 sock->port = ntohs(svp->s_port);
1279 sock->port = PW_AUTH_UDP_PORT;
1283 case RAD_LISTEN_ACCT:
1284 svp = getservbyname ("radacct", "udp");
1286 sock->port = ntohs(svp->s_port);
1288 sock->port = PW_ACCT_UDP_PORT;
1293 radlog(L_ERR|L_CONS, "ERROR: Non-fatal internal sanity check failed in bind.");
1299 * Find it in the old list, AFTER updating the port. If
1300 * it's there, use that, rather than creating a new
1301 * socket. This allows HUP's to re-use the old sockets,
1302 * which means that packets waiting in the socket queue
1305 for (last = &mainconfig.listen;
1307 last = &((*last)->next)) {
1308 if ((this->type == (*last)->type) &&
1309 (sock->port == ((listen_socket_t *)((*last)->data))->port) &&
1310 (sock->ipaddr.af == ((listen_socket_t *)((*last)->data))->ipaddr.af)) {
1313 if (sock->ipaddr.af == AF_INET) {
1314 equal = (sock->ipaddr.ipaddr.ip4addr.s_addr == ((listen_socket_t *)((*last)->data))->ipaddr.ipaddr.ip4addr.s_addr);
1315 } else if (sock->ipaddr.af == AF_INET6) {
1316 equal = IN6_ARE_ADDR_EQUAL(&(sock->ipaddr.ipaddr.ip6addr), &(((listen_socket_t *)((*last)->data))->ipaddr.ipaddr.ip6addr));
1322 this->fd = (*last)->fd;
1329 this->fd = lrad_socket(&sock->ipaddr, sock->port);
1331 radlog(L_ERR|L_CONS, "ERROR: Failed to open socket: %s",
1338 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
1339 radlog(L_ERR, "Failure in fcntl: %s)\n", strerror(errno));
1343 flags |= O_NONBLOCK;
1344 if( fcntl(this->fd, F_SETFL, flags) < 0) {
1345 radlog(L_ERR, "Failure in fcntl: %s)\n", strerror(errno));
1356 * Allocate & initialize a new listener.
1358 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
1362 this = rad_malloc(sizeof(*this));
1363 memset(this, 0, sizeof(*this));
1366 this->recv = master_listen[this->type].recv;
1367 this->send = master_listen[this->type].send;
1368 this->print = master_listen[this->type].print;
1369 this->encode = master_listen[this->type].encode;
1370 this->decode = master_listen[this->type].decode;
1373 case RAD_LISTEN_AUTH:
1374 case RAD_LISTEN_ACCT:
1375 case RAD_LISTEN_PROXY:
1376 this->data = rad_malloc(sizeof(listen_socket_t));
1377 memset(this->data, 0, sizeof(listen_socket_t));
1380 case RAD_LISTEN_DETAIL:
1381 this->data = rad_malloc(sizeof(listen_detail_t));
1382 memset(this->data, 0, sizeof(listen_detail_t));
1393 * Externally visible function for creating a new proxy LISTENER.
1395 * For now, don't take ipaddr or port.
1397 * Not thread-safe, but all calls to it are protected by the
1398 * proxy mutex in request_list.c
1400 rad_listen_t *proxy_new_listener()
1402 int last_proxy_port, port;
1403 rad_listen_t *this, *tmp, **last;
1404 listen_socket_t *sock, *old;
1406 this = listen_alloc(RAD_LISTEN_PROXY);
1409 * Find an existing proxy socket to copy.
1411 * FIXME: Make it per-realm, or per-home server!
1413 last_proxy_port = 0;
1415 last = &mainconfig.listen;
1416 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1417 if (tmp->type == RAD_LISTEN_PROXY) {
1419 if (sock->port > last_proxy_port) {
1420 last_proxy_port = sock->port + 1;
1422 if (!old) old = sock;
1425 last = &(tmp->next);
1428 if (!old) return NULL; /* This is a serious error. */
1431 * FIXME: find a new IP address to listen on?
1434 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1437 * Keep going until we find an unused port.
1439 for (port = last_proxy_port; port < 64000; port++) {
1441 if (listen_bind(this) == 0) {
1443 * Add the new listener to the list of
1455 static const LRAD_NAME_NUMBER listen_compare[] = {
1456 { "auth", RAD_LISTEN_AUTH },
1457 { "acct", RAD_LISTEN_ACCT },
1458 { "detail", RAD_LISTEN_DETAIL },
1464 * Generate a list of listeners. Takes an input list of
1465 * listeners, too, so we don't close sockets with waiting packets.
1467 int listen_init(const char *filename, rad_listen_t **head)
1471 rad_listen_t **last;
1473 lrad_ipaddr_t server_ipaddr;
1477 * We shouldn't be called with a pre-existing list.
1479 rad_assert(head && (*head == NULL));
1481 if (start_time != 0) start_time = time(NULL);
1484 server_ipaddr.af = AF_UNSPEC;
1487 * If the port is specified on the command-line,
1488 * it over-rides the configuration file.
1490 if (mainconfig.port >= 0) {
1491 auth_port = mainconfig.port;
1493 rcode = cf_item_parse(mainconfig.config, "port",
1494 PW_TYPE_INTEGER, &auth_port,
1495 Stringify(PW_AUTH_UDP_PORT));
1496 if (rcode < 0) return -1; /* error parsing it */
1499 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'.");
1503 * If the IP address was configured on the command-line,
1504 * use that as the "bind_address"
1506 if (mainconfig.myip.af != AF_UNSPEC) {
1507 memcpy(&server_ipaddr, &mainconfig.myip,
1508 sizeof(server_ipaddr));
1513 * Else look for bind_address and/or listen sections.
1515 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1516 rcode = cf_item_parse(mainconfig.config, "bind_address",
1518 &server_ipaddr.ipaddr.ip4addr, NULL);
1519 if (rcode < 0) return -1; /* error parsing it */
1521 if (rcode == 0) { /* successfully parsed IPv4 */
1522 listen_socket_t *sock;
1523 server_ipaddr.af = AF_INET;
1525 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'.");
1528 this = listen_alloc(RAD_LISTEN_AUTH);
1531 sock->ipaddr = server_ipaddr;
1532 sock->port = auth_port;
1534 if (listen_bind(this) < 0) {
1537 radlog(L_CONS|L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1540 auth_port = sock->port; /* may have been updated in listen_bind */
1542 last = &(this->next);
1545 * Open Accounting Socket.
1547 * If we haven't already gotten acct_port from
1548 * /etc/services, then make it auth_port + 1.
1550 this = listen_alloc(RAD_LISTEN_ACCT);
1554 * Create the accounting socket.
1556 * The accounting port is always the
1557 * authentication port + 1
1559 sock->ipaddr = server_ipaddr;
1560 sock->port = auth_port + 1;
1562 if (listen_bind(this) < 0) {
1565 radlog(L_CONS|L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1570 last = &(this->next);
1572 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1573 radlog(L_CONS|L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1579 * They specified an IP on the command-line, ignore
1580 * all listen sections.
1582 if (mainconfig.myip.af != AF_UNSPEC) goto do_proxy;
1585 * Walk through the "listen" sections, if they exist.
1587 for (cs = cf_subsection_find_next(mainconfig.config, NULL, "listen");
1589 cs = cf_subsection_find_next(mainconfig.config, cs, "listen")) {
1591 char *listen_type, *identity;
1592 int lineno = cf_section_lineno(cs);
1594 listen_type = identity = NULL;
1596 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1598 if (rcode < 0) return -1;
1602 radlog(L_ERR, "%s[%d]: No type specified in listen section",
1608 * See if there's an identity.
1610 rcode = cf_item_parse(cs, "identity", PW_TYPE_STRING_PTR,
1618 type = lrad_str2int(listen_compare, listen_type,
1621 if (type == RAD_LISTEN_NONE) {
1623 radlog(L_CONS|L_ERR, "%s[%d]: Invalid type in listen section.",
1629 * Set up cross-type data.
1631 this = listen_alloc(type);
1632 this->identity = identity;
1636 * Call per-type parser.
1638 if (master_listen[type].parse(filename, lineno,
1646 last = &(this->next);
1650 * If we're proxying requests, open the proxy FD.
1651 * Otherwise, don't do anything.
1654 if (mainconfig.proxy_requests == TRUE) {
1656 listen_socket_t *sock = NULL;
1659 * No sockets to receive packets, therefore
1660 * proxying is pointless.
1662 if (!*head) return -1;
1665 * If we previously had proxy sockets, copy them
1666 * to the new config.
1668 if (mainconfig.listen != NULL) {
1669 rad_listen_t *old, *next, **tail;
1671 tail = &mainconfig.listen;
1672 for (old = mainconfig.listen;
1677 if (old->type != RAD_LISTEN_PROXY) {
1678 tail = &((*tail)->next);
1685 last = &(old->next);
1692 * Find the first authentication port,
1695 for (this = *head; this != NULL; this = this->next) {
1696 if (this->type == RAD_LISTEN_AUTH) {
1698 if (server_ipaddr.af == AF_UNSPEC) {
1699 server_ipaddr = sock->ipaddr;
1701 port = sock->port + 2; /* skip acct port */
1706 if (port < 0) port = 1024 + (lrad_rand() & 0x1ff);
1709 * Address is still unspecified, use IPv4.
1711 if (server_ipaddr.af == AF_UNSPEC) {
1712 server_ipaddr.af = AF_INET;
1713 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1716 this = listen_alloc(RAD_LISTEN_PROXY);
1720 * Create the first proxy socket.
1722 sock->ipaddr = server_ipaddr;
1725 * Try to find a proxy port (value doesn't matter)
1727 for (sock->port = port;
1730 if (listen_bind(this) == 0) {
1732 last = &(this->next); /* just in case */
1737 if (sock->port >= 64000) {
1740 radlog(L_ERR|L_CONS, "Failed to open socket for proxying");
1747 if (mainconfig.do_snmp) {
1751 * Forget about the old one.
1753 for (this = mainconfig.listen;
1755 this = this->next) {
1756 if (this->type != RAD_LISTEN_SNMP) continue;
1760 this = rad_malloc(sizeof(*this));
1761 memset(this, 0, sizeof(*this));
1763 this->type = RAD_LISTEN_SNMP;
1764 this->fd = rad_snmp.smux_fd;
1766 this->recv = radius_snmp_recv;
1767 this->print = radius_snmp_print;
1770 last = &(this->next);
1775 * Sanity check the configuration.
1778 if (mainconfig.listen != NULL) for (this = *head;
1780 this = this->next) {
1781 if (((this->type == RAD_LISTEN_ACCT) &&
1782 (rcode == RAD_LISTEN_DETAIL)) ||
1783 ((this->type == RAD_LISTEN_DETAIL) &&
1784 (rcode == RAD_LISTEN_ACCT))) {
1785 rad_assert(0 == 1); /* FIXME: configuration error */
1788 if (rcode != 0) continue;
1790 if ((this->type == RAD_LISTEN_ACCT) ||
1791 (this->type == RAD_LISTEN_DETAIL)) {
1801 * Free a linked list of listeners;
1803 void listen_free(rad_listen_t **head)
1807 if (!head || !*head) return;
1811 rad_listen_t *next = this->next;
1813 free(this->identity);
1816 * Other code may have eaten the FD.
1818 if (this->fd >= 0) close(this->fd);
1820 if (master_listen[this->type].free) {
1821 master_listen[this->type].free(this);