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)
89 * We need mutexes around the event FD list *only* in certain
92 #if defined (HAVE_PTHREAD_H) && (defined(WITH_PROXY) || defined(WITH_TCP))
93 static pthread_mutex_t fd_mutex;
94 #define FD_MUTEX_LOCK if (have_children) pthread_mutex_lock
95 #define FD_MUTEX_UNLOCK if (have_children) pthread_mutex_unlock
98 * This is easier than ifdef's throughout the code.
100 #define FD_MUTEX_LOCK(_x)
101 #define FD_MUTEX_UNLOCK(_x)
105 #define INSERT_EVENT(_function, _ctx) if (!fr_event_insert(el, _function, _ctx, &((_ctx)->when), &((_ctx)->ev))) { _rad_panic(__FILE__, __LINE__, "Failed to insert event"); }
108 static fr_packet_list_t *proxy_list = NULL;
109 static void remove_from_proxy_hash(REQUEST *request);
111 static void check_for_zombie_home_server(REQUEST *request);
113 #define remove_from_proxy_hash(foo)
116 static void request_post_handler(REQUEST *request);
117 static void wait_a_bit(void *ctx);
118 static void event_socket_handler(fr_event_list_t *xel, UNUSED int fd, void *ctx);
120 static void event_poll_detail(void *ctx);
123 static void NEVER_RETURNS _rad_panic(const char *file, unsigned int line,
126 radlog(L_ERR, "[%s:%d] %s", file, line, msg);
130 #define rad_panic(x) _rad_panic(__FILE__, __LINE__, x)
133 static void tv_add(struct timeval *tv, int usec_delay)
135 if (usec_delay > USEC) {
136 tv->tv_sec += usec_delay / USEC;
139 tv->tv_usec += usec_delay;
141 if (tv->tv_usec > USEC) {
142 tv->tv_sec += tv->tv_usec / USEC;
147 static void remove_from_request_hash(REQUEST *request)
149 if (!request->in_request_hash) return;
151 fr_packet_list_yank(pl, request->packet);
152 request->in_request_hash = FALSE;
155 * FIXME: Move this to a "statistics" thread?
156 * Or (short term) add a mutex lock around it.
158 request_stats_final(request);
161 request->listener->count--;
165 static void ev_request_free(REQUEST **prequest)
169 if (!prequest || !*prequest) return;
176 * Divorce the child from the parent first,
177 * then clean up the child.
179 request->coa->parent = NULL;
180 ev_request_free(&request->coa);
184 * Divorce the parent from the child, and leave the
185 * parent still alive.
187 if (request->parent && (request->parent->coa == request)) {
188 request->parent->coa = NULL;
192 if (request->ev) fr_event_delete(el, &request->ev);
194 if (request->in_proxy_hash) remove_from_proxy_hash(request);
196 if (request->in_request_hash) remove_from_request_hash(request);
198 request_free(prequest);
202 static REQUEST *lookup_in_proxy_hash(RADIUS_PACKET *reply)
204 RADIUS_PACKET **proxy_p;
207 PTHREAD_MUTEX_LOCK(&proxy_mutex);
208 proxy_p = fr_packet_list_find_byreply(proxy_list, reply);
211 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
215 request = fr_packet2myptr(REQUEST, proxy, proxy_p);
216 request->num_proxied_responses++; /* needs to be protected by lock */
218 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
224 static void remove_from_proxy_hash(REQUEST *request)
227 * Check this without grabbing the mutex because it's a
228 * lot faster that way.
230 if (!request->in_proxy_hash) return;
233 * The "not in hash" flag is definitive. However, if the
234 * flag says that it IS in the hash, there might still be
235 * a race condition where it isn't.
237 PTHREAD_MUTEX_LOCK(&proxy_mutex);
239 if (!request->in_proxy_hash) {
240 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
244 fr_packet_list_yank(proxy_list, request->proxy);
245 fr_packet_list_id_free(proxy_list, request->proxy);
248 * On the FIRST reply, decrement the count of outstanding
249 * requests. Note that this is NOT the count of sent
250 * packets, but whether or not the home server has
253 if (!request->proxy_reply &&
254 request->home_server &&
255 request->home_server->currently_outstanding) {
256 request->home_server->currently_outstanding--;
260 request->proxy_listener->count--;
261 request->proxy_listener = NULL;
265 * Got from YES in hash, to NO, not in hash while we hold
266 * the mutex. This guarantees that when another thread
267 * grabs the mutex, the "not in hash" flag is correct.
269 request->in_proxy_hash = FALSE;
271 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
273 #endif /* WITH_PROXY */
276 static int remove_all_requests(void *ctx, void *data)
278 rad_listen_t *this = ctx;
279 RADIUS_PACKET **packet_p = data;
282 request = fr_packet2myptr(REQUEST, packet, packet_p);
283 if (request->packet->sockfd != this->fd) return 0;
285 switch (request->child_state) {
286 case REQUEST_RUNNING:
287 rad_assert(request->ev != NULL); /* or it's lost forever */
289 request->master_state = REQUEST_STOP_PROCESSING;
293 * Waiting for a reply. There's no point in
294 * doing anything else. We remove it from the
295 * request hash so that we can close the upstream
298 case REQUEST_PROXIED:
299 remove_from_request_hash(request);
300 request->child_state = REQUEST_DONE;
303 case REQUEST_REJECT_DELAY:
304 case REQUEST_CLEANUP_DELAY:
306 ev_request_free(&request);
314 static int remove_all_proxied_requests(void *ctx, void *data)
316 rad_listen_t *this = ctx;
317 RADIUS_PACKET **proxy_p = data;
320 request = fr_packet2myptr(REQUEST, proxy, proxy_p);
321 if (request->proxy->sockfd != this->fd) return 0;
323 switch (request->child_state) {
324 case REQUEST_RUNNING:
325 rad_assert(request->ev != NULL); /* or it's lost forever */
327 request->master_state = REQUEST_STOP_PROCESSING;
331 * Eventually we will discover that there is no
332 * response to the proxied request.
334 case REQUEST_PROXIED:
338 * Keep it in the cache for duplicate detection.
340 case REQUEST_REJECT_DELAY:
341 case REQUEST_CLEANUP_DELAY:
346 remove_from_proxy_hash(request);
349 #endif /* WITH_PROXY */
350 #endif /* WITH_TCP */
354 static int insert_into_proxy_hash(REQUEST *request)
358 void *proxy_listener;
360 rad_assert(request->proxy != NULL);
361 rad_assert(proxy_list != NULL);
365 PTHREAD_MUTEX_LOCK(&proxy_mutex);
366 rcode = fr_packet_list_id_alloc(proxy_list,
367 request->home_server->proto,
368 request->proxy, &proxy_listener);
369 request->num_proxied_requests = 1;
370 request->num_proxied_responses = 0;
371 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
374 if (proxy_no_new_sockets) return 0;
377 * Also locks the proxy mutex, so we have to call
378 * it with the mutex unlocked. Some systems
379 * don't support recursive mutexes.
381 if (!proxy_new_listener(request->home_server, 0)) {
382 radlog(L_ERR, "Failed to create a new socket for proxying requests.");
385 request->proxy->src_port = 0; /* Use any new socket */
389 RDEBUG2("ERROR: Failed allocating Id for new socket when proxying requests.");
396 request->proxy_listener = proxy_listener;
398 PTHREAD_MUTEX_LOCK(&proxy_mutex);
399 if (!fr_packet_list_insert(proxy_list, &request->proxy)) {
400 fr_packet_list_id_free(proxy_list, request->proxy);
401 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
402 radlog(L_PROXY, "Failed to insert entry into proxy list.");
406 request->in_proxy_hash = TRUE;
409 * Keep track of maximum outstanding requests to a
410 * particular home server. 'max_outstanding' is
411 * enforced in home_server_ldb(), in realms.c.
413 if (request->home_server) {
414 request->home_server->currently_outstanding++;
418 request->proxy_listener->count++;
421 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
423 RDEBUG3(" proxy: allocating destination %s port %d - Id %d",
424 inet_ntop(request->proxy->dst_ipaddr.af,
425 &request->proxy->dst_ipaddr.ipaddr, buf, sizeof(buf)),
426 request->proxy->dst_port,
434 * Called as BOTH an event, and in-line from other functions.
436 static void wait_for_proxy_id_to_expire(void *ctx)
438 REQUEST *request = ctx;
440 rad_assert(request->magic == REQUEST_MAGIC);
441 rad_assert(request->proxy != NULL);
443 fr_event_now(el, &now);
444 request->when = request->proxy_when;
447 if (((request->proxy->code == PW_COA_REQUEST) ||
448 (request->proxy->code == PW_DISCONNECT_REQUEST)) &&
449 (request->packet->code != request->proxy->code)) {
450 request->when.tv_sec += request->home_server->coa_mrd;
453 request->when.tv_sec += request->home_server->response_window;
455 if ((request->num_proxied_requests == request->num_proxied_responses) ||
457 (request->home_server->proto == IPPROTO_TCP) ||
459 timercmp(&now, &request->when, >)) {
460 if (request->packet) {
461 RDEBUG2("Cleaning up request packet ID %d with timestamp +%d",
463 (unsigned int) (request->timestamp - fr_start_time));
465 RDEBUG2("Cleaning up request with timestamp +%d",
466 (unsigned int) (request->timestamp - fr_start_time));
469 ev_request_free(&request);
473 INSERT_EVENT(wait_for_proxy_id_to_expire, request);
477 #ifdef HAVE_PTHREAD_H
478 static void wait_for_child_to_die(void *ctx)
480 REQUEST *request = ctx;
482 rad_assert(request->magic == REQUEST_MAGIC);
483 remove_from_request_hash(request);
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 max_request_time
497 if (request->delay < (USEC * request->root->max_request_time)) {
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 request->delay = USEC * request->root->max_request_time;
503 RDEBUG2("WARNING: Child is hung after \"max_request_time\" for request %u",
506 tv_add(&request->when, request->delay);
508 INSERT_EVENT(wait_for_child_to_die, request);
512 RDEBUG2("Child is finally responsive");
515 if (request->proxy) {
516 wait_for_proxy_id_to_expire(request);
521 ev_request_free(&request);
525 static void cleanup_delay(void *ctx)
527 REQUEST *request = ctx;
529 rad_assert(request->magic == REQUEST_MAGIC);
530 rad_assert((request->child_state == REQUEST_CLEANUP_DELAY) ||
531 (request->child_state == REQUEST_DONE));
533 remove_from_request_hash(request);
536 if (request->proxy && request->in_proxy_hash) {
537 wait_for_proxy_id_to_expire(request);
542 RDEBUG2("Cleaning up request packet ID %d with timestamp +%d",
544 (unsigned int) (request->timestamp - fr_start_time));
546 ev_request_free(&request);
551 * In daemon mode, AND this request has debug flags set.
553 #define DEBUG_PACKET if (!debug_flag && request->options && request->radlog) debug_packet
555 static void debug_packet(REQUEST *request, RADIUS_PACKET *packet, int direction)
559 const char *received, *from;
560 const fr_ipaddr_t *ip;
565 rad_assert(request->radlog != NULL);
567 if (direction == 0) {
568 received = "Received";
569 from = "from"; /* what else? */
570 ip = &packet->src_ipaddr;
571 port = packet->src_port;
574 received = "Sending";
575 from = "to"; /* hah! */
576 ip = &packet->dst_ipaddr;
577 port = packet->dst_port;
581 * Client-specific debugging re-prints the input
582 * packet into the client log.
584 * This really belongs in a utility library
586 if ((packet->code > 0) && (packet->code < FR_MAX_PACKET_CODE)) {
587 RDEBUG("%s %s packet %s host %s port %d, id=%d, length=%d",
588 received, fr_packet_codes[packet->code], from,
589 inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
590 port, packet->id, packet->data_len);
592 RDEBUG("%s packet %s host %s port %d code=%d, id=%d, length=%d",
594 inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
596 packet->code, packet->id, packet->data_len);
599 for (vp = packet->vps; vp != NULL; vp = vp->next) {
600 vp_prints(buffer, sizeof(buffer), vp);
601 request->radlog(L_DBG, 0, request, "\t%s", buffer);
605 static void reject_delay(void *ctx)
607 REQUEST *request = ctx;
609 rad_assert(request->magic == REQUEST_MAGIC);
610 rad_assert(request->child_state == REQUEST_REJECT_DELAY);
612 RDEBUG2("Sending delayed reject");
614 DEBUG_PACKET(request, request->reply, 1);
616 request->listener->send(request->listener, request);
618 request->when.tv_sec += request->root->cleanup_delay;
619 request->child_state = REQUEST_CLEANUP_DELAY;
621 INSERT_EVENT(cleanup_delay, request);
626 void revive_home_server(void *ctx)
628 home_server *home = ctx;
632 rad_assert(home->proto != IPPROTO_TCP);
635 home->state = HOME_STATE_ALIVE;
636 home->currently_outstanding = 0;
637 home->revive_time = now;
640 * Delete any outstanding events.
642 if (home->ev) fr_event_delete(el, &home->ev);
644 radlog(L_PROXY, "Marking home server %s port %d alive again... we have no idea if it really is alive or not.",
645 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
646 buffer, sizeof(buffer)),
652 static void no_response_to_ping(void *ctx)
654 REQUEST *request = ctx;
658 rad_assert(request->home_server != NULL);
660 home = request->home_server;
662 rad_assert(home->proto != IPPROTO_TCP);
665 home->num_received_pings = 0;
667 radlog(L_ERR, "No response to status check %d for home server %s port %d",
669 inet_ntop(request->proxy->dst_ipaddr.af,
670 &request->proxy->dst_ipaddr.ipaddr,
671 buffer, sizeof(buffer)),
672 request->proxy->dst_port);
674 check_for_zombie_home_server(request);
676 wait_for_proxy_id_to_expire(request);
681 * Note that we don't care what the value of the code field is.
682 * If the response has a valid (src ip/port, dst ip/port), id,
683 * and correctly signed Message-Authenticator, that's good
686 static void received_response_to_ping(REQUEST *request)
691 rad_assert(request->home_server != NULL);
693 home = request->home_server;
695 rad_assert(home->proto != IPPROTO_TCP);
698 home->num_received_pings++;
700 radlog(L_PROXY, "Received response to status check %d (%d in current sequence)",
701 request->number, home->num_received_pings);
704 * Remove the request from any hashes
706 fr_event_delete(el, &request->ev);
707 remove_from_proxy_hash(request);
708 rad_assert(request->in_request_hash == FALSE);
711 * The control socket may have marked the home server as
712 * alive. OR, it may have suddenly started responding to
713 * requests again. If so, don't re-do the "make alive"
716 if (home->state == HOME_STATE_ALIVE) return;
719 * We haven't received enough ping responses to mark it
720 * "alive". Wait a bit.
722 if (home->num_received_pings < home->num_pings_to_alive) {
726 home->state = HOME_STATE_ALIVE;
727 home->currently_outstanding = 0;
728 home->revive_time = now;
730 if (!fr_event_delete(el, &home->ev)) {
731 RDEBUG2("Hmm... no event for home server. Oh well.");
734 radlog(L_PROXY, "Marking home server %s port %d alive",
735 inet_ntop(request->proxy->dst_ipaddr.af,
736 &request->proxy->dst_ipaddr.ipaddr,
737 buffer, sizeof(buffer)),
738 request->proxy->dst_port);
743 * Called from start of zombie period, OR after control socket
744 * marks the home server dead.
746 static void ping_home_server(void *ctx)
749 home_server *home = ctx;
754 rad_assert(home->proto != IPPROTO_TCP);
757 if ((home->state == HOME_STATE_ALIVE) ||
758 (home->ping_check == HOME_PING_CHECK_NONE) ||
759 (home->ev != NULL)) {
763 request = request_alloc();
764 request->number = request_num_counter++;
766 request->proxy = rad_alloc(1);
767 rad_assert(request->proxy != NULL);
769 fr_event_now(el, &request->when);
770 home->when = request->when;
772 if (home->ping_check == HOME_PING_CHECK_STATUS_SERVER) {
773 request->proxy->code = PW_STATUS_SERVER;
775 radius_pairmake(request, &request->proxy->vps,
776 "Message-Authenticator", "0x00", T_OP_SET);
778 } else if (home->type == HOME_TYPE_AUTH) {
779 request->proxy->code = PW_AUTHENTICATION_REQUEST;
781 radius_pairmake(request, &request->proxy->vps,
782 "User-Name", home->ping_user_name, T_OP_SET);
783 radius_pairmake(request, &request->proxy->vps,
784 "User-Password", home->ping_user_password, T_OP_SET);
785 radius_pairmake(request, &request->proxy->vps,
786 "Service-Type", "Authenticate-Only", T_OP_SET);
787 radius_pairmake(request, &request->proxy->vps,
788 "Message-Authenticator", "0x00", T_OP_SET);
791 #ifdef WITH_ACCOUNTING
792 request->proxy->code = PW_ACCOUNTING_REQUEST;
794 radius_pairmake(request, &request->proxy->vps,
795 "User-Name", home->ping_user_name, T_OP_SET);
796 radius_pairmake(request, &request->proxy->vps,
797 "Acct-Status-Type", "Stop", T_OP_SET);
798 radius_pairmake(request, &request->proxy->vps,
799 "Acct-Session-Id", "00000000", T_OP_SET);
800 vp = radius_pairmake(request, &request->proxy->vps,
801 "Event-Timestamp", "0", T_OP_SET);
802 vp->vp_date = now.tv_sec;
804 rad_assert("Internal sanity check failed");
808 radius_pairmake(request, &request->proxy->vps,
809 "NAS-Identifier", "Status Check. Are you alive?",
812 request->proxy->dst_ipaddr = home->ipaddr;
813 request->proxy->dst_port = home->port;
814 request->home_server = home;
816 rad_assert(request->proxy_listener == NULL);
818 if (!insert_into_proxy_hash(request)) {
819 radlog(L_PROXY, "Failed to insert status check %d into proxy list. Discarding it.",
821 ev_request_free(&request);
824 rad_assert(request->proxy_listener != NULL);
825 request->proxy_listener->send(request->proxy_listener,
828 request->next_callback = NULL;
829 request->child_state = REQUEST_PROXIED;
830 request->when.tv_sec += home->ping_timeout;;
832 INSERT_EVENT(no_response_to_ping, request);
835 * Add +/- 2s of jitter, as suggested in RFC 3539
836 * and in the Issues and Fixes draft.
838 home->when.tv_sec += home->ping_interval - 2;
841 jitter ^= (jitter >> 10);
842 jitter &= ((1 << 23) - 1); /* 22 bits of 1 */
844 tv_add(&home->when, jitter);
846 INSERT_EVENT(ping_home_server, home);
850 void mark_home_server_dead(home_server *home, struct timeval *when)
852 int previous_state = home->state;
855 radlog(L_PROXY, "Marking home server %s port %d as dead.",
856 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
857 buffer, sizeof(buffer)),
860 home->state = HOME_STATE_IS_DEAD;
861 home->num_received_pings = 0;
863 if (home->ping_check != HOME_PING_CHECK_NONE) {
865 * If the control socket marks us dead, start
866 * pinging. Otherwise, we already started
867 * pinging when it was marked "zombie".
869 if (previous_state == HOME_STATE_ALIVE) {
870 ping_home_server(home);
875 * Revive it after a fixed period of time. This
876 * is very, very, bad.
879 home->when.tv_sec += home->revive_interval;
881 INSERT_EVENT(revive_home_server, home);
885 static void check_for_zombie_home_server(REQUEST *request)
890 home = request->home_server;
892 if (home->state != HOME_STATE_ZOMBIE) return;
894 when = home->zombie_period_start;
895 when.tv_sec += home->zombie_period;
897 fr_event_now(el, &now);
898 if (timercmp(&now, &when, <)) {
902 mark_home_server_dead(home, &request->when);
905 static int proxy_to_virtual_server(REQUEST *request);
907 static int virtual_server_handler(UNUSED REQUEST *request)
909 proxy_to_virtual_server(request);
913 static void proxy_fallback_handler(REQUEST *request)
916 * A proper time is required for wait_a_bit.
918 request->delay = USEC / 10;
919 gettimeofday(&now, NULL);
920 request->next_when = now;
921 tv_add(&request->next_when, request->delay);
922 request->next_callback = wait_a_bit;
925 * Re-queue the request.
927 request->child_state = REQUEST_QUEUED;
929 rad_assert(request->proxy != NULL);
930 thread_pool_addrequest(request, virtual_server_handler);
932 #ifdef HAVE_PTHREAD_H
934 * MAY free the request if we're over max_request_time,
935 * AND we're not in threaded mode!
937 * Note that we call this ONLY if we're threaded, as
938 * if we're NOT threaded, request_post_handler() calls
939 * wait_a_bit(), which means that "request" may not
942 if (have_children) wait_a_bit(request);
947 static int setup_post_proxy_fail(REQUEST *request)
949 DICT_VALUE *dval = NULL;
952 request->child_state = REQUEST_RUNNING;
954 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
955 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-Authentication");
957 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
958 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-Accounting");
962 * See no_response_to_coa_request
964 } else if (((request->packet->code >> 8) & 0xff) == PW_COA_REQUEST) {
965 request->packet->code &= 0xff; /* restore it */
967 if (request->proxy->code == PW_COA_REQUEST) {
968 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-CoA");
970 } else if (request->proxy->code == PW_DISCONNECT_REQUEST) {
971 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-Disconnect");
981 if (!dval) dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail");
984 pairdelete(&request->config_items, PW_POST_PROXY_TYPE, 0);
988 vp = pairfind(request->config_items, PW_POST_PROXY_TYPE, 0);
989 if (!vp) vp = radius_paircreate(request, &request->config_items,
990 PW_POST_PROXY_TYPE, 0, PW_TYPE_INTEGER);
991 vp->vp_integer = dval->value;
993 rad_assert(request->proxy_reply == NULL);
999 static int null_handler(UNUSED REQUEST *request)
1004 static void post_proxy_fail_handler(REQUEST *request)
1007 * A proper time is required for wait_a_bit.
1009 request->delay = USEC / 10;
1010 gettimeofday(&now, NULL);
1013 * Not set up to run Post-Proxy-Type = Fail.
1015 * Mark the request as still running, and figure out what
1018 if (!setup_post_proxy_fail(request)) {
1019 request_post_handler(request);
1023 * Re-queue the request.
1025 request->child_state = REQUEST_QUEUED;
1028 * There is a post-proxy-type of fail. We run
1029 * the request through the pre/post proxy
1030 * handlers, just like it was a real proxied
1031 * request. However, we set the per-request
1032 * handler to NULL, as we don't want to do
1035 * Note that when we're not threaded, this will
1036 * process the request even if it's greater than
1037 * max_request_time. That's not fatal.
1039 request->priority = 0;
1040 rad_assert(request->proxy != NULL);
1041 thread_pool_addrequest(request, null_handler);
1045 * MAY free the request if we're over max_request_time,
1046 * AND we're not in threaded mode!
1048 * Note that we call this ONLY if we're threaded, as
1049 * if we're NOT threaded, request_post_handler() calls
1050 * wait_a_bit(), which means that "request" may not
1053 if (have_children) wait_a_bit(request);
1056 /* maybe check this against wait_for_proxy_id_to_expire? */
1057 static void no_response_to_proxied_request(void *ctx)
1059 REQUEST *request = ctx;
1063 rad_assert(request->magic == REQUEST_MAGIC);
1065 if (request->master_state == REQUEST_STOP_PROCESSING) {
1066 ev_request_free(&request);
1070 rad_assert(request->child_state == REQUEST_PROXIED);
1073 * If we've failed over to an internal home server,
1074 * replace the callback with the correct one. This
1075 * is due to locking issues with child threads...
1077 if (request->home_server->server) {
1078 wait_a_bit(request);
1083 if (request->home_server->proto != IPPROTO_TCP)
1085 check_for_zombie_home_server(request);
1087 home = request->home_server;
1090 * The default as of 2.1.7 is to allow requests to
1091 * fail-over to a backup home server when this one does
1092 * not respond. The old behavior can be configured as
1095 if (home->no_response_fail) {
1096 radlog_request(L_ERR, 0, request, "Rejecting request (proxy Id %d) due to lack of any response from home server %s port %d",
1098 inet_ntop(request->proxy->dst_ipaddr.af,
1099 &request->proxy->dst_ipaddr.ipaddr,
1100 buffer, sizeof(buffer)),
1101 request->proxy->dst_port);
1103 post_proxy_fail_handler(request);
1106 * Enforce max_request_time.
1108 * We fail over to another backup home server
1109 * when the client re-transmits the request. If
1110 * the client doesn't re-transmit, no fail-over
1113 rad_assert(request->ev == NULL);
1114 request->child_state = REQUEST_RUNNING;
1115 wait_a_bit(request);
1119 * Don't touch request due to race conditions
1124 * Do nothing more. The home server didn't respond,
1125 * but that isn't a catastrophic failure. Some home
1126 * servers don't respond to packets...
1128 if (home->proto == IPPROTO_TCP) {
1130 * FIXME: Set up TCP pinging on this connection.
1132 * Maybe the CONNECTION is dead, but the home
1133 * server is alive. In that case, we need to start
1134 * pinging on the connection.
1136 * This means doing the pinging BEFORE the
1137 * post_proxy_fail_handler above, as it may do
1138 * something with the request, and cause the
1139 * proxy listener to go away!
1146 * If it's not alive, don't try to make it a zombie.
1148 if (home->state != HOME_STATE_ALIVE) {
1150 * Don't check home->ev due to race conditions.
1156 * We've received a real packet recently. Don't mark the
1157 * server as zombie until we've received NO packets for a
1158 * while. The "1/4" of zombie period was chosen rather
1159 * arbitrarily. It's a balance between too short, which
1160 * gives quick fail-over and fail-back, or too long,
1161 * where the proxy still sends packets to an unresponsive
1164 if ((home->last_packet + ((home->zombie_period + 3) / 4)) >= now.tv_sec) {
1169 * Enable the zombie period when we notice that the home
1170 * server hasn't responded for a while. We back-date the
1171 * zombie period to when we last received a response from
1174 home->state = HOME_STATE_ZOMBIE;
1176 home->zombie_period_start.tv_sec = home->last_packet;
1177 home->zombie_period_start.tv_usec = USEC / 2;
1179 fr_event_delete(el, &home->ev);
1180 home->currently_outstanding = 0;
1181 home->num_received_pings = 0;
1183 radlog(L_PROXY, "Marking home server %s port %d as zombie (it looks like it is dead).",
1184 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
1185 buffer, sizeof(buffer)),
1189 * Start pinging the home server.
1191 ping_home_server(home);
1195 static void wait_a_bit(void *ctx)
1197 struct timeval when;
1198 REQUEST *request = ctx;
1199 fr_event_callback_t callback = NULL;
1201 rad_assert(request->magic == REQUEST_MAGIC);
1203 #ifdef HAVE_PTHREAD_H
1205 * The socket was closed. Tell the request that
1206 * there is no point in continuing.
1208 if (request->listener->status != RAD_LISTEN_STATUS_KNOWN) {
1209 goto stop_processing;
1215 * The CoA request is a new (internally generated)
1216 * request, created in a child thread. We therefore need
1217 * some way to tie its events back into the main event
1220 if (request->coa && !request->coa->proxy_reply &&
1221 request->coa->next_callback) {
1222 request->coa->when = request->coa->next_when;
1223 INSERT_EVENT(request->coa->next_callback, request->coa);
1224 request->coa->next_callback = NULL;
1225 request->coa->parent = NULL;
1226 request->coa = NULL;
1230 switch (request->child_state) {
1231 case REQUEST_QUEUED:
1232 case REQUEST_RUNNING:
1234 * If we're not thread-capable, OR we're capable,
1235 * but have been told to run without threads, and
1236 * the request is still running. This is usually
1237 * because the request was proxied, and the home
1238 * server didn't respond.
1240 #ifdef HAVE_PTHREAD_H
1244 request->child_state = REQUEST_DONE;
1248 #ifdef HAVE_PTHREAD_H
1250 * If we have threads, wait for the child thread
1253 when = request->received;
1254 when.tv_sec += request->root->max_request_time;
1257 * Normally called from the event loop with the
1258 * proper event loop time. Otherwise, called from
1259 * post proxy fail handler, which sets "now", and
1260 * this call won't re-set it, because we're not
1261 * in the event loop.
1263 fr_event_now(el, &now);
1266 * Request still has more time. Continue
1269 if (timercmp(&now, &when, <)) {
1270 if (request->delay < (USEC / 10)) {
1271 request->delay = USEC / 10;
1273 request->delay += request->delay >> 1;
1276 * Cap delays at something reasonable.
1278 if (request->delay > (request->root->max_request_time * USEC)) {
1279 request->delay = request->root->max_request_time * USEC;
1282 request->when = now;
1283 tv_add(&request->when, request->delay);
1284 callback = wait_a_bit;
1289 request->master_state = REQUEST_STOP_PROCESSING;
1292 * A child thread MAY still be running on the
1293 * request. Ask the thread to stop working on
1296 if (have_children &&
1297 (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0)) {
1298 radlog(L_ERR, "WARNING: Unresponsive child for request %u, in component %s module %s",
1300 request->component ? request->component : "<server core>",
1301 request->module ? request->module : "<server core>");
1305 request->delay = USEC;
1306 tv_add(&request->when, request->delay);
1307 callback = wait_for_child_to_die;
1312 * Mark the request as no longer running,
1317 #ifdef HAVE_PTHREAD_H
1318 request->child_pid = NO_SUCH_CHILD_PID;
1323 * This is a CoA request. It's been divorced
1324 * from everything else, so we clean it up now.
1326 if (!request->in_request_hash &&
1328 (request->packet->code != request->proxy->code) &&
1329 ((request->proxy->code == PW_COA_REQUEST) ||
1330 (request->proxy->code == PW_DISCONNECT_REQUEST))) {
1332 * FIXME: Do CoA MIBs
1334 ev_request_free(&request);
1338 cleanup_delay(request);
1341 case REQUEST_REJECT_DELAY:
1342 case REQUEST_CLEANUP_DELAY:
1343 #ifdef HAVE_PTHREAD_H
1344 request->child_pid = NO_SUCH_CHILD_PID;
1347 case REQUEST_PROXIED:
1348 rad_assert(request->next_callback != NULL);
1349 rad_assert(request->next_callback != wait_a_bit);
1351 request->when = request->next_when;
1352 callback = request->next_callback;
1353 request->next_callback = NULL;
1357 rad_panic("Internal sanity check failure");
1362 * Something major went wrong. Discard the request, and
1365 * FIXME: No idea why this happens or how to fix it...
1366 * It seems to happen *only* when requests are proxied,
1367 * and where the home server doesn't respond. So it looks
1368 * like a race condition above, but it happens in debug
1369 * mode, with no threads...
1372 RDEBUG("WARNING: Internal sanity check failed in event handler: Discarding the request!");
1373 ev_request_free(&request);
1377 INSERT_EVENT(callback, request);
1381 static void no_response_to_coa_request(void *ctx)
1383 REQUEST *request = ctx;
1386 rad_assert(request->magic == REQUEST_MAGIC);
1387 rad_assert(request->child_state == REQUEST_PROXIED);
1388 rad_assert(request->home_server != NULL);
1389 rad_assert(!request->in_request_hash);
1391 radlog(L_ERR, "No response to CoA request sent to %s",
1392 inet_ntop(request->proxy->dst_ipaddr.af,
1393 &request->proxy->dst_ipaddr.ipaddr,
1394 buffer, sizeof(buffer)));
1399 request->packet->code |= (PW_COA_REQUEST << 8);
1400 post_proxy_fail_handler(request);
1404 static int update_event_timestamp(RADIUS_PACKET *packet, time_t when)
1408 vp = pairfind(packet->vps, PW_EVENT_TIMESTAMP, 0);
1415 packet->data = NULL;
1416 packet->data_len = 0;
1419 return 1; /* time stamp updated */
1424 * Called when we haven't received a response to a CoA request.
1426 static void retransmit_coa_request(void *ctx)
1430 REQUEST *request = ctx;
1432 rad_assert(request->magic == REQUEST_MAGIC);
1433 rad_assert(request->child_state == REQUEST_PROXIED);
1434 rad_assert(request->home_server != NULL);
1435 rad_assert(!request->in_request_hash);
1436 rad_assert(request->parent == NULL);
1438 fr_event_now(el, &now);
1441 * Cap count at MRC, if it is non-zero.
1443 if (request->home_server->coa_mrc &&
1444 (request->num_coa_requests >= request->home_server->coa_mrc)) {
1445 no_response_to_coa_request(request);
1450 * RFC 5080 Section 2.2.1
1452 * RT = 2*RTprev + RAND*RTprev
1453 * = 1.9 * RTprev + rand(0,.2) * RTprev
1454 * = 1.9 * RTprev + rand(0,1) * (RTprev / 5)
1457 delay ^= (delay >> 16);
1459 frac = request->delay / 5;
1460 delay = ((frac >> 16) * delay) + (((frac & 0xffff) * delay) >> 16);
1462 delay += (2 * request->delay) - (request->delay / 10);
1465 * Cap delay at MRT, if MRT is non-zero.
1467 if (request->home_server->coa_mrt &&
1468 (delay > (request->home_server->coa_mrt * USEC))) {
1469 int mrt_usec = request->home_server->coa_mrt * USEC;
1472 * delay = MRT + RAND * MRT
1473 * = 0.9 MRT + rand(0,.2) * MRT
1476 delay ^= (delay >> 15);
1478 delay = ((mrt_usec >> 16) * delay) + (((mrt_usec & 0xffff) * delay) >> 16);
1479 delay += mrt_usec - (mrt_usec / 10);
1482 request->delay = delay;
1483 request->when = now;
1484 tv_add(&request->when, request->delay);
1485 mrd = request->proxy_when;
1486 mrd.tv_sec += request->home_server->coa_mrd;
1489 * Cap duration at MRD.
1491 if (timercmp(&mrd, &request->when, <)) {
1492 request->when = mrd;
1493 INSERT_EVENT(no_response_to_coa_request, request);
1496 INSERT_EVENT(retransmit_coa_request, request);
1499 if (update_event_timestamp(request->proxy, now.tv_sec)) {
1501 * Keep a copy of the old Id so that the
1502 * re-transmitted request doesn't re-use the old
1505 RADIUS_PACKET old = *request->proxy;
1506 home_server *home = request->home_server;
1507 rad_listen_t *listener = request->proxy_listener;
1510 * Don't free the old Id on error.
1512 if (!insert_into_proxy_hash(request)) {
1513 radlog(L_PROXY,"Failed to insert retransmission of CoA request into proxy list.");
1518 * Now that we have a new Id, free the old one
1519 * and update the various statistics.
1521 PTHREAD_MUTEX_LOCK(&proxy_mutex);
1522 fr_packet_list_yank(proxy_list, &old);
1523 fr_packet_list_id_free(proxy_list, &old);
1524 if (home) home->currently_outstanding--;
1526 if (listener) listener->count--;
1528 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
1530 } else { /* FIXME: protect by a mutex? */
1531 request->num_proxied_requests++;
1534 request->num_coa_requests++; /* is NOT reset by code 3 lines above! */
1536 request->proxy_listener->send(request->proxy_listener,
1542 * The original request is either DONE, or in CLEANUP_DELAY.
1544 static int originated_coa_request(REQUEST *request)
1546 int delay, rcode, pre_proxy_type = 0;
1552 rad_assert(request->proxy == NULL);
1553 rad_assert(!request->in_proxy_hash);
1554 rad_assert(request->proxy_reply == NULL);
1557 * Check whether we want to originate one, or cancel one.
1559 vp = pairfind(request->config_items, PW_SEND_COA_REQUEST, 0);
1560 if (!vp && request->coa) {
1561 vp = pairfind(request->coa->proxy->vps, PW_SEND_COA_REQUEST, 0);
1565 if (vp->vp_integer == 0) {
1566 ev_request_free(&request->coa);
1567 return 1; /* success */
1571 if (!request->coa) request_alloc_coa(request);
1572 if (!request->coa) return 0;
1577 * src_ipaddr will be set up in proxy_encode.
1579 memset(&ipaddr, 0, sizeof(ipaddr));
1580 vp = pairfind(coa->proxy->vps, PW_PACKET_DST_IP_ADDRESS, 0);
1582 ipaddr.af = AF_INET;
1583 ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
1585 } else if ((vp = pairfind(coa->proxy->vps,
1586 PW_PACKET_DST_IPV6_ADDRESS, 0)) != NULL) {
1587 ipaddr.af = AF_INET6;
1588 ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
1590 } else if ((vp = pairfind(coa->proxy->vps,
1591 PW_HOME_SERVER_POOL, 0)) != NULL) {
1592 coa->home_pool = home_pool_byname(vp->vp_strvalue,
1594 if (!coa->home_pool) {
1595 RDEBUG2("WARNING: No such home_server_pool %s",
1598 ev_request_free(&request->coa);
1605 } else if (request->client->coa_pool) {
1606 coa->home_pool = request->client->coa_pool;
1608 } else if (request->client->coa_server) {
1609 coa->home_server = request->client->coa_server;
1613 * If all else fails, send it to the client that
1614 * originated this request.
1616 memcpy(&ipaddr, &request->packet->src_ipaddr, sizeof(ipaddr));
1620 * Use the pool, if it exists.
1622 if (coa->home_pool) {
1623 coa->home_server = home_server_ldb(NULL, coa->home_pool, coa);
1624 if (!coa->home_server) {
1625 RDEBUG("WARNING: No live home server for home_server_pool %s", vp->vp_strvalue);
1629 } else if (!coa->home_server) {
1630 int port = PW_COA_UDP_PORT;
1632 vp = pairfind(coa->proxy->vps, PW_PACKET_DST_PORT, 0);
1633 if (vp) port = vp->vp_integer;
1635 coa->home_server = home_server_find(&ipaddr, port, IPPROTO_UDP);
1636 if (!coa->home_server) {
1637 RDEBUG2("WARNING: Unknown destination %s:%d for CoA request.",
1638 inet_ntop(ipaddr.af, &ipaddr.ipaddr,
1639 buffer, sizeof(buffer)), port);
1644 vp = pairfind(coa->proxy->vps, PW_PACKET_TYPE, 0);
1646 switch (vp->vp_integer) {
1647 case PW_COA_REQUEST:
1648 case PW_DISCONNECT_REQUEST:
1649 coa->proxy->code = vp->vp_integer;
1653 DEBUG("Cannot set CoA Packet-Type to code %d",
1659 if (!coa->proxy->code) coa->proxy->code = PW_COA_REQUEST;
1662 * The rest of the server code assumes that
1663 * request->packet && request->reply exist. Copy them
1664 * from the original request.
1666 rad_assert(coa->packet != NULL);
1667 rad_assert(coa->packet->vps == NULL);
1668 memcpy(coa->packet, request->packet, sizeof(*request->packet));
1669 coa->packet->vps = paircopy(request->packet->vps);
1670 coa->packet->data = NULL;
1671 rad_assert(coa->reply != NULL);
1672 rad_assert(coa->reply->vps == NULL);
1673 memcpy(coa->reply, request->reply, sizeof(*request->reply));
1674 coa->reply->vps = paircopy(request->reply->vps);
1675 coa->reply->data = NULL;
1676 coa->config_items = paircopy(request->config_items);
1679 * Call the pre-proxy routines.
1681 vp = pairfind(request->config_items, PW_PRE_PROXY_TYPE, 0);
1683 RDEBUG2(" Found Pre-Proxy-Type %s", vp->vp_strvalue);
1684 pre_proxy_type = vp->vp_integer;
1687 if (coa->home_pool && coa->home_pool->virtual_server) {
1688 const char *old_server = coa->server;
1690 coa->server = coa->home_pool->virtual_server;
1691 RDEBUG2(" server %s {", coa->server);
1692 rcode = module_pre_proxy(pre_proxy_type, coa);
1694 coa->server = old_server;
1696 rcode = module_pre_proxy(pre_proxy_type, coa);
1703 * Only send the CoA packet if the pre-proxy code succeeded.
1705 case RLM_MODULE_NOOP:
1707 case RLM_MODULE_UPDATED:
1712 * Source IP / port is set when the proxy socket
1715 coa->proxy->dst_ipaddr = coa->home_server->ipaddr;
1716 coa->proxy->dst_port = coa->home_server->port;
1718 if (!insert_into_proxy_hash(coa)) {
1719 radlog(L_PROXY, "Failed to insert CoA request into proxy list.");
1724 * We CANNOT divorce the CoA request from the parent
1725 * request. This function is running in a child thread,
1726 * and we need access to the main event loop in order to
1727 * to add the timers for the CoA packet. See
1732 * Forget about the original request completely at this
1737 gettimeofday(&request->proxy_when, NULL);
1738 request->received = request->next_when = request->proxy_when;
1739 rad_assert(request->proxy_reply == NULL);
1742 * Implement re-transmit algorithm as per RFC 5080
1745 * We want IRT + RAND*IRT
1746 * or 0.9 IRT + rand(0,.2) IRT
1748 * 2^20 ~ USEC, and we want 2.
1749 * rand(0,0.2) USEC ~ (rand(0,2^21) / 10)
1751 delay = (fr_rand() & ((1 << 22) - 1)) / 10;
1752 request->delay = delay * request->home_server->coa_irt;
1753 delay = request->home_server->coa_irt * USEC;
1754 delay -= delay / 10;
1755 delay += request->delay;
1757 request->delay = delay;
1758 tv_add(&request->next_when, delay);
1759 request->next_callback = retransmit_coa_request;
1762 * Note that we set proxied BEFORE sending the packet.
1764 * Once we send it, the request is tainted, as
1765 * another thread may have picked it up. Don't
1768 request->child_pid = NO_SUCH_CHILD_PID;
1770 update_event_timestamp(request->proxy, request->proxy_when.tv_sec);
1772 request->child_state = REQUEST_PROXIED;
1774 DEBUG_PACKET(request, request->proxy, 1);
1776 request->proxy_listener->send(request->proxy_listener,
1780 #endif /* WITH_COA */
1783 static int process_proxy_reply(REQUEST *request)
1786 int post_proxy_type = 0;
1790 * Delete any reply we had accumulated until now.
1792 pairfree(&request->reply->vps);
1795 * Run the packet through the post-proxy stage,
1796 * BEFORE playing games with the attributes.
1798 vp = pairfind(request->config_items, PW_POST_PROXY_TYPE, 0);
1800 RDEBUG2(" Found Post-Proxy-Type %s", vp->vp_strvalue);
1801 post_proxy_type = vp->vp_integer;
1804 if (request->home_pool && request->home_pool->virtual_server) {
1805 const char *old_server = request->server;
1807 request->server = request->home_pool->virtual_server;
1808 RDEBUG2(" server %s {", request->server);
1809 rcode = module_post_proxy(post_proxy_type, request);
1811 request->server = old_server;
1813 rcode = module_post_proxy(post_proxy_type, request);
1817 if (request->packet->code == request->proxy->code)
1819 * Don't run the next bit if we originated a CoA
1820 * packet, after receiving an Access-Request or
1821 * Accounting-Request.
1826 * There may NOT be a proxy reply, as we may be
1827 * running Post-Proxy-Type = Fail.
1829 if (request->proxy_reply) {
1831 * Delete the Proxy-State Attributes from
1832 * the reply. These include Proxy-State
1833 * attributes from us and remote server.
1835 pairdelete(&request->proxy_reply->vps, PW_PROXY_STATE, 0);
1838 * Add the attributes left in the proxy
1839 * reply to the reply list.
1841 pairadd(&request->reply->vps, request->proxy_reply->vps);
1842 request->proxy_reply->vps = NULL;
1845 * Free proxy request pairs.
1847 pairfree(&request->proxy->vps);
1851 default: /* Don't do anything */
1853 case RLM_MODULE_FAIL:
1854 /* FIXME: debug print stuff */
1855 request->child_state = REQUEST_DONE;
1858 case RLM_MODULE_HANDLED:
1859 /* FIXME: debug print stuff */
1860 request->child_state = REQUEST_DONE;
1868 static int request_pre_handler(REQUEST *request)
1872 rad_assert(request->magic == REQUEST_MAGIC);
1873 rad_assert(request->packet != NULL);
1875 request->child_state = REQUEST_RUNNING;
1878 * Don't decode the packet if it's an internal "fake"
1879 * request. Instead, just return so that the caller can
1882 if (request->packet->dst_port == 0) {
1883 request->username = pairfind(request->packet->vps,
1885 request->password = pairfind(request->packet->vps,
1886 PW_USER_PASSWORD, 0);
1892 * Put the decoded packet into it's proper place.
1894 if (request->proxy_reply != NULL) {
1895 rcode = request->proxy_listener->decode(request->proxy_listener, request);
1896 DEBUG_PACKET(request, request->proxy_reply, 0);
1899 * Pro-actively remove it from the proxy hash.
1900 * This is later than in 2.1.x, but it means that
1901 * the replies are authenticated before being
1902 * removed from the hash.
1905 (request->num_proxied_requests <= request->num_proxied_responses)) {
1906 remove_from_proxy_hash(request);
1911 if (request->packet->vps == NULL) {
1912 rcode = request->listener->decode(request->listener, request);
1914 if (debug_condition) {
1916 const char *my_debug = debug_condition;
1919 * Ignore parse errors.
1921 radius_evaluate_condition(request, RLM_MODULE_OK, 0,
1925 request->options = 2;
1926 request->radlog = radlog_request;
1930 DEBUG_PACKET(request, request->packet, 0);
1936 RDEBUG("%s Dropping packet without response.", fr_strerror());
1937 request->reply->offset = -2; /* bad authenticator */
1938 request->child_state = REQUEST_DONE;
1942 if (!request->username) {
1943 request->username = pairfind(request->packet->vps,
1948 if (request->proxy) {
1949 return process_proxy_reply(request);
1959 * Do state handling when we proxy a request.
1961 static int proxy_request(REQUEST *request)
1963 struct timeval when;
1968 RDEBUG("WARNING: Cannot proxy and originate CoA packets at the same time. Cancelling CoA request");
1969 ev_request_free(&request->coa);
1973 if (request->home_server->server) {
1974 RDEBUG("ERROR: Cannot proxy to a virtual server.");
1978 if (!insert_into_proxy_hash(request)) {
1979 radlog(L_PROXY, "Failed to insert request into proxy list.");
1983 request->proxy_listener->encode(request->proxy_listener, request);
1985 when = request->received;
1986 when.tv_sec += request->root->max_request_time;
1988 gettimeofday(&request->proxy_when, NULL);
1990 request->next_when = request->proxy_when;
1991 request->next_when.tv_sec += request->home_server->response_window;
1993 rad_assert(request->home_server->response_window > 0);
1995 if (timercmp(&when, &request->next_when, <)) {
1996 request->next_when = when;
1998 request->next_callback = no_response_to_proxied_request;
2000 RDEBUG2("Proxying request to home server %s port %d",
2001 inet_ntop(request->proxy->dst_ipaddr.af,
2002 &request->proxy->dst_ipaddr.ipaddr,
2003 buffer, sizeof(buffer)),
2004 request->proxy->dst_port);
2007 * Note that we set proxied BEFORE sending the packet.
2009 * Once we send it, the request is tainted, as
2010 * another thread may have picked it up. Don't
2013 #ifdef HAVE_PTHREAD_H
2014 request->child_pid = NO_SUCH_CHILD_PID;
2016 request->child_state = REQUEST_PROXIED;
2018 DEBUG_PACKET(request, request->proxy, 1);
2020 request->proxy_listener->send(request->proxy_listener,
2027 * "Proxy" the request by sending it to a new virtual server.
2029 static int proxy_to_virtual_server(REQUEST *request)
2032 RAD_REQUEST_FUNP fun;
2034 if (!request->home_server || !request->home_server->server) return 0;
2036 if (request->parent) {
2037 RDEBUG2("WARNING: Cancelling proxy request to virtual server %s as this request was itself proxied.", request->home_server->server);
2041 fake = request_alloc_fake(request);
2043 RDEBUG2("WARNING: Out of memory");
2047 fake->packet->vps = paircopy(request->proxy->vps);
2048 fake->server = request->home_server->server;
2050 if (request->proxy->code == PW_AUTHENTICATION_REQUEST) {
2051 fun = rad_authenticate;
2053 #ifdef WITH_ACCOUNTING
2054 } else if (request->proxy->code == PW_ACCOUNTING_REQUEST) {
2055 fun = rad_accounting;
2059 RDEBUG2("Unknown packet type %d", request->proxy->code);
2060 ev_request_free(&fake);
2064 RDEBUG2(">>> Sending proxied request internally to virtual server.");
2065 radius_handle_request(fake, fun);
2066 RDEBUG2("<<< Received proxied response code %d from internal virtual server.", fake->reply->code);
2068 if (fake->reply->code != 0) {
2069 request->proxy_reply = fake->reply;
2073 * There was no response
2075 setup_post_proxy_fail(request);
2078 ev_request_free(&fake);
2080 process_proxy_reply(request);
2083 * Process it through the normal section again, but ONLY
2084 * if we received a proxy reply..
2086 if (request->proxy_reply) {
2087 if (request->server) RDEBUG("server %s {",
2088 request->server != NULL ?
2089 request->server : "");
2092 if (request->server) RDEBUG("} # server %s",
2093 request->server != NULL ?
2094 request->server : "");
2097 return 2; /* success, but NOT '1' !*/
2101 * Return 1 if we did proxy it, or the proxy attempt failed
2102 * completely. Either way, the caller doesn't touch the request
2103 * any more if we return 1.
2105 static int successfully_proxied_request(REQUEST *request)
2108 int pre_proxy_type = 0;
2109 VALUE_PAIR *realmpair;
2110 VALUE_PAIR *strippedname;
2112 char *realmname = NULL;
2114 REALM *realm = NULL;
2118 * If it was already proxied, do nothing.
2120 * FIXME: This should really be a serious error.
2122 if (request->in_proxy_hash ||
2123 (request->proxy_reply && (request->proxy_reply->code != 0))) {
2127 realmpair = pairfind(request->config_items, PW_PROXY_TO_REALM, 0);
2128 if (!realmpair || (realmpair->length == 0)) {
2131 vp = pairfind(request->config_items, PW_HOME_SERVER_POOL, 0);
2134 switch (request->packet->code) {
2135 case PW_AUTHENTICATION_REQUEST:
2136 pool_type = HOME_TYPE_AUTH;
2139 #ifdef WITH_ACCOUNTING
2140 case PW_ACCOUNTING_REQUEST:
2141 pool_type = HOME_TYPE_ACCT;
2146 case PW_COA_REQUEST:
2147 case PW_DISCONNECT_REQUEST:
2148 pool_type = HOME_TYPE_COA;
2156 pool = home_pool_byname(vp->vp_strvalue, pool_type);
2158 RDEBUG2("ERROR: Cannot proxy to unknown pool %s",
2163 realmname = NULL; /* no realms */
2168 realmname = (char *) realmpair->vp_strvalue;
2170 realm = realm_find2(realmname);
2172 RDEBUG2("ERROR: Cannot proxy to unknown realm %s", realmname);
2177 * Figure out which pool to use.
2179 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
2180 pool = realm->auth_pool;
2182 #ifdef WITH_ACCOUNTING
2183 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
2184 pool = realm->acct_pool;
2188 } else if ((request->packet->code == PW_COA_REQUEST) ||
2189 (request->packet->code == PW_DISCONNECT_REQUEST)) {
2190 pool = realm->acct_pool;
2194 rad_panic("Internal sanity check failed");
2198 RDEBUG2(" WARNING: Cancelling proxy to Realm %s, as the realm is local.",
2204 home = home_server_ldb(realmname, pool, request);
2206 RDEBUG2("ERROR: Failed to find live home server for realm %s",
2210 request->home_pool = pool;
2214 * Once we've decided to proxy a request, we cannot send
2215 * a CoA packet. So we free up any CoA packet here.
2217 ev_request_free(&request->coa);
2220 * Remember that we sent the request to a Realm.
2222 if (realmname) pairadd(&request->packet->vps,
2223 pairmake("Realm", realmname, T_OP_EQ));
2226 * Strip the name, if told to.
2228 * Doing it here catches the case of proxied tunneled
2231 if (realm && (realm->striprealm == TRUE) &&
2232 (strippedname = pairfind(request->proxy->vps, PW_STRIPPED_USER_NAME, 0)) != NULL) {
2234 * If there's a Stripped-User-Name attribute in
2235 * the request, then use THAT as the User-Name
2236 * for the proxied request, instead of the
2239 * This is done by making a copy of the
2240 * Stripped-User-Name attribute, turning it into
2241 * a User-Name attribute, deleting the
2242 * Stripped-User-Name and User-Name attributes
2243 * from the vps list, and making the new
2244 * User-Name the head of the vps list.
2246 vp = pairfind(request->proxy->vps, PW_USER_NAME, 0);
2248 vp = radius_paircreate(request, NULL,
2249 PW_USER_NAME, 0, PW_TYPE_STRING);
2250 rad_assert(vp != NULL); /* handled by above function */
2251 /* Insert at the START of the list */
2252 vp->next = request->proxy->vps;
2253 request->proxy->vps = vp;
2255 memcpy(vp->vp_strvalue, strippedname->vp_strvalue,
2256 sizeof(vp->vp_strvalue));
2257 vp->length = strippedname->length;
2260 * Do NOT delete Stripped-User-Name.
2265 * If there is no PW_CHAP_CHALLENGE attribute but
2266 * there is a PW_CHAP_PASSWORD we need to add it
2267 * since we can't use the request authenticator
2268 * anymore - we changed it.
2270 if ((request->packet->code == PW_AUTHENTICATION_REQUEST) &&
2271 pairfind(request->proxy->vps, PW_CHAP_PASSWORD, 0) &&
2272 pairfind(request->proxy->vps, PW_CHAP_CHALLENGE, 0) == NULL) {
2273 vp = radius_paircreate(request, &request->proxy->vps,
2274 PW_CHAP_CHALLENGE, 0, PW_TYPE_OCTETS);
2275 vp->length = AUTH_VECTOR_LEN;
2276 memcpy(vp->vp_strvalue, request->packet->vector, AUTH_VECTOR_LEN);
2280 * The RFC's say we have to do this, but FreeRADIUS
2283 vp = radius_paircreate(request, &request->proxy->vps,
2284 PW_PROXY_STATE, 0, PW_TYPE_OCTETS);
2285 snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%d",
2286 request->packet->id);
2287 vp->length = strlen(vp->vp_strvalue);
2290 * Should be done BEFORE inserting into proxy hash, as
2291 * pre-proxy may use this information, or change it.
2293 request->proxy->code = request->packet->code;
2296 * Call the pre-proxy routines.
2298 vp = pairfind(request->config_items, PW_PRE_PROXY_TYPE, 0);
2300 RDEBUG2(" Found Pre-Proxy-Type %s", vp->vp_strvalue);
2301 pre_proxy_type = vp->vp_integer;
2304 rad_assert(request->home_pool != NULL);
2306 if (request->home_pool->virtual_server) {
2307 const char *old_server = request->server;
2309 request->server = request->home_pool->virtual_server;
2310 RDEBUG2(" server %s {", request->server);
2311 rcode = module_pre_proxy(pre_proxy_type, request);
2313 request->server = old_server;
2315 rcode = module_pre_proxy(pre_proxy_type, request);
2318 case RLM_MODULE_FAIL:
2319 case RLM_MODULE_INVALID:
2320 case RLM_MODULE_NOTFOUND:
2321 case RLM_MODULE_USERLOCK:
2323 /* FIXME: debug print failed stuff */
2326 case RLM_MODULE_REJECT:
2327 case RLM_MODULE_HANDLED:
2331 * Only proxy the packet if the pre-proxy code succeeded.
2333 case RLM_MODULE_NOOP:
2335 case RLM_MODULE_UPDATED:
2340 * If it's a fake request, don't send the proxy
2341 * packet. The outer tunnel session will take
2342 * care of doing that.
2344 if (request->packet->dst_port == 0) {
2345 request->home_server = NULL;
2349 if (request->home_server->server) {
2350 return proxy_to_virtual_server(request);
2353 if (!proxy_request(request)) {
2354 RDEBUG("ERROR: Failed to proxy request");
2362 static void request_post_handler(REQUEST *request)
2364 int child_state = -1;
2365 struct timeval when;
2368 if ((request->master_state == REQUEST_STOP_PROCESSING) ||
2370 (request->parent->master_state == REQUEST_STOP_PROCESSING))) {
2371 RDEBUG2("request was cancelled.");
2372 #ifdef HAVE_PTHREAD_H
2373 request->child_pid = NO_SUCH_CHILD_PID;
2375 child_state = REQUEST_DONE;
2379 if (request->child_state != REQUEST_RUNNING) {
2380 rad_panic("Internal sanity check failed");
2385 * If it's not in the request hash, it's a CoA request.
2388 if (!request->in_request_hash &&
2390 ((request->proxy->code == PW_COA_REQUEST) ||
2391 (request->proxy->code == PW_DISCONNECT_REQUEST))) {
2392 request->next_callback = NULL;
2393 child_state = REQUEST_DONE;
2399 * Catch Auth-Type := Reject BEFORE proxying the packet.
2401 if ((request->packet->code == PW_AUTHENTICATION_REQUEST) &&
2402 (request->reply->code == 0) &&
2403 ((vp = pairfind(request->config_items, PW_AUTH_TYPE, 0)) != NULL) &&
2404 (vp->vp_integer == PW_AUTHTYPE_REJECT)) {
2405 request->reply->code = PW_AUTHENTICATION_REJECT;
2409 if (request->root->proxy_requests &&
2410 !request->in_proxy_hash &&
2411 (request->reply->code == 0) &&
2412 (request->packet->dst_port != 0) &&
2413 (request->packet->code != PW_STATUS_SERVER)) {
2414 int rcode = successfully_proxied_request(request);
2416 if (rcode == 1) return; /* request is invalid */
2419 * Failed proxying it (dead home servers, etc.)
2420 * Run it through Post-Proxy-Type = Fail, and
2421 * respond to the request.
2423 * Note that we're in a child thread here, so we
2424 * do NOT re-schedule the request. Instead, we
2425 * do what we would have done, which is run the
2426 * pre-handler, a NULL request handler, and then
2429 if ((rcode < 0) && setup_post_proxy_fail(request)) {
2430 request_pre_handler(request);
2434 * Else we weren't supposed to proxy it,
2435 * OR we proxied it internally to a virutal server.
2440 else if (request->proxy && request->coa) {
2441 RDEBUG("WARNING: Cannot proxy and originate CoA packets at the same time. Cancelling CoA request");
2442 ev_request_free(&request->coa);
2448 * Fake requests don't get encoded or signed. The caller
2449 * also requires the reply VP's, so we don't free them
2452 if (request->packet->dst_port == 0) {
2453 /* FIXME: RDEBUG going to the next request */
2454 #ifdef HAVE_PTHREAD_H
2455 request->child_pid = NO_SUCH_CHILD_PID;
2457 request->child_state = REQUEST_DONE;
2463 * Copy Proxy-State from the request to the reply.
2465 vp = paircopy2(request->packet->vps, PW_PROXY_STATE, 0);
2466 if (vp) pairadd(&request->reply->vps, vp);
2470 * Access-Requests get delayed or cached.
2472 switch (request->packet->code) {
2473 case PW_AUTHENTICATION_REQUEST:
2474 gettimeofday(&request->next_when, NULL);
2476 if (request->reply->code == 0) {
2478 * Check if the lack of response is intentional.
2480 vp = pairfind(request->config_items,
2481 PW_RESPONSE_PACKET_TYPE, 0);
2483 RDEBUG2("There was no response configured: rejecting request");
2484 request->reply->code = PW_AUTHENTICATION_REJECT;
2486 } else if (vp->vp_integer == 256) {
2487 RDEBUG2("Not responding to request");
2490 * Force cleanup after a long
2491 * time, so that we don't
2492 * re-process the packet.
2494 request->next_when.tv_sec += request->root->max_request_time;
2495 request->next_callback = cleanup_delay;
2496 child_state = REQUEST_CLEANUP_DELAY;
2499 request->reply->code = vp->vp_integer;
2505 * Run rejected packets through
2507 * Post-Auth-Type = Reject
2509 if (request->reply->code == PW_AUTHENTICATION_REJECT) {
2510 pairdelete(&request->config_items, PW_POST_AUTH_TYPE, 0);
2511 vp = radius_pairmake(request, &request->config_items,
2512 "Post-Auth-Type", "Reject",
2514 if (vp) rad_postauth(request);
2517 * If configured, delay Access-Reject packets.
2519 * If request->root->reject_delay = 0, we discover
2520 * that we have to send the packet now.
2522 when = request->received;
2523 when.tv_sec += request->root->reject_delay;
2525 if (timercmp(&when, &request->next_when, >)) {
2526 RDEBUG2("Delaying reject for %d seconds",
2527 request->root->reject_delay);
2528 request->next_when = when;
2529 request->next_callback = reject_delay;
2530 #ifdef HAVE_PTHREAD_H
2531 request->child_pid = NO_SUCH_CHILD_PID;
2533 request->child_state = REQUEST_REJECT_DELAY;
2539 case PW_COA_REQUEST:
2540 case PW_DISCONNECT_REQUEST:
2542 request->next_when.tv_sec += request->root->cleanup_delay;
2543 request->next_callback = cleanup_delay;
2544 child_state = REQUEST_CLEANUP_DELAY;
2547 case PW_ACCOUNTING_REQUEST:
2548 request->next_callback = NULL; /* just to be safe */
2549 child_state = REQUEST_DONE;
2553 * FIXME: Status-Server should probably not be
2556 case PW_STATUS_SERVER:
2557 request->next_callback = NULL;
2558 child_state = REQUEST_DONE;
2565 request->next_callback = NULL;
2566 child_state = REQUEST_DONE;
2571 * Suppress "no reply" packets here, unless we're reading
2572 * from the "detail" file. In that case, we've got to
2573 * tell the detail file handler that the request is dead,
2574 * and it should re-send it.
2575 * If configured, encode, sign, and send.
2577 if ((request->reply->code != 0)
2579 || (request->listener->type == RAD_LISTEN_DETAIL)
2582 DEBUG_PACKET(request, request->reply, 1);
2583 request->listener->send(request->listener, request);
2588 * Now that we've completely processed the request,
2589 * see if we need to originate a CoA request. But ONLY
2590 * if it wasn't proxied.
2592 if (!request->proxy &&
2593 (request->packet->code != PW_COA_REQUEST) &&
2594 (request->packet->code != PW_DISCONNECT_REQUEST) &&
2596 (pairfind(request->config_items, PW_SEND_COA_REQUEST, 0) != NULL))) {
2597 if (!originated_coa_request(request)) {
2598 RDEBUG2("Do CoA Fail handler here");
2600 /* request->coa is stil set, so we can update events */
2606 * Clean up. These are no longer needed.
2608 pairfree(&request->config_items);
2610 pairfree(&request->packet->vps);
2611 request->username = NULL;
2612 request->password = NULL;
2614 pairfree(&request->reply->vps);
2617 if (request->proxy) {
2618 pairfree(&request->proxy->vps);
2620 if (request->proxy_reply) {
2621 pairfree(&request->proxy_reply->vps);
2626 * We're not tracking responses from the home
2627 * server, we can therefore free this memory in
2630 if (!request->in_proxy_hash) {
2631 rad_free(&request->proxy);
2632 rad_free(&request->proxy_reply);
2633 request->home_server = NULL;
2639 RDEBUG2("Finished request.");
2640 rad_assert(child_state >= 0);
2641 request->child_state = child_state;
2644 * Single threaded mode: update timers now.
2646 if (!have_children) wait_a_bit(request);
2651 static void rad_retransmit_packet(REQUEST *request)
2656 if (request->home_server->proto == IPPROTO_TCP) {
2657 DEBUG2("Suppressing duplicate proxied request to home server %s port %d proto TCP - ID: %d",
2658 inet_ntop(request->proxy->dst_ipaddr.af,
2659 &request->proxy->dst_ipaddr.ipaddr,
2660 buffer, sizeof(buffer)),
2661 request->proxy->dst_port,
2662 request->proxy->id);
2663 return; /* don't do anything else */
2667 RDEBUG2("Sending duplicate proxied request to home server %s port %d - ID: %d",
2668 inet_ntop(request->proxy->dst_ipaddr.af,
2669 &request->proxy->dst_ipaddr.ipaddr,
2670 buffer, sizeof(buffer)),
2671 request->proxy->dst_port,
2672 request->proxy->id);
2673 request->num_proxied_requests++;
2675 DEBUG_PACKET(request, request->proxy, 1);
2676 request->proxy_listener->send(request->proxy_listener,
2681 static int rad_retransmit(REQUEST *request)
2684 * If we've just discovered that the home server
2685 * is dead, OR the socket has been closed, look for
2686 * another connection to a home server.
2688 if ((request->home_server->state == HOME_STATE_IS_DEAD) ||
2689 (request->proxy_listener->status != RAD_LISTEN_STATUS_KNOWN)) {
2692 remove_from_proxy_hash(request);
2694 home = home_server_ldb(NULL, request->home_pool, request);
2696 RDEBUG2("ERROR: Failed to find live home server for request");
2699 * Do post-request processing,
2700 * and any insertion of necessary
2703 post_proxy_fail_handler(request);
2707 request->proxy->code = request->packet->code;
2710 * Free the old packet, to force re-encoding
2712 free(request->proxy->data);
2713 request->proxy->data = NULL;
2714 request->proxy->data_len = 0;
2717 * This request failed over to a virtual
2718 * server. Push it back onto the queue
2721 if (request->home_server->server) {
2722 proxy_fallback_handler(request);
2727 * Try to proxy the request.
2729 if (!proxy_request(request)) {
2730 RDEBUG("ERROR: Failed to re-proxy request");
2731 goto no_home_servers;
2734 } /* else the home server is still alive */
2736 rad_retransmit_packet(request);
2742 static void received_retransmit(REQUEST *request, const RADCLIENT *client)
2745 RAD_STATS_TYPE_INC(request->listener, total_dup_requests);
2746 RAD_STATS_CLIENT_INC(request->listener, client, total_dup_requests);
2748 switch (request->child_state) {
2749 case REQUEST_QUEUED:
2750 case REQUEST_RUNNING:
2754 radlog(L_ERR, "Discarding duplicate request from "
2755 "client %s port %d - ID: %d due to unfinished request %u",
2757 request->packet->src_port,request->packet->id,
2762 case REQUEST_PROXIED:
2764 * We're not supposed to have duplicate
2765 * accounting packets. The other states handle
2766 * duplicates fine (discard, or send duplicate
2767 * reply). But we do NOT want to retransmit an
2768 * accounting request here, because that would
2769 * involve updating the Acct-Delay-Time, and
2770 * therefore changing the packet Id, etc.
2772 * Instead, we just discard the packet. We may
2773 * eventually respond, or the client will send a
2774 * new accounting packet.
2776 * The same comments go for Status-Server, and
2777 * other packet types.
2779 * FIXME: coa: when we proxy CoA && Disconnect
2780 * packets, this logic has to be fixed.
2782 if (request->packet->code != PW_AUTHENTICATION_REQUEST) {
2786 check_for_zombie_home_server(request);
2789 * Home server is still alive, and the proxy
2790 * socket is OK. Just re-send the packet.
2792 if ((request->home_server->state != HOME_STATE_IS_DEAD) &&
2793 (request->proxy_listener->status == RAD_LISTEN_STATUS_KNOWN)) {
2794 rad_retransmit_packet(request);
2799 * Otherwise, we need to fail over to another
2800 * home server, and possibly run "post-proxy-type
2801 * fail". Add an event waiting for the child to
2804 INSERT_EVENT(wait_a_bit, request);
2806 request->priority = RAD_LISTEN_PROXY;
2807 thread_pool_addrequest(request, rad_retransmit);
2811 case REQUEST_REJECT_DELAY:
2812 RDEBUG2("Waiting to send Access-Reject "
2813 "to client %s port %d - ID: %d",
2815 request->packet->src_port, request->packet->id);
2818 case REQUEST_CLEANUP_DELAY:
2820 if (request->reply->code == 0) {
2821 RDEBUG2("Ignoring retransmit from client %s port %d "
2822 "- ID: %d, no reply was configured",
2824 request->packet->src_port, request->packet->id);
2829 * FIXME: This sends duplicate replies to
2830 * accounting requests, even if Acct-Delay-Time
2831 * or Event-Timestamp is in the packet. In those
2832 * cases, the Id should be changed, and the packet
2835 RDEBUG2("Sending duplicate reply "
2836 "to client %s port %d - ID: %d",
2838 request->packet->src_port, request->packet->id);
2839 DEBUG_PACKET(request, request->reply, 1);
2840 request->listener->send(request->listener, request);
2846 static void received_conflicting_request(REQUEST *request,
2847 const RADCLIENT *client)
2849 radlog(L_ERR, "Received conflicting packet from "
2850 "client %s port %d - ID: %d due to unfinished request %u. Giving up on old request.",
2852 request->packet->src_port, request->packet->id,
2856 * Nuke it from the request hash, so we can receive new
2859 remove_from_request_hash(request);
2861 switch (request->child_state) {
2863 * Tell it to stop, and wait for it to do so.
2866 request->master_state = REQUEST_STOP_PROCESSING;
2867 request->delay += request->delay >> 1;
2869 tv_add(&request->when, request->delay);
2871 INSERT_EVENT(wait_for_child_to_die, request);
2875 * Catch race conditions. It may have switched
2876 * from running to done while this code is being
2879 case REQUEST_REJECT_DELAY:
2880 case REQUEST_CLEANUP_DELAY:
2887 static int can_handle_new_request(RADIUS_PACKET *packet,
2889 struct main_config_t *root)
2892 * Count the total number of requests, to see if
2893 * there are too many. If so, return with an
2896 if (root->max_requests) {
2897 int request_count = fr_packet_list_num_elements(pl);
2900 * This is a new request. Let's see if
2901 * it makes us go over our configured
2904 if (request_count > root->max_requests) {
2905 radlog(L_ERR, "Dropping request (%d is too many): "
2906 "from client %s port %d - ID: %d", request_count,
2908 packet->src_port, packet->id);
2909 radlog(L_INFO, "WARNING: Please check the configuration file.\n"
2910 "\tThe value for 'max_requests' is probably set too low.\n");
2912 } /* else there were a small number of requests */
2913 } /* else there was no configured limit for requests */
2916 * FIXME: Add per-client checks. If one client is sending
2917 * too many packets, start discarding them.
2919 * We increment the counters here, and decrement them
2920 * when the response is sent... somewhere in this file.
2924 * FUTURE: Add checks for system load. If the system is
2925 * busy, start dropping requests...
2927 * We can probably keep some statistics ourselves... if
2928 * there are more requests coming in than we can handle,
2929 * start dropping some.
2936 int received_request(rad_listen_t *listener,
2937 RADIUS_PACKET *packet, REQUEST **prequest,
2940 RADIUS_PACKET **packet_p;
2941 REQUEST *request = NULL;
2942 struct main_config_t *root = &mainconfig;
2944 packet_p = fr_packet_list_find(pl, packet);
2946 request = fr_packet2myptr(REQUEST, packet, packet_p);
2947 rad_assert(request->in_request_hash);
2949 if ((request->packet->data_len == packet->data_len) &&
2950 (memcmp(request->packet->vector, packet->vector,
2951 sizeof(packet->vector)) == 0)) {
2952 received_retransmit(request, client);
2957 * The new request is different from the old one,
2958 * but maybe the old is finished. If so, delete
2961 switch (request->child_state) {
2962 struct timeval when;
2966 * Special hacks for race conditions.
2967 * The reply is encoded, and therefore
2968 * likely sent. We received a *new*
2969 * packet from the client, likely before
2970 * the next line or two of code which
2971 * updated the child state. In this
2972 * case, just accept the new request.
2974 if ((request->reply->code != 0) &&
2975 request->reply->data) {
2976 radlog(L_INFO, "WARNING: Allowing fast client %s port %d - ID: %d for recent request %u.",
2978 packet->src_port, packet->id,
2980 remove_from_request_hash(request);
2985 gettimeofday(&when, NULL);
2989 * If the cached request was received
2990 * within the last second, then we
2991 * discard the NEW request instead of the
2992 * old one. This will happen ONLY when
2993 * the client is severely broken, and is
2994 * sending conflicting packets very
2997 if (timercmp(&when, &request->received, <)) {
2998 radlog(L_ERR, "Discarding conflicting packet from "
2999 "client %s port %d - ID: %d due to recent request %u.",
3001 packet->src_port, packet->id,
3006 received_conflicting_request(request, client);
3010 case REQUEST_REJECT_DELAY:
3011 case REQUEST_CLEANUP_DELAY:
3012 request->child_state = REQUEST_DONE;
3014 cleanup_delay(request);
3021 * We may want to quench the new request.
3025 (listener->type != RAD_LISTEN_DETAIL) &&
3027 !can_handle_new_request(packet, client, root)) {
3032 * Create and initialize the new request.
3034 request = request_alloc(); /* never fails */
3036 if ((request->reply = rad_alloc(0)) == NULL) {
3037 radlog(L_ERR, "No memory");
3041 request->listener = listener;
3042 request->client = client;
3043 request->packet = packet;
3044 request->packet->timestamp = request->timestamp;
3045 request->number = request_num_counter++;
3046 request->priority = listener->type;
3047 #ifdef HAVE_PTHREAD_H
3048 request->child_pid = NO_SUCH_CHILD_PID;
3052 * Status-Server packets go to the head of the queue.
3054 if (request->packet->code == PW_STATUS_SERVER) request->priority = 0;
3057 * Set virtual server identity
3059 if (client->server) {
3060 request->server = client->server;
3061 } else if (listener->server) {
3062 request->server = listener->server;
3064 request->server = NULL;
3068 * Remember the request in the list.
3070 if (!fr_packet_list_insert(pl, &request->packet)) {
3071 radlog(L_ERR, "Failed to insert request %u in the list of live requests: discarding", request->number);
3072 ev_request_free(&request);
3076 request->in_request_hash = TRUE;
3077 request->root = root;
3080 request->listener->count++;
3084 * The request passes many of our sanity checks.
3085 * From here on in, if anything goes wrong, we
3086 * send a reject message, instead of dropping the
3091 * Build the reply template from the request.
3094 request->reply->sockfd = request->packet->sockfd;
3095 request->reply->dst_ipaddr = request->packet->src_ipaddr;
3096 request->reply->src_ipaddr = request->packet->dst_ipaddr;
3097 request->reply->dst_port = request->packet->src_port;
3098 request->reply->src_port = request->packet->dst_port;
3099 request->reply->id = request->packet->id;
3100 request->reply->code = 0; /* UNKNOWN code */
3101 memcpy(request->reply->vector, request->packet->vector,
3102 sizeof(request->reply->vector));
3103 request->reply->vps = NULL;
3104 request->reply->data = NULL;
3105 request->reply->data_len = 0;
3107 request->master_state = REQUEST_ACTIVE;
3108 request->child_state = REQUEST_QUEUED;
3109 request->next_callback = NULL;
3111 gettimeofday(&request->received, NULL);
3112 request->timestamp = request->received.tv_sec;
3113 request->when = request->received;
3115 request->delay = USEC;
3117 tv_add(&request->when, request->delay);
3119 INSERT_EVENT(wait_a_bit, request);
3121 *prequest = request;
3127 REQUEST *received_proxy_response(RADIUS_PACKET *packet)
3133 * Lookup *without* removal. In versions prior to 2.2.0,
3134 * this did lookup *and* removal. That method allowed
3135 * attackers to spoof replies that caused entries to be
3136 * removed from the proxy hash prior to validation.
3138 request = lookup_in_proxy_hash(packet);
3141 radlog(L_PROXY, "No outstanding request was found for 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);
3150 * There's a reply: discard it if it's a conflicting one.
3152 if (request->proxy_reply) {
3154 * ? The home server gave us a new proxy
3155 * reply which doesn't match the old
3158 if (memcmp(request->proxy_reply->vector,
3160 sizeof(request->proxy_reply->vector)) != 0) {
3161 RDEBUG2("Ignoring conflicting proxy reply");
3164 /* assert that there's an event queued for request? */
3166 } /* else it had previously passed verification */
3169 * Verify the packet before doing ANYTHING with
3170 * it. This means we're doing more MD5 checks in
3171 * the server core. However, we can fix that by
3172 * moving to multiple threads listening on
3175 * We do this AFTER looking the request up in the
3176 * hash, and AFTER checking if we saw a previous
3177 * request. This helps minimize the DoS effect
3178 * of people attacking us with spoofed packets.
3180 * FIXME: move the "read from proxy socket" code
3181 * into one (or more) threads. Have it read from
3182 * the socket, do the validation, and write a
3183 * pointer to the packet into a pipe? Or queue it
3184 * to the main server?
3186 } else if (rad_verify(packet, request->proxy,
3187 request->home_server->secret) != 0) {
3188 DEBUG("Ignoring spoofed proxy reply. Signature is invalid");
3193 * Check (again) if it's a duplicate reply. We do this
3194 * after deleting the packet from the proxy hash.
3196 if (request->proxy_reply) {
3197 RDEBUG2("Discarding duplicate reply from host %s port %d - ID: %d",
3198 inet_ntop(packet->src_ipaddr.af,
3199 &packet->src_ipaddr.ipaddr,
3200 buffer, sizeof(buffer)),
3201 packet->src_port, packet->id);
3204 gettimeofday(&now, NULL);
3207 * "ping" packets have a different algorithm for marking
3208 * a home server alive. They also skip all of the CoA,
3211 if (!request->packet) {
3212 request->proxy_reply = packet;
3214 rad_assert(request->home_server != NULL);
3215 if (request->home_server->proto != IPPROTO_TCP)
3217 received_response_to_ping(request);
3218 request->proxy_reply = NULL; /* caller will free it */
3219 ev_request_free(&request);
3224 * Maybe move this earlier in the decision process?
3225 * Having it here means that late or duplicate proxy
3226 * replies no longer get the home server marked as
3227 * "alive". This might be good for stability, though.
3229 * FIXME: Do we really want to do this whenever we
3230 * receive a packet? Setting this here means that we
3231 * mark it alive on *any* packet, even if it's lost all
3232 * of the *other* packets in the last 10s.
3234 * This behavior could be configurable.
3236 request->home_server->state = HOME_STATE_ALIVE;
3237 request->home_server->last_packet = now.tv_sec;
3241 * When originating CoA, the "proxy" reply is the reply
3242 * to the CoA request that we originated. At this point,
3243 * the original request is finished, and it has a reply.
3245 * However, if we haven't separated the two requests, do
3246 * so now. This is done so that cleaning up the original
3247 * request won't cause the CoA request to be free'd. See
3248 * util.c, request_free()
3250 if (request->parent && (request->parent->coa == request)) {
3251 request->parent->coa = NULL;
3252 request->parent = NULL;
3255 * The proxied packet was different from the
3256 * original packet, AND the proxied packet was
3259 } else if ((request->packet->code != request->proxy->code) &&
3260 ((request->proxy->code == PW_COA_REQUEST) ||
3261 (request->proxy->code == PW_DISCONNECT_REQUEST))) {
3263 * It's already divorced: do nothing.
3268 * Skip the next set of checks, as the original
3269 * reply is cached. We want to be able to still
3270 * process the CoA reply, AND to reference the
3271 * original request/reply.
3273 * This is getting to be really quite a bit of a
3279 * If there's a reply to the NAS, ignore everything
3280 * related to proxy responses
3282 if (request->reply && request->reply->code != 0) {
3283 RDEBUG2("Ignoring proxy reply that arrived after we sent a reply to the NAS");
3289 * The average includes our time to receive packets and
3290 * look them up in the hashes, which should be the same
3293 * We update the response time only for the FIRST packet
3296 if (request->home_server->ema.window > 0) {
3297 radius_stats_ema(&request->home_server->ema,
3298 &now, &request->proxy_when);
3302 switch (request->child_state) {
3303 case REQUEST_QUEUED:
3304 case REQUEST_RUNNING:
3305 radlog(L_ERR, "Internal sanity check failed for child state");
3308 case REQUEST_REJECT_DELAY:
3309 case REQUEST_CLEANUP_DELAY:
3311 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'",
3312 inet_ntop(packet->src_ipaddr.af,
3313 &packet->src_ipaddr.ipaddr,
3314 buffer, sizeof(buffer)),
3315 packet->src_port, packet->id,
3317 /* assert that there's an event queued for request? */
3320 case REQUEST_PROXIED:
3324 request->proxy_reply = packet;
3328 * Perform RTT calculations, as per RFC 2988 (for TCP).
3329 * Note that we only do so on the first response.
3331 if ((request->num_proxied_responses == 1)
3333 home_server *home = request->home_server;
3335 rtt = now.tv_sec - request->proxy_when.tv_sec;
3338 rtt -= request->proxy_when.tv_usec;
3340 if (!home->has_rtt) {
3341 home->has_rtt = TRUE;
3344 home->rttvar = rtt / 2;
3347 home->rttvar -= home->rttvar >> 2;
3348 home->rttvar += (home->srtt - rtt);
3349 home->srtt -= home->srtt >> 3;
3350 home->srtt += rtt >> 3;
3353 home->rto = home->srtt;
3354 if (home->rttvar > (USEC / 4)) {
3355 home->rto += home->rttvar * 4;
3362 request->child_state = REQUEST_QUEUED;
3363 request->when = now;
3364 request->delay = USEC;
3365 request->priority = RAD_LISTEN_PROXY;
3366 tv_add(&request->when, request->delay);
3369 * Wait a bit will take care of max_request_time
3371 INSERT_EVENT(wait_a_bit, request);
3376 #endif /* WITH_PROXY */
3379 static void tcp_socket_lifetime(void *ctx)
3381 rad_listen_t *listener = ctx;
3384 listener->print(listener, buffer, sizeof(buffer));
3386 DEBUG("Reached maximum lifetime on socket %s", buffer);
3388 listener->status = RAD_LISTEN_STATUS_CLOSED;
3389 event_new_fd(listener);
3392 static void tcp_socket_idle_timeout(void *ctx)
3394 rad_listen_t *listener = ctx;
3395 listen_socket_t *sock = listener->data;
3398 fr_event_now(el, &now); /* should always succeed... */
3400 rad_assert(sock->home != NULL);
3403 * We implement idle timeout by polling, because it's
3404 * cheaper than resetting the idle timeout every time
3405 * we send / receive a packet.
3407 if ((sock->last_packet + sock->home->idle_timeout) > now.tv_sec) {
3408 struct timeval when;
3409 void *fun = tcp_socket_idle_timeout;
3411 when.tv_sec = sock->last_packet;
3412 when.tv_sec += sock->home->idle_timeout;
3415 if (sock->home->lifetime &&
3416 (sock->opened + sock->home->lifetime < when.tv_sec)) {
3417 when.tv_sec = sock->opened + sock->home->lifetime;
3418 fun = tcp_socket_lifetime;
3421 if (!fr_event_insert(el, fun, listener, &when, &sock->ev)) {
3422 rad_panic("Failed to insert event");
3428 listener->print(listener, buffer, sizeof(buffer));
3430 DEBUG("Reached idle timeout on socket %s", buffer);
3432 listener->status = RAD_LISTEN_STATUS_CLOSED;
3433 event_new_fd(listener);
3437 int event_new_fd(rad_listen_t *this)
3441 if (this->status == RAD_LISTEN_STATUS_KNOWN) return 1;
3443 this->print(this, buffer, sizeof(buffer));
3445 if (this->status == RAD_LISTEN_STATUS_INIT) {
3447 DEBUG("Listening on %s", buffer);
3449 radlog(L_INFO, " ... adding new socket %s", buffer);
3454 * Add it to the list of sockets we can use.
3455 * Server sockets (i.e. auth/acct) are never
3456 * added to the packet list.
3458 if (this->type == RAD_LISTEN_PROXY) {
3459 listen_socket_t *sock = this->data;
3461 PTHREAD_MUTEX_LOCK(&proxy_mutex);
3462 if (!fr_packet_list_socket_add(proxy_list, this->fd,
3464 &sock->other_ipaddr, sock->other_port,
3467 proxy_no_new_sockets = TRUE;
3468 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
3471 * This is bad. However, the
3472 * packet list now supports 256
3473 * open sockets, which should
3474 * minimize this problem.
3476 radlog(L_ERR, "Failed adding proxy socket: %s",
3482 sock->home->num_connections++;
3485 * If necessary, add it to the list of
3486 * new proxy listeners.
3488 if (sock->home->lifetime || sock->home->idle_timeout) {
3489 this->next = proxy_listener_list;
3490 proxy_listener_list = this;
3493 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
3496 * Tell the main thread that we've added
3497 * a proxy listener, but only if we need
3498 * to update the event list. Do this
3499 * with the mutex unlocked, to reduce
3503 if (sock->home->lifetime || sock->home->idle_timeout) {
3504 radius_signal_self(RADIUS_SIGNAL_SELF_NEW_FD);
3512 * Detail files are always known, and aren't
3513 * put into the socket event loop.
3515 if (this->type == RAD_LISTEN_DETAIL) {
3516 this->status = RAD_LISTEN_STATUS_KNOWN;
3519 * Set up the first poll interval.
3521 event_poll_detail(this);
3526 FD_MUTEX_LOCK(&fd_mutex);
3527 if (!fr_event_fd_insert(el, 0, this->fd,
3528 event_socket_handler, this)) {
3529 radlog(L_ERR, "Failed adding event handler for proxy socket!");
3532 FD_MUTEX_UNLOCK(&fd_mutex);
3534 this->status = RAD_LISTEN_STATUS_KNOWN;
3539 * Something went wrong with the socket: make it harmless.
3541 if (this->status == RAD_LISTEN_STATUS_REMOVE_FD) {
3545 * Remove it from the list of live FD's.
3547 FD_MUTEX_LOCK(&fd_mutex);
3548 fr_event_fd_delete(el, 0, this->fd);
3549 FD_MUTEX_UNLOCK(&fd_mutex);
3553 * We track requests using this socket only for
3554 * TCP. For UDP, we don't currently close
3558 if (this->type != RAD_LISTEN_PROXY)
3561 if (this->count != 0) {
3562 fr_packet_list_walk(pl, this,
3563 remove_all_requests);
3566 if (this->count == 0) {
3567 this->status = RAD_LISTEN_STATUS_FINISH;
3578 PTHREAD_MUTEX_LOCK(&proxy_mutex);
3579 if (!fr_packet_list_socket_freeze(proxy_list,
3581 radlog(L_ERR, "Fatal error freezing socket: %s",
3587 * Doing this with the proxy mutex held
3588 * is a Bad Thing. We should move to
3589 * finer-grained mutexes.
3591 count = this->count;
3593 fr_packet_list_walk(proxy_list, this,
3594 remove_all_proxied_requests);
3596 count = this->count; /* protected by mutex */
3597 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
3600 this->status = RAD_LISTEN_STATUS_FINISH;
3604 #endif /* WITH_PROXY */
3605 #endif /* WITH_TCP */
3608 * Re-open the socket, pointing it to /dev/null.
3609 * This means that all writes proceed without
3610 * blocking, and all reads return "no data".
3612 * This leaves the socket active, so any child
3613 * threads won't go insane. But it means that
3614 * they cannot send or receive any packets.
3616 * This is EXTRA work in the normal case, when
3617 * sockets are closed without error. But it lets
3618 * us have one simple processing method for all
3621 devnull = open("/dev/null", O_RDWR);
3623 radlog(L_ERR, "FATAL failure opening /dev/null: %s",
3627 if (dup2(devnull, this->fd) < 0) {
3628 radlog(L_ERR, "FATAL failure closing socket: %s",
3634 this->status = RAD_LISTEN_STATUS_CLOSED;
3637 * Fall through to the next section.
3643 * Called ONLY from the main thread. On the following
3649 * (and falling through from "forcibly close FD" above)
3650 * client closed connection on us
3651 * client sent us a bad packet.
3653 if (this->status == RAD_LISTEN_STATUS_CLOSED) {
3654 int count = this->count;
3657 rad_assert(this->type != RAD_LISTEN_DETAIL);
3662 * Remove it from the list of active sockets, so
3663 * that it isn't used when proxying new packets.
3665 if (this->type == RAD_LISTEN_PROXY) {
3666 PTHREAD_MUTEX_LOCK(&proxy_mutex);
3667 if (!fr_packet_list_socket_freeze(proxy_list,
3669 radlog(L_ERR, "Fatal error freezing socket: %s",
3673 count = this->count; /* protected by mutex */
3674 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
3679 * Requests are still using the socket. Wait for
3683 struct timeval when;
3684 listen_socket_t *sock = this->data;
3687 * Try again to clean up the socket in 30
3690 gettimeofday(&when, NULL);
3693 if (!fr_event_insert(el,
3694 (fr_event_callback_t) event_new_fd,
3695 this, &when, &sock->ev)) {
3696 rad_panic("Failed to insert event");
3703 * No one is using this socket: we can delete it
3706 this->status = RAD_LISTEN_STATUS_FINISH;
3710 if (this->status == RAD_LISTEN_STATUS_FINISH) {
3711 listen_socket_t *sock = this->data;
3713 rad_assert(this->count == 0);
3714 radlog(L_INFO, " ... closing socket %s", buffer);
3717 * Remove it from the list of live FD's. Note
3718 * that it MAY also have been removed above. We
3719 * do it again here, to catch the case of sockets
3720 * closing on idle timeout, or max
3721 * lifetime... AFTER all requests have finished
3724 FD_MUTEX_LOCK(&fd_mutex);
3725 fr_event_fd_delete(el, 0, this->fd);
3726 FD_MUTEX_UNLOCK(&fd_mutex);
3730 * Remove it from the list of sockets to be used
3733 if (this->type == RAD_LISTEN_PROXY) {
3734 PTHREAD_MUTEX_LOCK(&proxy_mutex);
3735 if (!fr_packet_list_socket_remove(proxy_list,
3737 radlog(L_ERR, "Fatal error removing socket: %s",
3741 if (sock->home) sock->home->num_connections--;
3742 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
3747 * Remove any pending cleanups.
3749 if (sock->ev) fr_event_delete(el, &sock->ev);
3752 * And finally, close the socket.
3756 #endif /* WITH_TCP */
3761 static void handle_signal_self(int flag)
3763 if ((flag & (RADIUS_SIGNAL_SELF_EXIT | RADIUS_SIGNAL_SELF_TERM)) != 0) {
3764 if ((flag & RADIUS_SIGNAL_SELF_EXIT) != 0) {
3765 radlog(L_INFO, "Received TERM signal");
3766 fr_event_loop_exit(el, 1);
3768 fr_event_loop_exit(el, 2);
3772 } /* else exit/term flags weren't set */
3775 * Tell the even loop to stop processing.
3777 if ((flag & RADIUS_SIGNAL_SELF_HUP) != 0) {
3779 static time_t last_hup = 0;
3782 if ((int) (when - last_hup) < 5) {
3783 radlog(L_INFO, "Ignoring HUP (less than 5s since last one)");
3787 radlog(L_INFO, "Received HUP signal.");
3791 fr_event_loop_exit(el, 0x80);
3795 if ((flag & RADIUS_SIGNAL_SELF_DETAIL) != 0) {
3799 * FIXME: O(N) loops suck.
3801 for (this = mainconfig.listen;
3803 this = this->next) {
3804 if (this->type != RAD_LISTEN_DETAIL) continue;
3807 * This one didn't send the signal, skip
3810 if (!this->decode(this, NULL)) continue;
3813 * Go service the interrupt.
3815 event_poll_detail(this);
3823 * Add event handlers for idle timeouts && maximum lifetime.
3825 if ((flag & RADIUS_SIGNAL_SELF_NEW_FD) != 0) {
3826 struct timeval when;
3829 fr_event_now(el, &now);
3831 PTHREAD_MUTEX_LOCK(&proxy_mutex);
3833 while (proxy_listener_list) {
3834 rad_listen_t *this = proxy_listener_list;
3835 listen_socket_t *sock = this->data;
3837 proxy_listener_list = this->next;
3840 if (!sock->home) continue; /* skip UDP sockets */
3844 if (!sock->home->idle_timeout) {
3845 rad_assert(sock->home->lifetime != 0);
3847 when.tv_sec += sock->home->lifetime;
3848 fun = tcp_socket_lifetime;
3850 rad_assert(sock->home->idle_timeout != 0);
3852 when.tv_sec += sock->home->idle_timeout;
3853 fun = tcp_socket_idle_timeout;
3856 if (!fr_event_insert(el, fun, this, &when,
3858 rad_panic("Failed to insert event");
3862 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
3864 #endif /* WITH_PROXY */
3865 #endif /* WITH_TCP */
3868 #ifndef WITH_SELF_PIPE
3869 void radius_signal_self(int flag)
3871 handle_signal_self(flag);
3875 * Inform ourselves that we received a signal.
3877 void radius_signal_self(int flag)
3883 * The read MUST be non-blocking for this to work.
3885 rcode = read(self_pipe[0], buffer, sizeof(buffer));
3889 for (i = 0; i < rcode; i++) {
3890 buffer[0] |= buffer[i];
3898 write(self_pipe[1], buffer, 1);
3902 static void event_signal_handler(UNUSED fr_event_list_t *xel,
3903 UNUSED int fd, UNUSED void *ctx)
3908 rcode = read(self_pipe[0], buffer, sizeof(buffer));
3909 if (rcode <= 0) return;
3912 * Merge pending signals.
3914 for (i = 0; i < rcode; i++) {
3915 buffer[0] |= buffer[i];
3918 handle_signal_self(buffer[0]);
3923 static void event_socket_handler(fr_event_list_t *xel, UNUSED int fd,
3926 rad_listen_t *listener = ctx;
3927 RAD_REQUEST_FUNP fun;
3930 rad_assert(xel == el);
3936 (listener->type != RAD_LISTEN_DETAIL) &&
3938 (listener->fd < 0)) {
3941 listener->print(listener, buffer, sizeof(buffer));
3942 radlog(L_ERR, "FATAL: Asked to read from closed socket: %s",
3945 rad_panic("Socket was closed on us!");
3949 if (!listener->recv(listener, &fun, &request)) return;
3951 rad_assert(fun != NULL);
3952 rad_assert(request != NULL);
3954 thread_pool_addrequest(request, fun);
3959 * This function is called periodically to see if this detail
3960 * file is available for reading.
3962 static void event_poll_detail(void *ctx)
3965 rad_listen_t *this = ctx;
3966 struct timeval when;
3967 listen_detail_t *detail = this->data;
3969 rad_assert(this->type == RAD_LISTEN_DETAIL);
3971 event_socket_handler(el, this->fd, this);
3973 fr_event_now(el, &now);
3977 * Backdoor API to get the delay until the next poll
3980 delay = this->encode(this, NULL);
3981 tv_add(&when, delay);
3983 if (!fr_event_insert(el, event_poll_detail, this,
3984 &when, &detail->ev)) {
3985 radlog(L_ERR, "Failed creating handler");
3991 static void event_status(struct timeval *wake)
3993 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
3997 if (debug_flag == 0) {
3999 radlog(L_INFO, "Ready to process requests.");
4000 just_started = FALSE;
4006 radlog(L_INFO, "Ready to process requests.");
4008 } else if ((wake->tv_sec != 0) ||
4009 (wake->tv_usec >= 100000)) {
4010 DEBUG("Waking up in %d.%01u seconds.",
4011 (int) wake->tv_sec, (unsigned int) wake->tv_usec / 100000);
4016 * FIXME: Put this somewhere else, where it isn't called
4017 * all of the time...
4020 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
4022 * If there are no child threads, then there may
4023 * be child processes. In that case, wait for
4024 * their exit status, and throw that exit status
4025 * away. This helps get rid of zxombie children.
4027 while (waitpid(-1, &argval, WNOHANG) > 0) {
4035 * Externally-visibly functions.
4037 int radius_event_init(CONF_SECTION *cs, int spawn_flag)
4039 rad_listen_t *head = NULL;
4043 time(&fr_start_time);
4045 el = fr_event_list_create(event_status);
4048 pl = fr_packet_list_create(0);
4049 if (!pl) return 0; /* leak el */
4051 request_num_counter = 0;
4054 if (mainconfig.proxy_requests) {
4056 * Create the tree for managing proxied requests and
4059 proxy_list = fr_packet_list_create(1);
4060 if (!proxy_list) return 0;
4062 #ifdef HAVE_PTHREAD_H
4063 if (pthread_mutex_init(&proxy_mutex, NULL) != 0) {
4064 radlog(L_ERR, "FATAL: Failed to initialize proxy mutex: %s",
4072 #ifdef HAVE_PTHREAD_H
4074 NO_SUCH_CHILD_PID = (pthread_t ) (0);
4076 NO_SUCH_CHILD_PID = pthread_self(); /* not a child thread */
4079 * Initialize the threads ONLY if we're spawning, AND
4080 * we're running normally.
4082 if (spawn_flag && !check_config &&
4083 (thread_pool_init(cs, &spawn_flag) < 0)) {
4089 * Move all of the thread calls to this file?
4091 * It may be best for the mutexes to be in this file...
4093 have_children = spawn_flag;
4096 DEBUG("%s: #### Skipping IP addresses and Ports ####",
4098 if (listen_init(cs, &head) < 0) {
4105 #ifdef WITH_SELF_PIPE
4107 * Child threads need a pipe to signal us, as do the
4110 if (pipe(self_pipe) < 0) {
4111 radlog(L_ERR, "radiusd: Error opening internal pipe: %s",
4115 if (fcntl(self_pipe[0], F_SETFL, O_NONBLOCK | FD_CLOEXEC) < 0) {
4116 radlog(L_ERR, "radiusd: Error setting internal flags: %s",
4120 if (fcntl(self_pipe[1], F_SETFL, O_NONBLOCK | FD_CLOEXEC) < 0) {
4121 radlog(L_ERR, "radiusd: Error setting internal flags: %s",
4126 if (!fr_event_fd_insert(el, 0, self_pipe[0],
4127 event_signal_handler, el)) {
4128 radlog(L_ERR, "Failed creating handler for signals");
4131 #endif /* WITH_SELF_PIPE */
4133 DEBUG("%s: #### Opening IP addresses and Ports ####",
4137 * The server temporarily switches to an unprivileged
4138 * user very early in the bootstrapping process.
4139 * However, some sockets MAY require privileged access
4140 * (bind to device, or to port < 1024, or to raw
4141 * sockets). Those sockets need to call suid up/down
4142 * themselves around the functions that need a privileged
4145 if (listen_init(cs, &head) < 0) {
4149 mainconfig.listen = head;
4152 * At this point, no one has any business *ever* going
4155 fr_suid_down_permanent();
4161 static int request_hash_cb(UNUSED void *ctx, void *data)
4163 REQUEST *request = fr_packet2myptr(REQUEST, packet, data);
4166 rad_assert(request->in_proxy_hash == FALSE);
4169 ev_request_free(&request);
4176 static int proxy_hash_cb(UNUSED void *ctx, void *data)
4178 REQUEST *request = fr_packet2myptr(REQUEST, proxy, data);
4180 ev_request_free(&request);
4186 void radius_event_free(void)
4189 * FIXME: Stop all threads, or at least check that
4190 * they're all waiting on the semaphore, and the queues
4196 * There are requests in the proxy hash that aren't
4197 * referenced from anywhere else. Remove them first.
4200 fr_packet_list_walk(proxy_list, NULL, proxy_hash_cb);
4201 fr_packet_list_free(proxy_list);
4206 fr_packet_list_walk(pl, NULL, request_hash_cb);
4208 fr_packet_list_free(pl);
4211 fr_event_list_free(el);
4214 int radius_event_process(void)
4218 return fr_event_loop(el);
4221 void radius_handle_request(REQUEST *request, RAD_REQUEST_FUNP fun)
4223 request->options = RAD_REQUEST_OPTION_DEBUG2;
4225 if (request_pre_handler(request)) {
4226 rad_assert(fun != NULL);
4227 rad_assert(request != NULL);
4229 if (request->server) RDEBUG("server %s {",
4230 request->server != NULL ?
4231 request->server : "");
4234 if (request->server) RDEBUG("} # server %s",
4235 request->server != NULL ?
4236 request->server : "");
4238 request_post_handler(request);
4241 DEBUG2("Going to the next request");