From patchwork Sat Jun 27 08:07:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jason A. Donenfeld" X-Patchwork-Id: 191943 Delivered-To: patch@linaro.org Received: by 2002:a92:d244:0:0:0:0:0 with SMTP id v4csp1178213ilg; Sat, 27 Jun 2020 01:07:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxCrFamOOhAPgqimSIlqmPMN7paZMwqK8i/tPeDy+GyfGrlKIiSOKZEe2hUE0LzGDavoOZO X-Received: by 2002:a50:934e:: with SMTP id n14mr7477676eda.88.1593245251346; Sat, 27 Jun 2020 01:07:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593245251; cv=none; d=google.com; s=arc-20160816; b=zfea8+CNh0d/UxvP1E2pdkdn2rvYBfldi12z0cj63XX2zwVc+yM7U6yuBHX04dbQIK uTgiTyH4iAFsl+BWHCjQMCHHPOyw1WJNU+edrdV8dM+WrdZCHNY+B1Oe+e4/4ozVIljn Dhq32GLyH7prxX3jmuLQGomL7eowhb8CQ09OmTH+pTw+Z0hEyaIGJZPWqbA51jNwG5ZD /dCff6AUlsQWOFzeSgAFri3mIKWJ2F5XCzmzvj32dvqUbs2Gf4czQ5TVgpnRF/dqNIyN jbp4a9n9OcoBtJ56VnL8IFjqFjnjDaeWtLfjeSqQFCmFDi2KD6yELznBbwqm4qp54WkP Kt/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=9Vp+tw0QaLIycF8HKfRzJuTNoSWc9BINsSk1bY7P088=; b=bNr7gdG/gz7QWlv27/OrxogKAikQ0gD0ERetGgXwvske5ns+74W8lTdZIIQx9G5uzL FUowFBl44BJYJrXVpcc7HAvLSbIYzXhwjwFmzFukv+r5xW8eBL+kKkBE54mjK2tmTAua NHYBO/aLg+oBLc2I0UASWgrcF0ViQ4o32wDmmhqqk1b6V6wiHg8+/fZV0JX17ExLPITS jUm1ChzM7i7R6uXRf3grBg/YNAZOU3V6btrR9EPjSkgjqqvac/J64yC2sXIU70S1G5lV HvyijEaS2lF2hpA1/Vy7J5lmYqaMp6T6EJ9HFr+yAvVGgN5J/rPeigrcbB32JAzGZy29 dBSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=mail header.b=U3nsn0Oe; spf=pass (google.com: domain of netdev-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=netdev-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zx2c4.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c25si9181037edv.335.2020.06.27.01.07.31; Sat, 27 Jun 2020 01:07:31 -0700 (PDT) Received-SPF: pass (google.com: domain of netdev-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@zx2c4.com header.s=mail header.b=U3nsn0Oe; spf=pass (google.com: domain of netdev-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=netdev-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zx2c4.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726312AbgF0IH3 (ORCPT + 9 others); Sat, 27 Jun 2020 04:07:29 -0400 Received: from mail.zx2c4.com ([192.95.5.64]:48179 "EHLO mail.zx2c4.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725991AbgF0IH1 (ORCPT ); Sat, 27 Jun 2020 04:07:27 -0400 Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTP id bea1e8e4; Sat, 27 Jun 2020 07:48:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=zx2c4.com; h=from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=mail; bh=Cz702woikBQPXSFMDcaXkR3Bj 5s=; b=U3nsn0OeI3SnCKIxa+FX+mmzoj6amZ2g3resRkHlEw/THYlKhhefc9oQp xzqvlsBgbvX+eD7Q/K8w+VuHblCnyK25VxyR5OG3+SU+tZZYuzBdPmTnM0iuBKWW +HN3rCqzlvR0w2vjicj9GjD0hNBFkEC3cjB0WZEmPH4yofmXw/gY3x6jwZh/c/rb bmMl0qNffKucERKRm1dfLntzaQhE+LhDG8QSAE0teUHRXDySreEn8fYis5ZWiNwu APK+4a17kxTyLM4EEE+P05+IE/dc+6XNOb2uhG4mbKW3KfY1rOPsix/kuw2Bzta2 XpyalB9SpcqHb10vlX5UQhNHqK2rg== Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 8efff8dc (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Sat, 27 Jun 2020 07:48:02 +0000 (UTC) From: "Jason A. Donenfeld" To: netdev@vger.kernel.org, davem@davemloft.net Cc: "Jason A. Donenfeld" Subject: [PATCH net 1/5] net: ip_tunnel: add header_ops for layer 3 devices Date: Sat, 27 Jun 2020 02:07:09 -0600 Message-Id: <20200627080713.179883-2-Jason@zx2c4.com> In-Reply-To: <20200627080713.179883-1-Jason@zx2c4.com> References: <20200627080713.179883-1-Jason@zx2c4.com> MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Some devices that take straight up layer 3 packets benefit from having a shared header_ops so that AF_PACKET sockets can inject packets that are recognized. This shared infrastructure will be used by other drivers that currently can't inject packets using AF_PACKET. It also exposes the parser function, as it is useful in standalone form too. Signed-off-by: Jason A. Donenfeld --- include/net/ip_tunnels.h | 3 +++ net/ipv4/ip_tunnel_core.c | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) -- 2.27.0 diff --git a/include/net/ip_tunnels.h b/include/net/ip_tunnels.h index 076e5d7db7d3..36025dea7612 100644 --- a/include/net/ip_tunnels.h +++ b/include/net/ip_tunnels.h @@ -290,6 +290,9 @@ int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[], struct ip_tunnel_parm *p, __u32 fwmark); void ip_tunnel_setup(struct net_device *dev, unsigned int net_id); +extern const struct header_ops ip_tunnel_header_ops; +__be16 ip_tunnel_parse_protocol(const struct sk_buff *skb); + struct ip_tunnel_encap_ops { size_t (*encap_hlen)(struct ip_tunnel_encap *e); int (*build_header)(struct sk_buff *skb, struct ip_tunnel_encap *e, diff --git a/net/ipv4/ip_tunnel_core.c b/net/ipv4/ip_tunnel_core.c index 181b7a2a0247..07d958aa03f8 100644 --- a/net/ipv4/ip_tunnel_core.c +++ b/net/ipv4/ip_tunnel_core.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-only /* * Copyright (c) 2013 Nicira, Inc. + * Copyright (C) 2020 Jason A. Donenfeld . All Rights Reserved. */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt @@ -844,3 +845,21 @@ void ip_tunnel_unneed_metadata(void) static_branch_dec(&ip_tunnel_metadata_cnt); } EXPORT_SYMBOL_GPL(ip_tunnel_unneed_metadata); + +/* Returns either the correct skb->protocol value, or 0 if invalid. */ +__be16 ip_tunnel_parse_protocol(const struct sk_buff *skb) +{ + if (skb_network_header(skb) >= skb->head && + (skb_network_header(skb) + sizeof(struct iphdr)) <= skb_tail_pointer(skb) && + ip_hdr(skb)->version == 4) + return htons(ETH_P_IP); + if (skb_network_header(skb) >= skb->head && + (skb_network_header(skb) + sizeof(struct ipv6hdr)) <= skb_tail_pointer(skb) && + ipv6_hdr(skb)->version == 6) + return htons(ETH_P_IPV6); + return 0; +} +EXPORT_SYMBOL(ip_tunnel_parse_protocol); + +const struct header_ops ip_tunnel_header_ops = { .parse_protocol = ip_tunnel_parse_protocol }; +EXPORT_SYMBOL(ip_tunnel_header_ops);