blob: 57c8ff7955dfbd109e8ac2c731d595539220b3d4 [file] [log] [blame]
Patrick McHardy96518512013-10-14 11:00:02 +02001#ifndef _NET_NF_TABLES_H
2#define _NET_NF_TABLES_H
3
4#include <linux/list.h>
5#include <linux/netfilter.h>
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02006#include <linux/netfilter/x_tables.h>
Patrick McHardy96518512013-10-14 11:00:02 +02007#include <linux/netfilter/nf_tables.h>
8#include <net/netlink.h>
9
Patrick McHardy20a69342013-10-11 12:06:22 +020010#define NFT_JUMP_STACK_SIZE 16
11
Patrick McHardy96518512013-10-14 11:00:02 +020012struct nft_pktinfo {
13 struct sk_buff *skb;
14 const struct net_device *in;
15 const struct net_device *out;
Patrick McHardyc9484872014-01-03 12:16:14 +000016 const struct nf_hook_ops *ops;
Patrick McHardy96518512013-10-14 11:00:02 +020017 u8 nhoff;
18 u8 thoff;
Patrick McHardy4566bf22014-01-03 12:16:18 +000019 u8 tprot;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +020020 /* for x_tables compatibility */
21 struct xt_action_param xt;
Patrick McHardy96518512013-10-14 11:00:02 +020022};
23
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +020024static 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;
Patrick McHardyc9484872014-01-03 12:16:14 +000033 pkt->ops = ops;
34 pkt->xt.hooknum = ops->hooknum;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +020035 pkt->xt.family = ops->pf;
36}
37
Patrick McHardy96518512013-10-14 11:00:02 +020038struct 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
48static 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
55static 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
63static 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/**
Patrick McHardy20a69342013-10-11 12:06:22 +020071 * struct nft_ctx - nf_tables rule/set context
Patrick McHardy96518512013-10-14 11:00:02 +020072 *
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020073 * @net: net namespace
Patrick McHardy20a69342013-10-11 12:06:22 +020074 * @skb: netlink skb
75 * @nlh: netlink message header
Patrick McHardy96518512013-10-14 11:00:02 +020076 * @afi: address family info
77 * @table: the table the chain is contained in
78 * @chain: the chain the rule is contained in
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +020079 * @nla: netlink attributes
Patrick McHardy96518512013-10-14 11:00:02 +020080 */
81struct nft_ctx {
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020082 struct net *net;
Patrick McHardy20a69342013-10-11 12:06:22 +020083 const struct sk_buff *skb;
84 const struct nlmsghdr *nlh;
Patrick McHardy96518512013-10-14 11:00:02 +020085 const struct nft_af_info *afi;
86 const struct nft_table *table;
87 const struct nft_chain *chain;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +020088 const struct nlattr * const *nla;
Patrick McHardy96518512013-10-14 11:00:02 +020089};
90
Patrick McHardy96518512013-10-14 11:00:02 +020091struct nft_data_desc {
92 enum nft_data_types type;
93 unsigned int len;
94};
95
Joe Perches5eccdfa2013-10-19 22:05:31 -070096int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
97 struct nft_data_desc *desc, const struct nlattr *nla);
98void nft_data_uninit(const struct nft_data *data, enum nft_data_types type);
99int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
100 enum nft_data_types type, unsigned int len);
Patrick McHardy96518512013-10-14 11:00:02 +0200101
102static 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
Patrick McHardy20a69342013-10-11 12:06:22 +0200107static 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
Joe Perches5eccdfa2013-10-19 22:05:31 -0700112int nft_validate_input_register(enum nft_registers reg);
113int nft_validate_output_register(enum nft_registers reg);
114int 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);
Patrick McHardy96518512013-10-14 11:00:02 +0200117
118/**
Patrick McHardy20a69342013-10-11 12:06:22 +0200119 * 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 */
129struct nft_set_elem {
130 void *cookie;
131 struct nft_data key;
132 struct nft_data data;
133 u32 flags;
134};
135
136struct nft_set;
137struct 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 */
161struct 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
Joe Perches5eccdfa2013-10-19 22:05:31 -0700185int nft_register_set(struct nft_set_ops *ops);
186void nft_unregister_set(struct nft_set_ops *ops);
Patrick McHardy20a69342013-10-11 12:06:22 +0200187
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 */
202struct 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
217static inline void *nft_set_priv(const struct nft_set *set)
218{
219 return (void *)set->data;
220}
221
Joe Perches5eccdfa2013-10-19 22:05:31 -0700222struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
223 const struct nlattr *nla);
Patrick McHardy20a69342013-10-11 12:06:22 +0200224
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 */
234struct nft_set_binding {
235 struct list_head list;
236 const struct nft_chain *chain;
237};
238
Joe Perches5eccdfa2013-10-19 22:05:31 -0700239int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
240 struct nft_set_binding *binding);
241void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
242 struct nft_set_binding *binding);
Patrick McHardy20a69342013-10-11 12:06:22 +0200243
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200244
Patrick McHardy20a69342013-10-11 12:06:22 +0200245/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200246 * struct nft_expr_type - nf_tables expression type
Patrick McHardy96518512013-10-14 11:00:02 +0200247 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200248 * @select_ops: function to select nft_expr_ops
249 * @ops: default ops, used when no select_ops functions is present
Patrick McHardy96518512013-10-14 11:00:02 +0200250 * @list: used internally
251 * @name: Identifier
252 * @owner: module reference
253 * @policy: netlink attribute policy
254 * @maxattr: highest netlink attribute number
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200255 */
256struct nft_expr_type {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200257 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
258 const struct nlattr * const tb[]);
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200259 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
Patrick McHardy96518512013-10-14 11:00:02 +0200271 * @size: full expression size, including private data size
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200272 * @init: initialization function
273 * @destroy: destruction function
274 * @dump: function to dump parameters
275 * @type: expression type
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200276 * @validate: validate expression, called during loop detection
277 * @data: extra data to attach to this expression operation
Patrick McHardy96518512013-10-14 11:00:02 +0200278 */
279struct nft_expr;
280struct 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);
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200284 unsigned int size;
285
Patrick McHardy96518512013-10-14 11:00:02 +0200286 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);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200292 int (*validate)(const struct nft_ctx *ctx,
293 const struct nft_expr *expr,
294 const struct nft_data **data);
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200295 const struct nft_expr_type *type;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200296 void *data;
Patrick McHardy96518512013-10-14 11:00:02 +0200297};
298
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200299#define NFT_EXPR_MAXATTR 16
Patrick McHardy96518512013-10-14 11:00:02 +0200300#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 */
309struct nft_expr {
310 const struct nft_expr_ops *ops;
311 unsigned char data[];
312};
313
314static 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
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +0200325 * @genmask: generation mask
Patrick McHardy96518512013-10-14 11:00:02 +0200326 * @dlen: length of expression data
327 * @data: expression data
328 */
329struct nft_rule {
330 struct list_head list;
331 struct rcu_head rcu_head;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +0200332 u64 handle:46,
333 genmask:2,
Patrick McHardy96518512013-10-14 11:00:02 +0200334 dlen:16;
335 unsigned char data[]
336 __attribute__((aligned(__alignof__(struct nft_expr))));
337};
338
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +0200339/**
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 */
349struct 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
Patrick McHardy96518512013-10-14 11:00:02 +0200358static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
359{
360 return (struct nft_expr *)&rule->data[0];
361}
362
363static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
364{
365 return ((void *)expr) + expr->ops->size;
366}
367
368static 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
383enum nft_chain_flags {
384 NFT_BASE_CHAIN = 0x1,
Patrick McHardy96518512013-10-14 11:00:02 +0200385};
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
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +0200393 * @net: net namespace that this chain belongs to
Pablo Neira Ayusob5bc89b2013-10-10 16:49:19 +0200394 * @table: table that this chain belongs to
Patrick McHardy96518512013-10-14 11:00:02 +0200395 * @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 */
401struct nft_chain {
402 struct list_head rules;
403 struct list_head list;
404 struct rcu_head rcu_head;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +0200405 struct net *net;
Pablo Neira Ayusob5bc89b2013-10-10 16:49:19 +0200406 struct nft_table *table;
Patrick McHardy96518512013-10-14 11:00:02 +0200407 u64 handle;
408 u8 flags;
409 u16 use;
410 u16 level;
411 char name[NFT_CHAIN_MAXNAMELEN];
412};
413
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200414enum 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
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200421struct nft_stats {
422 u64 bytes;
423 u64 pkts;
424};
425
Patrick McHardy115a60b2014-01-03 12:16:15 +0000426#define NFT_HOOK_OPS_MAX 2
427
Patrick McHardy96518512013-10-14 11:00:02 +0200428/**
429 * struct nft_base_chain - nf_tables base chain
430 *
431 * @ops: netfilter hook ops
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200432 * @type: chain type
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200433 * @policy: default policy
434 * @stats: per-cpu chain stats
Patrick McHardy96518512013-10-14 11:00:02 +0200435 * @chain: the chain
436 */
437struct nft_base_chain {
Patrick McHardy115a60b2014-01-03 12:16:15 +0000438 struct nf_hook_ops ops[NFT_HOOK_OPS_MAX];
Patrick McHardy2a37d752014-01-09 18:42:37 +0000439 const struct nf_chain_type *type;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200440 u8 policy;
441 struct nft_stats __percpu *stats;
Patrick McHardy96518512013-10-14 11:00:02 +0200442 struct nft_chain chain;
443};
444
445static 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
Patrick McHardy3876d222014-01-09 18:42:43 +0000450unsigned int nft_do_chain(struct nft_pktinfo *pkt,
451 const struct nf_hook_ops *ops);
Patrick McHardy96518512013-10-14 11:00:02 +0200452
Patrick McHardy96518512013-10-14 11:00:02 +0200453/**
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 */
464struct 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
Patrick McHardy115a60b2014-01-03 12:16:15 +0000482 * @nops: number of hook ops in this family
483 * @hook_ops_init: initialization function for chain hook ops
Patrick McHardy96518512013-10-14 11:00:02 +0200484 * @hooks: hookfn overrides for packet validation
485 */
486struct nft_af_info {
487 struct list_head list;
488 int family;
489 unsigned int nhooks;
490 struct module *owner;
491 struct list_head tables;
Patrick McHardy115a60b2014-01-03 12:16:15 +0000492 unsigned int nops;
493 void (*hook_ops_init)(struct nf_hook_ops *,
494 unsigned int);
Patrick McHardy96518512013-10-14 11:00:02 +0200495 nf_hookfn *hooks[NF_MAX_HOOKS];
496};
497
Joe Perches5eccdfa2013-10-19 22:05:31 -0700498int nft_register_afinfo(struct net *, struct nft_af_info *);
499void nft_unregister_afinfo(struct nft_af_info *);
Patrick McHardy96518512013-10-14 11:00:02 +0200500
Patrick McHardyfa2c1de2014-01-09 18:42:38 +0000501/**
502 * struct nf_chain_type - nf_tables chain type info
503 *
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
510 */
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200511struct nf_chain_type {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +0000512 const char *name;
513 enum nft_chain_type type;
514 int family;
515 struct module *owner;
516 unsigned int hook_mask;
517 nf_hookfn *hooks[NF_MAX_HOOKS];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200518};
519
Patrick McHardy2a37d752014-01-09 18:42:37 +0000520int nft_register_chain_type(const struct nf_chain_type *);
521void nft_unregister_chain_type(const struct nf_chain_type *);
Patrick McHardy96518512013-10-14 11:00:02 +0200522
Joe Perches5eccdfa2013-10-19 22:05:31 -0700523int nft_register_expr(struct nft_expr_type *);
524void nft_unregister_expr(struct nft_expr_type *);
Patrick McHardy96518512013-10-14 11:00:02 +0200525
526#define MODULE_ALIAS_NFT_FAMILY(family) \
527 MODULE_ALIAS("nft-afinfo-" __stringify(family))
528
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200529#define MODULE_ALIAS_NFT_CHAIN(family, name) \
530 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
Patrick McHardy96518512013-10-14 11:00:02 +0200531
532#define MODULE_ALIAS_NFT_EXPR(name) \
533 MODULE_ALIAS("nft-expr-" name)
534
Patrick McHardy20a69342013-10-11 12:06:22 +0200535#define MODULE_ALIAS_NFT_SET() \
536 MODULE_ALIAS("nft-set")
537
Patrick McHardy96518512013-10-14 11:00:02 +0200538#endif /* _NET_NF_TABLES_H */