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