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
288 static int event_new_fd(rad_listen_t *this);
291 * We need mutexes around the event FD list *only* in certain
294 #if defined (HAVE_PTHREAD_H) && (defined(WITH_PROXY) || defined(WITH_TCP))
295 static rad_listen_t *new_listeners = NULL;
297 static pthread_mutex_t fd_mutex;
298 # define FD_MUTEX_LOCK if (spawn_flag) pthread_mutex_lock
299 # define FD_MUTEX_UNLOCK if (spawn_flag) pthread_mutex_unlock
301 void radius_update_listener(rad_listen_t *this)
304 * Just do it ourselves.
306 if (we_are_master()) {
311 FD_MUTEX_LOCK(&fd_mutex);
314 * If it's already in the list, don't add it again.
317 FD_MUTEX_UNLOCK(&fd_mutex);
322 * Otherwise, add it to the list
324 this->next = new_listeners;
325 new_listeners = this;
326 FD_MUTEX_UNLOCK(&fd_mutex);
327 radius_signal_self(RADIUS_SIGNAL_SELF_NEW_FD);
330 void radius_update_listener(rad_listen_t *this)
333 * No threads. Just insert it.
338 * This is easier than ifdef's throughout the code.
340 # define FD_MUTEX_LOCK(_x)
341 # define FD_MUTEX_UNLOCK(_x)
344 static int request_num_counter = 1;
346 static int request_will_proxy(REQUEST *request) CC_HINT(nonnull);
347 static int request_proxy(REQUEST *request, int retransmit) CC_HINT(nonnull);
348 STATE_MACHINE_DECL(request_ping) CC_HINT(nonnull);
350 STATE_MACHINE_DECL(request_response_delay) CC_HINT(nonnull);
351 STATE_MACHINE_DECL(request_cleanup_delay) CC_HINT(nonnull);
352 STATE_MACHINE_DECL(request_running) CC_HINT(nonnull);
353 STATE_MACHINE_DECL(request_done) CC_HINT(nonnull);
355 STATE_MACHINE_DECL(proxy_no_reply) CC_HINT(nonnull);
356 STATE_MACHINE_DECL(proxy_running) CC_HINT(nonnull);
357 STATE_MACHINE_DECL(proxy_wait_for_reply) CC_HINT(nonnull);
359 static int process_proxy_reply(REQUEST *request, RADIUS_PACKET *reply) CC_HINT(nonnull (1));
360 static void remove_from_proxy_hash(REQUEST *request) CC_HINT(nonnull);
361 static void remove_from_proxy_hash_nl(REQUEST *request, bool yank) CC_HINT(nonnull);
362 static int insert_into_proxy_hash(REQUEST *request) CC_HINT(nonnull);
365 static REQUEST *request_setup(TALLOC_CTX *ctx, rad_listen_t *listener, RADIUS_PACKET *packet,
366 RADCLIENT *client, RAD_REQUEST_FUNP fun);
367 static int request_pre_handler(REQUEST *request, UNUSED int action) CC_HINT(nonnull);
370 static void request_coa_originate(REQUEST *request) CC_HINT(nonnull);
371 STATE_MACHINE_DECL(coa_wait_for_reply) CC_HINT(nonnull);
372 STATE_MACHINE_DECL(coa_no_reply) CC_HINT(nonnull);
373 STATE_MACHINE_DECL(coa_running) CC_HINT(nonnull);
374 static void coa_separate(REQUEST *request) CC_HINT(nonnull);
375 # define COA_SEPARATE if (request->coa) coa_separate(request->coa);
377 # define COA_SEPARATE
380 #define CHECK_FOR_STOP do { if (request->master_state == REQUEST_STOP_PROCESSING) {request_done(request, FR_ACTION_DONE);return;}} while (0)
383 #define USEC (1000000)
385 #define INSERT_EVENT(_function, _ctx) if (!fr_event_insert(el, _function, _ctx, &((_ctx)->when), &((_ctx)->ev))) { _rad_panic(__FILE__, __LINE__, "Failed to insert event"); }
387 static void tv_add(struct timeval *tv, int usec_delay)
389 if (usec_delay >= USEC) {
390 tv->tv_sec += usec_delay / USEC;
393 tv->tv_usec += usec_delay;
395 if (tv->tv_usec >= USEC) {
396 tv->tv_sec += tv->tv_usec / USEC;
402 * Debug the packet if requested.
404 static void debug_packet(REQUEST *request, RADIUS_PACKET *packet, bool received)
406 char src_ipaddr[128];
407 char dst_ipaddr[128];
410 if (!RDEBUG_ENABLED) return;
413 * Client-specific debugging re-prints the input
414 * packet into the client log.
416 * This really belongs in a utility library
418 if (is_radius_code(packet->code)) {
419 RDEBUG("%s %s Id %i from %s%s%s:%i to %s%s%s:%i length %zu",
420 received ? "Received" : "Sent",
421 fr_packet_codes[packet->code],
423 packet->src_ipaddr.af == AF_INET6 ? "[" : "",
424 inet_ntop(packet->src_ipaddr.af,
425 &packet->src_ipaddr.ipaddr,
426 src_ipaddr, sizeof(src_ipaddr)),
427 packet->src_ipaddr.af == AF_INET6 ? "]" : "",
429 packet->dst_ipaddr.af == AF_INET6 ? "[" : "",
430 inet_ntop(packet->dst_ipaddr.af,
431 &packet->dst_ipaddr.ipaddr,
432 dst_ipaddr, sizeof(dst_ipaddr)),
433 packet->dst_ipaddr.af == AF_INET6 ? "]" : "",
437 RDEBUG("%s code %u Id %i from %s%s%s:%i to %s%s%s:%i length %zu\n",
438 received ? "Received" : "Sent",
441 packet->src_ipaddr.af == AF_INET6 ? "[" : "",
442 inet_ntop(packet->src_ipaddr.af,
443 &packet->src_ipaddr.ipaddr,
444 src_ipaddr, sizeof(src_ipaddr)),
445 packet->src_ipaddr.af == AF_INET6 ? "]" : "",
447 packet->dst_ipaddr.af == AF_INET6 ? "[" : "",
448 inet_ntop(packet->dst_ipaddr.af,
449 &packet->dst_ipaddr.ipaddr,
450 dst_ipaddr, sizeof(dst_ipaddr)),
451 packet->dst_ipaddr.af == AF_INET6 ? "]" : "",
457 rdebug_pair_list(L_DBG_LVL_1, request, packet->vps, NULL);
459 rdebug_proto_pair_list(L_DBG_LVL_1, request, packet->vps);
464 /***********************************************************************
466 * Start of RADIUS server state machine.
468 ***********************************************************************/
470 static struct timeval *request_response_window(REQUEST *request)
472 VERIFY_REQUEST(request);
474 if (request->client) {
476 * The client hasn't set the response window. Return
477 * either the home server one, if set, or the global one.
479 if (!timerisset(&request->client->response_window)) {
480 return &request->home_server->response_window;
483 if (timercmp(&request->client->response_window,
484 &request->home_server->response_window, <)) {
485 return &request->client->response_window;
489 rad_assert(request->home_server != NULL);
490 return &request->home_server->response_window;
494 * Determine initial request processing delay.
496 static int request_init_delay(REQUEST *request)
498 struct timeval half_response_window;
500 VERIFY_REQUEST(request);
502 /* Allow client response window to lower initial delay */
503 if (timerisset(&request->client->response_window)) {
504 half_response_window.tv_sec = request->client->response_window.tv_sec >> 1;
505 half_response_window.tv_usec =
506 ((request->client->response_window.tv_sec & 1) * USEC +
507 request->client->response_window.tv_usec) >> 1;
508 if (timercmp(&half_response_window, &request->root->init_delay, <))
509 return (int)half_response_window.tv_sec * USEC +
510 (int)half_response_window.tv_usec;
513 return (int)request->root->init_delay.tv_sec * USEC +
514 (int)request->root->init_delay.tv_usec;
518 * Callback for ALL timer events related to the request.
520 static void request_timer(void *ctx)
522 REQUEST *request = talloc_get_type_abort(ctx, REQUEST);
525 action = request->timer_action;
529 request->process(request, action);
533 * Wrapper for talloc pools. If there's no parent, just free the
534 * request. If there is a parent, free the parent INSTEAD of the
537 static void request_free(REQUEST *request)
541 rad_assert(request->ev == NULL);
542 rad_assert(!request->in_request_hash);
543 rad_assert(!request->in_proxy_hash);
545 if ((request->options & RAD_REQUEST_OPTION_CTX) == 0) {
546 talloc_free(request);
550 ptr = talloc_parent(request);
551 rad_assert(ptr != NULL);
557 static void proxy_reply_too_late(REQUEST *request)
561 RDEBUG2("Reply from home server %s port %d - ID: %d arrived too late. Try increasing 'retry_delay' or 'max_request_time'",
562 inet_ntop(request->proxy->dst_ipaddr.af,
563 &request->proxy->dst_ipaddr.ipaddr,
564 buffer, sizeof(buffer)),
565 request->proxy->dst_port, request->proxy->id);
570 /** Mark a request DONE and clean it up.
572 * When a request is DONE, it can have ties to a number of other
573 * portions of the server. The request hash, proxy hash, events,
574 * child threads, etc. This function takes care of either cleaning
575 * up the request, or managing the timers to wait for the ties to be
580 * done -> done [ label = "still running" ];
584 static void request_done(REQUEST *request, int action)
586 struct timeval now, when;
588 VERIFY_REQUEST(request);
593 * Force this no matter what.
595 request->process = request_done;
599 * Tell the detail listener that we're done.
601 if (request->listener &&
602 (request->listener->type == RAD_LISTEN_DETAIL) &&
603 (request->simul_max != 1)) {
604 request->simul_max = 1;
605 request->listener->send(request->listener,
610 #ifdef HAVE_PTHREAD_H
612 * If called from a child thread, mark ourselves as done,
613 * and wait for the master thread timer to clean us up.
615 if (!we_are_master()) {
617 request->module = "<REQUEST_DONE>";
618 request->child_state = REQUEST_DONE;
624 * Mark the request as STOP.
626 request->master_state = REQUEST_STOP_PROCESSING;
630 * Move the CoA request to its own handler.
633 coa_separate(request->coa);
634 } else if (request->parent && (request->parent->coa == request)) {
635 coa_separate(request);
640 * It doesn't hurt to send duplicate replies. All other
641 * signals are ignored, as the request will be cleaned up
647 rad_assert(request->listener != NULL);
649 if (request->reply->code != 0) {
650 request->listener->send(request->listener, request);
653 RDEBUG("No reply. Ignoring retransmit");
658 * Mark the request as done.
661 #ifdef HAVE_PTHREAD_H
663 * If the child is still running, leave it alone.
665 if (spawn_flag && (request->child_state <= REQUEST_RUNNING)) {
670 #ifdef DEBUG_STATE_MACHINE
671 if (rad_debug_lvl) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n",
672 request->number, __FUNCTION__,
673 child_state_names[request->child_state],
674 child_state_names[REQUEST_DONE]);
676 request->child_state = REQUEST_DONE;
680 * Called when the child is taking too long to
681 * finish. We've already marked it "please
682 * stop", so we don't complain any more.
684 case FR_ACTION_TIMER:
688 case FR_ACTION_PROXY_REPLY:
689 proxy_reply_too_late(request);
698 * Remove it from the request hash.
700 if (request->in_request_hash) {
701 if (!rbtree_deletebydata(pl, &request->packet)) {
704 request->in_request_hash = false;
709 * Wait for the proxy ID to expire. This allows us to
710 * avoid re-use of proxy IDs for a while.
712 if (request->in_proxy_hash) {
713 rad_assert(request->proxy != NULL);
715 fr_event_now(el, &now);
716 when = request->proxy->timestamp;
719 if (((request->proxy->code == PW_CODE_COA_REQUEST) ||
720 (request->proxy->code == PW_CODE_DISCONNECT_REQUEST)) &&
721 (request->packet->code != request->proxy->code)) {
722 when.tv_sec += request->home_server->coa_mrd;
725 timeradd(&when, request_response_window(request), &when);
728 * We haven't received all responses, AND there's still
729 * time to wait. Do so.
731 if ((request->num_proxied_requests > request->num_proxied_responses) &&
733 (request->home_server->proto != IPPROTO_TCP) &&
735 timercmp(&now, &when, <)) {
736 RDEBUG("Waiting for more responses from the home server");
743 remove_from_proxy_hash(request);
747 #ifdef HAVE_PTHREAD_H
749 * If there's no children, we can mark the request as done.
751 if (!spawn_flag) request->child_state = REQUEST_DONE;
755 * If the child is still running, wait for it to be finished.
757 if (request->child_state <= REQUEST_RUNNING) {
758 gettimeofday(&now, NULL);
763 if (request->delay < (USEC / 3)) request->delay = USEC / 3;
764 tv_add(&when, request->delay);
765 request->delay += request->delay >> 1;
766 if (request->delay > (10 * USEC)) request->delay = 10 * USEC;
768 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
772 #ifdef HAVE_PTHREAD_H
773 rad_assert(request->child_pid == NO_SUCH_CHILD_PID);
777 * @todo: do final states for TCP sockets, too?
779 request_stats_final(request);
781 if (request->listener) {
782 request->listener->count--;
785 * If we're the last one, remove the listener now.
787 if ((request->listener->count == 0) &&
788 (request->listener->status >= RAD_LISTEN_STATUS_FROZEN)) {
789 event_new_fd(request->listener);
794 if (request->packet) {
795 RDEBUG2("Cleaning up request packet ID %u with timestamp +%d",
797 (unsigned int) (request->timestamp - fr_start_time));
798 } /* else don't print anything */
801 fr_event_delete(el, &request->ev);
802 request_free(request);
806 static void request_cleanup_delay_init(REQUEST *request)
808 struct timeval now, when;
810 VERIFY_REQUEST(request);
813 * Do cleanup delay ONLY for RADIUS packets from a real
814 * client. Everything else just gets cleaned up
817 if (request->packet->dst_port == 0) goto done;
820 * Accounting packets shouldn't be retransmitted. They
821 * should always be updated with Acct-Delay-Time.
823 #ifdef WITH_ACCOUNTING
824 if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) goto done;
828 if (request->listener->type == RAD_LISTEN_DHCP) goto done;
832 if (request->listener->type == RAD_LISTEN_VQP) goto done;
835 if (!request->root->cleanup_delay) goto done;
837 gettimeofday(&now, NULL);
839 rad_assert(request->reply->timestamp.tv_sec != 0);
840 when = request->reply->timestamp;
842 request->delay = request->root->cleanup_delay;
843 when.tv_sec += request->delay;
846 * Set timer for when we need to clean it up.
848 if (timercmp(&when, &now, >)) {
849 #ifdef DEBUG_STATE_MACHINE
850 if (rad_debug_lvl) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_cleanup_delay");
852 request->process = request_cleanup_delay;
854 if (!we_are_master()) {
856 request->module = "<REQUEST_CLEANUP_DELAY>";
857 request->child_state = REQUEST_CLEANUP_DELAY;
862 * Update this if we can, otherwise let the timers pick it up.
864 request->child_state = REQUEST_CLEANUP_DELAY;
865 #ifdef HAVE_PTHREAD_H
866 rad_assert(request->child_pid == NO_SUCH_CHILD_PID);
868 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
873 * Otherwise just clean it up.
876 request_done(request, FR_ACTION_DONE);
881 * Enforce max_request_time.
883 static bool request_max_time(REQUEST *request)
885 struct timeval now, when;
886 rad_assert(request->magic == REQUEST_MAGIC);
887 #ifdef DEBUG_STATE_MACHINE
888 int action = FR_ACTION_TIMER;
891 VERIFY_REQUEST(request);
897 * The child thread has acknowledged it's done.
898 * Transition to the DONE state.
900 * If the request was marked STOP, then the "check for
901 * stop" macro already took care of it.
903 if (request->child_state == REQUEST_DONE) {
905 request_done(request, FR_ACTION_DONE);
910 * The request is still running. Enforce max_request_time.
912 fr_event_now(el, &now);
913 when = request->packet->timestamp;
914 when.tv_sec += request->root->max_request_time;
917 * Taking too long: tell it to die.
919 if (timercmp(&now, &when, >=)) {
920 #ifdef HAVE_PTHREAD_H
922 * If there's a child thread processing it,
926 (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0)) {
927 ERROR("Unresponsive child for request %u, in component %s module %s",
929 request->component ? request->component : "<core>",
930 request->module ? request->module : "<core>");
931 exec_trigger(request, NULL, "server.thread.unresponsive", true);
935 * Tell the request that it's done.
941 * Sleep for some more. We HOPE that the child will
942 * become responsive at some point in the future. We do
943 * this by adding 50% to the current timer.
946 tv_add(&when, request->delay);
947 request->delay += request->delay >> 1;
948 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
952 static void request_queue_or_run(REQUEST *request,
953 fr_request_process_t process)
955 #ifdef DEBUG_STATE_MACHINE
956 int action = FR_ACTION_TIMER;
959 VERIFY_REQUEST(request);
964 * Do this here so that fewer other functions need to do
967 if (request->master_state == REQUEST_STOP_PROCESSING) {
968 #ifdef DEBUG_STATE_MACHINE
969 if (rad_debug_lvl) printf("(%u) ********\tSTATE %s M-%s causes C-%s-> C-%s\t********\n",
970 request->number, __FUNCTION__,
971 master_state_names[request->master_state],
972 child_state_names[request->child_state],
973 child_state_names[REQUEST_DONE]);
975 request_done(request, FR_ACTION_DONE);
979 request->process = process;
981 if (we_are_master()) {
985 * (re) set the initial delay.
987 request->delay = request_init_delay(request);
988 if (request->delay > USEC) request->delay = USEC;
989 gettimeofday(&when, NULL);
990 tv_add(&when, request->delay);
991 request->delay += request->delay >> 1;
993 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
995 #ifdef HAVE_PTHREAD_H
998 * A child thread will eventually pick it up.
1000 if (request_enqueue(request)) return;
1003 * Otherwise we're not going to do anything with
1006 request_done(request, FR_ACTION_DONE);
1012 request->child_state = REQUEST_RUNNING;
1013 request->process(request, FR_ACTION_RUN);
1017 * Requests that care about child process exit
1018 * codes have already either called
1019 * rad_waitpid(), or they've given up.
1021 while (waitpid(-1, NULL, WNOHANG) > 0);
1026 static void request_dup(REQUEST *request)
1028 ERROR("(%u) Ignoring duplicate packet from "
1029 "client %s port %d - ID: %u due to unfinished request "
1030 "in component %s module %s",
1031 request->number, request->client->shortname,
1032 request->packet->src_port,request->packet->id,
1033 request->component, request->module);
1037 /** Sit on a request until it's time to clean it up.
1039 * A NAS may not see a response from the server. When the NAS
1040 * retransmits, we want to be able to send a cached reply back. The
1041 * alternative is to re-process the packet, which does bad things for
1042 * EAP, among others.
1044 * IF we do see a NAS retransmit, we extend the cleanup delay,
1045 * because the NAS might miss our cached reply.
1047 * Otherwise, once we reach cleanup_delay, we transition to DONE.
1050 * digraph cleanup_delay {
1052 * send_reply [ label = "send_reply\nincrease cleanup delay" ];
1054 * cleanup_delay -> send_reply [ label = "DUP" ];
1055 * send_reply -> cleanup_delay;
1056 * cleanup_delay -> proxy_reply_too_late [ label = "PROXY_REPLY", arrowhead = "none" ];
1057 * cleanup_delay -> cleanup_delay [ label = "TIMER < timeout" ];
1058 * cleanup_delay -> done [ label = "TIMER >= timeout" ];
1062 static void request_cleanup_delay(REQUEST *request, int action)
1064 struct timeval when, now;
1066 VERIFY_REQUEST(request);
1068 TRACE_STATE_MACHINE;
1075 if (request->reply->code != 0) {
1076 request->listener->send(request->listener, request);
1078 RDEBUG("No reply. Ignoring retransmit");
1082 * Double the cleanup_delay to catch retransmits.
1084 when = request->reply->timestamp;
1085 request->delay += request->delay;
1086 when.tv_sec += request->delay;
1088 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
1092 case FR_ACTION_PROXY_REPLY:
1093 proxy_reply_too_late(request);
1097 case FR_ACTION_TIMER:
1098 fr_event_now(el, &now);
1100 rad_assert(request->root->cleanup_delay > 0);
1102 when = request->reply->timestamp;
1103 when.tv_sec += request->root->cleanup_delay;
1105 if (timercmp(&when, &now, >)) {
1106 #ifdef DEBUG_STATE_MACHINE
1107 if (rad_debug_lvl) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_cleanup_delay");
1109 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
1111 } /* else it's time to clean up */
1113 request_done(request, REQUEST_DONE);
1117 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
1123 /** Sit on a request until it's time to respond to it.
1125 * For security reasons, rejects (and maybe some other) packets are
1126 * delayed for a while before we respond. This delay means that
1127 * badly behaved NASes don't hammer the server with authentication
1130 * Otherwise, once we reach response_delay, we send the reply, and
1131 * transition to cleanup_delay.
1134 * digraph response_delay {
1135 * response_delay -> proxy_reply_too_late [ label = "PROXY_REPLY", arrowhead = "none" ];
1136 * response_delay -> response_delay [ label = "DUP, TIMER < timeout" ];
1137 * response_delay -> send_reply [ label = "TIMER >= timeout" ];
1138 * send_reply -> cleanup_delay;
1142 static void request_response_delay(REQUEST *request, int action)
1144 struct timeval when, now;
1146 VERIFY_REQUEST(request);
1148 TRACE_STATE_MACHINE;
1155 ERROR("(%u) Discarding duplicate request from "
1156 "client %s port %d - ID: %u due to delayed response",
1157 request->number, request->client->shortname,
1158 request->packet->src_port,request->packet->id);
1162 case FR_ACTION_PROXY_REPLY:
1163 proxy_reply_too_late(request);
1167 case FR_ACTION_TIMER:
1168 fr_event_now(el, &now);
1171 * See if it's time to send the reply. If not,
1172 * we wait some more.
1174 when = request->reply->timestamp;
1176 tv_add(&when, request->response_delay.tv_sec * USEC);
1177 tv_add(&when, request->response_delay.tv_usec);
1179 if (timercmp(&when, &now, >)) {
1180 #ifdef DEBUG_STATE_MACHINE
1181 if (rad_debug_lvl) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_response_delay");
1183 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
1185 } /* else it's time to send the reject */
1187 RDEBUG2("Sending delayed response");
1188 debug_packet(request, request->reply, false);
1189 request->listener->send(request->listener, request);
1192 * Clean up the request.
1194 request_cleanup_delay_init(request);
1198 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
1204 static int request_pre_handler(REQUEST *request, UNUSED int action)
1208 VERIFY_REQUEST(request);
1210 TRACE_STATE_MACHINE;
1212 if (request->master_state == REQUEST_STOP_PROCESSING) return 0;
1215 * Don't decode the packet if it's an internal "fake"
1216 * request. Instead, just return so that the caller can
1219 if (request->packet->dst_port == 0) {
1220 request->username = fr_pair_find_by_num(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1221 request->password = fr_pair_find_by_num(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
1225 if (!request->packet->vps) { /* FIXME: check for correct state */
1226 rcode = request->listener->decode(request->listener, request);
1229 if (debug_condition) {
1231 * Ignore parse errors.
1233 if (radius_evaluate_cond(request, RLM_MODULE_OK, 0, debug_condition)) {
1234 request->log.lvl = L_DBG_LVL_2;
1235 request->log.func = vradlog_request;
1240 debug_packet(request, request->packet, true);
1246 RATE_LIMIT(INFO("Dropping packet without response because of error: %s", fr_strerror()));
1247 request->reply->offset = -2; /* bad authenticator */
1251 if (!request->username) {
1252 request->username = fr_pair_find_by_num(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1259 /** Do the final processing of a request before we reply to the NAS.
1261 * Various cleanups, suppress responses, copy Proxy-State, and set
1262 * response_delay or cleanup_delay;
1264 static void request_finish(REQUEST *request, int action)
1268 VERIFY_REQUEST(request);
1270 TRACE_STATE_MACHINE;
1273 (void) action; /* -Wunused */
1277 * Don't do post-auth if we're a CoA request originated
1278 * from an Access-Request. See request_alloc_coa() for
1281 if ((request->options & RAD_REQUEST_OPTION_COA) != 0) goto done;
1285 * Override the response code if a control:Response-Packet-Type attribute is present.
1287 vp = fr_pair_find_by_num(request->config, PW_RESPONSE_PACKET_TYPE, 0, TAG_ANY);
1289 if (vp->vp_integer == 256) {
1290 RDEBUG2("Not responding to request");
1291 request->reply->code = 0;
1293 request->reply->code = vp->vp_integer;
1297 * Catch Auth-Type := Reject BEFORE proxying the packet.
1299 else if (request->packet->code == PW_CODE_ACCESS_REQUEST) {
1300 if (request->reply->code == 0) {
1301 vp = fr_pair_find_by_num(request->config, PW_AUTH_TYPE, 0, TAG_ANY);
1302 if (!vp || (vp->vp_integer != 5)) {
1303 RDEBUG2("There was no response configured: "
1304 "rejecting request");
1307 request->reply->code = PW_CODE_ACCESS_REJECT;
1312 * Copy Proxy-State from the request to the reply.
1314 vp = fr_pair_list_copy_by_num(request->reply, request->packet->vps,
1315 PW_PROXY_STATE, 0, TAG_ANY);
1316 if (vp) fr_pair_add(&request->reply->vps, vp);
1319 * Call Post-Auth for Access-Request packets.
1321 if (request->packet->code == PW_CODE_ACCESS_REQUEST) {
1322 rad_postauth(request);
1327 * Maybe originate a CoA request.
1329 if ((action == FR_ACTION_RUN) && !request->proxy && request->coa) {
1330 request_coa_originate(request);
1335 * Clean up. These are no longer needed.
1337 gettimeofday(&request->reply->timestamp, NULL);
1340 * Fake packets get marked as "done", and have the
1341 * proxy-reply section deal with the reply attributes.
1342 * We therefore don't free the reply attributes.
1344 if (request->packet->dst_port == 0) {
1345 RDEBUG("Finished internally proxied request.");
1347 request->module = "<REQUEST_DONE>";
1348 request->child_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->component = "<core>";
1468 request->module = "<done>";
1470 request_cleanup_delay_init(request);
1474 * Encode and sign it here, so that the master
1475 * thread can just send the encoded data, which
1476 * means it does less work.
1478 RDEBUG2("Delaying response for %d.%06d seconds",
1479 (int) request->response_delay.tv_sec, (int) request->response_delay.tv_usec);
1480 request->listener->encode(request->listener, request);
1481 request->component = "<core>";
1482 request->module = "<delay>";
1483 request->process = request_response_delay;
1485 request->module = "<REQUEST_RESPONSE_DELAY>";
1486 request->child_state = REQUEST_RESPONSE_DELAY;
1490 /** Process a request from a client.
1492 * The outcome might be that the request is proxied.
1496 * running -> running [ label = "TIMER < max_request_time" ];
1497 * running -> done [ label = "TIMER >= max_request_time" ];
1498 * running -> proxy [ label = "proxied" ];
1499 * running -> dup [ label = "DUP", arrowhead = "none" ];
1503 static void request_running(REQUEST *request, int action)
1505 VERIFY_REQUEST(request);
1507 TRACE_STATE_MACHINE;
1511 case FR_ACTION_TIMER:
1513 (void) request_max_time(request);
1517 request_dup(request);
1521 if (!request_pre_handler(request, action)) {
1522 #ifdef DEBUG_STATE_MACHINE
1523 if (rad_debug_lvl) printf("(%u) ********\tSTATE %s failed in pre-handler C-%s -> C-%s\t********\n",
1524 request->number, __FUNCTION__,
1525 child_state_names[request->child_state],
1526 child_state_names[REQUEST_DONE]);
1530 request->module = "<REQUEST_DONE>";
1531 request->child_state = REQUEST_DONE;
1535 rad_assert(request->handle != NULL);
1536 request->handle(request);
1540 * We may need to send a proxied request.
1542 if ((action == FR_ACTION_RUN) &&
1543 request_will_proxy(request)) {
1544 #ifdef DEBUG_STATE_MACHINE
1545 if (rad_debug_lvl) printf("(%u) ********\tWill Proxy\t********\n", request->number);
1549 * takes care of setting
1550 * up the post proxy fail
1553 if (request_proxy(request, 0) < 0) goto req_finished;
1557 #ifdef DEBUG_STATE_MACHINE
1558 if (rad_debug_lvl) printf("(%u) ********\tFinished\t********\n", request->number);
1564 request_finish(request, action);
1569 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
1574 int request_receive(TALLOC_CTX *ctx, rad_listen_t *listener, RADIUS_PACKET *packet,
1575 RADCLIENT *client, RAD_REQUEST_FUNP fun)
1578 RADIUS_PACKET **packet_p;
1579 REQUEST *request = NULL;
1581 listen_socket_t *sock = NULL;
1583 VERIFY_PACKET(packet);
1586 * Set the last packet received.
1588 gettimeofday(&now, NULL);
1590 packet->timestamp = now;
1592 #ifdef WITH_ACCOUNTING
1593 if (listener->type != RAD_LISTEN_DETAIL)
1596 sock = listener->data;
1597 sock->last_packet = now.tv_sec;
1600 packet->proto = sock->proto;
1605 * Skip everything if required.
1607 if (listener->nodup) goto skip_dup;
1609 packet_p = rbtree_finddata(pl, &packet);
1611 rad_child_state_t child_state;
1613 request = fr_packet2myptr(REQUEST, packet, packet_p);
1614 rad_assert(request->in_request_hash);
1615 child_state = request->child_state;
1618 * Same src/dst ip/port, length, and
1619 * authentication vector: must be a duplicate.
1621 if ((request->packet->data_len == packet->data_len) &&
1622 (memcmp(request->packet->vector, packet->vector,
1623 sizeof(packet->vector)) == 0)) {
1626 switch (packet->code) {
1627 case PW_CODE_ACCESS_REQUEST:
1628 FR_STATS_INC(auth, total_dup_requests);
1631 #ifdef WITH_ACCOUNTING
1632 case PW_CODE_ACCOUNTING_REQUEST:
1633 FR_STATS_INC(acct, total_dup_requests);
1637 case PW_CODE_COA_REQUEST:
1638 FR_STATS_INC(coa, total_dup_requests);
1641 case PW_CODE_DISCONNECT_REQUEST:
1642 FR_STATS_INC(dsc, total_dup_requests);
1649 #endif /* WITH_STATS */
1652 * Tell the state machine that there's a
1653 * duplicate request.
1655 request->process(request, FR_ACTION_DUP);
1656 return 0; /* duplicate of live request */
1660 * Mark the request as done ASAP, and before we
1661 * log anything. The child may stop processing
1662 * the request just as we're logging the
1665 request_done(request, FR_ACTION_DONE);
1669 * It's a new request, not a duplicate. If the
1670 * old one is done, then we can clean it up.
1672 if (child_state <= REQUEST_RUNNING) {
1674 * The request is still QUEUED or RUNNING. That's a problem.
1676 ERROR("Received conflicting packet from "
1677 "client %s port %d - ID: %u due to "
1678 "unfinished request. Giving up on old request.",
1680 packet->src_port, packet->id);
1684 * Mark the old request as done. If there's no
1685 * child, the request will be cleaned up
1686 * immediately. If there is a child, we'll set a
1687 * timer to go clean up the request.
1689 } /* else the new packet is unique */
1692 * Quench maximum number of outstanding requests.
1694 if (main_config.max_requests &&
1695 ((count = rbtree_num_elements(pl)) > main_config.max_requests)) {
1696 RATE_LIMIT(ERROR("Dropping request (%d is too many): from client %s port %d - ID: %d", count,
1698 packet->src_port, packet->id);
1699 WARN("Please check the configuration file.\n"
1700 "\tThe value for 'max_requests' is probably set too low.\n"));
1702 exec_trigger(NULL, NULL, "server.max_requests", true);
1708 * Rate-limit the incoming packets
1710 if (sock && sock->max_rate) {
1713 pps = rad_pps(&sock->rate_pps_old, &sock->rate_pps_now, &sock->rate_time, &now);
1714 if (pps > sock->max_rate) {
1715 DEBUG("Dropping request due to rate limiting");
1718 sock->rate_pps_now++;
1722 * Allocate a pool for the request.
1725 ctx = talloc_pool(NULL, main_config.talloc_pool_size);
1727 talloc_set_name_const(ctx, "request_receive_pool");
1730 * The packet is still allocated from a different
1731 * context, but oh well.
1733 (void) talloc_steal(ctx, packet);
1736 request = request_setup(ctx, listener, packet, client, fun);
1743 * Mark it as a "real" request with a context.
1745 request->options |= RAD_REQUEST_OPTION_CTX;
1748 * Remember the request in the list.
1750 if (!listener->nodup) {
1751 if (!rbtree_insert(pl, &request->packet)) {
1752 RERROR("Failed to insert request in the list of live requests: discarding it");
1753 request_done(request, FR_ACTION_DONE);
1757 request->in_request_hash = true;
1761 * Process it. Send a response, and free it.
1763 if (listener->synchronous) {
1765 rad_assert(listener->type != RAD_LISTEN_DETAIL);
1768 request->listener->decode(request->listener, request);
1769 request->username = fr_pair_find_by_num(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1770 request->password = fr_pair_find_by_num(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
1774 if (request->reply->code != 0) {
1775 request->listener->send(request->listener, request);
1777 RDEBUG("Not sending reply");
1781 * Don't do delayed reject. Oh well.
1783 request_free(request);
1788 * Otherwise, insert it into the state machine.
1789 * The child threads will take care of processing it.
1791 request_queue_or_run(request, request_running);
1797 static REQUEST *request_setup(TALLOC_CTX *ctx, rad_listen_t *listener, RADIUS_PACKET *packet,
1798 RADCLIENT *client, RAD_REQUEST_FUNP fun)
1803 * Create and initialize the new request.
1805 request = request_alloc(ctx);
1810 request->reply = rad_alloc(request, false);
1811 if (!request->reply) {
1813 talloc_free(request);
1817 request->listener = listener;
1818 request->client = client;
1819 request->packet = talloc_steal(request, packet);
1820 request->number = request_num_counter++;
1821 request->priority = listener->type;
1822 request->master_state = REQUEST_ACTIVE;
1823 request->child_state = REQUEST_RUNNING;
1824 #ifdef DEBUG_STATE_MACHINE
1825 if (rad_debug_lvl) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n",
1826 request->number, __FUNCTION__,
1827 child_state_names[request->child_state],
1828 child_state_names[REQUEST_RUNNING]);
1830 request->handle = fun;
1834 request->listener->stats.last_packet = request->packet->timestamp.tv_sec;
1835 if (packet->code == PW_CODE_ACCESS_REQUEST) {
1836 request->client->auth.last_packet = request->packet->timestamp.tv_sec;
1837 radius_auth_stats.last_packet = request->packet->timestamp.tv_sec;
1838 #ifdef WITH_ACCOUNTING
1839 } else if (packet->code == PW_CODE_ACCOUNTING_REQUEST) {
1840 request->client->acct.last_packet = request->packet->timestamp.tv_sec;
1841 radius_acct_stats.last_packet = request->packet->timestamp.tv_sec;
1844 #endif /* WITH_STATS */
1847 * Status-Server packets go to the head of the queue.
1849 if (request->packet->code == PW_CODE_STATUS_SERVER) request->priority = 0;
1852 * Set virtual server identity
1854 if (client->server) {
1855 request->server = client->server;
1856 } else if (listener->server) {
1857 request->server = listener->server;
1859 request->server = NULL;
1862 request->root = &main_config;
1864 request->listener->count++;
1868 * The request passes many of our sanity checks.
1869 * From here on in, if anything goes wrong, we
1870 * send a reject message, instead of dropping the
1875 * Build the reply template from the request.
1878 request->reply->sockfd = request->packet->sockfd;
1879 request->reply->dst_ipaddr = request->packet->src_ipaddr;
1880 request->reply->src_ipaddr = request->packet->dst_ipaddr;
1881 request->reply->dst_port = request->packet->src_port;
1882 request->reply->src_port = request->packet->dst_port;
1883 request->reply->id = request->packet->id;
1884 request->reply->code = 0; /* UNKNOWN code */
1885 memcpy(request->reply->vector, request->packet->vector,
1886 sizeof(request->reply->vector));
1887 request->reply->vps = NULL;
1888 request->reply->data = NULL;
1889 request->reply->data_len = 0;
1895 /***********************************************************************
1899 ***********************************************************************/
1902 * Timer function for all TCP sockets.
1904 static void tcp_socket_timer(void *ctx)
1906 rad_listen_t *listener = talloc_get_type_abort(ctx, rad_listen_t);
1907 listen_socket_t *sock = listener->data;
1908 struct timeval end, now;
1910 fr_socket_limit_t *limit;
1914 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return;
1916 fr_event_now(el, &now);
1918 switch (listener->type) {
1920 case RAD_LISTEN_PROXY:
1921 limit = &sock->home->limit;
1925 case RAD_LISTEN_AUTH:
1926 #ifdef WITH_ACCOUNTING
1927 case RAD_LISTEN_ACCT:
1929 limit = &sock->limit;
1937 * If we enforce a lifetime, do it now.
1939 if (limit->lifetime > 0) {
1940 end.tv_sec = sock->opened + limit->lifetime;
1943 if (timercmp(&end, &now, <=)) {
1944 listener->print(listener, buffer, sizeof(buffer));
1945 DEBUG("Reached maximum lifetime on socket %s", buffer);
1951 * Proxy sockets get frozen, so that we don't use
1952 * them for new requests. But we do keep them
1953 * open to listen for replies to requests we had
1956 if (listener->type == RAD_LISTEN_PROXY) {
1957 PTHREAD_MUTEX_LOCK(&proxy_mutex);
1958 if (!fr_packet_list_socket_freeze(proxy_list,
1960 ERROR("Fatal error freezing socket: %s", fr_strerror());
1963 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
1968 * Mark the socket as "don't use if at all possible".
1970 listener->status = RAD_LISTEN_STATUS_FROZEN;
1971 event_new_fd(listener);
1980 * Enforce an idle timeout.
1982 if (limit->idle_timeout > 0) {
1983 struct timeval idle;
1985 rad_assert(sock->last_packet != 0);
1986 idle.tv_sec = sock->last_packet + limit->idle_timeout;
1989 if (timercmp(&idle, &now, <=)) {
1990 listener->print(listener, buffer, sizeof(buffer));
1991 DEBUG("Reached idle timeout on socket %s", buffer);
1996 * Enforce the minimum of idle timeout or lifetime.
1998 if (timercmp(&idle, &end, <)) {
2004 * Wake up at t + 0.5s. The code above checks if the timers
2005 * are <= t. This addition gives us a bit of leeway.
2007 end.tv_usec = USEC / 2;
2010 if (!fr_event_insert(el, tcp_socket_timer, listener, &end, &sock->ev)) {
2011 rad_panic("Failed to insert event");
2018 * Add +/- 2s of jitter, as suggested in RFC 3539
2021 static void add_jitter(struct timeval *when)
2028 jitter ^= (jitter >> 10);
2029 jitter &= ((1 << 22) - 1); /* 22 bits of 1 */
2032 * Add in ~ (4 * USEC) of jitter.
2034 tv_add(when, jitter);
2038 * Called by socket_del to remove requests with this socket
2040 static int eol_proxy_listener(void *ctx, void *data)
2042 rad_listen_t *this = talloc_get_type_abort(ctx, rad_listen_t);
2043 RADIUS_PACKET **proxy_p = data;
2046 request = fr_packet2myptr(REQUEST, proxy, proxy_p);
2047 if (request->proxy_listener != this) return 0;
2050 * The normal "remove_from_proxy_hash" tries to grab the
2051 * proxy mutex. We already have it held, so grabbing it
2052 * again will cause a deadlock. Instead, call the "no
2053 * lock" version of the function.
2055 rad_assert(request->in_proxy_hash == true);
2056 remove_from_proxy_hash_nl(request, false);
2059 * Don't mark it as DONE. The client can retransmit, and
2060 * the packet SHOULD be re-proxied somewhere else.
2062 * Return "2" means that the rbtree code will remove it
2063 * from the tree, and we don't need to do it ourselves.
2067 #endif /* WITH_PROXY */
2069 static int eol_listener(void *ctx, void *data)
2071 rad_listen_t *this = talloc_get_type_abort(ctx, rad_listen_t);
2072 RADIUS_PACKET **packet_p = data;
2075 request = fr_packet2myptr(REQUEST, packet, packet_p);
2076 if (request->listener != this) return 0;
2078 request->master_state = REQUEST_STOP_PROCESSING;
2079 request->process = request_done;
2083 #endif /* WITH_TCP */
2086 /***********************************************************************
2088 * Proxy handlers for the state machine.
2090 ***********************************************************************/
2093 * Called with the proxy mutex held
2095 static void remove_from_proxy_hash_nl(REQUEST *request, bool yank)
2097 VERIFY_REQUEST(request);
2099 if (!request->in_proxy_hash) return;
2101 fr_packet_list_id_free(proxy_list, request->proxy, yank);
2102 request->in_proxy_hash = false;
2105 * On the FIRST reply, decrement the count of outstanding
2106 * requests. Note that this is NOT the count of sent
2107 * packets, but whether or not the home server has
2110 if (request->home_server &&
2111 request->home_server->currently_outstanding) {
2112 request->home_server->currently_outstanding--;
2115 * If we're NOT sending it packets, AND it's been
2116 * a while since we got a response, then we don't
2117 * know if it's alive or dead.
2119 if ((request->home_server->currently_outstanding == 0) &&
2120 (request->home_server->state == HOME_STATE_ALIVE)) {
2121 struct timeval when, now;
2123 when.tv_sec = request->home_server->last_packet_recv ;
2126 timeradd(&when, request_response_window(request), &when);
2127 gettimeofday(&now, NULL);
2130 * last_packet + response_window
2132 * We *administratively* mark the home
2133 * server as "unknown" state, because we
2134 * haven't seen a packet for a while.
2136 if (timercmp(&now, &when, >)) {
2137 request->home_server->state = HOME_STATE_UNKNOWN;
2138 request->home_server->last_packet_sent = 0;
2139 request->home_server->last_packet_recv = 0;
2145 rad_assert(request->proxy_listener != NULL);
2146 request->proxy_listener->count--;
2148 request->proxy_listener = NULL;
2151 * Got from YES in hash, to NO, not in hash while we hold
2152 * the mutex. This guarantees that when another thread
2153 * grabs the mutex, the "not in hash" flag is correct.
2157 static void remove_from_proxy_hash(REQUEST *request)
2159 VERIFY_REQUEST(request);
2162 * Check this without grabbing the mutex because it's a
2163 * lot faster that way.
2165 if (!request->in_proxy_hash) return;
2168 * The "not in hash" flag is definitive. However, if the
2169 * flag says that it IS in the hash, there might still be
2170 * a race condition where it isn't.
2172 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2174 if (!request->in_proxy_hash) {
2175 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2179 remove_from_proxy_hash_nl(request, true);
2181 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2184 static int insert_into_proxy_hash(REQUEST *request)
2188 bool success = false;
2189 void *proxy_listener;
2191 VERIFY_REQUEST(request);
2193 rad_assert(request->proxy != NULL);
2194 rad_assert(request->home_server != NULL);
2195 rad_assert(proxy_list != NULL);
2198 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2199 proxy_listener = NULL;
2200 request->num_proxied_requests = 1;
2201 request->num_proxied_responses = 0;
2203 for (tries = 0; tries < 2; tries++) {
2205 listen_socket_t *sock;
2207 RDEBUG3("proxy: Trying to allocate ID (%d/2)", tries);
2208 success = fr_packet_list_id_alloc(proxy_list,
2209 request->home_server->proto,
2210 &request->proxy, &proxy_listener);
2213 if (tries > 0) continue; /* try opening new socket only once */
2215 #ifdef HAVE_PTHREAD_H
2216 if (proxy_no_new_sockets) break;
2219 RDEBUG3("proxy: Trying to open a new listener to the home server");
2220 this = proxy_new_listener(proxy_ctx, request->home_server, 0);
2222 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2226 request->proxy->src_port = 0; /* Use any new socket */
2227 proxy_listener = this;
2230 if (!fr_packet_list_socket_add(proxy_list, this->fd,
2232 &sock->other_ipaddr, sock->other_port,
2235 #ifdef HAVE_PTHREAD_H
2236 proxy_no_new_sockets = true;
2238 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2241 * This is bad. However, the
2242 * packet list now supports 256
2243 * open sockets, which should
2244 * minimize this problem.
2246 ERROR("Failed adding proxy socket: %s",
2252 * Add it to the event loop. Ensure that we have
2253 * only one mutex locked at a time.
2255 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2256 radius_update_listener(this);
2257 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2260 if (!proxy_listener || !success) {
2261 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2262 REDEBUG2("proxy: Failed allocating Id for proxied request");
2264 request->proxy_listener = NULL;
2265 request->in_proxy_hash = false;
2269 rad_assert(request->proxy->id >= 0);
2271 request->proxy_listener = proxy_listener;
2272 request->in_proxy_hash = true;
2273 RDEBUG3("proxy: request is now in proxy hash");
2276 * Keep track of maximum outstanding requests to a
2277 * particular home server. 'max_outstanding' is
2278 * enforced in home_server_ldb(), in realms.c.
2280 request->home_server->currently_outstanding++;
2283 request->proxy_listener->count++;
2286 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2288 RDEBUG3("proxy: allocating destination %s port %d - Id %d",
2289 inet_ntop(request->proxy->dst_ipaddr.af,
2290 &request->proxy->dst_ipaddr.ipaddr, buf, sizeof(buf)),
2291 request->proxy->dst_port,
2292 request->proxy->id);
2297 static int process_proxy_reply(REQUEST *request, RADIUS_PACKET *reply)
2300 int post_proxy_type = 0;
2303 VERIFY_REQUEST(request);
2306 * There may be a proxy reply, but it may be too late.
2308 if (!request->home_server->server && !request->proxy_listener) return 0;
2311 * Delete any reply we had accumulated until now.
2313 RDEBUG2("Clearing existing &reply: attributes");
2314 fr_pair_list_free(&request->reply->vps);
2317 * Run the packet through the post-proxy stage,
2318 * BEFORE playing games with the attributes.
2320 vp = fr_pair_find_by_num(request->config, PW_POST_PROXY_TYPE, 0, TAG_ANY);
2322 post_proxy_type = vp->vp_integer;
2324 * If we have a proxy_reply, and it was a reject, or a NAK
2325 * setup Post-Proxy <type>.
2327 * If the <type> doesn't have a section, then the Post-Proxy
2328 * section is ignored.
2331 DICT_VALUE *dval = NULL;
2333 switch (reply->code) {
2334 case PW_CODE_ACCESS_REJECT:
2335 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Reject");
2336 if (dval) post_proxy_type = dval->value;
2339 case PW_CODE_DISCONNECT_NAK:
2340 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, fr_packet_codes[reply->code]);
2341 if (dval) post_proxy_type = dval->value;
2344 case PW_CODE_COA_NAK:
2345 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, fr_packet_codes[reply->code]);
2346 if (dval) post_proxy_type = dval->value;
2354 * Create config:Post-Proxy-Type
2357 vp = radius_pair_create(request, &request->config, PW_POST_PROXY_TYPE, 0);
2358 vp->vp_integer = dval->value;
2362 if (post_proxy_type > 0) RDEBUG2("Found Post-Proxy-Type %s",
2363 dict_valnamebyattr(PW_POST_PROXY_TYPE, 0, post_proxy_type));
2366 VERIFY_PACKET(reply);
2369 * Decode the packet if required.
2371 if (request->proxy_listener) {
2372 rcode = request->proxy_listener->decode(request->proxy_listener, request);
2373 debug_packet(request, reply, true);
2376 * Pro-actively remove it from the proxy hash.
2377 * This is later than in 2.1.x, but it means that
2378 * the replies are authenticated before being
2379 * removed from the hash.
2382 (request->num_proxied_requests <= request->num_proxied_responses)) {
2383 remove_from_proxy_hash(request);
2386 rad_assert(!request->in_proxy_hash);
2388 } else if (request->in_proxy_hash) {
2389 remove_from_proxy_hash(request);
2392 if (request->home_pool && request->home_pool->virtual_server) {
2393 char const *old_server = request->server;
2395 request->server = request->home_pool->virtual_server;
2396 RDEBUG2("server %s {", request->server);
2398 rcode = process_post_proxy(post_proxy_type, request);
2401 request->server = old_server;
2403 rcode = process_post_proxy(post_proxy_type, request);
2407 if (request->packet->code == request->proxy->code)
2409 * Don't run the next bit if we originated a CoA
2410 * packet, after receiving an Access-Request or
2411 * Accounting-Request.
2416 * There may NOT be a proxy reply, as we may be
2417 * running Post-Proxy-Type = Fail.
2420 fr_pair_add(&request->reply->vps, fr_pair_list_copy(request->reply, reply->vps));
2423 * Delete the Proxy-State Attributes from
2424 * the reply. These include Proxy-State
2425 * attributes from us and remote server.
2427 fr_pair_delete_by_num(&request->reply->vps, PW_PROXY_STATE, 0, TAG_ANY);
2431 default: /* Don't do anything */
2433 case RLM_MODULE_FAIL:
2436 case RLM_MODULE_HANDLED:
2443 static void mark_home_server_alive(REQUEST *request, home_server_t *home)
2447 home->state = HOME_STATE_ALIVE;
2448 home->response_timeouts = 0;
2449 exec_trigger(request, home->cs, "home_server.alive", false);
2450 home->currently_outstanding = 0;
2451 home->num_sent_pings = 0;
2452 home->num_received_pings = 0;
2453 gettimeofday(&home->revive_time, NULL);
2455 fr_event_delete(el, &home->ev);
2457 RPROXY("Marking home server %s port %d alive",
2458 inet_ntop(request->proxy->dst_ipaddr.af,
2459 &request->proxy->dst_ipaddr.ipaddr,
2460 buffer, sizeof(buffer)),
2461 request->proxy->dst_port);
2465 int request_proxy_reply(RADIUS_PACKET *packet)
2467 RADIUS_PACKET **proxy_p;
2472 VERIFY_PACKET(packet);
2474 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2475 proxy_p = fr_packet_list_find_byreply(proxy_list, packet);
2478 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2479 PROXY("No outstanding request was found for %s packet from host %s port %d - ID %u",
2480 fr_packet_codes[packet->code],
2481 inet_ntop(packet->src_ipaddr.af,
2482 &packet->src_ipaddr.ipaddr,
2483 buffer, sizeof(buffer)),
2484 packet->src_port, packet->id);
2488 request = fr_packet2myptr(REQUEST, proxy, proxy_p);
2489 request->num_proxied_responses++; /* needs to be protected by lock */
2491 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2494 * No reply, BUT the current packet fails verification:
2495 * ignore it. This does the MD5 calculations in the
2496 * server core, but I guess we can fix that later.
2498 if (!request->proxy_reply &&
2499 (rad_verify(packet, request->proxy,
2500 request->home_server->secret) != 0)) {
2501 DEBUG("Ignoring spoofed proxy reply. Signature is invalid");
2506 * The home server sent us a packet which doesn't match
2507 * something we have: ignore it. This is done only to
2508 * catch the case of broken systems.
2510 if (request->proxy_reply &&
2511 (memcmp(request->proxy_reply->vector,
2513 sizeof(request->proxy_reply->vector)) != 0)) {
2514 RDEBUG2("Ignoring conflicting proxy reply");
2518 gettimeofday(&now, NULL);
2521 * Status-Server packets don't count as real packets.
2523 if (request->proxy->code != PW_CODE_STATUS_SERVER) {
2524 listen_socket_t *sock = request->proxy_listener->data;
2526 request->home_server->last_packet_recv = now.tv_sec;
2527 sock->last_packet = now.tv_sec;
2531 * If we have previously seen a reply, ignore the
2534 if (request->proxy_reply) {
2535 RDEBUG2("Discarding duplicate reply from host %s port %d - ID: %d",
2536 inet_ntop(packet->src_ipaddr.af,
2537 &packet->src_ipaddr.ipaddr,
2538 buffer, sizeof(buffer)),
2539 packet->src_port, packet->id);
2544 * Call the state machine to do something useful with the
2547 request->proxy_reply = talloc_steal(request, packet);
2548 packet->timestamp = now;
2549 request->priority = RAD_LISTEN_PROXY;
2553 * Update the proxy listener stats here, because only one
2554 * thread accesses that at a time. The home_server and
2555 * main proxy_*_stats structures are updated once the
2556 * request is cleaned up.
2558 request->proxy_listener->stats.total_responses++;
2560 request->home_server->stats.last_packet = packet->timestamp.tv_sec;
2561 request->proxy_listener->stats.last_packet = packet->timestamp.tv_sec;
2563 switch (request->proxy->code) {
2564 case PW_CODE_ACCESS_REQUEST:
2565 proxy_auth_stats.last_packet = packet->timestamp.tv_sec;
2567 if (request->proxy_reply->code == PW_CODE_ACCESS_ACCEPT) {
2568 request->proxy_listener->stats.total_access_accepts++;
2570 } else if (request->proxy_reply->code == PW_CODE_ACCESS_REJECT) {
2571 request->proxy_listener->stats.total_access_rejects++;
2573 } else if (request->proxy_reply->code == PW_CODE_ACCESS_CHALLENGE) {
2574 request->proxy_listener->stats.total_access_challenges++;
2578 #ifdef WITH_ACCOUNTING
2579 case PW_CODE_ACCOUNTING_REQUEST:
2580 request->proxy_listener->stats.total_responses++;
2581 proxy_acct_stats.last_packet = packet->timestamp.tv_sec;
2587 case PW_CODE_COA_REQUEST:
2588 request->proxy_listener->stats.total_responses++;
2589 proxy_coa_stats.last_packet = packet->timestamp.tv_sec;
2592 case PW_CODE_DISCONNECT_REQUEST:
2593 request->proxy_listener->stats.total_responses++;
2594 proxy_dsc_stats.last_packet = packet->timestamp.tv_sec;
2604 * If we hadn't been sending the home server packets for
2605 * a while, just mark it alive. Or, if it was zombie,
2606 * it's now responded, and is therefore alive.
2608 if ((request->home_server->state == HOME_STATE_UNKNOWN) ||
2609 (request->home_server->state == HOME_STATE_ZOMBIE)) {
2610 mark_home_server_alive(request, request->home_server);
2614 * Tell the request state machine that we have a proxy
2615 * reply. Depending on the function, this should either
2616 * ignore it, or process it.
2618 request->process(request, FR_ACTION_PROXY_REPLY);
2624 static int setup_post_proxy_fail(REQUEST *request)
2626 DICT_VALUE const *dval = NULL;
2629 VERIFY_REQUEST(request);
2631 if (request->proxy->code == PW_CODE_ACCESS_REQUEST) {
2632 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0,
2633 "Fail-Authentication");
2634 #ifdef WITH_ACCOUNTING
2635 } else if (request->proxy->code == PW_CODE_ACCOUNTING_REQUEST) {
2636 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0,
2641 } else if (request->proxy->code == PW_CODE_COA_REQUEST) {
2642 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-CoA");
2644 } else if (request->proxy->code == PW_CODE_DISCONNECT_REQUEST) {
2645 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-Disconnect");
2648 WARN("Unknown packet type in Post-Proxy-Type Fail: ignoring");
2652 if (!dval) dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail");
2655 fr_pair_delete_by_num(&request->config, PW_POST_PROXY_TYPE, 0, TAG_ANY);
2659 vp = fr_pair_find_by_num(request->config, PW_POST_PROXY_TYPE, 0, TAG_ANY);
2660 if (!vp) vp = radius_pair_create(request, &request->config,
2661 PW_POST_PROXY_TYPE, 0);
2662 vp->vp_integer = dval->value;
2668 /** Process a request after the proxy has timed out.
2670 * Run the packet through Post-Proxy-Type Fail
2673 * digraph proxy_no_reply {
2676 * proxy_no_reply -> dup [ label = "DUP", arrowhead = "none" ];
2677 * proxy_no_reply -> timer [ label = "TIMER < max_request_time" ];
2678 * proxy_no_reply -> proxy_reply_too_late [ label = "PROXY_REPLY" arrowhead = "none"];
2679 * proxy_no_reply -> process_proxy_reply [ label = "RUN" ];
2680 * proxy_no_reply -> done [ label = "TIMER >= timeout" ];
2684 static void proxy_no_reply(REQUEST *request, int action)
2686 VERIFY_REQUEST(request);
2688 TRACE_STATE_MACHINE;
2693 request_dup(request);
2696 case FR_ACTION_TIMER:
2697 (void) request_max_time(request);
2700 case FR_ACTION_PROXY_REPLY:
2701 proxy_reply_too_late(request);
2705 if (process_proxy_reply(request, NULL)) {
2706 request->handle(request);
2708 request_finish(request, action);
2712 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
2717 /** Process the request after receiving a proxy reply.
2719 * Throught the post-proxy section, and the through the handler
2723 * digraph proxy_running {
2726 * proxy_running -> dup [ label = "DUP", arrowhead = "none" ];
2727 * proxy_running -> timer [ label = "TIMER < max_request_time" ];
2728 * proxy_running -> process_proxy_reply [ label = "RUN" ];
2729 * proxy_running -> done [ label = "TIMER >= timeout" ];
2733 static void proxy_running(REQUEST *request, int action)
2735 VERIFY_REQUEST(request);
2737 TRACE_STATE_MACHINE;
2742 request_dup(request);
2745 case FR_ACTION_TIMER:
2746 (void) request_max_time(request);
2750 if (process_proxy_reply(request, request->proxy_reply)) {
2751 request->handle(request);
2753 request_finish(request, action);
2756 default: /* duplicate proxy replies are suppressed */
2757 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
2762 /** Determine if a #REQUEST needs to be proxied, and perform pre-proxy operations
2764 * Whether a request will be proxied is determined by the attributes present
2765 * in request->config. If any of the following attributes are found, the
2766 * request may be proxied.
2768 * The key attributes are:
2769 * - PW_PROXY_TO_REALM - Specifies a realm the request should be proxied to.
2770 * - PW_HOME_SERVER_POOL - Specifies a specific home server pool to proxy to.
2771 * - PW_PACKET_DST_IP_ADDRESS - Specifies a specific IPv4 home server to proxy to.
2772 * - PW_PACKET_DST_IPV6_ADDRESS - Specifies a specific IPv6 home server to proxy to.
2774 * Certain packet types such as #PW_CODE_STATUS_SERVER will never be proxied.
2776 * If request should be proxied, will:
2777 * - Add request:Proxy-State
2778 * - Strip the current username value of its realm (depending on config)
2779 * - Create a CHAP-Challenge from the original request vector, if one doesn't already
2781 * - Call the pre-process section in the current server, or in the virtual server
2782 * associated with the home server pool we're proxying to.
2784 * @todo A lot of this logic is RADIUS specific, and should be moved out into a protocol
2785 * specific function.
2787 * @param request The #REQUEST to evaluate for proxying.
2788 * @return 0 if not proxying, 1 if request should be proxied, -1 on error.
2790 static int request_will_proxy(REQUEST *request)
2792 int rcode, pre_proxy_type = 0;
2793 char const *realmname = NULL;
2794 VALUE_PAIR *vp, *strippedname;
2795 home_server_t *home;
2796 REALM *realm = NULL;
2797 home_pool_t *pool = NULL;
2799 VERIFY_REQUEST(request);
2801 if (!request->root->proxy_requests) return 0;
2802 if (request->packet->dst_port == 0) return 0;
2803 if (request->packet->code == PW_CODE_STATUS_SERVER) return 0;
2804 if (request->in_proxy_hash) return 0;
2807 * FIXME: for 3.0, allow this only for rejects?
2809 if (request->reply->code != 0) return 0;
2811 vp = fr_pair_find_by_num(request->config, PW_PROXY_TO_REALM, 0, TAG_ANY);
2813 realm = realm_find2(vp->vp_strvalue);
2815 REDEBUG2("Cannot proxy to unknown realm %s",
2820 realmname = vp->vp_strvalue;
2823 * Figure out which pool to use.
2825 if (request->packet->code == PW_CODE_ACCESS_REQUEST) {
2826 pool = realm->auth_pool;
2828 #ifdef WITH_ACCOUNTING
2829 } else if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) {
2830 pool = realm->acct_pool;
2834 } else if ((request->packet->code == PW_CODE_COA_REQUEST) ||
2835 (request->packet->code == PW_CODE_DISCONNECT_REQUEST)) {
2836 pool = realm->coa_pool;
2843 } else if ((vp = fr_pair_find_by_num(request->config, PW_HOME_SERVER_POOL, 0, TAG_ANY)) != NULL) {
2846 switch (request->packet->code) {
2847 case PW_CODE_ACCESS_REQUEST:
2848 pool_type = HOME_TYPE_AUTH;
2851 #ifdef WITH_ACCOUNTING
2852 case PW_CODE_ACCOUNTING_REQUEST:
2853 pool_type = HOME_TYPE_ACCT;
2858 case PW_CODE_COA_REQUEST:
2859 case PW_CODE_DISCONNECT_REQUEST:
2860 pool_type = HOME_TYPE_COA;
2868 pool = home_pool_byname(vp->vp_strvalue, pool_type);
2871 * Send it directly to a home server (i.e. NAS)
2873 } else if (((vp = fr_pair_find_by_num(request->config, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY)) != NULL) ||
2874 ((vp = fr_pair_find_by_num(request->config, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY)) != NULL)) {
2876 fr_ipaddr_t dst_ipaddr;
2878 memset(&dst_ipaddr, 0, sizeof(dst_ipaddr));
2880 if (vp->da->attr == PW_PACKET_DST_IP_ADDRESS) {
2881 dst_ipaddr.af = AF_INET;
2882 dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
2883 dst_ipaddr.prefix = 32;
2885 dst_ipaddr.af = AF_INET6;
2886 memcpy(&dst_ipaddr.ipaddr.ip6addr, &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
2887 dst_ipaddr.prefix = 128;
2890 vp = fr_pair_find_by_num(request->config, PW_PACKET_DST_PORT, 0, TAG_ANY);
2892 if (request->packet->code == PW_CODE_ACCESS_REQUEST) {
2893 dst_port = PW_AUTH_UDP_PORT;
2895 #ifdef WITH_ACCOUNTING
2896 } else if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) {
2897 dst_port = PW_ACCT_UDP_PORT;
2901 } else if ((request->packet->code == PW_CODE_COA_REQUEST) ||
2902 (request->packet->code == PW_CODE_DISCONNECT_REQUEST)) {
2903 dst_port = PW_COA_UDP_PORT;
2905 } else { /* shouldn't happen for RADIUS... */
2910 dst_port = vp->vp_integer;
2914 * Nothing does CoA over TCP.
2916 home = home_server_find(&dst_ipaddr, dst_port, IPPROTO_UDP);
2920 WARN("No such home server %s port %u",
2921 inet_ntop(dst_ipaddr.af, &dst_ipaddr.ipaddr, buffer, sizeof(buffer)),
2922 (unsigned int) dst_port);
2927 * The home server is alive (or may be alive).
2928 * Send the packet to the IP.
2930 if (home->state != HOME_STATE_IS_DEAD) goto do_home;
2933 * The home server is dead. If you wanted
2934 * fail-over, you should have proxied to a pool.
2945 RWDEBUG2("Cancelling proxy as no home pool exists");
2949 if (request->listener->synchronous) {
2950 WARN("Cannot proxy a request which is from a 'synchronous' socket");
2954 request->home_pool = pool;
2956 home = home_server_ldb(realmname, pool, request);
2959 REDEBUG2("Failed to find live home server: Cancelling proxy");
2964 home_server_update_request(home, request);
2968 * Once we've decided to proxy a request, we cannot send
2969 * a CoA packet. So we free up any CoA packet here.
2971 if (request->coa) request_done(request->coa, FR_ACTION_DONE);
2975 * Remember that we sent the request to a Realm.
2977 if (realmname) pair_make_request("Realm", realmname, T_OP_EQ);
2980 * Strip the name, if told to.
2982 * Doing it here catches the case of proxied tunneled
2985 if (realm && (realm->strip_realm == true) &&
2986 (strippedname = fr_pair_find_by_num(request->proxy->vps, PW_STRIPPED_USER_NAME, 0, TAG_ANY)) != NULL) {
2988 * If there's a Stripped-User-Name attribute in
2989 * the request, then use THAT as the User-Name
2990 * for the proxied request, instead of the
2993 * This is done by making a copy of the
2994 * Stripped-User-Name attribute, turning it into
2995 * a User-Name attribute, deleting the
2996 * Stripped-User-Name and User-Name attributes
2997 * from the vps list, and making the new
2998 * User-Name the head of the vps list.
3000 vp = fr_pair_find_by_num(request->proxy->vps, PW_USER_NAME, 0, TAG_ANY);
3003 vp = radius_pair_create(NULL, NULL,
3005 rad_assert(vp != NULL); /* handled by above function */
3006 /* Insert at the START of the list */
3007 /* FIXME: Can't make assumptions about ordering */
3008 fr_cursor_init(&cursor, &vp);
3009 fr_cursor_merge(&cursor, request->proxy->vps);
3010 request->proxy->vps = vp;
3012 fr_pair_value_strcpy(vp, strippedname->vp_strvalue);
3015 * Do NOT delete Stripped-User-Name.
3020 * If there is no PW_CHAP_CHALLENGE attribute but
3021 * there is a PW_CHAP_PASSWORD we need to add it
3022 * since we can't use the request authenticator
3023 * anymore - we changed it.
3025 if ((request->packet->code == PW_CODE_ACCESS_REQUEST) &&
3026 fr_pair_find_by_num(request->proxy->vps, PW_CHAP_PASSWORD, 0, TAG_ANY) &&
3027 fr_pair_find_by_num(request->proxy->vps, PW_CHAP_CHALLENGE, 0, TAG_ANY) == NULL) {
3028 vp = radius_pair_create(request->proxy, &request->proxy->vps, PW_CHAP_CHALLENGE, 0);
3029 fr_pair_value_memcpy(vp, request->packet->vector, sizeof(request->packet->vector));
3033 * The RFC's say we have to do this, but FreeRADIUS
3036 vp = radius_pair_create(request->proxy, &request->proxy->vps, PW_PROXY_STATE, 0);
3037 fr_pair_value_sprintf(vp, "%u", request->packet->id);
3040 * Should be done BEFORE inserting into proxy hash, as
3041 * pre-proxy may use this information, or change it.
3043 request->proxy->code = request->packet->code;
3046 * Call the pre-proxy routines.
3048 vp = fr_pair_find_by_num(request->config, PW_PRE_PROXY_TYPE, 0, TAG_ANY);
3050 DICT_VALUE const *dval = dict_valbyattr(vp->da->attr, vp->da->vendor, vp->vp_integer);
3051 /* Must be a validation issue */
3053 RDEBUG2("Found Pre-Proxy-Type %s", dval->name);
3054 pre_proxy_type = vp->vp_integer;
3058 * home_pool may be NULL when originating CoA packets,
3059 * because they go directly to an IP address.
3061 if (request->home_pool && request->home_pool->virtual_server) {
3062 char const *old_server = request->server;
3064 request->server = request->home_pool->virtual_server;
3066 RDEBUG2("server %s {", request->server);
3068 rcode = process_pre_proxy(pre_proxy_type, request);
3072 request->server = old_server;
3074 rcode = process_pre_proxy(pre_proxy_type, request);
3078 case RLM_MODULE_FAIL:
3079 case RLM_MODULE_INVALID:
3080 case RLM_MODULE_NOTFOUND:
3081 case RLM_MODULE_USERLOCK:
3083 /* FIXME: debug print failed stuff */
3086 case RLM_MODULE_REJECT:
3087 case RLM_MODULE_HANDLED:
3091 * Only proxy the packet if the pre-proxy code succeeded.
3093 case RLM_MODULE_NOOP:
3095 case RLM_MODULE_UPDATED:
3100 static int proxy_to_virtual_server(REQUEST *request)
3104 if (request->packet->dst_port == 0) {
3105 WARN("Cannot proxy an internal request");
3109 DEBUG("Proxying to virtual server %s",
3110 request->home_server->server);
3113 * Packets to virtual servers don't get
3114 * retransmissions sent to them. And the virtual
3115 * server is run ONLY if we have no child
3116 * threads, or we're running in a child thread.
3118 rad_assert(!spawn_flag || !we_are_master());
3120 fake = request_alloc_fake(request);
3122 fake->packet->vps = fr_pair_list_copy(fake->packet, request->packet->vps);
3123 talloc_free(request->proxy);
3125 fake->server = request->home_server->server;
3126 fake->handle = request->handle;
3127 fake->process = NULL; /* should never be run for anything */
3130 * Run the virtual server.
3132 request_running(fake, FR_ACTION_RUN);
3134 request->proxy = talloc_steal(request, fake->packet);
3135 fake->packet = NULL;
3136 request->proxy_reply = talloc_steal(request, fake->reply);
3142 * No reply code, toss the reply we have,
3143 * and do post-proxy-type Fail.
3145 if (!request->proxy_reply->code) {
3146 TALLOC_FREE(request->proxy_reply);
3147 setup_post_proxy_fail(request);
3151 * Do the proxy reply (if any)
3153 if (process_proxy_reply(request, request->proxy_reply)) {
3154 request->handle(request);
3157 return -1; /* so we call request_finish */
3161 static int request_proxy(REQUEST *request, int retransmit)
3165 VERIFY_REQUEST(request);
3167 rad_assert(request->parent == NULL);
3168 rad_assert(request->home_server != NULL);
3170 if (request->master_state == REQUEST_STOP_PROCESSING) return 0;
3174 RWDEBUG("Cannot proxy and originate CoA packets at the same time. Cancelling CoA request");
3175 request_done(request->coa, FR_ACTION_DONE);
3180 * The request may need sending to a virtual server.
3181 * This code is more than a little screwed up. The rest
3182 * of the state machine doesn't handle parent / child
3183 * relationships well. i.e. if the child request takes
3184 * too long, the core will mark the *parent* as "stop
3185 * processing". And the child will continue without
3186 * knowing anything...
3188 * So, we have some horrible hacks to get around that.
3190 if (request->home_server->server) return proxy_to_virtual_server(request);
3193 * We're actually sending a proxied packet. Do that now.
3195 if (!request->in_proxy_hash && !insert_into_proxy_hash(request)) {
3196 RPROXY("Failed to insert request into the proxy list");
3200 rad_assert(request->proxy->id >= 0);
3202 if (rad_debug_lvl) {
3203 struct timeval *response_window;
3205 response_window = request_response_window(request);
3208 if (request->home_server->tls) {
3209 RDEBUG2("Proxying request to home server %s port %d (TLS) timeout %d.%06d",
3210 inet_ntop(request->proxy->dst_ipaddr.af,
3211 &request->proxy->dst_ipaddr.ipaddr,
3212 buffer, sizeof(buffer)),
3213 request->proxy->dst_port,
3214 (int) response_window->tv_sec, (int) response_window->tv_usec);
3217 RDEBUG2("Proxying request to home server %s port %d timeout %d.%06d",
3218 inet_ntop(request->proxy->dst_ipaddr.af,
3219 &request->proxy->dst_ipaddr.ipaddr,
3220 buffer, sizeof(buffer)),
3221 request->proxy->dst_port,
3222 (int) response_window->tv_sec, (int) response_window->tv_usec);
3227 gettimeofday(&request->proxy_retransmit, NULL);
3229 request->proxy->timestamp = request->proxy_retransmit;
3231 request->home_server->last_packet_sent = request->proxy_retransmit.tv_sec;
3234 * Encode the packet before we do anything else.
3236 request->proxy_listener->encode(request->proxy_listener, request);
3237 debug_packet(request, request->proxy, false);
3240 * Set the state function, then the state, no child, and
3243 * The order here is different from other state changes
3244 * due to race conditions with replies from the home
3247 request->process = proxy_wait_for_reply;
3248 request->child_state = REQUEST_PROXIED;
3249 request->module = "<REQUEST_PROXIED>";
3253 * And send the packet.
3255 request->proxy_listener->send(request->proxy_listener, request);
3260 * Proxy the packet as if it was new.
3262 static int request_proxy_anew(REQUEST *request)
3264 home_server_t *home;
3266 VERIFY_REQUEST(request);
3269 * Delete the request from the proxy list.
3271 * The packet list code takes care of ensuring that IDs
3272 * aren't reused until all 256 IDs have been used. So
3273 * there's a 1/256 chance of re-using the same ID when
3274 * we're sending to the same home server. Which is
3277 remove_from_proxy_hash(request);
3280 * Find a live home server for the request.
3282 home = home_server_ldb(NULL, request->home_pool, request);
3284 REDEBUG2("Failed to find live home server for request");
3286 if (setup_post_proxy_fail(request)) {
3287 request_queue_or_run(request, proxy_running);
3289 gettimeofday(&request->reply->timestamp, NULL);
3290 request_cleanup_delay_init(request);
3295 #ifdef WITH_ACCOUNTING
3297 * Update the Acct-Delay-Time attribute.
3299 if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) {
3302 vp = fr_pair_find_by_num(request->proxy->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY);
3303 if (!vp) vp = radius_pair_create(request->proxy,
3304 &request->proxy->vps,
3305 PW_ACCT_DELAY_TIME, 0);
3309 gettimeofday(&now, NULL);
3310 vp->vp_integer += now.tv_sec - request->proxy_retransmit.tv_sec;
3316 * May have failed over to a "fallback" virtual server.
3317 * If so, run that instead of doing proxying to a real
3321 request->home_server = home;
3322 TALLOC_FREE(request->proxy);
3324 (void) proxy_to_virtual_server(request);
3328 home_server_update_request(home, request);
3330 if (!insert_into_proxy_hash(request)) {
3331 RPROXY("Failed to insert retransmission into the proxy list");
3332 goto post_proxy_fail;
3336 * Free the old packet, to force re-encoding
3338 talloc_free(request->proxy->data);
3339 request->proxy->data = NULL;
3340 request->proxy->data_len = 0;
3342 if (request_proxy(request, 1) != 1) goto post_proxy_fail;
3348 /** Ping a home server.
3351 static void request_ping(REQUEST *request, int action)
3353 home_server_t *home = request->home_server;
3356 VERIFY_REQUEST(request);
3358 TRACE_STATE_MACHINE;
3362 case FR_ACTION_TIMER:
3363 ERROR("No response to status check %d ID %u for home server %s port %d",
3366 inet_ntop(request->proxy->dst_ipaddr.af,
3367 &request->proxy->dst_ipaddr.ipaddr,
3368 buffer, sizeof(buffer)),
3369 request->proxy->dst_port);
3372 case FR_ACTION_PROXY_REPLY:
3373 rad_assert(request->in_proxy_hash);
3375 request->home_server->num_received_pings++;
3376 RPROXY("Received response to status check %d ID %u (%d in current sequence)",
3377 request->number, request->proxy->id, home->num_received_pings);
3380 * Remove the request from any hashes
3382 fr_event_delete(el, &request->ev);
3383 remove_from_proxy_hash(request);
3386 * The control socket may have marked the home server as
3387 * alive. OR, it may have suddenly started responding to
3388 * requests again. If so, don't re-do the "make alive"
3391 if (home->state == HOME_STATE_ALIVE) break;
3394 * It's dead, and we haven't received enough ping
3395 * responses to mark it "alive". Wait a bit.
3397 * If it's zombie, we mark it alive immediately.
3399 if ((home->state == HOME_STATE_IS_DEAD) &&
3400 (home->num_received_pings < home->num_pings_to_alive)) {
3405 * Mark it alive and delete any outstanding
3408 mark_home_server_alive(request, home);
3412 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
3416 rad_assert(!request->in_request_hash);
3417 rad_assert(request->ev == NULL);
3419 request_done(request, FR_ACTION_DONE);
3423 * Called from start of zombie period, OR after control socket
3424 * marks the home server dead.
3426 static void ping_home_server(void *ctx)
3428 home_server_t *home = talloc_get_type_abort(ctx, home_server_t);
3431 struct timeval when, now;
3433 if ((home->state == HOME_STATE_ALIVE) ||
3435 (home->proto == IPPROTO_TCP) ||
3437 (home->ev != NULL)) {
3441 gettimeofday(&now, NULL);
3445 * We've run out of zombie time. Mark it dead.
3447 if (home->state == HOME_STATE_ZOMBIE) {
3448 when = home->zombie_period_start;
3449 when.tv_sec += home->zombie_period;
3451 if (timercmp(&when, &now, <)) {
3452 DEBUG("PING: Zombie period is over for home server %s", home->log_name);
3453 mark_home_server_dead(home, &now);
3458 * We're not supposed to be pinging it. Just wake up
3459 * when we're supposed to mark it dead.
3461 if (home->ping_check == HOME_PING_CHECK_NONE) {
3462 if (home->state == HOME_STATE_ZOMBIE) {
3463 home->when = home->zombie_period_start;
3464 home->when.tv_sec += home->zombie_period;
3465 INSERT_EVENT(ping_home_server, home);
3469 * Else mark_home_server_dead will set a timer
3470 * for revive_interval.
3476 request = request_alloc(NULL);
3477 if (!request) return;
3478 request->number = request_num_counter++;
3481 request->proxy = rad_alloc(request, true);
3482 rad_assert(request->proxy != NULL);
3484 if (home->ping_check == HOME_PING_CHECK_STATUS_SERVER) {
3485 request->proxy->code = PW_CODE_STATUS_SERVER;
3487 fr_pair_make(request->proxy, &request->proxy->vps,
3488 "Message-Authenticator", "0x00", T_OP_SET);
3490 } else if (home->type == HOME_TYPE_AUTH) {
3491 request->proxy->code = PW_CODE_ACCESS_REQUEST;
3493 fr_pair_make(request->proxy, &request->proxy->vps,
3494 "User-Name", home->ping_user_name, T_OP_SET);
3495 fr_pair_make(request->proxy, &request->proxy->vps,
3496 "User-Password", home->ping_user_password, T_OP_SET);
3497 fr_pair_make(request->proxy, &request->proxy->vps,
3498 "Service-Type", "Authenticate-Only", T_OP_SET);
3499 fr_pair_make(request->proxy, &request->proxy->vps,
3500 "Message-Authenticator", "0x00", T_OP_SET);
3503 #ifdef WITH_ACCOUNTING
3504 request->proxy->code = PW_CODE_ACCOUNTING_REQUEST;
3506 fr_pair_make(request->proxy, &request->proxy->vps,
3507 "User-Name", home->ping_user_name, T_OP_SET);
3508 fr_pair_make(request->proxy, &request->proxy->vps,
3509 "Acct-Status-Type", "Stop", T_OP_SET);
3510 fr_pair_make(request->proxy, &request->proxy->vps,
3511 "Acct-Session-Id", "00000000", T_OP_SET);
3512 vp = fr_pair_make(request->proxy, &request->proxy->vps,
3513 "Event-Timestamp", "0", T_OP_SET);
3514 vp->vp_date = now.tv_sec;
3516 rad_assert("Internal sanity check failed");
3520 vp = fr_pair_make(request->proxy, &request->proxy->vps,
3521 "NAS-Identifier", "", T_OP_SET);
3523 fr_pair_value_sprintf(vp, "Status Check %u. Are you alive?",
3524 home->num_sent_pings);
3528 request->proxy->proto = home->proto;
3530 request->proxy->src_ipaddr = home->src_ipaddr;
3531 request->proxy->dst_ipaddr = home->ipaddr;
3532 request->proxy->dst_port = home->port;
3533 request->home_server = home;
3534 #ifdef DEBUG_STATE_MACHINE
3535 if (rad_debug_lvl) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n", request->number, __FUNCTION__,
3536 child_state_names[request->child_state],
3537 child_state_names[REQUEST_DONE]);
3538 if (rad_debug_lvl) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_ping");
3540 #ifdef HAVE_PTHREAD_H
3541 rad_assert(request->child_pid == NO_SUCH_CHILD_PID);
3543 request->child_state = REQUEST_PROXIED;
3544 request->process = request_ping;
3546 rad_assert(request->proxy_listener == NULL);
3548 if (!insert_into_proxy_hash(request)) {
3549 RPROXY("Failed to insert status check %d into proxy list. Discarding it.",
3552 rad_assert(!request->in_request_hash);
3553 rad_assert(!request->in_proxy_hash);
3554 rad_assert(request->ev == NULL);
3555 talloc_free(request);
3560 * Set up the timer callback.
3563 when.tv_sec += home->ping_timeout;
3565 DEBUG("PING: Waiting %u seconds for response to ping",
3566 home->ping_timeout);
3568 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3569 home->num_sent_pings++;
3571 rad_assert(request->proxy_listener != NULL);
3572 debug_packet(request, request->proxy, false);
3573 request->proxy_listener->send(request->proxy_listener,
3577 * Add +/- 2s of jitter, as suggested in RFC 3539
3578 * and in the Issues and Fixes draft.
3581 home->when.tv_sec += home->ping_interval;
3583 add_jitter(&home->when);
3585 DEBUG("PING: Next status packet in %u seconds", home->ping_interval);
3586 INSERT_EVENT(ping_home_server, home);
3589 static void home_trigger(home_server_t *home, char const *trigger)
3591 REQUEST *my_request;
3592 RADIUS_PACKET *my_packet;
3594 my_request = talloc_zero(NULL, REQUEST);
3595 my_packet = talloc_zero(my_request, RADIUS_PACKET);
3596 my_request->proxy = my_packet;
3597 my_packet->dst_ipaddr = home->ipaddr;
3598 my_packet->src_ipaddr = home->src_ipaddr;
3600 exec_trigger(my_request, home->cs, trigger, false);
3601 talloc_free(my_request);
3604 static void mark_home_server_zombie(home_server_t *home, struct timeval *now, struct timeval *response_window)
3611 rad_assert((home->state == HOME_STATE_ALIVE) ||
3612 (home->state == HOME_STATE_UNKNOWN));
3615 if (home->proto == IPPROTO_TCP) {
3616 WARN("Not marking TCP server %s zombie", home->log_name);
3622 * We've received a real packet recently. Don't mark the
3623 * server as zombie until we've received NO packets for a
3624 * while. The "1/4" of zombie period was chosen rather
3625 * arbitrarily. It's a balance between too short, which
3626 * gives quick fail-over and fail-back, or too long,
3627 * where the proxy still sends packets to an unresponsive
3630 start = now->tv_sec - ((home->zombie_period + 3) / 4);
3631 if (home->last_packet_recv >= start) {
3632 DEBUG("Recieved reply from home server %d seconds ago. Might not be zombie.",
3633 (int) (now->tv_sec - home->last_packet_recv));
3637 home->state = HOME_STATE_ZOMBIE;
3638 home_trigger(home, "home_server.zombie");
3641 * Set the home server to "zombie", as of the time
3644 home->zombie_period_start.tv_sec = start;
3645 home->zombie_period_start.tv_usec = USEC / 2;
3647 fr_event_delete(el, &home->ev);
3649 home->num_sent_pings = 0;
3650 home->num_received_pings = 0;
3652 PROXY( "Marking home server %s port %d as zombie (it has not responded in %d.%06d seconds).",
3653 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
3654 buffer, sizeof(buffer)),
3655 home->port, (int) response_window->tv_sec, (int) response_window->tv_usec);
3657 ping_home_server(home);
3661 void revive_home_server(void *ctx)
3663 home_server_t *home = talloc_get_type_abort(ctx, home_server_t);
3667 rad_assert(home->proto != IPPROTO_TCP);
3670 home->state = HOME_STATE_ALIVE;
3671 home->response_timeouts = 0;
3672 home_trigger(home, "home_server.alive");
3673 home->currently_outstanding = 0;
3674 gettimeofday(&home->revive_time, NULL);
3677 * Delete any outstanding events.
3680 if (home->ev) fr_event_delete(el, &home->ev);
3682 PROXY( "Marking home server %s port %d alive again... we have no idea if it really is alive or not.",
3683 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
3684 buffer, sizeof(buffer)),
3688 void mark_home_server_dead(home_server_t *home, struct timeval *when)
3690 int previous_state = home->state;
3694 if (home->proto == IPPROTO_TCP) {
3695 WARN("Not marking TCP server dead");
3700 PROXY( "Marking home server %s port %d as dead.",
3701 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
3702 buffer, sizeof(buffer)),
3705 home->state = HOME_STATE_IS_DEAD;
3706 home_trigger(home, "home_server.dead");
3708 if (home->ping_check != HOME_PING_CHECK_NONE) {
3710 * If the control socket marks us dead, start
3711 * pinging. Otherwise, we already started
3712 * pinging when it was marked "zombie".
3714 if (previous_state == HOME_STATE_ALIVE) {
3715 ping_home_server(home);
3717 DEBUG("PING: Already pinging home server %s", home->log_name);
3722 * Revive it after a fixed period of time. This
3723 * is very, very, bad.
3726 home->when.tv_sec += home->revive_interval;
3728 DEBUG("PING: Reviving home server %s in %u seconds", home->log_name, home->revive_interval);
3730 INSERT_EVENT(revive_home_server, home);
3734 /** Wait for a reply after proxying a request.
3736 * Retransmit the proxied packet, or time out and go to
3737 * proxy_no_reply. Mark the home server unresponsive, etc.
3739 * If we do receive a reply, we transition to proxy_running.
3742 * digraph proxy_wait_for_reply {
3743 * proxy_wait_for_reply;
3745 * proxy_wait_for_reply -> retransmit_proxied_request [ label = "DUP", arrowhead = "none" ];
3746 * proxy_wait_for_reply -> proxy_no_reply [ label = "TIMER >= response_window" ];
3747 * proxy_wait_for_reply -> timer [ label = "TIMER < max_request_time" ];
3748 * proxy_wait_for_reply -> proxy_running [ label = "PROXY_REPLY" arrowhead = "none"];
3749 * proxy_wait_for_reply -> done [ label = "TIMER >= max_request_time" ];
3753 static void proxy_wait_for_reply(REQUEST *request, int action)
3755 struct timeval now, when;
3756 struct timeval *response_window = NULL;
3757 home_server_t *home = request->home_server;
3760 VERIFY_REQUEST(request);
3762 TRACE_STATE_MACHINE;
3765 rad_assert(request->packet->code != PW_CODE_STATUS_SERVER);
3766 rad_assert(request->home_server != NULL);
3768 gettimeofday(&now, NULL);
3773 * We have a reply, ignore the retransmit.
3775 if (request->proxy_reply) return;
3778 * The request was proxied to a virtual server.
3779 * Ignore the retransmit.
3781 if (request->home_server->server) return;
3784 * Use a new connection when the home server is
3785 * dead, or when there's no proxy listener, or
3786 * when the listener is failed or dead.
3788 * If the listener is known or frozen, use it for
3791 if ((home->state == HOME_STATE_IS_DEAD) ||
3792 !request->proxy_listener ||
3793 (request->proxy_listener->status >= RAD_LISTEN_STATUS_EOL)) {
3794 request_proxy_anew(request);
3800 * The home server is still alive, but TCP. We
3801 * rely on TCP to get the request and reply back.
3802 * So there's no need to retransmit.
3804 if (home->proto == IPPROTO_TCP) {
3805 DEBUG2("Suppressing duplicate proxied request (tcp) to home server %s port %d proto TCP - ID: %d",
3806 inet_ntop(request->proxy->dst_ipaddr.af,
3807 &request->proxy->dst_ipaddr.ipaddr,
3808 buffer, sizeof(buffer)),
3809 request->proxy->dst_port,
3810 request->proxy->id);
3816 * More than one retransmit a second is stupid,
3817 * and should be suppressed by the proxy.
3819 when = request->proxy_retransmit;
3822 if (timercmp(&now, &when, <)) {
3823 DEBUG2("Suppressing duplicate proxied request (too fast) to home server %s port %d proto TCP - ID: %d",
3824 inet_ntop(request->proxy->dst_ipaddr.af,
3825 &request->proxy->dst_ipaddr.ipaddr,
3826 buffer, sizeof(buffer)),
3827 request->proxy->dst_port,
3828 request->proxy->id);
3832 #ifdef WITH_ACCOUNTING
3834 * If we update the Acct-Delay-Time, we need to
3837 if ((request->packet->code == PW_CODE_ACCOUNTING_REQUEST) &&
3838 fr_pair_find_by_num(request->proxy->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY)) {
3839 request_proxy_anew(request);
3844 RDEBUG2("Sending duplicate proxied request to home server %s port %d - ID: %d",
3845 inet_ntop(request->proxy->dst_ipaddr.af,
3846 &request->proxy->dst_ipaddr.ipaddr,
3847 buffer, sizeof(buffer)),
3848 request->proxy->dst_port,
3849 request->proxy->id);
3850 request->num_proxied_requests++;
3852 rad_assert(request->proxy_listener != NULL);
3853 FR_STATS_TYPE_INC(home->stats.total_requests);
3854 home->last_packet_sent = now.tv_sec;
3855 request->proxy_retransmit = now;
3856 debug_packet(request, request->proxy, false);
3857 request->proxy_listener->send(request->proxy_listener, request);
3860 case FR_ACTION_TIMER:
3861 response_window = request_response_window(request);
3864 if (!request->proxy_listener ||
3865 (request->proxy_listener->status >= RAD_LISTEN_STATUS_EOL)) {
3866 remove_from_proxy_hash(request);
3868 when = request->packet->timestamp;
3869 when.tv_sec += request->root->max_request_time;
3871 if (timercmp(&when, &now, >)) {
3872 RDEBUG("Waiting for client retransmission in order to do a proxy retransmit");
3873 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3880 * Wake up "response_window" time in the future.
3881 * i.e. when MY packet hasn't received a response.
3883 * Note that we DO NOT mark the home server as
3884 * zombie if it doesn't respond to us. It may be
3885 * responding to other (better looking) packets.
3887 when = request->proxy->timestamp;
3888 timeradd(&when, response_window, &when);
3891 * Not at the response window. Set the timer for
3894 if (timercmp(&when, &now, >)) {
3895 struct timeval diff;
3896 timersub(&when, &now, &diff);
3898 RDEBUG("Expecting proxy response no later than %d.%06d seconds from now",
3899 (int) diff.tv_sec, (int) diff.tv_usec);
3900 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3905 RDEBUG("No proxy response, giving up on request and marking it done");
3908 * If we haven't received any packets for
3909 * "response_window", then mark the home server
3912 * If the connection is TCP, then another
3913 * "watchdog timer" function takes care of pings,
3914 * etc. So we don't need to do it here.
3916 * This check should really be part of a home
3917 * server state machine.
3919 if (((home->state == HOME_STATE_ALIVE) ||
3920 (home->state == HOME_STATE_UNKNOWN))
3922 && (home->proto != IPPROTO_TCP)
3925 home->response_timeouts++;
3926 if (home->response_timeouts >= home->max_response_timeouts)
3927 mark_home_server_zombie(home, &now, response_window);
3930 FR_STATS_TYPE_INC(home->stats.total_timeouts);
3931 if (home->type == HOME_TYPE_AUTH) {
3932 if (request->proxy_listener) FR_STATS_TYPE_INC(request->proxy_listener->stats.total_timeouts);
3933 FR_STATS_TYPE_INC(proxy_auth_stats.total_timeouts);
3936 else if (home->type == HOME_TYPE_ACCT) {
3937 if (request->proxy_listener) FR_STATS_TYPE_INC(request->proxy_listener->stats.total_timeouts);
3938 FR_STATS_TYPE_INC(proxy_acct_stats.total_timeouts);
3943 * There was no response within the window. Stop
3944 * the request. If the client retransmitted, it
3945 * may have failed over to another home server.
3946 * But that one may be dead, too.
3948 * The extra verbose message if we have a username,
3949 * is extremely useful if the proxy is part of a chain
3950 * and the final home server, is not the one we're
3953 if (request->username) {
3954 RERROR("Failing proxied request for user \"%s\", due to lack of any response from home "
3955 "server %s port %d",
3956 request->username->vp_strvalue,
3957 inet_ntop(request->proxy->dst_ipaddr.af,
3958 &request->proxy->dst_ipaddr.ipaddr,
3959 buffer, sizeof(buffer)),
3960 request->proxy->dst_port);
3962 RERROR("Failing proxied request, due to lack of any response from home server %s port %d",
3963 inet_ntop(request->proxy->dst_ipaddr.af,
3964 &request->proxy->dst_ipaddr.ipaddr,
3965 buffer, sizeof(buffer)),
3966 request->proxy->dst_port);
3969 if (setup_post_proxy_fail(request)) {
3970 request_queue_or_run(request, proxy_no_reply);
3972 gettimeofday(&request->reply->timestamp, NULL);
3973 request_cleanup_delay_init(request);
3978 * We received a new reply. Go process it.
3980 case FR_ACTION_PROXY_REPLY:
3981 request_queue_or_run(request, proxy_running);
3985 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
3989 #endif /* WITH_PROXY */
3992 /***********************************************************************
3996 ***********************************************************************/
3998 static int null_handler(UNUSED REQUEST *request)
4004 * See if we need to originate a CoA request.
4006 static void request_coa_originate(REQUEST *request)
4008 int rcode, pre_proxy_type = 0;
4014 VERIFY_REQUEST(request);
4016 rad_assert(request->coa != NULL);
4017 rad_assert(request->proxy == NULL);
4018 rad_assert(!request->in_proxy_hash);
4019 rad_assert(request->proxy_reply == NULL);
4022 * Check whether we want to originate one, or cancel one.
4024 vp = fr_pair_find_by_num(request->config, PW_SEND_COA_REQUEST, 0, TAG_ANY);
4026 vp = fr_pair_find_by_num(request->coa->proxy->vps, PW_SEND_COA_REQUEST, 0, TAG_ANY);
4030 if (vp->vp_integer == 0) {
4032 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;
4395 coa_retransmit(request);
4398 case FR_ACTION_PROXY_REPLY:
4399 request_queue_or_run(request, coa_running);
4403 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
4408 static void coa_separate(REQUEST *request)
4410 VERIFY_REQUEST(request);
4411 #ifdef DEBUG_STATE_MACHINE
4412 int action = FR_ACTION_TIMER;
4415 TRACE_STATE_MACHINE;
4418 rad_assert(request->parent != NULL);
4419 rad_assert(request->parent->coa == request);
4420 rad_assert(request->ev == NULL);
4421 rad_assert(!request->in_request_hash);
4422 rad_assert(request->coa == NULL);
4424 rad_assert(request->proxy_reply || request->proxy_listener);
4426 (void) talloc_steal(NULL, request);
4427 request->parent->coa = NULL;
4428 request->parent = NULL;
4430 if (we_are_master()) {
4432 coa_retransmit(request);
4437 /** Process a request after the CoA has timed out.
4439 * Run the packet through Post-Proxy-Type Fail
4442 * digraph coa_no_reply {
4445 * coa_no_reply -> dup [ label = "DUP", arrowhead = "none" ];
4446 * coa_no_reply -> timer [ label = "TIMER < max_request_time" ];
4447 * coa_no_reply -> coa_reply_too_late [ label = "PROXY_REPLY" arrowhead = "none"];
4448 * coa_no_reply -> process_proxy_reply [ label = "RUN" ];
4449 * coa_no_reply -> done [ label = "TIMER >= timeout" ];
4453 static void coa_no_reply(REQUEST *request, int action)
4457 VERIFY_REQUEST(request);
4459 TRACE_STATE_MACHINE;
4463 case FR_ACTION_TIMER:
4464 (void) request_max_time(request);
4467 case FR_ACTION_PROXY_REPLY: /* too late! */
4468 RDEBUG2("Reply from CoA server %s port %d - ID: %d arrived too late.",
4469 inet_ntop(request->proxy->src_ipaddr.af,
4470 &request->proxy->src_ipaddr.ipaddr,
4471 buffer, sizeof(buffer)),
4472 request->proxy->dst_port, request->proxy->id);
4476 if (process_proxy_reply(request, NULL)) {
4477 request->handle(request);
4479 request_done(request, FR_ACTION_DONE);
4483 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
4489 /** Process the request after receiving a coa reply.
4491 * Throught the post-proxy section, and the through the handler
4495 * digraph coa_running {
4498 * coa_running -> timer [ label = "TIMER < max_request_time" ];
4499 * coa_running -> process_proxy_reply [ label = "RUN" ];
4500 * coa_running -> done [ label = "TIMER >= timeout" ];
4504 static void coa_running(REQUEST *request, int action)
4506 VERIFY_REQUEST(request);
4508 TRACE_STATE_MACHINE;
4512 case FR_ACTION_TIMER:
4513 (void) request_max_time(request);
4517 if (process_proxy_reply(request, request->proxy_reply)) {
4518 request->handle(request);
4520 request_done(request, FR_ACTION_DONE);
4524 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
4528 #endif /* WITH_COA */
4530 /***********************************************************************
4532 * End of the State machine. Start of additional helper code.
4534 ***********************************************************************/
4536 /***********************************************************************
4540 ***********************************************************************/
4541 static void event_socket_handler(fr_event_list_t *xel, UNUSED int fd, void *ctx)
4543 rad_listen_t *listener = talloc_get_type_abort(ctx, rad_listen_t);
4545 rad_assert(xel == el);
4547 if ((listener->fd < 0)
4549 #ifndef WITH_DETAIL_THREAD
4550 && (listener->type != RAD_LISTEN_DETAIL)
4556 listener->print(listener, buffer, sizeof(buffer));
4557 ERROR("FATAL: Asked to read from closed socket: %s",
4560 rad_panic("Socket was closed on us!");
4564 listener->recv(listener);
4568 #ifdef WITH_DETAIL_THREAD
4571 * This function is called periodically to see if this detail
4572 * file is available for reading.
4574 static void event_poll_detail(void *ctx)
4577 rad_listen_t *this = talloc_get_type_abort(ctx, rad_listen_t);
4578 struct timeval when, now;
4579 listen_detail_t *detail = this->data;
4581 rad_assert(this->type == RAD_LISTEN_DETAIL);
4584 event_socket_handler(el, this->fd, this);
4586 fr_event_now(el, &now);
4590 * Backdoor API to get the delay until the next poll
4593 delay = this->encode(this, NULL);
4594 if (delay == 0) goto redo;
4596 tv_add(&when, delay);
4599 if (!fr_event_insert(el, event_poll_detail, this,
4600 &when, &detail->ev)) {
4601 ERROR("Failed creating handler");
4605 #endif /* WITH_DETAIL_THREAD */
4606 #endif /* WITH_DETAIL */
4608 static void event_status(struct timeval *wake)
4610 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
4614 if (rad_debug_lvl == 0) {
4616 INFO("Ready to process requests");
4617 just_started = false;
4623 INFO("Ready to process requests");
4625 } else if ((wake->tv_sec != 0) ||
4626 (wake->tv_usec >= 100000)) {
4627 DEBUG("Waking up in %d.%01u seconds.",
4628 (int) wake->tv_sec, (unsigned int) wake->tv_usec / 100000);
4633 * FIXME: Put this somewhere else, where it isn't called
4634 * all of the time...
4637 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
4639 * If there are no child threads, then there may
4640 * be child processes. In that case, wait for
4641 * their exit status, and throw that exit status
4642 * away. This helps get rid of zxombie children.
4644 while (waitpid(-1, &argval, WNOHANG) > 0) {
4652 static void listener_free_cb(void *ctx)
4654 rad_listen_t *this = talloc_get_type_abort(ctx, rad_listen_t);
4657 if (this->count > 0) {
4658 struct timeval when;
4659 listen_socket_t *sock = this->data;
4661 fr_event_now(el, &when);
4665 if (!fr_event_insert(el, listener_free_cb, this, &when,
4667 rad_panic("Failed to insert event");
4674 * It's all free, close the socket.
4677 this->print(this, buffer, sizeof(buffer));
4678 DEBUG("... cleaning up socket %s", buffer);
4679 rad_assert(this->next == NULL);
4685 static int proxy_eol_cb(void *ctx, void *data)
4687 struct timeval when;
4688 REQUEST *request = fr_packet2myptr(REQUEST, proxy, data);
4690 if (request->proxy_listener != ctx) return 0;
4693 * We don't care if it's being processed in a child thread.
4696 #ifdef WITH_ACCOUNTING
4698 * Accounting packets should be deleted immediately.
4699 * They will never be retransmitted by the client.
4701 if (request->proxy->code == PW_CODE_ACCOUNTING_REQUEST) {
4702 RDEBUG("Stopping request due to failed connection to home server");
4703 request->master_state = REQUEST_STOP_PROCESSING;
4708 * Reset the timer to be now, so that the request is
4709 * quickly updated. But spread the requests randomly
4710 * over the next second, so that we don't overload the
4713 fr_event_now(el, &when);
4714 tv_add(&when, fr_rand() % USEC);
4715 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
4718 * Don't delete it from the list.
4724 static int event_new_fd(rad_listen_t *this)
4730 if (this->status == RAD_LISTEN_STATUS_KNOWN) return 1;
4732 this->print(this, buffer, sizeof(buffer));
4734 if (this->status == RAD_LISTEN_STATUS_INIT) {
4735 listen_socket_t *sock = this->data;
4737 rad_assert(sock != NULL);
4739 DEBUG("Listening on %s", buffer);
4741 INFO(" ... adding new socket %s", buffer);
4745 if (!just_started && (this->type == RAD_LISTEN_PROXY)) {
4746 home_server_t *home;
4749 if (!home || !home->limit.max_connections) {
4750 INFO(" ... adding new socket %s", buffer);
4752 INFO(" ... adding new socket %s (%u of %u)", buffer,
4753 home->limit.num_connections, home->limit.max_connections);
4759 switch (this->type) {
4762 * Detail files are always known, and aren't
4763 * put into the socket event loop.
4765 case RAD_LISTEN_DETAIL:
4766 this->status = RAD_LISTEN_STATUS_KNOWN;
4768 #ifndef WITH_DETAIL_THREAD
4770 * Set up the first poll interval.
4772 event_poll_detail(this);
4775 break; /* add the FD to the list */
4777 #endif /* WITH_DETAIL */
4781 * Add it to the list of sockets we can use.
4782 * Server sockets (i.e. auth/acct) are never
4783 * added to the packet list.
4785 case RAD_LISTEN_PROXY:
4787 rad_assert((sock->proto == IPPROTO_UDP) || (sock->home != NULL));
4790 * Add timers to outgoing child sockets, if necessary.
4792 if (sock->proto == IPPROTO_TCP && sock->opened &&
4793 (sock->home->limit.lifetime || sock->home->limit.idle_timeout)) {
4794 struct timeval when;
4796 when.tv_sec = sock->opened + 1;
4800 if (!fr_event_insert(el, tcp_socket_timer, this, &when,
4802 rad_panic("Failed to insert event");
4807 #endif /* WITH_PROXY */
4810 * FIXME: put idle timers on command sockets.
4816 * Add timers to incoming child sockets, if necessary.
4818 if (sock->proto == IPPROTO_TCP && sock->opened &&
4819 (sock->limit.lifetime || sock->limit.idle_timeout)) {
4820 struct timeval when;
4822 when.tv_sec = sock->opened + 1;
4826 if (!fr_event_insert(el, tcp_socket_timer, this, &when,
4828 ERROR("Failed adding timer for socket: %s", fr_strerror());
4834 } /* switch over listener types */
4837 * All sockets: add the FD to the event handler.
4839 if (!fr_event_fd_insert(el, 0, this->fd,
4840 event_socket_handler, this)) {
4841 ERROR("Failed adding event handler for socket: %s", fr_strerror());
4845 this->status = RAD_LISTEN_STATUS_KNOWN;
4851 * The socket has reached a timeout. Try to close it.
4853 if (this->status == RAD_LISTEN_STATUS_FROZEN) {
4855 * Requests are still using the socket. Wait for
4858 if (this->count > 0) {
4859 struct timeval when;
4860 listen_socket_t *sock = this->data;
4863 * Try again to clean up the socket in 30
4866 gettimeofday(&when, NULL);
4870 if (!fr_event_insert(el,
4871 (fr_event_callback_t) event_new_fd,
4872 this, &when, &sock->ev)) {
4873 rad_panic("Failed to insert event");
4879 fr_event_fd_delete(el, 0, this->fd);
4880 this->status = RAD_LISTEN_STATUS_REMOVE_NOW;
4884 * The socket has had a catastrophic error. Close it.
4886 if (this->status == RAD_LISTEN_STATUS_EOL) {
4888 * Remove it from the list of live FD's.
4890 fr_event_fd_delete(el, 0, this->fd);
4894 * Tell all requests using this socket that the socket is dead.
4896 if (this->type == RAD_LISTEN_PROXY) {
4897 PTHREAD_MUTEX_LOCK(&proxy_mutex);
4898 if (!fr_packet_list_socket_freeze(proxy_list,
4900 ERROR("Fatal error freezing socket: %s", fr_strerror());
4904 if (this->count > 0) {
4905 fr_packet_list_walk(proxy_list, this, proxy_eol_cb);
4907 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
4912 * Requests are still using the socket. Wait for
4915 if (this->count > 0) {
4916 struct timeval when;
4917 listen_socket_t *sock = this->data;
4920 * Try again to clean up the socket in 30
4923 gettimeofday(&when, NULL);
4927 if (!fr_event_insert(el,
4928 (fr_event_callback_t) event_new_fd,
4929 this, &when, &sock->ev)) {
4930 rad_panic("Failed to insert event");
4937 * No one is using the socket. We can remove it now.
4939 this->status = RAD_LISTEN_STATUS_REMOVE_NOW;
4940 } /* socket is at EOL */
4946 if (this->status == RAD_LISTEN_STATUS_REMOVE_NOW) {
4949 listen_socket_t *sock = this->data;
4951 struct timeval when;
4954 * Re-open the socket, pointing it to /dev/null.
4955 * This means that all writes proceed without
4956 * blocking, and all reads return "no data".
4958 * This leaves the socket active, so any child
4959 * threads won't go insane. But it means that
4960 * they cannot send or receive any packets.
4962 * This is EXTRA work in the normal case, when
4963 * sockets are closed without error. But it lets
4964 * us have one simple processing method for all
4967 devnull = open("/dev/null", O_RDWR);
4969 ERROR("FATAL failure opening /dev/null: %s",
4970 fr_syserror(errno));
4973 if (dup2(devnull, this->fd) < 0) {
4974 ERROR("FATAL failure closing socket: %s",
4975 fr_syserror(errno));
4981 rad_assert(this->type != RAD_LISTEN_DETAIL);
4987 * The socket is dead. Force all proxied packets
4988 * to stop using it. And then remove it from the
4989 * list of outgoing sockets.
4991 if (this->type == RAD_LISTEN_PROXY) {
4992 home_server_t *home;
4995 if (!home || !home->limit.max_connections) {
4996 INFO(" ... shutting down socket %s", buffer);
4998 INFO(" ... shutting down socket %s (%u of %u)", buffer,
4999 home->limit.num_connections, home->limit.max_connections);
5002 PTHREAD_MUTEX_LOCK(&proxy_mutex);
5003 fr_packet_list_walk(proxy_list, this, eol_proxy_listener);
5005 if (!fr_packet_list_socket_del(proxy_list, this->fd)) {
5006 ERROR("Fatal error removing socket %s: %s",
5007 buffer, fr_strerror());
5010 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
5014 INFO(" ... shutting down socket %s", buffer);
5017 * EOL all requests using this socket.
5019 rbtree_walk(pl, RBTREE_DELETE_ORDER, eol_listener, this);
5023 * No child threads, clean it up now.
5027 if (sock->ev) fr_event_delete(el, &sock->ev);
5033 * Wait until all requests using this socket are done.
5035 gettimeofday(&when, NULL);
5039 if (!fr_event_insert(el, listener_free_cb, this, &when,
5041 rad_panic("Failed to insert event");
5044 #endif /* WITH_TCP */
5049 /***********************************************************************
5053 ***********************************************************************/
5055 static void handle_signal_self(int flag)
5059 if ((flag & (RADIUS_SIGNAL_SELF_EXIT | RADIUS_SIGNAL_SELF_TERM)) != 0) {
5060 if ((flag & RADIUS_SIGNAL_SELF_EXIT) != 0) {
5061 INFO("Signalled to exit");
5062 fr_event_loop_exit(el, 1);
5064 INFO("Signalled to terminate");
5065 fr_event_loop_exit(el, 2);
5069 } /* else exit/term flags weren't set */
5072 * Tell the even loop to stop processing.
5074 if ((flag & RADIUS_SIGNAL_SELF_HUP) != 0) {
5076 static time_t last_hup = 0;
5079 if ((int) (when - last_hup) < 5) {
5080 INFO("Ignoring HUP (less than 5s since last one)");
5084 INFO("Received HUP signal");
5088 exec_trigger(NULL, NULL, "server.signal.hup", true);
5089 fr_event_loop_exit(el, 0x80);
5092 #if defined(WITH_DETAIL) && !defined(WITH_DETAIL_THREAD)
5093 if ((flag & RADIUS_SIGNAL_SELF_DETAIL) != 0) {
5097 * FIXME: O(N) loops suck.
5099 for (this = main_config.listen;
5101 this = this->next) {
5102 if (this->type != RAD_LISTEN_DETAIL) continue;
5105 * This one didn't send the signal, skip
5108 if (!this->decode(this, NULL)) continue;
5111 * Go service the interrupt.
5113 event_poll_detail(this);
5118 #if defined(WITH_TCP) && defined(WITH_PROXY) && defined(HAVE_PTHREAD_H)
5120 * There are new listeners in the list. Run
5121 * event_new_fd() on them.
5123 if ((flag & RADIUS_SIGNAL_SELF_NEW_FD) != 0) {
5124 rad_listen_t *this, *next;
5126 FD_MUTEX_LOCK(&fd_mutex);
5129 * FIXME: unlock the mutex before calling
5132 for (this = new_listeners; this != NULL; this = next) {
5139 new_listeners = NULL;
5140 FD_MUTEX_UNLOCK(&fd_mutex);
5145 #ifndef HAVE_PTHREAD_H
5146 void radius_signal_self(int flag)
5148 return handle_signal_self(flag);
5152 static int self_pipe[2] = { -1, -1 };
5155 * Inform ourselves that we received a signal.
5157 void radius_signal_self(int flag)
5163 * The read MUST be non-blocking for this to work.
5165 rcode = read(self_pipe[0], buffer, sizeof(buffer));
5169 for (i = 0; i < rcode; i++) {
5170 buffer[0] |= buffer[i];
5178 if (write(self_pipe[1], buffer, 1) < 0) fr_exit(0);
5182 static void event_signal_handler(UNUSED fr_event_list_t *xel,
5183 UNUSED int fd, UNUSED void *ctx)
5188 rcode = read(self_pipe[0], buffer, sizeof(buffer));
5189 if (rcode <= 0) return;
5192 * Merge pending signals.
5194 for (i = 0; i < rcode; i++) {
5195 buffer[0] |= buffer[i];
5198 handle_signal_self(buffer[0]);
5200 #endif /* HAVE_PTHREAD_H */
5202 /***********************************************************************
5204 * Bootstrapping code.
5206 ***********************************************************************/
5209 * Externally-visibly functions.
5211 int radius_event_init(TALLOC_CTX *ctx) {
5212 el = fr_event_list_create(ctx, event_status);
5218 static int packet_entry_cmp(void const *one, void const *two)
5220 RADIUS_PACKET const * const *a = one;
5221 RADIUS_PACKET const * const *b = two;
5223 return fr_packet_cmp(*a, *b);
5228 * They haven't defined a proxy listener. Automatically
5229 * add one for them, with the correct address family.
5231 static void create_default_proxy_listener(int af)
5235 listen_socket_t *sock;
5238 memset(&home, 0, sizeof(home));
5241 * Open a default UDP port
5243 home.proto = IPPROTO_UDP;
5247 * Set the address family.
5249 home.src_ipaddr.af = af;
5250 home.ipaddr.af = af;
5253 * Get the correct listener.
5255 this = proxy_new_listener(proxy_ctx, &home, port);
5261 if (!fr_packet_list_socket_add(proxy_list, this->fd,
5263 &sock->other_ipaddr, sock->other_port,
5265 ERROR("Failed adding proxy socket");
5270 * Insert the FD into list of FDs to listen on.
5272 radius_update_listener(this);
5276 * See if we automatically need to open a proxy socket.
5278 static void check_proxy(rad_listen_t *head)
5281 bool has_v4, has_v6;
5284 if (check_config) return;
5285 if (!main_config.proxy_requests) return;
5287 if (!home_servers_udp) return;
5290 * We passed "-i" on the command line. Use that address
5291 * family for the proxy socket.
5293 if (main_config.myip.af != AF_UNSPEC) {
5294 create_default_proxy_listener(main_config.myip.af);
5298 defined_proxy = has_v4 = has_v6 = false;
5301 * Figure out if we need to open a proxy socket, and if
5304 for (this = head; this != NULL; this = this->next) {
5305 listen_socket_t *sock;
5307 switch (this->type) {
5308 case RAD_LISTEN_PROXY:
5309 defined_proxy = true;
5312 case RAD_LISTEN_AUTH:
5314 case RAD_LISTEN_ACCT:
5317 case RAD_LISTEN_COA:
5320 if (sock->my_ipaddr.af == AF_INET) has_v4 = true;
5321 if (sock->my_ipaddr.af == AF_INET6) has_v6 = true;
5330 * Assume they know what they're doing.
5332 if (defined_proxy) return;
5334 if (has_v4) create_default_proxy_listener(AF_INET);
5336 if (has_v6) create_default_proxy_listener(AF_INET6);
5340 int radius_event_start(CONF_SECTION *cs, bool have_children)
5342 rad_listen_t *head = NULL;
5344 if (fr_start_time != (time_t)-1) return 0;
5346 time(&fr_start_time);
5348 if (!check_config) {
5350 * radius_event_init() must be called first
5354 pl = rbtree_create(NULL, packet_entry_cmp, NULL, 0);
5355 if (!pl) return 0; /* leak el */
5358 request_num_counter = 0;
5361 if (main_config.proxy_requests && !check_config) {
5363 * Create the tree for managing proxied requests and
5366 proxy_list = fr_packet_list_create(1);
5367 if (!proxy_list) return 0;
5369 #ifdef HAVE_PTHREAD_H
5370 if (pthread_mutex_init(&proxy_mutex, NULL) != 0) {
5371 ERROR("FATAL: Failed to initialize proxy mutex: %s",
5372 fr_syserror(errno));
5378 * The "init_delay" is set to "response_window".
5379 * Reset it to half of "response_window" in order
5380 * to give the event loop enough time to service
5381 * the event before hitting "response_window".
5383 main_config.init_delay.tv_usec += (main_config.init_delay.tv_sec & 0x01) * USEC;
5384 main_config.init_delay.tv_usec >>= 1;
5385 main_config.init_delay.tv_sec >>= 1;
5387 proxy_ctx = talloc_init("proxy");
5392 * Move all of the thread calls to this file?
5394 * It may be best for the mutexes to be in this file...
5396 spawn_flag = have_children;
5398 #ifdef HAVE_PTHREAD_H
5399 NO_SUCH_CHILD_PID = pthread_self(); /* not a child thread */
5402 * Initialize the threads ONLY if we're spawning, AND
5403 * we're running normally.
5405 if (have_children && !check_config &&
5406 (thread_pool_init(cs, &spawn_flag) < 0)) {
5412 DEBUG("%s: #### Skipping IP addresses and Ports ####",
5414 if (listen_init(cs, &head, spawn_flag) < 0) {
5421 #ifdef HAVE_PTHREAD_H
5423 * Child threads need a pipe to signal us, as do the
5426 if (pipe(self_pipe) < 0) {
5427 ERROR("Error opening internal pipe: %s", fr_syserror(errno));
5430 if ((fcntl(self_pipe[0], F_SETFL, O_NONBLOCK) < 0) ||
5431 (fcntl(self_pipe[0], F_SETFD, FD_CLOEXEC) < 0)) {
5432 ERROR("Error setting internal flags: %s", fr_syserror(errno));
5435 if ((fcntl(self_pipe[1], F_SETFL, O_NONBLOCK) < 0) ||
5436 (fcntl(self_pipe[1], F_SETFD, FD_CLOEXEC) < 0)) {
5437 ERROR("Error setting internal flags: %s", fr_syserror(errno));
5440 DEBUG4("Created signal pipe. Read end FD %i, write end FD %i", self_pipe[0], self_pipe[1]);
5442 if (!fr_event_fd_insert(el, 0, self_pipe[0], event_signal_handler, el)) {
5443 ERROR("Failed creating signal pipe handler: %s", fr_strerror());
5448 DEBUG("%s: #### Opening IP addresses and Ports ####", main_config.name);
5451 * The server temporarily switches to an unprivileged
5452 * user very early in the bootstrapping process.
5453 * However, some sockets MAY require privileged access
5454 * (bind to device, or to port < 1024, or to raw
5455 * sockets). Those sockets need to call suid up/down
5456 * themselves around the functions that need a privileged
5459 if (listen_init(cs, &head, spawn_flag) < 0) {
5463 main_config.listen = head;
5470 * At this point, no one has any business *ever* going
5473 rad_suid_down_permanent();
5480 static int proxy_delete_cb(UNUSED void *ctx, void *data)
5482 REQUEST *request = fr_packet2myptr(REQUEST, proxy, data);
5484 VERIFY_REQUEST(request);
5486 request->master_state = REQUEST_STOP_PROCESSING;
5488 #ifdef HAVE_PTHREAD_H
5489 if (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0) return 0;
5493 * If it's queued we can't delete it from the queue.
5495 * Otherwise, it's OK to delete it. Even RUNNING, because
5496 * that will get caught by the check above.
5498 if (request->child_state == REQUEST_QUEUED) return 0;
5500 request->in_proxy_hash = false;
5502 if (!request->in_request_hash) {
5503 request_done(request, FR_ACTION_DONE);
5507 * Delete it from the list.
5514 static int request_delete_cb(UNUSED void *ctx, void *data)
5516 REQUEST *request = fr_packet2myptr(REQUEST, packet, data);
5518 VERIFY_REQUEST(request);
5520 request->master_state = REQUEST_STOP_PROCESSING;
5523 * Not done, or the child thread is still processing it.
5525 if (request->child_state < REQUEST_RESPONSE_DELAY) return 0; /* continue */
5527 #ifdef HAVE_PTHREAD_H
5528 if (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0) return 0;
5532 rad_assert(request->in_proxy_hash == false);
5535 request->in_request_hash = false;
5537 if (request->ev) fr_event_delete(el, &request->ev);
5539 if (main_config.memory_report) {
5540 RDEBUG2("Cleaning up request packet ID %u with timestamp +%d",
5541 request->packet->id,
5542 (unsigned int) (request->timestamp - fr_start_time));
5547 rad_assert(!request->coa->in_proxy_hash);
5551 request_free(request);
5554 * Delete it from the list, and continue;
5560 void radius_event_free(void)
5566 * There are requests in the proxy hash that aren't
5567 * referenced from anywhere else. Remove them first.
5570 fr_packet_list_walk(proxy_list, NULL, proxy_delete_cb);
5574 rbtree_walk(pl, RBTREE_DELETE_ORDER, request_delete_cb, NULL);
5578 * Now that all requests have been marked "please stop",
5579 * ensure that all of the threads have exited.
5581 #ifdef HAVE_PTHREAD_H
5586 * Walk the lists again, ensuring that all
5587 * requests are done.
5589 if (main_config.memory_report) {
5594 fr_packet_list_walk(proxy_list, NULL, proxy_delete_cb);
5595 num = fr_packet_list_num_elements(proxy_list);
5597 ERROR("Proxy list has %d requests still in it.", num);
5602 rbtree_walk(pl, RBTREE_DELETE_ORDER, request_delete_cb, NULL);
5603 num = rbtree_num_elements(pl);
5605 ERROR("Request list has %d requests still in it.", num);
5614 fr_packet_list_free(proxy_list);
5617 if (proxy_ctx) talloc_free(proxy_ctx);
5622 if (debug_condition) talloc_free(debug_condition);
5625 int radius_event_process(void)
5629 return fr_event_loop(el);