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