mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Jules Maselbas <jmaselbas@kalray.eu>
To: barebox@lists.infradead.org
Cc: Jules Maselbas <jmaselbas@kalray.eu>
Subject: [RFC PATCH 5/5] Add tcpdump command
Date: Tue,  9 Aug 2022 15:20:21 +0200	[thread overview]
Message-ID: <20220809132021.7110-5-jmaselbas@kalray.eu> (raw)
In-Reply-To: <20220809132021.7110-1-jmaselbas@kalray.eu>

This a mirror of the ethlog command but this will only dump tcp traffic
to the console.

This tcpdump command could be merged with the ethlog, also this could
also evolve to dump udp and bad packets.

Signed-off-by: Jules Maselbas <jmaselbas@kalray.eu>
---
 commands/Kconfig   |  8 +++++
 commands/Makefile  |  1 +
 commands/tcpdump.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++
 include/net.h      |  1 +
 net/net.c          | 10 ++++--
 5 files changed, 102 insertions(+), 2 deletions(-)
 create mode 100644 commands/tcpdump.c

diff --git a/commands/Kconfig b/commands/Kconfig
index 5faf8eccc1..cb27ce29ce 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -1289,6 +1289,14 @@ config CMD_ETHLOG
 
 	  Usage: ethlog [-r] [DEVICENAME]
 
+config CMD_TCPDUMP
+	tristate
+	prompt "tcpdump"
+	help
+	  dump tcp traffic.
+
+	  Usage: tcpdump [-r] [DEVICENAME]
+
 # end Network commands
 endmenu
 
diff --git a/commands/Makefile b/commands/Makefile
index ecdcfe9619..a09be2c8a3 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -104,6 +104,7 @@ obj-$(CONFIG_CMD_LN)		+= ln.o
 obj-$(CONFIG_CMD_CLK)		+= clk.o
 obj-$(CONFIG_CMD_KEYSTORE)	+= keystore.o
 obj-$(CONFIG_CMD_TFTP)		+= tftp.o
+obj-$(CONFIG_CMD_TCPDUMP)	+= tcpdump.o
 obj-$(CONFIG_CMD_FILETYPE)	+= filetype.o
 obj-$(CONFIG_CMD_BAREBOX_UPDATE)+= barebox-update.o
 obj-$(CONFIG_CMD_MIITOOL)	+= miitool.o
diff --git a/commands/tcpdump.c b/commands/tcpdump.c
new file mode 100644
index 0000000000..bcfe42a897
--- /dev/null
+++ b/commands/tcpdump.c
@@ -0,0 +1,84 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// SPDX-FileCopyrightText: (c) 2022 Jules Maselbas <jmaselbas@kalray.eu>
+
+#include <common.h>
+#include <command.h>
+#include <complete.h>
+#include <environment.h>
+#include <getopt.h>
+#include <net.h>
+
+static void tcp_dump(struct eth_device *edev, void *pkt, int len)
+{
+	struct iphdr *ip = net_eth_to_iphdr(pkt);
+	struct tcphdr *tcp = net_eth_to_tcphdr(pkt);
+	uint16_t flag = ntohs(tcp->doff_flag) & TCP_FLAG_MASK;
+	int opt = net_tcp_data_offset(tcp) - sizeof(struct tcphdr);
+	char cksum[sizeof("0xffff")];
+	char flags[sizeof("FSRPAU")] = {};
+	char *f = flags;
+
+	if (flag & TCP_FLAG_FIN) *f++ = 'F';
+	if (flag & TCP_FLAG_SYN) *f++ = 'S';
+	if (flag & TCP_FLAG_RST) *f++ = 'R';
+	if (flag & TCP_FLAG_PSH) *f++ = 'P';
+	if (flag & TCP_FLAG_ACK) *f++ = 'A';
+	if (flag & TCP_FLAG_URG) *f++ = 'U';
+
+	snprintf(cksum, sizeof(cksum), "%#.4x", tcp_checksum(ip, tcp, len));
+	pr_debug("%pI4:%u > %pI4:%u [%s] cksum %#.4x (%s) seq %u ack %u win %u opt [%d] len %d\n",
+		&ip->saddr, ntohs(tcp->src), &ip->daddr, ntohs(tcp->dst),
+		flags, ntohs(tcp->sum),
+		tcp_checksum_ok(ip, tcp, len) ? "correct" : cksum,
+		ntohl(tcp->seq), ntohl(tcp->ack), ntohs(tcp->wnd),
+		opt, len);
+}
+
+static int do_tcpdump(int argc, char *argv[])
+{
+	struct eth_device *edev;
+	const char *edevname;
+	bool remove = false;
+	int opt;
+
+	while ((opt = getopt(argc, argv, "r")) > 0) {
+		switch (opt) {
+		case 'r':
+			remove = true;
+			break;
+		default:
+			return COMMAND_ERROR_USAGE;
+		}
+	}
+
+	if (optind == argc)
+		edevname = "eth0";
+	else
+		edevname = argv[optind];
+
+	edev = eth_get_byname(edevname);
+	if (!edev) {
+		printf("No such network device: %s\n", edevname);
+		return 1;
+	}
+
+	if (remove)
+		edev->tcp_dump = NULL;
+	else
+		edev->tcp_dump = tcp_dump;
+
+	return 0;
+}
+
+BAREBOX_CMD_HELP_START(tcpdump)
+BAREBOX_CMD_HELP_TEXT("Options:")
+BAREBOX_CMD_HELP_OPT("-r", "remove log handler from Ethernet interface")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(tcpdump)
+	.cmd		= do_tcpdump,
+	BAREBOX_CMD_DESC("tcpdump - tool to get dump of TCP packets")
+	BAREBOX_CMD_OPTS("[-r] [device]")
+	BAREBOX_CMD_GROUP(CMD_GRP_NET)
+	BAREBOX_CMD_COMPLETE(eth_complete)
+BAREBOX_CMD_END
diff --git a/include/net.h b/include/net.h
index 76b64ccb21..ff9c7dc1f7 100644
--- a/include/net.h
+++ b/include/net.h
@@ -48,6 +48,7 @@ struct eth_device {
 				 int *length);
 	void (*rx_monitor) (struct eth_device*, void *packet, int length);
 	void (*tx_monitor) (struct eth_device*, void *packet, int length);
