1 /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 as
5 * published by the Free Software Foundation.
8 /* Kernel module implementing an IP set type: the hash:net type */
10 #include <linux/jhash.h>
11 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/errno.h>
15 #include <linux/random.h>
18 #include <net/netlink.h>
20 #include <linux/netfilter.h>
21 #include <linux/netfilter/ipset/pfxlen.h>
22 #include <linux/netfilter/ipset/ip_set.h>
23 #include <linux/netfilter/ipset/ip_set_hash.h>
25 #define REVISION_MIN 0
26 /* 1 Range as input support for IPv4 added */
27 /* 2 nomatch flag support added */
28 #define REVISION_MAX 3 /* Counters support added */
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
32 IP_SET_MODULE_DESC("hash:net", REVISION_MIN, REVISION_MAX);
33 MODULE_ALIAS("ip_set_hash:net");
35 /* Type specific function prefix */
36 #define HTYPE hash_net
37 #define IP_SET_HASH_WITH_NETS
42 struct hash_net4_elem {
49 struct hash_net4t_elem {
54 unsigned long timeout;
57 struct hash_net4c_elem {
62 struct ip_set_counter counter;
65 struct hash_net4ct_elem {
70 struct ip_set_counter counter;
71 unsigned long timeout;
74 /* Common functions */
77 hash_net4_data_equal(const struct hash_net4_elem *ip1,
78 const struct hash_net4_elem *ip2,
81 return ip1->ip == ip2->ip &&
82 ip1->cidr == ip2->cidr;
86 hash_net4_do_data_match(const struct hash_net4_elem *elem)
88 return elem->nomatch ? -ENOTEMPTY : 1;
92 hash_net4_data_set_flags(struct hash_net4_elem *elem, u32 flags)
94 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
98 hash_net4_data_reset_flags(struct hash_net4_elem *elem, u8 *flags)
100 swap(*flags, elem->nomatch);
104 hash_net4_data_netmask(struct hash_net4_elem *elem, u8 cidr)
106 elem->ip &= ip_set_netmask(cidr);
111 hash_net4_data_list(struct sk_buff *skb, const struct hash_net4_elem *data)
113 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
115 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
116 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
118 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
119 goto nla_put_failure;
127 hash_net4_data_next(struct hash_net4_elem *next,
128 const struct hash_net4_elem *d)
133 #define MTYPE hash_net4
136 #include "ip_set_hash_gen.h"
139 hash_net4_kadt(struct ip_set *set, const struct sk_buff *skb,
140 const struct xt_action_param *par,
141 enum ipset_adt adt, struct ip_set_adt_opt *opt)
143 const struct hash_net *h = set->data;
144 ipset_adtfn adtfn = set->variant->adt[adt];
145 struct hash_net4_elem e = {
146 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
148 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h);
152 if (adt == IPSET_TEST)
155 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
156 e.ip &= ip_set_netmask(e.cidr);
158 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
162 hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
163 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
165 const struct hash_net *h = set->data;
166 ipset_adtfn adtfn = set->variant->adt[adt];
167 struct hash_net4_elem e = { .cidr = HOST_MASK };
168 struct ip_set_ext ext = IP_SET_INIT_UEXT(h);
169 u32 ip = 0, ip_to, last;
172 if (unlikely(!tb[IPSET_ATTR_IP] ||
173 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
174 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
175 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
176 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
177 return -IPSET_ERR_PROTOCOL;
179 if (tb[IPSET_ATTR_LINENO])
180 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
182 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) ||
183 ip_set_get_extensions(set, tb, &ext);
187 if (tb[IPSET_ATTR_CIDR]) {
188 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
189 if (!e.cidr || e.cidr > HOST_MASK)
190 return -IPSET_ERR_INVALID_CIDR;
193 if (tb[IPSET_ATTR_CADT_FLAGS]) {
194 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
195 if (cadt_flags & IPSET_FLAG_NOMATCH)
196 flags |= (IPSET_FLAG_NOMATCH << 16);
199 if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
200 e.ip = htonl(ip & ip_set_hostmask(e.cidr));
201 ret = adtfn(set, &e, &ext, &ext, flags);
202 return ip_set_enomatch(ret, flags, adt) ? 1 :
203 ip_set_eexist(ret, flags) ? 0 : ret;
207 if (tb[IPSET_ATTR_IP_TO]) {
208 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
213 if (ip + UINT_MAX == ip_to)
214 return -IPSET_ERR_HASH_RANGE;
217 ip = ntohl(h->next.ip);
218 while (!after(ip, ip_to)) {
220 last = ip_set_range_to_cidr(ip, ip_to, &e.cidr);
221 ret = adtfn(set, &e, &ext, &ext, flags);
222 if (ret && !ip_set_eexist(ret, flags))
233 struct hash_net6_elem {
234 union nf_inet_addr ip;
240 struct hash_net6t_elem {
241 union nf_inet_addr ip;
245 unsigned long timeout;
248 struct hash_net6c_elem {
249 union nf_inet_addr ip;
253 struct ip_set_counter counter;
256 struct hash_net6ct_elem {
257 union nf_inet_addr ip;
261 struct ip_set_counter counter;
262 unsigned long timeout;
265 /* Common functions */
268 hash_net6_data_equal(const struct hash_net6_elem *ip1,
269 const struct hash_net6_elem *ip2,
272 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
273 ip1->cidr == ip2->cidr;
277 hash_net6_do_data_match(const struct hash_net6_elem *elem)
279 return elem->nomatch ? -ENOTEMPTY : 1;
283 hash_net6_data_set_flags(struct hash_net6_elem *elem, u32 flags)
285 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
289 hash_net6_data_reset_flags(struct hash_net6_elem *elem, u8 *flags)
291 swap(*flags, elem->nomatch);
295 hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr)
297 ip6_netmask(&elem->ip, cidr);
302 hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data)
304 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
306 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
307 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
309 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
310 goto nla_put_failure;
318 hash_net6_data_next(struct hash_net4_elem *next,
319 const struct hash_net6_elem *d)
327 #define MTYPE hash_net6
329 #define HOST_MASK 128
330 #define IP_SET_EMIT_CREATE
331 #include "ip_set_hash_gen.h"
334 hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
335 const struct xt_action_param *par,
336 enum ipset_adt adt, struct ip_set_adt_opt *opt)
338 const struct hash_net *h = set->data;
339 ipset_adtfn adtfn = set->variant->adt[adt];
340 struct hash_net6_elem e = {
341 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
343 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h);
347 if (adt == IPSET_TEST)
350 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
351 ip6_netmask(&e.ip, e.cidr);
353 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
357 hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
358 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
360 const struct hash_net *h = set->data;
361 ipset_adtfn adtfn = set->variant->adt[adt];
362 struct hash_net6_elem e = { .cidr = HOST_MASK };
363 struct ip_set_ext ext = IP_SET_INIT_UEXT(h);
366 if (unlikely(!tb[IPSET_ATTR_IP] ||
367 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
368 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
369 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
370 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
371 return -IPSET_ERR_PROTOCOL;
372 if (unlikely(tb[IPSET_ATTR_IP_TO]))
373 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
375 if (tb[IPSET_ATTR_LINENO])
376 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
378 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
379 ip_set_get_extensions(set, tb, &ext);
383 if (tb[IPSET_ATTR_CIDR])
384 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
386 if (!e.cidr || e.cidr > HOST_MASK)
387 return -IPSET_ERR_INVALID_CIDR;
389 ip6_netmask(&e.ip, e.cidr);
391 if (tb[IPSET_ATTR_CADT_FLAGS]) {
392 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
393 if (cadt_flags & IPSET_FLAG_NOMATCH)
394 flags |= (IPSET_FLAG_NOMATCH << 16);
397 ret = adtfn(set, &e, &ext, &ext, flags);
399 return ip_set_enomatch(ret, flags, adt) ? 1 :
400 ip_set_eexist(ret, flags) ? 0 : ret;
403 static struct ip_set_type hash_net_type __read_mostly = {
405 .protocol = IPSET_PROTOCOL,
406 .features = IPSET_TYPE_IP | IPSET_TYPE_NOMATCH,
407 .dimension = IPSET_DIM_ONE,
408 .family = NFPROTO_UNSPEC,
409 .revision_min = REVISION_MIN,
410 .revision_max = REVISION_MAX,
411 .create = hash_net_create,
413 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
414 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
415 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
416 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
417 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
418 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
421 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
422 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
423 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
424 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
425 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
426 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
427 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
435 return ip_set_type_register(&hash_net_type);
441 ip_set_type_unregister(&hash_net_type);
444 module_init(hash_net_init);
445 module_exit(hash_net_fini);