blob: 68dbe759ebcc4af58b1facbb08e68e341b6933ee [file] [log] [blame]
Andrew Yourtchenko7e682202016-08-04 13:39:35 +00001/*
2 * Copyright (c) 2016 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/ip/ping.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010017#include <vnet/fib/ip6_fib.h>
18#include <vnet/fib/ip4_fib.h>
19#include <vnet/fib/fib_entry.h>
Andrew Yourtchenko7e682202016-08-04 13:39:35 +000020
Billy McFall0683c9c2016-10-13 08:27:31 -040021/**
22 * @file
23 * @brief IPv4 and IPv6 ICMP Ping.
24 *
25 * This file contains code to suppport IPv4 or IPv6 ICMP ECHO_REQUEST to
26 * network hosts.
27 *
28 */
29
30
Andrew Yourtchenko7e682202016-08-04 13:39:35 +000031u8 *
Alexander Popovskyc90dfdc2016-12-04 02:39:38 -080032format_icmp_echo_trace (u8 * s, va_list * va)
Andrew Yourtchenko7e682202016-08-04 13:39:35 +000033{
34 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
35 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
Alexander Popovskyc90dfdc2016-12-04 02:39:38 -080036 icmp_echo_trace_t *t = va_arg (*va, icmp_echo_trace_t *);
Andrew Yourtchenko7e682202016-08-04 13:39:35 +000037
Alexander Popovskyc90dfdc2016-12-04 02:39:38 -080038 s = format (s, "ICMP echo id %d seq %d%s",
Dave Barachd7cb1b52016-12-09 09:52:16 -050039 clib_net_to_host_u16 (t->id),
40 clib_net_to_host_u16 (t->seq), t->bound ? "" : " (unknown)");
Andrew Yourtchenko7e682202016-08-04 13:39:35 +000041
42 return s;
43}
44
45/*
46 * If we can find the ping run by an ICMP ID, then we send the signal
47 * to the CLI process referenced by that ping run, alongside with
48 * a freshly made copy of the packet.
49 * I opted for a packet copy to keep the main packet processing path
50 * the same as for all the other nodes.
51 *
52 */
53
Alexander Popovskyc90dfdc2016-12-04 02:39:38 -080054static int
Andrew Yourtchenko7e682202016-08-04 13:39:35 +000055signal_ip46_icmp_reply_event (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -050056 u8 event_type, vlib_buffer_t * b0)
Andrew Yourtchenko7e682202016-08-04 13:39:35 +000057{
58 ping_main_t *pm = &ping_main;
59 u16 net_icmp_id = 0;
60 u32 bi0_copy = 0;
61
62 switch (event_type)
63 {
64 case PING_RESPONSE_IP4:
65 {
Dave Barachd7cb1b52016-12-09 09:52:16 -050066 icmp4_echo_request_header_t *h0 = vlib_buffer_get_current (b0);
67 net_icmp_id = h0->icmp_echo.id;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +000068 }
69 break;
70 case PING_RESPONSE_IP6:
71 {
Dave Barachd7cb1b52016-12-09 09:52:16 -050072 icmp6_echo_request_header_t *h0 = vlib_buffer_get_current (b0);
73 net_icmp_id = h0->icmp_echo.id;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +000074 }
75 break;
76 default:
Alexander Popovskyc90dfdc2016-12-04 02:39:38 -080077 return 0;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +000078 }
79
80 uword *p = hash_get (pm->ping_run_by_icmp_id,
Dave Barachd7cb1b52016-12-09 09:52:16 -050081 clib_net_to_host_u16 (net_icmp_id));
Andrew Yourtchenko7e682202016-08-04 13:39:35 +000082 if (!p)
Alexander Popovskyc90dfdc2016-12-04 02:39:38 -080083 return 0;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +000084
85 ping_run_t *pr = vec_elt_at_index (pm->ping_runs, p[0]);
86 if (vlib_buffer_alloc (vm, &bi0_copy, 1) == 1)
87 {
88 void *dst = vlib_buffer_get_current (vlib_get_buffer (vm, bi0_copy));
89 clib_memcpy (dst, vlib_buffer_get_current (b0), b0->current_length);
90 }
91 /* If buffer_alloc failed, bi0_copy == 0 - just signaling an event. */
92
93 vlib_process_signal_event (vm, pr->cli_process_id, event_type, bi0_copy);
Alexander Popovskyc90dfdc2016-12-04 02:39:38 -080094 return 1;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +000095}
96
97/*
98 * Process ICMPv6 echo replies
99 */
100static uword
101ip6_icmp_echo_reply_node_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500102 vlib_node_runtime_t * node, vlib_frame_t * frame)
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000103{
104 u32 n_left_from, *from;
105
Dave Barachd7cb1b52016-12-09 09:52:16 -0500106 from = vlib_frame_vector_args (frame); /* array of buffer indices */
107 n_left_from = frame->n_vectors; /* number of buffer indices */
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000108
109 while (n_left_from > 0)
110 {
111 u32 bi0;
112 vlib_buffer_t *b0;
113 u32 next0;
114
115 bi0 = from[0];
116 b0 = vlib_get_buffer (vm, bi0);
117
Alexander Popovskyc90dfdc2016-12-04 02:39:38 -0800118 next0 = signal_ip46_icmp_reply_event (vm, PING_RESPONSE_IP6, b0) ?
Dave Barachd7cb1b52016-12-09 09:52:16 -0500119 ICMP6_ECHO_REPLY_NEXT_DROP : ICMP6_ECHO_REPLY_NEXT_PUNT;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000120
Dave Barachd7cb1b52016-12-09 09:52:16 -0500121 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
122 {
123 icmp6_echo_request_header_t *h0 = vlib_buffer_get_current (b0);
124 icmp_echo_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
125 tr->id = h0->icmp_echo.id;
126 tr->seq = h0->icmp_echo.seq;
127 tr->bound = (next0 == ICMP6_ECHO_REPLY_NEXT_DROP);
128 }
Alexander Popovskyc90dfdc2016-12-04 02:39:38 -0800129
130 /* push this pkt to the next graph node */
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000131 vlib_set_next_frame_buffer (vm, node, next0, bi0);
132
133 from += 1;
134 n_left_from -= 1;
135 }
136
137 return frame->n_vectors;
138}
139
140/* *INDENT-OFF* */
141VLIB_REGISTER_NODE (ip6_icmp_echo_reply_node, static) =
142{
143 .function = ip6_icmp_echo_reply_node_fn,
144 .name = "ip6-icmp-echo-reply",
145 .vector_size = sizeof (u32),
Alexander Popovskyc90dfdc2016-12-04 02:39:38 -0800146 .format_trace = format_icmp_echo_trace,
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000147 .n_next_nodes = ICMP6_ECHO_REPLY_N_NEXT,
148 .next_nodes = {
Alexander Popovskyc90dfdc2016-12-04 02:39:38 -0800149 [ICMP6_ECHO_REPLY_NEXT_DROP] = "error-drop",
150 [ICMP6_ECHO_REPLY_NEXT_PUNT] = "error-punt",
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000151 },
152};
153/* *INDENT-ON* */
154
155/*
156 * Process ICMPv4 echo replies
157 */
158static uword
159ip4_icmp_echo_reply_node_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500160 vlib_node_runtime_t * node, vlib_frame_t * frame)
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000161{
162 u32 n_left_from, *from;
163
Dave Barachd7cb1b52016-12-09 09:52:16 -0500164 from = vlib_frame_vector_args (frame); /* array of buffer indices */
165 n_left_from = frame->n_vectors; /* number of buffer indices */
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000166
167 while (n_left_from > 0)
168 {
169 u32 bi0;
170 vlib_buffer_t *b0;
171 u32 next0;
172
173 bi0 = from[0];
174 b0 = vlib_get_buffer (vm, bi0);
175
Alexander Popovskyc90dfdc2016-12-04 02:39:38 -0800176 next0 = signal_ip46_icmp_reply_event (vm, PING_RESPONSE_IP4, b0) ?
Dave Barachd7cb1b52016-12-09 09:52:16 -0500177 ICMP4_ECHO_REPLY_NEXT_DROP : ICMP4_ECHO_REPLY_NEXT_PUNT;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000178
Dave Barachd7cb1b52016-12-09 09:52:16 -0500179 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
180 {
181 icmp4_echo_request_header_t *h0 = vlib_buffer_get_current (b0);
182 icmp_echo_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
183 tr->id = h0->icmp_echo.id;
184 tr->seq = h0->icmp_echo.seq;
185 tr->bound = (next0 == ICMP4_ECHO_REPLY_NEXT_DROP);
186 }
Alexander Popovskyc90dfdc2016-12-04 02:39:38 -0800187
188 /* push this pkt to the next graph node */
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000189 vlib_set_next_frame_buffer (vm, node, next0, bi0);
190
191 from += 1;
192 n_left_from -= 1;
193 }
194
195 return frame->n_vectors;
196}
197
198/* *INDENT-OFF* */
199VLIB_REGISTER_NODE (ip4_icmp_echo_reply_node, static) =
200{
201 .function = ip4_icmp_echo_reply_node_fn,
202 .name = "ip4-icmp-echo-reply",
203 .vector_size = sizeof (u32),
Alexander Popovskyc90dfdc2016-12-04 02:39:38 -0800204 .format_trace = format_icmp_echo_trace,
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000205 .n_next_nodes = ICMP4_ECHO_REPLY_N_NEXT,
206 .next_nodes = {
Alexander Popovskyc90dfdc2016-12-04 02:39:38 -0800207 [ICMP4_ECHO_REPLY_NEXT_DROP] = "error-drop",
208 [ICMP4_ECHO_REPLY_NEXT_PUNT] = "error-punt",
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000209 },
210};
211/* *INDENT-ON* */
212
213char *ip6_lookup_next_nodes[] = IP6_LOOKUP_NEXT_NODES;
214char *ip4_lookup_next_nodes[] = IP4_LOOKUP_NEXT_NODES;
215
216/* get first interface address */
217static ip6_address_t *
218ip6_interface_first_address (ip6_main_t * im, u32 sw_if_index)
219{
220 ip_lookup_main_t *lm = &im->lookup_main;
221 ip_interface_address_t *ia = 0;
222 ip6_address_t *result = 0;
223
Dave Barachd7cb1b52016-12-09 09:52:16 -0500224 /* *INDENT-OFF* */
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000225 foreach_ip_interface_address (lm, ia, sw_if_index,
226 1 /* honor unnumbered */ ,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500227 ({
228 ip6_address_t * a =
229 ip_interface_address_get_address (lm, ia);
230 result = a;
231 break;
232 }));
233 /* *INDENT-ON* */
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000234 return result;
235}
236
237/* Fill in the ICMP ECHO structure, return the safety-checked and possibly shrunk data_len */
238static u16
239init_icmp46_echo_request (icmp46_echo_request_t * icmp46_echo,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500240 u16 seq_host, u16 id_host, u16 data_len)
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000241{
242 int i;
243 icmp46_echo->seq = clib_host_to_net_u16 (seq_host);
244 icmp46_echo->id = clib_host_to_net_u16 (id_host);
245
246 for (i = 0; i < sizeof (icmp46_echo->data); i++)
247 {
248 icmp46_echo->data[i] = i % 256;
249 }
250
251 if (data_len > sizeof (icmp46_echo_request_t))
252 {
253 data_len = sizeof (icmp46_echo_request_t);
254 }
255 return data_len;
256}
257
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000258static send_ip46_ping_result_t
Neale Rannsf06aea52016-11-29 06:51:37 -0800259send_ip6_ping (vlib_main_t * vm, ip6_main_t * im,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500260 u32 table_id, ip6_address_t * pa6,
261 u32 sw_if_index, u16 seq_host, u16 id_host, u16 data_len,
262 u8 verbose)
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000263{
264 icmp6_echo_request_header_t *h0;
265 u32 bi0 = 0;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000266 int bogus_length = 0;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000267 vlib_buffer_t *p0;
268 vlib_frame_t *f;
269 u32 *to_next;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000270
271 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
272 return SEND_PING_ALLOC_FAIL;
273
274 p0 = vlib_get_buffer (vm, bi0);
275
Neale Rannsf06aea52016-11-29 06:51:37 -0800276 /*
277 * if the user did not provide a source interface, use the any interface
278 * that the destination resolves via.
279 */
280 if (~0 == sw_if_index)
281 {
282 fib_node_index_t fib_entry_index;
283 u32 fib_index;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100284
Dave Barachd7cb1b52016-12-09 09:52:16 -0500285 fib_index = ip6_fib_index_from_table_id (table_id);
Neale Rannsf06aea52016-11-29 06:51:37 -0800286
287 if (~0 == fib_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500288 {
289 vlib_buffer_free (vm, &bi0, 1);
290 return SEND_PING_NO_TABLE;
291 }
Neale Rannsf06aea52016-11-29 06:51:37 -0800292
Dave Barachd7cb1b52016-12-09 09:52:16 -0500293 fib_entry_index = ip6_fib_table_lookup (fib_index, pa6, 128);
294 sw_if_index = fib_entry_get_resolving_interface (fib_entry_index);
Neale Rannsf06aea52016-11-29 06:51:37 -0800295 /*
296 * Set the TX interface to force ip-lookup to use its table ID
297 */
298 vnet_buffer (p0)->sw_if_index[VLIB_TX] = fib_index;
299 }
300 else
301 {
302 /*
303 * force an IP lookup in the table bound to the user's chosen
304 * source interface.
305 */
306 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500307 ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
Neale Rannsf06aea52016-11-29 06:51:37 -0800308 }
309
310 if (~0 == sw_if_index)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100311 {
312 vlib_buffer_free (vm, &bi0, 1);
313 return SEND_PING_NO_INTERFACE;
314 }
315
Neale Rannsf06aea52016-11-29 06:51:37 -0800316 vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000317
318 h0 = vlib_buffer_get_current (p0);
319
320 /* Fill in ip6 header fields */
321 h0->ip6.ip_version_traffic_class_and_flow_label =
322 clib_host_to_net_u32 (0x6 << 28);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500323 h0->ip6.payload_length = 0; /* Set below */
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000324 h0->ip6.protocol = IP_PROTOCOL_ICMP6;
325 h0->ip6.hop_limit = 255;
326 h0->ip6.dst_address = *pa6;
327 h0->ip6.src_address = *pa6;
328
329 /* Fill in the correct source now */
Neale Rannsf06aea52016-11-29 06:51:37 -0800330 ip6_address_t *a = ip6_interface_first_address (im, sw_if_index);
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000331 h0->ip6.src_address = a[0];
332
333 /* Fill in icmp fields */
334 h0->icmp.type = ICMP6_echo_request;
335 h0->icmp.code = 0;
336 h0->icmp.checksum = 0;
337
338 data_len =
339 init_icmp46_echo_request (&h0->icmp_echo, seq_host, id_host, data_len);
340 h0->icmp_echo.time_sent = vlib_time_now (vm);
341
342 /* Fix up the lengths */
343 h0->ip6.payload_length =
344 clib_host_to_net_u16 (data_len + sizeof (icmp46_header_t));
345
346 p0->current_length = clib_net_to_host_u16 (h0->ip6.payload_length) +
347 STRUCT_OFFSET_OF (icmp6_echo_request_header_t, icmp);
348
349 /* Calculate the ICMP checksum */
350 h0->icmp.checksum = 0;
351 h0->icmp.checksum =
352 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip6, &bogus_length);
353
354 /* Enqueue the packet right now */
355 f = vlib_get_frame_to_node (vm, ip6_lookup_node.index);
356 to_next = vlib_frame_vector_args (f);
357 to_next[0] = bi0;
358 f->n_vectors = 1;
359 vlib_put_frame_to_node (vm, ip6_lookup_node.index, f);
360
361 return SEND_PING_OK;
362}
363
364static send_ip46_ping_result_t
365send_ip4_ping (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500366 ip4_main_t * im,
367 u32 table_id,
368 ip4_address_t * pa4,
369 u32 sw_if_index,
370 u16 seq_host, u16 id_host, u16 data_len, u8 verbose)
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000371{
372 icmp4_echo_request_header_t *h0;
373 u32 bi0 = 0;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000374 ip_lookup_main_t *lm = &im->lookup_main;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000375 vlib_buffer_t *p0;
376 vlib_frame_t *f;
377 u32 *to_next;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000378 u32 if_add_index0;
379
380 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
381 return SEND_PING_ALLOC_FAIL;
382
383 p0 = vlib_get_buffer (vm, bi0);
384
Neale Rannsf06aea52016-11-29 06:51:37 -0800385 /*
386 * if the user did not provide a source interface, use the any interface
387 * that the destination resolves via.
388 */
389 if (~0 == sw_if_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500390 {
Neale Rannsf06aea52016-11-29 06:51:37 -0800391 fib_node_index_t fib_entry_index;
392 u32 fib_index;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100393
Dave Barachd7cb1b52016-12-09 09:52:16 -0500394 fib_index = ip4_fib_index_from_table_id (table_id);
Neale Rannsf06aea52016-11-29 06:51:37 -0800395
396 if (~0 == fib_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500397 {
398 vlib_buffer_free (vm, &bi0, 1);
399 return SEND_PING_NO_TABLE;
400 }
Neale Rannsf06aea52016-11-29 06:51:37 -0800401
Dave Barachd7cb1b52016-12-09 09:52:16 -0500402 fib_entry_index =
403 ip4_fib_table_lookup (ip4_fib_get (fib_index), pa4, 32);
404 sw_if_index = fib_entry_get_resolving_interface (fib_entry_index);
Neale Rannsf06aea52016-11-29 06:51:37 -0800405 /*
406 * Set the TX interface to force ip-lookup to use the user's table ID
407 */
408 vnet_buffer (p0)->sw_if_index[VLIB_TX] = fib_index;
409 }
410 else
411 {
412 /*
413 * force an IP lookup in the table bound to the user's chosen
414 * source interface.
415 */
416 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500417 ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
Neale Rannsf06aea52016-11-29 06:51:37 -0800418 }
419
420 if (~0 == sw_if_index)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100421 {
422 vlib_buffer_free (vm, &bi0, 1);
423 return SEND_PING_NO_INTERFACE;
424 }
425
Neale Rannsf06aea52016-11-29 06:51:37 -0800426 vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000427
428 h0 = vlib_buffer_get_current (p0);
429
430 /* Fill in ip4 header fields */
431 h0->ip4.checksum = 0;
432 h0->ip4.ip_version_and_header_length = 0x45;
433 h0->ip4.tos = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500434 h0->ip4.length = 0; /* Set below */
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000435 h0->ip4.fragment_id = 0;
436 h0->ip4.flags_and_fragment_offset = 0;
437 h0->ip4.ttl = 0xff;
438 h0->ip4.protocol = IP_PROTOCOL_ICMP;
439 h0->ip4.dst_address = *pa4;
440 h0->ip4.src_address = *pa4;
441
442 /* Fill in the correct source now */
Neale Rannsf06aea52016-11-29 06:51:37 -0800443 if_add_index0 = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000444 if (PREDICT_TRUE (if_add_index0 != ~0))
445 {
446 ip_interface_address_t *if_add =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500447 pool_elt_at_index (lm->if_address_pool, if_add_index0);
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000448 ip4_address_t *if_ip = ip_interface_address_get_address (lm, if_add);
449 h0->ip4.src_address = *if_ip;
450 if (verbose)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500451 {
452 vlib_cli_output (vm, "Source address: %U",
453 format_ip4_address, &h0->ip4.src_address);
454 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000455 }
456
457 /* Fill in icmp fields */
458 h0->icmp.type = ICMP4_echo_request;
459 h0->icmp.code = 0;
460 h0->icmp.checksum = 0;
461
462 data_len =
463 init_icmp46_echo_request (&h0->icmp_echo, seq_host, id_host, data_len);
464 h0->icmp_echo.time_sent = vlib_time_now (vm);
465
466 /* Fix up the lengths */
467 h0->ip4.length =
468 clib_host_to_net_u16 (data_len + sizeof (icmp46_header_t) +
Dave Barachd7cb1b52016-12-09 09:52:16 -0500469 sizeof (ip4_header_t));
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000470
471 p0->current_length = clib_net_to_host_u16 (h0->ip4.length);
472
473 /* Calculate the IP and ICMP checksums */
474 h0->ip4.checksum = ip4_header_checksum (&(h0->ip4));
475 h0->icmp.checksum =
476 ~ip_csum_fold (ip_incremental_checksum (0, &(h0->icmp),
Dave Barachd7cb1b52016-12-09 09:52:16 -0500477 p0->current_length -
478 sizeof (ip4_header_t)));
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000479
480 /* Enqueue the packet right now */
481 f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
482 to_next = vlib_frame_vector_args (f);
483 to_next[0] = bi0;
484 f->n_vectors = 1;
485 vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
486
487 return SEND_PING_OK;
488}
489
490
491static void
492print_ip6_icmp_reply (vlib_main_t * vm, u32 bi0)
493{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500494 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000495 icmp6_echo_request_header_t *h0 = vlib_buffer_get_current (b0);
496 f64 rtt = vlib_time_now (vm) - h0->icmp_echo.time_sent;
497
498 vlib_cli_output (vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500499 "%d bytes from %U: icmp_seq=%d ttl=%d time=%.4f ms",
500 clib_host_to_net_u16 (h0->ip6.payload_length),
501 format_ip6_address,
502 &h0->ip6.src_address,
503 clib_host_to_net_u16 (h0->icmp_echo.seq),
504 h0->ip6.hop_limit, rtt * 1000.0);
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000505}
506
507static void
508print_ip4_icmp_reply (vlib_main_t * vm, u32 bi0)
509{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500510 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000511 icmp4_echo_request_header_t *h0 = vlib_buffer_get_current (b0);
512 f64 rtt = vlib_time_now (vm) - h0->icmp_echo.time_sent;
513 u32 rcvd_icmp_len =
514 clib_host_to_net_u16 (h0->ip4.length) -
515 (4 * (0xF & h0->ip4.ip_version_and_header_length));
516
517 vlib_cli_output (vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500518 "%d bytes from %U: icmp_seq=%d ttl=%d time=%.4f ms",
519 rcvd_icmp_len,
520 format_ip4_address,
521 &h0->ip4.src_address,
522 clib_host_to_net_u16 (h0->icmp_echo.seq),
523 h0->ip4.ttl, rtt * 1000.0);
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000524}
525
526
527/*
528 * Perform the ping run with the given parameters in the current CLI process.
529 * Depending on whether pa4 or pa6 is set, runs IPv4 or IPv6 ping.
530 * The amusing side effect is of course if both are set, then both pings are sent.
531 * This behavior can be used to ping a dualstack host over IPv4 and IPv6 at once.
532 */
533
534static void
Neale Rannsf06aea52016-11-29 06:51:37 -0800535run_ping_ip46_address (vlib_main_t * vm, u32 table_id, ip4_address_t * pa4,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500536 ip6_address_t * pa6, u32 sw_if_index,
537 f64 ping_interval, u32 ping_repeat, u32 data_len,
538 u32 verbose)
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000539{
540 int i;
541 ping_main_t *pm = &ping_main;
542 uword curr_proc = vlib_current_process (vm);
543 u32 n_replies = 0;
544 u32 n_requests = 0;
545 ping_run_t *pr = 0;
546 u32 ping_run_index = 0;
Andrew Yourtchenkoacfb47d2016-09-14 15:51:16 +0000547 u16 icmp_id;
548
549 static u32 rand_seed = 0;
550
Dave Barachd7cb1b52016-12-09 09:52:16 -0500551 if (PREDICT_FALSE (!rand_seed))
552 rand_seed = random_default_seed ();
Andrew Yourtchenkoacfb47d2016-09-14 15:51:16 +0000553
Dave Barachd7cb1b52016-12-09 09:52:16 -0500554 icmp_id = random_u32 (&rand_seed) & 0xffff;
Andrew Yourtchenkoacfb47d2016-09-14 15:51:16 +0000555
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000556 while (hash_get (pm->ping_run_by_icmp_id, icmp_id))
557 {
558 vlib_cli_output (vm, "ICMP ID collision at %d, incrementing", icmp_id);
559 icmp_id++;
560 }
561 pool_get (pm->ping_runs, pr);
562 ping_run_index = pr - pm->ping_runs;
563 pr->cli_process_id = curr_proc;
564 pr->icmp_id = icmp_id;
565 hash_set (pm->ping_run_by_icmp_id, icmp_id, ping_run_index);
566 for (i = 1; i <= ping_repeat; i++)
567 {
568 f64 sleep_interval;
569 f64 time_ping_sent = vlib_time_now (vm);
570 /* Reset pr: running ping in other process could have changed pm->ping_runs */
571 pr = vec_elt_at_index (pm->ping_runs, ping_run_index);
572 pr->curr_seq = i;
573 if (pa6 &&
Dave Barachd7cb1b52016-12-09 09:52:16 -0500574 (SEND_PING_OK ==
575 send_ip6_ping (vm, ping_main.ip6_main, table_id, pa6, sw_if_index,
576 i, icmp_id, data_len, verbose)))
577 {
578 n_requests++;
579 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000580 if (pa4 &&
Dave Barachd7cb1b52016-12-09 09:52:16 -0500581 (SEND_PING_OK ==
582 send_ip4_ping (vm, ping_main.ip4_main, table_id, pa4, sw_if_index,
583 i, icmp_id, data_len, verbose)))
584 {
585 n_requests++;
586 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000587 while ((i <= ping_repeat)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500588 &&
589 ((sleep_interval =
590 time_ping_sent + ping_interval - vlib_time_now (vm)) > 0.0))
591 {
592 uword event_type, *event_data = 0;
593 vlib_process_wait_for_event_or_clock (vm, sleep_interval);
594 event_type = vlib_process_get_events (vm, &event_data);
595 switch (event_type)
596 {
597 case ~0: /* no events => timeout */
598 break;
599 case PING_RESPONSE_IP6:
600 {
601 int i;
602 for (i = 0; i < vec_len (event_data); i++)
603 {
604 u32 bi0 = event_data[0];
605 print_ip6_icmp_reply (vm, bi0);
606 n_replies++;
607 if (0 != bi0)
608 {
609 vlib_buffer_free (vm, &bi0, 1);
610 }
611 }
612 }
613 break;
614 case PING_RESPONSE_IP4:
615 {
616 int i;
617 for (i = 0; i < vec_len (event_data); i++)
618 {
619 u32 bi0 = event_data[0];
620 print_ip4_icmp_reply (vm, bi0);
621 n_replies++;
622 if (0 != bi0)
623 {
624 vlib_buffer_free (vm, &bi0, 1);
625 }
626 }
627 }
628 break;
629 default:
630 /* someone pressed a key, abort */
631 vlib_cli_output (vm, "Aborted due to a keypress.");
632 i = 1 + ping_repeat;
633 break;
634 }
635 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000636 }
637 vlib_cli_output (vm, "\n");
638 {
639 float loss =
640 (0 ==
641 n_requests) ? 0 : 100.0 * ((float) n_requests -
Dave Barachd7cb1b52016-12-09 09:52:16 -0500642 (float) n_replies) / (float) n_requests;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000643 vlib_cli_output (vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500644 "Statistics: %u sent, %u received, %f%% packet loss\n",
645 n_requests, n_replies, loss);
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000646 /* Reset pr: running ping in other process could have changed pm->ping_runs */
647 pr = vec_elt_at_index (pm->ping_runs, ping_run_index);
648 hash_unset (pm->ping_run_by_icmp_id, icmp_id);
649 pool_put (pm->ping_runs, pr);
650 }
651}
652
653
654
655
656
657static clib_error_t *
658ping_ip_address (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500659 unformat_input_t * input, vlib_cli_command_t * cmd)
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000660{
661 ip4_address_t a4;
662 ip6_address_t a6;
663 clib_error_t *error = 0;
664 u32 ping_repeat = 5;
665 u8 ping_ip4, ping_ip6;
666 vnet_main_t *vnm = vnet_get_main ();
667 u32 data_len = PING_DEFAULT_DATA_LEN;
668 u32 verbose = 0;
669 f64 ping_interval = PING_DEFAULT_INTERVAL;
Neale Rannsf06aea52016-11-29 06:51:37 -0800670 u32 sw_if_index, table_id;
671
672 table_id = 0;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000673 ping_ip4 = ping_ip6 = 0;
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000674 sw_if_index = ~0;
Neale Rannsf06aea52016-11-29 06:51:37 -0800675
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000676 if (unformat (input, "%U", unformat_ip4_address, &a4))
677 {
678 ping_ip4 = 1;
679 }
680 else if (unformat (input, "%U", unformat_ip6_address, &a6))
681 {
682 ping_ip6 = 1;
683 }
684 else if (unformat (input, "ipv4"))
685 {
686 if (unformat (input, "%U", unformat_ip4_address, &a4))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500687 {
688 ping_ip4 = 1;
689 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000690 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500691 {
692 error =
693 clib_error_return (0,
694 "expecting IPv4 address but got `%U'",
695 format_unformat_error, input);
696 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000697 }
698 else if (unformat (input, "ipv6"))
699 {
700 if (unformat (input, "%U", unformat_ip6_address, &a6))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500701 {
702 ping_ip6 = 1;
703 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000704 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500705 {
706 error =
707 clib_error_return (0,
708 "expecting IPv6 address but got `%U'",
709 format_unformat_error, input);
710 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000711 }
712 else
713 {
714 error =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500715 clib_error_return (0,
716 "expecting IP4/IP6 address `%U'. Usage: ping <addr> [source <intf>] [size <datasz>] [repeat <count>] [verbose]",
717 format_unformat_error, input);
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000718 goto done;
719 }
720
721 /* allow for the second AF in the same ping */
722 if (!ping_ip4 && (unformat (input, "ipv4")))
723 {
724 if (unformat (input, "%U", unformat_ip4_address, &a4))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500725 {
726 ping_ip4 = 1;
727 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000728 }
729 else if (!ping_ip6 && (unformat (input, "ipv6")))
730 {
731 if (unformat (input, "%U", unformat_ip6_address, &a6))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500732 {
733 ping_ip6 = 1;
734 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000735 }
736
737 /* parse the rest of the parameters in a cycle */
738 while (!unformat_eof (input, NULL))
739 {
740 if (unformat (input, "source"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500741 {
742 if (!unformat_user
743 (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
744 {
745 error =
746 clib_error_return (0,
747 "unknown interface `%U'",
748 format_unformat_error, input);
749 goto done;
750 }
751 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000752 else if (unformat (input, "size"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500753 {
754 if (!unformat (input, "%u", &data_len))
755 {
756 error =
757 clib_error_return (0,
758 "expecting size but got `%U'",
759 format_unformat_error, input);
760 goto done;
761 }
762 }
Neale Rannsf06aea52016-11-29 06:51:37 -0800763 else if (unformat (input, "table-id"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500764 {
765 if (!unformat (input, "du", &table_id))
766 {
767 error =
768 clib_error_return (0,
769 "expecting table-id but got `%U'",
770 format_unformat_error, input);
771 goto done;
772 }
773 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000774 else if (unformat (input, "interval"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500775 {
776 if (!unformat (input, "%f", &ping_interval))
777 {
778 error =
779 clib_error_return (0,
780 "expecting interval (floating point number) got `%U'",
781 format_unformat_error, input);
782 goto done;
783 }
784 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000785 else if (unformat (input, "repeat"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500786 {
787 if (!unformat (input, "%u", &ping_repeat))
788 {
789 error =
790 clib_error_return (0,
791 "expecting repeat count but got `%U'",
792 format_unformat_error, input);
793 goto done;
794 }
795 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000796 else if (unformat (input, "verbose"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500797 {
798 verbose = 1;
799 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000800 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500801 {
802 error = clib_error_return (0, "unknown input `%U'",
803 format_unformat_error, input);
804 goto done;
805 }
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000806 }
807
Dave Barachd7cb1b52016-12-09 09:52:16 -0500808 run_ping_ip46_address (vm, table_id, ping_ip4 ? &a4 : NULL,
809 ping_ip6 ? &a6 : NULL, sw_if_index, ping_interval,
810 ping_repeat, data_len, verbose);
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000811done:
812 return error;
813}
814
Billy McFall0683c9c2016-10-13 08:27:31 -0400815/*?
816 * This command sends an ICMP ECHO_REQUEST to network hosts. The address
817 * can be an IPv4 or IPv6 address (or both at the same time).
818 *
819 * @cliexpar
820 * @parblock
821 * Example of how ping an IPv4 address:
822 * @cliexstart{ping 172.16.1.2 source GigabitEthernet2/0/0 repeat 2}
823 * 64 bytes from 172.16.1.2: icmp_seq=1 ttl=64 time=.1090 ms
824 * 64 bytes from 172.16.1.2: icmp_seq=2 ttl=64 time=.0914 ms
825 *
826 * Statistics: 2 sent, 2 received, 0% packet loss
827 * @cliexend
828 *
829 * Example of how ping both an IPv4 address and IPv6 address at the same time:
830 * @cliexstart{ping 172.16.1.2 ipv6 fe80::24a5:f6ff:fe9c:3a36 source GigabitEthernet2/0/0 repeat 2 verbose}
831 * Adjacency index: 10, sw_if_index: 1
832 * Adj: ip6-discover-neighbor
833 * Adj Interface: 0
834 * Forced set interface: 1
835 * Adjacency index: 0, sw_if_index: 4294967295
836 * Adj: ip4-miss
837 * Adj Interface: 0
838 * Forced set interface: 1
839 * Source address: 172.16.1.1
840 * 64 bytes from 172.16.1.2: icmp_seq=1 ttl=64 time=.1899 ms
841 * Adjacency index: 10, sw_if_index: 1
842 * Adj: ip6-discover-neighbor
843 * Adj Interface: 0
844 * Forced set interface: 1
845 * Adjacency index: 0, sw_if_index: 4294967295
846 * Adj: ip4-miss
847 * Adj Interface: 0
848 * Forced set interface: 1
849 * Source address: 172.16.1.1
850 * 64 bytes from 172.16.1.2: icmp_seq=2 ttl=64 time=.0910 ms
851 *
852 * Statistics: 4 sent, 2 received, 50% packet loss
853 * @cliexend
854 * @endparblock
855?*/
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000856/* *INDENT-OFF* */
857VLIB_CLI_COMMAND (ping_command, static) =
858{
859 .path = "ping",
860 .function = ping_ip_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500861 .short_help = "ping {<ip-addr> | ipv4 <ip4-addr> | ipv6 <ip6-addr>}"
862 " [ipv4 <ip4-addr> | ipv6 <ip6-addr>] [source <interface>]"
863 " [size <pktsize>] [interval <sec>] [repeat <cnt>] [table-id <id>]"
864 " [verbose]",
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000865};
866/* *INDENT-ON* */
867
868static clib_error_t *
869ping_cli_init (vlib_main_t * vm)
870{
871 ping_main_t *pm = &ping_main;
872 pm->ip6_main = &ip6_main;
873 pm->ip4_main = &ip4_main;
874 icmp6_register_type (vm, ICMP6_echo_reply, ip6_icmp_echo_reply_node.index);
875 ip4_icmp_register_type (vm, ICMP4_echo_reply,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500876 ip4_icmp_echo_reply_node.index);
Andrew Yourtchenko7e682202016-08-04 13:39:35 +0000877 return 0;
878}
879
880VLIB_INIT_FUNCTION (ping_cli_init);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500881
882/*
883 * fd.io coding-style-patch-verification: ON
884 *
885 * Local Variables:
886 * eval: (c-set-style "gnu")
887 * End:
888 */