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