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