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