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/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_timeout.h>
24 #include <linux/netfilter/ipset/ip_set_hash.h>
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
28 MODULE_DESCRIPTION("hash:net type of IP sets");
29 MODULE_ALIAS("ip_set_hash:net");
31 /* Type specific function prefix */
35 hash_net_same_set(const struct ip_set *a, const struct ip_set *b);
37 #define hash_net4_same_set hash_net_same_set
38 #define hash_net6_same_set hash_net_same_set
40 /* The type variant functions: IPv4 */
42 /* Member elements without timeout */
43 struct hash_net4_elem {
50 /* Member elements with timeout support */
51 struct hash_net4_telem {
56 unsigned long timeout;
60 hash_net4_data_equal(const struct hash_net4_elem *ip1,
61 const struct hash_net4_elem *ip2)
63 return ip1->ip == ip2->ip && ip1->cidr == ip2->cidr;
67 hash_net4_data_isnull(const struct hash_net4_elem *elem)
69 return elem->cidr == 0;
73 hash_net4_data_copy(struct hash_net4_elem *dst,
74 const struct hash_net4_elem *src)
77 dst->cidr = src->cidr;
81 hash_net4_data_netmask(struct hash_net4_elem *elem, u8 cidr)
83 elem->ip &= ip_set_netmask(cidr);
87 /* Zero CIDR values cannot be stored */
89 hash_net4_data_zero_out(struct hash_net4_elem *elem)
95 hash_net4_data_list(struct sk_buff *skb, const struct hash_net4_elem *data)
97 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, data->ip);
98 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
106 hash_net4_data_tlist(struct sk_buff *skb, const struct hash_net4_elem *data)
108 const struct hash_net4_telem *tdata =
109 (const struct hash_net4_telem *)data;
111 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, tdata->ip);
112 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, tdata->cidr);
113 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
114 htonl(ip_set_timeout_get(tdata->timeout)));
122 #define IP_SET_HASH_WITH_NETS
126 #include <linux/netfilter/ipset/ip_set_ahash.h>
129 hash_net4_kadt(struct ip_set *set, const struct sk_buff *skb,
130 enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
132 const struct ip_set_hash *h = set->data;
133 ipset_adtfn adtfn = set->variant->adt[adt];
134 struct hash_net4_elem data = { .cidr = h->nets[0].cidr || HOST_MASK };
138 if (adt == IPSET_TEST)
139 data.cidr = HOST_MASK;
141 ip4addrptr(skb, flags & IPSET_DIM_ONE_SRC, &data.ip);
142 data.ip &= ip_set_netmask(data.cidr);
144 return adtfn(set, &data, h->timeout);
148 hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
149 enum ipset_adt adt, u32 *lineno, u32 flags)
151 const struct ip_set_hash *h = set->data;
152 ipset_adtfn adtfn = set->variant->adt[adt];
153 struct hash_net4_elem data = { .cidr = HOST_MASK };
154 u32 timeout = h->timeout;
157 if (unlikely(!tb[IPSET_ATTR_IP] ||
158 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
159 return -IPSET_ERR_PROTOCOL;
161 if (tb[IPSET_ATTR_LINENO])
162 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
164 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &data.ip);
168 if (tb[IPSET_ATTR_CIDR])
169 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
172 return -IPSET_ERR_INVALID_CIDR;
174 data.ip &= ip_set_netmask(data.cidr);
176 if (tb[IPSET_ATTR_TIMEOUT]) {
177 if (!with_timeout(h->timeout))
178 return -IPSET_ERR_TIMEOUT;
179 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
182 ret = adtfn(set, &data, timeout);
184 return ip_set_eexist(ret, flags) ? 0 : ret;
188 hash_net_same_set(const struct ip_set *a, const struct ip_set *b)
190 const struct ip_set_hash *x = a->data;
191 const struct ip_set_hash *y = b->data;
193 /* Resizing changes htable_bits, so we ignore it */
194 return x->maxelem == y->maxelem &&
195 x->timeout == y->timeout;
198 /* The type variant functions: IPv6 */
200 struct hash_net6_elem {
201 union nf_inet_addr ip;
207 struct hash_net6_telem {
208 union nf_inet_addr ip;
212 unsigned long timeout;
216 hash_net6_data_equal(const struct hash_net6_elem *ip1,
217 const struct hash_net6_elem *ip2)
219 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
220 ip1->cidr == ip2->cidr;
224 hash_net6_data_isnull(const struct hash_net6_elem *elem)
226 return elem->cidr == 0;
230 hash_net6_data_copy(struct hash_net6_elem *dst,
231 const struct hash_net6_elem *src)
233 ipv6_addr_copy(&dst->ip.in6, &src->ip.in6);
234 dst->cidr = src->cidr;
238 hash_net6_data_zero_out(struct hash_net6_elem *elem)
244 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
246 ip->ip6[0] &= ip_set_netmask6(prefix)[0];
247 ip->ip6[1] &= ip_set_netmask6(prefix)[1];
248 ip->ip6[2] &= ip_set_netmask6(prefix)[2];
249 ip->ip6[3] &= ip_set_netmask6(prefix)[3];
253 hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr)
255 ip6_netmask(&elem->ip, cidr);
260 hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data)
262 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &data->ip);
263 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
271 hash_net6_data_tlist(struct sk_buff *skb, const struct hash_net6_elem *data)
273 const struct hash_net6_telem *e =
274 (const struct hash_net6_telem *)data;
276 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &e->ip);
277 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, e->cidr);
278 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
279 htonl(ip_set_timeout_get(e->timeout)));
290 #define HOST_MASK 128
291 #include <linux/netfilter/ipset/ip_set_ahash.h>
294 hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
295 enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
297 const struct ip_set_hash *h = set->data;
298 ipset_adtfn adtfn = set->variant->adt[adt];
299 struct hash_net6_elem data = { .cidr = h->nets[0].cidr || HOST_MASK };
303 if (adt == IPSET_TEST)
304 data.cidr = HOST_MASK;
306 ip6addrptr(skb, flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
307 ip6_netmask(&data.ip, data.cidr);
309 return adtfn(set, &data, h->timeout);
313 hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
314 enum ipset_adt adt, u32 *lineno, u32 flags)
316 const struct ip_set_hash *h = set->data;
317 ipset_adtfn adtfn = set->variant->adt[adt];
318 struct hash_net6_elem data = { .cidr = HOST_MASK };
319 u32 timeout = h->timeout;
322 if (unlikely(!tb[IPSET_ATTR_IP] ||
323 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
324 return -IPSET_ERR_PROTOCOL;
326 if (tb[IPSET_ATTR_LINENO])
327 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
329 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
333 if (tb[IPSET_ATTR_CIDR])
334 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
337 return -IPSET_ERR_INVALID_CIDR;
339 ip6_netmask(&data.ip, data.cidr);
341 if (tb[IPSET_ATTR_TIMEOUT]) {
342 if (!with_timeout(h->timeout))
343 return -IPSET_ERR_TIMEOUT;
344 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
347 ret = adtfn(set, &data, timeout);
349 return ip_set_eexist(ret, flags) ? 0 : ret;
352 /* Create hash:ip type of sets */
355 hash_net_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
357 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
358 struct ip_set_hash *h;
361 if (!(set->family == AF_INET || set->family == AF_INET6))
362 return -IPSET_ERR_INVALID_FAMILY;
364 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
365 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
366 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
367 return -IPSET_ERR_PROTOCOL;
369 if (tb[IPSET_ATTR_HASHSIZE]) {
370 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
371 if (hashsize < IPSET_MIMINAL_HASHSIZE)
372 hashsize = IPSET_MIMINAL_HASHSIZE;
375 if (tb[IPSET_ATTR_MAXELEM])
376 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
378 h = kzalloc(sizeof(*h)
379 + sizeof(struct ip_set_hash_nets)
380 * (set->family == AF_INET ? 32 : 128), GFP_KERNEL);
384 h->maxelem = maxelem;
385 get_random_bytes(&h->initval, sizeof(h->initval));
386 h->timeout = IPSET_NO_TIMEOUT;
388 hbits = htable_bits(hashsize);
389 h->table = ip_set_alloc(
390 sizeof(struct htable)
391 + jhash_size(hbits) * sizeof(struct hbucket));
396 h->table->htable_bits = hbits;
400 if (tb[IPSET_ATTR_TIMEOUT]) {
401 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
403 set->variant = set->family == AF_INET
404 ? &hash_net4_tvariant : &hash_net6_tvariant;
406 if (set->family == AF_INET)
407 hash_net4_gc_init(set);
409 hash_net6_gc_init(set);
411 set->variant = set->family == AF_INET
412 ? &hash_net4_variant : &hash_net6_variant;
415 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
416 set->name, jhash_size(h->table->htable_bits),
417 h->table->htable_bits, h->maxelem, set->data, h->table);
422 static struct ip_set_type hash_net_type __read_mostly = {
424 .protocol = IPSET_PROTOCOL,
425 .features = IPSET_TYPE_IP,
426 .dimension = IPSET_DIM_ONE,
429 .create = hash_net_create,
431 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
432 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
433 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
434 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
435 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
438 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
439 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
440 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
448 return ip_set_type_register(&hash_net_type);
454 ip_set_type_unregister(&hash_net_type);
457 module_init(hash_net_init);
458 module_exit(hash_net_fini);