blob: a785699329c9fa512e417f33c50182baa3cfe568 [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>
Patrick McHardy67a8fc22014-02-18 18:06:49 +00006#include <linux/netfilter/nfnetlink.h>
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02007#include <linux/netfilter/x_tables.h>
Patrick McHardy96518512013-10-14 11:00:02 +02008#include <linux/netfilter/nf_tables.h>
Eric Dumazetce355e22014-07-09 15:14:06 +02009#include <linux/u64_stats_sync.h>
Patrick McHardy96518512013-10-14 11:00:02 +020010#include <net/netlink.h>
11
Patrick McHardy20a69342013-10-11 12:06:22 +020012#define NFT_JUMP_STACK_SIZE 16
13
Patrick McHardy96518512013-10-14 11:00:02 +020014struct nft_pktinfo {
15 struct sk_buff *skb;
16 const struct net_device *in;
17 const struct net_device *out;
Patrick McHardyc9484872014-01-03 12:16:14 +000018 const struct nf_hook_ops *ops;
Patrick McHardy96518512013-10-14 11:00:02 +020019 u8 nhoff;
20 u8 thoff;
Patrick McHardy4566bf22014-01-03 12:16:18 +000021 u8 tprot;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +020022 /* for x_tables compatibility */
23 struct xt_action_param xt;
Patrick McHardy96518512013-10-14 11:00:02 +020024};
25
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +020026static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
27 const struct nf_hook_ops *ops,
28 struct sk_buff *skb,
29 const struct net_device *in,
30 const struct net_device *out)
31{
32 pkt->skb = skb;
33 pkt->in = pkt->xt.in = in;
34 pkt->out = pkt->xt.out = out;
Patrick McHardyc9484872014-01-03 12:16:14 +000035 pkt->ops = ops;
36 pkt->xt.hooknum = ops->hooknum;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +020037 pkt->xt.family = ops->pf;
38}
39
Patrick McHardy96518512013-10-14 11:00:02 +020040struct nft_data {
41 union {
42 u32 data[4];
43 struct {
44 u32 verdict;
45 struct nft_chain *chain;
46 };
47 };
48} __attribute__((aligned(__alignof__(u64))));
49
50static inline int nft_data_cmp(const struct nft_data *d1,
51 const struct nft_data *d2,
52 unsigned int len)
53{
54 return memcmp(d1->data, d2->data, len);
55}
56
57static inline void nft_data_copy(struct nft_data *dst,
58 const struct nft_data *src)
59{
60 BUILD_BUG_ON(__alignof__(*dst) != __alignof__(u64));
61 *(u64 *)&dst->data[0] = *(u64 *)&src->data[0];
62 *(u64 *)&dst->data[2] = *(u64 *)&src->data[2];
63}
64
65static inline void nft_data_debug(const struct nft_data *data)
66{
67 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
68 data->data[0], data->data[1],
69 data->data[2], data->data[3]);
70}
71
72/**
Patrick McHardy20a69342013-10-11 12:06:22 +020073 * struct nft_ctx - nf_tables rule/set context
Patrick McHardy96518512013-10-14 11:00:02 +020074 *
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020075 * @net: net namespace
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
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +020080 * @portid: netlink portID of the original message
81 * @seq: netlink sequence number
82 * @report: notify via unicast netlink message
Patrick McHardy96518512013-10-14 11:00:02 +020083 */
84struct nft_ctx {
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020085 struct net *net;
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +020086 struct nft_af_info *afi;
87 struct nft_table *table;
88 struct nft_chain *chain;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +020089 const struct nlattr * const *nla;
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +020090 u32 portid;
91 u32 seq;
92 bool report;
Patrick McHardy96518512013-10-14 11:00:02 +020093};
94
Patrick McHardy96518512013-10-14 11:00:02 +020095struct nft_data_desc {
96 enum nft_data_types type;
97 unsigned int len;
98};
99
Joe Perches5eccdfa2013-10-19 22:05:31 -0700100int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
101 struct nft_data_desc *desc, const struct nlattr *nla);
102void nft_data_uninit(const struct nft_data *data, enum nft_data_types type);
103int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
104 enum nft_data_types type, unsigned int len);
Patrick McHardy96518512013-10-14 11:00:02 +0200105
106static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
107{
108 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
109}
110
Patrick McHardy20a69342013-10-11 12:06:22 +0200111static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
112{
113 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1;
114}
115
Joe Perches5eccdfa2013-10-19 22:05:31 -0700116int nft_validate_input_register(enum nft_registers reg);
117int nft_validate_output_register(enum nft_registers reg);
118int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
119 const struct nft_data *data,
120 enum nft_data_types type);
Patrick McHardy96518512013-10-14 11:00:02 +0200121
Patrick McHardy86f1ec32015-03-03 20:04:20 +0000122
123/**
124 * struct nft_userdata - user defined data associated with an object
125 *
126 * @len: length of the data
127 * @data: content
128 *
129 * The presence of user data is indicated in an object specific fashion,
130 * so a length of zero can't occur and the value "len" indicates data
131 * of length len + 1.
132 */
133struct nft_userdata {
134 u8 len;
135 unsigned char data[0];
136};
137
Patrick McHardy96518512013-10-14 11:00:02 +0200138/**
Patrick McHardy20a69342013-10-11 12:06:22 +0200139 * struct nft_set_elem - generic representation of set elements
140 *
Patrick McHardy20a69342013-10-11 12:06:22 +0200141 * @key: element key
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000142 * @priv: element private data and extensions
Patrick McHardy20a69342013-10-11 12:06:22 +0200143 */
144struct nft_set_elem {
Patrick McHardy20a69342013-10-11 12:06:22 +0200145 struct nft_data key;
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000146 void *priv;
Patrick McHardy20a69342013-10-11 12:06:22 +0200147};
148
149struct nft_set;
150struct nft_set_iter {
151 unsigned int count;
152 unsigned int skip;
153 int err;
154 int (*fn)(const struct nft_ctx *ctx,
155 const struct nft_set *set,
156 const struct nft_set_iter *iter,
157 const struct nft_set_elem *elem);
158};
159
160/**
Patrick McHardyc50b9602014-03-28 10:19:47 +0000161 * struct nft_set_desc - description of set elements
162 *
163 * @klen: key length
164 * @dlen: data length
165 * @size: number of set elements
166 */
167struct nft_set_desc {
168 unsigned int klen;
169 unsigned int dlen;
170 unsigned int size;
171};
172
173/**
174 * enum nft_set_class - performance class
175 *
176 * @NFT_LOOKUP_O_1: constant, O(1)
177 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
178 * @NFT_LOOKUP_O_N: linear, O(N)
179 */
180enum nft_set_class {
181 NFT_SET_CLASS_O_1,
182 NFT_SET_CLASS_O_LOG_N,
183 NFT_SET_CLASS_O_N,
184};
185
186/**
187 * struct nft_set_estimate - estimation of memory and performance
188 * characteristics
189 *
190 * @size: required memory
191 * @class: lookup performance class
192 */
193struct nft_set_estimate {
194 unsigned int size;
195 enum nft_set_class class;
196};
197
Patrick McHardyb2832dd2015-03-25 14:08:48 +0000198struct nft_set_ext;
199
Patrick McHardyc50b9602014-03-28 10:19:47 +0000200/**
Patrick McHardy20a69342013-10-11 12:06:22 +0200201 * struct nft_set_ops - nf_tables set operations
202 *
203 * @lookup: look up an element within the set
204 * @insert: insert new element into set
Patrick McHardycc02e452015-03-25 14:08:50 +0000205 * @activate: activate new element in the next generation
206 * @deactivate: deactivate element in the next generation
Patrick McHardy20a69342013-10-11 12:06:22 +0200207 * @remove: remove element from set
208 * @walk: iterate over all set elemeennts
209 * @privsize: function to return size of set private data
210 * @init: initialize private data of new set instance
211 * @destroy: destroy private data of set instance
212 * @list: nf_tables_set_ops list node
213 * @owner: module reference
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000214 * @elemsize: element private size
Patrick McHardy20a69342013-10-11 12:06:22 +0200215 * @features: features supported by the implementation
216 */
217struct nft_set_ops {
218 bool (*lookup)(const struct nft_set *set,
219 const struct nft_data *key,
Patrick McHardyb2832dd2015-03-25 14:08:48 +0000220 const struct nft_set_ext **ext);
Patrick McHardy20a69342013-10-11 12:06:22 +0200221 int (*insert)(const struct nft_set *set,
222 const struct nft_set_elem *elem);
Patrick McHardycc02e452015-03-25 14:08:50 +0000223 void (*activate)(const struct nft_set *set,
224 const struct nft_set_elem *elem);
225 void * (*deactivate)(const struct nft_set *set,
226 const struct nft_set_elem *elem);
Patrick McHardy20a69342013-10-11 12:06:22 +0200227 void (*remove)(const struct nft_set *set,
228 const struct nft_set_elem *elem);
229 void (*walk)(const struct nft_ctx *ctx,
230 const struct nft_set *set,
231 struct nft_set_iter *iter);
232
233 unsigned int (*privsize)(const struct nlattr * const nla[]);
Patrick McHardyc50b9602014-03-28 10:19:47 +0000234 bool (*estimate)(const struct nft_set_desc *desc,
235 u32 features,
236 struct nft_set_estimate *est);
Patrick McHardy20a69342013-10-11 12:06:22 +0200237 int (*init)(const struct nft_set *set,
Patrick McHardyc50b9602014-03-28 10:19:47 +0000238 const struct nft_set_desc *desc,
Patrick McHardy20a69342013-10-11 12:06:22 +0200239 const struct nlattr * const nla[]);
240 void (*destroy)(const struct nft_set *set);
241
242 struct list_head list;
243 struct module *owner;
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000244 unsigned int elemsize;
Patrick McHardy20a69342013-10-11 12:06:22 +0200245 u32 features;
246};
247
Joe Perches5eccdfa2013-10-19 22:05:31 -0700248int nft_register_set(struct nft_set_ops *ops);
249void nft_unregister_set(struct nft_set_ops *ops);
Patrick McHardy20a69342013-10-11 12:06:22 +0200250
251/**
252 * struct nft_set - nf_tables set instance
253 *
254 * @list: table set list node
255 * @bindings: list of set bindings
256 * @name: name of the set
257 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
258 * @dtype: data type (verdict or numeric type defined by userspace)
Patrick McHardyc50b9602014-03-28 10:19:47 +0000259 * @size: maximum set size
260 * @nelems: number of elements
Patrick McHardy761da292015-03-26 12:39:36 +0000261 * @timeout: default timeout value in msecs
262 * @gc_int: garbage collection interval in msecs
Arturo Borrero9363dc42014-09-23 13:30:41 +0200263 * @policy: set parameterization (see enum nft_set_policies)
Patrick McHardy20a69342013-10-11 12:06:22 +0200264 * @ops: set ops
Patrick McHardycc02e452015-03-25 14:08:50 +0000265 * @pnet: network namespace
Patrick McHardy20a69342013-10-11 12:06:22 +0200266 * @flags: set flags
267 * @klen: key length
268 * @dlen: data length
269 * @data: private set data
270 */
271struct nft_set {
272 struct list_head list;
273 struct list_head bindings;
274 char name[IFNAMSIZ];
275 u32 ktype;
276 u32 dtype;
Patrick McHardyc50b9602014-03-28 10:19:47 +0000277 u32 size;
278 u32 nelems;
Patrick McHardy761da292015-03-26 12:39:36 +0000279 u64 timeout;
280 u32 gc_int;
Arturo Borrero9363dc42014-09-23 13:30:41 +0200281 u16 policy;
Patrick McHardy20a69342013-10-11 12:06:22 +0200282 /* runtime data below here */
283 const struct nft_set_ops *ops ____cacheline_aligned;
Patrick McHardycc02e452015-03-25 14:08:50 +0000284 possible_net_t pnet;
Patrick McHardy20a69342013-10-11 12:06:22 +0200285 u16 flags;
286 u8 klen;
287 u8 dlen;
288 unsigned char data[]
289 __attribute__((aligned(__alignof__(u64))));
290};
291
292static inline void *nft_set_priv(const struct nft_set *set)
293{
294 return (void *)set->data;
295}
296
Patrick McHardy9d098292015-03-26 12:39:40 +0000297static inline struct nft_set *nft_set_container_of(const void *priv)
298{
299 return (void *)priv - offsetof(struct nft_set, data);
300}
301
Joe Perches5eccdfa2013-10-19 22:05:31 -0700302struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
303 const struct nlattr *nla);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +0200304struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
305 const struct nlattr *nla);
Patrick McHardy20a69342013-10-11 12:06:22 +0200306
Patrick McHardy761da292015-03-26 12:39:36 +0000307static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
308{
309 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
310}
311
Patrick McHardy20a69342013-10-11 12:06:22 +0200312/**
313 * struct nft_set_binding - nf_tables set binding
314 *
315 * @list: set bindings list node
316 * @chain: chain containing the rule bound to the set
317 *
318 * A set binding contains all information necessary for validation
319 * of new elements added to a bound set.
320 */
321struct nft_set_binding {
322 struct list_head list;
323 const struct nft_chain *chain;
324};
325
Joe Perches5eccdfa2013-10-19 22:05:31 -0700326int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
327 struct nft_set_binding *binding);
328void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
329 struct nft_set_binding *binding);
Patrick McHardy20a69342013-10-11 12:06:22 +0200330
Patrick McHardy3ac4c072015-03-25 13:07:49 +0000331/**
332 * enum nft_set_extensions - set extension type IDs
333 *
334 * @NFT_SET_EXT_KEY: element key
335 * @NFT_SET_EXT_DATA: mapping data
336 * @NFT_SET_EXT_FLAGS: element flags
Patrick McHardyc3e1b002015-03-26 12:39:37 +0000337 * @NFT_SET_EXT_TIMEOUT: element timeout
338 * @NFT_SET_EXT_EXPIRATION: element expiration time
Patrick McHardy3ac4c072015-03-25 13:07:49 +0000339 * @NFT_SET_EXT_NUM: number of extension types
340 */
341enum nft_set_extensions {
342 NFT_SET_EXT_KEY,
343 NFT_SET_EXT_DATA,
344 NFT_SET_EXT_FLAGS,
Patrick McHardyc3e1b002015-03-26 12:39:37 +0000345 NFT_SET_EXT_TIMEOUT,
346 NFT_SET_EXT_EXPIRATION,
Patrick McHardy3ac4c072015-03-25 13:07:49 +0000347 NFT_SET_EXT_NUM
348};
349
350/**
351 * struct nft_set_ext_type - set extension type
352 *
353 * @len: fixed part length of the extension
354 * @align: alignment requirements of the extension
355 */
356struct nft_set_ext_type {
357 u8 len;
358 u8 align;
359};
360
361extern const struct nft_set_ext_type nft_set_ext_types[];
362
363/**
364 * struct nft_set_ext_tmpl - set extension template
365 *
366 * @len: length of extension area
367 * @offset: offsets of individual extension types
368 */
369struct nft_set_ext_tmpl {
370 u16 len;
371 u8 offset[NFT_SET_EXT_NUM];
372};
373
374/**
375 * struct nft_set_ext - set extensions
376 *
Patrick McHardycc02e452015-03-25 14:08:50 +0000377 * @genmask: generation mask
Patrick McHardy3ac4c072015-03-25 13:07:49 +0000378 * @offset: offsets of individual extension types
379 * @data: beginning of extension data
380 */
381struct nft_set_ext {
Patrick McHardycc02e452015-03-25 14:08:50 +0000382 u8 genmask;
Patrick McHardy3ac4c072015-03-25 13:07:49 +0000383 u8 offset[NFT_SET_EXT_NUM];
384 char data[0];
385};
386
387static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
388{
389 memset(tmpl, 0, sizeof(*tmpl));
390 tmpl->len = sizeof(struct nft_set_ext);
391}
392
393static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
394 unsigned int len)
395{
396 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
397 BUG_ON(tmpl->len > U8_MAX);
398 tmpl->offset[id] = tmpl->len;
399 tmpl->len += nft_set_ext_types[id].len + len;
400}
401
402static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
403{
404 nft_set_ext_add_length(tmpl, id, 0);
405}
406
407static inline void nft_set_ext_init(struct nft_set_ext *ext,
408 const struct nft_set_ext_tmpl *tmpl)
409{
410 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
411}
412
413static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
414{
415 return !!ext->offset[id];
416}
417
418static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
419{
420 return ext && __nft_set_ext_exists(ext, id);
421}
422
423static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
424{
425 return (void *)ext + ext->offset[id];
426}
427
428static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
429{
430 return nft_set_ext(ext, NFT_SET_EXT_KEY);
431}
432
433static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
434{
435 return nft_set_ext(ext, NFT_SET_EXT_DATA);
436}
437
438static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
439{
440 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
441}
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200442
Patrick McHardyc3e1b002015-03-26 12:39:37 +0000443static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
444{
445 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
446}
447
448static inline unsigned long *nft_set_ext_expiration(const struct nft_set_ext *ext)
449{
450 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
451}
452
453static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
454{
455 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
456 time_is_before_eq_jiffies(*nft_set_ext_expiration(ext));
457}
458
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000459static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
460 void *elem)
461{
462 return elem + set->ops->elemsize;
463}
464
Patrick McHardy61edafb2015-03-25 14:08:47 +0000465void nft_set_elem_destroy(const struct nft_set *set, void *elem);
466
Patrick McHardy20a69342013-10-11 12:06:22 +0200467/**
Patrick McHardycfed7e12015-03-26 12:39:38 +0000468 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
469 *
470 * @rcu: rcu head
471 * @set: set the elements belong to
472 * @cnt: count of elements
473 */
474struct nft_set_gc_batch_head {
475 struct rcu_head rcu;
476 const struct nft_set *set;
477 unsigned int cnt;
478};
479
480#define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
481 sizeof(struct nft_set_gc_batch_head)) / \
482 sizeof(void *))
483
484/**
485 * struct nft_set_gc_batch - nf_tables set garbage collection batch
486 *
487 * @head: GC batch head
488 * @elems: garbage collection elements
489 */
490struct nft_set_gc_batch {
491 struct nft_set_gc_batch_head head;
492 void *elems[NFT_SET_GC_BATCH_SIZE];
493};
494
495struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
496 gfp_t gfp);
497void nft_set_gc_batch_release(struct rcu_head *rcu);
498
499static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
500{
501 if (gcb != NULL)
502 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
503}
504
505static inline struct nft_set_gc_batch *
506nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
507 gfp_t gfp)
508{
509 if (gcb != NULL) {
510 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
511 return gcb;
512 nft_set_gc_batch_complete(gcb);
513 }
514 return nft_set_gc_batch_alloc(set, gfp);
515}
516
517static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
518 void *elem)
519{
520 gcb->elems[gcb->head.cnt++] = elem;
521}
522
523/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200524 * struct nft_expr_type - nf_tables expression type
Patrick McHardy96518512013-10-14 11:00:02 +0200525 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200526 * @select_ops: function to select nft_expr_ops
527 * @ops: default ops, used when no select_ops functions is present
Patrick McHardy96518512013-10-14 11:00:02 +0200528 * @list: used internally
529 * @name: Identifier
530 * @owner: module reference
531 * @policy: netlink attribute policy
532 * @maxattr: highest netlink attribute number
Patrick McHardy64d46802014-02-05 15:03:37 +0000533 * @family: address family for AF-specific types
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200534 */
535struct nft_expr_type {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200536 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
537 const struct nlattr * const tb[]);
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200538 const struct nft_expr_ops *ops;
539 struct list_head list;
540 const char *name;
541 struct module *owner;
542 const struct nla_policy *policy;
543 unsigned int maxattr;
Patrick McHardy64d46802014-02-05 15:03:37 +0000544 u8 family;
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200545};
546
547/**
548 * struct nft_expr_ops - nf_tables expression operations
549 *
550 * @eval: Expression evaluation function
Patrick McHardy96518512013-10-14 11:00:02 +0200551 * @size: full expression size, including private data size
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200552 * @init: initialization function
553 * @destroy: destruction function
554 * @dump: function to dump parameters
555 * @type: expression type
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200556 * @validate: validate expression, called during loop detection
557 * @data: extra data to attach to this expression operation
Patrick McHardy96518512013-10-14 11:00:02 +0200558 */
559struct nft_expr;
560struct nft_expr_ops {
561 void (*eval)(const struct nft_expr *expr,
562 struct nft_data data[NFT_REG_MAX + 1],
563 const struct nft_pktinfo *pkt);
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200564 unsigned int size;
565
Patrick McHardy96518512013-10-14 11:00:02 +0200566 int (*init)(const struct nft_ctx *ctx,
567 const struct nft_expr *expr,
568 const struct nlattr * const tb[]);
Patrick McHardy62472bc2014-03-07 19:08:30 +0100569 void (*destroy)(const struct nft_ctx *ctx,
570 const struct nft_expr *expr);
Patrick McHardy96518512013-10-14 11:00:02 +0200571 int (*dump)(struct sk_buff *skb,
572 const struct nft_expr *expr);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200573 int (*validate)(const struct nft_ctx *ctx,
574 const struct nft_expr *expr,
575 const struct nft_data **data);
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200576 const struct nft_expr_type *type;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200577 void *data;
Patrick McHardy96518512013-10-14 11:00:02 +0200578};
579
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200580#define NFT_EXPR_MAXATTR 16
Patrick McHardy96518512013-10-14 11:00:02 +0200581#define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
582 ALIGN(size, __alignof__(struct nft_expr)))
583
584/**
585 * struct nft_expr - nf_tables expression
586 *
587 * @ops: expression ops
588 * @data: expression private data
589 */
590struct nft_expr {
591 const struct nft_expr_ops *ops;
592 unsigned char data[];
593};
594
595static inline void *nft_expr_priv(const struct nft_expr *expr)
596{
597 return (void *)expr->data;
598}
599
600/**
601 * struct nft_rule - nf_tables rule
602 *
603 * @list: used internally
Patrick McHardy96518512013-10-14 11:00:02 +0200604 * @handle: rule handle
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +0200605 * @genmask: generation mask
Patrick McHardy96518512013-10-14 11:00:02 +0200606 * @dlen: length of expression data
Patrick McHardy86f1ec32015-03-03 20:04:20 +0000607 * @udata: user data is appended to the rule
Patrick McHardy96518512013-10-14 11:00:02 +0200608 * @data: expression data
609 */
610struct nft_rule {
611 struct list_head list;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +0100612 u64 handle:42,
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +0200613 genmask:2,
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +0100614 dlen:12,
Patrick McHardy86f1ec32015-03-03 20:04:20 +0000615 udata:1;
Patrick McHardy96518512013-10-14 11:00:02 +0200616 unsigned char data[]
617 __attribute__((aligned(__alignof__(struct nft_expr))));
618};
619
620static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
621{
622 return (struct nft_expr *)&rule->data[0];
623}
624
625static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
626{
627 return ((void *)expr) + expr->ops->size;
628}
629
630static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
631{
632 return (struct nft_expr *)&rule->data[rule->dlen];
633}
634
Patrick McHardy86f1ec32015-03-03 20:04:20 +0000635static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +0100636{
637 return (void *)&rule->data[rule->dlen];
638}
639
Patrick McHardy96518512013-10-14 11:00:02 +0200640/*
641 * The last pointer isn't really necessary, but the compiler isn't able to
642 * determine that the result of nft_expr_last() is always the same since it
643 * can't assume that the dlen value wasn't changed within calls in the loop.
644 */
645#define nft_rule_for_each_expr(expr, last, rule) \
646 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
647 (expr) != (last); \
648 (expr) = nft_expr_next(expr))
649
650enum nft_chain_flags {
651 NFT_BASE_CHAIN = 0x1,
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +0200652 NFT_CHAIN_INACTIVE = 0x2,
Patrick McHardy96518512013-10-14 11:00:02 +0200653};
654
655/**
656 * struct nft_chain - nf_tables chain
657 *
658 * @rules: list of rules in the chain
659 * @list: used internally
Pablo Neira Ayusob5bc89b2013-10-10 16:49:19 +0200660 * @table: table that this chain belongs to
Patrick McHardy96518512013-10-14 11:00:02 +0200661 * @handle: chain handle
Patrick McHardy96518512013-10-14 11:00:02 +0200662 * @use: number of jump references to this chain
663 * @level: length of longest path to this chain
Pablo Neira Ayusoa0a73792014-06-10 10:53:01 +0200664 * @flags: bitmask of enum nft_chain_flags
Patrick McHardy96518512013-10-14 11:00:02 +0200665 * @name: name of the chain
666 */
667struct nft_chain {
668 struct list_head rules;
669 struct list_head list;
Pablo Neira Ayusob5bc89b2013-10-10 16:49:19 +0200670 struct nft_table *table;
Patrick McHardy96518512013-10-14 11:00:02 +0200671 u64 handle;
Pablo Neira Ayusoa0a73792014-06-10 10:53:01 +0200672 u32 use;
Patrick McHardy96518512013-10-14 11:00:02 +0200673 u16 level;
Pablo Neira Ayusoa0a73792014-06-10 10:53:01 +0200674 u8 flags;
Patrick McHardy96518512013-10-14 11:00:02 +0200675 char name[NFT_CHAIN_MAXNAMELEN];
676};
677
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200678enum nft_chain_type {
679 NFT_CHAIN_T_DEFAULT = 0,
680 NFT_CHAIN_T_ROUTE,
681 NFT_CHAIN_T_NAT,
682 NFT_CHAIN_T_MAX
683};
684
Patrick McHardy1a1e1a12015-03-03 20:10:06 +0000685/**
686 * struct nf_chain_type - nf_tables chain type info
687 *
688 * @name: name of the type
689 * @type: numeric identifier
690 * @family: address family
691 * @owner: module owner
692 * @hook_mask: mask of valid hooks
693 * @hooks: hookfn overrides
694 */
695struct nf_chain_type {
696 const char *name;
697 enum nft_chain_type type;
698 int family;
699 struct module *owner;
700 unsigned int hook_mask;
701 nf_hookfn *hooks[NF_MAX_HOOKS];
702};
703
Pablo Neira Ayuso7210e4e2014-10-13 19:50:22 +0200704int nft_chain_validate_dependency(const struct nft_chain *chain,
705 enum nft_chain_type type);
Pablo Neira Ayuso75e8d062015-01-14 15:33:57 +0100706int nft_chain_validate_hooks(const struct nft_chain *chain,
707 unsigned int hook_flags);
Pablo Neira Ayuso7210e4e2014-10-13 19:50:22 +0200708
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200709struct nft_stats {
Eric Dumazetce355e22014-07-09 15:14:06 +0200710 u64 bytes;
711 u64 pkts;
712 struct u64_stats_sync syncp;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200713};
714
Patrick McHardy115a60b12014-01-03 12:16:15 +0000715#define NFT_HOOK_OPS_MAX 2
716
Patrick McHardy96518512013-10-14 11:00:02 +0200717/**
718 * struct nft_base_chain - nf_tables base chain
719 *
720 * @ops: netfilter hook ops
Patrick McHardy5ebb3352015-03-21 15:19:15 +0000721 * @pnet: net namespace that this chain belongs to
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200722 * @type: chain type
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200723 * @policy: default policy
724 * @stats: per-cpu chain stats
Patrick McHardy96518512013-10-14 11:00:02 +0200725 * @chain: the chain
726 */
727struct nft_base_chain {
Patrick McHardy115a60b12014-01-03 12:16:15 +0000728 struct nf_hook_ops ops[NFT_HOOK_OPS_MAX];
Patrick McHardy5ebb3352015-03-21 15:19:15 +0000729 possible_net_t pnet;
Patrick McHardy2a37d752014-01-09 18:42:37 +0000730 const struct nf_chain_type *type;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200731 u8 policy;
732 struct nft_stats __percpu *stats;
Patrick McHardy96518512013-10-14 11:00:02 +0200733 struct nft_chain chain;
734};
735
736static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
737{
738 return container_of(chain, struct nft_base_chain, chain);
739}
740
Patrick McHardy3876d222014-01-09 18:42:43 +0000741unsigned int nft_do_chain(struct nft_pktinfo *pkt,
742 const struct nf_hook_ops *ops);
Patrick McHardy96518512013-10-14 11:00:02 +0200743
Patrick McHardy96518512013-10-14 11:00:02 +0200744/**
745 * struct nft_table - nf_tables table
746 *
747 * @list: used internally
748 * @chains: chains in the table
749 * @sets: sets in the table
750 * @hgenerator: handle generator state
751 * @use: number of chain references to this table
752 * @flags: table flag (see enum nft_table_flags)
753 * @name: name of the table
754 */
755struct nft_table {
756 struct list_head list;
757 struct list_head chains;
758 struct list_head sets;
759 u64 hgenerator;
760 u32 use;
761 u16 flags;
Pablo Neira Ayuso1cae5652015-03-05 15:05:36 +0100762 char name[NFT_TABLE_MAXNAMELEN];
Patrick McHardy96518512013-10-14 11:00:02 +0200763};
764
765/**
766 * struct nft_af_info - nf_tables address family info
767 *
768 * @list: used internally
769 * @family: address family
770 * @nhooks: number of hooks in this family
771 * @owner: module owner
772 * @tables: used internally
Patrick McHardy115a60b12014-01-03 12:16:15 +0000773 * @nops: number of hook ops in this family
774 * @hook_ops_init: initialization function for chain hook ops
Patrick McHardy96518512013-10-14 11:00:02 +0200775 * @hooks: hookfn overrides for packet validation
776 */
777struct nft_af_info {
778 struct list_head list;
779 int family;
780 unsigned int nhooks;
781 struct module *owner;
782 struct list_head tables;
Patrick McHardy115a60b12014-01-03 12:16:15 +0000783 unsigned int nops;
784 void (*hook_ops_init)(struct nf_hook_ops *,
785 unsigned int);
Patrick McHardy96518512013-10-14 11:00:02 +0200786 nf_hookfn *hooks[NF_MAX_HOOKS];
787};
788
Joe Perches5eccdfa2013-10-19 22:05:31 -0700789int nft_register_afinfo(struct net *, struct nft_af_info *);
790void nft_unregister_afinfo(struct nft_af_info *);
Patrick McHardy96518512013-10-14 11:00:02 +0200791
Patrick McHardy2a37d752014-01-09 18:42:37 +0000792int nft_register_chain_type(const struct nf_chain_type *);
793void nft_unregister_chain_type(const struct nf_chain_type *);
Patrick McHardy96518512013-10-14 11:00:02 +0200794
Joe Perches5eccdfa2013-10-19 22:05:31 -0700795int nft_register_expr(struct nft_expr_type *);
796void nft_unregister_expr(struct nft_expr_type *);
Patrick McHardy96518512013-10-14 11:00:02 +0200797
Patrick McHardy67a8fc22014-02-18 18:06:49 +0000798#define nft_dereference(p) \
799 nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
800
Patrick McHardy96518512013-10-14 11:00:02 +0200801#define MODULE_ALIAS_NFT_FAMILY(family) \
802 MODULE_ALIAS("nft-afinfo-" __stringify(family))
803
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200804#define MODULE_ALIAS_NFT_CHAIN(family, name) \
805 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
Patrick McHardy96518512013-10-14 11:00:02 +0200806
Patrick McHardy64d46802014-02-05 15:03:37 +0000807#define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
808 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
809
Patrick McHardy96518512013-10-14 11:00:02 +0200810#define MODULE_ALIAS_NFT_EXPR(name) \
811 MODULE_ALIAS("nft-expr-" name)
812
Patrick McHardy20a69342013-10-11 12:06:22 +0200813#define MODULE_ALIAS_NFT_SET() \
814 MODULE_ALIAS("nft-set")
815
Patrick McHardyea4bd992015-03-25 14:08:49 +0000816/*
817 * The gencursor defines two generations, the currently active and the
818 * next one. Objects contain a bitmask of 2 bits specifying the generations
819 * they're active in. A set bit means they're inactive in the generation
820 * represented by that bit.
821 *
822 * New objects start out as inactive in the current and active in the
823 * next generation. When committing the ruleset the bitmask is cleared,
824 * meaning they're active in all generations. When removing an object,
825 * it is set inactive in the next generation. After committing the ruleset,
826 * the objects are removed.
827 */
828static inline unsigned int nft_gencursor_next(const struct net *net)
829{
830 return net->nft.gencursor + 1 == 1 ? 1 : 0;
831}
832
833static inline u8 nft_genmask_next(const struct net *net)
834{
835 return 1 << nft_gencursor_next(net);
836}
837
838static inline u8 nft_genmask_cur(const struct net *net)
839{
840 /* Use ACCESS_ONCE() to prevent refetching the value for atomicity */
841 return 1 << ACCESS_ONCE(net->nft.gencursor);
842}
843
Patrick McHardycc02e452015-03-25 14:08:50 +0000844/*
845 * Set element transaction helpers
846 */
847
848static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
849 u8 genmask)
850{
851 return !(ext->genmask & genmask);
852}
853
854static inline void nft_set_elem_change_active(const struct nft_set *set,
855 struct nft_set_ext *ext)
856{
857 ext->genmask ^= nft_genmask_next(read_pnet(&set->pnet));
858}
859
Patrick McHardy69086652015-03-26 12:39:39 +0000860/*
861 * We use a free bit in the genmask field to indicate the element
862 * is busy, meaning it is currently being processed either by
863 * the netlink API or GC.
864 *
865 * Even though the genmask is only a single byte wide, this works
866 * because the extension structure if fully constant once initialized,
867 * so there are no non-atomic write accesses unless it is already
868 * marked busy.
869 */
870#define NFT_SET_ELEM_BUSY_MASK (1 << 2)
871
872#if defined(__LITTLE_ENDIAN_BITFIELD)
873#define NFT_SET_ELEM_BUSY_BIT 2
874#elif defined(__BIG_ENDIAN_BITFIELD)
875#define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
876#else
877#error
878#endif
879
880static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
881{
882 unsigned long *word = (unsigned long *)ext;
883
884 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
885 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
886}
887
888static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
889{
890 unsigned long *word = (unsigned long *)ext;
891
892 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
893}
894
Patrick McHardy1a1e1a12015-03-03 20:10:06 +0000895/**
896 * struct nft_trans - nf_tables object update in transaction
897 *
898 * @list: used internally
899 * @msg_type: message type
900 * @ctx: transaction context
901 * @data: internal information related to the transaction
902 */
903struct nft_trans {
904 struct list_head list;
905 int msg_type;
906 struct nft_ctx ctx;
907 char data[0];
908};
909
910struct nft_trans_rule {
911 struct nft_rule *rule;
912};
913
914#define nft_trans_rule(trans) \
915 (((struct nft_trans_rule *)trans->data)->rule)
916
917struct nft_trans_set {
918 struct nft_set *set;
919 u32 set_id;
920};
921
922#define nft_trans_set(trans) \
923 (((struct nft_trans_set *)trans->data)->set)
924#define nft_trans_set_id(trans) \
925 (((struct nft_trans_set *)trans->data)->set_id)
926
927struct nft_trans_chain {
928 bool update;
929 char name[NFT_CHAIN_MAXNAMELEN];
930 struct nft_stats __percpu *stats;
931 u8 policy;
932};
933
934#define nft_trans_chain_update(trans) \
935 (((struct nft_trans_chain *)trans->data)->update)
936#define nft_trans_chain_name(trans) \
937 (((struct nft_trans_chain *)trans->data)->name)
938#define nft_trans_chain_stats(trans) \
939 (((struct nft_trans_chain *)trans->data)->stats)
940#define nft_trans_chain_policy(trans) \
941 (((struct nft_trans_chain *)trans->data)->policy)
942
943struct nft_trans_table {
944 bool update;
945 bool enable;
946};
947
948#define nft_trans_table_update(trans) \
949 (((struct nft_trans_table *)trans->data)->update)
950#define nft_trans_table_enable(trans) \
951 (((struct nft_trans_table *)trans->data)->enable)
952
953struct nft_trans_elem {
954 struct nft_set *set;
955 struct nft_set_elem elem;
956};
957
958#define nft_trans_elem_set(trans) \
959 (((struct nft_trans_elem *)trans->data)->set)
960#define nft_trans_elem(trans) \
961 (((struct nft_trans_elem *)trans->data)->elem)
962
Patrick McHardy96518512013-10-14 11:00:02 +0200963#endif /* _NET_NF_TABLES_H */