Commit | Line | Data |
---|---|---|
fe03d474 | 1 | /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org> |
b3837029 JK |
2 | * |
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. | |
6 | */ | |
7 | ||
8 | /* Kernel module implementing an IP set type: the hash:net type */ | |
9 | ||
10 | #include <linux/jhash.h> | |
11 | #include <linux/module.h> | |
12 | #include <linux/ip.h> | |
13 | #include <linux/skbuff.h> | |
14 | #include <linux/errno.h> | |
b3837029 JK |
15 | #include <linux/random.h> |
16 | #include <net/ip.h> | |
17 | #include <net/ipv6.h> | |
18 | #include <net/netlink.h> | |
19 | ||
20 | #include <linux/netfilter.h> | |
21 | #include <linux/netfilter/ipset/pfxlen.h> | |
22 | #include <linux/netfilter/ipset/ip_set.h> | |
b3837029 JK |
23 | #include <linux/netfilter/ipset/ip_set_hash.h> |
24 | ||
35b8dcf8 JK |
25 | #define IPSET_TYPE_REV_MIN 0 |
26 | /* 1 Range as input support for IPv4 added */ | |
27 | /* 2 nomatch flag support added */ | |
fda75c6d | 28 | /* 3 Counters support added */ |
07cf8f5a | 29 | /* 4 Comments support added */ |
af331419 AD |
30 | /* 5 Forceadd support added */ |
31 | #define IPSET_TYPE_REV_MAX 6 /* skbinfo mapping support added */ | |
10111a6e | 32 | |
b3837029 | 33 | MODULE_LICENSE("GPL"); |
fe03d474 | 34 | MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>"); |
35b8dcf8 | 35 | IP_SET_MODULE_DESC("hash:net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX); |
b3837029 JK |
36 | MODULE_ALIAS("ip_set_hash:net"); |
37 | ||
38 | /* Type specific function prefix */ | |
5d50e1d8 JK |
39 | #define HTYPE hash_net |
40 | #define IP_SET_HASH_WITH_NETS | |
b3837029 | 41 | |
03c8b234 | 42 | /* IPv4 variant */ |
b3837029 | 43 | |
5d50e1d8 | 44 | /* Member elements */ |
b3837029 JK |
45 | struct hash_net4_elem { |
46 | __be32 ip; | |
47 | u16 padding0; | |
2a7cef2a | 48 | u8 nomatch; |
b3837029 JK |
49 | u8 cidr; |
50 | }; | |
51 | ||
5d50e1d8 JK |
52 | /* Common functions */ |
53 | ||
b3837029 JK |
54 | static inline bool |
55 | hash_net4_data_equal(const struct hash_net4_elem *ip1, | |
89dc79b7 JK |
56 | const struct hash_net4_elem *ip2, |
57 | u32 *multi) | |
b3837029 | 58 | { |
2a7cef2a JK |
59 | return ip1->ip == ip2->ip && |
60 | ip1->cidr == ip2->cidr; | |
b3837029 JK |
61 | } |
62 | ||
5d50e1d8 JK |
63 | static inline int |
64 | hash_net4_do_data_match(const struct hash_net4_elem *elem) | |
b3837029 | 65 | { |
5d50e1d8 | 66 | return elem->nomatch ? -ENOTEMPTY : 1; |
2a7cef2a JK |
67 | } |
68 | ||
69 | static inline void | |
5d50e1d8 | 70 | hash_net4_data_set_flags(struct hash_net4_elem *elem, u32 flags) |
2a7cef2a | 71 | { |
5d50e1d8 | 72 | elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH; |
6eb4c7e9 JK |
73 | } |
74 | ||
75 | static inline void | |
5d50e1d8 | 76 | hash_net4_data_reset_flags(struct hash_net4_elem *elem, u8 *flags) |
2a7cef2a | 77 | { |
5d50e1d8 | 78 | swap(*flags, elem->nomatch); |
b3837029 JK |
79 | } |
80 | ||
81 | static inline void | |
82 | hash_net4_data_netmask(struct hash_net4_elem *elem, u8 cidr) | |
83 | { | |
84 | elem->ip &= ip_set_netmask(cidr); | |
85 | elem->cidr = cidr; | |
86 | } | |
87 | ||
b3837029 JK |
88 | static bool |
89 | hash_net4_data_list(struct sk_buff *skb, const struct hash_net4_elem *data) | |
90 | { | |
2a7cef2a JK |
91 | u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0; |
92 | ||
7cf7899d DM |
93 | if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) || |
94 | nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) || | |
95 | (flags && | |
96 | nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags)))) | |
97 | goto nla_put_failure; | |
728a7e69 | 98 | return false; |
b3837029 JK |
99 | |
100 | nla_put_failure: | |
728a7e69 | 101 | return true; |
b3837029 JK |
102 | } |
103 | ||
5d50e1d8 JK |
104 | static inline void |
105 | hash_net4_data_next(struct hash_net4_elem *next, | |
106 | const struct hash_net4_elem *d) | |
b3837029 | 107 | { |
5d50e1d8 | 108 | next->ip = d->ip; |
b3837029 JK |
109 | } |
110 | ||
5d50e1d8 | 111 | #define MTYPE hash_net4 |
b3837029 | 112 | #define HOST_MASK 32 |
5d50e1d8 | 113 | #include "ip_set_hash_gen.h" |
3d14b171 | 114 | |
b3837029 JK |
115 | static int |
116 | hash_net4_kadt(struct ip_set *set, const struct sk_buff *skb, | |
b66554cf | 117 | const struct xt_action_param *par, |
5d50e1d8 | 118 | enum ipset_adt adt, struct ip_set_adt_opt *opt) |
b3837029 | 119 | { |
21956ab2 | 120 | const struct hash_net4 *h = set->data; |
b3837029 | 121 | ipset_adtfn adtfn = set->variant->adt[adt]; |
5d50e1d8 | 122 | struct hash_net4_elem e = { |
f690cbae | 123 | .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK), |
9b03a5ef | 124 | }; |
ca134ce8 | 125 | struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set); |
b3837029 | 126 | |
5d50e1d8 | 127 | if (e.cidr == 0) |
b3837029 JK |
128 | return -EINVAL; |
129 | if (adt == IPSET_TEST) | |
5d50e1d8 | 130 | e.cidr = HOST_MASK; |
b3837029 | 131 | |
5d50e1d8 JK |
132 | ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip); |
133 | e.ip &= ip_set_netmask(e.cidr); | |
b3837029 | 134 | |
5d50e1d8 | 135 | return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags); |
b3837029 JK |
136 | } |
137 | ||
138 | static int | |
139 | hash_net4_uadt(struct ip_set *set, struct nlattr *tb[], | |
3d14b171 | 140 | enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) |
b3837029 | 141 | { |
21956ab2 | 142 | const struct hash_net4 *h = set->data; |
b3837029 | 143 | ipset_adtfn adtfn = set->variant->adt[adt]; |
5d50e1d8 | 144 | struct hash_net4_elem e = { .cidr = HOST_MASK }; |
ca134ce8 | 145 | struct ip_set_ext ext = IP_SET_INIT_UEXT(set); |
0b8d9073 | 146 | u32 ip = 0, ip_to = 0; |
b3837029 JK |
147 | int ret; |
148 | ||
a212e08e SP |
149 | if (tb[IPSET_ATTR_LINENO]) |
150 | *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]); | |
151 | ||
b3837029 | 152 | if (unlikely(!tb[IPSET_ATTR_IP] || |
7dd37bc8 | 153 | !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS))) |
b3837029 JK |
154 | return -IPSET_ERR_PROTOCOL; |
155 | ||
8e55d2e5 SP |
156 | ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip); |
157 | if (ret) | |
158 | return ret; | |
159 | ||
160 | ret = ip_set_get_extensions(set, tb, &ext); | |
b3837029 JK |
161 | if (ret) |
162 | return ret; | |
163 | ||
d0d9e0a5 | 164 | if (tb[IPSET_ATTR_CIDR]) { |
5d50e1d8 JK |
165 | e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]); |
166 | if (!e.cidr || e.cidr > HOST_MASK) | |
d0d9e0a5 JK |
167 | return -IPSET_ERR_INVALID_CIDR; |
168 | } | |
b3837029 | 169 | |
43c56e59 | 170 | if (tb[IPSET_ATTR_CADT_FLAGS]) { |
2a7cef2a | 171 | u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]); |
ca0f6a5c | 172 | |
2a7cef2a | 173 | if (cadt_flags & IPSET_FLAG_NOMATCH) |
43c56e59 | 174 | flags |= (IPSET_FLAG_NOMATCH << 16); |
2a7cef2a JK |
175 | } |
176 | ||
d0d9e0a5 | 177 | if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) { |
5d50e1d8 JK |
178 | e.ip = htonl(ip & ip_set_hostmask(e.cidr)); |
179 | ret = adtfn(set, &e, &ext, &ext, flags); | |
ca0f6a5c | 180 | return ip_set_enomatch(ret, flags, adt, set) ? -ret : |
43c56e59 | 181 | ip_set_eexist(ret, flags) ? 0 : ret; |
d0d9e0a5 | 182 | } |
b3837029 | 183 | |
d0d9e0a5 JK |
184 | ip_to = ip; |
185 | if (tb[IPSET_ATTR_IP_TO]) { | |
186 | ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to); | |
187 | if (ret) | |
188 | return ret; | |
189 | if (ip_to < ip) | |
190 | swap(ip, ip_to); | |
191 | if (ip + UINT_MAX == ip_to) | |
192 | return -IPSET_ERR_HASH_RANGE; | |
193 | } | |
194 | if (retried) | |
6e27c9b4 | 195 | ip = ntohl(h->next.ip); |
0b8d9073 | 196 | do { |
5d50e1d8 | 197 | e.ip = htonl(ip); |
0b8d9073 | 198 | ip = ip_set_range_to_cidr(ip, ip_to, &e.cidr); |
5d50e1d8 | 199 | ret = adtfn(set, &e, &ext, &ext, flags); |
d0d9e0a5 JK |
200 | if (ret && !ip_set_eexist(ret, flags)) |
201 | return ret; | |
ca0f6a5c JK |
202 | |
203 | ret = 0; | |
0b8d9073 | 204 | } while (ip++ < ip_to); |
d0d9e0a5 | 205 | return ret; |
b3837029 JK |
206 | } |
207 | ||
03c8b234 | 208 | /* IPv6 variant */ |
b3837029 JK |
209 | |
210 | struct hash_net6_elem { | |
211 | union nf_inet_addr ip; | |
212 | u16 padding0; | |
2a7cef2a | 213 | u8 nomatch; |
b3837029 JK |
214 | u8 cidr; |
215 | }; | |
216 | ||
5d50e1d8 JK |
217 | /* Common functions */ |
218 | ||
b3837029 JK |
219 | static inline bool |
220 | hash_net6_data_equal(const struct hash_net6_elem *ip1, | |
89dc79b7 JK |
221 | const struct hash_net6_elem *ip2, |
222 | u32 *multi) | |
b3837029 | 223 | { |
29e3b160 | 224 | return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) && |
b3837029 JK |
225 | ip1->cidr == ip2->cidr; |
226 | } | |
227 | ||
5d50e1d8 JK |
228 | static inline int |
229 | hash_net6_do_data_match(const struct hash_net6_elem *elem) | |
2a7cef2a | 230 | { |
5d50e1d8 | 231 | return elem->nomatch ? -ENOTEMPTY : 1; |
6eb4c7e9 JK |
232 | } |
233 | ||
234 | static inline void | |
5d50e1d8 | 235 | hash_net6_data_set_flags(struct hash_net6_elem *elem, u32 flags) |
2a7cef2a | 236 | { |
5d50e1d8 | 237 | elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH; |
b3837029 JK |
238 | } |
239 | ||
240 | static inline void | |
5d50e1d8 | 241 | hash_net6_data_reset_flags(struct hash_net6_elem *elem, u8 *flags) |
b3837029 | 242 | { |
5d50e1d8 | 243 | swap(*flags, elem->nomatch); |
b3837029 JK |
244 | } |
245 | ||
b3837029 JK |
246 | static inline void |
247 | hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr) | |
248 | { | |
249 | ip6_netmask(&elem->ip, cidr); | |
250 | elem->cidr = cidr; | |
251 | } | |
252 | ||
253 | static bool | |
254 | hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data) | |
255 | { | |
2a7cef2a JK |
256 | u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0; |
257 | ||
7cf7899d DM |
258 | if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) || |
259 | nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) || | |
260 | (flags && | |
261 | nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags)))) | |
262 | goto nla_put_failure; | |
728a7e69 | 263 | return false; |
b3837029 JK |
264 | |
265 | nla_put_failure: | |
728a7e69 | 266 | return true; |
b3837029 JK |
267 | } |
268 | ||
5d50e1d8 | 269 | static inline void |
21956ab2 | 270 | hash_net6_data_next(struct hash_net6_elem *next, |
5d50e1d8 | 271 | const struct hash_net6_elem *d) |
b3837029 | 272 | { |
b3837029 JK |
273 | } |
274 | ||
5d50e1d8 | 275 | #undef MTYPE |
b3837029 JK |
276 | #undef HOST_MASK |
277 | ||
5d50e1d8 | 278 | #define MTYPE hash_net6 |
b3837029 | 279 | #define HOST_MASK 128 |
5d50e1d8 JK |
280 | #define IP_SET_EMIT_CREATE |
281 | #include "ip_set_hash_gen.h" | |
3d14b171 | 282 | |
b3837029 JK |
283 | static int |
284 | hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb, | |
b66554cf | 285 | const struct xt_action_param *par, |
5d50e1d8 | 286 | enum ipset_adt adt, struct ip_set_adt_opt *opt) |
b3837029 | 287 | { |
21956ab2 | 288 | const struct hash_net6 *h = set->data; |
b3837029 | 289 | ipset_adtfn adtfn = set->variant->adt[adt]; |
5d50e1d8 | 290 | struct hash_net6_elem e = { |
f690cbae | 291 | .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK), |
9b03a5ef | 292 | }; |
ca134ce8 | 293 | struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set); |
b3837029 | 294 | |
5d50e1d8 | 295 | if (e.cidr == 0) |
b3837029 JK |
296 | return -EINVAL; |
297 | if (adt == IPSET_TEST) | |
5d50e1d8 | 298 | e.cidr = HOST_MASK; |
b3837029 | 299 | |
5d50e1d8 JK |
300 | ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6); |
301 | ip6_netmask(&e.ip, e.cidr); | |
b3837029 | 302 | |
5d50e1d8 | 303 | return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags); |
b3837029 JK |
304 | } |
305 | ||
306 | static int | |
307 | hash_net6_uadt(struct ip_set *set, struct nlattr *tb[], | |
3d14b171 | 308 | enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) |
b3837029 | 309 | { |
b3837029 | 310 | ipset_adtfn adtfn = set->variant->adt[adt]; |
5d50e1d8 | 311 | struct hash_net6_elem e = { .cidr = HOST_MASK }; |
ca134ce8 | 312 | struct ip_set_ext ext = IP_SET_INIT_UEXT(set); |
b3837029 JK |
313 | int ret; |
314 | ||
a212e08e SP |
315 | if (tb[IPSET_ATTR_LINENO]) |
316 | *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]); | |
317 | ||
b3837029 | 318 | if (unlikely(!tb[IPSET_ATTR_IP] || |
7dd37bc8 | 319 | !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS))) |
b3837029 | 320 | return -IPSET_ERR_PROTOCOL; |
d0d9e0a5 JK |
321 | if (unlikely(tb[IPSET_ATTR_IP_TO])) |
322 | return -IPSET_ERR_HASH_RANGE_UNSUPPORTED; | |
b3837029 | 323 | |
8e55d2e5 SP |
324 | ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip); |
325 | if (ret) | |
326 | return ret; | |
327 | ||
328 | ret = ip_set_get_extensions(set, tb, &ext); | |
b3837029 JK |
329 | if (ret) |
330 | return ret; | |
331 | ||
aff22758 | 332 | if (tb[IPSET_ATTR_CIDR]) { |
5d50e1d8 | 333 | e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]); |
aff22758 SP |
334 | if (!e.cidr || e.cidr > HOST_MASK) |
335 | return -IPSET_ERR_INVALID_CIDR; | |
336 | } | |
b3837029 | 337 | |
5d50e1d8 | 338 | ip6_netmask(&e.ip, e.cidr); |
b3837029 | 339 | |
43c56e59 | 340 | if (tb[IPSET_ATTR_CADT_FLAGS]) { |
2a7cef2a | 341 | u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]); |
ca0f6a5c | 342 | |
2a7cef2a | 343 | if (cadt_flags & IPSET_FLAG_NOMATCH) |
43c56e59 | 344 | flags |= (IPSET_FLAG_NOMATCH << 16); |
2a7cef2a JK |
345 | } |
346 | ||
5d50e1d8 | 347 | ret = adtfn(set, &e, &ext, &ext, flags); |
b3837029 | 348 | |
0f1799ba | 349 | return ip_set_enomatch(ret, flags, adt, set) ? -ret : |
43c56e59 | 350 | ip_set_eexist(ret, flags) ? 0 : ret; |
b3837029 JK |
351 | } |
352 | ||
b3837029 JK |
353 | static struct ip_set_type hash_net_type __read_mostly = { |
354 | .name = "hash:net", | |
355 | .protocol = IPSET_PROTOCOL, | |
3e0304a5 | 356 | .features = IPSET_TYPE_IP | IPSET_TYPE_NOMATCH, |
b3837029 | 357 | .dimension = IPSET_DIM_ONE, |
c15f1c83 | 358 | .family = NFPROTO_UNSPEC, |
35b8dcf8 JK |
359 | .revision_min = IPSET_TYPE_REV_MIN, |
360 | .revision_max = IPSET_TYPE_REV_MAX, | |
b3837029 JK |
361 | .create = hash_net_create, |
362 | .create_policy = { | |
363 | [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 }, | |
364 | [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 }, | |
365 | [IPSET_ATTR_PROBES] = { .type = NLA_U8 }, | |
366 | [IPSET_ATTR_RESIZE] = { .type = NLA_U8 }, | |
367 | [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, | |
5d50e1d8 | 368 | [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, |
b3837029 JK |
369 | }, |
370 | .adt_policy = { | |
371 | [IPSET_ATTR_IP] = { .type = NLA_NESTED }, | |
d0d9e0a5 | 372 | [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED }, |
b3837029 JK |
373 | [IPSET_ATTR_CIDR] = { .type = NLA_U8 }, |
374 | [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, | |
2a7cef2a | 375 | [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, |
00d71b27 JK |
376 | [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, |
377 | [IPSET_ATTR_PACKETS] = { .type = NLA_U64 }, | |
03726186 SP |
378 | [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING, |
379 | .len = IPSET_MAX_COMMENT_SIZE }, | |
af331419 AD |
380 | [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 }, |
381 | [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 }, | |
382 | [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 }, | |
b3837029 JK |
383 | }, |
384 | .me = THIS_MODULE, | |
385 | }; | |
386 | ||
387 | static int __init | |
388 | hash_net_init(void) | |
389 | { | |
390 | return ip_set_type_register(&hash_net_type); | |
391 | } | |
392 | ||
393 | static void __exit | |
394 | hash_net_fini(void) | |
395 | { | |
18f84d41 | 396 | rcu_barrier(); |
b3837029 JK |
397 | ip_set_type_unregister(&hash_net_type); |
398 | } | |
399 | ||
400 | module_init(hash_net_init); | |
401 | module_exit(hash_net_fini); |