From nobody Wed Feb 09 12:22:36 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 55EF719BA4F4; Wed, 9 Feb 2022 12:22:37 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4JtzXs0PYLz3Mkt; Wed, 9 Feb 2022 12:22:37 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1644409357; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=87+T12FJ2et+3HaUVRx4gfISXIc1m5k3TNEWcHJon7o=; b=V0SvjNXQy0TvLvehymcI9qLZj5j1esvF18mED8aIJS3IZiRNELRmP2AKZnhv4QCVGuvPHa 1Bn8FuEaBsNA/hXxmhigDYAXcbBA9wBiOse0ZhtPSx0HNLA0MWJWaSMk+7q0hjnxWzeosz 4aDrMByNK5JGVhhSZdPRoXsvtJKtQuoIkQxJBlX/o9emOxbSMhZ9rC3Zlec94Md4RB4r89 VoG5L2/6kEq/XtX/qfw+WQOHIvCci1UNI5HbdUzSpbJXnnKd/sxCLbFCz8UxqBF77Oyd63 iZkkokuYo2M3cke8DzqJpW4YPw4/QG4348+ltgS7RTvHURZbtjCxgdPZlIfrhw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id D97955329; Wed, 9 Feb 2022 12:22:36 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 219CMaGW018511; Wed, 9 Feb 2022 12:22:36 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 219CMa7G018510; Wed, 9 Feb 2022 12:22:36 GMT (envelope-from git) Date: Wed, 9 Feb 2022 12:22:36 GMT Message-Id: <202202091222.219CMa7G018510@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Michael Tuexen Subject: git: a35bdd4489b9 - main - tcp: add sysctl interface for setting socket options List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: tuexen X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: a35bdd4489b97f14cb46ebd0f5354685e4488af3 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1644409357; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=87+T12FJ2et+3HaUVRx4gfISXIc1m5k3TNEWcHJon7o=; b=Wd3sORjCvc1/d0MuvkapR5PlrL9jU7CCjsqheOyy7Hkhp/XlIjSMlj8F1bDzKJvi+/6aUH 0oMOHDoNzsXOWm10yM7KjzLdGBTf2D+nz9pRWVlUFwnQLXddvf+LzRkyj7VygcO9SBf52Y /vuvbbjxjescIaj5qlf8xuEufj27TRZ9oe7ecvA4eJ2OBaE4lOM6rRCp+j5lQ5tsTAe6pD 4h/dEylKDXV23xxdC0pVfA4rTU+Ya3m+7c5EJdWhB8+1yiqebKG5xlBUDiMV/jMk4yZ4j5 TKRh81OF2QOVfZl8A9hHSM5Ydl8RNwiiTnSEg9aptqreCAuw9U0fhGGKiKLwBw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1644409357; a=rsa-sha256; cv=none; b=Wo1Fp2UXYEOhTuFkuPMU/prKSziXMfAmiRZ5Xj2FxwK2MH+W8B45KCPj/jWl5JImP9oKeD LApR94cBLdfyvebA2ayJbilpm4hYqARGqrWAz9u/TODm7GegLtMRUcXmLNRzTdkTQZ3QW/ BfjxVf0Hakz18GSDQtCmWNZPZ1H2M4mujmRvyvy+aAv1Vi0wXRJ1o5rWkq9ln3IGgh+HYM pQ24CQr62j4C9vh8XqO3OgDMs6MbCvYUcBUdJD2YqkvR6DtqCLdKA7nq4fVCN6xeU9oxqG mPoRdbiZ5CvLYFBgtxrwRsqUxy07o63wM0dJBRHhSBe9NMzqWhpSEMlC5zIU8Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by tuexen: URL: https://cgit.FreeBSD.org/src/commit/?id=a35bdd4489b97f14cb46ebd0f5354685e4488af3 commit a35bdd4489b97f14cb46ebd0f5354685e4488af3 Author: Michael Tuexen AuthorDate: 2022-02-09 11:24:41 +0000 Commit: Michael Tuexen CommitDate: 2022-02-09 11:24:41 +0000 tcp: add sysctl interface for setting socket options This interface allows to set a socket option on a TCP endpoint, which is specified by its inp_gencnt. This interface will be used in an upcoming command line tool tcpsso. Reviewed by: glebius, rrs Sponsored by: Netflix, Inc. Differential Revision: https://reviews.freebsd.org/D34138 --- sys/netinet/in_pcb.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ sys/netinet/in_pcb.h | 12 ++++++++ sys/netinet/tcp_subr.c | 12 ++++++++ 3 files changed, 100 insertions(+) diff --git a/sys/netinet/in_pcb.c b/sys/netinet/in_pcb.c index 97471ae7800c..147e0b88214d 100644 --- a/sys/netinet/in_pcb.c +++ b/sys/netinet/in_pcb.c @@ -63,6 +63,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -2799,6 +2800,81 @@ in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi) xi->inp_ip_minttl = inp->inp_ip_minttl; } +int +sysctl_setsockopt(SYSCTL_HANDLER_ARGS, struct inpcbinfo *pcbinfo, + int (*ctloutput_set)(struct inpcb *, struct sockopt *)) +{ + struct sockopt sopt; + struct inpcb_iterator inpi = INP_ALL_ITERATOR(pcbinfo, + INPLOOKUP_WLOCKPCB); + struct inpcb *inp; + struct sockopt_parameters *params; + struct socket *so; + int error; + char buf[1024]; + + if (req->oldptr != NULL || req->oldlen != 0) + return (EINVAL); + if (req->newptr == NULL) + return (EPERM); + if (req->newlen > sizeof(buf)) + return (ENOMEM); + error = SYSCTL_IN(req, buf, req->newlen); + if (error != 0) + return (error); + if (req->newlen < sizeof(struct sockopt_parameters)) + return (EINVAL); + params = (struct sockopt_parameters *)buf; + sopt.sopt_level = params->sop_level; + sopt.sopt_name = params->sop_optname; + sopt.sopt_dir = SOPT_SET; + sopt.sopt_val = params->sop_optval; + sopt.sopt_valsize = req->newlen - sizeof(struct sockopt_parameters); + sopt.sopt_td = NULL; + if (params->sop_inc.inc_flags & INC_ISIPV6) { + if (IN6_IS_SCOPE_LINKLOCAL(¶ms->sop_inc.inc6_laddr)) + params->sop_inc.inc6_laddr.s6_addr16[1] = + htons(params->sop_inc.inc6_zoneid & 0xffff); + if (IN6_IS_SCOPE_LINKLOCAL(¶ms->sop_inc.inc6_faddr)) + params->sop_inc.inc6_faddr.s6_addr16[1] = + htons(params->sop_inc.inc6_zoneid & 0xffff); + } + if (params->sop_inc.inc_lport != htons(0)) { + if (params->sop_inc.inc_fport == htons(0)) + inpi.hash = INP_PCBHASH_WILD(params->sop_inc.inc_lport, + pcbinfo->ipi_hashmask); + else + if (params->sop_inc.inc_flags & INC_ISIPV6) + inpi.hash = INP6_PCBHASH( + ¶ms->sop_inc.inc6_faddr, + params->sop_inc.inc_lport, + params->sop_inc.inc_fport, + pcbinfo->ipi_hashmask); + else + inpi.hash = INP_PCBHASH( + ¶ms->sop_inc.inc_faddr, + params->sop_inc.inc_lport, + params->sop_inc.inc_fport, + pcbinfo->ipi_hashmask); + } + while ((inp = inp_next(&inpi)) != NULL) + if (inp->inp_gencnt == params->sop_id) { + if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { + INP_WUNLOCK(inp); + return (ECONNRESET); + } + so = inp->inp_socket; + KASSERT(so != NULL, ("inp_socket == NULL")); + soref(so); + error = (*ctloutput_set)(inp, &sopt); + sorele(so); + break; + } + if (inp == NULL) + error = ESRCH; + return (error); +} + #ifdef DDB static void db_print_indent(int indent) diff --git a/sys/netinet/in_pcb.h b/sys/netinet/in_pcb.h index 49b891e33b15..c6a8448b8791 100644 --- a/sys/netinet/in_pcb.h +++ b/sys/netinet/in_pcb.h @@ -52,6 +52,7 @@ #include #include #include +#include #include #include #endif @@ -368,7 +369,18 @@ struct xinpgen { so_gen_t xig_sogen; /* socket generation count this time */ uint64_t _xig_spare64[4]; } __aligned(8); + +struct sockopt_parameters { + struct in_conninfo sop_inc; + uint64_t sop_id; + int sop_level; + int sop_optname; + char sop_optval[]; +}; + #ifdef _KERNEL +int sysctl_setsockopt(SYSCTL_HANDLER_ARGS, struct inpcbinfo *pcbinfo, + int (*ctloutput_set)(struct inpcb *, struct sockopt *)); void in_pcbtoxinpcb(const struct inpcb *, struct xinpcb *); #endif #endif /* _SYS_SOCKETVAR_H_ */ diff --git a/sys/netinet/tcp_subr.c b/sys/netinet/tcp_subr.c index 1cd53db3bf99..298ae38d5b27 100644 --- a/sys/netinet/tcp_subr.c +++ b/sys/netinet/tcp_subr.c @@ -3834,6 +3834,18 @@ SYSCTL_PROC(_net_inet_tcp, TCPCTL_DROP, drop, CTLFLAG_NEEDGIANT, NULL, 0, sysctl_drop, "", "Drop TCP connection"); +static int +tcp_sysctl_setsockopt(SYSCTL_HANDLER_ARGS) +{ + return (sysctl_setsockopt(oidp, arg1, arg2, req, &V_tcbinfo, + &tcp_ctloutput_set)); +} + +SYSCTL_PROC(_net_inet_tcp, OID_AUTO, setsockopt, + CTLFLAG_VNET | CTLTYPE_STRUCT | CTLFLAG_WR | CTLFLAG_SKIP | + CTLFLAG_MPSAFE, NULL, 0, tcp_sysctl_setsockopt, "", + "Set socket option for TCP endpoint"); + #ifdef KERN_TLS static int sysctl_switch_tls(SYSCTL_HANDLER_ARGS)