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/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_getport.h>
25 #include <linux/netfilter/ipset/ip_set_hash.h>
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
29 MODULE_DESCRIPTION("hash:net,port type of IP sets");
30 MODULE_ALIAS("ip_set_hash:net,port");
32 /* Type specific function prefix */
33 #define TYPE hash_netport
36 hash_netport_same_set(const struct ip_set *a, const struct ip_set *b);
38 #define hash_netport4_same_set hash_netport_same_set
39 #define hash_netport6_same_set hash_netport_same_set
41 /* The type variant functions: IPv4 */
43 /* We squeeze the "nomatch" flag into cidr: we don't support cidr == 0
44 * However this way we have to store internally cidr - 1,
45 * dancing back and forth.
47 #define IP_SET_HASH_WITH_NETS_PACKED
49 /* Member elements without timeout */
50 struct hash_netport4_elem {
58 /* Member elements with timeout support */
59 struct hash_netport4_telem {
65 unsigned long timeout;
69 hash_netport4_data_equal(const struct hash_netport4_elem *ip1,
70 const struct hash_netport4_elem *ip2,
73 return ip1->ip == ip2->ip &&
74 ip1->port == ip2->port &&
75 ip1->proto == ip2->proto &&
76 ip1->cidr == ip2->cidr;
80 hash_netport4_data_isnull(const struct hash_netport4_elem *elem)
82 return elem->proto == 0;
86 hash_netport4_data_copy(struct hash_netport4_elem *dst,
87 const struct hash_netport4_elem *src)
90 dst->port = src->port;
91 dst->proto = src->proto;
92 dst->cidr = src->cidr;
93 dst->nomatch = src->nomatch;
97 hash_netport4_data_flags(struct hash_netport4_elem *dst, u32 flags)
99 dst->nomatch = !!(flags & IPSET_FLAG_NOMATCH);
103 hash_netport4_data_match(const struct hash_netport4_elem *elem)
105 return !elem->nomatch;
109 hash_netport4_data_netmask(struct hash_netport4_elem *elem, u8 cidr)
111 elem->ip &= ip_set_netmask(cidr);
112 elem->cidr = cidr - 1;
116 hash_netport4_data_zero_out(struct hash_netport4_elem *elem)
122 hash_netport4_data_list(struct sk_buff *skb,
123 const struct hash_netport4_elem *data)
125 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
127 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
128 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
129 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr + 1) ||
130 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
132 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
133 goto nla_put_failure;
141 hash_netport4_data_tlist(struct sk_buff *skb,
142 const struct hash_netport4_elem *data)
144 const struct hash_netport4_telem *tdata =
145 (const struct hash_netport4_telem *)data;
146 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
148 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, tdata->ip) ||
149 nla_put_net16(skb, IPSET_ATTR_PORT, tdata->port) ||
150 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr + 1) ||
151 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
152 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
153 htonl(ip_set_timeout_get(tdata->timeout))) ||
155 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
156 goto nla_put_failure;
163 #define IP_SET_HASH_WITH_PROTO
164 #define IP_SET_HASH_WITH_NETS
168 #include <linux/netfilter/ipset/ip_set_ahash.h>
171 hash_netport4_data_next(struct ip_set_hash *h,
172 const struct hash_netport4_elem *d)
174 h->next.ip = ntohl(d->ip);
175 h->next.port = ntohs(d->port);
179 hash_netport4_kadt(struct ip_set *set, const struct sk_buff *skb,
180 const struct xt_action_param *par,
181 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
183 const struct ip_set_hash *h = set->data;
184 ipset_adtfn adtfn = set->variant->adt[adt];
185 struct hash_netport4_elem data = {
186 .cidr = h->nets[0].cidr ? h->nets[0].cidr - 1 : HOST_MASK - 1
189 if (adt == IPSET_TEST)
190 data.cidr = HOST_MASK - 1;
192 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
193 &data.port, &data.proto))
196 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip);
197 data.ip &= ip_set_netmask(data.cidr + 1);
199 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
203 hash_netport4_uadt(struct ip_set *set, struct nlattr *tb[],
204 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
206 const struct ip_set_hash *h = set->data;
207 ipset_adtfn adtfn = set->variant->adt[adt];
208 struct hash_netport4_elem data = { .cidr = HOST_MASK - 1 };
209 u32 port, port_to, p = 0, ip = 0, ip_to, last;
210 u32 timeout = h->timeout;
211 bool with_ports = false;
215 if (unlikely(!tb[IPSET_ATTR_IP] ||
216 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
217 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
218 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
219 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
220 return -IPSET_ERR_PROTOCOL;
222 if (tb[IPSET_ATTR_LINENO])
223 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
225 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
229 if (tb[IPSET_ATTR_CIDR]) {
230 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
231 if (!cidr || cidr > HOST_MASK)
232 return -IPSET_ERR_INVALID_CIDR;
233 data.cidr = cidr - 1;
236 if (tb[IPSET_ATTR_PORT])
237 data.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
239 return -IPSET_ERR_PROTOCOL;
241 if (tb[IPSET_ATTR_PROTO]) {
242 data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
243 with_ports = ip_set_proto_with_ports(data.proto);
246 return -IPSET_ERR_INVALID_PROTO;
248 return -IPSET_ERR_MISSING_PROTO;
250 if (!(with_ports || data.proto == IPPROTO_ICMP))
253 if (tb[IPSET_ATTR_TIMEOUT]) {
254 if (!with_timeout(h->timeout))
255 return -IPSET_ERR_TIMEOUT;
256 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
259 with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
261 if (tb[IPSET_ATTR_CADT_FLAGS] && adt == IPSET_ADD) {
262 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
263 if (cadt_flags & IPSET_FLAG_NOMATCH)
264 flags |= (cadt_flags << 16);
267 if (adt == IPSET_TEST || !(with_ports || tb[IPSET_ATTR_IP_TO])) {
268 data.ip = htonl(ip & ip_set_hostmask(data.cidr + 1));
269 ret = adtfn(set, &data, timeout, flags);
270 return ip_set_eexist(ret, flags) ? 0 : ret;
273 port = port_to = ntohs(data.port);
274 if (tb[IPSET_ATTR_PORT_TO]) {
275 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
279 if (tb[IPSET_ATTR_IP_TO]) {
280 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
285 if (ip + UINT_MAX == ip_to)
286 return -IPSET_ERR_HASH_RANGE;
288 ip_set_mask_from_to(ip, ip_to, data.cidr + 1);
293 while (!after(ip, ip_to)) {
295 last = ip_set_range_to_cidr(ip, ip_to, &cidr);
296 data.cidr = cidr - 1;
297 p = retried && ip == h->next.ip ? h->next.port : port;
298 for (; p <= port_to; p++) {
299 data.port = htons(p);
300 ret = adtfn(set, &data, timeout, flags);
302 if (ret && !ip_set_eexist(ret, flags))
313 hash_netport_same_set(const struct ip_set *a, const struct ip_set *b)
315 const struct ip_set_hash *x = a->data;
316 const struct ip_set_hash *y = b->data;
318 /* Resizing changes htable_bits, so we ignore it */
319 return x->maxelem == y->maxelem &&
320 x->timeout == y->timeout;
323 /* The type variant functions: IPv6 */
325 struct hash_netport6_elem {
326 union nf_inet_addr ip;
333 struct hash_netport6_telem {
334 union nf_inet_addr ip;
339 unsigned long timeout;
343 hash_netport6_data_equal(const struct hash_netport6_elem *ip1,
344 const struct hash_netport6_elem *ip2,
347 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
348 ip1->port == ip2->port &&
349 ip1->proto == ip2->proto &&
350 ip1->cidr == ip2->cidr;
354 hash_netport6_data_isnull(const struct hash_netport6_elem *elem)
356 return elem->proto == 0;
360 hash_netport6_data_copy(struct hash_netport6_elem *dst,
361 const struct hash_netport6_elem *src)
363 memcpy(dst, src, sizeof(*dst));
367 hash_netport6_data_flags(struct hash_netport6_elem *dst, u32 flags)
369 dst->nomatch = !!(flags & IPSET_FLAG_NOMATCH);
373 hash_netport6_data_match(const struct hash_netport6_elem *elem)
375 return !elem->nomatch;
379 hash_netport6_data_zero_out(struct hash_netport6_elem *elem)
385 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
387 ip->ip6[0] &= ip_set_netmask6(prefix)[0];
388 ip->ip6[1] &= ip_set_netmask6(prefix)[1];
389 ip->ip6[2] &= ip_set_netmask6(prefix)[2];
390 ip->ip6[3] &= ip_set_netmask6(prefix)[3];
394 hash_netport6_data_netmask(struct hash_netport6_elem *elem, u8 cidr)
396 ip6_netmask(&elem->ip, cidr);
397 elem->cidr = cidr - 1;
401 hash_netport6_data_list(struct sk_buff *skb,
402 const struct hash_netport6_elem *data)
404 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
406 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
407 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
408 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr + 1) ||
409 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
411 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
412 goto nla_put_failure;
420 hash_netport6_data_tlist(struct sk_buff *skb,
421 const struct hash_netport6_elem *data)
423 const struct hash_netport6_telem *e =
424 (const struct hash_netport6_telem *)data;
425 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
427 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6) ||
428 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
429 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr + 1) ||
430 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
431 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
432 htonl(ip_set_timeout_get(e->timeout))) ||
434 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
435 goto nla_put_failure;
446 #define HOST_MASK 128
447 #include <linux/netfilter/ipset/ip_set_ahash.h>
450 hash_netport6_data_next(struct ip_set_hash *h,
451 const struct hash_netport6_elem *d)
453 h->next.port = ntohs(d->port);
457 hash_netport6_kadt(struct ip_set *set, const struct sk_buff *skb,
458 const struct xt_action_param *par,
459 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
461 const struct ip_set_hash *h = set->data;
462 ipset_adtfn adtfn = set->variant->adt[adt];
463 struct hash_netport6_elem data = {
464 .cidr = h->nets[0].cidr ? h->nets[0].cidr - 1 : HOST_MASK - 1,
467 if (adt == IPSET_TEST)
468 data.cidr = HOST_MASK - 1;
470 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
471 &data.port, &data.proto))
474 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
475 ip6_netmask(&data.ip, data.cidr + 1);
477 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
481 hash_netport6_uadt(struct ip_set *set, struct nlattr *tb[],
482 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
484 const struct ip_set_hash *h = set->data;
485 ipset_adtfn adtfn = set->variant->adt[adt];
486 struct hash_netport6_elem data = { .cidr = HOST_MASK - 1 };
488 u32 timeout = h->timeout;
489 bool with_ports = false;
493 if (unlikely(!tb[IPSET_ATTR_IP] ||
494 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
495 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
496 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
497 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
498 return -IPSET_ERR_PROTOCOL;
499 if (unlikely(tb[IPSET_ATTR_IP_TO]))
500 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
502 if (tb[IPSET_ATTR_LINENO])
503 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
505 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
509 if (tb[IPSET_ATTR_CIDR]) {
510 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
511 if (!cidr || cidr > HOST_MASK)
512 return -IPSET_ERR_INVALID_CIDR;
513 data.cidr = cidr - 1;
515 ip6_netmask(&data.ip, data.cidr + 1);
517 if (tb[IPSET_ATTR_PORT])
518 data.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
520 return -IPSET_ERR_PROTOCOL;
522 if (tb[IPSET_ATTR_PROTO]) {
523 data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
524 with_ports = ip_set_proto_with_ports(data.proto);
527 return -IPSET_ERR_INVALID_PROTO;
529 return -IPSET_ERR_MISSING_PROTO;
531 if (!(with_ports || data.proto == IPPROTO_ICMPV6))
534 if (tb[IPSET_ATTR_TIMEOUT]) {
535 if (!with_timeout(h->timeout))
536 return -IPSET_ERR_TIMEOUT;
537 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
540 if (tb[IPSET_ATTR_CADT_FLAGS] && adt == IPSET_ADD) {
541 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
542 if (cadt_flags & IPSET_FLAG_NOMATCH)
543 flags |= (cadt_flags << 16);
546 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
547 ret = adtfn(set, &data, timeout, flags);
548 return ip_set_eexist(ret, flags) ? 0 : ret;
551 port = ntohs(data.port);
552 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
558 for (; port <= port_to; port++) {
559 data.port = htons(port);
560 ret = adtfn(set, &data, timeout, flags);
562 if (ret && !ip_set_eexist(ret, flags))
570 /* Create hash:ip type of sets */
573 hash_netport_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
575 struct ip_set_hash *h;
576 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
580 if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6))
581 return -IPSET_ERR_INVALID_FAMILY;
583 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
584 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
585 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
586 return -IPSET_ERR_PROTOCOL;
588 if (tb[IPSET_ATTR_HASHSIZE]) {
589 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
590 if (hashsize < IPSET_MIMINAL_HASHSIZE)
591 hashsize = IPSET_MIMINAL_HASHSIZE;
594 if (tb[IPSET_ATTR_MAXELEM])
595 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
597 h = kzalloc(sizeof(*h)
598 + sizeof(struct ip_set_hash_nets)
599 * (set->family == NFPROTO_IPV4 ? 32 : 128), GFP_KERNEL);
603 h->maxelem = maxelem;
604 get_random_bytes(&h->initval, sizeof(h->initval));
605 h->timeout = IPSET_NO_TIMEOUT;
607 hbits = htable_bits(hashsize);
608 hsize = htable_size(hbits);
613 h->table = ip_set_alloc(hsize);
618 h->table->htable_bits = hbits;
622 if (tb[IPSET_ATTR_TIMEOUT]) {
623 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
625 set->variant = set->family == NFPROTO_IPV4
626 ? &hash_netport4_tvariant : &hash_netport6_tvariant;
628 if (set->family == NFPROTO_IPV4)
629 hash_netport4_gc_init(set);
631 hash_netport6_gc_init(set);
633 set->variant = set->family == NFPROTO_IPV4
634 ? &hash_netport4_variant : &hash_netport6_variant;
637 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
638 set->name, jhash_size(h->table->htable_bits),
639 h->table->htable_bits, h->maxelem, set->data, h->table);
644 static struct ip_set_type hash_netport_type __read_mostly = {
645 .name = "hash:net,port",
646 .protocol = IPSET_PROTOCOL,
647 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT,
648 .dimension = IPSET_DIM_TWO,
649 .family = NFPROTO_UNSPEC,
651 /* 1 SCTP and UDPLITE support added */
652 /* 2, Range as input support for IPv4 added */
653 .revision_max = 3, /* nomatch flag support added */
654 .create = hash_netport_create,
656 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
657 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
658 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
659 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
660 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
661 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
664 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
665 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
666 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
667 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
668 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
669 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
670 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
671 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
672 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
678 hash_netport_init(void)
680 return ip_set_type_register(&hash_netport_type);
684 hash_netport_fini(void)
686 ip_set_type_unregister(&hash_netport_type);
689 module_init(hash_netport_init);
690 module_exit(hash_netport_fini);