Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp107265ybm; Tue, 26 May 2020 11:59:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxD4gsOy/5BxT691beT+hfuhBERC4PhvQ5Qw5Y8aQbe6MdQuklBtsDCOg3kDHPKdvU7LE0w X-Received: by 2002:a50:da06:: with SMTP id z6mr20738598edj.372.1590519548027; Tue, 26 May 2020 11:59:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590519548; cv=none; d=google.com; s=arc-20160816; b=dHG2C9AJT51CO4vuY9fIsq3gNYKMTWxDqgXtxmOZNfjgA1Ix/lGfrpNyt5aVtp/kJT 1IJx7fkg2OCbCfwkgEKUwTlurRezp6OTWsuOHmc9Nl465hh+/ykAJRfAuj9ta8nQRNV2 i/k1g4OmsdRoaja+kojFrSkCf3T6I0nZhQgGuS9Q8hUdVZogYjJhtyUsoNt9+U8ZuuSJ Zcl8+LcqNO9zmuAQkiE+GBrP0uBks+p8rv1rZIvH6JmPjF9BLOGD6KI3vOpeJ5Z84ZH/ O+Hq3/Lau23IA+PcTkwHOGIpxbKAlNewPdgXU3Im9NlUpBm64yrbEo9uVNVUIHGfPBbY IPKw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=dLTwpCnH1BBPam1WaC2f2/Urp63Jvq0B8ycAu5GJYuo=; b=kloTK5BICSe4QfASIdMg/qa1rHqMWPRIvN9VqKRAAPwG9buugQrEp5ynEGDwuGvK5c UX3BCXsm90X8Y7i5HQJVj8KazG+HzjDRmQARVDxzucEQQ5XY4cgMywfhbGxmUxDgC6FO /N4QE4ys5+dfJkUrQdqzjJoeyyM1QwNPchJZSi/Kf9mICxUZmGYVcrPxgn1v7HnJaDu+ 2wuSTW6wZxC6OygmT84ei4PoKLKkQlKbrtKehHiAPrPtOJGnWmmvoT3S7a2EA7OxjhPR K7NQny+WliCeUNrsfFctErj7SBSTRhi3Nuo2gyaXiUJjuWshx8QDfQO1FWCA4qzw+i0/ ax/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=nuLCg0uq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x20si436380edi.323.2020.05.26.11.58.44; Tue, 26 May 2020 11:59:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=@kernel.org header.s=default header.b=nuLCg0uq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389784AbgEZS4T (ORCPT + 99 others); Tue, 26 May 2020 14:56:19 -0400 Received: from mail.kernel.org ([198.145.29.99]:49004 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389769AbgEZS4Q (ORCPT ); Tue, 26 May 2020 14:56:16 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E738A2086A; Tue, 26 May 2020 18:56:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1590519375; bh=o/jdbYgqxoIqWH8TuofPAQE8atoxvsnCieF1qOSh428=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nuLCg0uqhDyY1bqyKxznjRjFET7urNcMwjAvewhq9C8WowgP7jyfbUaim9uJw4B7X ZF7r2YF4xcpNN5a5P68QZUVvy5o4+G/0tuOGErpGdweGBXqezZ9gMBVSg8i2/RC/RL NTtZOd/C96Y0jJxwtW8QOjIcLCVK0TxHeRWbbVlc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, greg@kroah.com Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "R. Parameswaran" , "David S. Miller" , Giuliano Procida Subject: [PATCH 4.4 42/65] New kernel function to get IP overhead on a socket. Date: Tue, 26 May 2020 20:53:01 +0200 Message-Id: <20200526183920.541009923@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200526183905.988782958@linuxfoundation.org> References: <20200526183905.988782958@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "R. Parameswaran" commit 113c3075931a334f899008f6c753abe70a3a9323 upstream. A new function, kernel_sock_ip_overhead(), is provided to calculate the cumulative overhead imposed by the IP Header and IP options, if any, on a socket's payload. The new function returns an overhead of zero for sockets that do not belong to the IPv4 or IPv6 address families. This is used in the L2TP code path to compute the total outer IP overhead on the L2TP tunnel socket when calculating the default MTU for Ethernet pseudowires. Signed-off-by: R. Parameswaran Signed-off-by: David S. Miller Signed-off-by: Giuliano Procida Signed-off-by: Greg Kroah-Hartman --- include/linux/net.h | 3 +++ net/socket.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) --- a/include/linux/net.h +++ b/include/linux/net.h @@ -291,6 +291,9 @@ int kernel_sendpage(struct socket *sock, int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg); int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how); +/* Following routine returns the IP overhead imposed by a socket. */ +u32 kernel_sock_ip_overhead(struct sock *sk); + #define MODULE_ALIAS_NETPROTO(proto) \ MODULE_ALIAS("net-pf-" __stringify(proto)) --- a/net/socket.c +++ b/net/socket.c @@ -3304,3 +3304,49 @@ int kernel_sock_shutdown(struct socket * return sock->ops->shutdown(sock, how); } EXPORT_SYMBOL(kernel_sock_shutdown); + +/* This routine returns the IP overhead imposed by a socket i.e. + * the length of the underlying IP header, depending on whether + * this is an IPv4 or IPv6 socket and the length from IP options turned + * on at the socket. + */ +u32 kernel_sock_ip_overhead(struct sock *sk) +{ + struct inet_sock *inet; + struct ip_options_rcu *opt; + u32 overhead = 0; + bool owned_by_user; +#if IS_ENABLED(CONFIG_IPV6) + struct ipv6_pinfo *np; + struct ipv6_txoptions *optv6 = NULL; +#endif /* IS_ENABLED(CONFIG_IPV6) */ + + if (!sk) + return overhead; + + owned_by_user = sock_owned_by_user(sk); + switch (sk->sk_family) { + case AF_INET: + inet = inet_sk(sk); + overhead += sizeof(struct iphdr); + opt = rcu_dereference_protected(inet->inet_opt, + owned_by_user); + if (opt) + overhead += opt->opt.optlen; + return overhead; +#if IS_ENABLED(CONFIG_IPV6) + case AF_INET6: + np = inet6_sk(sk); + overhead += sizeof(struct ipv6hdr); + if (np) + optv6 = rcu_dereference_protected(np->opt, + owned_by_user); + if (optv6) + overhead += (optv6->opt_flen + optv6->opt_nflen); + return overhead; +#endif /* IS_ENABLED(CONFIG_IPV6) */ + default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */ + return overhead; + } +} +EXPORT_SYMBOL(kernel_sock_ip_overhead);