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;
56 dns_pending_request_t *pr;
59 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))
77 vlib_node_increment_counter (vm, dns46_reply_node.index,
78 DNS46_REPLY_ERROR_NO_ELT, 1);
79 dns_cache_unlock (dm);
83 ep = pool_elt_at_index (dm->entries, pool_index);
86 vec_free (ep->dns_response);
88 /* Handle [sic] recursion AKA CNAME indirection */
89 if (vnet_dns_cname_indirection_nolock (dm, pool_index, reply))
91 dns_cache_unlock (dm);
95 /* Save the response */
96 ep->dns_response = reply;
97 /* Pick some sensible default. */
98 ep->expiration_time = now + 600.0;
99 if (vec_len (ep->dns_response))
100 ep->flags |= DNS_CACHE_ENTRY_FLAG_VALID;
102 /* Most likely, send 1 message */
103 for (i = 0; i < vec_len (ep->pending_requests); i++)
105 vl_api_registration_t *regp;
107 pr = vec_elt_at_index (ep->pending_requests, i);
109 switch (pr->request_type)
111 case DNS_API_PENDING_NAME_TO_IP:
113 vl_api_dns_resolve_name_reply_t *rmp;
114 regp = vl_api_client_index_to_registration (pr->client_index);
118 rmp = vl_msg_api_alloc (sizeof (*rmp));
120 clib_host_to_net_u16 (VL_API_DNS_RESOLVE_NAME_REPLY);
121 rmp->context = pr->client_context;
123 rv = vnet_dns_response_to_reply (ep->dns_response, rmp, &min_ttl);
125 ep->expiration_time = now + min_ttl;
126 rmp->retval = clib_host_to_net_u32 (rv);
127 vl_msg_api_send (regp, (u8 *) rmp);
131 case DNS_API_PENDING_IP_TO_NAME:
133 vl_api_dns_resolve_ip_reply_t *rmp;
135 regp = vl_api_client_index_to_registration (pr->client_index);
139 rmp = vl_msg_api_alloc (sizeof (*rmp));
141 clib_host_to_net_u16 (VL_API_DNS_RESOLVE_IP_REPLY);
142 rmp->context = pr->client_context;
144 rv = vnet_dns_response_to_name (ep->dns_response, rmp, &min_ttl);
146 ep->expiration_time = now + min_ttl;
147 rmp->retval = clib_host_to_net_u32 (rv);
148 vl_msg_api_send (regp, (u8 *) rmp);
152 case DNS_PEER_PENDING_IP_TO_NAME:
153 case DNS_PEER_PENDING_NAME_TO_IP:
155 vnet_send_dns6_reply (dm, pr, ep, 0 /* allocate a buffer */ );
157 vnet_send_dns4_reply (dm, pr, ep, 0 /* allocate a buffer */ );
160 clib_warning ("request type %d unknown", pr->request_type);
164 vec_free (ep->pending_requests);
166 for (i = 0; i < vec_len (dm->unresolved_entries); i++)
168 if (dm->unresolved_entries[i] == pool_index)
170 vec_delete (dm->unresolved_entries, 1, i);
174 clib_warning ("pool index %d AWOL from unresolved vector", pool_index);
177 /* Deal with bogus names, server issues, etc. */
181 case DNS_RCODE_NO_ERROR:
184 case DNS_RCODE_SERVER_FAILURE:
185 case DNS_RCODE_NOT_IMPLEMENTED:
186 case DNS_RCODE_REFUSED:
187 if (ep->server_af == 0)
188 clib_warning ("name server %U backfire",
190 dm->ip4_name_servers + ep->server_rotor);
192 clib_warning ("name server %U backfire",
194 dm->ip6_name_servers + ep->server_rotor);
196 case DNS_RCODE_NAME_ERROR:
197 case DNS_RCODE_FORMAT_ERROR:
198 /* remove trash from the cache... */
199 vnet_dns_delete_entry_by_index_nolock (dm, ep - dm->entries);
203 dns_cache_unlock (dm);
208 retry_scan (dns_main_t * dm, f64 now)
211 dns_cache_entry_t *ep;
213 for (i = 0; i < vec_len (dm->unresolved_entries); i++)
216 ep = pool_elt_at_index (dm->entries, dm->unresolved_entries[i]);
218 ASSERT ((ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) == 0);
219 vnet_send_dns_request (dm, ep);
220 dns_cache_unlock (dm);
225 dns_resolver_process (vlib_main_t * vm,
226 vlib_node_runtime_t * rt, vlib_frame_t * f)
228 dns_main_t *dm = &dns_main;
230 f64 timeout = 1000.0;
231 uword *event_data = 0;
237 vlib_process_wait_for_event_or_clock (vm, timeout);
239 now = vlib_time_now (vm);
241 event_type = vlib_process_get_events (vm, (uword **) & event_data);
245 /* Send one of these when a resolution is pending */
246 case DNS_RESOLVER_EVENT_PENDING:
250 case DNS_RESOLVER_EVENT_RESOLVED:
251 for (i = 0; i < vec_len (event_data); i++)
252 resolve_event (dm, now, (u8 *) event_data[i]);
255 case ~0: /* timeout */
256 retry_scan (dm, now);
259 vec_reset_length (event_data);
261 /* No work? Back to slow timeout mode... */
262 if (vec_len (dm->unresolved_entries) == 0)
265 return 0; /* or not */
269 VLIB_REGISTER_NODE (dns_resolver_node) =
271 .function = dns_resolver_process,
272 .type = VLIB_NODE_TYPE_PROCESS,
273 .name = "dns-resolver-process",
279 * fd.io coding-style-patch-verification: ON
282 * eval: (c-set-style "gnu")