From nobody Wed Jan 29 10:01:01 2025 X-Original-To: dev-commits-src-main@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 4Yjd2j5Bhqz5mTTn; Wed, 29 Jan 2025 10:01:01 +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 "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Yjd2j2Srfz3vly; Wed, 29 Jan 2025 10:01:01 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1738144861; 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=nqX31xuRIXT4FAo1llu6h05FlswHLprNYEVL8JRwW8E=; b=fh8B2NAagU6eXF8nyMBOrNlR+WonRZLwJKK5i42m1BDAyDIHDDip6oEej3vr0ZIesV0Oi6 xaYV0lgOfoqWJZeR0tRJvvbNa3IJOoIeu3yi9BLYsPTKTD4cOliYKxhHQ1LHnZfEO6JPlM 23t5x6zmnZ4bh6byECiCHQWLxKlvfcd0B0wLuQs2yoO5cQj18YBVquCHbVJjaBdpZq1KDv Vd80xbRLz5pqPcHxMV3WrSBkCdV0LJArLkMJeKz7qDg0T9Rhv9lCSDWNI8F44ueQECPZME oOmzVKlNx8Bpc5MaLddoKDX4FiN5723EapdzDqdi/o1OqnUeHIqCkpNrrpAdKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1738144861; 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=nqX31xuRIXT4FAo1llu6h05FlswHLprNYEVL8JRwW8E=; b=MPdmEtRP72S/Q1arAJwu/UNj2w55ovRkH7kfbmrX/QaI+9LH7loFECMnBChaQV3D7jVlMt HHZzQlzRwisEXqehh2Fj4EBRn2wpjVJqLxMPUpH1UzGKSJPeOaWt8HnoQj0g+DSFCVx9ad L3Gf81M8u+OMAv9wKETfDH6FJosRyk7aZ6t+rKR/46A0tAt1+IKf51IfHWXpPm0rpjTYn0 f8AnzdISAQLZuVmd5e+ImnreNBIy+3J64vjlnxRrZdERQNIrIP4z5qZchTqjHw4ToewGkY g1NLHv/WM1VTtmNIAKRuViey1Xk9s+YKlM6wtcYMQZjooZTVjnCed3TXbPsmog== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1738144861; a=rsa-sha256; cv=none; b=dvi63u3APrt5IPo8KKRUc4tZwZTxIoFIR27Oy8KSvqnuEUWl3QxscSxgoPp1Bwj9BMB/QP BCNs/VgbccjzTCE9JfvxeIIge9VTLWzC5atzt37We0rmsb3mtNM2II5DxD53dTqxPlHRgb MGRQ0yILdAqZYQ1anym05zJydmmQ8sRpKfIpyVZ5yz/n54EnJwZuq1YKZEzUwsVaqQZukx h5Wa7kGhGTHn/1bwnhXdfCL9er58b6Dn6x6YR5UtyhPYSjUjikVKfZ/0WHhq9pZ6Mvn7tU KEpA+EH0sgbNqg7AZ2HPuawtjmLbcNcam55WG5w2agqaEyDsmmXizfZYxcfZKw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none 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 4Yjd2j1tK6zwkp; Wed, 29 Jan 2025 10:01:01 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 50TA11hs064276; Wed, 29 Jan 2025 10:01:01 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 50TA11eQ064273; Wed, 29 Jan 2025 10:01:01 GMT (envelope-from git) Date: Wed, 29 Jan 2025 10:01:01 GMT Message-Id: <202501291001.50TA11eQ064273@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Zhenlei Huang Subject: git: 6dcdd79a251b - main - ip6addrctl(8): Teach ip6addrctl to attach and run itself in a jail List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: zlei X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 6dcdd79a251b5e62ff9ce562ad451e92dec39408 Auto-Submitted: auto-generated The branch main has been updated by zlei: URL: https://cgit.FreeBSD.org/src/commit/?id=6dcdd79a251b5e62ff9ce562ad451e92dec39408 commit 6dcdd79a251b5e62ff9ce562ad451e92dec39408 Author: Zhenlei Huang AuthorDate: 2025-01-29 10:00:07 +0000 Commit: Zhenlei Huang CommitDate: 2025-01-29 10:00:07 +0000 ip6addrctl(8): Teach ip6addrctl to attach and run itself in a jail This will make it easier to manage address selection policies of vnet jails, especially for those light weighted OCI containers or slim jails. Requested by: dfr Reviewed by: dfr, markj MFC after: 1 week Relnotes: yes Differential Revision: https://reviews.freebsd.org/D48679 --- usr.sbin/ip6addrctl/Makefile | 7 ++++ usr.sbin/ip6addrctl/ip6addrctl.8 | 12 +++++- usr.sbin/ip6addrctl/ip6addrctl.c | 85 ++++++++++++++++++++++++++++++++-------- 3 files changed, 86 insertions(+), 18 deletions(-) diff --git a/usr.sbin/ip6addrctl/Makefile b/usr.sbin/ip6addrctl/Makefile index ba38dc1162d9..356c524629d8 100644 --- a/usr.sbin/ip6addrctl/Makefile +++ b/usr.sbin/ip6addrctl/Makefile @@ -1,5 +1,12 @@ +.include + PACKAGE= runtime PROG= ip6addrctl MAN= ip6addrctl.8 +.if ${MK_JAIL} != "no" +CFLAGS+= -DJAIL +LIBADD+= jail +.endif + .include diff --git a/usr.sbin/ip6addrctl/ip6addrctl.8 b/usr.sbin/ip6addrctl/ip6addrctl.8 index 50245cef91ea..8d3b1a229859 100644 --- a/usr.sbin/ip6addrctl/ip6addrctl.8 +++ b/usr.sbin/ip6addrctl/ip6addrctl.8 @@ -27,7 +27,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd August 10, 2024 +.Dd January 29, 2025 .Dt IP6ADDRCTL 8 .Os .\" @@ -37,16 +37,21 @@ .\" .Sh SYNOPSIS .Nm +.Op Fl j Ar jail .Op Cm show .Nm +.Op Fl j Ar jail .Cm add .Ar prefix precedence label .Nm +.Op Fl j Ar jail .Cm delete .Ar prefix .Nm +.Op Fl j Ar jail .Cm flush .Nm +.Op Fl j Ar jail .Cm install .Ar configfile .\" @@ -62,6 +67,10 @@ is invoked without an argument or with a single argument it prints the content of the policy table currently installed in the kernel. .Pp +To operate inside a jail, the option +.Op Fl j Ar jail +should precede other options. +.Pp To modify the table, the following operations are available: .Bl -tag -width indent .It Cm add Ar prefix precedence label @@ -100,6 +109,7 @@ In the configuration file, lines beginning with the pound-sign .Pq Ql # are comments and are ignored. +Note that the configuration file will be processed before attaching to a jail. .El .\" .Sh EXIT STATUS diff --git a/usr.sbin/ip6addrctl/ip6addrctl.c b/usr.sbin/ip6addrctl/ip6addrctl.c index dd5df48048aa..b42a839c62eb 100644 --- a/usr.sbin/ip6addrctl/ip6addrctl.c +++ b/usr.sbin/ip6addrctl/ip6addrctl.c @@ -36,6 +36,9 @@ #include #include #include +#ifdef JAIL +#include +#endif #include #include @@ -45,12 +48,18 @@ #include #include +#ifdef JAIL +#include +#endif #include #include #include #include #include +#ifdef JAIL +static char *jailname; +#endif static char *configfile; struct policyqueue { @@ -61,6 +70,7 @@ TAILQ_HEAD(policyhead, policyqueue); static struct policyhead policyhead = TAILQ_HEAD_INITIALIZER(policyhead); static void usage(void) __dead2; +static void attach_jail(void); static void get_policy(void); static void dump_policy(void); static int mask2plen(struct sockaddr_in6 *); @@ -75,27 +85,51 @@ static void flush_policy(void); int main(int argc, char *argv[]) { - if (argc == 1 || (argc == 2 && strcasecmp(argv[1], "show") == 0)) { + int ch; + + while ((ch = getopt(argc, argv, "j:")) != -1) { + switch (ch) { + case 'j': +#ifdef JAIL + if ((jailname = optarg) == NULL) + usage(); +#else + errx(1, "not built with jail support"); +#endif + break; + default: + usage(); + } + } + argc -= optind; + argv += optind; + + if (argc == 0 || (argc == 1 && strcasecmp(argv[0], "show") == 0)) { + attach_jail(); get_policy(); dump_policy(); - } else if (strcasecmp(argv[1], "add") == 0) { - if (argc != 5) + } else if (strcasecmp(argv[0], "add") == 0) { + if (argc != 4) usage(); - add_policy(argv[2], argv[3], argv[4]); - } else if (strcasecmp(argv[1], "delete") == 0) { - if (argc != 3) - usage(); - delete_policy(argv[2]); - } else if (strcasecmp(argv[1], "flush") == 0) { + attach_jail(); + add_policy(argv[1], argv[2], argv[3]); + } else if (strcasecmp(argv[0], "delete") == 0) { if (argc != 2) usage(); + attach_jail(); + delete_policy(argv[1]); + } else if (strcasecmp(argv[0], "flush") == 0) { + if (argc != 1) + usage(); + attach_jail(); get_policy(); flush_policy(); - } else if (strcasecmp(argv[1], "install") == 0) { - if (argc != 3) + } else if (strcasecmp(argv[0], "install") == 0) { + if (argc != 2) usage(); - configfile = argv[2]; + configfile = argv[1]; make_policy_fromfile(configfile); + attach_jail(); set_policy(); } else usage(); @@ -103,6 +137,23 @@ main(int argc, char *argv[]) exit(0); } +static void +attach_jail(void) +{ +#ifdef JAIL + int jid; + + if (jailname == NULL) + return; + + jid = jail_getid(jailname); + if (jid == -1) + errx(1, "jail not found"); + if (jail_attach(jid) != 0) + errx(1, "cannot attach to jail"); +#endif +} + static void get_policy(void) { @@ -445,12 +496,12 @@ flush_policy(void) static void usage(void) { - fprintf(stderr, "usage: ip6addrctl [show]\n"); - fprintf(stderr, " ip6addrctl add " + fprintf(stderr, "usage: ip6addrctl [-j jail] [show]\n"); + fprintf(stderr, " ip6addrctl [-j jail] add " "