ipw2x00: convert to seq_file
[safe/jmp/linux-2.6] / net / ipv6 / fib6_rules.c
index d587dde..00a7a5e 100644 (file)
@@ -17,6 +17,7 @@
 
 #include <net/fib_rules.h>
 #include <net/ipv6.h>
+#include <net/addrconf.h>
 #include <net/ip6_route.h>
 #include <net/netlink.h>
 
@@ -28,45 +29,22 @@ struct fib6_rule
        u8                      tclass;
 };
 
-static struct fib_rules_ops fib6_rules_ops;
-
-static struct fib6_rule main_rule = {
-       .common = {
-               .refcnt =       ATOMIC_INIT(2),
-               .pref =         0x7FFE,
-               .action =       FR_ACT_TO_TBL,
-               .table =        RT6_TABLE_MAIN,
-       },
-};
-
-static struct fib6_rule local_rule = {
-       .common = {
-               .refcnt =       ATOMIC_INIT(2),
-               .pref =         0,
-               .action =       FR_ACT_TO_TBL,
-               .table =        RT6_TABLE_LOCAL,
-               .flags =        FIB_RULE_PERMANENT,
-       },
-};
-
-static LIST_HEAD(fib6_rules);
-
-struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags,
-                                  pol_lookup_t lookup)
+struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi *fl,
+                                  int flags, pol_lookup_t lookup)
 {
        struct fib_lookup_arg arg = {
                .lookup_ptr = lookup,
        };
 
-       fib_rules_lookup(&fib6_rules_ops, fl, flags, &arg);
+       fib_rules_lookup(net->ipv6.fib6_rules_ops, fl, flags, &arg);
        if (arg.rule)
                fib_rule_put(arg.rule);
 
        if (arg.result)
                return arg.result;
 
-       dst_hold(&ip6_null_entry.u.dst);
-       return &ip6_null_entry.u.dst;
+       dst_hold(&net->ipv6.ip6_null_entry->u.dst);
+       return &net->ipv6.ip6_null_entry->u.dst;
 }
 
 static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
@@ -74,29 +52,60 @@ static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
 {
        struct rt6_info *rt = NULL;
        struct fib6_table *table;
+       struct net *net = rule->fr_net;
        pol_lookup_t lookup = arg->lookup_ptr;
 
        switch (rule->action) {
        case FR_ACT_TO_TBL:
                break;
        case FR_ACT_UNREACHABLE:
-               rt = &ip6_null_entry;
+               rt = net->ipv6.ip6_null_entry;
                goto discard_pkt;
        default:
        case FR_ACT_BLACKHOLE:
-               rt = &ip6_blk_hole_entry;
+               rt = net->ipv6.ip6_blk_hole_entry;
                goto discard_pkt;
        case FR_ACT_PROHIBIT:
-               rt = &ip6_prohibit_entry;
+               rt = net->ipv6.ip6_prohibit_entry;
                goto discard_pkt;
        }
 
-       table = fib6_get_table(rule->table);
+       table = fib6_get_table(net, rule->table);
        if (table)
-               rt = lookup(table, flp, flags);
-
-       if (rt != &ip6_null_entry)
+               rt = lookup(net, table, flp, flags);
+
+       if (rt != net->ipv6.ip6_null_entry) {
+               struct fib6_rule *r = (struct fib6_rule *)rule;
+
+               /*
+                * If we need to find a source address for this traffic,
+                * we check the result if it meets requirement of the rule.
+                */
+               if ((rule->flags & FIB_RULE_FIND_SADDR) &&
+                   r->src.plen && !(flags & RT6_LOOKUP_F_HAS_SADDR)) {
+                       struct in6_addr saddr;
+                       unsigned int srcprefs = 0;
+
+                       if (flags & RT6_LOOKUP_F_SRCPREF_TMP)
+                               srcprefs |= IPV6_PREFER_SRC_TMP;
+                       if (flags & RT6_LOOKUP_F_SRCPREF_PUBLIC)
+                               srcprefs |= IPV6_PREFER_SRC_PUBLIC;
+                       if (flags & RT6_LOOKUP_F_SRCPREF_COA)
+                               srcprefs |= IPV6_PREFER_SRC_COA;
+
+                       if (ipv6_dev_get_saddr(net,
+                                              ip6_dst_idev(&rt->u.dst)->dev,
+                                              &flp->fl6_dst, srcprefs,
+                                              &saddr))
+                               goto again;
+                       if (!ipv6_prefix_equal(&saddr, &r->src.addr,
+                                              r->src.plen))
+                               goto again;
+                       ipv6_addr_copy(&flp->fl6_src, &saddr);
+               }
                goto out;
+       }
+again:
        dst_release(&rt->u.dst);
        rt = NULL;
        goto out;
@@ -117,9 +126,17 @@ static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
            !ipv6_prefix_equal(&fl->fl6_dst, &r->dst.addr, r->dst.plen))
                return 0;
 
