All of lore.kernel.org
 help / color / mirror / Atom feed
From: Duncan Roe <duncan_roe@optusnet.com.au>
To: pablo@netfilter.org
Cc: netfilter-devel@vger.kernel.org
Subject: [PATCH RFC libnetfilter_queue] src: Simplify struct pkt_buff
Date: Fri, 10 Jan 2020 15:09:25 +1100	[thread overview]
Message-ID: <20200110040925.16124-1-duncan_roe@optusnet.com.au> (raw)

In struct pkt_buff:
- We only ever needed any 2 of len, data and tail.
  This has caused bugs in the past, e.g. commit 8a4316f31.
  Delete len, and where the value of pktb->len was required,
  use new PKTB_LEN macro.
- head and data always had the same value.
  head was in the minority, so replace with data where it was used.

Signed-off-by: Duncan Roe <duncan_roe@optusnet.com.au>
---
 src/extra/ipv4.c    |  2 +-
 src/extra/pktbuff.c | 21 +++++++--------------
 src/internal.h      |  3 +--
 3 files changed, 9 insertions(+), 17 deletions(-)

diff --git a/src/extra/ipv4.c b/src/extra/ipv4.c
index c03f23f..8e36861 100644
--- a/src/extra/ipv4.c
+++ b/src/extra/ipv4.c
@@ -70,7 +70,7 @@ int nfq_ip_set_transport_header(struct pkt_buff *pktb, struct iphdr *iph)
 	int doff = iph->ihl * 4;
 
 	/* Wrong offset to IPv4 payload. */
-	if ((int)pktb->len - doff <= 0)
+	if ((int)PKTB_LEN - doff <= 0)
 		return -1;
 
 	pktb->transport_header = pktb->network_header + doff;
diff --git a/src/extra/pktbuff.c b/src/extra/pktbuff.c
index 37f6bc0..0b67029 100644
--- a/src/extra/pktbuff.c
+++ b/src/extra/pktbuff.c
@@ -62,12 +62,10 @@ struct pkt_buff *pktb_alloc(int family, void *data, size_t len, size_t extra)
 	pkt_data = (uint8_t *)pktb + sizeof(struct pkt_buff);
 	memcpy(pkt_data, data, len);
 
-	pktb->len = len;
 	pktb->data_len = len + extra;
 
-	pktb->head = pkt_data;
 	pktb->data = pkt_data;
-	pktb->tail = pktb->head + len;
+	pktb->tail = pktb->data + len;
 
 	switch(family) {
 	case AF_INET:
@@ -121,7 +119,7 @@ uint8_t *pktb_data(struct pkt_buff *pktb)
 EXPORT_SYMBOL
 uint32_t pktb_len(struct pkt_buff *pktb)
 {
-	return pktb->len;
+	return PKTB_LEN;
 }
 
 /**
@@ -169,7 +167,6 @@ EXPORT_SYMBOL
 void pktb_push(struct pkt_buff *pktb, unsigned int len)
 {
 	pktb->data -= len;
-	pktb->len += len;
 }
 
 /**
@@ -181,7 +178,6 @@ EXPORT_SYMBOL
 void pktb_pull(struct pkt_buff *pktb, unsigned int len)
 {
 	pktb->data += len;
-	pktb->len -= len;
 }
 
 /**
@@ -193,7 +189,6 @@ EXPORT_SYMBOL
 void pktb_put(struct pkt_buff *pktb, unsigned int len)
 {
 	pktb->tail += len;
-	pktb->len += len;
 }
 
 /**
@@ -204,8 +199,7 @@ void pktb_put(struct pkt_buff *pktb, unsigned int len)
 EXPORT_SYMBOL
 void pktb_trim(struct pkt_buff *pktb, unsigned int len)
 {
-	pktb->len = len;
-	pktb->tail = pktb->head + len;
+	pktb->tail = pktb->data + len;
 }
 
 /**
@@ -224,7 +218,7 @@ void pktb_trim(struct pkt_buff *pktb, unsigned int len)
 EXPORT_SYMBOL
 unsigned int pktb_tailroom(struct pkt_buff *pktb)
 {
-	return pktb->data_len - pktb->len;
+	return pktb->data_len - PKTB_LEN;
 }
 
 /**
@@ -277,17 +271,16 @@ static int pktb_expand_tail(struct pkt_buff *pktb, int extra)
 	 * reallocation. Instead, increase the size of the extra room in
 	 * the tail in pktb_alloc.
 	 */
-	if (pktb->len + extra > pktb->data_len)
+	if (PKTB_LEN + extra > pktb->data_len)
 		return 0;
 
-	pktb->len += extra;
 	pktb->tail = pktb->tail + extra;
 	return 1;
 }
 
 static int enlarge_pkt(struct pkt_buff *pktb, unsigned int extra)
 {
-	if (pktb->len + extra > 65535)
+	if (PKTB_LEN + extra > 65535)
 		return 0;
 
 	if (!pktb_expand_tail(pktb, extra - pktb_tailroom(pktb)))
@@ -346,7 +339,7 @@ int pktb_mangle(struct pkt_buff *pktb,
 	if (rep_len > match_len)
 		pktb_put(pktb, rep_len - match_len);
 	else
-		pktb_trim(pktb, pktb->len + rep_len - match_len);
+		pktb_trim(pktb, PKTB_LEN + rep_len - match_len);
 
 	pktb->mangled = true;
 	return 1;
diff --git a/src/internal.h b/src/internal.h
index d968325..95ac03e 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -9,6 +9,7 @@
 #else
 #	define EXPORT_SYMBOL
 #endif
+#define PKTB_LEN (pktb->tail - pktb->data)
 
 struct iphdr;
 struct ip6_hdr;
@@ -23,11 +24,9 @@ struct pkt_buff {
 	uint8_t *network_header;
 	uint8_t *transport_header;
 
-	uint8_t *head;
 	uint8_t *data;
 	uint8_t *tail;
 
-	uint32_t len;
 	uint32_t data_len;
 
 	bool	mangled;
-- 
2.14.5


             reply	other threads:[~2020-01-10  4:09 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-10  4:09 Duncan Roe [this message]
2020-01-10 11:12 ` [PATCH RFC libnetfilter_queue] src: Simplify struct pkt_buff Pablo Neira Ayuso
2020-01-17 11:32   ` [PATCH libnetfilter_queue] src: Simplify struct pkt_buff: remove head Duncan Roe
2020-01-17 14:09     ` [PATCH libnetfilter_queue] src: Simplify struct pkt_buff: remove tail Duncan Roe
2020-01-18 20:43       ` Pablo Neira Ayuso
2020-01-26  4:02         ` [PATCH libnetfilter_queue v2 0/1] " Duncan Roe
2020-01-26  4:02         ` [PATCH libnetfilter_queue v2 1/1] " Duncan Roe
2020-01-29 17:39           ` Pablo Neira Ayuso
2020-01-18 20:40     ` [PATCH libnetfilter_queue] src: Simplify struct pkt_buff: remove head Pablo Neira Ayuso

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=20200110040925.16124-1-duncan_roe@optusnet.com.au \
    --to=duncan_roe@optusnet.com.au \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.