xref: /freebsd/sys/kern/kern_jail.c (revision 7cbf72137fadbb75662d02cabe4c7a0b1d51ab6d)
19454b2d8SWarner Losh /*-
2413628a7SBjoern A. Zeeb  * Copyright (c) 1999 Poul-Henning Kamp.
3413628a7SBjoern A. Zeeb  * Copyright (c) 2008 Bjoern A. Zeeb.
4b38ff370SJamie Gritton  * Copyright (c) 2009 James Gritton.
5413628a7SBjoern A. Zeeb  * All rights reserved.
6b9f0b66cSBjoern A. Zeeb  *
7b9f0b66cSBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
8b9f0b66cSBjoern A. Zeeb  * modification, are permitted provided that the following conditions
9b9f0b66cSBjoern A. Zeeb  * are met:
10b9f0b66cSBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
11b9f0b66cSBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer.
12b9f0b66cSBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
13b9f0b66cSBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer in the
14b9f0b66cSBjoern A. Zeeb  *    documentation and/or other materials provided with the distribution.
15b9f0b66cSBjoern A. Zeeb  *
16b9f0b66cSBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17b9f0b66cSBjoern A. Zeeb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18b9f0b66cSBjoern A. Zeeb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19b9f0b66cSBjoern A. Zeeb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20b9f0b66cSBjoern A. Zeeb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21b9f0b66cSBjoern A. Zeeb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22b9f0b66cSBjoern A. Zeeb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23b9f0b66cSBjoern A. Zeeb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24b9f0b66cSBjoern A. Zeeb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25b9f0b66cSBjoern A. Zeeb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26b9f0b66cSBjoern A. Zeeb  * SUCH DAMAGE.
2707901f22SPoul-Henning Kamp  */
2875c13541SPoul-Henning Kamp 
29677b542eSDavid E. O'Brien #include <sys/cdefs.h>
30677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
31677b542eSDavid E. O'Brien 
3276ca6f88SJamie Gritton #include "opt_compat.h"
33413628a7SBjoern A. Zeeb #include "opt_ddb.h"
34413628a7SBjoern A. Zeeb #include "opt_inet.h"
35413628a7SBjoern A. Zeeb #include "opt_inet6.h"
3646e3b1cbSPawel Jakub Dawidek 
3775c13541SPoul-Henning Kamp #include <sys/param.h>
3875c13541SPoul-Henning Kamp #include <sys/types.h>
3975c13541SPoul-Henning Kamp #include <sys/kernel.h>
4075c13541SPoul-Henning Kamp #include <sys/systm.h>
4175c13541SPoul-Henning Kamp #include <sys/errno.h>
4275c13541SPoul-Henning Kamp #include <sys/sysproto.h>
4375c13541SPoul-Henning Kamp #include <sys/malloc.h>
440304c731SJamie Gritton #include <sys/osd.h>
45800c9408SRobert Watson #include <sys/priv.h>
4675c13541SPoul-Henning Kamp #include <sys/proc.h>
47b3059e09SRobert Watson #include <sys/taskqueue.h>
4857b4252eSKonstantin Belousov #include <sys/fcntl.h>
4975c13541SPoul-Henning Kamp #include <sys/jail.h>
5001137630SRobert Watson #include <sys/lock.h>
5101137630SRobert Watson #include <sys/mutex.h>
52dc68a633SPawel Jakub Dawidek #include <sys/sx.h>
5376ca6f88SJamie Gritton #include <sys/sysent.h>
54fd7a8150SMike Barcroft #include <sys/namei.h>
55820a0de9SPawel Jakub Dawidek #include <sys/mount.h>
56fd7a8150SMike Barcroft #include <sys/queue.h>
5775c13541SPoul-Henning Kamp #include <sys/socket.h>
58fd7a8150SMike Barcroft #include <sys/syscallsubr.h>
5983f1e257SRobert Watson #include <sys/sysctl.h>
60fd7a8150SMike Barcroft #include <sys/vnode.h>
61603724d3SBjoern A. Zeeb #include <sys/vimage.h>
6275c13541SPoul-Henning Kamp #include <net/if.h>
6375c13541SPoul-Henning Kamp #include <netinet/in.h>
64413628a7SBjoern A. Zeeb #ifdef DDB
65413628a7SBjoern A. Zeeb #include <ddb/ddb.h>
66413628a7SBjoern A. Zeeb #ifdef INET6
67413628a7SBjoern A. Zeeb #include <netinet6/in6_var.h>
68413628a7SBjoern A. Zeeb #endif /* INET6 */
69413628a7SBjoern A. Zeeb #endif /* DDB */
7075c13541SPoul-Henning Kamp 
71aed55708SRobert Watson #include <security/mac/mac_framework.h>
72aed55708SRobert Watson 
7375c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
7475c13541SPoul-Henning Kamp 
750304c731SJamie Gritton /* prison0 describes what is "real" about the system. */
760304c731SJamie Gritton struct prison prison0 = {
770304c731SJamie Gritton 	.pr_id		= 0,
780304c731SJamie Gritton 	.pr_name	= "0",
790304c731SJamie Gritton 	.pr_ref		= 1,
800304c731SJamie Gritton 	.pr_uref	= 1,
810304c731SJamie Gritton 	.pr_path	= "/",
820304c731SJamie Gritton 	.pr_securelevel	= -1,
83b97457e2SJamie Gritton 	.pr_childmax	= JAIL_MAX,
84c1f19219SJamie Gritton 	.pr_hostuuid	= "00000000-0000-0000-0000-000000000000",
850304c731SJamie Gritton 	.pr_children	= LIST_HEAD_INITIALIZER(&prison0.pr_children),
8676ca6f88SJamie Gritton 	.pr_flags	= PR_HOST,
870304c731SJamie Gritton 	.pr_allow	= PR_ALLOW_ALL,
880304c731SJamie Gritton };
890304c731SJamie Gritton MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
9083f1e257SRobert Watson 
910304c731SJamie Gritton /* allprison and lastprid are protected by allprison_lock. */
92dc68a633SPawel Jakub Dawidek struct	sx allprison_lock;
93b38ff370SJamie Gritton SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
94b38ff370SJamie Gritton struct	prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
952110d913SXin LI int	lastprid = 0;
96fd7a8150SMike Barcroft 
97b38ff370SJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr);
98b3059e09SRobert Watson static void prison_complete(void *context, int pending);
99b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags);
1000304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2);
1010304c731SJamie Gritton static void prison_remove_one(struct prison *pr);
102413628a7SBjoern A. Zeeb #ifdef INET
1038571af59SJamie Gritton static int _prison_check_ip4(struct prison *pr, struct in_addr *ia);
1040304c731SJamie Gritton static int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4);
105413628a7SBjoern A. Zeeb #endif
106413628a7SBjoern A. Zeeb #ifdef INET6
1078571af59SJamie Gritton static int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6);
1080304c731SJamie Gritton static int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6);
109413628a7SBjoern A. Zeeb #endif
110fd7a8150SMike Barcroft 
111b38ff370SJamie Gritton /* Flags for prison_deref */
112b38ff370SJamie Gritton #define	PD_DEREF	0x01
113b38ff370SJamie Gritton #define	PD_DEUREF	0x02
114b38ff370SJamie Gritton #define	PD_LOCKED	0x04
115b38ff370SJamie Gritton #define	PD_LIST_SLOCKED	0x08
116b38ff370SJamie Gritton #define	PD_LIST_XLOCKED	0x10
117fd7a8150SMike Barcroft 
1180304c731SJamie Gritton /*
1190304c731SJamie Gritton  * Parameter names corresponding to PR_* flag values
1200304c731SJamie Gritton  */
1210304c731SJamie Gritton static char *pr_flag_names[] = {
1220304c731SJamie Gritton 	[0] = "persist",
1230304c731SJamie Gritton };
1240304c731SJamie Gritton 
1250304c731SJamie Gritton static char *pr_flag_nonames[] = {
1260304c731SJamie Gritton 	[0] = "nopersist",
1277cbf7213SJamie Gritton };
1287cbf7213SJamie Gritton 
1297cbf7213SJamie Gritton struct jailsys_flags {
1307cbf7213SJamie Gritton 	const char	*name;
1317cbf7213SJamie Gritton 	unsigned	 disable;
1327cbf7213SJamie Gritton 	unsigned	 new;
1337cbf7213SJamie Gritton } pr_flag_jailsys[] = {
1347cbf7213SJamie Gritton 	{ "host", 0, PR_HOST },
1357cbf7213SJamie Gritton #ifdef VIMAGE
1367cbf7213SJamie Gritton 	{ "vnet", 0, PR_VNET },
1377cbf7213SJamie Gritton #endif
1380304c731SJamie Gritton #ifdef INET
1397cbf7213SJamie Gritton 	{ "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER },
1400304c731SJamie Gritton #endif
1410304c731SJamie Gritton #ifdef INET6
1427cbf7213SJamie Gritton 	{ "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER },
143679e1390SJamie Gritton #endif
1440304c731SJamie Gritton };
1450304c731SJamie Gritton 
1460304c731SJamie Gritton static char *pr_allow_names[] = {
1470304c731SJamie Gritton 	"allow.set_hostname",
1480304c731SJamie Gritton 	"allow.sysvipc",
1490304c731SJamie Gritton 	"allow.raw_sockets",
1500304c731SJamie Gritton 	"allow.chflags",
1510304c731SJamie Gritton 	"allow.mount",
1520304c731SJamie Gritton 	"allow.quotas",
1530304c731SJamie Gritton 	"allow.socket_af",
1540304c731SJamie Gritton };
1550304c731SJamie Gritton 
1560304c731SJamie Gritton static char *pr_allow_nonames[] = {
1570304c731SJamie Gritton 	"allow.noset_hostname",
1580304c731SJamie Gritton 	"allow.nosysvipc",
1590304c731SJamie Gritton 	"allow.noraw_sockets",
1600304c731SJamie Gritton 	"allow.nochflags",
1610304c731SJamie Gritton 	"allow.nomount",
1620304c731SJamie Gritton 	"allow.noquotas",
1630304c731SJamie Gritton 	"allow.nosocket_af",
1640304c731SJamie Gritton };
1650304c731SJamie Gritton 
1660304c731SJamie Gritton #define	JAIL_DEFAULT_ALLOW	PR_ALLOW_SET_HOSTNAME
1670304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
1680304c731SJamie Gritton static int jail_default_enforce_statfs = 2;
1690304c731SJamie Gritton #if defined(INET) || defined(INET6)
170e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255;
1710304c731SJamie Gritton #endif
1720304c731SJamie Gritton 
173413628a7SBjoern A. Zeeb #ifdef INET
174413628a7SBjoern A. Zeeb static int
175413628a7SBjoern A. Zeeb qcmp_v4(const void *ip1, const void *ip2)
176413628a7SBjoern A. Zeeb {
177413628a7SBjoern A. Zeeb 	in_addr_t iaa, iab;
178413628a7SBjoern A. Zeeb 
179413628a7SBjoern A. Zeeb 	/*
180413628a7SBjoern A. Zeeb 	 * We need to compare in HBO here to get the list sorted as expected
181413628a7SBjoern A. Zeeb 	 * by the result of the code.  Sorting NBO addresses gives you
182413628a7SBjoern A. Zeeb 	 * interesting results.  If you do not understand, do not try.
183413628a7SBjoern A. Zeeb 	 */
184413628a7SBjoern A. Zeeb 	iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
185413628a7SBjoern A. Zeeb 	iab = ntohl(((const struct in_addr *)ip2)->s_addr);
186413628a7SBjoern A. Zeeb 
187413628a7SBjoern A. Zeeb 	/*
188413628a7SBjoern A. Zeeb 	 * Do not simply return the difference of the two numbers, the int is
189413628a7SBjoern A. Zeeb 	 * not wide enough.
190413628a7SBjoern A. Zeeb 	 */
191413628a7SBjoern A. Zeeb 	if (iaa > iab)
192413628a7SBjoern A. Zeeb 		return (1);
193413628a7SBjoern A. Zeeb 	else if (iaa < iab)
194413628a7SBjoern A. Zeeb 		return (-1);
195413628a7SBjoern A. Zeeb 	else
196413628a7SBjoern A. Zeeb 		return (0);
197413628a7SBjoern A. Zeeb }
198413628a7SBjoern A. Zeeb #endif
199413628a7SBjoern A. Zeeb 
200413628a7SBjoern A. Zeeb #ifdef INET6
201413628a7SBjoern A. Zeeb static int
202413628a7SBjoern A. Zeeb qcmp_v6(const void *ip1, const void *ip2)
203413628a7SBjoern A. Zeeb {
204413628a7SBjoern A. Zeeb 	const struct in6_addr *ia6a, *ia6b;
205413628a7SBjoern A. Zeeb 	int i, rc;
206413628a7SBjoern A. Zeeb 
207413628a7SBjoern A. Zeeb 	ia6a = (const struct in6_addr *)ip1;
208413628a7SBjoern A. Zeeb 	ia6b = (const struct in6_addr *)ip2;
209413628a7SBjoern A. Zeeb 
210413628a7SBjoern A. Zeeb 	rc = 0;
211413628a7SBjoern A. Zeeb 	for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
212413628a7SBjoern A. Zeeb 		if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
213413628a7SBjoern A. Zeeb 			rc = 1;
214413628a7SBjoern A. Zeeb 		else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
215413628a7SBjoern A. Zeeb 			rc = -1;
216413628a7SBjoern A. Zeeb 	}
217413628a7SBjoern A. Zeeb 	return (rc);
218413628a7SBjoern A. Zeeb }
219413628a7SBjoern A. Zeeb #endif
220413628a7SBjoern A. Zeeb 
221116734c4SMatthew Dillon /*
2229ddb7954SMike Barcroft  * struct jail_args {
2239ddb7954SMike Barcroft  *	struct jail *jail;
2249ddb7954SMike Barcroft  * };
225116734c4SMatthew Dillon  */
22675c13541SPoul-Henning Kamp int
2279ddb7954SMike Barcroft jail(struct thread *td, struct jail_args *uap)
22875c13541SPoul-Henning Kamp {
229413628a7SBjoern A. Zeeb 	uint32_t version;
230413628a7SBjoern A. Zeeb 	int error;
2310304c731SJamie Gritton 	struct jail j;
232413628a7SBjoern A. Zeeb 
233413628a7SBjoern A. Zeeb 	error = copyin(uap->jail, &version, sizeof(uint32_t));
234413628a7SBjoern A. Zeeb 	if (error)
235413628a7SBjoern A. Zeeb 		return (error);
236413628a7SBjoern A. Zeeb 
237413628a7SBjoern A. Zeeb 	switch (version) {
238413628a7SBjoern A. Zeeb 	case 0:
239413628a7SBjoern A. Zeeb 	{
240413628a7SBjoern A. Zeeb 		struct jail_v0 j0;
241413628a7SBjoern A. Zeeb 
2420304c731SJamie Gritton 		/* FreeBSD single IPv4 jails. */
2430304c731SJamie Gritton 		bzero(&j, sizeof(struct jail));
244413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
245413628a7SBjoern A. Zeeb 		if (error)
246413628a7SBjoern A. Zeeb 			return (error);
2470304c731SJamie Gritton 		j.version = j0.version;
2480304c731SJamie Gritton 		j.path = j0.path;
2490304c731SJamie Gritton 		j.hostname = j0.hostname;
2500304c731SJamie Gritton 		j.ip4s = j0.ip_number;
251413628a7SBjoern A. Zeeb 		break;
252413628a7SBjoern A. Zeeb 	}
253413628a7SBjoern A. Zeeb 
254413628a7SBjoern A. Zeeb 	case 1:
255413628a7SBjoern A. Zeeb 		/*
256413628a7SBjoern A. Zeeb 		 * Version 1 was used by multi-IPv4 jail implementations
257413628a7SBjoern A. Zeeb 		 * that never made it into the official kernel.
258413628a7SBjoern A. Zeeb 		 */
259413628a7SBjoern A. Zeeb 		return (EINVAL);
260413628a7SBjoern A. Zeeb 
261413628a7SBjoern A. Zeeb 	case 2:	/* JAIL_API_VERSION */
262413628a7SBjoern A. Zeeb 		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
263413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j, sizeof(struct jail));
264413628a7SBjoern A. Zeeb 		if (error)
265413628a7SBjoern A. Zeeb 			return (error);
2660304c731SJamie Gritton 		break;
2670304c731SJamie Gritton 
2680304c731SJamie Gritton 	default:
2690304c731SJamie Gritton 		/* Sci-Fi jails are not supported, sorry. */
2700304c731SJamie Gritton 		return (EINVAL);
2710304c731SJamie Gritton 	}
2720304c731SJamie Gritton 	return (kern_jail(td, &j));
2730304c731SJamie Gritton }
2740304c731SJamie Gritton 
2750304c731SJamie Gritton int
2760304c731SJamie Gritton kern_jail(struct thread *td, struct jail *j)
2770304c731SJamie Gritton {
278e92e0574SJamie Gritton 	struct iovec optiov[2 * (4
279e92e0574SJamie Gritton 			    + sizeof(pr_allow_names) / sizeof(pr_allow_names[0])
280e92e0574SJamie Gritton #ifdef INET
281e92e0574SJamie Gritton 			    + 1
282e92e0574SJamie Gritton #endif
283e92e0574SJamie Gritton #ifdef INET6
284e92e0574SJamie Gritton 			    + 1
285e92e0574SJamie Gritton #endif
286e92e0574SJamie Gritton 			    )];
2870304c731SJamie Gritton 	struct uio opt;
2880304c731SJamie Gritton 	char *u_path, *u_hostname, *u_name;
2890304c731SJamie Gritton #ifdef INET
290e92e0574SJamie Gritton 	uint32_t ip4s;
2910304c731SJamie Gritton 	struct in_addr *u_ip4;
2920304c731SJamie Gritton #endif
2930304c731SJamie Gritton #ifdef INET6
2940304c731SJamie Gritton 	struct in6_addr *u_ip6;
2950304c731SJamie Gritton #endif
2960304c731SJamie Gritton 	size_t tmplen;
2970304c731SJamie Gritton 	int error, enforce_statfs, fi;
2980304c731SJamie Gritton 
2990304c731SJamie Gritton 	bzero(&optiov, sizeof(optiov));
3000304c731SJamie Gritton 	opt.uio_iov = optiov;
3010304c731SJamie Gritton 	opt.uio_iovcnt = 0;
3020304c731SJamie Gritton 	opt.uio_offset = -1;
3030304c731SJamie Gritton 	opt.uio_resid = -1;
3040304c731SJamie Gritton 	opt.uio_segflg = UIO_SYSSPACE;
3050304c731SJamie Gritton 	opt.uio_rw = UIO_READ;
3060304c731SJamie Gritton 	opt.uio_td = td;
3070304c731SJamie Gritton 
3080304c731SJamie Gritton 	/* Set permissions for top-level jails from sysctls. */
3090304c731SJamie Gritton 	if (!jailed(td->td_ucred)) {
3100304c731SJamie Gritton 		for (fi = 0; fi < sizeof(pr_allow_names) /
3110304c731SJamie Gritton 		     sizeof(pr_allow_names[0]); fi++) {
3120304c731SJamie Gritton 			optiov[opt.uio_iovcnt].iov_base =
3130304c731SJamie Gritton 			    (jail_default_allow & (1 << fi))
3140304c731SJamie Gritton 			    ? pr_allow_names[fi] : pr_allow_nonames[fi];
3150304c731SJamie Gritton 			optiov[opt.uio_iovcnt].iov_len =
3160304c731SJamie Gritton 			    strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
3170304c731SJamie Gritton 			opt.uio_iovcnt += 2;
3180304c731SJamie Gritton 		}
3190304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
3200304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
3210304c731SJamie Gritton 		opt.uio_iovcnt++;
3220304c731SJamie Gritton 		enforce_statfs = jail_default_enforce_statfs;
3230304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
3240304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
3250304c731SJamie Gritton 		opt.uio_iovcnt++;
3260304c731SJamie Gritton 	}
3270304c731SJamie Gritton 
328b38ff370SJamie Gritton 	tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
329b38ff370SJamie Gritton #ifdef INET
3300304c731SJamie Gritton 	ip4s = (j->version == 0) ? 1 : j->ip4s;
3310304c731SJamie Gritton 	if (ip4s > jail_max_af_ips)
332b38ff370SJamie Gritton 		return (EINVAL);
3330304c731SJamie Gritton 	tmplen += ip4s * sizeof(struct in_addr);
334b38ff370SJamie Gritton #else
3350304c731SJamie Gritton 	if (j->ip4s > 0)
336b38ff370SJamie Gritton 		return (EINVAL);
337b38ff370SJamie Gritton #endif
338b38ff370SJamie Gritton #ifdef INET6
3390304c731SJamie Gritton 	if (j->ip6s > jail_max_af_ips)
340b38ff370SJamie Gritton 		return (EINVAL);
3410304c731SJamie Gritton 	tmplen += j->ip6s * sizeof(struct in6_addr);
342b38ff370SJamie Gritton #else
3430304c731SJamie Gritton 	if (j->ip6s > 0)
344b38ff370SJamie Gritton 		return (EINVAL);
345b38ff370SJamie Gritton #endif
346b38ff370SJamie Gritton 	u_path = malloc(tmplen, M_TEMP, M_WAITOK);
347b38ff370SJamie Gritton 	u_hostname = u_path + MAXPATHLEN;
348b38ff370SJamie Gritton 	u_name = u_hostname + MAXHOSTNAMELEN;
349b38ff370SJamie Gritton #ifdef INET
350b38ff370SJamie Gritton 	u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
351b38ff370SJamie Gritton #endif
352b38ff370SJamie Gritton #ifdef INET6
353b38ff370SJamie Gritton #ifdef INET
3540304c731SJamie Gritton 	u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
355b38ff370SJamie Gritton #else
356b38ff370SJamie Gritton 	u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
357b38ff370SJamie Gritton #endif
358b38ff370SJamie Gritton #endif
3590304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "path";
3600304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("path");
3610304c731SJamie Gritton 	opt.uio_iovcnt++;
3620304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_path;
3630304c731SJamie Gritton 	error = copyinstr(j->path, u_path, MAXPATHLEN,
3640304c731SJamie Gritton 	    &optiov[opt.uio_iovcnt].iov_len);
365b38ff370SJamie Gritton 	if (error) {
366b38ff370SJamie Gritton 		free(u_path, M_TEMP);
367b38ff370SJamie Gritton 		return (error);
368b38ff370SJamie Gritton 	}
3690304c731SJamie Gritton 	opt.uio_iovcnt++;
3700304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "host.hostname";
3710304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
3720304c731SJamie Gritton 	opt.uio_iovcnt++;
3730304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_hostname;
3740304c731SJamie Gritton 	error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
3750304c731SJamie Gritton 	    &optiov[opt.uio_iovcnt].iov_len);
376b38ff370SJamie Gritton 	if (error) {
377b38ff370SJamie Gritton 		free(u_path, M_TEMP);
378b38ff370SJamie Gritton 		return (error);
379b38ff370SJamie Gritton 	}
3800304c731SJamie Gritton 	opt.uio_iovcnt++;
3810304c731SJamie Gritton 	if (j->jailname != NULL) {
382b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = "name";
383b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof("name");
384b38ff370SJamie Gritton 		opt.uio_iovcnt++;
385b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = u_name;
3860304c731SJamie Gritton 		error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
387b38ff370SJamie Gritton 		    &optiov[opt.uio_iovcnt].iov_len);
388b38ff370SJamie Gritton 		if (error) {
389b38ff370SJamie Gritton 			free(u_path, M_TEMP);
390b38ff370SJamie Gritton 			return (error);
391b38ff370SJamie Gritton 		}
392b38ff370SJamie Gritton 		opt.uio_iovcnt++;
393b38ff370SJamie Gritton 	}
394b38ff370SJamie Gritton #ifdef INET
395b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
396b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
397b38ff370SJamie Gritton 	opt.uio_iovcnt++;
398b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_ip4;
3990304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
4000304c731SJamie Gritton 	if (j->version == 0)
4010304c731SJamie Gritton 		u_ip4->s_addr = j->ip4s;
4020304c731SJamie Gritton 	else {
4030304c731SJamie Gritton 		error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
404b38ff370SJamie Gritton 		if (error) {
405b38ff370SJamie Gritton 			free(u_path, M_TEMP);
406b38ff370SJamie Gritton 			return (error);
407b38ff370SJamie Gritton 		}
4080304c731SJamie Gritton 	}
409b38ff370SJamie Gritton 	opt.uio_iovcnt++;
410b38ff370SJamie Gritton #endif
411b38ff370SJamie Gritton #ifdef INET6
412b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
413b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
414b38ff370SJamie Gritton 	opt.uio_iovcnt++;
415b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_ip6;
4160304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
4170304c731SJamie Gritton 	error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
418b38ff370SJamie Gritton 	if (error) {
419b38ff370SJamie Gritton 		free(u_path, M_TEMP);
420b38ff370SJamie Gritton 		return (error);
421b38ff370SJamie Gritton 	}
422b38ff370SJamie Gritton 	opt.uio_iovcnt++;
423b38ff370SJamie Gritton #endif
4240304c731SJamie Gritton 	KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]),
4250304c731SJamie Gritton 	    ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
426b38ff370SJamie Gritton 	error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
427b38ff370SJamie Gritton 	free(u_path, M_TEMP);
428b38ff370SJamie Gritton 	return (error);
429b38ff370SJamie Gritton }
430b38ff370SJamie Gritton 
4310304c731SJamie Gritton 
432b38ff370SJamie Gritton /*
433b38ff370SJamie Gritton  * struct jail_set_args {
434b38ff370SJamie Gritton  *	struct iovec *iovp;
435b38ff370SJamie Gritton  *	unsigned int iovcnt;
436b38ff370SJamie Gritton  *	int flags;
437b38ff370SJamie Gritton  * };
438b38ff370SJamie Gritton  */
439b38ff370SJamie Gritton int
440b38ff370SJamie Gritton jail_set(struct thread *td, struct jail_set_args *uap)
441b38ff370SJamie Gritton {
442b38ff370SJamie Gritton 	struct uio *auio;
443b38ff370SJamie Gritton 	int error;
444b38ff370SJamie Gritton 
445b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
446b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
447b38ff370SJamie Gritton 		return (EINVAL);
448b38ff370SJamie Gritton 
449b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
450b38ff370SJamie Gritton 	if (error)
451b38ff370SJamie Gritton 		return (error);
452b38ff370SJamie Gritton 	error = kern_jail_set(td, auio, uap->flags);
453b38ff370SJamie Gritton 	free(auio, M_IOV);
454b38ff370SJamie Gritton 	return (error);
455413628a7SBjoern A. Zeeb }
456413628a7SBjoern A. Zeeb 
457413628a7SBjoern A. Zeeb int
458b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags)
459413628a7SBjoern A. Zeeb {
460fd7a8150SMike Barcroft 	struct nameidata nd;
461b38ff370SJamie Gritton #ifdef INET
462b38ff370SJamie Gritton 	struct in_addr *ip4;
463413628a7SBjoern A. Zeeb #endif
464b38ff370SJamie Gritton #ifdef INET6
465b38ff370SJamie Gritton 	struct in6_addr *ip6;
466b38ff370SJamie Gritton #endif
467b38ff370SJamie Gritton 	struct vfsopt *opt;
468b38ff370SJamie Gritton 	struct vfsoptlist *opts;
4690304c731SJamie Gritton 	struct prison *pr, *deadpr, *mypr, *ppr, *tpr;
470b38ff370SJamie Gritton 	struct vnode *root;
47176ca6f88SJamie Gritton 	char *domain, *errmsg, *host, *name, *p, *path, *uuid;
4720304c731SJamie Gritton #if defined(INET) || defined(INET6)
473b38ff370SJamie Gritton 	void *op;
4740304c731SJamie Gritton #endif
47576ca6f88SJamie Gritton 	unsigned long hid;
4760304c731SJamie Gritton 	size_t namelen, onamelen;
4770304c731SJamie Gritton 	int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos;
4787cbf7213SJamie Gritton 	int gotchildmax, gotenforce, gothid, gotslevel;
4797cbf7213SJamie Gritton 	int fi, jid, jsys, len, level;
480b97457e2SJamie Gritton 	int childmax, slevel, vfslocked;
481d465a41dSBjoern A. Zeeb #if defined(INET) || defined(INET6)
4820304c731SJamie Gritton 	int ii, ij;
483413628a7SBjoern A. Zeeb #endif
484413628a7SBjoern A. Zeeb #ifdef INET
4850304c731SJamie Gritton 	int ip4s, ip4a, redo_ip4;
486413628a7SBjoern A. Zeeb #endif
487b38ff370SJamie Gritton #ifdef INET6
4880304c731SJamie Gritton 	int ip6s, ip6a, redo_ip6;
489b38ff370SJamie Gritton #endif
490b38ff370SJamie Gritton 	unsigned pr_flags, ch_flags;
4910304c731SJamie Gritton 	unsigned pr_allow, ch_allow, tallow;
492b38ff370SJamie Gritton 	char numbuf[12];
493b38ff370SJamie Gritton 
494b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_SET);
495b38ff370SJamie Gritton 	if (!error && (flags & JAIL_ATTACH))
496b38ff370SJamie Gritton 		error = priv_check(td, PRIV_JAIL_ATTACH);
497b38ff370SJamie Gritton 	if (error)
49875c13541SPoul-Henning Kamp 		return (error);
4990304c731SJamie Gritton 	mypr = ppr = td->td_ucred->cr_prison;
500b97457e2SJamie Gritton 	if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
5010304c731SJamie Gritton 		return (EPERM);
502b38ff370SJamie Gritton 	if (flags & ~JAIL_SET_MASK)
503b38ff370SJamie Gritton 		return (EINVAL);
504b38ff370SJamie Gritton 
505b38ff370SJamie Gritton 	/*
506b38ff370SJamie Gritton 	 * Check all the parameters before committing to anything.  Not all
507b38ff370SJamie Gritton 	 * errors can be caught early, but we may as well try.  Also, this
508b38ff370SJamie Gritton 	 * takes care of some expensive stuff (path lookup) before getting
509b38ff370SJamie Gritton 	 * the allprison lock.
510b38ff370SJamie Gritton 	 *
511b38ff370SJamie Gritton 	 * XXX Jails are not filesystems, and jail parameters are not mount
512b38ff370SJamie Gritton 	 *     options.  But it makes more sense to re-use the vfsopt code
513b38ff370SJamie Gritton 	 *     than duplicate it under a different name.
514b38ff370SJamie Gritton 	 */
515b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
516b38ff370SJamie Gritton 	if (error)
517b38ff370SJamie Gritton 		return (error);
518b38ff370SJamie Gritton #ifdef INET
5190304c731SJamie Gritton 	ip4a = 0;
520b38ff370SJamie Gritton 	ip4 = NULL;
521b38ff370SJamie Gritton #endif
522b38ff370SJamie Gritton #ifdef INET6
5230304c731SJamie Gritton 	ip6a = 0;
524b38ff370SJamie Gritton 	ip6 = NULL;
525b38ff370SJamie Gritton #endif
526b38ff370SJamie Gritton 
5270304c731SJamie Gritton #if defined(INET) || defined(INET6)
5280304c731SJamie Gritton  again:
5290304c731SJamie Gritton #endif
530b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
531b38ff370SJamie Gritton 	if (error == ENOENT)
532b38ff370SJamie Gritton 		jid = 0;
533b38ff370SJamie Gritton 	else if (error != 0)
534b38ff370SJamie Gritton 		goto done_free;
535b38ff370SJamie Gritton 
536b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
537b38ff370SJamie Gritton 	if (error == ENOENT)
538b38ff370SJamie Gritton 		gotslevel = 0;
539b38ff370SJamie Gritton 	else if (error != 0)
540b38ff370SJamie Gritton 		goto done_free;
541b38ff370SJamie Gritton 	else
542b38ff370SJamie Gritton 		gotslevel = 1;
543b38ff370SJamie Gritton 
544b97457e2SJamie Gritton 	error =
545b97457e2SJamie Gritton 	    vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
546b97457e2SJamie Gritton 	if (error == ENOENT)
547b97457e2SJamie Gritton 		gotchildmax = 0;
548b97457e2SJamie Gritton 	else if (error != 0)
549b97457e2SJamie Gritton 		goto done_free;
550b97457e2SJamie Gritton 	else
551b97457e2SJamie Gritton 		gotchildmax = 1;
552b97457e2SJamie Gritton 
5530304c731SJamie Gritton 	error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
5540304c731SJamie Gritton 	gotenforce = (error == 0);
5550304c731SJamie Gritton 	if (gotenforce) {
5560304c731SJamie Gritton 		if (enforce < 0 || enforce > 2)
5570304c731SJamie Gritton 			return (EINVAL);
5580304c731SJamie Gritton 	} else if (error != ENOENT)
5590304c731SJamie Gritton 		goto done_free;
5600304c731SJamie Gritton 
561b38ff370SJamie Gritton 	pr_flags = ch_flags = 0;
5620304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
5630304c731SJamie Gritton 	    fi++) {
5640304c731SJamie Gritton 		if (pr_flag_names[fi] == NULL)
5650304c731SJamie Gritton 			continue;
5660304c731SJamie Gritton 		vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
5670304c731SJamie Gritton 		vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
5680304c731SJamie Gritton 	}
569b38ff370SJamie Gritton 	ch_flags |= pr_flags;
5707cbf7213SJamie Gritton 	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
5717cbf7213SJamie Gritton 	    fi++) {
5727cbf7213SJamie Gritton 		error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys,
5737cbf7213SJamie Gritton 		    sizeof(jsys));
5747cbf7213SJamie Gritton 		if (error == ENOENT)
5757cbf7213SJamie Gritton 			continue;
5767cbf7213SJamie Gritton 		if (error != 0)
5777cbf7213SJamie Gritton 			goto done_free;
5787cbf7213SJamie Gritton 		switch (jsys) {
5797cbf7213SJamie Gritton 		case JAIL_SYS_DISABLE:
5807cbf7213SJamie Gritton 			if (!pr_flag_jailsys[fi].disable) {
5817cbf7213SJamie Gritton 				error = EINVAL;
5827cbf7213SJamie Gritton 				goto done_free;
5837cbf7213SJamie Gritton 			}
5847cbf7213SJamie Gritton 			pr_flags |= pr_flag_jailsys[fi].disable;
5857cbf7213SJamie Gritton 			break;
5867cbf7213SJamie Gritton 		case JAIL_SYS_NEW:
5877cbf7213SJamie Gritton 			pr_flags |= pr_flag_jailsys[fi].new;
5887cbf7213SJamie Gritton 			break;
5897cbf7213SJamie Gritton 		case JAIL_SYS_INHERIT:
5907cbf7213SJamie Gritton 			break;
5917cbf7213SJamie Gritton 		default:
5927cbf7213SJamie Gritton 			error = EINVAL;
5937cbf7213SJamie Gritton 			goto done_free;
5947cbf7213SJamie Gritton 		}
5957cbf7213SJamie Gritton 		ch_flags |=
5967cbf7213SJamie Gritton 		    pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable;
5977cbf7213SJamie Gritton 	}
598b38ff370SJamie Gritton 	if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
599b38ff370SJamie Gritton 	    && !(pr_flags & PR_PERSIST)) {
600b38ff370SJamie Gritton 		error = EINVAL;
601b38ff370SJamie Gritton 		vfs_opterror(opts, "new jail must persist or attach");
602b38ff370SJamie Gritton 		goto done_errmsg;
603b38ff370SJamie Gritton 	}
604679e1390SJamie Gritton #ifdef VIMAGE
605679e1390SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
606679e1390SJamie Gritton 		error = EINVAL;
607679e1390SJamie Gritton 		vfs_opterror(opts, "vnet cannot be changed after creation");
608679e1390SJamie Gritton 		goto done_errmsg;
609679e1390SJamie Gritton 	}
610679e1390SJamie Gritton #endif
611b38ff370SJamie Gritton 
6120304c731SJamie Gritton 	pr_allow = ch_allow = 0;
6130304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
6140304c731SJamie Gritton 	    fi++) {
6150304c731SJamie Gritton 		vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
6160304c731SJamie Gritton 		vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
6170304c731SJamie Gritton 	}
6180304c731SJamie Gritton 	ch_allow |= pr_allow;
6190304c731SJamie Gritton 
620b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
621b38ff370SJamie Gritton 	if (error == ENOENT)
622b38ff370SJamie Gritton 		name = NULL;
623b38ff370SJamie Gritton 	else if (error != 0)
624b38ff370SJamie Gritton 		goto done_free;
625b38ff370SJamie Gritton 	else {
626b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
627b38ff370SJamie Gritton 			error = EINVAL;
628b38ff370SJamie Gritton 			goto done_free;
629b38ff370SJamie Gritton 		}
630b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
631b38ff370SJamie Gritton 			error = ENAMETOOLONG;
632b38ff370SJamie Gritton 			goto done_free;
633b38ff370SJamie Gritton 		}
634b38ff370SJamie Gritton 	}
635b38ff370SJamie Gritton 
636b38ff370SJamie Gritton 	error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
637b38ff370SJamie Gritton 	if (error == ENOENT)
638b38ff370SJamie Gritton 		host = NULL;
639b38ff370SJamie Gritton 	else if (error != 0)
640b38ff370SJamie Gritton 		goto done_free;
641b38ff370SJamie Gritton 	else {
64276ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
64376ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
644b38ff370SJamie Gritton 		if (len == 0 || host[len - 1] != '\0') {
645b38ff370SJamie Gritton 			error = EINVAL;
646b38ff370SJamie Gritton 			goto done_free;
647b38ff370SJamie Gritton 		}
648b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
649b38ff370SJamie Gritton 			error = ENAMETOOLONG;
650b38ff370SJamie Gritton 			goto done_free;
651b38ff370SJamie Gritton 		}
652b38ff370SJamie Gritton 	}
653b38ff370SJamie Gritton 
65476ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
65576ca6f88SJamie Gritton 	if (error == ENOENT)
65676ca6f88SJamie Gritton 		domain = NULL;
65776ca6f88SJamie Gritton 	else if (error != 0)
65876ca6f88SJamie Gritton 		goto done_free;
65976ca6f88SJamie Gritton 	else {
66076ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
66176ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
66276ca6f88SJamie Gritton 		if (len == 0 || domain[len - 1] != '\0') {
66376ca6f88SJamie Gritton 			error = EINVAL;
66476ca6f88SJamie Gritton 			goto done_free;
66576ca6f88SJamie Gritton 		}
66676ca6f88SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
66776ca6f88SJamie Gritton 			error = ENAMETOOLONG;
66876ca6f88SJamie Gritton 			goto done_free;
66976ca6f88SJamie Gritton 		}
67076ca6f88SJamie Gritton 	}
67176ca6f88SJamie Gritton 
67276ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
67376ca6f88SJamie Gritton 	if (error == ENOENT)
67476ca6f88SJamie Gritton 		uuid = NULL;
67576ca6f88SJamie Gritton 	else if (error != 0)
67676ca6f88SJamie Gritton 		goto done_free;
67776ca6f88SJamie Gritton 	else {
67876ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
67976ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
68076ca6f88SJamie Gritton 		if (len == 0 || uuid[len - 1] != '\0') {
68176ca6f88SJamie Gritton 			error = EINVAL;
68276ca6f88SJamie Gritton 			goto done_free;
68376ca6f88SJamie Gritton 		}
68476ca6f88SJamie Gritton 		if (len > HOSTUUIDLEN) {
68576ca6f88SJamie Gritton 			error = ENAMETOOLONG;
68676ca6f88SJamie Gritton 			goto done_free;
68776ca6f88SJamie Gritton 		}
68876ca6f88SJamie Gritton 	}
68976ca6f88SJamie Gritton 
69076ca6f88SJamie Gritton #ifdef COMPAT_IA32
69176ca6f88SJamie Gritton 	if (td->td_proc->p_sysent->sv_flags & SV_IA32) {
69276ca6f88SJamie Gritton 		uint32_t hid32;
69376ca6f88SJamie Gritton 
69476ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
69576ca6f88SJamie Gritton 		hid = hid32;
69676ca6f88SJamie Gritton 	} else
69776ca6f88SJamie Gritton #endif
69876ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
69976ca6f88SJamie Gritton 	if (error == ENOENT)
70076ca6f88SJamie Gritton 		gothid = 0;
70176ca6f88SJamie Gritton 	else if (error != 0)
70276ca6f88SJamie Gritton 		goto done_free;
70376ca6f88SJamie Gritton 	else {
70476ca6f88SJamie Gritton 		gothid = 1;
70576ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
70676ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
70776ca6f88SJamie Gritton 	}
70876ca6f88SJamie Gritton 
7090304c731SJamie Gritton 	/* This might be the second time around for this option. */
710b38ff370SJamie Gritton #ifdef INET
711b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
712b38ff370SJamie Gritton 	if (error == ENOENT)
7137cbf7213SJamie Gritton 		ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1;
714b38ff370SJamie Gritton 	else if (error != 0)
715b38ff370SJamie Gritton 		goto done_free;
716b38ff370SJamie Gritton 	else if (ip4s & (sizeof(*ip4) - 1)) {
717b38ff370SJamie Gritton 		error = EINVAL;
718b38ff370SJamie Gritton 		goto done_free;
7190304c731SJamie Gritton 	} else {
7207cbf7213SJamie Gritton 		ch_flags |= PR_IP4_USER | PR_IP4_DISABLE;
7217cbf7213SJamie Gritton 		if (ip4s == 0)
7227cbf7213SJamie Gritton 			pr_flags |= PR_IP4_USER | PR_IP4_DISABLE;
7237cbf7213SJamie Gritton 		else {
7247cbf7213SJamie Gritton 			pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER;
725b38ff370SJamie Gritton 			ip4s /= sizeof(*ip4);
726b38ff370SJamie Gritton 			if (ip4s > jail_max_af_ips) {
727b38ff370SJamie Gritton 				error = EINVAL;
728b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv4 addresses");
729b38ff370SJamie Gritton 				goto done_errmsg;
730b38ff370SJamie Gritton 			}
7310304c731SJamie Gritton 			if (ip4a < ip4s) {
7320304c731SJamie Gritton 				ip4a = ip4s;
7330304c731SJamie Gritton 				free(ip4, M_PRISON);
7340304c731SJamie Gritton 				ip4 = NULL;
7350304c731SJamie Gritton 			}
7360304c731SJamie Gritton 			if (ip4 == NULL)
7370304c731SJamie Gritton 				ip4 = malloc(ip4a * sizeof(*ip4), M_PRISON,
7380304c731SJamie Gritton 				    M_WAITOK);
739b38ff370SJamie Gritton 			bcopy(op, ip4, ip4s * sizeof(*ip4));
740b38ff370SJamie Gritton 			/*
7410304c731SJamie Gritton 			 * IP addresses are all sorted but ip[0] to preserve
7420304c731SJamie Gritton 			 * the primary IP address as given from userland.
7430304c731SJamie Gritton 			 * This special IP is used for unbound outgoing
7440304c731SJamie Gritton 			 * connections as well for "loopback" traffic.
745b38ff370SJamie Gritton 			 */
746b38ff370SJamie Gritton 			if (ip4s > 1)
747b38ff370SJamie Gritton 				qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4);
748b38ff370SJamie Gritton 			/*
7490304c731SJamie Gritton 			 * Check for duplicate addresses and do some simple
7500304c731SJamie Gritton 			 * zero and broadcast checks. If users give other bogus
7510304c731SJamie Gritton 			 * addresses it is their problem.
752b38ff370SJamie Gritton 			 *
7530304c731SJamie Gritton 			 * We do not have to care about byte order for these
7540304c731SJamie Gritton 			 * checks so we will do them in NBO.
755b38ff370SJamie Gritton 			 */
756b38ff370SJamie Gritton 			for (ii = 0; ii < ip4s; ii++) {
757b38ff370SJamie Gritton 				if (ip4[ii].s_addr == INADDR_ANY ||
758b38ff370SJamie Gritton 				    ip4[ii].s_addr == INADDR_BROADCAST) {
759b38ff370SJamie Gritton 					error = EINVAL;
760b38ff370SJamie Gritton 					goto done_free;
761b38ff370SJamie Gritton 				}
762b38ff370SJamie Gritton 				if ((ii+1) < ip4s &&
763b38ff370SJamie Gritton 				    (ip4[0].s_addr == ip4[ii+1].s_addr ||
764b38ff370SJamie Gritton 				     ip4[ii].s_addr == ip4[ii+1].s_addr)) {
765b38ff370SJamie Gritton 					error = EINVAL;
766b38ff370SJamie Gritton 					goto done_free;
767b38ff370SJamie Gritton 				}
768b38ff370SJamie Gritton 			}
769b38ff370SJamie Gritton 		}
7700304c731SJamie Gritton 	}
771b38ff370SJamie Gritton #endif
772b38ff370SJamie Gritton 
773b38ff370SJamie Gritton #ifdef INET6
774b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
775b38ff370SJamie Gritton 	if (error == ENOENT)
7767cbf7213SJamie Gritton 		ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1;
777b38ff370SJamie Gritton 	else if (error != 0)
778b38ff370SJamie Gritton 		goto done_free;
779b38ff370SJamie Gritton 	else if (ip6s & (sizeof(*ip6) - 1)) {
780b38ff370SJamie Gritton 		error = EINVAL;
781b38ff370SJamie Gritton 		goto done_free;
7820304c731SJamie Gritton 	} else {
7837cbf7213SJamie Gritton 		ch_flags |= PR_IP6_USER | PR_IP6_DISABLE;
7847cbf7213SJamie Gritton 		if (ip6s == 0)
7857cbf7213SJamie Gritton 			pr_flags |= PR_IP6_USER | PR_IP6_DISABLE;
7867cbf7213SJamie Gritton 		else {
7877cbf7213SJamie Gritton 			pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER;
788b38ff370SJamie Gritton 			ip6s /= sizeof(*ip6);
789b38ff370SJamie Gritton 			if (ip6s > jail_max_af_ips) {
790b38ff370SJamie Gritton 				error = EINVAL;
791b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv6 addresses");
792b38ff370SJamie Gritton 				goto done_errmsg;
793b38ff370SJamie Gritton 			}
7940304c731SJamie Gritton 			if (ip6a < ip6s) {
7950304c731SJamie Gritton 				ip6a = ip6s;
7960304c731SJamie Gritton 				free(ip6, M_PRISON);
7970304c731SJamie Gritton 				ip6 = NULL;
7980304c731SJamie Gritton 			}
7990304c731SJamie Gritton 			if (ip6 == NULL)
8000304c731SJamie Gritton 				ip6 = malloc(ip6a * sizeof(*ip6), M_PRISON,
8010304c731SJamie Gritton 				    M_WAITOK);
802b38ff370SJamie Gritton 			bcopy(op, ip6, ip6s * sizeof(*ip6));
803b38ff370SJamie Gritton 			if (ip6s > 1)
804b38ff370SJamie Gritton 				qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6);
805b38ff370SJamie Gritton 			for (ii = 0; ii < ip6s; ii++) {
8060304c731SJamie Gritton 				if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
807b38ff370SJamie Gritton 					error = EINVAL;
808b38ff370SJamie Gritton 					goto done_free;
809b38ff370SJamie Gritton 				}
810b38ff370SJamie Gritton 				if ((ii+1) < ip6s &&
811b38ff370SJamie Gritton 				    (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
812b38ff370SJamie Gritton 				     IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
813b38ff370SJamie Gritton 				{
814b38ff370SJamie Gritton 					error = EINVAL;
815b38ff370SJamie Gritton 					goto done_free;
816b38ff370SJamie Gritton 				}
817b38ff370SJamie Gritton 			}
818b38ff370SJamie Gritton 		}
8190304c731SJamie Gritton 	}
820b38ff370SJamie Gritton #endif
821b38ff370SJamie Gritton 
822b38ff370SJamie Gritton 	root = NULL;
823b38ff370SJamie Gritton 	error = vfs_getopt(opts, "path", (void **)&path, &len);
824b38ff370SJamie Gritton 	if (error == ENOENT)
825b38ff370SJamie Gritton 		path = NULL;
826b38ff370SJamie Gritton 	else if (error != 0)
827b38ff370SJamie Gritton 		goto done_free;
828b38ff370SJamie Gritton 	else {
829b38ff370SJamie Gritton 		if (flags & JAIL_UPDATE) {
830b38ff370SJamie Gritton 			error = EINVAL;
831b38ff370SJamie Gritton 			vfs_opterror(opts,
832b38ff370SJamie Gritton 			    "path cannot be changed after creation");
833b38ff370SJamie Gritton 			goto done_errmsg;
834b38ff370SJamie Gritton 		}
835b38ff370SJamie Gritton 		if (len == 0 || path[len - 1] != '\0') {
836b38ff370SJamie Gritton 			error = EINVAL;
837b38ff370SJamie Gritton 			goto done_free;
838b38ff370SJamie Gritton 		}
839b38ff370SJamie Gritton 		if (len < 2 || (len == 2 && path[0] == '/'))
840b38ff370SJamie Gritton 			path = NULL;
841b38ff370SJamie Gritton 		else {
8420304c731SJamie Gritton 			/* Leave room for a real-root full pathname. */
8430304c731SJamie Gritton 			if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
8440304c731SJamie Gritton 			    ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
8450304c731SJamie Gritton 				error = ENAMETOOLONG;
8460304c731SJamie Gritton 				goto done_free;
8470304c731SJamie Gritton 			}
848b38ff370SJamie Gritton 			NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE,
849b38ff370SJamie Gritton 			    path, td);
850b38ff370SJamie Gritton 			error = namei(&nd);
851b38ff370SJamie Gritton 			if (error)
852b38ff370SJamie Gritton 				goto done_free;
853b38ff370SJamie Gritton 			vfslocked = NDHASGIANT(&nd);
854b38ff370SJamie Gritton 			root = nd.ni_vp;
855b38ff370SJamie Gritton 			NDFREE(&nd, NDF_ONLY_PNBUF);
856b38ff370SJamie Gritton 			if (root->v_type != VDIR) {
857b38ff370SJamie Gritton 				error = ENOTDIR;
858b38ff370SJamie Gritton 				vrele(root);
859b38ff370SJamie Gritton 				VFS_UNLOCK_GIANT(vfslocked);
860b38ff370SJamie Gritton 				goto done_free;
861b38ff370SJamie Gritton 			}
862b38ff370SJamie Gritton 			VFS_UNLOCK_GIANT(vfslocked);
863b38ff370SJamie Gritton 		}
864b38ff370SJamie Gritton 	}
865b38ff370SJamie Gritton 
866b38ff370SJamie Gritton 	/*
867b38ff370SJamie Gritton 	 * Grab the allprison lock before letting modules check their
868b38ff370SJamie Gritton 	 * parameters.  Once we have it, do not let go so we'll have a
869b38ff370SJamie Gritton 	 * consistent view of the OSD list.
870b38ff370SJamie Gritton 	 */
871b38ff370SJamie Gritton 	sx_xlock(&allprison_lock);
872b38ff370SJamie Gritton 	error = osd_jail_call(NULL, PR_METHOD_CHECK, opts);
873b38ff370SJamie Gritton 	if (error)
874b38ff370SJamie Gritton 		goto done_unlock_list;
875b38ff370SJamie Gritton 
876b38ff370SJamie Gritton 	/* By now, all parameters should have been noted. */
877b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
878b38ff370SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
879b38ff370SJamie Gritton 			error = EINVAL;
880b38ff370SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
881b38ff370SJamie Gritton 			goto done_unlock_list;
882b38ff370SJamie Gritton 		}
883b38ff370SJamie Gritton 	}
884b38ff370SJamie Gritton 
885b38ff370SJamie Gritton 	/*
886b38ff370SJamie Gritton 	 * See if we are creating a new record or updating an existing one.
887b38ff370SJamie Gritton 	 * This abuses the file error codes ENOENT and EEXIST.
888b38ff370SJamie Gritton 	 */
889b38ff370SJamie Gritton 	cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
890b38ff370SJamie Gritton 	if (!cuflags) {
891b38ff370SJamie Gritton 		error = EINVAL;
892b38ff370SJamie Gritton 		vfs_opterror(opts, "no valid operation (create or update)");
893b38ff370SJamie Gritton 		goto done_unlock_list;
894b38ff370SJamie Gritton 	}
895b38ff370SJamie Gritton 	pr = NULL;
896b38ff370SJamie Gritton 	if (jid != 0) {
8970304c731SJamie Gritton 		/*
8980304c731SJamie Gritton 		 * See if a requested jid already exists.  There is an
8990304c731SJamie Gritton 		 * information leak here if the jid exists but is not within
9000304c731SJamie Gritton 		 * the caller's jail hierarchy.  Jail creators will get EEXIST
9010304c731SJamie Gritton 		 * even though they cannot see the jail, and CREATE | UPDATE
9020304c731SJamie Gritton 		 * will return ENOENT which is not normally a valid error.
9030304c731SJamie Gritton 		 */
904b38ff370SJamie Gritton 		if (jid < 0) {
905b38ff370SJamie Gritton 			error = EINVAL;
906b38ff370SJamie Gritton 			vfs_opterror(opts, "negative jid");
907b38ff370SJamie Gritton 			goto done_unlock_list;
908b38ff370SJamie Gritton 		}
909b38ff370SJamie Gritton 		pr = prison_find(jid);
910b38ff370SJamie Gritton 		if (pr != NULL) {
9110304c731SJamie Gritton 			ppr = pr->pr_parent;
912b38ff370SJamie Gritton 			/* Create: jid must not exist. */
913b38ff370SJamie Gritton 			if (cuflags == JAIL_CREATE) {
914b38ff370SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
915b38ff370SJamie Gritton 				error = EEXIST;
916b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d already exists",
917b38ff370SJamie Gritton 				    jid);
918b38ff370SJamie Gritton 				goto done_unlock_list;
919b38ff370SJamie Gritton 			}
9200304c731SJamie Gritton 			if (!prison_ischild(mypr, pr)) {
9210304c731SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
9220304c731SJamie Gritton 				pr = NULL;
9230304c731SJamie Gritton 			} else if (pr->pr_uref == 0) {
924b38ff370SJamie Gritton 				if (!(flags & JAIL_DYING)) {
925b38ff370SJamie Gritton 					mtx_unlock(&pr->pr_mtx);
926b38ff370SJamie Gritton 					error = ENOENT;
927b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
928b38ff370SJamie Gritton 					    jid);
929b38ff370SJamie Gritton 					goto done_unlock_list;
930b38ff370SJamie Gritton 				} else if ((flags & JAIL_ATTACH) ||
931b38ff370SJamie Gritton 				    (pr_flags & PR_PERSIST)) {
932b38ff370SJamie Gritton 					/*
933b38ff370SJamie Gritton 					 * A dying jail might be resurrected
934b38ff370SJamie Gritton 					 * (via attach or persist), but first
935b38ff370SJamie Gritton 					 * it must determine if another jail
936b38ff370SJamie Gritton 					 * has claimed its name.  Accomplish
937b38ff370SJamie Gritton 					 * this by implicitly re-setting the
938b38ff370SJamie Gritton 					 * name.
939b38ff370SJamie Gritton 					 */
940b38ff370SJamie Gritton 					if (name == NULL)
9410304c731SJamie Gritton 						name = prison_name(mypr, pr);
942b38ff370SJamie Gritton 				}
943b38ff370SJamie Gritton 			}
944b38ff370SJamie Gritton 		}
945b38ff370SJamie Gritton 		if (pr == NULL) {
946b38ff370SJamie Gritton 			/* Update: jid must exist. */
947b38ff370SJamie Gritton 			if (cuflags == JAIL_UPDATE) {
948b38ff370SJamie Gritton 				error = ENOENT;
949b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d not found", jid);
950b38ff370SJamie Gritton 				goto done_unlock_list;
951b38ff370SJamie Gritton 			}
952b38ff370SJamie Gritton 		}
953b38ff370SJamie Gritton 	}
954b38ff370SJamie Gritton 	/*
955b38ff370SJamie Gritton 	 * If the caller provided a name, look for a jail by that name.
956b38ff370SJamie Gritton 	 * This has different semantics for creates and updates keyed by jid
957b38ff370SJamie Gritton 	 * (where the name must not already exist in a different jail),
958b38ff370SJamie Gritton 	 * and updates keyed by the name itself (where the name must exist
959b38ff370SJamie Gritton 	 * because that is the jail being updated).
960b38ff370SJamie Gritton 	 */
961b38ff370SJamie Gritton 	if (name != NULL) {
9620304c731SJamie Gritton 		p = strrchr(name, '.');
9630304c731SJamie Gritton 		if (p != NULL) {
9640304c731SJamie Gritton 			/*
9650304c731SJamie Gritton 			 * This is a hierarchical name.  Split it into the
9660304c731SJamie Gritton 			 * parent and child names, and make sure the parent
9670304c731SJamie Gritton 			 * exists or matches an already found jail.
9680304c731SJamie Gritton 			 */
9690304c731SJamie Gritton 			*p = '\0';
9700304c731SJamie Gritton 			if (pr != NULL) {
9710304c731SJamie Gritton 				if (strncmp(name, ppr->pr_name, p - name) ||
9720304c731SJamie Gritton 				    ppr->pr_name[p - name] != '\0') {
9730304c731SJamie Gritton 					mtx_unlock(&pr->pr_mtx);
9740304c731SJamie Gritton 					error = EINVAL;
9750304c731SJamie Gritton 					vfs_opterror(opts,
9760304c731SJamie Gritton 					    "cannot change jail's parent");
9770304c731SJamie Gritton 					goto done_unlock_list;
9780304c731SJamie Gritton 				}
9790304c731SJamie Gritton 			} else {
9800304c731SJamie Gritton 				ppr = prison_find_name(mypr, name);
9810304c731SJamie Gritton 				if (ppr == NULL) {
9820304c731SJamie Gritton 					error = ENOENT;
9830304c731SJamie Gritton 					vfs_opterror(opts,
9840304c731SJamie Gritton 					    "jail \"%s\" not found", name);
9850304c731SJamie Gritton 					goto done_unlock_list;
9860304c731SJamie Gritton 				}
9870304c731SJamie Gritton 				mtx_unlock(&ppr->pr_mtx);
9880304c731SJamie Gritton 			}
9890304c731SJamie Gritton 			name = p + 1;
9900304c731SJamie Gritton 		}
991b38ff370SJamie Gritton 		if (name[0] != '\0') {
9920304c731SJamie Gritton 			namelen =
9930304c731SJamie Gritton 			    (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
994b38ff370SJamie Gritton  name_again:
9950304c731SJamie Gritton 			deadpr = NULL;
9960304c731SJamie Gritton 			FOREACH_PRISON_CHILD(ppr, tpr) {
997b38ff370SJamie Gritton 				if (tpr != pr && tpr->pr_ref > 0 &&
9980304c731SJamie Gritton 				    !strcmp(tpr->pr_name + namelen, name)) {
999b38ff370SJamie Gritton 					if (pr == NULL &&
1000b38ff370SJamie Gritton 					    cuflags != JAIL_CREATE) {
1001b38ff370SJamie Gritton 						mtx_lock(&tpr->pr_mtx);
1002b38ff370SJamie Gritton 						if (tpr->pr_ref > 0) {
1003b38ff370SJamie Gritton 							/*
1004b38ff370SJamie Gritton 							 * Use this jail
1005b38ff370SJamie Gritton 							 * for updates.
1006b38ff370SJamie Gritton 							 */
1007b38ff370SJamie Gritton 							if (tpr->pr_uref > 0) {
1008b38ff370SJamie Gritton 								pr = tpr;
1009b38ff370SJamie Gritton 								break;
1010b38ff370SJamie Gritton 							}
1011b38ff370SJamie Gritton 							deadpr = tpr;
1012b38ff370SJamie Gritton 						}
1013b38ff370SJamie Gritton 						mtx_unlock(&tpr->pr_mtx);
1014b38ff370SJamie Gritton 					} else if (tpr->pr_uref > 0) {
1015b38ff370SJamie Gritton 						/*
1016b38ff370SJamie Gritton 						 * Create, or update(jid):
1017b38ff370SJamie Gritton 						 * name must not exist in an
10180304c731SJamie Gritton 						 * active sibling jail.
1019b38ff370SJamie Gritton 						 */
1020b38ff370SJamie Gritton 						error = EEXIST;
1021b38ff370SJamie Gritton 						if (pr != NULL)
1022b38ff370SJamie Gritton 							mtx_unlock(&pr->pr_mtx);
1023b38ff370SJamie Gritton 						vfs_opterror(opts,
1024b38ff370SJamie Gritton 						   "jail \"%s\" already exists",
1025b38ff370SJamie Gritton 						   name);
1026b38ff370SJamie Gritton 						goto done_unlock_list;
1027b38ff370SJamie Gritton 					}
1028b38ff370SJamie Gritton 				}
1029b38ff370SJamie Gritton 			}
1030b38ff370SJamie Gritton 			/* If no active jail is found, use a dying one. */
1031b38ff370SJamie Gritton 			if (deadpr != NULL && pr == NULL) {
1032b38ff370SJamie Gritton 				if (flags & JAIL_DYING) {
1033b38ff370SJamie Gritton 					mtx_lock(&deadpr->pr_mtx);
1034b38ff370SJamie Gritton 					if (deadpr->pr_ref == 0) {
1035b38ff370SJamie Gritton 						mtx_unlock(&deadpr->pr_mtx);
1036b38ff370SJamie Gritton 						goto name_again;
1037b38ff370SJamie Gritton 					}
1038b38ff370SJamie Gritton 					pr = deadpr;
1039b38ff370SJamie Gritton 				} else if (cuflags == JAIL_UPDATE) {
1040b38ff370SJamie Gritton 					error = ENOENT;
1041b38ff370SJamie Gritton 					vfs_opterror(opts,
1042b38ff370SJamie Gritton 					    "jail \"%s\" is dying", name);
1043b38ff370SJamie Gritton 					goto done_unlock_list;
1044b38ff370SJamie Gritton 				}
1045b38ff370SJamie Gritton 			}
1046b38ff370SJamie Gritton 			/* Update: name must exist if no jid. */
1047b38ff370SJamie Gritton 			else if (cuflags == JAIL_UPDATE && pr == NULL) {
1048b38ff370SJamie Gritton 				error = ENOENT;
1049b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" not found",
1050b38ff370SJamie Gritton 				    name);
1051b38ff370SJamie Gritton 				goto done_unlock_list;
1052b38ff370SJamie Gritton 			}
1053b38ff370SJamie Gritton 		}
1054b38ff370SJamie Gritton 	}
1055b38ff370SJamie Gritton 	/* Update: must provide a jid or name. */
1056b38ff370SJamie Gritton 	else if (cuflags == JAIL_UPDATE && pr == NULL) {
1057b38ff370SJamie Gritton 		error = ENOENT;
1058b38ff370SJamie Gritton 		vfs_opterror(opts, "update specified no jail");
1059b38ff370SJamie Gritton 		goto done_unlock_list;
1060b38ff370SJamie Gritton 	}
1061b38ff370SJamie Gritton 
1062b38ff370SJamie Gritton 	/* If there's no prison to update, create a new one and link it in. */
1063b38ff370SJamie Gritton 	if (pr == NULL) {
1064b97457e2SJamie Gritton 		for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1065b97457e2SJamie Gritton 			if (tpr->pr_childcount >= tpr->pr_childmax) {
1066b97457e2SJamie Gritton 				error = EPERM;
1067b97457e2SJamie Gritton 				vfs_opterror(opts, "prison limit exceeded");
1068b97457e2SJamie Gritton 				goto done_unlock_list;
1069b97457e2SJamie Gritton 			}
1070b38ff370SJamie Gritton 		created = 1;
10710304c731SJamie Gritton 		mtx_lock(&ppr->pr_mtx);
10720304c731SJamie Gritton 		if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) {
10730304c731SJamie Gritton 			mtx_unlock(&ppr->pr_mtx);
10740304c731SJamie Gritton 			error = ENOENT;
10750304c731SJamie Gritton 			vfs_opterror(opts, "parent jail went away!");
10760304c731SJamie Gritton 			goto done_unlock_list;
10770304c731SJamie Gritton 		}
10780304c731SJamie Gritton 		ppr->pr_ref++;
10790304c731SJamie Gritton 		ppr->pr_uref++;
10800304c731SJamie Gritton 		mtx_unlock(&ppr->pr_mtx);
1081b38ff370SJamie Gritton 		pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1082b38ff370SJamie Gritton 		if (jid == 0) {
1083b38ff370SJamie Gritton 			/* Find the next free jid. */
1084b38ff370SJamie Gritton 			jid = lastprid + 1;
1085b38ff370SJamie Gritton  findnext:
1086b38ff370SJamie Gritton 			if (jid == JAIL_MAX)
1087b38ff370SJamie Gritton 				jid = 1;
1088b38ff370SJamie Gritton 			TAILQ_FOREACH(tpr, &allprison, pr_list) {
1089b38ff370SJamie Gritton 				if (tpr->pr_id < jid)
1090b38ff370SJamie Gritton 					continue;
1091b38ff370SJamie Gritton 				if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1092b38ff370SJamie Gritton 					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1093b38ff370SJamie Gritton 					break;
1094b38ff370SJamie Gritton 				}
1095b38ff370SJamie Gritton 				if (jid == lastprid) {
1096b38ff370SJamie Gritton 					error = EAGAIN;
1097b38ff370SJamie Gritton 					vfs_opterror(opts,
1098b38ff370SJamie Gritton 					    "no available jail IDs");
1099b38ff370SJamie Gritton 					free(pr, M_PRISON);
11000304c731SJamie Gritton 					prison_deref(ppr, PD_DEREF |
11010304c731SJamie Gritton 					    PD_DEUREF | PD_LIST_XLOCKED);
11020304c731SJamie Gritton 					goto done_releroot;
1103b38ff370SJamie Gritton 				}
1104b38ff370SJamie Gritton 				jid++;
1105b38ff370SJamie Gritton 				goto findnext;
1106b38ff370SJamie Gritton 			}
1107b38ff370SJamie Gritton 			lastprid = jid;
1108b38ff370SJamie Gritton 		} else {
1109b38ff370SJamie Gritton 			/*
1110b38ff370SJamie Gritton 			 * The jail already has a jid (that did not yet exist),
1111b38ff370SJamie Gritton 			 * so just find where to insert it.
1112b38ff370SJamie Gritton 			 */
1113b38ff370SJamie Gritton 			TAILQ_FOREACH(tpr, &allprison, pr_list)
1114b38ff370SJamie Gritton 				if (tpr->pr_id >= jid) {
1115b38ff370SJamie Gritton 					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1116b38ff370SJamie Gritton 					break;
1117b38ff370SJamie Gritton 				}
1118b38ff370SJamie Gritton 		}
1119b38ff370SJamie Gritton 		if (tpr == NULL)
1120b38ff370SJamie Gritton 			TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
11210304c731SJamie Gritton 		LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
11220304c731SJamie Gritton 		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1123b97457e2SJamie Gritton 			tpr->pr_childcount++;
1124b38ff370SJamie Gritton 
11250304c731SJamie Gritton 		pr->pr_parent = ppr;
1126b38ff370SJamie Gritton 		pr->pr_id = jid;
11270304c731SJamie Gritton 
11280304c731SJamie Gritton 		/* Set some default values, and inherit some from the parent. */
1129b38ff370SJamie Gritton 		if (name == NULL)
1130b38ff370SJamie Gritton 			name = "";
113176ca6f88SJamie Gritton 		if (host != NULL || domain != NULL || uuid != NULL || gothid) {
113276ca6f88SJamie Gritton 			if (host == NULL)
1133c1f19219SJamie Gritton 				host = ppr->pr_hostname;
113476ca6f88SJamie Gritton 			if (domain == NULL)
1135c1f19219SJamie Gritton 				domain = ppr->pr_domainname;
113676ca6f88SJamie Gritton 			if (uuid == NULL)
1137c1f19219SJamie Gritton 				uuid = ppr->pr_hostuuid;
113876ca6f88SJamie Gritton 			if (!gothid)
113976ca6f88SJamie Gritton 				hid = ppr->pr_hostid;
114076ca6f88SJamie Gritton 		}
1141b38ff370SJamie Gritton 		if (path == NULL) {
1142b38ff370SJamie Gritton 			path = "/";
11430304c731SJamie Gritton 			root = mypr->pr_root;
1144b38ff370SJamie Gritton 			vref(root);
1145b38ff370SJamie Gritton 		}
11460304c731SJamie Gritton #ifdef INET
11470304c731SJamie Gritton 		pr->pr_flags |= ppr->pr_flags & PR_IP4;
11480304c731SJamie Gritton 		pr->pr_ip4s = ppr->pr_ip4s;
11490304c731SJamie Gritton 		if (ppr->pr_ip4 != NULL) {
11500304c731SJamie Gritton 			pr->pr_ip4 = malloc(pr->pr_ip4s *
11510304c731SJamie Gritton 			    sizeof(struct in_addr), M_PRISON, M_WAITOK);
11520304c731SJamie Gritton 			bcopy(ppr->pr_ip4, pr->pr_ip4,
11530304c731SJamie Gritton 			    pr->pr_ip4s * sizeof(*pr->pr_ip4));
11540304c731SJamie Gritton 		}
11550304c731SJamie Gritton #endif
11560304c731SJamie Gritton #ifdef INET6
11570304c731SJamie Gritton 		pr->pr_flags |= ppr->pr_flags & PR_IP6;
11580304c731SJamie Gritton 		pr->pr_ip6s = ppr->pr_ip6s;
11590304c731SJamie Gritton 		if (ppr->pr_ip6 != NULL) {
11600304c731SJamie Gritton 			pr->pr_ip6 = malloc(pr->pr_ip6s *
11610304c731SJamie Gritton 			    sizeof(struct in6_addr), M_PRISON, M_WAITOK);
11620304c731SJamie Gritton 			bcopy(ppr->pr_ip6, pr->pr_ip6,
11630304c731SJamie Gritton 			    pr->pr_ip6s * sizeof(*pr->pr_ip6));
11640304c731SJamie Gritton 		}
11650304c731SJamie Gritton #endif
11660304c731SJamie Gritton 		pr->pr_securelevel = ppr->pr_securelevel;
11670304c731SJamie Gritton 		pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
11680304c731SJamie Gritton 		pr->pr_enforce_statfs = ppr->pr_enforce_statfs;
1169b38ff370SJamie Gritton 
11700304c731SJamie Gritton 		LIST_INIT(&pr->pr_children);
11710304c731SJamie Gritton 		mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1172b38ff370SJamie Gritton 
1173679e1390SJamie Gritton #ifdef VIMAGE
1174679e1390SJamie Gritton 		/* Allocate a new vnet if specified. */
1175679e1390SJamie Gritton 		pr->pr_vnet = (pr_flags & PR_VNET)
1176679e1390SJamie Gritton 		    ? vnet_alloc() : ppr->pr_vnet;
1177679e1390SJamie Gritton #endif
1178b38ff370SJamie Gritton 		/*
1179b38ff370SJamie Gritton 		 * Allocate a dedicated cpuset for each jail.
1180b38ff370SJamie Gritton 		 * Unlike other initial settings, this may return an erorr.
1181b38ff370SJamie Gritton 		 */
11820304c731SJamie Gritton 		error = cpuset_create_root(ppr, &pr->pr_cpuset);
1183b38ff370SJamie Gritton 		if (error) {
1184b38ff370SJamie Gritton 			prison_deref(pr, PD_LIST_XLOCKED);
1185b38ff370SJamie Gritton 			goto done_releroot;
1186b38ff370SJamie Gritton 		}
1187b38ff370SJamie Gritton 
1188b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
1189b38ff370SJamie Gritton 		/*
1190b38ff370SJamie Gritton 		 * New prisons do not yet have a reference, because we do not
1191b38ff370SJamie Gritton 		 * want other to see the incomplete prison once the
1192b38ff370SJamie Gritton 		 * allprison_lock is downgraded.
1193b38ff370SJamie Gritton 		 */
1194b38ff370SJamie Gritton 	} else {
1195b38ff370SJamie Gritton 		created = 0;
1196b38ff370SJamie Gritton 		/*
1197b38ff370SJamie Gritton 		 * Grab a reference for existing prisons, to ensure they
1198b38ff370SJamie Gritton 		 * continue to exist for the duration of the call.
1199b38ff370SJamie Gritton 		 */
1200b38ff370SJamie Gritton 		pr->pr_ref++;
1201b38ff370SJamie Gritton 	}
1202b38ff370SJamie Gritton 
1203b38ff370SJamie Gritton 	/* Do final error checking before setting anything. */
12040304c731SJamie Gritton 	if (gotslevel) {
12050304c731SJamie Gritton 		if (slevel < ppr->pr_securelevel) {
12060304c731SJamie Gritton 			error = EPERM;
12070304c731SJamie Gritton 			goto done_deref_locked;
12080304c731SJamie Gritton 		}
12090304c731SJamie Gritton 	}
1210b97457e2SJamie Gritton 	if (gotchildmax) {
1211b97457e2SJamie Gritton 		if (childmax >= ppr->pr_childmax) {
1212b97457e2SJamie Gritton 			error = EPERM;
1213b97457e2SJamie Gritton 			goto done_deref_locked;
1214b97457e2SJamie Gritton 		}
1215b97457e2SJamie Gritton 	}
12160304c731SJamie Gritton 	if (gotenforce) {
12170304c731SJamie Gritton 		if (enforce < ppr->pr_enforce_statfs) {
12180304c731SJamie Gritton 			error = EPERM;
12190304c731SJamie Gritton 			goto done_deref_locked;
12200304c731SJamie Gritton 		}
12210304c731SJamie Gritton 	}
1222b38ff370SJamie Gritton #ifdef INET
12230304c731SJamie Gritton 	if (ch_flags & PR_IP4_USER) {
12240304c731SJamie Gritton 		if (ppr->pr_flags & PR_IP4) {
12250304c731SJamie Gritton 			if (!(pr_flags & PR_IP4_USER)) {
1226b38ff370SJamie Gritton 				/*
12270304c731SJamie Gritton 				 * Silently ignore attempts to make the IP
12280304c731SJamie Gritton 				 * addresses unrestricted when the parent is
12290304c731SJamie Gritton 				 * restricted; in other words, interpret
12300304c731SJamie Gritton 				 * "unrestricted" as "as unrestricted as
12310304c731SJamie Gritton 				 * possible".
1232b38ff370SJamie Gritton 				 */
12330304c731SJamie Gritton 				ip4s = ppr->pr_ip4s;
12340304c731SJamie Gritton 				if (ip4s == 0) {
12350304c731SJamie Gritton 					free(ip4, M_PRISON);
12360304c731SJamie Gritton 					ip4 = NULL;
12370304c731SJamie Gritton 				} else if (ip4s <= ip4a) {
12380304c731SJamie Gritton 					/* Inherit the parent's address(es). */
12390304c731SJamie Gritton 					bcopy(ppr->pr_ip4, ip4,
12400304c731SJamie Gritton 					    ip4s * sizeof(*ip4));
12410304c731SJamie Gritton 				} else {
12420304c731SJamie Gritton 					/*
12430304c731SJamie Gritton 					 * There's no room for the parent's
12440304c731SJamie Gritton 					 * address list.  Allocate some more.
12450304c731SJamie Gritton 					 */
12460304c731SJamie Gritton 					ip4a = ip4s;
12470304c731SJamie Gritton 					free(ip4, M_PRISON);
12480304c731SJamie Gritton 					ip4 = malloc(ip4a * sizeof(*ip4),
12490304c731SJamie Gritton 					    M_PRISON, M_NOWAIT);
12500304c731SJamie Gritton 					if (ip4 != NULL)
12510304c731SJamie Gritton 						bcopy(ppr->pr_ip4, ip4,
12520304c731SJamie Gritton 						    ip4s * sizeof(*ip4));
12530304c731SJamie Gritton 					else {
12540304c731SJamie Gritton 						/* Allocation failed without
12550304c731SJamie Gritton 						 * sleeping.  Unlocking the
12560304c731SJamie Gritton 						 * prison now will invalidate
12570304c731SJamie Gritton 						 * some checks and prematurely
12580304c731SJamie Gritton 						 * show an unfinished new jail.
12590304c731SJamie Gritton 						 * So let go of everything and
12600304c731SJamie Gritton 						 * start over.
12610304c731SJamie Gritton 						 */
12620304c731SJamie Gritton 						prison_deref(pr, created
12630304c731SJamie Gritton 						    ? PD_LOCKED |
12640304c731SJamie Gritton 						      PD_LIST_XLOCKED
12650304c731SJamie Gritton 						    : PD_DEREF | PD_LOCKED |
12660304c731SJamie Gritton 						      PD_LIST_XLOCKED);
12670304c731SJamie Gritton 						if (root != NULL) {
12680304c731SJamie Gritton 							vfslocked =
12690304c731SJamie Gritton 							    VFS_LOCK_GIANT(
12700304c731SJamie Gritton 							    root->v_mount);
12710304c731SJamie Gritton 							vrele(root);
12720304c731SJamie Gritton 							VFS_UNLOCK_GIANT(
12730304c731SJamie Gritton 							    vfslocked);
12740304c731SJamie Gritton 						}
12750304c731SJamie Gritton 						ip4 = malloc(ip4a *
12760304c731SJamie Gritton 						    sizeof(*ip4), M_PRISON,
12770304c731SJamie Gritton 						    M_WAITOK);
12780304c731SJamie Gritton 						goto again;
12790304c731SJamie Gritton 					}
12800304c731SJamie Gritton 				}
12810304c731SJamie Gritton 			} else if (ip4s > 0) {
12820304c731SJamie Gritton 				/*
12830304c731SJamie Gritton 				 * Make sure the new set of IP addresses is a
12840304c731SJamie Gritton 				 * subset of the parent's list.  Don't worry
12850304c731SJamie Gritton 				 * about the parent being unlocked, as any
12860304c731SJamie Gritton 				 * setting is done with allprison_lock held.
12870304c731SJamie Gritton 				 */
12880304c731SJamie Gritton 				for (ij = 0; ij < ppr->pr_ip4s; ij++)
12890304c731SJamie Gritton 					if (ip4[0].s_addr ==
12900304c731SJamie Gritton 					    ppr->pr_ip4[ij].s_addr)
12910304c731SJamie Gritton 						break;
12920304c731SJamie Gritton 				if (ij == ppr->pr_ip4s) {
12930304c731SJamie Gritton 					error = EPERM;
12940304c731SJamie Gritton 					goto done_deref_locked;
12950304c731SJamie Gritton 				}
12960304c731SJamie Gritton 				if (ip4s > 1) {
12970304c731SJamie Gritton 					for (ii = ij = 1; ii < ip4s; ii++) {
12980304c731SJamie Gritton 						if (ip4[ii].s_addr ==
12990304c731SJamie Gritton 						    ppr->pr_ip4[0].s_addr)
1300b38ff370SJamie Gritton 							continue;
13010304c731SJamie Gritton 						for (; ij < ppr->pr_ip4s; ij++)
13020304c731SJamie Gritton 						    if (ip4[ii].s_addr ==
13030304c731SJamie Gritton 							ppr->pr_ip4[ij].s_addr)
13040304c731SJamie Gritton 							    break;
13050304c731SJamie Gritton 						if (ij == ppr->pr_ip4s)
13060304c731SJamie Gritton 							break;
13070304c731SJamie Gritton 					}
13080304c731SJamie Gritton 					if (ij == ppr->pr_ip4s) {
13090304c731SJamie Gritton 						error = EPERM;
13100304c731SJamie Gritton 						goto done_deref_locked;
13110304c731SJamie Gritton 					}
13120304c731SJamie Gritton 				}
13130304c731SJamie Gritton 			}
13140304c731SJamie Gritton 		}
13150304c731SJamie Gritton 		if (ip4s > 0) {
13160304c731SJamie Gritton 			/*
13170304c731SJamie Gritton 			 * Check for conflicting IP addresses.  We permit them
13180304c731SJamie Gritton 			 * if there is no more than one IP on each jail.  If
13190304c731SJamie Gritton 			 * there is a duplicate on a jail with more than one
13200304c731SJamie Gritton 			 * IP stop checking and return error.
13210304c731SJamie Gritton 			 */
13220304c731SJamie Gritton 			FOREACH_PRISON_DESCENDANT(&prison0, tpr, descend) {
13230304c731SJamie Gritton 				if (tpr == pr || tpr->pr_uref == 0) {
13240304c731SJamie Gritton 					descend = 0;
13250304c731SJamie Gritton 					continue;
13260304c731SJamie Gritton 				}
13270304c731SJamie Gritton 				if (!(tpr->pr_flags & PR_IP4_USER))
13280304c731SJamie Gritton 					continue;
13290304c731SJamie Gritton 				descend = 0;
13300304c731SJamie Gritton 				if (tpr->pr_ip4 == NULL ||
13310304c731SJamie Gritton 				    (ip4s == 1 && tpr->pr_ip4s == 1))
13320304c731SJamie Gritton 					continue;
13330304c731SJamie Gritton 				for (ii = 0; ii < ip4s; ii++) {
1334b38ff370SJamie Gritton 					if (_prison_check_ip4(tpr,
1335b38ff370SJamie Gritton 					    &ip4[ii]) == 0) {
13360304c731SJamie Gritton 						error = EADDRINUSE;
1337b38ff370SJamie Gritton 						vfs_opterror(opts,
1338b38ff370SJamie Gritton 						    "IPv4 addresses clash");
1339b38ff370SJamie Gritton 						goto done_deref_locked;
1340b38ff370SJamie Gritton 					}
13410304c731SJamie Gritton 				}
13420304c731SJamie Gritton 			}
13430304c731SJamie Gritton 		}
13440304c731SJamie Gritton 	}
1345b38ff370SJamie Gritton #endif
1346b38ff370SJamie Gritton #ifdef INET6
13470304c731SJamie Gritton 	if (ch_flags & PR_IP6_USER) {
13480304c731SJamie Gritton 		if (ppr->pr_flags & PR_IP6) {
13490304c731SJamie Gritton 			if (!(pr_flags & PR_IP6_USER)) {
13500304c731SJamie Gritton 				/*
13510304c731SJamie Gritton 				 * Silently ignore attempts to make the IP
13520304c731SJamie Gritton 				 * addresses unrestricted when the parent is
13530304c731SJamie Gritton 				 * restricted.
13540304c731SJamie Gritton 				 */
13550304c731SJamie Gritton 				ip6s = ppr->pr_ip6s;
13560304c731SJamie Gritton 				if (ip6s == 0) {
13570304c731SJamie Gritton 					free(ip6, M_PRISON);
13580304c731SJamie Gritton 					ip6 = NULL;
13590304c731SJamie Gritton 				} else if (ip6s <= ip6a) {
13600304c731SJamie Gritton 					/* Inherit the parent's address(es). */
13610304c731SJamie Gritton 					bcopy(ppr->pr_ip6, ip6,
13620304c731SJamie Gritton 					    ip6s * sizeof(*ip6));
13630304c731SJamie Gritton 				} else {
13640304c731SJamie Gritton 					/*
13650304c731SJamie Gritton 					 * There's no room for the parent's
13660304c731SJamie Gritton 					 * address list.
13670304c731SJamie Gritton 					 */
13680304c731SJamie Gritton 					ip6a = ip6s;
13690304c731SJamie Gritton 					free(ip6, M_PRISON);
13700304c731SJamie Gritton 					ip6 = malloc(ip6a * sizeof(*ip6),
13710304c731SJamie Gritton 					    M_PRISON, M_NOWAIT);
13720304c731SJamie Gritton 					if (ip6 != NULL)
13730304c731SJamie Gritton 						bcopy(ppr->pr_ip6, ip6,
13740304c731SJamie Gritton 						    ip6s * sizeof(*ip6));
13750304c731SJamie Gritton 					else {
13760304c731SJamie Gritton 						prison_deref(pr, created
13770304c731SJamie Gritton 						    ? PD_LOCKED |
13780304c731SJamie Gritton 						      PD_LIST_XLOCKED
13790304c731SJamie Gritton 						    : PD_DEREF | PD_LOCKED |
13800304c731SJamie Gritton 						      PD_LIST_XLOCKED);
13810304c731SJamie Gritton 						if (root != NULL) {
13820304c731SJamie Gritton 							vfslocked =
13830304c731SJamie Gritton 							    VFS_LOCK_GIANT(
13840304c731SJamie Gritton 							    root->v_mount);
13850304c731SJamie Gritton 							vrele(root);
13860304c731SJamie Gritton 							VFS_UNLOCK_GIANT(
13870304c731SJamie Gritton 							    vfslocked);
13880304c731SJamie Gritton 						}
13890304c731SJamie Gritton 						ip6 = malloc(ip6a *
13900304c731SJamie Gritton 						    sizeof(*ip6), M_PRISON,
13910304c731SJamie Gritton 						    M_WAITOK);
13920304c731SJamie Gritton 						goto again;
13930304c731SJamie Gritton 					}
13940304c731SJamie Gritton 				}
13950304c731SJamie Gritton 			} else if (ip6s > 0) {
13960304c731SJamie Gritton 				/*
13970304c731SJamie Gritton 				 * Make sure the new set of IP addresses is a
13980304c731SJamie Gritton 				 * subset of the parent's list.
13990304c731SJamie Gritton 				 */
14000304c731SJamie Gritton 				for (ij = 0; ij < ppr->pr_ip6s; ij++)
14010304c731SJamie Gritton 					if (IN6_ARE_ADDR_EQUAL(&ip6[0],
14020304c731SJamie Gritton 					    &ppr->pr_ip6[ij]))
14030304c731SJamie Gritton 						break;
14040304c731SJamie Gritton 				if (ij == ppr->pr_ip6s) {
14050304c731SJamie Gritton 					error = EPERM;
14060304c731SJamie Gritton 					goto done_deref_locked;
14070304c731SJamie Gritton 				}
14080304c731SJamie Gritton 				if (ip6s > 1) {
14090304c731SJamie Gritton 					for (ii = ij = 1; ii < ip6s; ii++) {
14100304c731SJamie Gritton 						if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
14110304c731SJamie Gritton 						    &ppr->pr_ip6[0]))
14120304c731SJamie Gritton 							continue;
14130304c731SJamie Gritton 						for (; ij < ppr->pr_ip6s; ij++)
14140304c731SJamie Gritton 							if (IN6_ARE_ADDR_EQUAL(
14150304c731SJamie Gritton 							    &ip6[ii],
14160304c731SJamie Gritton 							    &ppr->pr_ip6[ij]))
14170304c731SJamie Gritton 								break;
14180304c731SJamie Gritton 						if (ij == ppr->pr_ip6s)
14190304c731SJamie Gritton 							break;
14200304c731SJamie Gritton 					}
14210304c731SJamie Gritton 					if (ij == ppr->pr_ip6s) {
14220304c731SJamie Gritton 						error = EPERM;
14230304c731SJamie Gritton 						goto done_deref_locked;
14240304c731SJamie Gritton 					}
14250304c731SJamie Gritton 				}
14260304c731SJamie Gritton 			}
14270304c731SJamie Gritton 		}
14280304c731SJamie Gritton 		if (ip6s > 0) {
14290304c731SJamie Gritton 			/* Check for conflicting IP addresses. */
14300304c731SJamie Gritton 			FOREACH_PRISON_DESCENDANT(&prison0, tpr, descend) {
14310304c731SJamie Gritton 				if (tpr == pr || tpr->pr_uref == 0) {
14320304c731SJamie Gritton 					descend = 0;
14330304c731SJamie Gritton 					continue;
14340304c731SJamie Gritton 				}
14350304c731SJamie Gritton 				if (!(tpr->pr_flags & PR_IP6_USER))
14360304c731SJamie Gritton 					continue;
14370304c731SJamie Gritton 				descend = 0;
14380304c731SJamie Gritton 				if (tpr->pr_ip6 == NULL ||
14390304c731SJamie Gritton 				    (ip6s == 1 && tpr->pr_ip6s == 1))
14400304c731SJamie Gritton 					continue;
14410304c731SJamie Gritton 				for (ii = 0; ii < ip6s; ii++) {
1442b38ff370SJamie Gritton 					if (_prison_check_ip6(tpr,
1443b38ff370SJamie Gritton 					    &ip6[ii]) == 0) {
14440304c731SJamie Gritton 						error = EADDRINUSE;
1445b38ff370SJamie Gritton 						vfs_opterror(opts,
1446b38ff370SJamie Gritton 						    "IPv6 addresses clash");
1447b38ff370SJamie Gritton 						goto done_deref_locked;
1448b38ff370SJamie Gritton 					}
14490304c731SJamie Gritton 				}
14500304c731SJamie Gritton 			}
14510304c731SJamie Gritton 		}
1452b38ff370SJamie Gritton 	}
1453b38ff370SJamie Gritton #endif
14540304c731SJamie Gritton 	onamelen = namelen = 0;
14550304c731SJamie Gritton 	if (name != NULL) {
1456b38ff370SJamie Gritton 		/* Give a default name of the jid. */
1457b38ff370SJamie Gritton 		if (name[0] == '\0')
1458b38ff370SJamie Gritton 			snprintf(name = numbuf, sizeof(numbuf), "%d", jid);
1459b38ff370SJamie Gritton 		else if (strtoul(name, &p, 10) != jid && *p == '\0') {
1460b38ff370SJamie Gritton 			error = EINVAL;
1461b38ff370SJamie Gritton 			vfs_opterror(opts, "name cannot be numeric");
14620304c731SJamie Gritton 			goto done_deref_locked;
1463b38ff370SJamie Gritton 		}
1464b38ff370SJamie Gritton 		/*
14650304c731SJamie Gritton 		 * Make sure the name isn't too long for the prison or its
14660304c731SJamie Gritton 		 * children.
1467b38ff370SJamie Gritton 		 */
14680304c731SJamie Gritton 		onamelen = strlen(pr->pr_name);
14690304c731SJamie Gritton 		namelen = strlen(name);
14700304c731SJamie Gritton 		if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) {
14710304c731SJamie Gritton 			error = ENAMETOOLONG;
14720304c731SJamie Gritton 			goto done_deref_locked;
14730304c731SJamie Gritton 		}
14740304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
14750304c731SJamie Gritton 			if (strlen(tpr->pr_name) + (namelen - onamelen) >=
14760304c731SJamie Gritton 			    sizeof(pr->pr_name)) {
14770304c731SJamie Gritton 				error = ENAMETOOLONG;
14780304c731SJamie Gritton 				goto done_deref_locked;
14790304c731SJamie Gritton 			}
14800304c731SJamie Gritton 		}
14810304c731SJamie Gritton 	}
14820304c731SJamie Gritton 	if (pr_allow & ~ppr->pr_allow) {
14830304c731SJamie Gritton 		error = EPERM;
14840304c731SJamie Gritton 		goto done_deref_locked;
1485b38ff370SJamie Gritton 	}
1486b38ff370SJamie Gritton 
1487b38ff370SJamie Gritton 	/* Set the parameters of the prison. */
1488b38ff370SJamie Gritton #ifdef INET
14890304c731SJamie Gritton 	redo_ip4 = 0;
14900304c731SJamie Gritton 	if (ch_flags & PR_IP4_USER) {
14910304c731SJamie Gritton 		if (pr_flags & PR_IP4_USER) {
14920304c731SJamie Gritton 			/* Some restriction set. */
14930304c731SJamie Gritton 			pr->pr_flags |= PR_IP4;
1494b38ff370SJamie Gritton 			if (ip4s >= 0) {
1495b38ff370SJamie Gritton 				free(pr->pr_ip4, M_PRISON);
14960304c731SJamie Gritton 				pr->pr_ip4s = ip4s;
1497b38ff370SJamie Gritton 				pr->pr_ip4 = ip4;
1498b38ff370SJamie Gritton 				ip4 = NULL;
1499b38ff370SJamie Gritton 			}
15000304c731SJamie Gritton 		} else if (ppr->pr_flags & PR_IP4) {
15010304c731SJamie Gritton 			/* This restriction cleared, but keep inherited. */
15020304c731SJamie Gritton 			free(pr->pr_ip4, M_PRISON);
15030304c731SJamie Gritton 			pr->pr_ip4s = ip4s;
15040304c731SJamie Gritton 			pr->pr_ip4 = ip4;
15050304c731SJamie Gritton 			ip4 = NULL;
15060304c731SJamie Gritton 		} else {
15070304c731SJamie Gritton 			/* Restriction cleared, now unrestricted. */
15080304c731SJamie Gritton 			pr->pr_flags &= ~PR_IP4;
15090304c731SJamie Gritton 			free(pr->pr_ip4, M_PRISON);
15100304c731SJamie Gritton 			pr->pr_ip4s = 0;
15110304c731SJamie Gritton 		}
15120304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
15130304c731SJamie Gritton 			if (prison_restrict_ip4(tpr, NULL)) {
15140304c731SJamie Gritton 				redo_ip4 = 1;
15150304c731SJamie Gritton 				descend = 0;
15160304c731SJamie Gritton 			}
15170304c731SJamie Gritton 		}
15180304c731SJamie Gritton 	}
1519b38ff370SJamie Gritton #endif
1520b38ff370SJamie Gritton #ifdef INET6
15210304c731SJamie Gritton 	redo_ip6 = 0;
15220304c731SJamie Gritton 	if (ch_flags & PR_IP6_USER) {
15230304c731SJamie Gritton 		if (pr_flags & PR_IP6_USER) {
15240304c731SJamie Gritton 			/* Some restriction set. */
15250304c731SJamie Gritton 			pr->pr_flags |= PR_IP6;
1526b38ff370SJamie Gritton 			if (ip6s >= 0) {
1527b38ff370SJamie Gritton 				free(pr->pr_ip6, M_PRISON);
15280304c731SJamie Gritton 				pr->pr_ip6s = ip6s;
1529b38ff370SJamie Gritton 				pr->pr_ip6 = ip6;
1530b38ff370SJamie Gritton 				ip6 = NULL;
1531b38ff370SJamie Gritton 			}
15320304c731SJamie Gritton 		} else if (ppr->pr_flags & PR_IP6) {
15330304c731SJamie Gritton 			/* This restriction cleared, but keep inherited. */
15340304c731SJamie Gritton 			free(pr->pr_ip6, M_PRISON);
15350304c731SJamie Gritton 			pr->pr_ip6s = ip6s;
15360304c731SJamie Gritton 			pr->pr_ip6 = ip6;
15370304c731SJamie Gritton 			ip6 = NULL;
15380304c731SJamie Gritton 		} else {
15390304c731SJamie Gritton 			/* Restriction cleared, now unrestricted. */
15400304c731SJamie Gritton 			pr->pr_flags &= ~PR_IP6;
15410304c731SJamie Gritton 			free(pr->pr_ip6, M_PRISON);
15420304c731SJamie Gritton 			pr->pr_ip6s = 0;
15430304c731SJamie Gritton 		}
15440304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
15450304c731SJamie Gritton 			if (prison_restrict_ip6(tpr, NULL)) {
15460304c731SJamie Gritton 				redo_ip6 = 1;
15470304c731SJamie Gritton 				descend = 0;
15480304c731SJamie Gritton 			}
15490304c731SJamie Gritton 		}
15500304c731SJamie Gritton 	}
1551b38ff370SJamie Gritton #endif
15520304c731SJamie Gritton 	if (gotslevel) {
1553b38ff370SJamie Gritton 		pr->pr_securelevel = slevel;
15540304c731SJamie Gritton 		/* Set all child jails to be at least this level. */
15550304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
15560304c731SJamie Gritton 			if (tpr->pr_securelevel < slevel)
15570304c731SJamie Gritton 				tpr->pr_securelevel = slevel;
15580304c731SJamie Gritton 	}
1559b97457e2SJamie Gritton 	if (gotchildmax) {
1560b97457e2SJamie Gritton 		pr->pr_childmax = childmax;
1561b97457e2SJamie Gritton 		/* Set all child jails to under this limit. */
1562b97457e2SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1563b97457e2SJamie Gritton 			if (tpr->pr_childmax > childmax - level)
1564b97457e2SJamie Gritton 				tpr->pr_childmax = childmax > level
1565b97457e2SJamie Gritton 				    ? childmax - level : 0;
1566b97457e2SJamie Gritton 	}
15670304c731SJamie Gritton 	if (gotenforce) {
15680304c731SJamie Gritton 		pr->pr_enforce_statfs = enforce;
15690304c731SJamie Gritton 		/* Pass this restriction on to the children. */
15700304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
15710304c731SJamie Gritton 			if (tpr->pr_enforce_statfs < enforce)
15720304c731SJamie Gritton 				tpr->pr_enforce_statfs = enforce;
15730304c731SJamie Gritton 	}
15740304c731SJamie Gritton 	if (name != NULL) {
15750304c731SJamie Gritton 		if (ppr == &prison0)
1576b38ff370SJamie Gritton 			strlcpy(pr->pr_name, name, sizeof(pr->pr_name));
15770304c731SJamie Gritton 		else
15780304c731SJamie Gritton 			snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
15790304c731SJamie Gritton 			    ppr->pr_name, name);
15800304c731SJamie Gritton 		/* Change this component of child names. */
15810304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
15820304c731SJamie Gritton 			bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
15830304c731SJamie Gritton 			    strlen(tpr->pr_name + onamelen) + 1);
15840304c731SJamie Gritton 			bcopy(pr->pr_name, tpr->pr_name, namelen);
15850304c731SJamie Gritton 		}
15860304c731SJamie Gritton 	}
1587b38ff370SJamie Gritton 	if (path != NULL) {
15880304c731SJamie Gritton 		/* Try to keep a real-rooted full pathname. */
15890304c731SJamie Gritton 		if (path[0] == '/' && strcmp(mypr->pr_path, "/"))
15900304c731SJamie Gritton 			snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
15910304c731SJamie Gritton 			    mypr->pr_path, path);
15920304c731SJamie Gritton 		else
1593b38ff370SJamie Gritton 			strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1594b38ff370SJamie Gritton 		pr->pr_root = root;
1595b38ff370SJamie Gritton 	}
159676ca6f88SJamie Gritton 	if (PR_HOST & ch_flags & ~pr_flags) {
159776ca6f88SJamie Gritton 		if (pr->pr_flags & PR_HOST) {
159876ca6f88SJamie Gritton 			/*
159976ca6f88SJamie Gritton 			 * Copy the parent's host info.  As with pr_ip4 above,
160076ca6f88SJamie Gritton 			 * the lack of a lock on the parent is not a problem;
160176ca6f88SJamie Gritton 			 * it is always set with allprison_lock at least
160276ca6f88SJamie Gritton 			 * shared, and is held exclusively here.
160376ca6f88SJamie Gritton 			 */
1604c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1605c1f19219SJamie Gritton 			    sizeof(pr->pr_hostname));
1606c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1607c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
1608c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1609c1f19219SJamie Gritton 			    sizeof(pr->pr_hostuuid));
161076ca6f88SJamie Gritton 			pr->pr_hostid = pr->pr_parent->pr_hostid;
161176ca6f88SJamie Gritton 		}
161276ca6f88SJamie Gritton 	} else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
161376ca6f88SJamie Gritton 		/* Set this prison, and any descendants without PR_HOST. */
1614b38ff370SJamie Gritton 		if (host != NULL)
1615c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
161676ca6f88SJamie Gritton 		if (domain != NULL)
1617c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, domain,
1618c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
161976ca6f88SJamie Gritton 		if (uuid != NULL)
1620c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
162176ca6f88SJamie Gritton 		if (gothid)
162276ca6f88SJamie Gritton 			pr->pr_hostid = hid;
162376ca6f88SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
162476ca6f88SJamie Gritton 			if (tpr->pr_flags & PR_HOST)
162576ca6f88SJamie Gritton 				descend = 0;
162676ca6f88SJamie Gritton 			else {
162776ca6f88SJamie Gritton 				if (host != NULL)
1628c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostname,
1629c1f19219SJamie Gritton 					    pr->pr_hostname,
1630c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostname));
163176ca6f88SJamie Gritton 				if (domain != NULL)
1632c1f19219SJamie Gritton 					strlcpy(tpr->pr_domainname,
1633c1f19219SJamie Gritton 					    pr->pr_domainname,
1634c1f19219SJamie Gritton 					    sizeof(tpr->pr_domainname));
163576ca6f88SJamie Gritton 				if (uuid != NULL)
1636c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostuuid,
1637c1f19219SJamie Gritton 					    pr->pr_hostuuid,
1638c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostuuid));
163976ca6f88SJamie Gritton 				if (gothid)
164076ca6f88SJamie Gritton 					tpr->pr_hostid = hid;
164176ca6f88SJamie Gritton 			}
164276ca6f88SJamie Gritton 		}
164376ca6f88SJamie Gritton 	}
16440304c731SJamie Gritton 	if ((tallow = ch_allow & ~pr_allow)) {
16450304c731SJamie Gritton 		/* Clear allow bits in all children. */
16460304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
16470304c731SJamie Gritton 			tpr->pr_allow &= ~tallow;
16480304c731SJamie Gritton 	}
16490304c731SJamie Gritton 	pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1650b38ff370SJamie Gritton 	/*
1651b38ff370SJamie Gritton 	 * Persistent prisons get an extra reference, and prisons losing their
1652b38ff370SJamie Gritton 	 * persist flag lose that reference.  Only do this for existing prisons
1653b38ff370SJamie Gritton 	 * for now, so new ones will remain unseen until after the module
1654b38ff370SJamie Gritton 	 * handlers have completed.
1655b38ff370SJamie Gritton 	 */
1656b38ff370SJamie Gritton 	if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1657b38ff370SJamie Gritton 		if (pr_flags & PR_PERSIST) {
1658b38ff370SJamie Gritton 			pr->pr_ref++;
1659b38ff370SJamie Gritton 			pr->pr_uref++;
1660b38ff370SJamie Gritton 		} else {
1661b38ff370SJamie Gritton 			pr->pr_ref--;
1662b38ff370SJamie Gritton 			pr->pr_uref--;
1663b38ff370SJamie Gritton 		}
1664b38ff370SJamie Gritton 	}
1665b38ff370SJamie Gritton 	pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1666b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
1667b38ff370SJamie Gritton 
16680304c731SJamie Gritton 	/* Locks may have prevented a complete restriction of child IP
16690304c731SJamie Gritton 	 * addresses.  If so, allocate some more memory and try again.
16700304c731SJamie Gritton 	 */
16710304c731SJamie Gritton #ifdef INET
16720304c731SJamie Gritton 	while (redo_ip4) {
16730304c731SJamie Gritton 		ip4s = pr->pr_ip4s;
16740304c731SJamie Gritton 		ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
16750304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
16760304c731SJamie Gritton 		redo_ip4 = 0;
16770304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
16780304c731SJamie Gritton 			if (prison_restrict_ip4(tpr, ip4)) {
16790304c731SJamie Gritton 				if (ip4 != NULL)
16800304c731SJamie Gritton 					ip4 = NULL;
16810304c731SJamie Gritton 				else
16820304c731SJamie Gritton 					redo_ip4 = 1;
16830304c731SJamie Gritton 			}
16840304c731SJamie Gritton 		}
16850304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
16860304c731SJamie Gritton 	}
16870304c731SJamie Gritton #endif
16880304c731SJamie Gritton #ifdef INET6
16890304c731SJamie Gritton 	while (redo_ip6) {
16900304c731SJamie Gritton 		ip6s = pr->pr_ip6s;
16910304c731SJamie Gritton 		ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
16920304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
16930304c731SJamie Gritton 		redo_ip6 = 0;
16940304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
16950304c731SJamie Gritton 			if (prison_restrict_ip6(tpr, ip6)) {
16960304c731SJamie Gritton 				if (ip6 != NULL)
16970304c731SJamie Gritton 					ip6 = NULL;
16980304c731SJamie Gritton 				else
16990304c731SJamie Gritton 					redo_ip6 = 1;
17000304c731SJamie Gritton 			}
17010304c731SJamie Gritton 		}
17020304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
17030304c731SJamie Gritton 	}
17040304c731SJamie Gritton #endif
17050304c731SJamie Gritton 
1706b38ff370SJamie Gritton 	/* Let the modules do their work. */
1707b38ff370SJamie Gritton 	sx_downgrade(&allprison_lock);
1708b38ff370SJamie Gritton 	if (created) {
1709b38ff370SJamie Gritton 		error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1710b38ff370SJamie Gritton 		if (error) {
1711b38ff370SJamie Gritton 			prison_deref(pr, PD_LIST_SLOCKED);
1712b38ff370SJamie Gritton 			goto done_errmsg;
1713b38ff370SJamie Gritton 		}
1714b38ff370SJamie Gritton 	}
1715b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_SET, opts);
1716b38ff370SJamie Gritton 	if (error) {
1717b38ff370SJamie Gritton 		prison_deref(pr, created
1718b38ff370SJamie Gritton 		    ? PD_LIST_SLOCKED
1719b38ff370SJamie Gritton 		    : PD_DEREF | PD_LIST_SLOCKED);
1720b38ff370SJamie Gritton 		goto done_errmsg;
1721b38ff370SJamie Gritton 	}
1722b38ff370SJamie Gritton 
1723b38ff370SJamie Gritton 	/* Attach this process to the prison if requested. */
1724b38ff370SJamie Gritton 	if (flags & JAIL_ATTACH) {
1725b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
1726b38ff370SJamie Gritton 		error = do_jail_attach(td, pr);
1727b38ff370SJamie Gritton 		if (error) {
1728b38ff370SJamie Gritton 			vfs_opterror(opts, "attach failed");
1729b38ff370SJamie Gritton 			if (!created)
1730b38ff370SJamie Gritton 				prison_deref(pr, PD_DEREF);
1731b38ff370SJamie Gritton 			goto done_errmsg;
1732b38ff370SJamie Gritton 		}
1733b38ff370SJamie Gritton 	}
1734b38ff370SJamie Gritton 
1735b38ff370SJamie Gritton 	/*
1736b38ff370SJamie Gritton 	 * Now that it is all there, drop the temporary reference from existing
1737b38ff370SJamie Gritton 	 * prisons.  Or add a reference to newly created persistent prisons
1738b38ff370SJamie Gritton 	 * (which was not done earlier so that the prison would not be publicly
1739b38ff370SJamie Gritton 	 * visible).
1740b38ff370SJamie Gritton 	 */
1741b38ff370SJamie Gritton 	if (!created) {
1742b38ff370SJamie Gritton 		prison_deref(pr, (flags & JAIL_ATTACH)
1743b38ff370SJamie Gritton 		    ? PD_DEREF
1744b38ff370SJamie Gritton 		    : PD_DEREF | PD_LIST_SLOCKED);
1745b38ff370SJamie Gritton 	} else {
1746b38ff370SJamie Gritton 		if (pr_flags & PR_PERSIST) {
1747b38ff370SJamie Gritton 			mtx_lock(&pr->pr_mtx);
1748b38ff370SJamie Gritton 			pr->pr_ref++;
1749b38ff370SJamie Gritton 			pr->pr_uref++;
1750b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
1751b38ff370SJamie Gritton 		}
1752b38ff370SJamie Gritton 		if (!(flags & JAIL_ATTACH))
1753b38ff370SJamie Gritton 			sx_sunlock(&allprison_lock);
1754b38ff370SJamie Gritton 	}
1755b38ff370SJamie Gritton 	td->td_retval[0] = pr->pr_id;
1756b38ff370SJamie Gritton 	goto done_errmsg;
1757b38ff370SJamie Gritton 
17580304c731SJamie Gritton  done_deref_locked:
17590304c731SJamie Gritton 	prison_deref(pr, created
17600304c731SJamie Gritton 	    ? PD_LOCKED | PD_LIST_XLOCKED
17610304c731SJamie Gritton 	    : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
17620304c731SJamie Gritton 	goto done_releroot;
1763b38ff370SJamie Gritton  done_unlock_list:
1764b38ff370SJamie Gritton 	sx_xunlock(&allprison_lock);
1765b38ff370SJamie Gritton  done_releroot:
1766b38ff370SJamie Gritton 	if (root != NULL) {
1767b38ff370SJamie Gritton 		vfslocked = VFS_LOCK_GIANT(root->v_mount);
1768b38ff370SJamie Gritton 		vrele(root);
1769b38ff370SJamie Gritton 		VFS_UNLOCK_GIANT(vfslocked);
1770b38ff370SJamie Gritton 	}
1771b38ff370SJamie Gritton  done_errmsg:
1772b38ff370SJamie Gritton 	if (error) {
1773b38ff370SJamie Gritton 		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
1774b38ff370SJamie Gritton 		if (errmsg_len > 0) {
1775b38ff370SJamie Gritton 			errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1776b38ff370SJamie Gritton 			if (errmsg_pos > 0) {
1777b38ff370SJamie Gritton 				if (optuio->uio_segflg == UIO_SYSSPACE)
1778b38ff370SJamie Gritton 					bcopy(errmsg,
1779b38ff370SJamie Gritton 					   optuio->uio_iov[errmsg_pos].iov_base,
1780b38ff370SJamie Gritton 					   errmsg_len);
1781b38ff370SJamie Gritton 				else
1782b38ff370SJamie Gritton 					copyout(errmsg,
1783b38ff370SJamie Gritton 					   optuio->uio_iov[errmsg_pos].iov_base,
1784b38ff370SJamie Gritton 					   errmsg_len);
1785b38ff370SJamie Gritton 			}
1786b38ff370SJamie Gritton 		}
1787b38ff370SJamie Gritton 	}
1788b38ff370SJamie Gritton  done_free:
1789b38ff370SJamie Gritton #ifdef INET
1790b38ff370SJamie Gritton 	free(ip4, M_PRISON);
1791b38ff370SJamie Gritton #endif
1792b38ff370SJamie Gritton #ifdef INET6
1793b38ff370SJamie Gritton 	free(ip6, M_PRISON);
1794b38ff370SJamie Gritton #endif
1795b38ff370SJamie Gritton 	vfs_freeopts(opts);
1796b38ff370SJamie Gritton 	return (error);
1797b38ff370SJamie Gritton }
1798b38ff370SJamie Gritton 
1799b38ff370SJamie Gritton 
1800b38ff370SJamie Gritton /*
1801b38ff370SJamie Gritton  * struct jail_get_args {
1802b38ff370SJamie Gritton  *	struct iovec *iovp;
1803b38ff370SJamie Gritton  *	unsigned int iovcnt;
1804b38ff370SJamie Gritton  *	int flags;
1805b38ff370SJamie Gritton  * };
1806b38ff370SJamie Gritton  */
1807b38ff370SJamie Gritton int
1808b38ff370SJamie Gritton jail_get(struct thread *td, struct jail_get_args *uap)
1809b38ff370SJamie Gritton {
1810b38ff370SJamie Gritton 	struct uio *auio;
1811b38ff370SJamie Gritton 	int error;
1812b38ff370SJamie Gritton 
1813b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
1814b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
1815b38ff370SJamie Gritton 		return (EINVAL);
1816b38ff370SJamie Gritton 
1817b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1818b38ff370SJamie Gritton 	if (error)
1819b38ff370SJamie Gritton 		return (error);
1820b38ff370SJamie Gritton 	error = kern_jail_get(td, auio, uap->flags);
1821b38ff370SJamie Gritton 	if (error == 0)
1822b38ff370SJamie Gritton 		error = copyout(auio->uio_iov, uap->iovp,
1823b38ff370SJamie Gritton 		    uap->iovcnt * sizeof (struct iovec));
1824b38ff370SJamie Gritton 	free(auio, M_IOV);
1825b38ff370SJamie Gritton 	return (error);
1826b38ff370SJamie Gritton }
1827b38ff370SJamie Gritton 
1828b38ff370SJamie Gritton int
1829b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags)
1830b38ff370SJamie Gritton {
18310304c731SJamie Gritton 	struct prison *pr, *mypr;
1832b38ff370SJamie Gritton 	struct vfsopt *opt;
1833b38ff370SJamie Gritton 	struct vfsoptlist *opts;
1834b38ff370SJamie Gritton 	char *errmsg, *name;
18350304c731SJamie Gritton 	int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
1836b38ff370SJamie Gritton 
1837b38ff370SJamie Gritton 	if (flags & ~JAIL_GET_MASK)
1838b38ff370SJamie Gritton 		return (EINVAL);
1839b38ff370SJamie Gritton 
1840b38ff370SJamie Gritton 	/* Get the parameter list. */
1841b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
1842b38ff370SJamie Gritton 	if (error)
1843b38ff370SJamie Gritton 		return (error);
1844b38ff370SJamie Gritton 	errmsg_pos = vfs_getopt_pos(opts, "errmsg");
18450304c731SJamie Gritton 	mypr = td->td_ucred->cr_prison;
18461e2a13e6SJamie Gritton 
1847b38ff370SJamie Gritton 	/*
1848b38ff370SJamie Gritton 	 * Find the prison specified by one of: lastjid, jid, name.
1849b38ff370SJamie Gritton 	 */
1850b38ff370SJamie Gritton 	sx_slock(&allprison_lock);
1851b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
1852b38ff370SJamie Gritton 	if (error == 0) {
1853b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list) {
18540304c731SJamie Gritton 			if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
1855b38ff370SJamie Gritton 				mtx_lock(&pr->pr_mtx);
1856b38ff370SJamie Gritton 				if (pr->pr_ref > 0 &&
1857b38ff370SJamie Gritton 				    (pr->pr_uref > 0 || (flags & JAIL_DYING)))
1858b38ff370SJamie Gritton 					break;
1859b38ff370SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
1860b38ff370SJamie Gritton 			}
1861b38ff370SJamie Gritton 		}
1862b38ff370SJamie Gritton 		if (pr != NULL)
1863b38ff370SJamie Gritton 			goto found_prison;
1864b38ff370SJamie Gritton 		error = ENOENT;
1865b38ff370SJamie Gritton 		vfs_opterror(opts, "no jail after %d", jid);
1866b38ff370SJamie Gritton 		goto done_unlock_list;
1867b38ff370SJamie Gritton 	} else if (error != ENOENT)
1868b38ff370SJamie Gritton 		goto done_unlock_list;
1869b38ff370SJamie Gritton 
1870b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1871b38ff370SJamie Gritton 	if (error == 0) {
1872b38ff370SJamie Gritton 		if (jid != 0) {
18730304c731SJamie Gritton 			pr = prison_find_child(mypr, jid);
1874b38ff370SJamie Gritton 			if (pr != NULL) {
1875b38ff370SJamie Gritton 				if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1876b38ff370SJamie Gritton 					mtx_unlock(&pr->pr_mtx);
1877b38ff370SJamie Gritton 					error = ENOENT;
1878b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
1879b38ff370SJamie Gritton 					    jid);
1880b38ff370SJamie Gritton 					goto done_unlock_list;
1881b38ff370SJamie Gritton 				}
1882b38ff370SJamie Gritton 				goto found_prison;
1883b38ff370SJamie Gritton 			}
1884b38ff370SJamie Gritton 			error = ENOENT;
1885b38ff370SJamie Gritton 			vfs_opterror(opts, "jail %d not found", jid);
1886b38ff370SJamie Gritton 			goto done_unlock_list;
1887b38ff370SJamie Gritton 		}
1888b38ff370SJamie Gritton 	} else if (error != ENOENT)
1889b38ff370SJamie Gritton 		goto done_unlock_list;
1890b38ff370SJamie Gritton 
1891b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
1892b38ff370SJamie Gritton 	if (error == 0) {
1893b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
1894b38ff370SJamie Gritton 			error = EINVAL;
1895b38ff370SJamie Gritton 			goto done_unlock_list;
1896b38ff370SJamie Gritton 		}
18970304c731SJamie Gritton 		pr = prison_find_name(mypr, name);
1898b38ff370SJamie Gritton 		if (pr != NULL) {
1899b38ff370SJamie Gritton 			if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1900b38ff370SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
1901b38ff370SJamie Gritton 				error = ENOENT;
1902b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" is dying",
1903b38ff370SJamie Gritton 				    name);
1904b38ff370SJamie Gritton 				goto done_unlock_list;
1905b38ff370SJamie Gritton 			}
1906b38ff370SJamie Gritton 			goto found_prison;
1907b38ff370SJamie Gritton 		}
1908b38ff370SJamie Gritton 		error = ENOENT;
1909b38ff370SJamie Gritton 		vfs_opterror(opts, "jail \"%s\" not found", name);
1910b38ff370SJamie Gritton 		goto done_unlock_list;
1911b38ff370SJamie Gritton 	} else if (error != ENOENT)
1912b38ff370SJamie Gritton 		goto done_unlock_list;
1913b38ff370SJamie Gritton 
1914b38ff370SJamie Gritton 	vfs_opterror(opts, "no jail specified");
1915b38ff370SJamie Gritton 	error = ENOENT;
1916b38ff370SJamie Gritton 	goto done_unlock_list;
1917b38ff370SJamie Gritton 
1918b38ff370SJamie Gritton  found_prison:
1919b38ff370SJamie Gritton 	/* Get the parameters of the prison. */
1920b38ff370SJamie Gritton 	pr->pr_ref++;
1921b38ff370SJamie Gritton 	locked = PD_LOCKED;
1922b38ff370SJamie Gritton 	td->td_retval[0] = pr->pr_id;
1923b38ff370SJamie Gritton 	error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
1924b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1925b38ff370SJamie Gritton 		goto done_deref;
19260304c731SJamie Gritton 	i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
19270304c731SJamie Gritton 	error = vfs_setopt(opts, "parent", &i, sizeof(i));
1928b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1929b38ff370SJamie Gritton 		goto done_deref;
19300304c731SJamie Gritton 	error = vfs_setopts(opts, "name", prison_name(mypr, pr));
19310304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
19320304c731SJamie Gritton 		goto done_deref;
19330304c731SJamie Gritton 	error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
1934b38ff370SJamie Gritton 	    sizeof(pr->pr_cpuset->cs_id));
1935b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1936b38ff370SJamie Gritton 		goto done_deref;
19370304c731SJamie Gritton 	error = vfs_setopts(opts, "path", prison_path(mypr, pr));
1938b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1939b38ff370SJamie Gritton 		goto done_deref;
1940b38ff370SJamie Gritton #ifdef INET
1941b38ff370SJamie Gritton 	error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
1942b38ff370SJamie Gritton 	    pr->pr_ip4s * sizeof(*pr->pr_ip4));
1943b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1944b38ff370SJamie Gritton 		goto done_deref;
1945b38ff370SJamie Gritton #endif
1946b38ff370SJamie Gritton #ifdef INET6
1947b38ff370SJamie Gritton 	error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
1948b38ff370SJamie Gritton 	    pr->pr_ip6s * sizeof(*pr->pr_ip6));
1949b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1950b38ff370SJamie Gritton 		goto done_deref;
1951b38ff370SJamie Gritton #endif
1952b38ff370SJamie Gritton 	error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
1953b38ff370SJamie Gritton 	    sizeof(pr->pr_securelevel));
1954b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1955b38ff370SJamie Gritton 		goto done_deref;
1956b97457e2SJamie Gritton 	error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
1957b97457e2SJamie Gritton 	    sizeof(pr->pr_childcount));
1958b97457e2SJamie Gritton 	if (error != 0 && error != ENOENT)
1959b97457e2SJamie Gritton 		goto done_deref;
1960b97457e2SJamie Gritton 	error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
1961b97457e2SJamie Gritton 	    sizeof(pr->pr_childmax));
1962b97457e2SJamie Gritton 	if (error != 0 && error != ENOENT)
1963b97457e2SJamie Gritton 		goto done_deref;
1964c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
1965b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1966b38ff370SJamie Gritton 		goto done_deref;
1967c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
196876ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
196976ca6f88SJamie Gritton 		goto done_deref;
1970c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
197176ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
197276ca6f88SJamie Gritton 		goto done_deref;
197376ca6f88SJamie Gritton #ifdef COMPAT_IA32
197476ca6f88SJamie Gritton 	if (td->td_proc->p_sysent->sv_flags & SV_IA32) {
197576ca6f88SJamie Gritton 		uint32_t hid32 = pr->pr_hostid;
197676ca6f88SJamie Gritton 
197776ca6f88SJamie Gritton 		error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
197876ca6f88SJamie Gritton 	} else
197976ca6f88SJamie Gritton #endif
198076ca6f88SJamie Gritton 	error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
198176ca6f88SJamie Gritton 	    sizeof(pr->pr_hostid));
198276ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
198376ca6f88SJamie Gritton 		goto done_deref;
19840304c731SJamie Gritton 	error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
19850304c731SJamie Gritton 	    sizeof(pr->pr_enforce_statfs));
19860304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
19870304c731SJamie Gritton 		goto done_deref;
19880304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
19890304c731SJamie Gritton 	    fi++) {
19900304c731SJamie Gritton 		if (pr_flag_names[fi] == NULL)
19910304c731SJamie Gritton 			continue;
19920304c731SJamie Gritton 		i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
19930304c731SJamie Gritton 		error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
1994b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
1995b38ff370SJamie Gritton 			goto done_deref;
1996b38ff370SJamie Gritton 		i = !i;
19970304c731SJamie Gritton 		error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
1998b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
1999b38ff370SJamie Gritton 			goto done_deref;
20000304c731SJamie Gritton 	}
20017cbf7213SJamie Gritton 	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
20027cbf7213SJamie Gritton 	    fi++) {
20037cbf7213SJamie Gritton 		i = pr->pr_flags &
20047cbf7213SJamie Gritton 		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
20057cbf7213SJamie Gritton 		i = pr_flag_jailsys[fi].disable &&
20067cbf7213SJamie Gritton 		      (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE
20077cbf7213SJamie Gritton 		    : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW
20087cbf7213SJamie Gritton 		    : JAIL_SYS_INHERIT;
20097cbf7213SJamie Gritton 		error =
20107cbf7213SJamie Gritton 		    vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i));
20117cbf7213SJamie Gritton 		if (error != 0 && error != ENOENT)
20127cbf7213SJamie Gritton 			goto done_deref;
20137cbf7213SJamie Gritton 	}
20140304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
20150304c731SJamie Gritton 	    fi++) {
20160304c731SJamie Gritton 		if (pr_allow_names[fi] == NULL)
20170304c731SJamie Gritton 			continue;
20180304c731SJamie Gritton 		i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
20190304c731SJamie Gritton 		error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
20200304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
20210304c731SJamie Gritton 			goto done_deref;
20220304c731SJamie Gritton 		i = !i;
20230304c731SJamie Gritton 		error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
20240304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
20250304c731SJamie Gritton 			goto done_deref;
20260304c731SJamie Gritton 	}
2027b38ff370SJamie Gritton 	i = (pr->pr_uref == 0);
2028b38ff370SJamie Gritton 	error = vfs_setopt(opts, "dying", &i, sizeof(i));
2029b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2030b38ff370SJamie Gritton 		goto done_deref;
2031b38ff370SJamie Gritton 	i = !i;
2032b38ff370SJamie Gritton 	error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2033b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2034b38ff370SJamie Gritton 		goto done_deref;
2035b38ff370SJamie Gritton 
2036b38ff370SJamie Gritton 	/* Get the module parameters. */
2037b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2038b38ff370SJamie Gritton 	locked = 0;
2039b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_GET, opts);
2040b38ff370SJamie Gritton 	if (error)
2041b38ff370SJamie Gritton 		goto done_deref;
2042b38ff370SJamie Gritton 	prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
2043b38ff370SJamie Gritton 
2044b38ff370SJamie Gritton 	/* By now, all parameters should have been noted. */
2045b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
2046b38ff370SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
2047b38ff370SJamie Gritton 			error = EINVAL;
2048b38ff370SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
2049b38ff370SJamie Gritton 			goto done_errmsg;
2050b38ff370SJamie Gritton 		}
2051b38ff370SJamie Gritton 	}
2052b38ff370SJamie Gritton 
2053b38ff370SJamie Gritton 	/* Write the fetched parameters back to userspace. */
2054b38ff370SJamie Gritton 	error = 0;
2055b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
2056b38ff370SJamie Gritton 		if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2057b38ff370SJamie Gritton 			pos = 2 * opt->pos + 1;
2058b38ff370SJamie Gritton 			optuio->uio_iov[pos].iov_len = opt->len;
2059b38ff370SJamie Gritton 			if (opt->value != NULL) {
2060b38ff370SJamie Gritton 				if (optuio->uio_segflg == UIO_SYSSPACE) {
2061b38ff370SJamie Gritton 					bcopy(opt->value,
2062b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
2063b38ff370SJamie Gritton 					    opt->len);
2064b38ff370SJamie Gritton 				} else {
2065b38ff370SJamie Gritton 					error = copyout(opt->value,
2066b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
2067b38ff370SJamie Gritton 					    opt->len);
2068b38ff370SJamie Gritton 					if (error)
2069b38ff370SJamie Gritton 						break;
2070b38ff370SJamie Gritton 				}
2071b38ff370SJamie Gritton 			}
2072b38ff370SJamie Gritton 		}
2073b38ff370SJamie Gritton 	}
2074b38ff370SJamie Gritton 	goto done_errmsg;
2075b38ff370SJamie Gritton 
2076b38ff370SJamie Gritton  done_deref:
2077b38ff370SJamie Gritton 	prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2078b38ff370SJamie Gritton 	goto done_errmsg;
2079b38ff370SJamie Gritton 
2080b38ff370SJamie Gritton  done_unlock_list:
2081b38ff370SJamie Gritton 	sx_sunlock(&allprison_lock);
2082b38ff370SJamie Gritton  done_errmsg:
2083b38ff370SJamie Gritton 	if (error && errmsg_pos >= 0) {
2084b38ff370SJamie Gritton 		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2085b38ff370SJamie Gritton 		errmsg_pos = 2 * errmsg_pos + 1;
2086b38ff370SJamie Gritton 		if (errmsg_len > 0) {
2087b38ff370SJamie Gritton 			if (optuio->uio_segflg == UIO_SYSSPACE)
2088b38ff370SJamie Gritton 				bcopy(errmsg,
2089b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2090b38ff370SJamie Gritton 				    errmsg_len);
2091b38ff370SJamie Gritton 			else
2092b38ff370SJamie Gritton 				copyout(errmsg,
2093b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2094b38ff370SJamie Gritton 				    errmsg_len);
2095b38ff370SJamie Gritton 		}
2096b38ff370SJamie Gritton 	}
2097b38ff370SJamie Gritton 	vfs_freeopts(opts);
2098b38ff370SJamie Gritton 	return (error);
2099b38ff370SJamie Gritton }
2100b38ff370SJamie Gritton 
21010304c731SJamie Gritton 
2102b38ff370SJamie Gritton /*
2103b38ff370SJamie Gritton  * struct jail_remove_args {
2104b38ff370SJamie Gritton  *	int jid;
2105b38ff370SJamie Gritton  * };
2106b38ff370SJamie Gritton  */
2107b38ff370SJamie Gritton int
2108b38ff370SJamie Gritton jail_remove(struct thread *td, struct jail_remove_args *uap)
2109b38ff370SJamie Gritton {
21100304c731SJamie Gritton 	struct prison *pr, *cpr, *lpr, *tpr;
21110304c731SJamie Gritton 	int descend, error;
2112b38ff370SJamie Gritton 
2113b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_REMOVE);
2114b38ff370SJamie Gritton 	if (error)
2115b38ff370SJamie Gritton 		return (error);
2116b38ff370SJamie Gritton 
2117b38ff370SJamie Gritton 	sx_xlock(&allprison_lock);
21180304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2119b38ff370SJamie Gritton 	if (pr == NULL) {
2120b38ff370SJamie Gritton 		sx_xunlock(&allprison_lock);
2121b38ff370SJamie Gritton 		return (EINVAL);
2122b38ff370SJamie Gritton 	}
2123b38ff370SJamie Gritton 
21240304c731SJamie Gritton 	/* Remove all descendants of this prison, then remove this prison. */
21250304c731SJamie Gritton 	pr->pr_ref++;
21260304c731SJamie Gritton 	pr->pr_flags |= PR_REMOVE;
21270304c731SJamie Gritton 	if (!LIST_EMPTY(&pr->pr_children)) {
21280304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
21290304c731SJamie Gritton 		lpr = NULL;
21300304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
21310304c731SJamie Gritton 			mtx_lock(&cpr->pr_mtx);
21320304c731SJamie Gritton 			if (cpr->pr_ref > 0) {
21330304c731SJamie Gritton 				tpr = cpr;
21340304c731SJamie Gritton 				cpr->pr_ref++;
21350304c731SJamie Gritton 				cpr->pr_flags |= PR_REMOVE;
21360304c731SJamie Gritton 			} else {
21370304c731SJamie Gritton 				/* Already removed - do not do it again. */
21380304c731SJamie Gritton 				tpr = NULL;
21390304c731SJamie Gritton 			}
21400304c731SJamie Gritton 			mtx_unlock(&cpr->pr_mtx);
21410304c731SJamie Gritton 			if (lpr != NULL) {
21420304c731SJamie Gritton 				mtx_lock(&lpr->pr_mtx);
21430304c731SJamie Gritton 				prison_remove_one(lpr);
21440304c731SJamie Gritton 				sx_xlock(&allprison_lock);
21450304c731SJamie Gritton 			}
21460304c731SJamie Gritton 			lpr = tpr;
21470304c731SJamie Gritton 		}
21480304c731SJamie Gritton 		if (lpr != NULL) {
21490304c731SJamie Gritton 			mtx_lock(&lpr->pr_mtx);
21500304c731SJamie Gritton 			prison_remove_one(lpr);
21510304c731SJamie Gritton 			sx_xlock(&allprison_lock);
21520304c731SJamie Gritton 		}
21530304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
21540304c731SJamie Gritton 	}
21550304c731SJamie Gritton 	prison_remove_one(pr);
21560304c731SJamie Gritton 	return (0);
21570304c731SJamie Gritton }
21580304c731SJamie Gritton 
21590304c731SJamie Gritton static void
21600304c731SJamie Gritton prison_remove_one(struct prison *pr)
21610304c731SJamie Gritton {
21620304c731SJamie Gritton 	struct proc *p;
21630304c731SJamie Gritton 	int deuref;
21640304c731SJamie Gritton 
2165b38ff370SJamie Gritton 	/* If the prison was persistent, it is not anymore. */
2166b38ff370SJamie Gritton 	deuref = 0;
2167b38ff370SJamie Gritton 	if (pr->pr_flags & PR_PERSIST) {
2168b38ff370SJamie Gritton 		pr->pr_ref--;
2169b38ff370SJamie Gritton 		deuref = PD_DEUREF;
2170b38ff370SJamie Gritton 		pr->pr_flags &= ~PR_PERSIST;
2171b38ff370SJamie Gritton 	}
2172b38ff370SJamie Gritton 
21730304c731SJamie Gritton 	/*
21740304c731SJamie Gritton 	 * jail_remove added a reference.  If that's the only one, remove
21750304c731SJamie Gritton 	 * the prison now.
21760304c731SJamie Gritton 	 */
21770304c731SJamie Gritton 	KASSERT(pr->pr_ref > 0,
21780304c731SJamie Gritton 	    ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
21790304c731SJamie Gritton 	if (pr->pr_ref == 1) {
2180b38ff370SJamie Gritton 		prison_deref(pr,
2181b38ff370SJamie Gritton 		    deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
21820304c731SJamie Gritton 		return;
2183b38ff370SJamie Gritton 	}
2184b38ff370SJamie Gritton 
2185b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2186b38ff370SJamie Gritton 	sx_xunlock(&allprison_lock);
2187b38ff370SJamie Gritton 	/*
2188b38ff370SJamie Gritton 	 * Kill all processes unfortunate enough to be attached to this prison.
2189b38ff370SJamie Gritton 	 */
2190b38ff370SJamie Gritton 	sx_slock(&allproc_lock);
2191b38ff370SJamie Gritton 	LIST_FOREACH(p, &allproc, p_list) {
2192b38ff370SJamie Gritton 		PROC_LOCK(p);
2193b38ff370SJamie Gritton 		if (p->p_state != PRS_NEW && p->p_ucred &&
2194b38ff370SJamie Gritton 		    p->p_ucred->cr_prison == pr)
2195b38ff370SJamie Gritton 			psignal(p, SIGKILL);
2196b38ff370SJamie Gritton 		PROC_UNLOCK(p);
2197b38ff370SJamie Gritton 	}
2198b38ff370SJamie Gritton 	sx_sunlock(&allproc_lock);
21990304c731SJamie Gritton 	/* Remove the temporary reference added by jail_remove. */
2200b38ff370SJamie Gritton 	prison_deref(pr, deuref | PD_DEREF);
220175c13541SPoul-Henning Kamp }
220275c13541SPoul-Henning Kamp 
22038571af59SJamie Gritton 
2204fd7a8150SMike Barcroft /*
22059ddb7954SMike Barcroft  * struct jail_attach_args {
22069ddb7954SMike Barcroft  *	int jid;
22079ddb7954SMike Barcroft  * };
2208fd7a8150SMike Barcroft  */
2209fd7a8150SMike Barcroft int
22109ddb7954SMike Barcroft jail_attach(struct thread *td, struct jail_attach_args *uap)
2211fd7a8150SMike Barcroft {
2212b38ff370SJamie Gritton 	struct prison *pr;
2213b38ff370SJamie Gritton 	int error;
2214b38ff370SJamie Gritton 
2215b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_ATTACH);
2216b38ff370SJamie Gritton 	if (error)
2217b38ff370SJamie Gritton 		return (error);
2218b38ff370SJamie Gritton 
2219b38ff370SJamie Gritton 	sx_slock(&allprison_lock);
22200304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2221b38ff370SJamie Gritton 	if (pr == NULL) {
2222b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2223b38ff370SJamie Gritton 		return (EINVAL);
2224b38ff370SJamie Gritton 	}
2225b38ff370SJamie Gritton 
2226b38ff370SJamie Gritton 	/*
2227b38ff370SJamie Gritton 	 * Do not allow a process to attach to a prison that is not
2228b38ff370SJamie Gritton 	 * considered to be "alive".
2229b38ff370SJamie Gritton 	 */
2230b38ff370SJamie Gritton 	if (pr->pr_uref == 0) {
2231b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2232b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2233b38ff370SJamie Gritton 		return (EINVAL);
2234b38ff370SJamie Gritton 	}
2235b38ff370SJamie Gritton 
2236b38ff370SJamie Gritton 	return (do_jail_attach(td, pr));
2237b38ff370SJamie Gritton }
2238b38ff370SJamie Gritton 
2239b38ff370SJamie Gritton static int
2240b38ff370SJamie Gritton do_jail_attach(struct thread *td, struct prison *pr)
2241b38ff370SJamie Gritton {
22420304c731SJamie Gritton 	struct prison *ppr;
2243fd7a8150SMike Barcroft 	struct proc *p;
2244fd7a8150SMike Barcroft 	struct ucred *newcred, *oldcred;
2245453f7d53SChristian S.J. Peron 	int vfslocked, error;
2246fd7a8150SMike Barcroft 
224757f22bd4SJacques Vidrine 	/*
224857f22bd4SJacques Vidrine 	 * XXX: Note that there is a slight race here if two threads
224957f22bd4SJacques Vidrine 	 * in the same privileged process attempt to attach to two
225057f22bd4SJacques Vidrine 	 * different jails at the same time.  It is important for
225157f22bd4SJacques Vidrine 	 * user processes not to do this, or they might end up with
225257f22bd4SJacques Vidrine 	 * a process root from one prison, but attached to the jail
225357f22bd4SJacques Vidrine 	 * of another.
225457f22bd4SJacques Vidrine 	 */
2255fd7a8150SMike Barcroft 	pr->pr_ref++;
2256b38ff370SJamie Gritton 	pr->pr_uref++;
2257fd7a8150SMike Barcroft 	mtx_unlock(&pr->pr_mtx);
2258b38ff370SJamie Gritton 
2259b38ff370SJamie Gritton 	/* Let modules do whatever they need to prepare for attaching. */
2260b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2261b38ff370SJamie Gritton 	if (error) {
2262b38ff370SJamie Gritton 		prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2263b38ff370SJamie Gritton 		return (error);
2264b38ff370SJamie Gritton 	}
2265dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
2266fd7a8150SMike Barcroft 
2267413628a7SBjoern A. Zeeb 	/*
2268413628a7SBjoern A. Zeeb 	 * Reparent the newly attached process to this jail.
2269413628a7SBjoern A. Zeeb 	 */
22700304c731SJamie Gritton 	ppr = td->td_ucred->cr_prison;
2271b38ff370SJamie Gritton 	p = td->td_proc;
2272413628a7SBjoern A. Zeeb 	error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2273413628a7SBjoern A. Zeeb 	if (error)
2274b38ff370SJamie Gritton 		goto e_revert_osd;
2275413628a7SBjoern A. Zeeb 
2276453f7d53SChristian S.J. Peron 	vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2277cb05b60aSAttilio Rao 	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2278fd7a8150SMike Barcroft 	if ((error = change_dir(pr->pr_root, td)) != 0)
2279fd7a8150SMike Barcroft 		goto e_unlock;
2280fd7a8150SMike Barcroft #ifdef MAC
228130d239bcSRobert Watson 	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2282fd7a8150SMike Barcroft 		goto e_unlock;
2283fd7a8150SMike Barcroft #endif
228422db15c0SAttilio Rao 	VOP_UNLOCK(pr->pr_root, 0);
2285b38ff370SJamie Gritton 	if ((error = change_root(pr->pr_root, td)))
2286b38ff370SJamie Gritton 		goto e_unlock_giant;
2287453f7d53SChristian S.J. Peron 	VFS_UNLOCK_GIANT(vfslocked);
2288fd7a8150SMike Barcroft 
2289fd7a8150SMike Barcroft 	newcred = crget();
2290fd7a8150SMike Barcroft 	PROC_LOCK(p);
2291fd7a8150SMike Barcroft 	oldcred = p->p_ucred;
2292fd7a8150SMike Barcroft 	setsugid(p);
2293fd7a8150SMike Barcroft 	crcopy(newcred, oldcred);
229469c4ee54SJohn Baldwin 	newcred->cr_prison = pr;
2295fd7a8150SMike Barcroft 	p->p_ucred = newcred;
2296fd7a8150SMike Barcroft 	PROC_UNLOCK(p);
2297fd7a8150SMike Barcroft 	crfree(oldcred);
22980304c731SJamie Gritton 	prison_deref(ppr, PD_DEREF | PD_DEUREF);
2299fd7a8150SMike Barcroft 	return (0);
2300fd7a8150SMike Barcroft  e_unlock:
230122db15c0SAttilio Rao 	VOP_UNLOCK(pr->pr_root, 0);
2302b38ff370SJamie Gritton  e_unlock_giant:
2303453f7d53SChristian S.J. Peron 	VFS_UNLOCK_GIANT(vfslocked);
2304b38ff370SJamie Gritton  e_revert_osd:
2305b38ff370SJamie Gritton 	/* Tell modules this thread is still in its old jail after all. */
23060304c731SJamie Gritton 	(void)osd_jail_call(ppr, PR_METHOD_ATTACH, td);
2307b38ff370SJamie Gritton 	prison_deref(pr, PD_DEREF | PD_DEUREF);
2308fd7a8150SMike Barcroft 	return (error);
2309fd7a8150SMike Barcroft }
2310fd7a8150SMike Barcroft 
23110304c731SJamie Gritton 
2312fd7a8150SMike Barcroft /*
2313fd7a8150SMike Barcroft  * Returns a locked prison instance, or NULL on failure.
2314fd7a8150SMike Barcroft  */
231554b369c1SPawel Jakub Dawidek struct prison *
2316fd7a8150SMike Barcroft prison_find(int prid)
2317fd7a8150SMike Barcroft {
2318fd7a8150SMike Barcroft 	struct prison *pr;
2319fd7a8150SMike Barcroft 
2320dc68a633SPawel Jakub Dawidek 	sx_assert(&allprison_lock, SX_LOCKED);
2321b38ff370SJamie Gritton 	TAILQ_FOREACH(pr, &allprison, pr_list) {
2322fd7a8150SMike Barcroft 		if (pr->pr_id == prid) {
2323fd7a8150SMike Barcroft 			mtx_lock(&pr->pr_mtx);
2324b38ff370SJamie Gritton 			if (pr->pr_ref > 0)
2325fd7a8150SMike Barcroft 				return (pr);
2326b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2327fd7a8150SMike Barcroft 		}
2328fd7a8150SMike Barcroft 	}
2329fd7a8150SMike Barcroft 	return (NULL);
2330fd7a8150SMike Barcroft }
2331fd7a8150SMike Barcroft 
2332b38ff370SJamie Gritton /*
23330304c731SJamie Gritton  * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
2334b38ff370SJamie Gritton  */
2335b38ff370SJamie Gritton struct prison *
23360304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid)
2337b38ff370SJamie Gritton {
23380304c731SJamie Gritton 	struct prison *pr;
23390304c731SJamie Gritton 	int descend;
2340b38ff370SJamie Gritton 
2341b38ff370SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
23420304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
23430304c731SJamie Gritton 		if (pr->pr_id == prid) {
23440304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
23450304c731SJamie Gritton 			if (pr->pr_ref > 0)
23460304c731SJamie Gritton 				return (pr);
23470304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
23480304c731SJamie Gritton 		}
23490304c731SJamie Gritton 	}
23500304c731SJamie Gritton 	return (NULL);
23510304c731SJamie Gritton }
23520304c731SJamie Gritton 
23530304c731SJamie Gritton /*
23540304c731SJamie Gritton  * Look for the name relative to mypr.  Returns a locked prison or NULL.
23550304c731SJamie Gritton  */
23560304c731SJamie Gritton struct prison *
23570304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name)
23580304c731SJamie Gritton {
23590304c731SJamie Gritton 	struct prison *pr, *deadpr;
23600304c731SJamie Gritton 	size_t mylen;
23610304c731SJamie Gritton 	int descend;
23620304c731SJamie Gritton 
23630304c731SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
23640304c731SJamie Gritton 	mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2365b38ff370SJamie Gritton  again:
2366b38ff370SJamie Gritton 	deadpr = NULL;
23670304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
23680304c731SJamie Gritton 		if (!strcmp(pr->pr_name + mylen, name)) {
2369b38ff370SJamie Gritton 			mtx_lock(&pr->pr_mtx);
2370b38ff370SJamie Gritton 			if (pr->pr_ref > 0) {
2371b38ff370SJamie Gritton 				if (pr->pr_uref > 0)
2372b38ff370SJamie Gritton 					return (pr);
2373b38ff370SJamie Gritton 				deadpr = pr;
2374b38ff370SJamie Gritton 			}
2375b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2376b38ff370SJamie Gritton 		}
2377b38ff370SJamie Gritton 	}
23780304c731SJamie Gritton 	/* There was no valid prison - perhaps there was a dying one. */
2379b38ff370SJamie Gritton 	if (deadpr != NULL) {
2380b38ff370SJamie Gritton 		mtx_lock(&deadpr->pr_mtx);
2381b38ff370SJamie Gritton 		if (deadpr->pr_ref == 0) {
2382b38ff370SJamie Gritton 			mtx_unlock(&deadpr->pr_mtx);
2383b38ff370SJamie Gritton 			goto again;
2384b38ff370SJamie Gritton 		}
2385b38ff370SJamie Gritton 	}
2386b38ff370SJamie Gritton 	return (deadpr);
2387b38ff370SJamie Gritton }
2388b38ff370SJamie Gritton 
2389b38ff370SJamie Gritton /*
23900304c731SJamie Gritton  * See if a prison has the specific flag set.
23910304c731SJamie Gritton  */
23920304c731SJamie Gritton int
23930304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag)
23940304c731SJamie Gritton {
23950304c731SJamie Gritton 
23960304c731SJamie Gritton 	/* This is an atomic read, so no locking is necessary. */
23970304c731SJamie Gritton 	return (cred->cr_prison->pr_flags & flag);
23980304c731SJamie Gritton }
23990304c731SJamie Gritton 
24000304c731SJamie Gritton int
24010304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag)
24020304c731SJamie Gritton {
24030304c731SJamie Gritton 
24040304c731SJamie Gritton 	/* This is an atomic read, so no locking is necessary. */
24050304c731SJamie Gritton 	return (cred->cr_prison->pr_allow & flag);
24060304c731SJamie Gritton }
24070304c731SJamie Gritton 
24080304c731SJamie Gritton /*
2409b38ff370SJamie Gritton  * Remove a prison reference.  If that was the last reference, remove the
2410b38ff370SJamie Gritton  * prison itself - but not in this context in case there are locks held.
2411b38ff370SJamie Gritton  */
241291421ba2SRobert Watson void
24131ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr)
241491421ba2SRobert Watson {
241591421ba2SRobert Watson 
24161ba4a712SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
241791421ba2SRobert Watson 	pr->pr_ref--;
241891421ba2SRobert Watson 	if (pr->pr_ref == 0) {
241901137630SRobert Watson 		mtx_unlock(&pr->pr_mtx);
2420c2cda609SPawel Jakub Dawidek 		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
2421c2cda609SPawel Jakub Dawidek 		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2422c2cda609SPawel Jakub Dawidek 		return;
2423c2cda609SPawel Jakub Dawidek 	}
2424c2cda609SPawel Jakub Dawidek 	mtx_unlock(&pr->pr_mtx);
2425c2cda609SPawel Jakub Dawidek }
2426c2cda609SPawel Jakub Dawidek 
24271ba4a712SPawel Jakub Dawidek void
24281ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr)
24291ba4a712SPawel Jakub Dawidek {
24301ba4a712SPawel Jakub Dawidek 
24311ba4a712SPawel Jakub Dawidek 	mtx_lock(&pr->pr_mtx);
24321ba4a712SPawel Jakub Dawidek 	prison_free_locked(pr);
24331ba4a712SPawel Jakub Dawidek }
24341ba4a712SPawel Jakub Dawidek 
2435c2cda609SPawel Jakub Dawidek static void
2436c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending)
2437c2cda609SPawel Jakub Dawidek {
2438b38ff370SJamie Gritton 
2439b38ff370SJamie Gritton 	prison_deref((struct prison *)context, 0);
2440b38ff370SJamie Gritton }
2441b38ff370SJamie Gritton 
2442b38ff370SJamie Gritton /*
2443b38ff370SJamie Gritton  * Remove a prison reference (usually).  This internal version assumes no
2444b38ff370SJamie Gritton  * mutexes are held, except perhaps the prison itself.  If there are no more
2445b38ff370SJamie Gritton  * references, release and delist the prison.  On completion, the prison lock
2446b38ff370SJamie Gritton  * and the allprison lock are both unlocked.
2447b38ff370SJamie Gritton  */
2448b38ff370SJamie Gritton static void
2449b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags)
2450b38ff370SJamie Gritton {
24510304c731SJamie Gritton 	struct prison *ppr, *tpr;
2452c2cda609SPawel Jakub Dawidek 	int vfslocked;
2453c2cda609SPawel Jakub Dawidek 
2454b38ff370SJamie Gritton 	if (!(flags & PD_LOCKED))
2455b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
24560304c731SJamie Gritton 	/* Decrement the user references in a separate loop. */
2457b38ff370SJamie Gritton 	if (flags & PD_DEUREF) {
24580304c731SJamie Gritton 		for (tpr = pr;; tpr = tpr->pr_parent) {
24590304c731SJamie Gritton 			if (tpr != pr)
24600304c731SJamie Gritton 				mtx_lock(&tpr->pr_mtx);
24610304c731SJamie Gritton 			if (--tpr->pr_uref > 0)
24620304c731SJamie Gritton 				break;
24630304c731SJamie Gritton 			KASSERT(tpr != &prison0, ("prison0 pr_uref=0"));
24640304c731SJamie Gritton 			mtx_unlock(&tpr->pr_mtx);
24650304c731SJamie Gritton 		}
2466b38ff370SJamie Gritton 		/* Done if there were only user references to remove. */
2467b38ff370SJamie Gritton 		if (!(flags & PD_DEREF)) {
24680304c731SJamie Gritton 			mtx_unlock(&tpr->pr_mtx);
2469b38ff370SJamie Gritton 			if (flags & PD_LIST_SLOCKED)
2470b38ff370SJamie Gritton 				sx_sunlock(&allprison_lock);
2471b38ff370SJamie Gritton 			else if (flags & PD_LIST_XLOCKED)
2472b38ff370SJamie Gritton 				sx_xunlock(&allprison_lock);
2473b38ff370SJamie Gritton 			return;
2474b38ff370SJamie Gritton 		}
24750304c731SJamie Gritton 		if (tpr != pr) {
24760304c731SJamie Gritton 			mtx_unlock(&tpr->pr_mtx);
24770304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
2478b38ff370SJamie Gritton 		}
24790304c731SJamie Gritton 	}
24800304c731SJamie Gritton 
24810304c731SJamie Gritton 	for (;;) {
2482b38ff370SJamie Gritton 		if (flags & PD_DEREF)
2483b38ff370SJamie Gritton 			pr->pr_ref--;
2484b38ff370SJamie Gritton 		/* If the prison still has references, nothing else to do. */
2485b38ff370SJamie Gritton 		if (pr->pr_ref > 0) {
2486b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2487b38ff370SJamie Gritton 			if (flags & PD_LIST_SLOCKED)
2488b38ff370SJamie Gritton 				sx_sunlock(&allprison_lock);
2489b38ff370SJamie Gritton 			else if (flags & PD_LIST_XLOCKED)
2490b38ff370SJamie Gritton 				sx_xunlock(&allprison_lock);
2491b38ff370SJamie Gritton 			return;
2492b38ff370SJamie Gritton 		}
2493c2cda609SPawel Jakub Dawidek 
2494b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2495b38ff370SJamie Gritton 		if (flags & PD_LIST_SLOCKED) {
2496b38ff370SJamie Gritton 			if (!sx_try_upgrade(&allprison_lock)) {
2497b38ff370SJamie Gritton 				sx_sunlock(&allprison_lock);
2498c2cda609SPawel Jakub Dawidek 				sx_xlock(&allprison_lock);
2499b38ff370SJamie Gritton 			}
2500b38ff370SJamie Gritton 		} else if (!(flags & PD_LIST_XLOCKED))
2501b38ff370SJamie Gritton 			sx_xlock(&allprison_lock);
2502b38ff370SJamie Gritton 
2503b38ff370SJamie Gritton 		TAILQ_REMOVE(&allprison, pr, pr_list);
25040304c731SJamie Gritton 		LIST_REMOVE(pr, pr_sibling);
25050304c731SJamie Gritton 		ppr = pr->pr_parent;
25060304c731SJamie Gritton 		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2507b97457e2SJamie Gritton 			tpr->pr_childcount--;
25080304c731SJamie Gritton 		sx_downgrade(&allprison_lock);
25091ba4a712SPawel Jakub Dawidek 
2510679e1390SJamie Gritton #ifdef VIMAGE
2511679e1390SJamie Gritton 		if (pr->pr_flags & PR_VNET)
2512679e1390SJamie Gritton 			vnet_destroy(pr->pr_vnet);
2513679e1390SJamie Gritton #endif
2514b38ff370SJamie Gritton 		if (pr->pr_root != NULL) {
2515453f7d53SChristian S.J. Peron 			vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2516b3059e09SRobert Watson 			vrele(pr->pr_root);
2517453f7d53SChristian S.J. Peron 			VFS_UNLOCK_GIANT(vfslocked);
2518b38ff370SJamie Gritton 		}
2519b3059e09SRobert Watson 		mtx_destroy(&pr->pr_mtx);
2520413628a7SBjoern A. Zeeb #ifdef INET
2521413628a7SBjoern A. Zeeb 		free(pr->pr_ip4, M_PRISON);
2522413628a7SBjoern A. Zeeb #endif
2523b38ff370SJamie Gritton #ifdef INET6
2524b38ff370SJamie Gritton 		free(pr->pr_ip6, M_PRISON);
2525b38ff370SJamie Gritton #endif
2526b38ff370SJamie Gritton 		if (pr->pr_cpuset != NULL)
2527b38ff370SJamie Gritton 			cpuset_rel(pr->pr_cpuset);
2528b38ff370SJamie Gritton 		osd_jail_exit(pr);
25291ede983cSDag-Erling Smørgrav 		free(pr, M_PRISON);
25300304c731SJamie Gritton 
25310304c731SJamie Gritton 		/* Removing a prison frees a reference on its parent. */
25320304c731SJamie Gritton 		pr = ppr;
25330304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
25340304c731SJamie Gritton 		flags = PD_DEREF | PD_LIST_SLOCKED;
25350304c731SJamie Gritton 	}
2536b3059e09SRobert Watson }
2537b3059e09SRobert Watson 
253891421ba2SRobert Watson void
25391ba4a712SPawel Jakub Dawidek prison_hold_locked(struct prison *pr)
25401ba4a712SPawel Jakub Dawidek {
25411ba4a712SPawel Jakub Dawidek 
25421ba4a712SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
25431ba4a712SPawel Jakub Dawidek 	KASSERT(pr->pr_ref > 0,
2544af7bd9a4SJamie Gritton 	    ("Trying to hold dead prison (jid=%d).", pr->pr_id));
25451ba4a712SPawel Jakub Dawidek 	pr->pr_ref++;
25461ba4a712SPawel Jakub Dawidek }
25471ba4a712SPawel Jakub Dawidek 
25481ba4a712SPawel Jakub Dawidek void
254991421ba2SRobert Watson prison_hold(struct prison *pr)
255091421ba2SRobert Watson {
255191421ba2SRobert Watson 
255201137630SRobert Watson 	mtx_lock(&pr->pr_mtx);
25531ba4a712SPawel Jakub Dawidek 	prison_hold_locked(pr);
255401137630SRobert Watson 	mtx_unlock(&pr->pr_mtx);
255501137630SRobert Watson }
255601137630SRobert Watson 
2557413628a7SBjoern A. Zeeb void
2558413628a7SBjoern A. Zeeb prison_proc_hold(struct prison *pr)
255901137630SRobert Watson {
256001137630SRobert Watson 
2561413628a7SBjoern A. Zeeb 	mtx_lock(&pr->pr_mtx);
2562b38ff370SJamie Gritton 	KASSERT(pr->pr_uref > 0,
2563b38ff370SJamie Gritton 	    ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2564b38ff370SJamie Gritton 	pr->pr_uref++;
2565413628a7SBjoern A. Zeeb 	mtx_unlock(&pr->pr_mtx);
256675c13541SPoul-Henning Kamp }
256775c13541SPoul-Henning Kamp 
256875c13541SPoul-Henning Kamp void
2569413628a7SBjoern A. Zeeb prison_proc_free(struct prison *pr)
257075c13541SPoul-Henning Kamp {
2571413628a7SBjoern A. Zeeb 
2572413628a7SBjoern A. Zeeb 	mtx_lock(&pr->pr_mtx);
2573b38ff370SJamie Gritton 	KASSERT(pr->pr_uref > 0,
2574b38ff370SJamie Gritton 	    ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2575b38ff370SJamie Gritton 	prison_deref(pr, PD_DEUREF | PD_LOCKED);
2576413628a7SBjoern A. Zeeb }
2577413628a7SBjoern A. Zeeb 
2578413628a7SBjoern A. Zeeb 
2579413628a7SBjoern A. Zeeb #ifdef INET
2580413628a7SBjoern A. Zeeb /*
25810304c731SJamie Gritton  * Restrict a prison's IP address list with its parent's, possibly replacing
25820304c731SJamie Gritton  * it.  Return true if the replacement buffer was used (or would have been).
25830304c731SJamie Gritton  */
25840304c731SJamie Gritton static int
25850304c731SJamie Gritton prison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
25860304c731SJamie Gritton {
25870304c731SJamie Gritton 	int ii, ij, used;
25880304c731SJamie Gritton 	struct prison *ppr;
25890304c731SJamie Gritton 
25900304c731SJamie Gritton 	ppr = pr->pr_parent;
25910304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4_USER)) {
25920304c731SJamie Gritton 		/* This has no user settings, so just copy the parent's list. */
25930304c731SJamie Gritton 		if (pr->pr_ip4s < ppr->pr_ip4s) {
25940304c731SJamie Gritton 			/*
25950304c731SJamie Gritton 			 * There's no room for the parent's list.  Use the
25960304c731SJamie Gritton 			 * new list buffer, which is assumed to be big enough
25970304c731SJamie Gritton 			 * (if it was passed).  If there's no buffer, try to
25980304c731SJamie Gritton 			 * allocate one.
25990304c731SJamie Gritton 			 */
26000304c731SJamie Gritton 			used = 1;
26010304c731SJamie Gritton 			if (newip4 == NULL) {
26020304c731SJamie Gritton 				newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
26030304c731SJamie Gritton 				    M_PRISON, M_NOWAIT);
26040304c731SJamie Gritton 				if (newip4 != NULL)
26050304c731SJamie Gritton 					used = 0;
26060304c731SJamie Gritton 			}
26070304c731SJamie Gritton 			if (newip4 != NULL) {
26080304c731SJamie Gritton 				bcopy(ppr->pr_ip4, newip4,
26090304c731SJamie Gritton 				    ppr->pr_ip4s * sizeof(*newip4));
26100304c731SJamie Gritton 				free(pr->pr_ip4, M_PRISON);
26110304c731SJamie Gritton 				pr->pr_ip4 = newip4;
26120304c731SJamie Gritton 				pr->pr_ip4s = ppr->pr_ip4s;
26130304c731SJamie Gritton 				pr->pr_flags |= PR_IP4;
26140304c731SJamie Gritton 			}
26150304c731SJamie Gritton 			return (used);
26160304c731SJamie Gritton 		}
26170304c731SJamie Gritton 		pr->pr_ip4s = ppr->pr_ip4s;
26180304c731SJamie Gritton 		if (pr->pr_ip4s > 0)
26190304c731SJamie Gritton 			bcopy(ppr->pr_ip4, pr->pr_ip4,
26200304c731SJamie Gritton 			    pr->pr_ip4s * sizeof(*newip4));
26210304c731SJamie Gritton 		else if (pr->pr_ip4 != NULL) {
26220304c731SJamie Gritton 			free(pr->pr_ip4, M_PRISON);
26230304c731SJamie Gritton 			pr->pr_ip4 = NULL;
26240304c731SJamie Gritton 		}
26250304c731SJamie Gritton 		pr->pr_flags =
26260304c731SJamie Gritton 			(pr->pr_flags & ~PR_IP4) | (ppr->pr_flags & PR_IP4);
26270304c731SJamie Gritton 	} else if (pr->pr_ip4s > 0 && (ppr->pr_flags & PR_IP4)) {
26280304c731SJamie Gritton 		/* Remove addresses that aren't in the parent. */
26290304c731SJamie Gritton 		for (ij = 0; ij < ppr->pr_ip4s; ij++)
26300304c731SJamie Gritton 			if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
26310304c731SJamie Gritton 				break;
26320304c731SJamie Gritton 		if (ij < ppr->pr_ip4s)
26330304c731SJamie Gritton 			ii = 1;
26340304c731SJamie Gritton 		else {
26350304c731SJamie Gritton 			bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
26360304c731SJamie Gritton 			    --pr->pr_ip4s * sizeof(*pr->pr_ip4));
26370304c731SJamie Gritton 			ii = 0;
26380304c731SJamie Gritton 		}
26390304c731SJamie Gritton 		for (ij = 1; ii < pr->pr_ip4s; ) {
26400304c731SJamie Gritton 			if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
26410304c731SJamie Gritton 				ii++;
26420304c731SJamie Gritton 				continue;
26430304c731SJamie Gritton 			}
26440304c731SJamie Gritton 			switch (ij >= ppr->pr_ip4s ? -1 :
26450304c731SJamie Gritton 				qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
26460304c731SJamie Gritton 			case -1:
26470304c731SJamie Gritton 				bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
26480304c731SJamie Gritton 				    (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
26490304c731SJamie Gritton 				break;
26500304c731SJamie Gritton 			case 0:
26510304c731SJamie Gritton 				ii++;
26520304c731SJamie Gritton 				ij++;
26530304c731SJamie Gritton 				break;
26540304c731SJamie Gritton 			case 1:
26550304c731SJamie Gritton 				ij++;
26560304c731SJamie Gritton 				break;
26570304c731SJamie Gritton 			}
26580304c731SJamie Gritton 		}
26590304c731SJamie Gritton 		if (pr->pr_ip4s == 0) {
26607cbf7213SJamie Gritton 			pr->pr_flags |= PR_IP4_DISABLE;
26610304c731SJamie Gritton 			free(pr->pr_ip4, M_PRISON);
26620304c731SJamie Gritton 			pr->pr_ip4 = NULL;
26630304c731SJamie Gritton 		}
26640304c731SJamie Gritton 	}
26650304c731SJamie Gritton 	return (0);
26660304c731SJamie Gritton }
26670304c731SJamie Gritton 
26680304c731SJamie Gritton /*
2669413628a7SBjoern A. Zeeb  * Pass back primary IPv4 address of this jail.
2670413628a7SBjoern A. Zeeb  *
26710304c731SJamie Gritton  * If not restricted return success but do not alter the address.  Caller has
26720304c731SJamie Gritton  * to make sure to initialize it correctly (e.g. INADDR_ANY).
2673413628a7SBjoern A. Zeeb  *
2674b89e82ddSJamie Gritton  * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2675b89e82ddSJamie Gritton  * Address returned in NBO.
2676413628a7SBjoern A. Zeeb  */
2677413628a7SBjoern A. Zeeb int
26781cecba0fSBjoern A. Zeeb prison_get_ip4(struct ucred *cred, struct in_addr *ia)
2679413628a7SBjoern A. Zeeb {
2680b38ff370SJamie Gritton 	struct prison *pr;
2681413628a7SBjoern A. Zeeb 
2682413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2683413628a7SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
268475c13541SPoul-Henning Kamp 
2685b38ff370SJamie Gritton 	pr = cred->cr_prison;
26860304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4))
26870304c731SJamie Gritton 		return (0);
2688b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
26890304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4)) {
26900304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
26910304c731SJamie Gritton 		return (0);
26920304c731SJamie Gritton 	}
2693b38ff370SJamie Gritton 	if (pr->pr_ip4 == NULL) {
2694b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2695b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
2696b38ff370SJamie Gritton 	}
2697413628a7SBjoern A. Zeeb 
2698b38ff370SJamie Gritton 	ia->s_addr = pr->pr_ip4[0].s_addr;
2699b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2700413628a7SBjoern A. Zeeb 	return (0);
270175c13541SPoul-Henning Kamp }
2702413628a7SBjoern A. Zeeb 
2703413628a7SBjoern A. Zeeb /*
27040304c731SJamie Gritton  * Return true if pr1 and pr2 have the same IPv4 address restrictions.
27050304c731SJamie Gritton  */
27060304c731SJamie Gritton int
27070304c731SJamie Gritton prison_equal_ip4(struct prison *pr1, struct prison *pr2)
27080304c731SJamie Gritton {
27090304c731SJamie Gritton 
27100304c731SJamie Gritton 	if (pr1 == pr2)
27110304c731SJamie Gritton 		return (1);
27120304c731SJamie Gritton 
27130304c731SJamie Gritton 	/*
27140304c731SJamie Gritton 	 * jail_set maintains an exclusive hold on allprison_lock while it
27150304c731SJamie Gritton 	 * changes the IP addresses, so only a shared hold is needed.  This is
27160304c731SJamie Gritton 	 * easier than locking the two prisons which would require finding the
27170304c731SJamie Gritton 	 * proper locking order and end up needing allprison_lock anyway.
27180304c731SJamie Gritton 	 */
27190304c731SJamie Gritton 	sx_slock(&allprison_lock);
27200304c731SJamie Gritton 	while (pr1 != &prison0 && !(pr1->pr_flags & PR_IP4_USER))
27210304c731SJamie Gritton 		pr1 = pr1->pr_parent;
27220304c731SJamie Gritton 	while (pr2 != &prison0 && !(pr2->pr_flags & PR_IP4_USER))
27230304c731SJamie Gritton 		pr2 = pr2->pr_parent;
27240304c731SJamie Gritton 	sx_sunlock(&allprison_lock);
27250304c731SJamie Gritton 	return (pr1 == pr2);
27260304c731SJamie Gritton }
27270304c731SJamie Gritton 
27280304c731SJamie Gritton /*
2729413628a7SBjoern A. Zeeb  * Make sure our (source) address is set to something meaningful to this
2730413628a7SBjoern A. Zeeb  * jail.
2731413628a7SBjoern A. Zeeb  *
27320304c731SJamie Gritton  * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
27330304c731SJamie Gritton  * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
27340304c731SJamie Gritton  * doesn't allow IPv4.  Address passed in in NBO and returned in NBO.
2735413628a7SBjoern A. Zeeb  */
2736413628a7SBjoern A. Zeeb int
2737413628a7SBjoern A. Zeeb prison_local_ip4(struct ucred *cred, struct in_addr *ia)
2738413628a7SBjoern A. Zeeb {
2739b38ff370SJamie Gritton 	struct prison *pr;
2740413628a7SBjoern A. Zeeb 	struct in_addr ia0;
2741b38ff370SJamie Gritton 	int error;
2742413628a7SBjoern A. Zeeb 
2743413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2744413628a7SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2745413628a7SBjoern A. Zeeb 
2746b38ff370SJamie Gritton 	pr = cred->cr_prison;
27470304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4))
27480304c731SJamie Gritton 		return (0);
2749b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
27500304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4)) {
27510304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
27520304c731SJamie Gritton 		return (0);
27530304c731SJamie Gritton 	}
2754b38ff370SJamie Gritton 	if (pr->pr_ip4 == NULL) {
2755b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2756b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
2757b38ff370SJamie Gritton 	}
2758413628a7SBjoern A. Zeeb 
2759413628a7SBjoern A. Zeeb 	ia0.s_addr = ntohl(ia->s_addr);
2760413628a7SBjoern A. Zeeb 	if (ia0.s_addr == INADDR_LOOPBACK) {
2761b38ff370SJamie Gritton 		ia->s_addr = pr->pr_ip4[0].s_addr;
2762b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2763413628a7SBjoern A. Zeeb 		return (0);
2764413628a7SBjoern A. Zeeb 	}
2765413628a7SBjoern A. Zeeb 
2766b89e82ddSJamie Gritton 	if (ia0.s_addr == INADDR_ANY) {
2767413628a7SBjoern A. Zeeb 		/*
2768413628a7SBjoern A. Zeeb 		 * In case there is only 1 IPv4 address, bind directly.
2769413628a7SBjoern A. Zeeb 		 */
2770b38ff370SJamie Gritton 		if (pr->pr_ip4s == 1)
2771b38ff370SJamie Gritton 			ia->s_addr = pr->pr_ip4[0].s_addr;
2772b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2773413628a7SBjoern A. Zeeb 		return (0);
2774413628a7SBjoern A. Zeeb 	}
2775413628a7SBjoern A. Zeeb 
2776b38ff370SJamie Gritton 	error = _prison_check_ip4(pr, ia);
2777b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2778b38ff370SJamie Gritton 	return (error);
2779413628a7SBjoern A. Zeeb }
2780413628a7SBjoern A. Zeeb 
2781413628a7SBjoern A. Zeeb /*
2782413628a7SBjoern A. Zeeb  * Rewrite destination address in case we will connect to loopback address.
2783413628a7SBjoern A. Zeeb  *
2784b89e82ddSJamie Gritton  * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2785b89e82ddSJamie Gritton  * Address passed in in NBO and returned in NBO.
2786413628a7SBjoern A. Zeeb  */
2787413628a7SBjoern A. Zeeb int
2788413628a7SBjoern A. Zeeb prison_remote_ip4(struct ucred *cred, struct in_addr *ia)
2789413628a7SBjoern A. Zeeb {
2790b38ff370SJamie Gritton 	struct prison *pr;
2791413628a7SBjoern A. Zeeb 
2792413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2793413628a7SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2794413628a7SBjoern A. Zeeb 
2795b38ff370SJamie Gritton 	pr = cred->cr_prison;
27960304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4))
27970304c731SJamie Gritton 		return (0);
2798b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
27990304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4)) {
28000304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
28010304c731SJamie Gritton 		return (0);
28020304c731SJamie Gritton 	}
2803b38ff370SJamie Gritton 	if (pr->pr_ip4 == NULL) {
2804b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2805b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
2806b38ff370SJamie Gritton 	}
2807b89e82ddSJamie Gritton 
2808413628a7SBjoern A. Zeeb 	if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
2809b38ff370SJamie Gritton 		ia->s_addr = pr->pr_ip4[0].s_addr;
2810b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2811413628a7SBjoern A. Zeeb 		return (0);
2812413628a7SBjoern A. Zeeb 	}
2813413628a7SBjoern A. Zeeb 
2814413628a7SBjoern A. Zeeb 	/*
2815413628a7SBjoern A. Zeeb 	 * Return success because nothing had to be changed.
2816413628a7SBjoern A. Zeeb 	 */
2817b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2818413628a7SBjoern A. Zeeb 	return (0);
2819413628a7SBjoern A. Zeeb }
2820413628a7SBjoern A. Zeeb 
2821413628a7SBjoern A. Zeeb /*
2822b89e82ddSJamie Gritton  * Check if given address belongs to the jail referenced by cred/prison.
2823413628a7SBjoern A. Zeeb  *
28240304c731SJamie Gritton  * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
28250304c731SJamie Gritton  * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
28260304c731SJamie Gritton  * doesn't allow IPv4.  Address passed in in NBO.
2827413628a7SBjoern A. Zeeb  */
2828413628a7SBjoern A. Zeeb static int
2829413628a7SBjoern A. Zeeb _prison_check_ip4(struct prison *pr, struct in_addr *ia)
2830413628a7SBjoern A. Zeeb {
2831413628a7SBjoern A. Zeeb 	int i, a, z, d;
2832413628a7SBjoern A. Zeeb 
2833413628a7SBjoern A. Zeeb 	/*
2834413628a7SBjoern A. Zeeb 	 * Check the primary IP.
2835413628a7SBjoern A. Zeeb 	 */
2836413628a7SBjoern A. Zeeb 	if (pr->pr_ip4[0].s_addr == ia->s_addr)
2837b89e82ddSJamie Gritton 		return (0);
2838413628a7SBjoern A. Zeeb 
2839413628a7SBjoern A. Zeeb 	/*
2840413628a7SBjoern A. Zeeb 	 * All the other IPs are sorted so we can do a binary search.
2841413628a7SBjoern A. Zeeb 	 */
2842413628a7SBjoern A. Zeeb 	a = 0;
2843413628a7SBjoern A. Zeeb 	z = pr->pr_ip4s - 2;
2844413628a7SBjoern A. Zeeb 	while (a <= z) {
2845413628a7SBjoern A. Zeeb 		i = (a + z) / 2;
2846413628a7SBjoern A. Zeeb 		d = qcmp_v4(&pr->pr_ip4[i+1], ia);
2847413628a7SBjoern A. Zeeb 		if (d > 0)
2848413628a7SBjoern A. Zeeb 			z = i - 1;
2849413628a7SBjoern A. Zeeb 		else if (d < 0)
2850413628a7SBjoern A. Zeeb 			a = i + 1;
2851413628a7SBjoern A. Zeeb 		else
2852413628a7SBjoern A. Zeeb 			return (0);
285375c13541SPoul-Henning Kamp 	}
285475c13541SPoul-Henning Kamp 
2855b89e82ddSJamie Gritton 	return (EADDRNOTAVAIL);
2856b89e82ddSJamie Gritton }
2857b89e82ddSJamie Gritton 
285875c13541SPoul-Henning Kamp int
2859413628a7SBjoern A. Zeeb prison_check_ip4(struct ucred *cred, struct in_addr *ia)
2860413628a7SBjoern A. Zeeb {
2861b38ff370SJamie Gritton 	struct prison *pr;
2862b38ff370SJamie Gritton 	int error;
2863413628a7SBjoern A. Zeeb 
2864413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2865413628a7SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2866413628a7SBjoern A. Zeeb 
2867b38ff370SJamie Gritton 	pr = cred->cr_prison;
28680304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4))
28690304c731SJamie Gritton 		return (0);
2870b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
28710304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4)) {
28720304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
28730304c731SJamie Gritton 		return (0);
28740304c731SJamie Gritton 	}
2875b38ff370SJamie Gritton 	if (pr->pr_ip4 == NULL) {
2876b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2877b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
2878b38ff370SJamie Gritton 	}
2879413628a7SBjoern A. Zeeb 
2880b38ff370SJamie Gritton 	error = _prison_check_ip4(pr, ia);
2881b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2882b38ff370SJamie Gritton 	return (error);
2883413628a7SBjoern A. Zeeb }
2884413628a7SBjoern A. Zeeb #endif
2885413628a7SBjoern A. Zeeb 
2886413628a7SBjoern A. Zeeb #ifdef INET6
28870304c731SJamie Gritton static int
28880304c731SJamie Gritton prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
28890304c731SJamie Gritton {
28900304c731SJamie Gritton 	int ii, ij, used;
28910304c731SJamie Gritton 	struct prison *ppr;
28920304c731SJamie Gritton 
28930304c731SJamie Gritton 	ppr = pr->pr_parent;
28940304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6_USER)) {
28950304c731SJamie Gritton 		/* This has no user settings, so just copy the parent's list. */
28960304c731SJamie Gritton 		if (pr->pr_ip6s < ppr->pr_ip6s) {
28970304c731SJamie Gritton 			/*
28980304c731SJamie Gritton 			 * There's no room for the parent's list.  Use the
28990304c731SJamie Gritton 			 * new list buffer, which is assumed to be big enough
29000304c731SJamie Gritton 			 * (if it was passed).  If there's no buffer, try to
29010304c731SJamie Gritton 			 * allocate one.
29020304c731SJamie Gritton 			 */
29030304c731SJamie Gritton 			used = 1;
29040304c731SJamie Gritton 			if (newip6 == NULL) {
29050304c731SJamie Gritton 				newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
29060304c731SJamie Gritton 				    M_PRISON, M_NOWAIT);
29070304c731SJamie Gritton 				if (newip6 != NULL)
29080304c731SJamie Gritton 					used = 0;
29090304c731SJamie Gritton 			}
29100304c731SJamie Gritton 			if (newip6 != NULL) {
29110304c731SJamie Gritton 				bcopy(ppr->pr_ip6, newip6,
29120304c731SJamie Gritton 				    ppr->pr_ip6s * sizeof(*newip6));
29130304c731SJamie Gritton 				free(pr->pr_ip6, M_PRISON);
29140304c731SJamie Gritton 				pr->pr_ip6 = newip6;
29150304c731SJamie Gritton 				pr->pr_ip6s = ppr->pr_ip6s;
29160304c731SJamie Gritton 				pr->pr_flags |= PR_IP6;
29170304c731SJamie Gritton 			}
29180304c731SJamie Gritton 			return (used);
29190304c731SJamie Gritton 		}
29200304c731SJamie Gritton 		pr->pr_ip6s = ppr->pr_ip6s;
29210304c731SJamie Gritton 		if (pr->pr_ip6s > 0)
29220304c731SJamie Gritton 			bcopy(ppr->pr_ip6, pr->pr_ip6,
29230304c731SJamie Gritton 			    pr->pr_ip6s * sizeof(*newip6));
29240304c731SJamie Gritton 		else if (pr->pr_ip6 != NULL) {
29250304c731SJamie Gritton 			free(pr->pr_ip6, M_PRISON);
29260304c731SJamie Gritton 			pr->pr_ip6 = NULL;
29270304c731SJamie Gritton 		}
29280304c731SJamie Gritton 		pr->pr_flags =
29290304c731SJamie Gritton 			(pr->pr_flags & ~PR_IP6) | (ppr->pr_flags & PR_IP6);
29300304c731SJamie Gritton 	} else if (pr->pr_ip6s > 0 && (ppr->pr_flags & PR_IP6)) {
29310304c731SJamie Gritton 		/* Remove addresses that aren't in the parent. */
29320304c731SJamie Gritton 		for (ij = 0; ij < ppr->pr_ip6s; ij++)
29330304c731SJamie Gritton 			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
29340304c731SJamie Gritton 			    &ppr->pr_ip6[ij]))
29350304c731SJamie Gritton 				break;
29360304c731SJamie Gritton 		if (ij < ppr->pr_ip6s)
29370304c731SJamie Gritton 			ii = 1;
29380304c731SJamie Gritton 		else {
29390304c731SJamie Gritton 			bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
29400304c731SJamie Gritton 			    --pr->pr_ip6s * sizeof(*pr->pr_ip6));
29410304c731SJamie Gritton 			ii = 0;
29420304c731SJamie Gritton 		}
29430304c731SJamie Gritton 		for (ij = 1; ii < pr->pr_ip6s; ) {
29440304c731SJamie Gritton 			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
29450304c731SJamie Gritton 			    &ppr->pr_ip6[0])) {
29460304c731SJamie Gritton 				ii++;
29470304c731SJamie Gritton 				continue;
29480304c731SJamie Gritton 			}
29490304c731SJamie Gritton 			switch (ij >= ppr->pr_ip4s ? -1 :
29500304c731SJamie Gritton 				qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
29510304c731SJamie Gritton 			case -1:
29520304c731SJamie Gritton 				bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
29530304c731SJamie Gritton 				    (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
29540304c731SJamie Gritton 				break;
29550304c731SJamie Gritton 			case 0:
29560304c731SJamie Gritton 				ii++;
29570304c731SJamie Gritton 				ij++;
29580304c731SJamie Gritton 				break;
29590304c731SJamie Gritton 			case 1:
29600304c731SJamie Gritton 				ij++;
29610304c731SJamie Gritton 				break;
29620304c731SJamie Gritton 			}
29630304c731SJamie Gritton 		}
29640304c731SJamie Gritton 		if (pr->pr_ip6s == 0) {
29657cbf7213SJamie Gritton 			pr->pr_flags |= PR_IP6_DISABLE;
29660304c731SJamie Gritton 			free(pr->pr_ip6, M_PRISON);
29670304c731SJamie Gritton 			pr->pr_ip6 = NULL;
29680304c731SJamie Gritton 		}
29690304c731SJamie Gritton 	}
29700304c731SJamie Gritton 	return 0;
29710304c731SJamie Gritton }
29720304c731SJamie Gritton 
2973413628a7SBjoern A. Zeeb /*
2974413628a7SBjoern A. Zeeb  * Pass back primary IPv6 address for this jail.
2975413628a7SBjoern A. Zeeb  *
29760304c731SJamie Gritton  * If not restricted return success but do not alter the address.  Caller has
29770304c731SJamie Gritton  * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
2978413628a7SBjoern A. Zeeb  *
2979b89e82ddSJamie Gritton  * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
2980413628a7SBjoern A. Zeeb  */
2981413628a7SBjoern A. Zeeb int
29821cecba0fSBjoern A. Zeeb prison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
2983413628a7SBjoern A. Zeeb {
2984b38ff370SJamie Gritton 	struct prison *pr;
2985413628a7SBjoern A. Zeeb 
2986413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2987413628a7SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
2988413628a7SBjoern A. Zeeb 
2989b38ff370SJamie Gritton 	pr = cred->cr_prison;
29900304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6))
29910304c731SJamie Gritton 		return (0);
2992b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
29930304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6)) {
29940304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
29950304c731SJamie Gritton 		return (0);
29960304c731SJamie Gritton 	}
2997b38ff370SJamie Gritton 	if (pr->pr_ip6 == NULL) {
2998b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2999b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
3000b38ff370SJamie Gritton 	}
3001b89e82ddSJamie Gritton 
3002b38ff370SJamie Gritton 	bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3003b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3004413628a7SBjoern A. Zeeb 	return (0);
3005413628a7SBjoern A. Zeeb }
3006413628a7SBjoern A. Zeeb 
3007413628a7SBjoern A. Zeeb /*
30080304c731SJamie Gritton  * Return true if pr1 and pr2 have the same IPv6 address restrictions.
30090304c731SJamie Gritton  */
30100304c731SJamie Gritton int
30110304c731SJamie Gritton prison_equal_ip6(struct prison *pr1, struct prison *pr2)
30120304c731SJamie Gritton {
30130304c731SJamie Gritton 
30140304c731SJamie Gritton 	if (pr1 == pr2)
30150304c731SJamie Gritton 		return (1);
30160304c731SJamie Gritton 
30170304c731SJamie Gritton 	sx_slock(&allprison_lock);
30180304c731SJamie Gritton 	while (pr1 != &prison0 && !(pr1->pr_flags & PR_IP6_USER))
30190304c731SJamie Gritton 		pr1 = pr1->pr_parent;
30200304c731SJamie Gritton 	while (pr2 != &prison0 && !(pr2->pr_flags & PR_IP6_USER))
30210304c731SJamie Gritton 		pr2 = pr2->pr_parent;
30220304c731SJamie Gritton 	sx_sunlock(&allprison_lock);
30230304c731SJamie Gritton 	return (pr1 == pr2);
30240304c731SJamie Gritton }
30250304c731SJamie Gritton 
30260304c731SJamie Gritton /*
3027413628a7SBjoern A. Zeeb  * Make sure our (source) address is set to something meaningful to this jail.
3028413628a7SBjoern A. Zeeb  *
3029413628a7SBjoern A. Zeeb  * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
3030413628a7SBjoern A. Zeeb  * when needed while binding.
3031413628a7SBjoern A. Zeeb  *
30320304c731SJamie Gritton  * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
30330304c731SJamie Gritton  * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
30340304c731SJamie Gritton  * doesn't allow IPv6.
3035413628a7SBjoern A. Zeeb  */
3036413628a7SBjoern A. Zeeb int
3037413628a7SBjoern A. Zeeb prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
3038413628a7SBjoern A. Zeeb {
3039b38ff370SJamie Gritton 	struct prison *pr;
3040b38ff370SJamie Gritton 	int error;
3041413628a7SBjoern A. Zeeb 
3042413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3043413628a7SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3044413628a7SBjoern A. Zeeb 
3045b38ff370SJamie Gritton 	pr = cred->cr_prison;
30460304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6))
30470304c731SJamie Gritton 		return (0);
3048b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
30490304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6)) {
30500304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
30510304c731SJamie Gritton 		return (0);
30520304c731SJamie Gritton 	}
3053b38ff370SJamie Gritton 	if (pr->pr_ip6 == NULL) {
3054b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3055b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
3056b38ff370SJamie Gritton 	}
3057b89e82ddSJamie Gritton 
3058413628a7SBjoern A. Zeeb 	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3059b38ff370SJamie Gritton 		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3060b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3061413628a7SBjoern A. Zeeb 		return (0);
3062413628a7SBjoern A. Zeeb 	}
3063413628a7SBjoern A. Zeeb 
3064b89e82ddSJamie Gritton 	if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3065413628a7SBjoern A. Zeeb 		/*
3066b89e82ddSJamie Gritton 		 * In case there is only 1 IPv6 address, and v6only is true,
3067b89e82ddSJamie Gritton 		 * then bind directly.
3068413628a7SBjoern A. Zeeb 		 */
3069b38ff370SJamie Gritton 		if (v6only != 0 && pr->pr_ip6s == 1)
3070b38ff370SJamie Gritton 			bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3071b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3072413628a7SBjoern A. Zeeb 		return (0);
3073413628a7SBjoern A. Zeeb 	}
3074b89e82ddSJamie Gritton 
3075b38ff370SJamie Gritton 	error = _prison_check_ip6(pr, ia6);
3076b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3077b38ff370SJamie Gritton 	return (error);
3078413628a7SBjoern A. Zeeb }
3079413628a7SBjoern A. Zeeb 
3080413628a7SBjoern A. Zeeb /*
3081413628a7SBjoern A. Zeeb  * Rewrite destination address in case we will connect to loopback address.
3082413628a7SBjoern A. Zeeb  *
3083b89e82ddSJamie Gritton  * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3084413628a7SBjoern A. Zeeb  */
3085413628a7SBjoern A. Zeeb int
3086413628a7SBjoern A. Zeeb prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3087413628a7SBjoern A. Zeeb {
3088b38ff370SJamie Gritton 	struct prison *pr;
3089413628a7SBjoern A. Zeeb 
3090413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3091413628a7SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3092413628a7SBjoern A. Zeeb 
3093b38ff370SJamie Gritton 	pr = cred->cr_prison;
30940304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6))
30950304c731SJamie Gritton 		return (0);
3096b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
30970304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6)) {
30980304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
30990304c731SJamie Gritton 		return (0);
31000304c731SJamie Gritton 	}
3101b38ff370SJamie Gritton 	if (pr->pr_ip6 == NULL) {
3102b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3103b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
3104b38ff370SJamie Gritton 	}
3105b89e82ddSJamie Gritton 
3106413628a7SBjoern A. Zeeb 	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3107b38ff370SJamie Gritton 		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3108b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3109413628a7SBjoern A. Zeeb 		return (0);
3110413628a7SBjoern A. Zeeb 	}
3111413628a7SBjoern A. Zeeb 
3112413628a7SBjoern A. Zeeb 	/*
3113413628a7SBjoern A. Zeeb 	 * Return success because nothing had to be changed.
3114413628a7SBjoern A. Zeeb 	 */
3115b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3116413628a7SBjoern A. Zeeb 	return (0);
3117413628a7SBjoern A. Zeeb }
3118413628a7SBjoern A. Zeeb 
3119413628a7SBjoern A. Zeeb /*
3120b89e82ddSJamie Gritton  * Check if given address belongs to the jail referenced by cred/prison.
3121413628a7SBjoern A. Zeeb  *
31220304c731SJamie Gritton  * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
31230304c731SJamie Gritton  * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
31240304c731SJamie Gritton  * doesn't allow IPv6.
3125413628a7SBjoern A. Zeeb  */
3126413628a7SBjoern A. Zeeb static int
3127413628a7SBjoern A. Zeeb _prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
3128413628a7SBjoern A. Zeeb {
3129413628a7SBjoern A. Zeeb 	int i, a, z, d;
3130413628a7SBjoern A. Zeeb 
3131413628a7SBjoern A. Zeeb 	/*
3132413628a7SBjoern A. Zeeb 	 * Check the primary IP.
3133413628a7SBjoern A. Zeeb 	 */
3134413628a7SBjoern A. Zeeb 	if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3135b89e82ddSJamie Gritton 		return (0);
3136413628a7SBjoern A. Zeeb 
3137413628a7SBjoern A. Zeeb 	/*
3138413628a7SBjoern A. Zeeb 	 * All the other IPs are sorted so we can do a binary search.
3139413628a7SBjoern A. Zeeb 	 */
3140413628a7SBjoern A. Zeeb 	a = 0;
3141413628a7SBjoern A. Zeeb 	z = pr->pr_ip6s - 2;
3142413628a7SBjoern A. Zeeb 	while (a <= z) {
3143413628a7SBjoern A. Zeeb 		i = (a + z) / 2;
3144413628a7SBjoern A. Zeeb 		d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3145413628a7SBjoern A. Zeeb 		if (d > 0)
3146413628a7SBjoern A. Zeeb 			z = i - 1;
3147413628a7SBjoern A. Zeeb 		else if (d < 0)
3148413628a7SBjoern A. Zeeb 			a = i + 1;
3149413628a7SBjoern A. Zeeb 		else
3150413628a7SBjoern A. Zeeb 			return (0);
3151413628a7SBjoern A. Zeeb 	}
3152413628a7SBjoern A. Zeeb 
3153b89e82ddSJamie Gritton 	return (EADDRNOTAVAIL);
3154b89e82ddSJamie Gritton }
3155b89e82ddSJamie Gritton 
3156413628a7SBjoern A. Zeeb int
3157413628a7SBjoern A. Zeeb prison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3158413628a7SBjoern A. Zeeb {
3159b38ff370SJamie Gritton 	struct prison *pr;
3160b38ff370SJamie Gritton 	int error;
3161413628a7SBjoern A. Zeeb 
3162413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3163413628a7SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3164413628a7SBjoern A. Zeeb 
3165b38ff370SJamie Gritton 	pr = cred->cr_prison;
31660304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6))
31670304c731SJamie Gritton 		return (0);
3168b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
31690304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6)) {
31700304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
31710304c731SJamie Gritton 		return (0);
31720304c731SJamie Gritton 	}
3173b38ff370SJamie Gritton 	if (pr->pr_ip6 == NULL) {
3174b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3175b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
3176b38ff370SJamie Gritton 	}
3177413628a7SBjoern A. Zeeb 
3178b38ff370SJamie Gritton 	error = _prison_check_ip6(pr, ia6);
3179b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3180b38ff370SJamie Gritton 	return (error);
3181413628a7SBjoern A. Zeeb }
3182413628a7SBjoern A. Zeeb #endif
3183413628a7SBjoern A. Zeeb 
3184413628a7SBjoern A. Zeeb /*
3185ca04ba64SJamie Gritton  * Check if a jail supports the given address family.
3186ca04ba64SJamie Gritton  *
3187ca04ba64SJamie Gritton  * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3188ca04ba64SJamie Gritton  * if not.
3189ca04ba64SJamie Gritton  */
3190ca04ba64SJamie Gritton int
3191ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af)
3192ca04ba64SJamie Gritton {
31930304c731SJamie Gritton 	struct prison *pr;
3194ca04ba64SJamie Gritton 	int error;
3195ca04ba64SJamie Gritton 
3196ca04ba64SJamie Gritton 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3197ca04ba64SJamie Gritton 
31980304c731SJamie Gritton 	pr = cred->cr_prison;
3199499650a0SJamie Gritton #ifdef VIMAGE
32006bb79563SJamie Gritton 	/* Prisons with their own network stack are not limited. */
32016bb79563SJamie Gritton 	if (pr->pr_flags & PR_VNET)
32026bb79563SJamie Gritton 		return (0);
3203499650a0SJamie Gritton #endif
32046bb79563SJamie Gritton 
3205ca04ba64SJamie Gritton 	error = 0;
3206ca04ba64SJamie Gritton 	switch (af)
3207ca04ba64SJamie Gritton 	{
3208ca04ba64SJamie Gritton #ifdef INET
3209ca04ba64SJamie Gritton 	case AF_INET:
32100304c731SJamie Gritton 		if (pr->pr_flags & PR_IP4)
32110304c731SJamie Gritton 		{
32120304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
32130304c731SJamie Gritton 			if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3214ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
32150304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
32160304c731SJamie Gritton 		}
3217ca04ba64SJamie Gritton 		break;
3218ca04ba64SJamie Gritton #endif
3219ca04ba64SJamie Gritton #ifdef INET6
3220ca04ba64SJamie Gritton 	case AF_INET6:
32210304c731SJamie Gritton 		if (pr->pr_flags & PR_IP6)
32220304c731SJamie Gritton 		{
32230304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
32240304c731SJamie Gritton 			if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3225ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
32260304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
32270304c731SJamie Gritton 		}
3228ca04ba64SJamie Gritton 		break;
3229ca04ba64SJamie Gritton #endif
3230ca04ba64SJamie Gritton 	case AF_LOCAL:
3231ca04ba64SJamie Gritton 	case AF_ROUTE:
3232ca04ba64SJamie Gritton 		break;
3233ca04ba64SJamie Gritton 	default:
32340304c731SJamie Gritton 		if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3235ca04ba64SJamie Gritton 			error = EAFNOSUPPORT;
3236ca04ba64SJamie Gritton 	}
3237ca04ba64SJamie Gritton 	return (error);
3238ca04ba64SJamie Gritton }
3239ca04ba64SJamie Gritton 
3240ca04ba64SJamie Gritton /*
3241413628a7SBjoern A. Zeeb  * Check if given address belongs to the jail referenced by cred (wrapper to
3242413628a7SBjoern A. Zeeb  * prison_check_ip[46]).
3243413628a7SBjoern A. Zeeb  *
32440304c731SJamie Gritton  * Returns 0 if jail doesn't restrict the address family or if address belongs
32450304c731SJamie Gritton  * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
32460304c731SJamie Gritton  * the jail doesn't allow the address family.  IPv4 Address passed in in NBO.
3247413628a7SBjoern A. Zeeb  */
3248413628a7SBjoern A. Zeeb int
324991421ba2SRobert Watson prison_if(struct ucred *cred, struct sockaddr *sa)
325075c13541SPoul-Henning Kamp {
3251413628a7SBjoern A. Zeeb #ifdef INET
32529ddb7954SMike Barcroft 	struct sockaddr_in *sai;
3253413628a7SBjoern A. Zeeb #endif
3254413628a7SBjoern A. Zeeb #ifdef INET6
3255413628a7SBjoern A. Zeeb 	struct sockaddr_in6 *sai6;
3256413628a7SBjoern A. Zeeb #endif
3257b89e82ddSJamie Gritton 	int error;
325875c13541SPoul-Henning Kamp 
3259413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3260413628a7SBjoern A. Zeeb 	KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3261413628a7SBjoern A. Zeeb 
3262b89e82ddSJamie Gritton 	error = 0;
3263413628a7SBjoern A. Zeeb 	switch (sa->sa_family)
3264413628a7SBjoern A. Zeeb 	{
3265413628a7SBjoern A. Zeeb #ifdef INET
3266413628a7SBjoern A. Zeeb 	case AF_INET:
32679ddb7954SMike Barcroft 		sai = (struct sockaddr_in *)sa;
3268b89e82ddSJamie Gritton 		error = prison_check_ip4(cred, &sai->sin_addr);
3269413628a7SBjoern A. Zeeb 		break;
3270413628a7SBjoern A. Zeeb #endif
3271413628a7SBjoern A. Zeeb #ifdef INET6
3272413628a7SBjoern A. Zeeb 	case AF_INET6:
3273413628a7SBjoern A. Zeeb 		sai6 = (struct sockaddr_in6 *)sa;
3274b89e82ddSJamie Gritton 		error = prison_check_ip6(cred, &sai6->sin6_addr);
3275413628a7SBjoern A. Zeeb 		break;
3276413628a7SBjoern A. Zeeb #endif
3277413628a7SBjoern A. Zeeb 	default:
32780304c731SJamie Gritton 		if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3279b89e82ddSJamie Gritton 			error = EAFNOSUPPORT;
3280413628a7SBjoern A. Zeeb 	}
3281b89e82ddSJamie Gritton 	return (error);
328275c13541SPoul-Henning Kamp }
328391421ba2SRobert Watson 
328491421ba2SRobert Watson /*
328591421ba2SRobert Watson  * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
328691421ba2SRobert Watson  */
328791421ba2SRobert Watson int
32889ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2)
328991421ba2SRobert Watson {
329091421ba2SRobert Watson 
32910304c731SJamie Gritton 	return ((cred1->cr_prison == cred2->cr_prison ||
32920304c731SJamie Gritton 	    prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
32930304c731SJamie Gritton }
329491421ba2SRobert Watson 
32950304c731SJamie Gritton /*
32960304c731SJamie Gritton  * Return 1 if p2 is a child of p1, otherwise 0.
32970304c731SJamie Gritton  */
32980304c731SJamie Gritton int
32990304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2)
33000304c731SJamie Gritton {
33010304c731SJamie Gritton 
33020304c731SJamie Gritton 	for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
33030304c731SJamie Gritton 		if (pr1 == pr2)
33040304c731SJamie Gritton 			return (1);
330591421ba2SRobert Watson 	return (0);
330691421ba2SRobert Watson }
330791421ba2SRobert Watson 
330891421ba2SRobert Watson /*
330991421ba2SRobert Watson  * Return 1 if the passed credential is in a jail, otherwise 0.
331091421ba2SRobert Watson  */
331191421ba2SRobert Watson int
33129ddb7954SMike Barcroft jailed(struct ucred *cred)
331391421ba2SRobert Watson {
331491421ba2SRobert Watson 
33150304c731SJamie Gritton 	return (cred->cr_prison != &prison0);
331691421ba2SRobert Watson }
33179484d0c0SRobert Drehmel 
33189484d0c0SRobert Drehmel /*
33197455b100SJamie Gritton  * Return the correct hostname (domainname, et al) for the passed credential.
33209484d0c0SRobert Drehmel  */
3321ad1ff099SRobert Drehmel void
33229ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size)
33239484d0c0SRobert Drehmel {
332476ca6f88SJamie Gritton 	struct prison *pr;
33259484d0c0SRobert Drehmel 
33267455b100SJamie Gritton 	/*
33277455b100SJamie Gritton 	 * A NULL credential can be used to shortcut to the physical
33287455b100SJamie Gritton 	 * system's hostname.
33297455b100SJamie Gritton 	 */
333076ca6f88SJamie Gritton 	pr = (cred != NULL) ? cred->cr_prison : &prison0;
333176ca6f88SJamie Gritton 	mtx_lock(&pr->pr_mtx);
3332c1f19219SJamie Gritton 	strlcpy(buf, pr->pr_hostname, size);
333376ca6f88SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
33349484d0c0SRobert Drehmel }
3335fd7a8150SMike Barcroft 
33367455b100SJamie Gritton void
33377455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size)
33387455b100SJamie Gritton {
33397455b100SJamie Gritton 
33407455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
3341c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_domainname, size);
33427455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
33437455b100SJamie Gritton }
33447455b100SJamie Gritton 
33457455b100SJamie Gritton void
33467455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size)
33477455b100SJamie Gritton {
33487455b100SJamie Gritton 
33497455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
3350c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
33517455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
33527455b100SJamie Gritton }
33537455b100SJamie Gritton 
33547455b100SJamie Gritton void
33557455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid)
33567455b100SJamie Gritton {
33577455b100SJamie Gritton 
33587455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
33597455b100SJamie Gritton 	*hostid = cred->cr_prison->pr_hostid;
33607455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
33617455b100SJamie Gritton }
33627455b100SJamie Gritton 
3363f08df373SRobert Watson /*
3364820a0de9SPawel Jakub Dawidek  * Determine whether the subject represented by cred can "see"
3365820a0de9SPawel Jakub Dawidek  * status of a mount point.
3366820a0de9SPawel Jakub Dawidek  * Returns: 0 for permitted, ENOENT otherwise.
3367820a0de9SPawel Jakub Dawidek  * XXX: This function should be called cr_canseemount() and should be
3368820a0de9SPawel Jakub Dawidek  *      placed in kern_prot.c.
3369f08df373SRobert Watson  */
3370f08df373SRobert Watson int
3371820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp)
3372f08df373SRobert Watson {
3373820a0de9SPawel Jakub Dawidek 	struct prison *pr;
3374820a0de9SPawel Jakub Dawidek 	struct statfs *sp;
3375820a0de9SPawel Jakub Dawidek 	size_t len;
3376f08df373SRobert Watson 
3377820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
33780304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
33790304c731SJamie Gritton 		return (0);
3380820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp)
3381820a0de9SPawel Jakub Dawidek 		return (0);
33820304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 2)
3383820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3384820a0de9SPawel Jakub Dawidek 	/*
3385820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
3386820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
3387820a0de9SPawel Jakub Dawidek 	 * This is ugly check, but this is the only situation when jail's
3388820a0de9SPawel Jakub Dawidek 	 * directory ends with '/'.
3389820a0de9SPawel Jakub Dawidek 	 */
3390820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
3391820a0de9SPawel Jakub Dawidek 		return (0);
3392820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
3393820a0de9SPawel Jakub Dawidek 	sp = &mp->mnt_stat;
3394820a0de9SPawel Jakub Dawidek 	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3395820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3396820a0de9SPawel Jakub Dawidek 	/*
3397820a0de9SPawel Jakub Dawidek 	 * Be sure that we don't have situation where jail's root directory
3398820a0de9SPawel Jakub Dawidek 	 * is "/some/path" and mount point is "/some/pathpath".
3399820a0de9SPawel Jakub Dawidek 	 */
3400820a0de9SPawel Jakub Dawidek 	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3401820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3402f08df373SRobert Watson 	return (0);
3403f08df373SRobert Watson }
3404820a0de9SPawel Jakub Dawidek 
3405820a0de9SPawel Jakub Dawidek void
3406820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3407820a0de9SPawel Jakub Dawidek {
3408820a0de9SPawel Jakub Dawidek 	char jpath[MAXPATHLEN];
3409820a0de9SPawel Jakub Dawidek 	struct prison *pr;
3410820a0de9SPawel Jakub Dawidek 	size_t len;
3411820a0de9SPawel Jakub Dawidek 
3412820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
34130304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
34140304c731SJamie Gritton 		return;
3415820a0de9SPawel Jakub Dawidek 	if (prison_canseemount(cred, mp) != 0) {
3416820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3417820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, "[restricted]",
3418820a0de9SPawel Jakub Dawidek 		    sizeof(sp->f_mntonname));
3419820a0de9SPawel Jakub Dawidek 		return;
3420820a0de9SPawel Jakub Dawidek 	}
3421820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp) {
3422820a0de9SPawel Jakub Dawidek 		/*
3423820a0de9SPawel Jakub Dawidek 		 * Clear current buffer data, so we are sure nothing from
3424820a0de9SPawel Jakub Dawidek 		 * the valid path left there.
3425820a0de9SPawel Jakub Dawidek 		 */
3426820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3427820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3428820a0de9SPawel Jakub Dawidek 		return;
3429820a0de9SPawel Jakub Dawidek 	}
3430820a0de9SPawel Jakub Dawidek 	/*
3431820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
3432820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
3433820a0de9SPawel Jakub Dawidek 	 */
3434820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
3435820a0de9SPawel Jakub Dawidek 		return;
3436820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
3437820a0de9SPawel Jakub Dawidek 	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3438820a0de9SPawel Jakub Dawidek 	/*
3439820a0de9SPawel Jakub Dawidek 	 * Clear current buffer data, so we are sure nothing from
3440820a0de9SPawel Jakub Dawidek 	 * the valid path left there.
3441820a0de9SPawel Jakub Dawidek 	 */
3442820a0de9SPawel Jakub Dawidek 	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3443820a0de9SPawel Jakub Dawidek 	if (*jpath == '\0') {
3444820a0de9SPawel Jakub Dawidek 		/* Should never happen. */
3445820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3446820a0de9SPawel Jakub Dawidek 	} else {
3447820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3448820a0de9SPawel Jakub Dawidek 	}
3449f08df373SRobert Watson }
3450f08df373SRobert Watson 
3451800c9408SRobert Watson /*
3452800c9408SRobert Watson  * Check with permission for a specific privilege is granted within jail.  We
3453800c9408SRobert Watson  * have a specific list of accepted privileges; the rest are denied.
3454800c9408SRobert Watson  */
3455800c9408SRobert Watson int
3456800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv)
3457800c9408SRobert Watson {
3458800c9408SRobert Watson 
3459800c9408SRobert Watson 	if (!jailed(cred))
3460800c9408SRobert Watson 		return (0);
3461800c9408SRobert Watson 
34626bb79563SJamie Gritton #ifdef VIMAGE
34636bb79563SJamie Gritton 	/*
34646bb79563SJamie Gritton 	 * Privileges specific to prisons with a virtual network stack.
34656bb79563SJamie Gritton 	 * There might be a duplicate entry here in case the privilege
34666bb79563SJamie Gritton 	 * is only granted conditionally in the legacy jail case.
34676bb79563SJamie Gritton 	 */
34686bb79563SJamie Gritton 	switch (priv) {
34696bb79563SJamie Gritton #ifdef notyet
34706bb79563SJamie Gritton 		/*
34716bb79563SJamie Gritton 		 * NFS-specific privileges.
34726bb79563SJamie Gritton 		 */
34736bb79563SJamie Gritton 	case PRIV_NFS_DAEMON:
34746bb79563SJamie Gritton 	case PRIV_NFS_LOCKD:
34756bb79563SJamie Gritton #endif
34766bb79563SJamie Gritton 		/*
34776bb79563SJamie Gritton 		 * Network stack privileges.
34786bb79563SJamie Gritton 		 */
34796bb79563SJamie Gritton 	case PRIV_NET_BRIDGE:
34806bb79563SJamie Gritton 	case PRIV_NET_GRE:
34816bb79563SJamie Gritton 	case PRIV_NET_BPF:
34826bb79563SJamie Gritton 	case PRIV_NET_RAW:		/* Dup, cond. in legacy jail case. */
34836bb79563SJamie Gritton 	case PRIV_NET_ROUTE:
34846bb79563SJamie Gritton 	case PRIV_NET_TAP:
34856bb79563SJamie Gritton 	case PRIV_NET_SETIFMTU:
34866bb79563SJamie Gritton 	case PRIV_NET_SETIFFLAGS:
34876bb79563SJamie Gritton 	case PRIV_NET_SETIFCAP:
34886bb79563SJamie Gritton 	case PRIV_NET_SETIFNAME	:
34896bb79563SJamie Gritton 	case PRIV_NET_SETIFMETRIC:
34906bb79563SJamie Gritton 	case PRIV_NET_SETIFPHYS:
34916bb79563SJamie Gritton 	case PRIV_NET_SETIFMAC:
34926bb79563SJamie Gritton 	case PRIV_NET_ADDMULTI:
34936bb79563SJamie Gritton 	case PRIV_NET_DELMULTI:
34946bb79563SJamie Gritton 	case PRIV_NET_HWIOCTL:
34956bb79563SJamie Gritton 	case PRIV_NET_SETLLADDR:
34966bb79563SJamie Gritton 	case PRIV_NET_ADDIFGROUP:
34976bb79563SJamie Gritton 	case PRIV_NET_DELIFGROUP:
34986bb79563SJamie Gritton 	case PRIV_NET_IFCREATE:
34996bb79563SJamie Gritton 	case PRIV_NET_IFDESTROY:
35006bb79563SJamie Gritton 	case PRIV_NET_ADDIFADDR:
35016bb79563SJamie Gritton 	case PRIV_NET_DELIFADDR:
35026bb79563SJamie Gritton 	case PRIV_NET_LAGG:
35036bb79563SJamie Gritton 	case PRIV_NET_GIF:
35046bb79563SJamie Gritton 	case PRIV_NET_SETIFVNET:
35056bb79563SJamie Gritton 
35066bb79563SJamie Gritton 		/*
35076bb79563SJamie Gritton 		 * 802.11-related privileges.
35086bb79563SJamie Gritton 		 */
35096bb79563SJamie Gritton 	case PRIV_NET80211_GETKEY:
35106bb79563SJamie Gritton #ifdef notyet
35116bb79563SJamie Gritton 	case PRIV_NET80211_MANAGE:		/* XXX-BZ discuss with sam@ */
35126bb79563SJamie Gritton #endif
35136bb79563SJamie Gritton 
35146bb79563SJamie Gritton #ifdef notyet
35156bb79563SJamie Gritton 		/*
35166bb79563SJamie Gritton 		 * AppleTalk privileges.
35176bb79563SJamie Gritton 		 */
35186bb79563SJamie Gritton 	case PRIV_NETATALK_RESERVEDPORT:
35196bb79563SJamie Gritton 
35206bb79563SJamie Gritton 		/*
35216bb79563SJamie Gritton 		 * ATM privileges.
35226bb79563SJamie Gritton 		 */
35236bb79563SJamie Gritton 	case PRIV_NETATM_CFG:
35246bb79563SJamie Gritton 	case PRIV_NETATM_ADD:
35256bb79563SJamie Gritton 	case PRIV_NETATM_DEL:
35266bb79563SJamie Gritton 	case PRIV_NETATM_SET:
35276bb79563SJamie Gritton 
35286bb79563SJamie Gritton 		/*
35296bb79563SJamie Gritton 		 * Bluetooth privileges.
35306bb79563SJamie Gritton 		 */
35316bb79563SJamie Gritton 	case PRIV_NETBLUETOOTH_RAW:
35326bb79563SJamie Gritton #endif
35336bb79563SJamie Gritton 
35346bb79563SJamie Gritton 		/*
35356bb79563SJamie Gritton 		 * Netgraph and netgraph module privileges.
35366bb79563SJamie Gritton 		 */
35376bb79563SJamie Gritton 	case PRIV_NETGRAPH_CONTROL:
35386bb79563SJamie Gritton #ifdef notyet
35396bb79563SJamie Gritton 	case PRIV_NETGRAPH_TTY:
35406bb79563SJamie Gritton #endif
35416bb79563SJamie Gritton 
35426bb79563SJamie Gritton 		/*
35436bb79563SJamie Gritton 		 * IPv4 and IPv6 privileges.
35446bb79563SJamie Gritton 		 */
35456bb79563SJamie Gritton 	case PRIV_NETINET_IPFW:
35466bb79563SJamie Gritton 	case PRIV_NETINET_DIVERT:
35476bb79563SJamie Gritton 	case PRIV_NETINET_PF:
35486bb79563SJamie Gritton 	case PRIV_NETINET_DUMMYNET:
35496bb79563SJamie Gritton 	case PRIV_NETINET_CARP:
35506bb79563SJamie Gritton 	case PRIV_NETINET_MROUTE:
35516bb79563SJamie Gritton 	case PRIV_NETINET_RAW:
35526bb79563SJamie Gritton 	case PRIV_NETINET_ADDRCTRL6:
35536bb79563SJamie Gritton 	case PRIV_NETINET_ND6:
35546bb79563SJamie Gritton 	case PRIV_NETINET_SCOPE6:
35556bb79563SJamie Gritton 	case PRIV_NETINET_ALIFETIME6:
35566bb79563SJamie Gritton 	case PRIV_NETINET_IPSEC:
35576bb79563SJamie Gritton 	case PRIV_NETINET_BINDANY:
35586bb79563SJamie Gritton 
35596bb79563SJamie Gritton #ifdef notyet
35606bb79563SJamie Gritton 		/*
35616bb79563SJamie Gritton 		 * IPX/SPX privileges.
35626bb79563SJamie Gritton 		 */
35636bb79563SJamie Gritton 	case PRIV_NETIPX_RESERVEDPORT:
35646bb79563SJamie Gritton 	case PRIV_NETIPX_RAW:
35656bb79563SJamie Gritton 
35666bb79563SJamie Gritton 		/*
35676bb79563SJamie Gritton 		 * NCP privileges.
35686bb79563SJamie Gritton 		 */
35696bb79563SJamie Gritton 	case PRIV_NETNCP:
35706bb79563SJamie Gritton 
35716bb79563SJamie Gritton 		/*
35726bb79563SJamie Gritton 		 * SMB privileges.
35736bb79563SJamie Gritton 		 */
35746bb79563SJamie Gritton 	case PRIV_NETSMB:
35756bb79563SJamie Gritton #endif
35766bb79563SJamie Gritton 
35776bb79563SJamie Gritton 	/*
35786bb79563SJamie Gritton 	 * No default: or deny here.
35796bb79563SJamie Gritton 	 * In case of no permit fall through to next switch().
35806bb79563SJamie Gritton 	 */
35816bb79563SJamie Gritton 		if (cred->cr_prison->pr_flags & PR_VNET)
35826bb79563SJamie Gritton 			return (0);
35836bb79563SJamie Gritton 	}
35846bb79563SJamie Gritton #endif /* VIMAGE */
35856bb79563SJamie Gritton 
3586800c9408SRobert Watson 	switch (priv) {
3587800c9408SRobert Watson 
3588800c9408SRobert Watson 		/*
3589800c9408SRobert Watson 		 * Allow ktrace privileges for root in jail.
3590800c9408SRobert Watson 		 */
3591800c9408SRobert Watson 	case PRIV_KTRACE:
3592800c9408SRobert Watson 
3593c3c1b5e6SRobert Watson #if 0
3594800c9408SRobert Watson 		/*
3595800c9408SRobert Watson 		 * Allow jailed processes to configure audit identity and
3596800c9408SRobert Watson 		 * submit audit records (login, etc).  In the future we may
3597800c9408SRobert Watson 		 * want to further refine the relationship between audit and
3598800c9408SRobert Watson 		 * jail.
3599800c9408SRobert Watson 		 */
3600800c9408SRobert Watson 	case PRIV_AUDIT_GETAUDIT:
3601800c9408SRobert Watson 	case PRIV_AUDIT_SETAUDIT:
3602800c9408SRobert Watson 	case PRIV_AUDIT_SUBMIT:
3603c3c1b5e6SRobert Watson #endif
3604800c9408SRobert Watson 
3605800c9408SRobert Watson 		/*
3606800c9408SRobert Watson 		 * Allow jailed processes to manipulate process UNIX
3607800c9408SRobert Watson 		 * credentials in any way they see fit.
3608800c9408SRobert Watson 		 */
3609800c9408SRobert Watson 	case PRIV_CRED_SETUID:
3610800c9408SRobert Watson 	case PRIV_CRED_SETEUID:
3611800c9408SRobert Watson 	case PRIV_CRED_SETGID:
3612800c9408SRobert Watson 	case PRIV_CRED_SETEGID:
3613800c9408SRobert Watson 	case PRIV_CRED_SETGROUPS:
3614800c9408SRobert Watson 	case PRIV_CRED_SETREUID:
3615800c9408SRobert Watson 	case PRIV_CRED_SETREGID:
3616800c9408SRobert Watson 	case PRIV_CRED_SETRESUID:
3617800c9408SRobert Watson 	case PRIV_CRED_SETRESGID:
3618800c9408SRobert Watson 
3619800c9408SRobert Watson 		/*
3620800c9408SRobert Watson 		 * Jail implements visibility constraints already, so allow
3621800c9408SRobert Watson 		 * jailed root to override uid/gid-based constraints.
3622800c9408SRobert Watson 		 */
3623800c9408SRobert Watson 	case PRIV_SEEOTHERGIDS:
3624800c9408SRobert Watson 	case PRIV_SEEOTHERUIDS:
3625800c9408SRobert Watson 
3626800c9408SRobert Watson 		/*
3627800c9408SRobert Watson 		 * Jail implements inter-process debugging limits already, so
3628800c9408SRobert Watson 		 * allow jailed root various debugging privileges.
3629800c9408SRobert Watson 		 */
3630800c9408SRobert Watson 	case PRIV_DEBUG_DIFFCRED:
3631800c9408SRobert Watson 	case PRIV_DEBUG_SUGID:
3632800c9408SRobert Watson 	case PRIV_DEBUG_UNPRIV:
3633800c9408SRobert Watson 
3634800c9408SRobert Watson 		/*
3635800c9408SRobert Watson 		 * Allow jail to set various resource limits and login
3636800c9408SRobert Watson 		 * properties, and for now, exceed process resource limits.
3637800c9408SRobert Watson 		 */
3638800c9408SRobert Watson 	case PRIV_PROC_LIMIT:
3639800c9408SRobert Watson 	case PRIV_PROC_SETLOGIN:
3640800c9408SRobert Watson 	case PRIV_PROC_SETRLIMIT:
3641800c9408SRobert Watson 
3642800c9408SRobert Watson 		/*
3643800c9408SRobert Watson 		 * System V and POSIX IPC privileges are granted in jail.
3644800c9408SRobert Watson 		 */
3645800c9408SRobert Watson 	case PRIV_IPC_READ:
3646800c9408SRobert Watson 	case PRIV_IPC_WRITE:
3647800c9408SRobert Watson 	case PRIV_IPC_ADMIN:
3648800c9408SRobert Watson 	case PRIV_IPC_MSGSIZE:
3649800c9408SRobert Watson 	case PRIV_MQ_ADMIN:
3650800c9408SRobert Watson 
3651800c9408SRobert Watson 		/*
36520304c731SJamie Gritton 		 * Jail operations within a jail work on child jails.
36530304c731SJamie Gritton 		 */
36540304c731SJamie Gritton 	case PRIV_JAIL_ATTACH:
36550304c731SJamie Gritton 	case PRIV_JAIL_SET:
36560304c731SJamie Gritton 	case PRIV_JAIL_REMOVE:
36570304c731SJamie Gritton 
36580304c731SJamie Gritton 		/*
3659800c9408SRobert Watson 		 * Jail implements its own inter-process limits, so allow
3660800c9408SRobert Watson 		 * root processes in jail to change scheduling on other
3661800c9408SRobert Watson 		 * processes in the same jail.  Likewise for signalling.
3662800c9408SRobert Watson 		 */
3663800c9408SRobert Watson 	case PRIV_SCHED_DIFFCRED:
3664413628a7SBjoern A. Zeeb 	case PRIV_SCHED_CPUSET:
3665800c9408SRobert Watson 	case PRIV_SIGNAL_DIFFCRED:
3666800c9408SRobert Watson 	case PRIV_SIGNAL_SUGID:
3667800c9408SRobert Watson 
3668800c9408SRobert Watson 		/*
3669800c9408SRobert Watson 		 * Allow jailed processes to write to sysctls marked as jail
3670800c9408SRobert Watson 		 * writable.
3671800c9408SRobert Watson 		 */
3672800c9408SRobert Watson 	case PRIV_SYSCTL_WRITEJAIL:
3673800c9408SRobert Watson 
3674800c9408SRobert Watson 		/*
3675800c9408SRobert Watson 		 * Allow root in jail to manage a variety of quota
3676e82d0201SRobert Watson 		 * properties.  These should likely be conditional on a
3677e82d0201SRobert Watson 		 * configuration option.
3678800c9408SRobert Watson 		 */
367995b091d2SRobert Watson 	case PRIV_VFS_GETQUOTA:
368095b091d2SRobert Watson 	case PRIV_VFS_SETQUOTA:
3681800c9408SRobert Watson 
3682800c9408SRobert Watson 		/*
3683800c9408SRobert Watson 		 * Since Jail relies on chroot() to implement file system
3684800c9408SRobert Watson 		 * protections, grant many VFS privileges to root in jail.
3685800c9408SRobert Watson 		 * Be careful to exclude mount-related and NFS-related
3686800c9408SRobert Watson 		 * privileges.
3687800c9408SRobert Watson 		 */
3688800c9408SRobert Watson 	case PRIV_VFS_READ:
3689800c9408SRobert Watson 	case PRIV_VFS_WRITE:
3690800c9408SRobert Watson 	case PRIV_VFS_ADMIN:
3691800c9408SRobert Watson 	case PRIV_VFS_EXEC:
3692800c9408SRobert Watson 	case PRIV_VFS_LOOKUP:
3693800c9408SRobert Watson 	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
3694800c9408SRobert Watson 	case PRIV_VFS_CHFLAGS_DEV:
3695800c9408SRobert Watson 	case PRIV_VFS_CHOWN:
3696800c9408SRobert Watson 	case PRIV_VFS_CHROOT:
3697bb531912SPawel Jakub Dawidek 	case PRIV_VFS_RETAINSUGID:
3698800c9408SRobert Watson 	case PRIV_VFS_FCHROOT:
3699800c9408SRobert Watson 	case PRIV_VFS_LINK:
3700800c9408SRobert Watson 	case PRIV_VFS_SETGID:
3701e41966dcSRobert Watson 	case PRIV_VFS_STAT:
3702800c9408SRobert Watson 	case PRIV_VFS_STICKYFILE:
3703800c9408SRobert Watson 		return (0);
3704800c9408SRobert Watson 
3705800c9408SRobert Watson 		/*
3706800c9408SRobert Watson 		 * Depending on the global setting, allow privilege of
3707800c9408SRobert Watson 		 * setting system flags.
3708800c9408SRobert Watson 		 */
3709800c9408SRobert Watson 	case PRIV_VFS_SYSFLAGS:
37100304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3711800c9408SRobert Watson 			return (0);
3712800c9408SRobert Watson 		else
3713800c9408SRobert Watson 			return (EPERM);
3714800c9408SRobert Watson 
3715800c9408SRobert Watson 		/*
3716f3a8d2f9SPawel Jakub Dawidek 		 * Depending on the global setting, allow privilege of
3717f3a8d2f9SPawel Jakub Dawidek 		 * mounting/unmounting file systems.
3718f3a8d2f9SPawel Jakub Dawidek 		 */
3719f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT:
3720f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_UNMOUNT:
3721f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_NONUSER:
372224b0502eSPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_OWNER:
37230304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT)
3724f3a8d2f9SPawel Jakub Dawidek 			return (0);
3725f3a8d2f9SPawel Jakub Dawidek 		else
3726f3a8d2f9SPawel Jakub Dawidek 			return (EPERM);
3727f3a8d2f9SPawel Jakub Dawidek 
3728f3a8d2f9SPawel Jakub Dawidek 		/*
37294b084056SRobert Watson 		 * Allow jailed root to bind reserved ports and reuse in-use
37304b084056SRobert Watson 		 * ports.
3731800c9408SRobert Watson 		 */
3732800c9408SRobert Watson 	case PRIV_NETINET_RESERVEDPORT:
37334b084056SRobert Watson 	case PRIV_NETINET_REUSEPORT:
3734800c9408SRobert Watson 		return (0);
3735800c9408SRobert Watson 
3736800c9408SRobert Watson 		/*
373779ba3952SBjoern A. Zeeb 		 * Allow jailed root to set certian IPv4/6 (option) headers.
373879ba3952SBjoern A. Zeeb 		 */
373979ba3952SBjoern A. Zeeb 	case PRIV_NETINET_SETHDROPTS:
374079ba3952SBjoern A. Zeeb 		return (0);
374179ba3952SBjoern A. Zeeb 
374279ba3952SBjoern A. Zeeb 		/*
3743800c9408SRobert Watson 		 * Conditionally allow creating raw sockets in jail.
3744800c9408SRobert Watson 		 */
3745800c9408SRobert Watson 	case PRIV_NETINET_RAW:
37460304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3747800c9408SRobert Watson 			return (0);
3748800c9408SRobert Watson 		else
3749800c9408SRobert Watson 			return (EPERM);
3750800c9408SRobert Watson 
3751800c9408SRobert Watson 		/*
3752800c9408SRobert Watson 		 * Since jail implements its own visibility limits on netstat
3753800c9408SRobert Watson 		 * sysctls, allow getcred.  This allows identd to work in
3754800c9408SRobert Watson 		 * jail.
3755800c9408SRobert Watson 		 */
3756800c9408SRobert Watson 	case PRIV_NETINET_GETCRED:
3757800c9408SRobert Watson 		return (0);
3758800c9408SRobert Watson 
3759800c9408SRobert Watson 	default:
3760800c9408SRobert Watson 		/*
3761800c9408SRobert Watson 		 * In all remaining cases, deny the privilege request.  This
3762800c9408SRobert Watson 		 * includes almost all network privileges, many system
3763800c9408SRobert Watson 		 * configuration privileges.
3764800c9408SRobert Watson 		 */
3765800c9408SRobert Watson 		return (EPERM);
3766800c9408SRobert Watson 	}
3767800c9408SRobert Watson }
3768800c9408SRobert Watson 
37690304c731SJamie Gritton /*
37700304c731SJamie Gritton  * Return the part of pr2's name that is relative to pr1, or the whole name
37710304c731SJamie Gritton  * if it does not directly follow.
37720304c731SJamie Gritton  */
37730304c731SJamie Gritton 
37740304c731SJamie Gritton char *
37750304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2)
37760304c731SJamie Gritton {
37770304c731SJamie Gritton 	char *name;
37780304c731SJamie Gritton 
37790304c731SJamie Gritton 	/* Jails see themselves as "0" (if they see themselves at all). */
37800304c731SJamie Gritton 	if (pr1 == pr2)
37810304c731SJamie Gritton 		return "0";
37820304c731SJamie Gritton 	name = pr2->pr_name;
37830304c731SJamie Gritton 	if (prison_ischild(pr1, pr2)) {
37840304c731SJamie Gritton 		/*
37850304c731SJamie Gritton 		 * pr1 isn't locked (and allprison_lock may not be either)
37860304c731SJamie Gritton 		 * so its length can't be counted on.  But the number of dots
37870304c731SJamie Gritton 		 * can be counted on - and counted.
37880304c731SJamie Gritton 		 */
37890304c731SJamie Gritton 		for (; pr1 != &prison0; pr1 = pr1->pr_parent)
37900304c731SJamie Gritton 			name = strchr(name, '.') + 1;
37910304c731SJamie Gritton 	}
37920304c731SJamie Gritton 	return (name);
37930304c731SJamie Gritton }
37940304c731SJamie Gritton 
37950304c731SJamie Gritton /*
37960304c731SJamie Gritton  * Return the part of pr2's path that is relative to pr1, or the whole path
37970304c731SJamie Gritton  * if it does not directly follow.
37980304c731SJamie Gritton  */
37990304c731SJamie Gritton static char *
38000304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2)
38010304c731SJamie Gritton {
38020304c731SJamie Gritton 	char *path1, *path2;
38030304c731SJamie Gritton 	int len1;
38040304c731SJamie Gritton 
38050304c731SJamie Gritton 	path1 = pr1->pr_path;
38060304c731SJamie Gritton 	path2 = pr2->pr_path;
38070304c731SJamie Gritton 	if (!strcmp(path1, "/"))
38080304c731SJamie Gritton 		return (path2);
38090304c731SJamie Gritton 	len1 = strlen(path1);
38100304c731SJamie Gritton 	if (strncmp(path1, path2, len1))
38110304c731SJamie Gritton 		return (path2);
38120304c731SJamie Gritton 	if (path2[len1] == '\0')
38130304c731SJamie Gritton 		return "/";
38140304c731SJamie Gritton 	if (path2[len1] == '/')
38150304c731SJamie Gritton 		return (path2 + len1);
38160304c731SJamie Gritton 	return (path2);
38170304c731SJamie Gritton }
38180304c731SJamie Gritton 
38190304c731SJamie Gritton 
38200304c731SJamie Gritton /*
38210304c731SJamie Gritton  * Jail-related sysctls.
38220304c731SJamie Gritton  */
38230304c731SJamie Gritton SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
38240304c731SJamie Gritton     "Jails");
38250304c731SJamie Gritton 
3826fd7a8150SMike Barcroft static int
3827fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3828fd7a8150SMike Barcroft {
3829b38ff370SJamie Gritton 	struct xprison *xp;
38300304c731SJamie Gritton 	struct prison *pr, *cpr;
3831b38ff370SJamie Gritton #ifdef INET
3832b38ff370SJamie Gritton 	struct in_addr *ip4 = NULL;
3833b38ff370SJamie Gritton 	int ip4s = 0;
3834b38ff370SJamie Gritton #endif
3835b38ff370SJamie Gritton #ifdef INET6
3836b38ff370SJamie Gritton 	struct in_addr *ip6 = NULL;
3837b38ff370SJamie Gritton 	int ip6s = 0;
3838b38ff370SJamie Gritton #endif
38390304c731SJamie Gritton 	int descend, error;
3840fd7a8150SMike Barcroft 
3841b38ff370SJamie Gritton 	xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
38420304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
3843b38ff370SJamie Gritton 	error = 0;
3844dc68a633SPawel Jakub Dawidek 	sx_slock(&allprison_lock);
38450304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
38460304c731SJamie Gritton #if defined(INET) || defined(INET6)
3847b38ff370SJamie Gritton  again:
38480304c731SJamie Gritton #endif
38490304c731SJamie Gritton 		mtx_lock(&cpr->pr_mtx);
3850413628a7SBjoern A. Zeeb #ifdef INET
38510304c731SJamie Gritton 		if (cpr->pr_ip4s > 0) {
38520304c731SJamie Gritton 			if (ip4s < cpr->pr_ip4s) {
38530304c731SJamie Gritton 				ip4s = cpr->pr_ip4s;
38540304c731SJamie Gritton 				mtx_unlock(&cpr->pr_mtx);
3855b38ff370SJamie Gritton 				ip4 = realloc(ip4, ip4s *
3856b38ff370SJamie Gritton 				    sizeof(struct in_addr), M_TEMP, M_WAITOK);
3857b38ff370SJamie Gritton 				goto again;
3858b38ff370SJamie Gritton 			}
38590304c731SJamie Gritton 			bcopy(cpr->pr_ip4, ip4,
38600304c731SJamie Gritton 			    cpr->pr_ip4s * sizeof(struct in_addr));
3861b38ff370SJamie Gritton 		}
3862413628a7SBjoern A. Zeeb #endif
3863413628a7SBjoern A. Zeeb #ifdef INET6
38640304c731SJamie Gritton 		if (cpr->pr_ip6s > 0) {
38650304c731SJamie Gritton 			if (ip6s < cpr->pr_ip6s) {
38660304c731SJamie Gritton 				ip6s = cpr->pr_ip6s;
38670304c731SJamie Gritton 				mtx_unlock(&cpr->pr_mtx);
3868b38ff370SJamie Gritton 				ip6 = realloc(ip6, ip6s *
3869b38ff370SJamie Gritton 				    sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3870b38ff370SJamie Gritton 				goto again;
3871413628a7SBjoern A. Zeeb 			}
38720304c731SJamie Gritton 			bcopy(cpr->pr_ip6, ip6,
38730304c731SJamie Gritton 			    cpr->pr_ip6s * sizeof(struct in6_addr));
3874b38ff370SJamie Gritton 		}
3875b38ff370SJamie Gritton #endif
38760304c731SJamie Gritton 		if (cpr->pr_ref == 0) {
38770304c731SJamie Gritton 			mtx_unlock(&cpr->pr_mtx);
3878b38ff370SJamie Gritton 			continue;
3879b38ff370SJamie Gritton 		}
3880b38ff370SJamie Gritton 		bzero(xp, sizeof(*xp));
3881fd7a8150SMike Barcroft 		xp->pr_version = XPRISON_VERSION;
38820304c731SJamie Gritton 		xp->pr_id = cpr->pr_id;
38830304c731SJamie Gritton 		xp->pr_state = cpr->pr_uref > 0
3884b38ff370SJamie Gritton 		    ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
38850304c731SJamie Gritton 		strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3886c1f19219SJamie Gritton 		strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
38870304c731SJamie Gritton 		strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
3888413628a7SBjoern A. Zeeb #ifdef INET
38890304c731SJamie Gritton 		xp->pr_ip4s = cpr->pr_ip4s;
3890413628a7SBjoern A. Zeeb #endif
3891413628a7SBjoern A. Zeeb #ifdef INET6
38920304c731SJamie Gritton 		xp->pr_ip6s = cpr->pr_ip6s;
3893413628a7SBjoern A. Zeeb #endif
38940304c731SJamie Gritton 		mtx_unlock(&cpr->pr_mtx);
3895b38ff370SJamie Gritton 		error = SYSCTL_OUT(req, xp, sizeof(*xp));
3896b38ff370SJamie Gritton 		if (error)
3897b38ff370SJamie Gritton 			break;
3898413628a7SBjoern A. Zeeb #ifdef INET
3899b38ff370SJamie Gritton 		if (xp->pr_ip4s > 0) {
3900b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip4,
3901b38ff370SJamie Gritton 			    xp->pr_ip4s * sizeof(struct in_addr));
3902b38ff370SJamie Gritton 			if (error)
3903b38ff370SJamie Gritton 				break;
3904413628a7SBjoern A. Zeeb 		}
3905413628a7SBjoern A. Zeeb #endif
3906413628a7SBjoern A. Zeeb #ifdef INET6
3907b38ff370SJamie Gritton 		if (xp->pr_ip6s > 0) {
3908b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip6,
3909b38ff370SJamie Gritton 			    xp->pr_ip6s * sizeof(struct in6_addr));
3910b38ff370SJamie Gritton 			if (error)
3911b38ff370SJamie Gritton 				break;
3912413628a7SBjoern A. Zeeb 		}
3913413628a7SBjoern A. Zeeb #endif
3914fd7a8150SMike Barcroft 	}
3915dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
3916b38ff370SJamie Gritton 	free(xp, M_TEMP);
3917b38ff370SJamie Gritton #ifdef INET
3918b38ff370SJamie Gritton 	free(ip4, M_TEMP);
3919b38ff370SJamie Gritton #endif
3920b38ff370SJamie Gritton #ifdef INET6
3921b38ff370SJamie Gritton 	free(ip6, M_TEMP);
3922b38ff370SJamie Gritton #endif
3923fd7a8150SMike Barcroft 	return (error);
3924fd7a8150SMike Barcroft }
3925fd7a8150SMike Barcroft 
3926f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list,
3927f3b86a5fSEd Schouten     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3928f3b86a5fSEd Schouten     sysctl_jail_list, "S", "List of active jails");
3929461167c2SPawel Jakub Dawidek 
3930461167c2SPawel Jakub Dawidek static int
3931461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
3932461167c2SPawel Jakub Dawidek {
3933461167c2SPawel Jakub Dawidek 	int error, injail;
3934461167c2SPawel Jakub Dawidek 
3935461167c2SPawel Jakub Dawidek 	injail = jailed(req->td->td_ucred);
3936461167c2SPawel Jakub Dawidek 	error = SYSCTL_OUT(req, &injail, sizeof(injail));
3937461167c2SPawel Jakub Dawidek 
3938461167c2SPawel Jakub Dawidek 	return (error);
3939461167c2SPawel Jakub Dawidek }
39400304c731SJamie Gritton 
3941f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
3942f3b86a5fSEd Schouten     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3943f3b86a5fSEd Schouten     sysctl_jail_jailed, "I", "Process in jail?");
3944413628a7SBjoern A. Zeeb 
39450304c731SJamie Gritton #if defined(INET) || defined(INET6)
3946e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
39470304c731SJamie Gritton     &jail_max_af_ips, 0,
39480304c731SJamie Gritton     "Number of IP addresses a jail may have at most per address family");
39490304c731SJamie Gritton #endif
39500304c731SJamie Gritton 
39510304c731SJamie Gritton /*
39520304c731SJamie Gritton  * Default parameters for jail(2) compatability.  For historical reasons,
39530304c731SJamie Gritton  * the sysctl names have varying similarity to the parameter names.  Prisons
39540304c731SJamie Gritton  * just see their own parameters, and can't change them.
39550304c731SJamie Gritton  */
39560304c731SJamie Gritton static int
39570304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
39580304c731SJamie Gritton {
39590304c731SJamie Gritton 	struct prison *pr;
39600304c731SJamie Gritton 	int allow, error, i;
39610304c731SJamie Gritton 
39620304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
39630304c731SJamie Gritton 	allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
39640304c731SJamie Gritton 
39650304c731SJamie Gritton 	/* Get the current flag value, and convert it to a boolean. */
39660304c731SJamie Gritton 	i = (allow & arg2) ? 1 : 0;
39670304c731SJamie Gritton 	if (arg1 != NULL)
39680304c731SJamie Gritton 		i = !i;
39690304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &i, 0, req);
39700304c731SJamie Gritton 	if (error || !req->newptr)
39710304c731SJamie Gritton 		return (error);
39720304c731SJamie Gritton 	i = i ? arg2 : 0;
39730304c731SJamie Gritton 	if (arg1 != NULL)
39740304c731SJamie Gritton 		i ^= arg2;
39750304c731SJamie Gritton 	/*
39760304c731SJamie Gritton 	 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
39770304c731SJamie Gritton 	 * for writing.
39780304c731SJamie Gritton 	 */
39790304c731SJamie Gritton 	mtx_lock(&prison0.pr_mtx);
39800304c731SJamie Gritton 	jail_default_allow = (jail_default_allow & ~arg2) | i;
39810304c731SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
39820304c731SJamie Gritton 	return (0);
39830304c731SJamie Gritton }
39840304c731SJamie Gritton 
39850304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
39860304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
39870304c731SJamie Gritton     NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
39880304c731SJamie Gritton     "Processes in jail can set their hostnames");
39890304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
39900304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
39910304c731SJamie Gritton     (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
39920304c731SJamie Gritton     "Processes in jail are limited to creating UNIX/IP/route sockets only");
39930304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
39940304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
39950304c731SJamie Gritton     NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
39960304c731SJamie Gritton     "Processes in jail can use System V IPC primitives");
39970304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
39980304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
39990304c731SJamie Gritton     NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
40000304c731SJamie Gritton     "Prison root can create raw sockets");
40010304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
40020304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
40030304c731SJamie Gritton     NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
40040304c731SJamie Gritton     "Processes in jail can alter system file flags");
40050304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
40060304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
40070304c731SJamie Gritton     NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
40080304c731SJamie Gritton     "Processes in jail can mount/unmount jail-friendly file systems");
40090304c731SJamie Gritton 
40100304c731SJamie Gritton static int
40110304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
40120304c731SJamie Gritton {
40130304c731SJamie Gritton 	struct prison *pr;
40140304c731SJamie Gritton 	int level, error;
40150304c731SJamie Gritton 
40160304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
40170304c731SJamie Gritton 	level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
40180304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &level, 0, req);
40190304c731SJamie Gritton 	if (error || !req->newptr)
40200304c731SJamie Gritton 		return (error);
40210304c731SJamie Gritton 	*(int *)arg1 = level;
40220304c731SJamie Gritton 	return (0);
40230304c731SJamie Gritton }
40240304c731SJamie Gritton 
40250304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
40260304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
40270304c731SJamie Gritton     &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
40280304c731SJamie Gritton     sysctl_jail_default_level, "I",
40290304c731SJamie Gritton     "Processes in jail cannot see all mounted file systems");
40300304c731SJamie Gritton 
40310304c731SJamie Gritton /*
40320304c731SJamie Gritton  * Nodes to describe jail parameters.  Maximum length of string parameters
40330304c731SJamie Gritton  * is returned in the string itself, and the other parameters exist merely
40340304c731SJamie Gritton  * to make themselves and their types known.
40350304c731SJamie Gritton  */
40360304c731SJamie Gritton SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
40370304c731SJamie Gritton     "Jail parameters");
40380304c731SJamie Gritton 
40390304c731SJamie Gritton int
40400304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS)
40410304c731SJamie Gritton {
40420304c731SJamie Gritton 	int i;
40430304c731SJamie Gritton 	long l;
40440304c731SJamie Gritton 	size_t s;
40450304c731SJamie Gritton 	char numbuf[12];
40460304c731SJamie Gritton 
40470304c731SJamie Gritton 	switch (oidp->oid_kind & CTLTYPE)
40480304c731SJamie Gritton 	{
40490304c731SJamie Gritton 	case CTLTYPE_LONG:
40500304c731SJamie Gritton 	case CTLTYPE_ULONG:
40510304c731SJamie Gritton 		l = 0;
40520304c731SJamie Gritton #ifdef SCTL_MASK32
40530304c731SJamie Gritton 		if (!(req->flags & SCTL_MASK32))
40540304c731SJamie Gritton #endif
40550304c731SJamie Gritton 			return (SYSCTL_OUT(req, &l, sizeof(l)));
40560304c731SJamie Gritton 	case CTLTYPE_INT:
40570304c731SJamie Gritton 	case CTLTYPE_UINT:
40580304c731SJamie Gritton 		i = 0;
40590304c731SJamie Gritton 		return (SYSCTL_OUT(req, &i, sizeof(i)));
40600304c731SJamie Gritton 	case CTLTYPE_STRING:
40610304c731SJamie Gritton 		snprintf(numbuf, sizeof(numbuf), "%d", arg2);
40620304c731SJamie Gritton 		return
40630304c731SJamie Gritton 		    (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
40640304c731SJamie Gritton 	case CTLTYPE_STRUCT:
40650304c731SJamie Gritton 		s = (size_t)arg2;
40660304c731SJamie Gritton 		return (SYSCTL_OUT(req, &s, sizeof(s)));
40670304c731SJamie Gritton 	}
40680304c731SJamie Gritton 	return (0);
40690304c731SJamie Gritton }
40700304c731SJamie Gritton 
40710304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
40720304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
40730304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
40740304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
40750304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
40760304c731SJamie Gritton     "I", "Jail secure level");
40770304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
40780304c731SJamie Gritton     "I", "Jail cannot see all mounted file systems");
40790304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
40800304c731SJamie Gritton     "B", "Jail persistence");
4081679e1390SJamie Gritton #ifdef VIMAGE
4082679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
40837cbf7213SJamie Gritton     "E,jailsys", "Virtual network stack");
4084679e1390SJamie Gritton #endif
40850304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
40860304c731SJamie Gritton     "B", "Jail is in the process of shutting down");
40870304c731SJamie Gritton 
4088b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4089b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4090b97457e2SJamie Gritton     "I", "Current number of child jails");
4091b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4092b97457e2SJamie Gritton     "I", "Maximum number of child jails");
4093b97457e2SJamie Gritton 
40947cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
40950304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
40960304c731SJamie Gritton     "Jail hostname");
409776ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
409876ca6f88SJamie Gritton     "Jail NIS domainname");
409976ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
410076ca6f88SJamie Gritton     "Jail host UUID");
410176ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
410276ca6f88SJamie Gritton     "LU", "Jail host ID");
41030304c731SJamie Gritton 
41040304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
41050304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
41060304c731SJamie Gritton 
41070304c731SJamie Gritton #ifdef INET
41087cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RW, "Jail IPv4 address virtualization");
41090304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
41100304c731SJamie Gritton     "S,in_addr,a", "Jail IPv4 addresses");
41110304c731SJamie Gritton #endif
41120304c731SJamie Gritton #ifdef INET6
41137cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RW, "Jail IPv6 address virtualization");
41140304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
41150304c731SJamie Gritton     "S,in6_addr,a", "Jail IPv6 addresses");
41160304c731SJamie Gritton #endif
41170304c731SJamie Gritton 
41180304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
41190304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
41200304c731SJamie Gritton     "B", "Jail may set hostname");
41210304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
41220304c731SJamie Gritton     "B", "Jail may use SYSV IPC");
41230304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
41240304c731SJamie Gritton     "B", "Jail may create raw sockets");
41250304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
41260304c731SJamie Gritton     "B", "Jail may alter system file flags");
41270304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW,
41280304c731SJamie Gritton     "B", "Jail may mount/unmount jail-friendly file systems");
41290304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
41300304c731SJamie Gritton     "B", "Jail may set file quotas");
41310304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
41320304c731SJamie Gritton     "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
41330304c731SJamie Gritton 
41340304c731SJamie Gritton 
4135413628a7SBjoern A. Zeeb #ifdef DDB
4136b38ff370SJamie Gritton 
4137b38ff370SJamie Gritton static void
4138b38ff370SJamie Gritton db_show_prison(struct prison *pr)
4139413628a7SBjoern A. Zeeb {
41400304c731SJamie Gritton 	int fi;
4141b38ff370SJamie Gritton #if defined(INET) || defined(INET6)
4142b38ff370SJamie Gritton 	int ii;
4143413628a7SBjoern A. Zeeb #endif
41447cbf7213SJamie Gritton 	unsigned jsf;
4145413628a7SBjoern A. Zeeb #ifdef INET6
4146413628a7SBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
4147413628a7SBjoern A. Zeeb #endif
4148413628a7SBjoern A. Zeeb 
4149b38ff370SJamie Gritton 	db_printf("prison %p:\n", pr);
4150b38ff370SJamie Gritton 	db_printf(" jid             = %d\n", pr->pr_id);
4151b38ff370SJamie Gritton 	db_printf(" name            = %s\n", pr->pr_name);
41520304c731SJamie Gritton 	db_printf(" parent          = %p\n", pr->pr_parent);
4153b38ff370SJamie Gritton 	db_printf(" ref             = %d\n", pr->pr_ref);
4154b38ff370SJamie Gritton 	db_printf(" uref            = %d\n", pr->pr_uref);
4155b38ff370SJamie Gritton 	db_printf(" path            = %s\n", pr->pr_path);
4156b38ff370SJamie Gritton 	db_printf(" cpuset          = %d\n", pr->pr_cpuset
4157b38ff370SJamie Gritton 	    ? pr->pr_cpuset->cs_id : -1);
4158679e1390SJamie Gritton #ifdef VIMAGE
4159679e1390SJamie Gritton 	db_printf(" vnet            = %p\n", pr->pr_vnet);
4160679e1390SJamie Gritton #endif
4161b38ff370SJamie Gritton 	db_printf(" root            = %p\n", pr->pr_root);
4162b38ff370SJamie Gritton 	db_printf(" securelevel     = %d\n", pr->pr_securelevel);
4163b97457e2SJamie Gritton 	db_printf(" childcount      = %d\n", pr->pr_childcount);
41640304c731SJamie Gritton 	db_printf(" child           = %p\n", LIST_FIRST(&pr->pr_children));
41650304c731SJamie Gritton 	db_printf(" sibling         = %p\n", LIST_NEXT(pr, pr_sibling));
4166b38ff370SJamie Gritton 	db_printf(" flags           = %x", pr->pr_flags);
41670304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
41680304c731SJamie Gritton 	    fi++)
41690304c731SJamie Gritton 		if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
41700304c731SJamie Gritton 			db_printf(" %s", pr_flag_names[fi]);
41717cbf7213SJamie Gritton 	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
41727cbf7213SJamie Gritton 	    fi++) {
41737cbf7213SJamie Gritton 		jsf = pr->pr_flags &
41747cbf7213SJamie Gritton 		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
41757cbf7213SJamie Gritton 		db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
41767cbf7213SJamie Gritton 		    pr_flag_jailsys[fi].disable &&
41777cbf7213SJamie Gritton 		      (jsf == pr_flag_jailsys[fi].disable) ? "disable"
41787cbf7213SJamie Gritton 		    : (jsf == pr_flag_jailsys[fi].new) ? "new"
41797cbf7213SJamie Gritton 		    : "inherit");
41807cbf7213SJamie Gritton 	}
41810304c731SJamie Gritton 	db_printf(" allow           = %x", pr->pr_allow);
41820304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
41830304c731SJamie Gritton 	    fi++)
41840304c731SJamie Gritton 		if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
41850304c731SJamie Gritton 			db_printf(" %s", pr_allow_names[fi]);
4186b38ff370SJamie Gritton 	db_printf("\n");
41870304c731SJamie Gritton 	db_printf(" enforce_statfs  = %d\n", pr->pr_enforce_statfs);
4188c1f19219SJamie Gritton 	db_printf(" host.hostname   = %s\n", pr->pr_hostname);
4189c1f19219SJamie Gritton 	db_printf(" host.domainname = %s\n", pr->pr_domainname);
4190c1f19219SJamie Gritton 	db_printf(" host.hostuuid   = %s\n", pr->pr_hostuuid);
419176ca6f88SJamie Gritton 	db_printf(" host.hostid     = %lu\n", pr->pr_hostid);
4192413628a7SBjoern A. Zeeb #ifdef INET
4193b38ff370SJamie Gritton 	db_printf(" ip4s            = %d\n", pr->pr_ip4s);
4194b38ff370SJamie Gritton 	for (ii = 0; ii < pr->pr_ip4s; ii++)
4195b38ff370SJamie Gritton 		db_printf(" %s %s\n",
4196b38ff370SJamie Gritton 		    ii == 0 ? "ip4             =" : "                 ",
4197b38ff370SJamie Gritton 		    inet_ntoa(pr->pr_ip4[ii]));
4198413628a7SBjoern A. Zeeb #endif
4199413628a7SBjoern A. Zeeb #ifdef INET6
4200b38ff370SJamie Gritton 	db_printf(" ip6s            = %d\n", pr->pr_ip6s);
4201b38ff370SJamie Gritton 	for (ii = 0; ii < pr->pr_ip6s; ii++)
4202b38ff370SJamie Gritton 		db_printf(" %s %s\n",
4203b38ff370SJamie Gritton 		    ii == 0 ? "ip6             =" : "                 ",
4204b38ff370SJamie Gritton 		    ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4205b38ff370SJamie Gritton #endif
4206b38ff370SJamie Gritton }
4207b38ff370SJamie Gritton 
4208b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command)
4209b38ff370SJamie Gritton {
4210b38ff370SJamie Gritton 	struct prison *pr;
4211b38ff370SJamie Gritton 
4212b38ff370SJamie Gritton 	if (!have_addr) {
42130304c731SJamie Gritton 		/*
42140304c731SJamie Gritton 		 * Show all prisons in the list, and prison0 which is not
42150304c731SJamie Gritton 		 * listed.
42160304c731SJamie Gritton 		 */
42170304c731SJamie Gritton 		db_show_prison(&prison0);
42180304c731SJamie Gritton 		if (!db_pager_quit) {
4219b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list) {
4220b38ff370SJamie Gritton 				db_show_prison(pr);
4221413628a7SBjoern A. Zeeb 				if (db_pager_quit)
4222413628a7SBjoern A. Zeeb 					break;
4223413628a7SBjoern A. Zeeb 			}
42240304c731SJamie Gritton 		}
4225b38ff370SJamie Gritton 		return;
4226413628a7SBjoern A. Zeeb 	}
4227b38ff370SJamie Gritton 
42280304c731SJamie Gritton 	if (addr == 0)
42290304c731SJamie Gritton 		pr = &prison0;
42300304c731SJamie Gritton 	else {
4231b38ff370SJamie Gritton 		/* Look for a prison with the ID and with references. */
4232b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list)
4233b38ff370SJamie Gritton 			if (pr->pr_id == addr && pr->pr_ref > 0)
4234b38ff370SJamie Gritton 				break;
4235b38ff370SJamie Gritton 		if (pr == NULL)
4236b38ff370SJamie Gritton 			/* Look again, without requiring a reference. */
4237b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list)
4238b38ff370SJamie Gritton 				if (pr->pr_id == addr)
4239b38ff370SJamie Gritton 					break;
4240b38ff370SJamie Gritton 		if (pr == NULL)
4241b38ff370SJamie Gritton 			/* Assume address points to a valid prison. */
4242b38ff370SJamie Gritton 			pr = (struct prison *)addr;
42430304c731SJamie Gritton 	}
4244b38ff370SJamie Gritton 	db_show_prison(pr);
4245b38ff370SJamie Gritton }
4246b38ff370SJamie Gritton 
4247413628a7SBjoern A. Zeeb #endif /* DDB */
4248