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 /* 1 Forceadd support added */
28 #define IPSET_TYPE_REV_MAX 2 /* skbinfo support added */
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>");
32 IP_SET_MODULE_DESC("hash:net,net", IPSET_TYPE_REV_MIN
, IPSET_TYPE_REV_MAX
);
33 MODULE_ALIAS("ip_set_hash:net,net");
35 /* Type specific function prefix */
36 #define HTYPE hash_netnet
37 #define IP_SET_HASH_WITH_NETS
38 #define IPSET_NET_COUNT 2
43 struct hash_netnet4_elem
{
56 /* Common functions */
59 hash_netnet4_data_equal(const struct hash_netnet4_elem
*ip1
,
60 const struct hash_netnet4_elem
*ip2
,
63 return ip1
->ipcmp
== ip2
->ipcmp
&&
64 ip1
->ccmp
== ip2
->ccmp
;
68 hash_netnet4_do_data_match(const struct hash_netnet4_elem
*elem
)
70 return elem
->nomatch
? -ENOTEMPTY
: 1;
74 hash_netnet4_data_set_flags(struct hash_netnet4_elem
*elem
, u32 flags
)
76 elem
->nomatch
= (flags
>> 16) & IPSET_FLAG_NOMATCH
;
80 hash_netnet4_data_reset_flags(struct hash_netnet4_elem
*elem
, u8
*flags
)
82 swap(*flags
, elem
->nomatch
);
86 hash_netnet4_data_reset_elem(struct hash_netnet4_elem
*elem
,
87 struct hash_netnet4_elem
*orig
)
89 elem
->ip
[1] = orig
->ip
[1];
93 hash_netnet4_data_netmask(struct hash_netnet4_elem
*elem
, u8 cidr
, bool inner
)
96 elem
->ip
[1] &= ip_set_netmask(cidr
);
99 elem
->ip
[0] &= ip_set_netmask(cidr
);
100 elem
->cidr
[0] = cidr
;
105 hash_netnet4_data_list(struct sk_buff
*skb
,
106 const struct hash_netnet4_elem
*data
)
108 u32 flags
= data
->nomatch
? IPSET_FLAG_NOMATCH
: 0;
110 if (nla_put_ipaddr4(skb
, IPSET_ATTR_IP
, data
->ip
[0]) ||
111 nla_put_ipaddr4(skb
, IPSET_ATTR_IP2
, data
->ip
[1]) ||
112 nla_put_u8(skb
, IPSET_ATTR_CIDR
, data
->cidr
[0]) ||
113 nla_put_u8(skb
, IPSET_ATTR_CIDR2
, data
->cidr
[1]) ||
115 nla_put_net32(skb
, IPSET_ATTR_CADT_FLAGS
, htonl(flags
))))
116 goto nla_put_failure
;
124 hash_netnet4_data_next(struct hash_netnet4_elem
*next
,
125 const struct hash_netnet4_elem
*d
)
127 next
->ipcmp
= d
->ipcmp
;
130 #define MTYPE hash_netnet4
132 #include "ip_set_hash_gen.h"
135 hash_netnet4_init(struct hash_netnet4_elem
*e
)
137 e
->cidr
[0] = HOST_MASK
;
138 e
->cidr
[1] = HOST_MASK
;
142 hash_netnet4_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
143 const struct xt_action_param
*par
,
144 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
146 const struct hash_netnet4
*h
= set
->data
;
147 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
148 struct hash_netnet4_elem e
= { };
149 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
151 e
.cidr
[0] = INIT_CIDR(h
->nets
[0].cidr
[0], HOST_MASK
);
152 e
.cidr
[1] = INIT_CIDR(h
->nets
[0].cidr
[1], HOST_MASK
);
153 if (adt
== IPSET_TEST
)
154 e
.ccmp
= (HOST_MASK
<< (sizeof(e
.cidr
[0]) * 8)) | HOST_MASK
;
156 ip4addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
[0]);
157 ip4addrptr(skb
, opt
->flags
& IPSET_DIM_TWO_SRC
, &e
.ip
[1]);
158 e
.ip
[0] &= ip_set_netmask(e
.cidr
[0]);
159 e
.ip
[1] &= ip_set_netmask(e
.cidr
[1]);
161 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
165 hash_netnet4_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
166 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
168 const struct hash_netnet4
*h
= set
->data
;
169 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
170 struct hash_netnet4_elem e
= { };
171 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
172 u32 ip
= 0, ip_to
= 0, last
;
173 u32 ip2
= 0, ip2_from
= 0, ip2_to
= 0, last2
;
176 if (tb
[IPSET_ATTR_LINENO
])
177 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
179 hash_netnet4_init(&e
);
180 if (unlikely(!tb
[IPSET_ATTR_IP
] || !tb
[IPSET_ATTR_IP2
] ||
181 !ip_set_optattr_netorder(tb
, IPSET_ATTR_CADT_FLAGS
)))
182 return -IPSET_ERR_PROTOCOL
;
184 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP
], &ip
);
188 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP2
], &ip2_from
);
192 ret
= ip_set_get_extensions(set
, tb
, &ext
);
196 if (tb
[IPSET_ATTR_CIDR
]) {
197 e
.cidr
[0] = nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
198 if (!e
.cidr
[0] || e
.cidr
[0] > HOST_MASK
)
199 return -IPSET_ERR_INVALID_CIDR
;
202 if (tb
[IPSET_ATTR_CIDR2
]) {
203 e
.cidr
[1] = nla_get_u8(tb
[IPSET_ATTR_CIDR2
]);
204 if (!e
.cidr
[1] || e
.cidr
[1] > HOST_MASK
)
205 return -IPSET_ERR_INVALID_CIDR
;
208 if (tb
[IPSET_ATTR_CADT_FLAGS
]) {
209 u32 cadt_flags
= ip_set_get_h32(tb
[IPSET_ATTR_CADT_FLAGS
]);
211 if (cadt_flags
& IPSET_FLAG_NOMATCH
)
212 flags
|= (IPSET_FLAG_NOMATCH
<< 16);
215 if (adt
== IPSET_TEST
|| !(tb
[IPSET_ATTR_IP_TO
] ||
216 tb
[IPSET_ATTR_IP2_TO
])) {
217 e
.ip
[0] = htonl(ip
& ip_set_hostmask(e
.cidr
[0]));
218 e
.ip
[1] = htonl(ip2_from
& ip_set_hostmask(e
.cidr
[1]));
219 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
220 return ip_set_enomatch(ret
, flags
, adt
, set
) ? -ret
:
221 ip_set_eexist(ret
, flags
) ? 0 : ret
;
225 if (tb
[IPSET_ATTR_IP_TO
]) {
226 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP_TO
], &ip_to
);
231 if (unlikely(ip
+ UINT_MAX
== ip_to
))
232 return -IPSET_ERR_HASH_RANGE
;
234 ip_set_mask_from_to(ip
, ip_to
, e
.cidr
[0]);
238 if (tb
[IPSET_ATTR_IP2_TO
]) {
239 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP2_TO
], &ip2_to
);
242 if (ip2_to
< ip2_from
)
243 swap(ip2_from
, ip2_to
);
244 if (unlikely(ip2_from
+ UINT_MAX
== ip2_to
))
245 return -IPSET_ERR_HASH_RANGE
;
247 ip_set_mask_from_to(ip2_from
, ip2_to
, e
.cidr
[1]);
251 ip
= ntohl(h
->next
.ip
[0]);
253 while (!after(ip
, ip_to
)) {
255 last
= ip_set_range_to_cidr(ip
, ip_to
, &e
.cidr
[0]);
257 ip
== ntohl(h
->next
.ip
[0])) ? ntohl(h
->next
.ip
[1])
259 while (!after(ip2
, ip2_to
)) {
260 e
.ip
[1] = htonl(ip2
);
261 last2
= ip_set_range_to_cidr(ip2
, ip2_to
, &e
.cidr
[1]);
262 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
263 if (ret
&& !ip_set_eexist(ret
, flags
))
276 struct hash_netnet6_elem
{
277 union nf_inet_addr ip
[2];
286 /* Common functions */
289 hash_netnet6_data_equal(const struct hash_netnet6_elem
*ip1
,
290 const struct hash_netnet6_elem
*ip2
,
293 return ipv6_addr_equal(&ip1
->ip
[0].in6
, &ip2
->ip
[0].in6
) &&
294 ipv6_addr_equal(&ip1
->ip
[1].in6
, &ip2
->ip
[1].in6
) &&
295 ip1
->ccmp
== ip2
->ccmp
;
299 hash_netnet6_do_data_match(const struct hash_netnet6_elem
*elem
)
301 return elem
->nomatch
? -ENOTEMPTY
: 1;
305 hash_netnet6_data_set_flags(struct hash_netnet6_elem
*elem
, u32 flags
)
307 elem
->nomatch
= (flags
>> 16) & IPSET_FLAG_NOMATCH
;
311 hash_netnet6_data_reset_flags(struct hash_netnet6_elem
*elem
, u8
*flags
)
313 swap(*flags
, elem
->nomatch
);
317 hash_netnet6_data_reset_elem(struct hash_netnet6_elem
*elem
,
318 struct hash_netnet6_elem
*orig
)
320 elem
->ip
[1] = orig
->ip
[1];
324 hash_netnet6_data_netmask(struct hash_netnet6_elem
*elem
, u8 cidr
, bool inner
)
327 ip6_netmask(&elem
->ip
[1], cidr
);
328 elem
->cidr
[1] = cidr
;
330 ip6_netmask(&elem
->ip
[0], cidr
);
331 elem
->cidr
[0] = cidr
;
336 hash_netnet6_data_list(struct sk_buff
*skb
,
337 const struct hash_netnet6_elem
*data
)
339 u32 flags
= data
->nomatch
? IPSET_FLAG_NOMATCH
: 0;
341 if (nla_put_ipaddr6(skb
, IPSET_ATTR_IP
, &data
->ip
[0].in6
) ||
342 nla_put_ipaddr6(skb
, IPSET_ATTR_IP2
, &data
->ip
[1].in6
) ||
343 nla_put_u8(skb
, IPSET_ATTR_CIDR
, data
->cidr
[0]) ||
344 nla_put_u8(skb
, IPSET_ATTR_CIDR2
, data
->cidr
[1]) ||
346 nla_put_net32(skb
, IPSET_ATTR_CADT_FLAGS
, htonl(flags
))))
347 goto nla_put_failure
;
355 hash_netnet6_data_next(struct hash_netnet6_elem
*next
,
356 const struct hash_netnet6_elem
*d
)
363 #define MTYPE hash_netnet6
364 #define HOST_MASK 128
365 #define IP_SET_EMIT_CREATE
366 #include "ip_set_hash_gen.h"
369 hash_netnet6_init(struct hash_netnet6_elem
*e
)
371 e
->cidr
[0] = HOST_MASK
;
372 e
->cidr
[1] = HOST_MASK
;
376 hash_netnet6_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
377 const struct xt_action_param
*par
,
378 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
380 const struct hash_netnet6
*h
= set
->data
;
381 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
382 struct hash_netnet6_elem e
= { };
383 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
385 e
.cidr
[0] = INIT_CIDR(h
->nets
[0].cidr
[0], HOST_MASK
);
386 e
.cidr
[1] = INIT_CIDR(h
->nets
[0].cidr
[1], HOST_MASK
);
387 if (adt
== IPSET_TEST
)
388 e
.ccmp
= (HOST_MASK
<< (sizeof(u8
) * 8)) | HOST_MASK
;
390 ip6addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
[0].in6
);
391 ip6addrptr(skb
, opt
->flags
& IPSET_DIM_TWO_SRC
, &e
.ip
[1].in6
);
392 ip6_netmask(&e
.ip
[0], e
.cidr
[0]);
393 ip6_netmask(&e
.ip
[1], e
.cidr
[1]);
395 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
399 hash_netnet6_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
400 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
402 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
403 struct hash_netnet6_elem e
= { };
404 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
407 if (tb
[IPSET_ATTR_LINENO
])
408 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
410 hash_netnet6_init(&e
);
411 if (unlikely(!tb
[IPSET_ATTR_IP
] || !tb
[IPSET_ATTR_IP2
] ||
412 !ip_set_optattr_netorder(tb
, IPSET_ATTR_CADT_FLAGS
)))
413 return -IPSET_ERR_PROTOCOL
;
414 if (unlikely(tb
[IPSET_ATTR_IP_TO
] || tb
[IPSET_ATTR_IP2_TO
]))
415 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED
;
417 ret
= ip_set_get_ipaddr6(tb
[IPSET_ATTR_IP
], &e
.ip
[0]);
421 ret
= ip_set_get_ipaddr6(tb
[IPSET_ATTR_IP2
], &e
.ip
[1]);
425 ret
= ip_set_get_extensions(set
, tb
, &ext
);
429 if (tb
[IPSET_ATTR_CIDR
]) {
430 e
.cidr
[0] = nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
431 if (!e
.cidr
[0] || e
.cidr
[0] > HOST_MASK
)
432 return -IPSET_ERR_INVALID_CIDR
;
435 if (tb
[IPSET_ATTR_CIDR2
]) {
436 e
.cidr
[1] = nla_get_u8(tb
[IPSET_ATTR_CIDR2
]);
437 if (!e
.cidr
[1] || e
.cidr
[1] > HOST_MASK
)
438 return -IPSET_ERR_INVALID_CIDR
;
441 ip6_netmask(&e
.ip
[0], e
.cidr
[0]);
442 ip6_netmask(&e
.ip
[1], e
.cidr
[1]);
444 if (tb
[IPSET_ATTR_CADT_FLAGS
]) {
445 u32 cadt_flags
= ip_set_get_h32(tb
[IPSET_ATTR_CADT_FLAGS
]);
447 if (cadt_flags
& IPSET_FLAG_NOMATCH
)
448 flags
|= (IPSET_FLAG_NOMATCH
<< 16);
451 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
453 return ip_set_enomatch(ret
, flags
, adt
, set
) ? -ret
:
454 ip_set_eexist(ret
, flags
) ? 0 : ret
;
457 static struct ip_set_type hash_netnet_type __read_mostly
= {
458 .name
= "hash:net,net",
459 .protocol
= IPSET_PROTOCOL
,
460 .features
= IPSET_TYPE_IP
| IPSET_TYPE_IP2
| IPSET_TYPE_NOMATCH
,
461 .dimension
= IPSET_DIM_TWO
,
462 .family
= NFPROTO_UNSPEC
,
463 .revision_min
= IPSET_TYPE_REV_MIN
,
464 .revision_max
= IPSET_TYPE_REV_MAX
,
465 .create
= hash_netnet_create
,
467 [IPSET_ATTR_HASHSIZE
] = { .type
= NLA_U32
},
468 [IPSET_ATTR_MAXELEM
] = { .type
= NLA_U32
},
469 [IPSET_ATTR_PROBES
] = { .type
= NLA_U8
},
470 [IPSET_ATTR_RESIZE
] = { .type
= NLA_U8
},
471 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
472 [IPSET_ATTR_CADT_FLAGS
] = { .type
= NLA_U32
},
475 [IPSET_ATTR_IP
] = { .type
= NLA_NESTED
},
476 [IPSET_ATTR_IP_TO
] = { .type
= NLA_NESTED
},
477 [IPSET_ATTR_IP2
] = { .type
= NLA_NESTED
},
478 [IPSET_ATTR_IP2_TO
] = { .type
= NLA_NESTED
},
479 [IPSET_ATTR_CIDR
] = { .type
= NLA_U8
},
480 [IPSET_ATTR_CIDR2
] = { .type
= NLA_U8
},
481 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
482 [IPSET_ATTR_CADT_FLAGS
] = { .type
= NLA_U32
},
483 [IPSET_ATTR_BYTES
] = { .type
= NLA_U64
},
484 [IPSET_ATTR_PACKETS
] = { .type
= NLA_U64
},
485 [IPSET_ATTR_COMMENT
] = { .type
= NLA_NUL_STRING
,
486 .len
= IPSET_MAX_COMMENT_SIZE
},
487 [IPSET_ATTR_SKBMARK
] = { .type
= NLA_U64
},
488 [IPSET_ATTR_SKBPRIO
] = { .type
= NLA_U32
},
489 [IPSET_ATTR_SKBQUEUE
] = { .type
= NLA_U16
},
495 hash_netnet_init(void)
497 return ip_set_type_register(&hash_netnet_type
);
501 hash_netnet_fini(void)
504 ip_set_type_unregister(&hash_netnet_type
);
507 module_init(hash_netnet_init
);
508 module_exit(hash_netnet_fini
);