Skip to contentSkip to conversation

Fix source prefix routing problems when source address undefined.

RFC PATCH Applied Ville Nuorvala 3 participants 8 messages Oct 17, 2006

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 13/13 Fix source prefix routing problems when source address undefined.

With IPv6 routing subtrees we need to take into account that the source address is typically not specified at the time of the route lookup.

There are two separate cases where this can happen. In the typical case the source address hasn't been selected before the route lookup. Skipping a source prefix policy rule because of this will lead to inconsistent routing behavior between for example bound and unbound sockets.

We avoid this by passing the policy rule source prefix to the lookup and source address selection functions. For source prefix rules the source address is selected before the route lookup, otherwise we do it the other way around. The source address selection algorithm remains virtually unchanged; the source prefix is just used to verify the selected address is compatible with the rule. If the source address doesn't match, the route lookup with the current rule is aborted and is started again with the next rule in the policy chain.

The more uncommon case is where the unspecified address is actually used as a valid source address. When the kernel uses the unspecified address it doesn't touch the routing table. We need to make sure a userland application using a raw socket can do the same. If the user includes the IPv6 header we therefore have to bypass the source address selection even then the source address is unspecified. In addition, we don't insert any routing cache entry created by such a lookup.

---
 include/net/addrconf.h |    4 +++-
 include/net/ip6_fib.h  |   16 +++++++++++++++-
 net/ipv6/addrconf.c    |   13 +++++++++++--
 net/ipv6/fib6_rules.c  |   16 ++--------------
 net/ipv6/ip6_fib.c     |    2 +-
 net/ipv6/ndisc.c       |    2 +-
 net/ipv6/route.c       |   41 +++++++++++++++++++++++++++++++++--------
 7 files changed, 66 insertions(+), 28 deletions(-)
include/net/addrconf.h +3 -1
index d075693..7066362 100644
@@ -67,8 +67,10 @@ #endif
 extern struct inet6_ifaddr *	ipv6_get_ifaddr(struct in6_addr *addr,
 						struct net_device *dev,
 						int strict);
-extern int			ipv6_get_saddr(int pref_if,
+struct rt6key;
+extern int			ipv6_get_saddr(int pref_if,
 					       struct in6_addr *daddr,
+					       struct rt6key *sconstr,
 					       struct in6_addr *saddr);
 extern int			ipv6_get_lladdr(struct net_device *dev, struct in6_addr *);
 extern int			ipv6_rcv_saddr_equal(const struct sock *sk,
include/net/ip6_fib.h +15 -1
index e4438de..8887b5c 100644
@@ -21,6 +21,7 @@ #include <linux/spinlock.h>
 #include <net/dst.h>
 #include <net/flow.h>
 #include <net/netlink.h>
+#include <net/fib_rules.h>
 
 struct rt6_info;
 
@@ -77,6 +78,18 @@ struct rt6key
 	int		plen;
 };
 
+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;
+};
+
 struct fib6_table;
 
 struct rt6_info
@@ -174,7 +187,8 @@ #define RT6_TABLE_LOCAL		RT6_TABLE_MAIN
 #endif
 
 typedef struct rt6_info *(*pol_lookup_t)(struct fib6_table *,
-					 struct flowi *, int);
+					 struct flowi *, int,
+					 struct fib6_rule *);
 
 /*
  *	exported functions
net/ipv6/addrconf.c +11 -2
index 09a22c8..486af76 100644
@@ -904,7 +904,8 @@ static int inline ipv6_saddr_label(const
 	return 1;
 }
 
-int ipv6_get_saddr(int pref_if, struct in6_addr *daddr, struct in6_addr *saddr)
+int ipv6_get_saddr(int pref_if, struct in6_addr *daddr,
+		   struct rt6key *sconstr, struct in6_addr *saddr)
 {
 	struct ipv6_saddr_score hiscore;
 	struct inet6_ifaddr *ifa_result = NULL;
@@ -1151,7 +1152,15 @@ record_it:
 
 	if (!ifa_result)
 		return -EADDRNOTAVAIL;
-	
+#ifdef CONFIG_IPV6_SUBTREES
+	/* Don't let source address based routing interfere with the
+	   address selection, just make sure the selected address
+	   matches the routing policy constraints */
+
+	if (sconstr && sconstr->plen > 0 &&
+	    !ipv6_prefix_equal(saddr, &sconstr->addr, sconstr->plen))
+		return -EADDRNOTAVAIL;
+#endif
 	ipv6_addr_copy(saddr, &ifa_result->addr);
 	in6_ifa_put(ifa_result);
 	return 0;
