git: 61dc82aab18f - main - arp: Teach arp to attach and run itself in a jail

From: R. Christian McDonald <rcm_at_FreeBSD.org>
Date: Thu, 26 Nov 2026 20:06:57 UTC
The branch main has been updated by rcm:

URL: https://cgit.FreeBSD.org/src/commit/?id=61dc82aab18faae3c970208c269fb6123d7b27c0

commit 61dc82aab18faae3c970208c269fb6123d7b27c0
Author:     R. Christian McDonald <rcm@FreeBSD.org>
AuthorDate: 2026-11-26 20:04:24 +0000
Commit:     R. Christian McDonald <rcm@FreeBSD.org>
CommitDate: 2026-11-26 20:04:24 +0000

    arp: Teach arp to attach and run itself in a jail
    
    Add a -j jail option, which makes arp(8) attach to the named jail
    before it does its work.  This lets the host display and change the ARP
    table of a vnet jail that does not have arp(8) installed, such as a
    thin jail or a Linux jail.  For example, `arp -j foo -an` or
    `arp -j foo -d -a`.
    
    The file given to -f is opened before attaching and processed inside
    the jail, as sysctl(8) and ngctl(8) do.  It must be a regular file or
    a pipe, so that no directory or device descriptor is carried into the
    jail.  Host names are resolved inside the jail, as route(8) and
    netstat(1) do.
    
    Reviewed by:            pouria, zlei, ziaee
    MFC after:              1 week
    Relnotes:               yes
    Sponsored by:           Rubicon Communications, LLC ("Netgate")
    Differential Revision:  https://reviews.freebsd.org/D61725
---
 usr.sbin/arp/Makefile        |  5 +++
 usr.sbin/arp/Makefile.depend |  1 +
 usr.sbin/arp/arp.8           | 31 +++++++++++++++++-
 usr.sbin/arp/arp.c           | 78 ++++++++++++++++++++++++++++++++++----------
 4 files changed, 97 insertions(+), 18 deletions(-)

diff --git a/usr.sbin/arp/Makefile b/usr.sbin/arp/Makefile
index 7b6911f8e0f4..f34fd13df9f1 100644
--- a/usr.sbin/arp/Makefile
+++ b/usr.sbin/arp/Makefile
@@ -14,6 +14,11 @@ CFLAGS+=-DWITHOUT_NETLINK
 
 LIBADD=	xo
 
+.if ${MK_JAIL} != "no"
+CFLAGS+=-DJAIL
+LIBADD+=	jail
+.endif
+
 WARNS?=	3
 
 .include <bsd.prog.mk>
diff --git a/usr.sbin/arp/Makefile.depend b/usr.sbin/arp/Makefile.depend
index 11a91dcad4b2..845d2b50241d 100644
--- a/usr.sbin/arp/Makefile.depend
+++ b/usr.sbin/arp/Makefile.depend
@@ -7,6 +7,7 @@ DIRDEPS = \
 	lib/${CSU_DIR} \
 	lib/libc \
 	lib/libcompiler_rt \
+	lib/libjail \
 	lib/libxo/libxo \
 
 
diff --git a/usr.sbin/arp/arp.8 b/usr.sbin/arp/arp.8
index e1f883e2ca72..4fedbd618649 100644
--- a/usr.sbin/arp/arp.8
+++ b/usr.sbin/arp/arp.8
@@ -25,7 +25,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd July 16, 2025
+.Dd November 26, 2026
 .Dt ARP 8
 .Os
 .Sh NAME
@@ -34,31 +34,38 @@
 .Sh SYNOPSIS
 .Nm
 .Op Fl -libxo Ar options
+.Op Fl j Ar jail
 .Op Fl n
 .Op Fl i Ar interface
 .Ar hostname
 .Nm
 .Op Fl -libxo Ar options
+.Op Fl j Ar jail
 .Op Fl n
 .Op Fl i Ar interface
 .Fl a
 .Nm
+.Op Fl j Ar jail
 .Fl d Ar hostname
 .Nm
+.Op Fl j Ar jail
 .Fl d
 .Op Fl i Ar interface
 .Fl a
 .Nm
+.Op Fl j Ar jail
 .Fl s Ar hostname ether_addr
 .Op Cm temp
 .Op Cm blackhole No \&| Cm reject
 .Op Cm pub
 .Nm
+.Op Fl j Ar jail
 .Fl S Ar hostname ether_addr
 .Op Cm temp
 .Op Cm blackhole No \&| Cm reject
 .Op Cm pub
 .Nm
+.Op Fl j Ar jail
 .Fl f Ar filename
 .Sh DESCRIPTION
 The
@@ -107,6 +114,20 @@ entries on
 .Ar interface .
 Applicable only to the following operations:
 display one, display all, delete all.
