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 <linux/u64_stats_sync.h>
10 #include <net/netlink.h>
12 #define NFT_JUMP_STACK_SIZE 16
16 const struct net_device *in;
17 const struct net_device *out;
18 const struct nf_hook_ops *ops;
22 /* for x_tables compatibility */
23 struct xt_action_param xt;
26 static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
27 const struct nf_hook_ops *ops,
29 const struct net_device *in,
30 const struct net_device *out)
33 pkt->in = pkt->xt.in = in;
34 pkt->out = pkt->xt.out = out;
36 pkt->xt.hooknum = ops->hooknum;
37 pkt->xt.family = ops->pf;
45 struct nft_chain *chain;
48 } __attribute__((aligned(__alignof__(u64))));
50 static inline int nft_data_cmp(const struct nft_data *d1,
51 const struct nft_data *d2,
54 return memcmp(d1->data, d2->data, len);
57 static inline void nft_data_copy(struct nft_data *dst,
58 const struct nft_data *src)
60 BUILD_BUG_ON(__alignof__(*dst) != __alignof__(u64));
61 *(u64 *)&dst->data[0] = *(u64 *)&src->data[0];
62 *(u64 *)&dst->data[2] = *(u64 *)&src->data[2];
65 static inline void nft_data_debug(const struct nft_data *data)
67 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
68 data->data[0], data->data[1],
69 data->data[2], data->data[3]);
73 * struct nft_ctx - nf_tables rule/set context
76 * @afi: address family info
77 * @table: the table the chain is contained in
78 * @chain: the chain the rule is contained in
79 * @nla: netlink attributes
80 * @portid: netlink portID of the original message
81 * @seq: netlink sequence number
82 * @report: notify via unicast netlink message
86 struct nft_af_info *afi;
87 struct nft_table *table;
88 struct nft_chain *chain;
89 const struct nlattr * const *nla;
95 struct nft_data_desc {
96 enum nft_data_types type;
100 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
101 struct nft_data_desc *desc, const struct nlattr *nla);
102 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type);
103 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
104 enum nft_data_types type, unsigned int len);
106 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
108 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
111 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
113 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1;
116 int nft_validate_input_register(enum nft_registers reg);
117 int nft_validate_output_register(enum nft_registers reg);
118 int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
119 const struct nft_data *data,
120 enum nft_data_types type);
124 * struct nft_userdata - user defined data associated with an object
126 * @len: length of the data
129 * The presence of user data is indicated in an object specific fashion,
130 * so a length of zero can't occur and the value "len" indicates data
133 struct nft_userdata {
135 unsigned char data[0];
139 * struct nft_set_elem - generic representation of set elements
141 * @cookie: implementation specific element cookie
143 * @data: element data (maps only)
144 * @flags: element flags (end of interval)
146 * The cookie can be used to store a handle to the element for subsequent
149 struct nft_set_elem {
152 struct nft_data data;
157 struct nft_set_iter {
161 int (*fn)(const struct nft_ctx *ctx,
162 const struct nft_set *set,
163 const struct nft_set_iter *iter,
164 const struct nft_set_elem *elem);
168 * struct nft_set_desc - description of set elements
172 * @size: number of set elements
174 struct nft_set_desc {
181 * enum nft_set_class - performance class
183 * @NFT_LOOKUP_O_1: constant, O(1)
184 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
185 * @NFT_LOOKUP_O_N: linear, O(N)
189 NFT_SET_CLASS_O_LOG_N,
194 * struct nft_set_estimate - estimation of memory and performance
197 * @size: required memory
198 * @class: lookup performance class
200 struct nft_set_estimate {
202 enum nft_set_class class;
206 * struct nft_set_ops - nf_tables set operations
208 * @lookup: look up an element within the set
209 * @insert: insert new element into set
210 * @remove: remove element from set
211 * @walk: iterate over all set elemeennts
212 * @privsize: function to return size of set private data
213 * @init: initialize private data of new set instance
214 * @destroy: destroy private data of set instance
215 * @list: nf_tables_set_ops list node
216 * @owner: module reference
217 * @features: features supported by the implementation
220 bool (*lookup)(const struct nft_set *set,
221 const struct nft_data *key,
222 struct nft_data *data);
223 int (*get)(const struct nft_set *set,
224 struct nft_set_elem *elem);
225 int (*insert)(const struct nft_set *set,
226 const struct nft_set_elem *elem);
227 void (*remove)(const struct nft_set *set,
228 const struct nft_set_elem *elem);
229 void (*walk)(const struct nft_ctx *ctx,
230 const struct nft_set *set,
231 struct nft_set_iter *iter);
233 unsigned int (*privsize)(const struct nlattr * const nla[]);
234 bool (*estimate)(const struct nft_set_desc *desc,
236 struct nft_set_estimate *est);
237 int (*init)(const struct nft_set *set,
238 const struct nft_set_desc *desc,
239 const struct nlattr * const nla[]);
240 void (*destroy)(const struct nft_set *set);
242 struct list_head list;
243 struct module *owner;
247 int nft_register_set(struct nft_set_ops *ops);
248 void nft_unregister_set(struct nft_set_ops *ops);
251 * struct nft_set - nf_tables set instance
253 * @list: table set list node
254 * @bindings: list of set bindings
255 * @name: name of the set
256 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
257 * @dtype: data type (verdict or numeric type defined by userspace)
258 * @size: maximum set size
259 * @nelems: number of elements
260 * @policy: set parameterization (see enum nft_set_policies)
265 * @data: private set data
268 struct list_head list;
269 struct list_head bindings;
276 /* runtime data below here */
277 const struct nft_set_ops *ops ____cacheline_aligned;
282 __attribute__((aligned(__alignof__(u64))));
285 static inline void *nft_set_priv(const struct nft_set *set)
287 return (void *)set->data;
290 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
291 const struct nlattr *nla);
292 struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
293 const struct nlattr *nla);
296 * struct nft_set_binding - nf_tables set binding
298 * @list: set bindings list node
299 * @chain: chain containing the rule bound to the set
301 * A set binding contains all information necessary for validation
302 * of new elements added to a bound set.
304 struct nft_set_binding {
305 struct list_head list;
306 const struct nft_chain *chain;
309 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
310 struct nft_set_binding *binding);
311 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
312 struct nft_set_binding *binding);
316 * struct nft_expr_type - nf_tables expression type
318 * @select_ops: function to select nft_expr_ops
319 * @ops: default ops, used when no select_ops functions is present
320 * @list: used internally
322 * @owner: module reference
323 * @policy: netlink attribute policy
324 * @maxattr: highest netlink attribute number
325 * @family: address family for AF-specific types
327 struct nft_expr_type {
328 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
329 const struct nlattr * const tb[]);
330 const struct nft_expr_ops *ops;
331 struct list_head list;
333 struct module *owner;
334 const struct nla_policy *policy;
335 unsigned int maxattr;
340 * struct nft_expr_ops - nf_tables expression operations
342 * @eval: Expression evaluation function
343 * @size: full expression size, including private data size
344 * @init: initialization function
345 * @destroy: destruction function
346 * @dump: function to dump parameters
347 * @type: expression type
348 * @validate: validate expression, called during loop detection
349 * @data: extra data to attach to this expression operation
352 struct nft_expr_ops {
353 void (*eval)(const struct nft_expr *expr,
354 struct nft_data data[NFT_REG_MAX + 1],
355 const struct nft_pktinfo *pkt);
358 int (*init)(const struct nft_ctx *ctx,
359 const struct nft_expr *expr,
360 const struct nlattr * const tb[]);
361 void (*destroy)(const struct nft_ctx *ctx,
362 const struct nft_expr *expr);
363 int (*dump)(struct sk_buff *skb,
364 const struct nft_expr *expr);
365 int (*validate)(const struct nft_ctx *ctx,
366 const struct nft_expr *expr,
367 const struct nft_data **data);
368 const struct nft_expr_type *type;
372 #define NFT_EXPR_MAXATTR 16
373 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
374 ALIGN(size, __alignof__(struct nft_expr)))
377 * struct nft_expr - nf_tables expression
379 * @ops: expression ops
380 * @data: expression private data
383 const struct nft_expr_ops *ops;
384 unsigned char data[];
387 static inline void *nft_expr_priv(const struct nft_expr *expr)
389 return (void *)expr->data;
393 * struct nft_rule - nf_tables rule
395 * @list: used internally
396 * @handle: rule handle
397 * @genmask: generation mask
398 * @dlen: length of expression data
399 * @udata: user data is appended to the rule
400 * @data: expression data
403 struct list_head list;
409 __attribute__((aligned(__alignof__(struct nft_expr))));
412 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
414 return (struct nft_expr *)&rule->data[0];
417 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
419 return ((void *)expr) + expr->ops->size;
422 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
424 return (struct nft_expr *)&rule->data[rule->dlen];
427 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
429 return (void *)&rule->data[rule->dlen];
433 * The last pointer isn't really necessary, but the compiler isn't able to
434 * determine that the result of nft_expr_last() is always the same since it
435 * can't assume that the dlen value wasn't changed within calls in the loop.
437 #define nft_rule_for_each_expr(expr, last, rule) \
438 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
440 (expr) = nft_expr_next(expr))
442 enum nft_chain_flags {
443 NFT_BASE_CHAIN = 0x1,
444 NFT_CHAIN_INACTIVE = 0x2,
448 * struct nft_chain - nf_tables chain
450 * @rules: list of rules in the chain
451 * @list: used internally
452 * @net: net namespace that this chain belongs to
453 * @table: table that this chain belongs to
454 * @handle: chain handle
455 * @use: number of jump references to this chain
456 * @level: length of longest path to this chain
457 * @flags: bitmask of enum nft_chain_flags
458 * @name: name of the chain
461 struct list_head rules;
462 struct list_head list;
464 struct nft_table *table;
469 char name[NFT_CHAIN_MAXNAMELEN];
472 enum nft_chain_type {
473 NFT_CHAIN_T_DEFAULT = 0,
480 * struct nf_chain_type - nf_tables chain type info
482 * @name: name of the type
483 * @type: numeric identifier
484 * @family: address family
485 * @owner: module owner
486 * @hook_mask: mask of valid hooks
487 * @hooks: hookfn overrides
489 struct nf_chain_type {
491 enum nft_chain_type type;
493 struct module *owner;
494 unsigned int hook_mask;
495 nf_hookfn *hooks[NF_MAX_HOOKS];
498 int nft_chain_validate_dependency(const struct nft_chain *chain,
499 enum nft_chain_type type);
500 int nft_chain_validate_hooks(const struct nft_chain *chain,
501 unsigned int hook_flags);
506 struct u64_stats_sync syncp;
509 #define NFT_HOOK_OPS_MAX 2
512 * struct nft_base_chain - nf_tables base chain
514 * @ops: netfilter hook ops
516 * @policy: default policy
517 * @stats: per-cpu chain stats
520 struct nft_base_chain {
521 struct nf_hook_ops ops[NFT_HOOK_OPS_MAX];
522 const struct nf_chain_type *type;
524 struct nft_stats __percpu *stats;
525 struct nft_chain chain;
528 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
530 return container_of(chain, struct nft_base_chain, chain);
533 unsigned int nft_do_chain(struct nft_pktinfo *pkt,
534 const struct nf_hook_ops *ops);
537 * struct nft_table - nf_tables table
539 * @list: used internally
540 * @chains: chains in the table
541 * @sets: sets in the table
542 * @hgenerator: handle generator state
543 * @use: number of chain references to this table
544 * @flags: table flag (see enum nft_table_flags)
545 * @name: name of the table
548 struct list_head list;
549 struct list_head chains;
550 struct list_head sets;
554 char name[NFT_TABLE_MAXNAMELEN];
558 * struct nft_af_info - nf_tables address family info
560 * @list: used internally
561 * @family: address family
562 * @nhooks: number of hooks in this family
563 * @owner: module owner
564 * @tables: used internally
565 * @nops: number of hook ops in this family
566 * @hook_ops_init: initialization function for chain hook ops
567 * @hooks: hookfn overrides for packet validation
570 struct list_head list;
573 struct module *owner;
574 struct list_head tables;
576 void (*hook_ops_init)(struct nf_hook_ops *,
578 nf_hookfn *hooks[NF_MAX_HOOKS];
581 int nft_register_afinfo(struct net *, struct nft_af_info *);
582 void nft_unregister_afinfo(struct nft_af_info *);
584 int nft_register_chain_type(const struct nf_chain_type *);
585 void nft_unregister_chain_type(const struct nf_chain_type *);
587 int nft_register_expr(struct nft_expr_type *);
588 void nft_unregister_expr(struct nft_expr_type *);
590 #define nft_dereference(p) \
591 nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
593 #define MODULE_ALIAS_NFT_FAMILY(family) \
594 MODULE_ALIAS("nft-afinfo-" __stringify(family))
596 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
597 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
599 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
600 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
602 #define MODULE_ALIAS_NFT_EXPR(name) \
603 MODULE_ALIAS("nft-expr-" name)
605 #define MODULE_ALIAS_NFT_SET() \
606 MODULE_ALIAS("nft-set")
609 * struct nft_trans - nf_tables object update in transaction
611 * @list: used internally
612 * @msg_type: message type
613 * @ctx: transaction context
614 * @data: internal information related to the transaction
617 struct list_head list;
623 struct nft_trans_rule {
624 struct nft_rule *rule;
627 #define nft_trans_rule(trans) \
628 (((struct nft_trans_rule *)trans->data)->rule)
630 struct nft_trans_set {
635 #define nft_trans_set(trans) \
636 (((struct nft_trans_set *)trans->data)->set)
637 #define nft_trans_set_id(trans) \
638 (((struct nft_trans_set *)trans->data)->set_id)
640 struct nft_trans_chain {
642 char name[NFT_CHAIN_MAXNAMELEN];
643 struct nft_stats __percpu *stats;
647 #define nft_trans_chain_update(trans) \
648 (((struct nft_trans_chain *)trans->data)->update)
649 #define nft_trans_chain_name(trans) \
650 (((struct nft_trans_chain *)trans->data)->name)
651 #define nft_trans_chain_stats(trans) \
652 (((struct nft_trans_chain *)trans->data)->stats)
653 #define nft_trans_chain_policy(trans) \
654 (((struct nft_trans_chain *)trans->data)->policy)
656 struct nft_trans_table {
661 #define nft_trans_table_update(trans) \
662 (((struct nft_trans_table *)trans->data)->update)
663 #define nft_trans_table_enable(trans) \
664 (((struct nft_trans_table *)trans->data)->enable)
666 struct nft_trans_elem {
668 struct nft_set_elem elem;
671 #define nft_trans_elem_set(trans) \
672 (((struct nft_trans_elem *)trans->data)->set)
673 #define nft_trans_elem(trans) \
674 (((struct nft_trans_elem *)trans->data)->elem)
676 #endif /* _NET_NF_TABLES_H */