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 59C8EC169C4 for ; Fri, 8 Feb 2019 16:39:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2095520823 for ; Fri, 8 Feb 2019 16:39:07 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bUBsChgs" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727623AbfBHQjF (ORCPT ); Fri, 8 Feb 2019 11:39:05 -0500 Received: from mail-it1-f202.google.com ([209.85.166.202]:35101 "EHLO mail-it1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727592AbfBHQjF (ORCPT ); Fri, 8 Feb 2019 11:39:05 -0500 Received: by mail-it1-f202.google.com with SMTP id m84so6911156ita.0 for ; Fri, 08 Feb 2019 08:39:04 -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=olcyvJEwNbvDpORQjgEEWm0iXOM/GwNa/hpLqD7ZTIc=; b=bUBsChgsgjjJHyRaEjZkRltutVmU4V7tugL84nfaI6IhCZF6BHvz92jxri4R9VGMd6 tpYBI2HZQA23mO7pYvEBOjNnAN574/BGT+FAo6dCFcw6vVLGIIOWaNYiJcWfQYir0aPQ WGfAxhT4Nq6Npru0d4gIEhAYhgRKqZ2q6cIWtKCy9+Ij2M9747zxiCkO/qceztF8D1AF h+gd/DBE4mzs69VNSiIVk2VIWFt6m4JvV4ZpAsOQg8+s9mrHA6+MovKprTXHeBa/Kvk1 IVAvCD2CLeYESHzvXV2ZTUvBm3V9JZkSKsRDrVj2V1fCi8bd6vjjllwkUwqFUc5M5GIS jc/g== 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=olcyvJEwNbvDpORQjgEEWm0iXOM/GwNa/hpLqD7ZTIc=; b=hUOY3j/NzCJHe5OgYVORXLO3hkxeIm9EQlQ8hvUJwL3kbU9Qf0JX+O+CgQNWgPe1uF iC9kh/Cf+BIO89PZ8bBb99Ff9BmIUfkWKUz4jn/SrBw8l/UvvW7aLt1m9ybK7wXo+Ozr GJ/jT8dCxBxLwqjZXxZ3SCD39WcKfQQf+1OUaO9fKpUYBFBk1WjFj8FpKwKx0O0VoHGv p5OwBlMSP2pbTj4BNIu3yItR/GVf0Cq8OKSscwksZVVwXwgIbZn23+anS+Vrjlh1RiMF iLnOG3KfPOyqLRVlRQhxTOaVRC+QXh+g4cL+F17L0oRH1LARsxZiOF3LfktjiWZB+Znc J8lw== X-Gm-Message-State: AHQUAuZDjPRsX5ZeYvMFUm7wNAOsrF/r6Y5ZxOP1KJwucIeimJXbhPwM oj5nxKHfVkcv8aq029pWBpPikB9B X-Google-Smtp-Source: AHgI3IYyDOvcHXI4ljfef+5T8Hr8lmPb2TRbJ1CiPk1/2TjdMjz+thXY3I69p7MehzyOQHzdl2JXzhrv X-Received: by 2002:a24:3cc5:: with SMTP id m188mr8658465ita.5.1549643944240; Fri, 08 Feb 2019 08:39:04 -0800 (PST) Date: Fri, 8 Feb 2019 08:38:46 -0800 In-Reply-To: <20190208163849.151626-1-posk@google.com> Message-Id: <20190208163849.151626-4-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 3/6] bpf: handle GSO 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 handling of GSO packets in bpf_lwt_push_ip_encap() (called from bpf_lwt_push_encap): * IPIP, GRE, and UDP encapsulation types are deduced by looking into iphdr->protocol or ipv6hdr->next_header; * SCTP GSO packets are not supported (as bpf_skb_proto_4_to_6 and similar do); * UDP_L4 GSO packets are also not supported (although they are not blocked in bpf_skb_proto_4_to_6 and similar), as skb_decrease_gso_size() will break it; * SKB_GSO_DODGY bit is set. Note: it may be possible to support SCTP and UDP_L4 gso packets; but as these cases seem to be not well handled by other tunneling/encapping code paths, the solution should be generic enough to apply to all tunneling/encapping code. v8 changes: - make sure that if GRE or UDP encap is detected, there is enough of pushed bytes to cover both IP[v6] + GRE|UDP headers; - do not reject double-encapped packets; - whitelist TCP GSO packets rather than block SCTP GSO and UDP GSO. Signed-off-by: Peter Oskolkov --- net/core/lwt_bpf.c | 67 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/net/core/lwt_bpf.c b/net/core/lwt_bpf.c index e5a9850d9f48..079871fc020f 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; @@ -390,10 +391,72 @@ static const struct lwtunnel_encap_ops bpf_encap_ops = { .owner = THIS_MODULE, }; +static int handle_gso_type(struct sk_buff *skb, unsigned int gso_type, + int encap_len) +{ + struct skb_shared_info *shinfo = skb_shinfo(skb); + + gso_type |= SKB_GSO_DODGY; + shinfo->gso_type |= gso_type; + skb_decrease_gso_size(shinfo, encap_len); + shinfo->gso_segs = 0; + return 0; +} + static int handle_gso_encap(struct sk_buff *skb, bool ipv4, int encap_len) { - /* Handling of GSO-enabled packets is added in the next patch. */ - return -EOPNOTSUPP; + int next_hdr_offset; + void *next_hdr; + __u8 protocol; + + /* SCTP and UDP_L4 gso need more nuanced handling than what + * handle_gso_type() does above: skb_decrease_gso_size() is not enough. + * So at the moment only TCP GSO packets are let through. + */ + if (!(skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) + return -ENOTSUPP; + + if (ipv4) { + protocol = ip_hdr(skb)->protocol; + next_hdr_offset = sizeof(struct iphdr); + next_hdr = skb_network_header(skb) + next_hdr_offset; + } else { + protocol = ipv6_hdr(skb)->nexthdr; + next_hdr_offset = sizeof(struct ipv6hdr); + next_hdr = skb_network_header(skb) + next_hdr_offset; + } + + switch (protocol) { + case IPPROTO_GRE: + next_hdr_offset += sizeof(struct gre_base_hdr); + if (next_hdr_offset > encap_len) + return -EINVAL; + + if (((struct gre_base_hdr *)next_hdr)->flags & GRE_CSUM) + return handle_gso_type(skb, SKB_GSO_GRE_CSUM, + encap_len); + return handle_gso_type(skb, SKB_GSO_GRE, encap_len); + + case IPPROTO_UDP: + next_hdr_offset += sizeof(struct udphdr); + if (next_hdr_offset > encap_len) + return -EINVAL; + + if (((struct udphdr *)next_hdr)->check) + return handle_gso_type(skb, SKB_GSO_UDP_TUNNEL_CSUM, + encap_len); + return handle_gso_type(skb, SKB_GSO_UDP_TUNNEL, encap_len); + + case IPPROTO_IP: + case IPPROTO_IPV6: + if (ipv4) + return handle_gso_type(skb, SKB_GSO_IPXIP4, encap_len); + else + return handle_gso_type(skb, SKB_GSO_IPXIP6, encap_len); + + default: + return -EPROTONOSUPPORT; + } } int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, bool ingress) -- 2.20.1.791.gb4d0f1c61a-goog