blob: 6446880523737099bdcd1fa6835da3798a84def9 [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,
54 DNS46_REQUEST_N_NEXT,
55} dns46_request_next_t;
56
57static uword
58dns46_request_inline (vlib_main_t * vm,
59 vlib_node_runtime_t * node, vlib_frame_t * frame,
60 int is_ip6)
61{
62 u32 n_left_from, *from, *to_next;
63 dns46_request_next_t next_index;
64 dns_main_t *dm = &dns_main;
65
66 from = vlib_frame_vector_args (frame);
67 n_left_from = frame->n_vectors;
68 next_index = node->cached_next_index;
69
70 while (n_left_from > 0)
71 {
72 u32 n_left_to_next;
73
74 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
75
76#if 0
77 while (n_left_from >= 4 && n_left_to_next >= 2)
78 {
79 u32 next0 = DNS46_REQUEST_NEXT_INTERFACE_OUTPUT;
80 u32 next1 = DNS46_REQUEST_NEXT_INTERFACE_OUTPUT;
81 u32 sw_if_index0, sw_if_index1;
82 u8 tmp0[6], tmp1[6];
83 ethernet_header_t *en0, *en1;
84 u32 bi0, bi1;
85 vlib_buffer_t *b0, *b1;
86
87 /* Prefetch next iteration. */
88 {
89 vlib_buffer_t *p2, *p3;
90
91 p2 = vlib_get_buffer (vm, from[2]);
92 p3 = vlib_get_buffer (vm, from[3]);
93
94 vlib_prefetch_buffer_header (p2, LOAD);
95 vlib_prefetch_buffer_header (p3, LOAD);
96
97 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
98 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
99 }
100
101 /* speculatively enqueue b0 and b1 to the current next frame */
102 to_next[0] = bi0 = from[0];
103 to_next[1] = bi1 = from[1];
104 from += 2;
105 to_next += 2;
106 n_left_from -= 2;
107 n_left_to_next -= 2;
108
109 b0 = vlib_get_buffer (vm, bi0);
110 b1 = vlib_get_buffer (vm, bi1);
111
112 /* $$$$$ End of processing 2 x packets $$$$$ */
113
114 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
115 {
116 if (b0->flags & VLIB_BUFFER_IS_TRACED)
117 {
118 dns46_request_trace_t *t =
119 vlib_add_trace (vm, node, b0, sizeof (*t));
120 t->sw_if_index = sw_if_index0;
121 t->next_index = next0;
122 }
123 if (b1->flags & VLIB_BUFFER_IS_TRACED)
124 {
125 dns46_request_trace_t *t =
126 vlib_add_trace (vm, node, b1, sizeof (*t));
127 t->sw_if_index = sw_if_index1;
128 t->next_index = next1;
129 }
130 }
131
132 /* verify speculative enqueues, maybe switch current next frame */
133 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
134 to_next, n_left_to_next,
135 bi0, bi1, next0, next1);
136 }
137#endif
138
139 while (n_left_from > 0 && n_left_to_next > 0)
140 {
141 u32 bi0;
142 vlib_buffer_t *b0;
143 u32 next0 = DNS46_REQUEST_NEXT_DROP;
144 u32 error0 = DNS46_REQUEST_ERROR_NONE;
145 udp_header_t *u0;
146 dns_header_t *d0;
147 dns_query_t *q0;
148 ip4_header_t *ip40 = 0;
149 ip6_header_t *ip60 = 0;
150 dns_cache_entry_t *ep0;
151 dns_pending_request_t _t0, *t0 = &_t0;
152 u16 flags0;
153 u32 pool_index0 = ~0;
154 u8 *name0;
155 u8 *label0;
156
157 /* speculatively enqueue b0 to the current next frame */
158 bi0 = from[0];
159 to_next[0] = bi0;
160 from += 1;
161 to_next += 1;
162 n_left_from -= 1;
163
164 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));
169 if (is_ip6)
170 {
171 ip60 = (ip6_header_t *) (((u8 *) u0) - sizeof (ip4_header_t));
172 error0 = DNS46_REQUEST_ERROR_UNIMPLEMENTED;
173 goto done0;
174 }
175 else
176 {
177 ip40 = (ip4_header_t *) (((u8 *) u0) - sizeof (ip4_header_t));
178 if (ip40->ip_version_and_header_length != 0x45)
179 {
180 error0 = DNS46_REQUEST_ERROR_IP_OPTIONS;
181 goto done0;
182 }
183 }
184 /* Parse through the DNS request */
185 flags0 = clib_net_to_host_u16 (d0->flags);
186
187 /* Requests only */
188 if (flags0 & DNS_QR)
189 {
190 error0 = DNS46_REQUEST_ERROR_BAD_REQUEST;
191 goto done0;
192 }
193 if (clib_net_to_host_u16 (d0->qdcount) != 1)
194 {
195 error0 = DNS46_REQUEST_ERROR_TOO_MANY_REQUESTS;
196 goto done0;
197 }
198
199 label0 = (u8 *) (d0 + 1);
200
201 name0 = vnet_dns_labels_to_name (label0, (u8 *) d0, (u8 **) & q0);
202
203 t0->request_type = DNS_PEER_PENDING_NAME_TO_IP;
204
205 /*
206 * See if this is a reverse lookup. Both ip4 and ip6 reverse
207 * requests end with ".arpa"
208 */
209 if (PREDICT_TRUE (vec_len (name0) > 5))
210 {
211 u8 *aptr0 = name0 + vec_len (name0) - 5;
212
213 if (!memcmp (aptr0, ".arpa", 5))
214 t0->request_type = DNS_PEER_PENDING_IP_TO_NAME;
215 }
216
217 t0->client_index = ~0;
218 t0->is_ip6 = is_ip6;
219 t0->dst_port = u0->src_port;
220 t0->id = d0->id;
221 t0->name = name0;
222 if (is_ip6)
223 clib_memcpy (t0->dst_address, ip60->src_address.as_u8,
224 sizeof (ip6_address_t));
225 else
226 clib_memcpy (t0->dst_address, ip40->src_address.as_u8,
227 sizeof (ip4_address_t));
228
229 vnet_dns_resolve_name (dm, name0, t0, &ep0);
230
231 if (ep0)
232 {
233 if (is_ip6)
234 vnet_send_dns6_reply (dm, t0, ep0, b0);
235 else
236 vnet_send_dns4_reply (dm, t0, ep0, b0);
237 next0 = DNS46_REQUEST_NEXT_IP_LOOKUP;
238 }
239 else
240 {
241 error0 = DNS46_REQUEST_ERROR_RESOLUTION_REQUIRED;
242 }
243
244 done0:
245 b0->error = node->errors[error0];
246
247 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
248 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
249 {
250 dns46_request_trace_t *t =
251 vlib_add_trace (vm, node, b0, sizeof (*t));
252 t->disposition = error0;
253 t->pool_index = pool_index0;
254 }
255
256 /* verify speculative enqueue, maybe switch current next frame */
257 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
258 to_next, n_left_to_next,
259 bi0, next0);
260 }
261
262 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
263 }
264
265 return frame->n_vectors;
266}
267
268static uword
269dns4_request_node_fn (vlib_main_t * vm,
270 vlib_node_runtime_t * node, vlib_frame_t * frame)
271{
272
273 return dns46_request_inline (vm, node, frame, 0 /* is_ip6 */ );
274}
275
276/* *INDENT-OFF* */
277VLIB_REGISTER_NODE (dns4_request_node) =
278{
279 .function = dns4_request_node_fn,
280 .name = "dns4-request",
281 .vector_size = sizeof (u32),
282 .format_trace = format_dns46_request_trace,
283 .type = VLIB_NODE_TYPE_INTERNAL,
284 .n_errors = ARRAY_LEN (dns46_request_error_strings),
285 .error_strings = dns46_request_error_strings,
286 .n_next_nodes = DNS46_REQUEST_N_NEXT,
287 .next_nodes = {
288 [DNS46_REQUEST_NEXT_DROP] = "error-drop",
289 [DNS46_REQUEST_NEXT_IP_LOOKUP] = "ip4-lookup",
290 },
291};
292/* *INDENT-ON* */
293
294static uword
295dns6_request_node_fn (vlib_main_t * vm,
296 vlib_node_runtime_t * node, vlib_frame_t * frame)
297{
298
299 return dns46_request_inline (vm, node, frame, 1 /* is_ip6 */ );
300}
301
302/* *INDENT-OFF* */
303VLIB_REGISTER_NODE (dns6_request_node) =
304{
305 .function = dns6_request_node_fn,
306 .name = "dns6-request",
307 .vector_size = sizeof (u32),
308 .format_trace = format_dns46_request_trace,
309 .type = VLIB_NODE_TYPE_INTERNAL,
310 .n_errors = ARRAY_LEN (dns46_request_error_strings),
311 .error_strings = dns46_request_error_strings,
312 .n_next_nodes = DNS46_REQUEST_N_NEXT,
313 .next_nodes = {
314 [DNS46_REQUEST_NEXT_DROP] = "error-drop",
315 [DNS46_REQUEST_NEXT_IP_LOOKUP] = "ip6-lookup",
316 },
317};
318/* *INDENT-ON* */
319
320/*
321 * fd.io coding-style-patch-verification: ON
322 *
323 * Local Variables:
324 * eval: (c-set-style "gnu")
325 * End:
326 */