blob: b9a63381e34998e08ab5271f7d82cca9058a76d8 [file] [log] [blame]
Jozsef Kadlecsike3853572011-06-16 19:00:48 +02001/* 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
Jozsef Kadlecsik10111a62012-09-21 21:59:32 +020027#define REVISION_MIN 0
28/* 1 nomatch flag support added */
29#define REVISION_MAX 2 /* /0 support added */
30
Jozsef Kadlecsike3853572011-06-16 19:00:48 +020031MODULE_LICENSE("GPL");
32MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
Jozsef Kadlecsik10111a62012-09-21 21:59:32 +020033IP_SET_MODULE_DESC("hash:net,iface", REVISION_MIN, REVISION_MAX);
Jozsef Kadlecsike3853572011-06-16 19:00:48 +020034MODULE_ALIAS("ip_set_hash:net,iface");
35
36/* Interface name rbtree */
37
38struct 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
Jozsef Kadlecsike3853572011-06-16 19:00:48 +020045static void
46rbtree_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
75static int
76iface_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);
Florian Westphalef5b6e12012-06-17 09:56:46 +000082 int res = strcmp(*iface, d);
Jozsef Kadlecsik15b4d932011-06-16 19:01:26 +020083
Jozsef Kadlecsike3853572011-06-16 19:00:48 +020084 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
96static int
97iface_add(struct rb_root *root, const char **iface)
98{
99 struct rb_node **n = &(root->rb_node), *p = NULL;
100 struct iface_node *d;
Jozsef Kadlecsik15b4d932011-06-16 19:01:26 +0200101
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200102 while (*n) {
103 char *ifname = iface_data(*n);
Florian Westphalef5b6e12012-06-17 09:56:46 +0000104 int res = strcmp(*iface, ifname);
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200105
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
132static bool
133hash_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
Jozsef Kadlecsik89dc79b2011-07-21 12:06:18 +0200142struct hash_netiface4_elem_hashed {
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200143 __be32 ip;
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200144 u8 physdev;
145 u8 cidr;
Jozsef Kadlecsik2a7cef22012-01-14 17:16:36 +0100146 u8 nomatch;
Jozsef Kadlecsikbd9087e2012-09-10 21:22:23 +0200147 u8 elem;
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200148};
149
Jozsef Kadlecsik89dc79b2011-07-21 12:06:18 +0200150#define HKEY_DATALEN sizeof(struct hash_netiface4_elem_hashed)
151
152/* Member elements without timeout */
153struct hash_netiface4_elem {
154 __be32 ip;
155 u8 physdev;
156 u8 cidr;
Jozsef Kadlecsik2a7cef22012-01-14 17:16:36 +0100157 u8 nomatch;
Jozsef Kadlecsikbd9087e2012-09-10 21:22:23 +0200158 u8 elem;
Jozsef Kadlecsik89dc79b2011-07-21 12:06:18 +0200159 const char *iface;
160};
161
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200162/* Member elements with timeout support */
163struct hash_netiface4_telem {
164 __be32 ip;
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200165 u8 physdev;
166 u8 cidr;
Jozsef Kadlecsik2a7cef22012-01-14 17:16:36 +0100167 u8 nomatch;
Jozsef Kadlecsikbd9087e2012-09-10 21:22:23 +0200168 u8 elem;
Jozsef Kadlecsik89dc79b2011-07-21 12:06:18 +0200169 const char *iface;
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200170 unsigned long timeout;
171};
172
173static inline bool
174hash_netiface4_data_equal(const struct hash_netiface4_elem *ip1,
Jozsef Kadlecsik89dc79b2011-07-21 12:06:18 +0200175 const struct hash_netiface4_elem *ip2,
176 u32 *multi)
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200177{
178 return ip1->ip == ip2->ip &&
179 ip1->cidr == ip2->cidr &&
Jozsef Kadlecsik89dc79b2011-07-21 12:06:18 +0200180 (++*multi) &&
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200181 ip1->physdev == ip2->physdev &&
182 ip1->iface == ip2->iface;
183}
184
185static inline bool
186hash_netiface4_data_isnull(const struct hash_netiface4_elem *elem)
187{
Jozsef Kadlecsikbd9087e2012-09-10 21:22:23 +0200188 return elem->elem == 0;
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200189}
190
191static inline void
192hash_netiface4_data_copy(struct hash_netiface4_elem *dst,
Jozsef Kadlecsik2a7cef22012-01-14 17:16:36 +0100193 const struct hash_netiface4_elem *src)
194{
Jozsef Kadlecsikbd9087e2012-09-10 21:22:23 +0200195 memcpy(dst, src, sizeof(*dst));
Jozsef Kadlecsik2a7cef22012-01-14 17:16:36 +0100196}
197
198static inline void
199hash_netiface4_data_flags(struct hash_netiface4_elem *dst, u32 flags)
200{
201 dst->nomatch = flags & IPSET_FLAG_NOMATCH;
202}
203
Jozsef Kadlecsik3e0304a2012-09-21 22:02:36 +0200204static inline int
Jozsef Kadlecsik2a7cef22012-01-14 17:16:36 +0100205hash_netiface4_data_match(const struct hash_netiface4_elem *elem)
206{
Jozsef Kadlecsik3e0304a2012-09-21 22:02:36 +0200207 return elem->nomatch ? -ENOTEMPTY : 1;
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200208}
209
210static inline void
211hash_netiface4_data_netmask(struct hash_netiface4_elem *elem, u8 cidr)
212{
213 elem->ip &= ip_set_netmask(cidr);
214 elem->cidr = cidr;
215}
216
217static inline void
218hash_netiface4_data_zero_out(struct hash_netiface4_elem *elem)
219{
Jozsef Kadlecsikbd9087e2012-09-10 21:22:23 +0200220 elem->elem = 0;
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200221}
222
223static bool
224hash_netiface4_data_list(struct sk_buff *skb,
225 const struct hash_netiface4_elem *data)
226{
227 u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0;
228
Jozsef Kadlecsik2a7cef22012-01-14 17:16:36 +0100229 if (data->nomatch)
230 flags |= IPSET_FLAG_NOMATCH;
David S. Miller7cf78992012-04-01 19:54:46 -0400231 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
232 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
233 nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
234 (flags &&
235 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
236 goto nla_put_failure;
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200237 return 0;
238
239nla_put_failure:
240 return 1;
241}
242
243static bool
244hash_netiface4_data_tlist(struct sk_buff *skb,
245 const struct hash_netiface4_elem *data)
246{
247 const struct hash_netiface4_telem *tdata =
248 (const struct hash_netiface4_telem *)data;
249 u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0;
250
Jozsef Kadlecsik2a7cef22012-01-14 17:16:36 +0100251 if (data->nomatch)
252 flags |= IPSET_FLAG_NOMATCH;
David S. Miller7cf78992012-04-01 19:54:46 -0400253 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
254 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
255 nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
256 (flags &&
257 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))) ||
258 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
259 htonl(ip_set_timeout_get(tdata->timeout))))
260 goto nla_put_failure;
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200261
262 return 0;
263
264nla_put_failure:
265 return 1;
266}
267
268#define IP_SET_HASH_WITH_NETS
269#define IP_SET_HASH_WITH_RBTREE
Jozsef Kadlecsik89dc79b2011-07-21 12:06:18 +0200270#define IP_SET_HASH_WITH_MULTI
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200271
272#define PF 4
273#define HOST_MASK 32
274#include <linux/netfilter/ipset/ip_set_ahash.h>
275
276static inline void
277hash_netiface4_data_next(struct ip_set_hash *h,
278 const struct hash_netiface4_elem *d)
279{
Jozsef Kadlecsik6e27c9b2012-09-21 21:44:58 +0200280 h->next.ip = d->ip;
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200281}
282
283static int
284hash_netiface4_kadt(struct ip_set *set, const struct sk_buff *skb,
285 const struct xt_action_param *par,
286 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
287{
288 struct ip_set_hash *h = set->data;
289 ipset_adtfn adtfn = set->variant->adt[adt];
290 struct hash_netiface4_elem data = {
Jozsef Kadlecsikbd9087e2012-09-10 21:22:23 +0200291 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK,
292 .elem = 1,
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200293 };
294 int ret;
295
296 if (data.cidr == 0)
297 return -EINVAL;
298 if (adt == IPSET_TEST)
299 data.cidr = HOST_MASK;
300
301 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip);
302 data.ip &= ip_set_netmask(data.cidr);
303
304#define IFACE(dir) (par->dir ? par->dir->name : NULL)
305#define PHYSDEV(dir) (nf_bridge->dir ? nf_bridge->dir->name : NULL)
306#define SRCDIR (opt->flags & IPSET_DIM_TWO_SRC)
307
308 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
309#ifdef CONFIG_BRIDGE_NETFILTER
310 const struct nf_bridge_info *nf_bridge = skb->nf_bridge;
Jozsef Kadlecsik15b4d932011-06-16 19:01:26 +0200311
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200312 if (!nf_bridge)
313 return -EINVAL;
Jozsef Kadlecsik15b4d932011-06-16 19:01:26 +0200314 data.iface = SRCDIR ? PHYSDEV(physindev) : PHYSDEV(physoutdev);
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200315 data.physdev = 1;
316#else
317 data.iface = NULL;
318#endif
319 } else
320 data.iface = SRCDIR ? IFACE(in) : IFACE(out);
321
322 if (!data.iface)
323 return -EINVAL;
324 ret = iface_test(&h->rbtree, &data.iface);
325 if (adt == IPSET_ADD) {
326 if (!ret) {
327 ret = iface_add(&h->rbtree, &data.iface);
328 if (ret)
329 return ret;
330 }
331 } else if (!ret)
332 return ret;
333
334 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
335}
336
337static int
338hash_netiface4_uadt(struct ip_set *set, struct nlattr *tb[],
339 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
340{
341 struct ip_set_hash *h = set->data;
342 ipset_adtfn adtfn = set->variant->adt[adt];
Jozsef Kadlecsikbd9087e2012-09-10 21:22:23 +0200343 struct hash_netiface4_elem data = { .cidr = HOST_MASK, .elem = 1 };
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200344 u32 ip = 0, ip_to, last;
345 u32 timeout = h->timeout;
Florian Westphalef5b6e12012-06-17 09:56:46 +0000346 char iface[IFNAMSIZ];
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200347 int ret;
348
349 if (unlikely(!tb[IPSET_ATTR_IP] ||
350 !tb[IPSET_ATTR_IFACE] ||
351 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
352 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
353 return -IPSET_ERR_PROTOCOL;
354
355 if (tb[IPSET_ATTR_LINENO])
356 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
357
358 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
359 if (ret)
360 return ret;
361
362 if (tb[IPSET_ATTR_CIDR]) {
363 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
Jozsef Kadlecsikbd9087e2012-09-10 21:22:23 +0200364 if (data.cidr > HOST_MASK)
Jozsef Kadlecsik15b4d932011-06-16 19:01:26 +0200365 return -IPSET_ERR_INVALID_CIDR;
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200366 }
367
368 if (tb[IPSET_ATTR_TIMEOUT]) {
369 if (!with_timeout(h->timeout))
370 return -IPSET_ERR_TIMEOUT;
371 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
372 }
373
Jozsef Kadlecsik15b4d932011-06-16 19:01:26 +0200374 strcpy(iface, nla_data(tb[IPSET_ATTR_IFACE]));
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200375 data.iface = iface;
376 ret = iface_test(&h->rbtree, &data.iface);
377 if (adt == IPSET_ADD) {
378 if (!ret) {
379 ret = iface_add(&h->rbtree, &data.iface);
380 if (ret)
381 return ret;
382 }
383 } else if (!ret)
384 return ret;
385
386 if (tb[IPSET_ATTR_CADT_FLAGS]) {
Jozsef Kadlecsik15b4d932011-06-16 19:01:26 +0200387 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
388 if (cadt_flags & IPSET_FLAG_PHYSDEV)
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200389 data.physdev = 1;
Jozsef Kadlecsik2a7cef22012-01-14 17:16:36 +0100390 if (adt == IPSET_ADD && (cadt_flags & IPSET_FLAG_NOMATCH))
391 flags |= (cadt_flags << 16);
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200392 }
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200393 if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
394 data.ip = htonl(ip & ip_set_hostmask(data.cidr));
395 ret = adtfn(set, &data, timeout, flags);
396 return ip_set_eexist(ret, flags) ? 0 : ret;
397 }
398
399 if (tb[IPSET_ATTR_IP_TO]) {
400 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
401 if (ret)
402 return ret;
403 if (ip_to < ip)
404 swap(ip, ip_to);
405 if (ip + UINT_MAX == ip_to)
406 return -IPSET_ERR_HASH_RANGE;
407 } else {
408 ip_set_mask_from_to(ip, ip_to, data.cidr);
409 }
410
411 if (retried)
Jozsef Kadlecsik6e27c9b2012-09-21 21:44:58 +0200412 ip = ntohl(h->next.ip);
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200413 while (!after(ip, ip_to)) {
414 data.ip = htonl(ip);
415 last = ip_set_range_to_cidr(ip, ip_to, &data.cidr);
416 ret = adtfn(set, &data, timeout, flags);
417
418 if (ret && !ip_set_eexist(ret, flags))
419 return ret;
420 else
421 ret = 0;
422 ip = last + 1;
423 }
424 return ret;
425}
426
427static bool
428hash_netiface_same_set(const struct ip_set *a, const struct ip_set *b)
429{
430 const struct ip_set_hash *x = a->data;
431 const struct ip_set_hash *y = b->data;
432
433 /* Resizing changes htable_bits, so we ignore it */
434 return x->maxelem == y->maxelem &&
435 x->timeout == y->timeout;
436}
437
438/* The type variant functions: IPv6 */
439
Jozsef Kadlecsik89dc79b2011-07-21 12:06:18 +0200440struct hash_netiface6_elem_hashed {
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200441 union nf_inet_addr ip;
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200442 u8 physdev;
443 u8 cidr;
Jozsef Kadlecsik2a7cef22012-01-14 17:16:36 +0100444 u8 nomatch;
Jozsef Kadlecsikbd9087e2012-09-10 21:22:23 +0200445 u8 elem;
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200446};
447
Jozsef Kadlecsik89dc79b2011-07-21 12:06:18 +0200448#define HKEY_DATALEN sizeof(struct hash_netiface6_elem_hashed)
449
450struct hash_netiface6_elem {
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200451 union nf_inet_addr ip;
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200452 u8 physdev;
453 u8 cidr;
Jozsef Kadlecsik2a7cef22012-01-14 17:16:36 +0100454 u8 nomatch;
Jozsef Kadlecsikbd9087e2012-09-10 21:22:23 +0200455 u8 elem;
Jozsef Kadlecsik89dc79b2011-07-21 12:06:18 +0200456 const char *iface;
457};
458
459struct hash_netiface6_telem {
460 union nf_inet_addr ip;
461 u8 physdev;
462 u8 cidr;
Jozsef Kadlecsik2a7cef22012-01-14 17:16:36 +0100463 u8 nomatch;
Jozsef Kadlecsikbd9087e2012-09-10 21:22:23 +0200464 u8 elem;
Jozsef Kadlecsik89dc79b2011-07-21 12:06:18 +0200465 const char *iface;
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200466 unsigned long timeout;
467};
468
469static inline bool
470hash_netiface6_data_equal(const struct hash_netiface6_elem *ip1,
Jozsef Kadlecsik89dc79b2011-07-21 12:06:18 +0200471 const struct hash_netiface6_elem *ip2,
472 u32 *multi)
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200473{
474 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
475 ip1->cidr == ip2->cidr &&
Jozsef Kadlecsik89dc79b2011-07-21 12:06:18 +0200476 (++*multi) &&
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200477 ip1->physdev == ip2->physdev &&
478 ip1->iface == ip2->iface;
479}
480
481static inline bool
482hash_netiface6_data_isnull(const struct hash_netiface6_elem *elem)
483{
Jozsef Kadlecsikbd9087e2012-09-10 21:22:23 +0200484 return elem->elem == 0;
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200485}
486
487static inline void
488hash_netiface6_data_copy(struct hash_netiface6_elem *dst,
489 const struct hash_netiface6_elem *src)
490{
491 memcpy(dst, src, sizeof(*dst));
492}
493
494static inline void
Jozsef Kadlecsik2a7cef22012-01-14 17:16:36 +0100495hash_netiface6_data_flags(struct hash_netiface6_elem *dst, u32 flags)
496{
497 dst->nomatch = flags & IPSET_FLAG_NOMATCH;
498}
499
Jozsef Kadlecsik3e0304a2012-09-21 22:02:36 +0200500static inline int
Jozsef Kadlecsik2a7cef22012-01-14 17:16:36 +0100501hash_netiface6_data_match(const struct hash_netiface6_elem *elem)
502{
Jozsef Kadlecsik3e0304a2012-09-21 22:02:36 +0200503 return elem->nomatch ? -ENOTEMPTY : 1;
Jozsef Kadlecsik2a7cef22012-01-14 17:16:36 +0100504}
505
506static inline void
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200507hash_netiface6_data_zero_out(struct hash_netiface6_elem *elem)
508{
Jozsef Kadlecsikbd9087e2012-09-10 21:22:23 +0200509 elem->elem = 0;
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200510}
511
512static inline void
513ip6_netmask(union nf_inet_addr *ip, u8 prefix)
514{
515 ip->ip6[0] &= ip_set_netmask6(prefix)[0];
516 ip->ip6[1] &= ip_set_netmask6(prefix)[1];
517 ip->ip6[2] &= ip_set_netmask6(prefix)[2];
518 ip->ip6[3] &= ip_set_netmask6(prefix)[3];
519}
520
521static inline void
522hash_netiface6_data_netmask(struct hash_netiface6_elem *elem, u8 cidr)
523{
524 ip6_netmask(&elem->ip, cidr);
525 elem->cidr = cidr;
526}
527
528static bool
529hash_netiface6_data_list(struct sk_buff *skb,
530 const struct hash_netiface6_elem *data)
531{
532 u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0;
533
Jozsef Kadlecsik2a7cef22012-01-14 17:16:36 +0100534 if (data->nomatch)
535 flags |= IPSET_FLAG_NOMATCH;
David S. Miller7cf78992012-04-01 19:54:46 -0400536 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
537 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
538 nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
539 (flags &&
540 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
541 goto nla_put_failure;
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200542 return 0;
543
544nla_put_failure:
545 return 1;
546}
547
548static bool
549hash_netiface6_data_tlist(struct sk_buff *skb,
550 const struct hash_netiface6_elem *data)
551{
552 const struct hash_netiface6_telem *e =
553 (const struct hash_netiface6_telem *)data;
554 u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0;
555
Jozsef Kadlecsik2a7cef22012-01-14 17:16:36 +0100556 if (data->nomatch)
557 flags |= IPSET_FLAG_NOMATCH;
David S. Miller7cf78992012-04-01 19:54:46 -0400558 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6) ||
559 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
560 nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
561 (flags &&
562 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))) ||
563 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
564 htonl(ip_set_timeout_get(e->timeout))))
565 goto nla_put_failure;
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200566 return 0;
567
568nla_put_failure:
569 return 1;
570}
571
572#undef PF
573#undef HOST_MASK
574
575#define PF 6
576#define HOST_MASK 128
577#include <linux/netfilter/ipset/ip_set_ahash.h>
578
579static inline void
580hash_netiface6_data_next(struct ip_set_hash *h,
581 const struct hash_netiface6_elem *d)
582{
583}
584
585static int
586hash_netiface6_kadt(struct ip_set *set, const struct sk_buff *skb,
587 const struct xt_action_param *par,
588 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
589{
590 struct ip_set_hash *h = set->data;
591 ipset_adtfn adtfn = set->variant->adt[adt];
592 struct hash_netiface6_elem data = {
Jozsef Kadlecsikbd9087e2012-09-10 21:22:23 +0200593 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK,
594 .elem = 1,
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200595 };
596 int ret;
597
598 if (data.cidr == 0)
599 return -EINVAL;
600 if (adt == IPSET_TEST)
601 data.cidr = HOST_MASK;
602
603 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
604 ip6_netmask(&data.ip, data.cidr);
605
606 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
607#ifdef CONFIG_BRIDGE_NETFILTER
608 const struct nf_bridge_info *nf_bridge = skb->nf_bridge;
Jozsef Kadlecsik15b4d932011-06-16 19:01:26 +0200609
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200610 if (!nf_bridge)
611 return -EINVAL;
Jozsef Kadlecsik15b4d932011-06-16 19:01:26 +0200612 data.iface = SRCDIR ? PHYSDEV(physindev) : PHYSDEV(physoutdev);
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200613 data.physdev = 1;
614#else
615 data.iface = NULL;
616#endif
617 } else
618 data.iface = SRCDIR ? IFACE(in) : IFACE(out);
619
620 if (!data.iface)
621 return -EINVAL;
622 ret = iface_test(&h->rbtree, &data.iface);
623 if (adt == IPSET_ADD) {
624 if (!ret) {
625 ret = iface_add(&h->rbtree, &data.iface);
626 if (ret)
627 return ret;
628 }
629 } else if (!ret)
630 return ret;
631
632 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
633}
634
635static int
636hash_netiface6_uadt(struct ip_set *set, struct nlattr *tb[],
637 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
638{
639 struct ip_set_hash *h = set->data;
640 ipset_adtfn adtfn = set->variant->adt[adt];
Jozsef Kadlecsikbd9087e2012-09-10 21:22:23 +0200641 struct hash_netiface6_elem data = { .cidr = HOST_MASK, .elem = 1 };
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200642 u32 timeout = h->timeout;
Florian Westphalef5b6e12012-06-17 09:56:46 +0000643 char iface[IFNAMSIZ];
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200644 int ret;
645
646 if (unlikely(!tb[IPSET_ATTR_IP] ||
647 !tb[IPSET_ATTR_IFACE] ||
648 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
649 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
650 return -IPSET_ERR_PROTOCOL;
651 if (unlikely(tb[IPSET_ATTR_IP_TO]))
652 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
653
654 if (tb[IPSET_ATTR_LINENO])
655 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
656
657 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
658 if (ret)
659 return ret;
660
661 if (tb[IPSET_ATTR_CIDR])
662 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
Jozsef Kadlecsikbd9087e2012-09-10 21:22:23 +0200663 if (data.cidr > HOST_MASK)
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200664 return -IPSET_ERR_INVALID_CIDR;
665 ip6_netmask(&data.ip, data.cidr);
666
667 if (tb[IPSET_ATTR_TIMEOUT]) {
668 if (!with_timeout(h->timeout))
669 return -IPSET_ERR_TIMEOUT;
670 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
671 }
672
Jozsef Kadlecsik15b4d932011-06-16 19:01:26 +0200673 strcpy(iface, nla_data(tb[IPSET_ATTR_IFACE]));
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200674 data.iface = iface;
675 ret = iface_test(&h->rbtree, &data.iface);
676 if (adt == IPSET_ADD) {
677 if (!ret) {
678 ret = iface_add(&h->rbtree, &data.iface);
679 if (ret)
680 return ret;
681 }
682 } else if (!ret)
683 return ret;
684
685 if (tb[IPSET_ATTR_CADT_FLAGS]) {
Jozsef Kadlecsik15b4d932011-06-16 19:01:26 +0200686 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
687 if (cadt_flags & IPSET_FLAG_PHYSDEV)
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200688 data.physdev = 1;
Jozsef Kadlecsik2a7cef22012-01-14 17:16:36 +0100689 if (adt == IPSET_ADD && (cadt_flags & IPSET_FLAG_NOMATCH))
690 flags |= (cadt_flags << 16);
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200691 }
692
693 ret = adtfn(set, &data, timeout, flags);
694
695 return ip_set_eexist(ret, flags) ? 0 : ret;
696}
697
698/* Create hash:ip type of sets */
699
700static int
701hash_netiface_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
702{
703 struct ip_set_hash *h;
704 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
705 u8 hbits;
Jozsef Kadlecsik26a5d3c2012-05-14 01:47:01 +0000706 size_t hsize;
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200707
Jan Engelhardtc15f1c82012-02-14 00:24:10 +0100708 if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6))
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200709 return -IPSET_ERR_INVALID_FAMILY;
710
711 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
712 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
713 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
714 return -IPSET_ERR_PROTOCOL;
715
716 if (tb[IPSET_ATTR_HASHSIZE]) {
717 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
718 if (hashsize < IPSET_MIMINAL_HASHSIZE)
719 hashsize = IPSET_MIMINAL_HASHSIZE;
720 }
721
722 if (tb[IPSET_ATTR_MAXELEM])
723 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
724
725 h = kzalloc(sizeof(*h)
726 + sizeof(struct ip_set_hash_nets)
Jan Engelhardtc15f1c82012-02-14 00:24:10 +0100727 * (set->family == NFPROTO_IPV4 ? 32 : 128), GFP_KERNEL);
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200728 if (!h)
729 return -ENOMEM;
730
731 h->maxelem = maxelem;
732 get_random_bytes(&h->initval, sizeof(h->initval));
733 h->timeout = IPSET_NO_TIMEOUT;
Jozsef Kadlecsik89dc79b2011-07-21 12:06:18 +0200734 h->ahash_max = AHASH_MAX_SIZE;
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200735
736 hbits = htable_bits(hashsize);
Jozsef Kadlecsik26a5d3c2012-05-14 01:47:01 +0000737 hsize = htable_size(hbits);
738 if (hsize == 0) {
739 kfree(h);
740 return -ENOMEM;
741 }
742 h->table = ip_set_alloc(hsize);
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200743 if (!h->table) {
744 kfree(h);
745 return -ENOMEM;
746 }
747 h->table->htable_bits = hbits;
748 h->rbtree = RB_ROOT;
749
750 set->data = h;
751
752 if (tb[IPSET_ATTR_TIMEOUT]) {
753 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
754
Jan Engelhardtc15f1c82012-02-14 00:24:10 +0100755 set->variant = set->family == NFPROTO_IPV4
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200756 ? &hash_netiface4_tvariant : &hash_netiface6_tvariant;
757
Jan Engelhardtc15f1c82012-02-14 00:24:10 +0100758 if (set->family == NFPROTO_IPV4)
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200759 hash_netiface4_gc_init(set);
760 else
761 hash_netiface6_gc_init(set);
762 } else {
Jan Engelhardtc15f1c82012-02-14 00:24:10 +0100763 set->variant = set->family == NFPROTO_IPV4
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200764 ? &hash_netiface4_variant : &hash_netiface6_variant;
765 }
766
767 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
768 set->name, jhash_size(h->table->htable_bits),
769 h->table->htable_bits, h->maxelem, set->data, h->table);
770
771 return 0;
772}
773
774static struct ip_set_type hash_netiface_type __read_mostly = {
775 .name = "hash:net,iface",
776 .protocol = IPSET_PROTOCOL,
Jozsef Kadlecsik3e0304a2012-09-21 22:02:36 +0200777 .features = IPSET_TYPE_IP | IPSET_TYPE_IFACE |
778 IPSET_TYPE_NOMATCH,
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200779 .dimension = IPSET_DIM_TWO,
Jan Engelhardtc15f1c82012-02-14 00:24:10 +0100780 .family = NFPROTO_UNSPEC,
Jozsef Kadlecsik10111a62012-09-21 21:59:32 +0200781 .revision_min = REVISION_MIN,
782 .revision_max = REVISION_MAX,
Jozsef Kadlecsike3853572011-06-16 19:00:48 +0200783 .create = hash_netiface_create,
784 .create_policy = {
785 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
786 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
787 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
788 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
789 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
790 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
791 },
792 .adt_policy = {
793 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
794 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
795 [IPSET_ATTR_IFACE] = { .type = NLA_NUL_STRING,
796 .len = IPSET_MAXNAMELEN - 1 },
797 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
798 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
799 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
800 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
801 },
802 .me = THIS_MODULE,
803};
804
805static int __init
806hash_netiface_init(void)
807{
808 return ip_set_type_register(&hash_netiface_type);
809}
810
811static void __exit
812hash_netiface_fini(void)
813{
814 ip_set_type_unregister(&hash_netiface_type);
815}
816
817module_init(hash_netiface_init);
818module_exit(hash_netiface_fini);