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 RDEBUG("(%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)
1589 sock = listener->data;
1590 sock->last_packet = now.tv_sec;
1592 packet->proto = sock->proto;
1597 * Skip everything if required.
1599 if (listener->nodup) goto skip_dup;
1601 packet_p = rbtree_finddata(pl, &packet);
1603 rad_child_state_t child_state;
1605 request = fr_packet2myptr(REQUEST, packet, packet_p);
1606 rad_assert(request->in_request_hash);
1607 child_state = request->child_state;
1610 * Same src/dst ip/port, length, and
1611 * authentication vector: must be a duplicate.
1613 if ((request->packet->data_len == packet->data_len) &&
1614 (memcmp(request->packet->vector, packet->vector,
1615 sizeof(packet->vector)) == 0)) {
1618 switch (packet->code) {
1619 case PW_CODE_ACCESS_REQUEST:
1620 FR_STATS_INC(auth, total_dup_requests);
1623 #ifdef WITH_ACCOUNTING
1624 case PW_CODE_ACCOUNTING_REQUEST:
1625 FR_STATS_INC(acct, total_dup_requests);
1629 case PW_CODE_COA_REQUEST:
1630 FR_STATS_INC(coa, total_dup_requests);
1633 case PW_CODE_DISCONNECT_REQUEST:
1634 FR_STATS_INC(dsc, total_dup_requests);
1641 #endif /* WITH_STATS */
1644 * Tell the state machine that there's a
1645 * duplicate request.
1647 request->process(request, FR_ACTION_DUP);
1648 return 0; /* duplicate of live request */
1652 * Mark the request as done ASAP, and before we
1653 * log anything. The child may stop processing
1654 * the request just as we're logging the
1657 request_done(request, FR_ACTION_DONE);
1661 * It's a new request, not a duplicate. If the
1662 * old one is done, then we can clean it up.
1664 if (child_state <= REQUEST_RUNNING) {
1666 * The request is still QUEUED or RUNNING. That's a problem.
1668 ERROR("Received conflicting packet from "
1669 "client %s port %d - ID: %u due to "
1670 "unfinished request. Giving up on old request.",
1672 packet->src_port, packet->id);
1676 * Mark the old request as done. If there's no
1677 * child, the request will be cleaned up
1678 * immediately. If there is a child, we'll set a
1679 * timer to go clean up the request.
1681 } /* else the new packet is unique */
1684 * Quench maximum number of outstanding requests.
1686 if (main_config.max_requests &&
1687 ((count = rbtree_num_elements(pl)) > main_config.max_requests)) {
1688 RATE_LIMIT(ERROR("Dropping request (%d is too many): from client %s port %d - ID: %d", count,
1690 packet->src_port, packet->id);
1691 WARN("Please check the configuration file.\n"
1692 "\tThe value for 'max_requests' is probably set too low.\n"));
1694 exec_trigger(NULL, NULL, "server.max_requests", true);
1700 * Rate-limit the incoming packets
1702 if (sock && sock->max_rate) {
1705 pps = rad_pps(&sock->rate_pps_old, &sock->rate_pps_now, &sock->rate_time, &now);
1706 if (pps > sock->max_rate) {
1707 DEBUG("Dropping request due to rate limiting");
1710 sock->rate_pps_now++;
1714 * Allocate a pool for the request.
1717 ctx = talloc_pool(NULL, main_config.talloc_pool_size);
1719 talloc_set_name_const(ctx, "request_receive_pool");
1722 * The packet is still allocated from a different
1723 * context, but oh well.
1725 (void) talloc_steal(ctx, packet);
1728 request = request_setup(ctx, listener, packet, client, fun);
1735 * Mark it as a "real" request with a context.
1737 request->options |= RAD_REQUEST_OPTION_CTX;
1740 * Remember the request in the list.
1742 if (!listener->nodup) {
1743 if (!rbtree_insert(pl, &request->packet)) {
1744 RERROR("Failed to insert request in the list of live requests: discarding it");
1745 request_done(request, FR_ACTION_DONE);
1749 request->in_request_hash = true;
1753 * Process it. Send a response, and free it.
1755 if (listener->synchronous) {
1757 rad_assert(listener->type != RAD_LISTEN_DETAIL);
1760 request->listener->decode(request->listener, request);
1761 request->username = fr_pair_find_by_num(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1762 request->password = fr_pair_find_by_num(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
1766 if (request->reply->code != 0) {
1767 request->listener->send(request->listener, request);
1769 RDEBUG("Not sending reply");
1773 * Don't do delayed reject. Oh well.
1775 request_free(request);
1780 * Otherwise, insert it into the state machine.
1781 * The child threads will take care of processing it.
1783 request_queue_or_run(request, request_running);
1789 static REQUEST *request_setup(TALLOC_CTX *ctx, rad_listen_t *listener, RADIUS_PACKET *packet,
1790 RADCLIENT *client, RAD_REQUEST_FUNP fun)
1795 * Create and initialize the new request.
1797 request = request_alloc(ctx);
1802 request->reply = rad_alloc_reply(request, packet);
1803 if (!request->reply) {
1805 talloc_free(request);
1809 request->listener = listener;
1810 request->client = client;
1811 request->packet = talloc_steal(request, packet);
1812 request->number = request_num_counter++;
1813 request->priority = listener->type;
1814 request->master_state = REQUEST_ACTIVE;
1815 request->child_state = REQUEST_RUNNING;
1816 #ifdef DEBUG_STATE_MACHINE
1817 if (rad_debug_lvl) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n",
1818 request->number, __FUNCTION__,
1819 child_state_names[request->child_state],
1820 child_state_names[REQUEST_RUNNING]);
1822 request->handle = fun;
1826 request->listener->stats.last_packet = request->packet->timestamp.tv_sec;
1827 if (packet->code == PW_CODE_ACCESS_REQUEST) {
1828 request->client->auth.last_packet = request->packet->timestamp.tv_sec;
1829 radius_auth_stats.last_packet = request->packet->timestamp.tv_sec;
1830 #ifdef WITH_ACCOUNTING
1831 } else if (packet->code == PW_CODE_ACCOUNTING_REQUEST) {
1832 request->client->acct.last_packet = request->packet->timestamp.tv_sec;
1833 radius_acct_stats.last_packet = request->packet->timestamp.tv_sec;
1836 #endif /* WITH_STATS */
1839 * Status-Server packets go to the head of the queue.
1841 if (request->packet->code == PW_CODE_STATUS_SERVER) request->priority = 0;
1844 * Set virtual server identity
1846 if (client->server) {
1847 request->server = client->server;
1848 } else if (listener->server) {
1849 request->server = listener->server;
1851 request->server = NULL;
1854 request->root = &main_config;
1856 request->listener->count++;
1860 * The request passes many of our sanity checks.
1861 * From here on in, if anything goes wrong, we
1862 * send a reject message, instead of dropping the
1867 * Build the reply template from the request.
1870 request->reply->sockfd = request->packet->sockfd;
1871 request->reply->dst_ipaddr = request->packet->src_ipaddr;
1872 request->reply->src_ipaddr = request->packet->dst_ipaddr;
1873 request->reply->dst_port = request->packet->src_port;
1874 request->reply->src_port = request->packet->dst_port;
1875 request->reply->id = request->packet->id;
1876 request->reply->code = 0; /* UNKNOWN code */
1877 memcpy(request->reply->vector, request->packet->vector,
1878 sizeof(request->reply->vector));
1879 request->reply->vps = NULL;
1880 request->reply->data = NULL;
1881 request->reply->data_len = 0;
1887 /***********************************************************************
1891 ***********************************************************************/
1894 * Timer function for all TCP sockets.
1896 static void tcp_socket_timer(void *ctx)
1898 rad_listen_t *listener = talloc_get_type_abort(ctx, rad_listen_t);
1899 listen_socket_t *sock = listener->data;
1900 struct timeval end, now;
1902 fr_socket_limit_t *limit;
1906 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return;
1908 fr_event_now(el, &now);
1910 switch (listener->type) {
1912 case RAD_LISTEN_PROXY:
1913 limit = &sock->home->limit;
1917 case RAD_LISTEN_AUTH:
1918 #ifdef WITH_ACCOUNTING
1919 case RAD_LISTEN_ACCT:
1921 limit = &sock->limit;
1929 * If we enforce a lifetime, do it now.
1931 if (limit->lifetime > 0) {
1932 end.tv_sec = sock->opened + limit->lifetime;
1935 if (timercmp(&end, &now, <=)) {
1936 listener->print(listener, buffer, sizeof(buffer));
1937 DEBUG("Reached maximum lifetime on socket %s", buffer);
1943 * Proxy sockets get frozen, so that we don't use
1944 * them for new requests. But we do keep them
1945 * open to listen for replies to requests we had
1948 if (listener->type == RAD_LISTEN_PROXY) {
1949 PTHREAD_MUTEX_LOCK(&proxy_mutex);
1950 if (!fr_packet_list_socket_freeze(proxy_list,
1952 ERROR("Fatal error freezing socket: %s", fr_strerror());
1955 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
1960 * Mark the socket as "don't use if at all possible".
1962 listener->status = RAD_LISTEN_STATUS_FROZEN;
1963 event_new_fd(listener);
1972 * Enforce an idle timeout.
1974 if (limit->idle_timeout > 0) {
1975 struct timeval idle;
1977 rad_assert(sock->last_packet != 0);
1978 idle.tv_sec = sock->last_packet + limit->idle_timeout;
1981 if (timercmp(&idle, &now, <=)) {
1982 listener->print(listener, buffer, sizeof(buffer));
1983 DEBUG("Reached idle timeout on socket %s", buffer);
1988 * Enforce the minimum of idle timeout or lifetime.
1990 if (timercmp(&idle, &end, <)) {
1996 * Wake up at t + 0.5s. The code above checks if the timers
1997 * are <= t. This addition gives us a bit of leeway.
1999 end.tv_usec = USEC / 2;
2002 if (!fr_event_insert(el, tcp_socket_timer, listener, &end, &sock->ev)) {
2003 rad_panic("Failed to insert event");
2010 * Called by socket_del to remove requests with this socket
2012 static int eol_proxy_listener(void *ctx, void *data)
2014 rad_listen_t *this = talloc_get_type_abort(ctx, rad_listen_t);
2015 RADIUS_PACKET **proxy_p = data;
2018 request = fr_packet2myptr(REQUEST, proxy, proxy_p);
2019 if (request->proxy_listener != this) return 0;
2022 * The normal "remove_from_proxy_hash" tries to grab the
2023 * proxy mutex. We already have it held, so grabbing it
2024 * again will cause a deadlock. Instead, call the "no
2025 * lock" version of the function.
2027 rad_assert(request->in_proxy_hash == true);
2028 remove_from_proxy_hash_nl(request, false);
2031 * Don't mark it as DONE. The client can retransmit, and
2032 * the packet SHOULD be re-proxied somewhere else.
2034 * Return "2" means that the rbtree code will remove it
2035 * from the tree, and we don't need to do it ourselves.
2039 #endif /* WITH_PROXY */
2041 static int eol_listener(void *ctx, void *data)
2043 rad_listen_t *this = talloc_get_type_abort(ctx, rad_listen_t);
2044 RADIUS_PACKET **packet_p = data;
2047 request = fr_packet2myptr(REQUEST, packet, packet_p);
2048 if (request->listener != this) return 0;
2050 request->master_state = REQUEST_STOP_PROCESSING;
2051 request->process = request_done;
2055 #endif /* WITH_TCP */
2058 /***********************************************************************
2060 * Proxy handlers for the state machine.
2062 ***********************************************************************/
2065 * Called with the proxy mutex held
2067 static void remove_from_proxy_hash_nl(REQUEST *request, bool yank)
2069 VERIFY_REQUEST(request);
2071 if (!request->in_proxy_hash) return;
2073 fr_packet_list_id_free(proxy_list, request->proxy, yank);
2074 request->in_proxy_hash = false;
2077 * On the FIRST reply, decrement the count of outstanding
2078 * requests. Note that this is NOT the count of sent
2079 * packets, but whether or not the home server has
2082 if (request->home_server &&
2083 request->home_server->currently_outstanding) {
2084 request->home_server->currently_outstanding--;
2087 * If we're NOT sending it packets, AND it's been
2088 * a while since we got a response, then we don't
2089 * know if it's alive or dead.
2091 if ((request->home_server->currently_outstanding == 0) &&
2092 (request->home_server->state == HOME_STATE_ALIVE)) {
2093 struct timeval when, now;
2095 when.tv_sec = request->home_server->last_packet_recv ;
2098 timeradd(&when, request_response_window(request), &when);
2099 gettimeofday(&now, NULL);
2102 * last_packet + response_window
2104 * We *administratively* mark the home
2105 * server as "unknown" state, because we
2106 * haven't seen a packet for a while.
2108 if (timercmp(&now, &when, >)) {
2109 request->home_server->state = HOME_STATE_UNKNOWN;
2110 request->home_server->last_packet_sent = 0;
2111 request->home_server->last_packet_recv = 0;
2117 rad_assert(request->proxy_listener != NULL);
2118 request->proxy_listener->count--;
2120 request->proxy_listener = NULL;
2123 * Got from YES in hash, to NO, not in hash while we hold
2124 * the mutex. This guarantees that when another thread
2125 * grabs the mutex, the "not in hash" flag is correct.
2129 static void remove_from_proxy_hash(REQUEST *request)
2131 VERIFY_REQUEST(request);
2134 * Check this without grabbing the mutex because it's a
2135 * lot faster that way.
2137 if (!request->in_proxy_hash) return;
2140 * The "not in hash" flag is definitive. However, if the
2141 * flag says that it IS in the hash, there might still be
2142 * a race condition where it isn't.
2144 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2146 if (!request->in_proxy_hash) {
2147 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2151 remove_from_proxy_hash_nl(request, true);
2153 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2156 static int insert_into_proxy_hash(REQUEST *request)
2160 bool success = false;
2161 void *proxy_listener;
2163 VERIFY_REQUEST(request);
2165 rad_assert(request->proxy != NULL);
2166 rad_assert(request->home_server != NULL);
2167 rad_assert(proxy_list != NULL);
2170 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2171 proxy_listener = NULL;
2172 request->num_proxied_requests = 1;
2173 request->num_proxied_responses = 0;
2175 for (tries = 0; tries < 2; tries++) {
2177 listen_socket_t *sock;
2179 RDEBUG3("proxy: Trying to allocate ID (%d/2)", tries);
2180 success = fr_packet_list_id_alloc(proxy_list,
2181 request->home_server->proto,
2182 &request->proxy, &proxy_listener);
2185 if (tries > 0) continue; /* try opening new socket only once */
2187 #ifdef HAVE_PTHREAD_H
2188 if (proxy_no_new_sockets) break;
2191 RDEBUG3("proxy: Trying to open a new listener to the home server");
2192 this = proxy_new_listener(proxy_ctx, request->home_server, 0);
2194 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2198 request->proxy->src_port = 0; /* Use any new socket */
2199 proxy_listener = this;
2202 if (!fr_packet_list_socket_add(proxy_list, this->fd,
2204 &sock->other_ipaddr, sock->other_port,
2207 #ifdef HAVE_PTHREAD_H
2208 proxy_no_new_sockets = true;
2210 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2213 * This is bad. However, the
2214 * packet list now supports 256
2215 * open sockets, which should
2216 * minimize this problem.
2218 ERROR("Failed adding proxy socket: %s",
2224 * Add it to the event loop. Ensure that we have
2225 * only one mutex locked at a time.
2227 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2228 radius_update_listener(this);
2229 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2232 if (!proxy_listener || !success) {
2233 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2234 REDEBUG2("proxy: Failed allocating Id for proxied request");
2236 request->proxy_listener = NULL;
2237 request->in_proxy_hash = false;
2241 rad_assert(request->proxy->id >= 0);
2243 request->proxy_listener = proxy_listener;
2244 request->in_proxy_hash = true;
2245 RDEBUG3("proxy: request is now in proxy hash");
2248 * Keep track of maximum outstanding requests to a
2249 * particular home server. 'max_outstanding' is
2250 * enforced in home_server_ldb(), in realms.c.
2252 request->home_server->currently_outstanding++;
2255 request->proxy_listener->count++;
2258 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2260 RDEBUG3("proxy: allocating destination %s port %d - Id %d",
2261 inet_ntop(request->proxy->dst_ipaddr.af,
2262 &request->proxy->dst_ipaddr.ipaddr, buf, sizeof(buf)),
2263 request->proxy->dst_port,
2264 request->proxy->id);
2269 static int process_proxy_reply(REQUEST *request, RADIUS_PACKET *reply)
2272 int post_proxy_type = 0;
2275 VERIFY_REQUEST(request);
2278 * There may be a proxy reply, but it may be too late.
2280 if (!request->home_server->server && !request->proxy_listener) return 0;
2283 * Delete any reply we had accumulated until now.
2285 RDEBUG2("Clearing existing &reply: attributes");
2286 fr_pair_list_free(&request->reply->vps);
2289 * Run the packet through the post-proxy stage,
2290 * BEFORE playing games with the attributes.
2292 vp = fr_pair_find_by_num(request->config, PW_POST_PROXY_TYPE, 0, TAG_ANY);
2294 post_proxy_type = vp->vp_integer;
2296 * If we have a proxy_reply, and it was a reject, or a NAK
2297 * setup Post-Proxy <type>.
2299 * If the <type> doesn't have a section, then the Post-Proxy
2300 * section is ignored.
2303 DICT_VALUE *dval = NULL;
2305 switch (reply->code) {
2306 case PW_CODE_ACCESS_REJECT:
2307 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Reject");
2308 if (dval) post_proxy_type = dval->value;
2311 case PW_CODE_DISCONNECT_NAK:
2312 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, fr_packet_codes[reply->code]);
2313 if (dval) post_proxy_type = dval->value;
2316 case PW_CODE_COA_NAK:
2317 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, fr_packet_codes[reply->code]);
2318 if (dval) post_proxy_type = dval->value;
2326 * Create config:Post-Proxy-Type
2329 vp = radius_pair_create(request, &request->config, PW_POST_PROXY_TYPE, 0);
2330 vp->vp_integer = dval->value;
2334 if (post_proxy_type > 0) RDEBUG2("Found Post-Proxy-Type %s",
2335 dict_valnamebyattr(PW_POST_PROXY_TYPE, 0, post_proxy_type));
2338 VERIFY_PACKET(reply);
2341 * Decode the packet if required.
2343 if (request->proxy_listener) {
2344 rcode = request->proxy_listener->decode(request->proxy_listener, request);
2345 debug_packet(request, reply, true);
2348 * Pro-actively remove it from the proxy hash.
2349 * This is later than in 2.1.x, but it means that
2350 * the replies are authenticated before being
2351 * removed from the hash.
2354 (request->num_proxied_requests <= request->num_proxied_responses)) {
2355 remove_from_proxy_hash(request);
2358 rad_assert(!request->in_proxy_hash);
2360 } else if (request->in_proxy_hash) {
2361 remove_from_proxy_hash(request);
2364 if (request->home_pool && request->home_pool->virtual_server) {
2365 char const *old_server = request->server;
2367 request->server = request->home_pool->virtual_server;
2368 RDEBUG2("server %s {", request->server);
2370 rcode = process_post_proxy(post_proxy_type, request);
2373 request->server = old_server;
2375 rcode = process_post_proxy(post_proxy_type, request);
2379 if (request->packet->code == request->proxy->code) {
2381 * Don't run the next bit if we originated a CoA
2382 * packet, after receiving an Access-Request or
2383 * Accounting-Request.
2388 * There may NOT be a proxy reply, as we may be
2389 * running Post-Proxy-Type = Fail.
2392 fr_pair_add(&request->reply->vps, fr_pair_list_copy(request->reply, reply->vps));
2395 * Delete the Proxy-State Attributes from
2396 * the reply. These include Proxy-State
2397 * attributes from us and remote server.
2399 fr_pair_delete_by_num(&request->reply->vps, PW_PROXY_STATE, 0, TAG_ANY);
2402 vp = fr_pair_find_by_num(request->config, PW_RESPONSE_PACKET_TYPE, 0, TAG_ANY);
2403 if (vp && (vp->vp_integer != 256)) {
2404 request->proxy_reply = rad_alloc_reply(request, request->proxy);
2405 request->proxy_reply->code = vp->vp_integer;
2412 default: /* Don't do anything */
2414 case RLM_MODULE_FAIL:
2417 case RLM_MODULE_HANDLED:
2424 static void mark_home_server_alive(REQUEST *request, home_server_t *home)
2428 home->state = HOME_STATE_ALIVE;
2429 home->response_timeouts = 0;
2430 exec_trigger(request, home->cs, "home_server.alive", false);
2431 home->currently_outstanding = 0;
2432 home->num_sent_pings = 0;
2433 home->num_received_pings = 0;
2434 gettimeofday(&home->revive_time, NULL);
2436 fr_event_delete(el, &home->ev);
2438 RPROXY("Marking home server %s port %d alive",
2439 inet_ntop(request->proxy->dst_ipaddr.af,
2440 &request->proxy->dst_ipaddr.ipaddr,
2441 buffer, sizeof(buffer)),
2442 request->proxy->dst_port);
2446 int request_proxy_reply(RADIUS_PACKET *packet)
2448 RADIUS_PACKET **proxy_p;
2453 VERIFY_PACKET(packet);
2455 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2456 proxy_p = fr_packet_list_find_byreply(proxy_list, packet);
2459 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2460 PROXY("No outstanding request was found for %s packet from host %s port %d - ID %u",
2461 fr_packet_codes[packet->code],
2462 inet_ntop(packet->src_ipaddr.af,
2463 &packet->src_ipaddr.ipaddr,
2464 buffer, sizeof(buffer)),
2465 packet->src_port, packet->id);
2469 request = fr_packet2myptr(REQUEST, proxy, proxy_p);
2471 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2474 * No reply, BUT the current packet fails verification:
2475 * ignore it. This does the MD5 calculations in the
2476 * server core, but I guess we can fix that later.
2478 if (!request->proxy_reply &&
2479 (rad_verify(packet, request->proxy,
2480 request->home_server->secret) != 0)) {
2481 DEBUG("Ignoring spoofed proxy reply. Signature is invalid");
2486 * The home server sent us a packet which doesn't match
2487 * something we have: ignore it. This is done only to
2488 * catch the case of broken systems.
2490 if (request->proxy_reply &&
2491 (memcmp(request->proxy_reply->vector,
2493 sizeof(request->proxy_reply->vector)) != 0)) {
2494 RDEBUG2("Ignoring conflicting proxy reply");
2498 gettimeofday(&now, NULL);
2501 * Status-Server packets don't count as real packets.
2503 if (request->proxy->code != PW_CODE_STATUS_SERVER) {
2505 listen_socket_t *sock = request->proxy_listener->data;
2507 sock->last_packet = now.tv_sec;
2509 request->home_server->last_packet_recv = now.tv_sec;
2512 request->num_proxied_responses++;
2515 * If we have previously seen a reply, ignore the
2518 if (request->proxy_reply) {
2519 RDEBUG2("Discarding duplicate reply from host %s port %d - ID: %d",
2520 inet_ntop(packet->src_ipaddr.af,
2521 &packet->src_ipaddr.ipaddr,
2522 buffer, sizeof(buffer)),
2523 packet->src_port, packet->id);
2528 * Call the state machine to do something useful with the
2531 request->proxy_reply = talloc_steal(request, packet);
2532 packet->timestamp = now;
2533 request->priority = RAD_LISTEN_PROXY;
2537 * Update the proxy listener stats here, because only one
2538 * thread accesses that at a time. The home_server and
2539 * main proxy_*_stats structures are updated once the
2540 * request is cleaned up.
2542 request->proxy_listener->stats.total_responses++;
2544 request->home_server->stats.last_packet = packet->timestamp.tv_sec;
2545 request->proxy_listener->stats.last_packet = packet->timestamp.tv_sec;
2547 switch (request->proxy->code) {
2548 case PW_CODE_ACCESS_REQUEST:
2549 proxy_auth_stats.last_packet = packet->timestamp.tv_sec;
2551 if (request->proxy_reply->code == PW_CODE_ACCESS_ACCEPT) {
2552 request->proxy_listener->stats.total_access_accepts++;
2554 } else if (request->proxy_reply->code == PW_CODE_ACCESS_REJECT) {
2555 request->proxy_listener->stats.total_access_rejects++;
2557 } else if (request->proxy_reply->code == PW_CODE_ACCESS_CHALLENGE) {
2558 request->proxy_listener->stats.total_access_challenges++;
2562 #ifdef WITH_ACCOUNTING
2563 case PW_CODE_ACCOUNTING_REQUEST:
2564 proxy_acct_stats.last_packet = packet->timestamp.tv_sec;
2566 request->proxy_listener->stats.total_responses++;
2567 proxy_acct_stats.last_packet = packet->timestamp.tv_sec;
2573 case PW_CODE_COA_REQUEST:
2574 request->proxy_listener->stats.total_responses++;
2575 proxy_coa_stats.last_packet = packet->timestamp.tv_sec;
2578 case PW_CODE_DISCONNECT_REQUEST:
2579 request->proxy_listener->stats.total_responses++;
2580 proxy_dsc_stats.last_packet = packet->timestamp.tv_sec;
2590 * If we hadn't been sending the home server packets for
2591 * a while, just mark it alive. Or, if it was zombie,
2592 * it's now responded, and is therefore alive.
2594 if ((request->home_server->state == HOME_STATE_UNKNOWN) ||
2595 (request->home_server->state == HOME_STATE_ZOMBIE)) {
2596 mark_home_server_alive(request, request->home_server);
2600 * Tell the request state machine that we have a proxy
2601 * reply. Depending on the function, this should either
2602 * ignore it, or process it.
2604 request->process(request, FR_ACTION_PROXY_REPLY);
2610 static int setup_post_proxy_fail(REQUEST *request)
2612 DICT_VALUE const *dval = NULL;
2615 VERIFY_REQUEST(request);
2617 if (request->proxy->code == PW_CODE_ACCESS_REQUEST) {
2618 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0,
2619 "Fail-Authentication");
2620 #ifdef WITH_ACCOUNTING
2621 } else if (request->proxy->code == PW_CODE_ACCOUNTING_REQUEST) {
2622 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0,
2627 } else if (request->proxy->code == PW_CODE_COA_REQUEST) {
2628 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-CoA");
2630 } else if (request->proxy->code == PW_CODE_DISCONNECT_REQUEST) {
2631 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-Disconnect");
2634 WARN("Unknown packet type in Post-Proxy-Type Fail: ignoring");
2638 if (!dval) dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail");
2641 fr_pair_delete_by_num(&request->config, PW_POST_PROXY_TYPE, 0, TAG_ANY);
2645 vp = fr_pair_find_by_num(request->config, PW_POST_PROXY_TYPE, 0, TAG_ANY);
2646 if (!vp) vp = radius_pair_create(request, &request->config,
2647 PW_POST_PROXY_TYPE, 0);
2648 vp->vp_integer = dval->value;
2654 /** Process a request after the proxy has timed out.
2656 * Run the packet through Post-Proxy-Type Fail
2659 * digraph proxy_no_reply {
2662 * proxy_no_reply -> dup [ label = "DUP", arrowhead = "none" ];
2663 * proxy_no_reply -> timer [ label = "TIMER < max_request_time" ];
2664 * proxy_no_reply -> proxy_reply_too_late [ label = "PROXY_REPLY" arrowhead = "none"];
2665 * proxy_no_reply -> process_proxy_reply [ label = "RUN" ];
2666 * proxy_no_reply -> done [ label = "TIMER >= timeout" ];
2670 static void proxy_no_reply(REQUEST *request, int action)
2672 VERIFY_REQUEST(request);
2674 TRACE_STATE_MACHINE;
2679 request_dup(request);
2682 case FR_ACTION_TIMER:
2683 (void) request_max_time(request);
2686 case FR_ACTION_PROXY_REPLY:
2687 proxy_reply_too_late(request);
2691 if (process_proxy_reply(request, NULL)) {
2692 request->handle(request);
2694 request_finish(request, action);
2698 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
2703 /** Process the request after receiving a proxy reply.
2705 * Throught the post-proxy section, and the through the handler
2709 * digraph proxy_running {
2712 * proxy_running -> dup [ label = "DUP", arrowhead = "none" ];
2713 * proxy_running -> timer [ label = "TIMER < max_request_time" ];
2714 * proxy_running -> process_proxy_reply [ label = "RUN" ];
2715 * proxy_running -> done [ label = "TIMER >= timeout" ];
2719 static void proxy_running(REQUEST *request, int action)
2721 VERIFY_REQUEST(request);
2723 TRACE_STATE_MACHINE;
2728 request_dup(request);
2731 case FR_ACTION_TIMER:
2732 (void) request_max_time(request);
2736 if (process_proxy_reply(request, request->proxy_reply)) {
2737 request->handle(request);
2739 request_finish(request, action);
2742 default: /* duplicate proxy replies are suppressed */
2743 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
2748 /** Determine if a #REQUEST needs to be proxied, and perform pre-proxy operations
2750 * Whether a request will be proxied is determined by the attributes present
2751 * in request->config. If any of the following attributes are found, the
2752 * request may be proxied.
2754 * The key attributes are:
2755 * - PW_PROXY_TO_REALM - Specifies a realm the request should be proxied to.
2756 * - PW_HOME_SERVER_POOL - Specifies a specific home server pool to proxy to.
2757 * - PW_PACKET_DST_IP_ADDRESS - Specifies a specific IPv4 home server to proxy to.
2758 * - PW_PACKET_DST_IPV6_ADDRESS - Specifies a specific IPv6 home server to proxy to.
2760 * Certain packet types such as #PW_CODE_STATUS_SERVER will never be proxied.
2762 * If request should be proxied, will:
2763 * - Add request:Proxy-State
2764 * - Strip the current username value of its realm (depending on config)
2765 * - Create a CHAP-Challenge from the original request vector, if one doesn't already
2767 * - Call the pre-process section in the current server, or in the virtual server
2768 * associated with the home server pool we're proxying to.
2770 * @todo A lot of this logic is RADIUS specific, and should be moved out into a protocol
2771 * specific function.
2773 * @param request The #REQUEST to evaluate for proxying.
2774 * @return 0 if not proxying, 1 if request should be proxied, -1 on error.
2776 static int request_will_proxy(REQUEST *request)
2778 int rcode, pre_proxy_type = 0;
2779 char const *realmname = NULL;
2780 VALUE_PAIR *vp, *strippedname;
2781 home_server_t *home;
2782 REALM *realm = NULL;
2783 home_pool_t *pool = NULL;
2785 VERIFY_REQUEST(request);
2787 if (!request->root->proxy_requests) return 0;
2788 if (request->packet->dst_port == 0) return 0;
2789 if (request->packet->code == PW_CODE_STATUS_SERVER) return 0;
2790 if (request->in_proxy_hash) return 0;
2793 * FIXME: for 3.0, allow this only for rejects?
2795 if (request->reply->code != 0) return 0;
2797 vp = fr_pair_find_by_num(request->config, PW_PROXY_TO_REALM, 0, TAG_ANY);
2799 realm = realm_find2(vp->vp_strvalue);
2801 REDEBUG2("Cannot proxy to unknown realm %s",
2806 realmname = vp->vp_strvalue;
2809 * Figure out which pool to use.
2811 if (request->packet->code == PW_CODE_ACCESS_REQUEST) {
2812 pool = realm->auth_pool;
2814 #ifdef WITH_ACCOUNTING
2815 } else if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) {
2816 pool = realm->acct_pool;
2820 } else if ((request->packet->code == PW_CODE_COA_REQUEST) ||
2821 (request->packet->code == PW_CODE_DISCONNECT_REQUEST)) {
2822 pool = realm->coa_pool;
2829 } else if ((vp = fr_pair_find_by_num(request->config, PW_HOME_SERVER_POOL, 0, TAG_ANY)) != NULL) {
2832 switch (request->packet->code) {
2833 case PW_CODE_ACCESS_REQUEST:
2834 pool_type = HOME_TYPE_AUTH;
2837 #ifdef WITH_ACCOUNTING
2838 case PW_CODE_ACCOUNTING_REQUEST:
2839 pool_type = HOME_TYPE_ACCT;
2844 case PW_CODE_COA_REQUEST:
2845 case PW_CODE_DISCONNECT_REQUEST:
2846 pool_type = HOME_TYPE_COA;
2854 pool = home_pool_byname(vp->vp_strvalue, pool_type);
2857 * Send it directly to a home server (i.e. NAS)
2859 } else if (((vp = fr_pair_find_by_num(request->config, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY)) != NULL) ||
2860 ((vp = fr_pair_find_by_num(request->config, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY)) != NULL)) {
2862 fr_ipaddr_t dst_ipaddr;
2864 memset(&dst_ipaddr, 0, sizeof(dst_ipaddr));
2866 if (vp->da->attr == PW_PACKET_DST_IP_ADDRESS) {
2867 dst_ipaddr.af = AF_INET;
2868 dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
2869 dst_ipaddr.prefix = 32;
2871 dst_ipaddr.af = AF_INET6;
2872 memcpy(&dst_ipaddr.ipaddr.ip6addr, &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
2873 dst_ipaddr.prefix = 128;
2876 vp = fr_pair_find_by_num(request->config, PW_PACKET_DST_PORT, 0, TAG_ANY);
2878 if (request->packet->code == PW_CODE_ACCESS_REQUEST) {
2879 dst_port = PW_AUTH_UDP_PORT;
2881 #ifdef WITH_ACCOUNTING
2882 } else if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) {
2883 dst_port = PW_ACCT_UDP_PORT;
2887 } else if ((request->packet->code == PW_CODE_COA_REQUEST) ||
2888 (request->packet->code == PW_CODE_DISCONNECT_REQUEST)) {
2889 dst_port = PW_COA_UDP_PORT;
2891 } else { /* shouldn't happen for RADIUS... */
2896 dst_port = vp->vp_integer;
2900 * Nothing does CoA over TCP.
2902 home = home_server_find(&dst_ipaddr, dst_port, IPPROTO_UDP);
2906 WARN("No such home server %s port %u",
2907 inet_ntop(dst_ipaddr.af, &dst_ipaddr.ipaddr, buffer, sizeof(buffer)),
2908 (unsigned int) dst_port);
2913 * The home server is alive (or may be alive).
2914 * Send the packet to the IP.
2916 if (home->state != HOME_STATE_IS_DEAD) goto do_home;
2919 * The home server is dead. If you wanted
2920 * fail-over, you should have proxied to a pool.
2931 RWDEBUG2("Cancelling proxy as no home pool exists");
2935 if (request->listener->synchronous) {
2936 WARN("Cannot proxy a request which is from a 'synchronous' socket");
2940 request->home_pool = pool;
2942 home = home_server_ldb(realmname, pool, request);
2945 REDEBUG2("Failed to find live home server: Cancelling proxy");
2950 home_server_update_request(home, request);
2954 * Once we've decided to proxy a request, we cannot send
2955 * a CoA packet. So we free up any CoA packet here.
2957 if (request->coa) request_done(request->coa, FR_ACTION_DONE);
2961 * Remember that we sent the request to a Realm.
2963 if (realmname) pair_make_request("Realm", realmname, T_OP_EQ);
2966 * Strip the name, if told to.
2968 * Doing it here catches the case of proxied tunneled
2971 if (realm && (realm->strip_realm == true) &&
2972 (strippedname = fr_pair_find_by_num(request->proxy->vps, PW_STRIPPED_USER_NAME, 0, TAG_ANY)) != NULL) {
2974 * If there's a Stripped-User-Name attribute in
2975 * the request, then use THAT as the User-Name
2976 * for the proxied request, instead of the
2979 * This is done by making a copy of the
2980 * Stripped-User-Name attribute, turning it into
2981 * a User-Name attribute, deleting the
2982 * Stripped-User-Name and User-Name attributes
2983 * from the vps list, and making the new
2984 * User-Name the head of the vps list.
2986 vp = fr_pair_find_by_num(request->proxy->vps, PW_USER_NAME, 0, TAG_ANY);
2989 vp = radius_pair_create(NULL, NULL,
2991 rad_assert(vp != NULL); /* handled by above function */
2992 /* Insert at the START of the list */
2993 /* FIXME: Can't make assumptions about ordering */
2994 fr_cursor_init(&cursor, &vp);
2995 fr_cursor_merge(&cursor, request->proxy->vps);
2996 request->proxy->vps = vp;
2998 fr_pair_value_strcpy(vp, strippedname->vp_strvalue);
3001 * Do NOT delete Stripped-User-Name.
3006 * If there is no PW_CHAP_CHALLENGE attribute but
3007 * there is a PW_CHAP_PASSWORD we need to add it
3008 * since we can't use the request authenticator
3009 * anymore - we changed it.
3011 if ((request->packet->code == PW_CODE_ACCESS_REQUEST) &&
3012 fr_pair_find_by_num(request->proxy->vps, PW_CHAP_PASSWORD, 0, TAG_ANY) &&
3013 fr_pair_find_by_num(request->proxy->vps, PW_CHAP_CHALLENGE, 0, TAG_ANY) == NULL) {
3014 vp = radius_pair_create(request->proxy, &request->proxy->vps, PW_CHAP_CHALLENGE, 0);
3015 fr_pair_value_memcpy(vp, request->packet->vector, sizeof(request->packet->vector));
3019 * The RFC's say we have to do this, but FreeRADIUS
3022 vp = radius_pair_create(request->proxy, &request->proxy->vps, PW_PROXY_STATE, 0);
3023 fr_pair_value_sprintf(vp, "%u", request->packet->id);
3026 * Should be done BEFORE inserting into proxy hash, as
3027 * pre-proxy may use this information, or change it.
3029 request->proxy->code = request->packet->code;
3032 * Call the pre-proxy routines.
3034 vp = fr_pair_find_by_num(request->config, PW_PRE_PROXY_TYPE, 0, TAG_ANY);
3036 DICT_VALUE const *dval = dict_valbyattr(vp->da->attr, vp->da->vendor, vp->vp_integer);
3037 /* Must be a validation issue */
3039 RDEBUG2("Found Pre-Proxy-Type %s", dval->name);
3040 pre_proxy_type = vp->vp_integer;
3044 * home_pool may be NULL when originating CoA packets,
3045 * because they go directly to an IP address.
3047 if (request->home_pool && request->home_pool->virtual_server) {
3048 char const *old_server = request->server;
3050 request->server = request->home_pool->virtual_server;
3052 RDEBUG2("server %s {", request->server);
3054 rcode = process_pre_proxy(pre_proxy_type, request);
3058 request->server = old_server;
3060 rcode = process_pre_proxy(pre_proxy_type, request);
3064 case RLM_MODULE_FAIL:
3065 case RLM_MODULE_INVALID:
3066 case RLM_MODULE_NOTFOUND:
3067 case RLM_MODULE_USERLOCK:
3069 /* FIXME: debug print failed stuff */
3072 case RLM_MODULE_REJECT:
3073 case RLM_MODULE_HANDLED:
3077 * Only proxy the packet if the pre-proxy code succeeded.
3079 case RLM_MODULE_NOOP:
3081 case RLM_MODULE_UPDATED:
3086 static int proxy_to_virtual_server(REQUEST *request)
3090 if (request->packet->dst_port == 0) {
3091 WARN("Cannot proxy an internal request");
3095 DEBUG("Proxying to virtual server %s",
3096 request->home_server->server);
3099 * Packets to virtual servers don't get
3100 * retransmissions sent to them. And the virtual
3101 * server is run ONLY if we have no child
3102 * threads, or we're running in a child thread.
3104 rad_assert(!spawn_flag || !we_are_master());
3106 fake = request_alloc_fake(request);
3108 fake->packet->vps = fr_pair_list_copy(fake->packet, request->packet->vps);
3109 talloc_free(request->proxy);
3111 fake->server = request->home_server->server;
3112 fake->handle = request->handle;
3113 fake->process = NULL; /* should never be run for anything */
3116 * Run the virtual server.
3118 request_running(fake, FR_ACTION_RUN);
3120 request->proxy = talloc_steal(request, fake->packet);
3121 fake->packet = NULL;
3122 request->proxy_reply = talloc_steal(request, fake->reply);
3128 * No reply code, toss the reply we have,
3129 * and do post-proxy-type Fail.
3131 if (!request->proxy_reply->code) {
3132 TALLOC_FREE(request->proxy_reply);
3133 setup_post_proxy_fail(request);
3137 * Do the proxy reply (if any)
3139 if (process_proxy_reply(request, request->proxy_reply)) {
3140 request->handle(request);
3143 return -1; /* so we call request_finish */
3147 static int request_proxy(REQUEST *request)
3151 VERIFY_REQUEST(request);
3153 rad_assert(request->parent == NULL);
3154 rad_assert(request->home_server != NULL);
3156 if (request->master_state == REQUEST_STOP_PROCESSING) return 0;
3160 RWDEBUG("Cannot proxy and originate CoA packets at the same time. Cancelling CoA request");
3161 request_done(request->coa, FR_ACTION_DONE);
3166 * The request may need sending to a virtual server.
3167 * This code is more than a little screwed up. The rest
3168 * of the state machine doesn't handle parent / child
3169 * relationships well. i.e. if the child request takes
3170 * too long, the core will mark the *parent* as "stop
3171 * processing". And the child will continue without
3172 * knowing anything...
3174 * So, we have some horrible hacks to get around that.
3176 if (request->home_server->server) return proxy_to_virtual_server(request);
3179 * We're actually sending a proxied packet. Do that now.
3181 if (!request->in_proxy_hash && !insert_into_proxy_hash(request)) {
3182 RPROXY("Failed to insert request into the proxy list");
3186 rad_assert(request->proxy->id >= 0);
3188 if (rad_debug_lvl) {
3189 struct timeval *response_window;
3191 response_window = request_response_window(request);
3194 if (request->home_server->tls) {
3195 RDEBUG2("Proxying request to home server %s port %d (TLS) timeout %d.%06d",
3196 inet_ntop(request->proxy->dst_ipaddr.af,
3197 &request->proxy->dst_ipaddr.ipaddr,
3198 buffer, sizeof(buffer)),
3199 request->proxy->dst_port,
3200 (int) response_window->tv_sec, (int) response_window->tv_usec);
3203 RDEBUG2("Proxying request to home server %s port %d timeout %d.%06d",
3204 inet_ntop(request->proxy->dst_ipaddr.af,
3205 &request->proxy->dst_ipaddr.ipaddr,
3206 buffer, sizeof(buffer)),
3207 request->proxy->dst_port,
3208 (int) response_window->tv_sec, (int) response_window->tv_usec);
3213 gettimeofday(&request->proxy->timestamp, NULL);
3214 request->home_server->last_packet_sent = request->proxy->timestamp.tv_sec;
3217 * Encode the packet before we do anything else.
3219 request->proxy_listener->encode(request->proxy_listener, request);
3220 debug_packet(request, request->proxy, false);
3223 * Set the state function, then the state, no child, and
3226 * The order here is different from other state changes
3227 * due to race conditions with replies from the home
3230 request->process = proxy_wait_for_reply;
3231 request->child_state = REQUEST_PROXIED;
3232 request->component = "<REQUEST_PROXIED>";
3233 request->module = "";
3237 * And send the packet.
3239 request->proxy_listener->send(request->proxy_listener, request);
3244 * Proxy the packet as if it was new.
3246 static int request_proxy_anew(REQUEST *request)
3248 home_server_t *home;
3250 VERIFY_REQUEST(request);
3253 * Delete the request from the proxy list.
3255 * The packet list code takes care of ensuring that IDs
3256 * aren't reused until all 256 IDs have been used. So
3257 * there's a 1/256 chance of re-using the same ID when
3258 * we're sending to the same home server. Which is
3261 remove_from_proxy_hash(request);
3264 * Find a live home server for the request.
3266 home = home_server_ldb(NULL, request->home_pool, request);
3268 REDEBUG2("Failed to find live home server for request");
3270 if (setup_post_proxy_fail(request)) {
3271 request_queue_or_run(request, proxy_running);
3273 gettimeofday(&request->reply->timestamp, NULL);
3274 request_cleanup_delay_init(request);
3279 #ifdef WITH_ACCOUNTING
3281 * Update the Acct-Delay-Time attribute, since the LAST
3282 * time we tried to retransmit this packet.
3284 if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) {
3287 vp = fr_pair_find_by_num(request->proxy->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY);
3288 if (!vp) vp = radius_pair_create(request->proxy,
3289 &request->proxy->vps,
3290 PW_ACCT_DELAY_TIME, 0);
3294 gettimeofday(&now, NULL);
3295 vp->vp_integer += now.tv_sec - request->proxy->timestamp.tv_sec;
3301 * May have failed over to a "fallback" virtual server.
3302 * If so, run that instead of doing proxying to a real
3306 request->home_server = home;
3307 TALLOC_FREE(request->proxy);
3309 (void) proxy_to_virtual_server(request);
3313 home_server_update_request(home, request);
3315 if (!insert_into_proxy_hash(request)) {
3316 RPROXY("Failed to insert retransmission into the proxy list");
3317 goto post_proxy_fail;
3321 * Free the old packet, to force re-encoding
3323 talloc_free(request->proxy->data);
3324 request->proxy->data = NULL;
3325 request->proxy->data_len = 0;
3327 if (request_proxy(request) != 1) goto post_proxy_fail;
3333 /** Ping a home server.
3336 static void request_ping(REQUEST *request, int action)
3338 home_server_t *home = request->home_server;
3341 VERIFY_REQUEST(request);
3343 TRACE_STATE_MACHINE;
3347 case FR_ACTION_TIMER:
3348 ERROR("No response to status check %d ID %u for home server %s port %d",
3351 inet_ntop(request->proxy->dst_ipaddr.af,
3352 &request->proxy->dst_ipaddr.ipaddr,
3353 buffer, sizeof(buffer)),
3354 request->proxy->dst_port);
3357 case FR_ACTION_PROXY_REPLY:
3358 rad_assert(request->in_proxy_hash);
3360 request->home_server->num_received_pings++;
3361 RPROXY("Received response to status check %d ID %u (%d in current sequence)",
3362 request->number, request->proxy->id, home->num_received_pings);
3365 * Remove the request from any hashes
3367 fr_event_delete(el, &request->ev);
3368 remove_from_proxy_hash(request);
3371 * The control socket may have marked the home server as
3372 * alive. OR, it may have suddenly started responding to
3373 * requests again. If so, don't re-do the "make alive"
3376 if (home->state == HOME_STATE_ALIVE) break;
3379 * It's dead, and we haven't received enough ping
3380 * responses to mark it "alive". Wait a bit.
3382 * If it's zombie, we mark it alive immediately.
3384 if ((home->state == HOME_STATE_IS_DEAD) &&
3385 (home->num_received_pings < home->num_pings_to_alive)) {
3390 * Mark it alive and delete any outstanding
3393 mark_home_server_alive(request, home);
3397 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
3401 rad_assert(!request->in_request_hash);
3402 rad_assert(request->ev == NULL);
3404 request_done(request, FR_ACTION_DONE);
3408 * Add +/- 2s of jitter, as suggested in RFC 3539
3411 static void add_jitter(struct timeval *when)
3418 jitter ^= (jitter >> 10);
3419 jitter &= ((1 << 22) - 1); /* 22 bits of 1 */
3422 * Add in ~ (4 * USEC) of jitter.
3424 tv_add(when, jitter);
3428 * Called from start of zombie period, OR after control socket
3429 * marks the home server dead.
3431 static void ping_home_server(void *ctx)
3433 home_server_t *home = talloc_get_type_abort(ctx, home_server_t);
3436 struct timeval when, now;
3438 if ((home->state == HOME_STATE_ALIVE) ||
3439 (home->ev != NULL)) {
3443 gettimeofday(&now, NULL);
3447 * We've run out of zombie time. Mark it dead.
3449 if (home->state == HOME_STATE_ZOMBIE) {
3450 when = home->zombie_period_start;
3451 when.tv_sec += home->zombie_period;
3453 if (timercmp(&when, &now, <)) {
3454 DEBUG("PING: Zombie period is over for home server %s", home->log_name);
3455 mark_home_server_dead(home, &now);
3460 * We're not supposed to be pinging it. Just wake up
3461 * when we're supposed to mark it dead.
3463 if (home->ping_check == HOME_PING_CHECK_NONE) {
3464 if (home->state == HOME_STATE_ZOMBIE) {
3465 home->when = home->zombie_period_start;
3466 home->when.tv_sec += home->zombie_period;
3467 INSERT_EVENT(ping_home_server, home);
3471 * Else mark_home_server_dead will set a timer
3472 * for revive_interval.
3478 request = request_alloc(NULL);
3479 if (!request) return;
3480 request->number = request_num_counter++;
3483 request->proxy = rad_alloc(request, true);
3484 rad_assert(request->proxy != NULL);
3486 if (home->ping_check == HOME_PING_CHECK_STATUS_SERVER) {
3487 request->proxy->code = PW_CODE_STATUS_SERVER;
3489 fr_pair_make(request->proxy, &request->proxy->vps,
3490 "Message-Authenticator", "0x00", T_OP_SET);
3492 } else if ((home->type == HOME_TYPE_AUTH) ||
3493 (home->type == HOME_TYPE_AUTH_ACCT)) {
3494 request->proxy->code = PW_CODE_ACCESS_REQUEST;
3496 fr_pair_make(request->proxy, &request->proxy->vps,
3497 "User-Name", home->ping_user_name, T_OP_SET);
3498 fr_pair_make(request->proxy, &request->proxy->vps,
3499 "User-Password", home->ping_user_password, T_OP_SET);
3500 fr_pair_make(request->proxy, &request->proxy->vps,
3501 "Service-Type", "Authenticate-Only", T_OP_SET);
3502 fr_pair_make(request->proxy, &request->proxy->vps,
3503 "Message-Authenticator", "0x00", T_OP_SET);
3505 #ifdef WITH_ACCOUNTING
3506 } else if (home->type == HOME_TYPE_ACCT) {
3507 request->proxy->code = PW_CODE_ACCOUNTING_REQUEST;
3509 fr_pair_make(request->proxy, &request->proxy->vps,
3510 "User-Name", home->ping_user_name, T_OP_SET);
3511 fr_pair_make(request->proxy, &request->proxy->vps,
3512 "Acct-Status-Type", "Stop", T_OP_SET);
3513 fr_pair_make(request->proxy, &request->proxy->vps,
3514 "Acct-Session-Id", "00000000", T_OP_SET);
3515 vp = fr_pair_make(request->proxy, &request->proxy->vps,
3516 "Event-Timestamp", "0", T_OP_SET);
3517 vp->vp_date = now.tv_sec;
3522 * Unkown home server type.
3524 talloc_free(request);
3528 vp = fr_pair_make(request->proxy, &request->proxy->vps,
3529 "NAS-Identifier", "", T_OP_SET);
3531 fr_pair_value_sprintf(vp, "Status Check %u. Are you alive?",
3532 home->num_sent_pings);
3536 request->proxy->proto = home->proto;
3538 request->proxy->src_ipaddr = home->src_ipaddr;
3539 request->proxy->dst_ipaddr = home->ipaddr;
3540 request->proxy->dst_port = home->port;
3541 request->home_server = home;
3542 #ifdef DEBUG_STATE_MACHINE
3543 if (rad_debug_lvl) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n", request->number, __FUNCTION__,
3544 child_state_names[request->child_state],
3545 child_state_names[REQUEST_DONE]);
3546 if (rad_debug_lvl) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_ping");
3548 #ifdef HAVE_PTHREAD_H
3549 rad_assert(request->child_pid == NO_SUCH_CHILD_PID);
3551 request->child_state = REQUEST_PROXIED;
3552 request->process = request_ping;
3554 rad_assert(request->proxy_listener == NULL);
3556 if (!insert_into_proxy_hash(request)) {
3557 RPROXY("Failed to insert status check %d into proxy list. Discarding it.",
3560 rad_assert(!request->in_request_hash);
3561 rad_assert(!request->in_proxy_hash);
3562 rad_assert(request->ev == NULL);
3563 talloc_free(request);
3568 * Set up the timer callback.
3571 when.tv_sec += home->ping_timeout;
3573 DEBUG("PING: Waiting %u seconds for response to ping",
3574 home->ping_timeout);
3576 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3577 home->num_sent_pings++;
3579 rad_assert(request->proxy_listener != NULL);
3580 debug_packet(request, request->proxy, false);
3581 request->proxy_listener->send(request->proxy_listener,
3585 * Add +/- 2s of jitter, as suggested in RFC 3539
3586 * and in the Issues and Fixes draft.
3589 home->when.tv_sec += home->ping_interval;
3591 add_jitter(&home->when);
3593 DEBUG("PING: Next status packet in %u seconds", home->ping_interval);
3594 INSERT_EVENT(ping_home_server, home);
3597 static void home_trigger(home_server_t *home, char const *trigger)
3599 REQUEST *my_request;
3600 RADIUS_PACKET *my_packet;
3602 my_request = talloc_zero(NULL, REQUEST);
3603 my_packet = talloc_zero(my_request, RADIUS_PACKET);
3604 my_request->proxy = my_packet;
3605 my_packet->dst_ipaddr = home->ipaddr;
3606 my_packet->src_ipaddr = home->src_ipaddr;
3608 exec_trigger(my_request, home->cs, trigger, false);
3609 talloc_free(my_request);
3612 static void mark_home_server_zombie(home_server_t *home, struct timeval *now, struct timeval *response_window)
3619 rad_assert((home->state == HOME_STATE_ALIVE) ||
3620 (home->state == HOME_STATE_UNKNOWN));
3623 * We've received a real packet recently. Don't mark the
3624 * server as zombie until we've received NO packets for a
3625 * while. The "1/4" of zombie period was chosen rather
3626 * arbitrarily. It's a balance between too short, which
3627 * gives quick fail-over and fail-back, or too long,
3628 * where the proxy still sends packets to an unresponsive
3631 start = now->tv_sec - ((home->zombie_period + 3) / 4);
3632 if (home->last_packet_recv >= start) {
3633 DEBUG("Received reply from home server %d seconds ago. Might not be zombie.",
3634 (int) (now->tv_sec - home->last_packet_recv));
3638 home->state = HOME_STATE_ZOMBIE;
3639 home_trigger(home, "home_server.zombie");
3642 * Set the home server to "zombie", as of the time
3645 home->zombie_period_start.tv_sec = start;
3646 home->zombie_period_start.tv_usec = USEC / 2;
3648 fr_event_delete(el, &home->ev);
3650 home->num_sent_pings = 0;
3651 home->num_received_pings = 0;
3653 PROXY( "Marking home server %s port %d as zombie (it has not responded in %d.%06d seconds).",
3654 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
3655 buffer, sizeof(buffer)),
3656 home->port, (int) response_window->tv_sec, (int) response_window->tv_usec);
3658 ping_home_server(home);
3662 void revive_home_server(void *ctx)
3664 home_server_t *home = talloc_get_type_abort(ctx, home_server_t);
3667 home->state = HOME_STATE_ALIVE;
3668 home->response_timeouts = 0;
3669 home_trigger(home, "home_server.alive");
3670 home->currently_outstanding = 0;
3671 gettimeofday(&home->revive_time, NULL);
3674 * Delete any outstanding events.
3677 if (home->ev) fr_event_delete(el, &home->ev);
3679 PROXY( "Marking home server %s port %d alive again... we have no idea if it really is alive or not.",
3680 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
3681 buffer, sizeof(buffer)),
3685 void mark_home_server_dead(home_server_t *home, struct timeval *when)
3687 int previous_state = home->state;
3690 PROXY( "Marking home server %s port %d as dead.",
3691 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
3692 buffer, sizeof(buffer)),
3695 home->state = HOME_STATE_IS_DEAD;
3696 home_trigger(home, "home_server.dead");
3698 if (home->ping_check != HOME_PING_CHECK_NONE) {
3700 * If the control socket marks us dead, start
3701 * pinging. Otherwise, we already started
3702 * pinging when it was marked "zombie".
3704 if (previous_state == HOME_STATE_ALIVE) {
3705 ping_home_server(home);
3707 DEBUG("PING: Already pinging home server %s", home->log_name);
3712 * Revive it after a fixed period of time. This
3713 * is very, very, bad.
3716 home->when.tv_sec += home->revive_interval;
3718 DEBUG("PING: Reviving home server %s in %u seconds", home->log_name, home->revive_interval);
3720 INSERT_EVENT(revive_home_server, home);
3724 /** Wait for a reply after proxying a request.
3726 * Retransmit the proxied packet, or time out and go to
3727 * proxy_no_reply. Mark the home server unresponsive, etc.
3729 * If we do receive a reply, we transition to proxy_running.
3732 * digraph proxy_wait_for_reply {
3733 * proxy_wait_for_reply;
3735 * proxy_wait_for_reply -> retransmit_proxied_request [ label = "DUP", arrowhead = "none" ];
3736 * proxy_wait_for_reply -> proxy_no_reply [ label = "TIMER >= response_window" ];
3737 * proxy_wait_for_reply -> timer [ label = "TIMER < max_request_time" ];
3738 * proxy_wait_for_reply -> proxy_running [ label = "PROXY_REPLY" arrowhead = "none"];
3739 * proxy_wait_for_reply -> done [ label = "TIMER >= max_request_time" ];
3743 static void proxy_wait_for_reply(REQUEST *request, int action)
3745 struct timeval now, when;
3746 struct timeval *response_window = NULL;
3747 home_server_t *home = request->home_server;
3750 VERIFY_REQUEST(request);
3752 TRACE_STATE_MACHINE;
3755 rad_assert(request->packet->code != PW_CODE_STATUS_SERVER);
3756 rad_assert(request->home_server != NULL);
3758 gettimeofday(&now, NULL);
3763 * We have a reply, ignore the retransmit.
3765 if (request->proxy_reply) return;
3768 * The request was proxied to a virtual server.
3769 * Ignore the retransmit.
3771 if (request->home_server->server) return;
3774 * Use a new connection when the home server is
3775 * dead, or when there's no proxy listener, or
3776 * when the listener is failed or dead.
3778 * If the listener is known or frozen, use it for
3781 if ((home->state == HOME_STATE_IS_DEAD) ||
3782 !request->proxy_listener ||
3783 (request->proxy_listener->status >= RAD_LISTEN_STATUS_EOL)) {
3784 request_proxy_anew(request);
3790 * The home server is still alive, but TCP. We
3791 * rely on TCP to get the request and reply back.
3792 * So there's no need to retransmit.
3794 if (home->proto == IPPROTO_TCP) {
3795 DEBUG2("Suppressing duplicate proxied request (tcp) to home server %s port %d proto TCP - ID: %d",
3796 inet_ntop(request->proxy->dst_ipaddr.af,
3797 &request->proxy->dst_ipaddr.ipaddr,
3798 buffer, sizeof(buffer)),
3799 request->proxy->dst_port,
3800 request->proxy->id);
3806 * More than one retransmit a second is stupid,
3807 * and should be suppressed by the proxy.
3809 when = request->proxy->timestamp;
3812 if (timercmp(&now, &when, <)) {
3813 DEBUG2("Suppressing duplicate proxied request (too fast) to home server %s port %d proto TCP - ID: %d",
3814 inet_ntop(request->proxy->dst_ipaddr.af,
3815 &request->proxy->dst_ipaddr.ipaddr,
3816 buffer, sizeof(buffer)),
3817 request->proxy->dst_port,
3818 request->proxy->id);
3822 #ifdef WITH_ACCOUNTING
3824 * If we update the Acct-Delay-Time, we need to
3827 if ((request->packet->code == PW_CODE_ACCOUNTING_REQUEST) &&
3828 fr_pair_find_by_num(request->proxy->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY)) {
3829 request_proxy_anew(request);
3834 RDEBUG2("Sending duplicate proxied request to home server %s port %d - ID: %d",
3835 inet_ntop(request->proxy->dst_ipaddr.af,
3836 &request->proxy->dst_ipaddr.ipaddr,
3837 buffer, sizeof(buffer)),
3838 request->proxy->dst_port,
3839 request->proxy->id);
3840 request->num_proxied_requests++;
3842 rad_assert(request->proxy_listener != NULL);
3843 FR_STATS_TYPE_INC(home->stats.total_requests);
3844 home->last_packet_sent = now.tv_sec;
3845 request->proxy->timestamp = now;
3846 debug_packet(request, request->proxy, false);
3847 request->proxy_listener->send(request->proxy_listener, request);
3850 case FR_ACTION_TIMER:
3851 response_window = request_response_window(request);
3854 if (!request->proxy_listener ||
3855 (request->proxy_listener->status >= RAD_LISTEN_STATUS_EOL)) {
3856 remove_from_proxy_hash(request);
3858 when = request->packet->timestamp;
3859 when.tv_sec += request->root->max_request_time;
3861 if (timercmp(&when, &now, >)) {
3862 RDEBUG("Waiting for client retransmission in order to do a proxy retransmit");
3863 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3870 * Wake up "response_window" time in the future.
3871 * i.e. when MY packet hasn't received a response.
3873 * Note that we DO NOT mark the home server as
3874 * zombie if it doesn't respond to us. It may be
3875 * responding to other (better looking) packets.
3877 when = request->proxy->timestamp;
3878 timeradd(&when, response_window, &when);
3881 * Not at the response window. Set the timer for
3884 if (timercmp(&when, &now, >)) {
3885 struct timeval diff;
3886 timersub(&when, &now, &diff);
3888 RDEBUG("Expecting proxy response no later than %d.%06d seconds from now",
3889 (int) diff.tv_sec, (int) diff.tv_usec);
3890 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3895 RDEBUG("No proxy response, giving up on request and marking it done");
3898 * If we haven't received any packets for
3899 * "response_window", then mark the home server
3902 * This check should really be part of a home
3903 * server state machine.
3905 if (((home->state == HOME_STATE_ALIVE) ||
3906 (home->state == HOME_STATE_UNKNOWN))
3908 home->response_timeouts++;
3909 if (home->response_timeouts >= home->max_response_timeouts)
3910 mark_home_server_zombie(home, &now, response_window);
3913 FR_STATS_TYPE_INC(home->stats.total_timeouts);
3914 if (home->type == HOME_TYPE_AUTH) {
3915 if (request->proxy_listener) FR_STATS_TYPE_INC(request->proxy_listener->stats.total_timeouts);
3916 FR_STATS_TYPE_INC(proxy_auth_stats.total_timeouts);
3919 else if (home->type == HOME_TYPE_ACCT) {
3920 if (request->proxy_listener) FR_STATS_TYPE_INC(request->proxy_listener->stats.total_timeouts);
3921 FR_STATS_TYPE_INC(proxy_acct_stats.total_timeouts);
3925 else if (home->type == HOME_TYPE_COA) {
3926 if (request->proxy_listener) FR_STATS_TYPE_INC(request->proxy_listener->stats.total_timeouts);
3928 if (request->packet->code == PW_CODE_COA_REQUEST) {
3929 FR_STATS_TYPE_INC(proxy_coa_stats.total_timeouts);
3931 FR_STATS_TYPE_INC(proxy_dsc_stats.total_timeouts);
3937 * There was no response within the window. Stop
3938 * the request. If the client retransmitted, it
3939 * may have failed over to another home server.
3940 * But that one may be dead, too.
3942 * The extra verbose message if we have a username,
3943 * is extremely useful if the proxy is part of a chain
3944 * and the final home server, is not the one we're
3947 if (request->username) {
3948 RERROR("Failing proxied request for user \"%s\", due to lack of any response from home "
3949 "server %s port %d",
3950 request->username->vp_strvalue,
3951 inet_ntop(request->proxy->dst_ipaddr.af,
3952 &request->proxy->dst_ipaddr.ipaddr,
3953 buffer, sizeof(buffer)),
3954 request->proxy->dst_port);
3956 RERROR("Failing proxied request, due to lack of any response from home server %s port %d",
3957 inet_ntop(request->proxy->dst_ipaddr.af,
3958 &request->proxy->dst_ipaddr.ipaddr,
3959 buffer, sizeof(buffer)),
3960 request->proxy->dst_port);
3963 if (setup_post_proxy_fail(request)) {
3964 request_queue_or_run(request, proxy_no_reply);
3966 gettimeofday(&request->reply->timestamp, NULL);
3967 request_cleanup_delay_init(request);
3972 * We received a new reply. Go process it.
3974 case FR_ACTION_PROXY_REPLY:
3975 request_queue_or_run(request, proxy_running);
3979 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
3983 #endif /* WITH_PROXY */
3986 /***********************************************************************
3990 ***********************************************************************/
3992 static int null_handler(UNUSED REQUEST *request)
3998 * See if we need to originate a CoA request.
4000 static void request_coa_originate(REQUEST *request)
4002 int rcode, pre_proxy_type = 0;
4008 VERIFY_REQUEST(request);
4010 rad_assert(request->coa != NULL);
4011 rad_assert(request->proxy == NULL);
4012 rad_assert(!request->in_proxy_hash);
4013 rad_assert(request->proxy_reply == NULL);
4016 * Check whether we want to originate one, or cancel one.
4018 vp = fr_pair_find_by_num(request->config, PW_SEND_COA_REQUEST, 0, TAG_ANY);
4020 vp = fr_pair_find_by_num(request->coa->proxy->vps, PW_SEND_COA_REQUEST, 0, TAG_ANY);
4024 if (vp->vp_integer == 0) {
4026 TALLOC_FREE(request->coa);
4031 if (!main_config.proxy_requests) {
4032 RWDEBUG("Cannot originate CoA packets unless 'proxy_requests = yes'");
4033 TALLOC_FREE(request->coa);
4040 * src_ipaddr will be set up in proxy_encode.
4042 memset(&ipaddr, 0, sizeof(ipaddr));
4043 vp = fr_pair_find_by_num(coa->proxy->vps, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY);
4045 ipaddr.af = AF_INET;
4046 ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
4048 } else if ((vp = fr_pair_find_by_num(coa->proxy->vps, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY)) != NULL) {
4049 ipaddr.af = AF_INET6;
4050 ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
4051 ipaddr.prefix = 128;
4052 } else if ((vp = fr_pair_find_by_num(coa->proxy->vps, PW_HOME_SERVER_POOL, 0, TAG_ANY)) != NULL) {
4053 coa->home_pool = home_pool_byname(vp->vp_strvalue,
4055 if (!coa->home_pool) {
4056 RWDEBUG2("No such home_server_pool %s",
4062 * Prefer the pool to one server
4064 } else if (request->client->coa_pool) {
4065 coa->home_pool = request->client->coa_pool;
4067 } else if (request->client->coa_server) {
4068 coa->home_server = request->client->coa_server;
4072 * If all else fails, send it to the client that
4073 * originated this request.
4075 memcpy(&ipaddr, &request->packet->src_ipaddr, sizeof(ipaddr));
4079 * Use the pool, if it exists.
4081 if (coa->home_pool) {
4082 coa->home_server = home_server_ldb(NULL, coa->home_pool, coa);
4083 if (!coa->home_server) {
4084 RWDEBUG("No live home server for home_server_pool %s", coa->home_pool->name);
4087 home_server_update_request(coa->home_server, coa);
4089 } else if (!coa->home_server) {
4090 uint16_t port = PW_COA_UDP_PORT;
4092 vp = fr_pair_find_by_num(coa->proxy->vps, PW_PACKET_DST_PORT, 0, TAG_ANY);
4093 if (vp) port = vp->vp_integer;
4095 coa->home_server = home_server_find(&ipaddr, port, IPPROTO_UDP);
4096 if (!coa->home_server) {
4097 RWDEBUG2("Unknown destination %s:%d for CoA request.",
4098 inet_ntop(ipaddr.af, &ipaddr.ipaddr,
4099 buffer, sizeof(buffer)), port);
4104 vp = fr_pair_find_by_num(coa->proxy->vps, PW_PACKET_TYPE, 0, TAG_ANY);
4106 switch (vp->vp_integer) {
4107 case PW_CODE_COA_REQUEST:
4108 case PW_CODE_DISCONNECT_REQUEST:
4109 coa->proxy->code = vp->vp_integer;
4113 DEBUG("Cannot set CoA Packet-Type to code %d",
4119 if (!coa->proxy->code) coa->proxy->code = PW_CODE_COA_REQUEST;
4122 * The rest of the server code assumes that
4123 * request->packet && request->reply exist. Copy them
4124 * from the original request.
4126 rad_assert(coa->packet != NULL);
4127 rad_assert(coa->packet->vps == NULL);
4129 coa->packet = rad_copy_packet(coa, request->packet);
4130 coa->reply = rad_copy_packet(coa, request->reply);
4132 coa->config = fr_pair_list_copy(coa, request->config);
4133 coa->num_coa_requests = 0;
4134 coa->handle = null_handler;
4135 coa->number = request->number; /* it's associated with the same request */
4138 * Call the pre-proxy routines.
4140 vp = fr_pair_find_by_num(request->config, PW_PRE_PROXY_TYPE, 0, TAG_ANY);
4142 DICT_VALUE const *dval = dict_valbyattr(vp->da->attr, vp->da->vendor, vp->vp_integer);
4143 /* Must be a validation issue */
4145 RDEBUG2("Found Pre-Proxy-Type %s", dval->name);
4146 pre_proxy_type = vp->vp_integer;
4149 if (coa->home_pool && coa->home_pool->virtual_server) {
4150 char const *old_server = coa->server;
4152 coa->server = coa->home_pool->virtual_server;
4153 RDEBUG2("server %s {", coa->server);
4155 rcode = process_pre_proxy(pre_proxy_type, coa);
4158 coa->server = old_server;
4160 rcode = process_pre_proxy(pre_proxy_type, coa);
4167 * Only send the CoA packet if the pre-proxy code succeeded.
4169 case RLM_MODULE_NOOP:
4171 case RLM_MODULE_UPDATED:
4176 * Source IP / port is set when the proxy socket
4179 coa->proxy->dst_ipaddr = coa->home_server->ipaddr;
4180 coa->proxy->dst_port = coa->home_server->port;
4182 if (!insert_into_proxy_hash(coa)) {
4183 radlog_request(L_PROXY, 0, coa, "Failed to insert CoA request into proxy list");
4188 * We CANNOT divorce the CoA request from the parent
4189 * request. This function is running in a child thread,
4190 * and we need access to the main event loop in order to
4191 * to add the timers for the CoA packet.
4193 * Instead, we wait for the timer on the parent request
4196 gettimeofday(&coa->proxy->timestamp, NULL);
4197 coa->packet->timestamp = coa->proxy->timestamp; /* for max_request_time */
4198 coa->home_server->last_packet_sent = coa->proxy->timestamp.tv_sec;
4199 coa->delay = 0; /* need to calculate a new delay */
4202 * If requested, put a State attribute into the packet,
4203 * and cache the VPS.
4205 fr_state_put_vps(coa, NULL, coa->packet);
4208 * Encode the packet before we do anything else.
4210 coa->proxy_listener->encode(coa->proxy_listener, coa);
4211 debug_packet(coa, coa->proxy, false);
4213 #ifdef DEBUG_STATE_MACHINE
4214 if (rad_debug_lvl) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n", request->number, __FUNCTION__,
4215 child_state_names[request->child_state],
4216 child_state_names[REQUEST_PROXIED]);
4220 * Set the state function, then the state, no child, and
4223 coa->process = coa_wait_for_reply;
4224 coa->child_state = REQUEST_PROXIED;
4226 #ifdef HAVE_PTHREAD_H
4227 coa->child_pid = NO_SUCH_CHILD_PID;
4230 if (we_are_master()) coa_separate(request->coa);
4233 * And send the packet.
4235 coa->proxy_listener->send(coa->proxy_listener, coa);
4239 static void coa_retransmit(REQUEST *request)
4241 uint32_t delay, frac;
4242 struct timeval now, when, mrd;
4245 VERIFY_REQUEST(request);
4247 fr_event_now(el, &now);
4249 if (request->delay == 0) {
4251 * Implement re-transmit algorithm as per RFC 5080
4254 * We want IRT + RAND*IRT
4255 * or 0.9 IRT + rand(0,.2) IRT
4257 * 2^20 ~ USEC, and we want 2.
4258 * rand(0,0.2) USEC ~ (rand(0,2^21) / 10)
4260 delay = (fr_rand() & ((1 << 22) - 1)) / 10;
4261 request->delay = delay * request->home_server->coa_irt;
4262 delay = request->home_server->coa_irt * USEC;
4263 delay -= delay / 10;
4264 delay += request->delay;
4265 request->delay = delay;
4267 when = request->proxy->timestamp;
4268 tv_add(&when, delay);
4270 if (timercmp(&when, &now, >)) {
4271 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
4277 * Retransmit CoA request.
4281 * Cap count at MRC, if it is non-zero.
4283 if (request->home_server->coa_mrc &&
4284 (request->num_coa_requests >= request->home_server->coa_mrc)) {
4285 RERROR("Failing request - originate-coa ID %u, due to lack of any response from coa server %s port %d",
4287 inet_ntop(request->proxy->dst_ipaddr.af,
4288 &request->proxy->dst_ipaddr.ipaddr,
4289 buffer, sizeof(buffer)),
4290 request->proxy->dst_port);
4292 if (setup_post_proxy_fail(request)) {
4293 request_queue_or_run(request, coa_no_reply);
4295 request_done(request, FR_ACTION_DONE);
4301 * RFC 5080 Section 2.2.1
4303 * RT = 2*RTprev + RAND*RTprev
4304 * = 1.9 * RTprev + rand(0,.2) * RTprev
4305 * = 1.9 * RTprev + rand(0,1) * (RTprev / 5)
4308 delay ^= (delay >> 16);
4310 frac = request->delay / 5;
4311 delay = ((frac >> 16) * delay) + (((frac & 0xffff) * delay) >> 16);
4313 delay += (2 * request->delay) - (request->delay / 10);
4316 * Cap delay at MRT, if MRT is non-zero.
4318 if (request->home_server->coa_mrt &&
4319 (delay > (request->home_server->coa_mrt * USEC))) {
4320 int mrt_usec = request->home_server->coa_mrt * USEC;
4323 * delay = MRT + RAND * MRT
4324 * = 0.9 MRT + rand(0,.2) * MRT
4327 delay ^= (delay >> 15);
4329 delay = ((mrt_usec >> 16) * delay) + (((mrt_usec & 0xffff) * delay) >> 16);
4330 delay += mrt_usec - (mrt_usec / 10);
4333 request->delay = delay;
4335 tv_add(&when, request->delay);
4336 mrd = request->proxy->timestamp;
4337 mrd.tv_sec += request->home_server->coa_mrd;
4340 * Cap duration at MRD.
4342 if (timercmp(&mrd, &when, <)) {
4345 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
4347 request->num_coa_requests++; /* is NOT reset by code 3 lines above! */
4349 FR_STATS_TYPE_INC(request->home_server->stats.total_requests);
4351 RDEBUG2("Sending duplicate CoA request to home server %s port %d - ID: %d",
4352 inet_ntop(request->proxy->dst_ipaddr.af,
4353 &request->proxy->dst_ipaddr.ipaddr,
4354 buffer, sizeof(buffer)),
4355 request->proxy->dst_port,
4356 request->proxy->id);
4358 request->proxy_listener->send(request->proxy_listener,
4363 /** Wait for a reply after originating a CoA a request.
4365 * Retransmit the proxied packet, or time out and go to
4366 * coa_no_reply. Mark the home server unresponsive, etc.
4368 * If we do receive a reply, we transition to coa_running.
4371 * digraph coa_wait_for_reply {
4372 * coa_wait_for_reply;
4374 * coa_wait_for_reply -> coa_no_reply [ label = "TIMER >= response_window" ];
4375 * coa_wait_for_reply -> timer [ label = "TIMER < max_request_time" ];
4376 * coa_wait_for_reply -> coa_running [ label = "PROXY_REPLY" arrowhead = "none"];
4377 * coa_wait_for_reply -> done [ label = "TIMER >= max_request_time" ];
4381 static void coa_wait_for_reply(REQUEST *request, int action)
4383 VERIFY_REQUEST(request);
4385 TRACE_STATE_MACHINE;
4389 if (request->parent) coa_separate(request);
4392 case FR_ACTION_TIMER:
4393 if (request_max_time(request)) break;
4396 * Don't do fail-over. This is a 3.1 feature.
4398 if (!request->home_server ||
4399 (request->home_server->state == HOME_STATE_IS_DEAD) ||
4400 !request->proxy_listener ||
4401 (request->proxy_listener->status >= RAD_LISTEN_STATUS_EOL)) {
4402 request_done(request, FR_ACTION_DONE);
4406 coa_retransmit(request);
4409 case FR_ACTION_PROXY_REPLY:
4410 request_queue_or_run(request, coa_running);
4414 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
4419 static void coa_separate(REQUEST *request)
4421 VERIFY_REQUEST(request);
4422 #ifdef DEBUG_STATE_MACHINE
4423 int action = FR_ACTION_TIMER;
4426 TRACE_STATE_MACHINE;
4429 rad_assert(request->parent != NULL);
4430 rad_assert(request->parent->coa == request);
4431 rad_assert(request->ev == NULL);
4432 rad_assert(!request->in_request_hash);
4433 rad_assert(request->coa == NULL);
4435 rad_assert(request->proxy_reply || request->proxy_listener);
4437 (void) talloc_steal(NULL, request);
4438 request->parent->coa = NULL;
4439 request->parent = NULL;
4441 if (we_are_master()) {
4443 coa_retransmit(request);
4448 /** Process a request after the CoA has timed out.
4450 * Run the packet through Post-Proxy-Type Fail
4453 * digraph coa_no_reply {
4456 * coa_no_reply -> dup [ label = "DUP", arrowhead = "none" ];
4457 * coa_no_reply -> timer [ label = "TIMER < max_request_time" ];
4458 * coa_no_reply -> coa_reply_too_late [ label = "PROXY_REPLY" arrowhead = "none"];
4459 * coa_no_reply -> process_proxy_reply [ label = "RUN" ];
4460 * coa_no_reply -> done [ label = "TIMER >= timeout" ];
4464 static void coa_no_reply(REQUEST *request, int action)
4468 VERIFY_REQUEST(request);
4470 TRACE_STATE_MACHINE;
4474 case FR_ACTION_TIMER:
4475 (void) request_max_time(request);
4478 case FR_ACTION_PROXY_REPLY: /* too late! */
4479 RDEBUG2("Reply from CoA server %s port %d - ID: %d arrived too late.",
4480 inet_ntop(request->proxy->src_ipaddr.af,
4481 &request->proxy->src_ipaddr.ipaddr,
4482 buffer, sizeof(buffer)),
4483 request->proxy->dst_port, request->proxy->id);
4487 if (process_proxy_reply(request, NULL)) {
4488 request->handle(request);
4490 request_done(request, FR_ACTION_DONE);
4494 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
4500 /** Process the request after receiving a coa reply.
4502 * Throught the post-proxy section, and the through the handler
4506 * digraph coa_running {
4509 * coa_running -> timer [ label = "TIMER < max_request_time" ];
4510 * coa_running -> process_proxy_reply [ label = "RUN" ];
4511 * coa_running -> done [ label = "TIMER >= timeout" ];
4515 static void coa_running(REQUEST *request, int action)
4517 VERIFY_REQUEST(request);
4519 TRACE_STATE_MACHINE;
4523 case FR_ACTION_TIMER:
4524 (void) request_max_time(request);
4528 if (process_proxy_reply(request, request->proxy_reply)) {
4529 request->handle(request);
4531 request_done(request, FR_ACTION_DONE);
4535 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
4539 #endif /* WITH_COA */
4541 /***********************************************************************
4543 * End of the State machine. Start of additional helper code.
4545 ***********************************************************************/
4547 /***********************************************************************
4551 ***********************************************************************/
4552 static void event_socket_handler(fr_event_list_t *xel, UNUSED int fd, void *ctx)
4554 rad_listen_t *listener = talloc_get_type_abort(ctx, rad_listen_t);
4556 rad_assert(xel == el);
4558 if ((listener->fd < 0)
4560 #ifndef WITH_DETAIL_THREAD
4561 && (listener->type != RAD_LISTEN_DETAIL)
4567 listener->print(listener, buffer, sizeof(buffer));
4568 ERROR("FATAL: Asked to read from closed socket: %s",
4571 rad_panic("Socket was closed on us!");
4575 listener->recv(listener);
4579 #ifdef WITH_DETAIL_THREAD
4582 * This function is called periodically to see if this detail
4583 * file is available for reading.
4585 static void event_poll_detail(void *ctx)
4588 rad_listen_t *this = talloc_get_type_abort(ctx, rad_listen_t);
4589 struct timeval when, now;
4590 listen_detail_t *detail = this->data;
4592 rad_assert(this->type == RAD_LISTEN_DETAIL);
4595 event_socket_handler(el, this->fd, this);
4597 fr_event_now(el, &now);
4601 * Backdoor API to get the delay until the next poll
4604 delay = this->encode(this, NULL);
4605 if (delay == 0) goto redo;
4607 tv_add(&when, delay);
4610 if (!fr_event_insert(el, event_poll_detail, this,
4611 &when, &detail->ev)) {
4612 ERROR("Failed creating handler");
4616 #endif /* WITH_DETAIL_THREAD */
4617 #endif /* WITH_DETAIL */
4619 static void event_status(struct timeval *wake)
4621 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
4625 if (rad_debug_lvl == 0) {
4627 INFO("Ready to process requests");
4628 just_started = false;
4634 INFO("Ready to process requests");
4636 } else if ((wake->tv_sec != 0) ||
4637 (wake->tv_usec >= 100000)) {
4638 DEBUG("Waking up in %d.%01u seconds.",
4639 (int) wake->tv_sec, (unsigned int) wake->tv_usec / 100000);
4644 * FIXME: Put this somewhere else, where it isn't called
4645 * all of the time...
4648 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
4650 * If there are no child threads, then there may
4651 * be child processes. In that case, wait for
4652 * their exit status, and throw that exit status
4653 * away. This helps get rid of zxombie children.
4655 while (waitpid(-1, &argval, WNOHANG) > 0) {
4663 static void listener_free_cb(void *ctx)
4665 rad_listen_t *this = talloc_get_type_abort(ctx, rad_listen_t);
4668 if (this->count > 0) {
4669 struct timeval when;
4670 listen_socket_t *sock = this->data;
4672 fr_event_now(el, &when);
4676 if (!fr_event_insert(el, listener_free_cb, this, &when,
4678 rad_panic("Failed to insert event");
4685 * It's all free, close the socket.
4688 this->print(this, buffer, sizeof(buffer));
4689 DEBUG("... cleaning up socket %s", buffer);
4690 rad_assert(this->next == NULL);
4695 static int proxy_eol_cb(void *ctx, void *data)
4697 struct timeval when;
4698 REQUEST *request = fr_packet2myptr(REQUEST, proxy, data);
4700 if (request->proxy_listener != ctx) return 0;
4703 * We don't care if it's being processed in a child thread.
4706 #ifdef WITH_ACCOUNTING
4708 * Accounting packets should be deleted immediately.
4709 * They will never be retransmitted by the client.
4711 if (request->proxy->code == PW_CODE_ACCOUNTING_REQUEST) {
4712 RDEBUG("Stopping request due to failed connection to home server");
4713 request->master_state = REQUEST_STOP_PROCESSING;
4718 * Reset the timer to be now, so that the request is
4719 * quickly updated. But spread the requests randomly
4720 * over the next second, so that we don't overload the
4723 fr_event_now(el, &when);
4724 tv_add(&when, fr_rand() % USEC);
4725 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
4728 * Don't delete it from the list.
4732 #endif /* WITH_PROXY */
4733 #endif /* WITH_TCP */
4735 static int event_new_fd(rad_listen_t *this)
4741 if (this->status == RAD_LISTEN_STATUS_KNOWN) return 1;
4743 this->print(this, buffer, sizeof(buffer));
4745 if (this->status == RAD_LISTEN_STATUS_INIT) {
4746 listen_socket_t *sock = this->data;
4748 rad_assert(sock != NULL);
4750 DEBUG("Listening on %s", buffer);
4752 INFO(" ... adding new socket %s", buffer);
4756 if (!just_started && (this->type == RAD_LISTEN_PROXY)) {
4757 home_server_t *home;
4760 if (!home || !home->limit.max_connections) {
4761 INFO(" ... adding new socket %s", buffer);
4763 INFO(" ... adding new socket %s (%u of %u)", buffer,
4764 home->limit.num_connections, home->limit.max_connections);
4770 switch (this->type) {
4773 * Detail files are always known, and aren't
4774 * put into the socket event loop.
4776 case RAD_LISTEN_DETAIL:
4777 this->status = RAD_LISTEN_STATUS_KNOWN;
4779 #ifndef WITH_DETAIL_THREAD
4781 * Set up the first poll interval.
4783 event_poll_detail(this);
4786 break; /* add the FD to the list */
4788 #endif /* WITH_DETAIL */
4792 * Add it to the list of sockets we can use.
4793 * Server sockets (i.e. auth/acct) are never
4794 * added to the packet list.
4796 case RAD_LISTEN_PROXY:
4798 rad_assert((sock->proto == IPPROTO_UDP) || (sock->home != NULL));
4801 * Add timers to outgoing child sockets, if necessary.
4803 if (sock->proto == IPPROTO_TCP && sock->opened &&
4804 (sock->home->limit.lifetime || sock->home->limit.idle_timeout)) {
4805 struct timeval when;
4807 when.tv_sec = sock->opened + 1;
4811 if (!fr_event_insert(el, tcp_socket_timer, this, &when,
4813 rad_panic("Failed to insert event");
4816 #endif /* WITH_TCP */
4818 #endif /* WITH_PROXY */
4821 * FIXME: put idle timers on command sockets.
4827 * Add timers to incoming child sockets, if necessary.
4829 if (sock->proto == IPPROTO_TCP && sock->opened &&
4830 (sock->limit.lifetime || sock->limit.idle_timeout)) {
4831 struct timeval when;
4833 when.tv_sec = sock->opened + 1;
4837 if (!fr_event_insert(el, tcp_socket_timer, this, &when,
4839 ERROR("Failed adding timer for socket: %s", fr_strerror());
4843 #endif /* WITH_TCP */
4845 } /* switch over listener types */
4848 * All sockets: add the FD to the event handler.
4850 if (!fr_event_fd_insert(el, 0, this->fd,
4851 event_socket_handler, this)) {
4852 ERROR("Failed adding event handler for socket: %s", fr_strerror());
4856 this->status = RAD_LISTEN_STATUS_KNOWN;
4862 * The socket has reached a timeout. Try to close it.
4864 if (this->status == RAD_LISTEN_STATUS_FROZEN) {
4866 * Requests are still using the socket. Wait for
4869 if (this->count > 0) {
4870 struct timeval when;
4871 listen_socket_t *sock = this->data;
4874 * Try again to clean up the socket in 30
4877 gettimeofday(&when, NULL);
4881 if (!fr_event_insert(el,
4882 (fr_event_callback_t) event_new_fd,
4883 this, &when, &sock->ev)) {
4884 rad_panic("Failed to insert event");
4890 fr_event_fd_delete(el, 0, this->fd);
4891 this->status = RAD_LISTEN_STATUS_REMOVE_NOW;
4895 * The socket has had a catastrophic error. Close it.
4897 if (this->status == RAD_LISTEN_STATUS_EOL) {
4899 * Remove it from the list of live FD's.
4901 fr_event_fd_delete(el, 0, this->fd);
4905 * Tell all requests using this socket that the socket is dead.
4907 if (this->type == RAD_LISTEN_PROXY) {
4908 PTHREAD_MUTEX_LOCK(&proxy_mutex);
4909 if (!fr_packet_list_socket_freeze(proxy_list,
4911 ERROR("Fatal error freezing socket: %s", fr_strerror());
4915 if (this->count > 0) {
4916 fr_packet_list_walk(proxy_list, this, proxy_eol_cb);
4918 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
4920 #endif /* WITH_PROXY */
4923 * Requests are still using the socket. Wait for
4926 if (this->count > 0) {
4927 struct timeval when;
4928 listen_socket_t *sock = this->data;
4931 * Try again to clean up the socket in 30
4934 gettimeofday(&when, NULL);
4938 if (!fr_event_insert(el,
4939 (fr_event_callback_t) event_new_fd,
4940 this, &when, &sock->ev)) {
4941 rad_panic("Failed to insert event");
4948 * No one is using the socket. We can remove it now.
4950 this->status = RAD_LISTEN_STATUS_REMOVE_NOW;
4951 } /* socket is at EOL */
4952 #endif /* WITH_TCP */
4957 if (this->status == RAD_LISTEN_STATUS_REMOVE_NOW) {
4960 listen_socket_t *sock = this->data;
4961 struct timeval when;
4965 * Re-open the socket, pointing it to /dev/null.
4966 * This means that all writes proceed without
4967 * blocking, and all reads return "no data".
4969 * This leaves the socket active, so any child
4970 * threads won't go insane. But it means that
4971 * they cannot send or receive any packets.
4973 * This is EXTRA work in the normal case, when
4974 * sockets are closed without error. But it lets
4975 * us have one simple processing method for all
4978 devnull = open("/dev/null", O_RDWR);
4980 ERROR("FATAL failure opening /dev/null: %s",
4981 fr_syserror(errno));
4984 if (dup2(devnull, this->fd) < 0) {
4985 ERROR("FATAL failure closing socket: %s",
4986 fr_syserror(errno));
4992 rad_assert(this->type != RAD_LISTEN_DETAIL);
4998 * The socket is dead. Force all proxied packets
4999 * to stop using it. And then remove it from the
5000 * list of outgoing sockets.
5002 if (this->type == RAD_LISTEN_PROXY) {
5003 home_server_t *home;
5006 if (!home || !home->limit.max_connections) {
5007 INFO(" ... shutting down socket %s", buffer);
5009 INFO(" ... shutting down socket %s (%u of %u)", buffer,
5010 home->limit.num_connections, home->limit.max_connections);
5013 PTHREAD_MUTEX_LOCK(&proxy_mutex);
5014 fr_packet_list_walk(proxy_list, this, eol_proxy_listener);
5016 if (!fr_packet_list_socket_del(proxy_list, this->fd)) {
5017 ERROR("Fatal error removing socket %s: %s",
5018 buffer, fr_strerror());
5021 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
5023 #endif /* WITH_PROXY */
5025 INFO(" ... shutting down socket %s", buffer);
5028 * EOL all requests using this socket.
5030 rbtree_walk(pl, RBTREE_DELETE_ORDER, eol_listener, this);
5034 * No child threads, clean it up now.
5038 if (sock->ev) fr_event_delete(el, &sock->ev);
5044 * Wait until all requests using this socket are done.
5046 gettimeofday(&when, NULL);
5050 if (!fr_event_insert(el, listener_free_cb, this, &when,
5052 rad_panic("Failed to insert event");
5054 #endif /* WITH_TCP */
5060 /***********************************************************************
5064 ***********************************************************************/
5066 static void handle_signal_self(int flag)
5070 if ((flag & (RADIUS_SIGNAL_SELF_EXIT | RADIUS_SIGNAL_SELF_TERM)) != 0) {
5071 if ((flag & RADIUS_SIGNAL_SELF_EXIT) != 0) {
5072 INFO("Signalled to exit");
5073 fr_event_loop_exit(el, 1);
5075 INFO("Signalled to terminate");
5076 fr_event_loop_exit(el, 2);
5080 } /* else exit/term flags weren't set */
5083 * Tell the even loop to stop processing.
5085 if ((flag & RADIUS_SIGNAL_SELF_HUP) != 0) {
5087 static time_t last_hup = 0;
5090 if ((int) (when - last_hup) < 5) {
5091 INFO("Ignoring HUP (less than 5s since last one)");
5095 INFO("Received HUP signal");
5099 exec_trigger(NULL, NULL, "server.signal.hup", true);
5100 fr_event_loop_exit(el, 0x80);
5103 #if defined(WITH_DETAIL) && !defined(WITH_DETAIL_THREAD)
5104 if ((flag & RADIUS_SIGNAL_SELF_DETAIL) != 0) {
5108 * FIXME: O(N) loops suck.
5110 for (this = main_config.listen;
5112 this = this->next) {
5113 if (this->type != RAD_LISTEN_DETAIL) continue;
5116 * This one didn't send the signal, skip
5119 if (!this->decode(this, NULL)) continue;
5122 * Go service the interrupt.
5124 event_poll_detail(this);
5129 #if defined(WITH_TCP) && defined(WITH_PROXY) && defined(HAVE_PTHREAD_H)
5131 * There are new listeners in the list. Run
5132 * event_new_fd() on them.
5134 if ((flag & RADIUS_SIGNAL_SELF_NEW_FD) != 0) {
5135 rad_listen_t *this, *next;
5137 FD_MUTEX_LOCK(&fd_mutex);
5140 * FIXME: unlock the mutex before calling
5143 for (this = new_listeners; this != NULL; this = next) {
5150 new_listeners = NULL;
5151 FD_MUTEX_UNLOCK(&fd_mutex);
5156 #ifndef HAVE_PTHREAD_H
5157 void radius_signal_self(int flag)
5159 return handle_signal_self(flag);
5163 static int self_pipe[2] = { -1, -1 };
5166 * Inform ourselves that we received a signal.
5168 void radius_signal_self(int flag)
5174 * The read MUST be non-blocking for this to work.
5176 rcode = read(self_pipe[0], buffer, sizeof(buffer));
5180 for (i = 0; i < rcode; i++) {
5181 buffer[0] |= buffer[i];
5189 if (write(self_pipe[1], buffer, 1) < 0) fr_exit(0);
5193 static void event_signal_handler(UNUSED fr_event_list_t *xel,
5194 UNUSED int fd, UNUSED void *ctx)
5199 rcode = read(self_pipe[0], buffer, sizeof(buffer));
5200 if (rcode <= 0) return;
5203 * Merge pending signals.
5205 for (i = 0; i < rcode; i++) {
5206 buffer[0] |= buffer[i];
5209 handle_signal_self(buffer[0]);
5211 #endif /* HAVE_PTHREAD_H */
5213 /***********************************************************************
5215 * Bootstrapping code.
5217 ***********************************************************************/
5220 * Externally-visibly functions.
5222 int radius_event_init(TALLOC_CTX *ctx) {
5223 el = fr_event_list_create(ctx, event_status);
5229 static int packet_entry_cmp(void const *one, void const *two)
5231 RADIUS_PACKET const * const *a = one;
5232 RADIUS_PACKET const * const *b = two;
5234 return fr_packet_cmp(*a, *b);
5239 * They haven't defined a proxy listener. Automatically
5240 * add one for them, with the correct address family.
5242 static void create_default_proxy_listener(int af)
5246 listen_socket_t *sock;
5249 memset(&home, 0, sizeof(home));
5252 * Open a default UDP port
5254 home.proto = IPPROTO_UDP;
5258 * Set the address family.
5260 home.src_ipaddr.af = af;
5261 home.ipaddr.af = af;
5264 * Get the correct listener.
5266 this = proxy_new_listener(proxy_ctx, &home, port);
5272 if (!fr_packet_list_socket_add(proxy_list, this->fd,
5274 &sock->other_ipaddr, sock->other_port,
5276 ERROR("Failed adding proxy socket");
5281 * Insert the FD into list of FDs to listen on.
5283 radius_update_listener(this);
5287 * See if we automatically need to open a proxy socket.
5289 static void check_proxy(rad_listen_t *head)
5292 bool has_v4, has_v6;
5295 if (check_config) return;
5296 if (!main_config.proxy_requests) return;
5298 if (!home_servers_udp) return;
5301 * We passed "-i" on the command line. Use that address
5302 * family for the proxy socket.
5304 if (main_config.myip.af != AF_UNSPEC) {
5305 create_default_proxy_listener(main_config.myip.af);
5309 defined_proxy = has_v4 = has_v6 = false;
5312 * Figure out if we need to open a proxy socket, and if
5315 for (this = head; this != NULL; this = this->next) {
5316 listen_socket_t *sock;
5318 switch (this->type) {
5319 case RAD_LISTEN_PROXY:
5320 defined_proxy = true;
5323 case RAD_LISTEN_AUTH:
5325 case RAD_LISTEN_ACCT:
5328 case RAD_LISTEN_COA:
5331 if (sock->my_ipaddr.af == AF_INET) has_v4 = true;
5332 if (sock->my_ipaddr.af == AF_INET6) has_v6 = true;
5341 * Assume they know what they're doing.
5343 if (defined_proxy) return;
5345 if (has_v4) create_default_proxy_listener(AF_INET);
5347 if (has_v6) create_default_proxy_listener(AF_INET6);
5351 int radius_event_start(CONF_SECTION *cs, bool have_children)
5353 rad_listen_t *head = NULL;
5355 if (fr_start_time != (time_t)-1) return 0;
5357 time(&fr_start_time);
5359 if (!check_config) {
5361 * radius_event_init() must be called first
5365 pl = rbtree_create(NULL, packet_entry_cmp, NULL, 0);
5366 if (!pl) return 0; /* leak el */
5369 request_num_counter = 0;
5372 if (main_config.proxy_requests && !check_config) {
5374 * Create the tree for managing proxied requests and
5377 proxy_list = fr_packet_list_create(1);
5378 if (!proxy_list) return 0;
5380 #ifdef HAVE_PTHREAD_H
5381 if (pthread_mutex_init(&proxy_mutex, NULL) != 0) {
5382 ERROR("FATAL: Failed to initialize proxy mutex: %s",
5383 fr_syserror(errno));
5389 * The "init_delay" is set to "response_window".
5390 * Reset it to half of "response_window" in order
5391 * to give the event loop enough time to service
5392 * the event before hitting "response_window".
5394 main_config.init_delay.tv_usec += (main_config.init_delay.tv_sec & 0x01) * USEC;
5395 main_config.init_delay.tv_usec >>= 1;
5396 main_config.init_delay.tv_sec >>= 1;
5398 proxy_ctx = talloc_init("proxy");
5403 * Move all of the thread calls to this file?
5405 * It may be best for the mutexes to be in this file...
5407 spawn_flag = have_children;
5409 #ifdef HAVE_PTHREAD_H
5410 NO_SUCH_CHILD_PID = pthread_self(); /* not a child thread */
5413 * Initialize the threads ONLY if we're spawning, AND
5414 * we're running normally.
5416 if (have_children && !check_config &&
5417 (thread_pool_init(cs, &spawn_flag) < 0)) {
5423 DEBUG("%s: #### Skipping IP addresses and Ports ####",
5425 if (listen_init(cs, &head, spawn_flag) < 0) {
5432 #ifdef HAVE_PTHREAD_H
5434 * Child threads need a pipe to signal us, as do the
5437 if (pipe(self_pipe) < 0) {
5438 ERROR("Error opening internal pipe: %s", fr_syserror(errno));
5441 if ((fcntl(self_pipe[0], F_SETFL, O_NONBLOCK) < 0) ||
5442 (fcntl(self_pipe[0], F_SETFD, FD_CLOEXEC) < 0)) {
5443 ERROR("Error setting internal flags: %s", fr_syserror(errno));
5446 if ((fcntl(self_pipe[1], F_SETFL, O_NONBLOCK) < 0) ||
5447 (fcntl(self_pipe[1], F_SETFD, FD_CLOEXEC) < 0)) {
5448 ERROR("Error setting internal flags: %s", fr_syserror(errno));
5451 DEBUG4("Created signal pipe. Read end FD %i, write end FD %i", self_pipe[0], self_pipe[1]);
5453 if (!fr_event_fd_insert(el, 0, self_pipe[0], event_signal_handler, el)) {
5454 ERROR("Failed creating signal pipe handler: %s", fr_strerror());
5459 DEBUG("%s: #### Opening IP addresses and Ports ####", main_config.name);
5462 * The server temporarily switches to an unprivileged
5463 * user very early in the bootstrapping process.
5464 * However, some sockets MAY require privileged access
5465 * (bind to device, or to port < 1024, or to raw
5466 * sockets). Those sockets need to call suid up/down
5467 * themselves around the functions that need a privileged
5470 if (listen_init(cs, &head, spawn_flag) < 0) {
5474 main_config.listen = head;
5481 * At this point, no one has any business *ever* going
5484 rad_suid_down_permanent();
5491 static int proxy_delete_cb(UNUSED void *ctx, void *data)
5493 REQUEST *request = fr_packet2myptr(REQUEST, proxy, data);
5495 VERIFY_REQUEST(request);
5497 request->master_state = REQUEST_STOP_PROCESSING;
5499 #ifdef HAVE_PTHREAD_H
5500 if (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0) return 0;
5504 * If it's queued we can't delete it from the queue.
5506 * Otherwise, it's OK to delete it. Even RUNNING, because
5507 * that will get caught by the check above.
5509 if (request->child_state == REQUEST_QUEUED) return 0;
5511 request->in_proxy_hash = false;
5513 if (!request->in_request_hash) {
5514 request_done(request, FR_ACTION_DONE);
5518 * Delete it from the list.
5525 static int request_delete_cb(UNUSED void *ctx, void *data)
5527 REQUEST *request = fr_packet2myptr(REQUEST, packet, data);
5529 VERIFY_REQUEST(request);
5531 request->master_state = REQUEST_STOP_PROCESSING;
5534 * Not done, or the child thread is still processing it.
5536 if (request->child_state < REQUEST_RESPONSE_DELAY) return 0; /* continue */
5538 #ifdef HAVE_PTHREAD_H
5539 if (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0) return 0;
5543 rad_assert(request->in_proxy_hash == false);
5546 request->in_request_hash = false;
5548 if (request->ev) fr_event_delete(el, &request->ev);
5550 if (main_config.memory_report) {
5551 RDEBUG2("Cleaning up request packet ID %u with timestamp +%d",
5552 request->packet->id,
5553 (unsigned int) (request->timestamp - fr_start_time));
5558 rad_assert(!request->coa->in_proxy_hash);
5562 request_free(request);
5565 * Delete it from the list, and continue;
5571 void radius_event_free(void)
5577 * There are requests in the proxy hash that aren't
5578 * referenced from anywhere else. Remove them first.
5581 fr_packet_list_walk(proxy_list, NULL, proxy_delete_cb);
5585 rbtree_walk(pl, RBTREE_DELETE_ORDER, request_delete_cb, NULL);
5589 * Now that all requests have been marked "please stop",
5590 * ensure that all of the threads have exited.
5592 #ifdef HAVE_PTHREAD_H
5597 * Walk the lists again, ensuring that all
5598 * requests are done.
5600 if (main_config.memory_report) {
5605 fr_packet_list_walk(proxy_list, NULL, proxy_delete_cb);
5606 num = fr_packet_list_num_elements(proxy_list);
5608 ERROR("Proxy list has %d requests still in it.", num);
5613 rbtree_walk(pl, RBTREE_DELETE_ORDER, request_delete_cb, NULL);
5614 num = rbtree_num_elements(pl);
5616 ERROR("Request list has %d requests still in it.", num);
5625 fr_packet_list_free(proxy_list);
5628 if (proxy_ctx) talloc_free(proxy_ctx);
5633 if (debug_condition) talloc_free(debug_condition);
5636 int radius_event_process(void)
5640 return fr_event_loop(el);