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