4ed7c799ce2be0dfa3e3e0349b15c33119754604
[vpp.git] / src / vnet / dns / resolver_process.c
1 /*
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
41 extern int
42 vnet_dns_response_to_reply (u8 * response,
43                             vl_api_dns_resolve_name_reply_t * rmp,
44                             u32 * min_ttlp);
45 extern int
46 vnet_dns_response_to_name (u8 * response,
47                            vl_api_dns_resolve_ip_reply_t * rmp,
48                            u32 * min_ttlp);
49
50 static void
51 resolve_event (dns_main_t * dm, f64 now, u8 * reply)
52 {
53   vlib_main_t *vm = dm->vlib_main;
54   dns_pending_request_t *pr;
55   dns_header_t *d;
56   u32 pool_index;
57   dns_cache_entry_t *ep;
58   u32 min_ttl;
59   u16 flags;
60   u16 rcode;
61   int i;
62   int entry_was_valid;
63   int remove_count;
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       if (0)
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);
82       return;
83     }
84
85   ep = pool_elt_at_index (dm->entries, pool_index);
86
87   if (ep->dns_response)
88     vec_free (ep->dns_response);
89
90   /* Handle [sic] recursion AKA CNAME indirection */
91   rv = vnet_dns_cname_indirection_nolock (dm, pool_index, reply);
92
93   /* CNAME found, further resolution pending, we're done here */
94   if (rv > 0)
95     {
96       dns_cache_unlock (dm);
97       return;
98     }
99   /* Server backfire: refused to answer, or sent zero replies */
100   if (rv < 0)
101     {
102       /* Try a different server */
103       if (ep->server_af /* ip6 */ )
104         {
105           if (0)
106             clib_warning ("Server %U failed to resolve '%s'",
107                           format_ip6_address,
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)
111             {
112               /* No, tell the client to go away */
113               goto reply;
114             }
115           ep->retry_count = 0;
116           ep->server_rotor++;
117           ep->server_fails++;
118           if (ep->server_rotor >= vec_len (dm->ip6_name_servers))
119             ep->server_rotor = 0;
120           if (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);
125         }
126       else
127         {
128           if (0)
129             clib_warning ("Server %U failed to resolve '%s'",
130                           format_ip4_address,
131                           dm->ip4_name_servers + ep->server_rotor, ep->name);
132
133           if (ep->server_fails > 1 || vec_len (dm->ip4_name_servers) <= 1)
134             {
135               /* No, tell the client to go away */
136               goto reply;
137             }
138           ep->retry_count = 0;
139           ep->server_rotor++;
140           ep->server_fails++;
141           if (ep->server_rotor >= vec_len (dm->ip4_name_servers))
142             ep->server_rotor = 0;
143           if (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);
148         }
149       dns_cache_unlock (dm);
150       return;
151     }
152
153 reply:
154   /* Save the response */
155   ep->dns_response = reply;
156
157   /*
158    * Pick a sensible default cache entry expiration time.
159    * We don't play the 10-second timeout game.
160    */
161   ep->expiration_time = now + 600.0;
162
163   if (0)
164     clib_warning ("resolving '%s', was %s valid",
165                   ep->name, (ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) ?
166                   "already" : "not");
167   /*
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.
171    *
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.
174    */
175
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;
178
179   if (vec_len (ep->dns_response))
180     ep->flags |= DNS_CACHE_ENTRY_FLAG_VALID;
181
182   /* Most likely, send 1 message */
183   for (i = 0; i < vec_len (ep->pending_requests); i++)
184     {
185       vl_api_registration_t *regp;
186
187       pr = vec_elt_at_index (ep->pending_requests, i);
188
189       switch (pr->request_type)
190         {
191         case DNS_API_PENDING_NAME_TO_IP:
192           {
193             vl_api_dns_resolve_name_reply_t *rmp;
194             regp = vl_api_client_index_to_registration (pr->client_index);
195             if (regp == 0)
196               continue;
197
198             rmp = vl_msg_api_alloc (sizeof (*rmp));
199             rmp->_vl_msg_id =
200               clib_host_to_net_u16 (VL_API_DNS_RESOLVE_NAME_REPLY);
201             rmp->context = pr->client_context;
202             min_ttl = ~0;
203             rv = vnet_dns_response_to_reply (ep->dns_response, rmp, &min_ttl);
204             if (min_ttl != ~0)
205               ep->expiration_time = now + min_ttl;
206             rmp->retval = clib_host_to_net_u32 (rv);
207             vl_api_send_msg (regp, (u8 *) rmp);
208           }
209           break;
210
211         case DNS_API_PENDING_IP_TO_NAME:
212           {
213             vl_api_dns_resolve_ip_reply_t *rmp;
214
215             regp = vl_api_client_index_to_registration (pr->client_index);
216             if (regp == 0)
217               continue;
218
219             rmp = vl_msg_api_alloc (sizeof (*rmp));
220             rmp->_vl_msg_id =
221               clib_host_to_net_u16 (VL_API_DNS_RESOLVE_IP_REPLY);
222             rmp->context = pr->client_context;
223             min_ttl = ~0;
224             rv = vnet_dns_response_to_name (ep->dns_response, rmp, &min_ttl);
225             if (min_ttl != ~0)
226               ep->expiration_time = now + min_ttl;
227             rmp->retval = clib_host_to_net_u32 (rv);
228             vl_api_send_msg (regp, (u8 *) rmp);
229           }
230           break;
231
232         case DNS_PEER_PENDING_IP_TO_NAME:
233         case DNS_PEER_PENDING_NAME_TO_IP:
234           if (pr->is_ip6)
235             vnet_send_dns6_reply (dm, pr, ep, 0 /* allocate a buffer */ );
236           else
237             vnet_send_dns4_reply (dm, pr, ep, 0 /* allocate a buffer */ );
238           break;
239         default:
240           clib_warning ("request type %d unknown", pr->request_type);
241           break;
242         }
243     }
244   vec_free (ep->pending_requests);
245
246   remove_count = 0;
247   for (i = 0; i < vec_len (dm->unresolved_entries); i++)
248     {
249       if (dm->unresolved_entries[i] == pool_index)
250         {
251           vec_delete (dm->unresolved_entries, 1, i);
252           remove_count++;
253           i--;
254         }
255     }
256   /* See multiple response comment above... */
257   if (remove_count == 0)
258     {
259       u32 error_code = entry_was_valid ? DNS46_REPLY_ERROR_MULTIPLE_REPLY :
260         DNS46_REPLY_ERROR_NO_UNRESOLVED_ENTRY;
261
262       vlib_node_increment_counter (vm, dns46_reply_node.index, error_code, 1);
263       dns_cache_unlock (dm);
264       return;
265     }
266
267   /* Deal with bogus names, server issues, etc. */
268   switch (rcode)
269     {
270     default:
271     case DNS_RCODE_NO_ERROR:
272       break;
273
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'",
279                       format_ip4_address,
280                       dm->ip4_name_servers + ep->server_rotor, ep->name);
281       else
282         clib_warning ("name server %U can't resolve '%s'",
283                       format_ip6_address,
284                       dm->ip6_name_servers + ep->server_rotor, ep->name);
285       /* FALLTHROUGH */
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);
290       break;
291     }
292
293
294   dns_cache_unlock (dm);
295   return;
296 }
297
298 static void
299 retry_scan (dns_main_t * dm, f64 now)
300 {
301   int i;
302   dns_cache_entry_t *ep;
303
304   for (i = 0; i < vec_len (dm->unresolved_entries); i++)
305     {
306       dns_cache_lock (dm);
307       ep = pool_elt_at_index (dm->entries, dm->unresolved_entries[i]);
308
309       ASSERT ((ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) == 0);
310       vnet_send_dns_request (dm, ep);
311       dns_cache_unlock (dm);
312     }
313 }
314
315 static uword
316 dns_resolver_process (vlib_main_t * vm,
317                       vlib_node_runtime_t * rt, vlib_frame_t * f)
318 {
319   dns_main_t *dm = &dns_main;
320   f64 now;
321   f64 timeout = 1000.0;
322   uword *event_data = 0;
323   uword event_type;
324   int i;
325
326   while (1)
327     {
328       vlib_process_wait_for_event_or_clock (vm, timeout);
329
330       now = vlib_time_now (vm);
331
332       event_type = vlib_process_get_events (vm, (uword **) & event_data);
333
334       switch (event_type)
335         {
336           /* Send one of these when a resolution is pending */
337         case DNS_RESOLVER_EVENT_PENDING:
338           timeout = 2.0;
339           break;
340
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]);
344           break;
345
346         case ~0:                /* timeout */
347           retry_scan (dm, now);
348           break;
349         }
350       vec_reset_length (event_data);
351
352       /* No work? Back to slow timeout mode... */
353       if (vec_len (dm->unresolved_entries) == 0)
354         timeout = 1000.0;
355     }
356   return 0;                     /* or not */
357 }
358
359 void
360 vnet_dns_create_resolver_process (dns_main_t * dm)
361 {
362   /* Already created the resolver process? */
363   if (dm->resolver_process_node_index > 0)
364     return;
365
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 */ );
370 }
371
372 /*
373  * fd.io coding-style-patch-verification: ON
374  *
375  * Local Variables:
376  * eval: (c-set-style "gnu")
377  * End:
378  */