From patchwork Wed Oct 18 20:10:07 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Herbert X-Patchwork-Id: 827813 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=quantonium-net.20150623.gappssmtp.com header.i=@quantonium-net.20150623.gappssmtp.com header.b="MN8Fy4sV"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3yHNWS32WMz9t48 for ; Thu, 19 Oct 2017 07:12:04 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751612AbdJRUMC (ORCPT ); Wed, 18 Oct 2017 16:12:02 -0400 Received: from mail-pg0-f66.google.com ([74.125.83.66]:43403 "EHLO mail-pg0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751585AbdJRUK6 (ORCPT ); Wed, 18 Oct 2017 16:10:58 -0400 Received: by mail-pg0-f66.google.com with SMTP id s75so5163844pgs.0 for ; Wed, 18 Oct 2017 13:10:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quantonium-net.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=UfZXeb1MTZRBCMM+Pu5hKfwabos3PKc1FqGX5taghFY=; b=MN8Fy4sVxTph9ndanvTzljA41FB9MqcY4mU4IFXtPPkzg/3scE0KQUnjI6YoRrZ1Nd WZlP6ksBCPYCvChMqxx+aOiW01fEYL2b0uxVYpPFn49mUPj/AJG2YNdeHPkdzdhRUq5v LgohzuN0HxlV+O1NyUnLhC/n++KiIdrOcG6Ibjr4duxHQ8ZalTxwipvi+JH06nIpJn51 A/qCLutWPBBIprWkW4kAOS4DC+9mMr5ZWoYSqATg+RmEKpon+dVTZlKpUI30Bop3bkam Nzxv77U3IIJo/iaGL3QFzexO5OuRuSzQAAelHi1xmaL3t+XON3CKfiNfrCGj7kSro6UD zjFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=UfZXeb1MTZRBCMM+Pu5hKfwabos3PKc1FqGX5taghFY=; b=fS6FDKaPKhw8q3sbJCEHWXngscD5xbKfMjP5w+zSCEnAdhTkZxtI2Q65kBZsllC9Y9 /T6rODxAw3wdqaKwunT0+aG3gwmxbrDwO01HrNAE+hAyck5zHkrGuak4F7KunNyADuCN 9Xhr1fsi1nTlBTjb7KCQ0LV1VnQMEKTi3rAK8jYnReu1+zyH3ZqAbr2qUocp/WrnWCWK gVRCKKHuDYlRGCnOz2Q7Yz9EbI3wDaEaVEjLrqs6W1JwOvU+MTWBHud3QGhWQLh77up7 zguA5730c3cGkZ84IzxH4ysIeZpA7uWAAkmMs1N1w6JOcodIZX53CPVBnD7LojxK7iU5 m3Bw== X-Gm-Message-State: AMCzsaVaVcRO6FPzUAn0FmIOdTpIAs+3RpM98otUBu8s9GoKwtgFkZAI MctxRA/ANug9m9FhevZPE/Q/tg== X-Google-Smtp-Source: ABhQp+TqYfqfqoWNnOO9+hlnKMjmkFFzD1qU+p0IMEyleftL1Lsv7m7yRiman4XT3XKICUgN2gMLOA== X-Received: by 10.84.232.202 with SMTP id x10mr12268234plm.101.1508357458115; Wed, 18 Oct 2017 13:10:58 -0700 (PDT) Received: from localhost.localdomain (c-73-162-13-107.hsd1.ca.comcast.net. [73.162.13.107]) by smtp.gmail.com with ESMTPSA id l79sm26424108pfb.33.2017.10.18.13.10.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 18 Oct 2017 13:10:57 -0700 (PDT) From: Tom Herbert To: davem@davemloft.net Cc: pablo@netfilter.org, laforge@gnumonks.org, aschultz@tpip.net, netdev@vger.kernel.org, rohit@quantonium.net, Tom Herbert Subject: [PATCH v5 net-next 01/12] iptunnel: Add common functions to get a tunnel route Date: Wed, 18 Oct 2017 13:10:07 -0700 Message-Id: <20171018201018.5692-2-tom@quantonium.net> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171018201018.5692-1-tom@quantonium.net> References: <20171018201018.5692-1-tom@quantonium.net> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org ip_tunnel_get_route and ip6_tnl_get_route are created to return routes for a tunnel. These functions are derived from the VXLAN functions. Signed-off-by: Tom Herbert --- include/net/ip6_tunnel.h | 33 +++++++++++++++++++++++++++++++++ include/net/ip_tunnels.h | 32 ++++++++++++++++++++++++++++++++ net/ipv4/ip_tunnel.c | 40 ++++++++++++++++++++++++++++++++++++++++ net/ipv6/route.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+) diff --git a/include/net/ip6_tunnel.h b/include/net/ip6_tunnel.h index 08fbc7f7d8d7..f84325aacdaf 100644 --- a/include/net/ip6_tunnel.h +++ b/include/net/ip6_tunnel.h @@ -142,6 +142,39 @@ __u32 ip6_tnl_get_cap(struct ip6_tnl *t, const struct in6_addr *laddr, struct net *ip6_tnl_get_link_net(const struct net_device *dev); int ip6_tnl_get_iflink(const struct net_device *dev); int ip6_tnl_change_mtu(struct net_device *dev, int new_mtu); +struct dst_entry *__ip6_tnl_get_route(struct net_device *dev, + struct sk_buff *skb, struct sock *sk, + u8 proto, int oif, u8 tos, __be32 label, + const struct in6_addr *daddr, + struct in6_addr *saddr, + __be16 dport, __be16 sport, + struct dst_cache *dst_cache, + bool use_cache); + +static inline struct dst_entry *ip6_tnl_get_route(struct net_device *dev, + struct sk_buff *skb, struct sock *sk, u8 proto, + int oif, u8 tos, __be32 label, + const struct in6_addr *daddr, + struct in6_addr *saddr, + __be16 dport, __be16 sport, + struct dst_cache *dst_cache, + const struct ip_tunnel_info *info) +{ + bool use_cache = (ip_tunnel_dst_cache_usable(skb, info) && + (!tos || info)); + +#if IS_ENABLED(CONFIG_IPV6) + if (use_cache) { + struct dst_entry *ndst = dst_cache_get_ip6(dst_cache, saddr); + + if (ndst) + return ndst; + } +#endif + + return __ip6_tnl_get_route(dev, skb, sk, proto, oif, tos, label, daddr, + saddr, dport, sport, dst_cache, use_cache); +} static inline void ip6tunnel_xmit(struct sock *sk, struct sk_buff *skb, struct net_device *dev) diff --git a/include/net/ip_tunnels.h b/include/net/ip_tunnels.h index b41a1e057fce..2b05ae24f4f6 100644 --- a/include/net/ip_tunnels.h +++ b/include/net/ip_tunnels.h @@ -285,6 +285,38 @@ int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[], struct ip_tunnel_parm *p, __u32 fwmark); void ip_tunnel_setup(struct net_device *dev, unsigned int net_id); +struct rtable *__ip_tunnel_get_route(struct net_device *dev, + struct sk_buff *skb, u8 proto, + int oif, u8 tos, + __be32 daddr, __be32 *saddr, + __be16 dport, __be16 sport, + struct dst_cache *dst_cache, + bool use_cache); + +static inline struct rtable *ip_tunnel_get_route(struct net_device *dev, + struct sk_buff *skb, u8 proto, + int oif, u8 tos, + __be32 daddr, __be32 *saddr, + __be16 dport, __be16 sport, + struct dst_cache *dst_cache, + const struct ip_tunnel_info *info) +{ + bool use_cache = (ip_tunnel_dst_cache_usable(skb, info) && + (!tos || info)); + + if (use_cache) { + struct rtable *rt; + + rt = dst_cache_get_ip4(dst_cache, saddr); + if (rt) + return rt; + } + + return __ip_tunnel_get_route(dev, skb, proto, oif, tos, + daddr, saddr, dport, sport, + dst_cache, use_cache); +} + struct ip_tunnel_encap_ops { size_t (*encap_hlen)(struct ip_tunnel_encap *e); int (*build_header)(struct sk_buff *skb, struct ip_tunnel_encap *e, diff --git a/net/ipv4/ip_tunnel.c b/net/ipv4/ip_tunnel.c index fe6fee728ce4..c97525d8dff9 100644 --- a/net/ipv4/ip_tunnel.c +++ b/net/ipv4/ip_tunnel.c @@ -935,6 +935,46 @@ int ip_tunnel_ioctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd) } EXPORT_SYMBOL_GPL(ip_tunnel_ioctl); +struct rtable *__ip_tunnel_get_route(struct net_device *dev, + struct sk_buff *skb, u8 proto, + int oif, u8 tos, + __be32 daddr, __be32 *saddr, + __be16 dport, __be16 sport, + struct dst_cache *dst_cache, + bool use_cache) +{ + struct rtable *rt = NULL; + struct flowi4 fl4; + + memset(&fl4, 0, sizeof(fl4)); + fl4.flowi4_oif = oif; + fl4.flowi4_tos = RT_TOS(tos); + fl4.flowi4_mark = skb->mark; + fl4.flowi4_proto = proto; + fl4.daddr = daddr; + fl4.saddr = *saddr; + fl4.fl4_dport = dport; + fl4.fl4_sport = sport; + + rt = ip_route_output_key(dev_net(dev), &fl4); + if (likely(!IS_ERR(rt))) { + if (rt->dst.dev == dev) { + netdev_dbg(dev, "circular route to %pI4\n", &daddr); + ip_rt_put(rt); + return ERR_PTR(-ELOOP); + } + + *saddr = fl4.saddr; + if (use_cache) + dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr); + } else { + netdev_dbg(dev, "no route to %pI4\n", &daddr); + return ERR_PTR(-ENETUNREACH); + } + return rt; +} +EXPORT_SYMBOL_GPL(__ip_tunnel_get_route); + int __ip_tunnel_change_mtu(struct net_device *dev, int new_mtu, bool strict) { struct ip_tunnel *tunnel = netdev_priv(dev); diff --git a/net/ipv6/route.c b/net/ipv6/route.c index 2e8842fa6450..3b0c9e6de954 100644 --- a/net/ipv6/route.c +++ b/net/ipv6/route.c @@ -2449,6 +2449,48 @@ static struct rt6_info *ip6_nh_lookup_table(struct net *net, return rt; } +struct dst_entry *__ip6_tnl_get_route(struct net_device *dev, + struct sk_buff *skb, struct sock *sk, + u8 proto, int oif, u8 tos, __be32 label, + const struct in6_addr *daddr, + struct in6_addr *saddr, + __be16 dport, __be16 sport, + struct dst_cache *dst_cache, + bool use_cache) +{ + struct dst_entry *ndst; + struct flowi6 fl6; + int err; + + memset(&fl6, 0, sizeof(fl6)); + fl6.flowi6_oif = oif; + fl6.daddr = *daddr; + fl6.saddr = *saddr; + fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label); + fl6.flowi6_mark = skb->mark; + fl6.flowi6_proto = proto; + fl6.fl6_dport = dport; + fl6.fl6_sport = sport; + + err = ipv6_stub->ipv6_dst_lookup(dev_net(dev), sk, &ndst, &fl6); + if (unlikely(err < 0)) { + netdev_dbg(dev, "no route to %pI6\n", daddr); + return ERR_PTR(-ENETUNREACH); + } + + if (unlikely(ndst->dev == dev)) { + netdev_dbg(dev, "circular route to %pI6\n", daddr); + dst_release(ndst); + return ERR_PTR(-ELOOP); + } + + *saddr = fl6.saddr; + if (use_cache) + dst_cache_set_ip6(dst_cache, ndst, saddr); + return ndst; +} +EXPORT_SYMBOL_GPL(__ip6_tnl_get_route); + static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg, struct netlink_ext_ack *extack) {