The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
rlm_tacacs_tcp.c
Go to the documentation of this file.
1/*
2 * This program is 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: bfd6723f35ce8fcdea578c594b1ff589bfe17157 $
19 * @file rlm_tacacs_tcp.c
20 * @brief TACACS+ transport
21 *
22 * @copyright 2023 Network RADIUS SAS (legal@networkradius.com)
23 */
24RCSID("$Id: bfd6723f35ce8fcdea578c594b1ff589bfe17157 $")
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/util/heap.h>
31
32#include <sys/uio.h>
33
34#include "rlm_tacacs.h"
35
36/** Static configuration for the module.
37 *
38 */
39typedef struct {
40 rlm_tacacs_t *parent; //!< rlm_tacacs instance.
42
43 fr_ipaddr_t dst_ipaddr; //!< IP of the home server.
44 fr_ipaddr_t src_ipaddr; //!< IP we open our socket on.
45 uint16_t dst_port; //!< Port of the home server.
46 char const *secret; //!< Shared secret.
47 size_t secretlen; //!< length of secret
48
49 char const *interface; //!< Interface to bind to.
50
51 uint32_t recv_buff; //!< How big the kernel's receive buffer should be.
52 uint32_t send_buff; //!< How big the kernel's send buffer should be.
53
54 uint32_t max_packet_size; //!< Maximum packet size.
55 uint16_t max_send_coalesce; //!< Maximum number of packets to coalesce into one mmsg call.
56
57 bool recv_buff_is_set; //!< Whether we were provided with a recv_buf
58 bool send_buff_is_set; //!< Whether we were provided with a send_buf
59
60 fr_pair_list_t *trigger_args; //!< Pairs passed to trigger request.
62
63typedef struct {
64 fr_event_list_t *el; //!< Event list.
65
66 rlm_tacacs_tcp_t const *inst; //!< our instance
67
68 trunk_conf_t trunk_conf; //!< trunk configuration
69 trunk_t *trunk; //!< trunk handler
71
72typedef struct {
74 rlm_rcode_t rcode; //!< from the transport
76
78
79typedef struct {
80 uint8_t *read; //!< where we read data from
81 uint8_t *write; //!< where we write data to
82 uint8_t *end; //!< end of the buffer
83 uint8_t *data; //!< actual data
85
86/** Track the handle, which is tightly correlated with the FD
87 *
88 */
89typedef struct {
90 char const *name; //!< From IP PORT to IP PORT.
91 char const *module_name; //!< the module that opened the connection
92
93 int fd; //!< File descriptor.
94
95 trunk_request_t **coalesced; //!< Outbound coalesced requests.
96
97 size_t send_buff_actual; //!< What we believe the maximum SO_SNDBUF size to be.
98 ///< We don't try and encode more packet data than this
99 ///< in one go.
100
101 rlm_tacacs_tcp_t const *inst; //!< Our module instance.
103
104 uint32_t session_id; //!< for TACACS+ "security".
105
106 uint32_t max_packet_size; //!< Our max packet size. may be different from the parent.
107
108 fr_ipaddr_t src_ipaddr; //!< Source IP address. May be altered on bind
109 //!< to be the actual IP address packets will be
110 //!< sent on. This is why we can't use the inst
111 //!< src_ipaddr field.
112 uint16_t src_port; //!< Source port specific to this connection.
113 //!< @todo - not set by socket_client_tcp()
114
115 tcp_buffer_t recv; //!< receive buffer
116 tcp_buffer_t send; //!< send buffer
117
118 int id; //!< starts at 1.
119 int active; //!< active packets
120 trunk_request_t *tracking[UINT8_MAX + 1]; //!< all sequential!
121
122 fr_time_t mrs_time; //!< Most recent sent time which had a reply.
123 fr_time_t last_reply; //!< When we last received a reply.
124 fr_time_t first_sent; //!< first time we sent a packet since going idle
125 fr_time_t last_sent; //!< last time we sent a packet.
126 fr_time_t last_idle; //!< last time we had nothing to do
127
128 fr_timer_t *zombie_ev; //!< Zombie timeout.
129
130 trunk_connection_t *tconn; //!< trunk connection
132
133
134/** Connect request_t to local tracking structure
135 *
136 */
138 uint32_t priority; //!< copied from request->async->priority
139 fr_time_t recv_time; //!< copied from request->async->recv_time
140
141 uint8_t code; //!< Packet code.
142 uint8_t id; //!< Last ID assigned to this packet.
143 bool outstanding; //!< are we waiting for a reply?
144
145 uint8_t *packet; //!< Packet we write to the network.
146 size_t packet_len; //!< Length of the packet.
147
148 fr_timer_t *ev; //!< timer for retransmissions
149 fr_retry_t retry; //!< retransmission timers
150};
151
169
170static const conf_parser_t module_config[] = {
172 { FR_CONF_OFFSET_TYPE_FLAGS("ipv4addr", FR_TYPE_IPV4_ADDR, 0, rlm_tacacs_tcp_t, dst_ipaddr) },
173 { FR_CONF_OFFSET_TYPE_FLAGS("ipv6addr", FR_TYPE_IPV6_ADDR, 0, rlm_tacacs_tcp_t, dst_ipaddr) },
174
175 { FR_CONF_OFFSET("port", rlm_tacacs_tcp_t, dst_port) },
176
177 { FR_CONF_OFFSET("secret", rlm_tacacs_tcp_t, secret) }, /* can be NULL */
178
179 { FR_CONF_OFFSET("interface", rlm_tacacs_tcp_t, interface) },
180
181 { FR_CONF_OFFSET_IS_SET("recv_buff", FR_TYPE_UINT32, 0, rlm_tacacs_tcp_t, recv_buff) },
182 { FR_CONF_OFFSET_IS_SET("send_buff", FR_TYPE_UINT32, 0, rlm_tacacs_tcp_t, send_buff) },
183
184 { FR_CONF_OFFSET("max_packet_size", rlm_tacacs_tcp_t, max_packet_size), .dflt = STRINGIFY(FR_MAX_PACKET_SIZE) },
185 { FR_CONF_OFFSET("max_send_coalesce", rlm_tacacs_tcp_t, max_send_coalesce), .dflt = "1024" },
186
187 { FR_CONF_OFFSET_TYPE_FLAGS("src_ipaddr", FR_TYPE_COMBO_IP_ADDR, 0, rlm_tacacs_tcp_t, src_ipaddr) },
188 { FR_CONF_OFFSET_TYPE_FLAGS("src_ipv4addr", FR_TYPE_IPV4_ADDR, 0, rlm_tacacs_tcp_t, src_ipaddr) },
189 { FR_CONF_OFFSET_TYPE_FLAGS("src_ipv6addr", FR_TYPE_IPV6_ADDR, 0, rlm_tacacs_tcp_t, src_ipaddr) },
190
192};
193
194static fr_dict_t const *dict_tacacs;
195
198 { .out = &dict_tacacs, .proto = "tacacs" },
200};
201
205
208 { .out = &attr_packet_type, .name = "Packet-Type", .type = FR_TYPE_UINT32, .dict = &dict_tacacs },
209 { .out = &attr_packet_hdr, .name = "Packet", .type = FR_TYPE_STRUCT, .dict = &dict_tacacs },
210 { .out = &attr_session_id, .name = "Packet.Session-ID", .type = FR_TYPE_UINT32, .dict = &dict_tacacs },
212};
213
214/** Clear out any connection specific resources from a tcp request
215 *
216 */
218{
219 req->packet = NULL;
220
221 fr_assert(h->active > 0);
222 fr_assert(h->tracking[req->id] != NULL);
223 fr_assert(h->tracking[req->id]->preq == req);
224
225 h->tracking[req->id] = NULL;
226 req->outstanding = false;
227 h->active--;
228
229 FR_TIMER_DISARM(req->ev);
230
231 /*
232 * We've sent 255 packets, and received all replies. Shut the connection down.
233 *
234 * Welcome to the insanity that is TACACS+.
235 */
236 if ((h->active == 0) && (h->id > 255)) {
238 }
239}
240
241
242/** Free a connection handle, closing associated resources
243 *
244 */
246{
247 fr_assert(h->fd >= 0);
248
250
251 if (shutdown(h->fd, SHUT_RDWR) < 0) {
252 DEBUG3("%s - Failed shutting down connection %s: %s",
253 h->module_name, h->name, fr_syserror(errno));
254 }
255
256 if (close(h->fd) < 0) {
257 DEBUG3("%s - Failed closing connection %s: %s",
258 h->module_name, h->name, fr_syserror(errno));
259 }
260
261 h->fd = -1;
262
263 DEBUG("%s - Connection closed - %s", h->module_name, h->name);
264
265 return 0;
266}
267
268/** Initialise a new outbound connection
269 *
270 * @param[out] h_out Where to write the new file descriptor.
271 * @param[in] conn to initialise.
272 * @param[in] uctx A #tcp_thread_t
273 */
274CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
275static connection_state_t conn_init(void **h_out, connection_t *conn, void *uctx)
276{
277 int fd;
278 tcp_handle_t *h;
279 tcp_thread_t *thread = talloc_get_type_abort(uctx, tcp_thread_t);
280
281 MEM(h = talloc_zero(conn, tcp_handle_t));
282 h->thread = thread;
283 h->inst = thread->inst;
284 h->module_name = h->inst->parent->name;
285 h->src_ipaddr = h->inst->src_ipaddr;
286 h->src_port = 0;
288 h->last_idle = fr_time();
289
290 h->id = 1; /* clients send odd sequence numbers */
291 h->session_id = fr_rand();
292
293 /*
294 * Initialize the buffer of coalesced packets we're doing to write.
295 */
296 h->coalesced = talloc_zero_array(h, trunk_request_t *, h->inst->max_send_coalesce);
297
298 /*
299 * Open the outgoing socket.
300 */
301 fd = fr_socket_client_tcp(NULL, &h->src_ipaddr, &h->inst->dst_ipaddr, h->inst->dst_port, true);
302 if (fd < 0) {
303 PERROR("%s - Failed opening socket", h->module_name);
304 talloc_free(h);
306 }
307
308 /*
309 * Set the connection name.
310 */
311 h->name = fr_asprintf(h, "proto tcp local %pV port %u remote %pV port %u",
314
315 talloc_set_destructor(h, _tcp_handle_free);
316
317#ifdef SO_RCVBUF
318 if (h->inst->recv_buff_is_set) {
319 int opt;
320
321 opt = h->inst->recv_buff;
322 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(int)) < 0) {
323 WARN("%s - Failed setting 'SO_RCVBUF': %s", h->module_name, fr_syserror(errno));
324 }
325 }
326#endif
327
328#ifdef SO_SNDBUF
329 {
330 int opt;
331 socklen_t socklen = sizeof(int);
332
333 if (h->inst->send_buff_is_set) {
334 opt = h->inst->send_buff;
335 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(int)) < 0) {
336 WARN("%s - Failed setting 'SO_SNDBUF', write performance may be sub-optimal: %s",
337 h->module_name, fr_syserror(errno));
338 }
339 }
340
341 if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &opt, &socklen) < 0) {
342 WARN("%s - Failed getting 'SO_SNDBUF', write performance may be sub-optimal: %s",
343 h->module_name, fr_syserror(errno));
344
345 /*
346 * This controls how many packets we attempt
347 * to send at once. Nothing bad happens if
348 * we get it wrong, but the user may see
349 * ENOBUFS errors at high packet rates.
350 *
351 * Since this is TACACS, we have small
352 * packets and a maximum of 255 packets
353 * per connection. So don't set this too large.
354 */
355 if (h->inst->send_buff_is_set) {
357 } else {
359 if (h->send_buff_actual > 256*1024) h->send_buff_actual = 256*1024;
360 }
361
362 WARN("%s - Max coalesced outbound data will be %zu bytes", h->module_name,
364 } else {
365#ifdef __linux__
366 /*
367 * Linux doubles the buffer when you set it
368 * to account for "overhead".
369 */
370 h->send_buff_actual = ((size_t)opt) / 2;
371#else
372 h->send_buff_actual = (size_t)opt;
373#endif
374 }
375 }
376#else
378 h->inst_send_buff : h->max_packet_size * h->inst->max_send_coalesce;
379
380 WARN("%s - Modifying 'SO_SNDBUF' value is not supported on this system, "
381 "write performance may be sub-optimal", h->module_name);
382 WARN("%s - Max coalesced outbound data will be %zu bytes", h->module_name, h->inst->send_buff_actual);
383#endif
384
385 /*
386 * Allow receiving of 2 max-sized packets. In practice, most packets will be less than this.
387 */
388 MEM(h->recv.data = talloc_array(h, uint8_t, h->max_packet_size * 2));
389 h->recv.read = h->recv.write = h->recv.data;
390 h->recv.end = h->recv.data + h->max_packet_size * 2;
391
392 /*
393 * Use the system SO_SNDBUF for how many packets to send at once. In most circumstances the
394 * packets are small, and widely separated in time, and we really only need a very small buffer.
395 */
396 MEM(h->send.data = talloc_array(h, uint8_t, h->send_buff_actual));
397 h->send.read = h->send.write = h->send.data;
398 h->send.end = h->send.data + h->send_buff_actual;
399
400 h->fd = fd;
401
402 /*
403 * Signal the connection
404 * as open as soon as it becomes writable.
405 */
406 connection_signal_on_fd(conn, fd);
407
408 *h_out = h;
409
410 // @todo - initialize the tracking memory, etc.
411 // i.e. histograms (or hyperloglog) of packets, so we can see
412 // which connections / home servers are fast / slow.
413
415}
416
417/** Shutdown/close a file descriptor
418 *
419 */
420static void conn_close(UNUSED fr_event_list_t *el, void *handle, UNUSED void *uctx)
421{
422 tcp_handle_t *h = talloc_get_type_abort(handle, tcp_handle_t);
423
424 /*
425 * There's tracking entries still allocated
426 * this is bad, they should have all been
427 * released.
428 */
429 fr_assert(!h->active);
430
431 DEBUG4("Freeing rlm_tacacs_tcp handle %p", handle);
432
433 talloc_free(h);
434}
435
436CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
438 connection_conf_t const *conf,
439 char const *log_prefix, void *uctx)
440{
441 connection_t *conn;
442 tcp_thread_t *thread = talloc_get_type_abort(uctx, tcp_thread_t);
443
444 conn = connection_alloc(tconn, el,
446 .init = conn_init,
447 .close = conn_close,
448 },
449 conf,
450 log_prefix,
451 thread);
452 if (!conn) {
453 PERROR("%s - Failed allocating state handler for new connection", thread->inst->parent->name);
454 return NULL;
455 }
456
457 return conn;
458}
459
460/** Connection errored
461 *
462 * We were signalled by the event loop that a fatal error occurred on this connection.
463 *
464 * @param[in] el The event list signalling.
465 * @param[in] fd that errored.
466 * @param[in] flags El flags.
467 * @param[in] fd_errno The nature of the error.
468 * @param[in] uctx The trunk connection handle (tconn).
469 */
470static void conn_error(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, int fd_errno, void *uctx)
471{
472 trunk_connection_t *tconn = talloc_get_type_abort(uctx, trunk_connection_t);
473 connection_t *conn = tconn->conn;
474 tcp_handle_t *h = talloc_get_type_abort(conn->h, tcp_handle_t);
475
476 if (fd_errno) ERROR("%s - Connection %s failed: %s", h->module_name, h->name, fr_syserror(fd_errno));
477
479}
480
481CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
484 trunk_connection_event_t notify_on, UNUSED void *uctx)
485{
486 tcp_handle_t *h = talloc_get_type_abort(conn->h, tcp_handle_t);
487 fr_event_fd_cb_t read_fn = NULL;
488 fr_event_fd_cb_t write_fn = NULL;
489
490 switch (notify_on) {
492 return;
493
496 break;
497
500 break;
501
505 break;
506
507 }
508
509 if (fr_event_fd_insert(h, NULL, el, h->fd,
510 read_fn,
511 write_fn,
513 tconn) < 0) {
514 PERROR("%s - Failed inserting FD event", h->module_name);
515
516 /*
517 * May free the connection!
518 */
520 }
521}
522
523/*
524 * Return negative numbers to put 'a' at the top of the heap.
525 * Return positive numbers to put 'b' at the top of the heap.
526 *
527 * We want the value with the lowest timestamp to be prioritized at
528 * the top of the heap.
529 */
530static int8_t request_prioritise(void const *one, void const *two)
531{
532 tcp_request_t const *a = one;
533 tcp_request_t const *b = two;
534 int8_t ret;
535
536 /*
537 * Larger priority is more important.
538 */
539 ret = CMP_PREFER_LARGER(a->priority, b->priority);
540 if (ret != 0) return ret;
541
542 /*
543 * Smaller timestamp (i.e. earlier) is more important.
544 */
545 return fr_time_cmp(a->recv_time, b->recv_time);
546}
547
548/** Decode response packet data, extracting relevant information and validating the packet
549 *
550 * @param[in] ctx to allocate pairs in.
551 * @param[out] reply Pointer to head of pair list to add reply attributes to.
552 * @param[out] response_code The type of response packet.
553 * @param[in] h connection handle.
554 * @param[in] request the request.
555 * @param[in] req TCP request.
556 * @param[in] data to decode.
557 * @param[in] data_len Length of input data.
558 * @return
559 * - <0 on error
560 * - >0 for how many bytes were decoded
561 */
562static ssize_t decode(TALLOC_CTX *ctx, fr_pair_list_t *reply, uint8_t *response_code,
563 tcp_handle_t *h, request_t *request, tcp_request_t *req,
564 uint8_t *data, size_t data_len)
565{
566 rlm_tacacs_tcp_t const *inst = h->thread->inst;
567 ssize_t packet_len;
568 int code;
569
570 *response_code = 0; /* Initialise to keep the rest of the code happy */
571
572 /*
573 * Check the session ID.
574 */
575 if (memcmp(data + 4, req->packet + 4, 4) != 0) {
576 REDEBUG("Session ID %08x does not match expected number %08x",
578 }
579
580 /*
581 * Decode the attributes, in the context of the reply.
582 * This only fails if the packet is strangely malformed,
583 * or if we run out of memory.
584 */
585 packet_len = fr_tacacs_decode(ctx, reply, NULL, data, data_len, NULL, inst->secret, inst->secretlen, &code);
586 if (packet_len < 0) {
587 RPEDEBUG("Failed decoding TACACS+ reply packet");
588 fr_pair_list_free(reply);
589 return -1;
590 }
591
592 RDEBUG("Received %s ID %d length %ld reply packet on connection %s",
593 fr_tacacs_packet_names[code], code, packet_len, h->name);
594 log_request_pair_list(L_DBG_LVL_2, request, NULL, reply, NULL);
595
596 *response_code = code;
597
598 /*
599 * Fixup retry times
600 */
601 if (fr_time_gt(req->retry.start, h->mrs_time)) h->mrs_time = req->retry.start;
602
603 return packet_len;
604}
605
606static int encode(tcp_handle_t *h, request_t *request, tcp_request_t *req)
607{
608 ssize_t packet_len;
609 rlm_tacacs_tcp_t const *inst = h->inst;
610 fr_pair_t *hdr, *vp;
611
612 fr_assert(inst->parent->allowed[req->code]);
613 fr_assert(!req->packet);
614 fr_assert(!req->outstanding);
615
616 /*
617 * Encode the packet in the outbound buffer.
618 */
619 req->packet = h->send.write;
620
621 /*
622 * Set the session ID, if it hasn't already been set.
623 */
624 hdr = fr_pair_find_by_da(&request->request_pairs, NULL, attr_packet_hdr);
625 if (!hdr) hdr = request->request_ctx;
626
627 vp = fr_pair_find_by_da_nested(&hdr->vp_group, NULL, attr_session_id);
628 if (!vp) {
630
631 vp->vp_uint32 = h->session_id;
632 fr_pair_append(&hdr->vp_group, vp);
634 }
635
636 /*
637 * Encode the packet.
638 */
639 packet_len = fr_tacacs_encode(&FR_DBUFF_TMP(req->packet, (size_t) inst->max_packet_size), NULL,
640 inst->secret, inst->secretlen, request->reply->code, &request->request_pairs);
641 if (packet_len < 0) {
642 RPERROR("Failed encoding packet");
643 return -1;
644 }
645
646 /*
647 * Update the ID and the actual packet length;
648 */
649 req->packet[1] = req->id;
650 req->packet_len = packet_len;
651 req->outstanding = true;
652
653// fr_tacacs_packet_log_hex(&default_log, u->packet);
654
655 return 0;
656}
657
658
659/** Revive a connection after "revive_interval"
660 *
661 */
662static void revive_timeout(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
663{
664 trunk_connection_t *tconn = talloc_get_type_abort(uctx, trunk_connection_t);
665 tcp_handle_t *h = talloc_get_type_abort(tconn->conn->h, tcp_handle_t);
666
667 INFO("%s - Reviving connection %s", h->module_name, h->name);
669}
670
671/** Mark a connection dead after "zombie_interval"
672 *
673 */
674static void zombie_timeout(fr_timer_list_t *tl, fr_time_t now, void *uctx)
675{
676 trunk_connection_t *tconn = talloc_get_type_abort(uctx, trunk_connection_t);
677 tcp_handle_t *h = talloc_get_type_abort(tconn->conn->h, tcp_handle_t);
678
679 INFO("%s - No replies during 'zombie_period', marking connection %s as dead", h->module_name, h->name);
680
681 /*
682 * Don't use this connection, and re-queue all of its
683 * requests onto other connections.
684 */
687
688 /*
689 * Revive the connection after a time.
690 */
691 if (fr_timer_at(h, tl, &h->zombie_ev,
692 fr_time_add(now, h->inst->parent->revive_interval), false, revive_timeout, h) < 0) {
693 ERROR("Failed inserting revive timeout for connection");
695 }
696}
697
698
699/** See if the connection is zombied.
700 *
701 * We check for zombie when major events happen:
702 *
703 * 1) request hits its final timeout
704 * 2) request timer hits, and it needs to be retransmitted
705 * 3) a DUP packet comes in, and the request needs to be retransmitted
706 * 4) we're sending a packet.
707 *
708 * There MIGHT not be retries configured, so we MUST check for zombie
709 * when any new packet comes in. Similarly, there MIGHT not be new
710 * packets, but retries are configured, so we have to check there,
711 * too.
712 *
713 * Also, the socket might not be writable for a while. There MIGHT
714 * be a long time between getting the timer / DUP signal, and the
715 * request finally being written to the socket. So we need to check
716 * for zombie at BOTH the timeout and the mux / write function.
717 *
718 * @return
719 * - true if the connection is zombie.
720 * - false if the connection is not zombie.
721 */
723{
724 tcp_handle_t *h = talloc_get_type_abort(tconn->conn->h, tcp_handle_t);
725
726 /*
727 * If we're already zombie, don't go to zombie
728 *
729 */
730 if (h->zombie_ev) return true;
731
732 if (fr_time_eq(now, fr_time_wrap(0))) now = fr_time();
733
734 /*
735 * We received a reply since this packet was sent, the connection isn't zombie.
736 */
737 if (fr_time_gteq(h->last_reply, last_sent)) return false;
738
739 /*
740 * If we've seen ANY response in the allowed window, then the connection is still alive.
741 */
742 if (fr_time_gt(last_sent, fr_time_wrap(0)) &&
743 (fr_time_lt(fr_time_add(last_sent, h->inst->parent->response_window), now))) return false;
744
745 /*
746 * Mark the connection as inactive, but keep sending
747 * packets on it.
748 */
749 WARN("%s - Entering Zombie state - connection %s", h->module_name, h->name);
751
752 if (fr_timer_at(h, tl, &h->zombie_ev, fr_time_add(now, h->inst->parent->zombie_period),
753 false, zombie_timeout, h) < 0) {
754 ERROR("Failed inserting zombie timeout for connection");
756 }
757
758 return true;
759}
760
761/** Handle retries.
762 *
763 * Note that with TCP we don't actually retry on this particular connection, but the retry timer allows us to
764 * fail over from one connection to another when a connection fails.
765 */
766static void request_retry(fr_timer_list_t *tl, fr_time_t now, void *uctx)
767{
768 trunk_request_t *treq = talloc_get_type_abort(uctx, trunk_request_t);
769 tcp_request_t *req = talloc_get_type_abort(treq->preq, tcp_request_t);
770 tcp_result_t *r = talloc_get_type_abort(treq->rctx, tcp_result_t);
771 request_t *request = treq->request;
772 trunk_connection_t *tconn = treq->tconn;
773
774 fr_assert(treq->state == TRUNK_REQUEST_STATE_SENT); /* No other states should be timing out */
775 fr_assert(treq->preq); /* Must still have a protocol request */
776 fr_assert(tconn);
777
778 switch (fr_retry_next(&req->retry, now)) {
779 /*
780 * Queue the request for retransmission.
781 *
782 * @todo - set up "next" timer here, instead of in
783 * request_mux() ? That way we can catch the case of
784 * packets sitting in the queue for extended periods of
785 * time, and still run the timers.
786 */
789 return;
790
791 case FR_RETRY_MRD:
792 REDEBUG("Reached maximum_retransmit_duration (%pVs > %pVs), failing request",
794 break;
795
796 case FR_RETRY_MRC:
797 REDEBUG("Reached maximum_retransmit_count (%u > %u), failing request",
798 req->retry.count, req->retry.config->mrc);
799 break;
800 }
801
804
805 check_for_zombie(tl, tconn, now, req->retry.start);
806}
807
808CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
810 trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
811{
812 tcp_handle_t *h = talloc_get_type_abort(conn->h, tcp_handle_t);
813 rlm_tacacs_tcp_t const *inst = h->inst;
814 ssize_t sent;
815 uint16_t i, queued;
816 uint8_t const *written;
817 uint8_t *partial;
818
819 /*
820 * Encode multiple packets in preparation for transmission with write()
821 */
822 for (i = 0, queued = 0; (i < inst->max_send_coalesce); i++) {
823 trunk_request_t *treq;
824 tcp_request_t *req;
825 request_t *request;
826
827 if (unlikely(trunk_connection_pop_request(&treq, tconn) < 0)) return;
828
829 /*
830 * No more requests to send
831 */
832 if (!treq) break;
833
834 /*
835 * The partial write MUST be the first one popped off of the request list.
836 *
837 * If we have a partial packet, then we know that there's partial data in the output
838 * buffer. However, the request MAY still be freed or timed out before we can write the
839 * data. As a result, we ignore the tcp_request_t, and just keep writing the data.
840 */
841 if (treq->state == TRUNK_REQUEST_STATE_PARTIAL) {
842 fr_assert(h->send.read == h->send.data);
843 fr_assert(h->send.write > h->send.read);
844
845 fr_assert(i == 0);
846
847 h->coalesced[0] = treq;
848 goto next;
849 }
850
851 /*
852 * The request must still be pending.
853 */
855
856 request = treq->request;
857 req = talloc_get_type_abort(treq->preq, tcp_request_t);
858
859 /*
860 * We'd like to retransmit the packet on this connection, but it's TCP so we don't.
861 *
862 * The retransmission timers are really there to move the packet to a new connection if
863 * the current connection is dead.
864 */
865 if (req->outstanding) continue;
866
867 /*
868 * Not enough room for a full-sized packet, stop encoding packets
869 */
870 if ((h->send.end - h->send.write) < inst->max_packet_size) {
871 break;
872 }
873
874 /*
875 * Start retransmissions from when the socket is writable.
876 */
877 fr_retry_init(&req->retry, fr_time(), &h->inst->parent->retry);
880
881 /*
882 * Set up the packet for encoding.
883 */
884 req->id = h->id;
885 h->tconn = tconn;
886
887 h->tracking[req->id] = treq;
888 h->id += 2;
889 h->active++;
890
891 RDEBUG("Sending %s ID %d length %ld over connection %s",
892 fr_tacacs_packet_names[req->code], req->id, req->packet_len, h->name);
893
894 if (encode(h, request, req) < 0) {
895 /*
896 * Need to do this because request_conn_release
897 * may not be called.
898 */
899 tcp_request_reset(h, req);
901 continue;
902 }
903 RHEXDUMP3(req->packet, req->packet_len, "Encoded packet");
904
905 log_request_pair_list(L_DBG_LVL_2, request, NULL, &request->request_pairs, NULL);
906
907 /*
908 * Remember that we've encoded this packet.
909 */
910 h->coalesced[queued] = treq;
911 h->send.write += req->packet_len;
912
913 fr_assert(h->send.write <= h->send.end);
914
915 /*
916 * If we just hit this limit, stop using the connection.
917 *
918 * When we've received all replies (or timeouts), we'll close the connections.
919 */
920 if (h->id > 255) {
922 }
923
924 next:
925 /*
926 * Tell the trunk API that this request is now in
927 * the "sent" state. And we don't want to see
928 * this request again. The request hasn't actually
929 * been sent, but it's the only way to get at the
930 * next entry in the heap.
931 */
933 queued++;
934 }
935
936 if (queued == 0) return;
937
938 /*
939 * Verify nothing accidentally freed the connection handle
940 */
941 (void)talloc_get_type_abort(h, tcp_handle_t);
942
943 /*
944 * Send the packets as one system call.
945 */
946 sent = write(h->fd, h->send.read, h->send.write - h->send.read);
947 if (sent < 0) { /* Error means no messages were sent */
948 /*
949 * Temporary conditions
950 */
951 switch (errno) {
952#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
953 case EWOULDBLOCK: /* No outbound packet buffers, maybe? */
954#endif
955 case EAGAIN: /* No outbound packet buffers, maybe? */
956 case EINTR: /* Interrupted by signal */
957 case ENOBUFS: /* No outbound packet buffers, maybe? */
958 case ENOMEM: /* malloc failure in kernel? */
959 WARN("%s - Failed sending data over connection %s: %s",
960 h->module_name, h->name, fr_syserror(errno));
961 sent = 0;
962 break;
963
964 /*
965 * Will re-queue any 'sent' requests, so we don't
966 * have to do any cleanup.
967 */
968 default:
969 ERROR("%s - Failed sending data over connection %s: %s",
970 h->module_name, h->name, fr_syserror(errno));
972 return;
973 }
974 }
975
976 written = h->send.read + sent;
977 partial = h->send.read;
978
979 /*
980 * For all messages that were actually sent by writev()
981 * start the request timer.
982 */
983 for (i = 0; i < queued; i++) {
984 trunk_request_t *treq = h->coalesced[i];
985 tcp_request_t *req;
986 request_t *request;
987
988 /*
989 * We *think* we sent this, but we might not had :(
990 */
991 fr_assert(treq->state == TRUNK_REQUEST_STATE_SENT);
992
993 request = treq->request;
994 req = talloc_get_type_abort(treq->preq, tcp_request_t);
995
996 /*
997 * This packet ends before the piece we've
998 * written, so we've written all of it.
999 */
1000 if (req->packet + req->packet_len <= written) {
1001 h->last_sent = req->retry.start;
1003
1004 if (fr_timer_at(req, el->tl, &req->ev, req->retry.next, false, request_retry, treq) < 0) {
1005 RERROR("Failed inserting retransmit timeout for connection");
1007 }
1008
1009 /*
1010 * If the packet doesn't get a response, then the timer will hit
1011 * and will retransmit.
1012 */
1013 req->outstanding = true;
1014 continue;
1015 }
1016
1017 /*
1018 * The packet starts before the piece we've written, BUT ends after the written piece.
1019 *
1020 * We only wrote part of this packet, remember the partial packet we wrote. Note that
1021 * we only track the packet data, and not the tcp_request_t. The underlying request (and
1022 * u) may disappear at any time, even if there's still data in the buffer.
1023 *
1024 * Then, signal that isn't a partial packet, and stop processing the queue, as we know
1025 * that the next packet wasn't written.
1026 */
1027 if (req->packet < written) {
1028 size_t skip = written - req->packet;
1029 size_t left = req->packet_len - skip;
1030
1031 fr_assert(req->packet + req->packet_len > written);
1032
1033 memmove(h->send.data, req->packet, left);
1034
1035 fr_assert(h->send.read == h->send.data);
1036 partial = h->send.data + left;
1037 req->outstanding = true;
1038
1040 continue;
1041 }
1042
1043 /*
1044 * The packet starts after the piece we've written, so we haven't written any of it.
1045 *
1046 * Requests that weren't sent get re-enqueued. Which means that they get re-encoded, but
1047 * oh well.
1048 *
1049 * The cancel logic runs as per-normal and cleans up
1050 * the request ready for sending again...
1051 */
1053 fr_assert(!req->outstanding); /* must have called request_requeue() */
1054 }
1055
1056 /*
1057 * Remember where to write the next packet. Either at the start of the buffer, or after the one
1058 * which was partially written.
1059 */
1060 h->send.write = partial;
1061}
1062
1064{
1065 tcp_handle_t *h = talloc_get_type_abort(conn->h, tcp_handle_t);
1066 bool do_read = true;
1067
1068 DEBUG3("%s - Reading data for connection %s", h->module_name, h->name);
1069
1070 while (true) {
1071 ssize_t slen;
1072 size_t available, used, packet_len;
1073
1074 trunk_request_t *treq;
1075 request_t *request;
1076 tcp_request_t *req;
1077 tcp_result_t *r;
1078 uint8_t code = 0;
1079 fr_pair_list_t reply;
1080
1081 /*
1082 * Ensure that we can read at least one max-sized packet.
1083 *
1084 * If not, move the trailing bytes to the start of the buffer, and reset the read/write
1085 * pointers to the start of the buffer. Note that the read buffer has to be at least 2x
1086 * max_packet_size.
1087 */
1088 available = h->recv.end - h->recv.read;
1089 if (available < h->inst->max_packet_size) {
1091
1092 used = h->recv.write - h->recv.read;
1093
1094 memcpy(h->recv.data, h->recv.read, used);
1095 h->recv.read = h->recv.data;
1096 h->recv.write = h->recv.read + used;
1097 }
1098
1099 /*
1100 * Read as much data as possible.
1101 *
1102 * We don't need to call read() on every round through the loop. Instead, we call it
1103 * only when this function first gets called, OR if the read stopped at the end of the
1104 * buffer.
1105 *
1106 * This allows us to read a large amount of data at once, and then process multiple
1107 * packets without calling read() too many times.
1108 */
1109 if (do_read) {
1110 slen = read(h->fd, h->recv.write, h->recv.end - h->recv.write);
1111 if (slen < 0) {
1112 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) return;
1113
1114 ERROR("%s - Failed reading response from socket: %s",
1115 h->module_name, fr_syserror(errno));
1117 return;
1118 }
1119
1120 h->recv.write += slen;
1121 do_read = (h->recv.write == h->recv.end);
1122 }
1123
1124 used = h->recv.write - h->recv.read;
1125
1126 /*
1127 * We haven't received a full header, read more or return.
1128 */
1129 if (used < sizeof(fr_tacacs_packet_hdr_t)) {
1130 if (do_read) continue;
1131 return;
1132 }
1133
1134 /*
1135 * The packet contains a 4 octet length in the
1136 * header, but the header bytes aren't included
1137 * in the 4 octet length field.
1138 */
1139 packet_len = fr_nbo_to_uint32(h->recv.read + 8) + FR_HEADER_LENGTH;
1140
1141 /*
1142 * The packet is too large, reject it.
1143 */
1144 if (packet_len > h->inst->max_packet_size) {
1145 ERROR("%s - Packet is larger than max_packet_size",
1146 h->module_name);
1148 return;
1149 }
1150
1151 /*
1152 * We haven't received the full packet, read more or return.
1153 */
1154 if (used < packet_len) {
1155 if (do_read) continue;
1156 return;
1157 }
1158
1159 fr_assert(h->recv.read + packet_len <= h->recv.end);
1160
1161 /*
1162 * TACACS+ doesn't care about packet codes. All packet of the codes share the same ID
1163 * space.
1164 */
1165 treq = h->tracking[h->recv.read[1]];
1166 if (!treq) {
1167 WARN("%s - Ignoring reply with ID %i that arrived too late",
1168 h->module_name, h->recv.data[1]);
1169
1170 h->recv.read += packet_len;
1171 continue;
1172 }
1173
1174 treq = talloc_get_type_abort(treq, trunk_request_t);
1175 request = treq->request;
1176 fr_assert(request != NULL);
1177 req = talloc_get_type_abort(treq->preq, tcp_request_t);
1178 r = talloc_get_type_abort(treq->rctx, tcp_result_t);
1179
1180 fr_pair_list_init(&reply);
1181
1182 /*
1183 * Validate and decode the incoming packet
1184 */
1185 slen = decode(request->reply_ctx, &reply, &code, h, request, req, h->recv.read, packet_len);
1186 if (slen < 0) {
1187 // @todo - give real decode error?
1189 return;
1190 }
1191 h->recv.read += packet_len;
1192
1193 /*
1194 * Only valid packets are processed.
1195 */
1196 h->last_reply = fr_time();
1197
1198 treq->request->reply->code = code;
1199
1200 r->rcode = tacacs_code_to_rcode[code];
1201 fr_pair_list_append(&request->reply_pairs, &reply);
1203 }
1204}
1205
1206/** Remove the request from any tracking structures
1207 *
1208 * Frees encoded packets if the request is being moved to a new connection
1209 */
1210static void request_cancel(connection_t *conn, void *preq_to_reset,
1211 trunk_cancel_reason_t reason, UNUSED void *uctx)
1212{
1213 tcp_request_t *req = talloc_get_type_abort(preq_to_reset, tcp_request_t);
1214
1215 /*
1216 * Request has been requeued on the same
1217 * connection due to timeout or DUP signal.
1218 */
1219 if (reason == TRUNK_CANCEL_REASON_REQUEUE) {
1220 tcp_handle_t *h = talloc_get_type_abort(conn->h, tcp_handle_t);
1221
1222 tcp_request_reset(h, req);
1223 }
1224
1225 /*
1226 * Other cancellations are dealt with by
1227 * request_conn_release as the request is removed
1228 * from the trunk.
1229 */
1230}
1231
1232/** Clear out anything associated with the handle from the request
1233 *
1234 */
1235static void request_conn_release(connection_t *conn, void *preq_to_reset, UNUSED void *uctx)
1236{
1237 tcp_request_t *req = talloc_get_type_abort(preq_to_reset, tcp_request_t);
1238 tcp_handle_t *h = talloc_get_type_abort(conn->h, tcp_handle_t);
1239
1240 if (req->packet) tcp_request_reset(h, req);
1241
1242 /*
1243 * If there are no outstanding tracking entries
1244 * allocated then the connection is "idle".
1245 *
1246 * @todo - enable idle timeout?
1247 */
1248 if (!h->active) h->last_idle = fr_time();
1249}
1250
1251/** Write out a canned failure
1252 *
1253 */
1254static void request_fail(request_t *request, NDEBUG_UNUSED void *preq, void *rctx,
1255 NDEBUG_UNUSED trunk_request_state_t state, UNUSED void *uctx)
1256{
1257 tcp_result_t *r = talloc_get_type_abort(rctx, tcp_result_t);
1258#ifndef NDEBUG
1259 tcp_request_t *req = talloc_get_type_abort(preq, tcp_request_t);
1260#endif
1261
1262 fr_assert(!fr_timer_armed(req->ev)); /* Dealt with by request_conn_release */
1263
1265
1267 r->treq = NULL;
1268
1270}
1271
1272/** Response has already been written to the rctx at this point
1273 *
1274 */
1275static void request_complete(request_t *request, NDEBUG_UNUSED void *preq, void *rctx, UNUSED void *uctx)
1276{
1277 tcp_result_t *r = talloc_get_type_abort(rctx, tcp_result_t);
1278#ifndef NDEBUG
1279 tcp_request_t *req = talloc_get_type_abort(preq, tcp_request_t);
1280#endif
1281
1282 fr_assert(!req->packet && !fr_timer_armed(req->ev)); /* Dealt with by request_conn_release */
1283
1284 r->treq = NULL;
1285
1287}
1288
1289/** Explicitly free resources associated with the protocol request
1290 *
1291 */
1292static void request_free(UNUSED request_t *request, void *preq_to_free, UNUSED void *uctx)
1293{
1294 tcp_request_t *req = talloc_get_type_abort(preq_to_free, tcp_request_t);
1295
1296 fr_assert(!req->packet && !fr_timer_armed(req->ev)); /* Dealt with by request_conn_release */
1297
1298 talloc_free(req);
1299}
1300
1301/** Resume execution of the request, returning the rcode set during trunk execution
1302 *
1303 */
1305{
1306 tcp_result_t *r = talloc_get_type_abort(mctx->rctx, tcp_result_t);
1307 rlm_rcode_t rcode = r->rcode;
1308
1309 talloc_free(r);
1310
1311 RETURN_UNLANG_RCODE(rcode);
1312}
1313
1314static void mod_signal(module_ctx_t const *mctx, UNUSED request_t *request, fr_signal_t action)
1315{
1316// tcp_thread_t *t = talloc_get_type_abort(mctx->thread, tcp_thread_t);
1317 tcp_result_t *r = talloc_get_type_abort(mctx->rctx, tcp_result_t);
1318
1319 /*
1320 * If we don't have a treq associated with the
1321 * rctx it's likely because the request was
1322 * scheduled, but hasn't yet been resumed, and
1323 * has received a signal, OR has been resumed
1324 * and immediately cancelled as the event loop
1325 * is exiting, in which case
1326 * unlang_request_is_scheduled will return false
1327 * (don't use it).
1328 */
1329 if (!r->treq) {
1330 talloc_free(r);
1331 return;
1332 }
1333
1334 switch (action) {
1335 /*
1336 * The request is being cancelled, tell the
1337 * trunk so it can clean up the treq.
1338 */
1339 case FR_SIGNAL_CANCEL:
1341 r->treq = NULL;
1342 talloc_free(r); /* Should be freed soon anyway, but better to be explicit */
1343 return;
1344
1345 /*
1346 * Requeue the request on the same connection
1347 * causing a "retransmission" if the request
1348 * has already been sent out.
1349 */
1350 case FR_SIGNAL_DUP:
1351 /*
1352 * Retransmit the current request on the same connection.
1353 *
1354 * If it's zombie, we still resend it. If the
1355 * connection is dead, then a callback will move
1356 * this request to a new connection.
1357 */
1359 return;
1360
1361 default:
1362 return;
1363 }
1364}
1365
1366#ifndef NDEBUG
1367/** Free a tcp_result_t
1368 *
1369 * Allows us to set break points for debugging.
1370 */
1372{
1373 trunk_request_t *treq;
1374 tcp_request_t *req;
1375
1376 if (!r->treq) return 0;
1377
1378 treq = talloc_get_type_abort(r->treq, trunk_request_t);
1379 req = talloc_get_type_abort(treq->preq, tcp_request_t);
1380
1381 fr_assert_msg(!req->ev, "tcp_result_t freed with active timer");
1382
1383 return 0;
1384}
1385#endif
1386
1387static unlang_action_t mod_enqueue(unlang_result_t *p_result, void **rctx_out, UNUSED void *instance, void *thread, request_t *request)
1388{
1389 tcp_thread_t *t = talloc_get_type_abort(thread, tcp_thread_t);
1390 tcp_result_t *r;
1391 tcp_request_t *req;
1392 trunk_request_t *treq;
1394
1395 fr_assert(FR_TACACS_PACKET_CODE_VALID(request->packet->code));
1396
1397 treq = trunk_request_alloc(t->trunk, request);
1398 if (!treq) RETURN_UNLANG_FAIL;
1399
1400 MEM(r = talloc_zero(request, tcp_result_t));
1401#ifndef NDEBUG
1402 talloc_set_destructor(r, _tcp_result_free);
1403#endif
1404
1405 /*
1406 * Can't use compound literal - const issues.
1407 */
1408 MEM(req = talloc_zero(treq, tcp_request_t));
1409 req->code = request->packet->code;
1410 req->priority = request->priority;
1411 req->recv_time = request->async->recv_time;
1412
1414
1415 q = trunk_request_enqueue(&treq, t->trunk, request, req, r);
1416 if (q < 0) {
1417 fr_assert(!req->packet); /* Should not have been fed to the muxer */
1418 trunk_request_free(&treq); /* Return to the free list */
1419 fail:
1420 talloc_free(r);
1422 }
1423
1424 /*
1425 * All destinations are down.
1426 */
1427 if (q == TRUNK_ENQUEUE_IN_BACKLOG) {
1428 RDEBUG("All destinations are down - cannot send packet");
1429 goto fail;
1430 }
1431
1432 r->treq = treq; /* Remember for signalling purposes */
1433
1434 *rctx_out = r;
1435
1436 return UNLANG_ACTION_YIELD;
1437}
1438
1439/** Instantiate thread data for the submodule.
1440 *
1441 */
1443{
1444 rlm_tacacs_tcp_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_tacacs_tcp_t);
1445 tcp_thread_t *thread = talloc_get_type_abort(mctx->thread, tcp_thread_t);
1446
1447 static trunk_io_funcs_t io_funcs = {
1449 .connection_notify = thread_conn_notify,
1450 .request_prioritise = request_prioritise,
1451 .request_mux = request_mux,
1452 .request_demux = request_demux,
1453 .request_conn_release = request_conn_release,
1454 .request_complete = request_complete,
1455 .request_fail = request_fail,
1456 .request_cancel = request_cancel,
1457 .request_free = request_free
1458 };
1459
1460 thread->el = mctx->el;
1461 thread->inst = inst;
1462 thread->trunk = trunk_alloc(thread, mctx->el, &io_funcs,
1463 &inst->parent->trunk_conf, inst->parent->name, thread, false, inst->trigger_args);
1464 if (!thread->trunk) return -1;
1465
1466 return 0;
1467}
1468
1469static int mod_instantiate(module_inst_ctx_t const *mctx)
1470{
1471 rlm_tacacs_t *parent = talloc_get_type_abort(mctx->mi->parent->data, rlm_tacacs_t);
1472 rlm_tacacs_tcp_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_tacacs_tcp_t);
1473 CONF_SECTION *conf = mctx->mi->conf;
1474 char *server = NULL;
1475
1476 if (!parent) {
1477 ERROR("IO module cannot be instantiated directly");
1478 return -1;
1479 }
1480
1481 inst->parent = parent;
1482
1483 /*
1484 * Always need at least one mmsgvec
1485 */
1486 if (inst->max_send_coalesce == 0) inst->max_send_coalesce = 1;
1487
1488 /*
1489 * Ensure that we have a destination address.
1490 */
1491 if (inst->dst_ipaddr.af == AF_UNSPEC) {
1492 cf_log_err(conf, "A value must be given for 'ipaddr'");
1493 return -1;
1494 }
1495
1496 /*
1497 * If src_ipaddr isn't set, make sure it's INADDR_ANY, of
1498 * the same address family as dst_ipaddr.
1499 */
1500 if (inst->src_ipaddr.af == AF_UNSPEC) {
1501 memset(&inst->src_ipaddr, 0, sizeof(inst->src_ipaddr));
1502
1503 inst->src_ipaddr.af = inst->dst_ipaddr.af;
1504
1505 if (inst->src_ipaddr.af == AF_INET) {
1506 inst->src_ipaddr.prefix = 32;
1507 } else {
1508 inst->src_ipaddr.prefix = 128;
1509 }
1510 }
1511
1512 else if (inst->src_ipaddr.af != inst->dst_ipaddr.af) {
1513 cf_log_err(conf, "The 'ipaddr' and 'src_ipaddr' configuration items must "
1514 "be both of the same address family");
1515 return -1;
1516 }
1517
1518 if (!inst->dst_port) {
1519 cf_log_err(conf, "A value must be given for 'port'");
1520 return -1;
1521 }
1522
1523 /*
1524 * Clamp max_packet_size first before checking recv_buff and send_buff
1525 */
1526 FR_INTEGER_BOUND_CHECK("max_packet_size", inst->max_packet_size, >=, ((255 + (int) sizeof(fr_tacacs_packet_t)) & 0xffffff00));
1527 FR_INTEGER_BOUND_CHECK("max_packet_size", inst->max_packet_size, <=, 65535);
1528
1529
1530 if (inst->recv_buff_is_set) {
1531 FR_INTEGER_BOUND_CHECK("recv_buff", inst->recv_buff, >=, inst->max_packet_size);
1532 FR_INTEGER_BOUND_CHECK("recv_buff", inst->recv_buff, <=, (1 << 30));
1533 }
1534
1535 if (inst->send_buff_is_set) {
1536 FR_INTEGER_BOUND_CHECK("send_buff", inst->send_buff, >=, inst->max_packet_size);
1537 FR_INTEGER_BOUND_CHECK("send_buff", inst->send_buff, <=, (1 << 30));
1538 }
1539
1540
1541 /*
1542 * Empty secrets don't exist
1543 */
1544 if (inst->secret && !*inst->secret) {
1545 talloc_const_free(inst->secret);
1546 inst->secret = NULL;
1547 }
1548
1549 if (inst->secret) inst->secretlen = talloc_strlen(inst->secret);
1550
1551 if (!parent->trunk_conf.conn_triggers) return 0;
1552
1553 fr_value_box_aprint(inst, &server, fr_box_ipaddr(inst->dst_ipaddr), NULL);
1554
1555 MEM(inst->trigger_args = fr_pair_list_alloc(inst));
1556 if (module_trigger_args_build(inst->trigger_args, inst->trigger_args,
1559 .module = mctx->mi->module->name,
1560 .name = parent->name,
1561 .server = server,
1562 .port = inst->dst_port
1563 }) < 0) return -1;
1564
1565 return 0;
1566}
1567
1570 .common = {
1571 .magic = MODULE_MAGIC_INIT,
1572 .name = "tacacs_tcp",
1573 .inst_size = sizeof(rlm_tacacs_tcp_t),
1574
1575 .thread_inst_size = sizeof(tcp_thread_t),
1576 .thread_inst_type = "tcp_thread_t",
1577
1578 .config = module_config,
1579 .instantiate = mod_instantiate,
1580 .thread_instantiate = mod_thread_instantiate,
1581 },
1582 .enqueue = mod_enqueue,
1583 .signal = mod_signal,
1584 .resume = mod_resume,
1585};
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition action.h:35
@ UNLANG_ACTION_YIELD
Temporarily pause execution until an event occurs.
Definition action.h:41
#define RCSID(id)
Definition build.h:506
#define NDEBUG_UNUSED
Definition build.h:347
#define STRINGIFY(x)
Definition build.h:216
#define CC_NO_UBSAN(_sanitize)
Definition build.h:449
#define CMP_PREFER_LARGER(_a, _b)
Evaluates to -1 for a > b, and +1 for a < b.
Definition build.h:109
#define unlikely(_x)
Definition build.h:402
#define UNUSED
Definition build.h:336
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:657
#define FR_INTEGER_BOUND_CHECK(_name, _var, _op, _bound)
Definition cf_parse.h:517
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:280
#define FR_CONF_OFFSET_IS_SET(_name, _type, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct,...
Definition cf_parse.h:294
#define FR_CONF_OFFSET_TYPE_FLAGS(_name, _type, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:238
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:594
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:101
CONF_SECTION * cf_section_find(CONF_SECTION const *cs, char const *name1, char const *name2)
Find a CONF_SECTION with name1 and optionally name2.
Definition cf_util.c:1027
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition cf_util.c:683
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:285
#define cf_parent(_cf)
Definition cf_util.h:98
connection_state_t
Definition connection.h:47
@ CONNECTION_STATE_FAILED
Connection has failed.
Definition connection.h:56
@ CONNECTION_STATE_CONNECTING
Waiting for connection to establish.
Definition connection.h:52
@ CONNECTION_EXPIRED
Connection is being reconnected because it's at the end of its life.
Definition connection.h:86
@ CONNECTION_FAILED
Connection is being reconnected because it failed.
Definition connection.h:85
Holds a complete set of functions for a connection.
Definition connection.h:195
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
Definition dbuff.h:522
#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:212
#define MEM(x)
Definition debug.h:46
#define ERROR(fmt,...)
Definition dhcpclient.c:40
#define DEBUG(fmt,...)
Definition dhcpclient.c:38
fr_dict_attr_t const ** out
Where to write a pointer to the resolved fr_dict_attr_t.
Definition dict.h:292
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:305
#define DICT_AUTOLOAD_TERMINATOR
Definition dict.h:311
Specifies an attribute which must be present for the module to function.
Definition dict.h:291
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:304
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition dl_module.h:63
#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
@ FR_EVENT_FILTER_IO
Combined filter for read/write functions/.
Definition event.h:83
talloc_free(hp)
IPv4/6 prefix.
void unlang_interpret_mark_runnable(request_t *request)
Mark a request as resumable.
Definition interpret.c:1636
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:831
#define PERROR(_fmt,...)
Definition log.h:228
#define DEBUG3(_fmt,...)
Definition log.h:266
#define RERROR(fmt,...)
Definition log.h:310
#define DEBUG4(_fmt,...)
Definition log.h:267
#define RPERROR(fmt,...)
Definition log.h:314
#define RPEDEBUG(fmt,...)
Definition log.h:388
#define RHEXDUMP3(_data, _len, _fmt,...)
Definition log.h:717
#define fr_time()
Definition event.c:60
int fr_event_fd_delete(fr_event_list_t *el, int fd, fr_event_filter_t filter)
Remove a file descriptor from the event loop.
Definition event.c:1203
Stores all information relating to an event list.
Definition event.c:377
@ L_DBG_LVL_2
2nd highest priority debug messages (-xx | -X).
Definition log.h:68
unsigned short uint16_t
@ FR_TYPE_IPV4_ADDR
32 Bit IPv4 Address.
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_STRUCT
like TLV, but without T or L, and fixed-width children
@ FR_TYPE_IPV6_ADDR
128 Bit IPv6 Address.
@ FR_TYPE_COMBO_IP_ADDR
IPv4 or IPv6 address depending on length.
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
unsigned long int size_t
#define UINT8_MAX
static size_t used
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
module_instance_t * mi
Instance of the module being instantiated.
Definition module_ctx.h:51
Temporary structure to hold arguments for module calls.
Definition module_ctx.h:41
Temporary structure to hold arguments for instantiation calls.
Definition module_ctx.h:50
Temporary structure to hold arguments for thread_instantiation calls.
Definition module_ctx.h:63
static const trunk_io_funcs_t io_funcs
Definition bio.c:2646
static uint32_t fr_nbo_to_uint32(uint8_t const data[static sizeof(uint32_t)])
Read an unsigned 32bit integer from wire format (big endian)
Definition nbo.h:167
fr_pair_t * fr_pair_find_by_da_nested(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find a pair with a matching fr_dict_attr_t, by walking the nested fr_dict_attr_t tree.
Definition pair.c:784
int8_t fr_pair_cmp_by_parent_num(void const *a, void const *b)
Order attributes by their parent(s), attribute number, and tag.
Definition pair.c:1928
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
fr_pair_list_t * fr_pair_list_alloc(TALLOC_CTX *ctx)
Allocate a new pair list on the heap.
Definition pair.c:119
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
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
char * fr_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Special version of asprintf which implements custom format specifiers.
Definition print.c:883
char const * fr_tacacs_packet_names[FR_TACACS_CODE_MAX]
Definition base.c:119
ssize_t fr_tacacs_decode(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *vendor, uint8_t const *buffer, size_t buffer_len, const uint8_t *original, char const *const secret, size_t secret_len, int *code)
Decode a TACACS+ packet.
Definition decode.c:415
ssize_t fr_tacacs_encode(fr_dbuff_t *dbuff, uint8_t const *original_packet, char const *secret, size_t secret_len, unsigned int code, fr_pair_list_t *vps)
Encode VPS into a raw TACACS packet.
Definition encode.c:368
#define fr_assert(_expr)
Definition rad_assert.h:37
static char * secret
#define REDEBUG(fmt,...)
#define RDEBUG(fmt,...)
#define WARN(fmt,...)
#define INFO(fmt,...)
Definition radict.c:63
static rs_t * conf
Definition radsniff.c:52
uint32_t fr_rand(void)
Return a 32-bit random number.
Definition rand.c:104
#define RETURN_UNLANG_RCODE(_rcode)
Definition rcode.h:61
#define RETURN_UNLANG_FAIL
Definition rcode.h:63
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
module_t common
Common fields to all loadable modules.
Definition rlm_tacacs.h:74
fr_retry_config_t retry
retries shared by all packet types
Definition rlm_tacacs.h:57
char const * name
Definition rlm_tacacs.h:45
fr_time_delta_t revive_interval
Definition rlm_tacacs.h:51
fr_time_delta_t zombie_period
Definition rlm_tacacs.h:50
fr_time_delta_t response_window
Definition rlm_tacacs.h:49
Public structure describing an I/O path for an outgoing socket.
Definition rlm_tacacs.h:73
static int8_t request_prioritise(void const *one, void const *two)
fr_retry_t retry
retransmission timers
uint8_t code
Packet code.
uint8_t * write
where we write data to
static fr_dict_attr_t const * attr_packet_type
static bool check_for_zombie(fr_timer_list_t *tl, trunk_connection_t *tconn, fr_time_t now, fr_time_t last_sent)
See if the connection is zombied.
CONF_SECTION * config
bool send_buff_is_set
Whether we were provided with a send_buf.
size_t send_buff_actual
What we believe the maximum SO_SNDBUF size to be.
size_t secretlen
length of secret
static void request_demux(UNUSED fr_event_list_t *el, trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
static fr_dict_attr_t const * attr_session_id
static void request_mux(fr_event_list_t *el, trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
char const * interface
Interface to bind to.
char const * module_name
the module that opened the connection
int fd
File descriptor.
static ssize_t decode(TALLOC_CTX *ctx, fr_pair_list_t *reply, uint8_t *response_code, tcp_handle_t *h, request_t *request, tcp_request_t *req, uint8_t *data, size_t data_len)
Decode response packet data, extracting relevant information and validating the packet.
uint8_t * data
actual data
uint8_t id
Last ID assigned to this packet.
static fr_dict_attr_t const * attr_packet_hdr
uint16_t src_port
Source port specific to this connection.
size_t packet_len
Length of the packet.
fr_pair_list_t * trigger_args
Pairs passed to trigger request.
fr_dict_autoload_t rlm_tacacs_tcp_dict[]
static void conn_error(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, int fd_errno, void *uctx)
Connection errored.
trunk_connection_t * tconn
trunk connection
static void mod_signal(module_ctx_t const *mctx, UNUSED request_t *request, fr_signal_t action)
trunk_request_t * treq
static void conn_close(UNUSED fr_event_list_t *el, void *handle, UNUSED void *uctx)
Shutdown/close a file descriptor.
static void tcp_request_reset(tcp_handle_t *h, tcp_request_t *req)
Clear out any connection specific resources from a tcp request.
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)
fr_time_t last_reply
When we last received a reply.
char const * secret
Shared secret.
static unlang_action_t mod_enqueue(unlang_result_t *p_result, void **rctx_out, UNUSED void *instance, void *thread, request_t *request)
static int encode(tcp_handle_t *h, request_t *request, tcp_request_t *req)
fr_time_t recv_time
copied from request->async->recv_time
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.
uint16_t dst_port
Port of the home server.
static fr_dict_t const * dict_tacacs
uint32_t send_buff
How big the kernel's send buffer should be.
static void request_retry(fr_timer_list_t *tl, fr_time_t now, void *uctx)
Handle retries.
fr_time_t mrs_time
Most recent sent time which had a reply.
uint8_t * end
end of the buffer
static void request_free(UNUSED request_t *request, void *preq_to_free, UNUSED void *uctx)
Explicitly free resources associated with the protocol request.
rlm_tacacs_tcp_t const * inst
Our module instance.
uint32_t recv_buff
How big the kernel's receive buffer should be.
int id
starts at 1.
rlm_tacacs_io_t rlm_tacacs_tcp
static int _tcp_handle_free(tcp_handle_t *h)
Free a connection handle, closing associated resources.
fr_time_t last_idle
last time we had nothing to do
uint32_t max_packet_size
Our max packet size. may be different from the parent.
fr_ipaddr_t src_ipaddr
IP we open our socket on.
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.
uint32_t session_id
for TACACS+ "security".
rlm_rcode_t rcode
from the transport
bool recv_buff_is_set
Whether we were provided with a recv_buf.
bool outstanding
are we waiting for a reply?
uint32_t max_packet_size
Maximum packet size.
char const * name
From IP PORT to IP PORT.
static void zombie_timeout(fr_timer_list_t *tl, fr_time_t now, void *uctx)
Mark a connection dead after "zombie_interval".
static int mod_thread_instantiate(module_thread_inst_ctx_t const *mctx)
Instantiate thread data for the submodule.
trunk_conf_t trunk_conf
trunk configuration
fr_timer_t * ev
timer for retransmissions
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.
trunk_t * trunk
trunk handler
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)
uint8_t * read
where we read data from
trunk_request_t ** coalesced
Outbound coalesced requests.
static rlm_rcode_t tacacs_code_to_rcode[FR_TACACS_CODE_MAX]
static connection_state_t conn_init(void **h_out, connection_t *conn, void *uctx)
Initialise a new outbound connection.
rlm_tacacs_t * parent
rlm_tacacs instance.
static void revive_timeout(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
Revive a connection after "revive_interval".
static int _tcp_result_free(tcp_result_t *r)
Free a tcp_result_t.
fr_dict_attr_autoload_t rlm_tacacs_tcp_dict_attr[]
static const conf_parser_t module_config[]
uint8_t * packet
Packet we write to the network.
uint32_t priority
copied from request->async->priority
fr_ipaddr_t src_ipaddr
Source IP address.
tcp_buffer_t send
send buffer
rlm_tacacs_tcp_t const * inst
our instance
tcp_buffer_t recv
receive buffer
int active
active packets
fr_ipaddr_t dst_ipaddr
IP of the home server.
static int mod_instantiate(module_inst_ctx_t const *mctx)
trunk_request_t * tracking[UINT8_MAX+1]
all sequential!
tcp_thread_t * thread
static void request_cancel(connection_t *conn, void *preq_to_reset, trunk_cancel_reason_t reason, UNUSED void *uctx)
Remove the request from any tracking structures.
fr_event_list_t * el
Event list.
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.
uint16_t max_send_coalesce
Maximum number of packets to coalesce into one mmsg call.
fr_time_t first_sent
first time we sent a packet since going idle
fr_time_t last_sent
last time we sent a packet.
fr_timer_t * zombie_ev
Zombie timeout.
Static configuration for the module.
Track the handle, which is tightly correlated with the FD.
Connect request_t to local tracking structure.
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.
char const * name
Instance name e.g. user_database.
Definition module.h:357
CONF_SECTION * conf
Module's instance configuration.
Definition module.h:351
size_t inst_size
Size of the module's instance data.
Definition module.h:212
void * data
Module's instance data.
Definition module.h:293
module_instance_t const * parent
Parent module's instance (if any).
Definition module.h:359
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
int fr_socket_client_tcp(char const *ifname, fr_ipaddr_t *src_ipaddr, fr_ipaddr_t const *dst_ipaddr, uint16_t dst_port, bool async)
Establish a connected TCP socket.
Definition socket.c:708
eap_aka_sim_process_conf_t * inst
fr_pair_t * vp
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 FR_HEADER_LENGTH
Definition tacacs.h:26
#define FR_TACACS_PACKET_CODE_VALID(_code)
Definition tacacs.h:322
@ FR_TACACS_CODE_ACCT_ERROR
Definition tacacs.h:315
@ FR_TACACS_CODE_AUTH_GETDATA
Definition tacacs.h:298
@ FR_TACACS_CODE_AUTH_RESTART
Definition tacacs.h:301
@ FR_TACACS_CODE_AUTZ_PASS_REPLACE
Definition tacacs.h:309
@ FR_TACACS_CODE_MAX
Definition tacacs.h:317
@ FR_TACACS_CODE_AUTH_GETUSER
Definition tacacs.h:299
@ FR_TACACS_CODE_AUTH_GETPASS
Definition tacacs.h:300
@ FR_TACACS_CODE_AUTZ_FAIL
Definition tacacs.h:310
@ FR_TACACS_CODE_AUTH_PASS
Definition tacacs.h:296
@ FR_TACACS_CODE_AUTZ_PASS_ADD
Definition tacacs.h:308
@ FR_TACACS_CODE_AUTH_FAIL
Definition tacacs.h:297
@ FR_TACACS_CODE_AUTH_ERROR
Definition tacacs.h:302
@ FR_TACACS_CODE_AUTZ_ERROR
Definition tacacs.h:311
@ FR_TACACS_CODE_ACCT_SUCCESS
Definition tacacs.h:314
#define FR_MAX_PACKET_SIZE
Definition tacacs.h:27
static int talloc_const_free(void const *ptr)
Free const'd memory.
Definition talloc.h:253
static size_t talloc_strlen(char const *s)
Returns the length of a talloc array containing a string.
Definition talloc.h:136
#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_delta_ispos(_a)
Definition time.h:290
#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
#define fr_time_lt(_a, _b)
Definition time.h:239
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_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
int module_trigger_args_build(TALLOC_CTX *ctx, fr_pair_list_t *list, CONF_SECTION *cs, module_trigger_args_t *args)
Build trigger args pair list for modules.
Definition trigger.c:497
Common values used by modules when building trigger args.
Definition trigger.h:42
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:4061
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:4078
void trunk_request_signal_partial(trunk_request_t *treq)
Signal a partial write.
Definition trunk.c:2069
void trunk_request_signal_fail(trunk_request_t *treq)
Signal that a trunk request failed.
Definition trunk.c:2172
trunk_request_t * trunk_request_alloc(trunk_t *trunk, request_t *request)
(Pre-)Allocate a new trunk request
Definition trunk.c:2518
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:2050
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:2633
trunk_enqueue_t trunk_request_requeue(trunk_request_t *treq)
Re-enqueue a request on the same connection.
Definition trunk.c:2722
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:3930
void trunk_request_signal_cancel(trunk_request_t *treq)
Cancel a trunk request.
Definition trunk.c:2192
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:4995
void trunk_request_free(trunk_request_t **treq_to_free)
If the trunk request is freed then update the target requests.
Definition trunk.c:2362
void trunk_connection_signal_inactive(trunk_connection_t *tconn)
Signal a trunk connection cannot accept more requests.
Definition trunk.c:3984
void trunk_request_signal_sent(trunk_request_t *treq)
Signal that the request was written to a connection successfully.
Definition trunk.c:2090
void trunk_request_signal_complete(trunk_request_t *treq)
Signal that a trunk request is complete.
Definition trunk.c:2134
void trunk_connection_signal_reconnect(trunk_connection_t *tconn, connection_reason_t reason)
Signal a trunk connection is no longer viable.
Definition trunk.c:4046
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:196
trunk_connection_alloc_t connection_alloc
Allocate a new connection_t.
Definition trunk.h:738
trunk_connection_event_t
What type of I/O events the trunk connection is currently interested in receiving.
Definition trunk.h:72
@ TRUNK_CONN_EVENT_BOTH
Trunk should be notified if a connection is readable or writable.
Definition trunk.h:79
@ TRUNK_CONN_EVENT_WRITE
Trunk should be notified if a connection is writable.
Definition trunk.h:77
@ TRUNK_CONN_EVENT_NONE
Don't notify the trunk on connection state changes.
Definition trunk.h:73
@ TRUNK_CONN_EVENT_READ
Trunk should be notified if a connection is readable.
Definition trunk.h:75
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_t
Definition trunk.h:149
@ TRUNK_ENQUEUE_IN_BACKLOG
Request should be enqueued in backlog.
Definition trunk.h:150
trunk_request_state_t
Used for sanity checks and to simplify freeing.
Definition trunk.h:162
@ TRUNK_REQUEST_STATE_PARTIAL
Some of the request was written to the socket, more of it should be written later.
Definition trunk.h:171
@ TRUNK_REQUEST_STATE_INIT
Initial state.
Definition trunk.h:163
@ TRUNK_REQUEST_STATE_PENDING
In the queue of a connection and is pending writing.
Definition trunk.h:169
@ TRUNK_REQUEST_STATE_SENT
Was written to a socket. Waiting for a response.
Definition trunk.h:173
Common configuration parameters for a trunk.
Definition trunk.h:225
I/O functions to pass to trunk_alloc.
Definition trunk.h:737
static fr_event_list_t * el
void fr_pair_list_sort(fr_pair_list_t *list, fr_cmp_t cmp)
Sort a doubly linked list of fr_pair_ts using merge sort.
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.
static fr_slen_t parent
Definition pair.h:858
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_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 next
when the next timer should be set
Definition retry.h:55
static fr_slen_t fr_value_box_aprint(TALLOC_CTX *ctx, char **out, fr_value_box_t const *data, fr_sbuff_escape_rules_t const *e_rules) 1(fr_value_box_print
#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