blob: 7ba9a624090fbd344b48866a0925c11a242c7ada [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
Liping Zhang1894d7c2017-03-08 22:54:18 +080090/* Store/load an u16 or u8 integer to/from the u32 data register.
91 *
92 * Note, when using concatenations, register allocation happens at 32-bit
93 * level. So for store instruction, pad the rest part with zero to avoid
94 * garbage values.
95 */
96
97static inline void nft_reg_store16(u32 *dreg, u16 val)
98{
99 *dreg = 0;
100 *(u16 *)dreg = val;
101}
102
103static inline void nft_reg_store8(u32 *dreg, u8 val)
104{
105 *dreg = 0;
106 *(u8 *)dreg = val;
107}
108
109static inline u16 nft_reg_load16(u32 *sreg)
110{
111 return *(u16 *)sreg;
112}
113
114static inline u8 nft_reg_load8(u32 *sreg)
115{
116 return *(u8 *)sreg;
117}
118
Patrick McHardy49499c32015-04-11 02:27:37 +0100119static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
120 unsigned int len)
Patrick McHardy96518512013-10-14 11:00:02 +0200121{
Patrick McHardy49499c32015-04-11 02:27:37 +0100122 memcpy(dst, src, len);
Patrick McHardy96518512013-10-14 11:00:02 +0200123}
124
125static inline void nft_data_debug(const struct nft_data *data)
126{
127 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
128 data->data[0], data->data[1],
129 data->data[2], data->data[3]);
130}
131
132/**
Patrick McHardy20a69342013-10-11 12:06:22 +0200133 * struct nft_ctx - nf_tables rule/set context
Patrick McHardy96518512013-10-14 11:00:02 +0200134 *
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200135 * @net: net namespace
Patrick McHardy96518512013-10-14 11:00:02 +0200136 * @afi: address family info
137 * @table: the table the chain is contained in
138 * @chain: the chain the rule is contained in
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200139 * @nla: netlink attributes
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200140 * @portid: netlink portID of the original message
141 * @seq: netlink sequence number
142 * @report: notify via unicast netlink message
Patrick McHardy96518512013-10-14 11:00:02 +0200143 */
144struct nft_ctx {
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200145 struct net *net;
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +0200146 struct nft_af_info *afi;
147 struct nft_table *table;
148 struct nft_chain *chain;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200149 const struct nlattr * const *nla;
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200150 u32 portid;
151 u32 seq;
152 bool report;
Patrick McHardy96518512013-10-14 11:00:02 +0200153};
154
Patrick McHardy96518512013-10-14 11:00:02 +0200155struct nft_data_desc {
156 enum nft_data_types type;
157 unsigned int len;
158};
159
Patrick McHardyd0a11fc2015-04-11 02:27:38 +0100160int nft_data_init(const struct nft_ctx *ctx,
161 struct nft_data *data, unsigned int size,
Joe Perches5eccdfa2013-10-19 22:05:31 -0700162 struct nft_data_desc *desc, const struct nlattr *nla);
163void nft_data_uninit(const struct nft_data *data, enum nft_data_types type);
164int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
165 enum nft_data_types type, unsigned int len);
Patrick McHardy96518512013-10-14 11:00:02 +0200166
167static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
168{
169 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
170}
171
Patrick McHardy20a69342013-10-11 12:06:22 +0200172static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
173{
Pablo Neira Ayusobf798652015-08-12 17:41:00 +0200174 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
Patrick McHardy20a69342013-10-11 12:06:22 +0200175}
176
John W. Linvillef1d505b2016-10-25 15:56:39 -0400177int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
Patrick McHardyb1c96ed2015-04-11 02:27:36 +0100178unsigned int nft_parse_register(const struct nlattr *attr);
179int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
180
Patrick McHardyd07db982015-04-11 02:27:30 +0100181int nft_validate_register_load(enum nft_registers reg, unsigned int len);
Patrick McHardy1ec10212015-04-11 02:27:27 +0100182int nft_validate_register_store(const struct nft_ctx *ctx,
183 enum nft_registers reg,
184 const struct nft_data *data,
185 enum nft_data_types type, unsigned int len);
Patrick McHardy96518512013-10-14 11:00:02 +0200186
Patrick McHardy86f1ec32015-03-03 20:04:20 +0000187/**
188 * struct nft_userdata - user defined data associated with an object
189 *
190 * @len: length of the data
191 * @data: content
192 *
193 * The presence of user data is indicated in an object specific fashion,
194 * so a length of zero can't occur and the value "len" indicates data
195 * of length len + 1.
196 */
197struct nft_userdata {
198 u8 len;
199 unsigned char data[0];
200};
201
Patrick McHardy96518512013-10-14 11:00:02 +0200202/**
Patrick McHardy20a69342013-10-11 12:06:22 +0200203 * struct nft_set_elem - generic representation of set elements
204 *
Patrick McHardy20a69342013-10-11 12:06:22 +0200205 * @key: element key
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000206 * @priv: element private data and extensions
Patrick McHardy20a69342013-10-11 12:06:22 +0200207 */
208struct nft_set_elem {
Patrick McHardy7d740262015-04-11 02:27:39 +0100209 union {
210 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
211 struct nft_data val;
212 } key;
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000213 void *priv;
Patrick McHardy20a69342013-10-11 12:06:22 +0200214};
215
216struct nft_set;
217struct nft_set_iter {
Pablo Neira Ayuso8588ac02016-06-11 12:20:27 +0800218 u8 genmask;
Patrick McHardy20a69342013-10-11 12:06:22 +0200219 unsigned int count;
220 unsigned int skip;
221 int err;
222 int (*fn)(const struct nft_ctx *ctx,
223 const struct nft_set *set,
224 const struct nft_set_iter *iter,
225 const struct nft_set_elem *elem);
226};
227
228/**
Patrick McHardyc50b9602014-03-28 10:19:47 +0000229 * struct nft_set_desc - description of set elements
230 *
231 * @klen: key length
232 * @dlen: data length
233 * @size: number of set elements
234 */
235struct nft_set_desc {
236 unsigned int klen;
237 unsigned int dlen;
238 unsigned int size;
239};
240
241/**
242 * enum nft_set_class - performance class
243 *
244 * @NFT_LOOKUP_O_1: constant, O(1)
245 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
246 * @NFT_LOOKUP_O_N: linear, O(N)
247 */
248enum nft_set_class {
249 NFT_SET_CLASS_O_1,
250 NFT_SET_CLASS_O_LOG_N,
251 NFT_SET_CLASS_O_N,
252};
253
254/**
255 * struct nft_set_estimate - estimation of memory and performance
256 * characteristics
257 *
258 * @size: required memory
259 * @class: lookup performance class
260 */
261struct nft_set_estimate {
262 unsigned int size;
263 enum nft_set_class class;
264};
265
Patrick McHardyb2832dd2015-03-25 14:08:48 +0000266struct nft_set_ext;
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200267struct nft_expr;
Patrick McHardyb2832dd2015-03-25 14:08:48 +0000268
Patrick McHardyc50b9602014-03-28 10:19:47 +0000269/**
Patrick McHardy20a69342013-10-11 12:06:22 +0200270 * struct nft_set_ops - nf_tables set operations
271 *
272 * @lookup: look up an element within the set
273 * @insert: insert new element into set
Patrick McHardycc02e452015-03-25 14:08:50 +0000274 * @activate: activate new element in the next generation
275 * @deactivate: deactivate element in the next generation
Patrick McHardy20a69342013-10-11 12:06:22 +0200276 * @remove: remove element from set
277 * @walk: iterate over all set elemeennts
278 * @privsize: function to return size of set private data
279 * @init: initialize private data of new set instance
280 * @destroy: destroy private data of set instance
281 * @list: nf_tables_set_ops list node
282 * @owner: module reference
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000283 * @elemsize: element private size
Patrick McHardy20a69342013-10-11 12:06:22 +0200284 * @features: features supported by the implementation
285 */
286struct nft_set_ops {
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +0200287 bool (*lookup)(const struct net *net,
288 const struct nft_set *set,
Patrick McHardy8cd89372015-04-11 02:27:35 +0100289 const u32 *key,
Patrick McHardyb2832dd2015-03-25 14:08:48 +0000290 const struct nft_set_ext **ext);
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200291 bool (*update)(struct nft_set *set,
Patrick McHardy8cd89372015-04-11 02:27:35 +0100292 const u32 *key,
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200293 void *(*new)(struct nft_set *,
294 const struct nft_expr *,
Patrick McHardya55e22e2015-04-11 02:27:31 +0100295 struct nft_regs *),
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200296 const struct nft_expr *expr,
Patrick McHardya55e22e2015-04-11 02:27:31 +0100297 struct nft_regs *regs,
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200298 const struct nft_set_ext **ext);
299
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +0200300 int (*insert)(const struct net *net,
301 const struct nft_set *set,
Pablo Neira Ayusoc016c7e2016-08-24 12:41:54 +0200302 const struct nft_set_elem *elem,
303 struct nft_set_ext **ext);
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +0200304 void (*activate)(const struct net *net,
305 const struct nft_set *set,
Patrick McHardycc02e452015-03-25 14:08:50 +0000306 const struct nft_set_elem *elem);
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +0200307 void * (*deactivate)(const struct net *net,
308 const struct nft_set *set,
Patrick McHardycc02e452015-03-25 14:08:50 +0000309 const struct nft_set_elem *elem);
Patrick McHardy20a69342013-10-11 12:06:22 +0200310 void (*remove)(const struct nft_set *set,
311 const struct nft_set_elem *elem);
312 void (*walk)(const struct nft_ctx *ctx,
313 const struct nft_set *set,
314 struct nft_set_iter *iter);
315
316 unsigned int (*privsize)(const struct nlattr * const nla[]);
Patrick McHardyc50b9602014-03-28 10:19:47 +0000317 bool (*estimate)(const struct nft_set_desc *desc,
318 u32 features,
319 struct nft_set_estimate *est);
Patrick McHardy20a69342013-10-11 12:06:22 +0200320 int (*init)(const struct nft_set *set,
Patrick McHardyc50b9602014-03-28 10:19:47 +0000321 const struct nft_set_desc *desc,
Patrick McHardy20a69342013-10-11 12:06:22 +0200322 const struct nlattr * const nla[]);
323 void (*destroy)(const struct nft_set *set);
324
325 struct list_head list;
326 struct module *owner;
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000327 unsigned int elemsize;
Patrick McHardy20a69342013-10-11 12:06:22 +0200328 u32 features;
329};
330
Joe Perches5eccdfa2013-10-19 22:05:31 -0700331int nft_register_set(struct nft_set_ops *ops);
332void nft_unregister_set(struct nft_set_ops *ops);
Patrick McHardy20a69342013-10-11 12:06:22 +0200333
334/**
335 * struct nft_set - nf_tables set instance
336 *
337 * @list: table set list node
338 * @bindings: list of set bindings
339 * @name: name of the set
340 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
341 * @dtype: data type (verdict or numeric type defined by userspace)
Patrick McHardyc50b9602014-03-28 10:19:47 +0000342 * @size: maximum set size
343 * @nelems: number of elements
Patrick McHardy3dd06732015-04-05 14:41:06 +0200344 * @ndeact: number of deactivated elements queued for removal
Anders K. Pedersend3e2a112016-11-20 16:38:47 +0000345 * @timeout: default timeout value in jiffies
Patrick McHardy761da292015-03-26 12:39:36 +0000346 * @gc_int: garbage collection interval in msecs
Arturo Borrero9363dc42014-09-23 13:30:41 +0200347 * @policy: set parameterization (see enum nft_set_policies)
Carlos Falgueras Garcíae6d8eca2016-01-05 14:03:32 +0100348 * @udlen: user data length
349 * @udata: user data
Patrick McHardy20a69342013-10-11 12:06:22 +0200350 * @ops: set ops
351 * @flags: set flags
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +0200352 * @genmask: generation mask
Patrick McHardy20a69342013-10-11 12:06:22 +0200353 * @klen: key length
354 * @dlen: data length
355 * @data: private set data
356 */
357struct nft_set {
358 struct list_head list;
359 struct list_head bindings;
Pablo Neira Ayusocb39ad82016-05-04 17:49:53 +0200360 char name[NFT_SET_MAXNAMELEN];
Patrick McHardy20a69342013-10-11 12:06:22 +0200361 u32 ktype;
362 u32 dtype;
Patrick McHardyc50b9602014-03-28 10:19:47 +0000363 u32 size;
Patrick McHardy3dd06732015-04-05 14:41:06 +0200364 atomic_t nelems;
365 u32 ndeact;
Patrick McHardy761da292015-03-26 12:39:36 +0000366 u64 timeout;
367 u32 gc_int;
Arturo Borrero9363dc42014-09-23 13:30:41 +0200368 u16 policy;
Carlos Falgueras Garcíae6d8eca2016-01-05 14:03:32 +0100369 u16 udlen;
370 unsigned char *udata;
Patrick McHardy20a69342013-10-11 12:06:22 +0200371 /* runtime data below here */
372 const struct nft_set_ops *ops ____cacheline_aligned;
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +0200373 u16 flags:14,
374 genmask:2;
Patrick McHardy20a69342013-10-11 12:06:22 +0200375 u8 klen;
376 u8 dlen;
377 unsigned char data[]
378 __attribute__((aligned(__alignof__(u64))));
379};
380
381static inline void *nft_set_priv(const struct nft_set *set)
382{
383 return (void *)set->data;
384}
385
Patrick McHardy9d098292015-03-26 12:39:40 +0000386static inline struct nft_set *nft_set_container_of(const void *priv)
387{
388 return (void *)priv - offsetof(struct nft_set, data);
389}
390
Joe Perches5eccdfa2013-10-19 22:05:31 -0700391struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +0200392 const struct nlattr *nla, u8 genmask);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +0200393struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +0200394 const struct nlattr *nla, u8 genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +0200395
Patrick McHardy761da292015-03-26 12:39:36 +0000396static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
397{
398 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
399}
400
Patrick McHardy20a69342013-10-11 12:06:22 +0200401/**
402 * struct nft_set_binding - nf_tables set binding
403 *
404 * @list: set bindings list node
405 * @chain: chain containing the rule bound to the set
Patrick McHardy11113e12015-04-05 14:41:07 +0200406 * @flags: set action flags
Patrick McHardy20a69342013-10-11 12:06:22 +0200407 *
408 * A set binding contains all information necessary for validation
409 * of new elements added to a bound set.
410 */
411struct nft_set_binding {
412 struct list_head list;
413 const struct nft_chain *chain;
Patrick McHardy11113e12015-04-05 14:41:07 +0200414 u32 flags;
Patrick McHardy20a69342013-10-11 12:06:22 +0200415};
416
Joe Perches5eccdfa2013-10-19 22:05:31 -0700417int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
418 struct nft_set_binding *binding);
419void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
420 struct nft_set_binding *binding);
Patrick McHardy20a69342013-10-11 12:06:22 +0200421
Patrick McHardy3ac4c072015-03-25 13:07:49 +0000422/**
423 * enum nft_set_extensions - set extension type IDs
424 *
425 * @NFT_SET_EXT_KEY: element key
426 * @NFT_SET_EXT_DATA: mapping data
427 * @NFT_SET_EXT_FLAGS: element flags
Patrick McHardyc3e1b002015-03-26 12:39:37 +0000428 * @NFT_SET_EXT_TIMEOUT: element timeout
429 * @NFT_SET_EXT_EXPIRATION: element expiration time
Patrick McHardy68e942e2015-04-05 14:43:38 +0200430 * @NFT_SET_EXT_USERDATA: user data associated with the element
Patrick McHardyf25ad2e2015-04-11 10:46:39 +0100431 * @NFT_SET_EXT_EXPR: expression assiociated with the element
Patrick McHardy3ac4c072015-03-25 13:07:49 +0000432 * @NFT_SET_EXT_NUM: number of extension types
433 */
434enum nft_set_extensions {
435 NFT_SET_EXT_KEY,
436 NFT_SET_EXT_DATA,
437 NFT_SET_EXT_FLAGS,
Patrick McHardyc3e1b002015-03-26 12:39:37 +0000438 NFT_SET_EXT_TIMEOUT,
439 NFT_SET_EXT_EXPIRATION,
Patrick McHardy68e942e2015-04-05 14:43:38 +0200440 NFT_SET_EXT_USERDATA,
Patrick McHardyf25ad2e2015-04-11 10:46:39 +0100441 NFT_SET_EXT_EXPR,
Patrick McHardy3ac4c072015-03-25 13:07:49 +0000442 NFT_SET_EXT_NUM
443};
444
445/**
446 * struct nft_set_ext_type - set extension type
447 *
448 * @len: fixed part length of the extension
449 * @align: alignment requirements of the extension
450 */
451struct nft_set_ext_type {
452 u8 len;
453 u8 align;
454};
455
456extern const struct nft_set_ext_type nft_set_ext_types[];
457
458/**
459 * struct nft_set_ext_tmpl - set extension template
460 *
461 * @len: length of extension area
462 * @offset: offsets of individual extension types
463 */
464struct nft_set_ext_tmpl {
465 u16 len;
466 u8 offset[NFT_SET_EXT_NUM];
467};
468
469/**
470 * struct nft_set_ext - set extensions
471 *
Patrick McHardycc02e452015-03-25 14:08:50 +0000472 * @genmask: generation mask
Patrick McHardy3ac4c072015-03-25 13:07:49 +0000473 * @offset: offsets of individual extension types
474 * @data: beginning of extension data
475 */
476struct nft_set_ext {
Patrick McHardycc02e452015-03-25 14:08:50 +0000477 u8 genmask;
Patrick McHardy3ac4c072015-03-25 13:07:49 +0000478 u8 offset[NFT_SET_EXT_NUM];
479 char data[0];
480};
481
482static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
483{
484 memset(tmpl, 0, sizeof(*tmpl));
485 tmpl->len = sizeof(struct nft_set_ext);
486}
487
488static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
489 unsigned int len)
490{
491 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
492 BUG_ON(tmpl->len > U8_MAX);
493 tmpl->offset[id] = tmpl->len;
494 tmpl->len += nft_set_ext_types[id].len + len;
495}
496
497static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
498{
499 nft_set_ext_add_length(tmpl, id, 0);
500}
501
502static inline void nft_set_ext_init(struct nft_set_ext *ext,
503 const struct nft_set_ext_tmpl *tmpl)
504{
505 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
506}
507
508static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
509{
510 return !!ext->offset[id];
511}
512
513static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
514{
515 return ext && __nft_set_ext_exists(ext, id);
516}
517
518static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
519{
520 return (void *)ext + ext->offset[id];
521}
522
523static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
524{
525 return nft_set_ext(ext, NFT_SET_EXT_KEY);
526}
527
528static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
529{
530 return nft_set_ext(ext, NFT_SET_EXT_DATA);
531}
532
533static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
534{
535 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
536}
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200537
Patrick McHardyc3e1b002015-03-26 12:39:37 +0000538static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
539{
540 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
541}
542
543static inline unsigned long *nft_set_ext_expiration(const struct nft_set_ext *ext)
544{
545 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
546}
547
Patrick McHardy68e942e2015-04-05 14:43:38 +0200548static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
549{
550 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
551}
552
Patrick McHardyf25ad2e2015-04-11 10:46:39 +0100553static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
554{
555 return nft_set_ext(ext, NFT_SET_EXT_EXPR);
556}
557
Patrick McHardyc3e1b002015-03-26 12:39:37 +0000558static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
559{
560 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
561 time_is_before_eq_jiffies(*nft_set_ext_expiration(ext));
562}
563
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000564static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
565 void *elem)
566{
567 return elem + set->ops->elemsize;
568}
569
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200570void *nft_set_elem_init(const struct nft_set *set,
571 const struct nft_set_ext_tmpl *tmpl,
Patrick McHardy49499c32015-04-11 02:27:37 +0100572 const u32 *key, const u32 *data,
Patrick McHardy22fe54d2015-04-05 14:41:08 +0200573 u64 timeout, gfp_t gfp);
Liping Zhang61f9e292016-10-22 18:51:25 +0800574void nft_set_elem_destroy(const struct nft_set *set, void *elem,
575 bool destroy_expr);
Patrick McHardy61edafb2015-03-25 14:08:47 +0000576
Patrick McHardy20a69342013-10-11 12:06:22 +0200577/**
Patrick McHardycfed7e12015-03-26 12:39:38 +0000578 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
579 *
580 * @rcu: rcu head
581 * @set: set the elements belong to
582 * @cnt: count of elements
583 */
584struct nft_set_gc_batch_head {
585 struct rcu_head rcu;
586 const struct nft_set *set;
587 unsigned int cnt;
588};
589
590#define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
591 sizeof(struct nft_set_gc_batch_head)) / \
592 sizeof(void *))
593
594/**
595 * struct nft_set_gc_batch - nf_tables set garbage collection batch
596 *
597 * @head: GC batch head
598 * @elems: garbage collection elements
599 */
600struct nft_set_gc_batch {
601 struct nft_set_gc_batch_head head;
602 void *elems[NFT_SET_GC_BATCH_SIZE];
603};
604
605struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
606 gfp_t gfp);
607void nft_set_gc_batch_release(struct rcu_head *rcu);
608
609static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
610{
611 if (gcb != NULL)
612 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
613}
614
615static inline struct nft_set_gc_batch *
616nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
617 gfp_t gfp)
618{
619 if (gcb != NULL) {
620 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
621 return gcb;
622 nft_set_gc_batch_complete(gcb);
623 }
624 return nft_set_gc_batch_alloc(set, gfp);
625}
626
627static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
628 void *elem)
629{
630 gcb->elems[gcb->head.cnt++] = elem;
631}
632
633/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200634 * struct nft_expr_type - nf_tables expression type
Patrick McHardy96518512013-10-14 11:00:02 +0200635 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200636 * @select_ops: function to select nft_expr_ops
637 * @ops: default ops, used when no select_ops functions is present
Patrick McHardy96518512013-10-14 11:00:02 +0200638 * @list: used internally
639 * @name: Identifier
640 * @owner: module reference
641 * @policy: netlink attribute policy
642 * @maxattr: highest netlink attribute number
Patrick McHardy64d46802014-02-05 15:03:37 +0000643 * @family: address family for AF-specific types
Patrick McHardy151d7992015-04-11 10:46:40 +0100644 * @flags: expression type flags
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200645 */
646struct nft_expr_type {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200647 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
648 const struct nlattr * const tb[]);
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200649 const struct nft_expr_ops *ops;
650 struct list_head list;
651 const char *name;
652 struct module *owner;
653 const struct nla_policy *policy;
654 unsigned int maxattr;
Patrick McHardy64d46802014-02-05 15:03:37 +0000655 u8 family;
Patrick McHardy151d7992015-04-11 10:46:40 +0100656 u8 flags;
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200657};
658
Patrick McHardy151d7992015-04-11 10:46:40 +0100659#define NFT_EXPR_STATEFUL 0x1
660
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200661/**
662 * struct nft_expr_ops - nf_tables expression operations
663 *
664 * @eval: Expression evaluation function
Patrick McHardy96518512013-10-14 11:00:02 +0200665 * @size: full expression size, including private data size
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200666 * @init: initialization function
667 * @destroy: destruction function
668 * @dump: function to dump parameters
669 * @type: expression type
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200670 * @validate: validate expression, called during loop detection
671 * @data: extra data to attach to this expression operation
Patrick McHardy96518512013-10-14 11:00:02 +0200672 */
673struct nft_expr;
674struct nft_expr_ops {
675 void (*eval)(const struct nft_expr *expr,
Patrick McHardya55e22e2015-04-11 02:27:31 +0100676 struct nft_regs *regs,
Patrick McHardy96518512013-10-14 11:00:02 +0200677 const struct nft_pktinfo *pkt);
Pablo Neira Ayuso086f3322015-11-10 13:39:42 +0100678 int (*clone)(struct nft_expr *dst,
679 const struct nft_expr *src);
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200680 unsigned int size;
681
Patrick McHardy96518512013-10-14 11:00:02 +0200682 int (*init)(const struct nft_ctx *ctx,
683 const struct nft_expr *expr,
684 const struct nlattr * const tb[]);
Patrick McHardy62472bc2014-03-07 19:08:30 +0100685 void (*destroy)(const struct nft_ctx *ctx,
686 const struct nft_expr *expr);
Patrick McHardy96518512013-10-14 11:00:02 +0200687 int (*dump)(struct sk_buff *skb,
688 const struct nft_expr *expr);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200689 int (*validate)(const struct nft_ctx *ctx,
690 const struct nft_expr *expr,
691 const struct nft_data **data);
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200692 const struct nft_expr_type *type;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200693 void *data;
Patrick McHardy96518512013-10-14 11:00:02 +0200694};
695
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200696#define NFT_EXPR_MAXATTR 16
Patrick McHardy96518512013-10-14 11:00:02 +0200697#define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
698 ALIGN(size, __alignof__(struct nft_expr)))
699
700/**
701 * struct nft_expr - nf_tables expression
702 *
703 * @ops: expression ops
704 * @data: expression private data
705 */
706struct nft_expr {
707 const struct nft_expr_ops *ops;
Lukas Wunnerf823bf02019-10-31 11:06:24 +0100708 unsigned char data[]
709 __attribute__((aligned(__alignof__(u64))));
Patrick McHardy96518512013-10-14 11:00:02 +0200710};
711
712static inline void *nft_expr_priv(const struct nft_expr *expr)
713{
714 return (void *)expr->data;
715}
716
Patrick McHardy0b2d8a72015-04-11 10:46:38 +0100717struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
718 const struct nlattr *nla);
719void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
720int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
721 const struct nft_expr *expr);
722
Pablo Neira Ayuso086f3322015-11-10 13:39:42 +0100723static inline int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
Patrick McHardy0b2d8a72015-04-11 10:46:38 +0100724{
Pablo Neira Ayuso086f3322015-11-10 13:39:42 +0100725 int err;
726
Pablo Neira Ayuso086f3322015-11-10 13:39:42 +0100727 if (src->ops->clone) {
728 dst->ops = src->ops;
729 err = src->ops->clone(dst, src);
730 if (err < 0)
731 return err;
732 } else {
733 memcpy(dst, src, src->ops->size);
734 }
Liping Zhang61f9e292016-10-22 18:51:25 +0800735
736 __module_get(src->ops->type->owner);
Pablo Neira Ayuso086f3322015-11-10 13:39:42 +0100737 return 0;
Patrick McHardy0b2d8a72015-04-11 10:46:38 +0100738}
739
Patrick McHardy96518512013-10-14 11:00:02 +0200740/**
741 * struct nft_rule - nf_tables rule
742 *
743 * @list: used internally
Patrick McHardy96518512013-10-14 11:00:02 +0200744 * @handle: rule handle
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +0200745 * @genmask: generation mask
Patrick McHardy96518512013-10-14 11:00:02 +0200746 * @dlen: length of expression data
Patrick McHardy86f1ec32015-03-03 20:04:20 +0000747 * @udata: user data is appended to the rule
Patrick McHardy96518512013-10-14 11:00:02 +0200748 * @data: expression data
749 */
750struct nft_rule {
751 struct list_head list;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +0100752 u64 handle:42,
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +0200753 genmask:2,
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +0100754 dlen:12,
Patrick McHardy86f1ec32015-03-03 20:04:20 +0000755 udata:1;
Patrick McHardy96518512013-10-14 11:00:02 +0200756 unsigned char data[]
757 __attribute__((aligned(__alignof__(struct nft_expr))));
758};
759
760static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
761{
762 return (struct nft_expr *)&rule->data[0];
763}
764
765static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
766{
767 return ((void *)expr) + expr->ops->size;
768}
769
770static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
771{
772 return (struct nft_expr *)&rule->data[rule->dlen];
773}
774
Patrick McHardy86f1ec32015-03-03 20:04:20 +0000775static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +0100776{
777 return (void *)&rule->data[rule->dlen];
778}
779
Patrick McHardy96518512013-10-14 11:00:02 +0200780/*
781 * The last pointer isn't really necessary, but the compiler isn't able to
782 * determine that the result of nft_expr_last() is always the same since it
783 * can't assume that the dlen value wasn't changed within calls in the loop.
784 */
785#define nft_rule_for_each_expr(expr, last, rule) \
786 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
787 (expr) != (last); \
788 (expr) = nft_expr_next(expr))
789
790enum nft_chain_flags {
791 NFT_BASE_CHAIN = 0x1,
Patrick McHardy96518512013-10-14 11:00:02 +0200792};
793
794/**
795 * struct nft_chain - nf_tables chain
796 *
797 * @rules: list of rules in the chain
798 * @list: used internally
Pablo Neira Ayusob5bc89b2013-10-10 16:49:19 +0200799 * @table: table that this chain belongs to
Patrick McHardy96518512013-10-14 11:00:02 +0200800 * @handle: chain handle
Patrick McHardy96518512013-10-14 11:00:02 +0200801 * @use: number of jump references to this chain
802 * @level: length of longest path to this chain
Pablo Neira Ayusoa0a73792014-06-10 10:53:01 +0200803 * @flags: bitmask of enum nft_chain_flags
Patrick McHardy96518512013-10-14 11:00:02 +0200804 * @name: name of the chain
805 */
806struct nft_chain {
807 struct list_head rules;
808 struct list_head list;
Pablo Neira Ayusob5bc89b2013-10-10 16:49:19 +0200809 struct nft_table *table;
Patrick McHardy96518512013-10-14 11:00:02 +0200810 u64 handle;
Pablo Neira Ayusoa0a73792014-06-10 10:53:01 +0200811 u32 use;
Patrick McHardy96518512013-10-14 11:00:02 +0200812 u16 level;
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +0200813 u8 flags:6,
814 genmask:2;
Patrick McHardy96518512013-10-14 11:00:02 +0200815 char name[NFT_CHAIN_MAXNAMELEN];
816};
817
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200818enum nft_chain_type {
819 NFT_CHAIN_T_DEFAULT = 0,
820 NFT_CHAIN_T_ROUTE,
821 NFT_CHAIN_T_NAT,
822 NFT_CHAIN_T_MAX
823};
824
Patrick McHardy1a1e1a12015-03-03 20:10:06 +0000825/**
826 * struct nf_chain_type - nf_tables chain type info
827 *
828 * @name: name of the type
829 * @type: numeric identifier
830 * @family: address family
831 * @owner: module owner
832 * @hook_mask: mask of valid hooks
833 * @hooks: hookfn overrides
834 */
835struct nf_chain_type {
836 const char *name;
837 enum nft_chain_type type;
838 int family;
839 struct module *owner;
840 unsigned int hook_mask;
841 nf_hookfn *hooks[NF_MAX_HOOKS];
842};
843
Pablo Neira Ayuso7210e4e2014-10-13 19:50:22 +0200844int nft_chain_validate_dependency(const struct nft_chain *chain,
845 enum nft_chain_type type);
Pablo Neira Ayuso75e8d062015-01-14 15:33:57 +0100846int nft_chain_validate_hooks(const struct nft_chain *chain,
847 unsigned int hook_flags);
Pablo Neira Ayuso7210e4e2014-10-13 19:50:22 +0200848
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200849struct nft_stats {
Eric Dumazetce355e22014-07-09 15:14:06 +0200850 u64 bytes;
851 u64 pkts;
852 struct u64_stats_sync syncp;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200853};
854
Patrick McHardy115a60b2014-01-03 12:16:15 +0000855#define NFT_HOOK_OPS_MAX 2
856
Patrick McHardy96518512013-10-14 11:00:02 +0200857/**
858 * struct nft_base_chain - nf_tables base chain
859 *
860 * @ops: netfilter hook ops
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200861 * @type: chain type
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200862 * @policy: default policy
863 * @stats: per-cpu chain stats
Patrick McHardy96518512013-10-14 11:00:02 +0200864 * @chain: the chain
Pablo Neira Ayuso2cbce132015-06-12 13:55:41 +0200865 * @dev_name: device name that this base chain is attached to (if any)
Patrick McHardy96518512013-10-14 11:00:02 +0200866 */
867struct nft_base_chain {
Patrick McHardy115a60b2014-01-03 12:16:15 +0000868 struct nf_hook_ops ops[NFT_HOOK_OPS_MAX];
Patrick McHardy2a37d752014-01-09 18:42:37 +0000869 const struct nf_chain_type *type;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200870 u8 policy;
Pablo Neira Ayuso835b8032015-06-15 12:12:01 +0200871 u8 flags;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200872 struct nft_stats __percpu *stats;
Patrick McHardy96518512013-10-14 11:00:02 +0200873 struct nft_chain chain;
Pablo Neira Ayuso2cbce132015-06-12 13:55:41 +0200874 char dev_name[IFNAMSIZ];
Patrick McHardy96518512013-10-14 11:00:02 +0200875};
876
877static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
878{
879 return container_of(chain, struct nft_base_chain, chain);
880}
881
Pablo Neira Ayuso5ebe0b02015-12-15 19:40:49 +0100882int __nft_release_basechain(struct nft_ctx *ctx);
Pablo Neira Ayuso835b8032015-06-15 12:12:01 +0200883
Eric W. Biederman06198b32015-09-18 14:33:06 -0500884unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
Patrick McHardy96518512013-10-14 11:00:02 +0200885
Patrick McHardy96518512013-10-14 11:00:02 +0200886/**
887 * struct nft_table - nf_tables table
888 *
889 * @list: used internally
890 * @chains: chains in the table
891 * @sets: sets in the table
892 * @hgenerator: handle generator state
893 * @use: number of chain references to this table
894 * @flags: table flag (see enum nft_table_flags)
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +0200895 * @genmask: generation mask
Patrick McHardy96518512013-10-14 11:00:02 +0200896 * @name: name of the table
897 */
898struct nft_table {
899 struct list_head list;
900 struct list_head chains;
901 struct list_head sets;
902 u64 hgenerator;
903 u32 use;
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +0200904 u16 flags:14,
905 genmask:2;
Pablo Neira Ayuso1cae5652015-03-05 15:05:36 +0100906 char name[NFT_TABLE_MAXNAMELEN];
Pablo Neira Ayusoebddf1a2015-05-26 18:41:20 +0200907};
908
909enum nft_af_flags {
910 NFT_AF_NEEDS_DEV = (1 << 0),
Patrick McHardy96518512013-10-14 11:00:02 +0200911};
912
913/**
914 * struct nft_af_info - nf_tables address family info
915 *
916 * @list: used internally
917 * @family: address family
918 * @nhooks: number of hooks in this family
919 * @owner: module owner
920 * @tables: used internally
Pablo Neira Ayusoebddf1a2015-05-26 18:41:20 +0200921 * @flags: family flags
Patrick McHardy115a60b2014-01-03 12:16:15 +0000922 * @nops: number of hook ops in this family
923 * @hook_ops_init: initialization function for chain hook ops
Patrick McHardy96518512013-10-14 11:00:02 +0200924 * @hooks: hookfn overrides for packet validation
925 */
926struct nft_af_info {
927 struct list_head list;
928 int family;
929 unsigned int nhooks;
930 struct module *owner;
931 struct list_head tables;
Pablo Neira Ayusoebddf1a2015-05-26 18:41:20 +0200932 u32 flags;
Patrick McHardy115a60b2014-01-03 12:16:15 +0000933 unsigned int nops;
934 void (*hook_ops_init)(struct nf_hook_ops *,
935 unsigned int);
Patrick McHardy96518512013-10-14 11:00:02 +0200936 nf_hookfn *hooks[NF_MAX_HOOKS];
937};
938
Joe Perches5eccdfa2013-10-19 22:05:31 -0700939int nft_register_afinfo(struct net *, struct nft_af_info *);
Pablo Neira Ayusodf05ef82015-12-15 19:39:32 +0100940void nft_unregister_afinfo(struct net *, struct nft_af_info *);
Patrick McHardy96518512013-10-14 11:00:02 +0200941
Patrick McHardy2a37d752014-01-09 18:42:37 +0000942int nft_register_chain_type(const struct nf_chain_type *);
943void nft_unregister_chain_type(const struct nf_chain_type *);
Patrick McHardy96518512013-10-14 11:00:02 +0200944
Joe Perches5eccdfa2013-10-19 22:05:31 -0700945int nft_register_expr(struct nft_expr_type *);
946void nft_unregister_expr(struct nft_expr_type *);
Patrick McHardy96518512013-10-14 11:00:02 +0200947
Florian Westphal33d5a7b2015-11-28 21:53:04 +0100948int nft_verdict_dump(struct sk_buff *skb, int type,
949 const struct nft_verdict *v);
950
951/**
952 * struct nft_traceinfo - nft tracing information and state
953 *
954 * @pkt: pktinfo currently processed
955 * @basechain: base chain currently processed
956 * @chain: chain currently processed
957 * @rule: rule that was evaluated
958 * @verdict: verdict given by rule
959 * @type: event type (enum nft_trace_types)
960 * @packet_dumped: packet headers sent in a previous traceinfo message
961 * @trace: other struct members are initialised
962 */
963struct nft_traceinfo {
964 const struct nft_pktinfo *pkt;
965 const struct nft_base_chain *basechain;
966 const struct nft_chain *chain;
967 const struct nft_rule *rule;
968 const struct nft_verdict *verdict;
969 enum nft_trace_types type;
970 bool packet_dumped;
971 bool trace;
972};
973
974void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
975 const struct nft_verdict *verdict,
976 const struct nft_chain *basechain);
977
978void nft_trace_notify(struct nft_traceinfo *info);
979
Patrick McHardy67a8fc22014-02-18 18:06:49 +0000980#define nft_dereference(p) \
981 nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
982
Patrick McHardy96518512013-10-14 11:00:02 +0200983#define MODULE_ALIAS_NFT_FAMILY(family) \
984 MODULE_ALIAS("nft-afinfo-" __stringify(family))
985
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200986#define MODULE_ALIAS_NFT_CHAIN(family, name) \
987 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
Patrick McHardy96518512013-10-14 11:00:02 +0200988
Patrick McHardy64d46802014-02-05 15:03:37 +0000989#define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
990 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
991
Patrick McHardy96518512013-10-14 11:00:02 +0200992#define MODULE_ALIAS_NFT_EXPR(name) \
993 MODULE_ALIAS("nft-expr-" name)
994
Patrick McHardy20a69342013-10-11 12:06:22 +0200995#define MODULE_ALIAS_NFT_SET() \
996 MODULE_ALIAS("nft-set")
997
Patrick McHardyea4bd992015-03-25 14:08:49 +0000998/*
999 * The gencursor defines two generations, the currently active and the
1000 * next one. Objects contain a bitmask of 2 bits specifying the generations
1001 * they're active in. A set bit means they're inactive in the generation
1002 * represented by that bit.
1003 *
1004 * New objects start out as inactive in the current and active in the
1005 * next generation. When committing the ruleset the bitmask is cleared,
1006 * meaning they're active in all generations. When removing an object,
1007 * it is set inactive in the next generation. After committing the ruleset,
1008 * the objects are removed.
1009 */
1010static inline unsigned int nft_gencursor_next(const struct net *net)
1011{
1012 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1013}
1014
1015static inline u8 nft_genmask_next(const struct net *net)
1016{
1017 return 1 << nft_gencursor_next(net);
1018}
1019
1020static inline u8 nft_genmask_cur(const struct net *net)
1021{
1022 /* Use ACCESS_ONCE() to prevent refetching the value for atomicity */
1023 return 1 << ACCESS_ONCE(net->nft.gencursor);
1024}
1025
Patrick McHardy22fe54d2015-04-05 14:41:08 +02001026#define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1027
Patrick McHardycc02e452015-03-25 14:08:50 +00001028/*
Pablo Neira Ayuso889f7ee2016-06-12 18:07:07 +02001029 * Generic transaction helpers
1030 */
1031
1032/* Check if this object is currently active. */
1033#define nft_is_active(__net, __obj) \
1034 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1035
1036/* Check if this object is active in the next generation. */
1037#define nft_is_active_next(__net, __obj) \
1038 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1039
1040/* This object becomes active in the next generation. */
1041#define nft_activate_next(__net, __obj) \
1042 (__obj)->genmask = nft_genmask_cur(__net)
1043
1044/* This object becomes inactive in the next generation. */
1045#define nft_deactivate_next(__net, __obj) \
1046 (__obj)->genmask = nft_genmask_next(__net)
1047
1048/* After committing the ruleset, clear the stale generation bit. */
1049#define nft_clear(__net, __obj) \
1050 (__obj)->genmask &= ~nft_genmask_next(__net)
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02001051#define nft_active_genmask(__obj, __genmask) \
1052 !((__obj)->genmask & __genmask)
Pablo Neira Ayuso889f7ee2016-06-12 18:07:07 +02001053
1054/*
Patrick McHardycc02e452015-03-25 14:08:50 +00001055 * Set element transaction helpers
1056 */
1057
1058static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1059 u8 genmask)
1060{
1061 return !(ext->genmask & genmask);
1062}
1063
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +02001064static inline void nft_set_elem_change_active(const struct net *net,
1065 const struct nft_set *set,
Patrick McHardycc02e452015-03-25 14:08:50 +00001066 struct nft_set_ext *ext)
1067{
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +02001068 ext->genmask ^= nft_genmask_next(net);
Patrick McHardycc02e452015-03-25 14:08:50 +00001069}
1070
Patrick McHardy69086652015-03-26 12:39:39 +00001071/*
1072 * We use a free bit in the genmask field to indicate the element
1073 * is busy, meaning it is currently being processed either by
1074 * the netlink API or GC.
1075 *
1076 * Even though the genmask is only a single byte wide, this works
1077 * because the extension structure if fully constant once initialized,
1078 * so there are no non-atomic write accesses unless it is already
1079 * marked busy.
1080 */
1081#define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1082
1083#if defined(__LITTLE_ENDIAN_BITFIELD)
1084#define NFT_SET_ELEM_BUSY_BIT 2
1085#elif defined(__BIG_ENDIAN_BITFIELD)
1086#define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1087#else
1088#error
1089#endif
1090
1091static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1092{
1093 unsigned long *word = (unsigned long *)ext;
1094
1095 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1096 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1097}
1098
1099static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1100{
1101 unsigned long *word = (unsigned long *)ext;
1102
1103 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1104}
1105
Patrick McHardy1a1e1a12015-03-03 20:10:06 +00001106/**
1107 * struct nft_trans - nf_tables object update in transaction
1108 *
1109 * @list: used internally
1110 * @msg_type: message type
1111 * @ctx: transaction context
1112 * @data: internal information related to the transaction
1113 */
1114struct nft_trans {
1115 struct list_head list;
1116 int msg_type;
1117 struct nft_ctx ctx;
1118 char data[0];
1119};
1120
1121struct nft_trans_rule {
1122 struct nft_rule *rule;
1123};
1124
1125#define nft_trans_rule(trans) \
1126 (((struct nft_trans_rule *)trans->data)->rule)
1127
1128struct nft_trans_set {
1129 struct nft_set *set;
1130 u32 set_id;
1131};
1132
1133#define nft_trans_set(trans) \
1134 (((struct nft_trans_set *)trans->data)->set)
1135#define nft_trans_set_id(trans) \
1136 (((struct nft_trans_set *)trans->data)->set_id)
1137
1138struct nft_trans_chain {
1139 bool update;
1140 char name[NFT_CHAIN_MAXNAMELEN];
1141 struct nft_stats __percpu *stats;
1142 u8 policy;
1143};
1144
1145#define nft_trans_chain_update(trans) \
1146 (((struct nft_trans_chain *)trans->data)->update)
1147#define nft_trans_chain_name(trans) \
1148 (((struct nft_trans_chain *)trans->data)->name)
1149#define nft_trans_chain_stats(trans) \
1150 (((struct nft_trans_chain *)trans->data)->stats)
1151#define nft_trans_chain_policy(trans) \
1152 (((struct nft_trans_chain *)trans->data)->policy)
1153
1154struct nft_trans_table {
1155 bool update;
1156 bool enable;
1157};
1158
1159#define nft_trans_table_update(trans) \
1160 (((struct nft_trans_table *)trans->data)->update)
1161#define nft_trans_table_enable(trans) \
1162 (((struct nft_trans_table *)trans->data)->enable)
1163
1164struct nft_trans_elem {
1165 struct nft_set *set;
1166 struct nft_set_elem elem;
1167};
1168
1169#define nft_trans_elem_set(trans) \
1170 (((struct nft_trans_elem *)trans->data)->set)
1171#define nft_trans_elem(trans) \
1172 (((struct nft_trans_elem *)trans->data)->elem)
1173
Patrick McHardy96518512013-10-14 11:00:02 +02001174#endif /* _NET_NF_TABLES_H */