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