2 * event.c Server event handling
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2007 The FreeRADIUS server project
21 * Copyright 2007 Alan DeKok <aland@deployingradius.com>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29 #include <freeradius-devel/event.h>
30 #include <freeradius-devel/detail.h>
32 #include <freeradius-devel/rad_assert.h>
37 #ifdef HAVE_SYS_WAIT_H
38 # include <sys/wait.h>
41 #define USEC (1000000)
43 extern pid_t radius_pid;
45 extern int check_config;
46 extern char *debug_condition;
49 * Ridiculous amounts of local state.
51 static fr_event_list_t *el = NULL;
52 static fr_packet_list_t *pl = NULL;
53 static int request_num_counter = 0;
54 static struct timeval now;
56 static int have_children;
57 static int just_started = TRUE;
61 #define WITH_SELF_PIPE (1)
66 static int self_pipe[2];
71 static pthread_mutex_t proxy_mutex;
72 static rad_listen_t *proxy_listener_list = NULL;
73 static int proxy_no_new_sockets = FALSE;
76 #define PTHREAD_MUTEX_LOCK if (have_children) pthread_mutex_lock
77 #define PTHREAD_MUTEX_UNLOCK if (have_children) pthread_mutex_unlock
79 static pthread_t NO_SUCH_CHILD_PID;
82 * This is easier than ifdef's throughout the code.
84 #define PTHREAD_MUTEX_LOCK(_x)
85 #define PTHREAD_MUTEX_UNLOCK(_x)
86 int thread_pool_addrequest(REQUEST *request, RAD_REQUEST_FUNP fun)
88 radius_handle_request(request, fun);
94 * We need mutexes around the event FD list *only* in certain
97 #if defined (HAVE_PTHREAD_H) && (defined(WITH_PROXY) || defined(WITH_TCP))
98 static pthread_mutex_t fd_mutex;
99 #define FD_MUTEX_LOCK if (have_children) pthread_mutex_lock
100 #define FD_MUTEX_UNLOCK if (have_children) pthread_mutex_unlock
103 * This is easier than ifdef's throughout the code.
105 #define FD_MUTEX_LOCK(_x)
106 #define FD_MUTEX_UNLOCK(_x)
110 #define INSERT_EVENT(_function, _ctx) if (!fr_event_insert(el, _function, _ctx, &((_ctx)->when), &((_ctx)->ev))) { _rad_panic(__FILE__, __LINE__, "Failed to insert event"); }
113 static fr_packet_list_t *proxy_list = NULL;
114 static void remove_from_proxy_hash(REQUEST *request);
116 static void check_for_zombie_home_server(REQUEST *request);
118 #define remove_from_proxy_hash(foo)
121 static void request_post_handler(REQUEST *request);
122 static void wait_a_bit(void *ctx);
123 static void event_socket_handler(fr_event_list_t *xel, UNUSED int fd, void *ctx);
125 static void event_poll_detail(void *ctx);
128 static void NEVER_RETURNS _rad_panic(const char *file, unsigned int line,
131 radlog(L_ERR, "[%s:%d] %s", file, line, msg);
135 #define rad_panic(x) _rad_panic(__FILE__, __LINE__, x)
138 static void tv_add(struct timeval *tv, int usec_delay)
140 if (usec_delay > USEC) {
141 tv->tv_sec += usec_delay / USEC;
144 tv->tv_usec += usec_delay;
146 if (tv->tv_usec > USEC) {
147 tv->tv_sec += tv->tv_usec / USEC;
152 static void remove_from_request_hash(REQUEST *request)
154 if (!request->in_request_hash) return;
156 fr_packet_list_yank(pl, request->packet);
157 request->in_request_hash = FALSE;
159 request_stats_final(request);
162 request->listener->count--;
166 static void ev_request_free(REQUEST **prequest)
170 if (!prequest || !*prequest) return;
177 * Divorce the child from the parent first,
178 * then clean up the child.
180 request->coa->parent = NULL;
181 ev_request_free(&request->coa);
185 * Divorce the parent from the child, and leave the
186 * parent still alive.
188 if (request->parent && (request->parent->coa == request)) {
189 request->parent->coa = NULL;
193 if (request->ev) fr_event_delete(el, &request->ev);
195 if (request->in_proxy_hash) remove_from_proxy_hash(request);
197 if (request->in_request_hash) remove_from_request_hash(request);
199 request_free(prequest);
203 static REQUEST *lookup_in_proxy_hash(RADIUS_PACKET *reply)
205 RADIUS_PACKET **proxy_p;
208 PTHREAD_MUTEX_LOCK(&proxy_mutex);
209 proxy_p = fr_packet_list_find_byreply(proxy_list, reply);
212 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
216 request = fr_packet2myptr(REQUEST, proxy, proxy_p);
217 request->num_proxied_responses++; /* needs to be protected by lock */
219 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
225 static void remove_from_proxy_hash(REQUEST *request)
228 * Check this without grabbing the mutex because it's a
229 * lot faster that way.
231 if (!request->in_proxy_hash) return;
234 * The "not in hash" flag is definitive. However, if the
235 * flag says that it IS in the hash, there might still be
236 * a race condition where it isn't.
238 PTHREAD_MUTEX_LOCK(&proxy_mutex);
240 if (!request->in_proxy_hash) {
241 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
245 fr_packet_list_yank(proxy_list, request->proxy);
246 fr_packet_list_id_free(proxy_list, request->proxy);
249 * On the FIRST reply, decrement the count of outstanding
250 * requests. Note that this is NOT the count of sent
251 * packets, but whether or not the home server has
254 if (!request->proxy_reply &&
255 request->home_server &&
256 request->home_server->currently_outstanding) {
257 request->home_server->currently_outstanding--;
261 request->proxy_listener->count--;
262 request->proxy_listener = NULL;
266 * Got from YES in hash, to NO, not in hash while we hold
267 * the mutex. This guarantees that when another thread
268 * grabs the mutex, the "not in hash" flag is correct.
270 request->in_proxy_hash = FALSE;
272 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
274 #endif /* WITH_PROXY */
277 static int remove_all_requests(void *ctx, void *data)
279 rad_listen_t *this = ctx;
280 RADIUS_PACKET **packet_p = data;
283 request = fr_packet2myptr(REQUEST, packet, packet_p);
284 if (request->packet->sockfd != this->fd) return 0;
286 switch (request->child_state) {
287 case REQUEST_RUNNING:
288 rad_assert(request->ev != NULL); /* or it's lost forever */
290 request->master_state = REQUEST_STOP_PROCESSING;
294 * Waiting for a reply. There's no point in
295 * doing anything else. We remove it from the
296 * request hash so that we can close the upstream
299 case REQUEST_PROXIED:
300 remove_from_request_hash(request);
301 request->child_state = REQUEST_DONE;
304 case REQUEST_REJECT_DELAY:
305 case REQUEST_CLEANUP_DELAY:
307 ev_request_free(&request);
315 static int remove_all_proxied_requests(void *ctx, void *data)
317 rad_listen_t *this = ctx;
318 RADIUS_PACKET **proxy_p = data;
321 request = fr_packet2myptr(REQUEST, proxy, proxy_p);
322 if (request->proxy->sockfd != this->fd) return 0;
324 switch (request->child_state) {
325 case REQUEST_RUNNING:
326 rad_assert(request->ev != NULL); /* or it's lost forever */
328 request->master_state = REQUEST_STOP_PROCESSING;
332 * Eventually we will discover that there is no
333 * response to the proxied request.
335 case REQUEST_PROXIED:
339 * Keep it in the cache for duplicate detection.
341 case REQUEST_REJECT_DELAY:
342 case REQUEST_CLEANUP_DELAY:
347 remove_from_proxy_hash(request);
350 #endif /* WITH_PROXY */
351 #endif /* WITH_TCP */
355 static int insert_into_proxy_hash(REQUEST *request)
359 void *proxy_listener;
361 rad_assert(request->proxy != NULL);
362 rad_assert(proxy_list != NULL);
366 PTHREAD_MUTEX_LOCK(&proxy_mutex);
367 rcode = fr_packet_list_id_alloc(proxy_list,
368 request->home_server->proto,
369 request->proxy, &proxy_listener);
370 request->num_proxied_requests = 1;
371 request->num_proxied_responses = 0;
372 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
375 if (proxy_no_new_sockets) return 0;
378 * Also locks the proxy mutex, so we have to call
379 * it with the mutex unlocked. Some systems
380 * don't support recursive mutexes.
382 if (!proxy_new_listener(request->home_server, 0)) {
383 radlog(L_ERR, "Failed to create a new socket for proxying requests.");
386 request->proxy->src_port = 0; /* Use any new socket */
390 RDEBUG2("ERROR: Failed allocating Id for new socket when proxying requests.");
397 request->proxy_listener = proxy_listener;
399 PTHREAD_MUTEX_LOCK(&proxy_mutex);
400 if (!fr_packet_list_insert(proxy_list, &request->proxy)) {
401 fr_packet_list_id_free(proxy_list, request->proxy);
402 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
403 radlog(L_PROXY, "Failed to insert entry into proxy list");
407 request->in_proxy_hash = TRUE;
410 * Keep track of maximum outstanding requests to a
411 * particular home server. 'max_outstanding' is
412 * enforced in home_server_ldb(), in realms.c.
414 if (request->home_server) {
415 request->home_server->currently_outstanding++;
419 request->proxy_listener->count++;
422 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
424 RDEBUG3(" proxy: allocating destination %s port %d - Id %d",
425 inet_ntop(request->proxy->dst_ipaddr.af,
426 &request->proxy->dst_ipaddr.ipaddr, buf, sizeof(buf)),
427 request->proxy->dst_port,
435 * Called as BOTH an event, and in-line from other functions.
437 static void wait_for_proxy_id_to_expire(void *ctx)
439 REQUEST *request = ctx;
441 rad_assert(request->magic == REQUEST_MAGIC);
442 rad_assert(request->proxy != NULL);
444 fr_event_now(el, &now);
445 request->when = request->proxy_when;
448 if (((request->proxy->code == PW_COA_REQUEST) ||
449 (request->proxy->code == PW_DISCONNECT_REQUEST)) &&
450 (request->packet->code != request->proxy->code)) {
451 request->when.tv_sec += request->home_server->coa_mrd;
454 request->when.tv_sec += request->home_server->response_window;
456 if ((request->num_proxied_requests == request->num_proxied_responses) ||
458 (request->home_server->proto == IPPROTO_TCP) ||
460 timercmp(&now, &request->when, >)) {
461 if (request->packet) {
462 RDEBUG2("Cleaning up request packet ID %d with timestamp +%d",
464 (unsigned int) (request->timestamp - fr_start_time));
466 RDEBUG2("Cleaning up request with timestamp +%d",
467 (unsigned int) (request->timestamp - fr_start_time));
470 ev_request_free(&request);
474 INSERT_EVENT(wait_for_proxy_id_to_expire, request);
478 #ifdef HAVE_PTHREAD_H
479 static void wait_for_child_to_die(void *ctx)
481 REQUEST *request = ctx;
483 rad_assert(request->magic == REQUEST_MAGIC);
486 * If it's still queued (waiting for a thread to pick it
487 * up) OR, it's running AND there's still a child thread
488 * handling it, THEN delay some more.
490 if ((request->child_state == REQUEST_QUEUED) ||
491 ((request->child_state == REQUEST_RUNNING) &&
492 (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0))) {
495 * Cap delay at five minutes.
497 if (request->delay < (USEC * 60 * 5)) {
498 request->delay += (request->delay >> 1);
499 radlog_request(L_INFO, 0, request, "WARNING: Child is hung in component %s module %s.",
500 request->component, request->module);
502 RDEBUG2("Child is still stuck");
504 tv_add(&request->when, request->delay);
506 INSERT_EVENT(wait_for_child_to_die, request);
510 RDEBUG2("Child is finally responsive");
511 remove_from_request_hash(request);
514 if (request->proxy) {
515 wait_for_proxy_id_to_expire(request);
520 ev_request_free(&request);
524 static void cleanup_delay(void *ctx)
526 REQUEST *request = ctx;
528 rad_assert(request->magic == REQUEST_MAGIC);
529 rad_assert((request->child_state == REQUEST_CLEANUP_DELAY) ||
530 (request->child_state == REQUEST_DONE));
532 remove_from_request_hash(request);
535 if (request->proxy && request->in_proxy_hash) {
536 wait_for_proxy_id_to_expire(request);
541 RDEBUG2("Cleaning up request packet ID %d with timestamp +%d",
543 (unsigned int) (request->timestamp - fr_start_time));
545 ev_request_free(&request);
550 * In daemon mode, AND this request has debug flags set.
552 #define DEBUG_PACKET if (!debug_flag && request->options && request->radlog) debug_packet
554 static void debug_packet(REQUEST *request, RADIUS_PACKET *packet, int direction)
558 const char *received, *from;
559 const fr_ipaddr_t *ip;
564 rad_assert(request->radlog != NULL);
566 if (direction == 0) {
567 received = "Received";
568 from = "from"; /* what else? */
569 ip = &packet->src_ipaddr;
570 port = packet->src_port;
573 received = "Sending";
574 from = "to"; /* hah! */
575 ip = &packet->dst_ipaddr;
576 port = packet->dst_port;
580 * Client-specific debugging re-prints the input
581 * packet into the client log.
583 * This really belongs in a utility library
585 if ((packet->code > 0) && (packet->code < FR_MAX_PACKET_CODE)) {
586 RDEBUG("%s %s packet %s host %s port %d, id=%d, length=%d",
587 received, fr_packet_codes[packet->code], from,
588 inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
589 port, packet->id, packet->data_len);
591 RDEBUG("%s packet %s host %s port %d code=%d, id=%d, length=%d",
593 inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
595 packet->code, packet->id, packet->data_len);
598 for (vp = packet->vps; vp != NULL; vp = vp->next) {
599 vp_prints(buffer, sizeof(buffer), vp);
600 request->radlog(L_DBG, 0, request, "\t%s", buffer);
604 static void reject_delay(void *ctx)
606 REQUEST *request = ctx;
608 rad_assert(request->magic == REQUEST_MAGIC);
609 rad_assert(request->child_state == REQUEST_REJECT_DELAY);
611 RDEBUG2("Sending delayed reject");
613 DEBUG_PACKET(request, request->reply, 1);
615 request->listener->send(request->listener, request);
617 request->when.tv_sec += request->root->cleanup_delay;
618 request->child_state = REQUEST_CLEANUP_DELAY;
620 INSERT_EVENT(cleanup_delay, request);
625 void revive_home_server(void *ctx)
627 home_server *home = ctx;
631 rad_assert(home->proto != IPPROTO_TCP);
634 home->state = HOME_STATE_ALIVE;
635 home->currently_outstanding = 0;
636 home->revive_time = now;
639 * Delete any outstanding events.
641 if (home->ev) fr_event_delete(el, &home->ev);
643 radlog(L_PROXY, "Marking home server %s port %d alive again... we have no idea if it really is alive or not.",
644 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
645 buffer, sizeof(buffer)),
651 static void no_response_to_ping(void *ctx)
653 REQUEST *request = ctx;
657 rad_assert(request->home_server != NULL);
659 home = request->home_server;
661 rad_assert(home->proto != IPPROTO_TCP);
664 home->num_received_pings = 0;
666 radlog(L_ERR, "No response to status check %d for home server %s port %d",
668 inet_ntop(request->proxy->dst_ipaddr.af,
669 &request->proxy->dst_ipaddr.ipaddr,
670 buffer, sizeof(buffer)),
671 request->proxy->dst_port);
673 check_for_zombie_home_server(request);
675 wait_for_proxy_id_to_expire(request);
680 * Note that we don't care what the value of the code field is.
681 * If the response has a valid (src ip/port, dst ip/port), id,
682 * and correctly signed Message-Authenticator, that's good
685 static void received_response_to_ping(REQUEST *request)
690 rad_assert(request->home_server != NULL);
692 home = request->home_server;
694 rad_assert(home->proto != IPPROTO_TCP);
697 home->num_received_pings++;
699 radlog(L_PROXY, "Received response to status check %d (%d in current sequence)",
700 request->number, home->num_received_pings);
703 * Remove the request from any hashes
705 fr_event_delete(el, &request->ev);
706 remove_from_proxy_hash(request);
707 rad_assert(request->in_request_hash == FALSE);
710 * The control socket may have marked the home server as
711 * alive. OR, it may have suddenly started responding to
712 * requests again. If so, don't re-do the "make alive"
715 if (home->state == HOME_STATE_ALIVE) return;
718 * We haven't received enough ping responses to mark it
719 * "alive". Wait a bit.
721 if (home->num_received_pings < home->num_pings_to_alive) {
725 home->state = HOME_STATE_ALIVE;
726 home->currently_outstanding = 0;
727 home->revive_time = now;
729 if (!fr_event_delete(el, &home->ev)) {
730 RDEBUG2("Hmm... no event for home server. Oh well.");
733 radlog(L_PROXY, "Marking home server %s port %d alive",
734 inet_ntop(request->proxy->dst_ipaddr.af,
735 &request->proxy->dst_ipaddr.ipaddr,
736 buffer, sizeof(buffer)),
737 request->proxy->dst_port);
742 * Called from start of zombie period, OR after control socket
743 * marks the home server dead.
745 static void ping_home_server(void *ctx)
748 home_server *home = ctx;
753 rad_assert(home->proto != IPPROTO_TCP);
756 if ((home->state == HOME_STATE_ALIVE) ||
757 (home->ping_check == HOME_PING_CHECK_NONE) ||
758 (home->ev != NULL)) {
762 request = request_alloc();
763 request->number = request_num_counter++;
765 request->proxy = rad_alloc(1);
766 rad_assert(request->proxy != NULL);
768 fr_event_now(el, &request->when);
769 home->when = request->when;
771 if (home->ping_check == HOME_PING_CHECK_STATUS_SERVER) {
772 request->proxy->code = PW_STATUS_SERVER;
774 radius_pairmake(request, &request->proxy->vps,
775 "Message-Authenticator", "0x00", T_OP_SET);
777 } else if (home->type == HOME_TYPE_AUTH) {
778 request->proxy->code = PW_AUTHENTICATION_REQUEST;
780 radius_pairmake(request, &request->proxy->vps,
781 "User-Name", home->ping_user_name, T_OP_SET);
782 radius_pairmake(request, &request->proxy->vps,
783 "User-Password", home->ping_user_password, T_OP_SET);
784 radius_pairmake(request, &request->proxy->vps,
785 "Service-Type", "Authenticate-Only", T_OP_SET);
786 radius_pairmake(request, &request->proxy->vps,
787 "Message-Authenticator", "0x00", T_OP_SET);
790 #ifdef WITH_ACCOUNTING
791 request->proxy->code = PW_ACCOUNTING_REQUEST;
793 radius_pairmake(request, &request->proxy->vps,
794 "User-Name", home->ping_user_name, T_OP_SET);
795 radius_pairmake(request, &request->proxy->vps,
796 "Acct-Status-Type", "Stop", T_OP_SET);
797 radius_pairmake(request, &request->proxy->vps,
798 "Acct-Session-Id", "00000000", T_OP_SET);
799 vp = radius_pairmake(request, &request->proxy->vps,
800 "Event-Timestamp", "0", T_OP_SET);
801 vp->vp_date = now.tv_sec;
803 rad_assert("Internal sanity check failed");
807 radius_pairmake(request, &request->proxy->vps,
808 "NAS-Identifier", "Status Check. Are you alive?",
811 request->proxy->dst_ipaddr = home->ipaddr;
812 request->proxy->dst_port = home->port;
813 request->home_server = home;
815 rad_assert(request->proxy_listener == NULL);
817 if (!insert_into_proxy_hash(request)) {
818 radlog(L_PROXY, "Failed inserting status check %d into proxy hash. Discarding it.",
820 ev_request_free(&request);
823 rad_assert(request->proxy_listener != NULL);
824 request->proxy_listener->send(request->proxy_listener,
827 request->next_callback = NULL;
828 request->child_state = REQUEST_PROXIED;
829 request->when.tv_sec += home->ping_timeout;;
831 INSERT_EVENT(no_response_to_ping, request);
834 * Add +/- 2s of jitter, as suggested in RFC 3539
835 * and in the Issues and Fixes draft.
837 home->when.tv_sec += home->ping_interval - 2;
840 jitter ^= (jitter >> 10);
841 jitter &= ((1 << 23) - 1); /* 22 bits of 1 */
843 tv_add(&home->when, jitter);
845 INSERT_EVENT(ping_home_server, home);
849 void mark_home_server_dead(home_server *home, struct timeval *when)
851 int previous_state = home->state;
854 radlog(L_PROXY, "Marking home server %s port %d as dead.",
855 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
856 buffer, sizeof(buffer)),
859 home->state = HOME_STATE_IS_DEAD;
860 home->num_received_pings = 0;
862 if (home->ping_check != HOME_PING_CHECK_NONE) {
864 * If the control socket marks us dead, start
865 * pinging. Otherwise, we already started
866 * pinging when it was marked "zombie".
868 if (previous_state == HOME_STATE_ALIVE) {
869 ping_home_server(home);
874 * Revive it after a fixed period of time. This
875 * is very, very, bad.
878 home->when.tv_sec += home->revive_interval;
880 INSERT_EVENT(revive_home_server, home);
884 static void check_for_zombie_home_server(REQUEST *request)
889 home = request->home_server;
891 if (home->state != HOME_STATE_ZOMBIE) return;
893 when = home->zombie_period_start;
894 when.tv_sec += home->zombie_period;
896 fr_event_now(el, &now);
897 if (timercmp(&now, &when, <)) {
901 mark_home_server_dead(home, &request->when);
904 static int proxy_to_virtual_server(REQUEST *request);
906 static int virtual_server_handler(UNUSED REQUEST *request)
908 proxy_to_virtual_server(request);
912 static void proxy_fallback_handler(REQUEST *request)
915 * A proper time is required for wait_a_bit.
917 request->delay = USEC / 10;
918 gettimeofday(&now, NULL);
919 request->next_when = now;
920 tv_add(&request->next_when, request->delay);
921 request->next_callback = wait_a_bit;
924 * Re-queue the request.
926 request->child_state = REQUEST_QUEUED;
928 rad_assert(request->proxy != NULL);
929 if (!thread_pool_addrequest(request, virtual_server_handler)) {
930 request->child_state = REQUEST_DONE;
933 #ifdef HAVE_PTHREAD_H
935 * MAY free the request if we're over max_request_time,
936 * AND we're not in threaded mode!
938 * Note that we call this ONLY if we're threaded, as
939 * if we're NOT threaded, request_post_handler() calls
940 * wait_a_bit(), which means that "request" may not
943 if (have_children) wait_a_bit(request);
948 static int setup_post_proxy_fail(REQUEST *request)
950 DICT_VALUE *dval = NULL;
953 request->child_state = REQUEST_RUNNING;
955 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
956 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-Authentication");
958 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
959 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-Accounting");
963 * See no_response_to_coa_request
965 } else if (((request->packet->code >> 8) & 0xff) == PW_COA_REQUEST) {
966 request->packet->code &= 0xff; /* restore it */
968 if (request->proxy->code == PW_COA_REQUEST) {
969 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-CoA");
971 } else if (request->proxy->code == PW_DISCONNECT_REQUEST) {
972 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-Disconnect");
982 if (!dval) dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail");
985 pairdelete(&request->config_items, PW_POST_PROXY_TYPE, 0);
989 vp = pairfind(request->config_items, PW_POST_PROXY_TYPE, 0);
990 if (!vp) vp = radius_paircreate(request, &request->config_items,
991 PW_POST_PROXY_TYPE, 0, PW_TYPE_INTEGER);
992 vp->vp_integer = dval->value;
994 rad_assert(request->proxy_reply == NULL);
1000 static int null_handler(UNUSED REQUEST *request)
1005 static void post_proxy_fail_handler(REQUEST *request)
1008 * A proper time is required for wait_a_bit.
1010 request->delay = USEC / 10;
1011 gettimeofday(&now, NULL);
1014 * Not set up to run Post-Proxy-Type = Fail.
1016 * Mark the request as still running, and figure out what
1019 if (!setup_post_proxy_fail(request)) {
1020 request_post_handler(request);
1024 * Re-queue the request.
1026 request->child_state = REQUEST_QUEUED;
1029 * There is a post-proxy-type of fail. We run
1030 * the request through the pre/post proxy
1031 * handlers, just like it was a real proxied
1032 * request. However, we set the per-request
1033 * handler to NULL, as we don't want to do
1036 * Note that when we're not threaded, this will
1037 * process the request even if it's greater than
1038 * max_request_time. That's not fatal.
1040 request->priority = 0;
1041 rad_assert(request->proxy != NULL);
1042 thread_pool_addrequest(request, null_handler);
1046 * MAY free the request if we're over max_request_time,
1047 * AND we're not in threaded mode!
1049 * Note that we call this ONLY if we're threaded, as
1050 * if we're NOT threaded, request_post_handler() calls
1051 * wait_a_bit(), which means that "request" may not
1054 if (have_children) wait_a_bit(request);
1057 /* maybe check this against wait_for_proxy_id_to_expire? */
1058 static void no_response_to_proxied_request(void *ctx)
1060 REQUEST *request = ctx;
1064 rad_assert(request->magic == REQUEST_MAGIC);
1066 if (request->master_state == REQUEST_STOP_PROCESSING) {
1067 ev_request_free(&request);
1071 rad_assert(request->child_state == REQUEST_PROXIED);
1074 * If we've failed over to an internal home server,
1075 * replace the callback with the correct one. This
1076 * is due to locking issues with child threads...
1078 if (request->home_server->server) {
1079 wait_a_bit(request);
1084 if (request->home_server->proto != IPPROTO_TCP)
1086 check_for_zombie_home_server(request);
1088 home = request->home_server;
1091 * The default as of 2.1.7 is to allow requests to
1092 * fail-over to a backup home server when this one does
1093 * not respond. The old behavior can be configured as
1096 if (home->no_response_fail) {
1097 radlog_request(L_ERR, 0, request, "Rejecting request (proxy Id %d) due to lack of any response from home server %s port %d",
1099 inet_ntop(request->proxy->dst_ipaddr.af,
1100 &request->proxy->dst_ipaddr.ipaddr,
1101 buffer, sizeof(buffer)),
1102 request->proxy->dst_port);
1104 post_proxy_fail_handler(request);
1107 * Enforce max_request_time.
1109 * We fail over to another backup home server
1110 * when the client re-transmits the request. If
1111 * the client doesn't re-transmit, no fail-over
1114 rad_assert(request->ev == NULL);
1115 request->child_state = REQUEST_RUNNING;
1116 wait_a_bit(request);
1120 * Don't touch request due to race conditions
1125 * Do nothing more. The home server didn't respond,
1126 * but that isn't a catastrophic failure. Some home
1127 * servers don't respond to packets...
1129 if (home->proto == IPPROTO_TCP) {
1131 * FIXME: Set up TCP pinging on this connection.
1133 * Maybe the CONNECTION is dead, but the home
1134 * server is alive. In that case, we need to start
1135 * pinging on the connection.
1137 * This means doing the pinging BEFORE the
1138 * post_proxy_fail_handler above, as it may do
1139 * something with the request, and cause the
1140 * proxy listener to go away!
1146 if (home->state == HOME_STATE_IS_DEAD) {
1147 rad_assert(home->ev != NULL); /* or it will never wake up */
1152 * Enable the zombie period when we notice that the home
1153 * server hasn't responded. We do NOT back-date the start
1154 * of the zombie period.
1156 if (home->state == HOME_STATE_ALIVE) {
1157 home->state = HOME_STATE_ZOMBIE;
1158 home->zombie_period_start = now;
1159 fr_event_delete(el, &home->ev);
1160 home->currently_outstanding = 0;
1161 home->num_received_pings = 0;
1163 radlog(L_PROXY, "Marking home server %s port %d as zombie (it looks like it is dead).",
1164 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
1165 buffer, sizeof(buffer)),
1169 * Start pinging the home server.
1171 ping_home_server(home);
1176 static void wait_a_bit(void *ctx)
1178 struct timeval when;
1179 REQUEST *request = ctx;
1180 fr_event_callback_t callback = NULL;
1182 rad_assert(request->magic == REQUEST_MAGIC);
1185 * The socket was closed. Tell the request that
1186 * there is no point in continuing.
1188 if (request->listener->status != RAD_LISTEN_STATUS_KNOWN) {
1189 goto stop_processing;
1194 * The CoA request is a new (internally generated)
1195 * request, created in a child thread. We therefore need
1196 * some way to tie its events back into the main event
1199 if (request->coa && !request->coa->proxy_reply &&
1200 request->coa->next_callback) {
1201 request->coa->when = request->coa->next_when;
1202 INSERT_EVENT(request->coa->next_callback, request->coa);
1203 request->coa->next_callback = NULL;
1204 request->coa->parent = NULL;
1205 request->coa = NULL;
1209 switch (request->child_state) {
1210 case REQUEST_QUEUED:
1211 case REQUEST_RUNNING:
1212 when = request->received;
1213 when.tv_sec += request->root->max_request_time;
1216 * Normally called from the event loop with the
1217 * proper event loop time. Otherwise, called from
1218 * post proxy fail handler, which sets "now", and
1219 * this call won't re-set it, because we're not
1220 * in the event loop.
1222 fr_event_now(el, &now);
1225 * Request still has more time. Continue
1228 if (timercmp(&now, &when, <) ||
1229 ((request->listener->type == RAD_LISTEN_DETAIL) &&
1230 (request->child_state == REQUEST_QUEUED))) {
1231 if (request->delay < (USEC / 10)) {
1232 request->delay = USEC / 10;
1234 request->delay += request->delay >> 1;
1238 * Cap wait at some sane value for detail
1241 if ((request->listener->type == RAD_LISTEN_DETAIL) &&
1242 (request->delay > (request->root->max_request_time * USEC))) {
1243 request->delay = request->root->max_request_time * USEC;
1247 request->when = now;
1248 tv_add(&request->when, request->delay);
1249 callback = wait_a_bit;
1254 #if defined(HAVE_PTHREAD_H)
1256 * A child thread MAY still be running on the
1257 * request. Ask the thread to stop working on
1260 if (have_children &&
1261 (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0)) {
1262 request->master_state = REQUEST_STOP_PROCESSING;
1264 radlog_request(L_ERR, 0, request, "WARNING: Unresponsive child in module %s component %s",
1265 request->module ? request->module : "<server core>",
1266 request->component ? request->component : "<server core>");
1268 request->delay = USEC / 4;
1269 tv_add(&request->when, request->delay);
1270 callback = wait_for_child_to_die;
1276 * Else no child thread is processing the
1277 * request. We probably should have just marked
1278 * the request as 'done' elsewhere, like in the
1279 * post-proxy-fail handler. But doing that would
1280 * involve checking for max_request_time in
1281 * multiple places, so this may be simplest.
1283 request->child_state = REQUEST_DONE;
1287 * Mark the request as no longer running,
1291 #ifdef HAVE_PTHREAD_H
1292 request->child_pid = NO_SUCH_CHILD_PID;
1297 * This is a CoA request. It's been divorced
1298 * from everything else, so we clean it up now.
1300 if (!request->in_request_hash &&
1302 (request->packet->code != request->proxy->code) &&
1303 ((request->proxy->code == PW_COA_REQUEST) ||
1304 (request->proxy->code == PW_DISCONNECT_REQUEST))) {
1306 * FIXME: Do CoA MIBs
1308 ev_request_free(&request);
1312 request_stats_final(request);
1313 cleanup_delay(request);
1316 case REQUEST_REJECT_DELAY:
1317 case REQUEST_CLEANUP_DELAY:
1318 #ifdef HAVE_PTHREAD_H
1319 request->child_pid = NO_SUCH_CHILD_PID;
1321 request_stats_final(request);
1323 case REQUEST_PROXIED:
1324 rad_assert(request->next_callback != NULL);
1325 rad_assert(request->next_callback != wait_a_bit);
1327 request->when = request->next_when;
1328 callback = request->next_callback;
1329 request->next_callback = NULL;
1333 rad_panic("Internal sanity check failure");
1338 * Something major went wrong. Discard the request, and
1341 * FIXME: No idea why this happens or how to fix it...
1342 * It seems to happen *only* when requests are proxied,
1343 * and where the home server doesn't respond. So it looks
1344 * like a race condition above, but it happens in debug
1345 * mode, with no threads...
1348 RDEBUG("WARNING: Internal sanity check failed in event handler: Discarding the request!");
1349 ev_request_free(&request);
1353 INSERT_EVENT(callback, request);
1357 static void no_response_to_coa_request(void *ctx)
1359 REQUEST *request = ctx;
1362 rad_assert(request->magic == REQUEST_MAGIC);
1363 rad_assert(request->child_state == REQUEST_PROXIED);
1364 rad_assert(request->home_server != NULL);
1365 rad_assert(!request->in_request_hash);
1367 radlog(L_ERR, "No response to CoA request sent to %s",
1368 inet_ntop(request->proxy->dst_ipaddr.af,
1369 &request->proxy->dst_ipaddr.ipaddr,
1370 buffer, sizeof(buffer)));
1375 request->packet->code |= (PW_COA_REQUEST << 8);
1376 post_proxy_fail_handler(request);
1380 static int update_event_timestamp(RADIUS_PACKET *packet, time_t when)
1384 vp = pairfind(packet->vps, PW_EVENT_TIMESTAMP, 0);
1391 packet->data = NULL;
1392 packet->data_len = 0;
1395 return 1; /* time stamp updated */
1400 * Called when we haven't received a response to a CoA request.
1402 static void retransmit_coa_request(void *ctx)
1406 REQUEST *request = ctx;
1408 rad_assert(request->magic == REQUEST_MAGIC);
1409 rad_assert(request->child_state == REQUEST_PROXIED);
1410 rad_assert(request->home_server != NULL);
1411 rad_assert(!request->in_request_hash);
1412 rad_assert(request->parent == NULL);
1414 fr_event_now(el, &now);
1417 * Cap count at MRC, if it is non-zero.
1419 if (request->home_server->coa_mrc &&
1420 (request->num_coa_requests >= request->home_server->coa_mrc)) {
1421 no_response_to_coa_request(request);
1426 * RFC 5080 Section 2.2.1
1428 * RT = 2*RTprev + RAND*RTprev
1429 * = 1.9 * RTprev + rand(0,.2) * RTprev
1430 * = 1.9 * RTprev + rand(0,1) * (RTprev / 5)
1433 delay ^= (delay >> 16);
1435 frac = request->delay / 5;
1436 delay = ((frac >> 16) * delay) + (((frac & 0xffff) * delay) >> 16);
1438 delay += (2 * request->delay) - (request->delay / 10);
1441 * Cap delay at MRT, if MRT is non-zero.
1443 if (request->home_server->coa_mrt &&
1444 (delay > (request->home_server->coa_mrt * USEC))) {
1445 int mrt_usec = request->home_server->coa_mrt * USEC;
1448 * delay = MRT + RAND * MRT
1449 * = 0.9 MRT + rand(0,.2) * MRT
1452 delay ^= (delay >> 15);
1454 delay = ((mrt_usec >> 16) * delay) + (((mrt_usec & 0xffff) * delay) >> 16);
1455 delay += mrt_usec - (mrt_usec / 10);
1458 request->delay = delay;
1459 request->when = now;
1460 tv_add(&request->when, request->delay);
1461 mrd = request->proxy_when;
1462 mrd.tv_sec += request->home_server->coa_mrd;
1465 * Cap duration at MRD.
1467 if (timercmp(&mrd, &request->when, <)) {
1468 request->when = mrd;
1469 INSERT_EVENT(no_response_to_coa_request, request);
1472 INSERT_EVENT(retransmit_coa_request, request);
1475 if (update_event_timestamp(request->proxy, now.tv_sec)) {
1477 * Keep a copy of the old Id so that the
1478 * re-transmitted request doesn't re-use the old
1481 RADIUS_PACKET old = *request->proxy;
1482 home_server *home = request->home_server;
1483 rad_listen_t *listener = request->proxy_listener;
1486 * Don't free the old Id on error.
1488 if (!insert_into_proxy_hash(request)) {
1489 radlog(L_PROXY,"Failed re-inserting CoA request into proxy hash.");
1494 * Now that we have a new Id, free the old one
1495 * and update the various statistics.
1497 PTHREAD_MUTEX_LOCK(&proxy_mutex);
1498 fr_packet_list_yank(proxy_list, &old);
1499 fr_packet_list_id_free(proxy_list, &old);
1500 if (home) home->currently_outstanding--;
1502 if (listener) listener->count--;
1504 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
1506 } else { /* FIXME: protect by a mutex? */
1507 request->num_proxied_requests++;
1510 request->num_coa_requests++; /* is NOT reset by code 3 lines above! */
1512 request->proxy_listener->send(request->proxy_listener,
1518 * The original request is either DONE, or in CLEANUP_DELAY.
1520 static int originated_coa_request(REQUEST *request)
1522 int delay, rcode, pre_proxy_type = 0;
1528 rad_assert(request->proxy == NULL);
1529 rad_assert(!request->in_proxy_hash);
1530 rad_assert(request->proxy_reply == NULL);
1533 * Check whether we want to originate one, or cancel one.
1535 vp = pairfind(request->config_items, PW_SEND_COA_REQUEST, 0);
1536 if (!vp && request->coa) {
1537 vp = pairfind(request->coa->proxy->vps, PW_SEND_COA_REQUEST, 0);
1541 if (vp->vp_integer == 0) {
1542 ev_request_free(&request->coa);
1543 return 1; /* success */
1547 if (!request->coa) request_alloc_coa(request);
1548 if (!request->coa) return 0;
1553 * src_ipaddr will be set up in proxy_encode.
1555 memset(&ipaddr, 0, sizeof(ipaddr));
1556 vp = pairfind(coa->proxy->vps, PW_PACKET_DST_IP_ADDRESS, 0);
1558 ipaddr.af = AF_INET;
1559 ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
1561 } else if ((vp = pairfind(coa->proxy->vps,
1562 PW_PACKET_DST_IPV6_ADDRESS, 0)) != NULL) {
1563 ipaddr.af = AF_INET6;
1564 ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
1566 } else if ((vp = pairfind(coa->proxy->vps,
1567 PW_HOME_SERVER_POOL, 0)) != NULL) {
1568 coa->home_pool = home_pool_byname(vp->vp_strvalue,
1570 if (!coa->home_pool) {
1571 RDEBUG2("WARNING: No such home_server_pool %s",
1574 ev_request_free(&request->coa);
1581 } else if (request->client->coa_pool) {
1582 coa->home_pool = request->client->coa_pool;
1584 } else if (request->client->coa_server) {
1585 coa->home_server = request->client->coa_server;
1589 * If all else fails, send it to the client that
1590 * originated this request.
1592 memcpy(&ipaddr, &request->packet->src_ipaddr, sizeof(ipaddr));
1596 * Use the pool, if it exists.
1598 if (coa->home_pool) {
1599 coa->home_server = home_server_ldb(NULL, coa->home_pool, coa);
1600 if (!coa->home_server) {
1601 RDEBUG("WARNING: No live home server for home_server_pool %s", vp->vp_strvalue);
1605 } else if (!coa->home_server) {
1606 int port = PW_COA_UDP_PORT;
1608 vp = pairfind(coa->proxy->vps, PW_PACKET_DST_PORT, 0);
1609 if (vp) port = vp->vp_integer;
1611 coa->home_server = home_server_find(&ipaddr, port, IPPROTO_UDP);
1612 if (!coa->home_server) {
1613 RDEBUG2("WARNING: Unknown destination %s:%d for CoA request.",
1614 inet_ntop(ipaddr.af, &ipaddr.ipaddr,
1615 buffer, sizeof(buffer)), port);
1620 vp = pairfind(coa->proxy->vps, PW_PACKET_TYPE, 0);
1622 switch (vp->vp_integer) {
1623 case PW_COA_REQUEST:
1624 case PW_DISCONNECT_REQUEST:
1625 coa->proxy->code = vp->vp_integer;
1629 DEBUG("Cannot set CoA Packet-Type to code %d",
1635 if (!coa->proxy->code) coa->proxy->code = PW_COA_REQUEST;
1638 * The rest of the server code assumes that
1639 * request->packet && request->reply exist. Copy them
1640 * from the original request.
1642 rad_assert(coa->packet != NULL);
1643 rad_assert(coa->packet->vps == NULL);
1644 memcpy(coa->packet, request->packet, sizeof(*request->packet));
1645 coa->packet->vps = paircopy(request->packet->vps);
1646 coa->packet->data = NULL;
1647 rad_assert(coa->reply != NULL);
1648 rad_assert(coa->reply->vps == NULL);
1649 memcpy(coa->reply, request->reply, sizeof(*request->reply));
1650 coa->reply->vps = paircopy(request->reply->vps);
1651 coa->reply->data = NULL;
1652 coa->config_items = paircopy(request->config_items);
1655 * Call the pre-proxy routines.
1657 vp = pairfind(request->config_items, PW_PRE_PROXY_TYPE, 0);
1659 RDEBUG2(" Found Pre-Proxy-Type %s", vp->vp_strvalue);
1660 pre_proxy_type = vp->vp_integer;
1663 if (coa->home_pool && coa->home_pool->virtual_server) {
1664 const char *old_server = coa->server;
1666 coa->server = coa->home_pool->virtual_server;
1667 RDEBUG2(" server %s {", coa->server);
1668 rcode = module_pre_proxy(pre_proxy_type, coa);
1670 coa->server = old_server;
1672 rcode = module_pre_proxy(pre_proxy_type, coa);
1679 * Only send the CoA packet if the pre-proxy code succeeded.
1681 case RLM_MODULE_NOOP:
1683 case RLM_MODULE_UPDATED:
1688 * Source IP / port is set when the proxy socket
1691 coa->proxy->dst_ipaddr = coa->home_server->ipaddr;
1692 coa->proxy->dst_port = coa->home_server->port;
1694 if (!insert_into_proxy_hash(coa)) {
1695 radlog(L_PROXY, "Failed inserting CoA request into proxy hash.");
1700 * We CANNOT divorce the CoA request from the parent
1701 * request. This function is running in a child thread,
1702 * and we need access to the main event loop in order to
1703 * to add the timers for the CoA packet. See
1708 * Forget about the original request completely at this
1713 gettimeofday(&request->proxy_when, NULL);
1714 request->received = request->next_when = request->proxy_when;
1715 rad_assert(request->proxy_reply == NULL);
1718 * Implement re-transmit algorithm as per RFC 5080
1721 * We want IRT + RAND*IRT
1722 * or 0.9 IRT + rand(0,.2) IRT
1724 * 2^20 ~ USEC, and we want 2.
1725 * rand(0,0.2) USEC ~ (rand(0,2^21) / 10)
1727 delay = (fr_rand() & ((1 << 22) - 1)) / 10;
1728 request->delay = delay * request->home_server->coa_irt;
1729 delay = request->home_server->coa_irt * USEC;
1730 delay -= delay / 10;
1731 delay += request->delay;
1733 request->delay = delay;
1734 tv_add(&request->next_when, delay);
1735 request->next_callback = retransmit_coa_request;
1738 * Note that we set proxied BEFORE sending the packet.
1740 * Once we send it, the request is tainted, as
1741 * another thread may have picked it up. Don't
1744 request->child_pid = NO_SUCH_CHILD_PID;
1746 update_event_timestamp(request->proxy, request->proxy_when.tv_sec);
1748 request->child_state = REQUEST_PROXIED;
1750 DEBUG_PACKET(request, request->proxy, 1);
1752 request->proxy_listener->send(request->proxy_listener,
1756 #endif /* WITH_COA */
1759 static int process_proxy_reply(REQUEST *request)
1762 int post_proxy_type = 0;
1766 * Delete any reply we had accumulated until now.
1768 pairfree(&request->reply->vps);
1771 * Run the packet through the post-proxy stage,
1772 * BEFORE playing games with the attributes.
1774 vp = pairfind(request->config_items, PW_POST_PROXY_TYPE, 0);
1776 RDEBUG2(" Found Post-Proxy-Type %s", vp->vp_strvalue);
1777 post_proxy_type = vp->vp_integer;
1780 if (request->home_pool && request->home_pool->virtual_server) {
1781 const char *old_server = request->server;
1783 request->server = request->home_pool->virtual_server;
1784 RDEBUG2(" server %s {", request->server);
1785 rcode = module_post_proxy(post_proxy_type, request);
1787 request->server = old_server;
1789 rcode = module_post_proxy(post_proxy_type, request);
1793 if (request->packet->code == request->proxy->code)
1795 * Don't run the next bit if we originated a CoA
1796 * packet, after receiving an Access-Request or
1797 * Accounting-Request.
1802 * There may NOT be a proxy reply, as we may be
1803 * running Post-Proxy-Type = Fail.
1805 if (request->proxy_reply) {
1807 * Delete the Proxy-State Attributes from
1808 * the reply. These include Proxy-State
1809 * attributes from us and remote server.
1811 pairdelete(&request->proxy_reply->vps, PW_PROXY_STATE, 0);
1814 * Add the attributes left in the proxy
1815 * reply to the reply list.
1817 pairadd(&request->reply->vps, request->proxy_reply->vps);
1818 request->proxy_reply->vps = NULL;
1821 * Free proxy request pairs.
1823 pairfree(&request->proxy->vps);
1827 default: /* Don't do anything */
1829 case RLM_MODULE_FAIL:
1830 /* FIXME: debug print stuff */
1831 request->child_state = REQUEST_DONE;
1834 case RLM_MODULE_HANDLED:
1835 /* FIXME: debug print stuff */
1836 request->child_state = REQUEST_DONE;
1844 static int request_pre_handler(REQUEST *request)
1848 rad_assert(request->magic == REQUEST_MAGIC);
1849 rad_assert(request->packet != NULL);
1851 request->child_state = REQUEST_RUNNING;
1854 * Don't decode the packet if it's an internal "fake"
1855 * request. Instead, just return so that the caller can
1858 if (request->packet->dst_port == 0) {
1859 request->username = pairfind(request->packet->vps,
1861 request->password = pairfind(request->packet->vps,
1862 PW_USER_PASSWORD, 0);
1868 * Put the decoded packet into it's proper place.
1870 if (request->proxy_reply != NULL) {
1871 rcode = request->proxy_listener->decode(request->proxy_listener, request);
1872 DEBUG_PACKET(request, request->proxy_reply, 0);
1875 * Pro-actively remove it from the proxy hash.
1876 * This is later than in 2.1.x, but it means that
1877 * the replies are authenticated before being
1878 * removed from the hash.
1881 (request->num_proxied_requests <= request->num_proxied_responses)) {
1882 remove_from_proxy_hash(request);
1887 if (request->packet->vps == NULL) {
1888 rcode = request->listener->decode(request->listener, request);
1890 if (debug_condition) {
1892 const char *my_debug = debug_condition;
1895 * Ignore parse errors.
1897 radius_evaluate_condition(request, RLM_MODULE_OK, 0,
1901 request->options = 2;
1902 request->radlog = radlog_request;
1906 DEBUG_PACKET(request, request->packet, 0);
1912 RDEBUG("%s Dropping packet without response.", fr_strerror());
1913 request->reply->offset = -2; /* bad authenticator */
1914 request->child_state = REQUEST_DONE;
1918 if (!request->username) {
1919 request->username = pairfind(request->packet->vps,
1924 if (request->proxy) {
1925 return process_proxy_reply(request);
1935 * Do state handling when we proxy a request.
1937 static int proxy_request(REQUEST *request)
1939 struct timeval when;
1944 RDEBUG("WARNING: Cannot proxy and originate CoA packets at the same time. Cancelling CoA request");
1945 ev_request_free(&request->coa);
1949 if (request->home_server->server) {
1950 RDEBUG("ERROR: Cannot perform real proxying to a virtual server.");
1954 if (!insert_into_proxy_hash(request)) {
1955 radlog(L_PROXY, "Failed inserting request into proxy hash.");
1959 request->proxy_listener->encode(request->proxy_listener, request);
1961 when = request->received;
1962 when.tv_sec += request->root->max_request_time;
1964 gettimeofday(&request->proxy_when, NULL);
1966 request->next_when = request->proxy_when;
1967 request->next_when.tv_sec += request->home_server->response_window;
1969 rad_assert(request->home_server->response_window > 0);
1971 if (timercmp(&when, &request->next_when, <)) {
1972 request->next_when = when;
1974 request->next_callback = no_response_to_proxied_request;
1976 RDEBUG2("Proxying request to home server %s port %d",
1977 inet_ntop(request->proxy->dst_ipaddr.af,
1978 &request->proxy->dst_ipaddr.ipaddr,
1979 buffer, sizeof(buffer)),
1980 request->proxy->dst_port);
1983 * Note that we set proxied BEFORE sending the packet.
1985 * Once we send it, the request is tainted, as
1986 * another thread may have picked it up. Don't
1989 #ifdef HAVE_PTHREAD_H
1990 request->child_pid = NO_SUCH_CHILD_PID;
1992 request->child_state = REQUEST_PROXIED;
1994 DEBUG_PACKET(request, request->proxy, 1);
1996 request->proxy_listener->send(request->proxy_listener,
2003 * "Proxy" the request by sending it to a new virtual server.
2005 static int proxy_to_virtual_server(REQUEST *request)
2008 RAD_REQUEST_FUNP fun;
2010 if (!request->home_server || !request->home_server->server) return 0;
2012 if (request->parent) {
2013 RDEBUG2("WARNING: Cancelling proxy request to virtual server %s as this request was itself proxied.", request->home_server->server);
2017 fake = request_alloc_fake(request);
2019 RDEBUG2("WARNING: Out of memory");
2023 fake->packet->vps = paircopy(request->proxy->vps);
2024 fake->server = request->home_server->server;
2026 if (request->proxy->code == PW_AUTHENTICATION_REQUEST) {
2027 fun = rad_authenticate;
2029 #ifdef WITH_ACCOUNTING
2030 } else if (request->proxy->code == PW_ACCOUNTING_REQUEST) {
2031 fun = rad_accounting;
2035 RDEBUG2("Unknown packet type %d", request->proxy->code);
2036 ev_request_free(&fake);
2040 RDEBUG2(">>> Sending proxied request internally to virtual server.");
2041 radius_handle_request(fake, fun);
2042 RDEBUG2("<<< Received proxied response code %d from internal virtual server.", fake->reply->code);
2044 if (fake->reply->code != 0) {
2045 request->proxy_reply = fake->reply;
2049 * There was no response
2051 setup_post_proxy_fail(request);
2054 ev_request_free(&fake);
2056 process_proxy_reply(request);
2059 * Process it through the normal section again, but ONLY
2060 * if we received a proxy reply..
2062 if (request->proxy_reply) {
2063 if (request->server) RDEBUG("server %s {",
2064 request->server != NULL ?
2065 request->server : "");
2068 if (request->server) RDEBUG("} # server %s",
2069 request->server != NULL ?
2070 request->server : "");
2073 return 2; /* success, but NOT '1' !*/
2077 * Return 1 if we did proxy it, or the proxy attempt failed
2078 * completely. Either way, the caller doesn't touch the request
2079 * any more if we return 1.
2081 static int successfully_proxied_request(REQUEST *request)
2084 int pre_proxy_type = 0;
2085 VALUE_PAIR *realmpair;
2086 VALUE_PAIR *strippedname;
2088 char *realmname = NULL;
2090 REALM *realm = NULL;
2094 * If it was already proxied, do nothing.
2096 * FIXME: This should really be a serious error.
2098 if (request->in_proxy_hash ||
2099 (request->proxy_reply && (request->proxy_reply->code != 0))) {
2103 realmpair = pairfind(request->config_items, PW_PROXY_TO_REALM, 0);
2104 if (!realmpair || (realmpair->length == 0)) {
2107 vp = pairfind(request->config_items, PW_HOME_SERVER_POOL, 0);
2110 switch (request->packet->code) {
2111 case PW_AUTHENTICATION_REQUEST:
2112 pool_type = HOME_TYPE_AUTH;
2115 #ifdef WITH_ACCOUNTING
2116 case PW_ACCOUNTING_REQUEST:
2117 pool_type = HOME_TYPE_ACCT;
2122 case PW_COA_REQUEST:
2123 case PW_DISCONNECT_REQUEST:
2124 pool_type = HOME_TYPE_COA;
2132 pool = home_pool_byname(vp->vp_strvalue, pool_type);
2134 RDEBUG2("ERROR: Cannot proxy to unknown pool %s",
2139 realmname = NULL; /* no realms */
2144 realmname = (char *) realmpair->vp_strvalue;
2146 realm = realm_find2(realmname);
2148 RDEBUG2("ERROR: Cannot proxy to unknown realm %s", realmname);
2153 * Figure out which pool to use.
2155 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
2156 pool = realm->auth_pool;
2158 #ifdef WITH_ACCOUNTING
2159 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
2160 pool = realm->acct_pool;
2164 } else if ((request->packet->code == PW_COA_REQUEST) ||
2165 (request->packet->code == PW_DISCONNECT_REQUEST)) {
2166 pool = realm->acct_pool;
2170 rad_panic("Internal sanity check failed");
2174 RDEBUG2(" WARNING: Cancelling proxy to Realm %s, as the realm is local.",
2180 home = home_server_ldb(realmname, pool, request);
2182 RDEBUG2("ERROR: Failed to find live home server for realm %s",
2186 request->home_pool = pool;
2190 * Once we've decided to proxy a request, we cannot send
2191 * a CoA packet. So we free up any CoA packet here.
2193 ev_request_free(&request->coa);
2196 * Remember that we sent the request to a Realm.
2198 if (realmname) pairadd(&request->packet->vps,
2199 pairmake("Realm", realmname, T_OP_EQ));
2202 * Strip the name, if told to.
2204 * Doing it here catches the case of proxied tunneled
2207 if (realm && (realm->striprealm == TRUE) &&
2208 (strippedname = pairfind(request->proxy->vps, PW_STRIPPED_USER_NAME, 0)) != NULL) {
2210 * If there's a Stripped-User-Name attribute in
2211 * the request, then use THAT as the User-Name
2212 * for the proxied request, instead of the
2215 * This is done by making a copy of the
2216 * Stripped-User-Name attribute, turning it into
2217 * a User-Name attribute, deleting the
2218 * Stripped-User-Name and User-Name attributes
2219 * from the vps list, and making the new
2220 * User-Name the head of the vps list.
2222 vp = pairfind(request->proxy->vps, PW_USER_NAME, 0);
2224 vp = radius_paircreate(request, NULL,
2225 PW_USER_NAME, 0, PW_TYPE_STRING);
2226 rad_assert(vp != NULL); /* handled by above function */
2227 /* Insert at the START of the list */
2228 vp->next = request->proxy->vps;
2229 request->proxy->vps = vp;
2231 memcpy(vp->vp_strvalue, strippedname->vp_strvalue,
2232 sizeof(vp->vp_strvalue));
2233 vp->length = strippedname->length;
2236 * Do NOT delete Stripped-User-Name.
2241 * If there is no PW_CHAP_CHALLENGE attribute but
2242 * there is a PW_CHAP_PASSWORD we need to add it
2243 * since we can't use the request authenticator
2244 * anymore - we changed it.
2246 if ((request->packet->code == PW_AUTHENTICATION_REQUEST) &&
2247 pairfind(request->proxy->vps, PW_CHAP_PASSWORD, 0) &&
2248 pairfind(request->proxy->vps, PW_CHAP_CHALLENGE, 0) == NULL) {
2249 vp = radius_paircreate(request, &request->proxy->vps,
2250 PW_CHAP_CHALLENGE, 0, PW_TYPE_OCTETS);
2251 vp->length = AUTH_VECTOR_LEN;
2252 memcpy(vp->vp_strvalue, request->packet->vector, AUTH_VECTOR_LEN);
2256 * The RFC's say we have to do this, but FreeRADIUS
2259 vp = radius_paircreate(request, &request->proxy->vps,
2260 PW_PROXY_STATE, 0, PW_TYPE_OCTETS);
2261 snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%d",
2262 request->packet->id);
2263 vp->length = strlen(vp->vp_strvalue);
2266 * Should be done BEFORE inserting into proxy hash, as
2267 * pre-proxy may use this information, or change it.
2269 request->proxy->code = request->packet->code;
2272 * Call the pre-proxy routines.
2274 vp = pairfind(request->config_items, PW_PRE_PROXY_TYPE, 0);
2276 RDEBUG2(" Found Pre-Proxy-Type %s", vp->vp_strvalue);
2277 pre_proxy_type = vp->vp_integer;
2280 rad_assert(request->home_pool != NULL);
2282 if (request->home_pool->virtual_server) {
2283 const char *old_server = request->server;
2285 request->server = request->home_pool->virtual_server;
2286 RDEBUG2(" server %s {", request->server);
2287 rcode = module_pre_proxy(pre_proxy_type, request);
2289 request->server = old_server;
2291 rcode = module_pre_proxy(pre_proxy_type, request);
2294 case RLM_MODULE_FAIL:
2295 case RLM_MODULE_INVALID:
2296 case RLM_MODULE_NOTFOUND:
2297 case RLM_MODULE_USERLOCK:
2299 /* FIXME: debug print failed stuff */
2302 case RLM_MODULE_REJECT:
2303 case RLM_MODULE_HANDLED:
2307 * Only proxy the packet if the pre-proxy code succeeded.
2309 case RLM_MODULE_NOOP:
2311 case RLM_MODULE_UPDATED:
2316 * If it's a fake request, don't send the proxy
2317 * packet. The outer tunnel session will take
2318 * care of doing that.
2320 if (request->packet->dst_port == 0) {
2321 request->home_server = NULL;
2325 if (request->home_server->server) {
2326 return proxy_to_virtual_server(request);
2329 if (!proxy_request(request)) {
2330 RDEBUG("ERROR: Failed to proxy request");
2338 static void request_post_handler(REQUEST *request)
2340 int child_state = -1;
2341 struct timeval when;
2344 if ((request->master_state == REQUEST_STOP_PROCESSING) ||
2346 (request->parent->master_state == REQUEST_STOP_PROCESSING))) {
2347 RDEBUG2("request was cancelled.");
2348 #ifdef HAVE_PTHREAD_H
2349 request->child_pid = NO_SUCH_CHILD_PID;
2351 child_state = REQUEST_DONE;
2355 if (request->child_state != REQUEST_RUNNING) {
2356 rad_panic("Internal sanity check failed");
2361 * If it's not in the request hash, it's a CoA request.
2364 if (!request->in_request_hash &&
2366 ((request->proxy->code == PW_COA_REQUEST) ||
2367 (request->proxy->code == PW_DISCONNECT_REQUEST))) {
2368 request->next_callback = NULL;
2369 child_state = REQUEST_DONE;
2375 * Catch Auth-Type := Reject BEFORE proxying the packet.
2377 if ((request->packet->code == PW_AUTHENTICATION_REQUEST) &&
2378 (request->reply->code == 0) &&
2379 ((vp = pairfind(request->config_items, PW_AUTH_TYPE, 0)) != NULL) &&
2380 (vp->vp_integer == PW_AUTHTYPE_REJECT)) {
2381 request->reply->code = PW_AUTHENTICATION_REJECT;
2385 if (request->root->proxy_requests &&
2386 !request->in_proxy_hash &&
2387 (request->reply->code == 0) &&
2388 (request->packet->dst_port != 0) &&
2389 (request->packet->code != PW_STATUS_SERVER)) {
2390 int rcode = successfully_proxied_request(request);
2392 if (rcode == 1) return; /* request is invalid */
2395 * Failed proxying it (dead home servers, etc.)
2396 * Run it through Post-Proxy-Type = Fail, and
2397 * respond to the request.
2399 * Note that we're in a child thread here, so we
2400 * do NOT re-schedule the request. Instead, we
2401 * do what we would have done, which is run the
2402 * pre-handler, a NULL request handler, and then
2405 if ((rcode < 0) && setup_post_proxy_fail(request)) {
2406 request_pre_handler(request);
2410 * Else we weren't supposed to proxy it,
2411 * OR we proxied it internally to a virutal server.
2416 else if (request->proxy && request->coa) {
2417 RDEBUG("WARNING: Cannot proxy and originate CoA packets at the same time. Cancelling CoA request");
2418 ev_request_free(&request->coa);
2424 * Fake requests don't get encoded or signed. The caller
2425 * also requires the reply VP's, so we don't free them
2428 if (request->packet->dst_port == 0) {
2429 /* FIXME: RDEBUG going to the next request */
2430 #ifdef HAVE_PTHREAD_H
2431 request->child_pid = NO_SUCH_CHILD_PID;
2433 request->child_state = REQUEST_DONE;
2439 * Copy Proxy-State from the request to the reply.
2441 vp = paircopy2(request->packet->vps, PW_PROXY_STATE, 0);
2442 if (vp) pairadd(&request->reply->vps, vp);
2446 * Access-Requests get delayed or cached.
2448 switch (request->packet->code) {
2449 case PW_AUTHENTICATION_REQUEST:
2450 gettimeofday(&request->next_when, NULL);
2452 if (request->reply->code == 0) {
2454 * Check if the lack of response is intentional.
2456 vp = pairfind(request->config_items,
2457 PW_RESPONSE_PACKET_TYPE, 0);
2459 RDEBUG2("There was no response configured: rejecting request");
2460 request->reply->code = PW_AUTHENTICATION_REJECT;
2462 } else if (vp->vp_integer == 256) {
2463 RDEBUG2("Not responding to request");
2466 * Force cleanup after a long
2467 * time, so that we don't
2468 * re-process the packet.
2470 request->next_when.tv_sec += request->root->max_request_time;
2471 request->next_callback = cleanup_delay;
2472 child_state = REQUEST_CLEANUP_DELAY;
2475 request->reply->code = vp->vp_integer;
2481 * Run rejected packets through
2483 * Post-Auth-Type = Reject
2485 if (request->reply->code == PW_AUTHENTICATION_REJECT) {
2486 pairdelete(&request->config_items, PW_POST_AUTH_TYPE, 0);
2487 vp = radius_pairmake(request, &request->config_items,
2488 "Post-Auth-Type", "Reject",
2490 if (vp) rad_postauth(request);
2493 * If configured, delay Access-Reject packets.
2495 * If request->root->reject_delay = 0, we discover
2496 * that we have to send the packet now.
2498 when = request->received;
2499 when.tv_sec += request->root->reject_delay;
2501 if (timercmp(&when, &request->next_when, >)) {
2502 RDEBUG2("Delaying reject for %d seconds",
2503 request->root->reject_delay);
2504 request->next_when = when;
2505 request->next_callback = reject_delay;
2506 #ifdef HAVE_PTHREAD_H
2507 request->child_pid = NO_SUCH_CHILD_PID;
2509 request->child_state = REQUEST_REJECT_DELAY;
2515 case PW_COA_REQUEST:
2516 case PW_DISCONNECT_REQUEST:
2518 request->next_when.tv_sec += request->root->cleanup_delay;
2519 request->next_callback = cleanup_delay;
2520 child_state = REQUEST_CLEANUP_DELAY;
2523 case PW_ACCOUNTING_REQUEST:
2524 request->next_callback = NULL; /* just to be safe */
2525 child_state = REQUEST_DONE;
2529 * FIXME: Status-Server should probably not be
2532 case PW_STATUS_SERVER:
2533 request->next_callback = NULL;
2534 child_state = REQUEST_DONE;
2541 request->next_callback = NULL;
2542 child_state = REQUEST_DONE;
2547 * Suppress "no reply" packets here, unless we're reading
2548 * from the "detail" file. In that case, we've got to
2549 * tell the detail file handler that the request is dead,
2550 * and it should re-send it.
2551 * If configured, encode, sign, and send.
2553 if ((request->reply->code != 0) ||
2554 (request->listener->type == RAD_LISTEN_DETAIL)) {
2555 DEBUG_PACKET(request, request->reply, 1);
2556 request->listener->send(request->listener, request);
2561 * Now that we've completely processed the request,
2562 * see if we need to originate a CoA request. But ONLY
2563 * if it wasn't proxied.
2565 if (!request->proxy &&
2567 (pairfind(request->config_items, PW_SEND_COA_REQUEST, 0) != NULL))) {
2568 if (!originated_coa_request(request)) {
2569 RDEBUG2("Do CoA Fail handler here");
2571 /* request->coa is stil set, so we can update events */
2577 * Clean up. These are no longer needed.
2579 pairfree(&request->config_items);
2581 pairfree(&request->packet->vps);
2582 request->username = NULL;
2583 request->password = NULL;
2585 pairfree(&request->reply->vps);
2588 if (request->proxy) {
2589 pairfree(&request->proxy->vps);
2591 if (request->proxy_reply) {
2592 pairfree(&request->proxy_reply->vps);
2597 * We're not tracking responses from the home
2598 * server, we can therefore free this memory in
2601 if (!request->in_proxy_hash) {
2602 rad_free(&request->proxy);
2603 rad_free(&request->proxy_reply);
2604 request->home_server = NULL;
2610 RDEBUG2("Finished request.");
2611 rad_assert(child_state >= 0);
2612 request->child_state = child_state;
2615 * Single threaded mode: update timers now.
2617 if (!have_children) wait_a_bit(request);
2621 static void received_retransmit(REQUEST *request, const RADCLIENT *client)
2627 RAD_STATS_TYPE_INC(request->listener, total_dup_requests);
2628 RAD_STATS_CLIENT_INC(request->listener, client, total_dup_requests);
2630 switch (request->child_state) {
2631 case REQUEST_QUEUED:
2632 case REQUEST_RUNNING:
2636 radlog(L_ERR, "Discarding duplicate request from "
2637 "client %s port %d - ID: %d due to unfinished request %u",
2639 request->packet->src_port,request->packet->id,
2644 case REQUEST_PROXIED:
2646 * We're not supposed to have duplicate
2647 * accounting packets. The other states handle
2648 * duplicates fine (discard, or send duplicate
2649 * reply). But we do NOT want to retransmit an
2650 * accounting request here, because that would
2651 * involve updating the Acct-Delay-Time, and
2652 * therefore changing the packet Id, etc.
2654 * Instead, we just discard the packet. We may
2655 * eventually respond, or the client will send a
2656 * new accounting packet.
2658 * The same comments go for Status-Server, and
2659 * other packet types.
2661 * FIXME: coa: when we proxy CoA && Disconnect
2662 * packets, this logic has to be fixed.
2664 if (request->packet->code != PW_AUTHENTICATION_REQUEST) {
2668 check_for_zombie_home_server(request);
2671 * If we've just discovered that the home server
2672 * is dead, OR the socket has been closed, look for
2673 * another connection to a home server.
2675 if (((request->packet->dst_port != 0) &&
2676 (request->home_server->state == HOME_STATE_IS_DEAD)) ||
2677 (request->proxy_listener->status != RAD_LISTEN_STATUS_KNOWN)) {
2680 remove_from_proxy_hash(request);
2682 home = home_server_ldb(NULL, request->home_pool, request);
2684 RDEBUG2("Failed to find live home server for request");
2687 * Do post-request processing,
2688 * and any insertion of necessary
2691 post_proxy_fail_handler(request);
2695 request->proxy->code = request->packet->code;
2698 * Free the old packet, to force re-encoding
2700 free(request->proxy->data);
2701 request->proxy->data = NULL;
2702 request->proxy->data_len = 0;
2705 * This request failed over to a virtual
2706 * server. Push it back onto the queue
2709 if (request->home_server->server) {
2710 proxy_fallback_handler(request);
2715 * Try to proxy the request.
2717 if (!proxy_request(request)) {
2718 RDEBUG("ERROR: Failed to re-proxy request");
2719 goto no_home_servers;
2723 * This code executes in the main server
2724 * thread, so there's no need for locking.
2726 rad_assert(request->next_callback != NULL);
2727 INSERT_EVENT(request->next_callback, request);
2728 request->next_callback = NULL;
2730 } /* else the home server is still alive */
2733 if (request->home_server->proto == IPPROTO_TCP) {
2734 DEBUG2("Suppressing duplicate proxied request to home server %s port %d proto TCP - ID: %d",
2735 inet_ntop(request->proxy->dst_ipaddr.af,
2736 &request->proxy->dst_ipaddr.ipaddr,
2737 buffer, sizeof(buffer)),
2738 request->proxy->dst_port,
2739 request->proxy->id);
2744 RDEBUG2("Sending duplicate proxied request to home server %s port %d - ID: %d",
2745 inet_ntop(request->proxy->dst_ipaddr.af,
2746 &request->proxy->dst_ipaddr.ipaddr,
2747 buffer, sizeof(buffer)),
2748 request->proxy->dst_port,
2749 request->proxy->id);
2750 request->num_proxied_requests++;
2752 DEBUG_PACKET(request, request->proxy, 1);
2753 request->proxy_listener->send(request->proxy_listener,
2758 case REQUEST_REJECT_DELAY:
2759 RDEBUG2("Waiting to send Access-Reject "
2760 "to client %s port %d - ID: %d",
2762 request->packet->src_port, request->packet->id);
2765 case REQUEST_CLEANUP_DELAY:
2767 if (request->reply->code == 0) {
2768 RDEBUG2("Ignoring retransmit from client %s port %d "
2769 "- ID: %d, no reply was configured",
2771 request->packet->src_port, request->packet->id);
2776 * FIXME: This sends duplicate replies to
2777 * accounting requests, even if Acct-Delay-Time
2778 * or Event-Timestamp is in the packet. In those
2779 * cases, the Id should be changed, and the packet
2782 RDEBUG2("Sending duplicate reply "
2783 "to client %s port %d - ID: %d",
2785 request->packet->src_port, request->packet->id);
2786 DEBUG_PACKET(request, request->reply, 1);
2787 request->listener->send(request->listener, request);
2793 static void received_conflicting_request(REQUEST *request,
2794 const RADCLIENT *client)
2796 radlog(L_ERR, "Received conflicting packet from "
2797 "client %s port %d - ID: %d due to unfinished request %u. Giving up on old request.",
2799 request->packet->src_port, request->packet->id,
2803 * Nuke it from the request hash, so we can receive new
2806 remove_from_request_hash(request);
2808 switch (request->child_state) {
2810 * Tell it to stop, and wait for it to do so.
2813 request->master_state = REQUEST_STOP_PROCESSING;
2814 request->delay += request->delay >> 1;
2816 tv_add(&request->when, request->delay);
2818 INSERT_EVENT(wait_for_child_to_die, request);
2822 * Catch race conditions. It may have switched
2823 * from running to done while this code is being
2826 case REQUEST_REJECT_DELAY:
2827 case REQUEST_CLEANUP_DELAY:
2834 static int can_handle_new_request(RADIUS_PACKET *packet,
2836 struct main_config_t *root)
2839 * Count the total number of requests, to see if
2840 * there are too many. If so, return with an
2843 if (root->max_requests) {
2844 int request_count = fr_packet_list_num_elements(pl);
2847 * This is a new request. Let's see if
2848 * it makes us go over our configured
2851 if (request_count > root->max_requests) {
2852 radlog(L_ERR, "Dropping request (%d is too many): "
2853 "from client %s port %d - ID: %d", request_count,
2855 packet->src_port, packet->id);
2856 radlog(L_INFO, "WARNING: Please check the configuration file.\n"
2857 "\tThe value for 'max_requests' is probably set too low.\n");
2859 } /* else there were a small number of requests */
2860 } /* else there was no configured limit for requests */
2863 * FIXME: Add per-client checks. If one client is sending
2864 * too many packets, start discarding them.
2866 * We increment the counters here, and decrement them
2867 * when the response is sent... somewhere in this file.
2871 * FUTURE: Add checks for system load. If the system is
2872 * busy, start dropping requests...
2874 * We can probably keep some statistics ourselves... if
2875 * there are more requests coming in than we can handle,
2876 * start dropping some.
2883 int received_request(rad_listen_t *listener,
2884 RADIUS_PACKET *packet, REQUEST **prequest,
2887 RADIUS_PACKET **packet_p;
2888 REQUEST *request = NULL;
2889 struct main_config_t *root = &mainconfig;
2891 packet_p = fr_packet_list_find(pl, packet);
2893 request = fr_packet2myptr(REQUEST, packet, packet_p);
2894 rad_assert(request->in_request_hash);
2896 if ((request->packet->data_len == packet->data_len) &&
2897 (memcmp(request->packet->vector, packet->vector,
2898 sizeof(packet->vector)) == 0)) {
2899 received_retransmit(request, client);
2904 * The new request is different from the old one,
2905 * but maybe the old is finished. If so, delete
2908 switch (request->child_state) {
2909 struct timeval when;
2913 * Special hacks for race conditions.
2914 * The reply is encoded, and therefore
2915 * likely sent. We received a *new*
2916 * packet from the client, likely before
2917 * the next line or two of code which
2918 * updated the child state. In this
2919 * case, just accept the new request.
2921 if ((request->reply->code != 0) &&
2922 request->reply->data) {
2923 radlog(L_INFO, "WARNING: Allowing fast client %s port %d - ID: %d for recent request %u.",
2925 packet->src_port, packet->id,
2927 remove_from_request_hash(request);
2932 gettimeofday(&when, NULL);
2936 * If the cached request was received
2937 * within the last second, then we
2938 * discard the NEW request instead of the
2939 * old one. This will happen ONLY when
2940 * the client is severely broken, and is
2941 * sending conflicting packets very
2944 if (timercmp(&when, &request->received, <)) {
2945 radlog(L_ERR, "Discarding conflicting packet from "
2946 "client %s port %d - ID: %d due to recent request %u.",
2948 packet->src_port, packet->id,
2953 received_conflicting_request(request, client);
2957 case REQUEST_REJECT_DELAY:
2958 case REQUEST_CLEANUP_DELAY:
2959 request->child_state = REQUEST_DONE;
2961 cleanup_delay(request);
2968 * We may want to quench the new request.
2970 if ((listener->type != RAD_LISTEN_DETAIL) &&
2971 !can_handle_new_request(packet, client, root)) {
2976 * Create and initialize the new request.
2978 request = request_alloc(); /* never fails */
2980 if ((request->reply = rad_alloc(0)) == NULL) {
2981 radlog(L_ERR, "No memory");
2985 request->listener = listener;
2986 request->client = client;
2987 request->packet = packet;
2988 request->packet->timestamp = request->timestamp;
2989 request->number = request_num_counter++;
2990 request->priority = listener->type;
2991 #ifdef HAVE_PTHREAD_H
2992 request->child_pid = NO_SUCH_CHILD_PID;
2996 * Status-Server packets go to the head of the queue.
2998 if (request->packet->code == PW_STATUS_SERVER) request->priority = 0;
3001 * Set virtual server identity
3003 if (client->server) {
3004 request->server = client->server;
3005 } else if (listener->server) {
3006 request->server = listener->server;
3008 request->server = NULL;
3012 * Remember the request in the list.
3014 if (!fr_packet_list_insert(pl, &request->packet)) {
3015 radlog(L_ERR, "Failed to insert request %u in the list of live requests: discarding", request->number);
3016 ev_request_free(&request);
3020 request->in_request_hash = TRUE;
3021 request->root = root;
3024 request->listener->count++;
3028 * The request passes many of our sanity checks.
3029 * From here on in, if anything goes wrong, we
3030 * send a reject message, instead of dropping the
3035 * Build the reply template from the request.
3038 request->reply->sockfd = request->packet->sockfd;
3039 request->reply->dst_ipaddr = request->packet->src_ipaddr;
3040 request->reply->src_ipaddr = request->packet->dst_ipaddr;
3041 request->reply->dst_port = request->packet->src_port;
3042 request->reply->src_port = request->packet->dst_port;
3043 request->reply->id = request->packet->id;
3044 request->reply->code = 0; /* UNKNOWN code */
3045 memcpy(request->reply->vector, request->packet->vector,
3046 sizeof(request->reply->vector));
3047 request->reply->vps = NULL;
3048 request->reply->data = NULL;
3049 request->reply->data_len = 0;
3051 request->master_state = REQUEST_ACTIVE;
3052 request->child_state = REQUEST_QUEUED;
3053 request->next_callback = NULL;
3055 gettimeofday(&request->received, NULL);
3056 request->timestamp = request->received.tv_sec;
3057 request->when = request->received;
3059 request->delay = USEC;
3061 tv_add(&request->when, request->delay);
3063 INSERT_EVENT(wait_a_bit, request);
3065 *prequest = request;
3071 REQUEST *received_proxy_response(RADIUS_PACKET *packet)
3077 * Lookup *without* removal. In versions prior to 2.2.0,
3078 * this did lookup *and* removal. That method allowed
3079 * attackers to spoof replies that caused entries to be
3080 * removed from the proxy hash prior to validation.
3082 request = lookup_in_proxy_hash(packet);
3085 radlog(L_PROXY, "No outstanding request was found for reply from host %s port %d - ID %d",
3086 inet_ntop(packet->src_ipaddr.af,
3087 &packet->src_ipaddr.ipaddr,
3088 buffer, sizeof(buffer)),
3089 packet->src_port, packet->id);
3094 * There's a reply: discard it if it's a conflicting one.
3096 if (request->proxy_reply) {
3098 * ? The home server gave us a new proxy
3099 * reply which doesn't match the old
3102 if (memcmp(request->proxy_reply->vector,
3104 sizeof(request->proxy_reply->vector)) != 0) {
3105 RDEBUG2("Ignoring conflicting proxy reply");
3108 /* assert that there's an event queued for request? */
3110 } /* else it had previously passed verification */
3113 * Verify the packet before doing ANYTHING with
3114 * it. This means we're doing more MD5 checks in
3115 * the server core. However, we can fix that by
3116 * moving to multiple threads listening on
3119 * We do this AFTER looking the request up in the
3120 * hash, and AFTER checking if we saw a previous
3121 * request. This helps minimize the DoS effect
3122 * of people attacking us with spoofed packets.
3124 * FIXME: move the "read from proxy socket" code
3125 * into one (or more) threads. Have it read from
3126 * the socket, do the validation, and write a
3127 * pointer to the packet into a pipe? Or queue it
3128 * to the main server?
3130 } else if (rad_verify(packet, request->proxy,
3131 request->home_server->secret) != 0) {
3132 DEBUG("Ignoring spoofed proxy reply. Signature is invalid");
3137 * Check (again) if it's a duplicate reply. We do this
3138 * after deleting the packet from the proxy hash.
3140 if (request->proxy_reply) {
3141 RDEBUG2("Discarding duplicate reply from host %s port %d - ID: %d",
3142 inet_ntop(packet->src_ipaddr.af,
3143 &packet->src_ipaddr.ipaddr,
3144 buffer, sizeof(buffer)),
3145 packet->src_port, packet->id);
3148 gettimeofday(&now, NULL);
3151 * Maybe move this earlier in the decision process?
3152 * Having it here means that late or duplicate proxy
3153 * replies no longer get the home server marked as
3154 * "alive". This might be good for stability, though.
3156 * FIXME: Do we really want to do this whenever we
3157 * receive a packet? Setting this here means that we
3158 * mark it alive on *any* packet, even if it's lost all
3159 * of the *other* packets in the last 10s.
3161 if (request->proxy->code != PW_STATUS_SERVER) {
3162 request->home_server->state = HOME_STATE_ALIVE;
3167 * When originating CoA, the "proxy" reply is the reply
3168 * to the CoA request that we originated. At this point,
3169 * the original request is finished, and it has a reply.
3171 * However, if we haven't separated the two requests, do
3172 * so now. This is done so that cleaning up the original
3173 * request won't cause the CoA request to be free'd. See
3174 * util.c, request_free()
3176 if (request->parent && (request->parent->coa == request)) {
3177 request->parent->coa = NULL;
3178 request->parent = NULL;
3181 * The proxied packet was different from the
3182 * original packet, AND the proxied packet was
3185 } else if ((request->packet->code != request->proxy->code) &&
3186 ((request->proxy->code == PW_COA_REQUEST) ||
3187 (request->proxy->code == PW_DISCONNECT_REQUEST))) {
3189 * It's already divorced: do nothing.
3194 * Skip the next set of checks, as the original
3195 * reply is cached. We want to be able to still
3196 * process the CoA reply, AND to reference the
3197 * original request/reply.
3199 * This is getting to be really quite a bit of a
3205 * If there's a reply to the NAS, ignore everything
3206 * related to proxy responses
3208 if (request->reply && request->reply->code != 0) {
3209 RDEBUG2("Ignoring proxy reply that arrived after we sent a reply to the NAS");
3215 * The average includes our time to receive packets and
3216 * look them up in the hashes, which should be the same
3219 * We update the response time only for the FIRST packet
3222 if (request->home_server->ema.window > 0) {
3223 radius_stats_ema(&request->home_server->ema,
3224 &now, &request->proxy_when);
3228 switch (request->child_state) {
3229 case REQUEST_QUEUED:
3230 case REQUEST_RUNNING:
3231 radlog(L_ERR, "Internal sanity check failed for child state");
3234 case REQUEST_REJECT_DELAY:
3235 case REQUEST_CLEANUP_DELAY:
3237 radlog(L_ERR, "Reply from home server %s port %d - ID: %d arrived too late for request %u. Try increasing 'retry_delay' or 'max_request_time'",
3238 inet_ntop(packet->src_ipaddr.af,
3239 &packet->src_ipaddr.ipaddr,
3240 buffer, sizeof(buffer)),
3241 packet->src_port, packet->id,
3243 /* assert that there's an event queued for request? */
3246 case REQUEST_PROXIED:
3250 request->proxy_reply = packet;
3254 * Perform RTT calculations, as per RFC 2988 (for TCP).
3255 * Note that we only do so on the first response.
3257 if ((request->num_proxied_responses == 1)
3259 home_server *home = request->home_server;
3261 rtt = now.tv_sec - request->proxy_when.tv_sec;
3264 rtt -= request->proxy_when.tv_usec;
3266 if (!home->has_rtt) {
3267 home->has_rtt = TRUE;
3270 home->rttvar = rtt / 2;
3273 home->rttvar -= home->rttvar >> 2;
3274 home->rttvar += (home->srtt - rtt);
3275 home->srtt -= home->srtt >> 3;
3276 home->srtt += rtt >> 3;
3279 home->rto = home->srtt;
3280 if (home->rttvar > (USEC / 4)) {
3281 home->rto += home->rttvar * 4;
3289 * There's no incoming request, so it's a proxied packet
3292 if (!request->packet) {
3293 received_response_to_ping(request);
3294 request->proxy_reply = NULL; /* caller will free it */
3295 ev_request_free(&request);
3299 request->child_state = REQUEST_QUEUED;
3300 request->when = now;
3301 request->delay = USEC;
3302 request->priority = RAD_LISTEN_PROXY;
3303 tv_add(&request->when, request->delay);
3306 * Wait a bit will take care of max_request_time
3308 INSERT_EVENT(wait_a_bit, request);
3313 #endif /* WITH_PROXY */
3316 static void tcp_socket_lifetime(void *ctx)
3318 rad_listen_t *listener = ctx;
3321 listener->print(listener, buffer, sizeof(buffer));
3323 DEBUG("Reached maximum lifetime on socket %s", buffer);
3325 listener->status = RAD_LISTEN_STATUS_CLOSED;
3326 event_new_fd(listener);
3329 static void tcp_socket_idle_timeout(void *ctx)
3331 rad_listen_t *listener = ctx;
3332 listen_socket_t *sock = listener->data;
3335 fr_event_now(el, &now); /* should always succeed... */
3337 rad_assert(sock->home != NULL);
3340 * We implement idle timeout by polling, because it's
3341 * cheaper than resetting the idle timeout every time
3342 * we send / receive a packet.
3344 if ((sock->last_packet + sock->home->idle_timeout) > now.tv_sec) {
3345 struct timeval when;
3346 void *fun = tcp_socket_idle_timeout;
3348 when.tv_sec = sock->last_packet;
3349 when.tv_sec += sock->home->idle_timeout;
3352 if (sock->home->lifetime &&
3353 (sock->opened + sock->home->lifetime < when.tv_sec)) {
3354 when.tv_sec = sock->opened + sock->home->lifetime;
3355 fun = tcp_socket_lifetime;
3358 if (!fr_event_insert(el, fun, listener, &when, &sock->ev)) {
3359 rad_panic("Failed to insert event");
3365 listener->print(listener, buffer, sizeof(buffer));
3367 DEBUG("Reached idle timeout on socket %s", buffer);
3369 listener->status = RAD_LISTEN_STATUS_CLOSED;
3370 event_new_fd(listener);
3374 int event_new_fd(rad_listen_t *this)
3378 if (this->status == RAD_LISTEN_STATUS_KNOWN) return 1;
3380 this->print(this, buffer, sizeof(buffer));
3382 if (this->status == RAD_LISTEN_STATUS_INIT) {
3384 DEBUG("Listening on %s", buffer);
3386 radlog(L_INFO, " ... adding new socket %s", buffer);
3391 * Add it to the list of sockets we can use.
3392 * Server sockets (i.e. auth/acct) are never
3393 * added to the packet list.
3395 if (this->type == RAD_LISTEN_PROXY) {
3396 listen_socket_t *sock = this->data;
3398 PTHREAD_MUTEX_LOCK(&proxy_mutex);
3399 if (!fr_packet_list_socket_add(proxy_list, this->fd,
3401 &sock->other_ipaddr, sock->other_port,
3404 proxy_no_new_sockets = TRUE;
3405 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
3408 * This is bad. However, the
3409 * packet list now supports 256
3410 * open sockets, which should
3411 * minimize this problem.
3413 radlog(L_ERR, "Failed adding proxy socket: %s",
3419 sock->home->num_connections++;
3422 * If necessary, add it to the list of
3423 * new proxy listeners.
3425 if (sock->home->lifetime || sock->home->idle_timeout) {
3426 this->next = proxy_listener_list;
3427 proxy_listener_list = this;
3430 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
3433 * Tell the main thread that we've added
3434 * a proxy listener, but only if we need
3435 * to update the event list. Do this
3436 * with the mutex unlocked, to reduce
3440 if (sock->home->lifetime || sock->home->idle_timeout) {
3441 radius_signal_self(RADIUS_SIGNAL_SELF_NEW_FD);
3449 * Detail files are always known, and aren't
3450 * put into the socket event loop.
3452 if (this->type == RAD_LISTEN_DETAIL) {
3453 this->status = RAD_LISTEN_STATUS_KNOWN;
3456 * Set up the first poll interval.
3458 event_poll_detail(this);
3463 FD_MUTEX_LOCK(&fd_mutex);
3464 if (!fr_event_fd_insert(el, 0, this->fd,
3465 event_socket_handler, this)) {
3466 radlog(L_ERR, "Failed adding event handler for proxy socket!");
3469 FD_MUTEX_UNLOCK(&fd_mutex);
3471 this->status = RAD_LISTEN_STATUS_KNOWN;
3476 * Something went wrong with the socket: make it harmless.
3478 if (this->status == RAD_LISTEN_STATUS_REMOVE_FD) {
3482 * Remove it from the list of live FD's.
3484 FD_MUTEX_LOCK(&fd_mutex);
3485 fr_event_fd_delete(el, 0, this->fd);
3486 FD_MUTEX_UNLOCK(&fd_mutex);
3490 * We track requests using this socket only for
3491 * TCP. For UDP, we don't currently close
3495 if (this->type != RAD_LISTEN_PROXY)
3498 if (this->count != 0) {
3499 fr_packet_list_walk(pl, this,
3500 remove_all_requests);
3503 if (this->count == 0) {
3504 this->status = RAD_LISTEN_STATUS_FINISH;
3510 int count = this->count;
3515 PTHREAD_MUTEX_LOCK(&proxy_mutex);
3516 if (!fr_packet_list_socket_freeze(proxy_list,
3518 radlog(L_ERR, "Fatal error freezing socket: %s",
3524 * Doing this with the proxy mutex held
3525 * is a Bad Thing. We should move to
3526 * finer-grained mutexes.
3528 count = this->count;
3530 fr_packet_list_walk(proxy_list, this,
3531 remove_all_proxied_requests);
3533 count = this->count; /* protected by mutex */
3534 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
3537 this->status = RAD_LISTEN_STATUS_FINISH;
3541 #endif /* WITH_PROXY */
3542 #endif /* WITH_TCP */
3545 * Re-open the socket, pointing it to /dev/null.
3546 * This means that all writes proceed without
3547 * blocking, and all reads return "no data".
3549 * This leaves the socket active, so any child
3550 * threads won't go insane. But it means that
3551 * they cannot send or receive any packets.
3553 * This is EXTRA work in the normal case, when
3554 * sockets are closed without error. But it lets
3555 * us have one simple processing method for all
3558 devnull = open("/dev/null", O_RDWR);
3560 radlog(L_ERR, "FATAL failure opening /dev/null: %s",
3564 if (dup2(devnull, this->fd) < 0) {
3565 radlog(L_ERR, "FATAL failure closing socket: %s",
3571 this->status = RAD_LISTEN_STATUS_CLOSED;
3574 * Fall through to the next section.
3580 * Called ONLY from the main thread. On the following
3586 * (and falling through from "forcibly close FD" above)
3587 * client closed connection on us
3588 * client sent us a bad packet.
3590 if (this->status == RAD_LISTEN_STATUS_CLOSED) {
3591 int count = this->count;
3592 rad_assert(this->type != RAD_LISTEN_DETAIL);
3596 * Remove it from the list of active sockets, so
3597 * that it isn't used when proxying new packets.
3599 if (this->type == RAD_LISTEN_PROXY) {
3600 PTHREAD_MUTEX_LOCK(&proxy_mutex);
3601 if (!fr_packet_list_socket_freeze(proxy_list,
3603 radlog(L_ERR, "Fatal error freezing socket: %s",
3607 count = this->count; /* protected by mutex */
3608 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
3613 * Requests are still using the socket. Wait for
3617 struct timeval when;
3618 listen_socket_t *sock = this->data;
3621 * Try again to clean up the socket in 30
3624 gettimeofday(&when, NULL);
3627 if (!fr_event_insert(el,
3628 (fr_event_callback_t) event_new_fd,
3629 this, &when, &sock->ev)) {
3630 rad_panic("Failed to insert event");
3637 * No one is using this socket: we can delete it
3640 this->status = RAD_LISTEN_STATUS_FINISH;
3644 if (this->status == RAD_LISTEN_STATUS_FINISH) {
3645 listen_socket_t *sock = this->data;
3647 rad_assert(this->count == 0);
3648 radlog(L_INFO, " ... closing socket %s", buffer);
3651 * Remove it from the list of live FD's. Note
3652 * that it MAY also have been removed above. We
3653 * do it again here, to catch the case of sockets
3654 * closing on idle timeout, or max
3655 * lifetime... AFTER all requests have finished
3658 FD_MUTEX_LOCK(&fd_mutex);
3659 fr_event_fd_delete(el, 0, this->fd);
3660 FD_MUTEX_UNLOCK(&fd_mutex);
3664 * Remove it from the list of sockets to be used
3667 if (this->type == RAD_LISTEN_PROXY) {
3668 PTHREAD_MUTEX_LOCK(&proxy_mutex);
3669 if (!fr_packet_list_socket_remove(proxy_list,
3671 radlog(L_ERR, "Fatal error removing socket: %s",
3675 if (sock->home) sock->home->num_connections--;
3676 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
3681 * Remove any pending cleanups.
3683 if (sock->ev) fr_event_delete(el, &sock->ev);
3686 * And finally, close the socket.
3690 #endif /* WITH_TCP */
3695 static void handle_signal_self(int flag)
3697 if ((flag & (RADIUS_SIGNAL_SELF_EXIT | RADIUS_SIGNAL_SELF_TERM)) != 0) {
3698 if ((flag & RADIUS_SIGNAL_SELF_EXIT) != 0) {
3699 radlog(L_INFO, "Received TERM signal");
3700 fr_event_loop_exit(el, 1);
3702 fr_event_loop_exit(el, 2);
3706 } /* else exit/term flags weren't set */
3709 * Tell the even loop to stop processing.
3711 if ((flag & RADIUS_SIGNAL_SELF_HUP) != 0) {
3713 static time_t last_hup = 0;
3716 if ((int) (when - last_hup) < 5) {
3717 radlog(L_INFO, "Ignoring HUP (less than 5s since last one)");
3721 radlog(L_INFO, "Received HUP signal.");
3725 fr_event_loop_exit(el, 0x80);
3729 if ((flag & RADIUS_SIGNAL_SELF_DETAIL) != 0) {
3733 * FIXME: O(N) loops suck.
3735 for (this = mainconfig.listen;
3737 this = this->next) {
3738 if (this->type != RAD_LISTEN_DETAIL) continue;
3741 * This one didn't send the signal, skip
3744 if (!this->decode(this, NULL)) continue;
3747 * Go service the interrupt.
3749 event_poll_detail(this);
3757 * Add event handlers for idle timeouts && maximum lifetime.
3759 if ((flag & RADIUS_SIGNAL_SELF_NEW_FD) != 0) {
3760 struct timeval when;
3763 fr_event_now(el, &now);
3765 PTHREAD_MUTEX_LOCK(&proxy_mutex);
3767 while (proxy_listener_list) {
3768 rad_listen_t *this = proxy_listener_list;
3769 listen_socket_t *sock = this->data;
3771 proxy_listener_list = this->next;
3774 if (!sock->home) continue; /* skip UDP sockets */
3778 if (!sock->home->idle_timeout) {
3779 rad_assert(sock->home->lifetime != 0);
3781 when.tv_sec += sock->home->lifetime;
3782 fun = tcp_socket_lifetime;
3784 rad_assert(sock->home->idle_timeout != 0);
3786 when.tv_sec += sock->home->idle_timeout;
3787 fun = tcp_socket_idle_timeout;
3790 if (!fr_event_insert(el, fun, this, &when,
3792 rad_panic("Failed to insert event");
3796 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
3798 #endif /* WITH_PROXY */
3799 #endif /* WITH_TCP */
3802 #ifndef WITH_SELF_PIPE
3803 void radius_signal_self(int flag)
3805 handle_signal_self(flag);
3809 * Inform ourselves that we received a signal.
3811 void radius_signal_self(int flag)
3817 * The read MUST be non-blocking for this to work.
3819 rcode = read(self_pipe[0], buffer, sizeof(buffer));
3823 for (i = 0; i < rcode; i++) {
3824 buffer[0] |= buffer[i];
3832 write(self_pipe[1], buffer, 1);
3836 static void event_signal_handler(UNUSED fr_event_list_t *xel,
3837 UNUSED int fd, UNUSED void *ctx)
3842 rcode = read(self_pipe[0], buffer, sizeof(buffer));
3843 if (rcode <= 0) return;
3846 * Merge pending signals.
3848 for (i = 0; i < rcode; i++) {
3849 buffer[0] |= buffer[i];
3852 handle_signal_self(buffer[0]);
3857 static void event_socket_handler(fr_event_list_t *xel, UNUSED int fd,
3860 rad_listen_t *listener = ctx;
3861 RAD_REQUEST_FUNP fun;
3864 rad_assert(xel == el);
3868 if (listener->fd < 0) rad_panic("Socket was closed on us!");
3870 if (!listener->recv(listener, &fun, &request)) return;
3872 if (!thread_pool_addrequest(request, fun)) {
3873 request->child_state = REQUEST_DONE;
3879 * This function is called periodically to see if this detail
3880 * file is available for reading.
3882 static void event_poll_detail(void *ctx)
3885 RAD_REQUEST_FUNP fun;
3887 rad_listen_t *this = ctx;
3888 struct timeval when;
3889 listen_detail_t *detail = this->data;
3891 rad_assert(this->type == RAD_LISTEN_DETAIL);
3894 * Try to read something.
3896 * FIXME: This does poll AND receive.
3898 rcode = this->recv(this, &fun, &request);
3900 rad_assert(fun != NULL);
3901 rad_assert(request != NULL);
3903 if (!thread_pool_addrequest(request, fun)) {
3904 request->child_state = REQUEST_DONE;
3908 fr_event_now(el, &now);
3912 * Backdoor API to get the delay until the next poll
3915 delay = this->encode(this, NULL);
3916 tv_add(&when, delay);
3918 if (!fr_event_insert(el, event_poll_detail, this,
3919 &when, &detail->ev)) {
3920 radlog(L_ERR, "Failed creating handler");
3926 static void event_status(struct timeval *wake)
3928 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
3932 if (debug_flag == 0) {
3934 radlog(L_INFO, "Ready to process requests.");
3935 just_started = FALSE;
3941 radlog(L_INFO, "Ready to process requests.");
3943 } else if ((wake->tv_sec != 0) ||
3944 (wake->tv_usec >= 100000)) {
3945 DEBUG("Waking up in %d.%01u seconds.",
3946 (int) wake->tv_sec, (unsigned int) wake->tv_usec / 100000);
3951 * FIXME: Put this somewhere else, where it isn't called
3952 * all of the time...
3955 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
3957 * If there are no child threads, then there may
3958 * be child processes. In that case, wait for
3959 * their exit status, and throw that exit status
3960 * away. This helps get rid of zxombie children.
3962 while (waitpid(-1, &argval, WNOHANG) > 0) {
3970 * Externally-visibly functions.
3972 int radius_event_init(CONF_SECTION *cs, int spawn_flag)
3974 rad_listen_t *head = NULL;
3978 time(&fr_start_time);
3980 el = fr_event_list_create(event_status);
3983 pl = fr_packet_list_create(0);
3984 if (!pl) return 0; /* leak el */
3986 request_num_counter = 0;
3989 if (mainconfig.proxy_requests) {
3991 * Create the tree for managing proxied requests and
3994 proxy_list = fr_packet_list_create(1);
3995 if (!proxy_list) return 0;
3997 #ifdef HAVE_PTHREAD_H
3998 if (pthread_mutex_init(&proxy_mutex, NULL) != 0) {
3999 radlog(L_ERR, "FATAL: Failed to initialize proxy mutex: %s",
4007 #ifdef HAVE_PTHREAD_H
4009 NO_SUCH_CHILD_PID = (pthread_t ) (0);
4011 NO_SUCH_CHILD_PID = pthread_self(); /* not a child thread */
4014 * Initialize the threads ONLY if we're spawning, AND
4015 * we're running normally.
4017 if (spawn_flag && !check_config &&
4018 (thread_pool_init(cs, &spawn_flag) < 0)) {
4024 * Move all of the thread calls to this file?
4026 * It may be best for the mutexes to be in this file...
4028 have_children = spawn_flag;
4031 DEBUG("%s: #### Skipping IP addresses and Ports ####",
4036 #ifdef WITH_SELF_PIPE
4038 * Child threads need a pipe to signal us, as do the
4041 if (pipe(self_pipe) < 0) {
4042 radlog(L_ERR, "radiusd: Error opening internal pipe: %s",
4046 if (fcntl(self_pipe[0], F_SETFL, O_NONBLOCK | FD_CLOEXEC) < 0) {
4047 radlog(L_ERR, "radiusd: Error setting internal flags: %s",
4051 if (fcntl(self_pipe[1], F_SETFL, O_NONBLOCK | FD_CLOEXEC) < 0) {
4052 radlog(L_ERR, "radiusd: Error setting internal flags: %s",
4057 if (!fr_event_fd_insert(el, 0, self_pipe[0],
4058 event_signal_handler, el)) {
4059 radlog(L_ERR, "Failed creating handler for signals");
4062 #endif /* WITH_SELF_PIPE */
4064 DEBUG("%s: #### Opening IP addresses and Ports ####",
4068 * The server temporarily switches to an unprivileged
4069 * user very early in the bootstrapping process.
4070 * However, some sockets MAY require privileged access
4071 * (bind to device, or to port < 1024, or to raw
4072 * sockets). Those sockets need to call suid up/down
4073 * themselves around the functions that need a privileged
4076 if (listen_init(cs, &head) < 0) {
4080 mainconfig.listen = head;
4083 * At this point, no one has any business *ever* going
4086 fr_suid_down_permanent();
4092 static int request_hash_cb(UNUSED void *ctx, void *data)
4094 REQUEST *request = fr_packet2myptr(REQUEST, packet, data);
4097 rad_assert(request->in_proxy_hash == FALSE);
4100 ev_request_free(&request);
4107 static int proxy_hash_cb(UNUSED void *ctx, void *data)
4109 REQUEST *request = fr_packet2myptr(REQUEST, proxy, data);
4111 ev_request_free(&request);
4117 void radius_event_free(void)
4120 * FIXME: Stop all threads, or at least check that
4121 * they're all waiting on the semaphore, and the queues
4127 * There are requests in the proxy hash that aren't
4128 * referenced from anywhere else. Remove them first.
4131 fr_packet_list_walk(proxy_list, NULL, proxy_hash_cb);
4132 fr_packet_list_free(proxy_list);
4137 fr_packet_list_walk(pl, NULL, request_hash_cb);
4139 fr_packet_list_free(pl);
4142 fr_event_list_free(el);
4145 int radius_event_process(void)
4149 return fr_event_loop(el);
4152 void radius_handle_request(REQUEST *request, RAD_REQUEST_FUNP fun)
4154 request->options = RAD_REQUEST_OPTION_DEBUG2;
4156 if (request_pre_handler(request)) {
4157 rad_assert(fun != NULL);
4158 rad_assert(request != NULL);
4160 if (request->server) RDEBUG("server %s {",
4161 request->server != NULL ?
4162 request->server : "");
4165 if (request->server) RDEBUG("} # server %s",
4166 request->server != NULL ?
4167 request->server : "");
4169 request_post_handler(request);
4172 DEBUG2("Going to the next request");