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 | |
| 41 | vlib_node_registration_t dns_resolver_node; |
| 42 | |
| 43 | extern int |
| 44 | vnet_dns_response_to_reply (u8 * response, |
| 45 | vl_api_dns_resolve_name_reply_t * rmp, |
| 46 | u32 * min_ttlp); |
Dave Barach | d208015 | 2017-10-20 09:21:35 -0400 | [diff] [blame] | 47 | extern int |
| 48 | vnet_dns_response_to_name (u8 * response, |
| 49 | vl_api_dns_resolve_ip_reply_t * rmp, |
| 50 | u32 * min_ttlp); |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 51 | |
| 52 | static void |
| 53 | resolve_event (dns_main_t * dm, f64 now, u8 * reply) |
| 54 | { |
| 55 | vlib_main_t *vm = dm->vlib_main; |
Dave Barach | 9749450 | 2017-11-04 09:44:38 -0400 | [diff] [blame] | 56 | dns_pending_request_t *pr; |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 57 | 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 Barach | 0cb01bd | 2017-10-16 14:39:52 -0400 | [diff] [blame] | 89 | if (vnet_dns_cname_indirection_nolock (dm, pool_index, reply)) |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 90 | { |
| 91 | dns_cache_unlock (dm); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | /* Save the response */ |
| 96 | ep->dns_response = reply; |
| 97 | /* Pick some sensible default. */ |
| 98 | ep->expiration_time = now + 600.0; |
| 99 | if (vec_len (ep->dns_response)) |
| 100 | ep->flags |= DNS_CACHE_ENTRY_FLAG_VALID; |
| 101 | |
| 102 | /* Most likely, send 1 message */ |
Dave Barach | 9749450 | 2017-11-04 09:44:38 -0400 | [diff] [blame] | 103 | for (i = 0; i < vec_len (ep->pending_requests); i++) |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 104 | { |
| 105 | vl_api_registration_t *regp; |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 106 | |
Dave Barach | 9749450 | 2017-11-04 09:44:38 -0400 | [diff] [blame] | 107 | pr = vec_elt_at_index (ep->pending_requests, i); |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 108 | |
Dave Barach | 9749450 | 2017-11-04 09:44:38 -0400 | [diff] [blame] | 109 | switch (pr->request_type) |
| 110 | { |
| 111 | case DNS_API_PENDING_NAME_TO_IP: |
| 112 | { |
| 113 | vl_api_dns_resolve_name_reply_t *rmp; |
| 114 | regp = vl_api_client_index_to_registration (pr->client_index); |
| 115 | if (regp == 0) |
| 116 | continue; |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 117 | |
Dave Barach | 9749450 | 2017-11-04 09:44:38 -0400 | [diff] [blame] | 118 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 119 | rmp->_vl_msg_id = |
| 120 | clib_host_to_net_u16 (VL_API_DNS_RESOLVE_NAME_REPLY); |
| 121 | rmp->context = pr->client_context; |
| 122 | min_ttl = ~0; |
| 123 | rv = vnet_dns_response_to_reply (ep->dns_response, rmp, &min_ttl); |
| 124 | if (min_ttl != ~0) |
| 125 | ep->expiration_time = now + min_ttl; |
| 126 | rmp->retval = clib_host_to_net_u32 (rv); |
| 127 | vl_msg_api_send (regp, (u8 *) rmp); |
| 128 | } |
| 129 | break; |
| 130 | |
| 131 | case DNS_API_PENDING_IP_TO_NAME: |
| 132 | { |
| 133 | vl_api_dns_resolve_ip_reply_t *rmp; |
| 134 | |
| 135 | regp = vl_api_client_index_to_registration (pr->client_index); |
| 136 | if (regp == 0) |
| 137 | continue; |
| 138 | |
| 139 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 140 | rmp->_vl_msg_id = |
| 141 | clib_host_to_net_u16 (VL_API_DNS_RESOLVE_IP_REPLY); |
| 142 | rmp->context = pr->client_context; |
| 143 | min_ttl = ~0; |
| 144 | rv = vnet_dns_response_to_name (ep->dns_response, rmp, &min_ttl); |
| 145 | if (min_ttl != ~0) |
| 146 | ep->expiration_time = now + min_ttl; |
| 147 | rmp->retval = clib_host_to_net_u32 (rv); |
| 148 | vl_msg_api_send (regp, (u8 *) rmp); |
| 149 | } |
| 150 | break; |
| 151 | |
| 152 | case DNS_PEER_PENDING_IP_TO_NAME: |
| 153 | case DNS_PEER_PENDING_NAME_TO_IP: |
| 154 | if (pr->is_ip6) |
| 155 | vnet_send_dns6_reply (dm, pr, ep, 0 /* allocate a buffer */ ); |
| 156 | else |
| 157 | vnet_send_dns4_reply (dm, pr, ep, 0 /* allocate a buffer */ ); |
| 158 | break; |
| 159 | default: |
| 160 | clib_warning ("request type %d unknown", pr->request_type); |
| 161 | break; |
Dave Barach | d208015 | 2017-10-20 09:21:35 -0400 | [diff] [blame] | 162 | } |
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 | vec_free (ep->pending_requests); |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 165 | |
| 166 | for (i = 0; i < vec_len (dm->unresolved_entries); i++) |
| 167 | { |
| 168 | if (dm->unresolved_entries[i] == pool_index) |
| 169 | { |
| 170 | vec_delete (dm->unresolved_entries, 1, i); |
| 171 | goto found; |
| 172 | } |
| 173 | } |
| 174 | clib_warning ("pool index %d AWOL from unresolved vector", pool_index); |
| 175 | |
| 176 | found: |
| 177 | /* Deal with bogus names, server issues, etc. */ |
| 178 | switch (rcode) |
| 179 | { |
| 180 | default: |
| 181 | case DNS_RCODE_NO_ERROR: |
| 182 | break; |
| 183 | |
| 184 | case DNS_RCODE_SERVER_FAILURE: |
| 185 | case DNS_RCODE_NOT_IMPLEMENTED: |
| 186 | case DNS_RCODE_REFUSED: |
| 187 | if (ep->server_af == 0) |
| 188 | clib_warning ("name server %U backfire", |
| 189 | format_ip4_address, |
| 190 | dm->ip4_name_servers + ep->server_rotor); |
| 191 | else |
| 192 | clib_warning ("name server %U backfire", |
| 193 | format_ip6_address, |
| 194 | dm->ip6_name_servers + ep->server_rotor); |
| 195 | /* FALLTHROUGH */ |
| 196 | case DNS_RCODE_NAME_ERROR: |
| 197 | case DNS_RCODE_FORMAT_ERROR: |
| 198 | /* remove trash from the cache... */ |
| 199 | vnet_dns_delete_entry_by_index_nolock (dm, ep - dm->entries); |
| 200 | break; |
| 201 | } |
| 202 | |
| 203 | dns_cache_unlock (dm); |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | static void |
| 208 | retry_scan (dns_main_t * dm, f64 now) |
| 209 | { |
| 210 | int i; |
| 211 | dns_cache_entry_t *ep; |
| 212 | |
| 213 | for (i = 0; i < vec_len (dm->unresolved_entries); i++) |
| 214 | { |
| 215 | dns_cache_lock (dm); |
| 216 | ep = pool_elt_at_index (dm->entries, dm->unresolved_entries[i]); |
| 217 | |
| 218 | ASSERT ((ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) == 0); |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 219 | vnet_send_dns_request (dm, ep); |
| 220 | dns_cache_unlock (dm); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | static uword |
| 225 | dns_resolver_process (vlib_main_t * vm, |
| 226 | vlib_node_runtime_t * rt, vlib_frame_t * f) |
| 227 | { |
| 228 | dns_main_t *dm = &dns_main; |
| 229 | f64 now; |
| 230 | f64 timeout = 1000.0; |
| 231 | uword *event_data = 0; |
| 232 | uword event_type; |
| 233 | int i; |
| 234 | |
| 235 | while (1) |
| 236 | { |
| 237 | vlib_process_wait_for_event_or_clock (vm, timeout); |
| 238 | |
| 239 | now = vlib_time_now (vm); |
| 240 | |
| 241 | event_type = vlib_process_get_events (vm, (uword **) & event_data); |
| 242 | |
| 243 | switch (event_type) |
| 244 | { |
| 245 | /* Send one of these when a resolution is pending */ |
| 246 | case DNS_RESOLVER_EVENT_PENDING: |
| 247 | timeout = 2.0; |
| 248 | break; |
| 249 | |
| 250 | case DNS_RESOLVER_EVENT_RESOLVED: |
| 251 | for (i = 0; i < vec_len (event_data); i++) |
| 252 | resolve_event (dm, now, (u8 *) event_data[i]); |
| 253 | break; |
| 254 | |
| 255 | case ~0: /* timeout */ |
| 256 | retry_scan (dm, now); |
| 257 | break; |
| 258 | } |
| 259 | vec_reset_length (event_data); |
| 260 | |
| 261 | /* No work? Back to slow timeout mode... */ |
| 262 | if (vec_len (dm->unresolved_entries) == 0) |
| 263 | timeout = 1000.0; |
| 264 | } |
| 265 | return 0; /* or not */ |
| 266 | } |
| 267 | |
| 268 | /* *INDENT-OFF* */ |
| 269 | VLIB_REGISTER_NODE (dns_resolver_node) = |
| 270 | { |
| 271 | .function = dns_resolver_process, |
| 272 | .type = VLIB_NODE_TYPE_PROCESS, |
| 273 | .name = "dns-resolver-process", |
| 274 | }; |
| 275 | /* *INDENT-ON* */ |
| 276 | |
| 277 | |
| 278 | /* |
| 279 | * fd.io coding-style-patch-verification: ON |
| 280 | * |
| 281 | * Local Variables: |
| 282 | * eval: (c-set-style "gnu") |
| 283 | * End: |
| 284 | */ |