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