Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 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 | |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 41 | extern int |
| 42 | vnet_dns_response_to_reply (u8 * response, |
| 43 | vl_api_dns_resolve_name_reply_t * rmp, |
| 44 | u32 * min_ttlp); |
Dave Barach | d208015 | 2017-10-20 09:21:35 -0400 | [diff] [blame] | 45 | extern int |
| 46 | vnet_dns_response_to_name (u8 * response, |
| 47 | vl_api_dns_resolve_ip_reply_t * rmp, |
| 48 | u32 * min_ttlp); |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 49 | |
| 50 | static void |
| 51 | resolve_event (dns_main_t * dm, f64 now, u8 * reply) |
| 52 | { |
| 53 | vlib_main_t *vm = dm->vlib_main; |
Dave Barach | 9749450 | 2017-11-04 09:44:38 -0400 | [diff] [blame] | 54 | dns_pending_request_t *pr; |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 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 rv = 0; |
| 63 | |
| 64 | d = (dns_header_t *) reply; |
| 65 | flags = clib_net_to_host_u16 (d->flags); |
| 66 | rcode = flags & DNS_RCODE_MASK; |
| 67 | |
| 68 | /* $$$ u16 limits cache to 65K entries, fix later multiple dst ports */ |
| 69 | pool_index = clib_net_to_host_u16 (d->id); |
| 70 | dns_cache_lock (dm); |
| 71 | |
| 72 | if (pool_is_free_index (dm->entries, pool_index)) |
| 73 | { |
| 74 | vec_free (reply); |
| 75 | vlib_node_increment_counter (vm, dns46_reply_node.index, |
| 76 | DNS46_REPLY_ERROR_NO_ELT, 1); |
| 77 | dns_cache_unlock (dm); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | ep = pool_elt_at_index (dm->entries, pool_index); |
| 82 | |
| 83 | if (ep->dns_response) |
| 84 | vec_free (ep->dns_response); |
| 85 | |
| 86 | /* Handle [sic] recursion AKA CNAME indirection */ |
Dave Barach | 580eda7 | 2018-01-09 17:00:00 -0500 | [diff] [blame] | 87 | rv = vnet_dns_cname_indirection_nolock (dm, pool_index, reply); |
| 88 | |
| 89 | /* CNAME found, further resolution pending, we're done here */ |
| 90 | if (rv > 0) |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 91 | { |
| 92 | dns_cache_unlock (dm); |
| 93 | return; |
| 94 | } |
Dave Barach | 580eda7 | 2018-01-09 17:00:00 -0500 | [diff] [blame] | 95 | /* Server backfire: refused to answer, or sent zero replies */ |
| 96 | if (rv < 0) |
| 97 | { |
| 98 | /* Try a different server */ |
| 99 | if (ep->server_af /* ip6 */ ) |
| 100 | { |
| 101 | if (0) |
| 102 | clib_warning ("Server %U failed to resolve '%s'", |
| 103 | format_ip6_address, |
| 104 | dm->ip6_name_servers + ep->server_rotor, ep->name); |
| 105 | /* Any more servers to try? */ |
| 106 | if (ep->server_fails > 1 || vec_len (dm->ip6_name_servers) <= 1) |
| 107 | { |
| 108 | /* No, tell the client to go away */ |
| 109 | goto reply; |
| 110 | } |
| 111 | ep->retry_count = 0; |
| 112 | ep->server_rotor++; |
| 113 | ep->server_fails++; |
| 114 | if (ep->server_rotor >= vec_len (dm->ip6_name_servers)) |
| 115 | ep->server_rotor = 0; |
| 116 | if (0) |
| 117 | clib_warning ("Try server %U", format_ip6_address, |
| 118 | dm->ip6_name_servers + ep->server_rotor); |
| 119 | vnet_dns_send_dns6_request |
| 120 | (dm, ep, dm->ip6_name_servers + ep->server_rotor); |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | if (0) |
| 125 | clib_warning ("Server %U failed to resolve '%s'", |
| 126 | format_ip4_address, |
| 127 | dm->ip4_name_servers + ep->server_rotor, ep->name); |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 128 | |
Dave Barach | 580eda7 | 2018-01-09 17:00:00 -0500 | [diff] [blame] | 129 | if (ep->server_fails > 1 || vec_len (dm->ip4_name_servers) <= 1) |
| 130 | { |
| 131 | /* No, tell the client to go away */ |
| 132 | goto reply; |
| 133 | } |
| 134 | ep->retry_count = 0; |
| 135 | ep->server_rotor++; |
| 136 | ep->server_fails++; |
| 137 | if (ep->server_rotor >= vec_len (dm->ip4_name_servers)) |
| 138 | ep->server_rotor = 0; |
| 139 | if (0) |
| 140 | clib_warning ("Try server %U", format_ip4_address, |
| 141 | dm->ip4_name_servers + ep->server_rotor); |
| 142 | vnet_dns_send_dns4_request |
| 143 | (dm, ep, dm->ip4_name_servers + ep->server_rotor); |
| 144 | } |
| 145 | dns_cache_unlock (dm); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | reply: |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 150 | /* Save the response */ |
| 151 | ep->dns_response = reply; |
| 152 | /* Pick some sensible default. */ |
| 153 | ep->expiration_time = now + 600.0; |
| 154 | if (vec_len (ep->dns_response)) |
| 155 | ep->flags |= DNS_CACHE_ENTRY_FLAG_VALID; |
| 156 | |
| 157 | /* Most likely, send 1 message */ |
Dave Barach | 9749450 | 2017-11-04 09:44:38 -0400 | [diff] [blame] | 158 | for (i = 0; i < vec_len (ep->pending_requests); i++) |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 159 | { |
| 160 | vl_api_registration_t *regp; |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 161 | |
Dave Barach | 9749450 | 2017-11-04 09:44:38 -0400 | [diff] [blame] | 162 | pr = vec_elt_at_index (ep->pending_requests, i); |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 163 | |
Dave Barach | 9749450 | 2017-11-04 09:44:38 -0400 | [diff] [blame] | 164 | switch (pr->request_type) |
| 165 | { |
| 166 | case DNS_API_PENDING_NAME_TO_IP: |
| 167 | { |
| 168 | vl_api_dns_resolve_name_reply_t *rmp; |
| 169 | regp = vl_api_client_index_to_registration (pr->client_index); |
| 170 | if (regp == 0) |
| 171 | continue; |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 172 | |
Dave Barach | 9749450 | 2017-11-04 09:44:38 -0400 | [diff] [blame] | 173 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 174 | rmp->_vl_msg_id = |
| 175 | clib_host_to_net_u16 (VL_API_DNS_RESOLVE_NAME_REPLY); |
| 176 | rmp->context = pr->client_context; |
| 177 | min_ttl = ~0; |
| 178 | rv = vnet_dns_response_to_reply (ep->dns_response, rmp, &min_ttl); |
| 179 | if (min_ttl != ~0) |
| 180 | ep->expiration_time = now + min_ttl; |
| 181 | rmp->retval = clib_host_to_net_u32 (rv); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 182 | vl_api_send_msg (regp, (u8 *) rmp); |
Dave Barach | 9749450 | 2017-11-04 09:44:38 -0400 | [diff] [blame] | 183 | } |
| 184 | break; |
| 185 | |
| 186 | case DNS_API_PENDING_IP_TO_NAME: |
| 187 | { |
| 188 | vl_api_dns_resolve_ip_reply_t *rmp; |
| 189 | |
| 190 | regp = vl_api_client_index_to_registration (pr->client_index); |
| 191 | if (regp == 0) |
| 192 | continue; |
| 193 | |
| 194 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 195 | rmp->_vl_msg_id = |
| 196 | clib_host_to_net_u16 (VL_API_DNS_RESOLVE_IP_REPLY); |
| 197 | rmp->context = pr->client_context; |
| 198 | min_ttl = ~0; |
| 199 | rv = vnet_dns_response_to_name (ep->dns_response, rmp, &min_ttl); |
| 200 | if (min_ttl != ~0) |
| 201 | ep->expiration_time = now + min_ttl; |
| 202 | rmp->retval = clib_host_to_net_u32 (rv); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 203 | vl_api_send_msg (regp, (u8 *) rmp); |
Dave Barach | 9749450 | 2017-11-04 09:44:38 -0400 | [diff] [blame] | 204 | } |
| 205 | break; |
| 206 | |
| 207 | case DNS_PEER_PENDING_IP_TO_NAME: |
| 208 | case DNS_PEER_PENDING_NAME_TO_IP: |
| 209 | if (pr->is_ip6) |
| 210 | vnet_send_dns6_reply (dm, pr, ep, 0 /* allocate a buffer */ ); |
| 211 | else |
| 212 | vnet_send_dns4_reply (dm, pr, ep, 0 /* allocate a buffer */ ); |
| 213 | break; |
| 214 | default: |
| 215 | clib_warning ("request type %d unknown", pr->request_type); |
| 216 | break; |
Dave Barach | d208015 | 2017-10-20 09:21:35 -0400 | [diff] [blame] | 217 | } |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 218 | } |
Dave Barach | 9749450 | 2017-11-04 09:44:38 -0400 | [diff] [blame] | 219 | vec_free (ep->pending_requests); |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 220 | |
| 221 | for (i = 0; i < vec_len (dm->unresolved_entries); i++) |
| 222 | { |
| 223 | if (dm->unresolved_entries[i] == pool_index) |
| 224 | { |
| 225 | vec_delete (dm->unresolved_entries, 1, i); |
| 226 | goto found; |
| 227 | } |
| 228 | } |
| 229 | clib_warning ("pool index %d AWOL from unresolved vector", pool_index); |
| 230 | |
| 231 | found: |
| 232 | /* Deal with bogus names, server issues, etc. */ |
| 233 | switch (rcode) |
| 234 | { |
| 235 | default: |
| 236 | case DNS_RCODE_NO_ERROR: |
| 237 | break; |
| 238 | |
| 239 | case DNS_RCODE_SERVER_FAILURE: |
| 240 | case DNS_RCODE_NOT_IMPLEMENTED: |
| 241 | case DNS_RCODE_REFUSED: |
| 242 | if (ep->server_af == 0) |
| 243 | clib_warning ("name server %U backfire", |
| 244 | format_ip4_address, |
| 245 | dm->ip4_name_servers + ep->server_rotor); |
| 246 | else |
| 247 | clib_warning ("name server %U backfire", |
| 248 | format_ip6_address, |
| 249 | dm->ip6_name_servers + ep->server_rotor); |
| 250 | /* FALLTHROUGH */ |
| 251 | case DNS_RCODE_NAME_ERROR: |
| 252 | case DNS_RCODE_FORMAT_ERROR: |
| 253 | /* remove trash from the cache... */ |
| 254 | vnet_dns_delete_entry_by_index_nolock (dm, ep - dm->entries); |
| 255 | break; |
| 256 | } |
| 257 | |
| 258 | dns_cache_unlock (dm); |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | static void |
| 263 | retry_scan (dns_main_t * dm, f64 now) |
| 264 | { |
| 265 | int i; |
| 266 | dns_cache_entry_t *ep; |
| 267 | |
| 268 | for (i = 0; i < vec_len (dm->unresolved_entries); i++) |
| 269 | { |
| 270 | dns_cache_lock (dm); |
| 271 | ep = pool_elt_at_index (dm->entries, dm->unresolved_entries[i]); |
| 272 | |
| 273 | ASSERT ((ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) == 0); |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 274 | vnet_send_dns_request (dm, ep); |
| 275 | dns_cache_unlock (dm); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | static uword |
| 280 | dns_resolver_process (vlib_main_t * vm, |
| 281 | vlib_node_runtime_t * rt, vlib_frame_t * f) |
| 282 | { |
| 283 | dns_main_t *dm = &dns_main; |
| 284 | f64 now; |
| 285 | f64 timeout = 1000.0; |
| 286 | uword *event_data = 0; |
| 287 | uword event_type; |
| 288 | int i; |
| 289 | |
| 290 | while (1) |
| 291 | { |
| 292 | vlib_process_wait_for_event_or_clock (vm, timeout); |
| 293 | |
| 294 | now = vlib_time_now (vm); |
| 295 | |
| 296 | event_type = vlib_process_get_events (vm, (uword **) & event_data); |
| 297 | |
| 298 | switch (event_type) |
| 299 | { |
| 300 | /* Send one of these when a resolution is pending */ |
| 301 | case DNS_RESOLVER_EVENT_PENDING: |
| 302 | timeout = 2.0; |
| 303 | break; |
| 304 | |
| 305 | case DNS_RESOLVER_EVENT_RESOLVED: |
| 306 | for (i = 0; i < vec_len (event_data); i++) |
| 307 | resolve_event (dm, now, (u8 *) event_data[i]); |
| 308 | break; |
| 309 | |
| 310 | case ~0: /* timeout */ |
| 311 | retry_scan (dm, now); |
| 312 | break; |
| 313 | } |
| 314 | vec_reset_length (event_data); |
| 315 | |
| 316 | /* No work? Back to slow timeout mode... */ |
| 317 | if (vec_len (dm->unresolved_entries) == 0) |
| 318 | timeout = 1000.0; |
| 319 | } |
| 320 | return 0; /* or not */ |
| 321 | } |
| 322 | |
Dave Barach | 2466f50 | 2019-05-29 13:46:35 -0400 | [diff] [blame] | 323 | void |
| 324 | vnet_dns_create_resolver_process (dns_main_t * dm) |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 325 | { |
Dave Barach | 2466f50 | 2019-05-29 13:46:35 -0400 | [diff] [blame] | 326 | /* Already created the resolver process? */ |
| 327 | if (dm->resolver_process_node_index > 0) |
| 328 | return; |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 329 | |
Dave Barach | 2466f50 | 2019-05-29 13:46:35 -0400 | [diff] [blame] | 330 | /* No, create it now and make a note of the node index */ |
| 331 | dm->resolver_process_node_index = vlib_process_create |
| 332 | (dm->vlib_main, "dns-resolver-process", |
| 333 | dns_resolver_process, 16 /* log2_n_stack_bytes */ ); |
| 334 | } |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 335 | |
| 336 | /* |
| 337 | * fd.io coding-style-patch-verification: ON |
| 338 | * |
| 339 | * Local Variables: |
| 340 | * eval: (c-set-style "gnu") |
| 341 | * End: |
| 342 | */ |