/src/bind9/lib/dns/iptable.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
3 | | * |
4 | | * SPDX-License-Identifier: MPL-2.0 |
5 | | * |
6 | | * This Source Code Form is subject to the terms of the Mozilla Public |
7 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
8 | | * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
9 | | * |
10 | | * See the COPYRIGHT file distributed with this work for additional |
11 | | * information regarding copyright ownership. |
12 | | */ |
13 | | |
14 | | #include <inttypes.h> |
15 | | #include <stdbool.h> |
16 | | |
17 | | #include <isc/mem.h> |
18 | | #include <isc/radix.h> |
19 | | #include <isc/util.h> |
20 | | |
21 | | #include <dns/acl.h> |
22 | | |
23 | | /* |
24 | | * Create a new IP table and the underlying radix structure |
25 | | */ |
26 | | void |
27 | 0 | dns_iptable_create(isc_mem_t *mctx, dns_iptable_t **target) { |
28 | 0 | dns_iptable_t *tab = isc_mem_get(mctx, sizeof(*tab)); |
29 | 0 | *tab = (dns_iptable_t){ |
30 | 0 | .references = ISC_REFCOUNT_INITIALIZER(1), |
31 | 0 | .magic = DNS_IPTABLE_MAGIC, |
32 | 0 | }; |
33 | 0 | isc_mem_attach(mctx, &tab->mctx); |
34 | |
|
35 | 0 | isc_radix_create(mctx, &tab->radix, RADIX_MAXBITS); |
36 | |
|
37 | 0 | *target = tab; |
38 | 0 | } |
39 | | |
40 | | static bool dns_iptable_neg = false; |
41 | | static bool dns_iptable_pos = true; |
42 | | |
43 | | /* |
44 | | * Add an IP prefix to an existing IP table |
45 | | */ |
46 | | isc_result_t |
47 | | dns_iptable_addprefix(dns_iptable_t *tab, const isc_netaddr_t *addr, |
48 | 0 | uint16_t bitlen, bool pos) { |
49 | 0 | isc_result_t result; |
50 | 0 | isc_prefix_t pfx; |
51 | 0 | isc_radix_node_t *node = NULL; |
52 | 0 | int i; |
53 | |
|
54 | 0 | INSIST(DNS_IPTABLE_VALID(tab)); |
55 | 0 | INSIST(tab->radix != NULL); |
56 | |
|
57 | 0 | NETADDR_TO_PREFIX_T(addr, pfx, bitlen); |
58 | |
|
59 | 0 | result = isc_radix_insert(tab->radix, &node, NULL, &pfx); |
60 | 0 | if (result != ISC_R_SUCCESS) { |
61 | 0 | isc_refcount_destroy(&pfx.refcount); |
62 | 0 | return result; |
63 | 0 | } |
64 | | |
65 | | /* If a node already contains data, don't overwrite it */ |
66 | 0 | if (pfx.family == AF_UNSPEC) { |
67 | | /* "any" or "none" */ |
68 | 0 | INSIST(pfx.bitlen == 0); |
69 | 0 | for (i = 0; i < RADIX_FAMILIES; i++) { |
70 | 0 | if (node->data[i] == NULL) { |
71 | 0 | node->data[i] = pos ? &dns_iptable_pos |
72 | 0 | : &dns_iptable_neg; |
73 | 0 | } |
74 | 0 | } |
75 | 0 | } else { |
76 | | /* any other prefix */ |
77 | 0 | int fam = ISC_RADIX_FAMILY(&pfx); |
78 | 0 | if (node->data[fam] == NULL) { |
79 | 0 | node->data[fam] = pos ? &dns_iptable_pos |
80 | 0 | : &dns_iptable_neg; |
81 | 0 | } |
82 | 0 | } |
83 | |
|
84 | 0 | isc_refcount_destroy(&pfx.refcount); |
85 | 0 | return ISC_R_SUCCESS; |
86 | 0 | } |
87 | | |
88 | | /* |
89 | | * Merge one IP table into another one. |
90 | | */ |
91 | | isc_result_t |
92 | 0 | dns_iptable_merge(dns_iptable_t *tab, dns_iptable_t *source, bool pos) { |
93 | 0 | isc_result_t result; |
94 | 0 | isc_radix_node_t *node, *new_node; |
95 | 0 | int i, max_node = 0; |
96 | |
|
97 | 0 | RADIX_WALK(source->radix->head, node) { |
98 | 0 | new_node = NULL; |
99 | 0 | result = isc_radix_insert(tab->radix, &new_node, node, NULL); |
100 | |
|
101 | 0 | if (result != ISC_R_SUCCESS) { |
102 | 0 | return result; |
103 | 0 | } |
104 | | |
105 | | /* |
106 | | * If we're negating a nested ACL, then we should |
107 | | * reverse the sense of every node. However, this |
108 | | * could lead to a negative node in a nested ACL |
109 | | * becoming a positive match in the parent, which |
110 | | * could be a security risk. To prevent this, we |
111 | | * just leave the negative nodes negative. |
112 | | */ |
113 | 0 | for (i = 0; i < RADIX_FAMILIES; i++) { |
114 | 0 | if (!pos) { |
115 | 0 | if (node->data[i] && *(bool *)node->data[i]) { |
116 | 0 | new_node->data[i] = &dns_iptable_neg; |
117 | 0 | } |
118 | 0 | } |
119 | 0 | if (node->node_num[i] > max_node) { |
120 | 0 | max_node = node->node_num[i]; |
121 | 0 | } |
122 | 0 | } |
123 | 0 | } |
124 | 0 | RADIX_WALK_END; |
125 | | |
126 | 0 | tab->radix->num_added_node += max_node; |
127 | 0 | return ISC_R_SUCCESS; |
128 | 0 | } |
129 | | |
130 | | static void |
131 | 0 | dns__iptable_destroy(dns_iptable_t *dtab) { |
132 | 0 | REQUIRE(DNS_IPTABLE_VALID(dtab)); |
133 | |
|
134 | 0 | dtab->magic = 0; |
135 | |
|
136 | 0 | if (dtab->radix != NULL) { |
137 | 0 | isc_radix_destroy(dtab->radix, NULL); |
138 | 0 | dtab->radix = NULL; |
139 | 0 | } |
140 | |
|
141 | 0 | isc_mem_putanddetach(&dtab->mctx, dtab, sizeof(*dtab)); |
142 | 0 | } |
143 | | |
144 | | #if DNS_IPTABLE_TRACE |
145 | | ISC_REFCOUNT_TRACE_IMPL(dns_iptable, dns__iptable_destroy); |
146 | | #else |
147 | | ISC_REFCOUNT_IMPL(dns_iptable, dns__iptable_destroy); Unexecuted instantiation: dns_iptable_ref Unexecuted instantiation: dns_iptable_unref Unexecuted instantiation: dns_iptable_detach |
148 | | #endif |