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