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