1 /* Copyright (C) 2003-2011 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,ip 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_timeout.h>
25 #include <linux/netfilter/ipset/ip_set_getport.h>
26 #include <linux/netfilter/ipset/ip_set_hash.h>
28 MODULE_LICENSE("GPL");
29 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
30 MODULE_DESCRIPTION("hash:ip,port,ip type of IP sets");
31 MODULE_ALIAS("ip_set_hash:ip,port,ip");
33 /* Type specific function prefix */
34 #define TYPE hash_ipportip
37 hash_ipportip_same_set(const struct ip_set *a, const struct ip_set *b);
39 #define hash_ipportip4_same_set hash_ipportip_same_set
40 #define hash_ipportip6_same_set hash_ipportip_same_set
42 /* The type variant functions: IPv4 */
44 /* Member elements without timeout */
45 struct hash_ipportip4_elem {
53 /* Member elements with timeout support */
54 struct hash_ipportip4_telem {
60 unsigned long timeout;
64 hash_ipportip4_data_equal(const struct hash_ipportip4_elem *ip1,
65 const struct hash_ipportip4_elem *ip2,
68 return ip1->ip == ip2->ip &&
69 ip1->ip2 == ip2->ip2 &&
70 ip1->port == ip2->port &&
71 ip1->proto == ip2->proto;
75 hash_ipportip4_data_isnull(const struct hash_ipportip4_elem *elem)
77 return elem->proto == 0;
81 hash_ipportip4_data_copy(struct hash_ipportip4_elem *dst,
82 const struct hash_ipportip4_elem *src)
84 memcpy(dst, src, sizeof(*dst));
88 hash_ipportip4_data_zero_out(struct hash_ipportip4_elem *elem)
94 hash_ipportip4_data_list(struct sk_buff *skb,
95 const struct hash_ipportip4_elem *data)
97 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
98 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip2) ||
99 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
100 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto))
101 goto nla_put_failure;
109 hash_ipportip4_data_tlist(struct sk_buff *skb,
110 const struct hash_ipportip4_elem *data)
112 const struct hash_ipportip4_telem *tdata =
113 (const struct hash_ipportip4_telem *)data;
115 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, tdata->ip) ||
116 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, tdata->ip2) ||
117 nla_put_net16(skb, IPSET_ATTR_PORT, tdata->port) ||
118 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
119 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
120 htonl(ip_set_timeout_get(tdata->timeout))))
121 goto nla_put_failure;
130 #include <linux/netfilter/ipset/ip_set_ahash.h>
133 hash_ipportip4_data_next(struct ip_set_hash *h,
134 const struct hash_ipportip4_elem *d)
136 h->next.ip = ntohl(d->ip);
137 h->next.port = ntohs(d->port);
141 hash_ipportip4_kadt(struct ip_set *set, const struct sk_buff *skb,
142 const struct xt_action_param *par,
143 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
145 const struct ip_set_hash *h = set->data;
146 ipset_adtfn adtfn = set->variant->adt[adt];
147 struct hash_ipportip4_elem data = { };
149 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
150 &data.port, &data.proto))
153 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip);
154 ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &data.ip2);
156 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
160 hash_ipportip4_uadt(struct ip_set *set, struct nlattr *tb[],
161 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
163 const struct ip_set_hash *h = set->data;
164 ipset_adtfn adtfn = set->variant->adt[adt];
165 struct hash_ipportip4_elem data = { };
166 u32 ip, ip_to = 0, p = 0, port, port_to;
167 u32 timeout = h->timeout;
168 bool with_ports = false;
171 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
172 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
173 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
174 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
175 return -IPSET_ERR_PROTOCOL;
177 if (tb[IPSET_ATTR_LINENO])
178 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
180 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &data.ip);
184 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP2], &data.ip2);
188 if (tb[IPSET_ATTR_PORT])
189 data.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
191 return -IPSET_ERR_PROTOCOL;
193 if (tb[IPSET_ATTR_PROTO]) {
194 data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
195 with_ports = ip_set_proto_with_ports(data.proto);
198 return -IPSET_ERR_INVALID_PROTO;
200 return -IPSET_ERR_MISSING_PROTO;
202 if (!(with_ports || data.proto == IPPROTO_ICMP))
205 if (tb[IPSET_ATTR_TIMEOUT]) {
206 if (!with_timeout(h->timeout))
207 return -IPSET_ERR_TIMEOUT;
208 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
211 if (adt == IPSET_TEST ||
212 !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR] ||
213 tb[IPSET_ATTR_PORT_TO])) {
214 ret = adtfn(set, &data, timeout, flags);
215 return ip_set_eexist(ret, flags) ? 0 : ret;
219 if (tb[IPSET_ATTR_IP_TO]) {
220 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
225 } else if (tb[IPSET_ATTR_CIDR]) {
226 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
229 return -IPSET_ERR_INVALID_CIDR;
230 ip_set_mask_from_to(ip, ip_to, cidr);
234 port_to = port = ntohs(data.port);
235 if (with_ports && tb[IPSET_ATTR_PORT_TO]) {
236 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
243 for (; !before(ip_to, ip); ip++) {
244 p = retried && ip == h->next.ip ? h->next.port : port;
245 for (; p <= port_to; p++) {
247 data.port = htons(p);
248 ret = adtfn(set, &data, timeout, flags);
250 if (ret && !ip_set_eexist(ret, flags))
260 hash_ipportip_same_set(const struct ip_set *a, const struct ip_set *b)
262 const struct ip_set_hash *x = a->data;
263 const struct ip_set_hash *y = b->data;
265 /* Resizing changes htable_bits, so we ignore it */
266 return x->maxelem == y->maxelem &&
267 x->timeout == y->timeout;
270 /* The type variant functions: IPv6 */
272 struct hash_ipportip6_elem {
273 union nf_inet_addr ip;
274 union nf_inet_addr ip2;
280 struct hash_ipportip6_telem {
281 union nf_inet_addr ip;
282 union nf_inet_addr ip2;
286 unsigned long timeout;
290 hash_ipportip6_data_equal(const struct hash_ipportip6_elem *ip1,
291 const struct hash_ipportip6_elem *ip2,
294 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
295 ipv6_addr_cmp(&ip1->ip2.in6, &ip2->ip2.in6) == 0 &&
296 ip1->port == ip2->port &&
297 ip1->proto == ip2->proto;
301 hash_ipportip6_data_isnull(const struct hash_ipportip6_elem *elem)
303 return elem->proto == 0;
307 hash_ipportip6_data_copy(struct hash_ipportip6_elem *dst,
308 const struct hash_ipportip6_elem *src)
310 memcpy(dst, src, sizeof(*dst));
314 hash_ipportip6_data_zero_out(struct hash_ipportip6_elem *elem)
320 hash_ipportip6_data_list(struct sk_buff *skb,
321 const struct hash_ipportip6_elem *data)
323 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
324 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip2.in6) ||
325 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
326 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto))
327 goto nla_put_failure;
335 hash_ipportip6_data_tlist(struct sk_buff *skb,
336 const struct hash_ipportip6_elem *data)
338 const struct hash_ipportip6_telem *e =
339 (const struct hash_ipportip6_telem *)data;
341 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6) ||
342 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip2.in6) ||
343 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
344 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
345 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
346 htonl(ip_set_timeout_get(e->timeout))))
347 goto nla_put_failure;
358 #define HOST_MASK 128
359 #include <linux/netfilter/ipset/ip_set_ahash.h>
362 hash_ipportip6_data_next(struct ip_set_hash *h,
363 const struct hash_ipportip6_elem *d)
365 h->next.port = ntohs(d->port);
369 hash_ipportip6_kadt(struct ip_set *set, const struct sk_buff *skb,
370 const struct xt_action_param *par,
371 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
373 const struct ip_set_hash *h = set->data;
374 ipset_adtfn adtfn = set->variant->adt[adt];
375 struct hash_ipportip6_elem data = { };
377 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
378 &data.port, &data.proto))
381 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
382 ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &data.ip2.in6);
384 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
388 hash_ipportip6_uadt(struct ip_set *set, struct nlattr *tb[],
389 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
391 const struct ip_set_hash *h = set->data;
392 ipset_adtfn adtfn = set->variant->adt[adt];
393 struct hash_ipportip6_elem data = { };
395 u32 timeout = h->timeout;
396 bool with_ports = false;
399 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
400 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
401 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
402 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
403 tb[IPSET_ATTR_IP_TO] ||
404 tb[IPSET_ATTR_CIDR]))
405 return -IPSET_ERR_PROTOCOL;
407 if (tb[IPSET_ATTR_LINENO])
408 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
410 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
414 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &data.ip2);
418 if (tb[IPSET_ATTR_PORT])
419 data.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
421 return -IPSET_ERR_PROTOCOL;
423 if (tb[IPSET_ATTR_PROTO]) {
424 data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
425 with_ports = ip_set_proto_with_ports(data.proto);
428 return -IPSET_ERR_INVALID_PROTO;
430 return -IPSET_ERR_MISSING_PROTO;
432 if (!(with_ports || data.proto == IPPROTO_ICMPV6))
435 if (tb[IPSET_ATTR_TIMEOUT]) {
436 if (!with_timeout(h->timeout))
437 return -IPSET_ERR_TIMEOUT;
438 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
441 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
442 ret = adtfn(set, &data, timeout, flags);
443 return ip_set_eexist(ret, flags) ? 0 : ret;
446 port = ntohs(data.port);
447 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
453 for (; port <= port_to; port++) {
454 data.port = htons(port);
455 ret = adtfn(set, &data, timeout, flags);
457 if (ret && !ip_set_eexist(ret, flags))
465 /* Create hash:ip type of sets */
468 hash_ipportip_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
470 struct ip_set_hash *h;
471 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
475 if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6))
476 return -IPSET_ERR_INVALID_FAMILY;
478 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
479 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
480 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
481 return -IPSET_ERR_PROTOCOL;
483 if (tb[IPSET_ATTR_HASHSIZE]) {
484 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
485 if (hashsize < IPSET_MIMINAL_HASHSIZE)
486 hashsize = IPSET_MIMINAL_HASHSIZE;
489 if (tb[IPSET_ATTR_MAXELEM])
490 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
492 h = kzalloc(sizeof(*h), GFP_KERNEL);
496 h->maxelem = maxelem;
497 get_random_bytes(&h->initval, sizeof(h->initval));
498 h->timeout = IPSET_NO_TIMEOUT;
500 hbits = htable_bits(hashsize);
501 hsize = htable_size(hbits);
506 h->table = ip_set_alloc(hsize);
511 h->table->htable_bits = hbits;
515 if (tb[IPSET_ATTR_TIMEOUT]) {
516 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
518 set->variant = set->family == NFPROTO_IPV4
519 ? &hash_ipportip4_tvariant : &hash_ipportip6_tvariant;
521 if (set->family == NFPROTO_IPV4)
522 hash_ipportip4_gc_init(set);
524 hash_ipportip6_gc_init(set);
526 set->variant = set->family == NFPROTO_IPV4
527 ? &hash_ipportip4_variant : &hash_ipportip6_variant;
530 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
531 set->name, jhash_size(h->table->htable_bits),
532 h->table->htable_bits, h->maxelem, set->data, h->table);
537 static struct ip_set_type hash_ipportip_type __read_mostly = {
538 .name = "hash:ip,port,ip",
539 .protocol = IPSET_PROTOCOL,
540 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2,
541 .dimension = IPSET_DIM_THREE,
542 .family = NFPROTO_UNSPEC,
544 .revision_max = 1, /* SCTP and UDPLITE support added */
545 .create = hash_ipportip_create,
547 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
548 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
549 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
550 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
551 [IPSET_ATTR_TIMEOUT] = { .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_PORT] = { .type = NLA_U16 },
558 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
559 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
560 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
561 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
562 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
568 hash_ipportip_init(void)
570 return ip_set_type_register(&hash_ipportip_type);
574 hash_ipportip_fini(void)
576 ip_set_type_unregister(&hash_ipportip_type);
579 module_init(hash_ipportip_init);
580 module_exit(hash_ipportip_fini);