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