1 /* Copyright (C) 2003-2013 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:ip,port,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>
21 #include <linux/netfilter.h>
22 #include <linux/netfilter/ipset/pfxlen.h>
23 #include <linux/netfilter/ipset/ip_set.h>
24 #include <linux/netfilter/ipset/ip_set_getport.h>
25 #include <linux/netfilter/ipset/ip_set_hash.h>
27 #define REVISION_MIN 0
28 /* 1 SCTP and UDPLITE support added */
29 /* 2 Range as input support for IPv4 added */
30 /* 3 nomatch flag support added */
31 #define REVISION_MAX 4 /* Counters support added */
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
35 IP_SET_MODULE_DESC("hash:ip,port,net", REVISION_MIN, REVISION_MAX);
36 MODULE_ALIAS("ip_set_hash:ip,port,net");
38 /* Type specific function prefix */
39 #define HTYPE hash_ipportnet
41 /* We squeeze the "nomatch" flag into cidr: we don't support cidr == 0
42 * However this way we have to store internally cidr - 1,
43 * dancing back and forth.
45 #define IP_SET_HASH_WITH_NETS_PACKED
46 #define IP_SET_HASH_WITH_PROTO
47 #define IP_SET_HASH_WITH_NETS
52 struct hash_ipportnet4_elem {
61 struct hash_ipportnet4t_elem {
68 unsigned long timeout;
71 struct hash_ipportnet4c_elem {
78 struct ip_set_counter counter;
81 struct hash_ipportnet4ct_elem {
88 struct ip_set_counter counter;
89 unsigned long timeout;
92 /* Common functions */
95 hash_ipportnet4_data_equal(const struct hash_ipportnet4_elem *ip1,
96 const struct hash_ipportnet4_elem *ip2,
99 return ip1->ip == ip2->ip &&
100 ip1->ip2 == ip2->ip2 &&
101 ip1->cidr == ip2->cidr &&
102 ip1->port == ip2->port &&
103 ip1->proto == ip2->proto;
107 hash_ipportnet4_do_data_match(const struct hash_ipportnet4_elem *elem)
109 return elem->nomatch ? -ENOTEMPTY : 1;
113 hash_ipportnet4_data_set_flags(struct hash_ipportnet4_elem *elem, u32 flags)
115 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
119 hash_ipportnet4_data_reset_flags(struct hash_ipportnet4_elem *elem, u8 *flags)
121 swap(*flags, elem->nomatch);
125 hash_ipportnet4_data_netmask(struct hash_ipportnet4_elem *elem, u8 cidr)
127 elem->ip2 &= ip_set_netmask(cidr);
128 elem->cidr = cidr - 1;
132 hash_ipportnet4_data_list(struct sk_buff *skb,
133 const struct hash_ipportnet4_elem *data)
135 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
137 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
138 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip2) ||
139 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
140 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr + 1) ||
141 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
143 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
144 goto nla_put_failure;
152 hash_ipportnet4_data_next(struct hash_ipportnet4_elem *next,
153 const struct hash_ipportnet4_elem *d)
156 next->port = d->port;
160 #define MTYPE hash_ipportnet4
163 #include "ip_set_hash_gen.h"
166 hash_ipportnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
167 const struct xt_action_param *par,
168 enum ipset_adt adt, struct ip_set_adt_opt *opt)
170 const struct hash_ipportnet *h = set->data;
171 ipset_adtfn adtfn = set->variant->adt[adt];
172 struct hash_ipportnet4_elem e = {
173 .cidr = h->nets[0].cidr ? h->nets[0].cidr - 1 : HOST_MASK - 1
175 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h);
177 if (adt == IPSET_TEST)
178 e.cidr = HOST_MASK - 1;
180 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
184 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
185 ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip2);
186 e.ip2 &= ip_set_netmask(e.cidr + 1);
188 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
192 hash_ipportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
193 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
195 const struct hash_ipportnet *h = set->data;
196 ipset_adtfn adtfn = set->variant->adt[adt];
197 struct hash_ipportnet4_elem e = { .cidr = HOST_MASK - 1 };
198 struct ip_set_ext ext = IP_SET_INIT_UEXT(h);
199 u32 ip, ip_to, p = 0, port, port_to;
200 u32 ip2_from, ip2_to, ip2_last, ip2;
201 bool with_ports = false;
205 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
206 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
207 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
208 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
209 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
210 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
211 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
212 return -IPSET_ERR_PROTOCOL;
214 if (tb[IPSET_ATTR_LINENO])
215 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
217 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) ||
218 ip_set_get_extensions(set, tb, &ext);
222 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from);
226 if (tb[IPSET_ATTR_CIDR2]) {
227 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
228 if (!cidr || cidr > HOST_MASK)
229 return -IPSET_ERR_INVALID_CIDR;
233 if (tb[IPSET_ATTR_PORT])
234 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
236 return -IPSET_ERR_PROTOCOL;
238 if (tb[IPSET_ATTR_PROTO]) {
239 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
240 with_ports = ip_set_proto_with_ports(e.proto);
243 return -IPSET_ERR_INVALID_PROTO;
245 return -IPSET_ERR_MISSING_PROTO;
247 if (!(with_ports || e.proto == IPPROTO_ICMP))
250 if (tb[IPSET_ATTR_CADT_FLAGS]) {
251 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
252 if (cadt_flags & IPSET_FLAG_NOMATCH)
253 flags |= (IPSET_FLAG_NOMATCH << 16);
256 with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
257 if (adt == IPSET_TEST ||
258 !(tb[IPSET_ATTR_CIDR] || tb[IPSET_ATTR_IP_TO] || with_ports ||
259 tb[IPSET_ATTR_IP2_TO])) {
261 e.ip2 = htonl(ip2_from & ip_set_hostmask(e.cidr + 1));
262 ret = adtfn(set, &e, &ext, &ext, flags);
263 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
264 ip_set_eexist(ret, flags) ? 0 : ret;
268 if (tb[IPSET_ATTR_IP_TO]) {
269 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
274 } else if (tb[IPSET_ATTR_CIDR]) {
275 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
277 if (!cidr || cidr > 32)
278 return -IPSET_ERR_INVALID_CIDR;
279 ip_set_mask_from_to(ip, ip_to, cidr);
282 port_to = port = ntohs(e.port);
283 if (tb[IPSET_ATTR_PORT_TO]) {
284 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
290 if (tb[IPSET_ATTR_IP2_TO]) {
291 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to);
294 if (ip2_from > ip2_to)
295 swap(ip2_from, ip2_to);
296 if (ip2_from + UINT_MAX == ip2_to)
297 return -IPSET_ERR_HASH_RANGE;
299 ip_set_mask_from_to(ip2_from, ip2_to, e.cidr + 1);
302 ip = ntohl(h->next.ip);
303 for (; !before(ip_to, ip); ip++) {
305 p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port)
307 for (; p <= port_to; p++) {
310 && ip == ntohl(h->next.ip)
311 && p == ntohs(h->next.port)
312 ? ntohl(h->next.ip2) : ip2_from;
313 while (!after(ip2, ip2_to)) {
315 ip2_last = ip_set_range_to_cidr(ip2, ip2_to,
318 ret = adtfn(set, &e, &ext, &ext, flags);
320 if (ret && !ip_set_eexist(ret, flags))
333 struct hash_ipportnet6_elem {
334 union nf_inet_addr ip;
335 union nf_inet_addr ip2;
342 struct hash_ipportnet6t_elem {
343 union nf_inet_addr ip;
344 union nf_inet_addr ip2;
349 unsigned long timeout;
352 struct hash_ipportnet6c_elem {
353 union nf_inet_addr ip;
354 union nf_inet_addr ip2;
359 struct ip_set_counter counter;
362 struct hash_ipportnet6ct_elem {
363 union nf_inet_addr ip;
364 union nf_inet_addr ip2;
369 struct ip_set_counter counter;
370 unsigned long timeout;
373 /* Common functions */
376 hash_ipportnet6_data_equal(const struct hash_ipportnet6_elem *ip1,
377 const struct hash_ipportnet6_elem *ip2,
380 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
381 ipv6_addr_equal(&ip1->ip2.in6, &ip2->ip2.in6) &&
382 ip1->cidr == ip2->cidr &&
383 ip1->port == ip2->port &&
384 ip1->proto == ip2->proto;
388 hash_ipportnet6_do_data_match(const struct hash_ipportnet6_elem *elem)
390 return elem->nomatch ? -ENOTEMPTY : 1;
394 hash_ipportnet6_data_set_flags(struct hash_ipportnet6_elem *elem, u32 flags)
396 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
400 hash_ipportnet6_data_reset_flags(struct hash_ipportnet6_elem *elem, u8 *flags)
402 swap(*flags, elem->nomatch);
406 hash_ipportnet6_data_netmask(struct hash_ipportnet6_elem *elem, u8 cidr)
408 ip6_netmask(&elem->ip2, cidr);
409 elem->cidr = cidr - 1;
413 hash_ipportnet6_data_list(struct sk_buff *skb,
414 const struct hash_ipportnet6_elem *data)
416 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
418 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
419 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip2.in6) ||
420 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
421 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr + 1) ||
422 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
424 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
425 goto nla_put_failure;
433 hash_ipportnet6_data_next(struct hash_ipportnet4_elem *next,
434 const struct hash_ipportnet6_elem *d)
436 next->port = d->port;
443 #define MTYPE hash_ipportnet6
445 #define HOST_MASK 128
446 #define IP_SET_EMIT_CREATE
447 #include "ip_set_hash_gen.h"
450 hash_ipportnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
451 const struct xt_action_param *par,
452 enum ipset_adt adt, struct ip_set_adt_opt *opt)
454 const struct hash_ipportnet *h = set->data;
455 ipset_adtfn adtfn = set->variant->adt[adt];
456 struct hash_ipportnet6_elem e = {
457 .cidr = h->nets[0].cidr ? h->nets[0].cidr - 1 : HOST_MASK - 1
459 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h);
461 if (adt == IPSET_TEST)
462 e.cidr = HOST_MASK - 1;
464 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
468 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
469 ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip2.in6);
470 ip6_netmask(&e.ip2, e.cidr + 1);
472 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
476 hash_ipportnet6_uadt(struct ip_set *set, struct nlattr *tb[],
477 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
479 const struct hash_ipportnet *h = set->data;
480 ipset_adtfn adtfn = set->variant->adt[adt];
481 struct hash_ipportnet6_elem e = { .cidr = HOST_MASK - 1 };
482 struct ip_set_ext ext = IP_SET_INIT_UEXT(h);
484 bool with_ports = false;
488 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
489 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
490 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
491 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
492 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
493 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
494 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
495 tb[IPSET_ATTR_IP_TO] ||
496 tb[IPSET_ATTR_CIDR]))
497 return -IPSET_ERR_PROTOCOL;
498 if (unlikely(tb[IPSET_ATTR_IP_TO]))
499 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
501 if (tb[IPSET_ATTR_LINENO])
502 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
504 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
505 ip_set_get_extensions(set, tb, &ext);
509 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip2);
513 if (tb[IPSET_ATTR_CIDR2]) {
514 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
515 if (!cidr || cidr > HOST_MASK)
516 return -IPSET_ERR_INVALID_CIDR;
520 ip6_netmask(&e.ip2, e.cidr + 1);
522 if (tb[IPSET_ATTR_PORT])
523 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
525 return -IPSET_ERR_PROTOCOL;
527 if (tb[IPSET_ATTR_PROTO]) {
528 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
529 with_ports = ip_set_proto_with_ports(e.proto);
532 return -IPSET_ERR_INVALID_PROTO;
534 return -IPSET_ERR_MISSING_PROTO;
536 if (!(with_ports || e.proto == IPPROTO_ICMPV6))
539 if (tb[IPSET_ATTR_CADT_FLAGS]) {
540 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
541 if (cadt_flags & IPSET_FLAG_NOMATCH)
542 flags |= (IPSET_FLAG_NOMATCH << 16);
545 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
546 ret = adtfn(set, &e, &ext, &ext, flags);
547 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
548 ip_set_eexist(ret, flags) ? 0 : ret;
551 port = ntohs(e.port);
552 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
557 port = ntohs(h->next.port);
558 for (; port <= port_to; port++) {
559 e.port = htons(port);
560 ret = adtfn(set, &e, &ext, &ext, flags);
562 if (ret && !ip_set_eexist(ret, flags))
570 static struct ip_set_type hash_ipportnet_type __read_mostly = {
571 .name = "hash:ip,port,net",
572 .protocol = IPSET_PROTOCOL,
573 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2 |
575 .dimension = IPSET_DIM_THREE,
576 .family = NFPROTO_UNSPEC,
577 .revision_min = REVISION_MIN,
578 .revision_max = REVISION_MAX,
579 .create = hash_ipportnet_create,
581 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
582 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
583 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
584 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
585 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
586 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
589 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
590 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
591 [IPSET_ATTR_IP2] = { .type = NLA_NESTED },
592 [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED },
593 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
594 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
595 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
596 [IPSET_ATTR_CIDR2] = { .type = NLA_U8 },
597 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
598 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
599 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
600 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
601 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
602 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
608 hash_ipportnet_init(void)
610 return ip_set_type_register(&hash_ipportnet_type);
614 hash_ipportnet_fini(void)
616 ip_set_type_unregister(&hash_ipportnet_type);
619 module_init(hash_ipportnet_init);
620 module_exit(hash_ipportnet_fini);