blob: a7a7cebc8d0795a37f6d95723df619eb3975a535 [file] [log] [blame]
Patrick McHardy96518512013-10-14 11:00:02 +02001#ifndef _NET_NF_TABLES_H
2#define _NET_NF_TABLES_H
3
Patrick McHardy0b2d8a72015-04-11 10:46:38 +01004#include <linux/module.h>
Patrick McHardy96518512013-10-14 11:00:02 +02005#include <linux/list.h>
6#include <linux/netfilter.h>
Patrick McHardy67a8fc22014-02-18 18:06:49 +00007#include <linux/netfilter/nfnetlink.h>
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02008#include <linux/netfilter/x_tables.h>
Patrick McHardy96518512013-10-14 11:00:02 +02009#include <linux/netfilter/nf_tables.h>
Eric Dumazetce355e22014-07-09 15:14:06 +020010#include <linux/u64_stats_sync.h>
Patrick McHardy96518512013-10-14 11:00:02 +020011#include <net/netlink.h>
12
Patrick McHardy20a69342013-10-11 12:06:22 +020013#define NFT_JUMP_STACK_SIZE 16
14
Patrick McHardy96518512013-10-14 11:00:02 +020015struct nft_pktinfo {
16 struct sk_buff *skb;
Eric W. Biederman46448d02015-09-18 14:33:00 -050017 struct net *net;
Patrick McHardy96518512013-10-14 11:00:02 +020018 const struct net_device *in;
19 const struct net_device *out;
Eric W. Biederman6aa187f2015-09-18 14:32:57 -050020 u8 pf;
21 u8 hook;
Pablo Neira Ayusobeac5af2016-09-09 12:42:49 +020022 bool tprot_set;
Patrick McHardy4566bf22014-01-03 12:16:18 +000023 u8 tprot;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +020024 /* for x_tables compatibility */
25 struct xt_action_param xt;
Patrick McHardy96518512013-10-14 11:00:02 +020026};
27
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +020028static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +020029 struct sk_buff *skb,
David S. Miller073bfd52015-04-03 21:16:25 -040030 const struct nf_hook_state *state)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +020031{
32 pkt->skb = skb;
Eric W. Biederman46448d02015-09-18 14:33:00 -050033 pkt->net = pkt->xt.net = state->net;
David S. Miller073bfd52015-04-03 21:16:25 -040034 pkt->in = pkt->xt.in = state->in;
35 pkt->out = pkt->xt.out = state->out;
Eric W. Biederman6aa187f2015-09-18 14:32:57 -050036 pkt->hook = pkt->xt.hooknum = state->hook;
37 pkt->pf = pkt->xt.family = state->pf;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +020038}
39
Pablo Neira Ayusobeac5af2016-09-09 12:42:49 +020040static inline void nft_set_pktinfo_proto_unspec(struct nft_pktinfo *pkt,
41 struct sk_buff *skb)
42{
43 pkt->tprot_set = false;
44 pkt->tprot = 0;
45 pkt->xt.thoff = 0;
46 pkt->xt.fragoff = 0;
47}
48
49static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt,
50 struct sk_buff *skb,
51 const struct nf_hook_state *state)
52{
53 nft_set_pktinfo(pkt, skb, state);
54 nft_set_pktinfo_proto_unspec(pkt, skb);
55}
56
Patrick McHardya55e22e2015-04-11 02:27:31 +010057/**
58 * struct nft_verdict - nf_tables verdict
59 *
60 * @code: nf_tables/netfilter verdict code
61 * @chain: destination chain for NFT_JUMP/NFT_GOTO
62 */
63struct nft_verdict {
64 u32 code;
65 struct nft_chain *chain;
66};
67
Patrick McHardy96518512013-10-14 11:00:02 +020068struct nft_data {
69 union {
Patrick McHardy1ca2e172015-04-11 02:27:32 +010070 u32 data[4];
71 struct nft_verdict verdict;
Patrick McHardy96518512013-10-14 11:00:02 +020072 };
73} __attribute__((aligned(__alignof__(u64))));
74
Patrick McHardya55e22e2015-04-11 02:27:31 +010075/**
76 * struct nft_regs - nf_tables register set
77 *
78 * @data: data registers
79 * @verdict: verdict register
80 *
81 * The first four data registers alias to the verdict register.
82 */
83struct nft_regs {
84 union {
Patrick McHardy49499c32015-04-11 02:27:37 +010085 u32 data[20];
Patrick McHardya55e22e2015-04-11 02:27:31 +010086 struct nft_verdict verdict;
87 };
88};
89
Patrick McHardy49499c32015-04-11 02:27:37 +010090static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
91 unsigned int len)
Patrick McHardy96518512013-10-14 11:00:02 +020092{
Patrick McHardy49499c32015-04-11 02:27:37 +010093 memcpy(dst, src, len);
Patrick McHardy96518512013-10-14 11:00:02 +020094}
95
96static inline void nft_data_debug(const struct nft_data *data)
97{
98 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
99 data->data[0], data->data[1],
100 data->data[2], data->data[3]);
101}
102
103/**
Patrick McHardy20a69342013-10-11 12:06:22 +0200104 * struct nft_ctx - nf_tables rule/set context
Patrick McHardy96518512013-10-14 11:00:02 +0200105 *
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200106 * @net: net namespace
Patrick McHardy96518512013-10-14 11:00:02 +0200107 * @afi: address family info
108 * @table: the table the chain is contained in
109 * @chain: the chain the rule is contained in
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200110 * @nla: netlink attributes
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200111 * @portid: netlink portID of the original message
112 * @seq: netlink sequence number
113 * @report: notify via unicast netlink message
Patrick McHardy96518512013-10-14 11:00:02 +0200114 */
115struct nft_ctx {
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200116 struct net *net;
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +0200117 struct nft_af_info *afi;
118 struct nft_table *table;
119 struct nft_chain *chain;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200120 const struct nlattr * const *nla;
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200121 u32 portid;
122 u32 seq;
123 bool report;
Patrick McHardy96518512013-10-14 11:00:02 +0200124};
125
Patrick McHardy96518512013-10-14 11:00:02 +0200126struct nft_data_desc {
127 enum nft_data_types type;
128 unsigned int len;
129};
130
Patrick McHardyd0a11fc2015-04-11 02:27:38 +0100131int nft_data_init(const struct nft_ctx *ctx,
132 struct nft_data *data, unsigned int size,
Joe Perches5eccdfa2013-10-19 22:05:31 -0700133 struct nft_data_desc *desc, const struct nlattr *nla);
134void nft_data_uninit(const struct nft_data *data, enum nft_data_types type);
135int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
136 enum nft_data_types type, unsigned int len);
Patrick McHardy96518512013-10-14 11:00:02 +0200137
138static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
139{
140 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
141}
142
Patrick McHardy20a69342013-10-11 12:06:22 +0200143static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
144{
Pablo Neira Ayusobf798652015-08-12 17:41:00 +0200145 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
Patrick McHardy20a69342013-10-11 12:06:22 +0200146}
147
Patrick McHardyb1c96ed2015-04-11 02:27:36 +0100148unsigned int nft_parse_register(const struct nlattr *attr);
149int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
150
Patrick McHardyd07db982015-04-11 02:27:30 +0100151int nft_validate_register_load(enum nft_registers reg, unsigned int len);
Patrick McHardy1ec10212015-04-11 02:27:27 +0100152int nft_validate_register_store(const struct nft_ctx *ctx,
153 enum nft_registers reg,
154 const struct nft_data *data,
155 enum nft_data_types type, unsigned int len);
Patrick McHardy96518512013-10-14 11:00:02 +0200156
Patrick McHardy86f1ec32015-03-03 20:04:20 +0000157/**
158 * struct nft_userdata - user defined data associated with an object
159 *
160 * @len: length of the data
161 * @data: content
162 *
163 * The presence of user data is indicated in an object specific fashion,
164 * so a length of zero can't occur and the value "len" indicates data
165 * of length len + 1.
166 */
167struct nft_userdata {
168 u8 len;
169 unsigned char data[0];
170};
171
Patrick McHardy96518512013-10-14 11:00:02 +0200172/**
Patrick McHardy20a69342013-10-11 12:06:22 +0200173 * struct nft_set_elem - generic representation of set elements
174 *
Patrick McHardy20a69342013-10-11 12:06:22 +0200175 * @key: element key
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000176 * @priv: element private data and extensions
Patrick McHardy20a69342013-10-11 12:06:22 +0200177 */
178struct nft_set_elem {
Patrick McHardy7d740262015-04-11 02:27:39 +0100179 union {
180 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
181 struct nft_data val;
182 } key;
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000183 void *priv;
Patrick McHardy20a69342013-10-11 12:06:22 +0200184};
185
186struct nft_set;
187struct nft_set_iter {
Pablo Neira Ayuso8588ac02016-06-11 12:20:27 +0800188 u8 genmask;
Patrick McHardy20a69342013-10-11 12:06:22 +0200189 unsigned int count;
190 unsigned int skip;
191 int err;
192 int (*fn)(const struct nft_ctx *ctx,
193 const struct nft_set *set,
194 const struct nft_set_iter *iter,
195 const struct nft_set_elem *elem);
196};
197
198/**
Patrick McHardyc50b9602014-03-28 10:19:47 +0000199 * struct nft_set_desc - description of set elements
200 *
201 * @klen: key length
202 * @dlen: data length
203 * @size: number of set elements
204 */
205struct nft_set_desc {
206 unsigned int klen;
207 unsigned int dlen;
208 unsigned int size;
209};
210
211/**
212 * enum nft_set_class - performance class
213 *
214 * @NFT_LOOKUP_O_1: constant, O(1)
215 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
216 * @NFT_LOOKUP_O_N: linear, O(N)
217 */
218enum nft_set_class {
219 NFT_SET_CLASS_O_1,
220 NFT_SET_CLASS_O_LOG_N,
221 NFT_SET_CLASS_O_N,
222};
223
224/**
225 * struct nft_set_estimate - estimation of memory and performance
226 * characteristics
227 *
228 * @size: required memory
229 * @class: lookup performance class
230 */
231struct nft_set_estimate {
232 unsigned int size;
233 enum nft_set_class class;
234};
235
Patrick McHardyb2832dd2015-03-25 14:08:48 +0000236struct nft_set_ext;
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200237struct nft_expr;
Patrick McHardyb2832dd2015-03-25 14:08:48 +0000238
Patrick McHardyc50b9602014-03-28 10:19:47 +0000239/**
Patrick McHardy20a69342013-10-11 12:06:22 +0200240 * struct nft_set_ops - nf_tables set operations
241 *
242 * @lookup: look up an element within the set
243 * @insert: insert new element into set
Patrick McHardycc02e452015-03-25 14:08:50 +0000244 * @activate: activate new element in the next generation
245 * @deactivate: deactivate element in the next generation
Patrick McHardy20a69342013-10-11 12:06:22 +0200246 * @remove: remove element from set
247 * @walk: iterate over all set elemeennts
248 * @privsize: function to return size of set private data
249 * @init: initialize private data of new set instance
250 * @destroy: destroy private data of set instance
251 * @list: nf_tables_set_ops list node
252 * @owner: module reference
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000253 * @elemsize: element private size
Patrick McHardy20a69342013-10-11 12:06:22 +0200254 * @features: features supported by the implementation
255 */
256struct nft_set_ops {
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +0200257 bool (*lookup)(const struct net *net,
258 const struct nft_set *set,
Patrick McHardy8cd89372015-04-11 02:27:35 +0100259 const u32 *key,
Patrick McHardyb2832dd2015-03-25 14:08:48 +0000260 const struct nft_set_ext **ext);
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200261 bool (*update)(struct nft_set *set,
Patrick McHardy8cd89372015-04-11 02:27:35 +0100262 const u32 *key,
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200263 void *(*new)(struct nft_set *,
264 const struct nft_expr *,
Patrick McHardya55e22e2015-04-11 02:27:31 +0100265 struct nft_regs *),
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200266 const struct nft_expr *expr,
Patrick McHardya55e22e2015-04-11 02:27:31 +0100267 struct nft_regs *regs,
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200268 const struct nft_set_ext **ext);
269
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +0200270 int (*insert)(const struct net *net,
271 const struct nft_set *set,
Pablo Neira Ayusoc016c7e2016-08-24 12:41:54 +0200272 const struct nft_set_elem *elem,
273 struct nft_set_ext **ext);
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +0200274 void (*activate)(const struct net *net,
275 const struct nft_set *set,
Patrick McHardycc02e452015-03-25 14:08:50 +0000276 const struct nft_set_elem *elem);
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +0200277 void * (*deactivate)(const struct net *net,
278 const struct nft_set *set,
Patrick McHardycc02e452015-03-25 14:08:50 +0000279 const struct nft_set_elem *elem);
Patrick McHardy20a69342013-10-11 12:06:22 +0200280 void (*remove)(const struct nft_set *set,
281 const struct nft_set_elem *elem);
282 void (*walk)(const struct nft_ctx *ctx,
283 const struct nft_set *set,
284 struct nft_set_iter *iter);
285
286 unsigned int (*privsize)(const struct nlattr * const nla[]);
Patrick McHardyc50b9602014-03-28 10:19:47 +0000287 bool (*estimate)(const struct nft_set_desc *desc,
288 u32 features,
289 struct nft_set_estimate *est);
Patrick McHardy20a69342013-10-11 12:06:22 +0200290 int (*init)(const struct nft_set *set,
Patrick McHardyc50b9602014-03-28 10:19:47 +0000291 const struct nft_set_desc *desc,
Patrick McHardy20a69342013-10-11 12:06:22 +0200292 const struct nlattr * const nla[]);
293 void (*destroy)(const struct nft_set *set);
294
295 struct list_head list;
296 struct module *owner;
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000297 unsigned int elemsize;
Patrick McHardy20a69342013-10-11 12:06:22 +0200298 u32 features;
299};
300
Joe Perches5eccdfa2013-10-19 22:05:31 -0700301int nft_register_set(struct nft_set_ops *ops);
302void nft_unregister_set(struct nft_set_ops *ops);
Patrick McHardy20a69342013-10-11 12:06:22 +0200303
304/**
305 * struct nft_set - nf_tables set instance
306 *
307 * @list: table set list node
308 * @bindings: list of set bindings
309 * @name: name of the set
310 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
311 * @dtype: data type (verdict or numeric type defined by userspace)
Patrick McHardyc50b9602014-03-28 10:19:47 +0000312 * @size: maximum set size
313 * @nelems: number of elements
Patrick McHardy3dd06732015-04-05 14:41:06 +0200314 * @ndeact: number of deactivated elements queued for removal
Patrick McHardy761da292015-03-26 12:39:36 +0000315 * @timeout: default timeout value in msecs
316 * @gc_int: garbage collection interval in msecs
Arturo Borrero9363dc42014-09-23 13:30:41 +0200317 * @policy: set parameterization (see enum nft_set_policies)
Carlos Falgueras Garcíae6d8eca2016-01-05 14:03:32 +0100318 * @udlen: user data length
319 * @udata: user data
Patrick McHardy20a69342013-10-11 12:06:22 +0200320 * @ops: set ops
321 * @flags: set flags
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +0200322 * @genmask: generation mask
Patrick McHardy20a69342013-10-11 12:06:22 +0200323 * @klen: key length
324 * @dlen: data length
325 * @data: private set data
326 */
327struct nft_set {
328 struct list_head list;
329 struct list_head bindings;
Pablo Neira Ayusocb39ad82016-05-04 17:49:53 +0200330 char name[NFT_SET_MAXNAMELEN];
Patrick McHardy20a69342013-10-11 12:06:22 +0200331 u32 ktype;
332 u32 dtype;
Patrick McHardyc50b9602014-03-28 10:19:47 +0000333 u32 size;
Patrick McHardy3dd06732015-04-05 14:41:06 +0200334 atomic_t nelems;
335 u32 ndeact;
Patrick McHardy761da292015-03-26 12:39:36 +0000336 u64 timeout;
337 u32 gc_int;
Arturo Borrero9363dc42014-09-23 13:30:41 +0200338 u16 policy;
Carlos Falgueras Garcíae6d8eca2016-01-05 14:03:32 +0100339 u16 udlen;
340 unsigned char *udata;
Patrick McHardy20a69342013-10-11 12:06:22 +0200341 /* runtime data below here */
342 const struct nft_set_ops *ops ____cacheline_aligned;
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +0200343 u16 flags:14,
344 genmask:2;
Patrick McHardy20a69342013-10-11 12:06:22 +0200345 u8 klen;
346 u8 dlen;
347 unsigned char data[]
348 __attribute__((aligned(__alignof__(u64))));
349};
350
351static inline void *nft_set_priv(const struct nft_set *set)
352{
353 return (void *)set->data;
354}
355
Patrick McHardy9d098292015-03-26 12:39:40 +0000356static inline struct nft_set *nft_set_container_of(const void *priv)
357{
358 return (void *)priv - offsetof(struct nft_set, data);
359}
360
Joe Perches5eccdfa2013-10-19 22:05:31 -0700361struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +0200362 const struct nlattr *nla, u8 genmask);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +0200363struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +0200364 const struct nlattr *nla, u8 genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +0200365
Patrick McHardy761da292015-03-26 12:39:36 +0000366static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
367{
368 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
369}
370
Patrick McHardy20a69342013-10-11 12:06:22 +0200371/**
372 * struct nft_set_binding - nf_tables set binding
373 *
374 * @list: set bindings list node
375 * @chain: chain containing the rule bound to the set
Patrick McHardy11113e12015-04-05 14:41:07 +0200376 * @flags: set action flags
Patrick McHardy20a69342013-10-11 12:06:22 +0200377 *
378 * A set binding contains all information necessary for validation
379 * of new elements added to a bound set.
380 */
381struct nft_set_binding {
382 struct list_head list;
383 const struct nft_chain *chain;
Patrick McHardy11113e12015-04-05 14:41:07 +0200384 u32 flags;
Patrick McHardy20a69342013-10-11 12:06:22 +0200385};
386
Joe Perches5eccdfa2013-10-19 22:05:31 -0700387int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
388 struct nft_set_binding *binding);
389void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
390 struct nft_set_binding *binding);
Patrick McHardy20a69342013-10-11 12:06:22 +0200391
Patrick McHardy3ac4c072015-03-25 13:07:49 +0000392/**
393 * enum nft_set_extensions - set extension type IDs
394 *
395 * @NFT_SET_EXT_KEY: element key
396 * @NFT_SET_EXT_DATA: mapping data
397 * @NFT_SET_EXT_FLAGS: element flags
Patrick McHardyc3e1b002015-03-26 12:39:37 +0000398 * @NFT_SET_EXT_TIMEOUT: element timeout
399 * @NFT_SET_EXT_EXPIRATION: element expiration time
Patrick McHardy68e942e2015-04-05 14:43:38 +0200400 * @NFT_SET_EXT_USERDATA: user data associated with the element
Patrick McHardyf25ad2e2015-04-11 10:46:39 +0100401 * @NFT_SET_EXT_EXPR: expression assiociated with the element
Patrick McHardy3ac4c072015-03-25 13:07:49 +0000402 * @NFT_SET_EXT_NUM: number of extension types
403 */
404enum nft_set_extensions {
405 NFT_SET_EXT_KEY,
406 NFT_SET_EXT_DATA,
407 NFT_SET_EXT_FLAGS,
Patrick McHardyc3e1b002015-03-26 12:39:37 +0000408 NFT_SET_EXT_TIMEOUT,
409 NFT_SET_EXT_EXPIRATION,
Patrick McHardy68e942e2015-04-05 14:43:38 +0200410 NFT_SET_EXT_USERDATA,
Patrick McHardyf25ad2e2015-04-11 10:46:39 +0100411 NFT_SET_EXT_EXPR,
Patrick McHardy3ac4c072015-03-25 13:07:49 +0000412 NFT_SET_EXT_NUM
413};
414
415/**
416 * struct nft_set_ext_type - set extension type
417 *
418 * @len: fixed part length of the extension
419 * @align: alignment requirements of the extension
420 */
421struct nft_set_ext_type {
422 u8 len;
423 u8 align;
424};
425
426extern const struct nft_set_ext_type nft_set_ext_types[];
427
428/**
429 * struct nft_set_ext_tmpl - set extension template
430 *
431 * @len: length of extension area
432 * @offset: offsets of individual extension types
433 */
434struct nft_set_ext_tmpl {
435 u16 len;
436 u8 offset[NFT_SET_EXT_NUM];
437};
438
439/**
440 * struct nft_set_ext - set extensions
441 *
Patrick McHardycc02e452015-03-25 14:08:50 +0000442 * @genmask: generation mask
Patrick McHardy3ac4c072015-03-25 13:07:49 +0000443 * @offset: offsets of individual extension types
444 * @data: beginning of extension data
445 */
446struct nft_set_ext {
Patrick McHardycc02e452015-03-25 14:08:50 +0000447 u8 genmask;
Patrick McHardy3ac4c072015-03-25 13:07:49 +0000448 u8 offset[NFT_SET_EXT_NUM];
449 char data[0];
450};
451
452static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
453{
454 memset(tmpl, 0, sizeof(*tmpl));
455 tmpl->len = sizeof(struct nft_set_ext);
456}
457
458static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
459 unsigned int len)
460{
461 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
462 BUG_ON(tmpl->len > U8_MAX);
463 tmpl->offset[id] = tmpl->len;
464 tmpl->len += nft_set_ext_types[id].len + len;
465}
466
467static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
468{
469 nft_set_ext_add_length(tmpl, id, 0);
470}
471
472static inline void nft_set_ext_init(struct nft_set_ext *ext,
473 const struct nft_set_ext_tmpl *tmpl)
474{
475 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
476}
477
478static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
479{
480 return !!ext->offset[id];
481}
482
483static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
484{
485 return ext && __nft_set_ext_exists(ext, id);
486}
487
488static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
489{
490 return (void *)ext + ext->offset[id];
491}
492
493static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
494{
495 return nft_set_ext(ext, NFT_SET_EXT_KEY);
496}
497
498static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
499{
500 return nft_set_ext(ext, NFT_SET_EXT_DATA);
501}
502
503static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
504{
505 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
506}
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200507
Patrick McHardyc3e1b002015-03-26 12:39:37 +0000508static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
509{
510 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
511}
512
513static inline unsigned long *nft_set_ext_expiration(const struct nft_set_ext *ext)
514{
515 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
516}
517
Patrick McHardy68e942e2015-04-05 14:43:38 +0200518static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
519{
520 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
521}
522
Patrick McHardyf25ad2e2015-04-11 10:46:39 +0100523static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
524{
525 return nft_set_ext(ext, NFT_SET_EXT_EXPR);
526}
527
Patrick McHardyc3e1b002015-03-26 12:39:37 +0000528static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
529{
530 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
531 time_is_before_eq_jiffies(*nft_set_ext_expiration(ext));
532}
533
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000534static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
535 void *elem)
536{
537 return elem + set->ops->elemsize;
538}
539
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200540void *nft_set_elem_init(const struct nft_set *set,
541 const struct nft_set_ext_tmpl *tmpl,
Patrick McHardy49499c32015-04-11 02:27:37 +0100542 const u32 *key, const u32 *data,
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200543 u64 timeout, gfp_t gfp);
Patrick McHardy61edafb2015-03-25 14:08:47 +0000544void nft_set_elem_destroy(const struct nft_set *set, void *elem);
545
Patrick McHardy20a69342013-10-11 12:06:22 +0200546/**
Patrick McHardycfed7e12015-03-26 12:39:38 +0000547 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
548 *
549 * @rcu: rcu head
550 * @set: set the elements belong to
551 * @cnt: count of elements
552 */
553struct nft_set_gc_batch_head {
554 struct rcu_head rcu;
555 const struct nft_set *set;
556 unsigned int cnt;
557};
558
559#define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
560 sizeof(struct nft_set_gc_batch_head)) / \
561 sizeof(void *))
562
563/**
564 * struct nft_set_gc_batch - nf_tables set garbage collection batch
565 *
566 * @head: GC batch head
567 * @elems: garbage collection elements
568 */
569struct nft_set_gc_batch {
570 struct nft_set_gc_batch_head head;
571 void *elems[NFT_SET_GC_BATCH_SIZE];
572};
573
574struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
575 gfp_t gfp);
576void nft_set_gc_batch_release(struct rcu_head *rcu);
577
578static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
579{
580 if (gcb != NULL)
581 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
582}
583
584static inline struct nft_set_gc_batch *
585nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
586 gfp_t gfp)
587{
588 if (gcb != NULL) {
589 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
590 return gcb;
591 nft_set_gc_batch_complete(gcb);
592 }
593 return nft_set_gc_batch_alloc(set, gfp);
594}
595
596static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
597 void *elem)
598{
599 gcb->elems[gcb->head.cnt++] = elem;
600}
601
602/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200603 * struct nft_expr_type - nf_tables expression type
Patrick McHardy96518512013-10-14 11:00:02 +0200604 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200605 * @select_ops: function to select nft_expr_ops
606 * @ops: default ops, used when no select_ops functions is present
Patrick McHardy96518512013-10-14 11:00:02 +0200607 * @list: used internally
608 * @name: Identifier
609 * @owner: module reference
610 * @policy: netlink attribute policy
611 * @maxattr: highest netlink attribute number
Patrick McHardy64d46802014-02-05 15:03:37 +0000612 * @family: address family for AF-specific types
Patrick McHardy151d7992015-04-11 10:46:40 +0100613 * @flags: expression type flags
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200614 */
615struct nft_expr_type {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200616 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
617 const struct nlattr * const tb[]);
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200618 const struct nft_expr_ops *ops;
619 struct list_head list;
620 const char *name;
621 struct module *owner;
622 const struct nla_policy *policy;
623 unsigned int maxattr;
Patrick McHardy64d46802014-02-05 15:03:37 +0000624 u8 family;
Patrick McHardy151d7992015-04-11 10:46:40 +0100625 u8 flags;
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200626};
627
Patrick McHardy151d7992015-04-11 10:46:40 +0100628#define NFT_EXPR_STATEFUL 0x1
629
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200630/**
631 * struct nft_expr_ops - nf_tables expression operations
632 *
633 * @eval: Expression evaluation function
Patrick McHardy96518512013-10-14 11:00:02 +0200634 * @size: full expression size, including private data size
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200635 * @init: initialization function
636 * @destroy: destruction function
637 * @dump: function to dump parameters
638 * @type: expression type
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200639 * @validate: validate expression, called during loop detection
640 * @data: extra data to attach to this expression operation
Patrick McHardy96518512013-10-14 11:00:02 +0200641 */
642struct nft_expr;
643struct nft_expr_ops {
644 void (*eval)(const struct nft_expr *expr,
Patrick McHardya55e22e2015-04-11 02:27:31 +0100645 struct nft_regs *regs,
Patrick McHardy96518512013-10-14 11:00:02 +0200646 const struct nft_pktinfo *pkt);
Pablo Neira Ayuso086f3322015-11-10 13:39:42 +0100647 int (*clone)(struct nft_expr *dst,
648 const struct nft_expr *src);
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200649 unsigned int size;
650
Patrick McHardy96518512013-10-14 11:00:02 +0200651 int (*init)(const struct nft_ctx *ctx,
652 const struct nft_expr *expr,
653 const struct nlattr * const tb[]);
Patrick McHardy62472bc2014-03-07 19:08:30 +0100654 void (*destroy)(const struct nft_ctx *ctx,
655 const struct nft_expr *expr);
Patrick McHardy96518512013-10-14 11:00:02 +0200656 int (*dump)(struct sk_buff *skb,
657 const struct nft_expr *expr);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200658 int (*validate)(const struct nft_ctx *ctx,
659 const struct nft_expr *expr,
660 const struct nft_data **data);
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200661 const struct nft_expr_type *type;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200662 void *data;
Patrick McHardy96518512013-10-14 11:00:02 +0200663};
664
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200665#define NFT_EXPR_MAXATTR 16
Patrick McHardy96518512013-10-14 11:00:02 +0200666#define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
667 ALIGN(size, __alignof__(struct nft_expr)))
668
669/**
670 * struct nft_expr - nf_tables expression
671 *
672 * @ops: expression ops
673 * @data: expression private data
674 */
675struct nft_expr {
676 const struct nft_expr_ops *ops;
677 unsigned char data[];
678};
679
680static inline void *nft_expr_priv(const struct nft_expr *expr)
681{
682 return (void *)expr->data;
683}
684
Patrick McHardy0b2d8a72015-04-11 10:46:38 +0100685struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
686 const struct nlattr *nla);
687void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
688int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
689 const struct nft_expr *expr);
690
Pablo Neira Ayuso086f3322015-11-10 13:39:42 +0100691static inline int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
Patrick McHardy0b2d8a72015-04-11 10:46:38 +0100692{
Pablo Neira Ayuso086f3322015-11-10 13:39:42 +0100693 int err;
694
Patrick McHardy0b2d8a72015-04-11 10:46:38 +0100695 __module_get(src->ops->type->owner);
Pablo Neira Ayuso086f3322015-11-10 13:39:42 +0100696 if (src->ops->clone) {
697 dst->ops = src->ops;
698 err = src->ops->clone(dst, src);
699 if (err < 0)
700 return err;
701 } else {
702 memcpy(dst, src, src->ops->size);
703 }
704 return 0;
Patrick McHardy0b2d8a72015-04-11 10:46:38 +0100705}
706
Patrick McHardy96518512013-10-14 11:00:02 +0200707/**
708 * struct nft_rule - nf_tables rule
709 *
710 * @list: used internally
Patrick McHardy96518512013-10-14 11:00:02 +0200711 * @handle: rule handle
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +0200712 * @genmask: generation mask
Patrick McHardy96518512013-10-14 11:00:02 +0200713 * @dlen: length of expression data
Patrick McHardy86f1ec32015-03-03 20:04:20 +0000714 * @udata: user data is appended to the rule
Patrick McHardy96518512013-10-14 11:00:02 +0200715 * @data: expression data
716 */
717struct nft_rule {
718 struct list_head list;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +0100719 u64 handle:42,
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +0200720 genmask:2,
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +0100721 dlen:12,
Patrick McHardy86f1ec32015-03-03 20:04:20 +0000722 udata:1;
Patrick McHardy96518512013-10-14 11:00:02 +0200723 unsigned char data[]
724 __attribute__((aligned(__alignof__(struct nft_expr))));
725};
726
727static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
728{
729 return (struct nft_expr *)&rule->data[0];
730}
731
732static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
733{
734 return ((void *)expr) + expr->ops->size;
735}
736
737static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
738{
739 return (struct nft_expr *)&rule->data[rule->dlen];
740}
741
Patrick McHardy86f1ec32015-03-03 20:04:20 +0000742static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +0100743{
744 return (void *)&rule->data[rule->dlen];
745}
746
Patrick McHardy96518512013-10-14 11:00:02 +0200747/*
748 * The last pointer isn't really necessary, but the compiler isn't able to
749 * determine that the result of nft_expr_last() is always the same since it
750 * can't assume that the dlen value wasn't changed within calls in the loop.
751 */
752#define nft_rule_for_each_expr(expr, last, rule) \
753 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
754 (expr) != (last); \
755 (expr) = nft_expr_next(expr))
756
757enum nft_chain_flags {
758 NFT_BASE_CHAIN = 0x1,
Patrick McHardy96518512013-10-14 11:00:02 +0200759};
760
761/**
762 * struct nft_chain - nf_tables chain
763 *
764 * @rules: list of rules in the chain
765 * @list: used internally
Pablo Neira Ayusob5bc89b2013-10-10 16:49:19 +0200766 * @table: table that this chain belongs to
Patrick McHardy96518512013-10-14 11:00:02 +0200767 * @handle: chain handle
Patrick McHardy96518512013-10-14 11:00:02 +0200768 * @use: number of jump references to this chain
769 * @level: length of longest path to this chain
Pablo Neira Ayusoa0a73792014-06-10 10:53:01 +0200770 * @flags: bitmask of enum nft_chain_flags
Patrick McHardy96518512013-10-14 11:00:02 +0200771 * @name: name of the chain
772 */
773struct nft_chain {
774 struct list_head rules;
775 struct list_head list;
Pablo Neira Ayusob5bc89b2013-10-10 16:49:19 +0200776 struct nft_table *table;
Patrick McHardy96518512013-10-14 11:00:02 +0200777 u64 handle;
Pablo Neira Ayusoa0a73792014-06-10 10:53:01 +0200778 u32 use;
Patrick McHardy96518512013-10-14 11:00:02 +0200779 u16 level;
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +0200780 u8 flags:6,
781 genmask:2;
Patrick McHardy96518512013-10-14 11:00:02 +0200782 char name[NFT_CHAIN_MAXNAMELEN];
783};
784
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200785enum nft_chain_type {
786 NFT_CHAIN_T_DEFAULT = 0,
787 NFT_CHAIN_T_ROUTE,
788 NFT_CHAIN_T_NAT,
789 NFT_CHAIN_T_MAX
790};
791
Patrick McHardy1a1e1a12015-03-03 20:10:06 +0000792/**
793 * struct nf_chain_type - nf_tables chain type info
794 *
795 * @name: name of the type
796 * @type: numeric identifier
797 * @family: address family
798 * @owner: module owner
799 * @hook_mask: mask of valid hooks
800 * @hooks: hookfn overrides
801 */
802struct nf_chain_type {
803 const char *name;
804 enum nft_chain_type type;
805 int family;
806 struct module *owner;
807 unsigned int hook_mask;
808 nf_hookfn *hooks[NF_MAX_HOOKS];
809};
810
Pablo Neira Ayuso7210e4e2014-10-13 19:50:22 +0200811int nft_chain_validate_dependency(const struct nft_chain *chain,
812 enum nft_chain_type type);
Pablo Neira Ayuso75e8d062015-01-14 15:33:57 +0100813int nft_chain_validate_hooks(const struct nft_chain *chain,
814 unsigned int hook_flags);
Pablo Neira Ayuso7210e4e2014-10-13 19:50:22 +0200815
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200816struct nft_stats {
Eric Dumazetce355e22014-07-09 15:14:06 +0200817 u64 bytes;
818 u64 pkts;
819 struct u64_stats_sync syncp;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200820};
821
Patrick McHardy115a60b12014-01-03 12:16:15 +0000822#define NFT_HOOK_OPS_MAX 2
823
Patrick McHardy96518512013-10-14 11:00:02 +0200824/**
825 * struct nft_base_chain - nf_tables base chain
826 *
827 * @ops: netfilter hook ops
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200828 * @type: chain type
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200829 * @policy: default policy
830 * @stats: per-cpu chain stats
Patrick McHardy96518512013-10-14 11:00:02 +0200831 * @chain: the chain
Pablo Neira Ayuso2cbce132015-06-12 13:55:41 +0200832 * @dev_name: device name that this base chain is attached to (if any)
Patrick McHardy96518512013-10-14 11:00:02 +0200833 */
834struct nft_base_chain {
Patrick McHardy115a60b12014-01-03 12:16:15 +0000835 struct nf_hook_ops ops[NFT_HOOK_OPS_MAX];
Patrick McHardy2a37d752014-01-09 18:42:37 +0000836 const struct nf_chain_type *type;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200837 u8 policy;
Pablo Neira Ayuso835b8032015-06-15 12:12:01 +0200838 u8 flags;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200839 struct nft_stats __percpu *stats;
Patrick McHardy96518512013-10-14 11:00:02 +0200840 struct nft_chain chain;
Pablo Neira Ayuso2cbce132015-06-12 13:55:41 +0200841 char dev_name[IFNAMSIZ];
Patrick McHardy96518512013-10-14 11:00:02 +0200842};
843
844static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
845{
846 return container_of(chain, struct nft_base_chain, chain);
847}
848
Pablo Neira Ayuso5ebe0b02015-12-15 19:40:49 +0100849int __nft_release_basechain(struct nft_ctx *ctx);
Pablo Neira Ayuso835b8032015-06-15 12:12:01 +0200850
Eric W. Biederman06198b32015-09-18 14:33:06 -0500851unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
Patrick McHardy96518512013-10-14 11:00:02 +0200852
Patrick McHardy96518512013-10-14 11:00:02 +0200853/**
854 * struct nft_table - nf_tables table
855 *
856 * @list: used internally
857 * @chains: chains in the table
858 * @sets: sets in the table
859 * @hgenerator: handle generator state
860 * @use: number of chain references to this table
861 * @flags: table flag (see enum nft_table_flags)
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +0200862 * @genmask: generation mask
Patrick McHardy96518512013-10-14 11:00:02 +0200863 * @name: name of the table
864 */
865struct nft_table {
866 struct list_head list;
867 struct list_head chains;
868 struct list_head sets;
869 u64 hgenerator;
870 u32 use;
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +0200871 u16 flags:14,
872 genmask:2;
Pablo Neira Ayuso1cae5652015-03-05 15:05:36 +0100873 char name[NFT_TABLE_MAXNAMELEN];
Pablo Neira Ayusoebddf1a2015-05-26 18:41:20 +0200874};
875
876enum nft_af_flags {
877 NFT_AF_NEEDS_DEV = (1 << 0),
Patrick McHardy96518512013-10-14 11:00:02 +0200878};
879
880/**
881 * struct nft_af_info - nf_tables address family info
882 *
883 * @list: used internally
884 * @family: address family
885 * @nhooks: number of hooks in this family
886 * @owner: module owner
887 * @tables: used internally
Pablo Neira Ayusoebddf1a2015-05-26 18:41:20 +0200888 * @flags: family flags
Patrick McHardy115a60b12014-01-03 12:16:15 +0000889 * @nops: number of hook ops in this family
890 * @hook_ops_init: initialization function for chain hook ops
Patrick McHardy96518512013-10-14 11:00:02 +0200891 * @hooks: hookfn overrides for packet validation
892 */
893struct nft_af_info {
894 struct list_head list;
895 int family;
896 unsigned int nhooks;
897 struct module *owner;
898 struct list_head tables;
Pablo Neira Ayusoebddf1a2015-05-26 18:41:20 +0200899 u32 flags;
Patrick McHardy115a60b12014-01-03 12:16:15 +0000900 unsigned int nops;
901 void (*hook_ops_init)(struct nf_hook_ops *,
902 unsigned int);
Patrick McHardy96518512013-10-14 11:00:02 +0200903 nf_hookfn *hooks[NF_MAX_HOOKS];
904};
905
Joe Perches5eccdfa2013-10-19 22:05:31 -0700906int nft_register_afinfo(struct net *, struct nft_af_info *);
Pablo Neira Ayusodf05ef82015-12-15 19:39:32 +0100907void nft_unregister_afinfo(struct net *, struct nft_af_info *);
Patrick McHardy96518512013-10-14 11:00:02 +0200908
Patrick McHardy2a37d752014-01-09 18:42:37 +0000909int nft_register_chain_type(const struct nf_chain_type *);
910void nft_unregister_chain_type(const struct nf_chain_type *);
Patrick McHardy96518512013-10-14 11:00:02 +0200911
Joe Perches5eccdfa2013-10-19 22:05:31 -0700912int nft_register_expr(struct nft_expr_type *);
913void nft_unregister_expr(struct nft_expr_type *);
Patrick McHardy96518512013-10-14 11:00:02 +0200914
Florian Westphal33d5a7b2015-11-28 21:53:04 +0100915int nft_verdict_dump(struct sk_buff *skb, int type,
916 const struct nft_verdict *v);
917
918/**
919 * struct nft_traceinfo - nft tracing information and state
920 *
921 * @pkt: pktinfo currently processed
922 * @basechain: base chain currently processed
923 * @chain: chain currently processed
924 * @rule: rule that was evaluated
925 * @verdict: verdict given by rule
926 * @type: event type (enum nft_trace_types)
927 * @packet_dumped: packet headers sent in a previous traceinfo message
928 * @trace: other struct members are initialised
929 */
930struct nft_traceinfo {
931 const struct nft_pktinfo *pkt;
932 const struct nft_base_chain *basechain;
933 const struct nft_chain *chain;
934 const struct nft_rule *rule;
935 const struct nft_verdict *verdict;
936 enum nft_trace_types type;
937 bool packet_dumped;
938 bool trace;
939};
940
941void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
942 const struct nft_verdict *verdict,
943 const struct nft_chain *basechain);
944
945void nft_trace_notify(struct nft_traceinfo *info);
946
Patrick McHardy67a8fc22014-02-18 18:06:49 +0000947#define nft_dereference(p) \
948 nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
949
Patrick McHardy96518512013-10-14 11:00:02 +0200950#define MODULE_ALIAS_NFT_FAMILY(family) \
951 MODULE_ALIAS("nft-afinfo-" __stringify(family))
952
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200953#define MODULE_ALIAS_NFT_CHAIN(family, name) \
954 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
Patrick McHardy96518512013-10-14 11:00:02 +0200955
Patrick McHardy64d46802014-02-05 15:03:37 +0000956#define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
957 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
958
Patrick McHardy96518512013-10-14 11:00:02 +0200959#define MODULE_ALIAS_NFT_EXPR(name) \
960 MODULE_ALIAS("nft-expr-" name)
961
Patrick McHardy20a69342013-10-11 12:06:22 +0200962#define MODULE_ALIAS_NFT_SET() \
963 MODULE_ALIAS("nft-set")
964
Patrick McHardyea4bd992015-03-25 14:08:49 +0000965/*
966 * The gencursor defines two generations, the currently active and the
967 * next one. Objects contain a bitmask of 2 bits specifying the generations
968 * they're active in. A set bit means they're inactive in the generation
969 * represented by that bit.
970 *
971 * New objects start out as inactive in the current and active in the
972 * next generation. When committing the ruleset the bitmask is cleared,
973 * meaning they're active in all generations. When removing an object,
974 * it is set inactive in the next generation. After committing the ruleset,
975 * the objects are removed.
976 */
977static inline unsigned int nft_gencursor_next(const struct net *net)
978{
979 return net->nft.gencursor + 1 == 1 ? 1 : 0;
980}
981
982static inline u8 nft_genmask_next(const struct net *net)
983{
984 return 1 << nft_gencursor_next(net);
985}
986
987static inline u8 nft_genmask_cur(const struct net *net)
988{
989 /* Use ACCESS_ONCE() to prevent refetching the value for atomicity */
990 return 1 << ACCESS_ONCE(net->nft.gencursor);
991}
992
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200993#define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
994
Patrick McHardycc02e452015-03-25 14:08:50 +0000995/*
Pablo Neira Ayuso889f7ee2016-06-12 18:07:07 +0200996 * Generic transaction helpers
997 */
998
999/* Check if this object is currently active. */
1000#define nft_is_active(__net, __obj) \
1001 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1002
1003/* Check if this object is active in the next generation. */
1004#define nft_is_active_next(__net, __obj) \
1005 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1006
1007/* This object becomes active in the next generation. */
1008#define nft_activate_next(__net, __obj) \
1009 (__obj)->genmask = nft_genmask_cur(__net)
1010
1011/* This object becomes inactive in the next generation. */
1012#define nft_deactivate_next(__net, __obj) \
1013 (__obj)->genmask = nft_genmask_next(__net)
1014
1015/* After committing the ruleset, clear the stale generation bit. */
1016#define nft_clear(__net, __obj) \
1017 (__obj)->genmask &= ~nft_genmask_next(__net)
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02001018#define nft_active_genmask(__obj, __genmask) \
1019 !((__obj)->genmask & __genmask)
Pablo Neira Ayuso889f7ee2016-06-12 18:07:07 +02001020
1021/*
Patrick McHardycc02e452015-03-25 14:08:50 +00001022 * Set element transaction helpers
1023 */
1024
1025static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1026 u8 genmask)
1027{
1028 return !(ext->genmask & genmask);
1029}
1030
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +02001031static inline void nft_set_elem_change_active(const struct net *net,
1032 const struct nft_set *set,
Patrick McHardycc02e452015-03-25 14:08:50 +00001033 struct nft_set_ext *ext)
1034{
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +02001035 ext->genmask ^= nft_genmask_next(net);
Patrick McHardycc02e452015-03-25 14:08:50 +00001036}
1037
Patrick McHardy69086652015-03-26 12:39:39 +00001038/*
1039 * We use a free bit in the genmask field to indicate the element
1040 * is busy, meaning it is currently being processed either by
1041 * the netlink API or GC.
1042 *
1043 * Even though the genmask is only a single byte wide, this works
1044 * because the extension structure if fully constant once initialized,
1045 * so there are no non-atomic write accesses unless it is already
1046 * marked busy.
1047 */
1048#define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1049
1050#if defined(__LITTLE_ENDIAN_BITFIELD)
1051#define NFT_SET_ELEM_BUSY_BIT 2
1052#elif defined(__BIG_ENDIAN_BITFIELD)
1053#define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1054#else
1055#error
1056#endif
1057
1058static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1059{
1060 unsigned long *word = (unsigned long *)ext;
1061
1062 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1063 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1064}
1065
1066static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1067{
1068 unsigned long *word = (unsigned long *)ext;
1069
1070 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1071}
1072
Patrick McHardy1a1e1a12015-03-03 20:10:06 +00001073/**
1074 * struct nft_trans - nf_tables object update in transaction
1075 *
1076 * @list: used internally
1077 * @msg_type: message type
1078 * @ctx: transaction context
1079 * @data: internal information related to the transaction
1080 */
1081struct nft_trans {
1082 struct list_head list;
1083 int msg_type;
1084 struct nft_ctx ctx;
1085 char data[0];
1086};
1087
1088struct nft_trans_rule {
1089 struct nft_rule *rule;
1090};
1091
1092#define nft_trans_rule(trans) \
1093 (((struct nft_trans_rule *)trans->data)->rule)
1094
1095struct nft_trans_set {
1096 struct nft_set *set;
1097 u32 set_id;
1098};
1099
1100#define nft_trans_set(trans) \
1101 (((struct nft_trans_set *)trans->data)->set)
1102#define nft_trans_set_id(trans) \
1103 (((struct nft_trans_set *)trans->data)->set_id)
1104
1105struct nft_trans_chain {
1106 bool update;
1107 char name[NFT_CHAIN_MAXNAMELEN];
1108 struct nft_stats __percpu *stats;
1109 u8 policy;
1110};
1111
1112#define nft_trans_chain_update(trans) \
1113 (((struct nft_trans_chain *)trans->data)->update)
1114#define nft_trans_chain_name(trans) \
1115 (((struct nft_trans_chain *)trans->data)->name)
1116#define nft_trans_chain_stats(trans) \
1117 (((struct nft_trans_chain *)trans->data)->stats)
1118#define nft_trans_chain_policy(trans) \
1119 (((struct nft_trans_chain *)trans->data)->policy)
1120
1121struct nft_trans_table {
1122 bool update;
1123 bool enable;
1124};
1125
1126#define nft_trans_table_update(trans) \
1127 (((struct nft_trans_table *)trans->data)->update)
1128#define nft_trans_table_enable(trans) \
1129 (((struct nft_trans_table *)trans->data)->enable)
1130
1131struct nft_trans_elem {
1132 struct nft_set *set;
1133 struct nft_set_elem elem;
1134};
1135
1136#define nft_trans_elem_set(trans) \
1137 (((struct nft_trans_elem *)trans->data)->set)
1138#define nft_trans_elem(trans) \
1139 (((struct nft_trans_elem *)trans->data)->elem)
1140
Patrick McHardy96518512013-10-14 11:00:02 +02001141#endif /* _NET_NF_TABLES_H */