blob: ab871b3a9695e1f915ec5de349ace50959c9a849 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 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 * ip/icmp6.c: ip6 icmp
17 *
18 * Copyright (c) 2008 Eliot Dresselhaus
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
39
40#include <vlib/vlib.h>
41#include <vnet/ip/ip.h>
42#include <vnet/pg/pg.h>
43
Dave Barachd7cb1b52016-12-09 09:52:16 -050044static u8 *
45format_ip6_icmp_type_and_code (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070046{
47 icmp6_type_t type = va_arg (*args, int);
48 u8 code = va_arg (*args, int);
Dave Barachd7cb1b52016-12-09 09:52:16 -050049 char *t = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070050
51#define _(n,f) case n: t = #f; break;
52
53 switch (type)
54 {
55 foreach_icmp6_type;
56
57 default:
58 break;
59 }
60
61#undef _
62
Dave Barachd7cb1b52016-12-09 09:52:16 -050063 if (!t)
Ed Warnickecb9cada2015-12-08 15:45:58 -070064 return format (s, "unknown 0x%x", type);
65
66 s = format (s, "%s", t);
67
68 t = 0;
69 switch ((type << 8) | code)
70 {
71#define _(a,n,f) case (ICMP6_##a << 8) | (n): t = #f; break;
72
73 foreach_icmp6_code;
74
75#undef _
76 }
77
78 if (t)
79 s = format (s, " %s", t);
80
81 return s;
82}
83
Dave Barachd7cb1b52016-12-09 09:52:16 -050084static u8 *
85format_icmp6_header (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070086{
Dave Barachd7cb1b52016-12-09 09:52:16 -050087 icmp46_header_t *icmp = va_arg (*args, icmp46_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070088 u32 max_header_bytes = va_arg (*args, u32);
89
90 /* Nothing to do. */
91 if (max_header_bytes < sizeof (icmp[0]))
92 return format (s, "ICMP header truncated");
93
94 s = format (s, "ICMP %U checksum 0x%x",
95 format_ip6_icmp_type_and_code, icmp->type, icmp->code,
96 clib_net_to_host_u16 (icmp->checksum));
97
98 if (max_header_bytes >=
Dave Barachd7cb1b52016-12-09 09:52:16 -050099 sizeof (icmp6_neighbor_solicitation_or_advertisement_header_t) &&
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100 (icmp->type == ICMP6_neighbor_solicitation ||
101 icmp->type == ICMP6_neighbor_advertisement))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500102 {
103 icmp6_neighbor_solicitation_or_advertisement_header_t *icmp6_nd =
104 (icmp6_neighbor_solicitation_or_advertisement_header_t *) icmp;
105 s = format (s, "\n target address %U",
106 format_ip6_address, &icmp6_nd->target_address);
107 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108
109 return s;
110}
111
Dave Barachd7cb1b52016-12-09 09:52:16 -0500112u8 *
113format_icmp6_input_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114{
115 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
116 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500117 icmp6_input_trace_t *t = va_arg (*va, icmp6_input_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118
119 s = format (s, "%U",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500120 format_ip6_header, t->packet_data, sizeof (t->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121
122 return s;
123}
124
Dave Barachd7cb1b52016-12-09 09:52:16 -0500125static char *icmp_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126#define _(f,s) s,
127 foreach_icmp6_error
128#undef _
129};
130
Dave Barachd7cb1b52016-12-09 09:52:16 -0500131typedef enum
132{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133 ICMP_INPUT_NEXT_DROP,
134 ICMP_INPUT_N_NEXT,
135} icmp_input_next_t;
136
Dave Barachd7cb1b52016-12-09 09:52:16 -0500137typedef struct
138{
139 uword *type_and_code_by_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140
Dave Barachd7cb1b52016-12-09 09:52:16 -0500141 uword *type_by_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142
143 /* Vector dispatch table indexed by [icmp type]. */
144 u8 input_next_index_by_type[256];
145
146 /* Max valid code indexed by icmp type. */
147 u8 max_valid_code_by_type[256];
148
149 /* hop_limit must be >= this value for this icmp type. */
150 u8 min_valid_hop_limit_by_type[256];
151
152 u8 min_valid_length_by_type[256];
153} icmp6_main_t;
154
155icmp6_main_t icmp6_main;
156
157static uword
158ip6_icmp_input (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500159 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500161 icmp6_main_t *im = &icmp6_main;
162 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163 u32 n_left_from, n_left_to_next, next_index;
164
165 from = vlib_frame_vector_args (frame);
166 n_left_from = frame->n_vectors;
167 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500168
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 if (node->flags & VLIB_NODE_FLAG_TRACE)
170 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
171 /* stride */ 1,
172 sizeof (icmp6_input_trace_t));
173
174 while (n_left_from > 0)
175 {
176 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
177
178 while (n_left_from > 0 && n_left_to_next > 0)
179 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500180 vlib_buffer_t *b0;
181 ip6_header_t *ip0;
182 icmp46_header_t *icmp0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183 icmp6_type_t type0;
184 u32 bi0, next0, error0, len0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500185
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186 bi0 = to_next[0] = from[0];
187
188 from += 1;
189 n_left_from -= 1;
190 to_next += 1;
191 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500192
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193 b0 = vlib_get_buffer (vm, bi0);
194 ip0 = vlib_buffer_get_current (b0);
195 icmp0 = ip6_next_header (ip0);
196 type0 = icmp0->type;
197
198 error0 = ICMP6_ERROR_NONE;
199
200 next0 = im->input_next_index_by_type[type0];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500201 error0 =
202 next0 == ICMP_INPUT_NEXT_DROP ? ICMP6_ERROR_UNKNOWN_TYPE : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203
204 /* Check code is valid for type. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500205 error0 =
206 icmp0->code >
207 im->max_valid_code_by_type[type0] ?
208 ICMP6_ERROR_INVALID_CODE_FOR_TYPE : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209
210 /* Checksum is already validated by ip6_local node so we don't need to check that. */
211
212 /* Check that hop limit == 255 for certain types. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500213 error0 =
214 ip0->hop_limit <
215 im->min_valid_hop_limit_by_type[type0] ?
216 ICMP6_ERROR_INVALID_HOP_LIMIT_FOR_TYPE : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217
218 len0 = clib_net_to_host_u16 (ip0->payload_length);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500219 error0 =
220 len0 <
221 im->min_valid_length_by_type[type0] ?
222 ICMP6_ERROR_LENGTH_TOO_SMALL_FOR_TYPE : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223
224 b0->error = node->errors[error0];
225
226 next0 = error0 != ICMP6_ERROR_NONE ? ICMP_INPUT_NEXT_DROP : next0;
227
228 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
229 to_next, n_left_to_next,
230 bi0, next0);
231 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500232
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
234 }
235
236 return frame->n_vectors;
237}
238
Dave Barachd7cb1b52016-12-09 09:52:16 -0500239/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240VLIB_REGISTER_NODE (ip6_icmp_input_node) = {
241 .function = ip6_icmp_input,
242 .name = "ip6-icmp-input",
243
244 .vector_size = sizeof (u32),
245
246 .format_trace = format_icmp6_input_trace,
247
248 .n_errors = ARRAY_LEN (icmp_error_strings),
249 .error_strings = icmp_error_strings,
250
251 .n_next_nodes = 1,
252 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800253 [ICMP_INPUT_NEXT_DROP] = "ip6-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254 },
255};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500256/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257
Dave Barachd7cb1b52016-12-09 09:52:16 -0500258typedef enum
259{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260 ICMP6_ECHO_REQUEST_NEXT_LOOKUP,
261 ICMP6_ECHO_REQUEST_NEXT_OUTPUT,
262 ICMP6_ECHO_REQUEST_N_NEXT,
263} icmp6_echo_request_next_t;
264
265static uword
266ip6_icmp_echo_request (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500267 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500269 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270 u32 n_left_from, n_left_to_next, next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500271 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272
273 from = vlib_frame_vector_args (frame);
274 n_left_from = frame->n_vectors;
275 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500276
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277 if (node->flags & VLIB_NODE_FLAG_TRACE)
278 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
279 /* stride */ 1,
280 sizeof (icmp6_input_trace_t));
281
282 while (n_left_from > 0)
283 {
284 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
285
286 while (n_left_from > 2 && n_left_to_next > 2)
287 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500288 vlib_buffer_t *p0, *p1;
289 ip6_header_t *ip0, *ip1;
290 icmp46_header_t *icmp0, *icmp1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291 ip6_address_t tmp0, tmp1;
292 ip_csum_t sum0, sum1;
293 u32 bi0, bi1;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500294 u32 fib_index0, fib_index1;
295 u32 next0 = ICMP6_ECHO_REQUEST_NEXT_LOOKUP;
296 u32 next1 = ICMP6_ECHO_REQUEST_NEXT_LOOKUP;
297
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298 bi0 = to_next[0] = from[0];
299 bi1 = to_next[1] = from[1];
300
301 from += 2;
302 n_left_from -= 2;
303 to_next += 2;
304 n_left_to_next -= 2;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500305
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306 p0 = vlib_get_buffer (vm, bi0);
307 p1 = vlib_get_buffer (vm, bi1);
308 ip0 = vlib_buffer_get_current (p0);
309 ip1 = vlib_buffer_get_current (p1);
310 icmp0 = ip6_next_header (ip0);
311 icmp1 = ip6_next_header (ip1);
312
313 /* Check icmp type to echo reply and update icmp checksum. */
314 sum0 = icmp0->checksum;
315 sum1 = icmp1->checksum;
316
317 ASSERT (icmp0->type == ICMP6_echo_request);
318 ASSERT (icmp1->type == ICMP6_echo_request);
319 sum0 = ip_csum_update (sum0, ICMP6_echo_request, ICMP6_echo_reply,
320 icmp46_header_t, type);
321 sum1 = ip_csum_update (sum1, ICMP6_echo_request, ICMP6_echo_reply,
322 icmp46_header_t, type);
323
324 icmp0->checksum = ip_csum_fold (sum0);
325 icmp1->checksum = ip_csum_fold (sum1);
326
327 icmp0->type = ICMP6_echo_reply;
328 icmp1->type = ICMP6_echo_reply;
329
330 /* Swap source and destination address. */
331 tmp0 = ip0->src_address;
332 tmp1 = ip1->src_address;
333
334 ip0->src_address = ip0->dst_address;
335 ip1->src_address = ip1->dst_address;
336
337 ip0->dst_address = tmp0;
338 ip1->dst_address = tmp1;
339
340 /* New hop count. */
341 ip0->hop_limit = im->host_config.ttl;
342 ip1->hop_limit = im->host_config.ttl;
343
344 if (ip6_address_is_link_local_unicast (&ip0->dst_address))
345 {
346 ethernet_header_t *eth0;
347 u8 tmp_mac[6];
348 /* For link local, reuse current MAC header by sawpping
349 * SMAC to DMAC instead of IP6 lookup since link local
350 * is not in the IP6 FIB */
351 vlib_buffer_reset (p0);
352 eth0 = vlib_buffer_get_current (p0);
Damjan Marionf1213b82016-03-13 02:22:06 +0100353 clib_memcpy (tmp_mac, eth0->dst_address, 6);
354 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
355 clib_memcpy (eth0->src_address, tmp_mac, 6);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500356 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
357 vnet_buffer (p0)->sw_if_index[VLIB_RX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358 next0 = ICMP6_ECHO_REQUEST_NEXT_OUTPUT;
359 }
360 else
361 {
362 /* Determine the correct lookup fib indices... */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500363 fib_index0 = vec_elt (im->fib_index_by_sw_if_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700364 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
365 vnet_buffer (p0)->sw_if_index[VLIB_TX] = fib_index0;
366 }
367
368 if (ip6_address_is_link_local_unicast (&ip1->dst_address))
369 {
370 ethernet_header_t *eth1;
371 u8 tmp_mac[6];
372 /* For link local, reuse current MAC header by sawpping
373 * SMAC to DMAC instead of IP6 lookup since link local
374 * is not in the IP6 FIB */
375 vlib_buffer_reset (p1);
376 eth1 = vlib_buffer_get_current (p1);
Damjan Marionf1213b82016-03-13 02:22:06 +0100377 clib_memcpy (tmp_mac, eth1->dst_address, 6);
378 clib_memcpy (eth1->dst_address, eth1->src_address, 6);
379 clib_memcpy (eth1->src_address, tmp_mac, 6);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500380 vnet_buffer (p1)->sw_if_index[VLIB_TX] =
381 vnet_buffer (p1)->sw_if_index[VLIB_RX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700382 next1 = ICMP6_ECHO_REQUEST_NEXT_OUTPUT;
383 }
384 else
385 {
386 /* Determine the correct lookup fib indices... */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500387 fib_index1 = vec_elt (im->fib_index_by_sw_if_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388 vnet_buffer (p1)->sw_if_index[VLIB_RX]);
389 vnet_buffer (p1)->sw_if_index[VLIB_TX] = fib_index1;
390 }
391
Dave Barachd7cb1b52016-12-09 09:52:16 -0500392 vnet_buffer (p0)->sw_if_index[VLIB_RX]
393 = vnet_main.local_interface_sw_if_index;
394 vnet_buffer (p1)->sw_if_index[VLIB_RX]
395 = vnet_main.local_interface_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396
Dave Barachd7cb1b52016-12-09 09:52:16 -0500397 /* verify speculative enqueues, maybe switch current next frame */
398 /* if next0==next1==next_index then nothing special needs to be done */
399 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
400 to_next, n_left_to_next,
401 bi0, bi1, next0, next1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500403
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404 while (n_left_from > 0 && n_left_to_next > 0)
405 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500406 vlib_buffer_t *p0;
407 ip6_header_t *ip0;
408 icmp46_header_t *icmp0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409 u32 bi0;
410 ip6_address_t tmp0;
411 ip_csum_t sum0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500412 u32 fib_index0;
413 u32 next0 = ICMP6_ECHO_REQUEST_NEXT_LOOKUP;
414
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415 bi0 = to_next[0] = from[0];
416
417 from += 1;
418 n_left_from -= 1;
419 to_next += 1;
420 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500421
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422 p0 = vlib_get_buffer (vm, bi0);
423 ip0 = vlib_buffer_get_current (p0);
424 icmp0 = ip6_next_header (ip0);
425
426 /* Check icmp type to echo reply and update icmp checksum. */
427 sum0 = icmp0->checksum;
428
429 ASSERT (icmp0->type == ICMP6_echo_request);
430 sum0 = ip_csum_update (sum0, ICMP6_echo_request, ICMP6_echo_reply,
431 icmp46_header_t, type);
432
433 icmp0->checksum = ip_csum_fold (sum0);
434
435 icmp0->type = ICMP6_echo_reply;
436
437 /* Swap source and destination address. */
438 tmp0 = ip0->src_address;
439 ip0->src_address = ip0->dst_address;
440 ip0->dst_address = tmp0;
441
442 ip0->hop_limit = im->host_config.ttl;
443
444 if (ip6_address_is_link_local_unicast (&ip0->dst_address))
445 {
446 ethernet_header_t *eth0;
447 u8 tmp_mac[6];
448 /* For link local, reuse current MAC header by sawpping
449 * SMAC to DMAC instead of IP6 lookup since link local
450 * is not in the IP6 FIB */
451 vlib_buffer_reset (p0);
452 eth0 = vlib_buffer_get_current (p0);
Damjan Marionf1213b82016-03-13 02:22:06 +0100453 clib_memcpy (tmp_mac, eth0->dst_address, 6);
454 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
455 clib_memcpy (eth0->src_address, tmp_mac, 6);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500456 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
457 vnet_buffer (p0)->sw_if_index[VLIB_RX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700458 next0 = ICMP6_ECHO_REQUEST_NEXT_OUTPUT;
459 }
460 else
461 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500462 fib_index0 = vec_elt (im->fib_index_by_sw_if_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
464 vnet_buffer (p0)->sw_if_index[VLIB_TX] = fib_index0;
465 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500466 vnet_buffer (p0)->sw_if_index[VLIB_RX]
467 = vnet_main.local_interface_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468
Dave Barachd7cb1b52016-12-09 09:52:16 -0500469 /* Verify speculative enqueue, maybe switch current next frame */
470 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471 to_next, n_left_to_next,
472 bi0, next0);
473 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500474
Ed Warnickecb9cada2015-12-08 15:45:58 -0700475 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
476 }
477
478 vlib_error_count (vm, ip6_icmp_input_node.index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500479 ICMP6_ERROR_ECHO_REPLIES_SENT, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700480
481 return frame->n_vectors;
482}
483
Dave Barachd7cb1b52016-12-09 09:52:16 -0500484/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485VLIB_REGISTER_NODE (ip6_icmp_echo_request_node,static) = {
486 .function = ip6_icmp_echo_request,
487 .name = "ip6-icmp-echo-request",
488
489 .vector_size = sizeof (u32),
490
491 .format_trace = format_icmp6_input_trace,
492
493 .n_next_nodes = ICMP6_ECHO_REQUEST_N_NEXT,
494 .next_nodes = {
495 [ICMP6_ECHO_REQUEST_NEXT_LOOKUP] = "ip6-lookup",
496 [ICMP6_ECHO_REQUEST_NEXT_OUTPUT] = "interface-output",
497 },
498};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500499/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500
Dave Barachd7cb1b52016-12-09 09:52:16 -0500501typedef enum
502{
Ole Troancda94822016-01-07 14:37:25 +0100503 IP6_ICMP_ERROR_NEXT_DROP,
504 IP6_ICMP_ERROR_NEXT_LOOKUP,
505 IP6_ICMP_ERROR_N_NEXT,
506} ip6_icmp_error_next_t;
507
508void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500509icmp6_error_set_vnet_buffer (vlib_buffer_t * b, u8 type, u8 code, u32 data)
Ole Troancda94822016-01-07 14:37:25 +0100510{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500511 vnet_buffer (b)->ip.icmp.type = type;
512 vnet_buffer (b)->ip.icmp.code = code;
513 vnet_buffer (b)->ip.icmp.data = data;
Ole Troancda94822016-01-07 14:37:25 +0100514}
515
516static u8
517icmp6_icmp_type_to_error (u8 type)
518{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500519 switch (type)
520 {
521 case ICMP6_destination_unreachable:
522 return ICMP6_ERROR_DEST_UNREACH_SENT;
523 case ICMP6_packet_too_big:
524 return ICMP6_ERROR_PACKET_TOO_BIG_SENT;
525 case ICMP6_time_exceeded:
526 return ICMP6_ERROR_TTL_EXPIRE_SENT;
527 case ICMP6_parameter_problem:
528 return ICMP6_ERROR_PARAM_PROBLEM_SENT;
529 default:
530 return ICMP6_ERROR_DROP;
531 }
Ole Troancda94822016-01-07 14:37:25 +0100532}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533
534static uword
Ole Troancda94822016-01-07 14:37:25 +0100535ip6_icmp_error (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500536 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500538 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539 uword n_left_from, n_left_to_next;
Ole Troancda94822016-01-07 14:37:25 +0100540 ip6_icmp_error_next_t next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541 ip6_main_t *im = &ip6_main;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500542 ip_lookup_main_t *lm = &im->lookup_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543
Dave Barachd7cb1b52016-12-09 09:52:16 -0500544 from = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545 n_left_from = frame->n_vectors;
546 next_index = node->cached_next_index;
547
548 if (node->flags & VLIB_NODE_FLAG_TRACE)
549 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500550 /* stride */ 1,
551 sizeof (icmp6_input_trace_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700552
553 while (n_left_from > 0)
554 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500555 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700556
557 while (n_left_from > 0 && n_left_to_next > 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500558 {
559 u32 pi0 = from[0];
560 u32 next0 = IP6_ICMP_ERROR_NEXT_LOOKUP;
561 u8 error0 = ICMP6_ERROR_NONE;
562 vlib_buffer_t *p0;
563 ip6_header_t *ip0, *out_ip0;
564 icmp46_header_t *icmp0;
565 u32 sw_if_index0, if_add_index0;
566 int bogus_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700567
Dave Barachd7cb1b52016-12-09 09:52:16 -0500568 /* Speculatively enqueue p0 to the current next frame */
569 to_next[0] = pi0;
570 from += 1;
571 to_next += 1;
572 n_left_from -= 1;
573 n_left_to_next -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700574
Dave Barachd7cb1b52016-12-09 09:52:16 -0500575 p0 = vlib_get_buffer (vm, pi0);
576 ip0 = vlib_buffer_get_current (p0);
577 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700578
Dave Barachd7cb1b52016-12-09 09:52:16 -0500579 /* RFC4443 says to keep as much of the original packet as possible
580 * within the minimum MTU. We cheat "a little" here by keeping whatever fits
581 * in the first buffer, to be more efficient */
582 if (PREDICT_FALSE (p0->total_length_not_including_first_buffer))
583 { /* clear current_length of all other buffers in chain */
584 vlib_buffer_t *b = p0;
585 p0->total_length_not_including_first_buffer = 0;
586 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
587 {
588 b = vlib_get_buffer (vm, b->next_buffer);
589 b->current_length = 0;
590 }
591 }
592 p0->current_length =
593 p0->current_length > 1280 ? 1280 : p0->current_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700594
Dave Barachd7cb1b52016-12-09 09:52:16 -0500595 /* Add IP header and ICMPv6 header including a 4 byte data field */
596 vlib_buffer_advance (p0,
597 -sizeof (ip6_header_t) -
598 sizeof (icmp46_header_t) - 4);
599 out_ip0 = vlib_buffer_get_current (p0);
600 icmp0 = (icmp46_header_t *) & out_ip0[1];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700601
Dave Barachd7cb1b52016-12-09 09:52:16 -0500602 /* Fill ip header fields */
603 out_ip0->ip_version_traffic_class_and_flow_label =
604 clib_host_to_net_u32 (0x6 << 28);
Ole Troan9fb87552016-01-13 22:30:43 +0100605
Dave Barachd7cb1b52016-12-09 09:52:16 -0500606 out_ip0->payload_length =
607 clib_host_to_net_u16 (p0->current_length - sizeof (ip6_header_t));
608 out_ip0->protocol = IP_PROTOCOL_ICMP6;
609 out_ip0->hop_limit = 0xff;
610 out_ip0->dst_address = ip0->src_address;
611 if_add_index0 =
612 lm->if_address_pool_index_by_sw_if_index[sw_if_index0];
613 if (PREDICT_TRUE (if_add_index0 != ~0))
614 {
615 ip_interface_address_t *if_add =
616 pool_elt_at_index (lm->if_address_pool, if_add_index0);
617 ip6_address_t *if_ip =
618 ip_interface_address_get_address (lm, if_add);
619 out_ip0->src_address = *if_ip;
620 }
621 else /* interface has no IP6 address - should not happen */
622 {
623 next0 = IP6_ICMP_ERROR_NEXT_DROP;
624 error0 = ICMP6_ERROR_DROP;
625 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626
Dave Barachd7cb1b52016-12-09 09:52:16 -0500627 /* Fill icmp header fields */
628 icmp0->type = vnet_buffer (p0)->ip.icmp.type;
629 icmp0->code = vnet_buffer (p0)->ip.icmp.code;
630 *((u32 *) (icmp0 + 1)) =
631 clib_host_to_net_u32 (vnet_buffer (p0)->ip.icmp.data);
632 icmp0->checksum = 0;
633 icmp0->checksum =
634 ip6_tcp_udp_icmp_compute_checksum (vm, p0, out_ip0,
635 &bogus_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700636
Ole Troancda94822016-01-07 14:37:25 +0100637
638
Dave Barachd7cb1b52016-12-09 09:52:16 -0500639 /* Update error status */
Ole Troancda94822016-01-07 14:37:25 +0100640 if (error0 == ICMP6_ERROR_NONE)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500641 error0 = icmp6_icmp_type_to_error (icmp0->type);
642 vlib_error_count (vm, node->node_index, error0, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643
Dave Barachd7cb1b52016-12-09 09:52:16 -0500644 /* Verify speculative enqueue, maybe switch current next frame */
645 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
646 to_next, n_left_to_next,
647 pi0, next0);
648 }
649 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700650 }
651
652 return frame->n_vectors;
653}
654
Dave Barachd7cb1b52016-12-09 09:52:16 -0500655/* *INDENT-OFF* */
Ole Troancda94822016-01-07 14:37:25 +0100656VLIB_REGISTER_NODE (ip6_icmp_error_node) = {
657 .function = ip6_icmp_error,
658 .name = "ip6-icmp-error",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659 .vector_size = sizeof (u32),
660
661 .n_errors = ARRAY_LEN (icmp_error_strings),
662 .error_strings = icmp_error_strings,
663
Ole Troancda94822016-01-07 14:37:25 +0100664 .n_next_nodes = IP6_ICMP_ERROR_N_NEXT,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800666 [IP6_ICMP_ERROR_NEXT_DROP] = "ip6-drop",
Ole Troancda94822016-01-07 14:37:25 +0100667 [IP6_ICMP_ERROR_NEXT_LOOKUP] = "ip6-lookup",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668 },
669
670 .format_trace = format_icmp6_input_trace,
671};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500672/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673
674
Dave Barachd7cb1b52016-12-09 09:52:16 -0500675static uword
676unformat_icmp_type_and_code (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700677{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500678 icmp46_header_t *h = va_arg (*args, icmp46_header_t *);
679 icmp6_main_t *cm = &icmp6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700680 u32 i;
681
682 if (unformat_user (input, unformat_vlib_number_by_name,
683 cm->type_and_code_by_name, &i))
684 {
685 h->type = (i >> 8) & 0xff;
686 h->code = (i >> 0) & 0xff;
687 }
688 else if (unformat_user (input, unformat_vlib_number_by_name,
689 cm->type_by_name, &i))
690 {
691 h->type = i;
692 h->code = 0;
693 }
694 else
695 return 0;
696
697 return 1;
698}
699
700static void
701icmp6_pg_edit_function (pg_main_t * pg,
702 pg_stream_t * s,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500703 pg_edit_group_t * g, u32 * packets, u32 n_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700704{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500705 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700706 u32 ip_offset, icmp_offset;
707 int bogus_length;
708
709 icmp_offset = g->start_byte_offset;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500710 ip_offset = (g - 1)->start_byte_offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711
712 while (n_packets >= 1)
713 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500714 vlib_buffer_t *p0;
715 ip6_header_t *ip0;
716 icmp46_header_t *icmp0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717
718 p0 = vlib_get_buffer (vm, packets[0]);
719 n_packets -= 1;
720 packets += 1;
721
722 ASSERT (p0->current_data == 0);
723 ip0 = (void *) (p0->data + ip_offset);
724 icmp0 = (void *) (p0->data + icmp_offset);
725
Dave Barachd7cb1b52016-12-09 09:52:16 -0500726 icmp0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
727 &bogus_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728 ASSERT (bogus_length == 0);
729 }
730}
731
Dave Barachd7cb1b52016-12-09 09:52:16 -0500732typedef struct
733{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700734 pg_edit_t type, code;
735 pg_edit_t checksum;
736} pg_icmp46_header_t;
737
738always_inline void
739pg_icmp_header_init (pg_icmp46_header_t * p)
740{
741 /* Initialize fields that are not bit fields in the IP header. */
742#define _(f) pg_edit_init (&p->f, icmp46_header_t, f);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500743 _(type);
744 _(code);
745 _(checksum);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700746#undef _
747}
748
749static uword
750unformat_pg_icmp_header (unformat_input_t * input, va_list * args)
751{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500752 pg_stream_t *s = va_arg (*args, pg_stream_t *);
753 pg_icmp46_header_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700754 u32 group_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500755
Ed Warnickecb9cada2015-12-08 15:45:58 -0700756 p = pg_create_edit_group (s, sizeof (p[0]), sizeof (icmp46_header_t),
757 &group_index);
758 pg_icmp_header_init (p);
759
760 p->checksum.type = PG_EDIT_UNSPECIFIED;
761
762 {
763 icmp46_header_t tmp;
764
Dave Barachd7cb1b52016-12-09 09:52:16 -0500765 if (!unformat (input, "ICMP %U", unformat_icmp_type_and_code, &tmp))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700766 goto error;
767
768 pg_edit_set_fixed (&p->type, tmp.type);
769 pg_edit_set_fixed (&p->code, tmp.code);
770 }
771
772 /* Parse options. */
773 while (1)
774 {
775 if (unformat (input, "checksum %U",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500776 unformat_pg_edit, unformat_pg_number, &p->checksum))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777 ;
778
779 /* Can't parse input: try next protocol level. */
780 else
781 break;
782 }
783
Dave Barachd7cb1b52016-12-09 09:52:16 -0500784 if (!unformat_user (input, unformat_pg_payload, s))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700785 goto error;
786
787 if (p->checksum.type == PG_EDIT_UNSPECIFIED)
788 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500789 pg_edit_group_t *g = pg_stream_get_group (s, group_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700790 g->edit_function = icmp6_pg_edit_function;
791 g->edit_function_opaque = 0;
792 }
793
794 return 1;
795
Dave Barachd7cb1b52016-12-09 09:52:16 -0500796error:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700797 /* Free up any edits we may have added. */
798 pg_free_edit_group (s);
799 return 0;
800}
801
Dave Barachd7cb1b52016-12-09 09:52:16 -0500802void
803icmp6_register_type (vlib_main_t * vm, icmp6_type_t type, u32 node_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700804{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500805 icmp6_main_t *im = &icmp6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700806
Damjan Marion2c29d752015-12-18 10:26:56 +0100807 ASSERT ((int) type < ARRAY_LEN (im->input_next_index_by_type));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700808 im->input_next_index_by_type[type]
809 = vlib_node_add_next (vm, ip6_icmp_input_node.index, node_index);
810}
811
812static clib_error_t *
813icmp6_init (vlib_main_t * vm)
814{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500815 ip_main_t *im = &ip_main;
816 ip_protocol_info_t *pi;
817 icmp6_main_t *cm = &icmp6_main;
818 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700819
820 error = vlib_call_init_function (vm, ip_main_init);
821
822 if (error)
823 return error;
824
825 pi = ip_get_protocol_info (im, IP_PROTOCOL_ICMP6);
826 pi->format_header = format_icmp6_header;
827 pi->unformat_pg_edit = unformat_pg_icmp_header;
828
829 cm->type_by_name = hash_create_string (0, sizeof (uword));
830#define _(n,t) hash_set_mem (cm->type_by_name, #t, (n));
831 foreach_icmp6_type;
832#undef _
833
834 cm->type_and_code_by_name = hash_create_string (0, sizeof (uword));
835#define _(a,n,t) hash_set_mem (cm->type_by_name, #t, (n) | (ICMP6_##a << 8));
836 foreach_icmp6_code;
837#undef _
838
839 memset (cm->input_next_index_by_type,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500840 ICMP_INPUT_NEXT_DROP, sizeof (cm->input_next_index_by_type));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700841 memset (cm->max_valid_code_by_type, 0, sizeof (cm->max_valid_code_by_type));
842
843#define _(a,n,t) cm->max_valid_code_by_type[ICMP6_##a] = clib_max (cm->max_valid_code_by_type[ICMP6_##a], n);
844 foreach_icmp6_code;
845#undef _
846
Dave Barachd7cb1b52016-12-09 09:52:16 -0500847 memset (cm->min_valid_hop_limit_by_type, 0,
848 sizeof (cm->min_valid_hop_limit_by_type));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700849 cm->min_valid_hop_limit_by_type[ICMP6_router_solicitation] = 255;
850 cm->min_valid_hop_limit_by_type[ICMP6_router_advertisement] = 255;
851 cm->min_valid_hop_limit_by_type[ICMP6_neighbor_solicitation] = 255;
852 cm->min_valid_hop_limit_by_type[ICMP6_neighbor_advertisement] = 255;
853 cm->min_valid_hop_limit_by_type[ICMP6_redirect] = 255;
854
Dave Barachd7cb1b52016-12-09 09:52:16 -0500855 memset (cm->min_valid_length_by_type, sizeof (icmp46_header_t),
856 sizeof (cm->min_valid_length_by_type));
857 cm->min_valid_length_by_type[ICMP6_router_solicitation] =
858 sizeof (icmp6_neighbor_discovery_header_t);
859 cm->min_valid_length_by_type[ICMP6_router_advertisement] =
860 sizeof (icmp6_router_advertisement_header_t);
861 cm->min_valid_length_by_type[ICMP6_neighbor_solicitation] =
862 sizeof (icmp6_neighbor_solicitation_or_advertisement_header_t);
863 cm->min_valid_length_by_type[ICMP6_neighbor_advertisement] =
864 sizeof (icmp6_neighbor_solicitation_or_advertisement_header_t);
865 cm->min_valid_length_by_type[ICMP6_redirect] =
866 sizeof (icmp6_redirect_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700867
Dave Barachd7cb1b52016-12-09 09:52:16 -0500868 icmp6_register_type (vm, ICMP6_echo_request,
869 ip6_icmp_echo_request_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700870
871 return vlib_call_init_function (vm, ip6_neighbor_init);
872}
873
874VLIB_INIT_FUNCTION (icmp6_init);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500875
876/*
877 * fd.io coding-style-patch-verification: ON
878 *
879 * Local Variables:
880 * eval: (c-set-style "gnu")
881 * End:
882 */