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 F3AE8C282CA for ; Wed, 13 Feb 2019 19:53:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C33DB20675 for ; Wed, 13 Feb 2019 19:53:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="akgUMV/P" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2436569AbfBMTx4 (ORCPT ); Wed, 13 Feb 2019 14:53:56 -0500 Received: from mail-vs1-f74.google.com ([209.85.217.74]:52098 "EHLO mail-vs1-f74.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726291AbfBMTxz (ORCPT ); Wed, 13 Feb 2019 14:53:55 -0500 Received: by mail-vs1-f74.google.com with SMTP id o22so894864vsp.18 for ; Wed, 13 Feb 2019 11:53:54 -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=akgUMV/P5bhInoYGqIEXRz4RKkHFNBVMAbSzp4opqwNuQGbw2bjkD5mJ2xTwuf2/cr 4UGbjCWNLdwNoehQ0TJuCiZKbOm8HhgAed7JPo0t5wJeZqzjYfFmBPeD4LEFL1pN5xjG RL4DXJvUJTLb8CJrKVdPfPfVaG5ApFE/zGC1U1TWDhwEUDRZhKGcu2IPpTSh+9NInpV5 KyAUWzZ+4oYfgL7lI/opeewCOPa9AahkbAlkGcv9gr1myYJSfETr6/tpV2iQD+FgHCzr TfSilXxPhcVv74qOh9P+CcAQvGN85CG9E9SYbpjkfkaS7qUol0rN/JsXgayKP0/aVFHQ R8DA== 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=m0+sbOo+Y6E83655y+97wZynhGrptF5yJ5Lox4GVqtAr25LrxTsdl/ROdg67H99yvX +j+cxEDMXzjhqXqumCrZcikv/Wj/xSator5EcApbKUlvWp6NACX5cnTXaLDpZPG+AefE eapZp2Reh/MM+QoK6hSkEC2v94arjkAN3OLhnpLGbL0Ti8aF5wQYS5VIinZ6sS/U6Y/H vQtInd96RLd4ZDF/Iz4JZwX18aBrbMbzmO1wK0UPbcte3/AbgK20Zp/ynhGRzZVGOnJK CpqnmAf9m5qTLisaEFbFy06909WsrJnqBHfB3q0uqxCWi5DhPsa5zp5uRJ9KikIk6jsg lX3g== X-Gm-Message-State: AHQUAuYD2/Y/QrYmNJ3wAM6LTd/MMVxQ30XJhUBY4zJazLa1hL5AQDCl VkZLSJmSVRYzmfzL8Fzfp+/PAzKh X-Google-Smtp-Source: AHgI3IZo31vjdDmCJTl37WzlpBy22z3gavA3tAVhCXk7wqqlWYHJNqIHMLCHFDrN91Um6utmYFSO5PF+ X-Received: by 2002:a67:ee8e:: with SMTP id n14mr1598701vsp.45.1550087634070; Wed, 13 Feb 2019 11:53:54 -0800 (PST) Date: Wed, 13 Feb 2019 11:53:37 -0800 In-Reply-To: <20190213195341.184969-1-posk@google.com> Message-Id: <20190213195341.184969-4-posk@google.com> Mime-Version: 1.0 References: <20190213195341.184969-1-posk@google.com> X-Mailer: git-send-email 2.20.1.791.gb4d0f1c61a-goog Subject: [PATCH bpf-next v11 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