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