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 = {
135 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
140 if (adt == IPSET_TEST)
141 data.cidr = HOST_MASK;
143 ip4addrptr(skb, flags & IPSET_DIM_ONE_SRC, &data.ip);
144 data.ip &= ip_set_netmask(data.cidr);
146 return adtfn(set, &data, h->timeout);
150 hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
151 enum ipset_adt adt, u32 *lineno, u32 flags)
153 const struct ip_set_hash *h = set->data;
154 ipset_adtfn adtfn = set->variant->adt[adt];
155 struct hash_net4_elem data = { .cidr = HOST_MASK };
156 u32 timeout = h->timeout;
159 if (unlikely(!tb[IPSET_ATTR_IP] ||
160 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
161 return -IPSET_ERR_PROTOCOL;
163 if (tb[IPSET_ATTR_LINENO])
164 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
166 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &data.ip);
170 if (tb[IPSET_ATTR_CIDR])
171 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
174 return -IPSET_ERR_INVALID_CIDR;
176 data.ip &= ip_set_netmask(data.cidr);
178 if (tb[IPSET_ATTR_TIMEOUT]) {
179 if (!with_timeout(h->timeout))
180 return -IPSET_ERR_TIMEOUT;
181 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
184 ret = adtfn(set, &data, timeout);
186 return ip_set_eexist(ret, flags) ? 0 : ret;
190 hash_net_same_set(const struct ip_set *a, const struct ip_set *b)
192 const struct ip_set_hash *x = a->data;
193 const struct ip_set_hash *y = b->data;
195 /* Resizing changes htable_bits, so we ignore it */
196 return x->maxelem == y->maxelem &&
197 x->timeout == y->timeout;
200 /* The type variant functions: IPv6 */
202 struct hash_net6_elem {
203 union nf_inet_addr ip;
209 struct hash_net6_telem {
210 union nf_inet_addr ip;
214 unsigned long timeout;
218 hash_net6_data_equal(const struct hash_net6_elem *ip1,
219 const struct hash_net6_elem *ip2)
221 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
222 ip1->cidr == ip2->cidr;
226 hash_net6_data_isnull(const struct hash_net6_elem *elem)
228 return elem->cidr == 0;
232 hash_net6_data_copy(struct hash_net6_elem *dst,
233 const struct hash_net6_elem *src)
235 ipv6_addr_copy(&dst->ip.in6, &src->ip.in6);
236 dst->cidr = src->cidr;
240 hash_net6_data_zero_out(struct hash_net6_elem *elem)
246 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
248 ip->ip6[0] &= ip_set_netmask6(prefix)[0];
249 ip->ip6[1] &= ip_set_netmask6(prefix)[1];
250 ip->ip6[2] &= ip_set_netmask6(prefix)[2];
251 ip->ip6[3] &= ip_set_netmask6(prefix)[3];
255 hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr)
257 ip6_netmask(&elem->ip, cidr);
262 hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data)
264 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &data->ip);
265 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
273 hash_net6_data_tlist(struct sk_buff *skb, const struct hash_net6_elem *data)
275 const struct hash_net6_telem *e =
276 (const struct hash_net6_telem *)data;
278 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &e->ip);
279 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, e->cidr);
280 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
281 htonl(ip_set_timeout_get(e->timeout)));
292 #define HOST_MASK 128
293 #include <linux/netfilter/ipset/ip_set_ahash.h>
296 hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
297 enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
299 const struct ip_set_hash *h = set->data;
300 ipset_adtfn adtfn = set->variant->adt[adt];
301 struct hash_net6_elem data = {
302 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
307 if (adt == IPSET_TEST)
308 data.cidr = HOST_MASK;
310 ip6addrptr(skb, flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
311 ip6_netmask(&data.ip, data.cidr);
313 return adtfn(set, &data, h->timeout);
317 hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
318 enum ipset_adt adt, u32 *lineno, u32 flags)
320 const struct ip_set_hash *h = set->data;
321 ipset_adtfn adtfn = set->variant->adt[adt];
322 struct hash_net6_elem data = { .cidr = HOST_MASK };
323 u32 timeout = h->timeout;
326 if (unlikely(!tb[IPSET_ATTR_IP] ||
327 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
328 return -IPSET_ERR_PROTOCOL;
330 if (tb[IPSET_ATTR_LINENO])
331 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
333 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
337 if (tb[IPSET_ATTR_CIDR])
338 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
341 return -IPSET_ERR_INVALID_CIDR;
343 ip6_netmask(&data.ip, data.cidr);
345 if (tb[IPSET_ATTR_TIMEOUT]) {
346 if (!with_timeout(h->timeout))
347 return -IPSET_ERR_TIMEOUT;
348 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
351 ret = adtfn(set, &data, timeout);
353 return ip_set_eexist(ret, flags) ? 0 : ret;
356 /* Create hash:ip type of sets */
359 hash_net_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
361 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
362 struct ip_set_hash *h;
365 if (!(set->family == AF_INET || set->family == AF_INET6))
366 return -IPSET_ERR_INVALID_FAMILY;
368 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
369 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
370 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
371 return -IPSET_ERR_PROTOCOL;
373 if (tb[IPSET_ATTR_HASHSIZE]) {
374 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
375 if (hashsize < IPSET_MIMINAL_HASHSIZE)
376 hashsize = IPSET_MIMINAL_HASHSIZE;
379 if (tb[IPSET_ATTR_MAXELEM])
380 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
382 h = kzalloc(sizeof(*h)
383 + sizeof(struct ip_set_hash_nets)
384 * (set->family == AF_INET ? 32 : 128), GFP_KERNEL);
388 h->maxelem = maxelem;
389 get_random_bytes(&h->initval, sizeof(h->initval));
390 h->timeout = IPSET_NO_TIMEOUT;
392 hbits = htable_bits(hashsize);
393 h->table = ip_set_alloc(
394 sizeof(struct htable)
395 + jhash_size(hbits) * sizeof(struct hbucket));
400 h->table->htable_bits = hbits;
404 if (tb[IPSET_ATTR_TIMEOUT]) {
405 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
407 set->variant = set->family == AF_INET
408 ? &hash_net4_tvariant : &hash_net6_tvariant;
410 if (set->family == AF_INET)
411 hash_net4_gc_init(set);
413 hash_net6_gc_init(set);
415 set->variant = set->family == AF_INET
416 ? &hash_net4_variant : &hash_net6_variant;
419 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
420 set->name, jhash_size(h->table->htable_bits),
421 h->table->htable_bits, h->maxelem, set->data, h->table);
426 static struct ip_set_type hash_net_type __read_mostly = {
428 .protocol = IPSET_PROTOCOL,
429 .features = IPSET_TYPE_IP,
430 .dimension = IPSET_DIM_ONE,
433 .create = hash_net_create,
435 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
436 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
437 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
438 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
439 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
442 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
443 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
444 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
452 return ip_set_type_register(&hash_net_type);
458 ip_set_type_unregister(&hash_net_type);
461 module_init(hash_net_init);
462 module_exit(hash_net_fini);