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:net,port 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>
20 #include <linux/netfilter.h>
21 #include <linux/netfilter/ipset/pfxlen.h>
22 #include <linux/netfilter/ipset/ip_set.h>
23 #include <linux/netfilter/ipset/ip_set_getport.h>
24 #include <linux/netfilter/ipset/ip_set_hash.h>
26 #define REVISION_MIN 0
27 /* 1 SCTP and UDPLITE support added */
28 /* 2 Range as input support for IPv4 added */
29 /* 3 nomatch flag support added */
30 #define REVISION_MAX 4 /* Counters support added */
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
34 IP_SET_MODULE_DESC("hash:net,port", REVISION_MIN, REVISION_MAX);
35 MODULE_ALIAS("ip_set_hash:net,port");
37 /* Type specific function prefix */
38 #define HTYPE hash_netport
39 #define IP_SET_HASH_WITH_PROTO
40 #define IP_SET_HASH_WITH_NETS
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
51 struct hash_netport4_elem {
59 struct hash_netport4t_elem {
65 unsigned long timeout;
68 struct hash_netport4c_elem {
74 struct ip_set_counter counter;
77 struct hash_netport4ct_elem {
83 struct ip_set_counter counter;
84 unsigned long timeout;
87 /* Common functions */
90 hash_netport4_data_equal(const struct hash_netport4_elem *ip1,
91 const struct hash_netport4_elem *ip2,
94 return ip1->ip == ip2->ip &&
95 ip1->port == ip2->port &&
96 ip1->proto == ip2->proto &&
97 ip1->cidr == ip2->cidr;
101 hash_netport4_do_data_match(const struct hash_netport4_elem *elem)
103 return elem->nomatch ? -ENOTEMPTY : 1;
107 hash_netport4_data_set_flags(struct hash_netport4_elem *elem, u32 flags)
109 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
113 hash_netport4_data_reset_flags(struct hash_netport4_elem *elem, u8 *flags)
115 swap(*flags, elem->nomatch);
119 hash_netport4_data_netmask(struct hash_netport4_elem *elem, u8 cidr)
121 elem->ip &= ip_set_netmask(cidr);
122 elem->cidr = cidr - 1;
126 hash_netport4_data_list(struct sk_buff *skb,
127 const struct hash_netport4_elem *data)
129 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
131 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
132 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
133 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr + 1) ||
134 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
136 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
137 goto nla_put_failure;
145 hash_netport4_data_next(struct hash_netport4_elem *next,
146 const struct hash_netport4_elem *d)
149 next->port = d->port;
152 #define MTYPE hash_netport4
155 #include "ip_set_hash_gen.h"
158 hash_netport4_kadt(struct ip_set *set, const struct sk_buff *skb,
159 const struct xt_action_param *par,
160 enum ipset_adt adt, struct ip_set_adt_opt *opt)
162 const struct hash_netport *h = set->data;
163 ipset_adtfn adtfn = set->variant->adt[adt];
164 struct hash_netport4_elem e = {
165 .cidr = h->nets[0].cidr ? h->nets[0].cidr - 1 : HOST_MASK - 1
167 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h);
169 if (adt == IPSET_TEST)
170 e.cidr = HOST_MASK - 1;
172 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
176 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
177 e.ip &= ip_set_netmask(e.cidr + 1);
179 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
183 hash_netport4_uadt(struct ip_set *set, struct nlattr *tb[],
184 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
186 const struct hash_netport *h = set->data;
187 ipset_adtfn adtfn = set->variant->adt[adt];
188 struct hash_netport4_elem e = { .cidr = HOST_MASK - 1 };
189 struct ip_set_ext ext = IP_SET_INIT_UEXT(h);
190 u32 port, port_to, p = 0, ip = 0, ip_to, last;
191 bool with_ports = false;
195 if (unlikely(!tb[IPSET_ATTR_IP] ||
196 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
197 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
198 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
199 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
200 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
201 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
202 return -IPSET_ERR_PROTOCOL;
204 if (tb[IPSET_ATTR_LINENO])
205 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
207 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) ||
208 ip_set_get_extensions(set, tb, &ext);
212 if (tb[IPSET_ATTR_CIDR]) {
213 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
214 if (!cidr || cidr > HOST_MASK)
215 return -IPSET_ERR_INVALID_CIDR;
219 if (tb[IPSET_ATTR_PORT])
220 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
222 return -IPSET_ERR_PROTOCOL;
224 if (tb[IPSET_ATTR_PROTO]) {
225 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
226 with_ports = ip_set_proto_with_ports(e.proto);
229 return -IPSET_ERR_INVALID_PROTO;
231 return -IPSET_ERR_MISSING_PROTO;
233 if (!(with_ports || e.proto == IPPROTO_ICMP))
236 with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
238 if (tb[IPSET_ATTR_CADT_FLAGS]) {
239 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
240 if (cadt_flags & IPSET_FLAG_NOMATCH)
241 flags |= (IPSET_FLAG_NOMATCH << 16);
244 if (adt == IPSET_TEST || !(with_ports || tb[IPSET_ATTR_IP_TO])) {
245 e.ip = htonl(ip & ip_set_hostmask(e.cidr + 1));
246 ret = adtfn(set, &e, &ext, &ext, flags);
247 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
248 ip_set_eexist(ret, flags) ? 0 : ret;
251 port = port_to = ntohs(e.port);
252 if (tb[IPSET_ATTR_PORT_TO]) {
253 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
257 if (tb[IPSET_ATTR_IP_TO]) {
258 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
263 if (ip + UINT_MAX == ip_to)
264 return -IPSET_ERR_HASH_RANGE;
266 ip_set_mask_from_to(ip, ip_to, e.cidr + 1);
269 ip = ntohl(h->next.ip);
270 while (!after(ip, ip_to)) {
272 last = ip_set_range_to_cidr(ip, ip_to, &cidr);
274 p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port)
276 for (; p <= port_to; p++) {
278 ret = adtfn(set, &e, &ext, &ext, flags);
280 if (ret && !ip_set_eexist(ret, flags))
292 struct hash_netport6_elem {
293 union nf_inet_addr ip;
300 struct hash_netport6t_elem {
301 union nf_inet_addr ip;
306 unsigned long timeout;
309 struct hash_netport6c_elem {
310 union nf_inet_addr ip;
315 struct ip_set_counter counter;
318 struct hash_netport6ct_elem {
319 union nf_inet_addr ip;
324 struct ip_set_counter counter;
325 unsigned long timeout;
328 /* Common functions */
331 hash_netport6_data_equal(const struct hash_netport6_elem *ip1,
332 const struct hash_netport6_elem *ip2,
335 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
336 ip1->port == ip2->port &&
337 ip1->proto == ip2->proto &&
338 ip1->cidr == ip2->cidr;
342 hash_netport6_do_data_match(const struct hash_netport6_elem *elem)
344 return elem->nomatch ? -ENOTEMPTY : 1;
348 hash_netport6_data_set_flags(struct hash_netport6_elem *elem, u32 flags)
350 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
354 hash_netport6_data_reset_flags(struct hash_netport6_elem *elem, u8 *flags)
356 swap(*flags, elem->nomatch);
360 hash_netport6_data_netmask(struct hash_netport6_elem *elem, u8 cidr)
362 ip6_netmask(&elem->ip, cidr);
363 elem->cidr = cidr - 1;
367 hash_netport6_data_list(struct sk_buff *skb,
368 const struct hash_netport6_elem *data)
370 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
372 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
373 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
374 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr + 1) ||
375 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
377 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
378 goto nla_put_failure;
386 hash_netport6_data_next(struct hash_netport4_elem *next,
387 const struct hash_netport6_elem *d)
389 next->port = d->port;
396 #define MTYPE hash_netport6
398 #define HOST_MASK 128
399 #define IP_SET_EMIT_CREATE
400 #include "ip_set_hash_gen.h"
403 hash_netport6_kadt(struct ip_set *set, const struct sk_buff *skb,
404 const struct xt_action_param *par,
405 enum ipset_adt adt, struct ip_set_adt_opt *opt)
407 const struct hash_netport *h = set->data;
408 ipset_adtfn adtfn = set->variant->adt[adt];
409 struct hash_netport6_elem e = {
410 .cidr = h->nets[0].cidr ? h->nets[0].cidr - 1 : HOST_MASK - 1,
412 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h);
414 if (adt == IPSET_TEST)
415 e.cidr = HOST_MASK - 1;
417 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
421 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
422 ip6_netmask(&e.ip, e.cidr + 1);
424 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
428 hash_netport6_uadt(struct ip_set *set, struct nlattr *tb[],
429 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
431 const struct hash_netport *h = set->data;
432 ipset_adtfn adtfn = set->variant->adt[adt];
433 struct hash_netport6_elem e = { .cidr = HOST_MASK - 1 };
434 struct ip_set_ext ext = IP_SET_INIT_UEXT(h);
436 bool with_ports = false;
440 if (unlikely(!tb[IPSET_ATTR_IP] ||
441 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
442 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
443 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
444 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
445 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
446 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
447 return -IPSET_ERR_PROTOCOL;
448 if (unlikely(tb[IPSET_ATTR_IP_TO]))
449 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
451 if (tb[IPSET_ATTR_LINENO])
452 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
454 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
455 ip_set_get_extensions(set, tb, &ext);
459 if (tb[IPSET_ATTR_CIDR]) {
460 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
461 if (!cidr || cidr > HOST_MASK)
462 return -IPSET_ERR_INVALID_CIDR;
465 ip6_netmask(&e.ip, e.cidr + 1);
467 if (tb[IPSET_ATTR_PORT])
468 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
470 return -IPSET_ERR_PROTOCOL;
472 if (tb[IPSET_ATTR_PROTO]) {
473 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
474 with_ports = ip_set_proto_with_ports(e.proto);
477 return -IPSET_ERR_INVALID_PROTO;
479 return -IPSET_ERR_MISSING_PROTO;
481 if (!(with_ports || e.proto == IPPROTO_ICMPV6))
484 if (tb[IPSET_ATTR_CADT_FLAGS]) {
485 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
486 if (cadt_flags & IPSET_FLAG_NOMATCH)
487 flags |= (IPSET_FLAG_NOMATCH << 16);
490 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
491 ret = adtfn(set, &e, &ext, &ext, flags);
492 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
493 ip_set_eexist(ret, flags) ? 0 : ret;
496 port = ntohs(e.port);
497 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
502 port = ntohs(h->next.port);
503 for (; port <= port_to; port++) {
504 e.port = htons(port);
505 ret = adtfn(set, &e, &ext, &ext, flags);
507 if (ret && !ip_set_eexist(ret, flags))
515 static struct ip_set_type hash_netport_type __read_mostly = {
516 .name = "hash:net,port",
517 .protocol = IPSET_PROTOCOL,
518 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_NOMATCH,
519 .dimension = IPSET_DIM_TWO,
520 .family = NFPROTO_UNSPEC,
521 .revision_min = REVISION_MIN,
522 .revision_max = REVISION_MAX,
523 .create = hash_netport_create,
525 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
526 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
527 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
528 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
529 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
530 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
531 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
534 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
535 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
536 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
537 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
538 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
539 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
540 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
541 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
542 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
543 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
544 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
550 hash_netport_init(void)
552 return ip_set_type_register(&hash_netport_type);
556 hash_netport_fini(void)
558 ip_set_type_unregister(&hash_netport_type);
561 module_init(hash_netport_init);
562 module_exit(hash_netport_fini);