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 /* 1 SCTP and UDPLITE support added */
29 /* 2 Range as input support for IPv4 added */
30 /* 3 nomatch flag support added */
31 /* 4 Counters support added */
32 /* 5 Comments support added */
33 /* 6 Forceadd support added */
34 #define IPSET_TYPE_REV_MAX 7 /* skbinfo support added */
36 MODULE_LICENSE("GPL");
37 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
38 IP_SET_MODULE_DESC("hash:ip,port,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
39 MODULE_ALIAS("ip_set_hash:ip,port,net");
41 /* Type specific function prefix */
42 #define HTYPE hash_ipportnet
44 /* We squeeze the "nomatch" flag into cidr: we don't support cidr == 0
45 * However this way we have to store internally cidr - 1,
46 * dancing back and forth.
48 #define IP_SET_HASH_WITH_NETS_PACKED
49 #define IP_SET_HASH_WITH_PROTO
50 #define IP_SET_HASH_WITH_NETS
55 struct hash_ipportnet4_elem {
64 /* Common functions */
67 hash_ipportnet4_data_equal(const struct hash_ipportnet4_elem *ip1,
68 const struct hash_ipportnet4_elem *ip2,
71 return ip1->ip == ip2->ip &&
72 ip1->ip2 == ip2->ip2 &&
73 ip1->cidr == ip2->cidr &&
74 ip1->port == ip2->port &&
75 ip1->proto == ip2->proto;
79 hash_ipportnet4_do_data_match(const struct hash_ipportnet4_elem *elem)
81 return elem->nomatch ? -ENOTEMPTY : 1;
85 hash_ipportnet4_data_set_flags(struct hash_ipportnet4_elem *elem, u32 flags)
87 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
91 hash_ipportnet4_data_reset_flags(struct hash_ipportnet4_elem *elem, u8 *flags)
93 swap(*flags, elem->nomatch);
97 hash_ipportnet4_data_netmask(struct hash_ipportnet4_elem *elem, u8 cidr)
99 elem->ip2 &= ip_set_netmask(cidr);
100 elem->cidr = cidr - 1;
104 hash_ipportnet4_data_list(struct sk_buff *skb,
105 const struct hash_ipportnet4_elem *data)
107 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
109 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
110 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip2) ||
111 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
112 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr + 1) ||
113 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
115 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
116 goto nla_put_failure;
124 hash_ipportnet4_data_next(struct hash_ipportnet4_elem *next,
125 const struct hash_ipportnet4_elem *d)
128 next->port = d->port;
132 #define MTYPE hash_ipportnet4
134 #include "ip_set_hash_gen.h"
137 hash_ipportnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
138 const struct xt_action_param *par,
139 enum ipset_adt adt, struct ip_set_adt_opt *opt)
141 const struct hash_ipportnet *h = set->data;
142 ipset_adtfn adtfn = set->variant->adt[adt];
143 struct hash_ipportnet4_elem e = {
144 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
146 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
148 if (adt == IPSET_TEST)
149 e.cidr = HOST_MASK - 1;
151 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
155 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
156 ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip2);
157 e.ip2 &= ip_set_netmask(e.cidr + 1);
159 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
163 hash_ipportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
164 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
166 const struct hash_ipportnet *h = set->data;
167 ipset_adtfn adtfn = set->variant->adt[adt];
168 struct hash_ipportnet4_elem e = { .cidr = HOST_MASK - 1 };
169 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
170 u32 ip = 0, ip_to = 0, p = 0, port, port_to;
171 u32 ip2_from = 0, ip2_to = 0, ip2_last, ip2;
172 bool with_ports = false;
176 if (tb[IPSET_ATTR_LINENO])
177 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
179 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
180 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
181 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
182 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
183 return -IPSET_ERR_PROTOCOL;
185 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
189 ret = ip_set_get_extensions(set, tb, &ext);
193 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from);
197 if (tb[IPSET_ATTR_CIDR2]) {
198 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
199 if (!cidr || cidr > HOST_MASK)
200 return -IPSET_ERR_INVALID_CIDR;
204 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
206 if (tb[IPSET_ATTR_PROTO]) {
207 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
208 with_ports = ip_set_proto_with_ports(e.proto);
211 return -IPSET_ERR_INVALID_PROTO;
213 return -IPSET_ERR_MISSING_PROTO;
215 if (!(with_ports || e.proto == IPPROTO_ICMP))
218 if (tb[IPSET_ATTR_CADT_FLAGS]) {
219 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
220 if (cadt_flags & IPSET_FLAG_NOMATCH)
221 flags |= (IPSET_FLAG_NOMATCH << 16);
224 with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
225 if (adt == IPSET_TEST ||
226 !(tb[IPSET_ATTR_CIDR] || tb[IPSET_ATTR_IP_TO] || with_ports ||
227 tb[IPSET_ATTR_IP2_TO])) {
229 e.ip2 = htonl(ip2_from & ip_set_hostmask(e.cidr + 1));
230 ret = adtfn(set, &e, &ext, &ext, flags);
231 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
232 ip_set_eexist(ret, flags) ? 0 : ret;
236 if (tb[IPSET_ATTR_IP_TO]) {
237 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
242 } else if (tb[IPSET_ATTR_CIDR]) {
243 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
245 if (!cidr || cidr > HOST_MASK)
246 return -IPSET_ERR_INVALID_CIDR;
247 ip_set_mask_from_to(ip, ip_to, cidr);
250 port_to = port = ntohs(e.port);
251 if (tb[IPSET_ATTR_PORT_TO]) {
252 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
258 if (tb[IPSET_ATTR_IP2_TO]) {
259 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to);
262 if (ip2_from > ip2_to)
263 swap(ip2_from, ip2_to);
264 if (ip2_from + UINT_MAX == ip2_to)
265 return -IPSET_ERR_HASH_RANGE;
267 ip_set_mask_from_to(ip2_from, ip2_to, e.cidr + 1);
270 ip = ntohl(h->next.ip);
271 for (; !before(ip_to, ip); ip++) {
273 p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port)
275 for (; p <= port_to; p++) {
278 ip == ntohl(h->next.ip) &&
279 p == ntohs(h->next.port)
280 ? ntohl(h->next.ip2) : ip2_from;
281 while (!after(ip2, ip2_to)) {
283 ip2_last = ip_set_range_to_cidr(ip2, ip2_to,
286 ret = adtfn(set, &e, &ext, &ext, flags);
288 if (ret && !ip_set_eexist(ret, flags))
301 struct hash_ipportnet6_elem {
302 union nf_inet_addr ip;
303 union nf_inet_addr ip2;
310 /* Common functions */
313 hash_ipportnet6_data_equal(const struct hash_ipportnet6_elem *ip1,
314 const struct hash_ipportnet6_elem *ip2,
317 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
318 ipv6_addr_equal(&ip1->ip2.in6, &ip2->ip2.in6) &&
319 ip1->cidr == ip2->cidr &&
320 ip1->port == ip2->port &&
321 ip1->proto == ip2->proto;
325 hash_ipportnet6_do_data_match(const struct hash_ipportnet6_elem *elem)
327 return elem->nomatch ? -ENOTEMPTY : 1;
331 hash_ipportnet6_data_set_flags(struct hash_ipportnet6_elem *elem, u32 flags)
333 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
337 hash_ipportnet6_data_reset_flags(struct hash_ipportnet6_elem *elem, u8 *flags)
339 swap(*flags, elem->nomatch);
343 hash_ipportnet6_data_netmask(struct hash_ipportnet6_elem *elem, u8 cidr)
345 ip6_netmask(&elem->ip2, cidr);
346 elem->cidr = cidr - 1;
350 hash_ipportnet6_data_list(struct sk_buff *skb,
351 const struct hash_ipportnet6_elem *data)
353 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
355 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
356 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip2.in6) ||
357 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
358 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr + 1) ||
359 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
361 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
362 goto nla_put_failure;
370 hash_ipportnet6_data_next(struct hash_ipportnet4_elem *next,
371 const struct hash_ipportnet6_elem *d)
373 next->port = d->port;
379 #define MTYPE hash_ipportnet6
380 #define HOST_MASK 128
381 #define IP_SET_EMIT_CREATE
382 #include "ip_set_hash_gen.h"
385 hash_ipportnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
386 const struct xt_action_param *par,
387 enum ipset_adt adt, struct ip_set_adt_opt *opt)
389 const struct hash_ipportnet *h = set->data;
390 ipset_adtfn adtfn = set->variant->adt[adt];
391 struct hash_ipportnet6_elem e = {
392 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
394 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
396 if (adt == IPSET_TEST)
397 e.cidr = HOST_MASK - 1;
399 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
403 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
404 ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip2.in6);
405 ip6_netmask(&e.ip2, e.cidr + 1);
407 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
411 hash_ipportnet6_uadt(struct ip_set *set, struct nlattr *tb[],
412 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
414 const struct hash_ipportnet *h = set->data;
415 ipset_adtfn adtfn = set->variant->adt[adt];
416 struct hash_ipportnet6_elem e = { .cidr = HOST_MASK - 1 };
417 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
419 bool with_ports = false;
423 if (tb[IPSET_ATTR_LINENO])
424 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
426 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
427 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
428 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
429 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
430 return -IPSET_ERR_PROTOCOL;
431 if (unlikely(tb[IPSET_ATTR_IP_TO]))
432 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
433 if (unlikely(tb[IPSET_ATTR_CIDR])) {
434 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
436 if (cidr != HOST_MASK)
437 return -IPSET_ERR_INVALID_CIDR;
440 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
444 ret = ip_set_get_extensions(set, tb, &ext);
448 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip2);
452 if (tb[IPSET_ATTR_CIDR2]) {
453 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
454 if (!cidr || cidr > HOST_MASK)
455 return -IPSET_ERR_INVALID_CIDR;
459 ip6_netmask(&e.ip2, e.cidr + 1);
461 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
463 if (tb[IPSET_ATTR_PROTO]) {
464 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
465 with_ports = ip_set_proto_with_ports(e.proto);
468 return -IPSET_ERR_INVALID_PROTO;
470 return -IPSET_ERR_MISSING_PROTO;
472 if (!(with_ports || e.proto == IPPROTO_ICMPV6))
475 if (tb[IPSET_ATTR_CADT_FLAGS]) {
476 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
477 if (cadt_flags & IPSET_FLAG_NOMATCH)
478 flags |= (IPSET_FLAG_NOMATCH << 16);
481 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
482 ret = adtfn(set, &e, &ext, &ext, flags);
483 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
484 ip_set_eexist(ret, flags) ? 0 : ret;
487 port = ntohs(e.port);
488 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
493 port = ntohs(h->next.port);
494 for (; port <= port_to; port++) {
495 e.port = htons(port);
496 ret = adtfn(set, &e, &ext, &ext, flags);
498 if (ret && !ip_set_eexist(ret, flags))
506 static struct ip_set_type hash_ipportnet_type __read_mostly = {
507 .name = "hash:ip,port,net",
508 .protocol = IPSET_PROTOCOL,
509 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2 |
511 .dimension = IPSET_DIM_THREE,
512 .family = NFPROTO_UNSPEC,
513 .revision_min = IPSET_TYPE_REV_MIN,
514 .revision_max = IPSET_TYPE_REV_MAX,
515 .create = hash_ipportnet_create,
517 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
518 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
519 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
520 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
521 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
522 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
525 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
526 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
527 [IPSET_ATTR_IP2] = { .type = NLA_NESTED },
528 [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED },
529 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
530 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
531 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
532 [IPSET_ATTR_CIDR2] = { .type = NLA_U8 },
533 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
534 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
535 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
536 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
537 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
538 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
539 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
540 .len = IPSET_MAX_COMMENT_SIZE },
541 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
542 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
543 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
549 hash_ipportnet_init(void)
551 return ip_set_type_register(&hash_ipportnet_type);
555 hash_ipportnet_fini(void)
557 ip_set_type_unregister(&hash_ipportnet_type);
560 module_init(hash_ipportnet_init);
561 module_exit(hash_ipportnet_fini);