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 has_detail_listener = FALSE;
59 static int just_started = TRUE;
62 static int self_pipe[2];
67 static pthread_mutex_t proxy_mutex;
70 #define PTHREAD_MUTEX_LOCK if (have_children) pthread_mutex_lock
71 #define PTHREAD_MUTEX_UNLOCK if (have_children) pthread_mutex_unlock
73 static pthread_t NO_SUCH_CHILD_PID;
76 * This is easier than ifdef's throughout the code.
78 #define PTHREAD_MUTEX_LOCK(_x)
79 #define PTHREAD_MUTEX_UNLOCK(_x)
80 int thread_pool_addrequest(REQUEST *request, RAD_REQUEST_FUNP fun)
82 radius_handle_request(request, fun);
87 #define INSERT_EVENT(_function, _ctx) if (!fr_event_insert(el, _function, _ctx, &((_ctx)->when), &((_ctx)->ev))) { _rad_panic(__FILE__, __LINE__, "Failed to insert event"); }
90 static fr_packet_list_t *proxy_list = NULL;
93 * We keep the proxy FD's here. The RADIUS Id's are marked
94 * "allocated" per Id, via a bit per proxy FD.
96 static int proxy_fds[32];
97 static rad_listen_t *proxy_listeners[32];
99 #define remove_from_proxy_hash(foo)
102 static void request_post_handler(REQUEST *request);
103 static void wait_a_bit(void *ctx);
104 static void event_socket_handler(fr_event_list_t *xel, UNUSED int fd, void *ctx);
106 static void NEVER_RETURNS _rad_panic(const char *file, unsigned int line,
109 radlog(L_ERR, "[%s:%d] %s", file, line, msg);
113 #define rad_panic(x) _rad_panic(__FILE__, __LINE__, x)
116 static void tv_add(struct timeval *tv, int usec_delay)
118 if (usec_delay > USEC) {
119 tv->tv_sec += usec_delay / USEC;
122 tv->tv_usec += usec_delay;
124 if (tv->tv_usec > USEC) {
130 static void remove_from_request_hash(REQUEST *request)
132 if (!request->in_request_hash) return;
134 fr_packet_list_yank(pl, request->packet);
135 request->in_request_hash = FALSE;
137 request_stats_final(request);
142 static REQUEST *lookup_in_proxy_hash(RADIUS_PACKET *reply)
144 RADIUS_PACKET **proxy_p;
147 PTHREAD_MUTEX_LOCK(&proxy_mutex);
148 proxy_p = fr_packet_list_find_byreply(proxy_list, reply);
151 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
155 request = fr_packet2myptr(REQUEST, proxy, proxy_p);
158 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
162 request->num_proxied_responses++;
165 * Catch the most common case of everything working
168 if (request->num_proxied_requests == request->num_proxied_responses) {
169 fr_packet_list_yank(proxy_list, request->proxy);
170 fr_packet_list_id_free(proxy_list, request->proxy);
171 request->in_proxy_hash = FALSE;
175 * On the FIRST reply, decrement the count of outstanding
176 * requests. Note that this is NOT the count of sent
177 * packets, but whether or not the home server has
180 if (!request->proxy_reply &&
181 request->home_server->currently_outstanding) {
182 request->home_server->currently_outstanding--;
185 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
191 static void remove_from_proxy_hash(REQUEST *request)
193 if (!request->in_proxy_hash) return;
195 PTHREAD_MUTEX_LOCK(&proxy_mutex);
196 fr_packet_list_yank(proxy_list, request->proxy);
197 fr_packet_list_id_free(proxy_list, request->proxy);
200 * The home server hasn't replied, but we've given up on
201 * this request. Don't count this request against the
204 if (!request->proxy_reply &&
205 request->home_server->currently_outstanding) {
206 request->home_server->currently_outstanding--;
209 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
211 request->in_proxy_hash = FALSE;
215 static int insert_into_proxy_hash(REQUEST *request)
220 rad_assert(request->proxy != NULL);
221 rad_assert(proxy_list != NULL);
223 request->proxy->sockfd = -1;
225 PTHREAD_MUTEX_LOCK(&proxy_mutex);
227 request->home_server->currently_outstanding++;
229 if (!fr_packet_list_id_alloc(proxy_list, request->proxy)) {
231 rad_listen_t *proxy_listener;
234 * Allocate a new proxy fd. This function adds
235 * it to the tail of the list of listeners. With
236 * some care, this can be thread-safe.
238 proxy_listener = proxy_new_listener();
239 if (!proxy_listener) {
240 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
241 RDEBUG2("ERROR: Failed to create a new socket for proxying requests.");
249 proxy = proxy_listener->fd;
250 for (i = 0; i < 32; i++) {
252 * Found a free entry. Save the socket,
253 * and remember where we saved it.
255 if (proxy_fds[(proxy + i) & 0x1f] == -1) {
256 found = (proxy + i) & 0x1f;
257 proxy_fds[found] = proxy;
258 proxy_listeners[found] = proxy_listener;
262 rad_assert(found >= 0);
264 if (!fr_packet_list_socket_add(proxy_list, proxy_listener->fd)) {
265 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
266 RDEBUG2("ERROR: Failed to create a new socket for proxying requests.");
271 if (!fr_packet_list_id_alloc(proxy_list, request->proxy)) {
272 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
273 RDEBUG2("ERROR: Failed to create a new socket for proxying requests.");
278 * Signal the main thread to add the new FD to the list
281 radius_signal_self(RADIUS_SIGNAL_SELF_NEW_FD);
283 rad_assert(request->proxy->sockfd >= 0);
286 * FIXME: Hack until we get rid of rad_listen_t, and put
287 * the information into the packet_list.
290 for (i = 0; i < 32; i++) {
291 if (proxy_fds[i] == request->proxy->sockfd) {
298 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
299 RDEBUG2("ERROR: All sockets are full.");
303 rad_assert(proxy_fds[proxy] != -1);
304 rad_assert(proxy_listeners[proxy] != NULL);
305 request->proxy_listener = proxy_listeners[proxy];
307 if (!fr_packet_list_insert(proxy_list, &request->proxy)) {
308 fr_packet_list_id_free(proxy_list, request->proxy);
309 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
310 RDEBUG2("ERROR: Failed to insert entry into proxy list");
314 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
316 RDEBUG3(" proxy: allocating destination %s port %d - Id %d",
317 inet_ntop(request->proxy->dst_ipaddr.af,
318 &request->proxy->dst_ipaddr.ipaddr, buf, sizeof(buf)),
319 request->proxy->dst_port,
322 request->in_proxy_hash = TRUE;
329 * Called as BOTH an event, and in-line from other functions.
331 static void wait_for_proxy_id_to_expire(void *ctx)
333 REQUEST *request = ctx;
334 home_server *home = request->home_server;
336 rad_assert(request->magic == REQUEST_MAGIC);
337 rad_assert(request->proxy != NULL);
339 if (!fr_event_now(el, &now)) gettimeofday(&now, NULL);
340 request->when = request->proxy_when;
341 request->when.tv_sec += home->response_window;
343 if ((request->num_proxied_requests == request->num_proxied_responses) ||
344 timercmp(&now, &request->when, >)) {
345 if (request->packet) {
346 RDEBUG2("Cleaning up request %d ID %d with timestamp +%d",
347 request->number, request->packet->id,
348 (unsigned int) (request->timestamp - fr_start_time));
350 RDEBUG2("Cleaning up request %d with timestamp +%d",
352 (unsigned int) (request->timestamp - fr_start_time));
354 fr_event_delete(el, &request->ev);
355 remove_from_proxy_hash(request);
356 remove_from_request_hash(request);
357 request_free(&request);
361 INSERT_EVENT(wait_for_proxy_id_to_expire, request);
365 #ifdef HAVE_PTHREAD_H
366 static void wait_for_child_to_die(void *ctx)
368 REQUEST *request = ctx;
370 rad_assert(request->magic == REQUEST_MAGIC);
372 if ((request->child_state == REQUEST_QUEUED) |
373 (request->child_state == REQUEST_RUNNING)) {
374 request->delay += (request->delay >> 1);
375 tv_add(&request->when, request->delay);
377 RDEBUG2("Child is still stuck for request %d", request->number);
379 INSERT_EVENT(wait_for_child_to_die, request);
383 RDEBUG2("Child is finally responsive for request %d", request->number);
384 remove_from_request_hash(request);
387 if (request->proxy) {
388 wait_for_proxy_id_to_expire(request);
393 request_free(&request);
397 static void cleanup_delay(void *ctx)
399 REQUEST *request = ctx;
401 rad_assert(request->magic == REQUEST_MAGIC);
402 rad_assert((request->child_state == REQUEST_CLEANUP_DELAY) ||
403 (request->child_state == REQUEST_DONE));
405 remove_from_request_hash(request);
408 if (request->proxy && request->in_proxy_hash) {
409 wait_for_proxy_id_to_expire(request);
414 RDEBUG2("Cleaning up request %d ID %d with timestamp +%d",
415 request->number, request->packet->id,
416 (unsigned int) (request->timestamp - fr_start_time));
418 fr_event_delete(el, &request->ev);
419 request_free(&request);
424 * FIXME: Put into a libradius function.
426 #define MAX_PACKET_CODE (52)
427 static const char *packet_codes[] = {
432 "Accounting-Request",
433 "Accounting-Response",
438 "Accounting-Message",
449 "Resource-Free-Request",
450 "Resource-Free-Response",
451 "Resource-Query-Request",
452 "Resource-Query-Response",
453 "Alternate-Resource-Reclaim-Request",
454 "NAS-Reboot-Request",
455 "NAS-Reboot-Response",
468 "Disconnect-Request",
478 "IP-Address-Allocate",
484 * In daemon mode, AND this request has debug flags set.
486 #define DEBUG_PACKET if (!debug_flag && request->options && request->radlog) debug_packet
488 static void debug_packet(REQUEST *request, RADIUS_PACKET *packet, int direction)
492 const char *received, *from;
493 const fr_ipaddr_t *ip;
498 if (direction == 0) {
499 received = "Received";
500 from = "from"; /* what else? */
501 ip = &packet->src_ipaddr;
502 port = packet->src_port;
505 received = "Sending";
506 from = "to"; /* hah! */
507 ip = &packet->dst_ipaddr;
508 port = packet->dst_port;
512 * Client-specific debugging re-prints the input
513 * packet into the client log.
515 * This really belongs in a utility library
517 if ((packet->code > 0) && (packet->code < MAX_PACKET_CODE)) {
518 RDEBUG("%s %s packet %s host %s port %d, id=%d, length=%d",
519 received, packet_codes[packet->code], from,
520 inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
521 port, packet->id, packet->data_len);
523 RDEBUG("%s packet %s host %s port %d code=%d, id=%d, length=%d",
525 inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
527 packet->code, packet->id, packet->data_len);
530 for (vp = packet->vps; vp != NULL; vp = vp->next) {
531 vp_prints(buffer, sizeof(buffer), vp);
532 request->radlog(L_DBG, 0, request, "\t%s", buffer);
536 static void reject_delay(void *ctx)
538 REQUEST *request = ctx;
540 rad_assert(request->magic == REQUEST_MAGIC);
541 rad_assert(request->child_state == REQUEST_REJECT_DELAY);
543 RDEBUG2("Sending delayed reject for request %d", request->number);
545 DEBUG_PACKET(request, request->reply, 1);
547 request->listener->send(request->listener, request);
549 request->when.tv_sec += request->root->cleanup_delay;
550 request->child_state = REQUEST_CLEANUP_DELAY;
552 INSERT_EVENT(cleanup_delay, request);
557 void revive_home_server(void *ctx)
559 home_server *home = ctx;
562 home->state = HOME_STATE_ALIVE;
563 home->currently_outstanding = 0;
564 home->revive_time = now;
567 * Delete any outstanding events.
569 if (home->ev) fr_event_delete(el, &home->ev);
571 radlog(L_INFO, "PROXY: Marking home server %s port %d alive again... we have no idea if it really is alive or not.",
572 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
573 buffer, sizeof(buffer)),
579 static void no_response_to_ping(void *ctx)
581 REQUEST *request = ctx;
582 home_server *home = request->home_server;
585 home->num_received_pings = 0;
587 RDEBUG2("No response to status check %d from home server %s port %d",
589 inet_ntop(request->proxy->dst_ipaddr.af,
590 &request->proxy->dst_ipaddr.ipaddr,
591 buffer, sizeof(buffer)),
592 request->proxy->dst_port);
594 wait_for_proxy_id_to_expire(request);
598 static void received_response_to_ping(REQUEST *request)
600 home_server *home = request->home_server;
603 home->num_received_pings++;
605 RDEBUG2("Received response to status check %d (%d in current sequence)",
606 request->number, home->num_received_pings);
609 * Remove the request from any hashes
611 fr_event_delete(el, &request->ev);
612 remove_from_proxy_hash(request);
613 rad_assert(request->in_request_hash == FALSE);
614 request_free(&request);
617 * The control socket may have marked the home server as
618 * alive. OR, it may have suddenly started responding to
619 * requests again. If so, don't re-do the "make alive"
622 if (home->state == HOME_STATE_ALIVE) return;
625 * We haven't received enough ping responses to mark it
626 * "alive". Wait a bit.
628 if (home->num_received_pings < home->num_pings_to_alive) {
632 home->state = HOME_STATE_ALIVE;
633 home->currently_outstanding = 0;
634 home->revive_time = now;
636 if (!fr_event_delete(el, &home->ev)) {
637 RDEBUG2("Hmm... no event for home server. Oh well.");
640 radlog(L_INFO, "PROXY: Marking home server %s port %d alive",
641 inet_ntop(request->proxy->dst_ipaddr.af,
642 &request->proxy->dst_ipaddr.ipaddr,
643 buffer, sizeof(buffer)),
644 request->proxy->dst_port);
649 * Called from start of zombie period, OR after control socket
650 * marks the home server dead.
652 static void ping_home_server(void *ctx)
655 home_server *home = ctx;
659 if (home->state == HOME_STATE_ALIVE) {
660 radlog(L_INFO, "Suspicious proxy state... continuing");
664 request = request_alloc();
665 request->number = request_num_counter++;
667 request->proxy = rad_alloc(1);
668 rad_assert(request->proxy != NULL);
670 fr_event_now(el, &request->when);
671 home->when = request->when;
673 if (home->ping_check == HOME_PING_CHECK_STATUS_SERVER) {
674 request->proxy->code = PW_STATUS_SERVER;
676 radius_pairmake(request, &request->proxy->vps,
677 "Message-Authenticator", "0x00", T_OP_SET);
679 } else if (home->type == HOME_TYPE_AUTH) {
680 request->proxy->code = PW_AUTHENTICATION_REQUEST;
682 radius_pairmake(request, &request->proxy->vps,
683 "User-Name", home->ping_user_name, T_OP_SET);
684 radius_pairmake(request, &request->proxy->vps,
685 "User-Password", home->ping_user_password, T_OP_SET);
686 radius_pairmake(request, &request->proxy->vps,
687 "Service-Type", "Authenticate-Only", T_OP_SET);
688 radius_pairmake(request, &request->proxy->vps,
689 "Message-Authenticator", "0x00", T_OP_SET);
692 #ifdef WITH_ACCOUNTING
693 request->proxy->code = PW_ACCOUNTING_REQUEST;
695 radius_pairmake(request, &request->proxy->vps,
696 "User-Name", home->ping_user_name, T_OP_SET);
697 radius_pairmake(request, &request->proxy->vps,
698 "Acct-Status-Type", "Stop", T_OP_SET);
699 radius_pairmake(request, &request->proxy->vps,
700 "Acct-Session-Id", "00000000", T_OP_SET);
701 vp = radius_pairmake(request, &request->proxy->vps,
702 "Event-Timestamp", "0", T_OP_SET);
703 vp->vp_date = now.tv_sec;
705 rad_assert("Internal sanity check failed");
709 radius_pairmake(request, &request->proxy->vps,
710 "NAS-Identifier", "Status Check. Are you alive?",
713 request->proxy->dst_ipaddr = home->ipaddr;
714 request->proxy->dst_port = home->port;
715 request->home_server = home;
717 rad_assert(request->proxy_listener == NULL);
719 if (!insert_into_proxy_hash(request)) {
720 RDEBUG2("ERROR: Failed inserting status check %d into proxy hash. Discarding it.",
722 request_free(&request);
725 rad_assert(request->proxy_listener != NULL);
726 request->proxy_listener->send(request->proxy_listener,
729 request->next_callback = NULL;
730 request->child_state = REQUEST_PROXIED;
731 request->when.tv_sec += home->ping_timeout;;
733 INSERT_EVENT(no_response_to_ping, request);
736 * Add +/- 2s of jitter, as suggested in RFC 3539
737 * and in the Issues and Fixes draft.
739 home->when.tv_sec += home->ping_interval - 2;
742 jitter ^= (jitter >> 10);
743 jitter &= ((1 << 23) - 1); /* 22 bits of 1 */
745 tv_add(&home->when, jitter);
747 INSERT_EVENT(ping_home_server, home);
751 void mark_home_server_dead(home_server *home, struct timeval *when)
753 int previous_state = home->state;
756 radlog(L_INFO, "PROXY: Marking home server %s port %d as dead.",
757 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
758 buffer, sizeof(buffer)),
761 home->state = HOME_STATE_IS_DEAD;
762 home->num_received_pings = 0;
764 if (home->ping_check != HOME_PING_CHECK_NONE) {
766 * If the control socket marks us dead, start
767 * pinging. Otherwise, we already started
768 * pinging when it was marked "zombie".
770 if (previous_state == HOME_STATE_ALIVE) {
771 ping_home_server(home);
776 * Revive it after a fixed period of time. This
777 * is very, very, bad.
780 home->when.tv_sec += home->revive_interval;
782 INSERT_EVENT(revive_home_server, home);
786 static void check_for_zombie_home_server(REQUEST *request)
791 home = request->home_server;
793 if (home->state != HOME_STATE_ZOMBIE) return;
795 when = home->zombie_period_start;
796 when.tv_sec += home->zombie_period;
798 fr_event_now(el, &now);
799 if (timercmp(&now, &when, <)) {
803 mark_home_server_dead(home, &request->when);
806 static int proxy_to_virtual_server(REQUEST *request);
808 static int virtual_server_handler(UNUSED REQUEST *request)
810 proxy_to_virtual_server(request);
814 static void proxy_fallback_handler(REQUEST *request)
817 * A proper time is required for wait_a_bit.
819 request->delay = USEC / 10;
820 gettimeofday(&now, NULL);
821 request->next_when = now;
822 tv_add(&request->next_when, request->delay);
823 request->next_callback = wait_a_bit;
826 * Re-queue the request.
828 request->child_state = REQUEST_QUEUED;
830 rad_assert(request->proxy != NULL);
831 if (!thread_pool_addrequest(request, virtual_server_handler)) {
832 request->child_state = REQUEST_DONE;
835 #ifdef HAVE_PTHREAD_H
837 * MAY free the request if we're over max_request_time,
838 * AND we're not in threaded mode!
840 * Note that we call this ONLY if we're threaded, as
841 * if we're NOT threaded, request_post_handler() calls
842 * wait_a_bit(), which means that "request" may not
845 if (have_children) wait_a_bit(request);
850 static int setup_post_proxy_fail(REQUEST *request)
852 DICT_VALUE *dval = NULL;
855 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
856 dval = dict_valbyname(PW_POST_PROXY_TYPE, "Fail-Authentication");
858 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
859 dval = dict_valbyname(PW_POST_PROXY_TYPE, "Fail-Accounting");
865 if (!dval) dval = dict_valbyname(PW_POST_PROXY_TYPE, "Fail");
868 pairdelete(&request->config_items, PW_POST_PROXY_TYPE);
872 vp = pairfind(request->config_items, PW_POST_PROXY_TYPE);
873 if (!vp) vp = radius_paircreate(request, &request->config_items,
874 PW_POST_PROXY_TYPE, PW_TYPE_INTEGER);
875 vp->vp_integer = dval->value;
877 rad_assert(request->proxy_reply == NULL);
883 static int null_handler(UNUSED REQUEST *request)
888 static void post_proxy_fail_handler(REQUEST *request)
891 * A proper time is required for wait_a_bit.
893 request->delay = USEC / 10;
894 gettimeofday(&now, NULL);
897 * Not set up to run Post-Proxy-Type = Fail.
899 * Mark the request as still running, and figure out what
902 if (!setup_post_proxy_fail(request)) {
903 request->child_state = REQUEST_RUNNING;
904 request_post_handler(request);
908 * Re-queue the request.
910 request->child_state = REQUEST_QUEUED;
913 * There is a post-proxy-type of fail. We run
914 * the request through the pre/post proxy
915 * handlers, just like it was a real proxied
916 * request. However, we set the per-request
917 * handler to NULL, as we don't want to do
920 * Note that when we're not threaded, this will
921 * process the request even if it's greater than
922 * max_request_time. That's not fatal.
924 request->priority = 0;
925 rad_assert(request->proxy != NULL);
926 thread_pool_addrequest(request, null_handler);
930 * MAY free the request if we're over max_request_time,
931 * AND we're not in threaded mode!
933 * Note that we call this ONLY if we're threaded, as
934 * if we're NOT threaded, request_post_handler() calls
935 * wait_a_bit(), which means that "request" may not
938 if (have_children) wait_a_bit(request);
942 /* maybe check this against wait_for_proxy_id_to_expire? */
943 static void no_response_to_proxied_request(void *ctx)
945 REQUEST *request = ctx;
949 rad_assert(request->magic == REQUEST_MAGIC);
950 rad_assert(request->child_state == REQUEST_PROXIED);
953 * If we've failed over to an internal home server,
954 * replace the callback with the correct one. This
955 * is due to locking issues with child threads...
957 if (request->home_server->server) {
962 radlog(L_ERR, "Rejecting request %d due to lack of any response from home server %s port %d",
964 inet_ntop(request->proxy->dst_ipaddr.af,
965 &request->proxy->dst_ipaddr.ipaddr,
966 buffer, sizeof(buffer)),
967 request->proxy->dst_port);
969 check_for_zombie_home_server(request);
971 home = request->home_server;
973 post_proxy_fail_handler(request);
976 * Don't touch request due to race conditions
978 if (home->state == HOME_STATE_IS_DEAD) {
979 rad_assert(home->ev != NULL); /* or it will never wake up */
984 * Enable the zombie period when we notice that the home
985 * server hasn't responded. We do NOT back-date the start
986 * of the zombie period.
988 if (home->state == HOME_STATE_ALIVE) {
989 radlog(L_ERR, "PROXY: Marking home server %s port %d as zombie (it looks like it is dead).",
990 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
991 buffer, sizeof(buffer)),
993 home->state = HOME_STATE_ZOMBIE;
994 home->zombie_period_start = now;
997 * Start pinging the home server.
999 ping_home_server(home);
1004 static void wait_a_bit(void *ctx)
1006 struct timeval when;
1007 REQUEST *request = ctx;
1008 fr_event_callback_t callback = NULL;
1010 rad_assert(request->magic == REQUEST_MAGIC);
1012 switch (request->child_state) {
1013 case REQUEST_QUEUED:
1014 case REQUEST_RUNNING:
1015 when = request->received;
1016 when.tv_sec += request->root->max_request_time;
1019 * Normally called from the event loop with the
1020 * proper event loop time. Otherwise, called from
1021 * post proxy fail handler, which sets "now", and
1022 * this call won't re-set it, because we're not
1023 * in the event loop.
1025 fr_event_now(el, &now);
1028 * Request still has more time. Continue
1031 if (timercmp(&now, &when, <) ||
1032 ((request->listener->type == RAD_LISTEN_DETAIL) &&
1033 (request->child_state == REQUEST_QUEUED))) {
1034 if (request->delay < (USEC / 10)) {
1035 request->delay = USEC / 10;
1037 request->delay += request->delay >> 1;
1041 * Cap wait at some sane value for detail
1044 if ((request->listener->type == RAD_LISTEN_DETAIL) &&
1045 (request->delay > (request->root->max_request_time * USEC))) {
1046 request->delay = request->root->max_request_time * USEC;
1050 request->when = now;
1051 tv_add(&request->when, request->delay);
1052 callback = wait_a_bit;
1056 #if defined(HAVE_PTHREAD_H)
1058 * A child thread MAY still be running on the
1059 * request. Ask the thread to stop working on
1062 if (have_children) {
1063 /* FIXME: kill unresponsive children? */
1066 * Print this error message ONLY if
1067 * there's a child currently processing
1068 * the request. As we don't have thread
1069 * locks here, there may be race
1070 * conditions on this check. But it's
1071 * just an error message, so that's OK.
1073 if (!pthread_equal(request->child_pid, NO_SUCH_CHILD_PID)) {
1074 radlog(L_ERR, "WARNING: Unresponsive child for request %d, in module %s component %s",
1076 request->module ? request->module : "<server core>",
1077 request->component ? request->component : "<server core>");
1080 request->master_state = REQUEST_STOP_PROCESSING;
1082 request->delay = USEC / 4;
1083 tv_add(&request->when, request->delay);
1084 callback = wait_for_child_to_die;
1090 * Else there are no child threads. We probably
1091 * should have just marked the request as 'done'
1092 * elsewhere, like in the post-proxy-fail
1093 * handler. But doing that would involve
1094 * checking for max_request_time in multiple
1095 * places, so this may be simplest.
1097 request->child_state = REQUEST_DONE;
1101 * Mark the request as no longer running,
1105 #ifdef HAVE_PTHREAD_H
1106 request->child_pid = NO_SUCH_CHILD_PID;
1108 request_stats_final(request);
1109 cleanup_delay(request);
1112 case REQUEST_REJECT_DELAY:
1113 case REQUEST_CLEANUP_DELAY:
1114 #ifdef HAVE_PTHREAD_H
1115 request->child_pid = NO_SUCH_CHILD_PID;
1117 request_stats_final(request);
1119 case REQUEST_PROXIED:
1120 rad_assert(request->next_callback != NULL);
1121 rad_assert(request->next_callback != wait_a_bit);
1123 request->when = request->next_when;
1124 callback = request->next_callback;
1125 request->next_callback = NULL;
1129 rad_panic("Internal sanity check failure");
1134 * Something major went wrong. Discard the request, and
1137 * FIXME: No idea why this happens or how to fix it...
1138 * It seems to happen *only* when requests are proxied,
1139 * and where the home server doesn't respond. So it looks
1140 * like a race condition above, but it happens in debug
1141 * mode, with no threads...
1144 RDEBUG("WARNING: Internal sanity check failed in event handler for request %d: Discarding the request!", request->number);
1145 fr_event_delete(el, &request->ev);
1146 remove_from_proxy_hash(request);
1147 remove_from_request_hash(request);
1148 request_free(&request);
1152 INSERT_EVENT(callback, request);
1157 static int process_proxy_reply(REQUEST *request)
1160 int post_proxy_type = 0;
1164 * Delete any reply we had accumulated until now.
1166 pairfree(&request->reply->vps);
1169 * Run the packet through the post-proxy stage,
1170 * BEFORE playing games with the attributes.
1172 vp = pairfind(request->config_items, PW_POST_PROXY_TYPE);
1174 RDEBUG2(" Found Post-Proxy-Type %s", vp->vp_strvalue);
1175 post_proxy_type = vp->vp_integer;
1178 rad_assert(request->home_pool != NULL);
1180 if (request->home_pool->virtual_server) {
1181 const char *old_server = request->server;
1183 request->server = request->home_pool->virtual_server;
1184 RDEBUG2(" server %s {", request->server);
1185 rcode = module_post_proxy(post_proxy_type, request);
1187 request->server = old_server;
1189 rcode = module_post_proxy(post_proxy_type, request);
1193 * There may NOT be a proxy reply, as we may be
1194 * running Post-Proxy-Type = Fail.
1196 if (request->proxy_reply) {
1198 * Delete the Proxy-State Attributes from
1199 * the reply. These include Proxy-State
1200 * attributes from us and remote server.
1202 pairdelete(&request->proxy_reply->vps, PW_PROXY_STATE);
1205 * Add the attributes left in the proxy
1206 * reply to the reply list.
1208 pairadd(&request->reply->vps, request->proxy_reply->vps);
1209 request->proxy_reply->vps = NULL;
1212 * Free proxy request pairs.
1214 pairfree(&request->proxy->vps);
1218 default: /* Don't do anything */
1220 case RLM_MODULE_FAIL:
1221 /* FIXME: debug print stuff */
1222 request->child_state = REQUEST_DONE;
1225 case RLM_MODULE_HANDLED:
1226 /* FIXME: debug print stuff */
1227 request->child_state = REQUEST_DONE;
1235 static int request_pre_handler(REQUEST *request)
1239 rad_assert(request->magic == REQUEST_MAGIC);
1240 rad_assert(request->packet != NULL);
1242 request->child_state = REQUEST_RUNNING;
1245 * Don't decode the packet if it's an internal "fake"
1246 * request. Instead, just return so that the caller can
1249 if (request->packet->dst_port == 0) {
1250 request->username = pairfind(request->packet->vps,
1252 request->password = pairfind(request->packet->vps,
1259 * Put the decoded packet into it's proper place.
1261 if (request->proxy_reply != NULL) {
1262 rcode = request->proxy_listener->decode(request->proxy_listener,
1264 DEBUG_PACKET(request, request->proxy_reply, 0);
1267 if (request->packet->vps == NULL) {
1268 rcode = request->listener->decode(request->listener, request);
1270 if (debug_condition) {
1272 const char *my_debug = debug_condition;
1275 * Ignore parse errors.
1277 radius_evaluate_condition(request, RLM_MODULE_OK, 0,
1281 request->priority = 2;
1282 request->radlog = radlog_request;
1286 DEBUG_PACKET(request, request->packet, 0);
1292 radlog(L_ERR, "%s Dropping packet without response.", fr_strerror());
1293 request->child_state = REQUEST_DONE;
1298 if (!request->proxy)
1301 request->username = pairfind(request->packet->vps,
1306 return process_proxy_reply(request);
1316 * Do state handling when we proxy a request.
1318 static int proxy_request(REQUEST *request)
1320 struct timeval when;
1323 if (request->home_server->server) {
1324 RDEBUG("ERROR: Cannot perform real proxying to a virtual server.");
1328 if (!insert_into_proxy_hash(request)) {
1329 RDEBUG("ERROR: Failed inserting request into proxy hash.");
1333 request->proxy_listener->encode(request->proxy_listener, request);
1335 when = request->received;
1336 when.tv_sec += request->root->max_request_time;
1338 gettimeofday(&request->proxy_when, NULL);
1340 request->next_when = request->proxy_when;
1341 request->next_when.tv_sec += request->home_server->response_window;
1343 rad_assert(request->home_server->response_window > 0);
1345 if (timercmp(&when, &request->next_when, <)) {
1346 request->next_when = when;
1348 request->next_callback = no_response_to_proxied_request;
1350 RDEBUG2("Proxying request %d to home server %s port %d",
1352 inet_ntop(request->proxy->dst_ipaddr.af,
1353 &request->proxy->dst_ipaddr.ipaddr,
1354 buffer, sizeof(buffer)),
1355 request->proxy->dst_port);
1358 * Note that we set proxied BEFORE sending the packet.
1360 * Once we send it, the request is tainted, as
1361 * another thread may have picked it up. Don't
1364 request->num_proxied_requests = 1;
1365 request->num_proxied_responses = 0;
1366 #ifdef HAVE_PTHREAD_H
1367 request->child_pid = NO_SUCH_CHILD_PID;
1369 request->child_state = REQUEST_PROXIED;
1371 DEBUG_PACKET(request, request->proxy, 1);
1373 request->proxy_listener->send(request->proxy_listener,
1380 * "Proxy" the request by sending it to a new virtual server.
1382 static int proxy_to_virtual_server(REQUEST *request)
1385 RAD_REQUEST_FUNP fun;
1387 if (!request->home_server || !request->home_server->server) return 0;
1389 if (request->parent) {
1390 RDEBUG2("WARNING: Cancelling proxy request to virtual server %s as this request was itself proxied.", request->home_server->server);
1394 fake = request_alloc_fake(request);
1396 RDEBUG2("WARNING: Out of memory");
1400 fake->packet->vps = paircopy(request->proxy->vps);
1401 fake->server = request->home_server->server;
1403 if (request->proxy->code == PW_AUTHENTICATION_REQUEST) {
1404 fun = rad_authenticate;
1406 #ifdef WITH_ACCOUNTING
1407 } else if (request->proxy->code == PW_ACCOUNTING_REQUEST) {
1408 fun = rad_accounting;
1412 RDEBUG2("Unknown packet type %d", request->proxy->code);
1413 request_free(&fake);
1417 RDEBUG2(">>> Sending proxied request internally to virtual server.");
1418 radius_handle_request(fake, fun);
1419 RDEBUG2("<<< Received proxied response from internal virtual server.");
1421 request->proxy_reply = fake->reply;
1424 request_free(&fake);
1426 process_proxy_reply(request);
1429 return 2; /* success, but NOT '1' !*/
1434 * Return 1 if we did proxy it, or the proxy attempt failed
1435 * completely. Either way, the caller doesn't touch the request
1436 * any more if we return 1.
1438 static int successfully_proxied_request(REQUEST *request)
1441 int pre_proxy_type = 0;
1442 VALUE_PAIR *realmpair;
1443 VALUE_PAIR *strippedname;
1447 REALM *realm = NULL;
1451 * If it was already proxied, do nothing.
1453 * FIXME: This should really be a serious error.
1455 if (request->in_proxy_hash) {
1459 realmpair = pairfind(request->config_items, PW_PROXY_TO_REALM);
1460 if (!realmpair || (realmpair->length == 0)) {
1464 realmname = (char *) realmpair->vp_strvalue;
1466 realm = realm_find2(realmname);
1468 RDEBUG2("ERROR: Cannot proxy to unknown realm %s", realmname);
1473 * Figure out which pool to use.
1475 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
1476 pool = realm->auth_pool;
1478 #ifdef WITH_ACCOUNTING
1479 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
1480 pool = realm->acct_pool;
1484 rad_panic("Internal sanity check failed");
1488 RDEBUG2(" WARNING: Cancelling proxy to Realm %s, as the realm is local.",
1493 home = home_server_ldb(realmname, pool, request);
1495 RDEBUG2("ERROR: Failed to find live home server for realm %s",
1499 request->home_pool = pool;
1502 * Remember that we sent the request to a Realm.
1504 pairadd(&request->packet->vps,
1505 pairmake("Realm", realmname, T_OP_EQ));
1509 * We read the packet from a detail file, AND it came from
1510 * the server we're about to send it to. Don't do that.
1512 if ((request->packet->code == PW_ACCOUNTING_REQUEST) &&
1513 (request->listener->type == RAD_LISTEN_DETAIL) &&
1514 (home->ipaddr.af == AF_INET) &&
1515 (request->packet->src_ipaddr.af == AF_INET) &&
1516 (home->ipaddr.ipaddr.ip4addr.s_addr == request->packet->src_ipaddr.ipaddr.ip4addr.s_addr)) {
1517 RDEBUG2(" rlm_realm: Packet came from realm %s, proxy cancelled", realmname);
1523 * Allocate the proxy packet, only if it wasn't already
1524 * allocated by a module. This check is mainly to support
1525 * the proxying of EAP-TTLS and EAP-PEAP tunneled requests.
1527 * In those cases, the EAP module creates a "fake"
1528 * request, and recursively passes it through the
1529 * authentication stage of the server. The module then
1530 * checks if the request was supposed to be proxied, and
1531 * if so, creates a proxy packet from the TUNNELED request,
1532 * and not from the EAP request outside of the tunnel.
1534 * The proxy then works like normal, except that the response
1535 * packet is "eaten" by the EAP module, and encapsulated into
1538 if (!request->proxy) {
1539 if ((request->proxy = rad_alloc(TRUE)) == NULL) {
1540 radlog(L_ERR|L_CONS, "no memory");
1545 * Copy the request, then look up name and
1546 * plain-text password in the copy.
1548 * Note that the User-Name attribute is the
1549 * *original* as sent over by the client. The
1550 * Stripped-User-Name attribute is the one hacked
1551 * through the 'hints' file.
1553 request->proxy->vps = paircopy(request->packet->vps);
1557 * Strip the name, if told to.
1559 * Doing it here catches the case of proxied tunneled
1562 if (realm->striprealm == TRUE &&
1563 (strippedname = pairfind(request->proxy->vps, PW_STRIPPED_USER_NAME)) != NULL) {
1565 * If there's a Stripped-User-Name attribute in
1566 * the request, then use THAT as the User-Name
1567 * for the proxied request, instead of the
1570 * This is done by making a copy of the
1571 * Stripped-User-Name attribute, turning it into
1572 * a User-Name attribute, deleting the
1573 * Stripped-User-Name and User-Name attributes
1574 * from the vps list, and making the new
1575 * User-Name the head of the vps list.
1577 vp = pairfind(request->proxy->vps, PW_USER_NAME);
1579 vp = radius_paircreate(request, NULL,
1580 PW_USER_NAME, PW_TYPE_STRING);
1581 rad_assert(vp != NULL); /* handled by above function */
1582 /* Insert at the START of the list */
1583 vp->next = request->proxy->vps;
1584 request->proxy->vps = vp;
1586 memcpy(vp->vp_strvalue, strippedname->vp_strvalue,
1587 sizeof(vp->vp_strvalue));
1588 vp->length = strippedname->length;
1591 * Do NOT delete Stripped-User-Name.
1596 * If there is no PW_CHAP_CHALLENGE attribute but
1597 * there is a PW_CHAP_PASSWORD we need to add it
1598 * since we can't use the request authenticator
1599 * anymore - we changed it.
1601 if (pairfind(request->proxy->vps, PW_CHAP_PASSWORD) &&
1602 pairfind(request->proxy->vps, PW_CHAP_CHALLENGE) == NULL) {
1603 vp = radius_paircreate(request, &request->proxy->vps,
1604 PW_CHAP_CHALLENGE, PW_TYPE_OCTETS);
1605 vp->length = AUTH_VECTOR_LEN;
1606 memcpy(vp->vp_strvalue, request->packet->vector, AUTH_VECTOR_LEN);
1610 * The RFC's say we have to do this, but FreeRADIUS
1613 vp = radius_paircreate(request, &request->proxy->vps,
1614 PW_PROXY_STATE, PW_TYPE_OCTETS);
1615 snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%d",
1616 request->packet->id);
1617 vp->length = strlen(vp->vp_strvalue);
1620 * Should be done BEFORE inserting into proxy hash, as
1621 * pre-proxy may use this information, or change it.
1623 request->proxy->code = request->packet->code;
1624 request->proxy->dst_ipaddr = home->ipaddr;
1625 request->proxy->dst_port = home->port;
1626 request->home_server = home;
1629 * Call the pre-proxy routines.
1631 vp = pairfind(request->config_items, PW_PRE_PROXY_TYPE);
1633 RDEBUG2(" Found Pre-Proxy-Type %s", vp->vp_strvalue);
1634 pre_proxy_type = vp->vp_integer;
1637 rad_assert(request->home_pool != NULL);
1639 if (request->home_pool->virtual_server) {
1640 const char *old_server = request->server;
1642 request->server = request->home_pool->virtual_server;
1643 RDEBUG2(" server %s {", request->server);
1644 rcode = module_pre_proxy(pre_proxy_type, request);
1646 request->server = old_server;
1648 rcode = module_pre_proxy(pre_proxy_type, request);
1651 case RLM_MODULE_FAIL:
1652 case RLM_MODULE_INVALID:
1653 case RLM_MODULE_NOTFOUND:
1654 case RLM_MODULE_USERLOCK:
1656 /* FIXME: debug print failed stuff */
1659 case RLM_MODULE_REJECT:
1660 case RLM_MODULE_HANDLED:
1664 * Only proxy the packet if the pre-proxy code succeeded.
1666 case RLM_MODULE_NOOP:
1668 case RLM_MODULE_UPDATED:
1673 * If it's a fake request, don't send the proxy
1674 * packet. The outer tunnel session will take
1675 * care of doing that.
1677 if (request->packet->dst_port == 0) {
1678 request->home_server = NULL;
1682 if (request->home_server->server) {
1683 return proxy_to_virtual_server(request);
1686 if (!proxy_request(request)) {
1687 RDEBUG("ERROR: Failed to proxy request %d", request->number);
1696 static void request_post_handler(REQUEST *request)
1698 int child_state = -1;
1699 struct timeval when;
1702 if ((request->master_state == REQUEST_STOP_PROCESSING) ||
1704 (request->parent->master_state == REQUEST_STOP_PROCESSING))) {
1705 RDEBUG2("Request %d was cancelled.", request->number);
1706 #ifdef HAVE_PTHREAD_H
1707 request->child_pid = NO_SUCH_CHILD_PID;
1709 request->child_state = REQUEST_DONE;
1713 if (request->child_state != REQUEST_RUNNING) {
1714 rad_panic("Internal sanity check failed");
1717 if ((request->reply->code == 0) &&
1718 ((vp = pairfind(request->config_items, PW_AUTH_TYPE)) != NULL) &&
1719 (vp->vp_integer == PW_AUTHTYPE_REJECT)) {
1720 request->reply->code = PW_AUTHENTICATION_REJECT;
1724 if (request->root->proxy_requests &&
1725 !request->in_proxy_hash &&
1726 (request->reply->code == 0) &&
1727 (request->packet->dst_port != 0) &&
1728 (request->packet->code != PW_STATUS_SERVER)) {
1729 int rcode = successfully_proxied_request(request);
1731 if (rcode == 1) return;
1734 * Failed proxying it (dead home servers, etc.)
1735 * Run it through Post-Proxy-Type = Fail, and
1736 * respond to the request.
1738 * Note that we're in a child thread here, so we
1739 * do NOT re-schedule the request. Instead, we
1740 * do what we would have done, which is run the
1741 * pre-handler, a NULL request handler, and then
1744 if ((rcode < 0) && setup_post_proxy_fail(request)) {
1745 request_pre_handler(request);
1749 * Else we weren't supposed to proxy it,
1750 * OR we proxied it internally to a virutal server.
1756 * Fake requests don't get encoded or signed. The caller
1757 * also requires the reply VP's, so we don't free them
1760 if (request->packet->dst_port == 0) {
1761 /* FIXME: RDEBUG going to the next request */
1762 #ifdef HAVE_PTHREAD_H
1763 request->child_pid = NO_SUCH_CHILD_PID;
1765 request->child_state = REQUEST_DONE;
1771 * Copy Proxy-State from the request to the reply.
1773 vp = paircopy2(request->packet->vps, PW_PROXY_STATE);
1774 if (vp) pairadd(&request->reply->vps, vp);
1778 * Access-Requests get delayed or cached.
1780 switch (request->packet->code) {
1781 case PW_AUTHENTICATION_REQUEST:
1782 gettimeofday(&request->next_when, NULL);
1784 if (request->reply->code == 0) {
1786 * Check if the lack of response is intentional.
1788 vp = pairfind(request->config_items,
1789 PW_RESPONSE_PACKET_TYPE);
1791 RDEBUG2("There was no response configured: rejecting request %d",
1793 request->reply->code = PW_AUTHENTICATION_REJECT;
1794 } else if (vp->vp_integer == 256) {
1795 RDEBUG2("Not responding to request %d",
1799 request->reply->code = vp->vp_integer;
1805 * Run rejected packets through
1807 * Post-Auth-Type = Reject
1809 if (request->reply->code == PW_AUTHENTICATION_REJECT) {
1810 pairdelete(&request->config_items, PW_POST_AUTH_TYPE);
1811 vp = radius_pairmake(request, &request->config_items,
1812 "Post-Auth-Type", "Reject",
1814 if (vp) rad_postauth(request);
1817 * If configured, delay Access-Reject packets.
1819 * If request->root->reject_delay = 0, we discover
1820 * that we have to send the packet now.
1822 when = request->received;
1823 when.tv_sec += request->root->reject_delay;
1825 if (timercmp(&when, &request->next_when, >)) {
1826 RDEBUG2("Delaying reject of request %d for %d seconds",
1828 request->root->reject_delay);
1829 request->next_when = when;
1830 request->next_callback = reject_delay;
1831 #ifdef HAVE_PTHREAD_H
1832 request->child_pid = NO_SUCH_CHILD_PID;
1834 request->child_state = REQUEST_REJECT_DELAY;
1839 request->next_when.tv_sec += request->root->cleanup_delay;
1840 request->next_callback = cleanup_delay;
1841 child_state = REQUEST_CLEANUP_DELAY;
1844 case PW_ACCOUNTING_REQUEST:
1845 request->next_callback = NULL; /* just to be safe */
1846 child_state = REQUEST_DONE;
1850 * FIXME: Status-Server should probably not be
1853 case PW_STATUS_SERVER:
1854 request->next_callback = NULL;
1855 child_state = REQUEST_DONE;
1859 if ((request->packet->code > 1024) &&
1860 (request->packet->code < (1024 + 254 + 1))) {
1861 request->next_callback = NULL;
1862 child_state = REQUEST_DONE;
1866 radlog(L_ERR, "Unknown packet type %d", request->packet->code);
1867 rad_panic("Unknown packet type");
1872 * Suppress "no reply" packets here, unless we're reading
1873 * from the "detail" file. In that case, we've got to
1874 * tell the detail file handler that the request is dead,
1875 * and it should re-send it.
1876 * If configured, encode, sign, and send.
1878 if ((request->reply->code != 0) ||
1879 (request->listener->type == RAD_LISTEN_DETAIL)) {
1880 DEBUG_PACKET(request, request->reply, 1);
1881 request->listener->send(request->listener, request);
1885 * Clean up. These are no longer needed.
1887 pairfree(&request->config_items);
1889 pairfree(&request->packet->vps);
1890 request->username = NULL;
1891 request->password = NULL;
1893 pairfree(&request->reply->vps);
1896 if (request->proxy) {
1897 pairfree(&request->proxy->vps);
1899 if (request->proxy_reply) {
1900 pairfree(&request->proxy_reply->vps);
1905 * We're not tracking responses from the home
1906 * server, we can therefore free this memory in
1909 if (!request->in_proxy_hash) {
1910 rad_free(&request->proxy);
1911 rad_free(&request->proxy_reply);
1912 request->home_server = NULL;
1918 RDEBUG2("Finished request %d.", request->number);
1920 request->child_state = child_state;
1923 * Single threaded mode: update timers now.
1925 if (!have_children) wait_a_bit(request);
1929 static void received_retransmit(REQUEST *request, const RADCLIENT *client)
1935 RAD_STATS_TYPE_INC(request->listener, total_dup_requests);
1936 RAD_STATS_CLIENT_INC(request->listener, client, total_dup_requests);
1938 switch (request->child_state) {
1939 case REQUEST_QUEUED:
1940 case REQUEST_RUNNING:
1944 radlog(L_ERR, "Discarding duplicate request from "
1945 "client %s port %d - ID: %d due to unfinished request %d",
1947 request->packet->src_port,request->packet->id,
1952 case REQUEST_PROXIED:
1954 * We're not supposed to have duplicate
1955 * accounting packets. The other states handle
1956 * duplicates fine (discard, or send duplicate
1957 * reply). But we do NOT want to retransmit an
1958 * accounting request here, because that would
1959 * involve updating the Acct-Delay-Time, and
1960 * therefore changing the packet Id, etc.
1962 * Instead, we just discard the packet. We may
1963 * eventually respond, or the client will send a
1964 * new accounting packet.
1966 if (request->packet->code == PW_ACCOUNTING_REQUEST) {
1970 check_for_zombie_home_server(request);
1973 * If we've just discovered that the home server is
1974 * dead, send the packet to another one.
1976 if ((request->packet->dst_port != 0) &&
1977 (request->home_server->state == HOME_STATE_IS_DEAD)) {
1980 remove_from_proxy_hash(request);
1982 home = home_server_ldb(NULL, request->home_pool, request);
1984 RDEBUG2("Failed to find live home server for request %d", request->number);
1987 * Do post-request processing,
1988 * and any insertion of necessary
1991 post_proxy_fail_handler(request);
1995 request->proxy->code = request->packet->code;
1996 request->proxy->dst_ipaddr = home->ipaddr;
1997 request->proxy->dst_port = home->port;
1998 request->home_server = home;
2001 * Free the old packet, to force re-encoding
2003 free(request->proxy->data);
2004 request->proxy->data = NULL;
2005 request->proxy->data_len = 0;
2008 * This request failed over to a virtual
2009 * server. Push it back onto the queue
2012 if (request->home_server->server) {
2013 proxy_fallback_handler(request);
2018 * Try to proxy the request.
2020 if (!proxy_request(request)) {
2021 RDEBUG("ERROR: Failed to re-proxy request %d", request->number);
2022 goto no_home_servers;
2026 * This code executes in the main server
2027 * thread, so there's no need for locking.
2029 rad_assert(request->next_callback != NULL);
2030 INSERT_EVENT(request->next_callback, request);
2031 request->next_callback = NULL;
2033 } /* else the home server is still alive */
2035 RDEBUG2("Sending duplicate proxied request to home server %s port %d - ID: %d",
2036 inet_ntop(request->proxy->dst_ipaddr.af,
2037 &request->proxy->dst_ipaddr.ipaddr,
2038 buffer, sizeof(buffer)),
2039 request->proxy->dst_port,
2040 request->proxy->id);
2041 request->num_proxied_requests++;
2043 DEBUG_PACKET(request, request->proxy, 1);
2044 request->proxy_listener->send(request->proxy_listener,
2049 case REQUEST_REJECT_DELAY:
2050 RDEBUG2("Waiting to send Access-Reject "
2051 "to client %s port %d - ID: %d",
2053 request->packet->src_port, request->packet->id);
2056 case REQUEST_CLEANUP_DELAY:
2058 RDEBUG2("Sending duplicate reply "
2059 "to client %s port %d - ID: %d",
2061 request->packet->src_port, request->packet->id);
2062 DEBUG_PACKET(request, request->reply, 1);
2063 request->listener->send(request->listener, request);
2069 static void received_conflicting_request(REQUEST *request,
2070 const RADCLIENT *client)
2072 radlog(L_ERR, "Received conflicting packet from "
2073 "client %s port %d - ID: %d due to unfinished request %d. Giving up on old request.",
2075 request->packet->src_port, request->packet->id,
2079 * Nuke it from the request hash, so we can receive new
2082 remove_from_request_hash(request);
2084 switch (request->child_state) {
2085 #ifdef HAVE_PTHREAD_H
2087 * It's queued or running. Tell it to stop, and
2088 * wait for it to do so.
2090 case REQUEST_QUEUED:
2091 case REQUEST_RUNNING:
2092 request->master_state = REQUEST_STOP_PROCESSING;
2093 request->delay += request->delay >> 1;
2095 tv_add(&request->when, request->delay);
2097 INSERT_EVENT(wait_for_child_to_die, request);
2102 * It's in some other state, and therefore also
2103 * in the event queue. At some point, the
2104 * child will notice, and we can then delete it.
2107 rad_assert(request->ev != NULL);
2113 static int can_handle_new_request(RADIUS_PACKET *packet,
2115 struct main_config_t *root)
2118 * Count the total number of requests, to see if
2119 * there are too many. If so, return with an
2122 if (root->max_requests) {
2123 int request_count = fr_packet_list_num_elements(pl);
2126 * This is a new request. Let's see if
2127 * it makes us go over our configured
2130 if (request_count > root->max_requests) {
2131 radlog(L_ERR, "Dropping request (%d is too many): "
2132 "from client %s port %d - ID: %d", request_count,
2134 packet->src_port, packet->id);
2135 radlog(L_INFO, "WARNING: Please check the configuration file.\n"
2136 "\tThe value for 'max_requests' is probably set too low.\n");
2138 } /* else there were a small number of requests */
2139 } /* else there was no configured limit for requests */
2142 * FIXME: Add per-client checks. If one client is sending
2143 * too many packets, start discarding them.
2145 * We increment the counters here, and decrement them
2146 * when the response is sent... somewhere in this file.
2150 * FUTURE: Add checks for system load. If the system is
2151 * busy, start dropping requests...
2153 * We can probably keep some statistics ourselves... if
2154 * there are more requests coming in than we can handle,
2155 * start dropping some.
2162 int received_request(rad_listen_t *listener,
2163 RADIUS_PACKET *packet, REQUEST **prequest,
2166 RADIUS_PACKET **packet_p;
2167 REQUEST *request = NULL;
2168 struct main_config_t *root = &mainconfig;
2170 packet_p = fr_packet_list_find(pl, packet);
2172 request = fr_packet2myptr(REQUEST, packet, packet_p);
2173 rad_assert(request->in_request_hash);
2175 if ((request->packet->data_len == packet->data_len) &&
2176 (memcmp(request->packet->vector, packet->vector,
2177 sizeof(packet->vector)) == 0)) {
2178 received_retransmit(request, client);
2183 * The new request is different from the old one,
2184 * but maybe the old is finished. If so, delete
2187 switch (request->child_state) {
2188 struct timeval when;
2191 gettimeofday(&when, NULL);
2195 * If the cached request was received
2196 * within the last second, then we
2197 * discard the NEW request instead of the
2198 * old one. This will happen ONLY when
2199 * the client is severely broken, and is
2200 * sending conflicting packets very
2203 if (timercmp(&when, &request->received, <)) {
2204 radlog(L_ERR, "Discarding conflicting packet from "
2205 "client %s port %d - ID: %d due to recent request %d.",
2207 packet->src_port, packet->id,
2212 received_conflicting_request(request, client);
2216 case REQUEST_REJECT_DELAY:
2217 case REQUEST_CLEANUP_DELAY:
2218 request->child_state = REQUEST_DONE;
2220 cleanup_delay(request);
2227 * We may want to quench the new request.
2229 if ((listener->type != RAD_LISTEN_DETAIL) &&
2230 !can_handle_new_request(packet, client, root)) {
2235 * Create and initialize the new request.
2237 request = request_alloc(); /* never fails */
2239 if ((request->reply = rad_alloc(0)) == NULL) {
2240 radlog(L_ERR, "No memory");
2244 request->listener = listener;
2245 request->client = client;
2246 request->packet = packet;
2247 request->packet->timestamp = request->timestamp;
2248 request->number = request_num_counter++;
2249 request->priority = listener->type;
2250 #ifdef HAVE_PTHREAD_H
2251 request->child_pid = NO_SUCH_CHILD_PID;
2255 * Status-Server packets go to the head of the queue.
2257 if (request->packet->code == PW_STATUS_SERVER) request->priority = 0;
2260 * Set virtual server identity
2262 if (client->server) {
2263 request->server = client->server;
2264 } else if (listener->server) {
2265 request->server = listener->server;
2267 request->server = NULL;
2271 * Remember the request in the list.
2273 if (!fr_packet_list_insert(pl, &request->packet)) {
2274 radlog(L_ERR, "Failed to insert request %d in the list of live requests: discarding", request->number);
2275 request_free(&request);
2279 request->in_request_hash = TRUE;
2280 request->root = root;
2284 * The request passes many of our sanity checks.
2285 * From here on in, if anything goes wrong, we
2286 * send a reject message, instead of dropping the
2291 * Build the reply template from the request.
2294 request->reply->sockfd = request->packet->sockfd;
2295 request->reply->dst_ipaddr = request->packet->src_ipaddr;
2296 request->reply->src_ipaddr = request->packet->dst_ipaddr;
2297 request->reply->dst_port = request->packet->src_port;
2298 request->reply->src_port = request->packet->dst_port;
2299 request->reply->id = request->packet->id;
2300 request->reply->code = 0; /* UNKNOWN code */
2301 memcpy(request->reply->vector, request->packet->vector,
2302 sizeof(request->reply->vector));
2303 request->reply->vps = NULL;
2304 request->reply->data = NULL;
2305 request->reply->data_len = 0;
2307 request->master_state = REQUEST_ACTIVE;
2308 request->child_state = REQUEST_QUEUED;
2309 request->next_callback = NULL;
2311 gettimeofday(&request->received, NULL);
2312 request->timestamp = request->received.tv_sec;
2313 request->when = request->received;
2315 request->delay = USEC;
2317 tv_add(&request->when, request->delay);
2319 INSERT_EVENT(wait_a_bit, request);
2321 *prequest = request;
2327 REQUEST *received_proxy_response(RADIUS_PACKET *packet)
2333 if (!home_server_find(&packet->src_ipaddr, packet->src_port)) {
2334 radlog(L_ERR, "Ignoring request from unknown home server %s port %d",
2335 inet_ntop(packet->src_ipaddr.af,
2336 &packet->src_ipaddr.ipaddr,
2337 buffer, sizeof(buffer)),
2344 * Also removes from the proxy hash if responses == requests
2346 request = lookup_in_proxy_hash(packet);
2349 radlog(L_PROXY, "No outstanding request was found for proxy reply from home server %s port %d - ID %d",
2350 inet_ntop(packet->src_ipaddr.af,
2351 &packet->src_ipaddr.ipaddr,
2352 buffer, sizeof(buffer)),
2353 packet->src_port, packet->id);
2358 home = request->home_server;
2360 gettimeofday(&now, NULL);
2363 * FIXME: mark the home server alive?
2365 home->state = HOME_STATE_ALIVE;
2367 if (request->reply && request->reply->code != 0) {
2368 RDEBUG2("We already replied to this request. Discarding response from home server.");
2374 * We had previously received a reply, so we don't need
2375 * to do anything here.
2377 if (request->proxy_reply) {
2378 if (memcmp(request->proxy_reply->vector,
2380 sizeof(request->proxy_reply->vector)) == 0) {
2381 RDEBUG2("Discarding duplicate reply from home server %s port %d - ID: %d for request %d",
2382 inet_ntop(packet->src_ipaddr.af,
2383 &packet->src_ipaddr.ipaddr,
2384 buffer, sizeof(buffer)),
2385 packet->src_port, packet->id,
2389 * ? The home server gave us a new proxy
2390 * reply, which doesn't match the old
2393 RDEBUG2("Ignoring conflicting proxy reply");
2396 /* assert that there's an event queued for request? */
2402 * The average includes our time to receive packets and
2403 * look them up in the hashes, which should be the same
2406 * We update the response time only for the FIRST packet
2409 else if (home->ema.window > 0) {
2410 radius_stats_ema(&home->ema, &now, &request->proxy_when);
2415 switch (request->child_state) {
2416 case REQUEST_QUEUED:
2417 case REQUEST_RUNNING:
2418 rad_panic("Internal sanity check failed for child state");
2421 case REQUEST_REJECT_DELAY:
2422 case REQUEST_CLEANUP_DELAY:
2424 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'",
2425 inet_ntop(packet->src_ipaddr.af,
2426 &packet->src_ipaddr.ipaddr,
2427 buffer, sizeof(buffer)),
2428 packet->src_port, packet->id,
2430 /* assert that there's an event queued for request? */
2434 case REQUEST_PROXIED:
2438 request->proxy_reply = packet;
2442 * Perform RTT calculations, as per RFC 2988 (for TCP).
2443 * Note that we only do so on the first response.
2445 if ((request->num_proxied_responses == 1)
2447 home_server *home = request->home_server;
2449 rtt = now.tv_sec - request->proxy_when.tv_sec;
2452 rtt -= request->proxy_when.tv_usec;
2454 if (!home->has_rtt) {
2455 home->has_rtt = TRUE;
2458 home->rttvar = rtt / 2;
2461 home->rttvar -= home->rttvar >> 2;
2462 home->rttvar += (home->srtt - rtt);
2463 home->srtt -= home->srtt >> 3;
2464 home->srtt += rtt >> 3;
2467 home->rto = home->srtt;
2468 if (home->rttvar > (USEC / 4)) {
2469 home->rto += home->rttvar * 4;
2477 * There's no incoming request, so it's a proxied packet
2480 if (!request->packet) {
2481 received_response_to_ping(request);
2485 request->child_state = REQUEST_QUEUED;
2486 request->when = now;
2487 request->delay = USEC;
2488 request->priority = RAD_LISTEN_PROXY;
2489 tv_add(&request->when, request->delay);
2492 * Wait a bit will take care of max_request_time
2494 INSERT_EVENT(wait_a_bit, request);
2500 void event_new_fd(rad_listen_t *this)
2504 this->print(this, buffer, sizeof(buffer));
2506 if (this->status == RAD_LISTEN_STATUS_INIT) {
2508 DEBUG("Listening on %s", buffer);
2510 DEBUG2(" ... adding new socket %s", buffer);
2512 if (!fr_event_fd_insert(el, 0, this->fd,
2513 event_socket_handler, this)) {
2514 radlog(L_ERR, "Failed remembering handle for proxy socket!");
2518 this->status = RAD_LISTEN_STATUS_KNOWN;
2522 if (this->status == RAD_LISTEN_STATUS_CLOSED) {
2523 DEBUG2(" ... closing socket %s", buffer);
2525 fr_event_fd_delete(el, 0, this->fd);
2526 this->status = RAD_LISTEN_STATUS_FINISH;
2529 * Close the fd AFTER fixing up the requests and
2530 * listeners, so that they don't send/recv on the
2531 * wrong socket (if someone manages to open
2540 static void event_detail_timer(void *ctx)
2542 rad_listen_t *listener = ctx;
2543 RAD_REQUEST_FUNP fun;
2546 if (listener->recv(listener, &fun, &request)) {
2547 if (!thread_pool_addrequest(request, fun)) {
2548 request->child_state = REQUEST_DONE;
2554 static void handle_signal_self(int flag)
2556 if ((flag & (RADIUS_SIGNAL_SELF_EXIT | RADIUS_SIGNAL_SELF_TERM)) != 0) {
2557 if ((flag & RADIUS_SIGNAL_SELF_EXIT) != 0) {
2558 fr_event_loop_exit(el, 1);
2560 fr_event_loop_exit(el, 2);
2564 } /* else exit/term flags weren't set */
2567 * Tell the even loop to stop processing.
2569 if ((flag & RADIUS_SIGNAL_SELF_HUP) != 0) {
2571 static time_t last_hup = 0;
2573 DEBUG("Received HUP signal.");
2576 if ((int) (when - last_hup) < 5) {
2577 radlog(L_INFO, "Ignoring HUP (less than 5s since last one)");
2582 fr_event_loop_exit(el, 0x80);
2586 if ((flag & RADIUS_SIGNAL_SELF_DETAIL) != 0) {
2589 for (this = mainconfig.listen;
2591 this = this->next) {
2593 struct timeval when;
2595 if (this->type != RAD_LISTEN_DETAIL) continue;
2597 delay = detail_delay(this);
2598 if (!delay) continue;
2600 fr_event_now(el, &now);
2602 tv_add(&when, delay);
2604 if (delay > 100000) {
2605 DEBUG("Delaying next detail event for %d.%01u seconds.",
2606 delay / USEC, (delay % USEC) / 100000);
2609 if (!fr_event_insert(el, event_detail_timer, this,
2611 radlog(L_ERR, "Failed remembering timer");
2618 if ((flag & RADIUS_SIGNAL_SELF_NEW_FD) != 0) {
2621 for (this = mainconfig.listen;
2623 this = this->next) {
2624 if (this->status == RAD_LISTEN_STATUS_KNOWN) continue;
2632 void radius_signal_self(int flag)
2634 handle_signal_self(flag);
2638 * Inform ourselves that we received a signal.
2640 void radius_signal_self(int flag)
2646 * The read MUST be non-blocking for this to work.
2648 rcode = read(self_pipe[0], buffer, sizeof(buffer));
2652 for (i = 0; i < rcode; i++) {
2653 buffer[0] |= buffer[i];
2661 write(self_pipe[1], buffer, 1);
2665 static void event_signal_handler(UNUSED fr_event_list_t *xel,
2666 UNUSED int fd, UNUSED void *ctx)
2671 rcode = read(self_pipe[0], buffer, sizeof(buffer));
2672 if (rcode <= 0) return;
2675 * Merge pending signals.
2677 for (i = 0; i < rcode; i++) {
2678 buffer[0] |= buffer[i];
2681 handle_signal_self(buffer[0]);
2686 static void event_socket_handler(fr_event_list_t *xel, UNUSED int fd,
2689 rad_listen_t *listener = ctx;
2690 RAD_REQUEST_FUNP fun;
2693 rad_assert(xel == el);
2697 if (listener->fd < 0) rad_panic("Socket was closed on us!");
2699 if (!listener->recv(listener, &fun, &request)) return;
2701 if (!thread_pool_addrequest(request, fun)) {
2702 request->child_state = REQUEST_DONE;
2708 * This function is called periodically to see if any FD's are
2709 * available for reading.
2711 static void event_poll_detail(UNUSED void *ctx)
2714 RAD_REQUEST_FUNP fun;
2717 struct timeval when;
2719 fr_event_now(el, &now);
2723 for (this = mainconfig.listen; this != NULL; this = this->next) {
2724 if (this->type != RAD_LISTEN_DETAIL) continue;
2726 if (this->fd >= 0) continue;
2729 * Try to read something.
2731 * FIXME: This does poll AND receive.
2733 rcode = this->recv(this, &fun, &request);
2734 if (!rcode) continue;
2736 rad_assert(fun != NULL);
2737 rad_assert(request != NULL);
2739 if (!thread_pool_addrequest(request, fun)) {
2740 request->child_state = REQUEST_DONE;
2747 if (!fr_event_insert(el, event_poll_detail, NULL,
2749 radlog(L_ERR, "Failed creating handler");
2755 static void event_status(struct timeval *wake)
2757 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
2761 if (debug_flag == 0) {
2763 radlog(L_INFO, "Ready to process requests.");
2764 just_started = FALSE;
2770 DEBUG("Ready to process requests.");
2772 } else if ((wake->tv_sec != 0) ||
2773 (wake->tv_usec >= 100000)) {
2774 DEBUG("Waking up in %d.%01u seconds.",
2775 (int) wake->tv_sec, (unsigned int) wake->tv_usec / 100000);
2780 * FIXME: Put this somewhere else, where it isn't called
2781 * all of the time...
2784 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
2786 * If there are no child threads, then there may
2787 * be child processes. In that case, wait for
2788 * their exit status, and throw that exit status
2789 * away. This helps get rid of zxombie children.
2791 while (waitpid(-1, &argval, WNOHANG) > 0) {
2798 #if defined(HAVE_SETRESUID) && defined (HAVE_GETRESUID)
2799 static void fr_suid_up(void)
2801 uid_t ruid, euid, suid;
2803 if (getresuid(&ruid, &euid, &suid) < 0) {
2804 radlog(L_ERR, "Failed getting saved UID's");
2808 if (setresuid(-1, suid, -1) < 0) {
2809 radlog(L_ERR, "Failed switching to privileged user");
2813 if (geteuid() != suid) {
2814 radlog(L_ERR, "Switched to unknown UID");
2819 extern uid_t server_uid;
2820 extern int did_setuid;
2821 static void fr_suid_down(void)
2823 uid_t ruid, euid, suid;
2825 if (!did_setuid) return;
2827 if (getresuid(&ruid, &euid, &suid) < 0) {
2828 radlog(L_ERR, "Failed getting saved UID's");
2832 if (setresuid(server_uid, server_uid, server_uid) < 0) {
2833 radlog(L_ERR, "Failed to permanently switch UID to %u: %s",
2834 server_uid, strerror(errno));
2838 if (geteuid() != server_uid) {
2839 radlog(L_ERR, "Switched to unknown UID");
2844 if (getresuid(&ruid, &euid, &suid) < 0) {
2845 radlog(L_ERR, "Failed getting saved UID's: %s",
2852 * Much less secure...
2854 #define fr_suid_up()
2855 #define fr_suid_down()
2860 * Externally-visibly functions.
2862 int radius_event_init(CONF_SECTION *cs, int spawn_flag)
2864 rad_listen_t *this, *head = NULL;
2868 time(&fr_start_time);
2870 el = fr_event_list_create(event_status);
2873 pl = fr_packet_list_create(0);
2874 if (!pl) return 0; /* leak el */
2876 request_num_counter = 0;
2879 * Move all of the thread calls to this file?
2881 * It may be best for the mutexes to be in this file...
2883 have_children = spawn_flag;
2886 if (mainconfig.proxy_requests) {
2888 * Create the tree for managing proxied requests and
2891 proxy_list = fr_packet_list_create(1);
2892 if (!proxy_list) return 0;
2894 #ifdef HAVE_PTHREAD_H
2895 if (pthread_mutex_init(&proxy_mutex, NULL) != 0) {
2896 radlog(L_ERR, "FATAL: Failed to initialize proxy mutex: %s",
2905 * Just before we spawn the child threads, force the log
2906 * subsystem to re-open the log file for every write.
2908 if (spawn_flag) force_log_reopen();
2910 #ifdef HAVE_PTHREAD_H
2912 NO_SUCH_CHILD_PID = (pthread_t ) (0);
2914 NO_SUCH_CHILD_PID = pthread_self(); /* not a child thread */
2916 if (thread_pool_init(cs, spawn_flag) < 0) {
2922 DEBUG("%s: #### Skipping IP addresses and Ports ####",
2929 * Child threads need a pipe to signal us, as do the
2932 if (pipe(self_pipe) < 0) {
2933 radlog(L_ERR, "radiusd: Error opening internal pipe: %s",
2937 if (fcntl(self_pipe[0], F_SETFL, O_NONBLOCK | FD_CLOEXEC) < 0) {
2938 radlog(L_ERR, "radiusd: Error setting internal flags: %s",
2942 if (fcntl(self_pipe[1], F_SETFL, O_NONBLOCK | FD_CLOEXEC) < 0) {
2943 radlog(L_ERR, "radiusd: Error setting internal flags: %s",
2948 if (!fr_event_fd_insert(el, 0, self_pipe[0],
2949 event_signal_handler, el)) {
2950 radlog(L_ERR, "Failed creating handler for signals");
2957 * Mark the proxy Fd's as unused.
2962 for (i = 0; i < 32; i++) proxy_fds[i] = -1;
2966 DEBUG("%s: #### Opening IP addresses and Ports ####",
2969 fr_suid_up(); /* sockets may bind to privileged ports */
2971 if (listen_init(cs, &head) < 0) {
2978 * Add all of the sockets to the event loop.
2982 this = this->next) {
2985 this->print(this, buffer, sizeof(buffer));
2987 switch (this->type) {
2989 case RAD_LISTEN_DETAIL:
2990 DEBUG("Listening on %s", buffer);
2991 has_detail_listener = TRUE;
2996 case RAD_LISTEN_PROXY:
2997 rad_assert(proxy_fds[this->fd & 0x1f] == -1);
2998 rad_assert(proxy_listeners[this->fd & 0x1f] == NULL);
3000 proxy_fds[this->fd & 0x1f] = this->fd;
3001 proxy_listeners[this->fd & 0x1f] = this;
3002 if (!fr_packet_list_socket_add(proxy_list,
3014 * The file descriptor isn't ready. Poll for
3015 * when it will become ready. This is for the
3025 if (has_detail_listener) {
3026 struct timeval when;
3028 gettimeofday(&when, NULL);
3031 if (!fr_event_insert(el, event_poll_detail, NULL,
3033 radlog(L_ERR, "Failed creating handler");
3038 mainconfig.listen = head;
3044 static int request_hash_cb(UNUSED void *ctx, void *data)
3046 REQUEST *request = fr_packet2myptr(REQUEST, packet, data);
3049 rad_assert(request->in_proxy_hash == FALSE);
3052 fr_event_delete(el, &request->ev);
3053 remove_from_request_hash(request);
3054 request_free(&request);
3061 static int proxy_hash_cb(UNUSED void *ctx, void *data)
3063 REQUEST *request = fr_packet2myptr(REQUEST, proxy, data);
3065 fr_packet_list_yank(proxy_list, request->proxy);
3066 request->in_proxy_hash = FALSE;
3068 if (!request->in_request_hash) {
3069 fr_event_delete(el, &request->ev);
3070 request_free(&request);
3077 void radius_event_free(void)
3080 * FIXME: Stop all threads, or at least check that
3081 * they're all waiting on the semaphore, and the queues
3087 * There are requests in the proxy hash that aren't
3088 * referenced from anywhere else. Remove them first.
3091 PTHREAD_MUTEX_LOCK(&proxy_mutex);
3092 fr_packet_list_walk(proxy_list, NULL, proxy_hash_cb);
3093 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
3094 fr_packet_list_free(proxy_list);
3099 fr_packet_list_walk(pl, NULL, request_hash_cb);
3101 fr_packet_list_free(pl);
3104 fr_event_list_free(el);
3107 int radius_event_process(void)
3111 return fr_event_loop(el);
3114 void radius_handle_request(REQUEST *request, RAD_REQUEST_FUNP fun)
3116 request->options = RAD_REQUEST_OPTION_DEBUG2;
3118 if (request_pre_handler(request)) {
3119 rad_assert(fun != NULL);
3120 rad_assert(request != NULL);
3122 if (request->server) RDEBUG("server %s {",
3126 if (request->server) RDEBUG("} # server %s",
3129 request_post_handler(request);
3132 DEBUG2("Going to the next request");