From nobody Sat Sep 2 11:01:26 2023 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 4RdBm70Hw3z4rgHX; Sat, 2 Sep 2023 11:01:27 +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 4RdBm66v37z4TDr; Sat, 2 Sep 2023 11:01:26 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1693652487; 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=YQK9M6feFmvR6rlLKuT8oszRhdGVo59xxAl3xNxlKD0=; b=rlg+uD8Pcam1UQuHtVMCZd8QJAKuPGX6l8T1JxejFDKDdULLxCyP2TPkKf8gZUZus77WgQ vwcsI7PzbFg9Drrg/TcDnoJt66zR1C6pOO4dAA/u0fBICRnMrJt3pZ/yPpTA4U9mAuwGPC bE5WNBUEHmAQpQqa2p1PxSKmLeyvVeMHw/KQTdsrM234+lR8O0qstHVDx2DTkDDY5ODnam yi4r+a58UCgcgVNP360XJ3waYYp2+zBnv2sX9Amu5oDjyShK2Cd9G7UBQDjiD8ZWCiQ/mC P2OZqS0Pgw7AJwKuIHbWoy2AybcCwfuqg0ScB4FD5dmUmzLIokCCCrz6VOE7Ig== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1693652487; a=rsa-sha256; cv=none; b=wceGGUl/vnPBg24IMYwxiJtxRtsPkvsjfqW7EEpAvK0rMEM8z8g9NIX+o82eU4NZG5/HW2 j9Firj9xTcvZTFNlFqro46JpEYwk5BeYseGzWnG6RtC/Z0ok/+yil44pAS8DNja2e3dD38 vkYd0i4IGx5CGKodgdLdo8u+a9mqMcKPLR+IqSwCNYbnFsbEnFFHHlqW+8QOoN+bkSC/4d Uv1qqV7UVo8cH5n+cZ9UXb0yf3Zr+rhkYhOoe5SFbtOcv69lCkC9KX+F1YPyjEkZqhSrbt OesDq2egBUCvloXHNyaI1z7vQKx/oT2pRzUNWYnCXopxHH+vvYL9/jgTcsR3aA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1693652487; 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=YQK9M6feFmvR6rlLKuT8oszRhdGVo59xxAl3xNxlKD0=; b=A0Yf+b9szlz8zUiJy5ZibQxNeN05sgXuEYx5oZz0iuVw3QGv+0eND9R7ZAoVyP1qlnAGBG eLM5r+02B/CumLzfbge0dh5zqljbydOU09x8UdxJKFQ3HjEfvX+pPJxb22bJmxT287NCbV 9t8iDrs52pvzlGUk/smnybyH17XFBbFPiG07ErxexN2Xs1qI92GTku1zQQb2Bj6CkEh7AE OWORGr5Xs9UN3i7WEhZP9dHHYc+SD4hK9gD7xxCTdzH6r2wSu0aQnQ3qoLPc1klTCsYwTy r7D5REQ4RTUOTiRO/32dK6TnTXRSje0lGIiRH/nkxZ1JjT/+J/GMg0ViL5f8lg== 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 4RdBm65xvkzgCj; Sat, 2 Sep 2023 11:01:26 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 382B1Q12079118; Sat, 2 Sep 2023 11:01:26 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 382B1QV8079115; Sat, 2 Sep 2023 11:01:26 GMT (envelope-from git) Date: Sat, 2 Sep 2023 11:01:26 GMT Message-Id: <202309021101.382B1QV8079115@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Doug Rabson Subject: git: 153a0e6b038c - stable/13 - netstat(1): teach netstat to attach to jails 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: dfr X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 153a0e6b038ceb85d731ebb2ab269e9f783da28b Auto-Submitted: auto-generated The branch stable/13 has been updated by dfr: URL: https://cgit.FreeBSD.org/src/commit/?id=153a0e6b038ceb85d731ebb2ab269e9f783da28b commit 153a0e6b038ceb85d731ebb2ab269e9f783da28b Author: Doug Rabson AuthorDate: 2023-08-14 08:01:28 +0000 Commit: Doug Rabson CommitDate: 2023-09-02 08:08:49 +0000 netstat(1): teach netstat to attach to jails Add -j flag to netstat(1) to allow access to network information from a jail. MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D41446 (cherry picked from commit 182e8ae26d7cad119bcfce7cc788c1f5cb06e226) --- usr.bin/netstat/Makefile | 5 +++ usr.bin/netstat/main.c | 57 +++++++++++++++++++++++++++-------- usr.bin/netstat/netstat.1 | 77 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 13 deletions(-) diff --git a/usr.bin/netstat/Makefile b/usr.bin/netstat/Makefile index 1b999831c160..7888344cbf51 100644 --- a/usr.bin/netstat/Makefile +++ b/usr.bin/netstat/Makefile @@ -62,4 +62,9 @@ LIBADD+= netgraph CFLAGS+=-DNETGRAPH .endif +.if ${MK_JAIL} != "no" && !defined(RESCUE) +CFLAGS+= -DJAIL +LIBADD+= jail +.endif + .include diff --git a/usr.bin/netstat/main.c b/usr.bin/netstat/main.c index 5b05582ca990..a1727d7b7b58 100644 --- a/usr.bin/netstat/main.c +++ b/usr.bin/netstat/main.c @@ -44,6 +44,9 @@ static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 3/1/94"; #include #include #include +#ifdef JAIL +#include +#endif #include #include #include @@ -58,6 +61,9 @@ static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 3/1/94"; #include #include #include +#ifdef JAIL +#include +#endif #include #include #include @@ -231,6 +237,9 @@ int interval; /* repeat interval for i/f stats */ char *interface; /* desired i/f for stats, or NULL for all i/fs */ int unit; /* unit number for above */ +#ifdef JAIL +char *jail_name; /* desired jail to operate in */ +#endif static int af; /* address family */ int live; /* true if we are examining a live system */ @@ -243,6 +252,9 @@ main(int argc, char *argv[]) int fib = -1; char *endptr; bool first = true; +#ifdef JAIL + int jid; +#endif af = AF_UNSPEC; @@ -250,7 +262,7 @@ main(int argc, char *argv[]) if (argc < 0) exit(EXIT_FAILURE); - while ((ch = getopt(argc, argv, "46AaBbCcdF:f:ghI:iLlM:mN:nOoPp:Qq:RrSTsuWw:xz")) + while ((ch = getopt(argc, argv, "46AaBbCcdF:f:ghI:ij:LlM:mN:nOoPp:Qq:RrSTsuWw:xz")) != -1) switch(ch) { case '4': @@ -338,6 +350,15 @@ main(int argc, char *argv[]) case 'i': iflag = 1; break; + case 'j': +#ifdef JAIL + if (optarg == NULL) + usage(); + jail_name = optarg; +#else + errx(1, "Jail support is not compiled in"); +#endif + break; case 'L': Lflag = 1; break; @@ -434,6 +455,16 @@ main(int argc, char *argv[]) } #endif +#ifdef JAIL + if (jail_name != NULL) { + jid = jail_getid(jail_name); + if (jid == -1) + errx(1, "Jail not found"); + if (jail_attach(jid) != 0) + errx(1, "Cannot attach to jail"); + } +#endif + /* * Discard setgid privileges if not the running kernel so that bad * guys can't print interesting stuff from kernel memory. @@ -888,24 +919,24 @@ static void usage(void) { (void)xo_error("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", -"usage: netstat [-46AaCcLnRSTWx] [-f protocol_family | -p protocol]\n" +"usage: netstat [-j jail] [-46AaCcLnRSTWx] [-f protocol_family | -p protocol]\n" " [-M core] [-N system]", -" netstat -i | -I interface [-46abdhnW] [-f address_family]\n" +" netstat [-j jail] -i | -I interface [-46abdhnW] [-f address_family]\n" " [-M core] [-N system]", -" netstat -w wait [-I interface] [-46d] [-M core] [-N system]\n" +" netstat [-j jail] -w wait [-I interface] [-46d] [-M core] [-N system]\n" " [-q howmany]", -" netstat -s [-46sz] [-f protocol_family | -p protocol]\n" +" netstat [-j jail] -s [-46sz] [-f protocol_family | -p protocol]\n" " [-M core] [-N system]", -" netstat -i | -I interface -s [-46s]\n" +" netstat [-j jail] -i | -I interface -s [-46s]\n" " [-f protocol_family | -p protocol] [-M core] [-N system]", -" netstat -m [-M core] [-N system]", -" netstat -B [-z] [-I interface]", -" netstat -r [-46AnW] [-F fibnum] [-f address_family]\n" +" netstat [-j jail] -m [-M core] [-N system]", +" netstat [-j jail] -B [-z] [-I interface]", +" netstat [-j jail] -r [-46AnW] [-F fibnum] [-f address_family]\n" " [-M core] [-N system]", -" netstat -rs [-s] [-M core] [-N system]", -" netstat -g [-46W] [-f address_family] [-M core] [-N system]", -" netstat -gs [-46s] [-f address_family] [-M core] [-N system]", -" netstat -Q"); +" netstat [-j jail] -rs [-s] [-M core] [-N system]", +" netstat [-j jail] -g [-46W] [-f address_family] [-M core] [-N system]", +" netstat [-j jail] -gs [-46s] [-f address_family] [-M core] [-N system]", +" netstat [-j jail] -Q"); xo_finish(); exit(1); } diff --git a/usr.bin/netstat/netstat.1 b/usr.bin/netstat/netstat.1 index 1ea45f608550..d347b31d68d9 100644 --- a/usr.bin/netstat/netstat.1 +++ b/usr.bin/netstat/netstat.1 @@ -37,18 +37,21 @@ .Bk -words .Bl -tag -width "netstat" .It Nm +.Op Fl j Ar jail .Op Fl -libxo .Op Fl 46AaCLnPRSTWx .Op Fl f Ar protocol_family | Fl p Ar protocol .Op Fl M Ar core .Op Fl N Ar system .It Nm Fl i | I Ar interface +.Op Fl j Ar jail .Op Fl -libxo .Op Fl 46abdhnW .Op Fl f Ar address_family .Op Fl M Ar core .Op Fl N Ar system .It Nm Fl w Ar wait +.Op Fl j Ar jail .Op Fl -libxo .Op Fl I Ar interface .Op Fl 46d @@ -56,46 +59,55 @@ .Op Fl N Ar system .Op Fl q Ar howmany .It Nm Fl s +.Op Fl j Ar jail .Op Fl -libxo .Op Fl 46sz .Op Fl f Ar protocol_family | Fl p Ar protocol .Op Fl M Ar core .Op Fl N Ar system .It Nm Fl i | I Ar interface Fl s +.Op Fl j Ar jail .Op Fl -libxo .Op Fl 46s .Op Fl f Ar protocol_family | Fl p Ar protocol .Op Fl M Ar core .Op Fl N Ar system .It Nm Fl m +.Op Fl j Ar jail .Op Fl -libxo .Op Fl M Ar core .Op Fl N Ar system .It Nm Fl B +.Op Fl j Ar jail .Op Fl -libxo .Op Fl z .Op Fl I Ar interface .It Nm Fl r +.Op Fl j Ar jail .Op Fl -libxo .Op Fl 46nW .Op Fl F Ar fibnum .Op Fl f Ar address_family .It Nm Fl rs +.Op Fl j Ar jail .Op Fl -libxo .Op Fl s .Op Fl M Ar core .Op Fl N Ar system .It Nm Fl g +.Op Fl j Ar jail .Op Fl -libxo .Op Fl 46W .Op Fl f Ar address_family .It Nm Fl gs +.Op Fl j Ar jail .Op Fl -libxo .Op Fl 46s .Op Fl f Ar address_family .Op Fl M Ar core .Op Fl N Ar system .It Nm Fl Q +.Op Fl j Ar jail .Op Fl -libxo .El .Ek @@ -114,6 +126,7 @@ depending on the options for the information presented. .Op Fl f Ar protocol_family | Fl p Ar protocol .Op Fl M Ar core .Op Fl N Ar system +.Op Fl j Ar jail .Ek .Xc Display a list of active sockets @@ -252,6 +265,8 @@ See .Sx GENERAL OPTIONS . .It Fl N Use an alternative kernel image. +.It Fl j Ar jail +Run inside a jail. See .Sx GENERAL OPTIONS . .El @@ -263,6 +278,7 @@ See .Op Fl f Ar address_family .Op Fl M Ar core .Op Fl N Ar system +.Op Fl j Ar jail .Ek .Xc Show the state of all network interfaces or a single @@ -324,6 +340,10 @@ Filter by .Ar protocol_family . See .Sx GENERAL OPTIONS . +.It Fl j Ar jail +Run inside a jail. +See +.Sx GENERAL OPTIONS . .El .It Xo .Bk -words @@ -334,6 +354,7 @@ See .Op Fl M Ar core .Op Fl N Ar system .Op Fl q Ar howmany +.Op Fl j Ar jail .Ek .Xc At intervals of @@ -382,6 +403,10 @@ See Exit after .Ar howmany outputs. +.It Fl j Ar jail +Run inside a jail. +See +.Sx GENERAL OPTIONS . .El .It Xo .Bk -words @@ -391,6 +416,7 @@ outputs. .Op Fl f Ar protocol_family | Fl p Ar protocol .Op Fl M Ar core .Op Fl N Ar system +.Op Fl j Ar jail .Ek .Xc Display system-wide statistics for each network protocol. @@ -427,6 +453,10 @@ See Use an alternative kernel image See .Sx GENERAL OPTIONS . +.It Fl j Ar jail +Run inside a jail. +See +.Sx GENERAL OPTIONS . .El .It Xo .Bk -words @@ -436,6 +466,7 @@ See .Op Fl f Ar protocol_family | Fl p Ar protocol .Op Fl M Ar core .Op Fl N Ar system +.Op Fl j Ar jail .Ek .Xc Display per-interface statistics for each network protocol. @@ -470,6 +501,10 @@ See Use an alternative kernel image See .Sx GENERAL OPTIONS . +.It Fl j Ar jail +Run inside a jail. +See +.Sx GENERAL OPTIONS . .El .It Xo .Bk -words @@ -477,6 +512,7 @@ See .Fl m .Op Fl M Ar core .Op Fl N Ar system +.Op Fl j Ar jail .Ek .Xc Show statistics recorded by the memory management routines @@ -491,6 +527,10 @@ See Use an alternative kernel image See .Sx GENERAL OPTIONS . +.It Fl j Ar jail +Run inside a jail. +See +.Sx GENERAL OPTIONS . .El .It Xo .Bk -words @@ -498,6 +538,7 @@ See .Fl B .Op Fl z .Op Fl I Ar interface +.Op Fl j Ar jail .Ek .Xc Show statistics about @@ -534,6 +575,10 @@ For more information about these flags, please refer to .Bl -tag -width indent .It Fl z Reset statistic counters after displaying them. +.It Fl j Ar jail +Run inside a jail. +See +.Sx GENERAL OPTIONS . .El .It Xo .Bk -words @@ -544,6 +589,7 @@ Reset statistic counters after displaying them. .Op Fl f Ar address_family .Op Fl M Ar core .Op Fl N Ar system +.Op Fl j Ar jail .Ek .Xc Display the contents of routing tables. @@ -628,6 +674,10 @@ See Use an alternative kernel image See .Sx GENERAL OPTIONS . +.It Fl j Ar jail +Run inside a jail. +See +.Sx GENERAL OPTIONS . .El .It Xo .Bk -words @@ -636,6 +686,7 @@ See .Op Fl s .Op Fl M Ar core .Op Fl N Ar system +.Op Fl j Ar jail .Ek .Xc Display routing statistics. @@ -652,6 +703,10 @@ See Use an alternative kernel image See .Sx GENERAL OPTIONS . +.It Fl j Ar jail +Run inside a jail. +See +.Sx GENERAL OPTIONS . .El .It Xo .Bk -words @@ -661,6 +716,7 @@ See .Op Fl f Ar address_family .Op Fl M Ar core .Op Fl N Ar system +.Op Fl j Ar jail .Ek .Xc Display the contents of the multicast virtual interface tables, @@ -696,6 +752,10 @@ See Use an alternative kernel image See .Sx GENERAL OPTIONS . +.It Fl j Ar jail +Run inside a jail. +See +.Sx GENERAL OPTIONS . .El .It Xo .Bk -words @@ -705,6 +765,7 @@ See .Op Fl f Ar address_family .Op Fl M Ar core .Op Fl N Ar system +.Op Fl j Ar jail .Ek .Xc Show multicast routing statistics. @@ -734,11 +795,16 @@ See Use an alternative kernel image See .Sx GENERAL OPTIONS . +.It Fl j Ar jail +Run inside a jail. +See +.Sx GENERAL OPTIONS . .El .It Xo .Bk -words .Nm .Fl Q +.Op Fl j Ar jail .Ek .Xc Show @@ -749,6 +815,10 @@ The flags field shows available ISR handlers: .It Li C Ta Dv NETISR_SNP_FLAGS_M2CPUID Ta "Able to map mbuf to cpu id" .It Li D Ta Dv NETISR_SNP_FLAGS_DRAINEDCPU Ta "Has queue drain handler" .It Li F Ta Dv NETISR_SNP_FLAGS_M2FLOW Ta "Able to map mbuf to flow id" +.It Fl j Ar jail +Run inside a jail. +See +.Sx GENERAL OPTIONS . .El .El .Ss GENERAL OPTIONS @@ -808,6 +878,13 @@ Wider output; expand address fields, etc, to avoid truncation. Non-numeric values such as domain names may still be truncated; use the .Fl n option if necessary to avoid ambiguity. +.It Fl j Ar jail +Perform the actions inside the +.Ar jail . +This allows network state to be accessed even if the +.Cm netstat +binary is not available in the +.Ar jail . .El .Sh EXAMPLES Show packet traffic information (packets, bytes, errors, packet drops, etc) for