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 CE07DC282CD for ; Tue, 29 Jan 2019 01:12:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 910BD214DA 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="qXAi6qeT" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727439AbfA2BM1 (ORCPT ); Mon, 28 Jan 2019 20:12:27 -0500 Received: from mail-vs1-f74.google.com ([209.85.217.74]:37003 "EHLO mail-vs1-f74.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726998AbfA2BM1 (ORCPT ); Mon, 28 Jan 2019 20:12:27 -0500 Received: by mail-vs1-f74.google.com with SMTP id c201so8609942vsd.4 for ; Mon, 28 Jan 2019 17:12:26 -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=0rgb87XiVkxCc4OOyf1iw9e9dCh8JVkFzjEIAnDDYxw=; b=qXAi6qeTRuNk+xySh+fJq6zwL7Uwwv5IPvDgc/Mvsk8gGkwcK7mg9BaDd5h5L17Hhd W1HT4wfm0QGpx1FGzMf6DzSSe/y5Y+oGdBAX+9fnPmuLHNbYsq/rkPfVQ1Z7xJcpVtIv AHte+4sJI4ydrqRQ2b4qc6+tl5iNX1lOuAplXxTNmiNgTOkmHHCB88uhFDF8ZCbAcKYh 7U9eV3k65seQkOPff//RzhDSAK1uKWsMnSQD2AbGbdNYvrGwTGlw9S3ZtNdiIAuRr7Gn Xh5b9fx0lFmj73mgkbOz5EYyZjoLb1ot1AElDff2O/lLPAWr7h105jPtKQQ9bYqbIEvz WPyA== 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=0rgb87XiVkxCc4OOyf1iw9e9dCh8JVkFzjEIAnDDYxw=; b=LZ3IXfOsgp28GJRr0r8Sek9vJqkr10l/Mda9Gm1MOygicfsGA1tF9EFkrQzwrl6+sq 10hiPfwNxz9YGHL9TMF6jiKyL5+eDsKjUct9vc5zvq9rbxM+M8Rp79rgBNOSfohHm6YA tN1XY/C0Av4BcetDZ1KUmHXwknp29/nV/SMDRItLhoiRaqH+/84ihzL482XUWav2W1DB De3P0F6x+PoLKjQOfk+W+fHPq39Zai6YfaAMQn9OIBQy1502wDe0E1XVMwqd/HgMetwf alaGxITzzgA5hqq1zoKhgTeCsP8VrY2h8sV3HKZjjyWtb0qPq2Nlz2VNNlYvLHlTlath gS9w== X-Gm-Message-State: AHQUAuYVMnK1TzCkGXU9++SuVG4TfhzgnA5kJB0eaBW1mfjC/PgtjtA2 AR3WaJKV1ux19a6GTq1sjxOMIgSx X-Google-Smtp-Source: AHgI3IYe32XiXfadn1iIPeq75s4NEu6Nu23cVSGlO6t534gljAdLsJ6gejz4qDf2RNFNkY0IObkRq11Y X-Received: by 2002:a1f:b989:: with SMTP id j131mr1839390vkf.5.1548724345870; Mon, 28 Jan 2019 17:12:25 -0800 (PST) Date: Mon, 28 Jan 2019 17:12:14 -0800 In-Reply-To: <20190129011217.192510-1-posk@google.com> Message-Id: <20190129011217.192510-2-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 1/4] bpf: add plumbing for BPF_LWT_ENCAP_IP 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 adds all needed plumbing in preparation to allowing bpf programs to do IP encapping via bpf_lwt_push_encap. Actual implementation is added in the next patch in the patchset. Of note: - bpf_lwt_push_encap can now be called from BPF_PROG_TYPE_LWT_XMIT prog types in addition to BPF_PROG_TYPE_LWT_IN; - as route lookups are different for ingress vs egress, the single external bpf_lwt_push_encap BPF helper is routed internally to either bpf_lwt_in_push_encap or bpf_lwt_xmit_push_encap BPF_CALLs, depending on prog type. Signed-off-by: Peter Oskolkov --- include/uapi/linux/bpf.h | 23 ++++++++++++++++++-- net/core/filter.c | 46 +++++++++++++++++++++++++++++++++++----- 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 60b99b730a41..c4fee8b45762 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -2015,6 +2015,16 @@ union bpf_attr { * Only works if *skb* contains an IPv6 packet. Insert a * Segment Routing Header (**struct ipv6_sr_hdr**) inside * the IPv6 header. + * **BPF_LWT_ENCAP_IP** + * IP encapsulation (GRE/GUE/IPIP/etc). The outer header + * must be IPv4 or IPv6, followed by zero or more + * additional headers, up to LWT_BPF_MAX_HEADROOM total + * bytes in all prepended headers. + * + * BPF_LWT_ENCAP_SEG6*** types can be called by bpf programs of + * type BPF_PROG_TYPE_LWT_IN; BPF_LWT_ENCAP_IP type can be called + * by bpf programs of types BPF_PROG_TYPE_LWT_IN and + * BPF_PROG_TYPE_LWT_XMIT. * * A call to this helper is susceptible to change the underlaying * packet buffer. Therefore, at load time, all checks on pointers @@ -2495,7 +2505,8 @@ enum bpf_hdr_start_off { /* Encapsulation type for BPF_FUNC_lwt_push_encap helper. */ enum bpf_lwt_encap_mode { BPF_LWT_ENCAP_SEG6, - BPF_LWT_ENCAP_SEG6_INLINE + BPF_LWT_ENCAP_SEG6_INLINE, + BPF_LWT_ENCAP_IP, }; #define __bpf_md_ptr(type, name) \ @@ -2583,7 +2594,15 @@ enum bpf_ret_code { BPF_DROP = 2, /* 3-6 reserved */ BPF_REDIRECT = 7, - /* >127 are reserved for prog type specific return codes */ + /* >127 are reserved for prog type specific return codes. + * + * BPF_LWT_REROUTE: used by BPF_PROG_TYPE_LWT_IN and + * BPF_PROG_TYPE_LWT_XMIT to indicate that skb's dst + * has changed and appropriate dst_input() or dst_output() + * action has to be taken (this is an L3 redirect, as + * opposed to L2 redirect represented by BPF_REDIRECT above). + */ + BPF_LWT_REROUTE = 128, }; struct bpf_sock { diff --git a/net/core/filter.c b/net/core/filter.c index 8e587dd1da20..fd3ae092d3d7 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -4793,7 +4793,13 @@ static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len } #endif /* CONFIG_IPV6_SEG6_BPF */ -BPF_CALL_4(bpf_lwt_push_encap, struct sk_buff *, skb, u32, type, void *, hdr, +static int bpf_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, + bool ingress) +{ + return -EINVAL; /* Implemented in the next patch. */ +} + +BPF_CALL_4(bpf_lwt_in_push_encap, struct sk_buff *, skb, u32, type, void *, hdr, u32, len) { switch (type) { @@ -4801,14 +4807,41 @@ BPF_CALL_4(bpf_lwt_push_encap, struct sk_buff *, skb, u32, type, void *, hdr, case BPF_LWT_ENCAP_SEG6: case BPF_LWT_ENCAP_SEG6_INLINE: return bpf_push_seg6_encap(skb, type, hdr, len); +#endif +#if IS_ENABLED(CONFIG_LWTUNNEL_BPF) + case BPF_LWT_ENCAP_IP: + return bpf_push_ip_encap(skb, hdr, len, true /* ingress */); #endif default: return -EINVAL; } } -static const struct bpf_func_proto bpf_lwt_push_encap_proto = { - .func = bpf_lwt_push_encap, +BPF_CALL_4(bpf_lwt_xmit_push_encap, struct sk_buff *, skb, u32, type, + void *, hdr, u32, len) +{ + switch (type) { +#if IS_ENABLED(CONFIG_LWTUNNEL_BPF) + case BPF_LWT_ENCAP_IP: + return bpf_push_ip_encap(skb, hdr, len, false /* egress */); +#endif + default: + return -EINVAL; + } +} + +static const struct bpf_func_proto bpf_lwt_in_push_encap_proto = { + .func = bpf_lwt_in_push_encap, + .gpl_only = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_CTX, + .arg2_type = ARG_ANYTHING, + .arg3_type = ARG_PTR_TO_MEM, + .arg4_type = ARG_CONST_SIZE +}; + +static const struct bpf_func_proto bpf_lwt_xmit_push_encap_proto = { + .func = bpf_lwt_xmit_push_encap, .gpl_only = false, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, @@ -5274,7 +5307,8 @@ bool bpf_helper_changes_pkt_data(void *func) func == bpf_lwt_seg6_adjust_srh || func == bpf_lwt_seg6_action || #endif - func == bpf_lwt_push_encap) + func == bpf_lwt_in_push_encap || + func == bpf_lwt_xmit_push_encap) return true; return false; @@ -5652,7 +5686,7 @@ lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) { switch (func_id) { case BPF_FUNC_lwt_push_encap: - return &bpf_lwt_push_encap_proto; + return &bpf_lwt_in_push_encap_proto; default: return lwt_out_func_proto(func_id, prog); } @@ -5688,6 +5722,8 @@ lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) return &bpf_l4_csum_replace_proto; case BPF_FUNC_set_hash_invalid: return &bpf_set_hash_invalid_proto; + case BPF_FUNC_lwt_push_encap: + return &bpf_lwt_xmit_push_encap_proto; default: return lwt_out_func_proto(func_id, prog); } -- 2.20.1.495.gaa96b0ce6b-goog