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 | #ifndef included_dns_h |
| 17 | #define included_dns_h |
| 18 | |
| 19 | #include <vppinfra/time.h> |
| 20 | #include <vppinfra/cache.h> |
| 21 | #include <vppinfra/error.h> |
| 22 | |
| 23 | #include <vppinfra/hash.h> |
| 24 | #include <vnet/dns/dns_packet.h> |
| 25 | #include <vnet/ip/ip.h> |
| 26 | |
| 27 | typedef struct |
| 28 | { |
Dave Barach | d208015 | 2017-10-20 09:21:35 -0400 | [diff] [blame] | 29 | u32 request_type; |
| 30 | u32 client_index; |
| 31 | u32 client_context; |
Dave Barach | 9749450 | 2017-11-04 09:44:38 -0400 | [diff] [blame] | 32 | u8 is_ip6; |
| 33 | u16 dst_port; |
| 34 | u16 id; |
| 35 | u16 pad; |
| 36 | u8 dst_address[16]; |
| 37 | u8 *name; |
| 38 | } dns_pending_request_t; |
Dave Barach | d208015 | 2017-10-20 09:21:35 -0400 | [diff] [blame] | 39 | |
Dave Barach | 9749450 | 2017-11-04 09:44:38 -0400 | [diff] [blame] | 40 | typedef enum |
| 41 | { |
| 42 | DNS_API_PENDING_NAME_TO_IP = 1, |
| 43 | DNS_API_PENDING_IP_TO_NAME, |
| 44 | DNS_PEER_PENDING_NAME_TO_IP, |
| 45 | DNS_PEER_PENDING_IP_TO_NAME, |
| 46 | } dns_pending_request_type_t; |
Dave Barach | d208015 | 2017-10-20 09:21:35 -0400 | [diff] [blame] | 47 | |
| 48 | typedef struct |
| 49 | { |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 50 | /** flags */ |
| 51 | volatile u8 flags; |
| 52 | |
| 53 | /** The name in "normal human being" notation, e.g. www.foobar.com */ |
| 54 | u8 *name; |
| 55 | |
Dave Barach | 0cb01bd | 2017-10-16 14:39:52 -0400 | [diff] [blame] | 56 | /** For CNAME records, the "next name" to resolve */ |
| 57 | u8 *cname; |
| 58 | |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 59 | /** Expiration time */ |
| 60 | f64 expiration_time; |
| 61 | |
| 62 | /** Cached dns request, for sending retries */ |
| 63 | u8 *dns_request; |
| 64 | |
| 65 | /** Retry parameters */ |
| 66 | int retry_count; |
| 67 | int server_rotor; |
| 68 | int server_af; |
Dave Barach | 580eda7 | 2018-01-09 17:00:00 -0500 | [diff] [blame] | 69 | int server_fails; |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 70 | f64 retry_timer; |
| 71 | |
| 72 | /** Cached dns response */ |
| 73 | u8 *dns_response; |
| 74 | |
Dave Barach | 9749450 | 2017-11-04 09:44:38 -0400 | [diff] [blame] | 75 | /** Clients / peers awaiting responses */ |
| 76 | dns_pending_request_t *pending_requests; |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 77 | } dns_cache_entry_t; |
| 78 | |
| 79 | #define DNS_CACHE_ENTRY_FLAG_VALID (1<<0) /**< we have Actual Data */ |
| 80 | #define DNS_CACHE_ENTRY_FLAG_STATIC (1<<1) /**< static entry */ |
Dave Barach | 0cb01bd | 2017-10-16 14:39:52 -0400 | [diff] [blame] | 81 | #define DNS_CACHE_ENTRY_FLAG_CNAME (1<<2) /**< CNAME (indirect) entry */ |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 82 | |
| 83 | #define DNS_RETRIES_PER_SERVER 3 |
| 84 | |
| 85 | #define DNS_RESOLVER_EVENT_RESOLVED 1 |
| 86 | #define DNS_RESOLVER_EVENT_PENDING 2 |
| 87 | |
| 88 | |
| 89 | typedef struct |
| 90 | { |
| 91 | /** Pool of cache entries */ |
| 92 | dns_cache_entry_t *entries; |
| 93 | |
| 94 | /** Pool indices of unresolved entries */ |
| 95 | u32 *unresolved_entries; |
| 96 | |
| 97 | /** Find cached record by name */ |
| 98 | uword *cache_entry_by_name; |
| 99 | uword *cache_lock; |
| 100 | |
| 101 | /** enable / disable flag */ |
| 102 | int is_enabled; |
| 103 | |
Dave Barach | 8a9566e | 2018-10-23 10:47:36 -0400 | [diff] [blame] | 104 | /** udp port registration complete */ |
| 105 | int udp_ports_registered; |
| 106 | |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 107 | /** upstream name servers, e.g. 8.8.8.8 */ |
| 108 | ip4_address_t *ip4_name_servers; |
| 109 | ip6_address_t *ip6_name_servers; |
| 110 | |
Dave Barach | 2466f50 | 2019-05-29 13:46:35 -0400 | [diff] [blame] | 111 | /** resolver process node index */ |
| 112 | u32 resolver_process_node_index; |
| 113 | |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 114 | /** config parameters */ |
| 115 | u32 name_cache_size; |
| 116 | u32 max_ttl_in_seconds; |
| 117 | u32 random_seed; |
| 118 | |
| 119 | /* convenience */ |
| 120 | vlib_main_t *vlib_main; |
| 121 | vnet_main_t *vnet_main; |
| 122 | } dns_main_t; |
| 123 | |
| 124 | extern dns_main_t dns_main; |
| 125 | |
| 126 | extern vlib_node_registration_t dns46_reply_node; |
Dave Barach | 9749450 | 2017-11-04 09:44:38 -0400 | [diff] [blame] | 127 | extern vlib_node_registration_t dns4_request_node; |
| 128 | extern vlib_node_registration_t dns6_request_node; |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 129 | extern vlib_node_registration_t dns_resolver_node; |
| 130 | |
Dave Barach | 9749450 | 2017-11-04 09:44:38 -0400 | [diff] [blame] | 131 | #define foreach_dns46_request_error \ |
| 132 | _(NONE, "No error") \ |
| 133 | _(UNIMPLEMENTED, "Unimplemented") \ |
| 134 | _(PROCESSED, "DNS request pkts processed") \ |
| 135 | _(IP_OPTIONS, "DNS pkts with ip options (dropped)") \ |
| 136 | _(BAD_REQUEST, "DNS pkts with serious discrepanices (dropped)") \ |
| 137 | _(TOO_MANY_REQUESTS, "DNS pkts asking too many questions") \ |
| 138 | _(RESOLUTION_REQUIRED, "DNS pkts pending upstream name resolution") |
| 139 | |
| 140 | typedef enum |
| 141 | { |
| 142 | #define _(sym,str) DNS46_REQUEST_ERROR_##sym, |
| 143 | foreach_dns46_request_error |
| 144 | #undef _ |
| 145 | DNS46_REQUEST_N_ERROR, |
| 146 | } dns46_request_error_t; |
| 147 | |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 148 | #define foreach_dns46_reply_error \ |
Dave Barach | b8a0d2c | 2017-11-15 13:28:15 -0500 | [diff] [blame] | 149 | _(DISABLED, "DNS pkts punted (feature disabled)") \ |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 150 | _(PROCESSED, "DNS reply pkts processed") \ |
| 151 | _(NO_ELT, "No DNS pool element") \ |
| 152 | _(FORMAT_ERROR, "DNS format errors") \ |
| 153 | _(TEST_DROP, "DNS reply pkt dropped for test purposes") |
| 154 | |
| 155 | typedef enum |
| 156 | { |
| 157 | #define _(sym,str) DNS46_REPLY_ERROR_##sym, |
| 158 | foreach_dns46_reply_error |
| 159 | #undef _ |
| 160 | DNS46_REPLY_N_ERROR, |
| 161 | } dns46_reply_error_t; |
| 162 | |
| 163 | void vnet_send_dns_request (dns_main_t * dm, dns_cache_entry_t * ep); |
Dave Barach | 0cb01bd | 2017-10-16 14:39:52 -0400 | [diff] [blame] | 164 | int |
| 165 | vnet_dns_cname_indirection_nolock (dns_main_t * dm, u32 ep_index, u8 * reply); |
| 166 | |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 167 | int vnet_dns_delete_entry_by_index_nolock (dns_main_t * dm, u32 index); |
| 168 | |
Dave Barach | 9749450 | 2017-11-04 09:44:38 -0400 | [diff] [blame] | 169 | int |
| 170 | vnet_dns_resolve_name (dns_main_t * dm, u8 * name, dns_pending_request_t * t, |
| 171 | dns_cache_entry_t ** retp); |
| 172 | |
Dave Barach | 580eda7 | 2018-01-09 17:00:00 -0500 | [diff] [blame] | 173 | void |
| 174 | vnet_dns_send_dns6_request (dns_main_t * dm, |
| 175 | dns_cache_entry_t * ep, ip6_address_t * server); |
| 176 | void |
| 177 | vnet_dns_send_dns4_request (dns_main_t * dm, |
| 178 | dns_cache_entry_t * ep, ip4_address_t * server); |
| 179 | |
Dave Barach | 9749450 | 2017-11-04 09:44:38 -0400 | [diff] [blame] | 180 | void vnet_send_dns4_reply (dns_main_t * dm, dns_pending_request_t * t, |
| 181 | dns_cache_entry_t * ep, vlib_buffer_t * b0); |
| 182 | |
| 183 | void vnet_send_dns6_reply (dns_main_t * dm, dns_pending_request_t * t, |
| 184 | dns_cache_entry_t * ep, vlib_buffer_t * b0); |
| 185 | |
| 186 | u8 *vnet_dns_labels_to_name (u8 * label, u8 * full_text, |
| 187 | u8 ** parse_from_here); |
| 188 | |
Dave Barach | 2466f50 | 2019-05-29 13:46:35 -0400 | [diff] [blame] | 189 | void vnet_dns_create_resolver_process (dns_main_t * dm); |
| 190 | |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 191 | format_function_t format_dns_reply; |
| 192 | |
| 193 | static inline void |
| 194 | dns_cache_lock (dns_main_t * dm) |
| 195 | { |
| 196 | if (dm->cache_lock) |
| 197 | { |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 198 | while (clib_atomic_test_and_set (dm->cache_lock)) |
Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 199 | ; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | static inline void |
| 204 | dns_cache_unlock (dns_main_t * dm) |
| 205 | { |
| 206 | if (dm->cache_lock) |
| 207 | { |
| 208 | CLIB_MEMORY_BARRIER (); |
| 209 | *dm->cache_lock = 0; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | #endif /* included_dns_h */ |
| 214 | |
| 215 | /* |
| 216 | * fd.io coding-style-patch-verification: ON |
| 217 | * |
| 218 | * Local Variables: |
| 219 | * eval: (c-set-style "gnu") |
| 220 | * End: |
| 221 | */ |