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 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 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 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 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* 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 static int edns_keepalive(struct edns_data* edns_out, struct edns_data* edns_in, 132 struct comm_point* c, struct regional* region) 133 { 134 if(c->type == comm_udp) 135 return 1; 136 137 /* To respond with a Keepalive option, the client connection 138 * must have received one message with a TCP Keepalive EDNS option, 139 * and that option must have 0 length data. Subsequent messages 140 * sent on that connection will have a TCP Keepalive option. 141 */ 142 if(c->tcp_keepalive || 143 edns_opt_list_find(edns_in->opt_list, LDNS_EDNS_KEEPALIVE)) { 144 int keepalive = c->tcp_timeout_msec / 100; 145 uint8_t data[2]; 146 data[0] = (uint8_t)((keepalive >> 8) & 0xff); 147 data[1] = (uint8_t)(keepalive & 0xff); 148 if(!edns_opt_list_append(&edns_out->opt_list, LDNS_EDNS_KEEPALIVE, 149 sizeof(data), data, region)) 150 return 0; 151 c->tcp_keepalive = 1; 152 } 153 return 1; 154 } 155 156 int apply_edns_options(struct edns_data* edns_out, struct edns_data* edns_in, 157 struct config_file* cfg, struct comm_point* c, struct regional* region) 158 { 159 if(cfg->do_tcp_keepalive && 160 !edns_keepalive(edns_out, edns_in, c, region)) 161 return 0; 162 163 if (cfg->nsid && edns_opt_list_find(edns_in->opt_list, LDNS_EDNS_NSID) 164 && !edns_opt_list_append(&edns_out->opt_list, 165 LDNS_EDNS_NSID, cfg->nsid_len, cfg->nsid, region)) 166 return 0; 167 168 if(!cfg->pad_responses || c->type != comm_tcp || !c->ssl 169 || !edns_opt_list_find(edns_in->opt_list, LDNS_EDNS_PADDING)) { 170 ; /* pass */ 171 } 172 173 else if(!edns_opt_list_append(&edns_out->opt_list, LDNS_EDNS_PADDING 174 , 0, NULL, region)) 175 return 0; 176 else 177 edns_out->padding_block_size = cfg->pad_responses_block_size; 178 179 return 1; 180 } 181