]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/netfilter/ipset/ip_set_hash_netiface.c
Merge remote-tracking branch 'drm/drm-next'
[karo-tx-linux.git] / net / netfilter / ipset / ip_set_hash_netiface.c
1 /* Copyright (C) 2011-2013 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_hash.h>
25
26 #define IPSET_TYPE_REV_MIN      0
27 /*                              1    nomatch flag support added */
28 /*                              2    /0 support added */
29 /*                              3    Counters support added */
30 #define IPSET_TYPE_REV_MAX      4 /* Comments support added */
31
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
34 IP_SET_MODULE_DESC("hash:net,iface", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
35 MODULE_ALIAS("ip_set_hash:net,iface");
36
37 /* Interface name rbtree */
38
39 struct iface_node {
40         struct rb_node node;
41         char iface[IFNAMSIZ];
42 };
43
44 #define iface_data(n)   (rb_entry(n, struct iface_node, node)->iface)
45
46 static void
47 rbtree_destroy(struct rb_root *root)
48 {
49         struct rb_node *p, *n = root->rb_node;
50         struct iface_node *node;
51
52         /* Non-recursive destroy, like in ext3 */
53         while (n) {
54                 if (n->rb_left) {
55                         n = n->rb_left;
56                         continue;
57                 }
58                 if (n->rb_right) {
59                         n = n->rb_right;
60                         continue;
61                 }
62                 p = rb_parent(n);
63                 node = rb_entry(n, struct iface_node, node);
64                 if (!p)
65                         *root = RB_ROOT;
66                 else if (p->rb_left == n)
67                         p->rb_left = NULL;
68                 else if (p->rb_right == n)
69                         p->rb_right = NULL;
70
71                 kfree(node);
72                 n = p;
73         }
74 }
75
76 static int
77 iface_test(struct rb_root *root, const char **iface)
78 {
79         struct rb_node *n = root->rb_node;
80
81         while (n) {
82                 const char *d = iface_data(n);
83                 int res = strcmp(*iface, d);
84
85                 if (res < 0)
86                         n = n->rb_left;
87                 else if (res > 0)
88                         n = n->rb_right;
89                 else {
90                         *iface = d;
91                         return 1;
92                 }
93         }
94         return 0;
95 }
96
97 static int
98 iface_add(struct rb_root *root, const char **iface)
99 {
100         struct rb_node **n = &(root->rb_node), *p = NULL;
101         struct iface_node *d;
102
103         while (*n) {
104                 char *ifname = iface_data(*n);
105                 int res = strcmp(*iface, ifname);
106
107                 p = *n;
108                 if (res < 0)
109                         n = &((*n)->rb_left);
110                 else if (res > 0)
111                         n = &((*n)->rb_right);
112                 else {
113                         *iface = ifname;
114                         return 0;
115                 }
116         }
117
118         d = kzalloc(sizeof(*d), GFP_ATOMIC);
119         if (!d)
120                 return -ENOMEM;
121         strcpy(d->iface, *iface);
122
123         rb_link_node(&d->node, p, n);
124         rb_insert_color(&d->node, root);
125
126         *iface = d->iface;
127         return 0;
128 }
129
130 /* Type specific function prefix */
131 #define HTYPE           hash_netiface
132 #define IP_SET_HASH_WITH_NETS
133 #define IP_SET_HASH_WITH_RBTREE
134 #define IP_SET_HASH_WITH_MULTI
135
136 #define STREQ(a, b)     (strcmp(a, b) == 0)
137
138 /* IPv4 variant */
139
140 struct hash_netiface4_elem_hashed {
141         __be32 ip;
142         u8 physdev;
143         u8 cidr;
144         u8 nomatch;
145         u8 elem;
146 };
147
148 /* Member elements */
149 struct hash_netiface4_elem {
150         __be32 ip;
151         u8 physdev;
152         u8 cidr;
153         u8 nomatch;
154         u8 elem;
155         const char *iface;
156 };
157
158 /* Common functions */
159
160 static inline bool
161 hash_netiface4_data_equal(const struct hash_netiface4_elem *ip1,
162                           const struct hash_netiface4_elem *ip2,
163                           u32 *multi)
164 {
165         return ip1->ip == ip2->ip &&
166                ip1->cidr == ip2->cidr &&
167                (++*multi) &&
168                ip1->physdev == ip2->physdev &&
169                ip1->iface == ip2->iface;
170 }
171
172 static inline int
173 hash_netiface4_do_data_match(const struct hash_netiface4_elem *elem)
174 {
175         return elem->nomatch ? -ENOTEMPTY : 1;
176 }
177
178 static inline void
179 hash_netiface4_data_set_flags(struct hash_netiface4_elem *elem, u32 flags)
180 {
181         elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
182 }
183
184 static inline void
185 hash_netiface4_data_reset_flags(struct hash_netiface4_elem *elem, u8 *flags)
186 {
187         swap(*flags, elem->nomatch);
188 }
189
190 static inline void
191 hash_netiface4_data_netmask(struct hash_netiface4_elem *elem, u8 cidr)
192 {
193         elem->ip &= ip_set_netmask(cidr);
194         elem->cidr = cidr;
195 }
196
197 static bool
198 hash_netiface4_data_list(struct sk_buff *skb,
199                          const struct hash_netiface4_elem *data)
200 {
201         u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0;
202
203         if (data->nomatch)
204                 flags |= IPSET_FLAG_NOMATCH;
205         if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
206             nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
207             nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
208             (flags &&
209              nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
210                 goto nla_put_failure;
211         return 0;
212
213 nla_put_failure:
214         return 1;
215 }
216
217 static inline void
218 hash_netiface4_data_next(struct hash_netiface4_elem *next,
219                          const struct hash_netiface4_elem *d)
220 {
221         next->ip = d->ip;
222 }
223
224 #define MTYPE           hash_netiface4
225 #define PF              4
226 #define HOST_MASK       32
227 #define HKEY_DATALEN    sizeof(struct hash_netiface4_elem_hashed)
228 #include "ip_set_hash_gen.h"
229
230 static int
231 hash_netiface4_kadt(struct ip_set *set, const struct sk_buff *skb,
232                     const struct xt_action_param *par,
233                     enum ipset_adt adt, struct ip_set_adt_opt *opt)
234 {
235         struct hash_netiface *h = set->data;
236         ipset_adtfn adtfn = set->variant->adt[adt];
237         struct hash_netiface4_elem e = {
238                 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
239                 .elem = 1,
240         };
241         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
242         int ret;
243
244         if (e.cidr == 0)
245                 return -EINVAL;
246         if (adt == IPSET_TEST)
247                 e.cidr = HOST_MASK;
248
249         ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
250         e.ip &= ip_set_netmask(e.cidr);
251
252 #define IFACE(dir)      (par->dir ? par->dir->name : NULL)
253 #define PHYSDEV(dir)    (nf_bridge->dir ? nf_bridge->dir->name : NULL)
254 #define SRCDIR          (opt->flags & IPSET_DIM_TWO_SRC)
255
256         if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
257 #ifdef CONFIG_BRIDGE_NETFILTER
258                 const struct nf_bridge_info *nf_bridge = skb->nf_bridge;
259
260                 if (!nf_bridge)
261                         return -EINVAL;
262                 e.iface = SRCDIR ? PHYSDEV(physindev) : PHYSDEV(physoutdev);
263                 e.physdev = 1;
264 #else
265                 e.iface = NULL;
266 #endif
267         } else
268                 e.iface = SRCDIR ? IFACE(in) : IFACE(out);
269
270         if (!e.iface)
271                 return -EINVAL;
272         ret = iface_test(&h->rbtree, &e.iface);
273         if (adt == IPSET_ADD) {
274                 if (!ret) {
275                         ret = iface_add(&h->rbtree, &e.iface);
276                         if (ret)
277                                 return ret;
278                 }
279         } else if (!ret)
280                 return ret;
281
282         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
283 }
284
285 static int
286 hash_netiface4_uadt(struct ip_set *set, struct nlattr *tb[],
287                     enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
288 {
289         struct hash_netiface *h = set->data;
290         ipset_adtfn adtfn = set->variant->adt[adt];
291         struct hash_netiface4_elem e = { .cidr = HOST_MASK, .elem = 1 };
292         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
293         u32 ip = 0, ip_to = 0, last;
294         char iface[IFNAMSIZ];
295         int ret;
296
297         if (unlikely(!tb[IPSET_ATTR_IP] ||
298                      !tb[IPSET_ATTR_IFACE] ||
299                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
300                      !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
301                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
302                      !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
303                 return -IPSET_ERR_PROTOCOL;
304
305         if (tb[IPSET_ATTR_LINENO])
306                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
307
308         ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) ||
309               ip_set_get_extensions(set, tb, &ext);
310         if (ret)
311                 return ret;
312
313         if (tb[IPSET_ATTR_CIDR]) {
314                 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
315                 if (e.cidr > HOST_MASK)
316                         return -IPSET_ERR_INVALID_CIDR;
317         }
318
319         strcpy(iface, nla_data(tb[IPSET_ATTR_IFACE]));
320         e.iface = iface;
321         ret = iface_test(&h->rbtree, &e.iface);
322         if (adt == IPSET_ADD) {
323                 if (!ret) {
324                         ret = iface_add(&h->rbtree, &e.iface);
325                         if (ret)
326                                 return ret;
327                 }
328         } else if (!ret)
329                 return ret;
330
331         if (tb[IPSET_ATTR_CADT_FLAGS]) {
332                 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
333                 if (cadt_flags & IPSET_FLAG_PHYSDEV)
334                         e.physdev = 1;
335                 if (cadt_flags & IPSET_FLAG_NOMATCH)
336                         flags |= (IPSET_FLAG_NOMATCH << 16);
337         }
338         if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
339                 e.ip = htonl(ip & ip_set_hostmask(e.cidr));
340                 ret = adtfn(set, &e, &ext, &ext, flags);
341                 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
342                        ip_set_eexist(ret, flags) ? 0 : ret;
343         }
344
345         if (tb[IPSET_ATTR_IP_TO]) {
346                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
347                 if (ret)
348                         return ret;
349                 if (ip_to < ip)
350                         swap(ip, ip_to);
351                 if (ip + UINT_MAX == ip_to)
352                         return -IPSET_ERR_HASH_RANGE;
353         } else
354                 ip_set_mask_from_to(ip, ip_to, e.cidr);
355
356         if (retried)
357                 ip = ntohl(h->next.ip);
358         while (!after(ip, ip_to)) {
359                 e.ip = htonl(ip);
360                 last = ip_set_range_to_cidr(ip, ip_to, &e.cidr);
361                 ret = adtfn(set, &e, &ext, &ext, flags);
362
363                 if (ret && !ip_set_eexist(ret, flags))
364                         return ret;
365                 else
366                         ret = 0;
367                 ip = last + 1;
368         }
369         return ret;
370 }
371
372 /* IPv6 variant */
373
374 struct hash_netiface6_elem_hashed {
375         union nf_inet_addr ip;
376         u8 physdev;
377         u8 cidr;
378         u8 nomatch;
379         u8 elem;
380 };
381
382 struct hash_netiface6_elem {
383         union nf_inet_addr ip;
384         u8 physdev;
385         u8 cidr;
386         u8 nomatch;
387         u8 elem;
388         const char *iface;
389 };
390
391 /* Common functions */
392
393 static inline bool
394 hash_netiface6_data_equal(const struct hash_netiface6_elem *ip1,
395                           const struct hash_netiface6_elem *ip2,
396                           u32 *multi)
397 {
398         return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
399                ip1->cidr == ip2->cidr &&
400                (++*multi) &&
401                ip1->physdev == ip2->physdev &&
402                ip1->iface == ip2->iface;
403 }
404
405 static inline int
406 hash_netiface6_do_data_match(const struct hash_netiface6_elem *elem)
407 {
408         return elem->nomatch ? -ENOTEMPTY : 1;
409 }
410
411 static inline void
412 hash_netiface6_data_set_flags(struct hash_netiface6_elem *elem, u32 flags)
413 {
414         elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
415 }
416
417 static inline void
418 hash_netiface6_data_reset_flags(struct hash_netiface6_elem *elem, u8 *flags)
419 {
420         swap(*flags, elem->nomatch);
421 }
422
423 static inline void
424 hash_netiface6_data_netmask(struct hash_netiface6_elem *elem, u8 cidr)
425 {
426         ip6_netmask(&elem->ip, cidr);
427         elem->cidr = cidr;
428 }
429
430 static bool
431 hash_netiface6_data_list(struct sk_buff *skb,
432                          const struct hash_netiface6_elem *data)
433 {
434         u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0;
435
436         if (data->nomatch)
437                 flags |= IPSET_FLAG_NOMATCH;
438         if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
439             nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
440             nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
441             (flags &&
442              nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
443                 goto nla_put_failure;
444         return 0;
445
446 nla_put_failure:
447         return 1;
448 }
449
450 static inline void
451 hash_netiface6_data_next(struct hash_netiface4_elem *next,
452                          const struct hash_netiface6_elem *d)
453 {
454 }
455
456 #undef MTYPE
457 #undef PF
458 #undef HOST_MASK
459 #undef HKEY_DATALEN
460
461 #define MTYPE           hash_netiface6
462 #define PF              6
463 #define HOST_MASK       128
464 #define HKEY_DATALEN    sizeof(struct hash_netiface6_elem_hashed)
465 #define IP_SET_EMIT_CREATE
466 #include "ip_set_hash_gen.h"
467
468 static int
469 hash_netiface6_kadt(struct ip_set *set, const struct sk_buff *skb,
470                     const struct xt_action_param *par,
471                     enum ipset_adt adt, struct ip_set_adt_opt *opt)
472 {
473         struct hash_netiface *h = set->data;
474         ipset_adtfn adtfn = set->variant->adt[adt];
475         struct hash_netiface6_elem e = {
476                 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
477                 .elem = 1,
478         };
479         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
480         int ret;
481
482         if (e.cidr == 0)
483                 return -EINVAL;
484         if (adt == IPSET_TEST)
485                 e.cidr = HOST_MASK;
486
487         ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
488         ip6_netmask(&e.ip, e.cidr);
489
490         if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
491 #ifdef CONFIG_BRIDGE_NETFILTER
492                 const struct nf_bridge_info *nf_bridge = skb->nf_bridge;
493
494                 if (!nf_bridge)
495                         return -EINVAL;
496                 e.iface = SRCDIR ? PHYSDEV(physindev) : PHYSDEV(physoutdev);
497                 e.physdev = 1;
498 #else
499                 e.iface = NULL;
500 #endif
501         } else
502                 e.iface = SRCDIR ? IFACE(in) : IFACE(out);
503
504         if (!e.iface)
505                 return -EINVAL;
506         ret = iface_test(&h->rbtree, &e.iface);
507         if (adt == IPSET_ADD) {
508                 if (!ret) {
509                         ret = iface_add(&h->rbtree, &e.iface);
510                         if (ret)
511                                 return ret;
512                 }
513         } else if (!ret)
514                 return ret;
515
516         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
517 }
518
519 static int
520 hash_netiface6_uadt(struct ip_set *set, struct nlattr *tb[],
521                    enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
522 {
523         struct hash_netiface *h = set->data;
524         ipset_adtfn adtfn = set->variant->adt[adt];
525         struct hash_netiface6_elem e = { .cidr = HOST_MASK, .elem = 1 };
526         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
527         char iface[IFNAMSIZ];
528         int ret;
529
530         if (unlikely(!tb[IPSET_ATTR_IP] ||
531                      !tb[IPSET_ATTR_IFACE] ||
532                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
533                      !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
534                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
535                      !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
536                 return -IPSET_ERR_PROTOCOL;
537         if (unlikely(tb[IPSET_ATTR_IP_TO]))
538                 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
539
540         if (tb[IPSET_ATTR_LINENO])
541                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
542
543         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
544               ip_set_get_extensions(set, tb, &ext);
545         if (ret)
546                 return ret;
547
548         if (tb[IPSET_ATTR_CIDR])
549                 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
550         if (e.cidr > HOST_MASK)
551                 return -IPSET_ERR_INVALID_CIDR;
552         ip6_netmask(&e.ip, e.cidr);
553
554         strcpy(iface, nla_data(tb[IPSET_ATTR_IFACE]));
555         e.iface = iface;
556         ret = iface_test(&h->rbtree, &e.iface);
557         if (adt == IPSET_ADD) {
558                 if (!ret) {
559                         ret = iface_add(&h->rbtree, &e.iface);
560                         if (ret)
561                                 return ret;
562                 }
563         } else if (!ret)
564                 return ret;
565
566         if (tb[IPSET_ATTR_CADT_FLAGS]) {
567                 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
568                 if (cadt_flags & IPSET_FLAG_PHYSDEV)
569                         e.physdev = 1;
570                 if (cadt_flags & IPSET_FLAG_NOMATCH)
571                         flags |= (IPSET_FLAG_NOMATCH << 16);
572         }
573
574         ret = adtfn(set, &e, &ext, &ext, flags);
575
576         return ip_set_enomatch(ret, flags, adt, set) ? -ret :
577                ip_set_eexist(ret, flags) ? 0 : ret;
578 }
579
580 static struct ip_set_type hash_netiface_type __read_mostly = {
581         .name           = "hash:net,iface",
582         .protocol       = IPSET_PROTOCOL,
583         .features       = IPSET_TYPE_IP | IPSET_TYPE_IFACE |
584                           IPSET_TYPE_NOMATCH,
585         .dimension      = IPSET_DIM_TWO,
586         .family         = NFPROTO_UNSPEC,
587         .revision_min   = IPSET_TYPE_REV_MIN,
588         .revision_max   = IPSET_TYPE_REV_MAX,
589         .create         = hash_netiface_create,
590         .create_policy  = {
591                 [IPSET_ATTR_HASHSIZE]   = { .type = NLA_U32 },
592                 [IPSET_ATTR_MAXELEM]    = { .type = NLA_U32 },
593                 [IPSET_ATTR_PROBES]     = { .type = NLA_U8 },
594                 [IPSET_ATTR_RESIZE]     = { .type = NLA_U8  },
595                 [IPSET_ATTR_PROTO]      = { .type = NLA_U8 },
596                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
597                 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
598         },
599         .adt_policy     = {
600                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
601                 [IPSET_ATTR_IP_TO]      = { .type = NLA_NESTED },
602                 [IPSET_ATTR_IFACE]      = { .type = NLA_NUL_STRING,
603                                             .len  = IFNAMSIZ - 1 },
604                 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
605                 [IPSET_ATTR_CIDR]       = { .type = NLA_U8 },
606                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
607                 [IPSET_ATTR_LINENO]     = { .type = NLA_U32 },
608                 [IPSET_ATTR_BYTES]      = { .type = NLA_U64 },
609                 [IPSET_ATTR_PACKETS]    = { .type = NLA_U64 },
610                 [IPSET_ATTR_COMMENT]    = { .type = NLA_NUL_STRING },
611         },
612         .me             = THIS_MODULE,
613 };
614
615 static int __init
616 hash_netiface_init(void)
617 {
618         return ip_set_type_register(&hash_netiface_type);
619 }
620
621 static void __exit
622 hash_netiface_fini(void)
623 {
624         ip_set_type_unregister(&hash_netiface_type);
625 }
626
627 module_init(hash_netiface_init);
628 module_exit(hash_netiface_fini);