+	void (*tcp_dump) (struct eth_device*, void *packet, int length);
 
 	struct eth_device *next;
 	void *priv;
diff --git a/net/net.c b/net/net.c
index 855bb8e4c2..1717a0726f 100644
--- a/net/net.c
+++ b/net/net.c
@@ -586,6 +586,9 @@ static int tcp_send(struct net_connection *con, int len, uint16_t flags)
 	con->tcp->sum = 0;
 	con->tcp->sum = ~tcp_checksum(con->ip, con->tcp, len);
 
+	if (con->edev->tcp_dump)
+		con->edev->tcp_dump(con->edev, con->packet, len);
+
 	return net_ip_send(con, hdr_size + len);
 }
 
@@ -848,7 +851,7 @@ static int net_handle_udp(unsigned char *pkt, int len)
 	return -EINVAL;
 }
 
-static int net_handle_tcp(unsigned char *pkt, int len)
+static int net_handle_tcp(struct eth_device *edev, unsigned char *pkt, int len)
 {
 	size_t min_size = ETHER_HDR_SIZE + sizeof(struct iphdr);
 	struct net_connection *con;
@@ -890,6 +893,9 @@ static int net_handle_tcp(unsigned char *pkt, int len)
 		goto bad;
 	tcb = &con->tcb;
 
+	if (edev->tcp_dump)
+		edev->tcp_dump(edev, pkt, len);
+
 	/* segment arrives */
 	seg_last = seg_seq + seg_len - 1;
 	rcv_wnd = tcb->rcv_wnd;
@@ -1116,7 +1122,7 @@ static int net_handle_ip(struct eth_device *edev, unsigned char *pkt, int len)
 	case IPPROTO_UDP:
 		return net_handle_udp(pkt, len);
 	case IPPROTO_TCP:
-		return net_handle_tcp(pkt, len);
+		return net_handle_tcp(edev, pkt, len);
 	}
 
 	return 0;
-- 
2.17.1




      parent reply	other threads:[~2022-08-09 13:22 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-09 13:20 [RFC PATCH 1/5] net: Add a function to retrieve UDP connection Jules Maselbas
2022-08-09 13:20 ` [RFC PATCH 2/5] net: Implement source port randomization Jules Maselbas
2022-08-09 13:20 ` [RFC PATCH 3/5] net: Add simple TCP support Jules Maselbas
2022-08-12  7:04   ` Sascha Hauer
2022-08-12 16:17     ` Jules Maselbas
2022-08-09 13:20 ` [RFC PATCH 4/5] Add irc command Jules Maselbas
2022-08-09 13:20 ` Jules Maselbas [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220809132021.7110-5-jmaselbas@kalray.eu \
    --to=jmaselbas@kalray.eu \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox