Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1299567pxa; Sat, 15 Aug 2020 14:58:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwNt0Cvalw9QVWIKHcjVvndu+Bzoz5ROeDfx+GfbZ9MUAieOSWkXdTs0xudx2gaymMEIJAZ X-Received: by 2002:aa7:c544:: with SMTP id s4mr8470176edr.51.1597528711185; Sat, 15 Aug 2020 14:58:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597528711; cv=none; d=google.com; s=arc-20160816; b=CmYj+kk3RKXcQBa6BeciQkpK4J1JluStVY8/oD82co8n/E1+TsMyzCYKznCteXMTq8 3jA7ufsPtPZrmJWccKVyCzFEVsx0LHoGODN9Fp1+GO1dckg1hRttWE55zL7dw5yiStzV 0zZl4WVev8MNhdnf9GXd5YBUgbhON44OxQT33NWw9RE8byo4trC72AxIqLnerOl7p4KZ +LWlIdsETMp6DXmAadMUiIEt/1yzJKqON1aoOsZ9WZnOXCP9yKUu2wqMiQX7cGYH+HFz wYZEHsJ9/wb+HvO10wT9VxJMjzTRjjnGeEeZsVS9XT505jMfyKRGdjNQ7OegnIRWhcDX Byzg== 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=Npmo1qy1EtVMY1f22f/XoQlkfS4jbAFUtpYwb+UPN9s=; b=pH83Ol3mfGZLu8kovDzCVCQgAt23rAm1NXFN3U/DodX+7TjjS3x/De1Abms58XYIUz OwS0nGzzkCeeDK2JaUExG/URYmjuBC4YVywS+RGiOVnMuVkFfm7/9oSIkrTFP7F2sm03 TgC54gO6cAzf1F8t0MMgOoLPr4x/hUYg1jyKF03eKJbZgabd6BpQurw2uw2xXESn5JL6 wxaOS7Qy6adZDU8RvLZtjy5Cja9CD9Yz2KKV+2lwxlIVbbc7mIOnacipauLH2k58PzQx oW0ov0QMXnkN4n2a8AWpLZJfS99MPmu3+mxDegJUXHppIw9AIpeRdjOu4ccCvYz5GGZt SxBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@tfz-net.20150623.gappssmtp.com header.s=20150623 header.b=UGhjOy6s; 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 yj16si7930143ejb.59.2020.08.15.14.58.08; Sat, 15 Aug 2020 14:58:31 -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=@tfz-net.20150623.gappssmtp.com header.s=20150623 header.b=UGhjOy6s; 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 S1728991AbgHOV4d (ORCPT + 99 others); Sat, 15 Aug 2020 17:56:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728999AbgHOVwN (ORCPT ); Sat, 15 Aug 2020 17:52:13 -0400 Received: from mail-ot1-x344.google.com (mail-ot1-x344.google.com [IPv6:2607:f8b0:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 71026C004587 for ; Sat, 15 Aug 2020 11:24:08 -0700 (PDT) Received: by mail-ot1-x344.google.com with SMTP id 93so10274025otx.2 for ; Sat, 15 Aug 2020 11:24:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tfz-net.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Npmo1qy1EtVMY1f22f/XoQlkfS4jbAFUtpYwb+UPN9s=; b=UGhjOy6sny6yVuAYdTV7QBCsVIFSkm8sA3y/SwYBtDKROYWCu7teCOiIZNIbFqbCYc ExIJMnNURR2y62RErP89vr5vRlyCI26hAxWcIRy+YJupCpNAWZwAPEF0bvbN7jlfrNBW Z6XL/zXinZFM3jz26fjeTSDd5bpJjvs/+j1Wxbfowb0CHENC0egNjqh/Gl2vPvf5J5ti NYw9sRYdgG4Sm5vsHkdc0KonY3R8qwZLxvgscFYM88L8Wg+IO7UcFJd/T7ItwLGOEzEH n+YiJzdL+FBqViqWHvhlG20hAgDcQPgR3M6n3cgCQkXAdj4ed4Zuo02mdBGxq+kcLpfX Jr+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Npmo1qy1EtVMY1f22f/XoQlkfS4jbAFUtpYwb+UPN9s=; b=gj+IxyADHM4QfRZ8rHBpiYoYAFLOFPrgIodfok9wXJgbLzTmGn+HqpCAg+ANv4tjgk EVTgz4H9seJWm+wOg0hp9IY6QbCtnwjQj6uV8zr9HCBndyLimcxpqj+Is5luxVGaTL8F 1o/2oLhpPKSymJ6gycDuy6e4RJVdXcc9zUXin8AGhdwIEBszsVhvwolw07MWC5PrdlFZ 75hmc1LyM/WozmGZDawdFrqUGQVslbXZkHd1uvQLWUaLmaoMuXwbWASr6qN9v7DM95Rk ybauKrFuC+x/X3TdfClT0B6gBTXtvoM4a074fHeNE2zCp/b1vu+8FM2QQ24UNY530bsj Xy/w== X-Gm-Message-State: AOAM533/ZGZU9ifWdSdHjF5r1LrCW8FWTeogwobpL5y6XsBUQghh04xO oPVVl/WhSZSm6JbKbdSO98TT79hxKTbCdDJcKwk= X-Received: by 2002:a05:6830:605:: with SMTP id w5mr5942549oti.3.1597515847051; Sat, 15 Aug 2020 11:24:07 -0700 (PDT) Received: from foo.attlocal.net (108-232-117-128.lightspeed.sntcca.sbcglobal.net. [108.232.117.128]) by smtp.gmail.com with ESMTPSA id z72sm2397820ooa.42.2020.08.15.11.24.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 Aug 2020 11:24:06 -0700 (PDT) From: Pascal Bouchareine To: linux-kernel@vger.kernel.org Cc: Pascal Bouchareine , linux-api@vger.kernel.org, netdev@vger.kernel.org, "David S. Miller" , "Jakub Kicinski" , "Andrew Morton" , "Alexey Dobriyan" , "Al Viro" Subject: [PATCH 2/2] net: socket: implement SO_DESCRIPTION Date: Sat, 15 Aug 2020 11:23:44 -0700 Message-Id: <20200815182344.7469-3-kalou@tfz.net> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200815182344.7469-1-kalou@tfz.net> References: <20200815182344.7469-1-kalou@tfz.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This command attaches the zero terminated string in optval to the socket for troubleshooting purposes. The free string is displayed in the process fdinfo file for that fd (/proc//fdinfo/). One intended usage is to allow processes to self-document sockets for netstat and friends to report We ignore optlen and constrain the string to a static max size Signed-off-by: Pascal Bouchareine --- include/net/sock.h | 4 ++++ include/uapi/asm-generic/socket.h | 2 ++ net/core/sock.c | 23 +++++++++++++++++++++++ net/socket.c | 5 +++++ 4 files changed, 34 insertions(+) diff --git a/include/net/sock.h b/include/net/sock.h index 1183507df95b..6b4fd1383282 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -342,6 +342,7 @@ struct bpf_sk_storage; * @sk_txtime_deadline_mode: set deadline mode for SO_TXTIME * @sk_txtime_report_errors: set report errors mode for SO_TXTIME * @sk_txtime_unused: unused txtime flags + * @sk_description: user supplied with SO_DESCRIPTION */ struct sock { /* @@ -519,6 +520,9 @@ struct sock { struct bpf_sk_storage __rcu *sk_bpf_storage; #endif struct rcu_head sk_rcu; + +#define SK_MAX_DESC_SIZE 256 + char *sk_description; }; enum sk_pacing { diff --git a/include/uapi/asm-generic/socket.h b/include/uapi/asm-generic/socket.h index 77f7c1638eb1..fb51c4bb7a12 100644 --- a/include/uapi/asm-generic/socket.h +++ b/include/uapi/asm-generic/socket.h @@ -119,6 +119,8 @@ #define SO_DETACH_REUSEPORT_BPF 68 +#define SO_DESCRIPTION 69 + #if !defined(__KERNEL__) #if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__)) diff --git a/net/core/sock.c b/net/core/sock.c index 2e5b7870e5d3..2cb44a0e38b7 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -828,6 +828,24 @@ void sock_set_rcvbuf(struct sock *sk, int val) } EXPORT_SYMBOL(sock_set_rcvbuf); +int sock_set_description(struct sock *sk, char __user *user_desc) +{ + char *old, *desc; + + desc = strndup_user(user_desc, SK_MAX_DESC_SIZE, GFP_KERNEL_ACCOUNT); + if (IS_ERR(desc)) + return PTR_ERR(desc); + + lock_sock(sk); + old = sk->sk_description; + sk->sk_description = desc; + release_sock(sk); + + kfree(old); + + return 0; +} + /* * This is meant for all protocols to use and covers goings on * at the socket level. Everything here is generic. @@ -850,6 +868,9 @@ int sock_setsockopt(struct socket *sock, int level, int optname, if (optname == SO_BINDTODEVICE) return sock_setbindtodevice(sk, optval, optlen); + if (optname == SO_DESCRIPTION) + return sock_set_description(sk, optval); + if (optlen < sizeof(int)) return -EINVAL; @@ -1792,6 +1813,8 @@ static void __sk_destruct(struct rcu_head *head) RCU_INIT_POINTER(sk->sk_filter, NULL); } + kfree(sk->sk_description); + sock_disable_timestamp(sk, SK_FLAGS_TIMESTAMP); #ifdef CONFIG_BPF_SYSCALL diff --git a/net/socket.c b/net/socket.c index 976426d03f09..4f2c1a7744b0 100644 --- a/net/socket.c +++ b/net/socket.c @@ -134,6 +134,11 @@ static void sock_show_fdinfo(struct seq_file *m, struct file *f) { struct socket *sock = f->private_data; + lock_sock(sock->sk); + if (sock->sk->sk_description) + seq_printf(m, "desc:\t%s\n", sock->sk->sk_description); + release_sock(sock->sk); + if (sock->ops->show_fdinfo) sock->ops->show_fdinfo(m, sock); } -- 2.25.1