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 ECA1AC169C4 for ; Tue, 12 Feb 2019 00:43:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id BB29E20844 for ; Tue, 12 Feb 2019 00:43:09 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="BYqqflE/" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727867AbfBLAnI (ORCPT ); Mon, 11 Feb 2019 19:43:08 -0500 Received: from mail-vk1-f202.google.com ([209.85.221.202]:49195 "EHLO mail-vk1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727317AbfBLAnH (ORCPT ); Mon, 11 Feb 2019 19:43:07 -0500 Received: by mail-vk1-f202.google.com with SMTP id 14so366837vkx.16 for ; Mon, 11 Feb 2019 16:43:06 -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=BYqqflE/FHyRAc7u35G5gfXlOID08DVNMya8JKXOPa8EeLVnk4VXgNIEDqtUP/2Cxf yhoOWX1Y6/Xleahhu67G3zwHa4lYRDU9u49mrAjlEgqteXI72oykOmA2sOU9xIKVjXNt 3FpVmRNmBTuDF9kOqCt+zluom0rlwJMBOWcgmD2QRZVoNHl3RCdhkb4M00mUVOHLucgV pHiJo4MOaSACjOfdIqGbpWxVdy3S+pugD9OxxVSNY92XwXMU8J7ERfhBlHPmOj7J9rML tNnqLTPk9IN2FPyDsHwGngNfoVWtSn4a3TCbM4GqkXEx0x/+sH0zsabMzVkpkEGfdyQr YTJw== 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=GxA3rJLKAZCZ/Cr2WWYYUosP0rxzbLNDXWitfQI9pRlxdbZsDIKXGmtcpdK0j4/BYO gcxHZh9+mUwp1z2raJDh3qMQWSUIEd9pne1T0JnBBH8261G1aXF/kYHSP2h++HgMcSSM xuY5V0xSI8yZpOiI3teqNynEi+Cvx6n08gFVwsVJAzD5dxmuiA7lGbBurQu+rFqS3SAd ge34+xEFux0biCkjVROYNGx84f8swHnQW1AaBD4T8Jd+Mx2NbHfeH+vvbGbgBeShmerA ZxE/bmyNG6warfex+Sc7VoySYnnWZPuR549JbMk+h9dCP+pduZ9t2ne0USxZUneQcKb0 gaGw== X-Gm-Message-State: AHQUAuZpBy2XGXJ6dHqKieI6ZCSe3YdlI8q91Gor8fKdjgysrc0e9WKd cW0FyL7pO8iNI5bLyDVX8XhiytK0 X-Google-Smtp-Source: AHgI3IZKcytljoVmkKfzr26expIacKDWvqEOIFfdKwqddawD6Kd8HsOmJXQ0PT+jAFo/h1K5GFtCAtPm X-Received: by 2002:a67:bd0e:: with SMTP id y14mr729650vsq.10.1549932185734; Mon, 11 Feb 2019 16:43:05 -0800 (PST) Date: Mon, 11 Feb 2019 16:42:45 -0800 In-Reply-To: <20190212004249.219268-1-posk@google.com> Message-Id: <20190212004249.219268-4-posk@google.com> Mime-Version: 1.0 References: <20190212004249.219268-1-posk@google.com> X-Mailer: git-send-email 2.20.1.791.gb4d0f1c61a-goog Subject: [PATCH bpf-next v9 3/7] 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