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