7d3c4cf5eccd7dfec758f3099c988e33c068a592
[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
28 #include <freeradius-devel/radiusd.h>
29 #include <freeradius-devel/modules.h>
30 #include <freeradius-devel/event.h>
31 #include <freeradius-devel/radius_snmp.h>
32
33 #include <freeradius-devel/rad_assert.h>
34
35 #define USEC (1000000)
36
37 /*
38  *      Ridiculous amounts of local state.
39  */
40 static lrad_event_list_t        *el = NULL;
41 static lrad_packet_list_t       *pl = NULL;
42 static int                      request_num_counter = 0;
43 static struct timeval           now;
44 static time_t                   start_time;
45 static int                      have_children;
46
47 #ifdef HAVE_PTHREAD_H
48 static pthread_mutex_t  proxy_mutex;
49
50 #define PTHREAD_MUTEX_LOCK if (have_children) pthread_mutex_lock
51 #define PTHREAD_MUTEX_UNLOCK if (have_children) pthread_mutex_unlock
52 #else
53 /*
54  *      This is easier than ifdef's throughout the code.
55  */
56 #define PTHREAD_MUTEX_LOCK(_x)
57 #define PTHREAD_MUTEX_UNLOCK(_x)
58 #endif
59
60 #define INSERT_EVENT(_function, _ctx) if (!lrad_event_insert(el, _function, _ctx, &((_ctx)->when), &((_ctx)->ev))) { _rad_panic(__FILE__, __LINE__, "Failed to insert event"); }
61
62 static lrad_packet_list_t *proxy_list = NULL;
63
64 /*
65  *      We keep the proxy FD's here.  The RADIUS Id's are marked
66  *      "allocated" per Id, via a bit per proxy FD.
67  */
68 static int              proxy_fds[32];
69 static rad_listen_t     *proxy_listeners[32];
70
71
72 static void NEVER_RETURNS _rad_panic(const char *file, unsigned int line,
73                                     const char *msg)
74 {
75         radlog(L_ERR, "]%s:%d] %s", file, line, msg);
76         _exit(1);
77 }
78
79 #define rad_panic(x) _rad_panic(__FILE__, __LINE__, x)
80
81
82 static void tv_add(struct timeval *tv, int usec_delay)
83 {
84         if (usec_delay > USEC) {
85                 tv->tv_sec += usec_delay / USEC;
86                 usec_delay %= USEC;
87         }
88         tv->tv_usec += usec_delay;
89
90         if (tv->tv_usec > USEC) {
91                 tv->tv_usec -= USEC;
92                 tv->tv_sec++;
93         }
94 }
95
96 #ifdef WITH_SNMP
97 static void snmp_inc_client_responses(RADCLIENT *client,
98                                       REQUEST *request)
99 {
100         if (!mainconfig.do_snmp) return;
101
102         /*
103          *      Update the SNMP statistics.
104          *
105          *      Note that we do NOT do this in a child thread.
106          *      Instead, we update the stats when a request is
107          *      deleted, because only the main server thread calls
108          *      this function, which makes it thread-safe.
109          */
110         switch (request->reply->code) {
111         case PW_AUTHENTICATION_ACK:
112                 rad_snmp.auth.total_responses++;
113                 rad_snmp.auth.total_access_accepts++;
114                 if (client) client->auth->accepts++;
115                 break;
116
117         case PW_AUTHENTICATION_REJECT:
118                 rad_snmp.auth.total_responses++;
119                 rad_snmp.auth.total_access_rejects++;
120                 if (client) client->auth->rejects++;
121                 break;
122                 
123         case PW_ACCESS_CHALLENGE:
124                 rad_snmp.auth.total_responses++;
125                 rad_snmp.auth.total_access_challenges++;
126                 if (client) client->auth->challenges++;
127                 break;
128                 
129         case PW_ACCOUNTING_RESPONSE:
130                 rad_snmp.acct.total_responses++;
131                 if (client) client->auth->responses++;
132                 break;
133
134                 /*
135                  *      No response, it must have been a bad
136                  *      authenticator.
137                  */
138         case 0:
139                 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
140                         rad_snmp.auth.total_bad_authenticators++;
141                         if (client) client->auth->bad_authenticators++;
142                 }
143                 break;
144                 
145         default:
146                 break;
147         }
148 }
149 #else
150 #define snmp_inc_client_responses(_x, _y)
151 #endif
152
153
154 static void remove_from_request_hash(REQUEST *request)
155 {
156         if (!request->in_request_hash) return;
157
158         lrad_packet_list_yank(pl, request->packet);
159         request->in_request_hash = FALSE;
160
161 #ifdef WITH_SNMP
162         if ((request->listener->type == RAD_LISTEN_AUTH) ||
163             (request->listener->type == RAD_LISTEN_ACCT)) {
164                 snmp_inc_client_responses(client_listener_find(request->listener,
165                                                                &request->packet->src_ipaddr),
166                                           request);
167         }
168 #endif
169 }
170
171
172 static REQUEST *lookup_in_proxy_hash(RADIUS_PACKET *reply)
173 {
174         RADIUS_PACKET **proxy_p;
175         REQUEST *request;
176
177         PTHREAD_MUTEX_LOCK(&proxy_mutex);
178         proxy_p = lrad_packet_list_find_byreply(proxy_list, reply);
179
180         if (!proxy_p) {
181                 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
182                 return NULL;
183         }
184
185         request = lrad_packet2myptr(REQUEST, proxy, proxy_p);
186                 
187         if (!request) {
188                 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
189                 return NULL;
190         }
191
192         request->num_proxied_responses++;
193
194         /*
195          *      Catch the most common case of everything working
196          *      correctly.
197          */
198         if (request->num_proxied_requests == request->num_proxied_responses) {
199                 /*
200                  *      FIXME: remove from the event list, too?
201                  */
202                 lrad_packet_list_yank(proxy_list, request->proxy);
203                 lrad_packet_list_id_free(proxy_list, request->proxy);
204                 request->in_proxy_hash = FALSE;
205         }
206
207         PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
208
209         return request;
210 }
211
212
213 static void remove_from_proxy_hash(REQUEST *request)
214 {
215         if (!request->in_proxy_hash) return;
216
217         PTHREAD_MUTEX_LOCK(&proxy_mutex);
218         if (request->home_server->currently_outstanding) {
219                 request->home_server->currently_outstanding--;
220         }
221         lrad_packet_list_yank(proxy_list, request->proxy);
222         lrad_packet_list_id_free(proxy_list, request->proxy);
223         PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
224
225         request->in_proxy_hash = FALSE;
226 }
227
228
229 static int insert_into_proxy_hash(REQUEST *request)
230 {
231         int i, proxy;
232         char buf[128];
233
234         rad_assert(request->proxy != NULL);
235         rad_assert(proxy_list != NULL);
236
237         request->proxy->sockfd = -1;
238
239         PTHREAD_MUTEX_LOCK(&proxy_mutex);
240
241         request->home_server->currently_outstanding++;
242
243         if (!lrad_packet_list_id_alloc(proxy_list, request->proxy)) {
244                 int found;
245                 rad_listen_t *proxy_listener;
246
247                 /*
248                  *      Allocate a new proxy fd.  This function adds it
249                  *      into the list of listeners.
250                  */
251                 proxy_listener = proxy_new_listener();
252                 if (!proxy_listener) {
253                         PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
254                         DEBUG2("ERROR: Failed to create a new socket for proxying requests.");
255                         return 0;
256                 }
257
258                 /*
259                  *      Cache it locally.
260                  */
261                 found = -1;
262                 proxy = proxy_listener->fd;
263                 for (i = 0; i < 32; i++) {
264                         DEBUG2("PROXY %d %d", i, proxy_fds[(proxy + i) & 0x1f]);
265
266                         /*
267                          *      Found a free entry.  Save the socket,
268                          *      and remember where we saved it.
269                          */
270                         if (proxy_fds[(proxy + i) & 0x1f] == -1) {
271                                 found = (proxy + i) & 0x1f;
272                                 proxy_fds[found] = proxy;
273                                 proxy_listeners[found] = proxy_listener;
274                                 break;
275                         }
276                 }
277                 rad_assert(found >= 0);
278
279                 if (!lrad_packet_list_socket_add(proxy_list, proxy_listener->fd)) {
280                         PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
281                         DEBUG2("ERROR: Failed to create a new socket for proxying requests.");
282                         return 0; /* leak proxy_listener */
283                         
284                 }
285                     
286                 if (!lrad_packet_list_id_alloc(proxy_list, request->proxy)) {
287                         PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
288                         DEBUG2("ERROR: Failed to create a new socket for proxying requests.");
289                         return 0;
290                 }
291         }
292         rad_assert(request->proxy->sockfd >= 0);
293
294         /*
295          *      FIXME: Hack until we get rid of rad_listen_t, and put
296          *      the information into the packet_list.
297          */
298         proxy = -1;
299         for (i = 0; i < 32; i++) {
300                 if (proxy_fds[i] == request->proxy->sockfd) {
301                         proxy = i;
302                         break;
303                 }
304         }
305
306         if (proxy < 0) {
307                 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
308                 DEBUG2("ERROR: All sockets are full.");
309                 return 0;
310         }
311
312         rad_assert(proxy_fds[proxy] != -1);
313         rad_assert(proxy_listeners[proxy] != NULL);
314         request->proxy_listener = proxy_listeners[proxy];
315
316         if (!lrad_packet_list_insert(proxy_list, &request->proxy)) {
317                 /* FIXME: free id? */
318                 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
319                 DEBUG2("ERROR: Failed to insert entry into proxy list");
320                 return 0;
321         }
322         
323         PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
324
325         DEBUG3(" proxy: allocating destination %s port %d - Id %d",
326                inet_ntop(request->proxy->dst_ipaddr.af,
327                          &request->proxy->dst_ipaddr.ipaddr, buf, sizeof(buf)),
328                request->proxy->dst_port,
329                request->proxy->id);
330         
331         request->in_proxy_hash = TRUE;
332
333         return 1;
334 }
335
336
337 /*
338  *      Called as BOTH an event, and in-line from other functions.
339  */
340 static void wait_for_proxy_id_to_expire(void *ctx)
341 {
342         REQUEST *request = ctx;
343         home_server *home = request->home_server;
344
345         rad_assert(request->magic == REQUEST_MAGIC);
346         rad_assert(request->proxy != NULL);
347
348         /*
349          *      FIXME: handle the case of RETRY from one home server
350          *      to another!
351          *
352          *      Do we even have to do anything?
353          */
354         request->when = request->proxy_when;
355         request->when.tv_sec += home->response_window;
356
357         if ((request->num_proxied_requests == request->num_proxied_responses) ||
358             timercmp(&now, &request->when, >)) {
359                 if (request->packet) {
360                         DEBUG2("Cleaning up request %d ID %d with timestamp +%d",
361                                request->number, request->packet->id,
362                                (unsigned int) (request->timestamp - start_time));
363                 } else {
364                         DEBUG2("Cleaning up request %d with timestamp +%d",
365                                request->number,
366                                (unsigned int) (request->timestamp - start_time));
367                 }
368                 lrad_event_delete(el, &request->ev);
369                 remove_from_proxy_hash(request);
370                 remove_from_request_hash(request);
371                 request_free(&request);
372                 return;
373         }
374
375         INSERT_EVENT(wait_for_proxy_id_to_expire, request);
376 }
377
378
379 static void wait_for_child_to_die(void *ctx)
380 {
381         REQUEST *request = ctx;
382
383         rad_assert(request->magic == REQUEST_MAGIC);
384
385         if ((request->child_state == REQUEST_QUEUED) |
386             (request->child_state == REQUEST_RUNNING)) {
387                 request->delay += (request->delay >> 1);
388                 tv_add(&request->when, request->delay);
389                 
390                 DEBUG2("Child is still stuck for request %d", request->number);
391
392                 INSERT_EVENT(wait_for_child_to_die, request);
393                 return;
394         }
395         
396         DEBUG2("Child is finally responsive for request %d", request->number);
397         remove_from_request_hash(request);
398
399         if (request->proxy) {
400                 wait_for_proxy_id_to_expire(request);
401                 return;
402         }
403
404         request_free(&request);
405 }
406
407
408 static void cleanup_delay(void *ctx)
409 {
410         REQUEST *request = ctx;
411
412         rad_assert(request->magic == REQUEST_MAGIC);
413         rad_assert((request->child_state == REQUEST_CLEANUP_DELAY) ||
414                    (request->child_state == REQUEST_DONE));
415
416         remove_from_request_hash(request);
417
418         if (request->proxy &&
419             request->in_proxy_hash) {
420                 wait_for_proxy_id_to_expire(request);
421                 return;
422         }
423
424         DEBUG2("Cleaning up request %d ID %d with timestamp +%d",
425                request->number, request->packet->id,
426                (unsigned int) (request->timestamp - start_time));
427
428         lrad_event_delete(el, &request->ev);
429         request_free(&request); 
430 }
431
432
433 static void reject_delay(void *ctx)
434 {
435         REQUEST *request = ctx;
436
437         rad_assert(request->magic == REQUEST_MAGIC);
438         rad_assert(request->child_state == REQUEST_REJECT_DELAY);
439
440         DEBUG2("Sending delayed reject for request %d", request->number);
441
442         request->listener->send(request->listener, request);
443
444         request->when.tv_sec += mainconfig.cleanup_delay;
445         request->child_state = REQUEST_CLEANUP_DELAY;
446
447         INSERT_EVENT(cleanup_delay, request);
448 }
449
450
451 static void revive_home_server(void *ctx)
452 {
453         home_server *home = ctx;
454
455         home->state = HOME_STATE_ALIVE;
456         DEBUG2("Marking home server alive again... we have no idea if it really is alive or not.");
457         home->currently_outstanding = 0;
458 }
459
460
461 static void no_response_to_ping(void *ctx)
462 {
463         REQUEST *request = ctx;
464         home_server *home = request->home_server;
465         char buffer[128];
466
467         home->num_received_pings = 0;
468
469         DEBUG2("No response to ping %d from home server %s port %d",
470                request->number,
471                inet_ntop(request->proxy->dst_ipaddr.af,
472                          &request->proxy->dst_ipaddr.ipaddr,
473                          buffer, sizeof(buffer)),
474                request->proxy->dst_port);
475
476         wait_for_proxy_id_to_expire(request);   
477 }
478
479
480 static void received_response_to_ping(REQUEST *request)
481 {
482         home_server *home = request->home_server;
483         char buffer[128];
484
485         home->num_received_pings++;
486
487         DEBUG2("Received response to ping %d (%d in current sequence)",
488                request->number, home->num_received_pings);
489
490         if (home->num_received_pings < home->num_pings_to_alive) {
491                 wait_for_proxy_id_to_expire(request);
492                 return;
493         }
494
495         DEBUG2("Marking home server %s port %d alive",
496                inet_ntop(request->proxy->dst_ipaddr.af,
497                          &request->proxy->dst_ipaddr.ipaddr,
498                          buffer, sizeof(buffer)),
499                request->proxy->dst_port);
500
501         if (!lrad_event_delete(el, &home->ev)) {
502                 DEBUG2("Hmm... no event for home server, WTF?");
503         }
504
505         if (!lrad_event_delete(el, &request->ev)) {
506                 DEBUG2("Hmm... no event for request, WTF?");
507         }
508
509         wait_for_proxy_id_to_expire(request);
510
511         home->state = HOME_STATE_ALIVE;
512         home->currently_outstanding = 0;
513 }
514
515
516 static void ping_home_server(void *ctx)
517 {
518         uint32_t jitter;
519         home_server *home = ctx;
520         REQUEST *request;
521         VALUE_PAIR *vp;
522
523         if (home->state == HOME_STATE_ALIVE) {
524                 radlog(L_INFO, "Suspicious proxy state... continuing");
525                 return;
526         }
527
528         request = request_alloc();
529         request->number = request_num_counter++;
530
531         request->proxy = rad_alloc(1);
532         rad_assert(request->proxy != NULL);
533
534         gettimeofday(&request->when, NULL);
535         home->when = request->when;
536
537         if (home->ping_check == HOME_PING_CHECK_STATUS_SERVER) {
538                 request->proxy->code = PW_STATUS_SERVER;
539
540                 vp = pairmake("Message-Authenticator", "0x00", T_OP_SET);
541                 if (!vp) rad_panic("Out of memory");
542                 pairadd(&request->proxy->vps, vp);
543
544         } else if (home->type == HOME_TYPE_AUTH) {
545                 request->proxy->code = PW_AUTHENTICATION_REQUEST;
546
547                 vp = pairmake("User-Name", home->ping_user_name, T_OP_SET);
548                 if (!vp) rad_panic("Out of memory");
549                 pairadd(&request->proxy->vps, vp);
550
551                 vp = pairmake("User-Password", home->ping_user_password, T_OP_SET);
552                 if (!vp) rad_panic("Out of memory");
553                 pairadd(&request->proxy->vps, vp);
554
555                 vp = pairmake("Service-Type", "Authenticate-Only", T_OP_SET);
556                 if (!vp) rad_panic("Out of memory");
557                 pairadd(&request->proxy->vps, vp);
558
559                 vp = pairmake("Message-Authenticator", "0x00", T_OP_SET);
560                 if (!vp) rad_panic("Out of memory");
561                 pairadd(&request->proxy->vps, vp);
562
563         } else {
564                 request->proxy->code = PW_ACCOUNTING_REQUEST;
565
566                 vp = pairmake("User-Name", home->ping_user_name, T_OP_SET);
567                 if (!vp) rad_panic("Out of memory");
568                 pairadd(&request->proxy->vps, vp);
569
570                 vp = pairmake("Acct-Status-Type", "Stop", T_OP_SET);
571                 if (!vp) rad_panic("Out of memory");
572                 pairadd(&request->proxy->vps, vp);
573
574                 vp = pairmake("Acct-Session-Id", "00000000", T_OP_SET);
575                 if (!vp) rad_panic("Out of memory");
576                 pairadd(&request->proxy->vps, vp);
577
578                 vp = pairmake("Event-Timestamp", "0", T_OP_SET);
579                 if (!vp) rad_panic("Out of memory");
580                 vp->vp_date = now.tv_sec;
581                 pairadd(&request->proxy->vps, vp);
582         }
583
584         vp = pairmake("NAS-Identifier", "Ping! Are you alive?", T_OP_SET);
585         if (!vp) rad_panic("Out of memory");
586         pairadd(&request->proxy->vps, vp);
587
588         request->proxy->dst_ipaddr = home->ipaddr;
589         request->proxy->dst_port = home->port;
590         request->home_server = home;
591
592         rad_assert(request->proxy_listener == NULL);
593
594         if (!insert_into_proxy_hash(request)) {
595                 DEBUG2("Failed inserting ping %d into proxy hash.  Discarding it.",
596                        request->number);
597                 request_free(&request);
598                 return;
599         }
600         rad_assert(request->proxy_listener != NULL);
601         request->proxy_listener->send(request->proxy_listener,
602                                       request);
603
604         /*
605          *      FIXME: add a separate timeout for ping packets!
606          */
607         request->child_state = REQUEST_PROXIED;
608         request->when.tv_sec += mainconfig.cleanup_delay;
609
610         INSERT_EVENT(no_response_to_ping, request);
611
612         /*
613          *      Add +/- 2s of jitter, as suggested in RFC 3539
614          *      and in the Issues and Fixes draft.
615          */
616         home->when.tv_sec += home->ping_interval - 2;
617
618         jitter = lrad_rand();
619         jitter ^= (jitter >> 10);
620         jitter &= ((1 << 23) - 1); /* 22 bits of 1 */
621
622         tv_add(&home->when, jitter);
623
624
625         INSERT_EVENT(ping_home_server, home);
626 }
627
628
629 static void check_for_zombie_home_server(REQUEST *request)
630 {
631         home_server *home;
632         struct timeval when;
633         char buffer[128];
634
635         home = request->home_server;
636
637         if (home->state != HOME_STATE_ZOMBIE) return;
638
639         when = home->zombie_period_start;
640         when.tv_sec += home->zombie_period;
641
642         if (timercmp(&now, &when, <)) {
643                 return;
644         }
645
646         /*
647          *      It's been a zombie for too long, mark it as
648          *      dead.
649          */
650         DEBUG2("FAILURE: Home server %s port %d is dead.",
651                inet_ntop(request->proxy->dst_ipaddr.af,
652                          &request->proxy->dst_ipaddr.ipaddr,
653                          buffer, sizeof(buffer)),
654                request->proxy->dst_port);
655         home->state = HOME_STATE_IS_DEAD;
656         home->num_received_pings = 0;
657         home->when = request->when;
658         
659         if (home->ping_check != HOME_PING_CHECK_NONE) {
660                 rad_assert((home->ping_check == HOME_PING_CHECK_STATUS_SERVER) ||
661                            (home->ping_user_name != NULL));
662                 home->when.tv_sec += home->ping_interval;
663
664                 INSERT_EVENT(ping_home_server, home);
665         } else {
666                 home->when.tv_sec += home->revive_interval;
667
668                 INSERT_EVENT(revive_home_server, home);
669         }
670 }
671
672 /* maybe check this against wait_for_proxy_id_to_expire? */
673 static void no_response_to_proxied_request(void *ctx)
674 {
675         REQUEST *request = ctx;
676         home_server *home;
677         char buffer[128];
678         
679         rad_assert(request->magic == REQUEST_MAGIC);
680         rad_assert(request->child_state == REQUEST_PROXIED);
681
682         radlog(L_ERR, "Rejecting request %d due to lack of any response from home server %s port %d",
683                request->number,
684                inet_ntop(request->proxy->dst_ipaddr.af,
685                          &request->proxy->dst_ipaddr.ipaddr,
686                          buffer, sizeof(buffer)),
687                request->proxy->dst_port);
688
689         /*
690          *      FIXME: Run packets through post-proxy-type "Fail"
691          */
692         if ((request->proxy->code == PW_ACCOUNTING_REQUEST) ||
693             (request->proxy->code == PW_STATUS_SERVER)) {
694                 remove_from_request_hash(request);
695                 wait_for_proxy_id_to_expire(request);
696
697         } else {
698                 request->reply->code = PW_AUTHENTICATION_REJECT;
699
700                 request->listener->send(request->listener, request);
701
702                 request->child_state = REQUEST_CLEANUP_DELAY;
703                 request->when.tv_sec += mainconfig.cleanup_delay;
704                         
705                 /* cleanup_delay calls wait_for_proxy_id_to_expire */
706
707                 INSERT_EVENT(cleanup_delay, request);
708         }
709                 
710         home = request->home_server;
711         if (home->state == HOME_STATE_IS_DEAD) {
712                 /* FIXME: assert that some event is set for the home server */
713                 return;
714         }
715
716         /*
717          *      Enable the zombie period when we notice that the home
718          *      server hasn't responded.  We also back-date the start
719          *      of the zombie period to when the proxied request was
720          *      sent.
721          */
722         if (home->state == HOME_STATE_ALIVE) {
723                 DEBUG2("WARNING: Home server %s port %d may be dead.",
724                        inet_ntop(request->proxy->dst_ipaddr.af,
725                                  &request->proxy->dst_ipaddr.ipaddr,
726                                  buffer, sizeof(buffer)),
727                        request->proxy->dst_port);
728                 home->state = HOME_STATE_ZOMBIE;
729                 home->zombie_period_start = now;
730                 home->zombie_period_start.tv_sec -= home->response_window;
731                 return;
732         }
733
734         check_for_zombie_home_server(request);
735 }
736
737
738 static void wait_a_bit(void *ctx)
739 {
740         struct timeval when;
741         REQUEST *request = ctx;
742         lrad_event_callback_t callback = NULL;
743
744         rad_assert(request->magic == REQUEST_MAGIC);
745
746         switch (request->child_state) {
747         case REQUEST_QUEUED:
748         case REQUEST_RUNNING:
749                 when = request->received;
750                 when.tv_sec += mainconfig.max_request_time;
751
752                 if (timercmp(&now, &when, <)) {
753                         callback = wait_a_bit;
754                 } else {
755                         /* FIXME: kill unresponsive children? */
756                         radlog(L_ERR, "WARNING: Unresponsive child (id %lu) for request %d, in module %s component %s",
757                                (unsigned long)request->child_pid, request->number,
758                                request->module ? request->module : "<server core>",
759                                request->component ? request->component : "<server core>");             
760
761                         request->master_state = REQUEST_STOP_PROCESSING;
762
763                         request->delay = 500000;
764                         tv_add(&request->when, request->delay);
765                         callback = wait_for_child_to_die;
766                 }
767                 request->delay += request->delay >> 1;
768                 break;
769
770         case REQUEST_REJECT_DELAY:
771         case REQUEST_CLEANUP_DELAY:
772                 request->child_pid = NO_SUCH_CHILD_PID;
773
774         case REQUEST_PROXIED:
775                 rad_assert(request->next_callback != NULL);
776
777                 request->when = request->next_when;
778                 callback = request->next_callback;
779                 request->next_callback = NULL;
780                 break;
781
782                 /*
783                  *      Mark the request as no longer running,
784                  *      and clean it up.
785                  */
786         case REQUEST_DONE:
787                 request->child_pid = NO_SUCH_CHILD_PID;
788                 cleanup_delay(request);
789                 return;
790
791         default:
792                 rad_panic("Internal sanity check failure");
793                 return;
794         }
795
796         INSERT_EVENT(callback, request);
797 }
798
799
800 static int request_pre_handler(REQUEST *request)
801 {
802         int rcode;
803
804         rad_assert(request->magic == REQUEST_MAGIC);
805         rad_assert(request->packet != NULL);
806         rad_assert(request->packet->dst_port != 0);
807
808         request->child_state = REQUEST_RUNNING;
809
810         /*
811          *      Don't decode the packet if it's an internal "fake"
812          *      request.  Instead, just return so that the caller can
813          *      process it.
814          */
815         if (request->packet->dst_port == 0) {
816                 request->username = pairfind(request->packet->vps,
817                                              PW_USER_NAME);
818                 request->password = pairfind(request->packet->vps,
819                                              PW_USER_PASSWORD);
820                 return 1;
821         }
822
823         /*
824          *      Put the decoded packet into it's proper place.
825          */
826         if (request->proxy_reply != NULL) {
827                 rcode = request->proxy_listener->decode(request->proxy_listener,
828                                                         request);
829         } else {
830                 rcode = request->listener->decode(request->listener, request);
831         }
832
833         if (rcode < 0) {
834                 radlog(L_ERR, "%s Dropping packet without response.", librad_errstr);
835                 request->child_state = REQUEST_DONE;
836                 return 0;
837         }
838
839         if (!request->proxy) {
840                 request->username = pairfind(request->packet->vps,
841                                              PW_USER_NAME);
842         } else {
843                 int post_proxy_type = 0;
844                 VALUE_PAIR *vp;
845
846                 /*
847                  *      Delete any reply we had accumulated until now.
848                  */
849                 pairfree(&request->reply->vps);
850                 
851                 /*
852                  *      Run the packet through the post-proxy stage,
853                  *      BEFORE playing games with the attributes.
854                  */
855                 vp = pairfind(request->config_items, PW_POST_PROXY_TYPE);
856                 if (vp) {
857                         DEBUG2("  Found Post-Proxy-Type %s", vp->vp_strvalue);
858                         post_proxy_type = vp->vp_integer;
859                 }
860                 rcode = module_post_proxy(post_proxy_type, request);
861                 
862                 /*
863                  *      Delete the Proxy-State Attributes from the reply.
864                  *      These include Proxy-State attributes from us and
865                  *      remote server.
866                  */
867                 pairdelete(&request->proxy_reply->vps, PW_PROXY_STATE);
868                 
869                 /*
870                  *      Add the attributes left in the proxy reply to
871                  *      the reply list.
872                  */
873                 pairadd(&request->reply->vps, request->proxy_reply->vps);
874                 request->proxy_reply->vps = NULL;
875                 
876                 /*
877                  *      Free proxy request pairs.
878                  */
879                 pairfree(&request->proxy->vps);
880                 
881                 switch (rcode) {
882                 default:  /* Don't Do Anything */
883                         break;
884                 case RLM_MODULE_FAIL:
885                         /* FIXME: debug print stuff */
886                         request->child_state = REQUEST_DONE;
887                         return 0;
888
889                 case RLM_MODULE_HANDLED:
890                         /* FIXME: debug print stuff */
891                         request->child_state = REQUEST_DONE;
892                         return 0;
893                 }
894         }
895
896         return 1;
897 }
898
899
900 /*
901  *      Return 1 if we did proxy it, or the proxy attempt failed
902  *      completely.  Either way, the caller doesn't touch the request
903  *      any more if we return 1.
904  */
905 static int successfully_proxied_request(REQUEST *request)
906 {
907         int rcode;
908         int pre_proxy_type = 0;
909         VALUE_PAIR *realmpair;
910         VALUE_PAIR *strippedname;
911         VALUE_PAIR *vp;
912         char *realmname;
913         home_server *home;
914         struct timeval when;
915         REALM *realm = NULL;
916         home_pool_t *pool;
917         char buffer[128];
918
919         realmpair = pairfind(request->config_items, PW_PROXY_TO_REALM);
920         if (!realmpair ||
921             (realmpair->length == 0)) {
922                 return 0;
923         }
924
925         realmname = (char *) realmpair->vp_strvalue;
926
927         realm = realm_find(realmname);
928         if (!realm) {
929                 DEBUG2("ERROR: Cannot proxy to unknown realm %s",
930                        realmname);
931         reject_request:
932                 /*
933                  *      Failed proxying means silently discard
934                  *      accounting responses.
935                  */
936                 if (request->packet->code == PW_ACCOUNTING_REQUEST) {
937                         request->child_state = REQUEST_DONE;
938                         return 1;
939                 }
940                 
941                 rad_assert(request->packet->code == PW_AUTHENTICATION_REQUEST);
942                 
943                 request->reply->code = PW_AUTHENTICATION_REJECT;
944
945                 return 0;
946         }
947
948         /*
949          *      Figure out which pool to use.
950          */
951         if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
952                 pool = realm->auth_pool;
953
954         } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
955                 pool = realm->acct_pool;
956
957         } else {
958                 rad_panic("Internal sanity check failed");
959         }
960
961         if (!pool) {
962                 DEBUG2(" WARNING: Cancelling proxy to Realm %s, as the realm is local.",
963                        realmname);
964                 return 0;
965         }
966
967         home = home_server_ldb(realmname, pool, request);
968         if (!home) {
969                 /*
970                  *      FIXME: Run the request through Post-Proxy-Type = Fail
971                  */
972                 DEBUG2("ERROR: Failed to find live home server for realm %s",
973                        realmname);
974                 goto reject_request;
975         }
976         request->home_pool = pool;
977
978         /*
979          *      Remember that we sent the request to a Realm.
980          */
981         pairadd(&request->packet->vps,
982                 pairmake("Realm", realmname, T_OP_EQ));
983
984         /*
985          *      We read the packet from a detail file, AND it came from
986          *      the server we're about to send it to.  Don't do that.
987          */
988         if ((request->packet->code == PW_ACCOUNTING_REQUEST) &&
989             (request->listener->type == RAD_LISTEN_DETAIL) &&
990             (home->ipaddr.af == AF_INET) &&
991             (request->packet->src_ipaddr.af == AF_INET) &&
992             (home->ipaddr.ipaddr.ip4addr.s_addr == request->packet->src_ipaddr.ipaddr.ip4addr.s_addr)) {
993                 DEBUG2("    rlm_realm: Packet came from realm %s, proxy cancelled", realmname);
994                 return 0;
995         }
996
997         /*
998          *      Allocate the proxy packet, only if it wasn't already
999          *      allocated by a module.  This check is mainly to support
1000          *      the proxying of EAP-TTLS and EAP-PEAP tunneled requests.
1001          *
1002          *      In those cases, the EAP module creates a "fake"
1003          *      request, and recursively passes it through the
1004          *      authentication stage of the server.  The module then
1005          *      checks if the request was supposed to be proxied, and
1006          *      if so, creates a proxy packet from the TUNNELED request,
1007          *      and not from the EAP request outside of the tunnel.
1008          *
1009          *      The proxy then works like normal, except that the response
1010          *      packet is "eaten" by the EAP module, and encapsulated into
1011          *      an EAP packet.
1012          */
1013         if (!request->proxy) {
1014                 if ((request->proxy = rad_alloc(TRUE)) == NULL) {
1015                         radlog(L_ERR|L_CONS, "no memory");
1016                         exit(1);
1017                 }
1018
1019                 /*
1020                  *      Copy the request, then look up name and
1021                  *      plain-text password in the copy.
1022                  *
1023                  *      Note that the User-Name attribute is the
1024                  *      *original* as sent over by the client.  The
1025                  *      Stripped-User-Name attribute is the one hacked
1026                  *      through the 'hints' file.
1027                  */
1028                 request->proxy->vps =  paircopy(request->packet->vps);
1029         }
1030
1031         /*
1032          *      Strip the name, if told to.
1033          *
1034          *      Doing it here catches the case of proxied tunneled
1035          *      requests.
1036          */
1037         if (realm->striprealm == TRUE &&
1038            (strippedname = pairfind(request->proxy->vps, PW_STRIPPED_USER_NAME)) != NULL) {
1039                 /*
1040                  *      If there's a Stripped-User-Name attribute in
1041                  *      the request, then use THAT as the User-Name
1042                  *      for the proxied request, instead of the
1043                  *      original name.
1044                  *
1045                  *      This is done by making a copy of the
1046                  *      Stripped-User-Name attribute, turning it into
1047                  *      a User-Name attribute, deleting the
1048                  *      Stripped-User-Name and User-Name attributes
1049                  *      from the vps list, and making the new
1050                  *      User-Name the head of the vps list.
1051                  */
1052                 vp = pairfind(request->proxy->vps, PW_USER_NAME);
1053                 if (!vp) {
1054                         vp = paircreate(PW_USER_NAME, PW_TYPE_STRING);
1055                         if (!vp) {
1056                                 radlog(L_ERR|L_CONS, "no memory");
1057                                 exit(1);
1058                         }
1059                         vp->next = request->proxy->vps;
1060                         request->proxy->vps = vp;
1061                 }
1062                 memcpy(vp->vp_strvalue, strippedname->vp_strvalue,
1063                        sizeof(vp->vp_strvalue));
1064                 vp->length = strippedname->length;
1065
1066                 /*
1067                  *      Do NOT delete Stripped-User-Name.
1068                  */
1069         }
1070         
1071         /*
1072          *      If there is no PW_CHAP_CHALLENGE attribute but
1073          *      there is a PW_CHAP_PASSWORD we need to add it
1074          *      since we can't use the request authenticator
1075          *      anymore - we changed it.
1076          */
1077         if (pairfind(request->proxy->vps, PW_CHAP_PASSWORD) &&
1078             pairfind(request->proxy->vps, PW_CHAP_CHALLENGE) == NULL) {
1079                 vp = paircreate(PW_CHAP_CHALLENGE, PW_TYPE_STRING);
1080                 if (!vp) {
1081                         radlog(L_ERR|L_CONS, "no memory");
1082                         exit(1);
1083                 }
1084                 vp->length = AUTH_VECTOR_LEN;
1085                 memcpy(vp->vp_strvalue, request->packet->vector, AUTH_VECTOR_LEN);
1086                 pairadd(&(request->proxy->vps), vp);
1087         }
1088
1089         /*
1090          *      The RFC's say we have to do this, but FreeRADIUS
1091          *      doesn't need it.
1092          */
1093         vp = paircreate(PW_PROXY_STATE, PW_TYPE_STRING);
1094         if (!vp) {
1095                 radlog(L_ERR|L_CONS, "no memory");
1096                 exit(1);
1097         }
1098         sprintf(vp->vp_strvalue, "%d", request->packet->id);
1099         vp->length = strlen(vp->vp_strvalue);
1100
1101         pairadd(&request->proxy->vps, vp);
1102
1103         /*
1104          *      Should be done BEFORE inserting into proxy hash, as
1105          *      pre-proxy may use this information, or change it.
1106          */
1107         request->proxy->code = request->packet->code;
1108         request->proxy->dst_ipaddr = home->ipaddr;
1109         request->proxy->dst_port = home->port;
1110         request->home_server = home;
1111
1112         /*
1113          *      Call the pre-proxy routines.
1114          */
1115         vp = pairfind(request->config_items, PW_PRE_PROXY_TYPE);
1116         if (vp) {
1117                 DEBUG2("  Found Pre-Proxy-Type %s", vp->vp_strvalue);
1118                 pre_proxy_type = vp->vp_integer;
1119         }
1120         rcode = module_pre_proxy(pre_proxy_type, request);
1121         switch (rcode) {
1122         case RLM_MODULE_FAIL:
1123         case RLM_MODULE_INVALID:
1124         case RLM_MODULE_NOTFOUND:
1125         case RLM_MODULE_REJECT:
1126         case RLM_MODULE_USERLOCK:
1127         default:
1128                 /* FIXME: debug print failed stuff */
1129                 goto reject_request;
1130
1131         case RLM_MODULE_HANDLED:
1132                 return 0;
1133
1134         /*
1135          *      Only proxy the packet if the pre-proxy code succeeded.
1136          */
1137         case RLM_MODULE_NOOP:
1138         case RLM_MODULE_OK:
1139         case RLM_MODULE_UPDATED:
1140                 break;
1141         }
1142
1143         /*
1144          *      If it's a fake request, don't send the proxy
1145          *      packet.  The outer tunnel session will take
1146          *      care of doing that.
1147          *
1148          *      FIXME: Update the home_server to be NULL?
1149          */
1150         if (request->packet->dst_port == 0) {
1151                 return 1;
1152         }
1153         
1154         if (!insert_into_proxy_hash(request)) {
1155                 DEBUG("ERROR: Failed to proxy request %d", request->number);
1156                 goto reject_request;
1157         }
1158
1159         request->proxy_listener->encode(request->proxy_listener, request);
1160
1161         when = request->received;
1162         when.tv_sec += mainconfig.max_request_time;
1163         
1164         gettimeofday(&request->proxy_when, NULL);
1165
1166         request->next_when = request->proxy_when;
1167         request->next_when.tv_sec += home->response_window;
1168
1169         rad_assert(home->response_window > 0);
1170         
1171         if (timercmp(&when, &request->next_when, <)) {
1172                 request->next_when = when;
1173         }
1174         request->next_callback = no_response_to_proxied_request;
1175
1176         DEBUG2("Proxying request %d to realm %s, home server %s port %d",
1177                request->number, realmname,
1178                inet_ntop(request->proxy->dst_ipaddr.af,
1179                          &request->proxy->dst_ipaddr.ipaddr,
1180                          buffer, sizeof(buffer)),
1181                request->proxy->dst_port);
1182         
1183         /*
1184          *      Note that we set proxied AFTER creating the packet,
1185          *      but BEFORE actually sending it.
1186          *
1187          *      Once we send it, the request is tainted, as
1188          *      another thread may have picked it up.  Don't
1189          *      touch it!
1190          */
1191         request->num_proxied_requests = 1;
1192         request->num_proxied_responses = 0;
1193         request->child_state = REQUEST_PROXIED;
1194         request->proxy_listener->send(request->proxy_listener,
1195                                       request);
1196         return 1;
1197 }
1198
1199
1200 static void request_post_handler(REQUEST *request)
1201 {
1202         int child_state = -1;
1203         struct timeval when;
1204         VALUE_PAIR *vp;
1205
1206         if (request->master_state == REQUEST_STOP_PROCESSING) {
1207                 DEBUG2("Request %d was cancelled.", request->number);
1208                 request->child_state = REQUEST_DONE;
1209                 return;
1210         }
1211
1212         if (request->child_state != REQUEST_RUNNING) {
1213                 rad_panic("Internal sanity check failed");
1214         }
1215
1216         if ((request->reply->code == 0) &&
1217             ((vp = pairfind(request->config_items, PW_AUTH_TYPE)) != NULL) &&
1218             (vp->vp_integer == PW_AUTHTYPE_REJECT)) {
1219                 request->reply->code = PW_AUTHENTICATION_REJECT;
1220         }
1221
1222         if (mainconfig.proxy_requests &&
1223             (request->packet->code != PW_STATUS_SERVER) &&
1224             (request->reply->code == 0) &&
1225             !request->proxy &&
1226             successfully_proxied_request(request)) {
1227                 return;
1228         }
1229
1230         /*
1231          *      Fake requests don't get encoded or signed.  The caller
1232          *      also requires the reply VP's, so we don't free them
1233          *      here!
1234          */
1235         if (request->packet->dst_port == 0) {
1236                 /* FIXME: DEBUG going to the next request */
1237                 request->child_state = REQUEST_DONE;
1238                 return;
1239         }
1240
1241         /*
1242          *      Copy Proxy-State from the request to the reply.
1243          */
1244         vp = paircopy2(request->packet->vps, PW_PROXY_STATE);
1245         if (vp) pairadd(&request->reply->vps, vp);
1246
1247         /*
1248          *      Access-Requests get delayed or cached.
1249          */
1250         if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
1251                 gettimeofday(&request->next_when, NULL);
1252
1253                 if (request->reply->code == 0) {
1254                         DEBUG2("There was no response configured: rejecting request %d",
1255                                request->number);
1256                         request->reply->code = PW_AUTHENTICATION_REJECT;
1257                 }
1258                 
1259                 /*
1260                  *      Run rejected packets through
1261                  *
1262                  *      Post-Auth-Type = Reject
1263                  */
1264                 if (request->reply->code == PW_AUTHENTICATION_REJECT) {
1265                         vp = pairmake("Post-Auth-Type", "Reject", T_OP_SET);
1266                         if (vp) {
1267                                 pairdelete(&request->config_items, PW_POST_AUTH_TYPE);
1268                                 pairadd(&request->config_items, vp);
1269                                 rad_postauth(request);
1270                         } /* else no Reject section defined */
1271
1272                         /*
1273                          *      If configured, delay Access-Reject packets.
1274                          *
1275                          *      If mainconfig.reject_delay = 0, we discover
1276                          *      that we have to send the packet now.
1277                          */
1278                         when = request->received;
1279                         when.tv_sec += mainconfig.reject_delay;
1280                         
1281                         if (timercmp(&when, &request->next_when, >)) {
1282                                 DEBUG2("Delaying reject of request %d for %d seconds",
1283                                        request->number,
1284                                        mainconfig.reject_delay);
1285                                 request->next_when = when;
1286                                 request->next_callback = reject_delay;
1287                                 request->child_state = REQUEST_REJECT_DELAY;
1288                                 return;
1289                         }
1290                 }
1291
1292                 request->next_when.tv_sec += mainconfig.cleanup_delay;
1293                 request->next_callback = cleanup_delay;
1294                 child_state = REQUEST_CLEANUP_DELAY;
1295
1296         } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
1297                 request->next_callback = NULL; /* just to be safe */
1298                 child_state = REQUEST_DONE;
1299
1300                 /*
1301                  *      FIXME: Status-Server should probably not be
1302                  *      handled here...
1303                  */
1304         } else if (request->packet->code == PW_STATUS_SERVER) {
1305                 request->next_callback = NULL;
1306                 child_state = REQUEST_DONE;
1307
1308         } else {
1309                 rad_panic("Unknown packet type");
1310         }
1311
1312         /*
1313          *      Encode, sign, and send.  The accounting request
1314          *      handler takes care of suppressing responses when
1315          *      request->reply->code == 0.
1316          */
1317         request->listener->send(request->listener, request);
1318
1319         /*
1320          *      Clean up.  These are no longer needed.
1321          */
1322         pairfree(&request->config_items);
1323
1324         pairfree(&request->packet->vps);
1325         request->username = NULL;
1326         request->password = NULL;
1327
1328         pairfree(&request->reply->vps);
1329
1330         if (request->proxy) {
1331                 pairfree(&request->proxy->vps);
1332
1333                 if (request->proxy_reply) {
1334                         pairfree(&request->proxy_reply->vps);
1335                 }
1336         }
1337
1338         DEBUG2("Finished request %d state %d", request->number, child_state);
1339
1340         request->child_state = child_state;
1341 }
1342
1343
1344 static void received_retransmit(REQUEST *request, const RADCLIENT *client)
1345 {
1346         char buffer[128];
1347
1348         RAD_SNMP_TYPE_INC(request->listener, total_dup_requests);
1349         RAD_SNMP_CLIENT_INC(request->listener, client, dup_requests);
1350
1351         switch (request->child_state) {
1352         case REQUEST_QUEUED:
1353         case REQUEST_RUNNING:
1354         discard:
1355                 radlog(L_ERR, "Discarding duplicate request from "
1356                        "client %s port %d - ID: %d due to unfinished request %d",
1357                        client->shortname,
1358                        request->packet->src_port,request->packet->id,
1359                        request->number);
1360                 break;
1361
1362         case REQUEST_PROXIED:
1363                 /*
1364                  *      We're not supposed to have duplicate
1365                  *      accounting packets.  The other states handle
1366                  *      duplicates fine (discard, or send duplicate
1367                  *      reply).  But we do NOT want to retransmit an
1368                  *      accounting request here, because that would
1369                  *      involve updating the Acct-Delay-Time, and
1370                  *      therefore changing the packet Id, etc.
1371                  *
1372                  *      Instead, we just discard the packet.  We may
1373                  *      eventually respond, or the client will send a
1374                  *      new accounting packet.
1375                  */
1376                 if (request->packet->code == PW_ACCOUNTING_REQUEST) {
1377                         goto discard;
1378                 }
1379
1380                 check_for_zombie_home_server(request);
1381
1382                 /*
1383                  *      If we've just discovered that the home server is
1384                  *      dead, send the packet to another one.
1385                  */
1386                 if ((request->packet->dst_port != 0) &&
1387                     (request->home_server->state == HOME_STATE_IS_DEAD)) {
1388                         home_server *home;
1389
1390                         remove_from_proxy_hash(request);
1391
1392                         home = home_server_ldb(NULL, request->home_pool, request);
1393                         if (!home) {
1394                                 DEBUG2("Failed to find live home server for request %d", request->number);
1395                         no_home_servers:
1396                                 /*
1397                                  *      FIXME: Run the request through
1398                                  *      Post-Proxy-Type = Fail
1399                                  *
1400                                  *      Do post-request processing,
1401                                  *      and any insertion of necessary
1402                                  *      events.
1403                                  */
1404                                 request->child_state = REQUEST_RUNNING;
1405                                 request_post_handler(request);
1406                                 wait_a_bit(request);
1407                                 return;
1408                         }
1409                         
1410                         request->proxy->code = request->packet->code;
1411                         request->proxy->dst_ipaddr = home->ipaddr;
1412                         request->proxy->dst_port = home->port;
1413                         request->home_server = home;
1414
1415                         if (!insert_into_proxy_hash(request)) {
1416                                 DEBUG("ERROR: Failed to re-proxy request %d", request->number);
1417                                 goto no_home_servers;
1418                         }
1419
1420                         /*
1421                          *      Free the old packet, to force re-encoding
1422                          */
1423                         free(request->proxy->data);
1424                         request->proxy->data = NULL;
1425                         request->proxy->data_len = 0;
1426
1427                         DEBUG2("RETRY: Proxying request %d to different home server %s port %d",
1428                                request->number,
1429                                inet_ntop(request->proxy->dst_ipaddr.af,
1430                                          &request->proxy->dst_ipaddr.ipaddr,
1431                                          buffer, sizeof(buffer)),
1432                                request->proxy->dst_port);
1433                         
1434                         /*
1435                          *      Restart timers.  Note that we leave
1436                          *      the old timeout in place, as that is a
1437                          *      place-holder for when the request
1438                          *      times out.
1439                          */
1440                         gettimeofday(&request->proxy_when, NULL);
1441                         request->num_proxied_requests = 1;
1442                         request->num_proxied_responses = 0;
1443                         request->child_state = REQUEST_PROXIED;
1444                         request->proxy_listener->send(request->proxy_listener,
1445                                                       request);
1446                         return;
1447                 } /* else the home server is still alive */
1448
1449                 DEBUG2("Sending duplicate proxied request to home server %s port %d - ID: %d",
1450                        inet_ntop(request->proxy->dst_ipaddr.af,
1451                                  &request->proxy->dst_ipaddr.ipaddr,
1452                                  buffer, sizeof(buffer)),
1453                        request->proxy->dst_port,
1454                        request->proxy->id);
1455                 request->num_proxied_requests++;
1456                 request->proxy_listener->send(request->proxy_listener,
1457                                               request);
1458                 break;
1459
1460         case REQUEST_REJECT_DELAY:
1461                 DEBUG2("Waiting to send Access-Reject "
1462                        "to client %s port %d - ID: %d",
1463                        client->shortname,
1464                        request->packet->src_port, request->packet->id);
1465                 break;
1466
1467         case REQUEST_CLEANUP_DELAY:
1468         case REQUEST_DONE:
1469                 DEBUG2("Sending duplicate reply "
1470                        "to client %s port %d - ID: %d",
1471                        client->shortname,
1472                        request->packet->src_port, request->packet->id);
1473                 request->listener->send(request->listener, request);
1474                 break;
1475         }
1476 }
1477
1478
1479 static void received_conflicting_request(REQUEST *request,
1480                                          const RADCLIENT *client)
1481 {
1482         radlog(L_ERR, "Received conflicting packet from "
1483                "client %s port %d - ID: %d due to unfinished request %d.  Giving up on old request.",
1484                client->shortname,
1485                request->packet->src_port, request->packet->id,
1486                request->number);
1487
1488         switch (request->child_state) {
1489         case REQUEST_QUEUED:
1490         case REQUEST_RUNNING:
1491                 request->master_state = REQUEST_STOP_PROCESSING;
1492                 request->delay += request->delay >> 1;
1493
1494                 tv_add(&request->when, request->delay);
1495
1496                 INSERT_EVENT(wait_for_child_to_die, request);
1497                 return;
1498
1499         default:
1500                 break;
1501         }
1502
1503         remove_from_request_hash(request);
1504
1505         /*
1506          *      The request stays in the event queue.  At some point,
1507          *      the child will notice, and we can then delete it.
1508          */
1509 }
1510
1511
1512 static int can_handle_new_request(RADIUS_PACKET *packet,
1513                                   RADCLIENT *client)
1514 {
1515         /*
1516          *      Count the total number of requests, to see if
1517          *      there are too many.  If so, return with an
1518          *      error.
1519          */
1520         if (mainconfig.max_requests) {
1521                 int request_count = lrad_packet_list_num_elements(pl);
1522                 
1523                 /*
1524                  *      This is a new request.  Let's see if
1525                  *      it makes us go over our configured
1526                  *      bounds.
1527                  */
1528                 if (request_count > mainconfig.max_requests) {
1529                         radlog(L_ERR, "Dropping request (%d is too many): "
1530                                "from client %s port %d - ID: %d", request_count,
1531                                client->shortname,
1532                                packet->src_port, packet->id);
1533                         radlog(L_INFO, "WARNING: Please check the %s file.\n"
1534                                "\tThe value for 'max_requests' is probably set too low.\n", mainconfig.radiusd_conf);
1535                         return 0;
1536                 } /* else there were a small number of requests */
1537         } /* else there was no configured limit for requests */
1538
1539         /*
1540          *      FIXME: Add per-client checks.  If one client is sending
1541          *      too many packets, start discarding them.
1542          *
1543          *      We increment the counters here, and decrement them
1544          *      when the response is sent... somewhere in this file.
1545          */
1546         
1547         /*
1548          *      FUTURE: Add checks for system load.  If the system is
1549          *      busy, start dropping requests...
1550          *
1551          *      We can probably keep some statistics ourselves...  if
1552          *      there are more requests coming in than we can handle,
1553          *      start dropping some.
1554          */
1555         
1556         return 1;
1557 }
1558
1559
1560 int received_request(rad_listen_t *listener,
1561                      RADIUS_PACKET *packet, REQUEST **prequest,
1562                      RADCLIENT *client)
1563 {
1564         RADIUS_PACKET **packet_p;
1565         REQUEST *request = NULL;
1566
1567         packet_p = lrad_packet_list_find(pl, packet);
1568         if (packet_p) {
1569                 request = lrad_packet2myptr(REQUEST, packet, packet_p);
1570
1571                 if (memcmp(request->packet->vector, packet->vector,
1572                            sizeof(packet->vector)) == 0) {
1573                         received_retransmit(request, client);
1574                         return 0;
1575                 }
1576
1577                 /*
1578                  *      The new request is different from the old one,
1579                  *      but maybe the old is finished.  If so, delete
1580                  *      the old one.
1581                  */
1582                 switch (request->child_state) {
1583                 default:
1584                         received_conflicting_request(request, client);
1585                         request = NULL;
1586                         break;
1587
1588                 case REQUEST_REJECT_DELAY:
1589                 case REQUEST_CLEANUP_DELAY:
1590                         request->child_state = REQUEST_DONE;
1591                 case REQUEST_DONE:
1592                         cleanup_delay(request);
1593                         request = NULL;
1594                         break;
1595                 }
1596
1597
1598         }
1599
1600         /*
1601          *      We may want to quench the new request.
1602          */
1603         if (!can_handle_new_request(packet, client)) {
1604                 return 0;
1605         }
1606
1607         /*
1608          *      Create and initialize the new request.
1609          */
1610         request = request_alloc(); /* never fails */
1611         
1612         if ((request->reply = rad_alloc(0)) == NULL) {
1613                 radlog(L_ERR, "No memory");
1614                 exit(1);
1615         }
1616
1617         request->listener = listener;
1618         request->packet = packet;
1619         request->packet->timestamp = request->timestamp;
1620         request->number = request_num_counter++;
1621         strlcpy(request->secret, client->secret, sizeof(request->secret));
1622         
1623         /*
1624          *      Remember the request in the list.
1625          */
1626         if (!lrad_packet_list_insert(pl, &request->packet)) {
1627                 radlog(L_ERR, "Failed to insert request %d in the list of live requests: discarding", request->number);
1628                 request_free(&request);
1629                 return 0;
1630         }
1631         request->in_request_hash = TRUE;
1632         
1633         /*
1634          *      The request passes many of our sanity checks.
1635          *      From here on in, if anything goes wrong, we
1636          *      send a reject message, instead of dropping the
1637          *      packet.
1638          */
1639
1640         /*
1641          *      Build the reply template from the request.
1642          */
1643
1644         request->reply->sockfd = request->packet->sockfd;
1645         request->reply->dst_ipaddr = request->packet->src_ipaddr;
1646         request->reply->src_ipaddr = request->packet->dst_ipaddr;
1647         request->reply->dst_port = request->packet->src_port;
1648         request->reply->src_port = request->packet->dst_port;
1649         request->reply->id = request->packet->id;
1650         request->reply->code = 0; /* UNKNOWN code */
1651         memcpy(request->reply->vector, request->packet->vector,
1652                sizeof(request->reply->vector));
1653         request->reply->vps = NULL;
1654         request->reply->data = NULL;
1655         request->reply->data_len = 0;
1656
1657         request->master_state = REQUEST_ACTIVE;
1658         request->child_state = REQUEST_QUEUED;
1659         request->next_callback = NULL;
1660
1661         gettimeofday(&request->received, NULL);
1662         request->when = request->received;
1663         request->delay = USEC / 10;
1664
1665         tv_add(&request->when, request->delay);
1666
1667         INSERT_EVENT(wait_a_bit, request);
1668
1669         *prequest = request;
1670         return 1;
1671 }
1672
1673
1674 REQUEST *received_proxy_response(RADIUS_PACKET *packet)
1675 {
1676         char            buffer[128];
1677         home_server     *home;
1678         REQUEST         *request;
1679
1680         if (!home_server_find(&packet->src_ipaddr, packet->src_port)) {
1681                 radlog(L_ERR, "Ignoring request from unknown home server %s port %d",
1682                        inet_ntop(packet->src_ipaddr.af,
1683                                  &packet->src_ipaddr.ipaddr,
1684                                  buffer, sizeof(buffer)),
1685                                packet->src_port);
1686                 rad_free(&packet);
1687                 return NULL;
1688         }
1689
1690         /*
1691          *      Also removes from the proxy hash if responses == requests
1692          */
1693         request = lookup_in_proxy_hash(packet);
1694
1695         if (!request) {
1696                 radlog(L_PROXY, "No outstanding request was found for proxy reply from home server %s port %d - ID %d",
1697                        inet_ntop(packet->src_ipaddr.af,
1698                                  &packet->src_ipaddr.ipaddr,
1699                                  buffer, sizeof(buffer)),
1700                        packet->src_port, packet->id);
1701                 rad_free(&packet);
1702                 return NULL;
1703         }
1704
1705         home = request->home_server;
1706
1707         gettimeofday(&now, NULL);
1708         home->state = HOME_STATE_ALIVE;
1709
1710         if (request->reply && request->reply->code != 0) {
1711                 DEBUG2("We already replied to this request.  Discarding response from home server.");
1712                 rad_free(&packet);
1713                 return NULL;
1714         }
1715
1716         /*
1717          *      We had previously received a reply, so we don't need
1718          *      to do anything here.
1719          */
1720         if (request->proxy_reply) {
1721                 if (memcmp(request->proxy_reply->vector,
1722                            packet->vector,
1723                            sizeof(request->proxy_reply->vector)) == 0) {
1724                         DEBUG2("Discarding duplicate reply from home server %s port %d  - ID: %d for request %d",
1725                                inet_ntop(packet->src_ipaddr.af,
1726                                          &packet->src_ipaddr.ipaddr,
1727                                          buffer, sizeof(buffer)),
1728                                packet->src_port, packet->id,
1729                                request->number);
1730                 } else {
1731                         /*
1732                          *      ? The home server gave us a new proxy
1733                          *      reply, which doesn't match the old
1734                          *      one.  Delete it.
1735                          */
1736                         DEBUG2("Ignoring conflicting proxy reply");
1737                 }
1738
1739                 /* assert that there's an event queued for request? */
1740                 rad_free(&packet);
1741                 return NULL;
1742         }
1743
1744         switch (request->child_state) {
1745         case REQUEST_QUEUED:
1746         case REQUEST_RUNNING:
1747                 rad_panic("Internal sanity check failed for child state");
1748                 break;
1749
1750         case REQUEST_REJECT_DELAY:
1751         case REQUEST_CLEANUP_DELAY:
1752         case REQUEST_DONE:
1753                 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'",
1754                        inet_ntop(packet->src_ipaddr.af,
1755                                  &packet->src_ipaddr.ipaddr,
1756                                  buffer, sizeof(buffer)),
1757                        packet->src_port, packet->id,
1758                        request->number);
1759                 /* assert that there's an event queued for request? */
1760                 rad_free(&packet);
1761                 return NULL;
1762                 
1763         case REQUEST_PROXIED:
1764                 break;
1765         }
1766
1767         request->proxy_reply = packet;
1768
1769 #if 0
1770         /*
1771          *      Perform RTT calculations, as per RFC 2988 (for TCP).
1772          *      Note that we do so only if we sent one request, and
1773          *      received one response.  If we sent two requests, we
1774          *      have no idea if the response is for the first, or for
1775          *      the second request/
1776          */
1777         if (request->num_proxied_requests == 1) {
1778                 int rtt;
1779                 home_server *home = request->home_server;
1780
1781                 rtt = now.tv_sec - request->proxy_when.tv_sec;
1782                 rtt *= USEC;
1783                 rtt += now.tv_usec;
1784                 rtt -= request->proxy_when.tv_usec;
1785
1786                 if (!home->has_rtt) {
1787                         home->has_rtt = TRUE;
1788
1789                         home->srtt = rtt;
1790                         home->rttvar = rtt / 2;
1791
1792                 } else {
1793                         home->rttvar -= home->rttvar >> 2;
1794                         home->rttvar += (home->srtt - rtt);
1795                         home->srtt -= home->srtt >> 3;
1796                         home->srtt += rtt >> 3;
1797                 }
1798
1799                 home->rto = home->srtt;
1800                 if (home->rttvar > (USEC / 4)) {
1801                         home->rto += home->rttvar * 4;
1802                 } else {
1803                         home->rto += USEC;
1804                 }
1805         }
1806 #endif
1807
1808         /*
1809          *      There's no incoming request, so it's a proxied packet
1810          *      we originated.
1811          */
1812         if (!request->packet) {
1813                 received_response_to_ping(request);
1814                 return NULL;
1815         }
1816
1817         request->child_state = REQUEST_QUEUED;
1818         request->when = now;
1819         request->delay = USEC / 10;
1820         tv_add(&request->when, request->delay);
1821
1822         /*
1823          *      Wait a bit will take care of max_request_time
1824          */
1825         INSERT_EVENT(wait_a_bit, request);
1826
1827         return request;
1828 }
1829
1830
1831 /*
1832  *      Externally-visibly functions.
1833  */
1834 int radius_event_init(int spawn_flag)
1835 {
1836         if (el) return 0;
1837
1838         time(&start_time);
1839
1840         el = lrad_event_list_create();
1841         if (!el) return 0;
1842
1843         pl = lrad_packet_list_create(0);
1844         if (!el) return 0;
1845
1846         request_num_counter = 0;
1847
1848         /*
1849          *      Move all of the thread calls to this file?
1850          *
1851          *      It may be best for the mutexes to be in this file...
1852          */
1853         have_children = spawn_flag;
1854
1855         if (mainconfig.proxy_requests) {
1856                 int i;
1857                 rad_listen_t *listener;
1858                         
1859                 /*
1860                  *      Create the tree for managing proxied requests and
1861                  *      responses.
1862                  */
1863                 proxy_list = lrad_packet_list_create(1);
1864                 if (!proxy_list) return 0;
1865                 
1866 #ifdef HAVE_PTHREAD_H
1867                 if (pthread_mutex_init(&proxy_mutex, NULL) != 0) {
1868                         radlog(L_ERR, "FATAL: Failed to initialize proxy mutex: %s",
1869                                strerror(errno));
1870                         exit(1);
1871                 }
1872 #endif
1873
1874                 /*
1875                  *      Mark the Fd's as unused.
1876                  */
1877                 for (i = 0; i < 32; i++) proxy_fds[i] = -1;
1878                 
1879                 i = -1;
1880
1881                 for (listener = mainconfig.listen;
1882                      listener != NULL;
1883                      listener = listener->next) {
1884                         if (listener->type == RAD_LISTEN_PROXY) {
1885                                 /*
1886                                  *      FIXME: This works only because we
1887                                  *      start off with one proxy socket.
1888                                  */
1889                                 rad_assert(proxy_fds[listener->fd & 0x1f] == -1);
1890                                 rad_assert(proxy_listeners[listener->fd & 0x1f] == NULL);
1891
1892                                 proxy_fds[listener->fd & 0x1f] = listener->fd;
1893                                 proxy_listeners[listener->fd & 0x1f] = listener;
1894                                 if (!lrad_packet_list_socket_add(proxy_list, listener->fd)) {
1895                                         rad_assert(0 == 1);
1896                                 }
1897                                 i = listener->fd;
1898                         }
1899                 }
1900
1901                 if (mainconfig.proxy_requests) rad_assert(i >= 0);
1902         }
1903
1904         thread_pool_init(spawn_flag);
1905
1906         return 1;
1907 }
1908
1909
1910 static int request_hash_cb(void *ctx, void *data)
1911 {
1912         ctx = ctx;              /* -Wunused */
1913         REQUEST *request = lrad_packet2myptr(REQUEST, packet, data);
1914
1915         rad_assert(request->in_proxy_hash == FALSE);
1916
1917         lrad_event_delete(el, &request->ev);
1918         remove_from_request_hash(request);
1919         request_free(&request);
1920
1921         return 0;
1922 }
1923
1924
1925 static int proxy_hash_cb(void *ctx, void *data)
1926 {
1927         ctx = ctx;              /* -Wunused */
1928         REQUEST *request = lrad_packet2myptr(REQUEST, proxy, data);
1929
1930         lrad_packet_list_yank(proxy_list, request->proxy);
1931         request->in_proxy_hash = FALSE;
1932
1933         if (!request->in_request_hash) {
1934                 lrad_event_delete(el, &request->ev);
1935                 request_free(&request);
1936         }
1937
1938         return 0;
1939 }
1940
1941
1942 void radius_event_free(void)
1943 {
1944         /*
1945          *      FIXME: Stop all threads, or at least check that
1946          *      they're all waiting on the semaphore, and the queues
1947          *      are empty.
1948          */
1949
1950         /*
1951          *      There are requests in the proxy hash that aren't
1952          *      referenced from anywhere else.  Remove them first.
1953          */
1954         if (proxy_list) {
1955                 PTHREAD_MUTEX_LOCK(&proxy_mutex);
1956                 lrad_packet_list_walk(proxy_list, NULL, proxy_hash_cb);
1957                 PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
1958                 lrad_packet_list_free(proxy_list);
1959                 proxy_list = NULL;
1960         }
1961
1962         lrad_packet_list_walk(pl, NULL, request_hash_cb);
1963
1964         lrad_packet_list_free(pl);
1965         pl = NULL;
1966
1967         lrad_event_list_free(el);
1968 }
1969
1970 int radius_event_process(struct timeval **pptv)
1971 {
1972         int rcode;
1973         struct timeval when;
1974
1975         if (!el) return 0;
1976
1977         if (lrad_event_list_num_elements(el) == 0) {
1978                 *pptv = NULL;
1979                 return 1;
1980         }
1981
1982         gettimeofday(&now, NULL);
1983         when = now;
1984
1985         do {
1986                 rcode = lrad_event_run(el, &when);
1987         } while (rcode == 1);
1988
1989         gettimeofday(&now, NULL);
1990
1991         if ((when.tv_sec == 0) && (when.tv_usec == 0)) {
1992                 if (lrad_event_list_num_elements(el) == 0) {
1993                         *pptv = NULL;
1994                         return 1;
1995                 }
1996                 rad_panic("Internal sanity check failed");
1997                 
1998         } else if (timercmp(&now, &when, >)) {
1999                 DEBUG3("Event in the past... compensating");
2000                 when.tv_sec = 0;
2001                 when.tv_usec = 1;
2002
2003         } else {
2004                 when.tv_sec -= now.tv_sec;
2005                 when.tv_usec -= now.tv_usec;
2006                 if (when.tv_usec < 0) {
2007                         when.tv_sec--;
2008                         when.tv_usec += USEC;
2009                 }
2010         }
2011         **pptv = when;
2012
2013         return 1;
2014 }
2015
2016 void radius_handle_request(REQUEST *request, RAD_REQUEST_FUNP fun)
2017 {
2018         if (!request_pre_handler(request)) {
2019                 DEBUG2("Going to the next request at X");
2020                 return;
2021         }
2022         
2023         rad_assert(fun != NULL);
2024         rad_assert(request != NULL);
2025         
2026         fun(request);
2027         
2028         request_post_handler(request);
2029         DEBUG2("Going to the next request");
2030         return;
2031 }
2032