Skip to content

Commit cecb151

Browse files
kerumetogvisor-bot
authored andcommitted
Nftables NEW_RULE implementation.
Implements functionality to allow users to add rules to chains, limited to basic immediate expressions. Various validation details were included to ensure that specified data details were valid. Future work will include allowing JUMP and GOTO verdicts, as well as supporting other operation types. Updates #11778 PiperOrigin-RevId: 789825219
1 parent 6cadfa6 commit cecb151

File tree

11 files changed

+1801
-280
lines changed

11 files changed

+1801
-280
lines changed

pkg/abi/linux/netlink.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ type NetlinkAttrHeader struct {
122122
Type uint16
123123
}
124124

125+
// Netlink attribute flags, from uapi/linux/netlink.h.
126+
const (
127+
NLA_F_NESTED uint16 = 1 << 15
128+
NLA_F_NET_BYTEORDER = 1 << 14
129+
NLA_TYPE_MASK = ^(NLA_F_NESTED | NLA_F_NET_BYTEORDER)
130+
)
131+
125132
// NetlinkAttrHeaderSize is the size of NetlinkAttrHeader.
126133
const NetlinkAttrHeaderSize = 4
127134

pkg/abi/linux/nf_tables.go

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

141+
// NfTableListAttributes represents the netfilter attributes for lists of data.
142+
// These correspond to values in include/uapi/linux/netfilter/nf_tables.h.
143+
const (
144+
NFTA_LIST_UNSPEC uint16 = iota
145+
NFTA_LIST_ELEM
146+
__NFTA_LIST_MAX
147+
NFTA_LIST_MAX = __NFTA_LIST_MAX - 1
148+
)
149+
141150
// NfTableHookAttributes represents the netfilter hook attributes.
142151
// These correspond to values in include/uapi/linux/netfilter/nf_tables.h.
143152
const (
@@ -205,6 +214,82 @@ const (
205214
NFTA_CHAIN_MAX = __NFTA_CHAIN_MAX - 1
206215
)
207216

217+
// NfTableRuleAttributes represents the netfilter rule attributes.
218+
// These correspond to values in include/uapi/linux/netfilter/nf_tables.h.
219+
const (
220+
NFTA_RULE_UNSPEC uint16 = iota
221+
NFTA_RULE_TABLE
222+
NFTA_RULE_CHAIN
223+
NFTA_RULE_HANDLE
224+
NFTA_RULE_EXPRESSIONS
225+
NFTA_RULE_COMPAT
226+
NFTA_RULE_POSITION
227+
NFTA_RULE_USERDATA
228+
NFTA_RULE_PAD
229+
NFTA_RULE_ID
230+
NFTA_RULE_POSITION_ID
231+
NFTA_RULE_CHAIN_ID
232+
__NFTA_RULE_MAX
233+
NFTA_RULE_MAX = __NFTA_RULE_MAX - 1
234+
)
235+
236+
// NfTableDataTypes represents the netfilter data types.
237+
// These correspond to values in include/uapi/linux/netfilter/nf_tables.h.
238+
const (
239+
NFT_DATA_VALUE = iota
240+
NFT_DATA_VERDICT = 0xffffff00
241+
)
242+
243+
// NfTableDataReservedMask represents the netfilter data reserved mask for internally used types.
244+
// These correspond to values in include/uapi/linux/netfilter/nf_tables.h.
245+
const (
246+
NFT_DATA_RESERVED_MASK = 0xffffff00
247+
)
248+
249+
// NfTableDataAttributes represents the netfilter data attributes.
250+
// These correspond to values in include/uapi/linux/netfilter/nf_tables.h.
251+
const (
252+
NFTA_DATA_UNSPEC uint16 = iota
253+
NFTA_DATA_VALUE
254+
NFTA_DATA_VERDICT
255+
__NFTA_DATA_MAX
256+
NFTA_DATA_MAX = __NFTA_DATA_MAX - 1
257+
)
258+
259+
// NFT_DATA_VALUE_MAXLEN is the maximum length of a netfilter data value.
260+
const NFT_DATA_VALUE_MAXLEN = 64
261+
262+
// NfTableVerdictAttributes represents the netfilter verdict attributes.
263+
// These correspond to values in include/uapi/linux/netfilter/nf_tables.h.
264+
const (
265+
NFTA_VERDICT_UNSPEC uint16 = iota
266+
NFTA_VERDICT_CODE
267+
NFTA_VERDICT_CHAIN
268+
NFTA_VERDICT_CHAIN_ID
269+
__NFTA_VERDICT_MAX
270+
NFTA_VERDICT_MAX = __NFTA_VERDICT_MAX - 1
271+
)
272+
273+
// NfTableExprAttributes represents the netfilter expression attributes.
274+
// These correspond to values in include/uapi/linux/netfilter/nf_tables.h.
275+
const (
276+
NFTA_EXPR_UNSPEC uint16 = iota
277+
NFTA_EXPR_NAME
278+
NFTA_EXPR_DATA
279+
__NFTA_EXPR_MAX
280+
NFTA_EXPR_MAX = __NFTA_EXPR_MAX - 1
281+
)
282+
283+
// NfTableImmediateAttributes represents the netfilter immediate attributes.
284+
// These correspond to values in include/uapi/linux/netfilter/nf_tables.h.
285+
const (
286+
NFTA_IMMEDIATE_UNSPEC uint16 = iota
287+
NFTA_IMMEDIATE_DREG
288+
NFTA_IMMEDIATE_DATA
289+
__NFTA_IMMEDIATE_MAX
290+
NFTA_IMMEDIATE_MAX = __NFTA_IMMEDIATE_MAX - 1
291+
)
292+
208293
// Nf table relational operators.
209294
// Used by the nft comparison operation to compare values in registers.
210295
// These correspond to enum values in include/uapi/linux/netfilter/nf_tables.h.

0 commit comments

Comments
 (0)