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