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_data_next(struct ip_set_hash *h,
130 const struct hash_net4_elem *d)
135 hash_net4_kadt(struct ip_set *set, const struct sk_buff *skb,
136 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
138 const struct ip_set_hash *h = set->data;
139 ipset_adtfn adtfn = set->variant->adt[adt];
140 struct hash_net4_elem data = { .cidr = h->nets[0].cidr || HOST_MASK };
144 if (adt == IPSET_TEST)
145 data.cidr = HOST_MASK;
147 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip);
148 data.ip &= ip_set_netmask(data.cidr);
150 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
154 hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
155 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
157 const struct ip_set_hash *h = set->data;
158 ipset_adtfn adtfn = set->variant->adt[adt];
159 struct hash_net4_elem data = { .cidr = HOST_MASK };
160 u32 timeout = h->timeout;
163 if (unlikely(!tb[IPSET_ATTR_IP] ||
164 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
165 return -IPSET_ERR_PROTOCOL;
167 if (tb[IPSET_ATTR_LINENO])
168 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
170 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &data.ip);
174 if (tb[IPSET_ATTR_CIDR])
175 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
178 return -IPSET_ERR_INVALID_CIDR;
180 data.ip &= ip_set_netmask(data.cidr);
182 if (tb[IPSET_ATTR_TIMEOUT]) {
183 if (!with_timeout(h->timeout))
184 return -IPSET_ERR_TIMEOUT;
185 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
188 ret = adtfn(set, &data, timeout, flags);
190 return ip_set_eexist(ret, flags) ? 0 : ret;
194 hash_net_same_set(const struct ip_set *a, const struct ip_set *b)
196 const struct ip_set_hash *x = a->data;
197 const struct ip_set_hash *y = b->data;
199 /* Resizing changes htable_bits, so we ignore it */
200 return x->maxelem == y->maxelem &&
201 x->timeout == y->timeout;
204 /* The type variant functions: IPv6 */
206 struct hash_net6_elem {
207 union nf_inet_addr ip;
213 struct hash_net6_telem {
214 union nf_inet_addr ip;
218 unsigned long timeout;
222 hash_net6_data_equal(const struct hash_net6_elem *ip1,
223 const struct hash_net6_elem *ip2)
225 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
226 ip1->cidr == ip2->cidr;
230 hash_net6_data_isnull(const struct hash_net6_elem *elem)
232 return elem->cidr == 0;
236 hash_net6_data_copy(struct hash_net6_elem *dst,
237 const struct hash_net6_elem *src)
239 ipv6_addr_copy(&dst->ip.in6, &src->ip.in6);
240 dst->cidr = src->cidr;
244 hash_net6_data_zero_out(struct hash_net6_elem *elem)
250 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
252 ip->ip6[0] &= ip_set_netmask6(prefix)[0];
253 ip->ip6[1] &= ip_set_netmask6(prefix)[1];
254 ip->ip6[2] &= ip_set_netmask6(prefix)[2];
255 ip->ip6[3] &= ip_set_netmask6(prefix)[3];
259 hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr)
261 ip6_netmask(&elem->ip, cidr);
266 hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data)
268 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &data->ip);
269 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
277 hash_net6_data_tlist(struct sk_buff *skb, const struct hash_net6_elem *data)
279 const struct hash_net6_telem *e =
280 (const struct hash_net6_telem *)data;
282 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &e->ip);
283 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, e->cidr);
284 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
285 htonl(ip_set_timeout_get(e->timeout)));
296 #define HOST_MASK 128
297 #include <linux/netfilter/ipset/ip_set_ahash.h>
300 hash_net6_data_next(struct ip_set_hash *h,
301 const struct hash_net6_elem *d)
306 hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
307 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
309 const struct ip_set_hash *h = set->data;
310 ipset_adtfn adtfn = set->variant->adt[adt];
311 struct hash_net6_elem data = { .cidr = h->nets[0].cidr || HOST_MASK };
315 if (adt == IPSET_TEST)
316 data.cidr = HOST_MASK;
318 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
319 ip6_netmask(&data.ip, data.cidr);
321 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
325 hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
326 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
328 const struct ip_set_hash *h = set->data;
329 ipset_adtfn adtfn = set->variant->adt[adt];
330 struct hash_net6_elem data = { .cidr = HOST_MASK };
331 u32 timeout = h->timeout;
334 if (unlikely(!tb[IPSET_ATTR_IP] ||
335 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
336 return -IPSET_ERR_PROTOCOL;
338 if (tb[IPSET_ATTR_LINENO])
339 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
341 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
345 if (tb[IPSET_ATTR_CIDR])
346 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
349 return -IPSET_ERR_INVALID_CIDR;
351 ip6_netmask(&data.ip, data.cidr);
353 if (tb[IPSET_ATTR_TIMEOUT]) {
354 if (!with_timeout(h->timeout))
355 return -IPSET_ERR_TIMEOUT;
356 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
359 ret = adtfn(set, &data, timeout, flags);
361 return ip_set_eexist(ret, flags) ? 0 : ret;
364 /* Create hash:ip type of sets */
367 hash_net_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
369 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
370 struct ip_set_hash *h;
373 if (!(set->family == AF_INET || set->family == AF_INET6))
374 return -IPSET_ERR_INVALID_FAMILY;
376 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
377 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
378 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
379 return -IPSET_ERR_PROTOCOL;
381 if (tb[IPSET_ATTR_HASHSIZE]) {
382 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
383 if (hashsize < IPSET_MIMINAL_HASHSIZE)
384 hashsize = IPSET_MIMINAL_HASHSIZE;
387 if (tb[IPSET_ATTR_MAXELEM])
388 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
390 h = kzalloc(sizeof(*h)
391 + sizeof(struct ip_set_hash_nets)
392 * (set->family == AF_INET ? 32 : 128), GFP_KERNEL);
396 h->maxelem = maxelem;
397 get_random_bytes(&h->initval, sizeof(h->initval));
398 h->timeout = IPSET_NO_TIMEOUT;
400 hbits = htable_bits(hashsize);
401 h->table = ip_set_alloc(
402 sizeof(struct htable)
403 + jhash_size(hbits) * sizeof(struct hbucket));
408 h->table->htable_bits = hbits;
412 if (tb[IPSET_ATTR_TIMEOUT]) {
413 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
415 set->variant = set->family == AF_INET
416 ? &hash_net4_tvariant : &hash_net6_tvariant;
418 if (set->family == AF_INET)
419 hash_net4_gc_init(set);
421 hash_net6_gc_init(set);
423 set->variant = set->family == AF_INET
424 ? &hash_net4_variant : &hash_net6_variant;
427 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
428 set->name, jhash_size(h->table->htable_bits),
429 h->table->htable_bits, h->maxelem, set->data, h->table);
434 static struct ip_set_type hash_net_type __read_mostly = {
436 .protocol = IPSET_PROTOCOL,
437 .features = IPSET_TYPE_IP,
438 .dimension = IPSET_DIM_ONE,
442 .create = hash_net_create,
444 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
445 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
446 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
447 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
448 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
451 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
452 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
453 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
461 return ip_set_type_register(&hash_net_type);
467 ip_set_type_unregister(&hash_net_type);
470 module_init(hash_net_init);
471 module_exit(hash_net_fini);