From patchwork Fri Apr 9 18:45:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Xue X-Patchwork-Id: 1464642 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4FHcYn3xkrz9sVq for ; Sun, 11 Apr 2021 00:18:53 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 533A780C71; Sat, 10 Apr 2021 16:17:57 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=hotmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id 69428801F0; Fri, 9 Apr 2021 20:46:10 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=1.0 required=5.0 tests=BAYES_00, FORGED_HOTMAIL_RCVD2, FREEMAIL_FROM,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS, SPOOFED_FREEMAIL autolearn=no autolearn_force=no version=3.4.2 Received: from alln-iport-5.cisco.com (alln-iport-5.cisco.com [173.37.142.92]) (using TLSv1.2 with cipher DHE-RSA-SEED-SHA (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 1E41280050 for ; Fri, 9 Apr 2021 20:46:05 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=hotmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=joxue@cisco.com X-IPAS-Result: A0DBAgCFoHBgmJhdJa1aglszg0Y5MZYagneHTZAoggcBAQENAQE0BAEBhkkCJTYHDgIDAQEBAwIDAQEBAQEFAQEBAgEGBBQBAQECAmmFXYZyCwFGMF1EgnGCVQEDL60JFgUBARaBAYZsASOBLoEugTmIZYJDgkwcgguBE4Nggh6IGwSBVWsHgQ8lgjeRDYJ8i3yaLluDIQGXOIUoMqRwAaQVj2BHhAICBAYFAhaBWwwlgVtwgzlQGQ6OKxaOQyUyOAIGCgEBAwmNEQEB IronPort-HdrOrdr: A9a23:YZEMVa38HzIAQ0tYprFhUwqjBCIkLtp033Aq2lEZdDV+eKWj+P yGtvIdyBPylXItQ3kmg9+NI+2tRnnb+J5z7+AqTNKfdSTvpWfAFuBfxKT4xTmIIUDD38p88Y slTKRkEt33CjFB/KTHyS21CcwpztXC0K3Av4fj5kxgRw1rdK1shj0RYm3wLmRNWAJECZAlfa Dz2uN7oVObFUg/X4CeDnkBU/OrnaypqLvWJTgbGhUg9A6CyRSv5bKSKWn+4j4uFxVS3Lwl7W /J1zbc25zmmfS6xhjAvlWji6hrpA== X-IronPort-Anti-Spam-Filtered: true X-IronPort-AV: E=Sophos;i="5.82,210,1613433600"; d="scan'208";a="694981524" Received: from rcdn-core-1.cisco.com ([173.37.93.152]) by alln-iport-5.cisco.com with ESMTP/TLS/DHE-RSA-SEED-SHA; 09 Apr 2021 18:46:03 +0000 Received: from ott-ads-268.cisco.com (ott-ads-268.cisco.com [161.44.237.36]) by rcdn-core-1.cisco.com (8.15.2/8.15.2) with ESMTPS id 139Ik32O020717 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Fri, 9 Apr 2021 18:46:03 GMT Received: by ott-ads-268.cisco.com (Postfix, from userid 372173) id E192BCC1251; Fri, 9 Apr 2021 14:46:02 -0400 (EDT) From: lgxue@hotmail.com To: u-boot@lists.denx.de Cc: lgxue@hotmail.com, bmeng.cn@gmail.com, frederic.danis@collabora.com, xypron.glpk@gmx.de, joe.hershberger@ni.com, Olaf.Krebs@emh-metering.com, patrick.delaunay@foss.st.com, peng.fan@nxp.com, philippe.reynes@softathome.com, rfried.dev@gmail.com, sjg@chromium.org Subject: [PATCH] cmd: net: Add the "arp" command Date: Fri, 9 Apr 2021 14:45:36 -0400 Message-Id: <20210409184536.22861-1-lgxue@hotmail.com> X-Mailer: git-send-email 2.26.2.Cisco MIME-Version: 1.0 X-Outbound-SMTP-Client: 161.44.237.36, ott-ads-268.cisco.com X-Outbound-Node: rcdn-core-1.cisco.com X-Mailman-Approved-At: Sat, 10 Apr 2021 16:15:40 +0200 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.4 at phobos.denx.de X-Virus-Status: Clean From: Joe Xue The command is to query and show mac address of a specific ipAddress. Signed-off-by: Joe Xue --- cmd/Kconfig | 6 ++++++ cmd/net.c | 36 ++++++++++++++++++++++++++++++++++++ include/net.h | 5 +++++ net/arp.c | 24 ++++++++++++++++++++++++ net/arp.h | 4 ++++ net/net.c | 8 ++++++++ 6 files changed, 83 insertions(+) diff --git a/cmd/Kconfig b/cmd/Kconfig index 9bf5e863e4..1da4cb67f6 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -1587,6 +1587,12 @@ config CMD_PING help Send ICMP ECHO_REQUEST to network host +config CMD_ARP + bool "arp" + help + Sends ARP_REQUEST to network host and shows the result if there is arp + response. + config CMD_CDP bool "cdp" help diff --git a/cmd/net.c b/cmd/net.c index beb2877dfd..369d15474c 100644 --- a/cmd/net.c +++ b/cmd/net.c @@ -479,4 +479,40 @@ U_BOOT_CMD( "" ); +#ifdef CONFIG_CMD_ARP +static int do_arp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) +{ + u8 *ethaddr = arp_query_ethaddr; + + if (argc < 2) + return CMD_RET_USAGE; + + arp_query_ip = string_to_ip(argv[1]); + if (arp_query_ip.s_addr == 0) + return CMD_RET_USAGE; + + if ((arp_query_ip.s_addr & net_netmask.s_addr) != + (net_ip.s_addr & net_netmask.s_addr)) { + printf("The host %s is not in the same network\n", argv[1]); + return CMD_RET_SUCCESS; + } + + if (net_loop(ARP) < 0) { + printf("arp failed; host %s is not alive\n", argv[1]); + return CMD_RET_FAILURE; + } + + printf("%s\t%02x:%02x:%02x:%02x:%02x:%02x\n", argv[1], ethaddr[0], + ethaddr[1], ethaddr[2], ethaddr[3], ethaddr[4], ethaddr[5]); + + return CMD_RET_SUCCESS; +} + +U_BOOT_CMD( + arp, 2, 1, do_arp, + "send ARP ARP_REQUEST to network host", + "ipAddress" +); +#endif + #endif /* CONFIG_CMD_LINK_LOCAL */ diff --git a/include/net.h b/include/net.h index b95d6a6f60..60b4bf610e 100644 --- a/include/net.h +++ b/include/net.h @@ -580,6 +580,11 @@ extern char *net_dns_env_var; /* the env var to put the ip into */ extern struct in_addr net_ping_ip; /* the ip address to ping */ #endif +#if defined(CONFIG_CMD_ARP) +extern u8 arp_query_ethaddr[6]; /* the arp query result */ +extern struct in_addr arp_query_ip; /* the ip address to arp query */ +#endif + #if defined(CONFIG_CMD_CDP) /* when CDP completes these hold the return values */ extern ushort cdp_native_vlan; /* CDP returned native VLAN */ diff --git a/net/arp.c b/net/arp.c index 1d06ed2572..83c24072d7 100644 --- a/net/arp.c +++ b/net/arp.c @@ -220,11 +220,20 @@ void arp_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr, 0, len); +#ifdef CONFIG_CMD_ARP + if (arp_query_ip.s_addr != 0) { + arp_query_ip.s_addr = 0; + net_set_state(NETLOOP_SUCCESS); + } else { +#endif /* set the mac address in the waiting packet's header and transmit it */ memcpy(((struct ethernet_hdr *)net_tx_packet)->et_dest, &arp->ar_sha, ARP_HLEN); net_send_packet(net_tx_packet, arp_wait_tx_packet_size); +#ifdef CONFIG_CMD_ARP + } +#endif /* no arp request pending now */ net_arp_wait_packet_ip.s_addr = 0; @@ -243,3 +252,18 @@ bool arp_is_waiting(void) { return !!net_arp_wait_packet_ip.s_addr; } + +#ifdef CONFIG_CMD_ARP +u8 arp_query_ethaddr[6]; +struct in_addr arp_query_ip; + +void arp_query(void) +{ + arp_wait_packet_ethaddr = arp_query_ethaddr; + net_arp_wait_packet_ip = arp_query_ip; + + arp_wait_timer_start = get_timer(0); + printf("Using %s device\n", eth_get_name()); + arp_request(); +} +#endif diff --git a/net/arp.h b/net/arp.h index 25b3c00d5c..f3e5cb8504 100644 --- a/net/arp.h +++ b/net/arp.h @@ -29,4 +29,8 @@ void arp_raw_request(struct in_addr source_ip, const uchar *targetEther, int arp_timeout_check(void); void arp_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len); +#ifdef CONFIG_CMD_ARP +void arp_query(void); +#endif + #endif /* __ARP_H__ */ diff --git a/net/net.c b/net/net.c index b58f3062b2..21623562e8 100644 --- a/net/net.c +++ b/net/net.c @@ -492,6 +492,11 @@ restart: rarp_request(); break; #endif +#if defined(CONFIG_CMD_ARP) + case ARP: + arp_query(); + break; +#endif #if defined(CONFIG_CMD_PING) case PING: ping_start(); @@ -1385,6 +1390,9 @@ common: case CDP: case DHCP: case LINKLOCAL: +#ifdef CONFIG_CMD_ARP + case ARP: +#endif if (memcmp(net_ethaddr, "\0\0\0\0\0\0", 6) == 0) { int num = eth_get_dev_index();