1 /* 2 * util/module.c - module interface 3 * 4 * Copyright (c) 2007, 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 * \file 37 * Implementation of module.h. 38 */ 39 40 #include "config.h" 41 #include "util/module.h" 42 #include "sldns/wire2str.h" 43 #include "util/config_file.h" 44 #include "util/regional.h" 45 #include "util/data/dname.h" 46 #include "util/net_help.h" 47 48 const char* 49 strextstate(enum module_ext_state s) 50 { 51 switch(s) { 52 case module_state_initial: return "module_state_initial"; 53 case module_wait_reply: return "module_wait_reply"; 54 case module_wait_module: return "module_wait_module"; 55 case module_restart_next: return "module_restart_next"; 56 case module_wait_subquery: return "module_wait_subquery"; 57 case module_error: return "module_error"; 58 case module_finished: return "module_finished"; 59 } 60 return "bad_extstate_value"; 61 } 62 63 const char* 64 strmodulevent(enum module_ev e) 65 { 66 switch(e) { 67 case module_event_new: return "module_event_new"; 68 case module_event_pass: return "module_event_pass"; 69 case module_event_reply: return "module_event_reply"; 70 case module_event_noreply: return "module_event_noreply"; 71 case module_event_capsfail: return "module_event_capsfail"; 72 case module_event_moddone: return "module_event_moddone"; 73 case module_event_error: return "module_event_error"; 74 } 75 return "bad_event_value"; 76 } 77 78 void errinf(struct module_qstate* qstate, const char* str) 79 { 80 errinf_ede(qstate, str, LDNS_EDE_NONE); 81 } 82 83 void errinf_ede(struct module_qstate* qstate, 84 const char* str, sldns_ede_code reason_bogus) 85 { 86 struct errinf_strlist* p; 87 if(!str || (qstate->env->cfg->val_log_level < 2 && 88 !qstate->env->cfg->log_servfail)) { 89 return; 90 } 91 p = (struct errinf_strlist*)regional_alloc(qstate->region, sizeof(*p)); 92 if(!p) { 93 log_err("malloc failure in validator-error-info string"); 94 return; 95 } 96 p->next = NULL; 97 p->str = regional_strdup(qstate->region, str); 98 p->reason_bogus = reason_bogus; 99 if(!p->str) { 100 log_err("malloc failure in validator-error-info string"); 101 return; 102 } 103 /* add at end */ 104 if(qstate->errinf) { 105 struct errinf_strlist* q = qstate->errinf; 106 while(q->next) 107 q = q->next; 108 q->next = p; 109 } else qstate->errinf = p; 110 } 111 112 void errinf_origin(struct module_qstate* qstate, struct sock_list *origin) 113 { 114 struct sock_list* p; 115 if(qstate->env->cfg->val_log_level < 2 && !qstate->env->cfg->log_servfail) 116 return; 117 for(p=origin; p; p=p->next) { 118 char buf[256]; 119 if(p == origin) 120 snprintf(buf, sizeof(buf), "from "); 121 else snprintf(buf, sizeof(buf), "and "); 122 if(p->len == 0) 123 snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), 124 "cache"); 125 else 126 addr_to_str(&p->addr, p->len, buf+strlen(buf), 127 sizeof(buf)-strlen(buf)); 128 errinf(qstate, buf); 129 } 130 } 131 132 char* errinf_to_str_bogus(struct module_qstate* qstate) 133 { 134 char buf[20480]; 135 char* p = buf; 136 size_t left = sizeof(buf); 137 struct errinf_strlist* s; 138 char dname[LDNS_MAX_DOMAINLEN+1]; 139 char t[16], c[16]; 140 sldns_wire2str_type_buf(qstate->qinfo.qtype, t, sizeof(t)); 141 sldns_wire2str_class_buf(qstate->qinfo.qclass, c, sizeof(c)); 142 dname_str(qstate->qinfo.qname, dname); 143 snprintf(p, left, "validation failure <%s %s %s>:", dname, t, c); 144 left -= strlen(p); p += strlen(p); 145 if(!qstate->errinf) 146 snprintf(p, left, " misc failure"); 147 else for(s=qstate->errinf; s; s=s->next) { 148 snprintf(p, left, " %s", s->str); 149 left -= strlen(p); p += strlen(p); 150 } 151 p = strdup(buf); 152 if(!p) 153 log_err("malloc failure in errinf_to_str"); 154 return p; 155 } 156 157 /* Try to find the latest (most specific) dnssec failure */ 158 sldns_ede_code errinf_to_reason_bogus(struct module_qstate* qstate) 159 { 160 struct errinf_strlist* s; 161 sldns_ede_code ede = LDNS_EDE_NONE; 162 for(s=qstate->errinf; s; s=s->next) { 163 if(s->reason_bogus == LDNS_EDE_NONE) continue; 164 if(ede != LDNS_EDE_NONE 165 && ede != LDNS_EDE_DNSSEC_BOGUS 166 && s->reason_bogus == LDNS_EDE_DNSSEC_BOGUS) continue; 167 ede = s->reason_bogus; 168 } 169 return ede; 170 } 171 172 char* errinf_to_str_servfail(struct module_qstate* qstate) 173 { 174 char buf[20480]; 175 char* p = buf; 176 size_t left = sizeof(buf); 177 struct errinf_strlist* s; 178 char dname[LDNS_MAX_DOMAINLEN+1]; 179 char t[16], c[16]; 180 sldns_wire2str_type_buf(qstate->qinfo.qtype, t, sizeof(t)); 181 sldns_wire2str_class_buf(qstate->qinfo.qclass, c, sizeof(c)); 182 dname_str(qstate->qinfo.qname, dname); 183 snprintf(p, left, "SERVFAIL <%s %s %s>:", dname, t, c); 184 left -= strlen(p); p += strlen(p); 185 if(!qstate->errinf) 186 snprintf(p, left, " misc failure"); 187 else for(s=qstate->errinf; s; s=s->next) { 188 snprintf(p, left, " %s", s->str); 189 left -= strlen(p); p += strlen(p); 190 } 191 p = strdup(buf); 192 if(!p) 193 log_err("malloc failure in errinf_to_str"); 194 return p; 195 } 196 197 void errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr) 198 { 199 char buf[1024]; 200 char dname[LDNS_MAX_DOMAINLEN+1]; 201 char t[16], c[16]; 202 if((qstate->env->cfg->val_log_level < 2 && !qstate->env->cfg->log_servfail) || !rr) 203 return; 204 sldns_wire2str_type_buf(ntohs(rr->rk.type), t, sizeof(t)); 205 sldns_wire2str_class_buf(ntohs(rr->rk.rrset_class), c, sizeof(c)); 206 dname_str(rr->rk.dname, dname); 207 snprintf(buf, sizeof(buf), "for <%s %s %s>", dname, t, c); 208 errinf(qstate, buf); 209 } 210 211 void errinf_dname(struct module_qstate* qstate, const char* str, uint8_t* dname) 212 { 213 char b[1024]; 214 char buf[LDNS_MAX_DOMAINLEN+1]; 215 if((qstate->env->cfg->val_log_level < 2 && !qstate->env->cfg->log_servfail) || !str || !dname) 216 return; 217 dname_str(dname, buf); 218 snprintf(b, sizeof(b), "%s %s", str, buf); 219 errinf(qstate, b); 220 } 221 222 int 223 edns_known_options_init(struct module_env* env) 224 { 225 env->edns_known_options_num = 0; 226 env->edns_known_options = (struct edns_known_option*)calloc( 227 MAX_KNOWN_EDNS_OPTS, sizeof(struct edns_known_option)); 228 if(!env->edns_known_options) return 0; 229 return 1; 230 } 231 232 void 233 edns_known_options_delete(struct module_env* env) 234 { 235 free(env->edns_known_options); 236 env->edns_known_options = NULL; 237 env->edns_known_options_num = 0; 238 } 239 240 int 241 edns_register_option(uint16_t opt_code, int bypass_cache_stage, 242 int no_aggregation, struct module_env* env) 243 { 244 size_t i; 245 if(env->worker) { 246 log_err("invalid edns registration: " 247 "trying to register option after module init phase"); 248 return 0; 249 } 250 251 /** 252 * Checking if we are full first is faster but it does not provide 253 * the option to change the flags when the array is full. 254 * It only impacts unbound initialization, leave it for now. 255 */ 256 /* Check if the option is already registered. */ 257 for(i=0; i<env->edns_known_options_num; i++) 258 if(env->edns_known_options[i].opt_code == opt_code) 259 break; 260 /* If it is not yet registered check if we have space to add a new one. */ 261 if(i == env->edns_known_options_num) { 262 if(env->edns_known_options_num >= MAX_KNOWN_EDNS_OPTS) { 263 log_err("invalid edns registration: maximum options reached"); 264 return 0; 265 } 266 env->edns_known_options_num++; 267 } 268 env->edns_known_options[i].opt_code = opt_code; 269 env->edns_known_options[i].bypass_cache_stage = bypass_cache_stage; 270 env->edns_known_options[i].no_aggregation = no_aggregation; 271 return 1; 272 } 273 274 int 275 inplace_cb_register(void* cb, enum inplace_cb_list_type type, void* cbarg, 276 struct module_env* env, int id) 277 { 278 struct inplace_cb* callback; 279 struct inplace_cb** prevp; 280 if(env->worker) { 281 log_err("invalid edns callback registration: " 282 "trying to register callback after module init phase"); 283 return 0; 284 } 285 286 callback = (struct inplace_cb*)calloc(1, sizeof(*callback)); 287 if(callback == NULL) { 288 log_err("out of memory during edns callback registration."); 289 return 0; 290 } 291 callback->id = id; 292 callback->next = NULL; 293 callback->cb = cb; 294 callback->cb_arg = cbarg; 295 296 prevp = (struct inplace_cb**) &env->inplace_cb_lists[type]; 297 /* append at end of list */ 298 while(*prevp != NULL) 299 prevp = &((*prevp)->next); 300 *prevp = callback; 301 return 1; 302 } 303 304 void 305 inplace_cb_delete(struct module_env* env, enum inplace_cb_list_type type, 306 int id) 307 { 308 struct inplace_cb* temp = env->inplace_cb_lists[type]; 309 struct inplace_cb* prev = NULL; 310 311 while(temp) { 312 if(temp->id == id) { 313 if(!prev) { 314 env->inplace_cb_lists[type] = temp->next; 315 free(temp); 316 temp = env->inplace_cb_lists[type]; 317 } 318 else { 319 prev->next = temp->next; 320 free(temp); 321 temp = prev->next; 322 } 323 } 324 else { 325 prev = temp; 326 temp = temp->next; 327 } 328 } 329 } 330 331 struct edns_known_option* 332 edns_option_is_known(uint16_t opt_code, struct module_env* env) 333 { 334 size_t i; 335 for(i=0; i<env->edns_known_options_num; i++) 336 if(env->edns_known_options[i].opt_code == opt_code) 337 return env->edns_known_options + i; 338 return NULL; 339 } 340 341 int 342 edns_bypass_cache_stage(struct edns_option* list, struct module_env* env) 343 { 344 size_t i; 345 for(; list; list=list->next) 346 for(i=0; i<env->edns_known_options_num; i++) 347 if(env->edns_known_options[i].opt_code == list->opt_code && 348 env->edns_known_options[i].bypass_cache_stage == 1) 349 return 1; 350 return 0; 351 } 352 353 int 354 unique_mesh_state(struct edns_option* list, struct module_env* env) 355 { 356 size_t i; 357 if(env->unique_mesh) 358 return 1; 359 for(; list; list=list->next) 360 for(i=0; i<env->edns_known_options_num; i++) 361 if(env->edns_known_options[i].opt_code == list->opt_code && 362 env->edns_known_options[i].no_aggregation == 1) 363 return 1; 364 return 0; 365 } 366 367 void 368 log_edns_known_options(enum verbosity_value level, struct module_env* env) 369 { 370 size_t i; 371 char str[32], *s; 372 size_t slen; 373 if(env->edns_known_options_num > 0 && verbosity >= level) { 374 verbose(level, "EDNS known options:"); 375 verbose(level, " Code: Bypass_cache_stage: Aggregate_mesh:"); 376 for(i=0; i<env->edns_known_options_num; i++) { 377 s = str; 378 slen = sizeof(str); 379 (void)sldns_wire2str_edns_option_code_print(&s, &slen, 380 env->edns_known_options[i].opt_code); 381 verbose(level, " %-8.8s %-19s %-15s", str, 382 env->edns_known_options[i].bypass_cache_stage?"YES":"NO", 383 env->edns_known_options[i].no_aggregation?"NO":"YES"); 384 } 385 } 386 } 387 388 void 389 copy_state_to_super(struct module_qstate* qstate, int ATTR_UNUSED(id), 390 struct module_qstate* super) 391 { 392 /* Overwrite super's was_ratelimited only when it was not set */ 393 if(!super->was_ratelimited) { 394 super->was_ratelimited = qstate->was_ratelimited; 395 } 396 } 397