netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net-next 0/3] net/ipv6: Add support for ONLINK flag
@ 2018-01-24  3:00 David Ahern
  2018-01-24  3:00 ` [PATCH net-next 1/3] net/ipv6: Move gateway validation into helper David Ahern
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: David Ahern @ 2018-01-24  3:00 UTC (permalink / raw)
  To: netdev; +Cc: yoshfuji, idosch, roopa, David Ahern

Add support for RTNH_F_ONLINK with ipv6 routes.

First patch moves existing gateway validation into helper. The onlink
flag requires a different set of checks and the existing validation
makes ip6_route_info_create long enough.

Second patch makes the RT6_LOOKUP_F_IFACE flag an option to 
ip6_nh_lookup_table. onlink check needs to verify the gateway
without the flag.

Third patch adds support for RTNH_F_ONLINK.

David Ahern (3):
  net/ipv6: Move gateway validation into helper
  net/ipv6: Add flags arg to ip6_nh_lookup_table
  net/ipv6: Add support for onlink flag

 net/ipv6/route.c | 155 ++++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 119 insertions(+), 36 deletions(-)

-- 
2.11.0

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH net-next 1/3] net/ipv6: Move gateway validation into helper
  2018-01-24  3:00 [PATCH net-next 0/3] net/ipv6: Add support for ONLINK flag David Ahern
@ 2018-01-24  3:00 ` David Ahern
  2018-01-24  3:00 ` [PATCH net-next 2/3] net/ipv6: Add flags arg to ip6_nh_lookup_table David Ahern
  2018-01-24  3:00 ` [PATCH net-next 3/3] net/ipv6: Add support for onlink flag David Ahern
  2 siblings, 0 replies; 6+ messages in thread
From: David Ahern @ 2018-01-24  3:00 UTC (permalink / raw)
  To: netdev; +Cc: yoshfuji, idosch, roopa, David Ahern

Move existing code to validate nexthop into a helper. Next patch adds
support for nexthops marked with onlink and this helper keeps the
complexity of ip6_route_info_create in check.

Signed-off-by: David Ahern <dsahern@gmail.com>
---
 net/ipv6/route.c | 85 ++++++++++++++++++++++++++++++++------------------------
 1 file changed, 49 insertions(+), 36 deletions(-)

diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index f85da2f1e729..0ff4ca0948f0 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -2469,6 +2469,54 @@ static struct rt6_info *ip6_nh_lookup_table(struct net *net,
 	return rt;
 }
 
