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