From patchwork Fri Jun 14 06:18:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ramon Fried X-Patchwork-Id: 1115724 X-Patchwork-Delegate: joe.hershberger@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="g7fWLb9u"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 45Q9R74mV8z9s9y for ; Fri, 14 Jun 2019 16:18:43 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id 5BD3AC21F50; Fri, 14 Jun 2019 06:18:41 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_MSPIKE_H2, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id E60B4C21EC9; Fri, 14 Jun 2019 06:18:37 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 67A8EC21EC9; Fri, 14 Jun 2019 06:18:36 +0000 (UTC) Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by lists.denx.de (Postfix) with ESMTPS id 093DDC21E0D for ; Fri, 14 Jun 2019 06:18:36 +0000 (UTC) Received: by mail-wr1-f66.google.com with SMTP id n4so1135440wrw.13 for ; Thu, 13 Jun 2019 23:18:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=0yruSyli1GXebwieFN3M5w6g9MBauMOL/Gwemqtkx+4=; b=g7fWLb9uuqKcEnEsiB2+6wmbNaXRyWhbiReI0p6zOn7Q/xZoHxqSkAtVnYubKf2/UU bngewlqN4rcEXuEm46JxfBmErWp0Du9ILte4DxJj2L4uRbsHHdHu48QrpHo9+NRtDhmD 1+ivBmzTO3Az6tMcBQYXSBs8ma5GPFA5LhgMUU0nqud1oi7lMmycmpURvNB0A1Zw9stV eZUitwd1PlBN1VreHNMA9zLHM9uhk6ILHpYLyscqbnxqIS9uMZKrMMpgIBntFjgezovy W2Nvbp3AZ5EUzkvBXH4bXh8Ox7fyn7NRFH3vT2/BSanIFx8Mbj3HDB3cRAlonfDuiSbg 6zgQ== 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:mime-version :content-transfer-encoding; bh=0yruSyli1GXebwieFN3M5w6g9MBauMOL/Gwemqtkx+4=; b=B7FvQ5+uYkDbHjM9HgKgOCDS1AouOFsML8xzyiQ0HMsEz3qRGxRJ4KWlzgiSxILluF BqVlCioh/lmBrWytOdXSpp8PuFX0l2Lb5+Qsn743Lu7geTW5zH1f69ufaBfXRoWgwWvg aUBNS21jPkKzdMrmj/bKisiwv3/YMbQ4SwbMklzKtDeha9e6MpsyWQiiTV92G24cPCDg OO1ZITv8NkIXkVOZshlaFNkjVgjjPQqG1KEMUHGRUlYaYQqVpJ0Fb32LeEiE/Irljhp5 wc8d3yxfCGSOOQeMt8253AvY2NHS+SXPUJII+GIfGa+6GhOrfotitVRE2/WdeuWBuLbG JDTw== X-Gm-Message-State: APjAAAWdSG9Lz2Q6tjTTl+ZapxRr9gSNMjSBoZ86USEdypybD+xtq4sc vMrMk8Wd2ffQtbL0R30csnI4h1/Tyms= X-Google-Smtp-Source: APXvYqypGk/ZCwt4C0+fwcSkVU7jl/YVGX8OrdUnq9ftetKYArzlsoSVSCeOXXdWBlFz0/u/YUUPfg== X-Received: by 2002:a5d:69ce:: with SMTP id s14mr14888189wrw.135.1560493115478; Thu, 13 Jun 2019 23:18:35 -0700 (PDT) Received: from localhost.localdomain ([141.226.31.91]) by smtp.gmail.com with ESMTPSA id a3sm1478691wmb.35.2019.06.13.23.18.33 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Thu, 13 Jun 2019 23:18:34 -0700 (PDT) From: Ramon Fried To: u-boot@lists.denx.de Date: Fri, 14 Jun 2019 09:18:14 +0300 Message-Id: <20190614061814.6821-1-rfried.dev@gmail.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Cc: Duncan Hare , Alexander Graf , Joe Hershberger , Lothar Felten , Chris Packham Subject: [U-Boot] [PATCH] net: introduce packet capture support X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 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" Add support for capturing ethernet packets and storing them in memory in PCAP(2.4) format, later to be analyzed by any PCAP viewer software (IE. Wireshark) This feature greatly assist debugging network issues such as detecting dropped packets, packet corruption etc. Signed-off-by: Ramon Fried Reviewed-by: Alex Marginean Tested-by: Alex Marginean --- include/net.h | 29 ++++++++++++++++++ net/Kconfig | 22 ++++++++++++++ net/Makefile | 1 + net/net.c | 11 +++++++ net/pcap.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 146 insertions(+) create mode 100644 net/pcap.c diff --git a/include/net.h b/include/net.h index 44b32385c4..d1dc864896 100644 --- a/include/net.h +++ b/include/net.h @@ -630,6 +630,31 @@ bool arp_is_waiting(void); /* Waiting for ARP reply? */ void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */ void net_set_timeout_handler(ulong, thand_f *);/* Set timeout handler */ +/* PCAP extension */ + +/** + * pcap_init() - Initialize PCAP memory buffer + * + * @return 0 on success, -ERROR on error + */ +int pcap_init(void); + +/** + * pcap_post() - Post a packet to PCAP file + * + * @packet: packet to post + * @len: packet length in bytes + * @return 0 on success, -ERROR on error + */ +int pcap_post(const void *packet, size_t len); + +/** + * pcap_size() - get size of PCAP file + * + * @return size of PCAP file in bytes + */ +unsigned int pcap_size(void); + /* Network loop state */ enum net_loop_state { NETLOOP_CONTINUE, @@ -658,6 +683,10 @@ static inline void net_send_packet(uchar *pkt, int len) { /* Currently no way to return errors from eth_send() */ (void) eth_send(pkt, len); + +#if defined(CONFIG_NET_PCAP) + pcap_post(pkt, len); +#endif } /* diff --git a/net/Kconfig b/net/Kconfig index f2363e5256..8b8ab88e57 100644 --- a/net/Kconfig +++ b/net/Kconfig @@ -8,6 +8,28 @@ menuconfig NET if NET +config NET_PCAP + bool "PCAP network capture" + help + Selecting this will capture all Ethernet packets and store + them in a PCAP formated file later to be analyzed by PCAP + reader application (IE. WireShark). + +config NET_PCAP_ADDR + hex "Address of PCAP file in memory" + depends on NET_PCAP + help + Sets the address of the PCAP file in physical memory. + +config NET_PCAP_SIZE + hex "Size of PCAP file" + depends on NET_PCAP + default 0x100000 + help + Sets the maximum size of PCAP file in bytes. + When it fills up completely, it will stop capturing + packets silently. + config NET_RANDOM_ETHADDR bool "Random ethaddr if unset" help diff --git a/net/Makefile b/net/Makefile index ce36362168..85103fee93 100644 --- a/net/Makefile +++ b/net/Makefile @@ -18,6 +18,7 @@ endif obj-$(CONFIG_NET) += eth_common.o obj-$(CONFIG_CMD_LINK_LOCAL) += link_local.o obj-$(CONFIG_NET) += net.o +obj-$(CONFIG_NET_PCAP) += pcap.o obj-$(CONFIG_CMD_NFS) += nfs.o obj-$(CONFIG_CMD_PING) += ping.o obj-$(CONFIG_CMD_RARP) += rarp.o diff --git a/net/net.c b/net/net.c index 58b0417cbe..d0bdf8fe20 100644 --- a/net/net.c +++ b/net/net.c @@ -387,6 +387,10 @@ void net_init(void) /* Only need to setup buffer pointers once. */ first_call = 0; + +#if defined(CONFIG_NET_PCAP) + pcap_init(); +#endif } net_init_loop(); @@ -671,6 +675,10 @@ done: net_set_icmp_handler(NULL); #endif net_set_state(prev_net_state); + +#if defined(CONFIG_NET_PCAP) + printf("PCAP len: 0x%x\n", pcap_size()); +#endif return ret; } @@ -1083,6 +1091,9 @@ void net_process_received_packet(uchar *in_packet, int len) debug_cond(DEBUG_NET_PKT, "packet received\n"); +#if defined(CONFIG_NET_PCAP) + pcap_post(in_packet, len); +#endif net_rx_packet = in_packet; net_rx_packet_len = len; et = (struct ethernet_hdr *)in_packet; diff --git a/net/pcap.c b/net/pcap.c new file mode 100644 index 0000000000..dadfa7563b --- /dev/null +++ b/net/pcap.c @@ -0,0 +1,83 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright 2019 Ramon Fried + */ + +#include +#include +#include +#include + +#define LINKTYPE_ETHERNET 1 + +static void *buf; +static unsigned int pos; + +struct pcap_header { + u32 magic; + u16 version_major; + u16 version_minor; + s32 thiszone; + u32 sigfigs; + u32 snaplen; + u32 network; +}; + +struct pcap_packet_header { + u32 ts_sec; + u32 ts_usec; + u32 incl_len; + u32 orig_len; +}; + +static struct pcap_header file_header = { + .magic = 0xa1b2c3d4, + .version_major = 2, + .version_minor = 4, + .snaplen = 65535, + .network = LINKTYPE_ETHERNET, +}; + +int pcap_init(void) +{ + buf = map_physmem(CONFIG_NET_PCAP_ADDR, CONFIG_NET_PCAP_SIZE, 0); + if (!buf) { + printf("Failed mapping PCAP memory\n"); + return -ENOMEM; + } + + printf("PCAP capture initialized: addr: 0x%lx max length: 0x%x\n", + (unsigned long)buf, CONFIG_NET_PCAP_SIZE); + + memcpy(buf, &file_header, sizeof(file_header)); + pos += sizeof(file_header); + return 0; +} + +int pcap_post(const void *packet, size_t len) +{ + struct pcap_packet_header header; + u64 cur_time = timer_get_us(); + + if (!buf) + return -ENODEV; + if ((pos + len + sizeof(header)) >= CONFIG_NET_PCAP_SIZE) + return -ENOMEM; + + header.ts_sec = cur_time / 1000000; + header.ts_sec = cur_time % 1000000; + header.incl_len = len; + header.orig_len = len; + + memcpy(buf + pos, &header, sizeof(header)); + pos += sizeof(header); + memcpy(buf + pos, packet, len); + pos += len; + + return 0; +} + +unsigned int pcap_size(void) +{ + return pos; +}