1 /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
2 * Copyright (C) 2013 Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 /* Kernel module implementing an IP set type: the hash:net type */
11 #include <linux/jhash.h>
12 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/errno.h>
16 #include <linux/random.h>
19 #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_hash.h>
26 #define IPSET_TYPE_REV_MIN 0
27 #define IPSET_TYPE_REV_MAX 0
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>");
31 IP_SET_MODULE_DESC("hash:net,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
32 MODULE_ALIAS("ip_set_hash:net,net");
34 /* Type specific function prefix */
35 #define HTYPE hash_netnet
36 #define IP_SET_HASH_WITH_NETS
37 #define IPSET_NET_COUNT 2
42 struct hash_netnet4_elem {
54 /* Common functions */
57 hash_netnet4_data_equal(const struct hash_netnet4_elem *ip1,
58 const struct hash_netnet4_elem *ip2,
61 return ip1->ipcmp == ip2->ipcmp &&
62 ip1->ccmp == ip2->ccmp;
66 hash_netnet4_do_data_match(const struct hash_netnet4_elem *elem)
68 return elem->nomatch ? -ENOTEMPTY : 1;
72 hash_netnet4_data_set_flags(struct hash_netnet4_elem *elem, u32 flags)
74 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
78 hash_netnet4_data_reset_flags(struct hash_netnet4_elem *elem, u8 *flags)
80 swap(*flags, elem->nomatch);
84 hash_netnet4_data_reset_elem(struct hash_netnet4_elem *elem,
85 struct hash_netnet4_elem *orig)
87 elem->ip[1] = orig->ip[1];
91 hash_netnet4_data_netmask(struct hash_netnet4_elem *elem, u8 cidr, bool inner)
94 elem->ip[1] &= ip_set_netmask(cidr);
97 elem->ip[0] &= ip_set_netmask(cidr);
103 hash_netnet4_data_list(struct sk_buff *skb,
104 const struct hash_netnet4_elem *data)
106 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
108 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip[0]) ||
109 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip[1]) ||
110 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
111 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
113 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
114 goto nla_put_failure;
122 hash_netnet4_data_next(struct hash_netnet4_elem *next,
123 const struct hash_netnet4_elem *d)
125 next->ipcmp = d->ipcmp;
128 #define MTYPE hash_netnet4
131 #include "ip_set_hash_gen.h"
134 hash_netnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
135 const struct xt_action_param *par,
136 enum ipset_adt adt, struct ip_set_adt_opt *opt)
138 const struct hash_netnet *h = set->data;
139 ipset_adtfn adtfn = set->variant->adt[adt];
140 struct hash_netnet4_elem e = { };
141 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
143 e.cidr[0] = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
144 e.cidr[1] = IP_SET_INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
145 if (adt == IPSET_TEST)
146 e.ccmp = (HOST_MASK << (sizeof(e.cidr[0]) * 8)) | HOST_MASK;
148 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0]);
149 ip4addrptr(skb, opt->flags & IPSET_DIM_TWO_SRC, &e.ip[1]);
150 e.ip[0] &= ip_set_netmask(e.cidr[0]);
151 e.ip[1] &= ip_set_netmask(e.cidr[1]);
153 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
157 hash_netnet4_uadt(struct ip_set *set, struct nlattr *tb[],
158 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
160 const struct hash_netnet *h = set->data;
161 ipset_adtfn adtfn = set->variant->adt[adt];
162 struct hash_netnet4_elem e = { };
163 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
164 u32 ip = 0, ip_to = 0, last;
165 u32 ip2 = 0, ip2_from = 0, ip2_to = 0, last2;
169 e.cidr[0] = e.cidr[1] = HOST_MASK;
170 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
171 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
172 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
173 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
174 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
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_hostipaddr4(tb[IPSET_ATTR_IP], &ip) ||
181 ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from) ||
182 ip_set_get_extensions(set, tb, &ext);
186 if (tb[IPSET_ATTR_CIDR]) {
187 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
188 if (!cidr || cidr > HOST_MASK)
189 return -IPSET_ERR_INVALID_CIDR;
193 if (tb[IPSET_ATTR_CIDR2]) {
194 cidr2 = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
195 if (!cidr2 || cidr2 > HOST_MASK)
196 return -IPSET_ERR_INVALID_CIDR;
200 if (tb[IPSET_ATTR_CADT_FLAGS]) {
201 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
202 if (cadt_flags & IPSET_FLAG_NOMATCH)
203 flags |= (IPSET_FLAG_NOMATCH << 16);
206 if (adt == IPSET_TEST || !(tb[IPSET_ATTR_IP_TO] &&
207 tb[IPSET_ATTR_IP2_TO])) {
208 e.ip[0] = htonl(ip & ip_set_hostmask(e.cidr[0]));
209 e.ip[1] = htonl(ip2_from & ip_set_hostmask(e.cidr[1]));
210 ret = adtfn(set, &e, &ext, &ext, flags);
211 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
212 ip_set_eexist(ret, flags) ? 0 : ret;
216 if (tb[IPSET_ATTR_IP_TO]) {
217 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
222 if (ip + UINT_MAX == ip_to)
223 return -IPSET_ERR_HASH_RANGE;
227 if (tb[IPSET_ATTR_IP2_TO]) {
228 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to);
231 if (ip2_to < ip2_from)
232 swap(ip2_from, ip2_to);
233 if (ip2_from + UINT_MAX == ip2_to)
234 return -IPSET_ERR_HASH_RANGE;
239 ip = ntohl(h->next.ip[0]);
241 while (!after(ip, ip_to)) {
243 last = ip_set_range_to_cidr(ip, ip_to, &cidr);
246 ip == ntohl(h->next.ip[0])) ? ntohl(h->next.ip[1])
248 while (!after(ip2, ip2_to)) {
249 e.ip[1] = htonl(ip2);
250 last2 = ip_set_range_to_cidr(ip2, ip2_to, &cidr2);
252 ret = adtfn(set, &e, &ext, &ext, flags);
253 if (ret && !ip_set_eexist(ret, flags))
266 struct hash_netnet6_elem {
267 union nf_inet_addr ip[2];
275 /* Common functions */
278 hash_netnet6_data_equal(const struct hash_netnet6_elem *ip1,
279 const struct hash_netnet6_elem *ip2,
282 return ipv6_addr_equal(&ip1->ip[0].in6, &ip2->ip[0].in6) &&
283 ipv6_addr_equal(&ip1->ip[1].in6, &ip2->ip[1].in6) &&
284 ip1->ccmp == ip2->ccmp;
288 hash_netnet6_do_data_match(const struct hash_netnet6_elem *elem)
290 return elem->nomatch ? -ENOTEMPTY : 1;
294 hash_netnet6_data_set_flags(struct hash_netnet6_elem *elem, u32 flags)
296 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
300 hash_netnet6_data_reset_flags(struct hash_netnet6_elem *elem, u8 *flags)
302 swap(*flags, elem->nomatch);
306 hash_netnet6_data_reset_elem(struct hash_netnet6_elem *elem,
307 struct hash_netnet6_elem *orig)
309 elem->ip[1] = orig->ip[1];
313 hash_netnet6_data_netmask(struct hash_netnet6_elem *elem, u8 cidr, bool inner)
316 ip6_netmask(&elem->ip[1], cidr);
317 elem->cidr[1] = cidr;
319 ip6_netmask(&elem->ip[0], cidr);
320 elem->cidr[0] = cidr;
325 hash_netnet6_data_list(struct sk_buff *skb,
326 const struct hash_netnet6_elem *data)
328 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
330 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip[0].in6) ||
331 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip[1].in6) ||
332 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
333 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
335 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
336 goto nla_put_failure;
344 hash_netnet6_data_next(struct hash_netnet4_elem *next,
345 const struct hash_netnet6_elem *d)
353 #define MTYPE hash_netnet6
355 #define HOST_MASK 128
356 #define IP_SET_EMIT_CREATE
357 #include "ip_set_hash_gen.h"
360 hash_netnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
361 const struct xt_action_param *par,
362 enum ipset_adt adt, struct ip_set_adt_opt *opt)
364 const struct hash_netnet *h = set->data;
365 ipset_adtfn adtfn = set->variant->adt[adt];
366 struct hash_netnet6_elem e = { };
367 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
369 e.cidr[0] = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
370 e.cidr[1] = IP_SET_INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
371 if (adt == IPSET_TEST)
372 e.ccmp = (HOST_MASK << (sizeof(u8)*8)) | HOST_MASK;
374 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0].in6);
375 ip6addrptr(skb, opt->flags & IPSET_DIM_TWO_SRC, &e.ip[1].in6);
376 ip6_netmask(&e.ip[0], e.cidr[0]);
377 ip6_netmask(&e.ip[1], e.cidr[1]);
379 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
383 hash_netnet6_uadt(struct ip_set *set, struct nlattr *tb[],
384 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
386 ipset_adtfn adtfn = set->variant->adt[adt];
387 struct hash_netnet6_elem e = { };
388 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
391 e.cidr[0] = e.cidr[1] = HOST_MASK;
392 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
393 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
394 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
395 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
396 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
397 return -IPSET_ERR_PROTOCOL;
398 if (unlikely(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_IP2_TO]))
399 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
401 if (tb[IPSET_ATTR_LINENO])
402 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
404 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip[0]) ||
405 ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip[1]) ||
406 ip_set_get_extensions(set, tb, &ext);
410 if (tb[IPSET_ATTR_CIDR])
411 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
413 if (tb[IPSET_ATTR_CIDR2])
414 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
416 if (!e.cidr[0] || e.cidr[0] > HOST_MASK || !e.cidr[1] ||
417 e.cidr[1] > HOST_MASK)
418 return -IPSET_ERR_INVALID_CIDR;
420 ip6_netmask(&e.ip[0], e.cidr[0]);
421 ip6_netmask(&e.ip[1], e.cidr[1]);
423 if (tb[IPSET_ATTR_CADT_FLAGS]) {
424 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
425 if (cadt_flags & IPSET_FLAG_NOMATCH)
426 flags |= (IPSET_FLAG_NOMATCH << 16);
429 ret = adtfn(set, &e, &ext, &ext, flags);
431 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
432 ip_set_eexist(ret, flags) ? 0 : ret;
435 static struct ip_set_type hash_netnet_type __read_mostly = {
436 .name = "hash:net,net",
437 .protocol = IPSET_PROTOCOL,
438 .features = IPSET_TYPE_IP | IPSET_TYPE_IP2 | IPSET_TYPE_NOMATCH,
439 .dimension = IPSET_DIM_TWO,
440 .family = NFPROTO_UNSPEC,
441 .revision_min = IPSET_TYPE_REV_MIN,
442 .revision_max = IPSET_TYPE_REV_MAX,
443 .create = hash_netnet_create,
445 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
446 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
447 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
448 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
449 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
450 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
453 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
454 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
455 [IPSET_ATTR_IP2] = { .type = NLA_NESTED },
456 [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED },
457 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
458 [IPSET_ATTR_CIDR2] = { .type = NLA_U8 },
459 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
460 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
461 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
462 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
463 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
469 hash_netnet_init(void)
471 return ip_set_type_register(&hash_netnet_type);
475 hash_netnet_fini(void)
477 ip_set_type_unregister(&hash_netnet_type);
480 module_init(hash_netnet_init);
481 module_exit(hash_netnet_fini);