kern_jail.c revision 232186
1139804Simp/*-
2185435Sbz * Copyright (c) 1999 Poul-Henning Kamp.
3185435Sbz * Copyright (c) 2008 Bjoern A. Zeeb.
4191673Sjamie * Copyright (c) 2009 James Gritton.
5185435Sbz * All rights reserved.
6190466Sjamie *
7185404Sbz * Redistribution and use in source and binary forms, with or without
8185404Sbz * modification, are permitted provided that the following conditions
9185404Sbz * are met:
10185404Sbz * 1. Redistributions of source code must retain the above copyright
11185404Sbz *    notice, this list of conditions and the following disclaimer.
12185404Sbz * 2. Redistributions in binary form must reproduce the above copyright
13185404Sbz *    notice, this list of conditions and the following disclaimer in the
14185404Sbz *    documentation and/or other materials provided with the distribution.
15185404Sbz *
16185404Sbz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17185404Sbz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18185404Sbz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19185404Sbz * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20185404Sbz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21185404Sbz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22185404Sbz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23185404Sbz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24185404Sbz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25185404Sbz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26185404Sbz * SUCH DAMAGE.
2746197Sphk */
2846155Sphk
29116182Sobrien#include <sys/cdefs.h>
30116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/kern_jail.c 232186 2012-02-26 16:30:39Z mm $");
31116182Sobrien
32193066Sjamie#include "opt_compat.h"
33185435Sbz#include "opt_ddb.h"
34185435Sbz#include "opt_inet.h"
35185435Sbz#include "opt_inet6.h"
36131177Spjd
3746155Sphk#include <sys/param.h>
3846155Sphk#include <sys/types.h>
3946155Sphk#include <sys/kernel.h>
4046155Sphk#include <sys/systm.h>
4146155Sphk#include <sys/errno.h>
4246155Sphk#include <sys/sysproto.h>
4346155Sphk#include <sys/malloc.h>
44192895Sjamie#include <sys/osd.h>
45164032Srwatson#include <sys/priv.h>
4646155Sphk#include <sys/proc.h>
47124882Srwatson#include <sys/taskqueue.h>
48177785Skib#include <sys/fcntl.h>
4946155Sphk#include <sys/jail.h>
5087275Srwatson#include <sys/lock.h>
5187275Srwatson#include <sys/mutex.h>
52220137Strasz#include <sys/racct.h>
53221362Strasz#include <sys/refcount.h>
54168401Spjd#include <sys/sx.h>
55193066Sjamie#include <sys/sysent.h>
56113275Smike#include <sys/namei.h>
57147185Spjd#include <sys/mount.h>
58113275Smike#include <sys/queue.h>
5946155Sphk#include <sys/socket.h>
60113275Smike#include <sys/syscallsubr.h>
6157163Srwatson#include <sys/sysctl.h>
62113275Smike#include <sys/vnode.h>
63196019Srwatson
6446155Sphk#include <net/if.h>
65196019Srwatson#include <net/vnet.h>
66196019Srwatson
6746155Sphk#include <netinet/in.h>
68196019Srwatson
69185435Sbz#ifdef DDB
70185435Sbz#include <ddb/ddb.h>
71185435Sbz#ifdef INET6
72185435Sbz#include <netinet6/in6_var.h>
73185435Sbz#endif /* INET6 */
74185435Sbz#endif /* DDB */
7546155Sphk
76163606Srwatson#include <security/mac/mac_framework.h>
77163606Srwatson
78195944Sjamie#define	DEFAULT_HOSTUUID	"00000000-0000-0000-0000-000000000000"
79195944Sjamie
8046155SphkMALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
81227293Sedstatic MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
8246155Sphk
83202468Sbz/* Keep struct prison prison0 and some code in kern_jail_set() readable. */
84202468Sbz#ifdef INET
85202468Sbz#ifdef INET6
86202468Sbz#define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
87202468Sbz#else
88202468Sbz#define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL
89202468Sbz#endif
90202468Sbz#else /* !INET */
91202468Sbz#ifdef INET6
92202468Sbz#define	_PR_IP_SADDRSEL	PR_IP6_SADDRSEL
93202468Sbz#else
94202468Sbz#define	_PR_IP_SADDRSEL	0
95202468Sbz#endif
96202468Sbz#endif
97202468Sbz
98192895Sjamie/* prison0 describes what is "real" about the system. */
99192895Sjamiestruct prison prison0 = {
100192895Sjamie	.pr_id		= 0,
101192895Sjamie	.pr_name	= "0",
102192895Sjamie	.pr_ref		= 1,
103192895Sjamie	.pr_uref	= 1,
104192895Sjamie	.pr_path	= "/",
105192895Sjamie	.pr_securelevel	= -1,
106231267Smm	.pr_devfs_rsnum = 0,
107194762Sjamie	.pr_childmax	= JAIL_MAX,
108195944Sjamie	.pr_hostuuid	= DEFAULT_HOSTUUID,
109201145Santoine	.pr_children	= LIST_HEAD_INITIALIZER(prison0.pr_children),
110196176Sbz#ifdef VIMAGE
111202468Sbz	.pr_flags	= PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
112196176Sbz#else
113202468Sbz	.pr_flags	= PR_HOST|_PR_IP_SADDRSEL,
114196176Sbz#endif
115192895Sjamie	.pr_allow	= PR_ALLOW_ALL,
116192895Sjamie};
117192895SjamieMTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
11857163Srwatson
119221362Strasz/* allprison, allprison_racct and lastprid are protected by allprison_lock. */
120168401Spjdstruct	sx allprison_lock;
121191673SjamieSX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
122191673Sjamiestruct	prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
123221362StraszLIST_HEAD(, prison_racct) allprison_racct;
124179881Sdelphijint	lastprid = 0;
125113275Smike
126191673Sjamiestatic int do_jail_attach(struct thread *td, struct prison *pr);
127190466Sjamiestatic void prison_complete(void *context, int pending);
128191673Sjamiestatic void prison_deref(struct prison *pr, int flags);
129192895Sjamiestatic char *prison_path(struct prison *pr1, struct prison *pr2);
130192895Sjamiestatic void prison_remove_one(struct prison *pr);
131221362Strasz#ifdef RACCT
132221362Straszstatic void prison_racct_attach(struct prison *pr);
133221362Straszstatic void prison_racct_detach(struct prison *pr);
134221362Strasz#endif
135185435Sbz#ifdef INET
136190466Sjamiestatic int _prison_check_ip4(struct prison *pr, struct in_addr *ia);
137192895Sjamiestatic int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4);
138185435Sbz#endif
139185435Sbz#ifdef INET6
140190466Sjamiestatic int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6);
141192895Sjamiestatic int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6);
142185435Sbz#endif
143113275Smike
144191673Sjamie/* Flags for prison_deref */
145191673Sjamie#define	PD_DEREF	0x01
146191673Sjamie#define	PD_DEUREF	0x02
147191673Sjamie#define	PD_LOCKED	0x04
148191673Sjamie#define	PD_LIST_SLOCKED	0x08
149191673Sjamie#define	PD_LIST_XLOCKED	0x10
150113275Smike
151192895Sjamie/*
152216861Sbz * Parameter names corresponding to PR_* flag values.  Size values are for kvm
153216861Sbz * as we cannot figure out the size of a sparse array, or an array without a
154216861Sbz * terminating entry.
155192895Sjamie */
156192895Sjamiestatic char *pr_flag_names[] = {
157192895Sjamie	[0] = "persist",
158202468Sbz#ifdef INET
159202468Sbz	[7] = "ip4.saddrsel",
160202468Sbz#endif
161202468Sbz#ifdef INET6
162202468Sbz	[8] = "ip6.saddrsel",
163202468Sbz#endif
164192895Sjamie};
165216861Sbzconst size_t pr_flag_names_size = sizeof(pr_flag_names);
166192895Sjamie
167192895Sjamiestatic char *pr_flag_nonames[] = {
168192895Sjamie	[0] = "nopersist",
169202468Sbz#ifdef INET
170202468Sbz	[7] = "ip4.nosaddrsel",
171202468Sbz#endif
172202468Sbz#ifdef INET6
173202468Sbz	[8] = "ip6.nosaddrsel",
174202468Sbz#endif
175195870Sjamie};
176216861Sbzconst size_t pr_flag_nonames_size = sizeof(pr_flag_nonames);
177195870Sjamie
178195870Sjamiestruct jailsys_flags {
179195870Sjamie	const char	*name;
180195870Sjamie	unsigned	 disable;
181195870Sjamie	unsigned	 new;
182195870Sjamie} pr_flag_jailsys[] = {
183195870Sjamie	{ "host", 0, PR_HOST },
184195870Sjamie#ifdef VIMAGE
185195870Sjamie	{ "vnet", 0, PR_VNET },
186195870Sjamie#endif
187192895Sjamie#ifdef INET
188195870Sjamie	{ "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER },
189192895Sjamie#endif
190192895Sjamie#ifdef INET6
191195870Sjamie	{ "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER },
192192895Sjamie#endif
193192895Sjamie};
194216861Sbzconst size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
195192895Sjamie
196192895Sjamiestatic char *pr_allow_names[] = {
197192895Sjamie	"allow.set_hostname",
198192895Sjamie	"allow.sysvipc",
199192895Sjamie	"allow.raw_sockets",
200192895Sjamie	"allow.chflags",
201192895Sjamie	"allow.mount",
202192895Sjamie	"allow.quotas",
203192895Sjamie	"allow.socket_af",
204232059Smm	"allow.mount.devfs",
205232059Smm	"allow.mount.nullfs",
206232186Smm	"allow.mount.zfs",
207192895Sjamie};
208216861Sbzconst size_t pr_allow_names_size = sizeof(pr_allow_names);
209192895Sjamie
210192895Sjamiestatic char *pr_allow_nonames[] = {
211192895Sjamie	"allow.noset_hostname",
212192895Sjamie	"allow.nosysvipc",
213192895Sjamie	"allow.noraw_sockets",
214192895Sjamie	"allow.nochflags",
215192895Sjamie	"allow.nomount",
216192895Sjamie	"allow.noquotas",
217192895Sjamie	"allow.nosocket_af",
218232059Smm	"allow.mount.nodevfs",
219232059Smm	"allow.mount.nonullfs",
220232186Smm	"allow.mount.nozfs",
221192895Sjamie};
222216861Sbzconst size_t pr_allow_nonames_size = sizeof(pr_allow_nonames);
223192895Sjamie
224196002Sjamie#define	JAIL_DEFAULT_ALLOW		PR_ALLOW_SET_HOSTNAME
225196002Sjamie#define	JAIL_DEFAULT_ENFORCE_STATFS	2
226232059Smm#define	JAIL_DEFAULT_DEVFS_RSNUM	0
227192895Sjamiestatic unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
228196002Sjamiestatic int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
229231267Smmstatic int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
230192895Sjamie#if defined(INET) || defined(INET6)
231193865Sjamiestatic unsigned jail_max_af_ips = 255;
232192895Sjamie#endif
233192895Sjamie
234192895Sjamie#ifdef INET
235185435Sbzstatic int
236185435Sbzqcmp_v4(const void *ip1, const void *ip2)
237185435Sbz{
238185435Sbz	in_addr_t iaa, iab;
239185435Sbz
240185435Sbz	/*
241185435Sbz	 * We need to compare in HBO here to get the list sorted as expected
242185435Sbz	 * by the result of the code.  Sorting NBO addresses gives you
243185435Sbz	 * interesting results.  If you do not understand, do not try.
244185435Sbz	 */
245185435Sbz	iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
246185435Sbz	iab = ntohl(((const struct in_addr *)ip2)->s_addr);
247185435Sbz
248185435Sbz	/*
249185435Sbz	 * Do not simply return the difference of the two numbers, the int is
250185435Sbz	 * not wide enough.
251185435Sbz	 */
252185435Sbz	if (iaa > iab)
253185435Sbz		return (1);
254185435Sbz	else if (iaa < iab)
255185435Sbz		return (-1);
256185435Sbz	else
257185435Sbz		return (0);
258185435Sbz}
259185435Sbz#endif
260185435Sbz
261185435Sbz#ifdef INET6
262185435Sbzstatic int
263185435Sbzqcmp_v6(const void *ip1, const void *ip2)
264185435Sbz{
265185435Sbz	const struct in6_addr *ia6a, *ia6b;
266185435Sbz	int i, rc;
267185435Sbz
268185435Sbz	ia6a = (const struct in6_addr *)ip1;
269185435Sbz	ia6b = (const struct in6_addr *)ip2;
270185435Sbz
271185435Sbz	rc = 0;
272190466Sjamie	for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
273185435Sbz		if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
274185435Sbz			rc = 1;
275185435Sbz		else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
276185435Sbz			rc = -1;
277185435Sbz	}
278185435Sbz	return (rc);
279185435Sbz}
280185435Sbz#endif
281185435Sbz
282191673Sjamie/*
283191673Sjamie * struct jail_args {
284191673Sjamie *	struct jail *jail;
285191673Sjamie * };
286191673Sjamie */
287191673Sjamieint
288225617Skmacysys_jail(struct thread *td, struct jail_args *uap)
289185435Sbz{
290191673Sjamie	uint32_t version;
291191673Sjamie	int error;
292192895Sjamie	struct jail j;
293185435Sbz
294191673Sjamie	error = copyin(uap->jail, &version, sizeof(uint32_t));
295191673Sjamie	if (error)
296191673Sjamie		return (error);
297185435Sbz
298191673Sjamie	switch (version) {
299191673Sjamie	case 0:
300191673Sjamie	{
301191673Sjamie		struct jail_v0 j0;
302185435Sbz
303192895Sjamie		/* FreeBSD single IPv4 jails. */
304192895Sjamie		bzero(&j, sizeof(struct jail));
305191673Sjamie		error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
306191673Sjamie		if (error)
307191673Sjamie			return (error);
308192895Sjamie		j.version = j0.version;
309192895Sjamie		j.path = j0.path;
310192895Sjamie		j.hostname = j0.hostname;
311192895Sjamie		j.ip4s = j0.ip_number;
312191673Sjamie		break;
313191673Sjamie	}
314191673Sjamie
315191673Sjamie	case 1:
316185435Sbz		/*
317191673Sjamie		 * Version 1 was used by multi-IPv4 jail implementations
318191673Sjamie		 * that never made it into the official kernel.
319185435Sbz		 */
320191673Sjamie		return (EINVAL);
321185435Sbz
322191673Sjamie	case 2:	/* JAIL_API_VERSION */
323191673Sjamie		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
324191673Sjamie		error = copyin(uap->jail, &j, sizeof(struct jail));
325191673Sjamie		if (error)
326191673Sjamie			return (error);
327192895Sjamie		break;
328192895Sjamie
329192895Sjamie	default:
330192895Sjamie		/* Sci-Fi jails are not supported, sorry. */
331192895Sjamie		return (EINVAL);
332192895Sjamie	}
333192895Sjamie	return (kern_jail(td, &j));
334192895Sjamie}
335192895Sjamie
336192895Sjamieint
337192895Sjamiekern_jail(struct thread *td, struct jail *j)
338192895Sjamie{
339193865Sjamie	struct iovec optiov[2 * (4
340193865Sjamie			    + sizeof(pr_allow_names) / sizeof(pr_allow_names[0])
341193865Sjamie#ifdef INET
342193865Sjamie			    + 1
343193865Sjamie#endif
344193865Sjamie#ifdef INET6
345193865Sjamie			    + 1
346193865Sjamie#endif
347193865Sjamie			    )];
348192895Sjamie	struct uio opt;
349192895Sjamie	char *u_path, *u_hostname, *u_name;
350185435Sbz#ifdef INET
351193865Sjamie	uint32_t ip4s;
352192895Sjamie	struct in_addr *u_ip4;
353192895Sjamie#endif
354192895Sjamie#ifdef INET6
355192895Sjamie	struct in6_addr *u_ip6;
356192895Sjamie#endif
357192895Sjamie	size_t tmplen;
358192895Sjamie	int error, enforce_statfs, fi;
359192895Sjamie
360192895Sjamie	bzero(&optiov, sizeof(optiov));
361192895Sjamie	opt.uio_iov = optiov;
362192895Sjamie	opt.uio_iovcnt = 0;
363192895Sjamie	opt.uio_offset = -1;
364192895Sjamie	opt.uio_resid = -1;
365192895Sjamie	opt.uio_segflg = UIO_SYSSPACE;
366192895Sjamie	opt.uio_rw = UIO_READ;
367192895Sjamie	opt.uio_td = td;
368192895Sjamie
369192895Sjamie	/* Set permissions for top-level jails from sysctls. */
370192895Sjamie	if (!jailed(td->td_ucred)) {
371192895Sjamie		for (fi = 0; fi < sizeof(pr_allow_names) /
372192895Sjamie		     sizeof(pr_allow_names[0]); fi++) {
373192895Sjamie			optiov[opt.uio_iovcnt].iov_base =
374192895Sjamie			    (jail_default_allow & (1 << fi))
375192895Sjamie			    ? pr_allow_names[fi] : pr_allow_nonames[fi];
376192895Sjamie			optiov[opt.uio_iovcnt].iov_len =
377192895Sjamie			    strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
378192895Sjamie			opt.uio_iovcnt += 2;
379192895Sjamie		}
380192895Sjamie		optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
381192895Sjamie		optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
382192895Sjamie		opt.uio_iovcnt++;
383192895Sjamie		enforce_statfs = jail_default_enforce_statfs;
384192895Sjamie		optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
385192895Sjamie		optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
386192895Sjamie		opt.uio_iovcnt++;
387192895Sjamie	}
388192895Sjamie
389192895Sjamie	tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
390192895Sjamie#ifdef INET
391192895Sjamie	ip4s = (j->version == 0) ? 1 : j->ip4s;
392192895Sjamie	if (ip4s > jail_max_af_ips)
393192895Sjamie		return (EINVAL);
394192895Sjamie	tmplen += ip4s * sizeof(struct in_addr);
395191673Sjamie#else
396192895Sjamie	if (j->ip4s > 0)
397192895Sjamie		return (EINVAL);
398191673Sjamie#endif
399191673Sjamie#ifdef INET6
400192895Sjamie	if (j->ip6s > jail_max_af_ips)
401192895Sjamie		return (EINVAL);
402192895Sjamie	tmplen += j->ip6s * sizeof(struct in6_addr);
403191673Sjamie#else
404192895Sjamie	if (j->ip6s > 0)
405192895Sjamie		return (EINVAL);
406191673Sjamie#endif
407192895Sjamie	u_path = malloc(tmplen, M_TEMP, M_WAITOK);
408192895Sjamie	u_hostname = u_path + MAXPATHLEN;
409192895Sjamie	u_name = u_hostname + MAXHOSTNAMELEN;
410191673Sjamie#ifdef INET
411192895Sjamie	u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
412191673Sjamie#endif
413191673Sjamie#ifdef INET6
414191673Sjamie#ifdef INET
415192895Sjamie	u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
416191673Sjamie#else
417192895Sjamie	u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
418191673Sjamie#endif
419191673Sjamie#endif
420192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "path";
421192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("path");
422192895Sjamie	opt.uio_iovcnt++;
423192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_path;
424192895Sjamie	error = copyinstr(j->path, u_path, MAXPATHLEN,
425192895Sjamie	    &optiov[opt.uio_iovcnt].iov_len);
426192895Sjamie	if (error) {
427192895Sjamie		free(u_path, M_TEMP);
428192895Sjamie		return (error);
429192895Sjamie	}
430192895Sjamie	opt.uio_iovcnt++;
431192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "host.hostname";
432192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
433192895Sjamie	opt.uio_iovcnt++;
434192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_hostname;
435192895Sjamie	error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
436192895Sjamie	    &optiov[opt.uio_iovcnt].iov_len);
437192895Sjamie	if (error) {
438192895Sjamie		free(u_path, M_TEMP);
439192895Sjamie		return (error);
440192895Sjamie	}
441192895Sjamie	opt.uio_iovcnt++;
442192895Sjamie	if (j->jailname != NULL) {
443192895Sjamie		optiov[opt.uio_iovcnt].iov_base = "name";
444192895Sjamie		optiov[opt.uio_iovcnt].iov_len = sizeof("name");
445192895Sjamie		opt.uio_iovcnt++;
446192895Sjamie		optiov[opt.uio_iovcnt].iov_base = u_name;
447192895Sjamie		error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
448192895Sjamie		    &optiov[opt.uio_iovcnt].iov_len);
449191673Sjamie		if (error) {
450191673Sjamie			free(u_path, M_TEMP);
451191673Sjamie			return (error);
452191673Sjamie		}
453192895Sjamie		opt.uio_iovcnt++;
454192895Sjamie	}
455191673Sjamie#ifdef INET
456192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
457192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
458192895Sjamie	opt.uio_iovcnt++;
459192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_ip4;
460192895Sjamie	optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
461192895Sjamie	if (j->version == 0)
462192895Sjamie		u_ip4->s_addr = j->ip4s;
463192895Sjamie	else {
464192895Sjamie		error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
465191673Sjamie		if (error) {
466191673Sjamie			free(u_path, M_TEMP);
467191673Sjamie			return (error);
468191673Sjamie		}
469192895Sjamie	}
470192895Sjamie	opt.uio_iovcnt++;
471185435Sbz#endif
472185435Sbz#ifdef INET6
473192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
474192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
475192895Sjamie	opt.uio_iovcnt++;
476192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_ip6;
477192895Sjamie	optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
478192895Sjamie	error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
479192895Sjamie	if (error) {
480192895Sjamie		free(u_path, M_TEMP);
481192895Sjamie		return (error);
482192895Sjamie	}
483192895Sjamie	opt.uio_iovcnt++;
484185435Sbz#endif
485192895Sjamie	KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]),
486192895Sjamie	    ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
487191673Sjamie	error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
488191673Sjamie	free(u_path, M_TEMP);
489191673Sjamie	return (error);
490185435Sbz}
491185435Sbz
492192895Sjamie
493191673Sjamie/*
494191673Sjamie * struct jail_set_args {
495191673Sjamie *	struct iovec *iovp;
496191673Sjamie *	unsigned int iovcnt;
497191673Sjamie *	int flags;
498191673Sjamie * };
499191673Sjamie */
500191673Sjamieint
501225617Skmacysys_jail_set(struct thread *td, struct jail_set_args *uap)
502185435Sbz{
503191673Sjamie	struct uio *auio;
504191673Sjamie	int error;
505191673Sjamie
506191673Sjamie	/* Check that we have an even number of iovecs. */
507191673Sjamie	if (uap->iovcnt & 1)
508191673Sjamie		return (EINVAL);
509191673Sjamie
510191673Sjamie	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
511191673Sjamie	if (error)
512191673Sjamie		return (error);
513191673Sjamie	error = kern_jail_set(td, auio, uap->flags);
514191673Sjamie	free(auio, M_IOV);
515191673Sjamie	return (error);
516191673Sjamie}
517191673Sjamie
518191673Sjamieint
519191673Sjamiekern_jail_set(struct thread *td, struct uio *optuio, int flags)
520191673Sjamie{
521191673Sjamie	struct nameidata nd;
522185435Sbz#ifdef INET
523190466Sjamie	struct in_addr *ip4;
524185435Sbz#endif
525185435Sbz#ifdef INET6
526185435Sbz	struct in6_addr *ip6;
527185435Sbz#endif
528191673Sjamie	struct vfsopt *opt;
529191673Sjamie	struct vfsoptlist *opts;
530196135Sbz	struct prison *pr, *deadpr, *mypr, *ppr, *tpr;
531191673Sjamie	struct vnode *root;
532196835Sjamie	char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
533230407Smm	char *g_path;
534192895Sjamie#if defined(INET) || defined(INET6)
535196135Sbz	struct prison *tppr;
536191673Sjamie	void *op;
537192895Sjamie#endif
538193066Sjamie	unsigned long hid;
539192895Sjamie	size_t namelen, onamelen;
540192895Sjamie	int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos;
541231267Smm	int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
542195870Sjamie	int fi, jid, jsys, len, level;
543231267Smm	int childmax, rsnum, slevel, vfslocked;
544230129Smm	int fullpath_disabled;
545191673Sjamie#if defined(INET) || defined(INET6)
546192895Sjamie	int ii, ij;
547191673Sjamie#endif
548191673Sjamie#ifdef INET
549195974Sjamie	int ip4s, redo_ip4;
550191673Sjamie#endif
551191673Sjamie#ifdef INET6
552195974Sjamie	int ip6s, redo_ip6;
553191673Sjamie#endif
554224290Smckusick	uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
555224290Smckusick	unsigned tallow;
556191673Sjamie	char numbuf[12];
557185435Sbz
558191673Sjamie	error = priv_check(td, PRIV_JAIL_SET);
559191673Sjamie	if (!error && (flags & JAIL_ATTACH))
560191673Sjamie		error = priv_check(td, PRIV_JAIL_ATTACH);
561191673Sjamie	if (error)
562191673Sjamie		return (error);
563192895Sjamie	mypr = ppr = td->td_ucred->cr_prison;
564194762Sjamie	if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
565192895Sjamie		return (EPERM);
566191673Sjamie	if (flags & ~JAIL_SET_MASK)
567191673Sjamie		return (EINVAL);
568191673Sjamie
569185435Sbz	/*
570191673Sjamie	 * Check all the parameters before committing to anything.  Not all
571191673Sjamie	 * errors can be caught early, but we may as well try.  Also, this
572191673Sjamie	 * takes care of some expensive stuff (path lookup) before getting
573191673Sjamie	 * the allprison lock.
574185435Sbz	 *
575191673Sjamie	 * XXX Jails are not filesystems, and jail parameters are not mount
576191673Sjamie	 *     options.  But it makes more sense to re-use the vfsopt code
577191673Sjamie	 *     than duplicate it under a different name.
578185435Sbz	 */
579191673Sjamie	error = vfs_buildopts(optuio, &opts);
580191673Sjamie	if (error)
581191673Sjamie		return (error);
582185435Sbz#ifdef INET
583185435Sbz	ip4 = NULL;
584185435Sbz#endif
585185435Sbz#ifdef INET6
586185435Sbz	ip6 = NULL;
587185435Sbz#endif
588230407Smm	g_path = NULL;
589191673Sjamie
590191673Sjamie	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
591191673Sjamie	if (error == ENOENT)
592191673Sjamie		jid = 0;
593191673Sjamie	else if (error != 0)
594191673Sjamie		goto done_free;
595191673Sjamie
596191673Sjamie	error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
597191673Sjamie	if (error == ENOENT)
598191673Sjamie		gotslevel = 0;
599191673Sjamie	else if (error != 0)
600191673Sjamie		goto done_free;
601191673Sjamie	else
602191673Sjamie		gotslevel = 1;
603191673Sjamie
604194762Sjamie	error =
605194762Sjamie	    vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
606194762Sjamie	if (error == ENOENT)
607194762Sjamie		gotchildmax = 0;
608194762Sjamie	else if (error != 0)
609194762Sjamie		goto done_free;
610194762Sjamie	else
611194762Sjamie		gotchildmax = 1;
612194762Sjamie
613192895Sjamie	error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
614212436Sjamie	if (error == ENOENT)
615212436Sjamie		gotenforce = 0;
616212436Sjamie	else if (error != 0)
617192895Sjamie		goto done_free;
618212436Sjamie	else if (enforce < 0 || enforce > 2) {
619212436Sjamie		error = EINVAL;
620212436Sjamie		goto done_free;
621212436Sjamie	} else
622212436Sjamie		gotenforce = 1;
623192895Sjamie
624231267Smm	error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
625231267Smm	if (error == ENOENT)
626231267Smm		gotrsnum = 0;
627231267Smm	else if (error != 0)
628231267Smm		goto done_free;
629231267Smm	else
630231267Smm		gotrsnum = 1;
631231267Smm
632191673Sjamie	pr_flags = ch_flags = 0;
633192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
634192895Sjamie	    fi++) {
635192895Sjamie		if (pr_flag_names[fi] == NULL)
636192895Sjamie			continue;
637192895Sjamie		vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
638192895Sjamie		vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
639192895Sjamie	}
640191673Sjamie	ch_flags |= pr_flags;
641195870Sjamie	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
642195870Sjamie	    fi++) {
643195870Sjamie		error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys,
644195870Sjamie		    sizeof(jsys));
645195870Sjamie		if (error == ENOENT)
646195870Sjamie			continue;
647195870Sjamie		if (error != 0)
648195870Sjamie			goto done_free;
649195870Sjamie		switch (jsys) {
650195870Sjamie		case JAIL_SYS_DISABLE:
651195870Sjamie			if (!pr_flag_jailsys[fi].disable) {
652195870Sjamie				error = EINVAL;
653195870Sjamie				goto done_free;
654195870Sjamie			}
655195870Sjamie			pr_flags |= pr_flag_jailsys[fi].disable;
656195870Sjamie			break;
657195870Sjamie		case JAIL_SYS_NEW:
658195870Sjamie			pr_flags |= pr_flag_jailsys[fi].new;
659195870Sjamie			break;
660195870Sjamie		case JAIL_SYS_INHERIT:
661195870Sjamie			break;
662195870Sjamie		default:
663195870Sjamie			error = EINVAL;
664195870Sjamie			goto done_free;
665195870Sjamie		}
666195870Sjamie		ch_flags |=
667195870Sjamie		    pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable;
668195870Sjamie	}
669211085Sjamie	if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
670211085Sjamie	    && !(pr_flags & PR_PERSIST)) {
671211085Sjamie		error = EINVAL;
672211085Sjamie		vfs_opterror(opts, "new jail must persist or attach");
673211085Sjamie		goto done_errmsg;
674211085Sjamie	}
675194251Sjamie#ifdef VIMAGE
676194251Sjamie	if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
677194251Sjamie		error = EINVAL;
678194251Sjamie		vfs_opterror(opts, "vnet cannot be changed after creation");
679194251Sjamie		goto done_errmsg;
680194251Sjamie	}
681194251Sjamie#endif
682195974Sjamie#ifdef INET
683195974Sjamie	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
684195974Sjamie		error = EINVAL;
685195974Sjamie		vfs_opterror(opts, "ip4 cannot be changed after creation");
686195974Sjamie		goto done_errmsg;
687195974Sjamie	}
688195974Sjamie#endif
689195974Sjamie#ifdef INET6
690195974Sjamie	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
691195974Sjamie		error = EINVAL;
692195974Sjamie		vfs_opterror(opts, "ip6 cannot be changed after creation");
693195974Sjamie		goto done_errmsg;
694195974Sjamie	}
695195974Sjamie#endif
696191673Sjamie
697192895Sjamie	pr_allow = ch_allow = 0;
698192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
699192895Sjamie	    fi++) {
700192895Sjamie		vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
701192895Sjamie		vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
702192895Sjamie	}
703192895Sjamie	ch_allow |= pr_allow;
704192895Sjamie
705191673Sjamie	error = vfs_getopt(opts, "name", (void **)&name, &len);
706191673Sjamie	if (error == ENOENT)
707191673Sjamie		name = NULL;
708191673Sjamie	else if (error != 0)
709191673Sjamie		goto done_free;
710191673Sjamie	else {
711191673Sjamie		if (len == 0 || name[len - 1] != '\0') {
712191673Sjamie			error = EINVAL;
713191673Sjamie			goto done_free;
714191673Sjamie		}
715191673Sjamie		if (len > MAXHOSTNAMELEN) {
716191673Sjamie			error = ENAMETOOLONG;
717191673Sjamie			goto done_free;
718191673Sjamie		}
719191673Sjamie	}
720191673Sjamie
721191673Sjamie	error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
722191673Sjamie	if (error == ENOENT)
723191673Sjamie		host = NULL;
724191673Sjamie	else if (error != 0)
725191673Sjamie		goto done_free;
726191673Sjamie	else {
727193066Sjamie		ch_flags |= PR_HOST;
728193066Sjamie		pr_flags |= PR_HOST;
729191673Sjamie		if (len == 0 || host[len - 1] != '\0') {
730191673Sjamie			error = EINVAL;
731191673Sjamie			goto done_free;
732191673Sjamie		}
733191673Sjamie		if (len > MAXHOSTNAMELEN) {
734191673Sjamie			error = ENAMETOOLONG;
735191673Sjamie			goto done_free;
736191673Sjamie		}
737191673Sjamie	}
738191673Sjamie
739193066Sjamie	error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
740193066Sjamie	if (error == ENOENT)
741193066Sjamie		domain = NULL;
742193066Sjamie	else if (error != 0)
743193066Sjamie		goto done_free;
744193066Sjamie	else {
745193066Sjamie		ch_flags |= PR_HOST;
746193066Sjamie		pr_flags |= PR_HOST;
747193066Sjamie		if (len == 0 || domain[len - 1] != '\0') {
748193066Sjamie			error = EINVAL;
749193066Sjamie			goto done_free;
750193066Sjamie		}
751193066Sjamie		if (len > MAXHOSTNAMELEN) {
752193066Sjamie			error = ENAMETOOLONG;
753193066Sjamie			goto done_free;
754193066Sjamie		}
755193066Sjamie	}
756193066Sjamie
757193066Sjamie	error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
758193066Sjamie	if (error == ENOENT)
759193066Sjamie		uuid = NULL;
760193066Sjamie	else if (error != 0)
761193066Sjamie		goto done_free;
762193066Sjamie	else {
763193066Sjamie		ch_flags |= PR_HOST;
764193066Sjamie		pr_flags |= PR_HOST;
765193066Sjamie		if (len == 0 || uuid[len - 1] != '\0') {
766193066Sjamie			error = EINVAL;
767193066Sjamie			goto done_free;
768193066Sjamie		}
769193066Sjamie		if (len > HOSTUUIDLEN) {
770193066Sjamie			error = ENAMETOOLONG;
771193066Sjamie			goto done_free;
772193066Sjamie		}
773193066Sjamie	}
774193066Sjamie
775205014Snwhitehorn#ifdef COMPAT_FREEBSD32
776217896Sdchagin	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
777193066Sjamie		uint32_t hid32;
778193066Sjamie
779193066Sjamie		error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
780193066Sjamie		hid = hid32;
781193066Sjamie	} else
782193066Sjamie#endif
783193066Sjamie		error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
784193066Sjamie	if (error == ENOENT)
785193066Sjamie		gothid = 0;
786193066Sjamie	else if (error != 0)
787193066Sjamie		goto done_free;
788193066Sjamie	else {
789193066Sjamie		gothid = 1;
790193066Sjamie		ch_flags |= PR_HOST;
791193066Sjamie		pr_flags |= PR_HOST;
792193066Sjamie	}
793193066Sjamie
794185435Sbz#ifdef INET
795191673Sjamie	error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
796191673Sjamie	if (error == ENOENT)
797195870Sjamie		ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1;
798191673Sjamie	else if (error != 0)
799191673Sjamie		goto done_free;
800191673Sjamie	else if (ip4s & (sizeof(*ip4) - 1)) {
801191673Sjamie		error = EINVAL;
802191673Sjamie		goto done_free;
803192895Sjamie	} else {
804195870Sjamie		ch_flags |= PR_IP4_USER | PR_IP4_DISABLE;
805195870Sjamie		if (ip4s == 0)
806195870Sjamie			pr_flags |= PR_IP4_USER | PR_IP4_DISABLE;
807195870Sjamie		else {
808195870Sjamie			pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER;
809192895Sjamie			ip4s /= sizeof(*ip4);
810192895Sjamie			if (ip4s > jail_max_af_ips) {
811185435Sbz				error = EINVAL;
812192895Sjamie				vfs_opterror(opts, "too many IPv4 addresses");
813192895Sjamie				goto done_errmsg;
814185435Sbz			}
815195974Sjamie			ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
816192895Sjamie			bcopy(op, ip4, ip4s * sizeof(*ip4));
817192895Sjamie			/*
818192895Sjamie			 * IP addresses are all sorted but ip[0] to preserve
819192895Sjamie			 * the primary IP address as given from userland.
820192895Sjamie			 * This special IP is used for unbound outgoing
821202116Sbz			 * connections as well for "loopback" traffic in case
822202116Sbz			 * source address selection cannot find any more fitting
823202116Sbz			 * address to connect from.
824192895Sjamie			 */
825192895Sjamie			if (ip4s > 1)
826192895Sjamie				qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4);
827192895Sjamie			/*
828192895Sjamie			 * Check for duplicate addresses and do some simple
829192895Sjamie			 * zero and broadcast checks. If users give other bogus
830192895Sjamie			 * addresses it is their problem.
831192895Sjamie			 *
832192895Sjamie			 * We do not have to care about byte order for these
833192895Sjamie			 * checks so we will do them in NBO.
834192895Sjamie			 */
835192895Sjamie			for (ii = 0; ii < ip4s; ii++) {
836192895Sjamie				if (ip4[ii].s_addr == INADDR_ANY ||
837192895Sjamie				    ip4[ii].s_addr == INADDR_BROADCAST) {
838192895Sjamie					error = EINVAL;
839192895Sjamie					goto done_free;
840192895Sjamie				}
841192895Sjamie				if ((ii+1) < ip4s &&
842192895Sjamie				    (ip4[0].s_addr == ip4[ii+1].s_addr ||
843192895Sjamie				     ip4[ii].s_addr == ip4[ii+1].s_addr)) {
844192895Sjamie					error = EINVAL;
845192895Sjamie					goto done_free;
846192895Sjamie				}
847192895Sjamie			}
848185435Sbz		}
849191673Sjamie	}
850191673Sjamie#endif
851185435Sbz
852185435Sbz#ifdef INET6
853191673Sjamie	error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
854191673Sjamie	if (error == ENOENT)
855195870Sjamie		ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1;
856191673Sjamie	else if (error != 0)
857191673Sjamie		goto done_free;
858191673Sjamie	else if (ip6s & (sizeof(*ip6) - 1)) {
859191673Sjamie		error = EINVAL;
860191673Sjamie		goto done_free;
861192895Sjamie	} else {
862195870Sjamie		ch_flags |= PR_IP6_USER | PR_IP6_DISABLE;
863195870Sjamie		if (ip6s == 0)
864195870Sjamie			pr_flags |= PR_IP6_USER | PR_IP6_DISABLE;
865195870Sjamie		else {
866195870Sjamie			pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER;
867192895Sjamie			ip6s /= sizeof(*ip6);
868192895Sjamie			if (ip6s > jail_max_af_ips) {
869185435Sbz				error = EINVAL;
870192895Sjamie				vfs_opterror(opts, "too many IPv6 addresses");
871192895Sjamie				goto done_errmsg;
872185435Sbz			}
873195974Sjamie			ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
874192895Sjamie			bcopy(op, ip6, ip6s * sizeof(*ip6));
875192895Sjamie			if (ip6s > 1)
876192895Sjamie				qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6);
877192895Sjamie			for (ii = 0; ii < ip6s; ii++) {
878192895Sjamie				if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
879192895Sjamie					error = EINVAL;
880192895Sjamie					goto done_free;
881192895Sjamie				}
882192895Sjamie				if ((ii+1) < ip6s &&
883192895Sjamie				    (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
884192895Sjamie				     IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
885192895Sjamie				{
886192895Sjamie					error = EINVAL;
887192895Sjamie					goto done_free;
888192895Sjamie				}
889192895Sjamie			}
890185435Sbz		}
891191673Sjamie	}
892185435Sbz#endif
893185435Sbz
894195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6))
895195945Sjamie	if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
896195945Sjamie		error = EINVAL;
897195945Sjamie		vfs_opterror(opts,
898195945Sjamie		    "vnet jails cannot have IP address restrictions");
899195945Sjamie		goto done_errmsg;
900195945Sjamie	}
901195945Sjamie#endif
902195945Sjamie
903230143Smm	fullpath_disabled = 0;
904191673Sjamie	root = NULL;
905191673Sjamie	error = vfs_getopt(opts, "path", (void **)&path, &len);
906191673Sjamie	if (error == ENOENT)
907191673Sjamie		path = NULL;
908191673Sjamie	else if (error != 0)
909191673Sjamie		goto done_free;
910191673Sjamie	else {
911191673Sjamie		if (flags & JAIL_UPDATE) {
912191673Sjamie			error = EINVAL;
913191673Sjamie			vfs_opterror(opts,
914191673Sjamie			    "path cannot be changed after creation");
915191673Sjamie			goto done_errmsg;
916191673Sjamie		}
917191673Sjamie		if (len == 0 || path[len - 1] != '\0') {
918191673Sjamie			error = EINVAL;
919191673Sjamie			goto done_free;
920191673Sjamie		}
921230129Smm		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE, UIO_SYSSPACE,
922230129Smm		    path, td);
923230129Smm		error = namei(&nd);
924230129Smm		if (error)
925230129Smm			goto done_free;
926230129Smm		vfslocked = NDHASGIANT(&nd);
927230129Smm		root = nd.ni_vp;
928230129Smm		NDFREE(&nd, NDF_ONLY_PNBUF);
929230407Smm		g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
930230407Smm		strlcpy(g_path, path, MAXPATHLEN);
931230407Smm		error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
932230407Smm		if (error == 0)
933230407Smm			path = g_path;
934230407Smm		else if (error == ENODEV) {
935230129Smm			/* proceed if sysctl debug.disablefullpath == 1 */
936230129Smm			fullpath_disabled = 1;
937230129Smm			if (len < 2 || (len == 2 && path[0] == '/'))
938230129Smm				path = NULL;
939230407Smm		} else {
940230129Smm			/* exit on other errors */
941230129Smm			VFS_UNLOCK_GIANT(vfslocked);
942230129Smm			goto done_free;
943230129Smm		}
944230129Smm		if (root->v_type != VDIR) {
945230129Smm			error = ENOTDIR;
946230129Smm			vput(root);
947230129Smm			VFS_UNLOCK_GIANT(vfslocked);
948230129Smm			goto done_free;
949230129Smm		}
950230129Smm		VOP_UNLOCK(root, 0);
951230129Smm		VFS_UNLOCK_GIANT(vfslocked);
952230129Smm		if (fullpath_disabled) {
953192895Sjamie			/* Leave room for a real-root full pathname. */
954192895Sjamie			if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
955192895Sjamie			    ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
956192895Sjamie				error = ENAMETOOLONG;
957192895Sjamie				goto done_free;
958192895Sjamie			}
959191673Sjamie		}
960191673Sjamie	}
961185435Sbz
962191673Sjamie	/*
963191673Sjamie	 * Grab the allprison lock before letting modules check their
964191673Sjamie	 * parameters.  Once we have it, do not let go so we'll have a
965191673Sjamie	 * consistent view of the OSD list.
966191673Sjamie	 */
967191673Sjamie	sx_xlock(&allprison_lock);
968191673Sjamie	error = osd_jail_call(NULL, PR_METHOD_CHECK, opts);
969191673Sjamie	if (error)
970191673Sjamie		goto done_unlock_list;
971185435Sbz
972191673Sjamie	/* By now, all parameters should have been noted. */
973191673Sjamie	TAILQ_FOREACH(opt, opts, link) {
974191673Sjamie		if (!opt->seen && strcmp(opt->name, "errmsg")) {
975191673Sjamie			error = EINVAL;
976191673Sjamie			vfs_opterror(opts, "unknown parameter: %s", opt->name);
977191673Sjamie			goto done_unlock_list;
978191673Sjamie		}
979191673Sjamie	}
980191673Sjamie
981185435Sbz	/*
982191673Sjamie	 * See if we are creating a new record or updating an existing one.
983191673Sjamie	 * This abuses the file error codes ENOENT and EEXIST.
984185435Sbz	 */
985191673Sjamie	cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
986191673Sjamie	if (!cuflags) {
987191673Sjamie		error = EINVAL;
988191673Sjamie		vfs_opterror(opts, "no valid operation (create or update)");
989191673Sjamie		goto done_unlock_list;
990191673Sjamie	}
991191673Sjamie	pr = NULL;
992196835Sjamie	namelc = NULL;
993196835Sjamie	if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
994196835Sjamie		namelc = strrchr(name, '.');
995196835Sjamie		jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
996196835Sjamie		if (*p != '\0')
997196835Sjamie			jid = 0;
998196835Sjamie	}
999191673Sjamie	if (jid != 0) {
1000192895Sjamie		/*
1001192895Sjamie		 * See if a requested jid already exists.  There is an
1002192895Sjamie		 * information leak here if the jid exists but is not within
1003192895Sjamie		 * the caller's jail hierarchy.  Jail creators will get EEXIST
1004192895Sjamie		 * even though they cannot see the jail, and CREATE | UPDATE
1005192895Sjamie		 * will return ENOENT which is not normally a valid error.
1006192895Sjamie		 */
1007191673Sjamie		if (jid < 0) {
1008191673Sjamie			error = EINVAL;
1009191673Sjamie			vfs_opterror(opts, "negative jid");
1010191673Sjamie			goto done_unlock_list;
1011191673Sjamie		}
1012191673Sjamie		pr = prison_find(jid);
1013191673Sjamie		if (pr != NULL) {
1014192895Sjamie			ppr = pr->pr_parent;
1015191673Sjamie			/* Create: jid must not exist. */
1016191673Sjamie			if (cuflags == JAIL_CREATE) {
1017191673Sjamie				mtx_unlock(&pr->pr_mtx);
1018191673Sjamie				error = EEXIST;
1019191673Sjamie				vfs_opterror(opts, "jail %d already exists",
1020191673Sjamie				    jid);
1021191673Sjamie				goto done_unlock_list;
1022191673Sjamie			}
1023192895Sjamie			if (!prison_ischild(mypr, pr)) {
1024192895Sjamie				mtx_unlock(&pr->pr_mtx);
1025192895Sjamie				pr = NULL;
1026192895Sjamie			} else if (pr->pr_uref == 0) {
1027191673Sjamie				if (!(flags & JAIL_DYING)) {
1028191673Sjamie					mtx_unlock(&pr->pr_mtx);
1029191673Sjamie					error = ENOENT;
1030191673Sjamie					vfs_opterror(opts, "jail %d is dying",
1031191673Sjamie					    jid);
1032191673Sjamie					goto done_unlock_list;
1033191673Sjamie				} else if ((flags & JAIL_ATTACH) ||
1034191673Sjamie				    (pr_flags & PR_PERSIST)) {
1035191673Sjamie					/*
1036191673Sjamie					 * A dying jail might be resurrected
1037191673Sjamie					 * (via attach or persist), but first
1038191673Sjamie					 * it must determine if another jail
1039191673Sjamie					 * has claimed its name.  Accomplish
1040191673Sjamie					 * this by implicitly re-setting the
1041191673Sjamie					 * name.
1042191673Sjamie					 */
1043191673Sjamie					if (name == NULL)
1044192895Sjamie						name = prison_name(mypr, pr);
1045191673Sjamie				}
1046191673Sjamie			}
1047191673Sjamie		}
1048191673Sjamie		if (pr == NULL) {
1049191673Sjamie			/* Update: jid must exist. */
1050191673Sjamie			if (cuflags == JAIL_UPDATE) {
1051191673Sjamie				error = ENOENT;
1052191673Sjamie				vfs_opterror(opts, "jail %d not found", jid);
1053191673Sjamie				goto done_unlock_list;
1054191673Sjamie			}
1055191673Sjamie		}
1056191673Sjamie	}
1057191673Sjamie	/*
1058191673Sjamie	 * If the caller provided a name, look for a jail by that name.
1059191673Sjamie	 * This has different semantics for creates and updates keyed by jid
1060191673Sjamie	 * (where the name must not already exist in a different jail),
1061191673Sjamie	 * and updates keyed by the name itself (where the name must exist
1062191673Sjamie	 * because that is the jail being updated).
1063191673Sjamie	 */
1064191673Sjamie	if (name != NULL) {
1065196835Sjamie		namelc = strrchr(name, '.');
1066196835Sjamie		if (namelc == NULL)
1067196835Sjamie			namelc = name;
1068196835Sjamie		else {
1069192895Sjamie			/*
1070192895Sjamie			 * This is a hierarchical name.  Split it into the
1071192895Sjamie			 * parent and child names, and make sure the parent
1072192895Sjamie			 * exists or matches an already found jail.
1073192895Sjamie			 */
1074196835Sjamie			*namelc = '\0';
1075192895Sjamie			if (pr != NULL) {
1076196835Sjamie				if (strncmp(name, ppr->pr_name, namelc - name)
1077196835Sjamie				    || ppr->pr_name[namelc - name] != '\0') {
1078192895Sjamie					mtx_unlock(&pr->pr_mtx);
1079192895Sjamie					error = EINVAL;
1080192895Sjamie					vfs_opterror(opts,
1081192895Sjamie					    "cannot change jail's parent");
1082192895Sjamie					goto done_unlock_list;
1083192895Sjamie				}
1084192895Sjamie			} else {
1085192895Sjamie				ppr = prison_find_name(mypr, name);
1086192895Sjamie				if (ppr == NULL) {
1087192895Sjamie					error = ENOENT;
1088192895Sjamie					vfs_opterror(opts,
1089192895Sjamie					    "jail \"%s\" not found", name);
1090192895Sjamie					goto done_unlock_list;
1091192895Sjamie				}
1092192895Sjamie				mtx_unlock(&ppr->pr_mtx);
1093192895Sjamie			}
1094196835Sjamie			name = ++namelc;
1095192895Sjamie		}
1096191673Sjamie		if (name[0] != '\0') {
1097192895Sjamie			namelen =
1098192895Sjamie			    (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1099192895Sjamie name_again:
1100191673Sjamie			deadpr = NULL;
1101192895Sjamie			FOREACH_PRISON_CHILD(ppr, tpr) {
1102191673Sjamie				if (tpr != pr && tpr->pr_ref > 0 &&
1103192895Sjamie				    !strcmp(tpr->pr_name + namelen, name)) {
1104191673Sjamie					if (pr == NULL &&
1105191673Sjamie					    cuflags != JAIL_CREATE) {
1106191673Sjamie						mtx_lock(&tpr->pr_mtx);
1107191673Sjamie						if (tpr->pr_ref > 0) {
1108191673Sjamie							/*
1109191673Sjamie							 * Use this jail
1110191673Sjamie							 * for updates.
1111191673Sjamie							 */
1112191673Sjamie							if (tpr->pr_uref > 0) {
1113191673Sjamie								pr = tpr;
1114191673Sjamie								break;
1115191673Sjamie							}
1116191673Sjamie							deadpr = tpr;
1117191673Sjamie						}
1118191673Sjamie						mtx_unlock(&tpr->pr_mtx);
1119191673Sjamie					} else if (tpr->pr_uref > 0) {
1120191673Sjamie						/*
1121191673Sjamie						 * Create, or update(jid):
1122191673Sjamie						 * name must not exist in an
1123192895Sjamie						 * active sibling jail.
1124191673Sjamie						 */
1125191673Sjamie						error = EEXIST;
1126191673Sjamie						if (pr != NULL)
1127191673Sjamie							mtx_unlock(&pr->pr_mtx);
1128191673Sjamie						vfs_opterror(opts,
1129191673Sjamie						   "jail \"%s\" already exists",
1130191673Sjamie						   name);
1131191673Sjamie						goto done_unlock_list;
1132191673Sjamie					}
1133191673Sjamie				}
1134191673Sjamie			}
1135191673Sjamie			/* If no active jail is found, use a dying one. */
1136191673Sjamie			if (deadpr != NULL && pr == NULL) {
1137191673Sjamie				if (flags & JAIL_DYING) {
1138191673Sjamie					mtx_lock(&deadpr->pr_mtx);
1139191673Sjamie					if (deadpr->pr_ref == 0) {
1140191673Sjamie						mtx_unlock(&deadpr->pr_mtx);
1141191673Sjamie						goto name_again;
1142191673Sjamie					}
1143191673Sjamie					pr = deadpr;
1144191673Sjamie				} else if (cuflags == JAIL_UPDATE) {
1145191673Sjamie					error = ENOENT;
1146191673Sjamie					vfs_opterror(opts,
1147191673Sjamie					    "jail \"%s\" is dying", name);
1148191673Sjamie					goto done_unlock_list;
1149191673Sjamie				}
1150191673Sjamie			}
1151191673Sjamie			/* Update: name must exist if no jid. */
1152191673Sjamie			else if (cuflags == JAIL_UPDATE && pr == NULL) {
1153191673Sjamie				error = ENOENT;
1154191673Sjamie				vfs_opterror(opts, "jail \"%s\" not found",
1155191673Sjamie				    name);
1156191673Sjamie				goto done_unlock_list;
1157191673Sjamie			}
1158191673Sjamie		}
1159191673Sjamie	}
1160191673Sjamie	/* Update: must provide a jid or name. */
1161191673Sjamie	else if (cuflags == JAIL_UPDATE && pr == NULL) {
1162191673Sjamie		error = ENOENT;
1163191673Sjamie		vfs_opterror(opts, "update specified no jail");
1164191673Sjamie		goto done_unlock_list;
1165191673Sjamie	}
1166185435Sbz
1167191673Sjamie	/* If there's no prison to update, create a new one and link it in. */
1168191673Sjamie	if (pr == NULL) {
1169194762Sjamie		for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1170194762Sjamie			if (tpr->pr_childcount >= tpr->pr_childmax) {
1171194762Sjamie				error = EPERM;
1172194762Sjamie				vfs_opterror(opts, "prison limit exceeded");
1173194762Sjamie				goto done_unlock_list;
1174194762Sjamie			}
1175191673Sjamie		created = 1;
1176192895Sjamie		mtx_lock(&ppr->pr_mtx);
1177192895Sjamie		if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) {
1178192895Sjamie			mtx_unlock(&ppr->pr_mtx);
1179192895Sjamie			error = ENOENT;
1180192895Sjamie			vfs_opterror(opts, "parent jail went away!");
1181192895Sjamie			goto done_unlock_list;
1182192895Sjamie		}
1183192895Sjamie		ppr->pr_ref++;
1184192895Sjamie		ppr->pr_uref++;
1185192895Sjamie		mtx_unlock(&ppr->pr_mtx);
1186191673Sjamie		pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1187191673Sjamie		if (jid == 0) {
1188191673Sjamie			/* Find the next free jid. */
1189191673Sjamie			jid = lastprid + 1;
1190191673Sjamie findnext:
1191191673Sjamie			if (jid == JAIL_MAX)
1192191673Sjamie				jid = 1;
1193191673Sjamie			TAILQ_FOREACH(tpr, &allprison, pr_list) {
1194191673Sjamie				if (tpr->pr_id < jid)
1195191673Sjamie					continue;
1196191673Sjamie				if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1197191673Sjamie					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1198191673Sjamie					break;
1199191673Sjamie				}
1200191673Sjamie				if (jid == lastprid) {
1201191673Sjamie					error = EAGAIN;
1202191673Sjamie					vfs_opterror(opts,
1203191673Sjamie					    "no available jail IDs");
1204191673Sjamie					free(pr, M_PRISON);
1205192895Sjamie					prison_deref(ppr, PD_DEREF |
1206192895Sjamie					    PD_DEUREF | PD_LIST_XLOCKED);
1207192895Sjamie					goto done_releroot;
1208191673Sjamie				}
1209191673Sjamie				jid++;
1210191673Sjamie				goto findnext;
1211191673Sjamie			}
1212191673Sjamie			lastprid = jid;
1213191673Sjamie		} else {
1214191673Sjamie			/*
1215191673Sjamie			 * The jail already has a jid (that did not yet exist),
1216191673Sjamie			 * so just find where to insert it.
1217191673Sjamie			 */
1218191673Sjamie			TAILQ_FOREACH(tpr, &allprison, pr_list)
1219191673Sjamie				if (tpr->pr_id >= jid) {
1220191673Sjamie					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1221191673Sjamie					break;
1222191673Sjamie				}
1223191673Sjamie		}
1224191673Sjamie		if (tpr == NULL)
1225191673Sjamie			TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1226192895Sjamie		LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1227192895Sjamie		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1228194762Sjamie			tpr->pr_childcount++;
1229185435Sbz
1230192895Sjamie		pr->pr_parent = ppr;
1231191673Sjamie		pr->pr_id = jid;
1232192895Sjamie
1233192895Sjamie		/* Set some default values, and inherit some from the parent. */
1234191673Sjamie		if (name == NULL)
1235191673Sjamie			name = "";
1236191673Sjamie		if (path == NULL) {
1237191673Sjamie			path = "/";
1238192895Sjamie			root = mypr->pr_root;
1239191673Sjamie			vref(root);
1240191673Sjamie		}
1241195944Sjamie		strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1242195944Sjamie		pr->pr_flags |= PR_HOST;
1243195945Sjamie#if defined(INET) || defined(INET6)
1244195945Sjamie#ifdef VIMAGE
1245195945Sjamie		if (!(pr_flags & PR_VNET))
1246195945Sjamie#endif
1247195945Sjamie		{
1248192895Sjamie#ifdef INET
1249195974Sjamie			if (!(ch_flags & PR_IP4_USER))
1250195974Sjamie				pr->pr_flags |=
1251195974Sjamie				    PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE;
1252195974Sjamie			else if (!(pr_flags & PR_IP4_USER)) {
1253195974Sjamie				pr->pr_flags |= ppr->pr_flags & PR_IP4;
1254195974Sjamie				if (ppr->pr_ip4 != NULL) {
1255195974Sjamie					pr->pr_ip4s = ppr->pr_ip4s;
1256195974Sjamie					pr->pr_ip4 = malloc(pr->pr_ip4s *
1257195974Sjamie					    sizeof(struct in_addr), M_PRISON,
1258195974Sjamie					    M_WAITOK);
1259195974Sjamie					bcopy(ppr->pr_ip4, pr->pr_ip4,
1260195974Sjamie					    pr->pr_ip4s * sizeof(*pr->pr_ip4));
1261195974Sjamie				}
1262195974Sjamie			}
1263192895Sjamie#endif
1264192895Sjamie#ifdef INET6
1265195974Sjamie			if (!(ch_flags & PR_IP6_USER))
1266195974Sjamie				pr->pr_flags |=
1267195974Sjamie				    PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE;
1268195974Sjamie			else if (!(pr_flags & PR_IP6_USER)) {
1269195974Sjamie				pr->pr_flags |= ppr->pr_flags & PR_IP6;
1270195974Sjamie				if (ppr->pr_ip6 != NULL) {
1271195974Sjamie					pr->pr_ip6s = ppr->pr_ip6s;
1272195974Sjamie					pr->pr_ip6 = malloc(pr->pr_ip6s *
1273195974Sjamie					    sizeof(struct in6_addr), M_PRISON,
1274195974Sjamie					    M_WAITOK);
1275195974Sjamie					bcopy(ppr->pr_ip6, pr->pr_ip6,
1276195974Sjamie					    pr->pr_ip6s * sizeof(*pr->pr_ip6));
1277195974Sjamie				}
1278195974Sjamie			}
1279192895Sjamie#endif
1280195945Sjamie		}
1281195945Sjamie#endif
1282202468Sbz		/* Source address selection is always on by default. */
1283202468Sbz		pr->pr_flags |= _PR_IP_SADDRSEL;
1284202468Sbz
1285192895Sjamie		pr->pr_securelevel = ppr->pr_securelevel;
1286192895Sjamie		pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1287196002Sjamie		pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
1288232059Smm		pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1289191673Sjamie
1290192895Sjamie		LIST_INIT(&pr->pr_children);
1291192895Sjamie		mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1292191673Sjamie
1293194251Sjamie#ifdef VIMAGE
1294194251Sjamie		/* Allocate a new vnet if specified. */
1295194251Sjamie		pr->pr_vnet = (pr_flags & PR_VNET)
1296194251Sjamie		    ? vnet_alloc() : ppr->pr_vnet;
1297194251Sjamie#endif
1298185435Sbz		/*
1299191673Sjamie		 * Allocate a dedicated cpuset for each jail.
1300191673Sjamie		 * Unlike other initial settings, this may return an erorr.
1301185435Sbz		 */
1302192895Sjamie		error = cpuset_create_root(ppr, &pr->pr_cpuset);
1303191673Sjamie		if (error) {
1304191673Sjamie			prison_deref(pr, PD_LIST_XLOCKED);
1305191673Sjamie			goto done_releroot;
1306191673Sjamie		}
1307185435Sbz
1308191673Sjamie		mtx_lock(&pr->pr_mtx);
1309185435Sbz		/*
1310191673Sjamie		 * New prisons do not yet have a reference, because we do not
1311191673Sjamie		 * want other to see the incomplete prison once the
1312191673Sjamie		 * allprison_lock is downgraded.
1313185435Sbz		 */
1314191673Sjamie	} else {
1315191673Sjamie		created = 0;
1316195974Sjamie		/*
1317195974Sjamie		 * Grab a reference for existing prisons, to ensure they
1318195974Sjamie		 * continue to exist for the duration of the call.
1319195974Sjamie		 */
1320195974Sjamie		pr->pr_ref++;
1321195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6))
1322195945Sjamie		if ((pr->pr_flags & PR_VNET) &&
1323195945Sjamie		    (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1324195945Sjamie			error = EINVAL;
1325195945Sjamie			vfs_opterror(opts,
1326195945Sjamie			    "vnet jails cannot have IP address restrictions");
1327195945Sjamie			goto done_deref_locked;
1328195945Sjamie		}
1329195945Sjamie#endif
1330195974Sjamie#ifdef INET
1331195974Sjamie		if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1332195974Sjamie			error = EINVAL;
1333195974Sjamie			vfs_opterror(opts,
1334195974Sjamie			    "ip4 cannot be changed after creation");
1335195974Sjamie			goto done_deref_locked;
1336195974Sjamie		}
1337195974Sjamie#endif
1338195974Sjamie#ifdef INET6
1339195974Sjamie		if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1340195974Sjamie			error = EINVAL;
1341195974Sjamie			vfs_opterror(opts,
1342195974Sjamie			    "ip6 cannot be changed after creation");
1343195974Sjamie			goto done_deref_locked;
1344195974Sjamie		}
1345195974Sjamie#endif
1346191673Sjamie	}
1347185435Sbz
1348191673Sjamie	/* Do final error checking before setting anything. */
1349192895Sjamie	if (gotslevel) {
1350192895Sjamie		if (slevel < ppr->pr_securelevel) {
1351192895Sjamie			error = EPERM;
1352192895Sjamie			goto done_deref_locked;
1353192895Sjamie		}
1354192895Sjamie	}
1355194762Sjamie	if (gotchildmax) {
1356194762Sjamie		if (childmax >= ppr->pr_childmax) {
1357194762Sjamie			error = EPERM;
1358194762Sjamie			goto done_deref_locked;
1359194762Sjamie		}
1360194762Sjamie	}
1361192895Sjamie	if (gotenforce) {
1362192895Sjamie		if (enforce < ppr->pr_enforce_statfs) {
1363192895Sjamie			error = EPERM;
1364192895Sjamie			goto done_deref_locked;
1365192895Sjamie		}
1366192895Sjamie	}
1367231267Smm	if (gotrsnum) {
1368231267Smm		/*
1369231267Smm		 * devfs_rsnum is a uint16_t
1370231267Smm		 */
1371232059Smm		if (rsnum < 0 || rsnum > 65535) {
1372231267Smm			error = EINVAL;
1373231267Smm			goto done_deref_locked;
1374231267Smm		}
1375231267Smm		/*
1376232059Smm		 * Nested jails always inherit parent's devfs ruleset
1377231267Smm		 */
1378231267Smm		if (jailed(td->td_ucred)) {
1379231267Smm			if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
1380231267Smm				error = EPERM;
1381231267Smm				goto done_deref_locked;
1382232059Smm			} else
1383231267Smm				rsnum = ppr->pr_devfs_rsnum;
1384231267Smm		}
1385231267Smm	}
1386185435Sbz#ifdef INET
1387195974Sjamie	if (ip4s > 0) {
1388192895Sjamie		if (ppr->pr_flags & PR_IP4) {
1389195974Sjamie			/*
1390195974Sjamie			 * Make sure the new set of IP addresses is a
1391195974Sjamie			 * subset of the parent's list.  Don't worry
1392195974Sjamie			 * about the parent being unlocked, as any
1393195974Sjamie			 * setting is done with allprison_lock held.
1394195974Sjamie			 */
1395195974Sjamie			for (ij = 0; ij < ppr->pr_ip4s; ij++)
1396195974Sjamie				if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1397195974Sjamie					break;
1398195974Sjamie			if (ij == ppr->pr_ip4s) {
1399195974Sjamie				error = EPERM;
1400195974Sjamie				goto done_deref_locked;
1401195974Sjamie			}
1402195974Sjamie			if (ip4s > 1) {
1403195974Sjamie				for (ii = ij = 1; ii < ip4s; ii++) {
1404195974Sjamie					if (ip4[ii].s_addr ==
1405195974Sjamie					    ppr->pr_ip4[0].s_addr)
1406195974Sjamie						continue;
1407195974Sjamie					for (; ij < ppr->pr_ip4s; ij++)
1408195974Sjamie						if (ip4[ii].s_addr ==
1409195974Sjamie						    ppr->pr_ip4[ij].s_addr)
1410195974Sjamie							break;
1411195974Sjamie					if (ij == ppr->pr_ip4s)
1412195974Sjamie						break;
1413192895Sjamie				}
1414192895Sjamie				if (ij == ppr->pr_ip4s) {
1415192895Sjamie					error = EPERM;
1416192895Sjamie					goto done_deref_locked;
1417192895Sjamie				}
1418192895Sjamie			}
1419192895Sjamie		}
1420195974Sjamie		/*
1421195974Sjamie		 * Check for conflicting IP addresses.  We permit them
1422195974Sjamie		 * if there is no more than one IP on each jail.  If
1423195974Sjamie		 * there is a duplicate on a jail with more than one
1424195974Sjamie		 * IP stop checking and return error.
1425195974Sjamie		 */
1426195974Sjamie		tppr = ppr;
1427195945Sjamie#ifdef VIMAGE
1428195974Sjamie		for (; tppr != &prison0; tppr = tppr->pr_parent)
1429195974Sjamie			if (tppr->pr_flags & PR_VNET)
1430195974Sjamie				break;
1431195945Sjamie#endif
1432195974Sjamie		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1433195974Sjamie			if (tpr == pr ||
1434195945Sjamie#ifdef VIMAGE
1435195974Sjamie			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1436195945Sjamie#endif
1437195974Sjamie			    tpr->pr_uref == 0) {
1438192895Sjamie				descend = 0;
1439195974Sjamie				continue;
1440195974Sjamie			}
1441195974Sjamie			if (!(tpr->pr_flags & PR_IP4_USER))
1442195974Sjamie				continue;
1443195974Sjamie			descend = 0;
1444195974Sjamie			if (tpr->pr_ip4 == NULL ||
1445195974Sjamie			    (ip4s == 1 && tpr->pr_ip4s == 1))
1446195974Sjamie				continue;
1447195974Sjamie			for (ii = 0; ii < ip4s; ii++) {
1448195974Sjamie				if (_prison_check_ip4(tpr, &ip4[ii]) == 0) {
1449195974Sjamie					error = EADDRINUSE;
1450195974Sjamie					vfs_opterror(opts,
1451195974Sjamie					    "IPv4 addresses clash");
1452195974Sjamie					goto done_deref_locked;
1453192895Sjamie				}
1454192895Sjamie			}
1455192895Sjamie		}
1456192895Sjamie	}
1457185435Sbz#endif
1458191673Sjamie#ifdef INET6
1459195974Sjamie	if (ip6s > 0) {
1460192895Sjamie		if (ppr->pr_flags & PR_IP6) {
1461195974Sjamie			/*
1462195974Sjamie			 * Make sure the new set of IP addresses is a
1463195974Sjamie			 * subset of the parent's list.
1464195974Sjamie			 */
1465195974Sjamie			for (ij = 0; ij < ppr->pr_ip6s; ij++)
1466195974Sjamie				if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1467195974Sjamie				    &ppr->pr_ip6[ij]))
1468195974Sjamie					break;
1469195974Sjamie			if (ij == ppr->pr_ip6s) {
1470195974Sjamie				error = EPERM;
1471195974Sjamie				goto done_deref_locked;
1472195974Sjamie			}
1473195974Sjamie			if (ip6s > 1) {
1474195974Sjamie				for (ii = ij = 1; ii < ip6s; ii++) {
1475195974Sjamie					if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1476195974Sjamie					     &ppr->pr_ip6[0]))
1477195974Sjamie						continue;
1478195974Sjamie					for (; ij < ppr->pr_ip6s; ij++)
1479195974Sjamie						if (IN6_ARE_ADDR_EQUAL(
1480195974Sjamie						    &ip6[ii], &ppr->pr_ip6[ij]))
1481195974Sjamie							break;
1482195974Sjamie					if (ij == ppr->pr_ip6s)
1483195974Sjamie						break;
1484192895Sjamie				}
1485192895Sjamie				if (ij == ppr->pr_ip6s) {
1486192895Sjamie					error = EPERM;
1487192895Sjamie					goto done_deref_locked;
1488192895Sjamie				}
1489192895Sjamie			}
1490192895Sjamie		}
1491195974Sjamie		/* Check for conflicting IP addresses. */
1492195974Sjamie		tppr = ppr;
1493195945Sjamie#ifdef VIMAGE
1494195974Sjamie		for (; tppr != &prison0; tppr = tppr->pr_parent)
1495195974Sjamie			if (tppr->pr_flags & PR_VNET)
1496195974Sjamie				break;
1497195945Sjamie#endif
1498195974Sjamie		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1499195974Sjamie			if (tpr == pr ||
1500195945Sjamie#ifdef VIMAGE
1501195974Sjamie			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1502195945Sjamie#endif
1503195974Sjamie			    tpr->pr_uref == 0) {
1504192895Sjamie				descend = 0;
1505195974Sjamie				continue;
1506195974Sjamie			}
1507195974Sjamie			if (!(tpr->pr_flags & PR_IP6_USER))
1508195974Sjamie				continue;
1509195974Sjamie			descend = 0;
1510195974Sjamie			if (tpr->pr_ip6 == NULL ||
1511195974Sjamie			    (ip6s == 1 && tpr->pr_ip6s == 1))
1512195974Sjamie				continue;
1513195974Sjamie			for (ii = 0; ii < ip6s; ii++) {
1514195974Sjamie				if (_prison_check_ip6(tpr, &ip6[ii]) == 0) {
1515195974Sjamie					error = EADDRINUSE;
1516195974Sjamie					vfs_opterror(opts,
1517195974Sjamie					    "IPv6 addresses clash");
1518195974Sjamie					goto done_deref_locked;
1519192895Sjamie				}
1520192895Sjamie			}
1521191673Sjamie		}
1522192895Sjamie	}
1523191673Sjamie#endif
1524192895Sjamie	onamelen = namelen = 0;
1525192895Sjamie	if (name != NULL) {
1526191673Sjamie		/* Give a default name of the jid. */
1527191673Sjamie		if (name[0] == '\0')
1528191673Sjamie			snprintf(name = numbuf, sizeof(numbuf), "%d", jid);
1529196835Sjamie		else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid &&
1530196835Sjamie		    *p == '\0')) {
1531191673Sjamie			error = EINVAL;
1532196835Sjamie			vfs_opterror(opts,
1533196835Sjamie			    "name cannot be numeric (unless it is the jid)");
1534192895Sjamie			goto done_deref_locked;
1535191673Sjamie		}
1536191673Sjamie		/*
1537192895Sjamie		 * Make sure the name isn't too long for the prison or its
1538192895Sjamie		 * children.
1539191673Sjamie		 */
1540192895Sjamie		onamelen = strlen(pr->pr_name);
1541192895Sjamie		namelen = strlen(name);
1542192895Sjamie		if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) {
1543192895Sjamie			error = ENAMETOOLONG;
1544192895Sjamie			goto done_deref_locked;
1545192895Sjamie		}
1546192895Sjamie		FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1547192895Sjamie			if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1548192895Sjamie			    sizeof(pr->pr_name)) {
1549192895Sjamie				error = ENAMETOOLONG;
1550192895Sjamie				goto done_deref_locked;
1551192895Sjamie			}
1552192895Sjamie		}
1553191673Sjamie	}
1554192895Sjamie	if (pr_allow & ~ppr->pr_allow) {
1555192895Sjamie		error = EPERM;
1556192895Sjamie		goto done_deref_locked;
1557192895Sjamie	}
1558185435Sbz
1559191673Sjamie	/* Set the parameters of the prison. */
1560191673Sjamie#ifdef INET
1561192895Sjamie	redo_ip4 = 0;
1562195974Sjamie	if (pr_flags & PR_IP4_USER) {
1563195974Sjamie		pr->pr_flags |= PR_IP4;
1564195974Sjamie		free(pr->pr_ip4, M_PRISON);
1565195974Sjamie		pr->pr_ip4s = ip4s;
1566195974Sjamie		pr->pr_ip4 = ip4;
1567195974Sjamie		ip4 = NULL;
1568192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1569195945Sjamie#ifdef VIMAGE
1570195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1571195945Sjamie				descend = 0;
1572195945Sjamie				continue;
1573195945Sjamie			}
1574195945Sjamie#endif
1575192895Sjamie			if (prison_restrict_ip4(tpr, NULL)) {
1576192895Sjamie				redo_ip4 = 1;
1577192895Sjamie				descend = 0;
1578192895Sjamie			}
1579192895Sjamie		}
1580185435Sbz	}
1581191673Sjamie#endif
1582191673Sjamie#ifdef INET6
1583192895Sjamie	redo_ip6 = 0;
1584195974Sjamie	if (pr_flags & PR_IP6_USER) {
1585195974Sjamie		pr->pr_flags |= PR_IP6;
1586195974Sjamie		free(pr->pr_ip6, M_PRISON);
1587195974Sjamie		pr->pr_ip6s = ip6s;
1588195974Sjamie		pr->pr_ip6 = ip6;
1589195974Sjamie		ip6 = NULL;
1590192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1591195945Sjamie#ifdef VIMAGE
1592195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1593195945Sjamie				descend = 0;
1594195945Sjamie				continue;
1595195945Sjamie			}
1596195945Sjamie#endif
1597192895Sjamie			if (prison_restrict_ip6(tpr, NULL)) {
1598192895Sjamie				redo_ip6 = 1;
1599192895Sjamie				descend = 0;
1600192895Sjamie			}
1601192895Sjamie		}
1602191673Sjamie	}
1603191673Sjamie#endif
1604192895Sjamie	if (gotslevel) {
1605191673Sjamie		pr->pr_securelevel = slevel;
1606192895Sjamie		/* Set all child jails to be at least this level. */
1607192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1608192895Sjamie			if (tpr->pr_securelevel < slevel)
1609192895Sjamie				tpr->pr_securelevel = slevel;
1610192895Sjamie	}
1611194762Sjamie	if (gotchildmax) {
1612194762Sjamie		pr->pr_childmax = childmax;
1613194762Sjamie		/* Set all child jails to under this limit. */
1614194762Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1615194762Sjamie			if (tpr->pr_childmax > childmax - level)
1616194762Sjamie				tpr->pr_childmax = childmax > level
1617194762Sjamie				    ? childmax - level : 0;
1618194762Sjamie	}
1619192895Sjamie	if (gotenforce) {
1620192895Sjamie		pr->pr_enforce_statfs = enforce;
1621192895Sjamie		/* Pass this restriction on to the children. */
1622192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1623192895Sjamie			if (tpr->pr_enforce_statfs < enforce)
1624192895Sjamie				tpr->pr_enforce_statfs = enforce;
1625192895Sjamie	}
1626231267Smm	if (gotrsnum) {
1627231267Smm		pr->pr_devfs_rsnum = rsnum;
1628231267Smm		/* Pass this restriction on to the children. */
1629231267Smm		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1630232059Smm			tpr->pr_devfs_rsnum = rsnum;
1631231267Smm	}
1632192895Sjamie	if (name != NULL) {
1633192895Sjamie		if (ppr == &prison0)
1634192895Sjamie			strlcpy(pr->pr_name, name, sizeof(pr->pr_name));
1635192895Sjamie		else
1636192895Sjamie			snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1637192895Sjamie			    ppr->pr_name, name);
1638192895Sjamie		/* Change this component of child names. */
1639192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1640192895Sjamie			bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1641192895Sjamie			    strlen(tpr->pr_name + onamelen) + 1);
1642192895Sjamie			bcopy(pr->pr_name, tpr->pr_name, namelen);
1643192895Sjamie		}
1644192895Sjamie	}
1645191673Sjamie	if (path != NULL) {
1646192895Sjamie		/* Try to keep a real-rooted full pathname. */
1647230129Smm		if (fullpath_disabled && path[0] == '/' &&
1648230129Smm		    strcmp(mypr->pr_path, "/"))
1649192895Sjamie			snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
1650192895Sjamie			    mypr->pr_path, path);
1651192895Sjamie		else
1652192895Sjamie			strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1653191673Sjamie		pr->pr_root = root;
1654191673Sjamie	}
1655193066Sjamie	if (PR_HOST & ch_flags & ~pr_flags) {
1656193066Sjamie		if (pr->pr_flags & PR_HOST) {
1657193066Sjamie			/*
1658193066Sjamie			 * Copy the parent's host info.  As with pr_ip4 above,
1659193066Sjamie			 * the lack of a lock on the parent is not a problem;
1660193066Sjamie			 * it is always set with allprison_lock at least
1661193066Sjamie			 * shared, and is held exclusively here.
1662193066Sjamie			 */
1663194118Sjamie			strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1664194118Sjamie			    sizeof(pr->pr_hostname));
1665194118Sjamie			strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1666194118Sjamie			    sizeof(pr->pr_domainname));
1667194118Sjamie			strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1668194118Sjamie			    sizeof(pr->pr_hostuuid));
1669193066Sjamie			pr->pr_hostid = pr->pr_parent->pr_hostid;
1670193066Sjamie		}
1671193066Sjamie	} else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1672193066Sjamie		/* Set this prison, and any descendants without PR_HOST. */
1673193066Sjamie		if (host != NULL)
1674194118Sjamie			strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1675193066Sjamie		if (domain != NULL)
1676194118Sjamie			strlcpy(pr->pr_domainname, domain,
1677194118Sjamie			    sizeof(pr->pr_domainname));
1678193066Sjamie		if (uuid != NULL)
1679194118Sjamie			strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1680193066Sjamie		if (gothid)
1681193066Sjamie			pr->pr_hostid = hid;
1682193066Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1683193066Sjamie			if (tpr->pr_flags & PR_HOST)
1684193066Sjamie				descend = 0;
1685193066Sjamie			else {
1686193066Sjamie				if (host != NULL)
1687194118Sjamie					strlcpy(tpr->pr_hostname,
1688194118Sjamie					    pr->pr_hostname,
1689194118Sjamie					    sizeof(tpr->pr_hostname));
1690193066Sjamie				if (domain != NULL)
1691194118Sjamie					strlcpy(tpr->pr_domainname,
1692194118Sjamie					    pr->pr_domainname,
1693194118Sjamie					    sizeof(tpr->pr_domainname));
1694193066Sjamie				if (uuid != NULL)
1695194118Sjamie					strlcpy(tpr->pr_hostuuid,
1696194118Sjamie					    pr->pr_hostuuid,
1697194118Sjamie					    sizeof(tpr->pr_hostuuid));
1698193066Sjamie				if (gothid)
1699193066Sjamie					tpr->pr_hostid = hid;
1700193066Sjamie			}
1701193066Sjamie		}
1702193066Sjamie	}
1703192895Sjamie	if ((tallow = ch_allow & ~pr_allow)) {
1704192895Sjamie		/* Clear allow bits in all children. */
1705192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1706192895Sjamie			tpr->pr_allow &= ~tallow;
1707192895Sjamie	}
1708192895Sjamie	pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1709191673Sjamie	/*
1710191673Sjamie	 * Persistent prisons get an extra reference, and prisons losing their
1711191673Sjamie	 * persist flag lose that reference.  Only do this for existing prisons
1712191673Sjamie	 * for now, so new ones will remain unseen until after the module
1713191673Sjamie	 * handlers have completed.
1714191673Sjamie	 */
1715191673Sjamie	if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1716191673Sjamie		if (pr_flags & PR_PERSIST) {
1717191673Sjamie			pr->pr_ref++;
1718191673Sjamie			pr->pr_uref++;
1719191673Sjamie		} else {
1720191673Sjamie			pr->pr_ref--;
1721191673Sjamie			pr->pr_uref--;
1722191673Sjamie		}
1723191673Sjamie	}
1724191673Sjamie	pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1725191673Sjamie	mtx_unlock(&pr->pr_mtx);
1726185435Sbz
1727221362Strasz#ifdef RACCT
1728221362Strasz	if (created)
1729221362Strasz		prison_racct_attach(pr);
1730221362Strasz#endif
1731221362Strasz
1732192895Sjamie	/* Locks may have prevented a complete restriction of child IP
1733192895Sjamie	 * addresses.  If so, allocate some more memory and try again.
1734192895Sjamie	 */
1735192895Sjamie#ifdef INET
1736192895Sjamie	while (redo_ip4) {
1737192895Sjamie		ip4s = pr->pr_ip4s;
1738192895Sjamie		ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1739192895Sjamie		mtx_lock(&pr->pr_mtx);
1740192895Sjamie		redo_ip4 = 0;
1741192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1742195945Sjamie#ifdef VIMAGE
1743195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1744195945Sjamie				descend = 0;
1745195945Sjamie				continue;
1746195945Sjamie			}
1747195945Sjamie#endif
1748192895Sjamie			if (prison_restrict_ip4(tpr, ip4)) {
1749192895Sjamie				if (ip4 != NULL)
1750192895Sjamie					ip4 = NULL;
1751192895Sjamie				else
1752192895Sjamie					redo_ip4 = 1;
1753192895Sjamie			}
1754192895Sjamie		}
1755192895Sjamie		mtx_unlock(&pr->pr_mtx);
1756192895Sjamie	}
1757192895Sjamie#endif
1758192895Sjamie#ifdef INET6
1759192895Sjamie	while (redo_ip6) {
1760192895Sjamie		ip6s = pr->pr_ip6s;
1761192895Sjamie		ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1762192895Sjamie		mtx_lock(&pr->pr_mtx);
1763192895Sjamie		redo_ip6 = 0;
1764192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1765195945Sjamie#ifdef VIMAGE
1766195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1767195945Sjamie				descend = 0;
1768195945Sjamie				continue;
1769195945Sjamie			}
1770195945Sjamie#endif
1771192895Sjamie			if (prison_restrict_ip6(tpr, ip6)) {
1772192895Sjamie				if (ip6 != NULL)
1773192895Sjamie					ip6 = NULL;
1774192895Sjamie				else
1775192895Sjamie					redo_ip6 = 1;
1776192895Sjamie			}
1777192895Sjamie		}
1778192895Sjamie		mtx_unlock(&pr->pr_mtx);
1779192895Sjamie	}
1780192895Sjamie#endif
1781192895Sjamie
1782191673Sjamie	/* Let the modules do their work. */
1783191673Sjamie	sx_downgrade(&allprison_lock);
1784191673Sjamie	if (created) {
1785191673Sjamie		error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1786191673Sjamie		if (error) {
1787191673Sjamie			prison_deref(pr, PD_LIST_SLOCKED);
1788191673Sjamie			goto done_errmsg;
1789191673Sjamie		}
1790191673Sjamie	}
1791191673Sjamie	error = osd_jail_call(pr, PR_METHOD_SET, opts);
1792191673Sjamie	if (error) {
1793191673Sjamie		prison_deref(pr, created
1794191673Sjamie		    ? PD_LIST_SLOCKED
1795191673Sjamie		    : PD_DEREF | PD_LIST_SLOCKED);
1796191673Sjamie		goto done_errmsg;
1797191673Sjamie	}
1798191673Sjamie
1799191673Sjamie	/* Attach this process to the prison if requested. */
1800191673Sjamie	if (flags & JAIL_ATTACH) {
1801191673Sjamie		mtx_lock(&pr->pr_mtx);
1802191673Sjamie		error = do_jail_attach(td, pr);
1803191673Sjamie		if (error) {
1804191673Sjamie			vfs_opterror(opts, "attach failed");
1805191673Sjamie			if (!created)
1806191673Sjamie				prison_deref(pr, PD_DEREF);
1807191673Sjamie			goto done_errmsg;
1808191673Sjamie		}
1809191673Sjamie	}
1810191673Sjamie
1811191673Sjamie	/*
1812191673Sjamie	 * Now that it is all there, drop the temporary reference from existing
1813191673Sjamie	 * prisons.  Or add a reference to newly created persistent prisons
1814191673Sjamie	 * (which was not done earlier so that the prison would not be publicly
1815191673Sjamie	 * visible).
1816191673Sjamie	 */
1817191673Sjamie	if (!created) {
1818191673Sjamie		prison_deref(pr, (flags & JAIL_ATTACH)
1819191673Sjamie		    ? PD_DEREF
1820191673Sjamie		    : PD_DEREF | PD_LIST_SLOCKED);
1821191673Sjamie	} else {
1822191673Sjamie		if (pr_flags & PR_PERSIST) {
1823191673Sjamie			mtx_lock(&pr->pr_mtx);
1824191673Sjamie			pr->pr_ref++;
1825191673Sjamie			pr->pr_uref++;
1826191673Sjamie			mtx_unlock(&pr->pr_mtx);
1827191673Sjamie		}
1828191673Sjamie		if (!(flags & JAIL_ATTACH))
1829191673Sjamie			sx_sunlock(&allprison_lock);
1830191673Sjamie	}
1831191673Sjamie	td->td_retval[0] = pr->pr_id;
1832191673Sjamie	goto done_errmsg;
1833191673Sjamie
1834192895Sjamie done_deref_locked:
1835192895Sjamie	prison_deref(pr, created
1836192895Sjamie	    ? PD_LOCKED | PD_LIST_XLOCKED
1837192895Sjamie	    : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
1838192895Sjamie	goto done_releroot;
1839191673Sjamie done_unlock_list:
1840191673Sjamie	sx_xunlock(&allprison_lock);
1841191673Sjamie done_releroot:
1842191673Sjamie	if (root != NULL) {
1843191673Sjamie		vfslocked = VFS_LOCK_GIANT(root->v_mount);
1844191673Sjamie		vrele(root);
1845191673Sjamie		VFS_UNLOCK_GIANT(vfslocked);
1846191673Sjamie	}
1847191673Sjamie done_errmsg:
1848191673Sjamie	if (error) {
1849191673Sjamie		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
1850191673Sjamie		if (errmsg_len > 0) {
1851191673Sjamie			errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1852191673Sjamie			if (errmsg_pos > 0) {
1853191673Sjamie				if (optuio->uio_segflg == UIO_SYSSPACE)
1854191673Sjamie					bcopy(errmsg,
1855191673Sjamie					   optuio->uio_iov[errmsg_pos].iov_base,
1856191673Sjamie					   errmsg_len);
1857191673Sjamie				else
1858191673Sjamie					copyout(errmsg,
1859191673Sjamie					   optuio->uio_iov[errmsg_pos].iov_base,
1860191673Sjamie					   errmsg_len);
1861191673Sjamie			}
1862191673Sjamie		}
1863191673Sjamie	}
1864191673Sjamie done_free:
1865191673Sjamie#ifdef INET
1866191673Sjamie	free(ip4, M_PRISON);
1867191673Sjamie#endif
1868191673Sjamie#ifdef INET6
1869191673Sjamie	free(ip6, M_PRISON);
1870191673Sjamie#endif
1871230407Smm	if (g_path != NULL)
1872230407Smm		free(g_path, M_TEMP);
1873191673Sjamie	vfs_freeopts(opts);
1874191673Sjamie	return (error);
1875191673Sjamie}
1876191673Sjamie
1877191673Sjamie
187882710Sdillon/*
1879191673Sjamie * struct jail_get_args {
1880191673Sjamie *	struct iovec *iovp;
1881191673Sjamie *	unsigned int iovcnt;
1882191673Sjamie *	int flags;
1883114168Smike * };
188482710Sdillon */
188546155Sphkint
1886225617Skmacysys_jail_get(struct thread *td, struct jail_get_args *uap)
188746155Sphk{
1888191673Sjamie	struct uio *auio;
1889185435Sbz	int error;
1890185435Sbz
1891191673Sjamie	/* Check that we have an even number of iovecs. */
1892191673Sjamie	if (uap->iovcnt & 1)
1893191673Sjamie		return (EINVAL);
1894191673Sjamie
1895191673Sjamie	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1896185435Sbz	if (error)
1897185435Sbz		return (error);
1898191673Sjamie	error = kern_jail_get(td, auio, uap->flags);
1899191673Sjamie	if (error == 0)
1900191673Sjamie		error = copyout(auio->uio_iov, uap->iovp,
1901191673Sjamie		    uap->iovcnt * sizeof (struct iovec));
1902191673Sjamie	free(auio, M_IOV);
1903191673Sjamie	return (error);
1904191673Sjamie}
1905185435Sbz
1906191673Sjamieint
1907191673Sjamiekern_jail_get(struct thread *td, struct uio *optuio, int flags)
1908191673Sjamie{
1909192895Sjamie	struct prison *pr, *mypr;
1910191673Sjamie	struct vfsopt *opt;
1911191673Sjamie	struct vfsoptlist *opts;
1912191673Sjamie	char *errmsg, *name;
1913192895Sjamie	int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
1914185435Sbz
1915191673Sjamie	if (flags & ~JAIL_GET_MASK)
1916191673Sjamie		return (EINVAL);
1917185435Sbz
1918191673Sjamie	/* Get the parameter list. */
1919191673Sjamie	error = vfs_buildopts(optuio, &opts);
1920191673Sjamie	if (error)
1921191673Sjamie		return (error);
1922191673Sjamie	errmsg_pos = vfs_getopt_pos(opts, "errmsg");
1923192895Sjamie	mypr = td->td_ucred->cr_prison;
1924185435Sbz
1925191673Sjamie	/*
1926191673Sjamie	 * Find the prison specified by one of: lastjid, jid, name.
1927191673Sjamie	 */
1928191673Sjamie	sx_slock(&allprison_lock);
1929191673Sjamie	error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
1930191673Sjamie	if (error == 0) {
1931191673Sjamie		TAILQ_FOREACH(pr, &allprison, pr_list) {
1932192895Sjamie			if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
1933191673Sjamie				mtx_lock(&pr->pr_mtx);
1934191673Sjamie				if (pr->pr_ref > 0 &&
1935191673Sjamie				    (pr->pr_uref > 0 || (flags & JAIL_DYING)))
1936191673Sjamie					break;
1937191673Sjamie				mtx_unlock(&pr->pr_mtx);
1938191673Sjamie			}
1939191673Sjamie		}
1940191673Sjamie		if (pr != NULL)
1941191673Sjamie			goto found_prison;
1942191673Sjamie		error = ENOENT;
1943191673Sjamie		vfs_opterror(opts, "no jail after %d", jid);
1944191673Sjamie		goto done_unlock_list;
1945191673Sjamie	} else if (error != ENOENT)
1946191673Sjamie		goto done_unlock_list;
1947185435Sbz
1948191673Sjamie	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1949191673Sjamie	if (error == 0) {
1950191673Sjamie		if (jid != 0) {
1951192895Sjamie			pr = prison_find_child(mypr, jid);
1952191673Sjamie			if (pr != NULL) {
1953191673Sjamie				if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1954191673Sjamie					mtx_unlock(&pr->pr_mtx);
1955191673Sjamie					error = ENOENT;
1956191673Sjamie					vfs_opterror(opts, "jail %d is dying",
1957191673Sjamie					    jid);
1958191673Sjamie					goto done_unlock_list;
1959191673Sjamie				}
1960191673Sjamie				goto found_prison;
1961191673Sjamie			}
1962191673Sjamie			error = ENOENT;
1963191673Sjamie			vfs_opterror(opts, "jail %d not found", jid);
1964191673Sjamie			goto done_unlock_list;
1965191673Sjamie		}
1966191673Sjamie	} else if (error != ENOENT)
1967191673Sjamie		goto done_unlock_list;
196846155Sphk
1969191673Sjamie	error = vfs_getopt(opts, "name", (void **)&name, &len);
1970191673Sjamie	if (error == 0) {
1971191673Sjamie		if (len == 0 || name[len - 1] != '\0') {
1972191673Sjamie			error = EINVAL;
1973191673Sjamie			goto done_unlock_list;
1974191673Sjamie		}
1975192895Sjamie		pr = prison_find_name(mypr, name);
1976191673Sjamie		if (pr != NULL) {
1977191673Sjamie			if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1978191673Sjamie				mtx_unlock(&pr->pr_mtx);
1979191673Sjamie				error = ENOENT;
1980191673Sjamie				vfs_opterror(opts, "jail \"%s\" is dying",
1981191673Sjamie				    name);
1982191673Sjamie				goto done_unlock_list;
1983191673Sjamie			}
1984191673Sjamie			goto found_prison;
1985191673Sjamie		}
1986191673Sjamie		error = ENOENT;
1987191673Sjamie		vfs_opterror(opts, "jail \"%s\" not found", name);
1988191673Sjamie		goto done_unlock_list;
1989191673Sjamie	} else if (error != ENOENT)
1990191673Sjamie		goto done_unlock_list;
1991185435Sbz
1992191673Sjamie	vfs_opterror(opts, "no jail specified");
1993191673Sjamie	error = ENOENT;
1994191673Sjamie	goto done_unlock_list;
1995191673Sjamie
1996191673Sjamie found_prison:
1997191673Sjamie	/* Get the parameters of the prison. */
1998191673Sjamie	pr->pr_ref++;
1999191673Sjamie	locked = PD_LOCKED;
2000191673Sjamie	td->td_retval[0] = pr->pr_id;
2001191673Sjamie	error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2002191673Sjamie	if (error != 0 && error != ENOENT)
2003191673Sjamie		goto done_deref;
2004192895Sjamie	i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
2005192895Sjamie	error = vfs_setopt(opts, "parent", &i, sizeof(i));
2006191673Sjamie	if (error != 0 && error != ENOENT)
2007191673Sjamie		goto done_deref;
2008192895Sjamie	error = vfs_setopts(opts, "name", prison_name(mypr, pr));
2009192895Sjamie	if (error != 0 && error != ENOENT)
2010192895Sjamie		goto done_deref;
2011192895Sjamie	error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2012191673Sjamie	    sizeof(pr->pr_cpuset->cs_id));
2013191673Sjamie	if (error != 0 && error != ENOENT)
2014191673Sjamie		goto done_deref;
2015192895Sjamie	error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2016191673Sjamie	if (error != 0 && error != ENOENT)
2017191673Sjamie		goto done_deref;
2018191673Sjamie#ifdef INET
2019191673Sjamie	error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
2020191673Sjamie	    pr->pr_ip4s * sizeof(*pr->pr_ip4));
2021191673Sjamie	if (error != 0 && error != ENOENT)
2022191673Sjamie		goto done_deref;
2023191673Sjamie#endif
2024191673Sjamie#ifdef INET6
2025191673Sjamie	error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
2026191673Sjamie	    pr->pr_ip6s * sizeof(*pr->pr_ip6));
2027191673Sjamie	if (error != 0 && error != ENOENT)
2028191673Sjamie		goto done_deref;
2029191673Sjamie#endif
2030191673Sjamie	error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2031191673Sjamie	    sizeof(pr->pr_securelevel));
2032191673Sjamie	if (error != 0 && error != ENOENT)
2033191673Sjamie		goto done_deref;
2034194762Sjamie	error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2035194762Sjamie	    sizeof(pr->pr_childcount));
2036194762Sjamie	if (error != 0 && error != ENOENT)
2037194762Sjamie		goto done_deref;
2038194762Sjamie	error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2039194762Sjamie	    sizeof(pr->pr_childmax));
2040194762Sjamie	if (error != 0 && error != ENOENT)
2041194762Sjamie		goto done_deref;
2042194118Sjamie	error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2043191673Sjamie	if (error != 0 && error != ENOENT)
2044191673Sjamie		goto done_deref;
2045194118Sjamie	error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
2046193066Sjamie	if (error != 0 && error != ENOENT)
2047193066Sjamie		goto done_deref;
2048194118Sjamie	error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
2049193066Sjamie	if (error != 0 && error != ENOENT)
2050193066Sjamie		goto done_deref;
2051205014Snwhitehorn#ifdef COMPAT_FREEBSD32
2052217896Sdchagin	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
2053193066Sjamie		uint32_t hid32 = pr->pr_hostid;
2054193066Sjamie
2055193066Sjamie		error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
2056193066Sjamie	} else
2057193066Sjamie#endif
2058193066Sjamie	error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
2059193066Sjamie	    sizeof(pr->pr_hostid));
2060193066Sjamie	if (error != 0 && error != ENOENT)
2061193066Sjamie		goto done_deref;
2062192895Sjamie	error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
2063192895Sjamie	    sizeof(pr->pr_enforce_statfs));
2064191673Sjamie	if (error != 0 && error != ENOENT)
2065191673Sjamie		goto done_deref;
2066231267Smm	error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
2067231267Smm	    sizeof(pr->pr_devfs_rsnum));
2068231267Smm	if (error != 0 && error != ENOENT)
2069231267Smm		goto done_deref;
2070192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
2071192895Sjamie	    fi++) {
2072192895Sjamie		if (pr_flag_names[fi] == NULL)
2073192895Sjamie			continue;
2074192895Sjamie		i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
2075192895Sjamie		error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
2076192895Sjamie		if (error != 0 && error != ENOENT)
2077192895Sjamie			goto done_deref;
2078192895Sjamie		i = !i;
2079192895Sjamie		error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
2080192895Sjamie		if (error != 0 && error != ENOENT)
2081192895Sjamie			goto done_deref;
2082192895Sjamie	}
2083195870Sjamie	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
2084195870Sjamie	    fi++) {
2085195870Sjamie		i = pr->pr_flags &
2086195870Sjamie		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
2087195870Sjamie		i = pr_flag_jailsys[fi].disable &&
2088195870Sjamie		      (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE
2089195870Sjamie		    : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW
2090195870Sjamie		    : JAIL_SYS_INHERIT;
2091195870Sjamie		error =
2092195870Sjamie		    vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i));
2093195870Sjamie		if (error != 0 && error != ENOENT)
2094195870Sjamie			goto done_deref;
2095195870Sjamie	}
2096192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
2097192895Sjamie	    fi++) {
2098192895Sjamie		if (pr_allow_names[fi] == NULL)
2099192895Sjamie			continue;
2100192895Sjamie		i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
2101192895Sjamie		error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
2102192895Sjamie		if (error != 0 && error != ENOENT)
2103192895Sjamie			goto done_deref;
2104192895Sjamie		i = !i;
2105192895Sjamie		error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
2106192895Sjamie		if (error != 0 && error != ENOENT)
2107192895Sjamie			goto done_deref;
2108192895Sjamie	}
2109191673Sjamie	i = (pr->pr_uref == 0);
2110191673Sjamie	error = vfs_setopt(opts, "dying", &i, sizeof(i));
2111191673Sjamie	if (error != 0 && error != ENOENT)
2112191673Sjamie		goto done_deref;
2113191673Sjamie	i = !i;
2114191673Sjamie	error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2115191673Sjamie	if (error != 0 && error != ENOENT)
2116191673Sjamie		goto done_deref;
2117191673Sjamie
2118191673Sjamie	/* Get the module parameters. */
2119191673Sjamie	mtx_unlock(&pr->pr_mtx);
2120191673Sjamie	locked = 0;
2121191673Sjamie	error = osd_jail_call(pr, PR_METHOD_GET, opts);
212246155Sphk	if (error)
2123191673Sjamie		goto done_deref;
2124191673Sjamie	prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
212584828Sjhb
2126191673Sjamie	/* By now, all parameters should have been noted. */
2127191673Sjamie	TAILQ_FOREACH(opt, opts, link) {
2128191673Sjamie		if (!opt->seen && strcmp(opt->name, "errmsg")) {
2129191673Sjamie			error = EINVAL;
2130191673Sjamie			vfs_opterror(opts, "unknown parameter: %s", opt->name);
2131191673Sjamie			goto done_errmsg;
2132191673Sjamie		}
2133185435Sbz	}
2134191673Sjamie
2135191673Sjamie	/* Write the fetched parameters back to userspace. */
2136191673Sjamie	error = 0;
2137191673Sjamie	TAILQ_FOREACH(opt, opts, link) {
2138191673Sjamie		if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2139191673Sjamie			pos = 2 * opt->pos + 1;
2140191673Sjamie			optuio->uio_iov[pos].iov_len = opt->len;
2141191673Sjamie			if (opt->value != NULL) {
2142191673Sjamie				if (optuio->uio_segflg == UIO_SYSSPACE) {
2143191673Sjamie					bcopy(opt->value,
2144191673Sjamie					    optuio->uio_iov[pos].iov_base,
2145191673Sjamie					    opt->len);
2146191673Sjamie				} else {
2147191673Sjamie					error = copyout(opt->value,
2148191673Sjamie					    optuio->uio_iov[pos].iov_base,
2149191673Sjamie					    opt->len);
2150191673Sjamie					if (error)
2151191673Sjamie						break;
2152191673Sjamie				}
2153191673Sjamie			}
2154191673Sjamie		}
2155185435Sbz	}
2156191673Sjamie	goto done_errmsg;
2157191673Sjamie
2158191673Sjamie done_deref:
2159191673Sjamie	prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2160191673Sjamie	goto done_errmsg;
2161191673Sjamie
2162191673Sjamie done_unlock_list:
2163191673Sjamie	sx_sunlock(&allprison_lock);
2164191673Sjamie done_errmsg:
2165191673Sjamie	if (error && errmsg_pos >= 0) {
2166191673Sjamie		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2167191673Sjamie		errmsg_pos = 2 * errmsg_pos + 1;
2168191673Sjamie		if (errmsg_len > 0) {
2169191673Sjamie			if (optuio->uio_segflg == UIO_SYSSPACE)
2170191673Sjamie				bcopy(errmsg,
2171191673Sjamie				    optuio->uio_iov[errmsg_pos].iov_base,
2172191673Sjamie				    errmsg_len);
2173191673Sjamie			else
2174191673Sjamie				copyout(errmsg,
2175191673Sjamie				    optuio->uio_iov[errmsg_pos].iov_base,
2176191673Sjamie				    errmsg_len);
2177191673Sjamie		}
2178185435Sbz	}
2179191673Sjamie	vfs_freeopts(opts);
2180191673Sjamie	return (error);
2181191673Sjamie}
2182113275Smike
2183192895Sjamie
2184191673Sjamie/*
2185191673Sjamie * struct jail_remove_args {
2186191673Sjamie *	int jid;
2187191673Sjamie * };
2188191673Sjamie */
2189191673Sjamieint
2190225617Skmacysys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2191191673Sjamie{
2192192895Sjamie	struct prison *pr, *cpr, *lpr, *tpr;
2193192895Sjamie	int descend, error;
2194185435Sbz
2195191673Sjamie	error = priv_check(td, PRIV_JAIL_REMOVE);
2196185435Sbz	if (error)
2197191673Sjamie		return (error);
2198185435Sbz
2199185435Sbz	sx_xlock(&allprison_lock);
2200192895Sjamie	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2201191673Sjamie	if (pr == NULL) {
2202185435Sbz		sx_xunlock(&allprison_lock);
2203191673Sjamie		return (EINVAL);
2204185435Sbz	}
2205185435Sbz
2206192895Sjamie	/* Remove all descendants of this prison, then remove this prison. */
2207192895Sjamie	pr->pr_ref++;
2208192895Sjamie	pr->pr_flags |= PR_REMOVE;
2209192895Sjamie	if (!LIST_EMPTY(&pr->pr_children)) {
2210192895Sjamie		mtx_unlock(&pr->pr_mtx);
2211192895Sjamie		lpr = NULL;
2212192895Sjamie		FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
2213192895Sjamie			mtx_lock(&cpr->pr_mtx);
2214192895Sjamie			if (cpr->pr_ref > 0) {
2215192895Sjamie				tpr = cpr;
2216192895Sjamie				cpr->pr_ref++;
2217192895Sjamie				cpr->pr_flags |= PR_REMOVE;
2218192895Sjamie			} else {
2219192895Sjamie				/* Already removed - do not do it again. */
2220192895Sjamie				tpr = NULL;
2221192895Sjamie			}
2222192895Sjamie			mtx_unlock(&cpr->pr_mtx);
2223192895Sjamie			if (lpr != NULL) {
2224192895Sjamie				mtx_lock(&lpr->pr_mtx);
2225192895Sjamie				prison_remove_one(lpr);
2226192895Sjamie				sx_xlock(&allprison_lock);
2227192895Sjamie			}
2228192895Sjamie			lpr = tpr;
2229192895Sjamie		}
2230192895Sjamie		if (lpr != NULL) {
2231192895Sjamie			mtx_lock(&lpr->pr_mtx);
2232192895Sjamie			prison_remove_one(lpr);
2233192895Sjamie			sx_xlock(&allprison_lock);
2234192895Sjamie		}
2235192895Sjamie		mtx_lock(&pr->pr_mtx);
2236192895Sjamie	}
2237192895Sjamie	prison_remove_one(pr);
2238192895Sjamie	return (0);
2239192895Sjamie}
2240192895Sjamie
2241192895Sjamiestatic void
2242192895Sjamieprison_remove_one(struct prison *pr)
2243192895Sjamie{
2244192895Sjamie	struct proc *p;
2245192895Sjamie	int deuref;
2246192895Sjamie
2247191673Sjamie	/* If the prison was persistent, it is not anymore. */
2248191673Sjamie	deuref = 0;
2249191673Sjamie	if (pr->pr_flags & PR_PERSIST) {
2250191673Sjamie		pr->pr_ref--;
2251191673Sjamie		deuref = PD_DEUREF;
2252191673Sjamie		pr->pr_flags &= ~PR_PERSIST;
2253179881Sdelphij	}
2254113275Smike
2255192895Sjamie	/*
2256192895Sjamie	 * jail_remove added a reference.  If that's the only one, remove
2257192895Sjamie	 * the prison now.
2258192895Sjamie	 */
2259192895Sjamie	KASSERT(pr->pr_ref > 0,
2260192895Sjamie	    ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
2261192895Sjamie	if (pr->pr_ref == 1) {
2262191673Sjamie		prison_deref(pr,
2263191673Sjamie		    deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
2264192895Sjamie		return;
2265191673Sjamie	}
2266191673Sjamie
2267113275Smike	mtx_unlock(&pr->pr_mtx);
2268191673Sjamie	sx_xunlock(&allprison_lock);
2269191673Sjamie	/*
2270191673Sjamie	 * Kill all processes unfortunate enough to be attached to this prison.
2271191673Sjamie	 */
2272191673Sjamie	sx_slock(&allproc_lock);
2273191673Sjamie	LIST_FOREACH(p, &allproc, p_list) {
2274191673Sjamie		PROC_LOCK(p);
2275191673Sjamie		if (p->p_state != PRS_NEW && p->p_ucred &&
2276191673Sjamie		    p->p_ucred->cr_prison == pr)
2277225617Skmacy			kern_psignal(p, SIGKILL);
2278191673Sjamie		PROC_UNLOCK(p);
2279191673Sjamie	}
2280191673Sjamie	sx_sunlock(&allproc_lock);
2281192895Sjamie	/* Remove the temporary reference added by jail_remove. */
2282191673Sjamie	prison_deref(pr, deuref | PD_DEREF);
2283113275Smike}
2284113275Smike
2285190466Sjamie
2286113275Smike/*
2287114168Smike * struct jail_attach_args {
2288114168Smike *	int jid;
2289114168Smike * };
2290113275Smike */
2291113275Smikeint
2292225617Skmacysys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2293113275Smike{
2294113275Smike	struct prison *pr;
2295191673Sjamie	int error;
2296167309Spjd
2297164032Srwatson	error = priv_check(td, PRIV_JAIL_ATTACH);
2298126023Snectar	if (error)
2299126023Snectar		return (error);
2300126023Snectar
2301168401Spjd	sx_slock(&allprison_lock);
2302192895Sjamie	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2303113275Smike	if (pr == NULL) {
2304168401Spjd		sx_sunlock(&allprison_lock);
2305113275Smike		return (EINVAL);
2306113275Smike	}
2307185435Sbz
2308185435Sbz	/*
2309185435Sbz	 * Do not allow a process to attach to a prison that is not
2310191673Sjamie	 * considered to be "alive".
2311185435Sbz	 */
2312191673Sjamie	if (pr->pr_uref == 0) {
2313185435Sbz		mtx_unlock(&pr->pr_mtx);
2314185435Sbz		sx_sunlock(&allprison_lock);
2315185435Sbz		return (EINVAL);
2316185435Sbz	}
2317191673Sjamie
2318191673Sjamie	return (do_jail_attach(td, pr));
2319191673Sjamie}
2320191673Sjamie
2321191673Sjamiestatic int
2322191673Sjamiedo_jail_attach(struct thread *td, struct prison *pr)
2323191673Sjamie{
2324192895Sjamie	struct prison *ppr;
2325191673Sjamie	struct proc *p;
2326191673Sjamie	struct ucred *newcred, *oldcred;
2327191673Sjamie	int vfslocked, error;
2328191673Sjamie
2329191673Sjamie	/*
2330191673Sjamie	 * XXX: Note that there is a slight race here if two threads
2331191673Sjamie	 * in the same privileged process attempt to attach to two
2332191673Sjamie	 * different jails at the same time.  It is important for
2333191673Sjamie	 * user processes not to do this, or they might end up with
2334191673Sjamie	 * a process root from one prison, but attached to the jail
2335191673Sjamie	 * of another.
2336191673Sjamie	 */
2337113275Smike	pr->pr_ref++;
2338191673Sjamie	pr->pr_uref++;
2339113275Smike	mtx_unlock(&pr->pr_mtx);
2340191673Sjamie
2341191673Sjamie	/* Let modules do whatever they need to prepare for attaching. */
2342191673Sjamie	error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2343191673Sjamie	if (error) {
2344191673Sjamie		prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2345191673Sjamie		return (error);
2346191673Sjamie	}
2347168401Spjd	sx_sunlock(&allprison_lock);
2348113275Smike
2349185435Sbz	/*
2350185435Sbz	 * Reparent the newly attached process to this jail.
2351185435Sbz	 */
2352192895Sjamie	ppr = td->td_ucred->cr_prison;
2353191673Sjamie	p = td->td_proc;
2354185435Sbz	error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2355185435Sbz	if (error)
2356191673Sjamie		goto e_revert_osd;
2357185435Sbz
2358150652Scsjp	vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2359175202Sattilio	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2360113275Smike	if ((error = change_dir(pr->pr_root, td)) != 0)
2361113275Smike		goto e_unlock;
2362113275Smike#ifdef MAC
2363172930Srwatson	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2364113275Smike		goto e_unlock;
2365113275Smike#endif
2366175294Sattilio	VOP_UNLOCK(pr->pr_root, 0);
2367191673Sjamie	if ((error = change_root(pr->pr_root, td)))
2368191673Sjamie		goto e_unlock_giant;
2369150652Scsjp	VFS_UNLOCK_GIANT(vfslocked);
2370113275Smike
237184828Sjhb	newcred = crget();
237284828Sjhb	PROC_LOCK(p);
237384828Sjhb	oldcred = p->p_ucred;
2374113275Smike	setsugid(p);
237584828Sjhb	crcopy(newcred, oldcred);
2376113630Sjhb	newcred->cr_prison = pr;
237784828Sjhb	p->p_ucred = newcred;
237884828Sjhb	PROC_UNLOCK(p);
2379220137Strasz#ifdef RACCT
2380220137Strasz	racct_proc_ucred_changed(p, oldcred, newcred);
2381220137Strasz#endif
238284828Sjhb	crfree(oldcred);
2383192895Sjamie	prison_deref(ppr, PD_DEREF | PD_DEUREF);
238446155Sphk	return (0);
2385191673Sjamie e_unlock:
2386175294Sattilio	VOP_UNLOCK(pr->pr_root, 0);
2387191673Sjamie e_unlock_giant:
2388150652Scsjp	VFS_UNLOCK_GIANT(vfslocked);
2389191673Sjamie e_revert_osd:
2390191673Sjamie	/* Tell modules this thread is still in its old jail after all. */
2391192895Sjamie	(void)osd_jail_call(ppr, PR_METHOD_ATTACH, td);
2392191673Sjamie	prison_deref(pr, PD_DEREF | PD_DEUREF);
239346155Sphk	return (error);
239446155Sphk}
239546155Sphk
2396192895Sjamie
2397113275Smike/*
2398113275Smike * Returns a locked prison instance, or NULL on failure.
2399113275Smike */
2400168399Spjdstruct prison *
2401113275Smikeprison_find(int prid)
2402113275Smike{
2403113275Smike	struct prison *pr;
2404113275Smike
2405168401Spjd	sx_assert(&allprison_lock, SX_LOCKED);
2406191673Sjamie	TAILQ_FOREACH(pr, &allprison, pr_list) {
2407113275Smike		if (pr->pr_id == prid) {
2408113275Smike			mtx_lock(&pr->pr_mtx);
2409191673Sjamie			if (pr->pr_ref > 0)
2410191673Sjamie				return (pr);
2411191673Sjamie			mtx_unlock(&pr->pr_mtx);
2412113275Smike		}
2413113275Smike	}
2414113275Smike	return (NULL);
2415113275Smike}
2416113275Smike
2417191673Sjamie/*
2418192895Sjamie * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
2419191673Sjamie */
2420191673Sjamiestruct prison *
2421192895Sjamieprison_find_child(struct prison *mypr, int prid)
2422191673Sjamie{
2423192895Sjamie	struct prison *pr;
2424192895Sjamie	int descend;
2425192895Sjamie
2426192895Sjamie	sx_assert(&allprison_lock, SX_LOCKED);
2427192895Sjamie	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2428192895Sjamie		if (pr->pr_id == prid) {
2429192895Sjamie			mtx_lock(&pr->pr_mtx);
2430192895Sjamie			if (pr->pr_ref > 0)
2431192895Sjamie				return (pr);
2432192895Sjamie			mtx_unlock(&pr->pr_mtx);
2433192895Sjamie		}
2434192895Sjamie	}
2435192895Sjamie	return (NULL);
2436192895Sjamie}
2437192895Sjamie
2438192895Sjamie/*
2439192895Sjamie * Look for the name relative to mypr.  Returns a locked prison or NULL.
2440192895Sjamie */
2441192895Sjamiestruct prison *
2442192895Sjamieprison_find_name(struct prison *mypr, const char *name)
2443192895Sjamie{
2444191673Sjamie	struct prison *pr, *deadpr;
2445192895Sjamie	size_t mylen;
2446192895Sjamie	int descend;
2447191673Sjamie
2448191673Sjamie	sx_assert(&allprison_lock, SX_LOCKED);
2449192895Sjamie	mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2450191673Sjamie again:
2451191673Sjamie	deadpr = NULL;
2452192895Sjamie	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2453192895Sjamie		if (!strcmp(pr->pr_name + mylen, name)) {
2454191673Sjamie			mtx_lock(&pr->pr_mtx);
2455191673Sjamie			if (pr->pr_ref > 0) {
2456191673Sjamie				if (pr->pr_uref > 0)
2457191673Sjamie					return (pr);
2458191673Sjamie				deadpr = pr;
2459191673Sjamie			}
2460191673Sjamie			mtx_unlock(&pr->pr_mtx);
2461191673Sjamie		}
2462191673Sjamie	}
2463192895Sjamie	/* There was no valid prison - perhaps there was a dying one. */
2464191673Sjamie	if (deadpr != NULL) {
2465191673Sjamie		mtx_lock(&deadpr->pr_mtx);
2466191673Sjamie		if (deadpr->pr_ref == 0) {
2467191673Sjamie			mtx_unlock(&deadpr->pr_mtx);
2468191673Sjamie			goto again;
2469191673Sjamie		}
2470191673Sjamie	}
2471191673Sjamie	return (deadpr);
2472191673Sjamie}
2473191673Sjamie
2474191673Sjamie/*
2475192895Sjamie * See if a prison has the specific flag set.
2476192895Sjamie */
2477192895Sjamieint
2478192895Sjamieprison_flag(struct ucred *cred, unsigned flag)
2479192895Sjamie{
2480192895Sjamie
2481192895Sjamie	/* This is an atomic read, so no locking is necessary. */
2482192895Sjamie	return (cred->cr_prison->pr_flags & flag);
2483192895Sjamie}
2484192895Sjamie
2485192895Sjamieint
2486192895Sjamieprison_allow(struct ucred *cred, unsigned flag)
2487192895Sjamie{
2488192895Sjamie
2489192895Sjamie	/* This is an atomic read, so no locking is necessary. */
2490192895Sjamie	return (cred->cr_prison->pr_allow & flag);
2491192895Sjamie}
2492192895Sjamie
2493192895Sjamie/*
2494191673Sjamie * Remove a prison reference.  If that was the last reference, remove the
2495191673Sjamie * prison itself - but not in this context in case there are locks held.
2496191673Sjamie */
249772786Srwatsonvoid
2498185029Spjdprison_free_locked(struct prison *pr)
249972786Srwatson{
250072786Srwatson
2501185029Spjd	mtx_assert(&pr->pr_mtx, MA_OWNED);
250272786Srwatson	pr->pr_ref--;
250372786Srwatson	if (pr->pr_ref == 0) {
2504168483Spjd		mtx_unlock(&pr->pr_mtx);
2505124882Srwatson		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
2506144660Sjeff		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
250787275Srwatson		return;
250872786Srwatson	}
250987275Srwatson	mtx_unlock(&pr->pr_mtx);
251072786Srwatson}
251172786Srwatson
2512185029Spjdvoid
2513185029Spjdprison_free(struct prison *pr)
2514185029Spjd{
2515185029Spjd
2516185029Spjd	mtx_lock(&pr->pr_mtx);
2517185029Spjd	prison_free_locked(pr);
2518185029Spjd}
2519185029Spjd
2520124882Srwatsonstatic void
2521124882Srwatsonprison_complete(void *context, int pending)
2522124882Srwatson{
2523191673Sjamie
2524191673Sjamie	prison_deref((struct prison *)context, 0);
2525191673Sjamie}
2526191673Sjamie
2527191673Sjamie/*
2528191673Sjamie * Remove a prison reference (usually).  This internal version assumes no
2529191673Sjamie * mutexes are held, except perhaps the prison itself.  If there are no more
2530191673Sjamie * references, release and delist the prison.  On completion, the prison lock
2531191673Sjamie * and the allprison lock are both unlocked.
2532191673Sjamie */
2533191673Sjamiestatic void
2534191673Sjamieprison_deref(struct prison *pr, int flags)
2535191673Sjamie{
2536192895Sjamie	struct prison *ppr, *tpr;
2537150652Scsjp	int vfslocked;
2538124882Srwatson
2539191673Sjamie	if (!(flags & PD_LOCKED))
2540191673Sjamie		mtx_lock(&pr->pr_mtx);
2541225191Sjamie	for (;;) {
2542225191Sjamie		if (flags & PD_DEUREF) {
2543225191Sjamie			pr->pr_uref--;
2544225191Sjamie			KASSERT(prison0.pr_uref != 0, ("prison0 pr_uref=0"));
2545192895Sjamie		}
2546192895Sjamie		if (flags & PD_DEREF)
2547192895Sjamie			pr->pr_ref--;
2548192895Sjamie		/* If the prison still has references, nothing else to do. */
2549192895Sjamie		if (pr->pr_ref > 0) {
2550192895Sjamie			mtx_unlock(&pr->pr_mtx);
2551192895Sjamie			if (flags & PD_LIST_SLOCKED)
2552192895Sjamie				sx_sunlock(&allprison_lock);
2553192895Sjamie			else if (flags & PD_LIST_XLOCKED)
2554192895Sjamie				sx_xunlock(&allprison_lock);
2555192895Sjamie			return;
2556191673Sjamie		}
2557191673Sjamie
2558192895Sjamie		mtx_unlock(&pr->pr_mtx);
2559192895Sjamie		if (flags & PD_LIST_SLOCKED) {
2560192895Sjamie			if (!sx_try_upgrade(&allprison_lock)) {
2561192895Sjamie				sx_sunlock(&allprison_lock);
2562192895Sjamie				sx_xlock(&allprison_lock);
2563192895Sjamie			}
2564192895Sjamie		} else if (!(flags & PD_LIST_XLOCKED))
2565192895Sjamie			sx_xlock(&allprison_lock);
2566168489Spjd
2567192895Sjamie		TAILQ_REMOVE(&allprison, pr, pr_list);
2568192895Sjamie		LIST_REMOVE(pr, pr_sibling);
2569192895Sjamie		ppr = pr->pr_parent;
2570192895Sjamie		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2571194762Sjamie			tpr->pr_childcount--;
2572196592Sjamie		sx_xunlock(&allprison_lock);
2573192895Sjamie
2574194251Sjamie#ifdef VIMAGE
2575196505Szec		if (pr->pr_vnet != ppr->pr_vnet)
2576194251Sjamie			vnet_destroy(pr->pr_vnet);
2577194251Sjamie#endif
2578192895Sjamie		if (pr->pr_root != NULL) {
2579192895Sjamie			vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2580192895Sjamie			vrele(pr->pr_root);
2581192895Sjamie			VFS_UNLOCK_GIANT(vfslocked);
2582192895Sjamie		}
2583192895Sjamie		mtx_destroy(&pr->pr_mtx);
2584191673Sjamie#ifdef INET
2585192895Sjamie		free(pr->pr_ip4, M_PRISON);
2586191673Sjamie#endif
2587185435Sbz#ifdef INET6
2588192895Sjamie		free(pr->pr_ip6, M_PRISON);
2589185435Sbz#endif
2590192895Sjamie		if (pr->pr_cpuset != NULL)
2591192895Sjamie			cpuset_rel(pr->pr_cpuset);
2592192895Sjamie		osd_jail_exit(pr);
2593221362Strasz#ifdef RACCT
2594221362Strasz		prison_racct_detach(pr);
2595220163Strasz#endif
2596192895Sjamie		free(pr, M_PRISON);
2597192895Sjamie
2598192895Sjamie		/* Removing a prison frees a reference on its parent. */
2599192895Sjamie		pr = ppr;
2600192895Sjamie		mtx_lock(&pr->pr_mtx);
2601225191Sjamie		flags = PD_DEREF | PD_DEUREF;
2602192895Sjamie	}
2603124882Srwatson}
2604124882Srwatson
260572786Srwatsonvoid
2606185029Spjdprison_hold_locked(struct prison *pr)
260772786Srwatson{
260872786Srwatson
2609185029Spjd	mtx_assert(&pr->pr_mtx, MA_OWNED);
2610168489Spjd	KASSERT(pr->pr_ref > 0,
2611191671Sjamie	    ("Trying to hold dead prison (jid=%d).", pr->pr_id));
261272786Srwatson	pr->pr_ref++;
2613185029Spjd}
2614185029Spjd
2615185029Spjdvoid
2616185029Spjdprison_hold(struct prison *pr)
2617185029Spjd{
2618185029Spjd
2619185029Spjd	mtx_lock(&pr->pr_mtx);
2620185029Spjd	prison_hold_locked(pr);
262187275Srwatson	mtx_unlock(&pr->pr_mtx);
262272786Srwatson}
262372786Srwatson
2624185435Sbzvoid
2625185435Sbzprison_proc_hold(struct prison *pr)
262687275Srwatson{
262787275Srwatson
2628185435Sbz	mtx_lock(&pr->pr_mtx);
2629191673Sjamie	KASSERT(pr->pr_uref > 0,
2630191673Sjamie	    ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2631191673Sjamie	pr->pr_uref++;
2632185435Sbz	mtx_unlock(&pr->pr_mtx);
263387275Srwatson}
263487275Srwatson
2635185435Sbzvoid
2636185435Sbzprison_proc_free(struct prison *pr)
2637185435Sbz{
2638185435Sbz
2639185435Sbz	mtx_lock(&pr->pr_mtx);
2640191673Sjamie	KASSERT(pr->pr_uref > 0,
2641191673Sjamie	    ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2642191673Sjamie	prison_deref(pr, PD_DEUREF | PD_LOCKED);
2643185435Sbz}
2644185435Sbz
2645185435Sbz
2646185435Sbz#ifdef INET
2647185435Sbz/*
2648192895Sjamie * Restrict a prison's IP address list with its parent's, possibly replacing
2649192895Sjamie * it.  Return true if the replacement buffer was used (or would have been).
2650192895Sjamie */
2651192895Sjamiestatic int
2652192895Sjamieprison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
2653192895Sjamie{
2654192895Sjamie	int ii, ij, used;
2655192895Sjamie	struct prison *ppr;
2656192895Sjamie
2657192895Sjamie	ppr = pr->pr_parent;
2658192895Sjamie	if (!(pr->pr_flags & PR_IP4_USER)) {
2659192895Sjamie		/* This has no user settings, so just copy the parent's list. */
2660192895Sjamie		if (pr->pr_ip4s < ppr->pr_ip4s) {
2661192895Sjamie			/*
2662192895Sjamie			 * There's no room for the parent's list.  Use the
2663192895Sjamie			 * new list buffer, which is assumed to be big enough
2664192895Sjamie			 * (if it was passed).  If there's no buffer, try to
2665192895Sjamie			 * allocate one.
2666192895Sjamie			 */
2667192895Sjamie			used = 1;
2668192895Sjamie			if (newip4 == NULL) {
2669192895Sjamie				newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
2670192895Sjamie				    M_PRISON, M_NOWAIT);
2671192895Sjamie				if (newip4 != NULL)
2672192895Sjamie					used = 0;
2673192895Sjamie			}
2674192895Sjamie			if (newip4 != NULL) {
2675192895Sjamie				bcopy(ppr->pr_ip4, newip4,
2676192895Sjamie				    ppr->pr_ip4s * sizeof(*newip4));
2677192895Sjamie				free(pr->pr_ip4, M_PRISON);
2678192895Sjamie				pr->pr_ip4 = newip4;
2679192895Sjamie				pr->pr_ip4s = ppr->pr_ip4s;
2680192895Sjamie			}
2681192895Sjamie			return (used);
2682192895Sjamie		}
2683192895Sjamie		pr->pr_ip4s = ppr->pr_ip4s;
2684192895Sjamie		if (pr->pr_ip4s > 0)
2685192895Sjamie			bcopy(ppr->pr_ip4, pr->pr_ip4,
2686192895Sjamie			    pr->pr_ip4s * sizeof(*newip4));
2687192895Sjamie		else if (pr->pr_ip4 != NULL) {
2688192895Sjamie			free(pr->pr_ip4, M_PRISON);
2689192895Sjamie			pr->pr_ip4 = NULL;
2690192895Sjamie		}
2691195974Sjamie	} else if (pr->pr_ip4s > 0) {
2692192895Sjamie		/* Remove addresses that aren't in the parent. */
2693192895Sjamie		for (ij = 0; ij < ppr->pr_ip4s; ij++)
2694192895Sjamie			if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
2695192895Sjamie				break;
2696192895Sjamie		if (ij < ppr->pr_ip4s)
2697192895Sjamie			ii = 1;
2698192895Sjamie		else {
2699192895Sjamie			bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
2700192895Sjamie			    --pr->pr_ip4s * sizeof(*pr->pr_ip4));
2701192895Sjamie			ii = 0;
2702192895Sjamie		}
2703192895Sjamie		for (ij = 1; ii < pr->pr_ip4s; ) {
2704192895Sjamie			if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
2705192895Sjamie				ii++;
2706192895Sjamie				continue;
2707192895Sjamie			}
2708192895Sjamie			switch (ij >= ppr->pr_ip4s ? -1 :
2709192895Sjamie				qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
2710192895Sjamie			case -1:
2711192895Sjamie				bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
2712192895Sjamie				    (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
2713192895Sjamie				break;
2714192895Sjamie			case 0:
2715192895Sjamie				ii++;
2716192895Sjamie				ij++;
2717192895Sjamie				break;
2718192895Sjamie			case 1:
2719192895Sjamie				ij++;
2720192895Sjamie				break;
2721192895Sjamie			}
2722192895Sjamie		}
2723192895Sjamie		if (pr->pr_ip4s == 0) {
2724195870Sjamie			pr->pr_flags |= PR_IP4_DISABLE;
2725192895Sjamie			free(pr->pr_ip4, M_PRISON);
2726192895Sjamie			pr->pr_ip4 = NULL;
2727192895Sjamie		}
2728192895Sjamie	}
2729192895Sjamie	return (0);
2730192895Sjamie}
2731192895Sjamie
2732192895Sjamie/*
2733185435Sbz * Pass back primary IPv4 address of this jail.
2734185435Sbz *
2735192895Sjamie * If not restricted return success but do not alter the address.  Caller has
2736192895Sjamie * to make sure to initialize it correctly (e.g. INADDR_ANY).
2737185435Sbz *
2738188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2739188144Sjamie * Address returned in NBO.
2740185435Sbz */
274146155Sphkint
2742187684Sbzprison_get_ip4(struct ucred *cred, struct in_addr *ia)
274346155Sphk{
2744191673Sjamie	struct prison *pr;
274546155Sphk
2746185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2747185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2748185435Sbz
2749192895Sjamie	pr = cred->cr_prison;
2750192895Sjamie	if (!(pr->pr_flags & PR_IP4))
275146155Sphk		return (0);
2752191673Sjamie	mtx_lock(&pr->pr_mtx);
2753192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2754192895Sjamie		mtx_unlock(&pr->pr_mtx);
2755192895Sjamie		return (0);
2756192895Sjamie	}
2757191673Sjamie	if (pr->pr_ip4 == NULL) {
2758191673Sjamie		mtx_unlock(&pr->pr_mtx);
2759188144Sjamie		return (EAFNOSUPPORT);
2760191673Sjamie	}
2761185435Sbz
2762191673Sjamie	ia->s_addr = pr->pr_ip4[0].s_addr;
2763191673Sjamie	mtx_unlock(&pr->pr_mtx);
2764185435Sbz	return (0);
2765185435Sbz}
2766185435Sbz
2767185435Sbz/*
2768202468Sbz * Return 1 if we should do proper source address selection or are not jailed.
2769202468Sbz * We will return 0 if we should bypass source address selection in favour
2770202468Sbz * of the primary jail IPv4 address. Only in this case *ia will be updated and
2771202468Sbz * returned in NBO.
2772202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv4.
2773202468Sbz */
2774202468Sbzint
2775202468Sbzprison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia)
2776202468Sbz{
2777202468Sbz	struct prison *pr;
2778202468Sbz	struct in_addr lia;
2779202468Sbz	int error;
2780202468Sbz
2781202468Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2782202468Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2783202468Sbz
2784202468Sbz	if (!jailed(cred))
2785202468Sbz		return (1);
2786202468Sbz
2787202468Sbz	pr = cred->cr_prison;
2788202468Sbz	if (pr->pr_flags & PR_IP4_SADDRSEL)
2789202468Sbz		return (1);
2790202468Sbz
2791202468Sbz	lia.s_addr = INADDR_ANY;
2792202468Sbz	error = prison_get_ip4(cred, &lia);
2793202468Sbz	if (error)
2794202468Sbz		return (error);
2795202468Sbz	if (lia.s_addr == INADDR_ANY)
2796202468Sbz		return (1);
2797202468Sbz
2798202468Sbz	ia->s_addr = lia.s_addr;
2799202468Sbz	return (0);
2800202468Sbz}
2801202468Sbz
2802202468Sbz/*
2803192895Sjamie * Return true if pr1 and pr2 have the same IPv4 address restrictions.
2804192895Sjamie */
2805192895Sjamieint
2806192895Sjamieprison_equal_ip4(struct prison *pr1, struct prison *pr2)
2807192895Sjamie{
2808192895Sjamie
2809192895Sjamie	if (pr1 == pr2)
2810192895Sjamie		return (1);
2811192895Sjamie
2812192895Sjamie	/*
2813195974Sjamie	 * No need to lock since the PR_IP4_USER flag can't be altered for
2814195974Sjamie	 * existing prisons.
2815192895Sjamie	 */
2816195945Sjamie	while (pr1 != &prison0 &&
2817195945Sjamie#ifdef VIMAGE
2818195945Sjamie	       !(pr1->pr_flags & PR_VNET) &&
2819195945Sjamie#endif
2820195945Sjamie	       !(pr1->pr_flags & PR_IP4_USER))
2821192895Sjamie		pr1 = pr1->pr_parent;
2822195945Sjamie	while (pr2 != &prison0 &&
2823195945Sjamie#ifdef VIMAGE
2824195945Sjamie	       !(pr2->pr_flags & PR_VNET) &&
2825195945Sjamie#endif
2826195945Sjamie	       !(pr2->pr_flags & PR_IP4_USER))
2827192895Sjamie		pr2 = pr2->pr_parent;
2828192895Sjamie	return (pr1 == pr2);
2829192895Sjamie}
2830192895Sjamie
2831192895Sjamie/*
2832185435Sbz * Make sure our (source) address is set to something meaningful to this
2833185435Sbz * jail.
2834185435Sbz *
2835192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2836192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2837192895Sjamie * doesn't allow IPv4.  Address passed in in NBO and returned in NBO.
2838185435Sbz */
2839185435Sbzint
2840185435Sbzprison_local_ip4(struct ucred *cred, struct in_addr *ia)
2841185435Sbz{
2842191673Sjamie	struct prison *pr;
2843185435Sbz	struct in_addr ia0;
2844191673Sjamie	int error;
2845185435Sbz
2846185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2847185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2848185435Sbz
2849192895Sjamie	pr = cred->cr_prison;
2850192895Sjamie	if (!(pr->pr_flags & PR_IP4))
285146155Sphk		return (0);
2852191673Sjamie	mtx_lock(&pr->pr_mtx);
2853192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2854192895Sjamie		mtx_unlock(&pr->pr_mtx);
2855192895Sjamie		return (0);
2856192895Sjamie	}
2857191673Sjamie	if (pr->pr_ip4 == NULL) {
2858191673Sjamie		mtx_unlock(&pr->pr_mtx);
2859188144Sjamie		return (EAFNOSUPPORT);
2860191673Sjamie	}
2861185435Sbz
2862185435Sbz	ia0.s_addr = ntohl(ia->s_addr);
2863185435Sbz	if (ia0.s_addr == INADDR_LOOPBACK) {
2864191673Sjamie		ia->s_addr = pr->pr_ip4[0].s_addr;
2865191673Sjamie		mtx_unlock(&pr->pr_mtx);
2866185435Sbz		return (0);
286746155Sphk	}
2868185435Sbz
2869188144Sjamie	if (ia0.s_addr == INADDR_ANY) {
2870188144Sjamie		/*
2871188144Sjamie		 * In case there is only 1 IPv4 address, bind directly.
2872188144Sjamie		 */
2873191673Sjamie		if (pr->pr_ip4s == 1)
2874191673Sjamie			ia->s_addr = pr->pr_ip4[0].s_addr;
2875191673Sjamie		mtx_unlock(&pr->pr_mtx);
2876185435Sbz		return (0);
2877185435Sbz	}
2878185435Sbz
2879191673Sjamie	error = _prison_check_ip4(pr, ia);
2880191673Sjamie	mtx_unlock(&pr->pr_mtx);
2881191673Sjamie	return (error);
2882185435Sbz}
2883185435Sbz
2884185435Sbz/*
2885185435Sbz * Rewrite destination address in case we will connect to loopback address.
2886185435Sbz *
2887188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2888188144Sjamie * Address passed in in NBO and returned in NBO.
2889185435Sbz */
2890185435Sbzint
2891185435Sbzprison_remote_ip4(struct ucred *cred, struct in_addr *ia)
2892185435Sbz{
2893191673Sjamie	struct prison *pr;
2894185435Sbz
2895185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2896185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2897185435Sbz
2898192895Sjamie	pr = cred->cr_prison;
2899192895Sjamie	if (!(pr->pr_flags & PR_IP4))
2900185435Sbz		return (0);
2901191673Sjamie	mtx_lock(&pr->pr_mtx);
2902192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2903192895Sjamie		mtx_unlock(&pr->pr_mtx);
2904192895Sjamie		return (0);
2905192895Sjamie	}
2906191673Sjamie	if (pr->pr_ip4 == NULL) {
2907191673Sjamie		mtx_unlock(&pr->pr_mtx);
2908188144Sjamie		return (EAFNOSUPPORT);
2909191673Sjamie	}
2910188144Sjamie
2911185435Sbz	if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
2912191673Sjamie		ia->s_addr = pr->pr_ip4[0].s_addr;
2913191673Sjamie		mtx_unlock(&pr->pr_mtx);
2914185435Sbz		return (0);
2915185435Sbz	}
2916185435Sbz
2917185435Sbz	/*
2918185435Sbz	 * Return success because nothing had to be changed.
2919185435Sbz	 */
2920191673Sjamie	mtx_unlock(&pr->pr_mtx);
2921185435Sbz	return (0);
2922185435Sbz}
2923185435Sbz
2924185435Sbz/*
2925188144Sjamie * Check if given address belongs to the jail referenced by cred/prison.
2926185435Sbz *
2927192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2928192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2929192895Sjamie * doesn't allow IPv4.  Address passed in in NBO.
2930185435Sbz */
2931185435Sbzstatic int
2932185435Sbz_prison_check_ip4(struct prison *pr, struct in_addr *ia)
2933185435Sbz{
2934185435Sbz	int i, a, z, d;
2935185435Sbz
2936185435Sbz	/*
2937185435Sbz	 * Check the primary IP.
2938185435Sbz	 */
2939185435Sbz	if (pr->pr_ip4[0].s_addr == ia->s_addr)
2940188144Sjamie		return (0);
2941185435Sbz
2942185435Sbz	/*
2943185435Sbz	 * All the other IPs are sorted so we can do a binary search.
2944185435Sbz	 */
2945185435Sbz	a = 0;
2946185435Sbz	z = pr->pr_ip4s - 2;
2947185435Sbz	while (a <= z) {
2948185435Sbz		i = (a + z) / 2;
2949185435Sbz		d = qcmp_v4(&pr->pr_ip4[i+1], ia);
2950185435Sbz		if (d > 0)
2951185435Sbz			z = i - 1;
2952185435Sbz		else if (d < 0)
2953185435Sbz			a = i + 1;
295481114Srwatson		else
2955188144Sjamie			return (0);
2956185435Sbz	}
2957188144Sjamie
2958188144Sjamie	return (EADDRNOTAVAIL);
2959185435Sbz}
2960185435Sbz
2961185435Sbzint
2962185435Sbzprison_check_ip4(struct ucred *cred, struct in_addr *ia)
2963185435Sbz{
2964191673Sjamie	struct prison *pr;
2965191673Sjamie	int error;
2966185435Sbz
2967185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2968185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2969185435Sbz
2970192895Sjamie	pr = cred->cr_prison;
2971192895Sjamie	if (!(pr->pr_flags & PR_IP4))
2972188144Sjamie		return (0);
2973191673Sjamie	mtx_lock(&pr->pr_mtx);
2974192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2975192895Sjamie		mtx_unlock(&pr->pr_mtx);
2976192895Sjamie		return (0);
2977192895Sjamie	}
2978191673Sjamie	if (pr->pr_ip4 == NULL) {
2979191673Sjamie		mtx_unlock(&pr->pr_mtx);
2980188144Sjamie		return (EAFNOSUPPORT);
2981191673Sjamie	}
2982185435Sbz
2983191673Sjamie	error = _prison_check_ip4(pr, ia);
2984191673Sjamie	mtx_unlock(&pr->pr_mtx);
2985191673Sjamie	return (error);
2986185435Sbz}
2987185435Sbz#endif
2988185435Sbz
2989185435Sbz#ifdef INET6
2990192895Sjamiestatic int
2991192895Sjamieprison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
2992192895Sjamie{
2993192895Sjamie	int ii, ij, used;
2994192895Sjamie	struct prison *ppr;
2995192895Sjamie
2996192895Sjamie	ppr = pr->pr_parent;
2997192895Sjamie	if (!(pr->pr_flags & PR_IP6_USER)) {
2998192895Sjamie		/* This has no user settings, so just copy the parent's list. */
2999192895Sjamie		if (pr->pr_ip6s < ppr->pr_ip6s) {
3000192895Sjamie			/*
3001192895Sjamie			 * There's no room for the parent's list.  Use the
3002192895Sjamie			 * new list buffer, which is assumed to be big enough
3003192895Sjamie			 * (if it was passed).  If there's no buffer, try to
3004192895Sjamie			 * allocate one.
3005192895Sjamie			 */
3006192895Sjamie			used = 1;
3007192895Sjamie			if (newip6 == NULL) {
3008192895Sjamie				newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
3009192895Sjamie				    M_PRISON, M_NOWAIT);
3010192895Sjamie				if (newip6 != NULL)
3011192895Sjamie					used = 0;
3012192895Sjamie			}
3013192895Sjamie			if (newip6 != NULL) {
3014192895Sjamie				bcopy(ppr->pr_ip6, newip6,
3015192895Sjamie				    ppr->pr_ip6s * sizeof(*newip6));
3016192895Sjamie				free(pr->pr_ip6, M_PRISON);
3017192895Sjamie				pr->pr_ip6 = newip6;
3018192895Sjamie				pr->pr_ip6s = ppr->pr_ip6s;
3019192895Sjamie			}
3020192895Sjamie			return (used);
3021192895Sjamie		}
3022192895Sjamie		pr->pr_ip6s = ppr->pr_ip6s;
3023192895Sjamie		if (pr->pr_ip6s > 0)
3024192895Sjamie			bcopy(ppr->pr_ip6, pr->pr_ip6,
3025192895Sjamie			    pr->pr_ip6s * sizeof(*newip6));
3026192895Sjamie		else if (pr->pr_ip6 != NULL) {
3027192895Sjamie			free(pr->pr_ip6, M_PRISON);
3028192895Sjamie			pr->pr_ip6 = NULL;
3029192895Sjamie		}
3030195974Sjamie	} else if (pr->pr_ip6s > 0) {
3031192895Sjamie		/* Remove addresses that aren't in the parent. */
3032192895Sjamie		for (ij = 0; ij < ppr->pr_ip6s; ij++)
3033192895Sjamie			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
3034192895Sjamie			    &ppr->pr_ip6[ij]))
3035192895Sjamie				break;
3036192895Sjamie		if (ij < ppr->pr_ip6s)
3037192895Sjamie			ii = 1;
3038192895Sjamie		else {
3039192895Sjamie			bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
3040192895Sjamie			    --pr->pr_ip6s * sizeof(*pr->pr_ip6));
3041192895Sjamie			ii = 0;
3042192895Sjamie		}
3043192895Sjamie		for (ij = 1; ii < pr->pr_ip6s; ) {
3044192895Sjamie			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
3045192895Sjamie			    &ppr->pr_ip6[0])) {
3046192895Sjamie				ii++;
3047192895Sjamie				continue;
3048192895Sjamie			}
3049192895Sjamie			switch (ij >= ppr->pr_ip4s ? -1 :
3050192895Sjamie				qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
3051192895Sjamie			case -1:
3052192895Sjamie				bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
3053192895Sjamie				    (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
3054192895Sjamie				break;
3055192895Sjamie			case 0:
3056192895Sjamie				ii++;
3057192895Sjamie				ij++;
3058192895Sjamie				break;
3059192895Sjamie			case 1:
3060192895Sjamie				ij++;
3061192895Sjamie				break;
3062192895Sjamie			}
3063192895Sjamie		}
3064192895Sjamie		if (pr->pr_ip6s == 0) {
3065195870Sjamie			pr->pr_flags |= PR_IP6_DISABLE;
3066192895Sjamie			free(pr->pr_ip6, M_PRISON);
3067192895Sjamie			pr->pr_ip6 = NULL;
3068192895Sjamie		}
3069192895Sjamie	}
3070192895Sjamie	return 0;
3071192895Sjamie}
3072192895Sjamie
3073185435Sbz/*
3074185435Sbz * Pass back primary IPv6 address for this jail.
3075185435Sbz *
3076192895Sjamie * If not restricted return success but do not alter the address.  Caller has
3077192895Sjamie * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
3078185435Sbz *
3079188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3080185435Sbz */
3081185435Sbzint
3082187684Sbzprison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
3083185435Sbz{
3084191673Sjamie	struct prison *pr;
3085185435Sbz
3086185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3087185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3088185435Sbz
3089192895Sjamie	pr = cred->cr_prison;
3090192895Sjamie	if (!(pr->pr_flags & PR_IP6))
309181114Srwatson		return (0);
3092191673Sjamie	mtx_lock(&pr->pr_mtx);
3093192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3094192895Sjamie		mtx_unlock(&pr->pr_mtx);
3095192895Sjamie		return (0);
3096192895Sjamie	}
3097191673Sjamie	if (pr->pr_ip6 == NULL) {
3098191673Sjamie		mtx_unlock(&pr->pr_mtx);
3099188144Sjamie		return (EAFNOSUPPORT);
3100191673Sjamie	}
3101188144Sjamie
3102191673Sjamie	bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3103191673Sjamie	mtx_unlock(&pr->pr_mtx);
3104185435Sbz	return (0);
3105185435Sbz}
3106185435Sbz
3107185435Sbz/*
3108202468Sbz * Return 1 if we should do proper source address selection or are not jailed.
3109202468Sbz * We will return 0 if we should bypass source address selection in favour
3110202468Sbz * of the primary jail IPv6 address. Only in this case *ia will be updated and
3111202468Sbz * returned in NBO.
3112202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv6.
3113202468Sbz */
3114202468Sbzint
3115202468Sbzprison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6)
3116202468Sbz{
3117202468Sbz	struct prison *pr;
3118202468Sbz	struct in6_addr lia6;
3119202468Sbz	int error;
3120202468Sbz
3121202468Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3122202468Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3123202468Sbz
3124202468Sbz	if (!jailed(cred))
3125202468Sbz		return (1);
3126202468Sbz
3127202468Sbz	pr = cred->cr_prison;
3128202468Sbz	if (pr->pr_flags & PR_IP6_SADDRSEL)
3129202468Sbz		return (1);
3130202468Sbz
3131202468Sbz	lia6 = in6addr_any;
3132202468Sbz	error = prison_get_ip6(cred, &lia6);
3133202468Sbz	if (error)
3134202468Sbz		return (error);
3135202468Sbz	if (IN6_IS_ADDR_UNSPECIFIED(&lia6))
3136202468Sbz		return (1);
3137202468Sbz
3138202468Sbz	bcopy(&lia6, ia6, sizeof(struct in6_addr));
3139202468Sbz	return (0);
3140202468Sbz}
3141202468Sbz
3142202468Sbz/*
3143192895Sjamie * Return true if pr1 and pr2 have the same IPv6 address restrictions.
3144192895Sjamie */
3145192895Sjamieint
3146192895Sjamieprison_equal_ip6(struct prison *pr1, struct prison *pr2)
3147192895Sjamie{
3148192895Sjamie
3149192895Sjamie	if (pr1 == pr2)
3150192895Sjamie		return (1);
3151192895Sjamie
3152195945Sjamie	while (pr1 != &prison0 &&
3153195945Sjamie#ifdef VIMAGE
3154195945Sjamie	       !(pr1->pr_flags & PR_VNET) &&
3155195945Sjamie#endif
3156195945Sjamie	       !(pr1->pr_flags & PR_IP6_USER))
3157192895Sjamie		pr1 = pr1->pr_parent;
3158195945Sjamie	while (pr2 != &prison0 &&
3159195945Sjamie#ifdef VIMAGE
3160195945Sjamie	       !(pr2->pr_flags & PR_VNET) &&
3161195945Sjamie#endif
3162195945Sjamie	       !(pr2->pr_flags & PR_IP6_USER))
3163192895Sjamie		pr2 = pr2->pr_parent;
3164192895Sjamie	return (pr1 == pr2);
3165192895Sjamie}
3166192895Sjamie
3167192895Sjamie/*
3168185435Sbz * Make sure our (source) address is set to something meaningful to this jail.
3169185435Sbz *
3170185435Sbz * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
3171185435Sbz * when needed while binding.
3172185435Sbz *
3173192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3174192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3175192895Sjamie * doesn't allow IPv6.
3176185435Sbz */
3177185435Sbzint
3178185435Sbzprison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
3179185435Sbz{
3180191673Sjamie	struct prison *pr;
3181191673Sjamie	int error;
3182185435Sbz
3183185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3184185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3185185435Sbz
3186192895Sjamie	pr = cred->cr_prison;
3187192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3188185435Sbz		return (0);
3189191673Sjamie	mtx_lock(&pr->pr_mtx);
3190192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3191192895Sjamie		mtx_unlock(&pr->pr_mtx);
3192192895Sjamie		return (0);
3193192895Sjamie	}
3194191673Sjamie	if (pr->pr_ip6 == NULL) {
3195191673Sjamie		mtx_unlock(&pr->pr_mtx);
3196188144Sjamie		return (EAFNOSUPPORT);
3197191673Sjamie	}
3198188144Sjamie
3199185435Sbz	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3200191673Sjamie		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3201191673Sjamie		mtx_unlock(&pr->pr_mtx);
3202185435Sbz		return (0);
320381114Srwatson	}
3204185435Sbz
3205188144Sjamie	if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3206188144Sjamie		/*
3207188144Sjamie		 * In case there is only 1 IPv6 address, and v6only is true,
3208188144Sjamie		 * then bind directly.
3209188144Sjamie		 */
3210191673Sjamie		if (v6only != 0 && pr->pr_ip6s == 1)
3211191673Sjamie			bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3212191673Sjamie		mtx_unlock(&pr->pr_mtx);
3213185435Sbz		return (0);
3214185435Sbz	}
3215188144Sjamie
3216191673Sjamie	error = _prison_check_ip6(pr, ia6);
3217191673Sjamie	mtx_unlock(&pr->pr_mtx);
3218191673Sjamie	return (error);
3219185435Sbz}
3220185435Sbz
3221185435Sbz/*
3222185435Sbz * Rewrite destination address in case we will connect to loopback address.
3223185435Sbz *
3224188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3225185435Sbz */
3226185435Sbzint
3227185435Sbzprison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3228185435Sbz{
3229191673Sjamie	struct prison *pr;
3230185435Sbz
3231185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3232185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3233185435Sbz
3234192895Sjamie	pr = cred->cr_prison;
3235192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3236185435Sbz		return (0);
3237191673Sjamie	mtx_lock(&pr->pr_mtx);
3238192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3239192895Sjamie		mtx_unlock(&pr->pr_mtx);
3240192895Sjamie		return (0);
3241192895Sjamie	}
3242191673Sjamie	if (pr->pr_ip6 == NULL) {
3243191673Sjamie		mtx_unlock(&pr->pr_mtx);
3244188144Sjamie		return (EAFNOSUPPORT);
3245191673Sjamie	}
3246188144Sjamie
3247185435Sbz	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3248191673Sjamie		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3249191673Sjamie		mtx_unlock(&pr->pr_mtx);
3250185435Sbz		return (0);
3251185435Sbz	}
3252185435Sbz
3253185435Sbz	/*
3254185435Sbz	 * Return success because nothing had to be changed.
3255185435Sbz	 */
3256191673Sjamie	mtx_unlock(&pr->pr_mtx);
325746155Sphk	return (0);
325846155Sphk}
325946155Sphk
3260185435Sbz/*
3261188144Sjamie * Check if given address belongs to the jail referenced by cred/prison.
3262185435Sbz *
3263192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3264192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3265192895Sjamie * doesn't allow IPv6.
3266185435Sbz */
3267185435Sbzstatic int
3268185435Sbz_prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
326946155Sphk{
3270185435Sbz	int i, a, z, d;
327146155Sphk
3272185435Sbz	/*
3273185435Sbz	 * Check the primary IP.
3274185435Sbz	 */
3275185435Sbz	if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3276188144Sjamie		return (0);
3277185435Sbz
3278185435Sbz	/*
3279185435Sbz	 * All the other IPs are sorted so we can do a binary search.
3280185435Sbz	 */
3281185435Sbz	a = 0;
3282185435Sbz	z = pr->pr_ip6s - 2;
3283185435Sbz	while (a <= z) {
3284185435Sbz		i = (a + z) / 2;
3285185435Sbz		d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3286185435Sbz		if (d > 0)
3287185435Sbz			z = i - 1;
3288185435Sbz		else if (d < 0)
3289185435Sbz			a = i + 1;
329046155Sphk		else
3291188144Sjamie			return (0);
329246155Sphk	}
3293188144Sjamie
3294188144Sjamie	return (EADDRNOTAVAIL);
329546155Sphk}
329646155Sphk
329746155Sphkint
3298185435Sbzprison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3299185435Sbz{
3300191673Sjamie	struct prison *pr;
3301191673Sjamie	int error;
3302185435Sbz
3303185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3304185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3305185435Sbz
3306192895Sjamie	pr = cred->cr_prison;
3307192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3308188144Sjamie		return (0);
3309191673Sjamie	mtx_lock(&pr->pr_mtx);
3310192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3311192895Sjamie		mtx_unlock(&pr->pr_mtx);
3312192895Sjamie		return (0);
3313192895Sjamie	}
3314191673Sjamie	if (pr->pr_ip6 == NULL) {
3315191673Sjamie		mtx_unlock(&pr->pr_mtx);
3316188144Sjamie		return (EAFNOSUPPORT);
3317191673Sjamie	}
3318185435Sbz
3319191673Sjamie	error = _prison_check_ip6(pr, ia6);
3320191673Sjamie	mtx_unlock(&pr->pr_mtx);
3321191673Sjamie	return (error);
3322185435Sbz}
3323185435Sbz#endif
3324185435Sbz
3325185435Sbz/*
3326188146Sjamie * Check if a jail supports the given address family.
3327188146Sjamie *
3328188146Sjamie * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3329188146Sjamie * if not.
3330188146Sjamie */
3331188146Sjamieint
3332188146Sjamieprison_check_af(struct ucred *cred, int af)
3333188146Sjamie{
3334192895Sjamie	struct prison *pr;
3335188146Sjamie	int error;
3336188146Sjamie
3337188146Sjamie	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3338188146Sjamie
3339192895Sjamie	pr = cred->cr_prison;
3340194923Sjamie#ifdef VIMAGE
3341194915Sjamie	/* Prisons with their own network stack are not limited. */
3342200473Sbz	if (prison_owns_vnet(cred))
3343194915Sjamie		return (0);
3344194923Sjamie#endif
3345194915Sjamie
3346188146Sjamie	error = 0;
3347188146Sjamie	switch (af)
3348188146Sjamie	{
3349188146Sjamie#ifdef INET
3350188146Sjamie	case AF_INET:
3351192895Sjamie		if (pr->pr_flags & PR_IP4)
3352192895Sjamie		{
3353192895Sjamie			mtx_lock(&pr->pr_mtx);
3354192895Sjamie			if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3355192895Sjamie				error = EAFNOSUPPORT;
3356192895Sjamie			mtx_unlock(&pr->pr_mtx);
3357192895Sjamie		}
3358188146Sjamie		break;
3359188146Sjamie#endif
3360188146Sjamie#ifdef INET6
3361188146Sjamie	case AF_INET6:
3362192895Sjamie		if (pr->pr_flags & PR_IP6)
3363192895Sjamie		{
3364192895Sjamie			mtx_lock(&pr->pr_mtx);
3365192895Sjamie			if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3366192895Sjamie				error = EAFNOSUPPORT;
3367192895Sjamie			mtx_unlock(&pr->pr_mtx);
3368192895Sjamie		}
3369188146Sjamie		break;
3370188146Sjamie#endif
3371188146Sjamie	case AF_LOCAL:
3372188146Sjamie	case AF_ROUTE:
3373188146Sjamie		break;
3374188146Sjamie	default:
3375192895Sjamie		if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3376188146Sjamie			error = EAFNOSUPPORT;
3377188146Sjamie	}
3378188146Sjamie	return (error);
3379188146Sjamie}
3380188146Sjamie
3381188146Sjamie/*
3382185435Sbz * Check if given address belongs to the jail referenced by cred (wrapper to
3383185435Sbz * prison_check_ip[46]).
3384185435Sbz *
3385192895Sjamie * Returns 0 if jail doesn't restrict the address family or if address belongs
3386192895Sjamie * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3387192895Sjamie * the jail doesn't allow the address family.  IPv4 Address passed in in NBO.
3388185435Sbz */
3389185435Sbzint
339072786Srwatsonprison_if(struct ucred *cred, struct sockaddr *sa)
339146155Sphk{
3392185435Sbz#ifdef INET
3393114168Smike	struct sockaddr_in *sai;
3394185435Sbz#endif
3395185435Sbz#ifdef INET6
3396185435Sbz	struct sockaddr_in6 *sai6;
3397185435Sbz#endif
3398188144Sjamie	int error;
339946155Sphk
3400185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3401185435Sbz	KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3402185435Sbz
3403200473Sbz#ifdef VIMAGE
3404200473Sbz	if (prison_owns_vnet(cred))
3405200473Sbz		return (0);
3406200473Sbz#endif
3407200473Sbz
3408188144Sjamie	error = 0;
3409188144Sjamie	switch (sa->sa_family)
3410185435Sbz	{
3411185435Sbz#ifdef INET
3412185435Sbz	case AF_INET:
3413185435Sbz		sai = (struct sockaddr_in *)sa;
3414188144Sjamie		error = prison_check_ip4(cred, &sai->sin_addr);
3415185435Sbz		break;
3416185435Sbz#endif
3417185435Sbz#ifdef INET6
3418185435Sbz	case AF_INET6:
3419185435Sbz		sai6 = (struct sockaddr_in6 *)sa;
3420188144Sjamie		error = prison_check_ip6(cred, &sai6->sin6_addr);
3421185435Sbz		break;
3422185435Sbz#endif
3423185435Sbz	default:
3424192895Sjamie		if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3425188144Sjamie			error = EAFNOSUPPORT;
3426185435Sbz	}
3427188144Sjamie	return (error);
342846155Sphk}
342972786Srwatson
343072786Srwatson/*
343172786Srwatson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
343272786Srwatson */
343372786Srwatsonint
3434114168Smikeprison_check(struct ucred *cred1, struct ucred *cred2)
343572786Srwatson{
343672786Srwatson
3437192895Sjamie	return ((cred1->cr_prison == cred2->cr_prison ||
3438192895Sjamie	    prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3439192895Sjamie}
344072786Srwatson
3441192895Sjamie/*
3442192895Sjamie * Return 1 if p2 is a child of p1, otherwise 0.
3443192895Sjamie */
3444192895Sjamieint
3445192895Sjamieprison_ischild(struct prison *pr1, struct prison *pr2)
3446192895Sjamie{
3447192895Sjamie
3448192895Sjamie	for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3449192895Sjamie		if (pr1 == pr2)
3450192895Sjamie			return (1);
345172786Srwatson	return (0);
345272786Srwatson}
345372786Srwatson
345472786Srwatson/*
345572786Srwatson * Return 1 if the passed credential is in a jail, otherwise 0.
345672786Srwatson */
345772786Srwatsonint
3458114168Smikejailed(struct ucred *cred)
345972786Srwatson{
346072786Srwatson
3461192895Sjamie	return (cred->cr_prison != &prison0);
346272786Srwatson}
346391384Srobert
346491384Srobert/*
3465200473Sbz * Return 1 if the passed credential is in a jail and that jail does not
3466200473Sbz * have its own virtual network stack, otherwise 0.
3467200473Sbz */
3468200473Sbzint
3469200473Sbzjailed_without_vnet(struct ucred *cred)
3470200473Sbz{
3471200473Sbz
3472200473Sbz	if (!jailed(cred))
3473200473Sbz		return (0);
3474200473Sbz#ifdef VIMAGE
3475200473Sbz	if (prison_owns_vnet(cred))
3476200473Sbz		return (0);
3477200473Sbz#endif
3478200473Sbz
3479200473Sbz	return (1);
3480200473Sbz}
3481200473Sbz
3482200473Sbz/*
3483194090Sjamie * Return the correct hostname (domainname, et al) for the passed credential.
348491384Srobert */
348591391Srobertvoid
3486114168Smikegetcredhostname(struct ucred *cred, char *buf, size_t size)
348791384Srobert{
3488193066Sjamie	struct prison *pr;
348991384Srobert
3490194090Sjamie	/*
3491194090Sjamie	 * A NULL credential can be used to shortcut to the physical
3492194090Sjamie	 * system's hostname.
3493194090Sjamie	 */
3494193066Sjamie	pr = (cred != NULL) ? cred->cr_prison : &prison0;
3495193066Sjamie	mtx_lock(&pr->pr_mtx);
3496194118Sjamie	strlcpy(buf, pr->pr_hostname, size);
3497193066Sjamie	mtx_unlock(&pr->pr_mtx);
349891384Srobert}
3499113275Smike
3500194090Sjamievoid
3501194090Sjamiegetcreddomainname(struct ucred *cred, char *buf, size_t size)
3502194090Sjamie{
3503194090Sjamie
3504194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3505194118Sjamie	strlcpy(buf, cred->cr_prison->pr_domainname, size);
3506194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3507194090Sjamie}
3508194090Sjamie
3509194090Sjamievoid
3510194090Sjamiegetcredhostuuid(struct ucred *cred, char *buf, size_t size)
3511194090Sjamie{
3512194090Sjamie
3513194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3514194118Sjamie	strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3515194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3516194090Sjamie}
3517194090Sjamie
3518194090Sjamievoid
3519194090Sjamiegetcredhostid(struct ucred *cred, unsigned long *hostid)
3520194090Sjamie{
3521194090Sjamie
3522194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3523194090Sjamie	*hostid = cred->cr_prison->pr_hostid;
3524194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3525194090Sjamie}
3526194090Sjamie
3527196176Sbz#ifdef VIMAGE
3528125804Srwatson/*
3529196176Sbz * Determine whether the prison represented by cred owns
3530196176Sbz * its vnet rather than having it inherited.
3531196176Sbz *
3532196176Sbz * Returns 1 in case the prison owns the vnet, 0 otherwise.
3533196176Sbz */
3534196176Sbzint
3535196176Sbzprison_owns_vnet(struct ucred *cred)
3536196176Sbz{
3537196176Sbz
3538196176Sbz	/*
3539196176Sbz	 * vnets cannot be added/removed after jail creation,
3540196176Sbz	 * so no need to lock here.
3541196176Sbz	 */
3542196176Sbz	return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3543196176Sbz}
3544196176Sbz#endif
3545196176Sbz
3546196176Sbz/*
3547147185Spjd * Determine whether the subject represented by cred can "see"
3548147185Spjd * status of a mount point.
3549147185Spjd * Returns: 0 for permitted, ENOENT otherwise.
3550147185Spjd * XXX: This function should be called cr_canseemount() and should be
3551147185Spjd *      placed in kern_prot.c.
3552125804Srwatson */
3553125804Srwatsonint
3554147185Spjdprison_canseemount(struct ucred *cred, struct mount *mp)
3555125804Srwatson{
3556147185Spjd	struct prison *pr;
3557147185Spjd	struct statfs *sp;
3558147185Spjd	size_t len;
3559125804Srwatson
3560192895Sjamie	pr = cred->cr_prison;
3561192895Sjamie	if (pr->pr_enforce_statfs == 0)
3562147185Spjd		return (0);
3563147185Spjd	if (pr->pr_root->v_mount == mp)
3564147185Spjd		return (0);
3565192895Sjamie	if (pr->pr_enforce_statfs == 2)
3566147185Spjd		return (ENOENT);
3567147185Spjd	/*
3568147185Spjd	 * If jail's chroot directory is set to "/" we should be able to see
3569147185Spjd	 * all mount-points from inside a jail.
3570147185Spjd	 * This is ugly check, but this is the only situation when jail's
3571147185Spjd	 * directory ends with '/'.
3572147185Spjd	 */
3573147185Spjd	if (strcmp(pr->pr_path, "/") == 0)
3574147185Spjd		return (0);
3575147185Spjd	len = strlen(pr->pr_path);
3576147185Spjd	sp = &mp->mnt_stat;
3577147185Spjd	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3578147185Spjd		return (ENOENT);
3579147185Spjd	/*
3580147185Spjd	 * Be sure that we don't have situation where jail's root directory
3581147185Spjd	 * is "/some/path" and mount point is "/some/pathpath".
3582147185Spjd	 */
3583147185Spjd	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3584147185Spjd		return (ENOENT);
3585147185Spjd	return (0);
3586147185Spjd}
3587147185Spjd
3588147185Spjdvoid
3589147185Spjdprison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3590147185Spjd{
3591147185Spjd	char jpath[MAXPATHLEN];
3592147185Spjd	struct prison *pr;
3593147185Spjd	size_t len;
3594147185Spjd
3595192895Sjamie	pr = cred->cr_prison;
3596192895Sjamie	if (pr->pr_enforce_statfs == 0)
3597147185Spjd		return;
3598147185Spjd	if (prison_canseemount(cred, mp) != 0) {
3599147185Spjd		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3600147185Spjd		strlcpy(sp->f_mntonname, "[restricted]",
3601147185Spjd		    sizeof(sp->f_mntonname));
3602147185Spjd		return;
3603125804Srwatson	}
3604147185Spjd	if (pr->pr_root->v_mount == mp) {
3605147185Spjd		/*
3606147185Spjd		 * Clear current buffer data, so we are sure nothing from
3607147185Spjd		 * the valid path left there.
3608147185Spjd		 */
3609147185Spjd		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3610147185Spjd		*sp->f_mntonname = '/';
3611147185Spjd		return;
3612147185Spjd	}
3613147185Spjd	/*
3614147185Spjd	 * If jail's chroot directory is set to "/" we should be able to see
3615147185Spjd	 * all mount-points from inside a jail.
3616147185Spjd	 */
3617147185Spjd	if (strcmp(pr->pr_path, "/") == 0)
3618147185Spjd		return;
3619147185Spjd	len = strlen(pr->pr_path);
3620147185Spjd	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3621147185Spjd	/*
3622147185Spjd	 * Clear current buffer data, so we are sure nothing from
3623147185Spjd	 * the valid path left there.
3624147185Spjd	 */
3625147185Spjd	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3626147185Spjd	if (*jpath == '\0') {
3627147185Spjd		/* Should never happen. */
3628147185Spjd		*sp->f_mntonname = '/';
3629147185Spjd	} else {
3630147185Spjd		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3631147185Spjd	}
3632125804Srwatson}
3633125804Srwatson
3634164032Srwatson/*
3635164032Srwatson * Check with permission for a specific privilege is granted within jail.  We
3636164032Srwatson * have a specific list of accepted privileges; the rest are denied.
3637164032Srwatson */
3638164032Srwatsonint
3639164032Srwatsonprison_priv_check(struct ucred *cred, int priv)
3640164032Srwatson{
3641164032Srwatson
3642164032Srwatson	if (!jailed(cred))
3643164032Srwatson		return (0);
3644164032Srwatson
3645194915Sjamie#ifdef VIMAGE
3646194915Sjamie	/*
3647194915Sjamie	 * Privileges specific to prisons with a virtual network stack.
3648194915Sjamie	 * There might be a duplicate entry here in case the privilege
3649194915Sjamie	 * is only granted conditionally in the legacy jail case.
3650194915Sjamie	 */
3651164032Srwatson	switch (priv) {
3652194915Sjamie#ifdef notyet
3653194915Sjamie		/*
3654194915Sjamie		 * NFS-specific privileges.
3655194915Sjamie		 */
3656194915Sjamie	case PRIV_NFS_DAEMON:
3657194915Sjamie	case PRIV_NFS_LOCKD:
3658194915Sjamie#endif
3659194915Sjamie		/*
3660194915Sjamie		 * Network stack privileges.
3661194915Sjamie		 */
3662194915Sjamie	case PRIV_NET_BRIDGE:
3663194915Sjamie	case PRIV_NET_GRE:
3664194915Sjamie	case PRIV_NET_BPF:
3665194915Sjamie	case PRIV_NET_RAW:		/* Dup, cond. in legacy jail case. */
3666194915Sjamie	case PRIV_NET_ROUTE:
3667194915Sjamie	case PRIV_NET_TAP:
3668194915Sjamie	case PRIV_NET_SETIFMTU:
3669194915Sjamie	case PRIV_NET_SETIFFLAGS:
3670194915Sjamie	case PRIV_NET_SETIFCAP:
3671203052Sdelphij	case PRIV_NET_SETIFDESCR:
3672194915Sjamie	case PRIV_NET_SETIFNAME	:
3673194915Sjamie	case PRIV_NET_SETIFMETRIC:
3674194915Sjamie	case PRIV_NET_SETIFPHYS:
3675194915Sjamie	case PRIV_NET_SETIFMAC:
3676194915Sjamie	case PRIV_NET_ADDMULTI:
3677194915Sjamie	case PRIV_NET_DELMULTI:
3678194915Sjamie	case PRIV_NET_HWIOCTL:
3679194915Sjamie	case PRIV_NET_SETLLADDR:
3680194915Sjamie	case PRIV_NET_ADDIFGROUP:
3681194915Sjamie	case PRIV_NET_DELIFGROUP:
3682194915Sjamie	case PRIV_NET_IFCREATE:
3683194915Sjamie	case PRIV_NET_IFDESTROY:
3684194915Sjamie	case PRIV_NET_ADDIFADDR:
3685194915Sjamie	case PRIV_NET_DELIFADDR:
3686194915Sjamie	case PRIV_NET_LAGG:
3687194915Sjamie	case PRIV_NET_GIF:
3688194915Sjamie	case PRIV_NET_SETIFVNET:
3689223735Sbz	case PRIV_NET_SETIFFIB:
3690164032Srwatson
3691164032Srwatson		/*
3692194915Sjamie		 * 802.11-related privileges.
3693194915Sjamie		 */
3694194915Sjamie	case PRIV_NET80211_GETKEY:
3695194915Sjamie#ifdef notyet
3696194915Sjamie	case PRIV_NET80211_MANAGE:		/* XXX-BZ discuss with sam@ */
3697194915Sjamie#endif
3698194915Sjamie
3699194915Sjamie#ifdef notyet
3700194915Sjamie		/*
3701194915Sjamie		 * AppleTalk privileges.
3702194915Sjamie		 */
3703194915Sjamie	case PRIV_NETATALK_RESERVEDPORT:
3704194915Sjamie
3705194915Sjamie		/*
3706194915Sjamie		 * ATM privileges.
3707194915Sjamie		 */
3708194915Sjamie	case PRIV_NETATM_CFG:
3709194915Sjamie	case PRIV_NETATM_ADD:
3710194915Sjamie	case PRIV_NETATM_DEL:
3711194915Sjamie	case PRIV_NETATM_SET:
3712194915Sjamie
3713194915Sjamie		/*
3714194915Sjamie		 * Bluetooth privileges.
3715194915Sjamie		 */
3716194915Sjamie	case PRIV_NETBLUETOOTH_RAW:
3717194915Sjamie#endif
3718194915Sjamie
3719194915Sjamie		/*
3720194915Sjamie		 * Netgraph and netgraph module privileges.
3721194915Sjamie		 */
3722194915Sjamie	case PRIV_NETGRAPH_CONTROL:
3723194915Sjamie#ifdef notyet
3724194915Sjamie	case PRIV_NETGRAPH_TTY:
3725194915Sjamie#endif
3726194915Sjamie
3727194915Sjamie		/*
3728194915Sjamie		 * IPv4 and IPv6 privileges.
3729194915Sjamie		 */
3730194915Sjamie	case PRIV_NETINET_IPFW:
3731194915Sjamie	case PRIV_NETINET_DIVERT:
3732194915Sjamie	case PRIV_NETINET_PF:
3733194915Sjamie	case PRIV_NETINET_DUMMYNET:
3734194915Sjamie	case PRIV_NETINET_CARP:
3735194915Sjamie	case PRIV_NETINET_MROUTE:
3736194915Sjamie	case PRIV_NETINET_RAW:
3737194915Sjamie	case PRIV_NETINET_ADDRCTRL6:
3738194915Sjamie	case PRIV_NETINET_ND6:
3739194915Sjamie	case PRIV_NETINET_SCOPE6:
3740194915Sjamie	case PRIV_NETINET_ALIFETIME6:
3741194915Sjamie	case PRIV_NETINET_IPSEC:
3742194915Sjamie	case PRIV_NETINET_BINDANY:
3743194915Sjamie
3744194915Sjamie#ifdef notyet
3745194915Sjamie		/*
3746194915Sjamie		 * IPX/SPX privileges.
3747194915Sjamie		 */
3748194915Sjamie	case PRIV_NETIPX_RESERVEDPORT:
3749194915Sjamie	case PRIV_NETIPX_RAW:
3750194915Sjamie
3751194915Sjamie		/*
3752194915Sjamie		 * NCP privileges.
3753194915Sjamie		 */
3754194915Sjamie	case PRIV_NETNCP:
3755194915Sjamie
3756194915Sjamie		/*
3757194915Sjamie		 * SMB privileges.
3758194915Sjamie		 */
3759194915Sjamie	case PRIV_NETSMB:
3760194915Sjamie#endif
3761194915Sjamie
3762194915Sjamie	/*
3763194915Sjamie	 * No default: or deny here.
3764194915Sjamie	 * In case of no permit fall through to next switch().
3765194915Sjamie	 */
3766194915Sjamie		if (cred->cr_prison->pr_flags & PR_VNET)
3767194915Sjamie			return (0);
3768194915Sjamie	}
3769194915Sjamie#endif /* VIMAGE */
3770194915Sjamie
3771194915Sjamie	switch (priv) {
3772194915Sjamie
3773194915Sjamie		/*
3774164032Srwatson		 * Allow ktrace privileges for root in jail.
3775164032Srwatson		 */
3776164032Srwatson	case PRIV_KTRACE:
3777164032Srwatson
3778166827Srwatson#if 0
3779164032Srwatson		/*
3780164032Srwatson		 * Allow jailed processes to configure audit identity and
3781164032Srwatson		 * submit audit records (login, etc).  In the future we may
3782164032Srwatson		 * want to further refine the relationship between audit and
3783164032Srwatson		 * jail.
3784164032Srwatson		 */
3785164032Srwatson	case PRIV_AUDIT_GETAUDIT:
3786164032Srwatson	case PRIV_AUDIT_SETAUDIT:
3787164032Srwatson	case PRIV_AUDIT_SUBMIT:
3788166827Srwatson#endif
3789164032Srwatson
3790164032Srwatson		/*
3791164032Srwatson		 * Allow jailed processes to manipulate process UNIX
3792164032Srwatson		 * credentials in any way they see fit.
3793164032Srwatson		 */
3794164032Srwatson	case PRIV_CRED_SETUID:
3795164032Srwatson	case PRIV_CRED_SETEUID:
3796164032Srwatson	case PRIV_CRED_SETGID:
3797164032Srwatson	case PRIV_CRED_SETEGID:
3798164032Srwatson	case PRIV_CRED_SETGROUPS:
3799164032Srwatson	case PRIV_CRED_SETREUID:
3800164032Srwatson	case PRIV_CRED_SETREGID:
3801164032Srwatson	case PRIV_CRED_SETRESUID:
3802164032Srwatson	case PRIV_CRED_SETRESGID:
3803164032Srwatson
3804164032Srwatson		/*
3805164032Srwatson		 * Jail implements visibility constraints already, so allow
3806164032Srwatson		 * jailed root to override uid/gid-based constraints.
3807164032Srwatson		 */
3808164032Srwatson	case PRIV_SEEOTHERGIDS:
3809164032Srwatson	case PRIV_SEEOTHERUIDS:
3810164032Srwatson
3811164032Srwatson		/*
3812164032Srwatson		 * Jail implements inter-process debugging limits already, so
3813164032Srwatson		 * allow jailed root various debugging privileges.
3814164032Srwatson		 */
3815164032Srwatson	case PRIV_DEBUG_DIFFCRED:
3816164032Srwatson	case PRIV_DEBUG_SUGID:
3817164032Srwatson	case PRIV_DEBUG_UNPRIV:
3818164032Srwatson
3819164032Srwatson		/*
3820164032Srwatson		 * Allow jail to set various resource limits and login
3821164032Srwatson		 * properties, and for now, exceed process resource limits.
3822164032Srwatson		 */
3823164032Srwatson	case PRIV_PROC_LIMIT:
3824164032Srwatson	case PRIV_PROC_SETLOGIN:
3825164032Srwatson	case PRIV_PROC_SETRLIMIT:
3826164032Srwatson
3827164032Srwatson		/*
3828164032Srwatson		 * System V and POSIX IPC privileges are granted in jail.
3829164032Srwatson		 */
3830164032Srwatson	case PRIV_IPC_READ:
3831164032Srwatson	case PRIV_IPC_WRITE:
3832164032Srwatson	case PRIV_IPC_ADMIN:
3833164032Srwatson	case PRIV_IPC_MSGSIZE:
3834164032Srwatson	case PRIV_MQ_ADMIN:
3835164032Srwatson
3836164032Srwatson		/*
3837192895Sjamie		 * Jail operations within a jail work on child jails.
3838192895Sjamie		 */
3839192895Sjamie	case PRIV_JAIL_ATTACH:
3840192895Sjamie	case PRIV_JAIL_SET:
3841192895Sjamie	case PRIV_JAIL_REMOVE:
3842192895Sjamie
3843192895Sjamie		/*
3844164032Srwatson		 * Jail implements its own inter-process limits, so allow
3845164032Srwatson		 * root processes in jail to change scheduling on other
3846164032Srwatson		 * processes in the same jail.  Likewise for signalling.
3847164032Srwatson		 */
3848164032Srwatson	case PRIV_SCHED_DIFFCRED:
3849185435Sbz	case PRIV_SCHED_CPUSET:
3850164032Srwatson	case PRIV_SIGNAL_DIFFCRED:
3851164032Srwatson	case PRIV_SIGNAL_SUGID:
3852164032Srwatson
3853164032Srwatson		/*
3854164032Srwatson		 * Allow jailed processes to write to sysctls marked as jail
3855164032Srwatson		 * writable.
3856164032Srwatson		 */
3857164032Srwatson	case PRIV_SYSCTL_WRITEJAIL:
3858164032Srwatson
3859164032Srwatson		/*
3860164032Srwatson		 * Allow root in jail to manage a variety of quota
3861166831Srwatson		 * properties.  These should likely be conditional on a
3862166831Srwatson		 * configuration option.
3863164032Srwatson		 */
3864166832Srwatson	case PRIV_VFS_GETQUOTA:
3865166832Srwatson	case PRIV_VFS_SETQUOTA:
3866164032Srwatson
3867164032Srwatson		/*
3868164032Srwatson		 * Since Jail relies on chroot() to implement file system
3869164032Srwatson		 * protections, grant many VFS privileges to root in jail.
3870164032Srwatson		 * Be careful to exclude mount-related and NFS-related
3871164032Srwatson		 * privileges.
3872164032Srwatson		 */
3873164032Srwatson	case PRIV_VFS_READ:
3874164032Srwatson	case PRIV_VFS_WRITE:
3875164032Srwatson	case PRIV_VFS_ADMIN:
3876164032Srwatson	case PRIV_VFS_EXEC:
3877164032Srwatson	case PRIV_VFS_LOOKUP:
3878164032Srwatson	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
3879164032Srwatson	case PRIV_VFS_CHFLAGS_DEV:
3880164032Srwatson	case PRIV_VFS_CHOWN:
3881164032Srwatson	case PRIV_VFS_CHROOT:
3882167152Spjd	case PRIV_VFS_RETAINSUGID:
3883164032Srwatson	case PRIV_VFS_FCHROOT:
3884164032Srwatson	case PRIV_VFS_LINK:
3885164032Srwatson	case PRIV_VFS_SETGID:
3886172860Srwatson	case PRIV_VFS_STAT:
3887164032Srwatson	case PRIV_VFS_STICKYFILE:
3888164032Srwatson		return (0);
3889164032Srwatson
3890164032Srwatson		/*
3891164032Srwatson		 * Depending on the global setting, allow privilege of
3892164032Srwatson		 * setting system flags.
3893164032Srwatson		 */
3894164032Srwatson	case PRIV_VFS_SYSFLAGS:
3895192895Sjamie		if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3896164032Srwatson			return (0);
3897164032Srwatson		else
3898164032Srwatson			return (EPERM);
3899164032Srwatson
3900164032Srwatson		/*
3901168396Spjd		 * Depending on the global setting, allow privilege of
3902168396Spjd		 * mounting/unmounting file systems.
3903168396Spjd		 */
3904168396Spjd	case PRIV_VFS_MOUNT:
3905168396Spjd	case PRIV_VFS_UNMOUNT:
3906168396Spjd	case PRIV_VFS_MOUNT_NONUSER:
3907168699Spjd	case PRIV_VFS_MOUNT_OWNER:
3908224615Smm		if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT &&
3909224615Smm		    cred->cr_prison->pr_enforce_statfs < 2)
3910168396Spjd			return (0);
3911168396Spjd		else
3912168396Spjd			return (EPERM);
3913168396Spjd
3914168396Spjd		/*
3915168591Srwatson		 * Allow jailed root to bind reserved ports and reuse in-use
3916168591Srwatson		 * ports.
3917164032Srwatson		 */
3918164032Srwatson	case PRIV_NETINET_RESERVEDPORT:
3919168591Srwatson	case PRIV_NETINET_REUSEPORT:
3920164032Srwatson		return (0);
3921164032Srwatson
3922164032Srwatson		/*
3923175630Sbz		 * Allow jailed root to set certian IPv4/6 (option) headers.
3924175630Sbz		 */
3925175630Sbz	case PRIV_NETINET_SETHDROPTS:
3926175630Sbz		return (0);
3927175630Sbz
3928175630Sbz		/*
3929164032Srwatson		 * Conditionally allow creating raw sockets in jail.
3930164032Srwatson		 */
3931164032Srwatson	case PRIV_NETINET_RAW:
3932192895Sjamie		if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3933164032Srwatson			return (0);
3934164032Srwatson		else
3935164032Srwatson			return (EPERM);
3936164032Srwatson
3937164032Srwatson		/*
3938164032Srwatson		 * Since jail implements its own visibility limits on netstat
3939164032Srwatson		 * sysctls, allow getcred.  This allows identd to work in
3940164032Srwatson		 * jail.
3941164032Srwatson		 */
3942164032Srwatson	case PRIV_NETINET_GETCRED:
3943164032Srwatson		return (0);
3944164032Srwatson
3945219304Strasz		/*
3946219304Strasz		 * Allow jailed root to set loginclass.
3947219304Strasz		 */
3948219304Strasz	case PRIV_PROC_SETLOGINCLASS:
3949219304Strasz		return (0);
3950219304Strasz
3951164032Srwatson	default:
3952164032Srwatson		/*
3953164032Srwatson		 * In all remaining cases, deny the privilege request.  This
3954164032Srwatson		 * includes almost all network privileges, many system
3955164032Srwatson		 * configuration privileges.
3956164032Srwatson		 */
3957164032Srwatson		return (EPERM);
3958164032Srwatson	}
3959164032Srwatson}
3960164032Srwatson
3961192895Sjamie/*
3962192895Sjamie * Return the part of pr2's name that is relative to pr1, or the whole name
3963192895Sjamie * if it does not directly follow.
3964192895Sjamie */
3965192895Sjamie
3966192895Sjamiechar *
3967192895Sjamieprison_name(struct prison *pr1, struct prison *pr2)
3968192895Sjamie{
3969192895Sjamie	char *name;
3970192895Sjamie
3971192895Sjamie	/* Jails see themselves as "0" (if they see themselves at all). */
3972192895Sjamie	if (pr1 == pr2)
3973192895Sjamie		return "0";
3974192895Sjamie	name = pr2->pr_name;
3975192895Sjamie	if (prison_ischild(pr1, pr2)) {
3976192895Sjamie		/*
3977192895Sjamie		 * pr1 isn't locked (and allprison_lock may not be either)
3978192895Sjamie		 * so its length can't be counted on.  But the number of dots
3979192895Sjamie		 * can be counted on - and counted.
3980192895Sjamie		 */
3981192895Sjamie		for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3982192895Sjamie			name = strchr(name, '.') + 1;
3983192895Sjamie	}
3984192895Sjamie	return (name);
3985192895Sjamie}
3986192895Sjamie
3987192895Sjamie/*
3988192895Sjamie * Return the part of pr2's path that is relative to pr1, or the whole path
3989192895Sjamie * if it does not directly follow.
3990192895Sjamie */
3991192895Sjamiestatic char *
3992192895Sjamieprison_path(struct prison *pr1, struct prison *pr2)
3993192895Sjamie{
3994192895Sjamie	char *path1, *path2;
3995192895Sjamie	int len1;
3996192895Sjamie
3997192895Sjamie	path1 = pr1->pr_path;
3998192895Sjamie	path2 = pr2->pr_path;
3999192895Sjamie	if (!strcmp(path1, "/"))
4000192895Sjamie		return (path2);
4001192895Sjamie	len1 = strlen(path1);
4002192895Sjamie	if (strncmp(path1, path2, len1))
4003192895Sjamie		return (path2);
4004192895Sjamie	if (path2[len1] == '\0')
4005192895Sjamie		return "/";
4006192895Sjamie	if (path2[len1] == '/')
4007192895Sjamie		return (path2 + len1);
4008192895Sjamie	return (path2);
4009192895Sjamie}
4010192895Sjamie
4011192895Sjamie
4012192895Sjamie/*
4013192895Sjamie * Jail-related sysctls.
4014192895Sjamie */
4015227309Sedstatic SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
4016192895Sjamie    "Jails");
4017192895Sjamie
4018113275Smikestatic int
4019113275Smikesysctl_jail_list(SYSCTL_HANDLER_ARGS)
4020113275Smike{
4021191673Sjamie	struct xprison *xp;
4022192895Sjamie	struct prison *pr, *cpr;
4023191673Sjamie#ifdef INET
4024191673Sjamie	struct in_addr *ip4 = NULL;
4025191673Sjamie	int ip4s = 0;
4026191673Sjamie#endif
4027191673Sjamie#ifdef INET6
4028208803Scperciva	struct in6_addr *ip6 = NULL;
4029191673Sjamie	int ip6s = 0;
4030191673Sjamie#endif
4031192895Sjamie	int descend, error;
4032113275Smike
4033191673Sjamie	xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
4034192895Sjamie	pr = req->td->td_ucred->cr_prison;
4035191673Sjamie	error = 0;
4036168401Spjd	sx_slock(&allprison_lock);
4037192895Sjamie	FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
4038192895Sjamie#if defined(INET) || defined(INET6)
4039191673Sjamie again:
4040192895Sjamie#endif
4041192895Sjamie		mtx_lock(&cpr->pr_mtx);
4042185435Sbz#ifdef INET
4043192895Sjamie		if (cpr->pr_ip4s > 0) {
4044192895Sjamie			if (ip4s < cpr->pr_ip4s) {
4045192895Sjamie				ip4s = cpr->pr_ip4s;
4046192895Sjamie				mtx_unlock(&cpr->pr_mtx);
4047191673Sjamie				ip4 = realloc(ip4, ip4s *
4048191673Sjamie				    sizeof(struct in_addr), M_TEMP, M_WAITOK);
4049191673Sjamie				goto again;
4050191673Sjamie			}
4051192895Sjamie			bcopy(cpr->pr_ip4, ip4,
4052192895Sjamie			    cpr->pr_ip4s * sizeof(struct in_addr));
4053191673Sjamie		}
4054185435Sbz#endif
4055185435Sbz#ifdef INET6
4056192895Sjamie		if (cpr->pr_ip6s > 0) {
4057192895Sjamie			if (ip6s < cpr->pr_ip6s) {
4058192895Sjamie				ip6s = cpr->pr_ip6s;
4059192895Sjamie				mtx_unlock(&cpr->pr_mtx);
4060191673Sjamie				ip6 = realloc(ip6, ip6s *
4061191673Sjamie				    sizeof(struct in6_addr), M_TEMP, M_WAITOK);
4062191673Sjamie				goto again;
4063191673Sjamie			}
4064192895Sjamie			bcopy(cpr->pr_ip6, ip6,
4065192895Sjamie			    cpr->pr_ip6s * sizeof(struct in6_addr));
4066191673Sjamie		}
4067185435Sbz#endif
4068192895Sjamie		if (cpr->pr_ref == 0) {
4069192895Sjamie			mtx_unlock(&cpr->pr_mtx);
4070191673Sjamie			continue;
4071191673Sjamie		}
4072191673Sjamie		bzero(xp, sizeof(*xp));
4073113275Smike		xp->pr_version = XPRISON_VERSION;
4074192895Sjamie		xp->pr_id = cpr->pr_id;
4075192895Sjamie		xp->pr_state = cpr->pr_uref > 0
4076191673Sjamie		    ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
4077192895Sjamie		strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
4078194118Sjamie		strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
4079192895Sjamie		strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
4080185435Sbz#ifdef INET
4081192895Sjamie		xp->pr_ip4s = cpr->pr_ip4s;
4082185435Sbz#endif
4083185435Sbz#ifdef INET6
4084192895Sjamie		xp->pr_ip6s = cpr->pr_ip6s;
4085185435Sbz#endif
4086192895Sjamie		mtx_unlock(&cpr->pr_mtx);
4087191673Sjamie		error = SYSCTL_OUT(req, xp, sizeof(*xp));
4088191673Sjamie		if (error)
4089191673Sjamie			break;
4090185435Sbz#ifdef INET
4091191673Sjamie		if (xp->pr_ip4s > 0) {
4092191673Sjamie			error = SYSCTL_OUT(req, ip4,
4093191673Sjamie			    xp->pr_ip4s * sizeof(struct in_addr));
4094191673Sjamie			if (error)
4095191673Sjamie				break;
4096185435Sbz		}
4097185435Sbz#endif
4098185435Sbz#ifdef INET6
4099191673Sjamie		if (xp->pr_ip6s > 0) {
4100191673Sjamie			error = SYSCTL_OUT(req, ip6,
4101191673Sjamie			    xp->pr_ip6s * sizeof(struct in6_addr));
4102191673Sjamie			if (error)
4103191673Sjamie				break;
4104185435Sbz		}
4105185435Sbz#endif
4106113275Smike	}
4107168401Spjd	sx_sunlock(&allprison_lock);
4108191673Sjamie	free(xp, M_TEMP);
4109191673Sjamie#ifdef INET
4110191673Sjamie	free(ip4, M_TEMP);
4111191673Sjamie#endif
4112191673Sjamie#ifdef INET6
4113191673Sjamie	free(ip6, M_TEMP);
4114191673Sjamie#endif
4115167354Spjd	return (error);
4116113275Smike}
4117113275Smike
4118187864SedSYSCTL_OID(_security_jail, OID_AUTO, list,
4119187864Sed    CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4120187864Sed    sysctl_jail_list, "S", "List of active jails");
4121126004Spjd
4122126004Spjdstatic int
4123126004Spjdsysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4124126004Spjd{
4125126004Spjd	int error, injail;
4126126004Spjd
4127126004Spjd	injail = jailed(req->td->td_ucred);
4128126004Spjd	error = SYSCTL_OUT(req, &injail, sizeof(injail));
4129126004Spjd
4130126004Spjd	return (error);
4131126004Spjd}
4132192895Sjamie
4133187864SedSYSCTL_PROC(_security_jail, OID_AUTO, jailed,
4134187864Sed    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4135187864Sed    sysctl_jail_jailed, "I", "Process in jail?");
4136185435Sbz
4137192895Sjamie#if defined(INET) || defined(INET6)
4138193865SjamieSYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
4139192895Sjamie    &jail_max_af_ips, 0,
4140192895Sjamie    "Number of IP addresses a jail may have at most per address family");
4141192895Sjamie#endif
4142192895Sjamie
4143192895Sjamie/*
4144192895Sjamie * Default parameters for jail(2) compatability.  For historical reasons,
4145192895Sjamie * the sysctl names have varying similarity to the parameter names.  Prisons
4146192895Sjamie * just see their own parameters, and can't change them.
4147192895Sjamie */
4148192895Sjamiestatic int
4149192895Sjamiesysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
4150192895Sjamie{
4151192895Sjamie	struct prison *pr;
4152192895Sjamie	int allow, error, i;
4153192895Sjamie
4154192895Sjamie	pr = req->td->td_ucred->cr_prison;
4155192895Sjamie	allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
4156192895Sjamie
4157192895Sjamie	/* Get the current flag value, and convert it to a boolean. */
4158192895Sjamie	i = (allow & arg2) ? 1 : 0;
4159192895Sjamie	if (arg1 != NULL)
4160192895Sjamie		i = !i;
4161192895Sjamie	error = sysctl_handle_int(oidp, &i, 0, req);
4162192895Sjamie	if (error || !req->newptr)
4163192895Sjamie		return (error);
4164192895Sjamie	i = i ? arg2 : 0;
4165192895Sjamie	if (arg1 != NULL)
4166192895Sjamie		i ^= arg2;
4167192895Sjamie	/*
4168192895Sjamie	 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
4169192895Sjamie	 * for writing.
4170192895Sjamie	 */
4171192895Sjamie	mtx_lock(&prison0.pr_mtx);
4172192895Sjamie	jail_default_allow = (jail_default_allow & ~arg2) | i;
4173192895Sjamie	mtx_unlock(&prison0.pr_mtx);
4174192895Sjamie	return (0);
4175192895Sjamie}
4176192895Sjamie
4177192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
4178192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4179192895Sjamie    NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
4180192895Sjamie    "Processes in jail can set their hostnames");
4181192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
4182192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4183192895Sjamie    (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
4184192895Sjamie    "Processes in jail are limited to creating UNIX/IP/route sockets only");
4185192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4186192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4187192895Sjamie    NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4188192895Sjamie    "Processes in jail can use System V IPC primitives");
4189192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4190192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4191192895Sjamie    NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4192192895Sjamie    "Prison root can create raw sockets");
4193192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4194192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4195192895Sjamie    NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4196192895Sjamie    "Processes in jail can alter system file flags");
4197192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4198192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4199192895Sjamie    NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4200192895Sjamie    "Processes in jail can mount/unmount jail-friendly file systems");
4201232059SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_devfs_allowed,
4202232059Smm    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4203232059Smm    NULL, PR_ALLOW_MOUNT_DEVFS, sysctl_jail_default_allow, "I",
4204232186Smm    "Processes in jail can mount the devfs file system");
4205232059SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_nullfs_allowed,
4206232059Smm    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4207232059Smm    NULL, PR_ALLOW_MOUNT_NULLFS, sysctl_jail_default_allow, "I",
4208232186Smm    "Processes in jail can mount the nullfs file system");
4209232186SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_zfs_allowed,
4210232186Smm    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4211232186Smm    NULL, PR_ALLOW_MOUNT_ZFS, sysctl_jail_default_allow, "I",
4212232186Smm    "Processes in jail can mount the zfs file system");
4213192895Sjamie
4214192895Sjamiestatic int
4215192895Sjamiesysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
4216192895Sjamie{
4217192895Sjamie	struct prison *pr;
4218192895Sjamie	int level, error;
4219192895Sjamie
4220192895Sjamie	pr = req->td->td_ucred->cr_prison;
4221192895Sjamie	level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4222192895Sjamie	error = sysctl_handle_int(oidp, &level, 0, req);
4223192895Sjamie	if (error || !req->newptr)
4224192895Sjamie		return (error);
4225192895Sjamie	*(int *)arg1 = level;
4226192895Sjamie	return (0);
4227192895Sjamie}
4228192895Sjamie
4229192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4230192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4231192895Sjamie    &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4232192895Sjamie    sysctl_jail_default_level, "I",
4233192895Sjamie    "Processes in jail cannot see all mounted file systems");
4234192895Sjamie
4235231267SmmSYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
4236231267Smm    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
4237231267Smm    &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
4238231267Smm    sysctl_jail_default_level, "I",
4239231267Smm    "Ruleset for the devfs filesystem in jail");
4240231267Smm
4241192895Sjamie/*
4242192895Sjamie * Nodes to describe jail parameters.  Maximum length of string parameters
4243192895Sjamie * is returned in the string itself, and the other parameters exist merely
4244192895Sjamie * to make themselves and their types known.
4245192895Sjamie */
4246192895SjamieSYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
4247192895Sjamie    "Jail parameters");
4248192895Sjamie
4249192895Sjamieint
4250192895Sjamiesysctl_jail_param(SYSCTL_HANDLER_ARGS)
4251192895Sjamie{
4252192895Sjamie	int i;
4253192895Sjamie	long l;
4254192895Sjamie	size_t s;
4255192895Sjamie	char numbuf[12];
4256192895Sjamie
4257192895Sjamie	switch (oidp->oid_kind & CTLTYPE)
4258192895Sjamie	{
4259192895Sjamie	case CTLTYPE_LONG:
4260192895Sjamie	case CTLTYPE_ULONG:
4261192895Sjamie		l = 0;
4262192895Sjamie#ifdef SCTL_MASK32
4263192895Sjamie		if (!(req->flags & SCTL_MASK32))
4264192895Sjamie#endif
4265192895Sjamie			return (SYSCTL_OUT(req, &l, sizeof(l)));
4266192895Sjamie	case CTLTYPE_INT:
4267192895Sjamie	case CTLTYPE_UINT:
4268192895Sjamie		i = 0;
4269192895Sjamie		return (SYSCTL_OUT(req, &i, sizeof(i)));
4270192895Sjamie	case CTLTYPE_STRING:
4271219819Sjeff		snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
4272192895Sjamie		return
4273192895Sjamie		    (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4274192895Sjamie	case CTLTYPE_STRUCT:
4275192895Sjamie		s = (size_t)arg2;
4276192895Sjamie		return (SYSCTL_OUT(req, &s, sizeof(s)));
4277192895Sjamie	}
4278192895Sjamie	return (0);
4279192895Sjamie}
4280192895Sjamie
4281192895SjamieSYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4282192895SjamieSYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4283192895SjamieSYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4284192895SjamieSYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4285192895SjamieSYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4286192895Sjamie    "I", "Jail secure level");
4287192895SjamieSYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4288192895Sjamie    "I", "Jail cannot see all mounted file systems");
4289231267SmmSYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
4290231267Smm    "I", "Ruleset for in-jail devfs mounts");
4291192895SjamieSYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4292192895Sjamie    "B", "Jail persistence");
4293194251Sjamie#ifdef VIMAGE
4294194251SjamieSYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4295195870Sjamie    "E,jailsys", "Virtual network stack");
4296194251Sjamie#endif
4297192895SjamieSYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4298192895Sjamie    "B", "Jail is in the process of shutting down");
4299192895Sjamie
4300194762SjamieSYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4301194762SjamieSYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4302194762Sjamie    "I", "Current number of child jails");
4303194762SjamieSYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4304194762Sjamie    "I", "Maximum number of child jails");
4305194762Sjamie
4306195870SjamieSYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4307192895SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4308192895Sjamie    "Jail hostname");
4309193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4310193066Sjamie    "Jail NIS domainname");
4311193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4312193066Sjamie    "Jail host UUID");
4313193066SjamieSYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4314193066Sjamie    "LU", "Jail host ID");
4315192895Sjamie
4316192895SjamieSYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4317192895SjamieSYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4318192895Sjamie
4319192895Sjamie#ifdef INET
4320195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4321195974Sjamie    "Jail IPv4 address virtualization");
4322192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4323192895Sjamie    "S,in_addr,a", "Jail IPv4 addresses");
4324202468SbzSYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4325202468Sbz    "B", "Do (not) use IPv4 source address selection rather than the "
4326202468Sbz    "primary jail IPv4 address.");
4327192895Sjamie#endif
4328192895Sjamie#ifdef INET6
4329195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4330195974Sjamie    "Jail IPv6 address virtualization");
4331192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4332192895Sjamie    "S,in6_addr,a", "Jail IPv6 addresses");
4333202468SbzSYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4334202468Sbz    "B", "Do (not) use IPv6 source address selection rather than the "
4335202468Sbz    "primary jail IPv6 address.");
4336192895Sjamie#endif
4337192895Sjamie
4338192895SjamieSYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4339192895SjamieSYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4340192895Sjamie    "B", "Jail may set hostname");
4341192895SjamieSYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4342192895Sjamie    "B", "Jail may use SYSV IPC");
4343192895SjamieSYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4344192895Sjamie    "B", "Jail may create raw sockets");
4345192895SjamieSYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4346192895Sjamie    "B", "Jail may alter system file flags");
4347192895SjamieSYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4348192895Sjamie    "B", "Jail may set file quotas");
4349192895SjamieSYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4350192895Sjamie    "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4351192895Sjamie
4352232059SmmSYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
4353232059SmmSYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
4354232059Smm    "B", "Jail may mount/unmount jail-friendly file systems in general");
4355232059SmmSYSCTL_JAIL_PARAM(_allow_mount, devfs, CTLTYPE_INT | CTLFLAG_RW,
4356232186Smm    "B", "Jail may mount the devfs file system");
4357232059SmmSYSCTL_JAIL_PARAM(_allow_mount, nullfs, CTLTYPE_INT | CTLFLAG_RW,
4358232186Smm    "B", "Jail may mount the nullfs file system");
4359232186SmmSYSCTL_JAIL_PARAM(_allow_mount, zfs, CTLTYPE_INT | CTLFLAG_RW,
4360232186Smm    "B", "Jail may mount the zfs file system");
4361232059Smm
4362220137Straszvoid
4363220137Straszprison_racct_foreach(void (*callback)(struct racct *racct,
4364220137Strasz    void *arg2, void *arg3), void *arg2, void *arg3)
4365220137Strasz{
4366221362Strasz	struct prison_racct *prr;
4367192895Sjamie
4368220137Strasz	sx_slock(&allprison_lock);
4369221362Strasz	LIST_FOREACH(prr, &allprison_racct, prr_next)
4370221362Strasz		(callback)(prr->prr_racct, arg2, arg3);
4371220137Strasz	sx_sunlock(&allprison_lock);
4372220137Strasz}
4373220137Strasz
4374221362Straszstatic struct prison_racct *
4375221362Straszprison_racct_find_locked(const char *name)
4376221362Strasz{
4377221362Strasz	struct prison_racct *prr;
4378221362Strasz
4379221362Strasz	sx_assert(&allprison_lock, SA_XLOCKED);
4380221362Strasz
4381221362Strasz	if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4382221362Strasz		return (NULL);
4383221362Strasz
4384221362Strasz	LIST_FOREACH(prr, &allprison_racct, prr_next) {
4385221362Strasz		if (strcmp(name, prr->prr_name) != 0)
4386221362Strasz			continue;
4387221362Strasz
4388221362Strasz		/* Found prison_racct with a matching name? */
4389221362Strasz		prison_racct_hold(prr);
4390221362Strasz		return (prr);
4391221362Strasz	}
4392221362Strasz
4393221362Strasz	/* Add new prison_racct. */
4394221362Strasz	prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4395221362Strasz	racct_create(&prr->prr_racct);
4396221362Strasz
4397221362Strasz	strcpy(prr->prr_name, name);
4398221362Strasz	refcount_init(&prr->prr_refcount, 1);
4399221362Strasz	LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4400221362Strasz
4401221362Strasz	return (prr);
4402221362Strasz}
4403221362Strasz
4404221362Straszstruct prison_racct *
4405221362Straszprison_racct_find(const char *name)
4406221362Strasz{
4407221362Strasz	struct prison_racct *prr;
4408221362Strasz
4409221362Strasz	sx_xlock(&allprison_lock);
4410221362Strasz	prr = prison_racct_find_locked(name);
4411221362Strasz	sx_xunlock(&allprison_lock);
4412221362Strasz	return (prr);
4413221362Strasz}
4414221362Strasz
4415221362Straszvoid
4416221362Straszprison_racct_hold(struct prison_racct *prr)
4417221362Strasz{
4418221362Strasz
4419221362Strasz	refcount_acquire(&prr->prr_refcount);
4420221362Strasz}
4421221362Strasz
4422221362Straszvoid
4423221362Straszprison_racct_free(struct prison_racct *prr)
4424221362Strasz{
4425221362Strasz	int old;
4426221362Strasz
4427221362Strasz	old = prr->prr_refcount;
4428221362Strasz	if (old > 1 && atomic_cmpset_int(&prr->prr_refcount, old, old - 1))
4429221362Strasz		return;
4430221362Strasz
4431221362Strasz	sx_xlock(&allprison_lock);
4432221362Strasz	if (refcount_release(&prr->prr_refcount)) {
4433221362Strasz		racct_destroy(&prr->prr_racct);
4434221362Strasz		LIST_REMOVE(prr, prr_next);
4435221362Strasz		sx_xunlock(&allprison_lock);
4436221362Strasz		free(prr, M_PRISON_RACCT);
4437221362Strasz
4438221362Strasz		return;
4439221362Strasz	}
4440221362Strasz	sx_xunlock(&allprison_lock);
4441221362Strasz}
4442221362Strasz
4443221362Strasz#ifdef RACCT
4444221362Straszstatic void
4445221362Straszprison_racct_attach(struct prison *pr)
4446221362Strasz{
4447221362Strasz	struct prison_racct *prr;
4448221362Strasz
4449221362Strasz	prr = prison_racct_find_locked(pr->pr_name);
4450221362Strasz	KASSERT(prr != NULL, ("cannot find prison_racct"));
4451221362Strasz
4452221362Strasz	pr->pr_prison_racct = prr;
4453221362Strasz}
4454221362Strasz
4455221362Straszstatic void
4456221362Straszprison_racct_detach(struct prison *pr)
4457221362Strasz{
4458221362Strasz	prison_racct_free(pr->pr_prison_racct);
4459221362Strasz	pr->pr_prison_racct = NULL;
4460221362Strasz}
4461221362Strasz#endif /* RACCT */
4462221362Strasz
4463185435Sbz#ifdef DDB
4464191673Sjamie
4465191673Sjamiestatic void
4466191673Sjamiedb_show_prison(struct prison *pr)
4467185435Sbz{
4468192895Sjamie	int fi;
4469191673Sjamie#if defined(INET) || defined(INET6)
4470191673Sjamie	int ii;
4471185435Sbz#endif
4472195870Sjamie	unsigned jsf;
4473185435Sbz#ifdef INET6
4474185435Sbz	char ip6buf[INET6_ADDRSTRLEN];
4475185435Sbz#endif
4476185435Sbz
4477191673Sjamie	db_printf("prison %p:\n", pr);
4478191673Sjamie	db_printf(" jid             = %d\n", pr->pr_id);
4479191673Sjamie	db_printf(" name            = %s\n", pr->pr_name);
4480192895Sjamie	db_printf(" parent          = %p\n", pr->pr_parent);
4481191673Sjamie	db_printf(" ref             = %d\n", pr->pr_ref);
4482191673Sjamie	db_printf(" uref            = %d\n", pr->pr_uref);
4483191673Sjamie	db_printf(" path            = %s\n", pr->pr_path);
4484191673Sjamie	db_printf(" cpuset          = %d\n", pr->pr_cpuset
4485191673Sjamie	    ? pr->pr_cpuset->cs_id : -1);
4486194251Sjamie#ifdef VIMAGE
4487194251Sjamie	db_printf(" vnet            = %p\n", pr->pr_vnet);
4488194251Sjamie#endif
4489191673Sjamie	db_printf(" root            = %p\n", pr->pr_root);
4490191673Sjamie	db_printf(" securelevel     = %d\n", pr->pr_securelevel);
4491231267Smm	db_printf(" devfs_rsnum     = %d\n", pr->pr_devfs_rsnum);
4492202123Sbz	db_printf(" children.max    = %d\n", pr->pr_childmax);
4493202123Sbz	db_printf(" children.cur    = %d\n", pr->pr_childcount);
4494192895Sjamie	db_printf(" child           = %p\n", LIST_FIRST(&pr->pr_children));
4495192895Sjamie	db_printf(" sibling         = %p\n", LIST_NEXT(pr, pr_sibling));
4496202123Sbz	db_printf(" flags           = 0x%x", pr->pr_flags);
4497192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
4498192895Sjamie	    fi++)
4499192895Sjamie		if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
4500192895Sjamie			db_printf(" %s", pr_flag_names[fi]);
4501195870Sjamie	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
4502195870Sjamie	    fi++) {
4503195870Sjamie		jsf = pr->pr_flags &
4504195870Sjamie		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
4505195870Sjamie		db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
4506195870Sjamie		    pr_flag_jailsys[fi].disable &&
4507195870Sjamie		      (jsf == pr_flag_jailsys[fi].disable) ? "disable"
4508195870Sjamie		    : (jsf == pr_flag_jailsys[fi].new) ? "new"
4509195870Sjamie		    : "inherit");
4510195870Sjamie	}
4511202123Sbz	db_printf(" allow           = 0x%x", pr->pr_allow);
4512192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
4513192895Sjamie	    fi++)
4514192895Sjamie		if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
4515192895Sjamie			db_printf(" %s", pr_allow_names[fi]);
4516191673Sjamie	db_printf("\n");
4517192895Sjamie	db_printf(" enforce_statfs  = %d\n", pr->pr_enforce_statfs);
4518194118Sjamie	db_printf(" host.hostname   = %s\n", pr->pr_hostname);
4519194118Sjamie	db_printf(" host.domainname = %s\n", pr->pr_domainname);
4520194118Sjamie	db_printf(" host.hostuuid   = %s\n", pr->pr_hostuuid);
4521193066Sjamie	db_printf(" host.hostid     = %lu\n", pr->pr_hostid);
4522185435Sbz#ifdef INET
4523191673Sjamie	db_printf(" ip4s            = %d\n", pr->pr_ip4s);
4524191673Sjamie	for (ii = 0; ii < pr->pr_ip4s; ii++)
4525191673Sjamie		db_printf(" %s %s\n",
4526202123Sbz		    ii == 0 ? "ip4.addr        =" : "                 ",
4527191673Sjamie		    inet_ntoa(pr->pr_ip4[ii]));
4528185435Sbz#endif
4529185435Sbz#ifdef INET6
4530191673Sjamie	db_printf(" ip6s            = %d\n", pr->pr_ip6s);
4531191673Sjamie	for (ii = 0; ii < pr->pr_ip6s; ii++)
4532191673Sjamie		db_printf(" %s %s\n",
4533202123Sbz		    ii == 0 ? "ip6.addr        =" : "                 ",
4534191673Sjamie		    ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4535191673Sjamie#endif
4536191673Sjamie}
4537191673Sjamie
4538191673SjamieDB_SHOW_COMMAND(prison, db_show_prison_command)
4539191673Sjamie{
4540191673Sjamie	struct prison *pr;
4541191673Sjamie
4542191673Sjamie	if (!have_addr) {
4543192895Sjamie		/*
4544192895Sjamie		 * Show all prisons in the list, and prison0 which is not
4545192895Sjamie		 * listed.
4546192895Sjamie		 */
4547192895Sjamie		db_show_prison(&prison0);
4548192895Sjamie		if (!db_pager_quit) {
4549192895Sjamie			TAILQ_FOREACH(pr, &allprison, pr_list) {
4550192895Sjamie				db_show_prison(pr);
4551192895Sjamie				if (db_pager_quit)
4552192895Sjamie					break;
4553192895Sjamie			}
4554191673Sjamie		}
4555191673Sjamie		return;
4556191673Sjamie	}
4557191673Sjamie
4558192895Sjamie	if (addr == 0)
4559192895Sjamie		pr = &prison0;
4560192895Sjamie	else {
4561192895Sjamie		/* Look for a prison with the ID and with references. */
4562191673Sjamie		TAILQ_FOREACH(pr, &allprison, pr_list)
4563192895Sjamie			if (pr->pr_id == addr && pr->pr_ref > 0)
4564191673Sjamie				break;
4565192895Sjamie		if (pr == NULL)
4566192895Sjamie			/* Look again, without requiring a reference. */
4567192895Sjamie			TAILQ_FOREACH(pr, &allprison, pr_list)
4568192895Sjamie				if (pr->pr_id == addr)
4569192895Sjamie					break;
4570192895Sjamie		if (pr == NULL)
4571192895Sjamie			/* Assume address points to a valid prison. */
4572192895Sjamie			pr = (struct prison *)addr;
4573192895Sjamie	}
4574191673Sjamie	db_show_prison(pr);
4575185435Sbz}
4576191673Sjamie
4577185435Sbz#endif /* DDB */
4578