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,port 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_getport.h>
28 #include <linux/netfilter/ipset/ip_set_hash.h>
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
32 MODULE_DESCRIPTION("hash:net,port type of IP sets");
33 MODULE_ALIAS("ip_set_hash:net,port");
35 /* Type specific function prefix */
36 #define TYPE hash_netport
39 hash_netport_same_set(const struct ip_set *a, const struct ip_set *b);
41 #define hash_netport4_same_set hash_netport_same_set
42 #define hash_netport6_same_set hash_netport_same_set
44 /* The type variant functions: IPv4 */
46 /* Member elements without timeout */
47 struct hash_netport4_elem {
54 /* Member elements with timeout support */
55 struct hash_netport4_telem {
60 unsigned long timeout;
64 hash_netport4_data_equal(const struct hash_netport4_elem *ip1,
65 const struct hash_netport4_elem *ip2)
67 return ip1->ip == ip2->ip &&
68 ip1->port == ip2->port &&
69 ip1->proto == ip2->proto &&
70 ip1->cidr == ip2->cidr;
74 hash_netport4_data_isnull(const struct hash_netport4_elem *elem)
76 return elem->proto == 0;
80 hash_netport4_data_copy(struct hash_netport4_elem *dst,
81 const struct hash_netport4_elem *src)
84 dst->port = src->port;
85 dst->proto = src->proto;
86 dst->cidr = src->cidr;
90 hash_netport4_data_netmask(struct hash_netport4_elem *elem, u8 cidr)
92 elem->ip &= ip_set_netmask(cidr);
97 hash_netport4_data_zero_out(struct hash_netport4_elem *elem)
103 hash_netport4_data_list(struct sk_buff *skb,
104 const struct hash_netport4_elem *data)
106 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, data->ip);
107 NLA_PUT_NET16(skb, IPSET_ATTR_PORT, data->port);
108 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
109 NLA_PUT_U8(skb, IPSET_ATTR_PROTO, data->proto);
117 hash_netport4_data_tlist(struct sk_buff *skb,
118 const struct hash_netport4_elem *data)
120 const struct hash_netport4_telem *tdata =
121 (const struct hash_netport4_telem *)data;
123 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, tdata->ip);
124 NLA_PUT_NET16(skb, IPSET_ATTR_PORT, tdata->port);
125 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
126 NLA_PUT_U8(skb, IPSET_ATTR_PROTO, data->proto);
127 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
128 htonl(ip_set_timeout_get(tdata->timeout)));
136 #define IP_SET_HASH_WITH_PROTO
137 #define IP_SET_HASH_WITH_NETS
141 #include <linux/netfilter/ipset/ip_set_ahash.h>
144 hash_netport4_kadt(struct ip_set *set, const struct sk_buff *skb,
145 enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
147 const struct ip_set_hash *h = set->data;
148 ipset_adtfn adtfn = set->variant->adt[adt];
149 struct hash_netport4_elem data = {
150 .cidr = h->nets[0].cidr || HOST_MASK };
154 if (adt == IPSET_TEST)
155 data.cidr = HOST_MASK;
157 if (!ip_set_get_ip4_port(skb, flags & IPSET_DIM_TWO_SRC,
158 &data.port, &data.proto))
161 ip4addrptr(skb, flags & IPSET_DIM_ONE_SRC, &data.ip);
162 data.ip &= ip_set_netmask(data.cidr);
164 return adtfn(set, &data, h->timeout);
168 hash_netport4_uadt(struct ip_set *set, struct nlattr *tb[],
169 enum ipset_adt adt, u32 *lineno, u32 flags)
171 const struct ip_set_hash *h = set->data;
172 ipset_adtfn adtfn = set->variant->adt[adt];
173 struct hash_netport4_elem data = { .cidr = HOST_MASK };
175 u32 timeout = h->timeout;
178 if (unlikely(!tb[IPSET_ATTR_IP] ||
179 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
180 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
181 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
182 return -IPSET_ERR_PROTOCOL;
184 if (tb[IPSET_ATTR_LINENO])
185 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
187 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &data.ip);
191 if (tb[IPSET_ATTR_CIDR])
192 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
194 return -IPSET_ERR_INVALID_CIDR;
195 data.ip &= ip_set_netmask(data.cidr);
197 if (tb[IPSET_ATTR_PORT])
198 data.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
200 return -IPSET_ERR_PROTOCOL;
202 if (tb[IPSET_ATTR_PROTO]) {
203 data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
206 return -IPSET_ERR_INVALID_PROTO;
208 return -IPSET_ERR_MISSING_PROTO;
210 switch (data.proto) {
220 if (tb[IPSET_ATTR_TIMEOUT]) {
221 if (!with_timeout(h->timeout))
222 return -IPSET_ERR_TIMEOUT;
223 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
226 if (adt == IPSET_TEST ||
227 !(data.proto == IPPROTO_TCP || data.proto == IPPROTO_UDP) ||
228 !tb[IPSET_ATTR_PORT_TO]) {
229 ret = adtfn(set, &data, timeout);
230 return ip_set_eexist(ret, flags) ? 0 : ret;
233 port = ntohs(data.port);
234 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
238 for (; port <= port_to; port++) {
239 data.port = htons(port);
240 ret = adtfn(set, &data, timeout);
242 if (ret && !ip_set_eexist(ret, flags))
251 hash_netport_same_set(const struct ip_set *a, const struct ip_set *b)
253 const struct ip_set_hash *x = a->data;
254 const struct ip_set_hash *y = b->data;
256 /* Resizing changes htable_bits, so we ignore it */
257 return x->maxelem == y->maxelem &&
258 x->timeout == y->timeout;
261 /* The type variant functions: IPv6 */
263 struct hash_netport6_elem {
264 union nf_inet_addr ip;
270 struct hash_netport6_telem {
271 union nf_inet_addr ip;
275 unsigned long timeout;
279 hash_netport6_data_equal(const struct hash_netport6_elem *ip1,
280 const struct hash_netport6_elem *ip2)
282 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
283 ip1->port == ip2->port &&
284 ip1->proto == ip2->proto &&
285 ip1->cidr == ip2->cidr;
289 hash_netport6_data_isnull(const struct hash_netport6_elem *elem)
291 return elem->proto == 0;
295 hash_netport6_data_copy(struct hash_netport6_elem *dst,
296 const struct hash_netport6_elem *src)
298 memcpy(dst, src, sizeof(*dst));
302 hash_netport6_data_zero_out(struct hash_netport6_elem *elem)
308 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
310 ip->ip6[0] &= ip_set_netmask6(prefix)[0];
311 ip->ip6[1] &= ip_set_netmask6(prefix)[1];
312 ip->ip6[2] &= ip_set_netmask6(prefix)[2];
313 ip->ip6[3] &= ip_set_netmask6(prefix)[3];
317 hash_netport6_data_netmask(struct hash_netport6_elem *elem, u8 cidr)
319 ip6_netmask(&elem->ip, cidr);
324 hash_netport6_data_list(struct sk_buff *skb,
325 const struct hash_netport6_elem *data)
327 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &data->ip);
328 NLA_PUT_NET16(skb, IPSET_ATTR_PORT, data->port);
329 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
330 NLA_PUT_U8(skb, IPSET_ATTR_PROTO, data->proto);
338 hash_netport6_data_tlist(struct sk_buff *skb,
339 const struct hash_netport6_elem *data)
341 const struct hash_netport6_telem *e =
342 (const struct hash_netport6_telem *)data;
344 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &e->ip);
345 NLA_PUT_NET16(skb, IPSET_ATTR_PORT, data->port);
346 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
347 NLA_PUT_U8(skb, IPSET_ATTR_PROTO, data->proto);
348 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
349 htonl(ip_set_timeout_get(e->timeout)));
360 #define HOST_MASK 128
361 #include <linux/netfilter/ipset/ip_set_ahash.h>
364 hash_netport6_kadt(struct ip_set *set, const struct sk_buff *skb,
365 enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
367 const struct ip_set_hash *h = set->data;
368 ipset_adtfn adtfn = set->variant->adt[adt];
369 struct hash_netport6_elem data = {
370 .cidr = h->nets[0].cidr || HOST_MASK };
374 if (adt == IPSET_TEST)
375 data.cidr = HOST_MASK;
377 if (!ip_set_get_ip6_port(skb, flags & IPSET_DIM_TWO_SRC,
378 &data.port, &data.proto))
381 ip6addrptr(skb, flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
382 ip6_netmask(&data.ip, data.cidr);
384 return adtfn(set, &data, h->timeout);
388 hash_netport6_uadt(struct ip_set *set, struct nlattr *tb[],
389 enum ipset_adt adt, u32 *lineno, u32 flags)
391 const struct ip_set_hash *h = set->data;
392 ipset_adtfn adtfn = set->variant->adt[adt];
393 struct hash_netport6_elem data = { .cidr = HOST_MASK };
395 u32 timeout = h->timeout;
398 if (unlikely(!tb[IPSET_ATTR_IP] ||
399 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
400 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
401 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
402 return -IPSET_ERR_PROTOCOL;
404 if (tb[IPSET_ATTR_LINENO])
405 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
407 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
411 if (tb[IPSET_ATTR_CIDR])
412 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
414 return -IPSET_ERR_INVALID_CIDR;
415 ip6_netmask(&data.ip, data.cidr);
417 if (tb[IPSET_ATTR_PORT])
418 data.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
420 return -IPSET_ERR_PROTOCOL;
422 if (tb[IPSET_ATTR_PROTO]) {
423 data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
426 return -IPSET_ERR_INVALID_PROTO;
428 return -IPSET_ERR_MISSING_PROTO;
430 switch (data.proto) {
440 if (tb[IPSET_ATTR_TIMEOUT]) {
441 if (!with_timeout(h->timeout))
442 return -IPSET_ERR_TIMEOUT;
443 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
446 if (adt == IPSET_TEST ||
447 !(data.proto == IPPROTO_TCP || data.proto == IPPROTO_UDP) ||
448 !tb[IPSET_ATTR_PORT_TO]) {
449 ret = adtfn(set, &data, timeout);
450 return ip_set_eexist(ret, flags) ? 0 : ret;
453 port = ntohs(data.port);
454 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
458 for (; port <= port_to; port++) {
459 data.port = htons(port);
460 ret = adtfn(set, &data, timeout);
462 if (ret && !ip_set_eexist(ret, flags))
470 /* Create hash:ip type of sets */
473 hash_netport_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
475 struct ip_set_hash *h;
476 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
479 if (!(set->family == AF_INET || set->family == AF_INET6))
480 return -IPSET_ERR_INVALID_FAMILY;
482 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
483 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
484 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
485 return -IPSET_ERR_PROTOCOL;
487 if (tb[IPSET_ATTR_HASHSIZE]) {
488 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
489 if (hashsize < IPSET_MIMINAL_HASHSIZE)
490 hashsize = IPSET_MIMINAL_HASHSIZE;
493 if (tb[IPSET_ATTR_MAXELEM])
494 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
496 h = kzalloc(sizeof(*h)
497 + sizeof(struct ip_set_hash_nets)
498 * (set->family == AF_INET ? 32 : 128), GFP_KERNEL);
502 h->maxelem = maxelem;
503 get_random_bytes(&h->initval, sizeof(h->initval));
504 h->timeout = IPSET_NO_TIMEOUT;
506 hbits = htable_bits(hashsize);
507 h->table = ip_set_alloc(
508 sizeof(struct htable)
509 + jhash_size(hbits) * sizeof(struct hbucket));
514 h->table->htable_bits = hbits;
518 if (tb[IPSET_ATTR_TIMEOUT]) {
519 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
521 set->variant = set->family == AF_INET
522 ? &hash_netport4_tvariant : &hash_netport6_tvariant;
524 if (set->family == AF_INET)
525 hash_netport4_gc_init(set);
527 hash_netport6_gc_init(set);
529 set->variant = set->family == AF_INET
530 ? &hash_netport4_variant : &hash_netport6_variant;
533 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
534 set->name, jhash_size(h->table->htable_bits),
535 h->table->htable_bits, h->maxelem, set->data, h->table);
540 static struct ip_set_type hash_netport_type __read_mostly = {
541 .name = "hash:net,port",
542 .protocol = IPSET_PROTOCOL,
543 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT,
544 .dimension = IPSET_DIM_TWO,
547 .create = hash_netport_create,
549 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
550 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
551 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
552 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
553 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
554 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
557 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
558 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
559 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
560 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
561 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
562 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
563 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
569 hash_netport_init(void)
571 return ip_set_type_register(&hash_netport_type);
575 hash_netport_fini(void)
577 ip_set_type_unregister(&hash_netport_type);
580 module_init(hash_netport_init);
581 module_exit(hash_netport_fini);