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 071C7C4151A for ; Tue, 29 Jan 2019 01:12:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C952D2183F for ; Tue, 29 Jan 2019 01:12:33 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Eu+b2YyZ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727593AbfA2BMa (ORCPT ); Mon, 28 Jan 2019 20:12:30 -0500 Received: from mail-ot1-f73.google.com ([209.85.210.73]:51859 "EHLO mail-ot1-f73.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726998AbfA2BM3 (ORCPT ); Mon, 28 Jan 2019 20:12:29 -0500 Received: by mail-ot1-f73.google.com with SMTP id c33so6952524otb.18 for ; Mon, 28 Jan 2019 17:12:28 -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=Auty6B3z++8PnK5uLbXeGtCRlmHxAkQ5X7tdGo253hA=; b=Eu+b2YyZPRF+uNGFjOWgXgF/KhL3waaAonQxUtd6/mAV7rC45muTGkM8LodfmDaY5V NWibm52c5kqWA9m5rDg7p9YvP6YhthQjkovuj6vhZf/XuHEhnUTv2/3hx+EdtT6u6yNF DSF5NWpGsJZ0A1vgsUnLFgIMJzlgqKAdju13L4KC50MkKHmwC1I5Fggv8FKk/stQjIok oN48Y1QtsZM80QuM3jAw3KET1RryPdCqdMiLIo/GZM1FIKW+UgIRPsPhnDRVBZ8IWGmU 2xi1WO9qJijk3Z6SWeiF7ZyliKCjFXYMXMKDwq18Jq763iW38TmgnmiVoTXZnd+Z1blP eIQw== 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=Auty6B3z++8PnK5uLbXeGtCRlmHxAkQ5X7tdGo253hA=; b=KthJIPqK6spSqzHqiXEN/a7yf4veEaDOGnPru6gtBfbvhbpg9pYUULoURwVBKGfJv5 YGwZWsJbqscWKcDZImtCNRYE+x7VKp4wnpxJ5SHBr8y+fx5tM07TV5rTX+RhdK0sXxLp 4U2sxHw3ehFCg98QPH5TMfMpgIPIbU4/no7sYnRLX+7zWc4opIZffjutuYmWqBwOOqSC /GtxFsZiZiBE93Fbp7TAsm/enhFcYRUYQHx4DhujdDYjo0OD3MjH5eYoGjJ0KxDHjUjT i0PpBUJQCWBFPbcqZ0Wdxv6Ugk/Lp9/lFbI6GDJjnKSWQitXj1ymAYtrzbqC9Q0dcKmx ZrXg== X-Gm-Message-State: AHQUAubeplhBFtBpxOUkDNL7/Nc+z31PzUSj93ZZCJKo2QDKtK24eHzx jTv0kJTJNwKVtY0/UicFK7Bug7IM X-Google-Smtp-Source: AHgI3IYtHGAExUb8jfxCxB8nTOFq5W9DQPYNA4ltEhxRQZLTQUMH6ojPKfDYg2oHRrdc+FyjhY7kxlhv X-Received: by 2002:a05:6830:1d2:: with SMTP id r18mr1570344ota.58.1548724348189; Mon, 28 Jan 2019 17:12:28 -0800 (PST) Date: Mon, 28 Jan 2019 17:12:15 -0800 In-Reply-To: <20190129011217.192510-1-posk@google.com> Message-Id: <20190129011217.192510-3-posk@google.com> Mime-Version: 1.0 References: <20190129011217.192510-1-posk@google.com> X-Mailer: git-send-email 2.20.1.495.gaa96b0ce6b-goog Subject: [PATCH bpf-next v3 2/4] bpf: implement BPF_LWT_ENCAP_IP mode in bpf_lwt_push_encap 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 implements BPF_LWT_ENCAP_IP mode in bpf_lwt_push_encap BPF helper. It enables BPF programs (specifically, BPF_PROG_TYPE_LWT_IN and BPF_PROG_TYPE_LWT_XMIT prog types) to add IP encapsulation headers to packets (e.g. IP/GRE, GUE, IPIP). This is useful when thousands of different short-lived flows should be encapped, each with different and dynamically determined destination. Although lwtunnels can be used in some of these scenarios, the ability to dynamically generate encap headers adds more flexibility, e.g. when routing depends on the state of the host (reflected in global bpf maps). Signed-off-by: Peter Oskolkov --- include/net/lwtunnel.h | 3 + net/core/filter.c | 3 +- net/core/lwt_bpf.c | 148 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 153 insertions(+), 1 deletion(-) diff --git a/include/net/lwtunnel.h b/include/net/lwtunnel.h index 33fd9ba7e0e5..f0973eca8036 100644 --- a/include/net/lwtunnel.h +++ b/include/net/lwtunnel.h @@ -126,6 +126,8 @@ int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b); int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb); int lwtunnel_input(struct sk_buff *skb); int lwtunnel_xmit(struct sk_buff *skb); +int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, + bool ingress); static inline void lwtunnel_set_redirect(struct dst_entry *dst) { @@ -138,6 +140,7 @@ static inline void lwtunnel_set_redirect(struct dst_entry *dst) dst->input = lwtunnel_input; } } + #else static inline void lwtstate_free(struct lwtunnel_state *lws) diff --git a/net/core/filter.c b/net/core/filter.c index fd3ae092d3d7..81d18660c38b 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -73,6 +73,7 @@ #include #include #include +#include /** * sk_filter_trim_cap - run a packet through a socket filter @@ -4796,7 +4797,7 @@ static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len static int bpf_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, bool ingress) { - return -EINVAL; /* Implemented in the next patch. */ + return bpf_lwt_push_ip_encap(skb, hdr, len, ingress); } BPF_CALL_4(bpf_lwt_in_push_encap, struct sk_buff *, skb, u32, type, void *, hdr, diff --git a/net/core/lwt_bpf.c b/net/core/lwt_bpf.c index 3e85437f7106..a9ff71aa4566 100644 --- a/net/core/lwt_bpf.c +++ b/net/core/lwt_bpf.c @@ -16,6 +16,7 @@ #include #include #include +#include struct bpf_lwt_prog { struct bpf_prog *prog; @@ -55,6 +56,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: @@ -97,6 +99,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 dst_input(skb); } if (unlikely(!dst->lwtstate->orig_input)) { @@ -168,6 +172,13 @@ static int bpf_xmit(struct sk_buff *skb) return LWTUNNEL_XMIT_CONTINUE; case BPF_REDIRECT: return LWTUNNEL_XMIT_DONE; + case BPF_LWT_REROUTE: + ret = dst_output(dev_net(skb_dst(skb)->dev), + skb->sk, skb); + if (unlikely(ret)) + return ret; + /* ip[6]_finish_output2 understand LWTUNNEL_XMIT_DONE */ + return LWTUNNEL_XMIT_DONE; default: return ret; } @@ -389,6 +400,143 @@ static const struct lwtunnel_encap_ops bpf_encap_ops = { .owner = THIS_MODULE, }; +int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, bool ingress) +{ + struct dst_entry *dst = NULL; + struct iphdr *iph; + bool ipv4; + int err; + + if (unlikely(len < sizeof(struct iphdr) || len > LWT_BPF_MAX_HEADROOM)) + return -EINVAL; + + /* validate protocol and length */ + iph = (struct iphdr *)hdr; + if (iph->version == 4) { + ipv4 = true; + if (iph->ihl * 4 > len) + return -EINVAL; + } else if (iph->version == 6) { + ipv4 = false; + if (unlikely(len < sizeof(struct ipv6hdr))) + return -EINVAL; + } else { + return -EINVAL; + } + + /* allocate enough space for the encap headers + L2 hdr */ + if (ingress) { + err = skb_cow_head(skb, len + skb->mac_len); + if (unlikely(err)) + return err; + } else { + /* ip_route_input_noref below does route lookup and dst + * drop/set for ingress. There is no similar function for + * egress, so we need to do route lookup and replace skb's + * dst in this function. + */ + struct net_device *l3mdev = + l3mdev_master_dev_rcu(skb_dst(skb)->dev); + int oif = l3mdev ? l3mdev->ifindex : 0; + struct sock *sk; + struct net *net; + + 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 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 { + struct ipv6hdr *iph6 = (struct ipv6hdr *)hdr; + 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; + } + + err = skb_cow_head(skb, len + LL_RESERVED_SPACE(dst->dev)); + if (unlikely(err)) + return err; + } + + /* push the encap headers and fix pointers */ + skb_reset_inner_headers(skb); + skb->encapsulation = 1; + skb_push(skb, len); + if (ingress) + skb_postpush_rcsum(skb, iph, len); + skb_reset_network_header(skb); + memcpy(skb_network_header(skb), hdr, len); + bpf_compute_data_pointers(skb); + + /* final skb touches + routing */ + if (ipv4) { + skb->protocol = htons(ETH_P_IP); + iph = ip_hdr(skb); + if (iph->ihl * 4 < len) + skb_set_transport_header(skb, iph->ihl * 4); + + if (!iph->check) + iph->check = ip_fast_csum((unsigned char *)iph, + iph->ihl); + + if (ingress) { + err = ip_route_input_noref(skb, iph->daddr, iph->saddr, + iph->tos, skb_dst(skb)->dev); + if (err) + return err; + } else { + skb_dst_drop(skb); + skb_dst_set(skb, dst); + } + } else { + skb->protocol = htons(ETH_P_IPV6); + if (sizeof(struct ipv6hdr) < len) + skb_set_transport_header(skb, sizeof(struct ipv6hdr)); + + if (ingress) { + ip6_route_input(skb); + if (skb_dst(skb)->error) + return skb_dst(skb)->error; + } else { + skb_dst_drop(skb); + skb_dst_set(skb, dst); + } + } + + return 0; +} + static int __init bpf_lwt_init(void) { return lwtunnel_encap_add_ops(&bpf_encap_ops, LWTUNNEL_ENCAP_BPF); -- 2.20.1.495.gaa96b0ce6b-goog