xref: /freebsd/sys/kern/kern_jail.c (revision 176ff3a0667cdcee0c878442d8c3d6e09706f642)
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>
52097055e2SEdward Tomasz Napierala #include <sys/racct.h>
53a7ad07bfSEdward Tomasz Napierala #include <sys/refcount.h>
54dc68a633SPawel Jakub Dawidek #include <sys/sx.h>
5576ca6f88SJamie Gritton #include <sys/sysent.h>
56fd7a8150SMike Barcroft #include <sys/namei.h>
57820a0de9SPawel Jakub Dawidek #include <sys/mount.h>
58fd7a8150SMike Barcroft #include <sys/queue.h>
5975c13541SPoul-Henning Kamp #include <sys/socket.h>
60fd7a8150SMike Barcroft #include <sys/syscallsubr.h>
6183f1e257SRobert Watson #include <sys/sysctl.h>
62fd7a8150SMike Barcroft #include <sys/vnode.h>
63530c0060SRobert Watson 
6475c13541SPoul-Henning Kamp #include <net/if.h>
65530c0060SRobert Watson #include <net/vnet.h>
66530c0060SRobert Watson 
6775c13541SPoul-Henning Kamp #include <netinet/in.h>
68530c0060SRobert Watson 
69413628a7SBjoern A. Zeeb #ifdef DDB
70413628a7SBjoern A. Zeeb #include <ddb/ddb.h>
71413628a7SBjoern A. Zeeb #endif /* DDB */
7275c13541SPoul-Henning Kamp 
73aed55708SRobert Watson #include <security/mac/mac_framework.h>
74aed55708SRobert Watson 
758986e3a0SJamie Gritton #define	DEFAULT_HOSTUUID	"00000000-0000-0000-0000-000000000000"
768986e3a0SJamie Gritton 
7775c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
78d745c852SEd Schouten static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
7975c13541SPoul-Henning Kamp 
80592bcae8SBjoern A. Zeeb /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
81592bcae8SBjoern A. Zeeb #ifdef INET
82592bcae8SBjoern A. Zeeb #ifdef INET6
83592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
84592bcae8SBjoern A. Zeeb #else
85592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL
86592bcae8SBjoern A. Zeeb #endif
87592bcae8SBjoern A. Zeeb #else /* !INET */
88592bcae8SBjoern A. Zeeb #ifdef INET6
89592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP6_SADDRSEL
90592bcae8SBjoern A. Zeeb #else
91592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	0
92592bcae8SBjoern A. Zeeb #endif
93592bcae8SBjoern A. Zeeb #endif
94592bcae8SBjoern A. Zeeb 
950304c731SJamie Gritton /* prison0 describes what is "real" about the system. */
960304c731SJamie Gritton struct prison prison0 = {
970304c731SJamie Gritton 	.pr_id		= 0,
980304c731SJamie Gritton 	.pr_name	= "0",
990304c731SJamie Gritton 	.pr_ref		= 1,
1000304c731SJamie Gritton 	.pr_uref	= 1,
1010304c731SJamie Gritton 	.pr_path	= "/",
1020304c731SJamie Gritton 	.pr_securelevel	= -1,
1030cc207a6SMartin Matuska 	.pr_devfs_rsnum = 0,
104b97457e2SJamie Gritton 	.pr_childmax	= JAIL_MAX,
1058986e3a0SJamie Gritton 	.pr_hostuuid	= DEFAULT_HOSTUUID,
10613e403fdSAntoine Brodin 	.pr_children	= LIST_HEAD_INITIALIZER(prison0.pr_children),
107eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE
108592bcae8SBjoern A. Zeeb 	.pr_flags	= PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
109eb79e1c7SBjoern A. Zeeb #else
110592bcae8SBjoern A. Zeeb 	.pr_flags	= PR_HOST|_PR_IP_SADDRSEL,
111eb79e1c7SBjoern A. Zeeb #endif
1120304c731SJamie Gritton 	.pr_allow	= PR_ALLOW_ALL,
1130304c731SJamie Gritton };
1140304c731SJamie Gritton MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
11583f1e257SRobert Watson 
116a7ad07bfSEdward Tomasz Napierala /* allprison, allprison_racct and lastprid are protected by allprison_lock. */
117dc68a633SPawel Jakub Dawidek struct	sx allprison_lock;
118b38ff370SJamie Gritton SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
119b38ff370SJamie Gritton struct	prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
120a7ad07bfSEdward Tomasz Napierala LIST_HEAD(, prison_racct) allprison_racct;
1212110d913SXin LI int	lastprid = 0;
122fd7a8150SMike Barcroft 
123b38ff370SJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr);
124b3059e09SRobert Watson static void prison_complete(void *context, int pending);
125b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags);
1260304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2);
1270304c731SJamie Gritton static void prison_remove_one(struct prison *pr);
128a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
129a7ad07bfSEdward Tomasz Napierala static void prison_racct_attach(struct prison *pr);
130c34bbd2aSEdward Tomasz Napierala static void prison_racct_modify(struct prison *pr);
131a7ad07bfSEdward Tomasz Napierala static void prison_racct_detach(struct prison *pr);
132a7ad07bfSEdward Tomasz Napierala #endif
133413628a7SBjoern A. Zeeb #ifdef INET
1340d168b8dSGleb Smirnoff static int _prison_check_ip4(const struct prison *, const struct in_addr *);
1350304c731SJamie Gritton static int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4);
136413628a7SBjoern A. Zeeb #endif
137413628a7SBjoern A. Zeeb #ifdef INET6
1388571af59SJamie Gritton static int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6);
1390304c731SJamie Gritton static int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6);
140413628a7SBjoern A. Zeeb #endif
141fd7a8150SMike Barcroft 
142b38ff370SJamie Gritton /* Flags for prison_deref */
143b38ff370SJamie Gritton #define	PD_DEREF	0x01
144b38ff370SJamie Gritton #define	PD_DEUREF	0x02
145b38ff370SJamie Gritton #define	PD_LOCKED	0x04
146b38ff370SJamie Gritton #define	PD_LIST_SLOCKED	0x08
147b38ff370SJamie Gritton #define	PD_LIST_XLOCKED	0x10
148fd7a8150SMike Barcroft 
1490304c731SJamie Gritton /*
1505cc70397SBjoern A. Zeeb  * Parameter names corresponding to PR_* flag values.  Size values are for kvm
1515cc70397SBjoern A. Zeeb  * as we cannot figure out the size of a sparse array, or an array without a
1525cc70397SBjoern A. Zeeb  * terminating entry.
1530304c731SJamie Gritton  */
1540304c731SJamie Gritton static char *pr_flag_names[] = {
1550304c731SJamie Gritton 	[0] = "persist",
156592bcae8SBjoern A. Zeeb #ifdef INET
157592bcae8SBjoern A. Zeeb 	[7] = "ip4.saddrsel",
158592bcae8SBjoern A. Zeeb #endif
159592bcae8SBjoern A. Zeeb #ifdef INET6
160592bcae8SBjoern A. Zeeb 	[8] = "ip6.saddrsel",
161592bcae8SBjoern A. Zeeb #endif
1620304c731SJamie Gritton };
1635cc70397SBjoern A. Zeeb const size_t pr_flag_names_size = sizeof(pr_flag_names);
1640304c731SJamie Gritton 
1650304c731SJamie Gritton static char *pr_flag_nonames[] = {
1660304c731SJamie Gritton 	[0] = "nopersist",
167592bcae8SBjoern A. Zeeb #ifdef INET
168592bcae8SBjoern A. Zeeb 	[7] = "ip4.nosaddrsel",
169592bcae8SBjoern A. Zeeb #endif
170592bcae8SBjoern A. Zeeb #ifdef INET6
171592bcae8SBjoern A. Zeeb 	[8] = "ip6.nosaddrsel",
172592bcae8SBjoern A. Zeeb #endif
1737cbf7213SJamie Gritton };
1745cc70397SBjoern A. Zeeb const size_t pr_flag_nonames_size = sizeof(pr_flag_nonames);
1757cbf7213SJamie Gritton 
1767cbf7213SJamie Gritton struct jailsys_flags {
1777cbf7213SJamie Gritton 	const char	*name;
1787cbf7213SJamie Gritton 	unsigned	 disable;
1797cbf7213SJamie Gritton 	unsigned	 new;
1807cbf7213SJamie Gritton } pr_flag_jailsys[] = {
1817cbf7213SJamie Gritton 	{ "host", 0, PR_HOST },
1827cbf7213SJamie Gritton #ifdef VIMAGE
1837cbf7213SJamie Gritton 	{ "vnet", 0, PR_VNET },
1847cbf7213SJamie Gritton #endif
1850304c731SJamie Gritton #ifdef INET
1866a3f2779SJamie Gritton 	{ "ip4", PR_IP4_USER, PR_IP4_USER },
1870304c731SJamie Gritton #endif
1880304c731SJamie Gritton #ifdef INET6
1896a3f2779SJamie Gritton 	{ "ip6", PR_IP6_USER, PR_IP6_USER },
190679e1390SJamie Gritton #endif
1910304c731SJamie Gritton };
1925cc70397SBjoern A. Zeeb const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
1930304c731SJamie Gritton 
1940304c731SJamie Gritton static char *pr_allow_names[] = {
1950304c731SJamie Gritton 	"allow.set_hostname",
1960304c731SJamie Gritton 	"allow.sysvipc",
1970304c731SJamie Gritton 	"allow.raw_sockets",
1980304c731SJamie Gritton 	"allow.chflags",
1990304c731SJamie Gritton 	"allow.mount",
2000304c731SJamie Gritton 	"allow.quotas",
2010304c731SJamie Gritton 	"allow.socket_af",
202bf3db8aaSMartin Matuska 	"allow.mount.devfs",
203bf3db8aaSMartin Matuska 	"allow.mount.nullfs",
204e7af90abSMartin Matuska 	"allow.mount.zfs",
20541c0675eSMartin Matuska 	"allow.mount.procfs",
2062454886eSXin LI 	"allow.mount.tmpfs",
207464aad14SJamie Gritton 	"allow.mount.fdescfs",
208f19e47d6SMarcelo Araujo 	"allow.mount.linprocfs",
209f19e47d6SMarcelo Araujo 	"allow.mount.linsysfs",
2100304c731SJamie Gritton };
2115cc70397SBjoern A. Zeeb const size_t pr_allow_names_size = sizeof(pr_allow_names);
2120304c731SJamie Gritton 
2130304c731SJamie Gritton static char *pr_allow_nonames[] = {
2140304c731SJamie Gritton 	"allow.noset_hostname",
2150304c731SJamie Gritton 	"allow.nosysvipc",
2160304c731SJamie Gritton 	"allow.noraw_sockets",
2170304c731SJamie Gritton 	"allow.nochflags",
2180304c731SJamie Gritton 	"allow.nomount",
2190304c731SJamie Gritton 	"allow.noquotas",
2200304c731SJamie Gritton 	"allow.nosocket_af",
221bf3db8aaSMartin Matuska 	"allow.mount.nodevfs",
222bf3db8aaSMartin Matuska 	"allow.mount.nonullfs",
223e7af90abSMartin Matuska 	"allow.mount.nozfs",
22441c0675eSMartin Matuska 	"allow.mount.noprocfs",
2252454886eSXin LI 	"allow.mount.notmpfs",
226464aad14SJamie Gritton 	"allow.mount.nofdescfs",
227f19e47d6SMarcelo Araujo 	"allow.mount.nolinprocfs",
228f19e47d6SMarcelo Araujo 	"allow.mount.nolinsysfs",
2290304c731SJamie Gritton };
2305cc70397SBjoern A. Zeeb const size_t pr_allow_nonames_size = sizeof(pr_allow_nonames);
2310304c731SJamie Gritton 
2320304c731SJamie Gritton #define	JAIL_DEFAULT_ALLOW		PR_ALLOW_SET_HOSTNAME
23342bebd82SJamie Gritton #define	JAIL_DEFAULT_ENFORCE_STATFS	2
234bf3db8aaSMartin Matuska #define	JAIL_DEFAULT_DEVFS_RSNUM	0
2350304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
23642bebd82SJamie Gritton static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
2370cc207a6SMartin Matuska static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
2380304c731SJamie Gritton #if defined(INET) || defined(INET6)
239e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255;
2400304c731SJamie Gritton #endif
2410304c731SJamie Gritton 
242b96bd95bSIan Lepore /*
243b96bd95bSIan Lepore  * Initialize the parts of prison0 that can't be static-initialized with
244b96bd95bSIan Lepore  * constants.  This is called from proc0_init() after creating thread0 cpuset.
245b96bd95bSIan Lepore  */
246b96bd95bSIan Lepore void
247b96bd95bSIan Lepore prison0_init(void)
248b96bd95bSIan Lepore {
249b96bd95bSIan Lepore 
250b96bd95bSIan Lepore 	prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset);
251b96bd95bSIan Lepore 	prison0.pr_osreldate = osreldate;
252b96bd95bSIan Lepore 	strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease));
253b96bd95bSIan Lepore }
254b96bd95bSIan Lepore 
255413628a7SBjoern A. Zeeb #ifdef INET
256413628a7SBjoern A. Zeeb static int
257413628a7SBjoern A. Zeeb qcmp_v4(const void *ip1, const void *ip2)
258413628a7SBjoern A. Zeeb {
259413628a7SBjoern A. Zeeb 	in_addr_t iaa, iab;
260413628a7SBjoern A. Zeeb 
261413628a7SBjoern A. Zeeb 	/*
262413628a7SBjoern A. Zeeb 	 * We need to compare in HBO here to get the list sorted as expected
263413628a7SBjoern A. Zeeb 	 * by the result of the code.  Sorting NBO addresses gives you
264413628a7SBjoern A. Zeeb 	 * interesting results.  If you do not understand, do not try.
265413628a7SBjoern A. Zeeb 	 */
266413628a7SBjoern A. Zeeb 	iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
267413628a7SBjoern A. Zeeb 	iab = ntohl(((const struct in_addr *)ip2)->s_addr);
268413628a7SBjoern A. Zeeb 
269413628a7SBjoern A. Zeeb 	/*
270413628a7SBjoern A. Zeeb 	 * Do not simply return the difference of the two numbers, the int is
271413628a7SBjoern A. Zeeb 	 * not wide enough.
272413628a7SBjoern A. Zeeb 	 */
273413628a7SBjoern A. Zeeb 	if (iaa > iab)
274413628a7SBjoern A. Zeeb 		return (1);
275413628a7SBjoern A. Zeeb 	else if (iaa < iab)
276413628a7SBjoern A. Zeeb 		return (-1);
277413628a7SBjoern A. Zeeb 	else
278413628a7SBjoern A. Zeeb 		return (0);
279413628a7SBjoern A. Zeeb }
280413628a7SBjoern A. Zeeb #endif
281413628a7SBjoern A. Zeeb 
282413628a7SBjoern A. Zeeb #ifdef INET6
283413628a7SBjoern A. Zeeb static int
284413628a7SBjoern A. Zeeb qcmp_v6(const void *ip1, const void *ip2)
285413628a7SBjoern A. Zeeb {
286413628a7SBjoern A. Zeeb 	const struct in6_addr *ia6a, *ia6b;
287413628a7SBjoern A. Zeeb 	int i, rc;
288413628a7SBjoern A. Zeeb 
289413628a7SBjoern A. Zeeb 	ia6a = (const struct in6_addr *)ip1;
290413628a7SBjoern A. Zeeb 	ia6b = (const struct in6_addr *)ip2;
291413628a7SBjoern A. Zeeb 
292413628a7SBjoern A. Zeeb 	rc = 0;
293413628a7SBjoern A. Zeeb 	for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
294413628a7SBjoern A. Zeeb 		if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
295413628a7SBjoern A. Zeeb 			rc = 1;
296413628a7SBjoern A. Zeeb 		else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
297413628a7SBjoern A. Zeeb 			rc = -1;
298413628a7SBjoern A. Zeeb 	}
299413628a7SBjoern A. Zeeb 	return (rc);
300413628a7SBjoern A. Zeeb }
301413628a7SBjoern A. Zeeb #endif
302413628a7SBjoern A. Zeeb 
303116734c4SMatthew Dillon /*
3049ddb7954SMike Barcroft  * struct jail_args {
3059ddb7954SMike Barcroft  *	struct jail *jail;
3069ddb7954SMike Barcroft  * };
307116734c4SMatthew Dillon  */
30875c13541SPoul-Henning Kamp int
3098451d0ddSKip Macy sys_jail(struct thread *td, struct jail_args *uap)
31075c13541SPoul-Henning Kamp {
311413628a7SBjoern A. Zeeb 	uint32_t version;
312413628a7SBjoern A. Zeeb 	int error;
3130304c731SJamie Gritton 	struct jail j;
314413628a7SBjoern A. Zeeb 
315413628a7SBjoern A. Zeeb 	error = copyin(uap->jail, &version, sizeof(uint32_t));
316413628a7SBjoern A. Zeeb 	if (error)
317413628a7SBjoern A. Zeeb 		return (error);
318413628a7SBjoern A. Zeeb 
319413628a7SBjoern A. Zeeb 	switch (version) {
320413628a7SBjoern A. Zeeb 	case 0:
321413628a7SBjoern A. Zeeb 	{
322413628a7SBjoern A. Zeeb 		struct jail_v0 j0;
323413628a7SBjoern A. Zeeb 
3240304c731SJamie Gritton 		/* FreeBSD single IPv4 jails. */
3250304c731SJamie Gritton 		bzero(&j, sizeof(struct jail));
326413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
327413628a7SBjoern A. Zeeb 		if (error)
328413628a7SBjoern A. Zeeb 			return (error);
3290304c731SJamie Gritton 		j.version = j0.version;
3300304c731SJamie Gritton 		j.path = j0.path;
3310304c731SJamie Gritton 		j.hostname = j0.hostname;
332b5019bc4SPeter Wemm 		j.ip4s = htonl(j0.ip_number);	/* jail_v0 is host order */
333413628a7SBjoern A. Zeeb 		break;
334413628a7SBjoern A. Zeeb 	}
335413628a7SBjoern A. Zeeb 
336413628a7SBjoern A. Zeeb 	case 1:
337413628a7SBjoern A. Zeeb 		/*
338413628a7SBjoern A. Zeeb 		 * Version 1 was used by multi-IPv4 jail implementations
339413628a7SBjoern A. Zeeb 		 * that never made it into the official kernel.
340413628a7SBjoern A. Zeeb 		 */
341413628a7SBjoern A. Zeeb 		return (EINVAL);
342413628a7SBjoern A. Zeeb 
343413628a7SBjoern A. Zeeb 	case 2:	/* JAIL_API_VERSION */
344413628a7SBjoern A. Zeeb 		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
345413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j, sizeof(struct jail));
346413628a7SBjoern A. Zeeb 		if (error)
347413628a7SBjoern A. Zeeb 			return (error);
3480304c731SJamie Gritton 		break;
3490304c731SJamie Gritton 
3500304c731SJamie Gritton 	default:
3510304c731SJamie Gritton 		/* Sci-Fi jails are not supported, sorry. */
3520304c731SJamie Gritton 		return (EINVAL);
3530304c731SJamie Gritton 	}
3540304c731SJamie Gritton 	return (kern_jail(td, &j));
3550304c731SJamie Gritton }
3560304c731SJamie Gritton 
3570304c731SJamie Gritton int
3580304c731SJamie Gritton kern_jail(struct thread *td, struct jail *j)
3590304c731SJamie Gritton {
36002abd400SPedro F. Giffuni 	struct iovec optiov[2 * (4 + nitems(pr_allow_names)
361e92e0574SJamie Gritton #ifdef INET
362e92e0574SJamie Gritton 			    + 1
363e92e0574SJamie Gritton #endif
364e92e0574SJamie Gritton #ifdef INET6
365e92e0574SJamie Gritton 			    + 1
366e92e0574SJamie Gritton #endif
367e92e0574SJamie Gritton 			    )];
3680304c731SJamie Gritton 	struct uio opt;
3690304c731SJamie Gritton 	char *u_path, *u_hostname, *u_name;
3700304c731SJamie Gritton #ifdef INET
371e92e0574SJamie Gritton 	uint32_t ip4s;
3720304c731SJamie Gritton 	struct in_addr *u_ip4;
3730304c731SJamie Gritton #endif
3740304c731SJamie Gritton #ifdef INET6
3750304c731SJamie Gritton 	struct in6_addr *u_ip6;
3760304c731SJamie Gritton #endif
3770304c731SJamie Gritton 	size_t tmplen;
3780304c731SJamie Gritton 	int error, enforce_statfs, fi;
3790304c731SJamie Gritton 
3800304c731SJamie Gritton 	bzero(&optiov, sizeof(optiov));
3810304c731SJamie Gritton 	opt.uio_iov = optiov;
3820304c731SJamie Gritton 	opt.uio_iovcnt = 0;
3830304c731SJamie Gritton 	opt.uio_offset = -1;
3840304c731SJamie Gritton 	opt.uio_resid = -1;
3850304c731SJamie Gritton 	opt.uio_segflg = UIO_SYSSPACE;
3860304c731SJamie Gritton 	opt.uio_rw = UIO_READ;
3870304c731SJamie Gritton 	opt.uio_td = td;
3880304c731SJamie Gritton 
3890304c731SJamie Gritton 	/* Set permissions for top-level jails from sysctls. */
3900304c731SJamie Gritton 	if (!jailed(td->td_ucred)) {
39102abd400SPedro F. Giffuni 		for (fi = 0; fi < nitems(pr_allow_names); fi++) {
3920304c731SJamie Gritton 			optiov[opt.uio_iovcnt].iov_base =
3930304c731SJamie Gritton 			    (jail_default_allow & (1 << fi))
3940304c731SJamie Gritton 			    ? pr_allow_names[fi] : pr_allow_nonames[fi];
3950304c731SJamie Gritton 			optiov[opt.uio_iovcnt].iov_len =
3960304c731SJamie Gritton 			    strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
3970304c731SJamie Gritton 			opt.uio_iovcnt += 2;
3980304c731SJamie Gritton 		}
3990304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
4000304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
4010304c731SJamie Gritton 		opt.uio_iovcnt++;
4020304c731SJamie Gritton 		enforce_statfs = jail_default_enforce_statfs;
4030304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
4040304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
4050304c731SJamie Gritton 		opt.uio_iovcnt++;
4060304c731SJamie Gritton 	}
4070304c731SJamie Gritton 
408b38ff370SJamie Gritton 	tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
409b38ff370SJamie Gritton #ifdef INET
4100304c731SJamie Gritton 	ip4s = (j->version == 0) ? 1 : j->ip4s;
4110304c731SJamie Gritton 	if (ip4s > jail_max_af_ips)
412b38ff370SJamie Gritton 		return (EINVAL);
4130304c731SJamie Gritton 	tmplen += ip4s * sizeof(struct in_addr);
414b38ff370SJamie Gritton #else
4150304c731SJamie Gritton 	if (j->ip4s > 0)
416b38ff370SJamie Gritton 		return (EINVAL);
417b38ff370SJamie Gritton #endif
418b38ff370SJamie Gritton #ifdef INET6
4190304c731SJamie Gritton 	if (j->ip6s > jail_max_af_ips)
420b38ff370SJamie Gritton 		return (EINVAL);
4210304c731SJamie Gritton 	tmplen += j->ip6s * sizeof(struct in6_addr);
422b38ff370SJamie Gritton #else
4230304c731SJamie Gritton 	if (j->ip6s > 0)
424b38ff370SJamie Gritton 		return (EINVAL);
425b38ff370SJamie Gritton #endif
426b38ff370SJamie Gritton 	u_path = malloc(tmplen, M_TEMP, M_WAITOK);
427b38ff370SJamie Gritton 	u_hostname = u_path + MAXPATHLEN;
428b38ff370SJamie Gritton 	u_name = u_hostname + MAXHOSTNAMELEN;
429b38ff370SJamie Gritton #ifdef INET
430b38ff370SJamie Gritton 	u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
431b38ff370SJamie Gritton #endif
432b38ff370SJamie Gritton #ifdef INET6
433b38ff370SJamie Gritton #ifdef INET
4340304c731SJamie Gritton 	u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
435b38ff370SJamie Gritton #else
436b38ff370SJamie Gritton 	u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
437b38ff370SJamie Gritton #endif
438b38ff370SJamie Gritton #endif
4390304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "path";
4400304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("path");
4410304c731SJamie Gritton 	opt.uio_iovcnt++;
4420304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_path;
4430304c731SJamie Gritton 	error = copyinstr(j->path, u_path, MAXPATHLEN,
4440304c731SJamie Gritton 	    &optiov[opt.uio_iovcnt].iov_len);
445b38ff370SJamie Gritton 	if (error) {
446b38ff370SJamie Gritton 		free(u_path, M_TEMP);
447b38ff370SJamie Gritton 		return (error);
448b38ff370SJamie Gritton 	}
4490304c731SJamie Gritton 	opt.uio_iovcnt++;
4500304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "host.hostname";
4510304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
4520304c731SJamie Gritton 	opt.uio_iovcnt++;
4530304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_hostname;
4540304c731SJamie Gritton 	error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
4550304c731SJamie Gritton 	    &optiov[opt.uio_iovcnt].iov_len);
456b38ff370SJamie Gritton 	if (error) {
457b38ff370SJamie Gritton 		free(u_path, M_TEMP);
458b38ff370SJamie Gritton 		return (error);
459b38ff370SJamie Gritton 	}
4600304c731SJamie Gritton 	opt.uio_iovcnt++;
4610304c731SJamie Gritton 	if (j->jailname != NULL) {
462b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = "name";
463b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof("name");
464b38ff370SJamie Gritton 		opt.uio_iovcnt++;
465b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = u_name;
4660304c731SJamie Gritton 		error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
467b38ff370SJamie Gritton 		    &optiov[opt.uio_iovcnt].iov_len);
468b38ff370SJamie Gritton 		if (error) {
469b38ff370SJamie Gritton 			free(u_path, M_TEMP);
470b38ff370SJamie Gritton 			return (error);
471b38ff370SJamie Gritton 		}
472b38ff370SJamie Gritton 		opt.uio_iovcnt++;
473b38ff370SJamie Gritton 	}
474b38ff370SJamie Gritton #ifdef INET
475b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
476b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
477b38ff370SJamie Gritton 	opt.uio_iovcnt++;
478b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_ip4;
4790304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
4800304c731SJamie Gritton 	if (j->version == 0)
4810304c731SJamie Gritton 		u_ip4->s_addr = j->ip4s;
4820304c731SJamie Gritton 	else {
4830304c731SJamie Gritton 		error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
484b38ff370SJamie Gritton 		if (error) {
485b38ff370SJamie Gritton 			free(u_path, M_TEMP);
486b38ff370SJamie Gritton 			return (error);
487b38ff370SJamie Gritton 		}
4880304c731SJamie Gritton 	}
489b38ff370SJamie Gritton 	opt.uio_iovcnt++;
490b38ff370SJamie Gritton #endif
491b38ff370SJamie Gritton #ifdef INET6
492b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
493b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
494b38ff370SJamie Gritton 	opt.uio_iovcnt++;
495b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_ip6;
4960304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
4970304c731SJamie Gritton 	error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
498b38ff370SJamie Gritton 	if (error) {
499b38ff370SJamie Gritton 		free(u_path, M_TEMP);
500b38ff370SJamie Gritton 		return (error);
501b38ff370SJamie Gritton 	}
502b38ff370SJamie Gritton 	opt.uio_iovcnt++;
503b38ff370SJamie Gritton #endif
50402abd400SPedro F. Giffuni 	KASSERT(opt.uio_iovcnt <= nitems(optiov),
5050304c731SJamie Gritton 		("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
506b38ff370SJamie Gritton 	error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
507b38ff370SJamie Gritton 	free(u_path, M_TEMP);
508b38ff370SJamie Gritton 	return (error);
509b38ff370SJamie Gritton }
510b38ff370SJamie Gritton 
5110304c731SJamie Gritton 
512b38ff370SJamie Gritton /*
513b38ff370SJamie Gritton  * struct jail_set_args {
514b38ff370SJamie Gritton  *	struct iovec *iovp;
515b38ff370SJamie Gritton  *	unsigned int iovcnt;
516b38ff370SJamie Gritton  *	int flags;
517b38ff370SJamie Gritton  * };
518b38ff370SJamie Gritton  */
519b38ff370SJamie Gritton int
5208451d0ddSKip Macy sys_jail_set(struct thread *td, struct jail_set_args *uap)
521b38ff370SJamie Gritton {
522b38ff370SJamie Gritton 	struct uio *auio;
523b38ff370SJamie Gritton 	int error;
524b38ff370SJamie Gritton 
525b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
526b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
527b38ff370SJamie Gritton 		return (EINVAL);
528b38ff370SJamie Gritton 
529b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
530b38ff370SJamie Gritton 	if (error)
531b38ff370SJamie Gritton 		return (error);
532b38ff370SJamie Gritton 	error = kern_jail_set(td, auio, uap->flags);
533b38ff370SJamie Gritton 	free(auio, M_IOV);
534b38ff370SJamie Gritton 	return (error);
535413628a7SBjoern A. Zeeb }
536413628a7SBjoern A. Zeeb 
537413628a7SBjoern A. Zeeb int
538b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags)
539413628a7SBjoern A. Zeeb {
540fd7a8150SMike Barcroft 	struct nameidata nd;
541b38ff370SJamie Gritton #ifdef INET
542b38ff370SJamie Gritton 	struct in_addr *ip4;
543413628a7SBjoern A. Zeeb #endif
544b38ff370SJamie Gritton #ifdef INET6
545b38ff370SJamie Gritton 	struct in6_addr *ip6;
546b38ff370SJamie Gritton #endif
547b38ff370SJamie Gritton 	struct vfsopt *opt;
548b38ff370SJamie Gritton 	struct vfsoptlist *opts;
54957aea6dfSBjoern A. Zeeb 	struct prison *pr, *deadpr, *mypr, *ppr, *tpr;
550b38ff370SJamie Gritton 	struct vnode *root;
551babbbb9cSJamie Gritton 	char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
552b96bd95bSIan Lepore 	char *g_path, *osrelstr;
5530304c731SJamie Gritton #if defined(INET) || defined(INET6)
55457aea6dfSBjoern A. Zeeb 	struct prison *tppr;
555b38ff370SJamie Gritton 	void *op;
5560304c731SJamie Gritton #endif
55776ca6f88SJamie Gritton 	unsigned long hid;
558b6f47c23SJamie Gritton 	size_t namelen, onamelen, pnamelen;
559cc5fd8c7SJamie Gritton 	int born, created, cuflags, descend, enforce;
560cc5fd8c7SJamie Gritton 	int error, errmsg_len, errmsg_pos;
5610cc207a6SMartin Matuska 	int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
5627cbf7213SJamie Gritton 	int fi, jid, jsys, len, level;
563b96bd95bSIan Lepore 	int childmax, osreldt, rsnum, slevel;
564f6e633a9SMartin Matuska 	int fullpath_disabled;
565d465a41dSBjoern A. Zeeb #if defined(INET) || defined(INET6)
5660304c731SJamie Gritton 	int ii, ij;
567413628a7SBjoern A. Zeeb #endif
568413628a7SBjoern A. Zeeb #ifdef INET
5692b0d6f81SJamie Gritton 	int ip4s, redo_ip4;
570413628a7SBjoern A. Zeeb #endif
571b38ff370SJamie Gritton #ifdef INET6
5722b0d6f81SJamie Gritton 	int ip6s, redo_ip6;
573b38ff370SJamie Gritton #endif
5746beb3bb4SKirk McKusick 	uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
5756beb3bb4SKirk McKusick 	unsigned tallow;
576b38ff370SJamie Gritton 	char numbuf[12];
577b38ff370SJamie Gritton 
578b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_SET);
579b38ff370SJamie Gritton 	if (!error && (flags & JAIL_ATTACH))
580b38ff370SJamie Gritton 		error = priv_check(td, PRIV_JAIL_ATTACH);
581b38ff370SJamie Gritton 	if (error)
58275c13541SPoul-Henning Kamp 		return (error);
583b6f47c23SJamie Gritton 	mypr = td->td_ucred->cr_prison;
584b97457e2SJamie Gritton 	if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
5850304c731SJamie Gritton 		return (EPERM);
586b38ff370SJamie Gritton 	if (flags & ~JAIL_SET_MASK)
587b38ff370SJamie Gritton 		return (EINVAL);
588b38ff370SJamie Gritton 
589b38ff370SJamie Gritton 	/*
590b38ff370SJamie Gritton 	 * Check all the parameters before committing to anything.  Not all
591b38ff370SJamie Gritton 	 * errors can be caught early, but we may as well try.  Also, this
592b38ff370SJamie Gritton 	 * takes care of some expensive stuff (path lookup) before getting
593b38ff370SJamie Gritton 	 * the allprison lock.
594b38ff370SJamie Gritton 	 *
595b38ff370SJamie Gritton 	 * XXX Jails are not filesystems, and jail parameters are not mount
596b38ff370SJamie Gritton 	 *     options.  But it makes more sense to re-use the vfsopt code
597b38ff370SJamie Gritton 	 *     than duplicate it under a different name.
598b38ff370SJamie Gritton 	 */
599b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
600b38ff370SJamie Gritton 	if (error)
601b38ff370SJamie Gritton 		return (error);
602b38ff370SJamie Gritton #ifdef INET
603b38ff370SJamie Gritton 	ip4 = NULL;
604b38ff370SJamie Gritton #endif
605b38ff370SJamie Gritton #ifdef INET6
606b38ff370SJamie Gritton 	ip6 = NULL;
607b38ff370SJamie Gritton #endif
6086dfe0a3dSMartin Matuska 	g_path = NULL;
609b38ff370SJamie Gritton 
610b6f47c23SJamie Gritton 	cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
611b6f47c23SJamie Gritton 	if (!cuflags) {
612b6f47c23SJamie Gritton 		error = EINVAL;
613b6f47c23SJamie Gritton 		vfs_opterror(opts, "no valid operation (create or update)");
614b6f47c23SJamie Gritton 		goto done_errmsg;
615b6f47c23SJamie Gritton 	}
616b6f47c23SJamie Gritton 
617b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
618b38ff370SJamie Gritton 	if (error == ENOENT)
619b38ff370SJamie Gritton 		jid = 0;
620b38ff370SJamie Gritton 	else if (error != 0)
621b38ff370SJamie Gritton 		goto done_free;
622b38ff370SJamie Gritton 
623b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
624b38ff370SJamie Gritton 	if (error == ENOENT)
625b38ff370SJamie Gritton 		gotslevel = 0;
626b38ff370SJamie Gritton 	else if (error != 0)
627b38ff370SJamie Gritton 		goto done_free;
628b38ff370SJamie Gritton 	else
629b38ff370SJamie Gritton 		gotslevel = 1;
630b38ff370SJamie Gritton 
631b97457e2SJamie Gritton 	error =
632b97457e2SJamie Gritton 	    vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
633b97457e2SJamie Gritton 	if (error == ENOENT)
634b97457e2SJamie Gritton 		gotchildmax = 0;
635b97457e2SJamie Gritton 	else if (error != 0)
636b97457e2SJamie Gritton 		goto done_free;
637b97457e2SJamie Gritton 	else
638b97457e2SJamie Gritton 		gotchildmax = 1;
639b97457e2SJamie Gritton 
6400304c731SJamie Gritton 	error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
641f337198dSJamie Gritton 	if (error == ENOENT)
642f337198dSJamie Gritton 		gotenforce = 0;
643f337198dSJamie Gritton 	else if (error != 0)
6440304c731SJamie Gritton 		goto done_free;
645f337198dSJamie Gritton 	else if (enforce < 0 || enforce > 2) {
646f337198dSJamie Gritton 		error = EINVAL;
647f337198dSJamie Gritton 		goto done_free;
648f337198dSJamie Gritton 	} else
649f337198dSJamie Gritton 		gotenforce = 1;
6500304c731SJamie Gritton 
6510cc207a6SMartin Matuska 	error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
6520cc207a6SMartin Matuska 	if (error == ENOENT)
6530cc207a6SMartin Matuska 		gotrsnum = 0;
6540cc207a6SMartin Matuska 	else if (error != 0)
6550cc207a6SMartin Matuska 		goto done_free;
6560cc207a6SMartin Matuska 	else
6570cc207a6SMartin Matuska 		gotrsnum = 1;
6580cc207a6SMartin Matuska 
659b38ff370SJamie Gritton 	pr_flags = ch_flags = 0;
66002abd400SPedro F. Giffuni 	for (fi = 0; fi < nitems(pr_flag_names); fi++) {
6610304c731SJamie Gritton 		if (pr_flag_names[fi] == NULL)
6620304c731SJamie Gritton 			continue;
6630304c731SJamie Gritton 		vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
6640304c731SJamie Gritton 		vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
6650304c731SJamie Gritton 	}
666b38ff370SJamie Gritton 	ch_flags |= pr_flags;
66702abd400SPedro F. Giffuni 	for (fi = 0; fi < nitems(pr_flag_jailsys); fi++) {
6687cbf7213SJamie Gritton 		error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys,
6697cbf7213SJamie Gritton 		    sizeof(jsys));
6707cbf7213SJamie Gritton 		if (error == ENOENT)
6717cbf7213SJamie Gritton 			continue;
6727cbf7213SJamie Gritton 		if (error != 0)
6737cbf7213SJamie Gritton 			goto done_free;
6747cbf7213SJamie Gritton 		switch (jsys) {
6757cbf7213SJamie Gritton 		case JAIL_SYS_DISABLE:
6767cbf7213SJamie Gritton 			if (!pr_flag_jailsys[fi].disable) {
6777cbf7213SJamie Gritton 				error = EINVAL;
6787cbf7213SJamie Gritton 				goto done_free;
6797cbf7213SJamie Gritton 			}
6807cbf7213SJamie Gritton 			pr_flags |= pr_flag_jailsys[fi].disable;
6817cbf7213SJamie Gritton 			break;
6827cbf7213SJamie Gritton 		case JAIL_SYS_NEW:
6837cbf7213SJamie Gritton 			pr_flags |= pr_flag_jailsys[fi].new;
6847cbf7213SJamie Gritton 			break;
6857cbf7213SJamie Gritton 		case JAIL_SYS_INHERIT:
6867cbf7213SJamie Gritton 			break;
6877cbf7213SJamie Gritton 		default:
6887cbf7213SJamie Gritton 			error = EINVAL;
6897cbf7213SJamie Gritton 			goto done_free;
6907cbf7213SJamie Gritton 		}
6917cbf7213SJamie Gritton 		ch_flags |=
6927cbf7213SJamie Gritton 		    pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable;
6937cbf7213SJamie Gritton 	}
6944affa14cSJamie Gritton 	if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
6954affa14cSJamie Gritton 	    && !(pr_flags & PR_PERSIST)) {
6964affa14cSJamie Gritton 		error = EINVAL;
6974affa14cSJamie Gritton 		vfs_opterror(opts, "new jail must persist or attach");
6984affa14cSJamie Gritton 		goto done_errmsg;
6994affa14cSJamie Gritton 	}
700679e1390SJamie Gritton #ifdef VIMAGE
701679e1390SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
702679e1390SJamie Gritton 		error = EINVAL;
703679e1390SJamie Gritton 		vfs_opterror(opts, "vnet cannot be changed after creation");
704679e1390SJamie Gritton 		goto done_errmsg;
705679e1390SJamie Gritton 	}
706679e1390SJamie Gritton #endif
7072b0d6f81SJamie Gritton #ifdef INET
7082b0d6f81SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
7092b0d6f81SJamie Gritton 		error = EINVAL;
7102b0d6f81SJamie Gritton 		vfs_opterror(opts, "ip4 cannot be changed after creation");
7112b0d6f81SJamie Gritton 		goto done_errmsg;
7122b0d6f81SJamie Gritton 	}
7132b0d6f81SJamie Gritton #endif
7142b0d6f81SJamie Gritton #ifdef INET6
7152b0d6f81SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
7162b0d6f81SJamie Gritton 		error = EINVAL;
7172b0d6f81SJamie Gritton 		vfs_opterror(opts, "ip6 cannot be changed after creation");
7182b0d6f81SJamie Gritton 		goto done_errmsg;
7192b0d6f81SJamie Gritton 	}
7202b0d6f81SJamie Gritton #endif
721b38ff370SJamie Gritton 
7220304c731SJamie Gritton 	pr_allow = ch_allow = 0;
72302abd400SPedro F. Giffuni 	for (fi = 0; fi < nitems(pr_allow_names); fi++) {
7240304c731SJamie Gritton 		vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
7250304c731SJamie Gritton 		vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
7260304c731SJamie Gritton 	}
7270304c731SJamie Gritton 	ch_allow |= pr_allow;
7280304c731SJamie Gritton 
729b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
730b38ff370SJamie Gritton 	if (error == ENOENT)
731b38ff370SJamie Gritton 		name = NULL;
732b38ff370SJamie Gritton 	else if (error != 0)
733b38ff370SJamie Gritton 		goto done_free;
734b38ff370SJamie Gritton 	else {
735b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
736b38ff370SJamie Gritton 			error = EINVAL;
737b38ff370SJamie Gritton 			goto done_free;
738b38ff370SJamie Gritton 		}
739b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
740b38ff370SJamie Gritton 			error = ENAMETOOLONG;
741b38ff370SJamie Gritton 			goto done_free;
742b38ff370SJamie Gritton 		}
743b38ff370SJamie Gritton 	}
744b38ff370SJamie Gritton 
745b38ff370SJamie Gritton 	error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
746b38ff370SJamie Gritton 	if (error == ENOENT)
747b38ff370SJamie Gritton 		host = NULL;
748b38ff370SJamie Gritton 	else if (error != 0)
749b38ff370SJamie Gritton 		goto done_free;
750b38ff370SJamie Gritton 	else {
75176ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
75276ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
753b38ff370SJamie Gritton 		if (len == 0 || host[len - 1] != '\0') {
754b38ff370SJamie Gritton 			error = EINVAL;
755b38ff370SJamie Gritton 			goto done_free;
756b38ff370SJamie Gritton 		}
757b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
758b38ff370SJamie Gritton 			error = ENAMETOOLONG;
759b38ff370SJamie Gritton 			goto done_free;
760b38ff370SJamie Gritton 		}
761b38ff370SJamie Gritton 	}
762b38ff370SJamie Gritton 
76376ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
76476ca6f88SJamie Gritton 	if (error == ENOENT)
76576ca6f88SJamie Gritton 		domain = NULL;
76676ca6f88SJamie Gritton 	else if (error != 0)
76776ca6f88SJamie Gritton 		goto done_free;
76876ca6f88SJamie Gritton 	else {
76976ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
77076ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
77176ca6f88SJamie Gritton 		if (len == 0 || domain[len - 1] != '\0') {
77276ca6f88SJamie Gritton 			error = EINVAL;
77376ca6f88SJamie Gritton 			goto done_free;
77476ca6f88SJamie Gritton 		}
77576ca6f88SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
77676ca6f88SJamie Gritton 			error = ENAMETOOLONG;
77776ca6f88SJamie Gritton 			goto done_free;
77876ca6f88SJamie Gritton 		}
77976ca6f88SJamie Gritton 	}
78076ca6f88SJamie Gritton 
78176ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
78276ca6f88SJamie Gritton 	if (error == ENOENT)
78376ca6f88SJamie Gritton 		uuid = NULL;
78476ca6f88SJamie Gritton 	else if (error != 0)
78576ca6f88SJamie Gritton 		goto done_free;
78676ca6f88SJamie Gritton 	else {
78776ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
78876ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
78976ca6f88SJamie Gritton 		if (len == 0 || uuid[len - 1] != '\0') {
79076ca6f88SJamie Gritton 			error = EINVAL;
79176ca6f88SJamie Gritton 			goto done_free;
79276ca6f88SJamie Gritton 		}
79376ca6f88SJamie Gritton 		if (len > HOSTUUIDLEN) {
79476ca6f88SJamie Gritton 			error = ENAMETOOLONG;
79576ca6f88SJamie Gritton 			goto done_free;
79676ca6f88SJamie Gritton 		}
79776ca6f88SJamie Gritton 	}
79876ca6f88SJamie Gritton 
799841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
800a5c1afadSDmitry Chagin 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
80176ca6f88SJamie Gritton 		uint32_t hid32;
80276ca6f88SJamie Gritton 
80376ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
80476ca6f88SJamie Gritton 		hid = hid32;
80576ca6f88SJamie Gritton 	} else
80676ca6f88SJamie Gritton #endif
80776ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
80876ca6f88SJamie Gritton 	if (error == ENOENT)
80976ca6f88SJamie Gritton 		gothid = 0;
81076ca6f88SJamie Gritton 	else if (error != 0)
81176ca6f88SJamie Gritton 		goto done_free;
81276ca6f88SJamie Gritton 	else {
81376ca6f88SJamie Gritton 		gothid = 1;
81476ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
81576ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
81676ca6f88SJamie Gritton 	}
81776ca6f88SJamie Gritton 
818b38ff370SJamie Gritton #ifdef INET
819b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
820b38ff370SJamie Gritton 	if (error == ENOENT)
8210e5e396eSJamie Gritton 		ip4s = 0;
822b38ff370SJamie Gritton 	else if (error != 0)
823b38ff370SJamie Gritton 		goto done_free;
824b38ff370SJamie Gritton 	else if (ip4s & (sizeof(*ip4) - 1)) {
825b38ff370SJamie Gritton 		error = EINVAL;
826b38ff370SJamie Gritton 		goto done_free;
8270304c731SJamie Gritton 	} else {
8286a3f2779SJamie Gritton 		ch_flags |= PR_IP4_USER;
8296a3f2779SJamie Gritton 		pr_flags |= PR_IP4_USER;
8306a3f2779SJamie Gritton 		if (ip4s > 0) {
831b38ff370SJamie Gritton 			ip4s /= sizeof(*ip4);
832b38ff370SJamie Gritton 			if (ip4s > jail_max_af_ips) {
833b38ff370SJamie Gritton 				error = EINVAL;
834b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv4 addresses");
835b38ff370SJamie Gritton 				goto done_errmsg;
836b38ff370SJamie Gritton 			}
8372b0d6f81SJamie Gritton 			ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
838b38ff370SJamie Gritton 			bcopy(op, ip4, ip4s * sizeof(*ip4));
839b38ff370SJamie Gritton 			/*
8400304c731SJamie Gritton 			 * IP addresses are all sorted but ip[0] to preserve
8410304c731SJamie Gritton 			 * the primary IP address as given from userland.
8420304c731SJamie Gritton 			 * This special IP is used for unbound outgoing
843bef916f9SBjoern A. Zeeb 			 * connections as well for "loopback" traffic in case
844bef916f9SBjoern A. Zeeb 			 * source address selection cannot find any more fitting
845bef916f9SBjoern A. Zeeb 			 * address to connect from.
846b38ff370SJamie Gritton 			 */
847b38ff370SJamie Gritton 			if (ip4s > 1)
848b38ff370SJamie Gritton 				qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4);
849b38ff370SJamie Gritton 			/*
8500304c731SJamie Gritton 			 * Check for duplicate addresses and do some simple
8510304c731SJamie Gritton 			 * zero and broadcast checks. If users give other bogus
8520304c731SJamie Gritton 			 * addresses it is their problem.
853b38ff370SJamie Gritton 			 *
8540304c731SJamie Gritton 			 * We do not have to care about byte order for these
8550304c731SJamie Gritton 			 * checks so we will do them in NBO.
856b38ff370SJamie Gritton 			 */
857b38ff370SJamie Gritton 			for (ii = 0; ii < ip4s; ii++) {
858b38ff370SJamie Gritton 				if (ip4[ii].s_addr == INADDR_ANY ||
859b38ff370SJamie Gritton 				    ip4[ii].s_addr == INADDR_BROADCAST) {
860b38ff370SJamie Gritton 					error = EINVAL;
861b38ff370SJamie Gritton 					goto done_free;
862b38ff370SJamie Gritton 				}
863b38ff370SJamie Gritton 				if ((ii+1) < ip4s &&
864b38ff370SJamie Gritton 				    (ip4[0].s_addr == ip4[ii+1].s_addr ||
865b38ff370SJamie Gritton 				     ip4[ii].s_addr == ip4[ii+1].s_addr)) {
866b38ff370SJamie Gritton 					error = EINVAL;
867b38ff370SJamie Gritton 					goto done_free;
868b38ff370SJamie Gritton 				}
869b38ff370SJamie Gritton 			}
870b38ff370SJamie Gritton 		}
8710304c731SJamie Gritton 	}
872b38ff370SJamie Gritton #endif
873b38ff370SJamie Gritton 
874b38ff370SJamie Gritton #ifdef INET6
875b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
876b38ff370SJamie Gritton 	if (error == ENOENT)
8770e5e396eSJamie Gritton 		ip6s = 0;
878b38ff370SJamie Gritton 	else if (error != 0)
879b38ff370SJamie Gritton 		goto done_free;
880b38ff370SJamie Gritton 	else if (ip6s & (sizeof(*ip6) - 1)) {
881b38ff370SJamie Gritton 		error = EINVAL;
882b38ff370SJamie Gritton 		goto done_free;
8830304c731SJamie Gritton 	} else {
8846a3f2779SJamie Gritton 		ch_flags |= PR_IP6_USER;
8856a3f2779SJamie Gritton 		pr_flags |= PR_IP6_USER;
8866a3f2779SJamie Gritton 		if (ip6s > 0) {
887b38ff370SJamie Gritton 			ip6s /= sizeof(*ip6);
888b38ff370SJamie Gritton 			if (ip6s > jail_max_af_ips) {
889b38ff370SJamie Gritton 				error = EINVAL;
890b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv6 addresses");
891b38ff370SJamie Gritton 				goto done_errmsg;
892b38ff370SJamie Gritton 			}
8932b0d6f81SJamie Gritton 			ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
894b38ff370SJamie Gritton 			bcopy(op, ip6, ip6s * sizeof(*ip6));
895b38ff370SJamie Gritton 			if (ip6s > 1)
896b38ff370SJamie Gritton 				qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6);
897b38ff370SJamie Gritton 			for (ii = 0; ii < ip6s; ii++) {
8980304c731SJamie Gritton 				if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
899b38ff370SJamie Gritton 					error = EINVAL;
900b38ff370SJamie Gritton 					goto done_free;
901b38ff370SJamie Gritton 				}
902b38ff370SJamie Gritton 				if ((ii+1) < ip6s &&
903b38ff370SJamie Gritton 				    (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
904b38ff370SJamie Gritton 				     IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
905b38ff370SJamie Gritton 				{
906b38ff370SJamie Gritton 					error = EINVAL;
907b38ff370SJamie Gritton 					goto done_free;
908b38ff370SJamie Gritton 				}
909b38ff370SJamie Gritton 			}
910b38ff370SJamie Gritton 		}
9110304c731SJamie Gritton 	}
912b38ff370SJamie Gritton #endif
913b38ff370SJamie Gritton 
914bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6))
915bdfc8cc4SJamie Gritton 	if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
916bdfc8cc4SJamie Gritton 		error = EINVAL;
917bdfc8cc4SJamie Gritton 		vfs_opterror(opts,
918bdfc8cc4SJamie Gritton 		    "vnet jails cannot have IP address restrictions");
919bdfc8cc4SJamie Gritton 		goto done_errmsg;
920bdfc8cc4SJamie Gritton 	}
921bdfc8cc4SJamie Gritton #endif
922bdfc8cc4SJamie Gritton 
9239cbe30e1SMartin Matuska 	fullpath_disabled = 0;
924b38ff370SJamie Gritton 	root = NULL;
925b38ff370SJamie Gritton 	error = vfs_getopt(opts, "path", (void **)&path, &len);
926b38ff370SJamie Gritton 	if (error == ENOENT)
927b38ff370SJamie Gritton 		path = NULL;
928b38ff370SJamie Gritton 	else if (error != 0)
929b38ff370SJamie Gritton 		goto done_free;
930b38ff370SJamie Gritton 	else {
931b38ff370SJamie Gritton 		if (flags & JAIL_UPDATE) {
932b38ff370SJamie Gritton 			error = EINVAL;
933b38ff370SJamie Gritton 			vfs_opterror(opts,
934b38ff370SJamie Gritton 			    "path cannot be changed after creation");
935b38ff370SJamie Gritton 			goto done_errmsg;
936b38ff370SJamie Gritton 		}
937b38ff370SJamie Gritton 		if (len == 0 || path[len - 1] != '\0') {
938b38ff370SJamie Gritton 			error = EINVAL;
939b38ff370SJamie Gritton 			goto done_free;
940b38ff370SJamie Gritton 		}
9415050aa86SKonstantin Belousov 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
942b38ff370SJamie Gritton 		    path, td);
943b38ff370SJamie Gritton 		error = namei(&nd);
944b38ff370SJamie Gritton 		if (error)
945b38ff370SJamie Gritton 			goto done_free;
946b38ff370SJamie Gritton 		root = nd.ni_vp;
947b38ff370SJamie Gritton 		NDFREE(&nd, NDF_ONLY_PNBUF);
9486dfe0a3dSMartin Matuska 		g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
9496dfe0a3dSMartin Matuska 		strlcpy(g_path, path, MAXPATHLEN);
9506dfe0a3dSMartin Matuska 		error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
9516dfe0a3dSMartin Matuska 		if (error == 0)
9526dfe0a3dSMartin Matuska 			path = g_path;
9536dfe0a3dSMartin Matuska 		else if (error == ENODEV) {
954f6e633a9SMartin Matuska 			/* proceed if sysctl debug.disablefullpath == 1 */
955f6e633a9SMartin Matuska 			fullpath_disabled = 1;
956f6e633a9SMartin Matuska 			if (len < 2 || (len == 2 && path[0] == '/'))
957f6e633a9SMartin Matuska 				path = NULL;
9586dfe0a3dSMartin Matuska 		} else {
959f6e633a9SMartin Matuska 			/* exit on other errors */
960b38ff370SJamie Gritton 			goto done_free;
961b38ff370SJamie Gritton 		}
962f6e633a9SMartin Matuska 		if (root->v_type != VDIR) {
963f6e633a9SMartin Matuska 			error = ENOTDIR;
964f6e633a9SMartin Matuska 			vput(root);
965f6e633a9SMartin Matuska 			goto done_free;
966f6e633a9SMartin Matuska 		}
967f6e633a9SMartin Matuska 		VOP_UNLOCK(root, 0);
968f6e633a9SMartin Matuska 		if (fullpath_disabled) {
969f6e633a9SMartin Matuska 			/* Leave room for a real-root full pathname. */
970f6e633a9SMartin Matuska 			if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
971f6e633a9SMartin Matuska 			    ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
972f6e633a9SMartin Matuska 				error = ENAMETOOLONG;
973f6e633a9SMartin Matuska 				goto done_free;
974f6e633a9SMartin Matuska 			}
975b38ff370SJamie Gritton 		}
976b38ff370SJamie Gritton 	}
977b38ff370SJamie Gritton 
978b96bd95bSIan Lepore 	error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len);
979b96bd95bSIan Lepore 	if (error == ENOENT)
980b96bd95bSIan Lepore 		osrelstr = NULL;
981b96bd95bSIan Lepore 	else if (error != 0)
982b96bd95bSIan Lepore 		goto done_free;
983b96bd95bSIan Lepore 	else {
984b96bd95bSIan Lepore 		if (flags & JAIL_UPDATE) {
985b96bd95bSIan Lepore 			error = EINVAL;
986b96bd95bSIan Lepore 			vfs_opterror(opts,
987b96bd95bSIan Lepore 			    "osrelease cannot be changed after creation");
988b96bd95bSIan Lepore 			goto done_errmsg;
989b96bd95bSIan Lepore 		}
990b96bd95bSIan Lepore 		if (len == 0 || len >= OSRELEASELEN) {
991b96bd95bSIan Lepore 			error = EINVAL;
992b96bd95bSIan Lepore 			vfs_opterror(opts,
993b96bd95bSIan Lepore 			    "osrelease string must be 1-%d bytes long",
994b96bd95bSIan Lepore 			    OSRELEASELEN - 1);
995b96bd95bSIan Lepore 			goto done_errmsg;
996b96bd95bSIan Lepore 		}
997b96bd95bSIan Lepore 	}
998b96bd95bSIan Lepore 
999b96bd95bSIan Lepore 	error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt));
1000b96bd95bSIan Lepore 	if (error == ENOENT)
1001b96bd95bSIan Lepore 		osreldt = 0;
1002b96bd95bSIan Lepore 	else if (error != 0)
1003b96bd95bSIan Lepore 		goto done_free;
1004b96bd95bSIan Lepore 	else {
1005b96bd95bSIan Lepore 		if (flags & JAIL_UPDATE) {
1006b96bd95bSIan Lepore 			error = EINVAL;
1007b96bd95bSIan Lepore 			vfs_opterror(opts,
1008b96bd95bSIan Lepore 			    "osreldate cannot be changed after creation");
1009b96bd95bSIan Lepore 			goto done_errmsg;
1010b96bd95bSIan Lepore 		}
1011b96bd95bSIan Lepore 		if (osreldt == 0) {
1012b96bd95bSIan Lepore 			error = EINVAL;
1013b96bd95bSIan Lepore 			vfs_opterror(opts, "osreldate cannot be 0");
1014b96bd95bSIan Lepore 			goto done_errmsg;
1015b96bd95bSIan Lepore 		}
1016b96bd95bSIan Lepore 	}
1017b96bd95bSIan Lepore 
1018b38ff370SJamie Gritton 	/*
1019b6f47c23SJamie Gritton 	 * Find the specified jail, or at least its parent.
1020b38ff370SJamie Gritton 	 * This abuses the file error codes ENOENT and EEXIST.
1021b38ff370SJamie Gritton 	 */
1022b38ff370SJamie Gritton 	pr = NULL;
1023b6f47c23SJamie Gritton 	ppr = mypr;
1024babbbb9cSJamie Gritton 	if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
1025babbbb9cSJamie Gritton 		namelc = strrchr(name, '.');
1026babbbb9cSJamie Gritton 		jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
1027babbbb9cSJamie Gritton 		if (*p != '\0')
1028babbbb9cSJamie Gritton 			jid = 0;
1029babbbb9cSJamie Gritton 	}
1030b6f47c23SJamie Gritton 	sx_xlock(&allprison_lock);
1031b38ff370SJamie Gritton 	if (jid != 0) {
10320304c731SJamie Gritton 		/*
10330304c731SJamie Gritton 		 * See if a requested jid already exists.  There is an
10340304c731SJamie Gritton 		 * information leak here if the jid exists but is not within
10350304c731SJamie Gritton 		 * the caller's jail hierarchy.  Jail creators will get EEXIST
10360304c731SJamie Gritton 		 * even though they cannot see the jail, and CREATE | UPDATE
10370304c731SJamie Gritton 		 * will return ENOENT which is not normally a valid error.
10380304c731SJamie Gritton 		 */
1039b38ff370SJamie Gritton 		if (jid < 0) {
1040b38ff370SJamie Gritton 			error = EINVAL;
1041b38ff370SJamie Gritton 			vfs_opterror(opts, "negative jid");
1042b38ff370SJamie Gritton 			goto done_unlock_list;
1043b38ff370SJamie Gritton 		}
1044b38ff370SJamie Gritton 		pr = prison_find(jid);
1045b38ff370SJamie Gritton 		if (pr != NULL) {
10460304c731SJamie Gritton 			ppr = pr->pr_parent;
1047b38ff370SJamie Gritton 			/* Create: jid must not exist. */
1048b38ff370SJamie Gritton 			if (cuflags == JAIL_CREATE) {
1049b38ff370SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
1050b38ff370SJamie Gritton 				error = EEXIST;
1051b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d already exists",
1052b38ff370SJamie Gritton 				    jid);
1053b38ff370SJamie Gritton 				goto done_unlock_list;
1054b38ff370SJamie Gritton 			}
10550304c731SJamie Gritton 			if (!prison_ischild(mypr, pr)) {
10560304c731SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
10570304c731SJamie Gritton 				pr = NULL;
10580304c731SJamie Gritton 			} else if (pr->pr_uref == 0) {
1059b38ff370SJamie Gritton 				if (!(flags & JAIL_DYING)) {
1060b38ff370SJamie Gritton 					mtx_unlock(&pr->pr_mtx);
1061b38ff370SJamie Gritton 					error = ENOENT;
1062b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
1063b38ff370SJamie Gritton 					    jid);
1064b38ff370SJamie Gritton 					goto done_unlock_list;
1065b38ff370SJamie Gritton 				} else if ((flags & JAIL_ATTACH) ||
1066b38ff370SJamie Gritton 				    (pr_flags & PR_PERSIST)) {
1067b38ff370SJamie Gritton 					/*
1068b38ff370SJamie Gritton 					 * A dying jail might be resurrected
1069b38ff370SJamie Gritton 					 * (via attach or persist), but first
1070b38ff370SJamie Gritton 					 * it must determine if another jail
1071b38ff370SJamie Gritton 					 * has claimed its name.  Accomplish
1072b38ff370SJamie Gritton 					 * this by implicitly re-setting the
1073b38ff370SJamie Gritton 					 * name.
1074b38ff370SJamie Gritton 					 */
1075b38ff370SJamie Gritton 					if (name == NULL)
10760304c731SJamie Gritton 						name = prison_name(mypr, pr);
1077b38ff370SJamie Gritton 				}
1078b38ff370SJamie Gritton 			}
1079b38ff370SJamie Gritton 		}
1080b38ff370SJamie Gritton 		if (pr == NULL) {
1081b38ff370SJamie Gritton 			/* Update: jid must exist. */
1082b38ff370SJamie Gritton 			if (cuflags == JAIL_UPDATE) {
1083b38ff370SJamie Gritton 				error = ENOENT;
1084b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d not found", jid);
1085b38ff370SJamie Gritton 				goto done_unlock_list;
1086b38ff370SJamie Gritton 			}
1087b38ff370SJamie Gritton 		}
1088b38ff370SJamie Gritton 	}
1089b38ff370SJamie Gritton 	/*
1090b38ff370SJamie Gritton 	 * If the caller provided a name, look for a jail by that name.
1091b38ff370SJamie Gritton 	 * This has different semantics for creates and updates keyed by jid
1092b38ff370SJamie Gritton 	 * (where the name must not already exist in a different jail),
1093b38ff370SJamie Gritton 	 * and updates keyed by the name itself (where the name must exist
1094b38ff370SJamie Gritton 	 * because that is the jail being updated).
1095b38ff370SJamie Gritton 	 */
1096b6f47c23SJamie Gritton 	namelc = NULL;
1097b38ff370SJamie Gritton 	if (name != NULL) {
1098babbbb9cSJamie Gritton 		namelc = strrchr(name, '.');
1099babbbb9cSJamie Gritton 		if (namelc == NULL)
1100babbbb9cSJamie Gritton 			namelc = name;
1101babbbb9cSJamie Gritton 		else {
11020304c731SJamie Gritton 			/*
11030304c731SJamie Gritton 			 * This is a hierarchical name.  Split it into the
11040304c731SJamie Gritton 			 * parent and child names, and make sure the parent
11050304c731SJamie Gritton 			 * exists or matches an already found jail.
11060304c731SJamie Gritton 			 */
11070304c731SJamie Gritton 			if (pr != NULL) {
1108babbbb9cSJamie Gritton 				if (strncmp(name, ppr->pr_name, namelc - name)
1109babbbb9cSJamie Gritton 				    || ppr->pr_name[namelc - name] != '\0') {
11100304c731SJamie Gritton 					mtx_unlock(&pr->pr_mtx);
11110304c731SJamie Gritton 					error = EINVAL;
11120304c731SJamie Gritton 					vfs_opterror(opts,
11130304c731SJamie Gritton 					    "cannot change jail's parent");
11140304c731SJamie Gritton 					goto done_unlock_list;
11150304c731SJamie Gritton 				}
11160304c731SJamie Gritton 			} else {
1117b6f47c23SJamie Gritton 				*namelc = '\0';
11180304c731SJamie Gritton 				ppr = prison_find_name(mypr, name);
11190304c731SJamie Gritton 				if (ppr == NULL) {
11200304c731SJamie Gritton 					error = ENOENT;
11210304c731SJamie Gritton 					vfs_opterror(opts,
11220304c731SJamie Gritton 					    "jail \"%s\" not found", name);
11230304c731SJamie Gritton 					goto done_unlock_list;
11240304c731SJamie Gritton 				}
11250304c731SJamie Gritton 				mtx_unlock(&ppr->pr_mtx);
1126b6f47c23SJamie Gritton 				*namelc = '.';
11270304c731SJamie Gritton 			}
1128b6f47c23SJamie Gritton 			namelc++;
11290304c731SJamie Gritton 		}
1130b6f47c23SJamie Gritton 		if (namelc[0] != '\0') {
1131b6f47c23SJamie Gritton 			pnamelen =
11320304c731SJamie Gritton 			    (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1133b38ff370SJamie Gritton  name_again:
11340304c731SJamie Gritton 			deadpr = NULL;
11350304c731SJamie Gritton 			FOREACH_PRISON_CHILD(ppr, tpr) {
1136b38ff370SJamie Gritton 				if (tpr != pr && tpr->pr_ref > 0 &&
1137b6f47c23SJamie Gritton 				    !strcmp(tpr->pr_name + pnamelen, namelc)) {
1138b38ff370SJamie Gritton 					if (pr == NULL &&
1139b38ff370SJamie Gritton 					    cuflags != JAIL_CREATE) {
1140b38ff370SJamie Gritton 						mtx_lock(&tpr->pr_mtx);
1141b38ff370SJamie Gritton 						if (tpr->pr_ref > 0) {
1142b38ff370SJamie Gritton 							/*
1143b38ff370SJamie Gritton 							 * Use this jail
1144b38ff370SJamie Gritton 							 * for updates.
1145b38ff370SJamie Gritton 							 */
1146b38ff370SJamie Gritton 							if (tpr->pr_uref > 0) {
1147b38ff370SJamie Gritton 								pr = tpr;
1148b38ff370SJamie Gritton 								break;
1149b38ff370SJamie Gritton 							}
1150b38ff370SJamie Gritton 							deadpr = tpr;
1151b38ff370SJamie Gritton 						}
1152b38ff370SJamie Gritton 						mtx_unlock(&tpr->pr_mtx);
1153b38ff370SJamie Gritton 					} else if (tpr->pr_uref > 0) {
1154b38ff370SJamie Gritton 						/*
1155b38ff370SJamie Gritton 						 * Create, or update(jid):
1156b38ff370SJamie Gritton 						 * name must not exist in an
11570304c731SJamie Gritton 						 * active sibling jail.
1158b38ff370SJamie Gritton 						 */
1159b38ff370SJamie Gritton 						error = EEXIST;
1160b38ff370SJamie Gritton 						if (pr != NULL)
1161b38ff370SJamie Gritton 							mtx_unlock(&pr->pr_mtx);
1162b38ff370SJamie Gritton 						vfs_opterror(opts,
1163b38ff370SJamie Gritton 						   "jail \"%s\" already exists",
1164b38ff370SJamie Gritton 						   name);
1165b38ff370SJamie Gritton 						goto done_unlock_list;
1166b38ff370SJamie Gritton 					}
1167b38ff370SJamie Gritton 				}
1168b38ff370SJamie Gritton 			}
1169b38ff370SJamie Gritton 			/* If no active jail is found, use a dying one. */
1170b38ff370SJamie Gritton 			if (deadpr != NULL && pr == NULL) {
1171b38ff370SJamie Gritton 				if (flags & JAIL_DYING) {
1172b38ff370SJamie Gritton 					mtx_lock(&deadpr->pr_mtx);
1173b38ff370SJamie Gritton 					if (deadpr->pr_ref == 0) {
1174b38ff370SJamie Gritton 						mtx_unlock(&deadpr->pr_mtx);
1175b38ff370SJamie Gritton 						goto name_again;
1176b38ff370SJamie Gritton 					}
1177b38ff370SJamie Gritton 					pr = deadpr;
1178b38ff370SJamie Gritton 				} else if (cuflags == JAIL_UPDATE) {
1179b38ff370SJamie Gritton 					error = ENOENT;
1180b38ff370SJamie Gritton 					vfs_opterror(opts,
1181b38ff370SJamie Gritton 					    "jail \"%s\" is dying", name);
1182b38ff370SJamie Gritton 					goto done_unlock_list;
1183b38ff370SJamie Gritton 				}
1184b38ff370SJamie Gritton 			}
1185b38ff370SJamie Gritton 			/* Update: name must exist if no jid. */
1186b38ff370SJamie Gritton 			else if (cuflags == JAIL_UPDATE && pr == NULL) {
1187b38ff370SJamie Gritton 				error = ENOENT;
1188b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" not found",
1189b38ff370SJamie Gritton 				    name);
1190b38ff370SJamie Gritton 				goto done_unlock_list;
1191b38ff370SJamie Gritton 			}
1192b38ff370SJamie Gritton 		}
1193b38ff370SJamie Gritton 	}
1194b38ff370SJamie Gritton 	/* Update: must provide a jid or name. */
1195b38ff370SJamie Gritton 	else if (cuflags == JAIL_UPDATE && pr == NULL) {
1196b38ff370SJamie Gritton 		error = ENOENT;
1197b38ff370SJamie Gritton 		vfs_opterror(opts, "update specified no jail");
1198b38ff370SJamie Gritton 		goto done_unlock_list;
1199b38ff370SJamie Gritton 	}
1200b38ff370SJamie Gritton 
1201b38ff370SJamie Gritton 	/* If there's no prison to update, create a new one and link it in. */
1202b38ff370SJamie Gritton 	if (pr == NULL) {
1203b97457e2SJamie Gritton 		for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1204b97457e2SJamie Gritton 			if (tpr->pr_childcount >= tpr->pr_childmax) {
1205b97457e2SJamie Gritton 				error = EPERM;
1206b97457e2SJamie Gritton 				vfs_opterror(opts, "prison limit exceeded");
1207b97457e2SJamie Gritton 				goto done_unlock_list;
1208b97457e2SJamie Gritton 			}
1209b38ff370SJamie Gritton 		created = 1;
12100304c731SJamie Gritton 		mtx_lock(&ppr->pr_mtx);
12112a549507SJamie Gritton 		if (ppr->pr_ref == 0) {
12120304c731SJamie Gritton 			mtx_unlock(&ppr->pr_mtx);
12130304c731SJamie Gritton 			error = ENOENT;
1214b6f47c23SJamie Gritton 			vfs_opterror(opts, "jail \"%s\" not found",
1215b6f47c23SJamie Gritton 			    prison_name(mypr, ppr));
12160304c731SJamie Gritton 			goto done_unlock_list;
12170304c731SJamie Gritton 		}
12180304c731SJamie Gritton 		ppr->pr_ref++;
12190304c731SJamie Gritton 		ppr->pr_uref++;
12200304c731SJamie Gritton 		mtx_unlock(&ppr->pr_mtx);
1221b38ff370SJamie Gritton 		pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1222b38ff370SJamie Gritton 		if (jid == 0) {
1223b38ff370SJamie Gritton 			/* Find the next free jid. */
1224b38ff370SJamie Gritton 			jid = lastprid + 1;
1225b38ff370SJamie Gritton  findnext:
1226b38ff370SJamie Gritton 			if (jid == JAIL_MAX)
1227b38ff370SJamie Gritton 				jid = 1;
1228b38ff370SJamie Gritton 			TAILQ_FOREACH(tpr, &allprison, pr_list) {
1229b38ff370SJamie Gritton 				if (tpr->pr_id < jid)
1230b38ff370SJamie Gritton 					continue;
1231b38ff370SJamie Gritton 				if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1232b38ff370SJamie Gritton 					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1233b38ff370SJamie Gritton 					break;
1234b38ff370SJamie Gritton 				}
1235b38ff370SJamie Gritton 				if (jid == lastprid) {
1236b38ff370SJamie Gritton 					error = EAGAIN;
1237b38ff370SJamie Gritton 					vfs_opterror(opts,
1238b38ff370SJamie Gritton 					    "no available jail IDs");
1239b38ff370SJamie Gritton 					free(pr, M_PRISON);
12400304c731SJamie Gritton 					prison_deref(ppr, PD_DEREF |
12410304c731SJamie Gritton 					    PD_DEUREF | PD_LIST_XLOCKED);
12420304c731SJamie Gritton 					goto done_releroot;
1243b38ff370SJamie Gritton 				}
1244b38ff370SJamie Gritton 				jid++;
1245b38ff370SJamie Gritton 				goto findnext;
1246b38ff370SJamie Gritton 			}
1247b38ff370SJamie Gritton 			lastprid = jid;
1248b38ff370SJamie Gritton 		} else {
1249b38ff370SJamie Gritton 			/*
1250b38ff370SJamie Gritton 			 * The jail already has a jid (that did not yet exist),
1251b38ff370SJamie Gritton 			 * so just find where to insert it.
1252b38ff370SJamie Gritton 			 */
1253b38ff370SJamie Gritton 			TAILQ_FOREACH(tpr, &allprison, pr_list)
1254b38ff370SJamie Gritton 				if (tpr->pr_id >= jid) {
1255b38ff370SJamie Gritton 					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1256b38ff370SJamie Gritton 					break;
1257b38ff370SJamie Gritton 				}
1258b38ff370SJamie Gritton 		}
1259b38ff370SJamie Gritton 		if (tpr == NULL)
1260b38ff370SJamie Gritton 			TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
12610304c731SJamie Gritton 		LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
12620304c731SJamie Gritton 		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1263b97457e2SJamie Gritton 			tpr->pr_childcount++;
1264b38ff370SJamie Gritton 
12650304c731SJamie Gritton 		pr->pr_parent = ppr;
1266b38ff370SJamie Gritton 		pr->pr_id = jid;
12670304c731SJamie Gritton 
12680304c731SJamie Gritton 		/* Set some default values, and inherit some from the parent. */
1269b6f47c23SJamie Gritton 		if (namelc == NULL)
1270b6f47c23SJamie Gritton 			namelc = "";
1271b38ff370SJamie Gritton 		if (path == NULL) {
1272b38ff370SJamie Gritton 			path = "/";
12730304c731SJamie Gritton 			root = mypr->pr_root;
1274b38ff370SJamie Gritton 			vref(root);
1275b38ff370SJamie Gritton 		}
12768986e3a0SJamie Gritton 		strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
12778986e3a0SJamie Gritton 		pr->pr_flags |= PR_HOST;
1278bdfc8cc4SJamie Gritton #if defined(INET) || defined(INET6)
1279bdfc8cc4SJamie Gritton #ifdef VIMAGE
1280bdfc8cc4SJamie Gritton 		if (!(pr_flags & PR_VNET))
1281bdfc8cc4SJamie Gritton #endif
1282bdfc8cc4SJamie Gritton 		{
12830304c731SJamie Gritton #ifdef INET
12842b0d6f81SJamie Gritton 			if (!(ch_flags & PR_IP4_USER))
12856a3f2779SJamie Gritton 				pr->pr_flags |= PR_IP4 | PR_IP4_USER;
12862b0d6f81SJamie Gritton 			else if (!(pr_flags & PR_IP4_USER)) {
12872b0d6f81SJamie Gritton 				pr->pr_flags |= ppr->pr_flags & PR_IP4;
12882b0d6f81SJamie Gritton 				if (ppr->pr_ip4 != NULL) {
12892b0d6f81SJamie Gritton 					pr->pr_ip4s = ppr->pr_ip4s;
12902b0d6f81SJamie Gritton 					pr->pr_ip4 = malloc(pr->pr_ip4s *
12912b0d6f81SJamie Gritton 					    sizeof(struct in_addr), M_PRISON,
12922b0d6f81SJamie Gritton 					    M_WAITOK);
12932b0d6f81SJamie Gritton 					bcopy(ppr->pr_ip4, pr->pr_ip4,
12942b0d6f81SJamie Gritton 					    pr->pr_ip4s * sizeof(*pr->pr_ip4));
12952b0d6f81SJamie Gritton 				}
12962b0d6f81SJamie Gritton 			}
12970304c731SJamie Gritton #endif
12980304c731SJamie Gritton #ifdef INET6
12992b0d6f81SJamie Gritton 			if (!(ch_flags & PR_IP6_USER))
13006a3f2779SJamie Gritton 				pr->pr_flags |= PR_IP6 | PR_IP6_USER;
13012b0d6f81SJamie Gritton 			else if (!(pr_flags & PR_IP6_USER)) {
13022b0d6f81SJamie Gritton 				pr->pr_flags |= ppr->pr_flags & PR_IP6;
13032b0d6f81SJamie Gritton 				if (ppr->pr_ip6 != NULL) {
13042b0d6f81SJamie Gritton 					pr->pr_ip6s = ppr->pr_ip6s;
13052b0d6f81SJamie Gritton 					pr->pr_ip6 = malloc(pr->pr_ip6s *
13062b0d6f81SJamie Gritton 					    sizeof(struct in6_addr), M_PRISON,
13072b0d6f81SJamie Gritton 					    M_WAITOK);
13082b0d6f81SJamie Gritton 					bcopy(ppr->pr_ip6, pr->pr_ip6,
13092b0d6f81SJamie Gritton 					    pr->pr_ip6s * sizeof(*pr->pr_ip6));
13102b0d6f81SJamie Gritton 				}
13112b0d6f81SJamie Gritton 			}
13120304c731SJamie Gritton #endif
1313bdfc8cc4SJamie Gritton 		}
1314bdfc8cc4SJamie Gritton #endif
1315592bcae8SBjoern A. Zeeb 		/* Source address selection is always on by default. */
1316592bcae8SBjoern A. Zeeb 		pr->pr_flags |= _PR_IP_SADDRSEL;
1317592bcae8SBjoern A. Zeeb 
13180304c731SJamie Gritton 		pr->pr_securelevel = ppr->pr_securelevel;
13190304c731SJamie Gritton 		pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1320af10bf05SBjoern A. Zeeb 		pr->pr_enforce_statfs = jail_default_enforce_statfs;
1321bf3db8aaSMartin Matuska 		pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1322b38ff370SJamie Gritton 
1323b96bd95bSIan Lepore 		pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate;
1324b96bd95bSIan Lepore 		if (osrelstr == NULL)
1325b96bd95bSIan Lepore 		    strcpy(pr->pr_osrelease, ppr->pr_osrelease);
1326b96bd95bSIan Lepore 		else
1327b96bd95bSIan Lepore 		    strcpy(pr->pr_osrelease, osrelstr);
1328b96bd95bSIan Lepore 
13290304c731SJamie Gritton 		LIST_INIT(&pr->pr_children);
13300304c731SJamie Gritton 		mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
133173d9e52dSJamie Gritton 		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
1332b38ff370SJamie Gritton 
1333679e1390SJamie Gritton #ifdef VIMAGE
1334679e1390SJamie Gritton 		/* Allocate a new vnet if specified. */
1335679e1390SJamie Gritton 		pr->pr_vnet = (pr_flags & PR_VNET)
1336679e1390SJamie Gritton 		    ? vnet_alloc() : ppr->pr_vnet;
1337679e1390SJamie Gritton #endif
1338b38ff370SJamie Gritton 		/*
1339b38ff370SJamie Gritton 		 * Allocate a dedicated cpuset for each jail.
1340b38ff370SJamie Gritton 		 * Unlike other initial settings, this may return an erorr.
1341b38ff370SJamie Gritton 		 */
13420304c731SJamie Gritton 		error = cpuset_create_root(ppr, &pr->pr_cpuset);
1343b38ff370SJamie Gritton 		if (error) {
1344b38ff370SJamie Gritton 			prison_deref(pr, PD_LIST_XLOCKED);
1345b38ff370SJamie Gritton 			goto done_releroot;
1346b38ff370SJamie Gritton 		}
1347b38ff370SJamie Gritton 
1348b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
1349b38ff370SJamie Gritton 		/*
1350b38ff370SJamie Gritton 		 * New prisons do not yet have a reference, because we do not
1351b6f47c23SJamie Gritton 		 * want others to see the incomplete prison once the
1352b38ff370SJamie Gritton 		 * allprison_lock is downgraded.
1353b38ff370SJamie Gritton 		 */
1354b38ff370SJamie Gritton 	} else {
1355b38ff370SJamie Gritton 		created = 0;
13562b0d6f81SJamie Gritton 		/*
13572b0d6f81SJamie Gritton 		 * Grab a reference for existing prisons, to ensure they
13582b0d6f81SJamie Gritton 		 * continue to exist for the duration of the call.
13592b0d6f81SJamie Gritton 		 */
13602b0d6f81SJamie Gritton 		pr->pr_ref++;
1361bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1362bdfc8cc4SJamie Gritton 		if ((pr->pr_flags & PR_VNET) &&
1363bdfc8cc4SJamie Gritton 		    (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1364bdfc8cc4SJamie Gritton 			error = EINVAL;
1365bdfc8cc4SJamie Gritton 			vfs_opterror(opts,
1366bdfc8cc4SJamie Gritton 			    "vnet jails cannot have IP address restrictions");
1367bdfc8cc4SJamie Gritton 			goto done_deref_locked;
1368bdfc8cc4SJamie Gritton 		}
1369bdfc8cc4SJamie Gritton #endif
13702b0d6f81SJamie Gritton #ifdef INET
13712b0d6f81SJamie Gritton 		if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
13722b0d6f81SJamie Gritton 			error = EINVAL;
13732b0d6f81SJamie Gritton 			vfs_opterror(opts,
13742b0d6f81SJamie Gritton 			    "ip4 cannot be changed after creation");
13752b0d6f81SJamie Gritton 			goto done_deref_locked;
13762b0d6f81SJamie Gritton 		}
13772b0d6f81SJamie Gritton #endif
13782b0d6f81SJamie Gritton #ifdef INET6
13792b0d6f81SJamie Gritton 		if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
13802b0d6f81SJamie Gritton 			error = EINVAL;
13812b0d6f81SJamie Gritton 			vfs_opterror(opts,
13822b0d6f81SJamie Gritton 			    "ip6 cannot be changed after creation");
13832b0d6f81SJamie Gritton 			goto done_deref_locked;
13842b0d6f81SJamie Gritton 		}
13852b0d6f81SJamie Gritton #endif
1386b38ff370SJamie Gritton 	}
1387b38ff370SJamie Gritton 
1388b38ff370SJamie Gritton 	/* Do final error checking before setting anything. */
13890304c731SJamie Gritton 	if (gotslevel) {
13900304c731SJamie Gritton 		if (slevel < ppr->pr_securelevel) {
13910304c731SJamie Gritton 			error = EPERM;
13920304c731SJamie Gritton 			goto done_deref_locked;
13930304c731SJamie Gritton 		}
13940304c731SJamie Gritton 	}
1395b97457e2SJamie Gritton 	if (gotchildmax) {
1396b97457e2SJamie Gritton 		if (childmax >= ppr->pr_childmax) {
1397b97457e2SJamie Gritton 			error = EPERM;
1398b97457e2SJamie Gritton 			goto done_deref_locked;
1399b97457e2SJamie Gritton 		}
1400b97457e2SJamie Gritton 	}
14010304c731SJamie Gritton 	if (gotenforce) {
14020304c731SJamie Gritton 		if (enforce < ppr->pr_enforce_statfs) {
14030304c731SJamie Gritton 			error = EPERM;
14040304c731SJamie Gritton 			goto done_deref_locked;
14050304c731SJamie Gritton 		}
14060304c731SJamie Gritton 	}
14070cc207a6SMartin Matuska 	if (gotrsnum) {
14080cc207a6SMartin Matuska 		/*
14090cc207a6SMartin Matuska 		 * devfs_rsnum is a uint16_t
14100cc207a6SMartin Matuska 		 */
1411bf3db8aaSMartin Matuska 		if (rsnum < 0 || rsnum > 65535) {
14120cc207a6SMartin Matuska 			error = EINVAL;
14130cc207a6SMartin Matuska 			goto done_deref_locked;
14140cc207a6SMartin Matuska 		}
14150cc207a6SMartin Matuska 		/*
1416bf3db8aaSMartin Matuska 		 * Nested jails always inherit parent's devfs ruleset
14170cc207a6SMartin Matuska 		 */
14180cc207a6SMartin Matuska 		if (jailed(td->td_ucred)) {
14190cc207a6SMartin Matuska 			if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
14200cc207a6SMartin Matuska 				error = EPERM;
14210cc207a6SMartin Matuska 				goto done_deref_locked;
1422bf3db8aaSMartin Matuska 			} else
14230cc207a6SMartin Matuska 				rsnum = ppr->pr_devfs_rsnum;
14240cc207a6SMartin Matuska 		}
14250cc207a6SMartin Matuska 	}
1426b38ff370SJamie Gritton #ifdef INET
14272b0d6f81SJamie Gritton 	if (ip4s > 0) {
14280304c731SJamie Gritton 		if (ppr->pr_flags & PR_IP4) {
14290304c731SJamie Gritton 			/*
14300304c731SJamie Gritton 			 * Make sure the new set of IP addresses is a
14310304c731SJamie Gritton 			 * subset of the parent's list.  Don't worry
14320304c731SJamie Gritton 			 * about the parent being unlocked, as any
14330304c731SJamie Gritton 			 * setting is done with allprison_lock held.
14340304c731SJamie Gritton 			 */
14350304c731SJamie Gritton 			for (ij = 0; ij < ppr->pr_ip4s; ij++)
14362b0d6f81SJamie Gritton 				if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
14370304c731SJamie Gritton 					break;
14380304c731SJamie Gritton 			if (ij == ppr->pr_ip4s) {
14390304c731SJamie Gritton 				error = EPERM;
14400304c731SJamie Gritton 				goto done_deref_locked;
14410304c731SJamie Gritton 			}
14420304c731SJamie Gritton 			if (ip4s > 1) {
14430304c731SJamie Gritton 				for (ii = ij = 1; ii < ip4s; ii++) {
14440304c731SJamie Gritton 					if (ip4[ii].s_addr ==
14450304c731SJamie Gritton 					    ppr->pr_ip4[0].s_addr)
1446b38ff370SJamie Gritton 						continue;
14470304c731SJamie Gritton 					for (; ij < ppr->pr_ip4s; ij++)
14480304c731SJamie Gritton 						if (ip4[ii].s_addr ==
14490304c731SJamie Gritton 						    ppr->pr_ip4[ij].s_addr)
14500304c731SJamie Gritton 							break;
14510304c731SJamie Gritton 					if (ij == ppr->pr_ip4s)
14520304c731SJamie Gritton 						break;
14530304c731SJamie Gritton 				}
14540304c731SJamie Gritton 				if (ij == ppr->pr_ip4s) {
14550304c731SJamie Gritton 					error = EPERM;
14560304c731SJamie Gritton 					goto done_deref_locked;
14570304c731SJamie Gritton 				}
14580304c731SJamie Gritton 			}
14590304c731SJamie Gritton 		}
14600304c731SJamie Gritton 		/*
14610304c731SJamie Gritton 		 * Check for conflicting IP addresses.  We permit them
14620304c731SJamie Gritton 		 * if there is no more than one IP on each jail.  If
14630304c731SJamie Gritton 		 * there is a duplicate on a jail with more than one
14640304c731SJamie Gritton 		 * IP stop checking and return error.
14650304c731SJamie Gritton 		 */
1466bdfc8cc4SJamie Gritton 		tppr = ppr;
1467bdfc8cc4SJamie Gritton #ifdef VIMAGE
1468bdfc8cc4SJamie Gritton 		for (; tppr != &prison0; tppr = tppr->pr_parent)
1469bdfc8cc4SJamie Gritton 			if (tppr->pr_flags & PR_VNET)
1470bdfc8cc4SJamie Gritton 				break;
1471bdfc8cc4SJamie Gritton #endif
1472bdfc8cc4SJamie Gritton 		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1473bdfc8cc4SJamie Gritton 			if (tpr == pr ||
1474bdfc8cc4SJamie Gritton #ifdef VIMAGE
14752b0d6f81SJamie Gritton 			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1476bdfc8cc4SJamie Gritton #endif
1477bdfc8cc4SJamie Gritton 			    tpr->pr_uref == 0) {
14780304c731SJamie Gritton 				descend = 0;
14790304c731SJamie Gritton 				continue;
14800304c731SJamie Gritton 			}
14810304c731SJamie Gritton 			if (!(tpr->pr_flags & PR_IP4_USER))
14820304c731SJamie Gritton 				continue;
14830304c731SJamie Gritton 			descend = 0;
14840304c731SJamie Gritton 			if (tpr->pr_ip4 == NULL ||
14850304c731SJamie Gritton 			    (ip4s == 1 && tpr->pr_ip4s == 1))
14860304c731SJamie Gritton 				continue;
14870304c731SJamie Gritton 			for (ii = 0; ii < ip4s; ii++) {
14882b0d6f81SJamie Gritton 				if (_prison_check_ip4(tpr, &ip4[ii]) == 0) {
14890304c731SJamie Gritton 					error = EADDRINUSE;
1490b38ff370SJamie Gritton 					vfs_opterror(opts,
1491b38ff370SJamie Gritton 					    "IPv4 addresses clash");
1492b38ff370SJamie Gritton 					goto done_deref_locked;
1493b38ff370SJamie Gritton 				}
14940304c731SJamie Gritton 			}
14950304c731SJamie Gritton 		}
14960304c731SJamie Gritton 	}
1497b38ff370SJamie Gritton #endif
1498b38ff370SJamie Gritton #ifdef INET6
14992b0d6f81SJamie Gritton 	if (ip6s > 0) {
15000304c731SJamie Gritton 		if (ppr->pr_flags & PR_IP6) {
15010304c731SJamie Gritton 			/*
15020304c731SJamie Gritton 			 * Make sure the new set of IP addresses is a
15030304c731SJamie Gritton 			 * subset of the parent's list.
15040304c731SJamie Gritton 			 */
15050304c731SJamie Gritton 			for (ij = 0; ij < ppr->pr_ip6s; ij++)
15060304c731SJamie Gritton 				if (IN6_ARE_ADDR_EQUAL(&ip6[0],
15070304c731SJamie Gritton 				    &ppr->pr_ip6[ij]))
15080304c731SJamie Gritton 					break;
15090304c731SJamie Gritton 			if (ij == ppr->pr_ip6s) {
15100304c731SJamie Gritton 				error = EPERM;
15110304c731SJamie Gritton 				goto done_deref_locked;
15120304c731SJamie Gritton 			}
15130304c731SJamie Gritton 			if (ip6s > 1) {
15140304c731SJamie Gritton 				for (ii = ij = 1; ii < ip6s; ii++) {
15150304c731SJamie Gritton 					if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
15160304c731SJamie Gritton 					     &ppr->pr_ip6[0]))
15170304c731SJamie Gritton 						continue;
15180304c731SJamie Gritton 					for (; ij < ppr->pr_ip6s; ij++)
15190304c731SJamie Gritton 						if (IN6_ARE_ADDR_EQUAL(
15202b0d6f81SJamie Gritton 						    &ip6[ii], &ppr->pr_ip6[ij]))
15210304c731SJamie Gritton 							break;
15220304c731SJamie Gritton 					if (ij == ppr->pr_ip6s)
15230304c731SJamie Gritton 						break;
15240304c731SJamie Gritton 				}
15250304c731SJamie Gritton 				if (ij == ppr->pr_ip6s) {
15260304c731SJamie Gritton 					error = EPERM;
15270304c731SJamie Gritton 					goto done_deref_locked;
15280304c731SJamie Gritton 				}
15290304c731SJamie Gritton 			}
15300304c731SJamie Gritton 		}
15310304c731SJamie Gritton 		/* Check for conflicting IP addresses. */
1532bdfc8cc4SJamie Gritton 		tppr = ppr;
1533bdfc8cc4SJamie Gritton #ifdef VIMAGE
1534bdfc8cc4SJamie Gritton 		for (; tppr != &prison0; tppr = tppr->pr_parent)
1535bdfc8cc4SJamie Gritton 			if (tppr->pr_flags & PR_VNET)
1536bdfc8cc4SJamie Gritton 				break;
1537bdfc8cc4SJamie Gritton #endif
1538bdfc8cc4SJamie Gritton 		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1539bdfc8cc4SJamie Gritton 			if (tpr == pr ||
1540bdfc8cc4SJamie Gritton #ifdef VIMAGE
15412b0d6f81SJamie Gritton 			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1542bdfc8cc4SJamie Gritton #endif
1543bdfc8cc4SJamie Gritton 			    tpr->pr_uref == 0) {
15440304c731SJamie Gritton 				descend = 0;
15450304c731SJamie Gritton 				continue;
15460304c731SJamie Gritton 			}
15470304c731SJamie Gritton 			if (!(tpr->pr_flags & PR_IP6_USER))
15480304c731SJamie Gritton 				continue;
15490304c731SJamie Gritton 			descend = 0;
15500304c731SJamie Gritton 			if (tpr->pr_ip6 == NULL ||
15510304c731SJamie Gritton 			    (ip6s == 1 && tpr->pr_ip6s == 1))
15520304c731SJamie Gritton 				continue;
15530304c731SJamie Gritton 			for (ii = 0; ii < ip6s; ii++) {
15542b0d6f81SJamie Gritton 				if (_prison_check_ip6(tpr, &ip6[ii]) == 0) {
15550304c731SJamie Gritton 					error = EADDRINUSE;
1556b38ff370SJamie Gritton 					vfs_opterror(opts,
1557b38ff370SJamie Gritton 					    "IPv6 addresses clash");
1558b38ff370SJamie Gritton 					goto done_deref_locked;
1559b38ff370SJamie Gritton 				}
15600304c731SJamie Gritton 			}
15610304c731SJamie Gritton 		}
15620304c731SJamie Gritton 	}
1563b38ff370SJamie Gritton #endif
15640304c731SJamie Gritton 	onamelen = namelen = 0;
1565b6f47c23SJamie Gritton 	if (namelc != NULL) {
15666ab6058eSJamie Gritton 		/* Give a default name of the jid.  Also allow the name to be
15676ab6058eSJamie Gritton 		 * explicitly the jid - but not any other number, and only in
15686ab6058eSJamie Gritton 		 * normal form (no leading zero/etc).
15696ab6058eSJamie Gritton 		 */
1570b6f47c23SJamie Gritton 		if (namelc[0] == '\0')
1571b6f47c23SJamie Gritton 			snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid);
15726ab6058eSJamie Gritton 		else if ((strtoul(namelc, &p, 10) != jid ||
15736ab6058eSJamie Gritton 			  namelc[0] < '1' || namelc[0] > '9') && *p == '\0') {
1574b38ff370SJamie Gritton 			error = EINVAL;
1575babbbb9cSJamie Gritton 			vfs_opterror(opts,
1576babbbb9cSJamie Gritton 			    "name cannot be numeric (unless it is the jid)");
15770304c731SJamie Gritton 			goto done_deref_locked;
1578b38ff370SJamie Gritton 		}
1579b38ff370SJamie Gritton 		/*
15800304c731SJamie Gritton 		 * Make sure the name isn't too long for the prison or its
15810304c731SJamie Gritton 		 * children.
1582b38ff370SJamie Gritton 		 */
1583b6f47c23SJamie Gritton 		pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1584b6f47c23SJamie Gritton 		onamelen = strlen(pr->pr_name + pnamelen);
1585b6f47c23SJamie Gritton 		namelen = strlen(namelc);
1586b6f47c23SJamie Gritton 		if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) {
15870304c731SJamie Gritton 			error = ENAMETOOLONG;
15880304c731SJamie Gritton 			goto done_deref_locked;
15890304c731SJamie Gritton 		}
15900304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
15910304c731SJamie Gritton 			if (strlen(tpr->pr_name) + (namelen - onamelen) >=
15920304c731SJamie Gritton 			    sizeof(pr->pr_name)) {
15930304c731SJamie Gritton 				error = ENAMETOOLONG;
15940304c731SJamie Gritton 				goto done_deref_locked;
15950304c731SJamie Gritton 			}
15960304c731SJamie Gritton 		}
15970304c731SJamie Gritton 	}
15980304c731SJamie Gritton 	if (pr_allow & ~ppr->pr_allow) {
15990304c731SJamie Gritton 		error = EPERM;
16000304c731SJamie Gritton 		goto done_deref_locked;
1601b38ff370SJamie Gritton 	}
1602b38ff370SJamie Gritton 
1603b6f47c23SJamie Gritton 	/*
1604b6f47c23SJamie Gritton 	 * Let modules check their parameters.  This requires unlocking and
1605b6f47c23SJamie Gritton 	 * then re-locking the prison, but this is still a valid state as long
1606b6f47c23SJamie Gritton 	 * as allprison_lock remains xlocked.
1607b6f47c23SJamie Gritton 	 */
1608b6f47c23SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
1609b6f47c23SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_CHECK, opts);
1610b6f47c23SJamie Gritton 	if (error != 0) {
1611b6f47c23SJamie Gritton 		prison_deref(pr, created
1612b6f47c23SJamie Gritton 		    ? PD_LIST_XLOCKED
1613b6f47c23SJamie Gritton 		    : PD_DEREF | PD_LIST_XLOCKED);
1614b6f47c23SJamie Gritton 		goto done_releroot;
1615b6f47c23SJamie Gritton 	}
1616b6f47c23SJamie Gritton 	mtx_lock(&pr->pr_mtx);
1617b6f47c23SJamie Gritton 
1618b6f47c23SJamie Gritton 	/* At this point, all valid parameters should have been noted. */
1619b6f47c23SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
1620b6f47c23SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
1621b6f47c23SJamie Gritton 			error = EINVAL;
1622b6f47c23SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
1623b6f47c23SJamie Gritton 			goto done_deref_locked;
1624b6f47c23SJamie Gritton 		}
1625b6f47c23SJamie Gritton 	}
1626b6f47c23SJamie Gritton 
1627b38ff370SJamie Gritton 	/* Set the parameters of the prison. */
1628b38ff370SJamie Gritton #ifdef INET
16290304c731SJamie Gritton 	redo_ip4 = 0;
16300304c731SJamie Gritton 	if (pr_flags & PR_IP4_USER) {
16310304c731SJamie Gritton 		pr->pr_flags |= PR_IP4;
1632b38ff370SJamie Gritton 		free(pr->pr_ip4, M_PRISON);
16330304c731SJamie Gritton 		pr->pr_ip4s = ip4s;
1634b38ff370SJamie Gritton 		pr->pr_ip4 = ip4;
1635b38ff370SJamie Gritton 		ip4 = NULL;
16360304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1637bdfc8cc4SJamie Gritton #ifdef VIMAGE
1638bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1639bdfc8cc4SJamie Gritton 				descend = 0;
1640bdfc8cc4SJamie Gritton 				continue;
1641bdfc8cc4SJamie Gritton 			}
1642bdfc8cc4SJamie Gritton #endif
16430304c731SJamie Gritton 			if (prison_restrict_ip4(tpr, NULL)) {
16440304c731SJamie Gritton 				redo_ip4 = 1;
16450304c731SJamie Gritton 				descend = 0;
16460304c731SJamie Gritton 			}
16470304c731SJamie Gritton 		}
16480304c731SJamie Gritton 	}
1649b38ff370SJamie Gritton #endif
1650b38ff370SJamie Gritton #ifdef INET6
16510304c731SJamie Gritton 	redo_ip6 = 0;
16520304c731SJamie Gritton 	if (pr_flags & PR_IP6_USER) {
16530304c731SJamie Gritton 		pr->pr_flags |= PR_IP6;
1654b38ff370SJamie Gritton 		free(pr->pr_ip6, M_PRISON);
16550304c731SJamie Gritton 		pr->pr_ip6s = ip6s;
1656b38ff370SJamie Gritton 		pr->pr_ip6 = ip6;
1657b38ff370SJamie Gritton 		ip6 = NULL;
16580304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1659bdfc8cc4SJamie Gritton #ifdef VIMAGE
1660bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1661bdfc8cc4SJamie Gritton 				descend = 0;
1662bdfc8cc4SJamie Gritton 				continue;
1663bdfc8cc4SJamie Gritton 			}
1664bdfc8cc4SJamie Gritton #endif
16650304c731SJamie Gritton 			if (prison_restrict_ip6(tpr, NULL)) {
16660304c731SJamie Gritton 				redo_ip6 = 1;
16670304c731SJamie Gritton 				descend = 0;
16680304c731SJamie Gritton 			}
16690304c731SJamie Gritton 		}
16700304c731SJamie Gritton 	}
1671b38ff370SJamie Gritton #endif
16720304c731SJamie Gritton 	if (gotslevel) {
1673b38ff370SJamie Gritton 		pr->pr_securelevel = slevel;
16740304c731SJamie Gritton 		/* Set all child jails to be at least this level. */
16750304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
16760304c731SJamie Gritton 			if (tpr->pr_securelevel < slevel)
16770304c731SJamie Gritton 				tpr->pr_securelevel = slevel;
16780304c731SJamie Gritton 	}
1679b97457e2SJamie Gritton 	if (gotchildmax) {
1680b97457e2SJamie Gritton 		pr->pr_childmax = childmax;
1681b97457e2SJamie Gritton 		/* Set all child jails to under this limit. */
1682b97457e2SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1683b97457e2SJamie Gritton 			if (tpr->pr_childmax > childmax - level)
1684b97457e2SJamie Gritton 				tpr->pr_childmax = childmax > level
1685b97457e2SJamie Gritton 				    ? childmax - level : 0;
1686b97457e2SJamie Gritton 	}
16870304c731SJamie Gritton 	if (gotenforce) {
16880304c731SJamie Gritton 		pr->pr_enforce_statfs = enforce;
16890304c731SJamie Gritton 		/* Pass this restriction on to the children. */
16900304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
16910304c731SJamie Gritton 			if (tpr->pr_enforce_statfs < enforce)
16920304c731SJamie Gritton 				tpr->pr_enforce_statfs = enforce;
16930304c731SJamie Gritton 	}
16940cc207a6SMartin Matuska 	if (gotrsnum) {
16950cc207a6SMartin Matuska 		pr->pr_devfs_rsnum = rsnum;
16960cc207a6SMartin Matuska 		/* Pass this restriction on to the children. */
16970cc207a6SMartin Matuska 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
16980cc207a6SMartin Matuska 			tpr->pr_devfs_rsnum = rsnum;
16990cc207a6SMartin Matuska 	}
1700b6f47c23SJamie Gritton 	if (namelc != NULL) {
17010304c731SJamie Gritton 		if (ppr == &prison0)
1702b6f47c23SJamie Gritton 			strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name));
17030304c731SJamie Gritton 		else
17040304c731SJamie Gritton 			snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1705b6f47c23SJamie Gritton 			    ppr->pr_name, namelc);
17060304c731SJamie Gritton 		/* Change this component of child names. */
17070304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
17080304c731SJamie Gritton 			bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
17090304c731SJamie Gritton 			    strlen(tpr->pr_name + onamelen) + 1);
17100304c731SJamie Gritton 			bcopy(pr->pr_name, tpr->pr_name, namelen);
17110304c731SJamie Gritton 		}
17120304c731SJamie Gritton 	}
1713b38ff370SJamie Gritton 	if (path != NULL) {
17140304c731SJamie Gritton 		/* Try to keep a real-rooted full pathname. */
1715f6e633a9SMartin Matuska 		if (fullpath_disabled && path[0] == '/' &&
1716f6e633a9SMartin Matuska 		    strcmp(mypr->pr_path, "/"))
17170304c731SJamie Gritton 			snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
17180304c731SJamie Gritton 			    mypr->pr_path, path);
17190304c731SJamie Gritton 		else
1720b38ff370SJamie Gritton 			strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1721b38ff370SJamie Gritton 		pr->pr_root = root;
1722b38ff370SJamie Gritton 	}
172376ca6f88SJamie Gritton 	if (PR_HOST & ch_flags & ~pr_flags) {
172476ca6f88SJamie Gritton 		if (pr->pr_flags & PR_HOST) {
172576ca6f88SJamie Gritton 			/*
172676ca6f88SJamie Gritton 			 * Copy the parent's host info.  As with pr_ip4 above,
172776ca6f88SJamie Gritton 			 * the lack of a lock on the parent is not a problem;
172876ca6f88SJamie Gritton 			 * it is always set with allprison_lock at least
172976ca6f88SJamie Gritton 			 * shared, and is held exclusively here.
173076ca6f88SJamie Gritton 			 */
1731c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1732c1f19219SJamie Gritton 			    sizeof(pr->pr_hostname));
1733c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1734c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
1735c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1736c1f19219SJamie Gritton 			    sizeof(pr->pr_hostuuid));
173776ca6f88SJamie Gritton 			pr->pr_hostid = pr->pr_parent->pr_hostid;
173876ca6f88SJamie Gritton 		}
173976ca6f88SJamie Gritton 	} else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
174076ca6f88SJamie Gritton 		/* Set this prison, and any descendants without PR_HOST. */
1741b38ff370SJamie Gritton 		if (host != NULL)
1742c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
174376ca6f88SJamie Gritton 		if (domain != NULL)
1744c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, domain,
1745c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
174676ca6f88SJamie Gritton 		if (uuid != NULL)
1747c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
174876ca6f88SJamie Gritton 		if (gothid)
174976ca6f88SJamie Gritton 			pr->pr_hostid = hid;
175076ca6f88SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
175176ca6f88SJamie Gritton 			if (tpr->pr_flags & PR_HOST)
175276ca6f88SJamie Gritton 				descend = 0;
175376ca6f88SJamie Gritton 			else {
175476ca6f88SJamie Gritton 				if (host != NULL)
1755c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostname,
1756c1f19219SJamie Gritton 					    pr->pr_hostname,
1757c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostname));
175876ca6f88SJamie Gritton 				if (domain != NULL)
1759c1f19219SJamie Gritton 					strlcpy(tpr->pr_domainname,
1760c1f19219SJamie Gritton 					    pr->pr_domainname,
1761c1f19219SJamie Gritton 					    sizeof(tpr->pr_domainname));
176276ca6f88SJamie Gritton 				if (uuid != NULL)
1763c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostuuid,
1764c1f19219SJamie Gritton 					    pr->pr_hostuuid,
1765c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostuuid));
176676ca6f88SJamie Gritton 				if (gothid)
176776ca6f88SJamie Gritton 					tpr->pr_hostid = hid;
176876ca6f88SJamie Gritton 			}
176976ca6f88SJamie Gritton 		}
177076ca6f88SJamie Gritton 	}
17710304c731SJamie Gritton 	if ((tallow = ch_allow & ~pr_allow)) {
17720304c731SJamie Gritton 		/* Clear allow bits in all children. */
17730304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
17740304c731SJamie Gritton 			tpr->pr_allow &= ~tallow;
17750304c731SJamie Gritton 	}
17760304c731SJamie Gritton 	pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1777b38ff370SJamie Gritton 	/*
1778b38ff370SJamie Gritton 	 * Persistent prisons get an extra reference, and prisons losing their
1779b38ff370SJamie Gritton 	 * persist flag lose that reference.  Only do this for existing prisons
1780b38ff370SJamie Gritton 	 * for now, so new ones will remain unseen until after the module
1781b38ff370SJamie Gritton 	 * handlers have completed.
1782b38ff370SJamie Gritton 	 */
1783cc5fd8c7SJamie Gritton 	born = pr->pr_uref == 0;
1784b38ff370SJamie Gritton 	if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1785b38ff370SJamie Gritton 		if (pr_flags & PR_PERSIST) {
1786b38ff370SJamie Gritton 			pr->pr_ref++;
1787b38ff370SJamie Gritton 			pr->pr_uref++;
1788b38ff370SJamie Gritton 		} else {
1789b38ff370SJamie Gritton 			pr->pr_ref--;
1790b38ff370SJamie Gritton 			pr->pr_uref--;
1791b38ff370SJamie Gritton 		}
1792b38ff370SJamie Gritton 	}
1793b38ff370SJamie Gritton 	pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1794b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
1795b38ff370SJamie Gritton 
1796a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
17974b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && created)
1798a7ad07bfSEdward Tomasz Napierala 		prison_racct_attach(pr);
1799a7ad07bfSEdward Tomasz Napierala #endif
1800a7ad07bfSEdward Tomasz Napierala 
18010304c731SJamie Gritton 	/* Locks may have prevented a complete restriction of child IP
18020304c731SJamie Gritton 	 * addresses.  If so, allocate some more memory and try again.
18030304c731SJamie Gritton 	 */
18040304c731SJamie Gritton #ifdef INET
18050304c731SJamie Gritton 	while (redo_ip4) {
18060304c731SJamie Gritton 		ip4s = pr->pr_ip4s;
18070304c731SJamie Gritton 		ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
18080304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
18090304c731SJamie Gritton 		redo_ip4 = 0;
18100304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1811bdfc8cc4SJamie Gritton #ifdef VIMAGE
1812bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1813bdfc8cc4SJamie Gritton 				descend = 0;
1814bdfc8cc4SJamie Gritton 				continue;
1815bdfc8cc4SJamie Gritton 			}
1816bdfc8cc4SJamie Gritton #endif
18170304c731SJamie Gritton 			if (prison_restrict_ip4(tpr, ip4)) {
18180304c731SJamie Gritton 				if (ip4 != NULL)
18190304c731SJamie Gritton 					ip4 = NULL;
18200304c731SJamie Gritton 				else
18210304c731SJamie Gritton 					redo_ip4 = 1;
18220304c731SJamie Gritton 			}
18230304c731SJamie Gritton 		}
18240304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
18250304c731SJamie Gritton 	}
18260304c731SJamie Gritton #endif
18270304c731SJamie Gritton #ifdef INET6
18280304c731SJamie Gritton 	while (redo_ip6) {
18290304c731SJamie Gritton 		ip6s = pr->pr_ip6s;
18300304c731SJamie Gritton 		ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
18310304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
18320304c731SJamie Gritton 		redo_ip6 = 0;
18330304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1834bdfc8cc4SJamie Gritton #ifdef VIMAGE
1835bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1836bdfc8cc4SJamie Gritton 				descend = 0;
1837bdfc8cc4SJamie Gritton 				continue;
1838bdfc8cc4SJamie Gritton 			}
1839bdfc8cc4SJamie Gritton #endif
18400304c731SJamie Gritton 			if (prison_restrict_ip6(tpr, ip6)) {
18410304c731SJamie Gritton 				if (ip6 != NULL)
18420304c731SJamie Gritton 					ip6 = NULL;
18430304c731SJamie Gritton 				else
18440304c731SJamie Gritton 					redo_ip6 = 1;
18450304c731SJamie Gritton 			}
18460304c731SJamie Gritton 		}
18470304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
18480304c731SJamie Gritton 	}
18490304c731SJamie Gritton #endif
18500304c731SJamie Gritton 
1851b38ff370SJamie Gritton 	/* Let the modules do their work. */
1852b38ff370SJamie Gritton 	sx_downgrade(&allprison_lock);
1853cc5fd8c7SJamie Gritton 	if (born) {
1854b38ff370SJamie Gritton 		error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1855b38ff370SJamie Gritton 		if (error) {
1856cc5fd8c7SJamie Gritton 			(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
1857cc5fd8c7SJamie Gritton 			prison_deref(pr, created
1858cc5fd8c7SJamie Gritton 			    ? PD_LIST_SLOCKED
1859cc5fd8c7SJamie Gritton 			    : PD_DEREF | PD_LIST_SLOCKED);
1860b38ff370SJamie Gritton 			goto done_errmsg;
1861b38ff370SJamie Gritton 		}
1862b38ff370SJamie Gritton 	}
1863b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_SET, opts);
1864b38ff370SJamie Gritton 	if (error) {
1865cc5fd8c7SJamie Gritton 		if (born)
1866cc5fd8c7SJamie Gritton 			(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
1867b38ff370SJamie Gritton 		prison_deref(pr, created
1868b38ff370SJamie Gritton 		    ? PD_LIST_SLOCKED
1869b38ff370SJamie Gritton 		    : PD_DEREF | PD_LIST_SLOCKED);
1870b38ff370SJamie Gritton 		goto done_errmsg;
1871b38ff370SJamie Gritton 	}
1872b38ff370SJamie Gritton 
1873b38ff370SJamie Gritton 	/* Attach this process to the prison if requested. */
1874b38ff370SJamie Gritton 	if (flags & JAIL_ATTACH) {
1875b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
1876b38ff370SJamie Gritton 		error = do_jail_attach(td, pr);
1877b38ff370SJamie Gritton 		if (error) {
1878b38ff370SJamie Gritton 			vfs_opterror(opts, "attach failed");
1879b38ff370SJamie Gritton 			if (!created)
1880b38ff370SJamie Gritton 				prison_deref(pr, PD_DEREF);
1881b38ff370SJamie Gritton 			goto done_errmsg;
1882b38ff370SJamie Gritton 		}
1883b38ff370SJamie Gritton 	}
1884b38ff370SJamie Gritton 
18851fb24974SEdward Tomasz Napierala #ifdef RACCT
18864b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && !created) {
1887f514b97bSEdward Tomasz Napierala 		if (!(flags & JAIL_ATTACH))
18881fb24974SEdward Tomasz Napierala 			sx_sunlock(&allprison_lock);
18891fb24974SEdward Tomasz Napierala 		prison_racct_modify(pr);
1890f514b97bSEdward Tomasz Napierala 		if (!(flags & JAIL_ATTACH))
18911fb24974SEdward Tomasz Napierala 			sx_slock(&allprison_lock);
18921fb24974SEdward Tomasz Napierala 	}
18931fb24974SEdward Tomasz Napierala #endif
18941fb24974SEdward Tomasz Napierala 
18951fb24974SEdward Tomasz Napierala 	td->td_retval[0] = pr->pr_id;
18961fb24974SEdward Tomasz Napierala 
1897b38ff370SJamie Gritton 	/*
1898b38ff370SJamie Gritton 	 * Now that it is all there, drop the temporary reference from existing
1899b38ff370SJamie Gritton 	 * prisons.  Or add a reference to newly created persistent prisons
1900b38ff370SJamie Gritton 	 * (which was not done earlier so that the prison would not be publicly
1901b38ff370SJamie Gritton 	 * visible).
1902b38ff370SJamie Gritton 	 */
1903b38ff370SJamie Gritton 	if (!created) {
1904b38ff370SJamie Gritton 		prison_deref(pr, (flags & JAIL_ATTACH)
1905b38ff370SJamie Gritton 		    ? PD_DEREF
1906b38ff370SJamie Gritton 		    : PD_DEREF | PD_LIST_SLOCKED);
1907b38ff370SJamie Gritton 	} else {
1908b38ff370SJamie Gritton 		if (pr_flags & PR_PERSIST) {
1909b38ff370SJamie Gritton 			mtx_lock(&pr->pr_mtx);
1910b38ff370SJamie Gritton 			pr->pr_ref++;
1911b38ff370SJamie Gritton 			pr->pr_uref++;
1912b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
1913b38ff370SJamie Gritton 		}
1914b38ff370SJamie Gritton 		if (!(flags & JAIL_ATTACH))
1915b38ff370SJamie Gritton 			sx_sunlock(&allprison_lock);
1916b38ff370SJamie Gritton 	}
1917c34bbd2aSEdward Tomasz Napierala 
1918cc5fd8c7SJamie Gritton 	goto done_free;
1919b38ff370SJamie Gritton 
19200304c731SJamie Gritton  done_deref_locked:
19210304c731SJamie Gritton 	prison_deref(pr, created
19220304c731SJamie Gritton 	    ? PD_LOCKED | PD_LIST_XLOCKED
19230304c731SJamie Gritton 	    : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
19240304c731SJamie Gritton 	goto done_releroot;
1925b38ff370SJamie Gritton  done_unlock_list:
1926b38ff370SJamie Gritton 	sx_xunlock(&allprison_lock);
1927b38ff370SJamie Gritton  done_releroot:
19285050aa86SKonstantin Belousov 	if (root != NULL)
1929b38ff370SJamie Gritton 		vrele(root);
1930b38ff370SJamie Gritton  done_errmsg:
1931b38ff370SJamie Gritton 	if (error) {
1932*176ff3a0SJamie Gritton 		if (vfs_getopt(opts, "errmsg", (void **)&errmsg,
1933*176ff3a0SJamie Gritton 		    &errmsg_len) == 0 && errmsg_len > 0) {
1934b38ff370SJamie Gritton 			errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1935b38ff370SJamie Gritton 			if (optuio->uio_segflg == UIO_SYSSPACE)
1936b38ff370SJamie Gritton 				bcopy(errmsg,
1937b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
1938b38ff370SJamie Gritton 				    errmsg_len);
1939b38ff370SJamie Gritton 			else
1940b38ff370SJamie Gritton 				copyout(errmsg,
1941b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
1942b38ff370SJamie Gritton 				    errmsg_len);
1943b38ff370SJamie Gritton 		}
1944b38ff370SJamie Gritton 	}
1945b38ff370SJamie Gritton  done_free:
1946b38ff370SJamie Gritton #ifdef INET
1947b38ff370SJamie Gritton 	free(ip4, M_PRISON);
1948b38ff370SJamie Gritton #endif
1949b38ff370SJamie Gritton #ifdef INET6
1950b38ff370SJamie Gritton 	free(ip6, M_PRISON);
1951b38ff370SJamie Gritton #endif
19526dfe0a3dSMartin Matuska 	if (g_path != NULL)
19536dfe0a3dSMartin Matuska 		free(g_path, M_TEMP);
1954b38ff370SJamie Gritton 	vfs_freeopts(opts);
1955b38ff370SJamie Gritton 	return (error);
1956b38ff370SJamie Gritton }
1957b38ff370SJamie Gritton 
1958b38ff370SJamie Gritton 
1959b38ff370SJamie Gritton /*
1960b38ff370SJamie Gritton  * struct jail_get_args {
1961b38ff370SJamie Gritton  *	struct iovec *iovp;
1962b38ff370SJamie Gritton  *	unsigned int iovcnt;
1963b38ff370SJamie Gritton  *	int flags;
1964b38ff370SJamie Gritton  * };
1965b38ff370SJamie Gritton  */
1966b38ff370SJamie Gritton int
19678451d0ddSKip Macy sys_jail_get(struct thread *td, struct jail_get_args *uap)
1968b38ff370SJamie Gritton {
1969b38ff370SJamie Gritton 	struct uio *auio;
1970b38ff370SJamie Gritton 	int error;
1971b38ff370SJamie Gritton 
1972b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
1973b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
1974b38ff370SJamie Gritton 		return (EINVAL);
1975b38ff370SJamie Gritton 
1976b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1977b38ff370SJamie Gritton 	if (error)
1978b38ff370SJamie Gritton 		return (error);
1979b38ff370SJamie Gritton 	error = kern_jail_get(td, auio, uap->flags);
1980b38ff370SJamie Gritton 	if (error == 0)
1981b38ff370SJamie Gritton 		error = copyout(auio->uio_iov, uap->iovp,
1982b38ff370SJamie Gritton 		    uap->iovcnt * sizeof (struct iovec));
1983b38ff370SJamie Gritton 	free(auio, M_IOV);
1984b38ff370SJamie Gritton 	return (error);
1985b38ff370SJamie Gritton }
1986b38ff370SJamie Gritton 
1987b38ff370SJamie Gritton int
1988b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags)
1989b38ff370SJamie Gritton {
19900304c731SJamie Gritton 	struct prison *pr, *mypr;
1991b38ff370SJamie Gritton 	struct vfsopt *opt;
1992b38ff370SJamie Gritton 	struct vfsoptlist *opts;
1993b38ff370SJamie Gritton 	char *errmsg, *name;
19940304c731SJamie Gritton 	int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
1995b38ff370SJamie Gritton 
1996b38ff370SJamie Gritton 	if (flags & ~JAIL_GET_MASK)
1997b38ff370SJamie Gritton 		return (EINVAL);
1998b38ff370SJamie Gritton 
1999b38ff370SJamie Gritton 	/* Get the parameter list. */
2000b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
2001b38ff370SJamie Gritton 	if (error)
2002b38ff370SJamie Gritton 		return (error);
2003b38ff370SJamie Gritton 	errmsg_pos = vfs_getopt_pos(opts, "errmsg");
20040304c731SJamie Gritton 	mypr = td->td_ucred->cr_prison;
20051e2a13e6SJamie Gritton 
2006b38ff370SJamie Gritton 	/*
2007b38ff370SJamie Gritton 	 * Find the prison specified by one of: lastjid, jid, name.
2008b38ff370SJamie Gritton 	 */
2009b38ff370SJamie Gritton 	sx_slock(&allprison_lock);
2010b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
2011b38ff370SJamie Gritton 	if (error == 0) {
2012b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list) {
20130304c731SJamie Gritton 			if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
2014b38ff370SJamie Gritton 				mtx_lock(&pr->pr_mtx);
2015b38ff370SJamie Gritton 				if (pr->pr_ref > 0 &&
2016b38ff370SJamie Gritton 				    (pr->pr_uref > 0 || (flags & JAIL_DYING)))
2017b38ff370SJamie Gritton 					break;
2018b38ff370SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
2019b38ff370SJamie Gritton 			}
2020b38ff370SJamie Gritton 		}
2021b38ff370SJamie Gritton 		if (pr != NULL)
2022b38ff370SJamie Gritton 			goto found_prison;
2023b38ff370SJamie Gritton 		error = ENOENT;
2024b38ff370SJamie Gritton 		vfs_opterror(opts, "no jail after %d", jid);
2025b38ff370SJamie Gritton 		goto done_unlock_list;
2026b38ff370SJamie Gritton 	} else if (error != ENOENT)
2027b38ff370SJamie Gritton 		goto done_unlock_list;
2028b38ff370SJamie Gritton 
2029b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
2030b38ff370SJamie Gritton 	if (error == 0) {
2031b38ff370SJamie Gritton 		if (jid != 0) {
20320304c731SJamie Gritton 			pr = prison_find_child(mypr, jid);
2033b38ff370SJamie Gritton 			if (pr != NULL) {
2034b38ff370SJamie Gritton 				if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
2035b38ff370SJamie Gritton 					mtx_unlock(&pr->pr_mtx);
2036b38ff370SJamie Gritton 					error = ENOENT;
2037b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
2038b38ff370SJamie Gritton 					    jid);
2039b38ff370SJamie Gritton 					goto done_unlock_list;
2040b38ff370SJamie Gritton 				}
2041b38ff370SJamie Gritton 				goto found_prison;
2042b38ff370SJamie Gritton 			}
2043b38ff370SJamie Gritton 			error = ENOENT;
2044b38ff370SJamie Gritton 			vfs_opterror(opts, "jail %d not found", jid);
2045b38ff370SJamie Gritton 			goto done_unlock_list;
2046b38ff370SJamie Gritton 		}
2047b38ff370SJamie Gritton 	} else if (error != ENOENT)
2048b38ff370SJamie Gritton 		goto done_unlock_list;
2049b38ff370SJamie Gritton 
2050b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
2051b38ff370SJamie Gritton 	if (error == 0) {
2052b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
2053b38ff370SJamie Gritton 			error = EINVAL;
2054b38ff370SJamie Gritton 			goto done_unlock_list;
2055b38ff370SJamie Gritton 		}
20560304c731SJamie Gritton 		pr = prison_find_name(mypr, name);
2057b38ff370SJamie Gritton 		if (pr != NULL) {
2058b38ff370SJamie Gritton 			if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
2059b38ff370SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
2060b38ff370SJamie Gritton 				error = ENOENT;
2061b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" is dying",
2062b38ff370SJamie Gritton 				    name);
2063b38ff370SJamie Gritton 				goto done_unlock_list;
2064b38ff370SJamie Gritton 			}
2065b38ff370SJamie Gritton 			goto found_prison;
2066b38ff370SJamie Gritton 		}
2067b38ff370SJamie Gritton 		error = ENOENT;
2068b38ff370SJamie Gritton 		vfs_opterror(opts, "jail \"%s\" not found", name);
2069b38ff370SJamie Gritton 		goto done_unlock_list;
2070b38ff370SJamie Gritton 	} else if (error != ENOENT)
2071b38ff370SJamie Gritton 		goto done_unlock_list;
2072b38ff370SJamie Gritton 
2073b38ff370SJamie Gritton 	vfs_opterror(opts, "no jail specified");
2074b38ff370SJamie Gritton 	error = ENOENT;
2075b38ff370SJamie Gritton 	goto done_unlock_list;
2076b38ff370SJamie Gritton 
2077b38ff370SJamie Gritton  found_prison:
2078b38ff370SJamie Gritton 	/* Get the parameters of the prison. */
2079b38ff370SJamie Gritton 	pr->pr_ref++;
2080b38ff370SJamie Gritton 	locked = PD_LOCKED;
2081b38ff370SJamie Gritton 	td->td_retval[0] = pr->pr_id;
2082b38ff370SJamie Gritton 	error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2083b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2084b38ff370SJamie Gritton 		goto done_deref;
20850304c731SJamie Gritton 	i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
20860304c731SJamie Gritton 	error = vfs_setopt(opts, "parent", &i, sizeof(i));
2087b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2088b38ff370SJamie Gritton 		goto done_deref;
20890304c731SJamie Gritton 	error = vfs_setopts(opts, "name", prison_name(mypr, pr));
20900304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
20910304c731SJamie Gritton 		goto done_deref;
20920304c731SJamie Gritton 	error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2093b38ff370SJamie Gritton 	    sizeof(pr->pr_cpuset->cs_id));
2094b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2095b38ff370SJamie Gritton 		goto done_deref;
20960304c731SJamie Gritton 	error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2097b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2098b38ff370SJamie Gritton 		goto done_deref;
2099b38ff370SJamie Gritton #ifdef INET
2100b38ff370SJamie Gritton 	error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
2101b38ff370SJamie Gritton 	    pr->pr_ip4s * sizeof(*pr->pr_ip4));
2102b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2103b38ff370SJamie Gritton 		goto done_deref;
2104b38ff370SJamie Gritton #endif
2105b38ff370SJamie Gritton #ifdef INET6
2106b38ff370SJamie Gritton 	error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
2107b38ff370SJamie Gritton 	    pr->pr_ip6s * sizeof(*pr->pr_ip6));
2108b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2109b38ff370SJamie Gritton 		goto done_deref;
2110b38ff370SJamie Gritton #endif
2111b38ff370SJamie Gritton 	error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2112b38ff370SJamie Gritton 	    sizeof(pr->pr_securelevel));
2113b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2114b38ff370SJamie Gritton 		goto done_deref;
2115b97457e2SJamie Gritton 	error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2116b97457e2SJamie Gritton 	    sizeof(pr->pr_childcount));
2117b97457e2SJamie Gritton 	if (error != 0 && error != ENOENT)
2118b97457e2SJamie Gritton 		goto done_deref;
2119b97457e2SJamie Gritton 	error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2120b97457e2SJamie Gritton 	    sizeof(pr->pr_childmax));
2121b97457e2SJamie Gritton 	if (error != 0 && error != ENOENT)
2122b97457e2SJamie Gritton 		goto done_deref;
2123c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2124b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2125b38ff370SJamie Gritton 		goto done_deref;
2126c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
212776ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
212876ca6f88SJamie Gritton 		goto done_deref;
2129c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
213076ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
213176ca6f88SJamie Gritton 		goto done_deref;
2132841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
2133a5c1afadSDmitry Chagin 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
213476ca6f88SJamie Gritton 		uint32_t hid32 = pr->pr_hostid;
213576ca6f88SJamie Gritton 
213676ca6f88SJamie Gritton 		error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
213776ca6f88SJamie Gritton 	} else
213876ca6f88SJamie Gritton #endif
213976ca6f88SJamie Gritton 	error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
214076ca6f88SJamie Gritton 	    sizeof(pr->pr_hostid));
214176ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
214276ca6f88SJamie Gritton 		goto done_deref;
21430304c731SJamie Gritton 	error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
21440304c731SJamie Gritton 	    sizeof(pr->pr_enforce_statfs));
21450304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
21460304c731SJamie Gritton 		goto done_deref;
21470cc207a6SMartin Matuska 	error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
21480cc207a6SMartin Matuska 	    sizeof(pr->pr_devfs_rsnum));
21490cc207a6SMartin Matuska 	if (error != 0 && error != ENOENT)
21500cc207a6SMartin Matuska 		goto done_deref;
215102abd400SPedro F. Giffuni 	for (fi = 0; fi < nitems(pr_flag_names); fi++) {
21520304c731SJamie Gritton 		if (pr_flag_names[fi] == NULL)
21530304c731SJamie Gritton 			continue;
21540304c731SJamie Gritton 		i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
21550304c731SJamie Gritton 		error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
2156b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
2157b38ff370SJamie Gritton 			goto done_deref;
2158b38ff370SJamie Gritton 		i = !i;
21590304c731SJamie Gritton 		error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
2160b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
2161b38ff370SJamie Gritton 			goto done_deref;
21620304c731SJamie Gritton 	}
216302abd400SPedro F. Giffuni 	for (fi = 0; fi < nitems(pr_flag_jailsys); fi++) {
21647cbf7213SJamie Gritton 		i = pr->pr_flags &
21657cbf7213SJamie Gritton 		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
21667cbf7213SJamie Gritton 		i = pr_flag_jailsys[fi].disable &&
21677cbf7213SJamie Gritton 		      (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE
21687cbf7213SJamie Gritton 		    : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW
21697cbf7213SJamie Gritton 		    : JAIL_SYS_INHERIT;
21707cbf7213SJamie Gritton 		error =
21717cbf7213SJamie Gritton 		    vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i));
21727cbf7213SJamie Gritton 		if (error != 0 && error != ENOENT)
21737cbf7213SJamie Gritton 			goto done_deref;
21747cbf7213SJamie Gritton 	}
217502abd400SPedro F. Giffuni 	for (fi = 0; fi < nitems(pr_allow_names); fi++) {
21760304c731SJamie Gritton 		if (pr_allow_names[fi] == NULL)
21770304c731SJamie Gritton 			continue;
21780304c731SJamie Gritton 		i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
21790304c731SJamie Gritton 		error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
21800304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
21810304c731SJamie Gritton 			goto done_deref;
21820304c731SJamie Gritton 		i = !i;
21830304c731SJamie Gritton 		error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
21840304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
21850304c731SJamie Gritton 			goto done_deref;
21860304c731SJamie Gritton 	}
2187b38ff370SJamie Gritton 	i = (pr->pr_uref == 0);
2188b38ff370SJamie Gritton 	error = vfs_setopt(opts, "dying", &i, sizeof(i));
2189b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2190b38ff370SJamie Gritton 		goto done_deref;
2191b38ff370SJamie Gritton 	i = !i;
2192b38ff370SJamie Gritton 	error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2193b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2194b38ff370SJamie Gritton 		goto done_deref;
2195bd96bd15SIan Lepore 	error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate,
2196bd96bd15SIan Lepore 	    sizeof(pr->pr_osreldate));
2197a1a4c1b0SIan Lepore 	if (error != 0 && error != ENOENT)
2198a1a4c1b0SIan Lepore 		goto done_deref;
2199a1a4c1b0SIan Lepore 	error = vfs_setopts(opts, "osrelease", pr->pr_osrelease);
2200a1a4c1b0SIan Lepore 	if (error != 0 && error != ENOENT)
2201a1a4c1b0SIan Lepore 		goto done_deref;
2202b38ff370SJamie Gritton 
2203b38ff370SJamie Gritton 	/* Get the module parameters. */
2204b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2205b38ff370SJamie Gritton 	locked = 0;
2206b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_GET, opts);
2207b38ff370SJamie Gritton 	if (error)
2208b38ff370SJamie Gritton 		goto done_deref;
2209b38ff370SJamie Gritton 	prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
2210b38ff370SJamie Gritton 
2211b38ff370SJamie Gritton 	/* By now, all parameters should have been noted. */
2212b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
2213b38ff370SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
2214b38ff370SJamie Gritton 			error = EINVAL;
2215b38ff370SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
2216b38ff370SJamie Gritton 			goto done_errmsg;
2217b38ff370SJamie Gritton 		}
2218b38ff370SJamie Gritton 	}
2219b38ff370SJamie Gritton 
2220b38ff370SJamie Gritton 	/* Write the fetched parameters back to userspace. */
2221b38ff370SJamie Gritton 	error = 0;
2222b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
2223b38ff370SJamie Gritton 		if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2224b38ff370SJamie Gritton 			pos = 2 * opt->pos + 1;
2225b38ff370SJamie Gritton 			optuio->uio_iov[pos].iov_len = opt->len;
2226b38ff370SJamie Gritton 			if (opt->value != NULL) {
2227b38ff370SJamie Gritton 				if (optuio->uio_segflg == UIO_SYSSPACE) {
2228b38ff370SJamie Gritton 					bcopy(opt->value,
2229b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
2230b38ff370SJamie Gritton 					    opt->len);
2231b38ff370SJamie Gritton 				} else {
2232b38ff370SJamie Gritton 					error = copyout(opt->value,
2233b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
2234b38ff370SJamie Gritton 					    opt->len);
2235b38ff370SJamie Gritton 					if (error)
2236b38ff370SJamie Gritton 						break;
2237b38ff370SJamie Gritton 				}
2238b38ff370SJamie Gritton 			}
2239b38ff370SJamie Gritton 		}
2240b38ff370SJamie Gritton 	}
2241b38ff370SJamie Gritton 	goto done_errmsg;
2242b38ff370SJamie Gritton 
2243b38ff370SJamie Gritton  done_deref:
2244b38ff370SJamie Gritton 	prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2245b38ff370SJamie Gritton 	goto done_errmsg;
2246b38ff370SJamie Gritton 
2247b38ff370SJamie Gritton  done_unlock_list:
2248b38ff370SJamie Gritton 	sx_sunlock(&allprison_lock);
2249b38ff370SJamie Gritton  done_errmsg:
2250b38ff370SJamie Gritton 	if (error && errmsg_pos >= 0) {
2251b38ff370SJamie Gritton 		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2252b38ff370SJamie Gritton 		errmsg_pos = 2 * errmsg_pos + 1;
2253b38ff370SJamie Gritton 		if (errmsg_len > 0) {
2254b38ff370SJamie Gritton 			if (optuio->uio_segflg == UIO_SYSSPACE)
2255b38ff370SJamie Gritton 				bcopy(errmsg,
2256b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2257b38ff370SJamie Gritton 				    errmsg_len);
2258b38ff370SJamie Gritton 			else
2259b38ff370SJamie Gritton 				copyout(errmsg,
2260b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2261b38ff370SJamie Gritton 				    errmsg_len);
2262b38ff370SJamie Gritton 		}
2263b38ff370SJamie Gritton 	}
2264b38ff370SJamie Gritton 	vfs_freeopts(opts);
2265b38ff370SJamie Gritton 	return (error);
2266b38ff370SJamie Gritton }
2267b38ff370SJamie Gritton 
22680304c731SJamie Gritton 
2269b38ff370SJamie Gritton /*
2270b38ff370SJamie Gritton  * struct jail_remove_args {
2271b38ff370SJamie Gritton  *	int jid;
2272b38ff370SJamie Gritton  * };
2273b38ff370SJamie Gritton  */
2274b38ff370SJamie Gritton int
22758451d0ddSKip Macy sys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2276b38ff370SJamie Gritton {
22770304c731SJamie Gritton 	struct prison *pr, *cpr, *lpr, *tpr;
22780304c731SJamie Gritton 	int descend, error;
2279b38ff370SJamie Gritton 
2280b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_REMOVE);
2281b38ff370SJamie Gritton 	if (error)
2282b38ff370SJamie Gritton 		return (error);
2283b38ff370SJamie Gritton 
2284b38ff370SJamie Gritton 	sx_xlock(&allprison_lock);
22850304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2286b38ff370SJamie Gritton 	if (pr == NULL) {
2287b38ff370SJamie Gritton 		sx_xunlock(&allprison_lock);
2288b38ff370SJamie Gritton 		return (EINVAL);
2289b38ff370SJamie Gritton 	}
2290b38ff370SJamie Gritton 
22910304c731SJamie Gritton 	/* Remove all descendants of this prison, then remove this prison. */
22920304c731SJamie Gritton 	pr->pr_ref++;
22930304c731SJamie Gritton 	if (!LIST_EMPTY(&pr->pr_children)) {
22940304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
22950304c731SJamie Gritton 		lpr = NULL;
22960304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
22970304c731SJamie Gritton 			mtx_lock(&cpr->pr_mtx);
22980304c731SJamie Gritton 			if (cpr->pr_ref > 0) {
22990304c731SJamie Gritton 				tpr = cpr;
23000304c731SJamie Gritton 				cpr->pr_ref++;
23010304c731SJamie Gritton 			} else {
23020304c731SJamie Gritton 				/* Already removed - do not do it again. */
23030304c731SJamie Gritton 				tpr = NULL;
23040304c731SJamie Gritton 			}
23050304c731SJamie Gritton 			mtx_unlock(&cpr->pr_mtx);
23060304c731SJamie Gritton 			if (lpr != NULL) {
23070304c731SJamie Gritton 				mtx_lock(&lpr->pr_mtx);
23080304c731SJamie Gritton 				prison_remove_one(lpr);
23090304c731SJamie Gritton 				sx_xlock(&allprison_lock);
23100304c731SJamie Gritton 			}
23110304c731SJamie Gritton 			lpr = tpr;
23120304c731SJamie Gritton 		}
23130304c731SJamie Gritton 		if (lpr != NULL) {
23140304c731SJamie Gritton 			mtx_lock(&lpr->pr_mtx);
23150304c731SJamie Gritton 			prison_remove_one(lpr);
23160304c731SJamie Gritton 			sx_xlock(&allprison_lock);
23170304c731SJamie Gritton 		}
23180304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
23190304c731SJamie Gritton 	}
23200304c731SJamie Gritton 	prison_remove_one(pr);
23210304c731SJamie Gritton 	return (0);
23220304c731SJamie Gritton }
23230304c731SJamie Gritton 
23240304c731SJamie Gritton static void
23250304c731SJamie Gritton prison_remove_one(struct prison *pr)
23260304c731SJamie Gritton {
23270304c731SJamie Gritton 	struct proc *p;
23280304c731SJamie Gritton 	int deuref;
23290304c731SJamie Gritton 
2330b38ff370SJamie Gritton 	/* If the prison was persistent, it is not anymore. */
2331b38ff370SJamie Gritton 	deuref = 0;
2332b38ff370SJamie Gritton 	if (pr->pr_flags & PR_PERSIST) {
2333b38ff370SJamie Gritton 		pr->pr_ref--;
2334b38ff370SJamie Gritton 		deuref = PD_DEUREF;
2335b38ff370SJamie Gritton 		pr->pr_flags &= ~PR_PERSIST;
2336b38ff370SJamie Gritton 	}
2337b38ff370SJamie Gritton 
23380304c731SJamie Gritton 	/*
23390304c731SJamie Gritton 	 * jail_remove added a reference.  If that's the only one, remove
23400304c731SJamie Gritton 	 * the prison now.
23410304c731SJamie Gritton 	 */
23420304c731SJamie Gritton 	KASSERT(pr->pr_ref > 0,
23430304c731SJamie Gritton 	    ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
23440304c731SJamie Gritton 	if (pr->pr_ref == 1) {
2345b38ff370SJamie Gritton 		prison_deref(pr,
2346b38ff370SJamie Gritton 		    deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
23470304c731SJamie Gritton 		return;
2348b38ff370SJamie Gritton 	}
2349b38ff370SJamie Gritton 
2350b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2351b38ff370SJamie Gritton 	sx_xunlock(&allprison_lock);
2352b38ff370SJamie Gritton 	/*
2353b38ff370SJamie Gritton 	 * Kill all processes unfortunate enough to be attached to this prison.
2354b38ff370SJamie Gritton 	 */
2355b38ff370SJamie Gritton 	sx_slock(&allproc_lock);
2356b38ff370SJamie Gritton 	LIST_FOREACH(p, &allproc, p_list) {
2357b38ff370SJamie Gritton 		PROC_LOCK(p);
2358b38ff370SJamie Gritton 		if (p->p_state != PRS_NEW && p->p_ucred &&
2359b38ff370SJamie Gritton 		    p->p_ucred->cr_prison == pr)
23608451d0ddSKip Macy 			kern_psignal(p, SIGKILL);
2361b38ff370SJamie Gritton 		PROC_UNLOCK(p);
2362b38ff370SJamie Gritton 	}
2363b38ff370SJamie Gritton 	sx_sunlock(&allproc_lock);
23640304c731SJamie Gritton 	/* Remove the temporary reference added by jail_remove. */
2365b38ff370SJamie Gritton 	prison_deref(pr, deuref | PD_DEREF);
236675c13541SPoul-Henning Kamp }
236775c13541SPoul-Henning Kamp 
23688571af59SJamie Gritton 
2369fd7a8150SMike Barcroft /*
23709ddb7954SMike Barcroft  * struct jail_attach_args {
23719ddb7954SMike Barcroft  *	int jid;
23729ddb7954SMike Barcroft  * };
2373fd7a8150SMike Barcroft  */
2374fd7a8150SMike Barcroft int
23758451d0ddSKip Macy sys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2376fd7a8150SMike Barcroft {
2377b38ff370SJamie Gritton 	struct prison *pr;
2378b38ff370SJamie Gritton 	int error;
2379b38ff370SJamie Gritton 
2380b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_ATTACH);
2381b38ff370SJamie Gritton 	if (error)
2382b38ff370SJamie Gritton 		return (error);
2383b38ff370SJamie Gritton 
2384ef0ddea3SJamie Gritton 	/*
2385ef0ddea3SJamie Gritton 	 * Start with exclusive hold on allprison_lock to ensure that a possible
2386ef0ddea3SJamie Gritton 	 * PR_METHOD_REMOVE call isn't concurrent with jail_set or jail_remove.
2387ef0ddea3SJamie Gritton 	 * But then immediately downgrade it since we don't need to stop
2388ef0ddea3SJamie Gritton 	 * readers.
2389ef0ddea3SJamie Gritton 	 */
2390ef0ddea3SJamie Gritton 	sx_xlock(&allprison_lock);
2391ef0ddea3SJamie Gritton 	sx_downgrade(&allprison_lock);
23920304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2393b38ff370SJamie Gritton 	if (pr == NULL) {
2394b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2395b38ff370SJamie Gritton 		return (EINVAL);
2396b38ff370SJamie Gritton 	}
2397b38ff370SJamie Gritton 
2398b38ff370SJamie Gritton 	/*
2399b38ff370SJamie Gritton 	 * Do not allow a process to attach to a prison that is not
2400b38ff370SJamie Gritton 	 * considered to be "alive".
2401b38ff370SJamie Gritton 	 */
2402b38ff370SJamie Gritton 	if (pr->pr_uref == 0) {
2403b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2404b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2405b38ff370SJamie Gritton 		return (EINVAL);
2406b38ff370SJamie Gritton 	}
2407b38ff370SJamie Gritton 
2408b38ff370SJamie Gritton 	return (do_jail_attach(td, pr));
2409b38ff370SJamie Gritton }
2410b38ff370SJamie Gritton 
2411b38ff370SJamie Gritton static int
2412b38ff370SJamie Gritton do_jail_attach(struct thread *td, struct prison *pr)
2413b38ff370SJamie Gritton {
2414fd7a8150SMike Barcroft 	struct proc *p;
2415fd7a8150SMike Barcroft 	struct ucred *newcred, *oldcred;
24165050aa86SKonstantin Belousov 	int error;
2417fd7a8150SMike Barcroft 
241857f22bd4SJacques Vidrine 	/*
241957f22bd4SJacques Vidrine 	 * XXX: Note that there is a slight race here if two threads
242057f22bd4SJacques Vidrine 	 * in the same privileged process attempt to attach to two
242157f22bd4SJacques Vidrine 	 * different jails at the same time.  It is important for
242257f22bd4SJacques Vidrine 	 * user processes not to do this, or they might end up with
242357f22bd4SJacques Vidrine 	 * a process root from one prison, but attached to the jail
242457f22bd4SJacques Vidrine 	 * of another.
242557f22bd4SJacques Vidrine 	 */
2426fd7a8150SMike Barcroft 	pr->pr_ref++;
2427b38ff370SJamie Gritton 	pr->pr_uref++;
2428fd7a8150SMike Barcroft 	mtx_unlock(&pr->pr_mtx);
2429b38ff370SJamie Gritton 
2430b38ff370SJamie Gritton 	/* Let modules do whatever they need to prepare for attaching. */
2431b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2432b38ff370SJamie Gritton 	if (error) {
2433b38ff370SJamie Gritton 		prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2434b38ff370SJamie Gritton 		return (error);
2435b38ff370SJamie Gritton 	}
2436dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
2437fd7a8150SMike Barcroft 
2438413628a7SBjoern A. Zeeb 	/*
2439413628a7SBjoern A. Zeeb 	 * Reparent the newly attached process to this jail.
2440413628a7SBjoern A. Zeeb 	 */
2441b38ff370SJamie Gritton 	p = td->td_proc;
2442413628a7SBjoern A. Zeeb 	error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2443413628a7SBjoern A. Zeeb 	if (error)
2444b38ff370SJamie Gritton 		goto e_revert_osd;
2445413628a7SBjoern A. Zeeb 
2446cb05b60aSAttilio Rao 	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2447fd7a8150SMike Barcroft 	if ((error = change_dir(pr->pr_root, td)) != 0)
2448fd7a8150SMike Barcroft 		goto e_unlock;
2449fd7a8150SMike Barcroft #ifdef MAC
245030d239bcSRobert Watson 	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2451fd7a8150SMike Barcroft 		goto e_unlock;
2452fd7a8150SMike Barcroft #endif
245322db15c0SAttilio Rao 	VOP_UNLOCK(pr->pr_root, 0);
2454f0725a8eSMateusz Guzik 	if ((error = pwd_chroot(td, pr->pr_root)))
24555050aa86SKonstantin Belousov 		goto e_revert_osd;
2456fd7a8150SMike Barcroft 
2457fd7a8150SMike Barcroft 	newcred = crget();
2458fd7a8150SMike Barcroft 	PROC_LOCK(p);
24591fb6767dSJamie Gritton 	oldcred = crcopysafe(p, newcred);
246069c4ee54SJohn Baldwin 	newcred->cr_prison = pr;
2461daf63fd2SMateusz Guzik 	proc_set_cred(p, newcred);
24621fb6767dSJamie Gritton 	setsugid(p);
2463fd7a8150SMike Barcroft 	PROC_UNLOCK(p);
2464097055e2SEdward Tomasz Napierala #ifdef RACCT
2465097055e2SEdward Tomasz Napierala 	racct_proc_ucred_changed(p, oldcred, newcred);
2466097055e2SEdward Tomasz Napierala #endif
24671fb6767dSJamie Gritton 	prison_deref(oldcred->cr_prison, PD_DEREF | PD_DEUREF);
2468fd7a8150SMike Barcroft 	crfree(oldcred);
2469fd7a8150SMike Barcroft 	return (0);
24701fb6767dSJamie Gritton 
2471fd7a8150SMike Barcroft  e_unlock:
247222db15c0SAttilio Rao 	VOP_UNLOCK(pr->pr_root, 0);
2473b38ff370SJamie Gritton  e_revert_osd:
2474b38ff370SJamie Gritton 	/* Tell modules this thread is still in its old jail after all. */
24751fb6767dSJamie Gritton 	(void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td);
2476b38ff370SJamie Gritton 	prison_deref(pr, PD_DEREF | PD_DEUREF);
2477fd7a8150SMike Barcroft 	return (error);
2478fd7a8150SMike Barcroft }
2479fd7a8150SMike Barcroft 
24800304c731SJamie Gritton 
2481fd7a8150SMike Barcroft /*
2482fd7a8150SMike Barcroft  * Returns a locked prison instance, or NULL on failure.
2483fd7a8150SMike Barcroft  */
248454b369c1SPawel Jakub Dawidek struct prison *
2485fd7a8150SMike Barcroft prison_find(int prid)
2486fd7a8150SMike Barcroft {
2487fd7a8150SMike Barcroft 	struct prison *pr;
2488fd7a8150SMike Barcroft 
2489dc68a633SPawel Jakub Dawidek 	sx_assert(&allprison_lock, SX_LOCKED);
2490b38ff370SJamie Gritton 	TAILQ_FOREACH(pr, &allprison, pr_list) {
2491fd7a8150SMike Barcroft 		if (pr->pr_id == prid) {
2492fd7a8150SMike Barcroft 			mtx_lock(&pr->pr_mtx);
2493b38ff370SJamie Gritton 			if (pr->pr_ref > 0)
2494fd7a8150SMike Barcroft 				return (pr);
2495b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2496fd7a8150SMike Barcroft 		}
2497fd7a8150SMike Barcroft 	}
2498fd7a8150SMike Barcroft 	return (NULL);
2499fd7a8150SMike Barcroft }
2500fd7a8150SMike Barcroft 
2501b38ff370SJamie Gritton /*
25020304c731SJamie Gritton  * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
2503b38ff370SJamie Gritton  */
2504b38ff370SJamie Gritton struct prison *
25050304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid)
2506b38ff370SJamie Gritton {
25070304c731SJamie Gritton 	struct prison *pr;
25080304c731SJamie Gritton 	int descend;
2509b38ff370SJamie Gritton 
2510b38ff370SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
25110304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
25120304c731SJamie Gritton 		if (pr->pr_id == prid) {
25130304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
25140304c731SJamie Gritton 			if (pr->pr_ref > 0)
25150304c731SJamie Gritton 				return (pr);
25160304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
25170304c731SJamie Gritton 		}
25180304c731SJamie Gritton 	}
25190304c731SJamie Gritton 	return (NULL);
25200304c731SJamie Gritton }
25210304c731SJamie Gritton 
25220304c731SJamie Gritton /*
25230304c731SJamie Gritton  * Look for the name relative to mypr.  Returns a locked prison or NULL.
25240304c731SJamie Gritton  */
25250304c731SJamie Gritton struct prison *
25260304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name)
25270304c731SJamie Gritton {
25280304c731SJamie Gritton 	struct prison *pr, *deadpr;
25290304c731SJamie Gritton 	size_t mylen;
25300304c731SJamie Gritton 	int descend;
25310304c731SJamie Gritton 
25320304c731SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
25330304c731SJamie Gritton 	mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2534b38ff370SJamie Gritton  again:
2535b38ff370SJamie Gritton 	deadpr = NULL;
25360304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
25370304c731SJamie Gritton 		if (!strcmp(pr->pr_name + mylen, name)) {
2538b38ff370SJamie Gritton 			mtx_lock(&pr->pr_mtx);
2539b38ff370SJamie Gritton 			if (pr->pr_ref > 0) {
2540b38ff370SJamie Gritton 				if (pr->pr_uref > 0)
2541b38ff370SJamie Gritton 					return (pr);
2542b38ff370SJamie Gritton 				deadpr = pr;
2543b38ff370SJamie Gritton 			}
2544b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2545b38ff370SJamie Gritton 		}
2546b38ff370SJamie Gritton 	}
25470304c731SJamie Gritton 	/* There was no valid prison - perhaps there was a dying one. */
2548b38ff370SJamie Gritton 	if (deadpr != NULL) {
2549b38ff370SJamie Gritton 		mtx_lock(&deadpr->pr_mtx);
2550b38ff370SJamie Gritton 		if (deadpr->pr_ref == 0) {
2551b38ff370SJamie Gritton 			mtx_unlock(&deadpr->pr_mtx);
2552b38ff370SJamie Gritton 			goto again;
2553b38ff370SJamie Gritton 		}
2554b38ff370SJamie Gritton 	}
2555b38ff370SJamie Gritton 	return (deadpr);
2556b38ff370SJamie Gritton }
2557b38ff370SJamie Gritton 
2558b38ff370SJamie Gritton /*
25590304c731SJamie Gritton  * See if a prison has the specific flag set.
25600304c731SJamie Gritton  */
25610304c731SJamie Gritton int
25620304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag)
25630304c731SJamie Gritton {
25640304c731SJamie Gritton 
25650304c731SJamie Gritton 	/* This is an atomic read, so no locking is necessary. */
25660304c731SJamie Gritton 	return (cred->cr_prison->pr_flags & flag);
25670304c731SJamie Gritton }
25680304c731SJamie Gritton 
25690304c731SJamie Gritton int
25700304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag)
25710304c731SJamie Gritton {
25720304c731SJamie Gritton 
25730304c731SJamie Gritton 	/* This is an atomic read, so no locking is necessary. */
25740304c731SJamie Gritton 	return (cred->cr_prison->pr_allow & flag);
25750304c731SJamie Gritton }
25760304c731SJamie Gritton 
25770304c731SJamie Gritton /*
2578b38ff370SJamie Gritton  * Remove a prison reference.  If that was the last reference, remove the
2579b38ff370SJamie Gritton  * prison itself - but not in this context in case there are locks held.
2580b38ff370SJamie Gritton  */
258191421ba2SRobert Watson void
25821ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr)
258391421ba2SRobert Watson {
258473d9e52dSJamie Gritton 	int ref;
258591421ba2SRobert Watson 
25861ba4a712SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
258773d9e52dSJamie Gritton 	ref = --pr->pr_ref;
258801137630SRobert Watson 	mtx_unlock(&pr->pr_mtx);
258973d9e52dSJamie Gritton 	if (ref == 0)
2590c2cda609SPawel Jakub Dawidek 		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2591c2cda609SPawel Jakub Dawidek }
2592c2cda609SPawel Jakub Dawidek 
25931ba4a712SPawel Jakub Dawidek void
25941ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr)
25951ba4a712SPawel Jakub Dawidek {
25961ba4a712SPawel Jakub Dawidek 
25971ba4a712SPawel Jakub Dawidek 	mtx_lock(&pr->pr_mtx);
25981ba4a712SPawel Jakub Dawidek 	prison_free_locked(pr);
25991ba4a712SPawel Jakub Dawidek }
26001ba4a712SPawel Jakub Dawidek 
260173d9e52dSJamie Gritton /*
260273d9e52dSJamie Gritton  * Complete a call to either prison_free or prison_proc_free.
260373d9e52dSJamie Gritton  */
2604c2cda609SPawel Jakub Dawidek static void
2605c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending)
2606c2cda609SPawel Jakub Dawidek {
260773d9e52dSJamie Gritton 	struct prison *pr = context;
2608b38ff370SJamie Gritton 
2609ef0ddea3SJamie Gritton 	sx_xlock(&allprison_lock);
261073d9e52dSJamie Gritton 	mtx_lock(&pr->pr_mtx);
261173d9e52dSJamie Gritton 	prison_deref(pr, pr->pr_uref
2612ef0ddea3SJamie Gritton 	    ? PD_DEREF | PD_DEUREF | PD_LOCKED | PD_LIST_XLOCKED
2613ef0ddea3SJamie Gritton 	    : PD_LOCKED | PD_LIST_XLOCKED);
2614b38ff370SJamie Gritton }
2615b38ff370SJamie Gritton 
2616b38ff370SJamie Gritton /*
2617b38ff370SJamie Gritton  * Remove a prison reference (usually).  This internal version assumes no
2618b38ff370SJamie Gritton  * mutexes are held, except perhaps the prison itself.  If there are no more
2619b38ff370SJamie Gritton  * references, release and delist the prison.  On completion, the prison lock
2620b38ff370SJamie Gritton  * and the allprison lock are both unlocked.
2621b38ff370SJamie Gritton  */
2622b38ff370SJamie Gritton static void
2623b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags)
2624b38ff370SJamie Gritton {
26250304c731SJamie Gritton 	struct prison *ppr, *tpr;
2626cc5fd8c7SJamie Gritton 	int ref, lasturef;
2627c2cda609SPawel Jakub Dawidek 
2628b38ff370SJamie Gritton 	if (!(flags & PD_LOCKED))
2629b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
26300304c731SJamie Gritton 	for (;;) {
2631e6d5cb63SJamie Gritton 		if (flags & PD_DEUREF) {
263273d9e52dSJamie Gritton 			KASSERT(pr->pr_uref > 0,
263373d9e52dSJamie Gritton 			    ("prison_deref PD_DEUREF on a dead prison (jid=%d)",
263473d9e52dSJamie Gritton 			     pr->pr_id));
2635e6d5cb63SJamie Gritton 			pr->pr_uref--;
2636cc5fd8c7SJamie Gritton 			lasturef = pr->pr_uref == 0;
2637cc5fd8c7SJamie Gritton 			if (lasturef)
2638cc5fd8c7SJamie Gritton 				pr->pr_ref++;
2639e6d5cb63SJamie Gritton 			KASSERT(prison0.pr_uref != 0, ("prison0 pr_uref=0"));
2640cc5fd8c7SJamie Gritton 		} else
2641cc5fd8c7SJamie Gritton 			lasturef = 0;
264273d9e52dSJamie Gritton 		if (flags & PD_DEREF) {
264373d9e52dSJamie Gritton 			KASSERT(pr->pr_ref > 0,
264473d9e52dSJamie Gritton 			    ("prison_deref PD_DEREF on a dead prison (jid=%d)",
264573d9e52dSJamie Gritton 			     pr->pr_id));
2646b38ff370SJamie Gritton 			pr->pr_ref--;
264773d9e52dSJamie Gritton 		}
2648cc5fd8c7SJamie Gritton 		ref = pr->pr_ref;
2649b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2650cc5fd8c7SJamie Gritton 
2651cc5fd8c7SJamie Gritton 		/*
2652cc5fd8c7SJamie Gritton 		 * Tell the modules if the last user reference was removed
2653cc5fd8c7SJamie Gritton 		 * (even it sticks around in dying state).
2654cc5fd8c7SJamie Gritton 		 */
2655cc5fd8c7SJamie Gritton 		if (lasturef) {
2656cc5fd8c7SJamie Gritton 			if (!(flags & (PD_LIST_SLOCKED | PD_LIST_XLOCKED))) {
2657cc5fd8c7SJamie Gritton 				sx_xlock(&allprison_lock);
2658cc5fd8c7SJamie Gritton 				flags |= PD_LIST_XLOCKED;
2659cc5fd8c7SJamie Gritton 			}
2660cc5fd8c7SJamie Gritton 			(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
2661cc5fd8c7SJamie Gritton 			mtx_lock(&pr->pr_mtx);
2662cc5fd8c7SJamie Gritton 			ref = --pr->pr_ref;
2663cc5fd8c7SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2664cc5fd8c7SJamie Gritton 		}
2665cc5fd8c7SJamie Gritton 
2666cc5fd8c7SJamie Gritton 		/* If the prison still has references, nothing else to do. */
2667cc5fd8c7SJamie Gritton 		if (ref > 0) {
2668b38ff370SJamie Gritton 			if (flags & PD_LIST_SLOCKED)
2669b38ff370SJamie Gritton 				sx_sunlock(&allprison_lock);
2670b38ff370SJamie Gritton 			else if (flags & PD_LIST_XLOCKED)
2671b38ff370SJamie Gritton 				sx_xunlock(&allprison_lock);
2672b38ff370SJamie Gritton 			return;
2673b38ff370SJamie Gritton 		}
2674c2cda609SPawel Jakub Dawidek 
2675b38ff370SJamie Gritton 		if (flags & PD_LIST_SLOCKED) {
2676b38ff370SJamie Gritton 			if (!sx_try_upgrade(&allprison_lock)) {
2677b38ff370SJamie Gritton 				sx_sunlock(&allprison_lock);
2678c2cda609SPawel Jakub Dawidek 				sx_xlock(&allprison_lock);
2679b38ff370SJamie Gritton 			}
2680b38ff370SJamie Gritton 		} else if (!(flags & PD_LIST_XLOCKED))
2681b38ff370SJamie Gritton 			sx_xlock(&allprison_lock);
2682b38ff370SJamie Gritton 
2683b38ff370SJamie Gritton 		TAILQ_REMOVE(&allprison, pr, pr_list);
26840304c731SJamie Gritton 		LIST_REMOVE(pr, pr_sibling);
26850304c731SJamie Gritton 		ppr = pr->pr_parent;
26860304c731SJamie Gritton 		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2687b97457e2SJamie Gritton 			tpr->pr_childcount--;
2688c4884ffaSJamie Gritton 		sx_xunlock(&allprison_lock);
26891ba4a712SPawel Jakub Dawidek 
2690679e1390SJamie Gritton #ifdef VIMAGE
26910cb8b6a9SMarko Zec 		if (pr->pr_vnet != ppr->pr_vnet)
2692679e1390SJamie Gritton 			vnet_destroy(pr->pr_vnet);
2693679e1390SJamie Gritton #endif
26945050aa86SKonstantin Belousov 		if (pr->pr_root != NULL)
2695b3059e09SRobert Watson 			vrele(pr->pr_root);
2696b3059e09SRobert Watson 		mtx_destroy(&pr->pr_mtx);
2697413628a7SBjoern A. Zeeb #ifdef INET
2698413628a7SBjoern A. Zeeb 		free(pr->pr_ip4, M_PRISON);
2699413628a7SBjoern A. Zeeb #endif
2700b38ff370SJamie Gritton #ifdef INET6
2701b38ff370SJamie Gritton 		free(pr->pr_ip6, M_PRISON);
2702b38ff370SJamie Gritton #endif
2703b38ff370SJamie Gritton 		if (pr->pr_cpuset != NULL)
2704b38ff370SJamie Gritton 			cpuset_rel(pr->pr_cpuset);
2705b38ff370SJamie Gritton 		osd_jail_exit(pr);
2706a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
27074b5c9cf6SEdward Tomasz Napierala 		if (racct_enable)
2708a7ad07bfSEdward Tomasz Napierala 			prison_racct_detach(pr);
2709ec125fbbSEdward Tomasz Napierala #endif
27101ede983cSDag-Erling Smørgrav 		free(pr, M_PRISON);
27110304c731SJamie Gritton 
27120304c731SJamie Gritton 		/* Removing a prison frees a reference on its parent. */
27130304c731SJamie Gritton 		pr = ppr;
27140304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
2715e6d5cb63SJamie Gritton 		flags = PD_DEREF | PD_DEUREF;
27160304c731SJamie Gritton 	}
2717b3059e09SRobert Watson }
2718b3059e09SRobert Watson 
271991421ba2SRobert Watson void
27201ba4a712SPawel Jakub Dawidek prison_hold_locked(struct prison *pr)
27211ba4a712SPawel Jakub Dawidek {
27221ba4a712SPawel Jakub Dawidek 
27231ba4a712SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
27241ba4a712SPawel Jakub Dawidek 	KASSERT(pr->pr_ref > 0,
2725af7bd9a4SJamie Gritton 	    ("Trying to hold dead prison (jid=%d).", pr->pr_id));
27261ba4a712SPawel Jakub Dawidek 	pr->pr_ref++;
27271ba4a712SPawel Jakub Dawidek }
27281ba4a712SPawel Jakub Dawidek 
27291ba4a712SPawel Jakub Dawidek void
273091421ba2SRobert Watson prison_hold(struct prison *pr)
273191421ba2SRobert Watson {
273291421ba2SRobert Watson 
273301137630SRobert Watson 	mtx_lock(&pr->pr_mtx);
27341ba4a712SPawel Jakub Dawidek 	prison_hold_locked(pr);
273501137630SRobert Watson 	mtx_unlock(&pr->pr_mtx);
273601137630SRobert Watson }
273701137630SRobert Watson 
2738413628a7SBjoern A. Zeeb void
2739413628a7SBjoern A. Zeeb prison_proc_hold(struct prison *pr)
274001137630SRobert Watson {
274101137630SRobert Watson 
2742413628a7SBjoern A. Zeeb 	mtx_lock(&pr->pr_mtx);
2743b38ff370SJamie Gritton 	KASSERT(pr->pr_uref > 0,
2744b38ff370SJamie Gritton 	    ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2745b38ff370SJamie Gritton 	pr->pr_uref++;
2746413628a7SBjoern A. Zeeb 	mtx_unlock(&pr->pr_mtx);
274775c13541SPoul-Henning Kamp }
274875c13541SPoul-Henning Kamp 
274975c13541SPoul-Henning Kamp void
2750413628a7SBjoern A. Zeeb prison_proc_free(struct prison *pr)
275175c13541SPoul-Henning Kamp {
2752413628a7SBjoern A. Zeeb 
2753413628a7SBjoern A. Zeeb 	mtx_lock(&pr->pr_mtx);
2754b38ff370SJamie Gritton 	KASSERT(pr->pr_uref > 0,
2755b38ff370SJamie Gritton 	    ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
275673d9e52dSJamie Gritton 	if (pr->pr_uref > 1)
275773d9e52dSJamie Gritton 		pr->pr_uref--;
275873d9e52dSJamie Gritton 	else {
275973d9e52dSJamie Gritton 		/*
276073d9e52dSJamie Gritton 		 * Don't remove the last user reference in this context, which
276173d9e52dSJamie Gritton 		 * is expected to be a process that is not only locked, but
276273d9e52dSJamie Gritton 		 * also half dead.
276373d9e52dSJamie Gritton 		 */
276473d9e52dSJamie Gritton 		pr->pr_ref++;
276573d9e52dSJamie Gritton 		mtx_unlock(&pr->pr_mtx);
276673d9e52dSJamie Gritton 		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
276773d9e52dSJamie Gritton 		return;
276873d9e52dSJamie Gritton 	}
276973d9e52dSJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2770413628a7SBjoern A. Zeeb }
2771413628a7SBjoern A. Zeeb 
2772413628a7SBjoern A. Zeeb 
2773413628a7SBjoern A. Zeeb #ifdef INET
2774413628a7SBjoern A. Zeeb /*
27750304c731SJamie Gritton  * Restrict a prison's IP address list with its parent's, possibly replacing
27760304c731SJamie Gritton  * it.  Return true if the replacement buffer was used (or would have been).
27770304c731SJamie Gritton  */
27780304c731SJamie Gritton static int
27790304c731SJamie Gritton prison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
27800304c731SJamie Gritton {
27810304c731SJamie Gritton 	int ii, ij, used;
27820304c731SJamie Gritton 	struct prison *ppr;
27830304c731SJamie Gritton 
27840304c731SJamie Gritton 	ppr = pr->pr_parent;
27850304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4_USER)) {
27860304c731SJamie Gritton 		/* This has no user settings, so just copy the parent's list. */
27870304c731SJamie Gritton 		if (pr->pr_ip4s < ppr->pr_ip4s) {
27880304c731SJamie Gritton 			/*
27890304c731SJamie Gritton 			 * There's no room for the parent's list.  Use the
27900304c731SJamie Gritton 			 * new list buffer, which is assumed to be big enough
27910304c731SJamie Gritton 			 * (if it was passed).  If there's no buffer, try to
27920304c731SJamie Gritton 			 * allocate one.
27930304c731SJamie Gritton 			 */
27940304c731SJamie Gritton 			used = 1;
27950304c731SJamie Gritton 			if (newip4 == NULL) {
27960304c731SJamie Gritton 				newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
27970304c731SJamie Gritton 				    M_PRISON, M_NOWAIT);
27980304c731SJamie Gritton 				if (newip4 != NULL)
27990304c731SJamie Gritton 					used = 0;
28000304c731SJamie Gritton 			}
28010304c731SJamie Gritton 			if (newip4 != NULL) {
28020304c731SJamie Gritton 				bcopy(ppr->pr_ip4, newip4,
28030304c731SJamie Gritton 				    ppr->pr_ip4s * sizeof(*newip4));
28040304c731SJamie Gritton 				free(pr->pr_ip4, M_PRISON);
28050304c731SJamie Gritton 				pr->pr_ip4 = newip4;
28060304c731SJamie Gritton 				pr->pr_ip4s = ppr->pr_ip4s;
28070304c731SJamie Gritton 			}
28080304c731SJamie Gritton 			return (used);
28090304c731SJamie Gritton 		}
28100304c731SJamie Gritton 		pr->pr_ip4s = ppr->pr_ip4s;
28110304c731SJamie Gritton 		if (pr->pr_ip4s > 0)
28120304c731SJamie Gritton 			bcopy(ppr->pr_ip4, pr->pr_ip4,
28130304c731SJamie Gritton 			    pr->pr_ip4s * sizeof(*newip4));
28140304c731SJamie Gritton 		else if (pr->pr_ip4 != NULL) {
28150304c731SJamie Gritton 			free(pr->pr_ip4, M_PRISON);
28160304c731SJamie Gritton 			pr->pr_ip4 = NULL;
28170304c731SJamie Gritton 		}
28182b0d6f81SJamie Gritton 	} else if (pr->pr_ip4s > 0) {
28190304c731SJamie Gritton 		/* Remove addresses that aren't in the parent. */
28200304c731SJamie Gritton 		for (ij = 0; ij < ppr->pr_ip4s; ij++)
28210304c731SJamie Gritton 			if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
28220304c731SJamie Gritton 				break;
28230304c731SJamie Gritton 		if (ij < ppr->pr_ip4s)
28240304c731SJamie Gritton 			ii = 1;
28250304c731SJamie Gritton 		else {
28260304c731SJamie Gritton 			bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
28270304c731SJamie Gritton 			    --pr->pr_ip4s * sizeof(*pr->pr_ip4));
28280304c731SJamie Gritton 			ii = 0;
28290304c731SJamie Gritton 		}
28300304c731SJamie Gritton 		for (ij = 1; ii < pr->pr_ip4s; ) {
28310304c731SJamie Gritton 			if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
28320304c731SJamie Gritton 				ii++;
28330304c731SJamie Gritton 				continue;
28340304c731SJamie Gritton 			}
28350304c731SJamie Gritton 			switch (ij >= ppr->pr_ip4s ? -1 :
28360304c731SJamie Gritton 				qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
28370304c731SJamie Gritton 			case -1:
28380304c731SJamie Gritton 				bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
28390304c731SJamie Gritton 				    (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
28400304c731SJamie Gritton 				break;
28410304c731SJamie Gritton 			case 0:
28420304c731SJamie Gritton 				ii++;
28430304c731SJamie Gritton 				ij++;
28440304c731SJamie Gritton 				break;
28450304c731SJamie Gritton 			case 1:
28460304c731SJamie Gritton 				ij++;
28470304c731SJamie Gritton 				break;
28480304c731SJamie Gritton 			}
28490304c731SJamie Gritton 		}
28500304c731SJamie Gritton 		if (pr->pr_ip4s == 0) {
28510304c731SJamie Gritton 			free(pr->pr_ip4, M_PRISON);
28520304c731SJamie Gritton 			pr->pr_ip4 = NULL;
28530304c731SJamie Gritton 		}
28540304c731SJamie Gritton 	}
28550304c731SJamie Gritton 	return (0);
28560304c731SJamie Gritton }
28570304c731SJamie Gritton 
28580304c731SJamie Gritton /*
2859413628a7SBjoern A. Zeeb  * Pass back primary IPv4 address of this jail.
2860413628a7SBjoern A. Zeeb  *
28610304c731SJamie Gritton  * If not restricted return success but do not alter the address.  Caller has
28620304c731SJamie Gritton  * to make sure to initialize it correctly (e.g. INADDR_ANY).
2863413628a7SBjoern A. Zeeb  *
2864b89e82ddSJamie Gritton  * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2865b89e82ddSJamie Gritton  * Address returned in NBO.
2866413628a7SBjoern A. Zeeb  */
2867413628a7SBjoern A. Zeeb int
28681cecba0fSBjoern A. Zeeb prison_get_ip4(struct ucred *cred, struct in_addr *ia)
2869413628a7SBjoern A. Zeeb {
2870b38ff370SJamie Gritton 	struct prison *pr;
2871413628a7SBjoern A. Zeeb 
2872413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2873413628a7SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
287475c13541SPoul-Henning Kamp 
2875b38ff370SJamie Gritton 	pr = cred->cr_prison;
28760304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4))
28770304c731SJamie Gritton 		return (0);
2878b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
28790304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4)) {
28800304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
28810304c731SJamie Gritton 		return (0);
28820304c731SJamie Gritton 	}
2883b38ff370SJamie Gritton 	if (pr->pr_ip4 == NULL) {
2884b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2885b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
2886b38ff370SJamie Gritton 	}
2887413628a7SBjoern A. Zeeb 
2888b38ff370SJamie Gritton 	ia->s_addr = pr->pr_ip4[0].s_addr;
2889b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2890413628a7SBjoern A. Zeeb 	return (0);
289175c13541SPoul-Henning Kamp }
2892413628a7SBjoern A. Zeeb 
2893413628a7SBjoern A. Zeeb /*
2894592bcae8SBjoern A. Zeeb  * Return 1 if we should do proper source address selection or are not jailed.
2895592bcae8SBjoern A. Zeeb  * We will return 0 if we should bypass source address selection in favour
2896592bcae8SBjoern A. Zeeb  * of the primary jail IPv4 address. Only in this case *ia will be updated and
2897592bcae8SBjoern A. Zeeb  * returned in NBO.
2898592bcae8SBjoern A. Zeeb  * Return EAFNOSUPPORT, in case this jail does not allow IPv4.
2899592bcae8SBjoern A. Zeeb  */
2900592bcae8SBjoern A. Zeeb int
2901592bcae8SBjoern A. Zeeb prison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia)
2902592bcae8SBjoern A. Zeeb {
2903592bcae8SBjoern A. Zeeb 	struct prison *pr;
2904592bcae8SBjoern A. Zeeb 	struct in_addr lia;
2905592bcae8SBjoern A. Zeeb 	int error;
2906592bcae8SBjoern A. Zeeb 
2907592bcae8SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2908592bcae8SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2909592bcae8SBjoern A. Zeeb 
2910592bcae8SBjoern A. Zeeb 	if (!jailed(cred))
2911592bcae8SBjoern A. Zeeb 		return (1);
2912592bcae8SBjoern A. Zeeb 
2913592bcae8SBjoern A. Zeeb 	pr = cred->cr_prison;
2914592bcae8SBjoern A. Zeeb 	if (pr->pr_flags & PR_IP4_SADDRSEL)
2915592bcae8SBjoern A. Zeeb 		return (1);
2916592bcae8SBjoern A. Zeeb 
2917592bcae8SBjoern A. Zeeb 	lia.s_addr = INADDR_ANY;
2918592bcae8SBjoern A. Zeeb 	error = prison_get_ip4(cred, &lia);
2919592bcae8SBjoern A. Zeeb 	if (error)
2920592bcae8SBjoern A. Zeeb 		return (error);
2921592bcae8SBjoern A. Zeeb 	if (lia.s_addr == INADDR_ANY)
2922592bcae8SBjoern A. Zeeb 		return (1);
2923592bcae8SBjoern A. Zeeb 
2924592bcae8SBjoern A. Zeeb 	ia->s_addr = lia.s_addr;
2925592bcae8SBjoern A. Zeeb 	return (0);
2926592bcae8SBjoern A. Zeeb }
2927592bcae8SBjoern A. Zeeb 
2928592bcae8SBjoern A. Zeeb /*
29290304c731SJamie Gritton  * Return true if pr1 and pr2 have the same IPv4 address restrictions.
29300304c731SJamie Gritton  */
29310304c731SJamie Gritton int
29320304c731SJamie Gritton prison_equal_ip4(struct prison *pr1, struct prison *pr2)
29330304c731SJamie Gritton {
29340304c731SJamie Gritton 
29350304c731SJamie Gritton 	if (pr1 == pr2)
29360304c731SJamie Gritton 		return (1);
29370304c731SJamie Gritton 
29380304c731SJamie Gritton 	/*
29392b0d6f81SJamie Gritton 	 * No need to lock since the PR_IP4_USER flag can't be altered for
29402b0d6f81SJamie Gritton 	 * existing prisons.
29410304c731SJamie Gritton 	 */
2942bdfc8cc4SJamie Gritton 	while (pr1 != &prison0 &&
2943bdfc8cc4SJamie Gritton #ifdef VIMAGE
2944bdfc8cc4SJamie Gritton 	       !(pr1->pr_flags & PR_VNET) &&
2945bdfc8cc4SJamie Gritton #endif
2946bdfc8cc4SJamie Gritton 	       !(pr1->pr_flags & PR_IP4_USER))
29470304c731SJamie Gritton 		pr1 = pr1->pr_parent;
2948bdfc8cc4SJamie Gritton 	while (pr2 != &prison0 &&
2949bdfc8cc4SJamie Gritton #ifdef VIMAGE
2950bdfc8cc4SJamie Gritton 	       !(pr2->pr_flags & PR_VNET) &&
2951bdfc8cc4SJamie Gritton #endif
2952bdfc8cc4SJamie Gritton 	       !(pr2->pr_flags & PR_IP4_USER))
29530304c731SJamie Gritton 		pr2 = pr2->pr_parent;
29540304c731SJamie Gritton 	return (pr1 == pr2);
29550304c731SJamie Gritton }
29560304c731SJamie Gritton 
29570304c731SJamie Gritton /*
2958413628a7SBjoern A. Zeeb  * Make sure our (source) address is set to something meaningful to this
2959413628a7SBjoern A. Zeeb  * jail.
2960413628a7SBjoern A. Zeeb  *
29610304c731SJamie Gritton  * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
29620304c731SJamie Gritton  * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
29630304c731SJamie Gritton  * doesn't allow IPv4.  Address passed in in NBO and returned in NBO.
2964413628a7SBjoern A. Zeeb  */
2965413628a7SBjoern A. Zeeb int
2966413628a7SBjoern A. Zeeb prison_local_ip4(struct ucred *cred, struct in_addr *ia)
2967413628a7SBjoern A. Zeeb {
2968b38ff370SJamie Gritton 	struct prison *pr;
2969413628a7SBjoern A. Zeeb 	struct in_addr ia0;
2970b38ff370SJamie Gritton 	int error;
2971413628a7SBjoern A. Zeeb 
2972413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2973413628a7SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2974413628a7SBjoern A. Zeeb 
2975b38ff370SJamie Gritton 	pr = cred->cr_prison;
29760304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4))
29770304c731SJamie Gritton 		return (0);
2978b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
29790304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4)) {
29800304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
29810304c731SJamie Gritton 		return (0);
29820304c731SJamie Gritton 	}
2983b38ff370SJamie Gritton 	if (pr->pr_ip4 == NULL) {
2984b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2985b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
2986b38ff370SJamie Gritton 	}
2987413628a7SBjoern A. Zeeb 
2988413628a7SBjoern A. Zeeb 	ia0.s_addr = ntohl(ia->s_addr);
2989413628a7SBjoern A. Zeeb 	if (ia0.s_addr == INADDR_LOOPBACK) {
2990b38ff370SJamie Gritton 		ia->s_addr = pr->pr_ip4[0].s_addr;
2991b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2992413628a7SBjoern A. Zeeb 		return (0);
2993413628a7SBjoern A. Zeeb 	}
2994413628a7SBjoern A. Zeeb 
2995b89e82ddSJamie Gritton 	if (ia0.s_addr == INADDR_ANY) {
2996413628a7SBjoern A. Zeeb 		/*
2997413628a7SBjoern A. Zeeb 		 * In case there is only 1 IPv4 address, bind directly.
2998413628a7SBjoern A. Zeeb 		 */
2999b38ff370SJamie Gritton 		if (pr->pr_ip4s == 1)
3000b38ff370SJamie Gritton 			ia->s_addr = pr->pr_ip4[0].s_addr;
3001b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3002413628a7SBjoern A. Zeeb 		return (0);
3003413628a7SBjoern A. Zeeb 	}
3004413628a7SBjoern A. Zeeb 
3005b38ff370SJamie Gritton 	error = _prison_check_ip4(pr, ia);
3006b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3007b38ff370SJamie Gritton 	return (error);
3008413628a7SBjoern A. Zeeb }
3009413628a7SBjoern A. Zeeb 
3010413628a7SBjoern A. Zeeb /*
3011413628a7SBjoern A. Zeeb  * Rewrite destination address in case we will connect to loopback address.
3012413628a7SBjoern A. Zeeb  *
3013b89e82ddSJamie Gritton  * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
3014b89e82ddSJamie Gritton  * Address passed in in NBO and returned in NBO.
3015413628a7SBjoern A. Zeeb  */
3016413628a7SBjoern A. Zeeb int
3017413628a7SBjoern A. Zeeb prison_remote_ip4(struct ucred *cred, struct in_addr *ia)
3018413628a7SBjoern A. Zeeb {
3019b38ff370SJamie Gritton 	struct prison *pr;
3020413628a7SBjoern A. Zeeb 
3021413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3022413628a7SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
3023413628a7SBjoern A. Zeeb 
3024b38ff370SJamie Gritton 	pr = cred->cr_prison;
30250304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4))
30260304c731SJamie Gritton 		return (0);
3027b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
30280304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4)) {
30290304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
30300304c731SJamie Gritton 		return (0);
30310304c731SJamie Gritton 	}
3032b38ff370SJamie Gritton 	if (pr->pr_ip4 == NULL) {
3033b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3034b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
3035b38ff370SJamie Gritton 	}
3036b89e82ddSJamie Gritton 
3037413628a7SBjoern A. Zeeb 	if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
3038b38ff370SJamie Gritton 		ia->s_addr = pr->pr_ip4[0].s_addr;
3039b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3040413628a7SBjoern A. Zeeb 		return (0);
3041413628a7SBjoern A. Zeeb 	}
3042413628a7SBjoern A. Zeeb 
3043413628a7SBjoern A. Zeeb 	/*
3044413628a7SBjoern A. Zeeb 	 * Return success because nothing had to be changed.
3045413628a7SBjoern A. Zeeb 	 */
3046b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3047413628a7SBjoern A. Zeeb 	return (0);
3048413628a7SBjoern A. Zeeb }
3049413628a7SBjoern A. Zeeb 
3050413628a7SBjoern A. Zeeb /*
3051b89e82ddSJamie Gritton  * Check if given address belongs to the jail referenced by cred/prison.
3052413628a7SBjoern A. Zeeb  *
30530304c731SJamie Gritton  * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
30540304c731SJamie Gritton  * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
30550304c731SJamie Gritton  * doesn't allow IPv4.  Address passed in in NBO.
3056413628a7SBjoern A. Zeeb  */
3057413628a7SBjoern A. Zeeb static int
30580d168b8dSGleb Smirnoff _prison_check_ip4(const struct prison *pr, const struct in_addr *ia)
3059413628a7SBjoern A. Zeeb {
3060413628a7SBjoern A. Zeeb 	int i, a, z, d;
3061413628a7SBjoern A. Zeeb 
3062413628a7SBjoern A. Zeeb 	/*
3063413628a7SBjoern A. Zeeb 	 * Check the primary IP.
3064413628a7SBjoern A. Zeeb 	 */
3065413628a7SBjoern A. Zeeb 	if (pr->pr_ip4[0].s_addr == ia->s_addr)
3066b89e82ddSJamie Gritton 		return (0);
3067413628a7SBjoern A. Zeeb 
3068413628a7SBjoern A. Zeeb 	/*
3069413628a7SBjoern A. Zeeb 	 * All the other IPs are sorted so we can do a binary search.
3070413628a7SBjoern A. Zeeb 	 */
3071413628a7SBjoern A. Zeeb 	a = 0;
3072413628a7SBjoern A. Zeeb 	z = pr->pr_ip4s - 2;
3073413628a7SBjoern A. Zeeb 	while (a <= z) {
3074413628a7SBjoern A. Zeeb 		i = (a + z) / 2;
3075413628a7SBjoern A. Zeeb 		d = qcmp_v4(&pr->pr_ip4[i+1], ia);
3076413628a7SBjoern A. Zeeb 		if (d > 0)
3077413628a7SBjoern A. Zeeb 			z = i - 1;
3078413628a7SBjoern A. Zeeb 		else if (d < 0)
3079413628a7SBjoern A. Zeeb 			a = i + 1;
3080413628a7SBjoern A. Zeeb 		else
3081413628a7SBjoern A. Zeeb 			return (0);
308275c13541SPoul-Henning Kamp 	}
308375c13541SPoul-Henning Kamp 
3084b89e82ddSJamie Gritton 	return (EADDRNOTAVAIL);
3085b89e82ddSJamie Gritton }
3086b89e82ddSJamie Gritton 
308775c13541SPoul-Henning Kamp int
30880d168b8dSGleb Smirnoff prison_check_ip4(const struct ucred *cred, const struct in_addr *ia)
3089413628a7SBjoern A. Zeeb {
3090b38ff370SJamie Gritton 	struct prison *pr;
3091b38ff370SJamie Gritton 	int error;
3092413628a7SBjoern A. Zeeb 
3093413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3094413628a7SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
3095413628a7SBjoern A. Zeeb 
3096b38ff370SJamie Gritton 	pr = cred->cr_prison;
30970304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4))
30980304c731SJamie Gritton 		return (0);
3099b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
31000304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4)) {
31010304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
31020304c731SJamie Gritton 		return (0);
31030304c731SJamie Gritton 	}
3104b38ff370SJamie Gritton 	if (pr->pr_ip4 == NULL) {
3105b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3106b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
3107b38ff370SJamie Gritton 	}
3108413628a7SBjoern A. Zeeb 
3109b38ff370SJamie Gritton 	error = _prison_check_ip4(pr, ia);
3110b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3111b38ff370SJamie Gritton 	return (error);
3112413628a7SBjoern A. Zeeb }
3113413628a7SBjoern A. Zeeb #endif
3114413628a7SBjoern A. Zeeb 
3115413628a7SBjoern A. Zeeb #ifdef INET6
31160304c731SJamie Gritton static int
31170304c731SJamie Gritton prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
31180304c731SJamie Gritton {
31190304c731SJamie Gritton 	int ii, ij, used;
31200304c731SJamie Gritton 	struct prison *ppr;
31210304c731SJamie Gritton 
31220304c731SJamie Gritton 	ppr = pr->pr_parent;
31230304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6_USER)) {
31240304c731SJamie Gritton 		/* This has no user settings, so just copy the parent's list. */
31250304c731SJamie Gritton 		if (pr->pr_ip6s < ppr->pr_ip6s) {
31260304c731SJamie Gritton 			/*
31270304c731SJamie Gritton 			 * There's no room for the parent's list.  Use the
31280304c731SJamie Gritton 			 * new list buffer, which is assumed to be big enough
31290304c731SJamie Gritton 			 * (if it was passed).  If there's no buffer, try to
31300304c731SJamie Gritton 			 * allocate one.
31310304c731SJamie Gritton 			 */
31320304c731SJamie Gritton 			used = 1;
31330304c731SJamie Gritton 			if (newip6 == NULL) {
31340304c731SJamie Gritton 				newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
31350304c731SJamie Gritton 				    M_PRISON, M_NOWAIT);
31360304c731SJamie Gritton 				if (newip6 != NULL)
31370304c731SJamie Gritton 					used = 0;
31380304c731SJamie Gritton 			}
31390304c731SJamie Gritton 			if (newip6 != NULL) {
31400304c731SJamie Gritton 				bcopy(ppr->pr_ip6, newip6,
31410304c731SJamie Gritton 				    ppr->pr_ip6s * sizeof(*newip6));
31420304c731SJamie Gritton 				free(pr->pr_ip6, M_PRISON);
31430304c731SJamie Gritton 				pr->pr_ip6 = newip6;
31440304c731SJamie Gritton 				pr->pr_ip6s = ppr->pr_ip6s;
31450304c731SJamie Gritton 			}
31460304c731SJamie Gritton 			return (used);
31470304c731SJamie Gritton 		}
31480304c731SJamie Gritton 		pr->pr_ip6s = ppr->pr_ip6s;
31490304c731SJamie Gritton 		if (pr->pr_ip6s > 0)
31500304c731SJamie Gritton 			bcopy(ppr->pr_ip6, pr->pr_ip6,
31510304c731SJamie Gritton 			    pr->pr_ip6s * sizeof(*newip6));
31520304c731SJamie Gritton 		else if (pr->pr_ip6 != NULL) {
31530304c731SJamie Gritton 			free(pr->pr_ip6, M_PRISON);
31540304c731SJamie Gritton 			pr->pr_ip6 = NULL;
31550304c731SJamie Gritton 		}
31562b0d6f81SJamie Gritton 	} else if (pr->pr_ip6s > 0) {
31570304c731SJamie Gritton 		/* Remove addresses that aren't in the parent. */
31580304c731SJamie Gritton 		for (ij = 0; ij < ppr->pr_ip6s; ij++)
31590304c731SJamie Gritton 			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
31600304c731SJamie Gritton 			    &ppr->pr_ip6[ij]))
31610304c731SJamie Gritton 				break;
31620304c731SJamie Gritton 		if (ij < ppr->pr_ip6s)
31630304c731SJamie Gritton 			ii = 1;
31640304c731SJamie Gritton 		else {
31650304c731SJamie Gritton 			bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
31660304c731SJamie Gritton 			    --pr->pr_ip6s * sizeof(*pr->pr_ip6));
31670304c731SJamie Gritton 			ii = 0;
31680304c731SJamie Gritton 		}
31690304c731SJamie Gritton 		for (ij = 1; ii < pr->pr_ip6s; ) {
31700304c731SJamie Gritton 			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
31710304c731SJamie Gritton 			    &ppr->pr_ip6[0])) {
31720304c731SJamie Gritton 				ii++;
31730304c731SJamie Gritton 				continue;
31740304c731SJamie Gritton 			}
3175da0770bdSAndrey V. Elsukov 			switch (ij >= ppr->pr_ip6s ? -1 :
31760304c731SJamie Gritton 				qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
31770304c731SJamie Gritton 			case -1:
31780304c731SJamie Gritton 				bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
31790304c731SJamie Gritton 				    (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
31800304c731SJamie Gritton 				break;
31810304c731SJamie Gritton 			case 0:
31820304c731SJamie Gritton 				ii++;
31830304c731SJamie Gritton 				ij++;
31840304c731SJamie Gritton 				break;
31850304c731SJamie Gritton 			case 1:
31860304c731SJamie Gritton 				ij++;
31870304c731SJamie Gritton 				break;
31880304c731SJamie Gritton 			}
31890304c731SJamie Gritton 		}
31900304c731SJamie Gritton 		if (pr->pr_ip6s == 0) {
31910304c731SJamie Gritton 			free(pr->pr_ip6, M_PRISON);
31920304c731SJamie Gritton 			pr->pr_ip6 = NULL;
31930304c731SJamie Gritton 		}
31940304c731SJamie Gritton 	}
31950304c731SJamie Gritton 	return 0;
31960304c731SJamie Gritton }
31970304c731SJamie Gritton 
3198413628a7SBjoern A. Zeeb /*
3199413628a7SBjoern A. Zeeb  * Pass back primary IPv6 address for this jail.
3200413628a7SBjoern A. Zeeb  *
32010304c731SJamie Gritton  * If not restricted return success but do not alter the address.  Caller has
32020304c731SJamie Gritton  * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
3203413628a7SBjoern A. Zeeb  *
3204b89e82ddSJamie Gritton  * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3205413628a7SBjoern A. Zeeb  */
3206413628a7SBjoern A. Zeeb int
32071cecba0fSBjoern A. Zeeb prison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
3208413628a7SBjoern A. Zeeb {
3209b38ff370SJamie Gritton 	struct prison *pr;
3210413628a7SBjoern A. Zeeb 
3211413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3212413628a7SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3213413628a7SBjoern A. Zeeb 
3214b38ff370SJamie Gritton 	pr = cred->cr_prison;
32150304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6))
32160304c731SJamie Gritton 		return (0);
3217b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
32180304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6)) {
32190304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
32200304c731SJamie Gritton 		return (0);
32210304c731SJamie Gritton 	}
3222b38ff370SJamie Gritton 	if (pr->pr_ip6 == NULL) {
3223b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3224b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
3225b38ff370SJamie Gritton 	}
3226b89e82ddSJamie Gritton 
3227b38ff370SJamie Gritton 	bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3228b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3229413628a7SBjoern A. Zeeb 	return (0);
3230413628a7SBjoern A. Zeeb }
3231413628a7SBjoern A. Zeeb 
3232413628a7SBjoern A. Zeeb /*
3233592bcae8SBjoern A. Zeeb  * Return 1 if we should do proper source address selection or are not jailed.
3234592bcae8SBjoern A. Zeeb  * We will return 0 if we should bypass source address selection in favour
3235592bcae8SBjoern A. Zeeb  * of the primary jail IPv6 address. Only in this case *ia will be updated and
3236592bcae8SBjoern A. Zeeb  * returned in NBO.
3237592bcae8SBjoern A. Zeeb  * Return EAFNOSUPPORT, in case this jail does not allow IPv6.
3238592bcae8SBjoern A. Zeeb  */
3239592bcae8SBjoern A. Zeeb int
3240592bcae8SBjoern A. Zeeb prison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6)
3241592bcae8SBjoern A. Zeeb {
3242592bcae8SBjoern A. Zeeb 	struct prison *pr;
3243592bcae8SBjoern A. Zeeb 	struct in6_addr lia6;
3244592bcae8SBjoern A. Zeeb 	int error;
3245592bcae8SBjoern A. Zeeb 
3246592bcae8SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3247592bcae8SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3248592bcae8SBjoern A. Zeeb 
3249592bcae8SBjoern A. Zeeb 	if (!jailed(cred))
3250592bcae8SBjoern A. Zeeb 		return (1);
3251592bcae8SBjoern A. Zeeb 
3252592bcae8SBjoern A. Zeeb 	pr = cred->cr_prison;
3253592bcae8SBjoern A. Zeeb 	if (pr->pr_flags & PR_IP6_SADDRSEL)
3254592bcae8SBjoern A. Zeeb 		return (1);
3255592bcae8SBjoern A. Zeeb 
3256592bcae8SBjoern A. Zeeb 	lia6 = in6addr_any;
3257592bcae8SBjoern A. Zeeb 	error = prison_get_ip6(cred, &lia6);
3258592bcae8SBjoern A. Zeeb 	if (error)
3259592bcae8SBjoern A. Zeeb 		return (error);
3260592bcae8SBjoern A. Zeeb 	if (IN6_IS_ADDR_UNSPECIFIED(&lia6))
3261592bcae8SBjoern A. Zeeb 		return (1);
3262592bcae8SBjoern A. Zeeb 
3263592bcae8SBjoern A. Zeeb 	bcopy(&lia6, ia6, sizeof(struct in6_addr));
3264592bcae8SBjoern A. Zeeb 	return (0);
3265592bcae8SBjoern A. Zeeb }
3266592bcae8SBjoern A. Zeeb 
3267592bcae8SBjoern A. Zeeb /*
32680304c731SJamie Gritton  * Return true if pr1 and pr2 have the same IPv6 address restrictions.
32690304c731SJamie Gritton  */
32700304c731SJamie Gritton int
32710304c731SJamie Gritton prison_equal_ip6(struct prison *pr1, struct prison *pr2)
32720304c731SJamie Gritton {
32730304c731SJamie Gritton 
32740304c731SJamie Gritton 	if (pr1 == pr2)
32750304c731SJamie Gritton 		return (1);
32760304c731SJamie Gritton 
3277bdfc8cc4SJamie Gritton 	while (pr1 != &prison0 &&
3278bdfc8cc4SJamie Gritton #ifdef VIMAGE
3279bdfc8cc4SJamie Gritton 	       !(pr1->pr_flags & PR_VNET) &&
3280bdfc8cc4SJamie Gritton #endif
3281bdfc8cc4SJamie Gritton 	       !(pr1->pr_flags & PR_IP6_USER))
32820304c731SJamie Gritton 		pr1 = pr1->pr_parent;
3283bdfc8cc4SJamie Gritton 	while (pr2 != &prison0 &&
3284bdfc8cc4SJamie Gritton #ifdef VIMAGE
3285bdfc8cc4SJamie Gritton 	       !(pr2->pr_flags & PR_VNET) &&
3286bdfc8cc4SJamie Gritton #endif
3287bdfc8cc4SJamie Gritton 	       !(pr2->pr_flags & PR_IP6_USER))
32880304c731SJamie Gritton 		pr2 = pr2->pr_parent;
32890304c731SJamie Gritton 	return (pr1 == pr2);
32900304c731SJamie Gritton }
32910304c731SJamie Gritton 
32920304c731SJamie Gritton /*
3293413628a7SBjoern A. Zeeb  * Make sure our (source) address is set to something meaningful to this jail.
3294413628a7SBjoern A. Zeeb  *
3295413628a7SBjoern A. Zeeb  * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
3296413628a7SBjoern A. Zeeb  * when needed while binding.
3297413628a7SBjoern A. Zeeb  *
32980304c731SJamie Gritton  * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
32990304c731SJamie Gritton  * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
33000304c731SJamie Gritton  * doesn't allow IPv6.
3301413628a7SBjoern A. Zeeb  */
3302413628a7SBjoern A. Zeeb int
3303413628a7SBjoern A. Zeeb prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
3304413628a7SBjoern A. Zeeb {
3305b38ff370SJamie Gritton 	struct prison *pr;
3306b38ff370SJamie Gritton 	int error;
3307413628a7SBjoern A. Zeeb 
3308413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3309413628a7SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3310413628a7SBjoern A. Zeeb 
3311b38ff370SJamie Gritton 	pr = cred->cr_prison;
33120304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6))
33130304c731SJamie Gritton 		return (0);
3314b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
33150304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6)) {
33160304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
33170304c731SJamie Gritton 		return (0);
33180304c731SJamie Gritton 	}
3319b38ff370SJamie Gritton 	if (pr->pr_ip6 == NULL) {
3320b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3321b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
3322b38ff370SJamie Gritton 	}
3323b89e82ddSJamie Gritton 
3324413628a7SBjoern A. Zeeb 	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3325b38ff370SJamie Gritton 		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3326b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3327413628a7SBjoern A. Zeeb 		return (0);
3328413628a7SBjoern A. Zeeb 	}
3329413628a7SBjoern A. Zeeb 
3330b89e82ddSJamie Gritton 	if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3331413628a7SBjoern A. Zeeb 		/*
3332b89e82ddSJamie Gritton 		 * In case there is only 1 IPv6 address, and v6only is true,
3333b89e82ddSJamie Gritton 		 * then bind directly.
3334413628a7SBjoern A. Zeeb 		 */
3335b38ff370SJamie Gritton 		if (v6only != 0 && pr->pr_ip6s == 1)
3336b38ff370SJamie Gritton 			bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3337b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3338413628a7SBjoern A. Zeeb 		return (0);
3339413628a7SBjoern A. Zeeb 	}
3340b89e82ddSJamie Gritton 
3341b38ff370SJamie Gritton 	error = _prison_check_ip6(pr, ia6);
3342b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3343b38ff370SJamie Gritton 	return (error);
3344413628a7SBjoern A. Zeeb }
3345413628a7SBjoern A. Zeeb 
3346413628a7SBjoern A. Zeeb /*
3347413628a7SBjoern A. Zeeb  * Rewrite destination address in case we will connect to loopback address.
3348413628a7SBjoern A. Zeeb  *
3349b89e82ddSJamie Gritton  * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3350413628a7SBjoern A. Zeeb  */
3351413628a7SBjoern A. Zeeb int
3352413628a7SBjoern A. Zeeb prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3353413628a7SBjoern A. Zeeb {
3354b38ff370SJamie Gritton 	struct prison *pr;
3355413628a7SBjoern A. Zeeb 
3356413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3357413628a7SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3358413628a7SBjoern A. Zeeb 
3359b38ff370SJamie Gritton 	pr = cred->cr_prison;
33600304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6))
33610304c731SJamie Gritton 		return (0);
3362b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
33630304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6)) {
33640304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
33650304c731SJamie Gritton 		return (0);
33660304c731SJamie Gritton 	}
3367b38ff370SJamie Gritton 	if (pr->pr_ip6 == NULL) {
3368b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3369b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
3370b38ff370SJamie Gritton 	}
3371b89e82ddSJamie Gritton 
3372413628a7SBjoern A. Zeeb 	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3373b38ff370SJamie Gritton 		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3374b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3375413628a7SBjoern A. Zeeb 		return (0);
3376413628a7SBjoern A. Zeeb 	}
3377413628a7SBjoern A. Zeeb 
3378413628a7SBjoern A. Zeeb 	/*
3379413628a7SBjoern A. Zeeb 	 * Return success because nothing had to be changed.
3380413628a7SBjoern A. Zeeb 	 */
3381b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3382413628a7SBjoern A. Zeeb 	return (0);
3383413628a7SBjoern A. Zeeb }
3384413628a7SBjoern A. Zeeb 
3385413628a7SBjoern A. Zeeb /*
3386b89e82ddSJamie Gritton  * Check if given address belongs to the jail referenced by cred/prison.
3387413628a7SBjoern A. Zeeb  *
33880304c731SJamie Gritton  * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
33890304c731SJamie Gritton  * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
33900304c731SJamie Gritton  * doesn't allow IPv6.
3391413628a7SBjoern A. Zeeb  */
3392413628a7SBjoern A. Zeeb static int
3393413628a7SBjoern A. Zeeb _prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
3394413628a7SBjoern A. Zeeb {
3395413628a7SBjoern A. Zeeb 	int i, a, z, d;
3396413628a7SBjoern A. Zeeb 
3397413628a7SBjoern A. Zeeb 	/*
3398413628a7SBjoern A. Zeeb 	 * Check the primary IP.
3399413628a7SBjoern A. Zeeb 	 */
3400413628a7SBjoern A. Zeeb 	if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3401b89e82ddSJamie Gritton 		return (0);
3402413628a7SBjoern A. Zeeb 
3403413628a7SBjoern A. Zeeb 	/*
3404413628a7SBjoern A. Zeeb 	 * All the other IPs are sorted so we can do a binary search.
3405413628a7SBjoern A. Zeeb 	 */
3406413628a7SBjoern A. Zeeb 	a = 0;
3407413628a7SBjoern A. Zeeb 	z = pr->pr_ip6s - 2;
3408413628a7SBjoern A. Zeeb 	while (a <= z) {
3409413628a7SBjoern A. Zeeb 		i = (a + z) / 2;
3410413628a7SBjoern A. Zeeb 		d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3411413628a7SBjoern A. Zeeb 		if (d > 0)
3412413628a7SBjoern A. Zeeb 			z = i - 1;
3413413628a7SBjoern A. Zeeb 		else if (d < 0)
3414413628a7SBjoern A. Zeeb 			a = i + 1;
3415413628a7SBjoern A. Zeeb 		else
3416413628a7SBjoern A. Zeeb 			return (0);
3417413628a7SBjoern A. Zeeb 	}
3418413628a7SBjoern A. Zeeb 
3419b89e82ddSJamie Gritton 	return (EADDRNOTAVAIL);
3420b89e82ddSJamie Gritton }
3421b89e82ddSJamie Gritton 
3422413628a7SBjoern A. Zeeb int
3423413628a7SBjoern A. Zeeb prison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3424413628a7SBjoern A. Zeeb {
3425b38ff370SJamie Gritton 	struct prison *pr;
3426b38ff370SJamie Gritton 	int error;
3427413628a7SBjoern A. Zeeb 
3428413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3429413628a7SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3430413628a7SBjoern A. Zeeb 
3431b38ff370SJamie Gritton 	pr = cred->cr_prison;
34320304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6))
34330304c731SJamie Gritton 		return (0);
3434b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
34350304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6)) {
34360304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
34370304c731SJamie Gritton 		return (0);
34380304c731SJamie Gritton 	}
3439b38ff370SJamie Gritton 	if (pr->pr_ip6 == NULL) {
3440b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3441b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
3442b38ff370SJamie Gritton 	}
3443413628a7SBjoern A. Zeeb 
3444b38ff370SJamie Gritton 	error = _prison_check_ip6(pr, ia6);
3445b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3446b38ff370SJamie Gritton 	return (error);
3447413628a7SBjoern A. Zeeb }
3448413628a7SBjoern A. Zeeb #endif
3449413628a7SBjoern A. Zeeb 
3450413628a7SBjoern A. Zeeb /*
3451ca04ba64SJamie Gritton  * Check if a jail supports the given address family.
3452ca04ba64SJamie Gritton  *
3453ca04ba64SJamie Gritton  * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3454ca04ba64SJamie Gritton  * if not.
3455ca04ba64SJamie Gritton  */
3456ca04ba64SJamie Gritton int
3457ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af)
3458ca04ba64SJamie Gritton {
34590304c731SJamie Gritton 	struct prison *pr;
3460ca04ba64SJamie Gritton 	int error;
3461ca04ba64SJamie Gritton 
3462ca04ba64SJamie Gritton 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3463ca04ba64SJamie Gritton 
34640304c731SJamie Gritton 	pr = cred->cr_prison;
3465499650a0SJamie Gritton #ifdef VIMAGE
34666bb79563SJamie Gritton 	/* Prisons with their own network stack are not limited. */
3467de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
34686bb79563SJamie Gritton 		return (0);
3469499650a0SJamie Gritton #endif
34706bb79563SJamie Gritton 
3471ca04ba64SJamie Gritton 	error = 0;
3472ca04ba64SJamie Gritton 	switch (af)
3473ca04ba64SJamie Gritton 	{
3474ca04ba64SJamie Gritton #ifdef INET
3475ca04ba64SJamie Gritton 	case AF_INET:
34760304c731SJamie Gritton 		if (pr->pr_flags & PR_IP4)
34770304c731SJamie Gritton 		{
34780304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
34790304c731SJamie Gritton 			if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3480ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
34810304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
34820304c731SJamie Gritton 		}
3483ca04ba64SJamie Gritton 		break;
3484ca04ba64SJamie Gritton #endif
3485ca04ba64SJamie Gritton #ifdef INET6
3486ca04ba64SJamie Gritton 	case AF_INET6:
34870304c731SJamie Gritton 		if (pr->pr_flags & PR_IP6)
34880304c731SJamie Gritton 		{
34890304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
34900304c731SJamie Gritton 			if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3491ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
34920304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
34930304c731SJamie Gritton 		}
3494ca04ba64SJamie Gritton 		break;
3495ca04ba64SJamie Gritton #endif
3496ca04ba64SJamie Gritton 	case AF_LOCAL:
3497ca04ba64SJamie Gritton 	case AF_ROUTE:
3498ca04ba64SJamie Gritton 		break;
3499ca04ba64SJamie Gritton 	default:
35000304c731SJamie Gritton 		if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3501ca04ba64SJamie Gritton 			error = EAFNOSUPPORT;
3502ca04ba64SJamie Gritton 	}
3503ca04ba64SJamie Gritton 	return (error);
3504ca04ba64SJamie Gritton }
3505ca04ba64SJamie Gritton 
3506ca04ba64SJamie Gritton /*
3507413628a7SBjoern A. Zeeb  * Check if given address belongs to the jail referenced by cred (wrapper to
3508413628a7SBjoern A. Zeeb  * prison_check_ip[46]).
3509413628a7SBjoern A. Zeeb  *
35100304c731SJamie Gritton  * Returns 0 if jail doesn't restrict the address family or if address belongs
35110304c731SJamie Gritton  * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
35120304c731SJamie Gritton  * the jail doesn't allow the address family.  IPv4 Address passed in in NBO.
3513413628a7SBjoern A. Zeeb  */
3514413628a7SBjoern A. Zeeb int
351591421ba2SRobert Watson prison_if(struct ucred *cred, struct sockaddr *sa)
351675c13541SPoul-Henning Kamp {
3517413628a7SBjoern A. Zeeb #ifdef INET
35189ddb7954SMike Barcroft 	struct sockaddr_in *sai;
3519413628a7SBjoern A. Zeeb #endif
3520413628a7SBjoern A. Zeeb #ifdef INET6
3521413628a7SBjoern A. Zeeb 	struct sockaddr_in6 *sai6;
3522413628a7SBjoern A. Zeeb #endif
3523b89e82ddSJamie Gritton 	int error;
352475c13541SPoul-Henning Kamp 
3525413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3526413628a7SBjoern A. Zeeb 	KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3527413628a7SBjoern A. Zeeb 
3528de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE
3529de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
3530de0bd6f7SBjoern A. Zeeb 		return (0);
3531de0bd6f7SBjoern A. Zeeb #endif
3532de0bd6f7SBjoern A. Zeeb 
3533b89e82ddSJamie Gritton 	error = 0;
3534413628a7SBjoern A. Zeeb 	switch (sa->sa_family)
3535413628a7SBjoern A. Zeeb 	{
3536413628a7SBjoern A. Zeeb #ifdef INET
3537413628a7SBjoern A. Zeeb 	case AF_INET:
35389ddb7954SMike Barcroft 		sai = (struct sockaddr_in *)sa;
3539b89e82ddSJamie Gritton 		error = prison_check_ip4(cred, &sai->sin_addr);
3540413628a7SBjoern A. Zeeb 		break;
3541413628a7SBjoern A. Zeeb #endif
3542413628a7SBjoern A. Zeeb #ifdef INET6
3543413628a7SBjoern A. Zeeb 	case AF_INET6:
3544413628a7SBjoern A. Zeeb 		sai6 = (struct sockaddr_in6 *)sa;
3545b89e82ddSJamie Gritton 		error = prison_check_ip6(cred, &sai6->sin6_addr);
3546413628a7SBjoern A. Zeeb 		break;
3547413628a7SBjoern A. Zeeb #endif
3548413628a7SBjoern A. Zeeb 	default:
35490304c731SJamie Gritton 		if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3550b89e82ddSJamie Gritton 			error = EAFNOSUPPORT;
3551413628a7SBjoern A. Zeeb 	}
3552b89e82ddSJamie Gritton 	return (error);
355375c13541SPoul-Henning Kamp }
355491421ba2SRobert Watson 
355591421ba2SRobert Watson /*
355691421ba2SRobert Watson  * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
355791421ba2SRobert Watson  */
355891421ba2SRobert Watson int
35599ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2)
356091421ba2SRobert Watson {
356191421ba2SRobert Watson 
35620304c731SJamie Gritton 	return ((cred1->cr_prison == cred2->cr_prison ||
35630304c731SJamie Gritton 	    prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
35640304c731SJamie Gritton }
356591421ba2SRobert Watson 
35660304c731SJamie Gritton /*
35670304c731SJamie Gritton  * Return 1 if p2 is a child of p1, otherwise 0.
35680304c731SJamie Gritton  */
35690304c731SJamie Gritton int
35700304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2)
35710304c731SJamie Gritton {
35720304c731SJamie Gritton 
35730304c731SJamie Gritton 	for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
35740304c731SJamie Gritton 		if (pr1 == pr2)
35750304c731SJamie Gritton 			return (1);
357691421ba2SRobert Watson 	return (0);
357791421ba2SRobert Watson }
357891421ba2SRobert Watson 
357991421ba2SRobert Watson /*
358091421ba2SRobert Watson  * Return 1 if the passed credential is in a jail, otherwise 0.
358191421ba2SRobert Watson  */
358291421ba2SRobert Watson int
35839ddb7954SMike Barcroft jailed(struct ucred *cred)
358491421ba2SRobert Watson {
358591421ba2SRobert Watson 
35860304c731SJamie Gritton 	return (cred->cr_prison != &prison0);
358791421ba2SRobert Watson }
35889484d0c0SRobert Drehmel 
35899484d0c0SRobert Drehmel /*
3590de0bd6f7SBjoern A. Zeeb  * Return 1 if the passed credential is in a jail and that jail does not
3591de0bd6f7SBjoern A. Zeeb  * have its own virtual network stack, otherwise 0.
3592de0bd6f7SBjoern A. Zeeb  */
3593de0bd6f7SBjoern A. Zeeb int
3594de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred)
3595de0bd6f7SBjoern A. Zeeb {
3596de0bd6f7SBjoern A. Zeeb 
3597de0bd6f7SBjoern A. Zeeb 	if (!jailed(cred))
3598de0bd6f7SBjoern A. Zeeb 		return (0);
3599de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE
3600de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
3601de0bd6f7SBjoern A. Zeeb 		return (0);
3602de0bd6f7SBjoern A. Zeeb #endif
3603de0bd6f7SBjoern A. Zeeb 
3604de0bd6f7SBjoern A. Zeeb 	return (1);
3605de0bd6f7SBjoern A. Zeeb }
3606de0bd6f7SBjoern A. Zeeb 
3607de0bd6f7SBjoern A. Zeeb /*
36087455b100SJamie Gritton  * Return the correct hostname (domainname, et al) for the passed credential.
36099484d0c0SRobert Drehmel  */
3610ad1ff099SRobert Drehmel void
36119ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size)
36129484d0c0SRobert Drehmel {
361376ca6f88SJamie Gritton 	struct prison *pr;
36149484d0c0SRobert Drehmel 
36157455b100SJamie Gritton 	/*
36167455b100SJamie Gritton 	 * A NULL credential can be used to shortcut to the physical
36177455b100SJamie Gritton 	 * system's hostname.
36187455b100SJamie Gritton 	 */
361976ca6f88SJamie Gritton 	pr = (cred != NULL) ? cred->cr_prison : &prison0;
362076ca6f88SJamie Gritton 	mtx_lock(&pr->pr_mtx);
3621c1f19219SJamie Gritton 	strlcpy(buf, pr->pr_hostname, size);
362276ca6f88SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
36239484d0c0SRobert Drehmel }
3624fd7a8150SMike Barcroft 
36257455b100SJamie Gritton void
36267455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size)
36277455b100SJamie Gritton {
36287455b100SJamie Gritton 
36297455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
3630c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_domainname, size);
36317455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
36327455b100SJamie Gritton }
36337455b100SJamie Gritton 
36347455b100SJamie Gritton void
36357455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size)
36367455b100SJamie Gritton {
36377455b100SJamie Gritton 
36387455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
3639c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
36407455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
36417455b100SJamie Gritton }
36427455b100SJamie Gritton 
36437455b100SJamie Gritton void
36447455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid)
36457455b100SJamie Gritton {
36467455b100SJamie Gritton 
36477455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
36487455b100SJamie Gritton 	*hostid = cred->cr_prison->pr_hostid;
36497455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
36507455b100SJamie Gritton }
36517455b100SJamie Gritton 
3652eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE
3653eb79e1c7SBjoern A. Zeeb /*
3654eb79e1c7SBjoern A. Zeeb  * Determine whether the prison represented by cred owns
3655eb79e1c7SBjoern A. Zeeb  * its vnet rather than having it inherited.
3656eb79e1c7SBjoern A. Zeeb  *
3657eb79e1c7SBjoern A. Zeeb  * Returns 1 in case the prison owns the vnet, 0 otherwise.
3658eb79e1c7SBjoern A. Zeeb  */
3659eb79e1c7SBjoern A. Zeeb int
3660eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred)
3661eb79e1c7SBjoern A. Zeeb {
3662eb79e1c7SBjoern A. Zeeb 
3663eb79e1c7SBjoern A. Zeeb 	/*
3664eb79e1c7SBjoern A. Zeeb 	 * vnets cannot be added/removed after jail creation,
3665eb79e1c7SBjoern A. Zeeb 	 * so no need to lock here.
3666eb79e1c7SBjoern A. Zeeb 	 */
3667eb79e1c7SBjoern A. Zeeb 	return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3668eb79e1c7SBjoern A. Zeeb }
3669eb79e1c7SBjoern A. Zeeb #endif
3670eb79e1c7SBjoern A. Zeeb 
3671f08df373SRobert Watson /*
3672820a0de9SPawel Jakub Dawidek  * Determine whether the subject represented by cred can "see"
3673820a0de9SPawel Jakub Dawidek  * status of a mount point.
3674820a0de9SPawel Jakub Dawidek  * Returns: 0 for permitted, ENOENT otherwise.
3675820a0de9SPawel Jakub Dawidek  * XXX: This function should be called cr_canseemount() and should be
3676820a0de9SPawel Jakub Dawidek  *      placed in kern_prot.c.
3677f08df373SRobert Watson  */
3678f08df373SRobert Watson int
3679820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp)
3680f08df373SRobert Watson {
3681820a0de9SPawel Jakub Dawidek 	struct prison *pr;
3682820a0de9SPawel Jakub Dawidek 	struct statfs *sp;
3683820a0de9SPawel Jakub Dawidek 	size_t len;
3684f08df373SRobert Watson 
3685820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
36860304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
36870304c731SJamie Gritton 		return (0);
3688820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp)
3689820a0de9SPawel Jakub Dawidek 		return (0);
36900304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 2)
3691820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3692820a0de9SPawel Jakub Dawidek 	/*
3693820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
3694820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
3695820a0de9SPawel Jakub Dawidek 	 * This is ugly check, but this is the only situation when jail's
3696820a0de9SPawel Jakub Dawidek 	 * directory ends with '/'.
3697820a0de9SPawel Jakub Dawidek 	 */
3698820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
3699820a0de9SPawel Jakub Dawidek 		return (0);
3700820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
3701820a0de9SPawel Jakub Dawidek 	sp = &mp->mnt_stat;
3702820a0de9SPawel Jakub Dawidek 	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3703820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3704820a0de9SPawel Jakub Dawidek 	/*
3705820a0de9SPawel Jakub Dawidek 	 * Be sure that we don't have situation where jail's root directory
3706820a0de9SPawel Jakub Dawidek 	 * is "/some/path" and mount point is "/some/pathpath".
3707820a0de9SPawel Jakub Dawidek 	 */
3708820a0de9SPawel Jakub Dawidek 	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3709820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3710f08df373SRobert Watson 	return (0);
3711f08df373SRobert Watson }
3712820a0de9SPawel Jakub Dawidek 
3713820a0de9SPawel Jakub Dawidek void
3714820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3715820a0de9SPawel Jakub Dawidek {
3716820a0de9SPawel Jakub Dawidek 	char jpath[MAXPATHLEN];
3717820a0de9SPawel Jakub Dawidek 	struct prison *pr;
3718820a0de9SPawel Jakub Dawidek 	size_t len;
3719820a0de9SPawel Jakub Dawidek 
3720820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
37210304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
37220304c731SJamie Gritton 		return;
3723820a0de9SPawel Jakub Dawidek 	if (prison_canseemount(cred, mp) != 0) {
3724820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3725820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, "[restricted]",
3726820a0de9SPawel Jakub Dawidek 		    sizeof(sp->f_mntonname));
3727820a0de9SPawel Jakub Dawidek 		return;
3728820a0de9SPawel Jakub Dawidek 	}
3729820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp) {
3730820a0de9SPawel Jakub Dawidek 		/*
3731820a0de9SPawel Jakub Dawidek 		 * Clear current buffer data, so we are sure nothing from
3732820a0de9SPawel Jakub Dawidek 		 * the valid path left there.
3733820a0de9SPawel Jakub Dawidek 		 */
3734820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3735820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3736820a0de9SPawel Jakub Dawidek 		return;
3737820a0de9SPawel Jakub Dawidek 	}
3738820a0de9SPawel Jakub Dawidek 	/*
3739820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
3740820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
3741820a0de9SPawel Jakub Dawidek 	 */
3742820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
3743820a0de9SPawel Jakub Dawidek 		return;
3744820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
3745820a0de9SPawel Jakub Dawidek 	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3746820a0de9SPawel Jakub Dawidek 	/*
3747820a0de9SPawel Jakub Dawidek 	 * Clear current buffer data, so we are sure nothing from
3748820a0de9SPawel Jakub Dawidek 	 * the valid path left there.
3749820a0de9SPawel Jakub Dawidek 	 */
3750820a0de9SPawel Jakub Dawidek 	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3751820a0de9SPawel Jakub Dawidek 	if (*jpath == '\0') {
3752820a0de9SPawel Jakub Dawidek 		/* Should never happen. */
3753820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3754820a0de9SPawel Jakub Dawidek 	} else {
3755820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3756820a0de9SPawel Jakub Dawidek 	}
3757f08df373SRobert Watson }
3758f08df373SRobert Watson 
3759800c9408SRobert Watson /*
3760800c9408SRobert Watson  * Check with permission for a specific privilege is granted within jail.  We
3761800c9408SRobert Watson  * have a specific list of accepted privileges; the rest are denied.
3762800c9408SRobert Watson  */
3763800c9408SRobert Watson int
3764800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv)
3765800c9408SRobert Watson {
3766800c9408SRobert Watson 
3767800c9408SRobert Watson 	if (!jailed(cred))
3768800c9408SRobert Watson 		return (0);
3769800c9408SRobert Watson 
37706bb79563SJamie Gritton #ifdef VIMAGE
37716bb79563SJamie Gritton 	/*
37726bb79563SJamie Gritton 	 * Privileges specific to prisons with a virtual network stack.
37736bb79563SJamie Gritton 	 * There might be a duplicate entry here in case the privilege
37746bb79563SJamie Gritton 	 * is only granted conditionally in the legacy jail case.
37756bb79563SJamie Gritton 	 */
37766bb79563SJamie Gritton 	switch (priv) {
37776bb79563SJamie Gritton #ifdef notyet
37786bb79563SJamie Gritton 		/*
37796bb79563SJamie Gritton 		 * NFS-specific privileges.
37806bb79563SJamie Gritton 		 */
37816bb79563SJamie Gritton 	case PRIV_NFS_DAEMON:
37826bb79563SJamie Gritton 	case PRIV_NFS_LOCKD:
37836bb79563SJamie Gritton #endif
37846bb79563SJamie Gritton 		/*
37856bb79563SJamie Gritton 		 * Network stack privileges.
37866bb79563SJamie Gritton 		 */
37876bb79563SJamie Gritton 	case PRIV_NET_BRIDGE:
37886bb79563SJamie Gritton 	case PRIV_NET_GRE:
37896bb79563SJamie Gritton 	case PRIV_NET_BPF:
37906bb79563SJamie Gritton 	case PRIV_NET_RAW:		/* Dup, cond. in legacy jail case. */
37916bb79563SJamie Gritton 	case PRIV_NET_ROUTE:
37926bb79563SJamie Gritton 	case PRIV_NET_TAP:
37936bb79563SJamie Gritton 	case PRIV_NET_SETIFMTU:
37946bb79563SJamie Gritton 	case PRIV_NET_SETIFFLAGS:
37956bb79563SJamie Gritton 	case PRIV_NET_SETIFCAP:
3796215940b3SXin LI 	case PRIV_NET_SETIFDESCR:
37976bb79563SJamie Gritton 	case PRIV_NET_SETIFNAME	:
37986bb79563SJamie Gritton 	case PRIV_NET_SETIFMETRIC:
37996bb79563SJamie Gritton 	case PRIV_NET_SETIFPHYS:
38006bb79563SJamie Gritton 	case PRIV_NET_SETIFMAC:
38016bb79563SJamie Gritton 	case PRIV_NET_ADDMULTI:
38026bb79563SJamie Gritton 	case PRIV_NET_DELMULTI:
38036bb79563SJamie Gritton 	case PRIV_NET_HWIOCTL:
38046bb79563SJamie Gritton 	case PRIV_NET_SETLLADDR:
38056bb79563SJamie Gritton 	case PRIV_NET_ADDIFGROUP:
38066bb79563SJamie Gritton 	case PRIV_NET_DELIFGROUP:
38076bb79563SJamie Gritton 	case PRIV_NET_IFCREATE:
38086bb79563SJamie Gritton 	case PRIV_NET_IFDESTROY:
38096bb79563SJamie Gritton 	case PRIV_NET_ADDIFADDR:
38106bb79563SJamie Gritton 	case PRIV_NET_DELIFADDR:
38116bb79563SJamie Gritton 	case PRIV_NET_LAGG:
38126bb79563SJamie Gritton 	case PRIV_NET_GIF:
38136bb79563SJamie Gritton 	case PRIV_NET_SETIFVNET:
381435fd7bc0SBjoern A. Zeeb 	case PRIV_NET_SETIFFIB:
38156bb79563SJamie Gritton 
38166bb79563SJamie Gritton 		/*
38176bb79563SJamie Gritton 		 * 802.11-related privileges.
38186bb79563SJamie Gritton 		 */
38196bb79563SJamie Gritton 	case PRIV_NET80211_GETKEY:
38206bb79563SJamie Gritton #ifdef notyet
38216bb79563SJamie Gritton 	case PRIV_NET80211_MANAGE:		/* XXX-BZ discuss with sam@ */
38226bb79563SJamie Gritton #endif
38236bb79563SJamie Gritton 
38246bb79563SJamie Gritton #ifdef notyet
38256bb79563SJamie Gritton 		/*
38266bb79563SJamie Gritton 		 * ATM privileges.
38276bb79563SJamie Gritton 		 */
38286bb79563SJamie Gritton 	case PRIV_NETATM_CFG:
38296bb79563SJamie Gritton 	case PRIV_NETATM_ADD:
38306bb79563SJamie Gritton 	case PRIV_NETATM_DEL:
38316bb79563SJamie Gritton 	case PRIV_NETATM_SET:
38326bb79563SJamie Gritton 
38336bb79563SJamie Gritton 		/*
38346bb79563SJamie Gritton 		 * Bluetooth privileges.
38356bb79563SJamie Gritton 		 */
38366bb79563SJamie Gritton 	case PRIV_NETBLUETOOTH_RAW:
38376bb79563SJamie Gritton #endif
38386bb79563SJamie Gritton 
38396bb79563SJamie Gritton 		/*
38406bb79563SJamie Gritton 		 * Netgraph and netgraph module privileges.
38416bb79563SJamie Gritton 		 */
38426bb79563SJamie Gritton 	case PRIV_NETGRAPH_CONTROL:
38436bb79563SJamie Gritton #ifdef notyet
38446bb79563SJamie Gritton 	case PRIV_NETGRAPH_TTY:
38456bb79563SJamie Gritton #endif
38466bb79563SJamie Gritton 
38476bb79563SJamie Gritton 		/*
38486bb79563SJamie Gritton 		 * IPv4 and IPv6 privileges.
38496bb79563SJamie Gritton 		 */
38506bb79563SJamie Gritton 	case PRIV_NETINET_IPFW:
38516bb79563SJamie Gritton 	case PRIV_NETINET_DIVERT:
38526bb79563SJamie Gritton 	case PRIV_NETINET_PF:
38536bb79563SJamie Gritton 	case PRIV_NETINET_DUMMYNET:
38546bb79563SJamie Gritton 	case PRIV_NETINET_CARP:
38556bb79563SJamie Gritton 	case PRIV_NETINET_MROUTE:
38566bb79563SJamie Gritton 	case PRIV_NETINET_RAW:
38576bb79563SJamie Gritton 	case PRIV_NETINET_ADDRCTRL6:
38586bb79563SJamie Gritton 	case PRIV_NETINET_ND6:
38596bb79563SJamie Gritton 	case PRIV_NETINET_SCOPE6:
38606bb79563SJamie Gritton 	case PRIV_NETINET_ALIFETIME6:
38616bb79563SJamie Gritton 	case PRIV_NETINET_IPSEC:
38626bb79563SJamie Gritton 	case PRIV_NETINET_BINDANY:
38636bb79563SJamie Gritton 
38646bb79563SJamie Gritton #ifdef notyet
38656bb79563SJamie Gritton 		/*
38666bb79563SJamie Gritton 		 * NCP privileges.
38676bb79563SJamie Gritton 		 */
38686bb79563SJamie Gritton 	case PRIV_NETNCP:
38696bb79563SJamie Gritton 
38706bb79563SJamie Gritton 		/*
38716bb79563SJamie Gritton 		 * SMB privileges.
38726bb79563SJamie Gritton 		 */
38736bb79563SJamie Gritton 	case PRIV_NETSMB:
38746bb79563SJamie Gritton #endif
38756bb79563SJamie Gritton 
38766bb79563SJamie Gritton 	/*
38776bb79563SJamie Gritton 	 * No default: or deny here.
38786bb79563SJamie Gritton 	 * In case of no permit fall through to next switch().
38796bb79563SJamie Gritton 	 */
38806bb79563SJamie Gritton 		if (cred->cr_prison->pr_flags & PR_VNET)
38816bb79563SJamie Gritton 			return (0);
38826bb79563SJamie Gritton 	}
38836bb79563SJamie Gritton #endif /* VIMAGE */
38846bb79563SJamie Gritton 
3885800c9408SRobert Watson 	switch (priv) {
3886800c9408SRobert Watson 
3887800c9408SRobert Watson 		/*
3888800c9408SRobert Watson 		 * Allow ktrace privileges for root in jail.
3889800c9408SRobert Watson 		 */
3890800c9408SRobert Watson 	case PRIV_KTRACE:
3891800c9408SRobert Watson 
3892c3c1b5e6SRobert Watson #if 0
3893800c9408SRobert Watson 		/*
3894800c9408SRobert Watson 		 * Allow jailed processes to configure audit identity and
3895800c9408SRobert Watson 		 * submit audit records (login, etc).  In the future we may
3896800c9408SRobert Watson 		 * want to further refine the relationship between audit and
3897800c9408SRobert Watson 		 * jail.
3898800c9408SRobert Watson 		 */
3899800c9408SRobert Watson 	case PRIV_AUDIT_GETAUDIT:
3900800c9408SRobert Watson 	case PRIV_AUDIT_SETAUDIT:
3901800c9408SRobert Watson 	case PRIV_AUDIT_SUBMIT:
3902c3c1b5e6SRobert Watson #endif
3903800c9408SRobert Watson 
3904800c9408SRobert Watson 		/*
3905800c9408SRobert Watson 		 * Allow jailed processes to manipulate process UNIX
3906800c9408SRobert Watson 		 * credentials in any way they see fit.
3907800c9408SRobert Watson 		 */
3908800c9408SRobert Watson 	case PRIV_CRED_SETUID:
3909800c9408SRobert Watson 	case PRIV_CRED_SETEUID:
3910800c9408SRobert Watson 	case PRIV_CRED_SETGID:
3911800c9408SRobert Watson 	case PRIV_CRED_SETEGID:
3912800c9408SRobert Watson 	case PRIV_CRED_SETGROUPS:
3913800c9408SRobert Watson 	case PRIV_CRED_SETREUID:
3914800c9408SRobert Watson 	case PRIV_CRED_SETREGID:
3915800c9408SRobert Watson 	case PRIV_CRED_SETRESUID:
3916800c9408SRobert Watson 	case PRIV_CRED_SETRESGID:
3917800c9408SRobert Watson 
3918800c9408SRobert Watson 		/*
3919800c9408SRobert Watson 		 * Jail implements visibility constraints already, so allow
3920800c9408SRobert Watson 		 * jailed root to override uid/gid-based constraints.
3921800c9408SRobert Watson 		 */
3922800c9408SRobert Watson 	case PRIV_SEEOTHERGIDS:
3923800c9408SRobert Watson 	case PRIV_SEEOTHERUIDS:
3924800c9408SRobert Watson 
3925800c9408SRobert Watson 		/*
3926800c9408SRobert Watson 		 * Jail implements inter-process debugging limits already, so
3927800c9408SRobert Watson 		 * allow jailed root various debugging privileges.
3928800c9408SRobert Watson 		 */
3929800c9408SRobert Watson 	case PRIV_DEBUG_DIFFCRED:
3930800c9408SRobert Watson 	case PRIV_DEBUG_SUGID:
3931800c9408SRobert Watson 	case PRIV_DEBUG_UNPRIV:
3932800c9408SRobert Watson 
3933800c9408SRobert Watson 		/*
3934800c9408SRobert Watson 		 * Allow jail to set various resource limits and login
3935800c9408SRobert Watson 		 * properties, and for now, exceed process resource limits.
3936800c9408SRobert Watson 		 */
3937800c9408SRobert Watson 	case PRIV_PROC_LIMIT:
3938800c9408SRobert Watson 	case PRIV_PROC_SETLOGIN:
3939800c9408SRobert Watson 	case PRIV_PROC_SETRLIMIT:
3940800c9408SRobert Watson 
3941800c9408SRobert Watson 		/*
3942800c9408SRobert Watson 		 * System V and POSIX IPC privileges are granted in jail.
3943800c9408SRobert Watson 		 */
3944800c9408SRobert Watson 	case PRIV_IPC_READ:
3945800c9408SRobert Watson 	case PRIV_IPC_WRITE:
3946800c9408SRobert Watson 	case PRIV_IPC_ADMIN:
3947800c9408SRobert Watson 	case PRIV_IPC_MSGSIZE:
3948800c9408SRobert Watson 	case PRIV_MQ_ADMIN:
3949800c9408SRobert Watson 
3950800c9408SRobert Watson 		/*
39510304c731SJamie Gritton 		 * Jail operations within a jail work on child jails.
39520304c731SJamie Gritton 		 */
39530304c731SJamie Gritton 	case PRIV_JAIL_ATTACH:
39540304c731SJamie Gritton 	case PRIV_JAIL_SET:
39550304c731SJamie Gritton 	case PRIV_JAIL_REMOVE:
39560304c731SJamie Gritton 
39570304c731SJamie Gritton 		/*
3958800c9408SRobert Watson 		 * Jail implements its own inter-process limits, so allow
3959800c9408SRobert Watson 		 * root processes in jail to change scheduling on other
3960800c9408SRobert Watson 		 * processes in the same jail.  Likewise for signalling.
3961800c9408SRobert Watson 		 */
3962800c9408SRobert Watson 	case PRIV_SCHED_DIFFCRED:
3963413628a7SBjoern A. Zeeb 	case PRIV_SCHED_CPUSET:
3964800c9408SRobert Watson 	case PRIV_SIGNAL_DIFFCRED:
3965800c9408SRobert Watson 	case PRIV_SIGNAL_SUGID:
3966800c9408SRobert Watson 
3967800c9408SRobert Watson 		/*
3968800c9408SRobert Watson 		 * Allow jailed processes to write to sysctls marked as jail
3969800c9408SRobert Watson 		 * writable.
3970800c9408SRobert Watson 		 */
3971800c9408SRobert Watson 	case PRIV_SYSCTL_WRITEJAIL:
3972800c9408SRobert Watson 
3973800c9408SRobert Watson 		/*
3974800c9408SRobert Watson 		 * Allow root in jail to manage a variety of quota
3975e82d0201SRobert Watson 		 * properties.  These should likely be conditional on a
3976e82d0201SRobert Watson 		 * configuration option.
3977800c9408SRobert Watson 		 */
397895b091d2SRobert Watson 	case PRIV_VFS_GETQUOTA:
397995b091d2SRobert Watson 	case PRIV_VFS_SETQUOTA:
3980800c9408SRobert Watson 
3981800c9408SRobert Watson 		/*
3982800c9408SRobert Watson 		 * Since Jail relies on chroot() to implement file system
3983800c9408SRobert Watson 		 * protections, grant many VFS privileges to root in jail.
3984800c9408SRobert Watson 		 * Be careful to exclude mount-related and NFS-related
3985800c9408SRobert Watson 		 * privileges.
3986800c9408SRobert Watson 		 */
3987800c9408SRobert Watson 	case PRIV_VFS_READ:
3988800c9408SRobert Watson 	case PRIV_VFS_WRITE:
3989800c9408SRobert Watson 	case PRIV_VFS_ADMIN:
3990800c9408SRobert Watson 	case PRIV_VFS_EXEC:
3991800c9408SRobert Watson 	case PRIV_VFS_LOOKUP:
3992800c9408SRobert Watson 	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
3993800c9408SRobert Watson 	case PRIV_VFS_CHFLAGS_DEV:
3994800c9408SRobert Watson 	case PRIV_VFS_CHOWN:
3995800c9408SRobert Watson 	case PRIV_VFS_CHROOT:
3996bb531912SPawel Jakub Dawidek 	case PRIV_VFS_RETAINSUGID:
3997800c9408SRobert Watson 	case PRIV_VFS_FCHROOT:
3998800c9408SRobert Watson 	case PRIV_VFS_LINK:
3999800c9408SRobert Watson 	case PRIV_VFS_SETGID:
4000e41966dcSRobert Watson 	case PRIV_VFS_STAT:
4001800c9408SRobert Watson 	case PRIV_VFS_STICKYFILE:
4002bb56d716SJamie Gritton 
4003bb56d716SJamie Gritton 		/*
4004bb56d716SJamie Gritton 		 * As in the non-jail case, non-root users are expected to be
4005bb56d716SJamie Gritton 		 * able to read kernel/phyiscal memory (provided /dev/[k]mem
4006bb56d716SJamie Gritton 		 * exists in the jail and they have permission to access it).
4007bb56d716SJamie Gritton 		 */
4008bb56d716SJamie Gritton 	case PRIV_KMEM_READ:
4009800c9408SRobert Watson 		return (0);
4010800c9408SRobert Watson 
4011800c9408SRobert Watson 		/*
4012800c9408SRobert Watson 		 * Depending on the global setting, allow privilege of
4013800c9408SRobert Watson 		 * setting system flags.
4014800c9408SRobert Watson 		 */
4015800c9408SRobert Watson 	case PRIV_VFS_SYSFLAGS:
40160304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
4017800c9408SRobert Watson 			return (0);
4018800c9408SRobert Watson 		else
4019800c9408SRobert Watson 			return (EPERM);
4020800c9408SRobert Watson 
4021800c9408SRobert Watson 		/*
4022f3a8d2f9SPawel Jakub Dawidek 		 * Depending on the global setting, allow privilege of
4023f3a8d2f9SPawel Jakub Dawidek 		 * mounting/unmounting file systems.
4024f3a8d2f9SPawel Jakub Dawidek 		 */
4025f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT:
4026f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_UNMOUNT:
4027f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_NONUSER:
402824b0502eSPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_OWNER:
4029435d4667SMartin Matuska 		if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT &&
4030435d4667SMartin Matuska 		    cred->cr_prison->pr_enforce_statfs < 2)
4031f3a8d2f9SPawel Jakub Dawidek 			return (0);
4032f3a8d2f9SPawel Jakub Dawidek 		else
4033f3a8d2f9SPawel Jakub Dawidek 			return (EPERM);
4034f3a8d2f9SPawel Jakub Dawidek 
4035f3a8d2f9SPawel Jakub Dawidek 		/*
40364b084056SRobert Watson 		 * Allow jailed root to bind reserved ports and reuse in-use
40374b084056SRobert Watson 		 * ports.
4038800c9408SRobert Watson 		 */
4039800c9408SRobert Watson 	case PRIV_NETINET_RESERVEDPORT:
40404b084056SRobert Watson 	case PRIV_NETINET_REUSEPORT:
4041800c9408SRobert Watson 		return (0);
4042800c9408SRobert Watson 
4043800c9408SRobert Watson 		/*
4044e3043798SPedro F. Giffuni 		 * Allow jailed root to set certain IPv4/6 (option) headers.
404579ba3952SBjoern A. Zeeb 		 */
404679ba3952SBjoern A. Zeeb 	case PRIV_NETINET_SETHDROPTS:
404779ba3952SBjoern A. Zeeb 		return (0);
404879ba3952SBjoern A. Zeeb 
404979ba3952SBjoern A. Zeeb 		/*
4050800c9408SRobert Watson 		 * Conditionally allow creating raw sockets in jail.
4051800c9408SRobert Watson 		 */
4052800c9408SRobert Watson 	case PRIV_NETINET_RAW:
40530304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
4054800c9408SRobert Watson 			return (0);
4055800c9408SRobert Watson 		else
4056800c9408SRobert Watson 			return (EPERM);
4057800c9408SRobert Watson 
4058800c9408SRobert Watson 		/*
4059800c9408SRobert Watson 		 * Since jail implements its own visibility limits on netstat
4060800c9408SRobert Watson 		 * sysctls, allow getcred.  This allows identd to work in
4061800c9408SRobert Watson 		 * jail.
4062800c9408SRobert Watson 		 */
4063800c9408SRobert Watson 	case PRIV_NETINET_GETCRED:
4064800c9408SRobert Watson 		return (0);
4065800c9408SRobert Watson 
40662bfc50bcSEdward Tomasz Napierala 		/*
40672bfc50bcSEdward Tomasz Napierala 		 * Allow jailed root to set loginclass.
40682bfc50bcSEdward Tomasz Napierala 		 */
40692bfc50bcSEdward Tomasz Napierala 	case PRIV_PROC_SETLOGINCLASS:
40702bfc50bcSEdward Tomasz Napierala 		return (0);
40712bfc50bcSEdward Tomasz Napierala 
4072800c9408SRobert Watson 	default:
4073800c9408SRobert Watson 		/*
4074800c9408SRobert Watson 		 * In all remaining cases, deny the privilege request.  This
4075800c9408SRobert Watson 		 * includes almost all network privileges, many system
4076800c9408SRobert Watson 		 * configuration privileges.
4077800c9408SRobert Watson 		 */
4078800c9408SRobert Watson 		return (EPERM);
4079800c9408SRobert Watson 	}
4080800c9408SRobert Watson }
4081800c9408SRobert Watson 
40820304c731SJamie Gritton /*
40830304c731SJamie Gritton  * Return the part of pr2's name that is relative to pr1, or the whole name
40840304c731SJamie Gritton  * if it does not directly follow.
40850304c731SJamie Gritton  */
40860304c731SJamie Gritton 
40870304c731SJamie Gritton char *
40880304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2)
40890304c731SJamie Gritton {
40900304c731SJamie Gritton 	char *name;
40910304c731SJamie Gritton 
40920304c731SJamie Gritton 	/* Jails see themselves as "0" (if they see themselves at all). */
40930304c731SJamie Gritton 	if (pr1 == pr2)
40940304c731SJamie Gritton 		return "0";
40950304c731SJamie Gritton 	name = pr2->pr_name;
40960304c731SJamie Gritton 	if (prison_ischild(pr1, pr2)) {
40970304c731SJamie Gritton 		/*
40980304c731SJamie Gritton 		 * pr1 isn't locked (and allprison_lock may not be either)
40990304c731SJamie Gritton 		 * so its length can't be counted on.  But the number of dots
41000304c731SJamie Gritton 		 * can be counted on - and counted.
41010304c731SJamie Gritton 		 */
41020304c731SJamie Gritton 		for (; pr1 != &prison0; pr1 = pr1->pr_parent)
41030304c731SJamie Gritton 			name = strchr(name, '.') + 1;
41040304c731SJamie Gritton 	}
41050304c731SJamie Gritton 	return (name);
41060304c731SJamie Gritton }
41070304c731SJamie Gritton 
41080304c731SJamie Gritton /*
41090304c731SJamie Gritton  * Return the part of pr2's path that is relative to pr1, or the whole path
41100304c731SJamie Gritton  * if it does not directly follow.
41110304c731SJamie Gritton  */
41120304c731SJamie Gritton static char *
41130304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2)
41140304c731SJamie Gritton {
41150304c731SJamie Gritton 	char *path1, *path2;
41160304c731SJamie Gritton 	int len1;
41170304c731SJamie Gritton 
41180304c731SJamie Gritton 	path1 = pr1->pr_path;
41190304c731SJamie Gritton 	path2 = pr2->pr_path;
41200304c731SJamie Gritton 	if (!strcmp(path1, "/"))
41210304c731SJamie Gritton 		return (path2);
41220304c731SJamie Gritton 	len1 = strlen(path1);
41230304c731SJamie Gritton 	if (strncmp(path1, path2, len1))
41240304c731SJamie Gritton 		return (path2);
41250304c731SJamie Gritton 	if (path2[len1] == '\0')
41260304c731SJamie Gritton 		return "/";
41270304c731SJamie Gritton 	if (path2[len1] == '/')
41280304c731SJamie Gritton 		return (path2 + len1);
41290304c731SJamie Gritton 	return (path2);
41300304c731SJamie Gritton }
41310304c731SJamie Gritton 
41320304c731SJamie Gritton 
41330304c731SJamie Gritton /*
41340304c731SJamie Gritton  * Jail-related sysctls.
41350304c731SJamie Gritton  */
41366472ac3dSEd Schouten static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
41370304c731SJamie Gritton     "Jails");
41380304c731SJamie Gritton 
4139fd7a8150SMike Barcroft static int
4140fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS)
4141fd7a8150SMike Barcroft {
4142b38ff370SJamie Gritton 	struct xprison *xp;
41430304c731SJamie Gritton 	struct prison *pr, *cpr;
4144b38ff370SJamie Gritton #ifdef INET
4145b38ff370SJamie Gritton 	struct in_addr *ip4 = NULL;
4146b38ff370SJamie Gritton 	int ip4s = 0;
4147b38ff370SJamie Gritton #endif
4148b38ff370SJamie Gritton #ifdef INET6
41493beefaedSColin Percival 	struct in6_addr *ip6 = NULL;
4150b38ff370SJamie Gritton 	int ip6s = 0;
4151b38ff370SJamie Gritton #endif
41520304c731SJamie Gritton 	int descend, error;
4153fd7a8150SMike Barcroft 
4154b38ff370SJamie Gritton 	xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
41550304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
4156b38ff370SJamie Gritton 	error = 0;
4157dc68a633SPawel Jakub Dawidek 	sx_slock(&allprison_lock);
41580304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
41590304c731SJamie Gritton #if defined(INET) || defined(INET6)
4160b38ff370SJamie Gritton  again:
41610304c731SJamie Gritton #endif
41620304c731SJamie Gritton 		mtx_lock(&cpr->pr_mtx);
4163413628a7SBjoern A. Zeeb #ifdef INET
41640304c731SJamie Gritton 		if (cpr->pr_ip4s > 0) {
41650304c731SJamie Gritton 			if (ip4s < cpr->pr_ip4s) {
41660304c731SJamie Gritton 				ip4s = cpr->pr_ip4s;
41670304c731SJamie Gritton 				mtx_unlock(&cpr->pr_mtx);
4168b38ff370SJamie Gritton 				ip4 = realloc(ip4, ip4s *
4169b38ff370SJamie Gritton 				    sizeof(struct in_addr), M_TEMP, M_WAITOK);
4170b38ff370SJamie Gritton 				goto again;
4171b38ff370SJamie Gritton 			}
41720304c731SJamie Gritton 			bcopy(cpr->pr_ip4, ip4,
41730304c731SJamie Gritton 			    cpr->pr_ip4s * sizeof(struct in_addr));
4174b38ff370SJamie Gritton 		}
4175413628a7SBjoern A. Zeeb #endif
4176413628a7SBjoern A. Zeeb #ifdef INET6
41770304c731SJamie Gritton 		if (cpr->pr_ip6s > 0) {
41780304c731SJamie Gritton 			if (ip6s < cpr->pr_ip6s) {
41790304c731SJamie Gritton 				ip6s = cpr->pr_ip6s;
41800304c731SJamie Gritton 				mtx_unlock(&cpr->pr_mtx);
4181b38ff370SJamie Gritton 				ip6 = realloc(ip6, ip6s *
4182b38ff370SJamie Gritton 				    sizeof(struct in6_addr), M_TEMP, M_WAITOK);
4183b38ff370SJamie Gritton 				goto again;
4184413628a7SBjoern A. Zeeb 			}
41850304c731SJamie Gritton 			bcopy(cpr->pr_ip6, ip6,
41860304c731SJamie Gritton 			    cpr->pr_ip6s * sizeof(struct in6_addr));
4187b38ff370SJamie Gritton 		}
4188b38ff370SJamie Gritton #endif
41890304c731SJamie Gritton 		if (cpr->pr_ref == 0) {
41900304c731SJamie Gritton 			mtx_unlock(&cpr->pr_mtx);
4191b38ff370SJamie Gritton 			continue;
4192b38ff370SJamie Gritton 		}
4193b38ff370SJamie Gritton 		bzero(xp, sizeof(*xp));
4194fd7a8150SMike Barcroft 		xp->pr_version = XPRISON_VERSION;
41950304c731SJamie Gritton 		xp->pr_id = cpr->pr_id;
41960304c731SJamie Gritton 		xp->pr_state = cpr->pr_uref > 0
4197b38ff370SJamie Gritton 		    ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
41980304c731SJamie Gritton 		strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
4199c1f19219SJamie Gritton 		strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
42000304c731SJamie Gritton 		strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
4201413628a7SBjoern A. Zeeb #ifdef INET
42020304c731SJamie Gritton 		xp->pr_ip4s = cpr->pr_ip4s;
4203413628a7SBjoern A. Zeeb #endif
4204413628a7SBjoern A. Zeeb #ifdef INET6
42050304c731SJamie Gritton 		xp->pr_ip6s = cpr->pr_ip6s;
4206413628a7SBjoern A. Zeeb #endif
42070304c731SJamie Gritton 		mtx_unlock(&cpr->pr_mtx);
4208b38ff370SJamie Gritton 		error = SYSCTL_OUT(req, xp, sizeof(*xp));
4209b38ff370SJamie Gritton 		if (error)
4210b38ff370SJamie Gritton 			break;
4211413628a7SBjoern A. Zeeb #ifdef INET
4212b38ff370SJamie Gritton 		if (xp->pr_ip4s > 0) {
4213b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip4,
4214b38ff370SJamie Gritton 			    xp->pr_ip4s * sizeof(struct in_addr));
4215b38ff370SJamie Gritton 			if (error)
4216b38ff370SJamie Gritton 				break;
4217413628a7SBjoern A. Zeeb 		}
4218413628a7SBjoern A. Zeeb #endif
4219413628a7SBjoern A. Zeeb #ifdef INET6
4220b38ff370SJamie Gritton 		if (xp->pr_ip6s > 0) {
4221b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip6,
4222b38ff370SJamie Gritton 			    xp->pr_ip6s * sizeof(struct in6_addr));
4223b38ff370SJamie Gritton 			if (error)
4224b38ff370SJamie Gritton 				break;
4225413628a7SBjoern A. Zeeb 		}
4226413628a7SBjoern A. Zeeb #endif
4227fd7a8150SMike Barcroft 	}
4228dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
4229b38ff370SJamie Gritton 	free(xp, M_TEMP);
4230b38ff370SJamie Gritton #ifdef INET
4231b38ff370SJamie Gritton 	free(ip4, M_TEMP);
4232b38ff370SJamie Gritton #endif
4233b38ff370SJamie Gritton #ifdef INET6
4234b38ff370SJamie Gritton 	free(ip6, M_TEMP);
4235b38ff370SJamie Gritton #endif
4236fd7a8150SMike Barcroft 	return (error);
4237fd7a8150SMike Barcroft }
4238fd7a8150SMike Barcroft 
4239f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list,
4240f3b86a5fSEd Schouten     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4241f3b86a5fSEd Schouten     sysctl_jail_list, "S", "List of active jails");
4242461167c2SPawel Jakub Dawidek 
4243461167c2SPawel Jakub Dawidek static int
4244461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4245461167c2SPawel Jakub Dawidek {
4246461167c2SPawel Jakub Dawidek 	int error, injail;
4247461167c2SPawel Jakub Dawidek 
4248461167c2SPawel Jakub Dawidek 	injail = jailed(req->td->td_ucred);
4249461167c2SPawel Jakub Dawidek 	error = SYSCTL_OUT(req, &injail, sizeof(injail));
4250461167c2SPawel Jakub Dawidek 
4251461167c2SPawel Jakub Dawidek 	return (error);
4252461167c2SPawel Jakub Dawidek }
42530304c731SJamie Gritton 
4254f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
4255f3b86a5fSEd Schouten     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4256f3b86a5fSEd Schouten     sysctl_jail_jailed, "I", "Process in jail?");
4257413628a7SBjoern A. Zeeb 
4258761d2bb5SJamie Gritton static int
4259761d2bb5SJamie Gritton sysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
4260761d2bb5SJamie Gritton {
4261761d2bb5SJamie Gritton 	int error, havevnet;
4262761d2bb5SJamie Gritton #ifdef VIMAGE
4263761d2bb5SJamie Gritton 	struct ucred *cred = req->td->td_ucred;
4264761d2bb5SJamie Gritton 
4265761d2bb5SJamie Gritton 	havevnet = jailed(cred) && prison_owns_vnet(cred);
4266761d2bb5SJamie Gritton #else
4267761d2bb5SJamie Gritton 	havevnet = 0;
4268761d2bb5SJamie Gritton #endif
4269761d2bb5SJamie Gritton 	error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
4270761d2bb5SJamie Gritton 
4271761d2bb5SJamie Gritton 	return (error);
4272761d2bb5SJamie Gritton }
4273761d2bb5SJamie Gritton 
4274761d2bb5SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, vnet,
4275761d2bb5SJamie Gritton     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4276761d2bb5SJamie Gritton     sysctl_jail_vnet, "I", "Jail owns VNET?");
4277761d2bb5SJamie Gritton 
42780304c731SJamie Gritton #if defined(INET) || defined(INET6)
4279e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
42800304c731SJamie Gritton     &jail_max_af_ips, 0,
4281ee8d6bd3SJamie Gritton     "Number of IP addresses a jail may have at most per address family (deprecated)");
42820304c731SJamie Gritton #endif
42830304c731SJamie Gritton 
42840304c731SJamie Gritton /*
4285e3043798SPedro F. Giffuni  * Default parameters for jail(2) compatibility.  For historical reasons,
42860304c731SJamie Gritton  * the sysctl names have varying similarity to the parameter names.  Prisons
42870304c731SJamie Gritton  * just see their own parameters, and can't change them.
42880304c731SJamie Gritton  */
42890304c731SJamie Gritton static int
42900304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
42910304c731SJamie Gritton {
42920304c731SJamie Gritton 	struct prison *pr;
42930304c731SJamie Gritton 	int allow, error, i;
42940304c731SJamie Gritton 
42950304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
42960304c731SJamie Gritton 	allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
42970304c731SJamie Gritton 
42980304c731SJamie Gritton 	/* Get the current flag value, and convert it to a boolean. */
42990304c731SJamie Gritton 	i = (allow & arg2) ? 1 : 0;
43000304c731SJamie Gritton 	if (arg1 != NULL)
43010304c731SJamie Gritton 		i = !i;
43020304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &i, 0, req);
43030304c731SJamie Gritton 	if (error || !req->newptr)
43040304c731SJamie Gritton 		return (error);
43050304c731SJamie Gritton 	i = i ? arg2 : 0;
43060304c731SJamie Gritton 	if (arg1 != NULL)
43070304c731SJamie Gritton 		i ^= arg2;
43080304c731SJamie Gritton 	/*
43090304c731SJamie Gritton 	 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
43100304c731SJamie Gritton 	 * for writing.
43110304c731SJamie Gritton 	 */
43120304c731SJamie Gritton 	mtx_lock(&prison0.pr_mtx);
43130304c731SJamie Gritton 	jail_default_allow = (jail_default_allow & ~arg2) | i;
43140304c731SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
43150304c731SJamie Gritton 	return (0);
43160304c731SJamie Gritton }
43170304c731SJamie Gritton 
43180304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
43190304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
43200304c731SJamie Gritton     NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
4321ee8d6bd3SJamie Gritton     "Processes in jail can set their hostnames (deprecated)");
43220304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
43230304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
43240304c731SJamie Gritton     (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
4325ee8d6bd3SJamie Gritton     "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)");
43260304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
43270304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
43280304c731SJamie Gritton     NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4329ee8d6bd3SJamie Gritton     "Processes in jail can use System V IPC primitives (deprecated)");
43300304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
43310304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
43320304c731SJamie Gritton     NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4333ee8d6bd3SJamie Gritton     "Prison root can create raw sockets (deprecated)");
43340304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
43350304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
43360304c731SJamie Gritton     NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4337ee8d6bd3SJamie Gritton     "Processes in jail can alter system file flags (deprecated)");
43380304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
43390304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
43400304c731SJamie Gritton     NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4341ee8d6bd3SJamie Gritton     "Processes in jail can mount/unmount jail-friendly file systems (deprecated)");
4342bf3db8aaSMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, mount_devfs_allowed,
4343bf3db8aaSMartin Matuska     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4344bf3db8aaSMartin Matuska     NULL, PR_ALLOW_MOUNT_DEVFS, sysctl_jail_default_allow, "I",
4345ee8d6bd3SJamie Gritton     "Processes in jail can mount the devfs file system (deprecated)");
4346464aad14SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_fdescfs_allowed,
4347464aad14SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4348464aad14SJamie Gritton     NULL, PR_ALLOW_MOUNT_FDESCFS, sysctl_jail_default_allow, "I",
4349ee8d6bd3SJamie Gritton     "Processes in jail can mount the fdescfs file system (deprecated)");
4350bf3db8aaSMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, mount_nullfs_allowed,
4351bf3db8aaSMartin Matuska     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4352bf3db8aaSMartin Matuska     NULL, PR_ALLOW_MOUNT_NULLFS, sysctl_jail_default_allow, "I",
4353ee8d6bd3SJamie Gritton     "Processes in jail can mount the nullfs file system (deprecated)");
435441c0675eSMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, mount_procfs_allowed,
435541c0675eSMartin Matuska     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
435641c0675eSMartin Matuska     NULL, PR_ALLOW_MOUNT_PROCFS, sysctl_jail_default_allow, "I",
4357ee8d6bd3SJamie Gritton     "Processes in jail can mount the procfs file system (deprecated)");
4358f19e47d6SMarcelo Araujo SYSCTL_PROC(_security_jail, OID_AUTO, mount_linprocfs_allowed,
4359f19e47d6SMarcelo Araujo     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4360f19e47d6SMarcelo Araujo     NULL, PR_ALLOW_MOUNT_LINPROCFS, sysctl_jail_default_allow, "I",
4361ee8d6bd3SJamie Gritton     "Processes in jail can mount the linprocfs file system (deprecated)");
4362f19e47d6SMarcelo Araujo SYSCTL_PROC(_security_jail, OID_AUTO, mount_linsysfs_allowed,
4363f19e47d6SMarcelo Araujo     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4364f19e47d6SMarcelo Araujo     NULL, PR_ALLOW_MOUNT_LINSYSFS, sysctl_jail_default_allow, "I",
4365ee8d6bd3SJamie Gritton     "Processes in jail can mount the linsysfs file system (deprecated)");
43662454886eSXin LI SYSCTL_PROC(_security_jail, OID_AUTO, mount_tmpfs_allowed,
43672454886eSXin LI     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
43682454886eSXin LI     NULL, PR_ALLOW_MOUNT_TMPFS, sysctl_jail_default_allow, "I",
4369ee8d6bd3SJamie Gritton     "Processes in jail can mount the tmpfs file system (deprecated)");
4370e7af90abSMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, mount_zfs_allowed,
4371e7af90abSMartin Matuska     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4372e7af90abSMartin Matuska     NULL, PR_ALLOW_MOUNT_ZFS, sysctl_jail_default_allow, "I",
4373ee8d6bd3SJamie Gritton     "Processes in jail can mount the zfs file system (deprecated)");
43740304c731SJamie Gritton 
43750304c731SJamie Gritton static int
43760304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
43770304c731SJamie Gritton {
43780304c731SJamie Gritton 	struct prison *pr;
43790304c731SJamie Gritton 	int level, error;
43800304c731SJamie Gritton 
43810304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
43820304c731SJamie Gritton 	level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
43830304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &level, 0, req);
43840304c731SJamie Gritton 	if (error || !req->newptr)
43850304c731SJamie Gritton 		return (error);
43860304c731SJamie Gritton 	*(int *)arg1 = level;
43870304c731SJamie Gritton 	return (0);
43880304c731SJamie Gritton }
43890304c731SJamie Gritton 
43900304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
43910304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
43920304c731SJamie Gritton     &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
43930304c731SJamie Gritton     sysctl_jail_default_level, "I",
4394ee8d6bd3SJamie Gritton     "Processes in jail cannot see all mounted file systems (deprecated)");
43950304c731SJamie Gritton 
43960cc207a6SMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
43970cc207a6SMartin Matuska     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
43980cc207a6SMartin Matuska     &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
43990cc207a6SMartin Matuska     sysctl_jail_default_level, "I",
4400ee8d6bd3SJamie Gritton     "Ruleset for the devfs filesystem in jail (deprecated)");
44010cc207a6SMartin Matuska 
44020304c731SJamie Gritton /*
44030304c731SJamie Gritton  * Nodes to describe jail parameters.  Maximum length of string parameters
44040304c731SJamie Gritton  * is returned in the string itself, and the other parameters exist merely
44050304c731SJamie Gritton  * to make themselves and their types known.
44060304c731SJamie Gritton  */
44070304c731SJamie Gritton SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
44080304c731SJamie Gritton     "Jail parameters");
44090304c731SJamie Gritton 
44100304c731SJamie Gritton int
44110304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS)
44120304c731SJamie Gritton {
44130304c731SJamie Gritton 	int i;
44140304c731SJamie Gritton 	long l;
44150304c731SJamie Gritton 	size_t s;
44160304c731SJamie Gritton 	char numbuf[12];
44170304c731SJamie Gritton 
44180304c731SJamie Gritton 	switch (oidp->oid_kind & CTLTYPE)
44190304c731SJamie Gritton 	{
44200304c731SJamie Gritton 	case CTLTYPE_LONG:
44210304c731SJamie Gritton 	case CTLTYPE_ULONG:
44220304c731SJamie Gritton 		l = 0;
44230304c731SJamie Gritton #ifdef SCTL_MASK32
44240304c731SJamie Gritton 		if (!(req->flags & SCTL_MASK32))
44250304c731SJamie Gritton #endif
44260304c731SJamie Gritton 			return (SYSCTL_OUT(req, &l, sizeof(l)));
44270304c731SJamie Gritton 	case CTLTYPE_INT:
44280304c731SJamie Gritton 	case CTLTYPE_UINT:
44290304c731SJamie Gritton 		i = 0;
44300304c731SJamie Gritton 		return (SYSCTL_OUT(req, &i, sizeof(i)));
44310304c731SJamie Gritton 	case CTLTYPE_STRING:
4432e4cd31ddSJeff Roberson 		snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
44330304c731SJamie Gritton 		return
44340304c731SJamie Gritton 		    (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
44350304c731SJamie Gritton 	case CTLTYPE_STRUCT:
44360304c731SJamie Gritton 		s = (size_t)arg2;
44370304c731SJamie Gritton 		return (SYSCTL_OUT(req, &s, sizeof(s)));
44380304c731SJamie Gritton 	}
44390304c731SJamie Gritton 	return (0);
44400304c731SJamie Gritton }
44410304c731SJamie Gritton 
4442b96bd95bSIan Lepore /*
4443b96bd95bSIan Lepore  * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at
4444b96bd95bSIan Lepore  * jail creation time but cannot be changed in an existing jail.
4445b96bd95bSIan Lepore  */
44460304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
44470304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
44480304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
44490304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
44500304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
44510304c731SJamie Gritton     "I", "Jail secure level");
4452b96bd95bSIan Lepore SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I",
4453b96bd95bSIan Lepore     "Jail value for kern.osreldate and uname -K");
4454b96bd95bSIan Lepore SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN,
4455b96bd95bSIan Lepore     "Jail value for kern.osrelease and uname -r");
44560304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
44570304c731SJamie Gritton     "I", "Jail cannot see all mounted file systems");
44580cc207a6SMartin Matuska SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
44590cc207a6SMartin Matuska     "I", "Ruleset for in-jail devfs mounts");
44600304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
44610304c731SJamie Gritton     "B", "Jail persistence");
4462679e1390SJamie Gritton #ifdef VIMAGE
4463679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
44647cbf7213SJamie Gritton     "E,jailsys", "Virtual network stack");
4465679e1390SJamie Gritton #endif
44660304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
44670304c731SJamie Gritton     "B", "Jail is in the process of shutting down");
44680304c731SJamie Gritton 
4469b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4470b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4471b97457e2SJamie Gritton     "I", "Current number of child jails");
4472b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4473b97457e2SJamie Gritton     "I", "Maximum number of child jails");
4474b97457e2SJamie Gritton 
44757cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
44760304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
44770304c731SJamie Gritton     "Jail hostname");
447876ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
447976ca6f88SJamie Gritton     "Jail NIS domainname");
448076ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
448176ca6f88SJamie Gritton     "Jail host UUID");
448276ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
448376ca6f88SJamie Gritton     "LU", "Jail host ID");
44840304c731SJamie Gritton 
44850304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
44860304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
44870304c731SJamie Gritton 
44880304c731SJamie Gritton #ifdef INET
44892b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
44902b0d6f81SJamie Gritton     "Jail IPv4 address virtualization");
44910304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
44920304c731SJamie Gritton     "S,in_addr,a", "Jail IPv4 addresses");
4493592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4494592bcae8SBjoern A. Zeeb     "B", "Do (not) use IPv4 source address selection rather than the "
4495592bcae8SBjoern A. Zeeb     "primary jail IPv4 address.");
44960304c731SJamie Gritton #endif
44970304c731SJamie Gritton #ifdef INET6
44982b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
44992b0d6f81SJamie Gritton     "Jail IPv6 address virtualization");
45000304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
45010304c731SJamie Gritton     "S,in6_addr,a", "Jail IPv6 addresses");
4502592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4503592bcae8SBjoern A. Zeeb     "B", "Do (not) use IPv6 source address selection rather than the "
4504592bcae8SBjoern A. Zeeb     "primary jail IPv6 address.");
45050304c731SJamie Gritton #endif
45060304c731SJamie Gritton 
45070304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
45080304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
45090304c731SJamie Gritton     "B", "Jail may set hostname");
45100304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
45110304c731SJamie Gritton     "B", "Jail may use SYSV IPC");
45120304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
45130304c731SJamie Gritton     "B", "Jail may create raw sockets");
45140304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
45150304c731SJamie Gritton     "B", "Jail may alter system file flags");
45160304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
45170304c731SJamie Gritton     "B", "Jail may set file quotas");
45180304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
45190304c731SJamie Gritton     "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
45200304c731SJamie Gritton 
4521bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
4522bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
4523bf3db8aaSMartin Matuska     "B", "Jail may mount/unmount jail-friendly file systems in general");
4524bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, devfs, CTLTYPE_INT | CTLFLAG_RW,
4525e7af90abSMartin Matuska     "B", "Jail may mount the devfs file system");
4526464aad14SJamie Gritton SYSCTL_JAIL_PARAM(_allow_mount, fdescfs, CTLTYPE_INT | CTLFLAG_RW,
4527464aad14SJamie Gritton     "B", "Jail may mount the fdescfs file system");
4528bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, nullfs, CTLTYPE_INT | CTLFLAG_RW,
4529e7af90abSMartin Matuska     "B", "Jail may mount the nullfs file system");
453041c0675eSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, procfs, CTLTYPE_INT | CTLFLAG_RW,
453141c0675eSMartin Matuska     "B", "Jail may mount the procfs file system");
4532f19e47d6SMarcelo Araujo SYSCTL_JAIL_PARAM(_allow_mount, linprocfs, CTLTYPE_INT | CTLFLAG_RW,
4533f19e47d6SMarcelo Araujo     "B", "Jail may mount the linprocfs file system");
4534f19e47d6SMarcelo Araujo SYSCTL_JAIL_PARAM(_allow_mount, linsysfs, CTLTYPE_INT | CTLFLAG_RW,
4535f19e47d6SMarcelo Araujo     "B", "Jail may mount the linsysfs file system");
45362454886eSXin LI SYSCTL_JAIL_PARAM(_allow_mount, tmpfs, CTLTYPE_INT | CTLFLAG_RW,
45372454886eSXin LI     "B", "Jail may mount the tmpfs file system");
4538e7af90abSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, zfs, CTLTYPE_INT | CTLFLAG_RW,
4539e7af90abSMartin Matuska     "B", "Jail may mount the zfs file system");
4540bf3db8aaSMartin Matuska 
45414b5c9cf6SEdward Tomasz Napierala #ifdef RACCT
4542097055e2SEdward Tomasz Napierala void
4543097055e2SEdward Tomasz Napierala prison_racct_foreach(void (*callback)(struct racct *racct,
454415db3c07SEdward Tomasz Napierala     void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
454515db3c07SEdward Tomasz Napierala     void *arg2, void *arg3)
4546097055e2SEdward Tomasz Napierala {
4547a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4548097055e2SEdward Tomasz Napierala 
45494b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
45504b5c9cf6SEdward Tomasz Napierala 
4551097055e2SEdward Tomasz Napierala 	sx_slock(&allprison_lock);
455215db3c07SEdward Tomasz Napierala 	if (pre != NULL)
455315db3c07SEdward Tomasz Napierala 		(pre)();
4554a7ad07bfSEdward Tomasz Napierala 	LIST_FOREACH(prr, &allprison_racct, prr_next)
4555a7ad07bfSEdward Tomasz Napierala 		(callback)(prr->prr_racct, arg2, arg3);
455615db3c07SEdward Tomasz Napierala 	if (post != NULL)
455715db3c07SEdward Tomasz Napierala 		(post)();
4558097055e2SEdward Tomasz Napierala 	sx_sunlock(&allprison_lock);
4559097055e2SEdward Tomasz Napierala }
45600304c731SJamie Gritton 
4561a7ad07bfSEdward Tomasz Napierala static struct prison_racct *
4562a7ad07bfSEdward Tomasz Napierala prison_racct_find_locked(const char *name)
4563a7ad07bfSEdward Tomasz Napierala {
4564a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4565a7ad07bfSEdward Tomasz Napierala 
45664b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4567a7ad07bfSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4568a7ad07bfSEdward Tomasz Napierala 
4569a7ad07bfSEdward Tomasz Napierala 	if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4570a7ad07bfSEdward Tomasz Napierala 		return (NULL);
4571a7ad07bfSEdward Tomasz Napierala 
4572a7ad07bfSEdward Tomasz Napierala 	LIST_FOREACH(prr, &allprison_racct, prr_next) {
4573a7ad07bfSEdward Tomasz Napierala 		if (strcmp(name, prr->prr_name) != 0)
4574a7ad07bfSEdward Tomasz Napierala 			continue;
4575a7ad07bfSEdward Tomasz Napierala 
4576a7ad07bfSEdward Tomasz Napierala 		/* Found prison_racct with a matching name? */
4577a7ad07bfSEdward Tomasz Napierala 		prison_racct_hold(prr);
4578a7ad07bfSEdward Tomasz Napierala 		return (prr);
4579a7ad07bfSEdward Tomasz Napierala 	}
4580a7ad07bfSEdward Tomasz Napierala 
4581a7ad07bfSEdward Tomasz Napierala 	/* Add new prison_racct. */
4582a7ad07bfSEdward Tomasz Napierala 	prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4583a7ad07bfSEdward Tomasz Napierala 	racct_create(&prr->prr_racct);
4584a7ad07bfSEdward Tomasz Napierala 
4585a7ad07bfSEdward Tomasz Napierala 	strcpy(prr->prr_name, name);
4586a7ad07bfSEdward Tomasz Napierala 	refcount_init(&prr->prr_refcount, 1);
4587a7ad07bfSEdward Tomasz Napierala 	LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4588a7ad07bfSEdward Tomasz Napierala 
4589a7ad07bfSEdward Tomasz Napierala 	return (prr);
4590a7ad07bfSEdward Tomasz Napierala }
4591a7ad07bfSEdward Tomasz Napierala 
4592a7ad07bfSEdward Tomasz Napierala struct prison_racct *
4593a7ad07bfSEdward Tomasz Napierala prison_racct_find(const char *name)
4594a7ad07bfSEdward Tomasz Napierala {
4595a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4596a7ad07bfSEdward Tomasz Napierala 
45974b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
45984b5c9cf6SEdward Tomasz Napierala 
4599a7ad07bfSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4600a7ad07bfSEdward Tomasz Napierala 	prr = prison_racct_find_locked(name);
4601a7ad07bfSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4602a7ad07bfSEdward Tomasz Napierala 	return (prr);
4603a7ad07bfSEdward Tomasz Napierala }
4604a7ad07bfSEdward Tomasz Napierala 
4605a7ad07bfSEdward Tomasz Napierala void
4606a7ad07bfSEdward Tomasz Napierala prison_racct_hold(struct prison_racct *prr)
4607a7ad07bfSEdward Tomasz Napierala {
4608a7ad07bfSEdward Tomasz Napierala 
46094b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
46104b5c9cf6SEdward Tomasz Napierala 
4611a7ad07bfSEdward Tomasz Napierala 	refcount_acquire(&prr->prr_refcount);
4612a7ad07bfSEdward Tomasz Napierala }
4613a7ad07bfSEdward Tomasz Napierala 
4614c34bbd2aSEdward Tomasz Napierala static void
4615c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(struct prison_racct *prr)
4616c34bbd2aSEdward Tomasz Napierala {
4617c34bbd2aSEdward Tomasz Napierala 
46184b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4619c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4620c34bbd2aSEdward Tomasz Napierala 
4621c34bbd2aSEdward Tomasz Napierala 	if (refcount_release(&prr->prr_refcount)) {
4622c34bbd2aSEdward Tomasz Napierala 		racct_destroy(&prr->prr_racct);
4623c34bbd2aSEdward Tomasz Napierala 		LIST_REMOVE(prr, prr_next);
4624c34bbd2aSEdward Tomasz Napierala 		free(prr, M_PRISON_RACCT);
4625c34bbd2aSEdward Tomasz Napierala 	}
4626c34bbd2aSEdward Tomasz Napierala }
4627c34bbd2aSEdward Tomasz Napierala 
4628a7ad07bfSEdward Tomasz Napierala void
4629a7ad07bfSEdward Tomasz Napierala prison_racct_free(struct prison_racct *prr)
4630a7ad07bfSEdward Tomasz Napierala {
4631a7ad07bfSEdward Tomasz Napierala 	int old;
4632a7ad07bfSEdward Tomasz Napierala 
46334b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4634c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_UNLOCKED);
4635c34bbd2aSEdward Tomasz Napierala 
4636a7ad07bfSEdward Tomasz Napierala 	old = prr->prr_refcount;
4637a7ad07bfSEdward Tomasz Napierala 	if (old > 1 && atomic_cmpset_int(&prr->prr_refcount, old, old - 1))
4638a7ad07bfSEdward Tomasz Napierala 		return;
4639a7ad07bfSEdward Tomasz Napierala 
4640a7ad07bfSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4641c34bbd2aSEdward Tomasz Napierala 	prison_racct_free_locked(prr);
4642a7ad07bfSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4643a7ad07bfSEdward Tomasz Napierala }
4644a7ad07bfSEdward Tomasz Napierala 
4645a7ad07bfSEdward Tomasz Napierala static void
4646a7ad07bfSEdward Tomasz Napierala prison_racct_attach(struct prison *pr)
4647a7ad07bfSEdward Tomasz Napierala {
4648a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4649a7ad07bfSEdward Tomasz Napierala 
46504b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4651c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4652c34bbd2aSEdward Tomasz Napierala 
4653a7ad07bfSEdward Tomasz Napierala 	prr = prison_racct_find_locked(pr->pr_name);
4654a7ad07bfSEdward Tomasz Napierala 	KASSERT(prr != NULL, ("cannot find prison_racct"));
4655a7ad07bfSEdward Tomasz Napierala 
4656a7ad07bfSEdward Tomasz Napierala 	pr->pr_prison_racct = prr;
4657a7ad07bfSEdward Tomasz Napierala }
4658a7ad07bfSEdward Tomasz Napierala 
4659c34bbd2aSEdward Tomasz Napierala /*
4660c34bbd2aSEdward Tomasz Napierala  * Handle jail renaming.  From the racct point of view, renaming means
4661c34bbd2aSEdward Tomasz Napierala  * moving from one prison_racct to another.
4662c34bbd2aSEdward Tomasz Napierala  */
4663c34bbd2aSEdward Tomasz Napierala static void
4664c34bbd2aSEdward Tomasz Napierala prison_racct_modify(struct prison *pr)
4665c34bbd2aSEdward Tomasz Napierala {
4666c34bbd2aSEdward Tomasz Napierala 	struct proc *p;
4667c34bbd2aSEdward Tomasz Napierala 	struct ucred *cred;
4668c34bbd2aSEdward Tomasz Napierala 	struct prison_racct *oldprr;
4669c34bbd2aSEdward Tomasz Napierala 
46704b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
46714b5c9cf6SEdward Tomasz Napierala 
4672c34bbd2aSEdward Tomasz Napierala 	sx_slock(&allproc_lock);
4673c34bbd2aSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4674c34bbd2aSEdward Tomasz Napierala 
4675e30345e7SEdward Tomasz Napierala 	if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
4676e30345e7SEdward Tomasz Napierala 		sx_xunlock(&allprison_lock);
4677e30345e7SEdward Tomasz Napierala 		sx_sunlock(&allproc_lock);
4678c34bbd2aSEdward Tomasz Napierala 		return;
4679e30345e7SEdward Tomasz Napierala 	}
4680c34bbd2aSEdward Tomasz Napierala 
4681c34bbd2aSEdward Tomasz Napierala 	oldprr = pr->pr_prison_racct;
4682c34bbd2aSEdward Tomasz Napierala 	pr->pr_prison_racct = NULL;
4683c34bbd2aSEdward Tomasz Napierala 
4684c34bbd2aSEdward Tomasz Napierala 	prison_racct_attach(pr);
4685c34bbd2aSEdward Tomasz Napierala 
4686c34bbd2aSEdward Tomasz Napierala 	/*
4687c34bbd2aSEdward Tomasz Napierala 	 * Move resource utilisation records.
4688c34bbd2aSEdward Tomasz Napierala 	 */
4689c34bbd2aSEdward Tomasz Napierala 	racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
4690c34bbd2aSEdward Tomasz Napierala 
4691c34bbd2aSEdward Tomasz Napierala 	/*
4692c34bbd2aSEdward Tomasz Napierala 	 * Force rctl to reattach rules to processes.
4693c34bbd2aSEdward Tomasz Napierala 	 */
4694c34bbd2aSEdward Tomasz Napierala 	FOREACH_PROC_IN_SYSTEM(p) {
4695c34bbd2aSEdward Tomasz Napierala 		PROC_LOCK(p);
4696c34bbd2aSEdward Tomasz Napierala 		cred = crhold(p->p_ucred);
4697c34bbd2aSEdward Tomasz Napierala 		PROC_UNLOCK(p);
4698c34bbd2aSEdward Tomasz Napierala 		racct_proc_ucred_changed(p, cred, cred);
4699c34bbd2aSEdward Tomasz Napierala 		crfree(cred);
4700c34bbd2aSEdward Tomasz Napierala 	}
4701c34bbd2aSEdward Tomasz Napierala 
4702c34bbd2aSEdward Tomasz Napierala 	sx_sunlock(&allproc_lock);
4703c34bbd2aSEdward Tomasz Napierala 	prison_racct_free_locked(oldprr);
4704c34bbd2aSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4705c34bbd2aSEdward Tomasz Napierala }
4706c34bbd2aSEdward Tomasz Napierala 
4707a7ad07bfSEdward Tomasz Napierala static void
4708a7ad07bfSEdward Tomasz Napierala prison_racct_detach(struct prison *pr)
4709a7ad07bfSEdward Tomasz Napierala {
4710c34bbd2aSEdward Tomasz Napierala 
47114b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4712c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_UNLOCKED);
4713c34bbd2aSEdward Tomasz Napierala 
4714af3c786cSMateusz Guzik 	if (pr->pr_prison_racct == NULL)
4715af3c786cSMateusz Guzik 		return;
4716a7ad07bfSEdward Tomasz Napierala 	prison_racct_free(pr->pr_prison_racct);
4717a7ad07bfSEdward Tomasz Napierala 	pr->pr_prison_racct = NULL;
4718a7ad07bfSEdward Tomasz Napierala }
4719a7ad07bfSEdward Tomasz Napierala #endif /* RACCT */
4720a7ad07bfSEdward Tomasz Napierala 
4721413628a7SBjoern A. Zeeb #ifdef DDB
4722b38ff370SJamie Gritton 
4723b38ff370SJamie Gritton static void
4724b38ff370SJamie Gritton db_show_prison(struct prison *pr)
4725413628a7SBjoern A. Zeeb {
47260304c731SJamie Gritton 	int fi;
4727b38ff370SJamie Gritton #if defined(INET) || defined(INET6)
4728b38ff370SJamie Gritton 	int ii;
4729413628a7SBjoern A. Zeeb #endif
47307cbf7213SJamie Gritton 	unsigned jsf;
4731413628a7SBjoern A. Zeeb #ifdef INET6
4732413628a7SBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
4733413628a7SBjoern A. Zeeb #endif
4734413628a7SBjoern A. Zeeb 
4735b38ff370SJamie Gritton 	db_printf("prison %p:\n", pr);
4736b38ff370SJamie Gritton 	db_printf(" jid             = %d\n", pr->pr_id);
4737b38ff370SJamie Gritton 	db_printf(" name            = %s\n", pr->pr_name);
47380304c731SJamie Gritton 	db_printf(" parent          = %p\n", pr->pr_parent);
4739b38ff370SJamie Gritton 	db_printf(" ref             = %d\n", pr->pr_ref);
4740b38ff370SJamie Gritton 	db_printf(" uref            = %d\n", pr->pr_uref);
4741b38ff370SJamie Gritton 	db_printf(" path            = %s\n", pr->pr_path);
4742b38ff370SJamie Gritton 	db_printf(" cpuset          = %d\n", pr->pr_cpuset
4743b38ff370SJamie Gritton 	    ? pr->pr_cpuset->cs_id : -1);
4744679e1390SJamie Gritton #ifdef VIMAGE
4745679e1390SJamie Gritton 	db_printf(" vnet            = %p\n", pr->pr_vnet);
4746679e1390SJamie Gritton #endif
4747b38ff370SJamie Gritton 	db_printf(" root            = %p\n", pr->pr_root);
4748b38ff370SJamie Gritton 	db_printf(" securelevel     = %d\n", pr->pr_securelevel);
47490cc207a6SMartin Matuska 	db_printf(" devfs_rsnum     = %d\n", pr->pr_devfs_rsnum);
4750fe0518e9SBjoern A. Zeeb 	db_printf(" children.max    = %d\n", pr->pr_childmax);
4751fe0518e9SBjoern A. Zeeb 	db_printf(" children.cur    = %d\n", pr->pr_childcount);
47520304c731SJamie Gritton 	db_printf(" child           = %p\n", LIST_FIRST(&pr->pr_children));
47530304c731SJamie Gritton 	db_printf(" sibling         = %p\n", LIST_NEXT(pr, pr_sibling));
4754fe0518e9SBjoern A. Zeeb 	db_printf(" flags           = 0x%x", pr->pr_flags);
475502abd400SPedro F. Giffuni 	for (fi = 0; fi < nitems(pr_flag_names); fi++)
47560304c731SJamie Gritton 		if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
47570304c731SJamie Gritton 			db_printf(" %s", pr_flag_names[fi]);
475802abd400SPedro F. Giffuni 	for (fi = 0; fi < nitems(pr_flag_jailsys); fi++) {
47597cbf7213SJamie Gritton 		jsf = pr->pr_flags &
47607cbf7213SJamie Gritton 		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
47617cbf7213SJamie Gritton 		db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
47627cbf7213SJamie Gritton 		    pr_flag_jailsys[fi].disable &&
47637cbf7213SJamie Gritton 		      (jsf == pr_flag_jailsys[fi].disable) ? "disable"
47647cbf7213SJamie Gritton 		    : (jsf == pr_flag_jailsys[fi].new) ? "new"
47657cbf7213SJamie Gritton 		    : "inherit");
47667cbf7213SJamie Gritton 	}
4767fe0518e9SBjoern A. Zeeb 	db_printf(" allow           = 0x%x", pr->pr_allow);
476802abd400SPedro F. Giffuni 	for (fi = 0; fi < nitems(pr_allow_names); fi++)
47690304c731SJamie Gritton 		if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
47700304c731SJamie Gritton 			db_printf(" %s", pr_allow_names[fi]);
4771b38ff370SJamie Gritton 	db_printf("\n");
47720304c731SJamie Gritton 	db_printf(" enforce_statfs  = %d\n", pr->pr_enforce_statfs);
4773c1f19219SJamie Gritton 	db_printf(" host.hostname   = %s\n", pr->pr_hostname);
4774c1f19219SJamie Gritton 	db_printf(" host.domainname = %s\n", pr->pr_domainname);
4775c1f19219SJamie Gritton 	db_printf(" host.hostuuid   = %s\n", pr->pr_hostuuid);
477676ca6f88SJamie Gritton 	db_printf(" host.hostid     = %lu\n", pr->pr_hostid);
4777413628a7SBjoern A. Zeeb #ifdef INET
4778b38ff370SJamie Gritton 	db_printf(" ip4s            = %d\n", pr->pr_ip4s);
4779b38ff370SJamie Gritton 	for (ii = 0; ii < pr->pr_ip4s; ii++)
4780b38ff370SJamie Gritton 		db_printf(" %s %s\n",
4781fe0518e9SBjoern A. Zeeb 		    ii == 0 ? "ip4.addr        =" : "                 ",
4782b38ff370SJamie Gritton 		    inet_ntoa(pr->pr_ip4[ii]));
4783413628a7SBjoern A. Zeeb #endif
4784413628a7SBjoern A. Zeeb #ifdef INET6
4785b38ff370SJamie Gritton 	db_printf(" ip6s            = %d\n", pr->pr_ip6s);
4786b38ff370SJamie Gritton 	for (ii = 0; ii < pr->pr_ip6s; ii++)
4787b38ff370SJamie Gritton 		db_printf(" %s %s\n",
4788fe0518e9SBjoern A. Zeeb 		    ii == 0 ? "ip6.addr        =" : "                 ",
4789b38ff370SJamie Gritton 		    ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4790b38ff370SJamie Gritton #endif
4791b38ff370SJamie Gritton }
4792b38ff370SJamie Gritton 
4793b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command)
4794b38ff370SJamie Gritton {
4795b38ff370SJamie Gritton 	struct prison *pr;
4796b38ff370SJamie Gritton 
4797b38ff370SJamie Gritton 	if (!have_addr) {
47980304c731SJamie Gritton 		/*
47990304c731SJamie Gritton 		 * Show all prisons in the list, and prison0 which is not
48000304c731SJamie Gritton 		 * listed.
48010304c731SJamie Gritton 		 */
48020304c731SJamie Gritton 		db_show_prison(&prison0);
48030304c731SJamie Gritton 		if (!db_pager_quit) {
4804b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list) {
4805b38ff370SJamie Gritton 				db_show_prison(pr);
4806413628a7SBjoern A. Zeeb 				if (db_pager_quit)
4807413628a7SBjoern A. Zeeb 					break;
4808413628a7SBjoern A. Zeeb 			}
48090304c731SJamie Gritton 		}
4810b38ff370SJamie Gritton 		return;
4811413628a7SBjoern A. Zeeb 	}
4812b38ff370SJamie Gritton 
48130304c731SJamie Gritton 	if (addr == 0)
48140304c731SJamie Gritton 		pr = &prison0;
48150304c731SJamie Gritton 	else {
4816b38ff370SJamie Gritton 		/* Look for a prison with the ID and with references. */
4817b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list)
4818b38ff370SJamie Gritton 			if (pr->pr_id == addr && pr->pr_ref > 0)
4819b38ff370SJamie Gritton 				break;
4820b38ff370SJamie Gritton 		if (pr == NULL)
4821b38ff370SJamie Gritton 			/* Look again, without requiring a reference. */
4822b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list)
4823b38ff370SJamie Gritton 				if (pr->pr_id == addr)
4824b38ff370SJamie Gritton 					break;
4825b38ff370SJamie Gritton 		if (pr == NULL)
4826b38ff370SJamie Gritton 			/* Assume address points to a valid prison. */
4827b38ff370SJamie Gritton 			pr = (struct prison *)addr;
48280304c731SJamie Gritton 	}
4829b38ff370SJamie Gritton 	db_show_prison(pr);
4830b38ff370SJamie Gritton }
4831b38ff370SJamie Gritton 
4832413628a7SBjoern A. Zeeb #endif /* DDB */
4833