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