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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * ip/icmp4.c: ipv4 icmp
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #include <vlib/vlib.h>
41 #include <vnet/ip/ip.h>
42 #include <vnet/pg/pg.h>
44 static u8 * format_ip4_icmp_type_and_code (u8 * s, va_list * args)
46 icmp4_type_t type = va_arg (*args, int);
47 u8 code = va_arg (*args, int);
50 #define _(n,f) case n: t = #f; break;
63 return format (s, "unknown 0x%x", type);
65 s = format (s, "%s", t);
68 switch ((type << 8) | code)
70 #define _(a,n,f) case (ICMP4_##a << 8) | (n): t = #f; break;
78 s = format (s, " %s", t);
83 static u8 * format_ip4_icmp_header (u8 * s, va_list * args)
85 icmp46_header_t * icmp = va_arg (*args, icmp46_header_t *);
86 u32 max_header_bytes = va_arg (*args, u32);
89 if (max_header_bytes < sizeof (icmp[0]))
90 return format (s, "ICMP header truncated");
92 s = format (s, "ICMP %U checksum 0x%x",
93 format_ip4_icmp_type_and_code, icmp->type, icmp->code,
94 clib_net_to_host_u16 (icmp->checksum));
101 } icmp_input_trace_t;
103 static u8 * format_icmp_input_trace (u8 * s, va_list * va)
105 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
106 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
107 icmp_input_trace_t * t = va_arg (*va, icmp_input_trace_t *);
111 t->packet_data, sizeof (t->packet_data));
117 ICMP4_ERROR_UNKNOWN_TYPE,
118 ICMP4_ERROR_ECHO_REPLIES_SENT,
119 ICMP4_ERROR_TTL_EXPIRE_RESP_SENT,
120 ICMP4_ERROR_TTL_EXPIRE_RESP_DROP,
123 static char * icmp_error_strings[] = {
124 [ICMP4_ERROR_UNKNOWN_TYPE] = "unknown type",
125 [ICMP4_ERROR_ECHO_REPLIES_SENT] = "echo replies sent",
126 [ICMP4_ERROR_TTL_EXPIRE_RESP_SENT] = "TTL time exceeded response sent",
127 [ICMP4_ERROR_TTL_EXPIRE_RESP_DROP] = "TTL time exceeded response dropped",
131 ICMP_INPUT_NEXT_ERROR,
136 uword * type_and_code_by_name;
138 uword * type_by_name;
140 /* Vector dispatch table indexed by [icmp type]. */
141 u8 ip4_input_next_index_by_type[256];
144 icmp4_main_t icmp4_main;
147 ip4_icmp_input (vlib_main_t * vm,
148 vlib_node_runtime_t * node,
149 vlib_frame_t * frame)
151 icmp4_main_t * im = &icmp4_main;
152 uword n_packets = frame->n_vectors;
153 u32 * from, * to_next;
154 u32 n_left_from, n_left_to_next, next;
156 from = vlib_frame_vector_args (frame);
157 n_left_from = n_packets;
158 next = node->cached_next_index;
160 if (node->flags & VLIB_NODE_FLAG_TRACE)
161 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
163 sizeof (icmp_input_trace_t));
165 while (n_left_from > 0)
167 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
169 while (n_left_from > 0 && n_left_to_next > 0)
173 icmp46_header_t * icmp0;
177 if (PREDICT_TRUE (n_left_from > 2))
179 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
180 p0 = vlib_get_buffer (vm, from[1]);
181 ip0 = vlib_buffer_get_current (p0);
182 CLIB_PREFETCH(ip0, CLIB_CACHE_LINE_BYTES, LOAD);
185 bi0 = to_next[0] = from[0];
192 p0 = vlib_get_buffer (vm, bi0);
193 ip0 = vlib_buffer_get_current (p0);
194 icmp0 = ip4_next_header (ip0);
196 next0 = im->ip4_input_next_index_by_type[type0];
198 p0->error = node->errors[ICMP4_ERROR_UNKNOWN_TYPE];
199 if (PREDICT_FALSE (next0 != next))
201 vlib_put_next_frame (vm, node, next, n_left_to_next + 1);
203 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
210 vlib_put_next_frame (vm, node, next, n_left_to_next);
213 return frame->n_vectors;
216 VLIB_REGISTER_NODE (ip4_icmp_input_node,static) = {
217 .function = ip4_icmp_input,
218 .name = "ip4-icmp-input",
220 .vector_size = sizeof (u32),
222 .format_trace = format_icmp_input_trace,
224 .n_errors = ARRAY_LEN (icmp_error_strings),
225 .error_strings = icmp_error_strings,
229 [ICMP_INPUT_NEXT_ERROR] = "error-punt",
234 ip4_icmp_echo_request (vlib_main_t * vm,
235 vlib_node_runtime_t * node,
236 vlib_frame_t * frame)
238 uword n_packets = frame->n_vectors;
239 u32 * from, * to_next;
240 u32 n_left_from, n_left_to_next, next;
241 ip4_main_t * i4m = &ip4_main;
242 u16 * fragment_ids, * fid;
243 u8 host_config_ttl = i4m->host_config.ttl;
245 from = vlib_frame_vector_args (frame);
246 n_left_from = n_packets;
247 next = node->cached_next_index;
249 if (node->flags & VLIB_NODE_FLAG_TRACE)
250 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
252 sizeof (icmp_input_trace_t));
254 /* Get random fragment IDs for replies. */
255 fid = fragment_ids = clib_random_buffer_get_data (&vm->random_buffer,
256 n_packets * sizeof (fragment_ids[0]));
258 while (n_left_from > 0)
260 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
262 while (n_left_from > 2 && n_left_to_next > 2)
264 vlib_buffer_t * p0, * p1;
265 ip4_header_t * ip0, * ip1;
266 icmp46_header_t * icmp0, * icmp1;
269 ip_csum_t sum0, sum1;
271 bi0 = to_next[0] = from[0];
272 bi1 = to_next[1] = from[1];
279 p0 = vlib_get_buffer (vm, bi0);
280 p1 = vlib_get_buffer (vm, bi1);
281 ip0 = vlib_buffer_get_current (p0);
282 ip1 = vlib_buffer_get_current (p1);
283 icmp0 = ip4_next_header (ip0);
284 icmp1 = ip4_next_header (ip1);
286 vnet_buffer (p0)->sw_if_index[VLIB_RX] = vnet_main.local_interface_sw_if_index;
287 vnet_buffer (p1)->sw_if_index[VLIB_RX] = vnet_main.local_interface_sw_if_index;
289 /* Update ICMP checksum. */
290 sum0 = icmp0->checksum;
291 sum1 = icmp1->checksum;
293 ASSERT (icmp0->type == ICMP4_echo_request);
294 ASSERT (icmp1->type == ICMP4_echo_request);
295 sum0 = ip_csum_update (sum0, ICMP4_echo_request, ICMP4_echo_reply,
296 icmp46_header_t, type);
297 sum1 = ip_csum_update (sum1, ICMP4_echo_request, ICMP4_echo_reply,
298 icmp46_header_t, type);
299 icmp0->type = ICMP4_echo_reply;
300 icmp1->type = ICMP4_echo_reply;
302 icmp0->checksum = ip_csum_fold (sum0);
303 icmp1->checksum = ip_csum_fold (sum1);
305 src0 = ip0->src_address.data_u32;
306 src1 = ip1->src_address.data_u32;
307 dst0 = ip0->dst_address.data_u32;
308 dst1 = ip1->dst_address.data_u32;
310 /* Swap source and destination address.
311 Does not change checksum. */
312 ip0->src_address.data_u32 = dst0;
313 ip1->src_address.data_u32 = dst1;
314 ip0->dst_address.data_u32 = src0;
315 ip1->dst_address.data_u32 = src1;
317 /* Update IP checksum. */
318 sum0 = ip0->checksum;
319 sum1 = ip1->checksum;
321 sum0 = ip_csum_update (sum0, ip0->ttl, host_config_ttl,
323 sum1 = ip_csum_update (sum1, ip1->ttl, host_config_ttl,
325 ip0->ttl = host_config_ttl;
326 ip1->ttl = host_config_ttl;
328 /* New fragment id. */
329 sum0 = ip_csum_update (sum0, ip0->fragment_id, fid[0],
330 ip4_header_t, fragment_id);
331 sum1 = ip_csum_update (sum1, ip1->fragment_id, fid[1],
332 ip4_header_t, fragment_id);
333 ip0->fragment_id = fid[0];
334 ip1->fragment_id = fid[1];
337 ip0->checksum = ip_csum_fold (sum0);
338 ip1->checksum = ip_csum_fold (sum1);
340 ASSERT (ip0->checksum == ip4_header_checksum (ip0));
341 ASSERT (ip1->checksum == ip4_header_checksum (ip1));
344 while (n_left_from > 0 && n_left_to_next > 0)
348 icmp46_header_t * icmp0;
352 bi0 = to_next[0] = from[0];
359 p0 = vlib_get_buffer (vm, bi0);
360 ip0 = vlib_buffer_get_current (p0);
361 icmp0 = ip4_next_header (ip0);
363 vnet_buffer (p0)->sw_if_index[VLIB_RX] = vnet_main.local_interface_sw_if_index;
365 /* Update ICMP checksum. */
366 sum0 = icmp0->checksum;
368 ASSERT (icmp0->type == ICMP4_echo_request);
369 sum0 = ip_csum_update (sum0, ICMP4_echo_request, ICMP4_echo_reply,
370 icmp46_header_t, type);
371 icmp0->type = ICMP4_echo_reply;
372 icmp0->checksum = ip_csum_fold (sum0);
374 src0 = ip0->src_address.data_u32;
375 dst0 = ip0->dst_address.data_u32;
376 ip0->src_address.data_u32 = dst0;
377 ip0->dst_address.data_u32 = src0;
379 /* Update IP checksum. */
380 sum0 = ip0->checksum;
382 sum0 = ip_csum_update (sum0, ip0->ttl, host_config_ttl,
384 ip0->ttl = host_config_ttl;
386 sum0 = ip_csum_update (sum0, ip0->fragment_id, fid[0],
387 ip4_header_t, fragment_id);
388 ip0->fragment_id = fid[0];
391 ip0->checksum = ip_csum_fold (sum0);
393 ASSERT (ip0->checksum == ip4_header_checksum (ip0));
396 vlib_put_next_frame (vm, node, next, n_left_to_next);
399 vlib_error_count (vm, ip4_icmp_input_node.index,
400 ICMP4_ERROR_ECHO_REPLIES_SENT,
403 return frame->n_vectors;
406 VLIB_REGISTER_NODE (ip4_icmp_echo_request_node,static) = {
407 .function = ip4_icmp_echo_request,
408 .name = "ip4-icmp-echo-request",
410 .vector_size = sizeof (u32),
412 .format_trace = format_icmp_input_trace,
416 [0] = "ip4-rewrite-local",
421 ICMP4_TTL_EXPIRE_NEXT_DROP,
422 ICMP4_TTL_EXPIRE_NEXT_LOOKUP,
423 ICMP4_TTL_EXPIRE_N_NEXT,
424 } icmp_ttl_expire_next_t;
427 ip4_icmp_ttl_expire (vlib_main_t * vm,
428 vlib_node_runtime_t * node,
429 vlib_frame_t * frame)
431 u32 * from, * to_next;
432 uword n_left_from, n_left_to_next;
433 icmp_ttl_expire_next_t next_index;
434 ip4_main_t *im = &ip4_main;
435 ip_lookup_main_t * lm = &im->lookup_main;
437 from = vlib_frame_vector_args(frame);
438 n_left_from = frame->n_vectors;
439 next_index = node->cached_next_index;
441 if (node->flags & VLIB_NODE_FLAG_TRACE)
442 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
443 /* stride */ 1, sizeof (icmp_input_trace_t));
445 while (n_left_from > 0)
447 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
449 while (n_left_from > 0 && n_left_to_next > 0)
452 u32 next0 = ICMP4_TTL_EXPIRE_NEXT_LOOKUP;
453 u8 error0 = ICMP4_ERROR_TTL_EXPIRE_RESP_SENT;
456 ip4_header_t * ip0, * out_ip0;
457 icmp46_header_t * icmp0;
459 u32 sw_if_index0, if_add_index0;
461 /* Speculatively enqueue p0 to the current next frame */
468 p0 = vlib_get_buffer(vm, pi0);
469 ip0 = vlib_buffer_get_current(p0);
470 len0 = vlib_buffer_length_in_chain (vm, p0);
471 sw_if_index0 = vnet_buffer(p0)->sw_if_index[VLIB_RX];
473 /* Cut payload to just IP header plus first 8 bytes */
474 new_len0 = (ip0->ip_version_and_header_length &0xf)*4 + 8;
477 p0->current_length = new_len0; /* should fit in 1st buffer */
478 if (PREDICT_FALSE(p0->total_length_not_including_first_buffer))
479 { /* clear current_length of all other buffers in chain */
480 vlib_buffer_t *b = p0;
481 p0->total_length_not_including_first_buffer = 0;
482 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
484 b = vlib_get_buffer (vm, b->next_buffer);
485 b->current_length = 0;
490 /* Add IP header and ICMP header including a 4 byte unused field */
491 vlib_buffer_advance(p0,
492 -sizeof(ip4_header_t)-sizeof(icmp46_header_t)-4);
493 out_ip0 = vlib_buffer_get_current(p0);
494 icmp0 = (icmp46_header_t *) &out_ip0[1];
496 /* Fill ip header fields */
497 out_ip0->ip_version_and_header_length = 0x45;
499 out_ip0->length = clib_host_to_net_u16(p0->current_length);
500 out_ip0->fragment_id = 0;
502 out_ip0->protocol = IP_PROTOCOL_ICMP;
503 out_ip0->dst_address = ip0->src_address;
505 lm->if_address_pool_index_by_sw_if_index[sw_if_index0];
506 if (PREDICT_TRUE(if_add_index0 != ~0))
508 ip_interface_address_t *if_add =
509 pool_elt_at_index(lm->if_address_pool, if_add_index0);
510 ip4_address_t *if_ip =
511 ip_interface_address_get_address(lm, if_add);
512 out_ip0->src_address = *if_ip;
513 vlib_error_count (vm, node->node_index, error0, 1);
515 else /* interface has no IP4 address - should not happen */
517 next0 = ICMP4_TTL_EXPIRE_NEXT_DROP;
518 error0 = ICMP4_ERROR_TTL_EXPIRE_RESP_DROP;
520 out_ip0->checksum = ip4_header_checksum(out_ip0);
522 /* Fill icmp header fields */
523 icmp0->type = ICMP4_time_exceeded;
524 icmp0->code = ICMP4_time_exceeded_ttl_exceeded_in_transit;
526 sum = ip_incremental_checksum(
527 0, icmp0, p0->current_length - sizeof(ip4_header_t));
528 icmp0->checksum = ~ip_csum_fold(sum);
530 /* Update error status */
531 p0->error = node->errors[error0];
533 /* Verify speculative enqueue, maybe switch current next frame */
534 vlib_validate_buffer_enqueue_x1(vm, node, next_index,
535 to_next, n_left_to_next,
538 vlib_put_next_frame(vm, node, next_index, n_left_to_next);
541 return frame->n_vectors;
544 VLIB_REGISTER_NODE (ip4_icmp_ttl_expire_node) = {
545 .function = ip4_icmp_ttl_expire,
546 .name = "ip4-icmp-ttl-expire",
547 .vector_size = sizeof (u32),
549 .n_errors = ARRAY_LEN (icmp_error_strings),
550 .error_strings = icmp_error_strings,
552 .n_next_nodes = ICMP4_TTL_EXPIRE_N_NEXT,
554 [ICMP4_TTL_EXPIRE_NEXT_DROP] = "error-drop",
555 [ICMP4_TTL_EXPIRE_NEXT_LOOKUP] = "ip4-lookup",
558 .format_trace = format_icmp_input_trace,
562 static uword unformat_icmp_type_and_code (unformat_input_t * input, va_list * args)
564 icmp46_header_t * h = va_arg (*args, icmp46_header_t *);
565 icmp4_main_t * cm = &icmp4_main;
568 if (unformat_user (input, unformat_vlib_number_by_name,
569 cm->type_and_code_by_name, &i))
571 h->type = (i >> 8) & 0xff;
572 h->code = (i >> 0) & 0xff;
574 else if (unformat_user (input, unformat_vlib_number_by_name,
575 cm->type_by_name, &i))
587 icmp4_pg_edit_function (pg_main_t * pg,
593 vlib_main_t * vm = pg->vlib_main;
594 u32 ip_offset, icmp_offset;
596 icmp_offset = g->start_byte_offset;
597 ip_offset = (g-1)->start_byte_offset;
599 while (n_packets >= 1)
603 icmp46_header_t * icmp0;
606 p0 = vlib_get_buffer (vm, packets[0]);
610 ASSERT (p0->current_data == 0);
611 ip0 = (void *) (p0->data + ip_offset);
612 icmp0 = (void *) (p0->data + icmp_offset);
613 len0 = clib_net_to_host_u16 (ip0->length) - ip4_header_bytes (ip0);
614 icmp0->checksum = ~ ip_csum_fold (ip_incremental_checksum (0, icmp0, len0));
619 pg_edit_t type, code;
621 } pg_icmp46_header_t;
624 pg_icmp_header_init (pg_icmp46_header_t * p)
626 /* Initialize fields that are not bit fields in the IP header. */
627 #define _(f) pg_edit_init (&p->f, icmp46_header_t, f);
635 unformat_pg_icmp_header (unformat_input_t * input, va_list * args)
637 pg_stream_t * s = va_arg (*args, pg_stream_t *);
638 pg_icmp46_header_t * p;
641 p = pg_create_edit_group (s, sizeof (p[0]), sizeof (icmp46_header_t),
643 pg_icmp_header_init (p);
645 p->checksum.type = PG_EDIT_UNSPECIFIED;
650 if (! unformat (input, "ICMP %U", unformat_icmp_type_and_code, &tmp))
653 pg_edit_set_fixed (&p->type, tmp.type);
654 pg_edit_set_fixed (&p->code, tmp.code);
660 if (unformat (input, "checksum %U",
662 unformat_pg_number, &p->checksum))
665 /* Can't parse input: try next protocol level. */
670 if (! unformat_user (input, unformat_pg_payload, s))
673 if (p->checksum.type == PG_EDIT_UNSPECIFIED)
675 pg_edit_group_t * g = pg_stream_get_group (s, group_index);
676 g->edit_function = icmp4_pg_edit_function;
677 g->edit_function_opaque = 0;
683 /* Free up any edits we may have added. */
684 pg_free_edit_group (s);
688 void ip4_icmp_register_type (vlib_main_t * vm, icmp4_type_t type,
691 icmp4_main_t * im = &icmp4_main;
693 ASSERT ((int)type < ARRAY_LEN (im->ip4_input_next_index_by_type));
694 im->ip4_input_next_index_by_type[type]
695 = vlib_node_add_next (vm, ip4_icmp_input_node.index, node_index);
698 static clib_error_t *
699 icmp4_init (vlib_main_t * vm)
701 ip_main_t * im = &ip_main;
702 ip_protocol_info_t * pi;
703 icmp4_main_t * cm = &icmp4_main;
704 clib_error_t * error;
706 error = vlib_call_init_function (vm, ip_main_init);
711 pi = ip_get_protocol_info (im, IP_PROTOCOL_ICMP);
712 pi->format_header = format_ip4_icmp_header;
713 pi->unformat_pg_edit = unformat_pg_icmp_header;
715 cm->type_by_name = hash_create_string (0, sizeof (uword));
716 #define _(n,t) hash_set_mem (cm->type_by_name, #t, (n));
720 cm->type_and_code_by_name = hash_create_string (0, sizeof (uword));
721 #define _(a,n,t) hash_set_mem (cm->type_by_name, #t, (n) | (ICMP4_##a << 8));
725 memset (cm->ip4_input_next_index_by_type,
726 ICMP_INPUT_NEXT_ERROR,
727 sizeof (cm->ip4_input_next_index_by_type));
729 ip4_icmp_register_type (vm, ICMP4_echo_request, ip4_icmp_echo_request_node.index);
734 VLIB_INIT_FUNCTION (icmp4_init);