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