• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
3  *                         Patrick Schaaf <bof@bof.de>
4  *                         Martin Josefsson <gandalf@wlug.westbo.se>
5  * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org>
6  */
7 #ifndef _IP_SET_H
8 #define _IP_SET_H
9 
10 #include <linux/ip.h>
11 #include <linux/ipv6.h>
12 #include <linux/netlink.h>
13 #include <linux/netfilter.h>
14 #include <linux/netfilter/x_tables.h>
15 #include <linux/stringify.h>
16 #include <linux/vmalloc.h>
17 #include <linux/android_kabi.h>
18 #include <net/netlink.h>
19 #include <uapi/linux/netfilter/ipset/ip_set.h>
20 
21 #define _IP_SET_MODULE_DESC(a, b, c)		\
22 	MODULE_DESCRIPTION(a " type of IP sets, revisions " b "-" c)
23 #define IP_SET_MODULE_DESC(a, b, c)		\
24 	_IP_SET_MODULE_DESC(a, __stringify(b), __stringify(c))
25 
26 /* Set features */
27 enum ip_set_feature {
28 	IPSET_TYPE_IP_FLAG = 0,
29 	IPSET_TYPE_IP = (1 << IPSET_TYPE_IP_FLAG),
30 	IPSET_TYPE_PORT_FLAG = 1,
31 	IPSET_TYPE_PORT = (1 << IPSET_TYPE_PORT_FLAG),
32 	IPSET_TYPE_MAC_FLAG = 2,
33 	IPSET_TYPE_MAC = (1 << IPSET_TYPE_MAC_FLAG),
34 	IPSET_TYPE_IP2_FLAG = 3,
35 	IPSET_TYPE_IP2 = (1 << IPSET_TYPE_IP2_FLAG),
36 	IPSET_TYPE_NAME_FLAG = 4,
37 	IPSET_TYPE_NAME = (1 << IPSET_TYPE_NAME_FLAG),
38 	IPSET_TYPE_IFACE_FLAG = 5,
39 	IPSET_TYPE_IFACE = (1 << IPSET_TYPE_IFACE_FLAG),
40 	IPSET_TYPE_MARK_FLAG = 6,
41 	IPSET_TYPE_MARK = (1 << IPSET_TYPE_MARK_FLAG),
42 	IPSET_TYPE_NOMATCH_FLAG = 7,
43 	IPSET_TYPE_NOMATCH = (1 << IPSET_TYPE_NOMATCH_FLAG),
44 	/* Strictly speaking not a feature, but a flag for dumping:
45 	 * this settype must be dumped last */
46 	IPSET_DUMP_LAST_FLAG = 8,
47 	IPSET_DUMP_LAST = (1 << IPSET_DUMP_LAST_FLAG),
48 };
49 
50 /* Set extensions */
51 enum ip_set_extension {
52 	IPSET_EXT_BIT_TIMEOUT = 0,
53 	IPSET_EXT_TIMEOUT = (1 << IPSET_EXT_BIT_TIMEOUT),
54 	IPSET_EXT_BIT_COUNTER = 1,
55 	IPSET_EXT_COUNTER = (1 << IPSET_EXT_BIT_COUNTER),
56 	IPSET_EXT_BIT_COMMENT = 2,
57 	IPSET_EXT_COMMENT = (1 << IPSET_EXT_BIT_COMMENT),
58 	IPSET_EXT_BIT_SKBINFO = 3,
59 	IPSET_EXT_SKBINFO = (1 << IPSET_EXT_BIT_SKBINFO),
60 	/* 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),
63 };
64 
65 #define SET_WITH_TIMEOUT(s)	((s)->extensions & IPSET_EXT_TIMEOUT)
66 #define SET_WITH_COUNTER(s)	((s)->extensions & IPSET_EXT_COUNTER)
67 #define SET_WITH_COMMENT(s)	((s)->extensions & IPSET_EXT_COMMENT)
68 #define SET_WITH_SKBINFO(s)	((s)->extensions & IPSET_EXT_SKBINFO)
69 #define SET_WITH_FORCEADD(s)	((s)->flags & IPSET_CREATE_FLAG_FORCEADD)
70 
71 /* Extension id, in size order */
72 enum ip_set_ext_id {
73 	IPSET_EXT_ID_COUNTER = 0,
74 	IPSET_EXT_ID_TIMEOUT,
75 	IPSET_EXT_ID_SKBINFO,
76 	IPSET_EXT_ID_COMMENT,
77 	IPSET_EXT_ID_MAX,
78 };
79 
80 struct ip_set;
81 
82 /* Extension type */
83 struct ip_set_ext_type {
84 	/* Destroy extension private data (can be NULL) */
85 	void (*destroy)(struct ip_set *set, void *ext);
86 	enum ip_set_extension type;
87 	enum ipset_cadt_flags flag;
88 	/* Size and minimal alignment */
89 	u8 len;
90 	u8 align;
91 };
92 
93 extern const struct ip_set_ext_type ip_set_extensions[];
94 
95 struct ip_set_counter {
96 	atomic64_t bytes;
97 	atomic64_t packets;
98 };
99 
100 struct ip_set_comment_rcu {
101 	struct rcu_head rcu;
102 	char str[];
103 };
104 
105 struct ip_set_comment {
106 	struct ip_set_comment_rcu __rcu *c;
107 };
108 
109 struct ip_set_skbinfo {
110 	u32 skbmark;
111 	u32 skbmarkmask;
112 	u32 skbprio;
113 	u16 skbqueue;
114 	u16 __pad;
115 };
116 
117 struct ip_set_ext {
118 	struct ip_set_skbinfo skbinfo;
119 	u64 packets;
120 	u64 bytes;
121 	char *comment;
122 	u32 timeout;
123 	u8 packets_op;
124 	u8 bytes_op;
125 	bool target;
126 };
127 
128 #define ext_timeout(e, s)	\
129 ((unsigned long *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_TIMEOUT]))
130 #define ext_counter(e, s)	\
131 ((struct ip_set_counter *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COUNTER]))
132 #define ext_comment(e, s)	\
133 ((struct ip_set_comment *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COMMENT]))
134 #define ext_skbinfo(e, s)	\
135 ((struct ip_set_skbinfo *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_SKBINFO]))
136 
137 typedef int (*ipset_adtfn)(struct ip_set *set, void *value,
138 			   const struct ip_set_ext *ext,
139 			   struct ip_set_ext *mext, u32 cmdflags);
140 
141 /* Kernel API function options */
142 struct ip_set_adt_opt {
143 	u8 family;		/* Actual protocol family */
144 	u8 dim;			/* Dimension of match/target */
145 	u8 flags;		/* Direction and negation flags */
146 	u32 cmdflags;		/* Command-like flags */
147 	struct ip_set_ext ext;	/* Extensions */
148 };
149 
150 /* Set type, variant-specific part */
151 struct ip_set_type_variant {
152 	/* Kernelspace: test/add/del entries
153 	 *		returns negative error code,
154 	 *			zero for no match/success to add/delete
155 	 *			positive for matching element */
156 	int (*kadt)(struct ip_set *set, const struct sk_buff *skb,
157 		    const struct xt_action_param *par,
158 		    enum ipset_adt adt, struct ip_set_adt_opt *opt);
159 
160 	/* Userspace: test/add/del entries
161 	 *		returns negative error code,
162 	 *			zero for no match/success to add/delete
163 	 *			positive for matching element */
164 	int (*uadt)(struct ip_set *set, struct nlattr *tb[],
165 		    enum ipset_adt adt, u32 *lineno, u32 flags, bool retried);
166 
167 	/* Low level add/del/test functions */
168 	ipset_adtfn adt[IPSET_ADT_MAX];
169 
170 	/* When adding entries and set is full, try to resize the set */
171 	int (*resize)(struct ip_set *set, bool retried);
172 	/* Destroy the set */
173 	void (*destroy)(struct ip_set *set);
174 	/* Flush the elements */
175 	void (*flush)(struct ip_set *set);
176 	/* Expire entries before listing */
177 	void (*expire)(struct ip_set *set);
178 	/* List set header data */
179 	int (*head)(struct ip_set *set, struct sk_buff *skb);
180 	/* List elements */
181 	int (*list)(const struct ip_set *set, struct sk_buff *skb,
182 		    struct netlink_callback *cb);
183 	/* Keep listing private when resizing runs parallel */
184 	void (*uref)(struct ip_set *set, struct netlink_callback *cb,
185 		     bool start);
186 
187 	/* Return true if "b" set is the same as "a"
188 	 * according to the create set parameters */
189 	bool (*same_set)(const struct ip_set *a, const struct ip_set *b);
190 	/* Cancel ongoing garbage collectors before destroying the set*/
191 	void (*cancel_gc)(struct ip_set *set);
192 	/* Region-locking is used */
193 	bool region_lock;
194 
195 	ANDROID_KABI_RESERVE(1);
196 };
197 
198 struct ip_set_region {
199 	spinlock_t lock;	/* Region lock */
200 	size_t ext_size;	/* Size of the dynamic extensions */
201 	u32 elements;		/* Number of elements vs timeout */
202 };
203 
204 /* Max range where every element is added/deleted in one step */
205 #define IPSET_MAX_RANGE		(1<<14)
206 
207 /* The max revision number supported by any set type + 1 */
208 #define IPSET_REVISION_MAX	9
209 
210 /* The core set type structure */
211 struct ip_set_type {
212 	struct list_head list;
213 
214 	/* Typename */
215 	char name[IPSET_MAXNAMELEN];
216 	/* Protocol version */
217 	u8 protocol;
218 	/* Set type dimension */
219 	u8 dimension;
220 	/*
221 	 * Supported family: may be NFPROTO_UNSPEC for both
222 	 * NFPROTO_IPV4/NFPROTO_IPV6.
223 	 */
224 	u8 family;
225 	/* Type revisions */
226 	u8 revision_min, revision_max;
227 	/* Revision-specific supported (create) flags */
228 	u8 create_flags[IPSET_REVISION_MAX+1];
229 	/* Set features to control swapping */
230 	u16 features;
231 
232 	/* Create set */
233 	int (*create)(struct net *net, struct ip_set *set,
234 		      struct nlattr *tb[], u32 flags);
235 
236 	/* Attribute policies */
237 	const struct nla_policy create_policy[IPSET_ATTR_CREATE_MAX + 1];
238 	const struct nla_policy adt_policy[IPSET_ATTR_ADT_MAX + 1];
239 
240 	/* Set this to THIS_MODULE if you are a module, otherwise NULL */
241 	struct module *me;
242 
243 	ANDROID_KABI_RESERVE(1);
244 };
245 
246 /* register and unregister set type */
247 extern int ip_set_type_register(struct ip_set_type *set_type);
248 extern void ip_set_type_unregister(struct ip_set_type *set_type);
249 
250 /* A generic IP set */
251 struct ip_set {
252 	/* For call_cru in destroy */
253 	struct rcu_head rcu;
254 	/* The name of the set */
255 	char name[IPSET_MAXNAMELEN];
256 	/* Lock protecting the set data */
257 	spinlock_t lock;
258 	/* References to the set */
259 	u32 ref;
260 	/* References to the set for netlink events like dump,
261 	 * ref can be swapped out by ip_set_swap
262 	 */
263 	u32 ref_netlink;
264 	/* The core set type */
265 	struct ip_set_type *type;
266 	/* The type variant doing the real job */
267 	const struct ip_set_type_variant *variant;
268 	/* The actual INET family of the set */
269 	u8 family;
270 	/* The type revision */
271 	u8 revision;
272 	/* Extensions */
273 	u8 extensions;
274 	/* Create flags */
275 	u8 flags;
276 	/* Default timeout value, if enabled */
277 	u32 timeout;
278 	/* Number of elements (vs timeout) */
279 	u32 elements;
280 	/* Size of the dynamic extensions (vs timeout) */
281 	size_t ext_size;
282 	/* Element data size */
283 	size_t dsize;
284 	/* Offsets to extensions in elements */
285 	size_t offset[IPSET_EXT_ID_MAX];
286 	/* The type specific data */
287 	void *data;
288 
289 	ANDROID_KABI_RESERVE(1);
290 };
291 
292 static inline void
ip_set_ext_destroy(struct ip_set * set,void * data)293 ip_set_ext_destroy(struct ip_set *set, void *data)
294 {
295 	/* Check that the extension is enabled for the set and
296 	 * call it's destroy function for its extension part in data.
297 	 */
298 	if (SET_WITH_COMMENT(set)) {
299 		struct ip_set_comment *c = ext_comment(data, set);
300 
301 		ip_set_extensions[IPSET_EXT_ID_COMMENT].destroy(set, c);
302 	}
303 }
304 
305 int ip_set_put_flags(struct sk_buff *skb, struct ip_set *set);
306 
307 /* Netlink CB args */
308 enum {
309 	IPSET_CB_NET = 0,	/* net namespace */
310 	IPSET_CB_PROTO,		/* ipset protocol */
311 	IPSET_CB_DUMP,		/* dump single set/all sets */
312 	IPSET_CB_INDEX,		/* set index */
313 	IPSET_CB_PRIVATE,	/* set private data */
314 	IPSET_CB_ARG0,		/* type specific */
315 };
316 
317 /* register and unregister set references */
318 extern ip_set_id_t ip_set_get_byname(struct net *net,
319 				     const char *name, struct ip_set **set);
320 extern void ip_set_put_byindex(struct net *net, ip_set_id_t index);
321 extern void ip_set_name_byindex(struct net *net, ip_set_id_t index, char *name);
322 extern ip_set_id_t ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index);
323 extern void ip_set_nfnl_put(struct net *net, ip_set_id_t index);
324 
325 /* API for iptables set match, and SET target */
326 
327 extern int ip_set_add(ip_set_id_t id, const struct sk_buff *skb,
328 		      const struct xt_action_param *par,
329 		      struct ip_set_adt_opt *opt);
330 extern int ip_set_del(ip_set_id_t id, const struct sk_buff *skb,
331 		      const struct xt_action_param *par,
332 		      struct ip_set_adt_opt *opt);
333 extern int ip_set_test(ip_set_id_t id, const struct sk_buff *skb,
334 		       const struct xt_action_param *par,
335 		       struct ip_set_adt_opt *opt);
336 
337 /* Utility functions */
338 extern void *ip_set_alloc(size_t size);
339 extern void ip_set_free(void *members);
340 extern int ip_set_get_ipaddr4(struct nlattr *nla,  __be32 *ipaddr);
341 extern int ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr);
342 extern size_t ip_set_elem_len(struct ip_set *set, struct nlattr *tb[],
343 			      size_t len, size_t align);
344 extern int ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
345 				 struct ip_set_ext *ext);
346 extern int ip_set_put_extensions(struct sk_buff *skb, const struct ip_set *set,
347 				 const void *e, bool active);
348 extern bool ip_set_match_extensions(struct ip_set *set,
349 				    const struct ip_set_ext *ext,
350 				    struct ip_set_ext *mext,
351 				    u32 flags, void *data);
352 
353 static inline int
ip_set_get_hostipaddr4(struct nlattr * nla,u32 * ipaddr)354 ip_set_get_hostipaddr4(struct nlattr *nla, u32 *ipaddr)
355 {
356 	__be32 ip;
357 	int ret = ip_set_get_ipaddr4(nla, &ip);
358 
359 	if (ret)
360 		return ret;
361 	*ipaddr = ntohl(ip);
362 	return 0;
363 }
364 
365 /* Ignore IPSET_ERR_EXIST errors if asked to do so? */
366 static inline bool
ip_set_eexist(int ret,u32 flags)367 ip_set_eexist(int ret, u32 flags)
368 {
369 	return ret == -IPSET_ERR_EXIST && (flags & IPSET_FLAG_EXIST);
370 }
371 
372 /* Match elements marked with nomatch */
373 static inline bool
ip_set_enomatch(int ret,u32 flags,enum ipset_adt adt,struct ip_set * set)374 ip_set_enomatch(int ret, u32 flags, enum ipset_adt adt, struct ip_set *set)
375 {
376 	return adt == IPSET_TEST &&
377 	       (set->type->features & IPSET_TYPE_NOMATCH) &&
378 	       ((flags >> 16) & IPSET_FLAG_NOMATCH) &&
379 	       (ret > 0 || ret == -ENOTEMPTY);
380 }
381 
382 /* Check the NLA_F_NET_BYTEORDER flag */
383 static inline bool
ip_set_attr_netorder(struct nlattr * tb[],int type)384 ip_set_attr_netorder(struct nlattr *tb[], int type)
385 {
386 	return tb[type] && (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
387 }
388 
389 static inline bool
ip_set_optattr_netorder(struct nlattr * tb[],int type)390 ip_set_optattr_netorder(struct nlattr *tb[], int type)
391 {
392 	return !tb[type] || (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
393 }
394 
395 /* Useful converters */
396 static inline u32
ip_set_get_h32(const struct nlattr * attr)397 ip_set_get_h32(const struct nlattr *attr)
398 {
399 	return ntohl(nla_get_be32(attr));
400 }
401 
402 static inline u16
ip_set_get_h16(const struct nlattr * attr)403 ip_set_get_h16(const struct nlattr *attr)
404 {
405 	return ntohs(nla_get_be16(attr));
406 }
407 
nla_put_ipaddr4(struct sk_buff * skb,int type,__be32 ipaddr)408 static inline int nla_put_ipaddr4(struct sk_buff *skb, int type, __be32 ipaddr)
409 {
410 	struct nlattr *__nested = nla_nest_start(skb, type);
411 	int ret;
412 
413 	if (!__nested)
414 		return -EMSGSIZE;
415 	ret = nla_put_in_addr(skb, IPSET_ATTR_IPADDR_IPV4, ipaddr);
416 	if (!ret)
417 		nla_nest_end(skb, __nested);
418 	return ret;
419 }
420 
nla_put_ipaddr6(struct sk_buff * skb,int type,const struct in6_addr * ipaddrptr)421 static inline int nla_put_ipaddr6(struct sk_buff *skb, int type,
422 				  const struct in6_addr *ipaddrptr)
423 {
424 	struct nlattr *__nested = nla_nest_start(skb, type);
425 	int ret;
426 
427 	if (!__nested)
428 		return -EMSGSIZE;
429 	ret = nla_put_in6_addr(skb, IPSET_ATTR_IPADDR_IPV6, ipaddrptr);
430 	if (!ret)
431 		nla_nest_end(skb, __nested);
432 	return ret;
433 }
434 
435 /* Get address from skbuff */
436 static inline __be32
ip4addr(const struct sk_buff * skb,bool src)437 ip4addr(const struct sk_buff *skb, bool src)
438 {
439 	return src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
440 }
441 
442 static inline void
ip4addrptr(const struct sk_buff * skb,bool src,__be32 * addr)443 ip4addrptr(const struct sk_buff *skb, bool src, __be32 *addr)
444 {
445 	*addr = src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
446 }
447 
448 static inline void
ip6addrptr(const struct sk_buff * skb,bool src,struct in6_addr * addr)449 ip6addrptr(const struct sk_buff *skb, bool src, struct in6_addr *addr)
450 {
451 	memcpy(addr, src ? &ipv6_hdr(skb)->saddr : &ipv6_hdr(skb)->daddr,
452 	       sizeof(*addr));
453 }
454 
455 /* How often should the gc be run by default */
456 #define IPSET_GC_TIME			(3 * 60)
457 
458 /* Timeout period depending on the timeout value of the given set */
459 #define IPSET_GC_PERIOD(timeout) \
460 	((timeout/3) ? min_t(u32, (timeout)/3, IPSET_GC_TIME) : 1)
461 
462 /* Entry is set with no timeout value */
463 #define IPSET_ELEM_PERMANENT	0
464 
465 /* Set is defined with timeout support: timeout value may be 0 */
466 #define IPSET_NO_TIMEOUT	UINT_MAX
467 
468 /* Max timeout value, see msecs_to_jiffies() in jiffies.h */
469 #define IPSET_MAX_TIMEOUT	(UINT_MAX >> 1)/MSEC_PER_SEC
470 
471 #define ip_set_adt_opt_timeout(opt, set)	\
472 ((opt)->ext.timeout != IPSET_NO_TIMEOUT ? (opt)->ext.timeout : (set)->timeout)
473 
474 static inline unsigned int
ip_set_timeout_uget(struct nlattr * tb)475 ip_set_timeout_uget(struct nlattr *tb)
476 {
477 	unsigned int timeout = ip_set_get_h32(tb);
478 
479 	/* Normalize to fit into jiffies */
480 	if (timeout > IPSET_MAX_TIMEOUT)
481 		timeout = IPSET_MAX_TIMEOUT;
482 
483 	return timeout;
484 }
485 
486 static inline bool
ip_set_timeout_expired(const unsigned long * t)487 ip_set_timeout_expired(const unsigned long *t)
488 {
489 	return *t != IPSET_ELEM_PERMANENT && time_is_before_jiffies(*t);
490 }
491 
492 static inline void
ip_set_timeout_set(unsigned long * timeout,u32 value)493 ip_set_timeout_set(unsigned long *timeout, u32 value)
494 {
495 	unsigned long t;
496 
497 	if (!value) {
498 		*timeout = IPSET_ELEM_PERMANENT;
499 		return;
500 	}
501 
502 	t = msecs_to_jiffies(value * MSEC_PER_SEC) + jiffies;
503 	if (t == IPSET_ELEM_PERMANENT)
504 		/* Bingo! :-) */
505 		t--;
506 	*timeout = t;
507 }
508 
509 void ip_set_init_comment(struct ip_set *set, struct ip_set_comment *comment,
510 			 const struct ip_set_ext *ext);
511 
512 static inline void
ip_set_init_counter(struct ip_set_counter * counter,const struct ip_set_ext * ext)513 ip_set_init_counter(struct ip_set_counter *counter,
514 		    const struct ip_set_ext *ext)
515 {
516 	if (ext->bytes != ULLONG_MAX)
517 		atomic64_set(&(counter)->bytes, (long long)(ext->bytes));
518 	if (ext->packets != ULLONG_MAX)
519 		atomic64_set(&(counter)->packets, (long long)(ext->packets));
520 }
521 
522 static inline void
ip_set_init_skbinfo(struct ip_set_skbinfo * skbinfo,const struct ip_set_ext * ext)523 ip_set_init_skbinfo(struct ip_set_skbinfo *skbinfo,
524 		    const struct ip_set_ext *ext)
525 {
526 	*skbinfo = ext->skbinfo;
527 }
528 
529 #define IP_SET_INIT_KEXT(skb, opt, set)			\
530 	{ .bytes = (skb)->len, .packets = 1, .target = true,\
531 	  .timeout = ip_set_adt_opt_timeout(opt, set) }
532 
533 #define IP_SET_INIT_UEXT(set)				\
534 	{ .bytes = ULLONG_MAX, .packets = ULLONG_MAX,	\
535 	  .timeout = (set)->timeout }
536 
537 #define IPSET_CONCAT(a, b)		a##b
538 #define IPSET_TOKEN(a, b)		IPSET_CONCAT(a, b)
539 
540 #endif /*_IP_SET_H */
541