The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
master.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at 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: 0af9418fbc83ae14ca3855921ee006d130986e18 $
19 * @file io/master.c
20 * @brief Master IO handler
21 *
22 * @copyright 2018 Alan DeKok (aland@freeradius.org)
23 */
24#include <freeradius-devel/io/listen.h>
25#include <freeradius-devel/io/master.h>
26
27#include <freeradius-devel/util/syserror.h>
28
29FR_DLIST_TYPES(fr_io_nak_list)
30FR_DLIST_TYPEDEFS(fr_io_nak_list, fr_io_nak_list_head_t, fr_io_nak_list_entry_t)
31
32/** A negative cache entry.
33 *
34 * This is smaller and simpler than using a #fr_io_client_t. Plus, it doesn't affect the lifetime,
35 * ownership, or parenting of a #fr_io_client_t.
36 */
37typedef struct {
38 fr_ipaddr_t src_ipaddr; //!< the address which was NAK'd
39 fr_time_t expires; //!< when this entry stops being used
40 fr_io_nak_list_entry_t entry;
42
43FR_DLIST_FUNCS(fr_io_nak_list, fr_io_nak_entry_t, entry)
44
45typedef struct {
46 fr_event_list_t *el; //!< event list, for the master socket.
47 fr_network_t *nr; //!< network for the master socket
48
49 fr_trie_t *trie; //!< trie of clients
50 fr_trie_t *nak_trie; //!< trie of NAK clients
51 fr_io_nak_list_head_t nak_list; //!< time ordered list of NAK entries.
52
53 fr_heap_t *pending_clients; //!< heap of pending clients
54 fr_heap_t *alive_clients; //!< heap of active dynamic clients
55
56 fr_listen_instance_t *inst; //!< The master IO path
57 fr_listen_instance_t *child_inst; //!< The child (app_io) IO path
58 fr_schedule_t *sc; //!< the scheduler
59
60 uint32_t num_connections; //!< number of dynamic connections
61 uint32_t num_pending_packets; //!< number of pending packets
62 uint64_t client_id; //!< Unique client identifier.
63
64 struct {
65 fr_rate_limit_t accept_failed;
66 fr_rate_limit_t alloc_failed;
67 fr_rate_limit_t bad_type;
68 fr_rate_limit_t conn_alloc_failed;
69 fr_rate_limit_t max_connections;
70 fr_rate_limit_t queue_full;
71 fr_rate_limit_t repeat_nak;
72 fr_rate_limit_t too_many_pending;
73 fr_rate_limit_t tracking_failed;
74 fr_rate_limit_t unknown_client;
75 } rate_limit;
77
78/** A saved packet
79 *
80 */
89
90
91/** Client states
92 *
93 */
94typedef enum : uint8_t {
96 PR_CLIENT_STATIC, //!< static / global clients
97 PR_CLIENT_DYNAMIC, //!< dynamically defined client
98 PR_CLIENT_CONNECTED, //!< dynamically defined client in a connected socket
99 PR_CLIENT_PENDING, //!< dynamic client pending definition
101
102/*
103 * Dynamic clients are run through the normal src/process/foo state machine.
104 *
105 * request->async->packet_ctx is an fr_io_track_t
106 *
107 * track->dynamic is set to a non-zero value.
108 *
109 * The dynamic client code returns a buffer of 1 byte for a NAK.
110 *
111 * If the client creation is successful, then it does talloc(NULL, fr_client_t),
112 * fills out the structure, and sends the pointer in the buffer (8 bytes).
113 *
114 * This code will take over ownership of the structure, and
115 * create the dynamic client.
116 */
117
119
120/** Client definitions for master IO
121 *
122 */
124 fr_io_connection_t *connection; //!< parent connection
125 fr_io_client_state_t state; //!< state of this client
126 fr_ipaddr_t src_ipaddr; //!< packets come from this address
127 fr_ipaddr_t network; //!< network for dynamic clients
128 fr_client_t *radclient; //!< old-style definition of this client
129
130 int packets; //!< number of packets using this client
131 fr_heap_index_t pending_id; //!< for pending clients
132 fr_heap_index_t alive_id; //!< for all clients
133
134 bool use_connected; //!< does this client allow connected sub-sockets?
135 bool ready_to_delete; //!< are we ready to delete this client?
136 bool in_trie; //!< is the client in the trie?
137
138 fr_io_instance_t const *inst; //!< parent instance for master IO handler
140 fr_timer_t *ev; //!< when we clean up the client
141 fr_rb_tree_t *table; //!< tracking table for packets
142
143 fr_heap_t *pending; //!< pending packets for this client
144 fr_hash_table_t *addresses; //!< list of src/dst addresses used by this client
145
146 pthread_mutex_t mutex; //!< for parent / child signaling
147 fr_hash_table_t *ht; //!< for tracking connected sockets
148};
149
150/** Track a connection
151 *
152 * This structure contains information about the connection,
153 * a pointer to the library instance so that we can clean up on exit,
154 * and the listener.
155 *
156 * It also points to a client structure which is for this connection,
157 * and only this connection.
158 *
159 * Finally, a pointer to the parent client, so that the child can
160 * tell the parent it's alive, and the parent can push packets to the
161 * child.
162 */
164 char const *name; //!< taken from proto_FOO_TRANSPORT
165 int packets; //!< number of packets using this connection
166 fr_io_address_t *address; //!< full information about the connection.
167 fr_listen_instance_t *inst; //!< master listener for this socket
168 fr_listen_instance_t *child_inst; //!< child listener (app_io) for this socket
169 fr_io_client_t *client; //!< our local client (pending or connected).
170 fr_io_client_t *parent; //!< points to the parent client.
171 module_instance_t *mi; //!< for submodule
172
173 bool dead; //!< roundabout way to get the network side to close a socket
174 bool paused; //!< event filter doesn't like resuming something that isn't paused
175 bool in_parent_hash; //!< for tracking thread issues
176 fr_event_list_t *el; //!< event list for this connection
177 fr_network_t *nr; //!< network for this connection
178};
179
182 { 0 }
183};
184
187 { 0 }
188};
189
190static int track_free(fr_io_track_t *track)
191{
192 FR_TIMER_DELETE_RETURN(&track->ev);
193 talloc_free_children(track);
194
195 fr_assert(track->client->packets > 0);
196 track->client->packets--;
197
198 return 0;
199}
200
202{
203 void *found;
204
205 fr_assert(track->client->table != NULL);
206
207 /*
208 * If the tree is being freed, then we don't try to remove ourselves from it. Doing so would
209 * free this node, and therefore corrupt the tree.
210 *
211 * The talloc code will take care of cleaning up the children and events when this chunk is
212 * freed.
213 */
214 if (track->client->table->being_freed) return 0;
215
216 fr_rb_find(&found, track->client->table, track);
217 fr_assert(found != NULL);
218
219 if (fr_rb_delete(track->client->table, track) != 0) {
220 fr_assert(0);
221 }
222
223 return track_free(track);
224}
225
226/*
227 * Return negative numbers to put 'one' at the top of the heap.
228 * Return positive numbers to put 'two' at the top of the heap.
229 */
230static fr_cmp_ret_t pending_packet_cmp(void const *one, void const *two)
231{
234 int ret;
235
236 /*
237 * Higher priority elements are larger than lower
238 * priority elements. So if "a" is larger than "b", we
239 * wish to prefer "a".
240 */
241 ret = CMP_PREFER_LARGER(a->priority, b->priority);
242 if (ret != 0) return ret;
243
244 /*
245 * Smaller numbers mean packets were received earlier.
246 * We want to process packets in time order. So if "a"
247 * is smaller than "b", we wish to prefer "a".
248 *
249 * After that, it doesn't really matter what order the
250 * packets go in. Since we'll never have two identical
251 * "recv_time" values, the code should never get here.
252 */
254}
255
256/*
257 * Order clients in the pending_clients heap, based on the
258 * packets that they contain.
259 */
260static fr_cmp_ret_t pending_client_cmp(void const *one, void const *two)
261{
262 fr_io_pending_packet_t const *a;
263 fr_io_pending_packet_t const *b;
264
267
268 a = fr_heap_peek(c1->pending);
269 b = fr_heap_peek(c2->pending);
270
271 fr_assert(a != NULL);
272 fr_assert(b != NULL);
273
274 return pending_packet_cmp(a, b);
275}
276
277
278static fr_cmp_ret_t address_cmp(void const *one, void const *two)
279{
282
283 return fr_socket_cmp(&a->socket, &b->socket);
284}
285
286static uint32_t connection_hash(void const *ctx)
287{
290
291 hash = fr_hash(&c->address->socket.inet.src_ipaddr, sizeof(c->address->socket.inet.src_ipaddr));
292 hash = fr_hash_update(&c->address->socket.inet.src_port, sizeof(c->address->socket.inet.src_port), hash);
293
294 hash = fr_hash_update(&c->address->socket.inet.ifindex, sizeof(c->address->socket.inet.ifindex), hash);
295
296 hash = fr_hash_update(&c->address->socket.inet.dst_ipaddr, sizeof(c->address->socket.inet.dst_ipaddr), hash);
297 return fr_hash_update(&c->address->socket.inet.dst_port, sizeof(c->address->socket.inet.dst_port), hash);
298}
299
300static fr_cmp_ret_t connection_cmp(void const *one, void const *two)
301{
304 fr_cmp_ret_t ret;
305
306 ret = address_cmp(a->address, b->address);
307 if (ret != 0) return ret;
308
309 /*
310 * Two connections can have the same addresses, e.g. two connections to one Unix domain socket.
311 */
312 return CMP(a->address->socket.fd, b->address->socket.fd);
313}
314
315
316static fr_cmp_ret_t track_cmp(void const *one, void const *two)
317{
320 int ret;
321
322 fr_assert(a->client != NULL);
323 fr_assert(b->client != NULL);
324 fr_assert(a->client == b->client); /* tables are per-client */
325
328
329 /*
330 * Unconnected sockets must check src/dst ip/port.
331 */
332 ret = address_cmp(a->address, b->address);
333 if (ret != 0) return ret;
334
335 /*
336 * Call the per-protocol comparison function.
337 */
340 a->client->radclient,
341 a->packet, b->packet);
342 return CMP(ret, 0);
343}
344
345
346static fr_cmp_ret_t track_connected_cmp(void const *one, void const *two)
347{
350 int ret;
351
352 fr_assert(a->client != NULL);
353 fr_assert(b->client != NULL);
354
357 fr_assert(a->client == b->client);
359
360 /*
361 * Note that we pass the connection "client", as
362 * we may do negotiation specific to this connection.
363 */
367 a->packet, b->packet);
368 return CMP(ret, 0);
369}
370
371
373{
374 fr_io_client_t *client;
375 fr_io_pending_packet_t *pending;
376
377 fr_heap_pop((void **)&client, &thread->pending_clients);
378 if (!client) {
379 fr_assert(thread->num_pending_packets == 0);
380
381 /*
382 * 99% of the time we don't have pending clients.
383 * So we might as well free this, so that the
384 * caller doesn't keep checking us for every packet.
385 */
387 thread->pending_clients = NULL;
388 return NULL;
389 }
390
391 fr_heap_pop((void **)&pending, &client->pending);
392 fr_assert(pending != NULL);
393
394 /*
395 * If the client has more packets pending, add it back to
396 * the heap.
397 */
398 if (fr_heap_num_elements(client->pending) > 0) {
399 if (fr_heap_insert(&thread->pending_clients, client) < 0) {
400 fr_assert(0 == 1);
401 }
402 }
403
404 fr_assert(thread->num_pending_packets > 0);
405 thread->num_pending_packets--;
406
407 return pending;
408}
409
410static fr_client_t *radclient_clone(TALLOC_CTX *ctx, fr_client_t const *parent)
411{
412 fr_client_t *c;
413
414 if (!parent) return NULL;
415
416 c = talloc_zero(ctx, fr_client_t);
417 if (!c) return NULL;
418
419 /*
420 * Do NOT set ipaddr or src_ipaddr. The caller MUST do this!
421 */
422
423#define DUP_FIELD(_x) do { if (parent->_x) {c->_x = talloc_strdup(c, parent->_x); if (!c->_x) {goto error;}}} while (0)
424#define COPY_FIELD(_x) c->_x = parent->_x
425
426 DUP_FIELD(longname);
427 DUP_FIELD(shortname);
429 DUP_FIELD(nas_type);
430 DUP_FIELD(server);
431
432 COPY_FIELD(require_message_authenticator);
433 COPY_FIELD(require_message_authenticator_is_set);
434#ifdef NAS_VIOLATES_RFC
435 COPY_FIELD(allow_vulnerable_clients);
436#endif
437 COPY_FIELD(limit_proxy_state);
438 COPY_FIELD(limit_proxy_state_is_set);
439 /* dynamic MUST be false */
440 COPY_FIELD(server_cs);
441 COPY_FIELD(cs);
442 COPY_FIELD(proto);
443 COPY_FIELD(active);
444
445 COPY_FIELD(use_connected);
446
447#ifdef WITH_TLS
448 COPY_FIELD(tls_required);
449#endif
450
451 c->ipaddr = parent->ipaddr;
452 c->src_ipaddr = parent->src_ipaddr;
453
454 return c;
455
456 /*
457 * @todo - fill in other fields, too!
458 */
459
460error:
461 talloc_free(c);
462 return NULL;
463}
464#undef COPY_FIELD
465#undef DUP_FIELD
466
467/** Expire entries in the cache.
468 *
469 * Remove old entries from the head of the queue if they have expired, OR if the NAK list is large.
470 *
471 * @todo - make the size of the NAK list configurable. For now, a hard-coded limit of 1000 isn't terrible.
472 * If there are more NAK IPs than that, the admin should set up firewall rules to block traffic.
473 */
475{
477
478 while ((nak = fr_io_nak_list_head(&thread->nak_list)) != NULL) {
479 if (fr_time_gt(nak->expires, now) &&
480 (fr_io_nak_list_num_elements(&thread->nak_list) <= 1000)) {
481 break;
482 }
483
484 /*
485 * @todo - if the cache is full because we're expiring entries too quickly, then that's
486 * likely a firewall issue for the admin to resolve.
487 */
488
489 (void) fr_trie_remove_by_key(thread->nak_trie, &nak->src_ipaddr.addr, nak->src_ipaddr.prefix);
490 fr_io_nak_list_remove(&thread->nak_list, nak);
491 talloc_free(nak);
492 }
493
494}
495
496/** Add a negative cache entry.
497 *
498 * The new entry is either added, or it has its expiry updated. Deleting the existing entry would cause
499 * problems for the lifetime of the NAK entry.
500 *
501 * @param[in] thread thread instance
502 * @param[in] src_ipaddr to look add
503 * @param[in] lifetime how long the entry lives for.
504 */
505static void fr_io_nak_insert(fr_io_thread_t *thread, fr_ipaddr_t const *src_ipaddr, fr_time_delta_t lifetime)
506{
507 fr_time_t now = fr_time();
509
510 fr_io_nak_expire(thread, now);
511
512 nak = fr_trie_match_by_key(thread->nak_trie, &src_ipaddr->addr, src_ipaddr->prefix);
513 if (nak) {
514 fr_io_nak_list_remove(&thread->nak_list, nak);
515 goto do_insert;
516 }
517
518 /*
519 * @todo - rate limited warnings if the NAK cache is full.
520 */
521 if (fr_io_nak_list_num_elements(&thread->nak_list) > 1000) {
522 return;
523 }
524
525 /*
526 * No match, allocate and insert a new entry.
527 */
528 MEM(nak = talloc_zero(thread->nak_trie, fr_io_nak_entry_t));
529 nak->src_ipaddr = *src_ipaddr;
530 fr_io_nak_list_entry_init(nak);
531
532 if (fr_trie_insert_by_key(thread->nak_trie, &src_ipaddr->addr, src_ipaddr->prefix, nak)) {
533 talloc_free(nak);
534 return;
535 }
536
537do_insert:
538 /*
539 * This assumes that the lifetime is constant. If it varies per IP, we will need to move the
540 * dlist to an RB tree.
541 */
542 nak->expires = fr_time_add(now, lifetime);
543 fr_io_nak_list_insert_tail(&thread->nak_list, nak);
544}
545
546/** Look up a negative cache entry.
547 *
548 * Entries are expired first, and then looked up.
549 *
550 * @param[in] thread thread instance
551 * @param[in] src_ipaddr to look up.
552 * @return
553 * - true if the address is NAK'd, and the caller must refuse it.
554 * - false if it is not NAK'd, or if the entry has expired.
555 */
556static bool fr_io_nak_find(fr_io_thread_t *thread, fr_ipaddr_t const *src_ipaddr)
557{
558 fr_time_t now = fr_time();
560
561 fr_io_nak_expire(thread, now);
562
563 nak = fr_trie_lookup_by_key(thread->nak_trie, &src_ipaddr->addr, src_ipaddr->prefix);
564 return (nak != NULL);
565}
566
567
568/** Count the number of connections used by active clients.
569 */
570static int count_connections(UNUSED uint8_t const *key, UNUSED size_t keylen, void *data, void *ctx)
571{
572 fr_io_client_t *client = talloc_get_type_abort(data, fr_io_client_t);
573 int connections;
574
575 /*
576 * The mutex doesn't exist if there isn't a hash table.
577 */
578 if (!client->ht) return 0;
579
580 pthread_mutex_lock(&client->mutex);
581 connections = fr_hash_table_num_elements(client->ht);
582 pthread_mutex_unlock(&client->mutex);
583
584 /*
585 * Don't check "use_connected". Pending dynamic clients get an entry in the connection tracking
586 * table before the client is defined, and therefore before "use_connected" is set. As a result,
587 * we can't check the value of "use_connected" until much later.
588 */
589
590 *((uint32_t *) ctx) += connections;
591
592 return 0;
593}
594
595
596static int _client_free(fr_io_client_t *client)
597{
598 /*
599 * The mutex is initialized whenever the connection tracking table is created, which can happen
600 * for pending clients which do not (yet) have "use_connected" set. Since the mutex creation is
601 * conditional on the existence of the connection tracking table, we make the mutex deletion
602 * conditional on the existence of the tracking table.
603 */
604 if (client->ht) (void) pthread_mutex_destroy(&client->mutex);
605
606 TALLOC_FREE(client->pending);
607
608 return 0;
609}
610
612{
613 size_t num;
614
615 fr_assert(!client->connection);
616
617 if (!client->pending) return;
618
619 num = fr_heap_num_elements(client->pending);
620
621 fr_assert(client->thread->num_pending_packets >= num);
622 client->thread->num_pending_packets -= num;
623
624 TALLOC_FREE(client->pending);
625}
626
627
628static int connection_free(fr_io_connection_t *connection)
629{
630 /*
631 * This is it's own talloc context, as there are
632 * thousands of packets associated with it.
633 */
634 TALLOC_FREE(connection->client);
635
636 return 0;
637}
638
639/** Connection destructor
640 *
641 * Worker threads can hold packets associated with a connection, even after the FD has closed. We don't want
642 * to clear out the packet_ctx from under the worker, so we delay the free() until the listener is freed.
643 *
644 * Once all of the workers have replied, and s->outstanding==0, the network side can finally free the
645 * connection data structure.
646 *
647 */
649{
650 fr_io_connection_t *connection = li->thread_instance;
651
652 talloc_free(connection->mi);
653
654 return 0;
655}
656
657/** Create a new connection.
658 *
659 * Called ONLY from the master socket.
660 */
662 fr_io_thread_t *thread,
663 fr_io_client_t *client, int fd,
664 fr_io_address_t *address)
665{
666 int ret;
667 fr_io_connection_t *connection;
668 module_instance_t *mi = NULL;
670 fr_client_t *radclient;
671
672
673 /*
674 * Reload the app_io module as a "new" library. This
675 * causes the link count for the library to be correct.
676 * It also allocates a new instance data for it, too.
677 * Passing CONF_SECTION of NULL ensures that there's no
678 * config for it, as we'll just clone it's contents from
679 * the original. It also means that detach should be
680 * called when the instance data is freed.
681 */
682 {
683 CONF_SECTION *cs;
684 char *inst_name;
685
686 if (inst->max_connections || client->radclient->limit.max_connections) {
687 uint32_t max_connections = inst->max_connections ? inst->max_connections : client->radclient->limit.max_connections;
688
689 /*
690 * If we're already at the limit, don't add more.
691 */
692 if (thread->num_connections >= max_connections) {
693 thread->num_connections = 0;
694
695 (void) fr_trie_walk(thread->trie, &thread->num_connections, count_connections);
696
697 if (thread->num_connections >= max_connections) {
698 RATE_LIMIT_LOCAL(&thread->rate_limit.max_connections, INFO,
699 "proto_%s - Ignoring connection from client %s - 'max_connections' limit reached.",
700 inst->app->common.name, client->radclient->shortname);
701 if (fd >= 0) close(fd);
702 return NULL;
703 }
704 }
705 }
706
707 /*
708 * Add a client module into a sublist
709 */
710 inst_name = talloc_asprintf(NULL, "%"PRIu64, thread->client_id++);
711 mi = module_instance_copy(inst->clients, inst->submodule, inst_name);
712 talloc_free(inst_name);
713
714 cs = cf_section_dup(mi, NULL, inst->submodule->conf,
715 cf_section_name1(inst->submodule->conf),
716 cf_section_name2(inst->submodule->conf), false);
717
718 if (!cs) {
719 cf_log_err(inst->server_cs, "Failed duplicating module config");
720 goto cleanup;
721 }
722
723 /*
724 * Clear the "dynamic_clients" flag, so that the child instantiate routines don't check
725 * the network allow / deny list when instantiating child connections.
726 *
727 * This is a short-term and minimal hack to get the problem fixed. A longer term
728 * solution would be to update fr_master_io_network() so that it sets caches the trie
729 * _and_ the dynamic client flag in connection data structure. Which then means that the
730 * mod_network_get() API could also go away.
731 *
732 * But doing that involves more rearchitecture and code changes, which we're avoiding at
733 * this time.
734 */
735 cf_pair_replace_or_add(cs, "dynamic_clients", "no");
736
737 if (module_instance_conf_parse(mi, cs) < 0) {
738 cf_log_err(inst->server_cs, "Failed parsing module config");
739 goto cleanup;
740 }
741
742 /* Thread local module lists never run bootstrap */
743 if (module_instantiate(mi) < 0) {
744 cf_log_err(inst->server_cs, "Failed instantiating module");
745 goto cleanup;
746 }
747
748 if (module_thread_instantiate(mi, mi, thread->el) < 0) {
749 cf_log_err(inst->server_cs, "Failed instantiating module");
750 goto cleanup;
751 }
752
753 /*
754 * FIXME - Instantiate the new module?!
755 */
756 fr_assert(mi != NULL);
757 }
758
759 MEM(connection = talloc_zero(mi, fr_io_connection_t));
760 MEM(connection->address = talloc_memdup(connection, address, sizeof(*address)));
761 (void) talloc_set_name_const(connection->address, "fr_io_address_t");
762
763 connection->parent = client;
764 connection->mi = mi;
765
766 MEM(connection->client = talloc_named(NULL, sizeof(fr_io_client_t), "fr_io_client_t"));
767 memset(connection->client, 0, sizeof(*connection->client));
768
769 MEM(connection->client->radclient = radclient = radclient_clone(connection->client, client->radclient));
770
771 talloc_set_destructor(connection->client, _client_free);
772 talloc_set_destructor(connection, connection_free);
773
776 connection->client->connection = connection;
777
778 /*
779 * Create the packet tracking table for this client.
780 *
781 * #todo - unify the code with static clients?
782 */
783 if (inst->app_io->track_duplicates) {
784 MEM(connection->client->table = fr_rb_inline_talloc_alloc(client, fr_io_track_t, node,
785 track_connected_cmp, NULL));
786 }
787
788 /*
789 * Set this radclient to be dynamic, and active.
790 */
791 radclient->dynamic = true;
792 radclient->active = true;
793
794 /*
795 * address->socket.inet.client points to a "static" client. We want
796 * to clean up everything associated with the connection
797 * when it closes. So we need to point to our own copy
798 * of the client here.
799 */
800 connection->address->radclient = connection->client->radclient;
801 connection->client->inst = inst;
802 connection->client->thread = thread;
803
804 /*
805 * Create a heap for packets which are pending for this
806 * client.
807 */
808 MEM(connection->client->pending = fr_heap_alloc(connection->client, pending_packet_cmp,
809 fr_io_pending_packet_t, heap_id, 0));
810
811 /*
812 * Clients for connected sockets are always a /32 or /128.
813 */
814 connection->client->src_ipaddr = address->socket.inet.src_ipaddr;
815 connection->client->network = address->socket.inet.src_ipaddr;
816
817 /*
818 * Don't initialize mutex or hash table.
819 * Connections cannot spawn other connections.
820 */
821
822 /*
823 * If this client state is pending, then the connection
824 * state is pending, too. That allows NAT gateways to be
825 * defined dynamically, AND for them to have multiple
826 * connections, each with a different client. This
827 * allows for different shared secrets to be used for
828 * different connections. Once the client gets defined
829 * for this connection, it will be either "connected" or
830 * not. If connected, then the parent client remains
831 * PENDING. Otherwise, the parent client is moved to
832 * DYNAMIC
833 *
834 * If this client state is static or dynamic,
835 * then we're just using connected sockets behind
836 * that client. The connections here all use the
837 * same shared secret, but they use different
838 * sockets, so they allow for sharing of IO
839 * across CPUs / threads.
840 */
841 switch (client->state) {
843 connection->client->state = PR_CLIENT_PENDING;
844
845 /*
846 * Needed for rlm_radius, which refuses to proxy packets
847 * that define a dynamic client.
848 */
849 radclient->active = false;
850 break;
851
852 case PR_CLIENT_STATIC:
854 connection->client->state = PR_CLIENT_CONNECTED;
855 break;
856
859 fr_assert(0 == 1);
860 goto cleanup;
861 }
862
863 {
864 /*
865 * Get the child listener.
866 */
867 MEM(li = connection->child_inst = fr_listen_instance_alloc(connection, thread->child_inst->listen));
868 li->connected = true;
869
870 /*
871 * Create writable thread instance data.
872 */
873 li->thread_instance = talloc_zero_array(NULL, uint8_t, inst->app_io->common.thread_inst_size);
874 talloc_set_destructor(li, fr_io_listen_free);
875 talloc_set_name(li->thread_instance, "proto_%s_thread_t", inst->app_io->common.name);
876
877 /*
878 * Our own socket for the connection. All the variable
879 * data is in the connection, the listener is shared.
880 */
881 MEM(li = connection->inst = fr_listen_instance_alloc(connection, thread->inst->listen));
883
884 li->connected = true;
885 li->thread_instance = connection;
886 talloc_set_destructor(li, _connection_inst_free);
887
888 /*
889 * Instantiate the child, and open the socket.
890 */
891 fr_assert(inst->app_io->connection_set != NULL);
892
893 if (inst->app_io->connection_set(connection->child_inst, connection->address) < 0) {
894 DEBUG("proto_%s - Failed setting connection for socket.", inst->app->common.name);
895 goto cleanup;
896 }
897
898 /*
899 * UDP sockets: open a new socket, and then
900 * connect it to the client. This emulates the
901 * behavior of accept().
902 *
903 * Note that there is a small window between the
904 * bind() and connect() where UDP packets for the
905 * wildcard socket can get received by this
906 * socket. We hope that this time frame is as
907 * small as possible.
908 *
909 * i.e. we ignore the problem, and don't
910 * currently check dst ip/port for UDP packets
911 * received on connected sockets.
912 */
913 if (fd < 0) {
914 socklen_t salen;
915 struct sockaddr_storage src;
916
917 if (fr_ipaddr_to_sockaddr(&src, &salen,
918 &connection->address->socket.inet.src_ipaddr,
919 connection->address->socket.inet.src_port) < 0) {
920 DEBUG("proto_%s - Failed getting IP address", inst->app->common.name);
921 talloc_free(mi);
922 return NULL;
923 }
924
925 if (inst->app_io->open(connection->child_inst) < 0) {
926 DEBUG("proto_%s - Failed opening connected socket.", inst->app->common.name);
927 talloc_free(mi);
928 return NULL;
929 }
930
931 fd = connection->child_inst->fd;
932
933 if (connect(fd, (struct sockaddr *) &src, salen) < 0) {
934 ERROR("proto_%s - Failed in connect: %s", inst->app->common.name, fr_syserror(errno));
935 goto cleanup;
936 }
937 } else {
938 connection->child_inst->fd = fd;
939 }
940
941 /*
942 * Set the new FD, and get the module to set it's connection name.
943 */
944 if (inst->app_io->fd_set(connection->child_inst, fd) < 0) {
945 DEBUG3("Failed setting FD to %s", inst->app_io->common.name);
946 goto cleanup;
947 }
948
949 li->fd = fd;
950
951 if (!inst->app_io->get_name) {
952 connection->name = fr_asprintf(connection, "proto_%s from client %pV port "
953 "%u to server %pV port %u",
954 inst->app->common.name,
955 fr_box_ipaddr(connection->address->socket.inet.src_ipaddr),
956 connection->address->socket.inet.src_port,
957 fr_box_ipaddr(connection->address->socket.inet.dst_ipaddr),
958 connection->address->socket.inet.dst_port);
959 } else {
960 connection->name = inst->app_io->get_name(connection->child_inst);
961 }
962
963 /*
964 * Set the names for the listeners.
965 */
966 connection->inst->name = connection->name;
967 connection->child_inst->name = connection->name;
968 }
969
970 /*
971 * If the parent client is still PENDING, lazily create the hash table that tracks its child
972 * connections. We need this so that fr_io_connection_allow() can later find any sibling
973 * connections, and add them to the scheduler once the dynamic client is defined.
974 */
975 if ((client->state == PR_CLIENT_PENDING) && !client->ht) {
976 (void) pthread_mutex_init(&client->mutex, NULL);
977 MEM(client->ht = fr_hash_table_alloc(client, connection_hash, connection_cmp, NULL));
978 }
979
980 /*
981 * Add the connection to the set of connections for this client. Capture the pre-insert size so
982 * we can tell whether this is the first connection (which runs the dynamic-client verification)
983 * or a later connection (which is deferred until the first connection is verified).
984 */
985 pthread_mutex_lock(&client->mutex);
986 if (client->ht) {
987 size_t pre_size = fr_hash_table_num_elements(client->ht);
988
989 ret = fr_hash_table_insert(client->ht, connection);
990 client->ready_to_delete = false;
991 connection->in_parent_hash = true;
992
993 if (ret != 0) {
994 pthread_mutex_unlock(&client->mutex);
995 ERROR("proto_%s - Failed inserting connection into tracking table. "
996 "Closing it, and discarding all packets for connection %s.",
997 inst->app_io->common.name, connection->name);
998 goto cleanup;
999 }
1000
1001 /*
1002 * The first connection for a PENDING parent runs the dynamic client definition. All
1003 * later connections will be scheduled by fr_io_connection_allow(), once the parent is
1004 * defined (or not).
1005 */
1006 if ((client->state == PR_CLIENT_PENDING) && (pre_size > 0)) {
1007 pthread_mutex_unlock(&client->mutex);
1008 DEBUG("proto_%s - deferring scheduling of connection %s until parent client %pV is defined",
1009 inst->app_io->common.name, connection->name, fr_box_ipaddr(client->src_ipaddr));
1010 thread->num_connections++;
1011 return connection;
1012 }
1013 }
1014 pthread_mutex_unlock(&client->mutex);
1015
1016 DEBUG("proto_%s - starting connection %s", inst->app_io->common.name, connection->name);
1017 connection->nr = fr_schedule_listen_add(thread->sc, connection->inst);
1018 if (!connection->nr) {
1019 ERROR("proto_%s - Failed inserting connection into scheduler. "
1020 "Closing it, and diuscarding all packets for connection %s.",
1021 inst->app_io->common.name, connection->name);
1022 pthread_mutex_lock(&client->mutex);
1023 if (client->ht) (void) fr_hash_table_delete(client->ht, connection);
1024 pthread_mutex_unlock(&client->mutex);
1025
1026 cleanup:
1027 if (fd >= 0) close(fd);
1028 talloc_free(mi);
1029 return NULL;
1030 }
1031
1032 /*
1033 * We have one more connection. Note that we do
1034 * NOT decrement this counter when a connection
1035 * closes, as the close is done in a child
1036 * thread. Instead, we just let counter hit the
1037 * limit, and then walk over the clients to reset
1038 * the count.
1039 */
1040 thread->num_connections++;
1041
1042 return connection;
1043}
1044
1045
1046/*
1047 * And here we go into the rabbit hole...
1048 *
1049 * @todo future - have a similar structure
1050 * fr_io_connection_io, which will duplicate some code,
1051 * but may make things simpler?
1052 */
1054 fr_io_connection_t **connection, fr_listen_instance_t **child)
1055{
1056 if (!li->connected) {
1057 *inst = li->listen->app_io_instance;
1058 if (thread) *thread = li->thread_instance;
1059 *connection = NULL;
1060 if (child) *child = ((fr_io_thread_t *)li->thread_instance)->child_inst;
1061
1062 } else {
1063 fr_assert(connection != NULL);
1064
1065 *connection = li->thread_instance;
1066 *inst = (*connection)->client->inst;
1067 if (thread) *thread = NULL;
1068 if (child) *child = (*connection)->child_inst;
1069 }
1070}
1071
1072
1073static fr_client_t *radclient_alloc(TALLOC_CTX *ctx, int ipproto, fr_io_address_t *address)
1074{
1075 fr_client_t *radclient;
1076 char *shortname;
1077
1078 MEM(radclient = talloc_zero(ctx, fr_client_t));
1079
1080 fr_value_box_aprint(radclient, &shortname, fr_box_ipaddr(address->socket.inet.src_ipaddr), NULL);
1081 radclient->longname = radclient->shortname = shortname;
1082
1083 radclient->secret = radclient->nas_type = talloc_strdup(radclient, "");
1084
1085 radclient->ipaddr = address->socket.inet.src_ipaddr;
1086
1087 radclient->src_ipaddr = address->socket.inet.dst_ipaddr;
1088
1089 radclient->proto = ipproto;
1090 radclient->dynamic = true;
1091
1092 return radclient;
1093}
1094
1095/*
1096 * Remove a client from the list of "live" clients.
1097 *
1098 * This function is only used for the "main" socket. Clients
1099 * from connections do not use it.
1100 */
1102{
1103 talloc_get_type_abort(client, fr_io_client_t);
1104
1105 fr_assert(client->in_trie);
1106 fr_assert(!client->connection);
1107 fr_assert(client->thread);
1108
1109 if (client->pending) client_pending_free(client);
1110
1111 (void) fr_trie_remove_by_key(client->thread->trie, &client->src_ipaddr.addr, client->src_ipaddr.prefix);
1112
1113 /*
1114 * A client with pending packets is also in the thread's heap of pending clients. Nothing
1115 * else takes it out of that heap, so do it here, or the heap is left holding a pointer to
1116 * freed memory.
1117 */
1118 if (client->thread->pending_clients && fr_heap_entry_inserted(client->pending_id)) {
1119 (void) fr_heap_extract(&client->thread->pending_clients, client);
1120 }
1121
1122 if (client->thread->alive_clients) {
1124 (void) fr_heap_extract(&client->thread->alive_clients, client);
1125 }
1126
1127 /*
1128 * The mutex/ht pair is initialized either for use_connected
1129 * clients (in client_alloc) or for PENDING clients with
1130 * deferred sibling connections (in fr_io_connection_alloc).
1131 * The ht pointer is the canonical signal that the mutex
1132 * was initialized.
1133 */
1134 if (client->ht) (void) pthread_mutex_destroy(&client->mutex);
1135
1136 return 0;
1137}
1138
1139/** Allocate a dynamic client.
1140 *
1141 */
1142static fr_io_client_t *client_alloc(TALLOC_CTX *ctx, fr_io_client_state_t state,
1143 fr_io_instance_t const *inst, fr_io_thread_t *thread, fr_client_t *radclient,
1144 fr_ipaddr_t const *network)
1145{
1146 fr_io_client_t *client;
1147
1148 /*
1149 * Create our own local client. This client
1150 * holds our state which really shouldn't go into
1151 * fr_client_t.
1152 *
1153 * Note that we create a new top-level talloc
1154 * context for this client, as there may be tens
1155 * of thousands of packets associated with this
1156 * client. And we want to avoid problems with
1157 * O(N) issues in talloc.
1158 */
1159 MEM(client = talloc_named(ctx, sizeof(fr_io_client_t), "fr_io_client_t"));
1160 memset(client, 0, sizeof(*client));
1161
1162 client->state = state;
1163 client->src_ipaddr = radclient->ipaddr;
1164 client->radclient = radclient;
1165 client->inst = inst;
1166 client->thread = thread;
1167
1168 if (network) {
1169 client->network = *network;
1170 } else {
1171 client->network = client->src_ipaddr;
1172 }
1173
1174 /*
1175 * At this point, this variable can only be true
1176 * for STATIC clients. PENDING clients may set
1177 * it to true later, after they've been defined.
1178 */
1179 client->use_connected = radclient->use_connected;
1180
1181 /*
1182 * Create the pending heap for pending clients.
1183 */
1184 if (state == PR_CLIENT_PENDING) {
1185 MEM(client->pending = fr_heap_alloc(client, pending_packet_cmp,
1186 fr_io_pending_packet_t, heap_id, 0));
1187 }
1188
1189 /*
1190 * Create the packet tracking table for this client.
1191 */
1192 if (inst->app_io->track_duplicates) {
1193 fr_assert(inst->app_io->track_compare != NULL);
1194 MEM(client->table = fr_rb_inline_talloc_alloc(client, fr_io_track_t, node, track_cmp, NULL));
1195 }
1196
1197 /*
1198 * Allow connected sockets to be set on a
1199 * per-client basis.
1200 */
1201 if (client->use_connected) {
1202 fr_assert(client->state == PR_CLIENT_STATIC);
1203
1204 (void) pthread_mutex_init(&client->mutex, NULL);
1205 MEM(client->ht = fr_hash_table_alloc(client, connection_hash, connection_cmp, NULL));
1206 }
1207
1208 /*
1209 * Add the newly defined client to the trie of
1210 * allowed clients.
1211 */
1212 if (fr_trie_insert_by_key(thread->trie, &client->src_ipaddr.addr, client->src_ipaddr.prefix, client)) {
1213 ERROR("proto_%s - Failed inserting client %s into tracking table. Discarding client, and all packets for it.",
1214 inst->app_io->common.name, client->radclient->shortname);
1215 if (client->ht) (void) pthread_mutex_destroy(&client->mutex);
1216 talloc_free(client);
1217 return NULL;
1218 }
1219
1220 client->in_trie = true;
1221
1222 /*
1223 * It's a static client. Don't insert it into the list of alive clients, as those are only for
1224 * dynamic clients.
1225 */
1226 if (state == PR_CLIENT_STATIC) return client;
1227
1228 fr_assert(thread->alive_clients != NULL);
1229
1230 /*
1231 * Track the live clients so that we can clean
1232 * them up.
1233 */
1234 (void) fr_heap_insert(&thread->alive_clients, client);
1236
1237 /*
1238 * Now that we've inserted it into the heap and
1239 * incremented the numbers, set the destructor
1240 * function.
1241 */
1242 talloc_set_destructor(client, _client_live_free);
1243
1244 return client;
1245}
1246
1247
1249 uint8_t const *packet, size_t packet_len, fr_time_t recv_time, bool *is_dup)
1250{
1251 size_t len;
1252 fr_io_track_t *track, *old;
1253 TALLOC_CTX *track_ctx = client->inst->app_io->track_duplicates ? (TALLOC_CTX *)client->table : (TALLOC_CTX *)client;
1254
1255 *is_dup = false;
1256
1257 /*
1258 * Allocate a new tracking structure. Most of the time
1259 * there are no duplicates, so this is fine.
1260 */
1261 if (client->connection) {
1262 MEM(track = talloc_zero_pooled_object(track_ctx, fr_io_track_t, 1, sizeof(*track) + 64));
1263 track->address = client->connection->address;
1264 } else {
1265 fr_io_address_t *my_address;
1266
1267 MEM(track = talloc_zero_pooled_object(track_ctx, fr_io_track_t, 1, sizeof(*track) + sizeof(*track->address) + 64));
1268 MEM(track->address = my_address = talloc(track, fr_io_address_t));
1269
1270 *my_address = *address;
1271 my_address->radclient = client->radclient;
1272 }
1273
1274 track->li = li;
1275 track->client = client;
1276
1277 track->timestamp = recv_time;
1278 track->packets = 1;
1279
1280 /*
1281 * We're not tracking duplicates, so just return the
1282 * tracking entry. This tracks src/dst IP/port, client,
1283 * receive time, etc.
1284 */
1285 if (!client->inst->app_io->track_duplicates) {
1286 client->packets++;
1287 talloc_set_destructor(track, track_free);
1288 return track;
1289 }
1290
1291 /*
1292 * We are checking for duplicates, see if there is a dup
1293 * already in the tree.
1294 */
1295 track->packet = client->inst->app_io->track_create(client->inst->app_io_instance,
1297 client->radclient,
1298 track, packet, packet_len);
1299 if (!track->packet) {
1300 talloc_free(track);
1301 return NULL;
1302 }
1303
1304 /*
1305 * No existing duplicate. Return the new tracking entry.
1306 */
1307 fr_rb_find((void **)&old, client->table, track);
1308 if (!old) goto do_insert;
1309
1310 fr_assert(old->client == client);
1311
1312 /*
1313 * It cannot be both in the free list and in the tracking table.
1314 *
1315 * 2020-08-17, this assertion fails randomly in travis.
1316 * Which means that "track" was in the free list, *and*
1317 * in the rbtree.
1318 */
1319 fr_assert(old != track);
1320
1321 /*
1322 * The new packet has the same dedup fields as the old
1323 * one, BUT it may be a conflicting packet. Check for
1324 * that via a simple memcmp().
1325 *
1326 * It's an exact duplicate. Drop the new one and
1327 * use the old one.
1328 *
1329 * If there's a cached reply, the caller will take care
1330 * of sending it to the network layer.
1331 */
1332 len = talloc_array_length(old->packet);
1333 if ((len == talloc_array_length(track->packet)) &&
1334 (memcmp(old->packet, track->packet, len) == 0)) {
1335 fr_assert(old != track);
1336
1337 /*
1338 * Ignore duplicates while the client is
1339 * still pending.
1340 */
1341 if (client->state == PR_CLIENT_PENDING) {
1342 DEBUG("Ignoring duplicate packet while client %s is still pending dynamic definition",
1343 client->radclient->shortname);
1344 talloc_free(track);
1345 return NULL;
1346 }
1347
1348 *is_dup = true;
1349 old->packets++;
1350 talloc_free(track);
1351
1352 /*
1353 * Retransmits can sit in the outbound queue for
1354 * a while. We don't want to time out this
1355 * struct while the packet is in the outbound
1356 * queue.
1357 */
1358 FR_TIMER_DISARM(old->ev);
1359 return old;
1360 }
1361
1362 /*
1363 * Else it's a conflicting packet. Which is OK if we
1364 * already have a reply. We just delete the old entry,
1365 * and insert the new one.
1366 *
1367 * If there's no reply, then the old request is still
1368 * "live". Delete the old one from the tracking tree,
1369 * and return the new one.
1370 */
1371 if (old->reply_len || old->do_not_respond) {
1372 talloc_free(old);
1373
1374 } else {
1375 fr_assert(client == old->client);
1376
1377 if (fr_rb_delete(client->table, old) != 0) {
1378 fr_assert(0);
1379 }
1380 FR_TIMER_DELETE(&old->ev);
1381
1382 talloc_set_destructor(old, track_free);
1383
1384 old->discard = true; /* don't send any reply, there's nowhere for it to go */
1385 }
1386
1387do_insert:
1388 if (fr_rb_insert(client->table, track) != 0) {
1389 fr_assert(0);
1390 }
1391
1392 client->packets++;
1393 talloc_set_destructor(track, track_dedup_free);
1394 return track;
1395}
1396
1397
1399{
1400 fr_io_track_t *track = pending->track;
1401
1402 /*
1403 * Note that we don't check timestamps, replies, etc. If
1404 * a packet is pending, then any conflicting packet gets
1405 * the "pending" entry marked as such, and a new entry
1406 * added. Any duplicate packet gets suppressed. And
1407 * because the packets are pending, track->reply MUST be
1408 * NULL.
1409 */
1410 fr_assert(track->packets > 0);
1411 track->packets--;
1412
1413 /*
1414 * No more packets using this tracking entry,
1415 * delete it.
1416 */
1417 if (track->packets == 0) talloc_free(track);
1418
1419 return 0;
1420}
1421
1423 uint8_t const *buffer, size_t packet_len,
1424 fr_io_track_t *track,
1425 int priority)
1426{
1427 fr_io_pending_packet_t *pending;
1428
1429 MEM(pending = talloc_zero(client->pending, fr_io_pending_packet_t));
1430
1431 MEM(pending->buffer = talloc_memdup(pending, buffer, packet_len));
1432 pending->buffer_len = packet_len;
1433 pending->priority = priority;
1434 pending->track = track;
1435 pending->recv_time = track->timestamp; /* there can only be one */
1436
1437 talloc_set_destructor(pending, pending_free);
1438
1439 /*
1440 * Insert the pending packet for this client. If it
1441 * fails, silently discard the packet.
1442 */
1443 if (fr_heap_insert(&client->pending, pending) < 0) {
1444 talloc_free(pending);
1445 return NULL;
1446 }
1447
1448 /*
1449 * We only track pending packets for the
1450 * main socket. For connected sockets,
1451 * we pause the FD, so the number of
1452 * pending packets will always be small.
1453 */
1454 if (!connection) client->thread->num_pending_packets++;
1455
1456 return pending;
1457}
1458
1459
1460/*
1461 * Order clients in the alive_clients heap, based on their IP
1462 * address.
1463 *
1464 * This function is only used for the "main" socket. Clients
1465 * from connections do not use it.
1466 */
1467static fr_cmp_ret_t alive_client_cmp(void const *one, void const *two)
1468{
1471
1472 return fr_ipaddr_cmp(&a->src_ipaddr, &b->src_ipaddr);
1473}
1474
1475/** Implement 99% of the read routines.
1476 *
1477 * The app_io->read does the transport-specific data read.
1478 */
1480 size_t buffer_len, size_t *leftover, bool *msg_used)
1481{
1482 fr_io_instance_t const *inst;
1483 fr_io_thread_t *thread;
1484 ssize_t packet_len = -1;
1485 fr_time_t recv_time = fr_time_wrap(0);
1486 fr_io_client_t *client;
1487 fr_io_address_t address;
1488 fr_io_connection_t my_connection, *connection;
1489 fr_io_pending_packet_t *pending = NULL;
1490 fr_io_track_t *track;
1491 fr_listen_instance_t *child;
1492 int value, accept_fd = -1;
1493 uint32_t priority = PRIORITY_NORMAL;
1494
1495/** Log that we ignore clients in debug mode, or when it's enabled for a listener
1496 */
1497#define LOG_IGNORED_CLIENTS(_inst) ((_inst)->log_ignored_clients || fr_debug_lvl >= 1)
1498
1499 get_inst(li, &inst, &thread, &connection, &child);
1500
1501 track = NULL;
1502
1503 /*
1504 * There was data left over from the previous read, go
1505 * get the rest of it now. We MUST do this instead of
1506 * popping a pending packet, because the leftover bytes
1507 * are already in the output buffer.
1508 */
1509 if (*leftover) goto do_read;
1510
1511redo:
1512 /*
1513 * Read one pending packet. The packet may be pending
1514 * because of dynamic client definitions, or because it's
1515 * for a connected UDP socket, and was sent over by the
1516 * "master" UDP socket.
1517 */
1518 if (connection) {
1519 /*
1520 * The connection is dead. Tell the network side
1521 * to close it.
1522 */
1523 if (connection->dead) {
1524 DEBUG("Dead connection %s", connection->name);
1525 return -1;
1526 }
1527
1528 fr_heap_pop((void **)&pending, &connection->client->pending);
1529
1530 } else if (thread->pending_clients) {
1531 pending = pending_packet_pop(thread);
1532
1533 } else {
1534 pending = NULL;
1535 }
1536
1537 if (pending) {
1538 fr_assert(buffer_len >= pending->buffer_len);
1539 track = pending->track;
1540
1541 /*
1542 * Clear the destructor as we now own the
1543 * tracking entry.
1544 */
1545 talloc_set_destructor(pending, NULL);
1546
1547 /*
1548 * We received a conflicting packet while this
1549 * packet was pending. Discard this entry and
1550 * try to get another one.
1551 *
1552 * Note that the pending heap is *simple*. We
1553 * just track priority and recv_time. This means
1554 * it's fast, but also that it's hard to look up
1555 * random packets in the pending heap.
1556 */
1557 if (fr_time_neq(pending->recv_time, track->timestamp)) {
1558 DEBUG3("Discarding old packet");
1559 TALLOC_FREE(pending);
1560 goto redo;
1561 }
1562
1563 /*
1564 * We have a valid packet. Copy it over to the
1565 * caller, and return.
1566 */
1567 *packet_ctx = track;
1568 *leftover = 0;
1569 recv_time = *recv_time_p = pending->recv_time;
1570 client = track->client;
1571
1572 memcpy(buffer, pending->buffer, pending->buffer_len);
1573 packet_len = pending->buffer_len;
1574
1575 /*
1576 * Shouldn't be necessary, but what the heck...
1577 */
1578 memcpy(&address, track->address, sizeof(address));
1579 TALLOC_FREE(pending);
1580
1581 /*
1582 * Skip over all kinds of logic to find /
1583 * allocate the client, when we don't need to do
1584 * it any more.
1585 */
1586 goto have_client;
1587
1588 } else if (!connection && (inst->ipproto == IPPROTO_TCP)) {
1589 struct sockaddr_storage saremote;
1590 socklen_t salen;
1591
1592 salen = sizeof(saremote);
1593
1594 /*
1595 * We're a TCP socket but are NOT connected. We
1596 * must be the master socket. Accept the new
1597 * connection, and figure out src/dst IP/port.
1598 */
1599 accept_fd = accept(child->fd,
1600 (struct sockaddr *) &saremote, &salen);
1601
1602 /*
1603 * Couldn't open a NEW socket, but THIS ONE is
1604 * OK. So don't return -1.
1605 */
1606 if (accept_fd < 0) {
1607 RATE_LIMIT_LOCAL(&thread->rate_limit.accept_failed,
1608 INFO, "proto_%s - failed to accept new socket: %s",
1609 inst->app->common.name, fr_syserror(errno));
1610 return 0;
1611 }
1612
1613#ifdef STATIC_ANALYZER
1614 saremote.ss_family = AF_INET; /* static analyzer doesn't know that accept() initializes this */
1615#endif
1616
1617 /*
1618 * Always initialize the socket. accept() sets the address family of the peer. For a Unix
1619 * domain socket, the app_io connection_set() function sets the path.
1620 */
1621 address.socket = (fr_socket_t) {
1622 .af = saremote.ss_family,
1623 .type = (inst->ipproto == IPPROTO_TCP) ? SOCK_STREAM : SOCK_DGRAM,
1624 .fd = accept_fd,
1625 };
1626
1627 /*
1628 * Get IP addresses only if we have IP addresses.
1629 */
1630 if ((saremote.ss_family == AF_INET) || (saremote.ss_family == AF_INET6)) {
1631 (void) fr_ipaddr_from_sockaddr(&address.socket.inet.src_ipaddr, &address.socket.inet.src_port,
1632 &saremote, salen);
1633
1634 /*
1635 * If the IP is in the NAK cache, then ignore the new socket.
1636 *
1637 * An entry which has passed its expiry time is removed here, and the
1638 * connection is accepted as normal.
1639 */
1640 if (fr_io_nak_find(thread, &address.socket.inet.src_ipaddr)) {
1641 RATE_LIMIT_LOCAL(&thread->rate_limit.accept_failed,
1642 INFO, "proto_%s - ignoring connection request from address %pV due to NAK cache",
1643 inst->app->common.name, fr_box_ipaddr(address.socket.inet.src_ipaddr));
1644 close(accept_fd);
1645 return 0;
1646 }
1647
1648 /*
1649 * @todo - only if the local listen address is "*".
1650 */
1651 salen = sizeof(saremote);
1652 (void) getsockname(accept_fd, (struct sockaddr *) &saremote, &salen);
1653 (void) fr_ipaddr_from_sockaddr(&address.socket.inet.dst_ipaddr, &address.socket.inet.dst_port,
1654 &saremote, salen);
1655 }
1656
1657 /*
1658 * Set the new descriptor to be non-blocking.
1659 */
1660 (void) fr_nonblock(accept_fd);
1661
1662
1663 } else {
1664 fr_io_address_t *local_address;
1665
1666 /*
1667 * We're either not a TCP socket, or we are a
1668 * connected TCP socket. Just read it.
1669 */
1670do_read:
1671 local_address = &address;
1672
1673 /*
1674 * @todo - For connected TCP sockets which are
1675 * dynamically defined, the app_io read()
1676 * function should stop reading the socket if the
1677 * server is busy. That change puts TCP
1678 * backpressure on the client.
1679 *
1680 * @todo TLS - for TLS and dynamic sockets, do
1681 * the SSL setup here, but have a structure which
1682 * describes the TLS data and run THAT through
1683 * the dynamic client definition, instead of
1684 * using normal packets. Or, rely on the app_io
1685 * read() function to do all TLS work? Given
1686 * that some protocols have "starttls" beginning
1687 * after a clear-text exchange, it's likely best
1688 * to have yet another layer of trampoline
1689 * functions which do all of the TLS work.
1690 */
1691 packet_len = inst->app_io->read(child, (void **) &local_address, &recv_time,
1692 buffer, buffer_len, leftover, msg_used);
1693 if (packet_len <= 0) {
1694 return packet_len;
1695 }
1696
1697 /*
1698 * Not allowed? Discard it. The priority()
1699 * function has done any complaining, if
1700 * necessary.
1701 */
1702 if (inst->app->priority) {
1703 value = inst->app->priority(inst->app_instance, buffer, packet_len);
1704 if (value <= 0) {
1705 static fr_rate_limit_t bad_type;
1706
1707 /*
1708 * @todo - unix sockets. We need to use
1709 * the "name" of the socket, in the
1710 * listener?
1711 */
1713 RATE_LIMIT_LOCAL(thread ? &thread->rate_limit.bad_type : &bad_type, INFO,
1714 "proto_%s - ignoring packet from IP %pV. It is not configured as 'type = ...'",
1715 inst->app_io->common.name, fr_box_ipaddr(address.socket.inet.src_ipaddr));
1716 }
1717 return 0;
1718 }
1719 priority = value;
1720 }
1721
1722 /*
1723 * If the connection is pending, pause reading of
1724 * more packets. If mod_write() accepts the
1725 * connection, it will resume reading.
1726 * Otherwise, it will close the socket without
1727 * resuming it.
1728 */
1729 if (connection &&
1730 (connection->client->state == PR_CLIENT_PENDING)) {
1731 fr_assert(!connection->paused);
1732
1733 connection->paused = true;
1734 (void) fr_event_filter_update(connection->el,
1735 child->fd,
1737 }
1738 }
1739
1740 /*
1741 * Look up the client, unless we already have one (for a
1742 * connected socket).
1743 */
1744 if (!connection) {
1745 client = fr_trie_lookup_by_key(thread->trie,
1746 &address.socket.inet.src_ipaddr.addr, address.socket.inet.src_ipaddr.prefix);
1747 fr_assert(!client || !client->connection);
1748
1749 /*
1750 * Verify the cached client is the most specific match.
1751 * A broader subnet may have been cached first, shadowing
1752 * a more specific client definition.
1753 */
1754 if (client && (client->state == PR_CLIENT_STATIC)) {
1755 fr_client_t *radclient;
1756
1757 radclient = inst->app_io->client_find(thread->child_inst,
1758 &address.socket.inet.src_ipaddr, inst->ipproto);
1759 if (radclient && (radclient->ipaddr.prefix > client->src_ipaddr.prefix)) {
1760 client = NULL;
1761 }
1762 }
1763
1764 } else {
1765 client = connection->client;
1766
1767 /*
1768 * We don't care what the read function says
1769 * about address. We have it already.
1770 */
1771 address = *connection->address;
1772 }
1773
1774 /*
1775 * No client was found, and we don't have a connection. Check the NAK cache before defining a
1776 * dynamic client.
1777 *
1778 * The NAK cache has already been checked on the TCP path above, after the accept().
1779 */
1780 if (!client && !connection &&
1781 fr_io_nak_find(thread, &address.socket.inet.src_ipaddr)) {
1782 RATE_LIMIT_LOCAL(&thread->rate_limit.repeat_nak, ERROR,
1783 "proto_%s - Discarding packet from NAK'd dynamic client %pV",
1784 inst->app_io->common.name, fr_box_ipaddr(address.socket.inet.src_ipaddr));
1785 if (accept_fd >= 0) close(accept_fd);
1786 return 0;
1787 }
1788
1789 /*
1790 * If there's no client, try to pull one from the global
1791 * / static client list. Or if dynamic clients are
1792 * allowed, try to define a dynamic client.
1793 */
1794 if (!client) {
1795 fr_client_t *radclient = NULL;
1797 fr_ipaddr_t const *network = NULL;
1798 char const *error;
1799
1800 /*
1801 * We MUST be the master socket.
1802 */
1803 fr_assert(!connection);
1804
1805 radclient = inst->app_io->client_find(thread->child_inst, &address.socket.inet.src_ipaddr,
1806 inst->ipproto);
1807 if (radclient) {
1808 state = PR_CLIENT_STATIC;
1809
1810 /*
1811 * Make our own copy that we can modify it.
1812 */
1813 MEM(radclient = radclient_clone(thread, radclient));
1814 radclient->active = true;
1815
1816 } else if (inst->dynamic_clients) {
1817 if (inst->max_clients && (fr_heap_num_elements(thread->alive_clients) >= inst->max_clients)) {
1818 error = "Too many dynamic clients have been defined";
1819 goto ignore;
1820 }
1821
1822 /*
1823 * Look up the allowed networks.
1824 */
1825 network = fr_trie_lookup_by_key(inst->networks, &address.socket.inet.src_ipaddr.addr,
1826 address.socket.inet.src_ipaddr.prefix);
1827 if (!network) {
1828 error = "Address is outside of the the 'allow' network range";
1829 goto ignore;
1830 }
1831
1832 /*
1833 * It exists, but it's a "deny" rule, ignore it.
1834 */
1835 if (network->af == AF_UNSPEC) {
1836 error = "Address is forbidden by the 'deny' network range";
1837 goto ignore;
1838 }
1839
1840 /*
1841 * Allocate our local radclient as a
1842 * placeholder for the dynamic client.
1843 */
1844 radclient = radclient_alloc(thread, inst->ipproto, &address);
1845 state = PR_CLIENT_PENDING;
1846
1847 } else {
1848 char const *msg;
1849
1850 error = "No matching 'client' definition was found";
1851
1852 ignore:
1853 if (accept_fd < 0) {
1854 msg = "packet";
1855 } else {
1856 msg = "connection attempt";
1857 close(accept_fd);
1858 }
1859
1861 static fr_rate_limit_t unknown_client;
1862 RATE_LIMIT_LOCAL(thread ? &thread->rate_limit.unknown_client : &unknown_client,
1863 ERROR, "proto_%s - Ignoring %s from IP address %pV - %s",
1864 inst->app_io->common.name, msg, fr_box_ipaddr(address.socket.inet.src_ipaddr),
1865 error);
1866 }
1867
1868 return 0;
1869 }
1870
1871 MEM(client = client_alloc(thread, state, inst, thread, radclient, network));
1872
1873 /*
1874 * Parent the dynamic client radclient off the client - it
1875 * is the client which gets freed by the dynamic client timers.
1876 */
1877 if (state == PR_CLIENT_PENDING) talloc_steal(client, radclient);
1878 }
1879
1880have_client:
1881 fr_assert(client->state != PR_CLIENT_INVALID);
1882
1883 /*
1884 * We've accepted a new connection. Go allocate it, and
1885 * let it read from the socket.
1886 */
1887 if (accept_fd >= 0) {
1888 connection = fr_io_connection_alloc(inst, thread, client, accept_fd, &address);
1889 if (!connection) {
1890 static fr_rate_limit_t alloc_failed;
1891
1892 RATE_LIMIT_LOCAL(thread ? &thread->rate_limit.conn_alloc_failed : &alloc_failed,
1893 ERROR, "Failed to allocate connection from client %s", client->radclient->shortname);
1894 return 0;
1895 }
1896
1897 /*
1898 * The parent is in use - ensure the cleanup timer is disarmed.
1899 */
1900 if (fr_timer_armed(connection->parent->ev)) {
1901 FR_TIMER_DISARM_RETURN(connection->parent->ev);
1902 connection->parent->ready_to_delete = false;
1903 }
1904
1905 return 0;
1906 }
1907
1908 /*
1909 * No connected sockets, OR we are the connected socket.
1910 *
1911 * Track this packet and return it if necessary.
1912 */
1913 if (connection || !client->use_connected) {
1914 fr_io_track_t *to_free = NULL;
1915
1916 /*
1917 * Add the packet to the tracking table, if it's
1918 * not already there. Pending packets will be in
1919 * the tracking table, but won't be counted as
1920 * "live" packets.
1921 */
1922 if (!track) {
1923 static fr_rate_limit_t tracking_failed;
1924 bool is_dup = false;
1925
1926 track = fr_io_track_add(li, client, &address, buffer, packet_len, recv_time, &is_dup);
1927 if (!track) {
1928 RATE_LIMIT_LOCAL(thread ? &thread->rate_limit.tracking_failed : &tracking_failed,
1929 ERROR, "Failed tracking packet from client %s - discarding it",
1930 client->radclient->shortname);
1931 return 0;
1932 }
1933
1934 /*
1935 * If there's a cached reply, just send that and don't do anything else.
1936 */
1937 if (is_dup) {
1938 fr_network_t *nr;
1939
1940 if (track->do_not_respond) {
1941 DEBUG("Ignoring retransmit from client %s - we are not responding to this request", client->radclient->shortname);
1942 return 0;
1943 }
1944
1945 if (track->discard) {
1946 DEBUG("Ignoring transmit from client %s - we previously received a newer / conflicting packet", client->radclient->shortname);
1947 return 0;
1948 }
1949
1950 if (!track->reply) {
1951 fr_assert(!track->finished);
1952 DEBUG("Ignoring retransmit from client %s - we are still processing the request", client->radclient->shortname);
1953 return 0;
1954 }
1955
1956 if (connection) {
1957 nr = connection->nr;
1958 } else {
1959 nr = thread->nr;
1960 }
1961
1962 /*
1963 * @todo - mark things up so that we know to keep 'track' around
1964 * until the packet is actually written to the network. OR, add
1965 * a network API so that the talloc_free() function can remove
1966 * the packet from the queue of packets to be retransmitted.
1967 *
1968 * Perhaps via having fr_network_listen_write() return a pointer
1969 * to the localized message, and then caching that in the tracking
1970 * structure.
1971 */
1972 DEBUG("Sending duplicate reply to client %s", client->radclient->shortname);
1973 fr_network_listen_write(nr, li, track->reply, track->reply_len,
1974 track, track->timestamp);
1975 return 0;
1976 }
1977
1978 /*
1979 * Got to free this if we don't process the packet.
1980 */
1981 to_free = track;
1982 }
1983
1984 /*
1985 * This is a pending dynamic client. See if we
1986 * have to either run the dynamic client code to
1987 * define the client, OR to push the packet onto
1988 * the pending queue for this client.
1989 */
1990 if (client->state == PR_CLIENT_PENDING) {
1991 /*
1992 * Track pending packets for the master
1993 * socket. Connected sockets are paused
1994 * as soon as they are defined, so we
1995 * won't be reading any more packets from
1996 * them.
1997 *
1998 * Since we don't have pending packets
1999 * for connected sockets, we don't need
2000 * to track pending packets.
2001 */
2002 if (!connection && inst->max_pending_packets && (thread->num_pending_packets >= inst->max_pending_packets)) {
2003 RATE_LIMIT_LOCAL(&thread->rate_limit.too_many_pending,
2004 ERROR, "Too many pending dynamic client packets for listener - discarding packet from %pV",
2005 fr_box_ipaddr(client->src_ipaddr));
2006
2007 discard:
2008 talloc_free(to_free);
2009 return 0;
2010 }
2011
2012 /*
2013 * Allocate the pending packet structure.
2014 */
2015 pending = fr_io_pending_alloc(connection, client, buffer, packet_len,
2016 track, priority);
2017 if (!pending) {
2018 static fr_rate_limit_t alloc_failed;
2019 RATE_LIMIT_LOCAL(thread ? &thread->rate_limit.alloc_failed : &alloc_failed,
2020 ERROR, "proto_%s - Failed allocating space for dynamic client %pV - discarding packet",
2021 inst->app_io->common.name, fr_box_ipaddr(client->src_ipaddr));
2022 goto discard;
2023 }
2024
2025 if (fr_heap_num_elements(client->pending) > 1) {
2026 DEBUG("Verification is still pending for dynamic client %pV - queuing additional packet(s)",
2027 fr_box_ipaddr(client->src_ipaddr));
2028 return 0;
2029 }
2030
2031 /*
2032 * Tell this packet that it's defining a
2033 * dynamic client.
2034 */
2035 track->dynamic = recv_time;
2036
2037 INFO("proto_%s - Verification started for packet from dynamic client %pV - queuing new packets",
2038 inst->app_io->common.name, fr_box_ipaddr(client->src_ipaddr));
2039 }
2040
2041 /*
2042 * Remove all cleanup timers for the client /
2043 * connection. It's still in use, so we don't
2044 * want to clean it up.
2045 */
2046 if (fr_timer_armed(client->ev)) {
2047 FR_TIMER_DISARM_RETURN(client->ev);
2048 client->ready_to_delete = false;
2049 }
2050
2051 /*
2052 * Remove cleanup timers for the connection parent.
2053 */
2054 if (connection && fr_timer_armed(connection->parent->ev)) {
2055 FR_TIMER_DISARM_RETURN(connection->parent->ev);
2056 connection->parent->ready_to_delete = false;
2057 }
2058
2059 /*
2060 * Return the packet.
2061 */
2062 *recv_time_p = track->timestamp;
2063 *packet_ctx = track;
2064 return packet_len;
2065 }
2066
2067 /*
2068 *
2069 */
2070 fr_assert(!pending);
2071
2072 /*
2073 * This must be the main UDP socket which creates
2074 * connections.
2075 */
2076 fr_assert(inst->ipproto == IPPROTO_UDP);
2077
2078 /*
2079 * We're using connected sockets, but this socket isn't
2080 * connected. It must be the master socket. The master
2081 * can either be STATIC, DYNAMIC, or PENDING. Whatever
2082 * the state, the child socket will take care of handling
2083 * the packet. e.g. dynamic clients, etc.
2084 */
2085 my_connection.address = &address;
2086
2087 pthread_mutex_lock(&client->mutex);
2088 fr_hash_table_find((void **)&connection, client->ht, &my_connection);
2089 pthread_mutex_unlock(&client->mutex);
2090
2091 /*
2092 * No existing connection, create one.
2093 */
2094 if (!connection) {
2095 connection = fr_io_connection_alloc(inst, thread, client, -1, &address);
2096 if (!connection) {
2097 RATE_LIMIT_LOCAL(&thread->rate_limit.conn_alloc_failed,
2098 ERROR, "Failed to allocate connection from client %s. Discarding packet.", client->radclient->shortname);
2099 return 0;
2100 }
2101 }
2102
2103 DEBUG("Sending packet to connection %s", connection->name);
2104
2105 /*
2106 * Inject the packet into the connected socket. It will
2107 * process the packet as if it came in from the network.
2108 *
2109 * @todo future - after creating the connection, put the
2110 * current packet into connection->pending, instead of
2111 * inject?, and then call fr_network_listen_read() from
2112 * the child's instantiation routine???
2113 *
2114 * @todo TCP - for ACCEPT sockets, we don't have a
2115 * packet, so don't do this. Instead, the connection
2116 * will take care of figuring out what to do.
2117 *
2118 * We don't need "to_free" after this, as it will be
2119 * tracked in the connected socket.
2120 */
2121 if (fr_network_listen_inject(connection->nr, connection->inst,
2122 buffer, packet_len, recv_time) < 0) {
2123 RATE_LIMIT_LOCAL(&thread->rate_limit.queue_full, PERROR,
2124 "proto_%s - Discarding packet from dynamic client %pV - cannot push packet to connected socket",
2125 inst->app_io->common.name, fr_box_ipaddr(address.socket.inet.src_ipaddr));
2126 /*
2127 * Don't return an error, because that will cause the listener to close its socket.
2128 */
2129 }
2130
2131 return 0;
2132}
2133
2134/** Inject a packet to a connection.
2135 *
2136 * Always called in the context of the network.
2137 */
2138static int mod_inject(fr_listen_instance_t *li, uint8_t const *buffer, size_t buffer_len, fr_time_t recv_time)
2139{
2140 fr_io_instance_t const *inst;
2141 int priority;
2142 bool is_dup = false;
2143 fr_io_connection_t *connection;
2144 fr_io_pending_packet_t *pending;
2145 fr_io_track_t *track;
2146
2147 get_inst(li, &inst, NULL, &connection, NULL);
2148
2149 if (!connection) {
2150 DEBUG2("Received injected packet for an unconnected socket.");
2151 return -1;
2152 }
2153
2154 if (inst->app->priority) {
2155 priority = inst->app->priority(inst->app_instance, buffer, buffer_len);
2156 if (priority <= 0) {
2157 return -1;
2158 }
2159 } else {
2160 priority = PRIORITY_NORMAL;
2161 }
2162
2163 /*
2164 * Track this packet, because that's what mod_read expects.
2165 */
2166 track = fr_io_track_add(li, connection->client, connection->address,
2167 buffer, buffer_len, recv_time, &is_dup);
2168 if (!track) {
2169 DEBUG2("Failed injecting packet to tracking table");
2170 return -1;
2171 }
2172
2173 talloc_get_type_abort(track, fr_io_track_t);
2174
2175 /*
2176 * @todo future - what to do with duplicates?
2177 */
2178 fr_assert(!is_dup);
2179
2180 /*
2181 * Remember to restore this packet later.
2182 */
2183 pending = fr_io_pending_alloc(connection, connection->client, buffer, buffer_len,
2184 track, priority);
2185 if (!pending) {
2186 DEBUG2("Failed injecting packet due to allocation error");
2187 return -1;
2188 }
2189
2190 return 0;
2191}
2192
2193/** Open a new listener
2194 *
2195 */
2197{
2198 fr_io_thread_t *thread;
2199 fr_io_instance_t const *inst;
2200
2201 thread = li->thread_instance;
2203
2204 if (inst->app_io->open(thread->child_inst) < 0) return -1;
2205
2206 li->fd = thread->child_inst->fd; /* copy this back up */
2207
2208 /*
2209 * Set the name of the socket.
2210 */
2211 if (!li->listen->app_io->get_name) {
2212 li->name = li->listen->app_io->common.name;
2213 } else {
2214 li->name = li->listen->app_io->get_name(li);
2215 }
2216
2217 /*
2218 * Note that we're opening a child socket, so we don't
2219 * put it into the list of global listeners.
2220 */
2221
2222 return 0;
2223}
2224
2225
2226/** Set the event list for a new socket
2227 *
2228 * @param[in] li the listener
2229 * @param[in] el the event list
2230 * @param[in] nr context from the network side
2231 */
2233{
2234 fr_io_instance_t const *inst;
2235 fr_io_connection_t *connection;
2236 fr_io_thread_t *thread;
2237 fr_listen_instance_t *child;
2238
2239 get_inst(li, &inst, &thread, &connection, &child);
2240
2241 /*
2242 * We're not doing IO, so there are no timers for
2243 * cleaning up packets, dynamic clients, or connections.
2244 */
2245 if (!inst->submodule) return;
2246
2247 if (inst->app_io->event_list_set) {
2248 inst->app_io->event_list_set(child, el, nr);
2249 }
2250
2251 /*
2252 * Set event list and network side for this socket.
2253 */
2254 if (!connection) {
2255 thread->el = el;
2256 thread->nr = nr;
2257
2258 } else {
2259 connection->el = el;
2260 connection->nr = nr;
2261 }
2262}
2263
2264
2265/** Delete a client and any connection it belongs to.
2266 *
2267 * A connected socket owns a client, so we have to remove the connection from the parent "accept" connection.
2268 * We also mark the connection as dead, and tell the network side to read from the connection, which then
2269 * closes it.
2270 */
2272{
2273 fr_io_connection_t *connection = client->connection;
2274
2275 fr_assert(client->packets == 0);
2276
2277 if (connection) {
2278 pthread_mutex_lock(&connection->parent->mutex);
2279 if (connection->in_parent_hash) {
2280 connection->in_parent_hash = false;
2281 (void) fr_hash_table_delete(connection->parent->ht, connection);
2282 }
2283 pthread_mutex_unlock(&connection->parent->mutex);
2284
2285 connection->dead = true;
2286 fr_network_listen_read(connection->nr, connection->inst);
2287 return;
2288 }
2289
2290 talloc_free(client);
2291}
2292
2293static void client_expiry_timer(fr_timer_list_t *tl, fr_time_t now, void *uctx)
2294{
2295 fr_io_client_t *client = talloc_get_type_abort(uctx, fr_io_client_t);
2296 fr_io_instance_t const *inst;
2297 fr_io_connection_t *connection;
2298 fr_time_delta_t delay;
2299 int connections;
2300
2301 /*
2302 * No event list? We don't need to expire the client.
2303 */
2304 if (!tl) return;
2305
2306 // @todo - print out what we plan on doing next
2307 connection = client->connection;
2308 inst = client->inst;
2309
2310 fr_assert(client->state != PR_CLIENT_STATIC);
2311
2312 /*
2313 * Called from the read or write functions with
2314 * now==0, to signal that we have to *set* the timer.
2315 */
2316 if (fr_time_eq(now, fr_time_wrap(0))) {
2317 /*
2318 * The timer is already set, don't do anything.
2319 */
2320 if (fr_timer_armed(client->ev)) return;
2321
2322 switch (client->state) {
2324 fr_assert(connection != NULL);
2325 delay = inst->idle_timeout;
2327 (fr_time_delta_lt(client->radclient->limit.idle_timeout, inst->idle_timeout))) {
2328 delay = client->radclient->limit.idle_timeout;
2329 }
2330 break;
2331
2332 case PR_CLIENT_DYNAMIC:
2333 delay = inst->dynamic_timeout;
2334 break;
2335
2336 default:
2337 fr_assert(0 == 1);
2338 return;
2339 }
2340
2341 DEBUG("TIMER - setting idle timeout to %pVs for connection from client %s", fr_box_time_delta(delay), client->radclient->shortname);
2342
2343 goto reset_timer;
2344 }
2345
2346 DEBUG2("TIMER - checking status of dynamic client %s %pV", client->radclient->shortname, fr_box_ipaddr(client->src_ipaddr));
2347
2348 /*
2349 * It's a dynamically defined client. If no one is using
2350 * it, clean it up after an idle timeout.
2351 */
2352 if ((client->state == PR_CLIENT_DYNAMIC) ||
2353 (client->state == PR_CLIENT_CONNECTED)) {
2354 if (client->packets > 0) {
2355 client->ready_to_delete = false;
2356 return;
2357 }
2358
2359 /*
2360 * No packets, check / set idle timeout.
2361 */
2362 goto idle_timeout;
2363 }
2364
2365 /*
2366 * The client is pending definition. It's either a
2367 * dynamic client which has timed out, OR it's a
2368 * "place-holder" client for connected sockets.
2369 */
2370 fr_assert(client->state == PR_CLIENT_PENDING);
2371
2372 /*
2373 * This is a dynamic client pending definition.
2374 * But it's taken too long to define, so we just
2375 * delete the client, and all packets for it. A
2376 * new packet will cause the dynamic definition
2377 * to be run again.
2378 */
2379 if (!client->use_connected) {
2380 if (!client->packets) {
2381 DEBUG("proto_%s - No packets are using unconnected socket", inst->app_io->common.name);
2382 fr_io_client_delete(client);
2383 return;
2384 }
2385
2386 /*
2387 * Tell the writer to NOT dynamically define the
2388 * client. We've run into a problem. Then,
2389 * return. The writer will take care of calling
2390 * us again when it notices that a PENDING client
2391 * is ready to delete.
2392 *
2393 * TBH... that shouldn't happen? We should rely
2394 * on the write to do this all of the time...
2395 */
2396 client->ready_to_delete = true;
2397 return;
2398 }
2399
2400 fr_assert(!connection);
2401
2402 /*
2403 * Find out how many connections are using this
2404 * client.
2405 */
2406 pthread_mutex_lock(&client->mutex);
2407 fr_assert(client->ht != NULL);
2408 connections = fr_hash_table_num_elements(client->ht);
2409 pthread_mutex_unlock(&client->mutex);
2410
2411 /*
2412 * No connections are using this client. If
2413 * we've passed the idle timeout, then just
2414 * delete it. Otherwise, set an idle timeout (as
2415 * above);
2416 */
2417 if (!connections) {
2418idle_timeout:
2419 /*
2420 * We didn't receive any packets during the
2421 * idle_timeout, just delete it.
2422 */
2423 if (client->ready_to_delete) {
2424 if (connection) {
2425 DEBUG("proto_%s - idle timeout for connection %s", inst->app_io->common.name, connection->name);
2426 } else {
2427 DEBUG("proto_%s - idle timeout for client %s", inst->app_io->common.name, client->radclient->shortname);
2428 }
2429 fr_io_client_delete(client);
2430 return;
2431 }
2432
2433 /*
2434 * No packets and no idle timeout set, go set
2435 * idle timeut.
2436 */
2437 client->ready_to_delete = true;
2438 delay = client->state == PR_CLIENT_DYNAMIC ? inst->dynamic_timeout : inst->idle_timeout;
2439 goto reset_timer;
2440 }
2441
2442 /*
2443 * There are live sub-connections. Poll again after a
2444 * long period of time. Once all of the connections are
2445 * closed, we can then delete this client.
2446 *
2447 * @todo - maybe just leave it? we want to be able to
2448 * clean up this client after a while tho... especially
2449 * if the total number of clients is limited.
2450 */
2451 client->ready_to_delete = false;
2452 delay = inst->check_interval;
2453
2454reset_timer:
2455 if (fr_timer_in(client, tl, &client->ev,
2456 delay, false, client_expiry_timer, client) < 0) {
2457 ERROR("proto_%s - Failed adding timeout for dynamic client %s. It will be permanent!",
2458 inst->app_io->common.name, client->radclient->shortname);
2459 return;
2460 }
2461
2462 return;
2463}
2464
2465
2466/*
2467 * Expire cached packets after cleanup_delay time
2468 */
2469static void packet_expiry_timer(fr_timer_list_t *tl, fr_time_t now, void *uctx)
2470{
2471 fr_io_track_t *track = talloc_get_type_abort(uctx, fr_io_track_t);
2472 fr_io_client_t *client = track->client;
2473 fr_io_instance_t const *inst = client->inst;
2474
2475 /*
2476 * Insert the timer if requested.
2477 *
2478 * On duplicates this also extends the expiry timer.
2479 */
2480 if (fr_time_eq(now, fr_time_wrap(0)) && !track->discard && inst->app_io->track_duplicates) {
2481 fr_assert(fr_time_delta_ispos(inst->cleanup_delay));
2482 fr_assert(track->do_not_respond || track->reply_len);
2483
2484 track->expires = fr_time_add(fr_time(), inst->cleanup_delay);
2485
2486 /*
2487 * if the timer succeeds, then "track"
2488 * will be cleaned up when the timer
2489 * fires.
2490 */
2491 if (fr_timer_at(track, tl, &track->ev,
2492 track->expires,
2493 false, packet_expiry_timer, track) == 0) {
2494 DEBUG("proto_%s - cleaning up request in %.6fs", inst->app_io->common.name,
2495 fr_time_delta_to_sec_double(inst->cleanup_delay));
2496 return;
2497 }
2498
2499 DEBUG("proto_%s - Failed adding cleanup_delay for packet. Discarding packet immediately",
2500 inst->app_io->common.name);
2501 }
2502
2503 /*
2504 * So that all cleanup paths can come here, not just the
2505 * timeout ones.
2506 */
2507 if (fr_time_neq(now, fr_time_wrap(0))) {
2508 DEBUG2("TIMER - proto_%s - cleanup delay", inst->app_io->common.name);
2509 } else {
2510 DEBUG2("proto_%s - cleaning up", inst->app_io->common.name);
2511 }
2512
2513 /*
2514 * Delete the tracking entry.
2515 */
2516 talloc_free(track);
2517
2518 /*
2519 * The client isn't dynamic, stop here.
2520 */
2521 if (client->state == PR_CLIENT_STATIC) return;
2522
2523 fr_assert(client->state != PR_CLIENT_PENDING);
2524
2525 /*
2526 * If necessary, call the client expiry timer to clean up
2527 * the client.
2528 */
2529 if (client->packets == 0) {
2530 client_expiry_timer(tl, now, client);
2531 }
2532}
2533
2534static void update_client(fr_io_client_t *client, fr_client_t *radclient)
2535{
2536
2537 /*
2538 * The new client is mostly OK. Copy the various fields
2539 * over.
2540 */
2541#define COPY_FIELD(_dest, _x) _dest->radclient->_x = radclient->_x
2542#define DUP_FIELD(_dest, _x) _dest->radclient->_x = talloc_strdup(_dest->radclient, radclient->_x)
2543
2544 /*
2545 * Only these two fields are set. Other strings in
2546 * radclient are copies of these ones.
2547 */
2550
2551 DUP_FIELD(client, longname);
2552 DUP_FIELD(client, shortname);
2553 DUP_FIELD(client, secret);
2554 DUP_FIELD(client, nas_type);
2555
2556 COPY_FIELD(client, ipaddr);
2557 COPY_FIELD(client, src_ipaddr);
2558 COPY_FIELD(client, require_message_authenticator);
2559 COPY_FIELD(client, require_message_authenticator_is_set);
2560#ifdef NAS_VIOLATES_RFC
2561 COPY_FIELD(client, allow_vulnerable_clients);
2562#endif
2563 COPY_FIELD(client, limit_proxy_state);
2564 COPY_FIELD(client, limit_proxy_state_is_set);
2565 COPY_FIELD(client, use_connected);
2566 COPY_FIELD(client, cs);
2567}
2568
2569/** Tear down any deferred sibling connections under a pending parent.
2570 *
2571 * This function is Cclled from mod_write() when the first connection dynamic-client verification fails (NAK
2572 * or hard reject). Any other connections that were inserted into parent->ht are then freed.
2573 *
2574 * Walk parent->ht, find every deferred sibling, remove it from the hash table, close the socket, and frees
2575 * its module instance (which cascades to the connection itself). The first connection (the one whose
2576 * verification just failed) is identified by conn->nr != NULL and is left alone, as the caller already owns
2577 * its cleanup.
2578 *
2579 * Uses a find-one-then-restart loop rather than iterate-while-deleting, since the deferred sibling count is
2580 * small and the cost is irrelevant on the failure path.
2581 */
2583{
2584 if (!parent->ht) return;
2585
2586 while (true) {
2587 fr_hash_iter_t iter;
2588 fr_io_connection_t *target = NULL;
2589 fr_io_connection_t *conn;
2590
2591 pthread_mutex_lock(&parent->mutex);
2592 for (conn = fr_hash_table_iter_init(parent->ht, &iter);
2593 conn != NULL;
2594 conn = fr_hash_table_iter_next(parent->ht, &iter)) {
2595 if (conn->nr != NULL) continue; /* first child, owned by caller */
2596 if (conn->client->state != PR_CLIENT_PENDING) continue; /* already NAK or promoted */
2597 target = conn;
2598 break;
2599 }
2600 if (target && target->in_parent_hash) {
2601 target->in_parent_hash = false;
2602 (void) fr_hash_table_delete(parent->ht, target);
2603 }
2604 pthread_mutex_unlock(&parent->mutex);
2605
2606 if (!target) break;
2607
2608 DEBUG("proto_%s - cleaning up deferred connection %s after verification failure",
2609 target->client->inst->app_io->common.name, target->name);
2610
2611 if (target->child_inst) {
2612 if (target->client->inst->app_io->close) {
2613 (void) target->client->inst->app_io->close(target->child_inst);
2614 } else if (target->child_inst->fd >= 0) {
2615 close(target->child_inst->fd);
2616 }
2617 }
2618
2619 talloc_free(target->mi);
2620 }
2621}
2622
2623/** Promote a pending dynamic-client parent and all of its child connections.
2624 *
2625 * When multiple TCP connections from the same source IP arrive while the parent client is still
2626 * PR_CLIENT_PENDING, only the first connection is added to the scheduler. Later connection are inserted
2627 * into the parent's hash table, but their fr_schedule_listen_add() call is deferred to this function. When
2628 * the first connection verification completes successfully, every other connection is finished and
2629 * scheduled.
2630 *
2631 * This function:
2632 *
2633 * * Promotes the parent itself to PR_CLIENT_DYNAMIC and re-parents the cs.
2634 *
2635 * * Walks the parent's hash table of connections. For the first connection (already in the scheduler with
2636 * connection->nr set), it resumes reads if the connection was paused. For every later connection,
2637 * (connection->nr == NULL), it copies the verified radclient definition onto the connection and then calls
2638 * fr_schedule_listen_add() to add the connection to the scheduler.
2639 *
2640 * @param parent the parent client whose state and child connections to promote
2641 * @param radclient the verified radclient (typically the calling child's radclient)
2642 */
2644{
2645 fr_hash_iter_t iter;
2646 fr_io_connection_t *conn;
2647 fr_schedule_t *sc = parent->thread->sc;
2648
2649 /*
2650 * Promote the parent itself. Only the first connection to reach here does the work; later
2651 * connections will already see the parent as PR_CLIENT_DYNAMIC.
2652 */
2653 if (parent->state == PR_CLIENT_PENDING) {
2654 parent->radclient->active = true;
2655 parent->state = PR_CLIENT_DYNAMIC;
2656
2657 update_client(parent, radclient);
2658
2659 /*
2660 * Re-parent the conf section used to build this
2661 * client so its lifetime is linked to the parent
2662 * client.
2663 */
2664 talloc_steal(parent->radclient, parent->radclient->cs);
2665 } else {
2667 }
2668
2669 /*
2670 * Walk every child connection of this parent and promote the pending ones. The calling
2671 * connection is itself in parent->ht, so its per-child work is also done here.
2672 */
2673 pthread_mutex_lock(&parent->mutex);
2674 if (parent->ht) {
2675 for (conn = fr_hash_table_iter_init(parent->ht, &iter);
2676 conn != NULL;
2677 conn = fr_hash_table_iter_next(parent->ht, &iter)) {
2678 fr_io_client_t *child = conn->client;
2679
2680 if (child->state != PR_CLIENT_PENDING) continue;
2681
2682 /*
2683 * Connections can't spawn new connections.
2684 */
2685 child->use_connected = child->radclient->use_connected = false;
2686
2687 if (conn->nr == NULL) {
2688 /*
2689 * Deferred connection: its radclient was cloned from the parent's
2690 * placeholder before verification, so copy the now-verified fields onto
2691 * it before adding it to the scheduler.
2692 */
2693 update_client(child, radclient);
2694
2695 child->state = PR_CLIENT_DYNAMIC;
2696 child->radclient->active = true;
2697
2698 DEBUG("proto_%s - scheduling deferred connection %s",
2699 child->inst->app_io->common.name, conn->name);
2700
2701 conn->nr = fr_schedule_listen_add(sc, conn->inst);
2702 if (!conn->nr) {
2703 ERROR("proto_%s - Failed scheduling deferred connection %s",
2704 child->inst->app_io->common.name, conn->name);
2705 /*
2706 * Leave the entry in the hash table; the usual connection
2707 * cleanup path will eventually remove it.
2708 */
2709 }
2710 } else {
2711 /*
2712 * The first connection is already scheduled. Resume reads if it was
2713 * paused, while waiting on verification.
2714 */
2715 if (conn->paused) {
2716 conn->paused = false;
2717 (void) fr_event_filter_update(conn->el, conn->child_inst->fd,
2719 }
2720
2721 child->state = PR_CLIENT_DYNAMIC;
2722 child->radclient->active = true;
2723 }
2724 }
2725 }
2726 pthread_mutex_unlock(&parent->mutex);
2727}
2728
2730 size_t buffer_len, size_t written)
2731{
2732 fr_io_instance_t const *inst;
2733 fr_io_thread_t *thread;
2734 fr_io_connection_t *connection;
2735 fr_io_track_t *track = talloc_get_type_abort(packet_ctx, fr_io_track_t);
2736 fr_io_client_t *client;
2737 fr_client_t *radclient;
2738 fr_listen_instance_t *child;
2740 char const *name;
2741
2742 get_inst(li, &inst, &thread, &connection, &child);
2743
2744 client = track->client;
2745 if (connection) {
2746 el = connection->el;
2747 name = connection->name;
2748 } else {
2749 el = thread->el;
2750 name = li->name;
2751 }
2752
2753 DEBUG3("Processing reply for %s", name);
2754
2755 /*
2756 * A fully defined client means that we just send the reply.
2757 */
2758 if (client->state != PR_CLIENT_PENDING) {
2759 ssize_t packet_len;
2760
2761 track->finished = true;
2762
2763 /*
2764 * The request received a conflicting packet, so we
2765 * discard this one.
2766 */
2767 if (fr_time_neq(track->timestamp, request_time) || track->discard) {
2768 fr_assert(track->packets > 0);
2769 track->packets--;
2770 DEBUG3("Suppressing reply as we have a newer / conflicing packet from the same source");
2771 track->discard = true;
2772 goto setup_timer;
2773 }
2774
2775 /*
2776 * We have a NAK packet, or the request has timed
2777 * out, or it was discarded due to a conflicting
2778 * packet. We don't respond, but we do cache the
2779 * "do not respond" reply for a period of time.
2780 */
2781 if ((buffer_len == 1) || track->do_not_respond) {
2782 DEBUG3("Not sending response to request - it is marked as 'do not respond'");
2783 track->do_not_respond = true;
2784 goto setup_timer;
2785 }
2786
2787 /*
2788 * We have a real packet, write it to the network
2789 * via the underlying transport write.
2790 */
2791 packet_len = inst->app_io->write(child, track, request_time,
2792 buffer, buffer_len, written);
2793 if (packet_len <= 0) {
2794 ERROR("Failed writing the reply - not sending any response on %s", name);
2795 track->discard = true;
2796 packet_expiry_timer(el->tl, fr_time_wrap(0), track);
2797 return packet_len;
2798 }
2799
2800 /*
2801 * Only a partial write. The network code will
2802 * take care of calling us again, and we will set
2803 * the expiry timer at that point.
2804 */
2805 if ((size_t) packet_len < buffer_len) {
2806 DEBUG3("Partial write (%zd < %zu)", packet_len, buffer_len);
2807 return packet_len;
2808 }
2809
2810 /*
2811 * We're not tracking duplicates, so just expire
2812 * the packet now.
2813 */
2814 if (!inst->app_io->track_duplicates) {
2815 DEBUG3("Not tracking duplicates - expiring the request");
2816 goto setup_timer;
2817 }
2818
2819 /*
2820 * Cache the reply packet if we're doing dedup.
2821 *
2822 * On resend duplicate reply, the reply is
2823 * already filled out. So we don't do that twice.
2824 */
2825 if (!track->reply) {
2826 DEBUG3("Caching reply");
2827 MEM(track->reply = talloc_memdup(track, buffer, buffer_len));
2828 track->reply_len = buffer_len;
2829 }
2830
2831 /*
2832 * Set the timer to expire the packet.
2833 *
2834 * On dedup this also extends the timer.
2835 */
2836 setup_timer:
2837 packet_expiry_timer(el->tl, fr_time_wrap(0), track);
2838 return buffer_len;
2839 }
2840
2841 /*
2842 * The client is pending, so we MUST have dynamic clients.
2843 *
2844 * If there's a connected socket and no dynamic clients, then the
2845 * client state is set to CONNECTED when the client is created.
2846 */
2847 fr_assert(inst->dynamic_clients);
2848 fr_assert(client->pending != NULL);
2849
2850 /*
2851 * We have a response that indicates the dynamic client creation failed. Delete the client, and
2852 * all pending packets. Then, add the connection to the NAK cache.
2853 */
2854 if (buffer_len == 1) {
2855 if (*buffer == true) {
2856 DEBUG("Request failed trying to define a new client. Discarding client and pending packets.");
2857 } else {
2858 INFO("proto_%s - Verification failed for packet from dynamic client %pV - adding IP address to the NAK cache",
2859 inst->app_io->common.name, fr_box_ipaddr(client->src_ipaddr));
2860
2861 fr_io_nak_insert(client->thread, &client->src_ipaddr, inst->nak_lifetime);
2862 }
2863
2864 if (!connection) {
2865 talloc_free(client);
2866 return buffer_len;
2867 }
2868
2869 /*
2870 * Free pending packets and tracking table.
2871 * The table is parented by connection->parent, so won't
2872 * be auto-freed when connection->client is freed.
2873 */
2874 TALLOC_FREE(client->pending);
2875 if (client->table) TALLOC_FREE(client->table);
2876
2877 /*
2878 * Remove from parent's hash table so new packets won't
2879 * be routed to this connection.
2880 */
2881 pthread_mutex_lock(&connection->parent->mutex);
2882 if (connection->in_parent_hash) {
2883 connection->in_parent_hash = false;
2884 (void) fr_hash_table_delete(connection->parent->ht, connection);
2885 }
2886 pthread_mutex_unlock(&connection->parent->mutex);
2887
2888 /*
2889 * Tear down any sibling connections that were
2890 * deferred waiting on this verification.
2891 */
2892 fr_io_connection_deny(connection->parent);
2893
2894 /*
2895 * Mark the connection as dead, then trigger the
2896 * standard cleanup path via fr_network_listen_read().
2897 * This calls mod_read(), which sees connection->dead,
2898 * returns -1, and the network layer closes the
2899 * connection through its normal error handling.
2900 */
2901 connection->dead = true;
2902 fr_network_listen_read(connection->nr, connection->inst);
2903
2904 return buffer_len;
2905 }
2906
2907 fr_assert(buffer_len == sizeof(radclient));
2908
2909 memcpy(&radclient, buffer, sizeof(radclient));
2910
2911 if (!connection) {
2912 fr_ipaddr_t ipaddr;
2913
2914 /*
2915 * Check the encapsulating network against the
2916 * address that the user wants to use, but only
2917 * for unconnected sockets.
2918 */
2919 if (client->network.af != radclient->ipaddr.af) {
2920 DEBUG("Client IP address %pV IP family does not match the source network %pV of the packet.",
2921 fr_box_ipaddr(radclient->ipaddr), fr_box_ipaddr(client->network));
2922 goto error;
2923 }
2924
2925 /*
2926 * Network prefix is more restrictive than the one given
2927 * by the client... that's bad.
2928 */
2929 if (client->network.prefix > radclient->ipaddr.prefix) {
2930 DEBUG("Client IP address %pV is not within the prefix with the defined network %pV",
2931 fr_box_ipaddr(radclient->ipaddr), fr_box_ipaddr(client->network));
2932 goto error;
2933 }
2934
2935 ipaddr = radclient->ipaddr;
2936 fr_ipaddr_mask(&ipaddr, client->network.prefix);
2937 if (fr_ipaddr_cmp(&ipaddr, &client->network) != 0) {
2938 DEBUG("Client IP address %pV is not within the defined network %pV.",
2939 fr_box_ipaddr(radclient->ipaddr), fr_box_ipaddr(client->network));
2940 goto error;
2941 }
2942
2943 /*
2944 * We can't define dynamic clients as networks (for now).
2945 *
2946 * @todo - If we did allow it, we would have to remove
2947 * this client from the trie, update it's IP address, and
2948 * re-add it. We can PROBABLY do this if this client
2949 * isn't already connected, AND radclient->use_connected
2950 * is true. But that's for later...
2951 */
2952 if (((radclient->ipaddr.af == AF_INET) &&
2953 (radclient->ipaddr.prefix != 32)) ||
2954 ((radclient->ipaddr.af == AF_INET6) &&
2955 (radclient->ipaddr.prefix != 128))) {
2956 ERROR("Cannot define a dynamic client as a network");
2957
2958 error:
2959 talloc_free(radclient);
2960
2961 /*
2962 * Remove the pending client from the trie.
2963 */
2964 fr_assert(!connection);
2965 talloc_free(client);
2966 return buffer_len;
2967 }
2968 }
2969
2970 update_client(client, radclient);
2971
2972 // @todo - fill in other fields?
2973
2974 talloc_free(radclient);
2975
2976 radclient = client->radclient; /* laziness */
2977 radclient->server_cs = inst->server_cs;
2978 radclient->server = cf_section_name2(inst->server_cs);
2979
2980 /*
2981 * This is a connected socket, and it's just been
2982 * allowed. Go poke the network side to read from the
2983 * socket.
2984 */
2985 if (connection) {
2986 fr_assert(connection != NULL);
2987 fr_assert(connection->client == client);
2988 fr_assert(client->connection != NULL);
2989
2990 /*
2991 * Promote the parent and every sibling connection.
2992 * This also promotes the current child.
2993 */
2994 fr_io_connection_allow(connection->parent, radclient);
2995
2996 INFO("proto_%s - Verification succeeded for packet from dynamic client %pV - processing queued packets",
2997 inst->app_io->common.name, fr_box_ipaddr(client->src_ipaddr));
2998 goto finish;
2999 } else {
3000 /*
3001 * Re-parent the conf section used to build this client
3002 * so its lifetime is linked to the client
3003 */
3004 talloc_steal(radclient, radclient->cs);
3005 }
3006
3007 fr_assert(connection == NULL);
3008 fr_assert(client->use_connected == false); /* we weren't sure until now */
3009
3010 /*
3011 * Disallow unsupported configurations.
3012 */
3013 if (radclient->use_connected && !inst->app_io->connection_set) {
3014 DEBUG("proto_%s - cannot use connected sockets as underlying 'transport = %s' does not support it.",
3015 inst->app_io->common.name, inst->submodule->module->exported->name);
3016 goto error;
3017 }
3018
3019
3020 /*
3021 * Dynamic clients can spawn new connections.
3022 */
3023 client->use_connected = radclient->use_connected;
3024
3025 /*
3026 * The admin has defined a client which uses connected
3027 * sockets. Go spawn it
3028 */
3029 if (client->use_connected) {
3030 fr_assert(connection == NULL);
3031
3032
3033 /*
3034 * Leave the state as PENDING. Each connection
3035 * will then cause a dynamic client to be
3036 * defined.
3037 */
3038 (void) pthread_mutex_init(&client->mutex, NULL);
3039 MEM(client->ht = fr_hash_table_alloc(client, connection_hash, connection_cmp, NULL));
3040
3041 } else {
3042 /*
3043 * The client has been allowed.
3044 */
3045 client->state = PR_CLIENT_DYNAMIC;
3046 client->radclient->active = true;
3047
3048 INFO("proto_%s - Verification succeeded for packet from dynamic client %pV - processing %u queued packets",
3049 inst->app_io->common.name, fr_box_ipaddr(client->src_ipaddr),
3050 fr_heap_num_elements(client->pending));
3051 }
3052
3053 /*
3054 * Add this client to the master socket, so that
3055 * mod_read() will see the pending client, pop the
3056 * pending packet, and process it.
3057 *
3058 */
3059 if (!thread->pending_clients) {
3061 fr_io_client_t, pending_id, 0));
3062 }
3063
3065 (void) fr_heap_insert(&thread->pending_clients, client);
3066
3067finish:
3068 /*
3069 * Maybe we defined the client, but the original packet
3070 * timed out, so there's nothing more to do. In that case, set up the expiry timers.
3071 */
3072 if (client->packets == 0) {
3073 client_expiry_timer(el->tl, fr_time_wrap(0), client);
3074 }
3075
3076 /*
3077 * If there are pending packets (and there should be at
3078 * least one), tell the network socket to call our read()
3079 * function again.
3080 */
3081 if (fr_heap_num_elements(client->pending) > 0) {
3082 if (connection) {
3083 fr_network_listen_read(connection->nr, connection->inst);
3084 } else {
3085 fr_network_listen_read(thread->nr, thread->inst);
3086 }
3087 }
3088
3089 return buffer_len;
3090}
3091
3092/** Close the socket.
3093 *
3094 */
3096{
3097 fr_io_instance_t const *inst;
3098 fr_io_connection_t *connection;
3099 fr_listen_instance_t *child;
3100
3101 get_inst(li, &inst, NULL, &connection, &child);
3102
3103 if (inst->app_io->close) {
3104 int ret;
3105
3106 ret = inst->app_io->close(child);
3107 if (ret < 0) return ret;
3108 } else {
3109 close(child->fd);
3110// child->fd = -1;
3111 }
3112
3113 if (!connection) return 0;
3114
3115 /*
3116 * We allocated this, so we're responsible for closing
3117 * it.
3118 */
3119 DEBUG("Closing connection %s", connection->name);
3120 if (connection->client->pending) {
3121 TALLOC_FREE(connection->client->pending); /* for any pending packets */
3122 }
3123
3124 /*
3125 * Remove connection from parent hash table
3126 */
3127 pthread_mutex_lock(&connection->parent->mutex);
3128 if (connection->in_parent_hash) {
3129 connection->in_parent_hash = false;
3130 (void) fr_hash_table_delete(connection->parent->ht, connection);
3131 }
3132
3133 /*
3134 * If this is a dynamic client, and the parent has no more connections
3135 * set up the timer to expire the dynamic client.
3136 */
3137 if ((connection->parent->state == PR_CLIENT_DYNAMIC) &&
3138 ((!connection->parent->ht) || (fr_hash_table_num_elements(connection->parent->ht) == 0))) {
3139 client_expiry_timer(connection->el->tl, fr_time_wrap(0), connection->parent);
3140 }
3141 pthread_mutex_unlock(&connection->parent->mutex);
3142
3143 /*
3144 * Don't free connection->mi here. Workers may still hold messages whose packet_ctx points into
3145 * connection->client. The destructor _connection_inst_free() frees connection->mi when the
3146 * network thread frees the listener, the last worker has finished with the socket.
3147 */
3148
3149 return 0;
3150}
3151
3152static int mod_instantiate(module_inst_ctx_t const *mctx)
3153{
3154 fr_io_instance_t *inst = mctx->mi->data;
3155 CONF_SECTION *conf = mctx->mi->conf;
3156
3157 inst->mi = mctx->mi;
3158 inst->app_io = (fr_app_io_t const *) inst->submodule->exported;
3159 inst->app_io_conf = inst->submodule->conf;
3160 inst->app_io_instance = inst->submodule->data;
3161
3162 /*
3163 * If we're not tracking duplicates then we don't need a
3164 * cleanup delay.
3165 *
3166 * If we are tracking duplicates, then we must have a non-zero cleanup delay.
3167 */
3168 if (!inst->app_io->track_duplicates) {
3169 inst->cleanup_delay = fr_time_delta_wrap(0);
3170
3171 } else {
3172 FR_TIME_DELTA_BOUND_CHECK("cleanup_delay", inst->cleanup_delay, >=, fr_time_delta_from_sec(1));
3173
3174 if (!inst->app_io->track_create) {
3175 cf_log_err(inst->app_io_conf, "Internal error: 'track_duplicates' is set, but there is no 'track create' function");
3176 return -1;
3177 }
3178 }
3179
3180 /*
3181 * Get various information after bootstrapping the
3182 * application IO module.
3183 */
3184 if (inst->app_io->network_get) {
3185 inst->app_io->network_get(&inst->ipproto, &inst->dynamic_clients, &inst->networks, inst->app_io_instance);
3186 }
3187
3188 if ((inst->ipproto == IPPROTO_TCP) && !inst->app_io->connection_set) {
3189 cf_log_err(inst->app_io_conf, "Missing 'connection set' API for proto_%s", inst->app_io->common.name);
3190 return -1;
3191 }
3192
3193 /*
3194 * Ensure that the dynamic client sections exist
3195 */
3196 if (inst->dynamic_clients) {
3198
3199 if (!cf_section_find(server, "new", "client")) {
3200 cf_log_err(conf, "Cannot use 'dynamic_clients = yes' as the virtual server has no 'new client { ... }' section defined.");
3201 return -1;
3202 }
3203
3204 if (!cf_section_find(server, "add", "client")) {
3205 cf_log_warn(conf, "No 'add client { ... }' section was defined.");
3206 }
3207
3208 if (!cf_section_find(server, "deny", "client")) {
3209 cf_log_warn(conf, "No 'deny client { ... }' section was defined.");
3210 }
3211 }
3212
3213 /*
3214 * Create a list of client modules.
3215 *
3216 * FIXME - Probably only want to do this for connected sockets?
3217 *
3218 * FIXME - We probably want write protect enabled?
3219 */
3220 inst->clients = module_list_alloc(inst, &module_list_type_thread_local, "clients", false);
3222
3223 return 0;
3224}
3225
3226
3227static char const *mod_name(fr_listen_instance_t *li)
3228{
3229 fr_io_thread_t *thread;
3230 fr_io_connection_t *connection;
3231 fr_listen_instance_t *child;
3232 fr_io_instance_t const *inst;
3233
3234 get_inst(li, &inst, &thread, &connection, &child);
3235
3236 fr_assert(child != NULL);
3237 return child->listen->app_io->get_name(child);
3238}
3239
3240/** Create a trie from arrays of allow / deny IP addresses
3241 *
3242 * @param ctx the talloc ctx
3243 * @param af the address family to allow
3244 * @param allow the array of IPs / networks to allow. MUST be talloc'd
3245 * @param deny the array of IPs / networks to deny. MAY be NULL, MUST be talloc'd
3246 * @return
3247 * - fr_trie_t on success
3248 * - NULL on error
3249 */
3250fr_trie_t *fr_master_io_network(TALLOC_CTX *ctx, int af, fr_ipaddr_t *allow, fr_ipaddr_t *deny)
3251{
3252 fr_trie_t *trie;
3253 size_t i, num;
3254
3255 MEM(trie = fr_trie_alloc(ctx, NULL, NULL));
3256
3257 num = talloc_array_length(allow);
3258 fr_assert(num > 0);
3259
3260 for (i = 0; i < num; i++) {
3261 fr_ipaddr_t *network;
3262
3263 /*
3264 * Can't add v4 networks to a v6 socket, or vice versa.
3265 */
3266 if (allow[i].af != af) {
3267 fr_strerror_printf("Address family in entry %zu - 'allow = %pV' "
3268 "does not match 'ipaddr'", i + 1, fr_box_ipaddr(allow[i]));
3269 talloc_free(trie);
3270 return NULL;
3271 }
3272
3273 /*
3274 * Duplicates are bad.
3275 */
3276 network = fr_trie_match_by_key(trie,
3277 &allow[i].addr, allow[i].prefix);
3278 if (network) {
3279 fr_strerror_printf("Cannot add duplicate entry 'allow = %pV'",
3280 fr_box_ipaddr(allow[i]));
3281 talloc_free(trie);
3282 return NULL;
3283 }
3284
3285 /*
3286 * Look for overlapping entries.
3287 * i.e. the networks MUST be disjoint.
3288 *
3289 * Note that this catches 192.168.1/24
3290 * followed by 192.168/16, but NOT the
3291 * other way around. The best fix is
3292 * likely to add a flag to
3293 * fr_trie_alloc() saying "we can only
3294 * have terminal fr_trie_user_t nodes"
3295 */
3296 network = fr_trie_lookup_by_key(trie,
3297 &allow[i].addr, allow[i].prefix);
3298 if (network && (network->prefix <= allow[i].prefix)) {
3299 fr_strerror_printf("Cannot add overlapping entry 'allow = %pV'", fr_box_ipaddr(allow[i]));
3300 fr_strerror_const("Entry is completely enclosed inside of a previously defined network.");
3301 talloc_free(trie);
3302 return NULL;
3303 }
3304
3305 /*
3306 * Insert the network into the trie.
3307 * Lookups will return the fr_ipaddr_t of
3308 * the network.
3309 */
3310 if (fr_trie_insert_by_key(trie,
3311 &allow[i].addr, allow[i].prefix,
3312 &allow[i]) < 0) {
3313 fr_strerror_printf("Failed adding 'allow = %pV' to tracking table", fr_box_ipaddr(allow[i]));
3314 talloc_free(trie);
3315 return NULL;
3316 }
3317 }
3318
3319 /*
3320 * And now check denied networks.
3321 */
3322 num = talloc_array_length(deny);
3323 if (!num) return trie;
3324
3325 /*
3326 * Since the default is to deny, you can only add
3327 * a "deny" inside of a previous "allow".
3328 */
3329 for (i = 0; i < num; i++) {
3330 fr_ipaddr_t *network;
3331
3332 /*
3333 * Can't add v4 networks to a v6 socket, or vice versa.
3334 */
3335 if (deny[i].af != af) {
3336 fr_strerror_printf("Address family in entry %zu - 'deny = %pV' "
3337 "does not match 'ipaddr'", i + 1, fr_box_ipaddr(deny[i]));
3338 talloc_free(trie);
3339 return NULL;
3340 }
3341
3342 /*
3343 * Duplicates are bad.
3344 */
3345 network = fr_trie_match_by_key(trie,
3346 &deny[i].addr, deny[i].prefix);
3347 if (network) {
3348 fr_strerror_printf("Cannot add duplicate entry 'deny = %pV'", fr_box_ipaddr(deny[i]));
3349 talloc_free(trie);
3350 return NULL;
3351 }
3352
3353 /*
3354 * A "deny" can only be within a previous "allow".
3355 */
3356 network = fr_trie_lookup_by_key(trie,
3357 &deny[i].addr, deny[i].prefix);
3358 if (!network) {
3359 fr_strerror_printf("The network in entry %zu - 'deny = %pV' is not "
3360 "contained within a previous 'allow'", i + 1, fr_box_ipaddr(deny[i]));
3361 talloc_free(trie);
3362 return NULL;
3363 }
3364
3365 /*
3366 * We hack the AF in "deny" rules. If
3367 * the lookup gets AF_UNSPEC, then we're
3368 * adding a "deny" inside of a "deny".
3369 */
3370 if (network->af != af) {
3371 fr_strerror_printf("The network in entry %zu - 'deny = %pV' is overlaps "
3372 "with another 'deny' rule", i + 1, fr_box_ipaddr(deny[i]));
3373 talloc_free(trie);
3374 return NULL;
3375 }
3376
3377 /*
3378 * Insert the network into the trie.
3379 * Lookups will return the fr_ipaddr_t of
3380 * the network.
3381 */
3382 if (fr_trie_insert_by_key(trie,
3383 &deny[i].addr, deny[i].prefix,
3384 &deny[i]) < 0) {
3385 fr_strerror_printf("Failed adding 'deny = %pV' to tracking table", fr_box_ipaddr(deny[i]));
3386 talloc_free(trie);
3387 return NULL;
3388 }
3389
3390 /*
3391 * Hack it to make it a deny rule.
3392 */
3393 deny[i].af = AF_UNSPEC;
3394 }
3395
3396 return trie;
3397}
3398
3399
3400
3402 size_t default_message_size, size_t num_messages)
3403{
3404 fr_listen_instance_t *li, *child;
3405 fr_listen_t *listen, *child_listen;
3406 fr_io_thread_t *thread;
3407
3408 /*
3409 * No IO paths, so we don't initialize them.
3410 */
3411 if (!inst->app_io) {
3412 fr_assert(!inst->dynamic_clients);
3413 return 0;
3414 }
3415
3416 if (!inst->app_io->common.thread_inst_size) {
3417 fr_strerror_const("IO modules MUST set 'thread_inst_size' when using the master IO handler.");
3418 return -1;
3419 }
3420
3421 /*
3422 * Build the #fr_listen_instance_t. This describes the complete
3423 * path data takes from the socket to the decoder and
3424 * back again.
3425 */
3426 /*
3427 * The first listener is the one for the application
3428 * (e.g. RADIUS). However, we mangle the IO path to
3429 * point to the master IO handler. That allows all of
3430 * the high-level work (dynamic client checking,
3431 * connected sockets, etc.) to be handled by the master
3432 * IO handler.
3433 *
3434 * This listener is then passed to the network code,
3435 * which calls our trampoline functions to do the actual
3436 * work.
3437 */
3438 MEM(listen = fr_listen_alloc(NULL, inst->app_io_conf, inst->server_cs, inst->app, inst->app_instance,
3440
3441 /*
3442 * Set configurable parameters for message ring buffer.
3443 */
3444 listen->default_message_size = default_message_size;
3445 listen->num_messages = num_messages;
3446 listen->track_duplicates = inst->app_io->track_duplicates;
3447
3448 /*
3449 * Per-socket data lives here.
3450 */
3451 thread = talloc_zero(NULL, fr_io_thread_t);
3452 thread->sc = sc;
3453
3454 /*
3455 * Create the trie of clients and NAK clients.
3456 */
3457 MEM(thread->trie = fr_trie_alloc(thread, NULL, NULL));
3458 MEM(thread->nak_trie = fr_trie_alloc(thread, NULL, NULL));
3459 fr_io_nak_list_init(&thread->nak_list);
3460
3461 if (inst->dynamic_clients) {
3463 fr_io_client_t, alive_id, 0));
3464 }
3465
3466 /*
3467 * The socket of the listener the network reads.
3468 */
3469 MEM(li = fr_listen_instance_alloc(NULL, listen));
3470 talloc_set_destructor(li, fr_io_listen_free);
3471 li->thread_instance = thread;
3472 thread->inst = li;
3473 if (inst->app_io->hexdump_set) inst->app_io->hexdump_set(li, inst->app_io_instance);
3474
3475 /*
3476 * The child listener points to the *actual* IO path.
3477 *
3478 * We need to create a complete listener here (e.g.
3479 * RADIUS + RADIUS_UDP), because the underlying IO
3480 * functions expect to get passed a full listener.
3481 *
3482 * Once the network side calls us, we will call the child
3483 * listener to do the actual IO.
3484 */
3485 MEM(child_listen = fr_listen_alloc(listen, listen->cs, listen->server_cs, listen->app, listen->app_instance,
3486 inst->app_io, inst->app_io_instance));
3487 child_listen->default_message_size = listen->default_message_size;
3488 child_listen->num_messages = listen->num_messages;
3489 child_listen->track_duplicates = inst->app_io->track_duplicates;
3490
3491 /*
3492 * The socket of it which does the actual IO.
3493 */
3494 MEM(child = thread->child_inst = fr_listen_instance_alloc(li, child_listen));
3495
3496 if (inst->app_io->common.thread_inst_size > 0) {
3497 child->thread_instance = talloc_zero_array(NULL, uint8_t,
3498 inst->app_io->common.thread_inst_size);
3499 talloc_set_destructor(child, fr_io_listen_free);
3500
3501 talloc_set_name(child->thread_instance, "proto_%s_thread_t",
3502 inst->app_io->common.name);
3503 } else {
3504 child->thread_instance = UNCONST(void *, inst->app_io_instance);
3505 }
3506
3507 /*
3508 * Don't call connection_set() for the main socket. It's
3509 * not connected. Instead, tell the IO path to open the
3510 * socket for us.
3511 */
3512 if (inst->app_io->open(child) < 0) {
3513 talloc_free(li);
3514 return -1;
3515 }
3516
3517 li->fd = child->fd; /* copy this back up */
3518
3519 if (!child->listen->app_io->get_name) {
3520 child->name = child->listen->app_io->common.name;
3521 } else {
3522 child->name = child->listen->app_io->get_name(child);
3523 }
3524 li->name = child->name;
3525
3526 /*
3527 * Record which socket we opened.
3528 */
3529 if (child->app_io_addr) {
3530 fr_listen_instance_t *other;
3531
3532 other = listen_find_any(thread->child_inst);
3533 if (other) {
3534 cf_log_err(other->listen->cs, "Already opened socket %s", other->name);
3535 cf_log_err(li->listen->cs, "Failed opening duplicate socket - cannot use the same "
3536 "configuration for two different listen sections");
3537
3538 talloc_free(li);
3539 return -1;
3540 }
3541
3542 (void) listen_record(child);
3543 }
3544
3545 /*
3546 * Add the socket to the scheduler, where it might end up
3547 * in a different thread.
3548 */
3549 if (!fr_schedule_listen_add(sc, li)) {
3550 talloc_free(li);
3551 return -1;
3552 }
3553
3554 return 0;
3555}
3556
3557/*
3558 * Used to create a tracking structure for fr_network_sendto_worker()
3559 */
3561 int src_port, fr_ipaddr_t const *dst_ipaddr, int dst_port)
3562{
3563 fr_io_instance_t const *inst;
3564 fr_io_thread_t *thread;
3565 fr_io_connection_t *connection;
3566 fr_listen_instance_t *child;
3567 fr_io_track_t *track;
3568 fr_io_client_t *client;
3569 fr_io_address_t *address;
3570 fr_listen_instance_t *parent = talloc_parent(li);
3571
3572 (void) talloc_get_type_abort(parent, fr_listen_instance_t);
3573
3574 get_inst(parent, &inst, &thread, &connection, &child);
3575
3576 fr_assert(child == li);
3577
3578 if (unlikely(!thread)) return NULL;
3579 fr_assert(thread->trie != NULL);
3580
3581 client = fr_trie_lookup_by_key(thread->trie, &src_ipaddr->addr, src_ipaddr->prefix);
3582 if (!client) {
3583 MEM(client = client_alloc(thread, PR_CLIENT_STATIC, inst, thread, radclient, NULL));
3584 }
3585
3586 MEM(track = talloc_zero_pooled_object(client->table, fr_io_track_t, 1, sizeof(*track) + sizeof(*track->address) + 64));
3587 MEM(track->address = address = talloc_zero(track, fr_io_address_t));
3588
3589 track->li = li;
3590 track->client = client;
3591
3592 address->socket.inet.src_port = src_port;
3593 address->socket.inet.dst_port = dst_port;
3594
3595 address->socket.inet.src_ipaddr = *src_ipaddr;
3596 address->socket.inet.dst_ipaddr = *dst_ipaddr;
3597 address->radclient = radclient;
3598
3599 return track;
3600}
3601
3602
3604 .common = {
3605 .magic = MODULE_MAGIC_INIT,
3606 .name = "radius_master_io",
3607
3609 },
3610 .default_message_size = 4096,
3611 .track_duplicates = true,
3612
3613 .read = mod_read,
3614 .write = mod_write,
3615 .inject = mod_inject,
3616
3617 .open = mod_open,
3618 .close = mod_close,
3619 .event_list_set = mod_event_list_set,
3620 .get_name = mod_name,
3621};
static int const char char buffer[256]
Definition acutest.h:641
log_entry msg
Definition acutest.h:947
fr_io_close_t close
Close the transport.
Definition app_io.h:60
module_t common
Common fields to all loadable modules.
Definition app_io.h:34
fr_io_track_create_t track_create
create a tracking structure
Definition app_io.h:64
bool track_duplicates
track duplicate packets
Definition app_io.h:41
fr_io_name_t get_name
get the socket name
Definition app_io.h:70
fr_io_track_cmp_t track_compare
compare two tracking structures
Definition app_io.h:65
Public structure describing an I/O path for a protocol.
Definition app_io.h:33
static int packet_ctx
the tests pass only the address of packet_ctx
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:203
#define CMP_PREFER_SMALLER(_a, _b)
Evaluates to +1 for a > b, and -1 for a < b.
Definition build.h:122
#define CMP_PREFER_LARGER(_a, _b)
Evaluates to -1 for a > b, and +1 for a < b.
Definition build.h:126
#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:130
#define unlikely(_x)
Definition build.h:492
#define UNUSED
Definition build.h:415
fr_cmp_ret_t
Result of an ordering comparison.
Definition build.h:113
#define FR_TIME_DELTA_BOUND_CHECK(_name, _var, _op, _bound)
Definition cf_parse.h:583
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:106
int cf_pair_replace_or_add(CONF_SECTION *cs, char const *ref, char const *value)
Definition cf_util.c:2612
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1405
char const * cf_section_name1(CONF_SECTION const *cs)
Return the first identifier of a CONF_SECTION.
Definition cf_util.c:1391
CONF_SECTION * cf_section_find(CONF_SECTION const *cs, char const *name1, char const *name2)
Find a CONF_SECTION with name1 and optionally name2.
Definition cf_util.c:1247
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition cf_util.c:671
CONF_SECTION * cf_section_dup(TALLOC_CTX *ctx, CONF_SECTION *parent, CONF_SECTION const *cs, char const *name1, char const *name2, bool copy_meta)
Duplicate a configuration section.
Definition cf_util.c:1052
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:340
#define cf_parent(_cf)
Definition cf_util.h:115
#define cf_log_warn(_cf, _fmt,...)
Definition cf_util.h:341
#define PRIORITY_NORMAL
Definition channel.h:155
char const * server
Name of the virtual server client is associated with.
Definition client.h:127
fr_ipaddr_t ipaddr
IPv4/IPv6 address of the host.
Definition client.h:83
char const * secret
Secret PSK.
Definition client.h:90
bool active
for dynamic clients
Definition client.h:114
fr_ipaddr_t src_ipaddr
IPv4/IPv6 address to send responses from (family must match ipaddr).
Definition client.h:84
char const * nas_type
Type of client (arbitrary).
Definition client.h:125
int proto
Protocol number.
Definition client.h:141
CONF_SECTION * cs
CONF_SECTION that was parsed to generate the client.
Definition client.h:132
bool dynamic
Whether the client was dynamically defined.
Definition client.h:113
char const * longname
Client identifier.
Definition client.h:87
fr_socket_limit_t limit
Connections per client (TCP clients only).
Definition client.h:142
char const * shortname
Client nickname.
Definition client.h:88
bool use_connected
do we use connected sockets for this client
Definition client.h:115
CONF_SECTION * server_cs
Virtual server that the client is associated with.
Definition client.h:128
Describes a host allowed to send packets to the server.
Definition client.h:80
#define MEM(x)
Definition debug.h:38
#define ERROR(fmt,...)
Definition dhcpclient.c:40
#define DEBUG(fmt,...)
Definition dhcpclient.c:38
Test enumeration values.
Definition dict_test.h:92
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition dl_module.h:63
#define FR_DLIST_TYPES(_name)
Define type specific wrapper structs for dlists.
Definition dlist.h:1150
#define FR_DLIST_FUNCS(_name, _element_type, _element_entry)
Define type specific wrapper functions for dlists.
Definition dlist.h:1173
#define FR_DLIST_TYPEDEFS(_name, _head, _entry)
Define friendly names for type specific dlist head and entry structures.
Definition dlist.h:1160
#define fr_event_filter_update(...)
Definition event.h:243
#define FR_EVENT_RESUME(_s, _f)
Re-add the filter for a func from kevent.
Definition event.h:135
#define FR_EVENT_SUSPEND(_s, _f)
Temporarily remove the filter for a func from kevent.
Definition event.h:119
@ FR_EVENT_FILTER_IO
Combined filter for read/write functions/.
Definition event.h:87
Callbacks for the FR_EVENT_FILTER_IO filter.
Definition event.h:192
Structure describing a modification to a filter's state.
Definition event.h:100
void * fr_hash_table_iter_next(fr_hash_table_t *ht, fr_hash_iter_t *iter)
Iterate over entries in a hash table.
Definition hash.c:668
int fr_hash_table_find(void **found, fr_hash_table_t *ht, void const *data)
Find data in a hash table.
Definition hash.c:458
void * fr_hash_table_iter_init(fr_hash_table_t *ht, fr_hash_iter_t *iter)
Initialise an iterator.
Definition hash.c:723
uint32_t fr_hash_update(void const *data, size_t size, uint32_t hash)
Definition hash.c:900
uint32_t fr_hash(void const *data, size_t size)
Definition hash.c:866
int fr_hash_table_delete(fr_hash_table_t *ht, void const *data)
Remove and free data (if a free function was specified)
Definition hash.c:635
uint32_t fr_hash_table_num_elements(fr_hash_table_t *ht)
Definition hash.c:652
int fr_hash_table_insert(fr_hash_table_t *ht, void const *data)
Insert data into a hash table.
Definition hash.c:501
#define fr_hash_table_alloc(_ctx, _hash_node, _cmp_node, _free_node)
Definition hash.h:61
Stores the state of the current iteration operation.
Definition hash.h:41
int fr_heap_insert(fr_heap_t **hp, void *data)
Insert a new element into the heap.
Definition heap.c:149
int fr_heap_pop(void **out, fr_heap_t **hp)
Remove a node from the heap.
Definition heap.c:359
int fr_heap_extract(fr_heap_t **hp, void *data)
Remove a node from the heap.
Definition heap.c:259
unsigned int fr_heap_index_t
Definition heap.h:82
static void * fr_heap_peek(fr_heap_t *h)
Return the item from the top of the heap but don't pop it.
Definition heap.h:138
#define fr_heap_alloc(_ctx, _cmp, _type, _field, _init)
Creates a heap that can be used with non-talloced elements.
Definition heap.h:102
static bool fr_heap_entry_inserted(fr_heap_index_t heap_idx)
Check if an entry is inserted into a heap.
Definition heap.h:126
static unsigned int fr_heap_num_elements(fr_heap_t *h)
Return the number of elements in the heap.
Definition heap.h:181
#define FR_HEAP_INDEX_INVALID
Definition heap.h:85
The main heap structure.
Definition heap.h:68
talloc_free(hp)
int fr_ipaddr_from_sockaddr(fr_ipaddr_t *ipaddr, uint16_t *port, struct sockaddr_storage const *sa, socklen_t salen)
Convert sockaddr to our internal ip address representation.
Definition inet.c:1448
int fr_ipaddr_to_sockaddr(struct sockaddr_storage *sa, socklen_t *salen, fr_ipaddr_t const *ipaddr, uint16_t port)
Convert our internal ip address representation to a sockaddr.
Definition inet.c:1399
void fr_ipaddr_mask(fr_ipaddr_t *addr, uint8_t prefix)
Zeroes out the host portion of an fr_ipaddr_t.
Definition inet.c:218
fr_cmp_ret_t fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b)
Compare two ip addresses.
Definition inet.c:1353
uint8_t prefix
Prefix length - Between 0-32 for IPv4 and 0-128 for IPv6.
Definition inet.h:69
int af
Address family.
Definition inet.h:64
union fr_ipaddr_t::@162 addr
IPv4/6 prefix.
fr_socket_t socket
src/dst ip and port.
Definition base.h:341
fr_client_t const * radclient
old-style client definition
Definition base.h:343
void fr_network_listen_read(fr_network_t *nr, fr_listen_instance_t *li)
Signal the network to read from a listener.
Definition network.c:286
void fr_network_listen_write(fr_network_t *nr, fr_listen_instance_t *li, uint8_t const *packet, size_t packet_len, void *packet_ctx, fr_time_t request_time)
Inject a packet for a listener to write.
Definition network.c:312
int fr_network_listen_inject(fr_network_t *nr, fr_listen_instance_t *li, uint8_t const *packet, size_t packet_len, fr_time_t recv_time)
Inject a packet for a listener to read.
Definition network.c:360
#define PERROR(_fmt,...)
Definition log.h:233
#define DEBUG3(_fmt,...)
Definition log.h:271
#define RATE_LIMIT_LOCAL(_entry, _log, _fmt,...)
Rate limit messages using a local limiting entry.
Definition log.h:586
Track when a log message was last repeated.
Definition log.h:564
#define fr_time()
Definition event.c:61
Stores all information relating to an event list.
Definition event.c:415
int fr_io_listen_free(fr_listen_instance_t *li)
Free a socket's thread instance along with it.
Definition listen.c:126
fr_listen_t * fr_listen_alloc(TALLOC_CTX *ctx, CONF_SECTION *listen_cs, CONF_SECTION *server_cs, fr_app_t const *app, void const *app_instance, fr_app_io_t const *app_io, void const *app_io_instance)
Allocate a listener, and give it its id.
Definition listen.c:78
fr_listen_instance_t * fr_listen_instance_alloc(TALLOC_CTX *ctx, fr_listen_t *listen)
Allocate a socket of a listener.
Definition listen.c:111
size_t num_messages
for the message ring buffer
Definition listen.h:54
char const * name
printable name for this socket - set by open
Definition listen.h:68
fr_listen_t * listen
the listener this is a socket of.
Definition listen.h:65
int fd
file descriptor for this socket - set by open
Definition listen.h:67
fr_app_io_t const * app_io
I/O path functions.
Definition listen.h:33
fr_app_t const * app
Definition listen.h:36
bool track_duplicates
do we track duplicate packets?
Definition listen.h:42
void const * app_instance
Definition listen.h:37
void const * app_io_instance
I/O path configuration context.
Definition listen.h:34
CONF_SECTION * cs
of this listener
Definition listen.h:39
fr_socket_t * app_io_addr
for tracking duplicate sockets
Definition listen.h:72
void * thread_instance
thread / socket context
Definition listen.h:70
CONF_SECTION * server_cs
CONF_SECTION of the server.
Definition listen.h:40
size_t default_message_size
copied from app_io, but may be changed
Definition listen.h:53
bool connected
is this for a connected socket?
Definition listen.h:74
fr_network_t * nr
network for this connection
Definition master.c:177
static void fr_io_connection_allow(fr_io_client_t *parent, fr_client_t *radclient)
Promote a pending dynamic-client parent and all of its child connections.
Definition master.c:2643
bool in_trie
is the client in the trie?
Definition master.c:136
fr_listen_instance_t * inst
master listener for this socket
Definition master.c:167
static fr_io_pending_packet_t * fr_io_pending_alloc(fr_io_connection_t *connection, fr_io_client_t *client, uint8_t const *buffer, size_t packet_len, fr_io_track_t *track, int priority)
Definition master.c:1422
fr_io_nak_list_entry_t entry
Definition master.c:40
fr_io_client_state_t
Client states.
Definition master.c:94
@ PR_CLIENT_DYNAMIC
dynamically defined client
Definition master.c:97
@ PR_CLIENT_CONNECTED
dynamically defined client in a connected socket
Definition master.c:98
@ PR_CLIENT_PENDING
dynamic client pending definition
Definition master.c:99
@ PR_CLIENT_INVALID
Definition master.c:95
@ PR_CLIENT_STATIC
static / global clients
Definition master.c:96
bool paused
event filter doesn't like resuming something that isn't paused
Definition master.c:174
bool in_parent_hash
for tracking thread issues
Definition master.c:175
static fr_client_t * radclient_clone(TALLOC_CTX *ctx, fr_client_t const *parent)
Definition master.c:410
static void fr_io_client_delete(fr_io_client_t *client)
Delete a client and any connection it belongs to.
Definition master.c:2271
static fr_cmp_ret_t address_cmp(void const *one, void const *two)
Definition master.c:278
int packets
number of packets using this connection
Definition master.c:165
struct fr_io_thread_t::@57 rate_limit
fr_trie_t * nak_trie
trie of NAK clients
Definition master.c:50
uint32_t num_pending_packets
number of pending packets
Definition master.c:61
#define DUP_FIELD(_x)
static int track_dedup_free(fr_io_track_t *track)
Definition master.c:201
fr_rb_tree_t * table
tracking table for packets
Definition master.c:141
fr_time_t recv_time
Definition master.c:84
fr_heap_t * pending_clients
heap of pending clients
Definition master.c:53
bool ready_to_delete
are we ready to delete this client?
Definition master.c:135
static fr_cmp_ret_t pending_packet_cmp(void const *one, void const *two)
Definition master.c:230
fr_ipaddr_t src_ipaddr
the address which was NAK'd
Definition master.c:38
static int _client_live_free(fr_io_client_t *client)
Definition master.c:1101
fr_heap_index_t pending_id
for pending clients
Definition master.c:131
static int pending_free(fr_io_pending_packet_t *pending)
Definition master.c:1398
bool use_connected
does this client allow connected sub-sockets?
Definition master.c:134
fr_io_client_t * client
our local client (pending or connected).
Definition master.c:169
static int track_free(fr_io_track_t *track)
Definition master.c:190
fr_app_io_t fr_master_app_io
Definition master.c:3603
static void packet_expiry_timer(fr_timer_list_t *tl, fr_time_t now, void *uctx)
Definition master.c:2469
fr_io_address_t * address
full information about the connection.
Definition master.c:166
static int mod_open(fr_listen_instance_t *li)
Open a new listener.
Definition master.c:2196
fr_timer_t * ev
when we clean up the client
Definition master.c:140
fr_network_t * nr
network for the master socket
Definition master.c:47
fr_trie_t * fr_master_io_network(TALLOC_CTX *ctx, int af, fr_ipaddr_t *allow, fr_ipaddr_t *deny)
Create a trie from arrays of allow / deny IP addresses.
Definition master.c:3250
static fr_io_pending_packet_t * pending_packet_pop(fr_io_thread_t *thread)
Definition master.c:372
static fr_cmp_ret_t connection_cmp(void const *one, void const *two)
Definition master.c:300
static void update_client(fr_io_client_t *client, fr_client_t *radclient)
Definition master.c:2534
fr_ipaddr_t network
network for dynamic clients
Definition master.c:127
static int mod_close(fr_listen_instance_t *li)
Close the socket.
Definition master.c:3095
static ssize_t mod_read(fr_listen_instance_t *li, void **packet_ctx, fr_time_t *recv_time_p, uint8_t *buffer, size_t buffer_len, size_t *leftover, bool *msg_used)
Implement 99% of the read routines.
Definition master.c:1479
pthread_mutex_t mutex
for parent / child signaling
Definition master.c:146
fr_heap_index_t heap_id
Definition master.c:82
static fr_io_client_t * client_alloc(TALLOC_CTX *ctx, fr_io_client_state_t state, fr_io_instance_t const *inst, fr_io_thread_t *thread, fr_client_t *radclient, fr_ipaddr_t const *network)
Allocate a dynamic client.
Definition master.c:1142
fr_io_instance_t const * inst
parent instance for master IO handler
Definition master.c:138
#define LOG_IGNORED_CLIENTS(_inst)
static fr_cmp_ret_t alive_client_cmp(void const *one, void const *two)
Definition master.c:1467
static int mod_inject(fr_listen_instance_t *li, uint8_t const *buffer, size_t buffer_len, fr_time_t recv_time)
Inject a packet to a connection.
Definition master.c:2138
#define COPY_FIELD(_x)
fr_listen_instance_t * child_inst
The child (app_io) IO path.
Definition master.c:57
static void fr_io_connection_deny(fr_io_client_t *parent)
Tear down any deferred sibling connections under a pending parent.
Definition master.c:2582
fr_trie_t * trie
trie of clients
Definition master.c:49
static void client_expiry_timer(fr_timer_list_t *tl, fr_time_t now, void *uctx)
Definition master.c:2293
fr_listen_instance_t * child_inst
child listener (app_io) for this socket
Definition master.c:168
static void get_inst(fr_listen_instance_t *li, fr_io_instance_t const **inst, fr_io_thread_t **thread, fr_io_connection_t **connection, fr_listen_instance_t **child)
Definition master.c:1053
static void mod_event_list_set(fr_listen_instance_t *li, fr_event_list_t *el, void *nr)
Set the event list for a new socket.
Definition master.c:2232
static bool fr_io_nak_find(fr_io_thread_t *thread, fr_ipaddr_t const *src_ipaddr)
Look up a negative cache entry.
Definition master.c:556
static fr_io_track_t * fr_io_track_add(fr_listen_instance_t const *li, fr_io_client_t *client, fr_io_address_t *address, uint8_t const *packet, size_t packet_len, fr_time_t recv_time, bool *is_dup)
Definition master.c:1248
static char const * mod_name(fr_listen_instance_t *li)
Definition master.c:3227
static fr_event_update_t pause_read[]
Definition master.c:180
static int _connection_inst_free(fr_listen_instance_t *li)
Connection destructor.
Definition master.c:648
fr_heap_t * alive_clients
heap of active dynamic clients
Definition master.c:54
fr_schedule_t * sc
the scheduler
Definition master.c:58
fr_io_nak_list_head_t nak_list
time ordered list of NAK entries.
Definition master.c:51
static fr_cmp_ret_t track_cmp(void const *one, void const *two)
Definition master.c:316
fr_hash_table_t * addresses
list of src/dst addresses used by this client
Definition master.c:144
int fr_master_io_listen(fr_io_instance_t *inst, fr_schedule_t *sc, size_t default_message_size, size_t num_messages)
Definition master.c:3401
static int connection_free(fr_io_connection_t *connection)
Definition master.c:628
fr_time_t expires
when this entry stops being used
Definition master.c:39
fr_listen_instance_t * inst
The master IO path.
Definition master.c:56
char const * name
taken from proto_FOO_TRANSPORT
Definition master.c:164
fr_heap_index_t alive_id
for all clients
Definition master.c:132
fr_event_list_t * el
event list for this connection
Definition master.c:176
static fr_cmp_ret_t track_connected_cmp(void const *one, void const *two)
Definition master.c:346
static ssize_t mod_write(fr_listen_instance_t *li, void *packet_ctx, fr_time_t request_time, uint8_t *buffer, size_t buffer_len, size_t written)
Definition master.c:2729
fr_io_client_state_t state
state of this client
Definition master.c:125
int packets
number of packets using this client
Definition master.c:130
static fr_io_connection_t * fr_io_connection_alloc(fr_io_instance_t const *inst, fr_io_thread_t *thread, fr_io_client_t *client, int fd, fr_io_address_t *address)
Create a new connection.
Definition master.c:661
bool dead
roundabout way to get the network side to close a socket
Definition master.c:173
fr_event_list_t * el
event list, for the master socket.
Definition master.c:46
uint64_t client_id
Unique client identifier.
Definition master.c:62
fr_io_thread_t * thread
Definition master.c:139
module_instance_t * mi
for submodule
Definition master.c:171
static fr_cmp_ret_t pending_client_cmp(void const *one, void const *two)
Definition master.c:260
static int _client_free(fr_io_client_t *client)
Definition master.c:596
static uint32_t connection_hash(void const *ctx)
Definition master.c:286
fr_io_connection_t * connection
parent connection
Definition master.c:124
fr_heap_t * pending
pending packets for this client
Definition master.c:143
static void fr_io_nak_expire(fr_io_thread_t *thread, fr_time_t now)
Expire entries in the cache.
Definition master.c:474
static int count_connections(UNUSED uint8_t const *key, UNUSED size_t keylen, void *data, void *ctx)
Count the number of connections used by active clients.
Definition master.c:570
fr_hash_table_t * ht
for tracking connected sockets
Definition master.c:147
static int mod_instantiate(module_inst_ctx_t const *mctx)
Definition master.c:3152
fr_io_track_t * track
Definition master.c:85
fr_client_t * radclient
old-style definition of this client
Definition master.c:128
fr_ipaddr_t src_ipaddr
packets come from this address
Definition master.c:126
static fr_event_update_t resume_read[]
Definition master.c:185
static void client_pending_free(fr_io_client_t *client)
Definition master.c:611
fr_io_track_t * fr_master_io_track_alloc(fr_listen_instance_t *li, fr_client_t *radclient, fr_ipaddr_t const *src_ipaddr, int src_port, fr_ipaddr_t const *dst_ipaddr, int dst_port)
Definition master.c:3560
static void fr_io_nak_insert(fr_io_thread_t *thread, fr_ipaddr_t const *src_ipaddr, fr_time_delta_t lifetime)
Add a negative cache entry.
Definition master.c:505
uint32_t num_connections
number of dynamic connections
Definition master.c:60
fr_io_client_t * parent
points to the parent client.
Definition master.c:170
static fr_client_t * radclient_alloc(TALLOC_CTX *ctx, int ipproto, fr_io_address_t *address)
Definition master.c:1073
Client definitions for master IO.
Definition master.c:123
Track a connection.
Definition master.c:163
A negative cache entry.
Definition master.c:37
A saved packet.
Definition master.c:81
fr_timer_t * ev
when we clean up this tracking entry
Definition master.h:43
uint8_t * reply
reply packet (if any)
Definition master.h:47
int packets
number of packets using this entry
Definition master.h:46
fr_time_t dynamic
timestamp for packet doing dynamic client definition
Definition master.h:54
fr_listen_instance_t const * li
listener associated with this tracking structure
Definition master.h:42
void * app_io_instance
Easy access to the app_io instance.
Definition master.h:106
fr_app_io_t const * app_io
Easy access to the app_io handle.
Definition master.h:105
fr_io_address_t const * address
of this packet.. shared between multiple packets
Definition master.h:55
bool do_not_respond
don't respond
Definition master.h:51
bool discard
whether or not we discard the packet
Definition master.h:50
fr_time_t timestamp
when this packet was received
Definition master.h:44
bool finished
are we finished the request?
Definition master.h:52
uint8_t * packet
really a tracking structure, not a packet
Definition master.h:57
size_t reply_len
length of reply, or 1 for "do not reply"
Definition master.h:48
fr_io_client_t * client
client handling this packet.
Definition master.h:56
fr_time_t expires
when this packet expires
Definition master.h:45
The master IO instance.
Definition master.h:73
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
int fr_nonblock(UNUSED int fd)
Definition misc.c:293
module_instance_t * mi
Instance of the module being instantiated.
Definition module_ctx.h:51
Temporary structure to hold arguments for instantiation calls.
Definition module_ctx.h:50
char * fr_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Special version of asprintf which implements custom format specifiers.
Definition print.c:883
#define fr_assert(_expr)
Definition rad_assert.h:37
static int ipproto
Definition radclient.c:91
static char * secret
Definition radclient.c:68
#define DEBUG2(fmt,...)
Definition radclient.h:43
#define INFO(fmt,...)
Definition radict.c:67
static bool cleanup
Definition radsniff.c:59
static rs_t * conf
Definition radsniff.c:52
int fr_rb_find(void **found, fr_rb_tree_t const *tree, void const *data)
Find an element in the tree, returning the data, not the node.
Definition rb.c:586
int fr_rb_delete(fr_rb_tree_t *tree, void const *data)
Remove node and free data (if a free function was specified)
Definition rb.c:767
int fr_rb_insert(fr_rb_tree_t *tree, void const *data)
Insert data into a tree.
Definition rb.c:637
#define fr_rb_inline_talloc_alloc(_ctx, _type, _field, _data_cmp, _data_free)
Allocs a red black that verifies elements are of a specific talloc type.
Definition rb.h:244
bool being_freed
Prevent double frees in talloc_destructor.
Definition rb.h:94
The main red black tree structure.
Definition rb.h:71
static unsigned int hash(char const *username, unsigned int tablesize)
Definition rlm_passwd.c:132
static char const * name
fr_network_t * fr_schedule_listen_add(fr_schedule_t *sc, fr_listen_instance_t *li)
Add a fr_listen_instance_t to a scheduler.
Definition schedule.c:791
The scheduler.
Definition schedule.c:75
@ MODULE_INSTANCE_BOOTSTRAPPED
Module instance has been bootstrapped, but not yet instantiated.
Definition module.h:271
CONF_SECTION * conf
Module's instance configuration.
Definition module.h:356
void * data
Module's instance data.
Definition module.h:298
module_instantiate_t instantiate
Callback to allow the module to register any per-instance resources like sockets and file handles.
Definition module.h:229
Module instance data.
Definition module.h:292
fr_time_delta_t idle_timeout
Definition socket.h:38
uint32_t max_connections
Definition socket.h:33
static const uchar sc[16]
Definition smbdes.c:115
fr_cmp_ret_t fr_socket_cmp(fr_socket_t const *a, fr_socket_t const *b)
Compare the addresses of two sockets.
Definition socket.c:1110
module_list_type_t const module_list_type_thread_local
Callbacks for a thread local list.
Definition module.c:605
void module_list_mask_set(module_list_t *ml, module_instance_state_t mask)
Set a new bootstrap/instantiate state for a list.
Definition module.c:1926
module_instance_t * module_instance_copy(module_list_t *dst, module_instance_t const *src, char const *inst_name)
Duplicate a module instance, placing it in a new module list.
Definition module.c:1625
module_list_t * module_list_alloc(TALLOC_CTX *ctx, module_list_type_t const *type, char const *name, bool write_protect)
Allocate a new module list.
Definition module.c:1948
int module_thread_instantiate(TALLOC_CTX *ctx, module_instance_t *mi, fr_event_list_t *el)
Allocate thread-local instance data for a module.
Definition module.c:1100
int module_instantiate(module_instance_t *instance)
Manually complete module setup by calling its instantiate function.
Definition module.c:1274
int module_instance_conf_parse(module_instance_t *mi, CONF_SECTION *conf)
Covert a CONF_SECTION into parsed module instance data.
Definition module.c:781
eap_aka_sim_process_conf_t * inst
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
#define talloc_get_type_abort_const
Definition talloc.h:117
#define talloc_zero_pooled_object(_ctx, _type, _num_subobjects, _total_subobjects_size)
Definition talloc.h:209
static int talloc_const_free(void const *ptr)
Free const'd memory.
Definition talloc.h:289
#define talloc_asprintf
Definition talloc.h:151
#define talloc_strdup(_ctx, _str)
Definition talloc.h:149
#define fr_time_delta_lt(_a, _b)
Definition time.h:285
static int64_t fr_time_unwrap(fr_time_t time)
Definition time.h:146
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition time.h:599
#define fr_time_delta_wrap(_time)
Definition time.h:152
#define fr_time_wrap(_time)
Definition time.h:145
#define fr_time_delta_ispos(_a)
Definition time.h:290
#define fr_time_eq(_a, _b)
Definition time.h:241
static double fr_time_delta_to_sec_double(fr_time_delta_t delta)
Seconds as a double, for printing and for arithmetic which need not be exact.
Definition time.h:663
#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_neq(_a, _b)
Definition time.h:242
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
"server local" time.
Definition time.h:69
An event timer list.
Definition timer.c:65
A timer event.
Definition timer.c:100
#define FR_TIMER_DISARM_RETURN(_ev)
Definition timer.h:98
#define FR_TIMER_DELETE(_ev_p)
Definition timer.h:105
#define FR_TIMER_DELETE_RETURN(_ev_p)
Definition timer.h:112
#define fr_timer_in(...)
Definition timer.h:87
#define FR_TIMER_DISARM(_ev)
Definition timer.h:91
static bool fr_timer_armed(fr_timer_t *ev)
Definition timer.h:124
#define fr_timer_at(...)
Definition timer.h:81
void * fr_trie_remove_by_key(fr_trie_t *ft, void const *key, size_t keylen)
Remove a key and return the associated user ctx.
Definition trie.c:2157
fr_trie_t * fr_trie_alloc(TALLOC_CTX *ctx, fr_trie_key_t get_key, fr_free_t free_data)
Allocate a trie.
Definition trie.c:741
int fr_trie_walk(fr_trie_t *ft, void *ctx, fr_trie_walk_t callback)
Definition trie.c:2610
void * fr_trie_lookup_by_key(fr_trie_t const *ft, void const *key, size_t keylen)
Lookup a key in a trie and return user ctx, if any.
Definition trie.c:1265
void * fr_trie_match_by_key(fr_trie_t const *ft, void const *key, size_t keylen)
Match a key and length in a trie and return user ctx, if any.
Definition trie.c:1289
int fr_trie_insert_by_key(fr_trie_t *ft, void const *key, size_t keylen, void const *data)
Insert a key and user ctx into a trie.
Definition trie.c:1878
static fr_event_list_t * el
static fr_slen_t parent
Definition pair.h:864
int af
AF_INET, AF_INET6, or AF_UNIX.
Definition socket.h:83
int fd
File descriptor if this is a live socket.
Definition socket.h:86
Holds information necessary for binding or connecting to a socket.
Definition socket.h:60
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const(_msg)
Definition strerror.h:223
static fr_slen_t fr_value_box_aprint(TALLOC_CTX *ctx, char **out, fr_value_box_t const *data, fr_sbuff_escape_rules_t const *e_rules) 1(fr_value_box_print
#define fr_box_ipaddr(_val)
Definition value.h:348
static fr_slen_t data
Definition value.h:1366
static fr_sbuff_err_t char size_t * len
Definition value.h:1061
#define fr_box_time_delta(_val)
Definition value.h:397
bool listen_record(fr_listen_instance_t *li)
Record that we're listening on a particular IP / port.
fr_listen_instance_t * listen_find_any(fr_listen_instance_t *li)
See if another global listener is using a particular IP / port.