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