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 #define REVISION_MIN 0
29 #define REVISION_MAX 1 /* SCTP and UDPLITE support added */
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
33 IP_SET_MODULE_DESC("hash:ip,port,ip", REVISION_MIN, REVISION_MAX);
34 MODULE_ALIAS("ip_set_hash:ip,port,ip");
36 /* Type specific function prefix */
37 #define TYPE hash_ipportip
40 hash_ipportip_same_set(const struct ip_set *a, const struct ip_set *b);
42 #define hash_ipportip4_same_set hash_ipportip_same_set
43 #define hash_ipportip6_same_set hash_ipportip_same_set
45 /* The type variant functions: IPv4 */
47 /* Member elements without timeout */
48 struct hash_ipportip4_elem {
56 /* Member elements with timeout support */
57 struct hash_ipportip4_telem {
63 unsigned long timeout;
67 hash_ipportip4_data_equal(const struct hash_ipportip4_elem *ip1,
68 const struct hash_ipportip4_elem *ip2,
71 return ip1->ip == ip2->ip &&
72 ip1->ip2 == ip2->ip2 &&
73 ip1->port == ip2->port &&
74 ip1->proto == ip2->proto;
78 hash_ipportip4_data_isnull(const struct hash_ipportip4_elem *elem)
80 return elem->proto == 0;
84 hash_ipportip4_data_copy(struct hash_ipportip4_elem *dst,
85 const struct hash_ipportip4_elem *src)
87 memcpy(dst, src, sizeof(*dst));
91 hash_ipportip4_data_zero_out(struct hash_ipportip4_elem *elem)
97 hash_ipportip4_data_list(struct sk_buff *skb,
98 const struct hash_ipportip4_elem *data)
100 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
101 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip2) ||
102 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
103 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto))
104 goto nla_put_failure;
112 hash_ipportip4_data_tlist(struct sk_buff *skb,
113 const struct hash_ipportip4_elem *data)
115 const struct hash_ipportip4_telem *tdata =
116 (const struct hash_ipportip4_telem *)data;
118 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, tdata->ip) ||
119 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, tdata->ip2) ||
120 nla_put_net16(skb, IPSET_ATTR_PORT, tdata->port) ||
121 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
122 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
123 htonl(ip_set_timeout_get(tdata->timeout))))
124 goto nla_put_failure;
133 #include <linux/netfilter/ipset/ip_set_ahash.h>
136 hash_ipportip4_data_next(struct ip_set_hash *h,
137 const struct hash_ipportip4_elem *d)
140 h->next.port = d->port;
144 hash_ipportip4_kadt(struct ip_set *set, const struct sk_buff *skb,
145 const struct xt_action_param *par,
146 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
148 const struct ip_set_hash *h = set->data;
149 ipset_adtfn adtfn = set->variant->adt[adt];
150 struct hash_ipportip4_elem data = { };
152 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
153 &data.port, &data.proto))
156 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip);
157 ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &data.ip2);
159 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
163 hash_ipportip4_uadt(struct ip_set *set, struct nlattr *tb[],
164 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
166 const struct ip_set_hash *h = set->data;
167 ipset_adtfn adtfn = set->variant->adt[adt];
168 struct hash_ipportip4_elem data = { };
169 u32 ip, ip_to = 0, p = 0, port, port_to;
170 u32 timeout = h->timeout;
171 bool with_ports = false;
174 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
175 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
176 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
177 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
178 return -IPSET_ERR_PROTOCOL;
180 if (tb[IPSET_ATTR_LINENO])
181 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
183 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &data.ip);
187 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP2], &data.ip2);
191 if (tb[IPSET_ATTR_PORT])
192 data.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
194 return -IPSET_ERR_PROTOCOL;
196 if (tb[IPSET_ATTR_PROTO]) {
197 data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
198 with_ports = ip_set_proto_with_ports(data.proto);
201 return -IPSET_ERR_INVALID_PROTO;
203 return -IPSET_ERR_MISSING_PROTO;
205 if (!(with_ports || data.proto == IPPROTO_ICMP))
208 if (tb[IPSET_ATTR_TIMEOUT]) {
209 if (!with_timeout(h->timeout))
210 return -IPSET_ERR_TIMEOUT;
211 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
214 if (adt == IPSET_TEST ||
215 !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR] ||
216 tb[IPSET_ATTR_PORT_TO])) {
217 ret = adtfn(set, &data, timeout, flags);
218 return ip_set_eexist(ret, flags) ? 0 : ret;
222 if (tb[IPSET_ATTR_IP_TO]) {
223 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
228 } else if (tb[IPSET_ATTR_CIDR]) {
229 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
231 if (!cidr || cidr > 32)
232 return -IPSET_ERR_INVALID_CIDR;
233 ip_set_mask_from_to(ip, ip_to, cidr);
237 port_to = port = ntohs(data.port);
238 if (with_ports && tb[IPSET_ATTR_PORT_TO]) {
239 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
245 ip = ntohl(h->next.ip);
246 for (; !before(ip_to, ip); ip++) {
247 p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port)
249 for (; p <= port_to; p++) {
251 data.port = htons(p);
252 ret = adtfn(set, &data, timeout, flags);
254 if (ret && !ip_set_eexist(ret, flags))
264 hash_ipportip_same_set(const struct ip_set *a, const struct ip_set *b)
266 const struct ip_set_hash *x = a->data;
267 const struct ip_set_hash *y = b->data;
269 /* Resizing changes htable_bits, so we ignore it */
270 return x->maxelem == y->maxelem &&
271 x->timeout == y->timeout;
274 /* The type variant functions: IPv6 */
276 struct hash_ipportip6_elem {
277 union nf_inet_addr ip;
278 union nf_inet_addr ip2;
284 struct hash_ipportip6_telem {
285 union nf_inet_addr ip;
286 union nf_inet_addr ip2;
290 unsigned long timeout;
294 hash_ipportip6_data_equal(const struct hash_ipportip6_elem *ip1,
295 const struct hash_ipportip6_elem *ip2,
298 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
299 ipv6_addr_cmp(&ip1->ip2.in6, &ip2->ip2.in6) == 0 &&
300 ip1->port == ip2->port &&
301 ip1->proto == ip2->proto;
305 hash_ipportip6_data_isnull(const struct hash_ipportip6_elem *elem)
307 return elem->proto == 0;
311 hash_ipportip6_data_copy(struct hash_ipportip6_elem *dst,
312 const struct hash_ipportip6_elem *src)
314 memcpy(dst, src, sizeof(*dst));
318 hash_ipportip6_data_zero_out(struct hash_ipportip6_elem *elem)
324 hash_ipportip6_data_list(struct sk_buff *skb,
325 const struct hash_ipportip6_elem *data)
327 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
328 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip2.in6) ||
329 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
330 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto))
331 goto nla_put_failure;
339 hash_ipportip6_data_tlist(struct sk_buff *skb,
340 const struct hash_ipportip6_elem *data)
342 const struct hash_ipportip6_telem *e =
343 (const struct hash_ipportip6_telem *)data;
345 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6) ||
346 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip2.in6) ||
347 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
348 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
349 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
350 htonl(ip_set_timeout_get(e->timeout))))
351 goto nla_put_failure;
362 #define HOST_MASK 128
363 #include <linux/netfilter/ipset/ip_set_ahash.h>
366 hash_ipportip6_data_next(struct ip_set_hash *h,
367 const struct hash_ipportip6_elem *d)
369 h->next.port = d->port;
373 hash_ipportip6_kadt(struct ip_set *set, const struct sk_buff *skb,
374 const struct xt_action_param *par,
375 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
377 const struct ip_set_hash *h = set->data;
378 ipset_adtfn adtfn = set->variant->adt[adt];
379 struct hash_ipportip6_elem data = { };
381 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
382 &data.port, &data.proto))
385 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
386 ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &data.ip2.in6);
388 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
392 hash_ipportip6_uadt(struct ip_set *set, struct nlattr *tb[],
393 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
395 const struct ip_set_hash *h = set->data;
396 ipset_adtfn adtfn = set->variant->adt[adt];
397 struct hash_ipportip6_elem data = { };
399 u32 timeout = h->timeout;
400 bool with_ports = false;
403 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
404 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
405 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
406 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
407 tb[IPSET_ATTR_IP_TO] ||
408 tb[IPSET_ATTR_CIDR]))
409 return -IPSET_ERR_PROTOCOL;
411 if (tb[IPSET_ATTR_LINENO])
412 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
414 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
418 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &data.ip2);
422 if (tb[IPSET_ATTR_PORT])
423 data.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
425 return -IPSET_ERR_PROTOCOL;
427 if (tb[IPSET_ATTR_PROTO]) {
428 data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
429 with_ports = ip_set_proto_with_ports(data.proto);
432 return -IPSET_ERR_INVALID_PROTO;
434 return -IPSET_ERR_MISSING_PROTO;
436 if (!(with_ports || data.proto == IPPROTO_ICMPV6))
439 if (tb[IPSET_ATTR_TIMEOUT]) {
440 if (!with_timeout(h->timeout))
441 return -IPSET_ERR_TIMEOUT;
442 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
445 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
446 ret = adtfn(set, &data, timeout, flags);
447 return ip_set_eexist(ret, flags) ? 0 : ret;
450 port = ntohs(data.port);
451 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
456 port = ntohs(h->next.port);
457 for (; port <= port_to; port++) {
458 data.port = htons(port);
459 ret = adtfn(set, &data, timeout, flags);
461 if (ret && !ip_set_eexist(ret, flags))
469 /* Create hash:ip type of sets */
472 hash_ipportip_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
474 struct ip_set_hash *h;
475 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
479 if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6))
480 return -IPSET_ERR_INVALID_FAMILY;
482 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
483 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
484 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
485 return -IPSET_ERR_PROTOCOL;
487 if (tb[IPSET_ATTR_HASHSIZE]) {
488 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
489 if (hashsize < IPSET_MIMINAL_HASHSIZE)
490 hashsize = IPSET_MIMINAL_HASHSIZE;
493 if (tb[IPSET_ATTR_MAXELEM])
494 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
496 h = kzalloc(sizeof(*h), GFP_KERNEL);
500 h->maxelem = maxelem;
501 get_random_bytes(&h->initval, sizeof(h->initval));
502 h->timeout = IPSET_NO_TIMEOUT;
504 hbits = htable_bits(hashsize);
505 hsize = htable_size(hbits);
510 h->table = ip_set_alloc(hsize);
515 h->table->htable_bits = hbits;
519 if (tb[IPSET_ATTR_TIMEOUT]) {
520 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
522 set->variant = set->family == NFPROTO_IPV4
523 ? &hash_ipportip4_tvariant : &hash_ipportip6_tvariant;
525 if (set->family == NFPROTO_IPV4)
526 hash_ipportip4_gc_init(set);
528 hash_ipportip6_gc_init(set);
530 set->variant = set->family == NFPROTO_IPV4
531 ? &hash_ipportip4_variant : &hash_ipportip6_variant;
534 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
535 set->name, jhash_size(h->table->htable_bits),
536 h->table->htable_bits, h->maxelem, set->data, h->table);
541 static struct ip_set_type hash_ipportip_type __read_mostly = {
542 .name = "hash:ip,port,ip",
543 .protocol = IPSET_PROTOCOL,
544 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2,
545 .dimension = IPSET_DIM_THREE,
546 .family = NFPROTO_UNSPEC,
547 .revision_min = REVISION_MIN,
548 .revision_max = REVISION_MAX,
549 .create = hash_ipportip_create,
551 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
552 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
553 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
554 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
555 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
558 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
559 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
560 [IPSET_ATTR_IP2] = { .type = NLA_NESTED },
561 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
562 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
563 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
564 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
565 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
566 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
572 hash_ipportip_init(void)
574 return ip_set_type_register(&hash_ipportip_type);
578 hash_ipportip_fini(void)
580 ip_set_type_unregister(&hash_ipportip_type);
583 module_init(hash_ipportip_init);
584 module_exit(hash_ipportip_fini);