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