ipsecmod.c revision 1.1.1.2.2.1 1 1.1 christos /*
2 1.1 christos * ipsecmod/ipsecmod.c - facilitate opportunistic IPsec module
3 1.1 christos *
4 1.1 christos * Copyright (c) 2017, NLnet Labs. All rights reserved.
5 1.1 christos *
6 1.1 christos * This software is open source.
7 1.1 christos *
8 1.1 christos * Redistribution and use in source and binary forms, with or without
9 1.1 christos * modification, are permitted provided that the following conditions
10 1.1 christos * are met:
11 1.1 christos *
12 1.1 christos * Redistributions of source code must retain the above copyright notice,
13 1.1 christos * this list of conditions and the following disclaimer.
14 1.1 christos *
15 1.1 christos * Redistributions in binary form must reproduce the above copyright notice,
16 1.1 christos * this list of conditions and the following disclaimer in the documentation
17 1.1 christos * and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * Neither the name of the NLNET LABS nor the names of its contributors may
20 1.1 christos * be used to endorse or promote products derived from this software without
21 1.1 christos * specific prior written permission.
22 1.1 christos *
23 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 1.1 christos * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 1.1 christos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 1.1 christos * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 1.1 christos * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 1.1 christos * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 1.1 christos * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 1.1 christos * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.1 christos */
35 1.1 christos
36 1.1 christos /**
37 1.1 christos * \file
38 1.1 christos *
39 1.1 christos * This file contains a module that facilitates opportunistic IPsec. It does so
40 1.1 christos * by also quering for the IPSECKEY for A/AAAA queries and calling a
41 1.1 christos * configurable hook (eg. signaling an IKE daemon) before replying.
42 1.1 christos */
43 1.1 christos
44 1.1 christos #include "config.h"
45 1.1 christos #ifdef USE_IPSECMOD
46 1.1 christos #include "ipsecmod/ipsecmod.h"
47 1.1 christos #include "ipsecmod/ipsecmod-whitelist.h"
48 1.1 christos #include "util/fptr_wlist.h"
49 1.1 christos #include "util/regional.h"
50 1.1 christos #include "util/net_help.h"
51 1.1 christos #include "util/config_file.h"
52 1.1 christos #include "services/cache/dns.h"
53 1.1 christos #include "sldns/wire2str.h"
54 1.1 christos
55 1.1 christos /** Apply configuration to ipsecmod module 'global' state. */
56 1.1 christos static int
57 1.1 christos ipsecmod_apply_cfg(struct ipsecmod_env* ipsecmod_env, struct config_file* cfg)
58 1.1 christos {
59 1.1 christos if(!cfg->ipsecmod_hook || (cfg->ipsecmod_hook && !cfg->ipsecmod_hook[0])) {
60 1.1 christos log_err("ipsecmod: missing ipsecmod-hook.");
61 1.1 christos return 0;
62 1.1 christos }
63 1.1 christos if(cfg->ipsecmod_whitelist &&
64 1.1 christos !ipsecmod_whitelist_apply_cfg(ipsecmod_env, cfg))
65 1.1 christos return 0;
66 1.1 christos return 1;
67 1.1 christos }
68 1.1 christos
69 1.1 christos int
70 1.1 christos ipsecmod_init(struct module_env* env, int id)
71 1.1 christos {
72 1.1 christos struct ipsecmod_env* ipsecmod_env = (struct ipsecmod_env*)calloc(1,
73 1.1 christos sizeof(struct ipsecmod_env));
74 1.1 christos if(!ipsecmod_env) {
75 1.1 christos log_err("malloc failure");
76 1.1 christos return 0;
77 1.1 christos }
78 1.1 christos env->modinfo[id] = (void*)ipsecmod_env;
79 1.1 christos ipsecmod_env->whitelist = NULL;
80 1.1 christos if(!ipsecmod_apply_cfg(ipsecmod_env, env->cfg)) {
81 1.1 christos log_err("ipsecmod: could not apply configuration settings.");
82 1.1 christos return 0;
83 1.1 christos }
84 1.1 christos return 1;
85 1.1 christos }
86 1.1 christos
87 1.1 christos void
88 1.1 christos ipsecmod_deinit(struct module_env* env, int id)
89 1.1 christos {
90 1.1 christos struct ipsecmod_env* ipsecmod_env;
91 1.1 christos if(!env || !env->modinfo[id])
92 1.1 christos return;
93 1.1 christos ipsecmod_env = (struct ipsecmod_env*)env->modinfo[id];
94 1.1 christos /* Free contents. */
95 1.1 christos ipsecmod_whitelist_delete(ipsecmod_env->whitelist);
96 1.1 christos free(ipsecmod_env);
97 1.1 christos env->modinfo[id] = NULL;
98 1.1 christos }
99 1.1 christos
100 1.1 christos /** New query for ipsecmod. */
101 1.1 christos static int
102 1.1 christos ipsecmod_new(struct module_qstate* qstate, int id)
103 1.1 christos {
104 1.1 christos struct ipsecmod_qstate* iq = (struct ipsecmod_qstate*)regional_alloc(
105 1.1 christos qstate->region, sizeof(struct ipsecmod_qstate));
106 1.1 christos qstate->minfo[id] = iq;
107 1.1 christos if(!iq)
108 1.1 christos return 0;
109 1.1 christos /* Initialise it. */
110 1.1.1.2.2.1 martin memset(iq, 0, sizeof(*iq));
111 1.1 christos iq->enabled = qstate->env->cfg->ipsecmod_enabled;
112 1.1 christos iq->is_whitelisted = ipsecmod_domain_is_whitelisted(
113 1.1 christos (struct ipsecmod_env*)qstate->env->modinfo[id], qstate->qinfo.qname,
114 1.1 christos qstate->qinfo.qname_len, qstate->qinfo.qclass);
115 1.1 christos return 1;
116 1.1 christos }
117 1.1 christos
118 1.1 christos /**
119 1.1 christos * Exit module with an error status.
120 1.1 christos * @param qstate: query state
121 1.1 christos * @param id: module id.
122 1.1 christos */
123 1.1 christos static void
124 1.1 christos ipsecmod_error(struct module_qstate* qstate, int id)
125 1.1 christos {
126 1.1 christos qstate->ext_state[id] = module_error;
127 1.1 christos qstate->return_rcode = LDNS_RCODE_SERVFAIL;
128 1.1 christos }
129 1.1 christos
130 1.1 christos /**
131 1.1 christos * Generate a request for the IPSECKEY.
132 1.1 christos *
133 1.1 christos * @param qstate: query state that is the parent.
134 1.1 christos * @param id: module id.
135 1.1 christos * @param name: what name to query for.
136 1.1 christos * @param namelen: length of name.
137 1.1 christos * @param qtype: query type.
138 1.1 christos * @param qclass: query class.
139 1.1 christos * @param flags: additional flags, such as the CD bit (BIT_CD), or 0.
140 1.1 christos * @return false on alloc failure.
141 1.1 christos */
142 1.1 christos static int
143 1.1 christos generate_request(struct module_qstate* qstate, int id, uint8_t* name,
144 1.1 christos size_t namelen, uint16_t qtype, uint16_t qclass, uint16_t flags)
145 1.1 christos {
146 1.1 christos struct module_qstate* newq;
147 1.1 christos struct query_info ask;
148 1.1 christos ask.qname = name;
149 1.1 christos ask.qname_len = namelen;
150 1.1 christos ask.qtype = qtype;
151 1.1 christos ask.qclass = qclass;
152 1.1 christos ask.local_alias = NULL;
153 1.1 christos log_query_info(VERB_ALGO, "ipsecmod: generate request", &ask);
154 1.1 christos fptr_ok(fptr_whitelist_modenv_attach_sub(qstate->env->attach_sub));
155 1.1 christos if(!(*qstate->env->attach_sub)(qstate, &ask,
156 1.1 christos (uint16_t)(BIT_RD|flags), 0, 0, &newq)){
157 1.1 christos log_err("Could not generate request: out of memory");
158 1.1 christos return 0;
159 1.1 christos }
160 1.1 christos qstate->ext_state[id] = module_wait_subquery;
161 1.1 christos return 1;
162 1.1 christos }
163 1.1 christos
164 1.1 christos /**
165 1.1.1.2.2.1 martin * Check if the string passed is a valid domain name with safe characters to
166 1.1.1.2.2.1 martin * pass to a shell.
167 1.1.1.2.2.1 martin * This will only allow:
168 1.1.1.2.2.1 martin * - digits
169 1.1.1.2.2.1 martin * - alphas
170 1.1.1.2.2.1 martin * - hyphen (not at the start)
171 1.1.1.2.2.1 martin * - dot (not at the start, or the only character)
172 1.1.1.2.2.1 martin * - underscore
173 1.1.1.2.2.1 martin * @param s: pointer to the string.
174 1.1.1.2.2.1 martin * @param slen: string's length.
175 1.1.1.2.2.1 martin * @return true if s only contains safe characters; false otherwise.
176 1.1.1.2.2.1 martin */
177 1.1.1.2.2.1 martin static int
178 1.1.1.2.2.1 martin domainname_has_safe_characters(char* s, size_t slen) {
179 1.1.1.2.2.1 martin size_t i;
180 1.1.1.2.2.1 martin for(i = 0; i < slen; i++) {
181 1.1.1.2.2.1 martin if(s[i] == '\0') return 1;
182 1.1.1.2.2.1 martin if((s[i] == '-' && i != 0)
183 1.1.1.2.2.1 martin || (s[i] == '.' && (i != 0 || s[1] == '\0'))
184 1.1.1.2.2.1 martin || (s[i] == '_') || (s[i] >= '0' && s[i] <= '9')
185 1.1.1.2.2.1 martin || (s[i] >= 'A' && s[i] <= 'Z')
186 1.1.1.2.2.1 martin || (s[i] >= 'a' && s[i] <= 'z')) {
187 1.1.1.2.2.1 martin continue;
188 1.1.1.2.2.1 martin }
189 1.1.1.2.2.1 martin return 0;
190 1.1.1.2.2.1 martin }
191 1.1.1.2.2.1 martin return 1;
192 1.1.1.2.2.1 martin }
193 1.1.1.2.2.1 martin
194 1.1.1.2.2.1 martin /**
195 1.1.1.2.2.1 martin * Check if the stringified IPSECKEY RDATA contains safe characters to pass to
196 1.1.1.2.2.1 martin * a shell.
197 1.1.1.2.2.1 martin * This is only relevant for checking the gateway when the gateway type is 3
198 1.1.1.2.2.1 martin * (domainname).
199 1.1.1.2.2.1 martin * @param s: pointer to the string.
200 1.1.1.2.2.1 martin * @param slen: string's length.
201 1.1.1.2.2.1 martin * @return true if s contains only safe characters; false otherwise.
202 1.1.1.2.2.1 martin */
203 1.1.1.2.2.1 martin static int
204 1.1.1.2.2.1 martin ipseckey_has_safe_characters(char* s, size_t slen) {
205 1.1.1.2.2.1 martin int precedence, gateway_type, algorithm;
206 1.1.1.2.2.1 martin char* gateway;
207 1.1.1.2.2.1 martin gateway = (char*)calloc(slen, sizeof(char));
208 1.1.1.2.2.1 martin if(!gateway) {
209 1.1.1.2.2.1 martin log_err("ipsecmod: out of memory when calling the hook");
210 1.1.1.2.2.1 martin return 0;
211 1.1.1.2.2.1 martin }
212 1.1.1.2.2.1 martin if(sscanf(s, "%d %d %d %s ",
213 1.1.1.2.2.1 martin &precedence, &gateway_type, &algorithm, gateway) != 4) {
214 1.1.1.2.2.1 martin free(gateway);
215 1.1.1.2.2.1 martin return 0;
216 1.1.1.2.2.1 martin }
217 1.1.1.2.2.1 martin if(gateway_type != 3) {
218 1.1.1.2.2.1 martin free(gateway);
219 1.1.1.2.2.1 martin return 1;
220 1.1.1.2.2.1 martin }
221 1.1.1.2.2.1 martin if(domainname_has_safe_characters(gateway, slen)) {
222 1.1.1.2.2.1 martin free(gateway);
223 1.1.1.2.2.1 martin return 1;
224 1.1.1.2.2.1 martin }
225 1.1.1.2.2.1 martin free(gateway);
226 1.1.1.2.2.1 martin return 0;
227 1.1.1.2.2.1 martin }
228 1.1.1.2.2.1 martin
229 1.1.1.2.2.1 martin /**
230 1.1 christos * Prepare the data and call the hook.
231 1.1 christos *
232 1.1 christos * @param qstate: query state.
233 1.1 christos * @param iq: ipsecmod qstate.
234 1.1 christos * @param ie: ipsecmod environment.
235 1.1 christos * @return true on success, false otherwise.
236 1.1 christos */
237 1.1 christos static int
238 1.1 christos call_hook(struct module_qstate* qstate, struct ipsecmod_qstate* iq,
239 1.1 christos struct ipsecmod_env* ATTR_UNUSED(ie))
240 1.1 christos {
241 1.1 christos size_t slen, tempdata_len, tempstring_len, i;
242 1.1 christos char str[65535], *s, *tempstring;
243 1.1.1.2.2.1 martin int w = 0, w_temp, qtype;
244 1.1 christos struct ub_packed_rrset_key* rrset_key;
245 1.1 christos struct packed_rrset_data* rrset_data;
246 1.1 christos uint8_t *tempdata;
247 1.1 christos
248 1.1 christos /* Check if a shell is available */
249 1.1 christos if(system(NULL) == 0) {
250 1.1 christos log_err("ipsecmod: no shell available for ipsecmod-hook");
251 1.1 christos return 0;
252 1.1 christos }
253 1.1 christos
254 1.1 christos /* Zero the buffer. */
255 1.1 christos s = str;
256 1.1 christos slen = sizeof(str);
257 1.1 christos memset(s, 0, slen);
258 1.1 christos
259 1.1 christos /* Copy the hook into the buffer. */
260 1.1.1.2.2.1 martin w += sldns_str_print(&s, &slen, "%s", qstate->env->cfg->ipsecmod_hook);
261 1.1 christos /* Put space into the buffer. */
262 1.1.1.2.2.1 martin w += sldns_str_print(&s, &slen, " ");
263 1.1 christos /* Copy the qname into the buffer. */
264 1.1 christos tempstring = sldns_wire2str_dname(qstate->qinfo.qname,
265 1.1 christos qstate->qinfo.qname_len);
266 1.1 christos if(!tempstring) {
267 1.1 christos log_err("ipsecmod: out of memory when calling the hook");
268 1.1 christos return 0;
269 1.1 christos }
270 1.1.1.2.2.1 martin if(!domainname_has_safe_characters(tempstring, strlen(tempstring))) {
271 1.1.1.2.2.1 martin log_err("ipsecmod: qname has unsafe characters");
272 1.1.1.2.2.1 martin free(tempstring);
273 1.1.1.2.2.1 martin return 0;
274 1.1.1.2.2.1 martin }
275 1.1.1.2.2.1 martin w += sldns_str_print(&s, &slen, "\"%s\"", tempstring);
276 1.1 christos free(tempstring);
277 1.1 christos /* Put space into the buffer. */
278 1.1.1.2.2.1 martin w += sldns_str_print(&s, &slen, " ");
279 1.1 christos /* Copy the IPSECKEY TTL into the buffer. */
280 1.1 christos rrset_data = (struct packed_rrset_data*)iq->ipseckey_rrset->entry.data;
281 1.1.1.2.2.1 martin w += sldns_str_print(&s, &slen, "\"%ld\"", (long)rrset_data->ttl);
282 1.1 christos /* Put space into the buffer. */
283 1.1.1.2.2.1 martin w += sldns_str_print(&s, &slen, " ");
284 1.1 christos rrset_key = reply_find_answer_rrset(&qstate->return_msg->qinfo,
285 1.1 christos qstate->return_msg->rep);
286 1.1.1.2.2.1 martin /* Double check that the records are indeed A/AAAA.
287 1.1.1.2.2.1 martin * This should never happen as this function is only executed for A/AAAA
288 1.1.1.2.2.1 martin * queries but make sure we don't pass anything other than A/AAAA to the
289 1.1.1.2.2.1 martin * shell. */
290 1.1.1.2.2.1 martin qtype = ntohs(rrset_key->rk.type);
291 1.1.1.2.2.1 martin if(qtype != LDNS_RR_TYPE_AAAA && qtype != LDNS_RR_TYPE_A) {
292 1.1.1.2.2.1 martin log_err("ipsecmod: Answer is not of A or AAAA type");
293 1.1.1.2.2.1 martin return 0;
294 1.1.1.2.2.1 martin }
295 1.1 christos rrset_data = (struct packed_rrset_data*)rrset_key->entry.data;
296 1.1.1.2.2.1 martin /* Copy the A/AAAA record(s) into the buffer. Start and end this section
297 1.1.1.2.2.1 martin * with a double quote. */
298 1.1.1.2.2.1 martin w += sldns_str_print(&s, &slen, "\"");
299 1.1 christos for(i=0; i<rrset_data->count; i++) {
300 1.1 christos if(i > 0) {
301 1.1 christos /* Put space into the buffer. */
302 1.1.1.2.2.1 martin w += sldns_str_print(&s, &slen, " ");
303 1.1 christos }
304 1.1 christos /* Ignore the first two bytes, they are the rr_data len. */
305 1.1.1.2.2.1 martin w_temp = sldns_wire2str_rdata_buf(rrset_data->rr_data[i] + 2,
306 1.1 christos rrset_data->rr_len[i] - 2, s, slen, qstate->qinfo.qtype);
307 1.1.1.2.2.1 martin if(w_temp < 0) {
308 1.1 christos /* Error in printout. */
309 1.1.1.2.2.1 martin log_err("ipsecmod: Error in printing IP address");
310 1.1.1.2.2.1 martin return 0;
311 1.1.1.2.2.1 martin } else if((size_t)w_temp >= slen) {
312 1.1 christos s = NULL; /* We do not want str to point outside of buffer. */
313 1.1 christos slen = 0;
314 1.1.1.2.2.1 martin log_err("ipsecmod: shell command too long");
315 1.1.1.2.2.1 martin return 0;
316 1.1 christos } else {
317 1.1.1.2.2.1 martin s += w_temp;
318 1.1.1.2.2.1 martin slen -= w_temp;
319 1.1.1.2.2.1 martin w += w_temp;
320 1.1 christos }
321 1.1 christos }
322 1.1.1.2.2.1 martin w += sldns_str_print(&s, &slen, "\"");
323 1.1 christos /* Put space into the buffer. */
324 1.1.1.2.2.1 martin w += sldns_str_print(&s, &slen, " ");
325 1.1 christos /* Copy the IPSECKEY record(s) into the buffer. Start and end this section
326 1.1 christos * with a double quote. */
327 1.1.1.2.2.1 martin w += sldns_str_print(&s, &slen, "\"");
328 1.1 christos rrset_data = (struct packed_rrset_data*)iq->ipseckey_rrset->entry.data;
329 1.1 christos for(i=0; i<rrset_data->count; i++) {
330 1.1 christos if(i > 0) {
331 1.1 christos /* Put space into the buffer. */
332 1.1.1.2.2.1 martin w += sldns_str_print(&s, &slen, " ");
333 1.1 christos }
334 1.1 christos /* Ignore the first two bytes, they are the rr_data len. */
335 1.1 christos tempdata = rrset_data->rr_data[i] + 2;
336 1.1 christos tempdata_len = rrset_data->rr_len[i] - 2;
337 1.1 christos /* Save the buffer pointers. */
338 1.1 christos tempstring = s; tempstring_len = slen;
339 1.1.1.2.2.1 martin w_temp = sldns_wire2str_ipseckey_scan(&tempdata, &tempdata_len, &s,
340 1.1.1.2.2.1 martin &slen, NULL, 0, NULL);
341 1.1 christos /* There was an error when parsing the IPSECKEY; reset the buffer
342 1.1 christos * pointers to their previous values. */
343 1.1.1.2.2.1 martin if(w_temp == -1) {
344 1.1 christos s = tempstring; slen = tempstring_len;
345 1.1.1.2.2.1 martin } else if(w_temp > 0) {
346 1.1.1.2.2.1 martin if(!ipseckey_has_safe_characters(
347 1.1.1.2.2.1 martin tempstring, tempstring_len - slen)) {
348 1.1.1.2.2.1 martin log_err("ipsecmod: ipseckey has unsafe characters");
349 1.1.1.2.2.1 martin return 0;
350 1.1.1.2.2.1 martin }
351 1.1.1.2.2.1 martin w += w_temp;
352 1.1 christos }
353 1.1 christos }
354 1.1.1.2.2.1 martin w += sldns_str_print(&s, &slen, "\"");
355 1.1.1.2.2.1 martin if(w >= (int)sizeof(str)) {
356 1.1.1.2.2.1 martin log_err("ipsecmod: shell command too long");
357 1.1.1.2.2.1 martin return 0;
358 1.1.1.2.2.1 martin }
359 1.1.1.2.2.1 martin verbose(VERB_ALGO, "ipsecmod: shell command: '%s'", str);
360 1.1 christos /* ipsecmod-hook should return 0 on success. */
361 1.1 christos if(system(str) != 0)
362 1.1 christos return 0;
363 1.1 christos return 1;
364 1.1 christos }
365 1.1 christos
366 1.1 christos /**
367 1.1 christos * Handle an ipsecmod module event with a query
368 1.1 christos * @param qstate: query state (from the mesh), passed between modules.
369 1.1 christos * contains qstate->env module environment with global caches and so on.
370 1.1 christos * @param iq: query state specific for this module. per-query.
371 1.1 christos * @param ie: environment specific for this module. global.
372 1.1 christos * @param id: module id.
373 1.1 christos */
374 1.1 christos static void
375 1.1 christos ipsecmod_handle_query(struct module_qstate* qstate,
376 1.1 christos struct ipsecmod_qstate* iq, struct ipsecmod_env* ie, int id)
377 1.1 christos {
378 1.1 christos struct ub_packed_rrset_key* rrset_key;
379 1.1 christos struct packed_rrset_data* rrset_data;
380 1.1 christos size_t i;
381 1.1 christos /* Pass to next module if we are not enabled and whitelisted. */
382 1.1 christos if(!(iq->enabled && iq->is_whitelisted)) {
383 1.1 christos qstate->ext_state[id] = module_wait_module;
384 1.1 christos return;
385 1.1 christos }
386 1.1 christos /* New query, check if the query is for an A/AAAA record and disable
387 1.1 christos * caching for other modules. */
388 1.1 christos if(!iq->ipseckey_done) {
389 1.1 christos if(qstate->qinfo.qtype == LDNS_RR_TYPE_A ||
390 1.1 christos qstate->qinfo.qtype == LDNS_RR_TYPE_AAAA) {
391 1.1 christos char type[16];
392 1.1 christos sldns_wire2str_type_buf(qstate->qinfo.qtype, type,
393 1.1 christos sizeof(type));
394 1.1 christos verbose(VERB_ALGO, "ipsecmod: query for %s; engaging",
395 1.1 christos type);
396 1.1 christos qstate->no_cache_store = 1;
397 1.1 christos }
398 1.1 christos /* Pass request to next module. */
399 1.1 christos qstate->ext_state[id] = module_wait_module;
400 1.1 christos return;
401 1.1 christos }
402 1.1 christos /* IPSECKEY subquery is finished. */
403 1.1 christos /* We have an IPSECKEY answer. */
404 1.1 christos if(iq->ipseckey_rrset) {
405 1.1 christos rrset_data = (struct packed_rrset_data*)iq->ipseckey_rrset->entry.data;
406 1.1 christos if(rrset_data) {
407 1.1 christos /* If bogus return SERVFAIL. */
408 1.1 christos if(!qstate->env->cfg->ipsecmod_ignore_bogus &&
409 1.1 christos rrset_data->security == sec_status_bogus) {
410 1.1 christos log_err("ipsecmod: bogus IPSECKEY");
411 1.1 christos ipsecmod_error(qstate, id);
412 1.1 christos return;
413 1.1 christos }
414 1.1 christos /* We have a valid IPSECKEY reply, call hook. */
415 1.1 christos if(!call_hook(qstate, iq, ie) &&
416 1.1 christos qstate->env->cfg->ipsecmod_strict) {
417 1.1 christos log_err("ipsecmod: ipsecmod-hook failed");
418 1.1 christos ipsecmod_error(qstate, id);
419 1.1 christos return;
420 1.1 christos }
421 1.1 christos /* Make sure the A/AAAA's TTL is equal/less than the
422 1.1 christos * ipsecmod_max_ttl. */
423 1.1 christos rrset_key = reply_find_answer_rrset(&qstate->return_msg->qinfo,
424 1.1 christos qstate->return_msg->rep);
425 1.1 christos rrset_data = (struct packed_rrset_data*)rrset_key->entry.data;
426 1.1 christos if(rrset_data->ttl > (time_t)qstate->env->cfg->ipsecmod_max_ttl) {
427 1.1 christos /* Update TTL for rrset to fixed value. */
428 1.1 christos rrset_data->ttl = qstate->env->cfg->ipsecmod_max_ttl;
429 1.1 christos for(i=0; i<rrset_data->count+rrset_data->rrsig_count; i++)
430 1.1 christos rrset_data->rr_ttl[i] = qstate->env->cfg->ipsecmod_max_ttl;
431 1.1 christos /* Also update reply_info's TTL */
432 1.1 christos if(qstate->return_msg->rep->ttl > (time_t)qstate->env->cfg->ipsecmod_max_ttl) {
433 1.1 christos qstate->return_msg->rep->ttl =
434 1.1 christos qstate->env->cfg->ipsecmod_max_ttl;
435 1.1 christos qstate->return_msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(
436 1.1 christos qstate->return_msg->rep->ttl);
437 1.1.1.2 christos qstate->return_msg->rep->serve_expired_ttl = qstate->return_msg->rep->ttl +
438 1.1.1.2 christos qstate->env->cfg->serve_expired_ttl;
439 1.1 christos }
440 1.1 christos }
441 1.1 christos }
442 1.1 christos }
443 1.1 christos /* Store A/AAAA in cache. */
444 1.1 christos if(!dns_cache_store(qstate->env, &qstate->qinfo,
445 1.1 christos qstate->return_msg->rep, 0, qstate->prefetch_leeway,
446 1.1 christos 0, qstate->region, qstate->query_flags)) {
447 1.1 christos log_err("ipsecmod: out of memory caching record");
448 1.1 christos }
449 1.1 christos qstate->ext_state[id] = module_finished;
450 1.1 christos }
451 1.1 christos
452 1.1 christos /**
453 1.1 christos * Handle an ipsecmod module event with a response from the iterator.
454 1.1 christos * @param qstate: query state (from the mesh), passed between modules.
455 1.1 christos * contains qstate->env module environment with global caches and so on.
456 1.1 christos * @param iq: query state specific for this module. per-query.
457 1.1 christos * @param ie: environment specific for this module. global.
458 1.1 christos * @param id: module id.
459 1.1 christos */
460 1.1 christos static void
461 1.1 christos ipsecmod_handle_response(struct module_qstate* qstate,
462 1.1 christos struct ipsecmod_qstate* ATTR_UNUSED(iq),
463 1.1 christos struct ipsecmod_env* ATTR_UNUSED(ie), int id)
464 1.1 christos {
465 1.1 christos /* Pass to previous module if we are not enabled and whitelisted. */
466 1.1 christos if(!(iq->enabled && iq->is_whitelisted)) {
467 1.1 christos qstate->ext_state[id] = module_finished;
468 1.1 christos return;
469 1.1 christos }
470 1.1 christos /* check if the response is for an A/AAAA query. */
471 1.1 christos if((qstate->qinfo.qtype == LDNS_RR_TYPE_A ||
472 1.1 christos qstate->qinfo.qtype == LDNS_RR_TYPE_AAAA) &&
473 1.1 christos /* check that we had an answer for the A/AAAA query. */
474 1.1 christos qstate->return_msg &&
475 1.1 christos reply_find_answer_rrset(&qstate->return_msg->qinfo,
476 1.1 christos qstate->return_msg->rep) &&
477 1.1 christos /* check that another module didn't SERVFAIL. */
478 1.1 christos qstate->return_rcode == LDNS_RCODE_NOERROR) {
479 1.1 christos char type[16];
480 1.1 christos sldns_wire2str_type_buf(qstate->qinfo.qtype, type,
481 1.1 christos sizeof(type));
482 1.1 christos verbose(VERB_ALGO, "ipsecmod: response for %s; generating IPSECKEY "
483 1.1 christos "subquery", type);
484 1.1 christos /* generate an IPSECKEY query. */
485 1.1 christos if(!generate_request(qstate, id, qstate->qinfo.qname,
486 1.1 christos qstate->qinfo.qname_len, LDNS_RR_TYPE_IPSECKEY,
487 1.1 christos qstate->qinfo.qclass, 0)) {
488 1.1 christos log_err("ipsecmod: could not generate subquery.");
489 1.1 christos ipsecmod_error(qstate, id);
490 1.1 christos }
491 1.1 christos return;
492 1.1 christos }
493 1.1 christos /* we are done with the query. */
494 1.1 christos qstate->ext_state[id] = module_finished;
495 1.1 christos }
496 1.1 christos
497 1.1 christos void
498 1.1 christos ipsecmod_operate(struct module_qstate* qstate, enum module_ev event, int id,
499 1.1 christos struct outbound_entry* outbound)
500 1.1 christos {
501 1.1 christos struct ipsecmod_env* ie = (struct ipsecmod_env*)qstate->env->modinfo[id];
502 1.1 christos struct ipsecmod_qstate* iq = (struct ipsecmod_qstate*)qstate->minfo[id];
503 1.1 christos verbose(VERB_QUERY, "ipsecmod[module %d] operate: extstate:%s event:%s",
504 1.1 christos id, strextstate(qstate->ext_state[id]), strmodulevent(event));
505 1.1 christos if(iq) log_query_info(VERB_QUERY, "ipsecmod operate: query",
506 1.1 christos &qstate->qinfo);
507 1.1 christos
508 1.1 christos /* create ipsecmod_qstate. */
509 1.1 christos if((event == module_event_new || event == module_event_pass) &&
510 1.1 christos iq == NULL) {
511 1.1 christos if(!ipsecmod_new(qstate, id)) {
512 1.1 christos ipsecmod_error(qstate, id);
513 1.1 christos return;
514 1.1 christos }
515 1.1 christos iq = (struct ipsecmod_qstate*)qstate->minfo[id];
516 1.1 christos }
517 1.1 christos if(iq && (event == module_event_pass || event == module_event_new)) {
518 1.1 christos ipsecmod_handle_query(qstate, iq, ie, id);
519 1.1 christos return;
520 1.1 christos }
521 1.1 christos if(iq && (event == module_event_moddone)) {
522 1.1 christos ipsecmod_handle_response(qstate, iq, ie, id);
523 1.1 christos return;
524 1.1 christos }
525 1.1 christos if(iq && outbound) {
526 1.1 christos /* cachedb does not need to process responses at this time
527 1.1 christos * ignore it.
528 1.1 christos cachedb_process_response(qstate, iq, ie, id, outbound, event);
529 1.1 christos */
530 1.1 christos return;
531 1.1 christos }
532 1.1 christos if(event == module_event_error) {
533 1.1 christos verbose(VERB_ALGO, "got called with event error, giving up");
534 1.1 christos ipsecmod_error(qstate, id);
535 1.1 christos return;
536 1.1 christos }
537 1.1 christos if(!iq && (event == module_event_moddone)) {
538 1.1 christos /* during priming, module done but we never started. */
539 1.1 christos qstate->ext_state[id] = module_finished;
540 1.1 christos return;
541 1.1 christos }
542 1.1 christos
543 1.1 christos log_err("ipsecmod: bad event %s", strmodulevent(event));
544 1.1 christos ipsecmod_error(qstate, id);
545 1.1 christos return;
546 1.1 christos }
547 1.1 christos
548 1.1 christos void
549 1.1 christos ipsecmod_inform_super(struct module_qstate* qstate, int id,
550 1.1 christos struct module_qstate* super)
551 1.1 christos {
552 1.1 christos struct ipsecmod_qstate* siq;
553 1.1 christos log_query_info(VERB_ALGO, "ipsecmod: inform_super, sub is",
554 1.1 christos &qstate->qinfo);
555 1.1 christos log_query_info(VERB_ALGO, "super is", &super->qinfo);
556 1.1 christos siq = (struct ipsecmod_qstate*)super->minfo[id];
557 1.1 christos if(!siq) {
558 1.1 christos verbose(VERB_ALGO, "super has no ipsecmod state");
559 1.1 christos return;
560 1.1 christos }
561 1.1 christos
562 1.1 christos if(qstate->return_msg) {
563 1.1 christos struct ub_packed_rrset_key* rrset_key = reply_find_answer_rrset(
564 1.1 christos &qstate->return_msg->qinfo, qstate->return_msg->rep);
565 1.1 christos if(rrset_key) {
566 1.1 christos /* We have an answer. */
567 1.1 christos /* Copy to super's region. */
568 1.1 christos rrset_key = packed_rrset_copy_region(rrset_key, super->region, 0);
569 1.1 christos siq->ipseckey_rrset = rrset_key;
570 1.1 christos if(!rrset_key) {
571 1.1 christos log_err("ipsecmod: out of memory.");
572 1.1 christos }
573 1.1 christos }
574 1.1 christos }
575 1.1 christos /* Notify super to proceed. */
576 1.1 christos siq->ipseckey_done = 1;
577 1.1 christos }
578 1.1 christos
579 1.1 christos void
580 1.1 christos ipsecmod_clear(struct module_qstate* qstate, int id)
581 1.1 christos {
582 1.1 christos if(!qstate)
583 1.1 christos return;
584 1.1 christos qstate->minfo[id] = NULL;
585 1.1 christos }
586 1.1 christos
587 1.1 christos size_t
588 1.1 christos ipsecmod_get_mem(struct module_env* env, int id)
589 1.1 christos {
590 1.1 christos struct ipsecmod_env* ie = (struct ipsecmod_env*)env->modinfo[id];
591 1.1 christos if(!ie)
592 1.1 christos return 0;
593 1.1 christos return sizeof(*ie) + ipsecmod_whitelist_get_mem(ie->whitelist);
594 1.1 christos }
595 1.1 christos
596 1.1 christos /**
597 1.1 christos * The ipsecmod function block
598 1.1 christos */
599 1.1 christos static struct module_func_block ipsecmod_block = {
600 1.1 christos "ipsecmod",
601 1.1 christos &ipsecmod_init, &ipsecmod_deinit, &ipsecmod_operate,
602 1.1 christos &ipsecmod_inform_super, &ipsecmod_clear, &ipsecmod_get_mem
603 1.1 christos };
604 1.1 christos
605 1.1 christos struct module_func_block*
606 1.1 christos ipsecmod_get_funcblock(void)
607 1.1 christos {
608 1.1 christos return &ipsecmod_block;
609 1.1 christos }
610 1.1 christos #endif /* USE_IPSECMOD */
611