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 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 type of IP sets");
31 MODULE_ALIAS("ip_set_hash:ip,port");
33 /* Type specific function prefix */
34 #define TYPE hash_ipport
37 hash_ipport_same_set(const struct ip_set *a, const struct ip_set *b);
39 #define hash_ipport4_same_set hash_ipport_same_set
40 #define hash_ipport6_same_set hash_ipport_same_set
42 /* The type variant functions: IPv4 */
44 /* Member elements without timeout */
45 struct hash_ipport4_elem {
52 /* Member elements with timeout support */
53 struct hash_ipport4_telem {
58 unsigned long timeout;
62 hash_ipport4_data_equal(const struct hash_ipport4_elem *ip1,
63 const struct hash_ipport4_elem *ip2,
66 return ip1->ip == ip2->ip &&
67 ip1->port == ip2->port &&
68 ip1->proto == ip2->proto;
72 hash_ipport4_data_isnull(const struct hash_ipport4_elem *elem)
74 return elem->proto == 0;
78 hash_ipport4_data_copy(struct hash_ipport4_elem *dst,
79 const struct hash_ipport4_elem *src)
82 dst->port = src->port;
83 dst->proto = src->proto;
87 hash_ipport4_data_zero_out(struct hash_ipport4_elem *elem)
93 hash_ipport4_data_list(struct sk_buff *skb,
94 const struct hash_ipport4_elem *data)
96 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, data->ip);
97 NLA_PUT_NET16(skb, IPSET_ATTR_PORT, data->port);
98 NLA_PUT_U8(skb, IPSET_ATTR_PROTO, data->proto);
106 hash_ipport4_data_tlist(struct sk_buff *skb,
107 const struct hash_ipport4_elem *data)
109 const struct hash_ipport4_telem *tdata =
110 (const struct hash_ipport4_telem *)data;
112 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, tdata->ip);
113 NLA_PUT_NET16(skb, IPSET_ATTR_PORT, tdata->port);
114 NLA_PUT_U8(skb, IPSET_ATTR_PROTO, data->proto);
115 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
116 htonl(ip_set_timeout_get(tdata->timeout)));
126 #include <linux/netfilter/ipset/ip_set_ahash.h>
129 hash_ipport4_data_next(struct ip_set_hash *h,
130 const struct hash_ipport4_elem *d)
132 h->next.ip = ntohl(d->ip);
133 h->next.port = ntohs(d->port);
137 hash_ipport4_kadt(struct ip_set *set, const struct sk_buff *skb,
138 const struct xt_action_param *par,
139 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
141 const struct ip_set_hash *h = set->data;
142 ipset_adtfn adtfn = set->variant->adt[adt];
143 struct hash_ipport4_elem data = { };
145 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
146 &data.port, &data.proto))
149 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip);
151 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
155 hash_ipport4_uadt(struct ip_set *set, struct nlattr *tb[],
156 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
158 const struct ip_set_hash *h = set->data;
159 ipset_adtfn adtfn = set->variant->adt[adt];
160 struct hash_ipport4_elem data = { };
161 u32 ip, ip_to = 0, p = 0, port, port_to;
162 u32 timeout = h->timeout;
163 bool with_ports = false;
166 if (unlikely(!tb[IPSET_ATTR_IP] ||
167 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
168 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
169 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
170 return -IPSET_ERR_PROTOCOL;
172 if (tb[IPSET_ATTR_LINENO])
173 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
175 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &data.ip);
179 if (tb[IPSET_ATTR_PORT])
180 data.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
182 return -IPSET_ERR_PROTOCOL;
184 if (tb[IPSET_ATTR_PROTO]) {
185 data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
186 with_ports = ip_set_proto_with_ports(data.proto);
189 return -IPSET_ERR_INVALID_PROTO;
191 return -IPSET_ERR_MISSING_PROTO;
193 if (!(with_ports || data.proto == IPPROTO_ICMP))
196 if (tb[IPSET_ATTR_TIMEOUT]) {
197 if (!with_timeout(h->timeout))
198 return -IPSET_ERR_TIMEOUT;
199 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
202 if (adt == IPSET_TEST ||
203 !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR] ||
204 tb[IPSET_ATTR_PORT_TO])) {
205 ret = adtfn(set, &data, timeout, flags);
206 return ip_set_eexist(ret, flags) ? 0 : ret;
210 if (tb[IPSET_ATTR_IP_TO]) {
211 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
216 } else if (tb[IPSET_ATTR_CIDR]) {
217 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
220 return -IPSET_ERR_INVALID_CIDR;
221 ip_set_mask_from_to(ip, ip_to, cidr);
225 port_to = port = ntohs(data.port);
226 if (with_ports && tb[IPSET_ATTR_PORT_TO]) {
227 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
234 for (; !before(ip_to, ip); ip++) {
235 p = retried && ip == h->next.ip ? h->next.port : port;
236 for (; p <= port_to; p++) {
238 data.port = htons(p);
239 ret = adtfn(set, &data, timeout, flags);
241 if (ret && !ip_set_eexist(ret, flags))
251 hash_ipport_same_set(const struct ip_set *a, const struct ip_set *b)
253 const struct ip_set_hash *x = a->data;
254 const struct ip_set_hash *y = b->data;
256 /* Resizing changes htable_bits, so we ignore it */
257 return x->maxelem == y->maxelem &&
258 x->timeout == y->timeout;
261 /* The type variant functions: IPv6 */
263 struct hash_ipport6_elem {
264 union nf_inet_addr ip;
270 struct hash_ipport6_telem {
271 union nf_inet_addr ip;
275 unsigned long timeout;
279 hash_ipport6_data_equal(const struct hash_ipport6_elem *ip1,
280 const struct hash_ipport6_elem *ip2,
283 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
284 ip1->port == ip2->port &&
285 ip1->proto == ip2->proto;
289 hash_ipport6_data_isnull(const struct hash_ipport6_elem *elem)
291 return elem->proto == 0;
295 hash_ipport6_data_copy(struct hash_ipport6_elem *dst,
296 const struct hash_ipport6_elem *src)
298 memcpy(dst, src, sizeof(*dst));
302 hash_ipport6_data_zero_out(struct hash_ipport6_elem *elem)
308 hash_ipport6_data_list(struct sk_buff *skb,
309 const struct hash_ipport6_elem *data)
311 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &data->ip);
312 NLA_PUT_NET16(skb, IPSET_ATTR_PORT, data->port);
313 NLA_PUT_U8(skb, IPSET_ATTR_PROTO, data->proto);
321 hash_ipport6_data_tlist(struct sk_buff *skb,
322 const struct hash_ipport6_elem *data)
324 const struct hash_ipport6_telem *e =
325 (const struct hash_ipport6_telem *)data;
327 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &e->ip);
328 NLA_PUT_NET16(skb, IPSET_ATTR_PORT, data->port);
329 NLA_PUT_U8(skb, IPSET_ATTR_PROTO, data->proto);
330 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
331 htonl(ip_set_timeout_get(e->timeout)));
342 #define HOST_MASK 128
343 #include <linux/netfilter/ipset/ip_set_ahash.h>
346 hash_ipport6_data_next(struct ip_set_hash *h,
347 const struct hash_ipport6_elem *d)
349 h->next.port = ntohs(d->port);
353 hash_ipport6_kadt(struct ip_set *set, const struct sk_buff *skb,
354 const struct xt_action_param *par,
355 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
357 const struct ip_set_hash *h = set->data;
358 ipset_adtfn adtfn = set->variant->adt[adt];
359 struct hash_ipport6_elem data = { };
361 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
362 &data.port, &data.proto))
365 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
367 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
371 hash_ipport6_uadt(struct ip_set *set, struct nlattr *tb[],
372 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
374 const struct ip_set_hash *h = set->data;
375 ipset_adtfn adtfn = set->variant->adt[adt];
376 struct hash_ipport6_elem data = { };
378 u32 timeout = h->timeout;
379 bool with_ports = false;
382 if (unlikely(!tb[IPSET_ATTR_IP] ||
383 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
384 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
385 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
386 tb[IPSET_ATTR_IP_TO] ||
387 tb[IPSET_ATTR_CIDR]))
388 return -IPSET_ERR_PROTOCOL;
390 if (tb[IPSET_ATTR_LINENO])
391 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
393 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
397 if (tb[IPSET_ATTR_PORT])
398 data.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
400 return -IPSET_ERR_PROTOCOL;
402 if (tb[IPSET_ATTR_PROTO]) {
403 data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
404 with_ports = ip_set_proto_with_ports(data.proto);
407 return -IPSET_ERR_INVALID_PROTO;
409 return -IPSET_ERR_MISSING_PROTO;
411 if (!(with_ports || data.proto == IPPROTO_ICMPV6))
414 if (tb[IPSET_ATTR_TIMEOUT]) {
415 if (!with_timeout(h->timeout))
416 return -IPSET_ERR_TIMEOUT;
417 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
420 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
421 ret = adtfn(set, &data, timeout, flags);
422 return ip_set_eexist(ret, flags) ? 0 : ret;
425 port = ntohs(data.port);
426 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
432 for (; port <= port_to; port++) {
433 data.port = htons(port);
434 ret = adtfn(set, &data, timeout, flags);
436 if (ret && !ip_set_eexist(ret, flags))
444 /* Create hash:ip type of sets */
447 hash_ipport_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
449 struct ip_set_hash *h;
450 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
454 if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6))
455 return -IPSET_ERR_INVALID_FAMILY;
457 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
458 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
459 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
460 return -IPSET_ERR_PROTOCOL;
462 if (tb[IPSET_ATTR_HASHSIZE]) {
463 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
464 if (hashsize < IPSET_MIMINAL_HASHSIZE)
465 hashsize = IPSET_MIMINAL_HASHSIZE;
468 if (tb[IPSET_ATTR_MAXELEM])
469 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
471 h = kzalloc(sizeof(*h), GFP_KERNEL);
475 h->maxelem = maxelem;
476 get_random_bytes(&h->initval, sizeof(h->initval));
477 h->timeout = IPSET_NO_TIMEOUT;
479 hbits = htable_bits(hashsize);
480 hsize = htable_size(hbits);
485 h->table = ip_set_alloc(hsize);
490 h->table->htable_bits = hbits;
494 if (tb[IPSET_ATTR_TIMEOUT]) {
495 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
497 set->variant = set->family == NFPROTO_IPV4
498 ? &hash_ipport4_tvariant : &hash_ipport6_tvariant;
500 if (set->family == NFPROTO_IPV4)
501 hash_ipport4_gc_init(set);
503 hash_ipport6_gc_init(set);
505 set->variant = set->family == NFPROTO_IPV4
506 ? &hash_ipport4_variant : &hash_ipport6_variant;
509 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
510 set->name, jhash_size(h->table->htable_bits),
511 h->table->htable_bits, h->maxelem, set->data, h->table);
516 static struct ip_set_type hash_ipport_type __read_mostly = {
517 .name = "hash:ip,port",
518 .protocol = IPSET_PROTOCOL,
519 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT,
520 .dimension = IPSET_DIM_TWO,
521 .family = NFPROTO_UNSPEC,
523 .revision_max = 1, /* SCTP and UDPLITE support added */
524 .create = hash_ipport_create,
526 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
527 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
528 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
529 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
530 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
531 [IPSET_ATTR_TIMEOUT] = { .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_CIDR] = { .type = NLA_U8 },
539 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
540 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
541 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
547 hash_ipport_init(void)
549 return ip_set_type_register(&hash_ipport_type);
553 hash_ipport_fini(void)
555 ip_set_type_unregister(&hash_ipport_type);
558 module_init(hash_ipport_init);
559 module_exit(hash_ipport_fini);