2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * @brief Defines the state machines that control how requests are processed.
23 * @copyright 2012 The FreeRADIUS server project
24 * @copyright 2012 Alan DeKok <aland@deployingradius.com>
29 #include <freeradius-devel/radiusd.h>
30 #include <freeradius-devel/process.h>
31 #include <freeradius-devel/modules.h>
32 #include <freeradius-devel/state.h>
34 #include <freeradius-devel/rad_assert.h>
37 #include <freeradius-devel/detail.h>
43 #ifdef HAVE_SYS_WAIT_H
44 # include <sys/wait.h>
47 extern pid_t radius_pid;
48 extern fr_cond_t *debug_condition;
50 static bool spawn_flag = false;
51 static bool just_started = true;
52 time_t fr_start_time = (time_t)-1;
53 static rbtree_t *pl = NULL;
54 static fr_event_list_t *el = NULL;
56 fr_event_list_t *radius_event_list_corral(UNUSED event_corral_t hint) {
57 /* Currently we do not run a second event loop for modules. */
61 static char const *action_codes[] = {
72 #ifdef DEBUG_STATE_MACHINE
73 # define TRACE_STATE_MACHINE \
74 if (rad_debug_lvl) do { \
75 struct timeval debug_tv; \
76 gettimeofday(&debug_tv, NULL); \
77 debug_tv.tv_sec -= fr_start_time; \
78 printf("(%u) %d.%06d ********\tSTATE %s action %s live M-%s C-%s\t********\n",\
79 request->number, (int) debug_tv.tv_sec, (int) debug_tv.tv_usec, \
80 __FUNCTION__, action_codes[action], master_state_names[request->master_state], \
81 child_state_names[request->child_state]); \
84 static char const *master_state_names[REQUEST_MASTER_NUM_STATES] = {
91 static char const *child_state_names[REQUEST_CHILD_NUM_STATES] = {
102 # define TRACE_STATE_MACHINE {}
105 static NEVER_RETURNS void _rad_panic(char const *file, unsigned int line, char const *msg)
107 ERROR("%s[%u]: %s", file, line, msg);
111 #define rad_panic(x) _rad_panic(__FILE__, __LINE__, x)
113 /** Declare a state in the state machine
115 * Expands to the start of a function definition for a given state.
117 * @param _x the name of the state.
119 #define STATE_MACHINE_DECL(_x) static void _x(REQUEST *request, int action)
121 static void request_timer(void *ctx);
123 /** Insert #REQUEST back into the event heap, to continue executing at a future time
125 * @param file the state machine timer call occurred in.
126 * @param line the state machine timer call occurred on.
127 * @param request to set add the timer event for.
128 * @param when the event should fine.
129 * @param action to perform when we resume processing the request.
131 static inline void state_machine_timer(char const *file, int line, REQUEST *request,
132 struct timeval *when, fr_state_action_t action)
134 request->timer_action = action;
135 if (!fr_event_insert(el, request_timer, request, when, &request->ev)) {
136 _rad_panic(file, line, "Failed to insert event");
140 /** @copybrief state_machine_timer
142 * @param _x the action to perform when we resume processing the request.
144 #define STATE_MACHINE_TIMER(_x) state_machine_timer(__FILE__, __LINE__, request, &when, _x)
147 * We need a different VERIFY_REQUEST macro in process.c
148 * To avoid the race conditions with the master thread
149 * checking the REQUEST whilst it's being worked on by
152 #if defined(WITH_VERIFY_PTR) && defined(HAVE_PTHREAD_H)
153 # undef VERIFY_REQUEST
154 # define VERIFY_REQUEST(_x) if (pthread_equal(pthread_self(), _x->child_pid) != 0) verify_request(__FILE__, __LINE__, _x)
158 * @section request_timeline
160 * Time sequence of a request
163 * RQ-----------------P=============================Y-J-C
164 * ::::::::::::::::::::::::::::::::::::::::::::::::::::::::M
167 * - R: received. Duplicate detection is done, and request is
170 * - Q: Request is placed onto a queue for child threads to pick up.
171 * If there are no child threads, the request goes immediately
174 * - P: Processing the request through the modules.
176 * - Y: Reply is ready. Rejects MAY be delayed here. All other
177 * replies are sent immediately.
179 * - J: Reject is sent "response_delay" after the reply is ready.
181 * - C: For Access-Requests, After "cleanup_delay", the request is
182 * deleted. Accounting-Request packets go directly from Y to C.
184 * - M: Max request time. If the request hits this timer, it is
187 * Other considerations include duplicate and conflicting
188 * packets. When a dupicate packet is received, it is ignored
189 * until we've reached Y, as no response is ready. If the reply
190 * is a reject, duplicates are ignored until J, when we're ready
191 * to send the reply. In between the reply being sent (Y or J),
192 * and C, the server responds to duplicates by sending the cached
195 * Conflicting packets are sent in 2 situations.
197 * The first is in between R and Y. In that case, we consider
198 * it as a hint that we're taking too long, and the NAS has given
199 * up on the request. We then behave just as if the M timer was
200 * reached, and we discard the current request. This allows us
201 * to process the new one.
203 * The second case is when we're at Y, but we haven't yet
204 * finished processing the request. This is a race condition in
205 * the threading code (avoiding locks is faster). It means that
206 * a thread has actually encoded and sent the reply, and that the
207 * NAS has responded with a new packet. The server can then
208 * safely mark the current request as "OK to delete", and behaves
209 * just as if the M timer was reached. This usually happens only
210 * in high-load situations.
212 * Duplicate packets are sent when the NAS thinks we're taking
213 * too long, and wants a reply. From R-Y, duplicates are
214 * ignored. From Y-J (for Access-Rejects), duplicates are also
215 * ignored. From Y-C, duplicates get a duplicate reply. *And*,
216 * they cause the "cleanup_delay" time to be extended. This
217 * extension means that we're more likely to send a duplicate
218 * reply (if we have one), or to suppress processing the packet
219 * twice if we didn't reply to it.
221 * All functions in this file should be thread-safe, and should
222 * assume thet the REQUEST structure is being accessed
223 * simultaneously by the main thread, and by the child worker
224 * threads. This means that timers, etc. cannot be updated in
227 * Instead, the master thread periodically calls request->process
228 * with action TIMER. It's up to the individual functions to
229 * determine how to handle that. They need to check if they're
230 * being called from a child thread or the master, and then do
231 * different things based on that.
234 static fr_packet_list_t *proxy_list = NULL;
235 static TALLOC_CTX *proxy_ctx = NULL;
238 #ifdef HAVE_PTHREAD_H
240 static pthread_mutex_t proxy_mutex;
241 static bool proxy_no_new_sockets = false;
244 # define PTHREAD_MUTEX_LOCK if (spawn_flag) pthread_mutex_lock
245 # define PTHREAD_MUTEX_UNLOCK if (spawn_flag) pthread_mutex_unlock
247 static pthread_t NO_SUCH_CHILD_PID;
248 # define NO_CHILD_THREAD request->child_pid = NO_SUCH_CHILD_PID
252 * This is easier than ifdef's throughout the code.
254 # define PTHREAD_MUTEX_LOCK(_x)
255 # define PTHREAD_MUTEX_UNLOCK(_x)
256 # define NO_CHILD_THREAD
259 #ifdef HAVE_PTHREAD_H
260 static bool we_are_master(void)
263 (pthread_equal(pthread_self(), NO_SUCH_CHILD_PID) == 0)) {
271 * Assertions are debug checks.
274 # define ASSERT_MASTER if (!we_are_master()) rad_panic("We are not master")
279 * No threads: we're always master.
281 # define we_are_master(_x) (1)
282 #endif /* HAVE_PTHREAD_H */
284 #ifndef ASSERT_MASTER
285 # define ASSERT_MASTER
289 * Make state transitions simpler.
291 #define FINAL_STATE(_x) NO_CHILD_THREAD; request->component = "<" #_x ">"; request->module = ""; request->child_state = _x
294 static int event_new_fd(rad_listen_t *this);
297 * We need mutexes around the event FD list *only* in certain
300 #if defined (HAVE_PTHREAD_H) && (defined(WITH_PROXY) || defined(WITH_TCP))
301 static rad_listen_t *new_listeners = NULL;
303 static pthread_mutex_t fd_mutex;
304 # define FD_MUTEX_LOCK if (spawn_flag) pthread_mutex_lock
305 # define FD_MUTEX_UNLOCK if (spawn_flag) pthread_mutex_unlock
307 void radius_update_listener(rad_listen_t *this)
310 * Just do it ourselves.
312 if (we_are_master()) {
317 FD_MUTEX_LOCK(&fd_mutex);
320 * If it's already in the list, don't add it again.
323 FD_MUTEX_UNLOCK(&fd_mutex);
328 * Otherwise, add it to the list
330 this->next = new_listeners;
331 new_listeners = this;
332 FD_MUTEX_UNLOCK(&fd_mutex);
333 radius_signal_self(RADIUS_SIGNAL_SELF_NEW_FD);
336 void radius_update_listener(rad_listen_t *this)
339 * No threads. Just insert it.
344 * This is easier than ifdef's throughout the code.
346 # define FD_MUTEX_LOCK(_x)
347 # define FD_MUTEX_UNLOCK(_x)
350 static int request_num_counter = 1;
352 static int request_will_proxy(REQUEST *request) CC_HINT(nonnull);
353 static int request_proxy(REQUEST *request) CC_HINT(nonnull);
354 STATE_MACHINE_DECL(request_ping) CC_HINT(nonnull);
356 STATE_MACHINE_DECL(request_response_delay) CC_HINT(nonnull);
357 STATE_MACHINE_DECL(request_cleanup_delay) CC_HINT(nonnull);
358 STATE_MACHINE_DECL(request_running) CC_HINT(nonnull);
359 STATE_MACHINE_DECL(request_done) CC_HINT(nonnull);
361 STATE_MACHINE_DECL(proxy_no_reply) CC_HINT(nonnull);
362 STATE_MACHINE_DECL(proxy_running) CC_HINT(nonnull);
363 STATE_MACHINE_DECL(proxy_wait_for_reply) CC_HINT(nonnull);
365 static int process_proxy_reply(REQUEST *request, RADIUS_PACKET *reply) CC_HINT(nonnull (1));
366 static void remove_from_proxy_hash(REQUEST *request) CC_HINT(nonnull);
367 static void remove_from_proxy_hash_nl(REQUEST *request, bool yank) CC_HINT(nonnull);
368 static int insert_into_proxy_hash(REQUEST *request) CC_HINT(nonnull);
371 static REQUEST *request_setup(TALLOC_CTX *ctx, rad_listen_t *listener, RADIUS_PACKET *packet,
372 RADCLIENT *client, RAD_REQUEST_FUNP fun);
373 static int request_pre_handler(REQUEST *request, UNUSED int action) CC_HINT(nonnull);
376 static void request_coa_originate(REQUEST *request) CC_HINT(nonnull);
377 STATE_MACHINE_DECL(coa_wait_for_reply) CC_HINT(nonnull);
378 STATE_MACHINE_DECL(coa_no_reply) CC_HINT(nonnull);
379 STATE_MACHINE_DECL(coa_running) CC_HINT(nonnull);
380 static void coa_separate(REQUEST *request) CC_HINT(nonnull);
381 # define COA_SEPARATE if (request->coa) coa_separate(request->coa);
383 # define COA_SEPARATE
386 #define CHECK_FOR_STOP do { if (request->master_state == REQUEST_STOP_PROCESSING) {request_done(request, FR_ACTION_DONE);return;}} while (0)
389 #define USEC (1000000)
391 #define INSERT_EVENT(_function, _ctx) if (!fr_event_insert(el, _function, _ctx, &((_ctx)->when), &((_ctx)->ev))) { _rad_panic(__FILE__, __LINE__, "Failed to insert event"); }
393 static void tv_add(struct timeval *tv, int usec_delay)
395 if (usec_delay >= USEC) {
396 tv->tv_sec += usec_delay / USEC;
399 tv->tv_usec += usec_delay;
401 if (tv->tv_usec >= USEC) {
402 tv->tv_sec += tv->tv_usec / USEC;
408 * Debug the packet if requested.
410 static void debug_packet(REQUEST *request, RADIUS_PACKET *packet, bool received)
412 char src_ipaddr[128];
413 char dst_ipaddr[128];
416 if (!RDEBUG_ENABLED) return;
419 * Client-specific debugging re-prints the input
420 * packet into the client log.
422 * This really belongs in a utility library
424 if (is_radius_code(packet->code)) {
425 RDEBUG("%s %s Id %i from %s%s%s:%i to %s%s%s:%i length %zu",
426 received ? "Received" : "Sent",
427 fr_packet_codes[packet->code],
429 packet->src_ipaddr.af == AF_INET6 ? "[" : "",
430 inet_ntop(packet->src_ipaddr.af,
431 &packet->src_ipaddr.ipaddr,
432 src_ipaddr, sizeof(src_ipaddr)),
433 packet->src_ipaddr.af == AF_INET6 ? "]" : "",
435 packet->dst_ipaddr.af == AF_INET6 ? "[" : "",
436 inet_ntop(packet->dst_ipaddr.af,
437 &packet->dst_ipaddr.ipaddr,
438 dst_ipaddr, sizeof(dst_ipaddr)),
439 packet->dst_ipaddr.af == AF_INET6 ? "]" : "",
443 RDEBUG("%s code %u Id %i from %s%s%s:%i to %s%s%s:%i length %zu\n",
444 received ? "Received" : "Sent",
447 packet->src_ipaddr.af == AF_INET6 ? "[" : "",
448 inet_ntop(packet->src_ipaddr.af,
449 &packet->src_ipaddr.ipaddr,
450 src_ipaddr, sizeof(src_ipaddr)),
451 packet->src_ipaddr.af == AF_INET6 ? "]" : "",
453 packet->dst_ipaddr.af == AF_INET6 ? "[" : "",
454 inet_ntop(packet->dst_ipaddr.af,
455 &packet->dst_ipaddr.ipaddr,
456 dst_ipaddr, sizeof(dst_ipaddr)),
457 packet->dst_ipaddr.af == AF_INET6 ? "]" : "",
463 rdebug_pair_list(L_DBG_LVL_1, request, packet->vps, NULL);
465 rdebug_proto_pair_list(L_DBG_LVL_1, request, packet->vps);
470 /***********************************************************************
472 * Start of RADIUS server state machine.
474 ***********************************************************************/
476 static struct timeval *request_response_window(REQUEST *request)
478 VERIFY_REQUEST(request);
480 if (request->client) {
482 * The client hasn't set the response window. Return
483 * either the home server one, if set, or the global one.
485 if (!timerisset(&request->client->response_window)) {
486 return &request->home_server->response_window;
489 if (timercmp(&request->client->response_window,
490 &request->home_server->response_window, <)) {
491 return &request->client->response_window;
495 rad_assert(request->home_server != NULL);
496 return &request->home_server->response_window;
500 * Determine initial request processing delay.
502 static int request_init_delay(REQUEST *request)
504 struct timeval half_response_window;
506 VERIFY_REQUEST(request);
508 /* Allow client response window to lower initial delay */
509 if (timerisset(&request->client->response_window)) {
510 half_response_window.tv_sec = request->client->response_window.tv_sec >> 1;
511 half_response_window.tv_usec =
512 ((request->client->response_window.tv_sec & 1) * USEC +
513 request->client->response_window.tv_usec) >> 1;
514 if (timercmp(&half_response_window, &request->root->init_delay, <))
515 return (int)half_response_window.tv_sec * USEC +
516 (int)half_response_window.tv_usec;
519 return (int)request->root->init_delay.tv_sec * USEC +
520 (int)request->root->init_delay.tv_usec;
524 * Callback for ALL timer events related to the request.
526 static void request_timer(void *ctx)
528 REQUEST *request = talloc_get_type_abort(ctx, REQUEST);
531 action = request->timer_action;
535 request->process(request, action);
539 * Wrapper for talloc pools. If there's no parent, just free the
540 * request. If there is a parent, free the parent INSTEAD of the
543 static void request_free(REQUEST *request)
547 rad_assert(request->ev == NULL);
548 rad_assert(!request->in_request_hash);
549 rad_assert(!request->in_proxy_hash);
551 if ((request->options & RAD_REQUEST_OPTION_CTX) == 0) {
552 talloc_free(request);
556 ptr = talloc_parent(request);
557 rad_assert(ptr != NULL);
563 static void proxy_reply_too_late(REQUEST *request)
567 RDEBUG2("Reply from home server %s port %d - ID: %d arrived too late. Try increasing 'retry_delay' or 'max_request_time'",
568 inet_ntop(request->proxy->dst_ipaddr.af,
569 &request->proxy->dst_ipaddr.ipaddr,
570 buffer, sizeof(buffer)),
571 request->proxy->dst_port, request->proxy->id);
576 /** Mark a request DONE and clean it up.
578 * When a request is DONE, it can have ties to a number of other
579 * portions of the server. The request hash, proxy hash, events,
580 * child threads, etc. This function takes care of either cleaning
581 * up the request, or managing the timers to wait for the ties to be
586 * done -> done [ label = "still running" ];
590 static void request_done(REQUEST *request, int action)
592 struct timeval now, when;
594 VERIFY_REQUEST(request);
599 * Force this no matter what.
601 request->process = request_done;
605 * Tell the detail listener that we're done.
607 if (request->listener &&
608 (request->listener->type == RAD_LISTEN_DETAIL) &&
609 (request->simul_max != 1)) {
610 request->simul_max = 1;
611 request->listener->send(request->listener,
616 #ifdef HAVE_PTHREAD_H
618 * If called from a child thread, mark ourselves as done,
619 * and wait for the master thread timer to clean us up.
621 if (!we_are_master()) {
622 FINAL_STATE(REQUEST_DONE);
628 * Mark the request as STOP.
630 request->master_state = REQUEST_STOP_PROCESSING;
634 * Move the CoA request to its own handler.
637 coa_separate(request->coa);
638 } else if (request->parent && (request->parent->coa == request)) {
639 coa_separate(request);
644 * It doesn't hurt to send duplicate replies. All other
645 * signals are ignored, as the request will be cleaned up
651 rad_assert(request->listener != NULL);
653 if (request->reply->code != 0) {
654 request->listener->send(request->listener, request);
657 RDEBUG("No reply. Ignoring retransmit");
662 * Mark the request as done.
665 #ifdef HAVE_PTHREAD_H
667 * If the child is still running, leave it alone.
669 if (spawn_flag && (request->child_state <= REQUEST_RUNNING)) {
674 #ifdef DEBUG_STATE_MACHINE
675 if (rad_debug_lvl) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n",
676 request->number, __FUNCTION__,
677 child_state_names[request->child_state],
678 child_state_names[REQUEST_DONE]);
680 request->child_state = REQUEST_DONE;
684 * Called when the child is taking too long to
685 * finish. We've already marked it "please
686 * stop", so we don't complain any more.
688 case FR_ACTION_TIMER:
692 case FR_ACTION_PROXY_REPLY:
693 proxy_reply_too_late(request);
702 * Remove it from the request hash.
704 if (request->in_request_hash) {
705 if (!rbtree_deletebydata(pl, &request->packet)) {
708 request->in_request_hash = false;
713 * Wait for the proxy ID to expire. This allows us to
714 * avoid re-use of proxy IDs for a while.
716 if (request->in_proxy_hash) {
717 rad_assert(request->proxy != NULL);
719 fr_event_now(el, &now);
720 when = request->proxy->timestamp;
723 if (((request->proxy->code == PW_CODE_COA_REQUEST) ||
724 (request->proxy->code == PW_CODE_DISCONNECT_REQUEST)) &&
725 (request->packet->code != request->proxy->code)) {
726 when.tv_sec += request->home_server->coa_mrd;
729 timeradd(&when, request_response_window(request), &when);
732 * We haven't received all responses, AND there's still
733 * time to wait. Do so.
735 if ((request->num_proxied_requests > request->num_proxied_responses) &&
737 (request->home_server->proto != IPPROTO_TCP) &&
739 timercmp(&now, &when, <)) {
740 RDEBUG("Waiting for more responses from the home server");
747 remove_from_proxy_hash(request);
751 #ifdef HAVE_PTHREAD_H
753 * If there's no children, we can mark the request as done.
755 if (!spawn_flag) request->child_state = REQUEST_DONE;
759 * If the child is still running, wait for it to be finished.
761 if (request->child_state <= REQUEST_RUNNING) {
762 gettimeofday(&now, NULL);
767 if (request->delay < (USEC / 3)) request->delay = USEC / 3;
768 tv_add(&when, request->delay);
769 request->delay += request->delay >> 1;
770 if (request->delay > (10 * USEC)) request->delay = 10 * USEC;
772 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
776 #ifdef HAVE_PTHREAD_H
777 rad_assert(request->child_pid == NO_SUCH_CHILD_PID);
781 * @todo: do final states for TCP sockets, too?
783 request_stats_final(request);
785 if (request->listener) {
786 request->listener->count--;
789 * If we're the last one, remove the listener now.
791 if ((request->listener->count == 0) &&
792 (request->listener->status >= RAD_LISTEN_STATUS_FROZEN)) {
793 event_new_fd(request->listener);
798 if (request->packet) {
799 RDEBUG2("Cleaning up request packet ID %u with timestamp +%d",
801 (unsigned int) (request->timestamp - fr_start_time));
802 } /* else don't print anything */
805 fr_event_delete(el, &request->ev);
806 request_free(request);
810 static void request_cleanup_delay_init(REQUEST *request)
812 struct timeval now, when;
814 VERIFY_REQUEST(request);
817 * Do cleanup delay ONLY for RADIUS packets from a real
818 * client. Everything else just gets cleaned up
821 if (request->packet->dst_port == 0) goto done;
824 * Accounting packets shouldn't be retransmitted. They
825 * should always be updated with Acct-Delay-Time.
827 #ifdef WITH_ACCOUNTING
828 if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) goto done;
832 if (request->listener->type == RAD_LISTEN_DHCP) goto done;
836 if (request->listener->type == RAD_LISTEN_VQP) goto done;
839 if (!request->root->cleanup_delay) goto done;
841 gettimeofday(&now, NULL);
843 rad_assert(request->reply->timestamp.tv_sec != 0);
844 when = request->reply->timestamp;
846 request->delay = request->root->cleanup_delay;
847 when.tv_sec += request->delay;
850 * Set timer for when we need to clean it up.
852 if (timercmp(&when, &now, >)) {
853 #ifdef DEBUG_STATE_MACHINE
854 if (rad_debug_lvl) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_cleanup_delay");
856 request->process = request_cleanup_delay;
858 if (!we_are_master()) {
859 FINAL_STATE(REQUEST_CLEANUP_DELAY);
864 * Update this if we can, otherwise let the timers pick it up.
866 request->child_state = REQUEST_CLEANUP_DELAY;
867 #ifdef HAVE_PTHREAD_H
868 rad_assert(request->child_pid == NO_SUCH_CHILD_PID);
870 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
875 * Otherwise just clean it up.
878 request_done(request, FR_ACTION_DONE);
883 * Enforce max_request_time.
885 static bool request_max_time(REQUEST *request)
887 struct timeval now, when;
888 rad_assert(request->magic == REQUEST_MAGIC);
889 #ifdef DEBUG_STATE_MACHINE
890 int action = FR_ACTION_TIMER;
893 VERIFY_REQUEST(request);
899 * The child thread has acknowledged it's done.
900 * Transition to the DONE state.
902 * If the request was marked STOP, then the "check for
903 * stop" macro already took care of it.
905 if (request->child_state == REQUEST_DONE) {
907 request_done(request, FR_ACTION_DONE);
912 * The request is still running. Enforce max_request_time.
914 fr_event_now(el, &now);
915 when = request->packet->timestamp;
916 when.tv_sec += request->root->max_request_time;
919 * Taking too long: tell it to die.
921 if (timercmp(&now, &when, >=)) {
922 #ifdef HAVE_PTHREAD_H
924 * If there's a child thread processing it,
928 (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0)) {
929 ERROR("Unresponsive child for request %u, in component %s module %s",
931 request->component ? request->component : "<core>",
932 request->module ? request->module : "<core>");
933 exec_trigger(request, NULL, "server.thread.unresponsive", true);
937 * Tell the request that it's done.
943 * Sleep for some more. We HOPE that the child will
944 * become responsive at some point in the future. We do
945 * this by adding 50% to the current timer.
948 tv_add(&when, request->delay);
949 request->delay += request->delay >> 1;
950 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
954 static void request_queue_or_run(REQUEST *request,
955 fr_request_process_t process)
957 #ifdef DEBUG_STATE_MACHINE
958 int action = FR_ACTION_TIMER;
961 VERIFY_REQUEST(request);
966 * Do this here so that fewer other functions need to do
969 if (request->master_state == REQUEST_STOP_PROCESSING) {
970 #ifdef DEBUG_STATE_MACHINE
971 if (rad_debug_lvl) printf("(%u) ********\tSTATE %s M-%s causes C-%s-> C-%s\t********\n",
972 request->number, __FUNCTION__,
973 master_state_names[request->master_state],
974 child_state_names[request->child_state],
975 child_state_names[REQUEST_DONE]);
977 request_done(request, FR_ACTION_DONE);
981 request->process = process;
983 if (we_are_master()) {
987 * (re) set the initial delay.
989 request->delay = request_init_delay(request);
990 if (request->delay > USEC) request->delay = USEC;
991 gettimeofday(&when, NULL);
992 tv_add(&when, request->delay);
993 request->delay += request->delay >> 1;
995 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
997 #ifdef HAVE_PTHREAD_H
1000 * A child thread will eventually pick it up.
1002 if (request_enqueue(request)) return;
1005 * Otherwise we're not going to do anything with
1008 request_done(request, FR_ACTION_DONE);
1014 request->child_state = REQUEST_RUNNING;
1015 request->process(request, FR_ACTION_RUN);
1019 * Requests that care about child process exit
1020 * codes have already either called
1021 * rad_waitpid(), or they've given up.
1023 while (waitpid(-1, NULL, WNOHANG) > 0);
1028 static void request_dup(REQUEST *request)
1030 ERROR("(%u) Ignoring duplicate packet from "
1031 "client %s port %d - ID: %u due to unfinished request "
1032 "in component %s module %s",
1033 request->number, request->client->shortname,
1034 request->packet->src_port,request->packet->id,
1035 request->component, request->module);
1039 /** Sit on a request until it's time to clean it up.
1041 * A NAS may not see a response from the server. When the NAS
1042 * retransmits, we want to be able to send a cached reply back. The
1043 * alternative is to re-process the packet, which does bad things for
1044 * EAP, among others.
1046 * IF we do see a NAS retransmit, we extend the cleanup delay,
1047 * because the NAS might miss our cached reply.
1049 * Otherwise, once we reach cleanup_delay, we transition to DONE.
1052 * digraph cleanup_delay {
1054 * send_reply [ label = "send_reply\nincrease cleanup delay" ];
1056 * cleanup_delay -> send_reply [ label = "DUP" ];
1057 * send_reply -> cleanup_delay;
1058 * cleanup_delay -> proxy_reply_too_late [ label = "PROXY_REPLY", arrowhead = "none" ];
1059 * cleanup_delay -> cleanup_delay [ label = "TIMER < timeout" ];
1060 * cleanup_delay -> done [ label = "TIMER >= timeout" ];
1064 static void request_cleanup_delay(REQUEST *request, int action)
1066 struct timeval when, now;
1068 VERIFY_REQUEST(request);
1070 TRACE_STATE_MACHINE;
1077 if (request->reply->code != 0) {
1078 request->listener->send(request->listener, request);
1080 RDEBUG("No reply. Ignoring retransmit");
1084 * Double the cleanup_delay to catch retransmits.
1086 when = request->reply->timestamp;
1087 request->delay += request->delay;
1088 when.tv_sec += request->delay;
1090 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
1094 case FR_ACTION_PROXY_REPLY:
1095 proxy_reply_too_late(request);
1099 case FR_ACTION_TIMER:
1100 fr_event_now(el, &now);
1102 rad_assert(request->root->cleanup_delay > 0);
1104 when = request->reply->timestamp;
1105 when.tv_sec += request->root->cleanup_delay;
1107 if (timercmp(&when, &now, >)) {
1108 #ifdef DEBUG_STATE_MACHINE
1109 if (rad_debug_lvl) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_cleanup_delay");
1111 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
1113 } /* else it's time to clean up */
1115 request_done(request, FR_ACTION_DONE);
1119 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
1125 /** Sit on a request until it's time to respond to it.
1127 * For security reasons, rejects (and maybe some other) packets are
1128 * delayed for a while before we respond. This delay means that
1129 * badly behaved NASes don't hammer the server with authentication
1132 * Otherwise, once we reach response_delay, we send the reply, and
1133 * transition to cleanup_delay.
1136 * digraph response_delay {
1137 * response_delay -> proxy_reply_too_late [ label = "PROXY_REPLY", arrowhead = "none" ];
1138 * response_delay -> response_delay [ label = "DUP, TIMER < timeout" ];
1139 * response_delay -> send_reply [ label = "TIMER >= timeout" ];
1140 * send_reply -> cleanup_delay;
1144 static void request_response_delay(REQUEST *request, int action)
1146 struct timeval when, now;
1148 VERIFY_REQUEST(request);
1150 TRACE_STATE_MACHINE;
1157 ERROR("(%u) Discarding duplicate request from "
1158 "client %s port %d - ID: %u due to delayed response",
1159 request->number, request->client->shortname,
1160 request->packet->src_port,request->packet->id);
1164 case FR_ACTION_PROXY_REPLY:
1165 proxy_reply_too_late(request);
1169 case FR_ACTION_TIMER:
1170 fr_event_now(el, &now);
1173 * See if it's time to send the reply. If not,
1174 * we wait some more.
1176 when = request->reply->timestamp;
1178 tv_add(&when, request->response_delay.tv_sec * USEC);
1179 tv_add(&when, request->response_delay.tv_usec);
1181 if (timercmp(&when, &now, >)) {
1182 #ifdef DEBUG_STATE_MACHINE
1183 if (rad_debug_lvl) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_response_delay");
1185 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
1187 } /* else it's time to send the reject */
1189 RDEBUG2("Sending delayed response");
1190 debug_packet(request, request->reply, false);
1191 request->listener->send(request->listener, request);
1194 * Clean up the request.
1196 request_cleanup_delay_init(request);
1200 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
1206 static int request_pre_handler(REQUEST *request, UNUSED int action)
1210 VERIFY_REQUEST(request);
1212 TRACE_STATE_MACHINE;
1214 if (request->master_state == REQUEST_STOP_PROCESSING) return 0;
1217 * Don't decode the packet if it's an internal "fake"
1218 * request. Instead, just return so that the caller can
1221 if (request->packet->dst_port == 0) {
1222 request->username = fr_pair_find_by_num(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1223 request->password = fr_pair_find_by_num(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
1227 if (!request->packet->vps) { /* FIXME: check for correct state */
1228 rcode = request->listener->decode(request->listener, request);
1231 if (debug_condition) {
1233 * Ignore parse errors.
1235 if (radius_evaluate_cond(request, RLM_MODULE_OK, 0, debug_condition)) {
1236 request->log.lvl = L_DBG_LVL_2;
1237 request->log.func = vradlog_request;
1242 debug_packet(request, request->packet, true);
1248 RATE_LIMIT(INFO("Dropping packet without response because of error: %s", fr_strerror()));
1249 request->reply->offset = -2; /* bad authenticator */
1253 if (!request->username) {
1254 request->username = fr_pair_find_by_num(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1261 /** Do the final processing of a request before we reply to the NAS.
1263 * Various cleanups, suppress responses, copy Proxy-State, and set
1264 * response_delay or cleanup_delay;
1266 static void request_finish(REQUEST *request, int action)
1270 VERIFY_REQUEST(request);
1272 TRACE_STATE_MACHINE;
1275 (void) action; /* -Wunused */
1279 * Don't do post-auth if we're a CoA request originated
1280 * from an Access-Request. See request_alloc_coa() for
1283 if ((request->options & RAD_REQUEST_OPTION_COA) != 0) goto done;
1287 * Override the response code if a control:Response-Packet-Type attribute is present.
1289 vp = fr_pair_find_by_num(request->config, PW_RESPONSE_PACKET_TYPE, 0, TAG_ANY);
1291 if (vp->vp_integer == 256) {
1292 RDEBUG2("Not responding to request");
1293 request->reply->code = 0;
1295 request->reply->code = vp->vp_integer;
1299 * Catch Auth-Type := Reject BEFORE proxying the packet.
1301 else if (request->packet->code == PW_CODE_ACCESS_REQUEST) {
1302 if (request->reply->code == 0) {
1303 vp = fr_pair_find_by_num(request->config, PW_AUTH_TYPE, 0, TAG_ANY);
1304 if (!vp || (vp->vp_integer != 5)) {
1305 RDEBUG2("There was no response configured: "
1306 "rejecting request");
1309 request->reply->code = PW_CODE_ACCESS_REJECT;
1314 * Copy Proxy-State from the request to the reply.
1316 vp = fr_pair_list_copy_by_num(request->reply, request->packet->vps,
1317 PW_PROXY_STATE, 0, TAG_ANY);
1318 if (vp) fr_pair_add(&request->reply->vps, vp);
1321 * Call Post-Auth for Access-Request packets.
1323 if (request->packet->code == PW_CODE_ACCESS_REQUEST) {
1324 rad_postauth(request);
1329 * Maybe originate a CoA request.
1331 if ((action == FR_ACTION_RUN) && !request->proxy && request->coa) {
1332 request_coa_originate(request);
1337 * Clean up. These are no longer needed.
1339 gettimeofday(&request->reply->timestamp, NULL);
1342 * Fake packets get marked as "done", and have the
1343 * proxy-reply section deal with the reply attributes.
1344 * We therefore don't free the reply attributes.
1346 if (request->packet->dst_port == 0) {
1347 RDEBUG("Finished internally proxied request.");
1348 FINAL_STATE(REQUEST_DONE);
1354 * Always send the reply to the detail listener.
1356 if (request->listener->type == RAD_LISTEN_DETAIL) {
1357 request->simul_max = 1;
1360 * But only print the reply if there is one.
1362 if (request->reply->code != 0) {
1363 debug_packet(request, request->reply, false);
1366 request->listener->send(request->listener, request);
1372 * Ignore all "do not respond" packets.
1373 * Except for the detail ones, which need to ping
1374 * the detail file reader so that it will retransmit.
1376 if (!request->reply->code) {
1377 RDEBUG("Not sending reply to client.");
1382 * If it's not in the request hash, we MIGHT not want to
1385 * If duplicate packets are allowed, then then only
1386 * reason to NOT be in the request hash is because we
1387 * don't want to send a reply.
1389 * FIXME: this is crap. The rest of the state handling
1390 * should use a different field so that we don't have two
1393 * Otherwise duplicates are forbidden, and the request is
1394 * SUPPOSED to avoid the request hash.
1396 * In that case, we need to send a reply.
1398 if (!request->in_request_hash &&
1399 !request->listener->nodup) {
1400 RDEBUG("Suppressing reply to client.");
1405 * See if we need to delay an Access-Reject packet.
1407 if ((request->reply->code == PW_CODE_ACCESS_REJECT) &&
1408 (request->root->reject_delay.tv_sec > 0)) {
1409 request->response_delay = request->root->reject_delay;
1411 vp = fr_pair_find_by_num(request->reply->vps, PW_FREERADIUS_RESPONSE_DELAY, 0, TAG_ANY);
1413 if (vp->vp_integer <= 10) {
1414 request->response_delay.tv_sec = vp->vp_integer;
1416 request->response_delay.tv_sec = 10;
1418 request->response_delay.tv_usec = 0;
1420 vp = fr_pair_find_by_num(request->reply->vps, PW_FREERADIUS_RESPONSE_DELAY_USEC, 0, TAG_ANY);
1422 if (vp->vp_integer <= 10 * USEC) {
1423 request->response_delay.tv_sec = vp->vp_integer / USEC;
1424 request->response_delay.tv_usec = vp->vp_integer % USEC;
1426 request->response_delay.tv_sec = 10;
1427 request->response_delay.tv_usec = 0;
1434 * If we timed out a proxy packet, don't delay
1435 * the reject any more.
1437 if (request->proxy && !request->proxy_reply) {
1438 request->response_delay.tv_sec = 0;
1439 request->response_delay.tv_usec = 0;
1447 if ((request->response_delay.tv_sec == 0) &&
1448 (request->response_delay.tv_usec == 0)) {
1451 * Don't print a reply if there's none to send.
1453 if (request->reply->code != 0) {
1454 if (rad_debug_lvl && request->state &&
1455 (request->reply->code == PW_CODE_ACCESS_ACCEPT)) {
1456 if (!fr_pair_find_by_num(request->packet->vps, PW_STATE, 0, TAG_ANY)) {
1457 RWDEBUG2("Unused attributes found in &session-state:");
1461 debug_packet(request, request->reply, false);
1462 request->listener->send(request->listener, request);
1466 RDEBUG2("Finished request");
1467 request_cleanup_delay_init(request);
1471 * Encode and sign it here, so that the master
1472 * thread can just send the encoded data, which
1473 * means it does less work.
1475 RDEBUG2("Delaying response for %d.%06d seconds",
1476 (int) request->response_delay.tv_sec, (int) request->response_delay.tv_usec);
1477 request->listener->encode(request->listener, request);
1478 request->process = request_response_delay;
1480 FINAL_STATE(REQUEST_RESPONSE_DELAY);
1484 /** Process a request from a client.
1486 * The outcome might be that the request is proxied.
1490 * running -> running [ label = "TIMER < max_request_time" ];
1491 * running -> done [ label = "TIMER >= max_request_time" ];
1492 * running -> proxy [ label = "proxied" ];
1493 * running -> dup [ label = "DUP", arrowhead = "none" ];
1497 static void request_running(REQUEST *request, int action)
1499 VERIFY_REQUEST(request);
1501 TRACE_STATE_MACHINE;
1505 case FR_ACTION_TIMER:
1507 (void) request_max_time(request);
1511 request_dup(request);
1515 if (!request_pre_handler(request, action)) {
1516 #ifdef DEBUG_STATE_MACHINE
1517 if (rad_debug_lvl) printf("(%u) ********\tSTATE %s failed in pre-handler C-%s -> C-%s\t********\n",
1518 request->number, __FUNCTION__,
1519 child_state_names[request->child_state],
1520 child_state_names[REQUEST_DONE]);
1522 FINAL_STATE(REQUEST_DONE);
1526 rad_assert(request->handle != NULL);
1527 request->handle(request);
1531 * We may need to send a proxied request.
1533 if ((action == FR_ACTION_RUN) &&
1534 request_will_proxy(request)) {
1535 #ifdef DEBUG_STATE_MACHINE
1536 if (rad_debug_lvl) printf("(%u) ********\tWill Proxy\t********\n", request->number);
1540 * takes care of setting
1541 * up the post proxy fail
1544 if (request_proxy(request) < 0) goto req_finished;
1548 #ifdef DEBUG_STATE_MACHINE
1549 if (rad_debug_lvl) printf("(%u) ********\tFinished\t********\n", request->number);
1555 request_finish(request, action);
1560 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
1565 int request_receive(TALLOC_CTX *ctx, rad_listen_t *listener, RADIUS_PACKET *packet,
1566 RADCLIENT *client, RAD_REQUEST_FUNP fun)
1569 RADIUS_PACKET **packet_p;
1570 REQUEST *request = NULL;
1572 listen_socket_t *sock = NULL;
1574 VERIFY_PACKET(packet);
1577 * Set the last packet received.
1579 gettimeofday(&now, NULL);
1581 packet->timestamp = now;
1583 #ifdef WITH_ACCOUNTING
1584 if (listener->type != RAD_LISTEN_DETAIL)
1587 sock = listener->data;
1588 sock->last_packet = now.tv_sec;
1591 packet->proto = sock->proto;
1596 * Skip everything if required.
1598 if (listener->nodup) goto skip_dup;
1600 packet_p = rbtree_finddata(pl, &packet);
1602 rad_child_state_t child_state;
1604 request = fr_packet2myptr(REQUEST, packet, packet_p);
1605 rad_assert(request->in_request_hash);
1606 child_state = request->child_state;
1609 * Same src/dst ip/port, length, and
1610 * authentication vector: must be a duplicate.
1612 if ((request->packet->data_len == packet->data_len) &&
1613 (memcmp(request->packet->vector, packet->vector,
1614 sizeof(packet->vector)) == 0)) {
1617 switch (packet->code) {
1618 case PW_CODE_ACCESS_REQUEST:
1619 FR_STATS_INC(auth, total_dup_requests);
1622 #ifdef WITH_ACCOUNTING
1623 case PW_CODE_ACCOUNTING_REQUEST:
1624 FR_STATS_INC(acct, total_dup_requests);
1628 case PW_CODE_COA_REQUEST:
1629 FR_STATS_INC(coa, total_dup_requests);
1632 case PW_CODE_DISCONNECT_REQUEST:
1633 FR_STATS_INC(dsc, total_dup_requests);
1640 #endif /* WITH_STATS */
1643 * Tell the state machine that there's a
1644 * duplicate request.
1646 request->process(request, FR_ACTION_DUP);
1647 return 0; /* duplicate of live request */
1651 * Mark the request as done ASAP, and before we
1652 * log anything. The child may stop processing
1653 * the request just as we're logging the
1656 request_done(request, FR_ACTION_DONE);
1660 * It's a new request, not a duplicate. If the
1661 * old one is done, then we can clean it up.
1663 if (child_state <= REQUEST_RUNNING) {
1665 * The request is still QUEUED or RUNNING. That's a problem.
1667 ERROR("Received conflicting packet from "
1668 "client %s port %d - ID: %u due to "
1669 "unfinished request. Giving up on old request.",
1671 packet->src_port, packet->id);
1675 * Mark the old request as done. If there's no
1676 * child, the request will be cleaned up
1677 * immediately. If there is a child, we'll set a
1678 * timer to go clean up the request.
1680 } /* else the new packet is unique */
1683 * Quench maximum number of outstanding requests.
1685 if (main_config.max_requests &&
1686 ((count = rbtree_num_elements(pl)) > main_config.max_requests)) {
1687 RATE_LIMIT(ERROR("Dropping request (%d is too many): from client %s port %d - ID: %d", count,
1689 packet->src_port, packet->id);
1690 WARN("Please check the configuration file.\n"
1691 "\tThe value for 'max_requests' is probably set too low.\n"));
1693 exec_trigger(NULL, NULL, "server.max_requests", true);
1699 * Rate-limit the incoming packets
1701 if (sock && sock->max_rate) {
1704 pps = rad_pps(&sock->rate_pps_old, &sock->rate_pps_now, &sock->rate_time, &now);
1705 if (pps > sock->max_rate) {
1706 DEBUG("Dropping request due to rate limiting");
1709 sock->rate_pps_now++;
1713 * Allocate a pool for the request.
1716 ctx = talloc_pool(NULL, main_config.talloc_pool_size);
1718 talloc_set_name_const(ctx, "request_receive_pool");
1721 * The packet is still allocated from a different
1722 * context, but oh well.
1724 (void) talloc_steal(ctx, packet);
1727 request = request_setup(ctx, listener, packet, client, fun);
1734 * Mark it as a "real" request with a context.
1736 request->options |= RAD_REQUEST_OPTION_CTX;
1739 * Remember the request in the list.
1741 if (!listener->nodup) {
1742 if (!rbtree_insert(pl, &request->packet)) {
1743 RERROR("Failed to insert request in the list of live requests: discarding it");
1744 request_done(request, FR_ACTION_DONE);
1748 request->in_request_hash = true;
1752 * Process it. Send a response, and free it.
1754 if (listener->synchronous) {
1756 rad_assert(listener->type != RAD_LISTEN_DETAIL);
1759 request->listener->decode(request->listener, request);
1760 request->username = fr_pair_find_by_num(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1761 request->password = fr_pair_find_by_num(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
1765 if (request->reply->code != 0) {
1766 request->listener->send(request->listener, request);
1768 RDEBUG("Not sending reply");
1772 * Don't do delayed reject. Oh well.
1774 request_free(request);
1779 * Otherwise, insert it into the state machine.
1780 * The child threads will take care of processing it.
1782 request_queue_or_run(request, request_running);
1788 static REQUEST *request_setup(TALLOC_CTX *ctx, rad_listen_t *listener, RADIUS_PACKET *packet,
1789 RADCLIENT *client, RAD_REQUEST_FUNP fun)
1794 * Create and initialize the new request.
1796 request = request_alloc(ctx);
1801 request->reply = rad_alloc_reply(request, packet);
1802 if (!request->reply) {
1804 talloc_free(request);
1808 request->listener = listener;
1809 request->client = client;
1810 request->packet = talloc_steal(request, packet);
1811 request->number = request_num_counter++;
1812 request->priority = listener->type;
1813 request->master_state = REQUEST_ACTIVE;
1814 request->child_state = REQUEST_RUNNING;
1815 #ifdef DEBUG_STATE_MACHINE
1816 if (rad_debug_lvl) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n",
1817 request->number, __FUNCTION__,
1818 child_state_names[request->child_state],
1819 child_state_names[REQUEST_RUNNING]);
1821 request->handle = fun;
1825 request->listener->stats.last_packet = request->packet->timestamp.tv_sec;
1826 if (packet->code == PW_CODE_ACCESS_REQUEST) {
1827 request->client->auth.last_packet = request->packet->timestamp.tv_sec;
1828 radius_auth_stats.last_packet = request->packet->timestamp.tv_sec;
1829 #ifdef WITH_ACCOUNTING
1830 } else if (packet->code == PW_CODE_ACCOUNTING_REQUEST) {
1831 request->client->acct.last_packet = request->packet->timestamp.tv_sec;
1832 radius_acct_stats.last_packet = request->packet->timestamp.tv_sec;
1835 #endif /* WITH_STATS */
1838 * Status-Server packets go to the head of the queue.
1840 if (request->packet->code == PW_CODE_STATUS_SERVER) request->priority = 0;
1843 * Set virtual server identity
1845 if (client->server) {
1846 request->server = client->server;
1847 } else if (listener->server) {
1848 request->server = listener->server;
1850 request->server = NULL;
1853 request->root = &main_config;
1855 request->listener->count++;
1859 * The request passes many of our sanity checks.
1860 * From here on in, if anything goes wrong, we
1861 * send a reject message, instead of dropping the
1866 * Build the reply template from the request.
1869 request->reply->sockfd = request->packet->sockfd;
1870 request->reply->dst_ipaddr = request->packet->src_ipaddr;
1871 request->reply->src_ipaddr = request->packet->dst_ipaddr;
1872 request->reply->dst_port = request->packet->src_port;
1873 request->reply->src_port = request->packet->dst_port;
1874 request->reply->id = request->packet->id;
1875 request->reply->code = 0; /* UNKNOWN code */
1876 memcpy(request->reply->vector, request->packet->vector,
1877 sizeof(request->reply->vector));
1878 request->reply->vps = NULL;
1879 request->reply->data = NULL;
1880 request->reply->data_len = 0;
1886 /***********************************************************************
1890 ***********************************************************************/
1893 * Timer function for all TCP sockets.
1895 static void tcp_socket_timer(void *ctx)
1897 rad_listen_t *listener = talloc_get_type_abort(ctx, rad_listen_t);
1898 listen_socket_t *sock = listener->data;
1899 struct timeval end, now;
1901 fr_socket_limit_t *limit;
1905 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return;
1907 fr_event_now(el, &now);
1909 switch (listener->type) {
1911 case RAD_LISTEN_PROXY:
1912 limit = &sock->home->limit;
1916 case RAD_LISTEN_AUTH:
1917 #ifdef WITH_ACCOUNTING
1918 case RAD_LISTEN_ACCT:
1920 limit = &sock->limit;
1928 * If we enforce a lifetime, do it now.
1930 if (limit->lifetime > 0) {
1931 end.tv_sec = sock->opened + limit->lifetime;
1934 if (timercmp(&end, &now, <=)) {
1935 listener->print(listener, buffer, sizeof(buffer));
1936 DEBUG("Reached maximum lifetime on socket %s", buffer);
1942 * Proxy sockets get frozen, so that we don't use
1943 * them for new requests. But we do keep them
1944 * open to listen for replies to requests we had
1947 if (listener->type == RAD_LISTEN_PROXY) {
1948 PTHREAD_MUTEX_LOCK(&proxy_mutex);
1949 if (!fr_packet_list_socket_freeze(proxy_list,
1951 ERROR("Fatal error freezing socket: %s", fr_strerror());
1954 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
1959 * Mark the socket as "don't use if at all possible".
1961 listener->status = RAD_LISTEN_STATUS_FROZEN;
1962 event_new_fd(listener);
1971 * Enforce an idle timeout.
1973 if (limit->idle_timeout > 0) {
1974 struct timeval idle;
1976 rad_assert(sock->last_packet != 0);
1977 idle.tv_sec = sock->last_packet + limit->idle_timeout;
1980 if (timercmp(&idle, &now, <=)) {
1981 listener->print(listener, buffer, sizeof(buffer));
1982 DEBUG("Reached idle timeout on socket %s", buffer);
1987 * Enforce the minimum of idle timeout or lifetime.
1989 if (timercmp(&idle, &end, <)) {
1995 * Wake up at t + 0.5s. The code above checks if the timers
1996 * are <= t. This addition gives us a bit of leeway.
1998 end.tv_usec = USEC / 2;
2001 if (!fr_event_insert(el, tcp_socket_timer, listener, &end, &sock->ev)) {
2002 rad_panic("Failed to insert event");
2009 * Add +/- 2s of jitter, as suggested in RFC 3539
2012 static void add_jitter(struct timeval *when)
2019 jitter ^= (jitter >> 10);
2020 jitter &= ((1 << 22) - 1); /* 22 bits of 1 */
2023 * Add in ~ (4 * USEC) of jitter.
2025 tv_add(when, jitter);
2029 * Called by socket_del to remove requests with this socket
2031 static int eol_proxy_listener(void *ctx, void *data)
2033 rad_listen_t *this = talloc_get_type_abort(ctx, rad_listen_t);
2034 RADIUS_PACKET **proxy_p = data;
2037 request = fr_packet2myptr(REQUEST, proxy, proxy_p);
2038 if (request->proxy_listener != this) return 0;
2041 * The normal "remove_from_proxy_hash" tries to grab the
2042 * proxy mutex. We already have it held, so grabbing it
2043 * again will cause a deadlock. Instead, call the "no
2044 * lock" version of the function.
2046 rad_assert(request->in_proxy_hash == true);
2047 remove_from_proxy_hash_nl(request, false);
2050 * Don't mark it as DONE. The client can retransmit, and
2051 * the packet SHOULD be re-proxied somewhere else.
2053 * Return "2" means that the rbtree code will remove it
2054 * from the tree, and we don't need to do it ourselves.
2058 #endif /* WITH_PROXY */
2060 static int eol_listener(void *ctx, void *data)
2062 rad_listen_t *this = talloc_get_type_abort(ctx, rad_listen_t);
2063 RADIUS_PACKET **packet_p = data;
2066 request = fr_packet2myptr(REQUEST, packet, packet_p);
2067 if (request->listener != this) return 0;
2069 request->master_state = REQUEST_STOP_PROCESSING;
2070 request->process = request_done;
2074 #endif /* WITH_TCP */
2077 /***********************************************************************
2079 * Proxy handlers for the state machine.
2081 ***********************************************************************/
2084 * Called with the proxy mutex held
2086 static void remove_from_proxy_hash_nl(REQUEST *request, bool yank)
2088 VERIFY_REQUEST(request);
2090 if (!request->in_proxy_hash) return;
2092 fr_packet_list_id_free(proxy_list, request->proxy, yank);
2093 request->in_proxy_hash = false;
2096 * On the FIRST reply, decrement the count of outstanding
2097 * requests. Note that this is NOT the count of sent
2098 * packets, but whether or not the home server has
2101 if (request->home_server &&
2102 request->home_server->currently_outstanding) {
2103 request->home_server->currently_outstanding--;
2106 * If we're NOT sending it packets, AND it's been
2107 * a while since we got a response, then we don't
2108 * know if it's alive or dead.
2110 if ((request->home_server->currently_outstanding == 0) &&
2111 (request->home_server->state == HOME_STATE_ALIVE)) {
2112 struct timeval when, now;
2114 when.tv_sec = request->home_server->last_packet_recv ;
2117 timeradd(&when, request_response_window(request), &when);
2118 gettimeofday(&now, NULL);
2121 * last_packet + response_window
2123 * We *administratively* mark the home
2124 * server as "unknown" state, because we
2125 * haven't seen a packet for a while.
2127 if (timercmp(&now, &when, >)) {
2128 request->home_server->state = HOME_STATE_UNKNOWN;
2129 request->home_server->last_packet_sent = 0;
2130 request->home_server->last_packet_recv = 0;
2136 rad_assert(request->proxy_listener != NULL);
2137 request->proxy_listener->count--;
2139 request->proxy_listener = NULL;
2142 * Got from YES in hash, to NO, not in hash while we hold
2143 * the mutex. This guarantees that when another thread
2144 * grabs the mutex, the "not in hash" flag is correct.
2148 static void remove_from_proxy_hash(REQUEST *request)
2150 VERIFY_REQUEST(request);
2153 * Check this without grabbing the mutex because it's a
2154 * lot faster that way.
2156 if (!request->in_proxy_hash) return;
2159 * The "not in hash" flag is definitive. However, if the
2160 * flag says that it IS in the hash, there might still be
2161 * a race condition where it isn't.
2163 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2165 if (!request->in_proxy_hash) {
2166 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2170 remove_from_proxy_hash_nl(request, true);
2172 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2175 static int insert_into_proxy_hash(REQUEST *request)
2179 bool success = false;
2180 void *proxy_listener;
2182 VERIFY_REQUEST(request);
2184 rad_assert(request->proxy != NULL);
2185 rad_assert(request->home_server != NULL);
2186 rad_assert(proxy_list != NULL);
2189 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2190 proxy_listener = NULL;
2191 request->num_proxied_requests = 1;
2192 request->num_proxied_responses = 0;
2194 for (tries = 0; tries < 2; tries++) {
2196 listen_socket_t *sock;
2198 RDEBUG3("proxy: Trying to allocate ID (%d/2)", tries);
2199 success = fr_packet_list_id_alloc(proxy_list,
2200 request->home_server->proto,
2201 &request->proxy, &proxy_listener);
2204 if (tries > 0) continue; /* try opening new socket only once */
2206 #ifdef HAVE_PTHREAD_H
2207 if (proxy_no_new_sockets) break;
2210 RDEBUG3("proxy: Trying to open a new listener to the home server");
2211 this = proxy_new_listener(proxy_ctx, request->home_server, 0);
2213 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2217 request->proxy->src_port = 0; /* Use any new socket */
2218 proxy_listener = this;
2221 if (!fr_packet_list_socket_add(proxy_list, this->fd,
2223 &sock->other_ipaddr, sock->other_port,
2226 #ifdef HAVE_PTHREAD_H
2227 proxy_no_new_sockets = true;
2229 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2232 * This is bad. However, the
2233 * packet list now supports 256
2234 * open sockets, which should
2235 * minimize this problem.
2237 ERROR("Failed adding proxy socket: %s",
2243 * Add it to the event loop. Ensure that we have
2244 * only one mutex locked at a time.
2246 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2247 radius_update_listener(this);
2248 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2251 if (!proxy_listener || !success) {
2252 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2253 REDEBUG2("proxy: Failed allocating Id for proxied request");
2255 request->proxy_listener = NULL;
2256 request->in_proxy_hash = false;
2260 rad_assert(request->proxy->id >= 0);
2262 request->proxy_listener = proxy_listener;
2263 request->in_proxy_hash = true;
2264 RDEBUG3("proxy: request is now in proxy hash");
2267 * Keep track of maximum outstanding requests to a
2268 * particular home server. 'max_outstanding' is
2269 * enforced in home_server_ldb(), in realms.c.
2271 request->home_server->currently_outstanding++;
2274 request->proxy_listener->count++;
2277 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2279 RDEBUG3("proxy: allocating destination %s port %d - Id %d",
2280 inet_ntop(request->proxy->dst_ipaddr.af,
2281 &request->proxy->dst_ipaddr.ipaddr, buf, sizeof(buf)),
2282 request->proxy->dst_port,
2283 request->proxy->id);
2288 static int process_proxy_reply(REQUEST *request, RADIUS_PACKET *reply)
2291 int post_proxy_type = 0;
2294 VERIFY_REQUEST(request);
2297 * There may be a proxy reply, but it may be too late.
2299 if (!request->home_server->server && !request->proxy_listener) return 0;
2302 * Delete any reply we had accumulated until now.
2304 RDEBUG2("Clearing existing &reply: attributes");
2305 fr_pair_list_free(&request->reply->vps);
2308 * Run the packet through the post-proxy stage,
2309 * BEFORE playing games with the attributes.
2311 vp = fr_pair_find_by_num(request->config, PW_POST_PROXY_TYPE, 0, TAG_ANY);
2313 post_proxy_type = vp->vp_integer;
2315 * If we have a proxy_reply, and it was a reject, or a NAK
2316 * setup Post-Proxy <type>.
2318 * If the <type> doesn't have a section, then the Post-Proxy
2319 * section is ignored.
2322 DICT_VALUE *dval = NULL;
2324 switch (reply->code) {
2325 case PW_CODE_ACCESS_REJECT:
2326 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Reject");
2327 if (dval) post_proxy_type = dval->value;
2330 case PW_CODE_DISCONNECT_NAK:
2331 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, fr_packet_codes[reply->code]);
2332 if (dval) post_proxy_type = dval->value;
2335 case PW_CODE_COA_NAK:
2336 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, fr_packet_codes[reply->code]);
2337 if (dval) post_proxy_type = dval->value;
2345 * Create config:Post-Proxy-Type
2348 vp = radius_pair_create(request, &request->config, PW_POST_PROXY_TYPE, 0);
2349 vp->vp_integer = dval->value;
2353 if (post_proxy_type > 0) RDEBUG2("Found Post-Proxy-Type %s",
2354 dict_valnamebyattr(PW_POST_PROXY_TYPE, 0, post_proxy_type));
2357 VERIFY_PACKET(reply);
2360 * Decode the packet if required.
2362 if (request->proxy_listener) {
2363 rcode = request->proxy_listener->decode(request->proxy_listener, request);
2364 debug_packet(request, reply, true);
2367 * Pro-actively remove it from the proxy hash.
2368 * This is later than in 2.1.x, but it means that
2369 * the replies are authenticated before being
2370 * removed from the hash.
2373 (request->num_proxied_requests <= request->num_proxied_responses)) {
2374 remove_from_proxy_hash(request);
2377 rad_assert(!request->in_proxy_hash);
2379 } else if (request->in_proxy_hash) {
2380 remove_from_proxy_hash(request);
2383 if (request->home_pool && request->home_pool->virtual_server) {
2384 char const *old_server = request->server;
2386 request->server = request->home_pool->virtual_server;
2387 RDEBUG2("server %s {", request->server);
2389 rcode = process_post_proxy(post_proxy_type, request);
2392 request->server = old_server;
2394 rcode = process_post_proxy(post_proxy_type, request);
2398 if (request->packet->code == request->proxy->code) {
2400 * Don't run the next bit if we originated a CoA
2401 * packet, after receiving an Access-Request or
2402 * Accounting-Request.
2407 * There may NOT be a proxy reply, as we may be
2408 * running Post-Proxy-Type = Fail.
2411 fr_pair_add(&request->reply->vps, fr_pair_list_copy(request->reply, reply->vps));
2414 * Delete the Proxy-State Attributes from
2415 * the reply. These include Proxy-State
2416 * attributes from us and remote server.
2418 fr_pair_delete_by_num(&request->reply->vps, PW_PROXY_STATE, 0, TAG_ANY);
2421 vp = fr_pair_find_by_num(request->config, PW_RESPONSE_PACKET_TYPE, 0, TAG_ANY);
2422 if (vp && (vp->vp_integer != 256)) {
2423 request->proxy_reply = rad_alloc_reply(request, request->proxy);
2424 request->proxy_reply->code = vp->vp_integer;
2431 default: /* Don't do anything */
2433 case RLM_MODULE_FAIL:
2436 case RLM_MODULE_HANDLED:
2443 static void mark_home_server_alive(REQUEST *request, home_server_t *home)
2447 home->state = HOME_STATE_ALIVE;
2448 home->response_timeouts = 0;
2449 exec_trigger(request, home->cs, "home_server.alive", false);
2450 home->currently_outstanding = 0;
2451 home->num_sent_pings = 0;
2452 home->num_received_pings = 0;
2453 gettimeofday(&home->revive_time, NULL);
2455 fr_event_delete(el, &home->ev);
2457 RPROXY("Marking home server %s port %d alive",
2458 inet_ntop(request->proxy->dst_ipaddr.af,
2459 &request->proxy->dst_ipaddr.ipaddr,
2460 buffer, sizeof(buffer)),
2461 request->proxy->dst_port);
2465 int request_proxy_reply(RADIUS_PACKET *packet)
2467 RADIUS_PACKET **proxy_p;
2472 VERIFY_PACKET(packet);
2474 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2475 proxy_p = fr_packet_list_find_byreply(proxy_list, packet);
2478 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2479 PROXY("No outstanding request was found for %s packet from host %s port %d - ID %u",
2480 fr_packet_codes[packet->code],
2481 inet_ntop(packet->src_ipaddr.af,
2482 &packet->src_ipaddr.ipaddr,
2483 buffer, sizeof(buffer)),
2484 packet->src_port, packet->id);
2488 request = fr_packet2myptr(REQUEST, proxy, proxy_p);
2490 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2493 * No reply, BUT the current packet fails verification:
2494 * ignore it. This does the MD5 calculations in the
2495 * server core, but I guess we can fix that later.
2497 if (!request->proxy_reply &&
2498 (rad_verify(packet, request->proxy,
2499 request->home_server->secret) != 0)) {
2500 DEBUG("Ignoring spoofed proxy reply. Signature is invalid");
2505 * The home server sent us a packet which doesn't match
2506 * something we have: ignore it. This is done only to
2507 * catch the case of broken systems.
2509 if (request->proxy_reply &&
2510 (memcmp(request->proxy_reply->vector,
2512 sizeof(request->proxy_reply->vector)) != 0)) {
2513 RDEBUG2("Ignoring conflicting proxy reply");
2517 gettimeofday(&now, NULL);
2520 * Status-Server packets don't count as real packets.
2522 if (request->proxy->code != PW_CODE_STATUS_SERVER) {
2523 listen_socket_t *sock = request->proxy_listener->data;
2525 request->home_server->last_packet_recv = now.tv_sec;
2526 sock->last_packet = now.tv_sec;
2529 request->num_proxied_responses++;
2532 * If we have previously seen a reply, ignore the
2535 if (request->proxy_reply) {
2536 RDEBUG2("Discarding duplicate reply from host %s port %d - ID: %d",
2537 inet_ntop(packet->src_ipaddr.af,
2538 &packet->src_ipaddr.ipaddr,
2539 buffer, sizeof(buffer)),
2540 packet->src_port, packet->id);
2545 * Call the state machine to do something useful with the
2548 request->proxy_reply = talloc_steal(request, packet);
2549 packet->timestamp = now;
2550 request->priority = RAD_LISTEN_PROXY;
2554 * Update the proxy listener stats here, because only one
2555 * thread accesses that at a time. The home_server and
2556 * main proxy_*_stats structures are updated once the
2557 * request is cleaned up.
2559 request->proxy_listener->stats.total_responses++;
2561 request->home_server->stats.last_packet = packet->timestamp.tv_sec;
2562 request->proxy_listener->stats.last_packet = packet->timestamp.tv_sec;
2564 switch (request->proxy->code) {
2565 case PW_CODE_ACCESS_REQUEST:
2566 proxy_auth_stats.last_packet = packet->timestamp.tv_sec;
2568 if (request->proxy_reply->code == PW_CODE_ACCESS_ACCEPT) {
2569 request->proxy_listener->stats.total_access_accepts++;
2571 } else if (request->proxy_reply->code == PW_CODE_ACCESS_REJECT) {
2572 request->proxy_listener->stats.total_access_rejects++;
2574 } else if (request->proxy_reply->code == PW_CODE_ACCESS_CHALLENGE) {
2575 request->proxy_listener->stats.total_access_challenges++;
2579 #ifdef WITH_ACCOUNTING
2580 case PW_CODE_ACCOUNTING_REQUEST:
2581 proxy_acct_stats.last_packet = packet->timestamp.tv_sec;
2583 request->proxy_listener->stats.total_responses++;
2584 proxy_acct_stats.last_packet = packet->timestamp.tv_sec;
2590 case PW_CODE_COA_REQUEST:
2591 request->proxy_listener->stats.total_responses++;
2592 proxy_coa_stats.last_packet = packet->timestamp.tv_sec;
2595 case PW_CODE_DISCONNECT_REQUEST:
2596 request->proxy_listener->stats.total_responses++;
2597 proxy_dsc_stats.last_packet = packet->timestamp.tv_sec;
2607 * If we hadn't been sending the home server packets for
2608 * a while, just mark it alive. Or, if it was zombie,
2609 * it's now responded, and is therefore alive.
2611 if ((request->home_server->state == HOME_STATE_UNKNOWN) ||
2612 (request->home_server->state == HOME_STATE_ZOMBIE)) {
2613 mark_home_server_alive(request, request->home_server);
2617 * Tell the request state machine that we have a proxy
2618 * reply. Depending on the function, this should either
2619 * ignore it, or process it.
2621 request->process(request, FR_ACTION_PROXY_REPLY);
2627 static int setup_post_proxy_fail(REQUEST *request)
2629 DICT_VALUE const *dval = NULL;
2632 VERIFY_REQUEST(request);
2634 if (request->proxy->code == PW_CODE_ACCESS_REQUEST) {
2635 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0,
2636 "Fail-Authentication");
2637 #ifdef WITH_ACCOUNTING
2638 } else if (request->proxy->code == PW_CODE_ACCOUNTING_REQUEST) {
2639 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0,
2644 } else if (request->proxy->code == PW_CODE_COA_REQUEST) {
2645 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-CoA");
2647 } else if (request->proxy->code == PW_CODE_DISCONNECT_REQUEST) {
2648 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-Disconnect");
2651 WARN("Unknown packet type in Post-Proxy-Type Fail: ignoring");
2655 if (!dval) dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail");
2658 fr_pair_delete_by_num(&request->config, PW_POST_PROXY_TYPE, 0, TAG_ANY);
2662 vp = fr_pair_find_by_num(request->config, PW_POST_PROXY_TYPE, 0, TAG_ANY);
2663 if (!vp) vp = radius_pair_create(request, &request->config,
2664 PW_POST_PROXY_TYPE, 0);
2665 vp->vp_integer = dval->value;
2671 /** Process a request after the proxy has timed out.
2673 * Run the packet through Post-Proxy-Type Fail
2676 * digraph proxy_no_reply {
2679 * proxy_no_reply -> dup [ label = "DUP", arrowhead = "none" ];
2680 * proxy_no_reply -> timer [ label = "TIMER < max_request_time" ];
2681 * proxy_no_reply -> proxy_reply_too_late [ label = "PROXY_REPLY" arrowhead = "none"];
2682 * proxy_no_reply -> process_proxy_reply [ label = "RUN" ];
2683 * proxy_no_reply -> done [ label = "TIMER >= timeout" ];
2687 static void proxy_no_reply(REQUEST *request, int action)
2689 VERIFY_REQUEST(request);
2691 TRACE_STATE_MACHINE;
2696 request_dup(request);
2699 case FR_ACTION_TIMER:
2700 (void) request_max_time(request);
2703 case FR_ACTION_PROXY_REPLY:
2704 proxy_reply_too_late(request);
2708 if (process_proxy_reply(request, NULL)) {
2709 request->handle(request);
2711 request_finish(request, action);
2715 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
2720 /** Process the request after receiving a proxy reply.
2722 * Throught the post-proxy section, and the through the handler
2726 * digraph proxy_running {
2729 * proxy_running -> dup [ label = "DUP", arrowhead = "none" ];
2730 * proxy_running -> timer [ label = "TIMER < max_request_time" ];
2731 * proxy_running -> process_proxy_reply [ label = "RUN" ];
2732 * proxy_running -> done [ label = "TIMER >= timeout" ];
2736 static void proxy_running(REQUEST *request, int action)
2738 VERIFY_REQUEST(request);
2740 TRACE_STATE_MACHINE;
2745 request_dup(request);
2748 case FR_ACTION_TIMER:
2749 (void) request_max_time(request);
2753 if (process_proxy_reply(request, request->proxy_reply)) {
2754 request->handle(request);
2756 request_finish(request, action);
2759 default: /* duplicate proxy replies are suppressed */
2760 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
2765 /** Determine if a #REQUEST needs to be proxied, and perform pre-proxy operations
2767 * Whether a request will be proxied is determined by the attributes present
2768 * in request->config. If any of the following attributes are found, the
2769 * request may be proxied.
2771 * The key attributes are:
2772 * - PW_PROXY_TO_REALM - Specifies a realm the request should be proxied to.
2773 * - PW_HOME_SERVER_POOL - Specifies a specific home server pool to proxy to.
2774 * - PW_PACKET_DST_IP_ADDRESS - Specifies a specific IPv4 home server to proxy to.
2775 * - PW_PACKET_DST_IPV6_ADDRESS - Specifies a specific IPv6 home server to proxy to.
2777 * Certain packet types such as #PW_CODE_STATUS_SERVER will never be proxied.
2779 * If request should be proxied, will:
2780 * - Add request:Proxy-State
2781 * - Strip the current username value of its realm (depending on config)
2782 * - Create a CHAP-Challenge from the original request vector, if one doesn't already
2784 * - Call the pre-process section in the current server, or in the virtual server
2785 * associated with the home server pool we're proxying to.
2787 * @todo A lot of this logic is RADIUS specific, and should be moved out into a protocol
2788 * specific function.
2790 * @param request The #REQUEST to evaluate for proxying.
2791 * @return 0 if not proxying, 1 if request should be proxied, -1 on error.
2793 static int request_will_proxy(REQUEST *request)
2795 int rcode, pre_proxy_type = 0;
2796 char const *realmname = NULL;
2797 VALUE_PAIR *vp, *strippedname;
2798 home_server_t *home;
2799 REALM *realm = NULL;
2800 home_pool_t *pool = NULL;
2802 VERIFY_REQUEST(request);
2804 if (!request->root->proxy_requests) return 0;
2805 if (request->packet->dst_port == 0) return 0;
2806 if (request->packet->code == PW_CODE_STATUS_SERVER) return 0;
2807 if (request->in_proxy_hash) return 0;
2810 * FIXME: for 3.0, allow this only for rejects?
2812 if (request->reply->code != 0) return 0;
2814 vp = fr_pair_find_by_num(request->config, PW_PROXY_TO_REALM, 0, TAG_ANY);
2816 realm = realm_find2(vp->vp_strvalue);
2818 REDEBUG2("Cannot proxy to unknown realm %s",
2823 realmname = vp->vp_strvalue;
2826 * Figure out which pool to use.
2828 if (request->packet->code == PW_CODE_ACCESS_REQUEST) {
2829 pool = realm->auth_pool;
2831 #ifdef WITH_ACCOUNTING
2832 } else if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) {
2833 pool = realm->acct_pool;
2837 } else if ((request->packet->code == PW_CODE_COA_REQUEST) ||
2838 (request->packet->code == PW_CODE_DISCONNECT_REQUEST)) {
2839 pool = realm->coa_pool;
2846 } else if ((vp = fr_pair_find_by_num(request->config, PW_HOME_SERVER_POOL, 0, TAG_ANY)) != NULL) {
2849 switch (request->packet->code) {
2850 case PW_CODE_ACCESS_REQUEST:
2851 pool_type = HOME_TYPE_AUTH;
2854 #ifdef WITH_ACCOUNTING
2855 case PW_CODE_ACCOUNTING_REQUEST:
2856 pool_type = HOME_TYPE_ACCT;
2861 case PW_CODE_COA_REQUEST:
2862 case PW_CODE_DISCONNECT_REQUEST:
2863 pool_type = HOME_TYPE_COA;
2871 pool = home_pool_byname(vp->vp_strvalue, pool_type);
2874 * Send it directly to a home server (i.e. NAS)
2876 } else if (((vp = fr_pair_find_by_num(request->config, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY)) != NULL) ||
2877 ((vp = fr_pair_find_by_num(request->config, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY)) != NULL)) {
2879 fr_ipaddr_t dst_ipaddr;
2881 memset(&dst_ipaddr, 0, sizeof(dst_ipaddr));
2883 if (vp->da->attr == PW_PACKET_DST_IP_ADDRESS) {
2884 dst_ipaddr.af = AF_INET;
2885 dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
2886 dst_ipaddr.prefix = 32;
2888 dst_ipaddr.af = AF_INET6;
2889 memcpy(&dst_ipaddr.ipaddr.ip6addr, &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
2890 dst_ipaddr.prefix = 128;
2893 vp = fr_pair_find_by_num(request->config, PW_PACKET_DST_PORT, 0, TAG_ANY);
2895 if (request->packet->code == PW_CODE_ACCESS_REQUEST) {
2896 dst_port = PW_AUTH_UDP_PORT;
2898 #ifdef WITH_ACCOUNTING
2899 } else if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) {
2900 dst_port = PW_ACCT_UDP_PORT;
2904 } else if ((request->packet->code == PW_CODE_COA_REQUEST) ||
2905 (request->packet->code == PW_CODE_DISCONNECT_REQUEST)) {
2906 dst_port = PW_COA_UDP_PORT;
2908 } else { /* shouldn't happen for RADIUS... */
2913 dst_port = vp->vp_integer;
2917 * Nothing does CoA over TCP.
2919 home = home_server_find(&dst_ipaddr, dst_port, IPPROTO_UDP);
2923 WARN("No such home server %s port %u",
2924 inet_ntop(dst_ipaddr.af, &dst_ipaddr.ipaddr, buffer, sizeof(buffer)),
2925 (unsigned int) dst_port);
2930 * The home server is alive (or may be alive).
2931 * Send the packet to the IP.
2933 if (home->state != HOME_STATE_IS_DEAD) goto do_home;
2936 * The home server is dead. If you wanted
2937 * fail-over, you should have proxied to a pool.
2948 RWDEBUG2("Cancelling proxy as no home pool exists");
2952 if (request->listener->synchronous) {
2953 WARN("Cannot proxy a request which is from a 'synchronous' socket");
2957 request->home_pool = pool;
2959 home = home_server_ldb(realmname, pool, request);
2962 REDEBUG2("Failed to find live home server: Cancelling proxy");
2967 home_server_update_request(home, request);
2971 * Once we've decided to proxy a request, we cannot send
2972 * a CoA packet. So we free up any CoA packet here.
2974 if (request->coa) request_done(request->coa, FR_ACTION_DONE);
2978 * Remember that we sent the request to a Realm.
2980 if (realmname) pair_make_request("Realm", realmname, T_OP_EQ);
2983 * Strip the name, if told to.
2985 * Doing it here catches the case of proxied tunneled
2988 if (realm && (realm->strip_realm == true) &&
2989 (strippedname = fr_pair_find_by_num(request->proxy->vps, PW_STRIPPED_USER_NAME, 0, TAG_ANY)) != NULL) {
2991 * If there's a Stripped-User-Name attribute in
2992 * the request, then use THAT as the User-Name
2993 * for the proxied request, instead of the
2996 * This is done by making a copy of the
2997 * Stripped-User-Name attribute, turning it into
2998 * a User-Name attribute, deleting the
2999 * Stripped-User-Name and User-Name attributes
3000 * from the vps list, and making the new
3001 * User-Name the head of the vps list.
3003 vp = fr_pair_find_by_num(request->proxy->vps, PW_USER_NAME, 0, TAG_ANY);
3006 vp = radius_pair_create(NULL, NULL,
3008 rad_assert(vp != NULL); /* handled by above function */
3009 /* Insert at the START of the list */
3010 /* FIXME: Can't make assumptions about ordering */
3011 fr_cursor_init(&cursor, &vp);
3012 fr_cursor_merge(&cursor, request->proxy->vps);
3013 request->proxy->vps = vp;
3015 fr_pair_value_strcpy(vp, strippedname->vp_strvalue);
3018 * Do NOT delete Stripped-User-Name.
3023 * If there is no PW_CHAP_CHALLENGE attribute but
3024 * there is a PW_CHAP_PASSWORD we need to add it
3025 * since we can't use the request authenticator
3026 * anymore - we changed it.
3028 if ((request->packet->code == PW_CODE_ACCESS_REQUEST) &&
3029 fr_pair_find_by_num(request->proxy->vps, PW_CHAP_PASSWORD, 0, TAG_ANY) &&
3030 fr_pair_find_by_num(request->proxy->vps, PW_CHAP_CHALLENGE, 0, TAG_ANY) == NULL) {
3031 vp = radius_pair_create(request->proxy, &request->proxy->vps, PW_CHAP_CHALLENGE, 0);
3032 fr_pair_value_memcpy(vp, request->packet->vector, sizeof(request->packet->vector));
3036 * The RFC's say we have to do this, but FreeRADIUS
3039 vp = radius_pair_create(request->proxy, &request->proxy->vps, PW_PROXY_STATE, 0);
3040 fr_pair_value_sprintf(vp, "%u", request->packet->id);
3043 * Should be done BEFORE inserting into proxy hash, as
3044 * pre-proxy may use this information, or change it.
3046 request->proxy->code = request->packet->code;
3049 * Call the pre-proxy routines.
3051 vp = fr_pair_find_by_num(request->config, PW_PRE_PROXY_TYPE, 0, TAG_ANY);
3053 DICT_VALUE const *dval = dict_valbyattr(vp->da->attr, vp->da->vendor, vp->vp_integer);
3054 /* Must be a validation issue */
3056 RDEBUG2("Found Pre-Proxy-Type %s", dval->name);
3057 pre_proxy_type = vp->vp_integer;
3061 * home_pool may be NULL when originating CoA packets,
3062 * because they go directly to an IP address.
3064 if (request->home_pool && request->home_pool->virtual_server) {
3065 char const *old_server = request->server;
3067 request->server = request->home_pool->virtual_server;
3069 RDEBUG2("server %s {", request->server);
3071 rcode = process_pre_proxy(pre_proxy_type, request);
3075 request->server = old_server;
3077 rcode = process_pre_proxy(pre_proxy_type, request);
3081 case RLM_MODULE_FAIL:
3082 case RLM_MODULE_INVALID:
3083 case RLM_MODULE_NOTFOUND:
3084 case RLM_MODULE_USERLOCK:
3086 /* FIXME: debug print failed stuff */
3089 case RLM_MODULE_REJECT:
3090 case RLM_MODULE_HANDLED:
3094 * Only proxy the packet if the pre-proxy code succeeded.
3096 case RLM_MODULE_NOOP:
3098 case RLM_MODULE_UPDATED:
3103 static int proxy_to_virtual_server(REQUEST *request)
3107 if (request->packet->dst_port == 0) {
3108 WARN("Cannot proxy an internal request");
3112 DEBUG("Proxying to virtual server %s",
3113 request->home_server->server);
3116 * Packets to virtual servers don't get
3117 * retransmissions sent to them. And the virtual
3118 * server is run ONLY if we have no child
3119 * threads, or we're running in a child thread.
3121 rad_assert(!spawn_flag || !we_are_master());
3123 fake = request_alloc_fake(request);
3125 fake->packet->vps = fr_pair_list_copy(fake->packet, request->packet->vps);
3126 talloc_free(request->proxy);
3128 fake->server = request->home_server->server;
3129 fake->handle = request->handle;
3130 fake->process = NULL; /* should never be run for anything */
3133 * Run the virtual server.
3135 request_running(fake, FR_ACTION_RUN);
3137 request->proxy = talloc_steal(request, fake->packet);
3138 fake->packet = NULL;
3139 request->proxy_reply = talloc_steal(request, fake->reply);
3145 * No reply code, toss the reply we have,
3146 * and do post-proxy-type Fail.
3148 if (!request->proxy_reply->code) {
3149 TALLOC_FREE(request->proxy_reply);
3150 setup_post_proxy_fail(request);
3154 * Do the proxy reply (if any)
3156 if (process_proxy_reply(request, request->proxy_reply)) {
3157 request->handle(request);
3160 return -1; /* so we call request_finish */
3164 static int request_proxy(REQUEST *request)
3168 VERIFY_REQUEST(request);
3170 rad_assert(request->parent == NULL);
3171 rad_assert(request->home_server != NULL);
3173 if (request->master_state == REQUEST_STOP_PROCESSING) return 0;
3177 RWDEBUG("Cannot proxy and originate CoA packets at the same time. Cancelling CoA request");
3178 request_done(request->coa, FR_ACTION_DONE);
3183 * The request may need sending to a virtual server.
3184 * This code is more than a little screwed up. The rest
3185 * of the state machine doesn't handle parent / child
3186 * relationships well. i.e. if the child request takes
3187 * too long, the core will mark the *parent* as "stop
3188 * processing". And the child will continue without
3189 * knowing anything...
3191 * So, we have some horrible hacks to get around that.
3193 if (request->home_server->server) return proxy_to_virtual_server(request);
3196 * We're actually sending a proxied packet. Do that now.
3198 if (!request->in_proxy_hash && !insert_into_proxy_hash(request)) {
3199 RPROXY("Failed to insert request into the proxy list");
3203 rad_assert(request->proxy->id >= 0);
3205 if (rad_debug_lvl) {
3206 struct timeval *response_window;
3208 response_window = request_response_window(request);
3211 if (request->home_server->tls) {
3212 RDEBUG2("Proxying request to home server %s port %d (TLS) timeout %d.%06d",
3213 inet_ntop(request->proxy->dst_ipaddr.af,
3214 &request->proxy->dst_ipaddr.ipaddr,
3215 buffer, sizeof(buffer)),
3216 request->proxy->dst_port,
3217 (int) response_window->tv_sec, (int) response_window->tv_usec);
3220 RDEBUG2("Proxying request to home server %s port %d timeout %d.%06d",
3221 inet_ntop(request->proxy->dst_ipaddr.af,
3222 &request->proxy->dst_ipaddr.ipaddr,
3223 buffer, sizeof(buffer)),
3224 request->proxy->dst_port,
3225 (int) response_window->tv_sec, (int) response_window->tv_usec);
3230 gettimeofday(&request->proxy->timestamp, NULL);
3231 request->home_server->last_packet_sent = request->proxy->timestamp.tv_sec;
3234 * Encode the packet before we do anything else.
3236 request->proxy_listener->encode(request->proxy_listener, request);
3237 debug_packet(request, request->proxy, false);
3240 * Set the state function, then the state, no child, and
3243 * The order here is different from other state changes
3244 * due to race conditions with replies from the home
3247 request->process = proxy_wait_for_reply;
3248 request->child_state = REQUEST_PROXIED;
3249 request->component = "<REQUEST_PROXIED>";
3250 request->module = "";
3254 * And send the packet.
3256 request->proxy_listener->send(request->proxy_listener, request);
3261 * Proxy the packet as if it was new.
3263 static int request_proxy_anew(REQUEST *request)
3265 home_server_t *home;
3267 VERIFY_REQUEST(request);
3270 * Delete the request from the proxy list.
3272 * The packet list code takes care of ensuring that IDs
3273 * aren't reused until all 256 IDs have been used. So
3274 * there's a 1/256 chance of re-using the same ID when
3275 * we're sending to the same home server. Which is
3278 remove_from_proxy_hash(request);
3281 * Find a live home server for the request.
3283 home = home_server_ldb(NULL, request->home_pool, request);
3285 REDEBUG2("Failed to find live home server for request");
3287 if (setup_post_proxy_fail(request)) {
3288 request_queue_or_run(request, proxy_running);
3290 gettimeofday(&request->reply->timestamp, NULL);
3291 request_cleanup_delay_init(request);
3296 #ifdef WITH_ACCOUNTING
3298 * Update the Acct-Delay-Time attribute, since the LAST
3299 * time we tried to retransmit this packet.
3301 if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) {
3304 vp = fr_pair_find_by_num(request->proxy->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY);
3305 if (!vp) vp = radius_pair_create(request->proxy,
3306 &request->proxy->vps,
3307 PW_ACCT_DELAY_TIME, 0);
3311 gettimeofday(&now, NULL);
3312 vp->vp_integer += now.tv_sec - request->proxy->timestamp.tv_sec;
3318 * May have failed over to a "fallback" virtual server.
3319 * If so, run that instead of doing proxying to a real
3323 request->home_server = home;
3324 TALLOC_FREE(request->proxy);
3326 (void) proxy_to_virtual_server(request);
3330 home_server_update_request(home, request);
3332 if (!insert_into_proxy_hash(request)) {
3333 RPROXY("Failed to insert retransmission into the proxy list");
3334 goto post_proxy_fail;
3338 * Free the old packet, to force re-encoding
3340 talloc_free(request->proxy->data);
3341 request->proxy->data = NULL;
3342 request->proxy->data_len = 0;
3344 if (request_proxy(request) != 1) goto post_proxy_fail;
3350 /** Ping a home server.
3353 static void request_ping(REQUEST *request, int action)
3355 home_server_t *home = request->home_server;
3358 VERIFY_REQUEST(request);
3360 TRACE_STATE_MACHINE;
3364 case FR_ACTION_TIMER:
3365 ERROR("No response to status check %d ID %u for home server %s port %d",
3368 inet_ntop(request->proxy->dst_ipaddr.af,
3369 &request->proxy->dst_ipaddr.ipaddr,
3370 buffer, sizeof(buffer)),
3371 request->proxy->dst_port);
3374 case FR_ACTION_PROXY_REPLY:
3375 rad_assert(request->in_proxy_hash);
3377 request->home_server->num_received_pings++;
3378 RPROXY("Received response to status check %d ID %u (%d in current sequence)",
3379 request->number, request->proxy->id, home->num_received_pings);
3382 * Remove the request from any hashes
3384 fr_event_delete(el, &request->ev);
3385 remove_from_proxy_hash(request);
3388 * The control socket may have marked the home server as
3389 * alive. OR, it may have suddenly started responding to
3390 * requests again. If so, don't re-do the "make alive"
3393 if (home->state == HOME_STATE_ALIVE) break;
3396 * It's dead, and we haven't received enough ping
3397 * responses to mark it "alive". Wait a bit.
3399 * If it's zombie, we mark it alive immediately.
3401 if ((home->state == HOME_STATE_IS_DEAD) &&
3402 (home->num_received_pings < home->num_pings_to_alive)) {
3407 * Mark it alive and delete any outstanding
3410 mark_home_server_alive(request, home);
3414 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
3418 rad_assert(!request->in_request_hash);
3419 rad_assert(request->ev == NULL);
3421 request_done(request, FR_ACTION_DONE);
3425 * Called from start of zombie period, OR after control socket
3426 * marks the home server dead.
3428 static void ping_home_server(void *ctx)
3430 home_server_t *home = talloc_get_type_abort(ctx, home_server_t);
3433 struct timeval when, now;
3435 if ((home->state == HOME_STATE_ALIVE) ||
3436 (home->ev != NULL)) {
3440 gettimeofday(&now, NULL);
3444 * We've run out of zombie time. Mark it dead.
3446 if (home->state == HOME_STATE_ZOMBIE) {
3447 when = home->zombie_period_start;
3448 when.tv_sec += home->zombie_period;
3450 if (timercmp(&when, &now, <)) {
3451 DEBUG("PING: Zombie period is over for home server %s", home->log_name);
3452 mark_home_server_dead(home, &now);
3457 * We're not supposed to be pinging it. Just wake up
3458 * when we're supposed to mark it dead.
3460 if (home->ping_check == HOME_PING_CHECK_NONE) {
3461 if (home->state == HOME_STATE_ZOMBIE) {
3462 home->when = home->zombie_period_start;
3463 home->when.tv_sec += home->zombie_period;
3464 INSERT_EVENT(ping_home_server, home);
3468 * Else mark_home_server_dead will set a timer
3469 * for revive_interval.
3475 request = request_alloc(NULL);
3476 if (!request) return;
3477 request->number = request_num_counter++;
3480 request->proxy = rad_alloc(request, true);
3481 rad_assert(request->proxy != NULL);
3483 if (home->ping_check == HOME_PING_CHECK_STATUS_SERVER) {
3484 request->proxy->code = PW_CODE_STATUS_SERVER;
3486 fr_pair_make(request->proxy, &request->proxy->vps,
3487 "Message-Authenticator", "0x00", T_OP_SET);
3489 } else if ((home->type == HOME_TYPE_AUTH) ||
3490 (home->type == HOME_TYPE_AUTH_ACCT)) {
3491 request->proxy->code = PW_CODE_ACCESS_REQUEST;
3493 fr_pair_make(request->proxy, &request->proxy->vps,
3494 "User-Name", home->ping_user_name, T_OP_SET);
3495 fr_pair_make(request->proxy, &request->proxy->vps,
3496 "User-Password", home->ping_user_password, T_OP_SET);
3497 fr_pair_make(request->proxy, &request->proxy->vps,
3498 "Service-Type", "Authenticate-Only", T_OP_SET);
3499 fr_pair_make(request->proxy, &request->proxy->vps,
3500 "Message-Authenticator", "0x00", T_OP_SET);
3502 #ifdef WITH_ACCOUNTING
3503 } else if (home->type == HOME_TYPE_ACCT) {
3504 request->proxy->code = PW_CODE_ACCOUNTING_REQUEST;
3506 fr_pair_make(request->proxy, &request->proxy->vps,
3507 "User-Name", home->ping_user_name, T_OP_SET);
3508 fr_pair_make(request->proxy, &request->proxy->vps,
3509 "Acct-Status-Type", "Stop", T_OP_SET);
3510 fr_pair_make(request->proxy, &request->proxy->vps,
3511 "Acct-Session-Id", "00000000", T_OP_SET);
3512 vp = fr_pair_make(request->proxy, &request->proxy->vps,
3513 "Event-Timestamp", "0", T_OP_SET);
3514 vp->vp_date = now.tv_sec;
3519 * Unkown home server type.
3521 talloc_free(request);
3525 vp = fr_pair_make(request->proxy, &request->proxy->vps,
3526 "NAS-Identifier", "", T_OP_SET);
3528 fr_pair_value_sprintf(vp, "Status Check %u. Are you alive?",
3529 home->num_sent_pings);
3533 request->proxy->proto = home->proto;
3535 request->proxy->src_ipaddr = home->src_ipaddr;
3536 request->proxy->dst_ipaddr = home->ipaddr;
3537 request->proxy->dst_port = home->port;
3538 request->home_server = home;
3539 #ifdef DEBUG_STATE_MACHINE
3540 if (rad_debug_lvl) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n", request->number, __FUNCTION__,
3541 child_state_names[request->child_state],
3542 child_state_names[REQUEST_DONE]);
3543 if (rad_debug_lvl) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_ping");
3545 #ifdef HAVE_PTHREAD_H
3546 rad_assert(request->child_pid == NO_SUCH_CHILD_PID);
3548 request->child_state = REQUEST_PROXIED;
3549 request->process = request_ping;
3551 rad_assert(request->proxy_listener == NULL);
3553 if (!insert_into_proxy_hash(request)) {
3554 RPROXY("Failed to insert status check %d into proxy list. Discarding it.",
3557 rad_assert(!request->in_request_hash);
3558 rad_assert(!request->in_proxy_hash);
3559 rad_assert(request->ev == NULL);
3560 talloc_free(request);
3565 * Set up the timer callback.
3568 when.tv_sec += home->ping_timeout;
3570 DEBUG("PING: Waiting %u seconds for response to ping",
3571 home->ping_timeout);
3573 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3574 home->num_sent_pings++;
3576 rad_assert(request->proxy_listener != NULL);
3577 debug_packet(request, request->proxy, false);
3578 request->proxy_listener->send(request->proxy_listener,
3582 * Add +/- 2s of jitter, as suggested in RFC 3539
3583 * and in the Issues and Fixes draft.
3586 home->when.tv_sec += home->ping_interval;
3588 add_jitter(&home->when);
3590 DEBUG("PING: Next status packet in %u seconds", home->ping_interval);
3591 INSERT_EVENT(ping_home_server, home);
3594 static void home_trigger(home_server_t *home, char const *trigger)
3596 REQUEST *my_request;
3597 RADIUS_PACKET *my_packet;
3599 my_request = talloc_zero(NULL, REQUEST);
3600 my_packet = talloc_zero(my_request, RADIUS_PACKET);
3601 my_request->proxy = my_packet;
3602 my_packet->dst_ipaddr = home->ipaddr;
3603 my_packet->src_ipaddr = home->src_ipaddr;
3605 exec_trigger(my_request, home->cs, trigger, false);
3606 talloc_free(my_request);
3609 static void mark_home_server_zombie(home_server_t *home, struct timeval *now, struct timeval *response_window)
3616 rad_assert((home->state == HOME_STATE_ALIVE) ||
3617 (home->state == HOME_STATE_UNKNOWN));
3620 * We've received a real packet recently. Don't mark the
3621 * server as zombie until we've received NO packets for a
3622 * while. The "1/4" of zombie period was chosen rather
3623 * arbitrarily. It's a balance between too short, which
3624 * gives quick fail-over and fail-back, or too long,
3625 * where the proxy still sends packets to an unresponsive
3628 start = now->tv_sec - ((home->zombie_period + 3) / 4);
3629 if (home->last_packet_recv >= start) {
3630 DEBUG("Received reply from home server %d seconds ago. Might not be zombie.",
3631 (int) (now->tv_sec - home->last_packet_recv));
3635 home->state = HOME_STATE_ZOMBIE;
3636 home_trigger(home, "home_server.zombie");
3639 * Set the home server to "zombie", as of the time
3642 home->zombie_period_start.tv_sec = start;
3643 home->zombie_period_start.tv_usec = USEC / 2;
3645 fr_event_delete(el, &home->ev);
3647 home->num_sent_pings = 0;
3648 home->num_received_pings = 0;
3650 PROXY( "Marking home server %s port %d as zombie (it has not responded in %d.%06d seconds).",
3651 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
3652 buffer, sizeof(buffer)),
3653 home->port, (int) response_window->tv_sec, (int) response_window->tv_usec);
3655 ping_home_server(home);
3659 void revive_home_server(void *ctx)
3661 home_server_t *home = talloc_get_type_abort(ctx, home_server_t);
3664 home->state = HOME_STATE_ALIVE;
3665 home->response_timeouts = 0;
3666 home_trigger(home, "home_server.alive");
3667 home->currently_outstanding = 0;
3668 gettimeofday(&home->revive_time, NULL);
3671 * Delete any outstanding events.
3674 if (home->ev) fr_event_delete(el, &home->ev);
3676 PROXY( "Marking home server %s port %d alive again... we have no idea if it really is alive or not.",
3677 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
3678 buffer, sizeof(buffer)),
3682 void mark_home_server_dead(home_server_t *home, struct timeval *when)
3684 int previous_state = home->state;
3687 PROXY( "Marking home server %s port %d as dead.",
3688 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
3689 buffer, sizeof(buffer)),
3692 home->state = HOME_STATE_IS_DEAD;
3693 home_trigger(home, "home_server.dead");
3695 if (home->ping_check != HOME_PING_CHECK_NONE) {
3697 * If the control socket marks us dead, start
3698 * pinging. Otherwise, we already started
3699 * pinging when it was marked "zombie".
3701 if (previous_state == HOME_STATE_ALIVE) {
3702 ping_home_server(home);
3704 DEBUG("PING: Already pinging home server %s", home->log_name);
3709 * Revive it after a fixed period of time. This
3710 * is very, very, bad.
3713 home->when.tv_sec += home->revive_interval;
3715 DEBUG("PING: Reviving home server %s in %u seconds", home->log_name, home->revive_interval);
3717 INSERT_EVENT(revive_home_server, home);
3721 /** Wait for a reply after proxying a request.
3723 * Retransmit the proxied packet, or time out and go to
3724 * proxy_no_reply. Mark the home server unresponsive, etc.
3726 * If we do receive a reply, we transition to proxy_running.
3729 * digraph proxy_wait_for_reply {
3730 * proxy_wait_for_reply;
3732 * proxy_wait_for_reply -> retransmit_proxied_request [ label = "DUP", arrowhead = "none" ];
3733 * proxy_wait_for_reply -> proxy_no_reply [ label = "TIMER >= response_window" ];
3734 * proxy_wait_for_reply -> timer [ label = "TIMER < max_request_time" ];
3735 * proxy_wait_for_reply -> proxy_running [ label = "PROXY_REPLY" arrowhead = "none"];
3736 * proxy_wait_for_reply -> done [ label = "TIMER >= max_request_time" ];
3740 static void proxy_wait_for_reply(REQUEST *request, int action)
3742 struct timeval now, when;
3743 struct timeval *response_window = NULL;
3744 home_server_t *home = request->home_server;
3747 VERIFY_REQUEST(request);
3749 TRACE_STATE_MACHINE;
3752 rad_assert(request->packet->code != PW_CODE_STATUS_SERVER);
3753 rad_assert(request->home_server != NULL);
3755 gettimeofday(&now, NULL);
3760 * We have a reply, ignore the retransmit.
3762 if (request->proxy_reply) return;
3765 * The request was proxied to a virtual server.
3766 * Ignore the retransmit.
3768 if (request->home_server->server) return;
3771 * Use a new connection when the home server is
3772 * dead, or when there's no proxy listener, or
3773 * when the listener is failed or dead.
3775 * If the listener is known or frozen, use it for
3778 if ((home->state == HOME_STATE_IS_DEAD) ||
3779 !request->proxy_listener ||
3780 (request->proxy_listener->status >= RAD_LISTEN_STATUS_EOL)) {
3781 request_proxy_anew(request);
3787 * The home server is still alive, but TCP. We
3788 * rely on TCP to get the request and reply back.
3789 * So there's no need to retransmit.
3791 if (home->proto == IPPROTO_TCP) {
3792 DEBUG2("Suppressing duplicate proxied request (tcp) to home server %s port %d proto TCP - ID: %d",
3793 inet_ntop(request->proxy->dst_ipaddr.af,
3794 &request->proxy->dst_ipaddr.ipaddr,
3795 buffer, sizeof(buffer)),
3796 request->proxy->dst_port,
3797 request->proxy->id);
3803 * More than one retransmit a second is stupid,
3804 * and should be suppressed by the proxy.
3806 when = request->proxy->timestamp;
3809 if (timercmp(&now, &when, <)) {
3810 DEBUG2("Suppressing duplicate proxied request (too fast) to home server %s port %d proto TCP - ID: %d",
3811 inet_ntop(request->proxy->dst_ipaddr.af,
3812 &request->proxy->dst_ipaddr.ipaddr,
3813 buffer, sizeof(buffer)),
3814 request->proxy->dst_port,
3815 request->proxy->id);
3819 #ifdef WITH_ACCOUNTING
3821 * If we update the Acct-Delay-Time, we need to
3824 if ((request->packet->code == PW_CODE_ACCOUNTING_REQUEST) &&
3825 fr_pair_find_by_num(request->proxy->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY)) {
3826 request_proxy_anew(request);
3831 RDEBUG2("Sending duplicate proxied request to home server %s port %d - ID: %d",
3832 inet_ntop(request->proxy->dst_ipaddr.af,
3833 &request->proxy->dst_ipaddr.ipaddr,
3834 buffer, sizeof(buffer)),
3835 request->proxy->dst_port,
3836 request->proxy->id);
3837 request->num_proxied_requests++;
3839 rad_assert(request->proxy_listener != NULL);
3840 FR_STATS_TYPE_INC(home->stats.total_requests);
3841 home->last_packet_sent = now.tv_sec;
3842 request->proxy->timestamp = now;
3843 debug_packet(request, request->proxy, false);
3844 request->proxy_listener->send(request->proxy_listener, request);
3847 case FR_ACTION_TIMER:
3848 response_window = request_response_window(request);
3851 if (!request->proxy_listener ||
3852 (request->proxy_listener->status >= RAD_LISTEN_STATUS_EOL)) {
3853 remove_from_proxy_hash(request);
3855 when = request->packet->timestamp;
3856 when.tv_sec += request->root->max_request_time;
3858 if (timercmp(&when, &now, >)) {
3859 RDEBUG("Waiting for client retransmission in order to do a proxy retransmit");
3860 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3867 * Wake up "response_window" time in the future.
3868 * i.e. when MY packet hasn't received a response.
3870 * Note that we DO NOT mark the home server as
3871 * zombie if it doesn't respond to us. It may be
3872 * responding to other (better looking) packets.
3874 when = request->proxy->timestamp;
3875 timeradd(&when, response_window, &when);
3878 * Not at the response window. Set the timer for
3881 if (timercmp(&when, &now, >)) {
3882 struct timeval diff;
3883 timersub(&when, &now, &diff);
3885 RDEBUG("Expecting proxy response no later than %d.%06d seconds from now",
3886 (int) diff.tv_sec, (int) diff.tv_usec);
3887 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3892 RDEBUG("No proxy response, giving up on request and marking it done");
3895 * If we haven't received any packets for
3896 * "response_window", then mark the home server
3899 * This check should really be part of a home
3900 * server state machine.
3902 if (((home->state == HOME_STATE_ALIVE) ||
3903 (home->state == HOME_STATE_UNKNOWN))
3905 home->response_timeouts++;
3906 if (home->response_timeouts >= home->max_response_timeouts)
3907 mark_home_server_zombie(home, &now, response_window);
3910 FR_STATS_TYPE_INC(home->stats.total_timeouts);
3911 if (home->type == HOME_TYPE_AUTH) {
3912 if (request->proxy_listener) FR_STATS_TYPE_INC(request->proxy_listener->stats.total_timeouts);
3913 FR_STATS_TYPE_INC(proxy_auth_stats.total_timeouts);
3916 else if (home->type == HOME_TYPE_ACCT) {
3917 if (request->proxy_listener) FR_STATS_TYPE_INC(request->proxy_listener->stats.total_timeouts);
3918 FR_STATS_TYPE_INC(proxy_acct_stats.total_timeouts);
3922 else if (home->type == HOME_TYPE_COA) {
3923 if (request->proxy_listener) FR_STATS_TYPE_INC(request->proxy_listener->stats.total_timeouts);
3925 if (request->packet->code == PW_CODE_COA_REQUEST) {
3926 FR_STATS_TYPE_INC(proxy_coa_stats.total_timeouts);
3928 FR_STATS_TYPE_INC(proxy_dsc_stats.total_timeouts);
3934 * There was no response within the window. Stop
3935 * the request. If the client retransmitted, it
3936 * may have failed over to another home server.
3937 * But that one may be dead, too.
3939 * The extra verbose message if we have a username,
3940 * is extremely useful if the proxy is part of a chain
3941 * and the final home server, is not the one we're
3944 if (request->username) {
3945 RERROR("Failing proxied request for user \"%s\", due to lack of any response from home "
3946 "server %s port %d",
3947 request->username->vp_strvalue,
3948 inet_ntop(request->proxy->dst_ipaddr.af,
3949 &request->proxy->dst_ipaddr.ipaddr,
3950 buffer, sizeof(buffer)),
3951 request->proxy->dst_port);
3953 RERROR("Failing proxied request, due to lack of any response from home server %s port %d",
3954 inet_ntop(request->proxy->dst_ipaddr.af,
3955 &request->proxy->dst_ipaddr.ipaddr,
3956 buffer, sizeof(buffer)),
3957 request->proxy->dst_port);
3960 if (setup_post_proxy_fail(request)) {
3961 request_queue_or_run(request, proxy_no_reply);
3963 gettimeofday(&request->reply->timestamp, NULL);
3964 request_cleanup_delay_init(request);
3969 * We received a new reply. Go process it.
3971 case FR_ACTION_PROXY_REPLY:
3972 request_queue_or_run(request, proxy_running);
3976 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
3980 #endif /* WITH_PROXY */
3983 /***********************************************************************
3987 ***********************************************************************/
3989 static int null_handler(UNUSED REQUEST *request)
3995 * See if we need to originate a CoA request.
3997 static void request_coa_originate(REQUEST *request)
3999 int rcode, pre_proxy_type = 0;
4005 VERIFY_REQUEST(request);
4007 rad_assert(request->coa != NULL);
4008 rad_assert(request->proxy == NULL);
4009 rad_assert(!request->in_proxy_hash);
4010 rad_assert(request->proxy_reply == NULL);
4013 * Check whether we want to originate one, or cancel one.
4015 vp = fr_pair_find_by_num(request->config, PW_SEND_COA_REQUEST, 0, TAG_ANY);
4017 vp = fr_pair_find_by_num(request->coa->proxy->vps, PW_SEND_COA_REQUEST, 0, TAG_ANY);
4021 if (vp->vp_integer == 0) {
4023 TALLOC_FREE(request->coa);
4031 * src_ipaddr will be set up in proxy_encode.
4033 memset(&ipaddr, 0, sizeof(ipaddr));
4034 vp = fr_pair_find_by_num(coa->proxy->vps, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY);
4036 ipaddr.af = AF_INET;
4037 ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
4039 } else if ((vp = fr_pair_find_by_num(coa->proxy->vps, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY)) != NULL) {
4040 ipaddr.af = AF_INET6;
4041 ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
4042 ipaddr.prefix = 128;
4043 } else if ((vp = fr_pair_find_by_num(coa->proxy->vps, PW_HOME_SERVER_POOL, 0, TAG_ANY)) != NULL) {
4044 coa->home_pool = home_pool_byname(vp->vp_strvalue,
4046 if (!coa->home_pool) {
4047 RWDEBUG2("No such home_server_pool %s",
4053 * Prefer the pool to one server
4055 } else if (request->client->coa_pool) {
4056 coa->home_pool = request->client->coa_pool;
4058 } else if (request->client->coa_server) {
4059 coa->home_server = request->client->coa_server;
4063 * If all else fails, send it to the client that
4064 * originated this request.
4066 memcpy(&ipaddr, &request->packet->src_ipaddr, sizeof(ipaddr));
4070 * Use the pool, if it exists.
4072 if (coa->home_pool) {
4073 coa->home_server = home_server_ldb(NULL, coa->home_pool, coa);
4074 if (!coa->home_server) {
4075 RWDEBUG("No live home server for home_server_pool %s", coa->home_pool->name);
4078 home_server_update_request(coa->home_server, coa);
4080 } else if (!coa->home_server) {
4081 uint16_t port = PW_COA_UDP_PORT;
4083 vp = fr_pair_find_by_num(coa->proxy->vps, PW_PACKET_DST_PORT, 0, TAG_ANY);
4084 if (vp) port = vp->vp_integer;
4086 coa->home_server = home_server_find(&ipaddr, port, IPPROTO_UDP);
4087 if (!coa->home_server) {
4088 RWDEBUG2("Unknown destination %s:%d for CoA request.",
4089 inet_ntop(ipaddr.af, &ipaddr.ipaddr,
4090 buffer, sizeof(buffer)), port);
4095 vp = fr_pair_find_by_num(coa->proxy->vps, PW_PACKET_TYPE, 0, TAG_ANY);
4097 switch (vp->vp_integer) {
4098 case PW_CODE_COA_REQUEST:
4099 case PW_CODE_DISCONNECT_REQUEST:
4100 coa->proxy->code = vp->vp_integer;
4104 DEBUG("Cannot set CoA Packet-Type to code %d",
4110 if (!coa->proxy->code) coa->proxy->code = PW_CODE_COA_REQUEST;
4113 * The rest of the server code assumes that
4114 * request->packet && request->reply exist. Copy them
4115 * from the original request.
4117 rad_assert(coa->packet != NULL);
4118 rad_assert(coa->packet->vps == NULL);
4120 coa->packet = rad_copy_packet(coa, request->packet);
4121 coa->reply = rad_copy_packet(coa, request->reply);
4123 coa->config = fr_pair_list_copy(coa, request->config);
4124 coa->num_coa_requests = 0;
4125 coa->handle = null_handler;
4126 coa->number = request->number; /* it's associated with the same request */
4129 * Call the pre-proxy routines.
4131 vp = fr_pair_find_by_num(request->config, PW_PRE_PROXY_TYPE, 0, TAG_ANY);
4133 DICT_VALUE const *dval = dict_valbyattr(vp->da->attr, vp->da->vendor, vp->vp_integer);
4134 /* Must be a validation issue */
4136 RDEBUG2("Found Pre-Proxy-Type %s", dval->name);
4137 pre_proxy_type = vp->vp_integer;
4140 if (coa->home_pool && coa->home_pool->virtual_server) {
4141 char const *old_server = coa->server;
4143 coa->server = coa->home_pool->virtual_server;
4144 RDEBUG2("server %s {", coa->server);
4146 rcode = process_pre_proxy(pre_proxy_type, coa);
4149 coa->server = old_server;
4151 rcode = process_pre_proxy(pre_proxy_type, coa);
4158 * Only send the CoA packet if the pre-proxy code succeeded.
4160 case RLM_MODULE_NOOP:
4162 case RLM_MODULE_UPDATED:
4167 * Source IP / port is set when the proxy socket
4170 coa->proxy->dst_ipaddr = coa->home_server->ipaddr;
4171 coa->proxy->dst_port = coa->home_server->port;
4173 if (!insert_into_proxy_hash(coa)) {
4174 radlog_request(L_PROXY, 0, coa, "Failed to insert CoA request into proxy list");
4179 * We CANNOT divorce the CoA request from the parent
4180 * request. This function is running in a child thread,
4181 * and we need access to the main event loop in order to
4182 * to add the timers for the CoA packet.
4184 * Instead, we wait for the timer on the parent request
4187 gettimeofday(&coa->proxy->timestamp, NULL);
4188 coa->packet->timestamp = coa->proxy->timestamp; /* for max_request_time */
4189 coa->home_server->last_packet_sent = coa->proxy->timestamp.tv_sec;
4190 coa->delay = 0; /* need to calculate a new delay */
4193 * If requested, put a State attribute into the packet,
4194 * and cache the VPS.
4196 fr_state_put_vps(coa, NULL, coa->packet);
4199 * Encode the packet before we do anything else.
4201 coa->proxy_listener->encode(coa->proxy_listener, coa);
4202 debug_packet(coa, coa->proxy, false);
4204 #ifdef DEBUG_STATE_MACHINE
4205 if (rad_debug_lvl) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n", request->number, __FUNCTION__,
4206 child_state_names[request->child_state],
4207 child_state_names[REQUEST_PROXIED]);
4211 * Set the state function, then the state, no child, and
4214 coa->process = coa_wait_for_reply;
4215 coa->child_state = REQUEST_PROXIED;
4217 #ifdef HAVE_PTHREAD_H
4218 coa->child_pid = NO_SUCH_CHILD_PID;
4221 if (we_are_master()) coa_separate(request->coa);
4224 * And send the packet.
4226 coa->proxy_listener->send(coa->proxy_listener, coa);
4230 static void coa_retransmit(REQUEST *request)
4232 uint32_t delay, frac;
4233 struct timeval now, when, mrd;
4236 VERIFY_REQUEST(request);
4238 fr_event_now(el, &now);
4240 if (request->delay == 0) {
4242 * Implement re-transmit algorithm as per RFC 5080
4245 * We want IRT + RAND*IRT
4246 * or 0.9 IRT + rand(0,.2) IRT
4248 * 2^20 ~ USEC, and we want 2.
4249 * rand(0,0.2) USEC ~ (rand(0,2^21) / 10)
4251 delay = (fr_rand() & ((1 << 22) - 1)) / 10;
4252 request->delay = delay * request->home_server->coa_irt;
4253 delay = request->home_server->coa_irt * USEC;
4254 delay -= delay / 10;
4255 delay += request->delay;
4256 request->delay = delay;
4258 when = request->proxy->timestamp;
4259 tv_add(&when, delay);
4261 if (timercmp(&when, &now, >)) {
4262 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
4268 * Retransmit CoA request.
4272 * Cap count at MRC, if it is non-zero.
4274 if (request->home_server->coa_mrc &&
4275 (request->num_coa_requests >= request->home_server->coa_mrc)) {
4276 RERROR("Failing request - originate-coa ID %u, due to lack of any response from coa server %s port %d",
4278 inet_ntop(request->proxy->dst_ipaddr.af,
4279 &request->proxy->dst_ipaddr.ipaddr,
4280 buffer, sizeof(buffer)),
4281 request->proxy->dst_port);
4283 if (setup_post_proxy_fail(request)) {
4284 request_queue_or_run(request, coa_no_reply);
4286 request_done(request, FR_ACTION_DONE);
4292 * RFC 5080 Section 2.2.1
4294 * RT = 2*RTprev + RAND*RTprev
4295 * = 1.9 * RTprev + rand(0,.2) * RTprev
4296 * = 1.9 * RTprev + rand(0,1) * (RTprev / 5)
4299 delay ^= (delay >> 16);
4301 frac = request->delay / 5;
4302 delay = ((frac >> 16) * delay) + (((frac & 0xffff) * delay) >> 16);
4304 delay += (2 * request->delay) - (request->delay / 10);
4307 * Cap delay at MRT, if MRT is non-zero.
4309 if (request->home_server->coa_mrt &&
4310 (delay > (request->home_server->coa_mrt * USEC))) {
4311 int mrt_usec = request->home_server->coa_mrt * USEC;
4314 * delay = MRT + RAND * MRT
4315 * = 0.9 MRT + rand(0,.2) * MRT
4318 delay ^= (delay >> 15);
4320 delay = ((mrt_usec >> 16) * delay) + (((mrt_usec & 0xffff) * delay) >> 16);
4321 delay += mrt_usec - (mrt_usec / 10);
4324 request->delay = delay;
4326 tv_add(&when, request->delay);
4327 mrd = request->proxy->timestamp;
4328 mrd.tv_sec += request->home_server->coa_mrd;
4331 * Cap duration at MRD.
4333 if (timercmp(&mrd, &when, <)) {
4336 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
4338 request->num_coa_requests++; /* is NOT reset by code 3 lines above! */
4340 FR_STATS_TYPE_INC(request->home_server->stats.total_requests);
4342 RDEBUG2("Sending duplicate CoA request to home server %s port %d - ID: %d",
4343 inet_ntop(request->proxy->dst_ipaddr.af,
4344 &request->proxy->dst_ipaddr.ipaddr,
4345 buffer, sizeof(buffer)),
4346 request->proxy->dst_port,
4347 request->proxy->id);
4349 request->proxy_listener->send(request->proxy_listener,
4354 /** Wait for a reply after originating a CoA a request.
4356 * Retransmit the proxied packet, or time out and go to
4357 * coa_no_reply. Mark the home server unresponsive, etc.
4359 * If we do receive a reply, we transition to coa_running.
4362 * digraph coa_wait_for_reply {
4363 * coa_wait_for_reply;
4365 * coa_wait_for_reply -> coa_no_reply [ label = "TIMER >= response_window" ];
4366 * coa_wait_for_reply -> timer [ label = "TIMER < max_request_time" ];
4367 * coa_wait_for_reply -> coa_running [ label = "PROXY_REPLY" arrowhead = "none"];
4368 * coa_wait_for_reply -> done [ label = "TIMER >= max_request_time" ];
4372 static void coa_wait_for_reply(REQUEST *request, int action)
4374 VERIFY_REQUEST(request);
4376 TRACE_STATE_MACHINE;
4380 if (request->parent) coa_separate(request);
4383 case FR_ACTION_TIMER:
4384 if (request_max_time(request)) break;
4387 * Don't do fail-over. This is a 3.1 feature.
4389 if (!request->home_server ||
4390 (request->home_server->state == HOME_STATE_IS_DEAD) ||
4391 !request->proxy_listener ||
4392 (request->proxy_listener->status >= RAD_LISTEN_STATUS_EOL)) {
4393 request_done(request, FR_ACTION_DONE);
4397 coa_retransmit(request);
4400 case FR_ACTION_PROXY_REPLY:
4401 request_queue_or_run(request, coa_running);
4405 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
4410 static void coa_separate(REQUEST *request)
4412 VERIFY_REQUEST(request);
4413 #ifdef DEBUG_STATE_MACHINE
4414 int action = FR_ACTION_TIMER;
4417 TRACE_STATE_MACHINE;
4420 rad_assert(request->parent != NULL);
4421 rad_assert(request->parent->coa == request);
4422 rad_assert(request->ev == NULL);
4423 rad_assert(!request->in_request_hash);
4424 rad_assert(request->coa == NULL);
4426 rad_assert(request->proxy_reply || request->proxy_listener);
4428 (void) talloc_steal(NULL, request);
4429 request->parent->coa = NULL;
4430 request->parent = NULL;
4432 if (we_are_master()) {
4434 coa_retransmit(request);
4439 /** Process a request after the CoA has timed out.
4441 * Run the packet through Post-Proxy-Type Fail
4444 * digraph coa_no_reply {
4447 * coa_no_reply -> dup [ label = "DUP", arrowhead = "none" ];
4448 * coa_no_reply -> timer [ label = "TIMER < max_request_time" ];
4449 * coa_no_reply -> coa_reply_too_late [ label = "PROXY_REPLY" arrowhead = "none"];
4450 * coa_no_reply -> process_proxy_reply [ label = "RUN" ];
4451 * coa_no_reply -> done [ label = "TIMER >= timeout" ];
4455 static void coa_no_reply(REQUEST *request, int action)
4459 VERIFY_REQUEST(request);
4461 TRACE_STATE_MACHINE;
4465 case FR_ACTION_TIMER:
4466 (void) request_max_time(request);
4469 case FR_ACTION_PROXY_REPLY: /* too late! */
4470 RDEBUG2("Reply from CoA server %s port %d - ID: %d arrived too late.",
4471 inet_ntop(request->proxy->src_ipaddr.af,
4472 &request->proxy->src_ipaddr.ipaddr,
4473 buffer, sizeof(buffer)),
4474 request->proxy->dst_port, request->proxy->id);
4478 if (process_proxy_reply(request, NULL)) {
4479 request->handle(request);
4481 request_done(request, FR_ACTION_DONE);
4485 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
4491 /** Process the request after receiving a coa reply.
4493 * Throught the post-proxy section, and the through the handler
4497 * digraph coa_running {
4500 * coa_running -> timer [ label = "TIMER < max_request_time" ];
4501 * coa_running -> process_proxy_reply [ label = "RUN" ];
4502 * coa_running -> done [ label = "TIMER >= timeout" ];
4506 static void coa_running(REQUEST *request, int action)
4508 VERIFY_REQUEST(request);
4510 TRACE_STATE_MACHINE;
4514 case FR_ACTION_TIMER:
4515 (void) request_max_time(request);
4519 if (process_proxy_reply(request, request->proxy_reply)) {
4520 request->handle(request);
4522 request_done(request, FR_ACTION_DONE);
4526 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
4530 #endif /* WITH_COA */
4532 /***********************************************************************
4534 * End of the State machine. Start of additional helper code.
4536 ***********************************************************************/
4538 /***********************************************************************
4542 ***********************************************************************/
4543 static void event_socket_handler(fr_event_list_t *xel, UNUSED int fd, void *ctx)
4545 rad_listen_t *listener = talloc_get_type_abort(ctx, rad_listen_t);
4547 rad_assert(xel == el);
4549 if ((listener->fd < 0)
4551 #ifndef WITH_DETAIL_THREAD
4552 && (listener->type != RAD_LISTEN_DETAIL)
4558 listener->print(listener, buffer, sizeof(buffer));
4559 ERROR("FATAL: Asked to read from closed socket: %s",
4562 rad_panic("Socket was closed on us!");
4566 listener->recv(listener);
4570 #ifdef WITH_DETAIL_THREAD
4573 * This function is called periodically to see if this detail
4574 * file is available for reading.
4576 static void event_poll_detail(void *ctx)
4579 rad_listen_t *this = talloc_get_type_abort(ctx, rad_listen_t);
4580 struct timeval when, now;
4581 listen_detail_t *detail = this->data;
4583 rad_assert(this->type == RAD_LISTEN_DETAIL);
4586 event_socket_handler(el, this->fd, this);
4588 fr_event_now(el, &now);
4592 * Backdoor API to get the delay until the next poll
4595 delay = this->encode(this, NULL);
4596 if (delay == 0) goto redo;
4598 tv_add(&when, delay);
4601 if (!fr_event_insert(el, event_poll_detail, this,
4602 &when, &detail->ev)) {
4603 ERROR("Failed creating handler");
4607 #endif /* WITH_DETAIL_THREAD */
4608 #endif /* WITH_DETAIL */
4610 static void event_status(struct timeval *wake)
4612 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
4616 if (rad_debug_lvl == 0) {
4618 INFO("Ready to process requests");
4619 just_started = false;
4625 INFO("Ready to process requests");
4627 } else if ((wake->tv_sec != 0) ||
4628 (wake->tv_usec >= 100000)) {
4629 DEBUG("Waking up in %d.%01u seconds.",
4630 (int) wake->tv_sec, (unsigned int) wake->tv_usec / 100000);
4635 * FIXME: Put this somewhere else, where it isn't called
4636 * all of the time...
4639 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
4641 * If there are no child threads, then there may
4642 * be child processes. In that case, wait for
4643 * their exit status, and throw that exit status
4644 * away. This helps get rid of zxombie children.
4646 while (waitpid(-1, &argval, WNOHANG) > 0) {
4654 static void listener_free_cb(void *ctx)
4656 rad_listen_t *this = talloc_get_type_abort(ctx, rad_listen_t);
4659 if (this->count > 0) {
4660 struct timeval when;
4661 listen_socket_t *sock = this->data;
4663 fr_event_now(el, &when);
4667 if (!fr_event_insert(el, listener_free_cb, this, &when,
4669 rad_panic("Failed to insert event");
4676 * It's all free, close the socket.
4679 this->print(this, buffer, sizeof(buffer));
4680 DEBUG("... cleaning up socket %s", buffer);
4681 rad_assert(this->next == NULL);
4687 static int proxy_eol_cb(void *ctx, void *data)
4689 struct timeval when;
4690 REQUEST *request = fr_packet2myptr(REQUEST, proxy, data);
4692 if (request->proxy_listener != ctx) return 0;
4695 * We don't care if it's being processed in a child thread.
4698 #ifdef WITH_ACCOUNTING
4700 * Accounting packets should be deleted immediately.
4701 * They will never be retransmitted by the client.
4703 if (request->proxy->code == PW_CODE_ACCOUNTING_REQUEST) {
4704 RDEBUG("Stopping request due to failed connection to home server");
4705 request->master_state = REQUEST_STOP_PROCESSING;
4710 * Reset the timer to be now, so that the request is
4711 * quickly updated. But spread the requests randomly
4712 * over the next second, so that we don't overload the
4715 fr_event_now(el, &when);
4716 tv_add(&when, fr_rand() % USEC);
4717 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
4720 * Don't delete it from the list.
4726 static int event_new_fd(rad_listen_t *this)
4732 if (this->status == RAD_LISTEN_STATUS_KNOWN) return 1;
4734 this->print(this, buffer, sizeof(buffer));
4736 if (this->status == RAD_LISTEN_STATUS_INIT) {
4737 listen_socket_t *sock = this->data;
4739 rad_assert(sock != NULL);
4741 DEBUG("Listening on %s", buffer);
4743 INFO(" ... adding new socket %s", buffer);
4747 if (!just_started && (this->type == RAD_LISTEN_PROXY)) {
4748 home_server_t *home;
4751 if (!home || !home->limit.max_connections) {
4752 INFO(" ... adding new socket %s", buffer);
4754 INFO(" ... adding new socket %s (%u of %u)", buffer,
4755 home->limit.num_connections, home->limit.max_connections);
4761 switch (this->type) {
4764 * Detail files are always known, and aren't
4765 * put into the socket event loop.
4767 case RAD_LISTEN_DETAIL:
4768 this->status = RAD_LISTEN_STATUS_KNOWN;
4770 #ifndef WITH_DETAIL_THREAD
4772 * Set up the first poll interval.
4774 event_poll_detail(this);
4777 break; /* add the FD to the list */
4779 #endif /* WITH_DETAIL */
4783 * Add it to the list of sockets we can use.
4784 * Server sockets (i.e. auth/acct) are never
4785 * added to the packet list.
4787 case RAD_LISTEN_PROXY:
4789 rad_assert((sock->proto == IPPROTO_UDP) || (sock->home != NULL));
4792 * Add timers to outgoing child sockets, if necessary.
4794 if (sock->proto == IPPROTO_TCP && sock->opened &&
4795 (sock->home->limit.lifetime || sock->home->limit.idle_timeout)) {
4796 struct timeval when;
4798 when.tv_sec = sock->opened + 1;
4802 if (!fr_event_insert(el, tcp_socket_timer, this, &when,
4804 rad_panic("Failed to insert event");
4809 #endif /* WITH_PROXY */
4812 * FIXME: put idle timers on command sockets.
4818 * Add timers to incoming child sockets, if necessary.
4820 if (sock->proto == IPPROTO_TCP && sock->opened &&
4821 (sock->limit.lifetime || sock->limit.idle_timeout)) {
4822 struct timeval when;
4824 when.tv_sec = sock->opened + 1;
4828 if (!fr_event_insert(el, tcp_socket_timer, this, &when,
4830 ERROR("Failed adding timer for socket: %s", fr_strerror());
4836 } /* switch over listener types */
4839 * All sockets: add the FD to the event handler.
4841 if (!fr_event_fd_insert(el, 0, this->fd,
4842 event_socket_handler, this)) {
4843 ERROR("Failed adding event handler for socket: %s", fr_strerror());
4847 this->status = RAD_LISTEN_STATUS_KNOWN;
4853 * The socket has reached a timeout. Try to close it.
4855 if (this->status == RAD_LISTEN_STATUS_FROZEN) {
4857 * Requests are still using the socket. Wait for
4860 if (this->count > 0) {
4861 struct timeval when;
4862 listen_socket_t *sock = this->data;
4865 * Try again to clean up the socket in 30
4868 gettimeofday(&when, NULL);
4872 if (!fr_event_insert(el,
4873 (fr_event_callback_t) event_new_fd,
4874 this, &when, &sock->ev)) {
4875 rad_panic("Failed to insert event");
4881 fr_event_fd_delete(el, 0, this->fd);
4882 this->status = RAD_LISTEN_STATUS_REMOVE_NOW;
4886 * The socket has had a catastrophic error. Close it.
4888 if (this->status == RAD_LISTEN_STATUS_EOL) {
4890 * Remove it from the list of live FD's.
4892 fr_event_fd_delete(el, 0, this->fd);
4896 * Tell all requests using this socket that the socket is dead.
4898 if (this->type == RAD_LISTEN_PROXY) {
4899 PTHREAD_MUTEX_LOCK(&proxy_mutex);
4900 if (!fr_packet_list_socket_freeze(proxy_list,
4902 ERROR("Fatal error freezing socket: %s", fr_strerror());
4906 if (this->count > 0) {
4907 fr_packet_list_walk(proxy_list, this, proxy_eol_cb);
4909 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
4914 * Requests are still using the socket. Wait for
4917 if (this->count > 0) {
4918 struct timeval when;
4919 listen_socket_t *sock = this->data;
4922 * Try again to clean up the socket in 30
4925 gettimeofday(&when, NULL);
4929 if (!fr_event_insert(el,
4930 (fr_event_callback_t) event_new_fd,
4931 this, &when, &sock->ev)) {
4932 rad_panic("Failed to insert event");
4939 * No one is using the socket. We can remove it now.
4941 this->status = RAD_LISTEN_STATUS_REMOVE_NOW;
4942 } /* socket is at EOL */
4948 if (this->status == RAD_LISTEN_STATUS_REMOVE_NOW) {
4951 listen_socket_t *sock = this->data;
4953 struct timeval when;
4956 * Re-open the socket, pointing it to /dev/null.
4957 * This means that all writes proceed without
4958 * blocking, and all reads return "no data".
4960 * This leaves the socket active, so any child
4961 * threads won't go insane. But it means that
4962 * they cannot send or receive any packets.
4964 * This is EXTRA work in the normal case, when
4965 * sockets are closed without error. But it lets
4966 * us have one simple processing method for all
4969 devnull = open("/dev/null", O_RDWR);
4971 ERROR("FATAL failure opening /dev/null: %s",
4972 fr_syserror(errno));
4975 if (dup2(devnull, this->fd) < 0) {
4976 ERROR("FATAL failure closing socket: %s",
4977 fr_syserror(errno));
4983 rad_assert(this->type != RAD_LISTEN_DETAIL);
4989 * The socket is dead. Force all proxied packets
4990 * to stop using it. And then remove it from the
4991 * list of outgoing sockets.
4993 if (this->type == RAD_LISTEN_PROXY) {
4994 home_server_t *home;
4997 if (!home || !home->limit.max_connections) {
4998 INFO(" ... shutting down socket %s", buffer);
5000 INFO(" ... shutting down socket %s (%u of %u)", buffer,
5001 home->limit.num_connections, home->limit.max_connections);
5004 PTHREAD_MUTEX_LOCK(&proxy_mutex);
5005 fr_packet_list_walk(proxy_list, this, eol_proxy_listener);
5007 if (!fr_packet_list_socket_del(proxy_list, this->fd)) {
5008 ERROR("Fatal error removing socket %s: %s",
5009 buffer, fr_strerror());
5012 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
5016 INFO(" ... shutting down socket %s", buffer);
5019 * EOL all requests using this socket.
5021 rbtree_walk(pl, RBTREE_DELETE_ORDER, eol_listener, this);
5025 * No child threads, clean it up now.
5029 if (sock->ev) fr_event_delete(el, &sock->ev);
5035 * Wait until all requests using this socket are done.
5037 gettimeofday(&when, NULL);
5041 if (!fr_event_insert(el, listener_free_cb, this, &when,
5043 rad_panic("Failed to insert event");
5046 #endif /* WITH_TCP */
5051 /***********************************************************************
5055 ***********************************************************************/
5057 static void handle_signal_self(int flag)
5061 if ((flag & (RADIUS_SIGNAL_SELF_EXIT | RADIUS_SIGNAL_SELF_TERM)) != 0) {
5062 if ((flag & RADIUS_SIGNAL_SELF_EXIT) != 0) {
5063 INFO("Signalled to exit");
5064 fr_event_loop_exit(el, 1);
5066 INFO("Signalled to terminate");
5067 fr_event_loop_exit(el, 2);
5071 } /* else exit/term flags weren't set */
5074 * Tell the even loop to stop processing.
5076 if ((flag & RADIUS_SIGNAL_SELF_HUP) != 0) {
5078 static time_t last_hup = 0;
5081 if ((int) (when - last_hup) < 5) {
5082 INFO("Ignoring HUP (less than 5s since last one)");
5086 INFO("Received HUP signal");
5090 exec_trigger(NULL, NULL, "server.signal.hup", true);
5091 fr_event_loop_exit(el, 0x80);
5094 #if defined(WITH_DETAIL) && !defined(WITH_DETAIL_THREAD)
5095 if ((flag & RADIUS_SIGNAL_SELF_DETAIL) != 0) {
5099 * FIXME: O(N) loops suck.
5101 for (this = main_config.listen;
5103 this = this->next) {
5104 if (this->type != RAD_LISTEN_DETAIL) continue;
5107 * This one didn't send the signal, skip
5110 if (!this->decode(this, NULL)) continue;
5113 * Go service the interrupt.
5115 event_poll_detail(this);
5120 #if defined(WITH_TCP) && defined(WITH_PROXY) && defined(HAVE_PTHREAD_H)
5122 * There are new listeners in the list. Run
5123 * event_new_fd() on them.
5125 if ((flag & RADIUS_SIGNAL_SELF_NEW_FD) != 0) {
5126 rad_listen_t *this, *next;
5128 FD_MUTEX_LOCK(&fd_mutex);
5131 * FIXME: unlock the mutex before calling
5134 for (this = new_listeners; this != NULL; this = next) {
5141 new_listeners = NULL;
5142 FD_MUTEX_UNLOCK(&fd_mutex);
5147 #ifndef HAVE_PTHREAD_H
5148 void radius_signal_self(int flag)
5150 return handle_signal_self(flag);
5154 static int self_pipe[2] = { -1, -1 };
5157 * Inform ourselves that we received a signal.
5159 void radius_signal_self(int flag)
5165 * The read MUST be non-blocking for this to work.
5167 rcode = read(self_pipe[0], buffer, sizeof(buffer));
5171 for (i = 0; i < rcode; i++) {
5172 buffer[0] |= buffer[i];
5180 if (write(self_pipe[1], buffer, 1) < 0) fr_exit(0);
5184 static void event_signal_handler(UNUSED fr_event_list_t *xel,
5185 UNUSED int fd, UNUSED void *ctx)
5190 rcode = read(self_pipe[0], buffer, sizeof(buffer));
5191 if (rcode <= 0) return;
5194 * Merge pending signals.
5196 for (i = 0; i < rcode; i++) {
5197 buffer[0] |= buffer[i];
5200 handle_signal_self(buffer[0]);
5202 #endif /* HAVE_PTHREAD_H */
5204 /***********************************************************************
5206 * Bootstrapping code.
5208 ***********************************************************************/
5211 * Externally-visibly functions.
5213 int radius_event_init(TALLOC_CTX *ctx) {
5214 el = fr_event_list_create(ctx, event_status);
5220 static int packet_entry_cmp(void const *one, void const *two)
5222 RADIUS_PACKET const * const *a = one;
5223 RADIUS_PACKET const * const *b = two;
5225 return fr_packet_cmp(*a, *b);
5230 * They haven't defined a proxy listener. Automatically
5231 * add one for them, with the correct address family.
5233 static void create_default_proxy_listener(int af)
5237 listen_socket_t *sock;
5240 memset(&home, 0, sizeof(home));
5243 * Open a default UDP port
5245 home.proto = IPPROTO_UDP;
5249 * Set the address family.
5251 home.src_ipaddr.af = af;
5252 home.ipaddr.af = af;
5255 * Get the correct listener.
5257 this = proxy_new_listener(proxy_ctx, &home, port);
5263 if (!fr_packet_list_socket_add(proxy_list, this->fd,
5265 &sock->other_ipaddr, sock->other_port,
5267 ERROR("Failed adding proxy socket");
5272 * Insert the FD into list of FDs to listen on.
5274 radius_update_listener(this);
5278 * See if we automatically need to open a proxy socket.
5280 static void check_proxy(rad_listen_t *head)
5283 bool has_v4, has_v6;
5286 if (check_config) return;
5287 if (!main_config.proxy_requests) return;
5289 if (!home_servers_udp) return;
5292 * We passed "-i" on the command line. Use that address
5293 * family for the proxy socket.
5295 if (main_config.myip.af != AF_UNSPEC) {
5296 create_default_proxy_listener(main_config.myip.af);
5300 defined_proxy = has_v4 = has_v6 = false;
5303 * Figure out if we need to open a proxy socket, and if
5306 for (this = head; this != NULL; this = this->next) {
5307 listen_socket_t *sock;
5309 switch (this->type) {
5310 case RAD_LISTEN_PROXY:
5311 defined_proxy = true;
5314 case RAD_LISTEN_AUTH:
5316 case RAD_LISTEN_ACCT:
5319 case RAD_LISTEN_COA:
5322 if (sock->my_ipaddr.af == AF_INET) has_v4 = true;
5323 if (sock->my_ipaddr.af == AF_INET6) has_v6 = true;
5332 * Assume they know what they're doing.
5334 if (defined_proxy) return;
5336 if (has_v4) create_default_proxy_listener(AF_INET);
5338 if (has_v6) create_default_proxy_listener(AF_INET6);
5342 int radius_event_start(CONF_SECTION *cs, bool have_children)
5344 rad_listen_t *head = NULL;
5346 if (fr_start_time != (time_t)-1) return 0;
5348 time(&fr_start_time);
5350 if (!check_config) {
5352 * radius_event_init() must be called first
5356 pl = rbtree_create(NULL, packet_entry_cmp, NULL, 0);
5357 if (!pl) return 0; /* leak el */
5360 request_num_counter = 0;
5363 if (main_config.proxy_requests && !check_config) {
5365 * Create the tree for managing proxied requests and
5368 proxy_list = fr_packet_list_create(1);
5369 if (!proxy_list) return 0;
5371 #ifdef HAVE_PTHREAD_H
5372 if (pthread_mutex_init(&proxy_mutex, NULL) != 0) {
5373 ERROR("FATAL: Failed to initialize proxy mutex: %s",
5374 fr_syserror(errno));
5380 * The "init_delay" is set to "response_window".
5381 * Reset it to half of "response_window" in order
5382 * to give the event loop enough time to service
5383 * the event before hitting "response_window".
5385 main_config.init_delay.tv_usec += (main_config.init_delay.tv_sec & 0x01) * USEC;
5386 main_config.init_delay.tv_usec >>= 1;
5387 main_config.init_delay.tv_sec >>= 1;
5389 proxy_ctx = talloc_init("proxy");
5394 * Move all of the thread calls to this file?
5396 * It may be best for the mutexes to be in this file...
5398 spawn_flag = have_children;
5400 #ifdef HAVE_PTHREAD_H
5401 NO_SUCH_CHILD_PID = pthread_self(); /* not a child thread */
5404 * Initialize the threads ONLY if we're spawning, AND
5405 * we're running normally.
5407 if (have_children && !check_config &&
5408 (thread_pool_init(cs, &spawn_flag) < 0)) {
5414 DEBUG("%s: #### Skipping IP addresses and Ports ####",
5416 if (listen_init(cs, &head, spawn_flag) < 0) {
5423 #ifdef HAVE_PTHREAD_H
5425 * Child threads need a pipe to signal us, as do the
5428 if (pipe(self_pipe) < 0) {
5429 ERROR("Error opening internal pipe: %s", fr_syserror(errno));
5432 if ((fcntl(self_pipe[0], F_SETFL, O_NONBLOCK) < 0) ||
5433 (fcntl(self_pipe[0], F_SETFD, FD_CLOEXEC) < 0)) {
5434 ERROR("Error setting internal flags: %s", fr_syserror(errno));
5437 if ((fcntl(self_pipe[1], F_SETFL, O_NONBLOCK) < 0) ||
5438 (fcntl(self_pipe[1], F_SETFD, FD_CLOEXEC) < 0)) {
5439 ERROR("Error setting internal flags: %s", fr_syserror(errno));
5442 DEBUG4("Created signal pipe. Read end FD %i, write end FD %i", self_pipe[0], self_pipe[1]);
5444 if (!fr_event_fd_insert(el, 0, self_pipe[0], event_signal_handler, el)) {
5445 ERROR("Failed creating signal pipe handler: %s", fr_strerror());
5450 DEBUG("%s: #### Opening IP addresses and Ports ####", main_config.name);
5453 * The server temporarily switches to an unprivileged
5454 * user very early in the bootstrapping process.
5455 * However, some sockets MAY require privileged access
5456 * (bind to device, or to port < 1024, or to raw
5457 * sockets). Those sockets need to call suid up/down
5458 * themselves around the functions that need a privileged
5461 if (listen_init(cs, &head, spawn_flag) < 0) {
5465 main_config.listen = head;
5472 * At this point, no one has any business *ever* going
5475 rad_suid_down_permanent();
5482 static int proxy_delete_cb(UNUSED void *ctx, void *data)
5484 REQUEST *request = fr_packet2myptr(REQUEST, proxy, data);
5486 VERIFY_REQUEST(request);
5488 request->master_state = REQUEST_STOP_PROCESSING;
5490 #ifdef HAVE_PTHREAD_H
5491 if (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0) return 0;
5495 * If it's queued we can't delete it from the queue.
5497 * Otherwise, it's OK to delete it. Even RUNNING, because
5498 * that will get caught by the check above.
5500 if (request->child_state == REQUEST_QUEUED) return 0;
5502 request->in_proxy_hash = false;
5504 if (!request->in_request_hash) {
5505 request_done(request, FR_ACTION_DONE);
5509 * Delete it from the list.
5516 static int request_delete_cb(UNUSED void *ctx, void *data)
5518 REQUEST *request = fr_packet2myptr(REQUEST, packet, data);
5520 VERIFY_REQUEST(request);
5522 request->master_state = REQUEST_STOP_PROCESSING;
5525 * Not done, or the child thread is still processing it.
5527 if (request->child_state < REQUEST_RESPONSE_DELAY) return 0; /* continue */
5529 #ifdef HAVE_PTHREAD_H
5530 if (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0) return 0;
5534 rad_assert(request->in_proxy_hash == false);
5537 request->in_request_hash = false;
5539 if (request->ev) fr_event_delete(el, &request->ev);
5541 if (main_config.memory_report) {
5542 RDEBUG2("Cleaning up request packet ID %u with timestamp +%d",
5543 request->packet->id,
5544 (unsigned int) (request->timestamp - fr_start_time));
5549 rad_assert(!request->coa->in_proxy_hash);
5553 request_free(request);
5556 * Delete it from the list, and continue;
5562 void radius_event_free(void)
5568 * There are requests in the proxy hash that aren't
5569 * referenced from anywhere else. Remove them first.
5572 fr_packet_list_walk(proxy_list, NULL, proxy_delete_cb);
5576 rbtree_walk(pl, RBTREE_DELETE_ORDER, request_delete_cb, NULL);
5580 * Now that all requests have been marked "please stop",
5581 * ensure that all of the threads have exited.
5583 #ifdef HAVE_PTHREAD_H
5588 * Walk the lists again, ensuring that all
5589 * requests are done.
5591 if (main_config.memory_report) {
5596 fr_packet_list_walk(proxy_list, NULL, proxy_delete_cb);
5597 num = fr_packet_list_num_elements(proxy_list);
5599 ERROR("Proxy list has %d requests still in it.", num);
5604 rbtree_walk(pl, RBTREE_DELETE_ORDER, request_delete_cb, NULL);
5605 num = rbtree_num_elements(pl);
5607 ERROR("Request list has %d requests still in it.", num);
5616 fr_packet_list_free(proxy_list);
5619 if (proxy_ctx) talloc_free(proxy_ctx);
5624 if (debug_condition) talloc_free(debug_condition);
5627 int radius_event_process(void)
5631 return fr_event_loop(el);