The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
bio.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: c26f73b9cb4132dc8a1c1b896cb5f1879b8c16d5 $
19 * @file src/modules/rlm_radius/bio.c
20 * @brief RADIUS BIO transport
21 *
22 * @copyright 2017 Network RADIUS SAS
23 * @copyright 2020 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
24 */
25
26#include <freeradius-devel/io/application.h>
27#include <freeradius-devel/io/listen.h>
28#include <freeradius-devel/io/pair.h>
29#include <freeradius-devel/server/connection.h>
30#include <freeradius-devel/unlang/load_balance_priv.h>
31#include <freeradius-devel/util/heap.h>
32#include <freeradius-devel/util/rb_expire.h>
33
34
35//#include "rlm_radius.h"
36#include "track.h"
37
38typedef enum {
39 LIMIT_PORTS_NONE = 0, //!< Source port not restricted
40 LIMIT_PORTS_STATIC, //!< Limited source ports for static home servers
41 LIMIT_PORTS_DYNAMIC //!< Limited source ports for dynamic home servers
43
44typedef struct {
45 char const *module_name; //!< the module that opened the connection
46 rlm_radius_t const *inst; //!< our instance
47 fr_event_list_t *el; //!< Event list.
48 trunk_t *trunk; //!< trunk handler
49 fr_bio_fd_config_t fd_config; //!< for threads or sockets
50 fr_bio_fd_info_t const *fd_info; //!< status of the FD.
51 fr_radius_ctx_t radius_ctx; //!< for signing packets
52 bio_limit_ports_t limit_source_ports; //!< What type of port limit is in use.
53 bool dynamic; //!< is this a dynamic home server.
55
56typedef struct {
57 bio_handle_ctx_t ctx; //!< common struct for home servers and BIO handles
58
59 struct {
60 fr_bio_t *fd; //!< writing
61 uint32_t id; //!< for replication
62 fr_rb_expire_t expires; //!< for proxying / client sending
63 } bio;
64
68
70
71/** Track the handle, which is tightly correlated with the FD
72 *
73 */
74typedef struct {
75 bio_handle_ctx_t ctx; //!< common struct for home servers and BIO handles
76
77 int fd; //!< File descriptor.
78
79 struct {
80 fr_bio_t *main; //!< what we use for IO
81 fr_bio_t *fd; //!< raw FD
82 fr_bio_t *mem; //!< memory wrappers for stream sockets
83 } bio;
84
86
87 uint8_t last_id; //!< Used when replicating to ensure IDs are distributed
88 ///< evenly.
89
90 uint32_t max_packet_size; //!< Our max packet size. may be different from the parent.
91
92 uint8_t *buffer; //!< Receive buffer.
93 size_t buflen; //!< Receive buffer length.
94
95 radius_track_t *tt; //!< RADIUS ID tracking structure.
96
97 fr_time_t mrs_time; //!< Most recent sent time which had a reply.
98 fr_time_t last_reply; //!< When we last received a reply.
99 fr_time_t first_sent; //!< first time we sent a packet since going idle
100 fr_time_t last_sent; //!< last time we sent a packet.
101 fr_time_t last_idle; //!< last time we had nothing to do
102
103 fr_timer_t *zombie_ev; //!< Zombie timeout.
104 unlang_t const *instruction; //!< Instruction which triggered the start of the zombie period.
105
106 bool status_checking; //!< whether we're doing status checks
107 bio_request_t *status_u; //!< for sending status check packets
110
111
112/** Connect request_t to local tracking structure
113 *
114 */
117 rlm_rcode_t rcode; //!< from the transport
119
120 uint32_t priority; //!< copied from request->async->priority
121 fr_time_t recv_time; //!< copied from request->async->recv_time
122
123 uint32_t num_replies; //!< number of reply packets, sent is in retry.count
124
125 bool status_check; //!< is this packet a status check?
126 bool proxied; //!< is this request being proxied
127
128 fr_pair_list_t extra; //!< VPs for debugging, like Proxy-State.
129
130 uint8_t code; //!< Packet code.
131 uint8_t id; //!< Last ID assigned to this packet.
132 uint8_t *packet; //!< Packet we write to the network.
133 size_t packet_len; //!< Length of the packet.
134 size_t partial; //!< partially sent data
135
136 radius_track_entry_t *rr; //!< ID tracking, resend count, etc.
137 fr_timer_t *ev; //!< timer for retransmissions
138 fr_retry_t retry; //!< retransmission timers
139};
140
141typedef struct {
142 bio_handle_ctx_t ctx; //!< for copying to bio_handle_t
143
145
147 connection_t *connections[]; //!< for tracking outbound connections
149
150
151/** Turn a reply code into a module rcode;
152 *
153 */
169
171 UNUSED int flags, void *uctx);
172
173static int encode(bio_handle_t *h, request_t *request, bio_request_t *u, uint8_t id);
174
175static fr_radius_decode_fail_t decode(TALLOC_CTX *ctx, fr_pair_list_t *reply, uint8_t *response_code,
176 bio_handle_t *h, request_t *request, bio_request_t *u,
177 uint8_t const request_authenticator[static RADIUS_AUTH_VECTOR_LENGTH],
178 uint8_t *data, size_t data_len);
179
181
182static void mod_write(request_t *request, trunk_request_t *treq, bio_handle_t *h);
183
184static int _bio_request_free(bio_request_t *u);
185
186static fr_cmp_ret_t home_server_cmp(void const *one, void const *two);
187
188#ifndef NDEBUG
189/** Log additional information about a tracking entry
190 *
191 * @param[in] te Tracking entry we're logging information for.
192 * @param[in] log destination.
193 * @param[in] log_type Type of log message.
194 * @param[in] file the logging request was made in.
195 * @param[in] line logging request was made on.
196 */
197static void bio_tracking_entry_log(fr_log_t const *log, fr_log_type_t log_type, char const *file, int line,
199{
200 request_t *request;
201
202 if (!te->request) return; /* Free entry */
203
204 request = talloc_get_type_abort(te->request, request_t);
205
206 fr_log(log, log_type, file, line, "request %s, allocated %s:%d", request->name,
207 request->alloc_file, request->alloc_line);
208
209 trunk_request_state_log(log, log_type, file, line, talloc_get_type_abort(te->uctx, trunk_request_t));
210}
211#endif
212
213/** Clear out any connection specific resources from a udp request
214 *
215 */
217{
218 TALLOC_FREE(u->packet);
220
221 /*
222 * Can have packet put no u->rr
223 * if this is part of a pre-trunk status check.
224 */
225 if (u->rr) radius_track_entry_release(&u->rr);
226
228}
229
230/** Reset a status_check packet, ready to reuse
231 *
232 */
234{
235 fr_assert(u->status_check == true);
236
237 h->status_checking = false;
238 u->num_replies = 0; /* Reset */
239 u->retry.start = fr_time_wrap(0);
240
242
244}
245
246/*
247 * Status-Server checks. Manually build the packet, and
248 * all of its associated glue.
249 */
251{
252 bio_request_t *u;
253 request_t *request;
254 fr_pair_t *vp;
255 rlm_radius_t const *inst = h->ctx.inst;
256
258
259 MEM(request = request_local_alloc_external(h, (&(request_init_args_t){ .namespace = dict_radius })));
260 MEM(u = talloc_zero(request, bio_request_t));
261 talloc_set_destructor(u, _bio_request_free);
262
263 h->status_u = u;
264
265 h->status_request = request;
267
268 /*
269 * Status checks are prioritized over any other packet
270 */
271 u->priority = ~(uint32_t) 0;
272 u->status_check = true;
273
274 /*
275 * Allocate outside of the free list.
276 * There appears to be an issue where
277 * the thread destructor runs too
278 * early, and frees the freelist's
279 * head before the module destructor
280 * runs.
281 */
282 request->async = talloc_zero(request, fr_async_t);
283 talloc_const_free(request->name);
284 request->name = talloc_strdup(request, h->ctx.module_name);
285
286 request->packet = fr_packet_alloc(request, false);
287 request->reply = fr_packet_alloc(request, false);
288
289 /*
290 * Create the VPs, and ignore any errors creating them.
291 */
292 if (map_list_num_elements(&inst->status_check_map) > 0) {
293 map_t *map = NULL;
294
295 while ((map = map_list_next(&inst->status_check_map, map))) {
296 (void) map_to_request(request, map, map_to_vp, NULL);
297 }
298 } else {
299 /*
300 * Ensure that there's a NAS-Identifier with a value.
301 *
302 * Arguably if there's no status-check map, then the request_pairs are empty. But
303 * whatever...
304 */
305 if (!fr_pair_find_by_da(&request->request_pairs, NULL, attr_nas_identifier)) {
307 fr_pair_value_strdup(vp, "status check - are you alive?", false);
308 }
309 }
310
311 /*
312 * Always add an Event-Timestamp, and update its value every time we send a packet.
313 *
314 * The retry code will later update the timestamp, too, but updating it here means that the debug
315 * output doesn't show a zero-value Event-Timstamp.
316 */
317 vp = fr_pair_find_by_da(&request->request_pairs, NULL, attr_event_timestamp);
318 if (!vp) {
320 }
321 vp->vp_date = fr_time_to_unix_time(fr_time());
322
323 /*
324 * Initialize the request IO ctx. Note that we don't set
325 * destructors.
326 */
327 u->code = inst->status_check;
328 request->packet->code = u->code;
329
330 DEBUG3("%s - Status check packet type will be %s", h->ctx.module_name, fr_radius_packet_name[u->code]);
331 log_request_proto_pair_list(L_DBG_LVL_3, request, NULL, &request->request_pairs, NULL);
332}
333
334/** Connection errored
335 *
336 * We were signalled by the event loop that a fatal error occurred on this connection.
337 *
338 * @param[in] el The event list signalling.
339 * @param[in] fd that errored.
340 * @param[in] flags El flags.
341 * @param[in] fd_errno The nature of the error.
342 * @param[in] uctx The trunk connection handle (tconn).
343 */
344static void conn_init_error(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, int fd_errno, void *uctx)
345{
346 connection_t *conn = talloc_get_type_abort(uctx, connection_t);
347 bio_handle_t *h;
348
349 /*
350 * Connection must be in the connecting state when this fires
351 */
353
354 h = talloc_get_type_abort(conn->h, bio_handle_t);
355
356 ERROR("%s - Connection %s failed: %s", h->ctx.module_name, h->ctx.fd_info->name, fr_syserror(fd_errno));
357
359}
360
361/** Status check timer when opening the connection for the first time.
362 *
363 * Setup retries, or fail the connection.
364 */
365static void conn_init_timeout(UNUSED fr_timer_list_t *tl, fr_time_t now, void *uctx)
366{
367 connection_t *conn = talloc_get_type_abort(uctx, connection_t);
368 bio_handle_t *h;
369 bio_request_t *u;
370
371 /*
372 * Connection must be in the connecting state when this fires
373 */
375
376 h = talloc_get_type_abort(conn->h, bio_handle_t);
377 u = h->status_u;
378
379 /*
380 * We're only interested in contiguous, good, replies.
381 */
382 u->num_replies = 0;
383
384 switch (fr_retry_next(&u->retry, now)) {
385 case FR_RETRY_MRD:
386 DEBUG("%s - Reached maximum_retransmit_duration (%pVs > %pVs), failing status checks",
389 goto fail;
390
391 case FR_RETRY_MRC:
392 DEBUG("%s - Reached maximum_retransmit_count (%u > %u), failing status checks",
394 fail:
396 return;
397
399 if (fr_event_fd_insert(h, NULL, conn->el, h->fd, conn_init_writable, NULL,
400 conn_init_error, conn) < 0) {
401 PERROR("%s - Failed inserting FD event", h->ctx.module_name);
403 }
404 return;
405 }
406
407 fr_assert(0);
408}
409
410/** Perform the next step of init and negotiation.
411 *
412 */
413static void conn_init_next(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
414{
415 connection_t *conn = talloc_get_type_abort(uctx, connection_t);
416 bio_handle_t *h = talloc_get_type_abort(conn->h, bio_handle_t);
417
418 if (fr_event_fd_insert(h, NULL, conn->el, h->fd, conn_init_writable, NULL, conn_init_error, conn) < 0) {
419 PERROR("%s - Failed inserting FD event", h->ctx.module_name);
421 }
422}
423
424/** Read the connection during the init and negotiation stage.
425 *
426 */
427static void conn_init_readable(fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
428{
429 connection_t *conn = talloc_get_type_abort(uctx, connection_t);
430 bio_handle_t *h = talloc_get_type_abort(conn->h, bio_handle_t);
431 trunk_t *trunk = h->ctx.trunk;
432 rlm_radius_t const *inst = h->ctx.inst;
433 bio_request_t *u = h->status_u;
434 ssize_t slen;
435 fr_pair_list_t reply;
436 uint8_t code = 0;
437
438 fr_pair_list_init(&reply);
439 slen = fr_bio_read(h->bio.main, NULL, h->buffer, h->buflen);
440 if (slen == 0) {
441 /*
442 * @todo - set BIO FD EOF callback, so that we don't have to check it here.
443 */
444 if (h->ctx.fd_info->eof) goto failed;
445 return;
446 }
447
448 /*
449 * We're done reading, return.
450 */
451 if (slen == fr_bio_error(IO_WOULD_BLOCK)) return;
452
453 if (slen < 0) {
454 switch (errno) {
455 case ECONNREFUSED:
456 ERROR("%s - Failed reading response from socket: there is no server listening on outgoing connection %s",
457 h->ctx.module_name, h->ctx.fd_info->name);
458 break;
459
460 default:
461 ERROR("%s - Failed reading response from socket: %s",
462 h->ctx.module_name, fr_syserror(errno));
463 break;
464 }
465
466 failed:
468 return;
469 }
470
471 /*
472 * Where we just return in this function, we're letting
473 * the response timer take care of progressing the
474 * connection attempt.
475 */
476 fr_assert(slen >= RADIUS_HEADER_LENGTH); /* checked in verify */
477
478 if (!u->packet) {
479 ERROR("%s - Received response to expired status check packet",
480 h->ctx.module_name);
481 return;
482 }
483
484 if (u->id != h->buffer[1]) {
485 ERROR("%s - Received response with incorrect or expired ID. Expected %u, got %u",
486 h->ctx.module_name, u->id, h->buffer[1]);
487 return;
488 }
489
490 if (decode(h, &reply, &code,
492 h->buffer, slen) != FR_RADIUS_FAIL_NONE) return;
493
494 fr_pair_list_free(&reply); /* FIXME - Do something with these... */
495
496 /*
497 * Process the error, and count this as a success.
498 * This is usually used for dynamic configuration
499 * on startup.
500 */
502
503 /*
504 * Last trunk event was a failure, be more careful about
505 * bringing up the connection (require multiple responses).
506 */
507 if ((fr_time_gt(trunk->last_failed, fr_time_wrap(0)) && (fr_time_gt(trunk->last_failed, trunk->last_connected))) &&
508 (u->num_replies < inst->num_answers_to_alive)) {
509 /*
510 * Leave the timer in place. This timer is BOTH when we
511 * give up on the current status check, AND when we send
512 * the next status check.
513 */
514 DEBUG("%s - Received %u / %u replies for status check, on connection - %s",
515 h->ctx.module_name, u->num_replies, inst->num_answers_to_alive, h->ctx.fd_info->name);
516 DEBUG("%s - Next status check packet will be in %pVs",
518
519 /*
520 * Set the timer for the next retransmit.
521 */
522 if (fr_timer_at(h, el->tl, &u->ev, u->retry.next, false, conn_init_next, conn) < 0) {
524 }
525 return;
526 }
527
528 /*
529 * It's alive!
530 */
531 status_check_reset(h, u);
532
533 DEBUG("%s - Connection open - %s", h->ctx.module_name, h->ctx.fd_info->name);
534
536}
537
538/** Send initial negotiation.
539 *
540 */
541static void conn_init_writable(fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
542{
543 connection_t *conn = talloc_get_type_abort(uctx, connection_t);
544 bio_handle_t *h = talloc_get_type_abort(conn->h, bio_handle_t);
545 bio_request_t *u = h->status_u;
546 ssize_t slen;
547
548 if (fr_time_eq(u->retry.start, fr_time_wrap(0))) {
549 u->id = fr_rand() & 0xff; /* We don't care what the value is here */
550 h->status_checking = true; /* Ensure this is valid */
551 fr_retry_init(&u->retry, fr_time(), &h->ctx.inst->retry[u->code]);
552
553 /*
554 * Status checks can never be retransmitted
555 * So increment the ID here.
556 */
557 } else {
559 u->id++;
560 }
561
562 DEBUG("%s - Sending %s ID %d over connection %s",
564
565 if (encode(h, h->status_request, u, u->id) < 0) {
566 fail:
568 return;
569 }
570 DEBUG3("Encoded packet");
571 HEXDUMP3(u->packet, u->packet_len, NULL);
572
573 fr_assert(u->packet != NULL);
575
576 slen = fr_bio_write(h->bio.main, NULL, u->packet, u->packet_len);
577
578 if (slen == fr_bio_error(IO_WOULD_BLOCK)) goto blocked;
579
580 if (slen < 0) {
581 ERROR("%s - Failed sending %s ID %d length %zu over connection %s: %s",
583 goto fail;
584 }
585
586 /*
587 * @todo - handle partial packets and blocked writes.
588 */
589 if ((size_t)slen < u->packet_len) {
590 blocked:
591 ERROR("%s - Failed sending %s ID %d length %zu over connection %s: writing is blocked",
593 goto fail;
594 }
595
596 /*
597 * Switch to waiting on read and insert the event
598 * for the response timeout.
599 */
600 if (fr_event_fd_insert(h, NULL, conn->el, h->fd, conn_init_readable, NULL, conn_init_error, conn) < 0) {
601 PERROR("%s - Failed inserting FD event", h->ctx.module_name);
602 goto fail;
603 }
604
605 DEBUG("%s - %s request. Expecting response within %pVs",
606 h->ctx.module_name, (u->retry.count == 1) ? "Originated" : "Retransmitted",
608
609 if (fr_timer_at(h, el->tl, &u->ev, u->retry.next, false, conn_init_timeout, conn) < 0) {
610 PERROR("%s - Failed inserting timer event", h->ctx.module_name);
611 goto fail;
612 }
613}
614
615/** Free a connection handle, closing associated resources
616 *
617 */
619{
620 fr_assert(h != NULL);
621
622 fr_assert(h->fd >= 0);
623
625
626 /*
627 * The connection code will take care of deleting the FD from the event loop.
628 */
629
630 DEBUG("%s - Connection closed - %s", h->ctx.module_name, h->ctx.fd_info->name);
631
632 return 0;
633}
634
635static void bio_connected(fr_bio_t *bio)
636{
637 bio_handle_t *h = bio->uctx;
638
639 DEBUG("%s - Connection open - %s", h->ctx.module_name, h->ctx.fd_info->name);
640
642}
643
644static void bio_error(fr_bio_t *bio)
645{
646 bio_handle_t *h = bio->uctx;
647
648 DEBUG("%s - Connection failed - %s - %s", h->ctx.module_name, h->ctx.fd_info->name,
650
652}
653
654static fr_bio_verify_action_t rlm_radius_verify(UNUSED fr_bio_t *bio, void *verify_ctx, UNUSED void *packet_ctx, const void *data, size_t *size)
655{
657 size_t in_buffer = *size;
658 bio_handle_t *h = verify_ctx;
659 uint8_t const *hdr = data;
660 size_t want;
661
662 if (in_buffer < 20) {
663 *size = RADIUS_HEADER_LENGTH;
665 }
666
667 /*
668 * Packet is too large, discard it.
669 */
670 want = fr_nbo_to_uint16(hdr + 2);
671 if (want > h->ctx.inst->max_packet_size) {
672 ERROR("%s - Connection %s received too long packet", h->ctx.module_name, h->ctx.fd_info->name);
674 }
675
676 /*
677 * Not a full packet, we want more data.
678 */
679 if (want > *size) {
680 *size = want;
682 }
683
684#define REQUIRE_MA(_h) (((_h)->ctx.inst->require_message_authenticator == FR_RADIUS_REQUIRE_MA_YES) || *(_h)->ctx.inst->received_message_authenticator)
685
686 /*
687 * See if we need to discard the packet.
688 *
689 * @todo - rate limit these messages, and find a way to associate them with a request, or even
690 * the logging destination of the module.
691 */
692 if (!fr_radius_ok(data, size, h->ctx.inst->max_attributes, REQUIRE_MA(h), &failure)) {
694
695 PERROR("%s - Connection %s received bad packet", h->ctx.module_name, h->ctx.fd_info->name);
696
697 if (failure == FR_RADIUS_FAIL_MA_MISSING) {
699 ERROR("We are configured with 'require_message_authenticator = true'");
700 } else {
701 ERROR("We previously received a packet from this client which included a Message-Authenticator attribute");
702 }
703 }
704
705 if (h->ctx.fd_config.socket_type == SOCK_DGRAM) return FR_BIO_VERIFY_DISCARD;
706
708 }
709
710 /*
711 * @todo - check if the reply is allowed. Bad replies are discarded later, but it might be worth
712 * checking them here.
713 */
714
715 /*
716 * On input, *size is how much data we have. On output, *size is how much data we want.
717 */
718 return (in_buffer >= *size) ? FR_BIO_VERIFY_OK : FR_BIO_VERIFY_WANT_MORE;
719}
720
721
722/** Initialise a new outbound connection
723 *
724 * @param[out] h_out Where to write the new file descriptor.
725 * @param[in] conn to initialise.
726 * @param[in] uctx A #bio_handle_ctx_t
727 */
728CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
729static connection_state_t conn_init(void **h_out, connection_t *conn, void *uctx)
730{
731 int fd;
732 bio_handle_t *h;
733 bio_handle_ctx_t *ctx = uctx; /* thread or home server */
734 connection_t **to_save = NULL;
735
736 MEM(h = talloc_zero(conn, bio_handle_t));
737 h->ctx = *ctx;
738 h->conn = conn;
740 h->last_idle = fr_time();
741
742 MEM(h->buffer = talloc_array(h, uint8_t, h->max_packet_size));
743 h->buflen = h->max_packet_size;
744
745 MEM(h->tt = radius_track_alloc(h));
746
747 /*
748 * We are proxying to multiple home servers, but using a limited port range. We must track the
749 * source port for each home server, so that we only can select the right unused source port for
750 * this home server.
751 */
752 switch (ctx->limit_source_ports) {
753 case LIMIT_PORTS_NONE:
754 break;
755
756 /*
757 * Dynamic home servers store source port usage in the home_server_t
758 */
760 {
761 int i;
762 home_server_t *home = talloc_get_type_abort(ctx, home_server_t);
763
764 for (i = 0; i < home->num_ports; i++) {
765 if (!home->connections[i]) {
766 to_save = &home->connections[i];
767
768 /*
769 * Set the source port, but also leave the src_port_start and
770 * src_port_end alone.
771 */
773 break;
774 }
775 }
776
777 if (!to_save) {
778 ERROR("%s - Failed opening socket to home server %pV:%u - source port range is full",
780 goto fail;
781 }
782 }
783 break;
784
785 /*
786 * Static home servers store source port usage in bio_thread_t
787 */
789 {
790 int i;
791 bio_thread_t *thread = talloc_get_type_abort(ctx, bio_thread_t);
792
793 for (i = 0; i < thread->num_ports; i++) {
794 if (!thread->connections[i]) {
795 to_save = &thread->connections[i];
797 break;
798 }
799 }
800
801 if (!to_save) {
802 ERROR("%s - Failed opening socket to home server %pV:%u - source port range is full",
804 goto fail;
805 }
806 }
807 break;
808 }
809
810 h->bio.fd = fr_bio_fd_alloc(h, &h->ctx.fd_config, 0);
811 if (!h->bio.fd) {
812 PERROR("%s - failed opening socket", h->ctx.module_name);
813 fail:
814 talloc_free(h);
816 }
817
818 h->bio.fd->uctx = h;
820
821 fd = h->ctx.fd_info->socket.fd;
822 fr_assert(fd >= 0);
823
824 /*
825 * Create a memory BIO for stream sockets. We want to return only complete packets, and not
826 * partial packets.
827 *
828 * @todo - maybe we want to have a fr_bio_verify_t which is independent of fr_bio_mem_t. That
829 * way we don't need a memory BIO for UDP sockets, but we can still add a verification layer for
830 * UDP sockets?
831 */
832 h->bio.mem = fr_bio_mem_alloc(h, (h->ctx.fd_config.socket_type == SOCK_DGRAM) ? 0 : h->ctx.inst->max_packet_size * 4,
833 0, h->bio.fd);
834 if (!h->bio.mem) {
835 PERROR("%s - Failed allocating memory buffer - ", h->ctx.module_name);
836 goto fail;
837 }
838
839 if (fr_bio_mem_set_verify(h->bio.mem, rlm_radius_verify, h, (h->ctx.fd_config.socket_type == SOCK_DGRAM)) < 0) {
840 PERROR("%s - Failed setting validation callback - ", h->ctx.module_name);
841 goto fail;
842 }
843
844 /*
845 * Set the BIO read function to be the memory BIO, which will then call the packet verification
846 * routine.
847 */
848 h->bio.main = h->bio.mem;
849 h->bio.mem->uctx = h;
850
851 h->fd = fd;
852
853 talloc_set_destructor(h, _bio_handle_free);
854
855 /*
856 * If the socket isn't connected, then do that first.
857 */
859 int rcode;
860
862
863 /*
864 * We don't pass timeouts here because the trunk has it's own connection timeouts.
865 */
866 rcode = fr_bio_fd_connect_full(h->bio.fd, conn->el, bio_connected, bio_error, NULL, NULL);
867 if (rcode < 0) goto fail;
868
869 *h_out = h;
870
871 if (rcode == 0) return CONNECTION_STATE_CONNECTING;
872
873 fr_assert(rcode == 1);
875
876 /*
877 * If we're doing status checks, then we want at least
878 * one positive response before signalling that the
879 * connection is open.
880 *
881 * To do this we install special I/O handlers that
882 * only signal the connection as open once we get a
883 * status-check response.
884 */
885 } if (h->ctx.inst->status_check) {
887
888 /*
889 * Start status checking.
890 *
891 * If we've had no recent failures we need exactly
892 * one response to bring the connection online,
893 * otherwise we need inst->num_answers_to_alive
894 */
895 if (fr_event_fd_insert(h, NULL, conn->el, h->fd, NULL,
896 conn_init_writable, conn_init_error, conn) < 0) goto fail;
897
898 /*
899 * If we're not doing status-checks, signal the connection
900 * as open as soon as it becomes writable.
901 */
902 } else {
903 connection_signal_on_fd(conn, fd);
904 }
905
906 *h_out = h;
907
908 if (to_save) *to_save = conn;
909
911}
912
913/** Shutdown/close a file descriptor
914 *
915 */
916static void conn_close(UNUSED fr_event_list_t *el, void *handle, void *uctx)
917{
918 bio_handle_t *h = talloc_get_type_abort(handle, bio_handle_t);
919
920 /*
921 * There's tracking entries still allocated
922 * this is bad, they should have all been
923 * released.
924 */
925 if (h->tt && (h->tt->num_requests != 0)) {
926#ifndef NDEBUG
928#endif
929 fr_assert_fail("%u tracking entries still allocated at conn close", h->tt->num_requests);
930 }
931
932 /*
933 * We have opened a limited number of outbound source ports. This means that when we close a
934 * port, we have to mark it unused.
935 */
936 switch (h->ctx.limit_source_ports) {
937 case LIMIT_PORTS_NONE:
938 break;
939
941 {
942 int offset;
943 home_server_t *home = talloc_get_type_abort(uctx, home_server_t);
944
947
949 fr_assert(offset < home->num_ports);
950
951 fr_assert(home->connections[offset] == h->conn);
952
953 home->connections[offset] = NULL;
954 }
955 break;
956
958 {
959 int offset;
960 bio_thread_t *thread = talloc_get_type_abort(uctx, bio_thread_t);
961
964
966 fr_assert(offset < thread->num_ports);
967
968 fr_assert(thread->connections[offset] == h->conn);
969
970 thread->connections[offset] = NULL;
971 }
972 break;
973 }
974
975 DEBUG4("Freeing handle %p", handle);
976
977 talloc_free(h);
978}
979
980/** Connection failed
981 *
982 * @param[in] handle of connection that failed.
983 * @param[in] state the connection was in when it failed.
984 * @param[in] uctx UNUSED.
985 */
986static connection_state_t conn_failed(void *handle, connection_state_t state, UNUSED void *uctx)
987{
988 switch (state) {
989 /*
990 * If the connection was connected when it failed,
991 * we need to handle any outstanding packets and
992 * timer events before reconnecting.
993 */
995 {
996 bio_handle_t *h = talloc_get_type_abort(handle, bio_handle_t); /* h only available if connected */
997
998 /*
999 * Reset the Status-Server checks.
1000 */
1001 if (h->status_u) FR_TIMER_DISARM(h->status_u->ev);
1002 break;
1003
1004 default:
1005 break;
1006 }
1007 }
1008
1009 return CONNECTION_STATE_INIT;
1010}
1011
1012CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
1014 connection_conf_t const *conf,
1015 char const *log_prefix, void *uctx)
1016{
1017 connection_t *conn;
1018 bio_handle_ctx_t *ctx = uctx; /* thread or home server */
1019
1020 conn = connection_alloc(tconn, el,
1022 .init = conn_init,
1023 .close = conn_close,
1024 .failed = conn_failed
1025 },
1026 conf,
1027 log_prefix,
1028 uctx);
1029 if (!conn) {
1030 PERROR("%s - Failed allocating state handler for new connection", ctx->inst->name);
1031 return NULL;
1032 }
1033 ctx->trunk = tconn->trunk;
1034 ctx->module_name = log_prefix;
1035
1036 return conn;
1037}
1038
1039/** Read and discard data
1040 *
1041 */
1042static void conn_discard(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
1043{
1044 trunk_connection_t *tconn = talloc_get_type_abort(uctx, trunk_connection_t);
1045 bio_handle_t *h = talloc_get_type_abort(tconn->conn->h, bio_handle_t);
1046 uint8_t buffer[4096];
1047 ssize_t slen;
1048
1049 while ((slen = fr_bio_read(h->bio.main, NULL, buffer, sizeof(buffer))) > 0);
1050
1051 if (slen < 0) {
1052 switch (errno) {
1053 case EBADF:
1054 case ECONNRESET:
1055 case ENOTCONN:
1056 case ETIMEDOUT:
1057 ERROR("%s - Failed draining socket: %s", h->ctx.module_name, fr_syserror(errno));
1059 break;
1060
1061 default:
1062 break;
1063 }
1064 }
1065}
1066
1067/** Connection errored
1068 *
1069 * We were signalled by the event loop that a fatal error occurred on this connection.
1070 *
1071 * @param[in] el The event list signalling.
1072 * @param[in] fd that errored.
1073 * @param[in] flags El flags.
1074 * @param[in] fd_errno The nature of the error.
1075 * @param[in] uctx The trunk connection handle (tconn).
1076 */
1077static void conn_error(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, int fd_errno, void *uctx)
1078{
1079 trunk_connection_t *tconn = talloc_get_type_abort(uctx, trunk_connection_t);
1080 connection_t *conn = tconn->conn;
1081 bio_handle_t *h = talloc_get_type_abort(conn->h, bio_handle_t);
1082
1083 if (fd_errno) ERROR("%s - Connection %s failed: %s", h->ctx.module_name, h->ctx.fd_info->name, fr_syserror(fd_errno));
1084
1086}
1087
1088CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
1091 trunk_connection_event_t notify_on, UNUSED void *uctx)
1092{
1093 bio_handle_t *h = talloc_get_type_abort(conn->h, bio_handle_t);
1094 fr_event_fd_cb_t read_fn = NULL;
1095 fr_event_fd_cb_t write_fn = NULL;
1096
1097 switch (notify_on) {
1098 /*
1099 * We may have sent multiple requests to the
1100 * other end, so it might be sending us multiple
1101 * replies. We want to drain the socket, instead
1102 * of letting the packets sit in the UDP receive
1103 * queue.
1104 */
1106 read_fn = conn_discard;
1107 break;
1108
1111 break;
1112
1115 break;
1116
1120 break;
1121
1122 }
1123
1124 /*
1125 * Over-ride read for replication.
1126 */
1128 read_fn = conn_discard;
1129
1130 if (fr_bio_fd_write_only(h->bio.fd) < 0) {
1131 PERROR("%s - Failed setting socket to write-only", h->ctx.module_name);
1133 return;
1134 }
1135 }
1136
1137 if (fr_event_fd_insert(h, NULL, el, h->fd,
1138 read_fn,
1139 write_fn,
1140 conn_error,
1141 tconn) < 0) {
1142 PERROR("%s - Failed inserting FD event", h->ctx.module_name);
1143
1144 /*
1145 * May free the connection!
1146 */
1148 }
1149}
1150
1151/*
1152 * Return negative numbers to put 'a' at the top of the heap.
1153 * Return positive numbers to put 'b' at the top of the heap.
1154 *
1155 * We want the value with the lowest timestamp to be prioritized at
1156 * the top of the heap.
1157 */
1158static fr_cmp_ret_t request_prioritise(void const *one, void const *two)
1159{
1160 bio_request_t const *a = one;
1161 bio_request_t const *b = two;
1162 int8_t ret;
1163
1164 /*
1165 * Prioritise status check packets
1166 */
1168 if (ret != 0) return ret;
1169
1170 /*
1171 * Larger priority is more important.
1172 */
1173 ret = CMP_PREFER_LARGER(a->priority, b->priority);
1174 if (ret != 0) return ret;
1175
1176 /*
1177 * Smaller timestamp (i.e. earlier) is more important.
1178 */
1179 return fr_time_cmp(a->recv_time, b->recv_time);
1180}
1181
1182/** Decode response packet data, extracting relevant information and validating the packet
1183 *
1184 * @param[in] ctx to allocate pairs in.
1185 * @param[out] reply Pointer to head of pair list to add reply attributes to.
1186 * @param[out] response_code The type of response packet.
1187 * @param[in] h connection handle.
1188 * @param[in] request the request.
1189 * @param[in] u UDP request.
1190 * @param[in] request_authenticator from the original request.
1191 * @param[in] data to decode.
1192 * @param[in] data_len Length of input data.
1193 * @return
1194 * - FR_RADIUS_FAIL_NONE on success.
1195 * - FR_RADIUS_FAIL_* on failure.
1196 */
1197static fr_radius_decode_fail_t decode(TALLOC_CTX *ctx, fr_pair_list_t *reply, uint8_t *response_code,
1198 bio_handle_t *h, request_t *request, bio_request_t *u,
1199 uint8_t const request_authenticator[static RADIUS_AUTH_VECTOR_LENGTH],
1200 uint8_t *data, size_t data_len)
1201{
1203 uint8_t code;
1204 fr_radius_decode_ctx_t decode_ctx;
1205
1206 *response_code = 0; /* Initialise to keep the rest of the code happy */
1207
1208 RHEXDUMP3(data, data_len, "Read packet");
1209
1210 decode_ctx = (fr_radius_decode_ctx_t) {
1211 .common = &h->ctx.radius_ctx,
1212 .request_code = u->code,
1213 .request_authenticator = request_authenticator,
1214 .tmp_ctx = talloc(ctx, uint8_t),
1215 .end = data + data_len,
1216 .verify = true,
1217 .require_message_authenticator = REQUIRE_MA(h),
1218 };
1219
1220 if (fr_radius_decode(ctx, reply, data, data_len, &decode_ctx) < 0) {
1221 talloc_free(decode_ctx.tmp_ctx);
1222 RPEDEBUG("Failed reading packet");
1223 return decode_ctx.reason;
1224 }
1225 talloc_free(decode_ctx.tmp_ctx);
1226
1227 code = data[0];
1228
1229 RDEBUG("Received %s ID %d length %zu reply packet on connection %s",
1230 fr_radius_packet_name[code], data[1], data_len, h->ctx.fd_info->name);
1231 log_request_pair_list(L_DBG_LVL_2, request, NULL, reply, NULL);
1232
1233 /*
1234 * This code is for BlastRADIUS mitigation.
1235 *
1236 * The scenario where this applies is where we send Message-Authenticator
1237 * but the home server doesn't support it or require it, in which case
1238 * the response can be manipulated by an attacker.
1239 */
1240 if ((u->code == FR_RADIUS_CODE_ACCESS_REQUEST) &&
1241 (inst->require_message_authenticator == FR_RADIUS_REQUIRE_MA_AUTO) &&
1242 !*(inst->received_message_authenticator) &&
1244 !fr_pair_find_by_da(reply, NULL, attr_eap_message)) {
1245 RINFO("Packet contained a valid Message-Authenticator. Setting \"require_message_authenticator = yes\"");
1246 *(inst->received_message_authenticator) = true;
1247 }
1248
1249 *response_code = code;
1250
1251 /*
1252 * Record the fact we've seen a response
1253 */
1254 u->num_replies++;
1255
1256 /*
1257 * Fixup retry times
1258 */
1259 if (fr_time_gt(u->retry.start, h->mrs_time)) h->mrs_time = u->retry.start;
1260
1261 return FR_RADIUS_FAIL_NONE;
1262}
1263
1264static int encode(bio_handle_t *h, request_t *request, bio_request_t *u, uint8_t id)
1265{
1266 ssize_t packet_len;
1268 rlm_radius_t const *inst = h->ctx.inst;
1269
1270 fr_assert(inst->allowed[u->code]);
1271 fr_assert(!u->packet);
1272
1273 u->packet_len = inst->max_packet_size;
1274 u->packet = h->buffer;
1275
1276 /*
1277 * We should have at minimum 64-byte packets, so don't
1278 * bother doing run-time checks here.
1279 */
1281
1283 .common = &h->ctx.radius_ctx,
1284 .rand_ctx = (fr_fast_rand_t) {
1285 .a = fr_rand(),
1286 .b = fr_rand(),
1287 },
1288 .code = u->code,
1289 .id = id,
1290 .add_proxy_state = u->proxied,
1291 };
1292
1293 /*
1294 * If we're sending a status check packet, update any
1295 * necessary timestamps. Also, don't add Proxy-State, as
1296 * we're originating the packet.
1297 */
1298 if (u->status_check) {
1299 fr_pair_t *vp;
1300
1301 vp = fr_pair_find_by_da(&request->request_pairs, NULL, attr_event_timestamp);
1302 if (vp) vp->vp_date = fr_time_to_unix_time(u->retry.updated);
1303
1304 encode_ctx.add_proxy_state = false;
1305 }
1306
1307 /*
1308 * Encode it, leaving room for Proxy-State if necessary.
1309 */
1310 packet_len = fr_radius_encode(&FR_DBUFF_TMP(u->packet, u->packet_len),
1311 &request->request_pairs, &encode_ctx);
1312 if (packet_len < 0) {
1313 RPERROR("Failed encoding packet");
1314 return -1;
1315 }
1316
1317 /*
1318 * The encoded packet should NOT over-run the input buffer.
1319 */
1320 fr_assert((size_t) packet_len <= u->packet_len);
1321
1322 /*
1323 * Add Proxy-State to the tail end of the packet.
1324 *
1325 * We need to add it here, and NOT in
1326 * request->request_pairs, because multiple modules
1327 * may be sending the packets at the same time.
1328 */
1329 if (encode_ctx.add_proxy_state) {
1330 fr_pair_t *vp;
1331
1333 fr_pair_value_memdup(vp, (uint8_t const *) &inst->common_ctx.proxy_state, sizeof(inst->common_ctx.proxy_state), false);
1334 fr_pair_append(&u->extra, vp);
1335 packet_len += 2 + sizeof(inst->common_ctx.proxy_state);
1336 }
1337
1338 /*
1339 * Update our version of the packet length.
1340 */
1341 u->packet_len = packet_len;
1342
1343 /*
1344 * Now that we're done mangling the packet, sign it.
1345 */
1346 if (fr_radius_sign(u->packet, NULL, (uint8_t const *) h->ctx.radius_ctx.secret,
1347 h->ctx.radius_ctx.secret_length) < 0) {
1348 RPERROR("Failed signing packet");
1349 return -1;
1350 }
1351
1352 MEM(u->packet = talloc_memdup(u, h->buffer, packet_len));
1353
1354 return 0;
1355}
1356
1357
1358/** Revive a connection after "revive_interval"
1359 *
1360 */
1361static void revive_timeout(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
1362{
1363 trunk_connection_t *tconn = talloc_get_type_abort(uctx, trunk_connection_t);
1364 bio_handle_t *h = talloc_get_type_abort(tconn->conn->h, bio_handle_t);
1365
1366 INFO("%s - Reviving connection %s", h->ctx.module_name, h->ctx.fd_info->name);
1368}
1369
1370/** Mark a connection dead after "zombie_interval"
1371 *
1372 */
1373static void zombie_timeout(fr_timer_list_t *tl, fr_time_t now, void *uctx)
1374{
1375 trunk_connection_t *tconn = talloc_get_type_abort(uctx, trunk_connection_t);
1376 bio_handle_t *h = talloc_get_type_abort(tconn->conn->h, bio_handle_t);
1377
1378 INFO("%s - No replies during 'zombie_period', marking connection %s as dead", h->ctx.module_name, h->ctx.fd_info->name);
1379
1380 /*
1381 * Don't use this connection, and re-queue all of its
1382 * requests onto other connections.
1383 */
1385
1386 /*
1387 * We do have status checks. Try to reconnect the
1388 * connection immediately. If the status checks pass,
1389 * then the connection will be marked "alive"
1390 */
1391 if (h->ctx.inst->status_check) {
1393 return;
1394 }
1395
1396 if (!h->ctx.dynamic) {
1397 /*
1398 * Force the instruction to immediately fail until the revive interval has expired.
1399 */
1400 unlang_interpret_force_result(h->instruction, &(unlang_result_t){.rcode = RLM_MODULE_FAIL}, tl,
1402
1403 /*
1404 * Mark the connection as active, so when the module forced result times out
1405 * requests will be sent again.
1406 */
1408 return;
1409 }
1410
1411 /*
1412 * Revive the connection after a time.
1413 */
1414 if (fr_timer_at(h, tl, &h->zombie_ev,
1415 fr_time_add(now, h->ctx.inst->revive_interval), false,
1416 revive_timeout, tconn) < 0) {
1417 ERROR("Failed inserting revive timeout for connection");
1419 }
1420}
1421
1422
1423/** See if the connection is zombied.
1424 *
1425 * We check for zombie when major events happen:
1426 *
1427 * 1) request hits its final timeout
1428 * 2) request timer hits, and it needs to be retransmitted
1429 * 3) a DUP packet comes in, and the request needs to be retransmitted
1430 * 4) we're sending a packet.
1431 *
1432 * There MIGHT not be retries configured, so we MUST check for zombie
1433 * when any new packet comes in. Similarly, there MIGHT not be new
1434 * packets, but retries are configured, so we have to check there,
1435 * too.
1436 *
1437 * Also, the socket might not be writable for a while. There MIGHT
1438 * be a long time between getting the timer / DUP signal, and the
1439 * request finally being written to the socket. So we need to check
1440 * for zombie at BOTH the timeout and the mux / write function.
1441 *
1442 * @return
1443 * - true if the connection is zombie.
1444 * - false if the connection is not zombie.
1445 */
1446static bool check_for_zombie(request_t *request, trunk_connection_t *tconn, fr_time_t now, fr_time_t last_sent)
1447{
1448 bio_handle_t *h = talloc_get_type_abort(tconn->conn->h, bio_handle_t);
1450
1451 /*
1452 * We're replicating, and don't care about the health of
1453 * the home server, and this function should not be called.
1454 */
1456
1457 /*
1458 * If we're status checking OR already zombie, don't go to zombie
1459 */
1460 if (h->status_checking || fr_timer_armed(h->zombie_ev)) return true;
1461
1462 if (fr_time_eq(now, fr_time_wrap(0))) now = fr_time();
1463
1464 /*
1465 * We received a reply since this packet was sent, the connection isn't zombie.
1466 */
1467 if (fr_time_gteq(h->last_reply, last_sent)) return false;
1468
1469 /*
1470 * If we've seen ANY response in the allowed window, then the connection is still alive.
1471 */
1472 if ((h->ctx.inst->mode == RLM_RADIUS_MODE_PROXY) &&
1473 fr_time_gt(h->last_reply, fr_time_sub(now, h->ctx.inst->response_window))) return false;
1474
1475 /*
1476 * Stop using it for new requests.
1477 */
1478 WARN("%s - Entering Zombie state - connection %s", h->ctx.module_name, h->ctx.fd_info->name);
1480
1481 if (h->ctx.inst->status_check) {
1482 /*
1483 * If it's UDP, reconnect. This will start the sending of status checks.
1484 */
1485 if (h->ctx.inst->fd_config.socket_type == SOCK_DGRAM) {
1488 }
1489
1490 h->status_checking = true;
1491
1492 /*
1493 * Queue up the status check packet. It will be sent
1494 * when the connection is writable.
1495 */
1497 h->status_u->treq = NULL;
1498
1500 h->status_u, h->status_u, true) != TRUNK_ENQUEUE_OK) {
1502 }
1503 } else {
1504 /*
1505 * Capture the instruction which started the zombie period.
1506 */
1508
1509 if (fr_timer_at(h, el->tl, &h->zombie_ev, fr_time_add(now, h->ctx.inst->zombie_period),
1510 false, zombie_timeout, tconn) < 0) {
1511 ERROR("Failed inserting zombie timeout for connection");
1513 }
1514 }
1515
1516 return true;
1517}
1518
1519static void mod_dup(request_t *request, bio_request_t *u)
1520{
1521 bio_handle_t *h;
1522
1523 h = talloc_get_type_abort(u->treq->tconn->conn->h, bio_handle_t);
1524
1525 if (h->ctx.fd_config.socket_type != SOCK_DGRAM) {
1526 RDEBUG("Using stream sockets - suppressing retransmission");
1527 return;
1528 }
1529
1530 /*
1531 * Arguably this should never happen for UDP sockets.
1532 */
1533 if (h->ctx.fd_info->write_blocked) {
1534 RDEBUG("IO is blocked - suppressing retransmission");
1535 return;
1536 }
1537 u->is_retry = true;
1538
1539 /*
1540 * We are doing synchronous proxying, retransmit
1541 * the current request on the same connection.
1542 *
1543 * If it's zombie, we still resend it. If the
1544 * connection is dead, then a callback will move
1545 * this request to a new connection.
1546 */
1547 mod_write(request, u->treq, h);
1548}
1549
1550static void do_retry(rlm_radius_t const *inst, bio_request_t *u, request_t *request, fr_retry_t const *retry);
1551
1552/** Handle module retries.
1553 *
1554 */
1555static void mod_retry(module_ctx_t const *mctx, request_t *request, fr_retry_t const *retry)
1556{
1557 bio_request_t *u = talloc_get_type_abort(mctx->rctx, bio_request_t);
1558 rlm_radius_t const *inst = talloc_get_type_abort(mctx->mi->data, rlm_radius_t);
1559
1560 do_retry(inst, u, request, retry);
1561}
1562
1563static void do_retry(rlm_radius_t const *inst, bio_request_t *u, request_t *request, fr_retry_t const *retry)
1564{
1565 trunk_request_t *treq;
1566 trunk_connection_t *tconn;
1567 fr_time_t now;
1568
1569 if (!u->treq) {
1570 RDEBUG("Packet was cancelled by the connection handler - ignoring retry");
1571 return;
1572 }
1573
1574 treq = talloc_get_type_abort(u->treq, trunk_request_t);
1575
1576 fr_assert(request == treq->request);
1577 fr_assert(treq->preq); /* Must still have a protocol request */
1578 fr_assert(treq->preq == u);
1579
1580 tconn = treq->tconn;
1581 now = retry->updated;
1582
1583 switch (retry->state) {
1584 case FR_RETRY_CONTINUE:
1585 u->retry = *retry;
1586
1587 switch (treq->state) {
1590 fr_assert(0);
1591 break;
1592
1594 RDEBUG("Packet is still in the backlog queue to be sent - suppressing retransmission");
1595 return;
1596
1598 RDEBUG("Packet is still in the pending queue to be sent - suppressing retransmission");
1599 return;
1600
1602 RDEBUG("Packet was partially written, as IO is blocked - suppressing retransmission");
1603 return;
1604
1606 fr_assert(tconn);
1607
1608 mod_dup(request, u);
1609 return;
1610
1618 fr_assert(0);
1619 break;
1620 }
1621 break;
1622
1623 case FR_RETRY_MRD:
1624 REDEBUG("Reached maximum_retransmit_duration (%pVs > %pVs), failing request",
1626 break;
1627
1628 case FR_RETRY_MRC:
1629 REDEBUG("Reached maximum_retransmit_count (%u > %u), failing request",
1630 retry->count, retry->config->mrc);
1631 break;
1632 }
1633
1636
1637 /*
1638 * We don't do zombie stuff!
1639 */
1640 if (!tconn || (inst->mode == RLM_RADIUS_MODE_REPLICATE)) return;
1641
1642 check_for_zombie(request, tconn, now, retry->start);
1643}
1644
1645CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
1647 trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
1648{
1649 bio_handle_t *h = talloc_get_type_abort(conn->h, bio_handle_t);
1650 trunk_request_t *treq;
1651 request_t *request;
1652
1653 if (unlikely(trunk_connection_pop_request(&treq, tconn) < 0)) return;
1654
1655 /*
1656 * No more requests to send
1657 */
1658 if (!treq) return;
1659
1660 request = treq->request;
1661
1662 mod_write(request, treq, h);
1663}
1664
1665static void mod_write(request_t *request, trunk_request_t *treq, bio_handle_t *h)
1666{
1667 rlm_radius_t const *inst = h->ctx.inst;
1668 bio_request_t *u;
1669 char const *action;
1670 uint8_t const *packet;
1671 size_t packet_len;
1672 ssize_t slen;
1673
1674 u = treq->preq;
1675
1676 fr_assert((treq->state == TRUNK_REQUEST_STATE_PENDING) ||
1677 (treq->state == TRUNK_REQUEST_STATE_PARTIAL) ||
1678 ((u->retry.count > 0) && (treq->state == TRUNK_REQUEST_STATE_SENT)));
1679
1681
1682 /*
1683 * If it's a partial packet, then write the partial bit.
1684 */
1685 if (u->partial) {
1686 fr_assert(u->partial < u->packet_len);
1687 packet = u->packet + u->partial;
1688 packet_len = u->packet_len - u->partial;
1689 goto do_write;
1690 }
1691
1692 /*
1693 * No previous packet, OR can't retransmit the
1694 * existing one. Oh well.
1695 *
1696 * Note that if we can't retransmit the previous
1697 * packet, then u->rr MUST already have been
1698 * deleted in the request_cancel() function
1699 * or request_release_conn() function when
1700 * the REQUEUE signal was received.
1701 */
1702 if (!u->packet) {
1703 fr_assert(!u->rr);
1704
1705 if (unlikely(radius_track_entry_reserve(&u->rr, treq, h->tt, request, u->code, treq) < 0)) {
1706#ifndef NDEBUG
1707 radius_track_state_log(&default_log, L_ERR, __FILE__, __LINE__,
1709#endif
1710 fr_assert_fail("Tracking entry allocation failed: %s", fr_strerror());
1712 return;
1713 }
1714 fr_assert(u->rr);
1715 u->id = u->rr->id;
1716
1717 RDEBUG("Sending %s ID %d length %zu over connection %s",
1719
1720 if (encode(h, request, u, u->id) < 0) {
1721 /*
1722 * Need to do this because request_conn_release
1723 * may not be called.
1724 */
1727 return;
1728 }
1729 RHEXDUMP3(u->packet, u->packet_len, "Encoded packet");
1730
1731 /*
1732 * Remember the authentication vector, which now has the
1733 * packet signature.
1734 */
1736 } else {
1737 RDEBUG("Retransmitting %s ID %d length %zu over connection %s",
1739 }
1740
1741 /*
1742 * @todo - When logging Message-Authenticator, don't print its' value.
1743 */
1744 log_request_proto_pair_list(L_DBG_LVL_2, request, NULL, &request->request_pairs, NULL);
1745 if (!fr_pair_list_empty(&u->extra)) log_request_proto_pair_list(L_DBG_LVL_2, request, NULL, &u->extra, NULL);
1746
1747 packet = u->packet;
1748 packet_len = u->packet_len;
1749
1750do_write:
1751 fr_assert(packet != NULL);
1752 fr_assert(packet_len >= RADIUS_HEADER_LENGTH);
1753
1754 slen = fr_bio_write(h->bio.main, NULL, packet, packet_len);
1755
1756 /*
1757 * Can't write anything, requeue it on a different socket.
1758 */
1759 if (slen == fr_bio_error(IO_WOULD_BLOCK)) goto requeue;
1760
1761 if (slen < 0) {
1762 switch (errno) {
1763 /*
1764 * There is an error in the request.
1765 */
1766 case EMSGSIZE: /* Packet size exceeds max size allowed on socket */
1767 ERROR("%s - Failed sending data over connection %s: %s",
1768 h->ctx.module_name, h->ctx.fd_info->name, fr_syserror(errno));
1770 break;
1771
1772 /*
1773 * There is an error in the connection. The reconnection will re-queue any pending or
1774 * sent requests, so we don't have to do any cleanup.
1775 */
1776 default:
1777 ERROR("%s - Failed sending data over connection %s: %s",
1778 h->ctx.module_name, h->ctx.fd_info->name, fr_syserror(errno));
1780 break;
1781 }
1782
1783 return;
1784 }
1785
1786 /*
1787 * No data to send, ignore the write for partials, but otherwise requeue it.
1788 */
1789 if (slen == 0) {
1790 if (u->partial) return;
1791
1792 /*
1793 * We didn't get a "blocked" error, but we didn't write any data. And the socket is
1794 * unconnected. This means we just discard the packet.
1795 */
1797 ERROR("%s - Failed sending %s ID %d length %zu over connection %s: Destination network is down or unreachable",
1800 return;
1801 }
1802
1803 requeue:
1804 RWARN("%s - Failed sending data over connection %s: sent zero bytes",
1805 h->ctx.module_name, h->ctx.fd_info->name);
1807 return;
1808 }
1809
1810 packet_len += slen;
1811 if (packet_len < u->packet_len) {
1812 u->partial = packet_len;
1814 return;
1815 }
1816
1817 /*
1818 * For retransmissions.
1819 */
1820 u->partial = 0;
1821
1822 /*
1823 * Don't print anything extra for replication.
1824 */
1825 if (inst->mode == RLM_RADIUS_MODE_REPLICATE) {
1826 u->rcode = RLM_MODULE_OK;
1828 return;
1829 }
1830
1831 /*
1832 * On first packet, signal it as sent, and update stats.
1833 *
1834 * Later packets are just retransmissions to the BIO, and don't need to involve
1835 * the trunk code.
1836 */
1837 if (u->retry.count == 1) {
1838 h->last_sent = u->retry.start;
1840
1842
1843 action = u->proxied ? "Proxied" : "Originated";
1844
1845 } else {
1846 /*
1847 * We don't signal the trunk that it's been sent, it was already senty
1848 */
1849 action = "Retransmitted";
1850 }
1851
1853
1854 if (!u->proxied) {
1855 RDEBUG("%s request. Expecting response within %pVs", action,
1857
1858 } else {
1859 /*
1860 * If the packet doesn't get a response,
1861 * then bio_request_free() will notice, and run conn_zombie()
1862 */
1863 RDEBUG("%s request. Relying on NAS to perform more retransmissions", action);
1864 }
1865
1866 /*
1867 * We don't retransmit over TCP.
1868 */
1869 if (h->ctx.fd_config.socket_type != SOCK_DGRAM) return;
1870
1871 /*
1872 * If we only send one datagram packet, then don't bother saving it.
1873 */
1874 if (u->retry.config && u->retry.config->mrc == 1) {
1875 u->packet = NULL;
1876 return;
1877 }
1878
1879 MEM(u->packet = talloc_memdup(u, u->packet, u->packet_len));
1880}
1881
1882/** Deal with Protocol-Error replies, and possible negotiation
1883 *
1884 */
1886{
1887 bool error_601 = false;
1888 uint32_t response_length = 0;
1889 uint8_t const *attr, *end;
1890
1891 end = h->buffer + fr_nbo_to_uint16(h->buffer + 2);
1892
1893 for (attr = h->buffer + RADIUS_HEADER_LENGTH;
1894 attr < end;
1895 attr += attr[1]) {
1896 /*
1897 * Error-Cause = Response-Too-Big
1898 */
1899 if ((attr[0] == attr_error_cause->attr) && (attr[1] == 6)) {
1900 uint32_t error;
1901
1902 memcpy(&error, attr + 2, 4);
1903 error = ntohl(error);
1904 if (error == 601) error_601 = true;
1905 continue;
1906 }
1907
1908 /*
1909 * The other end wants us to increase our Response-Length
1910 */
1911 if ((attr[0] == attr_response_length->attr) && (attr[1] == 6)) {
1912 memcpy(&response_length, attr + 2, 4);
1913 continue;
1914 }
1915
1916 /*
1917 * Protocol-Error packets MUST contain an
1918 * Original-Packet-Code attribute.
1919 *
1920 * The attribute containing the
1921 * Original-Packet-Code is an extended
1922 * attribute.
1923 */
1924 if (attr[0] != attr_extended_attribute_1->attr) continue;
1925
1926 /*
1927 * ATTR + LEN + EXT-Attr + uint32
1928 */
1929 if (attr[1] != 7) continue;
1930
1931 /*
1932 * See if there's an Original-Packet-Code.
1933 */
1934 if (attr[2] != (uint8_t)attr_original_packet_code->attr) continue;
1935
1936 /*
1937 * Has to be an 8-bit number.
1938 */
1939 if ((attr[3] != 0) ||
1940 (attr[4] != 0) ||
1941 (attr[5] != 0)) {
1943 return;
1944 }
1945
1946 /*
1947 * The value has to match. We don't
1948 * currently multiplex different codes
1949 * with the same IDs on connections. So
1950 * this check is just for RFC compliance,
1951 * and for sanity.
1952 */
1953 if (attr[6] != u->code) {
1955 return;
1956 }
1957 }
1958
1959 /*
1960 * Error-Cause = Response-Too-Big
1961 *
1962 * The other end says it needs more room to send it's response
1963 *
1964 * Limit it to reasonable values.
1965 */
1966 if (error_601 && response_length && (response_length > h->buflen)) {
1967 if (response_length < 4096) response_length = 4096;
1968 if (response_length > 65535) response_length = 65535;
1969
1970 DEBUG("%s - Increasing buffer size to %u for connection %s", h->ctx.module_name, response_length, h->ctx.fd_info->name);
1971
1972 /*
1973 * Make sure to copy the packet over!
1974 */
1975 attr = h->buffer;
1976 h->buflen = response_length;
1977 MEM(h->buffer = talloc_array(h, uint8_t, h->buflen));
1978
1979 memcpy(h->buffer, attr, end - attr);
1980 }
1981
1982 /*
1983 * fail - something went wrong internally, or with the connection.
1984 * invalid - wrong response to packet
1985 * handled - best remaining alternative :(
1986 *
1987 * i.e. if the response is NOT accept, reject, whatever,
1988 * then we shouldn't allow the caller to do any more
1989 * processing of this packet. There was a protocol
1990 * error, and the response is valid, but not useful for
1991 * anything.
1992 */
1994}
1995
1996
1997/** Handle retries for a status check
1998 *
1999 */
2001{
2002 trunk_connection_t *tconn = talloc_get_type_abort(uctx, trunk_connection_t);
2003 bio_handle_t *h = talloc_get_type_abort(tconn->conn->h, bio_handle_t);
2004
2006 h->status_u, h->status_u, true) != TRUNK_ENQUEUE_OK) {
2008 }
2009}
2010
2011
2012/** Deal with replies replies to status checks and possible negotiation
2013 *
2014 */
2016{
2017 bio_handle_t *h = talloc_get_type_abort(treq->tconn->conn->h, bio_handle_t);
2018 rlm_radius_t const *inst = h->ctx.inst;
2019 bio_request_t *u = talloc_get_type_abort(treq->rctx, bio_request_t);
2020
2021 fr_assert(treq->preq == h->status_u);
2022 fr_assert(treq->rctx == h->status_u);
2023
2024 u->treq = NULL;
2025
2026 /*
2027 * @todo - do other negotiation and signaling.
2028 */
2030
2031 if (u->num_replies < inst->num_answers_to_alive) {
2032 DEBUG("Received %u / %u replies for status check, on connection - %s",
2033 u->num_replies, inst->num_answers_to_alive, h->ctx.fd_info->name);
2034 DEBUG("Next status check packet will be in %pVs", fr_box_time_delta(fr_time_sub(u->retry.next, now)));
2035
2036 /*
2037 * Set the timer for the next retransmit.
2038 */
2039 if (fr_timer_at(h, h->ctx.el->tl, &u->ev, u->retry.next, false, status_check_next, treq->tconn) < 0) {
2041 }
2042 return;
2043 }
2044
2045 DEBUG("Received enough replies to status check, marking connection as active - %s", h->ctx.fd_info->name);
2046
2047 /*
2048 * Set the "last idle" time to now, so that we don't
2049 * restart zombie_period until sufficient time has
2050 * passed.
2051 */
2052 h->last_idle = fr_time();
2053
2054 /*
2055 * Reset retry interval and retransmission counters
2056 * also frees u->ev.
2057 */
2058 status_check_reset(h, u);
2059 trunk_connection_signal_active(treq->tconn);
2060}
2061
2062CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
2064{
2065 bio_handle_t *h = talloc_get_type_abort(conn->h, bio_handle_t);
2066
2067 DEBUG3("%s - Reading data for connection %s", h->ctx.module_name, h->ctx.fd_info->name);
2068
2069 while (true) {
2070 ssize_t slen;
2071
2072 trunk_request_t *treq;
2073 request_t *request;
2074 bio_request_t *u;
2077 uint8_t code = 0;
2078 fr_pair_list_t reply;
2079 fr_pair_t *vp;
2080
2081 fr_time_t now;
2082
2083 fr_pair_list_init(&reply);
2084
2085 /*
2086 * Drain the socket of all packets. If we're busy, this
2087 * saves a round through the event loop. If we're not
2088 * busy, a few extra system calls don't matter.
2089 */
2090 slen = fr_bio_read(h->bio.main, NULL, h->buffer, h->buflen);
2091 if (slen == 0) {
2092 /*
2093 * @todo - set BIO FD EOF callback, so that we don't have to check it here.
2094 */
2096 return;
2097 }
2098
2099 /*
2100 * We're done reading, return.
2101 */
2102 if (slen == fr_bio_error(IO_WOULD_BLOCK)) return;
2103
2104 if (slen < 0) {
2105 ERROR("%s - Failed reading response from socket: %s",
2106 h->ctx.module_name, fr_syserror(errno));
2108 return;
2109 }
2110
2111 fr_assert(slen >= RADIUS_HEADER_LENGTH); /* checked in verify */
2112
2113 /*
2114 * Note that we don't care about packet codes. All
2115 * packet codes share the same ID space.
2116 */
2117 rr = radius_track_entry_find(h->tt, h->buffer[1], NULL);
2118 if (!rr) {
2119 WARN("%s - Ignoring reply with ID %i that arrived too late",
2120 h->ctx.module_name, h->buffer[1]);
2121 continue;
2122 }
2123
2124 treq = talloc_get_type_abort(rr->uctx, trunk_request_t);
2125 request = treq->request;
2126 fr_assert(request != NULL);
2127 u = talloc_get_type_abort(treq->rctx, bio_request_t);
2128 fr_assert(u == treq->preq);
2129
2130 /*
2131 * If we got a reply during the zombie period mark the
2132 * connection as active and remove the timer.
2133 */
2137 }
2138
2139 /*
2140 * Decode the incoming packet.
2141 */
2142 reason = decode(request->reply_ctx, &reply, &code, h, request, u, rr->vector, h->buffer, (size_t)slen);
2143 if (reason != FR_RADIUS_FAIL_NONE) continue;
2144
2145 /*
2146 * Only valid packets are processed
2147 * Otherwise an attacker could perform
2148 * a DoS attack against the proxying servers
2149 * by sending fake responses for upstream
2150 * servers.
2151 */
2152 h->last_reply = now = fr_time();
2153
2154 /*
2155 * Status-Server can have any reply code, we don't care
2156 * what it is. So long as it's signed properly, we
2157 * accept it. This flexibility is because we don't
2158 * expose Status-Server to the admins. It's only used by
2159 * this module for internal signalling.
2160 */
2161 if (u == h->status_u) {
2162 fr_pair_list_free(&reply); /* Probably want to pass this to status_check_reply? */
2163 status_check_reply(treq, now);
2165 continue;
2166 }
2167
2168 /*
2169 * Handle any state changes, etc. needed by receiving a
2170 * Protocol-Error reply packet.
2171 *
2172 * Protocol-Error is permitted as a reply to any
2173 * packet.
2174 */
2175 switch (code) {
2178
2179 vp = fr_pair_find_by_da(&request->reply_pairs, NULL, attr_original_packet_code);
2180 if (!vp) {
2181 RWDEBUG("Protocol-Error response is missing Original-Packet-Code");
2182 } else {
2183 fr_pair_delete_by_da(&request->reply_pairs, attr_original_packet_code);
2184 }
2185
2186 vp = fr_pair_find_by_da(&request->reply_pairs, NULL, attr_error_cause);
2187 if (!vp) {
2188 MEM(vp = fr_pair_afrom_da(request->reply_ctx, attr_error_cause));
2189 vp->vp_uint32 = FR_ERROR_CAUSE_VALUE_PROXY_PROCESSING_ERROR;
2190 fr_pair_append(&request->reply_pairs, vp);
2191 }
2192 break;
2193
2194 default:
2195 break;
2196 }
2197
2198 /*
2199 * Mark up the request as being an Access-Challenge, if
2200 * required.
2201 *
2202 * We don't do this for other packet types, because the
2203 * ok/fail nature of the module return code will
2204 * automatically result in it the parent request
2205 * returning an ok/fail packet code.
2206 */
2208 vp = fr_pair_find_by_da(&request->reply_pairs, NULL, attr_packet_type);
2209 if (!vp) {
2210 MEM(vp = fr_pair_afrom_da(request->reply_ctx, attr_packet_type));
2212 fr_pair_append(&request->reply_pairs, vp);
2213 }
2214
2215 /*
2216 * If the Access-Request got a challenge, AND the request doesn't have State, AND
2217 * the challenge does have State, THEN try to load-balance future packets to the
2218 * same destination.
2219 */
2220 if (h->ctx.inst->track_load_balance &&
2221 !fr_pair_find_by_da(&request->request_pairs, NULL, attr_state) &&
2222 fr_pair_find_by_da(&request->reply_pairs, NULL, attr_state)) {
2223 (void) unlang_load_balance_persist(request);
2224 }
2225 }
2226
2227 /*
2228 * Delete Proxy-State attributes from the reply.
2229 */
2231
2232 /*
2233 * If the reply has Message-Authenticator, then over-ride its value with all zeros, so
2234 * that we don't confuse anyone reading the debug output.
2235 */
2236 if ((vp = fr_pair_find_by_da(&reply, NULL, attr_message_authenticator)) != NULL) {
2237 (void) fr_pair_value_memdup(vp, (uint8_t const *) "", 1, false);
2238 }
2239
2240 treq->request->reply->code = code;
2241 u->rcode = radius_code_to_rcode[code];
2242 fr_pair_list_append(&request->reply_pairs, &reply);
2244 }
2245}
2246
2247/*
2248 * This is the same as request_mux(), except that we immediately mark the request as complete.
2249 */
2250CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
2252 trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
2253{
2254 bio_handle_t *h = talloc_get_type_abort(conn->h, bio_handle_t);
2255 trunk_request_t *treq;
2256
2257 if (unlikely(trunk_connection_pop_request(&treq, tconn) < 0)) return;
2258
2259 /*
2260 * No more requests to send
2261 */
2262 if (!treq) return;
2263
2264 mod_write(treq->request, treq, h);
2265}
2266
2267CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
2269{
2270 bio_handle_t *h = talloc_get_type_abort(conn->h, bio_handle_t);
2271
2272 DEBUG3("%s - Reading data for connection %s", h->ctx.module_name, h->ctx.fd_info->name);
2273
2274 while (true) {
2275 ssize_t slen;
2276
2277 trunk_request_t *treq;
2278 request_t *request;
2279 bio_request_t *u;
2282 uint8_t code = 0;
2283 fr_pair_list_t reply;
2284
2285 fr_time_t now;
2286
2287 fr_pair_list_init(&reply);
2288
2289 /*
2290 * Drain the socket of all packets. If we're busy, this
2291 * saves a round through the event loop. If we're not
2292 * busy, a few extra system calls don't matter.
2293 */
2294 slen = fr_bio_read(h->bio.main, NULL, h->buffer, h->buflen);
2295 if (slen == 0) {
2296 /*
2297 * @todo - set BIO FD EOF callback, so that we don't have to check it here.
2298 */
2300 return;
2301 }
2302
2303 /*
2304 * We're done reading, return.
2305 */
2306 if (slen == fr_bio_error(IO_WOULD_BLOCK)) return;
2307
2308 if (slen < 0) {
2309 ERROR("%s - Failed reading response from socket: %s",
2310 h->ctx.module_name, fr_syserror(errno));
2312 return;
2313 }
2314
2315 fr_assert(slen >= RADIUS_HEADER_LENGTH); /* checked in verify */
2316
2317 /*
2318 * We only pay attention to Protocol-Error replies.
2319 *
2320 * All other packets are discarded.
2321 */
2323 continue;
2324 }
2325
2326 /*
2327 * Note that we don't care about packet codes. All
2328 * packet codes share the same ID space.
2329 */
2330 rr = radius_track_entry_find(h->tt, h->buffer[1], NULL);
2331 if (!rr) {
2332 WARN("%s - Ignoring reply with ID %i that arrived too late",
2333 h->ctx.module_name, h->buffer[1]);
2334 continue;
2335 }
2336
2337 treq = talloc_get_type_abort(rr->uctx, trunk_request_t);
2338 request = treq->request;
2339 fr_assert(request != NULL);
2340 u = talloc_get_type_abort(treq->rctx, bio_request_t);
2341 fr_assert(u == treq->preq);
2342
2343 /*
2344 * Decode the incoming packet
2345 */
2346 reason = decode(request->reply_ctx, &reply, &code, h, request, u, rr->vector, h->buffer, (size_t)slen);
2347 if (reason != FR_RADIUS_FAIL_NONE) continue;
2348
2349 /*
2350 * Only valid packets are processed
2351 * Otherwise an attacker could perform
2352 * a DoS attack against the proxying servers
2353 * by sending fake responses for upstream
2354 * servers.
2355 */
2356 h->last_reply = now = fr_time();
2357
2358 /*
2359 * Status-Server can have any reply code, we don't care
2360 * what it is. So long as it's signed properly, we
2361 * accept it. This flexibility is because we don't
2362 * expose Status-Server to the admins. It's only used by
2363 * this module for internal signalling.
2364 */
2365 if (u == h->status_u) {
2366 fr_pair_list_free(&reply); /* Probably want to pass this to status_check_reply? */
2367 status_check_reply(treq, now);
2369 continue;
2370 }
2371
2372 /*
2373 * Handle any state changes, etc. needed by receiving a
2374 * Protocol-Error reply packet.
2375 *
2376 * Protocol-Error is also permitted as a reply to any
2377 * packet.
2378 */
2380 }
2381}
2382
2383
2384/** Remove the request from any tracking structures
2385 *
2386 * Frees encoded packets if the request is being moved to a new connection
2387 */
2388static void request_cancel(UNUSED connection_t *conn, void *preq_to_reset,
2389 trunk_cancel_reason_t reason, UNUSED void *uctx)
2390{
2391 bio_request_t *u = preq_to_reset;
2392
2393 /*
2394 * Request has been requeued on the same
2395 * connection due to timeout or DUP signal. We
2396 * keep the same packet to avoid re-encoding it.
2397 */
2398 if (reason == TRUNK_CANCEL_REASON_REQUEUE) {
2399 /*
2400 * Delete the request_timeout
2401 *
2402 * Note: There might not be a request timeout
2403 * set in the case where the request was
2404 * queued for sendmmsg but never actually
2405 * sent.
2406 */
2407 FR_TIMER_DISARM(u->ev);
2408 }
2409
2410 /*
2411 * Other cancellations are dealt with by
2412 * request_conn_release as the request is removed
2413 * from the trunk.
2414 */
2415}
2416
2417/** Clear out anything associated with the handle from the request
2418 *
2419 */
2420static void request_conn_release(connection_t *conn, void *preq_to_reset, UNUSED void *uctx)
2421{
2422 bio_request_t *u = preq_to_reset;
2423 bio_handle_t *h = talloc_get_type_abort(conn->h, bio_handle_t);
2424
2425 FR_TIMER_DISARM(u->ev);
2427
2428 if (h->ctx.inst->mode == RLM_RADIUS_MODE_REPLICATE) return;
2429
2430 u->num_replies = 0;
2431
2432 /*
2433 * If there are no outstanding tracking entries
2434 * allocated then the connection is "idle".
2435 */
2436 if (!h->tt || (h->tt->num_requests == 0)) h->last_idle = fr_time();
2437}
2438
2439/** Write out a canned failure
2440 *
2441 */
2442static void request_fail(request_t *request, NDEBUG_UNUSED void *preq, void *rctx,
2443 NDEBUG_UNUSED trunk_request_state_t state, UNUSED void *uctx)
2444{
2445 bio_request_t *u = talloc_get_type_abort(rctx, bio_request_t);
2446
2447 fr_assert(u == preq);
2448
2449 fr_assert(!u->rr && !u->packet && fr_pair_list_empty(&u->extra) && !u->ev); /* Dealt with by request_conn_release */
2450
2452
2453 if (u->status_check) return;
2454
2456 u->treq = NULL;
2457
2459}
2460
2461/** Response has already been written to the rctx at this point
2462 *
2463 */
2464static void request_complete(request_t *request, NDEBUG_UNUSED void *preq, void *rctx, UNUSED void *uctx)
2465{
2466 bio_request_t *u = talloc_get_type_abort(rctx, bio_request_t);
2467
2468 fr_assert(u == preq);
2469
2470 fr_assert(!u->rr && !u->packet && fr_pair_list_empty(&u->extra) && !u->ev); /* Dealt with by request_conn_release */
2471
2472 if (u->status_check) return;
2473
2474 u->treq = NULL;
2475
2477}
2478
2479/** Resume execution of the request, returning the rcode set during trunk execution
2480 *
2481 */
2483{
2484 bio_request_t *u = talloc_get_type_abort(mctx->rctx, bio_request_t);
2485 rlm_rcode_t rcode = u->rcode;
2486
2487 talloc_free(u);
2488
2489 RETURN_UNLANG_RCODE(rcode);
2490}
2491
2492static void do_signal(rlm_radius_t const *inst, bio_request_t *u, request_t *request, fr_signal_t action);
2493
2494static void mod_signal(module_ctx_t const *mctx, UNUSED request_t *request, fr_signal_t action)
2495{
2497
2498 bio_request_t *u = talloc_get_type_abort(mctx->rctx, bio_request_t);
2499
2500 do_signal(inst, u, request, action);
2501}
2502
2503static void do_signal(rlm_radius_t const *inst, bio_request_t *u, UNUSED request_t *request, fr_signal_t action)
2504{
2505 /*
2506 * We received a duplicate packet, but we're not doing
2507 * synchronous proxying. Ignore the dup, and rely on the
2508 * IO submodule to time it's own retransmissions.
2509 */
2510 if ((action == FR_SIGNAL_DUP) && (inst->mode != RLM_RADIUS_MODE_PROXY)) return;
2511
2512 /*
2513 * If we don't have a treq associated with the
2514 * rctx it's likely because the request was
2515 * scheduled, but hasn't yet been resumed, and
2516 * has received a signal, OR has been resumed
2517 * and immediately cancelled as the event loop
2518 * is exiting, in which case
2519 * unlang_request_is_scheduled will return false
2520 * (don't use it).
2521 */
2522 if (!u->treq) return;
2523
2524 switch (action) {
2525 /*
2526 * The request is being cancelled, tell the
2527 * trunk so it can clean up the treq.
2528 */
2529 case FR_SIGNAL_CANCEL:
2531 u->treq = NULL;
2532 return;
2533
2534 /*
2535 * Requeue the request on the same connection
2536 * causing a "retransmission" if the request
2537 * has already been sent out.
2538 */
2539 case FR_SIGNAL_DUP:
2540 mod_dup(request, u);
2541 return;
2542
2543 default:
2544 return;
2545 }
2546}
2547
2548/** Free a bio_request_t
2549 *
2550 * Allows us to set break points for debugging.
2551 */
2553{
2554 if (!u->treq) return 0;
2555
2556#ifndef NDEBUG
2557 {
2558 trunk_request_t *treq;
2559 treq = talloc_get_type_abort(u->treq, trunk_request_t);
2560 fr_assert(treq->preq == u);
2561 }
2562#endif
2563
2564 fr_assert_msg(!fr_timer_armed(u->ev), "bio_request_t freed with active timer");
2565
2567
2568 fr_assert(u->rr == NULL);
2569
2570 return 0;
2571}
2572
2573static int mod_enqueue(bio_request_t **p_u, fr_retry_config_t const **p_retry_config,
2574 rlm_radius_t const *inst, trunk_t *trunk, request_t *request)
2575{
2576 bio_request_t *u;
2577 trunk_request_t *treq;
2579
2580 fr_assert(request->packet->code > 0);
2581 fr_assert(request->packet->code < FR_RADIUS_CODE_MAX);
2582
2583 /*
2584 * Do any necessary RADIUS level fixups
2585 * - check Proxy-State
2586 * - do CHAP-Challenge fixups
2587 */
2588 if (radius_fixups(inst, request) < 0) return 0;
2589
2590 treq = trunk_request_alloc(trunk, request);
2591 if (!treq) {
2592 REDEBUG("Failed allocating handler for request");
2593 return -1;
2594 }
2595
2596 MEM(u = talloc_zero(request, bio_request_t));
2597 talloc_set_destructor(u, _bio_request_free);
2598
2599 /*
2600 * Can't use compound literal - const issues.
2601 */
2602 u->code = request->packet->code;
2603 u->priority = request->priority;
2604 u->recv_time = request->async->recv_time;
2606
2607 u->retry.count = 1;
2608
2610
2611 switch(trunk_request_enqueue(&treq, trunk, request, u, u)) {
2612 case TRUNK_ENQUEUE_OK:
2614 break;
2615
2617 REDEBUG("Unable to queue packet - connections at maximum capacity");
2618 fail:
2619 fr_assert(!u->rr && !u->packet); /* Should not have been fed to the muxer */
2620 trunk_request_free(&treq); /* Return to the free list */
2621 talloc_free(u);
2622 return -1;
2623
2625 REDEBUG("All destinations are down - cannot send packet");
2626 goto fail;
2627
2628 case TRUNK_ENQUEUE_FAIL:
2629 REDEBUG("Unable to queue packet");
2630 goto fail;
2631 }
2632
2633 u->treq = treq; /* Remember for signalling purposes */
2634 fr_assert(treq->rctx == u);
2635
2636 /*
2637 * Figure out if we're originating the packet or proxying it. And also figure out if we have to
2638 * retry.
2639 */
2640 switch (inst->mode) {
2642 case RLM_RADIUS_MODE_UNCONNECTED_REPLICATE: /* unconnected sockets are UDP, and bypass the trunk */
2643 REDEBUG("Internal sanity check failed - connection trunking cannot be used for replication");
2644 return -1;
2645
2646 /*
2647 * We originate this packet if it was taken from the detail module, which doesn't have a
2648 * real client. @todo - do a better check here.
2649 *
2650 * We originate this packet if the parent request is not compatible with this one
2651 * (i.e. it's from a different protocol).
2652 *
2653 * We originate the packet if the parent is from the same dictionary, but has a different
2654 * packet code. This lets us receive Accounting-Request, and originate
2655 * Disconnect-Request.
2656 */
2659 if (!request->parent) {
2660 u->proxied = (request->client && request->client->cs != NULL);
2661
2662 } else if (!fr_dict_compatible(request->parent->proto_dict, request->proto_dict)) {
2663 u->proxied = false;
2664
2665 } else {
2666 u->proxied = (request->parent->packet->code == request->packet->code);
2667 }
2668
2669 /*
2670 * Proxied packets get a final timeout, as we retry only on DUP packets.
2671 */
2672 if (u->proxied) goto timeout_retry;
2673
2675
2676 /*
2677 * Client packets (i.e. packets we originate) get retries for UDP. And no retries for TCP.
2678 */
2680 if (inst->fd_config.socket_type == SOCK_DGRAM) {
2681 retry_config = &inst->retry[u->code];
2682 break;
2683 }
2685
2686 /*
2687 * Replicated packets are never retried, but they have a timeout if the socket isn't
2688 * ready for writing.
2689 */
2691 timeout_retry:
2692 retry_config = &inst->timeout_retry;
2693 break;
2694
2695 default:
2696 fr_assert(0);
2697 return -1;
2698 }
2699
2700 /*
2701 * The event loop will take care of demux && sending the
2702 * packet, along with any retransmissions.
2703 */
2704 *p_u = u;
2705 *p_retry_config = retry_config;
2706
2707 return 1;
2708}
2709
2710static void home_server_free(void *data)
2711{
2712 home_server_t *home = data;
2713
2714 talloc_free(home);
2715}
2716
2719 .connection_notify = thread_conn_notify,
2720 .request_prioritise = request_prioritise,
2721 .request_mux = request_mux,
2722 .request_demux = request_demux,
2723 .request_conn_release = request_conn_release,
2724 .request_complete = request_complete,
2725 .request_fail = request_fail,
2726 .request_cancel = request_cancel,
2727};
2728
2731 .connection_notify = thread_conn_notify,
2732 .request_prioritise = request_prioritise,
2733 .request_mux = request_replicate_mux,
2734 .request_demux = request_replicate_demux,
2735 .request_conn_release = request_conn_release,
2736 .request_complete = request_complete,
2737 .request_fail = request_fail,
2738 .request_cancel = request_cancel,
2739};
2740
2741/** Instantiate thread data for the submodule.
2742 *
2743 */
2745{
2746 rlm_radius_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_radius_t);
2747 bio_thread_t *thread = talloc_get_type_abort(mctx->thread, bio_thread_t);
2748
2749 thread->ctx.el = mctx->el;
2750 thread->ctx.inst = inst;
2751 thread->ctx.fd_config = inst->fd_config;
2752 thread->ctx.radius_ctx = inst->common_ctx;
2753
2754 switch (inst->mode) {
2757 inst->home_server_lifetime);
2759
2760 default:
2761 /*
2762 * Assign each thread a portion of the available source port range.
2763 */
2764 if (thread->ctx.fd_config.src_port_start) {
2765 uint16_t range = inst->fd_config.src_port_end - inst->fd_config.src_port_start + 1;
2766 thread->num_ports = range / main_config->max_workers;
2767 thread->ctx.fd_config.src_port_start = inst->fd_config.src_port_start + (thread->num_ports * fr_schedule_worker_id());
2768 thread->ctx.fd_config.src_port_end = inst->fd_config.src_port_start + (thread->num_ports * (fr_schedule_worker_id() +1)) - 1;
2769 if (inst->mode != RLM_RADIUS_MODE_XLAT_PROXY) {
2770 thread->connections = talloc_zero_array(thread, connection_t *, thread->num_ports);
2772 }
2773 }
2774
2775 thread->ctx.trunk = trunk_alloc(thread, mctx->el, &io_funcs,
2776 &inst->trunk_conf, inst->name, thread, false, inst->trigger_args);
2777 if (!thread->ctx.trunk) return -1;
2778 return 0;
2779
2781 /*
2782 * We can replicate over TCP, but that uses trunks.
2783 */
2784 if (inst->fd_config.socket_type == SOCK_DGRAM) break;
2785
2786 thread->ctx.trunk = trunk_alloc(thread, mctx->el, &io_replicate_funcs,
2787 &inst->trunk_conf, inst->name, thread, false, inst->trigger_args);
2788 if (!thread->ctx.trunk) return -1;
2789 return 0;
2790
2792 break;
2793 }
2794
2795 /*
2796 * If we have a port range, allocate the source port based
2797 * on the range start, plus the thread ID. This means
2798 * that we can avoid "hunt and peck" attempts to open up
2799 * the source port.
2800 */
2801 if (thread->ctx.fd_config.src_port_start) {
2803 }
2804
2805 /*
2806 * Allocate an unconnected socket for replication.
2807 */
2808 thread->bio.fd = fr_bio_fd_alloc(thread, &thread->ctx.fd_config, 0);
2809 if (!thread->bio.fd) {
2810 PERROR("%s - failed opening socket", inst->name);
2811 return -1;
2812 }
2813
2814 thread->bio.fd->uctx = thread;
2815 thread->ctx.fd_info = fr_bio_fd_info(thread->bio.fd);
2816 fr_assert(thread->ctx.fd_info != NULL);
2817
2818 (void) fr_bio_fd_write_only(thread->bio.fd);
2819
2820 DEBUG("%s - Opened unconnected replication socket %s", inst->name, thread->ctx.fd_info->name);
2821 return 0;
2822}
2823
2825 { .required = true, .single = true, .type = FR_TYPE_COMBO_IP_ADDR },
2826 { .required = true, .single = true, .type = FR_TYPE_UINT16 },
2827 { .required = true, .single = true, .type = FR_TYPE_STRING },
2829};
2830
2831/*
2832 * %replicate.sendto.ipaddr(ipaddr, port, secret)
2833 */
2835 xlat_ctx_t const *xctx,
2836 request_t *request, fr_value_box_list_t *args)
2837{
2838 bio_thread_t *thread = talloc_get_type_abort(xctx->mctx->thread, bio_thread_t);
2839 fr_value_box_t *ipaddr, *port, *secret;
2840 ssize_t packet_len;
2841 uint8_t buffer[4096];
2842 fr_radius_ctx_t radius_ctx;
2845
2846 XLAT_ARGS(args, &ipaddr, &port, &secret);
2847
2848 /*
2849 * Can't change IP address families.
2850 */
2851 if (ipaddr->vb_ip.af != thread->ctx.fd_info->socket.af) {
2852 RPERROR("Invalid destination IP address family in %pV", ipaddr);
2853 return XLAT_ACTION_FAIL;
2854 }
2855
2856 /*
2857 * Warn if we're not replicating accounting data. It likely won't wokr/
2858 */
2859 if (request->packet->code != FR_RADIUS_CODE_ACCOUNTING_REQUEST) {
2860 RWDEBUG("Replication of packets other then Accounting-Request will likely not do what you want.");
2861 }
2862
2863 /*
2864 * Set up various context things.
2865 */
2866 radius_ctx = (fr_radius_ctx_t) {
2867 .secret = secret->vb_strvalue,
2868 .secret_length = secret->vb_length,
2869 .proxy_state = 0,
2870 };
2871
2873 .common = &radius_ctx,
2874 .rand_ctx = (fr_fast_rand_t) {
2875 .a = fr_rand(),
2876 .b = fr_rand(),
2877 },
2878 .code = request->packet->code,
2879 .id = thread->bio.id++ & 0xff,
2880 .add_proxy_state = false,
2881 };
2882
2883 /*
2884 * Encode the entire packet.
2885 */
2886 packet_len = fr_radius_encode(&FR_DBUFF_TMP(buffer, sizeof(buffer)),
2887 &request->request_pairs, &encode_ctx);
2888 if (packet_len < 0) {
2889 RPERROR("Failed encoding replicated packet");
2890 return XLAT_ACTION_FAIL;
2891 }
2892
2893 /*
2894 * Sign it.
2895 */
2896 if (fr_radius_sign(buffer, NULL, (uint8_t const *) radius_ctx.secret, radius_ctx.secret_length) < 0) {
2897 RPERROR("Failed signing replicated packet");
2898 return XLAT_ACTION_FAIL;
2899 }
2900
2901 /*
2902 * Prepare destination address.
2903 */
2904 addr = (fr_bio_fd_packet_ctx_t) {
2905 .socket = thread->ctx.fd_info->socket,
2906 };
2907 addr.socket.inet.dst_ipaddr = ipaddr->vb_ip;
2908 addr.socket.inet.dst_port = port->vb_uint16;
2909
2910 RDEBUG("Replicating packet to %pV:%u", ipaddr, port->vb_uint16);
2911
2912 /*
2913 * We either send it, or fail.
2914 */
2915 packet_len = fr_bio_write(thread->bio.fd, &addr, buffer, packet_len);
2916 if (packet_len < 0) {
2917 RPERROR("Failed replicating packet to %pV:%u", ipaddr, port->vb_uint16);
2918 return XLAT_ACTION_FAIL;
2919 }
2920
2921 /*
2922 * No return value.
2923 */
2924 return XLAT_ACTION_DONE;
2925}
2926
2927// **********************************************************************
2928
2929/** Dynamic home server code
2930 *
2931 */
2932
2933static fr_cmp_ret_t home_server_cmp(void const *one, void const *two)
2934{
2935 home_server_t const *a = one;
2936 home_server_t const *b = two;
2937 fr_cmp_ret_t rcode;
2938
2940 if (rcode != 0) return rcode;
2941
2943}
2944
2946 xlat_ctx_t const *xctx,
2947 request_t *request, UNUSED fr_value_box_list_t *in)
2948{
2949 bio_request_t *u = talloc_get_type_abort(xctx->rctx, bio_request_t);
2950 fr_value_box_t *dst;
2951
2952 if (u->rcode == RLM_MODULE_FAIL) return XLAT_ACTION_FAIL;
2953
2955 dst->vb_uint32 = request->reply->code;
2956
2957 fr_dcursor_append(out, dst);
2958
2959 return XLAT_ACTION_DONE;
2960}
2961
2962static void xlat_sendto_signal(xlat_ctx_t const *xctx, request_t *request, fr_signal_t action)
2963{
2964 rlm_radius_t const *inst = talloc_get_type_abort(xctx->mctx->mi->data, rlm_radius_t);
2965 bio_request_t *u = talloc_get_type_abort(xctx->rctx, bio_request_t);
2966
2967 do_signal(inst, u, request, action);
2968}
2969
2970/*
2971 * @todo - change this to mod_retry
2972 */
2973static void xlat_sendto_retry(xlat_ctx_t const *xctx, request_t *request, fr_retry_t const *retry)
2974{
2975 rlm_radius_t const *inst = talloc_get_type_abort(xctx->mctx->mi->data, rlm_radius_t);
2976 bio_request_t *u = talloc_get_type_abort(xctx->rctx, bio_request_t);
2977
2978 do_retry(inst, u, request, retry);
2979}
2980
2981/*
2982 * %proxy.sendto.ipaddr(ipaddr, port, secret)
2983 */
2985 xlat_ctx_t const *xctx,
2986 request_t *request, fr_value_box_list_t *args)
2987{
2988 rlm_radius_t const *inst = talloc_get_type_abort(xctx->mctx->mi->data, rlm_radius_t);
2989 bio_thread_t *thread = talloc_get_type_abort(xctx->mctx->thread, bio_thread_t);
2990 fr_value_box_t *ipaddr, *port, *secret;
2991 home_server_t *home;
2992 bio_request_t *u = NULL;
2993 fr_retry_config_t const *retry_config = NULL;
2994 int rcode;
2995
2996 XLAT_ARGS(args, &ipaddr, &port, &secret);
2997
2998 /*
2999 * Can't change IP address families.
3000 */
3001 if (ipaddr->vb_ip.af != thread->ctx.fd_config.src_ipaddr.af) {
3002 RDEBUG("Invalid destination IP address family in %pV", ipaddr);
3003 return XLAT_ACTION_DONE;
3004 }
3005
3006 fr_rb_find((void **)&home, &thread->bio.expires.tree, &(home_server_t) {
3007 .ctx = {
3008 .fd_config = (fr_bio_fd_config_t) {
3009 .dst_ipaddr = ipaddr->vb_ip,
3010 .dst_port = port->vb_uint16,
3011 },
3012 },
3013 });
3014 if (!home) {
3015 /*
3016 * Track which connections are made to this home server from which open ports.
3017 */
3018 MEM(home = (home_server_t *) talloc_zero_array(thread, uint8_t, sizeof(home_server_t) + sizeof(connection_t *) * thread->num_ports));
3019 talloc_set_type(home, home_server_t);
3020
3021 *home = (home_server_t) {
3022 .ctx = (bio_handle_ctx_t) {
3023 .el = unlang_interpret_event_list(request),
3024 .module_name = inst->name,
3025 .inst = inst,
3026 .limit_source_ports = (thread->num_ports > 0) ? LIMIT_PORTS_DYNAMIC : LIMIT_PORTS_NONE,
3027 .dynamic = true,
3028 },
3029 .num_ports = thread->num_ports,
3030 };
3031
3032 /*
3033 * Copy the home server configuration from the thread configuration. Then update it with
3034 * the needs of the home server.
3035 */
3036 home->ctx.fd_config = thread->ctx.fd_config;
3037 home->ctx.fd_config.type = FR_BIO_FD_CONNECTED;
3038 home->ctx.fd_config.dst_ipaddr = ipaddr->vb_ip;
3039 home->ctx.fd_config.dst_port = port->vb_uint32;
3040
3041 home->ctx.radius_ctx = (fr_radius_ctx_t) {
3042 .secret = talloc_strdup(home, secret->vb_strvalue),
3043 .secret_length = secret->vb_length,
3044 .proxy_state = inst->common_ctx.proxy_state,
3045 };
3046
3047 /*
3048 * Allocate the trunk and start it up.
3049 */
3050 home->ctx.trunk = trunk_alloc(home, unlang_interpret_event_list(request), &io_funcs,
3051 &inst->trunk_conf, inst->name, home, false, inst->trigger_args);
3052 if (!home->ctx.trunk) {
3053 fail:
3054 talloc_free(home);
3055 return XLAT_ACTION_FAIL;
3056 }
3057
3058 if (!fr_rb_expire_insert(&thread->bio.expires, home, fr_time())) goto fail;
3059 } else {
3060 fr_rb_expire_t *expire = &thread->bio.expires;
3061 fr_time_t now = fr_time();
3062 home_server_t *old;
3063
3064 /*
3065 * We can't change secrets on the fly. The home
3066 * server has to expire first, and then the
3067 * secret can be changed.
3068 */
3069 if ((home->ctx.radius_ctx.secret_length != secret->vb_length) ||
3070 (strcmp(home->ctx.radius_ctx.secret, secret->vb_strvalue) != 0)) {
3071 RWDEBUG("The new secret is not the same as the old secret: Ignoring the new one");
3072 }
3073
3074 fr_rb_expire_update(expire, home, now);
3075
3076 while ((old = fr_dlist_head(&expire->head)) != NULL) {
3077 (void) talloc_get_type_abort(old, home_server_t);
3078
3079 fr_assert(old->ctx.trunk);
3080
3081 /*
3082 * Don't delete the home server we're about to use.
3083 */
3084 if (old == home) break;
3085
3086 /*
3087 * It still has a request allocated, do nothing.
3088 */
3089 if (old->ctx.trunk->req_alloc) break;
3090
3091 /*
3092 * Not yet time to expire.
3093 */
3094 if (fr_time_gt(old->expire.when, now)) break;
3095
3096 fr_dlist_remove(&expire->head, old);
3097 fr_rb_delete(&expire->tree, old);
3098 }
3099 }
3100
3101 /*
3102 * Enqueue the packet on the per-home-server trunk.
3103 */
3104 rcode = mod_enqueue(&u, &retry_config, inst, home->ctx.trunk, request);
3105 if (rcode == 0) return XLAT_ACTION_DONE;
3106
3107 if (rcode < 0) {
3108 REDEBUG("Failed enqueuing packet");
3109 return XLAT_ACTION_FAIL;
3110 }
3111 fr_assert(u != NULL);
3112 fr_assert(retry_config != NULL);
3113
3114 /*
3115 * Start the retry.
3116 *
3117 * @todo - change unlang_xlat_timeout_add() to unlang_xlat_retry_add().
3118 */
3119 fr_retry_init(&u->retry, fr_time(), retry_config);
3120
3123 u, retry_config);
3124}
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition action.h:35
static int const char char buffer[256]
Definition acutest.h:576
int const char * file
Definition acutest.h:702
va_list args
Definition acutest.h:770
int const char int line
Definition acutest.h:702
static ssize_t fr_bio_write(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size)
Write raw data to a bio.
Definition base.h:184
static ssize_t fr_bio_read(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
Read raw data from a bio.
Definition base.h:161
void * uctx
user ctx, caller can manually set it.
Definition base.h:114
#define fr_bio_error(_x)
Definition base.h:200
#define NDEBUG_UNUSED
Definition build.h:395
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:391
#define CC_NO_UBSAN(_sanitize)
Definition build.h:503
#define CMP_PREFER_LARGER(_a, _b)
Evaluates to -1 for a > b, and +1 for a < b.
Definition build.h:109
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
Definition build.h:113
#define unlikely(_x)
Definition build.h:455
#define UNUSED
Definition build.h:384
connection_state_t
Definition connection.h:47
@ CONNECTION_STATE_FAILED
Connection has failed.
Definition connection.h:56
@ CONNECTION_STATE_CONNECTED
File descriptor is open (ready for writing).
Definition connection.h:54
@ CONNECTION_STATE_INIT
Init state, sets up connection.
Definition connection.h:51
@ CONNECTION_STATE_CONNECTING
Waiting for connection to establish.
Definition connection.h:52
@ CONNECTION_FAILED
Connection is being reconnected because it failed.
Definition connection.h:89
Holds a complete set of functions for a connection.
Definition connection.h:199
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
Definition dbuff.h:522
static int fr_dcursor_append(fr_dcursor_t *cursor, void *v)
Insert a single item at the end of the list.
Definition dcursor.h:406
#define fr_assert_msg(_x, _msg,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:202
#define fr_assert_fail(_msg,...)
Calls panic_action ifndef NDEBUG, else logs error.
Definition debug.h:208
#define MEM(x)
Definition debug.h:36
@ FR_RADIUS_CODE_ACCESS_CHALLENGE
RFC2865 - Access-Challenge.
Definition defs.h:43
@ FR_RADIUS_CODE_ACCESS_REQUEST
RFC2865 - Access-Request.
Definition defs.h:33
@ FR_RADIUS_CODE_MAX
Maximum possible protocol code.
Definition defs.h:53
@ FR_RADIUS_CODE_DISCONNECT_ACK
RFC3575/RFC5176 - Disconnect-Ack (positive)
Definition defs.h:47
@ FR_RADIUS_CODE_ACCESS_ACCEPT
RFC2865 - Access-Accept.
Definition defs.h:34
@ FR_RADIUS_CODE_ACCOUNTING_RESPONSE
RFC2866 - Accounting-Response.
Definition defs.h:37
@ FR_RADIUS_CODE_COA_NAK
RFC3575/RFC5176 - CoA-Nak (not willing to perform)
Definition defs.h:51
@ FR_RADIUS_CODE_COA_ACK
RFC3575/RFC5176 - CoA-Ack (positive)
Definition defs.h:50
@ FR_RADIUS_CODE_DISCONNECT_NAK
RFC3575/RFC5176 - Disconnect-Nak (not willing to perform)
Definition defs.h:48
@ FR_RADIUS_CODE_PROTOCOL_ERROR
RFC7930 - Protocol-Error (generic NAK)
Definition defs.h:52
@ FR_RADIUS_CODE_ACCOUNTING_REQUEST
RFC2866 - Accounting-Request.
Definition defs.h:36
@ FR_RADIUS_CODE_ACCESS_REJECT
RFC2865 - Access-Reject.
Definition defs.h:35
static fr_dict_attr_t const * attr_packet_type
Definition dhcpclient.c:88
#define ERROR(fmt,...)
Definition dhcpclient.c:40
int main(int argc, char **argv)
Definition dhcpclient.c:530
#define DEBUG(fmt,...)
Definition dhcpclient.c:38
bool fr_dict_compatible(fr_dict_t const *dict1, fr_dict_t const *dict2)
See if two dictionaries have the same end parent.
Definition dict_util.c:2867
static fr_slen_t in
Definition dict.h:882
static void * fr_dlist_head(fr_dlist_head_t const *list_head)
Return the HEAD item of a list or NULL if the list is empty.
Definition dlist.h:468
static void * fr_dlist_remove(fr_dlist_head_t *list_head, void *ptr)
Remove an item from the list.
Definition dlist.h:620
#define fr_event_fd_insert(...)
Definition event.h:247
void(* fr_event_fd_cb_t)(fr_event_list_t *el, int fd, int flags, void *uctx)
Called when an IO event occurs on a file descriptor.
Definition event.h:150
int fr_bio_fd_connect_full(fr_bio_t *bio, fr_event_list_t *el, fr_bio_callback_t connected_cb, fr_bio_callback_t error_cb, fr_time_delta_t *timeout, fr_bio_callback_t timeout_cb)
Finalize a connect()
Definition fd.c:1198
fr_bio_t * fr_bio_fd_alloc(TALLOC_CTX *ctx, fr_bio_fd_config_t const *cfg, size_t offset)
Allocate a FD bio.
Definition fd.c:971
fr_bio_fd_info_t const * fr_bio_fd_info(fr_bio_t *bio)
Returns a pointer to the bio-specific information.
Definition fd.c:1295
int fr_bio_fd_write_only(fr_bio_t *bio)
Mark up a bio as write-only.
Definition fd.c:1342
fr_socket_t socket
as connected socket
Definition fd.h:127
char const * name
printable name of this BIO
Definition fd.h:132
uint16_t src_port
our port
Definition fd.h:86
bool eof
are we at EOF?
Definition fd.h:136
@ FR_BIO_FD_CONNECTED
connected client sockets (UDP or TCP)
Definition fd.h:63
@ FR_BIO_FD_UNCONNECTED
unconnected UDP / datagram only
Definition fd.h:60
fr_bio_fd_type_t type
type of the socket
Definition fd.h:129
fr_bio_fd_state_t state
connecting, open, closed, etc.
Definition fd.h:130
uint16_t src_port_start
limit source port ranges for client BIOs
Definition fd.h:89
@ FR_BIO_FD_STATE_CONNECTING
Definition fd.h:55
@ FR_BIO_FD_STATE_OPEN
error states must be before this
Definition fd.h:54
int connect_errno
from connect() or other APIs
Definition fd.h:138
fr_ipaddr_t dst_ipaddr
their IP address
Definition fd.h:84
uint16_t src_port_end
limit source port ranges for client BIOs
Definition fd.h:90
int socket_type
SOCK_STREAM or SOCK_DGRAM.
Definition fd.h:78
uint16_t dst_port
their port
Definition fd.h:87
fr_socket_t socket
socket information, including FD.
Definition fd.h:47
bool write_blocked
did we block on write?
Definition fd.h:135
fr_ipaddr_t src_ipaddr
our IP address
Definition fd.h:83
Configuration for sockets.
Definition fd.h:75
Run-time status of the socket.
Definition fd.h:126
Per-packet context.
Definition fd.h:46
talloc_free(hp)
fr_cmp_ret_t fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b)
Compare two ip addresses.
Definition inet.c:1353
int af
Address family.
Definition inet.h:64
void unlang_interpret_mark_runnable(request_t *request)
Mark a request as resumable.
Definition interpret.c:2007
unlang_t const * unlang_interpret_instruction(request_t *request)
Get the current instruction.
Definition interpret.c:327
int unlang_interpret_force_result(unlang_t const *instruction, unlang_result_t *p_result, fr_timer_list_t *tl, fr_time_delta_t expire)
Set (or clear) a forced result.
Definition interpret.c:350
fr_event_list_t * unlang_interpret_event_list(request_t *request)
Get the event list for the current interpreter.
Definition interpret.c:2493
HIDDEN fr_dict_attr_t const * attr_eap_message
Definition base.c:94
void log_request_proto_pair_list(fr_log_lvl_t lvl, request_t *request, fr_pair_t const *parent, fr_pair_list_t const *vps, char const *prefix)
Print a list of protocol fr_pair_ts.
Definition log.c:902
void log_request_pair_list(fr_log_lvl_t lvl, request_t *request, fr_pair_t const *parent, fr_pair_list_t const *vps, char const *prefix)
Print a fr_pair_list_t.
Definition log.c:828
#define PERROR(_fmt,...)
Definition log.h:233
#define DEBUG3(_fmt,...)
Definition log.h:271
#define RWDEBUG(fmt,...)
Definition log.h:378
#define RWARN(fmt,...)
Definition log.h:314
#define DEBUG4(_fmt,...)
Definition log.h:272
#define RPERROR(fmt,...)
Definition log.h:319
#define RINFO(fmt,...)
Definition log.h:313
#define RPEDEBUG(fmt,...)
Definition log.h:393
#define HEXDUMP3(_data, _len, _fmt,...)
Definition log.h:740
#define RHEXDUMP3(_data, _len, _fmt,...)
Definition log.h:722
int map_to_vp(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, map_t const *map, UNUSED void *uctx)
Convert a map to a fr_pair_t.
Definition map.c:1604
int map_to_request(request_t *request, map_t const *map, radius_map_getvalue_t func, void *ctx)
Convert map_t to fr_pair_t (s) and add them to a request_t.
Definition map.c:1884
#define fr_time()
Definition event.c:60
Stores all information relating to an event list.
Definition event.c:377
fr_log_t default_log
Definition log.c:306
#define fr_log(_log, _lvl, _file, _line, _fmt,...)
Definition log.h:172
@ L_DBG_LVL_3
3rd highest priority debug messages (-xxx | -Xx).
Definition log.h:69
@ L_DBG_LVL_2
2nd highest priority debug messages (-xx | -X).
Definition log.h:68
fr_log_type_t
Definition log.h:51
@ L_ERR
Error message.
Definition log.h:53
fr_packet_t * fr_packet_alloc(TALLOC_CTX *ctx, bool new_vector)
Allocate a new fr_packet_t.
Definition packet.c:38
Minimal data structure to use the new code.
Definition listen.h:63
int unlang_load_balance_persist(request_t *request)
Persist the current load-balance selection.
main_config_t const * main_config
Main server configuration.
Definition main_config.c:56
uint32_t max_workers
for the scheduler
fr_bio_t * fr_bio_mem_alloc(TALLOC_CTX *ctx, size_t read_size, size_t write_size, fr_bio_t *next)
Allocate a memory buffer bio.
Definition mem.c:729
int fr_bio_mem_set_verify(fr_bio_t *bio, fr_bio_verify_t verify, void *verify_ctx, bool datagram)
Set the verification function for memory bios.
Definition mem.c:906
fr_bio_verify_action_t
Status returned by the verification callback.
Definition mem.h:32
@ FR_BIO_VERIFY_ERROR_CLOSE
fatal error, the bio should be closed.
Definition mem.h:36
@ FR_BIO_VERIFY_DISCARD
the packet should be discarded
Definition mem.h:34
@ FR_BIO_VERIFY_OK
packet is OK
Definition mem.h:33
@ FR_BIO_VERIFY_WANT_MORE
not enough data for one packet
Definition mem.h:35
bool fr_radius_ok(uint8_t const *packet, size_t *packet_len_p, uint32_t max_attributes, bool require_message_authenticator, decode_fail_t *reason)
unsigned short uint16_t
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_UINT16
16 Bit unsigned integer.
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_COMBO_IP_ADDR
IPv4 or IPv6 address depending on length.
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
fr_cmp_ret_t
Result of an ordering comparison.
Definition misc.h:50
module_instance_t const * mi
Instance of the module being instantiated.
Definition module_ctx.h:42
void * thread
Thread specific instance data.
Definition module_ctx.h:43
void * rctx
Resume ctx that a module previously set.
Definition module_ctx.h:45
fr_event_list_t * el
Event list to register any IO handlers and timers against.
Definition module_ctx.h:68
void * thread
Thread instance data.
Definition module_ctx.h:67
module_instance_t const * mi
Instance of the module being instantiated.
Definition module_ctx.h:64
Temporary structure to hold arguments for module calls.
Definition module_ctx.h:41
Temporary structure to hold arguments for thread_instantiation calls.
Definition module_ctx.h:63
static int mod_enqueue(bio_request_t **p_u, fr_retry_config_t const **p_retry_config, rlm_radius_t const *inst, trunk_t *trunk, request_t *request)
Definition bio.c:2573
fr_bio_fd_config_t fd_config
for threads or sockets
Definition bio.c:49
int num_ports
Definition bio.c:146
static void do_retry(rlm_radius_t const *inst, bio_request_t *u, request_t *request, fr_retry_t const *retry)
Definition bio.c:1563
fr_timer_t * ev
timer for retransmissions
Definition bio.c:137
static void request_replicate_mux(UNUSED fr_event_list_t *el, trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
Definition bio.c:2251
static void conn_init_error(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, int fd_errno, void *uctx)
Connection errored.
Definition bio.c:344
static void request_demux(UNUSED fr_event_list_t *el, trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
Definition bio.c:2063
static void conn_discard(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
Read and discard data.
Definition bio.c:1042
uint8_t id
Last ID assigned to this packet.
Definition bio.c:131
static void request_replicate_demux(UNUSED fr_event_list_t *el, trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
Definition bio.c:2268
uint32_t max_packet_size
Our max packet size. may be different from the parent.
Definition bio.c:90
static void do_signal(rlm_radius_t const *inst, bio_request_t *u, request_t *request, fr_signal_t action)
bool dynamic
is this a dynamic home server.
Definition bio.c:53
static void bio_connected(fr_bio_t *bio)
Definition bio.c:635
uint32_t num_replies
number of reply packets, sent is in retry.count
Definition bio.c:123
static const trunk_io_funcs_t io_funcs
Definition bio.c:2717
static void conn_close(UNUSED fr_event_list_t *el, void *handle, void *uctx)
Shutdown/close a file descriptor.
Definition bio.c:916
bio_limit_ports_t
Definition bio.c:38
@ LIMIT_PORTS_DYNAMIC
Limited source ports for dynamic home servers.
Definition bio.c:41
@ LIMIT_PORTS_NONE
Source port not restricted.
Definition bio.c:39
@ LIMIT_PORTS_STATIC
Limited source ports for static home servers.
Definition bio.c:40
uint32_t priority
copied from request->async->priority
Definition bio.c:120
static rlm_rcode_t radius_code_to_rcode[FR_RADIUS_CODE_MAX]
Turn a reply code into a module rcode;.
Definition bio.c:154
unlang_t const * instruction
Instruction which triggered the start of the zombie period.
Definition bio.c:104
static void conn_error(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, int fd_errno, void *uctx)
Connection errored.
Definition bio.c:1077
char const * module_name
the module that opened the connection
Definition bio.c:45
fr_bio_fd_info_t const * fd_info
status of the FD.
Definition bio.c:50
connection_t * connections[]
for tracking outbound connections
Definition bio.c:147
static void mod_signal(module_ctx_t const *mctx, UNUSED request_t *request, fr_signal_t action)
Definition bio.c:2494
struct bio_handle_t::@200 bio
uint8_t last_id
Used when replicating to ensure IDs are distributed evenly.
Definition bio.c:87
struct bio_thread_t::@199 bio
static void mod_retry(module_ctx_t const *mctx, request_t *request, fr_retry_t const *retry)
Handle module retries.
Definition bio.c:1555
static fr_radius_decode_fail_t decode(TALLOC_CTX *ctx, fr_pair_list_t *reply, uint8_t *response_code, bio_handle_t *h, request_t *request, bio_request_t *u, uint8_t const request_authenticator[static RADIUS_AUTH_VECTOR_LENGTH], uint8_t *data, size_t data_len)
Decode response packet data, extracting relevant information and validating the packet.
Definition bio.c:1197
static void thread_conn_notify(trunk_connection_t *tconn, connection_t *conn, fr_event_list_t *el, trunk_connection_event_t notify_on, UNUSED void *uctx)
Definition bio.c:1089
static int encode(bio_handle_t *h, request_t *request, bio_request_t *u, uint8_t id)
Definition bio.c:1264
fr_time_t last_reply
When we last received a reply.
Definition bio.c:98
static void request_conn_release(connection_t *conn, void *preq_to_reset, UNUSED void *uctx)
Clear out anything associated with the handle from the request.
Definition bio.c:2420
int fd
File descriptor.
Definition bio.c:77
bio_request_t * status_u
for sending status check packets
Definition bio.c:107
bio_handle_ctx_t ctx
common struct for home servers and BIO handles
Definition bio.c:57
static fr_bio_verify_action_t rlm_radius_verify(UNUSED fr_bio_t *bio, void *verify_ctx, UNUSED void *packet_ctx, const void *data, size_t *size)
Definition bio.c:654
static void mod_write(request_t *request, trunk_request_t *treq, bio_handle_t *h)
Definition bio.c:1665
#define REQUIRE_MA(_h)
fr_pair_list_t extra
VPs for debugging, like Proxy-State.
Definition bio.c:128
bool proxied
is this request being proxied
Definition bio.c:126
size_t buflen
Receive buffer length.
Definition bio.c:93
static void conn_init_readable(fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
Read the connection during the init and negotiation stage.
Definition bio.c:427
fr_time_t last_idle
last time we had nothing to do
Definition bio.c:101
static fr_cmp_ret_t request_prioritise(void const *one, void const *two)
Definition bio.c:1158
static void bio_tracking_entry_log(fr_log_t const *log, fr_log_type_t log_type, char const *file, int line, radius_track_entry_t *te)
Log additional information about a tracking entry.
Definition bio.c:197
static xlat_action_t xlat_radius_replicate(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Definition bio.c:2834
radius_track_entry_t * rr
ID tracking, resend count, etc.
Definition bio.c:136
size_t packet_len
Length of the packet.
Definition bio.c:133
static int _bio_handle_free(bio_handle_t *h)
Free a connection handle, closing associated resources.
Definition bio.c:618
size_t partial
partially sent data
Definition bio.c:134
fr_event_list_t * el
Event list.
Definition bio.c:47
static void request_cancel(UNUSED connection_t *conn, void *preq_to_reset, trunk_cancel_reason_t reason, UNUSED void *uctx)
Remove the request from any tracking structures.
Definition bio.c:2388
static void home_server_free(void *data)
Definition bio.c:2710
int num_ports
Definition bio.c:65
static void protocol_error_reply(bio_request_t *u, bio_handle_t *h)
Deal with Protocol-Error replies, and possible negotiation.
Definition bio.c:1885
static void status_check_reset(bio_handle_t *h, bio_request_t *u)
Reset a status_check packet, ready to reuse.
Definition bio.c:233
static const trunk_io_funcs_t io_replicate_funcs
Definition bio.c:2729
static void request_fail(request_t *request, NDEBUG_UNUSED void *preq, void *rctx, NDEBUG_UNUSED trunk_request_state_t state, UNUSED void *uctx)
Write out a canned failure.
Definition bio.c:2442
fr_time_t recv_time
copied from request->async->recv_time
Definition bio.c:121
fr_retry_t retry
retransmission timers
Definition bio.c:138
static void conn_init_writable(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
static xlat_action_t xlat_radius_client(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Definition bio.c:2984
rlm_rcode_t rcode
from the transport
Definition bio.c:117
static void status_check_alloc(bio_handle_t *h)
Definition bio.c:250
fr_time_t mrs_time
Most recent sent time which had a reply.
Definition bio.c:97
bool status_checking
whether we're doing status checks
Definition bio.c:106
trunk_t * trunk
trunk handler
Definition bio.c:48
fr_time_t last_sent
last time we sent a packet.
Definition bio.c:100
static void zombie_timeout(fr_timer_list_t *tl, fr_time_t now, void *uctx)
Mark a connection dead after "zombie_interval".
Definition bio.c:1373
bio_handle_ctx_t ctx
for copying to bio_handle_t
Definition bio.c:142
static int mod_thread_instantiate(module_thread_inst_ctx_t const *mctx)
Instantiate thread data for the submodule.
Definition bio.c:2744
static void bio_error(fr_bio_t *bio)
Definition bio.c:644
static void status_check_reply(trunk_request_t *treq, fr_time_t now)
Deal with replies replies to status checks and possible negotiation.
Definition bio.c:2015
static bool check_for_zombie(request_t *request, trunk_connection_t *tconn, fr_time_t now, fr_time_t last_sent)
See if the connection is zombied.
Definition bio.c:1446
static void conn_init_next(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
Perform the next step of init and negotiation.
Definition bio.c:413
bool is_retry
Definition bio.c:118
static int _bio_request_free(bio_request_t *u)
Free a bio_request_t.
Definition bio.c:2552
connection_t * conn
Definition bio.c:85
fr_time_t first_sent
first time we sent a packet since going idle
Definition bio.c:99
static unlang_action_t mod_resume(unlang_result_t *p_result, module_ctx_t const *mctx, UNUSED request_t *request)
Resume execution of the request, returning the rcode set during trunk execution.
Definition bio.c:2482
fr_timer_t * zombie_ev
Zombie timeout.
Definition bio.c:103
static connection_t * thread_conn_alloc(trunk_connection_t *tconn, fr_event_list_t *el, connection_conf_t const *conf, char const *log_prefix, void *uctx)
Definition bio.c:1013
static void xlat_sendto_retry(xlat_ctx_t const *xctx, request_t *request, fr_retry_t const *retry)
Definition bio.c:2973
static void bio_request_reset(bio_request_t *u)
Clear out any connection specific resources from a udp request.
Definition bio.c:216
trunk_request_t * treq
Definition bio.c:116
static xlat_action_t xlat_sendto_resume(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, UNUSED fr_value_box_list_t *in)
Definition bio.c:2945
static void status_check_next(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
Handle retries for a status check.
Definition bio.c:2000
rlm_radius_t const * inst
our instance
Definition bio.c:46
static connection_state_t conn_init(void **h_out, connection_t *conn, void *uctx)
Initialise a new outbound connection.
Definition bio.c:729
connection_t ** connections
Definition bio.c:66
fr_rb_expire_node_t expire
Definition bio.c:144
static void revive_timeout(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
Revive a connection after "revive_interval".
Definition bio.c:1361
uint8_t * packet
Packet we write to the network.
Definition bio.c:132
static void conn_init_timeout(UNUSED fr_timer_list_t *tl, fr_time_t now, void *uctx)
Status check timer when opening the connection for the first time.
Definition bio.c:365
static connection_state_t conn_failed(void *handle, connection_state_t state, UNUSED void *uctx)
Connection failed.
Definition bio.c:986
bio_limit_ports_t limit_source_ports
What type of port limit is in use.
Definition bio.c:52
fr_radius_ctx_t radius_ctx
for signing packets
Definition bio.c:51
radius_track_t * tt
RADIUS ID tracking structure.
Definition bio.c:95
static void mod_dup(request_t *request, bio_request_t *u)
Definition bio.c:1519
bio_handle_ctx_t ctx
common struct for home servers and BIO handles
Definition bio.c:75
uint8_t code
Packet code.
Definition bio.c:130
static void request_mux(UNUSED fr_event_list_t *el, trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
Definition bio.c:1646
static fr_cmp_ret_t home_server_cmp(void const *one, void const *two)
Dynamic home server code.
Definition bio.c:2933
uint8_t * buffer
Receive buffer.
Definition bio.c:92
request_t * status_request
Definition bio.c:108
static void request_complete(request_t *request, NDEBUG_UNUSED void *preq, void *rctx, UNUSED void *uctx)
Response has already been written to the rctx at this point.
Definition bio.c:2464
bool status_check
is this packet a status check?
Definition bio.c:125
static xlat_arg_parser_t const xlat_radius_send_args[]
Definition bio.c:2824
static void xlat_sendto_signal(xlat_ctx_t const *xctx, request_t *request, fr_signal_t action)
Definition bio.c:2962
Track the handle, which is tightly correlated with the FD.
Definition bio.c:74
Connect request_t to local tracking structure.
Definition bio.c:115
static uint16_t fr_nbo_to_uint16(uint8_t const data[static sizeof(uint16_t)])
Read an unsigned 16bit integer from wire format (big endian)
Definition nbo.h:146
#define RADIUS_HEADER_LENGTH
Definition net.h:80
#define RADIUS_AUTH_VECTOR_LENGTH
Definition net.h:89
int fr_pair_value_memdup(fr_pair_t *vp, uint8_t const *src, size_t len, bool tainted)
Copy data into an "octets" data type.
Definition pair.c:2962
int fr_pair_value_strdup(fr_pair_t *vp, char const *src, bool tainted)
Copy data into an "string" data type.
Definition pair.c:2663
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
Definition pair.c:707
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
Definition pair.c:1352
int fr_pair_delete_by_da(fr_pair_list_t *list, fr_dict_attr_t const *da)
Delete matching pairs from the specified list.
Definition pair.c:1696
fr_pair_t * fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Dynamically allocate a new attribute and assign a fr_dict_attr_t.
Definition pair.c:290
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
Definition pair.c:46
static fr_internal_encode_ctx_t encode_ctx
static fr_dict_attr_t const * attr_state
static fr_dict_attr_t const * attr_error_cause
ssize_t fr_radius_decode(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t *packet, size_t packet_len, fr_radius_decode_ctx_t *decode_ctx)
Definition base.c:1178
int fr_radius_sign(uint8_t *packet, uint8_t const *vector, uint8_t const *secret, size_t secret_len)
Sign a previously encoded packet.
Definition base.c:361
ssize_t fr_radius_encode(fr_dbuff_t *dbuff, fr_pair_list_t *vps, fr_radius_encode_ctx_t *packet_ctx)
Definition base.c:1028
char const * fr_radius_packet_name[FR_RADIUS_CODE_MAX]
Definition base.c:115
#define fr_assert(_expr)
Definition rad_assert.h:37
static char * secret
#define REDEBUG(fmt,...)
#define RDEBUG(fmt,...)
#define WARN(fmt,...)
static fr_dict_attr_t const * attr_proxy_state
Definition radclient.c:127
static fr_dict_attr_t const * attr_message_authenticator
Definition radclient.c:128
#define INFO(fmt,...)
Definition radict.c:63
@ FR_RADIUS_REQUIRE_MA_YES
Require Message-Authenticator.
Definition radius.h:63
@ FR_RADIUS_REQUIRE_MA_AUTO
Only require Message-Authenticator if we've previously received a packet from this client with Messag...
Definition radius.h:64
#define RADIUS_AUTH_VECTOR_OFFSET
Definition radius.h:33
fr_radius_decode_fail_t
Failure reasons.
Definition radius.h:89
@ FR_RADIUS_FAIL_NONE
Definition radius.h:90
@ FR_RADIUS_FAIL_MA_MISSING
Definition radius.h:108
@ FR_RADIUS_FAIL_UNKNOWN_PACKET_CODE
Definition radius.h:95
char const * secret
Definition radius.h:127
size_t secret_length
Definition radius.h:128
fr_radius_ctx_t const * common
Definition radius.h:159
fr_radius_decode_fail_t reason
reason for decode failure
Definition radius.h:166
TALLOC_CTX * tmp_ctx
for temporary things cleaned up during decoding
Definition radius.h:163
static fr_dict_t const * dict_radius
Definition radsniff.c:93
static rs_t * conf
Definition radsniff.c:52
static fr_dict_attr_t const * attr_extended_attribute_1
Definition radsnmp.c:110
uint32_t fr_rand(void)
Return a 32-bit random number.
Definition rand.c:104
Smaller fast random number generator.
Definition rand.h:54
int fr_rb_find(void **found, fr_rb_tree_t const *tree, void const *data)
Find an element in the tree, returning the data, not the node.
Definition rb.c:586
int fr_rb_delete(fr_rb_tree_t *tree, void const *data)
Remove node and free data (if a free function was specified)
Definition rb.c:767
bool fr_rb_expire_insert(fr_rb_expire_t *expire, void *data, fr_time_t now)
Attempt to find current data in the tree, if it does not exist insert it.
Definition rb_expire.c:39
void fr_rb_expire_update(fr_rb_expire_t *expire, void *data, fr_time_t now)
Definition rb_expire.c:57
#define fr_rb_expire_inline_talloc_init(_expire, _type, _field, _data_cmp, _data_free, _lifetime)
Definition rb_expire.h:50
fr_dlist_head_t head
Definition rb_expire.h:35
fr_rb_tree_t tree
Definition rb_expire.h:34
dlist for expiring old entries
Definition rb_expire.h:44
#define RETURN_UNLANG_RCODE(_rcode)
Definition rcode.h:61
rlm_rcode_t
Return codes indicating the result of the module call.
Definition rcode.h:44
@ RLM_MODULE_OK
The module is OK, continue.
Definition rcode.h:49
@ RLM_MODULE_FAIL
Module failed, don't reply.
Definition rcode.h:48
@ RLM_MODULE_REJECT
Immediately reject the request.
Definition rcode.h:47
@ RLM_MODULE_UPDATED
OK (pairs modified).
Definition rcode.h:55
@ RLM_MODULE_HANDLED
The module handled the request, so stop.
Definition rcode.h:50
#define request_local_alloc_external(_ctx, _args)
Allocate a new external request outside of the request pool.
Definition request.h:336
Optional arguments for initialising requests.
Definition request.h:288
static int radius_fixups(rlm_radius_t const *inst, request_t *request)
Do any RADIUS-layer fixups for proxying.
Definition rlm_radius.c:517
static fr_dict_attr_t const * attr_nas_identifier
Definition rlm_radius.c:194
static fr_dict_attr_t const * attr_original_packet_code
Definition rlm_radius.c:195
static fr_dict_attr_t const * attr_event_timestamp
Definition rlm_radius.c:190
static fr_dict_attr_t const * attr_response_length
Definition rlm_radius.c:196
fr_radius_require_ma_t require_message_authenticator
Require Message-Authenticator in responses.
Definition rlm_radius.h:75
fr_time_delta_t revive_interval
Definition rlm_radius.h:60
bool track_load_balance
do we track load-balance state
Definition rlm_radius.h:77
fr_retry_config_t retry[FR_RADIUS_CODE_MAX]
Definition rlm_radius.h:88
char const * name
Definition rlm_radius.h:56
uint32_t status_check
code of status-check type
Definition rlm_radius.h:80
rlm_radius_mode_t mode
proxy, client, etc.
Definition rlm_radius.h:71
@ RLM_RADIUS_MODE_XLAT_PROXY
radius.sendto.ipaddr(), but we do look for a reply.
Definition rlm_radius.h:47
@ RLM_RADIUS_MODE_INVALID
Definition rlm_radius.h:42
@ RLM_RADIUS_MODE_PROXY
we proxy to one home server
Definition rlm_radius.h:43
@ RLM_RADIUS_MODE_REPLICATE
to a particular destination
Definition rlm_radius.h:45
@ RLM_RADIUS_MODE_UNCONNECTED_REPLICATE
radius.sendto.ipaddr(), but we don't look for a reply
Definition rlm_radius.h:46
@ RLM_RADIUS_MODE_CLIENT
we are a client to one home server
Definition rlm_radius.h:44
uint32_t max_packet_size
Maximum packet size.
Definition rlm_radius.h:66
fr_time_delta_t response_window
Definition rlm_radius.h:58
uint32_t max_attributes
Maximum number of attributes to decode in response.
Definition rlm_radius.h:73
fr_time_delta_t zombie_period
Definition rlm_radius.h:59
fr_bio_fd_config_t fd_config
for now MUST be at the start!
Definition rlm_radius.h:54
static conf_parser_t retry_config[]
Definition rlm_tacacs.c:38
int fr_schedule_worker_id(void)
Return the worker id for the current thread.
Definition schedule.c:110
void connection_signal_reconnect(connection_t *conn, connection_reason_t reason)
Asynchronously signal the connection should be reconnected.
int connection_signal_on_fd(connection_t *conn, int fd)
Setup the connection to change states to connected or failed based on I/O events.
connection_t * connection_alloc(TALLOC_CTX *ctx, fr_event_list_t *el, connection_funcs_t const *funcs, connection_conf_t const *conf, char const *log_prefix, void const *uctx)
Allocate a new connection.
void connection_signal_connected(connection_t *conn)
Asynchronously signal that the connection is open.
void * data
Module's instance data.
Definition module.h:293
#define pair_append_request(_attr, _da)
Allocate and append a fr_pair_t to the request list.
Definition pair.h:37
fr_signal_t
Signals that can be generated/processed by request signal handlers.
Definition signal.h:38
@ FR_SIGNAL_DUP
A duplicate request was received.
Definition signal.h:44
@ FR_SIGNAL_CANCEL
Request has been cancelled.
Definition signal.h:40
eap_aka_sim_process_conf_t * inst
fr_pair_t * vp
Definition log.h:93
Value pair map.
Definition map.h:77
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
#define talloc_get_type_abort_const
Definition talloc.h:117
static int talloc_const_free(void const *ptr)
Free const'd memory.
Definition talloc.h:288
#define talloc_strdup(_ctx, _str)
Definition talloc.h:149
#define fr_time_gteq(_a, _b)
Definition time.h:238
#define fr_time_wrap(_time)
Definition time.h:145
#define fr_time_lteq(_a, _b)
Definition time.h:240
#define fr_time_eq(_a, _b)
Definition time.h:241
#define fr_time_add(_a, _b)
Add a time/time delta together.
Definition time.h:196
#define fr_time_gt(_a, _b)
Definition time.h:237
#define fr_time_sub(_a, _b)
Subtract one time from another.
Definition time.h:229
static fr_unix_time_t fr_time_to_unix_time(fr_time_t when)
Convert an fr_time_t (internal time) to our version of unix time (wallclock time)
Definition time.h:688
static int8_t fr_time_cmp(fr_time_t a, fr_time_t b)
Compare two fr_time_t values.
Definition time.h:916
"server local" time.
Definition time.h:69
An event timer list.
Definition timer.c:49
A timer event.
Definition timer.c:83
#define FR_TIMER_DELETE(_ev_p)
Definition timer.h:103
#define FR_TIMER_DELETE_RETURN(_ev_p)
Definition timer.h:110
#define FR_TIMER_DISARM(_ev)
Definition timer.h:91
static bool fr_timer_armed(fr_timer_t *ev)
Definition timer.h:120
#define fr_timer_at(...)
Definition timer.h:81
void radius_track_state_log(fr_log_t const *log, fr_log_type_t log_type, char const *file, int line, radius_track_t *tt, radius_track_log_extra_t extra)
Print out the state of every tracking entry.
Definition track.c:298
int radius_track_entry_update(radius_track_entry_t *te, uint8_t const *vector)
Update a tracking entry with the authentication vector.
Definition track.c:223
radius_track_entry_t * radius_track_entry_find(radius_track_t *tt, uint8_t packet_id, uint8_t const *vector)
Find a tracking entry from a request authenticator.
Definition track.c:252
radius_track_t * radius_track_alloc(TALLOC_CTX *ctx)
Create an radius_track_t.
Definition track.c:38
#define radius_track_entry_release(_te)
Definition track.h:90
void * uctx
Result/resumption context.
Definition track.h:47
uint8_t id
our ID
Definition track.h:50
unsigned int num_requests
number of requests in the allocation
Definition track.h:65
#define radius_track_entry_reserve(_te_out, _ctx, _tt, _request, _code, _uctx)
Definition track.h:82
request_t * request
as always...
Definition track.h:45
Track one request to a response.
Definition track.h:36
void trunk_connection_callback_readable(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
Standard I/O read function.
Definition trunk.c:4080
void trunk_connection_callback_writable(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
Standard I/O write function.
Definition trunk.c:4097
void trunk_request_signal_partial(trunk_request_t *treq)
Signal a partial write.
Definition trunk.c:2076
void trunk_request_signal_fail(trunk_request_t *treq)
Signal that a trunk request failed.
Definition trunk.c:2179
trunk_request_t * trunk_request_alloc(trunk_t *trunk, request_t *request)
(Pre-)Allocate a new trunk request
Definition trunk.c:2525
uint64_t trunk_connection_requests_requeue(trunk_connection_t *tconn, int states, uint64_t max, bool fail_bound)
Move requests off of a connection and requeue elsewhere.
Definition trunk.c:2057
trunk_enqueue_t trunk_request_enqueue_on_conn(trunk_request_t **treq_out, trunk_connection_t *tconn, request_t *request, void *preq, void *rctx, bool ignore_limits)
Enqueue additional requests on a specific connection.
Definition trunk.c:2794
trunk_enqueue_t trunk_request_enqueue(trunk_request_t **treq_out, trunk_t *trunk, request_t *request, void *preq, void *rctx)
Enqueue a request that needs data written to the trunk.
Definition trunk.c:2640
trunk_enqueue_t trunk_request_requeue(trunk_request_t *treq)
Re-enqueue a request on the same connection.
Definition trunk.c:2729
int trunk_connection_pop_request(trunk_request_t **treq_out, trunk_connection_t *tconn)
Pop a request off a connection's pending queue.
Definition trunk.c:3949
void trunk_request_signal_cancel(trunk_request_t *treq)
Cancel a trunk request.
Definition trunk.c:2199
trunk_t * trunk_alloc(TALLOC_CTX *ctx, fr_event_list_t *el, trunk_io_funcs_t const *funcs, trunk_conf_t const *conf, char const *log_prefix, void const *uctx, bool delay_start, fr_pair_list_t *trigger_args)
Allocate a new collection of connections.
Definition trunk.c:5094
void trunk_request_free(trunk_request_t **treq_to_free)
If the trunk request is freed then update the target requests.
Definition trunk.c:2369
void trunk_connection_signal_active(trunk_connection_t *tconn)
Signal a trunk connection is no longer full.
Definition trunk.c:4026
void trunk_connection_signal_inactive(trunk_connection_t *tconn)
Signal a trunk connection cannot accept more requests.
Definition trunk.c:4003
void trunk_request_signal_sent(trunk_request_t *treq)
Signal that the request was written to a connection successfully.
Definition trunk.c:2097
void trunk_request_signal_complete(trunk_request_t *treq)
Signal that a trunk request is complete.
Definition trunk.c:2141
void trunk_connection_signal_reconnect(trunk_connection_t *tconn, connection_reason_t reason)
Signal a trunk connection is no longer viable.
Definition trunk.c:4065
void trunk_request_state_log(fr_log_t const *log, fr_log_type_t log_type, char const *file, int line, trunk_request_t const *treq)
Definition trunk.c:2882
Associates request queues with a connection.
Definition trunk.c:133
Wraps a normal request.
Definition trunk.c:99
Main trunk management handle.
Definition trunk.c:215
#define TRUNK_REQUEST_STATE_ALL
All request states.
Definition trunk.h:205
trunk_connection_alloc_t connection_alloc
Allocate a new connection_t.
Definition trunk.h:747
trunk_connection_event_t
What type of I/O events the trunk connection is currently interested in receiving.
Definition trunk.h:81
@ TRUNK_CONN_EVENT_BOTH
Trunk should be notified if a connection is readable or writable.
Definition trunk.h:88
@ TRUNK_CONN_EVENT_WRITE
Trunk should be notified if a connection is writable.
Definition trunk.h:86
@ TRUNK_CONN_EVENT_NONE
Don't notify the trunk on connection state changes.
Definition trunk.h:82
@ TRUNK_CONN_EVENT_READ
Trunk should be notified if a connection is readable.
Definition trunk.h:84
trunk_cancel_reason_t
Reasons for a request being cancelled.
Definition trunk.h:55
@ TRUNK_CANCEL_REASON_REQUEUE
A previously sent request is being requeued.
Definition trunk.h:59
@ TRUNK_ENQUEUE_DST_UNAVAILABLE
Destination is down.
Definition trunk.h:163
@ TRUNK_ENQUEUE_FAIL
General internal sanity check failure.
Definition trunk.h:164
@ TRUNK_ENQUEUE_OK
Operation was successful.
Definition trunk.h:160
@ TRUNK_ENQUEUE_NO_CAPACITY
At maximum number of connections, and no connection has capacity.
Definition trunk.h:161
@ TRUNK_ENQUEUE_IN_BACKLOG
Request should be enqueued in backlog.
Definition trunk.h:159
trunk_request_state_t
Used for sanity checks and to simplify freeing.
Definition trunk.h:171
@ TRUNK_REQUEST_STATE_PARTIAL
Some of the request was written to the socket, more of it should be written later.
Definition trunk.h:180
@ TRUNK_REQUEST_STATE_REAPABLE
Request has been written, needs to persist, but we are not currently waiting for any response.
Definition trunk.h:183
@ TRUNK_REQUEST_STATE_UNASSIGNED
Transition state - Request currently not assigned to any connection.
Definition trunk.h:175
@ TRUNK_REQUEST_STATE_INIT
Initial state.
Definition trunk.h:172
@ TRUNK_REQUEST_STATE_CANCEL_SENT
We've informed the remote server that the request has been cancelled.
Definition trunk.h:195
@ TRUNK_REQUEST_STATE_COMPLETE
The request is complete.
Definition trunk.h:192
@ TRUNK_REQUEST_STATE_FAILED
The request failed.
Definition trunk.h:193
@ TRUNK_REQUEST_STATE_CANCEL
A request on a particular socket was cancel.
Definition trunk.h:194
@ TRUNK_REQUEST_STATE_CANCEL_PARTIAL
We partially wrote a cancellation request.
Definition trunk.h:197
@ TRUNK_REQUEST_STATE_BACKLOG
In the backlog.
Definition trunk.h:177
@ TRUNK_REQUEST_STATE_CANCEL_COMPLETE
Remote server has acknowledged our cancellation.
Definition trunk.h:198
@ TRUNK_REQUEST_STATE_PENDING
In the queue of a connection and is pending writing.
Definition trunk.h:178
@ TRUNK_REQUEST_STATE_SENT
Was written to a socket. Waiting for a response.
Definition trunk.h:182
I/O functions to pass to trunk_alloc.
Definition trunk.h:746
static fr_event_list_t * el
xlat_action_t unlang_xlat_yield_to_retry(request_t *request, xlat_func_t resume, fr_unlang_xlat_retry_t retry, xlat_func_signal_t signal, fr_signal_t sigmask, void *rctx, fr_retry_config_t const *retry_cfg)
Yield a request back to the interpreter, with retries.
Definition xlat.c:663
#define XLAT_ARGS(_list,...)
Populate local variables with value boxes from the input list.
Definition xlat.h:383
unsigned int required
Argument must be present, and non-empty.
Definition xlat.h:146
#define XLAT_ARG_PARSER_TERMINATOR
Definition xlat.h:170
xlat_action_t
Definition xlat.h:37
@ XLAT_ACTION_FAIL
An xlat function failed.
Definition xlat.h:44
@ XLAT_ACTION_DONE
We're done evaluating this level of nesting.
Definition xlat.h:43
Definition for a single argument consumed by an xlat function.
Definition xlat.h:145
A node in a graph of unlang_op_t (s) that we execute.
bool fr_pair_list_empty(fr_pair_list_t const *list)
Is a valuepair list empty.
void fr_pair_list_free(fr_pair_list_t *list)
Free memory used by a valuepair list.
void fr_pair_list_append(fr_pair_list_t *dst, fr_pair_list_t *src)
Appends a list of fr_pair_t from a temporary list to a destination list.
fr_retry_state_t fr_retry_next(fr_retry_t *r, fr_time_t now)
Initialize a retransmission counter.
Definition retry.c:110
void fr_retry_init(fr_retry_t *r, fr_time_t now, fr_retry_config_t const *config)
Initialize a retransmission counter.
Definition retry.c:36
fr_time_t start
when we started the retransmission
Definition retry.h:53
fr_time_delta_t rt
retransmit interval
Definition retry.h:57
uint32_t mrc
Maximum retransmission count.
Definition retry.h:36
fr_retry_config_t const * config
master configuration
Definition retry.h:52
fr_retry_state_t state
so callers can see what state it's in.
Definition retry.h:60
@ FR_RETRY_MRC
reached maximum retransmission count
Definition retry.h:47
@ FR_RETRY_CONTINUE
Definition retry.h:46
@ FR_RETRY_MRD
reached maximum retransmission duration
Definition retry.h:48
uint32_t count
number of sent packets
Definition retry.h:58
fr_time_delta_t mrd
Maximum retransmission duration.
Definition retry.h:35
fr_time_t updated
last update, really a cached "now".
Definition retry.h:56
fr_time_t next
when the next timer should be set
Definition retry.h:55
int af
AF_INET, AF_INET6, or AF_UNIX.
Definition socket.h:83
int fd
File descriptor if this is a live socket.
Definition socket.h:86
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:558
#define fr_value_box_alloc(_ctx, _type, _enumv)
Allocate a value box of a specific type.
Definition value.h:644
#define fr_box_ipaddr(_val)
Definition value.h:317
static fr_slen_t data
Definition value.h:1340
#define fr_box_time_delta(_val)
Definition value.h:366
int nonnull(2, 5))
static size_t char ** out
Definition value.h:1030
void * rctx
Resume context.
Definition xlat_ctx.h:54
module_ctx_t const * mctx
Synthesised module calling ctx.
Definition xlat_ctx.h:52
An xlat calling ctx.
Definition xlat_ctx.h:49