From patchwork Wed Jan 25 09:56:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Packham X-Patchwork-Id: 719614 X-Patchwork-Delegate: joe.hershberger@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 3v7gV732QFz9ryZ for ; Wed, 25 Jan 2017 20:58:23 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="SoG2bmsk"; dkim-atps=neutral Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 5BDE8B3878; Wed, 25 Jan 2017 10:58:14 +0100 (CET) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id NyYypX7O28sm; Wed, 25 Jan 2017 10:58:14 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 5BB18B38CA; Wed, 25 Jan 2017 10:57:53 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 94CA6B38B1 for ; Wed, 25 Jan 2017 10:57:43 +0100 (CET) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id cv1cM7arD-lb for ; Wed, 25 Jan 2017 10:57:43 +0100 (CET) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail-pf0-f194.google.com (mail-pf0-f194.google.com [209.85.192.194]) by theia.denx.de (Postfix) with ESMTPS id 4567AB38BF for ; Wed, 25 Jan 2017 10:57:24 +0100 (CET) Received: by mail-pf0-f194.google.com with SMTP id f144so14073498pfa.2 for ; Wed, 25 Jan 2017 01:57:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=/c/eqLtnkFIUzPufAClZjSim8T3cu5BYnowin784JnI=; b=SoG2bmskFd//76/u5Z2ZftIIs4qZDO/53LjgOFAXR8gPNCL30ES1S08nbhmraD8PKG bsfEqMLl2dTZsEJtl9Dthew1bz/FzqYy1mXI08cYKNj8VYh71V5aMqGfpNC6qhij4Zhb Qg1+lDWRXDgu8Q16UJlA4TRA8ijL5zpliR86EZDF67JWJ1niiXl0eWZpMVjldFIdtEk7 G5PPxdl9GUFTla8j5I1Vvuww+Vdr7mumNsus4mYVsECU3p2YTbk1s/yxVysg5wQTCDYT kqDTipjhn4bIYILKjgJi2unUJrgVUEh7VGuiayT8pkrLIv0d4Sri6bbEaFNzKH1huE64 6SUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=/c/eqLtnkFIUzPufAClZjSim8T3cu5BYnowin784JnI=; b=admPE7/8WBTy9LIiVMYumYuYVQ6kUxX9qu8Jo+gBHXyxoG0q4iJeJKcQpwRCGr9Q4s hi6bU6lHstjJt9DZ5rPSlJKcfxK8pomuVSg0YHlyE7kjplBKw+MpxxA+U/oLP+IscEAp S3MXsHU/qJoDvaJipqsG/X4VY1y59ZOv8b1dGSK3rr8rwz8JB19kvtdcVI4VFnfQP8u9 rTddauQqsaQ+fMLQqoeDNlNESVt2ZE6QyB3UwLaWU8C9ArEsa2PV+RUiw/wO50rL2Pao 9PCcopG3O6c3IwY1emZoDMm8q4KXBjybmrTFT1WmpFP/EhlGYwvFu0GydWF0hxNkWcGf 2utw== X-Gm-Message-State: AIkVDXJVzB9rGRljv+NVbwkUJ3LJD3xrhYUPoX0unlEQRxboRHjOqrH1delp2yzMw9LofA== X-Received: by 10.84.136.34 with SMTP id 31mr2274450plk.52.1485338243387; Wed, 25 Jan 2017 01:57:23 -0800 (PST) Received: from chrisp-dl.atlnz.lc ([2001:df5:b000:22:8d2c:469e:c0fc:f3ee]) by smtp.gmail.com with ESMTPSA id t87sm50980162pfe.59.2017.01.25.01.57.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 25 Jan 2017 01:57:22 -0800 (PST) From: Chris Packham To: u-boot@lists.denx.de, Joe Hershberger Date: Wed, 25 Jan 2017 22:56:18 +1300 Message-Id: <20170125095622.20326-8-judge.packham@gmail.com> X-Mailer: git-send-email 2.11.0.24.ge6920cf In-Reply-To: <20170125095622.20326-1-judge.packham@gmail.com> References: <20170125095622.20326-1-judge.packham@gmail.com> Cc: Baruch Siach , Angga , hannah@marvell.com, Alexey Brodkin , jp.tosoni@acksys.fr, Przemyslaw Marczak , Stefan Roese , luka.kovacica@gmail.com, reed@fb.com, Chris Packham Subject: [U-Boot] [RFC PATCH v3 07/11] net: Add ping6 command and implementation X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.15 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Signed-off-by: Chris Packham Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: - split ping6 support into it's own patch cmd/Kconfig | 6 ++++ cmd/net.c | 28 +++++++++++++++ include/net.h | 4 +-- net/net6.c | 7 ++++ net/ping6.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 net/ping6.c diff --git a/cmd/Kconfig b/cmd/Kconfig index 91bd3fb0b58e..37126577bc65 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -546,6 +546,12 @@ config CMD_PING help Send ICMP ECHO_REQUEST to network host +config CMD_PING6 + bool "ping6" + depends on CMD_NET6 + help + Send ICMPv6 ECHO_REQUEST to network host + config CMD_CDP bool "cdp" help diff --git a/cmd/net.c b/cmd/net.c index df8b6c9b53f0..7f40f257c03c 100644 --- a/cmd/net.c +++ b/cmd/net.c @@ -11,6 +11,7 @@ #include #include #include +#include static int netboot_common(enum proto_t, cmd_tbl_t *, int, char * const []); @@ -281,6 +282,33 @@ U_BOOT_CMD( ); #endif +#ifdef CONFIG_CMD_PING6 +int do_ping6(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +{ + if (argc < 2) + return -1; + + if (string_to_ip6(argv[1], &net_ping_ip6) != 0) + return CMD_RET_USAGE; + + if (net_loop(PING6) < 0) { + printf("ping6 failed; host %pI6c is not alive\n", + &net_ping_ip6); + return 1; + } + + printf("host %pI6c is alive\n", &net_ping_ip6); + + return 0; +} + +U_BOOT_CMD( + ping6, 2, 1, do_ping6, + "send ICMPv6 ECHO_REQUEST to network host", + "pingAddress" +); +#endif /* CONFIG_CMD_PING6 */ + #if defined(CONFIG_CMD_CDP) static void cdp_update_env(void) diff --git a/include/net.h b/include/net.h index b0348adf955e..be75c6c65c6b 100644 --- a/include/net.h +++ b/include/net.h @@ -545,8 +545,8 @@ extern ushort net_native_vlan; /* Our Native VLAN */ extern int net_restart_wrap; /* Tried all network devices */ enum proto_t { - BOOTP, RARP, ARP, TFTPGET, DHCP, PING, DNS, NFS, CDP, NETCONS, SNTP, - TFTPSRV, TFTPPUT, LINKLOCAL + BOOTP, RARP, ARP, TFTPGET, DHCP, PING, PING6, DNS, NFS, CDP, NETCONS, + SNTP, TFTPSRV, TFTPPUT, LINKLOCAL }; extern char net_boot_file_name[1024];/* Boot File name */ diff --git a/net/net6.c b/net/net6.c index 955a08987be9..8f0c7214f8e1 100644 --- a/net/net6.c +++ b/net/net6.c @@ -370,6 +370,13 @@ void net_ip6_handler(struct ethernet_hdr *et, struct ip6_hdr *ip6, int len) return; switch (icmp->icmp6_type) { +#ifdef CONFIG_CMD_PING6 + case IPV6_ICMP_ECHO_REQUEST: + case IPV6_ICMP_ECHO_REPLY: + ping6_receive(et, ip6, len); + break; +#endif /* CONFIG_CMD_PING6 */ + case IPV6_NDISC_NEIGHBOUR_SOLICITATION: case IPV6_NDISC_NEIGHBOUR_ADVERTISEMENT: ndisc_receive(et, ip6, len); diff --git a/net/ping6.c b/net/ping6.c new file mode 100644 index 000000000000..34796c6a288c --- /dev/null +++ b/net/ping6.c @@ -0,0 +1,111 @@ +/* + * net/ping6.c + * + * (C) Copyright 2013 Allied Telesis Labs NZ + * + * SPDX-License-Identifier: GPL-2.0+ + */ +#define DEBUG +#include +#include +#include +#include "ndisc.h" + +static ushort seq_no; + +/* the ipv6 address to ping */ +struct in6_addr net_ping_ip6; + +int +ip6_make_ping(uchar *eth_dst_addr, struct in6_addr *neigh_addr, uchar *pkt) +{ + struct echo_msg *msg; + __u16 len; + uchar *pkt_old = pkt; + + len = sizeof(struct echo_msg); + + pkt += net_set_ether(pkt, eth_dst_addr, PROT_IP6); + pkt += ip6_add_hdr(pkt, &net_ip6, neigh_addr, IPPROTO_ICMPV6, + IPV6_NDISC_HOPLIMIT, len); + + /* ICMPv6 - Echo */ + msg = (struct echo_msg *)pkt; + msg->icmph.icmp6_type = IPV6_ICMP_ECHO_REQUEST; + msg->icmph.icmp6_code = 0; + msg->icmph.icmp6_cksum = 0; + msg->icmph.icmp6_identifier = 0; + msg->icmph.icmp6_sequence = htons(seq_no++); + msg->id = msg->icmph.icmp6_identifier; /* these seem redundant */ + msg->sequence = msg->icmph.icmp6_sequence; + + /* checksum */ + msg->icmph.icmp6_cksum = csum_ipv6_magic(&net_ip6, neigh_addr, len, + IPPROTO_ICMPV6, + csum_partial((__u8 *)msg, len, 0)); + + pkt += len; + + return pkt - pkt_old; +} + +int ping6_send(void) +{ + uchar *pkt; + static uchar mac[6]; + + /* always send neighbor solicit */ + + memcpy(mac, net_null_ethaddr, 6); + + net_nd_sol_packet_ip6 = net_ping_ip6; + net_nd_packet_mac = mac; + + pkt = net_nd_tx_packet; + pkt += ip6_make_ping(mac, &net_ping_ip6, pkt); + + /* size of the waiting packet */ + net_nd_tx_packet_size = (pkt - net_nd_tx_packet); + + /* and do the ARP request */ + net_nd_try = 1; + net_nd_timer_start = get_timer(0); + ndisc_request(); + return 1; /* waiting */ +} + +static void ping6_timeout(void) +{ + eth_halt(); + net_set_state(NETLOOP_FAIL); /* we did not get the reply */ +} + +void ping6_start(void) +{ + printf("Using %s device\n", eth_get_name()); + net_set_timeout_handler(10000UL, ping6_timeout); + + ping6_send(); +} + +void ping6_receive(struct ethernet_hdr *et, struct ip6_hdr *ip6, int len) +{ + struct icmp6hdr *icmp = + (struct icmp6hdr *)(((uchar *)ip6) + IP6_HDR_SIZE); + struct in6_addr src_ip; + + switch (icmp->icmp6_type) { + case IPV6_ICMP_ECHO_REPLY: + src_ip = ip6->saddr; + if (memcmp(&net_ping_ip6, &src_ip, sizeof(struct in6_addr)) != 0) + return; + net_set_state(NETLOOP_SUCCESS); + break; + case IPV6_ICMP_ECHO_REQUEST: + debug("Got ICMPv6 ECHO REQUEST from %pI6c\n", &ip6->saddr); + /* ignore for now.... */ + break; + default: + debug("Unexpected ICMPv6 type 0x%x\n", icmp->icmp6_type); + } +}