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/nf_tables.h>
7 #include <net/netlink.h>
11 const struct net_device *in;
12 const struct net_device *out;
23 struct nft_chain *chain;
26 } __attribute__((aligned(__alignof__(u64))));
28 static inline int nft_data_cmp(const struct nft_data *d1,
29 const struct nft_data *d2,
32 return memcmp(d1->data, d2->data, len);
35 static inline void nft_data_copy(struct nft_data *dst,
36 const struct nft_data *src)
38 BUILD_BUG_ON(__alignof__(*dst) != __alignof__(u64));
39 *(u64 *)&dst->data[0] = *(u64 *)&src->data[0];
40 *(u64 *)&dst->data[2] = *(u64 *)&src->data[2];
43 static inline void nft_data_debug(const struct nft_data *data)
45 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
46 data->data[0], data->data[1],
47 data->data[2], data->data[3]);
51 * struct nft_ctx - nf_tables rule context
53 * @afi: address family info
54 * @table: the table the chain is contained in
55 * @chain: the chain the rule is contained in
58 const struct nft_af_info *afi;
59 const struct nft_table *table;
60 const struct nft_chain *chain;
68 struct nft_data_desc {
69 enum nft_data_types type;
73 extern int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
74 struct nft_data_desc *desc, const struct nlattr *nla);
75 extern void nft_data_uninit(const struct nft_data *data,
76 enum nft_data_types type);
77 extern int nft_data_dump(struct sk_buff *skb, int attr,
78 const struct nft_data *data,
79 enum nft_data_types type, unsigned int len);
81 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
83 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
86 extern int nft_validate_input_register(enum nft_registers reg);
87 extern int nft_validate_output_register(enum nft_registers reg);
88 extern int nft_validate_data_load(const struct nft_ctx *ctx,
89 enum nft_registers reg,
90 const struct nft_data *data,
91 enum nft_data_types type);
94 * struct nft_expr_ops - nf_tables expression operations
96 * @eval: Expression evaluation function
97 * @init: initialization function
98 * @destroy: destruction function
99 * @dump: function to dump parameters
100 * @list: used internally
102 * @owner: module reference
103 * @policy: netlink attribute policy
104 * @maxattr: highest netlink attribute number
105 * @size: full expression size, including private data size
108 struct nft_expr_ops {
109 void (*eval)(const struct nft_expr *expr,
110 struct nft_data data[NFT_REG_MAX + 1],
111 const struct nft_pktinfo *pkt);
112 int (*init)(const struct nft_ctx *ctx,
113 const struct nft_expr *expr,
114 const struct nlattr * const tb[]);
115 void (*destroy)(const struct nft_expr *expr);
116 int (*dump)(struct sk_buff *skb,
117 const struct nft_expr *expr);
119 struct list_head list;
121 struct module *owner;
122 const struct nla_policy *policy;
123 unsigned int maxattr;
127 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
128 ALIGN(size, __alignof__(struct nft_expr)))
131 * struct nft_expr - nf_tables expression
133 * @ops: expression ops
134 * @data: expression private data
137 const struct nft_expr_ops *ops;
138 unsigned char data[];
141 static inline void *nft_expr_priv(const struct nft_expr *expr)
143 return (void *)expr->data;
147 * struct nft_rule - nf_tables rule
149 * @list: used internally
150 * @rcu_head: used internally for rcu
151 * @handle: rule handle
152 * @dlen: length of expression data
153 * @data: expression data
156 struct list_head list;
157 struct rcu_head rcu_head;
161 __attribute__((aligned(__alignof__(struct nft_expr))));
164 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
166 return (struct nft_expr *)&rule->data[0];
169 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
171 return ((void *)expr) + expr->ops->size;
174 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
176 return (struct nft_expr *)&rule->data[rule->dlen];
180 * The last pointer isn't really necessary, but the compiler isn't able to
181 * determine that the result of nft_expr_last() is always the same since it
182 * can't assume that the dlen value wasn't changed within calls in the loop.
184 #define nft_rule_for_each_expr(expr, last, rule) \
185 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
187 (expr) = nft_expr_next(expr))
189 enum nft_chain_flags {
190 NFT_BASE_CHAIN = 0x1,
191 NFT_CHAIN_BUILTIN = 0x2,
195 * struct nft_chain - nf_tables chain
197 * @rules: list of rules in the chain
198 * @list: used internally
199 * @rcu_head: used internally
200 * @handle: chain handle
201 * @flags: bitmask of enum nft_chain_flags
202 * @use: number of jump references to this chain
203 * @level: length of longest path to this chain
204 * @name: name of the chain
207 struct list_head rules;
208 struct list_head list;
209 struct rcu_head rcu_head;
214 char name[NFT_CHAIN_MAXNAMELEN];
218 * struct nft_base_chain - nf_tables base chain
220 * @ops: netfilter hook ops
223 struct nft_base_chain {
224 struct nf_hook_ops ops;
225 struct nft_chain chain;
228 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
230 return container_of(chain, struct nft_base_chain, chain);
233 extern unsigned int nft_do_chain(const struct nf_hook_ops *ops,
235 const struct net_device *in,
236 const struct net_device *out,
237 int (*okfn)(struct sk_buff *));
239 enum nft_table_flags {
240 NFT_TABLE_BUILTIN = 0x1,
244 * struct nft_table - nf_tables table
246 * @list: used internally
247 * @chains: chains in the table
248 * @sets: sets in the table
249 * @hgenerator: handle generator state
250 * @use: number of chain references to this table
251 * @flags: table flag (see enum nft_table_flags)
252 * @name: name of the table
255 struct list_head list;
256 struct list_head chains;
257 struct list_head sets;
265 * struct nft_af_info - nf_tables address family info
267 * @list: used internally
268 * @family: address family
269 * @nhooks: number of hooks in this family
270 * @owner: module owner
271 * @tables: used internally
272 * @hooks: hookfn overrides for packet validation
275 struct list_head list;
278 struct module *owner;
279 struct list_head tables;
280 nf_hookfn *hooks[NF_MAX_HOOKS];
283 extern int nft_register_afinfo(struct nft_af_info *);
284 extern void nft_unregister_afinfo(struct nft_af_info *);
286 extern int nft_register_table(struct nft_table *, int family);
287 extern void nft_unregister_table(struct nft_table *, int family);
289 extern int nft_register_expr(struct nft_expr_ops *);
290 extern void nft_unregister_expr(struct nft_expr_ops *);
292 #define MODULE_ALIAS_NFT_FAMILY(family) \
293 MODULE_ALIAS("nft-afinfo-" __stringify(family))
295 #define MODULE_ALIAS_NFT_TABLE(family, name) \
296 MODULE_ALIAS("nft-table-" __stringify(family) "-" name)
298 #define MODULE_ALIAS_NFT_EXPR(name) \
299 MODULE_ALIAS("nft-expr-" name)
301 #endif /* _NET_NF_TABLES_H */