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 void force_log_reopen(void);
47 extern char *debug_condition;
50 * Ridiculous amounts of local state.
52 static fr_event_list_t *el = NULL;
53 static fr_packet_list_t *pl = NULL;
54 static int request_num_counter = 0;
55 static struct timeval now;
57 static int have_children;
58 static int just_started = TRUE;
62 #define WITH_SELF_PIPE (1)
67 static int self_pipe[2];
72 static pthread_mutex_t proxy_mutex;
75 #define PTHREAD_MUTEX_LOCK if (have_children) pthread_mutex_lock
76 #define PTHREAD_MUTEX_UNLOCK if (have_children) pthread_mutex_unlock
78 static pthread_t NO_SUCH_CHILD_PID;
81 * This is easier than ifdef's throughout the code.
83 #define PTHREAD_MUTEX_LOCK(_x)
84 #define PTHREAD_MUTEX_UNLOCK(_x)
85 int thread_pool_addrequest(REQUEST *request, RAD_REQUEST_FUNP fun)
87 radius_handle_request(request, fun);
92 #define INSERT_EVENT(_function, _ctx) if (!fr_event_insert(el, _function, _ctx, &((_ctx)->when), &((_ctx)->ev))) { _rad_panic(__FILE__, __LINE__, "Failed to insert event"); }
95 static fr_packet_list_t *proxy_list = NULL;
98 * We keep the proxy FD's here. The RADIUS Id's are marked
99 * "allocated" per Id, via a bit per proxy FD.
101 static int proxy_fds[32];
102 static rad_listen_t *proxy_listeners[32];
104 #define remove_from_proxy_hash(foo)
107 static void request_post_handler(REQUEST *request);
108 static void wait_a_bit(void *ctx);
109 static void event_socket_handler(fr_event_list_t *xel, UNUSED int fd, void *ctx);
111 static void event_poll_detail(void *ctx);
114 static void NEVER_RETURNS _rad_panic(const char *file, unsigned int line,
117 radlog(L_ERR, "[%s:%d] %s", file, line, msg);
121 #define rad_panic(x) _rad_panic(__FILE__, __LINE__, x)
124 static void tv_add(struct timeval *tv, int usec_delay)
126 if (usec_delay > USEC) {
127 tv->tv_sec += usec_delay / USEC;
130 tv->tv_usec += usec_delay;
132 if (tv->tv_usec > USEC) {
138 static void remove_from_request_hash(REQUEST *request)
140 if (!request->in_request_hash) return;
142 fr_packet_list_yank(pl, request->packet);
143 request->in_request_hash = FALSE;
145 request_stats_final(request);
148 request->listener->count--;
151 * The TCP socket was closed, but we've got to
152 * hang around until done.
154 if ((request->listener->status == RAD_LISTEN_STATUS_FINISH) &&
155 (request->listener->count == 0)) {
156 listen_free(&request->listener);
163 static REQUEST *lookup_in_proxy_hash(RADIUS_PACKET *reply)
165 RADIUS_PACKET **proxy_p;
168 PTHREAD_MUTEX_LOCK(&proxy_mutex);
169 proxy_p = fr_packet_list_find_byreply(proxy_list, reply);
172 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
176 request = fr_packet2myptr(REQUEST, proxy, proxy_p);
179 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
183 request->num_proxied_responses++;
186 * Catch the most common case of everything working
189 if (request->num_proxied_requests == request->num_proxied_responses) {
190 fr_packet_list_yank(proxy_list, request->proxy);
191 fr_packet_list_id_free(proxy_list, request->proxy);
192 request->in_proxy_hash = FALSE;
196 * On the FIRST reply, decrement the count of outstanding
197 * requests. Note that this is NOT the count of sent
198 * packets, but whether or not the home server has
201 if (!request->proxy_reply &&
202 request->home_server &&
203 request->home_server->currently_outstanding) {
204 request->home_server->currently_outstanding--;
207 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
213 static void remove_from_proxy_hash(REQUEST *request)
216 * Check this without grabbing the mutex because it's a
217 * lot faster that way.
219 if (!request->in_proxy_hash) return;
222 * The "not in hash" flag is definitive. However, if the
223 * flag says that it IS in the hash, there might still be
224 * a race condition where it isn't.
226 PTHREAD_MUTEX_LOCK(&proxy_mutex);
228 if (!request->in_proxy_hash) {
229 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
233 fr_packet_list_yank(proxy_list, request->proxy);
237 * This should be hit when accounting packets don't have
238 * responses, or when the home server is down.
240 if (request->home_server->proto == IPPROTO_TCP) {
241 fr_tcp_radius_t *tcp;
243 tcp = fr_listen2tcp(request->proxy_listener);
244 if (tcp && tcp->ids[request->proxy->id]) {
245 tcp->ids[request->proxy->id] = NULL;
250 fr_packet_list_id_free(proxy_list, request->proxy);
253 * The home server hasn't replied, but we've given up on
254 * this request. Don't count this request against the
257 if (!request->proxy_reply &&
258 request->home_server &&
259 request->home_server->currently_outstanding) {
260 request->home_server->currently_outstanding--;
264 * Got from YES in hash, to NO, not in hash while we hold
265 * the mutex. This guarantees that when another thread
266 * grans the mutex, the "not in hash" flag is correct.
268 request->in_proxy_hash = FALSE;
270 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
272 #endif /* WITH_PROXY */
274 static void ev_request_free(REQUEST **prequest)
278 if (!prequest || !*prequest) return;
285 * Divorce the child from the parent first,
286 * then clean up the child.
288 request->coa->parent = NULL;
289 ev_request_free(&request->coa);
293 * Divorce the parent from the child, and leave the
294 * parent still alive.
296 if (request->parent && (request->parent->coa == request)) {
297 request->parent->coa = NULL;
301 if (request->ev) fr_event_delete(el, &request->ev);
303 if (request->in_proxy_hash) remove_from_proxy_hash(request);
305 if (request->in_request_hash) remove_from_request_hash(request);
307 request_free(prequest);
311 static int proxy_id_alloc(REQUEST *request, RADIUS_PACKET *packet)
314 rad_listen_t *proxy_listener;
316 if (fr_packet_list_id_alloc(proxy_list, packet)) return 1;
319 * Allocate a new proxy fd. This function adds
320 * it to the tail of the list of listeners. With
321 * some care, this can be thread-safe.
323 proxy_listener = proxy_new_listener(&packet->src_ipaddr, FALSE);
324 if (!proxy_listener) {
325 RDEBUG2("ERROR: Failed to create a new socket for proxying requests.");
333 proxy = proxy_listener->fd;
334 for (i = 0; i < 32; i++) {
336 * Found a free entry. Save the socket,
337 * and remember where we saved it.
339 if (proxy_fds[(proxy + i) & 0x1f] == -1) {
340 found = (proxy + i) & 0x1f;
341 proxy_fds[found] = proxy;
342 proxy_listeners[found] = proxy_listener;
346 rad_assert(found >= 0);
348 if (!fr_packet_list_socket_add(proxy_list, proxy_listener->fd)) {
349 RDEBUG2("ERROR: Failed to create a new socket for proxying requests.");
354 if (!fr_packet_list_id_alloc(proxy_list, packet)) {
355 RDEBUG2("ERROR: Failed to create a new socket for proxying requests.");
360 * Signal the main thread to add the new FD to the list
363 radius_signal_self(RADIUS_SIGNAL_SELF_NEW_FD);
368 static int insert_into_proxy_hash(REQUEST *request, int retransmit)
373 rad_assert(request->proxy != NULL);
374 rad_assert(proxy_list != NULL);
376 PTHREAD_MUTEX_LOCK(&proxy_mutex);
379 * Keep track of maximum outstanding requests to a
380 * particular home server. 'max_outstanding' is
381 * enforced in home_server_ldb(), in realms.c.
383 if (request->home_server) {
384 request->home_server->currently_outstanding++;
388 if (request->home_server->proto == IPPROTO_TCP) {
389 rad_listen_t *this = NULL;
390 fr_tcp_radius_t *tcp = NULL;
393 * FIXME: Order the TCP sockets by most recently
394 * used, so that we keep the TCP pipe full.
396 * FIXME: Try to use the same TCP connection for
397 * all packets of an EAP conversation. This will
400 request->proxy->id = -1;
401 for (i = 0; i < request->home_server->max_connections; i++) {
402 this = request->home_server->listeners[i];
405 tcp = fr_listen2tcp(this);
409 * If max requests per connection are
410 * set, and we've reached that limit,
411 * then skip the connection. It will be
412 * closed later, when all of the
413 * responses come back.
415 if (request->home_server->max_requests &&
416 (tcp->num_packets >= request->home_server->max_requests)) {
421 * FIXME: This is inefficient. Use
422 * something else, like MRU.
424 for (i = 0; i < 256; i++) {
425 if (tcp->ids[i]) continue;
427 request->proxy->id = i;
431 if (request->proxy->id >= 0) break;
434 if (request->proxy->id < 0) {
436 * Allocate new listener for this home
439 * FIXME: split the "connect" portion
440 * into a separate call, so it can happen
441 * when the mutex is NOT held!
443 this = proxy_new_tcp_listener(request->home_server);
445 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
446 DEBUG2("ERROR: Failed to allocate id");
450 tcp = fr_listen2tcp(this);
453 rad_assert(tcp->used == 0);
454 rad_assert(tcp->ids[0] == NULL);
455 request->proxy->id = 0;
458 * FIXME: Start timers as per RFC 3539
463 rad_assert(tcp != NULL);
464 rad_assert(tcp->ids[request->proxy->id] == NULL);
466 tcp->ids[request->proxy->id] = &request->proxy;
467 request->proxy_listener = this;
468 request->proxy->sockfd = tcp->fd;
469 request->proxy->src_ipaddr = tcp->src_ipaddr;
470 request->proxy->dst_ipaddr = tcp->dst_ipaddr;
471 request->proxy->src_port = tcp->src_port;
472 request->proxy->dst_port = tcp->dst_port;
476 if (!fr_packet_list_insert(proxy_list, &request->proxy)) {
477 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
478 DEBUG2("ERROR: Failed to insert entry into proxy list");
483 tcp->ids[request->proxy->id] = &request->proxy;
490 RADIUS_PACKET packet;
492 packet = *request->proxy;
494 if (!proxy_id_alloc(request, &packet)) {
495 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
500 * Yank the request, free the old Id, and
501 * remember the new Id.
503 fr_packet_list_yank(proxy_list, request->proxy);
504 fr_packet_list_id_free(proxy_list, request->proxy);
505 *request->proxy = packet;
507 } else if (!proxy_id_alloc(request, request->proxy)) {
508 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
512 rad_assert(request->proxy->sockfd >= 0);
515 * FIXME: Hack until we get rid of rad_listen_t, and put
516 * the information into the packet_list.
519 for (i = 0; i < 32; i++) {
520 if (proxy_fds[i] == request->proxy->sockfd) {
527 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
528 RDEBUG2("ERROR: All sockets are full.");
532 rad_assert(proxy_fds[proxy] != -1);
533 rad_assert(proxy_listeners[proxy] != NULL);
534 request->proxy_listener = proxy_listeners[proxy];
536 if (!fr_packet_list_insert(proxy_list, &request->proxy)) {
537 fr_packet_list_id_free(proxy_list, request->proxy);
538 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
539 RDEBUG2("ERROR: Failed to insert entry into proxy list");
547 request->in_proxy_hash = TRUE;
549 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
551 RDEBUG3(" proxy: allocating destination %s port %d - Id %d",
552 inet_ntop(request->proxy->dst_ipaddr.af,
553 &request->proxy->dst_ipaddr.ipaddr, buf, sizeof(buf)),
554 request->proxy->dst_port,
562 * Called as BOTH an event, and in-line from other functions.
564 static void wait_for_proxy_id_to_expire(void *ctx)
566 REQUEST *request = ctx;
568 rad_assert(request->magic == REQUEST_MAGIC);
569 rad_assert(request->proxy != NULL);
571 if (!fr_event_now(el, &now)) gettimeofday(&now, NULL);
572 request->when = request->proxy_when;
575 if (((request->proxy->code == PW_COA_REQUEST) ||
576 (request->proxy->code == PW_DISCONNECT_REQUEST)) &&
577 (request->packet->code != request->proxy->code)) {
578 request->when.tv_sec += request->home_server->coa_mrd;
581 request->when.tv_sec += request->home_server->response_window;
583 if ((request->num_proxied_requests == request->num_proxied_responses) ||
585 (request->home_server->proto == IPPROTO_TCP) ||
587 timercmp(&now, &request->when, >)) {
588 if (request->packet) {
589 RDEBUG2("Cleaning up request %d ID %d with timestamp +%d",
590 request->number, request->packet->id,
591 (unsigned int) (request->timestamp - fr_start_time));
593 RDEBUG2("Cleaning up request %d with timestamp +%d",
595 (unsigned int) (request->timestamp - fr_start_time));
598 ev_request_free(&request);
602 INSERT_EVENT(wait_for_proxy_id_to_expire, request);
606 #ifdef HAVE_PTHREAD_H
607 static void wait_for_child_to_die(void *ctx)
609 REQUEST *request = ctx;
611 rad_assert(request->magic == REQUEST_MAGIC);
613 if ((request->child_state == REQUEST_QUEUED) |
614 (request->child_state == REQUEST_RUNNING)) {
615 request->delay += (request->delay >> 1);
616 tv_add(&request->when, request->delay);
618 RDEBUG2("Child is still stuck for request %d", request->number);
620 INSERT_EVENT(wait_for_child_to_die, request);
624 RDEBUG2("Child is finally responsive for request %d", request->number);
625 remove_from_request_hash(request);
628 if (request->proxy) {
629 wait_for_proxy_id_to_expire(request);
634 ev_request_free(&request);
638 static void cleanup_delay(void *ctx)
640 REQUEST *request = ctx;
642 rad_assert(request->magic == REQUEST_MAGIC);
643 rad_assert((request->child_state == REQUEST_CLEANUP_DELAY) ||
644 (request->child_state == REQUEST_DONE));
646 remove_from_request_hash(request);
649 if (request->proxy && request->in_proxy_hash) {
650 wait_for_proxy_id_to_expire(request);
655 RDEBUG2("Cleaning up request %d ID %d with timestamp +%d",
656 request->number, request->packet->id,
657 (unsigned int) (request->timestamp - fr_start_time));
659 ev_request_free(&request);
664 * In daemon mode, AND this request has debug flags set.
666 #define DEBUG_PACKET if (!debug_flag && request->options && request->radlog) debug_packet
668 static void debug_packet(REQUEST *request, RADIUS_PACKET *packet, int direction)
672 const char *received, *from;
673 const fr_ipaddr_t *ip;
678 rad_assert(request->radlog != NULL);
680 if (direction == 0) {
681 received = "Received";
682 from = "from"; /* what else? */
683 ip = &packet->src_ipaddr;
684 port = packet->src_port;
687 received = "Sending";
688 from = "to"; /* hah! */
689 ip = &packet->dst_ipaddr;
690 port = packet->dst_port;
694 * Client-specific debugging re-prints the input
695 * packet into the client log.
697 * This really belongs in a utility library
699 if ((packet->code > 0) && (packet->code < FR_MAX_PACKET_CODE)) {
700 RDEBUG("%s %s packet %s host %s port %d, id=%d, length=%d",
701 received, fr_packet_codes[packet->code], from,
702 inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
703 port, packet->id, packet->data_len);
705 RDEBUG("%s packet %s host %s port %d code=%d, id=%d, length=%d",
707 inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
709 packet->code, packet->id, packet->data_len);
712 for (vp = packet->vps; vp != NULL; vp = vp->next) {
713 vp_prints(buffer, sizeof(buffer), vp);
714 request->radlog(L_DBG, 0, request, "\t%s", buffer);
718 static void reject_delay(void *ctx)
720 REQUEST *request = ctx;
722 rad_assert(request->magic == REQUEST_MAGIC);
723 rad_assert(request->child_state == REQUEST_REJECT_DELAY);
725 RDEBUG2("Sending delayed reject for request %d", request->number);
727 DEBUG_PACKET(request, request->reply, 1);
729 request->listener->send(request->listener, request);
731 request->when.tv_sec += request->root->cleanup_delay;
732 request->child_state = REQUEST_CLEANUP_DELAY;
734 INSERT_EVENT(cleanup_delay, request);
739 void revive_home_server(void *ctx)
741 home_server *home = ctx;
745 rad_assert(home->proto != IPPROTO_TCP);
748 home->state = HOME_STATE_ALIVE;
749 home->currently_outstanding = 0;
750 home->revive_time = now;
753 * Delete any outstanding events.
755 if (home->ev) fr_event_delete(el, &home->ev);
757 radlog(L_INFO, "PROXY: Marking home server %s port %d alive again... we have no idea if it really is alive or not.",
758 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
759 buffer, sizeof(buffer)),
765 static void no_response_to_ping(void *ctx)
767 REQUEST *request = ctx;
771 rad_assert(request->home_server != NULL);
773 home = request->home_server;
775 rad_assert(home->proto != IPPROTO_TCP);
778 home->num_received_pings = 0;
780 RDEBUG2("No response to status check %d from home server %s port %d",
782 inet_ntop(request->proxy->dst_ipaddr.af,
783 &request->proxy->dst_ipaddr.ipaddr,
784 buffer, sizeof(buffer)),
785 request->proxy->dst_port);
787 wait_for_proxy_id_to_expire(request);
791 static void received_response_to_ping(REQUEST *request)
796 rad_assert(request->home_server != NULL);
798 home = request->home_server;
800 rad_assert(home->proto != IPPROTO_TCP);
803 home->num_received_pings++;
805 RDEBUG2("Received response to status check %d (%d in current sequence)",
806 request->number, home->num_received_pings);
809 * Remove the request from any hashes
811 fr_event_delete(el, &request->ev);
812 remove_from_proxy_hash(request);
813 rad_assert(request->in_request_hash == FALSE);
816 * The control socket may have marked the home server as
817 * alive. OR, it may have suddenly started responding to
818 * requests again. If so, don't re-do the "make alive"
821 if (home->state == HOME_STATE_ALIVE) return;
824 * We haven't received enough ping responses to mark it
825 * "alive". Wait a bit.
827 if (home->num_received_pings < home->num_pings_to_alive) {
831 home->state = HOME_STATE_ALIVE;
832 home->currently_outstanding = 0;
833 home->revive_time = now;
835 if (!fr_event_delete(el, &home->ev)) {
836 RDEBUG2("Hmm... no event for home server. Oh well.");
839 radlog(L_INFO, "PROXY: Marking home server %s port %d alive",
840 inet_ntop(request->proxy->dst_ipaddr.af,
841 &request->proxy->dst_ipaddr.ipaddr,
842 buffer, sizeof(buffer)),
843 request->proxy->dst_port);
848 * Called from start of zombie period, OR after control socket
849 * marks the home server dead.
851 static void ping_home_server(void *ctx)
854 home_server *home = ctx;
859 rad_assert(home->proto != IPPROTO_TCP);
862 if ((home->state == HOME_STATE_ALIVE) ||
863 (home->ping_check == HOME_PING_CHECK_NONE) ||
864 (home->ev != NULL)) {
868 request = request_alloc();
869 request->number = request_num_counter++;
871 request->proxy = rad_alloc(1);
872 rad_assert(request->proxy != NULL);
874 fr_event_now(el, &request->when);
875 home->when = request->when;
877 if (home->ping_check == HOME_PING_CHECK_STATUS_SERVER) {
878 request->proxy->code = PW_STATUS_SERVER;
880 radius_pairmake(request, &request->proxy->vps,
881 "Message-Authenticator", "0x00", T_OP_SET);
883 } else if (home->type == HOME_TYPE_AUTH) {
884 request->proxy->code = PW_AUTHENTICATION_REQUEST;
886 radius_pairmake(request, &request->proxy->vps,
887 "User-Name", home->ping_user_name, T_OP_SET);
888 radius_pairmake(request, &request->proxy->vps,
889 "User-Password", home->ping_user_password, T_OP_SET);
890 radius_pairmake(request, &request->proxy->vps,
891 "Service-Type", "Authenticate-Only", T_OP_SET);
892 radius_pairmake(request, &request->proxy->vps,
893 "Message-Authenticator", "0x00", T_OP_SET);
896 #ifdef WITH_ACCOUNTING
897 request->proxy->code = PW_ACCOUNTING_REQUEST;
899 radius_pairmake(request, &request->proxy->vps,
900 "User-Name", home->ping_user_name, T_OP_SET);
901 radius_pairmake(request, &request->proxy->vps,
902 "Acct-Status-Type", "Stop", T_OP_SET);
903 radius_pairmake(request, &request->proxy->vps,
904 "Acct-Session-Id", "00000000", T_OP_SET);
905 vp = radius_pairmake(request, &request->proxy->vps,
906 "Event-Timestamp", "0", T_OP_SET);
907 vp->vp_date = now.tv_sec;
909 rad_assert("Internal sanity check failed");
913 radius_pairmake(request, &request->proxy->vps,
914 "NAS-Identifier", "Status Check. Are you alive?",
917 request->proxy->dst_ipaddr = home->ipaddr;
918 request->proxy->dst_port = home->port;
919 request->home_server = home;
921 rad_assert(request->proxy_listener == NULL);
923 if (!insert_into_proxy_hash(request, FALSE)) {
924 RDEBUG2("ERROR: Failed inserting status check %d into proxy hash. Discarding it.",
926 ev_request_free(&request);
929 rad_assert(request->proxy_listener != NULL);
930 request->proxy_listener->send(request->proxy_listener,
933 request->next_callback = NULL;
934 request->child_state = REQUEST_PROXIED;
935 request->when.tv_sec += home->ping_timeout;;
937 INSERT_EVENT(no_response_to_ping, request);
940 * Add +/- 2s of jitter, as suggested in RFC 3539
941 * and in the Issues and Fixes draft.
943 home->when.tv_sec += home->ping_interval - 2;
946 jitter ^= (jitter >> 10);
947 jitter &= ((1 << 23) - 1); /* 22 bits of 1 */
949 tv_add(&home->when, jitter);
951 INSERT_EVENT(ping_home_server, home);
955 void mark_home_server_dead(home_server *home, struct timeval *when)
957 int previous_state = home->state;
960 radlog(L_INFO, "PROXY: Marking home server %s port %d as dead.",
961 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
962 buffer, sizeof(buffer)),
965 home->state = HOME_STATE_IS_DEAD;
966 home->num_received_pings = 0;
968 if (home->ping_check != HOME_PING_CHECK_NONE) {
970 * If the control socket marks us dead, start
971 * pinging. Otherwise, we already started
972 * pinging when it was marked "zombie".
974 if (previous_state == HOME_STATE_ALIVE) {
975 ping_home_server(home);
980 * Revive it after a fixed period of time. This
981 * is very, very, bad.
984 home->when.tv_sec += home->revive_interval;
986 INSERT_EVENT(revive_home_server, home);
990 static void check_for_zombie_home_server(REQUEST *request)
995 home = request->home_server;
997 if (home->state != HOME_STATE_ZOMBIE) return;
999 when = home->zombie_period_start;
1000 when.tv_sec += home->zombie_period;
1002 fr_event_now(el, &now);
1003 if (timercmp(&now, &when, <)) {
1007 mark_home_server_dead(home, &request->when);
1010 static int proxy_to_virtual_server(REQUEST *request);
1012 static int virtual_server_handler(UNUSED REQUEST *request)
1014 proxy_to_virtual_server(request);
1018 static void proxy_fallback_handler(REQUEST *request)
1021 * A proper time is required for wait_a_bit.
1023 request->delay = USEC / 10;
1024 gettimeofday(&now, NULL);
1025 request->next_when = now;
1026 tv_add(&request->next_when, request->delay);
1027 request->next_callback = wait_a_bit;
1030 * Re-queue the request.
1032 request->child_state = REQUEST_QUEUED;
1034 rad_assert(request->proxy != NULL);
1035 if (!thread_pool_addrequest(request, virtual_server_handler)) {
1036 request->child_state = REQUEST_DONE;
1039 #ifdef HAVE_PTHREAD_H
1041 * MAY free the request if we're over max_request_time,
1042 * AND we're not in threaded mode!
1044 * Note that we call this ONLY if we're threaded, as
1045 * if we're NOT threaded, request_post_handler() calls
1046 * wait_a_bit(), which means that "request" may not
1049 if (have_children) wait_a_bit(request);
1054 static int setup_post_proxy_fail(REQUEST *request)
1056 DICT_VALUE *dval = NULL;
1059 request->child_state = REQUEST_RUNNING;
1061 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
1062 dval = dict_valbyname(PW_POST_PROXY_TYPE, "Fail-Authentication");
1064 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
1065 dval = dict_valbyname(PW_POST_PROXY_TYPE, "Fail-Accounting");
1069 * See no_response_to_coa_request
1071 } else if (((request->packet->code >> 8) & 0xff) == PW_COA_REQUEST) {
1072 request->packet->code &= 0xff; /* restore it */
1074 if (request->proxy->code == PW_COA_REQUEST) {
1075 dval = dict_valbyname(PW_POST_PROXY_TYPE, "Fail-CoA");
1077 } else if (request->proxy->code == PW_DISCONNECT_REQUEST) {
1078 dval = dict_valbyname(PW_POST_PROXY_TYPE, "Fail-Disconnect");
1088 if (!dval) dval = dict_valbyname(PW_POST_PROXY_TYPE, "Fail");
1091 pairdelete(&request->config_items, PW_POST_PROXY_TYPE);
1095 vp = pairfind(request->config_items, PW_POST_PROXY_TYPE);
1096 if (!vp) vp = radius_paircreate(request, &request->config_items,
1097 PW_POST_PROXY_TYPE, PW_TYPE_INTEGER);
1098 vp->vp_integer = dval->value;
1100 rad_assert(request->proxy_reply == NULL);
1106 static int null_handler(UNUSED REQUEST *request)
1111 static void post_proxy_fail_handler(REQUEST *request)
1114 * A proper time is required for wait_a_bit.
1116 request->delay = USEC / 10;
1117 gettimeofday(&now, NULL);
1120 * Not set up to run Post-Proxy-Type = Fail.
1122 * Mark the request as still running, and figure out what
1125 if (!setup_post_proxy_fail(request)) {
1126 request_post_handler(request);
1130 * Re-queue the request.
1132 request->child_state = REQUEST_QUEUED;
1135 * There is a post-proxy-type of fail. We run
1136 * the request through the pre/post proxy
1137 * handlers, just like it was a real proxied
1138 * request. However, we set the per-request
1139 * handler to NULL, as we don't want to do
1142 * Note that when we're not threaded, this will
1143 * process the request even if it's greater than
1144 * max_request_time. That's not fatal.
1146 request->priority = 0;
1147 rad_assert(request->proxy != NULL);
1148 thread_pool_addrequest(request, null_handler);
1152 * MAY free the request if we're over max_request_time,
1153 * AND we're not in threaded mode!
1155 * Note that we call this ONLY if we're threaded, as
1156 * if we're NOT threaded, request_post_handler() calls
1157 * wait_a_bit(), which means that "request" may not
1160 if (have_children) wait_a_bit(request);
1164 static void no_response_to_tcp_request(REQUEST *request)
1166 fr_tcp_radius_t *tcp;
1167 rad_listen_t *proxy_listener;
1169 proxy_listener = request->proxy_listener;
1170 tcp = fr_listen2tcp(proxy_listener);
1171 /* MAY be NULL if proxy_listener is NULL */
1174 * May have been closed by someone else.
1176 if (!request->proxy_listener) return;
1178 rad_assert(tcp != NULL);
1179 rad_assert(tcp->state != HOME_STATE_IS_DEAD);
1182 * There's already an event set up to mark it dead.
1184 if (tcp->state == HOME_STATE_ZOMBIE) {
1185 rad_assert(tcp->ev != NULL);
1190 * Enable the zombie period when we notice that the home
1191 * server hasn't responded. We do NOT back-date the start
1192 * of the zombie period.
1194 if (tcp->state == HOME_STATE_ALIVE) {
1196 radlog(L_ERR, "PROXY: Marking TCP connection to %s port %d as zombie (it looks like it is dead).",
1197 inet_ntop(tcp->dst_ipaddr.af, &tcp->dst_ipaddr.ipaddr,
1198 buffer, sizeof(buffer)),
1200 tcp->state = HOME_STATE_ZOMBIE;
1203 #endif /* WITH_TCP */
1205 /* maybe check this against wait_for_proxy_id_to_expire? */
1206 static void no_response_to_proxied_request(void *ctx)
1208 REQUEST *request = ctx;
1212 rad_assert(request->magic == REQUEST_MAGIC);
1213 rad_assert(request->child_state == REQUEST_PROXIED);
1216 * If we've failed over to an internal home server,
1217 * replace the callback with the correct one. This
1218 * is due to locking issues with child threads...
1220 if (request->home_server->server) {
1221 wait_a_bit(request);
1225 home = request->home_server;
1229 * If there's no response, then the connection is likely
1232 if (home->proto == IPPROTO_TCP) {
1233 no_response_to_tcp_request(request);
1236 check_for_zombie_home_server(request);
1239 * The default as of 2.1.7 is to allow requests to
1240 * fail-over to a backup home server when this one does
1241 * not respond. The old behavior can be configured as
1244 if (home->no_response_fail) {
1245 radlog(L_ERR, "Rejecting request %d (proxy Id %d) due to lack of any response from home server %s port %d",
1246 request->number, request->proxy->id,
1247 inet_ntop(request->proxy->dst_ipaddr.af,
1248 &request->proxy->dst_ipaddr.ipaddr,
1249 buffer, sizeof(buffer)),
1250 request->proxy->dst_port);
1252 post_proxy_fail_handler(request);
1256 * Don't touch request due to race conditions
1258 if (home->state == HOME_STATE_IS_DEAD) {
1259 rad_assert(home->ev != NULL); /* or it will never wake up */
1264 * Enable the zombie period when we notice that the home
1265 * server hasn't responded. We do NOT back-date the start
1266 * of the zombie period.
1268 if (home->state == HOME_STATE_ALIVE) {
1269 home->state = HOME_STATE_ZOMBIE;
1270 home->zombie_period_start = now;
1272 radlog(L_ERR, "PROXY: Marking home server %s port %d as zombie (it looks like it is dead).",
1273 inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
1274 buffer, sizeof(buffer)),
1278 * Start pinging the home server.
1280 ping_home_server(home);
1285 static void wait_a_bit(void *ctx)
1287 struct timeval when;
1288 REQUEST *request = ctx;
1289 fr_event_callback_t callback = NULL;
1291 rad_assert(request->magic == REQUEST_MAGIC);
1294 * The socket was closed. Tell the request that
1295 * there is no point in continuing.
1297 if (request->listener->status != RAD_LISTEN_STATUS_KNOWN) {
1298 goto stop_processing;
1303 * The CoA request is a new (internally generated)
1304 * request, created in a child thread. We therefore need
1305 * some way to tie its events back into the main event
1308 if (request->coa && !request->coa->proxy_reply &&
1309 request->coa->next_callback) {
1310 request->coa->when = request->coa->next_when;
1311 INSERT_EVENT(request->coa->next_callback, request->coa);
1312 request->coa->next_callback = NULL;
1313 request->coa->parent = NULL;
1314 request->coa = NULL;
1318 switch (request->child_state) {
1319 case REQUEST_QUEUED:
1320 case REQUEST_RUNNING:
1321 when = request->received;
1322 when.tv_sec += request->root->max_request_time;
1325 * Normally called from the event loop with the
1326 * proper event loop time. Otherwise, called from
1327 * post proxy fail handler, which sets "now", and
1328 * this call won't re-set it, because we're not
1329 * in the event loop.
1331 fr_event_now(el, &now);
1334 * Request still has more time. Continue
1337 if (timercmp(&now, &when, <) ||
1338 ((request->listener->type == RAD_LISTEN_DETAIL) &&
1339 (request->child_state == REQUEST_QUEUED))) {
1340 if (request->delay < (USEC / 10)) {
1341 request->delay = USEC / 10;
1343 request->delay += request->delay >> 1;
1347 * Cap wait at some sane value for detail
1350 if ((request->listener->type == RAD_LISTEN_DETAIL) &&
1351 (request->delay > (request->root->max_request_time * USEC))) {
1352 request->delay = request->root->max_request_time * USEC;
1356 request->when = now;
1357 tv_add(&request->when, request->delay);
1358 callback = wait_a_bit;
1362 #if defined(HAVE_PTHREAD_H) || defined(WITH_PROXY)
1364 * A child thread MAY still be running on the
1365 * request. Ask the thread to stop working on
1368 if (have_children) {
1369 /* FIXME: kill unresponsive children? */
1372 * Print this error message ONLY if
1373 * there's a child currently processing
1374 * the request. As we don't have thread
1375 * locks here, there may be race
1376 * conditions on this check. But it's
1377 * just an error message, so that's OK.
1379 if (!pthread_equal(request->child_pid, NO_SUCH_CHILD_PID)) {
1380 radlog(L_ERR, "WARNING: Unresponsive child for request %d, in module %s component %s",
1382 request->module ? request->module : "<server core>",
1383 request->component ? request->component : "<server core>");
1387 request->master_state = REQUEST_STOP_PROCESSING;
1389 request->delay = USEC / 4;
1390 tv_add(&request->when, request->delay);
1391 callback = wait_for_child_to_die;
1394 #else /* no child threads */
1399 * Else there are no child threads. We probably
1400 * should have just marked the request as 'done'
1401 * elsewhere, like in the post-proxy-fail
1402 * handler. But doing that would involve
1403 * checking for max_request_time in multiple
1404 * places, so this may be simplest.
1406 request->child_state = REQUEST_DONE;
1410 * Mark the request as no longer running,
1414 #ifdef HAVE_PTHREAD_H
1415 request->child_pid = NO_SUCH_CHILD_PID;
1420 * This is a CoA request. It's been divorced
1421 * from everything else, so we clean it up now.
1423 if (!request->in_request_hash &&
1425 (request->packet->code != request->proxy->code) &&
1426 ((request->proxy->code == PW_COA_REQUEST) ||
1427 (request->proxy->code == PW_DISCONNECT_REQUEST))) {
1429 * FIXME: Do CoA MIBs
1431 ev_request_free(&request);
1435 request_stats_final(request);
1436 cleanup_delay(request);
1439 case REQUEST_REJECT_DELAY:
1440 case REQUEST_CLEANUP_DELAY:
1441 #ifdef HAVE_PTHREAD_H
1442 request->child_pid = NO_SUCH_CHILD_PID;
1444 request_stats_final(request);
1446 case REQUEST_PROXIED:
1447 rad_assert(request->next_callback != NULL);
1448 rad_assert(request->next_callback != wait_a_bit);
1450 request->when = request->next_when;
1451 callback = request->next_callback;
1452 request->next_callback = NULL;
1456 rad_panic("Internal sanity check failure");
1461 * Something major went wrong. Discard the request, and
1464 * FIXME: No idea why this happens or how to fix it...
1465 * It seems to happen *only* when requests are proxied,
1466 * and where the home server doesn't respond. So it looks
1467 * like a race condition above, but it happens in debug
1468 * mode, with no threads...
1471 RDEBUG("WARNING: Internal sanity check failed in event handler for request %d: Discarding the request!", request->number);
1472 ev_request_free(&request);
1476 INSERT_EVENT(callback, request);
1480 static void no_response_to_coa_request(void *ctx)
1482 REQUEST *request = ctx;
1485 rad_assert(request->magic == REQUEST_MAGIC);
1486 rad_assert(request->child_state == REQUEST_PROXIED);
1487 rad_assert(request->home_server != NULL);
1488 rad_assert(!request->in_request_hash);
1490 radlog(L_ERR, "No response to CoA request sent to %s",
1491 inet_ntop(request->proxy->dst_ipaddr.af,
1492 &request->proxy->dst_ipaddr.ipaddr,
1493 buffer, sizeof(buffer)));
1498 request->packet->code |= (PW_COA_REQUEST << 8);
1499 post_proxy_fail_handler(request);
1503 static int update_event_timestamp(RADIUS_PACKET *packet, time_t when)
1507 vp = pairfind(packet->vps, PW_EVENT_TIMESTAMP);
1514 packet->data = NULL;
1515 packet->data_len = 0;
1518 return 1; /* time stamp updated */
1523 * Called when we haven't received a response to a CoA request.
1525 static void retransmit_coa_request(void *ctx)
1529 REQUEST *request = ctx;
1531 rad_assert(request->magic == REQUEST_MAGIC);
1532 rad_assert(request->child_state == REQUEST_PROXIED);
1533 rad_assert(request->home_server != NULL);
1534 rad_assert(!request->in_request_hash);
1535 rad_assert(request->parent == NULL);
1537 fr_event_now(el, &now);
1540 * Cap count at MRC, if it is non-zero.
1542 if (request->home_server->coa_mrc &&
1543 (request->num_coa_requests >= request->home_server->coa_mrc)) {
1544 no_response_to_coa_request(request);
1549 * RFC 5080 Section 2.2.1
1551 * RT = 2*RTprev + RAND*RTprev
1552 * = 1.9 * RTprev + rand(0,.2) * RTprev
1553 * = 1.9 * RTprev + rand(0,1) * (RTprev / 5)
1556 delay ^= (delay >> 16);
1558 frac = request->delay / 5;
1559 delay = ((frac >> 16) * delay) + (((frac & 0xffff) * delay) >> 16);
1561 delay += (2 * request->delay) - (request->delay / 10);
1564 * Cap delay at MRT, if MRT is non-zero.
1566 if (request->home_server->coa_mrt &&
1567 (delay > (request->home_server->coa_mrt * USEC))) {
1568 int mrt_usec = request->home_server->coa_mrt * USEC;
1571 * delay = MRT + RAND * MRT
1572 * = 0.9 MRT + rand(0,.2) * MRT
1575 delay ^= (delay >> 15);
1577 delay = ((mrt_usec >> 16) * delay) + (((mrt_usec & 0xffff) * delay) >> 16);
1578 delay += mrt_usec - (mrt_usec / 10);
1581 request->delay = delay;
1582 request->when = now;
1583 tv_add(&request->when, request->delay);
1584 mrd = request->proxy_when;
1585 mrd.tv_sec += request->home_server->coa_mrd;
1588 * Cap duration at MRD.
1590 if (timercmp(&mrd, &request->when, <)) {
1591 request->when = mrd;
1592 INSERT_EVENT(no_response_to_coa_request, request);
1595 INSERT_EVENT(retransmit_coa_request, request);
1598 if (update_event_timestamp(request->proxy, now.tv_sec)) {
1599 if (!insert_into_proxy_hash(request, TRUE)) {
1600 DEBUG("ERROR: Failed re-inserting CoA request into proxy hash.");
1604 request->num_proxied_requests = 0;
1605 request->num_proxied_responses = 0;
1608 request->num_proxied_requests++;
1609 request->num_coa_requests++; /* is NOT reset by code 3 lines above! */
1611 request->proxy_listener->send(request->proxy_listener,
1617 * The original request is either DONE, or in CLEANUP_DELAY.
1619 static int originated_coa_request(REQUEST *request)
1621 int delay, rcode, pre_proxy_type = 0;
1627 rad_assert(request->proxy == NULL);
1628 rad_assert(!request->in_proxy_hash);
1629 rad_assert(request->proxy_reply == NULL);
1631 vp = pairfind(request->config_items, PW_SEND_COA_REQUEST);
1632 if (!vp && request->coa) vp = pairfind(request->coa->proxy->vps, PW_SEND_COA_REQUEST);
1634 if (vp->vp_integer == 0) {
1635 ev_request_free(&request->coa);
1636 return 1; /* success */
1639 if (!request->coa) request_alloc_coa(request);
1640 if (!request->coa) return 0;
1646 * src_ipaddr will be set up in proxy_encode.
1648 memset(&ipaddr, 0, sizeof(ipaddr));
1649 vp = pairfind(coa->proxy->vps, PW_PACKET_DST_IP_ADDRESS);
1651 ipaddr.af = AF_INET;
1652 ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
1654 } else if ((vp = pairfind(coa->proxy->vps,
1655 PW_PACKET_DST_IPV6_ADDRESS)) != NULL) {
1656 ipaddr.af = AF_INET6;
1657 ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
1659 } else if ((vp = pairfind(coa->proxy->vps,
1660 PW_HOME_SERVER_POOL)) != NULL) {
1661 coa->home_pool = home_pool_byname(vp->vp_strvalue,
1663 if (!coa->home_pool) {
1664 RDEBUG2("WARNING: No such home_server_pool %s",
1667 ev_request_free(&request->coa);
1674 } else if (request->client->coa_pool) {
1675 coa->home_pool = request->client->coa_pool;
1677 } else if (request->client->coa_server) {
1678 coa->home_server = request->client->coa_server;
1682 * If all else fails, send it to the client that
1683 * originated this request.
1685 memcpy(&ipaddr, &request->packet->src_ipaddr, sizeof(ipaddr));
1689 * Use the pool, if it exists.
1691 if (coa->home_pool) {
1692 coa->home_server = home_server_ldb(NULL, coa->home_pool, coa);
1693 if (!coa->home_server) {
1694 RDEBUG("WARNING: No live home server for home_server_pool %s", vp->vp_strvalue);
1698 } else if (!coa->home_server) {
1699 int port = PW_COA_UDP_PORT;
1701 vp = pairfind(coa->proxy->vps, PW_PACKET_DST_PORT);
1702 if (vp) port = vp->vp_integer;
1704 coa->home_server = home_server_find(&ipaddr, port);
1705 if (!coa->home_server) {
1706 RDEBUG2("WARNING: Unknown destination %s:%d for CoA request.",
1707 inet_ntop(ipaddr.af, &ipaddr.ipaddr,
1708 buffer, sizeof(buffer)), port);
1713 vp = pairfind(coa->proxy->vps, PW_PACKET_TYPE);
1715 switch (vp->vp_integer) {
1716 case PW_COA_REQUEST:
1717 case PW_DISCONNECT_REQUEST:
1718 coa->proxy->code = vp->vp_integer;
1722 DEBUG("Cannot set CoA Packet-Type to code %d",
1728 if (!coa->proxy->code) coa->proxy->code = PW_COA_REQUEST;
1731 * The rest of the server code assumes that
1732 * request->packet && request->reply exist. Copy them
1733 * from the original request.
1735 rad_assert(coa->packet != NULL);
1736 rad_assert(coa->packet->vps == NULL);
1737 memcpy(coa->packet, request->packet, sizeof(*request->packet));
1738 coa->packet->vps = paircopy(request->packet->vps);
1739 coa->packet->data = NULL;
1740 rad_assert(coa->reply != NULL);
1741 rad_assert(coa->reply->vps == NULL);
1742 memcpy(coa->reply, request->reply, sizeof(*request->reply));
1743 coa->reply->vps = paircopy(request->reply->vps);
1744 coa->reply->data = NULL;
1745 coa->config_items = paircopy(request->config_items);
1748 * Call the pre-proxy routines.
1750 vp = pairfind(request->config_items, PW_PRE_PROXY_TYPE);
1752 RDEBUG2(" Found Pre-Proxy-Type %s", vp->vp_strvalue);
1753 pre_proxy_type = vp->vp_integer;
1756 if (coa->home_pool && coa->home_pool->virtual_server) {
1757 const char *old_server = coa->server;
1759 coa->server = coa->home_pool->virtual_server;
1760 RDEBUG2(" server %s {", coa->server);
1761 rcode = module_pre_proxy(pre_proxy_type, coa);
1763 coa->server = old_server;
1765 rcode = module_pre_proxy(pre_proxy_type, coa);
1772 * Only send the CoA packet if the pre-proxy code succeeded.
1774 case RLM_MODULE_NOOP:
1776 case RLM_MODULE_UPDATED:
1781 * Source IP / port is set when the proxy socket
1784 coa->proxy->dst_ipaddr = coa->home_server->ipaddr;
1785 coa->proxy->dst_port = coa->home_server->port;
1787 if (!insert_into_proxy_hash(coa, FALSE)) {
1788 DEBUG("ERROR: Failed inserting CoA request into proxy hash.");
1793 * We CANNOT divorce the CoA request from the parent
1794 * request. This function is running in a child thread,
1795 * and we need access to the main event loop in order to
1796 * to add the timers for the CoA packet. See
1801 * Forget about the original request completely at this
1806 gettimeofday(&request->proxy_when, NULL);
1807 request->received = request->next_when = request->proxy_when;
1808 rad_assert(request->proxy_reply == NULL);
1811 * Implement re-transmit algorithm as per RFC 5080
1814 * We want IRT + RAND*IRT
1815 * or 0.9 IRT + rand(0,.2) IRT
1817 * 2^20 ~ USEC, and we want 2.
1818 * rand(0,0.2) USEC ~ (rand(0,2^21) / 10)
1820 delay = (fr_rand() & ((1 << 22) - 1)) / 10;
1821 request->delay = delay * request->home_server->coa_irt;
1822 delay = request->home_server->coa_irt * USEC;
1823 delay -= delay / 10;
1824 delay += request->delay;
1826 request->delay = delay;
1827 tv_add(&request->next_when, delay);
1828 request->next_callback = retransmit_coa_request;
1831 * Note that we set proxied BEFORE sending the packet.
1833 * Once we send it, the request is tainted, as
1834 * another thread may have picked it up. Don't
1837 request->num_proxied_requests = 1;
1838 request->num_proxied_responses = 0;
1839 request->child_pid = NO_SUCH_CHILD_PID;
1841 update_event_timestamp(request->proxy, request->proxy_when.tv_sec);
1843 request->child_state = REQUEST_PROXIED;
1845 DEBUG_PACKET(request, request->proxy, 1);
1847 request->proxy_listener->send(request->proxy_listener,
1851 #endif /* WITH_COA */
1854 static int process_proxy_reply(REQUEST *request)
1857 int post_proxy_type = 0;
1861 * Delete any reply we had accumulated until now.
1863 pairfree(&request->reply->vps);
1866 * Run the packet through the post-proxy stage,
1867 * BEFORE playing games with the attributes.
1869 vp = pairfind(request->config_items, PW_POST_PROXY_TYPE);
1871 RDEBUG2(" Found Post-Proxy-Type %s", vp->vp_strvalue);
1872 post_proxy_type = vp->vp_integer;
1875 if (request->home_pool && request->home_pool->virtual_server) {
1876 const char *old_server = request->server;
1878 request->server = request->home_pool->virtual_server;
1879 RDEBUG2(" server %s {", request->server);
1880 rcode = module_post_proxy(post_proxy_type, request);
1882 request->server = old_server;
1884 rcode = module_post_proxy(post_proxy_type, request);
1888 if (request->packet->code == request->proxy->code)
1890 * Don't run the next bit if we originated a CoA
1891 * packet, after receiving an Access-Request or
1892 * Accounting-Request.
1897 * There may NOT be a proxy reply, as we may be
1898 * running Post-Proxy-Type = Fail.
1900 if (request->proxy_reply) {
1902 * Delete the Proxy-State Attributes from
1903 * the reply. These include Proxy-State
1904 * attributes from us and remote server.
1906 pairdelete(&request->proxy_reply->vps, PW_PROXY_STATE);
1909 * Add the attributes left in the proxy
1910 * reply to the reply list.
1912 pairadd(&request->reply->vps, request->proxy_reply->vps);
1913 request->proxy_reply->vps = NULL;
1916 * Free proxy request pairs.
1918 pairfree(&request->proxy->vps);
1922 default: /* Don't do anything */
1924 case RLM_MODULE_FAIL:
1925 /* FIXME: debug print stuff */
1926 request->child_state = REQUEST_DONE;
1929 case RLM_MODULE_HANDLED:
1930 /* FIXME: debug print stuff */
1931 request->child_state = REQUEST_DONE;
1940 static void tcp_socket_lifetime(void *ctx)
1942 rad_listen_t *listener = ctx;
1944 DEBUG("Reached lifetime on outgoing connection to home server");
1945 proxy_close_tcp_listener(listener);
1948 static void tcp_idle_timeout(void *ctx)
1950 rad_listen_t *listener = ctx;
1951 fr_tcp_radius_t *tcp = fr_listen2tcp(listener);
1954 * Rather than adding && deleting the timer when "used"
1955 * changes from 1->0, or from 0->1, we just add it when
1956 * "used" reaches zero, and don't touch it after that.
1957 * However, in order to avoid deleting used sockets, we
1958 * double-check things here.
1960 if (tcp->used != 0) {
1962 * If it's NOT idle, we still need to enforce
1965 if (tcp->lifetime > 0) {
1966 struct timeval when;
1968 when.tv_sec = tcp->opened;
1969 when.tv_sec += tcp->lifetime;
1970 when.tv_usec = fr_rand() & 0xffff;
1972 fr_event_insert(el, tcp_socket_lifetime,
1974 &when, (fr_event_t **) &tcp->ev);
1980 DEBUG("Reasched idle timeout on outgoing connection to home server");
1981 proxy_close_tcp_listener(listener);
1986 static int request_pre_handler(REQUEST *request)
1990 rad_assert(request->magic == REQUEST_MAGIC);
1991 rad_assert(request->packet != NULL);
1993 request->child_state = REQUEST_RUNNING;
1996 * Don't decode the packet if it's an internal "fake"
1997 * request. Instead, just return so that the caller can
2000 if (request->packet->dst_port == 0) {
2001 request->username = pairfind(request->packet->vps,
2003 request->password = pairfind(request->packet->vps,
2010 * Put the decoded packet into it's proper place.
2012 if (request->proxy_reply != NULL) {
2013 rcode = request->proxy_listener->decode(request->proxy_listener,
2015 DEBUG_PACKET(request, request->proxy_reply, 0);
2018 if (request->packet->vps == NULL) {
2019 rcode = request->listener->decode(request->listener, request);
2021 if (debug_condition) {
2023 const char *my_debug = debug_condition;
2026 * Ignore parse errors.
2028 radius_evaluate_condition(request, RLM_MODULE_OK, 0,
2032 request->options = 2;
2033 request->radlog = radlog_request;
2037 DEBUG_PACKET(request, request->packet, 0);
2043 radlog(L_ERR, "%s Dropping packet without response.", fr_strerror());
2044 request->child_state = REQUEST_DONE;
2048 if (!request->username) {
2049 request->username = pairfind(request->packet->vps,
2054 if (request->proxy) {
2055 return process_proxy_reply(request);
2065 * Do state handling when we proxy a request.
2067 static int proxy_request(REQUEST *request)
2069 struct timeval when;
2072 if (request->home_server->server) {
2073 RDEBUG("ERROR: Cannot perform real proxying to a virtual server.");
2077 if (!insert_into_proxy_hash(request, FALSE)) {
2078 RDEBUG("ERROR: Failed inserting request into proxy hash.");
2082 request->proxy_listener->encode(request->proxy_listener, request);
2084 when = request->received;
2085 when.tv_sec += request->root->max_request_time;
2087 gettimeofday(&request->proxy_when, NULL);
2089 request->next_when = request->proxy_when;
2090 request->next_when.tv_sec += request->home_server->response_window;
2092 rad_assert(request->home_server->response_window > 0);
2094 if (timercmp(&when, &request->next_when, <)) {
2095 request->next_when = when;
2097 request->next_callback = no_response_to_proxied_request;
2099 RDEBUG2("Proxying request %d to home server %s port %d",
2101 inet_ntop(request->proxy->dst_ipaddr.af,
2102 &request->proxy->dst_ipaddr.ipaddr,
2103 buffer, sizeof(buffer)),
2104 request->proxy->dst_port);
2107 * Note that we set proxied BEFORE sending the packet.
2109 * Once we send it, the request is tainted, as
2110 * another thread may have picked it up. Don't
2113 request->num_proxied_requests = 1;
2114 request->num_proxied_responses = 0;
2115 #ifdef HAVE_PTHREAD_H
2116 request->child_pid = NO_SUCH_CHILD_PID;
2118 request->child_state = REQUEST_PROXIED;
2120 DEBUG_PACKET(request, request->proxy, 1);
2122 request->proxy_listener->send(request->proxy_listener,
2129 * "Proxy" the request by sending it to a new virtual server.
2131 static int proxy_to_virtual_server(REQUEST *request)
2134 RAD_REQUEST_FUNP fun;
2136 if (!request->home_server || !request->home_server->server) return 0;
2138 if (request->parent) {
2139 RDEBUG2("WARNING: Cancelling proxy request to virtual server %s as this request was itself proxied.", request->home_server->server);
2143 fake = request_alloc_fake(request);
2145 RDEBUG2("WARNING: Out of memory");
2149 fake->packet->vps = paircopy(request->proxy->vps);
2150 fake->server = request->home_server->server;
2152 if (request->proxy->code == PW_AUTHENTICATION_REQUEST) {
2153 fun = rad_authenticate;
2155 #ifdef WITH_ACCOUNTING
2156 } else if (request->proxy->code == PW_ACCOUNTING_REQUEST) {
2157 fun = rad_accounting;
2161 RDEBUG2("Unknown packet type %d", request->proxy->code);
2162 ev_request_free(&fake);
2166 RDEBUG2(">>> Sending proxied request internally to virtual server.");
2167 radius_handle_request(fake, fun);
2168 RDEBUG2("<<< Received proxied response code %d from internal virtual server.", fake->reply->code);
2170 if (fake->reply->code != 0) {
2171 request->proxy_reply = fake->reply;
2175 * There was no response
2177 setup_post_proxy_fail(request);
2180 ev_request_free(&fake);
2182 process_proxy_reply(request);
2185 * Process it through the normal section again, but ONLY
2186 * if we received a proxy reply..
2188 if (request->proxy_reply) {
2189 if (request->server) RDEBUG("server %s {",
2190 request->server != NULL ?
2191 request->server : "");
2194 if (request->server) RDEBUG("} # server %s",
2195 request->server != NULL ?
2196 request->server : "");
2199 return 2; /* success, but NOT '1' !*/
2203 * Return 1 if we did proxy it, or the proxy attempt failed
2204 * completely. Either way, the caller doesn't touch the request
2205 * any more if we return 1.
2207 static int successfully_proxied_request(REQUEST *request)
2210 int pre_proxy_type = 0;
2211 VALUE_PAIR *realmpair;
2212 VALUE_PAIR *strippedname;
2214 char *realmname = NULL;
2216 REALM *realm = NULL;
2220 * If it was already proxied, do nothing.
2222 * FIXME: This should really be a serious error.
2224 if (request->in_proxy_hash ||
2225 (request->proxy_reply && (request->proxy_reply->code != 0))) {
2229 realmpair = pairfind(request->config_items, PW_PROXY_TO_REALM);
2230 if (!realmpair || (realmpair->length == 0)) {
2233 vp = pairfind(request->config_items, PW_HOME_SERVER_POOL);
2236 switch (request->packet->code) {
2237 case PW_AUTHENTICATION_REQUEST:
2238 pool_type = HOME_TYPE_AUTH;
2241 #ifdef WITH_ACCOUNTING
2242 case PW_ACCOUNTING_REQUEST:
2243 pool_type = HOME_TYPE_ACCT;
2248 case PW_COA_REQUEST:
2249 case PW_DISCONNECT_REQUEST:
2250 pool_type = HOME_TYPE_COA;
2258 pool = home_pool_byname(vp->vp_strvalue, pool_type);
2260 RDEBUG2("ERROR: Cannot proxy to unknown pool %s",
2265 realmname = NULL; /* no realms */
2270 realmname = (char *) realmpair->vp_strvalue;
2272 realm = realm_find2(realmname);
2274 RDEBUG2("ERROR: Cannot proxy to unknown realm %s", realmname);
2279 * Figure out which pool to use.
2281 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
2282 pool = realm->auth_pool;
2284 #ifdef WITH_ACCOUNTING
2285 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
2286 pool = realm->acct_pool;
2290 } else if ((request->packet->code == PW_COA_REQUEST) ||
2291 (request->packet->code == PW_DISCONNECT_REQUEST)) {
2292 pool = realm->acct_pool;
2296 rad_panic("Internal sanity check failed");
2300 RDEBUG2(" WARNING: Cancelling proxy to Realm %s, as the realm is local.",
2306 home = home_server_ldb(realmname, pool, request);
2308 RDEBUG2("ERROR: Failed to find live home server for realm %s",
2312 request->home_pool = pool;
2316 * Once we've decided to proxy a request, we cannot send
2317 * a CoA packet. So we free up any CoA packet here.
2319 ev_request_free(&request->coa);
2322 * Remember that we sent the request to a Realm.
2324 if (realmname) pairadd(&request->packet->vps,
2325 pairmake("Realm", realmname, T_OP_EQ));
2328 * Strip the name, if told to.
2330 * Doing it here catches the case of proxied tunneled
2333 if (realm && (realm->striprealm == TRUE) &&
2334 (strippedname = pairfind(request->proxy->vps, PW_STRIPPED_USER_NAME)) != NULL) {
2336 * If there's a Stripped-User-Name attribute in
2337 * the request, then use THAT as the User-Name
2338 * for the proxied request, instead of the
2341 * This is done by making a copy of the
2342 * Stripped-User-Name attribute, turning it into
2343 * a User-Name attribute, deleting the
2344 * Stripped-User-Name and User-Name attributes
2345 * from the vps list, and making the new
2346 * User-Name the head of the vps list.
2348 vp = pairfind(request->proxy->vps, PW_USER_NAME);
2350 vp = radius_paircreate(request, NULL,
2351 PW_USER_NAME, PW_TYPE_STRING);
2352 rad_assert(vp != NULL); /* handled by above function */
2353 /* Insert at the START of the list */
2354 vp->next = request->proxy->vps;
2355 request->proxy->vps = vp;
2357 memcpy(vp->vp_strvalue, strippedname->vp_strvalue,
2358 sizeof(vp->vp_strvalue));
2359 vp->length = strippedname->length;
2362 * Do NOT delete Stripped-User-Name.
2367 * If there is no PW_CHAP_CHALLENGE attribute but
2368 * there is a PW_CHAP_PASSWORD we need to add it
2369 * since we can't use the request authenticator
2370 * anymore - we changed it.
2372 if ((request->packet->code == PW_AUTHENTICATION_REQUEST) &&
2373 pairfind(request->proxy->vps, PW_CHAP_PASSWORD) &&
2374 pairfind(request->proxy->vps, PW_CHAP_CHALLENGE) == NULL) {
2375 vp = radius_paircreate(request, &request->proxy->vps,
2376 PW_CHAP_CHALLENGE, PW_TYPE_OCTETS);
2377 vp->length = AUTH_VECTOR_LEN;
2378 memcpy(vp->vp_strvalue, request->packet->vector, AUTH_VECTOR_LEN);
2382 * The RFC's say we have to do this, but FreeRADIUS
2385 vp = radius_paircreate(request, &request->proxy->vps,
2386 PW_PROXY_STATE, PW_TYPE_OCTETS);
2387 snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%d",
2388 request->packet->id);
2389 vp->length = strlen(vp->vp_strvalue);
2392 * Should be done BEFORE inserting into proxy hash, as
2393 * pre-proxy may use this information, or change it.
2395 request->proxy->code = request->packet->code;
2398 * Call the pre-proxy routines.
2400 vp = pairfind(request->config_items, PW_PRE_PROXY_TYPE);
2402 RDEBUG2(" Found Pre-Proxy-Type %s", vp->vp_strvalue);
2403 pre_proxy_type = vp->vp_integer;
2406 rad_assert(request->home_pool != NULL);
2408 if (request->home_pool->virtual_server) {
2409 const char *old_server = request->server;
2411 request->server = request->home_pool->virtual_server;
2412 RDEBUG2(" server %s {", request->server);
2413 rcode = module_pre_proxy(pre_proxy_type, request);
2415 request->server = old_server;
2417 rcode = module_pre_proxy(pre_proxy_type, request);
2420 case RLM_MODULE_FAIL:
2421 case RLM_MODULE_INVALID:
2422 case RLM_MODULE_NOTFOUND:
2423 case RLM_MODULE_USERLOCK:
2425 /* FIXME: debug print failed stuff */
2428 case RLM_MODULE_REJECT:
2429 case RLM_MODULE_HANDLED:
2433 * Only proxy the packet if the pre-proxy code succeeded.
2435 case RLM_MODULE_NOOP:
2437 case RLM_MODULE_UPDATED:
2442 * If it's a fake request, don't send the proxy
2443 * packet. The outer tunnel session will take
2444 * care of doing that.
2446 if (request->packet->dst_port == 0) {
2447 request->home_server = NULL;
2451 if (request->home_server->server) {
2452 return proxy_to_virtual_server(request);
2455 if (!proxy_request(request)) {
2456 RDEBUG("ERROR: Failed to proxy request %d", request->number);
2464 static void request_post_handler(REQUEST *request)
2466 int child_state = -1;
2467 struct timeval when;
2470 if ((request->master_state == REQUEST_STOP_PROCESSING) ||
2472 (request->parent->master_state == REQUEST_STOP_PROCESSING))) {
2473 RDEBUG2("Request %d was cancelled.", request->number);
2474 #ifdef HAVE_PTHREAD_H
2475 request->child_pid = NO_SUCH_CHILD_PID;
2477 child_state = REQUEST_DONE;
2481 if (request->child_state != REQUEST_RUNNING) {
2482 rad_panic("Internal sanity check failed");
2487 * If it's not in the request hash, it's a CoA request.
2490 if (!request->in_request_hash &&
2492 ((request->proxy->code == PW_COA_REQUEST) ||
2493 (request->proxy->code == PW_DISCONNECT_REQUEST))) {
2494 request->next_callback = NULL;
2495 child_state = REQUEST_DONE;
2501 * Catch Auth-Type := Reject BEFORE proxying the packet.
2503 if ((request->packet->code == PW_AUTHENTICATION_REQUEST) &&
2504 (request->reply->code == 0) &&
2505 ((vp = pairfind(request->config_items, PW_AUTH_TYPE)) != NULL) &&
2506 (vp->vp_integer == PW_AUTHTYPE_REJECT)) {
2507 request->reply->code = PW_AUTHENTICATION_REJECT;
2511 if (request->root->proxy_requests &&
2512 !request->in_proxy_hash &&
2513 (request->reply->code == 0) &&
2514 (request->packet->dst_port != 0) &&
2515 (request->packet->code != PW_STATUS_SERVER)) {
2516 int rcode = successfully_proxied_request(request);
2518 if (rcode == 1) return; /* request is invalid */
2521 * Failed proxying it (dead home servers, etc.)
2522 * Run it through Post-Proxy-Type = Fail, and
2523 * respond to the request.
2525 * Note that we're in a child thread here, so we
2526 * do NOT re-schedule the request. Instead, we
2527 * do what we would have done, which is run the
2528 * pre-handler, a NULL request handler, and then
2531 if ((rcode < 0) && setup_post_proxy_fail(request)) {
2532 request_pre_handler(request);
2536 * Else we weren't supposed to proxy it,
2537 * OR we proxied it internally to a virutal server.
2543 * Fake requests don't get encoded or signed. The caller
2544 * also requires the reply VP's, so we don't free them
2547 if (request->packet->dst_port == 0) {
2548 /* FIXME: RDEBUG going to the next request */
2549 #ifdef HAVE_PTHREAD_H
2550 request->child_pid = NO_SUCH_CHILD_PID;
2552 request->child_state = REQUEST_DONE;
2558 * Copy Proxy-State from the request to the reply.
2560 vp = paircopy2(request->packet->vps, PW_PROXY_STATE);
2561 if (vp) pairadd(&request->reply->vps, vp);
2565 * Access-Requests get delayed or cached.
2567 switch (request->packet->code) {
2568 case PW_AUTHENTICATION_REQUEST:
2569 gettimeofday(&request->next_when, NULL);
2571 if (request->reply->code == 0) {
2573 * Check if the lack of response is intentional.
2575 vp = pairfind(request->config_items,
2576 PW_RESPONSE_PACKET_TYPE);
2578 RDEBUG2("There was no response configured: rejecting request %d",
2580 request->reply->code = PW_AUTHENTICATION_REJECT;
2582 } else if (vp->vp_integer == 256) {
2583 RDEBUG2("Not responding to request %d",
2587 * Force cleanup after a long
2588 * time, so that we don't
2589 * re-process the packet.
2591 request->next_when.tv_sec += request->root->max_request_time;
2592 request->next_callback = cleanup_delay;
2593 child_state = REQUEST_CLEANUP_DELAY;
2596 request->reply->code = vp->vp_integer;
2602 * Run rejected packets through
2604 * Post-Auth-Type = Reject
2606 if (request->reply->code == PW_AUTHENTICATION_REJECT) {
2607 pairdelete(&request->config_items, PW_POST_AUTH_TYPE);
2608 vp = radius_pairmake(request, &request->config_items,
2609 "Post-Auth-Type", "Reject",
2611 if (vp) rad_postauth(request);
2614 * If configured, delay Access-Reject packets.
2616 * If request->root->reject_delay = 0, we discover
2617 * that we have to send the packet now.
2619 when = request->received;
2620 when.tv_sec += request->root->reject_delay;
2622 if (timercmp(&when, &request->next_when, >)) {
2623 RDEBUG2("Delaying reject of request %d for %d seconds",
2625 request->root->reject_delay);
2626 request->next_when = when;
2627 request->next_callback = reject_delay;
2628 #ifdef HAVE_PTHREAD_H
2629 request->child_pid = NO_SUCH_CHILD_PID;
2631 request->child_state = REQUEST_REJECT_DELAY;
2637 case PW_COA_REQUEST:
2638 case PW_DISCONNECT_REQUEST:
2640 request->next_when.tv_sec += request->root->cleanup_delay;
2641 request->next_callback = cleanup_delay;
2642 child_state = REQUEST_CLEANUP_DELAY;
2645 case PW_ACCOUNTING_REQUEST:
2646 request->next_callback = NULL; /* just to be safe */
2647 child_state = REQUEST_DONE;
2651 * FIXME: Status-Server should probably not be
2654 case PW_STATUS_SERVER:
2655 request->next_callback = NULL;
2656 child_state = REQUEST_DONE;
2663 request->next_callback = NULL;
2664 child_state = REQUEST_DONE;
2669 * Suppress "no reply" packets here, unless we're reading
2670 * from the "detail" file. In that case, we've got to
2671 * tell the detail file handler that the request is dead,
2672 * and it should re-send it.
2673 * If configured, encode, sign, and send.
2675 if ((request->reply->code != 0) ||
2676 (request->listener->type == RAD_LISTEN_DETAIL)) {
2677 DEBUG_PACKET(request, request->reply, 1);
2678 request->listener->send(request->listener, request);
2683 * Now that we've completely processed the request,
2684 * see if we need to originate a CoA request.
2687 (pairfind(request->config_items, PW_SEND_COA_REQUEST) != NULL)) {
2688 if (!originated_coa_request(request)) {
2689 RDEBUG2("Do CoA Fail handler here");
2691 /* request->coa is stil set, so we can update events */
2697 * Clean up. These are no longer needed.
2699 pairfree(&request->config_items);
2701 pairfree(&request->packet->vps);
2702 request->username = NULL;
2703 request->password = NULL;
2705 pairfree(&request->reply->vps);
2708 if (request->proxy) {
2709 pairfree(&request->proxy->vps);
2711 if (request->proxy_reply) {
2712 pairfree(&request->proxy_reply->vps);
2717 * We're not tracking responses from the home
2718 * server, we can therefore free this memory in
2721 if (!request->in_proxy_hash) {
2722 rad_free(&request->proxy);
2723 rad_free(&request->proxy_reply);
2724 request->home_server = NULL;
2730 RDEBUG2("Finished request %d.", request->number);
2731 rad_assert(child_state >= 0);
2732 request->child_state = child_state;
2735 * Single threaded mode: update timers now.
2737 if (!have_children) wait_a_bit(request);
2741 static void received_retransmit(REQUEST *request, const RADCLIENT *client)
2747 RAD_STATS_TYPE_INC(request->listener, total_dup_requests);
2748 RAD_STATS_CLIENT_INC(request->listener, client, total_dup_requests);
2750 switch (request->child_state) {
2751 case REQUEST_QUEUED:
2752 case REQUEST_RUNNING:
2756 radlog(L_ERR, "Discarding duplicate request from "
2757 "client %s port %d - ID: %d due to unfinished request %d",
2759 request->packet->src_port,request->packet->id,
2764 case REQUEST_PROXIED:
2766 * We're not supposed to have duplicate
2767 * accounting packets. The other states handle
2768 * duplicates fine (discard, or send duplicate
2769 * reply). But we do NOT want to retransmit an
2770 * accounting request here, because that would
2771 * involve updating the Acct-Delay-Time, and
2772 * therefore changing the packet Id, etc.
2774 * Instead, we just discard the packet. We may
2775 * eventually respond, or the client will send a
2776 * new accounting packet.
2778 * The same comments go for Status-Server, and
2779 * other packet types.
2781 * FIXME: coa: when we proxy CoA && Disconnect
2782 * packets, this logic has to be fixed.
2784 if (request->packet->code != PW_AUTHENTICATION_REQUEST) {
2788 check_for_zombie_home_server(request);
2791 * If we've just discovered that the home server
2792 * is dead, OR the socket has been closed, look for
2793 * another connection to a home server.
2795 if (((request->packet->dst_port != 0) &&
2796 (request->home_server->state == HOME_STATE_IS_DEAD)) ||
2797 (request->proxy_listener->status != RAD_LISTEN_STATUS_KNOWN)) {
2800 remove_from_proxy_hash(request);
2802 home = home_server_ldb(NULL, request->home_pool, request);
2804 RDEBUG2("Failed to find live home server for request %d", request->number);
2807 * Do post-request processing,
2808 * and any insertion of necessary
2811 post_proxy_fail_handler(request);
2815 request->proxy->code = request->packet->code;
2818 * Free the old packet, to force re-encoding
2820 free(request->proxy->data);
2821 request->proxy->data = NULL;
2822 request->proxy->data_len = 0;
2825 * This request failed over to a virtual
2826 * server. Push it back onto the queue
2829 if (request->home_server->server) {
2830 proxy_fallback_handler(request);
2835 * Try to proxy the request.
2837 if (!proxy_request(request)) {
2838 RDEBUG("ERROR: Failed to re-proxy request %d", request->number);
2839 goto no_home_servers;
2843 * This code executes in the main server
2844 * thread, so there's no need for locking.
2846 rad_assert(request->next_callback != NULL);
2847 INSERT_EVENT(request->next_callback, request);
2848 request->next_callback = NULL;
2850 } /* else the home server is still alive */
2853 if (request->home_server->proto == IPPROTO_TCP) {
2854 DEBUG2("Suppressing duplicate proxied request to home server %s port %d proto TCP - ID: %d",
2855 inet_ntop(request->proxy->dst_ipaddr.af,
2856 &request->proxy->dst_ipaddr.ipaddr,
2857 buffer, sizeof(buffer)),
2858 request->proxy->dst_port,
2859 request->proxy->id);
2864 RDEBUG2("Sending duplicate proxied request to home server %s port %d - ID: %d",
2865 inet_ntop(request->proxy->dst_ipaddr.af,
2866 &request->proxy->dst_ipaddr.ipaddr,
2867 buffer, sizeof(buffer)),
2868 request->proxy->dst_port,
2869 request->proxy->id);
2870 request->num_proxied_requests++;
2872 DEBUG_PACKET(request, request->proxy, 1);
2873 request->proxy_listener->send(request->proxy_listener,
2878 case REQUEST_REJECT_DELAY:
2879 RDEBUG2("Waiting to send Access-Reject "
2880 "to client %s port %d - ID: %d",
2882 request->packet->src_port, request->packet->id);
2885 case REQUEST_CLEANUP_DELAY:
2887 if (request->reply->code == 0) {
2888 RDEBUG2("Ignoring retransmit from client %s port %d "
2889 "- ID: %d, no reply was configured",
2891 request->packet->src_port, request->packet->id);
2896 * FIXME: This sends duplicate replies to
2897 * accounting requests, even if Acct-Delay-Time
2898 * or Event-Timestamp is in the packet. In those
2899 * cases, the Id should be changed, and the packet
2902 RDEBUG2("Sending duplicate reply "
2903 "to client %s port %d - ID: %d",
2905 request->packet->src_port, request->packet->id);
2906 DEBUG_PACKET(request, request->reply, 1);
2907 request->listener->send(request->listener, request);
2913 static void received_conflicting_request(REQUEST *request,
2914 const RADCLIENT *client)
2916 radlog(L_ERR, "Received conflicting packet from "
2917 "client %s port %d - ID: %d due to unfinished request %d. Giving up on old request.",
2919 request->packet->src_port, request->packet->id,
2923 * Nuke it from the request hash, so we can receive new
2926 remove_from_request_hash(request);
2928 switch (request->child_state) {
2929 #ifdef HAVE_PTHREAD_H
2931 * It's queued or running. Tell it to stop, and
2932 * wait for it to do so.
2934 case REQUEST_QUEUED:
2935 case REQUEST_RUNNING:
2936 request->master_state = REQUEST_STOP_PROCESSING;
2937 request->delay += request->delay >> 1;
2939 tv_add(&request->when, request->delay);
2941 INSERT_EVENT(wait_for_child_to_die, request);
2946 * Catch race conditions. It may have switched
2947 * from running to done while this code is being
2950 case REQUEST_REJECT_DELAY:
2951 case REQUEST_CLEANUP_DELAY:
2956 * It's in some other state, and therefore also
2957 * in the event queue. At some point, the
2958 * child will notice, and we can then delete it.
2960 case REQUEST_PROXIED:
2962 rad_assert(request->ev != NULL);
2968 static int can_handle_new_request(RADIUS_PACKET *packet,
2970 struct main_config_t *root)
2973 * Count the total number of requests, to see if
2974 * there are too many. If so, return with an
2977 if (root->max_requests) {
2978 int request_count = fr_packet_list_num_elements(pl);
2981 * This is a new request. Let's see if
2982 * it makes us go over our configured
2985 if (request_count > root->max_requests) {
2986 radlog(L_ERR, "Dropping request (%d is too many): "
2987 "from client %s port %d - ID: %d", request_count,
2989 packet->src_port, packet->id);
2990 radlog(L_INFO, "WARNING: Please check the configuration file.\n"
2991 "\tThe value for 'max_requests' is probably set too low.\n");
2993 } /* else there were a small number of requests */
2994 } /* else there was no configured limit for requests */
2997 * FIXME: Add per-client checks. If one client is sending
2998 * too many packets, start discarding them.
3000 * We increment the counters here, and decrement them
3001 * when the response is sent... somewhere in this file.
3005 * FUTURE: Add checks for system load. If the system is
3006 * busy, start dropping requests...
3008 * We can probably keep some statistics ourselves... if
3009 * there are more requests coming in than we can handle,
3010 * start dropping some.
3017 int received_request(rad_listen_t *listener,
3018 RADIUS_PACKET *packet, REQUEST **prequest,
3021 RADIUS_PACKET **packet_p;
3022 REQUEST *request = NULL;
3023 struct main_config_t *root = &mainconfig;
3025 packet_p = fr_packet_list_find(pl, packet);
3027 request = fr_packet2myptr(REQUEST, packet, packet_p);
3028 rad_assert(request->in_request_hash);
3030 if ((request->packet->data_len == packet->data_len) &&
3031 (memcmp(request->packet->vector, packet->vector,
3032 sizeof(packet->vector)) == 0)) {
3033 received_retransmit(request, client);
3038 * The new request is different from the old one,
3039 * but maybe the old is finished. If so, delete
3042 switch (request->child_state) {
3043 struct timeval when;
3047 * Special hacks for race conditions.
3048 * The reply is encoded, and therefore
3049 * likely sent. We received a *new*
3050 * packet from the client, likely before
3051 * the next line or two of code which
3052 * updated the child state. In this
3053 * case, just accept the new request.
3055 if ((request->reply->code != 0) &&
3056 request->reply->data) {
3057 radlog(L_INFO, "WARNING: Allowing fast client %s port %d - ID: %d for recent request %d.",
3059 packet->src_port, packet->id,
3061 remove_from_request_hash(request);
3066 gettimeofday(&when, NULL);
3070 * If the cached request was received
3071 * within the last second, then we
3072 * discard the NEW request instead of the
3073 * old one. This will happen ONLY when
3074 * the client is severely broken, and is
3075 * sending conflicting packets very
3078 if (timercmp(&when, &request->received, <)) {
3079 radlog(L_ERR, "Discarding conflicting packet from "
3080 "client %s port %d - ID: %d due to recent request %d.",
3082 packet->src_port, packet->id,
3087 received_conflicting_request(request, client);
3091 case REQUEST_REJECT_DELAY:
3092 case REQUEST_CLEANUP_DELAY:
3093 request->child_state = REQUEST_DONE;
3095 cleanup_delay(request);
3102 * We may want to quench the new request.
3104 if ((listener->type != RAD_LISTEN_DETAIL) &&
3105 !can_handle_new_request(packet, client, root)) {
3110 * Create and initialize the new request.
3112 request = request_alloc(); /* never fails */
3114 if ((request->reply = rad_alloc(0)) == NULL) {
3115 radlog(L_ERR, "No memory");
3119 request->listener = listener;
3120 request->client = client;
3121 request->packet = packet;
3122 request->packet->timestamp = request->timestamp;
3123 request->number = request_num_counter++;
3124 request->priority = listener->type;
3125 #ifdef HAVE_PTHREAD_H
3126 request->child_pid = NO_SUCH_CHILD_PID;
3130 * Status-Server packets go to the head of the queue.
3132 if (request->packet->code == PW_STATUS_SERVER) request->priority = 0;
3135 * Set virtual server identity
3137 if (client->server) {
3138 request->server = client->server;
3139 } else if (listener->server) {
3140 request->server = listener->server;
3142 request->server = NULL;
3146 * Remember the request in the list.
3148 if (!fr_packet_list_insert(pl, &request->packet)) {
3149 radlog(L_ERR, "Failed to insert request %d in the list of live requests: discarding", request->number);
3150 ev_request_free(&request);
3154 request->in_request_hash = TRUE;
3155 request->root = root;
3158 request->listener->count++;
3162 * The request passes many of our sanity checks.
3163 * From here on in, if anything goes wrong, we
3164 * send a reject message, instead of dropping the
3169 * Build the reply template from the request.
3172 request->reply->sockfd = request->packet->sockfd;
3173 request->reply->dst_ipaddr = request->packet->src_ipaddr;
3174 request->reply->src_ipaddr = request->packet->dst_ipaddr;
3175 request->reply->dst_port = request->packet->src_port;
3176 request->reply->src_port = request->packet->dst_port;
3177 request->reply->id = request->packet->id;
3178 request->reply->code = 0; /* UNKNOWN code */
3179 memcpy(request->reply->vector, request->packet->vector,
3180 sizeof(request->reply->vector));
3181 request->reply->vps = NULL;
3182 request->reply->data = NULL;
3183 request->reply->data_len = 0;
3185 request->master_state = REQUEST_ACTIVE;
3186 request->child_state = REQUEST_QUEUED;
3187 request->next_callback = NULL;
3189 gettimeofday(&request->received, NULL);
3190 request->timestamp = request->received.tv_sec;
3191 request->when = request->received;
3193 request->delay = USEC;
3195 tv_add(&request->when, request->delay);
3197 INSERT_EVENT(wait_a_bit, request);
3199 *prequest = request;
3207 * received_proxy_response is split in two for TCP handling.
3209 static REQUEST *received_proxy_response_p2(RADIUS_PACKET *packet,
3213 REQUEST *received_proxy_response(RADIUS_PACKET *packet)
3222 * Also removes from the proxy hash if responses == requests
3224 request = lookup_in_proxy_hash(packet);
3227 return received_proxy_response_p2(packet, request);
3230 static REQUEST *received_proxy_response_p2(RADIUS_PACKET *packet,
3234 fr_tcp_radius_t *tcp;
3238 radlog(L_PROXY, "No outstanding request was found for reply from host %s port %d - ID %d",
3239 inet_ntop(packet->src_ipaddr.af,
3240 &packet->src_ipaddr.ipaddr,
3241 buffer, sizeof(buffer)),
3242 packet->src_port, packet->id);
3247 * We haven't replied to the NAS, but we have seen an
3248 * earlier reply from the home server. Ignore this packet,
3249 * as we're likely still processing the previous reply.
3251 if (request->proxy_reply) {
3252 if (memcmp(request->proxy_reply->vector,
3254 sizeof(request->proxy_reply->vector)) == 0) {
3255 RDEBUG2("Discarding duplicate reply from host %s port %d - ID: %d for request %d",
3256 inet_ntop(packet->src_ipaddr.af,
3257 &packet->src_ipaddr.ipaddr,
3258 buffer, sizeof(buffer)),
3259 packet->src_port, packet->id,
3263 * ? The home server gave us a new proxy
3264 * reply which doesn't match the old
3267 RDEBUG2("Ignoring conflicting proxy reply");
3270 /* assert that there's an event queued for request? */
3275 * Verify the packet before doing ANYTHING with it. This
3276 * means we're doing more MD5 checks in the server core.
3277 * However, we can fix that by moving to multiple threads
3278 * listening on sockets.
3280 * We do this AFTER looking the request up in the hash,
3281 * and AFTER vhecking if we saw a previous request. This
3282 * helps minimize the DoS effect of people attacking us
3283 * with spoofed packets.
3285 if (rad_verify(packet, request->proxy,
3286 request->home_server->secret) != 0) {
3287 DEBUG("Ignoring spoofed proxy reply. Signature is invalid");
3291 gettimeofday(&now, NULL);
3294 * Maybe move this earlier in the decision process?
3295 * Having it here means that late or duplicate proxy
3296 * replies no longer get the home server marked as
3297 * "alive". This might be good for stability, though.
3299 * FIXME: Do we really want to do this whenever we
3300 * receive a packet? Setting this here means that we
3301 * mark it alive on *any* packet, even if it's lost all
3302 * of the *other* packets in the last 10s.
3304 request->home_server->state = HOME_STATE_ALIVE;
3308 * When originating CoA, the "proxy" reply is the reply
3309 * to the CoA request that we originated. At this point,
3310 * the original request is finished, and it has a reply.
3312 * However, if we haven't separated the two requests, do
3313 * so now. This is done so that cleaning up the original
3314 * request won't cause the CoA request to be free'd. See
3315 * util.c, request_free()
3317 if (request->parent && (request->parent->coa == request)) {
3318 request->parent->coa = NULL;
3319 request->parent = NULL;
3323 * Skip the next set of checks, as the original
3324 * reply is cached. We want to be able to still
3325 * process the CoA reply, AND to reference the
3326 * original request/reply.
3328 * This is getting to be really quite a bit of a
3334 * If there's a reply to the NAS, ignore everything
3335 * related to proxy responses
3337 if (request->reply && request->reply->code != 0) {
3338 RDEBUG2("Ignoring proxy reply that arrived after we sent a reply to the NAS");
3344 * The average includes our time to receive packets and
3345 * look them up in the hashes, which should be the same
3348 * We update the response time only for the FIRST packet
3351 if (request->home_server->ema.window > 0) {
3352 radius_stats_ema(&request->home_server->ema,
3353 &now, &request->proxy_when);
3357 switch (request->child_state) {
3358 case REQUEST_QUEUED:
3359 case REQUEST_RUNNING:
3360 radlog(L_ERR, "Internal sanity check failed for child state");
3363 case REQUEST_REJECT_DELAY:
3364 case REQUEST_CLEANUP_DELAY:
3366 radlog(L_ERR, "Reply from home server %s port %d - ID: %d arrived too late for request %d. Try increasing 'retry_delay' or 'max_request_time'",
3367 inet_ntop(packet->src_ipaddr.af,
3368 &packet->src_ipaddr.ipaddr,
3369 buffer, sizeof(buffer)),
3370 packet->src_port, packet->id,
3372 /* assert that there's an event queued for request? */
3375 case REQUEST_PROXIED:
3379 request->proxy_reply = packet;
3383 * Perform RTT calculations, as per RFC 2988 (for TCP).
3384 * Note that we only do so on the first response.
3386 if ((request->num_proxied_responses == 1)
3388 home_server *home = request->home_server;
3390 rtt = now.tv_sec - request->proxy_when.tv_sec;
3393 rtt -= request->proxy_when.tv_usec;
3395 if (!home->has_rtt) {
3396 home->has_rtt = TRUE;
3399 home->rttvar = rtt / 2;
3402 home->rttvar -= home->rttvar >> 2;
3403 home->rttvar += (home->srtt - rtt);
3404 home->srtt -= home->srtt >> 3;
3405 home->srtt += rtt >> 3;
3408 home->rto = home->srtt;
3409 if (home->rttvar > (USEC / 4)) {
3410 home->rto += home->rttvar * 4;
3418 * There's no incoming request, so it's a proxied packet
3421 if (!request->packet) {
3422 received_response_to_ping(request);
3423 request->proxy_reply = NULL; /* caller will free it */
3424 ev_request_free(&request);
3428 request->child_state = REQUEST_QUEUED;
3429 request->when = now;
3430 request->delay = USEC;
3431 request->priority = RAD_LISTEN_PROXY;
3432 tv_add(&request->when, request->delay);
3435 tcp = fr_listen2tcp(request->proxy_listener);
3437 if ((tcp->lifetime > 0) &&
3438 (now.tv_sec > (tcp->opened + tcp->lifetime))) {
3439 proxy_close_tcp_listener(request->proxy_listener);
3443 * No outstanding packets. Set up idle timeout
3444 * timer, but only if we didn't already do so
3447 if ((tcp->used == 0) &&
3448 (request->home_server->idle_timeout != 0) &&
3449 (tcp->last_packet != now.tv_sec)) {
3450 struct timeval when;
3453 when.tv_sec += request->home_server->idle_timeout;
3455 fr_event_insert(el, tcp_idle_timeout,
3456 request->proxy_listener,
3457 &when, (fr_event_t **) &tcp->ev);
3460 tcp->last_packet = request->proxy_reply->timestamp = now.tv_sec;
3465 * Wait a bit will take care of max_request_time
3467 INSERT_EVENT(wait_a_bit, request);
3473 REQUEST *received_proxy_tcp_response(RADIUS_PACKET *packet,
3474 fr_tcp_radius_t *tcp)
3479 * Not found. Print the same message as all the other
3482 if (!tcp || !tcp->ids[packet->id]) {
3483 return received_proxy_response_p2(packet, NULL);
3487 * Find request from: tcp->ids[packet->id] = &request->proxy
3489 request = fr_packet2myptr(REQUEST, proxy, tcp->ids[packet->id]);
3492 * TCP sockets don't do re-transmits. Just nuke it.
3494 remove_from_proxy_hash(request);
3497 * FIXME: Update RFC 3539 watchdog timer.
3500 return received_proxy_response_p2(packet, request);
3502 #endif /* WITH_TCP */
3503 #endif /* WITH_PROXY */
3505 void event_new_fd(rad_listen_t *this)
3509 if (this->status == RAD_LISTEN_STATUS_KNOWN) return;
3511 this->print(this, buffer, sizeof(buffer));
3513 if (this->status == RAD_LISTEN_STATUS_INIT) {
3515 DEBUG("Listening on %s", buffer);
3517 DEBUG2(" ... adding new socket %s", buffer);
3519 if (!fr_event_fd_insert(el, 0, this->fd,
3520 event_socket_handler, this)) {
3521 radlog(L_ERR, "Failed remembering handle for proxy socket!");
3526 if (this->type == RAD_LISTEN_PROXY) {
3527 fr_tcp_radius_t *tcp = fr_listen2tcp(this);
3529 if (tcp && (tcp->lifetime > 0)) {
3530 struct timeval when;
3532 gettimeofday(&when, NULL);
3533 when.tv_sec += tcp->lifetime;
3535 fr_event_insert(el, tcp_socket_lifetime,
3537 &when, (fr_event_t **) &tcp->ev);
3542 this->status = RAD_LISTEN_STATUS_KNOWN;
3546 if (this->status == RAD_LISTEN_STATUS_CLOSED) {
3547 DEBUG2(" ... closing socket %s", buffer);
3549 fr_event_fd_delete(el, 0, this->fd);
3550 this->status = RAD_LISTEN_STATUS_FINISH;
3554 * FIXME: mark ALL requests for this connection
3555 * as MASTER FORCE STOP! we can't reply, so
3556 * there's no point in doing anything
3558 * ALSO, create packet lists JUST for each proxy
3562 * Once we're done, the caller free's the
3563 * listener. However, we've got to ensure that
3564 * all of the requests using this listener are
3567 if (this->type == RAD_LISTEN_PROXY) {
3570 fr_tcp_radius_t *tcp = fr_listen2tcp(this);
3574 for (i = 0; i < 256; i++) {
3575 if (!tcp->ids[i]) continue;
3577 request = fr_packet2myptr(REQUEST, proxy,
3579 request->proxy->sockfd = -1;
3580 request->proxy_listener = NULL;
3581 request->in_proxy_hash = FALSE;
3585 tcp->used = 0; /* just to be safe */
3587 fr_event_delete(el, (fr_event_t **) &tcp->ev);
3592 * Close the fd AFTER fixing up the requests and
3593 * listeners, so that they don't send/recv on the
3594 * wrong socket (if someone manages to open
3602 static void handle_signal_self(int flag)
3604 if ((flag & (RADIUS_SIGNAL_SELF_EXIT | RADIUS_SIGNAL_SELF_TERM)) != 0) {
3605 if ((flag & RADIUS_SIGNAL_SELF_EXIT) != 0) {
3606 fr_event_loop_exit(el, 1);
3608 fr_event_loop_exit(el, 2);
3612 } /* else exit/term flags weren't set */
3615 * Tell the even loop to stop processing.
3617 if ((flag & RADIUS_SIGNAL_SELF_HUP) != 0) {
3619 static time_t last_hup = 0;
3621 DEBUG("Received HUP signal.");
3624 if ((int) (when - last_hup) < 5) {
3625 radlog(L_INFO, "Ignoring HUP (less than 5s since last one)");
3630 fr_event_loop_exit(el, 0x80);
3634 if ((flag & RADIUS_SIGNAL_SELF_DETAIL) != 0) {
3638 * FIXME: O(N) loops suck.
3640 for (this = mainconfig.listen;
3642 this = this->next) {
3643 if (this->type != RAD_LISTEN_DETAIL) continue;
3646 * This one didn't send the signal, skip
3649 if (!this->decode(this, NULL)) continue;
3652 * Go service the interrupt.
3654 event_poll_detail(this);
3659 if ((flag & RADIUS_SIGNAL_SELF_NEW_FD) != 0) {
3662 for (this = mainconfig.listen;
3664 this = this->next) {
3670 #ifndef WITH_SELF_PIPE
3671 void radius_signal_self(int flag)
3673 handle_signal_self(flag);
3677 * Inform ourselves that we received a signal.
3679 void radius_signal_self(int flag)
3685 * The read MUST be non-blocking for this to work.
3687 rcode = read(self_pipe[0], buffer, sizeof(buffer));
3691 for (i = 0; i < rcode; i++) {
3692 buffer[0] |= buffer[i];
3700 write(self_pipe[1], buffer, 1);
3704 static void event_signal_handler(UNUSED fr_event_list_t *xel,
3705 UNUSED int fd, UNUSED void *ctx)
3710 rcode = read(self_pipe[0], buffer, sizeof(buffer));
3711 if (rcode <= 0) return;
3714 * Merge pending signals.
3716 for (i = 0; i < rcode; i++) {
3717 buffer[0] |= buffer[i];
3720 handle_signal_self(buffer[0]);
3725 static void event_socket_handler(fr_event_list_t *xel, UNUSED int fd,
3728 rad_listen_t *listener = ctx;
3729 RAD_REQUEST_FUNP fun;
3732 rad_assert(xel == el);
3736 if (listener->fd < 0) rad_panic("Socket was closed on us!");
3738 if (!listener->recv(listener, &fun, &request)) return;
3740 if (!thread_pool_addrequest(request, fun)) {
3741 request->child_state = REQUEST_DONE;
3745 typedef struct listen_detail_t {
3750 * This function is called periodically to see if this detail
3751 * file is available for reading.
3753 static void event_poll_detail(void *ctx)
3756 RAD_REQUEST_FUNP fun;
3758 rad_listen_t *this = ctx;
3759 struct timeval when;
3760 listen_detail_t *detail = this->data;
3762 rad_assert(this->type == RAD_LISTEN_DETAIL);
3765 * Try to read something.
3767 * FIXME: This does poll AND receive.
3769 rcode = this->recv(this, &fun, &request);
3771 rad_assert(fun != NULL);
3772 rad_assert(request != NULL);
3774 if (!thread_pool_addrequest(request, fun)) {
3775 request->child_state = REQUEST_DONE;
3779 if (!fr_event_now(el, &now)) gettimeofday(&now, NULL);
3783 * Backdoor API to get the delay until the next poll
3786 delay = this->encode(this, NULL);
3787 tv_add(&when, delay);
3789 if (!fr_event_insert(el, event_poll_detail, this,
3790 &when, &detail->ev)) {
3791 radlog(L_ERR, "Failed creating handler");
3797 static void event_status(struct timeval *wake)
3799 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
3803 if (debug_flag == 0) {
3805 radlog(L_INFO, "Ready to process requests.");
3806 just_started = FALSE;
3812 DEBUG("Ready to process requests.");
3814 } else if ((wake->tv_sec != 0) ||
3815 (wake->tv_usec >= 100000)) {
3816 DEBUG("Waking up in %d.%01u seconds.",
3817 (int) wake->tv_sec, (unsigned int) wake->tv_usec / 100000);
3822 * FIXME: Put this somewhere else, where it isn't called
3823 * all of the time...
3826 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
3828 * If there are no child threads, then there may
3829 * be child processes. In that case, wait for
3830 * their exit status, and throw that exit status
3831 * away. This helps get rid of zxombie children.
3833 while (waitpid(-1, &argval, WNOHANG) > 0) {
3841 * Externally-visibly functions.
3843 int radius_event_init(CONF_SECTION *cs, int spawn_flag)
3845 rad_listen_t *this, *head = NULL;
3849 time(&fr_start_time);
3851 el = fr_event_list_create(event_status);
3854 pl = fr_packet_list_create(0);
3855 if (!pl) return 0; /* leak el */
3857 request_num_counter = 0;
3860 if (mainconfig.proxy_requests) {
3861 pthread_mutexattr_t attr;
3864 * Create the tree for managing proxied requests and
3867 proxy_list = fr_packet_list_create(1);
3868 if (!proxy_list) return 0;
3870 #ifdef HAVE_PTHREAD_H
3871 pthread_mutexattr_init(&attr);
3873 #ifdef PTHREAD_MUTEX_RECURSIVE
3874 if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) < 0) {
3875 radlog(L_ERR, "FATAL: Failed to set type for proxy mutex: %s",
3881 if (pthread_mutex_init(&proxy_mutex, NULL) != 0) {
3882 radlog(L_ERR, "FATAL: Failed to initialize proxy mutex: %s",
3887 pthread_mutexattr_destroy(&attr);
3893 * Just before we spawn the child threads, force the log
3894 * subsystem to re-open the log file for every write.
3896 if (spawn_flag) force_log_reopen();
3898 #ifdef HAVE_PTHREAD_H
3900 NO_SUCH_CHILD_PID = (pthread_t ) (0);
3902 NO_SUCH_CHILD_PID = pthread_self(); /* not a child thread */
3905 * Initialize the threads ONLY if we're spawning, AND
3906 * we're running normally.
3908 if (spawn_flag && !check_config &&
3909 (thread_pool_init(cs, &spawn_flag) < 0)) {
3915 * Move all of the thread calls to this file?
3917 * It may be best for the mutexes to be in this file...
3919 have_children = spawn_flag;
3922 DEBUG("%s: #### Skipping IP addresses and Ports ####",
3927 #ifdef WITH_SELF_PIPE
3929 * Child threads need a pipe to signal us, as do the
3932 if (pipe(self_pipe) < 0) {
3933 radlog(L_ERR, "radiusd: Error opening internal pipe: %s",
3937 if (fcntl(self_pipe[0], F_SETFL, O_NONBLOCK | FD_CLOEXEC) < 0) {
3938 radlog(L_ERR, "radiusd: Error setting internal flags: %s",
3942 if (fcntl(self_pipe[1], F_SETFL, O_NONBLOCK | FD_CLOEXEC) < 0) {
3943 radlog(L_ERR, "radiusd: Error setting internal flags: %s",
3948 if (!fr_event_fd_insert(el, 0, self_pipe[0],
3949 event_signal_handler, el)) {
3950 radlog(L_ERR, "Failed creating handler for signals");
3953 #endif /* WITH_SELF_PIPE */
3957 * Mark the proxy Fd's as unused.
3962 for (i = 0; i < 32; i++) proxy_fds[i] = -1;
3966 DEBUG("%s: #### Opening IP addresses and Ports ####",
3970 * The server temporarily switches to an unprivileged
3971 * user very early in the bootstrapping process.
3972 * However, some sockets MAY require privileged access
3973 * (bind to device, or to port < 1024, or to raw
3974 * sockets). Those sockets need to call suid up/down
3975 * themselves around the functions that need a privileged
3978 if (listen_init(cs, &head) < 0) {
3983 * At this point, no one has any business *ever* going
3986 fr_suid_down_permanent();
3989 * Add all of the sockets to the event loop.
3993 this = this->next) {
3996 this->print(this, buffer, sizeof(buffer));
3998 switch (this->type) {
4000 case RAD_LISTEN_DETAIL:
4001 DEBUG("Listening on %s", buffer);
4004 * Detail files are always known, and aren't
4005 * put into the socket event loop.
4007 this->status = RAD_LISTEN_STATUS_KNOWN;
4010 * Set up the first poll interval.
4012 event_poll_detail(this);
4017 case RAD_LISTEN_PROXY:
4018 rad_assert(proxy_fds[this->fd & 0x1f] == -1);
4019 rad_assert(proxy_listeners[this->fd & 0x1f] == NULL);
4021 proxy_fds[this->fd & 0x1f] = this->fd;
4022 proxy_listeners[this->fd & 0x1f] = this;
4023 if (!fr_packet_list_socket_add(proxy_list,
4036 this->status = RAD_LISTEN_STATUS_KNOWN;
4039 mainconfig.listen = head;
4045 static int request_hash_cb(UNUSED void *ctx, void *data)
4047 REQUEST *request = fr_packet2myptr(REQUEST, packet, data);
4050 rad_assert(request->in_proxy_hash == FALSE);
4053 ev_request_free(&request);
4060 static int proxy_hash_cb(UNUSED void *ctx, void *data)
4062 REQUEST *request = fr_packet2myptr(REQUEST, proxy, data);
4064 ev_request_free(&request);
4070 void radius_event_free(void)
4073 * FIXME: Stop all threads, or at least check that
4074 * they're all waiting on the semaphore, and the queues
4080 * There are requests in the proxy hash that aren't
4081 * referenced from anywhere else. Remove them first.
4084 PTHREAD_MUTEX_LOCK(&proxy_mutex);
4085 fr_packet_list_walk(proxy_list, NULL, proxy_hash_cb);
4086 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
4087 fr_packet_list_free(proxy_list);
4092 fr_packet_list_walk(pl, NULL, request_hash_cb);
4094 fr_packet_list_free(pl);
4097 fr_event_list_free(el);
4100 int radius_event_process(void)
4104 return fr_event_loop(el);
4107 void radius_handle_request(REQUEST *request, RAD_REQUEST_FUNP fun)
4109 request->options = RAD_REQUEST_OPTION_DEBUG2;
4111 if (request_pre_handler(request)) {
4112 rad_assert(fun != NULL);
4113 rad_assert(request != NULL);
4115 if (request->server) RDEBUG("server %s {",
4116 request->server != NULL ?
4117 request->server : "");
4120 if (request->server) RDEBUG("} # server %s",
4121 request->server != NULL ?
4122 request->server : "");
4124 request_post_handler(request);
4127 DEBUG2("Going to the next request");