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,
49 resolve_event (dns_main_t * dm, f64 now, u8 * reply)
51 vlib_main_t *vm = dm->vlib_main;
54 dns_cache_entry_t *ep;
61 d = (dns_header_t *) reply;
62 flags = clib_net_to_host_u16 (d->flags);
63 rcode = flags & DNS_RCODE_MASK;
65 /* $$$ u16 limits cache to 65K entries, fix later multiple dst ports */
66 pool_index = clib_net_to_host_u16 (d->id);
69 if (pool_is_free_index (dm->entries, pool_index))
72 vlib_node_increment_counter (vm, dns46_reply_node.index,
73 DNS46_REPLY_ERROR_NO_ELT, 1);
74 dns_cache_unlock (dm);
78 ep = pool_elt_at_index (dm->entries, pool_index);
81 vec_free (ep->dns_response);
83 /* Handle [sic] recursion AKA CNAME indirection */
84 if (vnet_dns_cname_indirection_nolock (dm, ep, reply))
86 dns_cache_unlock (dm);
90 /* Save the response */
91 ep->dns_response = reply;
92 /* Pick some sensible default. */
93 ep->expiration_time = now + 600.0;
94 if (vec_len (ep->dns_response))
95 ep->flags |= DNS_CACHE_ENTRY_FLAG_VALID;
97 /* Most likely, send 1 message */
98 for (i = 0; i < vec_len (ep->api_clients_to_notify); i++)
100 vl_api_registration_t *regp;
101 vl_api_dns_resolve_name_reply_t *rmp;
103 regp = vl_api_client_index_to_registration
104 (ep->api_clients_to_notify[i]);
109 rmp = vl_msg_api_alloc (sizeof (*rmp) + vec_len (ep->dns_response));
110 rmp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DNS_RESOLVE_NAME_REPLY);
111 rmp->context = ep->api_client_contexts[i];
113 rv = vnet_dns_response_to_reply (ep->dns_response, rmp, &min_ttl);
115 ep->expiration_time = now + min_ttl;
116 rmp->retval = clib_host_to_net_u32 (rv);
117 vl_msg_api_send (regp, (u8 *) rmp);
119 vec_free (ep->api_clients_to_notify);
120 vec_free (ep->api_client_contexts);
122 /* $$$ Add ip4/ip6 reply code */
124 for (i = 0; i < vec_len (dm->unresolved_entries); i++)
126 if (dm->unresolved_entries[i] == pool_index)
128 vec_delete (dm->unresolved_entries, 1, i);
132 clib_warning ("pool index %d AWOL from unresolved vector", pool_index);
135 /* Deal with bogus names, server issues, etc. */
139 case DNS_RCODE_NO_ERROR:
142 case DNS_RCODE_SERVER_FAILURE:
143 case DNS_RCODE_NOT_IMPLEMENTED:
144 case DNS_RCODE_REFUSED:
145 if (ep->server_af == 0)
146 clib_warning ("name server %U backfire",
148 dm->ip4_name_servers + ep->server_rotor);
150 clib_warning ("name server %U backfire",
152 dm->ip6_name_servers + ep->server_rotor);
154 case DNS_RCODE_NAME_ERROR:
155 case DNS_RCODE_FORMAT_ERROR:
156 /* remove trash from the cache... */
157 vnet_dns_delete_entry_by_index_nolock (dm, ep - dm->entries);
161 dns_cache_unlock (dm);
166 retry_scan (dns_main_t * dm, f64 now)
169 dns_cache_entry_t *ep;
171 for (i = 0; i < vec_len (dm->unresolved_entries); i++)
174 ep = pool_elt_at_index (dm->entries, dm->unresolved_entries[i]);
176 ASSERT ((ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) == 0);
178 vnet_send_dns_request (dm, ep);
179 dns_cache_unlock (dm);
184 dns_resolver_process (vlib_main_t * vm,
185 vlib_node_runtime_t * rt, vlib_frame_t * f)
187 dns_main_t *dm = &dns_main;
189 f64 timeout = 1000.0;
190 uword *event_data = 0;
196 vlib_process_wait_for_event_or_clock (vm, timeout);
198 now = vlib_time_now (vm);
200 event_type = vlib_process_get_events (vm, (uword **) & event_data);
204 /* Send one of these when a resolution is pending */
205 case DNS_RESOLVER_EVENT_PENDING:
209 case DNS_RESOLVER_EVENT_RESOLVED:
210 for (i = 0; i < vec_len (event_data); i++)
211 resolve_event (dm, now, (u8 *) event_data[i]);
214 case ~0: /* timeout */
215 retry_scan (dm, now);
218 vec_reset_length (event_data);
220 /* No work? Back to slow timeout mode... */
221 if (vec_len (dm->unresolved_entries) == 0)
224 return 0; /* or not */
228 VLIB_REGISTER_NODE (dns_resolver_node) =
230 .function = dns_resolver_process,
231 .type = VLIB_NODE_TYPE_PROCESS,
232 .name = "dns-resolver-process",
238 * fd.io coding-style-patch-verification: ON
241 * eval: (c-set-style "gnu")