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) printf("(%u) ********\tSTATE %s action %s live M-%s C-%s\t********\n", request->number, __FUNCTION__, action_codes[action], master_state_names[request->master_state], child_state_names[request->child_state])
76 static char const *master_state_names[REQUEST_MASTER_NUM_STATES] = {
83 static char const *child_state_names[REQUEST_CHILD_NUM_STATES] = {
94 #define TRACE_STATE_MACHINE {}
98 * Declare a state in the state machine.
101 #define STATE_MACHINE_DECL(_x) static void CC_HINT(nonnull) _x(REQUEST *request, int action)
103 #define STATE_MACHINE_TIMER(_x) request->timer_action = _x; \
104 fr_event_insert(el, request_timer, request, \
105 &when, &request->ev);
110 * @section request_timeline
112 * Time sequence of a request
115 * RQ-----------------P=============================Y-J-C
116 * ::::::::::::::::::::::::::::::::::::::::::::::::::::::::M
119 * - R: received. Duplicate detection is done, and request is
122 * - Q: Request is placed onto a queue for child threads to pick up.
123 * If there are no child threads, the request goes immediately
126 * - P: Processing the request through the modules.
128 * - Y: Reply is ready. Rejects MAY be delayed here. All other
129 * replies are sent immediately.
131 * - J: Reject is sent "response_delay" after the reply is ready.
133 * - C: For Access-Requests, After "cleanup_delay", the request is
134 * deleted. Accounting-Request packets go directly from Y to C.
136 * - M: Max request time. If the request hits this timer, it is
139 * Other considerations include duplicate and conflicting
140 * packets. When a dupicate packet is received, it is ignored
141 * until we've reached Y, as no response is ready. If the reply
142 * is a reject, duplicates are ignored until J, when we're ready
143 * to send the reply. In between the reply being sent (Y or J),
144 * and C, the server responds to duplicates by sending the cached
147 * Conflicting packets are sent in 2 situations.
149 * The first is in between R and Y. In that case, we consider
150 * it as a hint that we're taking too long, and the NAS has given
151 * up on the request. We then behave just as if the M timer was
152 * reached, and we discard the current request. This allows us
153 * to process the new one.
155 * The second case is when we're at Y, but we haven't yet
156 * finished processing the request. This is a race condition in
157 * the threading code (avoiding locks is faster). It means that
158 * a thread has actually encoded and sent the reply, and that the
159 * NAS has responded with a new packet. The server can then
160 * safely mark the current request as "OK to delete", and behaves
161 * just as if the M timer was reached. This usually happens only
162 * in high-load situations.
164 * Duplicate packets are sent when the NAS thinks we're taking
165 * too long, and wants a reply. From R-Y, duplicates are
166 * ignored. From Y-J (for Access-Rejects), duplicates are also
167 * ignored. From Y-C, duplicates get a duplicate reply. *And*,
168 * they cause the "cleanup_delay" time to be extended. This
169 * extension means that we're more likely to send a duplicate
170 * reply (if we have one), or to suppress processing the packet
171 * twice if we didn't reply to it.
173 * All functions in this file should be thread-safe, and should
174 * assume thet the REQUEST structure is being accessed
175 * simultaneously by the main thread, and by the child worker
176 * threads. This means that timers, etc. cannot be updated in
179 * Instead, the master thread periodically calls request->process
180 * with action TIMER. It's up to the individual functions to
181 * determine how to handle that. They need to check if they're
182 * being called from a child thread or the master, and then do
183 * different things based on that.
188 static fr_packet_list_t *proxy_list = NULL;
191 #ifdef HAVE_PTHREAD_H
193 static pthread_mutex_t proxy_mutex;
194 static bool proxy_no_new_sockets = false;
197 #define PTHREAD_MUTEX_LOCK if (spawn_flag) pthread_mutex_lock
198 #define PTHREAD_MUTEX_UNLOCK if (spawn_flag) pthread_mutex_unlock
200 static pthread_t NO_SUCH_CHILD_PID;
201 #define NO_CHILD_THREAD request->child_pid = NO_SUCH_CHILD_PID
205 * This is easier than ifdef's throughout the code.
207 #define PTHREAD_MUTEX_LOCK(_x)
208 #define PTHREAD_MUTEX_UNLOCK(_x)
209 #define NO_CHILD_THREAD
212 #if defined(HAVE_PTHREAD_H) && !defined (NDEBUG)
213 static bool we_are_master(void)
216 (pthread_equal(pthread_self(), NO_SUCH_CHILD_PID) == 0)) {
222 #define ASSERT_MASTER if (!we_are_master()) rad_panic("We are not master")
225 #define we_are_master(_x) (1)
226 #define ASSERT_MASTER
229 static int event_new_fd(rad_listen_t *this);
232 * We need mutexes around the event FD list *only* in certain
235 #if defined (HAVE_PTHREAD_H) && (defined(WITH_PROXY) || defined(WITH_TCP))
236 static rad_listen_t *new_listeners = NULL;
238 static pthread_mutex_t fd_mutex;
239 #define FD_MUTEX_LOCK if (spawn_flag) pthread_mutex_lock
240 #define FD_MUTEX_UNLOCK if (spawn_flag) pthread_mutex_unlock
242 void radius_update_listener(rad_listen_t *this)
245 * Just do it ourselves.
247 if (we_are_master()) {
252 FD_MUTEX_LOCK(&fd_mutex);
255 * If it's already in the list, don't add it again.
258 FD_MUTEX_UNLOCK(&fd_mutex);
263 * Otherwise, add it to the list
265 this->next = new_listeners;
266 new_listeners = this;
267 FD_MUTEX_UNLOCK(&fd_mutex);
268 radius_signal_self(RADIUS_SIGNAL_SELF_NEW_FD);
271 void radius_update_listener(rad_listen_t *this)
274 * No threads. Just insert it.
279 * This is easier than ifdef's throughout the code.
281 #define FD_MUTEX_LOCK(_x)
282 #define FD_MUTEX_UNLOCK(_x)
285 static int request_num_counter = 1;
287 static int request_will_proxy(REQUEST *request);
288 static int request_proxy(REQUEST *request, int retransmit);
289 STATE_MACHINE_DECL(proxy_wait_for_reply);
290 STATE_MACHINE_DECL(proxy_no_reply);
291 STATE_MACHINE_DECL(proxy_running);
292 static int process_proxy_reply(REQUEST *request, RADIUS_PACKET *reply);
293 static void remove_from_proxy_hash(REQUEST *request);
294 static void remove_from_proxy_hash_nl(REQUEST *request, bool yank);
295 static int insert_into_proxy_hash(REQUEST *request);
298 static REQUEST *request_setup(rad_listen_t *listener, RADIUS_PACKET *packet,
299 RADCLIENT *client, RAD_REQUEST_FUNP fun);
301 STATE_MACHINE_DECL(request_common);
302 STATE_MACHINE_DECL(request_response_delay);
303 STATE_MACHINE_DECL(request_cleanup_delay);
304 STATE_MACHINE_DECL(request_running);
306 static void request_coa_originate(REQUEST *request);
307 STATE_MACHINE_DECL(coa_running);
308 STATE_MACHINE_DECL(coa_wait_for_reply);
309 STATE_MACHINE_DECL(coa_no_reply);
310 static void request_coa_separate(REQUEST *coa);
314 #define USEC (1000000)
316 #define INSERT_EVENT(_function, _ctx) if (!fr_event_insert(el, _function, _ctx, &((_ctx)->when), &((_ctx)->ev))) { _rad_panic(__FILE__, __LINE__, "Failed to insert event"); }
318 static void _rad_panic(char const *file, unsigned int line, char const *msg)
320 ERROR("[%s:%d] %s", file, line, msg);
327 #define rad_panic(x) _rad_panic(__FILE__, __LINE__, x)
329 static void tv_add(struct timeval *tv, int usec_delay)
331 if (usec_delay >= USEC) {
332 tv->tv_sec += usec_delay / USEC;
335 tv->tv_usec += usec_delay;
337 if (tv->tv_usec >= USEC) {
338 tv->tv_sec += tv->tv_usec / USEC;
344 * In daemon mode, AND this request has debug flags set.
346 #define DEBUG_PACKET if (!debug_flag && request->log.lvl && request->log.func) debug_packet
348 static void debug_packet(REQUEST *request, RADIUS_PACKET *packet, int direction)
353 char const *received, *from;
354 fr_ipaddr_t const *ip;
359 rad_assert(request->log.func != NULL);
361 if (direction == 0) {
362 received = "Received";
363 from = "from"; /* what else? */
364 ip = &packet->src_ipaddr;
365 port = packet->src_port;
368 received = "Sending";
369 from = "to"; /* hah! */
370 ip = &packet->dst_ipaddr;
371 port = packet->dst_port;
375 * Client-specific debugging re-prints the input
376 * packet into the client log.
378 * This really belongs in a utility library
380 if (is_radius_code(packet->code)) {
381 RDEBUG("%s %s packet %s host %s port %i, id=%i, length=%zu",
382 received, fr_packet_codes[packet->code], from,
383 inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
384 port, packet->id, packet->data_len);
386 RDEBUG("%s packet %s host %s port %d code=%d, id=%d, length=%zu",
388 inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
390 packet->code, packet->id, packet->data_len);
393 for (vp = fr_cursor_init(&cursor, &packet->vps);
395 vp = fr_cursor_next(&cursor)) {
396 vp_prints(buffer, sizeof(buffer), vp);
397 RDEBUG("\t%s", buffer);
402 /***********************************************************************
404 * Start of RADIUS server state machine.
406 ***********************************************************************/
408 static struct timeval *request_response_window(REQUEST *request)
411 * The client hasn't set the response window. Return
412 * either the home server one, if set, or the global one.
414 if (!timerisset(&request->client->response_window)) {
415 return &request->home_server->response_window;
418 if (timercmp(&request->client->response_window,
419 &request->home_server->response_window, <)) {
420 return &request->client->response_window;
423 return &request->home_server->response_window;
427 * Callback for ALL timer events related to the request.
429 static void request_timer(void *ctx)
431 REQUEST *request = ctx;
432 int action = request->timer_action;
436 request->process(request, action);
440 * Only ever called from the master thread.
442 STATE_MACHINE_DECL(request_done)
444 struct timeval now, when;
453 * CoA requests can be cleaned up in the child thread,
454 * but ONLY if they aren't tied into anything.
456 if (request->parent && (request->parent->coa == request)) {
457 rad_assert(!request->in_request_hash);
458 rad_assert(!request->in_proxy_hash);
459 rad_assert(action == FR_ACTION_DONE);
460 rad_assert(request->ev == NULL);
464 #ifdef HAVE_PTHREAD_H
466 * If called from a child thread, mark ourselves as done,
467 * and wait for the master thread timer to clean us up.
469 if (!we_are_master()) {
470 request->child_state = REQUEST_DONE;
478 * Move the CoA request to its own handler.
481 request_coa_separate(request->coa);
482 } else if (request->parent && (request->parent->coa == request)) {
483 request_coa_separate(request);
489 * It doesn't hurt to send duplicate replies. All other
490 * signals are ignored, as the request will be cleaned up
495 if (request->reply->code != 0) {
496 request->listener->send(request->listener, request);
499 RDEBUG("No reply. Ignoring retransmit");
504 * This is only called from the master thread
505 * when there is a child thread processing the
508 case FR_ACTION_CONFLICTING:
509 if (request->child_state == REQUEST_DONE) break;
512 * If there's a reply packet, then we presume
513 * that the child has sent the reply, and we get
514 * pinged here before the child has a chance to
517 if (request->reply->data) break;
519 RERROR("Received conflicting packet from "
520 "client %s port %d - ID: %u due to "
521 "unfinished request. Giving up on old request.",
522 request->client->shortname,
523 request->packet->src_port, request->packet->id);
527 * Called only when there's an error remembering
528 * the packet, or when the socket gets closed from
532 #ifdef HAVE_PTHREAD_H
534 * Do NOT set child_state to DONE if it's still in the queue.
536 if (we_are_master() && (request->child_state == REQUEST_QUEUED)) {
541 * If we have child threads and we're NOT the
542 * thread handling the request, don't do anything.
545 !pthread_equal(pthread_self(), request->child_pid)) {
549 #ifdef DEBUG_STATE_MACHINE
550 if (debug_flag) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n",
551 request->number, __FUNCTION__,
552 child_state_names[request->child_state],
553 child_state_names[REQUEST_DONE]);
555 request->child_state = REQUEST_DONE;
559 * Called when the child is taking too long to
560 * finish. We've already marked it "please
561 * stop", so we don't complain any more.
563 case FR_ACTION_TIMER:
568 * Child is still alive, and we're receiving more
569 * packets from the home server.
571 case FR_ACTION_PROXY_REPLY:
572 RDEBUG2("Reply from home server %s port %d - ID: %d arrived too late. Try increasing 'retry_delay' or 'max_request_time'",
573 inet_ntop(request->proxy->src_ipaddr.af,
574 &request->proxy->src_ipaddr.ipaddr,
575 buffer, sizeof(buffer)),
576 request->proxy->dst_port, request->proxy->id);
581 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
586 * Remove it from the request hash.
588 if (request->in_request_hash) {
590 if (!fr_packet_list_yank(pl, request->packet)) {
593 request->in_request_hash = false;
598 * Wait for the proxy ID to expire. This allows us to
599 * avoid re-use of proxy IDs for a while.
601 if (request->in_proxy_hash) {
602 rad_assert(request->proxy != NULL);
604 fr_event_now(el, &now);
605 when = request->proxy->timestamp;
608 if (((request->proxy->code == PW_CODE_COA_REQUEST) ||
609 (request->proxy->code == PW_CODE_DISCONNECT_REQUEST)) &&
610 (request->packet->code != request->proxy->code)) {
611 when.tv_sec += request->home_server->coa_mrd;
614 timeradd(&when, request_response_window(request), &when);
617 * We haven't received all responses, AND there's still
618 * time to wait. Do so.
620 if ((request->num_proxied_requests > request->num_proxied_responses) &&
622 (request->home_server->proto != IPPROTO_TCP) &&
624 timercmp(&now, &when, <)) {
625 RDEBUG("Waiting for more responses from the home server");
632 remove_from_proxy_hash(request);
636 #ifdef HAVE_PTHREAD_H
638 * If there's no children, we can mark the request as done.
641 request->child_state = REQUEST_DONE;
645 if (request->child_state != REQUEST_DONE) {
646 gettimeofday(&now, NULL);
651 #ifdef HAVE_PTHREAD_H
653 (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0)) {
654 RDEBUG("Waiting for child thread to stop");
659 if (request->delay < (USEC / 3)) request->delay = USEC / 3;
660 tv_add(&when, request->delay);
661 request->delay += request->delay >> 1;
662 if (request->delay > (10 * USEC)) request->delay = 10 * USEC;
664 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
668 #ifdef HAVE_PTHREAD_H
669 rad_assert(request->child_pid == NO_SUCH_CHILD_PID);
673 * @todo: do final states for TCP sockets, too?
675 request_stats_final(request);
677 if (request->listener) request->listener->count--;
680 if (request->packet) {
681 RDEBUG2("Cleaning up request packet ID %u with timestamp +%d",
683 (unsigned int) (request->timestamp - fr_start_time));
684 } /* else don't print anything */
686 if (request->ev) fr_event_delete(el, &request->ev);
688 request_free(&request);
692 static void request_cleanup_delay_init(REQUEST *request, struct timeval const *pnow)
694 struct timeval now, when;
696 if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) goto done;
698 if (!request->root->cleanup_delay) goto done;
703 gettimeofday(&now, NULL);
706 rad_assert(request->reply->timestamp.tv_sec != 0);
707 when = request->reply->timestamp;
709 request->delay = request->root->cleanup_delay;
710 when.tv_sec += request->delay;
713 * Set timer for when we need to clean it up.
715 if (timercmp(&when, &now, >)) {
716 #ifdef DEBUG_STATE_MACHINE
717 if (debug_flag) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_cleanup_delay");
719 request->process = request_cleanup_delay;
720 request->child_state = REQUEST_DONE;
721 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
726 * Otherwise just clean it up.
729 request_done(request, FR_ACTION_DONE);
734 * Function to do all time-related events.
736 static void request_process_timer(REQUEST *request)
738 struct timeval now, when;
739 rad_assert(request->magic == REQUEST_MAGIC);
740 #ifdef DEBUG_STATE_MACHINE
741 int action = FR_ACTION_TIMER;
749 * If we originated a CoA request, divorce it from the
750 * parent. Then, set up the timers so that we can clean
751 * it up as appropriate.
753 if (request->coa) request_coa_separate(request->coa);
756 * If we're the request, OR it isn't originating a CoA
757 * request, check more things.
759 if (!request->proxy || (request->packet->code == request->proxy->code))
762 rad_assert(request->listener != NULL);
765 * The socket was closed. Tell the request that
766 * there is no point in continuing.
768 if (request->listener->status != RAD_LISTEN_STATUS_KNOWN) {
769 if ((request->master_state == REQUEST_ACTIVE) &&
770 (request->child_state < REQUEST_RESPONSE_DELAY)) {
771 WARN("Socket was closed while processing request %u: Stopping it.", request->number);
772 request->master_state = REQUEST_STOP_PROCESSING;
777 gettimeofday(&now, NULL);
780 * The request was forcibly stopped.
782 if (request->master_state == REQUEST_STOP_PROCESSING) {
783 switch (request->child_state) {
785 case REQUEST_RUNNING:
786 #ifdef HAVE_PTHREAD_H
787 rad_assert(spawn_flag == true);
792 * Sleep for some more. We HOPE that the
793 * child will become responsive at some
794 * point in the future.
797 tv_add(&when, request->delay);
798 request->delay += request->delay >> 1;
799 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
803 * These should all be managed by the master thread
806 case REQUEST_PROXIED:
808 case REQUEST_RESPONSE_DELAY:
809 case REQUEST_CLEANUP_DELAY:
812 request_done(request, FR_ACTION_DONE);
817 rad_assert(request->master_state == REQUEST_ACTIVE);
820 * It's still supposed to be running.
822 switch (request->child_state) {
824 case REQUEST_RUNNING:
826 case REQUEST_PROXIED:
828 when = request->packet->timestamp;
829 when.tv_sec += request->root->max_request_time;
832 * Taking too long: tell it to die.
834 if (timercmp(&now, &when, >=)) {
835 #ifdef HAVE_PTHREAD_H
837 * If there's a child thread processing it,
841 (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0)) {
842 ERROR("Unresponsive child for request %u, in component %s module %s",
844 request->component ? request->component : "<core>",
845 request->module ? request->module : "<core>");
846 exec_trigger(request, NULL, "server.thread.unresponsive", true);
849 request->master_state = REQUEST_STOP_PROCESSING;
854 * We should wait for the proxy reply.
856 if (request->child_state == REQUEST_PROXIED) {
857 rad_assert(request->proxy != NULL);
863 if (request->packet->code != request->proxy->code) {
864 if (request->proxy_reply) {
865 request->process = coa_running;
867 request->process = coa_wait_for_reply;
872 if (request->proxy_reply) {
873 request->process = proxy_running;
875 request->process = proxy_wait_for_reply;
881 * If the request has been told to die, we wait.
882 * Otherwise, we wait for the child thread to
887 case REQUEST_RESPONSE_DELAY:
888 rad_assert(request->response_delay > 0);
890 rad_assert(!request->proxy || (request->packet->code == request->proxy->code));
893 request->process = request_response_delay;
895 when = request->reply->timestamp;
897 tv_add(&when, request->response_delay * USEC);
899 if (timercmp(&when, &now, >)) {
900 #ifdef DEBUG_STATE_MACHINE
901 if (debug_flag) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_response_delay");
903 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
905 } /* else it's time to send the reject */
907 RDEBUG2("Sending delayed response");
908 DEBUG_PACKET(request, request->reply, 1);
909 request->listener->send(request->listener, request);
910 request->child_state = REQUEST_CLEANUP_DELAY;
913 case REQUEST_CLEANUP_DELAY:
914 rad_assert(request->root->cleanup_delay > 0);
917 rad_assert(!request->proxy || (request->packet->code == request->proxy->code));
920 request->process = request_cleanup_delay;
922 when = request->reply->timestamp;
923 when.tv_sec += request->root->cleanup_delay;
925 if (timercmp(&when, &now, >)) {
926 #ifdef DEBUG_STATE_MACHINE
927 if (debug_flag) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_cleanup_delay");
929 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
931 } /* else it's time to clean up */
940 static void request_queue_or_run(UNUSED REQUEST *request,
941 fr_request_process_t process)
943 #ifdef DEBUG_STATE_MACHINE
944 int action = FR_ACTION_TIMER;
950 * Do this here so that fewer other functions need to do
953 if (request->master_state == REQUEST_STOP_PROCESSING) {
954 #ifdef DEBUG_STATE_MACHINE
955 if (debug_flag) printf("(%u) ********\tSTATE %s M-%s causes C-%s-> C-%s\t********\n",
956 request->number, __FUNCTION__,
957 master_state_names[request->master_state],
958 child_state_names[request->child_state],
959 child_state_names[REQUEST_DONE]);
961 request_done(request, FR_ACTION_DONE);
965 request->process = process;
967 if (we_are_master()) {
971 * (re) set the initial delay.
973 request->delay = (main_config.init_delay.tv_sec * USEC) + main_config.init_delay.tv_usec;
974 if (request->delay > USEC) request->delay = USEC;
975 gettimeofday(&when, NULL);
976 tv_add(&when, request->delay);
977 request->delay += request->delay >> 1;
979 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
981 #ifdef HAVE_PTHREAD_H
984 * A child thread will eventually pick it up.
986 if (request_enqueue(request)) return;
989 * Otherwise we're not going to do anything with
992 request_done(request, FR_ACTION_DONE);
998 request->child_state = REQUEST_RUNNING;
999 request->process(request, FR_ACTION_RUN);
1003 * Requests that care about child process exit
1004 * codes have already either called
1005 * rad_waitpid(), or they've given up.
1007 while (waitpid(-1, NULL, WNOHANG) > 0);
1011 STATE_MACHINE_DECL(request_common)
1017 TRACE_STATE_MACHINE;
1021 * Bail out as early as possible.
1023 if (request->master_state == REQUEST_STOP_PROCESSING) {
1024 request_done(request, FR_ACTION_DONE);
1032 * We're still waiting for a proxy reply.
1034 if (request->child_state == REQUEST_PROXIED) {
1035 request->process = proxy_wait_for_reply;
1036 proxy_wait_for_reply(request, action);
1041 ERROR("(%u) Ignoring duplicate packet from "
1042 "client %s port %d - ID: %u due to unfinished request "
1043 "in component %s module %s",
1044 request->number, request->client->shortname,
1045 request->packet->src_port,request->packet->id,
1046 request->component, request->module);
1049 case FR_ACTION_CONFLICTING:
1051 * We're in the master thread, ask the child to
1052 * stop processing the request.
1054 request_done(request, action);
1057 case FR_ACTION_TIMER:
1058 request_process_timer(request);
1062 case FR_ACTION_PROXY_REPLY:
1063 RDEBUG2("Reply from home server %s port %d - ID: %d arrived too late. Try increasing 'retry_delay' or 'max_request_time'",
1064 inet_ntop(request->proxy->dst_ipaddr.af,
1065 &request->proxy->dst_ipaddr.ipaddr,
1066 buffer, sizeof(buffer)),
1067 request->proxy->dst_port, request->proxy->id);
1072 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
1077 STATE_MACHINE_DECL(request_cleanup_delay)
1079 struct timeval when;
1081 TRACE_STATE_MACHINE;
1086 if (request->reply->code != 0) {
1087 request->listener->send(request->listener, request);
1089 RDEBUG("No reply. Ignoring retransmit");
1093 * Double the cleanup_delay to catch retransmits.
1095 when = request->reply->timestamp;
1096 request->delay += request->delay ;
1097 when.tv_sec += request->delay;
1099 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
1102 case FR_ACTION_CONFLICTING:
1103 request_done(request, FR_ACTION_DONE);
1107 case FR_ACTION_PROXY_REPLY:
1109 case FR_ACTION_TIMER:
1110 request_common(request, action);
1114 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
1119 STATE_MACHINE_DECL(request_response_delay)
1121 TRACE_STATE_MACHINE;
1126 ERROR("(%u) Discarding duplicate request from "
1127 "client %s port %d - ID: %u due to delayed response",
1128 request->number, request->client->shortname,
1129 request->packet->src_port,request->packet->id);
1133 case FR_ACTION_PROXY_REPLY:
1135 case FR_ACTION_CONFLICTING:
1136 case FR_ACTION_TIMER:
1137 request_common(request, action);
1141 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
1147 static int CC_HINT(nonnull) request_pre_handler(REQUEST *request, UNUSED int action)
1149 TRACE_STATE_MACHINE;
1153 if (request->master_state == REQUEST_STOP_PROCESSING) return 0;
1156 * Don't decode the packet if it's an internal "fake"
1157 * request. Instead, just return so that the caller can
1160 if (request->packet->dst_port == 0) {
1161 request->username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1162 request->password = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
1166 if (!request->packet->vps) { /* FIXME: check for correct state */
1167 rcode = request->listener->decode(request->listener, request);
1170 if (debug_condition) {
1172 * Ignore parse errors.
1174 if (radius_evaluate_cond(request, RLM_MODULE_OK, 0, debug_condition)) {
1175 request->log.lvl = L_DBG_LVL_2;
1176 request->log.func = vradlog_request;
1181 DEBUG_PACKET(request, request->packet, 0);
1187 RDEBUG("Dropping packet without response because of error: %s", fr_strerror());
1188 request->reply->offset = -2; /* bad authenticator */
1192 if (!request->username) {
1193 request->username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1199 STATE_MACHINE_DECL(request_finish)
1203 TRACE_STATE_MACHINE;
1205 (void) action; /* -Wunused */
1207 if (request->master_state == REQUEST_STOP_PROCESSING) {
1213 * Don't send replies if there are none to send.
1215 if (!request->in_request_hash) {
1217 if ((request->listener->type == RAD_LISTEN_AUTH)
1218 #ifdef WITH_ACCOUNTING
1219 || (request->listener->type == RAD_LISTEN_ACCT)
1222 listen_socket_t *sock = request->listener->data;
1224 if (sock->proto == IPPROTO_UDP) return;
1227 * TCP packets aren't in the request
1238 * Override the response code if a control:Response-Packet-Type attribute is present.
1240 vp = pairfind(request->config_items, PW_RESPONSE_PACKET_TYPE, 0, TAG_ANY);
1242 if (vp->vp_integer == 256) {
1243 RDEBUG2("Not responding to request");
1244 request->reply->code = 0;
1246 request->reply->code = vp->vp_integer;
1250 * Catch Auth-Type := Reject BEFORE proxying the packet.
1252 else if (request->packet->code == PW_CODE_AUTHENTICATION_REQUEST) {
1253 if (request->reply->code == 0) {
1254 vp = pairfind(request->config_items, PW_AUTH_TYPE, 0, TAG_ANY);
1256 if (!vp || (vp->vp_integer != PW_CODE_AUTHENTICATION_REJECT)) {
1257 RDEBUG2("There was no response configured: "
1258 "rejecting request");
1261 request->reply->code = PW_CODE_AUTHENTICATION_REJECT;
1266 * Copy Proxy-State from the request to the reply.
1268 vp = paircopy2(request->reply, request->packet->vps,
1269 PW_PROXY_STATE, 0, TAG_ANY);
1270 if (vp) pairadd(&request->reply->vps, vp);
1272 switch (request->reply->code) {
1273 case PW_CODE_AUTHENTICATION_ACK:
1274 rad_postauth(request);
1276 case PW_CODE_ACCESS_CHALLENGE:
1277 pairdelete(&request->config_items, PW_POST_AUTH_TYPE, 0,
1279 vp = pairmake_config("Post-Auth-Type", "Challenge", T_OP_SET);
1280 if (vp) rad_postauth(request);
1287 * Run rejected packets through
1289 * Post-Auth-Type = Reject
1291 * We do this separately so ACK and challenge can change the code
1292 * to reject if a module returns reject.
1294 if (request->reply->code == PW_CODE_AUTHENTICATION_REJECT) {
1295 pairdelete(&request->config_items, PW_POST_AUTH_TYPE, 0, TAG_ANY);
1296 vp = pairmake_config("Post-Auth-Type", "Reject", T_OP_SET);
1297 if (vp) rad_postauth(request);
1301 * Clean up. These are no longer needed.
1303 pairfree(&request->config_items);
1305 pairfree(&request->packet->vps);
1306 request->username = NULL;
1307 request->password = NULL;
1310 if (request->proxy) {
1311 pairfree(&request->proxy->vps);
1313 if (request->proxy_reply) {
1314 pairfree(&request->proxy_reply->vps);
1318 gettimeofday(&request->reply->timestamp, NULL);
1321 * Ignore all "do not respond" packets.
1323 if (!request->reply->code) {
1324 RDEBUG("Not sending reply");
1329 * See if we need to delay an Access-Reject packet.
1331 if ((request->reply->code == PW_CODE_AUTHENTICATION_REJECT) &&
1332 (request->root->reject_delay > 0)) {
1333 request->response_delay = request->root->reject_delay;
1337 * If we timed out a proxy packet, don't delay
1338 * the reject any more.
1340 if (request->proxy && !request->proxy_reply) {
1341 request->response_delay = 0;
1350 if (!request->response_delay) {
1351 DEBUG_PACKET(request, request->reply, 1);
1352 request->listener->send(request->listener,
1356 pairfree(&request->reply->vps);
1358 RDEBUG2("Finished request");
1359 #ifdef WITH_ACCOUNTING
1360 if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) {
1362 request->child_state = REQUEST_DONE;
1366 if (request->root->cleanup_delay == 0) {
1368 request->child_state = REQUEST_DONE;
1371 request->child_state = REQUEST_CLEANUP_DELAY;
1374 RDEBUG2("Delaying response for %d seconds",
1375 request->response_delay);
1377 request->child_state = REQUEST_RESPONSE_DELAY;
1381 STATE_MACHINE_DECL(request_running)
1383 TRACE_STATE_MACHINE;
1386 case FR_ACTION_TIMER:
1387 request_process_timer(request);
1390 case FR_ACTION_CONFLICTING:
1392 request_common(request, action);
1397 * This can happen due to a race condition where
1398 * we send a proxied request, and immediately get
1399 * another reply, before the timer has a chance
1400 * to update the various states.
1402 case FR_ACTION_PROXY_REPLY:
1403 request->child_state = REQUEST_RUNNING;
1404 request->process = proxy_running;
1405 request->process(request, FR_ACTION_RUN);
1410 if (!request_pre_handler(request, action)) {
1411 #ifdef DEBUG_STATE_MACHINE
1412 if (debug_flag) printf("(%u) ********\tSTATE %s failed in pre-handler C-%s -> C-%s\t********\n",
1413 request->number, __FUNCTION__,
1414 child_state_names[request->child_state],
1415 child_state_names[REQUEST_DONE]);
1419 request->child_state = REQUEST_DONE;
1423 rad_assert(request->handle != NULL);
1424 request->handle(request);
1428 * We may need to send a proxied request.
1430 if ((action == FR_ACTION_RUN) &&
1431 request_will_proxy(request)) {
1432 #ifdef DEBUG_STATE_MACHINE
1433 if (debug_flag) printf("(%u) ********\tWill Proxy\t********\n", request->number);
1437 * takes care of setting
1438 * up the post proxy fail
1441 if (request_proxy(request, 0) < 0) goto finished;
1445 #ifdef DEBUG_STATE_MACHINE
1446 if (debug_flag) printf("(%u) ********\tFinished\t********\n", request->number);
1451 * Maybe originate a CoA request.
1453 if ((action == FR_ACTION_RUN) && request->coa) {
1454 request_coa_originate(request);
1461 request_finish(request, action);
1466 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
1471 int request_receive(rad_listen_t *listener, RADIUS_PACKET *packet,
1472 RADCLIENT *client, RAD_REQUEST_FUNP fun)
1475 RADIUS_PACKET **packet_p;
1476 REQUEST *request = NULL;
1478 listen_socket_t *sock = NULL;
1481 * Set the last packet received.
1483 gettimeofday(&now, NULL);
1485 #ifdef WITH_ACCOUNTING
1486 if (listener->type != RAD_LISTEN_DETAIL)
1489 sock = listener->data;
1490 sock->last_packet = now.tv_sec;
1492 packet->timestamp = now;
1495 * Skip everything if required.
1497 if (listener->nodup) goto skip_dup;
1499 packet_p = fr_packet_list_find(pl, packet);
1501 request = fr_packet2myptr(REQUEST, packet, packet_p);
1502 rad_assert(request->in_request_hash);
1505 * Same src/dst ip/port, length, and
1506 * authentication vector: must be a duplicate.
1508 if ((request->packet->data_len == packet->data_len) &&
1509 (memcmp(request->packet->vector, packet->vector,
1510 sizeof(packet->vector)) == 0)) {
1513 * If the request is running, it'
1515 if (request->child_state != REQUEST_DONE) {
1516 request->process(request, FR_ACTION_DUP);
1519 switch (packet->code) {
1520 case PW_CODE_AUTHENTICATION_REQUEST:
1521 FR_STATS_INC(auth, total_dup_requests);
1524 #ifdef WITH_ACCOUNTING
1525 case PW_CODE_ACCOUNTING_REQUEST:
1526 FR_STATS_INC(acct, total_dup_requests);
1530 case PW_CODE_COA_REQUEST:
1531 FR_STATS_INC(coa, total_dup_requests);
1534 case PW_CODE_DISCONNECT_REQUEST:
1535 FR_STATS_INC(dsc, total_dup_requests);
1542 #endif /* WITH_STATS */
1543 return 0; /* duplicate of live request */
1545 #ifdef HAVE_PTHREAD_H
1547 * There should no longer be a child
1548 * thread associated with this request.
1550 rad_assert(pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) != 0);
1554 * Clean up the old request, and allow
1555 * the new one to continue.
1557 request_done(request, FR_ACTION_DONE);
1562 * Say we're ignoring the old one, and continue
1563 * to process the new one.
1565 request->process(request, FR_ACTION_CONFLICTING);
1571 * Quench maximum number of outstanding requests.
1573 if (main_config.max_requests &&
1574 ((count = fr_packet_list_num_elements(pl)) > main_config.max_requests)) {
1575 RATE_LIMIT(ERROR("Dropping request (%d is too many): from client %s port %d - ID: %d", count,
1577 packet->src_port, packet->id);
1578 WARN("Please check the configuration file.\n"
1579 "\tThe value for 'max_requests' is probably set too low.\n"));
1581 exec_trigger(NULL, NULL, "server.max_requests", true);
1587 * Rate-limit the incoming packets
1589 if (sock && sock->max_rate) {
1592 pps = rad_pps(&sock->rate_pps_old, &sock->rate_pps_now, &sock->rate_time, &now);
1593 if (pps > sock->max_rate) {
1594 DEBUG("Dropping request due to rate limiting");
1597 sock->rate_pps_now++;
1600 request = request_setup(listener, packet, client, fun);
1601 if (!request) return 1;
1604 * Remember the request in the list.
1606 if (!listener->nodup) {
1607 if (!fr_packet_list_insert(pl, &request->packet)) {
1608 RERROR("Failed to insert request in the list of live requests: discarding it");
1609 request_done(request, FR_ACTION_DONE);
1613 request->in_request_hash = true;
1617 * Process it. Send a response, and free it.
1619 if (listener->synchronous) {
1620 request->listener->decode(request->listener, request);
1621 request->username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1622 request->password = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
1626 if (request->reply->code != 0) {
1627 request->listener->send(request->listener, request);
1629 RDEBUG("Not sending reply");
1631 request_free(&request);
1636 * Otherwise, insert it into the state machine.
1637 * The child threads will take care of processing it.
1639 request_queue_or_run(request, request_running);
1645 static REQUEST *request_setup(rad_listen_t *listener, RADIUS_PACKET *packet,
1646 RADCLIENT *client, RAD_REQUEST_FUNP fun)
1651 * Create and initialize the new request.
1653 request = request_alloc(NULL);
1654 request->reply = rad_alloc(request, 0);
1655 if (!request->reply) {
1657 request_free(&request);
1661 request->listener = listener;
1662 request->client = client;
1663 request->packet = talloc_steal(request, packet);
1664 request->number = request_num_counter++;
1665 request->priority = listener->type;
1666 request->master_state = REQUEST_ACTIVE;
1667 #ifdef DEBUG_STATE_MACHINE
1668 if (debug_flag) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n",
1669 request->number, __FUNCTION__,
1670 child_state_names[request->child_state],
1671 child_state_names[REQUEST_RUNNING]);
1673 request->child_state = REQUEST_RUNNING;
1674 request->handle = fun;
1678 request->listener->stats.last_packet = request->packet->timestamp.tv_sec;
1679 if (packet->code == PW_CODE_AUTHENTICATION_REQUEST) {
1680 request->client->auth.last_packet = request->packet->timestamp.tv_sec;
1681 radius_auth_stats.last_packet = request->packet->timestamp.tv_sec;
1682 #ifdef WITH_ACCOUNTING
1683 } else if (packet->code == PW_CODE_ACCOUNTING_REQUEST) {
1684 request->client->acct.last_packet = request->packet->timestamp.tv_sec;
1685 radius_acct_stats.last_packet = request->packet->timestamp.tv_sec;
1688 #endif /* WITH_STATS */
1691 * Status-Server packets go to the head of the queue.
1693 if (request->packet->code == PW_CODE_STATUS_SERVER) request->priority = 0;
1696 * Set virtual server identity
1698 if (client->server) {
1699 request->server = client->server;
1700 } else if (listener->server) {
1701 request->server = listener->server;
1703 request->server = NULL;
1706 request->root = &main_config;
1708 request->listener->count++;
1712 * The request passes many of our sanity checks.
1713 * From here on in, if anything goes wrong, we
1714 * send a reject message, instead of dropping the
1719 * Build the reply template from the request.
1722 request->reply->sockfd = request->packet->sockfd;
1723 request->reply->dst_ipaddr = request->packet->src_ipaddr;
1724 request->reply->src_ipaddr = request->packet->dst_ipaddr;
1725 request->reply->dst_port = request->packet->src_port;
1726 request->reply->src_port = request->packet->dst_port;
1727 request->reply->id = request->packet->id;
1728 request->reply->code = 0; /* UNKNOWN code */
1729 memcpy(request->reply->vector, request->packet->vector,
1730 sizeof(request->reply->vector));
1731 request->reply->vps = NULL;
1732 request->reply->data = NULL;
1733 request->reply->data_len = 0;
1739 /***********************************************************************
1743 ***********************************************************************/
1746 * Timer function for all TCP sockets.
1748 static void tcp_socket_timer(void *ctx)
1750 rad_listen_t *listener = ctx;
1751 listen_socket_t *sock = listener->data;
1752 struct timeval end, now;
1754 fr_socket_limit_t *limit;
1758 fr_event_now(el, &now);
1760 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return;
1762 switch (listener->type) {
1764 case RAD_LISTEN_PROXY:
1765 limit = &sock->home->limit;
1769 case RAD_LISTEN_AUTH:
1770 #ifdef WITH_ACCOUNTING
1771 case RAD_LISTEN_ACCT:
1773 limit = &sock->limit;
1781 * If we enforce a lifetime, do it now.
1783 if (limit->lifetime > 0) {
1784 end.tv_sec = sock->opened + limit->lifetime;
1787 if (timercmp(&end, &now, <=)) {
1788 listener->print(listener, buffer, sizeof(buffer));
1789 DEBUG("Reached maximum lifetime on socket %s", buffer);
1793 listener->status = RAD_LISTEN_STATUS_EOL;
1794 event_new_fd(listener);
1803 * Enforce an idle timeout.
1805 if (limit->idle_timeout > 0) {
1806 struct timeval idle;
1808 rad_assert(sock->last_packet != 0);
1809 idle.tv_sec = sock->last_packet + limit->idle_timeout;
1812 if (timercmp(&idle, &now, <=)) {
1813 listener->print(listener, buffer, sizeof(buffer));
1814 DEBUG("Reached idle timeout on socket %s", buffer);
1819 * Enforce the minimum of idle timeout or lifetime.
1821 if (timercmp(&idle, &end, <)) {
1827 * Wake up at t + 0.5s. The code above checks if the timers
1828 * are <= t. This addition gives us a bit of leeway.
1830 end.tv_usec = USEC / 2;
1832 if (!fr_event_insert(el, tcp_socket_timer, listener, &end, &sock->ev)) {
1833 rad_panic("Failed to insert event");
1840 * Add +/- 2s of jitter, as suggested in RFC 3539
1843 static void add_jitter(struct timeval *when)
1850 jitter ^= (jitter >> 10);
1851 jitter &= ((1 << 22) - 1); /* 22 bits of 1 */
1854 * Add in ~ (4 * USEC) of jitter.
1856 tv_add(when, jitter);
1860 * Called by socket_del to remove requests with this socket
1862 static int eol_proxy_listener(void *ctx, void *data)
1864 rad_listen_t *this = ctx;
1865 RADIUS_PACKET **proxy_p = data;
1868 request = fr_packet2myptr(REQUEST, proxy, proxy_p);
1869 if (request->proxy_listener != this) return 0;
1872 * The normal "remove_from_proxy_hash" tries to grab the
1873 * proxy mutex. We already have it held, so grabbing it
1874 * again will cause a deadlock. Instead, call the "no
1875 * lock" version of the function.
1877 rad_assert(request->in_proxy_hash == true);
1878 remove_from_proxy_hash_nl(request, false);
1881 * Don't mark it as DONE. The client can retransmit, and
1882 * the packet SHOULD be re-proxied somewhere else.
1884 * Return "2" means that the rbtree code will remove it
1885 * from the tree, and we don't need to do it ourselves.
1889 #endif /* WITH_PROXY */
1891 static int eol_listener(void *ctx, void *data)
1893 rad_listen_t *this = ctx;
1894 RADIUS_PACKET **packet_p = data;
1897 request = fr_packet2myptr(REQUEST, packet, packet_p);
1898 if (request->listener != this) return 0;
1900 request->master_state = REQUEST_STOP_PROCESSING;
1904 #endif /* WITH_TCP */
1907 /***********************************************************************
1909 * Proxy handlers for the state machine.
1911 ***********************************************************************/
1914 * Called with the proxy mutex held
1916 static void remove_from_proxy_hash_nl(REQUEST *request, bool yank)
1918 if (!request->in_proxy_hash) return;
1920 fr_packet_list_id_free(proxy_list, request->proxy, yank);
1921 request->in_proxy_hash = false;
1924 * On the FIRST reply, decrement the count of outstanding
1925 * requests. Note that this is NOT the count of sent
1926 * packets, but whether or not the home server has
1929 if (request->home_server &&
1930 request->home_server->currently_outstanding) {
1931 request->home_server->currently_outstanding--;
1934 * If we're NOT sending it packets, then we don't know
1935 * if it's alive or dead.
1937 if ((request->home_server->currently_outstanding == 0) &&
1938 (request->home_server->state == HOME_STATE_ALIVE)) {
1939 request->home_server->state = HOME_STATE_UNKNOWN;
1940 request->home_server->last_packet_sent = 0;
1941 request->home_server->last_packet_recv = 0;
1946 rad_assert(request->proxy_listener != NULL);
1947 request->proxy_listener->count--;
1949 request->proxy_listener = NULL;
1952 * Got from YES in hash, to NO, not in hash while we hold
1953 * the mutex. This guarantees that when another thread
1954 * grabs the mutex, the "not in hash" flag is correct.
1956 RDEBUG3("proxy: request is no longer in proxy hash");
1959 static void remove_from_proxy_hash(REQUEST *request)
1962 * Check this without grabbing the mutex because it's a
1963 * lot faster that way.
1965 if (!request->in_proxy_hash) return;
1968 * The "not in hash" flag is definitive. However, if the
1969 * flag says that it IS in the hash, there might still be
1970 * a race condition where it isn't.
1972 PTHREAD_MUTEX_LOCK(&proxy_mutex);
1974 if (!request->in_proxy_hash) {
1975 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
1979 remove_from_proxy_hash_nl(request, true);
1981 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
1984 static int insert_into_proxy_hash(REQUEST *request)
1988 void *proxy_listener;
1990 rad_assert(request->proxy != NULL);
1991 rad_assert(request->home_server != NULL);
1992 rad_assert(proxy_list != NULL);
1995 PTHREAD_MUTEX_LOCK(&proxy_mutex);
1996 proxy_listener = NULL;
1997 request->num_proxied_requests = 1;
1998 request->num_proxied_responses = 0;
2000 for (tries = 0; tries < 2; tries++) {
2002 listen_socket_t *sock;
2004 RDEBUG3("proxy: Trying to allocate ID (%d/2)", tries);
2005 rcode = fr_packet_list_id_alloc(proxy_list,
2006 request->home_server->proto,
2007 &request->proxy, &proxy_listener);
2008 if ((debug_flag > 2) && (rcode == 0)) {
2009 RDEBUG("proxy: Failed allocating ID: %s", fr_strerror());
2011 if (rcode > 0) break;
2012 if (tries > 0) continue; /* try opening new socket only once */
2014 #ifdef HAVE_PTHREAD_H
2015 if (proxy_no_new_sockets) break;
2018 RDEBUG3("proxy: Trying to open a new listener to the home server");
2019 this = proxy_new_listener(request->home_server, 0);
2021 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2025 request->proxy->src_port = 0; /* Use any new socket */
2026 proxy_listener = this;
2029 if (!fr_packet_list_socket_add(proxy_list, this->fd,
2031 &sock->other_ipaddr, sock->other_port,
2034 #ifdef HAVE_PTHREAD_H
2035 proxy_no_new_sockets = true;
2037 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2040 * This is bad. However, the
2041 * packet list now supports 256
2042 * open sockets, which should
2043 * minimize this problem.
2045 ERROR("Failed adding proxy socket: %s",
2051 * Add it to the event loop. Ensure that we have
2052 * only one mutex locked at a time.
2054 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2055 radius_update_listener(this);
2056 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2059 if (!proxy_listener || (rcode == 0)) {
2060 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2061 REDEBUG2("proxy: Failed allocating Id for proxied request");
2063 request->proxy_listener = NULL;
2064 request->in_proxy_hash = false;
2068 rad_assert(request->proxy->id >= 0);
2070 request->proxy_listener = proxy_listener;
2071 request->in_proxy_hash = true;
2072 RDEBUG3("proxy: request is now in proxy hash");
2075 * Keep track of maximum outstanding requests to a
2076 * particular home server. 'max_outstanding' is
2077 * enforced in home_server_ldb(), in realms.c.
2079 request->home_server->currently_outstanding++;
2082 request->proxy_listener->count++;
2085 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2087 RDEBUG3("proxy: allocating destination %s port %d - Id %d",
2088 inet_ntop(request->proxy->dst_ipaddr.af,
2089 &request->proxy->dst_ipaddr.ipaddr, buf, sizeof(buf)),
2090 request->proxy->dst_port,
2091 request->proxy->id);
2096 static int process_proxy_reply(REQUEST *request, RADIUS_PACKET *reply)
2099 int post_proxy_type = 0;
2103 * There may be a proxy reply, but it may be too late.
2105 if (!request->proxy_listener) return 0;
2108 * Delete any reply we had accumulated until now.
2110 pairfree(&request->reply->vps);
2113 * Run the packet through the post-proxy stage,
2114 * BEFORE playing games with the attributes.
2116 vp = pairfind(request->config_items, PW_POST_PROXY_TYPE, 0, TAG_ANY);
2119 * If we have a proxy_reply, and it was a reject, setup
2120 * post-proxy-type Reject
2123 reply->code == PW_CODE_AUTHENTICATION_REJECT) {
2126 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Reject");
2128 vp = radius_paircreate(request, &request->config_items,
2129 PW_POST_PROXY_TYPE, 0);
2131 vp->vp_integer = dval->value;
2136 post_proxy_type = vp->vp_integer;
2138 RDEBUG2("Found Post-Proxy-Type %s", dict_valnamebyattr(PW_POST_PROXY_TYPE, 0, post_proxy_type));
2143 * Decode the packet.
2145 rcode = request->proxy_listener->decode(request->proxy_listener, request);
2146 DEBUG_PACKET(request, reply, 0);
2149 * Pro-actively remove it from the proxy hash.
2150 * This is later than in 2.1.x, but it means that
2151 * the replies are authenticated before being
2152 * removed from the hash.
2155 (request->num_proxied_requests <= request->num_proxied_responses)) {
2156 remove_from_proxy_hash(request);
2159 remove_from_proxy_hash(request);
2162 if (request->home_pool && request->home_pool->virtual_server) {
2163 char const *old_server = request->server;
2165 request->server = request->home_pool->virtual_server;
2166 RDEBUG2("server %s {", request->server);
2168 rcode = process_post_proxy(post_proxy_type, request);
2171 request->server = old_server;
2173 rcode = process_post_proxy(post_proxy_type, request);
2177 if (request->packet->code == request->proxy->code)
2179 * Don't run the next bit if we originated a CoA
2180 * packet, after receiving an Access-Request or
2181 * Accounting-Request.
2186 * There may NOT be a proxy reply, as we may be
2187 * running Post-Proxy-Type = Fail.
2190 request->reply->vps = paircopy(request->reply, reply->vps);
2193 * Delete the Proxy-State Attributes from
2194 * the reply. These include Proxy-State
2195 * attributes from us and remote server.
2197 pairdelete(&request->reply->vps, PW_PROXY_STATE, 0, TAG_ANY);
2201 default: /* Don't do anything */
2203 case RLM_MODULE_FAIL:
2206 case RLM_MODULE_HANDLED:
2213 int request_proxy_reply(RADIUS_PACKET *packet)
2215 RADIUS_PACKET **proxy_p;
2220 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2221 proxy_p = fr_packet_list_find_byreply(proxy_list, packet);
2224 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2225 PROXY( "No outstanding request was found for reply from host %s port %d - ID %u",
2226 inet_ntop(packet->src_ipaddr.af,
2227 &packet->src_ipaddr.ipaddr,
2228 buffer, sizeof(buffer)),
2229 packet->src_port, packet->id);
2233 request = fr_packet2myptr(REQUEST, proxy, proxy_p);
2234 request->num_proxied_responses++; /* needs to be protected by lock */
2236 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2239 * No reply, BUT the current packet fails verification:
2240 * ignore it. This does the MD5 calculations in the
2241 * server core, but I guess we can fix that later.
2243 if (!request->proxy_reply &&
2244 (rad_verify(packet, request->proxy,
2245 request->home_server->secret) != 0)) {
2246 DEBUG("Ignoring spoofed proxy reply. Signature is invalid");
2251 * The home server sent us a packet which doesn't match
2252 * something we have: ignore it. This is done only to
2253 * catch the case of broken systems.
2255 if (request->proxy_reply &&
2256 (memcmp(request->proxy_reply->vector,
2258 sizeof(request->proxy_reply->vector)) != 0)) {
2259 RDEBUG2("Ignoring conflicting proxy reply");
2263 gettimeofday(&now, NULL);
2266 * Status-Server packets don't count as real packets.
2268 if (request->proxy->code != PW_CODE_STATUS_SERVER) {
2269 listen_socket_t *sock = request->proxy_listener->data;
2271 request->home_server->last_packet_recv = now.tv_sec;
2272 sock->last_packet = now.tv_sec;
2276 * If we have previously seen a reply, ignore the
2279 if (request->proxy_reply) {
2280 RDEBUG2("Discarding duplicate reply from host %s port %d - ID: %d",
2281 inet_ntop(packet->src_ipaddr.af,
2282 &packet->src_ipaddr.ipaddr,
2283 buffer, sizeof(buffer)),
2284 packet->src_port, packet->id);
2289 * Call the state machine to do something useful with the
2292 request->proxy_reply = talloc_steal(request, packet);
2293 packet->timestamp = now;
2294 request->priority = RAD_LISTEN_PROXY;
2297 * We've received a reply. If we hadn't been sending it
2298 * packets for a while, just mark it alive.
2300 if (request->home_server->state == HOME_STATE_UNKNOWN) {
2301 request->home_server->state = HOME_STATE_ALIVE;
2305 request->home_server->stats.last_packet = packet->timestamp.tv_sec;
2306 request->proxy_listener->stats.last_packet = packet->timestamp.tv_sec;
2308 if (request->proxy->code == PW_CODE_AUTHENTICATION_REQUEST) {
2309 proxy_auth_stats.last_packet = packet->timestamp.tv_sec;
2310 #ifdef WITH_ACCOUNTING
2311 } else if (request->proxy->code == PW_CODE_ACCOUNTING_REQUEST) {
2312 proxy_acct_stats.last_packet = packet->timestamp.tv_sec;
2315 #endif /* WITH_STATS */
2319 * When we originate CoA requests, we patch them in here
2320 * so that they don't affect the rest of the state
2323 if (request->parent) {
2324 rad_assert(request->parent->coa == request);
2325 rad_assert((request->proxy->code == PW_CODE_COA_REQUEST) ||
2326 (request->proxy->code == PW_CODE_DISCONNECT_REQUEST));
2327 rad_assert(request->process != NULL);
2328 request_coa_separate(request);
2332 request->process(request, FR_ACTION_PROXY_REPLY);
2338 static int setup_post_proxy_fail(REQUEST *request)
2340 DICT_VALUE const *dval = NULL;
2343 if (request->proxy->code == PW_CODE_AUTHENTICATION_REQUEST) {
2344 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0,
2345 "Fail-Authentication");
2347 } else if (request->proxy->code == PW_CODE_ACCOUNTING_REQUEST) {
2348 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0,
2351 } else if (request->proxy->code == PW_CODE_COA_REQUEST) {
2352 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-CoA");
2354 } else if (request->proxy->code == PW_CODE_DISCONNECT_REQUEST) {
2355 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-Disconnect");
2358 WARN("Unknown packet type in Post-Proxy-Type Fail: ignoring");
2362 if (!dval) dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail");
2365 pairdelete(&request->config_items, PW_POST_PROXY_TYPE, 0, TAG_ANY);
2369 vp = pairfind(request->config_items, PW_POST_PROXY_TYPE, 0, TAG_ANY);
2370 if (!vp) vp = radius_paircreate(request, &request->config_items,
2371 PW_POST_PROXY_TYPE, 0);
2372 vp->vp_integer = dval->value;
2377 STATE_MACHINE_DECL(proxy_no_reply)
2379 TRACE_STATE_MACHINE;
2382 case FR_ACTION_CONFLICTING:
2384 case FR_ACTION_TIMER:
2385 case FR_ACTION_PROXY_REPLY:
2386 request_common(request, action);
2390 if (process_proxy_reply(request, NULL)) {
2391 request_finish(request, action);
2393 request_done(request, FR_ACTION_DONE);
2397 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
2402 STATE_MACHINE_DECL(proxy_running)
2404 TRACE_STATE_MACHINE;
2407 case FR_ACTION_CONFLICTING:
2409 case FR_ACTION_TIMER:
2410 case FR_ACTION_PROXY_REPLY:
2411 request_common(request, action);
2415 if (process_proxy_reply(request, request->proxy_reply)) {
2416 request->handle(request);
2417 request_finish(request, action);
2419 request_done(request, FR_ACTION_DONE);
2424 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
2429 static int request_will_proxy(REQUEST *request)
2431 int rcode, pre_proxy_type = 0;
2432 char const *realmname = NULL;
2433 VALUE_PAIR *vp, *strippedname;
2434 home_server_t *home;
2435 REALM *realm = NULL;
2436 home_pool_t *pool = NULL;
2438 if (!request->root->proxy_requests) return 0;
2439 if (request->packet->dst_port == 0) return 0;
2440 if (request->packet->code == PW_CODE_STATUS_SERVER) return 0;
2441 if (request->in_proxy_hash) return 0;
2444 * FIXME: for 3.0, allow this only for rejects?
2446 if (request->reply->code != 0) return 0;
2448 vp = pairfind(request->config_items, PW_PROXY_TO_REALM, 0, TAG_ANY);
2450 realm = realm_find2(vp->vp_strvalue);
2452 REDEBUG2("Cannot proxy to unknown realm %s",
2457 realmname = vp->vp_strvalue;
2460 * Figure out which pool to use.
2462 if (request->packet->code == PW_CODE_AUTHENTICATION_REQUEST) {
2463 pool = realm->auth_pool;
2465 #ifdef WITH_ACCOUNTING
2466 } else if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) {
2467 pool = realm->acct_pool;
2471 } else if ((request->packet->code == PW_CODE_COA_REQUEST) ||
2472 (request->packet->code == PW_CODE_DISCONNECT_REQUEST)) {
2473 pool = realm->coa_pool;
2483 vp = pairfind(request->config_items, PW_HOME_SERVER_POOL, 0, TAG_ANY);
2486 switch (request->packet->code) {
2487 case PW_CODE_AUTHENTICATION_REQUEST:
2488 pool_type = HOME_TYPE_AUTH;
2491 #ifdef WITH_ACCOUNTING
2492 case PW_CODE_ACCOUNTING_REQUEST:
2493 pool_type = HOME_TYPE_ACCT;
2498 case PW_CODE_COA_REQUEST:
2499 case PW_CODE_DISCONNECT_REQUEST:
2500 pool_type = HOME_TYPE_COA;
2508 pool = home_pool_byname(vp->vp_strvalue, pool_type);
2512 RWDEBUG2("Cancelling proxy as no home pool exists");
2516 if (request->listener->synchronous) {
2517 WARN("Cannot proxy a request which is from a 'synchronous' socket");
2521 request->home_pool = pool;
2523 home = home_server_ldb(realmname, pool, request);
2525 REDEBUG2("Failed to find live home server: Cancelling proxy");
2528 home_server_update_request(home, request);
2532 * Once we've decided to proxy a request, we cannot send
2533 * a CoA packet. So we free up any CoA packet here.
2535 if (request->coa) request_done(request->coa, FR_ACTION_DONE);
2539 * Remember that we sent the request to a Realm.
2541 if (realmname) pairmake_packet("Realm", realmname, T_OP_EQ);
2544 * Strip the name, if told to.
2546 * Doing it here catches the case of proxied tunneled
2549 if (realm && (realm->striprealm == true) &&
2550 (strippedname = pairfind(request->proxy->vps, PW_STRIPPED_USER_NAME, 0, TAG_ANY)) != NULL) {
2552 * If there's a Stripped-User-Name attribute in
2553 * the request, then use THAT as the User-Name
2554 * for the proxied request, instead of the
2557 * This is done by making a copy of the
2558 * Stripped-User-Name attribute, turning it into
2559 * a User-Name attribute, deleting the
2560 * Stripped-User-Name and User-Name attributes
2561 * from the vps list, and making the new
2562 * User-Name the head of the vps list.
2564 vp = pairfind(request->proxy->vps, PW_USER_NAME, 0, TAG_ANY);
2567 vp = radius_paircreate(NULL, NULL,
2569 rad_assert(vp != NULL); /* handled by above function */
2570 /* Insert at the START of the list */
2571 /* FIXME: Can't make assumptions about ordering */
2572 fr_cursor_init(&cursor, &vp);
2573 fr_cursor_insert(&cursor, request->proxy->vps);
2574 request->proxy->vps = vp;
2576 pairstrcpy(vp, strippedname->vp_strvalue);
2579 * Do NOT delete Stripped-User-Name.
2584 * If there is no PW_CHAP_CHALLENGE attribute but
2585 * there is a PW_CHAP_PASSWORD we need to add it
2586 * since we can't use the request authenticator
2587 * anymore - we changed it.
2589 if ((request->packet->code == PW_CODE_AUTHENTICATION_REQUEST) &&
2590 pairfind(request->proxy->vps, PW_CHAP_PASSWORD, 0, TAG_ANY) &&
2591 pairfind(request->proxy->vps, PW_CHAP_CHALLENGE, 0, TAG_ANY) == NULL) {
2592 vp = radius_paircreate(request->proxy, &request->proxy->vps, PW_CHAP_CHALLENGE, 0);
2593 pairmemcpy(vp, request->packet->vector, sizeof(request->packet->vector));
2597 * The RFC's say we have to do this, but FreeRADIUS
2600 vp = radius_paircreate(request->proxy, &request->proxy->vps, PW_PROXY_STATE, 0);
2601 pairsprintf(vp, "%u", request->packet->id);
2604 * Should be done BEFORE inserting into proxy hash, as
2605 * pre-proxy may use this information, or change it.
2607 request->proxy->code = request->packet->code;
2610 * Call the pre-proxy routines.
2612 vp = pairfind(request->config_items, PW_PRE_PROXY_TYPE, 0, TAG_ANY);
2614 DICT_VALUE const *dval = dict_valbyattr(vp->da->attr, vp->da->vendor, vp->vp_integer);
2615 /* Must be a validation issue */
2617 RDEBUG2("Found Pre-Proxy-Type %s", dval->name);
2618 pre_proxy_type = vp->vp_integer;
2621 rad_assert(request->home_pool != NULL);
2623 if (request->home_pool->virtual_server) {
2624 char const *old_server = request->server;
2626 request->server = request->home_pool->virtual_server;
2628 RDEBUG2("server %s {", request->server);
2630 rcode = process_pre_proxy(pre_proxy_type, request);
2634 request->server = old_server;
2636 rcode = process_pre_proxy(pre_proxy_type, request);
2639 case RLM_MODULE_FAIL:
2640 case RLM_MODULE_INVALID:
2641 case RLM_MODULE_NOTFOUND:
2642 case RLM_MODULE_USERLOCK:
2644 /* FIXME: debug print failed stuff */
2647 case RLM_MODULE_REJECT:
2648 case RLM_MODULE_HANDLED:
2652 * Only proxy the packet if the pre-proxy code succeeded.
2654 case RLM_MODULE_NOOP:
2656 case RLM_MODULE_UPDATED:
2663 static int request_proxy(REQUEST *request, int retransmit)
2667 rad_assert(request->parent == NULL);
2668 rad_assert(request->home_server != NULL);
2670 if (request->master_state == REQUEST_STOP_PROCESSING) return 0;
2674 RWDEBUG("Cannot proxy and originate CoA packets at the same time. Cancelling CoA request");
2675 request_done(request->coa, FR_ACTION_DONE);
2680 * The request may need sending to a virtual server.
2681 * This code is more than a little screwed up. The rest
2682 * of the state machine doesn't handle parent / child
2683 * relationships well. i.e. if the child request takes
2684 * too long, the core will mark the *parent* as "stop
2685 * processing". And the child will continue without
2686 * knowing anything...
2688 * So, we have some horrible hacks to get around that.
2690 if (request->home_server->server) {
2693 if (request->packet->dst_port == 0) {
2694 WARN("Cannot proxy an internal request");
2698 DEBUG("Proxying to virtual server %s",
2699 request->home_server->server);
2702 * Packets to virtual serrers don't get
2703 * retransmissions sent to them. And the virtual
2704 * server is run ONLY if we have no child
2705 * threads, or we're running in a child thread.
2707 rad_assert(retransmit == 0);
2708 rad_assert(!spawn_flag || !we_are_master());
2710 fake = request_alloc_fake(request);
2712 fake->packet->vps = paircopy(fake->packet, request->packet->vps);
2713 talloc_free(request->proxy);
2715 fake->server = request->home_server->server;
2716 fake->handle = request->handle;
2717 fake->process = NULL; /* should never be run for anything */
2720 * Run the virtual server.
2722 request_running(fake, FR_ACTION_RUN);
2724 request->proxy = talloc_steal(request, fake->packet);
2725 fake->packet = NULL;
2726 request->proxy_reply = talloc_steal(request, fake->reply);
2729 request_free(&fake);
2732 * Just do the work here, rather than trying to
2733 * run the "decode proxy reply" stuff...
2735 process_proxy_reply(request, request->proxy_reply);
2737 request->handle(request); /* to do more post-proxy stuff */
2739 return -1; /* so we call request_finish */
2743 * We're actually sending a proxied packet. Do that now.
2745 if (!request->in_proxy_hash && !insert_into_proxy_hash(request)) {
2746 ERROR("Failed to insert request into the proxy list");
2750 rad_assert(request->proxy->id >= 0);
2753 if (request->home_server->tls) {
2754 RDEBUG2("Proxying request to home server %s port %d (TLS)",
2755 inet_ntop(request->proxy->dst_ipaddr.af,
2756 &request->proxy->dst_ipaddr.ipaddr,
2757 buffer, sizeof(buffer)),
2758 request->proxy->dst_port);
2761 RDEBUG2("Proxying request to home server %s port %d",
2762 inet_ntop(request->proxy->dst_ipaddr.af,
2763 &request->proxy->dst_ipaddr.ipaddr,
2764 buffer, sizeof(buffer)),
2765 request->proxy->dst_port);
2767 DEBUG_PACKET(request, request->proxy, 1);
2769 gettimeofday(&request->proxy_retransmit, NULL);
2771 request->proxy->timestamp = request->proxy_retransmit;
2772 request->home_server->last_packet_sent = request->proxy_retransmit.tv_sec;
2775 FR_STATS_TYPE_INC(request->home_server->stats.total_requests);
2777 request->child_state = REQUEST_PROXIED;
2778 request->proxy_listener->send(request->proxy_listener,
2784 * Proxy the packet as if it was new.
2786 static int request_proxy_anew(REQUEST *request)
2788 home_server_t *home;
2791 * Delete the request from the proxy list.
2793 * The packet list code takes care of ensuring that IDs
2794 * aren't reused until all 256 IDs have been used. So
2795 * there's a 1/256 chance of re-using the same ID when
2796 * we're sending to the same home server. Which is
2799 remove_from_proxy_hash(request);
2802 * Find a live home server for the request.
2804 home = home_server_ldb(NULL, request->home_pool, request);
2806 REDEBUG2("Failed to find live home server for request");
2808 if (setup_post_proxy_fail(request)) {
2809 request_queue_or_run(request, proxy_running);
2811 gettimeofday(&request->reply->timestamp, NULL);
2812 request_cleanup_delay_init(request, NULL);
2816 home_server_update_request(home, request);
2818 if (!insert_into_proxy_hash(request)) {
2819 RPROXY("Failed to insert retransmission into the proxy list");
2820 goto post_proxy_fail;
2824 * Free the old packet, to force re-encoding
2826 talloc_free(request->proxy->data);
2827 request->proxy->data = NULL;
2828 request->proxy->data_len = 0;
2830 #ifdef WITH_ACCOUNTING
2832 * Update the Acct-Delay-Time attribute.
2834 if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) {
2837 vp = pairfind(request->proxy->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY);
2838 if (!vp) vp = radius_paircreate(request->proxy,
2839 &request->proxy->vps,
2840 PW_ACCT_DELAY_TIME, 0);
2844 gettimeofday(&now, NULL);
2845 vp->vp_integer += now.tv_sec - request->proxy_retransmit.tv_sec;
2850 if (request_proxy(request, 1) != 1) goto post_proxy_fail;
2855 STATE_MACHINE_DECL(request_ping)
2857 home_server_t *home = request->home_server;
2860 TRACE_STATE_MACHINE;
2864 case FR_ACTION_TIMER:
2865 ERROR("No response to status check %d for home server %s port %d",
2867 inet_ntop(request->proxy->dst_ipaddr.af,
2868 &request->proxy->dst_ipaddr.ipaddr,
2869 buffer, sizeof(buffer)),
2870 request->proxy->dst_port);
2873 case FR_ACTION_PROXY_REPLY:
2874 rad_assert(request->in_proxy_hash);
2876 request->home_server->num_received_pings++;
2877 RPROXY("Received response to status check %d (%d in current sequence)",
2878 request->number, home->num_received_pings);
2881 * Remove the request from any hashes
2883 fr_event_delete(el, &request->ev);
2884 remove_from_proxy_hash(request);
2887 * The control socket may have marked the home server as
2888 * alive. OR, it may have suddenly started responding to
2889 * requests again. If so, don't re-do the "make alive"
2892 if (home->state == HOME_STATE_ALIVE) break;
2895 * It's dead, and we haven't received enough ping
2896 * responses to mark it "alive". Wait a bit.
2898 * If it's zombie, we mark it alive immediately.
2900 if ((home->state == HOME_STATE_IS_DEAD) &&
2901 (home->num_received_pings < home->num_pings_to_alive)) {
2906 * Mark it alive and delete any outstanding
2909 home->state = HOME_STATE_ALIVE;
2910 exec_trigger(request, home->cs, "home_server.alive", false);
2911 home->currently_outstanding = 0;
2912 home->num_sent_pings = 0;
2913 home->num_received_pings = 0;
2914 gettimeofday(&home->revive_time, NULL);
2916 fr_event_delete(el, &home->ev);
2918 RPROXY("Marking home server %s port %d alive",
2919 inet_ntop(request->proxy->dst_ipaddr.af,
2920 &request->proxy->dst_ipaddr.ipaddr,
2921 buffer, sizeof(buffer)),
2922 request->proxy->dst_port);
2926 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
2930 rad_assert(!request->in_request_hash);
2931 rad_assert(request->ev == NULL);
2932 request_done(request, FR_ACTION_DONE);
2936 * Called from start of zombie period, OR after control socket
2937 * marks the home server dead.
2939 static void ping_home_server(void *ctx)
2941 home_server_t *home = ctx;
2944 struct timeval when, now;
2946 if ((home->state == HOME_STATE_ALIVE) ||
2947 (home->ping_check == HOME_PING_CHECK_NONE) ||
2949 (home->proto == IPPROTO_TCP) ||
2951 (home->ev != NULL)) {
2955 gettimeofday(&now, NULL);
2957 if (home->state == HOME_STATE_ZOMBIE) {
2958 when = home->zombie_period_start;
2959 when.tv_sec += home->zombie_period;
2961 if (timercmp(&when, &now, <)) {
2962 DEBUG("PING: Zombie period is over for home server %s",
2964 mark_home_server_dead(home, &now);
2968 request = request_alloc(NULL);
2969 request->number = request_num_counter++;
2972 request->proxy = rad_alloc(request, 1);
2973 rad_assert(request->proxy != NULL);
2975 if (home->ping_check == HOME_PING_CHECK_STATUS_SERVER) {
2976 request->proxy->code = PW_CODE_STATUS_SERVER;
2978 pairmake(request->proxy, &request->proxy->vps,
2979 "Message-Authenticator", "0x00", T_OP_SET);
2981 } else if (home->type == HOME_TYPE_AUTH) {
2982 request->proxy->code = PW_CODE_AUTHENTICATION_REQUEST;
2984 pairmake(request->proxy, &request->proxy->vps,
2985 "User-Name", home->ping_user_name, T_OP_SET);
2986 pairmake(request->proxy, &request->proxy->vps,
2987 "User-Password", home->ping_user_password, T_OP_SET);
2988 pairmake(request->proxy, &request->proxy->vps,
2989 "Service-Type", "Authenticate-Only", T_OP_SET);
2990 pairmake(request->proxy, &request->proxy->vps,
2991 "Message-Authenticator", "0x00", T_OP_SET);
2994 #ifdef WITH_ACCOUNTING
2995 request->proxy->code = PW_CODE_ACCOUNTING_REQUEST;
2997 pairmake(request->proxy, &request->proxy->vps,
2998 "User-Name", home->ping_user_name, T_OP_SET);
2999 pairmake(request->proxy, &request->proxy->vps,
3000 "Acct-Status-Type", "Stop", T_OP_SET);
3001 pairmake(request->proxy, &request->proxy->vps,
3002 "Acct-Session-Id", "00000000", T_OP_SET);
3003 vp = pairmake(request->proxy, &request->proxy->vps,
3004 "Event-Timestamp", "0", T_OP_SET);
3005 vp->vp_date = now.tv_sec;
3007 rad_assert("Internal sanity check failed");
3011 vp = pairmake(request->proxy, &request->proxy->vps,
3012 "NAS-Identifier", "", T_OP_SET);
3014 pairsprintf(vp, "Status Check %u. Are you alive?",
3015 home->num_sent_pings);
3018 request->proxy->src_ipaddr = home->src_ipaddr;
3019 request->proxy->dst_ipaddr = home->ipaddr;
3020 request->proxy->dst_port = home->port;
3021 request->home_server = home;
3022 #ifdef DEBUG_STATE_MACHINE
3023 if (debug_flag) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n", request->number, __FUNCTION__,
3024 child_state_names[request->child_state],
3025 child_state_names[REQUEST_DONE]);
3026 if (debug_flag) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_ping");
3028 #ifdef HAVE_PTHREAD_H
3029 rad_assert(request->child_pid == NO_SUCH_CHILD_PID);
3031 request->child_state = REQUEST_DONE;
3032 request->process = request_ping;
3034 rad_assert(request->proxy_listener == NULL);
3036 if (!insert_into_proxy_hash(request)) {
3037 RPROXY("Failed to insert status check %d into proxy list. Discarding it.",
3040 rad_assert(!request->in_request_hash);
3041 rad_assert(!request->in_proxy_hash);
3042 rad_assert(request->ev == NULL);
3043 request_free(&request);
3048 * Set up the timer callback.
3051 when.tv_sec += home->ping_timeout;
3053 DEBUG("PING: Waiting %u seconds for response to ping",
3054 home->ping_timeout);
3056 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3057 home->num_sent_pings++;
3059 rad_assert(request->proxy_listener != NULL);
3060 request->proxy_listener->send(request->proxy_listener,
3064 * Add +/- 2s of jitter, as suggested in RFC 3539
3065 * and in the Issues and Fixes draft.
3068 home->when.tv_sec += home->ping_interval;
3070 add_jitter(&home->when);
3072 DEBUG("PING: Next status packet in %u seconds", home->ping_interval);
3073 INSERT_EVENT(ping_home_server, home);
3076 static void home_trigger(home_server_t *home, char const *trigger)
3079 RADIUS_PACKET my_packet;
3081 memset(&my_request, 0, sizeof(my_request));
3082 memset(&my_packet, 0, sizeof(my_packet));
3083 my_request.proxy = &my_packet;
3084 my_packet.dst_ipaddr = home->ipaddr;
3085 my_packet.src_ipaddr = home->src_ipaddr;
3087 exec_trigger(&my_request, home->cs, trigger, false);
3090 static void mark_home_server_zombie(home_server_t *home, struct timeval *now, struct timeval *response_window)
3097 rad_assert((home->state == HOME_STATE_ALIVE) ||
3098 (home->state == HOME_STATE_UNKNOWN));
3101 if (home->proto == IPPROTO_TCP) {
3102 WARN("Not marking TCP server %s zombie", home->name);
3108 * We've received a real packet recently. Don't mark the
3109 * server as zombie until we've received NO packets for a
3110 * while. The "1/4" of zombie period was chosen rather
3111 * arbitrarily. It's a balance between too short, which
3112 * gives quick fail-over and fail-back, or too long,
3113 * where the proxy still sends packets to an unresponsive
3116 start = now->tv_sec - ((home->zombie_period + 3) / 4);
3117 if (home->last_packet_recv >= start) {
3118 DEBUG("Recieved reply from home server %d seconds ago. Might not be zombie.",
3119 (int) (now->tv_sec - home->last_packet_recv));
3123 home->state = HOME_STATE_ZOMBIE;
3124 home_trigger(home, "home_server.zombie");
3127 * Set the home server to "zombie", as of the time
3130 home->zombie_period_start.tv_sec = start;
3131 home->zombie_period_start.tv_usec = USEC / 2;
3133 fr_event_delete(el, &home->ev);
3134 home->num_sent_pings = 0;
3135 home->num_received_pings = 0;
3137 PROXY( "Marking home server %s port %d as zombie (it has not responded in %d.%06d seconds).",
3138 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
3139 buffer, sizeof(buffer)),
3140 home->port, (int) response_window->tv_sec, (int) response_window->tv_usec);
3142 ping_home_server(home);
3146 void revive_home_server(void *ctx)
3148 home_server_t *home = ctx;
3152 rad_assert(home->proto != IPPROTO_TCP);
3155 home->state = HOME_STATE_ALIVE;
3156 home_trigger(home, "home_server.alive");
3157 home->currently_outstanding = 0;
3158 gettimeofday(&home->revive_time, NULL);
3161 * Delete any outstanding events.
3163 if (home->ev) fr_event_delete(el, &home->ev);
3165 PROXY( "Marking home server %s port %d alive again... we have no idea if it really is alive or not.",
3166 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
3167 buffer, sizeof(buffer)),
3171 void mark_home_server_dead(home_server_t *home, struct timeval *when)
3173 int previous_state = home->state;
3177 if (home->proto == IPPROTO_TCP) {
3178 WARN("Not marking TCP server dead");
3183 PROXY( "Marking home server %s port %d as dead.",
3184 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
3185 buffer, sizeof(buffer)),
3188 home->state = HOME_STATE_IS_DEAD;
3189 home_trigger(home, "home_server.dead");
3191 if (home->ping_check != HOME_PING_CHECK_NONE) {
3193 * If the control socket marks us dead, start
3194 * pinging. Otherwise, we already started
3195 * pinging when it was marked "zombie".
3197 if (previous_state == HOME_STATE_ALIVE) {
3198 ping_home_server(home);
3200 DEBUG("PING: Already pinging home server %s",
3206 * Revive it after a fixed period of time. This
3207 * is very, very, bad.
3210 home->when.tv_sec += home->revive_interval;
3212 DEBUG("PING: Reviving home server %s in %u seconds",
3213 home->name, home->revive_interval);
3214 INSERT_EVENT(revive_home_server, home);
3218 STATE_MACHINE_DECL(proxy_wait_for_reply)
3220 struct timeval now, when;
3221 struct timeval *response_window = NULL;
3222 home_server_t *home = request->home_server;
3225 TRACE_STATE_MACHINE;
3227 rad_assert(request->packet->code != PW_CODE_STATUS_SERVER);
3228 rad_assert(request->home_server != NULL);
3230 if (request->master_state == REQUEST_STOP_PROCESSING) {
3231 request->child_state = REQUEST_DONE;
3235 gettimeofday(&now, NULL);
3240 * We have a reply, ignore the retransmit.
3242 if (request->proxy_reply) return;
3245 * The request was proxied to a virtual server.
3246 * Ignore the retransmit.
3248 if (request->home_server->server) return;
3250 if ((home->state == HOME_STATE_IS_DEAD) ||
3251 !request->proxy_listener ||
3252 (request->proxy_listener->status != RAD_LISTEN_STATUS_KNOWN)) {
3253 request_proxy_anew(request);
3258 if (home->proto == IPPROTO_TCP) {
3259 DEBUG2("Suppressing duplicate proxied request (tcp) to home server %s port %d proto TCP - ID: %d",
3260 inet_ntop(request->proxy->dst_ipaddr.af,
3261 &request->proxy->dst_ipaddr.ipaddr,
3262 buffer, sizeof(buffer)),
3263 request->proxy->dst_port,
3264 request->proxy->id);
3270 * More than one retransmit a second is stupid,
3271 * and should be suppressed by the proxy.
3273 when = request->proxy_retransmit;
3276 if (timercmp(&now, &when, <)) {
3277 DEBUG2("Suppressing duplicate proxied request (too fast) to home server %s port %d proto TCP - ID: %d",
3278 inet_ntop(request->proxy->dst_ipaddr.af,
3279 &request->proxy->dst_ipaddr.ipaddr,
3280 buffer, sizeof(buffer)),
3281 request->proxy->dst_port,
3282 request->proxy->id);
3286 #ifdef WITH_ACCOUNTING
3288 * If we update the Acct-Delay-Time, we need to
3291 if ((request->packet->code == PW_CODE_ACCOUNTING_REQUEST) &&
3292 pairfind(request->proxy->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY)) {
3293 request_proxy_anew(request);
3298 RDEBUG2("Sending duplicate proxied request to home server %s port %d - ID: %d",
3299 inet_ntop(request->proxy->dst_ipaddr.af,
3300 &request->proxy->dst_ipaddr.ipaddr,
3301 buffer, sizeof(buffer)),
3302 request->proxy->dst_port,
3303 request->proxy->id);
3304 request->num_proxied_requests++;
3306 rad_assert(request->proxy_listener != NULL);;
3307 DEBUG_PACKET(request, request->proxy, 1);
3308 FR_STATS_TYPE_INC(home->stats.total_requests);
3309 home->last_packet_sent = now.tv_sec;
3310 request->proxy_retransmit = now;
3311 request->proxy_listener->send(request->proxy_listener,
3315 case FR_ACTION_TIMER:
3316 response_window = request_response_window(request);
3319 if (!request->proxy_listener ||
3320 (request->proxy_listener->status != RAD_LISTEN_STATUS_KNOWN)) {
3321 remove_from_proxy_hash(request);
3323 when = request->packet->timestamp;
3324 when.tv_sec += request->root->max_request_time;
3326 if (timercmp(&when, &now, >)) {
3327 RDEBUG("Waiting for client retransmission in order to do a proxy retransmit");
3328 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3335 * Wake up "response_window" time in the future.
3336 * i.e. when MY packet hasn't received a response.
3338 * Note that we DO NOT mark the home server as
3339 * zombie if it doesn't respond to us. It may be
3340 * responding to other (better looking) packets.
3342 when = request->proxy->timestamp;
3343 timeradd(&when, response_window, &when);
3346 * Not at the response window. Set the timer for
3349 if (timercmp(&when, &now, >)) {
3350 RDEBUG("Expecting proxy response no later than %d.%06d seconds from now",
3351 (int) response_window->tv_sec, (int) response_window->tv_usec);
3352 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3357 RDEBUG("No proxy response, giving up on request and marking it done");
3360 * If we haven't received any packets for
3361 * "response_window", then mark the home server
3364 * If the connection is TCP, then another
3365 * "watchdog timer" function takes care of pings,
3366 * etc. So we don't need to do it here.
3368 * This check should really be part of a home
3369 * server state machine.
3371 if (((home->state == HOME_STATE_ALIVE) ||
3372 (home->state == HOME_STATE_UNKNOWN))
3374 && (home->proto != IPPROTO_TCP)
3377 mark_home_server_zombie(home, &now, response_window);
3380 FR_STATS_TYPE_INC(home->stats.total_timeouts);
3381 if (home->type == HOME_TYPE_AUTH) {
3382 if (request->proxy_listener) FR_STATS_TYPE_INC(request->proxy_listener->stats.total_timeouts);
3383 FR_STATS_TYPE_INC(proxy_auth_stats.total_timeouts);
3386 else if (home->type == HOME_TYPE_ACCT) {
3387 if (request->proxy_listener) FR_STATS_TYPE_INC(request->proxy_listener->stats.total_timeouts);
3388 FR_STATS_TYPE_INC(proxy_acct_stats.total_timeouts);
3393 * There was no response within the window. Stop
3394 * the request. If the client retransmitted, it
3395 * may have failed over to another home server.
3396 * But that one may be dead, too.
3398 RERROR("Failing proxied request, due to lack of any response from home server %s port %d",
3399 inet_ntop(request->proxy->dst_ipaddr.af,
3400 &request->proxy->dst_ipaddr.ipaddr,
3401 buffer, sizeof(buffer)),
3402 request->proxy->dst_port);
3404 if (setup_post_proxy_fail(request)) {
3405 request_queue_or_run(request, proxy_no_reply);
3407 gettimeofday(&request->reply->timestamp, NULL);
3408 request_cleanup_delay_init(request, NULL);
3413 * Duplicate proxy replies have been quenched by
3414 * now. This state is only called ONCE, when we
3415 * receive a new reply from the home server.
3417 case FR_ACTION_PROXY_REPLY:
3418 request_queue_or_run(request, proxy_running);
3421 case FR_ACTION_CONFLICTING:
3422 request_done(request, action);
3426 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
3430 #endif /* WITH_PROXY */
3432 /***********************************************************************
3436 ***********************************************************************/
3438 static int null_handler(UNUSED REQUEST *request)
3444 * See if we need to originate a CoA request.
3446 static void request_coa_originate(REQUEST *request)
3448 int rcode, pre_proxy_type = 0;
3454 rad_assert(request != NULL);
3455 rad_assert(request->coa != NULL);
3456 rad_assert(request->proxy == NULL);
3457 rad_assert(!request->in_proxy_hash);
3458 rad_assert(request->proxy_reply == NULL);
3461 * Check whether we want to originate one, or cancel one.
3463 vp = pairfind(request->config_items, PW_SEND_COA_REQUEST, 0, TAG_ANY);
3465 vp = pairfind(request->coa->proxy->vps, PW_SEND_COA_REQUEST, 0, TAG_ANY);
3469 if (vp->vp_integer == 0) {
3471 request_free(&request->coa);
3479 * src_ipaddr will be set up in proxy_encode.
3481 memset(&ipaddr, 0, sizeof(ipaddr));
3482 vp = pairfind(coa->proxy->vps, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY);
3484 ipaddr.af = AF_INET;
3485 ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
3487 } else if ((vp = pairfind(coa->proxy->vps, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY)) != NULL) {
3488 ipaddr.af = AF_INET6;
3489 ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
3491 } else if ((vp = pairfind(coa->proxy->vps, PW_HOME_SERVER_POOL, 0, TAG_ANY)) != NULL) {
3492 coa->home_pool = home_pool_byname(vp->vp_strvalue,
3494 if (!coa->home_pool) {
3495 RWDEBUG2("No such home_server_pool %s",
3501 * Prefer the pool to one server
3503 } else if (request->client->coa_pool) {
3504 coa->home_pool = request->client->coa_pool;
3506 } else if (request->client->coa_server) {
3507 coa->home_server = request->client->coa_server;
3511 * If all else fails, send it to the client that
3512 * originated this request.
3514 memcpy(&ipaddr, &request->packet->src_ipaddr, sizeof(ipaddr));
3518 * Use the pool, if it exists.
3520 if (coa->home_pool) {
3521 coa->home_server = home_server_ldb(NULL, coa->home_pool, coa);
3522 if (!coa->home_server) {
3523 RWDEBUG("No live home server for home_server_pool %s", coa->home_pool->name);
3526 home_server_update_request(coa->home_server, coa);
3528 } else if (!coa->home_server) {
3529 uint16_t port = PW_COA_UDP_PORT;
3531 vp = pairfind(coa->proxy->vps, PW_PACKET_DST_PORT, 0, TAG_ANY);
3532 if (vp) port = vp->vp_integer;
3534 coa->home_server = home_server_find(&ipaddr, port, IPPROTO_UDP);
3535 if (!coa->home_server) {
3536 RWDEBUG2("Unknown destination %s:%d for CoA request.",
3537 inet_ntop(ipaddr.af, &ipaddr.ipaddr,
3538 buffer, sizeof(buffer)), port);
3543 vp = pairfind(coa->proxy->vps, PW_PACKET_TYPE, 0, TAG_ANY);
3545 switch (vp->vp_integer) {
3546 case PW_CODE_COA_REQUEST:
3547 case PW_CODE_DISCONNECT_REQUEST:
3548 coa->proxy->code = vp->vp_integer;
3552 DEBUG("Cannot set CoA Packet-Type to code %d",
3558 if (!coa->proxy->code) coa->proxy->code = PW_CODE_COA_REQUEST;
3561 * The rest of the server code assumes that
3562 * request->packet && request->reply exist. Copy them
3563 * from the original request.
3565 rad_assert(coa->packet != NULL);
3566 rad_assert(coa->packet->vps == NULL);
3568 coa->packet = rad_copy_packet(coa, request->packet);
3569 coa->reply = rad_copy_packet(coa, request->reply);
3571 coa->config_items = paircopy(coa, request->config_items);
3572 coa->num_coa_requests = 0;
3573 coa->handle = null_handler;
3574 coa->number = request->number; /* it's associated with the same request */
3577 * Call the pre-proxy routines.
3579 vp = pairfind(request->config_items, PW_PRE_PROXY_TYPE, 0, TAG_ANY);
3581 DICT_VALUE const *dval = dict_valbyattr(vp->da->attr, vp->da->vendor, vp->vp_integer);
3582 /* Must be a validation issue */
3584 RDEBUG2("Found Pre-Proxy-Type %s", dval->name);
3585 pre_proxy_type = vp->vp_integer;
3588 if (coa->home_pool && coa->home_pool->virtual_server) {
3589 char const *old_server = coa->server;
3591 coa->server = coa->home_pool->virtual_server;
3592 RDEBUG2("server %s {", coa->server);
3594 rcode = process_pre_proxy(pre_proxy_type, coa);
3597 coa->server = old_server;
3599 rcode = process_pre_proxy(pre_proxy_type, coa);
3606 * Only send the CoA packet if the pre-proxy code succeeded.
3608 case RLM_MODULE_NOOP:
3610 case RLM_MODULE_UPDATED:
3615 * Source IP / port is set when the proxy socket
3618 coa->proxy->dst_ipaddr = coa->home_server->ipaddr;
3619 coa->proxy->dst_port = coa->home_server->port;
3621 if (!insert_into_proxy_hash(coa)) {
3622 radlog_request(L_PROXY, 0, coa, "Failed to insert CoA request into proxy list");
3627 * We CANNOT divorce the CoA request from the parent
3628 * request. This function is running in a child thread,
3629 * and we need access to the main event loop in order to
3630 * to add the timers for the CoA packet.
3632 * Instead, we wait for the timer on the parent request
3635 gettimeofday(&coa->proxy->timestamp, NULL);
3636 coa->packet->timestamp = coa->proxy->timestamp; /* for max_request_time */
3637 coa->delay = 0; /* need to calculate a new delay */
3639 DEBUG_PACKET(coa, coa->proxy, 1);
3641 coa->process = coa_wait_for_reply;
3642 #ifdef DEBUG_STATE_MACHINE
3643 if (debug_flag) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n", request->number, __FUNCTION__,
3644 child_state_names[request->child_state],
3645 child_state_names[REQUEST_RUNNING]);
3647 #ifdef HAVE_PTHREAD_H
3648 coa->child_pid = NO_SUCH_CHILD_PID;
3650 coa->child_state = REQUEST_PROXIED;
3651 rad_assert(coa->proxy_reply == NULL);
3652 FR_STATS_TYPE_INC(coa->home_server->stats.total_requests);
3653 coa->home_server->last_packet_sent = coa->proxy->timestamp.tv_sec;
3654 coa->proxy_listener->send(coa->proxy_listener, coa);
3658 static void coa_timer(REQUEST *request)
3660 uint32_t delay, frac;
3661 struct timeval now, when, mrd;
3663 rad_assert(request->parent == NULL);
3665 if (request->proxy_reply) return request_process_timer(request);
3667 gettimeofday(&now, NULL);
3669 if (request->delay == 0) {
3671 * Implement re-transmit algorithm as per RFC 5080
3674 * We want IRT + RAND*IRT
3675 * or 0.9 IRT + rand(0,.2) IRT
3677 * 2^20 ~ USEC, and we want 2.
3678 * rand(0,0.2) USEC ~ (rand(0,2^21) / 10)
3680 delay = (fr_rand() & ((1 << 22) - 1)) / 10;
3681 request->delay = delay * request->home_server->coa_irt;
3682 delay = request->home_server->coa_irt * USEC;
3683 delay -= delay / 10;
3684 delay += request->delay;
3685 request->delay = delay;
3687 when = request->proxy->timestamp;
3688 tv_add(&when, delay);
3690 if (timercmp(&when, &now, >)) {
3691 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3697 * Retransmit CoA request.
3701 * Cap count at MRC, if it is non-zero.
3703 if (request->home_server->coa_mrc &&
3704 (request->num_coa_requests >= request->home_server->coa_mrc)) {
3707 RERROR("Failing request - originate-coa ID %u, due to lack of any response from coa server %s port %d",
3709 inet_ntop(request->proxy->dst_ipaddr.af,
3710 &request->proxy->dst_ipaddr.ipaddr,
3711 buffer, sizeof(buffer)),
3712 request->proxy->dst_port);
3714 if (setup_post_proxy_fail(request)) {
3715 request_queue_or_run(request, coa_no_reply);
3717 request_done(request, FR_ACTION_DONE);
3723 * RFC 5080 Section 2.2.1
3725 * RT = 2*RTprev + RAND*RTprev
3726 * = 1.9 * RTprev + rand(0,.2) * RTprev
3727 * = 1.9 * RTprev + rand(0,1) * (RTprev / 5)
3730 delay ^= (delay >> 16);
3732 frac = request->delay / 5;
3733 delay = ((frac >> 16) * delay) + (((frac & 0xffff) * delay) >> 16);
3735 delay += (2 * request->delay) - (request->delay / 10);
3738 * Cap delay at MRT, if MRT is non-zero.
3740 if (request->home_server->coa_mrt &&
3741 (delay > (request->home_server->coa_mrt * USEC))) {
3742 int mrt_usec = request->home_server->coa_mrt * USEC;
3745 * delay = MRT + RAND * MRT
3746 * = 0.9 MRT + rand(0,.2) * MRT
3749 delay ^= (delay >> 15);
3751 delay = ((mrt_usec >> 16) * delay) + (((mrt_usec & 0xffff) * delay) >> 16);
3752 delay += mrt_usec - (mrt_usec / 10);
3755 request->delay = delay;
3757 tv_add(&when, request->delay);
3758 mrd = request->proxy->timestamp;
3759 mrd.tv_sec += request->home_server->coa_mrd;
3762 * Cap duration at MRD.
3764 if (timercmp(&mrd, &when, <)) {
3767 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3769 request->num_coa_requests++; /* is NOT reset by code 3 lines above! */
3771 FR_STATS_TYPE_INC(request->home_server->stats.total_requests);
3774 * Status servers don't count as real packets sent.
3776 request->proxy_listener->send(request->proxy_listener,
3780 STATE_MACHINE_DECL(coa_wait_for_reply)
3782 rad_assert(request->parent == NULL);
3784 TRACE_STATE_MACHINE;
3787 case FR_ACTION_TIMER:
3789 * This is big enough to be in it's own function.
3794 case FR_ACTION_PROXY_REPLY:
3795 rad_assert(request->parent == NULL);
3796 request_queue_or_run(request, coa_running);
3800 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
3805 static void request_coa_separate(REQUEST *request)
3807 #ifdef DEBUG_STATE_MACHINE
3808 int action = FR_ACTION_TIMER;
3810 TRACE_STATE_MACHINE;
3812 rad_assert(request->parent != NULL);
3813 rad_assert(request->parent->coa == request);
3814 rad_assert(request->ev == NULL);
3815 rad_assert(!request->in_request_hash);
3816 rad_assert(request->coa == NULL);
3818 rad_assert(request->proxy_listener != NULL);
3820 (void) talloc_steal(NULL, request);
3821 request->parent->coa = NULL;
3822 request->parent = NULL;
3825 * Should be coa_wait_for_reply()
3827 request->process(request, FR_ACTION_TIMER);
3830 STATE_MACHINE_DECL(coa_no_reply)
3834 TRACE_STATE_MACHINE;
3837 case FR_ACTION_TIMER:
3838 request_common(request, action);
3841 case FR_ACTION_PROXY_REPLY: /* too late! */
3842 RDEBUG2("Reply from CoA server %s port %d - ID: %d arrived too late.",
3843 inet_ntop(request->proxy->src_ipaddr.af,
3844 &request->proxy->src_ipaddr.ipaddr,
3845 buffer, sizeof(buffer)),
3846 request->proxy->dst_port, request->proxy->id);
3851 * FIXME: do recv_coa Fail
3853 (void) process_proxy_reply(request, NULL);
3854 request_done(request, FR_ACTION_DONE);
3858 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
3863 STATE_MACHINE_DECL(coa_running)
3865 TRACE_STATE_MACHINE;
3868 case FR_ACTION_TIMER:
3869 request_process_timer(request);
3872 case FR_ACTION_PROXY_REPLY:
3873 request_common(request, action);
3877 if (process_proxy_reply(request, request->proxy_reply)) {
3878 request->handle(request);
3879 request_finish(request, action);
3881 request_done(request, FR_ACTION_DONE);
3886 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
3890 #endif /* WITH_COA */
3892 /***********************************************************************
3894 * End of the State machine. Start of additional helper code.
3896 ***********************************************************************/
3898 /***********************************************************************
3902 ***********************************************************************/
3903 static void event_socket_handler(UNUSED fr_event_list_t *xel, UNUSED int fd, void *ctx)
3905 rad_listen_t *listener = ctx;
3907 rad_assert(xel == el);
3911 (listener->type != RAD_LISTEN_DETAIL) &&
3913 (listener->fd < 0)) {
3916 listener->print(listener, buffer, sizeof(buffer));
3917 ERROR("FATAL: Asked to read from closed socket: %s",
3920 rad_panic("Socket was closed on us!");
3924 listener->recv(listener);
3928 #ifdef WITH_DETAIL_THREAD
3931 * This function is called periodically to see if this detail
3932 * file is available for reading.
3934 static void event_poll_detail(void *ctx)
3937 rad_listen_t *this = ctx;
3938 struct timeval when, now;
3939 listen_detail_t *detail = this->data;
3941 rad_assert(this->type == RAD_LISTEN_DETAIL);
3944 event_socket_handler(el, this->fd, this);
3946 fr_event_now(el, &now);
3950 * Backdoor API to get the delay until the next poll
3953 delay = this->encode(this, NULL);
3954 if (delay == 0) goto redo;
3956 tv_add(&when, delay);
3958 if (!fr_event_insert(el, event_poll_detail, this,
3959 &when, &detail->ev)) {
3960 ERROR("Failed creating handler");
3964 #endif /* WITH_DETAIL_THREAD */
3965 #endif /* WITH_DETAIL */
3967 static void event_status(struct timeval *wake)
3969 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
3973 if (debug_flag == 0) {
3975 INFO("Ready to process requests");
3976 just_started = false;
3982 INFO("Ready to process requests");
3984 } else if ((wake->tv_sec != 0) ||
3985 (wake->tv_usec >= 100000)) {
3986 DEBUG("Waking up in %d.%01u seconds.",
3987 (int) wake->tv_sec, (unsigned int) wake->tv_usec / 100000);
3992 * FIXME: Put this somewhere else, where it isn't called
3993 * all of the time...
3996 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
3998 * If there are no child threads, then there may
3999 * be child processes. In that case, wait for
4000 * their exit status, and throw that exit status
4001 * away. This helps get rid of zxombie children.
4003 while (waitpid(-1, &argval, WNOHANG) > 0) {
4011 static void listener_free_cb(void *ctx)
4013 rad_listen_t *this = ctx;
4016 if (this->count > 0) {
4017 struct timeval when;
4018 listen_socket_t *sock = this->data;
4020 fr_event_now(el, &when);
4023 if (!fr_event_insert(el, listener_free_cb, this, &when,
4025 rad_panic("Failed to insert event");
4032 * It's all free, close the socket.
4035 this->print(this, buffer, sizeof(buffer));
4036 DEBUG("... cleaning up socket %s", buffer);
4042 static int proxy_eol_cb(void *ctx, void *data)
4044 struct timeval when;
4045 REQUEST *request = fr_packet2myptr(REQUEST, proxy, data);
4047 if (request->proxy_listener != ctx) return 0;
4050 * We don't care if it's being processed in a child thread.
4053 #ifdef WITH_ACCOUNTING
4055 * Accounting packets should be deleted immediately.
4056 * They will never be retransmitted by the client.
4058 if (request->proxy->code == PW_CODE_ACCOUNTING_REQUEST) {
4059 RDEBUG("Stopping request due to failed connection to home server");
4060 request->master_state = REQUEST_STOP_PROCESSING;
4065 * Reset the timer to be now, so that the request is
4066 * quickly updated. But spread the requests randomly
4067 * over the next second, so that we don't overload the
4070 fr_event_now(el, &when);
4071 tv_add(&when, fr_rand() % USEC);
4072 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
4075 * Don't delete it from the list.
4081 static int event_new_fd(rad_listen_t *this)
4087 if (this->status == RAD_LISTEN_STATUS_KNOWN) return 1;
4089 this->print(this, buffer, sizeof(buffer));
4091 if (this->status == RAD_LISTEN_STATUS_INIT) {
4092 listen_socket_t *sock = this->data;
4095 DEBUG("Listening on %s", buffer);
4097 INFO(" ... adding new socket %s", buffer);
4100 switch (this->type) {
4103 * Detail files are always known, and aren't
4104 * put into the socket event loop.
4106 case RAD_LISTEN_DETAIL:
4107 this->status = RAD_LISTEN_STATUS_KNOWN;
4109 #ifndef WITH_DETAIL_THREAD
4111 * Set up the first poll interval.
4113 event_poll_detail(this);
4116 break; /* add the FD to the list */
4118 #endif /* WITH_DETAIL */
4122 * Add it to the list of sockets we can use.
4123 * Server sockets (i.e. auth/acct) are never
4124 * added to the packet list.
4126 case RAD_LISTEN_PROXY:
4129 * Add timers to outgoing child sockets, if necessary.
4131 if (sock->proto == IPPROTO_TCP && sock->opened &&
4132 (sock->home->limit.lifetime || sock->home->limit.idle_timeout)) {
4133 struct timeval when;
4135 when.tv_sec = sock->opened + 1;
4138 if (!fr_event_insert(el, tcp_socket_timer, this, &when,
4140 rad_panic("Failed to insert event");
4145 #endif /* WITH_PROXY */
4148 * FIXME: put idle timers on command sockets.
4154 * Add timers to incoming child sockets, if necessary.
4156 if (sock->proto == IPPROTO_TCP && sock->opened &&
4157 (sock->limit.lifetime || sock->limit.idle_timeout)) {
4158 struct timeval when;
4160 when.tv_sec = sock->opened + 1;
4163 if (!fr_event_insert(el, tcp_socket_timer, this, &when,
4165 rad_panic("Failed to insert event");
4170 } /* switch over listener types */
4173 * All sockets: add the FD to the event handler.
4175 if (!fr_event_fd_insert(el, 0, this->fd,
4176 event_socket_handler, this)) {
4177 ERROR("Failed adding event handler for socket!");
4181 this->status = RAD_LISTEN_STATUS_KNOWN;
4187 * Stop using this socket, if at all possible.
4189 if (this->status == RAD_LISTEN_STATUS_EOL) {
4191 * Remove it from the list of live FD's.
4193 fr_event_fd_delete(el, 0, this->fd);
4197 * Proxy sockets get frozen, so that we don't use
4198 * them for new requests. But we do keep them
4199 * open to listen for replies to requests we had
4202 if (this->type == RAD_LISTEN_PROXY) {
4203 PTHREAD_MUTEX_LOCK(&proxy_mutex);
4204 if (!fr_packet_list_socket_freeze(proxy_list,
4206 ERROR("Fatal error freezing socket: %s", fr_strerror());
4210 fr_packet_list_walk(proxy_list, this, proxy_eol_cb);
4211 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
4216 * Requests are still using the socket. Wait for
4219 if (this->count > 0) {
4220 struct timeval when;
4221 listen_socket_t *sock = this->data;
4224 * Try again to clean up the socket in 30
4227 gettimeofday(&when, NULL);
4230 if (!fr_event_insert(el,
4231 (fr_event_callback_t) event_new_fd,
4232 this, &when, &sock->ev)) {
4233 rad_panic("Failed to insert event");
4240 * No one is using the socket. We can remove it now.
4242 this->status = RAD_LISTEN_STATUS_REMOVE_NOW;
4243 } /* socket is at EOL */
4249 if (this->status == RAD_LISTEN_STATUS_REMOVE_NOW) {
4252 listen_socket_t *sock = this->data;
4254 struct timeval when;
4257 * Re-open the socket, pointing it to /dev/null.
4258 * This means that all writes proceed without
4259 * blocking, and all reads return "no data".
4261 * This leaves the socket active, so any child
4262 * threads won't go insane. But it means that
4263 * they cannot send or receive any packets.
4265 * This is EXTRA work in the normal case, when
4266 * sockets are closed without error. But it lets
4267 * us have one simple processing method for all
4270 devnull = open("/dev/null", O_RDWR);
4272 ERROR("FATAL failure opening /dev/null: %s",
4273 fr_syserror(errno));
4276 if (dup2(devnull, this->fd) < 0) {
4277 ERROR("FATAL failure closing socket: %s",
4278 fr_syserror(errno));
4284 rad_assert(this->type != RAD_LISTEN_DETAIL);
4288 INFO(" ... shutting down socket %s", buffer);
4292 * The socket is dead. Force all proxied packets
4293 * to stop using it. And then remove it from the
4294 * list of outgoing sockets.
4296 if (this->type == RAD_LISTEN_PROXY) {
4297 PTHREAD_MUTEX_LOCK(&proxy_mutex);
4298 fr_packet_list_walk(proxy_list, this, eol_proxy_listener);
4300 if (!fr_packet_list_socket_del(proxy_list, this->fd)) {
4301 ERROR("Fatal error removing socket %s: %s",
4302 buffer, fr_strerror());
4305 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
4310 * EOL all requests using this socket.
4312 fr_packet_list_walk(pl, this, eol_listener);
4316 * No child threads, clean it up now.
4319 if (sock->ev) fr_event_delete(el, &sock->ev);
4325 * Wait until all requests using this socket are done.
4327 gettimeofday(&when, NULL);
4330 if (!fr_event_insert(el, listener_free_cb, this, &when,
4332 rad_panic("Failed to insert event");
4335 #endif /* WITH_TCP */
4340 /***********************************************************************
4344 ***********************************************************************/
4346 static void handle_signal_self(int flag)
4350 if ((flag & (RADIUS_SIGNAL_SELF_EXIT | RADIUS_SIGNAL_SELF_TERM)) != 0) {
4351 if ((flag & RADIUS_SIGNAL_SELF_EXIT) != 0) {
4352 INFO("Signalled to exit");
4353 fr_event_loop_exit(el, 1);
4355 INFO("Signalled to terminate");
4356 exec_trigger(NULL, NULL, "server.signal.term", true);
4357 fr_event_loop_exit(el, 2);
4361 } /* else exit/term flags weren't set */
4364 * Tell the even loop to stop processing.
4366 if ((flag & RADIUS_SIGNAL_SELF_HUP) != 0) {
4368 static time_t last_hup = 0;
4371 if ((int) (when - last_hup) < 5) {
4372 INFO("Ignoring HUP (less than 5s since last one)");
4376 INFO("Received HUP signal");
4380 exec_trigger(NULL, NULL, "server.signal.hup", true);
4381 fr_event_loop_exit(el, 0x80);
4385 #ifndef WITH_DETAIL_THREAD
4386 if ((flag & RADIUS_SIGNAL_SELF_DETAIL) != 0) {
4390 * FIXME: O(N) loops suck.
4392 for (this = main_config.listen;
4394 this = this->next) {
4395 if (this->type != RAD_LISTEN_DETAIL) continue;
4398 * This one didn't send the signal, skip
4401 if (!this->decode(this, NULL)) continue;
4404 * Go service the interrupt.
4406 event_poll_detail(this);
4414 #ifdef HAVE_PTHREAD_H
4416 * There are new listeners in the list. Run
4417 * event_new_fd() on them.
4419 if ((flag & RADIUS_SIGNAL_SELF_NEW_FD) != 0) {
4420 rad_listen_t *this, *next;
4422 FD_MUTEX_LOCK(&fd_mutex);
4425 * FIXME: unlock the mutex before calling
4428 for (this = new_listeners; this != NULL; this = next) {
4435 new_listeners = NULL;
4436 FD_MUTEX_UNLOCK(&fd_mutex);
4438 #endif /* HAVE_PTHREAD_H */
4439 #endif /* WITH_PROXY */
4440 #endif /* WITH_TCP */
4443 #ifndef HAVE_PTHREAD_H
4444 void radius_signal_self(int flag)
4446 return handle_signal_self(flag);
4450 static int self_pipe[2] = { -1, -1 };
4453 * Inform ourselves that we received a signal.
4455 void radius_signal_self(int flag)
4461 * The read MUST be non-blocking for this to work.
4463 rcode = read(self_pipe[0], buffer, sizeof(buffer));
4467 for (i = 0; i < rcode; i++) {
4468 buffer[0] |= buffer[i];
4476 if (write(self_pipe[1], buffer, 1) < 0) fr_exit(0);
4480 static void event_signal_handler(UNUSED fr_event_list_t *xel,
4481 UNUSED int fd, UNUSED void *ctx)
4486 rcode = read(self_pipe[0], buffer, sizeof(buffer));
4487 if (rcode <= 0) return;
4490 * Merge pending signals.
4492 for (i = 0; i < rcode; i++) {
4493 buffer[0] |= buffer[i];
4496 handle_signal_self(buffer[0]);
4498 #endif /* HAVE_PTHREAD_H */
4500 /***********************************************************************
4502 * Bootstrapping code.
4504 ***********************************************************************/
4507 * Externally-visibly functions.
4509 int radius_event_init(TALLOC_CTX *ctx) {
4510 el = fr_event_list_create(ctx, event_status);
4516 int radius_event_start(CONF_SECTION *cs, bool have_children)
4518 rad_listen_t *head = NULL;
4520 if (fr_start_time != (time_t)-1) return 0;
4522 time(&fr_start_time);
4525 * radius_event_init() must be called first
4528 if (fr_start_time == (time_t)-1) return 0;
4530 pl = fr_packet_list_create(0);
4531 if (!pl) return 0; /* leak el */
4533 request_num_counter = 0;
4536 if (main_config.proxy_requests) {
4538 * Create the tree for managing proxied requests and
4541 proxy_list = fr_packet_list_create(1);
4542 if (!proxy_list) return 0;
4544 #ifdef HAVE_PTHREAD_H
4545 if (pthread_mutex_init(&proxy_mutex, NULL) != 0) {
4546 ERROR("FATAL: Failed to initialize proxy mutex: %s",
4547 fr_syserror(errno));
4555 * Move all of the thread calls to this file?
4557 * It may be best for the mutexes to be in this file...
4559 spawn_flag = have_children;
4561 #ifdef HAVE_PTHREAD_H
4562 NO_SUCH_CHILD_PID = pthread_self(); /* not a child thread */
4565 * Initialize the threads ONLY if we're spawning, AND
4566 * we're running normally.
4568 if (have_children && !check_config &&
4569 (thread_pool_init(cs, &spawn_flag) < 0)) {
4575 DEBUG("%s: #### Skipping IP addresses and Ports ####",
4577 if (listen_init(cs, &head, spawn_flag) < 0) {
4584 #ifdef HAVE_PTHREAD_H
4586 * Child threads need a pipe to signal us, as do the
4589 if (pipe(self_pipe) < 0) {
4590 ERROR("radiusd: Error opening internal pipe: %s",
4591 fr_syserror(errno));
4594 if ((fcntl(self_pipe[0], F_SETFL, O_NONBLOCK) < 0) ||
4595 (fcntl(self_pipe[0], F_SETFD, FD_CLOEXEC) < 0)) {
4596 ERROR("radiusd: Error setting internal flags: %s",
4597 fr_syserror(errno));
4600 if ((fcntl(self_pipe[1], F_SETFL, O_NONBLOCK) < 0) ||
4601 (fcntl(self_pipe[1], F_SETFD, FD_CLOEXEC) < 0)) {
4602 ERROR("radiusd: Error setting internal flags: %s",
4603 fr_syserror(errno));
4607 if (!fr_event_fd_insert(el, 0, self_pipe[0],
4608 event_signal_handler, el)) {
4609 ERROR("Failed creating handler for signals");
4614 DEBUG("%s: #### Opening IP addresses and Ports ####",
4618 * The server temporarily switches to an unprivileged
4619 * user very early in the bootstrapping process.
4620 * However, some sockets MAY require privileged access
4621 * (bind to device, or to port < 1024, or to raw
4622 * sockets). Those sockets need to call suid up/down
4623 * themselves around the functions that need a privileged
4626 if (listen_init(cs, &head, spawn_flag) < 0) {
4630 main_config.listen = head;
4633 * At this point, no one has any business *ever* going
4636 fr_suid_down_permanent();
4643 static int proxy_delete_cb(UNUSED void *ctx, void *data)
4645 REQUEST *request = fr_packet2myptr(REQUEST, proxy, data);
4647 request->master_state = REQUEST_STOP_PROCESSING;
4649 #ifdef HAVE_PTHREAD_H
4650 if (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0) return 0;
4654 * If it's queued we can't delete it from the queue.
4656 * Otherwise, it's OK to delete it. Even RUNNING, because
4657 * that will get caught by the check above.
4659 if (request->child_state == REQUEST_QUEUED) return 0;
4661 request->in_proxy_hash = false;
4663 if (!request->in_request_hash) {
4664 request_done(request, FR_ACTION_DONE);
4668 * Delete it from the list.
4675 static int request_delete_cb(UNUSED void *ctx, void *data)
4677 REQUEST *request = fr_packet2myptr(REQUEST, packet, data);
4679 request->master_state = REQUEST_STOP_PROCESSING;
4682 * Not done, or the child thread is still processing it.
4684 if (request->child_state < REQUEST_RESPONSE_DELAY) return 0; /* continue */
4686 #ifdef HAVE_PTHREAD_H
4687 if (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0) return 0;
4691 rad_assert(request->in_proxy_hash == false);
4694 request->in_request_hash = false;
4695 if (request->ev) fr_event_delete(el, &request->ev);
4697 if (main_config.memory_report) {
4698 RDEBUG2("Cleaning up request packet ID %u with timestamp +%d",
4699 request->packet->id,
4700 (unsigned int) (request->timestamp - fr_start_time));
4705 rad_assert(!request->coa->in_proxy_hash);
4709 request_free(&request);
4712 * Delete it from the list, and continue;
4718 void radius_event_free(void)
4724 * There are requests in the proxy hash that aren't
4725 * referenced from anywhere else. Remove them first.
4728 fr_packet_list_walk(proxy_list, NULL, proxy_delete_cb);
4732 fr_packet_list_walk(pl, NULL, request_delete_cb);
4736 * Now that all requests have been marked "please stop",
4737 * ensure that all of the threads have exited.
4739 #ifdef HAVE_PTHREAD_H
4744 * Walk the lists again, ensuring that all
4745 * requests are done.
4747 if (main_config.memory_report) {
4752 fr_packet_list_walk(proxy_list, NULL, proxy_delete_cb);
4753 num = fr_packet_list_num_elements(proxy_list);
4755 ERROR("Proxy list has %d requests still in it.", num);
4760 fr_packet_list_walk(pl, NULL, request_delete_cb);
4761 num = fr_packet_list_num_elements(pl);
4763 ERROR("Request list has %d requests still in it.", num);
4768 fr_packet_list_free(pl);
4772 fr_packet_list_free(proxy_list);
4778 if (debug_condition) talloc_free(debug_condition);
4781 int radius_event_process(void)
4785 return fr_event_loop(el);