Simplify stats counting
[freeradius.git] / src / main / process.c
1 /*
2  *   This program is free software; you can redistribute it and/or modify
3  *   it under the terms of the GNU General Public License as published by
4  *   the Free Software Foundation; either version 2 of the License, or
5  *   (at your option) any later version.
6  *
7  *   This program is distributed in the hope that it will be useful,
8  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *   GNU General Public License for more details.
11  *
12  *   You should have received a copy of the GNU General Public License
13  *   along with this program; if not, write to the Free Software
14  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16
17 /**
18  * $Id$
19  *
20  * @file process.c
21  * @brief Defines the state machines that control how requests are processed.
22  *
23  * @copyright 2012  The FreeRADIUS server project
24  * @copyright 2012  Alan DeKok <aland@deployingradius.com>
25  */
26
27 RCSID("$Id$")
28
29 #include <freeradius-devel/radiusd.h>
30 #include <freeradius-devel/process.h>
31 #include <freeradius-devel/modules.h>
32 #include <freeradius-devel/state.h>
33
34 #include <freeradius-devel/rad_assert.h>
35
36 #ifdef WITH_DETAIL
37 #include <freeradius-devel/detail.h>
38 #endif
39
40 #include <signal.h>
41 #include <fcntl.h>
42
43 #ifdef HAVE_SYS_WAIT_H
44 #       include <sys/wait.h>
45 #endif
46
47 extern pid_t radius_pid;
48 extern fr_cond_t *debug_condition;
49
50 static bool spawn_flag = false;
51 static bool just_started = true;
52 time_t fr_start_time = (time_t)-1;
53 static rbtree_t *pl = NULL;
54 static fr_event_list_t *el = NULL;
55
56 fr_event_list_t *radius_event_list_corral(UNUSED event_corral_t hint) {
57         /* Currently we do not run a second event loop for modules. */
58         return el;
59 }
60
61 static char const *action_codes[] = {
62         "INVALID",
63         "run",
64         "done",
65         "dup",
66         "timer",
67 #ifdef WITH_PROXY
68         "proxy-reply"
69 #endif
70 };
71
72 #ifdef DEBUG_STATE_MACHINE
73 #define TRACE_STATE_MACHINE if (debug_flag) do { struct timeval debug_tv; \
74                                                  gettimeofday(&debug_tv, NULL);\
75                                                  debug_tv.tv_sec -= fr_start_time;\
76                                                  printf("(%u) %d.%06d ********\tSTATE %s action %s live M-%s C-%s\t********\n",\
77                                                         request->number, (int) debug_tv.tv_sec, (int) debug_tv.tv_usec,  __FUNCTION__, action_codes[action], master_state_names[request->master_state], child_state_names[request->child_state]); } while (0)
78
79 static char const *master_state_names[REQUEST_MASTER_NUM_STATES] = {
80         "?",
81         "active",
82         "stop-processing",
83         "counted"
84 };
85
86 static char const *child_state_names[REQUEST_CHILD_NUM_STATES] = {
87         "?",
88         "queued",
89         "running",
90         "proxied",
91         "reject-delay",
92         "cleanup-delay",
93         "done"
94 };
95
96 #else
97 #define TRACE_STATE_MACHINE {}
98 #endif
99
100 /*
101  *      Declare a state in the state machine.
102  *
103  */
104 #define STATE_MACHINE_DECL(_x) static void CC_HINT(nonnull) _x(REQUEST *request, int action)
105
106 #define STATE_MACHINE_TIMER(_x) request->timer_action = _x; \
107                 fr_event_insert(el, request_timer, request, \
108                                 &when, &request->ev);
109
110 /*
111  *      We need a different VERIFY_REQUEST macro in process.c
112  *      To avoid the race conditions with the master thread
113  *      checking the REQUEST whilst it's being worked on by
114  *      the child.
115  */
116 #if defined(WITH_VERIFY_PTR) && defined(HAVE_PTHREAD_H)
117 #  undef VERIFY_REQUEST
118 #  define VERIFY_REQUEST(_x) if (pthread_equal(pthread_self(), _x->child_pid) != 0) verify_request(__FILE__, __LINE__, _x)
119 #endif
120
121 /**
122  * @section request_timeline
123  *
124  *      Time sequence of a request
125  * @code
126  *
127  *      RQ-----------------P=============================Y-J-C
128  *       ::::::::::::::::::::::::::::::::::::::::::::::::::::::::M
129  * @endcode
130  *
131  * -    R: received.  Duplicate detection is done, and request is
132  *         cached.
133  *
134  * -    Q: Request is placed onto a queue for child threads to pick up.
135  *         If there are no child threads, the request goes immediately
136  *         to P.
137  *
138  * -    P: Processing the request through the modules.
139  *
140  * -    Y: Reply is ready.  Rejects MAY be delayed here.  All other
141  *         replies are sent immediately.
142  *
143  * -    J: Reject is sent "response_delay" after the reply is ready.
144  *
145  * -    C: For Access-Requests, After "cleanup_delay", the request is
146  *         deleted.  Accounting-Request packets go directly from Y to C.
147  *
148  * -    M: Max request time.  If the request hits this timer, it is
149  *         forcibly stopped.
150  *
151  *      Other considerations include duplicate and conflicting
152  *      packets.  When a dupicate packet is received, it is ignored
153  *      until we've reached Y, as no response is ready.  If the reply
154  *      is a reject, duplicates are ignored until J, when we're ready
155  *      to send the reply.  In between the reply being sent (Y or J),
156  *      and C, the server responds to duplicates by sending the cached
157  *      reply.
158  *
159  *      Conflicting packets are sent in 2 situations.
160  *
161  *      The first is in between R and Y.  In that case, we consider
162  *      it as a hint that we're taking too long, and the NAS has given
163  *      up on the request.  We then behave just as if the M timer was
164  *      reached, and we discard the current request.  This allows us
165  *      to process the new one.
166  *
167  *      The second case is when we're at Y, but we haven't yet
168  *      finished processing the request.  This is a race condition in
169  *      the threading code (avoiding locks is faster).  It means that
170  *      a thread has actually encoded and sent the reply, and that the
171  *      NAS has responded with a new packet.  The server can then
172  *      safely mark the current request as "OK to delete", and behaves
173  *      just as if the M timer was reached.  This usually happens only
174  *      in high-load situations.
175  *
176  *      Duplicate packets are sent when the NAS thinks we're taking
177  *      too long, and wants a reply.  From R-Y, duplicates are
178  *      ignored.  From Y-J (for Access-Rejects), duplicates are also
179  *      ignored.  From Y-C, duplicates get a duplicate reply.  *And*,
180  *      they cause the "cleanup_delay" time to be extended.  This
181  *      extension means that we're more likely to send a duplicate
182  *      reply (if we have one), or to suppress processing the packet
183  *      twice if we didn't reply to it.
184  *
185  *      All functions in this file should be thread-safe, and should
186  *      assume thet the REQUEST structure is being accessed
187  *      simultaneously by the main thread, and by the child worker
188  *      threads.  This means that timers, etc. cannot be updated in
189  *      the child thread.
190  *
191  *      Instead, the master thread periodically calls request->process
192  *      with action TIMER.  It's up to the individual functions to
193  *      determine how to handle that.  They need to check if they're
194  *      being called from a child thread or the master, and then do
195  *      different things based on that.
196  */
197
198
199 #ifdef WITH_PROXY
200 static fr_packet_list_t *proxy_list = NULL;
201 static TALLOC_CTX *proxy_ctx = NULL;
202 #endif
203
204 #ifdef HAVE_PTHREAD_H
205 #ifdef WITH_PROXY
206 static pthread_mutex_t proxy_mutex;
207 static bool proxy_no_new_sockets = false;
208 #endif
209
210 #define PTHREAD_MUTEX_LOCK if (spawn_flag) pthread_mutex_lock
211 #define PTHREAD_MUTEX_UNLOCK if (spawn_flag) pthread_mutex_unlock
212
213 static pthread_t NO_SUCH_CHILD_PID;
214 #define NO_CHILD_THREAD request->child_pid = NO_SUCH_CHILD_PID
215
216 #else
217 /*
218  *      This is easier than ifdef's throughout the code.
219  */
220 #define PTHREAD_MUTEX_LOCK(_x)
221 #define PTHREAD_MUTEX_UNLOCK(_x)
222 #define NO_CHILD_THREAD
223 #endif
224
225 #if  defined(HAVE_PTHREAD_H) && !defined (NDEBUG)
226 static bool we_are_master(void)
227 {
228         if (spawn_flag &&
229             (pthread_equal(pthread_self(), NO_SUCH_CHILD_PID) == 0)) {
230                 return false;
231         }
232
233         return true;
234 }
235 #define ASSERT_MASTER   if (!we_are_master()) rad_panic("We are not master")
236
237 #else
238 #define we_are_master(_x) (1)
239 #define ASSERT_MASTER
240 #endif
241
242 static int event_new_fd(rad_listen_t *this);
243
244 /*
245  *      We need mutexes around the event FD list *only* in certain
246  *      cases.
247  */
248 #if defined (HAVE_PTHREAD_H) && (defined(WITH_PROXY) || defined(WITH_TCP))
249 static rad_listen_t *new_listeners = NULL;
250
251 static pthread_mutex_t  fd_mutex;
252 #define FD_MUTEX_LOCK if (spawn_flag) pthread_mutex_lock
253 #define FD_MUTEX_UNLOCK if (spawn_flag) pthread_mutex_unlock
254
255 void radius_update_listener(rad_listen_t *this)
256 {
257         /*
258          *      Just do it ourselves.
259          */
260         if (we_are_master()) {
261                 event_new_fd(this);
262                 return;
263         }
264
265         FD_MUTEX_LOCK(&fd_mutex);
266
267         /*
268          *      If it's already in the list, don't add it again.
269          */
270         if (this->next) {
271                 FD_MUTEX_UNLOCK(&fd_mutex);
272                 return;
273         }
274
275         /*
276          *      Otherwise, add it to the list
277          */
278         this->next = new_listeners;
279         new_listeners = this;
280         FD_MUTEX_UNLOCK(&fd_mutex);
281         radius_signal_self(RADIUS_SIGNAL_SELF_NEW_FD);
282 }
283 #else
284 void radius_update_listener(rad_listen_t *this)
285 {
286         /*
287          *      No threads.  Just insert it.
288          */
289         event_new_fd(this);
290 }
291 /*
292  *      This is easier than ifdef's throughout the code.
293  */
294 #define FD_MUTEX_LOCK(_x)
295 #define FD_MUTEX_UNLOCK(_x)
296 #endif
297
298 static int request_num_counter = 1;
299 #ifdef WITH_PROXY
300 static int request_will_proxy(REQUEST *request);
301 static int request_proxy(REQUEST *request, int retransmit);
302 STATE_MACHINE_DECL(proxy_wait_for_reply);
303 STATE_MACHINE_DECL(proxy_no_reply);
304 STATE_MACHINE_DECL(proxy_running);
305 static int process_proxy_reply(REQUEST *request, RADIUS_PACKET *reply);
306 static void remove_from_proxy_hash(REQUEST *request);
307 static void remove_from_proxy_hash_nl(REQUEST *request, bool yank);
308 static int insert_into_proxy_hash(REQUEST *request);
309 #endif
310
311 static REQUEST *request_setup(TALLOC_CTX *ctx, rad_listen_t *listener, RADIUS_PACKET *packet,
312                               RADCLIENT *client, RAD_REQUEST_FUNP fun);
313
314 STATE_MACHINE_DECL(request_common);
315 STATE_MACHINE_DECL(request_response_delay);
316 STATE_MACHINE_DECL(request_cleanup_delay);
317 STATE_MACHINE_DECL(request_running);
318 #ifdef WITH_COA
319 static void request_coa_originate(REQUEST *request);
320 STATE_MACHINE_DECL(coa_running);
321 STATE_MACHINE_DECL(coa_wait_for_reply);
322 STATE_MACHINE_DECL(coa_no_reply);
323 STATE_MACHINE_DECL(coa_separate);
324 #endif
325
326 #undef USEC
327 #define USEC (1000000)
328
329 #define INSERT_EVENT(_function, _ctx) if (!fr_event_insert(el, _function, _ctx, &((_ctx)->when), &((_ctx)->ev))) { _rad_panic(__FILE__, __LINE__, "Failed to insert event"); }
330
331 static NEVER_RETURNS void _rad_panic(char const *file, unsigned int line, char const *msg)
332 {
333         ERROR("[%s:%d] %s", file, line, msg);
334 #ifndef NDEBUG
335         rad_assert(0 == 1);
336 #endif
337         fr_exit(1);
338 }
339
340 #define rad_panic(x) _rad_panic(__FILE__, __LINE__, x)
341
342 static void tv_add(struct timeval *tv, int usec_delay)
343 {
344         if (usec_delay >= USEC) {
345                 tv->tv_sec += usec_delay / USEC;
346                 usec_delay %= USEC;
347         }
348         tv->tv_usec += usec_delay;
349
350         if (tv->tv_usec >= USEC) {
351                 tv->tv_sec += tv->tv_usec / USEC;
352                 tv->tv_usec %= USEC;
353         }
354 }
355
356 /*
357  *      Debug the packet if requested.
358  */
359 static void debug_packet(REQUEST *request, RADIUS_PACKET *packet, bool received)
360 {
361         char src_ipaddr[128];
362         char dst_ipaddr[128];
363
364         if (!packet) return;
365         if (!RDEBUG_ENABLED) return;
366
367         /*
368          *      Client-specific debugging re-prints the input
369          *      packet into the client log.
370          *
371          *      This really belongs in a utility library
372          */
373         if (is_radius_code(packet->code)) {
374                 RDEBUG("%s %s Id %i from %s:%i to %s:%i length %zu",
375                        received ? "Received" : "Sent",
376                        fr_packet_codes[packet->code],
377                        packet->id,
378                        inet_ntop(packet->src_ipaddr.af,
379                                  &packet->src_ipaddr.ipaddr,
380                                  src_ipaddr, sizeof(src_ipaddr)),
381                        packet->src_port,
382                        inet_ntop(packet->dst_ipaddr.af,
383                                  &packet->dst_ipaddr.ipaddr,
384                                  dst_ipaddr, sizeof(dst_ipaddr)),
385                        packet->dst_port,
386                        packet->data_len);
387         } else {
388                 RDEBUG("%s code %i Id %i from %s:%i to %s:%i length %zu",
389                        received ? "Received" : "Sent",
390                        packet->code,
391                        packet->id,
392                        inet_ntop(packet->src_ipaddr.af,
393                                  &packet->src_ipaddr.ipaddr,
394                                  src_ipaddr, sizeof(src_ipaddr)),
395                        packet->src_port,
396                        inet_ntop(packet->dst_ipaddr.af,
397                                  &packet->dst_ipaddr.ipaddr,
398                                  dst_ipaddr, sizeof(dst_ipaddr)),
399                        packet->dst_port,
400                        packet->data_len);
401         }
402
403         if (received) {
404                 rdebug_pair_list(L_DBG_LVL_1, request, packet->vps, NULL);
405         } else {
406                 rdebug_proto_pair_list(L_DBG_LVL_1, request, packet->vps);
407         }
408 }
409
410
411 /***********************************************************************
412  *
413  *      Start of RADIUS server state machine.
414  *
415  ***********************************************************************/
416
417 static struct timeval *request_response_window(REQUEST *request)
418 {
419         VERIFY_REQUEST(request);
420
421         if (request->client) {
422                 /*
423                  *      The client hasn't set the response window.  Return
424                  *      either the home server one, if set, or the global one.
425                  */
426                 if (!timerisset(&request->client->response_window)) {
427                         return &request->home_server->response_window;
428                 }
429
430                 if (timercmp(&request->client->response_window,
431                              &request->home_server->response_window, <)) {
432                         return &request->client->response_window;
433                 }
434         }
435
436         rad_assert(request->home_server != NULL);
437         return &request->home_server->response_window;
438 }
439
440 /*
441  * Determine initial request processing delay.
442  */
443 static int request_init_delay(REQUEST *request)
444 {
445         struct timeval half_response_window;
446
447         VERIFY_REQUEST(request);
448
449         /* Allow client response window to lower initial delay */
450         if (timerisset(&request->client->response_window)) {
451                 half_response_window.tv_sec = request->client->response_window.tv_sec >> 1;
452                 half_response_window.tv_usec =
453                         ((request->client->response_window.tv_sec & 1) * USEC +
454                                 request->client->response_window.tv_usec) >> 1;
455                 if (timercmp(&half_response_window, &request->root->init_delay, <))
456                         return (int)half_response_window.tv_sec * USEC +
457                                 (int)half_response_window.tv_usec;
458         }
459
460         return (int)request->root->init_delay.tv_sec * USEC +
461                 (int)request->root->init_delay.tv_usec;
462 }
463
464 /*
465  *      Callback for ALL timer events related to the request.
466  */
467 static void request_timer(void *ctx)
468 {
469         REQUEST *request = talloc_get_type_abort(ctx, REQUEST);
470         int action;
471
472         action = request->timer_action;
473
474         TRACE_STATE_MACHINE;
475
476         request->process(request, action);
477 }
478
479 /*
480  *      Wrapper for talloc pools.  If there's no parent, just free the
481  *      request.  If there is a parent, free the parent INSTEAD of the
482  *      request.
483  */
484 static void request_free(REQUEST *request)
485 {
486         void *ptr;
487
488         rad_assert(request->ev == NULL);
489         rad_assert(!request->in_request_hash);
490         rad_assert(!request->in_proxy_hash);
491
492         if ((request->options & RAD_REQUEST_OPTION_CTX) == 0) {
493                 talloc_free(request);
494                 return;
495         }
496
497         ptr = talloc_parent(request);
498         rad_assert(ptr != NULL);
499         talloc_free(ptr);
500 }
501
502
503 /*
504  *      Only ever called from the master thread.
505  */
506 STATE_MACHINE_DECL(request_done)
507 {
508         struct timeval now, when;
509 #ifdef WITH_PROXY
510         char buffer[128];
511 #endif
512
513         VERIFY_REQUEST(request);
514
515         TRACE_STATE_MACHINE;
516
517         /*
518          *      Force this no matter what.
519          */
520         request->process = request_done;
521
522 #ifdef WITH_DETAIL
523         /*
524          *      Tell the detail listener that we're done.
525          */
526         if (request->listener &&
527             (request->listener->type == RAD_LISTEN_DETAIL) &&
528             (request->simul_max != 1)) {
529                 request->simul_max = 1;
530                 request->listener->send(request->listener,
531                                         request);
532         }
533 #endif
534
535 #ifdef HAVE_PTHREAD_H
536         /*
537          *      If called from a child thread, mark ourselves as done,
538          *      and wait for the master thread timer to clean us up.
539          */
540         if (!we_are_master()) {
541                 NO_CHILD_THREAD;
542                 request->child_state = REQUEST_DONE;
543                 return;
544         }
545 #endif
546
547         /*
548          *      Mark the request as STOP.
549          */
550         request->master_state = REQUEST_STOP_PROCESSING;
551
552 #ifdef WITH_COA
553         /*
554          *      Move the CoA request to its own handler.
555          */
556         if (request->coa) {
557                 coa_separate(request->coa, FR_ACTION_TIMER);
558         } else if (request->parent && (request->parent->coa == request)) {
559                 coa_separate(request, FR_ACTION_TIMER);
560         }
561 #endif
562
563         /*
564          *      It doesn't hurt to send duplicate replies.  All other
565          *      signals are ignored, as the request will be cleaned up
566          *      soon anyways.
567          */
568         switch (action) {
569         case FR_ACTION_DUP:
570 #ifdef WITH_DETAIL
571                 rad_assert(request->listener != NULL);
572 #endif
573                 if (request->reply->code != 0) {
574                         request->listener->send(request->listener, request);
575                         return;
576                 } else {
577                         RDEBUG("No reply.  Ignoring retransmit");
578                 }
579                 break;
580
581                 /*
582                  *      Mark the request as done.
583                  */
584         case FR_ACTION_DONE:
585 #ifdef HAVE_PTHREAD_H
586                 /*
587                  *      If the child is still running, leave it alone.
588                  */
589                 if (spawn_flag && (request->child_state <= REQUEST_RUNNING)) {
590                         break;
591                 }
592 #endif
593
594 #ifdef DEBUG_STATE_MACHINE
595                 if (debug_flag) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n",
596                                        request->number, __FUNCTION__,
597                                        child_state_names[request->child_state],
598                                        child_state_names[REQUEST_DONE]);
599 #endif
600                 request->child_state = REQUEST_DONE;
601                 break;
602
603                 /*
604                  *      Called when the child is taking too long to
605                  *      finish.  We've already marked it "please
606                  *      stop", so we don't complain any more.
607                  */
608         case FR_ACTION_TIMER:
609                 break;
610
611 #ifdef WITH_PROXY
612                 /*
613                  *      Child is still alive, and we're receiving more
614                  *      packets from the home server.
615                  */
616         case FR_ACTION_PROXY_REPLY:
617                 RDEBUG2("Reply from home server %s port %d  - ID: %d arrived too late.  Try increasing 'retry_delay' or 'max_request_time'",
618                        inet_ntop(request->proxy->src_ipaddr.af,
619                                  &request->proxy->src_ipaddr.ipaddr,
620                                  buffer, sizeof(buffer)),
621                         request->proxy->dst_port, request->proxy->id);
622                 return;
623 #endif
624
625         default:
626                 break;
627         }
628
629         /*
630          *      Remove it from the request hash.
631          */
632         if (request->in_request_hash) {
633                 if (!rbtree_deletebydata(pl, &request->packet)) {
634                         rad_assert(0 == 1);
635                 }
636                 request->in_request_hash = false;
637         }
638
639 #ifdef WITH_PROXY
640         /*
641          *      Wait for the proxy ID to expire.  This allows us to
642          *      avoid re-use of proxy IDs for a while.
643          */
644         if (request->in_proxy_hash) {
645                 rad_assert(request->proxy != NULL);
646
647                 fr_event_now(el, &now);
648                 when = request->proxy->timestamp;
649
650 #ifdef WITH_COA
651                 if (((request->proxy->code == PW_CODE_COA_REQUEST) ||
652                      (request->proxy->code == PW_CODE_DISCONNECT_REQUEST)) &&
653                     (request->packet->code != request->proxy->code)) {
654                         when.tv_sec += request->home_server->coa_mrd;
655                 } else
656 #endif
657                         timeradd(&when, request_response_window(request), &when);
658
659                 /*
660                  *      We haven't received all responses, AND there's still
661                  *      time to wait.  Do so.
662                  */
663                 if ((request->num_proxied_requests > request->num_proxied_responses) &&
664 #ifdef WITH_TCP
665                     (request->home_server->proto != IPPROTO_TCP) &&
666 #endif
667                     timercmp(&now, &when, <)) {
668                         RDEBUG("Waiting for more responses from the home server");
669                         goto wait_some_more;
670                 }
671
672                 /*
673                  *      Time to remove it.
674                  */
675                 remove_from_proxy_hash(request);
676         }
677 #endif
678
679 #ifdef HAVE_PTHREAD_H
680         /*
681          *      If there's no children, we can mark the request as done.
682          */
683         if (!spawn_flag) request->child_state = REQUEST_DONE;
684 #endif
685
686         /*
687          *      If the child is still running, wait for it to be finished.
688          */
689         if (request->child_state <= REQUEST_RUNNING) {
690                 gettimeofday(&now, NULL);
691 #ifdef WITH_PROXY
692         wait_some_more:
693 #endif
694                 when = now;
695                 if (request->delay < (USEC / 3)) request->delay = USEC / 3;
696                 tv_add(&when, request->delay);
697                 request->delay += request->delay >> 1;
698                 if (request->delay > (10 * USEC)) request->delay = 10 * USEC;
699
700                 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
701                 return;
702         }
703
704 #ifdef HAVE_PTHREAD_H
705         rad_assert(request->child_pid == NO_SUCH_CHILD_PID);
706 #endif
707
708         /*
709          *      @todo: do final states for TCP sockets, too?
710          */
711         request_stats_final(request);
712 #ifdef WITH_TCP
713         if (request->listener) {
714                 request->listener->count--;
715
716                 /*
717                  *      If we're the last one, remove the listener now.
718                  */
719                 if ((request->listener->count == 0) &&
720                     (request->listener->status == RAD_LISTEN_STATUS_EOL)) {
721                         request->listener->status = RAD_LISTEN_STATUS_REMOVE_NOW;
722                         event_new_fd(request->listener);
723                 }
724         }
725 #endif
726
727         if (request->packet) {
728                 RDEBUG2("Cleaning up request packet ID %u with timestamp +%d",
729                         request->packet->id,
730                         (unsigned int) (request->timestamp - fr_start_time));
731         } /* else don't print anything */
732
733         ASSERT_MASTER;
734         fr_event_delete(el, &request->ev);
735         request_free(request);
736 }
737
738
739 static void request_cleanup_delay_init(REQUEST *request, struct timeval const *pnow)
740 {
741         struct timeval now, when;
742
743         VERIFY_REQUEST(request);
744
745         if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) goto done;
746
747 #ifdef WITH_DETAIL
748         /*
749          *      If the packets are from the detail file, we can clean them up now.
750          */
751         if (request->listener->type == RAD_LISTEN_DETAIL) goto done;
752 #endif
753
754         if (!request->root->cleanup_delay) goto done;
755
756         if (pnow) {
757                 now = *pnow;
758         } else {
759                 gettimeofday(&now, NULL);
760         }
761
762         rad_assert(request->reply->timestamp.tv_sec != 0);
763         when = request->reply->timestamp;
764
765         request->delay = request->root->cleanup_delay;
766         when.tv_sec += request->delay;
767
768         /*
769          *      Set timer for when we need to clean it up.
770          */
771         if (timercmp(&when, &now, >)) {
772 #ifdef DEBUG_STATE_MACHINE
773                 if (debug_flag) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_cleanup_delay");
774 #endif
775                 request->process = request_cleanup_delay;
776                 request->child_state = REQUEST_DONE;
777                 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
778                 return;
779         }
780
781         /*
782          *      Otherwise just clean it up.
783          */
784 done:
785         request_done(request, FR_ACTION_DONE);
786 }
787
788
789 /*
790  *      Function to do all time-related events.
791  */
792 static void request_process_timer(REQUEST *request)
793 {
794         struct timeval now, when;
795         rad_assert(request->magic == REQUEST_MAGIC);
796 #ifdef DEBUG_STATE_MACHINE
797         int action = FR_ACTION_TIMER;
798 #endif
799
800         VERIFY_REQUEST(request);
801
802         TRACE_STATE_MACHINE;
803         ASSERT_MASTER;
804
805 #ifdef WITH_COA
806         /*
807          *      If we originated a CoA request, divorce it from the
808          *      parent.  Then, set up the timers so that we can clean
809          *      it up as appropriate.
810          */
811         if (request->coa) coa_separate(request->coa, FR_ACTION_TIMER);
812
813         /*
814          *      If we're the request, OR it isn't originating a CoA
815          *      request, check more things.
816          */
817         if (!request->proxy || (request->packet->code == request->proxy->code))
818 #endif
819         {
820                 rad_assert(request->listener != NULL);
821
822                 /*
823                  *      The socket was closed.  Tell the request that
824                  *      there is no point in continuing.
825                  */
826                 if (request->listener->status != RAD_LISTEN_STATUS_KNOWN) {
827                         if ((request->master_state == REQUEST_ACTIVE) &&
828                             (request->child_state < REQUEST_RESPONSE_DELAY)) {
829                                 WARN("Socket was closed while processing request %u: Stopping it.", request->number);
830                                 request->master_state = REQUEST_STOP_PROCESSING;
831                         }
832                 }
833         }
834
835         gettimeofday(&now, NULL);
836
837         /*
838          *      The request was forcibly stopped.
839          */
840         if (request->master_state == REQUEST_STOP_PROCESSING) {
841                 switch (request->child_state) {
842                 case REQUEST_QUEUED:
843                 case REQUEST_RUNNING:
844 #ifdef HAVE_PTHREAD_H
845                         rad_assert(spawn_flag == true);
846 #endif
847
848                 delay:
849                         /*
850                          *      Sleep for some more.  We HOPE that the
851                          *      child will become responsive at some
852                          *      point in the future.
853                          */
854                         when = now;
855                         tv_add(&when, request->delay);
856                         request->delay += request->delay >> 1;
857                         STATE_MACHINE_TIMER(FR_ACTION_TIMER);
858                         return;
859
860                         /*
861                          *      These should all be managed by the master thread
862                          */
863 #ifdef WITH_PROXY
864                 case REQUEST_PROXIED:
865 #endif
866                 case REQUEST_RESPONSE_DELAY:
867                 case REQUEST_CLEANUP_DELAY:
868                 case REQUEST_DONE:
869                 done:
870                         request_done(request, FR_ACTION_DONE);
871                         return;
872                 }
873         }
874
875         rad_assert(request->master_state == REQUEST_ACTIVE);
876
877         /*
878          *      It's still supposed to be running.
879          */
880         switch (request->child_state) {
881         case REQUEST_QUEUED:
882         case REQUEST_RUNNING:
883                 when = request->packet->timestamp;
884                 when.tv_sec += request->root->max_request_time;
885
886                 /*
887                  *      Taking too long: tell it to die.
888                  */
889                 if (timercmp(&now, &when, >=)) {
890 #ifdef HAVE_PTHREAD_H
891                         /*
892                          *      If there's a child thread processing it,
893                          *      complain.
894                          */
895                         if (spawn_flag &&
896                             (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0)) {
897                                 ERROR("Unresponsive child for request %u, in component %s module %s",
898                                       request->number,
899                                       request->component ? request->component : "<core>",
900                                       request->module ? request->module : "<core>");
901                                 exec_trigger(request, NULL, "server.thread.unresponsive", true);
902                         }
903 #endif
904                         request->master_state = REQUEST_STOP_PROCESSING;
905                 }
906                 goto delay;     /* sleep some more */
907
908 #ifdef WITH_PROXY
909         case REQUEST_PROXIED:
910                 when = request->packet->timestamp;
911                 when.tv_sec += request->root->max_request_time;
912
913                 if (timercmp(&now, &when, >=)) {
914                         RWDEBUG("No response to proxied request in 'max_request_time'.  Stopping it.");
915                         request->master_state = REQUEST_STOP_PROCESSING;
916                         request_done(request, FR_ACTION_DONE);
917                         break;
918                 }
919
920                 rad_assert(request->proxy != NULL);
921
922                 /*
923                  *      Delay some more, hoping that we get a response.
924                  */
925                 when = request->proxy->timestamp;
926                 tv_add(&when, request->delay);
927
928                 if (timercmp(&now, &when, >=)) {
929                         request->process(request, FR_ACTION_TIMER);
930                         return;
931                 }
932
933                 /*
934                  *      Otherwise set the timer for the future.
935                  */
936                 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
937                 return;
938 #endif  /* WITH_PROXY */
939
940         case REQUEST_RESPONSE_DELAY:
941                 rad_assert(request->response_delay.tv_sec > 0);
942 #ifdef WITH_COA
943                 rad_assert(!request->proxy || (request->packet->code == request->proxy->code));
944 #endif
945
946                 request->process = request_response_delay;
947
948                 when = request->reply->timestamp;
949
950                 tv_add(&when, request->response_delay.tv_sec * USEC);
951                 tv_add(&when, request->response_delay.tv_usec);
952
953                 if (timercmp(&when, &now, >)) {
954 #ifdef DEBUG_STATE_MACHINE
955                         if (debug_flag) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_response_delay");
956 #endif
957                         STATE_MACHINE_TIMER(FR_ACTION_TIMER);
958                         return;
959                 } /* else it's time to send the reject */
960
961                 RDEBUG2("Sending delayed response");
962                 request->listener->send(request->listener, request);
963                 debug_packet(request, request->reply, false);
964                 request->child_state = REQUEST_CLEANUP_DELAY;
965                 /* FALL-THROUGH */
966
967         case REQUEST_CLEANUP_DELAY:
968                 rad_assert(request->root->cleanup_delay > 0);
969
970 #ifdef WITH_COA
971                 rad_assert(!request->proxy || (request->packet->code == request->proxy->code));
972 #endif
973
974                 request->process = request_cleanup_delay;
975
976                 when = request->reply->timestamp;
977                 when.tv_sec += request->root->cleanup_delay;
978
979                 if (timercmp(&when, &now, >)) {
980 #ifdef DEBUG_STATE_MACHINE
981                         if (debug_flag) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_cleanup_delay");
982 #endif
983                         STATE_MACHINE_TIMER(FR_ACTION_TIMER);
984                         return;
985                 } /* else it's time to clean up */
986                 /* FALL-THROUGH */
987
988         case REQUEST_DONE:
989                 goto done;
990         }
991
992 }
993
994 static void request_queue_or_run(REQUEST *request,
995                                  fr_request_process_t process)
996 {
997 #ifdef DEBUG_STATE_MACHINE
998         int action = FR_ACTION_TIMER;
999 #endif
1000
1001         VERIFY_REQUEST(request);
1002
1003         TRACE_STATE_MACHINE;
1004
1005         /*
1006          *      Do this here so that fewer other functions need to do
1007          *      it.
1008          */
1009         if (request->master_state == REQUEST_STOP_PROCESSING) {
1010 #ifdef DEBUG_STATE_MACHINE
1011                 if (debug_flag) printf("(%u) ********\tSTATE %s M-%s causes C-%s-> C-%s\t********\n",
1012                                        request->number, __FUNCTION__,
1013                                        master_state_names[request->master_state],
1014                                        child_state_names[request->child_state],
1015                                        child_state_names[REQUEST_DONE]);
1016 #endif
1017                 request_done(request, FR_ACTION_DONE);
1018                 return;
1019         }
1020
1021         request->process = process;
1022
1023         if (we_are_master()) {
1024                 struct timeval when;
1025
1026                 /*
1027                  *      (re) set the initial delay.
1028                  */
1029                 request->delay = request_init_delay(request);
1030                 if (request->delay > USEC) request->delay = USEC;
1031                 gettimeofday(&when, NULL);
1032                 tv_add(&when, request->delay);
1033                 request->delay += request->delay >> 1;
1034
1035                 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
1036
1037 #ifdef HAVE_PTHREAD_H
1038                 if (spawn_flag) {
1039                         /*
1040                          *      A child thread will eventually pick it up.
1041                          */
1042                         if (request_enqueue(request)) return;
1043
1044                         /*
1045                          *      Otherwise we're not going to do anything with
1046                          *      it...
1047                          */
1048                         request_done(request, FR_ACTION_DONE);
1049                         return;
1050                 }
1051 #endif
1052         }
1053
1054         request->child_state = REQUEST_RUNNING;
1055         request->process(request, FR_ACTION_RUN);
1056
1057 #ifdef WNOHANG
1058         /*
1059          *      Requests that care about child process exit
1060          *      codes have already either called
1061          *      rad_waitpid(), or they've given up.
1062          */
1063         while (waitpid(-1, NULL, WNOHANG) > 0);
1064 #endif
1065 }
1066
1067 STATE_MACHINE_DECL(request_common)
1068 {
1069 #ifdef WITH_PROXY
1070         char buffer[128];
1071 #endif
1072
1073         VERIFY_REQUEST(request);
1074
1075         TRACE_STATE_MACHINE;
1076         ASSERT_MASTER;
1077
1078         /*
1079          *      Bail out as early as possible.
1080          */
1081         if (request->master_state == REQUEST_STOP_PROCESSING) {
1082                 request_done(request, FR_ACTION_DONE);
1083                 return;
1084         }
1085
1086         switch (action) {
1087         case FR_ACTION_DUP:
1088                 ERROR("(%u) Ignoring duplicate packet from "
1089                       "client %s port %d - ID: %u due to unfinished request "
1090                       "in component %s module %s",
1091                       request->number, request->client->shortname,
1092                       request->packet->src_port,request->packet->id,
1093                       request->component, request->module);
1094                 break;
1095
1096         case FR_ACTION_TIMER:
1097                 request_process_timer(request);
1098                 return;
1099
1100 #ifdef WITH_PROXY
1101         case FR_ACTION_PROXY_REPLY:
1102                 RDEBUG2("Reply from home server %s port %d  - ID: %d arrived too late.  Try increasing 'retry_delay' or 'max_request_time'",
1103                         inet_ntop(request->proxy->dst_ipaddr.af,
1104                                  &request->proxy->dst_ipaddr.ipaddr,
1105                                  buffer, sizeof(buffer)),
1106                         request->proxy->dst_port, request->proxy->id);
1107                 return;
1108 #endif
1109
1110         default:
1111                 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
1112                 break;
1113         }
1114 }
1115
1116 STATE_MACHINE_DECL(request_cleanup_delay)
1117 {
1118         struct timeval when;
1119
1120         VERIFY_REQUEST(request);
1121
1122         TRACE_STATE_MACHINE;
1123         ASSERT_MASTER;
1124
1125         switch (action) {
1126         case FR_ACTION_DUP:
1127                 if (request->reply->code != 0) {
1128                         request->listener->send(request->listener, request);
1129                 } else {
1130                         RDEBUG("No reply.  Ignoring retransmit");
1131                 }
1132
1133                 /*
1134                  *      Double the cleanup_delay to catch retransmits.
1135                  */
1136                 when = request->reply->timestamp;
1137                 request->delay += request->delay ;
1138                 when.tv_sec += request->delay;
1139
1140                 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
1141                 return;
1142
1143 #ifdef WITH_PROXY
1144         case FR_ACTION_PROXY_REPLY:
1145 #endif
1146         case FR_ACTION_TIMER:
1147                 request_common(request, action);
1148                 return;
1149
1150         default:
1151                 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
1152                 break;
1153         }
1154 }
1155
1156 STATE_MACHINE_DECL(request_response_delay)
1157 {
1158         VERIFY_REQUEST(request);
1159
1160         TRACE_STATE_MACHINE;
1161         ASSERT_MASTER;
1162
1163         switch (action) {
1164         case FR_ACTION_DUP:
1165                 ERROR("(%u) Discarding duplicate request from "
1166                        "client %s port %d - ID: %u due to delayed response",
1167                        request->number, request->client->shortname,
1168                        request->packet->src_port,request->packet->id);
1169                 return;
1170
1171 #ifdef WITH_PROXY
1172         case FR_ACTION_PROXY_REPLY:
1173 #endif
1174         case FR_ACTION_TIMER:
1175                 request_common(request, action);
1176                 break;
1177
1178         default:
1179                 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
1180                 break;
1181         }
1182 }
1183
1184
1185 static int CC_HINT(nonnull) request_pre_handler(REQUEST *request, UNUSED int action)
1186 {
1187         int rcode;
1188
1189         VERIFY_REQUEST(request);
1190
1191         TRACE_STATE_MACHINE;
1192
1193         if (request->master_state == REQUEST_STOP_PROCESSING) return 0;
1194
1195         /*
1196          *      Don't decode the packet if it's an internal "fake"
1197          *      request.  Instead, just return so that the caller can
1198          *      process it.
1199          */
1200         if (request->packet->dst_port == 0) {
1201                 request->username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1202                 request->password = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
1203                 return 1;
1204         }
1205
1206         if (!request->packet->vps) { /* FIXME: check for correct state */
1207                 rcode = request->listener->decode(request->listener, request);
1208
1209 #ifdef WITH_UNLANG
1210                 if (debug_condition) {
1211                         /*
1212                          *      Ignore parse errors.
1213                          */
1214                         if (radius_evaluate_cond(request, RLM_MODULE_OK, 0, debug_condition)) {
1215                                 request->log.lvl = L_DBG_LVL_2;
1216                                 request->log.func = vradlog_request;
1217                         }
1218                 }
1219 #endif
1220
1221                 debug_packet(request, request->packet, true);
1222         } else {
1223                 rcode = 0;
1224         }
1225
1226         if (rcode < 0) {
1227                 RATE_LIMIT(INFO("Dropping packet without response because of error: %s", fr_strerror()));
1228                 request->reply->offset = -2; /* bad authenticator */
1229                 return 0;
1230         }
1231
1232         if (!request->username) {
1233                 request->username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1234         }
1235
1236         return 1;
1237 }
1238
1239 STATE_MACHINE_DECL(request_finish)
1240 {
1241         VALUE_PAIR *vp;
1242
1243         VERIFY_REQUEST(request);
1244
1245         TRACE_STATE_MACHINE;
1246
1247         (void) action;  /* -Wunused */
1248
1249         if (request->master_state == REQUEST_STOP_PROCESSING) {
1250 #ifdef WITH_DETAIL
1251                 /*
1252                  *      Always send a reply to the detail listener.
1253                  */
1254                 if (request->listener->type == RAD_LISTEN_DETAIL) {
1255                         goto do_detail;
1256                 }
1257 #endif
1258                 NO_CHILD_THREAD;
1259                 request->child_state = REQUEST_DONE;
1260                 return;
1261         }
1262
1263 #ifdef WITH_COA
1264         /*
1265          *      Don't do post-auth if we're a CoA request originated
1266          *      from an Access-Request.  See request_alloc_coa() for
1267          *      details.
1268          */
1269         if ((request->options & RAD_REQUEST_OPTION_COA) != 0) goto done;
1270 #endif
1271
1272         /*
1273          *      Override the response code if a control:Response-Packet-Type attribute is present.
1274          */
1275         vp = pairfind(request->config_items, PW_RESPONSE_PACKET_TYPE, 0, TAG_ANY);
1276         if (vp) {
1277                 if (vp->vp_integer == 256) {
1278                         RDEBUG2("Not responding to request");
1279                         request->reply->code = 0;
1280                 } else {
1281                         request->reply->code = vp->vp_integer;
1282                 }
1283         }
1284         /*
1285          *      Catch Auth-Type := Reject BEFORE proxying the packet.
1286          */
1287         else if (request->packet->code == PW_CODE_ACCESS_REQUEST) {
1288                 if (request->reply->code == 0) {
1289                         vp = pairfind(request->config_items, PW_AUTH_TYPE, 0, TAG_ANY);
1290
1291                         if (!vp || (vp->vp_integer != PW_CODE_ACCESS_REJECT)) {
1292                                 RDEBUG2("There was no response configured: "
1293                                         "rejecting request");
1294                         }
1295
1296                         request->reply->code = PW_CODE_ACCESS_REJECT;
1297                 }
1298         }
1299
1300         /*
1301          *      Copy Proxy-State from the request to the reply.
1302          */
1303         vp = paircopy_by_num(request->reply, request->packet->vps,
1304                        PW_PROXY_STATE, 0, TAG_ANY);
1305         if (vp) pairadd(&request->reply->vps, vp);
1306
1307         /*
1308          *      Call Post-Auth for Access-Request packets.
1309          */
1310         if (request->packet->code == PW_CODE_ACCESS_REQUEST) {
1311                 rad_postauth(request);
1312         }
1313
1314
1315         /*
1316          *      Clean up.  These are no longer needed.
1317          */
1318         gettimeofday(&request->reply->timestamp, NULL);
1319
1320         /*
1321          *      Fake packets get marked as "done", and have the
1322          *      proxy-reply section deal with the reply attributes.
1323          *      We therefore don't free the reply attributes.
1324          */
1325         if (request->packet->dst_port == 0) {
1326                 RDEBUG("Finished internally proxied request.");
1327                 NO_CHILD_THREAD;
1328                 request->child_state = REQUEST_DONE;
1329                 return;
1330         }
1331
1332 #ifdef WITH_DETAIL
1333         /*
1334          *      Always send the reply to the detail listener.
1335          */
1336         if (request->listener->type == RAD_LISTEN_DETAIL) {
1337         do_detail:
1338                 request->simul_max = 1;
1339                 request->listener->send(request->listener, request);
1340                 /*
1341                  *      But only print the reply if there is one.
1342                  */
1343                 if (request->reply->code != 0) {
1344                         debug_packet(request, request->reply, false);
1345                 }
1346                 goto done;
1347         }
1348 #endif
1349
1350         /*
1351          *      Ignore all "do not respond" packets.
1352          *      Except for the detail ones, which need to ping
1353          *      the detail file reader so that it will retransmit.
1354          */
1355         if (!request->reply->code) {
1356                 RDEBUG("Not sending reply to client.");
1357                 goto done;
1358         }
1359
1360         /*
1361          *      If it's not in the request hash, we MIGHT not want to
1362          *      send a reply.
1363          *
1364          *      If duplicate packets are allowed, then then only
1365          *      reason to NOT be in the request hash is because we
1366          *      don't want to send a reply.
1367          *
1368          *      FIXME: this is crap.  The rest of the state handling
1369          *      should use a different field so that we don't have two
1370          *      meanings for it.
1371          *
1372          *      Otherwise duplicates are forbidden, and the request is
1373          *      SUPPOSED to avoid the request hash.
1374          *
1375          *      In that case, we need to send a reply.
1376          */
1377         if (!request->in_request_hash &&
1378             !request->listener->nodup) {
1379                 RDEBUG("Suppressing reply to client.");
1380                 goto done;
1381         }
1382
1383         /*
1384          *      See if we need to delay an Access-Reject packet.
1385          */
1386         if ((request->reply->code == PW_CODE_ACCESS_REJECT) &&
1387             (request->root->reject_delay.tv_sec > 0)) {
1388                 request->response_delay = request->root->reject_delay;
1389
1390 #ifdef WITH_PROXY
1391                 /*
1392                  *      If we timed out a proxy packet, don't delay
1393                  *      the reject any more.
1394                  */
1395                 if (request->proxy && !request->proxy_reply) {
1396                         request->response_delay.tv_sec = 0;
1397                         request->response_delay.tv_usec = 0;
1398                 }
1399 #endif
1400         }
1401
1402         /*
1403          *      Send the reply.
1404          */
1405         if (request->response_delay.tv_sec == 0) {
1406                 rad_assert(request->response_delay.tv_usec == 0);
1407
1408                 /*
1409                  *      Don't print a reply if there's none to send.
1410                  */
1411                 if (request->reply->code != 0) {
1412                         request->listener->send(request->listener, request);
1413                         debug_packet(request, request->reply, false);
1414                 }
1415
1416         done:
1417                 RDEBUG2("Finished request");
1418                 request->component = "<core>";
1419                 request->module = "<done>";
1420
1421 #ifdef WITH_ACCOUNTING
1422                 /*
1423                  *      Accounting packets can be cleaned up now.
1424                  */
1425                 if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) {
1426                         NO_CHILD_THREAD;
1427                         request->child_state = REQUEST_DONE;
1428                         return;
1429                 }
1430 #endif
1431
1432 #ifdef WITH_DETAIL
1433                 /*
1434                  *      If the packets are from the detail file, we can clean them up now.
1435                  */
1436                 if (request->listener->type == RAD_LISTEN_DETAIL) {
1437                         NO_CHILD_THREAD;
1438                         request->child_state = REQUEST_DONE;
1439                         return;
1440                 }
1441 #endif
1442
1443 #ifdef WITH_COA
1444                 /*
1445                  *      If we've originated this CoA request, it gets
1446                  *      cleaned up now.
1447                  */
1448                 if (request->proxy &&
1449                     ((request->proxy->code == PW_CODE_COA_REQUEST) ||
1450                      (request->proxy->code == PW_CODE_DISCONNECT_REQUEST)) &&
1451                     (request->packet->code != request->proxy->code)) {
1452                         NO_CHILD_THREAD;
1453                         request->child_state = REQUEST_DONE;
1454                         return;
1455                 }
1456 #endif
1457
1458                 /*
1459                  *      No cleanup, mark the request as done.
1460                  *
1461                  *      Otherwise, mark it as "please do cleanup delay".
1462                  */
1463                 if (request->root->cleanup_delay == 0) {
1464                         NO_CHILD_THREAD;
1465                         request->child_state = REQUEST_DONE;
1466                 } else {
1467                         NO_CHILD_THREAD;
1468                         request->child_state = REQUEST_CLEANUP_DELAY;
1469                 }
1470         } else {
1471                 /*
1472                  *      Encode and sign it here, so that the master
1473                  *      thread can just send the encoded data, which
1474                  *      means it does less work.
1475                  */
1476                 RDEBUG2("Delaying response for %d.%06d seconds",
1477                         (int) request->response_delay.tv_sec, (int) request->response_delay.tv_usec);
1478                 request->listener->encode(request->listener, request);
1479                 request->component = "<core>";
1480                 request->module = "<delay>";
1481                 NO_CHILD_THREAD;
1482                 request->child_state = REQUEST_RESPONSE_DELAY;
1483         }
1484 }
1485
1486 STATE_MACHINE_DECL(request_running)
1487 {
1488         VERIFY_REQUEST(request);
1489
1490         TRACE_STATE_MACHINE;
1491
1492         switch (action) {
1493         case FR_ACTION_TIMER:
1494                 request_process_timer(request);
1495                 break;
1496
1497         case FR_ACTION_DUP:
1498                 request_common(request, action);
1499                 return;
1500
1501         case FR_ACTION_RUN:
1502                 if (!request_pre_handler(request, action)) {
1503 #ifdef DEBUG_STATE_MACHINE
1504                         if (debug_flag) printf("(%u) ********\tSTATE %s failed in pre-handler C-%s -> C-%s\t********\n",
1505                                                request->number, __FUNCTION__,
1506                                                child_state_names[request->child_state],
1507                                                child_state_names[REQUEST_DONE]);
1508 #endif
1509
1510                         NO_CHILD_THREAD;
1511                         request->child_state = REQUEST_DONE;
1512                         break;
1513                 }
1514
1515                 rad_assert(request->handle != NULL);
1516                 request->handle(request);
1517
1518 #ifdef WITH_PROXY
1519                 /*
1520                  *      We may need to send a proxied request.
1521                  */
1522                 if ((action == FR_ACTION_RUN) &&
1523                     request_will_proxy(request)) {
1524 #ifdef DEBUG_STATE_MACHINE
1525                         if (debug_flag) printf("(%u) ********\tWill Proxy\t********\n", request->number);
1526 #endif
1527                         /*
1528                          *      If this fails, it
1529                          *      takes care of setting
1530                          *      up the post proxy fail
1531                          *      handler.
1532                          */
1533                         if (request_proxy(request, 0) < 0) goto req_finished;
1534                 } else
1535 #endif
1536                 {
1537 #ifdef DEBUG_STATE_MACHINE
1538                         if (debug_flag) printf("(%u) ********\tFinished\t********\n", request->number);
1539 #endif
1540
1541 #ifdef WITH_COA
1542                         /*
1543                          *      Maybe originate a CoA request.
1544                          */
1545                         if ((action == FR_ACTION_RUN) && request->coa) {
1546                                 request_coa_originate(request);
1547                         }
1548 #endif
1549
1550 #ifdef WITH_PROXY
1551                 req_finished:
1552 #endif
1553                         request_finish(request, action);
1554                 }
1555                 break;
1556
1557         default:
1558                 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
1559                 break;
1560         }
1561 }
1562
1563 int request_receive(TALLOC_CTX *ctx, rad_listen_t *listener, RADIUS_PACKET *packet,
1564                     RADCLIENT *client, RAD_REQUEST_FUNP fun)
1565 {
1566         uint32_t count;
1567         RADIUS_PACKET **packet_p;
1568         REQUEST *request = NULL;
1569         struct timeval now;
1570         listen_socket_t *sock = NULL;
1571
1572         VERIFY_PACKET(packet);
1573
1574         /*
1575          *      Set the last packet received.
1576          */
1577         gettimeofday(&now, NULL);
1578
1579         packet->timestamp = now;
1580
1581 #ifdef WITH_ACCOUNTING
1582         if (listener->type != RAD_LISTEN_DETAIL)
1583 #endif
1584         {
1585                 sock = listener->data;
1586                 sock->last_packet = now.tv_sec;
1587         }
1588
1589         /*
1590          *      Skip everything if required.
1591          */
1592         if (listener->nodup) goto skip_dup;
1593
1594         packet_p = rbtree_finddata(pl, &packet);
1595         if (packet_p) {
1596                 rad_child_state_t child_state;
1597
1598                 request = fr_packet2myptr(REQUEST, packet, packet_p);
1599                 rad_assert(request->in_request_hash);
1600                 child_state = request->child_state;
1601
1602                 /*
1603                  *      Same src/dst ip/port, length, and
1604                  *      authentication vector: must be a duplicate.
1605                  */
1606                 if ((request->packet->data_len == packet->data_len) &&
1607                     (memcmp(request->packet->vector, packet->vector,
1608                             sizeof(packet->vector)) == 0)) {
1609
1610 #ifdef WITH_STATS
1611                         switch (packet->code) {
1612                         case PW_CODE_ACCESS_REQUEST:
1613                                 FR_STATS_INC(auth, total_dup_requests);
1614                                 break;
1615
1616 #ifdef WITH_ACCOUNTING
1617                         case PW_CODE_ACCOUNTING_REQUEST:
1618                                 FR_STATS_INC(acct, total_dup_requests);
1619                                 break;
1620 #endif
1621 #ifdef WITH_COA
1622                         case PW_CODE_COA_REQUEST:
1623                                 FR_STATS_INC(coa, total_dup_requests);
1624                                 break;
1625
1626                         case PW_CODE_DISCONNECT_REQUEST:
1627                                 FR_STATS_INC(dsc, total_dup_requests);
1628                                 break;
1629 #endif
1630
1631                         default:
1632                                 break;
1633                         }
1634 #endif  /* WITH_STATS */
1635
1636                         /*
1637                          *      Tell the state machine that there's a
1638                          *      duplicate request.
1639                          */
1640                         request->process(request, FR_ACTION_DUP);
1641                         return 0; /* duplicate of live request */
1642                 }
1643
1644                 /*
1645                  *      Mark the request as done ASAP, and before we
1646                  *      log anything.  The child may stop processing
1647                  *      the request just as we're logging the
1648                  *      complaint.
1649                  */
1650                 request_done(request, FR_ACTION_DONE);
1651                 request = NULL;
1652
1653                 /*
1654                  *      It's a new request, not a duplicate.  If the
1655                  *      old one is done, then we can clean it up.
1656                  */
1657                 if (child_state <= REQUEST_RUNNING) {
1658                         /*
1659                          *      The request is still QUEUED or RUNNING.  That's a problem.
1660                          */
1661                         ERROR("Received conflicting packet from "
1662                               "client %s port %d - ID: %u due to "
1663                               "unfinished request.  Giving up on old request.",
1664                               client->shortname,
1665                               packet->src_port, packet->id);
1666                 }
1667
1668                 /*
1669                  *      Mark the old request as done.  If there's no
1670                  *      child, the request will be cleaned up
1671                  *      immediately.  If there is a child, we'll set a
1672                  *      timer to go clean up the request.
1673                  */
1674         } /* else the new packet is unique */
1675
1676         /*
1677          *      Quench maximum number of outstanding requests.
1678          */
1679         if (main_config.max_requests &&
1680             ((count = rbtree_num_elements(pl)) > main_config.max_requests)) {
1681                 RATE_LIMIT(ERROR("Dropping request (%d is too many): from client %s port %d - ID: %d", count,
1682                                  client->shortname,
1683                                  packet->src_port, packet->id);
1684                            WARN("Please check the configuration file.\n"
1685                                 "\tThe value for 'max_requests' is probably set too low.\n"));
1686
1687                 exec_trigger(NULL, NULL, "server.max_requests", true);
1688                 return 0;
1689         }
1690
1691 skip_dup:
1692         /*
1693          *      Rate-limit the incoming packets
1694          */
1695         if (sock && sock->max_rate) {
1696                 uint32_t pps;
1697
1698                 pps = rad_pps(&sock->rate_pps_old, &sock->rate_pps_now, &sock->rate_time, &now);
1699                 if (pps > sock->max_rate) {
1700                         DEBUG("Dropping request due to rate limiting");
1701                         return 0;
1702                 }
1703                 sock->rate_pps_now++;
1704         }
1705
1706         /*
1707          *      Allocate a pool for the request.
1708          */
1709         if (!ctx) {
1710                 ctx = talloc_pool(NULL, main_config.talloc_pool_size);
1711                 if (!ctx) return 0;
1712
1713                 /*
1714                  *      The packet is still allocated from a different
1715                  *      context, but oh well.
1716                  */
1717                 (void) talloc_steal(ctx, packet);
1718         }
1719
1720         request = request_setup(ctx, listener, packet, client, fun);
1721         if (!request) {
1722                 talloc_free(ctx);
1723                 return 1;
1724         }
1725
1726         /*
1727          *      Mark it as a "real" request with a context.
1728          */
1729         request->options |= RAD_REQUEST_OPTION_CTX;
1730
1731         /*
1732          *      Remember the request in the list.
1733          */
1734         if (!listener->nodup) {
1735                 if (!rbtree_insert(pl, &request->packet)) {
1736                         RERROR("Failed to insert request in the list of live requests: discarding it");
1737                         request_done(request, FR_ACTION_DONE);
1738                         return 1;
1739                 }
1740
1741                 request->in_request_hash = true;
1742         }
1743
1744         /*
1745          *      Process it.  Send a response, and free it.
1746          */
1747         if (listener->synchronous) {
1748 #ifdef WITH_DETAIL
1749                 rad_assert(listener->type != RAD_LISTEN_DETAIL);
1750 #endif
1751
1752                 request->listener->decode(request->listener, request);
1753                 request->username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1754                 request->password = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
1755
1756                 fun(request);
1757
1758                 if (request->reply->code != 0) {
1759                         request->listener->send(request->listener, request);
1760                 } else {
1761                         RDEBUG("Not sending reply");
1762                 }
1763
1764                 /*
1765                  *      Don't do delayed reject.  Oh well.
1766                  */
1767                 request_free(request);
1768                 return 1;
1769         }
1770
1771         /*
1772          *      Otherwise, insert it into the state machine.
1773          *      The child threads will take care of processing it.
1774          */
1775         request_queue_or_run(request, request_running);
1776
1777         return 1;
1778 }
1779
1780
1781 static REQUEST *request_setup(TALLOC_CTX *ctx, rad_listen_t *listener, RADIUS_PACKET *packet,
1782                               RADCLIENT *client, RAD_REQUEST_FUNP fun)
1783 {
1784         REQUEST *request;
1785
1786         /*
1787          *      Create and initialize the new request.
1788          */
1789         request = request_alloc(ctx);
1790         if (!request) {
1791                 ERROR("No memory");
1792                 return NULL;
1793         }
1794         request->reply = rad_alloc(request, false);
1795         if (!request->reply) {
1796                 ERROR("No memory");
1797                 talloc_free(request);
1798                 return NULL;
1799         }
1800
1801         request->listener = listener;
1802         request->client = client;
1803         request->packet = talloc_steal(request, packet);
1804         request->number = request_num_counter++;
1805         request->priority = listener->type;
1806         request->master_state = REQUEST_ACTIVE;
1807 #ifdef DEBUG_STATE_MACHINE
1808         if (debug_flag) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n",
1809                                request->number, __FUNCTION__,
1810                                child_state_names[request->child_state],
1811                                child_state_names[REQUEST_RUNNING]);
1812 #endif
1813 #ifdef HAVE_PTHREAD_H
1814         request->child_pid = NO_SUCH_CHILD_PID;
1815 #endif
1816         request->child_state = REQUEST_RUNNING;
1817         request->handle = fun;
1818         NO_CHILD_THREAD;
1819
1820 #ifdef WITH_STATS
1821         request->listener->stats.last_packet = request->packet->timestamp.tv_sec;
1822         if (packet->code == PW_CODE_ACCESS_REQUEST) {
1823                 request->client->auth.last_packet = request->packet->timestamp.tv_sec;
1824                 radius_auth_stats.last_packet = request->packet->timestamp.tv_sec;
1825 #ifdef WITH_ACCOUNTING
1826         } else if (packet->code == PW_CODE_ACCOUNTING_REQUEST) {
1827                 request->client->acct.last_packet = request->packet->timestamp.tv_sec;
1828                 radius_acct_stats.last_packet = request->packet->timestamp.tv_sec;
1829 #endif
1830         }
1831 #endif  /* WITH_STATS */
1832
1833         /*
1834          *      Status-Server packets go to the head of the queue.
1835          */
1836         if (request->packet->code == PW_CODE_STATUS_SERVER) request->priority = 0;
1837
1838         /*
1839          *      Set virtual server identity
1840          */
1841         if (client->server) {
1842                 request->server = client->server;
1843         } else if (listener->server) {
1844                 request->server = listener->server;
1845         } else {
1846                 request->server = NULL;
1847         }
1848
1849         request->root = &main_config;
1850 #ifdef WITH_TCP
1851         request->listener->count++;
1852 #endif
1853
1854         /*
1855          *      The request passes many of our sanity checks.
1856          *      From here on in, if anything goes wrong, we
1857          *      send a reject message, instead of dropping the
1858          *      packet.
1859          */
1860
1861         /*
1862          *      Build the reply template from the request.
1863          */
1864
1865         request->reply->sockfd = request->packet->sockfd;
1866         request->reply->dst_ipaddr = request->packet->src_ipaddr;
1867         request->reply->src_ipaddr = request->packet->dst_ipaddr;
1868         request->reply->dst_port = request->packet->src_port;
1869         request->reply->src_port = request->packet->dst_port;
1870         request->reply->id = request->packet->id;
1871         request->reply->code = 0; /* UNKNOWN code */
1872         memcpy(request->reply->vector, request->packet->vector,
1873                sizeof(request->reply->vector));
1874         request->reply->vps = NULL;
1875         request->reply->data = NULL;
1876         request->reply->data_len = 0;
1877
1878         return request;
1879 }
1880
1881 #ifdef WITH_TCP
1882 /***********************************************************************
1883  *
1884  *      TCP Handlers.
1885  *
1886  ***********************************************************************/
1887
1888 /*
1889  *      Timer function for all TCP sockets.
1890  */
1891 static void tcp_socket_timer(void *ctx)
1892 {
1893         rad_listen_t *listener = talloc_get_type_abort(ctx, rad_listen_t);
1894         listen_socket_t *sock = listener->data;
1895         struct timeval end, now;
1896         char buffer[256];
1897         fr_socket_limit_t *limit;
1898
1899         ASSERT_MASTER;
1900
1901         fr_event_now(el, &now);
1902
1903         if (listener->status != RAD_LISTEN_STATUS_KNOWN) return;
1904
1905         switch (listener->type) {
1906 #ifdef WITH_PROXY
1907         case RAD_LISTEN_PROXY:
1908                 limit = &sock->home->limit;
1909                 break;
1910 #endif
1911
1912         case RAD_LISTEN_AUTH:
1913 #ifdef WITH_ACCOUNTING
1914         case RAD_LISTEN_ACCT:
1915 #endif
1916                 limit = &sock->limit;
1917                 break;
1918
1919         default:
1920                 return;
1921         }
1922
1923         /*
1924          *      If we enforce a lifetime, do it now.
1925          */
1926         if (limit->lifetime > 0) {
1927                 end.tv_sec = sock->opened + limit->lifetime;
1928                 end.tv_usec = 0;
1929
1930                 if (timercmp(&end, &now, <=)) {
1931                         listener->print(listener, buffer, sizeof(buffer));
1932                         DEBUG("Reached maximum lifetime on socket %s", buffer);
1933
1934                 do_close:
1935
1936                         listener->status = RAD_LISTEN_STATUS_EOL;
1937                         event_new_fd(listener);
1938                         return;
1939                 }
1940         } else {
1941                 end = now;
1942                 end.tv_sec += 3600;
1943         }
1944
1945         /*
1946          *      Enforce an idle timeout.
1947          */
1948         if (limit->idle_timeout > 0) {
1949                 struct timeval idle;
1950
1951                 rad_assert(sock->last_packet != 0);
1952                 idle.tv_sec = sock->last_packet + limit->idle_timeout;
1953                 idle.tv_usec = 0;
1954
1955                 if (timercmp(&idle, &now, <=)) {
1956                         listener->print(listener, buffer, sizeof(buffer));
1957                         DEBUG("Reached idle timeout on socket %s", buffer);
1958                         goto do_close;
1959                 }
1960
1961                 /*
1962                  *      Enforce the minimum of idle timeout or lifetime.
1963                  */
1964                 if (timercmp(&idle, &end, <)) {
1965                         end = idle;
1966                 }
1967         }
1968
1969         /*
1970          *      Wake up at t + 0.5s.  The code above checks if the timers
1971          *      are <= t.  This addition gives us a bit of leeway.
1972          */
1973         end.tv_usec = USEC / 2;
1974
1975         ASSERT_MASTER;
1976         if (!fr_event_insert(el, tcp_socket_timer, listener, &end, &sock->ev)) {
1977                 rad_panic("Failed to insert event");
1978         }
1979 }
1980
1981
1982 #ifdef WITH_PROXY
1983 /*
1984  *      Add +/- 2s of jitter, as suggested in RFC 3539
1985  *      and in RFC 5080.
1986  */
1987 static void add_jitter(struct timeval *when)
1988 {
1989         uint32_t jitter;
1990
1991         when->tv_sec -= 2;
1992
1993         jitter = fr_rand();
1994         jitter ^= (jitter >> 10);
1995         jitter &= ((1 << 22) - 1); /* 22 bits of 1 */
1996
1997         /*
1998          *      Add in ~ (4 * USEC) of jitter.
1999          */
2000         tv_add(when, jitter);
2001 }
2002
2003 /*
2004  *      Called by socket_del to remove requests with this socket
2005  */
2006 static int eol_proxy_listener(void *ctx, void *data)
2007 {
2008         rad_listen_t *this = talloc_get_type_abort(ctx, rad_listen_t);
2009         RADIUS_PACKET **proxy_p = data;
2010         REQUEST *request;
2011
2012         request = fr_packet2myptr(REQUEST, proxy, proxy_p);
2013         if (request->proxy_listener != this) return 0;
2014
2015         /*
2016          *      The normal "remove_from_proxy_hash" tries to grab the
2017          *      proxy mutex.  We already have it held, so grabbing it
2018          *      again will cause a deadlock.  Instead, call the "no
2019          *      lock" version of the function.
2020          */
2021         rad_assert(request->in_proxy_hash == true);
2022         remove_from_proxy_hash_nl(request, false);
2023
2024         /*
2025          *      Don't mark it as DONE.  The client can retransmit, and
2026          *      the packet SHOULD be re-proxied somewhere else.
2027          *
2028          *      Return "2" means that the rbtree code will remove it
2029          *      from the tree, and we don't need to do it ourselves.
2030          */
2031         return 2;
2032 }
2033 #endif  /* WITH_PROXY */
2034
2035 static int eol_listener(void *ctx, void *data)
2036 {
2037         rad_listen_t *this = talloc_get_type_abort(ctx, rad_listen_t);
2038         RADIUS_PACKET **packet_p = data;
2039         REQUEST *request;
2040
2041         request = fr_packet2myptr(REQUEST, packet, packet_p);
2042         if (request->listener != this) return 0;
2043
2044         request->master_state = REQUEST_STOP_PROCESSING;
2045
2046         return 0;
2047 }
2048 #endif  /* WITH_TCP */
2049
2050 #ifdef WITH_PROXY
2051 /***********************************************************************
2052  *
2053  *      Proxy handlers for the state machine.
2054  *
2055  ***********************************************************************/
2056
2057 /*
2058  *      Called with the proxy mutex held
2059  */
2060 static void remove_from_proxy_hash_nl(REQUEST *request, bool yank)
2061 {
2062         VERIFY_REQUEST(request);
2063
2064         if (!request->in_proxy_hash) return;
2065
2066         fr_packet_list_id_free(proxy_list, request->proxy, yank);
2067         request->in_proxy_hash = false;
2068
2069         /*
2070          *      On the FIRST reply, decrement the count of outstanding
2071          *      requests.  Note that this is NOT the count of sent
2072          *      packets, but whether or not the home server has
2073          *      responded at all.
2074          */
2075         if (request->home_server &&
2076             request->home_server->currently_outstanding) {
2077                 request->home_server->currently_outstanding--;
2078
2079                 /*
2080                  *      If we're NOT sending it packets, AND it's been
2081                  *      a while since we got a response, then we don't
2082                  *      know if it's alive or dead.
2083                  */
2084                 if ((request->home_server->currently_outstanding == 0) &&
2085                     (request->home_server->state == HOME_STATE_ALIVE)) {
2086                         struct timeval when, now;
2087
2088                         when.tv_sec = request->home_server->last_packet_recv ;
2089                         when.tv_usec = 0;
2090
2091                         timeradd(&when, request_response_window(request), &when);
2092                         gettimeofday(&now, NULL);
2093
2094                         /*
2095                          *      last_packet + response_window
2096                          *
2097                          *      We *administratively* mark the home
2098                          *      server as "unknown" state, because we
2099                          *      haven't seen a packet for a while.
2100                          */
2101                         if (timercmp(&now, &when, >)) {
2102                                 request->home_server->state = HOME_STATE_UNKNOWN;
2103                                 request->home_server->last_packet_sent = 0;
2104                                 request->home_server->last_packet_recv = 0;
2105                         }
2106                 }
2107         }
2108
2109 #ifdef WITH_TCP
2110         rad_assert(request->proxy_listener != NULL);
2111         request->proxy_listener->count--;
2112 #endif
2113         request->proxy_listener = NULL;
2114
2115         /*
2116          *      Got from YES in hash, to NO, not in hash while we hold
2117          *      the mutex.  This guarantees that when another thread
2118          *      grabs the mutex, the "not in hash" flag is correct.
2119          */
2120         RDEBUG3("proxy: request is no longer in proxy hash");
2121 }
2122
2123 static void remove_from_proxy_hash(REQUEST *request)
2124 {
2125         VERIFY_REQUEST(request);
2126
2127         /*
2128          *      Check this without grabbing the mutex because it's a
2129          *      lot faster that way.
2130          */
2131         if (!request->in_proxy_hash) return;
2132
2133         /*
2134          *      The "not in hash" flag is definitive.  However, if the
2135          *      flag says that it IS in the hash, there might still be
2136          *      a race condition where it isn't.
2137          */
2138         PTHREAD_MUTEX_LOCK(&proxy_mutex);
2139
2140         if (!request->in_proxy_hash) {
2141                 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2142                 return;
2143         }
2144
2145         remove_from_proxy_hash_nl(request, true);
2146
2147         PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2148 }
2149
2150 static int insert_into_proxy_hash(REQUEST *request)
2151 {
2152         char buf[128];
2153         int tries;
2154         bool success = false;
2155         void *proxy_listener;
2156
2157         VERIFY_REQUEST(request);
2158
2159         rad_assert(request->proxy != NULL);
2160         rad_assert(request->home_server != NULL);
2161         rad_assert(proxy_list != NULL);
2162
2163
2164         PTHREAD_MUTEX_LOCK(&proxy_mutex);
2165         proxy_listener = NULL;
2166         request->num_proxied_requests = 1;
2167         request->num_proxied_responses = 0;
2168
2169         for (tries = 0; tries < 2; tries++) {
2170                 rad_listen_t *this;
2171                 listen_socket_t *sock;
2172
2173                 RDEBUG3("proxy: Trying to allocate ID (%d/2)", tries);
2174                 success = fr_packet_list_id_alloc(proxy_list,
2175                                                 request->home_server->proto,
2176                                                 &request->proxy, &proxy_listener);
2177                 if (success) break;
2178
2179                 if (tries > 0) continue; /* try opening new socket only once */
2180
2181 #ifdef HAVE_PTHREAD_H
2182                 if (proxy_no_new_sockets) break;
2183 #endif
2184
2185                 RDEBUG3("proxy: Trying to open a new listener to the home server");
2186                 this = proxy_new_listener(proxy_ctx, request->home_server, 0);
2187                 if (!this) {
2188                         PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2189                         goto fail;
2190                 }
2191
2192                 request->proxy->src_port = 0; /* Use any new socket */
2193                 proxy_listener = this;
2194
2195                 sock = this->data;
2196                 if (!fr_packet_list_socket_add(proxy_list, this->fd,
2197                                                sock->proto,
2198                                                &sock->other_ipaddr, sock->other_port,
2199                                                this)) {
2200
2201 #ifdef HAVE_PTHREAD_H
2202                         proxy_no_new_sockets = true;
2203 #endif
2204                         PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2205
2206                         /*
2207                          *      This is bad.  However, the
2208                          *      packet list now supports 256
2209                          *      open sockets, which should
2210                          *      minimize this problem.
2211                          */
2212                         ERROR("Failed adding proxy socket: %s",
2213                               fr_strerror());
2214                         goto fail;
2215                 }
2216
2217                 /*
2218                  *      Add it to the event loop.  Ensure that we have
2219                  *      only one mutex locked at a time.
2220                  */
2221                 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2222                 radius_update_listener(this);
2223                 PTHREAD_MUTEX_LOCK(&proxy_mutex);
2224         }
2225
2226         if (!proxy_listener || !success) {
2227                 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2228                 REDEBUG2("proxy: Failed allocating Id for proxied request");
2229         fail:
2230                 request->proxy_listener = NULL;
2231                 request->in_proxy_hash = false;
2232                 return 0;
2233         }
2234
2235         rad_assert(request->proxy->id >= 0);
2236
2237         request->proxy_listener = proxy_listener;
2238         request->in_proxy_hash = true;
2239         RDEBUG3("proxy: request is now in proxy hash");
2240
2241         /*
2242          *      Keep track of maximum outstanding requests to a
2243          *      particular home server.  'max_outstanding' is
2244          *      enforced in home_server_ldb(), in realms.c.
2245          */
2246         request->home_server->currently_outstanding++;
2247
2248 #ifdef WITH_TCP
2249         request->proxy_listener->count++;
2250 #endif
2251
2252         PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2253
2254         RDEBUG3("proxy: allocating destination %s port %d - Id %d",
2255                inet_ntop(request->proxy->dst_ipaddr.af,
2256                          &request->proxy->dst_ipaddr.ipaddr, buf, sizeof(buf)),
2257                request->proxy->dst_port,
2258                request->proxy->id);
2259
2260         return 1;
2261 }
2262
2263 static int process_proxy_reply(REQUEST *request, RADIUS_PACKET *reply)
2264 {
2265         int rcode;
2266         int post_proxy_type = 0;
2267         VALUE_PAIR *vp;
2268
2269         VERIFY_REQUEST(request);
2270
2271         /*
2272          *      There may be a proxy reply, but it may be too late.
2273          */
2274         if (!request->home_server->server && !request->proxy_listener) return 0;
2275
2276         /*
2277          *      Delete any reply we had accumulated until now.
2278          */
2279         RDEBUG2("Clearing existing &reply: attributes");
2280         pairfree(&request->reply->vps);
2281
2282         /*
2283          *      Run the packet through the post-proxy stage,
2284          *      BEFORE playing games with the attributes.
2285          */
2286         vp = pairfind(request->config_items, PW_POST_PROXY_TYPE, 0, TAG_ANY);
2287         if (vp) {
2288                 post_proxy_type = vp->vp_integer;
2289         /*
2290          *      If we have a proxy_reply, and it was a reject, or a NAK
2291          *      setup Post-Proxy <type>.
2292          *
2293          *      If the <type> doesn't have a section, then the Post-Proxy
2294          *      section is ignored.
2295          */
2296         } else if (reply) {
2297                 DICT_VALUE *dval = NULL;
2298
2299                 switch (reply->code) {
2300                 case PW_CODE_ACCESS_REJECT:
2301                         dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Reject");
2302                         if (dval) post_proxy_type = dval->value;
2303                         break;
2304
2305                 case PW_CODE_DISCONNECT_NAK:
2306                         dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, fr_packet_codes[reply->code]);
2307                         if (dval) post_proxy_type = dval->value;
2308                         break;
2309
2310                 case PW_CODE_COA_NAK:
2311                         dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, fr_packet_codes[reply->code]);
2312                         if (dval) post_proxy_type = dval->value;
2313                         break;
2314
2315                 default:
2316                         break;
2317                 }
2318
2319                 /*
2320                  *      Create config:Post-Proxy-Type
2321                  */
2322                 if (dval) {
2323                         vp = radius_paircreate(request, &request->config_items, PW_POST_PROXY_TYPE, 0);
2324                         vp->vp_integer = dval->value;
2325                 }
2326         }
2327
2328         if (post_proxy_type > 0) RDEBUG2("Found Post-Proxy-Type %s",
2329                                          dict_valnamebyattr(PW_POST_PROXY_TYPE, 0, post_proxy_type));
2330
2331         if (reply) {
2332                 VERIFY_PACKET(reply);
2333
2334                 /*
2335                  *      Decode the packet if required.
2336                  */
2337                 if (request->proxy_listener) {
2338                         rcode = request->proxy_listener->decode(request->proxy_listener, request);
2339                         debug_packet(request, reply, true);
2340
2341                         /*
2342                          *      Pro-actively remove it from the proxy hash.
2343                          *      This is later than in 2.1.x, but it means that
2344                          *      the replies are authenticated before being
2345                          *      removed from the hash.
2346                          */
2347                         if ((rcode == 0) &&
2348                             (request->num_proxied_requests <= request->num_proxied_responses)) {
2349                                 remove_from_proxy_hash(request);
2350                         }
2351                 } else {
2352                         rad_assert(!request->in_proxy_hash);
2353                 }
2354         } else if (request->in_proxy_hash) {
2355                 remove_from_proxy_hash(request);
2356         }
2357
2358         if (request->home_pool && request->home_pool->virtual_server) {
2359                 char const *old_server = request->server;
2360
2361                 request->server = request->home_pool->virtual_server;
2362                 RDEBUG2("server %s {", request->server);
2363                 RINDENT();
2364                 rcode = process_post_proxy(post_proxy_type, request);
2365                 REXDENT();
2366                 RDEBUG2("}");
2367                 request->server = old_server;
2368         } else {
2369                 rcode = process_post_proxy(post_proxy_type, request);
2370         }
2371
2372 #ifdef WITH_COA
2373         if (request->packet->code == request->proxy->code)
2374           /*
2375            *    Don't run the next bit if we originated a CoA
2376            *    packet, after receiving an Access-Request or
2377            *    Accounting-Request.
2378            */
2379 #endif
2380
2381         /*
2382          *      There may NOT be a proxy reply, as we may be
2383          *      running Post-Proxy-Type = Fail.
2384          */
2385         if (reply) {
2386                 pairadd(&request->reply->vps, paircopy(request->reply, reply->vps));
2387
2388                 /*
2389                  *      Delete the Proxy-State Attributes from
2390                  *      the reply.  These include Proxy-State
2391                  *      attributes from us and remote server.
2392                  */
2393                 pairdelete(&request->reply->vps, PW_PROXY_STATE, 0, TAG_ANY);
2394         }
2395
2396         switch (rcode) {
2397         default:  /* Don't do anything */
2398                 break;
2399         case RLM_MODULE_FAIL:
2400                 return 0;
2401
2402         case RLM_MODULE_HANDLED:
2403                 return 0;
2404         }
2405
2406         return 1;
2407 }
2408
2409 int request_proxy_reply(RADIUS_PACKET *packet)
2410 {
2411         RADIUS_PACKET **proxy_p;
2412         REQUEST *request;
2413         struct timeval now;
2414         char buffer[128];
2415
2416         VERIFY_PACKET(packet);
2417
2418         PTHREAD_MUTEX_LOCK(&proxy_mutex);
2419         proxy_p = fr_packet_list_find_byreply(proxy_list, packet);
2420
2421         if (!proxy_p) {
2422                 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2423                 PROXY("No outstanding request was found for reply from host %s port %d - ID %u",
2424                        inet_ntop(packet->src_ipaddr.af,
2425                                  &packet->src_ipaddr.ipaddr,
2426                                  buffer, sizeof(buffer)),
2427                        packet->src_port, packet->id);
2428                 return 0;
2429         }
2430
2431         request = fr_packet2myptr(REQUEST, proxy, proxy_p);
2432         request->num_proxied_responses++; /* needs to be protected by lock */
2433
2434         PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
2435
2436         /*
2437          *      No reply, BUT the current packet fails verification:
2438          *      ignore it.  This does the MD5 calculations in the
2439          *      server core, but I guess we can fix that later.
2440          */
2441         if (!request->proxy_reply &&
2442             (rad_verify(packet, request->proxy,
2443                         request->home_server->secret) != 0)) {
2444                 DEBUG("Ignoring spoofed proxy reply.  Signature is invalid");
2445                 return 0;
2446         }
2447
2448         /*
2449          *      The home server sent us a packet which doesn't match
2450          *      something we have: ignore it.  This is done only to
2451          *      catch the case of broken systems.
2452          */
2453         if (request->proxy_reply &&
2454             (memcmp(request->proxy_reply->vector,
2455                     packet->vector,
2456                     sizeof(request->proxy_reply->vector)) != 0)) {
2457                 RDEBUG2("Ignoring conflicting proxy reply");
2458                 return 0;
2459         }
2460
2461         gettimeofday(&now, NULL);
2462
2463         /*
2464          *      Status-Server packets don't count as real packets.
2465          */
2466         if (request->proxy->code != PW_CODE_STATUS_SERVER) {
2467                 listen_socket_t *sock = request->proxy_listener->data;
2468
2469                 request->home_server->last_packet_recv = now.tv_sec;
2470                 sock->last_packet = now.tv_sec;
2471         }
2472
2473         /*
2474          *      If we have previously seen a reply, ignore the
2475          *      duplicate.
2476          */
2477         if (request->proxy_reply) {
2478                 RDEBUG2("Discarding duplicate reply from host %s port %d  - ID: %d",
2479                         inet_ntop(packet->src_ipaddr.af,
2480                                   &packet->src_ipaddr.ipaddr,
2481                                   buffer, sizeof(buffer)),
2482                         packet->src_port, packet->id);
2483                 return 0;
2484         }
2485
2486         /*
2487          *      Call the state machine to do something useful with the
2488          *      request.
2489          */
2490         request->proxy_reply = talloc_steal(request, packet);
2491         packet->timestamp = now;
2492         request->priority = RAD_LISTEN_PROXY;
2493
2494 #ifdef WITH_STATS
2495         /*
2496          *      Update the proxy listener stats here, because only one
2497          *      thread accesses that at a time.  The home_server and
2498          *      main proxy_*_stats structures are updated once the
2499          *      request is cleaned up.
2500          */
2501         request->proxy_listener->stats.total_responses++;
2502
2503         request->home_server->stats.last_packet = packet->timestamp.tv_sec;
2504         request->proxy_listener->stats.last_packet = packet->timestamp.tv_sec;
2505
2506         switch (request->proxy->code) {
2507         case PW_CODE_ACCESS_REQUEST:
2508                 proxy_auth_stats.last_packet = packet->timestamp.tv_sec;
2509
2510                 if (request->proxy_reply->code == PW_CODE_ACCESS_ACCEPT) {
2511                         request->proxy_listener->stats.total_access_accepts++;
2512
2513                 } else if (request->proxy_reply->code == PW_CODE_ACCESS_REJECT) {
2514                         request->proxy_listener->stats.total_access_rejects++;
2515
2516                 } else if (request->proxy_reply->code == PW_CODE_ACCESS_CHALLENGE) {
2517                         request->proxy_listener->stats.total_access_challenges++;
2518                 }
2519                 break;
2520
2521 #ifdef WITH_ACCOUNTING
2522         case PW_CODE_ACCOUNTING_REQUEST:
2523                 request->proxy_listener->stats.total_responses++;
2524                 proxy_acct_stats.last_packet = packet->timestamp.tv_sec;
2525                 break;
2526
2527 #endif
2528
2529 #ifdef WITH_COA
2530         case PW_CODE_COA_REQUEST:
2531                 request->proxy_listener->stats.total_responses++;
2532                 proxy_coa_stats.last_packet = packet->timestamp.tv_sec;
2533                 break;
2534
2535         case PW_CODE_DISCONNECT_REQUEST:
2536                 request->proxy_listener->stats.total_responses++;
2537                 proxy_dsc_stats.last_packet = packet->timestamp.tv_sec;
2538                 break;
2539
2540 #endif
2541         default:
2542                 break;
2543         }
2544 #endif
2545
2546         /*
2547          *      We've received a reply.  If we hadn't been sending it
2548          *      packets for a while, just mark it alive.
2549          */
2550         if (request->home_server->state == HOME_STATE_UNKNOWN) {
2551                 request->home_server->state = HOME_STATE_ALIVE;
2552                 request->home_server->response_timeouts = 0;
2553         }
2554
2555         /*
2556          *      Tell the request state machine that we have a proxy
2557          *      reply.  Depending on the function, this should either
2558          *      ignore it, or process it.
2559          */
2560         request->process(request, FR_ACTION_PROXY_REPLY);
2561
2562         return 1;
2563 }
2564
2565
2566 static int setup_post_proxy_fail(REQUEST *request)
2567 {
2568         DICT_VALUE const *dval = NULL;
2569         VALUE_PAIR *vp;
2570
2571         VERIFY_REQUEST(request);
2572
2573         if (request->proxy->code == PW_CODE_ACCESS_REQUEST) {
2574                 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0,
2575                                       "Fail-Authentication");
2576
2577         } else if (request->proxy->code == PW_CODE_ACCOUNTING_REQUEST) {
2578                 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0,
2579                                       "Fail-Accounting");
2580 #ifdef WITH_COA
2581         } else if (request->proxy->code == PW_CODE_COA_REQUEST) {
2582                 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-CoA");
2583
2584         } else if (request->proxy->code == PW_CODE_DISCONNECT_REQUEST) {
2585                 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-Disconnect");
2586 #endif
2587         } else {
2588                 WARN("Unknown packet type in Post-Proxy-Type Fail: ignoring");
2589                 return 0;
2590         }
2591
2592         if (!dval) dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail");
2593
2594         if (!dval) {
2595                 pairdelete(&request->config_items, PW_POST_PROXY_TYPE, 0, TAG_ANY);
2596                 return 0;
2597         }
2598
2599         vp = pairfind(request->config_items, PW_POST_PROXY_TYPE, 0, TAG_ANY);
2600         if (!vp) vp = radius_paircreate(request, &request->config_items,
2601                                         PW_POST_PROXY_TYPE, 0);
2602         vp->vp_integer = dval->value;
2603
2604         return 1;
2605 }
2606
2607 STATE_MACHINE_DECL(proxy_no_reply)
2608 {
2609         VERIFY_REQUEST(request);
2610
2611         TRACE_STATE_MACHINE;
2612
2613         switch (action) {
2614         case FR_ACTION_DUP:
2615         case FR_ACTION_TIMER:
2616         case FR_ACTION_PROXY_REPLY:
2617                 request_common(request, action);
2618                 break;
2619
2620         case FR_ACTION_RUN:
2621                 if (process_proxy_reply(request, NULL)) {
2622                         request_finish(request, action);
2623                 }
2624                 request_done(request, FR_ACTION_DONE);
2625                 break;
2626
2627         default:
2628                 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
2629                 break;
2630         }
2631 }
2632
2633 STATE_MACHINE_DECL(proxy_running)
2634 {
2635         VERIFY_REQUEST(request);
2636
2637         TRACE_STATE_MACHINE;
2638
2639         switch (action) {
2640                 /*
2641                  *      Silently ignore duplicate proxy replies.
2642                  */
2643         case FR_ACTION_PROXY_REPLY:
2644                 break;
2645
2646         case FR_ACTION_DUP:
2647         case FR_ACTION_TIMER:
2648                 request_common(request, action);
2649                 break;
2650
2651         case FR_ACTION_RUN:
2652                 if (process_proxy_reply(request, request->proxy_reply)) {
2653                         request->handle(request);
2654                         request_finish(request, action);
2655                 } else {
2656                         request_done(request, FR_ACTION_DONE);
2657                 }
2658                 break;
2659
2660         default:
2661                 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
2662                 break;
2663         }
2664 }
2665
2666 static int request_will_proxy(REQUEST *request)
2667 {
2668         int rcode, pre_proxy_type = 0;
2669         char const *realmname = NULL;
2670         VALUE_PAIR *vp, *strippedname;
2671         home_server_t *home;
2672         REALM *realm = NULL;
2673         home_pool_t *pool = NULL;
2674
2675         VERIFY_REQUEST(request);
2676
2677         if (!request->root->proxy_requests) return 0;
2678         if (request->packet->dst_port == 0) return 0;
2679         if (request->packet->code == PW_CODE_STATUS_SERVER) return 0;
2680         if (request->in_proxy_hash) return 0;
2681
2682         /*
2683          *      FIXME: for 3.0, allow this only for rejects?
2684          */
2685         if (request->reply->code != 0) return 0;
2686
2687         vp = pairfind(request->config_items, PW_PROXY_TO_REALM, 0, TAG_ANY);
2688         if (vp) {
2689                 realm = realm_find2(vp->vp_strvalue);
2690                 if (!realm) {
2691                         REDEBUG2("Cannot proxy to unknown realm %s",
2692                                 vp->vp_strvalue);
2693                         return 0;
2694                 }
2695
2696                 realmname = vp->vp_strvalue;
2697
2698                 /*
2699                  *      Figure out which pool to use.
2700                  */
2701                 if (request->packet->code == PW_CODE_ACCESS_REQUEST) {
2702                         pool = realm->auth_pool;
2703
2704 #ifdef WITH_ACCOUNTING
2705                 } else if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) {
2706                         pool = realm->acct_pool;
2707 #endif
2708
2709 #ifdef WITH_COA
2710                 } else if ((request->packet->code == PW_CODE_COA_REQUEST) ||
2711                            (request->packet->code == PW_CODE_DISCONNECT_REQUEST)) {
2712                         pool = realm->coa_pool;
2713 #endif
2714
2715                 } else {
2716                         return 0;
2717                 }
2718
2719         } else if ((vp = pairfind(request->config_items, PW_HOME_SERVER_POOL, 0, TAG_ANY)) != NULL) {
2720                 int pool_type;
2721
2722                 switch (request->packet->code) {
2723                 case PW_CODE_ACCESS_REQUEST:
2724                         pool_type = HOME_TYPE_AUTH;
2725                         break;
2726
2727 #ifdef WITH_ACCOUNTING
2728                 case PW_CODE_ACCOUNTING_REQUEST:
2729                         pool_type = HOME_TYPE_ACCT;
2730                         break;
2731 #endif
2732
2733 #ifdef WITH_COA
2734                 case PW_CODE_COA_REQUEST:
2735                 case PW_CODE_DISCONNECT_REQUEST:
2736                         pool_type = HOME_TYPE_COA;
2737                         break;
2738 #endif
2739
2740                 default:
2741                         return 0;
2742                 }
2743
2744                 pool = home_pool_byname(vp->vp_strvalue, pool_type);
2745
2746                 /*
2747                  *      Send it directly to a home server (i.e. NAS)
2748                  */
2749         } else if (((vp = pairfind(request->config_items, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY)) != NULL) ||
2750                    ((vp = pairfind(request->config_items, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY)) != NULL)) {
2751                 VALUE_PAIR *port;
2752                 uint16_t dst_port;
2753                 fr_ipaddr_t dst_ipaddr;
2754
2755                 memset(&dst_ipaddr, 0, sizeof(dst_ipaddr));
2756
2757                 if (vp->da->attr == PW_PACKET_DST_IP_ADDRESS) {
2758                         dst_ipaddr.af = AF_INET;
2759                         dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
2760                         dst_ipaddr.prefix = 32;
2761                 } else {
2762                         dst_ipaddr.af = AF_INET6;
2763                         memcpy(&dst_ipaddr.ipaddr.ip6addr, &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
2764                         dst_ipaddr.prefix = 128;
2765                 }
2766
2767                 port = pairfind(request->config_items, PW_PACKET_DST_PORT, 0, TAG_ANY);
2768                 if (!port) {
2769                 dst_port = PW_COA_UDP_PORT;
2770                 } else {
2771                         dst_port = vp->vp_integer;
2772                 }
2773
2774                 /*
2775                  *      Nothing does CoA over TCP.
2776                  */
2777                 home = home_server_find(&dst_ipaddr, dst_port, IPPROTO_UDP);
2778                 if (!home) {
2779                         char buffer[256];
2780
2781                         WARN("No such CoA home server %s port %u",
2782                              inet_ntop(dst_ipaddr.af, &dst_ipaddr.ipaddr, buffer, sizeof(buffer)),
2783                              (unsigned int) dst_port);
2784                         return 0;
2785                 }
2786
2787                 goto do_home;
2788
2789         } else {
2790                 return 0;
2791         }
2792
2793         if (!pool) {
2794                 RWDEBUG2("Cancelling proxy as no home pool exists");
2795                 return 0;
2796         }
2797
2798         if (request->listener->synchronous) {
2799                 WARN("Cannot proxy a request which is from a 'synchronous' socket");
2800                 return 0;
2801         }
2802
2803         request->home_pool = pool;
2804
2805         home = home_server_ldb(realmname, pool, request);
2806
2807         if (!home) {
2808                 REDEBUG2("Failed to find live home server: Cancelling proxy");
2809                 return 0;
2810         }
2811
2812 do_home:
2813         home_server_update_request(home, request);
2814
2815 #ifdef WITH_COA
2816         /*
2817          *      Once we've decided to proxy a request, we cannot send
2818          *      a CoA packet.  So we free up any CoA packet here.
2819          */
2820         if (request->coa) request_done(request->coa, FR_ACTION_DONE);
2821 #endif
2822
2823         /*
2824          *      Remember that we sent the request to a Realm.
2825          */
2826         if (realmname) pairmake_packet("Realm", realmname, T_OP_EQ);
2827
2828         /*
2829          *      Strip the name, if told to.
2830          *
2831          *      Doing it here catches the case of proxied tunneled
2832          *      requests.
2833          */
2834         if (realm && (realm->strip_realm == true) &&
2835            (strippedname = pairfind(request->proxy->vps, PW_STRIPPED_USER_NAME, 0, TAG_ANY)) != NULL) {
2836                 /*
2837                  *      If there's a Stripped-User-Name attribute in
2838                  *      the request, then use THAT as the User-Name
2839                  *      for the proxied request, instead of the
2840                  *      original name.
2841                  *
2842                  *      This is done by making a copy of the
2843                  *      Stripped-User-Name attribute, turning it into
2844                  *      a User-Name attribute, deleting the
2845                  *      Stripped-User-Name and User-Name attributes
2846                  *      from the vps list, and making the new
2847                  *      User-Name the head of the vps list.
2848                  */
2849                 vp = pairfind(request->proxy->vps, PW_USER_NAME, 0, TAG_ANY);
2850                 if (!vp) {
2851                         vp_cursor_t cursor;
2852                         vp = radius_paircreate(NULL, NULL,
2853                                                PW_USER_NAME, 0);
2854                         rad_assert(vp != NULL); /* handled by above function */
2855                         /* Insert at the START of the list */
2856                         /* FIXME: Can't make assumptions about ordering */
2857                         fr_cursor_init(&cursor, &vp);
2858                         fr_cursor_merge(&cursor, request->proxy->vps);
2859                         request->proxy->vps = vp;
2860                 }
2861                 pairstrcpy(vp, strippedname->vp_strvalue);
2862
2863                 /*
2864                  *      Do NOT delete Stripped-User-Name.
2865                  */
2866         }
2867
2868         /*
2869          *      If there is no PW_CHAP_CHALLENGE attribute but
2870          *      there is a PW_CHAP_PASSWORD we need to add it
2871          *      since we can't use the request authenticator
2872          *      anymore - we changed it.
2873          */
2874         if ((request->packet->code == PW_CODE_ACCESS_REQUEST) &&
2875             pairfind(request->proxy->vps, PW_CHAP_PASSWORD, 0, TAG_ANY) &&
2876             pairfind(request->proxy->vps, PW_CHAP_CHALLENGE, 0, TAG_ANY) == NULL) {
2877                 vp = radius_paircreate(request->proxy, &request->proxy->vps, PW_CHAP_CHALLENGE, 0);
2878                 pairmemcpy(vp, request->packet->vector, sizeof(request->packet->vector));
2879         }
2880
2881         /*
2882          *      The RFC's say we have to do this, but FreeRADIUS
2883          *      doesn't need it.
2884          */
2885         vp = radius_paircreate(request->proxy, &request->proxy->vps, PW_PROXY_STATE, 0);
2886         pairsprintf(vp, "%u", request->packet->id);
2887
2888         /*
2889          *      Should be done BEFORE inserting into proxy hash, as
2890          *      pre-proxy may use this information, or change it.
2891          */
2892         request->proxy->code = request->packet->code;
2893
2894         /*
2895          *      Call the pre-proxy routines.
2896          */
2897         vp = pairfind(request->config_items, PW_PRE_PROXY_TYPE, 0, TAG_ANY);
2898         if (vp) {
2899                 DICT_VALUE const *dval = dict_valbyattr(vp->da->attr, vp->da->vendor, vp->vp_integer);
2900                 /* Must be a validation issue */
2901                 rad_assert(dval);
2902                 RDEBUG2("Found Pre-Proxy-Type %s", dval->name);
2903                 pre_proxy_type = vp->vp_integer;
2904         }
2905
2906         /*
2907          *      home_pool may be NULL when originating CoA packets,
2908          *      because they go directly to an IP address.
2909          */
2910         if (request->home_pool && request->home_pool->virtual_server) {
2911                 char const *old_server = request->server;
2912
2913                 request->server = request->home_pool->virtual_server;
2914
2915                 RDEBUG2("server %s {", request->server);
2916                 RINDENT();
2917                 rcode = process_pre_proxy(pre_proxy_type, request);
2918                 REXDENT();
2919                 RDEBUG2("}");
2920
2921                 request->server = old_server;
2922         } else {
2923                 rcode = process_pre_proxy(pre_proxy_type, request);
2924         }
2925         switch (rcode) {
2926         case RLM_MODULE_FAIL:
2927         case RLM_MODULE_INVALID:
2928         case RLM_MODULE_NOTFOUND:
2929         case RLM_MODULE_USERLOCK:
2930         default:
2931                 /* FIXME: debug print failed stuff */
2932                 return -1;
2933
2934         case RLM_MODULE_REJECT:
2935         case RLM_MODULE_HANDLED:
2936                 return 0;
2937
2938         /*
2939          *      Only proxy the packet if the pre-proxy code succeeded.
2940          */
2941         case RLM_MODULE_NOOP:
2942         case RLM_MODULE_OK:
2943         case RLM_MODULE_UPDATED:
2944                 break;
2945         }
2946
2947         return 1;
2948 }
2949
2950 static int request_proxy(REQUEST *request, int retransmit)
2951 {
2952         char buffer[128];
2953
2954         VERIFY_REQUEST(request);
2955
2956         rad_assert(request->parent == NULL);
2957         rad_assert(request->home_server != NULL);
2958
2959         if (request->master_state == REQUEST_STOP_PROCESSING) return 0;
2960
2961 #ifdef WITH_COA
2962         if (request->coa) {
2963                 RWDEBUG("Cannot proxy and originate CoA packets at the same time.  Cancelling CoA request");
2964                 request_done(request->coa, FR_ACTION_DONE);
2965         }
2966 #endif
2967
2968         /*
2969          *      The request may need sending to a virtual server.
2970          *      This code is more than a little screwed up.  The rest
2971          *      of the state machine doesn't handle parent / child
2972          *      relationships well.  i.e. if the child request takes
2973          *      too long, the core will mark the *parent* as "stop
2974          *      processing".  And the child will continue without
2975          *      knowing anything...
2976          *
2977          *      So, we have some horrible hacks to get around that.
2978          */
2979         if (request->home_server->server) {
2980                 REQUEST *fake;
2981
2982                 if (request->packet->dst_port == 0) {
2983                         WARN("Cannot proxy an internal request");
2984                         return 0;
2985                 }
2986
2987                 DEBUG("Proxying to virtual server %s",
2988                       request->home_server->server);
2989
2990                 /*
2991                  *      Packets to virtual serrers don't get
2992                  *      retransmissions sent to them.  And the virtual
2993                  *      server is run ONLY if we have no child
2994                  *      threads, or we're running in a child thread.
2995                  */
2996                 rad_assert(retransmit == 0);
2997                 rad_assert(!spawn_flag || !we_are_master());
2998
2999                 fake = request_alloc_fake(request);
3000
3001                 fake->packet->vps = paircopy(fake->packet, request->packet->vps);
3002                 talloc_free(request->proxy);
3003
3004                 fake->server = request->home_server->server;
3005                 fake->handle = request->handle;
3006                 fake->process = NULL; /* should never be run for anything */
3007
3008                 /*
3009                  *      Run the virtual server.
3010                  */
3011                 request_running(fake, FR_ACTION_RUN);
3012
3013                 request->proxy = talloc_steal(request, fake->packet);
3014                 fake->packet = NULL;
3015                 request->proxy_reply = talloc_steal(request, fake->reply);
3016                 fake->reply = NULL;
3017
3018                 talloc_free(fake);
3019
3020                 /*
3021                  *      No reply code, toss the reply we have,
3022                  *      and do post-proxy-type Fail.
3023                  */
3024                 if (!request->proxy_reply->code) {
3025                         TALLOC_FREE(request->proxy_reply);
3026                         setup_post_proxy_fail(request);
3027                 }
3028
3029                 /*
3030                  *      Just do the work here, rather than trying to
3031                  *      run the "decode proxy reply" stuff...
3032                  */
3033                 process_proxy_reply(request, request->proxy_reply);
3034
3035                 /*
3036                  *      If we have a reply, run it through the handler.
3037                  */
3038                 if (request->proxy_reply) {
3039                         request->handle(request); /* to do more post-proxy stuff */
3040                 }
3041
3042                 return -1;      /* so we call request_finish */
3043         }
3044
3045         /*
3046          *      We're actually sending a proxied packet.  Do that now.
3047          */
3048         if (!request->in_proxy_hash && !insert_into_proxy_hash(request)) {
3049                 ERROR("Failed to insert request into the proxy list");
3050                 return -1;
3051         }
3052
3053         rad_assert(request->proxy->id >= 0);
3054
3055         if (debug_flag) {
3056                 struct timeval *response_window;
3057
3058                 response_window = request_response_window(request);
3059
3060 #ifdef WITH_TLS
3061                 if (request->home_server->tls) {
3062                         RDEBUG2("Proxying request to home server %s port %d (TLS) timeout %d.%06d",
3063                                 inet_ntop(request->proxy->dst_ipaddr.af,
3064                                           &request->proxy->dst_ipaddr.ipaddr,
3065                                           buffer, sizeof(buffer)),
3066                                 request->proxy->dst_port,
3067                                 (int) response_window->tv_sec, (int) response_window->tv_usec);
3068                 } else
3069 #endif
3070                         RDEBUG2("Proxying request to home server %s port %d timeout %d.%06d",
3071                                 inet_ntop(request->proxy->dst_ipaddr.af,
3072                                           &request->proxy->dst_ipaddr.ipaddr,
3073                                           buffer, sizeof(buffer)),
3074                                 request->proxy->dst_port,
3075                                 (int) response_window->tv_sec, (int) response_window->tv_usec);
3076
3077
3078         }
3079
3080         gettimeofday(&request->proxy_retransmit, NULL);
3081         if (!retransmit) {
3082                 request->proxy->timestamp = request->proxy_retransmit;
3083         }
3084         request->home_server->last_packet_sent = request->proxy_retransmit.tv_sec;
3085
3086         /*
3087          *      Encode the packet before we do anything else.
3088          */
3089         request->proxy_listener->encode(request->proxy_listener, request);
3090         debug_packet(request, request->proxy, false);
3091
3092         /*
3093          *      Set the state function, then the state, no child, and
3094          *      send the packet.
3095          */
3096         request->process = proxy_wait_for_reply;
3097         request->child_state = REQUEST_PROXIED;
3098         NO_CHILD_THREAD;
3099
3100         /*
3101          *      And send the packet.
3102          */
3103         request->proxy_listener->send(request->proxy_listener, request);
3104         return 1;
3105 }
3106
3107 /*
3108  *      Proxy the packet as if it was new.
3109  */
3110 static int request_proxy_anew(REQUEST *request)
3111 {
3112         home_server_t *home;
3113
3114         VERIFY_REQUEST(request);
3115
3116         /*
3117          *      Delete the request from the proxy list.
3118          *
3119          *      The packet list code takes care of ensuring that IDs
3120          *      aren't reused until all 256 IDs have been used.  So
3121          *      there's a 1/256 chance of re-using the same ID when
3122          *      we're sending to the same home server.  Which is
3123          *      acceptable.
3124          */
3125         remove_from_proxy_hash(request);
3126
3127         /*
3128          *      Find a live home server for the request.
3129          */
3130         home = home_server_ldb(NULL, request->home_pool, request);
3131         if (!home) {
3132                 REDEBUG2("Failed to find live home server for request");
3133         post_proxy_fail:
3134                 if (setup_post_proxy_fail(request)) {
3135                         request_queue_or_run(request, proxy_running);
3136                 } else {
3137                         gettimeofday(&request->reply->timestamp, NULL);
3138                         request_cleanup_delay_init(request, NULL);
3139                 }
3140                 return 0;
3141         }
3142         home_server_update_request(home, request);
3143
3144         if (!insert_into_proxy_hash(request)) {
3145                 RPROXY("Failed to insert retransmission into the proxy list");
3146                 goto post_proxy_fail;
3147         }
3148
3149         /*
3150          *      Free the old packet, to force re-encoding
3151          */
3152         talloc_free(request->proxy->data);
3153         request->proxy->data = NULL;
3154         request->proxy->data_len = 0;
3155
3156 #ifdef WITH_ACCOUNTING
3157         /*
3158          *      Update the Acct-Delay-Time attribute.
3159          */
3160         if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) {
3161                 VALUE_PAIR *vp;
3162
3163                 vp = pairfind(request->proxy->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY);
3164                 if (!vp) vp = radius_paircreate(request->proxy,
3165                                                 &request->proxy->vps,
3166                                                 PW_ACCT_DELAY_TIME, 0);
3167                 if (vp) {
3168                         struct timeval now;
3169
3170                         gettimeofday(&now, NULL);
3171                         vp->vp_integer += now.tv_sec - request->proxy_retransmit.tv_sec;
3172                 }
3173         }
3174 #endif
3175
3176         if (request_proxy(request, 1) != 1) goto post_proxy_fail;
3177
3178         return 1;
3179 }
3180
3181 STATE_MACHINE_DECL(request_ping)
3182 {
3183         home_server_t *home = request->home_server;
3184         char buffer[128];
3185
3186         VERIFY_REQUEST(request);
3187
3188         TRACE_STATE_MACHINE;
3189         ASSERT_MASTER;
3190
3191         switch (action) {
3192         case FR_ACTION_TIMER:
3193                 ERROR("No response to status check %d for home server %s port %d",
3194                        request->number,
3195                        inet_ntop(request->proxy->dst_ipaddr.af,
3196                                  &request->proxy->dst_ipaddr.ipaddr,
3197                                  buffer, sizeof(buffer)),
3198                        request->proxy->dst_port);
3199                 break;
3200
3201         case FR_ACTION_PROXY_REPLY:
3202                 rad_assert(request->in_proxy_hash);
3203
3204                 request->home_server->num_received_pings++;
3205                 RPROXY("Received response to status check %d (%d in current sequence)",
3206                        request->number, home->num_received_pings);
3207
3208                 /*
3209                  *      Remove the request from any hashes
3210                  */
3211                 ASSERT_MASTER;
3212                 fr_event_delete(el, &request->ev);
3213                 remove_from_proxy_hash(request);
3214
3215                 /*
3216                  *      The control socket may have marked the home server as
3217                  *      alive.  OR, it may have suddenly started responding to
3218                  *      requests again.  If so, don't re-do the "make alive"
3219                  *      work.
3220                  */
3221                 if (home->state == HOME_STATE_ALIVE) break;
3222
3223                 /*
3224                  *      It's dead, and we haven't received enough ping
3225                  *      responses to mark it "alive".  Wait a bit.
3226                  *
3227                  *      If it's zombie, we mark it alive immediately.
3228                  */
3229                 if ((home->state == HOME_STATE_IS_DEAD) &&
3230                     (home->num_received_pings < home->num_pings_to_alive)) {
3231                         return;
3232                 }
3233
3234                 /*
3235                  *      Mark it alive and delete any outstanding
3236                  *      pings.
3237                  */
3238                 home->state = HOME_STATE_ALIVE;
3239                 home->response_timeouts = 0;
3240                 exec_trigger(request, home->cs, "home_server.alive", false);
3241                 home->currently_outstanding = 0;
3242                 home->num_sent_pings = 0;
3243                 home->num_received_pings = 0;
3244                 gettimeofday(&home->revive_time, NULL);
3245
3246                 ASSERT_MASTER;
3247                 fr_event_delete(el, &home->ev);
3248
3249                 RPROXY("Marking home server %s port %d alive",
3250                        inet_ntop(request->proxy->dst_ipaddr.af,
3251                                  &request->proxy->dst_ipaddr.ipaddr,
3252                                  buffer, sizeof(buffer)),
3253                        request->proxy->dst_port);
3254                 break;
3255
3256         default:
3257                 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
3258                 break;
3259         }
3260
3261         rad_assert(!request->in_request_hash);
3262         rad_assert(request->ev == NULL);
3263         request_done(request, FR_ACTION_DONE);
3264 }
3265
3266 /*
3267  *      Called from start of zombie period, OR after control socket
3268  *      marks the home server dead.
3269  */
3270 static void ping_home_server(void *ctx)
3271 {
3272         home_server_t *home = talloc_get_type_abort(ctx, home_server_t);
3273         REQUEST *request;
3274         VALUE_PAIR *vp;
3275         struct timeval when, now;
3276
3277         if ((home->state == HOME_STATE_ALIVE) ||
3278 #ifdef WITH_TCP
3279             (home->proto == IPPROTO_TCP) ||
3280 #endif
3281             (home->ev != NULL)) {
3282                 return;
3283         }
3284
3285         gettimeofday(&now, NULL);
3286
3287         /*
3288          *      We've run out of zombie time.  Mark it dead.
3289          */
3290         if (home->state == HOME_STATE_ZOMBIE) {
3291                 when = home->zombie_period_start;
3292                 when.tv_sec += home->zombie_period;
3293
3294                 if (timercmp(&when, &now, <)) {
3295                         DEBUG("PING: Zombie period is over for home server %s", home->log_name);
3296                         mark_home_server_dead(home, &now);
3297                 }
3298         }
3299
3300         /*
3301          *      We're not supposed to be pinging it.  Just wake up
3302          *      when we're supposed to mark it dead.
3303          */
3304         if (home->ping_check == HOME_PING_CHECK_NONE) {
3305                 if (home->state == HOME_STATE_ZOMBIE) {
3306                         when = home->zombie_period_start;
3307                         when.tv_sec += home->zombie_period;
3308                         ASSERT_MASTER;
3309                         INSERT_EVENT(ping_home_server, home);
3310                 }
3311
3312                 /*
3313                  *      Else mark_home_server_dead will set a timer
3314                  *      for revive_interval.
3315                  */
3316                 return;
3317         }
3318
3319
3320         request = request_alloc(NULL);
3321         if (!request) return;
3322         request->number = request_num_counter++;
3323         NO_CHILD_THREAD;
3324
3325         request->proxy = rad_alloc(request, true);
3326         rad_assert(request->proxy != NULL);
3327
3328         if (home->ping_check == HOME_PING_CHECK_STATUS_SERVER) {
3329                 request->proxy->code = PW_CODE_STATUS_SERVER;
3330
3331                 pairmake(request->proxy, &request->proxy->vps,
3332                          "Message-Authenticator", "0x00", T_OP_SET);
3333
3334         } else if (home->type == HOME_TYPE_AUTH) {
3335                 request->proxy->code = PW_CODE_ACCESS_REQUEST;
3336
3337                 pairmake(request->proxy, &request->proxy->vps,
3338                          "User-Name", home->ping_user_name, T_OP_SET);
3339                 pairmake(request->proxy, &request->proxy->vps,
3340                          "User-Password", home->ping_user_password, T_OP_SET);
3341                 pairmake(request->proxy, &request->proxy->vps,
3342                          "Service-Type", "Authenticate-Only", T_OP_SET);
3343                 pairmake(request->proxy, &request->proxy->vps,
3344                          "Message-Authenticator", "0x00", T_OP_SET);
3345
3346         } else {
3347 #ifdef WITH_ACCOUNTING
3348                 request->proxy->code = PW_CODE_ACCOUNTING_REQUEST;
3349
3350                 pairmake(request->proxy, &request->proxy->vps,
3351                          "User-Name", home->ping_user_name, T_OP_SET);
3352                 pairmake(request->proxy, &request->proxy->vps,
3353                          "Acct-Status-Type", "Stop", T_OP_SET);
3354                 pairmake(request->proxy, &request->proxy->vps,
3355                          "Acct-Session-Id", "00000000", T_OP_SET);
3356                 vp = pairmake(request->proxy, &request->proxy->vps,
3357                               "Event-Timestamp", "0", T_OP_SET);
3358                 vp->vp_date = now.tv_sec;
3359 #else
3360                 rad_assert("Internal sanity check failed");
3361 #endif
3362         }
3363
3364         vp = pairmake(request->proxy, &request->proxy->vps,
3365                       "NAS-Identifier", "", T_OP_SET);
3366         if (vp) {
3367                 pairsprintf(vp, "Status Check %u. Are you alive?",
3368                             home->num_sent_pings);
3369         }
3370
3371         request->proxy->src_ipaddr = home->src_ipaddr;
3372         request->proxy->dst_ipaddr = home->ipaddr;
3373         request->proxy->dst_port = home->port;
3374         request->home_server = home;
3375 #ifdef DEBUG_STATE_MACHINE
3376         if (debug_flag) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n", request->number, __FUNCTION__,
3377                                child_state_names[request->child_state],
3378                                child_state_names[REQUEST_DONE]);
3379         if (debug_flag) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_ping");
3380 #endif
3381 #ifdef HAVE_PTHREAD_H
3382         rad_assert(request->child_pid == NO_SUCH_CHILD_PID);
3383 #endif
3384         request->child_state = REQUEST_DONE;
3385         request->process = request_ping;
3386
3387         rad_assert(request->proxy_listener == NULL);
3388
3389         if (!insert_into_proxy_hash(request)) {
3390                 RPROXY("Failed to insert status check %d into proxy list.  Discarding it.",
3391                        request->number);
3392
3393                 rad_assert(!request->in_request_hash);
3394                 rad_assert(!request->in_proxy_hash);
3395                 rad_assert(request->ev == NULL);
3396                 talloc_free(request);
3397                 return;
3398         }
3399
3400         /*
3401          *      Set up the timer callback.
3402          */
3403         when = now;
3404         when.tv_sec += home->ping_timeout;
3405
3406         DEBUG("PING: Waiting %u seconds for response to ping",
3407               home->ping_timeout);
3408
3409         STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3410         home->num_sent_pings++;
3411
3412         rad_assert(request->proxy_listener != NULL);
3413         request->proxy_listener->send(request->proxy_listener,
3414                                       request);
3415
3416         /*
3417          *      Add +/- 2s of jitter, as suggested in RFC 3539
3418          *      and in the Issues and Fixes draft.
3419          */
3420         home->when = now;
3421         home->when.tv_sec += home->ping_interval;
3422
3423         add_jitter(&home->when);
3424
3425         DEBUG("PING: Next status packet in %u seconds", home->ping_interval);
3426         ASSERT_MASTER;
3427         INSERT_EVENT(ping_home_server, home);
3428 }
3429
3430 static void home_trigger(home_server_t *home, char const *trigger)
3431 {
3432         REQUEST *my_request;
3433         RADIUS_PACKET *my_packet;
3434
3435         my_request = talloc_zero(NULL, REQUEST);
3436         my_packet = talloc_zero(my_request, RADIUS_PACKET);
3437         my_request->proxy = my_packet;
3438         my_packet->dst_ipaddr = home->ipaddr;
3439         my_packet->src_ipaddr = home->src_ipaddr;
3440
3441         exec_trigger(my_request, home->cs, trigger, false);
3442         talloc_free(my_request);
3443 }
3444
3445 static void mark_home_server_zombie(home_server_t *home, struct timeval *now, struct timeval *response_window)
3446 {
3447         time_t start;
3448         char buffer[128];
3449
3450         ASSERT_MASTER;
3451
3452         rad_assert((home->state == HOME_STATE_ALIVE) ||
3453                    (home->state == HOME_STATE_UNKNOWN));
3454
3455 #ifdef WITH_TCP
3456         if (home->proto == IPPROTO_TCP) {
3457                 WARN("Not marking TCP server %s zombie", home->log_name);
3458                 return;
3459         }
3460 #endif
3461
3462         /*
3463          *      We've received a real packet recently.  Don't mark the
3464          *      server as zombie until we've received NO packets for a
3465          *      while.  The "1/4" of zombie period was chosen rather
3466          *      arbitrarily.  It's a balance between too short, which
3467          *      gives quick fail-over and fail-back, or too long,
3468          *      where the proxy still sends packets to an unresponsive
3469          *      home server.
3470          */
3471         start = now->tv_sec - ((home->zombie_period + 3) / 4);
3472         if (home->last_packet_recv >= start) {
3473                 DEBUG("Recieved reply from home server %d seconds ago.  Might not be zombie.",
3474                       (int) (now->tv_sec - home->last_packet_recv));
3475                 return;
3476         }
3477
3478         home->state = HOME_STATE_ZOMBIE;
3479         home_trigger(home, "home_server.zombie");
3480
3481         /*
3482          *      Set the home server to "zombie", as of the time
3483          *      calculated above.
3484          */
3485         home->zombie_period_start.tv_sec = start;
3486         home->zombie_period_start.tv_usec = USEC / 2;
3487
3488         ASSERT_MASTER;
3489         fr_event_delete(el, &home->ev);
3490         home->num_sent_pings = 0;
3491         home->num_received_pings = 0;
3492
3493         PROXY( "Marking home server %s port %d as zombie (it has not responded in %d.%06d seconds).",
3494                inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
3495                          buffer, sizeof(buffer)),
3496                home->port, (int) response_window->tv_sec, (int) response_window->tv_usec);
3497
3498         ping_home_server(home);
3499 }
3500
3501
3502 void revive_home_server(void *ctx)
3503 {
3504         home_server_t *home = talloc_get_type_abort(ctx, home_server_t);
3505         char buffer[128];
3506
3507 #ifdef WITH_TCP
3508         rad_assert(home->proto != IPPROTO_TCP);
3509 #endif
3510
3511         home->state = HOME_STATE_ALIVE;
3512         home->response_timeouts = 0;
3513         home_trigger(home, "home_server.alive");
3514         home->currently_outstanding = 0;
3515         gettimeofday(&home->revive_time, NULL);
3516
3517         /*
3518          *      Delete any outstanding events.
3519          */
3520         ASSERT_MASTER;
3521         if (home->ev) fr_event_delete(el, &home->ev);
3522
3523         PROXY( "Marking home server %s port %d alive again... we have no idea if it really is alive or not.",
3524                inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
3525                          buffer, sizeof(buffer)),
3526                home->port);
3527 }
3528
3529 void mark_home_server_dead(home_server_t *home, struct timeval *when)
3530 {
3531         int previous_state = home->state;
3532         char buffer[128];
3533
3534 #ifdef WITH_TCP
3535         if (home->proto == IPPROTO_TCP) {
3536                 WARN("Not marking TCP server dead");
3537                 return;
3538         }
3539 #endif
3540
3541         PROXY( "Marking home server %s port %d as dead.",
3542                inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
3543                          buffer, sizeof(buffer)),
3544                home->port);
3545
3546         home->state = HOME_STATE_IS_DEAD;
3547         home_trigger(home, "home_server.dead");
3548
3549         if (home->ping_check != HOME_PING_CHECK_NONE) {
3550                 /*
3551                  *      If the control socket marks us dead, start
3552                  *      pinging.  Otherwise, we already started
3553                  *      pinging when it was marked "zombie".
3554                  */
3555                 if (previous_state == HOME_STATE_ALIVE) {
3556                         ping_home_server(home);
3557                 } else {
3558                         DEBUG("PING: Already pinging home server %s", home->log_name);
3559                 }
3560
3561         } else {
3562                 /*
3563                  *      Revive it after a fixed period of time.  This
3564                  *      is very, very, bad.
3565                  */
3566                 home->when = *when;
3567                 home->when.tv_sec += home->revive_interval;
3568
3569                 DEBUG("PING: Reviving home server %s in %u seconds", home->log_name, home->revive_interval);
3570                 ASSERT_MASTER;
3571                 INSERT_EVENT(revive_home_server, home);
3572         }
3573 }
3574
3575 STATE_MACHINE_DECL(proxy_wait_for_reply)
3576 {
3577         struct timeval now, when;
3578         struct timeval *response_window = NULL;
3579         home_server_t *home = request->home_server;
3580         char buffer[128];
3581
3582         VERIFY_REQUEST(request);
3583
3584         TRACE_STATE_MACHINE;
3585
3586         rad_assert(request->packet->code != PW_CODE_STATUS_SERVER);
3587         rad_assert(request->home_server != NULL);
3588
3589         if (request->master_state == REQUEST_STOP_PROCESSING) {
3590                 request->child_state = REQUEST_DONE;
3591                 return;
3592         }
3593
3594         gettimeofday(&now, NULL);
3595
3596         switch (action) {
3597         case FR_ACTION_DUP:
3598                 /*
3599                  *      We have a reply, ignore the retransmit.
3600                  */
3601                 if (request->proxy_reply) return;
3602
3603                 /*
3604                  *      The request was proxied to a virtual server.
3605                  *      Ignore the retransmit.
3606                  */
3607                 if (request->home_server->server) return;
3608
3609                 if ((home->state == HOME_STATE_IS_DEAD) ||
3610                     !request->proxy_listener ||
3611                     (request->proxy_listener->status != RAD_LISTEN_STATUS_KNOWN)) {
3612                         request_proxy_anew(request);
3613                         return;
3614                 }
3615
3616 #ifdef WITH_TCP
3617                 if (home->proto == IPPROTO_TCP) {
3618                         DEBUG2("Suppressing duplicate proxied request (tcp) to home server %s port %d proto TCP - ID: %d",
3619                                inet_ntop(request->proxy->dst_ipaddr.af,
3620                                          &request->proxy->dst_ipaddr.ipaddr,
3621                                          buffer, sizeof(buffer)),
3622                                request->proxy->dst_port,
3623                                request->proxy->id);
3624                         return;
3625                 }
3626 #endif
3627
3628                 /*
3629                  *      More than one retransmit a second is stupid,
3630                  *      and should be suppressed by the proxy.
3631                  */
3632                 when = request->proxy_retransmit;
3633                 when.tv_sec++;
3634
3635                 if (timercmp(&now, &when, <)) {
3636                         DEBUG2("Suppressing duplicate proxied request (too fast) to home server %s port %d proto TCP - ID: %d",
3637                                inet_ntop(request->proxy->dst_ipaddr.af,
3638                                          &request->proxy->dst_ipaddr.ipaddr,
3639                                          buffer, sizeof(buffer)),
3640                                request->proxy->dst_port,
3641                                request->proxy->id);
3642                         return;
3643                 }
3644
3645 #ifdef WITH_ACCOUNTING
3646                 /*
3647                  *      If we update the Acct-Delay-Time, we need to
3648                  *      get a new ID.
3649                  */
3650                 if ((request->packet->code == PW_CODE_ACCOUNTING_REQUEST) &&
3651                     pairfind(request->proxy->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY)) {
3652                         request_proxy_anew(request);
3653                         return;
3654                 }
3655 #endif
3656
3657                 RDEBUG2("Sending duplicate proxied request to home server %s port %d - ID: %d",
3658                         inet_ntop(request->proxy->dst_ipaddr.af,
3659                                   &request->proxy->dst_ipaddr.ipaddr,
3660                                   buffer, sizeof(buffer)),
3661                         request->proxy->dst_port,
3662                         request->proxy->id);
3663                 request->num_proxied_requests++;
3664
3665                 rad_assert(request->proxy_listener != NULL);
3666                 FR_STATS_TYPE_INC(home->stats.total_requests);
3667                 home->last_packet_sent = now.tv_sec;
3668                 request->proxy_retransmit = now;
3669                 request->proxy_listener->send(request->proxy_listener, request);
3670                 debug_packet(request, request->proxy, false);
3671                 break;
3672
3673         case FR_ACTION_TIMER:
3674                 response_window = request_response_window(request);
3675
3676 #ifdef WITH_TCP
3677                 if (!request->proxy_listener ||
3678                     (request->proxy_listener->status != RAD_LISTEN_STATUS_KNOWN)) {
3679                         remove_from_proxy_hash(request);
3680
3681                         when = request->packet->timestamp;
3682                         when.tv_sec += request->root->max_request_time;
3683
3684                         if (timercmp(&when, &now, >)) {
3685                                 RDEBUG("Waiting for client retransmission in order to do a proxy retransmit");
3686                                 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3687                                 return;
3688                         }
3689                 } else
3690 #endif
3691                 {
3692                         /*
3693                          *      Wake up "response_window" time in the future.
3694                          *      i.e. when MY packet hasn't received a response.
3695                          *
3696                          *      Note that we DO NOT mark the home server as
3697                          *      zombie if it doesn't respond to us.  It may be
3698                          *      responding to other (better looking) packets.
3699                          */
3700                         when = request->proxy->timestamp;
3701                         timeradd(&when, response_window, &when);
3702
3703                         /*
3704                          *      Not at the response window.  Set the timer for
3705                          *      that.
3706                          */
3707                         if (timercmp(&when, &now, >)) {
3708                                 struct timeval diff;
3709                                 timersub(&when, &now, &diff);
3710
3711                                 RDEBUG("Expecting proxy response no later than %d.%06d seconds from now",
3712                                        (int) diff.tv_sec, (int) diff.tv_usec);
3713                                 STATE_MACHINE_TIMER(FR_ACTION_TIMER);
3714                                 return;
3715                         }
3716                 }
3717
3718                 RDEBUG("No proxy response, giving up on request and marking it done");
3719
3720                 /*
3721                  *      If we haven't received any packets for
3722                  *      "response_window", then mark the home server
3723                  *      as zombie.
3724                  *
3725                  *      If the connection is TCP, then another
3726                  *      "watchdog timer" function takes care of pings,
3727                  *      etc.  So we don't need to do it here.
3728                  *
3729                  *      This check should really be part of a home
3730                  *      server state machine.
3731                  */
3732                 if (((home->state == HOME_STATE_ALIVE) ||
3733                      (home->state == HOME_STATE_UNKNOWN))
3734 #ifdef WITH_TCP
3735                     && (home->proto != IPPROTO_TCP)
3736 #endif
3737                         ) {
3738                         home->response_timeouts++;
3739                         if (home->response_timeouts >= home->max_response_timeouts)
3740                                 mark_home_server_zombie(home, &now, response_window);
3741                 }
3742
3743                 FR_STATS_TYPE_INC(home->stats.total_timeouts);
3744                 if (home->type == HOME_TYPE_AUTH) {
3745                         if (request->proxy_listener) FR_STATS_TYPE_INC(request->proxy_listener->stats.total_timeouts);
3746                         FR_STATS_TYPE_INC(proxy_auth_stats.total_timeouts);
3747                 }
3748 #ifdef WITH_ACCT
3749                 else if (home->type == HOME_TYPE_ACCT) {
3750                         if (request->proxy_listener) FR_STATS_TYPE_INC(request->proxy_listener->stats.total_timeouts);
3751                         FR_STATS_TYPE_INC(proxy_acct_stats.total_timeouts);
3752                 }
3753 #endif
3754
3755                 /*
3756                  *      There was no response within the window.  Stop
3757                  *      the request.  If the client retransmitted, it
3758                  *      may have failed over to another home server.
3759                  *      But that one may be dead, too.
3760                  *
3761                  *      The extra verbose message if we have a username,
3762                  *      is extremely useful if the proxy is part of a chain
3763                  *      and the final home server, is not the one we're
3764                  *      proxying to.
3765                  */
3766                 if (request->username) {
3767                         RERROR("Failing proxied request for user \"%s\", due to lack of any response from home "
3768                                "server %s port %d",
3769                                request->username->vp_strvalue,
3770                                inet_ntop(request->proxy->dst_ipaddr.af,
3771                                          &request->proxy->dst_ipaddr.ipaddr,
3772                                          buffer, sizeof(buffer)),
3773                                request->proxy->dst_port);
3774                 } else {
3775                         RERROR("Failing proxied request, due to lack of any response from home server %s port %d",
3776                                inet_ntop(request->proxy->dst_ipaddr.af,
3777                                          &request->proxy->dst_ipaddr.ipaddr,
3778                                          buffer, sizeof(buffer)),
3779                                request->proxy->dst_port);
3780                 }
3781
3782                 if (setup_post_proxy_fail(request)) {
3783                         request_queue_or_run(request, proxy_no_reply);
3784                 } else {
3785                         gettimeofday(&request->reply->timestamp, NULL);
3786                         request_cleanup_delay_init(request, NULL);
3787                 }
3788                 break;
3789
3790                 /*
3791                  *      We received a new reply.  Go process it.
3792                  */
3793         case FR_ACTION_PROXY_REPLY:
3794                 request_queue_or_run(request, proxy_running);
3795                 break;
3796
3797         default:
3798                 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
3799                 break;
3800         }
3801 }
3802 #endif  /* WITH_PROXY */
3803
3804 /***********************************************************************
3805  *
3806  *  CoA code
3807  *
3808  ***********************************************************************/
3809 #ifdef WITH_COA
3810 static int null_handler(UNUSED REQUEST *request)
3811 {
3812         return 0;
3813 }
3814
3815 /*
3816  *      See if we need to originate a CoA request.
3817  */
3818 static void request_coa_originate(REQUEST *request)
3819 {
3820         int rcode, pre_proxy_type = 0;
3821         VALUE_PAIR *vp;
3822         REQUEST *coa;
3823         fr_ipaddr_t ipaddr;
3824         char buffer[256];
3825
3826         VERIFY_REQUEST(request);
3827
3828         rad_assert(request->coa != NULL);
3829         rad_assert(request->proxy == NULL);
3830         rad_assert(!request->in_proxy_hash);
3831         rad_assert(request->proxy_reply == NULL);
3832
3833         /*
3834          *      Check whether we want to originate one, or cancel one.
3835          */
3836         vp = pairfind(request->config_items, PW_SEND_COA_REQUEST, 0, TAG_ANY);
3837         if (!vp) {
3838                 vp = pairfind(request->coa->proxy->vps, PW_SEND_COA_REQUEST, 0, TAG_ANY);
3839         }
3840
3841         if (vp) {
3842                 if (vp->vp_integer == 0) {
3843                 fail:
3844                         TALLOC_FREE(request->coa);
3845                         return;
3846                 }
3847         }
3848
3849         coa = request->coa;
3850
3851         /*
3852          *      src_ipaddr will be set up in proxy_encode.
3853          */
3854         memset(&ipaddr, 0, sizeof(ipaddr));
3855         vp = pairfind(coa->proxy->vps, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY);
3856         if (vp) {
3857                 ipaddr.af = AF_INET;
3858                 ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
3859                 ipaddr.prefix = 32;
3860         } else if ((vp = pairfind(coa->proxy->vps, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY)) != NULL) {
3861                 ipaddr.af = AF_INET6;
3862                 ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
3863                 ipaddr.prefix = 128;
3864         } else if ((vp = pairfind(coa->proxy->vps, PW_HOME_SERVER_POOL, 0, TAG_ANY)) != NULL) {
3865                 coa->home_pool = home_pool_byname(vp->vp_strvalue,
3866                                                   HOME_TYPE_COA);
3867                 if (!coa->home_pool) {
3868                         RWDEBUG2("No such home_server_pool %s",
3869                                vp->vp_strvalue);
3870                         goto fail;
3871                 }
3872
3873                 /*
3874                  *      Prefer the pool to one server
3875                  */
3876         } else if (request->client->coa_pool) {
3877                 coa->home_pool = request->client->coa_pool;
3878
3879         } else if (request->client->coa_server) {
3880                 coa->home_server = request->client->coa_server;
3881
3882         } else {
3883                 /*
3884                  *      If all else fails, send it to the client that
3885                  *      originated this request.
3886                  */
3887                 memcpy(&ipaddr, &request->packet->src_ipaddr, sizeof(ipaddr));
3888         }
3889
3890         /*
3891          *      Use the pool, if it exists.
3892          */
3893         if (coa->home_pool) {
3894                 coa->home_server = home_server_ldb(NULL, coa->home_pool, coa);
3895                 if (!coa->home_server) {
3896                         RWDEBUG("No live home server for home_server_pool %s", coa->home_pool->name);
3897                         goto fail;
3898                 }
3899                 home_server_update_request(coa->home_server, coa);
3900
3901         } else if (!coa->home_server) {
3902                 uint16_t port = PW_COA_UDP_PORT;
3903
3904                 vp = pairfind(coa->proxy->vps, PW_PACKET_DST_PORT, 0, TAG_ANY);
3905                 if (vp) port = vp->vp_integer;
3906
3907                 coa->home_server = home_server_find(&ipaddr, port, IPPROTO_UDP);
3908                 if (!coa->home_server) {
3909                         RWDEBUG2("Unknown destination %s:%d for CoA request.",
3910                                inet_ntop(ipaddr.af, &ipaddr.ipaddr,
3911                                          buffer, sizeof(buffer)), port);
3912                         goto fail;
3913                 }
3914         }
3915
3916         vp = pairfind(coa->proxy->vps, PW_PACKET_TYPE, 0, TAG_ANY);
3917         if (vp) {
3918                 switch (vp->vp_integer) {
3919                 case PW_CODE_COA_REQUEST:
3920                 case PW_CODE_DISCONNECT_REQUEST:
3921                         coa->proxy->code = vp->vp_integer;
3922                         break;
3923
3924                 default:
3925                         DEBUG("Cannot set CoA Packet-Type to code %d",
3926                               vp->vp_integer);
3927                         goto fail;
3928                 }
3929         }
3930
3931         if (!coa->proxy->code) coa->proxy->code = PW_CODE_COA_REQUEST;
3932
3933         /*
3934          *      The rest of the server code assumes that
3935          *      request->packet && request->reply exist.  Copy them
3936          *      from the original request.
3937          */
3938         rad_assert(coa->packet != NULL);
3939         rad_assert(coa->packet->vps == NULL);
3940
3941         coa->packet = rad_copy_packet(coa, request->packet);
3942         coa->reply = rad_copy_packet(coa, request->reply);
3943
3944         coa->config_items = paircopy(coa, request->config_items);
3945         coa->num_coa_requests = 0;
3946         coa->handle = null_handler;
3947         coa->number = request->number; /* it's associated with the same request */
3948
3949         /*
3950          *      Call the pre-proxy routines.
3951          */
3952         vp = pairfind(request->config_items, PW_PRE_PROXY_TYPE, 0, TAG_ANY);
3953         if (vp) {
3954                 DICT_VALUE const *dval = dict_valbyattr(vp->da->attr, vp->da->vendor, vp->vp_integer);
3955                 /* Must be a validation issue */
3956                 rad_assert(dval);
3957                 RDEBUG2("Found Pre-Proxy-Type %s", dval->name);
3958                 pre_proxy_type = vp->vp_integer;
3959         }
3960
3961         if (coa->home_pool && coa->home_pool->virtual_server) {
3962                 char const *old_server = coa->server;
3963
3964                 coa->server = coa->home_pool->virtual_server;
3965                 RDEBUG2("server %s {", coa->server);
3966                 RINDENT();
3967                 rcode = process_pre_proxy(pre_proxy_type, coa);
3968                 REXDENT();
3969                 RDEBUG2("}");
3970                 coa->server = old_server;
3971         } else {
3972                 rcode = process_pre_proxy(pre_proxy_type, coa);
3973         }
3974         switch (rcode) {
3975         default:
3976                 goto fail;
3977
3978         /*
3979          *      Only send the CoA packet if the pre-proxy code succeeded.
3980          */
3981         case RLM_MODULE_NOOP:
3982         case RLM_MODULE_OK:
3983         case RLM_MODULE_UPDATED:
3984                 break;
3985         }
3986
3987         /*
3988          *      Source IP / port is set when the proxy socket
3989          *      is chosen.
3990          */
3991         coa->proxy->dst_ipaddr = coa->home_server->ipaddr;
3992         coa->proxy->dst_port = coa->home_server->port;
3993
3994         if (!insert_into_proxy_hash(coa)) {
3995                 radlog_request(L_PROXY, 0, coa, "Failed to insert CoA request into proxy list");
3996                 goto fail;
3997         }
3998
3999         /*
4000          *      We CANNOT divorce the CoA request from the parent
4001          *      request.  This function is running in a child thread,
4002          *      and we need access to the main event loop in order to
4003          *      to add the timers for the CoA packet.
4004          *
4005          *      Instead, we wait for the timer on the parent request
4006          *      to fire.
4007          */
4008         gettimeofday(&coa->proxy->timestamp, NULL);
4009         coa->packet->timestamp = coa->proxy->timestamp; /* for max_request_time */
4010         coa->home_server->last_packet_sent = coa->proxy->timestamp.tv_sec;
4011         coa->delay = 0;         /* need to calculate a new delay */
4012
4013         /*
4014          *      If requested, put a State attribute into the packet,
4015          *      and cache the VPS.
4016          */
4017         fr_state_put_vps(coa, NULL, coa->packet);
4018
4019         /*
4020          *      Encode the packet before we do anything else.
4021          */
4022         coa->proxy_listener->encode(coa->proxy_listener, coa);
4023         debug_packet(coa, coa->proxy, false);
4024
4025 #ifdef DEBUG_STATE_MACHINE
4026         if (debug_flag) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n", request->number, __FUNCTION__,
4027                                child_state_names[request->child_state],
4028                                child_state_names[REQUEST_PROXIED]);
4029 #endif
4030
4031         /*
4032          *      Set the state function, then the state, no child, and
4033          *      send the packet.
4034          */
4035         coa->process = coa_wait_for_reply;
4036         coa->child_state = REQUEST_PROXIED;
4037
4038 #ifdef HAVE_PTHREAD_H
4039         coa->child_pid = NO_SUCH_CHILD_PID;
4040 #endif
4041
4042         /*
4043          *      And send the packet.
4044          */
4045         coa->proxy_listener->send(coa->proxy_listener, coa);
4046 }
4047
4048
4049 static void coa_timer(REQUEST *request)
4050 {
4051         uint32_t delay, frac;
4052         struct timeval now, when, mrd;
4053
4054         VERIFY_REQUEST(request);
4055
4056         rad_assert(request->parent == NULL);
4057
4058         if (request->proxy_reply) {
4059                 request_process_timer(request);
4060                 return;
4061         }
4062         gettimeofday(&now, NULL);
4063
4064         if (request->delay == 0) {
4065                 /*
4066                  *      Implement re-transmit algorithm as per RFC 5080
4067                  *      Section 2.2.1.
4068                  *
4069                  *      We want IRT + RAND*IRT
4070                  *      or 0.9 IRT + rand(0,.2) IRT
4071                  *
4072                  *      2^20 ~ USEC, and we want 2.
4073                  *      rand(0,0.2) USEC ~ (rand(0,2^21) / 10)
4074                  */
4075                 delay = (fr_rand() & ((1 << 22) - 1)) / 10;
4076                 request->delay = delay * request->home_server->coa_irt;
4077                 delay = request->home_server->coa_irt * USEC;
4078                 delay -= delay / 10;
4079                 delay += request->delay;
4080                 request->delay = delay;
4081
4082                 when = request->proxy->timestamp;
4083                 tv_add(&when, delay);
4084
4085                 if (timercmp(&when, &now, >)) {
4086                         STATE_MACHINE_TIMER(FR_ACTION_TIMER);
4087                         return;
4088                 }
4089         }
4090
4091         /*
4092          *      Retransmit CoA request.
4093          */
4094
4095         /*
4096          *      Cap count at MRC, if it is non-zero.
4097          */
4098         if (request->home_server->coa_mrc &&
4099             (request->num_coa_requests >= request->home_server->coa_mrc)) {
4100                 char buffer[128];
4101
4102                 RERROR("Failing request - originate-coa ID %u, due to lack of any response from coa server %s port %d",
4103                        request->proxy->id,
4104                                inet_ntop(request->proxy->dst_ipaddr.af,
4105                                          &request->proxy->dst_ipaddr.ipaddr,
4106                                          buffer, sizeof(buffer)),
4107                                request->proxy->dst_port);
4108
4109                 if (setup_post_proxy_fail(request)) {
4110                         request_queue_or_run(request, coa_no_reply);
4111                 } else {
4112                         request_done(request, FR_ACTION_DONE);
4113                 }
4114                 return;
4115         }
4116
4117         /*
4118          *      RFC 5080 Section 2.2.1
4119          *
4120          *      RT = 2*RTprev + RAND*RTprev
4121          *         = 1.9 * RTprev + rand(0,.2) * RTprev
4122          *         = 1.9 * RTprev + rand(0,1) * (RTprev / 5)
4123          */
4124         delay = fr_rand();
4125         delay ^= (delay >> 16);
4126         delay &= 0xffff;
4127         frac = request->delay / 5;
4128         delay = ((frac >> 16) * delay) + (((frac & 0xffff) * delay) >> 16);
4129
4130         delay += (2 * request->delay) - (request->delay / 10);
4131
4132         /*
4133          *      Cap delay at MRT, if MRT is non-zero.
4134          */
4135         if (request->home_server->coa_mrt &&
4136             (delay > (request->home_server->coa_mrt * USEC))) {
4137                 int mrt_usec = request->home_server->coa_mrt * USEC;
4138
4139                 /*
4140                  *      delay = MRT + RAND * MRT
4141                  *            = 0.9 MRT + rand(0,.2)  * MRT
4142                  */
4143                 delay = fr_rand();
4144                 delay ^= (delay >> 15);
4145                 delay &= 0x1ffff;
4146                 delay = ((mrt_usec >> 16) * delay) + (((mrt_usec & 0xffff) * delay) >> 16);
4147                 delay += mrt_usec - (mrt_usec / 10);
4148         }
4149
4150         request->delay = delay;
4151         when = now;
4152         tv_add(&when, request->delay);
4153         mrd = request->proxy->timestamp;
4154         mrd.tv_sec += request->home_server->coa_mrd;
4155
4156         /*
4157          *      Cap duration at MRD.
4158          */
4159         if (timercmp(&mrd, &when, <)) {
4160                 when = mrd;
4161         }
4162         STATE_MACHINE_TIMER(FR_ACTION_TIMER);
4163
4164         request->num_coa_requests++; /* is NOT reset by code 3 lines above! */
4165
4166         FR_STATS_TYPE_INC(request->home_server->stats.total_requests);
4167
4168         /*
4169          *      Status servers don't count as real packets sent.
4170          */
4171         request->proxy_listener->send(request->proxy_listener,
4172                                       request);
4173 }
4174
4175 STATE_MACHINE_DECL(coa_wait_for_reply)
4176 {
4177         VERIFY_REQUEST(request);
4178
4179         TRACE_STATE_MACHINE;
4180
4181         switch (action) {
4182         case FR_ACTION_TIMER:
4183                 /*
4184                  *      This is big enough to be in it's own function.
4185                  */
4186                 coa_timer(request);
4187                 break;
4188
4189         case FR_ACTION_PROXY_REPLY:
4190                 rad_assert(request->parent != NULL);
4191                 rad_assert(request->parent->coa == request);
4192                 rad_assert((request->proxy->code == PW_CODE_COA_REQUEST) ||
4193                            (request->proxy->code == PW_CODE_DISCONNECT_REQUEST));
4194                 rad_assert(request->process != NULL);
4195
4196                 coa_separate(request, FR_ACTION_PROXY_REPLY);
4197
4198                 rad_assert(request->parent == NULL);
4199
4200                 /*
4201                  *      Do NOT get the session-state VPs.  The request
4202                  *      already contains the packet and the reply, so
4203                  *      there's no more state we need to maintain.
4204                  *
4205                  *      The state for "originate CoA" is for the next
4206                  *      Access-Request, not for the CoA ACK/BAK
4207                  */
4208
4209                 request_queue_or_run(request, coa_running);
4210                 break;
4211
4212         default:
4213                 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
4214                 break;
4215         }
4216 }
4217
4218 STATE_MACHINE_DECL(coa_separate)
4219 {
4220         VERIFY_REQUEST(request);
4221
4222         TRACE_STATE_MACHINE;
4223
4224         rad_assert(request->parent != NULL);
4225         rad_assert(request->parent->coa == request);
4226         rad_assert(request->ev == NULL);
4227         rad_assert(!request->in_request_hash);
4228         rad_assert(request->coa == NULL);
4229
4230         rad_assert(request->proxy_reply || request->proxy_listener);
4231
4232         (void) talloc_steal(NULL, request);
4233         request->parent->coa = NULL;
4234         request->parent = NULL;
4235
4236         /*
4237          *      Most of the time we're called for timers.
4238          */
4239         switch (action) {
4240         case FR_ACTION_TIMER:
4241                 request->process(request, FR_ACTION_TIMER);
4242                 break;
4243
4244                 /*
4245                  *      Set up the main timers.
4246                  */
4247         case FR_ACTION_PROXY_REPLY:
4248                 request->child_state = REQUEST_QUEUED;
4249                 request_process_timer(request);
4250                 break;
4251
4252         default:
4253                 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
4254                 break;
4255         }
4256 }
4257
4258 STATE_MACHINE_DECL(coa_no_reply)
4259 {
4260         char buffer[128];
4261
4262         VERIFY_REQUEST(request);
4263
4264         TRACE_STATE_MACHINE;
4265
4266         switch (action) {
4267         case FR_ACTION_TIMER:
4268                 request_common(request, action);
4269                 break;
4270
4271         case FR_ACTION_PROXY_REPLY: /* too late! */
4272                 RDEBUG2("Reply from CoA server %s port %d  - ID: %d arrived too late.",
4273                         inet_ntop(request->proxy->src_ipaddr.af,
4274                                   &request->proxy->src_ipaddr.ipaddr,
4275                                   buffer, sizeof(buffer)),
4276                         request->proxy->dst_port, request->proxy->id);
4277                 break;
4278
4279         case FR_ACTION_RUN:
4280                 /*
4281                  *      FIXME: do recv_coa Fail
4282                  */
4283                 (void) process_proxy_reply(request, NULL);
4284                 request_done(request, FR_ACTION_DONE);
4285                 break;
4286
4287         default:
4288                 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
4289                 break;
4290         }
4291 }
4292
4293 STATE_MACHINE_DECL(coa_running)
4294 {
4295         VERIFY_REQUEST(request);
4296
4297         TRACE_STATE_MACHINE;
4298
4299         switch (action) {
4300                 /*
4301                  *      Silently ignore duplicate proxy replies.
4302                  */
4303         case FR_ACTION_PROXY_REPLY:
4304                 break;
4305
4306         case FR_ACTION_TIMER:
4307                 request_process_timer(request);
4308                 break;
4309
4310         case FR_ACTION_RUN:
4311                 if (process_proxy_reply(request, request->proxy_reply)) {
4312                         request->handle(request);
4313                         request_finish(request, action);
4314                 } else {
4315                         request_done(request, FR_ACTION_DONE);
4316                 }
4317                 break;
4318
4319         default:
4320                 RDEBUG3("%s: Ignoring action %s", __FUNCTION__, action_codes[action]);
4321                 break;
4322         }
4323 }
4324 #endif  /* WITH_COA */
4325
4326 /***********************************************************************
4327  *
4328  *  End of the State machine.  Start of additional helper code.
4329  *
4330  ***********************************************************************/
4331
4332 /***********************************************************************
4333  *
4334  *      Event handlers.
4335  *
4336  ***********************************************************************/
4337 static void event_socket_handler(fr_event_list_t *xel, UNUSED int fd, void *ctx)
4338 {
4339         rad_listen_t *listener = talloc_get_type_abort(ctx, rad_listen_t);
4340
4341         rad_assert(xel == el);
4342
4343         if ((listener->fd < 0)
4344 #ifdef WITH_DETAIL
4345 #ifndef WITH_DETAIL_THREAD
4346             && (listener->type != RAD_LISTEN_DETAIL)
4347 #endif
4348 #endif
4349                 ) {
4350                 char buffer[256];
4351
4352                 listener->print(listener, buffer, sizeof(buffer));
4353                 ERROR("FATAL: Asked to read from closed socket: %s",
4354                        buffer);
4355
4356                 rad_panic("Socket was closed on us!");
4357                 fr_exit_now(1);
4358         }
4359
4360         listener->recv(listener);
4361 }
4362
4363 #ifdef WITH_DETAIL
4364 #ifdef WITH_DETAIL_THREAD
4365 #else
4366 /*
4367  *      This function is called periodically to see if this detail
4368  *      file is available for reading.
4369  */
4370 static void event_poll_detail(void *ctx)
4371 {
4372         int delay;
4373         rad_listen_t *this = talloc_get_type_abort(ctx, rad_listen_t);
4374         struct timeval when, now;
4375         listen_detail_t *detail = this->data;
4376
4377         rad_assert(this->type == RAD_LISTEN_DETAIL);
4378
4379  redo:
4380         event_socket_handler(el, this->fd, this);
4381
4382         fr_event_now(el, &now);
4383         when = now;
4384
4385         /*
4386          *      Backdoor API to get the delay until the next poll
4387          *      time.
4388          */
4389         delay = this->encode(this, NULL);
4390         if (delay == 0) goto redo;
4391
4392         tv_add(&when, delay);
4393
4394         ASSERT_MASTER;
4395         if (!fr_event_insert(el, event_poll_detail, this,
4396                              &when, &detail->ev)) {
4397                 ERROR("Failed creating handler");
4398                 fr_exit(1);
4399         }
4400 }
4401 #endif  /* WITH_DETAIL_THREAD */
4402 #endif  /* WITH_DETAIL */
4403
4404 static void event_status(struct timeval *wake)
4405 {
4406 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
4407         int argval;
4408 #endif
4409
4410         if (debug_flag == 0) {
4411                 if (just_started) {
4412                         INFO("Ready to process requests");
4413                         just_started = false;
4414                 }
4415                 return;
4416         }
4417
4418         if (!wake) {
4419                 INFO("Ready to process requests");
4420
4421         } else if ((wake->tv_sec != 0) ||
4422                    (wake->tv_usec >= 100000)) {
4423                 DEBUG("Waking up in %d.%01u seconds.",
4424                       (int) wake->tv_sec, (unsigned int) wake->tv_usec / 100000);
4425         }
4426
4427
4428         /*
4429          *      FIXME: Put this somewhere else, where it isn't called
4430          *      all of the time...
4431          */
4432
4433 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
4434         /*
4435          *      If there are no child threads, then there may
4436          *      be child processes.  In that case, wait for
4437          *      their exit status, and throw that exit status
4438          *      away.  This helps get rid of zxombie children.
4439          */
4440         while (waitpid(-1, &argval, WNOHANG) > 0) {
4441                 /* do nothing */
4442         }
4443 #endif
4444
4445 }
4446
4447 #ifdef WITH_TCP
4448 static void listener_free_cb(void *ctx)
4449 {
4450         rad_listen_t *this = talloc_get_type_abort(ctx, rad_listen_t);
4451         char buffer[1024];
4452
4453         if (this->count > 0) {
4454                 struct timeval when;
4455                 listen_socket_t *sock = this->data;
4456
4457                 fr_event_now(el, &when);
4458                 when.tv_sec += 3;
4459
4460                 ASSERT_MASTER;
4461                 if (!fr_event_insert(el, listener_free_cb, this, &when,
4462                                      &(sock->ev))) {
4463                         rad_panic("Failed to insert event");
4464                 }
4465
4466                 return;
4467         }
4468
4469         /*
4470          *      It's all free, close the socket.
4471          */
4472
4473         this->print(this, buffer, sizeof(buffer));
4474         DEBUG("... cleaning up socket %s", buffer);
4475         rad_assert(this->next == NULL);
4476         talloc_free(this);
4477 }
4478 #endif
4479
4480 #ifdef WITH_PROXY
4481 static int proxy_eol_cb(void *ctx, void *data)
4482 {
4483         struct timeval when;
4484         REQUEST *request = fr_packet2myptr(REQUEST, proxy, data);
4485
4486         if (request->proxy_listener != ctx) return 0;
4487
4488         /*
4489          *      We don't care if it's being processed in a child thread.
4490          */
4491
4492 #ifdef WITH_ACCOUNTING
4493         /*
4494          *      Accounting packets should be deleted immediately.
4495          *      They will never be retransmitted by the client.
4496          */
4497         if (request->proxy->code == PW_CODE_ACCOUNTING_REQUEST) {
4498                 RDEBUG("Stopping request due to failed connection to home server");
4499                 request->master_state = REQUEST_STOP_PROCESSING;
4500         }
4501 #endif
4502
4503         /*
4504          *      Reset the timer to be now, so that the request is
4505          *      quickly updated.  But spread the requests randomly
4506          *      over the next second, so that we don't overload the
4507          *      server.
4508          */
4509         fr_event_now(el, &when);
4510         tv_add(&when, fr_rand() % USEC);
4511         STATE_MACHINE_TIMER(FR_ACTION_TIMER);
4512
4513         /*
4514          *      Don't delete it from the list.
4515          */
4516         return 0;
4517 }
4518 #endif
4519
4520 static int event_new_fd(rad_listen_t *this)
4521 {
4522         char buffer[1024];
4523
4524         ASSERT_MASTER;
4525
4526         if (this->status == RAD_LISTEN_STATUS_KNOWN) return 1;
4527
4528         this->print(this, buffer, sizeof(buffer));
4529
4530         if (this->status == RAD_LISTEN_STATUS_INIT) {
4531                 listen_socket_t *sock = this->data;
4532
4533                 rad_assert(sock != NULL);
4534                 if (just_started) {
4535                         DEBUG("Listening on %s", buffer);
4536
4537 #ifdef WITH_PROXY
4538                 } else if (this->type == RAD_LISTEN_PROXY) {
4539                         home_server_t *home;
4540
4541                         home = sock->home;
4542                         if (!home || !home->limit.max_connections) {
4543                                 INFO(" ... adding new socket %s", buffer);
4544                         } else {
4545                                 INFO(" ... adding new socket %s (%u of %u)", buffer,
4546                                      home->limit.num_connections, home->limit.max_connections);
4547                         }
4548
4549 #endif
4550                 } else {
4551                         INFO(" ... adding new socket %s", buffer);
4552                 }
4553
4554                 switch (this->type) {
4555 #ifdef WITH_DETAIL
4556                 /*
4557                  *      Detail files are always known, and aren't
4558                  *      put into the socket event loop.
4559                  */
4560                 case RAD_LISTEN_DETAIL:
4561                         this->status = RAD_LISTEN_STATUS_KNOWN;
4562
4563 #ifndef WITH_DETAIL_THREAD
4564                         /*
4565                          *      Set up the first poll interval.
4566                          */
4567                         event_poll_detail(this);
4568                         return 1;
4569 #else
4570                         break;  /* add the FD to the list */
4571 #endif
4572 #endif  /* WITH_DETAIL */
4573
4574 #ifdef WITH_PROXY
4575                 /*
4576                  *      Add it to the list of sockets we can use.
4577                  *      Server sockets (i.e. auth/acct) are never
4578                  *      added to the packet list.
4579                  */
4580                 case RAD_LISTEN_PROXY:
4581 #ifdef WITH_TCP
4582                         rad_assert((sock->proto == IPPROTO_UDP) || (sock->home != NULL));
4583
4584                         /*
4585                          *      Add timers to outgoing child sockets, if necessary.
4586                          */
4587                         if (sock->proto == IPPROTO_TCP && sock->opened &&
4588                             (sock->home->limit.lifetime || sock->home->limit.idle_timeout)) {
4589                                 struct timeval when;
4590
4591                                 when.tv_sec = sock->opened + 1;
4592                                 when.tv_usec = 0;
4593
4594                                 ASSERT_MASTER;
4595                                 if (!fr_event_insert(el, tcp_socket_timer, this, &when,
4596                                                      &(sock->ev))) {
4597                                         rad_panic("Failed to insert event");
4598                                 }
4599                         }
4600 #endif
4601                         break;
4602 #endif  /* WITH_PROXY */
4603
4604                         /*
4605                          *      FIXME: put idle timers on command sockets.
4606                          */
4607
4608                 default:
4609 #ifdef WITH_TCP
4610                         /*
4611                          *      Add timers to incoming child sockets, if necessary.
4612                          */
4613                         if (sock->proto == IPPROTO_TCP && sock->opened &&
4614                             (sock->limit.lifetime || sock->limit.idle_timeout)) {
4615                                 struct timeval when;
4616
4617                                 when.tv_sec = sock->opened + 1;
4618                                 when.tv_usec = 0;
4619
4620                                 ASSERT_MASTER;
4621                                 if (!fr_event_insert(el, tcp_socket_timer, this, &when,
4622                                                      &(sock->ev))) {
4623                                         ERROR("Failed adding timer for socket: %s", fr_strerror());
4624                                         fr_exit(1);
4625                                 }
4626                         }
4627 #endif
4628                         break;
4629                 } /* switch over listener types */
4630
4631                 /*
4632                  *      All sockets: add the FD to the event handler.
4633                  */
4634                 if (!fr_event_fd_insert(el, 0, this->fd,
4635                                         event_socket_handler, this)) {
4636                         ERROR("Failed adding event handler for socket: %s", fr_strerror());
4637                         fr_exit(1);
4638                 }
4639
4640                 this->status = RAD_LISTEN_STATUS_KNOWN;
4641                 return 1;
4642         } /* end of INIT */
4643
4644 #ifdef WITH_TCP
4645         /*
4646          *      Stop using this socket, if at all possible.
4647          */
4648         if (this->status == RAD_LISTEN_STATUS_EOL) {
4649                 /*
4650                  *      Remove it from the list of live FD's.
4651                  */
4652                 fr_event_fd_delete(el, 0, this->fd);
4653
4654 #ifdef WITH_PROXY
4655                 /*
4656                  *      Proxy sockets get frozen, so that we don't use
4657                  *      them for new requests.  But we do keep them
4658                  *      open to listen for replies to requests we had
4659                  *      previously sent.
4660                  */
4661                 if (this->type == RAD_LISTEN_PROXY) {
4662                         PTHREAD_MUTEX_LOCK(&proxy_mutex);
4663                         if (!fr_packet_list_socket_freeze(proxy_list,
4664                                                           this->fd)) {
4665                                 ERROR("Fatal error freezing socket: %s", fr_strerror());
4666                                 fr_exit(1);
4667                         }
4668
4669                         fr_packet_list_walk(proxy_list, this, proxy_eol_cb);
4670                         PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
4671                 }
4672 #endif
4673
4674                 /*
4675                  *      Requests are still using the socket.  Wait for
4676                  *      them to finish.
4677                  */
4678                 if (this->count > 0) {
4679                         struct timeval when;
4680                         listen_socket_t *sock = this->data;
4681
4682                         /*
4683                          *      Try again to clean up the socket in 30
4684                          *      seconds.
4685                          */
4686                         gettimeofday(&when, NULL);
4687                         when.tv_sec += 30;
4688
4689                         ASSERT_MASTER;
4690                         if (!fr_event_insert(el,
4691                                              (fr_event_callback_t) event_new_fd,
4692                                              this, &when, &sock->ev)) {
4693                                 rad_panic("Failed to insert event");
4694                         }
4695
4696                         return 1;
4697                 }
4698
4699                 /*
4700                  *      No one is using the socket.  We can remove it now.
4701                  */
4702                 this->status = RAD_LISTEN_STATUS_REMOVE_NOW;
4703         } /* socket is at EOL */
4704 #endif
4705
4706         /*
4707          *      Nuke the socket.
4708          */
4709         if (this->status == RAD_LISTEN_STATUS_REMOVE_NOW) {
4710                 int devnull;
4711 #ifdef WITH_TCP
4712                 listen_socket_t *sock = this->data;
4713 #endif
4714                 struct timeval when;
4715
4716                 /*
4717                  *      Re-open the socket, pointing it to /dev/null.
4718                  *      This means that all writes proceed without
4719                  *      blocking, and all reads return "no data".
4720                  *
4721                  *      This leaves the socket active, so any child
4722                  *      threads won't go insane.  But it means that
4723                  *      they cannot send or receive any packets.
4724                  *
4725                  *      This is EXTRA work in the normal case, when
4726                  *      sockets are closed without error.  But it lets
4727                  *      us have one simple processing method for all
4728                  *      sockets.
4729                  */
4730                 devnull = open("/dev/null", O_RDWR);
4731                 if (devnull < 0) {
4732                         ERROR("FATAL failure opening /dev/null: %s",
4733                                fr_syserror(errno));
4734                         fr_exit(1);
4735                 }
4736                 if (dup2(devnull, this->fd) < 0) {
4737                         ERROR("FATAL failure closing socket: %s",
4738                                fr_syserror(errno));
4739                         fr_exit(1);
4740                 }
4741                 close(devnull);
4742
4743 #ifdef WITH_DETAIL
4744                 rad_assert(this->type != RAD_LISTEN_DETAIL);
4745 #endif
4746
4747 #ifdef WITH_TCP
4748 #ifdef WITH_PROXY
4749                 /*
4750                  *      The socket is dead.  Force all proxied packets
4751                  *      to stop using it.  And then remove it from the
4752                  *      list of outgoing sockets.
4753                  */
4754                 if (this->type == RAD_LISTEN_PROXY) {
4755                         home_server_t *home;
4756
4757                         home = sock->home;
4758                         if (!home || !home->limit.max_connections) {
4759                                 INFO(" ... shutting down socket %s", buffer);
4760                         } else {
4761                                 INFO(" ... shutting down socket %s (%u of %u)", buffer,
4762                                      home->limit.num_connections, home->limit.max_connections);
4763                         }
4764
4765                         PTHREAD_MUTEX_LOCK(&proxy_mutex);
4766                         fr_packet_list_walk(proxy_list, this, eol_proxy_listener);
4767
4768                         if (!fr_packet_list_socket_del(proxy_list, this->fd)) {
4769                                 ERROR("Fatal error removing socket %s: %s",
4770                                       buffer, fr_strerror());
4771                                 fr_exit(1);
4772                         }
4773                         PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
4774                 } else
4775 #endif
4776                 {
4777                         INFO(" ... shutting down socket %s", buffer);
4778
4779                         /*
4780                          *      EOL all requests using this socket.
4781                          */
4782                         rbtree_walk(pl, RBTREE_DELETE_ORDER, eol_listener, this);
4783                 }
4784
4785                 /*
4786                  *      No child threads, clean it up now.
4787                  */
4788                 if (!spawn_flag) {
4789                         ASSERT_MASTER;
4790                         if (sock->ev) fr_event_delete(el, &sock->ev);
4791                         listen_free(&this);
4792                         return 1;
4793                 }
4794
4795                 /*
4796                  *      Wait until all requests using this socket are done.
4797                  */
4798                 gettimeofday(&when, NULL);
4799                 when.tv_sec += 3;
4800
4801                 ASSERT_MASTER;
4802                 if (!fr_event_insert(el, listener_free_cb, this, &when,
4803                                      &(sock->ev))) {
4804                         rad_panic("Failed to insert event");
4805                 }
4806         }
4807 #endif  /* WITH_TCP */
4808
4809         return 1;
4810 }
4811
4812 /***********************************************************************
4813  *
4814  *      Signal handlers.
4815  *
4816  ***********************************************************************/
4817
4818 static void handle_signal_self(int flag)
4819 {
4820         ASSERT_MASTER;
4821
4822         if ((flag & (RADIUS_SIGNAL_SELF_EXIT | RADIUS_SIGNAL_SELF_TERM)) != 0) {
4823                 if ((flag & RADIUS_SIGNAL_SELF_EXIT) != 0) {
4824                         INFO("Signalled to exit");
4825                         fr_event_loop_exit(el, 1);
4826                 } else {
4827                         INFO("Signalled to terminate");
4828                         fr_event_loop_exit(el, 2);
4829                 }
4830
4831                 return;
4832         } /* else exit/term flags weren't set */
4833
4834         /*
4835          *      Tell the even loop to stop processing.
4836          */
4837         if ((flag & RADIUS_SIGNAL_SELF_HUP) != 0) {
4838                 time_t when;
4839                 static time_t last_hup = 0;
4840
4841                 when = time(NULL);
4842                 if ((int) (when - last_hup) < 5) {
4843                         INFO("Ignoring HUP (less than 5s since last one)");
4844                         return;
4845                 }
4846
4847                 INFO("Received HUP signal");
4848
4849                 last_hup = when;
4850
4851                 exec_trigger(NULL, NULL, "server.signal.hup", true);
4852                 fr_event_loop_exit(el, 0x80);
4853         }
4854
4855 #ifdef WITH_DETAIL
4856 #ifndef WITH_DETAIL_THREAD
4857         if ((flag & RADIUS_SIGNAL_SELF_DETAIL) != 0) {
4858                 rad_listen_t *this;
4859
4860                 /*
4861                  *      FIXME: O(N) loops suck.
4862                  */
4863                 for (this = main_config.listen;
4864                      this != NULL;
4865                      this = this->next) {
4866                         if (this->type != RAD_LISTEN_DETAIL) continue;
4867
4868                         /*
4869                          *      This one didn't send the signal, skip
4870                          *      it.
4871                          */
4872                         if (!this->decode(this, NULL)) continue;
4873
4874                         /*
4875                          *      Go service the interrupt.
4876                          */
4877                         event_poll_detail(this);
4878                 }
4879         }
4880 #endif
4881 #endif
4882
4883 #ifdef WITH_TCP
4884 #ifdef WITH_PROXY
4885 #ifdef HAVE_PTHREAD_H
4886         /*
4887          *      There are new listeners in the list.  Run
4888          *      event_new_fd() on them.
4889          */
4890         if ((flag & RADIUS_SIGNAL_SELF_NEW_FD) != 0) {
4891                 rad_listen_t *this, *next;
4892
4893                 FD_MUTEX_LOCK(&fd_mutex);
4894
4895                 /*
4896                  *      FIXME: unlock the mutex before calling
4897                  *      event_new_fd()?
4898                  */
4899                 for (this = new_listeners; this != NULL; this = next) {
4900                         next = this->next;
4901                         this->next = NULL;
4902
4903                         event_new_fd(this);
4904                 }
4905
4906                 new_listeners = NULL;
4907                 FD_MUTEX_UNLOCK(&fd_mutex);
4908         }
4909 #endif  /* HAVE_PTHREAD_H */
4910 #endif  /* WITH_PROXY */
4911 #endif  /* WITH_TCP */
4912 }
4913
4914 #ifndef HAVE_PTHREAD_H
4915 void radius_signal_self(int flag)
4916 {
4917         return handle_signal_self(flag);
4918 }
4919
4920 #else
4921 static int self_pipe[2] = { -1, -1 };
4922
4923 /*
4924  *      Inform ourselves that we received a signal.
4925  */
4926 void radius_signal_self(int flag)
4927 {
4928         ssize_t rcode;
4929         uint8_t buffer[16];
4930
4931         /*
4932          *      The read MUST be non-blocking for this to work.
4933          */
4934         rcode = read(self_pipe[0], buffer, sizeof(buffer));
4935         if (rcode > 0) {
4936                 ssize_t i;
4937
4938                 for (i = 0; i < rcode; i++) {
4939                         buffer[0] |= buffer[i];
4940                 }
4941         } else {
4942                 buffer[0] = 0;
4943         }
4944
4945         buffer[0] |= flag;
4946
4947         if (write(self_pipe[1], buffer, 1) < 0) fr_exit(0);
4948 }
4949
4950
4951 static void event_signal_handler(UNUSED fr_event_list_t *xel,
4952                                  UNUSED int fd, UNUSED void *ctx)
4953 {
4954         ssize_t i, rcode;
4955         uint8_t buffer[32];
4956
4957         rcode = read(self_pipe[0], buffer, sizeof(buffer));
4958         if (rcode <= 0) return;
4959
4960         /*
4961          *      Merge pending signals.
4962          */
4963         for (i = 0; i < rcode; i++) {
4964                 buffer[0] |= buffer[i];
4965         }
4966
4967         handle_signal_self(buffer[0]);
4968 }
4969 #endif  /* HAVE_PTHREAD_H */
4970
4971 /***********************************************************************
4972  *
4973  *      Bootstrapping code.
4974  *
4975  ***********************************************************************/
4976
4977 /*
4978  *      Externally-visibly functions.
4979  */
4980 int radius_event_init(TALLOC_CTX *ctx) {
4981         el = fr_event_list_create(ctx, event_status);
4982         if (!el) return 0;
4983
4984         return 1;
4985 }
4986
4987 static int packet_entry_cmp(void const *one, void const *two)
4988 {
4989         RADIUS_PACKET const * const *a = one;
4990         RADIUS_PACKET const * const *b = two;
4991
4992         return fr_packet_cmp(*a, *b);
4993 }
4994
4995
4996 int radius_event_start(CONF_SECTION *cs, bool have_children)
4997 {
4998         rad_listen_t *head = NULL;
4999
5000         if (fr_start_time != (time_t)-1) return 0;
5001
5002         time(&fr_start_time);
5003
5004         if (!check_config) {
5005                 /*
5006                  *  radius_event_init() must be called first
5007                  */
5008                 rad_assert(el);
5009
5010                 pl = rbtree_create(NULL, packet_entry_cmp, NULL, 0);
5011                 if (!pl) return 0;      /* leak el */
5012         }
5013
5014         request_num_counter = 0;
5015
5016 #ifdef WITH_PROXY
5017         if (main_config.proxy_requests) {
5018                 /*
5019                  *      Create the tree for managing proxied requests and
5020                  *      responses.
5021                  */
5022                 proxy_list = fr_packet_list_create(1);
5023                 if (!proxy_list) return 0;
5024
5025 #ifdef HAVE_PTHREAD_H
5026                 if (pthread_mutex_init(&proxy_mutex, NULL) != 0) {
5027                         ERROR("FATAL: Failed to initialize proxy mutex: %s",
5028                                fr_syserror(errno));
5029                         fr_exit(1);
5030                 }
5031 #endif
5032
5033                 /*
5034                  *      The "init_delay" is set to "response_window".
5035                  *      Reset it to half of "response_window" in order
5036                  *      to give the event loop enough time to service
5037                  *      the event before hitting "response_window".
5038                  */
5039                 main_config.init_delay.tv_usec += (main_config.init_delay.tv_sec & 0x01) * USEC;
5040                 main_config.init_delay.tv_usec >>= 1;
5041                 main_config.init_delay.tv_sec >>= 1;
5042
5043                 proxy_ctx = talloc_init("proxy");
5044         }
5045 #endif
5046
5047         /*
5048          *      Move all of the thread calls to this file?
5049          *
5050          *      It may be best for the mutexes to be in this file...
5051          */
5052         spawn_flag = have_children;
5053
5054 #ifdef HAVE_PTHREAD_H
5055         NO_SUCH_CHILD_PID = pthread_self(); /* not a child thread */
5056
5057         /*
5058          *      Initialize the threads ONLY if we're spawning, AND
5059          *      we're running normally.
5060          */
5061         if (have_children && !check_config &&
5062             (thread_pool_init(cs, &spawn_flag) < 0)) {
5063                 fr_exit(1);
5064         }
5065 #endif
5066
5067         if (check_config) {
5068                 DEBUG("%s: #### Skipping IP addresses and Ports ####",
5069                        main_config.name);
5070                 if (listen_init(cs, &head, spawn_flag) < 0) {
5071                         fflush(NULL);
5072                         fr_exit(1);
5073                 }
5074                 return 1;
5075         }
5076
5077 #ifdef HAVE_PTHREAD_H
5078         /*
5079          *      Child threads need a pipe to signal us, as do the
5080          *      signal handlers.
5081          */
5082         if (pipe(self_pipe) < 0) {
5083                 ERROR("Error opening internal pipe: %s", fr_syserror(errno));
5084                 fr_exit(1);
5085         }
5086         if ((fcntl(self_pipe[0], F_SETFL, O_NONBLOCK) < 0) ||
5087             (fcntl(self_pipe[0], F_SETFD, FD_CLOEXEC) < 0)) {
5088                 ERROR("Error setting internal flags: %s", fr_syserror(errno));
5089                 fr_exit(1);
5090         }
5091         if ((fcntl(self_pipe[1], F_SETFL, O_NONBLOCK) < 0) ||
5092             (fcntl(self_pipe[1], F_SETFD, FD_CLOEXEC) < 0)) {
5093                 ERROR("Error setting internal flags: %s", fr_syserror(errno));
5094                 fr_exit(1);
5095         }
5096         DEBUG4("Created signal pipe.  Read end FD %i, write end FD %i", self_pipe[0], self_pipe[1]);
5097
5098         if (!fr_event_fd_insert(el, 0, self_pipe[0], event_signal_handler, el)) {
5099                 ERROR("Failed creating signal pipe handler: %s", fr_strerror());
5100                 fr_exit(1);
5101         }
5102 #endif
5103
5104        DEBUG("%s: #### Opening IP addresses and Ports ####", main_config.name);
5105
5106        /*
5107         *       The server temporarily switches to an unprivileged
5108         *       user very early in the bootstrapping process.
5109         *       However, some sockets MAY require privileged access
5110         *       (bind to device, or to port < 1024, or to raw
5111         *       sockets).  Those sockets need to call suid up/down
5112         *       themselves around the functions that need a privileged
5113         *       uid.
5114         */
5115        if (listen_init(cs, &head, spawn_flag) < 0) {
5116                 fr_exit_now(1);
5117         }
5118
5119         main_config.listen = head;
5120
5121         /*
5122          *      At this point, no one has any business *ever* going
5123          *      back to root uid.
5124          */
5125         rad_suid_down_permanent();
5126
5127         return 1;
5128 }
5129
5130
5131 #ifdef WITH_PROXY
5132 static int proxy_delete_cb(UNUSED void *ctx, void *data)
5133 {
5134         REQUEST *request = fr_packet2myptr(REQUEST, proxy, data);
5135
5136         VERIFY_REQUEST(request);
5137
5138         request->master_state = REQUEST_STOP_PROCESSING;
5139
5140 #ifdef HAVE_PTHREAD_H
5141         if (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0) return 0;
5142 #endif
5143
5144         /*
5145          *      If it's queued we can't delete it from the queue.
5146          *
5147          *      Otherwise, it's OK to delete it.  Even RUNNING, because
5148          *      that will get caught by the check above.
5149          */
5150         if (request->child_state == REQUEST_QUEUED) return 0;
5151
5152         request->in_proxy_hash = false;
5153
5154         if (!request->in_request_hash) {
5155                 request_done(request, FR_ACTION_DONE);
5156         }
5157
5158         /*
5159          *      Delete it from the list.
5160          */
5161         return 2;
5162 }
5163 #endif
5164
5165
5166 static int request_delete_cb(UNUSED void *ctx, void *data)
5167 {
5168         REQUEST *request = fr_packet2myptr(REQUEST, packet, data);
5169
5170         VERIFY_REQUEST(request);
5171
5172         request->master_state = REQUEST_STOP_PROCESSING;
5173
5174         /*
5175          *      Not done, or the child thread is still processing it.
5176          */
5177         if (request->child_state < REQUEST_RESPONSE_DELAY) return 0; /* continue */
5178
5179 #ifdef HAVE_PTHREAD_H
5180         if (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0) return 0;
5181 #endif
5182
5183 #ifdef WITH_PROXY
5184         rad_assert(request->in_proxy_hash == false);
5185 #endif
5186
5187         request->in_request_hash = false;
5188         ASSERT_MASTER;
5189         if (request->ev) fr_event_delete(el, &request->ev);
5190
5191         if (main_config.memory_report) {
5192                 RDEBUG2("Cleaning up request packet ID %u with timestamp +%d",
5193                         request->packet->id,
5194                         (unsigned int) (request->timestamp - fr_start_time));
5195         }
5196
5197 #ifdef WITH_COA
5198         if (request->coa) {
5199                 rad_assert(!request->coa->in_proxy_hash);
5200         }
5201 #endif
5202
5203         request_free(request);
5204
5205         /*
5206          *      Delete it from the list, and continue;
5207          */
5208         return 2;
5209 }
5210
5211
5212 void radius_event_free(void)
5213 {
5214         ASSERT_MASTER;
5215
5216 #ifdef WITH_PROXY
5217         /*
5218          *      There are requests in the proxy hash that aren't
5219          *      referenced from anywhere else.  Remove them first.
5220          */
5221         if (proxy_list) {
5222                 fr_packet_list_walk(proxy_list, NULL, proxy_delete_cb);
5223         }
5224 #endif
5225
5226         rbtree_walk(pl, RBTREE_DELETE_ORDER,  request_delete_cb, NULL);
5227
5228         if (spawn_flag) {
5229                 /*
5230                  *      Now that all requests have been marked "please stop",
5231                  *      ensure that all of the threads have exited.
5232                  */
5233 #ifdef HAVE_PTHREAD_H
5234                 thread_pool_stop();
5235 #endif
5236
5237                 /*
5238                  *      Walk the lists again, ensuring that all
5239                  *      requests are done.
5240                  */
5241                 if (main_config.memory_report) {
5242                         int num;
5243
5244 #ifdef WITH_PROXY
5245                         if (proxy_list) {
5246                                 fr_packet_list_walk(proxy_list, NULL, proxy_delete_cb);
5247                                 num = fr_packet_list_num_elements(proxy_list);
5248                                 if (num > 0) {
5249                                         ERROR("Proxy list has %d requests still in it.", num);
5250                                 }
5251                         }
5252 #endif
5253
5254                         rbtree_walk(pl, RBTREE_DELETE_ORDER, request_delete_cb, NULL);
5255                         num = rbtree_num_elements(pl);
5256                         if (num > 0) {
5257                                 ERROR("Request list has %d requests still in it.", num);
5258                         }
5259                 }
5260         }
5261
5262         rbtree_free(pl);
5263         pl = NULL;
5264
5265 #ifdef WITH_PROXY
5266         fr_packet_list_free(proxy_list);
5267         proxy_list = NULL;
5268
5269         if (proxy_ctx) talloc_free(proxy_ctx);
5270 #endif
5271
5272         TALLOC_FREE(el);
5273
5274         if (debug_condition) talloc_free(debug_condition);
5275 }
5276
5277 int radius_event_process(void)
5278 {
5279         if (!el) return 0;
5280
5281         return fr_event_loop(el);
5282 }