78bb22053e97cb49dfd8784b5a4c648256876e8c
[freeradius.git] / src / main / event.c
1 /*
2  * event.c      Server event handling
3  *
4  * Version:     $Id$
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2007  The FreeRADIUS server project
21  * Copyright 2007  Alan DeKok <aland@deployingradius.com>
22  */
23
24 #include <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29 #include <freeradius-devel/event.h>
30 #include <freeradius-devel/detail.h>
31
32 #include <freeradius-devel/rad_assert.h>
33
34 #include <signal.h>
35 #include <fcntl.h>
36
37 #ifdef HAVE_SYS_WAIT_H
38 #       include <sys/wait.h>
39 #endif
40
41 #define USEC (1000000)
42
43 extern pid_t radius_pid;
44 extern int dont_fork;
45 extern int check_config;
46 extern char *debug_condition;
47
48 /*
49  *      Ridiculous amounts of local state.
50  */
51 static fr_event_list_t  *el = NULL;
52 static fr_packet_list_t *pl = NULL;
53 static int                      request_num_counter = 0;
54 static struct timeval           now;
55 time_t                          fr_start_time;
56 static int                      have_children;
57 static int                      just_started = TRUE;
58
59 #ifndef __MINGW32__
60 #ifdef HAVE_PTHREAD_H
61 #define WITH_SELF_PIPE (1)
62 #endif
63 #endif
64
65 #ifdef WITH_SELF_PIPE
66 static int self_pipe[2];
67 #endif
68
69 #ifdef HAVE_PTHREAD_H
70 #ifdef WITH_PROXY
71 static pthread_mutex_t  proxy_mutex;
72 static rad_listen_t *proxy_listener_list = NULL;
73 static int proxy_no_new_sockets = FALSE;
74 #endif
75
76 #define PTHREAD_MUTEX_LOCK if (have_children) pthread_mutex_lock
77 #define PTHREAD_MUTEX_UNLOCK if (have_children) pthread_mutex_unlock
78
79 static pthread_t NO_SUCH_CHILD_PID;
80 #else
81 /*
82  *      This is easier than ifdef's throughout the code.
83  */
84 #define PTHREAD_MUTEX_LOCK(_x)
85 #define PTHREAD_MUTEX_UNLOCK(_x)
86 #endif
87
88 /*
89  *      We need mutexes around the event FD list *only* in certain
90  *      cases.
91  */
92 #if defined (HAVE_PTHREAD_H) && (defined(WITH_PROXY) || defined(WITH_TCP))
93 static pthread_mutex_t  fd_mutex;
94 #define FD_MUTEX_LOCK if (have_children) pthread_mutex_lock
95 #define FD_MUTEX_UNLOCK if (have_children) pthread_mutex_unlock
96 #else
97 /*
98  *      This is easier than ifdef's throughout the code.
99  */
100 #define FD_MUTEX_LOCK(_x)
101 #define FD_MUTEX_UNLOCK(_x)
102 #endif
103
104
105 #define INSERT_EVENT(_function, _ctx) if (!fr_event_insert(el, _function, _ctx, &((_ctx)->when), &((_ctx)->ev))) { _rad_panic(__FILE__, __LINE__, "Failed to insert event"); }
106
107 #ifdef WITH_PROXY
108 static fr_packet_list_t *proxy_list = NULL;
109 static void remove_from_proxy_hash(REQUEST *request);
110
111 static void check_for_zombie_home_server(REQUEST *request);
112 #else
113 #define remove_from_proxy_hash(foo)
114 #endif
115
116 static void request_post_handler(REQUEST *request);
117 static void wait_a_bit(void *ctx);
118 static void event_socket_handler(fr_event_list_t *xel, UNUSED int fd, void *ctx);
119 #ifdef WITH_DETAIL
120 static void event_poll_detail(void *ctx);
121 #endif
122
123 static void NEVER_RETURNS _rad_panic(const char *file, unsigned int line,
124                                     const char *msg)
125 {
126         radlog(L_ERR, "[%s:%d] %s", file, line, msg);
127         _exit(1);
128 }
129
130 #define rad_panic(x) _rad_panic(__FILE__, __LINE__, x)
131
132
133 static void tv_add(struct timeval *tv, int usec_delay)
134 {
135         if (usec_delay > USEC) {
136                 tv->tv_sec += usec_delay / USEC;
137                 usec_delay %= USEC;
138         }
139         tv->tv_usec += usec_delay;
140
141         if (tv->tv_usec > USEC) {
142                 tv->tv_sec += tv->tv_usec / USEC;
143                 tv->tv_usec %= USEC;
144         }
145 }
146
147 static void remove_from_request_hash(REQUEST *request)
148 {
149         if (!request->in_request_hash) return;
150
151         fr_packet_list_yank(pl, request->packet);
152         request->in_request_hash = FALSE;
153
154         /*
155          *      FIXME: Move this to a "statistics" thread?
156          *      Or (short term) add a mutex lock around it.
157          */
158         request_stats_final(request);
159
160 #ifdef WITH_TCP
161         request->listener->count--;
162 #endif
163 }
164
165 static void ev_request_free(REQUEST **prequest)
166 {
167         REQUEST *request;
168         
169         if (!prequest || !*prequest) return;
170
171         request = *prequest;
172
173 #ifdef WITH_COA
174         if (request->coa) {
175                 /*
176                  *      Divorce the child from the parent first,
177                  *      then clean up the child.
178                  */
179                 request->coa->parent = NULL;
180                 ev_request_free(&request->coa);
181         }
182
183         /*
184          *      Divorce the parent from the child, and leave the
185          *      parent still alive.
186          */
187         if (request->parent && (request->parent->coa == request)) {
188                 request->parent->coa = NULL;
189         }
190 #endif
191
192         if (request->ev) fr_event_delete(el, &request->ev);
193 #ifdef WITH_PROXY
194         if (request->in_proxy_hash) remove_from_proxy_hash(request);
195 #endif
196         if (request->in_request_hash) remove_from_request_hash(request);
197
198         request_free(prequest);
199 }
200
201 #ifdef WITH_PROXY
202 static REQUEST *lookup_in_proxy_hash(RADIUS_PACKET *reply)
203 {
204         RADIUS_PACKET **proxy_p;
205         REQUEST *request;
206
207         PTHREAD_MUTEX_LOCK(&proxy_mutex);
208         proxy_p = fr_packet_list_find_byreply(proxy_list, reply);
209
210         if (!proxy_p) {
211                 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
212                 return NULL;
213         }
214
215         request = fr_packet2myptr(REQUEST, proxy, proxy_p);
216         request->num_proxied_responses++; /* needs to be protected by lock */
217
218         PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
219
220         return request;
221 }
222
223
224 static void remove_from_proxy_hash(REQUEST *request)
225 {
226         /*
227          *      Check this without grabbing the mutex because it's a
228          *      lot faster that way.
229          */
230         if (!request->in_proxy_hash) return;
231
232         /*
233          *      The "not in hash" flag is definitive.  However, if the
234          *      flag says that it IS in the hash, there might still be
235          *      a race condition where it isn't.
236          */
237         PTHREAD_MUTEX_LOCK(&proxy_mutex);
238
239         if (!request->in_proxy_hash) {
240                 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
241                 return;
242         }
243
244         fr_packet_list_yank(proxy_list, request->proxy);
245         fr_packet_list_id_free(proxy_list, request->proxy);
246
247         /*
248          *      On the FIRST reply, decrement the count of outstanding
249          *      requests.  Note that this is NOT the count of sent
250          *      packets, but whether or not the home server has
251          *      responded at all.
252          */
253         if (!request->proxy_reply &&
254             request->home_server &&
255             request->home_server->currently_outstanding) {
256                 request->home_server->currently_outstanding--;
257         }
258
259 #ifdef WITH_TCP
260         request->proxy_listener->count--;
261         request->proxy_listener = NULL;
262 #endif
263
264         /*
265          *      Got from YES in hash, to NO, not in hash while we hold
266          *      the mutex.  This guarantees that when another thread
267          *      grabs the mutex, the "not in hash" flag is correct.
268          */
269         request->in_proxy_hash = FALSE;
270
271         PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
272 }
273 #endif  /* WITH_PROXY */
274
275 #ifdef WITH_TCP
276 static int remove_all_requests(void *ctx, void *data)
277 {
278         rad_listen_t *this = ctx;
279         RADIUS_PACKET **packet_p = data;
280         REQUEST *request;
281         
282         request = fr_packet2myptr(REQUEST, packet, packet_p);
283         if (request->packet->sockfd != this->fd) return 0;
284
285         switch (request->child_state) {
286         case REQUEST_RUNNING:
287                 rad_assert(request->ev != NULL); /* or it's lost forever */
288         case REQUEST_QUEUED:
289                 request->master_state = REQUEST_STOP_PROCESSING;
290                 return 0;
291
292                 /*
293                  *      Waiting for a reply.  There's no point in
294                  *      doing anything else.  We remove it from the
295                  *      request hash so that we can close the upstream
296                  *      socket.
297                  */
298         case REQUEST_PROXIED:
299                 remove_from_request_hash(request);
300                 request->child_state = REQUEST_DONE;
301                 return 0;
302
303         case REQUEST_REJECT_DELAY:
304         case REQUEST_CLEANUP_DELAY:
305         case REQUEST_DONE:
306                 ev_request_free(&request);
307                 break;
308         }
309
310         return 0;
311 }
312
313 #ifdef WITH_PROXY
314 static int remove_all_proxied_requests(void *ctx, void *data)
315 {
316         rad_listen_t *this = ctx;
317         RADIUS_PACKET **proxy_p = data;
318         REQUEST *request;
319         
320         request = fr_packet2myptr(REQUEST, proxy, proxy_p);
321         if (request->proxy->sockfd != this->fd) return 0;
322
323         switch (request->child_state) {
324         case REQUEST_RUNNING:
325                 rad_assert(request->ev != NULL); /* or it's lost forever */
326         case REQUEST_QUEUED:
327                 request->master_state = REQUEST_STOP_PROCESSING;
328                 return 0;
329
330                 /*
331                  *      Eventually we will discover that there is no
332                  *      response to the proxied request.
333                  */
334         case REQUEST_PROXIED:
335                 break;
336
337                 /*
338                  *      Keep it in the cache for duplicate detection.
339                  */
340         case REQUEST_REJECT_DELAY:
341         case REQUEST_CLEANUP_DELAY:
342         case REQUEST_DONE:
343                 break;
344         }
345
346         remove_from_proxy_hash(request);
347         return 0;
348 }
349 #endif  /* WITH_PROXY */
350 #endif  /* WITH_TCP */
351
352
353 #ifdef WITH_PROXY
354 static int insert_into_proxy_hash(REQUEST *request)
355 {
356         char buf[128];
357         int rcode, tries;
358         void *proxy_listener;
359
360         rad_assert(request->proxy != NULL);
361         rad_assert(proxy_list != NULL);
362
363         tries = 1;
364 retry:
365         PTHREAD_MUTEX_LOCK(&proxy_mutex);
366         rcode = fr_packet_list_id_alloc(proxy_list,
367                                         request->home_server->proto,
368                                         request->proxy, &proxy_listener);
369         request->num_proxied_requests = 1;
370         request->num_proxied_responses = 0;
371         PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
372         
373         if (!rcode) {
374                 if (proxy_no_new_sockets) return 0;
375
376                 /*
377                  *      Also locks the proxy mutex, so we have to call
378                  *      it with the mutex unlocked.  Some systems
379                  *      don't support recursive mutexes.
380                  */
381                 if (!proxy_new_listener(request->home_server, 0)) {
382                         radlog(L_ERR, "Failed to create a new socket for proxying requests.");
383                         return 0;
384                 }
385                 request->proxy->src_port = 0; /* Use any new socket */
386
387                 tries++;
388                 if (tries > 2) {
389                         RDEBUG2("ERROR: Failed allocating Id for new socket when proxying requests.");
390                         return 0;
391                 }
392                 
393                 goto retry;
394         }
395
396         request->proxy_listener = proxy_listener;
397
398         PTHREAD_MUTEX_LOCK(&proxy_mutex);
399         if (!fr_packet_list_insert(proxy_list, &request->proxy)) {
400                 fr_packet_list_id_free(proxy_list, request->proxy);
401                 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
402                 radlog(L_PROXY, "Failed to insert entry into proxy list.");
403                 return 0;
404         }
405
406         request->in_proxy_hash = TRUE;
407
408         /*
409          *      Keep track of maximum outstanding requests to a
410          *      particular home server.  'max_outstanding' is
411          *      enforced in home_server_ldb(), in realms.c.
412          */
413         if (request->home_server) {
414                 request->home_server->currently_outstanding++;
415         }
416
417 #ifdef WITH_TCP
418         request->proxy_listener->count++;
419 #endif
420
421         PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
422
423         RDEBUG3(" proxy: allocating destination %s port %d - Id %d",
424                inet_ntop(request->proxy->dst_ipaddr.af,
425                          &request->proxy->dst_ipaddr.ipaddr, buf, sizeof(buf)),
426                request->proxy->dst_port,
427                request->proxy->id);
428
429         return 1;
430 }
431
432
433 /*
434  *      Called as BOTH an event, and in-line from other functions.
435  */
436 static void wait_for_proxy_id_to_expire(void *ctx)
437 {
438         REQUEST *request = ctx;
439
440         rad_assert(request->magic == REQUEST_MAGIC);
441         rad_assert(request->proxy != NULL);
442
443         fr_event_now(el, &now);
444         request->when = request->proxy_when;
445
446 #ifdef WITH_COA
447         if (((request->proxy->code == PW_COA_REQUEST) ||
448              (request->proxy->code == PW_DISCONNECT_REQUEST)) &&
449             (request->packet->code != request->proxy->code)) {
450                 request->when.tv_sec += request->home_server->coa_mrd;
451         } else
452 #endif
453         request->when.tv_sec += request->home_server->response_window;
454
455         if ((request->num_proxied_requests == request->num_proxied_responses) ||
456 #ifdef WITH_TCP
457             (request->home_server->proto == IPPROTO_TCP) ||
458 #endif
459             timercmp(&now, &request->when, >)) {
460                 if (request->packet) {
461                         RDEBUG2("Cleaning up request packet ID %d with timestamp +%d",
462                                request->packet->id,
463                                (unsigned int) (request->timestamp - fr_start_time));
464                 } else {
465                         RDEBUG2("Cleaning up request with timestamp +%d",
466                                (unsigned int) (request->timestamp - fr_start_time));
467                 }
468
469                 ev_request_free(&request);
470                 return;
471         }
472
473         INSERT_EVENT(wait_for_proxy_id_to_expire, request);
474 }
475 #endif
476
477 #ifdef HAVE_PTHREAD_H
478 static void wait_for_child_to_die(void *ctx)
479 {
480         REQUEST *request = ctx;
481
482         rad_assert(request->magic == REQUEST_MAGIC);
483         remove_from_request_hash(request);
484
485         /*
486          *      If it's still queued (waiting for a thread to pick it
487          *      up) OR, it's running AND there's still a child thread
488          *      handling it, THEN delay some more.
489          */
490         if ((request->child_state == REQUEST_QUEUED) ||
491             ((request->child_state == REQUEST_RUNNING) &&
492              (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0))) {
493
494                 /*
495                  *      Cap delay at max_request_time
496                  */
497                 if (request->delay < (USEC * request->root->max_request_time)) {
498                         request->delay += (request->delay >> 1);
499                         radlog_request(L_INFO, 0, request, "WARNING: Child is hung in component %s module %s.",
500                                request->component, request->module);
501                 } else {
502                         request->delay = USEC * request->root->max_request_time;
503                         RDEBUG2("WARNING: Child is hung after \"max_request_time\" for request %u",
504                                 request->number);
505                 }
506                 tv_add(&request->when, request->delay);
507
508                 INSERT_EVENT(wait_for_child_to_die, request);
509                 return;
510         }
511
512         RDEBUG2("Child is finally responsive");
513
514 #ifdef WITH_PROXY
515         if (request->proxy) {
516                 wait_for_proxy_id_to_expire(request);
517                 return;
518         }
519 #endif
520
521         ev_request_free(&request);
522 }
523 #endif
524
525 static void cleanup_delay(void *ctx)
526 {
527         REQUEST *request = ctx;
528
529         rad_assert(request->magic == REQUEST_MAGIC);
530         rad_assert((request->child_state == REQUEST_CLEANUP_DELAY) ||
531                    (request->child_state == REQUEST_DONE));
532
533         remove_from_request_hash(request);
534
535 #ifdef WITH_PROXY
536         if (request->proxy && request->in_proxy_hash) {
537                 wait_for_proxy_id_to_expire(request);
538                 return;
539         }
540 #endif
541
542         RDEBUG2("Cleaning up request packet ID %d with timestamp +%d",
543                 request->packet->id,
544                (unsigned int) (request->timestamp - fr_start_time));
545
546         ev_request_free(&request);
547 }
548
549
550 /*
551  *      In daemon mode, AND this request has debug flags set.
552  */
553 #define DEBUG_PACKET if (!debug_flag && request->options && request->radlog) debug_packet
554
555 static void debug_packet(REQUEST *request, RADIUS_PACKET *packet, int direction)
556 {
557         VALUE_PAIR *vp;
558         char buffer[1024];
559         const char *received, *from;
560         const fr_ipaddr_t *ip;
561         int port;
562
563         if (!packet) return;
564
565         rad_assert(request->radlog != NULL);
566
567         if (direction == 0) {
568                 received = "Received";
569                 from = "from";  /* what else? */
570                 ip = &packet->src_ipaddr;
571                 port = packet->src_port;
572
573         } else {
574                 received = "Sending";
575                 from = "to";    /* hah! */
576                 ip = &packet->dst_ipaddr;
577                 port = packet->dst_port;
578         }
579         
580         /*
581          *      Client-specific debugging re-prints the input
582          *      packet into the client log.
583          *
584          *      This really belongs in a utility library
585          */
586         if ((packet->code > 0) && (packet->code < FR_MAX_PACKET_CODE)) {
587                 RDEBUG("%s %s packet %s host %s port %d, id=%d, length=%d",
588                        received, fr_packet_codes[packet->code], from,
589                        inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
590                        port, packet->id, packet->data_len);
591         } else {
592                 RDEBUG("%s packet %s host %s port %d code=%d, id=%d, length=%d",
593                        received, from,
594                        inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
595                        port,
596                        packet->code, packet->id, packet->data_len);
597         }
598
599         for (vp = packet->vps; vp != NULL; vp = vp->next) {
600                 vp_prints(buffer, sizeof(buffer), vp);
601                 request->radlog(L_DBG, 0, request, "\t%s", buffer);
602         }
603 }
604
605 static void reject_delay(void *ctx)
606 {
607         REQUEST *request = ctx;
608
609         rad_assert(request->magic == REQUEST_MAGIC);
610         rad_assert(request->child_state == REQUEST_REJECT_DELAY);
611
612         RDEBUG2("Sending delayed reject");
613
614         DEBUG_PACKET(request, request->reply, 1);
615
616         request->listener->send(request->listener, request);
617
618         request->when.tv_sec += request->root->cleanup_delay;
619         request->child_state = REQUEST_CLEANUP_DELAY;
620
621         INSERT_EVENT(cleanup_delay, request);
622 }
623
624
625 #ifdef WITH_PROXY
626 void revive_home_server(void *ctx)
627 {
628         home_server *home = ctx;
629         char buffer[128];
630
631 #ifdef WITH_TCP
632         rad_assert(home->proto != IPPROTO_TCP);
633 #endif
634
635         home->state = HOME_STATE_ALIVE;
636         home->currently_outstanding = 0;
637         home->revive_time = now;
638
639         /*
640          *      Delete any outstanding events.
641          */
642         if (home->ev) fr_event_delete(el, &home->ev);
643
644         radlog(L_PROXY, "Marking home server %s port %d alive again... we have no idea if it really is alive or not.",
645                inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
646                          buffer, sizeof(buffer)),
647                home->port);
648
649 }
650
651
652 static void no_response_to_ping(void *ctx)
653 {
654         REQUEST *request = ctx;
655         home_server *home;
656         char buffer[128];
657
658         rad_assert(request->home_server != NULL);
659
660         home = request->home_server;
661 #ifdef WITH_TCP
662         rad_assert(home->proto != IPPROTO_TCP);
663 #endif
664
665         home->num_received_pings = 0;
666
667         radlog(L_ERR, "No response to status check %d for home server %s port %d",
668                request->number,
669                inet_ntop(request->proxy->dst_ipaddr.af,
670                          &request->proxy->dst_ipaddr.ipaddr,
671                          buffer, sizeof(buffer)),
672                request->proxy->dst_port);
673
674         check_for_zombie_home_server(request);
675
676         wait_for_proxy_id_to_expire(request);
677 }
678
679
680 /*
681  *      Note that we don't care what the value of the code field is.
682  *      If the response has a valid (src ip/port, dst ip/port), id,
683  *      and correctly signed Message-Authenticator, that's good
684  *      enough.
685  */
686 static void received_response_to_ping(REQUEST *request)
687 {
688         home_server *home;
689         char buffer[128];
690
691         rad_assert(request->home_server != NULL);
692
693         home = request->home_server;
694 #ifdef WITH_TCP
695         rad_assert(home->proto != IPPROTO_TCP);
696 #endif
697
698         home->num_received_pings++;
699
700         radlog(L_PROXY, "Received response to status check %d (%d in current sequence)",
701                request->number, home->num_received_pings);
702
703         /*
704          *      Remove the request from any hashes
705          */
706         fr_event_delete(el, &request->ev);
707         remove_from_proxy_hash(request);
708         rad_assert(request->in_request_hash == FALSE);
709
710         /*
711          *      The control socket may have marked the home server as
712          *      alive.  OR, it may have suddenly started responding to
713          *      requests again.  If so, don't re-do the "make alive"
714          *      work.
715          */
716         if (home->state == HOME_STATE_ALIVE) return;
717
718         /*
719          *      We haven't received enough ping responses to mark it
720          *      "alive".  Wait a bit.
721          */
722         if (home->num_received_pings < home->num_pings_to_alive) {
723                 return;
724         }
725
726         home->state = HOME_STATE_ALIVE;
727         home->currently_outstanding = 0;
728         home->revive_time = now;
729
730         if (!fr_event_delete(el, &home->ev)) {
731                 RDEBUG2("Hmm... no event for home server.  Oh well.");
732         }
733
734         radlog(L_PROXY, "Marking home server %s port %d alive",
735                inet_ntop(request->proxy->dst_ipaddr.af,
736                          &request->proxy->dst_ipaddr.ipaddr,
737                          buffer, sizeof(buffer)),
738                request->proxy->dst_port);
739 }
740
741
742 /*
743  *      Called from start of zombie period, OR after control socket
744  *      marks the home server dead.
745  */
746 static void ping_home_server(void *ctx)
747 {
748         uint32_t jitter;
749         home_server *home = ctx;
750         REQUEST *request;
751         VALUE_PAIR *vp;
752
753 #ifdef WITH_TCP
754         rad_assert(home->proto != IPPROTO_TCP);
755 #endif
756
757         if ((home->state == HOME_STATE_ALIVE) ||
758             (home->ping_check == HOME_PING_CHECK_NONE) ||
759             (home->ev != NULL)) {
760                 return;
761         }
762
763         request = request_alloc();
764         request->number = request_num_counter++;
765
766         request->proxy = rad_alloc(1);
767         rad_assert(request->proxy != NULL);
768
769         fr_event_now(el, &request->when);
770         home->when = request->when;
771
772         if (home->ping_check == HOME_PING_CHECK_STATUS_SERVER) {
773                 request->proxy->code = PW_STATUS_SERVER;
774
775                 radius_pairmake(request, &request->proxy->vps,
776                                 "Message-Authenticator", "0x00", T_OP_SET);
777
778         } else if (home->type == HOME_TYPE_AUTH) {
779                 request->proxy->code = PW_AUTHENTICATION_REQUEST;
780
781                 radius_pairmake(request, &request->proxy->vps,
782                                 "User-Name", home->ping_user_name, T_OP_SET);
783                 radius_pairmake(request, &request->proxy->vps,
784                                 "User-Password", home->ping_user_password, T_OP_SET);
785                 radius_pairmake(request, &request->proxy->vps,
786                                 "Service-Type", "Authenticate-Only", T_OP_SET);
787                 radius_pairmake(request, &request->proxy->vps,
788                                 "Message-Authenticator", "0x00", T_OP_SET);
789
790         } else {
791 #ifdef WITH_ACCOUNTING
792                 request->proxy->code = PW_ACCOUNTING_REQUEST;
793                 
794                 radius_pairmake(request, &request->proxy->vps,
795                                 "User-Name", home->ping_user_name, T_OP_SET);
796                 radius_pairmake(request, &request->proxy->vps,
797                                 "Acct-Status-Type", "Stop", T_OP_SET);
798                 radius_pairmake(request, &request->proxy->vps,
799                                 "Acct-Session-Id", "00000000", T_OP_SET);
800                 vp = radius_pairmake(request, &request->proxy->vps,
801                                      "Event-Timestamp", "0", T_OP_SET);
802                 vp->vp_date = now.tv_sec;
803 #else
804                 rad_assert("Internal sanity check failed");
805 #endif
806         }
807
808         radius_pairmake(request, &request->proxy->vps,
809                         "NAS-Identifier", "Status Check. Are you alive?",
810                         T_OP_SET);
811
812         request->proxy->dst_ipaddr = home->ipaddr;
813         request->proxy->dst_port = home->port;
814         request->home_server = home;
815
816         rad_assert(request->proxy_listener == NULL);
817
818         if (!insert_into_proxy_hash(request)) {
819                 radlog(L_PROXY, "Failed to insert status check %d into proxy list.  Discarding it.",
820                        request->number);
821                 ev_request_free(&request);
822                 return;
823         }
824         rad_assert(request->proxy_listener != NULL);
825         request->proxy_listener->send(request->proxy_listener,
826                                       request);
827
828         request->next_callback = NULL;
829         request->child_state = REQUEST_PROXIED;
830         request->when.tv_sec += home->ping_timeout;;
831
832         INSERT_EVENT(no_response_to_ping, request);
833
834         /*
835          *      Add +/- 2s of jitter, as suggested in RFC 3539
836          *      and in the Issues and Fixes draft.
837          */
838         home->when.tv_sec += home->ping_interval - 2;
839
840         jitter = fr_rand();
841         jitter ^= (jitter >> 10);
842         jitter &= ((1 << 23) - 1); /* 22 bits of 1 */
843
844         tv_add(&home->when, jitter);
845
846         INSERT_EVENT(ping_home_server, home);
847 }
848
849
850 void mark_home_server_dead(home_server *home, struct timeval *when)
851 {
852         int previous_state = home->state;
853         char buffer[128];
854
855         radlog(L_PROXY, "Marking home server %s port %d as dead.",
856                inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
857                          buffer, sizeof(buffer)),
858                home->port);
859
860         home->state = HOME_STATE_IS_DEAD;
861         home->num_received_pings = 0;
862
863         if (home->ping_check != HOME_PING_CHECK_NONE) {
864                 /*
865                  *      If the control socket marks us dead, start
866                  *      pinging.  Otherwise, we already started
867                  *      pinging when it was marked "zombie".
868                  */
869                 if (previous_state == HOME_STATE_ALIVE) {
870                         ping_home_server(home);
871                 }
872
873         } else {
874                 /*
875                  *      Revive it after a fixed period of time.  This
876                  *      is very, very, bad.
877                  */
878                 home->when = *when;
879                 home->when.tv_sec += home->revive_interval;
880
881                 INSERT_EVENT(revive_home_server, home);
882         }
883 }
884
885 static void check_for_zombie_home_server(REQUEST *request)
886 {
887         home_server *home;
888         struct timeval when;
889
890         home = request->home_server;
891
892         if (home->state != HOME_STATE_ZOMBIE) return;
893
894         when = home->zombie_period_start;
895         when.tv_sec += home->zombie_period;
896
897         fr_event_now(el, &now);
898         if (timercmp(&now, &when, <)) {
899                 return;
900         }
901
902         mark_home_server_dead(home, &request->when);
903 }
904
905 static int proxy_to_virtual_server(REQUEST *request);
906
907 static int virtual_server_handler(UNUSED REQUEST *request)
908 {
909         proxy_to_virtual_server(request);
910         return 0;
911 }
912
913 static void proxy_fallback_handler(REQUEST *request)
914 {
915         /*
916          *      A proper time is required for wait_a_bit.
917          */
918         request->delay = USEC / 10;
919         gettimeofday(&now, NULL);
920         request->next_when = now;
921         tv_add(&request->next_when, request->delay);
922         request->next_callback = wait_a_bit;
923
924         /*
925          *      Re-queue the request.
926          */
927         request->child_state = REQUEST_QUEUED;
928         
929         rad_assert(request->proxy != NULL);
930         thread_pool_addrequest(request, virtual_server_handler);
931
932 #ifdef HAVE_PTHREAD_H
933         /*
934          *      MAY free the request if we're over max_request_time,
935          *      AND we're not in threaded mode!
936          *
937          *      Note that we call this ONLY if we're threaded, as
938          *      if we're NOT threaded, request_post_handler() calls
939          *      wait_a_bit(), which means that "request" may not
940          *      exist any more...
941          */
942         if (have_children) wait_a_bit(request);
943 #endif
944 }
945
946
947 static int setup_post_proxy_fail(REQUEST *request)
948 {
949         DICT_VALUE *dval = NULL;
950         VALUE_PAIR *vp;
951
952         request->child_state = REQUEST_RUNNING;
953
954         if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
955           dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-Authentication");
956
957         } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
958                 dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-Accounting");
959
960 #ifdef WITH_COA
961                 /*
962                  *      See no_response_to_coa_request
963                  */
964         } else if (((request->packet->code >> 8) & 0xff) == PW_COA_REQUEST) {
965                 request->packet->code &= 0xff; /* restore it */
966
967                 if (request->proxy->code == PW_COA_REQUEST) {
968                         dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-CoA");
969
970                 } else if (request->proxy->code == PW_DISCONNECT_REQUEST) {
971                         dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail-Disconnect");
972                 } else {
973                         return 0;
974                 }
975
976 #endif
977         } else {
978                 return 0;
979         }
980
981         if (!dval) dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail");
982
983         if (!dval) {
984                 pairdelete(&request->config_items, PW_POST_PROXY_TYPE, 0);
985                 return 0;
986         }
987
988         vp = pairfind(request->config_items, PW_POST_PROXY_TYPE, 0);
989         if (!vp) vp = radius_paircreate(request, &request->config_items,
990                                         PW_POST_PROXY_TYPE, 0, PW_TYPE_INTEGER);
991         vp->vp_integer = dval->value;
992
993         rad_assert(request->proxy_reply == NULL);
994
995         return 1;
996 }
997
998
999 static int null_handler(UNUSED REQUEST *request)
1000 {
1001         return 0;
1002 }
1003
1004 static void post_proxy_fail_handler(REQUEST *request)
1005 {
1006         /*
1007          *      A proper time is required for wait_a_bit.
1008          */
1009         request->delay = USEC / 10;
1010         gettimeofday(&now, NULL);
1011
1012         /*
1013          *      Not set up to run Post-Proxy-Type = Fail.
1014          *
1015          *      Mark the request as still running, and figure out what
1016          *      to do next.
1017          */
1018         if (!setup_post_proxy_fail(request)) {
1019                 request_post_handler(request);
1020
1021         } else {
1022                 /*
1023                  *      Re-queue the request.
1024                  */
1025                 request->child_state = REQUEST_QUEUED;
1026
1027                 /*
1028                  *      There is a post-proxy-type of fail.  We run
1029                  *      the request through the pre/post proxy
1030                  *      handlers, just like it was a real proxied
1031                  *      request.  However, we set the per-request
1032                  *      handler to NULL, as we don't want to do
1033                  *      anything else.
1034                  *
1035                  *      Note that when we're not threaded, this will
1036                  *      process the request even if it's greater than
1037                  *      max_request_time.  That's not fatal.
1038                  */
1039                 request->priority = 0;
1040                 rad_assert(request->proxy != NULL);
1041                 thread_pool_addrequest(request, null_handler);
1042         }
1043
1044         /*
1045          *      MAY free the request if we're over max_request_time,
1046          *      AND we're not in threaded mode!
1047          *
1048          *      Note that we call this ONLY if we're threaded, as
1049          *      if we're NOT threaded, request_post_handler() calls
1050          *      wait_a_bit(), which means that "request" may not
1051          *      exist any more...
1052          */
1053         if (have_children) wait_a_bit(request);
1054 }
1055
1056 /* maybe check this against wait_for_proxy_id_to_expire? */
1057 static void no_response_to_proxied_request(void *ctx)
1058 {
1059         REQUEST *request = ctx;
1060         home_server *home;
1061         char buffer[128];
1062
1063         rad_assert(request->magic == REQUEST_MAGIC);
1064
1065         if (request->master_state == REQUEST_STOP_PROCESSING) {
1066                 ev_request_free(&request);
1067                 return;
1068         }
1069
1070         rad_assert(request->child_state == REQUEST_PROXIED);
1071
1072         /*
1073          *      If we've failed over to an internal home server,
1074          *      replace the callback with the correct one.  This
1075          *      is due to locking issues with child threads...
1076          */
1077         if (request->home_server->server) {
1078                 wait_a_bit(request);
1079                 return;
1080         }
1081
1082 #ifdef WITH_TCP
1083         if (request->home_server->proto != IPPROTO_TCP)
1084 #endif
1085                 check_for_zombie_home_server(request);
1086
1087         home = request->home_server;
1088
1089         /*
1090          *      The default as of 2.1.7 is to allow requests to
1091          *      fail-over to a backup home server when this one does
1092          *      not respond.  The old behavior can be configured as
1093          *      well.
1094          */
1095         if (home->no_response_fail) {
1096                 radlog_request(L_ERR, 0, request, "Rejecting request (proxy Id %d) due to lack of any response from home server %s port %d",
1097                        request->proxy->id,
1098                        inet_ntop(request->proxy->dst_ipaddr.af,
1099                                  &request->proxy->dst_ipaddr.ipaddr,
1100                                  buffer, sizeof(buffer)),
1101                        request->proxy->dst_port);
1102
1103                 post_proxy_fail_handler(request);
1104         } else {
1105                 /*
1106                  *      Enforce max_request_time.
1107                  *
1108                  *      We fail over to another backup home server
1109                  *      when the client re-transmits the request.  If
1110                  *      the client doesn't re-transmit, no fail-over
1111                  *      occurs.
1112                  */
1113                 rad_assert(request->ev == NULL);
1114                 request->child_state = REQUEST_RUNNING;
1115                 wait_a_bit(request);
1116         }
1117
1118         /*
1119          *      Don't touch request due to race conditions
1120          */
1121
1122 #ifdef WITH_TCP
1123         /*
1124          *      Do nothing more.  The home server didn't respond,
1125          *      but that isn't a catastrophic failure.  Some home
1126          *      servers don't respond to packets...
1127          */
1128         if (home->proto == IPPROTO_TCP) {
1129                 /*
1130                  *      FIXME: Set up TCP pinging on this connection.
1131                  *
1132                  *      Maybe the CONNECTION is dead, but the home
1133                  *      server is alive.  In that case, we need to start
1134                  *      pinging on the connection.
1135                  *
1136                  *      This means doing the pinging BEFORE the
1137                  *      post_proxy_fail_handler above, as it may do
1138                  *      something with the request, and cause the
1139                  *      proxy listener to go away!
1140                  */
1141                 return;
1142         }
1143 #endif
1144
1145         /*
1146          *      If it's not alive, don't try to make it a zombie.
1147          */
1148         if (home->state != HOME_STATE_ALIVE) {
1149                 /*
1150                  *      Don't check home->ev due to race conditions.
1151                  */
1152                 return;
1153         }
1154
1155         /*
1156          *      We've received a real packet recently.  Don't mark the
1157          *      server as zombie until we've received NO packets for a
1158          *      while.  The "1/4" of zombie period was chosen rather
1159          *      arbitrarily.  It's a balance between too short, which
1160          *      gives quick fail-over and fail-back, or too long,
1161          *      where the proxy still sends packets to an unresponsive
1162          *      home server.
1163          */
1164         if ((home->last_packet + ((home->zombie_period + 3) / 4)) >= now.tv_sec) {
1165                 return;
1166         }
1167
1168         /*
1169          *      Enable the zombie period when we notice that the home
1170          *      server hasn't responded for a while.  We back-date the
1171          *      zombie period to when we last received a response from
1172          *      the home server.
1173          */
1174         home->state = HOME_STATE_ZOMBIE;
1175         
1176         home->zombie_period_start.tv_sec = home->last_packet;
1177         home->zombie_period_start.tv_sec = USEC / 2;
1178         
1179         fr_event_delete(el, &home->ev);
1180         home->currently_outstanding = 0;
1181         home->num_received_pings = 0;
1182         
1183         radlog(L_PROXY, "Marking home server %s port %d as zombie (it looks like it is dead).",
1184                inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
1185                          buffer, sizeof(buffer)),
1186                home->port);
1187         
1188         /*
1189          *      Start pinging the home server.
1190          */
1191         ping_home_server(home);
1192 }
1193 #endif
1194
1195 static void wait_a_bit(void *ctx)
1196 {
1197         struct timeval when;
1198         REQUEST *request = ctx;
1199         fr_event_callback_t callback = NULL;
1200
1201         rad_assert(request->magic == REQUEST_MAGIC);
1202
1203 #ifdef HAVE_PTHREAD_H
1204         /*
1205          *      The socket was closed.  Tell the request that
1206          *      there is no point in continuing.
1207          */
1208         if (request->listener->status != RAD_LISTEN_STATUS_KNOWN) {
1209                 goto stop_processing;
1210         }
1211 #endif
1212
1213 #ifdef WITH_COA
1214         /*
1215          *      The CoA request is a new (internally generated)
1216          *      request, created in a child thread.  We therefore need
1217          *      some way to tie its events back into the main event
1218          *      handler.
1219          */
1220         if (request->coa && !request->coa->proxy_reply &&
1221             request->coa->next_callback) {
1222                 request->coa->when = request->coa->next_when;
1223                 INSERT_EVENT(request->coa->next_callback, request->coa);
1224                 request->coa->next_callback = NULL;
1225                 request->coa->parent = NULL;
1226                 request->coa = NULL;
1227         }
1228 #endif
1229
1230         switch (request->child_state) {
1231         case REQUEST_QUEUED:
1232         case REQUEST_RUNNING:
1233                 /*
1234                  *      If we're not thread-capable, OR we're capable,
1235                  *      but have been told to run without threads,
1236                  *      complain when the requests is queued for a
1237                  *      thread, or running in a child thread.
1238                  */
1239 #ifdef HAVE_PTHREAD_H
1240                 if (!have_children)
1241 #endif
1242                 {
1243                         rad_assert("We do not have threads, but the request is marked as queued or running in a child thread" == NULL);
1244                         break;
1245                 }
1246
1247 #ifdef HAVE_PTHREAD_H
1248                 /*
1249                  *      If we have threads, wait for the child thread
1250                  *      to stop.
1251                  */
1252                 when = request->received;
1253                 when.tv_sec += request->root->max_request_time;
1254
1255                 /*
1256                  *      Normally called from the event loop with the
1257                  *      proper event loop time.  Otherwise, called from
1258                  *      post proxy fail handler, which sets "now", and
1259                  *      this call won't re-set it, because we're not
1260                  *      in the event loop.
1261                  */
1262                 fr_event_now(el, &now);
1263
1264                 /*
1265                  *      Request still has more time.  Continue
1266                  *      waiting.
1267                  */
1268                 if (timercmp(&now, &when, <)) {
1269                         if (request->delay < (USEC / 10)) {
1270                                 request->delay = USEC / 10;
1271                         }
1272                         request->delay += request->delay >> 1;
1273
1274                         /*
1275                          *      Cap delays at something reasonable.
1276                          */
1277                         if (request->delay > (request->root->max_request_time * USEC)) {
1278                                 request->delay = request->root->max_request_time * USEC;
1279                         }
1280
1281                         request->when = now;
1282                         tv_add(&request->when, request->delay);
1283                         callback = wait_a_bit;
1284                         break;
1285                 }
1286
1287         stop_processing:
1288                 request->master_state = REQUEST_STOP_PROCESSING;
1289
1290                 /*
1291                  *      A child thread MAY still be running on the
1292                  *      request.  Ask the thread to stop working on
1293                  *      the request.
1294                  */
1295                 if (have_children &&
1296                     (pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0)) {
1297                         radlog(L_ERR, "WARNING: Unresponsive child for request %u, in component %s module %s",
1298                                request->number,
1299                                request->component ? request->component : "<server core>",
1300                                request->module ? request->module : "<server core>");
1301
1302                 }
1303                         
1304                 request->delay = USEC;
1305                 tv_add(&request->when, request->delay);
1306                 callback = wait_for_child_to_die;
1307                 break;
1308 #endif
1309
1310                 /*
1311                  *      Mark the request as no longer running,
1312                  *      and clean it up.
1313                  */
1314         case REQUEST_DONE:
1315 #ifdef HAVE_PTHREAD_H
1316                 request->child_pid = NO_SUCH_CHILD_PID;
1317 #endif
1318
1319 #ifdef WITH_COA
1320                 /*
1321                  *      This is a CoA request.  It's been divorced
1322                  *      from everything else, so we clean it up now.
1323                  */
1324                 if (!request->in_request_hash &&
1325                     request->proxy &&
1326                     (request->packet->code != request->proxy->code) &&
1327                     ((request->proxy->code == PW_COA_REQUEST) ||
1328                      (request->proxy->code == PW_DISCONNECT_REQUEST))) {
1329                         /*
1330                          *      FIXME: Do CoA MIBs
1331                          */
1332                         ev_request_free(&request);
1333                         return;
1334                 }
1335 #endif
1336                 cleanup_delay(request);
1337                 return;
1338
1339         case REQUEST_REJECT_DELAY:
1340         case REQUEST_CLEANUP_DELAY:
1341 #ifdef HAVE_PTHREAD_H
1342                 request->child_pid = NO_SUCH_CHILD_PID;
1343 #endif
1344
1345         case REQUEST_PROXIED:
1346                 rad_assert(request->next_callback != NULL);
1347                 rad_assert(request->next_callback != wait_a_bit);
1348
1349                 request->when = request->next_when;
1350                 callback = request->next_callback;
1351                 request->next_callback = NULL;
1352                 break;
1353
1354         default:
1355                 rad_panic("Internal sanity check failure");
1356                 return;
1357         }
1358
1359         /*
1360          *      Something major went wrong.  Discard the request, and
1361          *      keep running.
1362          *
1363          *      FIXME: No idea why this happens or how to fix it...
1364          *      It seems to happen *only* when requests are proxied,
1365          *      and where the home server doesn't respond.  So it looks
1366          *      like a race condition above, but it happens in debug
1367          *      mode, with no threads...
1368          */
1369         if (!callback) {
1370                 RDEBUG("WARNING: Internal sanity check failed in event handler: Discarding the request!");
1371                 ev_request_free(&request);
1372                 return;
1373         }
1374
1375         INSERT_EVENT(callback, request);
1376 }
1377
1378 #ifdef WITH_COA
1379 static void no_response_to_coa_request(void *ctx)
1380 {
1381         REQUEST *request = ctx;
1382         char buffer[128];
1383
1384         rad_assert(request->magic == REQUEST_MAGIC);
1385         rad_assert(request->child_state == REQUEST_PROXIED);
1386         rad_assert(request->home_server != NULL);
1387         rad_assert(!request->in_request_hash);
1388
1389         radlog(L_ERR, "No response to CoA request sent to %s",
1390                inet_ntop(request->proxy->dst_ipaddr.af,
1391                          &request->proxy->dst_ipaddr.ipaddr,
1392                          buffer, sizeof(buffer)));
1393
1394         /*
1395          *      Hack.
1396          */
1397         request->packet->code |= (PW_COA_REQUEST << 8);
1398         post_proxy_fail_handler(request);
1399 }
1400
1401
1402 static int update_event_timestamp(RADIUS_PACKET *packet, time_t when)
1403 {
1404         VALUE_PAIR *vp;
1405
1406         vp = pairfind(packet->vps, PW_EVENT_TIMESTAMP, 0);
1407         if (!vp) return 0;
1408
1409         vp->vp_date = when;
1410
1411         if (packet->data) {
1412                 free(packet->data);
1413                 packet->data = NULL;
1414                 packet->data_len = 0;
1415         }
1416
1417         return 1;               /* time stamp updated */
1418 }
1419
1420
1421 /*
1422  *      Called when we haven't received a response to a CoA request.
1423  */
1424 static void retransmit_coa_request(void *ctx)
1425 {
1426         int delay, frac;
1427         struct timeval mrd;
1428         REQUEST *request = ctx;
1429
1430         rad_assert(request->magic == REQUEST_MAGIC);
1431         rad_assert(request->child_state == REQUEST_PROXIED);
1432         rad_assert(request->home_server != NULL);
1433         rad_assert(!request->in_request_hash);
1434         rad_assert(request->parent == NULL);
1435         
1436         fr_event_now(el, &now);
1437
1438         /*
1439          *      Cap count at MRC, if it is non-zero.
1440          */
1441         if (request->home_server->coa_mrc &&
1442             (request->num_coa_requests >= request->home_server->coa_mrc)) {
1443                 no_response_to_coa_request(request);
1444                 return;
1445         }
1446
1447         /*
1448          *      RFC 5080 Section 2.2.1
1449          *
1450          *      RT = 2*RTprev + RAND*RTprev
1451          *         = 1.9 * RTprev + rand(0,.2) * RTprev
1452          *         = 1.9 * RTprev + rand(0,1) * (RTprev / 5)
1453          */
1454         delay = fr_rand();
1455         delay ^= (delay >> 16);
1456         delay &= 0xffff;
1457         frac = request->delay / 5;
1458         delay = ((frac >> 16) * delay) + (((frac & 0xffff) * delay) >> 16);
1459
1460         delay += (2 * request->delay) - (request->delay / 10);
1461
1462         /*
1463          *      Cap delay at MRT, if MRT is non-zero.
1464          */
1465         if (request->home_server->coa_mrt &&
1466             (delay > (request->home_server->coa_mrt * USEC))) {
1467                 int mrt_usec = request->home_server->coa_mrt * USEC;
1468
1469                 /*
1470                  *      delay = MRT + RAND * MRT
1471                  *            = 0.9 MRT + rand(0,.2)  * MRT
1472                  */
1473                 delay = fr_rand();
1474                 delay ^= (delay >> 15);
1475                 delay &= 0x1ffff;
1476                 delay = ((mrt_usec >> 16) * delay) + (((mrt_usec & 0xffff) * delay) >> 16);
1477                 delay += mrt_usec - (mrt_usec / 10);
1478         }
1479
1480         request->delay = delay;
1481         request->when = now;
1482         tv_add(&request->when, request->delay);
1483         mrd = request->proxy_when;
1484         mrd.tv_sec += request->home_server->coa_mrd;
1485
1486         /*
1487          *      Cap duration at MRD.
1488          */
1489         if (timercmp(&mrd, &request->when, <)) {
1490                 request->when = mrd;
1491                 INSERT_EVENT(no_response_to_coa_request, request);
1492
1493         } else {
1494                 INSERT_EVENT(retransmit_coa_request, request);
1495         }
1496         
1497         if (update_event_timestamp(request->proxy, now.tv_sec)) {
1498                 /*
1499                  *      Keep a copy of the old Id so that the
1500                  *      re-transmitted request doesn't re-use the old
1501                  *      Id.
1502                  */
1503                 RADIUS_PACKET old = *request->proxy;
1504                 home_server *home = request->home_server;
1505                 rad_listen_t *listener = request->proxy_listener;
1506
1507                 /*
1508                  *      Don't free the old Id on error.
1509                  */
1510                 if (!insert_into_proxy_hash(request)) {
1511                         radlog(L_PROXY,"Failed to insert retransmission of CoA request into proxy list.");
1512                         return;
1513                 }
1514
1515                 /*
1516                  *      Now that we have a new Id, free the old one
1517                  *      and update the various statistics.
1518                  */
1519                 PTHREAD_MUTEX_LOCK(&proxy_mutex);
1520                 fr_packet_list_yank(proxy_list, &old);
1521                 fr_packet_list_id_free(proxy_list, &old);
1522                 if (home) home->currently_outstanding--;
1523 #ifdef WITH_TCP
1524                 if (listener) listener->count--;
1525 #endif
1526                 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
1527
1528         } else {                /* FIXME: protect by a mutex? */
1529                 request->num_proxied_requests++;
1530         }
1531
1532         request->num_coa_requests++; /* is NOT reset by code 3 lines above! */
1533
1534         request->proxy_listener->send(request->proxy_listener,
1535                                       request);
1536 }
1537
1538
1539 /*
1540  *      The original request is either DONE, or in CLEANUP_DELAY.
1541  */
1542 static int originated_coa_request(REQUEST *request)
1543 {
1544         int delay, rcode, pre_proxy_type = 0;
1545         VALUE_PAIR *vp;
1546         REQUEST *coa;
1547         fr_ipaddr_t ipaddr;
1548         char buffer[256];
1549
1550         rad_assert(request->proxy == NULL);
1551         rad_assert(!request->in_proxy_hash);
1552         rad_assert(request->proxy_reply == NULL);
1553
1554         /*
1555          *      Check whether we want to originate one, or cancel one.
1556          */
1557         vp = pairfind(request->config_items, PW_SEND_COA_REQUEST, 0);
1558         if (!vp && request->coa) {
1559                 vp = pairfind(request->coa->proxy->vps, PW_SEND_COA_REQUEST, 0);
1560         }
1561
1562         if (vp) {
1563                 if (vp->vp_integer == 0) {
1564                         ev_request_free(&request->coa);
1565                         return 1;       /* success */
1566                 }
1567         }
1568
1569         if (!request->coa) request_alloc_coa(request);
1570         if (!request->coa) return 0;
1571
1572         coa = request->coa;
1573
1574         /*
1575          *      src_ipaddr will be set up in proxy_encode.
1576          */
1577         memset(&ipaddr, 0, sizeof(ipaddr));
1578         vp = pairfind(coa->proxy->vps, PW_PACKET_DST_IP_ADDRESS, 0);
1579         if (vp) {
1580                 ipaddr.af = AF_INET;
1581                 ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
1582
1583         } else if ((vp = pairfind(coa->proxy->vps,
1584                                   PW_PACKET_DST_IPV6_ADDRESS, 0)) != NULL) {
1585                 ipaddr.af = AF_INET6;
1586                 ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
1587                 
1588         } else if ((vp = pairfind(coa->proxy->vps,
1589                                   PW_HOME_SERVER_POOL, 0)) != NULL) {
1590                 coa->home_pool = home_pool_byname(vp->vp_strvalue,
1591                                                   HOME_TYPE_COA);
1592                 if (!coa->home_pool) {
1593                         RDEBUG2("WARNING: No such home_server_pool %s",
1594                                vp->vp_strvalue);
1595         fail:
1596                         ev_request_free(&request->coa);
1597                         return 0;
1598                 }
1599
1600                 /*
1601                  *      Prefer
1602                  */
1603         } else if (request->client->coa_pool) {
1604                 coa->home_pool = request->client->coa_pool;
1605
1606         } else if (request->client->coa_server) {
1607                 coa->home_server = request->client->coa_server;
1608
1609         } else {
1610                 /*
1611                  *      If all else fails, send it to the client that
1612                  *      originated this request.
1613                  */
1614                 memcpy(&ipaddr, &request->packet->src_ipaddr, sizeof(ipaddr));
1615         }
1616
1617         /*
1618          *      Use the pool, if it exists.
1619          */
1620         if (coa->home_pool) {
1621                 coa->home_server = home_server_ldb(NULL, coa->home_pool, coa);
1622                 if (!coa->home_server) {
1623                         RDEBUG("WARNING: No live home server for home_server_pool %s", vp->vp_strvalue);
1624                         goto fail;
1625                 }
1626
1627         } else if (!coa->home_server) {
1628                 int port = PW_COA_UDP_PORT;
1629
1630                 vp = pairfind(coa->proxy->vps, PW_PACKET_DST_PORT, 0);
1631                 if (vp) port = vp->vp_integer;
1632
1633                 coa->home_server = home_server_find(&ipaddr, port, IPPROTO_UDP);
1634                 if (!coa->home_server) {
1635                         RDEBUG2("WARNING: Unknown destination %s:%d for CoA request.",
1636                                inet_ntop(ipaddr.af, &ipaddr.ipaddr,
1637                                          buffer, sizeof(buffer)), port);
1638                         goto fail;
1639                 }
1640         }
1641
1642         vp = pairfind(coa->proxy->vps, PW_PACKET_TYPE, 0);
1643         if (vp) {
1644                 switch (vp->vp_integer) {
1645                 case PW_COA_REQUEST:
1646                 case PW_DISCONNECT_REQUEST:
1647                         coa->proxy->code = vp->vp_integer;
1648                         break;
1649                         
1650                 default:
1651                         DEBUG("Cannot set CoA Packet-Type to code %d",
1652                               vp->vp_integer);
1653                         goto fail;
1654                 }
1655         }
1656
1657         if (!coa->proxy->code) coa->proxy->code = PW_COA_REQUEST;
1658
1659         /*
1660          *      The rest of the server code assumes that
1661          *      request->packet && request->reply exist.  Copy them
1662          *      from the original request.
1663          */
1664         rad_assert(coa->packet != NULL);
1665         rad_assert(coa->packet->vps == NULL);
1666         memcpy(coa->packet, request->packet, sizeof(*request->packet));
1667         coa->packet->vps = paircopy(request->packet->vps);
1668         coa->packet->data = NULL;
1669         rad_assert(coa->reply != NULL);
1670         rad_assert(coa->reply->vps == NULL);
1671         memcpy(coa->reply, request->reply, sizeof(*request->reply));
1672         coa->reply->vps = paircopy(request->reply->vps);
1673         coa->reply->data = NULL;
1674         coa->config_items = paircopy(request->config_items);
1675
1676         /*
1677          *      Call the pre-proxy routines.
1678          */
1679         vp = pairfind(request->config_items, PW_PRE_PROXY_TYPE, 0);
1680         if (vp) {
1681                 RDEBUG2("  Found Pre-Proxy-Type %s", vp->vp_strvalue);
1682                 pre_proxy_type = vp->vp_integer;
1683         }
1684
1685         if (coa->home_pool && coa->home_pool->virtual_server) {
1686                 const char *old_server = coa->server;
1687                 
1688                 coa->server = coa->home_pool->virtual_server;
1689                 RDEBUG2(" server %s {", coa->server);
1690                 rcode = module_pre_proxy(pre_proxy_type, coa);
1691                 RDEBUG2(" }");
1692                 coa->server = old_server;
1693         } else {
1694                 rcode = module_pre_proxy(pre_proxy_type, coa);
1695         }
1696         switch (rcode) {
1697         default:
1698                 goto fail;
1699
1700         /*
1701          *      Only send the CoA packet if the pre-proxy code succeeded.
1702          */
1703         case RLM_MODULE_NOOP:
1704         case RLM_MODULE_OK:
1705         case RLM_MODULE_UPDATED:
1706                 break;
1707         }
1708
1709         /*
1710          *      Source IP / port is set when the proxy socket
1711          *      is chosen.
1712          */
1713         coa->proxy->dst_ipaddr = coa->home_server->ipaddr;
1714         coa->proxy->dst_port = coa->home_server->port;
1715
1716         if (!insert_into_proxy_hash(coa)) {
1717                 radlog(L_PROXY, "Failed to insert CoA request into proxy list.");
1718                 goto fail;
1719         }
1720
1721         /*
1722          *      We CANNOT divorce the CoA request from the parent
1723          *      request.  This function is running in a child thread,
1724          *      and we need access to the main event loop in order to
1725          *      to add the timers for the CoA packet.  See
1726          *      wait_a_bit().
1727          */
1728
1729         /*
1730          *      Forget about the original request completely at this
1731          *      point.
1732          */
1733         request = coa;
1734
1735         gettimeofday(&request->proxy_when, NULL);       
1736         request->received = request->next_when = request->proxy_when;
1737         rad_assert(request->proxy_reply == NULL);
1738
1739         /*
1740          *      Implement re-transmit algorithm as per RFC 5080
1741          *      Section 2.2.1.
1742          *
1743          *      We want IRT + RAND*IRT
1744          *      or 0.9 IRT + rand(0,.2) IRT
1745          *
1746          *      2^20 ~ USEC, and we want 2.
1747          *      rand(0,0.2) USEC ~ (rand(0,2^21) / 10)
1748          */
1749         delay = (fr_rand() & ((1 << 22) - 1)) / 10;
1750         request->delay = delay * request->home_server->coa_irt;
1751         delay = request->home_server->coa_irt * USEC;
1752         delay -= delay / 10;
1753         delay += request->delay;
1754      
1755         request->delay = delay;
1756         tv_add(&request->next_when, delay);
1757         request->next_callback = retransmit_coa_request;
1758         
1759         /*
1760          *      Note that we set proxied BEFORE sending the packet.
1761          *
1762          *      Once we send it, the request is tainted, as
1763          *      another thread may have picked it up.  Don't
1764          *      touch it!
1765          */
1766         request->child_pid = NO_SUCH_CHILD_PID;
1767
1768         update_event_timestamp(request->proxy, request->proxy_when.tv_sec);
1769
1770         request->child_state = REQUEST_PROXIED;
1771
1772         DEBUG_PACKET(request, request->proxy, 1);
1773
1774         request->proxy_listener->send(request->proxy_listener,
1775                                       request);
1776         return 1;
1777 }
1778 #endif  /* WITH_COA */
1779
1780 #ifdef WITH_PROXY
1781 static int process_proxy_reply(REQUEST *request)
1782 {
1783         int rcode;
1784         int post_proxy_type = 0;
1785         VALUE_PAIR *vp;
1786         
1787         /*
1788          *      Delete any reply we had accumulated until now.
1789          */
1790         pairfree(&request->reply->vps);
1791         
1792         /*
1793          *      Run the packet through the post-proxy stage,
1794          *      BEFORE playing games with the attributes.
1795          */
1796         vp = pairfind(request->config_items, PW_POST_PROXY_TYPE, 0);
1797         if (vp) {
1798                 RDEBUG2("  Found Post-Proxy-Type %s", vp->vp_strvalue);
1799                 post_proxy_type = vp->vp_integer;
1800         }
1801         
1802         if (request->home_pool && request->home_pool->virtual_server) {
1803                 const char *old_server = request->server;
1804                 
1805                 request->server = request->home_pool->virtual_server;
1806                 RDEBUG2(" server %s {", request->server);
1807                 rcode = module_post_proxy(post_proxy_type, request);
1808                 RDEBUG2(" }");
1809                 request->server = old_server;
1810         } else {
1811                 rcode = module_post_proxy(post_proxy_type, request);
1812         }
1813
1814 #ifdef WITH_COA
1815         if (request->packet->code == request->proxy->code)
1816           /*
1817            *    Don't run the next bit if we originated a CoA
1818            *    packet, after receiving an Access-Request or
1819            *    Accounting-Request.
1820            */
1821 #endif
1822         
1823         /*
1824          *      There may NOT be a proxy reply, as we may be
1825          *      running Post-Proxy-Type = Fail.
1826          */
1827         if (request->proxy_reply) {
1828                 /*
1829                  *      Delete the Proxy-State Attributes from
1830                  *      the reply.  These include Proxy-State
1831                  *      attributes from us and remote server.
1832                  */
1833                 pairdelete(&request->proxy_reply->vps, PW_PROXY_STATE, 0);
1834                 
1835                 /*
1836                  *      Add the attributes left in the proxy
1837                  *      reply to the reply list.
1838                  */
1839                 pairadd(&request->reply->vps, request->proxy_reply->vps);
1840                 request->proxy_reply->vps = NULL;
1841                 
1842                 /*
1843                  *      Free proxy request pairs.
1844                  */
1845                 pairfree(&request->proxy->vps);
1846         }
1847         
1848         switch (rcode) {
1849         default:  /* Don't do anything */
1850                 break;
1851         case RLM_MODULE_FAIL:
1852                 /* FIXME: debug print stuff */
1853                 request->child_state = REQUEST_DONE;
1854                 return 0;
1855                 
1856         case RLM_MODULE_HANDLED:
1857                 /* FIXME: debug print stuff */
1858                 request->child_state = REQUEST_DONE;
1859                 return 0;
1860         }
1861
1862         return 1;
1863 }
1864 #endif
1865
1866 static int request_pre_handler(REQUEST *request)
1867 {
1868         int rcode;
1869
1870         rad_assert(request->magic == REQUEST_MAGIC);
1871         rad_assert(request->packet != NULL);
1872
1873         request->child_state = REQUEST_RUNNING;
1874
1875         /*
1876          *      Don't decode the packet if it's an internal "fake"
1877          *      request.  Instead, just return so that the caller can
1878          *      process it.
1879          */
1880         if (request->packet->dst_port == 0) {
1881                 request->username = pairfind(request->packet->vps,
1882                                              PW_USER_NAME, 0);
1883                 request->password = pairfind(request->packet->vps,
1884                                              PW_USER_PASSWORD, 0);
1885                 return 1;
1886         }
1887
1888 #ifdef WITH_PROXY
1889         /*
1890          *      Put the decoded packet into it's proper place.
1891          */
1892         if (request->proxy_reply != NULL) {
1893                 rcode = request->proxy_listener->decode(request->proxy_listener, request);
1894                 DEBUG_PACKET(request, request->proxy_reply, 0);
1895
1896                 /*
1897                  *      Pro-actively remove it from the proxy hash.
1898                  *      This is later than in 2.1.x, but it means that
1899                  *      the replies are authenticated before being
1900                  *      removed from the hash.
1901                  */
1902                 if ((rcode == 0) &&
1903                     (request->num_proxied_requests <= request->num_proxied_responses)) {
1904                         remove_from_proxy_hash(request);
1905                 }
1906
1907         } else
1908 #endif
1909         if (request->packet->vps == NULL) {
1910                 rcode = request->listener->decode(request->listener, request);
1911                 
1912                 if (debug_condition) {
1913                         int result = FALSE;
1914                         const char *my_debug = debug_condition;
1915
1916                         /*
1917                          *      Ignore parse errors.
1918                          */
1919                         radius_evaluate_condition(request, RLM_MODULE_OK, 0,
1920                                                   &my_debug, 1,
1921                                                   &result);
1922                         if (result) {
1923                                 request->options = 2;
1924                                 request->radlog = radlog_request;
1925                         }
1926                 }
1927                 
1928                 DEBUG_PACKET(request, request->packet, 0);
1929         } else {
1930                 rcode = 0;
1931         }
1932
1933         if (rcode < 0) {
1934                 RDEBUG("%s Dropping packet without response.", fr_strerror());
1935                 request->reply->offset = -2; /* bad authenticator */
1936                 request->child_state = REQUEST_DONE;
1937                 return 0;
1938         }
1939
1940         if (!request->username) {
1941                 request->username = pairfind(request->packet->vps,
1942                                              PW_USER_NAME, 0);
1943         }
1944
1945 #ifdef WITH_PROXY
1946         if (request->proxy) {
1947                 return process_proxy_reply(request);
1948         }
1949 #endif
1950
1951         return 1;
1952 }
1953
1954
1955 #ifdef WITH_PROXY
1956 /*
1957  *      Do state handling when we proxy a request.
1958  */
1959 static int proxy_request(REQUEST *request)
1960 {
1961         struct timeval when;
1962         char buffer[128];
1963
1964 #ifdef WITH_COA
1965         if (request->coa) {
1966                 RDEBUG("WARNING: Cannot proxy and originate CoA packets at the same time.  Cancelling CoA request");
1967                 ev_request_free(&request->coa);
1968         }
1969 #endif
1970
1971         if (request->home_server->server) {
1972                 RDEBUG("ERROR: Cannot proxy to a virtual server.");
1973                 return 0;
1974         }
1975
1976         if (!insert_into_proxy_hash(request)) {
1977                 radlog(L_PROXY, "Failed to insert request into proxy list.");
1978                 return 0;
1979         }
1980
1981         request->proxy_listener->encode(request->proxy_listener, request);
1982
1983         when = request->received;
1984         when.tv_sec += request->root->max_request_time;
1985
1986         gettimeofday(&request->proxy_when, NULL);
1987
1988         request->next_when = request->proxy_when;
1989         request->next_when.tv_sec += request->home_server->response_window;
1990
1991         rad_assert(request->home_server->response_window > 0);
1992
1993         if (timercmp(&when, &request->next_when, <)) {
1994                 request->next_when = when;
1995         }
1996         request->next_callback = no_response_to_proxied_request;
1997
1998         RDEBUG2("Proxying request to home server %s port %d",
1999                inet_ntop(request->proxy->dst_ipaddr.af,
2000                          &request->proxy->dst_ipaddr.ipaddr,
2001                          buffer, sizeof(buffer)),
2002                 request->proxy->dst_port);
2003
2004         /*
2005          *      Note that we set proxied BEFORE sending the packet.
2006          *
2007          *      Once we send it, the request is tainted, as
2008          *      another thread may have picked it up.  Don't
2009          *      touch it!
2010          */
2011 #ifdef HAVE_PTHREAD_H
2012         request->child_pid = NO_SUCH_CHILD_PID;
2013 #endif
2014         request->child_state = REQUEST_PROXIED;
2015
2016         DEBUG_PACKET(request, request->proxy, 1);
2017
2018         request->proxy_listener->send(request->proxy_listener,
2019                                       request);
2020         return 1;
2021 }
2022
2023
2024 /*
2025  *      "Proxy" the request by sending it to a new virtual server.
2026  */
2027 static int proxy_to_virtual_server(REQUEST *request)
2028 {
2029         REQUEST *fake;
2030         RAD_REQUEST_FUNP fun;
2031
2032         if (!request->home_server || !request->home_server->server) return 0;
2033
2034         if (request->parent) {
2035                 RDEBUG2("WARNING: Cancelling proxy request to virtual server %s as this request was itself proxied.", request->home_server->server);
2036                 return 0;
2037         }
2038
2039         fake = request_alloc_fake(request);
2040         if (!fake) {
2041                 RDEBUG2("WARNING: Out of memory");
2042                 return 0;
2043         }
2044
2045         fake->packet->vps = paircopy(request->proxy->vps);
2046         fake->server = request->home_server->server;
2047
2048         if (request->proxy->code == PW_AUTHENTICATION_REQUEST) {
2049                 fun = rad_authenticate;
2050
2051 #ifdef WITH_ACCOUNTING
2052         } else if (request->proxy->code == PW_ACCOUNTING_REQUEST) {
2053                 fun = rad_accounting;
2054 #endif
2055
2056         } else {
2057                 RDEBUG2("Unknown packet type %d", request->proxy->code);
2058                 ev_request_free(&fake);
2059                 return 0;
2060         }
2061
2062         RDEBUG2(">>> Sending proxied request internally to virtual server.");
2063         radius_handle_request(fake, fun);
2064         RDEBUG2("<<< Received proxied response code %d from internal virtual server.", fake->reply->code);
2065
2066         if (fake->reply->code != 0) {
2067                 request->proxy_reply = fake->reply;
2068                 fake->reply = NULL;
2069         } else {
2070                 /*
2071                  *      There was no response
2072                  */
2073                 setup_post_proxy_fail(request);
2074         }
2075
2076         ev_request_free(&fake);
2077
2078         process_proxy_reply(request);
2079
2080         /*
2081          *      Process it through the normal section again, but ONLY
2082          *      if we received a proxy reply..
2083          */
2084         if (request->proxy_reply) {
2085                 if (request->server) RDEBUG("server %s {",
2086                                             request->server != NULL ?
2087                                             request->server : ""); 
2088                 fun(request);
2089                 
2090                 if (request->server) RDEBUG("} # server %s",
2091                                             request->server != NULL ?
2092                                             request->server : "");
2093         }
2094
2095         return 2;               /* success, but NOT '1' !*/
2096 }
2097
2098 /*
2099  *      Return 1 if we did proxy it, or the proxy attempt failed
2100  *      completely.  Either way, the caller doesn't touch the request
2101  *      any more if we return 1.
2102  */
2103 static int successfully_proxied_request(REQUEST *request)
2104 {
2105         int rcode;
2106         int pre_proxy_type = 0;
2107         VALUE_PAIR *realmpair;
2108         VALUE_PAIR *strippedname;
2109         VALUE_PAIR *vp;
2110         char *realmname = NULL;
2111         home_server *home;
2112         REALM *realm = NULL;
2113         home_pool_t *pool;
2114
2115         /*
2116          *      If it was already proxied, do nothing.
2117          *
2118          *      FIXME: This should really be a serious error.
2119          */
2120         if (request->in_proxy_hash ||
2121             (request->proxy_reply && (request->proxy_reply->code != 0))) {
2122                 return 0;
2123         }
2124
2125         realmpair = pairfind(request->config_items, PW_PROXY_TO_REALM, 0);
2126         if (!realmpair || (realmpair->length == 0)) {
2127                 int pool_type;
2128
2129                 vp = pairfind(request->config_items, PW_HOME_SERVER_POOL, 0);
2130                 if (!vp) return 0;
2131
2132                 switch (request->packet->code) {
2133                 case PW_AUTHENTICATION_REQUEST:
2134                         pool_type = HOME_TYPE_AUTH;
2135                         break;
2136
2137 #ifdef WITH_ACCOUNTING
2138                 case PW_ACCOUNTING_REQUEST:
2139                         pool_type = HOME_TYPE_ACCT;
2140                         break;
2141 #endif
2142
2143 #ifdef WITH_COA
2144                 case PW_COA_REQUEST:
2145                 case PW_DISCONNECT_REQUEST:
2146                         pool_type = HOME_TYPE_COA;
2147                         break;
2148 #endif
2149
2150                 default:
2151                         return 0;
2152                 }
2153
2154                 pool = home_pool_byname(vp->vp_strvalue, pool_type);
2155                 if (!pool) {
2156                         RDEBUG2("ERROR: Cannot proxy to unknown pool %s",
2157                                 vp->vp_strvalue);
2158                         return 0;
2159                 }
2160
2161                 realmname = NULL; /* no realms */
2162                 realm = NULL;
2163                 goto found_pool;
2164         }
2165
2166         realmname = (char *) realmpair->vp_strvalue;
2167
2168         realm = realm_find2(realmname);
2169         if (!realm) {
2170                 RDEBUG2("ERROR: Cannot proxy to unknown realm %s", realmname);
2171                 return 0;
2172         }
2173
2174         /*
2175          *      Figure out which pool to use.
2176          */
2177         if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
2178                 pool = realm->auth_pool;
2179
2180 #ifdef WITH_ACCOUNTING
2181         } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
2182                 pool = realm->acct_pool;
2183 #endif
2184
2185 #ifdef WITH_COA
2186         } else if ((request->packet->code == PW_COA_REQUEST) ||
2187                    (request->packet->code == PW_DISCONNECT_REQUEST)) {
2188                 pool = realm->acct_pool;
2189 #endif
2190
2191         } else {
2192                 rad_panic("Internal sanity check failed");
2193         }
2194
2195         if (!pool) {
2196                 RDEBUG2(" WARNING: Cancelling proxy to Realm %s, as the realm is local.",
2197                        realmname);
2198                 return 0;
2199         }
2200
2201 found_pool:
2202         home = home_server_ldb(realmname, pool, request);
2203         if (!home) {
2204                 RDEBUG2("ERROR: Failed to find live home server for realm %s",
2205                        realmname);
2206                 return -1;
2207         }
2208         request->home_pool = pool;
2209
2210 #ifdef WITH_COA
2211         /*
2212          *      Once we've decided to proxy a request, we cannot send
2213          *      a CoA packet.  So we free up any CoA packet here.
2214          */
2215         ev_request_free(&request->coa);
2216 #endif
2217         /*
2218          *      Remember that we sent the request to a Realm.
2219          */
2220         if (realmname) pairadd(&request->packet->vps,
2221                                pairmake("Realm", realmname, T_OP_EQ));
2222
2223         /*
2224          *      Strip the name, if told to.
2225          *
2226          *      Doing it here catches the case of proxied tunneled
2227          *      requests.
2228          */
2229         if (realm && (realm->striprealm == TRUE) &&
2230            (strippedname = pairfind(request->proxy->vps, PW_STRIPPED_USER_NAME, 0)) != NULL) {
2231                 /*
2232                  *      If there's a Stripped-User-Name attribute in
2233                  *      the request, then use THAT as the User-Name
2234                  *      for the proxied request, instead of the
2235                  *      original name.
2236                  *
2237                  *      This is done by making a copy of the
2238                  *      Stripped-User-Name attribute, turning it into
2239                  *      a User-Name attribute, deleting the
2240                  *      Stripped-User-Name and User-Name attributes
2241                  *      from the vps list, and making the new
2242                  *      User-Name the head of the vps list.
2243                  */
2244                 vp = pairfind(request->proxy->vps, PW_USER_NAME, 0);
2245                 if (!vp) {
2246                         vp = radius_paircreate(request, NULL,
2247                                                PW_USER_NAME, 0, PW_TYPE_STRING);
2248                         rad_assert(vp != NULL); /* handled by above function */
2249                         /* Insert at the START of the list */
2250                         vp->next = request->proxy->vps;
2251                         request->proxy->vps = vp;
2252                 }
2253                 memcpy(vp->vp_strvalue, strippedname->vp_strvalue,
2254                        sizeof(vp->vp_strvalue));
2255                 vp->length = strippedname->length;
2256
2257                 /*
2258                  *      Do NOT delete Stripped-User-Name.
2259                  */
2260         }
2261
2262         /*
2263          *      If there is no PW_CHAP_CHALLENGE attribute but
2264          *      there is a PW_CHAP_PASSWORD we need to add it
2265          *      since we can't use the request authenticator
2266          *      anymore - we changed it.
2267          */
2268         if ((request->packet->code == PW_AUTHENTICATION_REQUEST) &&
2269             pairfind(request->proxy->vps, PW_CHAP_PASSWORD, 0) &&
2270             pairfind(request->proxy->vps, PW_CHAP_CHALLENGE, 0) == NULL) {
2271                 vp = radius_paircreate(request, &request->proxy->vps,
2272                                        PW_CHAP_CHALLENGE, 0, PW_TYPE_OCTETS);
2273                 vp->length = AUTH_VECTOR_LEN;
2274                 memcpy(vp->vp_strvalue, request->packet->vector, AUTH_VECTOR_LEN);
2275         }
2276
2277         /*
2278          *      The RFC's say we have to do this, but FreeRADIUS
2279          *      doesn't need it.
2280          */
2281         vp = radius_paircreate(request, &request->proxy->vps,
2282                                PW_PROXY_STATE, 0, PW_TYPE_OCTETS);
2283         snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%d",
2284                  request->packet->id);
2285         vp->length = strlen(vp->vp_strvalue);
2286
2287         /*
2288          *      Should be done BEFORE inserting into proxy hash, as
2289          *      pre-proxy may use this information, or change it.
2290          */
2291         request->proxy->code = request->packet->code;
2292
2293         /*
2294          *      Call the pre-proxy routines.
2295          */
2296         vp = pairfind(request->config_items, PW_PRE_PROXY_TYPE, 0);
2297         if (vp) {
2298                 RDEBUG2("  Found Pre-Proxy-Type %s", vp->vp_strvalue);
2299                 pre_proxy_type = vp->vp_integer;
2300         }
2301
2302         rad_assert(request->home_pool != NULL);
2303
2304         if (request->home_pool->virtual_server) {
2305                 const char *old_server = request->server;
2306                 
2307                 request->server = request->home_pool->virtual_server;
2308                 RDEBUG2(" server %s {", request->server);
2309                 rcode = module_pre_proxy(pre_proxy_type, request);
2310                 RDEBUG2(" }");
2311                         request->server = old_server;
2312         } else {
2313                 rcode = module_pre_proxy(pre_proxy_type, request);
2314         }
2315         switch (rcode) {
2316         case RLM_MODULE_FAIL:
2317         case RLM_MODULE_INVALID:
2318         case RLM_MODULE_NOTFOUND:
2319         case RLM_MODULE_USERLOCK:
2320         default:
2321                 /* FIXME: debug print failed stuff */
2322                 return -1;
2323
2324         case RLM_MODULE_REJECT:
2325         case RLM_MODULE_HANDLED:
2326                 return 0;
2327
2328         /*
2329          *      Only proxy the packet if the pre-proxy code succeeded.
2330          */
2331         case RLM_MODULE_NOOP:
2332         case RLM_MODULE_OK:
2333         case RLM_MODULE_UPDATED:
2334                 break;
2335         }
2336
2337         /*
2338          *      If it's a fake request, don't send the proxy
2339          *      packet.  The outer tunnel session will take
2340          *      care of doing that.
2341          */
2342         if (request->packet->dst_port == 0) {
2343                 request->home_server = NULL;
2344                 return 1;
2345         }
2346
2347         if (request->home_server->server) {
2348                 return proxy_to_virtual_server(request);
2349         }
2350
2351         if (!proxy_request(request)) {
2352                 RDEBUG("ERROR: Failed to proxy request");
2353                 return -1;
2354         }
2355         
2356         return 1;
2357 }
2358 #endif
2359
2360 static void request_post_handler(REQUEST *request)
2361 {
2362         int child_state = -1;
2363         struct timeval when;
2364         VALUE_PAIR *vp;
2365
2366         if ((request->master_state == REQUEST_STOP_PROCESSING) ||
2367             (request->parent &&
2368              (request->parent->master_state == REQUEST_STOP_PROCESSING))) {
2369                 RDEBUG2("request was cancelled.");
2370 #ifdef HAVE_PTHREAD_H
2371                 request->child_pid = NO_SUCH_CHILD_PID;
2372 #endif
2373                 child_state = REQUEST_DONE;
2374                 goto cleanup;
2375         }
2376
2377         if (request->child_state != REQUEST_RUNNING) {
2378                 rad_panic("Internal sanity check failed");
2379         }
2380
2381 #ifdef WITH_COA
2382         /*
2383          *      If it's not in the request hash, it's a CoA request.
2384          *      We hope.
2385          */
2386         if (!request->in_request_hash &&
2387             request->proxy &&
2388             ((request->proxy->code == PW_COA_REQUEST) ||
2389              (request->proxy->code == PW_DISCONNECT_REQUEST))) {
2390                 request->next_callback = NULL;
2391                 child_state = REQUEST_DONE;
2392                 goto cleanup;
2393         }
2394 #endif
2395
2396         /*
2397          *      Catch Auth-Type := Reject BEFORE proxying the packet.
2398          */
2399         if ((request->packet->code == PW_AUTHENTICATION_REQUEST) &&
2400             (request->reply->code == 0) &&
2401             ((vp = pairfind(request->config_items, PW_AUTH_TYPE, 0)) != NULL) &&
2402             (vp->vp_integer == PW_AUTHTYPE_REJECT)) {
2403                 request->reply->code = PW_AUTHENTICATION_REJECT;
2404         }
2405
2406 #ifdef WITH_PROXY
2407         if (request->root->proxy_requests &&
2408             !request->in_proxy_hash &&
2409             (request->reply->code == 0) &&
2410             (request->packet->dst_port != 0) &&
2411             (request->packet->code != PW_STATUS_SERVER)) {
2412                 int rcode = successfully_proxied_request(request);
2413
2414                 if (rcode == 1) return; /* request is invalid */
2415
2416                 /*
2417                  *      Failed proxying it (dead home servers, etc.)
2418                  *      Run it through Post-Proxy-Type = Fail, and
2419                  *      respond to the request.
2420                  *
2421                  *      Note that we're in a child thread here, so we
2422                  *      do NOT re-schedule the request.  Instead, we
2423                  *      do what we would have done, which is run the
2424                  *      pre-handler, a NULL request handler, and then
2425                  *      the post handler.
2426                  */
2427                 if ((rcode < 0) && setup_post_proxy_fail(request)) {
2428                         request_pre_handler(request);
2429                 }
2430
2431                 /*
2432                  *      Else we weren't supposed to proxy it,
2433                  *      OR we proxied it internally to a virutal server.
2434                  */
2435         }
2436
2437 #ifdef WITH_COA
2438         else if (request->proxy && request->coa) {
2439                 RDEBUG("WARNING: Cannot proxy and originate CoA packets at the same time.  Cancelling CoA request");
2440                 ev_request_free(&request->coa);
2441         }
2442 #endif
2443 #endif
2444
2445         /*
2446          *      Fake requests don't get encoded or signed.  The caller
2447          *      also requires the reply VP's, so we don't free them
2448          *      here!
2449          */
2450         if (request->packet->dst_port == 0) {
2451                 /* FIXME: RDEBUG going to the next request */
2452 #ifdef HAVE_PTHREAD_H
2453                 request->child_pid = NO_SUCH_CHILD_PID;
2454 #endif
2455                 request->child_state = REQUEST_DONE;
2456                 return;
2457         }
2458
2459 #ifdef WITH_PROXY
2460         /*
2461          *      Copy Proxy-State from the request to the reply.
2462          */
2463         vp = paircopy2(request->packet->vps, PW_PROXY_STATE, 0);
2464         if (vp) pairadd(&request->reply->vps, vp);
2465 #endif
2466
2467         /*
2468          *      Access-Requests get delayed or cached.
2469          */
2470         switch (request->packet->code) {
2471         case PW_AUTHENTICATION_REQUEST:
2472                 gettimeofday(&request->next_when, NULL);
2473
2474                 if (request->reply->code == 0) {
2475                         /*
2476                          *      Check if the lack of response is intentional.
2477                          */
2478                         vp = pairfind(request->config_items,
2479                                       PW_RESPONSE_PACKET_TYPE, 0);
2480                         if (!vp) {
2481                                 RDEBUG2("There was no response configured: rejecting request");
2482                                 request->reply->code = PW_AUTHENTICATION_REJECT;
2483
2484                         } else if (vp->vp_integer == 256) {
2485                                 RDEBUG2("Not responding to request");
2486
2487                                 /*
2488                                  *      Force cleanup after a long
2489                                  *      time, so that we don't
2490                                  *      re-process the packet.
2491                                  */
2492                                 request->next_when.tv_sec += request->root->max_request_time;
2493                                 request->next_callback = cleanup_delay;
2494                                 child_state = REQUEST_CLEANUP_DELAY;
2495                                 break;
2496                         } else {
2497                                 request->reply->code = vp->vp_integer;
2498
2499                         }
2500                 }
2501
2502                 /*
2503                  *      Run rejected packets through
2504                  *
2505                  *      Post-Auth-Type = Reject
2506                  */
2507                 if (request->reply->code == PW_AUTHENTICATION_REJECT) {
2508                         pairdelete(&request->config_items, PW_POST_AUTH_TYPE, 0);
2509                         vp = radius_pairmake(request, &request->config_items,
2510                                              "Post-Auth-Type", "Reject",
2511                                              T_OP_SET);
2512                         if (vp) rad_postauth(request);
2513
2514                         /*
2515                          *      If configured, delay Access-Reject packets.
2516                          *
2517                          *      If request->root->reject_delay = 0, we discover
2518                          *      that we have to send the packet now.
2519                          */
2520                         when = request->received;
2521                         when.tv_sec += request->root->reject_delay;
2522
2523                         if (timercmp(&when, &request->next_when, >)) {
2524                                 RDEBUG2("Delaying reject  for %d seconds",
2525                                        request->root->reject_delay);
2526                                 request->next_when = when;
2527                                 request->next_callback = reject_delay;
2528 #ifdef HAVE_PTHREAD_H
2529                                 request->child_pid = NO_SUCH_CHILD_PID;
2530 #endif
2531                                 request->child_state = REQUEST_REJECT_DELAY;
2532                                 return;
2533                         }
2534                 }
2535
2536 #ifdef WITH_COA
2537         case PW_COA_REQUEST:
2538         case PW_DISCONNECT_REQUEST:
2539 #endif
2540                 request->next_when.tv_sec += request->root->cleanup_delay;
2541                 request->next_callback = cleanup_delay;
2542                 child_state = REQUEST_CLEANUP_DELAY;
2543                 break;
2544
2545         case PW_ACCOUNTING_REQUEST:
2546                 request->next_callback = NULL; /* just to be safe */
2547                 child_state = REQUEST_DONE;
2548                 break;
2549
2550                 /*
2551                  *      FIXME: Status-Server should probably not be
2552                  *      handled here...
2553                  */
2554         case PW_STATUS_SERVER:
2555                 request->next_callback = NULL;
2556                 child_state = REQUEST_DONE;
2557                 break;
2558
2559         default:
2560                 /*
2561                  *      DHCP, VMPS, etc.
2562                  */
2563                 request->next_callback = NULL;
2564                 child_state = REQUEST_DONE;
2565                 break;
2566         }
2567
2568         /*
2569          *      Suppress "no reply" packets here, unless we're reading
2570          *      from the "detail" file.  In that case, we've got to
2571          *      tell the detail file handler that the request is dead,
2572          *      and it should re-send it.
2573          *      If configured, encode, sign, and send.
2574          */
2575         if ((request->reply->code != 0)
2576 #ifdef WITH_DETAIL
2577             || (request->listener->type == RAD_LISTEN_DETAIL)
2578 #endif
2579             ) {
2580                 DEBUG_PACKET(request, request->reply, 1);
2581                 request->listener->send(request->listener, request);
2582         }
2583
2584 #ifdef WITH_COA
2585         /*
2586          *      Now that we've completely processed the request,
2587          *      see if we need to originate a CoA request.  But ONLY
2588          *      if it wasn't proxied.
2589          */
2590         if (!request->proxy &&
2591             (request->packet->code != PW_COA_REQUEST) &&
2592             (request->packet->code != PW_DISCONNECT_REQUEST) &&
2593             (request->coa ||
2594              (pairfind(request->config_items, PW_SEND_COA_REQUEST, 0) != NULL))) {
2595                 if (!originated_coa_request(request)) {
2596                         RDEBUG2("Do CoA Fail handler here");
2597                 }
2598                 /* request->coa is stil set, so we can update events */
2599         }
2600 #endif
2601
2602  cleanup:
2603         /*
2604          *      Clean up.  These are no longer needed.
2605          */
2606         pairfree(&request->config_items);
2607
2608         pairfree(&request->packet->vps);
2609         request->username = NULL;
2610         request->password = NULL;
2611
2612         pairfree(&request->reply->vps);
2613
2614 #ifdef WITH_PROXY
2615         if (request->proxy) {
2616                 pairfree(&request->proxy->vps);
2617
2618                 if (request->proxy_reply) {
2619                         pairfree(&request->proxy_reply->vps);
2620                 }
2621
2622 #if 0
2623                 /*
2624                  *      We're not tracking responses from the home
2625                  *      server, we can therefore free this memory in
2626                  *      the child thread.
2627                  */
2628                 if (!request->in_proxy_hash) {
2629                         rad_free(&request->proxy);
2630                         rad_free(&request->proxy_reply);
2631                         request->home_server = NULL;
2632                 }
2633 #endif
2634         }
2635 #endif
2636
2637         RDEBUG2("Finished request.");
2638         rad_assert(child_state >= 0);
2639         request->child_state = child_state;
2640
2641         /*
2642          *      Single threaded mode: update timers now.
2643          */
2644         if (!have_children) wait_a_bit(request);
2645 }
2646
2647
2648 #ifdef WITH_PROXY
2649 static void rad_retransmit_packet(REQUEST *request)
2650 {
2651         char buffer[256];
2652
2653 #ifdef WITH_TCP
2654         if (request->home_server->proto == IPPROTO_TCP) {
2655                 DEBUG2("Suppressing duplicate proxied request to home server %s port %d proto TCP - ID: %d",
2656                        inet_ntop(request->proxy->dst_ipaddr.af,
2657                                  &request->proxy->dst_ipaddr.ipaddr,
2658                                  buffer, sizeof(buffer)),
2659                        request->proxy->dst_port,
2660                        request->proxy->id);
2661                 return;         /* don't do anything else */
2662         }
2663 #endif
2664
2665         RDEBUG2("Sending duplicate proxied request to home server %s port %d - ID: %d",
2666                 inet_ntop(request->proxy->dst_ipaddr.af,
2667                           &request->proxy->dst_ipaddr.ipaddr,
2668                           buffer, sizeof(buffer)),
2669                 request->proxy->dst_port,
2670                 request->proxy->id);
2671         request->num_proxied_requests++;
2672
2673         DEBUG_PACKET(request, request->proxy, 1);
2674         request->proxy_listener->send(request->proxy_listener,
2675                                       request);
2676 }
2677
2678
2679 static int rad_retransmit(REQUEST *request)
2680 {
2681         /*
2682          *      If we've just discovered that the home server
2683          *      is dead, OR the socket has been closed, look for
2684          *      another connection to a home server.
2685          */
2686         if ((request->home_server->state == HOME_STATE_IS_DEAD) ||
2687             (request->proxy_listener->status != RAD_LISTEN_STATUS_KNOWN)) {
2688                 home_server *home;
2689                 
2690                 remove_from_proxy_hash(request);
2691                 
2692                 home = home_server_ldb(NULL, request->home_pool, request);
2693                 if (!home) {
2694                         RDEBUG2("ERROR: Failed to find live home server for request");
2695                 no_home_servers:
2696                         /*
2697                          *      Do post-request processing,
2698                          *      and any insertion of necessary
2699                          *      events.
2700                          */
2701                         post_proxy_fail_handler(request);
2702                         return 1;
2703                 }
2704
2705                 request->proxy->code = request->packet->code;
2706
2707                 /*
2708                  *      Free the old packet, to force re-encoding
2709                  */
2710                 free(request->proxy->data);
2711                 request->proxy->data = NULL;
2712                 request->proxy->data_len = 0;
2713
2714                 /*
2715                  *      This request failed over to a virtual
2716                  *      server.  Push it back onto the queue
2717                  *      to be processed.
2718                  */
2719                 if (request->home_server->server) {
2720                         proxy_fallback_handler(request);
2721                         return 1;
2722                 }
2723
2724                 /*
2725                  *      Try to proxy the request.
2726                  */
2727                 if (!proxy_request(request)) {
2728                         RDEBUG("ERROR: Failed to re-proxy request");
2729                         goto no_home_servers;
2730                 }
2731                 return 1;
2732         } /* else the home server is still alive */
2733
2734         rad_retransmit_packet(request);
2735
2736         return 1;
2737 }
2738 #endif
2739
2740 static void received_retransmit(REQUEST *request, const RADCLIENT *client)
2741 {
2742
2743         RAD_STATS_TYPE_INC(request->listener, total_dup_requests);
2744         RAD_STATS_CLIENT_INC(request->listener, client, total_dup_requests);
2745         
2746         switch (request->child_state) {
2747         case REQUEST_QUEUED:
2748         case REQUEST_RUNNING:
2749 #ifdef WITH_PROXY
2750         discard:
2751 #endif
2752                 radlog(L_ERR, "Discarding duplicate request from "
2753                        "client %s port %d - ID: %d due to unfinished request %u",
2754                        client->shortname,
2755                        request->packet->src_port,request->packet->id,
2756                        request->number);
2757                 break;
2758
2759 #ifdef WITH_PROXY
2760         case REQUEST_PROXIED:
2761                 /*
2762                  *      We're not supposed to have duplicate
2763                  *      accounting packets.  The other states handle
2764                  *      duplicates fine (discard, or send duplicate
2765                  *      reply).  But we do NOT want to retransmit an
2766                  *      accounting request here, because that would
2767                  *      involve updating the Acct-Delay-Time, and
2768                  *      therefore changing the packet Id, etc.
2769                  *
2770                  *      Instead, we just discard the packet.  We may
2771                  *      eventually respond, or the client will send a
2772                  *      new accounting packet.            
2773                  *
2774                  *      The same comments go for Status-Server, and
2775                  *      other packet types.
2776                  *
2777                  *      FIXME: coa: when we proxy CoA && Disconnect
2778                  *      packets, this logic has to be fixed.
2779                  */
2780                 if (request->packet->code != PW_AUTHENTICATION_REQUEST) {
2781                         goto discard;
2782                 }
2783
2784                 check_for_zombie_home_server(request);
2785
2786                 /*
2787                  *      Home server is still alive, and the proxy
2788                  *      socket is OK.  Just re-send the packet.
2789                  */
2790                 if ((request->home_server->state != HOME_STATE_IS_DEAD) &&
2791                     (request->proxy_listener->status == RAD_LISTEN_STATUS_KNOWN)) {
2792                         rad_retransmit_packet(request);
2793                         break;
2794                 }
2795
2796                 /*
2797                  *      Otherwise, we need to fail over to another
2798                  *      home server, and possibly run "post-proxy-type
2799                  *      fail".  Add an event waiting for the child to
2800                  *      have a result.
2801                  */
2802                 INSERT_EVENT(wait_a_bit, request);
2803
2804                 request->priority = RAD_LISTEN_PROXY;
2805                 thread_pool_addrequest(request, rad_retransmit);
2806                 break;
2807 #endif
2808
2809         case REQUEST_REJECT_DELAY:
2810                 RDEBUG2("Waiting to send Access-Reject "
2811                        "to client %s port %d - ID: %d",
2812                        client->shortname,
2813                        request->packet->src_port, request->packet->id);
2814                 break;
2815
2816         case REQUEST_CLEANUP_DELAY:
2817         case REQUEST_DONE:
2818                 if (request->reply->code == 0) {
2819                         RDEBUG2("Ignoring retransmit from client %s port %d "
2820                                 "- ID: %d, no reply was configured",
2821                                 client->shortname,
2822                                 request->packet->src_port, request->packet->id);
2823                         return;
2824                 }
2825
2826                 /*
2827                  *      FIXME: This sends duplicate replies to
2828                  *      accounting requests, even if Acct-Delay-Time
2829                  *      or Event-Timestamp is in the packet.  In those
2830                  *      cases, the Id should be changed, and the packet
2831                  *      re-calculated.
2832                  */
2833                 RDEBUG2("Sending duplicate reply "
2834                        "to client %s port %d - ID: %d",
2835                        client->shortname,
2836                        request->packet->src_port, request->packet->id);
2837                 DEBUG_PACKET(request, request->reply, 1);
2838                 request->listener->send(request->listener, request);
2839                 break;
2840         }
2841 }
2842
2843
2844 static void received_conflicting_request(REQUEST *request,
2845                                          const RADCLIENT *client)
2846 {
2847         radlog(L_ERR, "Received conflicting packet from "
2848                "client %s port %d - ID: %d due to unfinished request %u.  Giving up on old request.",
2849                client->shortname,
2850                request->packet->src_port, request->packet->id,
2851                request->number);
2852
2853         /*
2854          *      Nuke it from the request hash, so we can receive new
2855          *      packets.
2856          */
2857         remove_from_request_hash(request);
2858
2859         switch (request->child_state) {
2860                 /*
2861                  *      Tell it to stop, and wait for it to do so.
2862                  */
2863         default:
2864                 request->master_state = REQUEST_STOP_PROCESSING;
2865                 request->delay += request->delay >> 1;
2866
2867                 tv_add(&request->when, request->delay);
2868
2869                 INSERT_EVENT(wait_for_child_to_die, request);
2870                 return;
2871
2872                 /*
2873                  *      Catch race conditions.  It may have switched
2874                  *      from running to done while this code is being
2875                  *      executed.
2876                  */
2877         case REQUEST_REJECT_DELAY:
2878         case REQUEST_CLEANUP_DELAY:
2879         case REQUEST_DONE:
2880                 break;
2881         }
2882 }
2883
2884
2885 static int can_handle_new_request(RADIUS_PACKET *packet,
2886                                   RADCLIENT *client,
2887                                   struct main_config_t *root)
2888 {
2889         /*
2890          *      Count the total number of requests, to see if
2891          *      there are too many.  If so, return with an
2892          *      error.
2893          */
2894         if (root->max_requests) {
2895                 int request_count = fr_packet_list_num_elements(pl);
2896
2897                 /*
2898                  *      This is a new request.  Let's see if
2899                  *      it makes us go over our configured
2900                  *      bounds.
2901                  */
2902                 if (request_count > root->max_requests) {
2903                         radlog(L_ERR, "Dropping request (%d is too many): "
2904                                "from client %s port %d - ID: %d", request_count,
2905                                client->shortname,
2906                                packet->src_port, packet->id);
2907                         radlog(L_INFO, "WARNING: Please check the configuration file.\n"
2908                                "\tThe value for 'max_requests' is probably set too low.\n");
2909                         return 0;
2910                 } /* else there were a small number of requests */
2911         } /* else there was no configured limit for requests */
2912
2913         /*
2914          *      FIXME: Add per-client checks.  If one client is sending
2915          *      too many packets, start discarding them.
2916          *
2917          *      We increment the counters here, and decrement them
2918          *      when the response is sent... somewhere in this file.
2919          */
2920
2921         /*
2922          *      FUTURE: Add checks for system load.  If the system is
2923          *      busy, start dropping requests...
2924          *
2925          *      We can probably keep some statistics ourselves...  if
2926          *      there are more requests coming in than we can handle,
2927          *      start dropping some.
2928          */
2929
2930         return 1;
2931 }
2932
2933
2934 int received_request(rad_listen_t *listener,
2935                      RADIUS_PACKET *packet, REQUEST **prequest,
2936                      RADCLIENT *client)
2937 {
2938         RADIUS_PACKET **packet_p;
2939         REQUEST *request = NULL;
2940         struct main_config_t *root = &mainconfig;
2941
2942         packet_p = fr_packet_list_find(pl, packet);
2943         if (packet_p) {
2944                 request = fr_packet2myptr(REQUEST, packet, packet_p);
2945                 rad_assert(request->in_request_hash);
2946
2947                 if ((request->packet->data_len == packet->data_len) &&
2948                     (memcmp(request->packet->vector, packet->vector,
2949                             sizeof(packet->vector)) == 0)) {
2950                         received_retransmit(request, client);
2951                         return 0;
2952                 }
2953
2954                 /*
2955                  *      The new request is different from the old one,
2956                  *      but maybe the old is finished.  If so, delete
2957                  *      the old one.
2958                  */
2959                 switch (request->child_state) {
2960                         struct timeval when;
2961
2962                 default:
2963                         /*
2964                          *      Special hacks for race conditions.
2965                          *      The reply is encoded, and therefore
2966                          *      likely sent.  We received a *new*
2967                          *      packet from the client, likely before
2968                          *      the next line or two of code which
2969                          *      updated the child state.  In this
2970                          *      case, just accept the new request.
2971                          */
2972                         if ((request->reply->code != 0) &&
2973                             request->reply->data) {
2974                                 radlog(L_INFO, "WARNING: Allowing fast client %s port %d - ID: %d for recent request %u.",
2975                                        client->shortname,
2976                                        packet->src_port, packet->id,
2977                                        request->number);
2978                                 remove_from_request_hash(request);
2979                                 request = NULL;
2980                                 break;
2981                         }
2982
2983                         gettimeofday(&when, NULL);
2984                         when.tv_sec -= 1;
2985
2986                         /*
2987                          *      If the cached request was received
2988                          *      within the last second, then we
2989                          *      discard the NEW request instead of the
2990                          *      old one.  This will happen ONLY when
2991                          *      the client is severely broken, and is
2992                          *      sending conflicting packets very
2993                          *      quickly.
2994                          */
2995                         if (timercmp(&when, &request->received, <)) {
2996                                 radlog(L_ERR, "Discarding conflicting packet from "
2997                                        "client %s port %d - ID: %d due to recent request %u.",
2998                                        client->shortname,
2999                                        packet->src_port, packet->id,
3000                                        request->number);
3001                                 return 0;
3002                         }
3003
3004                         received_conflicting_request(request, client);
3005                         request = NULL;
3006                         break;
3007
3008                 case REQUEST_REJECT_DELAY:
3009                 case REQUEST_CLEANUP_DELAY:
3010                         request->child_state = REQUEST_DONE;
3011                 case REQUEST_DONE:
3012                         cleanup_delay(request);
3013                         request = NULL;
3014                         break;
3015                 }
3016         }
3017
3018         /*
3019          *      We may want to quench the new request.
3020          */
3021         if (
3022 #ifdef WITH_DETAIL
3023             (listener->type != RAD_LISTEN_DETAIL) &&
3024 #endif
3025             !can_handle_new_request(packet, client, root)) {
3026                 return 0;
3027         }
3028
3029         /*
3030          *      Create and initialize the new request.
3031          */
3032         request = request_alloc(); /* never fails */
3033
3034         if ((request->reply = rad_alloc(0)) == NULL) {
3035                 radlog(L_ERR, "No memory");
3036                 return 0;
3037         }
3038
3039         request->listener = listener;
3040         request->client = client;
3041         request->packet = packet;
3042         request->packet->timestamp = request->timestamp;
3043         request->number = request_num_counter++;
3044         request->priority = listener->type;
3045 #ifdef HAVE_PTHREAD_H
3046         request->child_pid = NO_SUCH_CHILD_PID;
3047 #endif
3048
3049         /*
3050          *      Status-Server packets go to the head of the queue.
3051          */
3052         if (request->packet->code == PW_STATUS_SERVER) request->priority = 0;
3053
3054         /*
3055          *      Set virtual server identity
3056          */
3057         if (client->server) {
3058                 request->server = client->server;
3059         } else if (listener->server) {
3060                 request->server = listener->server;
3061         } else {
3062                 request->server = NULL;
3063         }
3064
3065         /*
3066          *      Remember the request in the list.
3067          */
3068         if (!fr_packet_list_insert(pl, &request->packet)) {
3069                 radlog(L_ERR, "Failed to insert request %u in the list of live requests: discarding", request->number);
3070                 ev_request_free(&request);
3071                 return 0;
3072         }
3073
3074         request->in_request_hash = TRUE;
3075         request->root = root;
3076         root->refcount++;
3077 #ifdef WITH_TCP
3078         request->listener->count++;
3079 #endif
3080
3081         /*
3082          *      The request passes many of our sanity checks.
3083          *      From here on in, if anything goes wrong, we
3084          *      send a reject message, instead of dropping the
3085          *      packet.
3086          */
3087
3088         /*
3089          *      Build the reply template from the request.
3090          */
3091
3092         request->reply->sockfd = request->packet->sockfd;
3093         request->reply->dst_ipaddr = request->packet->src_ipaddr;
3094         request->reply->src_ipaddr = request->packet->dst_ipaddr;
3095         request->reply->dst_port = request->packet->src_port;
3096         request->reply->src_port = request->packet->dst_port;
3097         request->reply->id = request->packet->id;
3098         request->reply->code = 0; /* UNKNOWN code */
3099         memcpy(request->reply->vector, request->packet->vector,
3100                sizeof(request->reply->vector));
3101         request->reply->vps = NULL;
3102         request->reply->data = NULL;
3103         request->reply->data_len = 0;
3104
3105         request->master_state = REQUEST_ACTIVE;
3106         request->child_state = REQUEST_QUEUED;
3107         request->next_callback = NULL;
3108
3109         gettimeofday(&request->received, NULL);
3110         request->timestamp = request->received.tv_sec;
3111         request->when = request->received;
3112
3113         request->delay = USEC;
3114
3115         tv_add(&request->when, request->delay);
3116
3117         INSERT_EVENT(wait_a_bit, request);
3118
3119         *prequest = request;
3120         return 1;
3121 }
3122
3123
3124 #ifdef WITH_PROXY
3125 REQUEST *received_proxy_response(RADIUS_PACKET *packet)
3126 {
3127         char            buffer[128];
3128         REQUEST         *request;
3129
3130         /*
3131          *      Lookup *without* removal.  In versions prior to 2.2.0,
3132          *      this did lookup *and* removal.  That method allowed
3133          *      attackers to spoof replies that caused entries to be
3134          *      removed from the proxy hash prior to validation.
3135          */
3136         request = lookup_in_proxy_hash(packet);
3137
3138         if (!request) {
3139                 radlog(L_PROXY, "No outstanding request was found for reply from host %s port %d - ID %d",
3140                        inet_ntop(packet->src_ipaddr.af,
3141                                  &packet->src_ipaddr.ipaddr,
3142                                  buffer, sizeof(buffer)),
3143                        packet->src_port, packet->id);
3144                 return NULL;
3145         }
3146
3147         /*
3148          *      There's a reply: discard it if it's a conflicting one.
3149          */
3150         if (request->proxy_reply) {
3151                 /*
3152                  *      ? The home server gave us a new proxy
3153                  *      reply which doesn't match the old
3154                  *      one.  Delete it.
3155                  */
3156                 if (memcmp(request->proxy_reply->vector,
3157                            packet->vector,
3158                            sizeof(request->proxy_reply->vector)) != 0) {
3159                         RDEBUG2("Ignoring conflicting proxy reply");
3160                         
3161                 
3162                         /* assert that there's an event queued for request? */
3163                         return NULL;
3164                 } /* else it had previously passed verification */
3165
3166                 /*
3167                  *      Verify the packet before doing ANYTHING with
3168                  *      it.  This means we're doing more MD5 checks in
3169                  *      the server core.  However, we can fix that by
3170                  *      moving to multiple threads listening on
3171                  *      sockets.
3172                  *
3173                  *      We do this AFTER looking the request up in the
3174                  *      hash, and AFTER checking if we saw a previous
3175                  *      request.  This helps minimize the DoS effect
3176                  *      of people attacking us with spoofed packets.
3177                  *
3178                  *      FIXME: move the "read from proxy socket" code
3179                  *      into one (or more) threads.  Have it read from
3180                  *      the socket, do the validation, and write a
3181                  *      pointer to the packet into a pipe? Or queue it
3182                  *      to the main server?
3183                  */
3184         } else if (rad_verify(packet, request->proxy,
3185                               request->home_server->secret) != 0) {
3186                 DEBUG("Ignoring spoofed proxy reply.  Signature is invalid");
3187                 return NULL;
3188         }
3189
3190         /*
3191          *      Check (again) if it's a duplicate reply.  We do this
3192          *      after deleting the packet from the proxy hash.
3193          */
3194         if (request->proxy_reply) {
3195                 RDEBUG2("Discarding duplicate reply from host %s port %d  - ID: %d",
3196                         inet_ntop(packet->src_ipaddr.af,
3197                                   &packet->src_ipaddr.ipaddr,
3198                                   buffer, sizeof(buffer)),
3199                         packet->src_port, packet->id);
3200         }
3201
3202         gettimeofday(&now, NULL);
3203
3204         /*
3205          *      "ping" packets have a different algorithm for marking
3206          *      a home server alive.  They also skip all of the CoA,
3207          *      etc. checks.
3208          */
3209         if (!request->packet) {
3210                 request->proxy_reply = packet;
3211 #ifdef WITH_TCP
3212                 rad_assert(request->home_server != NULL);
3213                 if (request->home_server->proto != IPPROTO_TCP)
3214 #endif
3215                         received_response_to_ping(request);
3216                 request->proxy_reply = NULL; /* caller will free it */
3217                 ev_request_free(&request);
3218                 return NULL;
3219         }
3220
3221         /*
3222          *      Maybe move this earlier in the decision process?
3223          *      Having it here means that late or duplicate proxy
3224          *      replies no longer get the home server marked as
3225          *      "alive".  This might be good for stability, though.
3226          *
3227          *      FIXME: Do we really want to do this whenever we
3228          *      receive a packet?  Setting this here means that we
3229          *      mark it alive on *any* packet, even if it's lost all
3230          *      of the *other* packets in the last 10s.
3231          *
3232          *      This behavior could be configurable.
3233          */
3234         request->home_server->state = HOME_STATE_ALIVE;
3235         request->home_server->last_packet = now.tv_sec;
3236         
3237 #ifdef WITH_COA
3238         /*
3239          *      When originating CoA, the "proxy" reply is the reply
3240          *      to the CoA request that we originated.  At this point,
3241          *      the original request is finished, and it has a reply.
3242          *
3243          *      However, if we haven't separated the two requests, do
3244          *      so now.  This is done so that cleaning up the original
3245          *      request won't cause the CoA request to be free'd.  See
3246          *      util.c, request_free()
3247          */
3248         if (request->parent && (request->parent->coa == request)) {
3249                 request->parent->coa = NULL;
3250                 request->parent = NULL;
3251
3252                 /*
3253                  *      The proxied packet was different from the
3254                  *      original packet, AND the proxied packet was
3255                  *      a CoA: allow it.
3256                  */
3257         } else if ((request->packet->code != request->proxy->code) &&
3258                    ((request->proxy->code == PW_COA_REQUEST) ||
3259                     (request->proxy->code == PW_DISCONNECT_REQUEST))) {
3260           /*
3261            *    It's already divorced: do nothing.
3262            */
3263           
3264         } else
3265                 /*
3266                  *      Skip the next set of checks, as the original
3267                  *      reply is cached.  We want to be able to still
3268                  *      process the CoA reply, AND to reference the
3269                  *      original request/reply.
3270                  *
3271                  *      This is getting to be really quite a bit of a
3272                  *      hack.
3273                  */
3274 #endif
3275
3276         /*
3277          *      If there's a reply to the NAS, ignore everything
3278          *      related to proxy responses
3279          */
3280         if (request->reply && request->reply->code != 0) {
3281                 RDEBUG2("Ignoring proxy reply that arrived after we sent a reply to the NAS");
3282                 return NULL;
3283         }
3284
3285 #ifdef WITH_STATS
3286         /*
3287          *      The average includes our time to receive packets and
3288          *      look them up in the hashes, which should be the same
3289          *      for all packets.
3290          *
3291          *      We update the response time only for the FIRST packet
3292          *      we receive.
3293          */
3294         if (request->home_server->ema.window > 0) {
3295                 radius_stats_ema(&request->home_server->ema,
3296                                  &now, &request->proxy_when);
3297         }
3298 #endif
3299
3300         switch (request->child_state) {
3301         case REQUEST_QUEUED:
3302         case REQUEST_RUNNING:
3303                 radlog(L_ERR, "Internal sanity check failed for child state");
3304                 /* FALL-THROUGH */
3305
3306         case REQUEST_REJECT_DELAY:
3307         case REQUEST_CLEANUP_DELAY:
3308         case REQUEST_DONE:
3309                 radlog(L_ERR, "Reply from home server %s port %d  - ID: %d arrived too late for request %u. Try increasing 'retry_delay' or 'max_request_time'",
3310                        inet_ntop(packet->src_ipaddr.af,
3311                                  &packet->src_ipaddr.ipaddr,
3312                                  buffer, sizeof(buffer)),
3313                        packet->src_port, packet->id,
3314                        request->number);
3315                 /* assert that there's an event queued for request? */
3316                 return NULL;
3317
3318         case REQUEST_PROXIED:
3319                 break;
3320         }
3321
3322         request->proxy_reply = packet;
3323
3324 #if 0
3325         /*
3326          *      Perform RTT calculations, as per RFC 2988 (for TCP).
3327          *      Note that we only do so on the first response.
3328          */
3329         if ((request->num_proxied_responses == 1)
3330                 int rtt;
3331                 home_server *home = request->home_server;
3332
3333                 rtt = now.tv_sec - request->proxy_when.tv_sec;
3334                 rtt *= USEC;
3335                 rtt += now.tv_usec;
3336                 rtt -= request->proxy_when.tv_usec;
3337
3338                 if (!home->has_rtt) {
3339                         home->has_rtt = TRUE;
3340
3341                         home->srtt = rtt;
3342                         home->rttvar = rtt / 2;
3343
3344                 } else {
3345                         home->rttvar -= home->rttvar >> 2;
3346                         home->rttvar += (home->srtt - rtt);
3347                         home->srtt -= home->srtt >> 3;
3348                         home->srtt += rtt >> 3;
3349                 }
3350
3351                 home->rto = home->srtt;
3352                 if (home->rttvar > (USEC / 4)) {
3353                         home->rto += home->rttvar * 4;
3354                 } else {
3355                         home->rto += USEC;
3356                 }
3357         }
3358 #endif
3359
3360         request->child_state = REQUEST_QUEUED;
3361         request->when = now;
3362         request->delay = USEC;
3363         request->priority = RAD_LISTEN_PROXY;
3364         tv_add(&request->when, request->delay);
3365
3366         /*
3367          *      Wait a bit will take care of max_request_time
3368          */
3369         INSERT_EVENT(wait_a_bit, request);
3370
3371         return request;
3372 }
3373
3374 #endif /* WITH_PROXY */
3375
3376 #ifdef WITH_TCP
3377 static void tcp_socket_lifetime(void *ctx)
3378 {
3379         rad_listen_t *listener = ctx;
3380         char buffer[256];
3381
3382         listener->print(listener, buffer, sizeof(buffer));
3383
3384         DEBUG("Reached maximum lifetime on socket %s", buffer);
3385
3386         listener->status = RAD_LISTEN_STATUS_CLOSED;
3387         event_new_fd(listener);
3388 }
3389
3390 static void tcp_socket_idle_timeout(void *ctx)
3391 {
3392         rad_listen_t *listener = ctx;
3393         listen_socket_t *sock = listener->data;
3394         char buffer[256];
3395
3396         fr_event_now(el, &now); /* should always succeed... */
3397
3398         rad_assert(sock->home != NULL);
3399
3400         /*
3401          *      We implement idle timeout by polling, because it's
3402          *      cheaper than resetting the idle timeout every time
3403          *      we send / receive a packet.
3404          */
3405         if ((sock->last_packet + sock->home->idle_timeout) > now.tv_sec) {
3406                 struct timeval when;
3407                 void *fun = tcp_socket_idle_timeout;
3408                 
3409                 when.tv_sec = sock->last_packet;
3410                 when.tv_sec += sock->home->idle_timeout;
3411                 when.tv_usec = 0;
3412
3413                 if (sock->home->lifetime &&
3414                     (sock->opened + sock->home->lifetime < when.tv_sec)) {
3415                         when.tv_sec = sock->opened + sock->home->lifetime;
3416                         fun = tcp_socket_lifetime;
3417                 }
3418                 
3419                 if (!fr_event_insert(el, fun, listener, &when, &sock->ev)) {
3420                         rad_panic("Failed to insert event");
3421                 }
3422
3423                 return;
3424         }
3425
3426         listener->print(listener, buffer, sizeof(buffer));
3427         
3428         DEBUG("Reached idle timeout on socket %s", buffer);
3429
3430         listener->status = RAD_LISTEN_STATUS_CLOSED;
3431         event_new_fd(listener);
3432 }
3433 #endif
3434
3435 int event_new_fd(rad_listen_t *this)
3436 {
3437         char buffer[1024];
3438
3439         if (this->status == RAD_LISTEN_STATUS_KNOWN) return 1;
3440
3441         this->print(this, buffer, sizeof(buffer));
3442
3443         if (this->status == RAD_LISTEN_STATUS_INIT) {
3444                 if (just_started) {
3445                         DEBUG("Listening on %s", buffer);
3446                 } else {
3447                         radlog(L_INFO, " ... adding new socket %s", buffer);
3448                 }
3449
3450 #ifdef WITH_PROXY
3451                 /*
3452                  *      Add it to the list of sockets we can use.
3453                  *      Server sockets (i.e. auth/acct) are never
3454                  *      added to the packet list.
3455                  */
3456                 if (this->type == RAD_LISTEN_PROXY) {
3457                         listen_socket_t *sock = this->data;
3458
3459                         PTHREAD_MUTEX_LOCK(&proxy_mutex);
3460                         if (!fr_packet_list_socket_add(proxy_list, this->fd,
3461                                                        sock->proto,
3462                                                        &sock->other_ipaddr, sock->other_port,
3463                                                        this)) {
3464
3465                                 proxy_no_new_sockets = TRUE;
3466                                 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
3467
3468                                 /*
3469                                  *      This is bad.  However, the
3470                                  *      packet list now supports 256
3471                                  *      open sockets, which should
3472                                  *      minimize this problem.
3473                                  */
3474                                 radlog(L_ERR, "Failed adding proxy socket: %s",
3475                                        fr_strerror());
3476                                 return 0;
3477                         }
3478
3479                         if (sock->home) {
3480                                 sock->home->num_connections++;
3481                                 
3482                                 /*
3483                                  *      If necessary, add it to the list of
3484                                  *      new proxy listeners.
3485                                  */
3486                                 if (sock->home->lifetime || sock->home->idle_timeout) {
3487                                         this->next = proxy_listener_list;
3488                                         proxy_listener_list = this;
3489                                 }
3490                         }
3491                         PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
3492
3493                         /*
3494                          *      Tell the main thread that we've added
3495                          *      a proxy listener, but only if we need
3496                          *      to update the event list.  Do this
3497                          *      with the mutex unlocked, to reduce
3498                          *      contention.
3499                          */
3500                         if (sock->home) {
3501                                 if (sock->home->lifetime || sock->home->idle_timeout) {
3502                                         radius_signal_self(RADIUS_SIGNAL_SELF_NEW_FD);
3503                                 }
3504                         }
3505                 }
3506 #endif          
3507
3508 #ifdef WITH_DETAIL
3509                 /*
3510                  *      Detail files are always known, and aren't
3511                  *      put into the socket event loop.
3512                  */
3513                 if (this->type == RAD_LISTEN_DETAIL) {
3514                         this->status = RAD_LISTEN_STATUS_KNOWN;
3515                         
3516                         /*
3517                          *      Set up the first poll interval.
3518                          */
3519                         event_poll_detail(this);
3520                         return 1;
3521                 }
3522 #endif
3523
3524                 FD_MUTEX_LOCK(&fd_mutex);
3525                 if (!fr_event_fd_insert(el, 0, this->fd,
3526                                         event_socket_handler, this)) {
3527                         radlog(L_ERR, "Failed adding event handler for proxy socket!");
3528                         exit(1);
3529                 }
3530                 FD_MUTEX_UNLOCK(&fd_mutex);
3531                 
3532                 this->status = RAD_LISTEN_STATUS_KNOWN;
3533                 return 1;
3534         }
3535
3536         /*
3537          *      Something went wrong with the socket: make it harmless.
3538          */
3539         if (this->status == RAD_LISTEN_STATUS_REMOVE_FD) {
3540                 int devnull;
3541
3542                 /*
3543                  *      Remove it from the list of live FD's.
3544                  */
3545                 FD_MUTEX_LOCK(&fd_mutex);
3546                 fr_event_fd_delete(el, 0, this->fd);
3547                 FD_MUTEX_UNLOCK(&fd_mutex);
3548
3549 #ifdef WITH_TCP
3550                 /*
3551                  *      We track requests using this socket only for
3552                  *      TCP.  For UDP, we don't currently close
3553                  *      sockets.
3554                  */
3555 #ifdef WITH_PROXY
3556                 if (this->type != RAD_LISTEN_PROXY)
3557 #endif
3558                 {
3559                         if (this->count != 0) {
3560                                 fr_packet_list_walk(pl, this,
3561                                                     remove_all_requests);
3562                         }
3563
3564                         if (this->count == 0) {
3565                                 this->status = RAD_LISTEN_STATUS_FINISH;
3566                                 goto finish;
3567                         }
3568                 }               
3569 #ifdef WITH_PROXY
3570                 else {
3571                         int count;
3572
3573                         /*
3574                          *      Duplicate code
3575                          */
3576                         PTHREAD_MUTEX_LOCK(&proxy_mutex);
3577                         if (!fr_packet_list_socket_freeze(proxy_list,
3578                                                           this->fd)) {
3579                                 radlog(L_ERR, "Fatal error freezing socket: %s",
3580                                        fr_strerror());
3581                                 exit(1);
3582                         }
3583
3584                         /*
3585                          *      Doing this with the proxy mutex held
3586                          *      is a Bad Thing.  We should move to
3587                          *      finer-grained mutexes.
3588                          */
3589                         count = this->count;
3590                         if (count > 0) {
3591                                 fr_packet_list_walk(proxy_list, this,
3592                                                     remove_all_proxied_requests);
3593                         }
3594                         count = this->count; /* protected by mutex */
3595                         PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
3596
3597                         if (count == 0) {
3598                                 this->status = RAD_LISTEN_STATUS_FINISH;
3599                                 goto finish;
3600                         }
3601                 }
3602 #endif  /* WITH_PROXY */
3603 #endif  /* WITH_TCP */
3604
3605                 /*
3606                  *      Re-open the socket, pointing it to /dev/null.
3607                  *      This means that all writes proceed without
3608                  *      blocking, and all reads return "no data".
3609                  *
3610                  *      This leaves the socket active, so any child
3611                  *      threads won't go insane.  But it means that
3612                  *      they cannot send or receive any packets.
3613                  *
3614                  *      This is EXTRA work in the normal case, when
3615                  *      sockets are closed without error.  But it lets
3616                  *      us have one simple processing method for all
3617                  *      sockets.
3618                  */
3619                 devnull = open("/dev/null", O_RDWR);
3620                 if (devnull < 0) {
3621                         radlog(L_ERR, "FATAL failure opening /dev/null: %s",
3622                                strerror(errno));
3623                         exit(1);
3624                 }
3625                 if (dup2(devnull, this->fd) < 0) {
3626                         radlog(L_ERR, "FATAL failure closing socket: %s",
3627                                strerror(errno));
3628                         exit(1);
3629                 }
3630                 close(devnull);
3631
3632                 this->status = RAD_LISTEN_STATUS_CLOSED;
3633
3634                 /*
3635                  *      Fall through to the next section.
3636                  */
3637         }
3638
3639 #ifdef WITH_TCP
3640         /*
3641          *      Called ONLY from the main thread.  On the following
3642          *      conditions:
3643          *
3644          *      idle timeout
3645          *      max lifetime
3646          *
3647          *      (and falling through from "forcibly close FD" above)
3648          *      client closed connection on us
3649          *      client sent us a bad packet.
3650          */
3651         if (this->status == RAD_LISTEN_STATUS_CLOSED) {
3652                 int count = this->count;
3653
3654 #ifdef WITH_DETAIL
3655                 rad_assert(this->type != RAD_LISTEN_DETAIL);
3656 #endif
3657
3658 #ifdef WITH_PROXY
3659                 /*
3660                  *      Remove it from the list of active sockets, so
3661                  *      that it isn't used when proxying new packets.
3662                  */
3663                 if (this->type == RAD_LISTEN_PROXY) {
3664                         PTHREAD_MUTEX_LOCK(&proxy_mutex);
3665                         if (!fr_packet_list_socket_freeze(proxy_list,
3666                                                           this->fd)) {
3667                                 radlog(L_ERR, "Fatal error freezing socket: %s",
3668                                        fr_strerror());
3669                                 exit(1);
3670                         }
3671                         count = this->count; /* protected by mutex */
3672                         PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
3673                 }
3674 #endif
3675
3676                 /*
3677                  *      Requests are still using the socket.  Wait for
3678                  *      them to finish.
3679                  */
3680                 if (count != 0) {
3681                         struct timeval when;
3682                         listen_socket_t *sock = this->data;
3683
3684                         /*
3685                          *      Try again to clean up the socket in 30
3686                          *      seconds.
3687                          */
3688                         gettimeofday(&when, NULL);
3689                         when.tv_sec += 30;
3690                         
3691                         if (!fr_event_insert(el,
3692                                              (fr_event_callback_t) event_new_fd,
3693                                              this, &when, &sock->ev)) {
3694                                 rad_panic("Failed to insert event");
3695                         }
3696                        
3697                         return 1;
3698                 }
3699
3700                 /*
3701                  *      No one is using this socket: we can delete it
3702                  *      immediately.
3703                  */
3704                 this->status = RAD_LISTEN_STATUS_FINISH;
3705         }
3706         
3707 finish:
3708         if (this->status == RAD_LISTEN_STATUS_FINISH) {
3709                 listen_socket_t *sock = this->data;
3710
3711                 rad_assert(this->count == 0);
3712                 radlog(L_INFO, " ... closing socket %s", buffer);
3713
3714                 /*
3715                  *      Remove it from the list of live FD's.  Note
3716                  *      that it MAY also have been removed above.  We
3717                  *      do it again here, to catch the case of sockets
3718                  *      closing on idle timeout, or max
3719                  *      lifetime... AFTER all requests have finished
3720                  *      using it.
3721                  */
3722                 FD_MUTEX_LOCK(&fd_mutex);
3723                 fr_event_fd_delete(el, 0, this->fd);
3724                 FD_MUTEX_UNLOCK(&fd_mutex);
3725                 
3726 #ifdef WITH_PROXY
3727                 /*
3728                  *      Remove it from the list of sockets to be used
3729                  *      when proxying.
3730                  */
3731                 if (this->type == RAD_LISTEN_PROXY) {
3732                         PTHREAD_MUTEX_LOCK(&proxy_mutex);
3733                         if (!fr_packet_list_socket_remove(proxy_list,
3734                                                           this->fd, NULL)) {
3735                                 radlog(L_ERR, "Fatal error removing socket: %s",
3736                                        fr_strerror());
3737                                 exit(1);
3738                         }
3739                         if (sock->home) sock->home->num_connections--;
3740                         PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
3741                 }
3742 #endif
3743
3744                 /*
3745                  *      Remove any pending cleanups.
3746                  */
3747                 if (sock->ev) fr_event_delete(el, &sock->ev);
3748
3749                 /*
3750                  *      And finally, close the socket.
3751                  */
3752                 listen_free(&this);
3753         }
3754 #endif  /* WITH_TCP */
3755
3756         return 1;
3757 }
3758
3759 static void handle_signal_self(int flag)
3760 {
3761         if ((flag & (RADIUS_SIGNAL_SELF_EXIT | RADIUS_SIGNAL_SELF_TERM)) != 0) {
3762                 if ((flag & RADIUS_SIGNAL_SELF_EXIT) != 0) {
3763                         radlog(L_INFO, "Received TERM signal");
3764                         fr_event_loop_exit(el, 1);
3765                 } else {
3766                         fr_event_loop_exit(el, 2);
3767                 }
3768
3769                 return;
3770         } /* else exit/term flags weren't set */
3771
3772         /*
3773          *      Tell the even loop to stop processing.
3774          */
3775         if ((flag & RADIUS_SIGNAL_SELF_HUP) != 0) {
3776                 time_t when;
3777                 static time_t last_hup = 0;
3778
3779                 when = time(NULL);
3780                 if ((int) (when - last_hup) < 5) {
3781                         radlog(L_INFO, "Ignoring HUP (less than 5s since last one)");
3782                         return;
3783                 }
3784
3785                 radlog(L_INFO, "Received HUP signal.");
3786
3787                 last_hup = when;
3788
3789                 fr_event_loop_exit(el, 0x80);
3790         }
3791
3792 #ifdef WITH_DETAIL
3793         if ((flag & RADIUS_SIGNAL_SELF_DETAIL) != 0) {
3794                 rad_listen_t *this;
3795                 
3796                 /*
3797                  *      FIXME: O(N) loops suck.
3798                  */
3799                 for (this = mainconfig.listen;
3800                      this != NULL;
3801                      this = this->next) {
3802                         if (this->type != RAD_LISTEN_DETAIL) continue;
3803
3804                         /*
3805                          *      This one didn't send the signal, skip
3806                          *      it.
3807                          */
3808                         if (!this->decode(this, NULL)) continue;
3809
3810                         /*
3811                          *      Go service the interrupt.
3812                          */
3813                         event_poll_detail(this);
3814                 }
3815         }
3816 #endif
3817
3818 #ifdef WITH_TCP
3819 #ifdef WITH_PROXY
3820         /*
3821          *      Add event handlers for idle timeouts && maximum lifetime.
3822          */
3823         if ((flag & RADIUS_SIGNAL_SELF_NEW_FD) != 0) {
3824                 struct timeval when;
3825                 void *fun = NULL;
3826
3827                 fr_event_now(el, &now);
3828
3829                 PTHREAD_MUTEX_LOCK(&proxy_mutex);
3830
3831                 while (proxy_listener_list) {
3832                         rad_listen_t *this = proxy_listener_list;
3833                         listen_socket_t *sock = this->data;
3834
3835                         proxy_listener_list = this->next;
3836                         this->next = NULL;
3837
3838                         if (!sock->home) continue; /* skip UDP sockets */
3839
3840                         when = now;
3841
3842                         if (!sock->home->idle_timeout) {
3843                                 rad_assert(sock->home->lifetime != 0);
3844
3845                                 when.tv_sec += sock->home->lifetime;
3846                                 fun = tcp_socket_lifetime;
3847                         } else {
3848                                 rad_assert(sock->home->idle_timeout != 0);
3849
3850                                 when.tv_sec += sock->home->idle_timeout;
3851                                 fun = tcp_socket_idle_timeout;
3852                         }
3853
3854                         if (!fr_event_insert(el, fun, this, &when,
3855                                              &(sock->ev))) {
3856                                 rad_panic("Failed to insert event");
3857                         }
3858                 }
3859
3860                 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
3861         }
3862 #endif  /* WITH_PROXY */
3863 #endif  /* WITH_TCP */
3864 }
3865
3866 #ifndef WITH_SELF_PIPE
3867 void radius_signal_self(int flag)
3868 {
3869         handle_signal_self(flag);
3870 }
3871 #else
3872 /*
3873  *      Inform ourselves that we received a signal.
3874  */
3875 void radius_signal_self(int flag)
3876 {
3877         ssize_t rcode;
3878         uint8_t buffer[16];
3879
3880         /*
3881          *      The read MUST be non-blocking for this to work.
3882          */
3883         rcode = read(self_pipe[0], buffer, sizeof(buffer));
3884         if (rcode > 0) {
3885                 ssize_t i;
3886
3887                 for (i = 0; i < rcode; i++) {
3888                         buffer[0] |= buffer[i];
3889                 }
3890         } else {
3891                 buffer[0] = 0;
3892         }
3893
3894         buffer[0] |= flag;
3895
3896         write(self_pipe[1], buffer, 1);
3897 }
3898
3899
3900 static void event_signal_handler(UNUSED fr_event_list_t *xel,
3901                                  UNUSED int fd, UNUSED void *ctx)
3902 {
3903         ssize_t i, rcode;
3904         uint8_t buffer[32];
3905
3906         rcode = read(self_pipe[0], buffer, sizeof(buffer));
3907         if (rcode <= 0) return;
3908
3909         /*
3910          *      Merge pending signals.
3911          */
3912         for (i = 0; i < rcode; i++) {
3913                 buffer[0] |= buffer[i];
3914         }
3915
3916         handle_signal_self(buffer[0]);
3917 }
3918 #endif
3919
3920
3921 static void event_socket_handler(fr_event_list_t *xel, UNUSED int fd,
3922                                  void *ctx)
3923 {
3924         rad_listen_t *listener = ctx;
3925         RAD_REQUEST_FUNP fun;
3926         REQUEST *request;
3927
3928         rad_assert(xel == el);
3929
3930         xel = xel;
3931
3932         if (
3933 #ifdef WITH_DETAIL
3934             (listener->type != RAD_LISTEN_DETAIL) &&
3935 #endif
3936             (listener->fd < 0)) {
3937                 char buffer[256];
3938
3939                 listener->print(listener, buffer, sizeof(buffer));
3940                 radlog(L_ERR, "FATAL: Asked to read from closed socket: %s",
3941                        buffer);
3942         
3943                 rad_panic("Socket was closed on us!");
3944                 _exit(1);
3945         }
3946         
3947         if (!listener->recv(listener, &fun, &request)) return;
3948
3949         rad_assert(fun != NULL);
3950         rad_assert(request != NULL);
3951
3952         thread_pool_addrequest(request, fun);
3953 }
3954
3955 #ifdef WITH_DETAIL
3956 /*
3957  *      This function is called periodically to see if this detail
3958  *      file is available for reading.
3959  */
3960 static void event_poll_detail(void *ctx)
3961 {
3962         int delay;
3963         rad_listen_t *this = ctx;
3964         struct timeval when;
3965         listen_detail_t *detail = this->data;
3966
3967         rad_assert(this->type == RAD_LISTEN_DETAIL);
3968
3969         event_socket_handler(el, this->fd, this);
3970
3971         fr_event_now(el, &now);
3972         when = now;
3973
3974         /*
3975          *      Backdoor API to get the delay until the next poll
3976          *      time.
3977          */
3978         delay = this->encode(this, NULL);
3979         tv_add(&when, delay);
3980
3981         if (!fr_event_insert(el, event_poll_detail, this,
3982                              &when, &detail->ev)) {
3983                 radlog(L_ERR, "Failed creating handler");
3984                 exit(1);
3985         }
3986 }
3987 #endif
3988
3989 static void event_status(struct timeval *wake)
3990 {
3991 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
3992         int argval;
3993 #endif
3994
3995         if (debug_flag == 0) {
3996                 if (just_started) {
3997                         radlog(L_INFO, "Ready to process requests.");
3998                         just_started = FALSE;
3999                 }
4000                 return;
4001         }
4002
4003         if (!wake) {
4004                 radlog(L_INFO, "Ready to process requests.");
4005
4006         } else if ((wake->tv_sec != 0) ||
4007                    (wake->tv_usec >= 100000)) {
4008                 DEBUG("Waking up in %d.%01u seconds.",
4009                       (int) wake->tv_sec, (unsigned int) wake->tv_usec / 100000);
4010         }
4011
4012
4013         /*
4014          *      FIXME: Put this somewhere else, where it isn't called
4015          *      all of the time...
4016          */
4017
4018 #if !defined(HAVE_PTHREAD_H) && defined(WNOHANG)
4019         /*
4020          *      If there are no child threads, then there may
4021          *      be child processes.  In that case, wait for
4022          *      their exit status, and throw that exit status
4023          *      away.  This helps get rid of zxombie children.
4024          */
4025         while (waitpid(-1, &argval, WNOHANG) > 0) {
4026                 /* do nothing */
4027         }
4028 #endif
4029
4030 }
4031
4032 /*
4033  *      Externally-visibly functions.
4034  */
4035 int radius_event_init(CONF_SECTION *cs, int spawn_flag)
4036 {
4037         rad_listen_t *head = NULL;
4038
4039         if (el) return 0;
4040
4041         time(&fr_start_time);
4042
4043         el = fr_event_list_create(event_status);
4044         if (!el) return 0;
4045
4046         pl = fr_packet_list_create(0);
4047         if (!pl) return 0;      /* leak el */
4048
4049         request_num_counter = 0;
4050
4051 #ifdef WITH_PROXY
4052         if (mainconfig.proxy_requests) {
4053                 /*
4054                  *      Create the tree for managing proxied requests and
4055                  *      responses.
4056                  */
4057                 proxy_list = fr_packet_list_create(1);
4058                 if (!proxy_list) return 0;
4059
4060 #ifdef HAVE_PTHREAD_H
4061                 if (pthread_mutex_init(&proxy_mutex, NULL) != 0) {
4062                         radlog(L_ERR, "FATAL: Failed to initialize proxy mutex: %s",
4063                                strerror(errno));
4064                         exit(1);
4065                 }
4066 #endif
4067         }
4068 #endif
4069
4070 #ifdef HAVE_PTHREAD_H
4071 #ifndef __MINGW32__
4072         NO_SUCH_CHILD_PID = (pthread_t ) (0);
4073 #else
4074         NO_SUCH_CHILD_PID = pthread_self(); /* not a child thread */
4075 #endif
4076         /*
4077          *      Initialize the threads ONLY if we're spawning, AND
4078          *      we're running normally.
4079          */
4080         if (spawn_flag && !check_config &&
4081             (thread_pool_init(cs, &spawn_flag) < 0)) {
4082                 exit(1);
4083         }
4084 #endif
4085
4086         /*
4087          *      Move all of the thread calls to this file?
4088          *
4089          *      It may be best for the mutexes to be in this file...
4090          */
4091         have_children = spawn_flag;
4092
4093         if (check_config) {
4094                 DEBUG("%s: #### Skipping IP addresses and Ports ####",
4095                        mainconfig.name);
4096                 if (listen_init(cs, &head) < 0) {
4097                         fflush(NULL);
4098                         exit(1);
4099                 }
4100                 return 1;
4101         }
4102
4103 #ifdef WITH_SELF_PIPE
4104         /*
4105          *      Child threads need a pipe to signal us, as do the
4106          *      signal handlers.
4107          */
4108         if (pipe(self_pipe) < 0) {
4109                 radlog(L_ERR, "radiusd: Error opening internal pipe: %s",
4110                        strerror(errno));
4111                 exit(1);
4112         }
4113         if (fcntl(self_pipe[0], F_SETFL, O_NONBLOCK | FD_CLOEXEC) < 0) {
4114                 radlog(L_ERR, "radiusd: Error setting internal flags: %s",
4115                        strerror(errno));
4116                 exit(1);
4117         }
4118         if (fcntl(self_pipe[1], F_SETFL, O_NONBLOCK | FD_CLOEXEC) < 0) {
4119                 radlog(L_ERR, "radiusd: Error setting internal flags: %s",
4120                        strerror(errno));
4121                 exit(1);
4122         }
4123
4124         if (!fr_event_fd_insert(el, 0, self_pipe[0],
4125                                   event_signal_handler, el)) {
4126                 radlog(L_ERR, "Failed creating handler for signals");
4127                 exit(1);
4128         }
4129 #endif  /* WITH_SELF_PIPE */
4130
4131        DEBUG("%s: #### Opening IP addresses and Ports ####",
4132                mainconfig.name);
4133
4134        /*
4135         *       The server temporarily switches to an unprivileged
4136         *       user very early in the bootstrapping process.
4137         *       However, some sockets MAY require privileged access
4138         *       (bind to device, or to port < 1024, or to raw
4139         *       sockets).  Those sockets need to call suid up/down
4140         *       themselves around the functions that need a privileged
4141         *       uid.
4142         */
4143         if (listen_init(cs, &head) < 0) {
4144                 _exit(1);
4145         }
4146         
4147         mainconfig.listen = head;
4148
4149         /*
4150          *      At this point, no one has any business *ever* going
4151          *      back to root uid.
4152          */
4153         fr_suid_down_permanent();
4154
4155         return 1;
4156 }
4157
4158
4159 static int request_hash_cb(UNUSED void *ctx, void *data)
4160 {
4161         REQUEST *request = fr_packet2myptr(REQUEST, packet, data);
4162
4163 #ifdef WITH_PROXY
4164         rad_assert(request->in_proxy_hash == FALSE);
4165 #endif
4166
4167         ev_request_free(&request);
4168
4169         return 0;
4170 }
4171
4172
4173 #ifdef WITH_PROXY
4174 static int proxy_hash_cb(UNUSED void *ctx, void *data)
4175 {
4176         REQUEST *request = fr_packet2myptr(REQUEST, proxy, data);
4177
4178         ev_request_free(&request);
4179
4180         return 0;
4181 }
4182 #endif
4183
4184 void radius_event_free(void)
4185 {
4186         /*
4187          *      FIXME: Stop all threads, or at least check that
4188          *      they're all waiting on the semaphore, and the queues
4189          *      are empty.
4190          */
4191
4192 #ifdef WITH_PROXY
4193         /*
4194          *      There are requests in the proxy hash that aren't
4195          *      referenced from anywhere else.  Remove them first.
4196          */
4197         if (proxy_list) {
4198                 fr_packet_list_walk(proxy_list, NULL, proxy_hash_cb);
4199                 fr_packet_list_free(proxy_list);
4200                 proxy_list = NULL;
4201         }
4202 #endif
4203
4204         fr_packet_list_walk(pl, NULL, request_hash_cb);
4205
4206         fr_packet_list_free(pl);
4207         pl = NULL;
4208
4209         fr_event_list_free(el);
4210 }
4211
4212 int radius_event_process(void)
4213 {
4214         if (!el) return 0;
4215
4216         return fr_event_loop(el);
4217 }
4218
4219 void radius_handle_request(REQUEST *request, RAD_REQUEST_FUNP fun)
4220 {
4221         request->options = RAD_REQUEST_OPTION_DEBUG2;
4222
4223         if (request_pre_handler(request)) {
4224                 rad_assert(fun != NULL);
4225                 rad_assert(request != NULL);
4226                 
4227                 if (request->server) RDEBUG("server %s {",
4228                                             request->server != NULL ?
4229                                             request->server : ""); 
4230                 fun(request);
4231
4232                 if (request->server) RDEBUG("} # server %s",
4233                                              request->server != NULL ?
4234                                             request->server : "");
4235
4236                 request_post_handler(request);
4237         }
4238
4239         DEBUG2("Going to the next request");
4240         return;
4241 }