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/uaccess.h>
16 #include <linux/bitops.h>
17 #include <linux/spinlock.h>
18 #include <linux/random.h>
21 #include <net/netlink.h>
24 #include <linux/netfilter.h>
25 #include <linux/netfilter/ipset/pfxlen.h>
26 #include <linux/netfilter/ipset/ip_set.h>
27 #include <linux/netfilter/ipset/ip_set_timeout.h>
28 #include <linux/netfilter/ipset/ip_set_getport.h>
29 #include <linux/netfilter/ipset/ip_set_hash.h>
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
33 MODULE_DESCRIPTION("hash:ip,port type of IP sets");
34 MODULE_ALIAS("ip_set_hash:ip,port");
36 /* Type specific function prefix */
37 #define TYPE hash_ipport
40 hash_ipport_same_set(const struct ip_set *a, const struct ip_set *b);
42 #define hash_ipport4_same_set hash_ipport_same_set
43 #define hash_ipport6_same_set hash_ipport_same_set
45 /* The type variant functions: IPv4 */
47 /* Member elements without timeout */
48 struct hash_ipport4_elem {
55 /* Member elements with timeout support */
56 struct hash_ipport4_telem {
61 unsigned long timeout;
65 hash_ipport4_data_equal(const struct hash_ipport4_elem *ip1,
66 const struct hash_ipport4_elem *ip2)
68 return ip1->ip == ip2->ip &&
69 ip1->port == ip2->port &&
70 ip1->proto == ip2->proto;
74 hash_ipport4_data_isnull(const struct hash_ipport4_elem *elem)
76 return elem->proto == 0;
80 hash_ipport4_data_copy(struct hash_ipport4_elem *dst,
81 const struct hash_ipport4_elem *src)
84 dst->port = src->port;
85 dst->proto = src->proto;
89 hash_ipport4_data_zero_out(struct hash_ipport4_elem *elem)
95 hash_ipport4_data_list(struct sk_buff *skb,
96 const struct hash_ipport4_elem *data)
98 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, data->ip);
99 NLA_PUT_NET16(skb, IPSET_ATTR_PORT, data->port);
100 NLA_PUT_U8(skb, IPSET_ATTR_PROTO, data->proto);
108 hash_ipport4_data_tlist(struct sk_buff *skb,
109 const struct hash_ipport4_elem *data)
111 const struct hash_ipport4_telem *tdata =
112 (const struct hash_ipport4_telem *)data;
114 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, tdata->ip);
115 NLA_PUT_NET16(skb, IPSET_ATTR_PORT, tdata->port);
116 NLA_PUT_U8(skb, IPSET_ATTR_PROTO, data->proto);
117 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
118 htonl(ip_set_timeout_get(tdata->timeout)));
128 #include <linux/netfilter/ipset/ip_set_ahash.h>
131 hash_ipport4_kadt(struct ip_set *set, const struct sk_buff *skb,
132 enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
134 const struct ip_set_hash *h = set->data;
135 ipset_adtfn adtfn = set->variant->adt[adt];
136 struct hash_ipport4_elem data = { };
138 if (!ip_set_get_ip4_port(skb, flags & IPSET_DIM_TWO_SRC,
139 &data.port, &data.proto))
142 ip4addrptr(skb, flags & IPSET_DIM_ONE_SRC, &data.ip);
144 return adtfn(set, &data, h->timeout);
148 hash_ipport4_uadt(struct ip_set *set, struct nlattr *tb[],
149 enum ipset_adt adt, u32 *lineno, u32 flags)
151 const struct ip_set_hash *h = set->data;
152 ipset_adtfn adtfn = set->variant->adt[adt];
153 struct hash_ipport4_elem data = { };
154 u32 ip, ip_to, p, port, port_to;
155 u32 timeout = h->timeout;
158 if (unlikely(!tb[IPSET_ATTR_IP] ||
159 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
160 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
161 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
162 return -IPSET_ERR_PROTOCOL;
164 if (tb[IPSET_ATTR_LINENO])
165 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
167 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &data.ip);
171 if (tb[IPSET_ATTR_PORT])
172 data.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
174 return -IPSET_ERR_PROTOCOL;
176 if (tb[IPSET_ATTR_PROTO]) {
177 data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
180 return -IPSET_ERR_INVALID_PROTO;
182 return -IPSET_ERR_MISSING_PROTO;
184 switch (data.proto) {
194 if (tb[IPSET_ATTR_TIMEOUT]) {
195 if (!with_timeout(h->timeout))
196 return -IPSET_ERR_TIMEOUT;
197 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
200 if (adt == IPSET_TEST ||
201 !(data.proto == IPPROTO_TCP || data.proto == IPPROTO_UDP) ||
202 !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR] ||
203 tb[IPSET_ATTR_PORT_TO])) {
204 ret = adtfn(set, &data, timeout);
205 return ip_set_eexist(ret, flags) ? 0 : ret;
209 if (tb[IPSET_ATTR_IP_TO]) {
210 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
215 } else if (tb[IPSET_ATTR_CIDR]) {
216 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
219 return -IPSET_ERR_INVALID_CIDR;
220 ip &= ip_set_hostmask(cidr);
221 ip_to = ip | ~ip_set_hostmask(cidr);
225 port = ntohs(data.port);
226 if (tb[IPSET_ATTR_PORT_TO]) {
227 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
233 for (; !before(ip_to, ip); ip++)
234 for (p = port; p <= port_to; p++) {
236 data.port = htons(p);
237 ret = adtfn(set, &data, timeout);
239 if (ret && !ip_set_eexist(ret, flags))
248 hash_ipport_same_set(const struct ip_set *a, const struct ip_set *b)
250 const struct ip_set_hash *x = a->data;
251 const struct ip_set_hash *y = b->data;
253 /* Resizing changes htable_bits, so we ignore it */
254 return x->maxelem == y->maxelem &&
255 x->timeout == y->timeout;
258 /* The type variant functions: IPv6 */
260 struct hash_ipport6_elem {
261 union nf_inet_addr ip;
267 struct hash_ipport6_telem {
268 union nf_inet_addr ip;
272 unsigned long timeout;
276 hash_ipport6_data_equal(const struct hash_ipport6_elem *ip1,
277 const struct hash_ipport6_elem *ip2)
279 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
280 ip1->port == ip2->port &&
281 ip1->proto == ip2->proto;
285 hash_ipport6_data_isnull(const struct hash_ipport6_elem *elem)
287 return elem->proto == 0;
291 hash_ipport6_data_copy(struct hash_ipport6_elem *dst,
292 const struct hash_ipport6_elem *src)
294 memcpy(dst, src, sizeof(*dst));
298 hash_ipport6_data_zero_out(struct hash_ipport6_elem *elem)
304 hash_ipport6_data_list(struct sk_buff *skb,
305 const struct hash_ipport6_elem *data)
307 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &data->ip);
308 NLA_PUT_NET16(skb, IPSET_ATTR_PORT, data->port);
309 NLA_PUT_U8(skb, IPSET_ATTR_PROTO, data->proto);
317 hash_ipport6_data_tlist(struct sk_buff *skb,
318 const struct hash_ipport6_elem *data)
320 const struct hash_ipport6_telem *e =
321 (const struct hash_ipport6_telem *)data;
323 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &e->ip);
324 NLA_PUT_NET16(skb, IPSET_ATTR_PORT, data->port);
325 NLA_PUT_U8(skb, IPSET_ATTR_PROTO, data->proto);
326 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
327 htonl(ip_set_timeout_get(e->timeout)));
338 #define HOST_MASK 128
339 #include <linux/netfilter/ipset/ip_set_ahash.h>
342 hash_ipport6_kadt(struct ip_set *set, const struct sk_buff *skb,
343 enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
345 const struct ip_set_hash *h = set->data;
346 ipset_adtfn adtfn = set->variant->adt[adt];
347 struct hash_ipport6_elem data = { };
349 if (!ip_set_get_ip6_port(skb, flags & IPSET_DIM_TWO_SRC,
350 &data.port, &data.proto))
353 ip6addrptr(skb, flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
355 return adtfn(set, &data, h->timeout);
359 hash_ipport6_uadt(struct ip_set *set, struct nlattr *tb[],
360 enum ipset_adt adt, u32 *lineno, u32 flags)
362 const struct ip_set_hash *h = set->data;
363 ipset_adtfn adtfn = set->variant->adt[adt];
364 struct hash_ipport6_elem data = { };
366 u32 timeout = h->timeout;
369 if (unlikely(!tb[IPSET_ATTR_IP] ||
370 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
371 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
372 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
373 tb[IPSET_ATTR_IP_TO] ||
374 tb[IPSET_ATTR_CIDR]))
375 return -IPSET_ERR_PROTOCOL;
377 if (tb[IPSET_ATTR_LINENO])
378 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
380 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
384 if (tb[IPSET_ATTR_PORT])
385 data.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
387 return -IPSET_ERR_PROTOCOL;
389 if (tb[IPSET_ATTR_PROTO]) {
390 data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
393 return -IPSET_ERR_INVALID_PROTO;
395 return -IPSET_ERR_MISSING_PROTO;
397 switch (data.proto) {
407 if (tb[IPSET_ATTR_TIMEOUT]) {
408 if (!with_timeout(h->timeout))
409 return -IPSET_ERR_TIMEOUT;
410 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
413 if (adt == IPSET_TEST ||
414 !(data.proto == IPPROTO_TCP || data.proto == IPPROTO_UDP) ||
415 !tb[IPSET_ATTR_PORT_TO]) {
416 ret = adtfn(set, &data, timeout);
417 return ip_set_eexist(ret, flags) ? 0 : ret;
420 port = ntohs(data.port);
421 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
425 for (; port <= port_to; port++) {
426 data.port = htons(port);
427 ret = adtfn(set, &data, timeout);
429 if (ret && !ip_set_eexist(ret, flags))
437 /* Create hash:ip type of sets */
440 hash_ipport_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
442 struct ip_set_hash *h;
443 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
446 if (!(set->family == AF_INET || set->family == AF_INET6))
447 return -IPSET_ERR_INVALID_FAMILY;
449 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
450 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
451 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
452 return -IPSET_ERR_PROTOCOL;
454 if (tb[IPSET_ATTR_HASHSIZE]) {
455 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
456 if (hashsize < IPSET_MIMINAL_HASHSIZE)
457 hashsize = IPSET_MIMINAL_HASHSIZE;
460 if (tb[IPSET_ATTR_MAXELEM])
461 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
463 h = kzalloc(sizeof(*h), GFP_KERNEL);
467 h->maxelem = maxelem;
468 get_random_bytes(&h->initval, sizeof(h->initval));
469 h->timeout = IPSET_NO_TIMEOUT;
471 hbits = htable_bits(hashsize);
472 h->table = ip_set_alloc(
473 sizeof(struct htable)
474 + jhash_size(hbits) * sizeof(struct hbucket));
479 h->table->htable_bits = hbits;
483 if (tb[IPSET_ATTR_TIMEOUT]) {
484 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
486 set->variant = set->family == AF_INET
487 ? &hash_ipport4_tvariant : &hash_ipport6_tvariant;
489 if (set->family == AF_INET)
490 hash_ipport4_gc_init(set);
492 hash_ipport6_gc_init(set);
494 set->variant = set->family == AF_INET
495 ? &hash_ipport4_variant : &hash_ipport6_variant;
498 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
499 set->name, jhash_size(h->table->htable_bits),
500 h->table->htable_bits, h->maxelem, set->data, h->table);
505 static struct ip_set_type hash_ipport_type __read_mostly = {
506 .name = "hash:ip,port",
507 .protocol = IPSET_PROTOCOL,
508 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT,
509 .dimension = IPSET_DIM_TWO,
512 .create = hash_ipport_create,
514 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
515 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
516 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
517 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
518 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
519 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
522 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
523 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
524 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
525 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
526 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
527 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
528 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
529 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
535 hash_ipport_init(void)
537 return ip_set_type_register(&hash_ipport_type);
541 hash_ipport_fini(void)
543 ip_set_type_unregister(&hash_ipport_type);
546 module_init(hash_ipport_init);
547 module_exit(hash_ipport_fini);