Skip to content

Commit 8e41100

Browse files
icmp: ident field setting from cli
Setting the ident field in the ICMP request message is useful for debugging and bringing stability in the future tests. Signed-off-by: Christophe Fontaine <[email protected]>
1 parent d9c830f commit 8e41100

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

modules/ip/cli/icmp.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,28 @@ static cmd_status_t icmp_send(
3333
struct gr_ip4_icmp_send_req *req,
3434
uint16_t msdelay,
3535
uint16_t count,
36+
uint16_t ident,
3637
bool mode_traceroute
3738
) {
3839
struct gr_ip4_icmp_recv_resp *reply_resp;
3940
struct gr_ip4_icmp_recv_req reply_req;
4041
int timeout, ret, errors;
4142
void *resp_ptr = NULL;
42-
uint16_t ping_id;
4343

4444
stop = false;
4545
errors = 0;
4646
errno = 0;
47-
ping_id = random();
4847

4948
for (int i = mode_traceroute; i < count && stop == false; i++) {
5049
req->ttl = mode_traceroute ? i : 64;
51-
req->ident = ping_id;
50+
req->ident = ident;
5251
req->seq_num = i;
5352

5453
ret = gr_api_client_send_recv(c, GR_IP4_ICMP_SEND, sizeof(*req), req, NULL);
5554
if (ret < 0)
5655
return CMD_ERROR;
5756

58-
reply_req.ident = ping_id;
57+
reply_req.ident = ident;
5958
reply_req.seq_num = i;
6059
timeout = 50;
6160
do {
@@ -132,6 +131,7 @@ static cmd_status_t ping(struct gr_api_client *c, const struct ec_pnode *p) {
132131
struct gr_ip4_icmp_send_req req = {.seq_num = 0, .vrf = 0};
133132
cmd_status_t ret = CMD_ERROR;
134133
uint16_t count = UINT16_MAX;
134+
uint16_t ident = random();
135135
uint16_t msdelay = 1000;
136136

137137
if (arg_ip4(p, "IP", &req.addr) < 0)
@@ -142,12 +142,14 @@ static cmd_status_t ping(struct gr_api_client *c, const struct ec_pnode *p) {
142142
return CMD_ERROR;
143143
if ((ret = arg_u16(p, "DELAY", &msdelay)) < 0 && ret != ENOENT)
144144
return CMD_ERROR;
145+
if ((ret = arg_u16(p, "IDENT", &ident)) < 0 && ret != ENOENT)
146+
return CMD_ERROR;
145147

146148
sighandler_t prev_handler = signal(SIGINT, sighandler);
147149
if (prev_handler == SIG_ERR)
148150
return CMD_ERROR;
149151

150-
ret = icmp_send(c, &req, msdelay, count, false);
152+
ret = icmp_send(c, &req, msdelay, count, ident, false);
151153

152154
signal(SIGINT, prev_handler);
153155

@@ -157,17 +159,20 @@ static cmd_status_t ping(struct gr_api_client *c, const struct ec_pnode *p) {
157159
static cmd_status_t traceroute(struct gr_api_client *c, const struct ec_pnode *p) {
158160
struct gr_ip4_icmp_send_req req = {.seq_num = 0, .vrf = 0};
159161
cmd_status_t ret = CMD_SUCCESS;
162+
uint16_t ident = random();
160163

161164
if (arg_ip4(p, "IP", &req.addr) < 0)
162165
return CMD_ERROR;
166+
if ((ret = arg_u16(p, "IDENT", &ident)) < 0 && ret != ENOENT)
167+
return CMD_ERROR;
163168
if ((ret = arg_u16(p, "VRF", &req.vrf)) < 0 && ret != ENOENT)
164169
return CMD_ERROR;
165170

166171
sighandler_t prev_handler = signal(SIGINT, sighandler);
167172
if (prev_handler == SIG_ERR)
168173
return CMD_ERROR;
169174

170-
ret = icmp_send(c, &req, 0, 255, true);
175+
ret = icmp_send(c, &req, 0, 255, ident, true);
171176

172177
signal(SIGINT, prev_handler);
173178

@@ -181,23 +186,31 @@ static int ctx_init(struct ec_node *root) {
181186
CLI_CONTEXT(
182187
root, CTX_ARG("ping", "Send IPv4 ICMP echo requests and wait for replies.")
183188
),
184-
"IP [vrf VRF] [count COUNT] [delay DELAY]",
189+
"IP [vrf VRF] [count COUNT] [delay DELAY] [ident IDENT]",
185190
ping,
186191
"Send IPv4 ICMP echo requests and wait for replies.",
187192
with_help("IPv4 destination address.", ec_node_re("IP", IPV4_RE)),
188193
with_help("L3 routing domain ID.", ec_node_uint("VRF", 0, UINT16_MAX - 1, 10)),
189194
with_help("Number of packets to send.", ec_node_uint("COUNT", 1, UINT16_MAX, 10)),
190-
with_help("Delay in ms between icmp echo.", ec_node_uint("DELAY", 0, 10000, 10))
195+
with_help("Delay in ms between icmp echo.", ec_node_uint("DELAY", 0, 10000, 10)),
196+
with_help(
197+
"Icmp ident field (default: random).",
198+
ec_node_uint("IDENT", 1, UINT16_MAX, 10)
199+
)
191200
);
192201
if (ret < 0)
193202
return ret;
194203

195204
ret = CLI_COMMAND(
196205
CLI_CONTEXT(root, CTX_ARG("traceroute", "Discover IPv4 intermediate gateways.")),
197-
"IP [vrf VRF]",
206+
"IP [ident IDENT] [vrf VRF]",
198207
traceroute,
199208
"Discover IPv4 intermediate gateways.",
200209
with_help("IPv4 destination address.", ec_node_re("IP", IPV4_RE)),
210+
with_help(
211+
"Icmp ident field (default: random).",
212+
ec_node_uint("IDENT", 1, UINT16_MAX, 10)
213+
),
201214
with_help("L3 routing domain ID.", ec_node_uint("VRF", 0, UINT16_MAX - 1, 10))
202215
);
203216

modules/ip6/cli/icmp6.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,29 @@ static cmd_status_t icmp_send(
2525
struct gr_ip6_icmp_send_req *req,
2626
uint16_t msdelay,
2727
uint16_t count,
28+
uint16_t ident,
2829
bool mode_traceroute
2930
) {
3031
struct gr_ip6_icmp_recv_resp *reply_resp;
3132
struct gr_ip6_icmp_recv_req reply_req;
3233
int i, timeout, ret, errors;
3334
void *resp_ptr = NULL;
34-
uint16_t ping_id;
3535
const char *errdesc;
3636

3737
stop = false;
3838
errors = 0;
3939
errno = 0;
40-
ping_id = random();
4140

4241
for (i = !!mode_traceroute; i < count && stop == false; i++) {
43-
req->ident = ping_id;
42+
req->ident = ident;
4443
req->seq_num = i;
4544
req->ttl = mode_traceroute ? i : 64;
4645

4746
ret = gr_api_client_send_recv(c, GR_IP6_ICMP6_SEND, sizeof(*req), req, NULL);
4847
if (ret < 0)
4948
return CMD_ERROR;
5049

51-
reply_req.ident = ping_id;
50+
reply_req.ident = ident;
5251
reply_req.seq_num = i;
5352
timeout = 50;
5453
do {
@@ -142,6 +141,7 @@ static cmd_status_t ping(struct gr_api_client *c, const struct ec_pnode *p) {
142141
struct gr_ip6_icmp_send_req req = {.iface = GR_IFACE_ID_UNDEF, .vrf = 0};
143142
cmd_status_t ret = CMD_ERROR;
144143
uint16_t count = UINT16_MAX;
144+
uint16_t ident = random();
145145
uint16_t msdelay = 1000;
146146
const char *str;
147147

@@ -153,6 +153,8 @@ static cmd_status_t ping(struct gr_api_client *c, const struct ec_pnode *p) {
153153
return CMD_ERROR;
154154
if ((ret = arg_u16(p, "DELAY", &msdelay)) < 0 && ret != ENOENT)
155155
return CMD_ERROR;
156+
if ((ret = arg_u16(p, "IDENT", &ident)) < 0 && ret != ENOENT)
157+
return CMD_ERROR;
156158
if ((str = arg_str(p, "IFACE")) != NULL) {
157159
struct gr_iface *iface = iface_from_name(c, str);
158160
if (iface == NULL)
@@ -165,7 +167,7 @@ static cmd_status_t ping(struct gr_api_client *c, const struct ec_pnode *p) {
165167
if (prev_handler == SIG_ERR)
166168
return CMD_ERROR;
167169

168-
ret = icmp_send(c, &req, msdelay, count, false);
170+
ret = icmp_send(c, &req, msdelay, count, ident, false);
169171

170172
signal(SIGINT, prev_handler);
171173

@@ -175,12 +177,15 @@ static cmd_status_t ping(struct gr_api_client *c, const struct ec_pnode *p) {
175177
static cmd_status_t traceroute(struct gr_api_client *c, const struct ec_pnode *p) {
176178
struct gr_ip6_icmp_send_req req = {.iface = GR_IFACE_ID_UNDEF, .vrf = 0};
177179
cmd_status_t ret = CMD_SUCCESS;
180+
uint16_t ident = random();
178181
const char *str;
179182

180183
if (arg_ip6(p, "DEST", &req.addr) < 0)
181184
return CMD_ERROR;
182185
if ((ret = arg_u16(p, "VRF", &req.vrf)) < 0 && ret != ENOENT)
183186
return CMD_ERROR;
187+
if ((ret = arg_u16(p, "IDENT", &ident)) < 0 && ret != ENOENT)
188+
return CMD_ERROR;
184189
if ((str = arg_str(p, "IFACE")) != NULL) {
185190
struct gr_iface *iface = iface_from_name(c, str);
186191
if (iface == NULL)
@@ -193,7 +198,7 @@ static cmd_status_t traceroute(struct gr_api_client *c, const struct ec_pnode *p
193198
if (prev_handler == SIG_ERR)
194199
return CMD_ERROR;
195200

196-
ret = icmp_send(c, &req, 0, 255, true);
201+
ret = icmp_send(c, &req, 0, 255, ident, true);
197202

198203
signal(SIGINT, prev_handler);
199204

@@ -207,7 +212,7 @@ static int ctx_init(struct ec_node *root) {
207212
CLI_CONTEXT(
208213
root, CTX_ARG("ping", "Send ICMPv6 echo requests and wait for replies.")
209214
),
210-
"DEST [vrf VRF] [count COUNT] [delay DELAY] [iface IFACE]",
215+
"DEST [vrf VRF] [count COUNT] [delay DELAY] [iface IFACE] [ident IDENT]",
211216
ping,
212217
"Send ICMPv6 echo requests and wait for replies.",
213218
with_help("IPv6 destination address.", ec_node_re("DEST", IPV6_RE)),
@@ -217,21 +222,29 @@ static int ctx_init(struct ec_node *root) {
217222
),
218223
with_help("L3 routing domain ID.", ec_node_uint("VRF", 0, UINT16_MAX - 1, 10)),
219224
with_help("Number of packets to send.", ec_node_uint("COUNT", 1, UINT16_MAX, 10)),
220-
with_help("Delay in ms between icmp6 echo.", ec_node_uint("DELAY", 0, 10000, 10))
225+
with_help("Delay in ms between icmp6 echo.", ec_node_uint("DELAY", 0, 10000, 10)),
226+
with_help(
227+
"Icmp ident field (default: random).",
228+
ec_node_uint("IDENT", 1, UINT16_MAX, 10)
229+
)
221230
);
222231
if (ret < 0)
223232
return ret;
224233

225234
ret = CLI_COMMAND(
226235
CLI_CONTEXT(root, CTX_ARG("traceroute", "Discover IPv6 intermediate gateways.")),
227-
"DEST [vrf VRF] [iface IFACE]",
236+
"DEST [vrf VRF] [iface IFACE] [ident IDENT]",
228237
traceroute,
229238
"Discover IPv6 intermediate gateways.",
230239
with_help("IPv6 destination address.", ec_node_re("DEST", IPV6_RE)),
231240
with_help("L3 routing domain ID.", ec_node_uint("VRF", 0, UINT16_MAX - 1, 10)),
232241
with_help(
233242
"Output interface name.",
234243
ec_node_dyn("IFACE", complete_iface_names, INT2PTR(GR_IFACE_TYPE_UNDEF))
244+
),
245+
with_help(
246+
"Icmp ident field (default: random).",
247+
ec_node_uint("IDENT", 1, UINT16_MAX, 10)
235248
)
236249
);
237250

0 commit comments

Comments
 (0)