From d746ecb0e494e7200180c3886fde9664d9100729 Mon Sep 17 00:00:00 2001
From: turekt <32360115+turekt@users.noreply.github.com>
Date: Thu, 18 May 2023 18:05:49 +0200
Subject: [PATCH] Implement set KeyByteOrder (#226)

Fixes https://github.com/google/nftables/issues/225
Introduced KeyByteOrder in sets which fills UDATA with endianess information
---
 set.go | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/set.go b/set.go
index 1ef8e89..b1f63e8 100644
--- a/set.go
+++ b/set.go
@@ -261,6 +261,9 @@ type Set struct {
 	Timeout       time.Duration
 	KeyType       SetDatatype
 	DataType      SetDatatype
+	// Either host (binaryutil.NativeEndian) or big (binaryutil.BigEndian) endian as per
+	// https://git.netfilter.org/nftables/tree/include/datatype.h?id=d486c9e626405e829221b82d7355558005b26d8a#n109
+	KeyByteOrder binaryutil.ByteOrder
 }
 
 // SetElement represents a data point within a set.
@@ -560,11 +563,11 @@ func (cc *Conn) AddSet(s *Set, vals []SetElement) error {
 		// Marshal concat size description as set description
 		tableInfo = append(tableInfo, netlink.Attribute{Type: unix.NLA_F_NESTED | unix.NFTA_SET_DESC, Data: concatBytes})
 	}
-	if s.Anonymous || s.Constant || s.Interval {
+	if s.Anonymous || s.Constant || s.Interval || s.KeyByteOrder == binaryutil.BigEndian {
 		tableInfo = append(tableInfo,
 			// Semantically useless - kept for binary compatability with nft
 			netlink.Attribute{Type: unix.NFTA_SET_USERDATA, Data: []byte("\x00\x04\x02\x00\x00\x00")})
-	} else if !s.IsMap {
+	} else if s.KeyByteOrder == binaryutil.NativeEndian {
 		// Per https://git.netfilter.org/nftables/tree/src/mnl.c?id=187c6d01d35722618c2711bbc49262c286472c8f#n1165
 		tableInfo = append(tableInfo,
 			netlink.Attribute{Type: unix.NFTA_SET_USERDATA, Data: []byte("\x00\x04\x01\x00\x00\x00")})
-- 
2.39.2

