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>
41 vlib_node_registration_t dns_resolver_node;
44 vnet_dns_response_to_reply (u8 * response,
45 vl_api_dns_resolve_name_reply_t * rmp,
48 vnet_dns_response_to_name (u8 * response,
49 vl_api_dns_resolve_ip_reply_t * rmp,
53 resolve_event (dns_main_t * dm, f64 now, u8 * reply)
55 vlib_main_t *vm = dm->vlib_main;
58 dns_cache_entry_t *ep;
65 d = (dns_header_t *) reply;
66 flags = clib_net_to_host_u16 (d->flags);
67 rcode = flags & DNS_RCODE_MASK;
69 /* $$$ u16 limits cache to 65K entries, fix later multiple dst ports */
70 pool_index = clib_net_to_host_u16 (d->id);
73 if (pool_is_free_index (dm->entries, pool_index))
76 vlib_node_increment_counter (vm, dns46_reply_node.index,
77 DNS46_REPLY_ERROR_NO_ELT, 1);
78 dns_cache_unlock (dm);
82 ep = pool_elt_at_index (dm->entries, pool_index);
85 vec_free (ep->dns_response);
87 /* Handle [sic] recursion AKA CNAME indirection */
88 if (vnet_dns_cname_indirection_nolock (dm, pool_index, reply))
90 dns_cache_unlock (dm);
94 /* Save the response */
95 ep->dns_response = reply;
96 /* Pick some sensible default. */
97 ep->expiration_time = now + 600.0;
98 if (vec_len (ep->dns_response))
99 ep->flags |= DNS_CACHE_ENTRY_FLAG_VALID;
101 /* Most likely, send 1 message */
102 for (i = 0; i < vec_len (ep->pending_api_requests); i++)
104 vl_api_registration_t *regp;
106 regp = vl_api_client_index_to_registration
107 (ep->pending_api_requests[i].client_index);
112 if (ep->pending_api_requests[i].request_type
113 == DNS_API_PENDING_NAME_TO_IP)
115 vl_api_dns_resolve_name_reply_t *rmp;
116 rmp = vl_msg_api_alloc (sizeof (*rmp));
118 clib_host_to_net_u16 (VL_API_DNS_RESOLVE_NAME_REPLY);
119 rmp->context = ep->pending_api_requests[i].client_context;
121 rv = vnet_dns_response_to_reply (ep->dns_response, rmp, &min_ttl);
123 ep->expiration_time = now + min_ttl;
124 rmp->retval = clib_host_to_net_u32 (rv);
125 vl_msg_api_send (regp, (u8 *) rmp);
129 vl_api_dns_resolve_ip_reply_t *rmp;
130 rmp = vl_msg_api_alloc (sizeof (*rmp));
132 clib_host_to_net_u16 (VL_API_DNS_RESOLVE_IP_REPLY);
133 rmp->context = ep->pending_api_requests[i].client_context;
135 rv = vnet_dns_response_to_name (ep->dns_response, rmp, &min_ttl);
137 ep->expiration_time = now + min_ttl;
138 rmp->retval = clib_host_to_net_u32 (rv);
139 vl_msg_api_send (regp, (u8 *) rmp);
142 vec_free (ep->pending_api_requests);
144 /* $$$ Add ip4/ip6 reply code */
145 vec_free (ep->ip4_peers_to_notify);
146 vec_free (ep->ip6_peers_to_notify);
148 for (i = 0; i < vec_len (dm->unresolved_entries); i++)
150 if (dm->unresolved_entries[i] == pool_index)
152 vec_delete (dm->unresolved_entries, 1, i);
156 clib_warning ("pool index %d AWOL from unresolved vector", pool_index);
159 /* Deal with bogus names, server issues, etc. */
163 case DNS_RCODE_NO_ERROR:
166 case DNS_RCODE_SERVER_FAILURE:
167 case DNS_RCODE_NOT_IMPLEMENTED:
168 case DNS_RCODE_REFUSED:
169 if (ep->server_af == 0)
170 clib_warning ("name server %U backfire",
172 dm->ip4_name_servers + ep->server_rotor);
174 clib_warning ("name server %U backfire",
176 dm->ip6_name_servers + ep->server_rotor);
178 case DNS_RCODE_NAME_ERROR:
179 case DNS_RCODE_FORMAT_ERROR:
180 /* remove trash from the cache... */
181 vnet_dns_delete_entry_by_index_nolock (dm, ep - dm->entries);
185 dns_cache_unlock (dm);
190 retry_scan (dns_main_t * dm, f64 now)
193 dns_cache_entry_t *ep;
195 for (i = 0; i < vec_len (dm->unresolved_entries); i++)
198 ep = pool_elt_at_index (dm->entries, dm->unresolved_entries[i]);
200 ASSERT ((ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) == 0);
201 vnet_send_dns_request (dm, ep);
202 dns_cache_unlock (dm);
207 dns_resolver_process (vlib_main_t * vm,
208 vlib_node_runtime_t * rt, vlib_frame_t * f)
210 dns_main_t *dm = &dns_main;
212 f64 timeout = 1000.0;
213 uword *event_data = 0;
219 vlib_process_wait_for_event_or_clock (vm, timeout);
221 now = vlib_time_now (vm);
223 event_type = vlib_process_get_events (vm, (uword **) & event_data);
227 /* Send one of these when a resolution is pending */
228 case DNS_RESOLVER_EVENT_PENDING:
232 case DNS_RESOLVER_EVENT_RESOLVED:
233 for (i = 0; i < vec_len (event_data); i++)
234 resolve_event (dm, now, (u8 *) event_data[i]);
237 case ~0: /* timeout */
238 retry_scan (dm, now);
241 vec_reset_length (event_data);
243 /* No work? Back to slow timeout mode... */
244 if (vec_len (dm->unresolved_entries) == 0)
247 return 0; /* or not */
251 VLIB_REGISTER_NODE (dns_resolver_node) =
253 .function = dns_resolver_process,
254 .type = VLIB_NODE_TYPE_PROCESS,
255 .name = "dns-resolver-process",
261 * fd.io coding-style-patch-verification: ON
264 * eval: (c-set-style "gnu")