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 #define IPSET_TYPE_REV_MAX 0 /* Comments support added */
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>");
32 IP_SET_MODULE_DESC("hash:net,port,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
33 MODULE_ALIAS("ip_set_hash:net,port,net");
35 /* Type specific function prefix */
36 #define HTYPE hash_netportnet
37 #define IP_SET_HASH_WITH_PROTO
38 #define IP_SET_HASH_WITH_NETS
39 #define IPSET_NET_COUNT 2
44 struct hash_netportnet4_elem {
58 /* Common functions */
61 hash_netportnet4_data_equal(const struct hash_netportnet4_elem *ip1,
62 const struct hash_netportnet4_elem *ip2,
65 return ip1->ipcmp == ip2->ipcmp &&
66 ip1->ccmp == ip2->ccmp &&
67 ip1->port == ip2->port &&
68 ip1->proto == ip2->proto;
72 hash_netportnet4_do_data_match(const struct hash_netportnet4_elem *elem)
74 return elem->nomatch ? -ENOTEMPTY : 1;
78 hash_netportnet4_data_set_flags(struct hash_netportnet4_elem *elem, u32 flags)
80 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
84 hash_netportnet4_data_reset_flags(struct hash_netportnet4_elem *elem, u8 *flags)
86 swap(*flags, elem->nomatch);
90 hash_netportnet4_data_reset_elem(struct hash_netportnet4_elem *elem,
91 struct hash_netportnet4_elem *orig)
93 elem->ip[1] = orig->ip[1];
97 hash_netportnet4_data_netmask(struct hash_netportnet4_elem *elem,
101 elem->ip[1] &= ip_set_netmask(cidr);
102 elem->cidr[1] = cidr;
104 elem->ip[0] &= ip_set_netmask(cidr);
105 elem->cidr[0] = cidr;
110 hash_netportnet4_data_list(struct sk_buff *skb,
111 const struct hash_netportnet4_elem *data)
113 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
115 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip[0]) ||
116 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip[1]) ||
117 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
118 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
119 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
120 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
122 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
123 goto nla_put_failure;
131 hash_netportnet4_data_next(struct hash_netportnet4_elem *next,
132 const struct hash_netportnet4_elem *d)
134 next->ipcmp = d->ipcmp;
135 next->port = d->port;
138 #define MTYPE hash_netportnet4
141 #include "ip_set_hash_gen.h"
144 hash_netportnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
145 const struct xt_action_param *par,
146 enum ipset_adt adt, struct ip_set_adt_opt *opt)
148 const struct hash_netportnet *h = set->data;
149 ipset_adtfn adtfn = set->variant->adt[adt];
150 struct hash_netportnet4_elem e = { };
151 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
153 e.cidr[0] = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
154 e.cidr[1] = IP_SET_INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
155 if (adt == IPSET_TEST)
156 e.ccmp = (HOST_MASK << (sizeof(e.cidr[0]) * 8)) | HOST_MASK;
158 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
162 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0]);
163 ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip[1]);
164 e.ip[0] &= ip_set_netmask(e.cidr[0]);
165 e.ip[1] &= ip_set_netmask(e.cidr[1]);
167 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
171 hash_netportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
172 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
174 const struct hash_netportnet *h = set->data;
175 ipset_adtfn adtfn = set->variant->adt[adt];
176 struct hash_netportnet4_elem e = { };
177 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
178 u32 ip = 0, ip_to = 0, ip_last, p = 0, port, port_to;
179 u32 ip2_from = 0, ip2_to = 0, ip2_last, ip2;
180 bool with_ports = false;
184 e.cidr[0] = e.cidr[1] = HOST_MASK;
185 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
186 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
187 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
188 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
189 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
190 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
191 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
192 return -IPSET_ERR_PROTOCOL;
194 if (tb[IPSET_ATTR_LINENO])
195 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
197 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) ||
198 ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from) ||
199 ip_set_get_extensions(set, tb, &ext);
203 if (tb[IPSET_ATTR_CIDR]) {
204 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
205 if (!cidr || cidr > HOST_MASK)
206 return -IPSET_ERR_INVALID_CIDR;
210 if (tb[IPSET_ATTR_CIDR2]) {
211 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
212 if (!cidr || cidr > HOST_MASK)
213 return -IPSET_ERR_INVALID_CIDR;
217 if (tb[IPSET_ATTR_PORT])
218 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
220 return -IPSET_ERR_PROTOCOL;
222 if (tb[IPSET_ATTR_PROTO]) {
223 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
224 with_ports = ip_set_proto_with_ports(e.proto);
227 return -IPSET_ERR_INVALID_PROTO;
229 return -IPSET_ERR_MISSING_PROTO;
231 if (!(with_ports || e.proto == IPPROTO_ICMP))
234 if (tb[IPSET_ATTR_CADT_FLAGS]) {
235 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
236 if (cadt_flags & IPSET_FLAG_NOMATCH)
237 flags |= (IPSET_FLAG_NOMATCH << 16);
240 with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
241 if (adt == IPSET_TEST ||
242 !(tb[IPSET_ATTR_IP_TO] || with_ports || tb[IPSET_ATTR_IP2_TO])) {
243 e.ip[0] = htonl(ip & ip_set_hostmask(e.cidr[0]));
244 e.ip[1] = htonl(ip2_from & ip_set_hostmask(e.cidr[1]));
245 ret = adtfn(set, &e, &ext, &ext, flags);
246 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
247 ip_set_eexist(ret, flags) ? 0 : ret;
251 if (tb[IPSET_ATTR_IP_TO]) {
252 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
257 if (unlikely(ip + UINT_MAX == ip_to))
258 return -IPSET_ERR_HASH_RANGE;
261 port_to = port = ntohs(e.port);
262 if (tb[IPSET_ATTR_PORT_TO]) {
263 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
269 if (tb[IPSET_ATTR_IP2_TO]) {
270 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to);
273 if (ip2_from > ip2_to)
274 swap(ip2_from, ip2_to);
275 if (unlikely(ip2_from + UINT_MAX == ip2_to))
276 return -IPSET_ERR_HASH_RANGE;
280 ip = ntohl(h->next.ip[0]);
282 while (!after(ip, ip_to)) {
284 ip_last = ip_set_range_to_cidr(ip, ip_to, &cidr);
286 p = retried && ip == ntohl(h->next.ip[0]) ? ntohs(h->next.port)
288 for (; p <= port_to; p++) {
290 ip2 = (retried && ip == ntohl(h->next.ip[0]) &&
291 p == ntohs(h->next.port)) ? ntohl(h->next.ip[1])
293 while (!after(ip2, ip2_to)) {
294 e.ip[1] = htonl(ip2);
295 ip2_last = ip_set_range_to_cidr(ip2, ip2_to,
298 ret = adtfn(set, &e, &ext, &ext, flags);
299 if (ret && !ip_set_eexist(ret, flags))
313 struct hash_netportnet6_elem {
314 union nf_inet_addr ip[2];
324 /* Common functions */
327 hash_netportnet6_data_equal(const struct hash_netportnet6_elem *ip1,
328 const struct hash_netportnet6_elem *ip2,
331 return ipv6_addr_equal(&ip1->ip[0].in6, &ip2->ip[0].in6) &&
332 ipv6_addr_equal(&ip1->ip[1].in6, &ip2->ip[1].in6) &&
333 ip1->ccmp == ip2->ccmp &&
334 ip1->port == ip2->port &&
335 ip1->proto == ip2->proto;
339 hash_netportnet6_do_data_match(const struct hash_netportnet6_elem *elem)
341 return elem->nomatch ? -ENOTEMPTY : 1;
345 hash_netportnet6_data_set_flags(struct hash_netportnet6_elem *elem, u32 flags)
347 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
351 hash_netportnet6_data_reset_flags(struct hash_netportnet6_elem *elem, u8 *flags)
353 swap(*flags, elem->nomatch);
357 hash_netportnet6_data_reset_elem(struct hash_netportnet6_elem *elem,
358 struct hash_netportnet6_elem *orig)
360 elem->ip[1] = orig->ip[1];
364 hash_netportnet6_data_netmask(struct hash_netportnet6_elem *elem,
368 ip6_netmask(&elem->ip[1], cidr);
369 elem->cidr[1] = cidr;
371 ip6_netmask(&elem->ip[0], cidr);
372 elem->cidr[0] = cidr;
377 hash_netportnet6_data_list(struct sk_buff *skb,
378 const struct hash_netportnet6_elem *data)
380 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
382 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip[0].in6) ||
383 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip[1].in6) ||
384 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
385 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
386 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
387 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
389 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
390 goto nla_put_failure;
398 hash_netportnet6_data_next(struct hash_netportnet4_elem *next,
399 const struct hash_netportnet6_elem *d)
401 next->port = d->port;
408 #define MTYPE hash_netportnet6
410 #define HOST_MASK 128
411 #define IP_SET_EMIT_CREATE
412 #include "ip_set_hash_gen.h"
415 hash_netportnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
416 const struct xt_action_param *par,
417 enum ipset_adt adt, struct ip_set_adt_opt *opt)
419 const struct hash_netportnet *h = set->data;
420 ipset_adtfn adtfn = set->variant->adt[adt];
421 struct hash_netportnet6_elem e = { };
422 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
424 e.cidr[0] = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
425 e.cidr[1] = IP_SET_INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
426 if (adt == IPSET_TEST)
427 e.ccmp = (HOST_MASK << (sizeof(u8) * 8)) | HOST_MASK;
429 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
433 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0].in6);
434 ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip[1].in6);
435 ip6_netmask(&e.ip[0], e.cidr[0]);
436 ip6_netmask(&e.ip[1], e.cidr[1]);
438 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
442 hash_netportnet6_uadt(struct ip_set *set, struct nlattr *tb[],
443 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
445 const struct hash_netportnet *h = set->data;
446 ipset_adtfn adtfn = set->variant->adt[adt];
447 struct hash_netportnet6_elem e = { };
448 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
450 bool with_ports = false;
453 e.cidr[0] = e.cidr[1] = HOST_MASK;
454 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
455 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
456 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
457 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
458 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
459 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
460 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
461 return -IPSET_ERR_PROTOCOL;
462 if (unlikely(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_IP2_TO]))
463 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
465 if (tb[IPSET_ATTR_LINENO])
466 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
468 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip[0]) ||
469 ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip[1]) ||
470 ip_set_get_extensions(set, tb, &ext);
474 if (tb[IPSET_ATTR_CIDR])
475 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
477 if (tb[IPSET_ATTR_CIDR2])
478 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
480 if (unlikely(!e.cidr[0] || e.cidr[0] > HOST_MASK || !e.cidr[1] ||
481 e.cidr[1] > HOST_MASK))
482 return -IPSET_ERR_INVALID_CIDR;
484 ip6_netmask(&e.ip[0], e.cidr[0]);
485 ip6_netmask(&e.ip[1], e.cidr[1]);
487 if (tb[IPSET_ATTR_PORT])
488 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
490 return -IPSET_ERR_PROTOCOL;
492 if (tb[IPSET_ATTR_PROTO]) {
493 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
494 with_ports = ip_set_proto_with_ports(e.proto);
497 return -IPSET_ERR_INVALID_PROTO;
499 return -IPSET_ERR_MISSING_PROTO;
501 if (!(with_ports || e.proto == IPPROTO_ICMPV6))
504 if (tb[IPSET_ATTR_CADT_FLAGS]) {
505 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
506 if (cadt_flags & IPSET_FLAG_NOMATCH)
507 flags |= (IPSET_FLAG_NOMATCH << 16);
510 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
511 ret = adtfn(set, &e, &ext, &ext, flags);
512 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
513 ip_set_eexist(ret, flags) ? 0 : ret;
516 port = ntohs(e.port);
517 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
522 port = ntohs(h->next.port);
523 for (; port <= port_to; port++) {
524 e.port = htons(port);
525 ret = adtfn(set, &e, &ext, &ext, flags);
527 if (ret && !ip_set_eexist(ret, flags))
535 static struct ip_set_type hash_netportnet_type __read_mostly = {
536 .name = "hash:net,port,net",
537 .protocol = IPSET_PROTOCOL,
538 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2 |
540 .dimension = IPSET_DIM_THREE,
541 .family = NFPROTO_UNSPEC,
542 .revision_min = IPSET_TYPE_REV_MIN,
543 .revision_max = IPSET_TYPE_REV_MAX,
544 .create = hash_netportnet_create,
546 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
547 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
548 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
549 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
550 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
551 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
554 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
555 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
556 [IPSET_ATTR_IP2] = { .type = NLA_NESTED },
557 [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED },
558 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
559 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
560 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
561 [IPSET_ATTR_CIDR2] = { .type = NLA_U8 },
562 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
563 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
564 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
565 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
566 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
567 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
568 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
574 hash_netportnet_init(void)
576 return ip_set_type_register(&hash_netportnet_type);
580 hash_netportnet_fini(void)
582 ip_set_type_unregister(&hash_netportnet_type);
585 module_init(hash_netportnet_init);
586 module_exit(hash_netportnet_fini);