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 /* 1 Forceadd support added */
30 #define IPSET_TYPE_REV_MAX 2 /* skbinfo support added */
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>");
34 IP_SET_MODULE_DESC("hash:net,port,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
35 MODULE_ALIAS("ip_set_hash:net,port,net");
37 /* Type specific function prefix */
38 #define HTYPE hash_netportnet
39 #define IP_SET_HASH_WITH_PROTO
40 #define IP_SET_HASH_WITH_NETS
41 #define IPSET_NET_COUNT 2
46 struct hash_netportnet4_elem {
60 /* Common functions */
63 hash_netportnet4_data_equal(const struct hash_netportnet4_elem *ip1,
64 const struct hash_netportnet4_elem *ip2,
67 return ip1->ipcmp == ip2->ipcmp &&
68 ip1->ccmp == ip2->ccmp &&
69 ip1->port == ip2->port &&
70 ip1->proto == ip2->proto;
74 hash_netportnet4_do_data_match(const struct hash_netportnet4_elem *elem)
76 return elem->nomatch ? -ENOTEMPTY : 1;
80 hash_netportnet4_data_set_flags(struct hash_netportnet4_elem *elem, u32 flags)
82 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
86 hash_netportnet4_data_reset_flags(struct hash_netportnet4_elem *elem, u8 *flags)
88 swap(*flags, elem->nomatch);
92 hash_netportnet4_data_reset_elem(struct hash_netportnet4_elem *elem,
93 struct hash_netportnet4_elem *orig)
95 elem->ip[1] = orig->ip[1];
99 hash_netportnet4_data_netmask(struct hash_netportnet4_elem *elem,
103 elem->ip[1] &= ip_set_netmask(cidr);
104 elem->cidr[1] = cidr;
106 elem->ip[0] &= ip_set_netmask(cidr);
107 elem->cidr[0] = cidr;
112 hash_netportnet4_data_list(struct sk_buff *skb,
113 const struct hash_netportnet4_elem *data)
115 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
117 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip[0]) ||
118 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip[1]) ||
119 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
120 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
121 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
122 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
124 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
125 goto nla_put_failure;
133 hash_netportnet4_data_next(struct hash_netportnet4_elem *next,
134 const struct hash_netportnet4_elem *d)
136 next->ipcmp = d->ipcmp;
137 next->port = d->port;
140 #define MTYPE hash_netportnet4
143 #include "ip_set_hash_gen.h"
146 hash_netportnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
147 const struct xt_action_param *par,
148 enum ipset_adt adt, struct ip_set_adt_opt *opt)
150 const struct hash_netportnet *h = set->data;
151 ipset_adtfn adtfn = set->variant->adt[adt];
152 struct hash_netportnet4_elem e = { };
153 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
155 e.cidr[0] = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
156 e.cidr[1] = IP_SET_INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
157 if (adt == IPSET_TEST)
158 e.ccmp = (HOST_MASK << (sizeof(e.cidr[0]) * 8)) | HOST_MASK;
160 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
164 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0]);
165 ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip[1]);
166 e.ip[0] &= ip_set_netmask(e.cidr[0]);
167 e.ip[1] &= ip_set_netmask(e.cidr[1]);
169 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
173 hash_netportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
174 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
176 const struct hash_netportnet *h = set->data;
177 ipset_adtfn adtfn = set->variant->adt[adt];
178 struct hash_netportnet4_elem e = { };
179 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
180 u32 ip = 0, ip_to = 0, ip_last, p = 0, port, port_to;
181 u32 ip2_from = 0, ip2_to = 0, ip2_last, ip2;
182 bool with_ports = false;
186 e.cidr[0] = e.cidr[1] = HOST_MASK;
187 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
188 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
189 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
190 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
191 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
192 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
193 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
194 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
195 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
196 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
197 return -IPSET_ERR_PROTOCOL;
199 if (tb[IPSET_ATTR_LINENO])
200 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
202 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) ||
203 ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from) ||
204 ip_set_get_extensions(set, tb, &ext);
208 if (tb[IPSET_ATTR_CIDR]) {
209 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
210 if (!cidr || cidr > HOST_MASK)
211 return -IPSET_ERR_INVALID_CIDR;
215 if (tb[IPSET_ATTR_CIDR2]) {
216 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
217 if (!cidr || cidr > HOST_MASK)
218 return -IPSET_ERR_INVALID_CIDR;
222 if (tb[IPSET_ATTR_PORT])
223 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
225 return -IPSET_ERR_PROTOCOL;
227 if (tb[IPSET_ATTR_PROTO]) {
228 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
229 with_ports = ip_set_proto_with_ports(e.proto);
232 return -IPSET_ERR_INVALID_PROTO;
234 return -IPSET_ERR_MISSING_PROTO;
236 if (!(with_ports || e.proto == IPPROTO_ICMP))
239 if (tb[IPSET_ATTR_CADT_FLAGS]) {
240 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
241 if (cadt_flags & IPSET_FLAG_NOMATCH)
242 flags |= (IPSET_FLAG_NOMATCH << 16);
245 with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
246 if (adt == IPSET_TEST ||
247 !(tb[IPSET_ATTR_IP_TO] || with_ports || tb[IPSET_ATTR_IP2_TO])) {
248 e.ip[0] = htonl(ip & ip_set_hostmask(e.cidr[0]));
249 e.ip[1] = htonl(ip2_from & ip_set_hostmask(e.cidr[1]));
250 ret = adtfn(set, &e, &ext, &ext, flags);
251 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
252 ip_set_eexist(ret, flags) ? 0 : ret;
256 if (tb[IPSET_ATTR_IP_TO]) {
257 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
262 if (unlikely(ip + UINT_MAX == ip_to))
263 return -IPSET_ERR_HASH_RANGE;
265 ip_set_mask_from_to(ip, ip_to, e.cidr[0]);
267 port_to = port = ntohs(e.port);
268 if (tb[IPSET_ATTR_PORT_TO]) {
269 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
275 if (tb[IPSET_ATTR_IP2_TO]) {
276 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to);
279 if (ip2_from > ip2_to)
280 swap(ip2_from, ip2_to);
281 if (unlikely(ip2_from + UINT_MAX == ip2_to))
282 return -IPSET_ERR_HASH_RANGE;
284 ip_set_mask_from_to(ip2_from, ip2_to, e.cidr[1]);
287 ip = ntohl(h->next.ip[0]);
289 while (!after(ip, ip_to)) {
291 ip_last = ip_set_range_to_cidr(ip, ip_to, &cidr);
293 p = retried && ip == ntohl(h->next.ip[0]) ? ntohs(h->next.port)
295 for (; p <= port_to; p++) {
297 ip2 = (retried && ip == ntohl(h->next.ip[0]) &&
298 p == ntohs(h->next.port)) ? ntohl(h->next.ip[1])
300 while (!after(ip2, ip2_to)) {
301 e.ip[1] = htonl(ip2);
302 ip2_last = ip_set_range_to_cidr(ip2, ip2_to,
305 ret = adtfn(set, &e, &ext, &ext, flags);
306 if (ret && !ip_set_eexist(ret, flags))
320 struct hash_netportnet6_elem {
321 union nf_inet_addr ip[2];
331 /* Common functions */
334 hash_netportnet6_data_equal(const struct hash_netportnet6_elem *ip1,
335 const struct hash_netportnet6_elem *ip2,
338 return ipv6_addr_equal(&ip1->ip[0].in6, &ip2->ip[0].in6) &&
339 ipv6_addr_equal(&ip1->ip[1].in6, &ip2->ip[1].in6) &&
340 ip1->ccmp == ip2->ccmp &&
341 ip1->port == ip2->port &&
342 ip1->proto == ip2->proto;
346 hash_netportnet6_do_data_match(const struct hash_netportnet6_elem *elem)
348 return elem->nomatch ? -ENOTEMPTY : 1;
352 hash_netportnet6_data_set_flags(struct hash_netportnet6_elem *elem, u32 flags)
354 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
358 hash_netportnet6_data_reset_flags(struct hash_netportnet6_elem *elem, u8 *flags)
360 swap(*flags, elem->nomatch);
364 hash_netportnet6_data_reset_elem(struct hash_netportnet6_elem *elem,
365 struct hash_netportnet6_elem *orig)
367 elem->ip[1] = orig->ip[1];
371 hash_netportnet6_data_netmask(struct hash_netportnet6_elem *elem,
375 ip6_netmask(&elem->ip[1], cidr);
376 elem->cidr[1] = cidr;
378 ip6_netmask(&elem->ip[0], cidr);
379 elem->cidr[0] = cidr;
384 hash_netportnet6_data_list(struct sk_buff *skb,
385 const struct hash_netportnet6_elem *data)
387 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
389 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip[0].in6) ||
390 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip[1].in6) ||
391 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
392 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
393 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
394 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
396 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
397 goto nla_put_failure;
405 hash_netportnet6_data_next(struct hash_netportnet4_elem *next,
406 const struct hash_netportnet6_elem *d)
408 next->port = d->port;
415 #define MTYPE hash_netportnet6
417 #define HOST_MASK 128
418 #define IP_SET_EMIT_CREATE
419 #include "ip_set_hash_gen.h"
422 hash_netportnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
423 const struct xt_action_param *par,
424 enum ipset_adt adt, struct ip_set_adt_opt *opt)
426 const struct hash_netportnet *h = set->data;
427 ipset_adtfn adtfn = set->variant->adt[adt];
428 struct hash_netportnet6_elem e = { };
429 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
431 e.cidr[0] = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
432 e.cidr[1] = IP_SET_INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
433 if (adt == IPSET_TEST)
434 e.ccmp = (HOST_MASK << (sizeof(u8) * 8)) | HOST_MASK;
436 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
440 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0].in6);
441 ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip[1].in6);
442 ip6_netmask(&e.ip[0], e.cidr[0]);
443 ip6_netmask(&e.ip[1], e.cidr[1]);
445 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
449 hash_netportnet6_uadt(struct ip_set *set, struct nlattr *tb[],
450 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
452 const struct hash_netportnet *h = set->data;
453 ipset_adtfn adtfn = set->variant->adt[adt];
454 struct hash_netportnet6_elem e = { };
455 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
457 bool with_ports = false;
460 e.cidr[0] = e.cidr[1] = HOST_MASK;
461 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
462 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
463 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
464 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
465 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
466 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
467 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
468 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
469 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
470 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
471 return -IPSET_ERR_PROTOCOL;
472 if (unlikely(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_IP2_TO]))
473 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
475 if (tb[IPSET_ATTR_LINENO])
476 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
478 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip[0]) ||
479 ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip[1]) ||
480 ip_set_get_extensions(set, tb, &ext);
484 if (tb[IPSET_ATTR_CIDR])
485 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
487 if (tb[IPSET_ATTR_CIDR2])
488 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
490 if (unlikely(!e.cidr[0] || e.cidr[0] > HOST_MASK || !e.cidr[1] ||
491 e.cidr[1] > HOST_MASK))
492 return -IPSET_ERR_INVALID_CIDR;
494 ip6_netmask(&e.ip[0], e.cidr[0]);
495 ip6_netmask(&e.ip[1], e.cidr[1]);
497 if (tb[IPSET_ATTR_PORT])
498 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
500 return -IPSET_ERR_PROTOCOL;
502 if (tb[IPSET_ATTR_PROTO]) {
503 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
504 with_ports = ip_set_proto_with_ports(e.proto);
507 return -IPSET_ERR_INVALID_PROTO;
509 return -IPSET_ERR_MISSING_PROTO;
511 if (!(with_ports || e.proto == IPPROTO_ICMPV6))
514 if (tb[IPSET_ATTR_CADT_FLAGS]) {
515 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
516 if (cadt_flags & IPSET_FLAG_NOMATCH)
517 flags |= (IPSET_FLAG_NOMATCH << 16);
520 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
521 ret = adtfn(set, &e, &ext, &ext, flags);
522 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
523 ip_set_eexist(ret, flags) ? 0 : ret;
526 port = ntohs(e.port);
527 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
532 port = ntohs(h->next.port);
533 for (; port <= port_to; port++) {
534 e.port = htons(port);
535 ret = adtfn(set, &e, &ext, &ext, flags);
537 if (ret && !ip_set_eexist(ret, flags))
545 static struct ip_set_type hash_netportnet_type __read_mostly = {
546 .name = "hash:net,port,net",
547 .protocol = IPSET_PROTOCOL,
548 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2 |
550 .dimension = IPSET_DIM_THREE,
551 .family = NFPROTO_UNSPEC,
552 .revision_min = IPSET_TYPE_REV_MIN,
553 .revision_max = IPSET_TYPE_REV_MAX,
554 .create = hash_netportnet_create,
556 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
557 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
558 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
559 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
560 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
561 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
564 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
565 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
566 [IPSET_ATTR_IP2] = { .type = NLA_NESTED },
567 [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED },
568 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
569 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
570 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
571 [IPSET_ATTR_CIDR2] = { .type = NLA_U8 },
572 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
573 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
574 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
575 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
576 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
577 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
578 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
579 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
580 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
581 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
587 hash_netportnet_init(void)
589 return ip_set_type_register(&hash_netportnet_type);
593 hash_netportnet_fini(void)
595 ip_set_type_unregister(&hash_netportnet_type);
598 module_init(hash_netportnet_init);
599 module_exit(hash_netportnet_fini);