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