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:%i to %s:%i length %zu",
420 received ? "Received" : "Sent",
421 fr_packet_codes[packet->code],
423 inet_ntop(packet->src_ipaddr.af,
424 &packet->src_ipaddr.ipaddr,
425 src_ipaddr, sizeof(src_ipaddr)),
427 inet_ntop(packet->dst_ipaddr.af,
428 &packet->dst_ipaddr.ipaddr,
429 dst_ipaddr, sizeof(dst_ipaddr)),
433 RDEBUG("%s code %i Id %i from %s:%i to %s:%i length %zu",
434 received ? "Received" : "Sent",
437 inet_ntop(packet->src_ipaddr.af,
438 &packet->src_ipaddr.ipaddr,
439 src_ipaddr, sizeof(src_ipaddr)),
441 inet_ntop(packet->dst_ipaddr.af,
442 &packet->dst_ipaddr.ipaddr,
443 dst_ipaddr, sizeof(dst_ipaddr)),
449 rdebug_pair_list(L_DBG_LVL_1, request, packet->vps, NULL);
451 rdebug_proto_pair_list(L_DBG_LVL_1, request, packet->vps);
456 /***********************************************************************
458 * Start of RADIUS server state machine.
460 ***********************************************************************/
462 static struct timeval *request_response_window(REQUEST *request)
464 VERIFY_REQUEST(request);
466 if (request->client) {
468 * The client hasn't set the response window. Return
469 * either the home server one, if set, or the global one.
471 if (!timerisset(&request->client->response_window)) {
472 return &request->home_server->response_window;
475 if (timercmp(&request->client->response_window,
476 &request->home_server->response_window, <)) {
477 return &request->client->response_window;
481 rad_assert(request->home_server != NULL);
482 return &request->home_server->response_window;
486 * Determine initial request processing delay.
488 static int request_init_delay(REQUEST *request)
490 struct timeval half_response_window;
492 VERIFY_REQUEST(request);
494 /* Allow client response window to lower initial delay */
495 if (timerisset(&request->client->response_window)) {
496 half_response_window.tv_sec = request->client->response_window.tv_sec >> 1;
497 half_response_window.tv_usec =
498 ((request->client->response_window.tv_sec & 1) * USEC +
499 request->client->response_window.tv_usec) >> 1;
500 if (timercmp(&half_response_window, &request->root->init_delay, <))
501 return (int)half_response_window.tv_sec * USEC +
502 (int)half_response_window.tv_usec;
505 return (int)request->root->init_delay.tv_sec * USEC +
506 (int)request->root->init_delay.tv_usec;
510 * Callback for ALL timer events related to the request.
512 static void request_timer(void *ctx)
514 REQUEST *request = talloc_get_type_abort(ctx, REQUEST);
517 action = request->timer_action;
521 request->process(request, action);
525 * Wrapper for talloc pools. If there's no parent, just free the
526 * request. If there is a parent, free the parent INSTEAD of the
529 static void request_free(REQUEST *request)
533 rad_assert(request->ev == NULL);
534 rad_assert(!request->in_request_hash);
535 rad_assert(!request->in_proxy_hash);
537 if ((request->options & RAD_REQUEST_OPTION_CTX) == 0) {
538 talloc_free(request);
542 ptr = talloc_parent(request);
543 rad_assert(ptr != NULL);
549 static void proxy_reply_too_late(REQUEST *request)
553 RDEBUG2("Reply from home server %s port %d - ID: %d arrived too late. Try increasing 'retry_delay' or 'max_request_time'",
554 inet_ntop(request->proxy->dst_ipaddr.af,
555 &request->proxy->dst_ipaddr.ipaddr,
556 buffer, sizeof(buffer)),
557 request->proxy->dst_port, request->proxy->id);
562 /** Mark a request DONE and clean it up.
564 * When a request is DONE, it can have ties to a number of other
565 * portions of the server. The request hash, proxy hash, events,
566 * child threads, etc. This function takes care of either cleaning
567 * up the request, or managing the timers to wait for the ties to be
572 * done -> done [ label = "still running" ];
576 static void request_done(REQUEST *request, int action)
578 struct timeval now, when;
580 VERIFY_REQUEST(request);
585 * Force this no matter what.
587 request->process = request_done;
591 * Tell the detail listener that we're done.
593 if (request->listener &&
594 (request->listener->type == RAD_LISTEN_DETAIL) &&
595 (request->simul_max != 1)) {
596 request->simul_max = 1;
597 request->listener->send(request->listener,
602 #ifdef HAVE_PTHREAD_H
604 * If called from a child thread, mark ourselves as done,
605 * and wait for the master thread timer to clean us up.
607 if (!we_are_master()) {
609 request->child_state = REQUEST_DONE;
615 * Mark the request as STOP.
617 request->master_state = REQUEST_STOP_PROCESSING;
621 * Move the CoA request to its own handler.
624 coa_separate(request->coa);
625 } else if (request->parent && (request->parent->coa == request)) {
626 coa_separate(request);
631 * It doesn't hurt to send duplicate replies. All other
632 * signals are ignored, as the request will be cleaned up
638 rad_assert(request->listener != NULL);
640 if (request->reply->code != 0) {
641 request->listener->send(request->listener, request);
644 RDEBUG("No reply. Ignoring retransmit");
649 * Mark the request as done.
652 #ifdef HAVE_PTHREAD_H
654 * If the child is still running, leave it alone.
656 if (spawn_flag && (request->child_state <= REQUEST_RUNNING)) {
661 #ifdef DEBUG_STATE_MACHINE
662 if (rad_debug_lvl) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n",
663 request->number, __FUNCTION__,
664 child_state_names[request->child_state],
665 child_state_names[REQUEST_DONE]);
667 request->child_state = REQUEST_DONE;
671 * Called when the child is taking too long to
672 * finish. We've already marked it "please
673 * stop", so we don't complain any more.
675 case FR_ACTION_TIMER:
679 case FR_ACTION_PROXY_REPLY:
680 proxy_reply_too_late(request);
689 * Remove it from the request hash.
691 if (request->in_request_hash) {
692 if (!rbtree_deletebydata(pl, &request->packet)) {
695 request->in_request_hash = false;
700 * Wait for the proxy ID to expire. This allows us to
701 * avoid re-use of proxy IDs for a while.
703 if (request->in_proxy_hash) {
704 rad_assert(request->proxy != NULL);
706 fr_event_now(el, &now);
707 when = request->proxy->timestamp;
710 if (((request->proxy->code == PW_CODE_COA_REQUEST) ||
711 (request->proxy->code == PW_CODE_DISCONNECT_REQUEST)) &&
712 (request->packet->code != request->proxy->code)) {
713 when.tv_sec += request->home_server->coa_mrd;
716 timeradd(&when, request_response_window(request), &when);
719 * We haven't received all responses, AND there's still
720 * time to wait. Do so.
722 if ((request->num_proxied_requests > request->num_proxied_responses) &&
724 (request->home_server->proto != IPPROTO_TCP) &&
726 timercmp(&now, &when, <)) {
727 RDEBUG("Waiting for more responses from the home server");
734 remove_from_proxy_hash(request);
738 #ifdef HAVE_PTHREAD_H
740 * If there's no children, we can mark the request as done.
742 if (!spawn_flag) request->child_state = REQUEST_DONE;
746 * If the child is still running, wait for it to be finished.
748 if (request->child_state <= REQUEST_RUNNING) {
749 gettimeofday(&now, NULL);
754 if (request->delay < (USEC / 3)) request->delay = USEC / 3;
755 tv_add(&when, request->delay);
756 request->delay += request->delay >> 1;
757 if (request->delay > (10 * USEC)) request->delay = 10 * USEC;
759 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
763 #ifdef HAVE_PTHREAD_H
764 rad_assert(request->child_pid == NO_SUCH_CHILD_PID);
768 * @todo: do final states for TCP sockets, too?
770 request_stats_final(request);
772 if (request->listener) {
773 request->listener->count--;
776 * If we're the last one, remove the listener now.
778 if ((request->listener->count == 0) &&
779 (request->listener->status >= RAD_LISTEN_STATUS_FROZEN)) {
780 event_new_fd(request->listener);
785 if (request->packet) {
786 RDEBUG2("Cleaning up request packet ID %u with timestamp +%d",
788 (unsigned int) (request->timestamp - fr_start_time));
789 } /* else don't print anything */
792 fr_event_delete(el, &request->ev);
793 request_free(request);
797 static void request_cleanup_delay_init(REQUEST *request)
799 struct timeval now, when;
801 VERIFY_REQUEST(request);
804 * Do cleanup delay ONLY for RADIUS packets from a real
805 * client. Everything else just gets cleaned up
808 if (!(request->packet->code == PW_CODE_ACCESS_REQUEST)
810 || (request->packet->code == PW_CODE_COA_REQUEST)
811 || (request->packet->code == PW_CODE_DISCONNECT_REQUEST)
815 if (!request->root->cleanup_delay) goto done;
817 gettimeofday(&now, NULL);
819 rad_assert(request->reply->timestamp.tv_sec != 0);
820 when = request->reply->timestamp;
822 request->delay = request->root->cleanup_delay;
823 when.tv_sec += request->delay;
826 * Set timer for when we need to clean it up.
828 if (timercmp(&when, &now, >)) {
829 #ifdef DEBUG_STATE_MACHINE
830 if (rad_debug_lvl) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_cleanup_delay");
832 request->process = request_cleanup_delay;
833 request->child_state = REQUEST_CLEANUP_DELAY;
836 * Update this if we can, otherwise let the timers pick it up.
838 if (we_are_master()) {
839 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
847 * Otherwise just clean it up.
850 request_done(request, FR_ACTION_DONE);
855 * Enforce max_request_time.
857 static void request_max_time(REQUEST *request)
859 struct timeval now, when;
860 rad_assert(request->magic == REQUEST_MAGIC);
861 #ifdef DEBUG_STATE_MACHINE
862 int action = FR_ACTION_TIMER;
865 VERIFY_REQUEST(request);
871 * The child thread has acknowledged it's done.
872 * Transition to the DONE state.
874 * If the request was marked STOP, then the "check for
875 * stop" macro already took care of it.
877 if (request->child_state == REQUEST_DONE) {
879 request_done(request, FR_ACTION_DONE);
884 * The request is still running. Enforce max_request_time.
886 fr_event_now(el, &now);
887 when = request->packet->timestamp;
888 when.tv_sec += request->root->max_request_time;
891 * Taking too long: tell it to die.
893 if (timercmp(&now, &when, >=)) {
894 #ifdef HAVE_PTHREAD_H
896 * If there's a child thread processing it,
900 (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0)) {
901 ERROR("Unresponsive child for request %u, in component %s module %s",
903 request->component ? request->component : "<core>",
904 request->module ? request->module : "<core>");
905 exec_trigger(request, NULL, "server.thread.unresponsive", true);
909 * Tell the request that it's done.
915 * Sleep for some more. We HOPE that the child will
916 * become responsive at some point in the future. We do
917 * this by adding 50% to the current timer.
920 tv_add(&when, request->delay);
921 request->delay += request->delay >> 1;
922 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
925 static void request_queue_or_run(REQUEST *request,
926 fr_request_process_t process)
928 #ifdef DEBUG_STATE_MACHINE
929 int action = FR_ACTION_TIMER;
932 VERIFY_REQUEST(request);
937 * Do this here so that fewer other functions need to do
940 if (request->master_state == REQUEST_STOP_PROCESSING) {
941 #ifdef DEBUG_STATE_MACHINE
942 if (rad_debug_lvl) printf("(%u) ********\tSTATE %s M-%s causes C-%s-> C-%s\t********\n",
943 request->number, __FUNCTION__,
944 master_state_names[request->master_state],
945 child_state_names[request->child_state],
946 child_state_names[REQUEST_DONE]);
948 request_done(request, FR_ACTION_DONE);
952 request->process = process;
954 if (we_are_master()) {
958 * (re) set the initial delay.
960 request->delay = request_init_delay(request);
961 if (request->delay > USEC) request->delay = USEC;
962 gettimeofday(&when, NULL);
963 tv_add(&when, request->delay);
964 request->delay += request->delay >> 1;
966 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
968 #ifdef HAVE_PTHREAD_H
971 * A child thread will eventually pick it up.
973 if (request_enqueue(request)) return;
976 * Otherwise we're not going to do anything with
979 request_done(request, FR_ACTION_DONE);
985 request->child_state = REQUEST_RUNNING;
986 request->process(request, FR_ACTION_RUN);
990 * Requests that care about child process exit
991 * codes have already either called
992 * rad_waitpid(), or they've given up.
994 while (waitpid(-1, NULL, WNOHANG) > 0);
999 static void request_dup(REQUEST *request)
1001 ERROR("(%u) Ignoring duplicate packet from "
1002 "client %s port %d - ID: %u due to unfinished request "
1003 "in component %s module %s",
1004 request->number, request->client->shortname,
1005 request->packet->src_port,request->packet->id,
1006 request->component, request->module);
1010 /** Sit on a request until it's time to clean it up.
1012 * A NAS may not see a response from the server. When the NAS
1013 * retransmits, we want to be able to send a cached reply back. The
1014 * alternative is to re-process the packet, which does bad things for
1015 * EAP, among others.
1017 * IF we do see a NAS retransmit, we extend the cleanup delay,
1018 * because the NAS might miss our cached reply.
1020 * Otherwise, once we reach cleanup_delay, we transition to DONE.
1023 * digraph cleanup_delay {
1025 * send_reply [ label = "send_reply\nincrease cleanup delay" ];
1027 * cleanup_delay -> send_reply [ label = "DUP" ];
1028 * send_reply -> cleanup_delay;
1029 * cleanup_delay -> proxy_reply_too_late [ label = "PROXY_REPLY", arrowhead = "none" ];
1030 * cleanup_delay -> cleanup_delay [ label = "TIMER < timeout" ];
1031 * cleanup_delay -> done [ label = "TIMER >= timeout" ];
1035 static void request_cleanup_delay(REQUEST *request, int action)
1037 struct timeval when, now;
1039 VERIFY_REQUEST(request);
1041 TRACE_STATE_MACHINE;
1048 if (request->reply->code != 0) {
1049 request->listener->send(request->listener, request);
1051 RDEBUG("No reply. Ignoring retransmit");
1055 * Double the cleanup_delay to catch retransmits.
1057 when = request->reply->timestamp;
1058 request->delay += request->delay;
1059 when.tv_sec += request->delay;
1061 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
1065 case FR_ACTION_PROXY_REPLY:
1066 proxy_reply_too_late(request);
1070 case FR_ACTION_TIMER:
1071 fr_event_now(el, &now);
1073 rad_assert(request->root->cleanup_delay > 0);
1075 when = request->reply->timestamp;
1076 when.tv_sec += request->root->cleanup_delay;
1078 if (timercmp(&when, &now, >)) {
1079 #ifdef DEBUG_STATE_MACHINE
1080 if (rad_debug_lvl) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_cleanup_delay");
1082 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
1084 } /* else it's time to clean up */
1086 request_done(request, REQUEST_DONE);
1090 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
1096 /** Sit on a request until it's time to respond to it.
1098 * For security reasons, rejects (and maybe some other) packets are
1099 * delayed for a while before we respond. This delay means that
1100 * badly behaved NASes don't hammer the server with authentication
1103 * Otherwise, once we reach response_delay, we send the reply, and
1104 * transition to cleanup_delay.
1107 * digraph response_delay {
1108 * response_delay -> proxy_reply_too_late [ label = "PROXY_REPLY", arrowhead = "none" ];
1109 * response_delay -> response_delay [ label = "DUP, TIMER < timeout" ];
1110 * response_delay -> send_reply [ label = "TIMER >= timeout" ];
1111 * send_reply -> cleanup_delay;
1115 static void request_response_delay(REQUEST *request, int action)
1117 struct timeval when, now;
1119 VERIFY_REQUEST(request);
1121 TRACE_STATE_MACHINE;
1128 ERROR("(%u) Discarding duplicate request from "
1129 "client %s port %d - ID: %u due to delayed response",
1130 request->number, request->client->shortname,
1131 request->packet->src_port,request->packet->id);
1135 case FR_ACTION_PROXY_REPLY:
1136 proxy_reply_too_late(request);
1140 case FR_ACTION_TIMER:
1141 fr_event_now(el, &now);
1143 rad_assert(request->response_delay.tv_sec > 0);
1146 * See if it's time to send the reply. If not,
1147 * we wait some more.
1149 when = request->reply->timestamp;
1151 tv_add(&when, request->response_delay.tv_sec * USEC);
1152 tv_add(&when, request->response_delay.tv_usec);
1154 if (timercmp(&when, &now, >)) {
1155 #ifdef DEBUG_STATE_MACHINE
1156 if (rad_debug_lvl) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_response_delay");
1158 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
1160 } /* else it's time to send the reject */
1162 RDEBUG2("Sending delayed response");
1163 debug_packet(request, request->reply, false);
1164 request->listener->send(request->listener, request);
1167 * Clean up the request.
1169 request_cleanup_delay_init(request);
1173 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
1179 static int request_pre_handler(REQUEST *request, UNUSED int action)
1183 VERIFY_REQUEST(request);
1185 TRACE_STATE_MACHINE;
1187 if (request->master_state == REQUEST_STOP_PROCESSING) return 0;
1190 * Don't decode the packet if it's an internal "fake"
1191 * request. Instead, just return so that the caller can
1194 if (request->packet->dst_port == 0) {
1195 request->username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1196 request->password = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
1200 if (!request->packet->vps) { /* FIXME: check for correct state */
1201 rcode = request->listener->decode(request->listener, request);
1204 if (debug_condition) {
1206 * Ignore parse errors.
1208 if (radius_evaluate_cond(request, RLM_MODULE_OK, 0, debug_condition)) {
1209 request->log.lvl = L_DBG_LVL_2;
1210 request->log.func = vradlog_request;
1215 debug_packet(request, request->packet, true);
1221 RATE_LIMIT(INFO("Dropping packet without response because of error: %s", fr_strerror()));
1222 request->reply->offset = -2; /* bad authenticator */
1226 if (!request->username) {
1227 request->username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1234 /** Do the final processing of a request before we reply to the NAS.
1236 * Various cleanups, suppress responses, copy Proxy-State, and set
1237 * response_delay or cleanup_delay;
1239 static void request_finish(REQUEST *request, int action)
1243 VERIFY_REQUEST(request);
1245 TRACE_STATE_MACHINE;
1248 (void) action; /* -Wunused */
1252 * Don't do post-auth if we're a CoA request originated
1253 * from an Access-Request. See request_alloc_coa() for
1256 if ((request->options & RAD_REQUEST_OPTION_COA) != 0) goto done;
1260 * Override the response code if a control:Response-Packet-Type attribute is present.
1262 vp = pairfind(request->config, PW_RESPONSE_PACKET_TYPE, 0, TAG_ANY);
1264 if (vp->vp_integer == 256) {
1265 RDEBUG2("Not responding to request");
1266 request->reply->code = 0;
1268 request->reply->code = vp->vp_integer;
1272 * Catch Auth-Type := Reject BEFORE proxying the packet.
1274 else if (request->packet->code == PW_CODE_ACCESS_REQUEST) {
1275 if (request->reply->code == 0) {
1276 vp = pairfind(request->config, PW_AUTH_TYPE, 0, TAG_ANY);
1277 if (!vp || (vp->vp_integer != 5)) {
1278 RDEBUG2("There was no response configured: "
1279 "rejecting request");
1282 request->reply->code = PW_CODE_ACCESS_REJECT;
1287 * Copy Proxy-State from the request to the reply.
1289 vp = paircopy_by_num(request->reply, request->packet->vps,
1290 PW_PROXY_STATE, 0, TAG_ANY);
1291 if (vp) pairadd(&request->reply->vps, vp);
1294 * Call Post-Auth for Access-Request packets.
1296 if (request->packet->code == PW_CODE_ACCESS_REQUEST) {
1297 rad_postauth(request);
1302 * Clean up. These are no longer needed.
1304 gettimeofday(&request->reply->timestamp, NULL);
1307 * Fake packets get marked as "done", and have the
1308 * proxy-reply section deal with the reply attributes.
1309 * We therefore don't free the reply attributes.
1311 if (request->packet->dst_port == 0) {
1312 RDEBUG("Finished internally proxied request.");
1314 request->child_state = REQUEST_DONE;
1320 * Always send the reply to the detail listener.
1322 if (request->listener->type == RAD_LISTEN_DETAIL) {
1323 request->simul_max = 1;
1326 * But only print the reply if there is one.
1328 if (request->reply->code != 0) {
1329 debug_packet(request, request->reply, false);
1332 request->listener->send(request->listener, request);
1338 * Ignore all "do not respond" packets.
1339 * Except for the detail ones, which need to ping
1340 * the detail file reader so that it will retransmit.
1342 if (!request->reply->code) {
1343 RDEBUG("Not sending reply to client.");
1348 * If it's not in the request hash, we MIGHT not want to
1351 * If duplicate packets are allowed, then then only
1352 * reason to NOT be in the request hash is because we
1353 * don't want to send a reply.
1355 * FIXME: this is crap. The rest of the state handling
1356 * should use a different field so that we don't have two
1359 * Otherwise duplicates are forbidden, and the request is
1360 * SUPPOSED to avoid the request hash.
1362 * In that case, we need to send a reply.
1364 if (!request->in_request_hash &&
1365 !request->listener->nodup) {
1366 RDEBUG("Suppressing reply to client.");
1371 * See if we need to delay an Access-Reject packet.
1373 if ((request->reply->code == PW_CODE_ACCESS_REJECT) &&
1374 (request->root->reject_delay.tv_sec > 0)) {
1375 request->response_delay = request->root->reject_delay;
1379 * If we timed out a proxy packet, don't delay
1380 * the reject any more.
1382 if (request->proxy && !request->proxy_reply) {
1383 request->response_delay.tv_sec = 0;
1384 request->response_delay.tv_usec = 0;
1392 if (request->response_delay.tv_sec == 0) {
1393 rad_assert(request->response_delay.tv_usec == 0);
1396 * Don't print a reply if there's none to send.
1398 if (request->reply->code != 0) {
1399 debug_packet(request, request->reply, false);
1400 request->listener->send(request->listener, request);
1404 RDEBUG2("Finished request");
1405 request->component = "<core>";
1406 request->module = "<done>";
1408 request_cleanup_delay_init(request);
1412 * Encode and sign it here, so that the master
1413 * thread can just send the encoded data, which
1414 * means it does less work.
1416 RDEBUG2("Delaying response for %d.%06d seconds",
1417 (int) request->response_delay.tv_sec, (int) request->response_delay.tv_usec);
1418 request->listener->encode(request->listener, request);
1419 request->component = "<core>";
1420 request->module = "<delay>";
1421 request->process = request_response_delay;
1423 request->child_state = REQUEST_RESPONSE_DELAY;
1427 /** Process a request from a client.
1429 * The outcome might be that the request is proxied.
1433 * running -> running [ label = "TIMER < max_request_time" ];
1434 * running -> done [ label = "TIMER >= max_request_time" ];
1435 * running -> proxy [ label = "proxied" ];
1436 * running -> dup [ label = "DUP", arrowhead = "none" ];
1440 static void request_running(REQUEST *request, int action)
1442 VERIFY_REQUEST(request);
1444 TRACE_STATE_MACHINE;
1448 case FR_ACTION_TIMER:
1450 request_max_time(request);
1454 request_dup(request);
1458 if (!request_pre_handler(request, action)) {
1459 #ifdef DEBUG_STATE_MACHINE
1460 if (rad_debug_lvl) printf("(%u) ********\tSTATE %s failed in pre-handler C-%s -> C-%s\t********\n",
1461 request->number, __FUNCTION__,
1462 child_state_names[request->child_state],
1463 child_state_names[REQUEST_DONE]);
1467 request->child_state = REQUEST_DONE;
1471 rad_assert(request->handle != NULL);
1472 request->handle(request);
1476 * We may need to send a proxied request.
1478 if ((action == FR_ACTION_RUN) &&
1479 request_will_proxy(request)) {
1480 #ifdef DEBUG_STATE_MACHINE
1481 if (rad_debug_lvl) printf("(%u) ********\tWill Proxy\t********\n", request->number);
1485 * takes care of setting
1486 * up the post proxy fail
1489 if (request_proxy(request, 0) < 0) goto req_finished;
1493 #ifdef DEBUG_STATE_MACHINE
1494 if (rad_debug_lvl) printf("(%u) ********\tFinished\t********\n", request->number);
1499 * Maybe originate a CoA request.
1501 if ((action == FR_ACTION_RUN) && request->coa) {
1502 request_coa_originate(request);
1509 request_finish(request, action);
1514 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
1519 int request_receive(TALLOC_CTX *ctx, rad_listen_t *listener, RADIUS_PACKET *packet,
1520 RADCLIENT *client, RAD_REQUEST_FUNP fun)
1523 RADIUS_PACKET **packet_p;
1524 REQUEST *request = NULL;
1526 listen_socket_t *sock = NULL;
1528 VERIFY_PACKET(packet);
1531 * Set the last packet received.
1533 gettimeofday(&now, NULL);
1535 packet->timestamp = now;
1537 #ifdef WITH_ACCOUNTING
1538 if (listener->type != RAD_LISTEN_DETAIL)
1541 sock = listener->data;
1542 sock->last_packet = now.tv_sec;
1546 * Skip everything if required.
1548 if (listener->nodup) goto skip_dup;
1550 packet_p = rbtree_finddata(pl, &packet);
1552 rad_child_state_t child_state;
1554 request = fr_packet2myptr(REQUEST, packet, packet_p);
1555 rad_assert(request->in_request_hash);
1556 child_state = request->child_state;
1559 * Same src/dst ip/port, length, and
1560 * authentication vector: must be a duplicate.
1562 if ((request->packet->data_len == packet->data_len) &&
1563 (memcmp(request->packet->vector, packet->vector,
1564 sizeof(packet->vector)) == 0)) {
1567 switch (packet->code) {
1568 case PW_CODE_ACCESS_REQUEST:
1569 FR_STATS_INC(auth, total_dup_requests);
1572 #ifdef WITH_ACCOUNTING
1573 case PW_CODE_ACCOUNTING_REQUEST:
1574 FR_STATS_INC(acct, total_dup_requests);
1578 case PW_CODE_COA_REQUEST:
1579 FR_STATS_INC(coa, total_dup_requests);
1582 case PW_CODE_DISCONNECT_REQUEST:
1583 FR_STATS_INC(dsc, total_dup_requests);
1590 #endif /* WITH_STATS */
1593 * Tell the state machine that there's a
1594 * duplicate request.
1596 request->process(request, FR_ACTION_DUP);
1597 return 0; /* duplicate of live request */
1601 * Mark the request as done ASAP, and before we
1602 * log anything. The child may stop processing
1603 * the request just as we're logging the
1606 request_done(request, FR_ACTION_DONE);
1610 * It's a new request, not a duplicate. If the
1611 * old one is done, then we can clean it up.
1613 if (child_state <= REQUEST_RUNNING) {
1615 * The request is still QUEUED or RUNNING. That's a problem.
1617 ERROR("Received conflicting packet from "
1618 "client %s port %d - ID: %u due to "
1619 "unfinished request. Giving up on old request.",
1621 packet->src_port, packet->id);
1625 * Mark the old request as done. If there's no
1626 * child, the request will be cleaned up
1627 * immediately. If there is a child, we'll set a
1628 * timer to go clean up the request.
1630 } /* else the new packet is unique */
1633 * Quench maximum number of outstanding requests.
1635 if (main_config.max_requests &&
1636 ((count = rbtree_num_elements(pl)) > main_config.max_requests)) {
1637 RATE_LIMIT(ERROR("Dropping request (%d is too many): from client %s port %d - ID: %d", count,
1639 packet->src_port, packet->id);
1640 WARN("Please check the configuration file.\n"
1641 "\tThe value for 'max_requests' is probably set too low.\n"));
1643 exec_trigger(NULL, NULL, "server.max_requests", true);
1649 * Rate-limit the incoming packets
1651 if (sock && sock->max_rate) {
1654 pps = rad_pps(&sock->rate_pps_old, &sock->rate_pps_now, &sock->rate_time, &now);
1655 if (pps > sock->max_rate) {
1656 DEBUG("Dropping request due to rate limiting");
1659 sock->rate_pps_now++;
1663 * Allocate a pool for the request.
1666 ctx = talloc_pool(NULL, main_config.talloc_pool_size);
1670 * The packet is still allocated from a different
1671 * context, but oh well.
1673 (void) talloc_steal(ctx, packet);
1676 request = request_setup(ctx, listener, packet, client, fun);
1683 * Mark it as a "real" request with a context.
1685 request->options |= RAD_REQUEST_OPTION_CTX;
1688 * Remember the request in the list.
1690 if (!listener->nodup) {
1691 if (!rbtree_insert(pl, &request->packet)) {
1692 RERROR("Failed to insert request in the list of live requests: discarding it");
1693 request_done(request, FR_ACTION_DONE);
1697 request->in_request_hash = true;
1701 * Process it. Send a response, and free it.
1703 if (listener->synchronous) {
1705 rad_assert(listener->type != RAD_LISTEN_DETAIL);
1708 request->listener->decode(request->listener, request);
1709 request->username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1710 request->password = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
1714 if (request->reply->code != 0) {
1715 request->listener->send(request->listener, request);
1717 RDEBUG("Not sending reply");
1721 * Don't do delayed reject. Oh well.
1723 request_free(request);
1728 * Otherwise, insert it into the state machine.
1729 * The child threads will take care of processing it.
1731 request_queue_or_run(request, request_running);
1737 static REQUEST *request_setup(TALLOC_CTX *ctx, rad_listen_t *listener, RADIUS_PACKET *packet,
1738 RADCLIENT *client, RAD_REQUEST_FUNP fun)
1743 * Create and initialize the new request.
1745 request = request_alloc(ctx);
1750 request->reply = rad_alloc(request, false);
1751 if (!request->reply) {
1753 talloc_free(request);
1757 request->listener = listener;
1758 request->client = client;
1759 request->packet = talloc_steal(request, packet);
1760 request->number = request_num_counter++;
1761 request->priority = listener->type;
1762 request->master_state = REQUEST_ACTIVE;
1763 request->child_state = REQUEST_RUNNING;
1764 #ifdef DEBUG_STATE_MACHINE
1765 if (rad_debug_lvl) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n",
1766 request->number, __FUNCTION__,
1767 child_state_names[request->child_state],
1768 child_state_names[REQUEST_RUNNING]);
1770 #ifdef HAVE_PTHREAD_H
1771 request->child_pid = NO_SUCH_CHILD_PID;
1773 request->handle = fun;
1777 request->listener->stats.last_packet = request->packet->timestamp.tv_sec;
1778 if (packet->code == PW_CODE_ACCESS_REQUEST) {
1779 request->client->auth.last_packet = request->packet->timestamp.tv_sec;
1780 radius_auth_stats.last_packet = request->packet->timestamp.tv_sec;
1781 #ifdef WITH_ACCOUNTING
1782 } else if (packet->code == PW_CODE_ACCOUNTING_REQUEST) {
1783 request->client->acct.last_packet = request->packet->timestamp.tv_sec;
1784 radius_acct_stats.last_packet = request->packet->timestamp.tv_sec;
1787 #endif /* WITH_STATS */
1790 * Status-Server packets go to the head of the queue.
1792 if (request->packet->code == PW_CODE_STATUS_SERVER) request->priority = 0;
1795 * Set virtual server identity
1797 if (client->server) {
1798 request->server = client->server;
1799 } else if (listener->server) {
1800 request->server = listener->server;
1802 request->server = NULL;
1805 request->root = &main_config;
1807 request->listener->count++;
1811 * The request passes many of our sanity checks.
1812 * From here on in, if anything goes wrong, we
1813 * send a reject message, instead of dropping the
1818 * Build the reply template from the request.
1821 request->reply->sockfd = request->packet->sockfd;
1822 request->reply->dst_ipaddr = request->packet->src_ipaddr;
1823 request->reply->src_ipaddr = request->packet->dst_ipaddr;
1824 request->reply->dst_port = request->packet->src_port;
1825 request->reply->src_port = request->packet->dst_port;
1826 request->reply->id = request->packet->id;
1827 request->reply->code = 0; /* UNKNOWN code */
1828 memcpy(request->reply->vector, request->packet->vector,
1829 sizeof(request->reply->vector));
1830 request->reply->vps = NULL;
1831 request->reply->data = NULL;
1832 request->reply->data_len = 0;
1838 /***********************************************************************
1842 ***********************************************************************/
1845 * Timer function for all TCP sockets.
1847 static void tcp_socket_timer(void *ctx)
1849 rad_listen_t *listener = talloc_get_type_abort(ctx, rad_listen_t);
1850 listen_socket_t *sock = listener->data;
1851 struct timeval end, now;
1853 fr_socket_limit_t *limit;
1857 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return;
1859 fr_event_now(el, &now);
1861 switch (listener->type) {
1863 case RAD_LISTEN_PROXY:
1864 limit = &sock->home->limit;
1868 case RAD_LISTEN_AUTH:
1869 #ifdef WITH_ACCOUNTING
1870 case RAD_LISTEN_ACCT:
1872 limit = &sock->limit;
1880 * If we enforce a lifetime, do it now.
1882 if (limit->lifetime > 0) {
1883 end.tv_sec = sock->opened + limit->lifetime;
1886 if (timercmp(&end, &now, <=)) {
1887 listener->print(listener, buffer, sizeof(buffer));
1888 DEBUG("Reached maximum lifetime on socket %s", buffer);
1894 * Proxy sockets get frozen, so that we don't use
1895 * them for new requests. But we do keep them
1896 * open to listen for replies to requests we had
1899 if (listener->type == RAD_LISTEN_PROXY) {
1900 PTHREAD_MUTEX_LOCK(&proxy_mutex);
1901 if (!fr_packet_list_socket_freeze(proxy_list,
1903 ERROR("Fatal error freezing socket: %s", fr_strerror());
1906 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
1911 * Mark the socket as "don't use if at all possible".
1913 listener->status = RAD_LISTEN_STATUS_FROZEN;
1914 event_new_fd(listener);
1923 * Enforce an idle timeout.
1925 if (limit->idle_timeout > 0) {
1926 struct timeval idle;
1928 rad_assert(sock->last_packet != 0);
1929 idle.tv_sec = sock->last_packet + limit->idle_timeout;
1932 if (timercmp(&idle, &now, <=)) {
1933 listener->print(listener, buffer, sizeof(buffer));
1934 DEBUG("Reached idle timeout on socket %s", buffer);
1939 * Enforce the minimum of idle timeout or lifetime.
1941 if (timercmp(&idle, &end, <)) {
1947 * Wake up at t + 0.5s. The code above checks if the timers
1948 * are <= t. This addition gives us a bit of leeway.
1950 end.tv_usec = USEC / 2;
1953 if (!fr_event_insert(el, tcp_socket_timer, listener, &end, &sock->ev)) {
1954 rad_panic("Failed to insert event");
1961 * Add +/- 2s of jitter, as suggested in RFC 3539
1964 static void add_jitter(struct timeval *when)
1971 jitter ^= (jitter >> 10);
1972 jitter &= ((1 << 22) - 1); /* 22 bits of 1 */
1975 * Add in ~ (4 * USEC) of jitter.
1977 tv_add(when, jitter);
1981 * Called by socket_del to remove requests with this socket
1983 static int eol_proxy_listener(void *ctx, void *data)
1985 rad_listen_t *this = talloc_get_type_abort(ctx, rad_listen_t);
1986 RADIUS_PACKET **proxy_p = data;
1989 request = fr_packet2myptr(REQUEST, proxy, proxy_p);
1990 if (request->proxy_listener != this) return 0;
1993 * The normal "remove_from_proxy_hash" tries to grab the
1994 * proxy mutex. We already have it held, so grabbing it
1995 * again will cause a deadlock. Instead, call the "no
1996 * lock" version of the function.
1998 rad_assert(request->in_proxy_hash == true);
1999 remove_from_proxy_hash_nl(request, false);
2002 * Don't mark it as DONE. The client can retransmit, and
2003 * the packet SHOULD be re-proxied somewhere else.
2005 * Return "2" means that the rbtree code will remove it
2006 * from the tree, and we don't need to do it ourselves.
2010 #endif /* WITH_PROXY */
2012 static int eol_listener(void *ctx, void *data)
2014 rad_listen_t *this = talloc_get_type_abort(ctx, rad_listen_t);
2015 RADIUS_PACKET **packet_p = data;
2018 request = fr_packet2myptr(REQUEST, packet, packet_p);
2019 if (request->listener != this) return 0;
2021 request->master_state = REQUEST_STOP_PROCESSING;
2022 request->process = request_done;
2026 #endif /* WITH_TCP */
2029 /***********************************************************************
2031 * Proxy handlers for the state machine.
2033 ***********************************************************************/
2036 * Called with the proxy mutex held
2038 static void remove_from_proxy_hash_nl(REQUEST *request, bool yank)
2040 VERIFY_REQUEST(request);
2042 if (!request->in_proxy_hash) return;
2044 fr_packet_list_id_free(proxy_list, request->proxy, yank);
2045 request->in_proxy_hash = false;
2048 * On the FIRST reply, decrement the count of outstanding
2049 * requests. Note that this is NOT the count of sent
2050 * packets, but whether or not the home server has
2053 if (request->home_server &&
2054 request->home_server->currently_outstanding) {
2055 request->home_server->currently_outstanding--;
2058 * If we're NOT sending it packets, AND it's been
2059 * a while since we got a response, then we don't
2060 * know if it's alive or dead.
2062 if ((request->home_server->currently_outstanding == 0) &&
2063 (request->home_server->state == HOME_STATE_ALIVE)) {
2064 struct timeval when, now;
2066 when.tv_sec = request->home_server->last_packet_recv ;
2069 timeradd(&when, request_response_window(request), &when);
2070 gettimeofday(&now, NULL);
2073 * last_packet + response_window
2075 * We *administratively* mark the home
2076 * server as "unknown" state, because we
2077 * haven't seen a packet for a while.
2079 if (timercmp(&now, &when, >)) {
2080 request->home_server->state = HOME_STATE_UNKNOWN;
2081 request->home_server->last_packet_sent = 0;
2082 request->home_server->last_packet_recv = 0;
2088 rad_assert(request->proxy_listener != NULL);
2089 request->proxy_listener->count--;
2091 request->proxy_listener = NULL;
2094 * Got from YES in hash, to NO, not in hash while we hold
2095 * the mutex. This guarantees that when another thread
2096 * grabs the mutex, the "not in hash" flag is correct.
2098 RDEBUG3("proxy: request is no longer in proxy hash");
2101 static void remove_from_proxy_hash(REQUEST *request)
2103 VERIFY_REQUEST(request);
2106 * Check this without grabbing the mutex because it's a
2107 * lot faster that way.
2109 if (!request->in_proxy_hash) return;
2112 * The "not in hash" flag is definitive. However, if the
2113 * flag says that it IS in the hash, there might still be
2114 * a race condition where it isn't.
2116 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2118 if (!request->in_proxy_hash) {
2119 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2123 remove_from_proxy_hash_nl(request, true);
2125 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2128 static int insert_into_proxy_hash(REQUEST *request)
2132 bool success = false;
2133 void *proxy_listener;
2135 VERIFY_REQUEST(request);
2137 rad_assert(request->proxy != NULL);
2138 rad_assert(request->home_server != NULL);
2139 rad_assert(proxy_list != NULL);
2142 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2143 proxy_listener = NULL;
2144 request->num_proxied_requests = 1;
2145 request->num_proxied_responses = 0;
2147 for (tries = 0; tries < 2; tries++) {
2149 listen_socket_t *sock;
2151 RDEBUG3("proxy: Trying to allocate ID (%d/2)", tries);
2152 success = fr_packet_list_id_alloc(proxy_list,
2153 request->home_server->proto,
2154 &request->proxy, &proxy_listener);
2157 if (tries > 0) continue; /* try opening new socket only once */
2159 #ifdef HAVE_PTHREAD_H
2160 if (proxy_no_new_sockets) break;
2163 RDEBUG3("proxy: Trying to open a new listener to the home server");
2164 this = proxy_new_listener(proxy_ctx, request->home_server, 0);
2166 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2170 request->proxy->src_port = 0; /* Use any new socket */
2171 proxy_listener = this;
2174 if (!fr_packet_list_socket_add(proxy_list, this->fd,
2176 &sock->other_ipaddr, sock->other_port,
2179 #ifdef HAVE_PTHREAD_H
2180 proxy_no_new_sockets = true;
2182 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2185 * This is bad. However, the
2186 * packet list now supports 256
2187 * open sockets, which should
2188 * minimize this problem.
2190 ERROR("Failed adding proxy socket: %s",
2196 * Add it to the event loop. Ensure that we have
2197 * only one mutex locked at a time.
2199 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2200 radius_update_listener(this);
2201 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2204 if (!proxy_listener || !success) {
2205 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2206 REDEBUG2("proxy: Failed allocating Id for proxied request");
2208 request->proxy_listener = NULL;
2209 request->in_proxy_hash = false;
2213 rad_assert(request->proxy->id >= 0);
2215 request->proxy_listener = proxy_listener;
2216 request->in_proxy_hash = true;
2217 RDEBUG3("proxy: request is now in proxy hash");
2220 * Keep track of maximum outstanding requests to a
2221 * particular home server. 'max_outstanding' is
2222 * enforced in home_server_ldb(), in realms.c.
2224 request->home_server->currently_outstanding++;
2227 request->proxy_listener->count++;
2230 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2232 RDEBUG3("proxy: allocating destination %s port %d - Id %d",
2233 inet_ntop(request->proxy->dst_ipaddr.af,
2234 &request->proxy->dst_ipaddr.ipaddr, buf, sizeof(buf)),
2235 request->proxy->dst_port,
2236 request->proxy->id);
2241 static int process_proxy_reply(REQUEST *request, RADIUS_PACKET *reply)
2244 int post_proxy_type = 0;
2247 VERIFY_REQUEST(request);
2250 * There may be a proxy reply, but it may be too late.
2252 if (!request->home_server->server && !request->proxy_listener) return 0;
2255 * Delete any reply we had accumulated until now.
2257 RDEBUG2("Clearing existing &reply: attributes");
2258 pairfree(&request->reply->vps);
2261 * Run the packet through the post-proxy stage,
2262 * BEFORE playing games with the attributes.
2264 vp = pairfind(request->config, PW_POST_PROXY_TYPE, 0, TAG_ANY);
2266 post_proxy_type = vp->vp_integer;
2268 * If we have a proxy_reply, and it was a reject, or a NAK
2269 * setup Post-Proxy <type>.
2271 * If the <type> doesn't have a section, then the Post-Proxy
2272 * section is ignored.
2275 DICT_VALUE *dval = NULL;
2277 switch (reply->code) {
2278 case PW_CODE_ACCESS_REJECT:
2279 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Reject");
2280 if (dval) post_proxy_type = dval->value;
2283 case PW_CODE_DISCONNECT_NAK:
2284 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, fr_packet_codes[reply->code]);
2285 if (dval) post_proxy_type = dval->value;
2288 case PW_CODE_COA_NAK:
2289 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, fr_packet_codes[reply->code]);
2290 if (dval) post_proxy_type = dval->value;
2298 * Create config:Post-Proxy-Type
2301 vp = radius_paircreate(request, &request->config, PW_POST_PROXY_TYPE, 0);
2302 vp->vp_integer = dval->value;
2306 if (post_proxy_type > 0) RDEBUG2("Found Post-Proxy-Type %s",
2307 dict_valnamebyattr(PW_POST_PROXY_TYPE, 0, post_proxy_type));
2310 VERIFY_PACKET(reply);
2313 * Decode the packet if required.
2315 if (request->proxy_listener) {
2316 rcode = request->proxy_listener->decode(request->proxy_listener, request);
2317 debug_packet(request, reply, true);
2320 * Pro-actively remove it from the proxy hash.
2321 * This is later than in 2.1.x, but it means that
2322 * the replies are authenticated before being
2323 * removed from the hash.
2326 (request->num_proxied_requests <= request->num_proxied_responses)) {
2327 remove_from_proxy_hash(request);
2330 rad_assert(!request->in_proxy_hash);
2332 } else if (request->in_proxy_hash) {
2333 remove_from_proxy_hash(request);
2336 if (request->home_pool && request->home_pool->virtual_server) {
2337 char const *old_server = request->server;
2339 request->server = request->home_pool->virtual_server;
2340 RDEBUG2("server %s {", request->server);
2342 rcode = process_post_proxy(post_proxy_type, request);
2345 request->server = old_server;
2347 rcode = process_post_proxy(post_proxy_type, request);
2351 if (request->packet->code == request->proxy->code)
2353 * Don't run the next bit if we originated a CoA
2354 * packet, after receiving an Access-Request or
2355 * Accounting-Request.
2360 * There may NOT be a proxy reply, as we may be
2361 * running Post-Proxy-Type = Fail.
2364 pairadd(&request->reply->vps, paircopy(request->reply, reply->vps));
2367 * Delete the Proxy-State Attributes from
2368 * the reply. These include Proxy-State
2369 * attributes from us and remote server.
2371 pairdelete(&request->reply->vps, PW_PROXY_STATE, 0, TAG_ANY);
2375 default: /* Don't do anything */
2377 case RLM_MODULE_FAIL:
2380 case RLM_MODULE_HANDLED:
2387 int request_proxy_reply(RADIUS_PACKET *packet)
2389 RADIUS_PACKET **proxy_p;
2394 VERIFY_PACKET(packet);
2396 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2397 proxy_p = fr_packet_list_find_byreply(proxy_list, packet);
2400 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2401 PROXY("No outstanding request was found for reply from host %s port %d - ID %u",
2402 inet_ntop(packet->src_ipaddr.af,
2403 &packet->src_ipaddr.ipaddr,
2404 buffer, sizeof(buffer)),
2405 packet->src_port, packet->id);
2409 request = fr_packet2myptr(REQUEST, proxy, proxy_p);
2410 request->num_proxied_responses++; /* needs to be protected by lock */
2412 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2415 * No reply, BUT the current packet fails verification:
2416 * ignore it. This does the MD5 calculations in the
2417 * server core, but I guess we can fix that later.
2419 if (!request->proxy_reply &&
2420 (rad_verify(packet, request->proxy,
2421 request->home_server->secret) != 0)) {
2422 DEBUG("Ignoring spoofed proxy reply. Signature is invalid");
2427 * The home server sent us a packet which doesn't match
2428 * something we have: ignore it. This is done only to
2429 * catch the case of broken systems.
2431 if (request->proxy_reply &&
2432 (memcmp(request->proxy_reply->vector,
2434 sizeof(request->proxy_reply->vector)) != 0)) {
2435 RDEBUG2("Ignoring conflicting proxy reply");
2439 gettimeofday(&now, NULL);
2442 * Status-Server packets don't count as real packets.
2444 if (request->proxy->code != PW_CODE_STATUS_SERVER) {
2445 listen_socket_t *sock = request->proxy_listener->data;
2447 request->home_server->last_packet_recv = now.tv_sec;
2448 sock->last_packet = now.tv_sec;
2452 * If we have previously seen a reply, ignore the
2455 if (request->proxy_reply) {
2456 RDEBUG2("Discarding duplicate reply from host %s port %d - ID: %d",
2457 inet_ntop(packet->src_ipaddr.af,
2458 &packet->src_ipaddr.ipaddr,
2459 buffer, sizeof(buffer)),
2460 packet->src_port, packet->id);
2465 * Call the state machine to do something useful with the
2468 request->proxy_reply = talloc_steal(request, packet);
2469 packet->timestamp = now;
2470 request->priority = RAD_LISTEN_PROXY;
2474 * Update the proxy listener stats here, because only one
2475 * thread accesses that at a time. The home_server and
2476 * main proxy_*_stats structures are updated once the
2477 * request is cleaned up.
2479 request->proxy_listener->stats.total_responses++;
2481 request->home_server->stats.last_packet = packet->timestamp.tv_sec;
2482 request->proxy_listener->stats.last_packet = packet->timestamp.tv_sec;
2484 switch (request->proxy->code) {
2485 case PW_CODE_ACCESS_REQUEST:
2486 proxy_auth_stats.last_packet = packet->timestamp.tv_sec;
2488 if (request->proxy_reply->code == PW_CODE_ACCESS_ACCEPT) {
2489 request->proxy_listener->stats.total_access_accepts++;
2491 } else if (request->proxy_reply->code == PW_CODE_ACCESS_REJECT) {
2492 request->proxy_listener->stats.total_access_rejects++;
2494 } else if (request->proxy_reply->code == PW_CODE_ACCESS_CHALLENGE) {
2495 request->proxy_listener->stats.total_access_challenges++;
2499 #ifdef WITH_ACCOUNTING
2500 case PW_CODE_ACCOUNTING_REQUEST:
2501 request->proxy_listener->stats.total_responses++;
2502 proxy_acct_stats.last_packet = packet->timestamp.tv_sec;
2508 case PW_CODE_COA_REQUEST:
2509 request->proxy_listener->stats.total_responses++;
2510 proxy_coa_stats.last_packet = packet->timestamp.tv_sec;
2513 case PW_CODE_DISCONNECT_REQUEST:
2514 request->proxy_listener->stats.total_responses++;
2515 proxy_dsc_stats.last_packet = packet->timestamp.tv_sec;
2525 * We've received a reply. If we hadn't been sending it
2526 * packets for a while, just mark it alive.
2528 if (request->home_server->state == HOME_STATE_UNKNOWN) {
2529 request->home_server->state = HOME_STATE_ALIVE;
2530 request->home_server->response_timeouts = 0;
2534 * Tell the request state machine that we have a proxy
2535 * reply. Depending on the function, this should either
2536 * ignore it, or process it.
2538 request->process(request, FR_ACTION_PROXY_REPLY);
2544 static int setup_post_proxy_fail(REQUEST *request)
2546 DICT_VALUE const *dval = NULL;
2549 VERIFY_REQUEST(request);
2551 if (request->proxy->code == PW_CODE_ACCESS_REQUEST) {
2552 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0,
2553 "Fail-Authentication");
2554 } else if (request->proxy->code == PW_CODE_ACCOUNTING_REQUEST) {
2555 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0,
2558 } else if (request->proxy->code == PW_CODE_COA_REQUEST) {
2559 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-CoA");
2561 } else if (request->proxy->code == PW_CODE_DISCONNECT_REQUEST) {
2562 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-Disconnect");
2565 WARN("Unknown packet type in Post-Proxy-Type Fail: ignoring");
2569 if (!dval) dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail");
2572 pairdelete(&request->config, PW_POST_PROXY_TYPE, 0, TAG_ANY);
2576 vp = pairfind(request->config, PW_POST_PROXY_TYPE, 0, TAG_ANY);
2577 if (!vp) vp = radius_paircreate(request, &request->config,
2578 PW_POST_PROXY_TYPE, 0);
2579 vp->vp_integer = dval->value;
2585 /** Process a request after the proxy has timed out.
2587 * Run the packet through Post-Proxy-Type Fail
2590 * digraph proxy_no_reply {
2593 * proxy_no_reply -> dup [ label = "DUP", arrowhead = "none" ];
2594 * proxy_no_reply -> timer [ label = "TIMER < max_request_time" ];
2595 * proxy_no_reply -> proxy_reply_too_late [ label = "PROXY_REPLY" arrowhead = "none"];
2596 * proxy_no_reply -> process_proxy_reply [ label = "RUN" ];
2597 * proxy_no_reply -> done [ label = "TIMER >= timeout" ];
2601 static void proxy_no_reply(REQUEST *request, int action)
2603 VERIFY_REQUEST(request);
2605 TRACE_STATE_MACHINE;
2610 request_dup(request);
2613 case FR_ACTION_TIMER:
2614 request_max_time(request);
2617 case FR_ACTION_PROXY_REPLY:
2618 proxy_reply_too_late(request);
2622 if (process_proxy_reply(request, NULL)) {
2623 request->handle(request);
2625 request_finish(request, action);
2629 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
2634 /** Process the request after receiving a proxy reply.
2636 * Throught the post-proxy section, and the through the handler
2640 * digraph proxy_running {
2643 * proxy_running -> dup [ label = "DUP", arrowhead = "none" ];
2644 * proxy_running -> timer [ label = "TIMER < max_request_time" ];
2645 * proxy_running -> process_proxy_reply [ label = "RUN" ];
2646 * proxy_running -> done [ label = "TIMER >= timeout" ];
2650 static void proxy_running(REQUEST *request, int action)
2652 VERIFY_REQUEST(request);
2654 TRACE_STATE_MACHINE;
2659 request_dup(request);
2662 case FR_ACTION_TIMER:
2663 request_max_time(request);
2667 if (process_proxy_reply(request, request->proxy_reply)) {
2668 request->handle(request);
2670 request_finish(request, action);
2673 default: /* duplicate proxy replies are suppressed */
2674 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
2679 /** Determine if a #REQUEST needs to be proxied, and perform pre-proxy operations
2681 * Whether a request will be proxied is determined by the attributes present
2682 * in request->config. If any of the following attributes are found, the
2683 * request may be proxied.
2685 * The key attributes are:
2686 * - PW_PROXY_TO_REALM - Specifies a realm the request should be proxied to.
2687 * - PW_HOME_SERVER_POOL - Specifies a specific home server pool to proxy to.
2688 * - PW_PACKET_DST_IP_ADDRESS - Specifies a specific IPv4 home server to proxy to.
2689 * - PW_PACKET_DST_IPV6_ADDRESS - Specifies a specific IPv6 home server to proxy to.
2691 * Certain packet types such as #PW_CODE_STATUS_SERVER will never be proxied.
2693 * If request should be proxied, will:
2694 * - Add request:Proxy-State
2695 * - Strip the current username value of its realm (depending on config)
2696 * - Create a CHAP-Challenge from the original request vector, if one doesn't already
2698 * - Call the pre-process section in the current server, or in the virtual server
2699 * associated with the home server pool we're proxying to.
2701 * @todo A lot of this logic is RADIUS specific, and should be moved out into a protocol
2702 * specific function.
2704 * @param request The #REQUEST to evaluate for proxying.
2705 * @return 0 if not proxying, 1 if request should be proxied, -1 on error.
2707 static int request_will_proxy(REQUEST *request)
2709 int rcode, pre_proxy_type = 0;
2710 char const *realmname = NULL;
2711 VALUE_PAIR *vp, *strippedname;
2712 home_server_t *home;
2713 REALM *realm = NULL;
2714 home_pool_t *pool = NULL;
2716 VERIFY_REQUEST(request);
2718 if (!request->root->proxy_requests) return 0;
2719 if (request->packet->dst_port == 0) return 0;
2720 if (request->packet->code == PW_CODE_STATUS_SERVER) return 0;
2721 if (request->in_proxy_hash) return 0;
2724 * FIXME: for 3.0, allow this only for rejects?
2726 if (request->reply->code != 0) return 0;
2728 vp = pairfind(request->config, PW_PROXY_TO_REALM, 0, TAG_ANY);
2730 realm = realm_find2(vp->vp_strvalue);
2732 REDEBUG2("Cannot proxy to unknown realm %s",
2737 realmname = vp->vp_strvalue;
2740 * Figure out which pool to use.
2742 if (request->packet->code == PW_CODE_ACCESS_REQUEST) {
2743 pool = realm->auth_pool;
2745 #ifdef WITH_ACCOUNTING
2746 } else if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) {
2747 pool = realm->acct_pool;
2751 } else if ((request->packet->code == PW_CODE_COA_REQUEST) ||
2752 (request->packet->code == PW_CODE_DISCONNECT_REQUEST)) {
2753 pool = realm->coa_pool;
2760 } else if ((vp = pairfind(request->config, PW_HOME_SERVER_POOL, 0, TAG_ANY)) != NULL) {
2763 switch (request->packet->code) {
2764 case PW_CODE_ACCESS_REQUEST:
2765 pool_type = HOME_TYPE_AUTH;
2768 #ifdef WITH_ACCOUNTING
2769 case PW_CODE_ACCOUNTING_REQUEST:
2770 pool_type = HOME_TYPE_ACCT;
2775 case PW_CODE_COA_REQUEST:
2776 case PW_CODE_DISCONNECT_REQUEST:
2777 pool_type = HOME_TYPE_COA;
2785 pool = home_pool_byname(vp->vp_strvalue, pool_type);
2788 * Send it directly to a home server (i.e. NAS)
2790 } else if (((vp = pairfind(request->config, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY)) != NULL) ||
2791 ((vp = pairfind(request->config, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY)) != NULL)) {
2794 fr_ipaddr_t dst_ipaddr;
2796 memset(&dst_ipaddr, 0, sizeof(dst_ipaddr));
2798 if (vp->da->attr == PW_PACKET_DST_IP_ADDRESS) {
2799 dst_ipaddr.af = AF_INET;
2800 dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
2801 dst_ipaddr.prefix = 32;
2803 dst_ipaddr.af = AF_INET6;
2804 memcpy(&dst_ipaddr.ipaddr.ip6addr, &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
2805 dst_ipaddr.prefix = 128;
2808 port = pairfind(request->config, PW_PACKET_DST_PORT, 0, TAG_ANY);
2810 dst_port = PW_COA_UDP_PORT;
2812 dst_port = vp->vp_integer;
2816 * Nothing does CoA over TCP.
2818 home = home_server_find(&dst_ipaddr, dst_port, IPPROTO_UDP);
2822 WARN("No such CoA home server %s port %u",
2823 inet_ntop(dst_ipaddr.af, &dst_ipaddr.ipaddr, buffer, sizeof(buffer)),
2824 (unsigned int) dst_port);
2835 RWDEBUG2("Cancelling proxy as no home pool exists");
2839 if (request->listener->synchronous) {
2840 WARN("Cannot proxy a request which is from a 'synchronous' socket");
2844 request->home_pool = pool;
2846 home = home_server_ldb(realmname, pool, request);
2849 REDEBUG2("Failed to find live home server: Cancelling proxy");
2854 home_server_update_request(home, request);
2858 * Once we've decided to proxy a request, we cannot send
2859 * a CoA packet. So we free up any CoA packet here.
2861 if (request->coa) request_done(request->coa, FR_ACTION_DONE);
2865 * Remember that we sent the request to a Realm.
2867 if (realmname) pairmake_packet("Realm", realmname, T_OP_EQ);
2870 * Strip the name, if told to.
2872 * Doing it here catches the case of proxied tunneled
2875 if (realm && (realm->strip_realm == true) &&
2876 (strippedname = pairfind(request->proxy->vps, PW_STRIPPED_USER_NAME, 0, TAG_ANY)) != NULL) {
2878 * If there's a Stripped-User-Name attribute in
2879 * the request, then use THAT as the User-Name
2880 * for the proxied request, instead of the
2883 * This is done by making a copy of the
2884 * Stripped-User-Name attribute, turning it into
2885 * a User-Name attribute, deleting the
2886 * Stripped-User-Name and User-Name attributes
2887 * from the vps list, and making the new
2888 * User-Name the head of the vps list.
2890 vp = pairfind(request->proxy->vps, PW_USER_NAME, 0, TAG_ANY);
2893 vp = radius_paircreate(NULL, NULL,
2895 rad_assert(vp != NULL); /* handled by above function */
2896 /* Insert at the START of the list */
2897 /* FIXME: Can't make assumptions about ordering */
2898 fr_cursor_init(&cursor, &vp);
2899 fr_cursor_merge(&cursor, request->proxy->vps);
2900 request->proxy->vps = vp;
2902 pairstrcpy(vp, strippedname->vp_strvalue);
2905 * Do NOT delete Stripped-User-Name.
2910 * If there is no PW_CHAP_CHALLENGE attribute but
2911 * there is a PW_CHAP_PASSWORD we need to add it
2912 * since we can't use the request authenticator
2913 * anymore - we changed it.
2915 if ((request->packet->code == PW_CODE_ACCESS_REQUEST) &&
2916 pairfind(request->proxy->vps, PW_CHAP_PASSWORD, 0, TAG_ANY) &&
2917 pairfind(request->proxy->vps, PW_CHAP_CHALLENGE, 0, TAG_ANY) == NULL) {
2918 vp = radius_paircreate(request->proxy, &request->proxy->vps, PW_CHAP_CHALLENGE, 0);
2919 pairmemcpy(vp, request->packet->vector, sizeof(request->packet->vector));
2923 * The RFC's say we have to do this, but FreeRADIUS
2926 vp = radius_paircreate(request->proxy, &request->proxy->vps, PW_PROXY_STATE, 0);
2927 pairsprintf(vp, "%u", request->packet->id);
2930 * Should be done BEFORE inserting into proxy hash, as
2931 * pre-proxy may use this information, or change it.
2933 request->proxy->code = request->packet->code;
2936 * Call the pre-proxy routines.
2938 vp = pairfind(request->config, PW_PRE_PROXY_TYPE, 0, TAG_ANY);
2940 DICT_VALUE const *dval = dict_valbyattr(vp->da->attr, vp->da->vendor, vp->vp_integer);
2941 /* Must be a validation issue */
2943 RDEBUG2("Found Pre-Proxy-Type %s", dval->name);
2944 pre_proxy_type = vp->vp_integer;
2948 * home_pool may be NULL when originating CoA packets,
2949 * because they go directly to an IP address.
2951 if (request->home_pool && request->home_pool->virtual_server) {
2952 char const *old_server = request->server;
2954 request->server = request->home_pool->virtual_server;
2956 RDEBUG2("server %s {", request->server);
2958 rcode = process_pre_proxy(pre_proxy_type, request);
2962 request->server = old_server;
2964 rcode = process_pre_proxy(pre_proxy_type, request);
2968 case RLM_MODULE_FAIL:
2969 case RLM_MODULE_INVALID:
2970 case RLM_MODULE_NOTFOUND:
2971 case RLM_MODULE_USERLOCK:
2973 /* FIXME: debug print failed stuff */
2976 case RLM_MODULE_REJECT:
2977 case RLM_MODULE_HANDLED:
2981 * Only proxy the packet if the pre-proxy code succeeded.
2983 case RLM_MODULE_NOOP:
2985 case RLM_MODULE_UPDATED:
2990 static int proxy_to_virtual_server(REQUEST *request)
2994 if (request->packet->dst_port == 0) {
2995 WARN("Cannot proxy an internal request");
2999 DEBUG("Proxying to virtual server %s",
3000 request->home_server->server);
3003 * Packets to virtual servers don't get
3004 * retransmissions sent to them. And the virtual
3005 * server is run ONLY if we have no child
3006 * threads, or we're running in a child thread.
3008 rad_assert(!spawn_flag || !we_are_master());
3010 fake = request_alloc_fake(request);
3012 fake->packet->vps = paircopy(fake->packet, request->packet->vps);
3013 talloc_free(request->proxy);
3015 fake->server = request->home_server->server;
3016 fake->handle = request->handle;
3017 fake->process = NULL; /* should never be run for anything */
3020 * Run the virtual server.
3022 request_running(fake, FR_ACTION_RUN);
3024 request->proxy = talloc_steal(request, fake->packet);
3025 fake->packet = NULL;
3026 request->proxy_reply = talloc_steal(request, fake->reply);
3032 * No reply code, toss the reply we have,
3033 * and do post-proxy-type Fail.
3035 if (!request->proxy_reply->code) {
3036 TALLOC_FREE(request->proxy_reply);
3037 setup_post_proxy_fail(request);
3041 * Do the proxy reply (if any)
3043 if (process_proxy_reply(request, request->proxy_reply)) {
3044 request->handle(request);
3047 return -1; /* so we call request_finish */
3051 static int request_proxy(REQUEST *request, int retransmit)
3055 VERIFY_REQUEST(request);
3057 rad_assert(request->parent == NULL);
3058 rad_assert(request->home_server != NULL);
3060 if (request->master_state == REQUEST_STOP_PROCESSING) return 0;
3064 RWDEBUG("Cannot proxy and originate CoA packets at the same time. Cancelling CoA request");
3065 request_done(request->coa, FR_ACTION_DONE);
3070 * The request may need sending to a virtual server.
3071 * This code is more than a little screwed up. The rest
3072 * of the state machine doesn't handle parent / child
3073 * relationships well. i.e. if the child request takes
3074 * too long, the core will mark the *parent* as "stop
3075 * processing". And the child will continue without
3076 * knowing anything...
3078 * So, we have some horrible hacks to get around that.
3080 if (request->home_server->server) return proxy_to_virtual_server(request);
3083 * We're actually sending a proxied packet. Do that now.
3085 if (!request->in_proxy_hash && !insert_into_proxy_hash(request)) {
3086 RPROXY("Failed to insert request into the proxy list");
3090 rad_assert(request->proxy->id >= 0);
3092 if (rad_debug_lvl) {
3093 struct timeval *response_window;
3095 response_window = request_response_window(request);
3098 if (request->home_server->tls) {
3099 RDEBUG2("Proxying request to home server %s port %d (TLS) timeout %d.%06d",
3100 inet_ntop(request->proxy->dst_ipaddr.af,
3101 &request->proxy->dst_ipaddr.ipaddr,
3102 buffer, sizeof(buffer)),
3103 request->proxy->dst_port,
3104 (int) response_window->tv_sec, (int) response_window->tv_usec);
3107 RDEBUG2("Proxying request to home server %s port %d timeout %d.%06d",
3108 inet_ntop(request->proxy->dst_ipaddr.af,
3109 &request->proxy->dst_ipaddr.ipaddr,
3110 buffer, sizeof(buffer)),
3111 request->proxy->dst_port,
3112 (int) response_window->tv_sec, (int) response_window->tv_usec);
3117 gettimeofday(&request->proxy_retransmit, NULL);
3119 request->proxy->timestamp = request->proxy_retransmit;
3121 request->home_server->last_packet_sent = request->proxy_retransmit.tv_sec;
3124 * Encode the packet before we do anything else.
3126 request->proxy_listener->encode(request->proxy_listener, request);
3127 debug_packet(request, request->proxy, false);
3130 * Set the state function, then the state, no child, and
3133 request->process = proxy_wait_for_reply;
3134 request->child_state = REQUEST_PROXIED;
3138 * And send the packet.
3140 request->proxy_listener->send(request->proxy_listener, request);
3145 * Proxy the packet as if it was new.
3147 static int request_proxy_anew(REQUEST *request)
3149 home_server_t *home;
3151 VERIFY_REQUEST(request);
3154 * Delete the request from the proxy list.
3156 * The packet list code takes care of ensuring that IDs
3157 * aren't reused until all 256 IDs have been used. So
3158 * there's a 1/256 chance of re-using the same ID when
3159 * we're sending to the same home server. Which is
3162 remove_from_proxy_hash(request);
3165 * Find a live home server for the request.
3167 home = home_server_ldb(NULL, request->home_pool, request);
3169 REDEBUG2("Failed to find live home server for request");
3171 if (setup_post_proxy_fail(request)) {
3172 request_queue_or_run(request, proxy_running);
3174 gettimeofday(&request->reply->timestamp, NULL);
3175 request_cleanup_delay_init(request);
3180 #ifdef WITH_ACCOUNTING
3182 * Update the Acct-Delay-Time attribute.
3184 if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) {
3187 vp = pairfind(request->proxy->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY);
3188 if (!vp) vp = radius_paircreate(request->proxy,
3189 &request->proxy->vps,
3190 PW_ACCT_DELAY_TIME, 0);
3194 gettimeofday(&now, NULL);
3195 vp->vp_integer += now.tv_sec - request->proxy_retransmit.tv_sec;
3201 * May have failed over to a "fallback" virtual server.
3202 * If so, run that instead of doing proxying to a real
3206 request->home_server = home;
3207 TALLOC_FREE(request->proxy);
3209 (void) proxy_to_virtual_server(request);
3213 home_server_update_request(home, request);
3215 if (!insert_into_proxy_hash(request)) {
3216 RPROXY("Failed to insert retransmission into the proxy list");
3217 goto post_proxy_fail;
3221 * Free the old packet, to force re-encoding
3223 talloc_free(request->proxy->data);
3224 request->proxy->data = NULL;
3225 request->proxy->data_len = 0;
3227 if (request_proxy(request, 1) != 1) goto post_proxy_fail;
3233 /** Ping a home server.
3236 static void request_ping(REQUEST *request, int action)
3238 home_server_t *home = request->home_server;
3241 VERIFY_REQUEST(request);
3243 TRACE_STATE_MACHINE;
3247 case FR_ACTION_TIMER:
3248 ERROR("No response to status check %d for home server %s port %d",
3250 inet_ntop(request->proxy->dst_ipaddr.af,
3251 &request->proxy->dst_ipaddr.ipaddr,
3252 buffer, sizeof(buffer)),
3253 request->proxy->dst_port);
3256 case FR_ACTION_PROXY_REPLY:
3257 rad_assert(request->in_proxy_hash);
3259 request->home_server->num_received_pings++;
3260 RPROXY("Received response to status check %d (%d in current sequence)",
3261 request->number, home->num_received_pings);
3264 * Remove the request from any hashes
3266 fr_event_delete(el, &request->ev);
3267 remove_from_proxy_hash(request);
3270 * The control socket may have marked the home server as
3271 * alive. OR, it may have suddenly started responding to
3272 * requests again. If so, don't re-do the "make alive"
3275 if (home->state == HOME_STATE_ALIVE) break;
3278 * It's dead, and we haven't received enough ping
3279 * responses to mark it "alive". Wait a bit.
3281 * If it's zombie, we mark it alive immediately.
3283 if ((home->state == HOME_STATE_IS_DEAD) &&
3284 (home->num_received_pings < home->num_pings_to_alive)) {
3289 * Mark it alive and delete any outstanding
3292 home->state = HOME_STATE_ALIVE;
3293 home->response_timeouts = 0;
3294 exec_trigger(request, home->cs, "home_server.alive", false);
3295 home->currently_outstanding = 0;
3296 home->num_sent_pings = 0;
3297 home->num_received_pings = 0;
3298 gettimeofday(&home->revive_time, NULL);
3300 fr_event_delete(el, &home->ev);
3302 RPROXY("Marking home server %s port %d alive",
3303 inet_ntop(request->proxy->dst_ipaddr.af,
3304 &request->proxy->dst_ipaddr.ipaddr,
3305 buffer, sizeof(buffer)),
3306 request->proxy->dst_port);
3310 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
3314 rad_assert(!request->in_request_hash);
3315 rad_assert(request->ev == NULL);
3317 request_done(request, FR_ACTION_DONE);
3321 * Called from start of zombie period, OR after control socket
3322 * marks the home server dead.
3324 static void ping_home_server(void *ctx)
3326 home_server_t *home = talloc_get_type_abort(ctx, home_server_t);
3329 struct timeval when, now;
3331 if ((home->state == HOME_STATE_ALIVE) ||
3333 (home->proto == IPPROTO_TCP) ||
3335 (home->ev != NULL)) {
3339 gettimeofday(&now, NULL);
3343 * We've run out of zombie time. Mark it dead.
3345 if (home->state == HOME_STATE_ZOMBIE) {
3346 when = home->zombie_period_start;
3347 when.tv_sec += home->zombie_period;
3349 if (timercmp(&when, &now, <)) {
3350 DEBUG("PING: Zombie period is over for home server %s", home->log_name);
3351 mark_home_server_dead(home, &now);
3356 * We're not supposed to be pinging it. Just wake up
3357 * when we're supposed to mark it dead.
3359 if (home->ping_check == HOME_PING_CHECK_NONE) {
3360 if (home->state == HOME_STATE_ZOMBIE) {
3361 when = home->zombie_period_start;
3362 when.tv_sec += home->zombie_period;
3363 INSERT_EVENT(ping_home_server, home);
3367 * Else mark_home_server_dead will set a timer
3368 * for revive_interval.
3374 request = request_alloc(NULL);
3375 if (!request) return;
3376 request->number = request_num_counter++;
3379 request->proxy = rad_alloc(request, true);
3380 rad_assert(request->proxy != NULL);
3382 if (home->ping_check == HOME_PING_CHECK_STATUS_SERVER) {
3383 request->proxy->code = PW_CODE_STATUS_SERVER;
3385 pairmake(request->proxy, &request->proxy->vps,
3386 "Message-Authenticator", "0x00", T_OP_SET);
3388 } else if (home->type == HOME_TYPE_AUTH) {
3389 request->proxy->code = PW_CODE_ACCESS_REQUEST;
3391 pairmake(request->proxy, &request->proxy->vps,
3392 "User-Name", home->ping_user_name, T_OP_SET);
3393 pairmake(request->proxy, &request->proxy->vps,
3394 "User-Password", home->ping_user_password, T_OP_SET);
3395 pairmake(request->proxy, &request->proxy->vps,
3396 "Service-Type", "Authenticate-Only", T_OP_SET);
3397 pairmake(request->proxy, &request->proxy->vps,
3398 "Message-Authenticator", "0x00", T_OP_SET);
3401 #ifdef WITH_ACCOUNTING
3402 request->proxy->code = PW_CODE_ACCOUNTING_REQUEST;
3404 pairmake(request->proxy, &request->proxy->vps,
3405 "User-Name", home->ping_user_name, T_OP_SET);
3406 pairmake(request->proxy, &request->proxy->vps,
3407 "Acct-Status-Type", "Stop", T_OP_SET);
3408 pairmake(request->proxy, &request->proxy->vps,
3409 "Acct-Session-Id", "00000000", T_OP_SET);
3410 vp = pairmake(request->proxy, &request->proxy->vps,
3411 "Event-Timestamp", "0", T_OP_SET);
3412 vp->vp_date = now.tv_sec;
3414 rad_assert("Internal sanity check failed");
3418 vp = pairmake(request->proxy, &request->proxy->vps,
3419 "NAS-Identifier", "", T_OP_SET);
3421 pairsprintf(vp, "Status Check %u. Are you alive?",
3422 home->num_sent_pings);
3425 request->proxy->src_ipaddr = home->src_ipaddr;
3426 request->proxy->dst_ipaddr = home->ipaddr;
3427 request->proxy->dst_port = home->port;
3428 request->home_server = home;
3429 #ifdef DEBUG_STATE_MACHINE
3430 if (rad_debug_lvl) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n", request->number, __FUNCTION__,
3431 child_state_names[request->child_state],
3432 child_state_names[REQUEST_DONE]);
3433 if (rad_debug_lvl) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_ping");
3435 #ifdef HAVE_PTHREAD_H
3436 rad_assert(request->child_pid == NO_SUCH_CHILD_PID);
3438 request->child_state = REQUEST_PROXIED;
3439 request->process = request_ping;
3441 rad_assert(request->proxy_listener == NULL);
3443 if (!insert_into_proxy_hash(request)) {
3444 RPROXY("Failed to insert status check %d into proxy list. Discarding it.",
3447 rad_assert(!request->in_request_hash);
3448 rad_assert(!request->in_proxy_hash);
3449 rad_assert(request->ev == NULL);
3450 talloc_free(request);
3455 * Set up the timer callback.
3458 when.tv_sec += home->ping_timeout;
3460 DEBUG("PING: Waiting %u seconds for response to ping",
3461 home->ping_timeout);
3463 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3464 home->num_sent_pings++;
3466 rad_assert(request->proxy_listener != NULL);
3467 request->proxy_listener->send(request->proxy_listener,
3471 * Add +/- 2s of jitter, as suggested in RFC 3539
3472 * and in the Issues and Fixes draft.
3475 home->when.tv_sec += home->ping_interval;
3477 add_jitter(&home->when);
3479 DEBUG("PING: Next status packet in %u seconds", home->ping_interval);
3480 INSERT_EVENT(ping_home_server, home);
3483 static void home_trigger(home_server_t *home, char const *trigger)
3485 REQUEST *my_request;
3486 RADIUS_PACKET *my_packet;
3488 my_request = talloc_zero(NULL, REQUEST);
3489 my_packet = talloc_zero(my_request, RADIUS_PACKET);
3490 my_request->proxy = my_packet;
3491 my_packet->dst_ipaddr = home->ipaddr;
3492 my_packet->src_ipaddr = home->src_ipaddr;
3494 exec_trigger(my_request, home->cs, trigger, false);
3495 talloc_free(my_request);
3498 static void mark_home_server_zombie(home_server_t *home, struct timeval *now, struct timeval *response_window)
3505 rad_assert((home->state == HOME_STATE_ALIVE) ||
3506 (home->state == HOME_STATE_UNKNOWN));
3509 if (home->proto == IPPROTO_TCP) {
3510 WARN("Not marking TCP server %s zombie", home->log_name);
3516 * We've received a real packet recently. Don't mark the
3517 * server as zombie until we've received NO packets for a
3518 * while. The "1/4" of zombie period was chosen rather
3519 * arbitrarily. It's a balance between too short, which
3520 * gives quick fail-over and fail-back, or too long,
3521 * where the proxy still sends packets to an unresponsive
3524 start = now->tv_sec - ((home->zombie_period + 3) / 4);
3525 if (home->last_packet_recv >= start) {
3526 DEBUG("Recieved reply from home server %d seconds ago. Might not be zombie.",
3527 (int) (now->tv_sec - home->last_packet_recv));
3531 home->state = HOME_STATE_ZOMBIE;
3532 home_trigger(home, "home_server.zombie");
3535 * Set the home server to "zombie", as of the time
3538 home->zombie_period_start.tv_sec = start;
3539 home->zombie_period_start.tv_usec = USEC / 2;
3541 fr_event_delete(el, &home->ev);
3543 home->num_sent_pings = 0;
3544 home->num_received_pings = 0;
3546 PROXY( "Marking home server %s port %d as zombie (it has not responded in %d.%06d seconds).",
3547 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
3548 buffer, sizeof(buffer)),
3549 home->port, (int) response_window->tv_sec, (int) response_window->tv_usec);
3551 ping_home_server(home);
3555 void revive_home_server(void *ctx)
3557 home_server_t *home = talloc_get_type_abort(ctx, home_server_t);
3561 rad_assert(home->proto != IPPROTO_TCP);
3564 home->state = HOME_STATE_ALIVE;
3565 home->response_timeouts = 0;
3566 home_trigger(home, "home_server.alive");
3567 home->currently_outstanding = 0;
3568 gettimeofday(&home->revive_time, NULL);
3571 * Delete any outstanding events.
3574 if (home->ev) fr_event_delete(el, &home->ev);
3576 PROXY( "Marking home server %s port %d alive again... we have no idea if it really is alive or not.",
3577 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
3578 buffer, sizeof(buffer)),
3582 void mark_home_server_dead(home_server_t *home, struct timeval *when)
3584 int previous_state = home->state;
3588 if (home->proto == IPPROTO_TCP) {
3589 WARN("Not marking TCP server dead");
3594 PROXY( "Marking home server %s port %d as dead.",
3595 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
3596 buffer, sizeof(buffer)),
3599 home->state = HOME_STATE_IS_DEAD;
3600 home_trigger(home, "home_server.dead");
3602 if (home->ping_check != HOME_PING_CHECK_NONE) {
3604 * If the control socket marks us dead, start
3605 * pinging. Otherwise, we already started
3606 * pinging when it was marked "zombie".
3608 if (previous_state == HOME_STATE_ALIVE) {
3609 ping_home_server(home);
3611 DEBUG("PING: Already pinging home server %s", home->log_name);
3616 * Revive it after a fixed period of time. This
3617 * is very, very, bad.
3620 home->when.tv_sec += home->revive_interval;
3622 DEBUG("PING: Reviving home server %s in %u seconds", home->log_name, home->revive_interval);
3624 INSERT_EVENT(revive_home_server, home);
3628 /** Wait for a reply after proxying a request.
3630 * Retransmit the proxied packet, or time out and go to
3631 * proxy_no_reply. Mark the home server unresponsive, etc.
3633 * If we do receive a reply, we transition to proxy_running.
3636 * digraph proxy_wait_for_reply {
3637 * proxy_wait_for_reply;
3639 * proxy_wait_for_reply -> retransmit_proxied_request [ label = "DUP", arrowhead = "none" ];
3640 * proxy_wait_for_reply -> proxy_no_reply [ label = "TIMER >= response_window" ];
3641 * proxy_wait_for_reply -> timer [ label = "TIMER < max_request_time" ];
3642 * proxy_wait_for_reply -> proxy_running [ label = "PROXY_REPLY" arrowhead = "none"];
3643 * proxy_wait_for_reply -> done [ label = "TIMER >= max_request_time" ];
3647 static void proxy_wait_for_reply(REQUEST *request, int action)
3649 struct timeval now, when;
3650 struct timeval *response_window = NULL;
3651 home_server_t *home = request->home_server;
3654 VERIFY_REQUEST(request);
3656 TRACE_STATE_MACHINE;
3659 rad_assert(request->packet->code != PW_CODE_STATUS_SERVER);
3660 rad_assert(request->home_server != NULL);
3662 gettimeofday(&now, NULL);
3667 * We have a reply, ignore the retransmit.
3669 if (request->proxy_reply) return;
3672 * The request was proxied to a virtual server.
3673 * Ignore the retransmit.
3675 if (request->home_server->server) return;
3678 * Use a new connection when the home server is
3679 * dead, or when there's no proxy listener, or
3680 * when the listener is failed or dead.
3682 * If the listener is known or frozen, use it for
3685 if ((home->state == HOME_STATE_IS_DEAD) ||
3686 !request->proxy_listener ||
3687 (request->proxy_listener->status >= RAD_LISTEN_STATUS_EOL)) {
3688 request_proxy_anew(request);
3694 * The home server is still alive, but TCP. We
3695 * rely on TCP to get the request and reply back.
3696 * So there's no need to retransmit.
3698 if (home->proto == IPPROTO_TCP) {
3699 DEBUG2("Suppressing duplicate proxied request (tcp) to home server %s port %d proto TCP - ID: %d",
3700 inet_ntop(request->proxy->dst_ipaddr.af,
3701 &request->proxy->dst_ipaddr.ipaddr,
3702 buffer, sizeof(buffer)),
3703 request->proxy->dst_port,
3704 request->proxy->id);
3710 * More than one retransmit a second is stupid,
3711 * and should be suppressed by the proxy.
3713 when = request->proxy_retransmit;
3716 if (timercmp(&now, &when, <)) {
3717 DEBUG2("Suppressing duplicate proxied request (too fast) to home server %s port %d proto TCP - ID: %d",
3718 inet_ntop(request->proxy->dst_ipaddr.af,
3719 &request->proxy->dst_ipaddr.ipaddr,
3720 buffer, sizeof(buffer)),
3721 request->proxy->dst_port,
3722 request->proxy->id);
3726 #ifdef WITH_ACCOUNTING
3728 * If we update the Acct-Delay-Time, we need to
3731 if ((request->packet->code == PW_CODE_ACCOUNTING_REQUEST) &&
3732 pairfind(request->proxy->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY)) {
3733 request_proxy_anew(request);
3738 RDEBUG2("Sending duplicate proxied request to home server %s port %d - ID: %d",
3739 inet_ntop(request->proxy->dst_ipaddr.af,
3740 &request->proxy->dst_ipaddr.ipaddr,
3741 buffer, sizeof(buffer)),
3742 request->proxy->dst_port,
3743 request->proxy->id);
3744 request->num_proxied_requests++;
3746 rad_assert(request->proxy_listener != NULL);
3747 FR_STATS_TYPE_INC(home->stats.total_requests);
3748 home->last_packet_sent = now.tv_sec;
3749 request->proxy_retransmit = now;
3750 debug_packet(request, request->proxy, false);
3751 request->proxy_listener->send(request->proxy_listener, request);
3754 case FR_ACTION_TIMER:
3755 response_window = request_response_window(request);
3758 if (!request->proxy_listener ||
3759 (request->proxy_listener->status >= RAD_LISTEN_STATUS_EOL)) {
3760 remove_from_proxy_hash(request);
3762 when = request->packet->timestamp;
3763 when.tv_sec += request->root->max_request_time;
3765 if (timercmp(&when, &now, >)) {
3766 RDEBUG("Waiting for client retransmission in order to do a proxy retransmit");
3767 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3774 * Wake up "response_window" time in the future.
3775 * i.e. when MY packet hasn't received a response.
3777 * Note that we DO NOT mark the home server as
3778 * zombie if it doesn't respond to us. It may be
3779 * responding to other (better looking) packets.
3781 when = request->proxy->timestamp;
3782 timeradd(&when, response_window, &when);
3785 * Not at the response window. Set the timer for
3788 if (timercmp(&when, &now, >)) {
3789 struct timeval diff;
3790 timersub(&when, &now, &diff);
3792 RDEBUG("Expecting proxy response no later than %d.%06d seconds from now",
3793 (int) diff.tv_sec, (int) diff.tv_usec);
3794 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3799 RDEBUG("No proxy response, giving up on request and marking it done");
3802 * If we haven't received any packets for
3803 * "response_window", then mark the home server
3806 * If the connection is TCP, then another
3807 * "watchdog timer" function takes care of pings,
3808 * etc. So we don't need to do it here.
3810 * This check should really be part of a home
3811 * server state machine.
3813 if (((home->state == HOME_STATE_ALIVE) ||
3814 (home->state == HOME_STATE_UNKNOWN))
3816 && (home->proto != IPPROTO_TCP)
3819 home->response_timeouts++;
3820 if (home->response_timeouts >= home->max_response_timeouts)
3821 mark_home_server_zombie(home, &now, response_window);
3824 FR_STATS_TYPE_INC(home->stats.total_timeouts);
3825 if (home->type == HOME_TYPE_AUTH) {
3826 if (request->proxy_listener) FR_STATS_TYPE_INC(request->proxy_listener->stats.total_timeouts);
3827 FR_STATS_TYPE_INC(proxy_auth_stats.total_timeouts);
3830 else if (home->type == HOME_TYPE_ACCT) {
3831 if (request->proxy_listener) FR_STATS_TYPE_INC(request->proxy_listener->stats.total_timeouts);
3832 FR_STATS_TYPE_INC(proxy_acct_stats.total_timeouts);
3837 * There was no response within the window. Stop
3838 * the request. If the client retransmitted, it
3839 * may have failed over to another home server.
3840 * But that one may be dead, too.
3842 * The extra verbose message if we have a username,
3843 * is extremely useful if the proxy is part of a chain
3844 * and the final home server, is not the one we're
3847 if (request->username) {
3848 RERROR("Failing proxied request for user \"%s\", due to lack of any response from home "
3849 "server %s port %d",
3850 request->username->vp_strvalue,
3851 inet_ntop(request->proxy->dst_ipaddr.af,
3852 &request->proxy->dst_ipaddr.ipaddr,
3853 buffer, sizeof(buffer)),
3854 request->proxy->dst_port);
3856 RERROR("Failing proxied request, due to lack of any response from home server %s port %d",
3857 inet_ntop(request->proxy->dst_ipaddr.af,
3858 &request->proxy->dst_ipaddr.ipaddr,
3859 buffer, sizeof(buffer)),
3860 request->proxy->dst_port);
3863 if (setup_post_proxy_fail(request)) {
3864 request_queue_or_run(request, proxy_no_reply);
3866 gettimeofday(&request->reply->timestamp, NULL);
3867 request_cleanup_delay_init(request);
3872 * We received a new reply. Go process it.
3874 case FR_ACTION_PROXY_REPLY:
3875 request_queue_or_run(request, proxy_running);
3879 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
3883 #endif /* WITH_PROXY */
3886 /***********************************************************************
3890 ***********************************************************************/
3892 static int null_handler(UNUSED REQUEST *request)
3898 * See if we need to originate a CoA request.
3900 static void request_coa_originate(REQUEST *request)
3902 int rcode, pre_proxy_type = 0;
3908 VERIFY_REQUEST(request);
3910 rad_assert(request->coa != NULL);
3911 rad_assert(request->proxy == NULL);
3912 rad_assert(!request->in_proxy_hash);
3913 rad_assert(request->proxy_reply == NULL);
3916 * Check whether we want to originate one, or cancel one.
3918 vp = pairfind(request->config, PW_SEND_COA_REQUEST, 0, TAG_ANY);
3920 vp = pairfind(request->coa->proxy->vps, PW_SEND_COA_REQUEST, 0, TAG_ANY);
3924 if (vp->vp_integer == 0) {
3926 TALLOC_FREE(request->coa);
3934 * src_ipaddr will be set up in proxy_encode.
3936 memset(&ipaddr, 0, sizeof(ipaddr));
3937 vp = pairfind(coa->proxy->vps, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY);
3939 ipaddr.af = AF_INET;
3940 ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
3942 } else if ((vp = pairfind(coa->proxy->vps, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY)) != NULL) {
3943 ipaddr.af = AF_INET6;
3944 ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
3945 ipaddr.prefix = 128;
3946 } else if ((vp = pairfind(coa->proxy->vps, PW_HOME_SERVER_POOL, 0, TAG_ANY)) != NULL) {
3947 coa->home_pool = home_pool_byname(vp->vp_strvalue,
3949 if (!coa->home_pool) {
3950 RWDEBUG2("No such home_server_pool %s",
3956 * Prefer the pool to one server
3958 } else if (request->client->coa_pool) {
3959 coa->home_pool = request->client->coa_pool;
3961 } else if (request->client->coa_server) {
3962 coa->home_server = request->client->coa_server;
3966 * If all else fails, send it to the client that
3967 * originated this request.
3969 memcpy(&ipaddr, &request->packet->src_ipaddr, sizeof(ipaddr));
3973 * Use the pool, if it exists.
3975 if (coa->home_pool) {
3976 coa->home_server = home_server_ldb(NULL, coa->home_pool, coa);
3977 if (!coa->home_server) {
3978 RWDEBUG("No live home server for home_server_pool %s", coa->home_pool->name);
3981 home_server_update_request(coa->home_server, coa);
3983 } else if (!coa->home_server) {
3984 uint16_t port = PW_COA_UDP_PORT;
3986 vp = pairfind(coa->proxy->vps, PW_PACKET_DST_PORT, 0, TAG_ANY);
3987 if (vp) port = vp->vp_integer;
3989 coa->home_server = home_server_find(&ipaddr, port, IPPROTO_UDP);
3990 if (!coa->home_server) {
3991 RWDEBUG2("Unknown destination %s:%d for CoA request.",
3992 inet_ntop(ipaddr.af, &ipaddr.ipaddr,
3993 buffer, sizeof(buffer)), port);
3998 vp = pairfind(coa->proxy->vps, PW_PACKET_TYPE, 0, TAG_ANY);
4000 switch (vp->vp_integer) {
4001 case PW_CODE_COA_REQUEST:
4002 case PW_CODE_DISCONNECT_REQUEST:
4003 coa->proxy->code = vp->vp_integer;
4007 DEBUG("Cannot set CoA Packet-Type to code %d",
4013 if (!coa->proxy->code) coa->proxy->code = PW_CODE_COA_REQUEST;
4016 * The rest of the server code assumes that
4017 * request->packet && request->reply exist. Copy them
4018 * from the original request.
4020 rad_assert(coa->packet != NULL);
4021 rad_assert(coa->packet->vps == NULL);
4023 coa->packet = rad_copy_packet(coa, request->packet);
4024 coa->reply = rad_copy_packet(coa, request->reply);
4026 coa->config = paircopy(coa, request->config);
4027 coa->num_coa_requests = 0;
4028 coa->handle = null_handler;
4029 coa->number = request->number; /* it's associated with the same request */
4032 * Call the pre-proxy routines.
4034 vp = pairfind(request->config, PW_PRE_PROXY_TYPE, 0, TAG_ANY);
4036 DICT_VALUE const *dval = dict_valbyattr(vp->da->attr, vp->da->vendor, vp->vp_integer);
4037 /* Must be a validation issue */
4039 RDEBUG2("Found Pre-Proxy-Type %s", dval->name);
4040 pre_proxy_type = vp->vp_integer;
4043 if (coa->home_pool && coa->home_pool->virtual_server) {
4044 char const *old_server = coa->server;
4046 coa->server = coa->home_pool->virtual_server;
4047 RDEBUG2("server %s {", coa->server);
4049 rcode = process_pre_proxy(pre_proxy_type, coa);
4052 coa->server = old_server;
4054 rcode = process_pre_proxy(pre_proxy_type, coa);
4061 * Only send the CoA packet if the pre-proxy code succeeded.
4063 case RLM_MODULE_NOOP:
4065 case RLM_MODULE_UPDATED:
4070 * Source IP / port is set when the proxy socket
4073 coa->proxy->dst_ipaddr = coa->home_server->ipaddr;
4074 coa->proxy->dst_port = coa->home_server->port;
4076 if (!insert_into_proxy_hash(coa)) {
4077 radlog_request(L_PROXY, 0, coa, "Failed to insert CoA request into proxy list");
4082 * We CANNOT divorce the CoA request from the parent
4083 * request. This function is running in a child thread,
4084 * and we need access to the main event loop in order to
4085 * to add the timers for the CoA packet.
4087 * Instead, we wait for the timer on the parent request
4090 gettimeofday(&coa->proxy->timestamp, NULL);
4091 coa->packet->timestamp = coa->proxy->timestamp; /* for max_request_time */
4092 coa->home_server->last_packet_sent = coa->proxy->timestamp.tv_sec;
4093 coa->delay = 0; /* need to calculate a new delay */
4096 * If requested, put a State attribute into the packet,
4097 * and cache the VPS.
4099 fr_state_put_vps(coa, NULL, coa->packet);
4102 * Encode the packet before we do anything else.
4104 coa->proxy_listener->encode(coa->proxy_listener, coa);
4105 debug_packet(coa, coa->proxy, false);
4107 #ifdef DEBUG_STATE_MACHINE
4108 if (rad_debug_lvl) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n", request->number, __FUNCTION__,
4109 child_state_names[request->child_state],
4110 child_state_names[REQUEST_PROXIED]);
4114 * Set the state function, then the state, no child, and
4117 coa->process = coa_wait_for_reply;
4118 coa->child_state = REQUEST_PROXIED;
4120 #ifdef HAVE_PTHREAD_H
4121 coa->child_pid = NO_SUCH_CHILD_PID;
4125 * And send the packet.
4127 coa->proxy_listener->send(coa->proxy_listener, coa);
4131 static void coa_retransmit(REQUEST *request)
4133 uint32_t delay, frac;
4134 struct timeval now, when, mrd;
4136 VERIFY_REQUEST(request);
4138 fr_event_now(el, &now);
4141 * FIXME: Enforce max_request_time
4144 if (request->delay == 0) {
4146 * Implement re-transmit algorithm as per RFC 5080
4149 * We want IRT + RAND*IRT
4150 * or 0.9 IRT + rand(0,.2) IRT
4152 * 2^20 ~ USEC, and we want 2.
4153 * rand(0,0.2) USEC ~ (rand(0,2^21) / 10)
4155 delay = (fr_rand() & ((1 << 22) - 1)) / 10;
4156 request->delay = delay * request->home_server->coa_irt;
4157 delay = request->home_server->coa_irt * USEC;
4158 delay -= delay / 10;
4159 delay += request->delay;
4160 request->delay = delay;
4162 when = request->proxy->timestamp;
4163 tv_add(&when, delay);
4165 if (timercmp(&when, &now, >)) {
4166 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
4172 * Retransmit CoA request.
4176 * Cap count at MRC, if it is non-zero.
4178 if (request->home_server->coa_mrc &&
4179 (request->num_coa_requests >= request->home_server->coa_mrc)) {
4182 RERROR("Failing request - originate-coa ID %u, due to lack of any response from coa server %s port %d",
4184 inet_ntop(request->proxy->dst_ipaddr.af,
4185 &request->proxy->dst_ipaddr.ipaddr,
4186 buffer, sizeof(buffer)),
4187 request->proxy->dst_port);
4189 if (setup_post_proxy_fail(request)) {
4190 request_queue_or_run(request, coa_no_reply);
4192 request_done(request, FR_ACTION_DONE);
4198 * RFC 5080 Section 2.2.1
4200 * RT = 2*RTprev + RAND*RTprev
4201 * = 1.9 * RTprev + rand(0,.2) * RTprev
4202 * = 1.9 * RTprev + rand(0,1) * (RTprev / 5)
4205 delay ^= (delay >> 16);
4207 frac = request->delay / 5;
4208 delay = ((frac >> 16) * delay) + (((frac & 0xffff) * delay) >> 16);
4210 delay += (2 * request->delay) - (request->delay / 10);
4213 * Cap delay at MRT, if MRT is non-zero.
4215 if (request->home_server->coa_mrt &&
4216 (delay > (request->home_server->coa_mrt * USEC))) {
4217 int mrt_usec = request->home_server->coa_mrt * USEC;
4220 * delay = MRT + RAND * MRT
4221 * = 0.9 MRT + rand(0,.2) * MRT
4224 delay ^= (delay >> 15);
4226 delay = ((mrt_usec >> 16) * delay) + (((mrt_usec & 0xffff) * delay) >> 16);
4227 delay += mrt_usec - (mrt_usec / 10);
4230 request->delay = delay;
4232 tv_add(&when, request->delay);
4233 mrd = request->proxy->timestamp;
4234 mrd.tv_sec += request->home_server->coa_mrd;
4237 * Cap duration at MRD.
4239 if (timercmp(&mrd, &when, <)) {
4242 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
4244 request->num_coa_requests++; /* is NOT reset by code 3 lines above! */
4246 FR_STATS_TYPE_INC(request->home_server->stats.total_requests);
4248 request->proxy_listener->send(request->proxy_listener,
4253 /** Wait for a reply after originating a CoA a request.
4255 * Retransmit the proxied packet, or time out and go to
4256 * coa_no_reply. Mark the home server unresponsive, etc.
4258 * If we do receive a reply, we transition to coa_running.
4261 * digraph coa_wait_for_reply {
4262 * coa_wait_for_reply;
4264 * coa_wait_for_reply -> coa_no_reply [ label = "TIMER >= response_window" ];
4265 * coa_wait_for_reply -> timer [ label = "TIMER < max_request_time" ];
4266 * coa_wait_for_reply -> coa_running [ label = "PROXY_REPLY" arrowhead = "none"];
4267 * coa_wait_for_reply -> done [ label = "TIMER >= max_request_time" ];
4271 static void coa_wait_for_reply(REQUEST *request, int action)
4273 VERIFY_REQUEST(request);
4275 TRACE_STATE_MACHINE;
4280 case FR_ACTION_TIMER:
4281 if (request->parent) coa_separate(request);
4283 coa_retransmit(request);
4286 case FR_ACTION_PROXY_REPLY:
4287 if (request->parent) coa_separate(request);
4289 request_queue_or_run(request, coa_running);
4293 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
4298 static void coa_separate(REQUEST *request)
4300 VERIFY_REQUEST(request);
4301 #ifdef DEBUG_STATE_MACHINE
4302 int action = FR_ACTION_TIMER;
4305 TRACE_STATE_MACHINE;
4308 rad_assert(request->parent != NULL);
4309 rad_assert(request->parent->coa == request);
4310 rad_assert(request->ev == NULL);
4311 rad_assert(!request->in_request_hash);
4312 rad_assert(request->coa == NULL);
4314 rad_assert(request->proxy_reply || request->proxy_listener);
4316 (void) talloc_steal(NULL, request);
4317 request->parent->coa = NULL;
4318 request->parent = NULL;
4322 /** Process a request after the CoA has timed out.
4324 * Run the packet through Post-Proxy-Type Fail
4327 * digraph coa_no_reply {
4330 * coa_no_reply -> dup [ label = "DUP", arrowhead = "none" ];
4331 * coa_no_reply -> timer [ label = "TIMER < max_request_time" ];
4332 * coa_no_reply -> coa_reply_too_late [ label = "PROXY_REPLY" arrowhead = "none"];
4333 * coa_no_reply -> process_proxy_reply [ label = "RUN" ];
4334 * coa_no_reply -> done [ label = "TIMER >= timeout" ];
4338 static void coa_no_reply(REQUEST *request, int action)
4342 VERIFY_REQUEST(request);
4344 TRACE_STATE_MACHINE;
4348 case FR_ACTION_TIMER:
4349 request_max_time(request);
4352 case FR_ACTION_PROXY_REPLY: /* too late! */
4353 RDEBUG2("Reply from CoA server %s port %d - ID: %d arrived too late.",
4354 inet_ntop(request->proxy->src_ipaddr.af,
4355 &request->proxy->src_ipaddr.ipaddr,
4356 buffer, sizeof(buffer)),
4357 request->proxy->dst_port, request->proxy->id);
4361 if (process_proxy_reply(request, NULL)) {
4362 request->handle(request);
4364 request_done(request, FR_ACTION_DONE);
4368 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
4374 /** Process the request after receiving a coa reply.
4376 * Throught the post-proxy section, and the through the handler
4380 * digraph coa_running {
4383 * coa_running -> timer [ label = "TIMER < max_request_time" ];
4384 * coa_running -> process_proxy_reply [ label = "RUN" ];
4385 * coa_running -> done [ label = "TIMER >= timeout" ];
4389 static void coa_running(REQUEST *request, int action)
4391 VERIFY_REQUEST(request);
4393 TRACE_STATE_MACHINE;
4397 case FR_ACTION_TIMER:
4398 request_max_time(request);
4402 if (process_proxy_reply(request, request->proxy_reply)) {
4403 request->handle(request);
4405 request_done(request, FR_ACTION_DONE);
4409 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
4413 #endif /* WITH_COA */
4415 /***********************************************************************
4417 * End of the State machine. Start of additional helper code.
4419 ***********************************************************************/
4421 /***********************************************************************
4425 ***********************************************************************/
4426 static void event_socket_handler(fr_event_list_t *xel, UNUSED int fd, void *ctx)
4428 rad_listen_t *listener = talloc_get_type_abort(ctx, rad_listen_t);
4430 rad_assert(xel == el);
4432 if ((listener->fd < 0)
4434 #ifndef WITH_DETAIL_THREAD
4435 && (listener->type != RAD_LISTEN_DETAIL)
4441 listener->print(listener, buffer, sizeof(buffer));
4442 ERROR("FATAL: Asked to read from closed socket: %s",
4445 rad_panic("Socket was closed on us!");
4449 listener->recv(listener);
4453 #ifdef WITH_DETAIL_THREAD
4456 * This function is called periodically to see if this detail
4457 * file is available for reading.
4459 static void event_poll_detail(void *ctx)
4462 rad_listen_t *this = talloc_get_type_abort(ctx, rad_listen_t);
4463 struct timeval when, now;
4464 listen_detail_t *detail = this->data;
4466 rad_assert(this->type == RAD_LISTEN_DETAIL);
4469 event_socket_handler(el, this->fd, this);
4471 fr_event_now(el, &now);
4475 * Backdoor API to get the delay until the next poll
4478 delay = this->encode(this, NULL);
4479 if (delay == 0) goto redo;
4481 tv_add(&when, delay);
4484 if (!fr_event_insert(el, event_poll_detail, this,
4485 &when, &detail->ev)) {
4486 ERROR("Failed creating handler");
4490 #endif /* WITH_DETAIL_THREAD */
4491 #endif /* WITH_DETAIL */
4493 static void event_status(struct timeval *wake)
4495 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
4499 if (rad_debug_lvl == 0) {
4501 INFO("Ready to process requests");
4502 just_started = false;
4508 INFO("Ready to process requests");
4510 } else if ((wake->tv_sec != 0) ||
4511 (wake->tv_usec >= 100000)) {
4512 DEBUG("Waking up in %d.%01u seconds.",
4513 (int) wake->tv_sec, (unsigned int) wake->tv_usec / 100000);
4518 * FIXME: Put this somewhere else, where it isn't called
4519 * all of the time...
4522 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
4524 * If there are no child threads, then there may
4525 * be child processes. In that case, wait for
4526 * their exit status, and throw that exit status
4527 * away. This helps get rid of zxombie children.
4529 while (waitpid(-1, &argval, WNOHANG) > 0) {
4537 static void listener_free_cb(void *ctx)
4539 rad_listen_t *this = talloc_get_type_abort(ctx, rad_listen_t);
4542 if (this->count > 0) {
4543 struct timeval when;
4544 listen_socket_t *sock = this->data;
4546 fr_event_now(el, &when);
4550 if (!fr_event_insert(el, listener_free_cb, this, &when,
4552 rad_panic("Failed to insert event");
4559 * It's all free, close the socket.
4562 this->print(this, buffer, sizeof(buffer));
4563 DEBUG("... cleaning up socket %s", buffer);
4564 rad_assert(this->next == NULL);
4570 static int proxy_eol_cb(void *ctx, void *data)
4572 struct timeval when;
4573 REQUEST *request = fr_packet2myptr(REQUEST, proxy, data);
4575 if (request->proxy_listener != ctx) return 0;
4578 * We don't care if it's being processed in a child thread.
4581 #ifdef WITH_ACCOUNTING
4583 * Accounting packets should be deleted immediately.
4584 * They will never be retransmitted by the client.
4586 if (request->proxy->code == PW_CODE_ACCOUNTING_REQUEST) {
4587 RDEBUG("Stopping request due to failed connection to home server");
4588 request->master_state = REQUEST_STOP_PROCESSING;
4593 * Reset the timer to be now, so that the request is
4594 * quickly updated. But spread the requests randomly
4595 * over the next second, so that we don't overload the
4598 fr_event_now(el, &when);
4599 tv_add(&when, fr_rand() % USEC);
4600 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
4603 * Don't delete it from the list.
4609 static int event_new_fd(rad_listen_t *this)
4615 if (this->status == RAD_LISTEN_STATUS_KNOWN) return 1;
4617 this->print(this, buffer, sizeof(buffer));
4619 if (this->status == RAD_LISTEN_STATUS_INIT) {
4620 listen_socket_t *sock = this->data;
4622 rad_assert(sock != NULL);
4624 DEBUG("Listening on %s", buffer);
4627 } else if (this->type == RAD_LISTEN_PROXY) {
4628 home_server_t *home;
4631 if (!home || !home->limit.max_connections) {
4632 INFO(" ... adding new socket %s", buffer);
4634 INFO(" ... adding new socket %s (%u of %u)", buffer,
4635 home->limit.num_connections, home->limit.max_connections);
4640 INFO(" ... adding new socket %s", buffer);
4643 switch (this->type) {
4646 * Detail files are always known, and aren't
4647 * put into the socket event loop.
4649 case RAD_LISTEN_DETAIL:
4650 this->status = RAD_LISTEN_STATUS_KNOWN;
4652 #ifndef WITH_DETAIL_THREAD
4654 * Set up the first poll interval.
4656 event_poll_detail(this);
4659 break; /* add the FD to the list */
4661 #endif /* WITH_DETAIL */
4665 * Add it to the list of sockets we can use.
4666 * Server sockets (i.e. auth/acct) are never
4667 * added to the packet list.
4669 case RAD_LISTEN_PROXY:
4671 rad_assert((sock->proto == IPPROTO_UDP) || (sock->home != NULL));
4674 * Add timers to outgoing child sockets, if necessary.
4676 if (sock->proto == IPPROTO_TCP && sock->opened &&
4677 (sock->home->limit.lifetime || sock->home->limit.idle_timeout)) {
4678 struct timeval when;
4680 when.tv_sec = sock->opened + 1;
4684 if (!fr_event_insert(el, tcp_socket_timer, this, &when,
4686 rad_panic("Failed to insert event");
4691 #endif /* WITH_PROXY */
4694 * FIXME: put idle timers on command sockets.
4700 * Add timers to incoming child sockets, if necessary.
4702 if (sock->proto == IPPROTO_TCP && sock->opened &&
4703 (sock->limit.lifetime || sock->limit.idle_timeout)) {
4704 struct timeval when;
4706 when.tv_sec = sock->opened + 1;
4710 if (!fr_event_insert(el, tcp_socket_timer, this, &when,
4712 ERROR("Failed adding timer for socket: %s", fr_strerror());
4718 } /* switch over listener types */
4721 * All sockets: add the FD to the event handler.
4723 if (!fr_event_fd_insert(el, 0, this->fd,
4724 event_socket_handler, this)) {
4725 ERROR("Failed adding event handler for socket: %s", fr_strerror());
4729 this->status = RAD_LISTEN_STATUS_KNOWN;
4735 * The socket has reached a timeout. Try to close it.
4737 if (this->status == RAD_LISTEN_STATUS_FROZEN) {
4739 * Requests are still using the socket. Wait for
4742 if (this->count > 0) {
4743 struct timeval when;
4744 listen_socket_t *sock = this->data;
4747 * Try again to clean up the socket in 30
4750 gettimeofday(&when, NULL);
4754 if (!fr_event_insert(el,
4755 (fr_event_callback_t) event_new_fd,
4756 this, &when, &sock->ev)) {
4757 rad_panic("Failed to insert event");
4763 fr_event_fd_delete(el, 0, this->fd);
4764 this->status = RAD_LISTEN_STATUS_REMOVE_NOW;
4768 * The socket has had a catastrophic error. Close it.
4770 if (this->status == RAD_LISTEN_STATUS_EOL) {
4772 * Remove it from the list of live FD's.
4774 fr_event_fd_delete(el, 0, this->fd);
4778 * Tell all requests using this socket that the socket is dead.
4780 if (this->type == RAD_LISTEN_PROXY) {
4781 PTHREAD_MUTEX_LOCK(&proxy_mutex);
4782 if (!fr_packet_list_socket_freeze(proxy_list,
4784 ERROR("Fatal error freezing socket: %s", fr_strerror());
4788 if (this->count > 0) {
4789 fr_packet_list_walk(proxy_list, this, proxy_eol_cb);
4791 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
4796 * Requests are still using the socket. Wait for
4799 if (this->count > 0) {
4800 struct timeval when;
4801 listen_socket_t *sock = this->data;
4804 * Try again to clean up the socket in 30
4807 gettimeofday(&when, NULL);
4811 if (!fr_event_insert(el,
4812 (fr_event_callback_t) event_new_fd,
4813 this, &when, &sock->ev)) {
4814 rad_panic("Failed to insert event");
4821 * No one is using the socket. We can remove it now.
4823 this->status = RAD_LISTEN_STATUS_REMOVE_NOW;
4824 } /* socket is at EOL */
4830 if (this->status == RAD_LISTEN_STATUS_REMOVE_NOW) {
4833 listen_socket_t *sock = this->data;
4835 struct timeval when;
4838 * Re-open the socket, pointing it to /dev/null.
4839 * This means that all writes proceed without
4840 * blocking, and all reads return "no data".
4842 * This leaves the socket active, so any child
4843 * threads won't go insane. But it means that
4844 * they cannot send or receive any packets.
4846 * This is EXTRA work in the normal case, when
4847 * sockets are closed without error. But it lets
4848 * us have one simple processing method for all
4851 devnull = open("/dev/null", O_RDWR);
4853 ERROR("FATAL failure opening /dev/null: %s",
4854 fr_syserror(errno));
4857 if (dup2(devnull, this->fd) < 0) {
4858 ERROR("FATAL failure closing socket: %s",
4859 fr_syserror(errno));
4865 rad_assert(this->type != RAD_LISTEN_DETAIL);
4871 * The socket is dead. Force all proxied packets
4872 * to stop using it. And then remove it from the
4873 * list of outgoing sockets.
4875 if (this->type == RAD_LISTEN_PROXY) {
4876 home_server_t *home;
4879 if (!home || !home->limit.max_connections) {
4880 INFO(" ... shutting down socket %s", buffer);
4882 INFO(" ... shutting down socket %s (%u of %u)", buffer,
4883 home->limit.num_connections, home->limit.max_connections);
4886 PTHREAD_MUTEX_LOCK(&proxy_mutex);
4887 fr_packet_list_walk(proxy_list, this, eol_proxy_listener);
4889 if (!fr_packet_list_socket_del(proxy_list, this->fd)) {
4890 ERROR("Fatal error removing socket %s: %s",
4891 buffer, fr_strerror());
4894 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
4898 INFO(" ... shutting down socket %s", buffer);
4901 * EOL all requests using this socket.
4903 rbtree_walk(pl, RBTREE_DELETE_ORDER, eol_listener, this);
4907 * No child threads, clean it up now.
4911 if (sock->ev) fr_event_delete(el, &sock->ev);
4917 * Wait until all requests using this socket are done.
4919 gettimeofday(&when, NULL);
4923 if (!fr_event_insert(el, listener_free_cb, this, &when,
4925 rad_panic("Failed to insert event");
4928 #endif /* WITH_TCP */
4933 /***********************************************************************
4937 ***********************************************************************/
4939 static void handle_signal_self(int flag)
4943 if ((flag & (RADIUS_SIGNAL_SELF_EXIT | RADIUS_SIGNAL_SELF_TERM)) != 0) {
4944 if ((flag & RADIUS_SIGNAL_SELF_EXIT) != 0) {
4945 INFO("Signalled to exit");
4946 fr_event_loop_exit(el, 1);
4948 INFO("Signalled to terminate");
4949 fr_event_loop_exit(el, 2);
4953 } /* else exit/term flags weren't set */
4956 * Tell the even loop to stop processing.
4958 if ((flag & RADIUS_SIGNAL_SELF_HUP) != 0) {
4960 static time_t last_hup = 0;
4963 if ((int) (when - last_hup) < 5) {
4964 INFO("Ignoring HUP (less than 5s since last one)");
4968 INFO("Received HUP signal");
4972 exec_trigger(NULL, NULL, "server.signal.hup", true);
4973 fr_event_loop_exit(el, 0x80);
4976 #if defined(WITH_DETAIL) && !defined(WITH_DETAIL_THREAD)
4977 if ((flag & RADIUS_SIGNAL_SELF_DETAIL) != 0) {
4981 * FIXME: O(N) loops suck.
4983 for (this = main_config.listen;
4985 this = this->next) {
4986 if (this->type != RAD_LISTEN_DETAIL) continue;
4989 * This one didn't send the signal, skip
4992 if (!this->decode(this, NULL)) continue;
4995 * Go service the interrupt.
4997 event_poll_detail(this);
5002 #if defined(WITH_TCP) && defined(WITH_PROXY) && defined(HAVE_PTHREAD_H)
5004 * There are new listeners in the list. Run
5005 * event_new_fd() on them.
5007 if ((flag & RADIUS_SIGNAL_SELF_NEW_FD) != 0) {
5008 rad_listen_t *this, *next;
5010 FD_MUTEX_LOCK(&fd_mutex);
5013 * FIXME: unlock the mutex before calling
5016 for (this = new_listeners; this != NULL; this = next) {
5023 new_listeners = NULL;
5024 FD_MUTEX_UNLOCK(&fd_mutex);
5029 #ifndef HAVE_PTHREAD_H
5030 void radius_signal_self(int flag)
5032 return handle_signal_self(flag);
5036 static int self_pipe[2] = { -1, -1 };
5039 * Inform ourselves that we received a signal.
5041 void radius_signal_self(int flag)
5047 * The read MUST be non-blocking for this to work.
5049 rcode = read(self_pipe[0], buffer, sizeof(buffer));
5053 for (i = 0; i < rcode; i++) {
5054 buffer[0] |= buffer[i];
5062 if (write(self_pipe[1], buffer, 1) < 0) fr_exit(0);
5066 static void event_signal_handler(UNUSED fr_event_list_t *xel,
5067 UNUSED int fd, UNUSED void *ctx)
5072 rcode = read(self_pipe[0], buffer, sizeof(buffer));
5073 if (rcode <= 0) return;
5076 * Merge pending signals.
5078 for (i = 0; i < rcode; i++) {
5079 buffer[0] |= buffer[i];
5082 handle_signal_self(buffer[0]);
5084 #endif /* HAVE_PTHREAD_H */
5086 /***********************************************************************
5088 * Bootstrapping code.
5090 ***********************************************************************/
5093 * Externally-visibly functions.
5095 int radius_event_init(TALLOC_CTX *ctx) {
5096 el = fr_event_list_create(ctx, event_status);
5102 static int packet_entry_cmp(void const *one, void const *two)
5104 RADIUS_PACKET const * const *a = one;
5105 RADIUS_PACKET const * const *b = two;
5107 return fr_packet_cmp(*a, *b);
5111 int radius_event_start(CONF_SECTION *cs, bool have_children)
5113 rad_listen_t *head = NULL;
5115 if (fr_start_time != (time_t)-1) return 0;
5117 time(&fr_start_time);
5119 if (!check_config) {
5121 * radius_event_init() must be called first
5125 pl = rbtree_create(NULL, packet_entry_cmp, NULL, 0);
5126 if (!pl) return 0; /* leak el */
5129 request_num_counter = 0;
5132 if (main_config.proxy_requests && !check_config) {
5134 * Create the tree for managing proxied requests and
5137 proxy_list = fr_packet_list_create(1);
5138 if (!proxy_list) return 0;
5140 #ifdef HAVE_PTHREAD_H
5141 if (pthread_mutex_init(&proxy_mutex, NULL) != 0) {
5142 ERROR("FATAL: Failed to initialize proxy mutex: %s",
5143 fr_syserror(errno));
5149 * The "init_delay" is set to "response_window".
5150 * Reset it to half of "response_window" in order
5151 * to give the event loop enough time to service
5152 * the event before hitting "response_window".
5154 main_config.init_delay.tv_usec += (main_config.init_delay.tv_sec & 0x01) * USEC;
5155 main_config.init_delay.tv_usec >>= 1;
5156 main_config.init_delay.tv_sec >>= 1;
5158 proxy_ctx = talloc_init("proxy");
5163 * Move all of the thread calls to this file?
5165 * It may be best for the mutexes to be in this file...
5167 spawn_flag = have_children;
5169 #ifdef HAVE_PTHREAD_H
5170 NO_SUCH_CHILD_PID = pthread_self(); /* not a child thread */
5173 * Initialize the threads ONLY if we're spawning, AND
5174 * we're running normally.
5176 if (have_children && !check_config &&
5177 (thread_pool_init(cs, &spawn_flag) < 0)) {
5183 DEBUG("%s: #### Skipping IP addresses and Ports ####",
5185 if (listen_init(cs, &head, spawn_flag) < 0) {
5192 #ifdef HAVE_PTHREAD_H
5194 * Child threads need a pipe to signal us, as do the
5197 if (pipe(self_pipe) < 0) {
5198 ERROR("Error opening internal pipe: %s", fr_syserror(errno));
5201 if ((fcntl(self_pipe[0], F_SETFL, O_NONBLOCK) < 0) ||
5202 (fcntl(self_pipe[0], F_SETFD, FD_CLOEXEC) < 0)) {
5203 ERROR("Error setting internal flags: %s", fr_syserror(errno));
5206 if ((fcntl(self_pipe[1], F_SETFL, O_NONBLOCK) < 0) ||
5207 (fcntl(self_pipe[1], F_SETFD, FD_CLOEXEC) < 0)) {
5208 ERROR("Error setting internal flags: %s", fr_syserror(errno));
5211 DEBUG4("Created signal pipe. Read end FD %i, write end FD %i", self_pipe[0], self_pipe[1]);
5213 if (!fr_event_fd_insert(el, 0, self_pipe[0], event_signal_handler, el)) {
5214 ERROR("Failed creating signal pipe handler: %s", fr_strerror());
5219 DEBUG("%s: #### Opening IP addresses and Ports ####", main_config.name);
5222 * The server temporarily switches to an unprivileged
5223 * user very early in the bootstrapping process.
5224 * However, some sockets MAY require privileged access
5225 * (bind to device, or to port < 1024, or to raw
5226 * sockets). Those sockets need to call suid up/down
5227 * themselves around the functions that need a privileged
5230 if (listen_init(cs, &head, spawn_flag) < 0) {
5234 main_config.listen = head;
5237 * At this point, no one has any business *ever* going
5240 rad_suid_down_permanent();
5247 static int proxy_delete_cb(UNUSED void *ctx, void *data)
5249 REQUEST *request = fr_packet2myptr(REQUEST, proxy, data);
5251 VERIFY_REQUEST(request);
5253 request->master_state = REQUEST_STOP_PROCESSING;
5255 #ifdef HAVE_PTHREAD_H
5256 if (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0) return 0;
5260 * If it's queued we can't delete it from the queue.
5262 * Otherwise, it's OK to delete it. Even RUNNING, because
5263 * that will get caught by the check above.
5265 if (request->child_state == REQUEST_QUEUED) return 0;
5267 request->in_proxy_hash = false;
5269 if (!request->in_request_hash) {
5270 request_done(request, FR_ACTION_DONE);
5274 * Delete it from the list.
5281 static int request_delete_cb(UNUSED void *ctx, void *data)
5283 REQUEST *request = fr_packet2myptr(REQUEST, packet, data);
5285 VERIFY_REQUEST(request);
5287 request->master_state = REQUEST_STOP_PROCESSING;
5290 * Not done, or the child thread is still processing it.
5292 if (request->child_state < REQUEST_RESPONSE_DELAY) return 0; /* continue */
5294 #ifdef HAVE_PTHREAD_H
5295 if (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0) return 0;
5299 rad_assert(request->in_proxy_hash == false);
5302 request->in_request_hash = false;
5304 if (request->ev) fr_event_delete(el, &request->ev);
5306 if (main_config.memory_report) {
5307 RDEBUG2("Cleaning up request packet ID %u with timestamp +%d",
5308 request->packet->id,
5309 (unsigned int) (request->timestamp - fr_start_time));
5314 rad_assert(!request->coa->in_proxy_hash);
5318 request_free(request);
5321 * Delete it from the list, and continue;
5327 void radius_event_free(void)
5333 * There are requests in the proxy hash that aren't
5334 * referenced from anywhere else. Remove them first.
5337 fr_packet_list_walk(proxy_list, NULL, proxy_delete_cb);
5341 rbtree_walk(pl, RBTREE_DELETE_ORDER, request_delete_cb, NULL);
5345 * Now that all requests have been marked "please stop",
5346 * ensure that all of the threads have exited.
5348 #ifdef HAVE_PTHREAD_H
5353 * Walk the lists again, ensuring that all
5354 * requests are done.
5356 if (main_config.memory_report) {
5361 fr_packet_list_walk(proxy_list, NULL, proxy_delete_cb);
5362 num = fr_packet_list_num_elements(proxy_list);
5364 ERROR("Proxy list has %d requests still in it.", num);
5369 rbtree_walk(pl, RBTREE_DELETE_ORDER, request_delete_cb, NULL);
5370 num = rbtree_num_elements(pl);
5372 ERROR("Request list has %d requests still in it.", num);
5381 fr_packet_list_free(proxy_list);
5384 if (proxy_ctx) talloc_free(proxy_ctx);
5389 if (debug_condition) talloc_free(debug_condition);
5392 int radius_event_process(void)
5396 return fr_event_loop(el);