From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9185AC282CB for ; Fri, 8 Feb 2019 16:39:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 566512086C for ; Fri, 8 Feb 2019 16:39:11 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="XGZpO++k" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727633AbfBHQjK (ORCPT ); Fri, 8 Feb 2019 11:39:10 -0500 Received: from mail-vk1-f201.google.com ([209.85.221.201]:42940 "EHLO mail-vk1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727121AbfBHQjI (ORCPT ); Fri, 8 Feb 2019 11:39:08 -0500 Received: by mail-vk1-f201.google.com with SMTP id t192so1350516vkt.9 for ; Fri, 08 Feb 2019 08:39:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=0pEQkkwVAkw5yXx1m77/HxGuqvvyzoWCqGz5B9hBc1Q=; b=XGZpO++kYLi3gqZeu/a3JHtUopU6I3ZOPaPxhWZGOW2/AbLNJDhhfMjUWnJZQx3CE6 WUAscl8e17P6hw+S6f+CGsLWbIGusn/PWrEBEGqbXhTFy1o4lt/Y6PCf46psCHarAt+M viZP7PZm7Y/o9wb6k6fO+VoN7rGDRqwdKQS+wvWPl+Zn+5Wcdz6FwsNFebMNFDsjjd+9 sKfUvnMAOxG4aV6BKhxTRG2mGnSpLYVeH7i5vZfK3A7iurVti3pjDIWlUa0Lfr/iU8xR LTMKqh5b3g7mi+LGYNyW8RWPnFAkQ+rb2jOkQ8LYLetIDUeQZsvoxYbf3l6CZJz/B8ce ub/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=0pEQkkwVAkw5yXx1m77/HxGuqvvyzoWCqGz5B9hBc1Q=; b=nDxV1eAs3/An0XgzJ7BRIl4uvmmF+SRK1/mfekY2fG6j3nIsq5Q4VBMzbzmoHVXClG pBt/jtTBm0S+15FjmMc9IRiw+wmPYSbdvxvXVwiUUTz0XAKmOGX+tPLwzRt8BLqyPNr+ MtrxS7hJTP/oJjiXji/DOhUkLN52LF6pvIk+/0f+4gQHE8o6kXy9nrqvJ30rIHCRN3C/ WL05wwqa+aJTC04aKJGJRxiz8VaS4oLwhUoeTAjnb5C2URuEjGTj7rsIsOa+WMZ/XBHU MamGqS2m8FP8N8rvLHC8k5J0deHH8B7t1OT9qHVvIECw8+B8u+y6Q4tFvJQf85N9mvNT mPQg== X-Gm-Message-State: AHQUAuaVdDoO0rwtDSpcm/8YuKUwfG5kNO5Y7MmIEjxrSwlVSQtYFa0d sflyK+Z437barXuMNaE+wPje4D5w X-Google-Smtp-Source: AHgI3IZernVnCNziw1zR3vbljrhbK3sqJgS3qqhU45EN5D5TUOMxzGLT8lSkoUqCM454eDycRgNxZWeK X-Received: by 2002:a67:df96:: with SMTP id x22mr2182851vsk.32.1549643947018; Fri, 08 Feb 2019 08:39:07 -0800 (PST) Date: Fri, 8 Feb 2019 08:38:47 -0800 In-Reply-To: <20190208163849.151626-1-posk@google.com> Message-Id: <20190208163849.151626-5-posk@google.com> Mime-Version: 1.0 References: <20190208163849.151626-1-posk@google.com> X-Mailer: git-send-email 2.20.1.791.gb4d0f1c61a-goog Subject: [PATCH bpf-next v8 4/6] bpf: add handling of BPF_LWT_REROUTE to lwt_bpf.c From: Peter Oskolkov To: Alexei Starovoitov , Daniel Borkmann , netdev@vger.kernel.org Cc: Peter Oskolkov , David Ahern , Willem de Bruijn , Peter Oskolkov Content-Type: text/plain; charset="UTF-8" Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org This patch builds on top of the previous patch in the patchset, which added BPF_LWT_ENCAP_IP mode to bpf_lwt_push_encap. As the encapping can result in the skb needing to go via a different interface/route/dst, bpf programs can indicate this by returning BPF_LWT_REROUTE, which triggers a new route lookup for the skb. v8 changes: fix kbuild errors when LWTUNNEL_BPF is builtin, but IPV6 is a module: as LWTUNNEL_BPF can only be either Y or N, call IPV6 routing functions only if they are built-in. Signed-off-by: Peter Oskolkov --- net/core/lwt_bpf.c | 134 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) diff --git a/net/core/lwt_bpf.c b/net/core/lwt_bpf.c index 079871fc020f..ebe294de36e1 100644 --- a/net/core/lwt_bpf.c +++ b/net/core/lwt_bpf.c @@ -17,6 +17,7 @@ #include #include #include +#include struct bpf_lwt_prog { struct bpf_prog *prog; @@ -56,6 +57,7 @@ static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt, switch (ret) { case BPF_OK: + case BPF_LWT_REROUTE: break; case BPF_REDIRECT: @@ -88,6 +90,36 @@ static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt, return ret; } +static int bpf_lwt_input_reroute(struct sk_buff *skb) +{ + int err = -EINVAL; + + if (skb->protocol == htons(ETH_P_IP)) { + struct iphdr *iph = ip_hdr(skb); + + err = ip_route_input_noref(skb, iph->daddr, iph->saddr, + iph->tos, skb_dst(skb)->dev); + } else if (skb->protocol == htons(ETH_P_IPV6)) { +#if IS_BUILTIN(CONFIG_IPV6) + ip6_route_input(skb); + err = skb_dst(skb)->error; +#else + pr_warn_once("BPF_LWT_REROUTE input: IPV6 not built-in\n"); +#endif + } else { + pr_warn_once("BPF_LWT_REROUTE input: unsupported proto %d\n", + skb->protocol); + } + + if (err) + goto err; + return dst_input(skb); + +err: + kfree_skb(skb); + return err; +} + static int bpf_input(struct sk_buff *skb) { struct dst_entry *dst = skb_dst(skb); @@ -99,6 +131,8 @@ static int bpf_input(struct sk_buff *skb) ret = run_lwt_bpf(skb, &bpf->in, dst, NO_REDIRECT); if (ret < 0) return ret; + if (ret == BPF_LWT_REROUTE) + return bpf_lwt_input_reroute(skb); } if (unlikely(!dst->lwtstate->orig_input)) { @@ -148,6 +182,95 @@ static int xmit_check_hhlen(struct sk_buff *skb) return 0; } +static int bpf_lwt_xmit_reroute(struct sk_buff *skb) +{ + struct net_device *l3mdev = l3mdev_master_dev_rcu(skb_dst(skb)->dev); + int oif = l3mdev ? l3mdev->ifindex : 0; + struct dst_entry *dst = NULL; + struct sock *sk; + struct net *net; + bool ipv4; + int err; + + if (skb->protocol == htons(ETH_P_IP)) { + ipv4 = true; + } else if (skb->protocol == htons(ETH_P_IPV6)) { + ipv4 = false; + } else { + pr_warn_once("BPF_LWT_REROUTE xmit: unsupported proto %d\n", + skb->protocol); + return -EINVAL; + } + + sk = sk_to_full_sk(skb->sk); + if (sk) { + if (sk->sk_bound_dev_if) + oif = sk->sk_bound_dev_if; + net = sock_net(sk); + } else { + net = dev_net(skb_dst(skb)->dev); + } + + if (ipv4) { + struct iphdr *iph = ip_hdr(skb); + struct flowi4 fl4 = {0}; + struct rtable *rt; + + fl4.flowi4_oif = oif; + fl4.flowi4_mark = skb->mark; + fl4.flowi4_uid = sock_net_uid(net, sk); + fl4.flowi4_tos = RT_TOS(iph->tos); + fl4.flowi4_flags = FLOWI_FLAG_ANYSRC; + fl4.flowi4_proto = iph->protocol; + fl4.daddr = iph->daddr; + fl4.saddr = iph->saddr; + + rt = ip_route_output_key(net, &fl4); + if (IS_ERR(rt) || rt->dst.error) + return -EINVAL; + dst = &rt->dst; + } else { +#if IS_BUILTIN(CONFIG_IPV6) + struct ipv6hdr *iph6 = ipv6_hdr(skb); + struct flowi6 fl6 = {0}; + + fl6.flowi6_oif = oif; + fl6.flowi6_mark = skb->mark; + fl6.flowi6_uid = sock_net_uid(net, sk); + fl6.flowlabel = ip6_flowinfo(iph6); + fl6.flowi6_proto = iph6->nexthdr; + fl6.daddr = iph6->daddr; + fl6.saddr = iph6->saddr; + + dst = ip6_route_output(net, skb->sk, &fl6); + if (IS_ERR(dst) || dst->error) + return -EINVAL; +#else + pr_warn_once("BPF_LWT_REROUTE xmit: IPV6 not built-in\n"); + return -EINVAL; +#endif + } + + /* Although skb header was reserved in bpf_lwt_push_ip_encap(), it + * was done for the previous dst, so we are doing it here again, in + * case the new dst needs much more space. The call below is a noop + * if there is enough header space in skb. + */ + err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev)); + if (unlikely(err)) + return err; + + skb_dst_drop(skb); + skb_dst_set(skb, dst); + + err = dst_output(dev_net(skb_dst(skb)->dev), skb->sk, skb); + if (unlikely(err)) + return err; + + /* ip[6]_finish_output2 understand LWTUNNEL_XMIT_DONE */ + return LWTUNNEL_XMIT_DONE; +} + static int bpf_xmit(struct sk_buff *skb) { struct dst_entry *dst = skb_dst(skb); @@ -155,11 +278,20 @@ static int bpf_xmit(struct sk_buff *skb) bpf = bpf_lwt_lwtunnel(dst->lwtstate); if (bpf->xmit.prog) { + __be16 proto = skb->protocol; int ret; ret = run_lwt_bpf(skb, &bpf->xmit, dst, CAN_REDIRECT); switch (ret) { case BPF_OK: + /* If the header changed, e.g. via bpf_lwt_push_encap, + * BPF_LWT_REROUTE below should have been used if the + * protocol was also changed. + */ + if (skb->protocol != proto) { + kfree_skb(skb); + return -EINVAL; + } /* If the header was expanded, headroom might be too * small for L2 header to come, expand as needed. */ @@ -170,6 +302,8 @@ static int bpf_xmit(struct sk_buff *skb) return LWTUNNEL_XMIT_CONTINUE; case BPF_REDIRECT: return LWTUNNEL_XMIT_DONE; + case BPF_LWT_REROUTE: + return bpf_lwt_xmit_reroute(skb); default: return ret; } -- 2.20.1.791.gb4d0f1c61a-goog