FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
resolver_process.c
Go to the documentation of this file.
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 <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 /* define message IDs */
24 #include <dns/dns.api_enum.h>
25 #include <dns/dns.api_types.h>
26 
28 
29 int
30 vnet_dns_response_to_reply (u8 * response,
32  u32 * min_ttlp);
33 int
34 vnet_dns_response_to_name (u8 * response,
36  u32 * min_ttlp);
37 
38 static void
40 {
42  dns_header_t *d;
43  u32 pool_index;
45  u32 min_ttl;
46  u16 flags;
47  u16 rcode;
48  int i;
49  int entry_was_valid;
50  int remove_count;
51  int rv = 0;
52 
53  d = (dns_header_t *) reply;
54  flags = clib_net_to_host_u16 (d->flags);
55  rcode = flags & DNS_RCODE_MASK;
56 
57  /* $$$ u16 limits cache to 65K entries, fix later multiple dst ports */
58  pool_index = clib_net_to_host_u16 (d->id);
59  dns_cache_lock (dm, 10);
60 
61  if (pool_is_free_index (dm->entries, pool_index))
62  {
63  vec_free (reply);
64  if (0)
65  clib_warning ("pool index %d is free", pool_index);
67  DNS46_REPLY_ERROR_NO_ELT, 1);
68  dns_cache_unlock (dm);
69  return;
70  }
71 
72  ep = pool_elt_at_index (dm->entries, pool_index);
73 
74  if (ep->dns_response)
75  vec_free (ep->dns_response);
76 
77  /* Handle [sic] recursion AKA CNAME indirection */
78  rv = vnet_dns_cname_indirection_nolock (vm, dm, pool_index, reply);
79 
80  /* CNAME found, further resolution pending, we're done here */
81  if (rv > 0)
82  {
83  dns_cache_unlock (dm);
84  return;
85  }
86  /* Server backfire: refused to answer, or sent zero replies */
87  if (rv < 0)
88  {
89  /* Try a different server */
90  if (ep->server_af /* ip6 */ )
91  {
92  if (0)
93  clib_warning ("Server %U failed to resolve '%s'",
95  dm->ip6_name_servers + ep->server_rotor, ep->name);
96  /* Any more servers to try? */
97  if (ep->server_fails > 1 || vec_len (dm->ip6_name_servers) <= 1)
98  {
99  /* No, tell the client to go away */
100  goto reply;
101  }
102  ep->retry_count = 0;
103  ep->server_rotor++;
104  ep->server_fails++;
105  if (ep->server_rotor >= vec_len (dm->ip6_name_servers))
106  ep->server_rotor = 0;
107  if (0)
108  clib_warning ("Try server %U", format_ip6_address,
109  dm->ip6_name_servers + ep->server_rotor);
111  (vm, dm, ep, dm->ip6_name_servers + ep->server_rotor);
112  }
113  else
114  {
115  if (0)
116  clib_warning ("Server %U failed to resolve '%s'",
118  dm->ip4_name_servers + ep->server_rotor, ep->name);
119 
120  if (ep->server_fails > 1 || vec_len (dm->ip4_name_servers) <= 1)
121  {
122  /* No, tell the client to go away */
123  goto reply;
124  }
125  ep->retry_count = 0;
126  ep->server_rotor++;
127  ep->server_fails++;
128  if (ep->server_rotor >= vec_len (dm->ip4_name_servers))
129  ep->server_rotor = 0;
130  if (0)
131  clib_warning ("Try server %U", format_ip4_address,
132  dm->ip4_name_servers + ep->server_rotor);
134  (vm, dm, ep, dm->ip4_name_servers + ep->server_rotor);
135  }
136  dns_cache_unlock (dm);
137  return;
138  }
139 
140 reply:
141  /* Save the response */
142  ep->dns_response = reply;
143 
144  /*
145  * Pick a sensible default cache entry expiration time.
146  * We don't play the 10-second timeout game.
147  */
148  ep->expiration_time = now + 600.0;
149 
150  if (0)
151  clib_warning ("resolving '%s', was %s valid",
152  ep->name, (ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) ?
153  "already" : "not");
154  /*
155  * The world is a mess. A single DNS request sent to e.g. 8.8.8.8
156  * may yield multiple, subtly different responses - all with the same
157  * DNS protocol-level ID.
158  *
159  * Last response wins in terms of what ends up in the cache.
160  * First response wins in terms of the response sent to the client.
161  */
162 
163  /* Strong hint that we may not find a pending resolution entry */
164  entry_was_valid = (ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) ? 1 : 0;
165 
166  if (vec_len (ep->dns_response))
168 
169  /* Most likely, send 1 message */
170  for (i = 0; i < vec_len (ep->pending_requests); i++)
171  {
172  vl_api_registration_t *regp;
173 
174  pr = vec_elt_at_index (ep->pending_requests, i);
175 
176  switch (pr->request_type)
177  {
179  {
182  if (regp == 0)
183  continue;
184 
185  rmp = vl_msg_api_alloc (sizeof (*rmp));
186  rmp->_vl_msg_id =
187  clib_host_to_net_u16 (VL_API_DNS_RESOLVE_NAME_REPLY
188  + dm->msg_id_base);
189  rmp->context = pr->client_context;
190  min_ttl = ~0;
191  rv = vnet_dns_response_to_reply (ep->dns_response, rmp, &min_ttl);
192  if (min_ttl != ~0)
193  ep->expiration_time = now + min_ttl;
194  rmp->retval = clib_host_to_net_u32 (rv);
195  vl_api_send_msg (regp, (u8 *) rmp);
196  }
197  break;
198 
200  {
202 
204  if (regp == 0)
205  continue;
206 
207  rmp = vl_msg_api_alloc (sizeof (*rmp));
208  rmp->_vl_msg_id =
209  clib_host_to_net_u16 (VL_API_DNS_RESOLVE_IP_REPLY
210  + dm->msg_id_base);
211  rmp->context = pr->client_context;
212  min_ttl = ~0;
213  rv = vnet_dns_response_to_name (ep->dns_response, rmp, &min_ttl);
214  if (min_ttl != ~0)
215  ep->expiration_time = now + min_ttl;
216  rmp->retval = clib_host_to_net_u32 (rv);
217  vl_api_send_msg (regp, (u8 *) rmp);
218  }
219  break;
220 
223  if (pr->is_ip6)
224  vnet_send_dns6_reply (vm, dm, pr, ep, 0 /* allocate a buffer */ );
225  else
226  vnet_send_dns4_reply (vm, dm, pr, ep, 0 /* allocate a buffer */ );
227  break;
228  default:
229  clib_warning ("request type %d unknown", pr->request_type);
230  break;
231  }
232  }
234 
235  remove_count = 0;
236  for (i = 0; i < vec_len (dm->unresolved_entries); i++)
237  {
238  if (dm->unresolved_entries[i] == pool_index)
239  {
240  vec_delete (dm->unresolved_entries, 1, i);
241  remove_count++;
242  i--;
243  }
244  }
245  /* See multiple response comment above... */
246  if (remove_count == 0)
247  {
248  u32 error_code = entry_was_valid ? DNS46_REPLY_ERROR_MULTIPLE_REPLY :
249  DNS46_REPLY_ERROR_NO_UNRESOLVED_ENTRY;
250 
251  vlib_node_increment_counter (vm, dns46_reply_node.index, error_code, 1);
252  dns_cache_unlock (dm);
253  return;
254  }
255 
256  /* Deal with bogus names, server issues, etc. */
257  switch (rcode)
258  {
259  default:
260  case DNS_RCODE_NO_ERROR:
261  break;
262 
265  case DNS_RCODE_REFUSED:
266  if (ep->server_af == 0)
267  clib_warning ("name server %U can't resolve '%s'",
269  dm->ip4_name_servers + ep->server_rotor, ep->name);
270  else
271  clib_warning ("name server %U can't resolve '%s'",
273  dm->ip6_name_servers + ep->server_rotor, ep->name);
274  /* FALLTHROUGH */
277  /* remove trash from the cache... */
279  break;
280  }
281 
282 
283  dns_cache_unlock (dm);
284  return;
285 }
286 
287 static void
289 {
290  int i;
291  dns_cache_entry_t *ep;
292 
293  for (i = 0; i < vec_len (dm->unresolved_entries); i++)
294  {
295  dns_cache_lock (dm, 11);
297 
298  ASSERT ((ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) == 0);
299  vnet_send_dns_request (vm, dm, ep);
300  dns_cache_unlock (dm);
301  }
302 }
303 
304 static uword
307 {
308  dns_main_t *dm = &dns_main;
309  f64 now;
310  f64 timeout = 1000.0;
311  uword *event_data = 0;
312  uword event_type;
313  int i;
314 
315  while (1)
316  {
318 
319  now = vlib_time_now (vm);
320 
321  event_type = vlib_process_get_events (vm, (uword **) & event_data);
322 
323  switch (event_type)
324  {
325  /* Send one of these when a resolution is pending */
327  timeout = 2.0;
328  break;
329 
331  for (i = 0; i < vec_len (event_data); i++)
332  resolve_event (vm, dm, now, (u8 *) event_data[i]);
333  break;
334 
335  case ~0: /* timeout */
336  retry_scan (vm, dm, now);
337  break;
338  }
339  vec_reset_length (event_data);
340 
341  /* No work? Back to slow timeout mode... */
342  if (vec_len (dm->unresolved_entries) == 0)
343  timeout = 1000.0;
344  }
345  return 0; /* or not */
346 }
347 
348 void
350 {
351  /* Already created the resolver process? */
352  if (dm->resolver_process_node_index > 0)
353  return;
354 
355  /* No, create it now and make a note of the node index */
357  (vm, "dns-resolver-process",
358  dns_resolver_process, 16 /* log2_n_stack_bytes */ );
359 }
360 
361 /*
362  * fd.io coding-style-patch-verification: ON
363  *
364  * Local Variables:
365  * eval: (c-set-style "gnu")
366  * End:
367  */
vec_reset_length
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
Definition: vec_bootstrap.h:194
vlib.h
vl_api_client_index_to_registration
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:79
DNS_RESOLVER_EVENT_RESOLVED
#define DNS_RESOLVER_EVENT_RESOLVED
Definition: dns.h:92
api.h
vl_api_dns_resolve_ip_reply_t::context
u32 context
Definition: dns.api:103
DNS_RCODE_REFUSED
#define DNS_RCODE_REFUSED
Definition: dns_packet.h:40
dns_main_t::entries
dns_cache_entry_t * entries
Pool of cache entries.
Definition: dns.h:99
dns_cache_entry_t
Definition: dns.h:55
format_ip4_address
format_function_t format_ip4_address
Definition: format.h:73
dns_main_t::msg_id_base
u16 msg_id_base
message-ID base
Definition: dns.h:128
DNS_API_PENDING_IP_TO_NAME
@ DNS_API_PENDING_IP_TO_NAME
Definition: dns.h:50
vl_api_send_msg
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
dns_cache_lock
static void dns_cache_lock(dns_main_t *dm, int tag)
Definition: dns.h:209
DNS_RCODE_NOT_IMPLEMENTED
#define DNS_RCODE_NOT_IMPLEMENTED
Definition: dns_packet.h:39
DNS_RCODE_NAME_ERROR
#define DNS_RCODE_NAME_ERROR
Definition: dns_packet.h:38
vl_api_dns_resolve_name_reply_t
DNS name resolution reply.
Definition: dns.api:71
f
vlib_frame_t * f
Definition: interface_output.c:1098
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:549
vl_api_dns_resolve_name_reply_t::retval
i32 retval
Definition: dns.api:73
vnet_dns_send_dns6_request
void vnet_dns_send_dns6_request(vlib_main_t *vm, dns_main_t *dm, dns_cache_entry_t *ep, ip6_address_t *server)
Definition: dns.c:288
DNS_API_PENDING_NAME_TO_IP
@ DNS_API_PENDING_NAME_TO_IP
Definition: dns.h:49
u16
unsigned short u16
Definition: types.h:57
dns_cache_entry_t::flags
volatile u8 flags
flags
Definition: dns.h:58
dns_main
dns_main_t dns_main
Definition: dns.c:41
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
api.h
DNS_RCODE_NO_ERROR
#define DNS_RCODE_NO_ERROR
Definition: dns_packet.h:35
vec_delete
#define vec_delete(V, N, M)
Delete N elements starting at element M.
Definition: vec.h:875
vl_api_dns_resolve_ip_reply_t::retval
i32 retval
Definition: dns.api:104
dns_cache_unlock
static void dns_cache_unlock(dns_main_t *dm)
Definition: dns.h:221
vlib_frame_t
Definition: node.h:372
dns_main_t::ip4_name_servers
ip4_address_t * ip4_name_servers
upstream name servers, e.g.
Definition: dns.h:116
dns_cache_entry_t::server_rotor
int server_rotor
Definition: dns.h:74
vnet_dns_cname_indirection_nolock
int vnet_dns_cname_indirection_nolock(vlib_main_t *vm, dns_main_t *dm, u32 ep_index, u8 *reply)
Handle cname indirection.
Definition: dns.c:876
pool_is_free_index
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:302
dns_cache_entry_t::pending_requests
dns_pending_request_t * pending_requests
Clients / peers awaiting responses.
Definition: dns.h:83
vnet_dns_create_resolver_process
void vnet_dns_create_resolver_process(vlib_main_t *vm, dns_main_t *dm)
Definition: resolver_process.c:349
dns_main_t::ip6_name_servers
ip6_address_t * ip6_name_servers
Definition: dns.h:117
vlib_process_get_events
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type,...
Definition: node_funcs.h:583
vnet_send_dns_request
void vnet_send_dns_request(vlib_main_t *vm, dns_main_t *dm, dns_cache_entry_t *ep)
Definition: dns.c:447
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
dns_cache_entry_t::name
u8 * name
The name in "normal human being" notation, e.g.
Definition: dns.h:61
dns_pending_request_t::client_index
u32 client_index
Definition: dns.h:32
vnet_send_dns6_reply
void vnet_send_dns6_reply(vlib_main_t *vm, dns_main_t *dm, dns_pending_request_t *pr, dns_cache_entry_t *ep, vlib_buffer_t *b0)
Definition: dns.c:2648
vec_elt_at_index
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Definition: vec_bootstrap.h:203
vnet_dns_send_dns4_request
void vnet_dns_send_dns4_request(vlib_main_t *vm, dns_main_t *dm, dns_cache_entry_t *ep, ip4_address_t *server)
Definition: dns.c:220
vlib_process_create
u32 vlib_process_create(vlib_main_t *vm, char *name, vlib_node_function_t *f, u32 log2_n_stack_bytes)
Create a vlib process.
Definition: node.c:795
vl_api_registration_
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
vl_api_dns_resolve_name_reply_t::context
u32 context
Definition: dns.api:72
vnet_send_dns4_reply
void vnet_send_dns4_reply(vlib_main_t *vm, dns_main_t *dm, dns_pending_request_t *pr, dns_cache_entry_t *ep, vlib_buffer_t *b0)
Definition: dns.c:2657
dns_pending_request_t::client_context
u32 client_context
Definition: dns.h:33
uword
u64 uword
Definition: types.h:112
vlib_node_increment_counter
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1244
f64
double f64
Definition: types.h:142
dns_cache_entry_t::expiration_time
f64 expiration_time
Expiration time.
Definition: dns.h:67
dns_pending_request_t::request_type
u32 request_type
Definition: dns.h:31
DNS_PEER_PENDING_IP_TO_NAME
@ DNS_PEER_PENDING_IP_TO_NAME
Definition: dns.h:52
dns46_reply_node
vlib_node_registration_t dns46_reply_node
(constructor) VLIB_REGISTER_NODE (dns46_reply_node)
Definition: reply_node.c:42
dns_cache_entry_t::dns_response
u8 * dns_response
Cached dns response.
Definition: dns.h:80
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
vlib_process_wait_for_event_or_clock
static f64 vlib_process_wait_for_event_or_clock(vlib_main_t *vm, f64 dt)
Suspend a cooperative multi-tasking thread Waits for an event, or for the indicated number of seconds...
Definition: node_funcs.h:755
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
u32
unsigned int u32
Definition: types.h:88
api_helper_macros.h
for
for(i=1;i<=collision_buckets;i++)
Definition: flowhash_template.h:378
now
f64 now
Definition: nat44_ei_out2in.c:710
dns_pending_request_t::is_ip6
u8 is_ip6
Definition: dns.h:34
vlib_main_t
Definition: main.h:102
resolve_event
static void resolve_event(vlib_main_t *vm, dns_main_t *dm, f64 now, u8 *reply)
Definition: resolver_process.c:39
DNS_PEER_PENDING_NAME_TO_IP
@ DNS_PEER_PENDING_NAME_TO_IP
Definition: dns.h:51
u8
unsigned char u8
Definition: types.h:56
rt
vnet_interface_output_runtime_t * rt
Definition: interface_output.c:419
format_ip6_address
format_function_t format_ip6_address
Definition: format.h:91
i
int i
Definition: flowhash_template.h:376
DNS_RESOLVER_EVENT_PENDING
#define DNS_RESOLVER_EVENT_PENDING
Definition: dns.h:93
vl_api_dns_resolve_ip_reply_t
DNS ip->name resolution reply.
Definition: dns.api:102
clib_warning
#define clib_warning(format, args...)
Definition: error.h:59
DNS_RCODE_MASK
#define DNS_RCODE_MASK
Definition: dns_packet.h:34
DNS_RCODE_FORMAT_ERROR
#define DNS_RCODE_FORMAT_ERROR
Definition: dns_packet.h:36
rv
int __clib_unused rv
Definition: application.c:491
dns_main_t::resolver_process_node_index
u32 resolver_process_node_index
resolver process node index
Definition: dns.h:120
dns_main_t::unresolved_entries
u32 * unresolved_entries
Pool indices of unresolved entries.
Definition: dns.h:102
retry_scan
static void retry_scan(vlib_main_t *vm, dns_main_t *dm, f64 now)
Definition: resolver_process.c:288
dns_pending_request_t
Definition: dns.h:29
vlib_time_now
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:327
vnet_dns_delete_entry_by_index_nolock
int vnet_dns_delete_entry_by_index_nolock(dns_main_t *dm, u32 index)
Definition: dns.c:572
vnet.h
dns.h
vlib_node_runtime_t
Definition: node.h:454
dns_main_t
Definition: dns.h:96
DNS_RCODE_SERVER_FAILURE
#define DNS_RCODE_SERVER_FAILURE
Definition: dns_packet.h:37
vnet_dns_response_to_reply
int vnet_dns_response_to_reply(u8 *response, vl_api_dns_resolve_name_reply_t *rmp, u32 *min_ttlp)
dns_cache_entry_t::server_af
int server_af
Definition: dns.h:75
dns_cache_entry_t::retry_count
int retry_count
Retry parameters.
Definition: dns.h:73
dns_cache_entry_t::server_fails
int server_fails
Definition: dns.h:76
DNS_CACHE_ENTRY_FLAG_VALID
#define DNS_CACHE_ENTRY_FLAG_VALID
we have Actual Data
Definition: dns.h:86
vl_msg_api_alloc
void * vl_msg_api_alloc(int nbytes)
Definition: memory_shared.c:199
dns_resolver_process
static uword dns_resolver_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: resolver_process.c:305
vnet_dns_response_to_name
int vnet_dns_response_to_name(u8 *response, vl_api_dns_resolve_ip_reply_t *rmp, u32 *min_ttlp)
Definition: dns.c:1212
flags
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105