2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 #include <vlibapi/api.h>
18 #include <vlibmemory/api.h>
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
23 /* define message IDs */
24 #include <dns/dns_msg_enum.h>
26 #define vl_typedefs /* define message structures */
27 #include <dns/dns_all_api_h.h>
30 #define vl_endianfun /* define message structures */
31 #include <dns/dns_all_api_h.h>
34 /* instantiate all the print functions we know about */
35 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
37 #include <dns/dns_all_api_h.h>
40 #include <vlibapi/api_helper_macros.h>
43 vnet_dns_response_to_reply (u8 * response,
44 vl_api_dns_resolve_name_reply_t * rmp,
47 vnet_dns_response_to_name (u8 * response,
48 vl_api_dns_resolve_ip_reply_t * rmp,
52 resolve_event (dns_main_t * dm, f64 now, u8 * reply)
54 vlib_main_t *vm = dm->vlib_main;
55 dns_pending_request_t *pr;
58 dns_cache_entry_t *ep;
67 d = (dns_header_t *) reply;
68 flags = clib_net_to_host_u16 (d->flags);
69 rcode = flags & DNS_RCODE_MASK;
71 /* $$$ u16 limits cache to 65K entries, fix later multiple dst ports */
72 pool_index = clib_net_to_host_u16 (d->id);
75 if (pool_is_free_index (dm->entries, pool_index))
79 clib_warning ("pool index %d is free", pool_index);
80 vlib_node_increment_counter (vm, dns46_reply_node.index,
81 DNS46_REPLY_ERROR_NO_ELT, 1);
82 dns_cache_unlock (dm);
86 ep = pool_elt_at_index (dm->entries, pool_index);
89 vec_free (ep->dns_response);
91 /* Handle [sic] recursion AKA CNAME indirection */
92 rv = vnet_dns_cname_indirection_nolock (dm, pool_index, reply);
94 /* CNAME found, further resolution pending, we're done here */
97 dns_cache_unlock (dm);
100 /* Server backfire: refused to answer, or sent zero replies */
103 /* Try a different server */
104 if (ep->server_af /* ip6 */ )
107 clib_warning ("Server %U failed to resolve '%s'",
109 dm->ip6_name_servers + ep->server_rotor, ep->name);
110 /* Any more servers to try? */
111 if (ep->server_fails > 1 || vec_len (dm->ip6_name_servers) <= 1)
113 /* No, tell the client to go away */
119 if (ep->server_rotor >= vec_len (dm->ip6_name_servers))
120 ep->server_rotor = 0;
122 clib_warning ("Try server %U", format_ip6_address,
123 dm->ip6_name_servers + ep->server_rotor);
124 vnet_dns_send_dns6_request
125 (dm, ep, dm->ip6_name_servers + ep->server_rotor);
130 clib_warning ("Server %U failed to resolve '%s'",
132 dm->ip4_name_servers + ep->server_rotor, ep->name);
134 if (ep->server_fails > 1 || vec_len (dm->ip4_name_servers) <= 1)
136 /* No, tell the client to go away */
142 if (ep->server_rotor >= vec_len (dm->ip4_name_servers))
143 ep->server_rotor = 0;
145 clib_warning ("Try server %U", format_ip4_address,
146 dm->ip4_name_servers + ep->server_rotor);
147 vnet_dns_send_dns4_request
148 (dm, ep, dm->ip4_name_servers + ep->server_rotor);
150 dns_cache_unlock (dm);
155 /* Save the response */
156 ep->dns_response = reply;
159 * Pick a sensible default cache entry expiration time.
160 * We don't play the 10-second timeout game.
162 ep->expiration_time = now + 600.0;
165 clib_warning ("resolving '%s', was %s valid",
166 ep->name, (ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) ?
169 * The world is a mess. A single DNS request sent to e.g. 8.8.8.8
170 * may yield multiple, subtly different responses - all with the same
171 * DNS protocol-level ID.
173 * Last response wins in terms of what ends up in the cache.
174 * First response wins in terms of the response sent to the client.
177 /* Strong hint that we may not find a pending resolution entry */
178 entry_was_valid = (ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) ? 1 : 0;
180 if (vec_len (ep->dns_response))
181 ep->flags |= DNS_CACHE_ENTRY_FLAG_VALID;
183 /* Most likely, send 1 message */
184 for (i = 0; i < vec_len (ep->pending_requests); i++)
186 vl_api_registration_t *regp;
188 pr = vec_elt_at_index (ep->pending_requests, i);
190 switch (pr->request_type)
192 case DNS_API_PENDING_NAME_TO_IP:
194 vl_api_dns_resolve_name_reply_t *rmp;
195 regp = vl_api_client_index_to_registration (pr->client_index);
199 rmp = vl_msg_api_alloc (sizeof (*rmp));
201 clib_host_to_net_u16 (VL_API_DNS_RESOLVE_NAME_REPLY
203 rmp->context = pr->client_context;
205 rv = vnet_dns_response_to_reply (ep->dns_response, rmp, &min_ttl);
207 ep->expiration_time = now + min_ttl;
208 rmp->retval = clib_host_to_net_u32 (rv);
209 vl_api_send_msg (regp, (u8 *) rmp);
213 case DNS_API_PENDING_IP_TO_NAME:
215 vl_api_dns_resolve_ip_reply_t *rmp;
217 regp = vl_api_client_index_to_registration (pr->client_index);
221 rmp = vl_msg_api_alloc (sizeof (*rmp));
223 clib_host_to_net_u16 (VL_API_DNS_RESOLVE_IP_REPLY
225 rmp->context = pr->client_context;
227 rv = vnet_dns_response_to_name (ep->dns_response, rmp, &min_ttl);
229 ep->expiration_time = now + min_ttl;
230 rmp->retval = clib_host_to_net_u32 (rv);
231 vl_api_send_msg (regp, (u8 *) rmp);
235 case DNS_PEER_PENDING_IP_TO_NAME:
236 case DNS_PEER_PENDING_NAME_TO_IP:
238 vnet_send_dns6_reply (dm, pr, ep, 0 /* allocate a buffer */ );
240 vnet_send_dns4_reply (dm, pr, ep, 0 /* allocate a buffer */ );
243 clib_warning ("request type %d unknown", pr->request_type);
247 vec_free (ep->pending_requests);
250 for (i = 0; i < vec_len (dm->unresolved_entries); i++)
252 if (dm->unresolved_entries[i] == pool_index)
254 vec_delete (dm->unresolved_entries, 1, i);
259 /* See multiple response comment above... */
260 if (remove_count == 0)
262 u32 error_code = entry_was_valid ? DNS46_REPLY_ERROR_MULTIPLE_REPLY :
263 DNS46_REPLY_ERROR_NO_UNRESOLVED_ENTRY;
265 vlib_node_increment_counter (vm, dns46_reply_node.index, error_code, 1);
266 dns_cache_unlock (dm);
270 /* Deal with bogus names, server issues, etc. */
274 case DNS_RCODE_NO_ERROR:
277 case DNS_RCODE_SERVER_FAILURE:
278 case DNS_RCODE_NOT_IMPLEMENTED:
279 case DNS_RCODE_REFUSED:
280 if (ep->server_af == 0)
281 clib_warning ("name server %U can't resolve '%s'",
283 dm->ip4_name_servers + ep->server_rotor, ep->name);
285 clib_warning ("name server %U can't resolve '%s'",
287 dm->ip6_name_servers + ep->server_rotor, ep->name);
289 case DNS_RCODE_NAME_ERROR:
290 case DNS_RCODE_FORMAT_ERROR:
291 /* remove trash from the cache... */
292 vnet_dns_delete_entry_by_index_nolock (dm, ep - dm->entries);
297 dns_cache_unlock (dm);
302 retry_scan (dns_main_t * dm, f64 now)
305 dns_cache_entry_t *ep;
307 for (i = 0; i < vec_len (dm->unresolved_entries); i++)
310 ep = pool_elt_at_index (dm->entries, dm->unresolved_entries[i]);
312 ASSERT ((ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) == 0);
313 vnet_send_dns_request (dm, ep);
314 dns_cache_unlock (dm);
319 dns_resolver_process (vlib_main_t * vm,
320 vlib_node_runtime_t * rt, vlib_frame_t * f)
322 dns_main_t *dm = &dns_main;
324 f64 timeout = 1000.0;
325 uword *event_data = 0;
331 vlib_process_wait_for_event_or_clock (vm, timeout);
333 now = vlib_time_now (vm);
335 event_type = vlib_process_get_events (vm, (uword **) & event_data);
339 /* Send one of these when a resolution is pending */
340 case DNS_RESOLVER_EVENT_PENDING:
344 case DNS_RESOLVER_EVENT_RESOLVED:
345 for (i = 0; i < vec_len (event_data); i++)
346 resolve_event (dm, now, (u8 *) event_data[i]);
349 case ~0: /* timeout */
350 retry_scan (dm, now);
353 vec_reset_length (event_data);
355 /* No work? Back to slow timeout mode... */
356 if (vec_len (dm->unresolved_entries) == 0)
359 return 0; /* or not */
363 vnet_dns_create_resolver_process (dns_main_t * dm)
365 /* Already created the resolver process? */
366 if (dm->resolver_process_node_index > 0)
369 /* No, create it now and make a note of the node index */
370 dm->resolver_process_node_index = vlib_process_create
371 (dm->vlib_main, "dns-resolver-process",
372 dns_resolver_process, 16 /* log2_n_stack_bytes */ );
376 * fd.io coding-style-patch-verification: ON
379 * eval: (c-set-style "gnu")