Commit | Line | Data |
---|---|---|
e385357a JK |
1 | /* Copyright (C) 2011 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> |
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,iface 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> | |
15 | #include <linux/random.h> | |
16 | #include <linux/rbtree.h> | |
17 | #include <net/ip.h> | |
18 | #include <net/ipv6.h> | |
19 | #include <net/netlink.h> | |
20 | ||
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_timeout.h> | |
25 | #include <linux/netfilter/ipset/ip_set_hash.h> | |
26 | ||
10111a6e JK |
27 | #define REVISION_MIN 0 |
28 | /* 1 nomatch flag support added */ | |
29 | #define REVISION_MAX 2 /* /0 support added */ | |
30 | ||
e385357a JK |
31 | MODULE_LICENSE("GPL"); |
32 | MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>"); | |
10111a6e | 33 | IP_SET_MODULE_DESC("hash:net,iface", REVISION_MIN, REVISION_MAX); |
e385357a JK |
34 | MODULE_ALIAS("ip_set_hash:net,iface"); |
35 | ||
36 | /* Interface name rbtree */ | |
37 | ||
38 | struct iface_node { | |
39 | struct rb_node node; | |
40 | char iface[IFNAMSIZ]; | |
41 | }; | |
42 | ||
43 | #define iface_data(n) (rb_entry(n, struct iface_node, node)->iface) | |
44 | ||
e385357a JK |
45 | static void |
46 | rbtree_destroy(struct rb_root *root) | |
47 | { | |
48 | struct rb_node *p, *n = root->rb_node; | |
49 | struct iface_node *node; | |
50 | ||
51 | /* Non-recursive destroy, like in ext3 */ | |
52 | while (n) { | |
53 | if (n->rb_left) { | |
54 | n = n->rb_left; | |
55 | continue; | |
56 | } | |
57 | if (n->rb_right) { | |
58 | n = n->rb_right; | |
59 | continue; | |
60 | } | |
61 | p = rb_parent(n); | |
62 | node = rb_entry(n, struct iface_node, node); | |
63 | if (!p) | |
64 | *root = RB_ROOT; | |
65 | else if (p->rb_left == n) | |
66 | p->rb_left = NULL; | |
67 | else if (p->rb_right == n) | |
68 | p->rb_right = NULL; | |
69 | ||
70 | kfree(node); | |
71 | n = p; | |
72 | } | |
73 | } | |
74 | ||
75 | static int | |
76 | iface_test(struct rb_root *root, const char **iface) | |
77 | { | |
78 | struct rb_node *n = root->rb_node; | |
79 | ||
80 | while (n) { | |
81 | const char *d = iface_data(n); | |
ef5b6e12 | 82 | int res = strcmp(*iface, d); |
15b4d93f | 83 | |
e385357a JK |
84 | if (res < 0) |
85 | n = n->rb_left; | |
86 | else if (res > 0) | |
87 | n = n->rb_right; | |
88 | else { | |
89 | *iface = d; | |
90 | return 1; | |
91 | } | |
92 | } | |
93 | return 0; | |
94 | } | |
95 | ||
96 | static int | |
97 | iface_add(struct rb_root *root, const char **iface) | |
98 | { | |
99 | struct rb_node **n = &(root->rb_node), *p = NULL; | |
100 | struct iface_node *d; | |
15b4d93f | 101 | |
e385357a JK |
102 | while (*n) { |
103 | char *ifname = iface_data(*n); | |
ef5b6e12 | 104 | int res = strcmp(*iface, ifname); |
e385357a JK |
105 | |
106 | p = *n; | |
107 | if (res < 0) | |
108 | n = &((*n)->rb_left); | |
109 | else if (res > 0) | |
110 | n = &((*n)->rb_right); | |
111 | else { | |
112 | *iface = ifname; | |
113 | return 0; | |
114 | } | |
115 | } | |
116 | ||
117 | d = kzalloc(sizeof(*d), GFP_ATOMIC); | |
118 | if (!d) | |
119 | return -ENOMEM; | |
120 | strcpy(d->iface, *iface); | |
121 | ||
122 | rb_link_node(&d->node, p, n); | |
123 | rb_insert_color(&d->node, root); | |
124 | ||
125 | *iface = d->iface; | |
126 | return 0; | |
127 | } | |
128 | ||
129 | /* Type specific function prefix */ | |
130 | #define TYPE hash_netiface | |
131 | ||
132 | static bool | |
133 | hash_netiface_same_set(const struct ip_set *a, const struct ip_set *b); | |
134 | ||
135 | #define hash_netiface4_same_set hash_netiface_same_set | |
136 | #define hash_netiface6_same_set hash_netiface_same_set | |
137 | ||
138 | #define STREQ(a, b) (strcmp(a, b) == 0) | |
139 | ||
140 | /* The type variant functions: IPv4 */ | |
141 | ||
89dc79b7 JK |
142 | struct hash_netiface4_elem_hashed { |
143 | __be32 ip; | |
144 | u8 physdev; | |
145 | u8 cidr; | |
2a7cef2a | 146 | u8 nomatch; |
bd9087e0 | 147 | u8 elem; |
89dc79b7 JK |
148 | }; |
149 | ||
150 | #define HKEY_DATALEN sizeof(struct hash_netiface4_elem_hashed) | |
151 | ||
e385357a JK |
152 | /* Member elements without timeout */ |
153 | struct hash_netiface4_elem { | |
154 | __be32 ip; | |
e385357a JK |
155 | u8 physdev; |
156 | u8 cidr; | |
2a7cef2a | 157 | u8 nomatch; |
bd9087e0 | 158 | u8 elem; |
89dc79b7 | 159 | const char *iface; |
e385357a JK |
160 | }; |
161 | ||
162 | /* Member elements with timeout support */ | |
163 | struct hash_netiface4_telem { | |
164 | __be32 ip; | |
e385357a JK |
165 | u8 physdev; |
166 | u8 cidr; | |
2a7cef2a | 167 | u8 nomatch; |
bd9087e0 | 168 | u8 elem; |
89dc79b7 | 169 | const char *iface; |
e385357a JK |
170 | unsigned long timeout; |
171 | }; | |
172 | ||
173 | static inline bool | |
174 | hash_netiface4_data_equal(const struct hash_netiface4_elem *ip1, | |
89dc79b7 JK |
175 | const struct hash_netiface4_elem *ip2, |
176 | u32 *multi) | |
e385357a JK |
177 | { |
178 | return ip1->ip == ip2->ip && | |
179 | ip1->cidr == ip2->cidr && | |
89dc79b7 | 180 | (++*multi) && |
e385357a JK |
181 | ip1->physdev == ip2->physdev && |
182 | ip1->iface == ip2->iface; | |
183 | } | |
184 | ||
185 | static inline bool | |
186 | hash_netiface4_data_isnull(const struct hash_netiface4_elem *elem) | |
187 | { | |
bd9087e0 | 188 | return elem->elem == 0; |
e385357a JK |
189 | } |
190 | ||
191 | static inline void | |
192 | hash_netiface4_data_copy(struct hash_netiface4_elem *dst, | |
2a7cef2a JK |
193 | const struct hash_netiface4_elem *src) |
194 | { | |
bd9087e0 | 195 | memcpy(dst, src, sizeof(*dst)); |
2a7cef2a JK |
196 | } |
197 | ||
198 | static inline void | |
199 | hash_netiface4_data_flags(struct hash_netiface4_elem *dst, u32 flags) | |
200 | { | |
6eb4c7e9 JK |
201 | dst->nomatch = !!(flags & IPSET_FLAG_NOMATCH); |
202 | } | |
203 | ||
204 | static inline void | |
205 | hash_netiface4_data_reset_flags(struct hash_netiface4_elem *dst, u32 *flags) | |
206 | { | |
207 | if (dst->nomatch) { | |
208 | *flags = IPSET_FLAG_NOMATCH; | |
209 | dst->nomatch = 0; | |
210 | } | |
2a7cef2a JK |
211 | } |
212 | ||
3e0304a5 | 213 | static inline int |
2a7cef2a JK |
214 | hash_netiface4_data_match(const struct hash_netiface4_elem *elem) |
215 | { | |
3e0304a5 | 216 | return elem->nomatch ? -ENOTEMPTY : 1; |
e385357a JK |
217 | } |
218 | ||
219 | static inline void | |
220 | hash_netiface4_data_netmask(struct hash_netiface4_elem *elem, u8 cidr) | |
221 | { | |
222 | elem->ip &= ip_set_netmask(cidr); | |
223 | elem->cidr = cidr; | |
224 | } | |
225 | ||
226 | static inline void | |
227 | hash_netiface4_data_zero_out(struct hash_netiface4_elem *elem) | |
228 | { | |
bd9087e0 | 229 | elem->elem = 0; |
e385357a JK |
230 | } |
231 | ||
232 | static bool | |
233 | hash_netiface4_data_list(struct sk_buff *skb, | |
234 | const struct hash_netiface4_elem *data) | |
235 | { | |
236 | u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0; | |
237 | ||
2a7cef2a JK |
238 | if (data->nomatch) |
239 | flags |= IPSET_FLAG_NOMATCH; | |
7cf7899d DM |
240 | if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) || |
241 | nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) || | |
242 | nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) || | |
243 | (flags && | |
244 | nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags)))) | |
245 | goto nla_put_failure; | |
e385357a JK |
246 | return 0; |
247 | ||
248 | nla_put_failure: | |
249 | return 1; | |
250 | } | |
251 | ||
252 | static bool | |
253 | hash_netiface4_data_tlist(struct sk_buff *skb, | |
254 | const struct hash_netiface4_elem *data) | |
255 | { | |
256 | const struct hash_netiface4_telem *tdata = | |
257 | (const struct hash_netiface4_telem *)data; | |
258 | u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0; | |
259 | ||
2a7cef2a JK |
260 | if (data->nomatch) |
261 | flags |= IPSET_FLAG_NOMATCH; | |
7cf7899d DM |
262 | if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) || |
263 | nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) || | |
264 | nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) || | |
265 | (flags && | |
266 | nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))) || | |
267 | nla_put_net32(skb, IPSET_ATTR_TIMEOUT, | |
268 | htonl(ip_set_timeout_get(tdata->timeout)))) | |
269 | goto nla_put_failure; | |
e385357a JK |
270 | |
271 | return 0; | |
272 | ||
273 | nla_put_failure: | |
274 | return 1; | |
275 | } | |
276 | ||
277 | #define IP_SET_HASH_WITH_NETS | |
278 | #define IP_SET_HASH_WITH_RBTREE | |
89dc79b7 | 279 | #define IP_SET_HASH_WITH_MULTI |
e385357a JK |
280 | |
281 | #define PF 4 | |
282 | #define HOST_MASK 32 | |
283 | #include <linux/netfilter/ipset/ip_set_ahash.h> | |
284 | ||
285 | static inline void | |
286 | hash_netiface4_data_next(struct ip_set_hash *h, | |
287 | const struct hash_netiface4_elem *d) | |
288 | { | |
6e27c9b4 | 289 | h->next.ip = d->ip; |
e385357a JK |
290 | } |
291 | ||
292 | static int | |
293 | hash_netiface4_kadt(struct ip_set *set, const struct sk_buff *skb, | |
294 | const struct xt_action_param *par, | |
295 | enum ipset_adt adt, const struct ip_set_adt_opt *opt) | |
296 | { | |
297 | struct ip_set_hash *h = set->data; | |
298 | ipset_adtfn adtfn = set->variant->adt[adt]; | |
299 | struct hash_netiface4_elem data = { | |
bd9087e0 JK |
300 | .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK, |
301 | .elem = 1, | |
e385357a JK |
302 | }; |
303 | int ret; | |
304 | ||
305 | if (data.cidr == 0) | |
306 | return -EINVAL; | |
307 | if (adt == IPSET_TEST) | |
308 | data.cidr = HOST_MASK; | |
309 | ||
310 | ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip); | |
311 | data.ip &= ip_set_netmask(data.cidr); | |
312 | ||
313 | #define IFACE(dir) (par->dir ? par->dir->name : NULL) | |
314 | #define PHYSDEV(dir) (nf_bridge->dir ? nf_bridge->dir->name : NULL) | |
315 | #define SRCDIR (opt->flags & IPSET_DIM_TWO_SRC) | |
316 | ||
317 | if (opt->cmdflags & IPSET_FLAG_PHYSDEV) { | |
318 | #ifdef CONFIG_BRIDGE_NETFILTER | |
319 | const struct nf_bridge_info *nf_bridge = skb->nf_bridge; | |
15b4d93f | 320 | |
e385357a JK |
321 | if (!nf_bridge) |
322 | return -EINVAL; | |
15b4d93f | 323 | data.iface = SRCDIR ? PHYSDEV(physindev) : PHYSDEV(physoutdev); |
e385357a JK |
324 | data.physdev = 1; |
325 | #else | |
326 | data.iface = NULL; | |
327 | #endif | |
328 | } else | |
329 | data.iface = SRCDIR ? IFACE(in) : IFACE(out); | |
330 | ||
331 | if (!data.iface) | |
332 | return -EINVAL; | |
333 | ret = iface_test(&h->rbtree, &data.iface); | |
334 | if (adt == IPSET_ADD) { | |
335 | if (!ret) { | |
336 | ret = iface_add(&h->rbtree, &data.iface); | |
337 | if (ret) | |
338 | return ret; | |
339 | } | |
340 | } else if (!ret) | |
341 | return ret; | |
342 | ||
343 | return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags); | |
344 | } | |
345 | ||
346 | static int | |
347 | hash_netiface4_uadt(struct ip_set *set, struct nlattr *tb[], | |
348 | enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) | |
349 | { | |
350 | struct ip_set_hash *h = set->data; | |
351 | ipset_adtfn adtfn = set->variant->adt[adt]; | |
bd9087e0 | 352 | struct hash_netiface4_elem data = { .cidr = HOST_MASK, .elem = 1 }; |
e385357a JK |
353 | u32 ip = 0, ip_to, last; |
354 | u32 timeout = h->timeout; | |
ef5b6e12 | 355 | char iface[IFNAMSIZ]; |
e385357a JK |
356 | int ret; |
357 | ||
358 | if (unlikely(!tb[IPSET_ATTR_IP] || | |
359 | !tb[IPSET_ATTR_IFACE] || | |
360 | !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) || | |
361 | !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS))) | |
362 | return -IPSET_ERR_PROTOCOL; | |
363 | ||
364 | if (tb[IPSET_ATTR_LINENO]) | |
365 | *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]); | |
366 | ||
367 | ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip); | |
368 | if (ret) | |
369 | return ret; | |
370 | ||
371 | if (tb[IPSET_ATTR_CIDR]) { | |
372 | data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]); | |
bd9087e0 | 373 | if (data.cidr > HOST_MASK) |
15b4d93f | 374 | return -IPSET_ERR_INVALID_CIDR; |
e385357a JK |
375 | } |
376 | ||
377 | if (tb[IPSET_ATTR_TIMEOUT]) { | |
378 | if (!with_timeout(h->timeout)) | |
379 | return -IPSET_ERR_TIMEOUT; | |
380 | timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]); | |
381 | } | |
382 | ||
15b4d93f | 383 | strcpy(iface, nla_data(tb[IPSET_ATTR_IFACE])); |
e385357a JK |
384 | data.iface = iface; |
385 | ret = iface_test(&h->rbtree, &data.iface); | |
386 | if (adt == IPSET_ADD) { | |
387 | if (!ret) { | |
388 | ret = iface_add(&h->rbtree, &data.iface); | |
389 | if (ret) | |
390 | return ret; | |
391 | } | |
392 | } else if (!ret) | |
393 | return ret; | |
394 | ||
395 | if (tb[IPSET_ATTR_CADT_FLAGS]) { | |
15b4d93f JK |
396 | u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]); |
397 | if (cadt_flags & IPSET_FLAG_PHYSDEV) | |
e385357a | 398 | data.physdev = 1; |
2a7cef2a JK |
399 | if (adt == IPSET_ADD && (cadt_flags & IPSET_FLAG_NOMATCH)) |
400 | flags |= (cadt_flags << 16); | |
e385357a | 401 | } |
e385357a JK |
402 | if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) { |
403 | data.ip = htonl(ip & ip_set_hostmask(data.cidr)); | |
404 | ret = adtfn(set, &data, timeout, flags); | |
405 | return ip_set_eexist(ret, flags) ? 0 : ret; | |
406 | } | |
407 | ||
408 | if (tb[IPSET_ATTR_IP_TO]) { | |
409 | ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to); | |
410 | if (ret) | |
411 | return ret; | |
412 | if (ip_to < ip) | |
413 | swap(ip, ip_to); | |
414 | if (ip + UINT_MAX == ip_to) | |
415 | return -IPSET_ERR_HASH_RANGE; | |
416 | } else { | |
417 | ip_set_mask_from_to(ip, ip_to, data.cidr); | |
418 | } | |
419 | ||
420 | if (retried) | |
6e27c9b4 | 421 | ip = ntohl(h->next.ip); |
e385357a JK |
422 | while (!after(ip, ip_to)) { |
423 | data.ip = htonl(ip); | |
424 | last = ip_set_range_to_cidr(ip, ip_to, &data.cidr); | |
425 | ret = adtfn(set, &data, timeout, flags); | |
426 | ||
427 | if (ret && !ip_set_eexist(ret, flags)) | |
428 | return ret; | |
429 | else | |
430 | ret = 0; | |
431 | ip = last + 1; | |
432 | } | |
433 | return ret; | |
434 | } | |
435 | ||
436 | static bool | |
437 | hash_netiface_same_set(const struct ip_set *a, const struct ip_set *b) | |
438 | { | |
439 | const struct ip_set_hash *x = a->data; | |
440 | const struct ip_set_hash *y = b->data; | |
441 | ||
442 | /* Resizing changes htable_bits, so we ignore it */ | |
443 | return x->maxelem == y->maxelem && | |
444 | x->timeout == y->timeout; | |
445 | } | |
446 | ||
447 | /* The type variant functions: IPv6 */ | |
448 | ||
89dc79b7 JK |
449 | struct hash_netiface6_elem_hashed { |
450 | union nf_inet_addr ip; | |
451 | u8 physdev; | |
452 | u8 cidr; | |
2a7cef2a | 453 | u8 nomatch; |
bd9087e0 | 454 | u8 elem; |
89dc79b7 JK |
455 | }; |
456 | ||
457 | #define HKEY_DATALEN sizeof(struct hash_netiface6_elem_hashed) | |
458 | ||
e385357a JK |
459 | struct hash_netiface6_elem { |
460 | union nf_inet_addr ip; | |
e385357a JK |
461 | u8 physdev; |
462 | u8 cidr; | |
2a7cef2a | 463 | u8 nomatch; |
bd9087e0 | 464 | u8 elem; |
89dc79b7 | 465 | const char *iface; |
e385357a JK |
466 | }; |
467 | ||
468 | struct hash_netiface6_telem { | |
469 | union nf_inet_addr ip; | |
e385357a JK |
470 | u8 physdev; |
471 | u8 cidr; | |
2a7cef2a | 472 | u8 nomatch; |
bd9087e0 | 473 | u8 elem; |
89dc79b7 | 474 | const char *iface; |
e385357a JK |
475 | unsigned long timeout; |
476 | }; | |
477 | ||
478 | static inline bool | |
479 | hash_netiface6_data_equal(const struct hash_netiface6_elem *ip1, | |
89dc79b7 JK |
480 | const struct hash_netiface6_elem *ip2, |
481 | u32 *multi) | |
e385357a | 482 | { |
29e3b160 | 483 | return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) && |
e385357a | 484 | ip1->cidr == ip2->cidr && |
89dc79b7 | 485 | (++*multi) && |
e385357a JK |
486 | ip1->physdev == ip2->physdev && |
487 | ip1->iface == ip2->iface; | |
488 | } | |
489 | ||
490 | static inline bool | |
491 | hash_netiface6_data_isnull(const struct hash_netiface6_elem *elem) | |
492 | { | |
bd9087e0 | 493 | return elem->elem == 0; |
e385357a JK |
494 | } |
495 | ||
496 | static inline void | |
497 | hash_netiface6_data_copy(struct hash_netiface6_elem *dst, | |
498 | const struct hash_netiface6_elem *src) | |
499 | { | |
500 | memcpy(dst, src, sizeof(*dst)); | |
501 | } | |
502 | ||
2a7cef2a JK |
503 | static inline void |
504 | hash_netiface6_data_flags(struct hash_netiface6_elem *dst, u32 flags) | |
505 | { | |
6eb4c7e9 | 506 | dst->nomatch = !!(flags & IPSET_FLAG_NOMATCH); |
2a7cef2a JK |
507 | } |
508 | ||
3e0304a5 | 509 | static inline int |
2a7cef2a JK |
510 | hash_netiface6_data_match(const struct hash_netiface6_elem *elem) |
511 | { | |
3e0304a5 | 512 | return elem->nomatch ? -ENOTEMPTY : 1; |
2a7cef2a JK |
513 | } |
514 | ||
6eb4c7e9 JK |
515 | static inline void |
516 | hash_netiface6_data_reset_flags(struct hash_netiface6_elem *dst, u32 *flags) | |
517 | { | |
518 | if (dst->nomatch) { | |
519 | *flags = IPSET_FLAG_NOMATCH; | |
520 | dst->nomatch = 0; | |
521 | } | |
522 | } | |
523 | ||
e385357a JK |
524 | static inline void |
525 | hash_netiface6_data_zero_out(struct hash_netiface6_elem *elem) | |
526 | { | |
bd9087e0 | 527 | elem->elem = 0; |
e385357a JK |
528 | } |
529 | ||
530 | static inline void | |
531 | ip6_netmask(union nf_inet_addr *ip, u8 prefix) | |
532 | { | |
533 | ip->ip6[0] &= ip_set_netmask6(prefix)[0]; | |
534 | ip->ip6[1] &= ip_set_netmask6(prefix)[1]; | |
535 | ip->ip6[2] &= ip_set_netmask6(prefix)[2]; | |
536 | ip->ip6[3] &= ip_set_netmask6(prefix)[3]; | |
537 | } | |
538 | ||
539 | static inline void | |
540 | hash_netiface6_data_netmask(struct hash_netiface6_elem *elem, u8 cidr) | |
541 | { | |
542 | ip6_netmask(&elem->ip, cidr); | |
543 | elem->cidr = cidr; | |
544 | } | |
545 | ||
546 | static bool | |
547 | hash_netiface6_data_list(struct sk_buff *skb, | |
548 | const struct hash_netiface6_elem *data) | |
549 | { | |
550 | u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0; | |
551 | ||
2a7cef2a JK |
552 | if (data->nomatch) |
553 | flags |= IPSET_FLAG_NOMATCH; | |
7cf7899d DM |
554 | if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) || |
555 | nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) || | |
556 | nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) || | |
557 | (flags && | |
558 | nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags)))) | |
559 | goto nla_put_failure; | |
e385357a JK |
560 | return 0; |
561 | ||
562 | nla_put_failure: | |
563 | return 1; | |
564 | } | |
565 | ||
566 | static bool | |
567 | hash_netiface6_data_tlist(struct sk_buff *skb, | |
568 | const struct hash_netiface6_elem *data) | |
569 | { | |
570 | const struct hash_netiface6_telem *e = | |
571 | (const struct hash_netiface6_telem *)data; | |
572 | u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0; | |
573 | ||
2a7cef2a JK |
574 | if (data->nomatch) |
575 | flags |= IPSET_FLAG_NOMATCH; | |
7cf7899d DM |
576 | if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6) || |
577 | nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) || | |
578 | nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) || | |
579 | (flags && | |
580 | nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))) || | |
581 | nla_put_net32(skb, IPSET_ATTR_TIMEOUT, | |
582 | htonl(ip_set_timeout_get(e->timeout)))) | |
583 | goto nla_put_failure; | |
e385357a JK |
584 | return 0; |
585 | ||
586 | nla_put_failure: | |
587 | return 1; | |
588 | } | |
589 | ||
590 | #undef PF | |
591 | #undef HOST_MASK | |
592 | ||
593 | #define PF 6 | |
594 | #define HOST_MASK 128 | |
595 | #include <linux/netfilter/ipset/ip_set_ahash.h> | |
596 | ||
597 | static inline void | |
598 | hash_netiface6_data_next(struct ip_set_hash *h, | |
599 | const struct hash_netiface6_elem *d) | |
600 | { | |
601 | } | |
602 | ||
603 | static int | |
604 | hash_netiface6_kadt(struct ip_set *set, const struct sk_buff *skb, | |
605 | const struct xt_action_param *par, | |
606 | enum ipset_adt adt, const struct ip_set_adt_opt *opt) | |
607 | { | |
608 | struct ip_set_hash *h = set->data; | |
609 | ipset_adtfn adtfn = set->variant->adt[adt]; | |
610 | struct hash_netiface6_elem data = { | |
bd9087e0 JK |
611 | .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK, |
612 | .elem = 1, | |
e385357a JK |
613 | }; |
614 | int ret; | |
615 | ||
616 | if (data.cidr == 0) | |
617 | return -EINVAL; | |
618 | if (adt == IPSET_TEST) | |
619 | data.cidr = HOST_MASK; | |
620 | ||
621 | ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6); | |
622 | ip6_netmask(&data.ip, data.cidr); | |
623 | ||
624 | if (opt->cmdflags & IPSET_FLAG_PHYSDEV) { | |
625 | #ifdef CONFIG_BRIDGE_NETFILTER | |
626 | const struct nf_bridge_info *nf_bridge = skb->nf_bridge; | |
15b4d93f | 627 | |
e385357a JK |
628 | if (!nf_bridge) |
629 | return -EINVAL; | |
15b4d93f | 630 | data.iface = SRCDIR ? PHYSDEV(physindev) : PHYSDEV(physoutdev); |
e385357a JK |
631 | data.physdev = 1; |
632 | #else | |
633 | data.iface = NULL; | |
634 | #endif | |
635 | } else | |
636 | data.iface = SRCDIR ? IFACE(in) : IFACE(out); | |
637 | ||
638 | if (!data.iface) | |
639 | return -EINVAL; | |
640 | ret = iface_test(&h->rbtree, &data.iface); | |
641 | if (adt == IPSET_ADD) { | |
642 | if (!ret) { | |
643 | ret = iface_add(&h->rbtree, &data.iface); | |
644 | if (ret) | |
645 | return ret; | |
646 | } | |
647 | } else if (!ret) | |
648 | return ret; | |
649 | ||
650 | return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags); | |
651 | } | |
652 | ||
653 | static int | |
654 | hash_netiface6_uadt(struct ip_set *set, struct nlattr *tb[], | |
655 | enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) | |
656 | { | |
657 | struct ip_set_hash *h = set->data; | |
658 | ipset_adtfn adtfn = set->variant->adt[adt]; | |
bd9087e0 | 659 | struct hash_netiface6_elem data = { .cidr = HOST_MASK, .elem = 1 }; |
e385357a | 660 | u32 timeout = h->timeout; |
ef5b6e12 | 661 | char iface[IFNAMSIZ]; |
e385357a JK |
662 | int ret; |
663 | ||
664 | if (unlikely(!tb[IPSET_ATTR_IP] || | |
665 | !tb[IPSET_ATTR_IFACE] || | |
666 | !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) || | |
667 | !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS))) | |
668 | return -IPSET_ERR_PROTOCOL; | |
669 | if (unlikely(tb[IPSET_ATTR_IP_TO])) | |
670 | return -IPSET_ERR_HASH_RANGE_UNSUPPORTED; | |
671 | ||
672 | if (tb[IPSET_ATTR_LINENO]) | |
673 | *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]); | |
674 | ||
675 | ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip); | |
676 | if (ret) | |
677 | return ret; | |
678 | ||
679 | if (tb[IPSET_ATTR_CIDR]) | |
680 | data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]); | |
bd9087e0 | 681 | if (data.cidr > HOST_MASK) |
e385357a JK |
682 | return -IPSET_ERR_INVALID_CIDR; |
683 | ip6_netmask(&data.ip, data.cidr); | |
684 | ||
685 | if (tb[IPSET_ATTR_TIMEOUT]) { | |
686 | if (!with_timeout(h->timeout)) | |
687 | return -IPSET_ERR_TIMEOUT; | |
688 | timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]); | |
689 | } | |
690 | ||
15b4d93f | 691 | strcpy(iface, nla_data(tb[IPSET_ATTR_IFACE])); |
e385357a JK |
692 | data.iface = iface; |
693 | ret = iface_test(&h->rbtree, &data.iface); | |
694 | if (adt == IPSET_ADD) { | |
695 | if (!ret) { | |
696 | ret = iface_add(&h->rbtree, &data.iface); | |
697 | if (ret) | |
698 | return ret; | |
699 | } | |
700 | } else if (!ret) | |
701 | return ret; | |
702 | ||
703 | if (tb[IPSET_ATTR_CADT_FLAGS]) { | |
15b4d93f JK |
704 | u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]); |
705 | if (cadt_flags & IPSET_FLAG_PHYSDEV) | |
e385357a | 706 | data.physdev = 1; |
2a7cef2a JK |
707 | if (adt == IPSET_ADD && (cadt_flags & IPSET_FLAG_NOMATCH)) |
708 | flags |= (cadt_flags << 16); | |
e385357a JK |
709 | } |
710 | ||
711 | ret = adtfn(set, &data, timeout, flags); | |
712 | ||
713 | return ip_set_eexist(ret, flags) ? 0 : ret; | |
714 | } | |
715 | ||
716 | /* Create hash:ip type of sets */ | |
717 | ||
718 | static int | |
719 | hash_netiface_create(struct ip_set *set, struct nlattr *tb[], u32 flags) | |
720 | { | |
721 | struct ip_set_hash *h; | |
722 | u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM; | |
723 | u8 hbits; | |
26a5d3cc | 724 | size_t hsize; |
e385357a | 725 | |
c15f1c83 | 726 | if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6)) |
e385357a JK |
727 | return -IPSET_ERR_INVALID_FAMILY; |
728 | ||
729 | if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) || | |
730 | !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) || | |
731 | !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT))) | |
732 | return -IPSET_ERR_PROTOCOL; | |
733 | ||
734 | if (tb[IPSET_ATTR_HASHSIZE]) { | |
735 | hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]); | |
736 | if (hashsize < IPSET_MIMINAL_HASHSIZE) | |
737 | hashsize = IPSET_MIMINAL_HASHSIZE; | |
738 | } | |
739 | ||
740 | if (tb[IPSET_ATTR_MAXELEM]) | |
741 | maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]); | |
742 | ||
743 | h = kzalloc(sizeof(*h) | |
744 | + sizeof(struct ip_set_hash_nets) | |
c15f1c83 | 745 | * (set->family == NFPROTO_IPV4 ? 32 : 128), GFP_KERNEL); |
e385357a JK |
746 | if (!h) |
747 | return -ENOMEM; | |
748 | ||
749 | h->maxelem = maxelem; | |
750 | get_random_bytes(&h->initval, sizeof(h->initval)); | |
751 | h->timeout = IPSET_NO_TIMEOUT; | |
89dc79b7 | 752 | h->ahash_max = AHASH_MAX_SIZE; |
e385357a JK |
753 | |
754 | hbits = htable_bits(hashsize); | |
26a5d3cc JK |
755 | hsize = htable_size(hbits); |
756 | if (hsize == 0) { | |
757 | kfree(h); | |
758 | return -ENOMEM; | |
759 | } | |
760 | h->table = ip_set_alloc(hsize); | |
e385357a JK |
761 | if (!h->table) { |
762 | kfree(h); | |
763 | return -ENOMEM; | |
764 | } | |
765 | h->table->htable_bits = hbits; | |
766 | h->rbtree = RB_ROOT; | |
767 | ||
768 | set->data = h; | |
769 | ||
770 | if (tb[IPSET_ATTR_TIMEOUT]) { | |
771 | h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]); | |
772 | ||
c15f1c83 | 773 | set->variant = set->family == NFPROTO_IPV4 |
e385357a JK |
774 | ? &hash_netiface4_tvariant : &hash_netiface6_tvariant; |
775 | ||
c15f1c83 | 776 | if (set->family == NFPROTO_IPV4) |
e385357a JK |
777 | hash_netiface4_gc_init(set); |
778 | else | |
779 | hash_netiface6_gc_init(set); | |
780 | } else { | |
c15f1c83 | 781 | set->variant = set->family == NFPROTO_IPV4 |
e385357a JK |
782 | ? &hash_netiface4_variant : &hash_netiface6_variant; |
783 | } | |
784 | ||
785 | pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n", | |
786 | set->name, jhash_size(h->table->htable_bits), | |
787 | h->table->htable_bits, h->maxelem, set->data, h->table); | |
788 | ||
789 | return 0; | |
790 | } | |
791 | ||
792 | static struct ip_set_type hash_netiface_type __read_mostly = { | |
793 | .name = "hash:net,iface", | |
794 | .protocol = IPSET_PROTOCOL, | |
3e0304a5 JK |
795 | .features = IPSET_TYPE_IP | IPSET_TYPE_IFACE | |
796 | IPSET_TYPE_NOMATCH, | |
e385357a | 797 | .dimension = IPSET_DIM_TWO, |
c15f1c83 | 798 | .family = NFPROTO_UNSPEC, |
10111a6e JK |
799 | .revision_min = REVISION_MIN, |
800 | .revision_max = REVISION_MAX, | |
e385357a JK |
801 | .create = hash_netiface_create, |
802 | .create_policy = { | |
803 | [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 }, | |
804 | [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 }, | |
805 | [IPSET_ATTR_PROBES] = { .type = NLA_U8 }, | |
806 | [IPSET_ATTR_RESIZE] = { .type = NLA_U8 }, | |
807 | [IPSET_ATTR_PROTO] = { .type = NLA_U8 }, | |
808 | [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, | |
809 | }, | |
810 | .adt_policy = { | |
811 | [IPSET_ATTR_IP] = { .type = NLA_NESTED }, | |
812 | [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED }, | |
813 | [IPSET_ATTR_IFACE] = { .type = NLA_NUL_STRING, | |
4a6dd664 | 814 | .len = IFNAMSIZ - 1 }, |
e385357a JK |
815 | [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, |
816 | [IPSET_ATTR_CIDR] = { .type = NLA_U8 }, | |
817 | [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, | |
818 | [IPSET_ATTR_LINENO] = { .type = NLA_U32 }, | |
819 | }, | |
820 | .me = THIS_MODULE, | |
821 | }; | |
822 | ||
823 | static int __init | |
824 | hash_netiface_init(void) | |
825 | { | |
826 | return ip_set_type_register(&hash_netiface_type); | |
827 | } | |
828 | ||
829 | static void __exit | |
830 | hash_netiface_fini(void) | |
831 | { | |
832 | ip_set_type_unregister(&hash_netiface_type); | |
833 | } | |
834 | ||
835 | module_init(hash_netiface_init); | |
836 | module_exit(hash_netiface_fini); |