diff mbox series

[xtables-addons,3/3] xt_geoip: fix in6_addr little-endian byte-swapping.

Message ID 20191130170219.368867-5-jeremy@azazel.net
State Changes Requested
Delegated to: Pablo Neira
Headers show
Series [xtables-addons,1/3] configure: Fix max. supported kernel version. | expand

Commit Message

Jeremy Sowden Nov. 30, 2019, 5:02 p.m. UTC
libxt_geoip attempts to byte-swap IPv6 addresses on little-endian
systems but it doesn't get it quite right.  Rather than doing ntohl on
each 32-bit segment, it does ntohs on each 16-bit segment.

This means that:

  1234::cdef

becomes:

  2143::dcfe

instead of:

  4321::fedc

Fixes: b91dbd03c717 ("geoip: store database in network byte order")
Reported-by: "Thomas B. Clark" <kernel@clark.bz>
Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
---
 extensions/libxt_geoip.c | 28 ++++++++--------------------
 1 file changed, 8 insertions(+), 20 deletions(-)

Comments

Jeremy Sowden Nov. 30, 2019, 5:11 p.m. UTC | #1
On 2019-11-30, at 17:02:19 +0000, Jeremy Sowden wrote:
> libxt_geoip attempts to byte-swap IPv6 addresses on little-endian
> systems but it doesn't get it quite right.  Rather than doing ntohl on
> each 32-bit segment, it does ntohs on each 16-bit segment.
>
> This means that:
>
>   1234::cdef
>
> becomes:
>
>   2143::dcfe
>
> instead of:
>
>   4321::fedc

I'm an idiot.  This commit message is wrong.  Will fix and resend.

J.
diff mbox series

Patch

diff --git a/extensions/libxt_geoip.c b/extensions/libxt_geoip.c
index 116f5f86eb01..5b8697dc6161 100644
--- a/extensions/libxt_geoip.c
+++ b/extensions/libxt_geoip.c
@@ -50,26 +50,6 @@  static struct option geoip_opts[] = {
 };
 
 #if __BYTE_ORDER == __LITTLE_ENDIAN
-static void geoip_swap_le16(uint16_t *buf)
-{
-	unsigned char *p = (void *)buf;
-	uint16_t n= p[0] + (p[1] << 8);
-	p[0] = (n >> 8) & 0xff;
-	p[1] = n & 0xff;
-}
-
-static void geoip_swap_in6(struct in6_addr *in6)
-{
-	geoip_swap_le16(&in6->s6_addr16[0]);
-	geoip_swap_le16(&in6->s6_addr16[1]);
-	geoip_swap_le16(&in6->s6_addr16[2]);
-	geoip_swap_le16(&in6->s6_addr16[3]);
-	geoip_swap_le16(&in6->s6_addr16[4]);
-	geoip_swap_le16(&in6->s6_addr16[5]);
-	geoip_swap_le16(&in6->s6_addr16[6]);
-	geoip_swap_le16(&in6->s6_addr16[7]);
-}
-
 static void geoip_swap_le32(uint32_t *buf)
 {
 	unsigned char *p = (void *)buf;
@@ -79,6 +59,14 @@  static void geoip_swap_le32(uint32_t *buf)
 	p[2] = (n >> 8) & 0xff;
 	p[3] = n & 0xff;
 }
+
+static void geoip_swap_in6(struct in6_addr *in6)
+{
+	geoip_swap_le32(&in6->s6_addr32[0]);
+	geoip_swap_le32(&in6->s6_addr32[1]);
+	geoip_swap_le32(&in6->s6_addr32[2]);
+	geoip_swap_le32(&in6->s6_addr32[3]);
+}
 #endif
 
 static void *