/
event.c
3814 lines (3183 loc) · 91.4 KB
/
event.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* event.c Server event handling
*
* Version: $Id$
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* Copyright 2007 The FreeRADIUS server project
* Copyright 2007 Alan DeKok <aland@deployingradius.com>
*/
#include <freeradius-devel/ident.h>
RCSID("$Id$")
#include <freeradius-devel/radiusd.h>
#include <freeradius-devel/modules.h>
#include <freeradius-devel/event.h>
#include <freeradius-devel/detail.h>
#include <freeradius-devel/rad_assert.h>
#include <signal.h>
#include <fcntl.h>
#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif
#define USEC (1000000)
extern pid_t radius_pid;
extern int dont_fork;
extern int check_config;
extern char *debug_condition;
/*
* Ridiculous amounts of local state.
*/
static fr_event_list_t *el = NULL;
static fr_packet_list_t *pl = NULL;
static int request_num_counter = 0;
static struct timeval now;
time_t fr_start_time;
static int have_children;
static int just_started = TRUE;
#ifndef __MINGW32__
#ifdef HAVE_PTHREAD_H
#define WITH_SELF_PIPE (1)
#endif
#endif
#ifdef WITH_SELF_PIPE
static int self_pipe[2];
#endif
#ifdef HAVE_PTHREAD_H
#ifdef WITH_PROXY
static pthread_mutex_t proxy_mutex;
#endif
#define PTHREAD_MUTEX_LOCK if (have_children) pthread_mutex_lock
#define PTHREAD_MUTEX_UNLOCK if (have_children) pthread_mutex_unlock
static pthread_t NO_SUCH_CHILD_PID;
#else
/*
* This is easier than ifdef's throughout the code.
*/
#define PTHREAD_MUTEX_LOCK(_x)
#define PTHREAD_MUTEX_UNLOCK(_x)
int thread_pool_addrequest(REQUEST *request, RAD_REQUEST_FUNP fun)
{
radius_handle_request(request, fun);
return 1;
}
#endif
#define INSERT_EVENT(_function, _ctx) if (!fr_event_insert(el, _function, _ctx, &((_ctx)->when), &((_ctx)->ev))) { _rad_panic(__FILE__, __LINE__, "Failed to insert event"); }
#ifdef WITH_PROXY
static fr_packet_list_t *proxy_list = NULL;
/*
* We keep the proxy FD's here. The RADIUS Id's are marked
* "allocated" per Id, via a bit per proxy FD.
*/
static int proxy_all_used = FALSE;
static int proxy_fds[32];
static rad_listen_t *proxy_listeners[32];
static void check_for_zombie_home_server(REQUEST *request);
static void remove_from_proxy_hash(REQUEST *request);
#else
#define remove_from_proxy_hash(foo)
#endif
static void request_post_handler(REQUEST *request);
static void wait_a_bit(void *ctx);
static void event_socket_handler(fr_event_list_t *xel, UNUSED int fd, void *ctx);
#ifdef WITH_DETAIL
static void event_poll_detail(void *ctx);
#endif
static void NEVER_RETURNS _rad_panic(const char *file, unsigned int line,
const char *msg)
{
radlog(L_ERR, "[%s:%d] %s", file, line, msg);
_exit(1);
}
#define rad_panic(x) _rad_panic(__FILE__, __LINE__, x)
static void tv_add(struct timeval *tv, int usec_delay)
{
if (usec_delay >= USEC) {
tv->tv_sec += usec_delay / USEC;
usec_delay %= USEC;
}
tv->tv_usec += usec_delay;
if (tv->tv_usec >= USEC) {
tv->tv_sec += tv->tv_usec / USEC;
tv->tv_usec %= USEC;
}
}
static void remove_from_request_hash(REQUEST *request)
{
if (!request->in_request_hash) return;
fr_packet_list_yank(pl, request->packet);
request->in_request_hash = FALSE;
request_stats_final(request);
}
static void ev_request_free(REQUEST **prequest)
{
REQUEST *request;
if (!prequest || !*prequest) return;
request = *prequest;
#ifdef WITH_COA
if (request->coa) {
/*
* Divorce the child from the parent first,
* then clean up the child.
*/
request->coa->parent = NULL;
ev_request_free(&request->coa);
}
/*
* Divorce the parent from the child, and leave the
* parent still alive.
*/
if (request->parent && (request->parent->coa == request)) {
request->parent->coa = NULL;
}
#endif
if (request->ev) fr_event_delete(el, &request->ev);
#ifdef WITH_PROXY
if (request->in_proxy_hash) remove_from_proxy_hash(request);
#endif
if (request->in_request_hash) remove_from_request_hash(request);
rad_assert(request->child_state != REQUEST_QUEUED);
request_free(prequest);
}
#ifdef WITH_PROXY
static REQUEST *lookup_in_proxy_hash(RADIUS_PACKET *reply)
{
RADIUS_PACKET **proxy_p;
REQUEST *request;
PTHREAD_MUTEX_LOCK(&proxy_mutex);
proxy_p = fr_packet_list_find_byreply(proxy_list, reply);
if (!proxy_p) {
PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
return NULL;
}
request = fr_packet2myptr(REQUEST, proxy, proxy_p);
if (!request) {
PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
return NULL;
}
request->num_proxied_responses++;
/*
* Catch the most common case of everything working
* correctly.
*/
if (request->num_proxied_requests == request->num_proxied_responses) {
fr_packet_list_yank(proxy_list, request->proxy);
fr_packet_list_id_free(proxy_list, request->proxy);
request->in_proxy_hash = FALSE;
}
/*
* On the FIRST reply, decrement the count of outstanding
* requests. Note that this is NOT the count of sent
* packets, but whether or not the home server has
* responded at all.
*/
if (!request->proxy_reply &&
request->home_server &&
request->home_server->currently_outstanding) {
request->home_server->currently_outstanding--;
}
PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
return request;
}
static void remove_from_proxy_hash(REQUEST *request)
{
/*
* Check this without grabbing the mutex because it's a
* lot faster that way.
*/
if (!request->in_proxy_hash) return;
/*
* The "not in hash" flag is definitive. However, if the
* flag says that it IS in the hash, there might still be
* a race condition where it isn't.
*/
PTHREAD_MUTEX_LOCK(&proxy_mutex);
if (!request->in_proxy_hash) {
PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
return;
}
fr_packet_list_yank(proxy_list, request->proxy);
fr_packet_list_id_free(proxy_list, request->proxy);
/*
* The home server hasn't replied, but we've given up on
* this request. Don't count this request against the
* home server.
*/
if (!request->proxy_reply &&
request->home_server &&
request->home_server->currently_outstanding) {
request->home_server->currently_outstanding--;
}
/*
* Got from YES in hash, to NO, not in hash while we hold
* the mutex. This guarantees that when another thread
* grans the mutex, the "not in hash" flag is correct.
*/
request->in_proxy_hash = FALSE;
PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
}
static int proxy_add_fds(rad_listen_t *proxy_listener)
{
int i, proxy, found = -1;
proxy = proxy_listener->fd;
for (i = 0; i < 32; i++) {
/*
* Found a free entry. Save the socket,
* and remember where we saved it.
*/
if (proxy_fds[(proxy + i) & 0x1f] == -1) {
found = (proxy + i) & 0x1f;
proxy_fds[found] = proxy;
proxy_listeners[found] = proxy_listener;
break;
}
}
return found;
}
static int proxy_id_alloc(REQUEST *request, RADIUS_PACKET *packet)
{
rad_listen_t *proxy_listener;
if (fr_packet_list_id_alloc(proxy_list, packet)) return 1;
if (proxy_all_used) return 0;
/*
* Allocate a new proxy fd. This function adds
* it to the tail of the list of listeners. With
* some care, this can be thread-safe.
*/
proxy_listener = proxy_new_listener(&packet->src_ipaddr, FALSE);
if (!proxy_listener) {
radlog(L_PROXY, "Failed to create a new socket for proxying requests.");
return 0;
}
/*
* Cache it locally.
*/
if (proxy_add_fds(proxy_listener) < 0) {
proxy_all_used = TRUE;
listen_free(&proxy_listener);
radlog(L_ERR, "Failed creating new proxy socket: server is too busy and home servers appear to be down");
return 0;
}
if (!fr_packet_list_socket_add(proxy_list, proxy_listener->fd)) {
RDEBUG2("ERROR: Failed to create a new socket for proxying requests.");
return 0;
}
if (!fr_packet_list_id_alloc(proxy_list, packet)) {
RDEBUG2("ERROR: Failed to create a new socket for proxying requests.");
return 0;
}
/*
* Signal the main thread to add the new FD to the list
* of listening FD's.
*/
radius_signal_self(RADIUS_SIGNAL_SELF_NEW_FD);
return 1;
}
static int insert_into_proxy_hash(REQUEST *request, int retransmit)
{
int i, proxy;
char buf[128];
rad_assert(request->proxy != NULL);
rad_assert(proxy_list != NULL);
PTHREAD_MUTEX_LOCK(&proxy_mutex);
/*
* Keep track of maximum outstanding requests to a
* particular home server. 'max_outstanding' is
* enforced in home_server_ldb(), in realms.c.
*/
if (request->home_server) {
request->home_server->currently_outstanding++;
}
if (retransmit) {
RADIUS_PACKET packet;
packet = *request->proxy;
if (!proxy_id_alloc(request, &packet)) {
PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
return 0;
}
/*
* Yank the request, free the old Id, and
* remember the new Id.
*/
fr_packet_list_yank(proxy_list, request->proxy);
fr_packet_list_id_free(proxy_list, request->proxy);
*request->proxy = packet;
} else if (!proxy_id_alloc(request, request->proxy)) {
PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
return 0;
}
rad_assert(request->proxy->sockfd >= 0);
/*
* FIXME: Hack until we get rid of rad_listen_t, and put
* the information into the packet_list.
*/
proxy = -1;
for (i = 0; i < 32; i++) {
if (proxy_fds[i] == request->proxy->sockfd) {
proxy = i;
break;
}
}
if (proxy < 0) {
PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
RDEBUG2("ERROR: All sockets are full.");
return 0;
}
rad_assert(proxy_fds[proxy] != -1);
rad_assert(proxy_listeners[proxy] != NULL);
request->proxy_listener = proxy_listeners[proxy];
if (!fr_packet_list_insert(proxy_list, &request->proxy)) {
fr_packet_list_id_free(proxy_list, request->proxy);
PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
RDEBUG2("ERROR: Failed to insert entry into proxy list.");
return 0;
}
request->in_proxy_hash = TRUE;
PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
RDEBUG3(" proxy: allocating destination %s port %d - Id %d",
inet_ntop(request->proxy->dst_ipaddr.af,
&request->proxy->dst_ipaddr.ipaddr, buf, sizeof(buf)),
request->proxy->dst_port,
request->proxy->id);
return 1;
}
/*
* Called as BOTH an event, and in-line from other functions.
*/
static void wait_for_proxy_id_to_expire(void *ctx)
{
REQUEST *request = ctx;
rad_assert(request->magic == REQUEST_MAGIC);
rad_assert(request->proxy != NULL);
if (!fr_event_now(el, &now)) gettimeofday(&now, NULL);
request->when = request->proxy_when;
#ifdef WITH_COA
if (((request->proxy->code == PW_COA_REQUEST) ||
(request->proxy->code == PW_DISCONNECT_REQUEST)) &&
(request->packet->code != request->proxy->code)) {
request->when.tv_sec += request->home_server->coa_mrd;
} else
#endif
request->when.tv_sec += request->home_server->response_window;
if ((request->num_proxied_requests == request->num_proxied_responses) ||
timercmp(&now, &request->when, >)) {
if (request->packet) {
RDEBUG2("Cleaning up request %u ID %d with timestamp +%d",
request->number, request->packet->id,
(unsigned int) (request->timestamp - fr_start_time));
} else {
RDEBUG2("Cleaning up request %u with timestamp +%d",
request->number,
(unsigned int) (request->timestamp - fr_start_time));
}
ev_request_free(&request);
return;
}
INSERT_EVENT(wait_for_proxy_id_to_expire, request);
}
#endif
#ifdef HAVE_PTHREAD_H
static void wait_for_child_to_die(void *ctx)
{
REQUEST *request = ctx;
rad_assert(request->magic == REQUEST_MAGIC);
remove_from_request_hash(request);
/*
* If it's still queued (waiting for a thread to pick it
* up) OR, it's running AND there's still a child thread
* handling it, THEN delay some more.
*/
if ((request->child_state == REQUEST_QUEUED) ||
((request->child_state == REQUEST_RUNNING) &&
(pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0))) {
/*
* Cap delay at max_request_time
*/
if (request->delay < (USEC * request->root->max_request_time)) {
request->delay += (request->delay >> 1);
radlog(L_INFO, "WARNING: Child is hung for request %u in component %s module %s.",
request->number, request->component, request->module);
} else {
request->delay = USEC * request->root->max_request_time;
RDEBUG2("WARNING: Child is hung after \"max_request_time\" for request %u",
request->number);
}
tv_add(&request->when, request->delay);
INSERT_EVENT(wait_for_child_to_die, request);
return;
}
RDEBUG2("Child is finally responsive for request %u", request->number);
#ifdef WITH_PROXY
if (request->proxy) {
wait_for_proxy_id_to_expire(request);
return;
}
#endif
ev_request_free(&request);
}
#endif
static void cleanup_delay(void *ctx)
{
REQUEST *request = ctx;
rad_assert(request->magic == REQUEST_MAGIC);
rad_assert((request->child_state == REQUEST_CLEANUP_DELAY) ||
(request->child_state == REQUEST_DONE));
remove_from_request_hash(request);
#ifdef WITH_PROXY
if (request->proxy && request->in_proxy_hash) {
wait_for_proxy_id_to_expire(request);
return;
}
#endif
RDEBUG2("Cleaning up request %u ID %d with timestamp +%d",
request->number, request->packet->id,
(unsigned int) (request->timestamp - fr_start_time));
ev_request_free(&request);
}
/*
* In daemon mode, AND this request has debug flags set.
*/
#define DEBUG_PACKET if (!debug_flag && request->options && request->radlog) debug_packet
static void debug_packet(REQUEST *request, RADIUS_PACKET *packet, int direction)
{
VALUE_PAIR *vp;
char buffer[1024];
const char *received, *from;
const fr_ipaddr_t *ip;
int port;
if (!packet) return;
rad_assert(request->radlog != NULL);
if (direction == 0) {
received = "Received";
from = "from"; /* what else? */
ip = &packet->src_ipaddr;
port = packet->src_port;
} else {
received = "Sending";
from = "to"; /* hah! */
ip = &packet->dst_ipaddr;
port = packet->dst_port;
}
/*
* Client-specific debugging re-prints the input
* packet into the client log.
*
* This really belongs in a utility library
*/
if ((packet->code > 0) && (packet->code < FR_MAX_PACKET_CODE)) {
RDEBUG("%s %s packet %s host %s port %d, id=%d, length=%d",
received, fr_packet_codes[packet->code], from,
inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
port, packet->id, packet->data_len);
} else {
RDEBUG("%s packet %s host %s port %d code=%d, id=%d, length=%d",
received, from,
inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
port,
packet->code, packet->id, packet->data_len);
}
for (vp = packet->vps; vp != NULL; vp = vp->next) {
vp_prints(buffer, sizeof(buffer), vp);
request->radlog(L_DBG, 0, request, "\t%s", buffer);
}
}
static void reject_delay(void *ctx)
{
REQUEST *request = ctx;
rad_assert(request->magic == REQUEST_MAGIC);
rad_assert(request->child_state == REQUEST_REJECT_DELAY);
RDEBUG2("Sending delayed reject for request %u", request->number);
DEBUG_PACKET(request, request->reply, 1);
request->listener->send(request->listener, request);
request->when.tv_sec += request->root->cleanup_delay;
request->child_state = REQUEST_CLEANUP_DELAY;
INSERT_EVENT(cleanup_delay, request);
}
#ifdef WITH_PROXY
void revive_home_server(void *ctx)
{
home_server *home = ctx;
char buffer[128];
home->state = HOME_STATE_ALIVE;
home->currently_outstanding = 0;
home->revive_time = now;
/*
* Delete any outstanding events.
*/
if (home->ev) fr_event_delete(el, &home->ev);
radlog(L_PROXY, "Marking home server %s port %d alive again... we have no idea if it really is alive or not.",
inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
buffer, sizeof(buffer)),
home->port);
}
static void no_response_to_ping(void *ctx)
{
REQUEST *request = ctx;
home_server *home;
char buffer[128];
rad_assert(request->home_server != NULL);
home = request->home_server;
home->num_received_pings = 0;
radlog(L_ERR, "No response to status check %d for home server %s port %d",
request->number,
inet_ntop(request->proxy->dst_ipaddr.af,
&request->proxy->dst_ipaddr.ipaddr,
buffer, sizeof(buffer)),
request->proxy->dst_port);
check_for_zombie_home_server(request);
wait_for_proxy_id_to_expire(request);
}
static void received_response_to_ping(REQUEST *request)
{
home_server *home;
char buffer[128];
rad_assert(request->home_server != NULL);
home = request->home_server;
home->num_received_pings++;
radlog(L_PROXY, "Received response to status check %d (%d in current sequence)",
request->number, home->num_received_pings);
/*
* Remove the request from any hashes
*/
fr_event_delete(el, &request->ev);
remove_from_proxy_hash(request);
rad_assert(request->in_request_hash == FALSE);
/*
* The control socket may have marked the home server as
* alive. OR, it may have suddenly started responding to
* requests again. If so, don't re-do the "make alive"
* work.
*/
if (home->state == HOME_STATE_ALIVE) return;
/*
* We haven't received enough ping responses to mark it
* "alive". Wait a bit.
*/
if (home->num_received_pings < home->num_pings_to_alive) {
return;
}
home->state = HOME_STATE_ALIVE;
home->currently_outstanding = 0;
home->revive_time = now;
if (!fr_event_delete(el, &home->ev)) {
RDEBUG2("Hmm... no event for home server. Oh well.");
}
radlog(L_PROXY, "Marking home server %s port %d alive",
inet_ntop(request->proxy->dst_ipaddr.af,
&request->proxy->dst_ipaddr.ipaddr,
buffer, sizeof(buffer)),
request->proxy->dst_port);
}
/*
* Called from start of zombie period, OR after control socket
* marks the home server dead.
*/
static void ping_home_server(void *ctx)
{
uint32_t jitter;
home_server *home = ctx;
REQUEST *request;
VALUE_PAIR *vp;
if ((home->state == HOME_STATE_ALIVE) ||
(home->ping_check == HOME_PING_CHECK_NONE) ||
(home->ev != NULL)) {
return;
}
request = request_alloc();
request->number = request_num_counter++;
request->proxy = rad_alloc(1);
rad_assert(request->proxy != NULL);
fr_event_now(el, &request->when);
home->when = request->when;
if (home->ping_check == HOME_PING_CHECK_STATUS_SERVER) {
request->proxy->code = PW_STATUS_SERVER;
radius_pairmake(request, &request->proxy->vps,
"Message-Authenticator", "0x00", T_OP_SET);
} else if (home->type == HOME_TYPE_AUTH) {
request->proxy->code = PW_AUTHENTICATION_REQUEST;
radius_pairmake(request, &request->proxy->vps,
"User-Name", home->ping_user_name, T_OP_SET);
radius_pairmake(request, &request->proxy->vps,
"User-Password", home->ping_user_password, T_OP_SET);
radius_pairmake(request, &request->proxy->vps,
"Service-Type", "Authenticate-Only", T_OP_SET);
radius_pairmake(request, &request->proxy->vps,
"Message-Authenticator", "0x00", T_OP_SET);
} else {
#ifdef WITH_ACCOUNTING
request->proxy->code = PW_ACCOUNTING_REQUEST;
radius_pairmake(request, &request->proxy->vps,
"User-Name", home->ping_user_name, T_OP_SET);
radius_pairmake(request, &request->proxy->vps,
"Acct-Status-Type", "Stop", T_OP_SET);
radius_pairmake(request, &request->proxy->vps,
"Acct-Session-Id", "00000000", T_OP_SET);
vp = radius_pairmake(request, &request->proxy->vps,
"Event-Timestamp", "0", T_OP_SET);
vp->vp_date = now.tv_sec;
#else
rad_assert("Internal sanity check failed");
#endif
}
radius_pairmake(request, &request->proxy->vps,
"NAS-Identifier", "Status Check. Are you alive?",
T_OP_SET);
request->proxy->src_ipaddr = home->src_ipaddr;
request->proxy->dst_ipaddr = home->ipaddr;
request->proxy->dst_port = home->port;
request->home_server = home;
rad_assert(request->proxy_listener == NULL);
if (!insert_into_proxy_hash(request, FALSE)) {
RDEBUG2("ERROR: Failed to insert status check %d into proxy list. Discarding it.",
request->number);
ev_request_free(&request);
return;
}
rad_assert(request->proxy_listener != NULL);
request->proxy_listener->send(request->proxy_listener,
request);
request->next_callback = NULL;
request->child_state = REQUEST_PROXIED;
gettimeofday(&request->when, NULL);
home->when = request->when;
request->when.tv_sec += home->ping_timeout;;
INSERT_EVENT(no_response_to_ping, request);
/*
* Add +/- 2s of jitter, as suggested in RFC 3539
* and in the Issues and Fixes draft.
*/
home->when.tv_sec += home->ping_interval - 2;
jitter = fr_rand();
jitter ^= (jitter >> 10);
jitter &= ((1 << 23) - 1); /* 22 bits of 1 */
tv_add(&home->when, jitter);
INSERT_EVENT(ping_home_server, home);
}
void mark_home_server_dead(home_server *home, struct timeval *when)
{
int previous_state = home->state;
char buffer[128];
radlog(L_PROXY, "Marking home server %s port %d as dead.",
inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
buffer, sizeof(buffer)),
home->port);
home->state = HOME_STATE_IS_DEAD;
home->num_received_pings = 0;
if (home->ping_check != HOME_PING_CHECK_NONE) {
/*
* If the control socket marks us dead, start
* pinging. Otherwise, we already started
* pinging when it was marked "zombie".
*/
if (previous_state == HOME_STATE_ALIVE) {
ping_home_server(home);
}
} else {
/*
* Revive it after a fixed period of time. This
* is very, very, bad.
*/
home->when = *when;
home->when.tv_sec += home->revive_interval;
INSERT_EVENT(revive_home_server, home);
}
}
static void check_for_zombie_home_server(REQUEST *request)
{
home_server *home;
struct timeval when;
home = request->home_server;
if (home->state != HOME_STATE_ZOMBIE) return;
when = home->zombie_period_start;
when.tv_sec += home->zombie_period;
fr_event_now(el, &now);
if (timercmp(&now, &when, <)) {
return;
}
mark_home_server_dead(home, &request->when);
}
static int proxy_to_virtual_server(REQUEST *request);
static int virtual_server_handler(UNUSED REQUEST *request)
{
proxy_to_virtual_server(request);
return 0;
}
static void proxy_fallback_handler(REQUEST *request)
{
/*
* A proper time is required for wait_a_bit.
*/
request->delay = USEC / 10;
gettimeofday(&now, NULL);
request->next_when = now;
tv_add(&request->next_when, request->delay);
request->next_callback = wait_a_bit;
/*
* Re-queue the request.
*/
request->child_state = REQUEST_QUEUED;
rad_assert(request->proxy != NULL);
if (!thread_pool_addrequest(request, virtual_server_handler)) {
request->child_state = REQUEST_DONE;
}
#ifdef HAVE_PTHREAD_H
/*
* MAY free the request if we're over max_request_time,
* AND we're not in threaded mode!
*
* Note that we call this ONLY if we're threaded, as
* if we're NOT threaded, request_post_handler() calls
* wait_a_bit(), which means that "request" may not
* exist any more...
*/
if (have_children) wait_a_bit(request);
#endif
}
static int setup_post_proxy_fail(REQUEST *request)
{
DICT_VALUE *dval = NULL;
VALUE_PAIR *vp;
request->child_state = REQUEST_RUNNING;
if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
dval = dict_valbyname(PW_POST_PROXY_TYPE, "Fail-Authentication");
} else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
dval = dict_valbyname(PW_POST_PROXY_TYPE, "Fail-Accounting");
#ifdef WITH_COA
/*
* See no_response_to_coa_request
*/
} else if (((request->packet->code >> 8) & 0xff) == PW_COA_REQUEST) {
request->packet->code &= 0xff; /* restore it */
if (request->proxy->code == PW_COA_REQUEST) {
dval = dict_valbyname(PW_POST_PROXY_TYPE, "Fail-CoA");
} else if (request->proxy->code == PW_DISCONNECT_REQUEST) {
dval = dict_valbyname(PW_POST_PROXY_TYPE, "Fail-Disconnect");
} else {
return 0;
}
#endif
} else {
return 0;
}
if (!dval) dval = dict_valbyname(PW_POST_PROXY_TYPE, "Fail");
if (!dval) {
pairdelete(&request->config_items, PW_POST_PROXY_TYPE);
return 0;
}
vp = pairfind(request->config_items, PW_POST_PROXY_TYPE);
if (!vp) vp = radius_paircreate(request, &request->config_items,
PW_POST_PROXY_TYPE, PW_TYPE_INTEGER);
vp->vp_integer = dval->value;
rad_assert(request->proxy_reply == NULL);
return 1;
}
static int null_handler(UNUSED REQUEST *request)
{
return 0;
}
static void post_proxy_fail_handler(REQUEST *request)
{
/*
* A proper time is required for wait_a_bit.
*/
request->delay = USEC / 10;
gettimeofday(&now, NULL);