1 #ifndef _NET_NF_TABLES_H
2 #define _NET_NF_TABLES_H
4 #include <linux/list.h>
5 #include <linux/netfilter.h>
6 #include <linux/netfilter/nfnetlink.h>
7 #include <linux/netfilter/x_tables.h>
8 #include <linux/netfilter/nf_tables.h>
9 #include <net/netlink.h>
11 #define NFT_JUMP_STACK_SIZE 16
15 const struct net_device
*in
;
16 const struct net_device
*out
;
17 const struct nf_hook_ops
*ops
;
21 /* for x_tables compatibility */
22 struct xt_action_param xt
;
25 static inline void nft_set_pktinfo(struct nft_pktinfo
*pkt
,
26 const struct nf_hook_ops
*ops
,
28 const struct net_device
*in
,
29 const struct net_device
*out
)
32 pkt
->in
= pkt
->xt
.in
= in
;
33 pkt
->out
= pkt
->xt
.out
= out
;
35 pkt
->xt
.hooknum
= ops
->hooknum
;
36 pkt
->xt
.family
= ops
->pf
;
44 struct nft_chain
*chain
;
47 } __attribute__((aligned(__alignof__(u64
))));
49 static inline int nft_data_cmp(const struct nft_data
*d1
,
50 const struct nft_data
*d2
,
53 return memcmp(d1
->data
, d2
->data
, len
);
56 static inline void nft_data_copy(struct nft_data
*dst
,
57 const struct nft_data
*src
)
59 BUILD_BUG_ON(__alignof__(*dst
) != __alignof__(u64
));
60 *(u64
*)&dst
->data
[0] = *(u64
*)&src
->data
[0];
61 *(u64
*)&dst
->data
[2] = *(u64
*)&src
->data
[2];
64 static inline void nft_data_debug(const struct nft_data
*data
)
66 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
67 data
->data
[0], data
->data
[1],
68 data
->data
[2], data
->data
[3]);
72 * struct nft_ctx - nf_tables rule/set context
76 * @nlh: netlink message header
77 * @afi: address family info
78 * @table: the table the chain is contained in
79 * @chain: the chain the rule is contained in
80 * @nla: netlink attributes
84 const struct sk_buff
*skb
;
85 const struct nlmsghdr
*nlh
;
86 const struct nft_af_info
*afi
;
87 const struct nft_table
*table
;
88 const struct nft_chain
*chain
;
89 const struct nlattr
* const *nla
;
92 struct nft_data_desc
{
93 enum nft_data_types type
;
97 int nft_data_init(const struct nft_ctx
*ctx
, struct nft_data
*data
,
98 struct nft_data_desc
*desc
, const struct nlattr
*nla
);
99 void nft_data_uninit(const struct nft_data
*data
, enum nft_data_types type
);
100 int nft_data_dump(struct sk_buff
*skb
, int attr
, const struct nft_data
*data
,
101 enum nft_data_types type
, unsigned int len
);
103 static inline enum nft_data_types
nft_dreg_to_type(enum nft_registers reg
)
105 return reg
== NFT_REG_VERDICT
? NFT_DATA_VERDICT
: NFT_DATA_VALUE
;
108 static inline enum nft_registers
nft_type_to_reg(enum nft_data_types type
)
110 return type
== NFT_DATA_VERDICT
? NFT_REG_VERDICT
: NFT_REG_1
;
113 int nft_validate_input_register(enum nft_registers reg
);
114 int nft_validate_output_register(enum nft_registers reg
);
115 int nft_validate_data_load(const struct nft_ctx
*ctx
, enum nft_registers reg
,
116 const struct nft_data
*data
,
117 enum nft_data_types type
);
120 * struct nft_set_elem - generic representation of set elements
122 * @cookie: implementation specific element cookie
124 * @data: element data (maps only)
125 * @flags: element flags (end of interval)
127 * The cookie can be used to store a handle to the element for subsequent
130 struct nft_set_elem
{
133 struct nft_data data
;
138 struct nft_set_iter
{
142 int (*fn
)(const struct nft_ctx
*ctx
,
143 const struct nft_set
*set
,
144 const struct nft_set_iter
*iter
,
145 const struct nft_set_elem
*elem
);
149 * struct nft_set_ops - nf_tables set operations
151 * @lookup: look up an element within the set
152 * @insert: insert new element into set
153 * @remove: remove element from set
154 * @walk: iterate over all set elemeennts
155 * @privsize: function to return size of set private data
156 * @init: initialize private data of new set instance
157 * @destroy: destroy private data of set instance
158 * @list: nf_tables_set_ops list node
159 * @owner: module reference
160 * @features: features supported by the implementation
163 bool (*lookup
)(const struct nft_set
*set
,
164 const struct nft_data
*key
,
165 struct nft_data
*data
);
166 int (*get
)(const struct nft_set
*set
,
167 struct nft_set_elem
*elem
);
168 int (*insert
)(const struct nft_set
*set
,
169 const struct nft_set_elem
*elem
);
170 void (*remove
)(const struct nft_set
*set
,
171 const struct nft_set_elem
*elem
);
172 void (*walk
)(const struct nft_ctx
*ctx
,
173 const struct nft_set
*set
,
174 struct nft_set_iter
*iter
);
176 unsigned int (*privsize
)(const struct nlattr
* const nla
[]);
177 int (*init
)(const struct nft_set
*set
,
178 const struct nlattr
* const nla
[]);
179 void (*destroy
)(const struct nft_set
*set
);
181 struct list_head list
;
182 struct module
*owner
;
186 int nft_register_set(struct nft_set_ops
*ops
);
187 void nft_unregister_set(struct nft_set_ops
*ops
);
190 * struct nft_set - nf_tables set instance
192 * @list: table set list node
193 * @bindings: list of set bindings
194 * @name: name of the set
195 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
196 * @dtype: data type (verdict or numeric type defined by userspace)
201 * @data: private set data
204 struct list_head list
;
205 struct list_head bindings
;
209 /* runtime data below here */
210 const struct nft_set_ops
*ops ____cacheline_aligned
;
215 __attribute__((aligned(__alignof__(u64
))));
218 static inline void *nft_set_priv(const struct nft_set
*set
)
220 return (void *)set
->data
;
223 struct nft_set
*nf_tables_set_lookup(const struct nft_table
*table
,
224 const struct nlattr
*nla
);
227 * struct nft_set_binding - nf_tables set binding
229 * @list: set bindings list node
230 * @chain: chain containing the rule bound to the set
232 * A set binding contains all information necessary for validation
233 * of new elements added to a bound set.
235 struct nft_set_binding
{
236 struct list_head list
;
237 const struct nft_chain
*chain
;
240 int nf_tables_bind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
241 struct nft_set_binding
*binding
);
242 void nf_tables_unbind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
243 struct nft_set_binding
*binding
);
247 * struct nft_expr_type - nf_tables expression type
249 * @select_ops: function to select nft_expr_ops
250 * @ops: default ops, used when no select_ops functions is present
251 * @list: used internally
253 * @owner: module reference
254 * @policy: netlink attribute policy
255 * @maxattr: highest netlink attribute number
256 * @family: address family for AF-specific types
258 struct nft_expr_type
{
259 const struct nft_expr_ops
*(*select_ops
)(const struct nft_ctx
*,
260 const struct nlattr
* const tb
[]);
261 const struct nft_expr_ops
*ops
;
262 struct list_head list
;
264 struct module
*owner
;
265 const struct nla_policy
*policy
;
266 unsigned int maxattr
;
271 * struct nft_expr_ops - nf_tables expression operations
273 * @eval: Expression evaluation function
274 * @size: full expression size, including private data size
275 * @init: initialization function
276 * @destroy: destruction function
277 * @dump: function to dump parameters
278 * @type: expression type
279 * @validate: validate expression, called during loop detection
280 * @data: extra data to attach to this expression operation
283 struct nft_expr_ops
{
284 void (*eval
)(const struct nft_expr
*expr
,
285 struct nft_data data
[NFT_REG_MAX
+ 1],
286 const struct nft_pktinfo
*pkt
);
289 int (*init
)(const struct nft_ctx
*ctx
,
290 const struct nft_expr
*expr
,
291 const struct nlattr
* const tb
[]);
292 void (*destroy
)(const struct nft_ctx
*ctx
,
293 const struct nft_expr
*expr
);
294 int (*dump
)(struct sk_buff
*skb
,
295 const struct nft_expr
*expr
);
296 int (*validate
)(const struct nft_ctx
*ctx
,
297 const struct nft_expr
*expr
,
298 const struct nft_data
**data
);
299 const struct nft_expr_type
*type
;
303 #define NFT_EXPR_MAXATTR 16
304 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
305 ALIGN(size, __alignof__(struct nft_expr)))
308 * struct nft_expr - nf_tables expression
310 * @ops: expression ops
311 * @data: expression private data
314 const struct nft_expr_ops
*ops
;
315 unsigned char data
[];
318 static inline void *nft_expr_priv(const struct nft_expr
*expr
)
320 return (void *)expr
->data
;
324 * struct nft_rule - nf_tables rule
326 * @list: used internally
327 * @handle: rule handle
328 * @genmask: generation mask
329 * @dlen: length of expression data
330 * @ulen: length of user data (used for comments)
331 * @data: expression data
334 struct list_head list
;
340 __attribute__((aligned(__alignof__(struct nft_expr
))));
344 * struct nft_rule_trans - nf_tables rule update in transaction
346 * @list: used internally
348 * @rule: rule that needs to be updated
350 struct nft_rule_trans
{
351 struct list_head list
;
353 struct nft_rule
*rule
;
356 static inline struct nft_expr
*nft_expr_first(const struct nft_rule
*rule
)
358 return (struct nft_expr
*)&rule
->data
[0];
361 static inline struct nft_expr
*nft_expr_next(const struct nft_expr
*expr
)
363 return ((void *)expr
) + expr
->ops
->size
;
366 static inline struct nft_expr
*nft_expr_last(const struct nft_rule
*rule
)
368 return (struct nft_expr
*)&rule
->data
[rule
->dlen
];
371 static inline void *nft_userdata(const struct nft_rule
*rule
)
373 return (void *)&rule
->data
[rule
->dlen
];
377 * The last pointer isn't really necessary, but the compiler isn't able to
378 * determine that the result of nft_expr_last() is always the same since it
379 * can't assume that the dlen value wasn't changed within calls in the loop.
381 #define nft_rule_for_each_expr(expr, last, rule) \
382 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
384 (expr) = nft_expr_next(expr))
386 enum nft_chain_flags
{
387 NFT_BASE_CHAIN
= 0x1,
391 * struct nft_chain - nf_tables chain
393 * @rules: list of rules in the chain
394 * @list: used internally
395 * @net: net namespace that this chain belongs to
396 * @table: table that this chain belongs to
397 * @handle: chain handle
398 * @flags: bitmask of enum nft_chain_flags
399 * @use: number of jump references to this chain
400 * @level: length of longest path to this chain
401 * @name: name of the chain
404 struct list_head rules
;
405 struct list_head list
;
407 struct nft_table
*table
;
412 char name
[NFT_CHAIN_MAXNAMELEN
];
415 enum nft_chain_type
{
416 NFT_CHAIN_T_DEFAULT
= 0,
427 #define NFT_HOOK_OPS_MAX 2
430 * struct nft_base_chain - nf_tables base chain
432 * @ops: netfilter hook ops
434 * @policy: default policy
435 * @stats: per-cpu chain stats
438 struct nft_base_chain
{
439 struct nf_hook_ops ops
[NFT_HOOK_OPS_MAX
];
440 const struct nf_chain_type
*type
;
442 struct nft_stats __percpu
*stats
;
443 struct nft_chain chain
;
446 static inline struct nft_base_chain
*nft_base_chain(const struct nft_chain
*chain
)
448 return container_of(chain
, struct nft_base_chain
, chain
);
451 unsigned int nft_do_chain(struct nft_pktinfo
*pkt
,
452 const struct nf_hook_ops
*ops
);
455 * struct nft_table - nf_tables table
457 * @list: used internally
458 * @chains: chains in the table
459 * @sets: sets in the table
460 * @hgenerator: handle generator state
461 * @use: number of chain references to this table
462 * @flags: table flag (see enum nft_table_flags)
463 * @name: name of the table
466 struct list_head list
;
467 struct list_head chains
;
468 struct list_head sets
;
476 * struct nft_af_info - nf_tables address family info
478 * @list: used internally
479 * @family: address family
480 * @nhooks: number of hooks in this family
481 * @owner: module owner
482 * @tables: used internally
483 * @nops: number of hook ops in this family
484 * @hook_ops_init: initialization function for chain hook ops
485 * @hooks: hookfn overrides for packet validation
488 struct list_head list
;
491 struct module
*owner
;
492 struct list_head tables
;
494 void (*hook_ops_init
)(struct nf_hook_ops
*,
496 nf_hookfn
*hooks
[NF_MAX_HOOKS
];
499 int nft_register_afinfo(struct net
*, struct nft_af_info
*);
500 void nft_unregister_afinfo(struct nft_af_info
*);
503 * struct nf_chain_type - nf_tables chain type info
505 * @name: name of the type
506 * @type: numeric identifier
507 * @family: address family
508 * @owner: module owner
509 * @hook_mask: mask of valid hooks
510 * @hooks: hookfn overrides
512 struct nf_chain_type
{
514 enum nft_chain_type type
;
516 struct module
*owner
;
517 unsigned int hook_mask
;
518 nf_hookfn
*hooks
[NF_MAX_HOOKS
];
521 int nft_register_chain_type(const struct nf_chain_type
*);
522 void nft_unregister_chain_type(const struct nf_chain_type
*);
524 int nft_register_expr(struct nft_expr_type
*);
525 void nft_unregister_expr(struct nft_expr_type
*);
527 #define nft_dereference(p) \
528 nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
530 #define MODULE_ALIAS_NFT_FAMILY(family) \
531 MODULE_ALIAS("nft-afinfo-" __stringify(family))
533 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
534 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
536 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
537 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
539 #define MODULE_ALIAS_NFT_EXPR(name) \
540 MODULE_ALIAS("nft-expr-" name)
542 #define MODULE_ALIAS_NFT_SET() \
543 MODULE_ALIAS("nft-set")
545 #endif /* _NET_NF_TABLES_H */