+.It Fl j Ar jail
+Perform the operation inside the jail with the given name or JID.
+This is meant for
+.Va vnet
+jails, which have their own network stack and
+.Tn ARP
+table; see
+.Xr jail 8 .
+.Pp
+Host names are resolved inside the jail, using its name service
+configuration and network stack.
+Use numeric addresses and the
+.Fl n
+flag if the jail cannot resolve names.
 .It Fl n
 Show network addresses as numbers (normally
 .Nm
@@ -179,6 +200,13 @@ Leading whitespace and empty lines are ignored.
 A
 .Ql #
 character will mark the rest of the line as a comment.
+.Pp
+With
+.Fl j ,
+the file is opened before attaching to the jail, so
+.Ar filename
+is looked up outside the jail.
+It must be a regular file or a pipe.
 .El
 .Sh SEE ALSO
 .Xr inet 3 ,
@@ -186,6 +214,7 @@ character will mark the rest of the line as a comment.
 .Xr arp 4 ,
 .Xr xo_options 7 ,
 .Xr ifconfig 8 ,
+.Xr jail 8 ,
 .Xr ndp 8
 .Sh HISTORY
 The
diff --git a/usr.sbin/arp/arp.c b/usr.sbin/arp/arp.c
index 055ef2ffe225..f464c15c2a92 100644
--- a/usr.sbin/arp/arp.c
+++ b/usr.sbin/arp/arp.c
@@ -40,8 +40,10 @@
 #include <sys/file.h>
 #include <sys/socket.h>
 #include <sys/sockio.h>
+#include <sys/stat.h>
 #include <sys/sysctl.h>
 #include <sys/ioctl.h>
+#include <sys/jail.h>
 #include <sys/time.h>
 
 #include <net/if.h>
@@ -66,6 +68,9 @@
 #include <strings.h>
 #include <unistd.h>
 #include <ifaddrs.h>
+#ifdef JAIL
+#include <jail.h>
+#endif
 #include <libxo/xo.h>
 #include "arp.h"
 
@@ -78,7 +83,7 @@ static int delete(char *host);
 static void usage(void) __dead2;
 static int set(int argc, char **argv);
 static int get(char *host);
-static int file(char *name);
+static int file(FILE *fp);
 static struct rt_msghdr *rtmsg(int cmd,
     struct sockaddr_in *dst, struct sockaddr_dl *sdl);
 static int set_rtsock(struct sockaddr_in *dst, struct sockaddr_dl *sdl_m,
@@ -104,12 +109,18 @@ main(int argc, char *argv[])
 {
 	int ch, func = 0;
 	int rtn = 0;
+	FILE *fp = NULL;
+#ifdef JAIL
+	char *jail_name = NULL;
+	struct stat sb;
+	int jid;
+#endif
 
 	argc = xo_parse_args(argc, argv);
 	if (argc < 0)
 		exit(1);
 
-	while ((ch = getopt(argc, argv, "andfsSi:")) != -1)
+	while ((ch = getopt(argc, argv, "andfsSi:j:")) != -1)
 		switch(ch) {
 		case 'a':
 			opts.aflag = true;
@@ -132,6 +143,13 @@ main(int argc, char *argv[])
 		case 'i':
 			opts.rifname = optarg;
 			break;
+		case 'j':
+#ifdef JAIL
+			jail_name = optarg;
+#else
+			xo_errx(1, "Jail support is not compiled in");
+#endif
+			break;
 		case '?':
 		default:
 			usage();
@@ -139,6 +157,35 @@ main(int argc, char *argv[])
 	argc -= optind;
 	argv += optind;
 
+	/* The file is named relative to the caller, not to the jail. */
+	if (func == F_FILESET) {
+		if (argc != 1)
+			usage();
+		if ((fp = fopen(argv[0], "r")) == NULL)
+			xo_err(1, "cannot open %s", argv[0]);
+	}
+
+#ifdef JAIL
+	if (jail_name != NULL) {
+		/*
+		 * Jailed root can trace us once we are attached and use fp,
+		 * so do not carry a directory or device into the jail.
+		 */
+		if (fp != NULL) {
+			if (fstat(fileno(fp), &sb) != 0)
+				xo_err(1, "%s", argv[0]);
+			if (!S_ISREG(sb.st_mode) && !S_ISFIFO(sb.st_mode))
+				xo_errx(1, "%s: not a regular file or pipe",
+				    argv[0]);
+		}
+		jid = jail_getid(jail_name);
+		if (jid == -1)
+			xo_errx(1, "%s", jail_errmsg);
+		if (jail_attach(jid) != 0)
+			xo_err(1, "cannot attach to jail %s", jail_name);
+	}
+#endif
+
 	if (!func)
 		func = F_GET;
 	if (opts.rifname) {
@@ -197,9 +244,7 @@ main(int argc, char *argv[])
 		}
 		break;
 	case F_FILESET:
-		if (argc != 1)
-			usage();
-		rtn = file(argv[0]);
+		rtn = file(fp);
 		break;
 	}
 
@@ -213,14 +258,11 @@ main(int argc, char *argv[])
  * Process a file to set standard arp entries
  */
 static int
-file(char *name)
+file(FILE *fp)
 {
-	FILE *fp;
 	int i, retval;
 	char line[100], arg[5][50], *args[5], *p;
 
-	if ((fp = fopen(name, "r")) == NULL)
-		xo_err(1, "cannot open %s", name);
 	args[0] = &arg[0][0];
 	args[1] = &arg[1][0];
 	args[2] = &arg[2][0];
@@ -726,14 +768,16 @@ nuke_entries(uint32_t ifindex, struct in_addr addr)
 static void
 usage(void)
 {
-	xo_error("%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
-	    "usage: arp [-n] [-i interface] hostname",
-	    "       arp [-n] [-i interface] -a",
-	    "       arp -d hostname [pub]",
-	    "       arp -d [-i interface] -a",
-	    "       arp -s hostname ether_addr [temp] [reject | blackhole] [pub [only]]",
-	    "       arp -S hostname ether_addr [temp] [reject | blackhole] [pub [only]]",
-	    "       arp -f filename");
+	xo_error("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
+	    "usage: arp [-j jail] [-n] [-i interface] hostname",
+	    "       arp [-j jail] [-n] [-i interface] -a",
+	    "       arp [-j jail] -d hostname [pub]",
+	    "       arp [-j jail] -d [-i interface] -a",
+	    "       arp [-j jail] -s hostname ether_addr [temp] [reject | blackhole]",
+	    "           [pub [only]]",
+	    "       arp [-j jail] -S hostname ether_addr [temp] [reject | blackhole]",
+	    "           [pub [only]]",
+	    "       arp [-j jail] -f filename");
 	exit(1);
 }