ipv6: keep track of routes using src
authorPaolo Abeni <pabeni@redhat.com>
Wed, 20 Nov 2019 12:47:34 +0000 (13:47 +0100)
committerDavid S. Miller <davem@davemloft.net>
Thu, 21 Nov 2019 22:45:55 +0000 (14:45 -0800)
Use a per namespace counter, increment it on successful creation
of any route using the source address, decrement it on deletion
of such routes.

This allows us to check easily if the routing decision in the
current namespace depends on the packet source. Will be used
by the next patch.

Suggested-by: David Ahern <dsahern@gmail.com>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
Reviewed-by: David Ahern <dsahern@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/net/ip6_fib.h
include/net/netns/ipv6.h
net/ipv6/ip6_fib.c
net/ipv6/route.c

index 8ac3a59..f1535f1 100644 (file)
@@ -90,7 +90,32 @@ struct fib6_gc_args {
 
 #ifndef CONFIG_IPV6_SUBTREES
 #define FIB6_SUBTREE(fn)       NULL
+
+static inline bool fib6_routes_require_src(const struct net *net)
+{
+       return false;
+}
+
+static inline void fib6_routes_require_src_inc(struct net *net) {}
+static inline void fib6_routes_require_src_dec(struct net *net) {}
+
 #else
+
+static inline bool fib6_routes_require_src(const struct net *net)
+{
+       return net->ipv6.fib6_routes_require_src > 0;
+}
+
+static inline void fib6_routes_require_src_inc(struct net *net)
+{
+       net->ipv6.fib6_routes_require_src++;
+}
+
+static inline void fib6_routes_require_src_dec(struct net *net)
+{
+       net->ipv6.fib6_routes_require_src--;
+}
+
 #define FIB6_SUBTREE(fn)       (rcu_dereference_protected((fn)->subtree, 1))
 #endif
 
@@ -212,6 +237,11 @@ static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst)
        return ((struct rt6_info *)dst)->rt6i_idev;
 }
 
+static inline bool fib6_requires_src(const struct fib6_info *rt)
+{
+       return rt->fib6_src.plen > 0;
+}
+
 static inline void fib6_clean_expires(struct fib6_info *f6i)
 {
        f6i->fib6_flags &= ~RTF_EXPIRES;
index 022a0fd..5ec0544 100644 (file)
@@ -83,6 +83,9 @@ struct netns_ipv6 {
 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
        unsigned int            fib6_rules_require_fldissect;
        bool                    fib6_has_custom_rules;
+#ifdef CONFIG_IPV6_SUBTREES
+       unsigned int            fib6_routes_require_src;
+#endif
        struct rt6_info         *ip6_prohibit_entry;
        struct rt6_info         *ip6_blk_hole_entry;
        struct fib6_table       *fib6_local_tbl;
index f66bc2a..7bae6a9 100644 (file)
@@ -1461,6 +1461,8 @@ out:
                }
 #endif
                goto failure;
+       } else if (fib6_requires_src(rt)) {
+               fib6_routes_require_src_inc(info->nl_net);
        }
        return err;
 
@@ -1933,6 +1935,8 @@ int fib6_del(struct fib6_info *rt, struct nl_info *info)
                struct fib6_info *cur = rcu_dereference_protected(*rtp,
                                        lockdep_is_held(&table->tb6_lock));
                if (rt == cur) {
+                       if (fib6_requires_src(cur))
+                               fib6_routes_require_src_dec(info->nl_net);
                        fib6_del_route(table, fn, rtp, info);
                        return 0;
                }
index edcb525..c92b367 100644 (file)
@@ -6199,6 +6199,9 @@ static int __net_init ip6_route_net_init(struct net *net)
        dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
                         ip6_template_metrics, true);
        INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached);
+#ifdef CONFIG_IPV6_SUBTREES
+       net->ipv6.fib6_routes_require_src = 0;
+#endif
 #endif
 
        net->ipv6.sysctl.flush_delay = 0;