2 * event.c Server event handling
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 2007 The FreeRADIUS server project
21 * Copyright 2007 Alan DeKok <aland@deployingradius.com>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29 #include <freeradius-devel/event.h>
30 #include <freeradius-devel/detail.h>
32 #include <freeradius-devel/rad_assert.h>
37 #ifdef HAVE_SYS_WAIT_H
38 # include <sys/wait.h>
41 #define USEC (1000000)
43 extern pid_t radius_pid;
45 extern int check_config;
46 extern void force_log_reopen(void);
47 extern char *debug_condition;
50 * Ridiculous amounts of local state.
52 static fr_event_list_t *el = NULL;
53 static fr_packet_list_t *pl = NULL;
54 static int request_num_counter = 0;
55 static struct timeval now;
57 static int have_children;
58 static int just_started = TRUE;
62 #define WITH_SELF_PIPE (1)
67 static int self_pipe[2];
72 static pthread_mutex_t proxy_mutex;
75 #define PTHREAD_MUTEX_LOCK if (have_children) pthread_mutex_lock
76 #define PTHREAD_MUTEX_UNLOCK if (have_children) pthread_mutex_unlock
78 static pthread_t NO_SUCH_CHILD_PID;
81 * This is easier than ifdef's throughout the code.
83 #define PTHREAD_MUTEX_LOCK(_x)
84 #define PTHREAD_MUTEX_UNLOCK(_x)
85 int thread_pool_addrequest(REQUEST *request, RAD_REQUEST_FUNP fun)
87 radius_handle_request(request, fun);
92 #define INSERT_EVENT(_function, _ctx) if (!fr_event_insert(el, _function, _ctx, &((_ctx)->when), &((_ctx)->ev))) { _rad_panic(__FILE__, __LINE__, "Failed to insert event"); }
95 static fr_packet_list_t *proxy_list = NULL;
98 * We keep the proxy FD's here. The RADIUS Id's are marked
99 * "allocated" per Id, via a bit per proxy FD.
101 static int proxy_fds[32];
102 static rad_listen_t *proxy_listeners[32];
104 #define remove_from_proxy_hash(foo)
107 static void request_post_handler(REQUEST *request);
108 static void wait_a_bit(void *ctx);
109 static void event_socket_handler(fr_event_list_t *xel, UNUSED int fd, void *ctx);
111 static void event_poll_detail(void *ctx);
114 static void NEVER_RETURNS _rad_panic(const char *file, unsigned int line,
117 radlog(L_ERR, "[%s:%d] %s", file, line, msg);
121 #define rad_panic(x) _rad_panic(__FILE__, __LINE__, x)
124 static void tv_add(struct timeval *tv, int usec_delay)
126 if (usec_delay > USEC) {
127 tv->tv_sec += usec_delay / USEC;
130 tv->tv_usec += usec_delay;
132 if (tv->tv_usec > USEC) {
138 static void remove_from_request_hash(REQUEST *request)
140 if (!request->in_request_hash) return;
142 fr_packet_list_yank(pl, request->packet);
143 request->in_request_hash = FALSE;
145 request_stats_final(request);
150 static REQUEST *lookup_in_proxy_hash(RADIUS_PACKET *reply)
152 RADIUS_PACKET **proxy_p;
155 PTHREAD_MUTEX_LOCK(&proxy_mutex);
156 proxy_p = fr_packet_list_find_byreply(proxy_list, reply);
159 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
163 request = fr_packet2myptr(REQUEST, proxy, proxy_p);
166 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
170 request->num_proxied_responses++;
173 * Catch the most common case of everything working
176 if (request->num_proxied_requests == request->num_proxied_responses) {
177 fr_packet_list_yank(proxy_list, request->proxy);
178 fr_packet_list_id_free(proxy_list, request->proxy);
179 request->in_proxy_hash = FALSE;
183 * On the FIRST reply, decrement the count of outstanding
184 * requests. Note that this is NOT the count of sent
185 * packets, but whether or not the home server has
188 if (!request->proxy_reply &&
189 request->home_server &&
190 request->home_server->currently_outstanding) {
191 request->home_server->currently_outstanding--;
194 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
200 static void remove_from_proxy_hash(REQUEST *request)
202 if (!request->in_proxy_hash) return;
204 PTHREAD_MUTEX_LOCK(&proxy_mutex);
205 fr_packet_list_yank(proxy_list, request->proxy);
206 fr_packet_list_id_free(proxy_list, request->proxy);
209 * The home server hasn't replied, but we've given up on
210 * this request. Don't count this request against the
213 if (!request->proxy_reply &&
214 request->home_server &&
215 request->home_server->currently_outstanding) {
216 request->home_server->currently_outstanding--;
219 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
221 request->in_proxy_hash = FALSE;
224 static int proxy_id_alloc(REQUEST *request, RADIUS_PACKET *packet)
227 rad_listen_t *proxy_listener;
229 if (fr_packet_list_id_alloc(proxy_list, packet)) return 1;
232 * Allocate a new proxy fd. This function adds
233 * it to the tail of the list of listeners. With
234 * some care, this can be thread-safe.
236 proxy_listener = proxy_new_listener();
237 if (!proxy_listener) {
238 RDEBUG2("ERROR: Failed to create a new socket for proxying requests.");
246 proxy = proxy_listener->fd;
247 for (i = 0; i < 32; i++) {
249 * Found a free entry. Save the socket,
250 * and remember where we saved it.
252 if (proxy_fds[(proxy + i) & 0x1f] == -1) {
253 found = (proxy + i) & 0x1f;
254 proxy_fds[found] = proxy;
255 proxy_listeners[found] = proxy_listener;
259 rad_assert(found >= 0);
261 if (!fr_packet_list_socket_add(proxy_list, proxy_listener->fd)) {
262 RDEBUG2("ERROR: Failed to create a new socket for proxying requests.");
267 if (!fr_packet_list_id_alloc(proxy_list, packet)) {
268 RDEBUG2("ERROR: Failed to create a new socket for proxying requests.");
273 * Signal the main thread to add the new FD to the list
276 radius_signal_self(RADIUS_SIGNAL_SELF_NEW_FD);
281 static int insert_into_proxy_hash(REQUEST *request, int retransmit)
286 rad_assert(request->proxy != NULL);
287 rad_assert(proxy_list != NULL);
289 PTHREAD_MUTEX_LOCK(&proxy_mutex);
292 * Keep track of maximum outstanding requests to a
293 * particular home server. 'max_outstanding' is
294 * enforced in home_server_ldb(), in realms.c.
296 if (request->home_server) {
297 request->home_server->currently_outstanding++;
298 request->home_server->stats.total_requests++;
302 RADIUS_PACKET packet;
304 packet = *request->proxy;
306 if (!proxy_id_alloc(request, &packet)) {
307 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
312 * Yank the request, free the old Id, and
313 * remember the new Id.
315 fr_packet_list_yank(proxy_list, request->proxy);
316 fr_packet_list_id_free(proxy_list, request->proxy);
317 *request->proxy = packet;
319 } else if (!proxy_id_alloc(request, request->proxy)) {
320 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
324 rad_assert(request->proxy->sockfd >= 0);
327 * FIXME: Hack until we get rid of rad_listen_t, and put
328 * the information into the packet_list.
331 for (i = 0; i < 32; i++) {
332 if (proxy_fds[i] == request->proxy->sockfd) {
339 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
340 RDEBUG2("ERROR: All sockets are full.");
344 rad_assert(proxy_fds[proxy] != -1);
345 rad_assert(proxy_listeners[proxy] != NULL);
346 request->proxy_listener = proxy_listeners[proxy];
348 if (!fr_packet_list_insert(proxy_list, &request->proxy)) {
349 fr_packet_list_id_free(proxy_list, request->proxy);
350 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
351 RDEBUG2("ERROR: Failed to insert entry into proxy list");
355 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
357 RDEBUG3(" proxy: allocating destination %s port %d - Id %d",
358 inet_ntop(request->proxy->dst_ipaddr.af,
359 &request->proxy->dst_ipaddr.ipaddr, buf, sizeof(buf)),
360 request->proxy->dst_port,
363 request->in_proxy_hash = TRUE;
370 * Called as BOTH an event, and in-line from other functions.
372 static void wait_for_proxy_id_to_expire(void *ctx)
374 REQUEST *request = ctx;
376 rad_assert(request->magic == REQUEST_MAGIC);
377 rad_assert(request->proxy != NULL);
379 if (!fr_event_now(el, &now)) gettimeofday(&now, NULL);
380 request->when = request->proxy_when;
383 if ((request->proxy->code == PW_COA_REQUEST) ||
384 (request->proxy->code == PW_DISCONNECT_REQUEST)) {
385 request->when.tv_sec += request->home_server->coa_mrd;
388 request->when.tv_sec += request->home_server->response_window;
390 if ((request->num_proxied_requests == request->num_proxied_responses) ||
391 timercmp(&now, &request->when, >)) {
392 if (request->packet) {
393 RDEBUG2("Cleaning up request %d ID %d with timestamp +%d",
394 request->number, request->packet->id,
395 (unsigned int) (request->timestamp - fr_start_time));
397 RDEBUG2("Cleaning up request %d with timestamp +%d",
399 (unsigned int) (request->timestamp - fr_start_time));
402 fr_event_delete(el, &request->ev);
403 remove_from_proxy_hash(request);
404 remove_from_request_hash(request);
405 request_free(&request);
409 INSERT_EVENT(wait_for_proxy_id_to_expire, request);
413 #ifdef HAVE_PTHREAD_H
414 static void wait_for_child_to_die(void *ctx)
416 REQUEST *request = ctx;
418 rad_assert(request->magic == REQUEST_MAGIC);
420 if ((request->child_state == REQUEST_QUEUED) |
421 (request->child_state == REQUEST_RUNNING)) {
422 request->delay += (request->delay >> 1);
423 tv_add(&request->when, request->delay);
425 RDEBUG2("Child is still stuck for request %d", request->number);
427 INSERT_EVENT(wait_for_child_to_die, request);
431 RDEBUG2("Child is finally responsive for request %d", request->number);
432 remove_from_request_hash(request);
435 if (request->proxy) {
436 wait_for_proxy_id_to_expire(request);
441 request_free(&request);
445 static void cleanup_delay(void *ctx)
447 REQUEST *request = ctx;
449 rad_assert(request->magic == REQUEST_MAGIC);
450 rad_assert((request->child_state == REQUEST_CLEANUP_DELAY) ||
451 (request->child_state == REQUEST_DONE));
453 remove_from_request_hash(request);
456 if (request->proxy && request->in_proxy_hash) {
457 wait_for_proxy_id_to_expire(request);
462 RDEBUG2("Cleaning up request %d ID %d with timestamp +%d",
463 request->number, request->packet->id,
464 (unsigned int) (request->timestamp - fr_start_time));
466 fr_event_delete(el, &request->ev);
467 request_free(&request);
472 * FIXME: Put into a libradius function.
474 #define MAX_PACKET_CODE (52)
475 static const char *packet_codes[] = {
480 "Accounting-Request",
481 "Accounting-Response",
486 "Accounting-Message",
497 "Resource-Free-Request",
498 "Resource-Free-Response",
499 "Resource-Query-Request",
500 "Resource-Query-Response",
501 "Alternate-Resource-Reclaim-Request",
502 "NAS-Reboot-Request",
503 "NAS-Reboot-Response",
516 "Disconnect-Request",
526 "IP-Address-Allocate",
532 * In daemon mode, AND this request has debug flags set.
534 #define DEBUG_PACKET if (!debug_flag && request->options && request->radlog) debug_packet
536 static void debug_packet(REQUEST *request, RADIUS_PACKET *packet, int direction)
540 const char *received, *from;
541 const fr_ipaddr_t *ip;
546 rad_assert(debug_flag != 0);
547 rad_assert(request->radlog != NULL);
549 if (direction == 0) {
550 received = "Received";
551 from = "from"; /* what else? */
552 ip = &packet->src_ipaddr;
553 port = packet->src_port;
556 received = "Sending";
557 from = "to"; /* hah! */
558 ip = &packet->dst_ipaddr;
559 port = packet->dst_port;
563 * Client-specific debugging re-prints the input
564 * packet into the client log.
566 * This really belongs in a utility library
568 if ((packet->code > 0) && (packet->code < MAX_PACKET_CODE)) {
569 RDEBUG("%s %s packet %s host %s port %d, id=%d, length=%d",
570 received, packet_codes[packet->code], from,
571 inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
572 port, packet->id, packet->data_len);
574 RDEBUG("%s packet %s host %s port %d code=%d, id=%d, length=%d",
576 inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
578 packet->code, packet->id, packet->data_len);
581 for (vp = packet->vps; vp != NULL; vp = vp->next) {
582 vp_prints(buffer, sizeof(buffer), vp);
583 request->radlog(L_DBG, 0, request, "\t%s", buffer);
587 static void reject_delay(void *ctx)
589 REQUEST *request = ctx;
591 rad_assert(request->magic == REQUEST_MAGIC);
592 rad_assert(request->child_state == REQUEST_REJECT_DELAY);
594 RDEBUG2("Sending delayed reject for request %d", request->number);
596 DEBUG_PACKET(request, request->reply, 1);
598 request->listener->send(request->listener, request);
600 request->when.tv_sec += request->root->cleanup_delay;
601 request->child_state = REQUEST_CLEANUP_DELAY;
603 INSERT_EVENT(cleanup_delay, request);
608 void revive_home_server(void *ctx)
610 home_server *home = ctx;
613 home->state = HOME_STATE_ALIVE;
614 home->currently_outstanding = 0;
615 home->revive_time = now;
618 * Delete any outstanding events.
620 if (home->ev) fr_event_delete(el, &home->ev);
622 radlog(L_INFO, "PROXY: Marking home server %s port %d alive again... we have no idea if it really is alive or not.",
623 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
624 buffer, sizeof(buffer)),
630 static void no_response_to_ping(void *ctx)
632 REQUEST *request = ctx;
636 rad_assert(request->home_server != NULL);
638 home = request->home_server;
639 home->num_received_pings = 0;
641 RDEBUG2("No response to status check %d from home server %s port %d",
643 inet_ntop(request->proxy->dst_ipaddr.af,
644 &request->proxy->dst_ipaddr.ipaddr,
645 buffer, sizeof(buffer)),
646 request->proxy->dst_port);
648 wait_for_proxy_id_to_expire(request);
652 static void received_response_to_ping(REQUEST *request)
657 rad_assert(request->home_server != NULL);
659 if (rad_verify(request->proxy_reply, request->proxy,
660 request->home_server->secret) != 0) {
661 DEBUG("Ignoring spoofed proxy reply. Signature is invalid");
665 home = request->home_server;
666 home->num_received_pings++;
668 RDEBUG2("Received response to status check %d (%d in current sequence)",
669 request->number, home->num_received_pings);
672 * Remove the request from any hashes
674 fr_event_delete(el, &request->ev);
675 remove_from_proxy_hash(request);
676 rad_assert(request->in_request_hash == FALSE);
679 * The control socket may have marked the home server as
680 * alive. OR, it may have suddenly started responding to
681 * requests again. If so, don't re-do the "make alive"
684 if (home->state == HOME_STATE_ALIVE) return;
687 * We haven't received enough ping responses to mark it
688 * "alive". Wait a bit.
690 if (home->num_received_pings < home->num_pings_to_alive) {
694 home->state = HOME_STATE_ALIVE;
695 home->currently_outstanding = 0;
696 home->revive_time = now;
698 if (!fr_event_delete(el, &home->ev)) {
699 RDEBUG2("Hmm... no event for home server. Oh well.");
702 radlog(L_INFO, "PROXY: Marking home server %s port %d alive",
703 inet_ntop(request->proxy->dst_ipaddr.af,
704 &request->proxy->dst_ipaddr.ipaddr,
705 buffer, sizeof(buffer)),
706 request->proxy->dst_port);
711 * Called from start of zombie period, OR after control socket
712 * marks the home server dead.
714 static void ping_home_server(void *ctx)
717 home_server *home = ctx;
721 if (home->state == HOME_STATE_ALIVE) {
722 radlog(L_INFO, "Suspicious proxy state... continuing");
726 request = request_alloc();
727 request->number = request_num_counter++;
729 request->proxy = rad_alloc(1);
730 rad_assert(request->proxy != NULL);
732 fr_event_now(el, &request->when);
733 home->when = request->when;
735 if (home->ping_check == HOME_PING_CHECK_STATUS_SERVER) {
736 request->proxy->code = PW_STATUS_SERVER;
738 radius_pairmake(request, &request->proxy->vps,
739 "Message-Authenticator", "0x00", T_OP_SET);
741 } else if (home->type == HOME_TYPE_AUTH) {
742 request->proxy->code = PW_AUTHENTICATION_REQUEST;
744 radius_pairmake(request, &request->proxy->vps,
745 "User-Name", home->ping_user_name, T_OP_SET);
746 radius_pairmake(request, &request->proxy->vps,
747 "User-Password", home->ping_user_password, T_OP_SET);
748 radius_pairmake(request, &request->proxy->vps,
749 "Service-Type", "Authenticate-Only", T_OP_SET);
750 radius_pairmake(request, &request->proxy->vps,
751 "Message-Authenticator", "0x00", T_OP_SET);
754 #ifdef WITH_ACCOUNTING
755 request->proxy->code = PW_ACCOUNTING_REQUEST;
757 radius_pairmake(request, &request->proxy->vps,
758 "User-Name", home->ping_user_name, T_OP_SET);
759 radius_pairmake(request, &request->proxy->vps,
760 "Acct-Status-Type", "Stop", T_OP_SET);
761 radius_pairmake(request, &request->proxy->vps,
762 "Acct-Session-Id", "00000000", T_OP_SET);
763 vp = radius_pairmake(request, &request->proxy->vps,
764 "Event-Timestamp", "0", T_OP_SET);
765 vp->vp_date = now.tv_sec;
767 rad_assert("Internal sanity check failed");
771 radius_pairmake(request, &request->proxy->vps,
772 "NAS-Identifier", "Status Check. Are you alive?",
775 request->proxy->dst_ipaddr = home->ipaddr;
776 request->proxy->dst_port = home->port;
777 request->home_server = home;
779 rad_assert(request->proxy_listener == NULL);
781 if (!insert_into_proxy_hash(request, FALSE)) {
782 RDEBUG2("ERROR: Failed inserting status check %d into proxy hash. Discarding it.",
784 request_free(&request);
787 rad_assert(request->proxy_listener != NULL);
788 request->proxy_listener->send(request->proxy_listener,
791 request->next_callback = NULL;
792 request->child_state = REQUEST_PROXIED;
793 request->when.tv_sec += home->ping_timeout;;
795 INSERT_EVENT(no_response_to_ping, request);
798 * Add +/- 2s of jitter, as suggested in RFC 3539
799 * and in the Issues and Fixes draft.
801 home->when.tv_sec += home->ping_interval - 2;
804 jitter ^= (jitter >> 10);
805 jitter &= ((1 << 23) - 1); /* 22 bits of 1 */
807 tv_add(&home->when, jitter);
809 INSERT_EVENT(ping_home_server, home);
813 void mark_home_server_dead(home_server *home, struct timeval *when)
815 int previous_state = home->state;
818 radlog(L_INFO, "PROXY: Marking home server %s port %d as dead.",
819 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
820 buffer, sizeof(buffer)),
823 home->state = HOME_STATE_IS_DEAD;
824 home->num_received_pings = 0;
826 if (home->ping_check != HOME_PING_CHECK_NONE) {
828 * If the control socket marks us dead, start
829 * pinging. Otherwise, we already started
830 * pinging when it was marked "zombie".
832 if (previous_state == HOME_STATE_ALIVE) {
833 ping_home_server(home);
838 * Revive it after a fixed period of time. This
839 * is very, very, bad.
842 home->when.tv_sec += home->revive_interval;
844 INSERT_EVENT(revive_home_server, home);
848 static void check_for_zombie_home_server(REQUEST *request)
853 home = request->home_server;
855 if (home->state != HOME_STATE_ZOMBIE) return;
857 when = home->zombie_period_start;
858 when.tv_sec += home->zombie_period;
860 fr_event_now(el, &now);
861 if (timercmp(&now, &when, <)) {
865 mark_home_server_dead(home, &request->when);
868 static int proxy_to_virtual_server(REQUEST *request);
870 static int virtual_server_handler(UNUSED REQUEST *request)
872 proxy_to_virtual_server(request);
876 static void proxy_fallback_handler(REQUEST *request)
879 * A proper time is required for wait_a_bit.
881 request->delay = USEC / 10;
882 gettimeofday(&now, NULL);
883 request->next_when = now;
884 tv_add(&request->next_when, request->delay);
885 request->next_callback = wait_a_bit;
888 * Re-queue the request.
890 request->child_state = REQUEST_QUEUED;
892 rad_assert(request->proxy != NULL);
893 if (!thread_pool_addrequest(request, virtual_server_handler)) {
894 request->child_state = REQUEST_DONE;
897 #ifdef HAVE_PTHREAD_H
899 * MAY free the request if we're over max_request_time,
900 * AND we're not in threaded mode!
902 * Note that we call this ONLY if we're threaded, as
903 * if we're NOT threaded, request_post_handler() calls
904 * wait_a_bit(), which means that "request" may not
907 if (have_children) wait_a_bit(request);
912 static int setup_post_proxy_fail(REQUEST *request)
914 DICT_VALUE *dval = NULL;
917 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
918 dval = dict_valbyname(PW_POST_PROXY_TYPE, "Fail-Authentication");
920 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
921 dval = dict_valbyname(PW_POST_PROXY_TYPE, "Fail-Accounting");
925 * See no_response_to_coa_request
927 } else if (((request->packet->code >> 8) & 0xff) == PW_COA_REQUEST) {
928 request->packet->code &= 0xff; /* restore it */
930 if (request->proxy->code == PW_COA_REQUEST) {
931 dval = dict_valbyname(PW_POST_PROXY_TYPE, "Fail-CoA");
933 } else if (request->proxy->code == PW_DISCONNECT_REQUEST) {
934 dval = dict_valbyname(PW_POST_PROXY_TYPE, "Fail-Disconnect");
944 if (!dval) dval = dict_valbyname(PW_POST_PROXY_TYPE, "Fail");
947 pairdelete(&request->config_items, PW_POST_PROXY_TYPE);
951 vp = pairfind(request->config_items, PW_POST_PROXY_TYPE);
952 if (!vp) vp = radius_paircreate(request, &request->config_items,
953 PW_POST_PROXY_TYPE, PW_TYPE_INTEGER);
954 vp->vp_integer = dval->value;
956 rad_assert(request->proxy_reply == NULL);
962 static int null_handler(UNUSED REQUEST *request)
967 static void post_proxy_fail_handler(REQUEST *request)
970 * A proper time is required for wait_a_bit.
972 request->delay = USEC / 10;
973 gettimeofday(&now, NULL);
976 * Not set up to run Post-Proxy-Type = Fail.
978 * Mark the request as still running, and figure out what
981 if (!setup_post_proxy_fail(request)) {
982 request->child_state = REQUEST_RUNNING;
983 request_post_handler(request);
987 * Re-queue the request.
989 request->child_state = REQUEST_QUEUED;
992 * There is a post-proxy-type of fail. We run
993 * the request through the pre/post proxy
994 * handlers, just like it was a real proxied
995 * request. However, we set the per-request
996 * handler to NULL, as we don't want to do
999 * Note that when we're not threaded, this will
1000 * process the request even if it's greater than
1001 * max_request_time. That's not fatal.
1003 request->priority = 0;
1004 rad_assert(request->proxy != NULL);
1005 thread_pool_addrequest(request, null_handler);
1009 * MAY free the request if we're over max_request_time,
1010 * AND we're not in threaded mode!
1012 * Note that we call this ONLY if we're threaded, as
1013 * if we're NOT threaded, request_post_handler() calls
1014 * wait_a_bit(), which means that "request" may not
1017 if (have_children) wait_a_bit(request);
1021 /* maybe check this against wait_for_proxy_id_to_expire? */
1022 static void no_response_to_proxied_request(void *ctx)
1024 REQUEST *request = ctx;
1028 rad_assert(request->magic == REQUEST_MAGIC);
1029 rad_assert(request->child_state == REQUEST_PROXIED);
1032 * If we've failed over to an internal home server,
1033 * replace the callback with the correct one. This
1034 * is due to locking issues with child threads...
1036 if (request->home_server->server) {
1037 wait_a_bit(request);
1041 radlog(L_ERR, "Rejecting request %d due to lack of any response from home server %s port %d",
1043 inet_ntop(request->proxy->dst_ipaddr.af,
1044 &request->proxy->dst_ipaddr.ipaddr,
1045 buffer, sizeof(buffer)),
1046 request->proxy->dst_port);
1048 check_for_zombie_home_server(request);
1050 home = request->home_server;
1052 post_proxy_fail_handler(request);
1055 * Don't touch request due to race conditions
1057 if (home->state == HOME_STATE_IS_DEAD) {
1058 rad_assert(home->ev != NULL); /* or it will never wake up */
1063 * Enable the zombie period when we notice that the home
1064 * server hasn't responded. We do NOT back-date the start
1065 * of the zombie period.
1067 if (home->state == HOME_STATE_ALIVE) {
1068 radlog(L_ERR, "PROXY: Marking home server %s port %d as zombie (it looks like it is dead).",
1069 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
1070 buffer, sizeof(buffer)),
1072 home->state = HOME_STATE_ZOMBIE;
1073 home->zombie_period_start = now;
1076 * Start pinging the home server.
1078 ping_home_server(home);
1083 static void wait_a_bit(void *ctx)
1085 struct timeval when;
1086 REQUEST *request = ctx;
1087 fr_event_callback_t callback = NULL;
1089 rad_assert(request->magic == REQUEST_MAGIC);
1093 * The CoA request is a new (internally generated)
1094 * request, created in a child thread. We therefore need
1095 * some way to tie its events back into the main event
1098 if (request->coa && !request->coa->proxy_reply &&
1099 request->coa->next_callback) {
1100 request->coa->when = request->coa->next_when;
1101 INSERT_EVENT(request->coa->next_callback, request->coa);
1102 request->coa->next_callback = NULL;
1103 request->coa->parent = NULL;
1104 request->coa = NULL;
1108 switch (request->child_state) {
1109 case REQUEST_QUEUED:
1110 case REQUEST_RUNNING:
1111 when = request->received;
1112 when.tv_sec += request->root->max_request_time;
1115 * Normally called from the event loop with the
1116 * proper event loop time. Otherwise, called from
1117 * post proxy fail handler, which sets "now", and
1118 * this call won't re-set it, because we're not
1119 * in the event loop.
1121 fr_event_now(el, &now);
1124 * Request still has more time. Continue
1127 if (timercmp(&now, &when, <) ||
1128 ((request->listener->type == RAD_LISTEN_DETAIL) &&
1129 (request->child_state == REQUEST_QUEUED))) {
1130 if (request->delay < (USEC / 10)) {
1131 request->delay = USEC / 10;
1133 request->delay += request->delay >> 1;
1137 * Cap wait at some sane value for detail
1140 if ((request->listener->type == RAD_LISTEN_DETAIL) &&
1141 (request->delay > (request->root->max_request_time * USEC))) {
1142 request->delay = request->root->max_request_time * USEC;
1146 request->when = now;
1147 tv_add(&request->when, request->delay);
1148 callback = wait_a_bit;
1152 #if defined(HAVE_PTHREAD_H)
1154 * A child thread MAY still be running on the
1155 * request. Ask the thread to stop working on
1158 if (have_children) {
1159 /* FIXME: kill unresponsive children? */
1162 * Print this error message ONLY if
1163 * there's a child currently processing
1164 * the request. As we don't have thread
1165 * locks here, there may be race
1166 * conditions on this check. But it's
1167 * just an error message, so that's OK.
1169 if (!pthread_equal(request->child_pid, NO_SUCH_CHILD_PID)) {
1170 radlog(L_ERR, "WARNING: Unresponsive child for request %d, in module %s component %s",
1172 request->module ? request->module : "<server core>",
1173 request->component ? request->component : "<server core>");
1176 request->master_state = REQUEST_STOP_PROCESSING;
1178 request->delay = USEC / 4;
1179 tv_add(&request->when, request->delay);
1180 callback = wait_for_child_to_die;
1186 * Else there are no child threads. We probably
1187 * should have just marked the request as 'done'
1188 * elsewhere, like in the post-proxy-fail
1189 * handler. But doing that would involve
1190 * checking for max_request_time in multiple
1191 * places, so this may be simplest.
1193 request->child_state = REQUEST_DONE;
1197 * Mark the request as no longer running,
1201 #ifdef HAVE_PTHREAD_H
1202 request->child_pid = NO_SUCH_CHILD_PID;
1207 * This is a CoA request. It's been divorced
1208 * from everything else, so we clean it up now.
1210 if (!request->in_request_hash &&
1212 (request->packet->code != request->proxy->code) &&
1213 ((request->proxy->code == PW_COA_REQUEST) ||
1214 (request->proxy->code == PW_DISCONNECT_REQUEST))) {
1216 * FIXME: Do CoA MIBs
1218 fr_event_delete(el, &request->ev);
1219 remove_from_proxy_hash(request);
1220 request_free(&request);
1224 request_stats_final(request);
1225 cleanup_delay(request);
1228 case REQUEST_REJECT_DELAY:
1229 case REQUEST_CLEANUP_DELAY:
1230 #ifdef HAVE_PTHREAD_H
1231 request->child_pid = NO_SUCH_CHILD_PID;
1233 request_stats_final(request);
1235 case REQUEST_PROXIED:
1236 rad_assert(request->next_callback != NULL);
1237 rad_assert(request->next_callback != wait_a_bit);
1239 request->when = request->next_when;
1240 callback = request->next_callback;
1241 request->next_callback = NULL;
1245 rad_panic("Internal sanity check failure");
1250 * Something major went wrong. Discard the request, and
1253 * FIXME: No idea why this happens or how to fix it...
1254 * It seems to happen *only* when requests are proxied,
1255 * and where the home server doesn't respond. So it looks
1256 * like a race condition above, but it happens in debug
1257 * mode, with no threads...
1260 RDEBUG("WARNING: Internal sanity check failed in event handler for request %d: Discarding the request!", request->number);
1261 fr_event_delete(el, &request->ev);
1262 remove_from_proxy_hash(request);
1263 remove_from_request_hash(request);
1264 request_free(&request);
1268 INSERT_EVENT(callback, request);
1272 static void no_response_to_coa_request(void *ctx)
1274 REQUEST *request = ctx;
1277 rad_assert(request->magic == REQUEST_MAGIC);
1278 rad_assert(request->child_state == REQUEST_PROXIED);
1279 rad_assert(request->home_server != NULL);
1280 rad_assert(!request->in_request_hash);
1282 radlog(L_ERR, "No response to CoA request sent to %s",
1283 inet_ntop(request->proxy->dst_ipaddr.af,
1284 &request->proxy->dst_ipaddr.ipaddr,
1285 buffer, sizeof(buffer)));
1290 request->packet->code |= (PW_COA_REQUEST << 8);
1291 post_proxy_fail_handler(request);
1295 static int update_event_timestamp(RADIUS_PACKET *packet, time_t when)
1299 vp = pairfind(packet->vps, PW_EVENT_TIMESTAMP);
1306 packet->data = NULL;
1307 packet->data_len = 0;
1310 return 1; /* time stamp updated */
1315 * Called when we haven't received a response to a CoA request.
1317 static void retransmit_coa_request(void *ctx)
1321 REQUEST *request = ctx;
1323 rad_assert(request->magic == REQUEST_MAGIC);
1324 rad_assert(request->child_state == REQUEST_PROXIED);
1325 rad_assert(request->home_server != NULL);
1326 rad_assert(!request->in_request_hash);
1327 rad_assert(request->parent == NULL);
1329 fr_event_now(el, &now);
1332 * Cap count at MRC, if it is non-zero.
1334 if (request->home_server->coa_mrc &&
1335 (request->num_coa_requests >= request->home_server->coa_mrc)) {
1336 no_response_to_coa_request(request);
1341 * RFC 5080 Section 2.2.1
1343 * RT = 2*RTprev + RAND*RTprev
1344 * = 1.9 * RTprev + rand(0,.2) * RTprev
1345 * = 1.9 * RTprev + rand(0,1) * (RTprev / 5)
1348 delay ^= (delay >> 16);
1350 frac = request->delay / 5;
1351 delay = ((frac >> 16) * delay) + (((frac & 0xffff) * delay) >> 16);
1353 delay += (2 * request->delay) - (request->delay / 10);
1356 * Cap delay at MRT, if MRT is non-zero.
1358 if (request->home_server->coa_mrt &&
1359 (delay > (request->home_server->coa_mrt * USEC))) {
1360 int mrt_usec = request->home_server->coa_mrt * USEC;
1363 * delay = MRT + RAND * MRT
1364 * = 0.9 MRT + rand(0,.2) * MRT
1367 delay ^= (delay >> 15);
1369 delay = ((mrt_usec >> 16) * delay) + (((mrt_usec & 0xffff) * delay) >> 16);
1370 delay += mrt_usec - (mrt_usec / 10);
1373 request->delay = delay;
1374 request->when = now;
1375 tv_add(&request->when, request->delay);
1376 mrd = request->proxy_when;
1377 mrd.tv_sec += request->home_server->coa_mrd;
1380 * Cap duration at MRD.
1382 if (timercmp(&mrd, &request->when, <)) {
1383 request->when = mrd;
1384 INSERT_EVENT(no_response_to_coa_request, request);
1387 INSERT_EVENT(retransmit_coa_request, request);
1390 if (update_event_timestamp(request->proxy, now.tv_sec)) {
1391 if (!insert_into_proxy_hash(request, TRUE)) {
1392 DEBUG("ERROR: Failed re-inserting CoA request into proxy hash.");
1396 request->num_proxied_requests = 0;
1397 request->num_proxied_responses = 0;
1400 request->num_proxied_requests++;
1401 request->num_coa_requests++; /* is NOT reset by code 3 lines above! */
1403 request->proxy_listener->send(request->proxy_listener,
1409 * The original request is either DONE, or in CLEANUP_DELAY.
1411 static int originated_coa_request(REQUEST *request)
1413 int delay, rcode, pre_proxy_type = 0;
1419 rad_assert(request->proxy == NULL);
1420 rad_assert(!request->in_proxy_hash);
1421 rad_assert(request->proxy_reply == NULL);
1423 vp = pairfind(request->config_items, PW_SEND_COA_REQUEST);
1424 if (!vp && request->coa) vp = pairfind(request->coa->proxy->vps, PW_SEND_COA_REQUEST);
1426 if (vp->vp_integer == 0) {
1427 request_free(&request->coa);
1428 return 1; /* success */
1431 if (!request->coa) request_alloc_coa(request);
1432 if (!request->coa) return 0;
1438 * src_ipaddr will be set up in proxy_encode.
1440 memset(&ipaddr, 0, sizeof(ipaddr));
1441 vp = pairfind(coa->proxy->vps, PW_PACKET_DST_IP_ADDRESS);
1443 ipaddr.af = AF_INET;
1444 ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
1446 } else if ((vp = pairfind(coa->proxy->vps,
1447 PW_PACKET_DST_IPV6_ADDRESS)) != NULL) {
1448 ipaddr.af = AF_INET6;
1449 ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
1451 } else if ((vp = pairfind(coa->proxy->vps,
1452 PW_HOME_SERVER_POOL)) != NULL) {
1453 coa->home_pool = home_pool_byname(vp->vp_strvalue,
1455 if (!coa->home_pool) {
1456 RDEBUG2("WARNING: No such home_server_pool %s",
1459 request_free(&request->coa);
1466 } else if (request->client->coa_pool) {
1467 coa->home_pool = request->client->coa_pool;
1469 } else if (request->client->coa_server) {
1470 coa->home_server = request->client->coa_server;
1474 * If all else fails, send it to the client that
1475 * originated this request.
1477 memcpy(&ipaddr, &request->packet->src_ipaddr, sizeof(ipaddr));
1481 * Use the pool, if it exists.
1483 if (coa->home_pool) {
1484 coa->home_server = home_server_ldb(NULL, coa->home_pool, coa);
1485 if (!coa->home_server) {
1486 RDEBUG("WARNING: No live home server for home_server_pool %s", vp->vp_strvalue);
1490 } else if (!coa->home_server) {
1491 int port = PW_COA_UDP_PORT;
1493 vp = pairfind(coa->proxy->vps, PW_PACKET_DST_PORT);
1494 if (vp) port = vp->vp_integer;
1496 coa->home_server = home_server_find(&ipaddr, port);
1497 if (!coa->home_server) {
1498 RDEBUG2("WARNING: Unknown destination %s:%d for CoA request.",
1499 inet_ntop(ipaddr.af, &ipaddr.ipaddr,
1500 buffer, sizeof(buffer)), port);
1505 vp = pairfind(coa->proxy->vps, PW_PACKET_TYPE);
1507 switch (vp->vp_integer) {
1508 case PW_COA_REQUEST:
1509 case PW_DISCONNECT_REQUEST:
1510 coa->proxy->code = vp->vp_integer;
1514 DEBUG("Cannot set CoA Packet-Type to code %d",
1520 if (!coa->proxy->code) coa->proxy->code = PW_COA_REQUEST;
1523 * The rest of the server code assumes that
1524 * request->packet && request->reply exist. Copy them
1525 * from the original request.
1527 rad_assert(coa->packet != NULL);
1528 rad_assert(coa->packet->vps == NULL);
1529 memcpy(coa->packet, request->packet, sizeof(*request->packet));
1530 coa->packet->vps = paircopy(request->packet->vps);
1531 coa->packet->data = NULL;
1532 rad_assert(coa->reply != NULL);
1533 rad_assert(coa->reply->vps == NULL);
1534 memcpy(coa->reply, request->reply, sizeof(*request->reply));
1535 coa->reply->vps = paircopy(request->reply->vps);
1536 coa->reply->data = NULL;
1537 coa->config_items = paircopy(request->config_items);
1540 * Call the pre-proxy routines.
1542 vp = pairfind(request->config_items, PW_PRE_PROXY_TYPE);
1544 RDEBUG2(" Found Pre-Proxy-Type %s", vp->vp_strvalue);
1545 pre_proxy_type = vp->vp_integer;
1548 if (coa->home_pool && coa->home_pool->virtual_server) {
1549 const char *old_server = coa->server;
1551 coa->server = coa->home_pool->virtual_server;
1552 RDEBUG2(" server %s {", coa->server);
1553 rcode = module_pre_proxy(pre_proxy_type, coa);
1555 coa->server = old_server;
1557 rcode = module_pre_proxy(pre_proxy_type, coa);
1564 * Only send the CoA packet if the pre-proxy code succeeded.
1566 case RLM_MODULE_NOOP:
1568 case RLM_MODULE_UPDATED:
1573 * Source IP / port is set when the proxy socket
1576 coa->proxy->dst_ipaddr = coa->home_server->ipaddr;
1577 coa->proxy->dst_port = coa->home_server->port;
1579 if (!insert_into_proxy_hash(coa, FALSE)) {
1580 DEBUG("ERROR: Failed inserting CoA request into proxy hash.");
1585 * We CANNOT divorce the CoA request from the parent
1586 * request. This function is running in a child thread,
1587 * and we need access to the main event loop in order to
1588 * to add the timers for the CoA packet. See
1593 * Forget about the original request completely at this
1598 gettimeofday(&request->proxy_when, NULL);
1599 request->received = request->next_when = request->proxy_when;
1600 rad_assert(request->proxy_reply == NULL);
1603 * Implement re-transmit algorithm as per RFC 5080
1606 * We want IRT + RAND*IRT
1607 * or 0.9 IRT + rand(0,.2) IRT
1609 * 2^20 ~ USEC, and we want 2.
1610 * rand(0,0.2) USEC ~ (rand(0,2^21) / 10)
1612 delay = (fr_rand() & ((1 << 22) - 1)) / 10;
1613 request->delay = delay * request->home_server->coa_irt;
1614 delay = request->home_server->coa_irt * USEC;
1615 delay -= delay / 10;
1616 delay += request->delay;
1618 request->delay = delay;
1619 tv_add(&request->next_when, delay);
1620 request->next_callback = retransmit_coa_request;
1623 * Note that we set proxied BEFORE sending the packet.
1625 * Once we send it, the request is tainted, as
1626 * another thread may have picked it up. Don't
1629 request->num_proxied_requests = 1;
1630 request->num_proxied_responses = 0;
1631 request->child_pid = NO_SUCH_CHILD_PID;
1633 update_event_timestamp(request->proxy, request->proxy_when.tv_sec);
1635 request->child_state = REQUEST_PROXIED;
1637 DEBUG_PACKET(request, request->proxy, 1);
1639 request->proxy_listener->send(request->proxy_listener,
1643 #endif /* WITH_COA */
1646 static int process_proxy_reply(REQUEST *request)
1649 int post_proxy_type = 0;
1653 * Delete any reply we had accumulated until now.
1655 pairfree(&request->reply->vps);
1658 * Run the packet through the post-proxy stage,
1659 * BEFORE playing games with the attributes.
1661 vp = pairfind(request->config_items, PW_POST_PROXY_TYPE);
1663 RDEBUG2(" Found Post-Proxy-Type %s", vp->vp_strvalue);
1664 post_proxy_type = vp->vp_integer;
1667 if (request->home_pool && request->home_pool->virtual_server) {
1668 const char *old_server = request->server;
1670 request->server = request->home_pool->virtual_server;
1671 RDEBUG2(" server %s {", request->server);
1672 rcode = module_post_proxy(post_proxy_type, request);
1674 request->server = old_server;
1676 rcode = module_post_proxy(post_proxy_type, request);
1680 if (request->packet->code == request->proxy->code)
1682 * Don't run the next bit if we originated a CoA
1683 * packet, after receiving an Access-Request or
1684 * Accounting-Request.
1689 * There may NOT be a proxy reply, as we may be
1690 * running Post-Proxy-Type = Fail.
1692 if (request->proxy_reply) {
1694 * Delete the Proxy-State Attributes from
1695 * the reply. These include Proxy-State
1696 * attributes from us and remote server.
1698 pairdelete(&request->proxy_reply->vps, PW_PROXY_STATE);
1701 * Add the attributes left in the proxy
1702 * reply to the reply list.
1704 pairadd(&request->reply->vps, request->proxy_reply->vps);
1705 request->proxy_reply->vps = NULL;
1708 * Free proxy request pairs.
1710 pairfree(&request->proxy->vps);
1714 default: /* Don't do anything */
1716 case RLM_MODULE_FAIL:
1717 /* FIXME: debug print stuff */
1718 request->child_state = REQUEST_DONE;
1721 case RLM_MODULE_HANDLED:
1722 /* FIXME: debug print stuff */
1723 request->child_state = REQUEST_DONE;
1731 static int request_pre_handler(REQUEST *request)
1735 rad_assert(request->magic == REQUEST_MAGIC);
1736 rad_assert(request->packet != NULL);
1738 request->child_state = REQUEST_RUNNING;
1741 * Don't decode the packet if it's an internal "fake"
1742 * request. Instead, just return so that the caller can
1745 if (request->packet->dst_port == 0) {
1746 request->username = pairfind(request->packet->vps,
1748 request->password = pairfind(request->packet->vps,
1755 * Put the decoded packet into it's proper place.
1757 if (request->proxy_reply != NULL) {
1758 rcode = request->proxy_listener->decode(request->proxy_listener,
1760 DEBUG_PACKET(request, request->proxy_reply, 0);
1763 if (request->packet->vps == NULL) {
1764 rcode = request->listener->decode(request->listener, request);
1766 if (debug_condition) {
1768 const char *my_debug = debug_condition;
1771 * Ignore parse errors.
1773 radius_evaluate_condition(request, RLM_MODULE_OK, 0,
1777 request->options = 2;
1778 request->radlog = radlog_request;
1782 DEBUG_PACKET(request, request->packet, 0);
1788 radlog(L_ERR, "%s Dropping packet without response.", fr_strerror());
1789 request->child_state = REQUEST_DONE;
1793 if (!request->username) {
1794 request->username = pairfind(request->packet->vps,
1799 if (request->proxy) {
1800 return process_proxy_reply(request);
1810 * Do state handling when we proxy a request.
1812 static int proxy_request(REQUEST *request)
1814 struct timeval when;
1817 if (request->home_server->server) {
1818 RDEBUG("ERROR: Cannot perform real proxying to a virtual server.");
1822 if (!insert_into_proxy_hash(request, FALSE)) {
1823 RDEBUG("ERROR: Failed inserting request into proxy hash.");
1827 request->proxy_listener->encode(request->proxy_listener, request);
1829 when = request->received;
1830 when.tv_sec += request->root->max_request_time;
1832 gettimeofday(&request->proxy_when, NULL);
1834 request->next_when = request->proxy_when;
1835 request->next_when.tv_sec += request->home_server->response_window;
1837 rad_assert(request->home_server->response_window > 0);
1839 if (timercmp(&when, &request->next_when, <)) {
1840 request->next_when = when;
1842 request->next_callback = no_response_to_proxied_request;
1844 RDEBUG2("Proxying request %d to home server %s port %d",
1846 inet_ntop(request->proxy->dst_ipaddr.af,
1847 &request->proxy->dst_ipaddr.ipaddr,
1848 buffer, sizeof(buffer)),
1849 request->proxy->dst_port);
1852 * Note that we set proxied BEFORE sending the packet.
1854 * Once we send it, the request is tainted, as
1855 * another thread may have picked it up. Don't
1858 request->num_proxied_requests = 1;
1859 request->num_proxied_responses = 0;
1860 #ifdef HAVE_PTHREAD_H
1861 request->child_pid = NO_SUCH_CHILD_PID;
1863 request->child_state = REQUEST_PROXIED;
1865 DEBUG_PACKET(request, request->proxy, 1);
1867 request->proxy_listener->send(request->proxy_listener,
1874 * "Proxy" the request by sending it to a new virtual server.
1876 static int proxy_to_virtual_server(REQUEST *request)
1879 RAD_REQUEST_FUNP fun;
1881 if (!request->home_server || !request->home_server->server) return 0;
1883 if (request->parent) {
1884 RDEBUG2("WARNING: Cancelling proxy request to virtual server %s as this request was itself proxied.", request->home_server->server);
1888 fake = request_alloc_fake(request);
1890 RDEBUG2("WARNING: Out of memory");
1894 fake->packet->vps = paircopy(request->proxy->vps);
1895 fake->server = request->home_server->server;
1897 if (request->proxy->code == PW_AUTHENTICATION_REQUEST) {
1898 fun = rad_authenticate;
1900 #ifdef WITH_ACCOUNTING
1901 } else if (request->proxy->code == PW_ACCOUNTING_REQUEST) {
1902 fun = rad_accounting;
1906 RDEBUG2("Unknown packet type %d", request->proxy->code);
1907 request_free(&fake);
1911 RDEBUG2(">>> Sending proxied request internally to virtual server.");
1912 radius_handle_request(fake, fun);
1913 RDEBUG2("<<< Received proxied response from internal virtual server.");
1915 request->proxy_reply = fake->reply;
1918 request_free(&fake);
1920 process_proxy_reply(request);
1922 if (request->server) RDEBUG("server %s {",
1923 request->server != NULL ?
1924 request->server : "");
1927 if (request->server) RDEBUG("} # server %s",
1928 request->server != NULL ?
1929 request->server : "");
1931 return 2; /* success, but NOT '1' !*/
1935 * Return 1 if we did proxy it, or the proxy attempt failed
1936 * completely. Either way, the caller doesn't touch the request
1937 * any more if we return 1.
1939 static int successfully_proxied_request(REQUEST *request)
1942 int pre_proxy_type = 0;
1943 VALUE_PAIR *realmpair;
1944 VALUE_PAIR *strippedname;
1946 char *realmname = NULL;
1948 REALM *realm = NULL;
1952 * If it was already proxied, do nothing.
1954 * FIXME: This should really be a serious error.
1956 if (request->in_proxy_hash) {
1960 realmpair = pairfind(request->config_items, PW_PROXY_TO_REALM);
1961 if (!realmpair || (realmpair->length == 0)) {
1965 realmname = (char *) realmpair->vp_strvalue;
1967 realm = realm_find2(realmname);
1969 RDEBUG2("ERROR: Cannot proxy to unknown realm %s", realmname);
1974 * Figure out which pool to use.
1976 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
1977 pool = realm->auth_pool;
1979 #ifdef WITH_ACCOUNTING
1980 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
1981 pool = realm->acct_pool;
1985 rad_panic("Internal sanity check failed");
1989 RDEBUG2(" WARNING: Cancelling proxy to Realm %s, as the realm is local.",
1994 home = home_server_ldb(realmname, pool, request);
1996 RDEBUG2("ERROR: Failed to find live home server for realm %s",
2000 request->home_pool = pool;
2003 * Remember that we sent the request to a Realm.
2005 pairadd(&request->packet->vps,
2006 pairmake("Realm", realmname, T_OP_EQ));
2009 * Strip the name, if told to.
2011 * Doing it here catches the case of proxied tunneled
2014 if (realm->striprealm == TRUE &&
2015 (strippedname = pairfind(request->proxy->vps, PW_STRIPPED_USER_NAME)) != NULL) {
2017 * If there's a Stripped-User-Name attribute in
2018 * the request, then use THAT as the User-Name
2019 * for the proxied request, instead of the
2022 * This is done by making a copy of the
2023 * Stripped-User-Name attribute, turning it into
2024 * a User-Name attribute, deleting the
2025 * Stripped-User-Name and User-Name attributes
2026 * from the vps list, and making the new
2027 * User-Name the head of the vps list.
2029 vp = pairfind(request->proxy->vps, PW_USER_NAME);
2031 vp = radius_paircreate(request, NULL,
2032 PW_USER_NAME, PW_TYPE_STRING);
2033 rad_assert(vp != NULL); /* handled by above function */
2034 /* Insert at the START of the list */
2035 vp->next = request->proxy->vps;
2036 request->proxy->vps = vp;
2038 memcpy(vp->vp_strvalue, strippedname->vp_strvalue,
2039 sizeof(vp->vp_strvalue));
2040 vp->length = strippedname->length;
2043 * Do NOT delete Stripped-User-Name.
2048 * If there is no PW_CHAP_CHALLENGE attribute but
2049 * there is a PW_CHAP_PASSWORD we need to add it
2050 * since we can't use the request authenticator
2051 * anymore - we changed it.
2053 if (pairfind(request->proxy->vps, PW_CHAP_PASSWORD) &&
2054 pairfind(request->proxy->vps, PW_CHAP_CHALLENGE) == NULL) {
2055 vp = radius_paircreate(request, &request->proxy->vps,
2056 PW_CHAP_CHALLENGE, PW_TYPE_OCTETS);
2057 vp->length = AUTH_VECTOR_LEN;
2058 memcpy(vp->vp_strvalue, request->packet->vector, AUTH_VECTOR_LEN);
2062 * The RFC's say we have to do this, but FreeRADIUS
2065 vp = radius_paircreate(request, &request->proxy->vps,
2066 PW_PROXY_STATE, PW_TYPE_OCTETS);
2067 snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%d",
2068 request->packet->id);
2069 vp->length = strlen(vp->vp_strvalue);
2072 * Should be done BEFORE inserting into proxy hash, as
2073 * pre-proxy may use this information, or change it.
2075 request->proxy->code = request->packet->code;
2078 * Call the pre-proxy routines.
2080 vp = pairfind(request->config_items, PW_PRE_PROXY_TYPE);
2082 RDEBUG2(" Found Pre-Proxy-Type %s", vp->vp_strvalue);
2083 pre_proxy_type = vp->vp_integer;
2086 rad_assert(request->home_pool != NULL);
2088 if (request->home_pool->virtual_server) {
2089 const char *old_server = request->server;
2091 request->server = request->home_pool->virtual_server;
2092 RDEBUG2(" server %s {", request->server);
2093 rcode = module_pre_proxy(pre_proxy_type, request);
2095 request->server = old_server;
2097 rcode = module_pre_proxy(pre_proxy_type, request);
2100 case RLM_MODULE_FAIL:
2101 case RLM_MODULE_INVALID:
2102 case RLM_MODULE_NOTFOUND:
2103 case RLM_MODULE_USERLOCK:
2105 /* FIXME: debug print failed stuff */
2108 case RLM_MODULE_REJECT:
2109 case RLM_MODULE_HANDLED:
2113 * Only proxy the packet if the pre-proxy code succeeded.
2115 case RLM_MODULE_NOOP:
2117 case RLM_MODULE_UPDATED:
2122 * If it's a fake request, don't send the proxy
2123 * packet. The outer tunnel session will take
2124 * care of doing that.
2126 if (request->packet->dst_port == 0) {
2127 request->home_server = NULL;
2131 if (request->home_server->server) {
2132 return proxy_to_virtual_server(request);
2135 if (!proxy_request(request)) {
2136 RDEBUG("ERROR: Failed to proxy request %d", request->number);
2144 static void request_post_handler(REQUEST *request)
2146 int child_state = -1;
2147 struct timeval when;
2150 if ((request->master_state == REQUEST_STOP_PROCESSING) ||
2152 (request->parent->master_state == REQUEST_STOP_PROCESSING))) {
2153 RDEBUG2("Request %d was cancelled.", request->number);
2154 #ifdef HAVE_PTHREAD_H
2155 request->child_pid = NO_SUCH_CHILD_PID;
2157 child_state = REQUEST_DONE;
2161 if (request->child_state != REQUEST_RUNNING) {
2162 rad_panic("Internal sanity check failed");
2167 * If it's not in the request hash, it's a CoA request.
2170 if (!request->in_request_hash &&
2172 ((request->proxy->code == PW_COA_REQUEST) ||
2173 (request->proxy->code == PW_DISCONNECT_REQUEST))) {
2174 request->next_callback = NULL;
2175 child_state = REQUEST_DONE;
2180 if ((request->reply->code == 0) &&
2181 ((vp = pairfind(request->config_items, PW_AUTH_TYPE)) != NULL) &&
2182 (vp->vp_integer == PW_AUTHTYPE_REJECT)) {
2183 request->reply->code = PW_AUTHENTICATION_REJECT;
2187 if (request->root->proxy_requests &&
2188 !request->in_proxy_hash &&
2189 (request->reply->code == 0) &&
2190 (request->packet->dst_port != 0) &&
2191 (request->packet->code != PW_STATUS_SERVER)) {
2192 int rcode = successfully_proxied_request(request);
2196 * If we proxy it, we CANNOT originate a CoA
2197 * request at the same time.
2199 if (rcode != 0) request_free(&request->coa);
2202 if (rcode == 1) return;
2205 * Failed proxying it (dead home servers, etc.)
2206 * Run it through Post-Proxy-Type = Fail, and
2207 * respond to the request.
2209 * Note that we're in a child thread here, so we
2210 * do NOT re-schedule the request. Instead, we
2211 * do what we would have done, which is run the
2212 * pre-handler, a NULL request handler, and then
2215 if ((rcode < 0) && setup_post_proxy_fail(request)) {
2216 request_pre_handler(request);
2220 * Else we weren't supposed to proxy it,
2221 * OR we proxied it internally to a virutal server.
2227 * Fake requests don't get encoded or signed. The caller
2228 * also requires the reply VP's, so we don't free them
2231 if (request->packet->dst_port == 0) {
2232 /* FIXME: RDEBUG going to the next request */
2233 #ifdef HAVE_PTHREAD_H
2234 request->child_pid = NO_SUCH_CHILD_PID;
2236 request->child_state = REQUEST_DONE;
2242 * Copy Proxy-State from the request to the reply.
2244 vp = paircopy2(request->packet->vps, PW_PROXY_STATE);
2245 if (vp) pairadd(&request->reply->vps, vp);
2249 * Access-Requests get delayed or cached.
2251 switch (request->packet->code) {
2252 case PW_AUTHENTICATION_REQUEST:
2253 gettimeofday(&request->next_when, NULL);
2255 if (request->reply->code == 0) {
2257 * Check if the lack of response is intentional.
2259 vp = pairfind(request->config_items,
2260 PW_RESPONSE_PACKET_TYPE);
2262 RDEBUG2("There was no response configured: rejecting request %d",
2264 request->reply->code = PW_AUTHENTICATION_REJECT;
2265 } else if (vp->vp_integer == 256) {
2266 RDEBUG2("Not responding to request %d",
2270 request->reply->code = vp->vp_integer;
2276 * Run rejected packets through
2278 * Post-Auth-Type = Reject
2280 if (request->reply->code == PW_AUTHENTICATION_REJECT) {
2281 pairdelete(&request->config_items, PW_POST_AUTH_TYPE);
2282 vp = radius_pairmake(request, &request->config_items,
2283 "Post-Auth-Type", "Reject",
2285 if (vp) rad_postauth(request);
2288 * If configured, delay Access-Reject packets.
2290 * If request->root->reject_delay = 0, we discover
2291 * that we have to send the packet now.
2293 when = request->received;
2294 when.tv_sec += request->root->reject_delay;
2296 if (timercmp(&when, &request->next_when, >)) {
2297 RDEBUG2("Delaying reject of request %d for %d seconds",
2299 request->root->reject_delay);
2300 request->next_when = when;
2301 request->next_callback = reject_delay;
2302 #ifdef HAVE_PTHREAD_H
2303 request->child_pid = NO_SUCH_CHILD_PID;
2305 request->child_state = REQUEST_REJECT_DELAY;
2310 request->next_when.tv_sec += request->root->cleanup_delay;
2311 request->next_callback = cleanup_delay;
2312 child_state = REQUEST_CLEANUP_DELAY;
2315 case PW_ACCOUNTING_REQUEST:
2316 request->next_callback = NULL; /* just to be safe */
2317 child_state = REQUEST_DONE;
2321 * FIXME: Status-Server should probably not be
2324 case PW_STATUS_SERVER:
2325 request->next_callback = NULL;
2326 child_state = REQUEST_DONE;
2330 if ((request->packet->code > 1024) &&
2331 (request->packet->code < (1024 + 254 + 1))) {
2332 request->next_callback = NULL;
2333 child_state = REQUEST_DONE;
2337 radlog(L_ERR, "Unknown packet type %d", request->packet->code);
2338 request->next_callback = NULL;
2339 child_state = REQUEST_DONE;
2344 * Suppress "no reply" packets here, unless we're reading
2345 * from the "detail" file. In that case, we've got to
2346 * tell the detail file handler that the request is dead,
2347 * and it should re-send it.
2348 * If configured, encode, sign, and send.
2350 if ((request->reply->code != 0) ||
2351 (request->listener->type == RAD_LISTEN_DETAIL)) {
2352 DEBUG_PACKET(request, request->reply, 1);
2353 request->listener->send(request->listener, request);
2358 * Now that we've completely processed the request,
2359 * see if we need to originate a CoA request.
2362 (pairfind(request->config_items, PW_SEND_COA_REQUEST) != NULL)) {
2363 if (!originated_coa_request(request)) {
2364 RDEBUG2("Do CoA Fail handler here");
2366 /* request->coa is stil set, so we can update events */
2372 * Clean up. These are no longer needed.
2374 pairfree(&request->config_items);
2376 pairfree(&request->packet->vps);
2377 request->username = NULL;
2378 request->password = NULL;
2380 pairfree(&request->reply->vps);
2383 if (request->proxy) {
2384 pairfree(&request->proxy->vps);
2386 if (request->proxy_reply) {
2387 pairfree(&request->proxy_reply->vps);
2392 * We're not tracking responses from the home
2393 * server, we can therefore free this memory in
2396 if (!request->in_proxy_hash) {
2397 rad_free(&request->proxy);
2398 rad_free(&request->proxy_reply);
2399 request->home_server = NULL;
2405 RDEBUG2("Finished request %d.", request->number);
2406 rad_assert(child_state >= 0);
2407 request->child_state = child_state;
2410 * Single threaded mode: update timers now.
2412 if (!have_children) wait_a_bit(request);
2416 static void received_retransmit(REQUEST *request, const RADCLIENT *client)
2422 RAD_STATS_TYPE_INC(request->listener, total_dup_requests);
2423 RAD_STATS_CLIENT_INC(request->listener, client, total_dup_requests);
2425 switch (request->child_state) {
2426 case REQUEST_QUEUED:
2427 case REQUEST_RUNNING:
2431 radlog(L_ERR, "Discarding duplicate request from "
2432 "client %s port %d - ID: %d due to unfinished request %d",
2434 request->packet->src_port,request->packet->id,
2439 case REQUEST_PROXIED:
2441 * We're not supposed to have duplicate
2442 * accounting packets. The other states handle
2443 * duplicates fine (discard, or send duplicate
2444 * reply). But we do NOT want to retransmit an
2445 * accounting request here, because that would
2446 * involve updating the Acct-Delay-Time, and
2447 * therefore changing the packet Id, etc.
2449 * Instead, we just discard the packet. We may
2450 * eventually respond, or the client will send a
2451 * new accounting packet.
2453 * The same comments go for Status-Server, and
2454 * other packet types.
2456 * FIXME: coa: when we proxy CoA && Disconnect
2457 * packets, this logic has to be fixed.
2459 if (request->packet->code != PW_AUTHENTICATION_REQUEST) {
2463 check_for_zombie_home_server(request);
2466 * If we've just discovered that the home server is
2467 * dead, send the packet to another one.
2469 if ((request->packet->dst_port != 0) &&
2470 (request->home_server->state == HOME_STATE_IS_DEAD)) {
2473 remove_from_proxy_hash(request);
2475 home = home_server_ldb(NULL, request->home_pool, request);
2477 RDEBUG2("Failed to find live home server for request %d", request->number);
2480 * Do post-request processing,
2481 * and any insertion of necessary
2484 post_proxy_fail_handler(request);
2488 request->proxy->code = request->packet->code;
2491 * Free the old packet, to force re-encoding
2493 free(request->proxy->data);
2494 request->proxy->data = NULL;
2495 request->proxy->data_len = 0;
2498 * This request failed over to a virtual
2499 * server. Push it back onto the queue
2502 if (request->home_server->server) {
2503 proxy_fallback_handler(request);
2508 * Try to proxy the request.
2510 if (!proxy_request(request)) {
2511 RDEBUG("ERROR: Failed to re-proxy request %d", request->number);
2512 goto no_home_servers;
2516 * This code executes in the main server
2517 * thread, so there's no need for locking.
2519 rad_assert(request->next_callback != NULL);
2520 INSERT_EVENT(request->next_callback, request);
2521 request->next_callback = NULL;
2523 } /* else the home server is still alive */
2525 RDEBUG2("Sending duplicate proxied request to home server %s port %d - ID: %d",
2526 inet_ntop(request->proxy->dst_ipaddr.af,
2527 &request->proxy->dst_ipaddr.ipaddr,
2528 buffer, sizeof(buffer)),
2529 request->proxy->dst_port,
2530 request->proxy->id);
2531 request->num_proxied_requests++;
2533 DEBUG_PACKET(request, request->proxy, 1);
2534 request->proxy_listener->send(request->proxy_listener,
2539 case REQUEST_REJECT_DELAY:
2540 RDEBUG2("Waiting to send Access-Reject "
2541 "to client %s port %d - ID: %d",
2543 request->packet->src_port, request->packet->id);
2546 case REQUEST_CLEANUP_DELAY:
2548 if (request->reply->code == 0) {
2549 RDEBUG2("Ignoring retransmit from client %s port %d "
2550 "- ID: %d, no reply was configured",
2552 request->packet->src_port, request->packet->id);
2557 * FIXME: This sends duplicate replies to
2558 * accounting requests, even if Acct-Delay-Time
2559 * or Event-Timestamp is in the packet. In those
2560 * cases, the Id should be changed, and the packet
2563 RDEBUG2("Sending duplicate reply "
2564 "to client %s port %d - ID: %d",
2566 request->packet->src_port, request->packet->id);
2567 DEBUG_PACKET(request, request->reply, 1);
2568 request->listener->send(request->listener, request);
2574 static void received_conflicting_request(REQUEST *request,
2575 const RADCLIENT *client)
2577 radlog(L_ERR, "Received conflicting packet from "
2578 "client %s port %d - ID: %d due to unfinished request %d. Giving up on old request.",
2580 request->packet->src_port, request->packet->id,
2584 * Nuke it from the request hash, so we can receive new
2587 remove_from_request_hash(request);
2589 switch (request->child_state) {
2590 #ifdef HAVE_PTHREAD_H
2592 * It's queued or running. Tell it to stop, and
2593 * wait for it to do so.
2595 case REQUEST_QUEUED:
2596 case REQUEST_RUNNING:
2597 request->master_state = REQUEST_STOP_PROCESSING;
2598 request->delay += request->delay >> 1;
2600 tv_add(&request->when, request->delay);
2602 INSERT_EVENT(wait_for_child_to_die, request);
2607 * It's in some other state, and therefore also
2608 * in the event queue. At some point, the
2609 * child will notice, and we can then delete it.
2612 rad_assert(request->ev != NULL);
2618 static int can_handle_new_request(RADIUS_PACKET *packet,
2620 struct main_config_t *root)
2623 * Count the total number of requests, to see if
2624 * there are too many. If so, return with an
2627 if (root->max_requests) {
2628 int request_count = fr_packet_list_num_elements(pl);
2631 * This is a new request. Let's see if
2632 * it makes us go over our configured
2635 if (request_count > root->max_requests) {
2636 radlog(L_ERR, "Dropping request (%d is too many): "
2637 "from client %s port %d - ID: %d", request_count,
2639 packet->src_port, packet->id);
2640 radlog(L_INFO, "WARNING: Please check the configuration file.\n"
2641 "\tThe value for 'max_requests' is probably set too low.\n");
2643 } /* else there were a small number of requests */
2644 } /* else there was no configured limit for requests */
2647 * FIXME: Add per-client checks. If one client is sending
2648 * too many packets, start discarding them.
2650 * We increment the counters here, and decrement them
2651 * when the response is sent... somewhere in this file.
2655 * FUTURE: Add checks for system load. If the system is
2656 * busy, start dropping requests...
2658 * We can probably keep some statistics ourselves... if
2659 * there are more requests coming in than we can handle,
2660 * start dropping some.
2667 int received_request(rad_listen_t *listener,
2668 RADIUS_PACKET *packet, REQUEST **prequest,
2671 RADIUS_PACKET **packet_p;
2672 REQUEST *request = NULL;
2673 struct main_config_t *root = &mainconfig;
2675 packet_p = fr_packet_list_find(pl, packet);
2677 request = fr_packet2myptr(REQUEST, packet, packet_p);
2678 rad_assert(request->in_request_hash);
2680 if ((request->packet->data_len == packet->data_len) &&
2681 (memcmp(request->packet->vector, packet->vector,
2682 sizeof(packet->vector)) == 0)) {
2683 received_retransmit(request, client);
2688 * The new request is different from the old one,
2689 * but maybe the old is finished. If so, delete
2692 switch (request->child_state) {
2693 struct timeval when;
2697 * Special hacks for race conditions.
2698 * The reply is encoded, and therefore
2699 * likely sent. We received a *new*
2700 * packet from the client, likely before
2701 * the next line or two of code which
2702 * updated the child state. In this
2703 * case, just accept the new request.
2705 if ((request->reply->code != 0) &&
2706 request->reply->data) {
2707 radlog(L_INFO, "WARNING: Allowing fast client %s port %d - ID: %d for recent request %d.",
2709 packet->src_port, packet->id,
2711 remove_from_request_hash(request);
2716 gettimeofday(&when, NULL);
2720 * If the cached request was received
2721 * within the last second, then we
2722 * discard the NEW request instead of the
2723 * old one. This will happen ONLY when
2724 * the client is severely broken, and is
2725 * sending conflicting packets very
2728 if (timercmp(&when, &request->received, <)) {
2729 radlog(L_ERR, "Discarding conflicting packet from "
2730 "client %s port %d - ID: %d due to recent request %d.",
2732 packet->src_port, packet->id,
2737 received_conflicting_request(request, client);
2741 case REQUEST_REJECT_DELAY:
2742 case REQUEST_CLEANUP_DELAY:
2743 request->child_state = REQUEST_DONE;
2745 cleanup_delay(request);
2752 * We may want to quench the new request.
2754 if ((listener->type != RAD_LISTEN_DETAIL) &&
2755 !can_handle_new_request(packet, client, root)) {
2760 * Create and initialize the new request.
2762 request = request_alloc(); /* never fails */
2764 if ((request->reply = rad_alloc(0)) == NULL) {
2765 radlog(L_ERR, "No memory");
2769 request->listener = listener;
2770 request->client = client;
2771 request->packet = packet;
2772 request->packet->timestamp = request->timestamp;
2773 request->number = request_num_counter++;
2774 request->priority = listener->type;
2775 #ifdef HAVE_PTHREAD_H
2776 request->child_pid = NO_SUCH_CHILD_PID;
2780 * Status-Server packets go to the head of the queue.
2782 if (request->packet->code == PW_STATUS_SERVER) request->priority = 0;
2785 * Set virtual server identity
2787 if (client->server) {
2788 request->server = client->server;
2789 } else if (listener->server) {
2790 request->server = listener->server;
2792 request->server = NULL;
2796 * Remember the request in the list.
2798 if (!fr_packet_list_insert(pl, &request->packet)) {
2799 radlog(L_ERR, "Failed to insert request %d in the list of live requests: discarding", request->number);
2800 request_free(&request);
2804 request->in_request_hash = TRUE;
2805 request->root = root;
2809 * The request passes many of our sanity checks.
2810 * From here on in, if anything goes wrong, we
2811 * send a reject message, instead of dropping the
2816 * Build the reply template from the request.
2819 request->reply->sockfd = request->packet->sockfd;
2820 request->reply->dst_ipaddr = request->packet->src_ipaddr;
2821 request->reply->src_ipaddr = request->packet->dst_ipaddr;
2822 request->reply->dst_port = request->packet->src_port;
2823 request->reply->src_port = request->packet->dst_port;
2824 request->reply->id = request->packet->id;
2825 request->reply->code = 0; /* UNKNOWN code */
2826 memcpy(request->reply->vector, request->packet->vector,
2827 sizeof(request->reply->vector));
2828 request->reply->vps = NULL;
2829 request->reply->data = NULL;
2830 request->reply->data_len = 0;
2832 request->master_state = REQUEST_ACTIVE;
2833 request->child_state = REQUEST_QUEUED;
2834 request->next_callback = NULL;
2836 gettimeofday(&request->received, NULL);
2837 request->timestamp = request->received.tv_sec;
2838 request->when = request->received;
2840 request->delay = USEC;
2842 tv_add(&request->when, request->delay);
2844 INSERT_EVENT(wait_a_bit, request);
2846 *prequest = request;
2852 REQUEST *received_proxy_response(RADIUS_PACKET *packet)
2858 * Also removes from the proxy hash if responses == requests
2860 request = lookup_in_proxy_hash(packet);
2863 radlog(L_PROXY, "No outstanding request was found for reply from host %s port %d - ID %d",
2864 inet_ntop(packet->src_ipaddr.af,
2865 &packet->src_ipaddr.ipaddr,
2866 buffer, sizeof(buffer)),
2867 packet->src_port, packet->id);
2872 * We haven't replied to the NAS, but we have seen an
2873 * earlier reply from the home server. Ignore this packet,
2874 * as we're likely still processing the previous reply.
2876 if (request->proxy_reply) {
2877 if (memcmp(request->proxy_reply->vector,
2879 sizeof(request->proxy_reply->vector)) == 0) {
2880 RDEBUG2("Discarding duplicate reply from host %s port %d - ID: %d for request %d",
2881 inet_ntop(packet->src_ipaddr.af,
2882 &packet->src_ipaddr.ipaddr,
2883 buffer, sizeof(buffer)),
2884 packet->src_port, packet->id,
2888 * ? The home server gave us a new proxy
2889 * reply which doesn't match the old
2892 RDEBUG2("Ignoring conflicting proxy reply");
2895 /* assert that there's an event queued for request? */
2899 gettimeofday(&now, NULL);
2902 * Maybe move this earlier in the decision process?
2903 * Having it here means that late or duplicate proxy
2904 * replies no longer get the home server marked as
2905 * "alive". This might be good for stability, though.
2907 request->home_server->state = HOME_STATE_ALIVE;
2911 * When originating CoA, the "proxy" reply is the reply
2912 * to the CoA request that we originated. At this point,
2913 * the original request is finished, and it has a reply.
2915 * However, if we haven't separated the two requests, do
2916 * so now. This is done so that cleaning up the original
2917 * request won't cause the CoA request to be free'd. See
2918 * util.c, request_free()
2920 if (request->parent && (request->parent->coa == request)) {
2921 request->parent->coa = NULL;
2922 request->parent = NULL;
2926 * Skip the next set of checks, as the original
2927 * reply is cached. We want to be able to still
2928 * process the CoA reply, AND to reference the
2929 * original request/reply.
2931 * This is getting to be really quite a bit of a
2937 * If there's a reply to the NAS, ignore everything
2938 * related to proxy responses
2940 if (request->reply && request->reply->code != 0) {
2941 RDEBUG2("Ignoring proxy reply that arrived after we sent a reply to the NAS");
2947 * The average includes our time to receive packets and
2948 * look them up in the hashes, which should be the same
2951 * We update the response time only for the FIRST packet
2954 if (request->home_server->ema.window > 0) {
2955 radius_stats_ema(&request->home_server->ema,
2956 &now, &request->proxy_when);
2960 switch (request->child_state) {
2961 case REQUEST_QUEUED:
2962 case REQUEST_RUNNING:
2963 radlog(L_ERR, "Internal sanity check failed for child state");
2966 case REQUEST_REJECT_DELAY:
2967 case REQUEST_CLEANUP_DELAY:
2969 radlog(L_ERR, "Reply from home server %s port %d - ID: %d arrived too late for request %d. Try increasing 'retry_delay' or 'max_request_time'",
2970 inet_ntop(packet->src_ipaddr.af,
2971 &packet->src_ipaddr.ipaddr,
2972 buffer, sizeof(buffer)),
2973 packet->src_port, packet->id,
2975 /* assert that there's an event queued for request? */
2978 case REQUEST_PROXIED:
2982 request->proxy_reply = packet;
2986 * Perform RTT calculations, as per RFC 2988 (for TCP).
2987 * Note that we only do so on the first response.
2989 if ((request->num_proxied_responses == 1)
2991 home_server *home = request->home_server;
2993 rtt = now.tv_sec - request->proxy_when.tv_sec;
2996 rtt -= request->proxy_when.tv_usec;
2998 if (!home->has_rtt) {
2999 home->has_rtt = TRUE;
3002 home->rttvar = rtt / 2;
3005 home->rttvar -= home->rttvar >> 2;
3006 home->rttvar += (home->srtt - rtt);
3007 home->srtt -= home->srtt >> 3;
3008 home->srtt += rtt >> 3;
3011 home->rto = home->srtt;
3012 if (home->rttvar > (USEC / 4)) {
3013 home->rto += home->rttvar * 4;
3021 * There's no incoming request, so it's a proxied packet
3024 if (!request->packet) {
3025 received_response_to_ping(request);
3026 request->proxy_reply = NULL; /* caller will free it */
3027 request_free(&request);
3031 request->child_state = REQUEST_QUEUED;
3032 request->when = now;
3033 request->delay = USEC;
3034 request->priority = RAD_LISTEN_PROXY;
3035 tv_add(&request->when, request->delay);
3038 * Wait a bit will take care of max_request_time
3040 INSERT_EVENT(wait_a_bit, request);
3046 void event_new_fd(rad_listen_t *this)
3050 if (this->status == RAD_LISTEN_STATUS_KNOWN) return;
3052 this->print(this, buffer, sizeof(buffer));
3054 if (this->status == RAD_LISTEN_STATUS_INIT) {
3056 DEBUG("Listening on %s", buffer);
3058 DEBUG2(" ... adding new socket %s", buffer);
3060 if (!fr_event_fd_insert(el, 0, this->fd,
3061 event_socket_handler, this)) {
3062 radlog(L_ERR, "Failed remembering handle for proxy socket!");
3066 this->status = RAD_LISTEN_STATUS_KNOWN;
3070 if (this->status == RAD_LISTEN_STATUS_CLOSED) {
3071 DEBUG2(" ... closing socket %s", buffer);
3073 fr_event_fd_delete(el, 0, this->fd);
3074 this->status = RAD_LISTEN_STATUS_FINISH;
3077 * Close the fd AFTER fixing up the requests and
3078 * listeners, so that they don't send/recv on the
3079 * wrong socket (if someone manages to open
3087 static void handle_signal_self(int flag)
3089 if ((flag & (RADIUS_SIGNAL_SELF_EXIT | RADIUS_SIGNAL_SELF_TERM)) != 0) {
3090 if ((flag & RADIUS_SIGNAL_SELF_EXIT) != 0) {
3091 fr_event_loop_exit(el, 1);
3093 fr_event_loop_exit(el, 2);
3097 } /* else exit/term flags weren't set */
3100 * Tell the even loop to stop processing.
3102 if ((flag & RADIUS_SIGNAL_SELF_HUP) != 0) {
3104 static time_t last_hup = 0;
3106 DEBUG("Received HUP signal.");
3109 if ((int) (when - last_hup) < 5) {
3110 radlog(L_INFO, "Ignoring HUP (less than 5s since last one)");
3115 fr_event_loop_exit(el, 0x80);
3119 if ((flag & RADIUS_SIGNAL_SELF_DETAIL) != 0) {
3123 * FIXME: O(N) loops suck.
3125 for (this = mainconfig.listen;
3127 this = this->next) {
3128 if (this->type != RAD_LISTEN_DETAIL) continue;
3131 * This one didn't send the signal, skip
3134 if (!this->decode(this, NULL)) continue;
3137 * Go service the interrupt.
3139 event_poll_detail(this);
3144 if ((flag & RADIUS_SIGNAL_SELF_NEW_FD) != 0) {
3147 for (this = mainconfig.listen;
3149 this = this->next) {
3155 #ifndef WITH_SELF_PIPE
3156 void radius_signal_self(int flag)
3158 handle_signal_self(flag);
3162 * Inform ourselves that we received a signal.
3164 void radius_signal_self(int flag)
3170 * The read MUST be non-blocking for this to work.
3172 rcode = read(self_pipe[0], buffer, sizeof(buffer));
3176 for (i = 0; i < rcode; i++) {
3177 buffer[0] |= buffer[i];
3185 write(self_pipe[1], buffer, 1);
3189 static void event_signal_handler(UNUSED fr_event_list_t *xel,
3190 UNUSED int fd, UNUSED void *ctx)
3195 rcode = read(self_pipe[0], buffer, sizeof(buffer));
3196 if (rcode <= 0) return;
3199 * Merge pending signals.
3201 for (i = 0; i < rcode; i++) {
3202 buffer[0] |= buffer[i];
3205 handle_signal_self(buffer[0]);
3210 static void event_socket_handler(fr_event_list_t *xel, UNUSED int fd,
3213 rad_listen_t *listener = ctx;
3214 RAD_REQUEST_FUNP fun;
3217 rad_assert(xel == el);
3221 if (listener->fd < 0) rad_panic("Socket was closed on us!");
3223 if (!listener->recv(listener, &fun, &request)) return;
3225 if (!thread_pool_addrequest(request, fun)) {
3226 request->child_state = REQUEST_DONE;
3230 typedef struct listen_detail_t {
3235 * This function is called periodically to see if this detail
3236 * file is available for reading.
3238 static void event_poll_detail(void *ctx)
3241 RAD_REQUEST_FUNP fun;
3243 rad_listen_t *this = ctx;
3244 struct timeval when;
3245 listen_detail_t *detail = this->data;
3247 rad_assert(this->type == RAD_LISTEN_DETAIL);
3250 * Try to read something.
3252 * FIXME: This does poll AND receive.
3254 rcode = this->recv(this, &fun, &request);
3256 rad_assert(fun != NULL);
3257 rad_assert(request != NULL);
3259 if (!thread_pool_addrequest(request, fun)) {
3260 request->child_state = REQUEST_DONE;
3264 if (!fr_event_now(el, &now)) gettimeofday(&now, NULL);
3268 * Backdoor API to get the delay until the next poll time.
3270 delay = this->encode(this, NULL);
3271 tv_add(&when, delay);
3273 if (!fr_event_insert(el, event_poll_detail, this,
3274 &when, &detail->ev)) {
3275 radlog(L_ERR, "Failed creating handler");
3281 static void event_status(struct timeval *wake)
3283 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
3287 if (debug_flag == 0) {
3289 radlog(L_INFO, "Ready to process requests.");
3290 just_started = FALSE;
3296 DEBUG("Ready to process requests.");
3298 } else if ((wake->tv_sec != 0) ||
3299 (wake->tv_usec >= 100000)) {
3300 DEBUG("Waking up in %d.%01u seconds.",
3301 (int) wake->tv_sec, (unsigned int) wake->tv_usec / 100000);
3306 * FIXME: Put this somewhere else, where it isn't called
3307 * all of the time...
3310 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
3312 * If there are no child threads, then there may
3313 * be child processes. In that case, wait for
3314 * their exit status, and throw that exit status
3315 * away. This helps get rid of zxombie children.
3317 while (waitpid(-1, &argval, WNOHANG) > 0) {
3325 * Externally-visibly functions.
3327 int radius_event_init(CONF_SECTION *cs, int spawn_flag)
3329 rad_listen_t *this, *head = NULL;
3333 time(&fr_start_time);
3335 el = fr_event_list_create(event_status);
3338 pl = fr_packet_list_create(0);
3339 if (!pl) return 0; /* leak el */
3341 request_num_counter = 0;
3344 * Move all of the thread calls to this file?
3346 * It may be best for the mutexes to be in this file...
3348 have_children = spawn_flag;
3351 if (mainconfig.proxy_requests) {
3353 * Create the tree for managing proxied requests and
3356 proxy_list = fr_packet_list_create(1);
3357 if (!proxy_list) return 0;
3359 #ifdef HAVE_PTHREAD_H
3360 if (pthread_mutex_init(&proxy_mutex, NULL) != 0) {
3361 radlog(L_ERR, "FATAL: Failed to initialize proxy mutex: %s",
3370 * Just before we spawn the child threads, force the log
3371 * subsystem to re-open the log file for every write.
3373 if (spawn_flag) force_log_reopen();
3375 #ifdef HAVE_PTHREAD_H
3377 NO_SUCH_CHILD_PID = (pthread_t ) (0);
3379 NO_SUCH_CHILD_PID = pthread_self(); /* not a child thread */
3381 if (thread_pool_init(cs, spawn_flag) < 0) {
3387 DEBUG("%s: #### Skipping IP addresses and Ports ####",
3392 #ifdef WITH_SELF_PIPE
3394 * Child threads need a pipe to signal us, as do the
3397 if (pipe(self_pipe) < 0) {
3398 radlog(L_ERR, "radiusd: Error opening internal pipe: %s",
3402 if (fcntl(self_pipe[0], F_SETFL, O_NONBLOCK | FD_CLOEXEC) < 0) {
3403 radlog(L_ERR, "radiusd: Error setting internal flags: %s",
3407 if (fcntl(self_pipe[1], F_SETFL, O_NONBLOCK | FD_CLOEXEC) < 0) {
3408 radlog(L_ERR, "radiusd: Error setting internal flags: %s",
3413 if (!fr_event_fd_insert(el, 0, self_pipe[0],
3414 event_signal_handler, el)) {
3415 radlog(L_ERR, "Failed creating handler for signals");
3418 #endif /* WITH_SELF_PIPE */
3422 * Mark the proxy Fd's as unused.
3427 for (i = 0; i < 32; i++) proxy_fds[i] = -1;
3431 DEBUG("%s: #### Opening IP addresses and Ports ####",
3435 * The server temporarily switches to an unprivileged
3436 * user very early in the bootstrapping process.
3437 * However, some sockets MAY require privileged access
3438 * (bind to device, or to port < 1024, or to raw
3439 * sockets). Those sockets need to call suid up/down
3440 * themselves around the functions that need a privileged
3443 if (listen_init(cs, &head) < 0) {
3448 * At this point, no one has any business *ever* going
3451 fr_suid_down_permanent();
3454 * Add all of the sockets to the event loop.
3458 this = this->next) {
3461 this->print(this, buffer, sizeof(buffer));
3463 switch (this->type) {
3465 case RAD_LISTEN_DETAIL:
3466 DEBUG("Listening on %s", buffer);
3469 * Detail files are always known, and aren't
3470 * put into the socket event loop.
3472 this->status = RAD_LISTEN_STATUS_KNOWN;
3475 * Set up the first poll interval.
3477 event_poll_detail(this);
3482 case RAD_LISTEN_PROXY:
3483 rad_assert(proxy_fds[this->fd & 0x1f] == -1);
3484 rad_assert(proxy_listeners[this->fd & 0x1f] == NULL);
3486 proxy_fds[this->fd & 0x1f] = this->fd;
3487 proxy_listeners[this->fd & 0x1f] = this;
3488 if (!fr_packet_list_socket_add(proxy_list,
3502 mainconfig.listen = head;
3508 static int request_hash_cb(UNUSED void *ctx, void *data)
3510 REQUEST *request = fr_packet2myptr(REQUEST, packet, data);
3513 rad_assert(request->in_proxy_hash == FALSE);
3516 fr_event_delete(el, &request->ev);
3517 remove_from_request_hash(request);
3518 request_free(&request);
3525 static int proxy_hash_cb(UNUSED void *ctx, void *data)
3527 REQUEST *request = fr_packet2myptr(REQUEST, proxy, data);
3529 fr_packet_list_yank(proxy_list, request->proxy);
3530 request->in_proxy_hash = FALSE;
3532 if (!request->in_request_hash) {
3533 fr_event_delete(el, &request->ev);
3534 request_free(&request);
3541 void radius_event_free(void)
3544 * FIXME: Stop all threads, or at least check that
3545 * they're all waiting on the semaphore, and the queues
3551 * There are requests in the proxy hash that aren't
3552 * referenced from anywhere else. Remove them first.
3555 PTHREAD_MUTEX_LOCK(&proxy_mutex);
3556 fr_packet_list_walk(proxy_list, NULL, proxy_hash_cb);
3557 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
3558 fr_packet_list_free(proxy_list);
3563 fr_packet_list_walk(pl, NULL, request_hash_cb);
3565 fr_packet_list_free(pl);
3568 fr_event_list_free(el);
3571 int radius_event_process(void)
3575 return fr_event_loop(el);
3578 void radius_handle_request(REQUEST *request, RAD_REQUEST_FUNP fun)
3580 request->options = RAD_REQUEST_OPTION_DEBUG2;
3582 if (request_pre_handler(request)) {
3583 rad_assert(fun != NULL);
3584 rad_assert(request != NULL);
3586 if (request->server) RDEBUG("server %s {",
3587 request->server != NULL ?
3588 request->server : "");
3591 if (request->server) RDEBUG("} # server %s",
3592 request->server != NULL ?
3593 request->server : "");
3595 request_post_handler(request);
3598 DEBUG2("Going to the next request");