From nobody Tue May 23 20:56:21 2023 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 4QQmnd6gpPz4T2nw; Tue, 23 May 2023 20:56:21 +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 4QQmnd3qZpz3wBd; Tue, 23 May 2023 20:56:21 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1684875381; 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=Ge5xNhLdD5JOKlvbcx10rsp1pAQ3JRwCSgihlmpSGUA=; b=gLugtUGsLVK36E+WZ49C7AhHSaemP9Wj9eM1fMbWMpbXFrI09yDijEoQm2/yNLUTZaAOh2 1i+UKrfvfVNYVpmUYh9vpAYnGDZS5K8X+RqRCW1AL+eSZwBZZnFjMbv5nbNJ7PuYPVrBtY EngrFcomCOwvNpCKRbIhYWK+hNtnHLGAG3xXCAx6KjZ3qDSzsOMdbE7L7Jacz182Sg7lHf 1fw79ni3GCMMYuX10NzXGg36/dMBc/NC9Z3O47ujQs+tqrTHvCPasu0ZBE1x6oAnPDDqxw 3YZEqwZaGBEbzH6+Orf6eFO9I+cpJvZmjmT6yBix+UhXuW9Ffzla82HhcOfsxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1684875381; 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=Ge5xNhLdD5JOKlvbcx10rsp1pAQ3JRwCSgihlmpSGUA=; b=bF23c1bM+ybtxYv8WZSJ2iGuavh6957aJjH2zLFI1FUPKBrn2Viur8bT6ILYI4nHoYgWbD utAVEWjj4jq9B1zhDoztAcapnwZ84J7bK7/zElnwZ8hHre6Q/20/FXbCzox/8XefwvZvxD nxCJRMBCZlIMl9EAro2xqLeV6CH/GLc3bhiQcJSPIvsNsOl+LfXOwZqIcRVNUaHIIv5Aj+ RWENWTOZI37a0MaEbFqL5EuPuiXCu3I4rL2El2OH9kOwcM9OjamLxwxUFCSMFP7ZDsaUES 8mxsvR1gfnK+JC60OTJfQoRumIgJ5+t1H8EYSzVsmUr1TQz4bn1cQlyFoT8yqA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1684875381; a=rsa-sha256; cv=none; b=Qj4ZxGZqfME+F5Yr1IhBpXrfyziJI4Q9biEjaqTICEdW4qvJ5lkQFqPxjdmWuB8lgjFU6v bqocYmadNUnIrMQ9HNfeGEjB+LRu0fWWDk9UZ0Pap1bz0nA2BFfSodlxUQo0UpbCqWZdRT SJK9et9Vku04BWvoWUMK77fB0FMqQ1IrBiTKcMw5j/SlHC+VjKimMxxC+Ksf9pJPclULXx vHP559934vbwOltA/2QBlAj1o01F/kxIpPb79bLHWz63z2arJSY9Tha1A7fG3dalvjPLRv D/ojw+9IXH/VtIg7iY1n5eDwLx8yALneo9Mvc9H+YQtxJlgfkszfGayO7q7fMw== 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 4QQmnd2wmGzFhX; Tue, 23 May 2023 20:56:21 +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 34NKuL3l059110; Tue, 23 May 2023 20:56:21 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 34NKuLHU059109; Tue, 23 May 2023 20:56:21 GMT (envelope-from git) Date: Tue, 23 May 2023 20:56:21 GMT Message-Id: <202305232056.34NKuLHU059109@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Li-Wen Hsu Subject: git: 7e49aa86a282 - main - ifconfig(8): Teach ifconfig 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: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: lwhsu X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 7e49aa86a2824e76e9d9becf61db12066bc0d79d Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by lwhsu: URL: https://cgit.FreeBSD.org/src/commit/?id=7e49aa86a2824e76e9d9becf61db12066bc0d79d commit 7e49aa86a2824e76e9d9becf61db12066bc0d79d Author: Yan Ka Chiu AuthorDate: 2023-05-23 20:39:22 +0000 Commit: Li-Wen Hsu CommitDate: 2023-05-23 20:39:22 +0000 ifconfig(8): Teach ifconfig to attach and run itself in a jail Add -j flag to ifconfig to allow ifconfig to attach and run inside a jail. This allow parent to configure network interfaces of its children even if ifconfig is not available in child's tree (e.g. Linux Jails) Reviewed by: emaste, khng, melifaro Event: Kitchener-Waterloo Hackathon 202305 MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D40213 --- sbin/ifconfig/ifconfig.8 | 24 +++++++++++++++++++++++- sbin/ifconfig/ifconfig.c | 38 +++++++++++++++++++++++++++++++------- sbin/ifconfig/ifconfig.h | 1 + 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/sbin/ifconfig/ifconfig.8 b/sbin/ifconfig/ifconfig.8 index 4669e96e28e6..a7e1bb26d81a 100644 --- a/sbin/ifconfig/ifconfig.8 +++ b/sbin/ifconfig/ifconfig.8 @@ -28,7 +28,7 @@ .\" From: @(#)ifconfig.8 8.3 (Berkeley) 1/5/94 .\" $FreeBSD$ .\" -.Dd March 6, 2023 +.Dd May 22, 2023 .Dt IFCONFIG 8 .Os .Sh NAME @@ -36,6 +36,7 @@ .Nd configure network interface parameters .Sh SYNOPSIS .Nm +.Op Fl j Ar jail .Op Fl kLmn .Op Fl f Ar type Ns Cm \&: Ns Ar format .Ar interface @@ -49,9 +50,11 @@ .Oc .Op Ar parameters .Nm +.Op Fl j Ar jail .Ar interface .Cm destroy .Nm +.Op Fl j Ar jail .Fl a .Op Fl dkLmuv .Op Fl f Ar type Ns Cm \&: Ns Ar format @@ -61,13 +64,16 @@ .Nm .Fl C .Nm +.Op Fl j Ar jail .Fl g Ar groupname .Nm +.Op Fl j Ar jail .Fl l .Op Fl du .Op Fl g Ar groupname .Op Ar address_family .Nm +.Op Fl j Ar jail .Op Fl dkLmuv .Op Fl f Ar type Ns Cm \&: Ns Ar format .Sh DESCRIPTION @@ -233,6 +239,22 @@ Setting to .Cm all selects all interfaces. +.It Fl j Ar jail +Perform the actions inside the +.Ar jail . +.Pp +The +.Cm ifconfig +will first attach to the +.Ar jail +(by jail id or jail name) before performing the effects. +.Pp +This allow network interfaces of +.Ar jail +to be configured even if the +.Cm ifconfig +binary is not available in +.Ar jail . .It Fl k Print keying information for the .Ar interface , diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c index c5e7b7befc72..e1467e661657 100644 --- a/sbin/ifconfig/ifconfig.c +++ b/sbin/ifconfig/ifconfig.c @@ -42,6 +42,9 @@ static const char rcsid[] = #include #include +#ifdef JAIL +#include +#endif #include #include #include @@ -184,12 +187,12 @@ usage(void) } fprintf(stderr, - "usage: ifconfig [-f type:format] %sinterface address_family\n" + "usage: ifconfig [-j jail] [-f type:format] %sinterface address_family\n" " [address [dest_address]] [parameters]\n" - " ifconfig interface create\n" - " ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n" - " ifconfig -l [-d] [-u] [address_family]\n" - " ifconfig %s[-d] [-m] [-u] [-v]\n", + " ifconfig [-j jail] interface create\n" + " ifconfig [-j jail] -a %s[-d] [-m] [-u] [-v] [address_family]\n" + " ifconfig [-j jail] -l [-d] [-u] [address_family]\n" + " ifconfig [-j jail] %s[-d] [-m] [-u] [-v]\n", options, options, options); exit(1); } @@ -437,7 +440,7 @@ args_parse(struct ifconfig_args *args, int argc, char *argv[]) int c; /* Parse leading line options */ - strlcpy(options, "G:adf:klmnuv", sizeof(options)); + strlcpy(options, "G:adf:j:klmnuv", sizeof(options)); for (p = opts; p != NULL; p = p->next) strlcat(options, p->opt, sizeof(options)); while ((c = getopt(argc, argv, options)) != -1) { @@ -458,6 +461,15 @@ args_parse(struct ifconfig_args *args, int argc, char *argv[]) usage(); args->nogroup = optarg; break; + case 'j': +#ifdef JAIL + if (optarg == NULL) + usage(); + args->jail_name = optarg; +#else + Perror("not built with jail support"); +#endif + break; case 'k': args->printkeys = true; break; @@ -547,7 +559,9 @@ main(int ac, char *av[]) char *envformat; size_t iflen; int flags; - +#ifdef JAIL + int jid; +#endif f_inet = f_inet6 = f_ether = f_addr = NULL; lifh = ifconfig_open(); @@ -566,6 +580,16 @@ main(int ac, char *av[]) args_parse(&args, ac, av); +#ifdef JAIL + if (args.jail_name) { + jid = jail_getid(args.jail_name); + if (jid == -1) + Perror("jail not found"); + if (jail_attach(jid) != 0) + Perror("cannot attach to jail"); + } +#endif + if (!args.all && !args.namesonly) { /* not listing, need an argument */ args.ifname = args_pop(&args); diff --git a/sbin/ifconfig/ifconfig.h b/sbin/ifconfig/ifconfig.h index 7b2b88a4dfac..1df94f357c43 100644 --- a/sbin/ifconfig/ifconfig.h +++ b/sbin/ifconfig/ifconfig.h @@ -219,6 +219,7 @@ struct ifconfig_args { const char *matchgroup; /* Group name to match */ const char *nogroup; /* Group name to exclude */ const struct afswtch *afp; /* AF we're operating on */ + const char *jail_name; /* Jail name or jail id specified */ }; struct option {