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: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_timeout.h>
24 #include <linux/netfilter/ipset/ip_set_getport.h>
25 #include <linux/netfilter/ipset/ip_set_hash.h>
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
29 MODULE_DESCRIPTION("hash:net,port type of IP sets");
30 MODULE_ALIAS("ip_set_hash:net,port");
32 /* Type specific function prefix */
33 #define TYPE hash_netport
36 hash_netport_same_set(const struct ip_set *a, const struct ip_set *b);
38 #define hash_netport4_same_set hash_netport_same_set
39 #define hash_netport6_same_set hash_netport_same_set
41 /* The type variant functions: IPv4 */
43 /* Member elements without timeout */
44 struct hash_netport4_elem {
51 /* Member elements with timeout support */
52 struct hash_netport4_telem {
57 unsigned long timeout;
61 hash_netport4_data_equal(const struct hash_netport4_elem *ip1,
62 const struct hash_netport4_elem *ip2)
64 return ip1->ip == ip2->ip &&
65 ip1->port == ip2->port &&
66 ip1->proto == ip2->proto &&
67 ip1->cidr == ip2->cidr;
71 hash_netport4_data_isnull(const struct hash_netport4_elem *elem)
73 return elem->proto == 0;
77 hash_netport4_data_copy(struct hash_netport4_elem *dst,
78 const struct hash_netport4_elem *src)
81 dst->port = src->port;
82 dst->proto = src->proto;
83 dst->cidr = src->cidr;
87 hash_netport4_data_netmask(struct hash_netport4_elem *elem, u8 cidr)
89 elem->ip &= ip_set_netmask(cidr);
94 hash_netport4_data_zero_out(struct hash_netport4_elem *elem)
100 hash_netport4_data_list(struct sk_buff *skb,
101 const struct hash_netport4_elem *data)
103 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, data->ip);
104 NLA_PUT_NET16(skb, IPSET_ATTR_PORT, data->port);
105 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
106 NLA_PUT_U8(skb, IPSET_ATTR_PROTO, data->proto);
114 hash_netport4_data_tlist(struct sk_buff *skb,
115 const struct hash_netport4_elem *data)
117 const struct hash_netport4_telem *tdata =
118 (const struct hash_netport4_telem *)data;
120 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, tdata->ip);
121 NLA_PUT_NET16(skb, IPSET_ATTR_PORT, tdata->port);
122 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
123 NLA_PUT_U8(skb, IPSET_ATTR_PROTO, data->proto);
124 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
125 htonl(ip_set_timeout_get(tdata->timeout)));
133 #define IP_SET_HASH_WITH_PROTO
134 #define IP_SET_HASH_WITH_NETS
138 #include <linux/netfilter/ipset/ip_set_ahash.h>
141 hash_netport4_kadt(struct ip_set *set, const struct sk_buff *skb,
142 enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
144 const struct ip_set_hash *h = set->data;
145 ipset_adtfn adtfn = set->variant->adt[adt];
146 struct hash_netport4_elem data = {
147 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
152 if (adt == IPSET_TEST)
153 data.cidr = HOST_MASK;
155 if (!ip_set_get_ip4_port(skb, flags & IPSET_DIM_TWO_SRC,
156 &data.port, &data.proto))
159 ip4addrptr(skb, flags & IPSET_DIM_ONE_SRC, &data.ip);
160 data.ip &= ip_set_netmask(data.cidr);
162 return adtfn(set, &data, h->timeout);
166 hash_netport4_uadt(struct ip_set *set, struct nlattr *tb[],
167 enum ipset_adt adt, u32 *lineno, u32 flags)
169 const struct ip_set_hash *h = set->data;
170 ipset_adtfn adtfn = set->variant->adt[adt];
171 struct hash_netport4_elem data = { .cidr = HOST_MASK };
173 u32 timeout = h->timeout;
174 bool with_ports = false;
177 if (unlikely(!tb[IPSET_ATTR_IP] ||
178 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
179 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
180 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
181 return -IPSET_ERR_PROTOCOL;
183 if (tb[IPSET_ATTR_LINENO])
184 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
186 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &data.ip);
190 if (tb[IPSET_ATTR_CIDR])
191 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
193 return -IPSET_ERR_INVALID_CIDR;
194 data.ip &= ip_set_netmask(data.cidr);
196 if (tb[IPSET_ATTR_PORT])
197 data.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
199 return -IPSET_ERR_PROTOCOL;
201 if (tb[IPSET_ATTR_PROTO]) {
202 data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
203 with_ports = ip_set_proto_with_ports(data.proto);
206 return -IPSET_ERR_INVALID_PROTO;
208 return -IPSET_ERR_MISSING_PROTO;
210 if (!(with_ports || data.proto == IPPROTO_ICMP))
213 if (tb[IPSET_ATTR_TIMEOUT]) {
214 if (!with_timeout(h->timeout))
215 return -IPSET_ERR_TIMEOUT;
216 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
219 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
220 ret = adtfn(set, &data, timeout);
221 return ip_set_eexist(ret, flags) ? 0 : ret;
224 port = ntohs(data.port);
225 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
229 for (; port <= port_to; port++) {
230 data.port = htons(port);
231 ret = adtfn(set, &data, timeout);
233 if (ret && !ip_set_eexist(ret, flags))
242 hash_netport_same_set(const struct ip_set *a, const struct ip_set *b)
244 const struct ip_set_hash *x = a->data;
245 const struct ip_set_hash *y = b->data;
247 /* Resizing changes htable_bits, so we ignore it */
248 return x->maxelem == y->maxelem &&
249 x->timeout == y->timeout;
252 /* The type variant functions: IPv6 */
254 struct hash_netport6_elem {
255 union nf_inet_addr ip;
261 struct hash_netport6_telem {
262 union nf_inet_addr ip;
266 unsigned long timeout;
270 hash_netport6_data_equal(const struct hash_netport6_elem *ip1,
271 const struct hash_netport6_elem *ip2)
273 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
274 ip1->port == ip2->port &&
275 ip1->proto == ip2->proto &&
276 ip1->cidr == ip2->cidr;
280 hash_netport6_data_isnull(const struct hash_netport6_elem *elem)
282 return elem->proto == 0;
286 hash_netport6_data_copy(struct hash_netport6_elem *dst,
287 const struct hash_netport6_elem *src)
289 memcpy(dst, src, sizeof(*dst));
293 hash_netport6_data_zero_out(struct hash_netport6_elem *elem)
299 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
301 ip->ip6[0] &= ip_set_netmask6(prefix)[0];
302 ip->ip6[1] &= ip_set_netmask6(prefix)[1];
303 ip->ip6[2] &= ip_set_netmask6(prefix)[2];
304 ip->ip6[3] &= ip_set_netmask6(prefix)[3];
308 hash_netport6_data_netmask(struct hash_netport6_elem *elem, u8 cidr)
310 ip6_netmask(&elem->ip, cidr);
315 hash_netport6_data_list(struct sk_buff *skb,
316 const struct hash_netport6_elem *data)
318 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &data->ip);
319 NLA_PUT_NET16(skb, IPSET_ATTR_PORT, data->port);
320 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
321 NLA_PUT_U8(skb, IPSET_ATTR_PROTO, data->proto);
329 hash_netport6_data_tlist(struct sk_buff *skb,
330 const struct hash_netport6_elem *data)
332 const struct hash_netport6_telem *e =
333 (const struct hash_netport6_telem *)data;
335 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &e->ip);
336 NLA_PUT_NET16(skb, IPSET_ATTR_PORT, data->port);
337 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
338 NLA_PUT_U8(skb, IPSET_ATTR_PROTO, data->proto);
339 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
340 htonl(ip_set_timeout_get(e->timeout)));
351 #define HOST_MASK 128
352 #include <linux/netfilter/ipset/ip_set_ahash.h>
355 hash_netport6_kadt(struct ip_set *set, const struct sk_buff *skb,
356 enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
358 const struct ip_set_hash *h = set->data;
359 ipset_adtfn adtfn = set->variant->adt[adt];
360 struct hash_netport6_elem data = {
361 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
366 if (adt == IPSET_TEST)
367 data.cidr = HOST_MASK;
369 if (!ip_set_get_ip6_port(skb, flags & IPSET_DIM_TWO_SRC,
370 &data.port, &data.proto))
373 ip6addrptr(skb, flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
374 ip6_netmask(&data.ip, data.cidr);
376 return adtfn(set, &data, h->timeout);
380 hash_netport6_uadt(struct ip_set *set, struct nlattr *tb[],
381 enum ipset_adt adt, u32 *lineno, u32 flags)
383 const struct ip_set_hash *h = set->data;
384 ipset_adtfn adtfn = set->variant->adt[adt];
385 struct hash_netport6_elem data = { .cidr = HOST_MASK };
387 u32 timeout = h->timeout;
388 bool with_ports = false;
391 if (unlikely(!tb[IPSET_ATTR_IP] ||
392 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
393 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
394 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
395 return -IPSET_ERR_PROTOCOL;
397 if (tb[IPSET_ATTR_LINENO])
398 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
400 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
404 if (tb[IPSET_ATTR_CIDR])
405 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
407 return -IPSET_ERR_INVALID_CIDR;
408 ip6_netmask(&data.ip, data.cidr);
410 if (tb[IPSET_ATTR_PORT])
411 data.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
413 return -IPSET_ERR_PROTOCOL;
415 if (tb[IPSET_ATTR_PROTO]) {
416 data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
417 with_ports = ip_set_proto_with_ports(data.proto);
420 return -IPSET_ERR_INVALID_PROTO;
422 return -IPSET_ERR_MISSING_PROTO;
424 if (!(with_ports || data.proto == IPPROTO_ICMPV6))
427 if (tb[IPSET_ATTR_TIMEOUT]) {
428 if (!with_timeout(h->timeout))
429 return -IPSET_ERR_TIMEOUT;
430 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
433 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
434 ret = adtfn(set, &data, timeout);
435 return ip_set_eexist(ret, flags) ? 0 : ret;
438 port = ntohs(data.port);
439 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
443 for (; port <= port_to; port++) {
444 data.port = htons(port);
445 ret = adtfn(set, &data, timeout);
447 if (ret && !ip_set_eexist(ret, flags))
455 /* Create hash:ip type of sets */
458 hash_netport_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
460 struct ip_set_hash *h;
461 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
464 if (!(set->family == AF_INET || set->family == AF_INET6))
465 return -IPSET_ERR_INVALID_FAMILY;
467 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
468 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
469 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
470 return -IPSET_ERR_PROTOCOL;
472 if (tb[IPSET_ATTR_HASHSIZE]) {
473 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
474 if (hashsize < IPSET_MIMINAL_HASHSIZE)
475 hashsize = IPSET_MIMINAL_HASHSIZE;
478 if (tb[IPSET_ATTR_MAXELEM])
479 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
481 h = kzalloc(sizeof(*h)
482 + sizeof(struct ip_set_hash_nets)
483 * (set->family == AF_INET ? 32 : 128), GFP_KERNEL);
487 h->maxelem = maxelem;
488 get_random_bytes(&h->initval, sizeof(h->initval));
489 h->timeout = IPSET_NO_TIMEOUT;
491 hbits = htable_bits(hashsize);
492 h->table = ip_set_alloc(
493 sizeof(struct htable)
494 + jhash_size(hbits) * sizeof(struct hbucket));
499 h->table->htable_bits = hbits;
503 if (tb[IPSET_ATTR_TIMEOUT]) {
504 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
506 set->variant = set->family == AF_INET
507 ? &hash_netport4_tvariant : &hash_netport6_tvariant;
509 if (set->family == AF_INET)
510 hash_netport4_gc_init(set);
512 hash_netport6_gc_init(set);
514 set->variant = set->family == AF_INET
515 ? &hash_netport4_variant : &hash_netport6_variant;
518 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
519 set->name, jhash_size(h->table->htable_bits),
520 h->table->htable_bits, h->maxelem, set->data, h->table);
525 static struct ip_set_type hash_netport_type __read_mostly = {
526 .name = "hash:net,port",
527 .protocol = IPSET_PROTOCOL,
528 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT,
529 .dimension = IPSET_DIM_TWO,
532 .create = hash_netport_create,
534 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
535 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
536 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
537 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
538 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
539 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
542 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
543 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
544 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
545 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
546 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
547 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
548 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
554 hash_netport_init(void)
556 return ip_set_type_register(&hash_netport_type);
560 hash_netport_fini(void)
562 ip_set_type_unregister(&hash_netport_type);
565 module_init(hash_netport_init);
566 module_exit(hash_netport_fini);