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