blob: 1272e756d7c4e2f36db87def44e149a193e723a0 [file] [log] [blame]
Dave Barach65457162017-10-10 17:53:14 -04001/*
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
27typedef struct
28{
Dave Barachd2080152017-10-20 09:21:35 -040029 u32 request_type;
30 u32 client_index;
31 u32 client_context;
Dave Barach97494502017-11-04 09:44:38 -040032 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 Barachd2080152017-10-20 09:21:35 -040039
Dave Barach97494502017-11-04 09:44:38 -040040typedef 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 Barachd2080152017-10-20 09:21:35 -040047
48typedef struct
49{
Dave Barach65457162017-10-10 17:53:14 -040050 /** flags */
51 volatile u8 flags;
52
53 /** The name in "normal human being" notation, e.g. www.foobar.com */
54 u8 *name;
55
Dave Barach0cb01bd2017-10-16 14:39:52 -040056 /** For CNAME records, the "next name" to resolve */
57 u8 *cname;
58
Dave Barach65457162017-10-10 17:53:14 -040059 /** 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;
69 f64 retry_timer;
70
71 /** Cached dns response */
72 u8 *dns_response;
73
Dave Barach97494502017-11-04 09:44:38 -040074 /** Clients / peers awaiting responses */
75 dns_pending_request_t *pending_requests;
Dave Barach65457162017-10-10 17:53:14 -040076} dns_cache_entry_t;
77
78#define DNS_CACHE_ENTRY_FLAG_VALID (1<<0) /**< we have Actual Data */
79#define DNS_CACHE_ENTRY_FLAG_STATIC (1<<1) /**< static entry */
Dave Barach0cb01bd2017-10-16 14:39:52 -040080#define DNS_CACHE_ENTRY_FLAG_CNAME (1<<2) /**< CNAME (indirect) entry */
Dave Barach65457162017-10-10 17:53:14 -040081
82#define DNS_RETRIES_PER_SERVER 3
83
84#define DNS_RESOLVER_EVENT_RESOLVED 1
85#define DNS_RESOLVER_EVENT_PENDING 2
86
87
88typedef struct
89{
90 /** Pool of cache entries */
91 dns_cache_entry_t *entries;
92
93 /** Pool indices of unresolved entries */
94 u32 *unresolved_entries;
95
96 /** Find cached record by name */
97 uword *cache_entry_by_name;
98 uword *cache_lock;
99
100 /** enable / disable flag */
101 int is_enabled;
102
103 /** upstream name servers, e.g. 8.8.8.8 */
104 ip4_address_t *ip4_name_servers;
105 ip6_address_t *ip6_name_servers;
106
107 /** config parameters */
108 u32 name_cache_size;
109 u32 max_ttl_in_seconds;
110 u32 random_seed;
111
112 /* convenience */
113 vlib_main_t *vlib_main;
114 vnet_main_t *vnet_main;
115} dns_main_t;
116
117extern dns_main_t dns_main;
118
119extern vlib_node_registration_t dns46_reply_node;
Dave Barach97494502017-11-04 09:44:38 -0400120extern vlib_node_registration_t dns4_request_node;
121extern vlib_node_registration_t dns6_request_node;
Dave Barach65457162017-10-10 17:53:14 -0400122extern vlib_node_registration_t dns_resolver_node;
123
Dave Barach97494502017-11-04 09:44:38 -0400124#define foreach_dns46_request_error \
125_(NONE, "No error") \
126_(UNIMPLEMENTED, "Unimplemented") \
127_(PROCESSED, "DNS request pkts processed") \
128_(IP_OPTIONS, "DNS pkts with ip options (dropped)") \
129_(BAD_REQUEST, "DNS pkts with serious discrepanices (dropped)") \
130_(TOO_MANY_REQUESTS, "DNS pkts asking too many questions") \
131_(RESOLUTION_REQUIRED, "DNS pkts pending upstream name resolution")
132
133typedef enum
134{
135#define _(sym,str) DNS46_REQUEST_ERROR_##sym,
136 foreach_dns46_request_error
137#undef _
138 DNS46_REQUEST_N_ERROR,
139} dns46_request_error_t;
140
Dave Barach65457162017-10-10 17:53:14 -0400141#define foreach_dns46_reply_error \
Dave Barachb8a0d2c2017-11-15 13:28:15 -0500142_(DISABLED, "DNS pkts punted (feature disabled)") \
Dave Barach65457162017-10-10 17:53:14 -0400143_(PROCESSED, "DNS reply pkts processed") \
144_(NO_ELT, "No DNS pool element") \
145_(FORMAT_ERROR, "DNS format errors") \
146_(TEST_DROP, "DNS reply pkt dropped for test purposes")
147
148typedef enum
149{
150#define _(sym,str) DNS46_REPLY_ERROR_##sym,
151 foreach_dns46_reply_error
152#undef _
153 DNS46_REPLY_N_ERROR,
154} dns46_reply_error_t;
155
156void vnet_send_dns_request (dns_main_t * dm, dns_cache_entry_t * ep);
Dave Barach0cb01bd2017-10-16 14:39:52 -0400157int
158vnet_dns_cname_indirection_nolock (dns_main_t * dm, u32 ep_index, u8 * reply);
159
Dave Barach65457162017-10-10 17:53:14 -0400160int vnet_dns_delete_entry_by_index_nolock (dns_main_t * dm, u32 index);
161
Dave Barach97494502017-11-04 09:44:38 -0400162int
163vnet_dns_resolve_name (dns_main_t * dm, u8 * name, dns_pending_request_t * t,
164 dns_cache_entry_t ** retp);
165
166void vnet_send_dns4_reply (dns_main_t * dm, dns_pending_request_t * t,
167 dns_cache_entry_t * ep, vlib_buffer_t * b0);
168
169void vnet_send_dns6_reply (dns_main_t * dm, dns_pending_request_t * t,
170 dns_cache_entry_t * ep, vlib_buffer_t * b0);
171
172u8 *vnet_dns_labels_to_name (u8 * label, u8 * full_text,
173 u8 ** parse_from_here);
174
Dave Barach65457162017-10-10 17:53:14 -0400175format_function_t format_dns_reply;
176
177static inline void
178dns_cache_lock (dns_main_t * dm)
179{
180 if (dm->cache_lock)
181 {
182 while (__sync_lock_test_and_set (dm->cache_lock, 1))
183 ;
184 }
185}
186
187static inline void
188dns_cache_unlock (dns_main_t * dm)
189{
190 if (dm->cache_lock)
191 {
192 CLIB_MEMORY_BARRIER ();
193 *dm->cache_lock = 0;
194 }
195}
196
197#endif /* included_dns_h */
198
199/*
200 * fd.io coding-style-patch-verification: ON
201 *
202 * Local Variables:
203 * eval: (c-set-style "gnu")
204 * End:
205 */