/
process.c
5590 lines (4712 loc) · 138 KB
/
process.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
/*
* 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
*/
/**
* $Id$
*
* @file process.c
* @brief Defines the state machines that control how requests are processed.
*
* @copyright 2012 The FreeRADIUS server project
* @copyright 2012 Alan DeKok <aland@deployingradius.com>
*/
RCSID("$Id$")
#include <freeradius-devel/radiusd.h>
#include <freeradius-devel/process.h>
#include <freeradius-devel/modules.h>
#include <freeradius-devel/state.h>
#include <freeradius-devel/rad_assert.h>
#ifdef WITH_DETAIL
#include <freeradius-devel/detail.h>
#endif
#include <signal.h>
#include <fcntl.h>
#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif
extern pid_t radius_pid;
extern fr_cond_t *debug_condition;
static bool spawn_flag = false;
static bool just_started = true;
time_t fr_start_time = (time_t)-1;
static rbtree_t *pl = NULL;
static fr_event_list_t *el = NULL;
fr_event_list_t *radius_event_list_corral(UNUSED event_corral_t hint) {
/* Currently we do not run a second event loop for modules. */
return el;
}
static char const *action_codes[] = {
"INVALID",
"run",
"done",
"dup",
"timer",
#ifdef WITH_PROXY
"proxy-reply"
#endif
};
#ifdef DEBUG_STATE_MACHINE
# define TRACE_STATE_MACHINE \
if (rad_debug_lvl) do { \
struct timeval debug_tv; \
gettimeofday(&debug_tv, NULL); \
debug_tv.tv_sec -= fr_start_time; \
printf("(%u) %d.%06d ********\tSTATE %s action %s live M-%s C-%s\t********\n",\
request->number, (int) debug_tv.tv_sec, (int) debug_tv.tv_usec, \
__FUNCTION__, action_codes[action], master_state_names[request->master_state], \
child_state_names[request->child_state]); \
} while (0)
static char const *master_state_names[REQUEST_MASTER_NUM_STATES] = {
"?",
"active",
"stop-processing",
"counted"
};
static char const *child_state_names[REQUEST_CHILD_NUM_STATES] = {
"?",
"queued",
"running",
"proxied",
"reject-delay",
"cleanup-delay",
"done"
};
#else
# define TRACE_STATE_MACHINE {}
#endif
static NEVER_RETURNS void _rad_panic(char const *file, unsigned int line, char const *msg)
{
ERROR("%s[%u]: %s", file, line, msg);
fr_exit_now(1);
}
#define rad_panic(x) _rad_panic(__FILE__, __LINE__, x)
/** Declare a state in the state machine
*
* Expands to the start of a function definition for a given state.
*
* @param _x the name of the state.
*/
#define STATE_MACHINE_DECL(_x) static void _x(REQUEST *request, int action)
static void request_timer(void *ctx);
/** Insert #REQUEST back into the event heap, to continue executing at a future time
*
* @param file the state machine timer call occurred in.
* @param line the state machine timer call occurred on.
* @param request to set add the timer event for.
* @param when the event should fine.
* @param action to perform when we resume processing the request.
*/
static inline void state_machine_timer(char const *file, int line, REQUEST *request,
struct timeval *when, fr_state_action_t action)
{
request->timer_action = action;
if (!fr_event_insert(el, request_timer, request, when, &request->ev)) {
_rad_panic(file, line, "Failed to insert event");
}
}
/** @copybrief state_machine_timer
*
* @param _x the action to perform when we resume processing the request.
*/
#define STATE_MACHINE_TIMER(_x) state_machine_timer(__FILE__, __LINE__, request, &when, _x)
/*
* We need a different VERIFY_REQUEST macro in process.c
* To avoid the race conditions with the master thread
* checking the REQUEST whilst it's being worked on by
* the child.
*/
#if defined(WITH_VERIFY_PTR) && defined(HAVE_PTHREAD_H)
# undef VERIFY_REQUEST
# define VERIFY_REQUEST(_x) if (pthread_equal(pthread_self(), _x->child_pid) != 0) verify_request(__FILE__, __LINE__, _x)
#endif
/**
* @section request_timeline
*
* Time sequence of a request
* @code
*
* RQ-----------------P=============================Y-J-C
* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::M
* @endcode
*
* - R: received. Duplicate detection is done, and request is
* cached.
*
* - Q: Request is placed onto a queue for child threads to pick up.
* If there are no child threads, the request goes immediately
* to P.
*
* - P: Processing the request through the modules.
*
* - Y: Reply is ready. Rejects MAY be delayed here. All other
* replies are sent immediately.
*
* - J: Reject is sent "response_delay" after the reply is ready.
*
* - C: For Access-Requests, After "cleanup_delay", the request is
* deleted. Accounting-Request packets go directly from Y to C.
*
* - M: Max request time. If the request hits this timer, it is
* forcibly stopped.
*
* Other considerations include duplicate and conflicting
* packets. When a dupicate packet is received, it is ignored
* until we've reached Y, as no response is ready. If the reply
* is a reject, duplicates are ignored until J, when we're ready
* to send the reply. In between the reply being sent (Y or J),
* and C, the server responds to duplicates by sending the cached
* reply.
*
* Conflicting packets are sent in 2 situations.
*
* The first is in between R and Y. In that case, we consider
* it as a hint that we're taking too long, and the NAS has given
* up on the request. We then behave just as if the M timer was
* reached, and we discard the current request. This allows us
* to process the new one.
*
* The second case is when we're at Y, but we haven't yet
* finished processing the request. This is a race condition in
* the threading code (avoiding locks is faster). It means that
* a thread has actually encoded and sent the reply, and that the
* NAS has responded with a new packet. The server can then
* safely mark the current request as "OK to delete", and behaves
* just as if the M timer was reached. This usually happens only
* in high-load situations.
*
* Duplicate packets are sent when the NAS thinks we're taking
* too long, and wants a reply. From R-Y, duplicates are
* ignored. From Y-J (for Access-Rejects), duplicates are also
* ignored. From Y-C, duplicates get a duplicate reply. *And*,
* they cause the "cleanup_delay" time to be extended. This
* extension means that we're more likely to send a duplicate
* reply (if we have one), or to suppress processing the packet
* twice if we didn't reply to it.
*
* All functions in this file should be thread-safe, and should
* assume thet the REQUEST structure is being accessed
* simultaneously by the main thread, and by the child worker
* threads. This means that timers, etc. cannot be updated in
* the child thread.
*
* Instead, the master thread periodically calls request->process
* with action TIMER. It's up to the individual functions to
* determine how to handle that. They need to check if they're
* being called from a child thread or the master, and then do
* different things based on that.
*/
#ifdef WITH_PROXY
static fr_packet_list_t *proxy_list = NULL;
static TALLOC_CTX *proxy_ctx = NULL;
#endif
#ifdef HAVE_PTHREAD_H
# ifdef WITH_PROXY
static pthread_mutex_t proxy_mutex;
static bool proxy_no_new_sockets = false;
# endif
# define PTHREAD_MUTEX_LOCK if (spawn_flag) pthread_mutex_lock
# define PTHREAD_MUTEX_UNLOCK if (spawn_flag) pthread_mutex_unlock
static pthread_t NO_SUCH_CHILD_PID;
# define NO_CHILD_THREAD request->child_pid = NO_SUCH_CHILD_PID
#else
/*
* This is easier than ifdef's throughout the code.
*/
# define PTHREAD_MUTEX_LOCK(_x)
# define PTHREAD_MUTEX_UNLOCK(_x)
# define NO_CHILD_THREAD
#endif
#ifdef HAVE_PTHREAD_H
static bool we_are_master(void)
{
if (spawn_flag &&
(pthread_equal(pthread_self(), NO_SUCH_CHILD_PID) == 0)) {
return false;
}
return true;
}
/*
* Assertions are debug checks.
*/
# ifndef NDEBUG
# define ASSERT_MASTER if (!we_are_master()) rad_panic("We are not master")
# endif
#else
/*
* No threads: we're always master.
*/
# define we_are_master(_x) (1)
#endif /* HAVE_PTHREAD_H */
#ifndef ASSERT_MASTER
# define ASSERT_MASTER
#endif
/*
* Make state transitions simpler.
*/
#define FINAL_STATE(_x) NO_CHILD_THREAD; request->component = "<" #_x ">"; request->module = ""; request->child_state = _x
static int event_new_fd(rad_listen_t *this);
/*
* We need mutexes around the event FD list *only* in certain
* cases.
*/
#if defined (HAVE_PTHREAD_H) && (defined(WITH_PROXY) || defined(WITH_TCP))
static rad_listen_t *new_listeners = NULL;
static pthread_mutex_t fd_mutex;
# define FD_MUTEX_LOCK if (spawn_flag) pthread_mutex_lock
# define FD_MUTEX_UNLOCK if (spawn_flag) pthread_mutex_unlock
void radius_update_listener(rad_listen_t *this)
{
/*
* Just do it ourselves.
*/
if (we_are_master()) {
event_new_fd(this);
return;
}
FD_MUTEX_LOCK(&fd_mutex);
/*
* If it's already in the list, don't add it again.
*/
if (this->next) {
FD_MUTEX_UNLOCK(&fd_mutex);
return;
}
/*
* Otherwise, add it to the list
*/
this->next = new_listeners;
new_listeners = this;
FD_MUTEX_UNLOCK(&fd_mutex);
radius_signal_self(RADIUS_SIGNAL_SELF_NEW_FD);
}
#else
void radius_update_listener(rad_listen_t *this)
{
/*
* No threads. Just insert it.
*/
event_new_fd(this);
}
/*
* This is easier than ifdef's throughout the code.
*/
# define FD_MUTEX_LOCK(_x)
# define FD_MUTEX_UNLOCK(_x)
#endif
static int request_num_counter = 1;
#ifdef WITH_PROXY
static int request_will_proxy(REQUEST *request) CC_HINT(nonnull);
static int request_proxy(REQUEST *request, int retransmit) CC_HINT(nonnull);
STATE_MACHINE_DECL(request_ping) CC_HINT(nonnull);
STATE_MACHINE_DECL(request_response_delay) CC_HINT(nonnull);
STATE_MACHINE_DECL(request_cleanup_delay) CC_HINT(nonnull);
STATE_MACHINE_DECL(request_running) CC_HINT(nonnull);
STATE_MACHINE_DECL(request_done) CC_HINT(nonnull);
STATE_MACHINE_DECL(proxy_no_reply) CC_HINT(nonnull);
STATE_MACHINE_DECL(proxy_running) CC_HINT(nonnull);
STATE_MACHINE_DECL(proxy_wait_for_reply) CC_HINT(nonnull);
static int process_proxy_reply(REQUEST *request, RADIUS_PACKET *reply) CC_HINT(nonnull (1));
static void remove_from_proxy_hash(REQUEST *request) CC_HINT(nonnull);
static void remove_from_proxy_hash_nl(REQUEST *request, bool yank) CC_HINT(nonnull);
static int insert_into_proxy_hash(REQUEST *request) CC_HINT(nonnull);
#endif
static REQUEST *request_setup(TALLOC_CTX *ctx, rad_listen_t *listener, RADIUS_PACKET *packet,
RADCLIENT *client, RAD_REQUEST_FUNP fun);
static int request_pre_handler(REQUEST *request, UNUSED int action) CC_HINT(nonnull);
#ifdef WITH_COA
static void request_coa_originate(REQUEST *request) CC_HINT(nonnull);
STATE_MACHINE_DECL(coa_wait_for_reply) CC_HINT(nonnull);
STATE_MACHINE_DECL(coa_no_reply) CC_HINT(nonnull);
STATE_MACHINE_DECL(coa_running) CC_HINT(nonnull);
static void coa_separate(REQUEST *request) CC_HINT(nonnull);
# define COA_SEPARATE if (request->coa) coa_separate(request->coa);
#else
# define COA_SEPARATE
#endif
#define CHECK_FOR_STOP do { if (request->master_state == REQUEST_STOP_PROCESSING) {request_done(request, FR_ACTION_DONE);return;}} while (0)
#undef USEC
#define USEC (1000000)
#define INSERT_EVENT(_function, _ctx) if (!fr_event_insert(el, _function, _ctx, &((_ctx)->when), &((_ctx)->ev))) { _rad_panic(__FILE__, __LINE__, "Failed to insert event"); }
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;
}
}
/***********************************************************************
*
* Start of RADIUS server state machine.
*
***********************************************************************/
static struct timeval *request_response_window(REQUEST *request)
{
VERIFY_REQUEST(request);
if (request->client) {
/*
* The client hasn't set the response window. Return
* either the home server one, if set, or the global one.
*/
if (!timerisset(&request->client->response_window)) {
return &request->home_server->response_window;
}
if (timercmp(&request->client->response_window,
&request->home_server->response_window, <)) {
return &request->client->response_window;
}
}
rad_assert(request->home_server != NULL);
return &request->home_server->response_window;
}
/*
* Determine initial request processing delay.
*/
static int request_init_delay(REQUEST *request)
{
struct timeval half_response_window;
VERIFY_REQUEST(request);
/* Allow client response window to lower initial delay */
if (timerisset(&request->client->response_window)) {
half_response_window.tv_sec = request->client->response_window.tv_sec >> 1;
half_response_window.tv_usec =
((request->client->response_window.tv_sec & 1) * USEC +
request->client->response_window.tv_usec) >> 1;
if (timercmp(&half_response_window, &request->root->init_delay, <))
return (int)half_response_window.tv_sec * USEC +
(int)half_response_window.tv_usec;
}
return (int)request->root->init_delay.tv_sec * USEC +
(int)request->root->init_delay.tv_usec;
}
/*
* Callback for ALL timer events related to the request.
*/
static void request_timer(void *ctx)
{
REQUEST *request = talloc_get_type_abort(ctx, REQUEST);
int action;
action = request->timer_action;
TRACE_STATE_MACHINE;
request->process(request, action);
}
/*
* Wrapper for talloc pools. If there's no parent, just free the
* request. If there is a parent, free the parent INSTEAD of the
* request.
*/
static void request_free(REQUEST *request)
{
void *ptr;
rad_assert(request->ev == NULL);
rad_assert(!request->in_request_hash);
rad_assert(!request->in_proxy_hash);
if ((request->options & RAD_REQUEST_OPTION_CTX) == 0) {
talloc_free(request);
return;
}
ptr = talloc_parent(request);
rad_assert(ptr != NULL);
talloc_free(ptr);
}
#ifdef WITH_PROXY
static void proxy_reply_too_late(REQUEST *request)
{
char buffer[INET6_ADDRSTRLEN];
RDEBUG2("Reply from home server %s port %d - ID: %d arrived too late. "
"Try increasing 'retry_delay' or 'max_request_time'",
inet_ntop(request->proxy->dst_ipaddr.af,
&request->proxy->dst_ipaddr.ipaddr,
buffer, sizeof(buffer)),
request->proxy->dst_port, request->proxy->id);
}
#endif
/** Mark a request DONE and clean it up.
*
* When a request is DONE, it can have ties to a number of other
* portions of the server. The request hash, proxy hash, events,
* child threads, etc. This function takes care of either cleaning
* up the request, or managing the timers to wait for the ties to be
* removed.
*
* \dot
* digraph done {
* done -> done [ label = "still running" ];
* }
* \enddot
*/
static void request_done(REQUEST *request, int action)
{
struct timeval now, when;
VERIFY_REQUEST(request);
TRACE_STATE_MACHINE;
/*
* Force this no matter what.
*/
request->process = request_done;
#ifdef WITH_DETAIL
/*
* Tell the detail listener that we're done.
*/
if (request->listener &&
(request->listener->type == RAD_LISTEN_DETAIL) &&
(request->simul_max != 1)) {
request->simul_max = 1;
request->listener->send(request->listener,
request);
}
#endif
#ifdef HAVE_PTHREAD_H
/*
* If called from a child thread, mark ourselves as done,
* and wait for the master thread timer to clean us up.
*/
if (!we_are_master()) {
FINAL_STATE(REQUEST_DONE);
return;
}
#endif
/*
* Mark the request as STOP.
*/
request->master_state = REQUEST_STOP_PROCESSING;
#ifdef WITH_COA
/*
* Move the CoA request to its own handler.
*/
if (request->coa) {
coa_separate(request->coa);
} else if (request->parent && (request->parent->coa == request)) {
coa_separate(request);
}
#endif
/*
* It doesn't hurt to send duplicate replies. All other
* signals are ignored, as the request will be cleaned up
* soon anyways.
*/
switch (action) {
case FR_ACTION_DUP:
#ifdef WITH_DETAIL
rad_assert(request->listener != NULL);
#endif
if (request->reply->code != 0) {
request->listener->send(request->listener, request);
return;
} else {
RDEBUG("No reply. Ignoring retransmit");
}
break;
/*
* Mark the request as done.
*/
case FR_ACTION_DONE:
#ifdef HAVE_PTHREAD_H
/*
* If the child is still running, leave it alone.
*/
if (spawn_flag && (request->child_state <= REQUEST_RUNNING)) {
break;
}
#endif
#ifdef DEBUG_STATE_MACHINE
if (rad_debug_lvl) printf("(%u) ********\tSTATE %s C-%s -> C-%s\t********\n",
request->number, __FUNCTION__,
child_state_names[request->child_state],
child_state_names[REQUEST_DONE]);
#endif
request->child_state = REQUEST_DONE;
break;
/*
* Called when the child is taking too long to
* finish. We've already marked it "please
* stop", so we don't complain any more.
*/
case FR_ACTION_TIMER:
break;
#ifdef WITH_PROXY
case FR_ACTION_PROXY_REPLY:
proxy_reply_too_late(request);
break;
#endif
default:
break;
}
/*
* Remove it from the request hash.
*/
if (request->in_request_hash) {
if (!rbtree_deletebydata(pl, &request->packet)) {
rad_assert(0 == 1);
}
request->in_request_hash = false;
}
#ifdef WITH_PROXY
/*
* Wait for the proxy ID to expire. This allows us to
* avoid re-use of proxy IDs for a while.
*/
if (request->in_proxy_hash) {
rad_assert(request->proxy != NULL);
fr_event_now(el, &now);
when = request->proxy->timestamp;
#ifdef WITH_COA
if (((request->proxy->code == PW_CODE_COA_REQUEST) ||
(request->proxy->code == PW_CODE_DISCONNECT_REQUEST)) &&
(request->packet->code != request->proxy->code)) {
when.tv_sec += request->home_server->coa_mrd;
} else
#endif
timeradd(&when, request_response_window(request), &when);
/*
* We haven't received all responses, AND there's still
* time to wait. Do so.
*/
if ((request->num_proxied_requests > request->num_proxied_responses) &&
#ifdef WITH_TCP
(request->home_server->proto != IPPROTO_TCP) &&
#endif
timercmp(&now, &when, <)) {
RDEBUG("Waiting for more responses from the home server");
goto wait_some_more;
}
/*
* Time to remove it.
*/
remove_from_proxy_hash(request);
}
#endif
#ifdef HAVE_PTHREAD_H
/*
* If there's no children, we can mark the request as done.
*/
if (!spawn_flag) request->child_state = REQUEST_DONE;
#endif
/*
* If the child is still running, wait for it to be finished.
*/
if (request->child_state <= REQUEST_RUNNING) {
gettimeofday(&now, NULL);
#ifdef WITH_PROXY
wait_some_more:
#endif
when = now;
if (request->delay < (USEC / 3)) request->delay = USEC / 3;
tv_add(&when, request->delay);
request->delay += request->delay >> 1;
if (request->delay > (10 * USEC)) request->delay = 10 * USEC;
STATE_MACHINE_TIMER(FR_ACTION_TIMER);
return;
}
#ifdef HAVE_PTHREAD_H
rad_assert(request->child_pid == NO_SUCH_CHILD_PID);
#endif
/*
* @todo: do final states for TCP sockets, too?
*/
request_stats_final(request);
#ifdef WITH_TCP
if (request->listener) {
request->listener->count--;
/*
* If we're the last one, remove the listener now.
*/
if ((request->listener->count == 0) &&
(request->listener->status >= RAD_LISTEN_STATUS_FROZEN)) {
event_new_fd(request->listener);
}
}
#endif
if (request->packet) {
RDEBUG2("Cleaning up request packet ID %u with timestamp +%d",
request->packet->id,
(unsigned int) (request->timestamp - fr_start_time));
} /* else don't print anything */
ASSERT_MASTER;
fr_event_delete(el, &request->ev);
request_free(request);
}
static void request_cleanup_delay_init(REQUEST *request)
{
struct timeval now, when;
VERIFY_REQUEST(request);
/*
* Do cleanup delay ONLY for RADIUS packets from a real
* client. Everything else just gets cleaned up
* immediately.
*/
if (request->packet->dst_port == 0) goto done;
/*
* Accounting packets shouldn't be retransmitted. They
* should always be updated with Acct-Delay-Time.
*/
#ifdef WITH_ACCOUNTING
if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) goto done;
#endif
#ifdef WITH_DHCP
if (request->listener->type == RAD_LISTEN_DHCP) goto done;
#endif
#ifdef WITH_VMPS
if (request->listener->type == RAD_LISTEN_VQP) goto done;
#endif
if (!request->root->cleanup_delay) goto done;
gettimeofday(&now, NULL);
rad_assert(request->reply->timestamp.tv_sec != 0);
when = request->reply->timestamp;
request->delay = request->root->cleanup_delay;
when.tv_sec += request->delay;
/*
* Set timer for when we need to clean it up.
*/
if (timercmp(&when, &now, >)) {
#ifdef DEBUG_STATE_MACHINE
if (rad_debug_lvl) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_cleanup_delay");
#endif
request->process = request_cleanup_delay;
if (!we_are_master()) {
FINAL_STATE(REQUEST_CLEANUP_DELAY);
return;
}
/*
* Update this if we can, otherwise let the timers pick it up.
*/
request->child_state = REQUEST_CLEANUP_DELAY;
#ifdef HAVE_PTHREAD_H
rad_assert(request->child_pid == NO_SUCH_CHILD_PID);
#endif
STATE_MACHINE_TIMER(FR_ACTION_TIMER);
return;
}
/*
* Otherwise just clean it up.
*/
done:
request_done(request, FR_ACTION_DONE);
}
/*
* Enforce max_request_time.
*/
static bool request_max_time(REQUEST *request)
{
struct timeval now, when;
rad_assert(request->magic == REQUEST_MAGIC);
#ifdef DEBUG_STATE_MACHINE
int action = FR_ACTION_TIMER;
#endif
VERIFY_REQUEST(request);
TRACE_STATE_MACHINE;
ASSERT_MASTER;
/*
* The child thread has acknowledged it's done.
* Transition to the DONE state.
*
* If the request was marked STOP, then the "check for
* stop" macro already took care of it.
*/
if (request->child_state == REQUEST_DONE) {
done:
request_done(request, FR_ACTION_DONE);
return true;
}
/*
* The request is still running. Enforce max_request_time.
*/
fr_event_now(el, &now);
when = request->packet->timestamp;
when.tv_sec += request->root->max_request_time;
/*
* Taking too long: tell it to die.
*/
if (timercmp(&now, &when, >=)) {
#ifdef HAVE_PTHREAD_H
/*
* If there's a child thread processing it,
* complain.
*/
if (spawn_flag &&
(pthread_equal(request->child_pid, NO_SUCH_CHILD_PID) == 0)) {
ERROR("Unresponsive child for request %u, in component %s module %s",
request->number,
request->component ? request->component : "<core>",
request->module ? request->module : "<core>");
exec_trigger(request, NULL, "server.thread.unresponsive", true);
}
#endif
/*
* Tell the request that it's done.
*/
goto done;
}
/*
* Sleep for some more. We HOPE that the child will
* become responsive at some point in the future. We do
* this by adding 50% to the current timer.
*/
when = now;
tv_add(&when, request->delay);
request->delay += request->delay >> 1;
STATE_MACHINE_TIMER(FR_ACTION_TIMER);
return false;
}
static void request_queue_or_run(REQUEST *request,
fr_request_process_t process)
{
#ifdef DEBUG_STATE_MACHINE
int action = FR_ACTION_TIMER;
#endif
VERIFY_REQUEST(request);
TRACE_STATE_MACHINE;
/*
* Do this here so that fewer other functions need to do
* it.
*/
if (request->master_state == REQUEST_STOP_PROCESSING) {
#ifdef DEBUG_STATE_MACHINE
if (rad_debug_lvl) printf("(%u) ********\tSTATE %s M-%s causes C-%s-> C-%s\t********\n",
request->number, __FUNCTION__,
master_state_names[request->master_state],
child_state_names[request->child_state],
child_state_names[REQUEST_DONE]);
#endif
request_done(request, FR_ACTION_DONE);
return;
}
request->process = process;
if (we_are_master()) {
struct timeval when;
/*
* (re) set the initial delay.
*/
request->delay = request_init_delay(request);
if (request->delay > USEC) request->delay = USEC;
gettimeofday(&when, NULL);
tv_add(&when, request->delay);
request->delay += request->delay >> 1;
STATE_MACHINE_TIMER(FR_ACTION_TIMER);
#ifdef HAVE_PTHREAD_H
if (spawn_flag) {
/*
* A child thread will eventually pick it up.
*/
if (request_enqueue(request)) return;
/*
* Otherwise we're not going to do anything with
* it...
*/
request_done(request, FR_ACTION_DONE);
return;
}
#endif
}
request->child_state = REQUEST_RUNNING;
request->process(request, FR_ACTION_RUN);
#ifdef WNOHANG
/*
* Requests that care about child process exit
* codes have already either called
* rad_waitpid(), or they've given up.
*/
while (waitpid(-1, NULL, WNOHANG) > 0);
#endif
}
static void request_dup(REQUEST *request)
{
ERROR("(%u) Ignoring duplicate packet from "
"client %s port %d - ID: %u due to unfinished request "
"in component %s module %s",
request->number, request->client->shortname,
request->packet->src_port,request->packet->id,
request->component, request->module);
}
/** Sit on a request until it's time to clean it up.
*
* A NAS may not see a response from the server. When the NAS
* retransmits, we want to be able to send a cached reply back. The
* alternative is to re-process the packet, which does bad things for
* EAP, among others.
*
* IF we do see a NAS retransmit, we extend the cleanup delay,
* because the NAS might miss our cached reply.
*
* Otherwise, once we reach cleanup_delay, we transition to DONE.
*
* \dot
* digraph cleanup_delay {
* cleanup_delay;
* send_reply [ label = "send_reply\nincrease cleanup delay" ];
*
* cleanup_delay -> send_reply [ label = "DUP" ];
* send_reply -> cleanup_delay;
* cleanup_delay -> proxy_reply_too_late [ label = "PROXY_REPLY", arrowhead = "none" ];
* cleanup_delay -> cleanup_delay [ label = "TIMER < timeout" ];
* cleanup_delay -> done [ label = "TIMER >= timeout" ];
* }
* \enddot