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
75 * @afi: address family info
76 * @table: the table the chain is contained in
77 * @chain: the chain the rule is contained in
78 * @nla: netlink attributes
79 * @portid: netlink portID of the original message
80 * @seq: netlink sequence number
81 * @report: notify via unicast netlink message
85 struct nft_af_info *afi;
86 struct nft_table *table;
87 struct nft_chain *chain;
88 const struct nlattr * const *nla;
94 struct nft_data_desc {
95 enum nft_data_types type;
99 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
100 struct nft_data_desc *desc, const struct nlattr *nla);
101 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type);
102 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
103 enum nft_data_types type, unsigned int len);
105 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
107 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
110 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
112 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1;
115 int nft_validate_input_register(enum nft_registers reg);
116 int nft_validate_output_register(enum nft_registers reg);
117 int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
118 const struct nft_data *data,
119 enum nft_data_types type);
122 * struct nft_set_elem - generic representation of set elements
124 * @cookie: implementation specific element cookie
126 * @data: element data (maps only)
127 * @flags: element flags (end of interval)
129 * The cookie can be used to store a handle to the element for subsequent
132 struct nft_set_elem {
135 struct nft_data data;
140 struct nft_set_iter {
144 int (*fn)(const struct nft_ctx *ctx,
145 const struct nft_set *set,
146 const struct nft_set_iter *iter,
147 const struct nft_set_elem *elem);
151 * struct nft_set_desc - description of set elements
155 * @size: number of set elements
157 struct nft_set_desc {
164 * enum nft_set_class - performance class
166 * @NFT_LOOKUP_O_1: constant, O(1)
167 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
168 * @NFT_LOOKUP_O_N: linear, O(N)
172 NFT_SET_CLASS_O_LOG_N,
177 * struct nft_set_estimate - estimation of memory and performance
180 * @size: required memory
181 * @class: lookup performance class
183 struct nft_set_estimate {
185 enum nft_set_class class;
189 * struct nft_set_ops - nf_tables set operations
191 * @lookup: look up an element within the set
192 * @insert: insert new element into set
193 * @remove: remove element from set
194 * @walk: iterate over all set elemeennts
195 * @privsize: function to return size of set private data
196 * @init: initialize private data of new set instance
197 * @destroy: destroy private data of set instance
198 * @list: nf_tables_set_ops list node
199 * @owner: module reference
200 * @features: features supported by the implementation
203 bool (*lookup)(const struct nft_set *set,
204 const struct nft_data *key,
205 struct nft_data *data);
206 int (*get)(const struct nft_set *set,
207 struct nft_set_elem *elem);
208 int (*insert)(const struct nft_set *set,
209 const struct nft_set_elem *elem);
210 void (*remove)(const struct nft_set *set,
211 const struct nft_set_elem *elem);
212 void (*walk)(const struct nft_ctx *ctx,
213 const struct nft_set *set,
214 struct nft_set_iter *iter);
216 unsigned int (*privsize)(const struct nlattr * const nla[]);
217 bool (*estimate)(const struct nft_set_desc *desc,
219 struct nft_set_estimate *est);
220 int (*init)(const struct nft_set *set,
221 const struct nft_set_desc *desc,
222 const struct nlattr * const nla[]);
223 void (*destroy)(const struct nft_set *set);
225 struct list_head list;
226 struct module *owner;
230 int nft_register_set(struct nft_set_ops *ops);
231 void nft_unregister_set(struct nft_set_ops *ops);
234 * struct nft_set - nf_tables set instance
236 * @list: table set list node
237 * @bindings: list of set bindings
238 * @name: name of the set
239 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
240 * @dtype: data type (verdict or numeric type defined by userspace)
241 * @size: maximum set size
242 * @nelems: number of elements
247 * @data: private set data
250 struct list_head list;
251 struct list_head bindings;
257 /* runtime data below here */
258 const struct nft_set_ops *ops ____cacheline_aligned;
263 __attribute__((aligned(__alignof__(u64))));
266 static inline void *nft_set_priv(const struct nft_set *set)
268 return (void *)set->data;
271 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
272 const struct nlattr *nla);
273 struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
274 const struct nlattr *nla);
277 * struct nft_set_binding - nf_tables set binding
279 * @list: set bindings list node
280 * @chain: chain containing the rule bound to the set
282 * A set binding contains all information necessary for validation
283 * of new elements added to a bound set.
285 struct nft_set_binding {
286 struct list_head list;
287 const struct nft_chain *chain;
290 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
291 struct nft_set_binding *binding);
292 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
293 struct nft_set_binding *binding);
297 * struct nft_expr_type - nf_tables expression type
299 * @select_ops: function to select nft_expr_ops
300 * @ops: default ops, used when no select_ops functions is present
301 * @list: used internally
303 * @owner: module reference
304 * @policy: netlink attribute policy
305 * @maxattr: highest netlink attribute number
306 * @family: address family for AF-specific types
308 struct nft_expr_type {
309 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
310 const struct nlattr * const tb[]);
311 const struct nft_expr_ops *ops;
312 struct list_head list;
314 struct module *owner;
315 const struct nla_policy *policy;
316 unsigned int maxattr;
321 * struct nft_expr_ops - nf_tables expression operations
323 * @eval: Expression evaluation function
324 * @size: full expression size, including private data size
325 * @init: initialization function
326 * @destroy: destruction function
327 * @dump: function to dump parameters
328 * @type: expression type
329 * @validate: validate expression, called during loop detection
330 * @data: extra data to attach to this expression operation
333 struct nft_expr_ops {
334 void (*eval)(const struct nft_expr *expr,
335 struct nft_data data[NFT_REG_MAX + 1],
336 const struct nft_pktinfo *pkt);
339 int (*init)(const struct nft_ctx *ctx,
340 const struct nft_expr *expr,
341 const struct nlattr * const tb[]);
342 void (*destroy)(const struct nft_ctx *ctx,
343 const struct nft_expr *expr);
344 int (*dump)(struct sk_buff *skb,
345 const struct nft_expr *expr);
346 int (*validate)(const struct nft_ctx *ctx,
347 const struct nft_expr *expr,
348 const struct nft_data **data);
349 const struct nft_expr_type *type;
353 #define NFT_EXPR_MAXATTR 16
354 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
355 ALIGN(size, __alignof__(struct nft_expr)))
358 * struct nft_expr - nf_tables expression
360 * @ops: expression ops
361 * @data: expression private data
364 const struct nft_expr_ops *ops;
365 unsigned char data[];
368 static inline void *nft_expr_priv(const struct nft_expr *expr)
370 return (void *)expr->data;
374 * struct nft_rule - nf_tables rule
376 * @list: used internally
377 * @handle: rule handle
378 * @genmask: generation mask
379 * @dlen: length of expression data
380 * @ulen: length of user data (used for comments)
381 * @data: expression data
384 struct list_head list;
390 __attribute__((aligned(__alignof__(struct nft_expr))));
394 * struct nft_trans - nf_tables object update in transaction
396 * @rcu_head: rcu head to defer release of transaction data
397 * @list: used internally
398 * @msg_type: message type
399 * @ctx: transaction context
400 * @data: internal information related to the transaction
403 struct rcu_head rcu_head;
404 struct list_head list;
410 struct nft_trans_rule {
411 struct nft_rule *rule;
414 #define nft_trans_rule(trans) \
415 (((struct nft_trans_rule *)trans->data)->rule)
417 struct nft_trans_set {
422 #define nft_trans_set(trans) \
423 (((struct nft_trans_set *)trans->data)->set)
424 #define nft_trans_set_id(trans) \
425 (((struct nft_trans_set *)trans->data)->set_id)
427 struct nft_trans_chain {
429 char name[NFT_CHAIN_MAXNAMELEN];
430 struct nft_stats __percpu *stats;
434 #define nft_trans_chain_update(trans) \
435 (((struct nft_trans_chain *)trans->data)->update)
436 #define nft_trans_chain_name(trans) \
437 (((struct nft_trans_chain *)trans->data)->name)
438 #define nft_trans_chain_stats(trans) \
439 (((struct nft_trans_chain *)trans->data)->stats)
440 #define nft_trans_chain_policy(trans) \
441 (((struct nft_trans_chain *)trans->data)->policy)
443 struct nft_trans_table {
448 #define nft_trans_table_update(trans) \
449 (((struct nft_trans_table *)trans->data)->update)
450 #define nft_trans_table_enable(trans) \
451 (((struct nft_trans_table *)trans->data)->enable)
453 struct nft_trans_elem {
455 struct nft_set_elem elem;
458 #define nft_trans_elem_set(trans) \
459 (((struct nft_trans_elem *)trans->data)->set)
460 #define nft_trans_elem(trans) \
461 (((struct nft_trans_elem *)trans->data)->elem)
463 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
465 return (struct nft_expr *)&rule->data[0];
468 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
470 return ((void *)expr) + expr->ops->size;
473 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
475 return (struct nft_expr *)&rule->data[rule->dlen];
478 static inline void *nft_userdata(const struct nft_rule *rule)
480 return (void *)&rule->data[rule->dlen];
484 * The last pointer isn't really necessary, but the compiler isn't able to
485 * determine that the result of nft_expr_last() is always the same since it
486 * can't assume that the dlen value wasn't changed within calls in the loop.
488 #define nft_rule_for_each_expr(expr, last, rule) \
489 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
491 (expr) = nft_expr_next(expr))
493 enum nft_chain_flags {
494 NFT_BASE_CHAIN = 0x1,
495 NFT_CHAIN_INACTIVE = 0x2,
499 * struct nft_chain - nf_tables chain
501 * @rules: list of rules in the chain
502 * @list: used internally
503 * @net: net namespace that this chain belongs to
504 * @table: table that this chain belongs to
505 * @handle: chain handle
506 * @flags: bitmask of enum nft_chain_flags
507 * @use: number of jump references to this chain
508 * @level: length of longest path to this chain
509 * @name: name of the chain
512 struct list_head rules;
513 struct list_head list;
515 struct nft_table *table;
520 char name[NFT_CHAIN_MAXNAMELEN];
523 enum nft_chain_type {
524 NFT_CHAIN_T_DEFAULT = 0,
535 #define NFT_HOOK_OPS_MAX 2
538 * struct nft_base_chain - nf_tables base chain
540 * @ops: netfilter hook ops
542 * @policy: default policy
543 * @stats: per-cpu chain stats
546 struct nft_base_chain {
547 struct nf_hook_ops ops[NFT_HOOK_OPS_MAX];
548 const struct nf_chain_type *type;
550 struct nft_stats __percpu *stats;
551 struct nft_chain chain;
554 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
556 return container_of(chain, struct nft_base_chain, chain);
559 unsigned int nft_do_chain(struct nft_pktinfo *pkt,
560 const struct nf_hook_ops *ops);
563 * struct nft_table - nf_tables table
565 * @list: used internally
566 * @chains: chains in the table
567 * @sets: sets in the table
568 * @hgenerator: handle generator state
569 * @use: number of chain references to this table
570 * @flags: table flag (see enum nft_table_flags)
571 * @name: name of the table
574 struct list_head list;
575 struct list_head chains;
576 struct list_head sets;
584 * struct nft_af_info - nf_tables address family info
586 * @list: used internally
587 * @family: address family
588 * @nhooks: number of hooks in this family
589 * @owner: module owner
590 * @tables: used internally
591 * @nops: number of hook ops in this family
592 * @hook_ops_init: initialization function for chain hook ops
593 * @hooks: hookfn overrides for packet validation
596 struct list_head list;
599 struct module *owner;
600 struct list_head tables;
602 void (*hook_ops_init)(struct nf_hook_ops *,
604 nf_hookfn *hooks[NF_MAX_HOOKS];
607 int nft_register_afinfo(struct net *, struct nft_af_info *);
608 void nft_unregister_afinfo(struct nft_af_info *);
611 * struct nf_chain_type - nf_tables chain type info
613 * @name: name of the type
614 * @type: numeric identifier
615 * @family: address family
616 * @owner: module owner
617 * @hook_mask: mask of valid hooks
618 * @hooks: hookfn overrides
620 struct nf_chain_type {
622 enum nft_chain_type type;
624 struct module *owner;
625 unsigned int hook_mask;
626 nf_hookfn *hooks[NF_MAX_HOOKS];
629 int nft_register_chain_type(const struct nf_chain_type *);
630 void nft_unregister_chain_type(const struct nf_chain_type *);
632 int nft_register_expr(struct nft_expr_type *);
633 void nft_unregister_expr(struct nft_expr_type *);
635 #define nft_dereference(p) \
636 nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
638 #define MODULE_ALIAS_NFT_FAMILY(family) \
639 MODULE_ALIAS("nft-afinfo-" __stringify(family))
641 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
642 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
644 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
645 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
647 #define MODULE_ALIAS_NFT_EXPR(name) \
648 MODULE_ALIAS("nft-expr-" name)
650 #define MODULE_ALIAS_NFT_SET() \
651 MODULE_ALIAS("nft-set")
653 #endif /* _NET_NF_TABLES_H */