1 /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
2 * Patrick Schaaf <bof@bof.de>
3 * Martin Josefsson <gandalf@wlug.westbo.se>
4 * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
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.
14 #include <linux/ipv6.h>
15 #include <linux/netlink.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/x_tables.h>
18 #include <linux/stringify.h>
19 #include <linux/vmalloc.h>
20 #include <net/netlink.h>
21 #include <uapi/linux/netfilter/ipset/ip_set.h>
23 #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))
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
),
40 IPSET_TYPE_IFACE_FLAG
= 5,
41 IPSET_TYPE_IFACE
= (1 << IPSET_TYPE_IFACE_FLAG
),
42 IPSET_TYPE_MARK_FLAG
= 6,
43 IPSET_TYPE_MARK
= (1 << IPSET_TYPE_MARK_FLAG
),
44 IPSET_TYPE_NOMATCH_FLAG
= 7,
45 IPSET_TYPE_NOMATCH
= (1 << IPSET_TYPE_NOMATCH_FLAG
),
46 /* Strictly speaking not a feature, but a flag for dumping:
47 * this settype must be dumped last */
48 IPSET_DUMP_LAST_FLAG
= 8,
49 IPSET_DUMP_LAST
= (1 << IPSET_DUMP_LAST_FLAG
),
53 enum ip_set_extension
{
54 IPSET_EXT_BIT_TIMEOUT
= 0,
55 IPSET_EXT_TIMEOUT
= (1 << IPSET_EXT_BIT_TIMEOUT
),
56 IPSET_EXT_BIT_COUNTER
= 1,
57 IPSET_EXT_COUNTER
= (1 << IPSET_EXT_BIT_COUNTER
),
58 IPSET_EXT_BIT_COMMENT
= 2,
59 IPSET_EXT_COMMENT
= (1 << IPSET_EXT_BIT_COMMENT
),
60 IPSET_EXT_BIT_SKBINFO
= 3,
61 IPSET_EXT_SKBINFO
= (1 << IPSET_EXT_BIT_SKBINFO
),
62 /* Mark set with an extension which needs to call destroy */
63 IPSET_EXT_BIT_DESTROY
= 7,
64 IPSET_EXT_DESTROY
= (1 << IPSET_EXT_BIT_DESTROY
),
67 #define SET_WITH_TIMEOUT(s) ((s)->extensions & IPSET_EXT_TIMEOUT)
68 #define SET_WITH_COUNTER(s) ((s)->extensions & IPSET_EXT_COUNTER)
69 #define SET_WITH_COMMENT(s) ((s)->extensions & IPSET_EXT_COMMENT)
70 #define SET_WITH_SKBINFO(s) ((s)->extensions & IPSET_EXT_SKBINFO)
71 #define SET_WITH_FORCEADD(s) ((s)->flags & IPSET_CREATE_FLAG_FORCEADD)
73 /* Extension id, in size order */
75 IPSET_EXT_ID_COUNTER
= 0,
83 struct ip_set_ext_type
{
84 /* Destroy extension private data (can be NULL) */
85 void (*destroy
)(void *ext
);
86 enum ip_set_extension type
;
87 enum ipset_cadt_flags flag
;
88 /* Size and minimal alignment */
93 extern const struct ip_set_ext_type ip_set_extensions
[];
106 struct ip_set_counter
{
111 struct ip_set_comment
{
115 struct ip_set_skbinfo
{
124 #define ext_timeout(e, s) \
125 ((unsigned long *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_TIMEOUT]))
126 #define ext_counter(e, s) \
127 ((struct ip_set_counter *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COUNTER]))
128 #define ext_comment(e, s) \
129 ((struct ip_set_comment *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COMMENT]))
130 #define ext_skbinfo(e, s) \
131 ((struct ip_set_skbinfo *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_SKBINFO]))
133 typedef int (*ipset_adtfn
)(struct ip_set
*set
, void *value
,
134 const struct ip_set_ext
*ext
,
135 struct ip_set_ext
*mext
, u32 cmdflags
);
137 /* Kernel API function options */
138 struct ip_set_adt_opt
{
139 u8 family
; /* Actual protocol family */
140 u8 dim
; /* Dimension of match/target */
141 u8 flags
; /* Direction and negation flags */
142 u32 cmdflags
; /* Command-like flags */
143 struct ip_set_ext ext
; /* Extensions */
146 /* Set type, variant-specific part */
147 struct ip_set_type_variant
{
148 /* Kernelspace: test/add/del entries
149 * returns negative error code,
150 * zero for no match/success to add/delete
151 * positive for matching element */
152 int (*kadt
)(struct ip_set
*set
, const struct sk_buff
*skb
,
153 const struct xt_action_param
*par
,
154 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
);
156 /* Userspace: test/add/del entries
157 * returns negative error code,
158 * zero for no match/success to add/delete
159 * positive for matching element */
160 int (*uadt
)(struct ip_set
*set
, struct nlattr
*tb
[],
161 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
);
163 /* Low level add/del/test functions */
164 ipset_adtfn adt
[IPSET_ADT_MAX
];
166 /* When adding entries and set is full, try to resize the set */
167 int (*resize
)(struct ip_set
*set
, bool retried
);
168 /* Destroy the set */
169 void (*destroy
)(struct ip_set
*set
);
170 /* Flush the elements */
171 void (*flush
)(struct ip_set
*set
);
172 /* Expire entries before listing */
173 void (*expire
)(struct ip_set
*set
);
174 /* List set header data */
175 int (*head
)(struct ip_set
*set
, struct sk_buff
*skb
);
177 int (*list
)(const struct ip_set
*set
, struct sk_buff
*skb
,
178 struct netlink_callback
*cb
);
179 /* Keep listing private when resizing runs parallel */
180 void (*uref
)(struct ip_set
*set
, struct netlink_callback
*cb
,
183 /* Return true if "b" set is the same as "a"
184 * according to the create set parameters */
185 bool (*same_set
)(const struct ip_set
*a
, const struct ip_set
*b
);
188 /* The core set type structure */
190 struct list_head list
;
193 char name
[IPSET_MAXNAMELEN
];
194 /* Protocol version */
196 /* Set type dimension */
199 * Supported family: may be NFPROTO_UNSPEC for both
200 * NFPROTO_IPV4/NFPROTO_IPV6.
204 u8 revision_min
, revision_max
;
205 /* Set features to control swapping */
209 int (*create
)(struct net
*net
, struct ip_set
*set
,
210 struct nlattr
*tb
[], u32 flags
);
212 /* Attribute policies */
213 const struct nla_policy create_policy
[IPSET_ATTR_CREATE_MAX
+ 1];
214 const struct nla_policy adt_policy
[IPSET_ATTR_ADT_MAX
+ 1];
216 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
220 /* register and unregister set type */
221 extern int ip_set_type_register(struct ip_set_type
*set_type
);
222 extern void ip_set_type_unregister(struct ip_set_type
*set_type
);
224 /* A generic IP set */
226 /* The name of the set */
227 char name
[IPSET_MAXNAMELEN
];
228 /* Lock protecting the set data */
230 /* References to the set */
232 /* The core set type */
233 struct ip_set_type
*type
;
234 /* The type variant doing the real job */
235 const struct ip_set_type_variant
*variant
;
236 /* The actual INET family of the set */
238 /* The type revision */
244 /* Default timeout value, if enabled */
246 /* Element data size */
248 /* Offsets to extensions in elements */
249 size_t offset
[IPSET_EXT_ID_MAX
];
250 /* The type specific data */
255 ip_set_ext_destroy(struct ip_set
*set
, void *data
)
257 /* Check that the extension is enabled for the set and
258 * call it's destroy function for its extension part in data.
260 if (SET_WITH_COMMENT(set
))
261 ip_set_extensions
[IPSET_EXT_ID_COMMENT
].destroy(
262 ext_comment(data
, set
));
266 ip_set_put_flags(struct sk_buff
*skb
, struct ip_set
*set
)
270 if (SET_WITH_TIMEOUT(set
))
271 if (unlikely(nla_put_net32(skb
, IPSET_ATTR_TIMEOUT
,
272 htonl(set
->timeout
))))
274 if (SET_WITH_COUNTER(set
))
275 cadt_flags
|= IPSET_FLAG_WITH_COUNTERS
;
276 if (SET_WITH_COMMENT(set
))
277 cadt_flags
|= IPSET_FLAG_WITH_COMMENT
;
278 if (SET_WITH_SKBINFO(set
))
279 cadt_flags
|= IPSET_FLAG_WITH_SKBINFO
;
280 if (SET_WITH_FORCEADD(set
))
281 cadt_flags
|= IPSET_FLAG_WITH_FORCEADD
;
285 return nla_put_net32(skb
, IPSET_ATTR_CADT_FLAGS
, htonl(cadt_flags
));
289 ip_set_add_bytes(u64 bytes
, struct ip_set_counter
*counter
)
291 atomic64_add((long long)bytes
, &(counter
)->bytes
);
295 ip_set_add_packets(u64 packets
, struct ip_set_counter
*counter
)
297 atomic64_add((long long)packets
, &(counter
)->packets
);
301 ip_set_get_bytes(const struct ip_set_counter
*counter
)
303 return (u64
)atomic64_read(&(counter
)->bytes
);
307 ip_set_get_packets(const struct ip_set_counter
*counter
)
309 return (u64
)atomic64_read(&(counter
)->packets
);
313 ip_set_update_counter(struct ip_set_counter
*counter
,
314 const struct ip_set_ext
*ext
,
315 struct ip_set_ext
*mext
, u32 flags
)
317 if (ext
->packets
!= ULLONG_MAX
&&
318 !(flags
& IPSET_FLAG_SKIP_COUNTER_UPDATE
)) {
319 ip_set_add_bytes(ext
->bytes
, counter
);
320 ip_set_add_packets(ext
->packets
, counter
);
322 if (flags
& IPSET_FLAG_MATCH_COUNTERS
) {
323 mext
->packets
= ip_set_get_packets(counter
);
324 mext
->bytes
= ip_set_get_bytes(counter
);
329 ip_set_get_skbinfo(struct ip_set_skbinfo
*skbinfo
,
330 const struct ip_set_ext
*ext
,
331 struct ip_set_ext
*mext
, u32 flags
)
333 mext
->skbmark
= skbinfo
->skbmark
;
334 mext
->skbmarkmask
= skbinfo
->skbmarkmask
;
335 mext
->skbprio
= skbinfo
->skbprio
;
336 mext
->skbqueue
= skbinfo
->skbqueue
;
339 ip_set_put_skbinfo(struct sk_buff
*skb
, struct ip_set_skbinfo
*skbinfo
)
341 /* Send nonzero parameters only */
342 return ((skbinfo
->skbmark
|| skbinfo
->skbmarkmask
) &&
343 nla_put_net64(skb
, IPSET_ATTR_SKBMARK
,
344 cpu_to_be64((u64
)skbinfo
->skbmark
<< 32 |
345 skbinfo
->skbmarkmask
))) ||
347 nla_put_net32(skb
, IPSET_ATTR_SKBPRIO
,
348 cpu_to_be32(skbinfo
->skbprio
))) ||
349 (skbinfo
->skbqueue
&&
350 nla_put_net16(skb
, IPSET_ATTR_SKBQUEUE
,
351 cpu_to_be16(skbinfo
->skbqueue
)));
356 ip_set_init_skbinfo(struct ip_set_skbinfo
*skbinfo
,
357 const struct ip_set_ext
*ext
)
359 skbinfo
->skbmark
= ext
->skbmark
;
360 skbinfo
->skbmarkmask
= ext
->skbmarkmask
;
361 skbinfo
->skbprio
= ext
->skbprio
;
362 skbinfo
->skbqueue
= ext
->skbqueue
;
366 ip_set_put_counter(struct sk_buff
*skb
, struct ip_set_counter
*counter
)
368 return nla_put_net64(skb
, IPSET_ATTR_BYTES
,
369 cpu_to_be64(ip_set_get_bytes(counter
))) ||
370 nla_put_net64(skb
, IPSET_ATTR_PACKETS
,
371 cpu_to_be64(ip_set_get_packets(counter
)));
375 ip_set_init_counter(struct ip_set_counter
*counter
,
376 const struct ip_set_ext
*ext
)
378 if (ext
->bytes
!= ULLONG_MAX
)
379 atomic64_set(&(counter
)->bytes
, (long long)(ext
->bytes
));
380 if (ext
->packets
!= ULLONG_MAX
)
381 atomic64_set(&(counter
)->packets
, (long long)(ext
->packets
));
384 /* Netlink CB args */
386 IPSET_CB_NET
= 0, /* net namespace */
387 IPSET_CB_DUMP
, /* dump single set/all sets */
388 IPSET_CB_INDEX
, /* set index */
389 IPSET_CB_PRIVATE
, /* set private data */
390 IPSET_CB_ARG0
, /* type specific */
394 /* register and unregister set references */
395 extern ip_set_id_t
ip_set_get_byname(struct net
*net
,
396 const char *name
, struct ip_set
**set
);
397 extern void ip_set_put_byindex(struct net
*net
, ip_set_id_t index
);
398 extern const char *ip_set_name_byindex(struct net
*net
, ip_set_id_t index
);
399 extern ip_set_id_t
ip_set_nfnl_get_byindex(struct net
*net
, ip_set_id_t index
);
400 extern void ip_set_nfnl_put(struct net
*net
, ip_set_id_t index
);
402 /* API for iptables set match, and SET target */
404 extern int ip_set_add(ip_set_id_t id
, const struct sk_buff
*skb
,
405 const struct xt_action_param
*par
,
406 struct ip_set_adt_opt
*opt
);
407 extern int ip_set_del(ip_set_id_t id
, const struct sk_buff
*skb
,
408 const struct xt_action_param
*par
,
409 struct ip_set_adt_opt
*opt
);
410 extern int ip_set_test(ip_set_id_t id
, const struct sk_buff
*skb
,
411 const struct xt_action_param
*par
,
412 struct ip_set_adt_opt
*opt
);
414 /* Utility functions */
415 extern void *ip_set_alloc(size_t size
);
416 extern void ip_set_free(void *members
);
417 extern int ip_set_get_ipaddr4(struct nlattr
*nla
, __be32
*ipaddr
);
418 extern int ip_set_get_ipaddr6(struct nlattr
*nla
, union nf_inet_addr
*ipaddr
);
419 extern size_t ip_set_elem_len(struct ip_set
*set
, struct nlattr
*tb
[],
421 extern int ip_set_get_extensions(struct ip_set
*set
, struct nlattr
*tb
[],
422 struct ip_set_ext
*ext
);
425 ip_set_get_hostipaddr4(struct nlattr
*nla
, u32
*ipaddr
)
428 int ret
= ip_set_get_ipaddr4(nla
, &ip
);
436 /* Ignore IPSET_ERR_EXIST errors if asked to do so? */
438 ip_set_eexist(int ret
, u32 flags
)
440 return ret
== -IPSET_ERR_EXIST
&& (flags
& IPSET_FLAG_EXIST
);
443 /* Match elements marked with nomatch */
445 ip_set_enomatch(int ret
, u32 flags
, enum ipset_adt adt
, struct ip_set
*set
)
447 return adt
== IPSET_TEST
&&
448 (set
->type
->features
& IPSET_TYPE_NOMATCH
) &&
449 ((flags
>> 16) & IPSET_FLAG_NOMATCH
) &&
450 (ret
> 0 || ret
== -ENOTEMPTY
);
453 /* Check the NLA_F_NET_BYTEORDER flag */
455 ip_set_attr_netorder(struct nlattr
*tb
[], int type
)
457 return tb
[type
] && (tb
[type
]->nla_type
& NLA_F_NET_BYTEORDER
);
461 ip_set_optattr_netorder(struct nlattr
*tb
[], int type
)
463 return !tb
[type
] || (tb
[type
]->nla_type
& NLA_F_NET_BYTEORDER
);
466 /* Useful converters */
468 ip_set_get_h32(const struct nlattr
*attr
)
470 return ntohl(nla_get_be32(attr
));
474 ip_set_get_h16(const struct nlattr
*attr
)
476 return ntohs(nla_get_be16(attr
));
479 #define ipset_nest_start(skb, attr) nla_nest_start(skb, attr | NLA_F_NESTED)
480 #define ipset_nest_end(skb, start) nla_nest_end(skb, start)
482 static inline int nla_put_ipaddr4(struct sk_buff
*skb
, int type
, __be32 ipaddr
)
484 struct nlattr
*__nested
= ipset_nest_start(skb
, type
);
489 ret
= nla_put_in_addr(skb
, IPSET_ATTR_IPADDR_IPV4
, ipaddr
);
491 ipset_nest_end(skb
, __nested
);
495 static inline int nla_put_ipaddr6(struct sk_buff
*skb
, int type
,
496 const struct in6_addr
*ipaddrptr
)
498 struct nlattr
*__nested
= ipset_nest_start(skb
, type
);
503 ret
= nla_put_in6_addr(skb
, IPSET_ATTR_IPADDR_IPV6
, ipaddrptr
);
505 ipset_nest_end(skb
, __nested
);
509 /* Get address from skbuff */
511 ip4addr(const struct sk_buff
*skb
, bool src
)
513 return src
? ip_hdr(skb
)->saddr
: ip_hdr(skb
)->daddr
;
517 ip4addrptr(const struct sk_buff
*skb
, bool src
, __be32
*addr
)
519 *addr
= src
? ip_hdr(skb
)->saddr
: ip_hdr(skb
)->daddr
;
523 ip6addrptr(const struct sk_buff
*skb
, bool src
, struct in6_addr
*addr
)
525 memcpy(addr
, src
? &ipv6_hdr(skb
)->saddr
: &ipv6_hdr(skb
)->daddr
,
529 /* Calculate the bytes required to store the inclusive range of a-b */
531 bitmap_bytes(u32 a
, u32 b
)
533 return 4 * ((((b
- a
+ 8) / 8) + 3) / 4);
536 #include <linux/netfilter/ipset/ip_set_timeout.h>
537 #include <linux/netfilter/ipset/ip_set_comment.h>
540 ip_set_put_extensions(struct sk_buff
*skb
, const struct ip_set
*set
,
541 const void *e
, bool active
);
543 #define IP_SET_INIT_KEXT(skb, opt, set) \
544 { .bytes = (skb)->len, .packets = 1, \
545 .timeout = ip_set_adt_opt_timeout(opt, set) }
547 #define IP_SET_INIT_UEXT(set) \
548 { .bytes = ULLONG_MAX, .packets = ULLONG_MAX, \
549 .timeout = (set)->timeout }
551 #define IPSET_CONCAT(a, b) a##b
552 #define IPSET_TOKEN(a, b) IPSET_CONCAT(a, b)
554 #endif /*_IP_SET_H */