Merge branch 'e1000-fixes' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[pandora-kernel.git] / net / ipv6 / fib6_rules.c
index 34f5bfa..fc3882c 100644 (file)
  *     Ville Nuorvala          <vnuorval@tcs.hut.fi>
  */
 
-#include <linux/config.h>
 #include <linux/netdevice.h>
 
 #include <net/fib_rules.h>
 #include <net/ipv6.h>
+#include <net/addrconf.h>
 #include <net/ip6_route.h>
 #include <net/netlink.h>
 
@@ -26,10 +26,6 @@ struct fib6_rule
        struct fib_rule         common;
        struct rt6key           src;
        struct rt6key           dst;
-#ifdef CONFIG_IPV6_ROUTE_FWMARK
-       u32                     fwmark;
-       u32                     fwmask;
-#endif
        u8                      tclass;
 };
 
@@ -68,7 +64,7 @@ struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags,
                fib_rule_put(arg.rule);
 
        if (arg.result)
-               return (struct dst_entry *) arg.result;
+               return arg.result;
 
        dst_hold(&ip6_null_entry.u.dst);
        return &ip6_null_entry.u.dst;
@@ -100,8 +96,27 @@ static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
        if (table)
                rt = lookup(table, flp, flags);
 
-       if (rt != &ip6_null_entry)
+       if (rt != &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;
+                       if (ipv6_get_saddr(&rt->u.dst, &flp->fl6_dst,
+                                          &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;
@@ -118,32 +133,32 @@ static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
 {
        struct fib6_rule *r = (struct fib6_rule *) rule;
 
-       if (!ipv6_prefix_equal(&fl->fl6_dst, &r->dst.addr, r->dst.plen))
+       if (r->dst.plen &&
+           !ipv6_prefix_equal(&fl->fl6_dst, &r->dst.addr, r->dst.plen))
                return 0;
 
-       if ((flags & RT6_LOOKUP_F_HAS_SADDR) &&
-           !ipv6_prefix_equal(&fl->fl6_src, &r->src.addr, r->src.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) {
+                       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;
+       }
 
        if (r->tclass && r->tclass != ((ntohl(fl->fl6_flowlabel) >> 20) & 0xff))
                return 0;
 
-#ifdef CONFIG_IPV6_ROUTE_FWMARK
-       if ((r->fwmark ^ fl->fl6_fwmark) & r->fwmask)
-               return 0;
-#endif
-
        return 1;
 }
 
 static struct nla_policy fib6_rule_policy[FRA_MAX+1] __read_mostly = {
-       [FRA_IFNAME]    = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
-       [FRA_PRIORITY]  = { .type = NLA_U32 },
-       [FRA_SRC]       = { .len = sizeof(struct in6_addr) },
-       [FRA_DST]       = { .len = sizeof(struct in6_addr) },
-       [FRA_FWMARK]    = { .type = NLA_U32 },
-       [FRA_FWMASK]    = { .type = NLA_U32 },
-       [FRA_TABLE]     = { .type = NLA_U32 },
+       FRA_GENERIC_POLICY,
 };
 
 static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
@@ -153,10 +168,6 @@ static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
        int err = -EINVAL;
        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;
@@ -167,31 +178,14 @@ static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
                }
        }
 
-       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));
 
-#ifdef CONFIG_IPV6_ROUTE_FWMARK
-       if (tb[FRA_FWMARK]) {
-               rule6->fwmark = nla_get_u32(tb[FRA_FWMARK]);
-               if (rule6->fwmark) {
-                       /*
-                        * if the mark value is non-zero,
-                        * all bits are compared by default
-                        * unless a mask is explicitly specified.
-                        */
-                       rule6->fwmask = 0xFFFFFFFF;
-               }
-       }
-
-       if (tb[FRA_FWMASK])
-               rule6->fwmask = nla_get_u32(tb[FRA_FWMASK]);
-#endif
-
        rule6->src.plen = frh->src_len;
        rule6->dst.plen = frh->dst_len;
        rule6->tclass = frh->tos;
@@ -215,22 +209,14 @@ 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;
 
-#ifdef CONFIG_IPV6_ROUTE_FWMARK
-       if (tb[FRA_FWMARK] && (rule6->fwmark != nla_get_u32(tb[FRA_FWMARK])))
-               return 0;
-
-       if (tb[FRA_FWMASK] && (rule6->fwmask != nla_get_u32(tb[FRA_FWMASK])))
-               return 0;
-#endif
-
        return 1;
 }
 
@@ -252,39 +238,34 @@ static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
                NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr),
                        &rule6->src.addr);
 
-#ifdef CONFIG_IPV6_ROUTE_FWMARK
-       if (rule6->fwmark)
-               NLA_PUT_U32(skb, FRA_FWMARK, rule6->fwmark);
-
-       if (rule6->fwmask || rule6->fwmark)
-               NLA_PUT_U32(skb, FRA_FWMASK, rule6->fwmask);
-#endif
-
        return 0;
 
 nla_put_failure:
        return -ENOBUFS;
 }
 
-int fib6_rules_dump(struct sk_buff *skb, struct netlink_callback *cb)
+static u32 fib6_rule_default_pref(void)
 {
-       return fib_rules_dump(skb, cb, AF_INET6);
+       return 0x3FFF;
 }
 
-static u32 fib6_rule_default_pref(void)
+static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
 {
-       return 0x3FFF;
+       return nla_total_size(16) /* dst */
+              + nla_total_size(16); /* src */
 }
 
 static struct fib_rules_ops fib6_rules_ops = {
        .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,
        .compare                = fib6_rule_compare,
        .fill                   = fib6_rule_fill,
        .default_pref           = fib6_rule_default_pref,
+       .nlmsg_payload          = fib6_rule_nlmsg_payload,
        .nlgroup                = RTNLGRP_IPV6_RULE,
        .policy                 = fib6_rule_policy,
        .rules_list             = &fib6_rules,