net/ipv6/fib6_rules.c +2 -14
index fc56a19..a5b7803 100644
@@ -21,18 +21,6 @@ #include <net/ipv6.h>
 #include <net/ip6_route.h>
 #include <net/netlink.h>
 
-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;
-};
-
 static struct fib_rules_ops fib6_rules_ops;
 
 static struct fib6_rule main_rule = {
@@ -98,7 +86,7 @@ static int fib6_rule_action(struct fib_r
 
 	table = fib6_get_table(rule->table);
 	if (table)
-		rt = lookup(table, flp, flags);
+		rt = lookup(table, flp, flags, (struct fib6_rule *) rule);
 
 	if (rt != &ip6_null_entry)
 		goto out;
@@ -123,7 +111,7 @@ static int fib6_rule_match(struct fib_ru
 		return 0;
 
 	if (r->src.plen) {
-		if (!(flags & RT6_LOOKUP_F_HAS_SADDR) ||
+		if (flags & RT6_LOOKUP_F_HAS_SADDR &&
 		    !ipv6_prefix_equal(&fl->fl6_src, &r->src.addr, r->src.plen))
 			return 0;
 	}
net/ipv6/ip6_fib.c +1 -1
index 8fcae7a..c77e44b 100644
@@ -280,7 +280,7 @@ struct fib6_table *fib6_get_table(u32 id
 struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags,
 				   pol_lookup_t lookup)
 {
-	return (struct dst_entry *) lookup(&fib6_main_tbl, fl, flags);
+	return (struct dst_entry *) lookup(&fib6_main_tbl, fl, flags, NULL);
 }
 
 static void __init fib6_tables_init(void)
net/ipv6/ndisc.c +1 -1
index 3ac4e12..809132f 100644
@@ -449,7 +449,7 @@ static void ndisc_send_na(struct net_dev
 		src_addr = solicited_addr;
 		in6_ifa_put(ifp);
 	} else {
-		if (ipv6_get_saddr(dev->ifindex, daddr, &tmpaddr))
+		if (ipv6_get_saddr(dev->ifindex, daddr, NULL, &tmpaddr))
 			return;
 		src_addr = &tmpaddr;
 	}
net/ipv6/route.c +33 -8

95 lines not on this page · raw message

Signature · 1 line
1.4.2.3
#1 lore raw

* Ville Nuorvala <[email protected]> 2006-10-17 03:27

net/ipv6/fib6_rules.c static int fib6_rule_match(struct fib_ru · line 117

Quoted 9 lines
net/ipv6/fib6_rules.c +1 -1
@@ -123,7 +111,7 @@ static int fib6_rule_match(struct fib_ru
 		return 0;
 
 	if (r->src.plen) {
-		if (!(flags & RT6_LOOKUP_F_HAS_SADDR) ||
+		if (flags & RT6_LOOKUP_F_HAS_SADDR &&
 		    !ipv6_prefix_equal(&fl->fl6_src, &r->src.addr, r->src.plen))
 			return 0;
 	}

This is wrong, rules matching on a source prefix must fail if no source address is available.

Quoted 13 lines

static struct rt6_info *ip6_pol_route_output(struct fib6_table *table,

net/ipv6/route.c +4 -1
-					     struct flowi *fl, int flags)
+					     struct flowi *fl, int flags,
+					     struct fib6_rule *rule)
 {
 	struct fib6_node *fn;
 	struct rt6_info *rt, *nrt;
@@ -730,6 +733,16 @@ static struct rt6_info *ip6_pol_route_ou
 	struct in6_addr saddr;
 
 	ipv6_addr_copy(&saddr, &fl->fl6_src);
+#ifdef CONFIG_IPV6_SUBTREES
+	if (!has_saddr && rule->src.plen > 0) {

net/ipv6/route.c static struct rt6_info *ip6_pol_route_ou · line 747

Quoted 10 lines
net/ipv6/route.c +8 -0
+		/* a source prefix specific policy rule has to override the
+		   normal source address selection process */
+		if (ipv6_get_saddr(fl->oif, &fl->fl6_dst, &rule->src, &saddr))
+			goto no_saddr;
+		has_saddr = RT6_LOOKUP_F_HAS_SADDR;
+		ipv6_addr_copy(&fl->fl6_src, &saddr);
+	}
+#endif
 relookup:
 	read_lock_bh(&table->tb6_lock);

I can see why you want the above change but we have to find another way.

#2 lore raw

In article <[email protected]> (at Tue, 17 Oct 2006 12:14:02 +0200), Thomas Graf <[email protected]> says:

Quoted 4 lines

* Ville Nuorvala <[email protected]> 2006-10-17 03:27

Quoted 3 lines
net/ipv6/fib6_rules.c +0 -0
@@ -123,7 +111,7 @@ static int fib6_rule_match(struct fib_ru
 		return 0;
 
Quoted 9 lines

net/ipv6/fib6_rules.c static int fib6_rule_match(struct fib_ru · line 117

Quoted 6 lines
net/ipv6/fib6_rules.c +1 -1
 	if (r->src.plen) {
-		if (!(flags & RT6_LOOKUP_F_HAS_SADDR) ||
+		if (flags & RT6_LOOKUP_F_HAS_SADDR &&
 		    !ipv6_prefix_equal(&fl->fl6_src, &r->src.addr, r->src.plen))
 			return 0;
 	}

This is wrong, rules matching on a source prefix must fail if no source address is available.

Why?

Quoted 10 lines
Quoted 10 lines

static struct rt6_info *ip6_pol_route_output(struct fib6_table *table,

net/ipv6/route.c +2 -1
-					     struct flowi *fl, int flags)
+					     struct flowi *fl, int flags,
+					     struct fib6_rule *rule)
 {
 	struct fib6_node *fn;
 	struct rt6_info *rt, *nrt;
@@ -730,6 +733,16 @@ static struct rt6_info *ip6_pol_route_ou
 	struct in6_addr saddr;
 
Quoted 15 lines

net/ipv6/route.c static struct rt6_info *ip6_pol_route_ou · line 747

Quoted 13 lines
net/ipv6/route.c +10 -0
 	ipv6_addr_copy(&saddr, &fl->fl6_src);
+#ifdef CONFIG_IPV6_SUBTREES
+	if (!has_saddr && rule->src.plen > 0) {
+		/* a source prefix specific policy rule has to override the
+		   normal source address selection process */
+		if (ipv6_get_saddr(fl->oif, &fl->fl6_dst, &rule->src, &saddr))
+			goto no_saddr;
+		has_saddr = RT6_LOOKUP_F_HAS_SADDR;
+		ipv6_addr_copy(&fl->fl6_src, &saddr);
+	}
+#endif
 relookup:
 	read_lock_bh(&table->tb6_lock);

I can see why you want the above change but we have to find another way.

Please explain.

Do you mean that we should decide source address before we look up rule and route?

We have to allow users to use auto-bind; user may NOT supply a source address for the packet.
Routing is done BEFORE the source address selection (RFC3484 Section 7).

We have rule for routing lookup with source address. To get consistent result, source address in the rule should be ignored when matching if user is not supplied the source. Instead, we try finding appropriate source address which is compliant to the rule.

--yoshfuji

#3 lore raw

* YOSHIFUJI Hideaki / ?$B5HF#1QL@ <[email protected]> 2006-10-17 20:00

Quoted 16 lines

In article <[email protected]> (at Tue, 17 Oct 2006 12:14:02 +0200), Thomas Graf <[email protected]> says:

Quoted 4 lines

* Ville Nuorvala <[email protected]> 2006-10-17 03:27

Quoted 3 lines
net/ipv6/fib6_rules.c +0 -0
@@ -123,7 +111,7 @@ static int fib6_rule_match(struct fib_ru
 		return 0;
 
Quoted 9 lines

net/ipv6/fib6_rules.c static int fib6_rule_match(struct fib_ru · line 117

Quoted 6 lines
net/ipv6/fib6_rules.c +1 -1
 	if (r->src.plen) {
-		if (!(flags & RT6_LOOKUP_F_HAS_SADDR) ||
+		if (flags & RT6_LOOKUP_F_HAS_SADDR &&
 		    !ipv6_prefix_equal(&fl->fl6_src, &r->src.addr, r->src.plen))
 			return 0;
 	}

This is wrong, rules matching on a source prefix must fail if no source address is available.

Quoted 1 line

Why?

Because otherwise a rule containing only a source prefix match is equivalent to a catch-all rule for all lookups not providing a source address. An example: Someone adding the rule

	ip rule add from 2001::1/128 unreachable

results in _all_ lookups not providing a source address to resolve to unreachable which means that all source address lookups will fail.

Quoted 2 lines

Do you mean that we should decide source address before we look up rule and route?

Not necessarly, allowing route lookups with a "any" source address and delaying the source address selection is fine but the proposed way of achieving this is not working.

Quoted 3 lines

We have to allow users to use auto-bind; user may NOT supply a source address for the packet.
Routing is done BEFORE the source address selection (RFC3484 Section 7).

I'm sure you know the RFC better than I do but it is my opinion that the RFC is not very clear on this point and doesn't consider the side effects of policy routing and implementation specific problems at all.

Quoted 4 lines

We have rule for routing lookup with source address. To get consistent result, source address in the rule should be ignored when matching if user is not supplied the source. Instead, we try finding appropriate source address which is compliant to the rule.

See the example in my first paragraph and you see this can't possibly work in its current form.

The problem starts that both the routing decision and source address selection is both a routing decision sharing the same logic which are now conflicting as the behaviour for a "from ANY" requires different logic. In order to solve this, rules must be restricted to one of these paths, i.e. a rule intending to make certain prefixes unreachable may not apply to the source selection logic. This can be achieved using the 'reason' field I proposed in my netconf slides, it would allow turning the first rule example into

rule add from 2001::1/128 for INPUT unreachable

which would no longer apply when looking up the source address or deciding the outgoing route.

#4 lore raw

In article <[email protected]> (at Tue, 17 Oct 2006 13:35:11 +0200), Thomas Graf <[email protected]> says:

:

Quoted 10 lines

different logic. In order to solve this, rules must be restricted to one of these paths, i.e. a rule intending to make certain prefixes unreachable may not apply to the source selection logic. This can be achieved using the 'reason' field I proposed in my netconf slides, it would allow turning the first rule example into

rule add from 2001::1/128 for INPUT unreachable

which would no longer apply when looking up the source address or deciding the outgoing route.

And/or, to take into account input interface, we could do

	rule add from 2001::1/128 in eth0 unreachable

maybe.

--yoshfuji

#5 lore raw

* YOSHIFUJI Hideaki / ?$B5HF#1QL@ <[email protected]> 2006-10-17 21:17

Quoted 9 lines

In article <[email protected]> (at Tue, 17 Oct 2006 13:35:11 +0200), Thomas Graf <[email protected]> says:

:

Quoted 6 lines

different logic. In order to solve this, rules must be restricted to one of these paths, i.e. a rule intending to make certain prefixes unreachable may not apply to the source selection logic. This can be achieved using the 'reason' field I proposed in my netconf slides, it would allow turning the first rule example into

Quoted 8 lines
Quoted 4 lines

rule add from 2001::1/128 for INPUT unreachable

which would no longer apply when looking up the source address or deciding the outgoing route.

And/or, to take into account input interface, we could do

	rule add from 2001::1/128 in eth0 unreachable

maybe.

Right, that works as well but duplicating the rule for every interface is not very practical.

Anyways, the point is that the proposed change to modify the source prefix matching logic is not acceptable and other way has to be found.

#6 lore raw

In article <[email protected]> (at Tue, 17 Oct 2006 14:34:15 +0200), Thomas Graf <[email protected]> says:

Quoted 2 lines

Right, that works as well but duplicating the rule for every interface is not very practical.

Though I think it is, but we could have "non-local" flag or such.

Quoted 3 lines

Anyways, the point is that the proposed change to modify the source prefix matching logic is not acceptable and other way has to be found.

I still do not agree.

The "reject" case that you told me can also be solved using netfilter. On the other side, the source address selection can not be solved in other way. The stack is broken without the proposed patch.

--yoshfuji

#7 lore raw
In reply to Thomas Graf

On 10/17/06 14:35, Thomas Graf wrote:

Hi Thomas,

Quoted 9 lines

Because otherwise a rule containing only a source prefix match is equivalent to a catch-all rule for all lookups not providing a source address. An example: Someone adding the rule

	ip rule add from 2001::1/128 unreachable

results in _all_ lookups not providing a source address to resolve to unreachable which means that all source address lookups will fail.

one quick but ugly hack would be to handle FR_ACT_TO_TBL rules differently than the others in fib6_rule_match(). I don't necessarily recommend this approach, but it could work. What do you say?

Quoted 9 lines

The problem starts that both the routing decision and source address selection is both a routing decision sharing the same logic which are now conflicting as the behaviour for a "from ANY" requires different logic. In order to solve this, rules must be restricted to one of these paths, i.e. a rule intending to make certain prefixes unreachable may not apply to the source selection logic. This can be achieved using the 'reason' field I proposed in my netconf slides, it would allow turning the first rule example into

Quoted 4 lines

rule add from 2001::1/128 for INPUT unreachable

which would no longer apply when looking up the source address or deciding the outgoing route.

Is there any reason we couldn't implement this?

BTW I will not really able to participate in this discussion until Thursday, but please continue! Hopefully someone has a working solution that everyone is ok with by then ;-)

I'm currently cooking something up, but I'm not yet sure what will become of it. It might not work at all. It will also change quite a lot of things, so there might be less invasive and cleaner solutions. Let's just say rt6_lookup(), which appears to cause most of these problems, will not be the same...

Regards,
Ville