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.
16 #include <vnet/dns/dns.h>
17 #include <vlibapi/api.h>
18 #include <vlibmemory/api.h>
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
23 #include <vnet/vnet_msg_enum.h>
25 #define vl_typedefs /* define message structures */
26 #include <vnet/vnet_all_api_h.h>
29 #define vl_endianfun /* define message structures */
30 #include <vnet/vnet_all_api_h.h>
33 /* instantiate all the print functions we know about */
34 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
36 #include <vnet/vnet_all_api_h.h>
39 #include <vlibapi/api_helper_macros.h>
42 vnet_dns_response_to_reply (u8 * response,
43 vl_api_dns_resolve_name_reply_t * rmp,
46 vnet_dns_response_to_name (u8 * response,
47 vl_api_dns_resolve_ip_reply_t * rmp,
51 resolve_event (dns_main_t * dm, f64 now, u8 * reply)
53 vlib_main_t *vm = dm->vlib_main;
54 dns_pending_request_t *pr;
57 dns_cache_entry_t *ep;
66 d = (dns_header_t *) reply;
67 flags = clib_net_to_host_u16 (d->flags);
68 rcode = flags & DNS_RCODE_MASK;
70 /* $$$ u16 limits cache to 65K entries, fix later multiple dst ports */
71 pool_index = clib_net_to_host_u16 (d->id);
74 if (pool_is_free_index (dm->entries, pool_index))
78 clib_warning ("pool index %d is free", pool_index);
79 vlib_node_increment_counter (vm, dns46_reply_node.index,
80 DNS46_REPLY_ERROR_NO_ELT, 1);
81 dns_cache_unlock (dm);
85 ep = pool_elt_at_index (dm->entries, pool_index);
88 vec_free (ep->dns_response);
90 /* Handle [sic] recursion AKA CNAME indirection */
91 rv = vnet_dns_cname_indirection_nolock (dm, pool_index, reply);
93 /* CNAME found, further resolution pending, we're done here */
96 dns_cache_unlock (dm);
99 /* Server backfire: refused to answer, or sent zero replies */
102 /* Try a different server */
103 if (ep->server_af /* ip6 */ )
106 clib_warning ("Server %U failed to resolve '%s'",
108 dm->ip6_name_servers + ep->server_rotor, ep->name);
109 /* Any more servers to try? */
110 if (ep->server_fails > 1 || vec_len (dm->ip6_name_servers) <= 1)
112 /* No, tell the client to go away */
118 if (ep->server_rotor >= vec_len (dm->ip6_name_servers))
119 ep->server_rotor = 0;
121 clib_warning ("Try server %U", format_ip6_address,
122 dm->ip6_name_servers + ep->server_rotor);
123 vnet_dns_send_dns6_request
124 (dm, ep, dm->ip6_name_servers + ep->server_rotor);
129 clib_warning ("Server %U failed to resolve '%s'",
131 dm->ip4_name_servers + ep->server_rotor, ep->name);
133 if (ep->server_fails > 1 || vec_len (dm->ip4_name_servers) <= 1)
135 /* No, tell the client to go away */
141 if (ep->server_rotor >= vec_len (dm->ip4_name_servers))
142 ep->server_rotor = 0;
144 clib_warning ("Try server %U", format_ip4_address,
145 dm->ip4_name_servers + ep->server_rotor);
146 vnet_dns_send_dns4_request
147 (dm, ep, dm->ip4_name_servers + ep->server_rotor);
149 dns_cache_unlock (dm);
154 /* Save the response */
155 ep->dns_response = reply;
158 * Pick a sensible default cache entry expiration time.
159 * We don't play the 10-second timeout game.
161 ep->expiration_time = now + 600.0;
164 clib_warning ("resolving '%s', was %s valid",
165 ep->name, (ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) ?
168 * The world is a mess. A single DNS request sent to e.g. 8.8.8.8
169 * may yield multiple, subtly different responses - all with the same
170 * DNS protocol-level ID.
172 * Last response wins in terms of what ends up in the cache.
173 * First response wins in terms of the response sent to the client.
176 /* Strong hint that we may not find a pending resolution entry */
177 entry_was_valid = (ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) ? 1 : 0;
179 if (vec_len (ep->dns_response))
180 ep->flags |= DNS_CACHE_ENTRY_FLAG_VALID;
182 /* Most likely, send 1 message */
183 for (i = 0; i < vec_len (ep->pending_requests); i++)
185 vl_api_registration_t *regp;
187 pr = vec_elt_at_index (ep->pending_requests, i);
189 switch (pr->request_type)
191 case DNS_API_PENDING_NAME_TO_IP:
193 vl_api_dns_resolve_name_reply_t *rmp;
194 regp = vl_api_client_index_to_registration (pr->client_index);
198 rmp = vl_msg_api_alloc (sizeof (*rmp));
200 clib_host_to_net_u16 (VL_API_DNS_RESOLVE_NAME_REPLY);
201 rmp->context = pr->client_context;
203 rv = vnet_dns_response_to_reply (ep->dns_response, rmp, &min_ttl);
205 ep->expiration_time = now + min_ttl;
206 rmp->retval = clib_host_to_net_u32 (rv);
207 vl_api_send_msg (regp, (u8 *) rmp);
211 case DNS_API_PENDING_IP_TO_NAME:
213 vl_api_dns_resolve_ip_reply_t *rmp;
215 regp = vl_api_client_index_to_registration (pr->client_index);
219 rmp = vl_msg_api_alloc (sizeof (*rmp));
221 clib_host_to_net_u16 (VL_API_DNS_RESOLVE_IP_REPLY);
222 rmp->context = pr->client_context;
224 rv = vnet_dns_response_to_name (ep->dns_response, rmp, &min_ttl);
226 ep->expiration_time = now + min_ttl;
227 rmp->retval = clib_host_to_net_u32 (rv);
228 vl_api_send_msg (regp, (u8 *) rmp);
232 case DNS_PEER_PENDING_IP_TO_NAME:
233 case DNS_PEER_PENDING_NAME_TO_IP:
235 vnet_send_dns6_reply (dm, pr, ep, 0 /* allocate a buffer */ );
237 vnet_send_dns4_reply (dm, pr, ep, 0 /* allocate a buffer */ );
240 clib_warning ("request type %d unknown", pr->request_type);
244 vec_free (ep->pending_requests);
247 for (i = 0; i < vec_len (dm->unresolved_entries); i++)
249 if (dm->unresolved_entries[i] == pool_index)
251 vec_delete (dm->unresolved_entries, 1, i);
256 /* See multiple response comment above... */
257 if (remove_count == 0)
259 u32 error_code = entry_was_valid ? DNS46_REPLY_ERROR_MULTIPLE_REPLY :
260 DNS46_REPLY_ERROR_NO_UNRESOLVED_ENTRY;
262 vlib_node_increment_counter (vm, dns46_reply_node.index, error_code, 1);
263 dns_cache_unlock (dm);
267 /* Deal with bogus names, server issues, etc. */
271 case DNS_RCODE_NO_ERROR:
274 case DNS_RCODE_SERVER_FAILURE:
275 case DNS_RCODE_NOT_IMPLEMENTED:
276 case DNS_RCODE_REFUSED:
277 if (ep->server_af == 0)
278 clib_warning ("name server %U can't resolve '%s'",
280 dm->ip4_name_servers + ep->server_rotor, ep->name);
282 clib_warning ("name server %U can't resolve '%s'",
284 dm->ip6_name_servers + ep->server_rotor, ep->name);
286 case DNS_RCODE_NAME_ERROR:
287 case DNS_RCODE_FORMAT_ERROR:
288 /* remove trash from the cache... */
289 vnet_dns_delete_entry_by_index_nolock (dm, ep - dm->entries);
294 dns_cache_unlock (dm);
299 retry_scan (dns_main_t * dm, f64 now)
302 dns_cache_entry_t *ep;
304 for (i = 0; i < vec_len (dm->unresolved_entries); i++)
307 ep = pool_elt_at_index (dm->entries, dm->unresolved_entries[i]);
309 ASSERT ((ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) == 0);
310 vnet_send_dns_request (dm, ep);
311 dns_cache_unlock (dm);
316 dns_resolver_process (vlib_main_t * vm,
317 vlib_node_runtime_t * rt, vlib_frame_t * f)
319 dns_main_t *dm = &dns_main;
321 f64 timeout = 1000.0;
322 uword *event_data = 0;
328 vlib_process_wait_for_event_or_clock (vm, timeout);
330 now = vlib_time_now (vm);
332 event_type = vlib_process_get_events (vm, (uword **) & event_data);
336 /* Send one of these when a resolution is pending */
337 case DNS_RESOLVER_EVENT_PENDING:
341 case DNS_RESOLVER_EVENT_RESOLVED:
342 for (i = 0; i < vec_len (event_data); i++)
343 resolve_event (dm, now, (u8 *) event_data[i]);
346 case ~0: /* timeout */
347 retry_scan (dm, now);
350 vec_reset_length (event_data);
352 /* No work? Back to slow timeout mode... */
353 if (vec_len (dm->unresolved_entries) == 0)
356 return 0; /* or not */
360 vnet_dns_create_resolver_process (dns_main_t * dm)
362 /* Already created the resolver process? */
363 if (dm->resolver_process_node_index > 0)
366 /* No, create it now and make a note of the node index */
367 dm->resolver_process_node_index = vlib_process_create
368 (dm->vlib_main, "dns-resolver-process",
369 dns_resolver_process, 16 /* log2_n_stack_bytes */ );
373 * fd.io coding-style-patch-verification: ON
376 * eval: (c-set-style "gnu")