2007-09-12 01:46:28 +00:00
|
|
|
/*
|
2014-08-30 12:27:49 +10:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2007-09-12 01:46:28 +00:00
|
|
|
*
|
2007-09-12 23:46:47 +00:00
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
2021-06-03 08:37:05 +02:00
|
|
|
*
|
2007-09-12 23:46:47 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2007-09-12 01:46:28 +00:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
2007-09-12 23:46:47 +00:00
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
2008-01-18 23:46:58 +00:00
|
|
|
* information regarding copyright ownership.
|
2007-09-12 01:46:28 +00:00
|
|
|
*/
|
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
2018-03-28 14:19:37 +02:00
|
|
|
|
2007-09-12 01:46:28 +00:00
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/radix.h>
|
2017-10-19 12:26:32 +11:00
|
|
|
#include <isc/util.h>
|
2007-09-12 01:46:28 +00:00
|
|
|
|
|
|
|
#include <dns/acl.h>
|
|
|
|
|
|
|
|
static void
|
|
|
|
destroy_iptable(dns_iptable_t *dtab);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new IP table and the underlying radix structure
|
|
|
|
*/
|
|
|
|
isc_result_t
|
|
|
|
dns_iptable_create(isc_mem_t *mctx, dns_iptable_t **target) {
|
|
|
|
isc_result_t result;
|
|
|
|
dns_iptable_t *tab;
|
|
|
|
|
|
|
|
tab = isc_mem_get(mctx, sizeof(*tab));
|
2013-02-20 21:39:05 -08:00
|
|
|
tab->mctx = NULL;
|
|
|
|
isc_mem_attach(mctx, &tab->mctx);
|
2007-09-12 01:46:28 +00:00
|
|
|
isc_refcount_init(&tab->refcount, 1);
|
2008-12-01 00:04:21 +00:00
|
|
|
tab->radix = NULL;
|
2007-09-12 01:46:28 +00:00
|
|
|
tab->magic = DNS_IPTABLE_MAGIC;
|
2008-01-18 23:46:58 +00:00
|
|
|
|
2007-09-12 01:46:28 +00:00
|
|
|
result = isc_radix_create(mctx, &tab->radix, RADIX_MAXBITS);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto cleanup;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2007-09-12 01:46:28 +00:00
|
|
|
|
|
|
|
*target = tab;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
dns_iptable_detach(&tab);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool dns_iptable_neg = false;
|
|
|
|
static bool dns_iptable_pos = true;
|
2007-09-12 01:46:28 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Add an IP prefix to an existing IP table
|
|
|
|
*/
|
|
|
|
isc_result_t
|
2016-12-30 15:45:08 +11:00
|
|
|
dns_iptable_addprefix(dns_iptable_t *tab, const isc_netaddr_t *addr,
|
2018-04-17 08:29:14 -07:00
|
|
|
uint16_t bitlen, bool pos) {
|
2007-09-12 01:46:28 +00:00
|
|
|
isc_result_t result;
|
|
|
|
isc_prefix_t pfx;
|
2008-08-27 04:44:18 +00:00
|
|
|
isc_radix_node_t *node = NULL;
|
2014-08-28 22:05:57 -07:00
|
|
|
int i;
|
2007-09-12 01:46:28 +00:00
|
|
|
|
|
|
|
INSIST(DNS_IPTABLE_VALID(tab));
|
2018-06-04 13:41:09 +02:00
|
|
|
INSIST(tab->radix != NULL);
|
2007-09-12 01:46:28 +00:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
NETADDR_TO_PREFIX_T(addr, pfx, bitlen);
|
2007-09-12 01:46:28 +00:00
|
|
|
|
|
|
|
result = isc_radix_insert(tab->radix, &node, NULL, &pfx);
|
2008-09-10 21:52:49 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
isc_refcount_destroy(&pfx.refcount);
|
2007-09-12 01:46:28 +00:00
|
|
|
return (result);
|
2008-09-10 21:52:49 +00:00
|
|
|
}
|
2007-09-12 01:46:28 +00:00
|
|
|
|
2008-09-26 21:12:02 +00:00
|
|
|
/* If a node already contains data, don't overwrite it */
|
2014-08-28 22:05:57 -07:00
|
|
|
if (pfx.family == AF_UNSPEC) {
|
2008-09-26 21:12:02 +00:00
|
|
|
/* "any" or "none" */
|
|
|
|
INSIST(pfx.bitlen == 0);
|
2018-05-25 13:25:54 -07:00
|
|
|
for (i = 0; i < RADIX_FAMILIES; i++) {
|
2014-08-28 22:05:57 -07:00
|
|
|
if (node->data[i] == NULL) {
|
|
|
|
node->data[i] = pos ? &dns_iptable_pos
|
|
|
|
: &dns_iptable_neg;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2008-09-26 21:12:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* any other prefix */
|
2018-05-25 13:25:54 -07:00
|
|
|
int fam = ISC_RADIX_FAMILY(&pfx);
|
|
|
|
if (node->data[fam] == NULL) {
|
|
|
|
node->data[fam] = pos ? &dns_iptable_pos
|
2014-08-28 22:05:57 -07:00
|
|
|
: &dns_iptable_neg;
|
2008-09-26 21:12:02 +00:00
|
|
|
}
|
2008-01-18 23:46:58 +00:00
|
|
|
}
|
2007-09-12 01:46:28 +00:00
|
|
|
|
2008-09-10 21:52:49 +00:00
|
|
|
isc_refcount_destroy(&pfx.refcount);
|
2007-09-12 01:46:28 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Merge one IP table into another one.
|
|
|
|
*/
|
2007-09-14 01:46:06 +00:00
|
|
|
isc_result_t
|
2018-04-17 08:29:14 -07:00
|
|
|
dns_iptable_merge(dns_iptable_t *tab, dns_iptable_t *source, bool pos) {
|
2007-09-14 01:46:06 +00:00
|
|
|
isc_result_t result;
|
2007-09-12 01:46:28 +00:00
|
|
|
isc_radix_node_t *node, *new_node;
|
2014-08-28 22:05:57 -07:00
|
|
|
int i, max_node = 0;
|
2007-09-12 01:46:28 +00:00
|
|
|
|
|
|
|
RADIX_WALK(source->radix->head, node) {
|
2008-08-27 04:44:18 +00:00
|
|
|
new_node = NULL;
|
2007-09-14 01:46:06 +00:00
|
|
|
result = isc_radix_insert(tab->radix, &new_node, node, NULL);
|
|
|
|
|
2008-01-18 23:46:58 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
return (result);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2008-01-18 23:46:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're negating a nested ACL, then we should
|
|
|
|
* reverse the sense of every node. However, this
|
|
|
|
* could lead to a negative node in a nested ACL
|
|
|
|
* becoming a positive match in the parent, which
|
|
|
|
* could be a security risk. To prevent this, we
|
|
|
|
* just leave the negative nodes negative.
|
|
|
|
*/
|
2018-05-25 13:25:54 -07:00
|
|
|
for (i = 0; i < RADIX_FAMILIES; i++) {
|
2014-08-28 22:05:57 -07:00
|
|
|
if (!pos) {
|
2018-04-17 08:29:14 -07:00
|
|
|
if (node->data[i] && *(bool *)node->data[i]) {
|
2014-08-28 22:05:57 -07:00
|
|
|
new_node->data[i] = &dns_iptable_neg;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-08-28 22:05:57 -07:00
|
|
|
}
|
|
|
|
if (node->node_num[i] > max_node) {
|
|
|
|
max_node = node->node_num[i];
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2008-01-21 20:38:54 +00:00
|
|
|
}
|
2007-09-12 01:46:28 +00:00
|
|
|
}
|
|
|
|
RADIX_WALK_END;
|
|
|
|
|
2008-01-18 23:46:58 +00:00
|
|
|
tab->radix->num_added_node += max_node;
|
|
|
|
return (ISC_R_SUCCESS);
|
2007-09-12 01:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dns_iptable_attach(dns_iptable_t *source, dns_iptable_t **target) {
|
|
|
|
REQUIRE(DNS_IPTABLE_VALID(source));
|
2018-08-17 15:16:59 +02:00
|
|
|
isc_refcount_increment(&source->refcount);
|
2007-09-12 01:46:28 +00:00
|
|
|
*target = source;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dns_iptable_detach(dns_iptable_t **tabp) {
|
2018-08-28 10:18:59 +02:00
|
|
|
REQUIRE(tabp != NULL && DNS_IPTABLE_VALID(*tabp));
|
2007-09-12 01:46:28 +00:00
|
|
|
dns_iptable_t *tab = *tabp;
|
2018-08-28 10:18:59 +02:00
|
|
|
*tabp = NULL;
|
2018-08-17 15:16:59 +02:00
|
|
|
|
|
|
|
if (isc_refcount_decrement(&tab->refcount) == 1) {
|
2018-08-28 10:18:59 +02:00
|
|
|
isc_refcount_destroy(&tab->refcount);
|
2007-09-12 01:46:28 +00:00
|
|
|
destroy_iptable(tab);
|
2018-08-17 15:16:59 +02:00
|
|
|
}
|
2007-09-12 01:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
destroy_iptable(dns_iptable_t *dtab) {
|
|
|
|
REQUIRE(DNS_IPTABLE_VALID(dtab));
|
|
|
|
|
|
|
|
if (dtab->radix != NULL) {
|
2007-09-28 00:11:32 +00:00
|
|
|
isc_radix_destroy(dtab->radix, NULL);
|
2007-09-12 01:46:28 +00:00
|
|
|
dtab->radix = NULL;
|
|
|
|
}
|
2008-01-18 23:46:58 +00:00
|
|
|
|
2007-09-12 01:46:28 +00:00
|
|
|
dtab->magic = 0;
|
2013-02-20 21:39:05 -08:00
|
|
|
isc_mem_putanddetach(&dtab->mctx, dtab, sizeof(*dtab));
|
2007-09-12 01:46:28 +00:00
|
|
|
}
|