blob: 5f43fada983a318f57eeaffb64cc3b6da4032b61 [file] [log] [blame]
Dave Barach65457162017-10-10 17:53:14 -04001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <vnet/dns/dns.h>
17#include <vlibapi/api.h>
18#include <vlibmemory/api.h>
19
20#include <vlib/vlib.h>
21#include <vnet/vnet.h>
22
23#include <vnet/vnet_msg_enum.h>
24
25#define vl_typedefs /* define message structures */
26#include <vnet/vnet_all_api_h.h>
27#undef vl_typedefs
28
29#define vl_endianfun /* define message structures */
30#include <vnet/vnet_all_api_h.h>
31#undef vl_endianfun
32
33/* instantiate all the print functions we know about */
34#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
35#define vl_printfun
36#include <vnet/vnet_all_api_h.h>
37#undef vl_printfun
38
39#include <vlibapi/api_helper_macros.h>
40
41vlib_node_registration_t dns_resolver_node;
42
43extern int
44vnet_dns_response_to_reply (u8 * response,
45 vl_api_dns_resolve_name_reply_t * rmp,
46 u32 * min_ttlp);
Dave Barachd2080152017-10-20 09:21:35 -040047extern int
48vnet_dns_response_to_name (u8 * response,
49 vl_api_dns_resolve_ip_reply_t * rmp,
50 u32 * min_ttlp);
Dave Barach65457162017-10-10 17:53:14 -040051
52static void
53resolve_event (dns_main_t * dm, f64 now, u8 * reply)
54{
55 vlib_main_t *vm = dm->vlib_main;
Dave Barach97494502017-11-04 09:44:38 -040056 dns_pending_request_t *pr;
Dave Barach65457162017-10-10 17:53:14 -040057 dns_header_t *d;
58 u32 pool_index;
59 dns_cache_entry_t *ep;
60 u32 min_ttl;
61 u16 flags;
62 u16 rcode;
63 int i;
64 int rv = 0;
65
66 d = (dns_header_t *) reply;
67 flags = clib_net_to_host_u16 (d->flags);
68 rcode = flags & DNS_RCODE_MASK;
69
70 /* $$$ u16 limits cache to 65K entries, fix later multiple dst ports */
71 pool_index = clib_net_to_host_u16 (d->id);
72 dns_cache_lock (dm);
73
74 if (pool_is_free_index (dm->entries, pool_index))
75 {
76 vec_free (reply);
77 vlib_node_increment_counter (vm, dns46_reply_node.index,
78 DNS46_REPLY_ERROR_NO_ELT, 1);
79 dns_cache_unlock (dm);
80 return;
81 }
82
83 ep = pool_elt_at_index (dm->entries, pool_index);
84
85 if (ep->dns_response)
86 vec_free (ep->dns_response);
87
88 /* Handle [sic] recursion AKA CNAME indirection */
Dave Barach580eda72018-01-09 17:00:00 -050089 rv = vnet_dns_cname_indirection_nolock (dm, pool_index, reply);
90
91 /* CNAME found, further resolution pending, we're done here */
92 if (rv > 0)
Dave Barach65457162017-10-10 17:53:14 -040093 {
94 dns_cache_unlock (dm);
95 return;
96 }
Dave Barach580eda72018-01-09 17:00:00 -050097 /* Server backfire: refused to answer, or sent zero replies */
98 if (rv < 0)
99 {
100 /* Try a different server */
101 if (ep->server_af /* ip6 */ )
102 {
103 if (0)
104 clib_warning ("Server %U failed to resolve '%s'",
105 format_ip6_address,
106 dm->ip6_name_servers + ep->server_rotor, ep->name);
107 /* Any more servers to try? */
108 if (ep->server_fails > 1 || vec_len (dm->ip6_name_servers) <= 1)
109 {
110 /* No, tell the client to go away */
111 goto reply;
112 }
113 ep->retry_count = 0;
114 ep->server_rotor++;
115 ep->server_fails++;
116 if (ep->server_rotor >= vec_len (dm->ip6_name_servers))
117 ep->server_rotor = 0;
118 if (0)
119 clib_warning ("Try server %U", format_ip6_address,
120 dm->ip6_name_servers + ep->server_rotor);
121 vnet_dns_send_dns6_request
122 (dm, ep, dm->ip6_name_servers + ep->server_rotor);
123 }
124 else
125 {
126 if (0)
127 clib_warning ("Server %U failed to resolve '%s'",
128 format_ip4_address,
129 dm->ip4_name_servers + ep->server_rotor, ep->name);
Dave Barach65457162017-10-10 17:53:14 -0400130
Dave Barach580eda72018-01-09 17:00:00 -0500131 if (ep->server_fails > 1 || vec_len (dm->ip4_name_servers) <= 1)
132 {
133 /* No, tell the client to go away */
134 goto reply;
135 }
136 ep->retry_count = 0;
137 ep->server_rotor++;
138 ep->server_fails++;
139 if (ep->server_rotor >= vec_len (dm->ip4_name_servers))
140 ep->server_rotor = 0;
141 if (0)
142 clib_warning ("Try server %U", format_ip4_address,
143 dm->ip4_name_servers + ep->server_rotor);
144 vnet_dns_send_dns4_request
145 (dm, ep, dm->ip4_name_servers + ep->server_rotor);
146 }
147 dns_cache_unlock (dm);
148 return;
149 }
150
151reply:
Dave Barach65457162017-10-10 17:53:14 -0400152 /* Save the response */
153 ep->dns_response = reply;
154 /* Pick some sensible default. */
155 ep->expiration_time = now + 600.0;
156 if (vec_len (ep->dns_response))
157 ep->flags |= DNS_CACHE_ENTRY_FLAG_VALID;
158
159 /* Most likely, send 1 message */
Dave Barach97494502017-11-04 09:44:38 -0400160 for (i = 0; i < vec_len (ep->pending_requests); i++)
Dave Barach65457162017-10-10 17:53:14 -0400161 {
162 vl_api_registration_t *regp;
Dave Barach65457162017-10-10 17:53:14 -0400163
Dave Barach97494502017-11-04 09:44:38 -0400164 pr = vec_elt_at_index (ep->pending_requests, i);
Dave Barach65457162017-10-10 17:53:14 -0400165
Dave Barach97494502017-11-04 09:44:38 -0400166 switch (pr->request_type)
167 {
168 case DNS_API_PENDING_NAME_TO_IP:
169 {
170 vl_api_dns_resolve_name_reply_t *rmp;
171 regp = vl_api_client_index_to_registration (pr->client_index);
172 if (regp == 0)
173 continue;
Dave Barach65457162017-10-10 17:53:14 -0400174
Dave Barach97494502017-11-04 09:44:38 -0400175 rmp = vl_msg_api_alloc (sizeof (*rmp));
176 rmp->_vl_msg_id =
177 clib_host_to_net_u16 (VL_API_DNS_RESOLVE_NAME_REPLY);
178 rmp->context = pr->client_context;
179 min_ttl = ~0;
180 rv = vnet_dns_response_to_reply (ep->dns_response, rmp, &min_ttl);
181 if (min_ttl != ~0)
182 ep->expiration_time = now + min_ttl;
183 rmp->retval = clib_host_to_net_u32 (rv);
Florin Corase86a8ed2018-01-05 03:20:25 -0800184 vl_api_send_msg (regp, (u8 *) rmp);
Dave Barach97494502017-11-04 09:44:38 -0400185 }
186 break;
187
188 case DNS_API_PENDING_IP_TO_NAME:
189 {
190 vl_api_dns_resolve_ip_reply_t *rmp;
191
192 regp = vl_api_client_index_to_registration (pr->client_index);
193 if (regp == 0)
194 continue;
195
196 rmp = vl_msg_api_alloc (sizeof (*rmp));
197 rmp->_vl_msg_id =
198 clib_host_to_net_u16 (VL_API_DNS_RESOLVE_IP_REPLY);
199 rmp->context = pr->client_context;
200 min_ttl = ~0;
201 rv = vnet_dns_response_to_name (ep->dns_response, rmp, &min_ttl);
202 if (min_ttl != ~0)
203 ep->expiration_time = now + min_ttl;
204 rmp->retval = clib_host_to_net_u32 (rv);
Florin Corase86a8ed2018-01-05 03:20:25 -0800205 vl_api_send_msg (regp, (u8 *) rmp);
Dave Barach97494502017-11-04 09:44:38 -0400206 }
207 break;
208
209 case DNS_PEER_PENDING_IP_TO_NAME:
210 case DNS_PEER_PENDING_NAME_TO_IP:
211 if (pr->is_ip6)
212 vnet_send_dns6_reply (dm, pr, ep, 0 /* allocate a buffer */ );
213 else
214 vnet_send_dns4_reply (dm, pr, ep, 0 /* allocate a buffer */ );
215 break;
216 default:
217 clib_warning ("request type %d unknown", pr->request_type);
218 break;
Dave Barachd2080152017-10-20 09:21:35 -0400219 }
Dave Barach65457162017-10-10 17:53:14 -0400220 }
Dave Barach97494502017-11-04 09:44:38 -0400221 vec_free (ep->pending_requests);
Dave Barach65457162017-10-10 17:53:14 -0400222
223 for (i = 0; i < vec_len (dm->unresolved_entries); i++)
224 {
225 if (dm->unresolved_entries[i] == pool_index)
226 {
227 vec_delete (dm->unresolved_entries, 1, i);
228 goto found;
229 }
230 }
231 clib_warning ("pool index %d AWOL from unresolved vector", pool_index);
232
233found:
234 /* Deal with bogus names, server issues, etc. */
235 switch (rcode)
236 {
237 default:
238 case DNS_RCODE_NO_ERROR:
239 break;
240
241 case DNS_RCODE_SERVER_FAILURE:
242 case DNS_RCODE_NOT_IMPLEMENTED:
243 case DNS_RCODE_REFUSED:
244 if (ep->server_af == 0)
245 clib_warning ("name server %U backfire",
246 format_ip4_address,
247 dm->ip4_name_servers + ep->server_rotor);
248 else
249 clib_warning ("name server %U backfire",
250 format_ip6_address,
251 dm->ip6_name_servers + ep->server_rotor);
252 /* FALLTHROUGH */
253 case DNS_RCODE_NAME_ERROR:
254 case DNS_RCODE_FORMAT_ERROR:
255 /* remove trash from the cache... */
256 vnet_dns_delete_entry_by_index_nolock (dm, ep - dm->entries);
257 break;
258 }
259
260 dns_cache_unlock (dm);
261 return;
262}
263
264static void
265retry_scan (dns_main_t * dm, f64 now)
266{
267 int i;
268 dns_cache_entry_t *ep;
269
270 for (i = 0; i < vec_len (dm->unresolved_entries); i++)
271 {
272 dns_cache_lock (dm);
273 ep = pool_elt_at_index (dm->entries, dm->unresolved_entries[i]);
274
275 ASSERT ((ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) == 0);
Dave Barach65457162017-10-10 17:53:14 -0400276 vnet_send_dns_request (dm, ep);
277 dns_cache_unlock (dm);
278 }
279}
280
281static uword
282dns_resolver_process (vlib_main_t * vm,
283 vlib_node_runtime_t * rt, vlib_frame_t * f)
284{
285 dns_main_t *dm = &dns_main;
286 f64 now;
287 f64 timeout = 1000.0;
288 uword *event_data = 0;
289 uword event_type;
290 int i;
291
292 while (1)
293 {
294 vlib_process_wait_for_event_or_clock (vm, timeout);
295
296 now = vlib_time_now (vm);
297
298 event_type = vlib_process_get_events (vm, (uword **) & event_data);
299
300 switch (event_type)
301 {
302 /* Send one of these when a resolution is pending */
303 case DNS_RESOLVER_EVENT_PENDING:
304 timeout = 2.0;
305 break;
306
307 case DNS_RESOLVER_EVENT_RESOLVED:
308 for (i = 0; i < vec_len (event_data); i++)
309 resolve_event (dm, now, (u8 *) event_data[i]);
310 break;
311
312 case ~0: /* timeout */
313 retry_scan (dm, now);
314 break;
315 }
316 vec_reset_length (event_data);
317
318 /* No work? Back to slow timeout mode... */
319 if (vec_len (dm->unresolved_entries) == 0)
320 timeout = 1000.0;
321 }
322 return 0; /* or not */
323}
324
325/* *INDENT-OFF* */
326VLIB_REGISTER_NODE (dns_resolver_node) =
327{
328 .function = dns_resolver_process,
329 .type = VLIB_NODE_TYPE_PROCESS,
330 .name = "dns-resolver-process",
331};
332/* *INDENT-ON* */
333
334
335/*
336 * fd.io coding-style-patch-verification: ON
337 *
338 * Local Variables:
339 * eval: (c-set-style "gnu")
340 * End:
341 */