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 #define IPSET_TYPE_REV_MAX 5 /* Comments support added */
34 MODULE_LICENSE("GPL");
35 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
36 IP_SET_MODULE_DESC("hash:ip,port,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
37 MODULE_ALIAS("ip_set_hash:ip,port,net");
39 /* Type specific function prefix */
40 #define HTYPE hash_ipportnet
42 /* We squeeze the "nomatch" flag into cidr: we don't support cidr == 0
43 * However this way we have to store internally cidr - 1,
44 * dancing back and forth.
46 #define IP_SET_HASH_WITH_NETS_PACKED
47 #define IP_SET_HASH_WITH_PROTO
48 #define IP_SET_HASH_WITH_NETS
53 struct hash_ipportnet4_elem {
62 /* Common functions */
65 hash_ipportnet4_data_equal(const struct hash_ipportnet4_elem *ip1,
66 const struct hash_ipportnet4_elem *ip2,
69 return ip1->ip == ip2->ip &&
70 ip1->ip2 == ip2->ip2 &&
71 ip1->cidr == ip2->cidr &&
72 ip1->port == ip2->port &&
73 ip1->proto == ip2->proto;
77 hash_ipportnet4_do_data_match(const struct hash_ipportnet4_elem *elem)
79 return elem->nomatch ? -ENOTEMPTY : 1;
83 hash_ipportnet4_data_set_flags(struct hash_ipportnet4_elem *elem, u32 flags)
85 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
89 hash_ipportnet4_data_reset_flags(struct hash_ipportnet4_elem *elem, u8 *flags)
91 swap(*flags, elem->nomatch);
95 hash_ipportnet4_data_netmask(struct hash_ipportnet4_elem *elem, u8 cidr)
97 elem->ip2 &= ip_set_netmask(cidr);
98 elem->cidr = cidr - 1;
102 hash_ipportnet4_data_list(struct sk_buff *skb,
103 const struct hash_ipportnet4_elem *data)
105 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
107 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
108 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip2) ||
109 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
110 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr + 1) ||
111 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
113 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
114 goto nla_put_failure;
122 hash_ipportnet4_data_next(struct hash_ipportnet4_elem *next,
123 const struct hash_ipportnet4_elem *d)
126 next->port = d->port;
130 #define MTYPE hash_ipportnet4
133 #include "ip_set_hash_gen.h"
136 hash_ipportnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
137 const struct xt_action_param *par,
138 enum ipset_adt adt, struct ip_set_adt_opt *opt)
140 const struct hash_ipportnet *h = set->data;
141 ipset_adtfn adtfn = set->variant->adt[adt];
142 struct hash_ipportnet4_elem e = {
143 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK) - 1,
145 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
147 if (adt == IPSET_TEST)
148 e.cidr = HOST_MASK - 1;
150 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
154 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
155 ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip2);
156 e.ip2 &= ip_set_netmask(e.cidr + 1);
158 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
162 hash_ipportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
163 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
165 const struct hash_ipportnet *h = set->data;
166 ipset_adtfn adtfn = set->variant->adt[adt];
167 struct hash_ipportnet4_elem e = { .cidr = HOST_MASK - 1 };
168 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
169 u32 ip = 0, ip_to = 0, p = 0, port, port_to;
170 u32 ip2_from = 0, ip2_to = 0, ip2_last, ip2;
171 bool with_ports = false;
175 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
176 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
177 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
178 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
179 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
180 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
181 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
182 return -IPSET_ERR_PROTOCOL;
184 if (tb[IPSET_ATTR_LINENO])
185 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
187 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) ||
188 ip_set_get_extensions(set, tb, &ext);
192 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from);
196 if (tb[IPSET_ATTR_CIDR2]) {
197 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
198 if (!cidr || cidr > HOST_MASK)
199 return -IPSET_ERR_INVALID_CIDR;
203 if (tb[IPSET_ATTR_PORT])
204 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
206 return -IPSET_ERR_PROTOCOL;
208 if (tb[IPSET_ATTR_PROTO]) {
209 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
210 with_ports = ip_set_proto_with_ports(e.proto);
213 return -IPSET_ERR_INVALID_PROTO;
215 return -IPSET_ERR_MISSING_PROTO;
217 if (!(with_ports || e.proto == IPPROTO_ICMP))
220 if (tb[IPSET_ATTR_CADT_FLAGS]) {
221 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
222 if (cadt_flags & IPSET_FLAG_NOMATCH)
223 flags |= (IPSET_FLAG_NOMATCH << 16);
226 with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
227 if (adt == IPSET_TEST ||
228 !(tb[IPSET_ATTR_CIDR] || tb[IPSET_ATTR_IP_TO] || with_ports ||
229 tb[IPSET_ATTR_IP2_TO])) {
231 e.ip2 = htonl(ip2_from & ip_set_hostmask(e.cidr + 1));
232 ret = adtfn(set, &e, &ext, &ext, flags);
233 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
234 ip_set_eexist(ret, flags) ? 0 : ret;
238 if (tb[IPSET_ATTR_IP_TO]) {
239 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
244 } else if (tb[IPSET_ATTR_CIDR]) {
245 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
247 if (!cidr || cidr > 32)
248 return -IPSET_ERR_INVALID_CIDR;
249 ip_set_mask_from_to(ip, ip_to, cidr);
252 port_to = port = ntohs(e.port);
253 if (tb[IPSET_ATTR_PORT_TO]) {
254 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
260 if (tb[IPSET_ATTR_IP2_TO]) {
261 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to);
264 if (ip2_from > ip2_to)
265 swap(ip2_from, ip2_to);
266 if (ip2_from + UINT_MAX == ip2_to)
267 return -IPSET_ERR_HASH_RANGE;
269 ip_set_mask_from_to(ip2_from, ip2_to, e.cidr + 1);
272 ip = ntohl(h->next.ip);
273 for (; !before(ip_to, ip); ip++) {
275 p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port)
277 for (; p <= port_to; p++) {
280 ip == ntohl(h->next.ip) &&
281 p == ntohs(h->next.port)
282 ? ntohl(h->next.ip2) : ip2_from;
283 while (!after(ip2, ip2_to)) {
285 ip2_last = ip_set_range_to_cidr(ip2, ip2_to,
288 ret = adtfn(set, &e, &ext, &ext, flags);
290 if (ret && !ip_set_eexist(ret, flags))
303 struct hash_ipportnet6_elem {
304 union nf_inet_addr ip;
305 union nf_inet_addr ip2;
312 /* Common functions */
315 hash_ipportnet6_data_equal(const struct hash_ipportnet6_elem *ip1,
316 const struct hash_ipportnet6_elem *ip2,
319 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
320 ipv6_addr_equal(&ip1->ip2.in6, &ip2->ip2.in6) &&
321 ip1->cidr == ip2->cidr &&
322 ip1->port == ip2->port &&
323 ip1->proto == ip2->proto;
327 hash_ipportnet6_do_data_match(const struct hash_ipportnet6_elem *elem)
329 return elem->nomatch ? -ENOTEMPTY : 1;
333 hash_ipportnet6_data_set_flags(struct hash_ipportnet6_elem *elem, u32 flags)
335 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
339 hash_ipportnet6_data_reset_flags(struct hash_ipportnet6_elem *elem, u8 *flags)
341 swap(*flags, elem->nomatch);
345 hash_ipportnet6_data_netmask(struct hash_ipportnet6_elem *elem, u8 cidr)
347 ip6_netmask(&elem->ip2, cidr);
348 elem->cidr = cidr - 1;
352 hash_ipportnet6_data_list(struct sk_buff *skb,
353 const struct hash_ipportnet6_elem *data)
355 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
357 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
358 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip2.in6) ||
359 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
360 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr + 1) ||
361 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
363 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
364 goto nla_put_failure;
372 hash_ipportnet6_data_next(struct hash_ipportnet4_elem *next,
373 const struct hash_ipportnet6_elem *d)
375 next->port = d->port;
382 #define MTYPE hash_ipportnet6
384 #define HOST_MASK 128
385 #define IP_SET_EMIT_CREATE
386 #include "ip_set_hash_gen.h"
389 hash_ipportnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
390 const struct xt_action_param *par,
391 enum ipset_adt adt, struct ip_set_adt_opt *opt)
393 const struct hash_ipportnet *h = set->data;
394 ipset_adtfn adtfn = set->variant->adt[adt];
395 struct hash_ipportnet6_elem e = {
396 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK) - 1,
398 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
400 if (adt == IPSET_TEST)
401 e.cidr = HOST_MASK - 1;
403 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
407 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
408 ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip2.in6);
409 ip6_netmask(&e.ip2, e.cidr + 1);
411 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
415 hash_ipportnet6_uadt(struct ip_set *set, struct nlattr *tb[],
416 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
418 const struct hash_ipportnet *h = set->data;
419 ipset_adtfn adtfn = set->variant->adt[adt];
420 struct hash_ipportnet6_elem e = { .cidr = HOST_MASK - 1 };
421 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
423 bool with_ports = false;
427 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
428 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
429 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
430 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
431 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
432 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
433 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
434 tb[IPSET_ATTR_IP_TO] ||
435 tb[IPSET_ATTR_CIDR]))
436 return -IPSET_ERR_PROTOCOL;
437 if (unlikely(tb[IPSET_ATTR_IP_TO]))
438 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
440 if (tb[IPSET_ATTR_LINENO])
441 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
443 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
444 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 if (tb[IPSET_ATTR_PORT])
462 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
464 return -IPSET_ERR_PROTOCOL;
466 if (tb[IPSET_ATTR_PROTO]) {
467 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
468 with_ports = ip_set_proto_with_ports(e.proto);
471 return -IPSET_ERR_INVALID_PROTO;
473 return -IPSET_ERR_MISSING_PROTO;
475 if (!(with_ports || e.proto == IPPROTO_ICMPV6))
478 if (tb[IPSET_ATTR_CADT_FLAGS]) {
479 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
480 if (cadt_flags & IPSET_FLAG_NOMATCH)
481 flags |= (IPSET_FLAG_NOMATCH << 16);
484 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
485 ret = adtfn(set, &e, &ext, &ext, flags);
486 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
487 ip_set_eexist(ret, flags) ? 0 : ret;
490 port = ntohs(e.port);
491 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
496 port = ntohs(h->next.port);
497 for (; port <= port_to; port++) {
498 e.port = htons(port);
499 ret = adtfn(set, &e, &ext, &ext, flags);
501 if (ret && !ip_set_eexist(ret, flags))
509 static struct ip_set_type hash_ipportnet_type __read_mostly = {
510 .name = "hash:ip,port,net",
511 .protocol = IPSET_PROTOCOL,
512 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2 |
514 .dimension = IPSET_DIM_THREE,
515 .family = NFPROTO_UNSPEC,
516 .revision_min = IPSET_TYPE_REV_MIN,
517 .revision_max = IPSET_TYPE_REV_MAX,
518 .create = hash_ipportnet_create,
520 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
521 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
522 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
523 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
524 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
525 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
528 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
529 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
530 [IPSET_ATTR_IP2] = { .type = NLA_NESTED },
531 [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED },
532 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
533 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
534 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
535 [IPSET_ATTR_CIDR2] = { .type = NLA_U8 },
536 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
537 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
538 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
539 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
540 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
541 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
542 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
548 hash_ipportnet_init(void)
550 return ip_set_type_register(&hash_ipportnet_type);
554 hash_ipportnet_fini(void)
556 ip_set_type_unregister(&hash_ipportnet_type);
559 module_init(hash_ipportnet_init);
560 module_exit(hash_ipportnet_fini);