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 1A9DDC282C3 for ; Thu, 24 Jan 2019 19:58:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id CD38321726 for ; Thu, 24 Jan 2019 19:58:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="uJDg/INj" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731588AbfAXT6W (ORCPT ); Thu, 24 Jan 2019 14:58:22 -0500 Received: from mail-pg1-f202.google.com ([209.85.215.202]:43437 "EHLO mail-pg1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731823AbfAXTec (ORCPT ); Thu, 24 Jan 2019 14:34:32 -0500 Received: by mail-pg1-f202.google.com with SMTP id v72so4610497pgb.10 for ; Thu, 24 Jan 2019 11:34:32 -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=ZLVKEs3RXQ/kGZJJiuzMCzQuX18bTfWVysF+M8KdEho=; b=uJDg/INjiZlvmpeyDofoRa6+1OyCKMA/Hc6iJN0RkSAxwuuR+YyJEYZjxntj4AuSbl A+N6H0CUvLBifrg10nD83VY5aYqPga79xcQChrxYWKOqg/9YS3/Jbj2Ege7fsDFKGZz2 a6f3IkAkO3ccWP5jjK9PDgWF65HlHykcPsLdEJ9aqyjOI8mPFwe59DL2d8oQ//F9kgGf 0tiqmC1LFGucTJqdkNverJgmuly9+F0ZMqGrW+jIcR95a9dNqPWOQnTkozdS/LHEHG/g IM+J1tWJAfmcUaJRn6CzY4+uaaf58kh7chjaZkanYnjQg030TBMT7bSU9IpMZAINUIlz E2cA== 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=ZLVKEs3RXQ/kGZJJiuzMCzQuX18bTfWVysF+M8KdEho=; b=XrFoEN+E6x5S52bqxW7H1RNkUz1HGupvuk12eIiw7clBFGdd+nXfie2g8/JUuEctZ9 UvkIzGDQ3Za0XcefFYPsGnKOAVlBitXx00dOBiuWD7QNZSBdGFPUNYY81nkBPUe7fuzb 5p9l83klpAgPrnAsGUqptBPvg2RmuIFrNe42yDII8wBuGW8ynx3VCiESBRMRSlK9wwmB ztn/xYb1hEFGyVp8gTeXkpUuZduFBbQLJksvpG/ArI/QVD+wpaQcsYTFBGcxA/YHun/C 07SPQJFYG9USXJ5CsVEcptKdOlsWsPLcUB6hg5bN8q8EsgJTngIKRMiIIYb57yGuHwLw VvUw== X-Gm-Message-State: AJcUukfCY17urTOMWv8zUwzNzgly8TtvsaICdSmLzdRCuR757q53qDnV r/R/MyuMekgHykQ+dUOnJYpi+7Xh X-Google-Smtp-Source: ALg8bN4+XAEwUqR2rSjP7aieYXDBxGfqamzMcsxrfjr8VNXxr3YhCzCG2OFaVzUjkp6PiX4G/yByOK1j X-Received: by 2002:a17:902:834b:: with SMTP id z11mr2881857pln.93.1548358471813; Thu, 24 Jan 2019 11:34:31 -0800 (PST) Date: Thu, 24 Jan 2019 11:34:17 -0800 In-Reply-To: <20190124193418.81674-1-posk@google.com> Message-Id: <20190124193418.81674-3-posk@google.com> Mime-Version: 1.0 References: <20190124193418.81674-1-posk@google.com> X-Mailer: git-send-email 2.20.1.321.g9e740568ce-goog Subject: [PATCH bpf-next v2 2/3] 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 | 142 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 147 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..a3f79bff3776 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,137 @@ 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 sock *sk; + struct net *net; + + sk = sk_to_full_sk(skb->sk); + if (sk) + net = sock_net(sk); + else + net = dev_net(skb_dst(skb)->dev); + + if (ipv4) { + struct flowi4 fl4 = {0}; + struct rtable *rt; + + fl4.flowi4_oif = sk ? sk->sk_bound_dev_if : 0; + 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 = sk ? sk->sk_bound_dev_if : 0; + 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); + iph = ip_hdr(skb); + memcpy(iph, hdr, len); + bpf_compute_data_pointers(skb); + + /* final skb touches + routing */ + if (ipv4) { + skb->protocol = htons(ETH_P_IP); + 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.321.g9e740568ce-goog