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 IPSET_TYPE_REV_MIN 0
28 /* 0 Comments support added */
29 #define IPSET_TYPE_REV_MAX 1 /* Forceadd support added */
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>");
33 IP_SET_MODULE_DESC("hash:net,port,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
34 MODULE_ALIAS("ip_set_hash:net,port,net");
36 /* Type specific function prefix */
37 #define HTYPE hash_netportnet
38 #define IP_SET_HASH_WITH_PROTO
39 #define IP_SET_HASH_WITH_NETS
40 #define IPSET_NET_COUNT 2
45 struct hash_netportnet4_elem {
59 /* Common functions */
62 hash_netportnet4_data_equal(const struct hash_netportnet4_elem *ip1,
63 const struct hash_netportnet4_elem *ip2,
66 return ip1->ipcmp == ip2->ipcmp &&
67 ip1->ccmp == ip2->ccmp &&
68 ip1->port == ip2->port &&
69 ip1->proto == ip2->proto;
73 hash_netportnet4_do_data_match(const struct hash_netportnet4_elem *elem)
75 return elem->nomatch ? -ENOTEMPTY : 1;
79 hash_netportnet4_data_set_flags(struct hash_netportnet4_elem *elem, u32 flags)
81 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
85 hash_netportnet4_data_reset_flags(struct hash_netportnet4_elem *elem, u8 *flags)
87 swap(*flags, elem->nomatch);
91 hash_netportnet4_data_reset_elem(struct hash_netportnet4_elem *elem,
92 struct hash_netportnet4_elem *orig)
94 elem->ip[1] = orig->ip[1];
98 hash_netportnet4_data_netmask(struct hash_netportnet4_elem *elem,
102 elem->ip[1] &= ip_set_netmask(cidr);
103 elem->cidr[1] = cidr;
105 elem->ip[0] &= ip_set_netmask(cidr);
106 elem->cidr[0] = cidr;
111 hash_netportnet4_data_list(struct sk_buff *skb,
112 const struct hash_netportnet4_elem *data)
114 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
116 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip[0]) ||
117 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip[1]) ||
118 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
119 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
120 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
121 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
123 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
124 goto nla_put_failure;
132 hash_netportnet4_data_next(struct hash_netportnet4_elem *next,
133 const struct hash_netportnet4_elem *d)
135 next->ipcmp = d->ipcmp;
136 next->port = d->port;
139 #define MTYPE hash_netportnet4
142 #include "ip_set_hash_gen.h"
145 hash_netportnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
146 const struct xt_action_param *par,
147 enum ipset_adt adt, struct ip_set_adt_opt *opt)
149 const struct hash_netportnet *h = set->data;
150 ipset_adtfn adtfn = set->variant->adt[adt];
151 struct hash_netportnet4_elem e = { };
152 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
154 e.cidr[0] = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
155 e.cidr[1] = IP_SET_INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
156 if (adt == IPSET_TEST)
157 e.ccmp = (HOST_MASK << (sizeof(e.cidr[0]) * 8)) | HOST_MASK;
159 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
163 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0]);
164 ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip[1]);
165 e.ip[0] &= ip_set_netmask(e.cidr[0]);
166 e.ip[1] &= ip_set_netmask(e.cidr[1]);
168 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
172 hash_netportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
173 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
175 const struct hash_netportnet *h = set->data;
176 ipset_adtfn adtfn = set->variant->adt[adt];
177 struct hash_netportnet4_elem e = { };
178 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
179 u32 ip = 0, ip_to = 0, ip_last, p = 0, port, port_to;
180 u32 ip2_from = 0, ip2_to = 0, ip2_last, ip2;
181 bool with_ports = false;
185 e.cidr[0] = e.cidr[1] = HOST_MASK;
186 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
187 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
188 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
189 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
190 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
191 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
192 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
193 return -IPSET_ERR_PROTOCOL;
195 if (tb[IPSET_ATTR_LINENO])
196 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
198 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) ||
199 ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from) ||
200 ip_set_get_extensions(set, tb, &ext);
204 if (tb[IPSET_ATTR_CIDR]) {
205 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
206 if (!cidr || cidr > HOST_MASK)
207 return -IPSET_ERR_INVALID_CIDR;
211 if (tb[IPSET_ATTR_CIDR2]) {
212 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
213 if (!cidr || cidr > HOST_MASK)
214 return -IPSET_ERR_INVALID_CIDR;
218 if (tb[IPSET_ATTR_PORT])
219 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
221 return -IPSET_ERR_PROTOCOL;
223 if (tb[IPSET_ATTR_PROTO]) {
224 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
225 with_ports = ip_set_proto_with_ports(e.proto);
228 return -IPSET_ERR_INVALID_PROTO;
230 return -IPSET_ERR_MISSING_PROTO;
232 if (!(with_ports || e.proto == IPPROTO_ICMP))
235 if (tb[IPSET_ATTR_CADT_FLAGS]) {
236 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
237 if (cadt_flags & IPSET_FLAG_NOMATCH)
238 flags |= (IPSET_FLAG_NOMATCH << 16);
241 with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
242 if (adt == IPSET_TEST ||
243 !(tb[IPSET_ATTR_IP_TO] || with_ports || tb[IPSET_ATTR_IP2_TO])) {
244 e.ip[0] = htonl(ip & ip_set_hostmask(e.cidr[0]));
245 e.ip[1] = htonl(ip2_from & ip_set_hostmask(e.cidr[1]));
246 ret = adtfn(set, &e, &ext, &ext, flags);
247 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
248 ip_set_eexist(ret, flags) ? 0 : ret;
252 if (tb[IPSET_ATTR_IP_TO]) {
253 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
258 if (unlikely(ip + UINT_MAX == ip_to))
259 return -IPSET_ERR_HASH_RANGE;
262 port_to = port = ntohs(e.port);
263 if (tb[IPSET_ATTR_PORT_TO]) {
264 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
270 if (tb[IPSET_ATTR_IP2_TO]) {
271 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to);
274 if (ip2_from > ip2_to)
275 swap(ip2_from, ip2_to);
276 if (unlikely(ip2_from + UINT_MAX == ip2_to))
277 return -IPSET_ERR_HASH_RANGE;
281 ip = ntohl(h->next.ip[0]);
283 while (!after(ip, ip_to)) {
285 ip_last = ip_set_range_to_cidr(ip, ip_to, &cidr);
287 p = retried && ip == ntohl(h->next.ip[0]) ? ntohs(h->next.port)
289 for (; p <= port_to; p++) {
291 ip2 = (retried && ip == ntohl(h->next.ip[0]) &&
292 p == ntohs(h->next.port)) ? ntohl(h->next.ip[1])
294 while (!after(ip2, ip2_to)) {
295 e.ip[1] = htonl(ip2);
296 ip2_last = ip_set_range_to_cidr(ip2, ip2_to,
299 ret = adtfn(set, &e, &ext, &ext, flags);
300 if (ret && !ip_set_eexist(ret, flags))
314 struct hash_netportnet6_elem {
315 union nf_inet_addr ip[2];
325 /* Common functions */
328 hash_netportnet6_data_equal(const struct hash_netportnet6_elem *ip1,
329 const struct hash_netportnet6_elem *ip2,
332 return ipv6_addr_equal(&ip1->ip[0].in6, &ip2->ip[0].in6) &&
333 ipv6_addr_equal(&ip1->ip[1].in6, &ip2->ip[1].in6) &&
334 ip1->ccmp == ip2->ccmp &&
335 ip1->port == ip2->port &&
336 ip1->proto == ip2->proto;
340 hash_netportnet6_do_data_match(const struct hash_netportnet6_elem *elem)
342 return elem->nomatch ? -ENOTEMPTY : 1;
346 hash_netportnet6_data_set_flags(struct hash_netportnet6_elem *elem, u32 flags)
348 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
352 hash_netportnet6_data_reset_flags(struct hash_netportnet6_elem *elem, u8 *flags)
354 swap(*flags, elem->nomatch);
358 hash_netportnet6_data_reset_elem(struct hash_netportnet6_elem *elem,
359 struct hash_netportnet6_elem *orig)
361 elem->ip[1] = orig->ip[1];
365 hash_netportnet6_data_netmask(struct hash_netportnet6_elem *elem,
369 ip6_netmask(&elem->ip[1], cidr);
370 elem->cidr[1] = cidr;
372 ip6_netmask(&elem->ip[0], cidr);
373 elem->cidr[0] = cidr;
378 hash_netportnet6_data_list(struct sk_buff *skb,
379 const struct hash_netportnet6_elem *data)
381 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
383 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip[0].in6) ||
384 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip[1].in6) ||
385 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
386 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
387 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
388 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
390 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
391 goto nla_put_failure;
399 hash_netportnet6_data_next(struct hash_netportnet4_elem *next,
400 const struct hash_netportnet6_elem *d)
402 next->port = d->port;
409 #define MTYPE hash_netportnet6
411 #define HOST_MASK 128
412 #define IP_SET_EMIT_CREATE
413 #include "ip_set_hash_gen.h"
416 hash_netportnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
417 const struct xt_action_param *par,
418 enum ipset_adt adt, struct ip_set_adt_opt *opt)
420 const struct hash_netportnet *h = set->data;
421 ipset_adtfn adtfn = set->variant->adt[adt];
422 struct hash_netportnet6_elem e = { };
423 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
425 e.cidr[0] = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
426 e.cidr[1] = IP_SET_INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
427 if (adt == IPSET_TEST)
428 e.ccmp = (HOST_MASK << (sizeof(u8) * 8)) | HOST_MASK;
430 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
434 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0].in6);
435 ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip[1].in6);
436 ip6_netmask(&e.ip[0], e.cidr[0]);
437 ip6_netmask(&e.ip[1], e.cidr[1]);
439 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
443 hash_netportnet6_uadt(struct ip_set *set, struct nlattr *tb[],
444 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
446 const struct hash_netportnet *h = set->data;
447 ipset_adtfn adtfn = set->variant->adt[adt];
448 struct hash_netportnet6_elem e = { };
449 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
451 bool with_ports = false;
454 e.cidr[0] = e.cidr[1] = HOST_MASK;
455 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
456 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
457 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
458 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
459 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
460 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
461 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
462 return -IPSET_ERR_PROTOCOL;
463 if (unlikely(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_IP2_TO]))
464 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
466 if (tb[IPSET_ATTR_LINENO])
467 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
469 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip[0]) ||
470 ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip[1]) ||
471 ip_set_get_extensions(set, tb, &ext);
475 if (tb[IPSET_ATTR_CIDR])
476 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
478 if (tb[IPSET_ATTR_CIDR2])
479 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
481 if (unlikely(!e.cidr[0] || e.cidr[0] > HOST_MASK || !e.cidr[1] ||
482 e.cidr[1] > HOST_MASK))
483 return -IPSET_ERR_INVALID_CIDR;
485 ip6_netmask(&e.ip[0], e.cidr[0]);
486 ip6_netmask(&e.ip[1], e.cidr[1]);
488 if (tb[IPSET_ATTR_PORT])
489 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
491 return -IPSET_ERR_PROTOCOL;
493 if (tb[IPSET_ATTR_PROTO]) {
494 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
495 with_ports = ip_set_proto_with_ports(e.proto);
498 return -IPSET_ERR_INVALID_PROTO;
500 return -IPSET_ERR_MISSING_PROTO;
502 if (!(with_ports || e.proto == IPPROTO_ICMPV6))
505 if (tb[IPSET_ATTR_CADT_FLAGS]) {
506 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
507 if (cadt_flags & IPSET_FLAG_NOMATCH)
508 flags |= (IPSET_FLAG_NOMATCH << 16);
511 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
512 ret = adtfn(set, &e, &ext, &ext, flags);
513 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
514 ip_set_eexist(ret, flags) ? 0 : ret;
517 port = ntohs(e.port);
518 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
523 port = ntohs(h->next.port);
524 for (; port <= port_to; port++) {
525 e.port = htons(port);
526 ret = adtfn(set, &e, &ext, &ext, flags);
528 if (ret && !ip_set_eexist(ret, flags))
536 static struct ip_set_type hash_netportnet_type __read_mostly = {
537 .name = "hash:net,port,net",
538 .protocol = IPSET_PROTOCOL,
539 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2 |
541 .dimension = IPSET_DIM_THREE,
542 .family = NFPROTO_UNSPEC,
543 .revision_min = IPSET_TYPE_REV_MIN,
544 .revision_max = IPSET_TYPE_REV_MAX,
545 .create = hash_netportnet_create,
547 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
548 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
549 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
550 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
551 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
552 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
555 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
556 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
557 [IPSET_ATTR_IP2] = { .type = NLA_NESTED },
558 [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED },
559 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
560 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
561 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
562 [IPSET_ATTR_CIDR2] = { .type = NLA_U8 },
563 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
564 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
565 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
566 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
567 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
568 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
569 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
575 hash_netportnet_init(void)
577 return ip_set_type_register(&hash_netportnet_type);
581 hash_netportnet_fini(void)
583 ip_set_type_unregister(&hash_netportnet_type);
586 module_init(hash_netportnet_init);
587 module_exit(hash_netportnet_fini);