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;
216 if (!(with_ports || e.proto == IPPROTO_ICMP))
219 if (tb[IPSET_ATTR_CADT_FLAGS]) {
220 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 > HOST_MASK)
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);
273 ip = ntohl(h->next.ip);
274 for (; !before(ip_to, ip); ip++) {
276 p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port)
278 for (; p <= port_to; p++) {
281 ip == ntohl(h->next.ip) &&
282 p == ntohs(h->next.port)
283 ? ntohl(h->next.ip2) : ip2_from;
284 while (!after(ip2, ip2_to)) {
286 ip2_last = ip_set_range_to_cidr(ip2, ip2_to,
289 ret = adtfn(set, &e, &ext, &ext, flags);
291 if (ret && !ip_set_eexist(ret, flags))
304 struct hash_ipportnet6_elem {
305 union nf_inet_addr ip;
306 union nf_inet_addr ip2;
313 /* Common functions */
316 hash_ipportnet6_data_equal(const struct hash_ipportnet6_elem *ip1,
317 const struct hash_ipportnet6_elem *ip2,
320 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
321 ipv6_addr_equal(&ip1->ip2.in6, &ip2->ip2.in6) &&
322 ip1->cidr == ip2->cidr &&
323 ip1->port == ip2->port &&
324 ip1->proto == ip2->proto;
328 hash_ipportnet6_do_data_match(const struct hash_ipportnet6_elem *elem)
330 return elem->nomatch ? -ENOTEMPTY : 1;
334 hash_ipportnet6_data_set_flags(struct hash_ipportnet6_elem *elem, u32 flags)
336 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
340 hash_ipportnet6_data_reset_flags(struct hash_ipportnet6_elem *elem, u8 *flags)
342 swap(*flags, elem->nomatch);
346 hash_ipportnet6_data_netmask(struct hash_ipportnet6_elem *elem, u8 cidr)
348 ip6_netmask(&elem->ip2, cidr);
349 elem->cidr = cidr - 1;
353 hash_ipportnet6_data_list(struct sk_buff *skb,
354 const struct hash_ipportnet6_elem *data)
356 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
358 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
359 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip2.in6) ||
360 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
361 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr + 1) ||
362 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
364 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
365 goto nla_put_failure;
373 hash_ipportnet6_data_next(struct hash_ipportnet4_elem *next,
374 const struct hash_ipportnet6_elem *d)
376 next->port = d->port;
382 #define MTYPE hash_ipportnet6
383 #define HOST_MASK 128
384 #define IP_SET_EMIT_CREATE
385 #include "ip_set_hash_gen.h"
388 hash_ipportnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
389 const struct xt_action_param *par,
390 enum ipset_adt adt, struct ip_set_adt_opt *opt)
392 const struct hash_ipportnet *h = set->data;
393 ipset_adtfn adtfn = set->variant->adt[adt];
394 struct hash_ipportnet6_elem e = {
395 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
397 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
399 if (adt == IPSET_TEST)
400 e.cidr = HOST_MASK - 1;
402 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
406 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
407 ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip2.in6);
408 ip6_netmask(&e.ip2, e.cidr + 1);
410 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
414 hash_ipportnet6_uadt(struct ip_set *set, struct nlattr *tb[],
415 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
417 const struct hash_ipportnet *h = set->data;
418 ipset_adtfn adtfn = set->variant->adt[adt];
419 struct hash_ipportnet6_elem e = { .cidr = HOST_MASK - 1 };
420 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
422 bool with_ports = false;
426 if (tb[IPSET_ATTR_LINENO])
427 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
429 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
430 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
431 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
432 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
433 return -IPSET_ERR_PROTOCOL;
434 if (unlikely(tb[IPSET_ATTR_IP_TO]))
435 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
436 if (unlikely(tb[IPSET_ATTR_CIDR])) {
437 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
439 if (cidr != HOST_MASK)
440 return -IPSET_ERR_INVALID_CIDR;
443 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
447 ret = ip_set_get_extensions(set, tb, &ext);
451 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip2);
455 if (tb[IPSET_ATTR_CIDR2]) {
456 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
457 if (!cidr || cidr > HOST_MASK)
458 return -IPSET_ERR_INVALID_CIDR;
462 ip6_netmask(&e.ip2, e.cidr + 1);
464 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
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;
476 if (!(with_ports || e.proto == IPPROTO_ICMPV6))
479 if (tb[IPSET_ATTR_CADT_FLAGS]) {
480 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
482 if (cadt_flags & IPSET_FLAG_NOMATCH)
483 flags |= (IPSET_FLAG_NOMATCH << 16);
486 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
487 ret = adtfn(set, &e, &ext, &ext, flags);
488 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
489 ip_set_eexist(ret, flags) ? 0 : ret;
492 port = ntohs(e.port);
493 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
498 port = ntohs(h->next.port);
499 for (; port <= port_to; port++) {
500 e.port = htons(port);
501 ret = adtfn(set, &e, &ext, &ext, flags);
503 if (ret && !ip_set_eexist(ret, flags))
511 static struct ip_set_type hash_ipportnet_type __read_mostly = {
512 .name = "hash:ip,port,net",
513 .protocol = IPSET_PROTOCOL,
514 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2 |
516 .dimension = IPSET_DIM_THREE,
517 .family = NFPROTO_UNSPEC,
518 .revision_min = IPSET_TYPE_REV_MIN,
519 .revision_max = IPSET_TYPE_REV_MAX,
520 .create = hash_ipportnet_create,
522 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
523 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
524 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
525 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
526 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
527 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
530 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
531 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
532 [IPSET_ATTR_IP2] = { .type = NLA_NESTED },
533 [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED },
534 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
535 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
536 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
537 [IPSET_ATTR_CIDR2] = { .type = NLA_U8 },
538 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
539 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
540 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
541 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
542 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
543 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
544 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
545 .len = IPSET_MAX_COMMENT_SIZE },
546 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
547 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
548 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
554 hash_ipportnet_init(void)
556 return ip_set_type_register(&hash_ipportnet_type);
560 hash_ipportnet_fini(void)
563 ip_set_type_unregister(&hash_ipportnet_type);
566 module_init(hash_ipportnet_init);
567 module_exit(hash_ipportnet_fini);