1 /* Copyright (C) 2003-2011 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/uaccess.h>
16 #include <linux/bitops.h>
17 #include <linux/spinlock.h>
18 #include <linux/random.h>
21 #include <net/netlink.h>
23 #include <linux/netfilter.h>
24 #include <linux/netfilter/ipset/pfxlen.h>
25 #include <linux/netfilter/ipset/ip_set.h>
26 #include <linux/netfilter/ipset/ip_set_timeout.h>
27 #include <linux/netfilter/ipset/ip_set_hash.h>
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
31 MODULE_DESCRIPTION("hash:net type of IP sets");
32 MODULE_ALIAS("ip_set_hash:net");
34 /* Type specific function prefix */
38 hash_net_same_set(const struct ip_set *a, const struct ip_set *b);
40 #define hash_net4_same_set hash_net_same_set
41 #define hash_net6_same_set hash_net_same_set
43 /* The type variant functions: IPv4 */
45 /* Member elements without timeout */
46 struct hash_net4_elem {
53 /* Member elements with timeout support */
54 struct hash_net4_telem {
59 unsigned long timeout;
63 hash_net4_data_equal(const struct hash_net4_elem *ip1,
64 const struct hash_net4_elem *ip2)
66 return ip1->ip == ip2->ip && ip1->cidr == ip2->cidr;
70 hash_net4_data_isnull(const struct hash_net4_elem *elem)
72 return elem->cidr == 0;
76 hash_net4_data_copy(struct hash_net4_elem *dst,
77 const struct hash_net4_elem *src)
80 dst->cidr = src->cidr;
84 hash_net4_data_netmask(struct hash_net4_elem *elem, u8 cidr)
86 elem->ip &= ip_set_netmask(cidr);
90 /* Zero CIDR values cannot be stored */
92 hash_net4_data_zero_out(struct hash_net4_elem *elem)
98 hash_net4_data_list(struct sk_buff *skb, const struct hash_net4_elem *data)
100 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, data->ip);
101 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
109 hash_net4_data_tlist(struct sk_buff *skb, const struct hash_net4_elem *data)
111 const struct hash_net4_telem *tdata =
112 (const struct hash_net4_telem *)data;
114 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, tdata->ip);
115 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, tdata->cidr);
116 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
117 htonl(ip_set_timeout_get(tdata->timeout)));
125 #define IP_SET_HASH_WITH_NETS
129 #include <linux/netfilter/ipset/ip_set_ahash.h>
132 hash_net4_kadt(struct ip_set *set, const struct sk_buff *skb,
133 enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
135 const struct ip_set_hash *h = set->data;
136 ipset_adtfn adtfn = set->variant->adt[adt];
137 struct hash_net4_elem data = { .cidr = h->nets[0].cidr || HOST_MASK };
141 if (adt == IPSET_TEST)
142 data.cidr = HOST_MASK;
144 ip4addrptr(skb, flags & IPSET_DIM_ONE_SRC, &data.ip);
145 data.ip &= ip_set_netmask(data.cidr);
147 return adtfn(set, &data, h->timeout);
151 hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
152 enum ipset_adt adt, u32 *lineno, u32 flags)
154 const struct ip_set_hash *h = set->data;
155 ipset_adtfn adtfn = set->variant->adt[adt];
156 struct hash_net4_elem data = { .cidr = HOST_MASK };
157 u32 timeout = h->timeout;
160 if (unlikely(!tb[IPSET_ATTR_IP] ||
161 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
162 return -IPSET_ERR_PROTOCOL;
164 if (tb[IPSET_ATTR_LINENO])
165 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
167 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &data.ip);
171 if (tb[IPSET_ATTR_CIDR])
172 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
175 return -IPSET_ERR_INVALID_CIDR;
177 data.ip &= ip_set_netmask(data.cidr);
179 if (tb[IPSET_ATTR_TIMEOUT]) {
180 if (!with_timeout(h->timeout))
181 return -IPSET_ERR_TIMEOUT;
182 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
185 ret = adtfn(set, &data, timeout);
187 return ip_set_eexist(ret, flags) ? 0 : ret;
191 hash_net_same_set(const struct ip_set *a, const struct ip_set *b)
193 const struct ip_set_hash *x = a->data;
194 const struct ip_set_hash *y = b->data;
196 /* Resizing changes htable_bits, so we ignore it */
197 return x->maxelem == y->maxelem &&
198 x->timeout == y->timeout;
201 /* The type variant functions: IPv6 */
203 struct hash_net6_elem {
204 union nf_inet_addr ip;
210 struct hash_net6_telem {
211 union nf_inet_addr ip;
215 unsigned long timeout;
219 hash_net6_data_equal(const struct hash_net6_elem *ip1,
220 const struct hash_net6_elem *ip2)
222 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
223 ip1->cidr == ip2->cidr;
227 hash_net6_data_isnull(const struct hash_net6_elem *elem)
229 return elem->cidr == 0;
233 hash_net6_data_copy(struct hash_net6_elem *dst,
234 const struct hash_net6_elem *src)
236 ipv6_addr_copy(&dst->ip.in6, &src->ip.in6);
237 dst->cidr = src->cidr;
241 hash_net6_data_zero_out(struct hash_net6_elem *elem)
247 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
249 ip->ip6[0] &= ip_set_netmask6(prefix)[0];
250 ip->ip6[1] &= ip_set_netmask6(prefix)[1];
251 ip->ip6[2] &= ip_set_netmask6(prefix)[2];
252 ip->ip6[3] &= ip_set_netmask6(prefix)[3];
256 hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr)
258 ip6_netmask(&elem->ip, cidr);
263 hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data)
265 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &data->ip);
266 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
274 hash_net6_data_tlist(struct sk_buff *skb, const struct hash_net6_elem *data)
276 const struct hash_net6_telem *e =
277 (const struct hash_net6_telem *)data;
279 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &e->ip);
280 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, e->cidr);
281 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
282 htonl(ip_set_timeout_get(e->timeout)));
293 #define HOST_MASK 128
294 #include <linux/netfilter/ipset/ip_set_ahash.h>
297 hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
298 enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
300 const struct ip_set_hash *h = set->data;
301 ipset_adtfn adtfn = set->variant->adt[adt];
302 struct hash_net6_elem data = { .cidr = h->nets[0].cidr || HOST_MASK };
306 if (adt == IPSET_TEST)
307 data.cidr = HOST_MASK;
309 ip6addrptr(skb, flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
310 ip6_netmask(&data.ip, data.cidr);
312 return adtfn(set, &data, h->timeout);
316 hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
317 enum ipset_adt adt, u32 *lineno, u32 flags)
319 const struct ip_set_hash *h = set->data;
320 ipset_adtfn adtfn = set->variant->adt[adt];
321 struct hash_net6_elem data = { .cidr = HOST_MASK };
322 u32 timeout = h->timeout;
325 if (unlikely(!tb[IPSET_ATTR_IP] ||
326 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
327 return -IPSET_ERR_PROTOCOL;
329 if (tb[IPSET_ATTR_LINENO])
330 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
332 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
336 if (tb[IPSET_ATTR_CIDR])
337 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
340 return -IPSET_ERR_INVALID_CIDR;
342 ip6_netmask(&data.ip, data.cidr);
344 if (tb[IPSET_ATTR_TIMEOUT]) {
345 if (!with_timeout(h->timeout))
346 return -IPSET_ERR_TIMEOUT;
347 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
350 ret = adtfn(set, &data, timeout);
352 return ip_set_eexist(ret, flags) ? 0 : ret;
355 /* Create hash:ip type of sets */
358 hash_net_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
360 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
361 struct ip_set_hash *h;
364 if (!(set->family == AF_INET || set->family == AF_INET6))
365 return -IPSET_ERR_INVALID_FAMILY;
367 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
368 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
369 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
370 return -IPSET_ERR_PROTOCOL;
372 if (tb[IPSET_ATTR_HASHSIZE]) {
373 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
374 if (hashsize < IPSET_MIMINAL_HASHSIZE)
375 hashsize = IPSET_MIMINAL_HASHSIZE;
378 if (tb[IPSET_ATTR_MAXELEM])
379 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
381 h = kzalloc(sizeof(*h)
382 + sizeof(struct ip_set_hash_nets)
383 * (set->family == AF_INET ? 32 : 128), GFP_KERNEL);
387 h->maxelem = maxelem;
388 get_random_bytes(&h->initval, sizeof(h->initval));
389 h->timeout = IPSET_NO_TIMEOUT;
391 hbits = htable_bits(hashsize);
392 h->table = ip_set_alloc(
393 sizeof(struct htable)
394 + jhash_size(hbits) * sizeof(struct hbucket));
399 h->table->htable_bits = hbits;
403 if (tb[IPSET_ATTR_TIMEOUT]) {
404 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
406 set->variant = set->family == AF_INET
407 ? &hash_net4_tvariant : &hash_net6_tvariant;
409 if (set->family == AF_INET)
410 hash_net4_gc_init(set);
412 hash_net6_gc_init(set);
414 set->variant = set->family == AF_INET
415 ? &hash_net4_variant : &hash_net6_variant;
418 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
419 set->name, jhash_size(h->table->htable_bits),
420 h->table->htable_bits, h->maxelem, set->data, h->table);
425 static struct ip_set_type hash_net_type __read_mostly = {
427 .protocol = IPSET_PROTOCOL,
428 .features = IPSET_TYPE_IP,
429 .dimension = IPSET_DIM_ONE,
432 .create = hash_net_create,
434 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
435 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
436 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
437 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
438 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
441 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
442 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
443 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
451 return ip_set_type_register(&hash_net_type);
457 ip_set_type_unregister(&hash_net_type);
460 module_init(hash_net_init);
461 module_exit(hash_net_fini);