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
;
19 /* for x_tables compatibility */
20 struct xt_action_param xt
;
23 static inline void nft_set_pktinfo(struct nft_pktinfo
*pkt
,
24 const struct nf_hook_ops
*ops
,
26 const struct net_device
*in
,
27 const struct net_device
*out
)
30 pkt
->in
= pkt
->xt
.in
= in
;
31 pkt
->out
= pkt
->xt
.out
= out
;
32 pkt
->hooknum
= pkt
->xt
.hooknum
= ops
->hooknum
;
33 pkt
->xt
.family
= ops
->pf
;
41 struct nft_chain
*chain
;
44 } __attribute__((aligned(__alignof__(u64
))));
46 static inline int nft_data_cmp(const struct nft_data
*d1
,
47 const struct nft_data
*d2
,
50 return memcmp(d1
->data
, d2
->data
, len
);
53 static inline void nft_data_copy(struct nft_data
*dst
,
54 const struct nft_data
*src
)
56 BUILD_BUG_ON(__alignof__(*dst
) != __alignof__(u64
));
57 *(u64
*)&dst
->data
[0] = *(u64
*)&src
->data
[0];
58 *(u64
*)&dst
->data
[2] = *(u64
*)&src
->data
[2];
61 static inline void nft_data_debug(const struct nft_data
*data
)
63 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
64 data
->data
[0], data
->data
[1],
65 data
->data
[2], data
->data
[3]);
69 * struct nft_ctx - nf_tables rule/set context
72 * @nlh: netlink message header
73 * @afi: address family info
74 * @table: the table the chain is contained in
75 * @chain: the chain the rule is contained in
76 * @nla: netlink attributes
79 const struct sk_buff
*skb
;
80 const struct nlmsghdr
*nlh
;
81 const struct nft_af_info
*afi
;
82 const struct nft_table
*table
;
83 const struct nft_chain
*chain
;
84 const struct nlattr
* const *nla
;
87 struct nft_data_desc
{
88 enum nft_data_types type
;
92 extern int nft_data_init(const struct nft_ctx
*ctx
, struct nft_data
*data
,
93 struct nft_data_desc
*desc
, const struct nlattr
*nla
);
94 extern void nft_data_uninit(const struct nft_data
*data
,
95 enum nft_data_types type
);
96 extern int nft_data_dump(struct sk_buff
*skb
, int attr
,
97 const struct nft_data
*data
,
98 enum nft_data_types type
, unsigned int len
);
100 static inline enum nft_data_types
nft_dreg_to_type(enum nft_registers reg
)
102 return reg
== NFT_REG_VERDICT
? NFT_DATA_VERDICT
: NFT_DATA_VALUE
;
105 static inline enum nft_registers
nft_type_to_reg(enum nft_data_types type
)
107 return type
== NFT_DATA_VERDICT
? NFT_REG_VERDICT
: NFT_REG_1
;
110 extern int nft_validate_input_register(enum nft_registers reg
);
111 extern int nft_validate_output_register(enum nft_registers reg
);
112 extern int nft_validate_data_load(const struct nft_ctx
*ctx
,
113 enum nft_registers reg
,
114 const struct nft_data
*data
,
115 enum nft_data_types type
);
118 * struct nft_set_elem - generic representation of set elements
120 * @cookie: implementation specific element cookie
122 * @data: element data (maps only)
123 * @flags: element flags (end of interval)
125 * The cookie can be used to store a handle to the element for subsequent
128 struct nft_set_elem
{
131 struct nft_data data
;
136 struct nft_set_iter
{
140 int (*fn
)(const struct nft_ctx
*ctx
,
141 const struct nft_set
*set
,
142 const struct nft_set_iter
*iter
,
143 const struct nft_set_elem
*elem
);
147 * struct nft_set_ops - nf_tables set operations
149 * @lookup: look up an element within the set
150 * @insert: insert new element into set
151 * @remove: remove element from set
152 * @walk: iterate over all set elemeennts
153 * @privsize: function to return size of set private data
154 * @init: initialize private data of new set instance
155 * @destroy: destroy private data of set instance
156 * @list: nf_tables_set_ops list node
157 * @owner: module reference
158 * @features: features supported by the implementation
161 bool (*lookup
)(const struct nft_set
*set
,
162 const struct nft_data
*key
,
163 struct nft_data
*data
);
164 int (*get
)(const struct nft_set
*set
,
165 struct nft_set_elem
*elem
);
166 int (*insert
)(const struct nft_set
*set
,
167 const struct nft_set_elem
*elem
);
168 void (*remove
)(const struct nft_set
*set
,
169 const struct nft_set_elem
*elem
);
170 void (*walk
)(const struct nft_ctx
*ctx
,
171 const struct nft_set
*set
,
172 struct nft_set_iter
*iter
);
174 unsigned int (*privsize
)(const struct nlattr
* const nla
[]);
175 int (*init
)(const struct nft_set
*set
,
176 const struct nlattr
* const nla
[]);
177 void (*destroy
)(const struct nft_set
*set
);
179 struct list_head list
;
180 struct module
*owner
;
184 extern int nft_register_set(struct nft_set_ops
*ops
);
185 extern void nft_unregister_set(struct nft_set_ops
*ops
);
188 * struct nft_set - nf_tables set instance
190 * @list: table set list node
191 * @bindings: list of set bindings
192 * @name: name of the set
193 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
194 * @dtype: data type (verdict or numeric type defined by userspace)
199 * @data: private set data
202 struct list_head list
;
203 struct list_head bindings
;
207 /* runtime data below here */
208 const struct nft_set_ops
*ops ____cacheline_aligned
;
213 __attribute__((aligned(__alignof__(u64
))));
216 static inline void *nft_set_priv(const struct nft_set
*set
)
218 return (void *)set
->data
;
221 extern struct nft_set
*nf_tables_set_lookup(const struct nft_table
*table
,
222 const struct nlattr
*nla
);
225 * struct nft_set_binding - nf_tables set binding
227 * @list: set bindings list node
228 * @chain: chain containing the rule bound to the set
230 * A set binding contains all information necessary for validation
231 * of new elements added to a bound set.
233 struct nft_set_binding
{
234 struct list_head list
;
235 const struct nft_chain
*chain
;
238 extern int nf_tables_bind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
239 struct nft_set_binding
*binding
);
240 extern void nf_tables_unbind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
241 struct nft_set_binding
*binding
);
245 * struct nft_expr_type - nf_tables expression type
247 * @select_ops: function to select nft_expr_ops
248 * @ops: default ops, used when no select_ops functions is present
249 * @list: used internally
251 * @owner: module reference
252 * @policy: netlink attribute policy
253 * @maxattr: highest netlink attribute number
255 struct nft_expr_type
{
256 const struct nft_expr_ops
*(*select_ops
)(const struct nft_ctx
*,
257 const struct nlattr
* const tb
[]);
258 const struct nft_expr_ops
*ops
;
259 struct list_head list
;
261 struct module
*owner
;
262 const struct nla_policy
*policy
;
263 unsigned int maxattr
;
267 * struct nft_expr_ops - nf_tables expression operations
269 * @eval: Expression evaluation function
270 * @size: full expression size, including private data size
271 * @init: initialization function
272 * @destroy: destruction function
273 * @dump: function to dump parameters
274 * @type: expression type
275 * @validate: validate expression, called during loop detection
276 * @data: extra data to attach to this expression operation
279 struct nft_expr_ops
{
280 void (*eval
)(const struct nft_expr
*expr
,
281 struct nft_data data
[NFT_REG_MAX
+ 1],
282 const struct nft_pktinfo
*pkt
);
285 int (*init
)(const struct nft_ctx
*ctx
,
286 const struct nft_expr
*expr
,
287 const struct nlattr
* const tb
[]);
288 void (*destroy
)(const struct nft_expr
*expr
);
289 int (*dump
)(struct sk_buff
*skb
,
290 const struct nft_expr
*expr
);
291 int (*validate
)(const struct nft_ctx
*ctx
,
292 const struct nft_expr
*expr
,
293 const struct nft_data
**data
);
294 const struct nft_expr_type
*type
;
298 #define NFT_EXPR_MAXATTR 16
299 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
300 ALIGN(size, __alignof__(struct nft_expr)))
303 * struct nft_expr - nf_tables expression
305 * @ops: expression ops
306 * @data: expression private data
309 const struct nft_expr_ops
*ops
;
310 unsigned char data
[];
313 static inline void *nft_expr_priv(const struct nft_expr
*expr
)
315 return (void *)expr
->data
;
319 * struct nft_rule - nf_tables rule
321 * @list: used internally
322 * @rcu_head: used internally for rcu
323 * @handle: rule handle
324 * @dlen: length of expression data
325 * @data: expression data
328 struct list_head list
;
329 struct rcu_head rcu_head
;
333 __attribute__((aligned(__alignof__(struct nft_expr
))));
336 static inline struct nft_expr
*nft_expr_first(const struct nft_rule
*rule
)
338 return (struct nft_expr
*)&rule
->data
[0];
341 static inline struct nft_expr
*nft_expr_next(const struct nft_expr
*expr
)
343 return ((void *)expr
) + expr
->ops
->size
;
346 static inline struct nft_expr
*nft_expr_last(const struct nft_rule
*rule
)
348 return (struct nft_expr
*)&rule
->data
[rule
->dlen
];
352 * The last pointer isn't really necessary, but the compiler isn't able to
353 * determine that the result of nft_expr_last() is always the same since it
354 * can't assume that the dlen value wasn't changed within calls in the loop.
356 #define nft_rule_for_each_expr(expr, last, rule) \
357 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
359 (expr) = nft_expr_next(expr))
361 enum nft_chain_flags
{
362 NFT_BASE_CHAIN
= 0x1,
366 * struct nft_chain - nf_tables chain
368 * @rules: list of rules in the chain
369 * @list: used internally
370 * @rcu_head: used internally
371 * @handle: chain handle
372 * @flags: bitmask of enum nft_chain_flags
373 * @use: number of jump references to this chain
374 * @level: length of longest path to this chain
375 * @name: name of the chain
378 struct list_head rules
;
379 struct list_head list
;
380 struct rcu_head rcu_head
;
385 char name
[NFT_CHAIN_MAXNAMELEN
];
388 enum nft_chain_type
{
389 NFT_CHAIN_T_DEFAULT
= 0,
401 * struct nft_base_chain - nf_tables base chain
403 * @ops: netfilter hook ops
405 * @policy: default policy
406 * @stats: per-cpu chain stats
409 struct nft_base_chain
{
410 struct nf_hook_ops ops
;
411 enum nft_chain_type type
;
413 struct nft_stats __percpu
*stats
;
414 struct nft_chain chain
;
417 static inline struct nft_base_chain
*nft_base_chain(const struct nft_chain
*chain
)
419 return container_of(chain
, struct nft_base_chain
, chain
);
422 extern unsigned int nft_do_chain_pktinfo(struct nft_pktinfo
*pkt
,
423 const struct nf_hook_ops
*ops
);
426 * struct nft_table - nf_tables table
428 * @list: used internally
429 * @chains: chains in the table
430 * @sets: sets in the table
431 * @hgenerator: handle generator state
432 * @use: number of chain references to this table
433 * @flags: table flag (see enum nft_table_flags)
434 * @name: name of the table
437 struct list_head list
;
438 struct list_head chains
;
439 struct list_head sets
;
447 * struct nft_af_info - nf_tables address family info
449 * @list: used internally
450 * @family: address family
451 * @nhooks: number of hooks in this family
452 * @owner: module owner
453 * @tables: used internally
454 * @hooks: hookfn overrides for packet validation
457 struct list_head list
;
460 struct module
*owner
;
461 struct list_head tables
;
462 nf_hookfn
*hooks
[NF_MAX_HOOKS
];
465 extern int nft_register_afinfo(struct nft_af_info
*);
466 extern void nft_unregister_afinfo(struct nft_af_info
*);
468 struct nf_chain_type
{
469 unsigned int hook_mask
;
471 enum nft_chain_type type
;
472 nf_hookfn
*fn
[NF_MAX_HOOKS
];
477 extern int nft_register_chain_type(struct nf_chain_type
*);
478 extern void nft_unregister_chain_type(struct nf_chain_type
*);
480 extern int nft_register_expr(struct nft_expr_type
*);
481 extern void nft_unregister_expr(struct nft_expr_type
*);
483 #define MODULE_ALIAS_NFT_FAMILY(family) \
484 MODULE_ALIAS("nft-afinfo-" __stringify(family))
486 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
487 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
489 #define MODULE_ALIAS_NFT_EXPR(name) \
490 MODULE_ALIAS("nft-expr-" name)
492 #define MODULE_ALIAS_NFT_SET() \
493 MODULE_ALIAS("nft-set")
495 #endif /* _NET_NF_TABLES_H */