1 /*
2  * util/edns.c - handle base EDNS options.
3  *
4  * Copyright (c) 2018, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains functions for base EDNS options.
40  */
41 
42 #include "config.h"
43 #include "util/edns.h"
44 #include "util/config_file.h"
45 #include "util/netevent.h"
46 #include "util/net_help.h"
47 #include "util/regional.h"
48 #include "util/data/msgparse.h"
49 #include "util/data/msgreply.h"
50 
edns_strings_create(void)51 struct edns_strings* edns_strings_create(void)
52 {
53 	struct edns_strings* edns_strings = calloc(1,
54 		sizeof(struct edns_strings));
55 	if(!edns_strings)
56 		return NULL;
57 	if(!(edns_strings->region = regional_create())) {
58 		edns_strings_delete(edns_strings);
59 		return NULL;
60 	}
61 	return edns_strings;
62 }
63 
edns_strings_delete(struct edns_strings * edns_strings)64 void edns_strings_delete(struct edns_strings* edns_strings)
65 {
66 	if(!edns_strings)
67 		return;
68 	regional_destroy(edns_strings->region);
69 	free(edns_strings);
70 }
71 
72 static int
edns_strings_client_insert(struct edns_strings * edns_strings,struct sockaddr_storage * addr,socklen_t addrlen,int net,const char * string)73 edns_strings_client_insert(struct edns_strings* edns_strings,
74 	struct sockaddr_storage* addr, socklen_t addrlen, int net,
75 	const char* string)
76 {
77 	struct edns_string_addr* esa = regional_alloc_zero(edns_strings->region,
78 		sizeof(struct edns_string_addr));
79 	if(!esa)
80 		return 0;
81 	esa->string_len = strlen(string);
82 	esa->string = regional_alloc_init(edns_strings->region, string,
83 		esa->string_len);
84 	if(!esa->string)
85 		return 0;
86 	if(!addr_tree_insert(&edns_strings->client_strings, &esa->node, addr,
87 		addrlen, net)) {
88 		verbose(VERB_QUERY, "duplicate EDNS client string ignored.");
89 	}
90 	return 1;
91 }
92 
edns_strings_apply_cfg(struct edns_strings * edns_strings,struct config_file * config)93 int edns_strings_apply_cfg(struct edns_strings* edns_strings,
94 	struct config_file* config)
95 {
96 	struct config_str2list* c;
97 	regional_free_all(edns_strings->region);
98 	addr_tree_init(&edns_strings->client_strings);
99 
100 	for(c=config->edns_client_strings; c; c=c->next) {
101 		struct sockaddr_storage addr;
102 		socklen_t addrlen;
103 		int net;
104 		log_assert(c->str && c->str2);
105 
106 		if(!netblockstrtoaddr(c->str, UNBOUND_DNS_PORT, &addr, &addrlen,
107 			&net)) {
108 			log_err("cannot parse EDNS client string IP netblock: "
109 				"%s", c->str);
110 			return 0;
111 		}
112 		if(!edns_strings_client_insert(edns_strings, &addr, addrlen,
113 			net, c->str2)) {
114 			log_err("out of memory while adding EDNS strings");
115 			return 0;
116 		}
117 	}
118 	edns_strings->client_string_opcode = config->edns_client_string_opcode;
119 
120 	addr_tree_init_parents(&edns_strings->client_strings);
121 	return 1;
122 }
123 
124 struct edns_string_addr*
edns_string_addr_lookup(rbtree_type * tree,struct sockaddr_storage * addr,socklen_t addrlen)125 edns_string_addr_lookup(rbtree_type* tree, struct sockaddr_storage* addr,
126 	socklen_t addrlen)
127 {
128 	return (struct edns_string_addr*)addr_tree_lookup(tree, addr, addrlen);
129 }
130 
131