blob: f7446cce8253e8e286f41f7c12719ae34f08577a [file] [log] [blame]
Dave Barach97494502017-11-04 09:44:38 -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#include <vnet/dns/dns.h>
17
18#include <vlib/vlib.h>
19#include <vnet/vnet.h>
20
21vlib_node_registration_t dns46_request_node;
22
23typedef struct
24{
25 u32 pool_index;
26 u32 disposition;
27} dns46_request_trace_t;
28
29/* packet trace format function */
30static u8 *
31format_dns46_request_trace (u8 * s, va_list * args)
32{
33 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
34 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
35 dns46_request_trace_t *t = va_arg (*args, dns46_request_trace_t *);
36
37 s = format (s, "DNS46_REPLY: pool index %d, disposition %d",
38 t->pool_index, t->disposition);
39 return s;
40}
41
42vlib_node_registration_t dns46_request_node;
43
44static char *dns46_request_error_strings[] = {
45#define _(sym,string) string,
46 foreach_dns46_request_error
47#undef _
48};
49
50typedef enum
51{
52 DNS46_REQUEST_NEXT_DROP,
53 DNS46_REQUEST_NEXT_IP_LOOKUP,
Dave Barachb8a0d2c2017-11-15 13:28:15 -050054 DNS46_REQUEST_NEXT_PUNT,
Dave Barach97494502017-11-04 09:44:38 -040055 DNS46_REQUEST_N_NEXT,
56} dns46_request_next_t;
57
58static uword
59dns46_request_inline (vlib_main_t * vm,
60 vlib_node_runtime_t * node, vlib_frame_t * frame,
61 int is_ip6)
62{
63 u32 n_left_from, *from, *to_next;
64 dns46_request_next_t next_index;
65 dns_main_t *dm = &dns_main;
66
67 from = vlib_frame_vector_args (frame);
68 n_left_from = frame->n_vectors;
69 next_index = node->cached_next_index;
70
71 while (n_left_from > 0)
72 {
73 u32 n_left_to_next;
74
75 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
76
77#if 0
78 while (n_left_from >= 4 && n_left_to_next >= 2)
79 {
80 u32 next0 = DNS46_REQUEST_NEXT_INTERFACE_OUTPUT;
81 u32 next1 = DNS46_REQUEST_NEXT_INTERFACE_OUTPUT;
82 u32 sw_if_index0, sw_if_index1;
83 u8 tmp0[6], tmp1[6];
84 ethernet_header_t *en0, *en1;
85 u32 bi0, bi1;
86 vlib_buffer_t *b0, *b1;
87
88 /* Prefetch next iteration. */
89 {
90 vlib_buffer_t *p2, *p3;
91
92 p2 = vlib_get_buffer (vm, from[2]);
93 p3 = vlib_get_buffer (vm, from[3]);
94
95 vlib_prefetch_buffer_header (p2, LOAD);
96 vlib_prefetch_buffer_header (p3, LOAD);
97
98 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
99 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
100 }
101
102 /* speculatively enqueue b0 and b1 to the current next frame */
103 to_next[0] = bi0 = from[0];
104 to_next[1] = bi1 = from[1];
105 from += 2;
106 to_next += 2;
107 n_left_from -= 2;
108 n_left_to_next -= 2;
109
110 b0 = vlib_get_buffer (vm, bi0);
111 b1 = vlib_get_buffer (vm, bi1);
112
113 /* $$$$$ End of processing 2 x packets $$$$$ */
114
115 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
116 {
117 if (b0->flags & VLIB_BUFFER_IS_TRACED)
118 {
119 dns46_request_trace_t *t =
120 vlib_add_trace (vm, node, b0, sizeof (*t));
121 t->sw_if_index = sw_if_index0;
122 t->next_index = next0;
123 }
124 if (b1->flags & VLIB_BUFFER_IS_TRACED)
125 {
126 dns46_request_trace_t *t =
127 vlib_add_trace (vm, node, b1, sizeof (*t));
128 t->sw_if_index = sw_if_index1;
129 t->next_index = next1;
130 }
131 }
132
133 /* verify speculative enqueues, maybe switch current next frame */
134 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
135 to_next, n_left_to_next,
136 bi0, bi1, next0, next1);
137 }
138#endif
139
140 while (n_left_from > 0 && n_left_to_next > 0)
141 {
142 u32 bi0;
143 vlib_buffer_t *b0;
144 u32 next0 = DNS46_REQUEST_NEXT_DROP;
145 u32 error0 = DNS46_REQUEST_ERROR_NONE;
146 udp_header_t *u0;
147 dns_header_t *d0;
148 dns_query_t *q0;
149 ip4_header_t *ip40 = 0;
150 ip6_header_t *ip60 = 0;
151 dns_cache_entry_t *ep0;
152 dns_pending_request_t _t0, *t0 = &_t0;
153 u16 flags0;
154 u32 pool_index0 = ~0;
155 u8 *name0;
156 u8 *label0;
157
158 /* speculatively enqueue b0 to the current next frame */
159 bi0 = from[0];
160 to_next[0] = bi0;
161 from += 1;
162 to_next += 1;
163 n_left_from -= 1;
Dave Barach97494502017-11-04 09:44:38 -0400164 n_left_to_next -= 1;
165
166 b0 = vlib_get_buffer (vm, bi0);
167 d0 = vlib_buffer_get_current (b0);
168 u0 = (udp_header_t *) ((u8 *) d0 - sizeof (*u0));
Dave Barachb8a0d2c2017-11-15 13:28:15 -0500169
170 if (PREDICT_FALSE (dm->is_enabled == 0))
171 {
172 next0 = DNS46_REQUEST_NEXT_PUNT;
173 goto done0;
174 }
175
Dave Barach97494502017-11-04 09:44:38 -0400176 if (is_ip6)
177 {
Dave Barachb8a0d2c2017-11-15 13:28:15 -0500178 ip60 = (ip6_header_t *) (((u8 *) u0) - sizeof (ip6_header_t));
179 next0 = DNS46_REQUEST_NEXT_DROP;
Dave Barach97494502017-11-04 09:44:38 -0400180 error0 = DNS46_REQUEST_ERROR_UNIMPLEMENTED;
181 goto done0;
182 }
183 else
184 {
185 ip40 = (ip4_header_t *) (((u8 *) u0) - sizeof (ip4_header_t));
186 if (ip40->ip_version_and_header_length != 0x45)
187 {
188 error0 = DNS46_REQUEST_ERROR_IP_OPTIONS;
189 goto done0;
190 }
191 }
192 /* Parse through the DNS request */
193 flags0 = clib_net_to_host_u16 (d0->flags);
194
195 /* Requests only */
196 if (flags0 & DNS_QR)
197 {
Dave Barachb8a0d2c2017-11-15 13:28:15 -0500198 next0 = DNS46_REQUEST_NEXT_DROP;
Dave Barach97494502017-11-04 09:44:38 -0400199 error0 = DNS46_REQUEST_ERROR_BAD_REQUEST;
200 goto done0;
201 }
202 if (clib_net_to_host_u16 (d0->qdcount) != 1)
203 {
Dave Barachb8a0d2c2017-11-15 13:28:15 -0500204 next0 = DNS46_REQUEST_NEXT_DROP;
Dave Barach97494502017-11-04 09:44:38 -0400205 error0 = DNS46_REQUEST_ERROR_TOO_MANY_REQUESTS;
206 goto done0;
207 }
208
209 label0 = (u8 *) (d0 + 1);
210
211 name0 = vnet_dns_labels_to_name (label0, (u8 *) d0, (u8 **) & q0);
212
213 t0->request_type = DNS_PEER_PENDING_NAME_TO_IP;
214
215 /*
216 * See if this is a reverse lookup. Both ip4 and ip6 reverse
217 * requests end with ".arpa"
218 */
219 if (PREDICT_TRUE (vec_len (name0) > 5))
220 {
221 u8 *aptr0 = name0 + vec_len (name0) - 5;
222
223 if (!memcmp (aptr0, ".arpa", 5))
224 t0->request_type = DNS_PEER_PENDING_IP_TO_NAME;
225 }
226
227 t0->client_index = ~0;
228 t0->is_ip6 = is_ip6;
229 t0->dst_port = u0->src_port;
230 t0->id = d0->id;
231 t0->name = name0;
232 if (is_ip6)
233 clib_memcpy (t0->dst_address, ip60->src_address.as_u8,
234 sizeof (ip6_address_t));
235 else
236 clib_memcpy (t0->dst_address, ip40->src_address.as_u8,
237 sizeof (ip4_address_t));
238
239 vnet_dns_resolve_name (dm, name0, t0, &ep0);
240
241 if (ep0)
242 {
243 if (is_ip6)
244 vnet_send_dns6_reply (dm, t0, ep0, b0);
245 else
246 vnet_send_dns4_reply (dm, t0, ep0, b0);
247 next0 = DNS46_REQUEST_NEXT_IP_LOOKUP;
248 }
249 else
250 {
251 error0 = DNS46_REQUEST_ERROR_RESOLUTION_REQUIRED;
252 }
253
254 done0:
255 b0->error = node->errors[error0];
256
257 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
258 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
259 {
260 dns46_request_trace_t *t =
261 vlib_add_trace (vm, node, b0, sizeof (*t));
262 t->disposition = error0;
263 t->pool_index = pool_index0;
264 }
265
266 /* verify speculative enqueue, maybe switch current next frame */
267 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
268 to_next, n_left_to_next,
269 bi0, next0);
270 }
271
272 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
273 }
274
275 return frame->n_vectors;
276}
277
278static uword
279dns4_request_node_fn (vlib_main_t * vm,
280 vlib_node_runtime_t * node, vlib_frame_t * frame)
281{
282
283 return dns46_request_inline (vm, node, frame, 0 /* is_ip6 */ );
284}
285
286/* *INDENT-OFF* */
287VLIB_REGISTER_NODE (dns4_request_node) =
288{
289 .function = dns4_request_node_fn,
290 .name = "dns4-request",
291 .vector_size = sizeof (u32),
292 .format_trace = format_dns46_request_trace,
293 .type = VLIB_NODE_TYPE_INTERNAL,
294 .n_errors = ARRAY_LEN (dns46_request_error_strings),
295 .error_strings = dns46_request_error_strings,
296 .n_next_nodes = DNS46_REQUEST_N_NEXT,
297 .next_nodes = {
298 [DNS46_REQUEST_NEXT_DROP] = "error-drop",
Dave Barachb8a0d2c2017-11-15 13:28:15 -0500299 [DNS46_REQUEST_NEXT_PUNT] = "error-punt",
Dave Barach97494502017-11-04 09:44:38 -0400300 [DNS46_REQUEST_NEXT_IP_LOOKUP] = "ip4-lookup",
301 },
302};
303/* *INDENT-ON* */
304
305static uword
306dns6_request_node_fn (vlib_main_t * vm,
307 vlib_node_runtime_t * node, vlib_frame_t * frame)
308{
309
310 return dns46_request_inline (vm, node, frame, 1 /* is_ip6 */ );
311}
312
313/* *INDENT-OFF* */
314VLIB_REGISTER_NODE (dns6_request_node) =
315{
316 .function = dns6_request_node_fn,
317 .name = "dns6-request",
318 .vector_size = sizeof (u32),
319 .format_trace = format_dns46_request_trace,
320 .type = VLIB_NODE_TYPE_INTERNAL,
321 .n_errors = ARRAY_LEN (dns46_request_error_strings),
322 .error_strings = dns46_request_error_strings,
323 .n_next_nodes = DNS46_REQUEST_N_NEXT,
324 .next_nodes = {
325 [DNS46_REQUEST_NEXT_DROP] = "error-drop",
Dave Barachb8a0d2c2017-11-15 13:28:15 -0500326 [DNS46_REQUEST_NEXT_PUNT] = "error-punt",
Dave Barach97494502017-11-04 09:44:38 -0400327 [DNS46_REQUEST_NEXT_IP_LOOKUP] = "ip6-lookup",
328 },
329};
330/* *INDENT-ON* */
331
332/*
333 * fd.io coding-style-patch-verification: ON
334 *
335 * Local Variables:
336 * eval: (c-set-style "gnu")
337 * End:
338 */