public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top, Stefano Brivio <sbrivio@redhat.com>
Cc: jlesev@gmail.com, David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 3/8] tcp: Remove some redundant packet_get() operations
Date: Fri,  8 Sep 2023 11:49:48 +1000	[thread overview]
Message-ID: <20230908014953.822952-4-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20230908014953.822952-1-david@gibson.dropbear.id.au>

Both tcp_data_from_tap() and tcp_tap_handler() call packet_get() to get
the entire L4 packet length, then immediately call it again to check that
the packet is long enough to include a TCP header.  The features of
packet_get() let us easily combine these together, we just need to adjust
the length slightly, because we want the value to include the TCP header
length.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 tcp.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)

diff --git a/tcp.c b/tcp.c
index d8c2327..6a34f82 100644
--- a/tcp.c
+++ b/tcp.c
@@ -2320,16 +2320,12 @@ static void tcp_data_from_tap(struct ctx *c, struct tcp_tap_conn *conn,
 		char *data;
 		size_t off;
 
-		if (!packet_get(p, i, 0, 0, &len)) {
-			tcp_rst(c, conn);
-			return;
-		}
-
-		th = packet_get(p, i, 0, sizeof(*th), NULL);
+		th = packet_get(p, i, 0, sizeof(*th), &len);
 		if (!th) {
 			tcp_rst(c, conn);
 			return;
 		}
+		len += sizeof(*th);
 
 		off = th->doff * 4UL;
 		if (off < sizeof(*th) || off > len) {
@@ -2545,12 +2541,10 @@ int tcp_tap_handler(struct ctx *c, int af, const void *saddr, const void *daddr,
 	int ack_due = 0;
 	char *opts;
 
-	if (!packet_get(p, idx, 0, 0, &len))
-		return 1;
-
-	th = packet_get(p, idx, 0, sizeof(*th), NULL);
+	th = packet_get(p, idx, 0, sizeof(*th), &len);
 	if (!th)
 		return 1;
+	len += sizeof(*th);
 
 	optlen = th->doff * 4UL - sizeof(*th);
 	/* Static checkers might fail to see this: */
-- 
@@ -2320,16 +2320,12 @@ static void tcp_data_from_tap(struct ctx *c, struct tcp_tap_conn *conn,
 		char *data;
 		size_t off;
 
-		if (!packet_get(p, i, 0, 0, &len)) {
-			tcp_rst(c, conn);
-			return;
-		}
-
-		th = packet_get(p, i, 0, sizeof(*th), NULL);
+		th = packet_get(p, i, 0, sizeof(*th), &len);
 		if (!th) {
 			tcp_rst(c, conn);
 			return;
 		}
+		len += sizeof(*th);
 
 		off = th->doff * 4UL;
 		if (off < sizeof(*th) || off > len) {
@@ -2545,12 +2541,10 @@ int tcp_tap_handler(struct ctx *c, int af, const void *saddr, const void *daddr,
 	int ack_due = 0;
 	char *opts;
 
-	if (!packet_get(p, idx, 0, 0, &len))
-		return 1;
-
-	th = packet_get(p, idx, 0, sizeof(*th), NULL);
+	th = packet_get(p, idx, 0, sizeof(*th), &len);
 	if (!th)
 		return 1;
+	len += sizeof(*th);
 
 	optlen = th->doff * 4UL - sizeof(*th);
 	/* Static checkers might fail to see this: */
-- 
2.41.0


  parent reply	other threads:[~2023-09-08  1:50 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-08  1:49 [PATCH 0/8] Fix a number of bugs with handling of TCP packets from tap David Gibson
2023-09-08  1:49 ` [PATCH 1/8] tcp, tap: Correctly advance through packets in tcp_tap_handler() David Gibson
2023-09-08  1:49 ` [PATCH 2/8] udp, tap: Correctly advance through packets in udp_tap_handler() David Gibson
2023-09-08  1:49 ` David Gibson [this message]
2023-09-08  1:49 ` [PATCH 4/8] tcp: Never hash match closed connections David Gibson
2023-09-08  1:49 ` [PATCH 5/8] tcp: Return consumed packet count from tcp_data_from_tap() David Gibson
2023-09-08  1:49 ` [PATCH 6/8] tcp: Correctly handle RST followed rapidly by SYN David Gibson
2023-09-08  1:49 ` [PATCH 7/8] tcp: Consolidate paths where we initiate reset on tap interface David Gibson
2023-09-08  1:49 ` [PATCH 8/8] tcp: Correct handling of FIN,ACK followed by SYN David Gibson
2023-09-08 15:27 ` [PATCH 0/8] Fix a number of bugs with handling of TCP packets from tap Stefano Brivio

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=20230908014953.822952-4-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=jlesev@gmail.com \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).