blob: 6996931fa4045d7ee805c5b424da66a717bc5ac8 [file] [log] [blame]
Jerome Forissier8f29e002024-10-16 12:04:06 +02001// SPDX-License-Identifier: GPL-2.0+
2/* Copyright (C) 2024 Linaro Ltd. */
3
4#include <command.h>
5#include <console.h>
6#include <dm/device.h>
7#include <linux/delay.h>
8#include <linux/errno.h>
9#include <lwip/icmp.h>
10#include <lwip/inet_chksum.h>
11#include <lwip/raw.h>
12#include <lwip/timeouts.h>
13#include <net.h>
14#include <time.h>
15
Jerome Forissier53a7e982025-06-25 15:19:12 +020016U_BOOT_CMD(ping, 2, 1, do_ping, "send ICMP ECHO_REQUEST to network host",
17 "pingAddress");
18
Jerome Forissier8f29e002024-10-16 12:04:06 +020019#define PING_DELAY_MS 1000
20#define PING_COUNT 5
21/* Ping identifier - must fit on a u16_t */
22#define PING_ID 0xAFAF
23
24struct ping_ctx {
25 ip_addr_t target;
26 struct raw_pcb *pcb;
27 struct icmp_echo_hdr *iecho;
28 uint16_t seq_num;
29 bool alive;
30};
31
32static u8_t ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p,
33 const ip_addr_t *addr)
34{
35 struct ping_ctx *ctx = arg;
36 struct icmp_echo_hdr *iecho = ctx->iecho;
37
38 if (addr->addr != ctx->target.addr)
39 return 0;
40
41 if ((p->tot_len >= (IP_HLEN + sizeof(struct icmp_echo_hdr))) &&
42 pbuf_remove_header(p, IP_HLEN) == 0) {
43 iecho = (struct icmp_echo_hdr *)p->payload;
44
Jerome Forissier97083502024-11-07 12:27:57 +010045 if (iecho->id == PING_ID &&
46 iecho->seqno == lwip_htons(ctx->seq_num)) {
Jerome Forissier8f29e002024-10-16 12:04:06 +020047 ctx->alive = true;
48 printf("host %s is alive\n", ipaddr_ntoa(addr));
49 pbuf_free(p);
50 return 1; /* eat the packet */
51 }
52 /* not eaten, restore original packet */
53 pbuf_add_header(p, IP_HLEN);
54 }
55
56 return 0; /* don't eat the packet */
57}
58
59static int ping_raw_init(struct ping_ctx *ctx)
60{
61 ctx->pcb = raw_new(IP_PROTO_ICMP);
62 if (!ctx->pcb)
63 return -ENOMEM;
64
65 raw_recv(ctx->pcb, ping_recv, ctx);
66 raw_bind(ctx->pcb, IP_ADDR_ANY);
67
68 return 0;
69}
70
71static void ping_raw_stop(struct ping_ctx *ctx)
72{
73 if (ctx->pcb)
74 raw_remove(ctx->pcb);
75}
76
77static void ping_prepare_echo(struct ping_ctx *ctx)
78{
79 struct icmp_echo_hdr *iecho = ctx->iecho;
80
81 ICMPH_TYPE_SET(iecho, ICMP_ECHO);
82 ICMPH_CODE_SET(iecho, 0);
83 iecho->chksum = 0;
84 iecho->id = PING_ID;
85 iecho->seqno = lwip_htons(ctx->seq_num);
86
87 iecho->chksum = inet_chksum(iecho, sizeof(*iecho));
88}
89
90static void ping_send_icmp(struct ping_ctx *ctx)
91{
92 struct pbuf *p;
93 size_t ping_size = sizeof(struct icmp_echo_hdr);
94
95 p = pbuf_alloc(PBUF_IP, (u16_t)ping_size, PBUF_RAM);
96 if (!p)
97 return;
98
Jerome Forissier97083502024-11-07 12:27:57 +010099 if (p->len == p->tot_len && !p->next) {
Jerome Forissier8f29e002024-10-16 12:04:06 +0200100 ctx->iecho = (struct icmp_echo_hdr *)p->payload;
101 ping_prepare_echo(ctx);
102 raw_sendto(ctx->pcb, p, &ctx->target);
103 }
104
105 pbuf_free(p);
106}
107
108static void ping_send(void *arg)
109{
110 struct ping_ctx *ctx = arg;
111
112 ctx->seq_num++;
113 if (ctx->seq_num <= PING_COUNT) {
114 ping_send_icmp(ctx);
115 sys_timeout(PING_DELAY_MS, ping_send, ctx);
116 }
117}
118
Jerome Forissier97083502024-11-07 12:27:57 +0100119static int ping_loop(struct udevice *udev, const ip_addr_t *addr)
Jerome Forissier8f29e002024-10-16 12:04:06 +0200120{
121 struct ping_ctx ctx = {};
122 struct netif *netif;
123 int ret;
124
125 netif = net_lwip_new_netif(udev);
126 if (!netif)
Jerome Forissierb5089272025-03-14 09:44:10 +0100127 return -ENODEV;
Jerome Forissier8f29e002024-10-16 12:04:06 +0200128
129 printf("Using %s device\n", udev->name);
130
131 ret = ping_raw_init(&ctx);
132 if (ret < 0) {
133 net_lwip_remove_netif(netif);
134 return ret;
135 }
136
137 ctx.target = *addr;
138
139 ping_send(&ctx);
140
141 do {
Jerome Forissier8f29e002024-10-16 12:04:06 +0200142 net_lwip_rx(udev, netif);
143 if (ctx.alive)
144 break;
145 if (ctrlc()) {
146 printf("\nAbort\n");
147 break;
148 }
149 } while (ctx.seq_num <= PING_COUNT);
150
151 sys_untimeout(ping_send, &ctx);
152 ping_raw_stop(&ctx);
153
154 net_lwip_remove_netif(netif);
155
156 if (ctx.alive)
157 return 0;
158
159 printf("ping failed; host %s is not alive\n", ipaddr_ntoa(addr));
160 return -1;
161}
162
163int do_ping(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
164{
165 ip_addr_t addr;
166
167 if (argc < 2)
168 return CMD_RET_USAGE;
169
170 if (!ipaddr_aton(argv[1], &addr))
171 return CMD_RET_USAGE;
172
Jerome Forissier0c9cb752025-04-15 23:17:38 +0200173restart:
174 if (net_lwip_eth_start() < 0 || ping_loop(eth_get_dev(), &addr) < 0) {
175 if (net_start_again() == 0)
176 goto restart;
177 else
178 return CMD_RET_FAILURE;
179 }
Jerome Forissier8f29e002024-10-16 12:04:06 +0200180
181 return CMD_RET_SUCCESS;
182}