1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2013 Allied Telesis Labs NZ
6 * Copyright (C) 2022 YADRO
10 /* Simple ping6 implementation */
18 /* the ipv6 address to ping */
19 struct in6_addr net_ping_ip6;
22 ip6_make_ping(uchar *eth_dst_addr, struct in6_addr *neigh_addr, uchar *pkt)
29 len = sizeof(struct echo_msg);
31 pkt += net_set_ether(pkt, eth_dst_addr, PROT_IP6);
32 pkt += ip6_add_hdr(pkt, &net_ip6, neigh_addr, PROT_ICMPV6,
33 IPV6_NDISC_HOPLIMIT, len);
36 msg = (struct echo_msg *)pkt;
37 msg->icmph.icmp6_type = IPV6_ICMP_ECHO_REQUEST;
38 msg->icmph.icmp6_code = 0;
39 msg->icmph.icmp6_cksum = 0;
40 msg->icmph.icmp6_identifier = 0;
41 msg->icmph.icmp6_sequence = htons(seq_no++);
42 msg->id = msg->icmph.icmp6_identifier; /* these seem redundant */
43 msg->sequence = msg->icmph.icmp6_sequence;
46 csum_p = csum_partial((u8 *)msg, len, 0);
47 msg->icmph.icmp6_cksum = csum_ipv6_magic(&net_ip6, neigh_addr, len,
60 /* always send neighbor solicit */
62 memcpy(mac, net_null_ethaddr, 6);
64 net_nd_sol_packet_ip6 = net_ping_ip6;
65 net_nd_packet_mac = mac;
67 pkt = net_nd_tx_packet;
68 pkt += ip6_make_ping(mac, &net_ping_ip6, pkt);
70 /* size of the waiting packet */
71 net_nd_tx_packet_size = (pkt - net_nd_tx_packet);
73 /* and do the ARP request */
75 net_nd_timer_start = get_timer(0);
77 return 1; /* waiting */
80 static void ping6_timeout(void)
83 net_set_state(NETLOOP_FAIL); /* we did not get the reply */
86 void ping6_start(void)
88 printf("Using %s device\n", eth_get_name());
89 net_set_timeout_handler(10000UL, ping6_timeout);
94 int ping6_receive(struct ethernet_hdr *et, struct ip6_hdr *ip6, int len)
96 struct icmp6hdr *icmp =
97 (struct icmp6hdr *)(((uchar *)ip6) + IP6_HDR_SIZE);
98 struct in6_addr src_ip;
100 switch (icmp->icmp6_type) {
101 case IPV6_ICMP_ECHO_REPLY:
103 if (memcmp(&net_ping_ip6, &src_ip, sizeof(struct in6_addr)))
105 net_set_state(NETLOOP_SUCCESS);
107 case IPV6_ICMP_ECHO_REQUEST:
108 /* ignore for now.... */
109 debug("Got ICMPv6 ECHO REQUEST from %pI6c\n", &ip6->saddr);
112 debug("Unexpected ICMPv6 type 0x%x\n", icmp->icmp6_type);