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