blob: f476bcec25eaa2925395aca566e18bb1b24e8084 [file] [log] [blame]
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +01001/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
2 * Patrick Schaaf <bof@bof.de>
3 * Martin Josefsson <gandalf@wlug.westbo.se>
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +02004 * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +01005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
David Howellsa8201412012-10-09 09:48:55 +010010#ifndef _IP_SET_H
11#define _IP_SET_H
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +010012
Jan Engelhardtae8ded12012-01-14 16:26:20 +010013#include <linux/ip.h>
14#include <linux/ipv6.h>
15#include <linux/netlink.h>
16#include <linux/netfilter.h>
17#include <linux/netfilter/x_tables.h>
Jozsef Kadlecsik10111a62012-09-21 21:59:32 +020018#include <linux/stringify.h>
Jan Engelhardtae8ded12012-01-14 16:26:20 +010019#include <linux/vmalloc.h>
20#include <net/netlink.h>
David Howellsa8201412012-10-09 09:48:55 +010021#include <uapi/linux/netfilter/ipset/ip_set.h>
Jan Engelhardtae8ded12012-01-14 16:26:20 +010022
Jozsef Kadlecsik10111a62012-09-21 21:59:32 +020023#define _IP_SET_MODULE_DESC(a, b, c) \
24 MODULE_DESCRIPTION(a " type of IP sets, revisions " b "-" c)
25#define IP_SET_MODULE_DESC(a, b, c) \
26 _IP_SET_MODULE_DESC(a, __stringify(b), __stringify(c))
27
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +010028/* Set features */
29enum ip_set_feature {
30 IPSET_TYPE_IP_FLAG = 0,
31 IPSET_TYPE_IP = (1 << IPSET_TYPE_IP_FLAG),
32 IPSET_TYPE_PORT_FLAG = 1,
33 IPSET_TYPE_PORT = (1 << IPSET_TYPE_PORT_FLAG),
34 IPSET_TYPE_MAC_FLAG = 2,
35 IPSET_TYPE_MAC = (1 << IPSET_TYPE_MAC_FLAG),
36 IPSET_TYPE_IP2_FLAG = 3,
37 IPSET_TYPE_IP2 = (1 << IPSET_TYPE_IP2_FLAG),
38 IPSET_TYPE_NAME_FLAG = 4,
39 IPSET_TYPE_NAME = (1 << IPSET_TYPE_NAME_FLAG),
Jozsef Kadlecsike3853572011-06-16 19:00:48 +020040 IPSET_TYPE_IFACE_FLAG = 5,
41 IPSET_TYPE_IFACE = (1 << IPSET_TYPE_IFACE_FLAG),
Vytas Dauksa3b02b562013-12-17 14:01:43 +000042 IPSET_TYPE_MARK_FLAG = 6,
43 IPSET_TYPE_MARK = (1 << IPSET_TYPE_MARK_FLAG),
44 IPSET_TYPE_NOMATCH_FLAG = 7,
Jozsef Kadlecsik3e0304a2012-09-21 22:02:36 +020045 IPSET_TYPE_NOMATCH = (1 << IPSET_TYPE_NOMATCH_FLAG),
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +010046 /* Strictly speaking not a feature, but a flag for dumping:
47 * this settype must be dumped last */
Vytas Dauksa3b02b562013-12-17 14:01:43 +000048 IPSET_DUMP_LAST_FLAG = 8,
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +010049 IPSET_DUMP_LAST = (1 << IPSET_DUMP_LAST_FLAG),
50};
51
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +020052/* Set extensions */
53enum ip_set_extension {
Jozsef Kadlecsik40cd63b2013-09-09 14:44:29 +020054 IPSET_EXT_BIT_TIMEOUT = 0,
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +020055 IPSET_EXT_TIMEOUT = (1 << IPSET_EXT_BIT_TIMEOUT),
Jozsef Kadlecsik40cd63b2013-09-09 14:44:29 +020056 IPSET_EXT_BIT_COUNTER = 1,
Jozsef Kadlecsik34d666d2013-04-27 14:38:56 +020057 IPSET_EXT_COUNTER = (1 << IPSET_EXT_BIT_COUNTER),
Oliver Smith68b63f02013-09-22 20:56:30 +020058 IPSET_EXT_BIT_COMMENT = 2,
59 IPSET_EXT_COMMENT = (1 << IPSET_EXT_BIT_COMMENT),
Jozsef Kadlecsik40cd63b2013-09-09 14:44:29 +020060 /* Mark set with an extension which needs to call destroy */
61 IPSET_EXT_BIT_DESTROY = 7,
62 IPSET_EXT_DESTROY = (1 << IPSET_EXT_BIT_DESTROY),
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +020063};
64
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +020065#define SET_WITH_TIMEOUT(s) ((s)->extensions & IPSET_EXT_TIMEOUT)
Jozsef Kadlecsik34d666d2013-04-27 14:38:56 +020066#define SET_WITH_COUNTER(s) ((s)->extensions & IPSET_EXT_COUNTER)
Oliver Smith68b63f02013-09-22 20:56:30 +020067#define SET_WITH_COMMENT(s) ((s)->extensions & IPSET_EXT_COMMENT)
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +020068
Jozsef Kadlecsikf925f702013-09-06 22:31:40 +020069/* Extension id, in size order */
70enum ip_set_ext_id {
71 IPSET_EXT_ID_COUNTER = 0,
72 IPSET_EXT_ID_TIMEOUT,
Oliver Smith68b63f02013-09-22 20:56:30 +020073 IPSET_EXT_ID_COMMENT,
Jozsef Kadlecsikf925f702013-09-06 22:31:40 +020074 IPSET_EXT_ID_MAX,
75};
76
Jozsef Kadlecsik03c8b232013-09-07 00:43:52 +020077/* Extension type */
78struct ip_set_ext_type {
Jozsef Kadlecsik40cd63b2013-09-09 14:44:29 +020079 /* Destroy extension private data (can be NULL) */
80 void (*destroy)(void *ext);
Jozsef Kadlecsik03c8b232013-09-07 00:43:52 +020081 enum ip_set_extension type;
82 enum ipset_cadt_flags flag;
83 /* Size and minimal alignment */
84 u8 len;
85 u8 align;
86};
87
88extern const struct ip_set_ext_type ip_set_extensions[];
89
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +020090struct ip_set_ext {
Jozsef Kadlecsik34d666d2013-04-27 14:38:56 +020091 u64 packets;
92 u64 bytes;
Jozsef Kadlecsikf925f702013-09-06 22:31:40 +020093 u32 timeout;
Oliver Smith68b63f02013-09-22 20:56:30 +020094 char *comment;
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +020095};
96
Jozsef Kadlecsikca134ce2013-09-07 00:10:07 +020097struct ip_set_counter {
98 atomic64_t bytes;
99 atomic64_t packets;
100};
101
Oliver Smith68b63f02013-09-22 20:56:30 +0200102struct ip_set_comment {
103 char *str;
104};
Jozsef Kadlecsik40cd63b2013-09-09 14:44:29 +0200105
Oliver Smith68b63f02013-09-22 20:56:30 +0200106struct ip_set;
Jozsef Kadlecsik40cd63b2013-09-09 14:44:29 +0200107
Jozsef Kadlecsikca134ce2013-09-07 00:10:07 +0200108#define ext_timeout(e, s) \
109(unsigned long *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_TIMEOUT])
110#define ext_counter(e, s) \
111(struct ip_set_counter *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COUNTER])
Oliver Smith68b63f02013-09-22 20:56:30 +0200112#define ext_comment(e, s) \
113(struct ip_set_comment *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COMMENT])
114
Jozsef Kadlecsikca134ce2013-09-07 00:10:07 +0200115
Jozsef Kadlecsik54162192011-06-16 18:40:55 +0200116typedef int (*ipset_adtfn)(struct ip_set *set, void *value,
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +0200117 const struct ip_set_ext *ext,
Jozsef Kadlecsik6e017812013-04-27 14:40:50 +0200118 struct ip_set_ext *mext, u32 cmdflags);
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100119
Jozsef Kadlecsikac8cc922011-06-16 18:42:40 +0200120/* Kernel API function options */
121struct ip_set_adt_opt {
122 u8 family; /* Actual protocol family */
123 u8 dim; /* Dimension of match/target */
124 u8 flags; /* Direction and negation flags */
125 u32 cmdflags; /* Command-like flags */
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +0200126 struct ip_set_ext ext; /* Extensions */
Jozsef Kadlecsikac8cc922011-06-16 18:42:40 +0200127};
128
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100129/* Set type, variant-specific part */
130struct ip_set_type_variant {
131 /* Kernelspace: test/add/del entries
132 * returns negative error code,
133 * zero for no match/success to add/delete
134 * positive for matching element */
Jozsef Kadlecsik3ace95c2012-09-21 22:01:45 +0200135 int (*kadt)(struct ip_set *set, const struct sk_buff *skb,
Jozsef Kadlecsikb66554c2011-06-16 18:56:47 +0200136 const struct xt_action_param *par,
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +0200137 enum ipset_adt adt, struct ip_set_adt_opt *opt);
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100138
139 /* Userspace: test/add/del entries
140 * returns negative error code,
141 * zero for no match/success to add/delete
142 * positive for matching element */
143 int (*uadt)(struct ip_set *set, struct nlattr *tb[],
Jozsef Kadlecsik3d14b172011-06-16 18:49:17 +0200144 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried);
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100145
146 /* Low level add/del/test functions */
147 ipset_adtfn adt[IPSET_ADT_MAX];
148
149 /* When adding entries and set is full, try to resize the set */
150 int (*resize)(struct ip_set *set, bool retried);
151 /* Destroy the set */
152 void (*destroy)(struct ip_set *set);
153 /* Flush the elements */
154 void (*flush)(struct ip_set *set);
155 /* Expire entries before listing */
156 void (*expire)(struct ip_set *set);
157 /* List set header data */
158 int (*head)(struct ip_set *set, struct sk_buff *skb);
159 /* List elements */
160 int (*list)(const struct ip_set *set, struct sk_buff *skb,
161 struct netlink_callback *cb);
162
163 /* Return true if "b" set is the same as "a"
164 * according to the create set parameters */
165 bool (*same_set)(const struct ip_set *a, const struct ip_set *b);
166};
167
168/* The core set type structure */
169struct ip_set_type {
170 struct list_head list;
171
172 /* Typename */
173 char name[IPSET_MAXNAMELEN];
174 /* Protocol version */
175 u8 protocol;
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100176 /* Set type dimension */
177 u8 dimension;
Jan Engelhardtc15f1c82012-02-14 00:24:10 +0100178 /*
179 * Supported family: may be NFPROTO_UNSPEC for both
180 * NFPROTO_IPV4/NFPROTO_IPV6.
181 */
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100182 u8 family;
Jozsef Kadlecsikf1e00b32011-06-16 18:51:41 +0200183 /* Type revisions */
184 u8 revision_min, revision_max;
Vytas Dauksa3b02b562013-12-17 14:01:43 +0000185 /* Set features to control swapping */
186 u16 features;
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100187
188 /* Create set */
Vitaly Lavrov1785e8f2013-09-30 17:07:02 +0200189 int (*create)(struct net *net, struct ip_set *set,
190 struct nlattr *tb[], u32 flags);
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100191
192 /* Attribute policies */
193 const struct nla_policy create_policy[IPSET_ATTR_CREATE_MAX + 1];
194 const struct nla_policy adt_policy[IPSET_ATTR_ADT_MAX + 1];
195
196 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
197 struct module *me;
198};
199
200/* register and unregister set type */
201extern int ip_set_type_register(struct ip_set_type *set_type);
202extern void ip_set_type_unregister(struct ip_set_type *set_type);
203
204/* A generic IP set */
205struct ip_set {
206 /* The name of the set */
207 char name[IPSET_MAXNAMELEN];
208 /* Lock protecting the set data */
209 rwlock_t lock;
210 /* References to the set */
Jozsef Kadlecsik2f9f28b2011-04-04 15:19:25 +0200211 u32 ref;
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100212 /* The core set type */
213 struct ip_set_type *type;
214 /* The type variant doing the real job */
215 const struct ip_set_type_variant *variant;
216 /* The actual INET family of the set */
217 u8 family;
Jozsef Kadlecsikf1e00b32011-06-16 18:51:41 +0200218 /* The type revision */
219 u8 revision;
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +0200220 /* Extensions */
221 u8 extensions;
Jozsef Kadlecsikaf284ec2014-02-13 12:19:56 +0100222 /* Create flags */
223 u8 flags;
Jozsef Kadlecsikca134ce2013-09-07 00:10:07 +0200224 /* Default timeout value, if enabled */
225 u32 timeout;
226 /* Element data size */
227 size_t dsize;
228 /* Offsets to extensions in elements */
229 size_t offset[IPSET_EXT_ID_MAX];
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100230 /* The type specific data */
231 void *data;
232};
233
Jozsef Kadlecsik34d666d2013-04-27 14:38:56 +0200234static inline void
Oliver Smith68b63f02013-09-22 20:56:30 +0200235ip_set_ext_destroy(struct ip_set *set, void *data)
236{
237 /* Check that the extension is enabled for the set and
238 * call it's destroy function for its extension part in data.
239 */
240 if (SET_WITH_COMMENT(set))
241 ip_set_extensions[IPSET_EXT_ID_COMMENT].destroy(
242 ext_comment(data, set));
243}
244
245static inline int
246ip_set_put_flags(struct sk_buff *skb, struct ip_set *set)
247{
248 u32 cadt_flags = 0;
249
250 if (SET_WITH_TIMEOUT(set))
251 if (unlikely(nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
252 htonl(set->timeout))))
253 return -EMSGSIZE;
254 if (SET_WITH_COUNTER(set))
255 cadt_flags |= IPSET_FLAG_WITH_COUNTERS;
256 if (SET_WITH_COMMENT(set))
257 cadt_flags |= IPSET_FLAG_WITH_COMMENT;
258
259 if (!cadt_flags)
260 return 0;
261 return nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(cadt_flags));
262}
263
264static inline void
Jozsef Kadlecsik34d666d2013-04-27 14:38:56 +0200265ip_set_add_bytes(u64 bytes, struct ip_set_counter *counter)
266{
267 atomic64_add((long long)bytes, &(counter)->bytes);
268}
269
270static inline void
271ip_set_add_packets(u64 packets, struct ip_set_counter *counter)
272{
273 atomic64_add((long long)packets, &(counter)->packets);
274}
275
276static inline u64
277ip_set_get_bytes(const struct ip_set_counter *counter)
278{
279 return (u64)atomic64_read(&(counter)->bytes);
280}
281
282static inline u64
283ip_set_get_packets(const struct ip_set_counter *counter)
284{
285 return (u64)atomic64_read(&(counter)->packets);
286}
287
288static inline void
289ip_set_update_counter(struct ip_set_counter *counter,
290 const struct ip_set_ext *ext,
291 struct ip_set_ext *mext, u32 flags)
292{
Jozsef Kadlecsik6e017812013-04-27 14:40:50 +0200293 if (ext->packets != ULLONG_MAX &&
294 !(flags & IPSET_FLAG_SKIP_COUNTER_UPDATE)) {
Jozsef Kadlecsik34d666d2013-04-27 14:38:56 +0200295 ip_set_add_bytes(ext->bytes, counter);
296 ip_set_add_packets(ext->packets, counter);
297 }
Jozsef Kadlecsik6e017812013-04-27 14:40:50 +0200298 if (flags & IPSET_FLAG_MATCH_COUNTERS) {
299 mext->packets = ip_set_get_packets(counter);
300 mext->bytes = ip_set_get_bytes(counter);
301 }
Jozsef Kadlecsik34d666d2013-04-27 14:38:56 +0200302}
303
304static inline bool
305ip_set_put_counter(struct sk_buff *skb, struct ip_set_counter *counter)
306{
307 return nla_put_net64(skb, IPSET_ATTR_BYTES,
308 cpu_to_be64(ip_set_get_bytes(counter))) ||
309 nla_put_net64(skb, IPSET_ATTR_PACKETS,
310 cpu_to_be64(ip_set_get_packets(counter)));
311}
312
313static inline void
314ip_set_init_counter(struct ip_set_counter *counter,
315 const struct ip_set_ext *ext)
316{
317 if (ext->bytes != ULLONG_MAX)
318 atomic64_set(&(counter)->bytes, (long long)(ext->bytes));
319 if (ext->packets != ULLONG_MAX)
320 atomic64_set(&(counter)->packets, (long long)(ext->packets));
321}
322
Jozsef Kadlecsik93302882013-10-18 11:41:55 +0200323/* Netlink CB args */
324enum {
325 IPSET_CB_NET = 0,
326 IPSET_CB_DUMP,
327 IPSET_CB_INDEX,
328 IPSET_CB_ARG0,
329 IPSET_CB_ARG1,
330 IPSET_CB_ARG2,
331};
332
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100333/* register and unregister set references */
Vitaly Lavrov1785e8f2013-09-30 17:07:02 +0200334extern ip_set_id_t ip_set_get_byname(struct net *net,
335 const char *name, struct ip_set **set);
336extern void ip_set_put_byindex(struct net *net, ip_set_id_t index);
337extern const char *ip_set_name_byindex(struct net *net, ip_set_id_t index);
Vitaly Lavrov1785e8f2013-09-30 17:07:02 +0200338extern ip_set_id_t ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index);
339extern void ip_set_nfnl_put(struct net *net, ip_set_id_t index);
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100340
341/* API for iptables set match, and SET target */
Jozsef Kadlecsikac8cc922011-06-16 18:42:40 +0200342
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100343extern int ip_set_add(ip_set_id_t id, const struct sk_buff *skb,
Jozsef Kadlecsikb66554c2011-06-16 18:56:47 +0200344 const struct xt_action_param *par,
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +0200345 struct ip_set_adt_opt *opt);
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100346extern int ip_set_del(ip_set_id_t id, const struct sk_buff *skb,
Jozsef Kadlecsikb66554c2011-06-16 18:56:47 +0200347 const struct xt_action_param *par,
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +0200348 struct ip_set_adt_opt *opt);
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100349extern int ip_set_test(ip_set_id_t id, const struct sk_buff *skb,
Jozsef Kadlecsikb66554c2011-06-16 18:56:47 +0200350 const struct xt_action_param *par,
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +0200351 struct ip_set_adt_opt *opt);
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100352
353/* Utility functions */
Jozsef Kadlecsik15b4d932011-06-16 19:01:26 +0200354extern void *ip_set_alloc(size_t size);
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100355extern void ip_set_free(void *members);
356extern int ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr);
357extern int ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr);
Jozsef Kadlecsik03c8b232013-09-07 00:43:52 +0200358extern size_t ip_set_elem_len(struct ip_set *set, struct nlattr *tb[],
359 size_t len);
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +0200360extern int ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
361 struct ip_set_ext *ext);
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100362
363static inline int
364ip_set_get_hostipaddr4(struct nlattr *nla, u32 *ipaddr)
365{
366 __be32 ip;
367 int ret = ip_set_get_ipaddr4(nla, &ip);
Jozsef Kadlecsik15b4d932011-06-16 19:01:26 +0200368
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100369 if (ret)
370 return ret;
371 *ipaddr = ntohl(ip);
372 return 0;
373}
374
375/* Ignore IPSET_ERR_EXIST errors if asked to do so? */
376static inline bool
377ip_set_eexist(int ret, u32 flags)
378{
379 return ret == -IPSET_ERR_EXIST && (flags & IPSET_FLAG_EXIST);
380}
381
Jozsef Kadlecsik43c56e52013-04-08 21:51:25 +0200382/* Match elements marked with nomatch */
383static inline bool
Jozsef Kadlecsik0f1799b2013-09-16 20:04:53 +0200384ip_set_enomatch(int ret, u32 flags, enum ipset_adt adt, struct ip_set *set)
Jozsef Kadlecsik43c56e52013-04-08 21:51:25 +0200385{
386 return adt == IPSET_TEST &&
Jozsef Kadlecsik0f1799b2013-09-16 20:04:53 +0200387 (set->type->features & IPSET_TYPE_NOMATCH) &&
388 ((flags >> 16) & IPSET_FLAG_NOMATCH) &&
389 (ret > 0 || ret == -ENOTEMPTY);
Jozsef Kadlecsik43c56e52013-04-08 21:51:25 +0200390}
391
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100392/* Check the NLA_F_NET_BYTEORDER flag */
393static inline bool
394ip_set_attr_netorder(struct nlattr *tb[], int type)
395{
396 return tb[type] && (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
397}
398
399static inline bool
400ip_set_optattr_netorder(struct nlattr *tb[], int type)
401{
402 return !tb[type] || (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
403}
404
405/* Useful converters */
406static inline u32
407ip_set_get_h32(const struct nlattr *attr)
408{
409 return ntohl(nla_get_be32(attr));
410}
411
412static inline u16
413ip_set_get_h16(const struct nlattr *attr)
414{
415 return ntohs(nla_get_be16(attr));
416}
417
418#define ipset_nest_start(skb, attr) nla_nest_start(skb, attr | NLA_F_NESTED)
419#define ipset_nest_end(skb, start) nla_nest_end(skb, start)
420
David S. Miller7cf78992012-04-01 19:54:46 -0400421static inline int nla_put_ipaddr4(struct sk_buff *skb, int type, __be32 ipaddr)
422{
423 struct nlattr *__nested = ipset_nest_start(skb, type);
424 int ret;
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100425
David S. Miller7cf78992012-04-01 19:54:46 -0400426 if (!__nested)
427 return -EMSGSIZE;
428 ret = nla_put_net32(skb, IPSET_ATTR_IPADDR_IPV4, ipaddr);
429 if (!ret)
430 ipset_nest_end(skb, __nested);
431 return ret;
432}
433
Jozsef Kadlecsik3ace95c2012-09-21 22:01:45 +0200434static inline int nla_put_ipaddr6(struct sk_buff *skb, int type,
435 const struct in6_addr *ipaddrptr)
David S. Miller7cf78992012-04-01 19:54:46 -0400436{
437 struct nlattr *__nested = ipset_nest_start(skb, type);
438 int ret;
439
440 if (!__nested)
441 return -EMSGSIZE;
442 ret = nla_put(skb, IPSET_ATTR_IPADDR_IPV6,
443 sizeof(struct in6_addr), ipaddrptr);
444 if (!ret)
445 ipset_nest_end(skb, __nested);
446 return ret;
447}
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100448
449/* Get address from skbuff */
450static inline __be32
451ip4addr(const struct sk_buff *skb, bool src)
452{
453 return src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
454}
455
456static inline void
457ip4addrptr(const struct sk_buff *skb, bool src, __be32 *addr)
458{
459 *addr = src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
460}
461
462static inline void
463ip6addrptr(const struct sk_buff *skb, bool src, struct in6_addr *addr)
464{
465 memcpy(addr, src ? &ipv6_hdr(skb)->saddr : &ipv6_hdr(skb)->daddr,
466 sizeof(*addr));
467}
468
469/* Calculate the bytes required to store the inclusive range of a-b */
470static inline int
471bitmap_bytes(u32 a, u32 b)
472{
473 return 4 * ((((b - a + 8) / 8) + 3) / 4);
474}
475
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +0200476#include <linux/netfilter/ipset/ip_set_timeout.h>
Oliver Smith68b63f02013-09-22 20:56:30 +0200477#include <linux/netfilter/ipset/ip_set_comment.h>
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +0200478
Jozsef Kadlecsik3fd986b2013-09-25 17:44:35 +0200479static inline int
480ip_set_put_extensions(struct sk_buff *skb, const struct ip_set *set,
481 const void *e, bool active)
482{
483 if (SET_WITH_TIMEOUT(set)) {
484 unsigned long *timeout = ext_timeout(e, set);
485
486 if (nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
487 htonl(active ? ip_set_timeout_get(timeout)
488 : *timeout)))
489 return -EMSGSIZE;
490 }
491 if (SET_WITH_COUNTER(set) &&
492 ip_set_put_counter(skb, ext_counter(e, set)))
493 return -EMSGSIZE;
494 if (SET_WITH_COMMENT(set) &&
495 ip_set_put_comment(skb, ext_comment(e, set)))
496 return -EMSGSIZE;
497 return 0;
498}
499
Jozsef Kadlecsikca134ce2013-09-07 00:10:07 +0200500#define IP_SET_INIT_KEXT(skb, opt, set) \
Jozsef Kadlecsik34d666d2013-04-27 14:38:56 +0200501 { .bytes = (skb)->len, .packets = 1, \
Jozsef Kadlecsikca134ce2013-09-07 00:10:07 +0200502 .timeout = ip_set_adt_opt_timeout(opt, set) }
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +0200503
Jozsef Kadlecsikca134ce2013-09-07 00:10:07 +0200504#define IP_SET_INIT_UEXT(set) \
Jozsef Kadlecsik34d666d2013-04-27 14:38:56 +0200505 { .bytes = ULLONG_MAX, .packets = ULLONG_MAX, \
Jozsef Kadlecsikca134ce2013-09-07 00:10:07 +0200506 .timeout = (set)->timeout }
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +0200507
Jozsef Kadlecsika04d8b62013-09-30 09:05:54 +0200508#define IP_SET_INIT_CIDR(a, b) ((a) ? (a) : (b))
509
Jozsef Kadlecsik35b8dcf2013-04-30 23:02:43 +0200510#define IPSET_CONCAT(a, b) a##b
511#define IPSET_TOKEN(a, b) IPSET_CONCAT(a, b)
512
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100513#endif /*_IP_SET_H */