Skip to content

Commit 305c56d

Browse files
kerumetogvisor-bot
authored andcommitted
Support batch messages for NETFILTER sockets.
Implements the functionality required for NETFILTER sockets to process batch messages for nftables. Batch messaging is required for most NETFILTER requests. Updated tests to put the required requests in batch message format. PiperOrigin-RevId: 795161788
1 parent d3ae922 commit 305c56d

File tree

12 files changed

+1870
-1260
lines changed

12 files changed

+1870
-1260
lines changed

pkg/abi/linux/netlink_netfilter.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,9 @@ const SizeOfNetfilterGenMsg = 4
4949
// NFNETLINK_V0 is the default version of the netlink netfilter.
5050
const NFNETLINK_V0 = 0
5151

52-
// SubsysID describes Netlink Netfilter subsystem IDs, from uapi/linux/netfilter/nfnetlink.h.
53-
type SubsysID uint16
54-
55-
// Netlink Netfilter subsystem IDs.
52+
// Netlink Netfilter subsystem IDs, from uapi/linux/netfilter/nfnetlink.h.
5653
const (
57-
NFNL_SUBSYS_NONE SubsysID = iota
54+
NFNL_SUBSYS_NONE = iota
5855
NFNL_SUBSYS_CTNETLINK
5956
NFNL_SUBSYS_CTNETLINK_EXP
6057
NFNL_SUBSYS_QUEUE
@@ -67,11 +64,12 @@ const (
6764
NFNL_SUBSYS_NFTABLES
6865
NFNL_SUBSYS_NFT_COMPAT
6966
NFNL_SUBSYS_HOOK
67+
NFNL_SUBSYS_COUNT
7068
)
7169

7270
// NetFilterSubsysID returns the Netfilter Subsystem ID from the netlink message header.
73-
func (hdr *NetlinkMessageHeader) NetFilterSubsysID() SubsysID {
74-
return SubsysID((hdr.Type & 0xff00) >> 8)
71+
func (hdr *NetlinkMessageHeader) NetFilterSubsysID() uint16 {
72+
return (hdr.Type & 0xff00) >> 8
7573
}
7674

7775
// NetFilterMsgType returns the Netfilter Message Type from the netlink message header.
@@ -85,12 +83,9 @@ const (
8583
NFNL_MSG_BATCH_END = NLMSG_MIN_TYPE + 1
8684
)
8785

88-
// NetlinkBatchAttr describes Netlink Netfilter batch attributes, from uapi/linux/netfilter/nfnetlink.h.
89-
type NetlinkBatchAttr uint16
90-
9186
// Netlink Netfilter batch attributes.
9287
const (
93-
NFNL_BATCH_UNSPEC NetlinkBatchAttr = iota
88+
NFNL_BATCH_UNSPEC = iota
9489
NFNL_BATCH_GENID
9590
__NFNL_BATCH_MAX
9691
NFNL_BATCH_MAX = __NFNL_BATCH_MAX - 1

pkg/abi/linux/nf_tables.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,52 @@ const (
138138
NFT_MSG_MAX
139139
)
140140

141+
var nfTableMsgTypeStrings = [...]string{
142+
NFT_MSG_NEWTABLE: "NFT_MSG_NEWTABLE",
143+
NFT_MSG_GETTABLE: "NFT_MSG_GETTABLE",
144+
NFT_MSG_DELTABLE: "NFT_MSG_DELTABLE",
145+
NFT_MSG_NEWCHAIN: "NFT_MSG_NEWCHAIN",
146+
NFT_MSG_GETCHAIN: "NFT_MSG_GETCHAIN",
147+
NFT_MSG_DELCHAIN: "NFT_MSG_DELCHAIN",
148+
NFT_MSG_NEWRULE: "NFT_MSG_NEWRULE",
149+
NFT_MSG_GETRULE: "NFT_MSG_GETRULE",
150+
NFT_MSG_DELRULE: "NFT_MSG_DELRULE",
151+
NFT_MSG_NEWSET: "NFT_MSG_NEWSET",
152+
NFT_MSG_GETSET: "NFT_MSG_GETSET",
153+
NFT_MSG_DELSET: "NFT_MSG_DELSET",
154+
NFT_MSG_NEWSETELEM: "NFT_MSG_NEWSETELEM",
155+
NFT_MSG_GETSETELEM: "NFT_MSG_GETSETELEM",
156+
NFT_MSG_DELSETELEM: "NFT_MSG_DELSETELEM",
157+
NFT_MSG_NEWGEN: "NFT_MSG_NEWGEN",
158+
NFT_MSG_GETGEN: "NFT_MSG_GETGEN",
159+
NFT_MSG_TRACE: "NFT_MSG_TRACE",
160+
NFT_MSG_NEWOBJ: "NFT_MSG_NEWOBJ",
161+
NFT_MSG_GETOBJ: "NFT_MSG_GETOBJ",
162+
NFT_MSG_DELOBJ: "NFT_MSG_DELOBJ",
163+
NFT_MSG_GETOBJ_RESET: "NFT_MSG_GETOBJ_RESET",
164+
NFT_MSG_NEWFLOWTABLE: "NFT_MSG_NEWFLOWTABLE",
165+
NFT_MSG_GETFLOWTABLE: "NFT_MSG_GETFLOWTABLE",
166+
NFT_MSG_DELFLOWTABLE: "NFT_MSG_DELFLOWTABLE",
167+
NFT_MSG_GETRULE_RESET: "NFT_MSG_GETRULE_RESET",
168+
NFT_MSG_DESTROYTABLE: "NFT_MSG_DESTROYTABLE",
169+
NFT_MSG_DESTROYCHAIN: "NFT_MSG_DESTROYCHAIN",
170+
NFT_MSG_DESTROYRULE: "NFT_MSG_DESTROYRULE",
171+
NFT_MSG_DESTROYSET: "NFT_MSG_DESTROYSET",
172+
NFT_MSG_DESTROYSETELEM: "NFT_MSG_DESTROYSETELEM",
173+
NFT_MSG_DESTROYOBJ: "NFT_MSG_DESTROYOBJ",
174+
NFT_MSG_DESTROYFLOWTABLE: "NFT_MSG_DESTROYFLOWTABLE",
175+
NFT_MSG_GETSETELEM_RESET: "NFT_MSG_GETSETELEM_RESET",
176+
NFT_MSG_MAX: "NFT_MSG_MAX",
177+
}
178+
179+
// String returns the string representation of the NfTableMsgType.
180+
func (msg NfTableMsgType) String() string {
181+
if int(msg) < len(nfTableMsgTypeStrings) {
182+
return nfTableMsgTypeStrings[msg]
183+
}
184+
return "UNKNOWN"
185+
}
186+
141187
// NfTableListAttributes represents the netfilter attributes for lists of data.
142188
// These correspond to values in include/uapi/linux/netfilter/nf_tables.h.
143189
const (

pkg/sentry/socket/netlink/netfilter/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ go_library(
1212
deps = [
1313
"//pkg/abi/linux",
1414
"//pkg/atomicbitops",
15+
"//pkg/bits",
1516
"//pkg/context",
1617
"//pkg/log",
1718
"//pkg/marshal/primitive",

0 commit comments

Comments
 (0)