+static int ip6_route_check_nh(struct net *net,
+			      struct fib6_config *cfg,
+			      struct net_device **_dev,
+			      struct inet6_dev **idev)
+{
+	const struct in6_addr *gw_addr = &cfg->fc_gateway;
+	struct net_device *dev = _dev ? *_dev : NULL;
+	struct rt6_info *grt = NULL;
+	int err = -EHOSTUNREACH;
+
+	if (cfg->fc_table) {
+		grt = ip6_nh_lookup_table(net, cfg, gw_addr);
+		if (grt) {
+			if (grt->rt6i_flags & RTF_GATEWAY ||
+			    (dev && dev != grt->dst.dev)) {
+				ip6_rt_put(grt);
+				grt = NULL;
+			}
+		}
+	}
+
+	if (!grt)
+		grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
+
+	if (!grt)
+		goto out;
+
+	if (dev) {
+		if (dev != grt->dst.dev) {
+			ip6_rt_put(grt);
+			goto out;
+		}
+	} else {
+		*_dev = dev = grt->dst.dev;
+		*idev = grt->rt6i_idev;
+		dev_hold(dev);
+		in6_dev_hold(grt->rt6i_idev);
+	}
+
+	if (!(grt->rt6i_flags & RTF_GATEWAY))
+		err = 0;
+
+	ip6_rt_put(grt);
+
+out:
+	return err;
+}
+
 static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg,
 					      struct netlink_ext_ack *extack)
 {
@@ -2664,8 +2712,6 @@ static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg,
 		rt->rt6i_gateway = *gw_addr;
 
 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
-			struct rt6_info *grt = NULL;
-
 			/* IPv6 strictly inhibits using not link-local
 			   addresses as nexthop address.
 			   Otherwise, router will not able to send redirects.
@@ -2682,40 +2728,7 @@ static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg,
 				goto out;
 			}
 
-			if (cfg->fc_table) {
-				grt = ip6_nh_lookup_table(net, cfg, gw_addr);
-
-				if (grt) {
-					if (grt->rt6i_flags & RTF_GATEWAY ||
-					    (dev && dev != grt->dst.dev)) {
-						ip6_rt_put(grt);
-						grt = NULL;
-					}
-				}
-			}
-
-			if (!grt)
-				grt = rt6_lookup(net, gw_addr, NULL,
-						 cfg->fc_ifindex, 1);
-
-			err = -EHOSTUNREACH;
-			if (!grt)
-				goto out;
-			if (dev) {
-				if (dev != grt->dst.dev) {
-					ip6_rt_put(grt);
-					goto out;
-				}
-			} else {
-				dev = grt->dst.dev;
-				idev = grt->rt6i_idev;
-				dev_hold(dev);
-				in6_dev_hold(grt->rt6i_idev);
-			}
-			if (!(grt->rt6i_flags & RTF_GATEWAY))
-				err = 0;
-			ip6_rt_put(grt);
-
+			err = ip6_route_check_nh(net, cfg, &dev, &idev);
 			if (err)
 				goto out;
 		}
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH net-next 2/3] net/ipv6: Add flags arg to ip6_nh_lookup_table
  2018-01-24  3:00 [PATCH net-next 0/3] net/ipv6: Add support for ONLINK flag David Ahern
  2018-01-24  3:00 ` [PATCH net-next 1/3] net/ipv6: Move gateway validation into helper David Ahern
@ 2018-01-24  3:00 ` David Ahern
  2018-01-24  3:00 ` [PATCH net-next 3/3] net/ipv6: Add support for onlink flag David Ahern
  2 siblings, 0 replies; 6+ messages in thread
From: David Ahern @ 2018-01-24  3:00 UTC (permalink / raw)
  To: netdev; +Cc: yoshfuji, idosch, roopa, David Ahern

onlink verification needs to do a lookup without the
RT6_LOOKUP_F_IFACE flag. Change ip6_nh_lookup_table to take a
flags argument and pass RT6_LOOKUP_F_IFACE. Both verifications
want to ignore link state, so add that in the lookup helper.

Signed-off-by: David Ahern <dsahern@gmail.com>
---
 net/ipv6/route.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index 0ff4ca0948f0..00acf0103037 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -2440,7 +2440,8 @@ static int ip6_convert_metrics(struct mx6_config *mxc,
 
 static struct rt6_info *ip6_nh_lookup_table(struct net *net,
 					    struct fib6_config *cfg,
-					    const struct in6_addr *gw_addr)
+					    const struct in6_addr *gw_addr,
+					    int flags)
 {
 	struct flowi6 fl6 = {
 		.flowi6_oif = cfg->fc_ifindex,
@@ -2449,7 +2450,6 @@ static struct rt6_info *ip6_nh_lookup_table(struct net *net,
 	};
 	struct fib6_table *table;
 	struct rt6_info *rt;
-	int flags = RT6_LOOKUP_F_IFACE | RT6_LOOKUP_F_IGNORE_LINKSTATE;
 
 	table = fib6_get_table(net, cfg->fc_table);
 	if (!table)
@@ -2458,6 +2458,7 @@ static struct rt6_info *ip6_nh_lookup_table(struct net *net,
 	if (!ipv6_addr_any(&cfg->fc_prefsrc))
 		flags |= RT6_LOOKUP_F_HAS_SADDR;
 
+	flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
 	rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, flags);
 
 	/* if table lookup failed, fall back to full lookup */
@@ -2480,7 +2481,9 @@ static int ip6_route_check_nh(struct net *net,
 	int err = -EHOSTUNREACH;
 
 	if (cfg->fc_table) {
-		grt = ip6_nh_lookup_table(net, cfg, gw_addr);
+		int flags = RT6_LOOKUP_F_IFACE;
+
+		grt = ip6_nh_lookup_table(net, cfg, gw_addr, flags);
 		if (grt) {
 			if (grt->rt6i_flags & RTF_GATEWAY ||
 			    (dev && dev != grt->dst.dev)) {
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH net-next 3/3] net/ipv6: Add support for onlink flag
  2018-01-24  3:00 [PATCH net-next 0/3] net/ipv6: Add support for ONLINK flag David Ahern
  2018-01-24  3:00 ` [PATCH net-next 1/3] net/ipv6: Move gateway validation into helper David Ahern
  2018-01-24  3:00 ` [PATCH net-next 2/3] net/ipv6: Add flags arg to ip6_nh_lookup_table David Ahern
@ 2018-01-24  3:00 ` David Ahern
  2018-01-24 22:08   ` David Ahern
  2 siblings, 1 reply; 6+ messages in thread
From: David Ahern @ 2018-01-24  3:00 UTC (permalink / raw)
  To: netdev; +Cc: yoshfuji, idosch, roopa, David Ahern

Similar to IPv4 allow routes to be added with the RTNH_F_ONLINK flag.
The onlink option requires a gateway and a nexthop device. Any unicast
gateway is allowed (including IPv4 mapped addresses and unresolved
ones) as long as the gateway is not a local address and if it resolves
it must match the given device.

Signed-off-by: David Ahern <dsahern@gmail.com>
---
 net/ipv6/route.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 68 insertions(+), 1 deletion(-)

diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index 00acf0103037..842b2646f1c0 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -2470,6 +2470,58 @@ static struct rt6_info *ip6_nh_lookup_table(struct net *net,
 	return rt;
 }
 
+static int ip6_route_onlink_check(struct fib6_config *cfg,
+				  struct net_device *dev,
+				  struct netlink_ext_ack *extack)
+{
+	u32 tbid;
+
+	if (!dev) {
+		NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
+		return -ENODEV;
+	}
+
+	if (!(dev->flags & IFF_UP)) {
+		NL_SET_ERR_MSG(extack, "Nexthop device is not up");
+		return -ENETDOWN;
+	}
+
+	tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
+	if (cfg->fc_table && cfg->fc_table != tbid) {
+		NL_SET_ERR_MSG(extack,
+			       "Table id mismatch between given table and device");
+		return -EINVAL;
+	}
+
+	cfg->fc_table = tbid;
+
+	return 0;
+}
+
+static int ip6_route_check_nh_onlink(struct net *net,
+				     struct fib6_config *cfg,
+				     struct net_device *dev,
+				     struct netlink_ext_ack *extack)
+{
+	const struct in6_addr *gw_addr = &cfg->fc_gateway;
+	u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT;
+	struct rt6_info *grt;
+	int err;
+
+	err = 0;
+	grt = ip6_nh_lookup_table(net, cfg, gw_addr, 0);
+	if (grt) {
+		if (grt->rt6i_flags & flags || dev != grt->dst.dev) {
+			NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
+			err = -EINVAL;
+		}
+
+		ip6_rt_put(grt);
+	}
+
+	return err;
+}
+
 static int ip6_route_check_nh(struct net *net,
 			      struct fib6_config *cfg,
 			      struct net_device **_dev,
@@ -2571,6 +2623,12 @@ static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg,
 	if (cfg->fc_metric == 0)
 		cfg->fc_metric = IP6_RT_PRIO_USER;
 
+	if (cfg->fc_flags & RTNH_F_ONLINK) {
+		err = ip6_route_onlink_check(cfg, dev, extack);
+		if (err)
+			goto out;
+	}
+
 	err = -ENOBUFS;
 	if (cfg->fc_nlinfo.nlh &&
 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
@@ -2731,7 +2789,12 @@ static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg,
 				goto out;
 			}
 
-			err = ip6_route_check_nh(net, cfg, &dev, &idev);
+			if (cfg->fc_flags & RTNH_F_ONLINK) {
+				err = ip6_route_check_nh_onlink(net, cfg, dev,
+								extack);
+			} else {
+				err = ip6_route_check_nh(net, cfg, &dev, &idev);
+			}
 			if (err)
 				goto out;
 		}
@@ -2767,6 +2830,7 @@ static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg,
 	if (!(rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
 	    !netif_carrier_ok(dev))
 		rt->rt6i_nh_flags |= RTNH_F_LINKDOWN;
+	rt->rt6i_nh_flags |= (cfg->fc_flags & RTNH_F_ONLINK);
 	rt->dst.dev = dev;
 	rt->rt6i_idev = idev;
 	rt->rt6i_table = table;
@@ -3836,6 +3900,8 @@ static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
 	if (rtm->rtm_flags & RTM_F_CLONED)
 		cfg->fc_flags |= RTF_CACHE;
 
+	cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
+
 	cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
 	cfg->fc_nlinfo.nlh = nlh;
 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
@@ -4241,6 +4307,7 @@ static int rt6_nexthop_info(struct sk_buff *skb, struct rt6_info *rt,
 			goto nla_put_failure;
 	}
 
+	*flags |= (rt->rt6i_nh_flags & RTNH_F_ONLINK);
 	if (rt->rt6i_nh_flags & RTNH_F_OFFLOAD)
 		*flags |= RTNH_F_OFFLOAD;
 
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH net-next 3/3] net/ipv6: Add support for onlink flag
  2018-01-24  3:00 ` [PATCH net-next 3/3] net/ipv6: Add support for onlink flag David Ahern
@ 2018-01-24 22:08   ` David Ahern
  2018-01-24 22:53     ` David Miller
  0 siblings, 1 reply; 6+ messages in thread
From: David Ahern @ 2018-01-24 22:08 UTC (permalink / raw)
  To: netdev, David Miller; +Cc: yoshfuji, idosch, roopa

On 1/23/18 8:00 PM, David Ahern wrote:
> +	tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
> +	if (cfg->fc_table && cfg->fc_table != tbid) {
> +		NL_SET_ERR_MSG(extack,
> +			       "Table id mismatch between given table and device");
> +		return -EINVAL;
> +	}
> +
> +	cfg->fc_table = tbid;
> +
> +	return 0;

This table check is too restrictive for some PBR cases.

Dave: please drop this set; I'll repost.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH net-next 3/3] net/ipv6: Add support for onlink flag
  2018-01-24 22:08   ` David Ahern
@ 2018-01-24 22:53     ` David Miller
  0 siblings, 0 replies; 6+ messages in thread
From: David Miller @ 2018-01-24 22:53 UTC (permalink / raw)
  To: dsahern; +Cc: netdev, yoshfuji, idosch, roopa

From: David Ahern <dsahern@gmail.com>
Date: Wed, 24 Jan 2018 15:08:39 -0700

> On 1/23/18 8:00 PM, David Ahern wrote:
>> +	tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
>> +	if (cfg->fc_table && cfg->fc_table != tbid) {
>> +		NL_SET_ERR_MSG(extack,
>> +			       "Table id mismatch between given table and device");
>> +		return -EINVAL;
>> +	}
>> +
>> +	cfg->fc_table = tbid;
>> +
>> +	return 0;
> 
> This table check is too restrictive for some PBR cases.
> 
> Dave: please drop this set; I'll repost.

Ok.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2018-01-24 22:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-24  3:00 [PATCH net-next 0/3] net/ipv6: Add support for ONLINK flag David Ahern
2018-01-24  3:00 ` [PATCH net-next 1/3] net/ipv6: Move gateway validation into helper David Ahern
2018-01-24  3:00 ` [PATCH net-next 2/3] net/ipv6: Add flags arg to ip6_nh_lookup_table David Ahern
2018-01-24  3:00 ` [PATCH net-next 3/3] net/ipv6: Add support for onlink flag David Ahern
2018-01-24 22:08   ` David Ahern
2018-01-24 22:53     ` David Miller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).