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>
33 #include <freeradius-devel/rad_assert.h>
36 #include <freeradius-devel/detail.h>
42 #ifdef HAVE_SYS_WAIT_H
43 # include <sys/wait.h>
46 extern pid_t radius_pid;
47 extern bool check_config;
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 fr_packet_list_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[] = {
73 #ifdef DEBUG_STATE_MACHINE
74 #define TRACE_STATE_MACHINE if (debug_flag) do { struct timeval debug_tv; \
75 gettimeofday(&debug_tv, NULL);\
76 debug_tv.tv_sec -= fr_start_time;\
77 printf("(%u) %d.%06d ********\tSTATE %s action %s live M-%s C-%s\t********\n",\
78 request->number, (int) debug_tv.tv_sec, (int) debug_tv.tv_usec, __FUNCTION__, action_codes[action], master_state_names[request->master_state], child_state_names[request->child_state]); } while (0)
80 static char const *master_state_names[REQUEST_MASTER_NUM_STATES] = {
87 static char const *child_state_names[REQUEST_CHILD_NUM_STATES] = {
98 #define TRACE_STATE_MACHINE {}
102 * Declare a state in the state machine.
105 #define STATE_MACHINE_DECL(_x) static void CC_HINT(nonnull) _x(REQUEST *request, int action)
107 #define STATE_MACHINE_TIMER(_x) request->timer_action = _x; \
108 fr_event_insert(el, request_timer, request, \
109 &when, &request->ev);
114 * @section request_timeline
116 * Time sequence of a request
119 * RQ-----------------P=============================Y-J-C
120 * ::::::::::::::::::::::::::::::::::::::::::::::::::::::::M
123 * - R: received. Duplicate detection is done, and request is
126 * - Q: Request is placed onto a queue for child threads to pick up.
127 * If there are no child threads, the request goes immediately
130 * - P: Processing the request through the modules.
132 * - Y: Reply is ready. Rejects MAY be delayed here. All other
133 * replies are sent immediately.
135 * - J: Reject is sent "response_delay" after the reply is ready.
137 * - C: For Access-Requests, After "cleanup_delay", the request is
138 * deleted. Accounting-Request packets go directly from Y to C.
140 * - M: Max request time. If the request hits this timer, it is
143 * Other considerations include duplicate and conflicting
144 * packets. When a dupicate packet is received, it is ignored
145 * until we've reached Y, as no response is ready. If the reply
146 * is a reject, duplicates are ignored until J, when we're ready
147 * to send the reply. In between the reply being sent (Y or J),
148 * and C, the server responds to duplicates by sending the cached
151 * Conflicting packets are sent in 2 situations.
153 * The first is in between R and Y. In that case, we consider
154 * it as a hint that we're taking too long, and the NAS has given
155 * up on the request. We then behave just as if the M timer was
156 * reached, and we discard the current request. This allows us
157 * to process the new one.
159 * The second case is when we're at Y, but we haven't yet
160 * finished processing the request. This is a race condition in
161 * the threading code (avoiding locks is faster). It means that
162 * a thread has actually encoded and sent the reply, and that the
163 * NAS has responded with a new packet. The server can then
164 * safely mark the current request as "OK to delete", and behaves
165 * just as if the M timer was reached. This usually happens only
166 * in high-load situations.
168 * Duplicate packets are sent when the NAS thinks we're taking
169 * too long, and wants a reply. From R-Y, duplicates are
170 * ignored. From Y-J (for Access-Rejects), duplicates are also
171 * ignored. From Y-C, duplicates get a duplicate reply. *And*,
172 * they cause the "cleanup_delay" time to be extended. This
173 * extension means that we're more likely to send a duplicate
174 * reply (if we have one), or to suppress processing the packet
175 * twice if we didn't reply to it.
177 * All functions in this file should be thread-safe, and should
178 * assume thet the REQUEST structure is being accessed
179 * simultaneously by the main thread, and by the child worker
180 * threads. This means that timers, etc. cannot be updated in
183 * Instead, the master thread periodically calls request->process
184 * with action TIMER. It's up to the individual functions to
185 * determine how to handle that. They need to check if they're
186 * being called from a child thread or the master, and then do
187 * different things based on that.
192 static fr_packet_list_t *proxy_list = NULL;
195 #ifdef HAVE_PTHREAD_H
197 static pthread_mutex_t proxy_mutex;
198 static bool proxy_no_new_sockets = false;
201 #define PTHREAD_MUTEX_LOCK if (spawn_flag) pthread_mutex_lock
202 #define PTHREAD_MUTEX_UNLOCK if (spawn_flag) pthread_mutex_unlock
204 static pthread_t NO_SUCH_CHILD_PID;
205 #define NO_CHILD_THREAD request->child_pid = NO_SUCH_CHILD_PID
209 * This is easier than ifdef's throughout the code.
211 #define PTHREAD_MUTEX_LOCK(_x)
212 #define PTHREAD_MUTEX_UNLOCK(_x)
213 #define NO_CHILD_THREAD
216 #if defined(HAVE_PTHREAD_H) && !defined (NDEBUG)
217 static bool we_are_master(void)
220 (pthread_equal(pthread_self(), NO_SUCH_CHILD_PID) == 0)) {
226 #define ASSERT_MASTER if (!we_are_master()) rad_panic("We are not master")
229 #define we_are_master(_x) (1)
230 #define ASSERT_MASTER
233 static int event_new_fd(rad_listen_t *this);
236 * We need mutexes around the event FD list *only* in certain
239 #if defined (HAVE_PTHREAD_H) && (defined(WITH_PROXY) || defined(WITH_TCP))
240 static rad_listen_t *new_listeners = NULL;
242 static pthread_mutex_t fd_mutex;
243 #define FD_MUTEX_LOCK if (spawn_flag) pthread_mutex_lock
244 #define FD_MUTEX_UNLOCK if (spawn_flag) pthread_mutex_unlock
246 void radius_update_listener(rad_listen_t *this)
249 * Just do it ourselves.
251 if (we_are_master()) {
256 FD_MUTEX_LOCK(&fd_mutex);
259 * If it's already in the list, don't add it again.
262 FD_MUTEX_UNLOCK(&fd_mutex);
267 * Otherwise, add it to the list
269 this->next = new_listeners;
270 new_listeners = this;
271 FD_MUTEX_UNLOCK(&fd_mutex);
272 radius_signal_self(RADIUS_SIGNAL_SELF_NEW_FD);
275 void radius_update_listener(rad_listen_t *this)
278 * No threads. Just insert it.
283 * This is easier than ifdef's throughout the code.
285 #define FD_MUTEX_LOCK(_x)
286 #define FD_MUTEX_UNLOCK(_x)
289 static int request_num_counter = 1;
291 static int request_will_proxy(REQUEST *request);
292 static int request_proxy(REQUEST *request, int retransmit);
293 STATE_MACHINE_DECL(proxy_wait_for_reply);
294 STATE_MACHINE_DECL(proxy_no_reply);
295 STATE_MACHINE_DECL(proxy_running);
296 static int process_proxy_reply(REQUEST *request, RADIUS_PACKET *reply);
297 static void remove_from_proxy_hash(REQUEST *request);
298 static void remove_from_proxy_hash_nl(REQUEST *request, bool yank);
299 static int insert_into_proxy_hash(REQUEST *request);
302 static REQUEST *request_setup(rad_listen_t *listener, RADIUS_PACKET *packet,
303 RADCLIENT *client, RAD_REQUEST_FUNP fun);
305 STATE_MACHINE_DECL(request_common);
306 STATE_MACHINE_DECL(request_response_delay);
307 STATE_MACHINE_DECL(request_cleanup_delay);
308 STATE_MACHINE_DECL(request_running);
310 static void request_coa_originate(REQUEST *request);
311 STATE_MACHINE_DECL(coa_running);
312 STATE_MACHINE_DECL(coa_wait_for_reply);
313 STATE_MACHINE_DECL(coa_no_reply);
314 static void request_coa_separate(REQUEST *coa);
318 #define USEC (1000000)
320 #define INSERT_EVENT(_function, _ctx) if (!fr_event_insert(el, _function, _ctx, &((_ctx)->when), &((_ctx)->ev))) { _rad_panic(__FILE__, __LINE__, "Failed to insert event"); }
322 static void _rad_panic(char const *file, unsigned int line, char const *msg)
324 ERROR("[%s:%d] %s", file, line, msg);
331 #define rad_panic(x) _rad_panic(__FILE__, __LINE__, x)
333 static void tv_add(struct timeval *tv, int usec_delay)
335 if (usec_delay >= USEC) {
336 tv->tv_sec += usec_delay / USEC;
339 tv->tv_usec += usec_delay;
341 if (tv->tv_usec >= USEC) {
342 tv->tv_sec += tv->tv_usec / USEC;
348 * In daemon mode, AND this request has debug flags set.
350 #define DEBUG_PACKET if (!debug_flag && request->log.lvl && request->log.func) debug_packet
352 static void debug_packet(REQUEST *request, RADIUS_PACKET *packet, int direction)
357 char const *received, *from;
358 fr_ipaddr_t const *ip;
363 rad_assert(request->log.func != NULL);
365 if (direction == 0) {
366 received = "Received";
367 from = "from"; /* what else? */
368 ip = &packet->src_ipaddr;
369 port = packet->src_port;
372 received = "Sending";
373 from = "to"; /* hah! */
374 ip = &packet->dst_ipaddr;
375 port = packet->dst_port;
379 * Client-specific debugging re-prints the input
380 * packet into the client log.
382 * This really belongs in a utility library
384 if (is_radius_code(packet->code)) {
385 RDEBUG("%s %s packet %s host %s port %i, id=%i, length=%zu",
386 received, fr_packet_codes[packet->code], from,
387 inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
388 port, packet->id, packet->data_len);
390 RDEBUG("%s packet %s host %s port %d code=%d, id=%d, length=%zu",
392 inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
394 packet->code, packet->id, packet->data_len);
397 for (vp = fr_cursor_init(&cursor, &packet->vps);
399 vp = fr_cursor_next(&cursor)) {
400 vp_prints(buffer, sizeof(buffer), vp);
401 RDEBUG("\t%s", buffer);
406 /***********************************************************************
408 * Start of RADIUS server state machine.
410 ***********************************************************************/
412 static struct timeval *request_response_window(REQUEST *request)
415 * The client hasn't set the response window. Return
416 * either the home server one, if set, or the global one.
418 if (!timerisset(&request->client->response_window)) {
419 return &request->home_server->response_window;
422 if (timercmp(&request->client->response_window,
423 &request->home_server->response_window, <)) {
424 return &request->client->response_window;
427 return &request->home_server->response_window;
431 * Determine initial request processing delay.
433 static int request_init_delay(REQUEST *request)
435 struct timeval half_response_window;
437 /* Allow client response window to lower initial delay */
438 if (timerisset(&request->client->response_window)) {
439 half_response_window.tv_sec = request->client->response_window.tv_sec >> 1;
440 half_response_window.tv_usec =
441 ((request->client->response_window.tv_sec & 1) * USEC +
442 request->client->response_window.tv_usec) >> 1;
443 if (timercmp(&half_response_window, &request->root->init_delay, <))
444 return (int)half_response_window.tv_sec * USEC +
445 (int)half_response_window.tv_usec;
448 return (int)request->root->init_delay.tv_sec * USEC +
449 (int)request->root->init_delay.tv_usec;
453 * Callback for ALL timer events related to the request.
455 static void request_timer(void *ctx)
457 REQUEST *request = ctx;
458 int action = request->timer_action;
462 request->process(request, action);
466 * Only ever called from the master thread.
468 STATE_MACHINE_DECL(request_done)
470 struct timeval now, when;
479 * CoA requests can be cleaned up in the child thread,
480 * but ONLY if they aren't tied into anything.
482 if (request->parent && (request->parent->coa == request)) {
483 rad_assert(!request->in_request_hash);
484 rad_assert(!request->in_proxy_hash);
485 rad_assert(action == FR_ACTION_DONE);
486 rad_assert(request->ev == NULL);
490 #ifdef HAVE_PTHREAD_H
492 * If called from a child thread, mark ourselves as done,
493 * and wait for the master thread timer to clean us up.
495 if (!we_are_master()) {
496 request->child_state = REQUEST_DONE;
504 * Move the CoA request to its own handler.
507 request_coa_separate(request->coa);
508 } else if (request->parent && (request->parent->coa == request)) {
509 request_coa_separate(request);
515 * It doesn't hurt to send duplicate replies. All other
516 * signals are ignored, as the request will be cleaned up
521 if (request->reply->code != 0) {
522 request->listener->send(request->listener, request);
525 RDEBUG("No reply. Ignoring retransmit");
530 * This is only called from the master thread
531 * when there is a child thread processing the
534 case FR_ACTION_CONFLICTING:
535 if (request->child_state == REQUEST_DONE) break;
538 * If there's a reply packet, then we presume
539 * that the child has sent the reply, and we get
540 * pinged here before the child has a chance to
543 if (request->reply->data) break;
545 RERROR("Received conflicting packet from "
546 "client %s port %d - ID: %u due to "
547 "unfinished request. Giving up on old request.",
548 request->client->shortname,
549 request->packet->src_port, request->packet->id);
553 * Called only when there's an error remembering
554 * the packet, or when the socket gets closed from
558 #ifdef HAVE_PTHREAD_H
560 * Do NOT set child_state to DONE if it's still in the queue.
562 if (we_are_master() && (request->child_state == REQUEST_QUEUED)) {
567 * If we have child threads and we're NOT the
568 * thread handling the request, don't do anything.
571 !pthread_equal(pthread_self(), request->child_pid)) {
575 #ifdef DEBUG_STATE_MACHINE
576 if (debug_flag) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n",
577 request->number, __FUNCTION__,
578 child_state_names[request->child_state],
579 child_state_names[REQUEST_DONE]);
581 request->child_state = REQUEST_DONE;
585 * Called when the child is taking too long to
586 * finish. We've already marked it "please
587 * stop", so we don't complain any more.
589 case FR_ACTION_TIMER:
594 * Child is still alive, and we're receiving more
595 * packets from the home server.
597 case FR_ACTION_PROXY_REPLY:
598 RDEBUG2("Reply from home server %s port %d - ID: %d arrived too late. Try increasing 'retry_delay' or 'max_request_time'",
599 inet_ntop(request->proxy->src_ipaddr.af,
600 &request->proxy->src_ipaddr.ipaddr,
601 buffer, sizeof(buffer)),
602 request->proxy->dst_port, request->proxy->id);
607 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
612 * Remove it from the request hash.
614 if (request->in_request_hash) {
616 if (!fr_packet_list_yank(pl, request->packet)) {
619 request->in_request_hash = false;
624 * Wait for the proxy ID to expire. This allows us to
625 * avoid re-use of proxy IDs for a while.
627 if (request->in_proxy_hash) {
628 rad_assert(request->proxy != NULL);
630 fr_event_now(el, &now);
631 when = request->proxy->timestamp;
634 if (((request->proxy->code == PW_CODE_COA_REQUEST) ||
635 (request->proxy->code == PW_CODE_DISCONNECT_REQUEST)) &&
636 (request->packet->code != request->proxy->code)) {
637 when.tv_sec += request->home_server->coa_mrd;
640 timeradd(&when, request_response_window(request), &when);
643 * We haven't received all responses, AND there's still
644 * time to wait. Do so.
646 if ((request->num_proxied_requests > request->num_proxied_responses) &&
648 (request->home_server->proto != IPPROTO_TCP) &&
650 timercmp(&now, &when, <)) {
651 RDEBUG("Waiting for more responses from the home server");
658 remove_from_proxy_hash(request);
662 #ifdef HAVE_PTHREAD_H
664 * If there's no children, we can mark the request as done.
667 request->child_state = REQUEST_DONE;
671 if (request->child_state != REQUEST_DONE) {
672 gettimeofday(&now, NULL);
677 #ifdef HAVE_PTHREAD_H
679 (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0)) {
680 RDEBUG("Waiting for child thread to stop");
685 if (request->delay < (USEC / 3)) request->delay = USEC / 3;
686 tv_add(&when, request->delay);
687 request->delay += request->delay >> 1;
688 if (request->delay > (10 * USEC)) request->delay = 10 * USEC;
690 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
694 #ifdef HAVE_PTHREAD_H
695 rad_assert(request->child_pid == NO_SUCH_CHILD_PID);
699 * @todo: do final states for TCP sockets, too?
701 request_stats_final(request);
703 if (request->listener) request->listener->count--;
706 if (request->packet) {
707 RDEBUG2("Cleaning up request packet ID %u with timestamp +%d",
709 (unsigned int) (request->timestamp - fr_start_time));
710 } /* else don't print anything */
712 if (request->ev) fr_event_delete(el, &request->ev);
714 talloc_free(request);
718 static void request_cleanup_delay_init(REQUEST *request, struct timeval const *pnow)
720 struct timeval now, when;
722 if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) goto done;
724 if (!request->root->cleanup_delay) goto done;
729 gettimeofday(&now, NULL);
732 rad_assert(request->reply->timestamp.tv_sec != 0);
733 when = request->reply->timestamp;
735 request->delay = request->root->cleanup_delay;
736 when.tv_sec += request->delay;
739 * Set timer for when we need to clean it up.
741 if (timercmp(&when, &now, >)) {
742 #ifdef DEBUG_STATE_MACHINE
743 if (debug_flag) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_cleanup_delay");
745 request->process = request_cleanup_delay;
746 request->child_state = REQUEST_DONE;
747 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
752 * Otherwise just clean it up.
755 request_done(request, FR_ACTION_DONE);
760 * Function to do all time-related events.
762 static void request_process_timer(REQUEST *request)
764 struct timeval now, when;
765 rad_assert(request->magic == REQUEST_MAGIC);
766 #ifdef DEBUG_STATE_MACHINE
767 int action = FR_ACTION_TIMER;
775 * If we originated a CoA request, divorce it from the
776 * parent. Then, set up the timers so that we can clean
777 * it up as appropriate.
779 if (request->coa) request_coa_separate(request->coa);
782 * If we're the request, OR it isn't originating a CoA
783 * request, check more things.
785 if (!request->proxy || (request->packet->code == request->proxy->code))
788 rad_assert(request->listener != NULL);
791 * The socket was closed. Tell the request that
792 * there is no point in continuing.
794 if (request->listener->status != RAD_LISTEN_STATUS_KNOWN) {
795 if ((request->master_state == REQUEST_ACTIVE) &&
796 (request->child_state < REQUEST_RESPONSE_DELAY)) {
797 WARN("Socket was closed while processing request %u: Stopping it.", request->number);
798 request->master_state = REQUEST_STOP_PROCESSING;
803 gettimeofday(&now, NULL);
806 * The request was forcibly stopped.
808 if (request->master_state == REQUEST_STOP_PROCESSING) {
809 switch (request->child_state) {
811 case REQUEST_RUNNING:
812 #ifdef HAVE_PTHREAD_H
813 rad_assert(spawn_flag == true);
818 * Sleep for some more. We HOPE that the
819 * child will become responsive at some
820 * point in the future.
823 tv_add(&when, request->delay);
824 request->delay += request->delay >> 1;
825 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
829 * These should all be managed by the master thread
832 case REQUEST_PROXIED:
834 case REQUEST_RESPONSE_DELAY:
835 case REQUEST_CLEANUP_DELAY:
838 request_done(request, FR_ACTION_DONE);
843 rad_assert(request->master_state == REQUEST_ACTIVE);
846 * It's still supposed to be running.
848 switch (request->child_state) {
850 case REQUEST_RUNNING:
851 when = request->packet->timestamp;
852 when.tv_sec += request->root->max_request_time;
855 * Taking too long: tell it to die.
857 if (timercmp(&now, &when, >=)) {
858 #ifdef HAVE_PTHREAD_H
860 * If there's a child thread processing it,
864 (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0)) {
865 ERROR("Unresponsive child for request %u, in component %s module %s",
867 request->component ? request->component : "<core>",
868 request->module ? request->module : "<core>");
869 exec_trigger(request, NULL, "server.thread.unresponsive", true);
872 request->master_state = REQUEST_STOP_PROCESSING;
874 goto delay; /* sleep some more */
877 case REQUEST_PROXIED:
878 when = request->packet->timestamp;
879 when.tv_sec += request->root->max_request_time;
881 if (timercmp(&now, &when, >=)) {
882 RWDEBUG("No response to proxied request in 'max_request_time'. Stopping it.");
883 request->master_state = REQUEST_STOP_PROCESSING;
884 request_done(request, FR_ACTION_DONE);
888 rad_assert(request->proxy != NULL);
893 if (request->packet->code != request->proxy->code) {
894 if (request->proxy_reply) {
895 request->process = coa_running;
897 request->process = coa_wait_for_reply;
902 if (request->proxy_reply) {
903 request->process = proxy_running;
905 request->process = proxy_wait_for_reply;
908 when = request->proxy->timestamp;
909 tv_add(&when, request->delay);
911 if (timercmp(&now, &when, >=)) {
912 request->process(request, FR_ACTION_TIMER);
917 * Leave the initial delay alone.
919 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
921 #endif /* WITH_PROXY */
923 case REQUEST_RESPONSE_DELAY:
924 rad_assert(request->response_delay > 0);
926 rad_assert(!request->proxy || (request->packet->code == request->proxy->code));
929 request->process = request_response_delay;
931 when = request->reply->timestamp;
933 tv_add(&when, request->response_delay * USEC);
935 if (timercmp(&when, &now, >)) {
936 #ifdef DEBUG_STATE_MACHINE
937 if (debug_flag) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_response_delay");
939 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
941 } /* else it's time to send the reject */
943 RDEBUG2("Sending delayed response");
944 DEBUG_PACKET(request, request->reply, 1);
945 request->listener->send(request->listener, request);
946 request->child_state = REQUEST_CLEANUP_DELAY;
949 case REQUEST_CLEANUP_DELAY:
950 rad_assert(request->root->cleanup_delay > 0);
953 rad_assert(!request->proxy || (request->packet->code == request->proxy->code));
956 request->process = request_cleanup_delay;
958 when = request->reply->timestamp;
959 when.tv_sec += request->root->cleanup_delay;
961 if (timercmp(&when, &now, >)) {
962 #ifdef DEBUG_STATE_MACHINE
963 if (debug_flag) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_cleanup_delay");
965 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
967 } /* else it's time to clean up */
976 static void request_queue_or_run(UNUSED REQUEST *request,
977 fr_request_process_t process)
979 #ifdef DEBUG_STATE_MACHINE
980 int action = FR_ACTION_TIMER;
986 * Do this here so that fewer other functions need to do
989 if (request->master_state == REQUEST_STOP_PROCESSING) {
990 #ifdef DEBUG_STATE_MACHINE
991 if (debug_flag) printf("(%u) ********\tSTATE %s M-%s causes C-%s-> C-%s\t********\n",
992 request->number, __FUNCTION__,
993 master_state_names[request->master_state],
994 child_state_names[request->child_state],
995 child_state_names[REQUEST_DONE]);
997 request_done(request, FR_ACTION_DONE);
1001 request->process = process;
1003 if (we_are_master()) {
1004 struct timeval when;
1007 * (re) set the initial delay.
1009 request->delay = request_init_delay(request);
1010 if (request->delay > USEC) request->delay = USEC;
1011 gettimeofday(&when, NULL);
1012 tv_add(&when, request->delay);
1013 request->delay += request->delay >> 1;
1015 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
1017 #ifdef HAVE_PTHREAD_H
1020 * A child thread will eventually pick it up.
1022 if (request_enqueue(request)) return;
1025 * Otherwise we're not going to do anything with
1028 request_done(request, FR_ACTION_DONE);
1034 request->child_state = REQUEST_RUNNING;
1035 request->process(request, FR_ACTION_RUN);
1039 * Requests that care about child process exit
1040 * codes have already either called
1041 * rad_waitpid(), or they've given up.
1043 while (waitpid(-1, NULL, WNOHANG) > 0);
1047 STATE_MACHINE_DECL(request_common)
1053 TRACE_STATE_MACHINE;
1057 * Bail out as early as possible.
1059 if (request->master_state == REQUEST_STOP_PROCESSING) {
1060 request_done(request, FR_ACTION_DONE);
1068 * We're still waiting for a proxy reply.
1070 if (request->child_state == REQUEST_PROXIED) {
1071 request->process = proxy_wait_for_reply;
1072 proxy_wait_for_reply(request, action);
1077 ERROR("(%u) Ignoring duplicate packet from "
1078 "client %s port %d - ID: %u due to unfinished request "
1079 "in component %s module %s",
1080 request->number, request->client->shortname,
1081 request->packet->src_port,request->packet->id,
1082 request->component, request->module);
1085 case FR_ACTION_CONFLICTING:
1087 * We're in the master thread, ask the child to
1088 * stop processing the request.
1090 request_done(request, action);
1093 case FR_ACTION_TIMER:
1094 request_process_timer(request);
1098 case FR_ACTION_PROXY_REPLY:
1099 RDEBUG2("Reply from home server %s port %d - ID: %d arrived too late. Try increasing 'retry_delay' or 'max_request_time'",
1100 inet_ntop(request->proxy->dst_ipaddr.af,
1101 &request->proxy->dst_ipaddr.ipaddr,
1102 buffer, sizeof(buffer)),
1103 request->proxy->dst_port, request->proxy->id);
1108 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
1113 STATE_MACHINE_DECL(request_cleanup_delay)
1115 struct timeval when;
1117 TRACE_STATE_MACHINE;
1122 if (request->reply->code != 0) {
1123 request->listener->send(request->listener, request);
1125 RDEBUG("No reply. Ignoring retransmit");
1129 * Double the cleanup_delay to catch retransmits.
1131 when = request->reply->timestamp;
1132 request->delay += request->delay ;
1133 when.tv_sec += request->delay;
1135 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
1138 case FR_ACTION_CONFLICTING:
1139 request_done(request, FR_ACTION_DONE);
1143 case FR_ACTION_PROXY_REPLY:
1145 case FR_ACTION_TIMER:
1146 request_common(request, action);
1150 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
1155 STATE_MACHINE_DECL(request_response_delay)
1157 TRACE_STATE_MACHINE;
1162 ERROR("(%u) Discarding duplicate request from "
1163 "client %s port %d - ID: %u due to delayed response",
1164 request->number, request->client->shortname,
1165 request->packet->src_port,request->packet->id);
1169 case FR_ACTION_PROXY_REPLY:
1171 case FR_ACTION_CONFLICTING:
1172 case FR_ACTION_TIMER:
1173 request_common(request, action);
1177 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
1183 static int CC_HINT(nonnull) request_pre_handler(REQUEST *request, UNUSED int action)
1185 TRACE_STATE_MACHINE;
1189 if (request->master_state == REQUEST_STOP_PROCESSING) return 0;
1192 * Don't decode the packet if it's an internal "fake"
1193 * request. Instead, just return so that the caller can
1196 if (request->packet->dst_port == 0) {
1197 request->username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1198 request->password = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
1202 if (!request->packet->vps) { /* FIXME: check for correct state */
1203 rcode = request->listener->decode(request->listener, request);
1206 if (debug_condition) {
1208 * Ignore parse errors.
1210 if (radius_evaluate_cond(request, RLM_MODULE_OK, 0, debug_condition)) {
1211 request->log.lvl = L_DBG_LVL_2;
1212 request->log.func = vradlog_request;
1217 DEBUG_PACKET(request, request->packet, 0);
1223 RDEBUG("Dropping packet without response because of error: %s", fr_strerror());
1224 request->reply->offset = -2; /* bad authenticator */
1228 if (!request->username) {
1229 request->username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1235 STATE_MACHINE_DECL(request_finish)
1239 TRACE_STATE_MACHINE;
1241 (void) action; /* -Wunused */
1243 if (request->master_state == REQUEST_STOP_PROCESSING) {
1249 * Don't send replies if there are none to send.
1251 if (!request->in_request_hash) {
1253 if ((request->listener->type == RAD_LISTEN_AUTH)
1254 #ifdef WITH_ACCOUNTING
1255 || (request->listener->type == RAD_LISTEN_ACCT)
1258 listen_socket_t *sock = request->listener->data;
1260 if (sock->proto == IPPROTO_UDP) return;
1263 * TCP packets aren't in the request
1274 * Override the response code if a control:Response-Packet-Type attribute is present.
1276 vp = pairfind(request->config_items, PW_RESPONSE_PACKET_TYPE, 0, TAG_ANY);
1278 if (vp->vp_integer == 256) {
1279 RDEBUG2("Not responding to request");
1280 request->reply->code = 0;
1282 request->reply->code = vp->vp_integer;
1286 * Catch Auth-Type := Reject BEFORE proxying the packet.
1288 else if (request->packet->code == PW_CODE_AUTHENTICATION_REQUEST) {
1289 if (request->reply->code == 0) {
1290 vp = pairfind(request->config_items, PW_AUTH_TYPE, 0, TAG_ANY);
1292 if (!vp || (vp->vp_integer != PW_CODE_AUTHENTICATION_REJECT)) {
1293 RDEBUG2("There was no response configured: "
1294 "rejecting request");
1297 request->reply->code = PW_CODE_AUTHENTICATION_REJECT;
1302 * Copy Proxy-State from the request to the reply.
1304 vp = paircopy2(request->reply, request->packet->vps,
1305 PW_PROXY_STATE, 0, TAG_ANY);
1306 if (vp) pairadd(&request->reply->vps, vp);
1308 switch (request->reply->code) {
1309 case PW_CODE_AUTHENTICATION_ACK:
1310 rad_postauth(request);
1312 case PW_CODE_ACCESS_CHALLENGE:
1313 pairdelete(&request->config_items, PW_POST_AUTH_TYPE, 0,
1315 vp = pairmake_config("Post-Auth-Type", "Challenge", T_OP_SET);
1316 if (vp) rad_postauth(request);
1323 * Run rejected packets through
1325 * Post-Auth-Type = Reject
1327 * We do this separately so ACK and challenge can change the code
1328 * to reject if a module returns reject.
1330 if (request->reply->code == PW_CODE_AUTHENTICATION_REJECT) {
1331 pairdelete(&request->config_items, PW_POST_AUTH_TYPE, 0, TAG_ANY);
1332 vp = pairmake_config("Post-Auth-Type", "Reject", T_OP_SET);
1333 if (vp) rad_postauth(request);
1337 * Clean up. These are no longer needed.
1339 pairfree(&request->config_items);
1341 pairfree(&request->packet->vps);
1342 request->username = NULL;
1343 request->password = NULL;
1346 if (request->proxy) {
1347 pairfree(&request->proxy->vps);
1349 if (request->proxy_reply) {
1350 pairfree(&request->proxy_reply->vps);
1354 gettimeofday(&request->reply->timestamp, NULL);
1357 * Ignore all "do not respond" packets.
1359 if (!request->reply->code) {
1360 RDEBUG("Not sending reply");
1365 * See if we need to delay an Access-Reject packet.
1367 if ((request->reply->code == PW_CODE_AUTHENTICATION_REJECT) &&
1368 (request->root->reject_delay > 0)) {
1369 request->response_delay = request->root->reject_delay;
1373 * If we timed out a proxy packet, don't delay
1374 * the reject any more.
1376 if (request->proxy && !request->proxy_reply) {
1377 request->response_delay = 0;
1386 if (!request->response_delay) {
1387 DEBUG_PACKET(request, request->reply, 1);
1388 request->listener->send(request->listener,
1392 pairfree(&request->reply->vps);
1394 RDEBUG2("Finished request");
1395 #ifdef WITH_ACCOUNTING
1396 if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) {
1398 request->child_state = REQUEST_DONE;
1402 if (request->root->cleanup_delay == 0) {
1404 request->child_state = REQUEST_DONE;
1407 request->child_state = REQUEST_CLEANUP_DELAY;
1410 RDEBUG2("Delaying response for %d seconds",
1411 request->response_delay);
1413 request->child_state = REQUEST_RESPONSE_DELAY;
1417 STATE_MACHINE_DECL(request_running)
1419 TRACE_STATE_MACHINE;
1422 case FR_ACTION_TIMER:
1423 request_process_timer(request);
1426 case FR_ACTION_CONFLICTING:
1428 request_common(request, action);
1433 * This can happen due to a race condition where
1434 * we send a proxied request, and immediately get
1435 * another reply, before the timer has a chance
1436 * to update the various states.
1438 case FR_ACTION_PROXY_REPLY:
1439 request->child_state = REQUEST_RUNNING;
1440 request->process = proxy_running;
1441 request->process(request, FR_ACTION_RUN);
1446 if (!request_pre_handler(request, action)) {
1447 #ifdef DEBUG_STATE_MACHINE
1448 if (debug_flag) printf("(%u) ********\tSTATE %s failed in pre-handler C-%s -> C-%s\t********\n",
1449 request->number, __FUNCTION__,
1450 child_state_names[request->child_state],
1451 child_state_names[REQUEST_DONE]);
1455 request->child_state = REQUEST_DONE;
1459 rad_assert(request->handle != NULL);
1460 request->handle(request);
1464 * We may need to send a proxied request.
1466 if ((action == FR_ACTION_RUN) &&
1467 request_will_proxy(request)) {
1468 #ifdef DEBUG_STATE_MACHINE
1469 if (debug_flag) printf("(%u) ********\tWill Proxy\t********\n", request->number);
1473 * takes care of setting
1474 * up the post proxy fail
1477 if (request_proxy(request, 0) < 0) goto finished;
1481 #ifdef DEBUG_STATE_MACHINE
1482 if (debug_flag) printf("(%u) ********\tFinished\t********\n", request->number);
1487 * Maybe originate a CoA request.
1489 if ((action == FR_ACTION_RUN) && request->coa) {
1490 request_coa_originate(request);
1497 request_finish(request, action);
1502 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
1507 int request_receive(rad_listen_t *listener, RADIUS_PACKET *packet,
1508 RADCLIENT *client, RAD_REQUEST_FUNP fun)
1511 RADIUS_PACKET **packet_p;
1512 REQUEST *request = NULL;
1514 listen_socket_t *sock = NULL;
1517 * Set the last packet received.
1519 gettimeofday(&now, NULL);
1521 #ifdef WITH_ACCOUNTING
1522 if (listener->type != RAD_LISTEN_DETAIL)
1525 sock = listener->data;
1526 sock->last_packet = now.tv_sec;
1528 packet->timestamp = now;
1531 * Skip everything if required.
1533 if (listener->nodup) goto skip_dup;
1535 packet_p = fr_packet_list_find(pl, packet);
1537 request = fr_packet2myptr(REQUEST, packet, packet_p);
1538 rad_assert(request->in_request_hash);
1541 * Same src/dst ip/port, length, and
1542 * authentication vector: must be a duplicate.
1544 if ((request->packet->data_len == packet->data_len) &&
1545 (memcmp(request->packet->vector, packet->vector,
1546 sizeof(packet->vector)) == 0)) {
1549 * If the request is running, it'
1551 if (request->child_state != REQUEST_DONE) {
1552 request->process(request, FR_ACTION_DUP);
1555 switch (packet->code) {
1556 case PW_CODE_AUTHENTICATION_REQUEST:
1557 FR_STATS_INC(auth, total_dup_requests);
1560 #ifdef WITH_ACCOUNTING
1561 case PW_CODE_ACCOUNTING_REQUEST:
1562 FR_STATS_INC(acct, total_dup_requests);
1566 case PW_CODE_COA_REQUEST:
1567 FR_STATS_INC(coa, total_dup_requests);
1570 case PW_CODE_DISCONNECT_REQUEST:
1571 FR_STATS_INC(dsc, total_dup_requests);
1578 #endif /* WITH_STATS */
1579 return 0; /* duplicate of live request */
1581 #ifdef HAVE_PTHREAD_H
1583 * There should no longer be a child
1584 * thread associated with this request.
1586 rad_assert(pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) != 0);
1590 * Clean up the old request, and allow
1591 * the new one to continue.
1593 request_done(request, FR_ACTION_DONE);
1598 * Say we're ignoring the old one, and continue
1599 * to process the new one.
1601 request->process(request, FR_ACTION_CONFLICTING);
1607 * Quench maximum number of outstanding requests.
1609 if (main_config.max_requests &&
1610 ((count = fr_packet_list_num_elements(pl)) > main_config.max_requests)) {
1611 RATE_LIMIT(ERROR("Dropping request (%d is too many): from client %s port %d - ID: %d", count,
1613 packet->src_port, packet->id);
1614 WARN("Please check the configuration file.\n"
1615 "\tThe value for 'max_requests' is probably set too low.\n"));
1617 exec_trigger(NULL, NULL, "server.max_requests", true);
1623 * Rate-limit the incoming packets
1625 if (sock && sock->max_rate) {
1628 pps = rad_pps(&sock->rate_pps_old, &sock->rate_pps_now, &sock->rate_time, &now);
1629 if (pps > sock->max_rate) {
1630 DEBUG("Dropping request due to rate limiting");
1633 sock->rate_pps_now++;
1636 request = request_setup(listener, packet, client, fun);
1637 if (!request) return 1;
1640 * Remember the request in the list.
1642 if (!listener->nodup) {
1643 if (!fr_packet_list_insert(pl, &request->packet)) {
1644 RERROR("Failed to insert request in the list of live requests: discarding it");
1645 request_done(request, FR_ACTION_DONE);
1649 request->in_request_hash = true;
1653 * Process it. Send a response, and free it.
1655 if (listener->synchronous) {
1656 request->listener->decode(request->listener, request);
1657 request->username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1658 request->password = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
1662 if (request->reply->code != 0) {
1663 request->listener->send(request->listener, request);
1665 RDEBUG("Not sending reply");
1667 talloc_free(request);
1672 * Otherwise, insert it into the state machine.
1673 * The child threads will take care of processing it.
1675 request_queue_or_run(request, request_running);
1681 static REQUEST *request_setup(rad_listen_t *listener, RADIUS_PACKET *packet,
1682 RADCLIENT *client, RAD_REQUEST_FUNP fun)
1687 * Create and initialize the new request.
1689 request = request_alloc(NULL);
1690 request->reply = rad_alloc(request, 0);
1691 if (!request->reply) {
1693 talloc_free(request);
1697 request->listener = listener;
1698 request->client = client;
1699 request->packet = talloc_steal(request, packet);
1700 request->number = request_num_counter++;
1701 request->priority = listener->type;
1702 request->master_state = REQUEST_ACTIVE;
1703 #ifdef DEBUG_STATE_MACHINE
1704 if (debug_flag) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n",
1705 request->number, __FUNCTION__,
1706 child_state_names[request->child_state],
1707 child_state_names[REQUEST_RUNNING]);
1709 request->child_state = REQUEST_RUNNING;
1710 request->handle = fun;
1714 request->listener->stats.last_packet = request->packet->timestamp.tv_sec;
1715 if (packet->code == PW_CODE_AUTHENTICATION_REQUEST) {
1716 request->client->auth.last_packet = request->packet->timestamp.tv_sec;
1717 radius_auth_stats.last_packet = request->packet->timestamp.tv_sec;
1718 #ifdef WITH_ACCOUNTING
1719 } else if (packet->code == PW_CODE_ACCOUNTING_REQUEST) {
1720 request->client->acct.last_packet = request->packet->timestamp.tv_sec;
1721 radius_acct_stats.last_packet = request->packet->timestamp.tv_sec;
1724 #endif /* WITH_STATS */
1727 * Status-Server packets go to the head of the queue.
1729 if (request->packet->code == PW_CODE_STATUS_SERVER) request->priority = 0;
1732 * Set virtual server identity
1734 if (client->server) {
1735 request->server = client->server;
1736 } else if (listener->server) {
1737 request->server = listener->server;
1739 request->server = NULL;
1742 request->root = &main_config;
1744 request->listener->count++;
1748 * The request passes many of our sanity checks.
1749 * From here on in, if anything goes wrong, we
1750 * send a reject message, instead of dropping the
1755 * Build the reply template from the request.
1758 request->reply->sockfd = request->packet->sockfd;
1759 request->reply->dst_ipaddr = request->packet->src_ipaddr;
1760 request->reply->src_ipaddr = request->packet->dst_ipaddr;
1761 request->reply->dst_port = request->packet->src_port;
1762 request->reply->src_port = request->packet->dst_port;
1763 request->reply->id = request->packet->id;
1764 request->reply->code = 0; /* UNKNOWN code */
1765 memcpy(request->reply->vector, request->packet->vector,
1766 sizeof(request->reply->vector));
1767 request->reply->vps = NULL;
1768 request->reply->data = NULL;
1769 request->reply->data_len = 0;
1775 /***********************************************************************
1779 ***********************************************************************/
1782 * Timer function for all TCP sockets.
1784 static void tcp_socket_timer(void *ctx)
1786 rad_listen_t *listener = ctx;
1787 listen_socket_t *sock = listener->data;
1788 struct timeval end, now;
1790 fr_socket_limit_t *limit;
1794 fr_event_now(el, &now);
1796 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return;
1798 switch (listener->type) {
1800 case RAD_LISTEN_PROXY:
1801 limit = &sock->home->limit;
1805 case RAD_LISTEN_AUTH:
1806 #ifdef WITH_ACCOUNTING
1807 case RAD_LISTEN_ACCT:
1809 limit = &sock->limit;
1817 * If we enforce a lifetime, do it now.
1819 if (limit->lifetime > 0) {
1820 end.tv_sec = sock->opened + limit->lifetime;
1823 if (timercmp(&end, &now, <=)) {
1824 listener->print(listener, buffer, sizeof(buffer));
1825 DEBUG("Reached maximum lifetime on socket %s", buffer);
1829 listener->status = RAD_LISTEN_STATUS_EOL;
1830 event_new_fd(listener);
1839 * Enforce an idle timeout.
1841 if (limit->idle_timeout > 0) {
1842 struct timeval idle;
1844 rad_assert(sock->last_packet != 0);
1845 idle.tv_sec = sock->last_packet + limit->idle_timeout;
1848 if (timercmp(&idle, &now, <=)) {
1849 listener->print(listener, buffer, sizeof(buffer));
1850 DEBUG("Reached idle timeout on socket %s", buffer);
1855 * Enforce the minimum of idle timeout or lifetime.
1857 if (timercmp(&idle, &end, <)) {
1863 * Wake up at t + 0.5s. The code above checks if the timers
1864 * are <= t. This addition gives us a bit of leeway.
1866 end.tv_usec = USEC / 2;
1868 if (!fr_event_insert(el, tcp_socket_timer, listener, &end, &sock->ev)) {
1869 rad_panic("Failed to insert event");
1876 * Add +/- 2s of jitter, as suggested in RFC 3539
1879 static void add_jitter(struct timeval *when)
1886 jitter ^= (jitter >> 10);
1887 jitter &= ((1 << 22) - 1); /* 22 bits of 1 */
1890 * Add in ~ (4 * USEC) of jitter.
1892 tv_add(when, jitter);
1896 * Called by socket_del to remove requests with this socket
1898 static int eol_proxy_listener(void *ctx, void *data)
1900 rad_listen_t *this = ctx;
1901 RADIUS_PACKET **proxy_p = data;
1904 request = fr_packet2myptr(REQUEST, proxy, proxy_p);
1905 if (request->proxy_listener != this) return 0;
1908 * The normal "remove_from_proxy_hash" tries to grab the
1909 * proxy mutex. We already have it held, so grabbing it
1910 * again will cause a deadlock. Instead, call the "no
1911 * lock" version of the function.
1913 rad_assert(request->in_proxy_hash == true);
1914 remove_from_proxy_hash_nl(request, false);
1917 * Don't mark it as DONE. The client can retransmit, and
1918 * the packet SHOULD be re-proxied somewhere else.
1920 * Return "2" means that the rbtree code will remove it
1921 * from the tree, and we don't need to do it ourselves.
1925 #endif /* WITH_PROXY */
1927 static int eol_listener(void *ctx, void *data)
1929 rad_listen_t *this = ctx;
1930 RADIUS_PACKET **packet_p = data;
1933 request = fr_packet2myptr(REQUEST, packet, packet_p);
1934 if (request->listener != this) return 0;
1936 request->master_state = REQUEST_STOP_PROCESSING;
1940 #endif /* WITH_TCP */
1943 /***********************************************************************
1945 * Proxy handlers for the state machine.
1947 ***********************************************************************/
1950 * Called with the proxy mutex held
1952 static void remove_from_proxy_hash_nl(REQUEST *request, bool yank)
1954 if (!request->in_proxy_hash) return;
1956 fr_packet_list_id_free(proxy_list, request->proxy, yank);
1957 request->in_proxy_hash = false;
1960 * On the FIRST reply, decrement the count of outstanding
1961 * requests. Note that this is NOT the count of sent
1962 * packets, but whether or not the home server has
1965 if (request->home_server &&
1966 request->home_server->currently_outstanding) {
1967 request->home_server->currently_outstanding--;
1970 * If we're NOT sending it packets, then we don't know
1971 * if it's alive or dead.
1973 if ((request->home_server->currently_outstanding == 0) &&
1974 (request->home_server->state == HOME_STATE_ALIVE)) {
1975 request->home_server->state = HOME_STATE_UNKNOWN;
1976 request->home_server->last_packet_sent = 0;
1977 request->home_server->last_packet_recv = 0;
1982 rad_assert(request->proxy_listener != NULL);
1983 request->proxy_listener->count--;
1985 request->proxy_listener = NULL;
1988 * Got from YES in hash, to NO, not in hash while we hold
1989 * the mutex. This guarantees that when another thread
1990 * grabs the mutex, the "not in hash" flag is correct.
1992 RDEBUG3("proxy: request is no longer in proxy hash");
1995 static void remove_from_proxy_hash(REQUEST *request)
1998 * Check this without grabbing the mutex because it's a
1999 * lot faster that way.
2001 if (!request->in_proxy_hash) return;
2004 * The "not in hash" flag is definitive. However, if the
2005 * flag says that it IS in the hash, there might still be
2006 * a race condition where it isn't.
2008 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2010 if (!request->in_proxy_hash) {
2011 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2015 remove_from_proxy_hash_nl(request, true);
2017 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2020 static int insert_into_proxy_hash(REQUEST *request)
2024 void *proxy_listener;
2026 rad_assert(request->proxy != NULL);
2027 rad_assert(request->home_server != NULL);
2028 rad_assert(proxy_list != NULL);
2031 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2032 proxy_listener = NULL;
2033 request->num_proxied_requests = 1;
2034 request->num_proxied_responses = 0;
2036 for (tries = 0; tries < 2; tries++) {
2038 listen_socket_t *sock;
2040 RDEBUG3("proxy: Trying to allocate ID (%d/2)", tries);
2041 rcode = fr_packet_list_id_alloc(proxy_list,
2042 request->home_server->proto,
2043 &request->proxy, &proxy_listener);
2044 if ((debug_flag > 2) && (rcode == 0)) {
2045 RDEBUG("proxy: Failed allocating ID: %s", fr_strerror());
2047 if (rcode > 0) break;
2048 if (tries > 0) continue; /* try opening new socket only once */
2050 #ifdef HAVE_PTHREAD_H
2051 if (proxy_no_new_sockets) break;
2054 RDEBUG3("proxy: Trying to open a new listener to the home server");
2055 this = proxy_new_listener(request->home_server, 0);
2057 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2061 request->proxy->src_port = 0; /* Use any new socket */
2062 proxy_listener = this;
2065 if (!fr_packet_list_socket_add(proxy_list, this->fd,
2067 &sock->other_ipaddr, sock->other_port,
2070 #ifdef HAVE_PTHREAD_H
2071 proxy_no_new_sockets = true;
2073 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2076 * This is bad. However, the
2077 * packet list now supports 256
2078 * open sockets, which should
2079 * minimize this problem.
2081 ERROR("Failed adding proxy socket: %s",
2087 * Add it to the event loop. Ensure that we have
2088 * only one mutex locked at a time.
2090 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2091 radius_update_listener(this);
2092 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2095 if (!proxy_listener || (rcode == 0)) {
2096 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2097 REDEBUG2("proxy: Failed allocating Id for proxied request");
2099 request->proxy_listener = NULL;
2100 request->in_proxy_hash = false;
2104 rad_assert(request->proxy->id >= 0);
2106 request->proxy_listener = proxy_listener;
2107 request->in_proxy_hash = true;
2108 RDEBUG3("proxy: request is now in proxy hash");
2111 * Keep track of maximum outstanding requests to a
2112 * particular home server. 'max_outstanding' is
2113 * enforced in home_server_ldb(), in realms.c.
2115 request->home_server->currently_outstanding++;
2118 request->proxy_listener->count++;
2121 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2123 RDEBUG3("proxy: allocating destination %s port %d - Id %d",
2124 inet_ntop(request->proxy->dst_ipaddr.af,
2125 &request->proxy->dst_ipaddr.ipaddr, buf, sizeof(buf)),
2126 request->proxy->dst_port,
2127 request->proxy->id);
2132 static int process_proxy_reply(REQUEST *request, RADIUS_PACKET *reply)
2135 int post_proxy_type = 0;
2139 * There may be a proxy reply, but it may be too late.
2141 if (!request->proxy_listener) return 0;
2144 * Delete any reply we had accumulated until now.
2146 pairfree(&request->reply->vps);
2149 * Run the packet through the post-proxy stage,
2150 * BEFORE playing games with the attributes.
2152 vp = pairfind(request->config_items, PW_POST_PROXY_TYPE, 0, TAG_ANY);
2155 * If we have a proxy_reply, and it was a reject, setup
2156 * post-proxy-type Reject
2159 reply->code == PW_CODE_AUTHENTICATION_REJECT) {
2162 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Reject");
2164 vp = radius_paircreate(request, &request->config_items,
2165 PW_POST_PROXY_TYPE, 0);
2167 vp->vp_integer = dval->value;
2172 post_proxy_type = vp->vp_integer;
2174 RDEBUG2("Found Post-Proxy-Type %s", dict_valnamebyattr(PW_POST_PROXY_TYPE, 0, post_proxy_type));
2179 * Decode the packet.
2181 rcode = request->proxy_listener->decode(request->proxy_listener, request);
2182 DEBUG_PACKET(request, reply, 0);
2185 * Pro-actively remove it from the proxy hash.
2186 * This is later than in 2.1.x, but it means that
2187 * the replies are authenticated before being
2188 * removed from the hash.
2191 (request->num_proxied_requests <= request->num_proxied_responses)) {
2192 remove_from_proxy_hash(request);
2195 remove_from_proxy_hash(request);
2198 if (request->home_pool && request->home_pool->virtual_server) {
2199 char const *old_server = request->server;
2201 request->server = request->home_pool->virtual_server;
2202 RDEBUG2("server %s {", request->server);
2204 rcode = process_post_proxy(post_proxy_type, request);
2207 request->server = old_server;
2209 rcode = process_post_proxy(post_proxy_type, request);
2213 if (request->packet->code == request->proxy->code)
2215 * Don't run the next bit if we originated a CoA
2216 * packet, after receiving an Access-Request or
2217 * Accounting-Request.
2222 * There may NOT be a proxy reply, as we may be
2223 * running Post-Proxy-Type = Fail.
2226 pairadd(&request->reply->vps, paircopy(request->reply, reply->vps));
2229 * Delete the Proxy-State Attributes from
2230 * the reply. These include Proxy-State
2231 * attributes from us and remote server.
2233 pairdelete(&request->reply->vps, PW_PROXY_STATE, 0, TAG_ANY);
2237 default: /* Don't do anything */
2239 case RLM_MODULE_FAIL:
2242 case RLM_MODULE_HANDLED:
2249 int request_proxy_reply(RADIUS_PACKET *packet)
2251 RADIUS_PACKET **proxy_p;
2256 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2257 proxy_p = fr_packet_list_find_byreply(proxy_list, packet);
2260 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2261 PROXY( "No outstanding request was found for reply from host %s port %d - ID %u",
2262 inet_ntop(packet->src_ipaddr.af,
2263 &packet->src_ipaddr.ipaddr,
2264 buffer, sizeof(buffer)),
2265 packet->src_port, packet->id);
2269 request = fr_packet2myptr(REQUEST, proxy, proxy_p);
2270 request->num_proxied_responses++; /* needs to be protected by lock */
2272 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2275 * No reply, BUT the current packet fails verification:
2276 * ignore it. This does the MD5 calculations in the
2277 * server core, but I guess we can fix that later.
2279 if (!request->proxy_reply &&
2280 (rad_verify(packet, request->proxy,
2281 request->home_server->secret) != 0)) {
2282 DEBUG("Ignoring spoofed proxy reply. Signature is invalid");
2287 * The home server sent us a packet which doesn't match
2288 * something we have: ignore it. This is done only to
2289 * catch the case of broken systems.
2291 if (request->proxy_reply &&
2292 (memcmp(request->proxy_reply->vector,
2294 sizeof(request->proxy_reply->vector)) != 0)) {
2295 RDEBUG2("Ignoring conflicting proxy reply");
2299 gettimeofday(&now, NULL);
2302 * Status-Server packets don't count as real packets.
2304 if (request->proxy->code != PW_CODE_STATUS_SERVER) {
2305 listen_socket_t *sock = request->proxy_listener->data;
2307 request->home_server->last_packet_recv = now.tv_sec;
2308 sock->last_packet = now.tv_sec;
2312 * If we have previously seen a reply, ignore the
2315 if (request->proxy_reply) {
2316 RDEBUG2("Discarding duplicate reply from host %s port %d - ID: %d",
2317 inet_ntop(packet->src_ipaddr.af,
2318 &packet->src_ipaddr.ipaddr,
2319 buffer, sizeof(buffer)),
2320 packet->src_port, packet->id);
2325 * Call the state machine to do something useful with the
2328 request->proxy_reply = talloc_steal(request, packet);
2329 packet->timestamp = now;
2330 request->priority = RAD_LISTEN_PROXY;
2333 * We've received a reply. If we hadn't been sending it
2334 * packets for a while, just mark it alive.
2336 if (request->home_server->state == HOME_STATE_UNKNOWN) {
2337 request->home_server->state = HOME_STATE_ALIVE;
2338 request->home_server->response_timeouts = 0;
2342 request->home_server->stats.last_packet = packet->timestamp.tv_sec;
2343 request->proxy_listener->stats.last_packet = packet->timestamp.tv_sec;
2345 if (request->proxy->code == PW_CODE_AUTHENTICATION_REQUEST) {
2346 proxy_auth_stats.last_packet = packet->timestamp.tv_sec;
2347 #ifdef WITH_ACCOUNTING
2348 } else if (request->proxy->code == PW_CODE_ACCOUNTING_REQUEST) {
2349 proxy_acct_stats.last_packet = packet->timestamp.tv_sec;
2352 #endif /* WITH_STATS */
2356 * When we originate CoA requests, we patch them in here
2357 * so that they don't affect the rest of the state
2360 if (request->parent) {
2361 rad_assert(request->parent->coa == request);
2362 rad_assert((request->proxy->code == PW_CODE_COA_REQUEST) ||
2363 (request->proxy->code == PW_CODE_DISCONNECT_REQUEST));
2364 rad_assert(request->process != NULL);
2365 request_coa_separate(request);
2369 request->process(request, FR_ACTION_PROXY_REPLY);
2375 static int setup_post_proxy_fail(REQUEST *request)
2377 DICT_VALUE const *dval = NULL;
2380 if (request->proxy->code == PW_CODE_AUTHENTICATION_REQUEST) {
2381 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0,
2382 "Fail-Authentication");
2384 } else if (request->proxy->code == PW_CODE_ACCOUNTING_REQUEST) {
2385 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0,
2388 } else if (request->proxy->code == PW_CODE_COA_REQUEST) {
2389 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-CoA");
2391 } else if (request->proxy->code == PW_CODE_DISCONNECT_REQUEST) {
2392 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-Disconnect");
2395 WARN("Unknown packet type in Post-Proxy-Type Fail: ignoring");
2399 if (!dval) dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail");
2402 pairdelete(&request->config_items, PW_POST_PROXY_TYPE, 0, TAG_ANY);
2406 vp = pairfind(request->config_items, PW_POST_PROXY_TYPE, 0, TAG_ANY);
2407 if (!vp) vp = radius_paircreate(request, &request->config_items,
2408 PW_POST_PROXY_TYPE, 0);
2409 vp->vp_integer = dval->value;
2414 STATE_MACHINE_DECL(proxy_no_reply)
2416 TRACE_STATE_MACHINE;
2419 case FR_ACTION_CONFLICTING:
2421 case FR_ACTION_TIMER:
2422 case FR_ACTION_PROXY_REPLY:
2423 request_common(request, action);
2427 if (process_proxy_reply(request, NULL)) {
2428 request_finish(request, action);
2430 request_done(request, FR_ACTION_DONE);
2434 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
2439 STATE_MACHINE_DECL(proxy_running)
2441 TRACE_STATE_MACHINE;
2444 case FR_ACTION_CONFLICTING:
2446 case FR_ACTION_TIMER:
2447 case FR_ACTION_PROXY_REPLY:
2448 request_common(request, action);
2452 if (process_proxy_reply(request, request->proxy_reply)) {
2453 request->handle(request);
2454 request_finish(request, action);
2456 request_done(request, FR_ACTION_DONE);
2461 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
2466 static int request_will_proxy(REQUEST *request)
2468 int rcode, pre_proxy_type = 0;
2469 char const *realmname = NULL;
2470 VALUE_PAIR *vp, *strippedname;
2471 home_server_t *home;
2472 REALM *realm = NULL;
2473 home_pool_t *pool = NULL;
2475 if (!request->root->proxy_requests) return 0;
2476 if (request->packet->dst_port == 0) return 0;
2477 if (request->packet->code == PW_CODE_STATUS_SERVER) return 0;
2478 if (request->in_proxy_hash) return 0;
2481 * FIXME: for 3.0, allow this only for rejects?
2483 if (request->reply->code != 0) return 0;
2485 vp = pairfind(request->config_items, PW_PROXY_TO_REALM, 0, TAG_ANY);
2487 realm = realm_find2(vp->vp_strvalue);
2489 REDEBUG2("Cannot proxy to unknown realm %s",
2494 realmname = vp->vp_strvalue;
2497 * Figure out which pool to use.
2499 if (request->packet->code == PW_CODE_AUTHENTICATION_REQUEST) {
2500 pool = realm->auth_pool;
2502 #ifdef WITH_ACCOUNTING
2503 } else if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) {
2504 pool = realm->acct_pool;
2508 } else if ((request->packet->code == PW_CODE_COA_REQUEST) ||
2509 (request->packet->code == PW_CODE_DISCONNECT_REQUEST)) {
2510 pool = realm->coa_pool;
2520 vp = pairfind(request->config_items, PW_HOME_SERVER_POOL, 0, TAG_ANY);
2523 switch (request->packet->code) {
2524 case PW_CODE_AUTHENTICATION_REQUEST:
2525 pool_type = HOME_TYPE_AUTH;
2528 #ifdef WITH_ACCOUNTING
2529 case PW_CODE_ACCOUNTING_REQUEST:
2530 pool_type = HOME_TYPE_ACCT;
2535 case PW_CODE_COA_REQUEST:
2536 case PW_CODE_DISCONNECT_REQUEST:
2537 pool_type = HOME_TYPE_COA;
2545 pool = home_pool_byname(vp->vp_strvalue, pool_type);
2549 RWDEBUG2("Cancelling proxy as no home pool exists");
2553 if (request->listener->synchronous) {
2554 WARN("Cannot proxy a request which is from a 'synchronous' socket");
2558 request->home_pool = pool;
2560 home = home_server_ldb(realmname, pool, request);
2562 REDEBUG2("Failed to find live home server: Cancelling proxy");
2565 home_server_update_request(home, request);
2569 * Once we've decided to proxy a request, we cannot send
2570 * a CoA packet. So we free up any CoA packet here.
2572 if (request->coa) request_done(request->coa, FR_ACTION_DONE);
2576 * Remember that we sent the request to a Realm.
2578 if (realmname) pairmake_packet("Realm", realmname, T_OP_EQ);
2581 * Strip the name, if told to.
2583 * Doing it here catches the case of proxied tunneled
2586 if (realm && (realm->striprealm == true) &&
2587 (strippedname = pairfind(request->proxy->vps, PW_STRIPPED_USER_NAME, 0, TAG_ANY)) != NULL) {
2589 * If there's a Stripped-User-Name attribute in
2590 * the request, then use THAT as the User-Name
2591 * for the proxied request, instead of the
2594 * This is done by making a copy of the
2595 * Stripped-User-Name attribute, turning it into
2596 * a User-Name attribute, deleting the
2597 * Stripped-User-Name and User-Name attributes
2598 * from the vps list, and making the new
2599 * User-Name the head of the vps list.
2601 vp = pairfind(request->proxy->vps, PW_USER_NAME, 0, TAG_ANY);
2604 vp = radius_paircreate(NULL, NULL,
2606 rad_assert(vp != NULL); /* handled by above function */
2607 /* Insert at the START of the list */
2608 /* FIXME: Can't make assumptions about ordering */
2609 fr_cursor_init(&cursor, &vp);
2610 fr_cursor_insert(&cursor, request->proxy->vps);
2611 request->proxy->vps = vp;
2613 pairstrcpy(vp, strippedname->vp_strvalue);
2616 * Do NOT delete Stripped-User-Name.
2621 * If there is no PW_CHAP_CHALLENGE attribute but
2622 * there is a PW_CHAP_PASSWORD we need to add it
2623 * since we can't use the request authenticator
2624 * anymore - we changed it.
2626 if ((request->packet->code == PW_CODE_AUTHENTICATION_REQUEST) &&
2627 pairfind(request->proxy->vps, PW_CHAP_PASSWORD, 0, TAG_ANY) &&
2628 pairfind(request->proxy->vps, PW_CHAP_CHALLENGE, 0, TAG_ANY) == NULL) {
2629 vp = radius_paircreate(request->proxy, &request->proxy->vps, PW_CHAP_CHALLENGE, 0);
2630 pairmemcpy(vp, request->packet->vector, sizeof(request->packet->vector));
2634 * The RFC's say we have to do this, but FreeRADIUS
2637 vp = radius_paircreate(request->proxy, &request->proxy->vps, PW_PROXY_STATE, 0);
2638 pairsprintf(vp, "%u", request->packet->id);
2641 * Should be done BEFORE inserting into proxy hash, as
2642 * pre-proxy may use this information, or change it.
2644 request->proxy->code = request->packet->code;
2647 * Call the pre-proxy routines.
2649 vp = pairfind(request->config_items, PW_PRE_PROXY_TYPE, 0, TAG_ANY);
2651 DICT_VALUE const *dval = dict_valbyattr(vp->da->attr, vp->da->vendor, vp->vp_integer);
2652 /* Must be a validation issue */
2654 RDEBUG2("Found Pre-Proxy-Type %s", dval->name);
2655 pre_proxy_type = vp->vp_integer;
2658 rad_assert(request->home_pool != NULL);
2660 if (request->home_pool->virtual_server) {
2661 char const *old_server = request->server;
2663 request->server = request->home_pool->virtual_server;
2665 RDEBUG2("server %s {", request->server);
2667 rcode = process_pre_proxy(pre_proxy_type, request);
2671 request->server = old_server;
2673 rcode = process_pre_proxy(pre_proxy_type, request);
2676 case RLM_MODULE_FAIL:
2677 case RLM_MODULE_INVALID:
2678 case RLM_MODULE_NOTFOUND:
2679 case RLM_MODULE_USERLOCK:
2681 /* FIXME: debug print failed stuff */
2684 case RLM_MODULE_REJECT:
2685 case RLM_MODULE_HANDLED:
2689 * Only proxy the packet if the pre-proxy code succeeded.
2691 case RLM_MODULE_NOOP:
2693 case RLM_MODULE_UPDATED:
2700 static int request_proxy(REQUEST *request, int retransmit)
2704 rad_assert(request->parent == NULL);
2705 rad_assert(request->home_server != NULL);
2707 if (request->master_state == REQUEST_STOP_PROCESSING) return 0;
2711 RWDEBUG("Cannot proxy and originate CoA packets at the same time. Cancelling CoA request");
2712 request_done(request->coa, FR_ACTION_DONE);
2717 * The request may need sending to a virtual server.
2718 * This code is more than a little screwed up. The rest
2719 * of the state machine doesn't handle parent / child
2720 * relationships well. i.e. if the child request takes
2721 * too long, the core will mark the *parent* as "stop
2722 * processing". And the child will continue without
2723 * knowing anything...
2725 * So, we have some horrible hacks to get around that.
2727 if (request->home_server->server) {
2730 if (request->packet->dst_port == 0) {
2731 WARN("Cannot proxy an internal request");
2735 DEBUG("Proxying to virtual server %s",
2736 request->home_server->server);
2739 * Packets to virtual serrers don't get
2740 * retransmissions sent to them. And the virtual
2741 * server is run ONLY if we have no child
2742 * threads, or we're running in a child thread.
2744 rad_assert(retransmit == 0);
2745 rad_assert(!spawn_flag || !we_are_master());
2747 fake = request_alloc_fake(request);
2749 fake->packet->vps = paircopy(fake->packet, request->packet->vps);
2750 talloc_free(request->proxy);
2752 fake->server = request->home_server->server;
2753 fake->handle = request->handle;
2754 fake->process = NULL; /* should never be run for anything */
2757 * Run the virtual server.
2759 request_running(fake, FR_ACTION_RUN);
2761 request->proxy = talloc_steal(request, fake->packet);
2762 fake->packet = NULL;
2763 request->proxy_reply = talloc_steal(request, fake->reply);
2769 * Just do the work here, rather than trying to
2770 * run the "decode proxy reply" stuff...
2772 process_proxy_reply(request, request->proxy_reply);
2774 request->handle(request); /* to do more post-proxy stuff */
2776 return -1; /* so we call request_finish */
2780 * We're actually sending a proxied packet. Do that now.
2782 if (!request->in_proxy_hash && !insert_into_proxy_hash(request)) {
2783 ERROR("Failed to insert request into the proxy list");
2787 rad_assert(request->proxy->id >= 0);
2790 struct timeval *response_window;
2792 response_window = request_response_window(request);
2795 if (request->home_server->tls) {
2796 RDEBUG2("Proxying request to home server %s port %d (TLS) timeout %d.%06d",
2797 inet_ntop(request->proxy->dst_ipaddr.af,
2798 &request->proxy->dst_ipaddr.ipaddr,
2799 buffer, sizeof(buffer)),
2800 request->proxy->dst_port,
2801 (int) response_window->tv_sec, (int) response_window->tv_usec);
2804 RDEBUG2("Proxying request to home server %s port %d timeout %d.%06d",
2805 inet_ntop(request->proxy->dst_ipaddr.af,
2806 &request->proxy->dst_ipaddr.ipaddr,
2807 buffer, sizeof(buffer)),
2808 request->proxy->dst_port,
2809 (int) response_window->tv_sec, (int) response_window->tv_usec);
2811 DEBUG_PACKET(request, request->proxy, 1);
2814 gettimeofday(&request->proxy_retransmit, NULL);
2816 request->proxy->timestamp = request->proxy_retransmit;
2817 request->home_server->last_packet_sent = request->proxy_retransmit.tv_sec;
2820 FR_STATS_TYPE_INC(request->home_server->stats.total_requests);
2822 request->child_state = REQUEST_PROXIED;
2823 request->proxy_listener->send(request->proxy_listener,
2829 * Proxy the packet as if it was new.
2831 static int request_proxy_anew(REQUEST *request)
2833 home_server_t *home;
2836 * Delete the request from the proxy list.
2838 * The packet list code takes care of ensuring that IDs
2839 * aren't reused until all 256 IDs have been used. So
2840 * there's a 1/256 chance of re-using the same ID when
2841 * we're sending to the same home server. Which is
2844 remove_from_proxy_hash(request);
2847 * Find a live home server for the request.
2849 home = home_server_ldb(NULL, request->home_pool, request);
2851 REDEBUG2("Failed to find live home server for request");
2853 if (setup_post_proxy_fail(request)) {
2854 request_queue_or_run(request, proxy_running);
2856 gettimeofday(&request->reply->timestamp, NULL);
2857 request_cleanup_delay_init(request, NULL);
2861 home_server_update_request(home, request);
2863 if (!insert_into_proxy_hash(request)) {
2864 RPROXY("Failed to insert retransmission into the proxy list");
2865 goto post_proxy_fail;
2869 * Free the old packet, to force re-encoding
2871 talloc_free(request->proxy->data);
2872 request->proxy->data = NULL;
2873 request->proxy->data_len = 0;
2875 #ifdef WITH_ACCOUNTING
2877 * Update the Acct-Delay-Time attribute.
2879 if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) {
2882 vp = pairfind(request->proxy->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY);
2883 if (!vp) vp = radius_paircreate(request->proxy,
2884 &request->proxy->vps,
2885 PW_ACCT_DELAY_TIME, 0);
2889 gettimeofday(&now, NULL);
2890 vp->vp_integer += now.tv_sec - request->proxy_retransmit.tv_sec;
2895 if (request_proxy(request, 1) != 1) goto post_proxy_fail;
2900 STATE_MACHINE_DECL(request_ping)
2902 home_server_t *home = request->home_server;
2905 TRACE_STATE_MACHINE;
2909 case FR_ACTION_TIMER:
2910 ERROR("No response to status check %d for home server %s port %d",
2912 inet_ntop(request->proxy->dst_ipaddr.af,
2913 &request->proxy->dst_ipaddr.ipaddr,
2914 buffer, sizeof(buffer)),
2915 request->proxy->dst_port);
2918 case FR_ACTION_PROXY_REPLY:
2919 rad_assert(request->in_proxy_hash);
2921 request->home_server->num_received_pings++;
2922 RPROXY("Received response to status check %d (%d in current sequence)",
2923 request->number, home->num_received_pings);
2926 * Remove the request from any hashes
2928 fr_event_delete(el, &request->ev);
2929 remove_from_proxy_hash(request);
2932 * The control socket may have marked the home server as
2933 * alive. OR, it may have suddenly started responding to
2934 * requests again. If so, don't re-do the "make alive"
2937 if (home->state == HOME_STATE_ALIVE) break;
2940 * It's dead, and we haven't received enough ping
2941 * responses to mark it "alive". Wait a bit.
2943 * If it's zombie, we mark it alive immediately.
2945 if ((home->state == HOME_STATE_IS_DEAD) &&
2946 (home->num_received_pings < home->num_pings_to_alive)) {
2951 * Mark it alive and delete any outstanding
2954 home->state = HOME_STATE_ALIVE;
2955 home->response_timeouts = 0;
2956 exec_trigger(request, home->cs, "home_server.alive", false);
2957 home->currently_outstanding = 0;
2958 home->num_sent_pings = 0;
2959 home->num_received_pings = 0;
2960 gettimeofday(&home->revive_time, NULL);
2962 fr_event_delete(el, &home->ev);
2964 RPROXY("Marking home server %s port %d alive",
2965 inet_ntop(request->proxy->dst_ipaddr.af,
2966 &request->proxy->dst_ipaddr.ipaddr,
2967 buffer, sizeof(buffer)),
2968 request->proxy->dst_port);
2972 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
2976 rad_assert(!request->in_request_hash);
2977 rad_assert(request->ev == NULL);
2978 request_done(request, FR_ACTION_DONE);
2982 * Called from start of zombie period, OR after control socket
2983 * marks the home server dead.
2985 static void ping_home_server(void *ctx)
2987 home_server_t *home = ctx;
2990 struct timeval when, now;
2992 if ((home->state == HOME_STATE_ALIVE) ||
2993 (home->ping_check == HOME_PING_CHECK_NONE) ||
2995 (home->proto == IPPROTO_TCP) ||
2997 (home->ev != NULL)) {
3001 gettimeofday(&now, NULL);
3003 if (home->state == HOME_STATE_ZOMBIE) {
3004 when = home->zombie_period_start;
3005 when.tv_sec += home->zombie_period;
3007 if (timercmp(&when, &now, <)) {
3008 DEBUG("PING: Zombie period is over for home server %s",
3010 mark_home_server_dead(home, &now);
3014 request = request_alloc(NULL);
3015 request->number = request_num_counter++;
3018 request->proxy = rad_alloc(request, 1);
3019 rad_assert(request->proxy != NULL);
3021 if (home->ping_check == HOME_PING_CHECK_STATUS_SERVER) {
3022 request->proxy->code = PW_CODE_STATUS_SERVER;
3024 pairmake(request->proxy, &request->proxy->vps,
3025 "Message-Authenticator", "0x00", T_OP_SET);
3027 } else if (home->type == HOME_TYPE_AUTH) {
3028 request->proxy->code = PW_CODE_AUTHENTICATION_REQUEST;
3030 pairmake(request->proxy, &request->proxy->vps,
3031 "User-Name", home->ping_user_name, T_OP_SET);
3032 pairmake(request->proxy, &request->proxy->vps,
3033 "User-Password", home->ping_user_password, T_OP_SET);
3034 pairmake(request->proxy, &request->proxy->vps,
3035 "Service-Type", "Authenticate-Only", T_OP_SET);
3036 pairmake(request->proxy, &request->proxy->vps,
3037 "Message-Authenticator", "0x00", T_OP_SET);
3040 #ifdef WITH_ACCOUNTING
3041 request->proxy->code = PW_CODE_ACCOUNTING_REQUEST;
3043 pairmake(request->proxy, &request->proxy->vps,
3044 "User-Name", home->ping_user_name, T_OP_SET);
3045 pairmake(request->proxy, &request->proxy->vps,
3046 "Acct-Status-Type", "Stop", T_OP_SET);
3047 pairmake(request->proxy, &request->proxy->vps,
3048 "Acct-Session-Id", "00000000", T_OP_SET);
3049 vp = pairmake(request->proxy, &request->proxy->vps,
3050 "Event-Timestamp", "0", T_OP_SET);
3051 vp->vp_date = now.tv_sec;
3053 rad_assert("Internal sanity check failed");
3057 vp = pairmake(request->proxy, &request->proxy->vps,
3058 "NAS-Identifier", "", T_OP_SET);
3060 pairsprintf(vp, "Status Check %u. Are you alive?",
3061 home->num_sent_pings);
3064 request->proxy->src_ipaddr = home->src_ipaddr;
3065 request->proxy->dst_ipaddr = home->ipaddr;
3066 request->proxy->dst_port = home->port;
3067 request->home_server = home;
3068 #ifdef DEBUG_STATE_MACHINE
3069 if (debug_flag) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n", request->number, __FUNCTION__,
3070 child_state_names[request->child_state],
3071 child_state_names[REQUEST_DONE]);
3072 if (debug_flag) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_ping");
3074 #ifdef HAVE_PTHREAD_H
3075 rad_assert(request->child_pid == NO_SUCH_CHILD_PID);
3077 request->child_state = REQUEST_DONE;
3078 request->process = request_ping;
3080 rad_assert(request->proxy_listener == NULL);
3082 if (!insert_into_proxy_hash(request)) {
3083 RPROXY("Failed to insert status check %d into proxy list. Discarding it.",
3086 rad_assert(!request->in_request_hash);
3087 rad_assert(!request->in_proxy_hash);
3088 rad_assert(request->ev == NULL);
3089 talloc_free(request);
3094 * Set up the timer callback.
3097 when.tv_sec += home->ping_timeout;
3099 DEBUG("PING: Waiting %u seconds for response to ping",
3100 home->ping_timeout);
3102 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3103 home->num_sent_pings++;
3105 rad_assert(request->proxy_listener != NULL);
3106 request->proxy_listener->send(request->proxy_listener,
3110 * Add +/- 2s of jitter, as suggested in RFC 3539
3111 * and in the Issues and Fixes draft.
3114 home->when.tv_sec += home->ping_interval;
3116 add_jitter(&home->when);
3118 DEBUG("PING: Next status packet in %u seconds", home->ping_interval);
3119 INSERT_EVENT(ping_home_server, home);
3122 static void home_trigger(home_server_t *home, char const *trigger)
3125 RADIUS_PACKET my_packet;
3127 memset(&my_request, 0, sizeof(my_request));
3128 memset(&my_packet, 0, sizeof(my_packet));
3129 my_request.proxy = &my_packet;
3130 my_packet.dst_ipaddr = home->ipaddr;
3131 my_packet.src_ipaddr = home->src_ipaddr;
3133 exec_trigger(&my_request, home->cs, trigger, false);
3136 static void mark_home_server_zombie(home_server_t *home, struct timeval *now, struct timeval *response_window)
3143 rad_assert((home->state == HOME_STATE_ALIVE) ||
3144 (home->state == HOME_STATE_UNKNOWN));
3147 if (home->proto == IPPROTO_TCP) {
3148 WARN("Not marking TCP server %s zombie", home->name);
3154 * We've received a real packet recently. Don't mark the
3155 * server as zombie until we've received NO packets for a
3156 * while. The "1/4" of zombie period was chosen rather
3157 * arbitrarily. It's a balance between too short, which
3158 * gives quick fail-over and fail-back, or too long,
3159 * where the proxy still sends packets to an unresponsive
3162 start = now->tv_sec - ((home->zombie_period + 3) / 4);
3163 if (home->last_packet_recv >= start) {
3164 DEBUG("Recieved reply from home server %d seconds ago. Might not be zombie.",
3165 (int) (now->tv_sec - home->last_packet_recv));
3169 home->state = HOME_STATE_ZOMBIE;
3170 home_trigger(home, "home_server.zombie");
3173 * Set the home server to "zombie", as of the time
3176 home->zombie_period_start.tv_sec = start;
3177 home->zombie_period_start.tv_usec = USEC / 2;
3179 fr_event_delete(el, &home->ev);
3180 home->num_sent_pings = 0;
3181 home->num_received_pings = 0;
3183 PROXY( "Marking home server %s port %d as zombie (it has not responded in %d.%06d seconds).",
3184 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
3185 buffer, sizeof(buffer)),
3186 home->port, (int) response_window->tv_sec, (int) response_window->tv_usec);
3188 ping_home_server(home);
3192 void revive_home_server(void *ctx)
3194 home_server_t *home = ctx;
3198 rad_assert(home->proto != IPPROTO_TCP);
3201 home->state = HOME_STATE_ALIVE;
3202 home->response_timeouts = 0;
3203 home_trigger(home, "home_server.alive");
3204 home->currently_outstanding = 0;
3205 gettimeofday(&home->revive_time, NULL);
3208 * Delete any outstanding events.
3210 if (home->ev) fr_event_delete(el, &home->ev);
3212 PROXY( "Marking home server %s port %d alive again... we have no idea if it really is alive or not.",
3213 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
3214 buffer, sizeof(buffer)),
3218 void mark_home_server_dead(home_server_t *home, struct timeval *when)
3220 int previous_state = home->state;
3224 if (home->proto == IPPROTO_TCP) {
3225 WARN("Not marking TCP server dead");
3230 PROXY( "Marking home server %s port %d as dead.",
3231 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
3232 buffer, sizeof(buffer)),
3235 home->state = HOME_STATE_IS_DEAD;
3236 home_trigger(home, "home_server.dead");
3238 if (home->ping_check != HOME_PING_CHECK_NONE) {
3240 * If the control socket marks us dead, start
3241 * pinging. Otherwise, we already started
3242 * pinging when it was marked "zombie".
3244 if (previous_state == HOME_STATE_ALIVE) {
3245 ping_home_server(home);
3247 DEBUG("PING: Already pinging home server %s",
3253 * Revive it after a fixed period of time. This
3254 * is very, very, bad.
3257 home->when.tv_sec += home->revive_interval;
3259 DEBUG("PING: Reviving home server %s in %u seconds",
3260 home->name, home->revive_interval);
3261 INSERT_EVENT(revive_home_server, home);
3265 STATE_MACHINE_DECL(proxy_wait_for_reply)
3267 struct timeval now, when;
3268 struct timeval *response_window = NULL;
3269 home_server_t *home = request->home_server;
3272 TRACE_STATE_MACHINE;
3274 rad_assert(request->packet->code != PW_CODE_STATUS_SERVER);
3275 rad_assert(request->home_server != NULL);
3277 if (request->master_state == REQUEST_STOP_PROCESSING) {
3278 request->child_state = REQUEST_DONE;
3282 gettimeofday(&now, NULL);
3287 * We have a reply, ignore the retransmit.
3289 if (request->proxy_reply) return;
3292 * The request was proxied to a virtual server.
3293 * Ignore the retransmit.
3295 if (request->home_server->server) return;
3297 if ((home->state == HOME_STATE_IS_DEAD) ||
3298 !request->proxy_listener ||
3299 (request->proxy_listener->status != RAD_LISTEN_STATUS_KNOWN)) {
3300 request_proxy_anew(request);
3305 if (home->proto == IPPROTO_TCP) {
3306 DEBUG2("Suppressing duplicate proxied request (tcp) to home server %s port %d proto TCP - ID: %d",
3307 inet_ntop(request->proxy->dst_ipaddr.af,
3308 &request->proxy->dst_ipaddr.ipaddr,
3309 buffer, sizeof(buffer)),
3310 request->proxy->dst_port,
3311 request->proxy->id);
3317 * More than one retransmit a second is stupid,
3318 * and should be suppressed by the proxy.
3320 when = request->proxy_retransmit;
3323 if (timercmp(&now, &when, <)) {
3324 DEBUG2("Suppressing duplicate proxied request (too fast) to home server %s port %d proto TCP - ID: %d",
3325 inet_ntop(request->proxy->dst_ipaddr.af,
3326 &request->proxy->dst_ipaddr.ipaddr,
3327 buffer, sizeof(buffer)),
3328 request->proxy->dst_port,
3329 request->proxy->id);
3333 #ifdef WITH_ACCOUNTING
3335 * If we update the Acct-Delay-Time, we need to
3338 if ((request->packet->code == PW_CODE_ACCOUNTING_REQUEST) &&
3339 pairfind(request->proxy->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY)) {
3340 request_proxy_anew(request);
3345 RDEBUG2("Sending duplicate proxied request to home server %s port %d - ID: %d",
3346 inet_ntop(request->proxy->dst_ipaddr.af,
3347 &request->proxy->dst_ipaddr.ipaddr,
3348 buffer, sizeof(buffer)),
3349 request->proxy->dst_port,
3350 request->proxy->id);
3351 request->num_proxied_requests++;
3353 rad_assert(request->proxy_listener != NULL);;
3354 DEBUG_PACKET(request, request->proxy, 1);
3355 FR_STATS_TYPE_INC(home->stats.total_requests);
3356 home->last_packet_sent = now.tv_sec;
3357 request->proxy_retransmit = now;
3358 request->proxy_listener->send(request->proxy_listener,
3362 case FR_ACTION_TIMER:
3363 response_window = request_response_window(request);
3366 if (!request->proxy_listener ||
3367 (request->proxy_listener->status != RAD_LISTEN_STATUS_KNOWN)) {
3368 remove_from_proxy_hash(request);
3370 when = request->packet->timestamp;
3371 when.tv_sec += request->root->max_request_time;
3373 if (timercmp(&when, &now, >)) {
3374 RDEBUG("Waiting for client retransmission in order to do a proxy retransmit");
3375 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3382 * Wake up "response_window" time in the future.
3383 * i.e. when MY packet hasn't received a response.
3385 * Note that we DO NOT mark the home server as
3386 * zombie if it doesn't respond to us. It may be
3387 * responding to other (better looking) packets.
3389 when = request->proxy->timestamp;
3390 timeradd(&when, response_window, &when);
3393 * Not at the response window. Set the timer for
3396 if (timercmp(&when, &now, >)) {
3397 struct timeval diff;
3398 timersub(&when, &now, &diff);
3400 RDEBUG("Expecting proxy response no later than %d.%06d seconds from now",
3401 (int) diff.tv_sec, (int) diff.tv_usec);
3402 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3407 RDEBUG("No proxy response, giving up on request and marking it done");
3410 * If we haven't received any packets for
3411 * "response_window", then mark the home server
3414 * If the connection is TCP, then another
3415 * "watchdog timer" function takes care of pings,
3416 * etc. So we don't need to do it here.
3418 * This check should really be part of a home
3419 * server state machine.
3421 if (((home->state == HOME_STATE_ALIVE) ||
3422 (home->state == HOME_STATE_UNKNOWN))
3424 && (home->proto != IPPROTO_TCP)
3427 home->response_timeouts++;
3428 if (home->response_timeouts > home->max_response_timeouts)
3429 mark_home_server_zombie(home, &now, response_window);
3432 FR_STATS_TYPE_INC(home->stats.total_timeouts);
3433 if (home->type == HOME_TYPE_AUTH) {
3434 if (request->proxy_listener) FR_STATS_TYPE_INC(request->proxy_listener->stats.total_timeouts);
3435 FR_STATS_TYPE_INC(proxy_auth_stats.total_timeouts);
3438 else if (home->type == HOME_TYPE_ACCT) {
3439 if (request->proxy_listener) FR_STATS_TYPE_INC(request->proxy_listener->stats.total_timeouts);
3440 FR_STATS_TYPE_INC(proxy_acct_stats.total_timeouts);
3445 * There was no response within the window. Stop
3446 * the request. If the client retransmitted, it
3447 * may have failed over to another home server.
3448 * But that one may be dead, too.
3450 RERROR("Failing proxied request, due to lack of any response from home server %s port %d",
3451 inet_ntop(request->proxy->dst_ipaddr.af,
3452 &request->proxy->dst_ipaddr.ipaddr,
3453 buffer, sizeof(buffer)),
3454 request->proxy->dst_port);
3456 if (setup_post_proxy_fail(request)) {
3457 request_queue_or_run(request, proxy_no_reply);
3459 gettimeofday(&request->reply->timestamp, NULL);
3460 request_cleanup_delay_init(request, NULL);
3465 * Duplicate proxy replies have been quenched by
3466 * now. This state is only called ONCE, when we
3467 * receive a new reply from the home server.
3469 case FR_ACTION_PROXY_REPLY:
3470 request_queue_or_run(request, proxy_running);
3473 case FR_ACTION_CONFLICTING:
3474 request_done(request, action);
3478 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
3482 #endif /* WITH_PROXY */
3484 /***********************************************************************
3488 ***********************************************************************/
3490 static int null_handler(UNUSED REQUEST *request)
3496 * See if we need to originate a CoA request.
3498 static void request_coa_originate(REQUEST *request)
3500 int rcode, pre_proxy_type = 0;
3506 rad_assert(request != NULL);
3507 rad_assert(request->coa != NULL);
3508 rad_assert(request->proxy == NULL);
3509 rad_assert(!request->in_proxy_hash);
3510 rad_assert(request->proxy_reply == NULL);
3513 * Check whether we want to originate one, or cancel one.
3515 vp = pairfind(request->config_items, PW_SEND_COA_REQUEST, 0, TAG_ANY);
3517 vp = pairfind(request->coa->proxy->vps, PW_SEND_COA_REQUEST, 0, TAG_ANY);
3521 if (vp->vp_integer == 0) {
3523 TALLOC_FREE(request->coa);
3531 * src_ipaddr will be set up in proxy_encode.
3533 memset(&ipaddr, 0, sizeof(ipaddr));
3534 vp = pairfind(coa->proxy->vps, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY);
3536 ipaddr.af = AF_INET;
3537 ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
3539 } else if ((vp = pairfind(coa->proxy->vps, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY)) != NULL) {
3540 ipaddr.af = AF_INET6;
3541 ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
3543 } else if ((vp = pairfind(coa->proxy->vps, PW_HOME_SERVER_POOL, 0, TAG_ANY)) != NULL) {
3544 coa->home_pool = home_pool_byname(vp->vp_strvalue,
3546 if (!coa->home_pool) {
3547 RWDEBUG2("No such home_server_pool %s",
3553 * Prefer the pool to one server
3555 } else if (request->client->coa_pool) {
3556 coa->home_pool = request->client->coa_pool;
3558 } else if (request->client->coa_server) {
3559 coa->home_server = request->client->coa_server;
3563 * If all else fails, send it to the client that
3564 * originated this request.
3566 memcpy(&ipaddr, &request->packet->src_ipaddr, sizeof(ipaddr));
3570 * Use the pool, if it exists.
3572 if (coa->home_pool) {
3573 coa->home_server = home_server_ldb(NULL, coa->home_pool, coa);
3574 if (!coa->home_server) {
3575 RWDEBUG("No live home server for home_server_pool %s", coa->home_pool->name);
3578 home_server_update_request(coa->home_server, coa);
3580 } else if (!coa->home_server) {
3581 uint16_t port = PW_COA_UDP_PORT;
3583 vp = pairfind(coa->proxy->vps, PW_PACKET_DST_PORT, 0, TAG_ANY);
3584 if (vp) port = vp->vp_integer;
3586 coa->home_server = home_server_find(&ipaddr, port, IPPROTO_UDP);
3587 if (!coa->home_server) {
3588 RWDEBUG2("Unknown destination %s:%d for CoA request.",
3589 inet_ntop(ipaddr.af, &ipaddr.ipaddr,
3590 buffer, sizeof(buffer)), port);
3595 vp = pairfind(coa->proxy->vps, PW_PACKET_TYPE, 0, TAG_ANY);
3597 switch (vp->vp_integer) {
3598 case PW_CODE_COA_REQUEST:
3599 case PW_CODE_DISCONNECT_REQUEST:
3600 coa->proxy->code = vp->vp_integer;
3604 DEBUG("Cannot set CoA Packet-Type to code %d",
3610 if (!coa->proxy->code) coa->proxy->code = PW_CODE_COA_REQUEST;
3613 * The rest of the server code assumes that
3614 * request->packet && request->reply exist. Copy them
3615 * from the original request.
3617 rad_assert(coa->packet != NULL);
3618 rad_assert(coa->packet->vps == NULL);
3620 coa->packet = rad_copy_packet(coa, request->packet);
3621 coa->reply = rad_copy_packet(coa, request->reply);
3623 coa->config_items = paircopy(coa, request->config_items);
3624 coa->num_coa_requests = 0;
3625 coa->handle = null_handler;
3626 coa->number = request->number; /* it's associated with the same request */
3629 * Call the pre-proxy routines.
3631 vp = pairfind(request->config_items, PW_PRE_PROXY_TYPE, 0, TAG_ANY);
3633 DICT_VALUE const *dval = dict_valbyattr(vp->da->attr, vp->da->vendor, vp->vp_integer);
3634 /* Must be a validation issue */
3636 RDEBUG2("Found Pre-Proxy-Type %s", dval->name);
3637 pre_proxy_type = vp->vp_integer;
3640 if (coa->home_pool && coa->home_pool->virtual_server) {
3641 char const *old_server = coa->server;
3643 coa->server = coa->home_pool->virtual_server;
3644 RDEBUG2("server %s {", coa->server);
3646 rcode = process_pre_proxy(pre_proxy_type, coa);
3649 coa->server = old_server;
3651 rcode = process_pre_proxy(pre_proxy_type, coa);
3658 * Only send the CoA packet if the pre-proxy code succeeded.
3660 case RLM_MODULE_NOOP:
3662 case RLM_MODULE_UPDATED:
3667 * Source IP / port is set when the proxy socket
3670 coa->proxy->dst_ipaddr = coa->home_server->ipaddr;
3671 coa->proxy->dst_port = coa->home_server->port;
3673 if (!insert_into_proxy_hash(coa)) {
3674 radlog_request(L_PROXY, 0, coa, "Failed to insert CoA request into proxy list");
3679 * We CANNOT divorce the CoA request from the parent
3680 * request. This function is running in a child thread,
3681 * and we need access to the main event loop in order to
3682 * to add the timers for the CoA packet.
3684 * Instead, we wait for the timer on the parent request
3687 gettimeofday(&coa->proxy->timestamp, NULL);
3688 coa->packet->timestamp = coa->proxy->timestamp; /* for max_request_time */
3689 coa->delay = 0; /* need to calculate a new delay */
3691 DEBUG_PACKET(coa, coa->proxy, 1);
3693 coa->process = coa_wait_for_reply;
3694 #ifdef DEBUG_STATE_MACHINE
3695 if (debug_flag) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n", request->number, __FUNCTION__,
3696 child_state_names[request->child_state],
3697 child_state_names[REQUEST_RUNNING]);
3699 #ifdef HAVE_PTHREAD_H
3700 coa->child_pid = NO_SUCH_CHILD_PID;
3702 coa->child_state = REQUEST_PROXIED;
3703 rad_assert(coa->proxy_reply == NULL);
3704 FR_STATS_TYPE_INC(coa->home_server->stats.total_requests);
3705 coa->home_server->last_packet_sent = coa->proxy->timestamp.tv_sec;
3706 coa->proxy_listener->send(coa->proxy_listener, coa);
3710 static void coa_timer(REQUEST *request)
3712 uint32_t delay, frac;
3713 struct timeval now, when, mrd;
3715 rad_assert(request->parent == NULL);
3717 if (request->proxy_reply) return request_process_timer(request);
3719 gettimeofday(&now, NULL);
3721 if (request->delay == 0) {
3723 * Implement re-transmit algorithm as per RFC 5080
3726 * We want IRT + RAND*IRT
3727 * or 0.9 IRT + rand(0,.2) IRT
3729 * 2^20 ~ USEC, and we want 2.
3730 * rand(0,0.2) USEC ~ (rand(0,2^21) / 10)
3732 delay = (fr_rand() & ((1 << 22) - 1)) / 10;
3733 request->delay = delay * request->home_server->coa_irt;
3734 delay = request->home_server->coa_irt * USEC;
3735 delay -= delay / 10;
3736 delay += request->delay;
3737 request->delay = delay;
3739 when = request->proxy->timestamp;
3740 tv_add(&when, delay);
3742 if (timercmp(&when, &now, >)) {
3743 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3749 * Retransmit CoA request.
3753 * Cap count at MRC, if it is non-zero.
3755 if (request->home_server->coa_mrc &&
3756 (request->num_coa_requests >= request->home_server->coa_mrc)) {
3759 RERROR("Failing request - originate-coa ID %u, due to lack of any response from coa server %s port %d",
3761 inet_ntop(request->proxy->dst_ipaddr.af,
3762 &request->proxy->dst_ipaddr.ipaddr,
3763 buffer, sizeof(buffer)),
3764 request->proxy->dst_port);
3766 if (setup_post_proxy_fail(request)) {
3767 request_queue_or_run(request, coa_no_reply);
3769 request_done(request, FR_ACTION_DONE);
3775 * RFC 5080 Section 2.2.1
3777 * RT = 2*RTprev + RAND*RTprev
3778 * = 1.9 * RTprev + rand(0,.2) * RTprev
3779 * = 1.9 * RTprev + rand(0,1) * (RTprev / 5)
3782 delay ^= (delay >> 16);
3784 frac = request->delay / 5;
3785 delay = ((frac >> 16) * delay) + (((frac & 0xffff) * delay) >> 16);
3787 delay += (2 * request->delay) - (request->delay / 10);
3790 * Cap delay at MRT, if MRT is non-zero.
3792 if (request->home_server->coa_mrt &&
3793 (delay > (request->home_server->coa_mrt * USEC))) {
3794 int mrt_usec = request->home_server->coa_mrt * USEC;
3797 * delay = MRT + RAND * MRT
3798 * = 0.9 MRT + rand(0,.2) * MRT
3801 delay ^= (delay >> 15);
3803 delay = ((mrt_usec >> 16) * delay) + (((mrt_usec & 0xffff) * delay) >> 16);
3804 delay += mrt_usec - (mrt_usec / 10);
3807 request->delay = delay;
3809 tv_add(&when, request->delay);
3810 mrd = request->proxy->timestamp;
3811 mrd.tv_sec += request->home_server->coa_mrd;
3814 * Cap duration at MRD.
3816 if (timercmp(&mrd, &when, <)) {
3819 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3821 request->num_coa_requests++; /* is NOT reset by code 3 lines above! */
3823 FR_STATS_TYPE_INC(request->home_server->stats.total_requests);
3826 * Status servers don't count as real packets sent.
3828 request->proxy_listener->send(request->proxy_listener,
3832 STATE_MACHINE_DECL(coa_wait_for_reply)
3834 rad_assert(request->parent == NULL);
3836 TRACE_STATE_MACHINE;
3839 case FR_ACTION_TIMER:
3841 * This is big enough to be in it's own function.
3846 case FR_ACTION_PROXY_REPLY:
3847 rad_assert(request->parent == NULL);
3848 request_queue_or_run(request, coa_running);
3852 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
3857 static void request_coa_separate(REQUEST *request)
3859 #ifdef DEBUG_STATE_MACHINE
3860 int action = FR_ACTION_TIMER;
3862 TRACE_STATE_MACHINE;
3864 rad_assert(request->parent != NULL);
3865 rad_assert(request->parent->coa == request);
3866 rad_assert(request->ev == NULL);
3867 rad_assert(!request->in_request_hash);
3868 rad_assert(request->coa == NULL);
3870 rad_assert(request->proxy_listener != NULL);
3872 (void) talloc_steal(NULL, request);
3873 request->parent->coa = NULL;
3874 request->parent = NULL;
3877 * Should be coa_wait_for_reply()
3879 request->process(request, FR_ACTION_TIMER);
3882 STATE_MACHINE_DECL(coa_no_reply)
3886 TRACE_STATE_MACHINE;
3889 case FR_ACTION_TIMER:
3890 request_common(request, action);
3893 case FR_ACTION_PROXY_REPLY: /* too late! */
3894 RDEBUG2("Reply from CoA server %s port %d - ID: %d arrived too late.",
3895 inet_ntop(request->proxy->src_ipaddr.af,
3896 &request->proxy->src_ipaddr.ipaddr,
3897 buffer, sizeof(buffer)),
3898 request->proxy->dst_port, request->proxy->id);
3903 * FIXME: do recv_coa Fail
3905 (void) process_proxy_reply(request, NULL);
3906 request_done(request, FR_ACTION_DONE);
3910 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
3915 STATE_MACHINE_DECL(coa_running)
3917 TRACE_STATE_MACHINE;
3920 case FR_ACTION_TIMER:
3921 request_process_timer(request);
3924 case FR_ACTION_PROXY_REPLY:
3925 request_common(request, action);
3929 if (process_proxy_reply(request, request->proxy_reply)) {
3930 request->handle(request);
3931 request_finish(request, action);
3933 request_done(request, FR_ACTION_DONE);
3938 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
3942 #endif /* WITH_COA */
3944 /***********************************************************************
3946 * End of the State machine. Start of additional helper code.
3948 ***********************************************************************/
3950 /***********************************************************************
3954 ***********************************************************************/
3955 static void event_socket_handler(UNUSED fr_event_list_t *xel, UNUSED int fd, void *ctx)
3957 rad_listen_t *listener = ctx;
3959 rad_assert(xel == el);
3963 (listener->type != RAD_LISTEN_DETAIL) &&
3965 (listener->fd < 0)) {
3968 listener->print(listener, buffer, sizeof(buffer));
3969 ERROR("FATAL: Asked to read from closed socket: %s",
3972 rad_panic("Socket was closed on us!");
3976 listener->recv(listener);
3980 #ifdef WITH_DETAIL_THREAD
3983 * This function is called periodically to see if this detail
3984 * file is available for reading.
3986 static void event_poll_detail(void *ctx)
3989 rad_listen_t *this = ctx;
3990 struct timeval when, now;
3991 listen_detail_t *detail = this->data;
3993 rad_assert(this->type == RAD_LISTEN_DETAIL);
3996 event_socket_handler(el, this->fd, this);
3998 fr_event_now(el, &now);
4002 * Backdoor API to get the delay until the next poll
4005 delay = this->encode(this, NULL);
4006 if (delay == 0) goto redo;
4008 tv_add(&when, delay);
4010 if (!fr_event_insert(el, event_poll_detail, this,
4011 &when, &detail->ev)) {
4012 ERROR("Failed creating handler");
4016 #endif /* WITH_DETAIL_THREAD */
4017 #endif /* WITH_DETAIL */
4019 static void event_status(struct timeval *wake)
4021 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
4025 if (debug_flag == 0) {
4027 INFO("Ready to process requests");
4028 just_started = false;
4034 INFO("Ready to process requests");
4036 } else if ((wake->tv_sec != 0) ||
4037 (wake->tv_usec >= 100000)) {
4038 DEBUG("Waking up in %d.%01u seconds.",
4039 (int) wake->tv_sec, (unsigned int) wake->tv_usec / 100000);
4044 * FIXME: Put this somewhere else, where it isn't called
4045 * all of the time...
4048 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
4050 * If there are no child threads, then there may
4051 * be child processes. In that case, wait for
4052 * their exit status, and throw that exit status
4053 * away. This helps get rid of zxombie children.
4055 while (waitpid(-1, &argval, WNOHANG) > 0) {
4063 static void listener_free_cb(void *ctx)
4065 rad_listen_t *this = ctx;
4068 if (this->count > 0) {
4069 struct timeval when;
4070 listen_socket_t *sock = this->data;
4072 fr_event_now(el, &when);
4075 if (!fr_event_insert(el, listener_free_cb, this, &when,
4077 rad_panic("Failed to insert event");
4084 * It's all free, close the socket.
4087 this->print(this, buffer, sizeof(buffer));
4088 DEBUG("... cleaning up socket %s", buffer);
4094 static int proxy_eol_cb(void *ctx, void *data)
4096 struct timeval when;
4097 REQUEST *request = fr_packet2myptr(REQUEST, proxy, data);
4099 if (request->proxy_listener != ctx) return 0;
4102 * We don't care if it's being processed in a child thread.
4105 #ifdef WITH_ACCOUNTING
4107 * Accounting packets should be deleted immediately.
4108 * They will never be retransmitted by the client.
4110 if (request->proxy->code == PW_CODE_ACCOUNTING_REQUEST) {
4111 RDEBUG("Stopping request due to failed connection to home server");
4112 request->master_state = REQUEST_STOP_PROCESSING;
4117 * Reset the timer to be now, so that the request is
4118 * quickly updated. But spread the requests randomly
4119 * over the next second, so that we don't overload the
4122 fr_event_now(el, &when);
4123 tv_add(&when, fr_rand() % USEC);
4124 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
4127 * Don't delete it from the list.
4133 static int event_new_fd(rad_listen_t *this)
4139 if (this->status == RAD_LISTEN_STATUS_KNOWN) return 1;
4141 this->print(this, buffer, sizeof(buffer));
4143 if (this->status == RAD_LISTEN_STATUS_INIT) {
4144 listen_socket_t *sock = this->data;
4147 DEBUG("Listening on %s", buffer);
4149 INFO(" ... adding new socket %s", buffer);
4152 switch (this->type) {
4155 * Detail files are always known, and aren't
4156 * put into the socket event loop.
4158 case RAD_LISTEN_DETAIL:
4159 this->status = RAD_LISTEN_STATUS_KNOWN;
4161 #ifndef WITH_DETAIL_THREAD
4163 * Set up the first poll interval.
4165 event_poll_detail(this);
4168 break; /* add the FD to the list */
4170 #endif /* WITH_DETAIL */
4174 * Add it to the list of sockets we can use.
4175 * Server sockets (i.e. auth/acct) are never
4176 * added to the packet list.
4178 case RAD_LISTEN_PROXY:
4181 * Add timers to outgoing child sockets, if necessary.
4183 if (sock->proto == IPPROTO_TCP && sock->opened &&
4184 (sock->home->limit.lifetime || sock->home->limit.idle_timeout)) {
4185 struct timeval when;
4187 when.tv_sec = sock->opened + 1;
4190 if (!fr_event_insert(el, tcp_socket_timer, this, &when,
4192 rad_panic("Failed to insert event");
4197 #endif /* WITH_PROXY */
4200 * FIXME: put idle timers on command sockets.
4206 * Add timers to incoming child sockets, if necessary.
4208 if (sock->proto == IPPROTO_TCP && sock->opened &&
4209 (sock->limit.lifetime || sock->limit.idle_timeout)) {
4210 struct timeval when;
4212 when.tv_sec = sock->opened + 1;
4215 if (!fr_event_insert(el, tcp_socket_timer, this, &when,
4217 rad_panic("Failed to insert event");
4222 } /* switch over listener types */
4225 * All sockets: add the FD to the event handler.
4227 if (!fr_event_fd_insert(el, 0, this->fd,
4228 event_socket_handler, this)) {
4229 ERROR("Failed adding event handler for socket!");
4233 this->status = RAD_LISTEN_STATUS_KNOWN;
4239 * Stop using this socket, if at all possible.
4241 if (this->status == RAD_LISTEN_STATUS_EOL) {
4243 * Remove it from the list of live FD's.
4245 fr_event_fd_delete(el, 0, this->fd);
4249 * Proxy sockets get frozen, so that we don't use
4250 * them for new requests. But we do keep them
4251 * open to listen for replies to requests we had
4254 if (this->type == RAD_LISTEN_PROXY) {
4255 PTHREAD_MUTEX_LOCK(&proxy_mutex);
4256 if (!fr_packet_list_socket_freeze(proxy_list,
4258 ERROR("Fatal error freezing socket: %s", fr_strerror());
4262 fr_packet_list_walk(proxy_list, this, proxy_eol_cb);
4263 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
4268 * Requests are still using the socket. Wait for
4271 if (this->count > 0) {
4272 struct timeval when;
4273 listen_socket_t *sock = this->data;
4276 * Try again to clean up the socket in 30
4279 gettimeofday(&when, NULL);
4282 if (!fr_event_insert(el,
4283 (fr_event_callback_t) event_new_fd,
4284 this, &when, &sock->ev)) {
4285 rad_panic("Failed to insert event");
4292 * No one is using the socket. We can remove it now.
4294 this->status = RAD_LISTEN_STATUS_REMOVE_NOW;
4295 } /* socket is at EOL */
4301 if (this->status == RAD_LISTEN_STATUS_REMOVE_NOW) {
4304 listen_socket_t *sock = this->data;
4306 struct timeval when;
4309 * Re-open the socket, pointing it to /dev/null.
4310 * This means that all writes proceed without
4311 * blocking, and all reads return "no data".
4313 * This leaves the socket active, so any child
4314 * threads won't go insane. But it means that
4315 * they cannot send or receive any packets.
4317 * This is EXTRA work in the normal case, when
4318 * sockets are closed without error. But it lets
4319 * us have one simple processing method for all
4322 devnull = open("/dev/null", O_RDWR);
4324 ERROR("FATAL failure opening /dev/null: %s",
4325 fr_syserror(errno));
4328 if (dup2(devnull, this->fd) < 0) {
4329 ERROR("FATAL failure closing socket: %s",
4330 fr_syserror(errno));
4336 rad_assert(this->type != RAD_LISTEN_DETAIL);
4340 INFO(" ... shutting down socket %s", buffer);
4344 * The socket is dead. Force all proxied packets
4345 * to stop using it. And then remove it from the
4346 * list of outgoing sockets.
4348 if (this->type == RAD_LISTEN_PROXY) {
4349 PTHREAD_MUTEX_LOCK(&proxy_mutex);
4350 fr_packet_list_walk(proxy_list, this, eol_proxy_listener);
4352 if (!fr_packet_list_socket_del(proxy_list, this->fd)) {
4353 ERROR("Fatal error removing socket %s: %s",
4354 buffer, fr_strerror());
4357 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
4362 * EOL all requests using this socket.
4364 fr_packet_list_walk(pl, this, eol_listener);
4368 * No child threads, clean it up now.
4371 if (sock->ev) fr_event_delete(el, &sock->ev);
4377 * Wait until all requests using this socket are done.
4379 gettimeofday(&when, NULL);
4382 if (!fr_event_insert(el, listener_free_cb, this, &when,
4384 rad_panic("Failed to insert event");
4387 #endif /* WITH_TCP */
4392 /***********************************************************************
4396 ***********************************************************************/
4398 static void handle_signal_self(int flag)
4402 if ((flag & (RADIUS_SIGNAL_SELF_EXIT | RADIUS_SIGNAL_SELF_TERM)) != 0) {
4403 if ((flag & RADIUS_SIGNAL_SELF_EXIT) != 0) {
4404 INFO("Signalled to exit");
4405 fr_event_loop_exit(el, 1);
4407 INFO("Signalled to terminate");
4408 exec_trigger(NULL, NULL, "server.signal.term", true);
4409 fr_event_loop_exit(el, 2);
4413 } /* else exit/term flags weren't set */
4416 * Tell the even loop to stop processing.
4418 if ((flag & RADIUS_SIGNAL_SELF_HUP) != 0) {
4420 static time_t last_hup = 0;
4423 if ((int) (when - last_hup) < 5) {
4424 INFO("Ignoring HUP (less than 5s since last one)");
4428 INFO("Received HUP signal");
4432 exec_trigger(NULL, NULL, "server.signal.hup", true);
4433 fr_event_loop_exit(el, 0x80);
4437 #ifndef WITH_DETAIL_THREAD
4438 if ((flag & RADIUS_SIGNAL_SELF_DETAIL) != 0) {
4442 * FIXME: O(N) loops suck.
4444 for (this = main_config.listen;
4446 this = this->next) {
4447 if (this->type != RAD_LISTEN_DETAIL) continue;
4450 * This one didn't send the signal, skip
4453 if (!this->decode(this, NULL)) continue;
4456 * Go service the interrupt.
4458 event_poll_detail(this);
4466 #ifdef HAVE_PTHREAD_H
4468 * There are new listeners in the list. Run
4469 * event_new_fd() on them.
4471 if ((flag & RADIUS_SIGNAL_SELF_NEW_FD) != 0) {
4472 rad_listen_t *this, *next;
4474 FD_MUTEX_LOCK(&fd_mutex);
4477 * FIXME: unlock the mutex before calling
4480 for (this = new_listeners; this != NULL; this = next) {
4487 new_listeners = NULL;
4488 FD_MUTEX_UNLOCK(&fd_mutex);
4490 #endif /* HAVE_PTHREAD_H */
4491 #endif /* WITH_PROXY */
4492 #endif /* WITH_TCP */
4495 #ifndef HAVE_PTHREAD_H
4496 void radius_signal_self(int flag)
4498 return handle_signal_self(flag);
4502 static int self_pipe[2] = { -1, -1 };
4505 * Inform ourselves that we received a signal.
4507 void radius_signal_self(int flag)
4513 * The read MUST be non-blocking for this to work.
4515 rcode = read(self_pipe[0], buffer, sizeof(buffer));
4519 for (i = 0; i < rcode; i++) {
4520 buffer[0] |= buffer[i];
4528 if (write(self_pipe[1], buffer, 1) < 0) fr_exit(0);
4532 static void event_signal_handler(UNUSED fr_event_list_t *xel,
4533 UNUSED int fd, UNUSED void *ctx)
4538 rcode = read(self_pipe[0], buffer, sizeof(buffer));
4539 if (rcode <= 0) return;
4542 * Merge pending signals.
4544 for (i = 0; i < rcode; i++) {
4545 buffer[0] |= buffer[i];
4548 handle_signal_self(buffer[0]);
4550 #endif /* HAVE_PTHREAD_H */
4552 /***********************************************************************
4554 * Bootstrapping code.
4556 ***********************************************************************/
4559 * Externally-visibly functions.
4561 int radius_event_init(TALLOC_CTX *ctx) {
4562 el = fr_event_list_create(ctx, event_status);
4568 int radius_event_start(CONF_SECTION *cs, bool have_children)
4570 rad_listen_t *head = NULL;
4572 if (fr_start_time != (time_t)-1) return 0;
4574 time(&fr_start_time);
4576 if (!check_config) {
4578 * radius_event_init() must be called first
4582 pl = fr_packet_list_create(0);
4583 if (!pl) return 0; /* leak el */
4586 request_num_counter = 0;
4589 if (main_config.proxy_requests) {
4591 * Create the tree for managing proxied requests and
4594 proxy_list = fr_packet_list_create(1);
4595 if (!proxy_list) return 0;
4597 #ifdef HAVE_PTHREAD_H
4598 if (pthread_mutex_init(&proxy_mutex, NULL) != 0) {
4599 ERROR("FATAL: Failed to initialize proxy mutex: %s",
4600 fr_syserror(errno));
4606 * The "init_delay" is set to "response_window".
4607 * Reset it to half of "response_window" in order
4608 * to give the event loop enough time to service
4609 * the event before hitting "response_window".
4611 main_config.init_delay.tv_usec += (main_config.init_delay.tv_sec & 0x01) * USEC;
4612 main_config.init_delay.tv_usec >>= 1;
4613 main_config.init_delay.tv_sec >>= 1;
4619 * Move all of the thread calls to this file?
4621 * It may be best for the mutexes to be in this file...
4623 spawn_flag = have_children;
4625 #ifdef HAVE_PTHREAD_H
4626 NO_SUCH_CHILD_PID = pthread_self(); /* not a child thread */
4629 * Initialize the threads ONLY if we're spawning, AND
4630 * we're running normally.
4632 if (have_children && !check_config &&
4633 (thread_pool_init(cs, &spawn_flag) < 0)) {
4639 DEBUG("%s: #### Skipping IP addresses and Ports ####",
4641 if (listen_init(cs, &head, spawn_flag) < 0) {
4648 #ifdef HAVE_PTHREAD_H
4650 * Child threads need a pipe to signal us, as do the
4653 if (pipe(self_pipe) < 0) {
4654 ERROR("radiusd: Error opening internal pipe: %s",
4655 fr_syserror(errno));
4658 if ((fcntl(self_pipe[0], F_SETFL, O_NONBLOCK) < 0) ||
4659 (fcntl(self_pipe[0], F_SETFD, FD_CLOEXEC) < 0)) {
4660 ERROR("radiusd: Error setting internal flags: %s",
4661 fr_syserror(errno));
4664 if ((fcntl(self_pipe[1], F_SETFL, O_NONBLOCK) < 0) ||
4665 (fcntl(self_pipe[1], F_SETFD, FD_CLOEXEC) < 0)) {
4666 ERROR("radiusd: Error setting internal flags: %s",
4667 fr_syserror(errno));
4671 if (!fr_event_fd_insert(el, 0, self_pipe[0],
4672 event_signal_handler, el)) {
4673 ERROR("Failed creating handler for signals");
4678 DEBUG("%s: #### Opening IP addresses and Ports ####",
4682 * The server temporarily switches to an unprivileged
4683 * user very early in the bootstrapping process.
4684 * However, some sockets MAY require privileged access
4685 * (bind to device, or to port < 1024, or to raw
4686 * sockets). Those sockets need to call suid up/down
4687 * themselves around the functions that need a privileged
4690 if (listen_init(cs, &head, spawn_flag) < 0) {
4694 main_config.listen = head;
4697 * At this point, no one has any business *ever* going
4700 fr_suid_down_permanent();
4707 static int proxy_delete_cb(UNUSED void *ctx, void *data)
4709 REQUEST *request = fr_packet2myptr(REQUEST, proxy, data);
4711 request->master_state = REQUEST_STOP_PROCESSING;
4713 #ifdef HAVE_PTHREAD_H
4714 if (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0) return 0;
4718 * If it's queued we can't delete it from the queue.
4720 * Otherwise, it's OK to delete it. Even RUNNING, because
4721 * that will get caught by the check above.
4723 if (request->child_state == REQUEST_QUEUED) return 0;
4725 request->in_proxy_hash = false;
4727 if (!request->in_request_hash) {
4728 request_done(request, FR_ACTION_DONE);
4732 * Delete it from the list.
4739 static int request_delete_cb(UNUSED void *ctx, void *data)
4741 REQUEST *request = fr_packet2myptr(REQUEST, packet, data);
4743 request->master_state = REQUEST_STOP_PROCESSING;
4746 * Not done, or the child thread is still processing it.
4748 if (request->child_state < REQUEST_RESPONSE_DELAY) return 0; /* continue */
4750 #ifdef HAVE_PTHREAD_H
4751 if (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0) return 0;
4755 rad_assert(request->in_proxy_hash == false);
4758 request->in_request_hash = false;
4759 if (request->ev) fr_event_delete(el, &request->ev);
4761 if (main_config.memory_report) {
4762 RDEBUG2("Cleaning up request packet ID %u with timestamp +%d",
4763 request->packet->id,
4764 (unsigned int) (request->timestamp - fr_start_time));
4769 rad_assert(!request->coa->in_proxy_hash);
4773 talloc_free(request);
4776 * Delete it from the list, and continue;
4782 void radius_event_free(void)
4788 * There are requests in the proxy hash that aren't
4789 * referenced from anywhere else. Remove them first.
4792 fr_packet_list_walk(proxy_list, NULL, proxy_delete_cb);
4796 fr_packet_list_walk(pl, NULL, request_delete_cb);
4800 * Now that all requests have been marked "please stop",
4801 * ensure that all of the threads have exited.
4803 #ifdef HAVE_PTHREAD_H
4808 * Walk the lists again, ensuring that all
4809 * requests are done.
4811 if (main_config.memory_report) {
4816 fr_packet_list_walk(proxy_list, NULL, proxy_delete_cb);
4817 num = fr_packet_list_num_elements(proxy_list);
4819 ERROR("Proxy list has %d requests still in it.", num);
4824 fr_packet_list_walk(pl, NULL, request_delete_cb);
4825 num = fr_packet_list_num_elements(pl);
4827 ERROR("Request list has %d requests still in it.", num);
4832 fr_packet_list_free(pl);
4836 fr_packet_list_free(proxy_list);
4842 if (debug_condition) talloc_free(debug_condition);
4845 int radius_event_process(void)
4849 return fr_event_loop(el);