blob: 8dafa25959fdf4ea15053cbc398edae59b03b2b5 [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
16#define PING_DELAY_MS 1000
17#define PING_COUNT 5
18/* Ping identifier - must fit on a u16_t */
19#define PING_ID 0xAFAF
20
21struct ping_ctx {
22 ip_addr_t target;
23 struct raw_pcb *pcb;
24 struct icmp_echo_hdr *iecho;
25 uint16_t seq_num;
26 bool alive;
27};
28
29static u8_t ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p,
30 const ip_addr_t *addr)
31{
32 struct ping_ctx *ctx = arg;
33 struct icmp_echo_hdr *iecho = ctx->iecho;
34
35 if (addr->addr != ctx->target.addr)
36 return 0;
37
38 if ((p->tot_len >= (IP_HLEN + sizeof(struct icmp_echo_hdr))) &&
39 pbuf_remove_header(p, IP_HLEN) == 0) {
40 iecho = (struct icmp_echo_hdr *)p->payload;
41
42 if ((iecho->id == PING_ID) &&
43 (iecho->seqno == lwip_htons(ctx->seq_num))) {
44 ctx->alive = true;
45 printf("host %s is alive\n", ipaddr_ntoa(addr));
46 pbuf_free(p);
47 return 1; /* eat the packet */
48 }
49 /* not eaten, restore original packet */
50 pbuf_add_header(p, IP_HLEN);
51 }
52
53 return 0; /* don't eat the packet */
54}
55
56static int ping_raw_init(struct ping_ctx *ctx)
57{
58 ctx->pcb = raw_new(IP_PROTO_ICMP);
59 if (!ctx->pcb)
60 return -ENOMEM;
61
62 raw_recv(ctx->pcb, ping_recv, ctx);
63 raw_bind(ctx->pcb, IP_ADDR_ANY);
64
65 return 0;
66}
67
68static void ping_raw_stop(struct ping_ctx *ctx)
69{
70 if (ctx->pcb)
71 raw_remove(ctx->pcb);
72}
73
74static void ping_prepare_echo(struct ping_ctx *ctx)
75{
76 struct icmp_echo_hdr *iecho = ctx->iecho;
77
78 ICMPH_TYPE_SET(iecho, ICMP_ECHO);
79 ICMPH_CODE_SET(iecho, 0);
80 iecho->chksum = 0;
81 iecho->id = PING_ID;
82 iecho->seqno = lwip_htons(ctx->seq_num);
83
84 iecho->chksum = inet_chksum(iecho, sizeof(*iecho));
85}
86
87static void ping_send_icmp(struct ping_ctx *ctx)
88{
89 struct pbuf *p;
90 size_t ping_size = sizeof(struct icmp_echo_hdr);
91
92 p = pbuf_alloc(PBUF_IP, (u16_t)ping_size, PBUF_RAM);
93 if (!p)
94 return;
95
96 if ((p->len == p->tot_len) && !p->next) {
97 ctx->iecho = (struct icmp_echo_hdr *)p->payload;
98 ping_prepare_echo(ctx);
99 raw_sendto(ctx->pcb, p, &ctx->target);
100 }
101
102 pbuf_free(p);
103}
104
105static void ping_send(void *arg)
106{
107 struct ping_ctx *ctx = arg;
108
109 ctx->seq_num++;
110 if (ctx->seq_num <= PING_COUNT) {
111 ping_send_icmp(ctx);
112 sys_timeout(PING_DELAY_MS, ping_send, ctx);
113 }
114}
115
116static int ping_loop(struct udevice *udev, const ip_addr_t* addr)
117{
118 struct ping_ctx ctx = {};
119 struct netif *netif;
120 int ret;
121
122 netif = net_lwip_new_netif(udev);
123 if (!netif)
124 return CMD_RET_FAILURE;
125
126 printf("Using %s device\n", udev->name);
127
128 ret = ping_raw_init(&ctx);
129 if (ret < 0) {
130 net_lwip_remove_netif(netif);
131 return ret;
132 }
133
134 ctx.target = *addr;
135
136 ping_send(&ctx);
137
138 do {
139 sys_check_timeouts();
140 net_lwip_rx(udev, netif);
141 if (ctx.alive)
142 break;
143 if (ctrlc()) {
144 printf("\nAbort\n");
145 break;
146 }
147 } while (ctx.seq_num <= PING_COUNT);
148
149 sys_untimeout(ping_send, &ctx);
150 ping_raw_stop(&ctx);
151
152 net_lwip_remove_netif(netif);
153
154 if (ctx.alive)
155 return 0;
156
157 printf("ping failed; host %s is not alive\n", ipaddr_ntoa(addr));
158 return -1;
159}
160
161int do_ping(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
162{
163 ip_addr_t addr;
164
165 if (argc < 2)
166 return CMD_RET_USAGE;
167
168 if (!ipaddr_aton(argv[1], &addr))
169 return CMD_RET_USAGE;
170
171 eth_set_current();
172
173 if (ping_loop(eth_get_dev(), &addr) < 0)
174 return CMD_RET_FAILURE;
175
176 return CMD_RET_SUCCESS;
177}