+       /*
+        * If FIB_RULE_FIND_SADDR is set and we do not have a
+        * source address for the traffic, we defer check for
+        * source address.
+        */
        if (r->src.plen) {
-               if (!(flags & RT6_LOOKUP_F_HAS_SADDR) ||
-                   !ipv6_prefix_equal(&fl->fl6_src, &r->src.addr, r->src.plen))
+               if (flags & RT6_LOOKUP_F_HAS_SADDR) {
+                       if (!ipv6_prefix_equal(&fl->fl6_src, &r->src.addr,
+                                              r->src.plen))
+                               return 0;
+               } else if (!(r->common.flags & FIB_RULE_FIND_SADDR))
                        return 0;
        }
 
@@ -129,38 +146,33 @@ static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
        return 1;
 }
 
-static struct nla_policy fib6_rule_policy[FRA_MAX+1] __read_mostly = {
+static const struct nla_policy fib6_rule_policy[FRA_MAX+1] = {
        FRA_GENERIC_POLICY,
-       [FRA_SRC]       = { .len = sizeof(struct in6_addr) },
-       [FRA_DST]       = { .len = sizeof(struct in6_addr) },
 };
 
 static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
-                              struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
+                              struct fib_rule_hdr *frh,
                               struct nlattr **tb)
 {
        int err = -EINVAL;
+       struct net *net = sock_net(skb->sk);
        struct fib6_rule *rule6 = (struct fib6_rule *) rule;
 
-       if (frh->src_len > 128 || frh->dst_len > 128 ||
-           (frh->tos & ~IPV6_FLOWINFO_MASK))
-               goto errout;
-
        if (rule->action == FR_ACT_TO_TBL) {
                if (rule->table == RT6_TABLE_UNSPEC)
                        goto errout;
 
-               if (fib6_new_table(rule->table) == NULL) {
+               if (fib6_new_table(net, rule->table) == NULL) {
                        err = -ENOBUFS;
                        goto errout;
                }
        }
 
-       if (tb[FRA_SRC])
+       if (frh->src_len)
                nla_memcpy(&rule6->src.addr, tb[FRA_SRC],
                           sizeof(struct in6_addr));
 
-       if (tb[FRA_DST])
+       if (frh->dst_len)
                nla_memcpy(&rule6->dst.addr, tb[FRA_DST],
                           sizeof(struct in6_addr));
 
@@ -187,11 +199,11 @@ static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
        if (frh->tos && (rule6->tclass != frh->tos))
                return 0;
 
-       if (tb[FRA_SRC] &&
+       if (frh->src_len &&
            nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
                return 0;
 
-       if (tb[FRA_DST] &&
+       if (frh->dst_len &&
            nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
                return 0;
 
@@ -199,7 +211,7 @@ static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
 }
 
 static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
-                         struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
+                         struct fib_rule_hdr *frh)
 {
        struct fib6_rule *rule6 = (struct fib6_rule *) rule;
 
@@ -222,12 +234,7 @@ nla_put_failure:
        return -ENOBUFS;
 }
 
-int fib6_rules_dump(struct sk_buff *skb, struct netlink_callback *cb)
-{
-       return fib_rules_dump(skb, cb, AF_INET6);
-}
-
-static u32 fib6_rule_default_pref(void)
+static u32 fib6_rule_default_pref(struct fib_rules_ops *ops)
 {
        return 0x3FFF;
 }
@@ -238,9 +245,10 @@ static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
               + nla_total_size(16); /* src */
 }
 
-static struct fib_rules_ops fib6_rules_ops = {
+static struct fib_rules_ops fib6_rules_ops_template = {
        .family                 = AF_INET6,
        .rule_size              = sizeof(struct fib6_rule),
+       .addr_size              = sizeof(struct in6_addr),
        .action                 = fib6_rule_action,
        .match                  = fib6_rule_match,
        .configure              = fib6_rule_configure,
@@ -250,19 +258,64 @@ static struct fib_rules_ops fib6_rules_ops = {
        .nlmsg_payload          = fib6_rule_nlmsg_payload,
        .nlgroup                = RTNLGRP_IPV6_RULE,
        .policy                 = fib6_rule_policy,
-       .rules_list             = &fib6_rules,
        .owner                  = THIS_MODULE,
+       .fro_net                = &init_net,
 };
 
-void __init fib6_rules_init(void)
+static int fib6_rules_net_init(struct net *net)
 {
-       list_add_tail(&local_rule.common.list, &fib6_rules);
-       list_add_tail(&main_rule.common.list, &fib6_rules);
+       int err = -ENOMEM;
+
+       net->ipv6.fib6_rules_ops = kmemdup(&fib6_rules_ops_template,
+                                          sizeof(*net->ipv6.fib6_rules_ops),
+                                          GFP_KERNEL);
+       if (!net->ipv6.fib6_rules_ops)
+               goto out;
+
+       net->ipv6.fib6_rules_ops->fro_net = net;
+       INIT_LIST_HEAD(&net->ipv6.fib6_rules_ops->rules_list);
 
-       fib_rules_register(&fib6_rules_ops);
+       err = fib_default_rule_add(net->ipv6.fib6_rules_ops, 0,
+                                  RT6_TABLE_LOCAL, FIB_RULE_PERMANENT);
+       if (err)
+               goto out_fib6_rules_ops;
+
+       err = fib_default_rule_add(net->ipv6.fib6_rules_ops,
+                                  0x7FFE, RT6_TABLE_MAIN, 0);
+       if (err)
+               goto out_fib6_default_rule_add;
+
+       err = fib_rules_register(net->ipv6.fib6_rules_ops);
+       if (err)
+               goto out_fib6_default_rule_add;
+out:
+       return err;
+
+out_fib6_default_rule_add:
+       fib_rules_cleanup_ops(net->ipv6.fib6_rules_ops);
+out_fib6_rules_ops:
+       kfree(net->ipv6.fib6_rules_ops);
+       goto out;
 }
 
+static void fib6_rules_net_exit(struct net *net)
+{
+       fib_rules_unregister(net->ipv6.fib6_rules_ops);
+       kfree(net->ipv6.fib6_rules_ops);
+}
+
+static struct pernet_operations fib6_rules_net_ops = {
+       .init = fib6_rules_net_init,
+       .exit = fib6_rules_net_exit,
+};
+
+int __init fib6_rules_init(void)
+{
+       return register_pernet_subsys(&fib6_rules_net_ops);
+}
+
+
 void fib6_rules_cleanup(void)
 {
-       fib_rules_unregister(&fib6_rules_ops);
+       unregister_pernet_subsys(&fib6_rules_net_ops);
 }