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/x_tables.h>
7 #include <linux/netfilter/nf_tables.h>
8 #include <net/netlink.h>
10 #define NFT_JUMP_STACK_SIZE 16
14 const struct net_device *in;
15 const struct net_device *out;
16 const struct nf_hook_ops *ops;
20 /* for x_tables compatibility */
21 struct xt_action_param xt;
24 static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
25 const struct nf_hook_ops *ops,
27 const struct net_device *in,
28 const struct net_device *out)
31 pkt->in = pkt->xt.in = in;
32 pkt->out = pkt->xt.out = out;
34 pkt->xt.hooknum = ops->hooknum;
35 pkt->xt.family = ops->pf;
43 struct nft_chain *chain;
46 } __attribute__((aligned(__alignof__(u64))));
48 static inline int nft_data_cmp(const struct nft_data *d1,
49 const struct nft_data *d2,
52 return memcmp(d1->data, d2->data, len);
55 static inline void nft_data_copy(struct nft_data *dst,
56 const struct nft_data *src)
58 BUILD_BUG_ON(__alignof__(*dst) != __alignof__(u64));
59 *(u64 *)&dst->data[0] = *(u64 *)&src->data[0];
60 *(u64 *)&dst->data[2] = *(u64 *)&src->data[2];
63 static inline void nft_data_debug(const struct nft_data *data)
65 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
66 data->data[0], data->data[1],
67 data->data[2], data->data[3]);
71 * struct nft_ctx - nf_tables rule/set context
75 * @nlh: netlink message header
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
83 const struct sk_buff *skb;
84 const struct nlmsghdr *nlh;
85 const struct nft_af_info *afi;
86 const struct nft_table *table;
87 const struct nft_chain *chain;
88 const struct nlattr * const *nla;
91 struct nft_data_desc {
92 enum nft_data_types type;
96 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
97 struct nft_data_desc *desc, const struct nlattr *nla);
98 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type);
99 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
100 enum nft_data_types type, unsigned int len);
102 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
104 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
107 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
109 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1;
112 int nft_validate_input_register(enum nft_registers reg);
113 int nft_validate_output_register(enum nft_registers reg);
114 int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
115 const struct nft_data *data,
116 enum nft_data_types type);
119 * struct nft_set_elem - generic representation of set elements
121 * @cookie: implementation specific element cookie
123 * @data: element data (maps only)
124 * @flags: element flags (end of interval)
126 * The cookie can be used to store a handle to the element for subsequent
129 struct nft_set_elem {
132 struct nft_data data;
137 struct nft_set_iter {
141 int (*fn)(const struct nft_ctx *ctx,
142 const struct nft_set *set,
143 const struct nft_set_iter *iter,
144 const struct nft_set_elem *elem);
148 * struct nft_set_ops - nf_tables set operations
150 * @lookup: look up an element within the set
151 * @insert: insert new element into set
152 * @remove: remove element from set
153 * @walk: iterate over all set elemeennts
154 * @privsize: function to return size of set private data
155 * @init: initialize private data of new set instance
156 * @destroy: destroy private data of set instance
157 * @list: nf_tables_set_ops list node
158 * @owner: module reference
159 * @features: features supported by the implementation
162 bool (*lookup)(const struct nft_set *set,
163 const struct nft_data *key,
164 struct nft_data *data);
165 int (*get)(const struct nft_set *set,
166 struct nft_set_elem *elem);
167 int (*insert)(const struct nft_set *set,
168 const struct nft_set_elem *elem);
169 void (*remove)(const struct nft_set *set,
170 const struct nft_set_elem *elem);
171 void (*walk)(const struct nft_ctx *ctx,
172 const struct nft_set *set,
173 struct nft_set_iter *iter);
175 unsigned int (*privsize)(const struct nlattr * const nla[]);
176 int (*init)(const struct nft_set *set,
177 const struct nlattr * const nla[]);
178 void (*destroy)(const struct nft_set *set);
180 struct list_head list;
181 struct module *owner;
185 int nft_register_set(struct nft_set_ops *ops);
186 void nft_unregister_set(struct nft_set_ops *ops);
189 * struct nft_set - nf_tables set instance
191 * @list: table set list node
192 * @bindings: list of set bindings
193 * @name: name of the set
194 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
195 * @dtype: data type (verdict or numeric type defined by userspace)
200 * @data: private set data
203 struct list_head list;
204 struct list_head bindings;
208 /* runtime data below here */
209 const struct nft_set_ops *ops ____cacheline_aligned;
214 __attribute__((aligned(__alignof__(u64))));
217 static inline void *nft_set_priv(const struct nft_set *set)
219 return (void *)set->data;
222 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
223 const struct nlattr *nla);
226 * struct nft_set_binding - nf_tables set binding
228 * @list: set bindings list node
229 * @chain: chain containing the rule bound to the set
231 * A set binding contains all information necessary for validation
232 * of new elements added to a bound set.
234 struct nft_set_binding {
235 struct list_head list;
236 const struct nft_chain *chain;
239 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
240 struct nft_set_binding *binding);
241 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
242 struct nft_set_binding *binding);
246 * struct nft_expr_type - nf_tables expression type
248 * @select_ops: function to select nft_expr_ops
249 * @ops: default ops, used when no select_ops functions is present
250 * @list: used internally
252 * @owner: module reference
253 * @policy: netlink attribute policy
254 * @maxattr: highest netlink attribute number
256 struct nft_expr_type {
257 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
258 const struct nlattr * const tb[]);
259 const struct nft_expr_ops *ops;
260 struct list_head list;
262 struct module *owner;
263 const struct nla_policy *policy;
264 unsigned int maxattr;
268 * struct nft_expr_ops - nf_tables expression operations
270 * @eval: Expression evaluation function
271 * @size: full expression size, including private data size
272 * @init: initialization function
273 * @destroy: destruction function
274 * @dump: function to dump parameters
275 * @type: expression type
276 * @validate: validate expression, called during loop detection
277 * @data: extra data to attach to this expression operation
280 struct nft_expr_ops {
281 void (*eval)(const struct nft_expr *expr,
282 struct nft_data data[NFT_REG_MAX + 1],
283 const struct nft_pktinfo *pkt);
286 int (*init)(const struct nft_ctx *ctx,
287 const struct nft_expr *expr,
288 const struct nlattr * const tb[]);
289 void (*destroy)(const struct nft_expr *expr);
290 int (*dump)(struct sk_buff *skb,
291 const struct nft_expr *expr);
292 int (*validate)(const struct nft_ctx *ctx,
293 const struct nft_expr *expr,
294 const struct nft_data **data);
295 const struct nft_expr_type *type;
299 #define NFT_EXPR_MAXATTR 16
300 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
301 ALIGN(size, __alignof__(struct nft_expr)))
304 * struct nft_expr - nf_tables expression
306 * @ops: expression ops
307 * @data: expression private data
310 const struct nft_expr_ops *ops;
311 unsigned char data[];
314 static inline void *nft_expr_priv(const struct nft_expr *expr)
316 return (void *)expr->data;
320 * struct nft_rule - nf_tables rule
322 * @list: used internally
323 * @rcu_head: used internally for rcu
324 * @handle: rule handle
325 * @genmask: generation mask
326 * @dlen: length of expression data
327 * @data: expression data
330 struct list_head list;
331 struct rcu_head rcu_head;
336 __attribute__((aligned(__alignof__(struct nft_expr))));
340 * struct nft_rule_trans - nf_tables rule update in transaction
342 * @list: used internally
343 * @rule: rule that needs to be updated
344 * @chain: chain that this rule belongs to
345 * @table: table for which this chain applies
346 * @nlh: netlink header of the message that contain this update
347 * @family: family expressesed as AF_*
349 struct nft_rule_trans {
350 struct list_head list;
351 struct nft_rule *rule;
352 const struct nft_chain *chain;
353 const struct nft_table *table;
354 const struct nlmsghdr *nlh;
358 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
360 return (struct nft_expr *)&rule->data[0];
363 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
365 return ((void *)expr) + expr->ops->size;
368 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
370 return (struct nft_expr *)&rule->data[rule->dlen];
374 * The last pointer isn't really necessary, but the compiler isn't able to
375 * determine that the result of nft_expr_last() is always the same since it
376 * can't assume that the dlen value wasn't changed within calls in the loop.
378 #define nft_rule_for_each_expr(expr, last, rule) \
379 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
381 (expr) = nft_expr_next(expr))
383 enum nft_chain_flags {
384 NFT_BASE_CHAIN = 0x1,
388 * struct nft_chain - nf_tables chain
390 * @rules: list of rules in the chain
391 * @list: used internally
392 * @rcu_head: used internally
393 * @net: net namespace that this chain belongs to
394 * @table: table that this chain belongs to
395 * @handle: chain handle
396 * @flags: bitmask of enum nft_chain_flags
397 * @use: number of jump references to this chain
398 * @level: length of longest path to this chain
399 * @name: name of the chain
402 struct list_head rules;
403 struct list_head list;
404 struct rcu_head rcu_head;
406 struct nft_table *table;
411 char name[NFT_CHAIN_MAXNAMELEN];
414 enum nft_chain_type {
415 NFT_CHAIN_T_DEFAULT = 0,
426 #define NFT_HOOK_OPS_MAX 2
429 * struct nft_base_chain - nf_tables base chain
431 * @ops: netfilter hook ops
433 * @policy: default policy
434 * @stats: per-cpu chain stats
437 struct nft_base_chain {
438 struct nf_hook_ops ops[NFT_HOOK_OPS_MAX];
439 const struct nf_chain_type *type;
441 struct nft_stats __percpu *stats;
442 struct nft_chain chain;
445 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
447 return container_of(chain, struct nft_base_chain, chain);
450 unsigned int nft_do_chain(struct nft_pktinfo *pkt,
451 const struct nf_hook_ops *ops);
454 * struct nft_table - nf_tables table
456 * @list: used internally
457 * @chains: chains in the table
458 * @sets: sets in the table
459 * @hgenerator: handle generator state
460 * @use: number of chain references to this table
461 * @flags: table flag (see enum nft_table_flags)
462 * @name: name of the table
465 struct list_head list;
466 struct list_head chains;
467 struct list_head sets;
475 * struct nft_af_info - nf_tables address family info
477 * @list: used internally
478 * @family: address family
479 * @nhooks: number of hooks in this family
480 * @owner: module owner
481 * @tables: used internally
482 * @nops: number of hook ops in this family
483 * @hook_ops_init: initialization function for chain hook ops
484 * @hooks: hookfn overrides for packet validation
487 struct list_head list;
490 struct module *owner;
491 struct list_head tables;
493 void (*hook_ops_init)(struct nf_hook_ops *,
495 nf_hookfn *hooks[NF_MAX_HOOKS];
498 int nft_register_afinfo(struct net *, struct nft_af_info *);
499 void nft_unregister_afinfo(struct nft_af_info *);
502 * struct nf_chain_type - nf_tables chain type info
504 * @name: name of the type
505 * @type: numeric identifier
506 * @family: address family
507 * @owner: module owner
508 * @hook_mask: mask of valid hooks
509 * @hooks: hookfn overrides
511 struct nf_chain_type {
513 enum nft_chain_type type;
515 struct module *owner;
516 unsigned int hook_mask;
517 nf_hookfn *hooks[NF_MAX_HOOKS];
520 int nft_register_chain_type(const struct nf_chain_type *);
521 void nft_unregister_chain_type(const struct nf_chain_type *);
523 int nft_register_expr(struct nft_expr_type *);
524 void nft_unregister_expr(struct nft_expr_type *);
526 #define MODULE_ALIAS_NFT_FAMILY(family) \
527 MODULE_ALIAS("nft-afinfo-" __stringify(family))
529 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
530 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
532 #define MODULE_ALIAS_NFT_EXPR(name) \
533 MODULE_ALIAS("nft-expr-" name)
535 #define MODULE_ALIAS_NFT_SET() \
536 MODULE_ALIAS("nft-set")
538 #endif /* _NET_NF_TABLES_H */