e9b97862bf529087a6eaf4b14b7f16f8861742bf
[deliverable/linux.git] / include / net / netfilter / nf_tables.h
1 #ifndef _NET_NF_TABLES_H
2 #define _NET_NF_TABLES_H
3
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>
9
10 #define NFT_JUMP_STACK_SIZE 16
11
12 struct nft_pktinfo {
13 struct sk_buff *skb;
14 const struct net_device *in;
15 const struct net_device *out;
16 const struct nf_hook_ops *ops;
17 u8 nhoff;
18 u8 thoff;
19 u8 tprot;
20 /* for x_tables compatibility */
21 struct xt_action_param xt;
22 };
23
24 static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
25 const struct nf_hook_ops *ops,
26 struct sk_buff *skb,
27 const struct net_device *in,
28 const struct net_device *out)
29 {
30 pkt->skb = skb;
31 pkt->in = pkt->xt.in = in;
32 pkt->out = pkt->xt.out = out;
33 pkt->ops = ops;
34 pkt->xt.hooknum = ops->hooknum;
35 pkt->xt.family = ops->pf;
36 }
37
38 struct nft_data {
39 union {
40 u32 data[4];
41 struct {
42 u32 verdict;
43 struct nft_chain *chain;
44 };
45 };
46 } __attribute__((aligned(__alignof__(u64))));
47
48 static inline int nft_data_cmp(const struct nft_data *d1,
49 const struct nft_data *d2,
50 unsigned int len)
51 {
52 return memcmp(d1->data, d2->data, len);
53 }
54
55 static inline void nft_data_copy(struct nft_data *dst,
56 const struct nft_data *src)
57 {
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];
61 }
62
63 static inline void nft_data_debug(const struct nft_data *data)
64 {
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]);
68 }
69
70 /**
71 * struct nft_ctx - nf_tables rule/set context
72 *
73 * @net: net namespace
74 * @skb: netlink skb
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
80 */
81 struct nft_ctx {
82 struct net *net;
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;
89 };
90
91 struct nft_data_desc {
92 enum nft_data_types type;
93 unsigned int len;
94 };
95
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);
101
102 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
103 {
104 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
105 }
106
107 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
108 {
109 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1;
110 }
111
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);
117
118 /**
119 * struct nft_set_elem - generic representation of set elements
120 *
121 * @cookie: implementation specific element cookie
122 * @key: element key
123 * @data: element data (maps only)
124 * @flags: element flags (end of interval)
125 *
126 * The cookie can be used to store a handle to the element for subsequent
127 * removal.
128 */
129 struct nft_set_elem {
130 void *cookie;
131 struct nft_data key;
132 struct nft_data data;
133 u32 flags;
134 };
135
136 struct nft_set;
137 struct nft_set_iter {
138 unsigned int count;
139 unsigned int skip;
140 int err;
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);
145 };
146
147 /**
148 * struct nft_set_ops - nf_tables set operations
149 *
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
160 */
161 struct nft_set_ops {
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);
174
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);
179
180 struct list_head list;
181 struct module *owner;
182 u32 features;
183 };
184
185 int nft_register_set(struct nft_set_ops *ops);
186 void nft_unregister_set(struct nft_set_ops *ops);
187
188 /**
189 * struct nft_set - nf_tables set instance
190 *
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)
196 * @ops: set ops
197 * @flags: set flags
198 * @klen: key length
199 * @dlen: data length
200 * @data: private set data
201 */
202 struct nft_set {
203 struct list_head list;
204 struct list_head bindings;
205 char name[IFNAMSIZ];
206 u32 ktype;
207 u32 dtype;
208 /* runtime data below here */
209 const struct nft_set_ops *ops ____cacheline_aligned;
210 u16 flags;
211 u8 klen;
212 u8 dlen;
213 unsigned char data[]
214 __attribute__((aligned(__alignof__(u64))));
215 };
216
217 static inline void *nft_set_priv(const struct nft_set *set)
218 {
219 return (void *)set->data;
220 }
221
222 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
223 const struct nlattr *nla);
224
225 /**
226 * struct nft_set_binding - nf_tables set binding
227 *
228 * @list: set bindings list node
229 * @chain: chain containing the rule bound to the set
230 *
231 * A set binding contains all information necessary for validation
232 * of new elements added to a bound set.
233 */
234 struct nft_set_binding {
235 struct list_head list;
236 const struct nft_chain *chain;
237 };
238
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);
243
244
245 /**
246 * struct nft_expr_type - nf_tables expression type
247 *
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
251 * @name: Identifier
252 * @owner: module reference
253 * @policy: netlink attribute policy
254 * @maxattr: highest netlink attribute number
255 */
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;
261 const char *name;
262 struct module *owner;
263 const struct nla_policy *policy;
264 unsigned int maxattr;
265 };
266
267 /**
268 * struct nft_expr_ops - nf_tables expression operations
269 *
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
278 */
279 struct nft_expr;
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);
284 unsigned int size;
285
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;
296 void *data;
297 };
298
299 #define NFT_EXPR_MAXATTR 16
300 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
301 ALIGN(size, __alignof__(struct nft_expr)))
302
303 /**
304 * struct nft_expr - nf_tables expression
305 *
306 * @ops: expression ops
307 * @data: expression private data
308 */
309 struct nft_expr {
310 const struct nft_expr_ops *ops;
311 unsigned char data[];
312 };
313
314 static inline void *nft_expr_priv(const struct nft_expr *expr)
315 {
316 return (void *)expr->data;
317 }
318
319 /**
320 * struct nft_rule - nf_tables rule
321 *
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
328 */
329 struct nft_rule {
330 struct list_head list;
331 struct rcu_head rcu_head;
332 u64 handle:46,
333 genmask:2,
334 dlen:16;
335 unsigned char data[]
336 __attribute__((aligned(__alignof__(struct nft_expr))));
337 };
338
339 /**
340 * struct nft_rule_trans - nf_tables rule update in transaction
341 *
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_*
348 */
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;
355 u8 family;
356 };
357
358 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
359 {
360 return (struct nft_expr *)&rule->data[0];
361 }
362
363 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
364 {
365 return ((void *)expr) + expr->ops->size;
366 }
367
368 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
369 {
370 return (struct nft_expr *)&rule->data[rule->dlen];
371 }
372
373 /*
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.
377 */
378 #define nft_rule_for_each_expr(expr, last, rule) \
379 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
380 (expr) != (last); \
381 (expr) = nft_expr_next(expr))
382
383 enum nft_chain_flags {
384 NFT_BASE_CHAIN = 0x1,
385 };
386
387 /**
388 * struct nft_chain - nf_tables chain
389 *
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
400 */
401 struct nft_chain {
402 struct list_head rules;
403 struct list_head list;
404 struct rcu_head rcu_head;
405 struct net *net;
406 struct nft_table *table;
407 u64 handle;
408 u8 flags;
409 u16 use;
410 u16 level;
411 char name[NFT_CHAIN_MAXNAMELEN];
412 };
413
414 enum nft_chain_type {
415 NFT_CHAIN_T_DEFAULT = 0,
416 NFT_CHAIN_T_ROUTE,
417 NFT_CHAIN_T_NAT,
418 NFT_CHAIN_T_MAX
419 };
420
421 struct nft_stats {
422 u64 bytes;
423 u64 pkts;
424 };
425
426 #define NFT_HOOK_OPS_MAX 2
427
428 /**
429 * struct nft_base_chain - nf_tables base chain
430 *
431 * @ops: netfilter hook ops
432 * @type: chain type
433 * @policy: default policy
434 * @stats: per-cpu chain stats
435 * @chain: the chain
436 */
437 struct nft_base_chain {
438 struct nf_hook_ops ops[NFT_HOOK_OPS_MAX];
439 struct nf_chain_type *type;
440 u8 policy;
441 struct nft_stats __percpu *stats;
442 struct nft_chain chain;
443 };
444
445 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
446 {
447 return container_of(chain, struct nft_base_chain, chain);
448 }
449
450 unsigned int nft_do_chain_pktinfo(struct nft_pktinfo *pkt,
451 const struct nf_hook_ops *ops);
452
453 /**
454 * struct nft_table - nf_tables table
455 *
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
463 */
464 struct nft_table {
465 struct list_head list;
466 struct list_head chains;
467 struct list_head sets;
468 u64 hgenerator;
469 u32 use;
470 u16 flags;
471 char name[];
472 };
473
474 /**
475 * struct nft_af_info - nf_tables address family info
476 *
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
485 */
486 struct nft_af_info {
487 struct list_head list;
488 int family;
489 unsigned int nhooks;
490 struct module *owner;
491 struct list_head tables;
492 unsigned int nops;
493 void (*hook_ops_init)(struct nf_hook_ops *,
494 unsigned int);
495 nf_hookfn *hooks[NF_MAX_HOOKS];
496 };
497
498 int nft_register_afinfo(struct net *, struct nft_af_info *);
499 void nft_unregister_afinfo(struct nft_af_info *);
500
501 struct nf_chain_type {
502 unsigned int hook_mask;
503 const char *name;
504 enum nft_chain_type type;
505 nf_hookfn *fn[NF_MAX_HOOKS];
506 struct module *me;
507 int family;
508 };
509
510 int nft_register_chain_type(struct nf_chain_type *);
511 void nft_unregister_chain_type(struct nf_chain_type *);
512
513 int nft_register_expr(struct nft_expr_type *);
514 void nft_unregister_expr(struct nft_expr_type *);
515
516 #define MODULE_ALIAS_NFT_FAMILY(family) \
517 MODULE_ALIAS("nft-afinfo-" __stringify(family))
518
519 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
520 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
521
522 #define MODULE_ALIAS_NFT_EXPR(name) \
523 MODULE_ALIAS("nft-expr-" name)
524
525 #define MODULE_ALIAS_NFT_SET() \
526 MODULE_ALIAS("nft-set")
527
528 #endif /* _NET_NF_TABLES_H */
This page took 0.073079 seconds and 4 git commands to generate.