kern_jail.c revision 232278
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 232278 2012-02-29 00:30:18Z 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",
207232278Smm	"allow.mount.procfs",
208192895Sjamie};
209216861Sbzconst size_t pr_allow_names_size = sizeof(pr_allow_names);
210192895Sjamie
211192895Sjamiestatic char *pr_allow_nonames[] = {
212192895Sjamie	"allow.noset_hostname",
213192895Sjamie	"allow.nosysvipc",
214192895Sjamie	"allow.noraw_sockets",
215192895Sjamie	"allow.nochflags",
216192895Sjamie	"allow.nomount",
217192895Sjamie	"allow.noquotas",
218192895Sjamie	"allow.nosocket_af",
219232059Smm	"allow.mount.nodevfs",
220232059Smm	"allow.mount.nonullfs",
221232186Smm	"allow.mount.nozfs",
222232278Smm	"allow.mount.noprocfs",
223192895Sjamie};
224216861Sbzconst size_t pr_allow_nonames_size = sizeof(pr_allow_nonames);
225192895Sjamie
226196002Sjamie#define	JAIL_DEFAULT_ALLOW		PR_ALLOW_SET_HOSTNAME
227196002Sjamie#define	JAIL_DEFAULT_ENFORCE_STATFS	2
228232059Smm#define	JAIL_DEFAULT_DEVFS_RSNUM	0
229192895Sjamiestatic unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
230196002Sjamiestatic int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
231231267Smmstatic int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
232192895Sjamie#if defined(INET) || defined(INET6)
233193865Sjamiestatic unsigned jail_max_af_ips = 255;
234192895Sjamie#endif
235192895Sjamie
236192895Sjamie#ifdef INET
237185435Sbzstatic int
238185435Sbzqcmp_v4(const void *ip1, const void *ip2)
239185435Sbz{
240185435Sbz	in_addr_t iaa, iab;
241185435Sbz
242185435Sbz	/*
243185435Sbz	 * We need to compare in HBO here to get the list sorted as expected
244185435Sbz	 * by the result of the code.  Sorting NBO addresses gives you
245185435Sbz	 * interesting results.  If you do not understand, do not try.
246185435Sbz	 */
247185435Sbz	iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
248185435Sbz	iab = ntohl(((const struct in_addr *)ip2)->s_addr);
249185435Sbz
250185435Sbz	/*
251185435Sbz	 * Do not simply return the difference of the two numbers, the int is
252185435Sbz	 * not wide enough.
253185435Sbz	 */
254185435Sbz	if (iaa > iab)
255185435Sbz		return (1);
256185435Sbz	else if (iaa < iab)
257185435Sbz		return (-1);
258185435Sbz	else
259185435Sbz		return (0);
260185435Sbz}
261185435Sbz#endif
262185435Sbz
263185435Sbz#ifdef INET6
264185435Sbzstatic int
265185435Sbzqcmp_v6(const void *ip1, const void *ip2)
266185435Sbz{
267185435Sbz	const struct in6_addr *ia6a, *ia6b;
268185435Sbz	int i, rc;
269185435Sbz
270185435Sbz	ia6a = (const struct in6_addr *)ip1;
271185435Sbz	ia6b = (const struct in6_addr *)ip2;
272185435Sbz
273185435Sbz	rc = 0;
274190466Sjamie	for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
275185435Sbz		if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
276185435Sbz			rc = 1;
277185435Sbz		else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
278185435Sbz			rc = -1;
279185435Sbz	}
280185435Sbz	return (rc);
281185435Sbz}
282185435Sbz#endif
283185435Sbz
284191673Sjamie/*
285191673Sjamie * struct jail_args {
286191673Sjamie *	struct jail *jail;
287191673Sjamie * };
288191673Sjamie */
289191673Sjamieint
290225617Skmacysys_jail(struct thread *td, struct jail_args *uap)
291185435Sbz{
292191673Sjamie	uint32_t version;
293191673Sjamie	int error;
294192895Sjamie	struct jail j;
295185435Sbz
296191673Sjamie	error = copyin(uap->jail, &version, sizeof(uint32_t));
297191673Sjamie	if (error)
298191673Sjamie		return (error);
299185435Sbz
300191673Sjamie	switch (version) {
301191673Sjamie	case 0:
302191673Sjamie	{
303191673Sjamie		struct jail_v0 j0;
304185435Sbz
305192895Sjamie		/* FreeBSD single IPv4 jails. */
306192895Sjamie		bzero(&j, sizeof(struct jail));
307191673Sjamie		error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
308191673Sjamie		if (error)
309191673Sjamie			return (error);
310192895Sjamie		j.version = j0.version;
311192895Sjamie		j.path = j0.path;
312192895Sjamie		j.hostname = j0.hostname;
313192895Sjamie		j.ip4s = j0.ip_number;
314191673Sjamie		break;
315191673Sjamie	}
316191673Sjamie
317191673Sjamie	case 1:
318185435Sbz		/*
319191673Sjamie		 * Version 1 was used by multi-IPv4 jail implementations
320191673Sjamie		 * that never made it into the official kernel.
321185435Sbz		 */
322191673Sjamie		return (EINVAL);
323185435Sbz
324191673Sjamie	case 2:	/* JAIL_API_VERSION */
325191673Sjamie		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
326191673Sjamie		error = copyin(uap->jail, &j, sizeof(struct jail));
327191673Sjamie		if (error)
328191673Sjamie			return (error);
329192895Sjamie		break;
330192895Sjamie
331192895Sjamie	default:
332192895Sjamie		/* Sci-Fi jails are not supported, sorry. */
333192895Sjamie		return (EINVAL);
334192895Sjamie	}
335192895Sjamie	return (kern_jail(td, &j));
336192895Sjamie}
337192895Sjamie
338192895Sjamieint
339192895Sjamiekern_jail(struct thread *td, struct jail *j)
340192895Sjamie{
341193865Sjamie	struct iovec optiov[2 * (4
342193865Sjamie			    + sizeof(pr_allow_names) / sizeof(pr_allow_names[0])
343193865Sjamie#ifdef INET
344193865Sjamie			    + 1
345193865Sjamie#endif
346193865Sjamie#ifdef INET6
347193865Sjamie			    + 1
348193865Sjamie#endif
349193865Sjamie			    )];
350192895Sjamie	struct uio opt;
351192895Sjamie	char *u_path, *u_hostname, *u_name;
352185435Sbz#ifdef INET
353193865Sjamie	uint32_t ip4s;
354192895Sjamie	struct in_addr *u_ip4;
355192895Sjamie#endif
356192895Sjamie#ifdef INET6
357192895Sjamie	struct in6_addr *u_ip6;
358192895Sjamie#endif
359192895Sjamie	size_t tmplen;
360192895Sjamie	int error, enforce_statfs, fi;
361192895Sjamie
362192895Sjamie	bzero(&optiov, sizeof(optiov));
363192895Sjamie	opt.uio_iov = optiov;
364192895Sjamie	opt.uio_iovcnt = 0;
365192895Sjamie	opt.uio_offset = -1;
366192895Sjamie	opt.uio_resid = -1;
367192895Sjamie	opt.uio_segflg = UIO_SYSSPACE;
368192895Sjamie	opt.uio_rw = UIO_READ;
369192895Sjamie	opt.uio_td = td;
370192895Sjamie
371192895Sjamie	/* Set permissions for top-level jails from sysctls. */
372192895Sjamie	if (!jailed(td->td_ucred)) {
373192895Sjamie		for (fi = 0; fi < sizeof(pr_allow_names) /
374192895Sjamie		     sizeof(pr_allow_names[0]); fi++) {
375192895Sjamie			optiov[opt.uio_iovcnt].iov_base =
376192895Sjamie			    (jail_default_allow & (1 << fi))
377192895Sjamie			    ? pr_allow_names[fi] : pr_allow_nonames[fi];
378192895Sjamie			optiov[opt.uio_iovcnt].iov_len =
379192895Sjamie			    strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
380192895Sjamie			opt.uio_iovcnt += 2;
381192895Sjamie		}
382192895Sjamie		optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
383192895Sjamie		optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
384192895Sjamie		opt.uio_iovcnt++;
385192895Sjamie		enforce_statfs = jail_default_enforce_statfs;
386192895Sjamie		optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
387192895Sjamie		optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
388192895Sjamie		opt.uio_iovcnt++;
389192895Sjamie	}
390192895Sjamie
391192895Sjamie	tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
392192895Sjamie#ifdef INET
393192895Sjamie	ip4s = (j->version == 0) ? 1 : j->ip4s;
394192895Sjamie	if (ip4s > jail_max_af_ips)
395192895Sjamie		return (EINVAL);
396192895Sjamie	tmplen += ip4s * sizeof(struct in_addr);
397191673Sjamie#else
398192895Sjamie	if (j->ip4s > 0)
399192895Sjamie		return (EINVAL);
400191673Sjamie#endif
401191673Sjamie#ifdef INET6
402192895Sjamie	if (j->ip6s > jail_max_af_ips)
403192895Sjamie		return (EINVAL);
404192895Sjamie	tmplen += j->ip6s * sizeof(struct in6_addr);
405191673Sjamie#else
406192895Sjamie	if (j->ip6s > 0)
407192895Sjamie		return (EINVAL);
408191673Sjamie#endif
409192895Sjamie	u_path = malloc(tmplen, M_TEMP, M_WAITOK);
410192895Sjamie	u_hostname = u_path + MAXPATHLEN;
411192895Sjamie	u_name = u_hostname + MAXHOSTNAMELEN;
412191673Sjamie#ifdef INET
413192895Sjamie	u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
414191673Sjamie#endif
415191673Sjamie#ifdef INET6
416191673Sjamie#ifdef INET
417192895Sjamie	u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
418191673Sjamie#else
419192895Sjamie	u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
420191673Sjamie#endif
421191673Sjamie#endif
422192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "path";
423192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("path");
424192895Sjamie	opt.uio_iovcnt++;
425192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_path;
426192895Sjamie	error = copyinstr(j->path, u_path, MAXPATHLEN,
427192895Sjamie	    &optiov[opt.uio_iovcnt].iov_len);
428192895Sjamie	if (error) {
429192895Sjamie		free(u_path, M_TEMP);
430192895Sjamie		return (error);
431192895Sjamie	}
432192895Sjamie	opt.uio_iovcnt++;
433192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "host.hostname";
434192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
435192895Sjamie	opt.uio_iovcnt++;
436192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_hostname;
437192895Sjamie	error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
438192895Sjamie	    &optiov[opt.uio_iovcnt].iov_len);
439192895Sjamie	if (error) {
440192895Sjamie		free(u_path, M_TEMP);
441192895Sjamie		return (error);
442192895Sjamie	}
443192895Sjamie	opt.uio_iovcnt++;
444192895Sjamie	if (j->jailname != NULL) {
445192895Sjamie		optiov[opt.uio_iovcnt].iov_base = "name";
446192895Sjamie		optiov[opt.uio_iovcnt].iov_len = sizeof("name");
447192895Sjamie		opt.uio_iovcnt++;
448192895Sjamie		optiov[opt.uio_iovcnt].iov_base = u_name;
449192895Sjamie		error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
450192895Sjamie		    &optiov[opt.uio_iovcnt].iov_len);
451191673Sjamie		if (error) {
452191673Sjamie			free(u_path, M_TEMP);
453191673Sjamie			return (error);
454191673Sjamie		}
455192895Sjamie		opt.uio_iovcnt++;
456192895Sjamie	}
457191673Sjamie#ifdef INET
458192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
459192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
460192895Sjamie	opt.uio_iovcnt++;
461192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_ip4;
462192895Sjamie	optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
463192895Sjamie	if (j->version == 0)
464192895Sjamie		u_ip4->s_addr = j->ip4s;
465192895Sjamie	else {
466192895Sjamie		error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
467191673Sjamie		if (error) {
468191673Sjamie			free(u_path, M_TEMP);
469191673Sjamie			return (error);
470191673Sjamie		}
471192895Sjamie	}
472192895Sjamie	opt.uio_iovcnt++;
473185435Sbz#endif
474185435Sbz#ifdef INET6
475192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
476192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
477192895Sjamie	opt.uio_iovcnt++;
478192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_ip6;
479192895Sjamie	optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
480192895Sjamie	error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
481192895Sjamie	if (error) {
482192895Sjamie		free(u_path, M_TEMP);
483192895Sjamie		return (error);
484192895Sjamie	}
485192895Sjamie	opt.uio_iovcnt++;
486185435Sbz#endif
487192895Sjamie	KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]),
488192895Sjamie	    ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
489191673Sjamie	error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
490191673Sjamie	free(u_path, M_TEMP);
491191673Sjamie	return (error);
492185435Sbz}
493185435Sbz
494192895Sjamie
495191673Sjamie/*
496191673Sjamie * struct jail_set_args {
497191673Sjamie *	struct iovec *iovp;
498191673Sjamie *	unsigned int iovcnt;
499191673Sjamie *	int flags;
500191673Sjamie * };
501191673Sjamie */
502191673Sjamieint
503225617Skmacysys_jail_set(struct thread *td, struct jail_set_args *uap)
504185435Sbz{
505191673Sjamie	struct uio *auio;
506191673Sjamie	int error;
507191673Sjamie
508191673Sjamie	/* Check that we have an even number of iovecs. */
509191673Sjamie	if (uap->iovcnt & 1)
510191673Sjamie		return (EINVAL);
511191673Sjamie
512191673Sjamie	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
513191673Sjamie	if (error)
514191673Sjamie		return (error);
515191673Sjamie	error = kern_jail_set(td, auio, uap->flags);
516191673Sjamie	free(auio, M_IOV);
517191673Sjamie	return (error);
518191673Sjamie}
519191673Sjamie
520191673Sjamieint
521191673Sjamiekern_jail_set(struct thread *td, struct uio *optuio, int flags)
522191673Sjamie{
523191673Sjamie	struct nameidata nd;
524185435Sbz#ifdef INET
525190466Sjamie	struct in_addr *ip4;
526185435Sbz#endif
527185435Sbz#ifdef INET6
528185435Sbz	struct in6_addr *ip6;
529185435Sbz#endif
530191673Sjamie	struct vfsopt *opt;
531191673Sjamie	struct vfsoptlist *opts;
532196135Sbz	struct prison *pr, *deadpr, *mypr, *ppr, *tpr;
533191673Sjamie	struct vnode *root;
534196835Sjamie	char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
535230407Smm	char *g_path;
536192895Sjamie#if defined(INET) || defined(INET6)
537196135Sbz	struct prison *tppr;
538191673Sjamie	void *op;
539192895Sjamie#endif
540193066Sjamie	unsigned long hid;
541192895Sjamie	size_t namelen, onamelen;
542192895Sjamie	int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos;
543231267Smm	int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
544195870Sjamie	int fi, jid, jsys, len, level;
545231267Smm	int childmax, rsnum, slevel, vfslocked;
546230129Smm	int fullpath_disabled;
547191673Sjamie#if defined(INET) || defined(INET6)
548192895Sjamie	int ii, ij;
549191673Sjamie#endif
550191673Sjamie#ifdef INET
551195974Sjamie	int ip4s, redo_ip4;
552191673Sjamie#endif
553191673Sjamie#ifdef INET6
554195974Sjamie	int ip6s, redo_ip6;
555191673Sjamie#endif
556224290Smckusick	uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
557224290Smckusick	unsigned tallow;
558191673Sjamie	char numbuf[12];
559185435Sbz
560191673Sjamie	error = priv_check(td, PRIV_JAIL_SET);
561191673Sjamie	if (!error && (flags & JAIL_ATTACH))
562191673Sjamie		error = priv_check(td, PRIV_JAIL_ATTACH);
563191673Sjamie	if (error)
564191673Sjamie		return (error);
565192895Sjamie	mypr = ppr = td->td_ucred->cr_prison;
566194762Sjamie	if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
567192895Sjamie		return (EPERM);
568191673Sjamie	if (flags & ~JAIL_SET_MASK)
569191673Sjamie		return (EINVAL);
570191673Sjamie
571185435Sbz	/*
572191673Sjamie	 * Check all the parameters before committing to anything.  Not all
573191673Sjamie	 * errors can be caught early, but we may as well try.  Also, this
574191673Sjamie	 * takes care of some expensive stuff (path lookup) before getting
575191673Sjamie	 * the allprison lock.
576185435Sbz	 *
577191673Sjamie	 * XXX Jails are not filesystems, and jail parameters are not mount
578191673Sjamie	 *     options.  But it makes more sense to re-use the vfsopt code
579191673Sjamie	 *     than duplicate it under a different name.
580185435Sbz	 */
581191673Sjamie	error = vfs_buildopts(optuio, &opts);
582191673Sjamie	if (error)
583191673Sjamie		return (error);
584185435Sbz#ifdef INET
585185435Sbz	ip4 = NULL;
586185435Sbz#endif
587185435Sbz#ifdef INET6
588185435Sbz	ip6 = NULL;
589185435Sbz#endif
590230407Smm	g_path = NULL;
591191673Sjamie
592191673Sjamie	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
593191673Sjamie	if (error == ENOENT)
594191673Sjamie		jid = 0;
595191673Sjamie	else if (error != 0)
596191673Sjamie		goto done_free;
597191673Sjamie
598191673Sjamie	error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
599191673Sjamie	if (error == ENOENT)
600191673Sjamie		gotslevel = 0;
601191673Sjamie	else if (error != 0)
602191673Sjamie		goto done_free;
603191673Sjamie	else
604191673Sjamie		gotslevel = 1;
605191673Sjamie
606194762Sjamie	error =
607194762Sjamie	    vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
608194762Sjamie	if (error == ENOENT)
609194762Sjamie		gotchildmax = 0;
610194762Sjamie	else if (error != 0)
611194762Sjamie		goto done_free;
612194762Sjamie	else
613194762Sjamie		gotchildmax = 1;
614194762Sjamie
615192895Sjamie	error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
616212436Sjamie	if (error == ENOENT)
617212436Sjamie		gotenforce = 0;
618212436Sjamie	else if (error != 0)
619192895Sjamie		goto done_free;
620212436Sjamie	else if (enforce < 0 || enforce > 2) {
621212436Sjamie		error = EINVAL;
622212436Sjamie		goto done_free;
623212436Sjamie	} else
624212436Sjamie		gotenforce = 1;
625192895Sjamie
626231267Smm	error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
627231267Smm	if (error == ENOENT)
628231267Smm		gotrsnum = 0;
629231267Smm	else if (error != 0)
630231267Smm		goto done_free;
631231267Smm	else
632231267Smm		gotrsnum = 1;
633231267Smm
634191673Sjamie	pr_flags = ch_flags = 0;
635192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
636192895Sjamie	    fi++) {
637192895Sjamie		if (pr_flag_names[fi] == NULL)
638192895Sjamie			continue;
639192895Sjamie		vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
640192895Sjamie		vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
641192895Sjamie	}
642191673Sjamie	ch_flags |= pr_flags;
643195870Sjamie	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
644195870Sjamie	    fi++) {
645195870Sjamie		error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys,
646195870Sjamie		    sizeof(jsys));
647195870Sjamie		if (error == ENOENT)
648195870Sjamie			continue;
649195870Sjamie		if (error != 0)
650195870Sjamie			goto done_free;
651195870Sjamie		switch (jsys) {
652195870Sjamie		case JAIL_SYS_DISABLE:
653195870Sjamie			if (!pr_flag_jailsys[fi].disable) {
654195870Sjamie				error = EINVAL;
655195870Sjamie				goto done_free;
656195870Sjamie			}
657195870Sjamie			pr_flags |= pr_flag_jailsys[fi].disable;
658195870Sjamie			break;
659195870Sjamie		case JAIL_SYS_NEW:
660195870Sjamie			pr_flags |= pr_flag_jailsys[fi].new;
661195870Sjamie			break;
662195870Sjamie		case JAIL_SYS_INHERIT:
663195870Sjamie			break;
664195870Sjamie		default:
665195870Sjamie			error = EINVAL;
666195870Sjamie			goto done_free;
667195870Sjamie		}
668195870Sjamie		ch_flags |=
669195870Sjamie		    pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable;
670195870Sjamie	}
671211085Sjamie	if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
672211085Sjamie	    && !(pr_flags & PR_PERSIST)) {
673211085Sjamie		error = EINVAL;
674211085Sjamie		vfs_opterror(opts, "new jail must persist or attach");
675211085Sjamie		goto done_errmsg;
676211085Sjamie	}
677194251Sjamie#ifdef VIMAGE
678194251Sjamie	if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
679194251Sjamie		error = EINVAL;
680194251Sjamie		vfs_opterror(opts, "vnet cannot be changed after creation");
681194251Sjamie		goto done_errmsg;
682194251Sjamie	}
683194251Sjamie#endif
684195974Sjamie#ifdef INET
685195974Sjamie	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
686195974Sjamie		error = EINVAL;
687195974Sjamie		vfs_opterror(opts, "ip4 cannot be changed after creation");
688195974Sjamie		goto done_errmsg;
689195974Sjamie	}
690195974Sjamie#endif
691195974Sjamie#ifdef INET6
692195974Sjamie	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
693195974Sjamie		error = EINVAL;
694195974Sjamie		vfs_opterror(opts, "ip6 cannot be changed after creation");
695195974Sjamie		goto done_errmsg;
696195974Sjamie	}
697195974Sjamie#endif
698191673Sjamie
699192895Sjamie	pr_allow = ch_allow = 0;
700192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
701192895Sjamie	    fi++) {
702192895Sjamie		vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
703192895Sjamie		vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
704192895Sjamie	}
705192895Sjamie	ch_allow |= pr_allow;
706192895Sjamie
707191673Sjamie	error = vfs_getopt(opts, "name", (void **)&name, &len);
708191673Sjamie	if (error == ENOENT)
709191673Sjamie		name = NULL;
710191673Sjamie	else if (error != 0)
711191673Sjamie		goto done_free;
712191673Sjamie	else {
713191673Sjamie		if (len == 0 || name[len - 1] != '\0') {
714191673Sjamie			error = EINVAL;
715191673Sjamie			goto done_free;
716191673Sjamie		}
717191673Sjamie		if (len > MAXHOSTNAMELEN) {
718191673Sjamie			error = ENAMETOOLONG;
719191673Sjamie			goto done_free;
720191673Sjamie		}
721191673Sjamie	}
722191673Sjamie
723191673Sjamie	error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
724191673Sjamie	if (error == ENOENT)
725191673Sjamie		host = NULL;
726191673Sjamie	else if (error != 0)
727191673Sjamie		goto done_free;
728191673Sjamie	else {
729193066Sjamie		ch_flags |= PR_HOST;
730193066Sjamie		pr_flags |= PR_HOST;
731191673Sjamie		if (len == 0 || host[len - 1] != '\0') {
732191673Sjamie			error = EINVAL;
733191673Sjamie			goto done_free;
734191673Sjamie		}
735191673Sjamie		if (len > MAXHOSTNAMELEN) {
736191673Sjamie			error = ENAMETOOLONG;
737191673Sjamie			goto done_free;
738191673Sjamie		}
739191673Sjamie	}
740191673Sjamie
741193066Sjamie	error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
742193066Sjamie	if (error == ENOENT)
743193066Sjamie		domain = NULL;
744193066Sjamie	else if (error != 0)
745193066Sjamie		goto done_free;
746193066Sjamie	else {
747193066Sjamie		ch_flags |= PR_HOST;
748193066Sjamie		pr_flags |= PR_HOST;
749193066Sjamie		if (len == 0 || domain[len - 1] != '\0') {
750193066Sjamie			error = EINVAL;
751193066Sjamie			goto done_free;
752193066Sjamie		}
753193066Sjamie		if (len > MAXHOSTNAMELEN) {
754193066Sjamie			error = ENAMETOOLONG;
755193066Sjamie			goto done_free;
756193066Sjamie		}
757193066Sjamie	}
758193066Sjamie
759193066Sjamie	error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
760193066Sjamie	if (error == ENOENT)
761193066Sjamie		uuid = NULL;
762193066Sjamie	else if (error != 0)
763193066Sjamie		goto done_free;
764193066Sjamie	else {
765193066Sjamie		ch_flags |= PR_HOST;
766193066Sjamie		pr_flags |= PR_HOST;
767193066Sjamie		if (len == 0 || uuid[len - 1] != '\0') {
768193066Sjamie			error = EINVAL;
769193066Sjamie			goto done_free;
770193066Sjamie		}
771193066Sjamie		if (len > HOSTUUIDLEN) {
772193066Sjamie			error = ENAMETOOLONG;
773193066Sjamie			goto done_free;
774193066Sjamie		}
775193066Sjamie	}
776193066Sjamie
777205014Snwhitehorn#ifdef COMPAT_FREEBSD32
778217896Sdchagin	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
779193066Sjamie		uint32_t hid32;
780193066Sjamie
781193066Sjamie		error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
782193066Sjamie		hid = hid32;
783193066Sjamie	} else
784193066Sjamie#endif
785193066Sjamie		error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
786193066Sjamie	if (error == ENOENT)
787193066Sjamie		gothid = 0;
788193066Sjamie	else if (error != 0)
789193066Sjamie		goto done_free;
790193066Sjamie	else {
791193066Sjamie		gothid = 1;
792193066Sjamie		ch_flags |= PR_HOST;
793193066Sjamie		pr_flags |= PR_HOST;
794193066Sjamie	}
795193066Sjamie
796185435Sbz#ifdef INET
797191673Sjamie	error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
798191673Sjamie	if (error == ENOENT)
799195870Sjamie		ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1;
800191673Sjamie	else if (error != 0)
801191673Sjamie		goto done_free;
802191673Sjamie	else if (ip4s & (sizeof(*ip4) - 1)) {
803191673Sjamie		error = EINVAL;
804191673Sjamie		goto done_free;
805192895Sjamie	} else {
806195870Sjamie		ch_flags |= PR_IP4_USER | PR_IP4_DISABLE;
807195870Sjamie		if (ip4s == 0)
808195870Sjamie			pr_flags |= PR_IP4_USER | PR_IP4_DISABLE;
809195870Sjamie		else {
810195870Sjamie			pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER;
811192895Sjamie			ip4s /= sizeof(*ip4);
812192895Sjamie			if (ip4s > jail_max_af_ips) {
813185435Sbz				error = EINVAL;
814192895Sjamie				vfs_opterror(opts, "too many IPv4 addresses");
815192895Sjamie				goto done_errmsg;
816185435Sbz			}
817195974Sjamie			ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
818192895Sjamie			bcopy(op, ip4, ip4s * sizeof(*ip4));
819192895Sjamie			/*
820192895Sjamie			 * IP addresses are all sorted but ip[0] to preserve
821192895Sjamie			 * the primary IP address as given from userland.
822192895Sjamie			 * This special IP is used for unbound outgoing
823202116Sbz			 * connections as well for "loopback" traffic in case
824202116Sbz			 * source address selection cannot find any more fitting
825202116Sbz			 * address to connect from.
826192895Sjamie			 */
827192895Sjamie			if (ip4s > 1)
828192895Sjamie				qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4);
829192895Sjamie			/*
830192895Sjamie			 * Check for duplicate addresses and do some simple
831192895Sjamie			 * zero and broadcast checks. If users give other bogus
832192895Sjamie			 * addresses it is their problem.
833192895Sjamie			 *
834192895Sjamie			 * We do not have to care about byte order for these
835192895Sjamie			 * checks so we will do them in NBO.
836192895Sjamie			 */
837192895Sjamie			for (ii = 0; ii < ip4s; ii++) {
838192895Sjamie				if (ip4[ii].s_addr == INADDR_ANY ||
839192895Sjamie				    ip4[ii].s_addr == INADDR_BROADCAST) {
840192895Sjamie					error = EINVAL;
841192895Sjamie					goto done_free;
842192895Sjamie				}
843192895Sjamie				if ((ii+1) < ip4s &&
844192895Sjamie				    (ip4[0].s_addr == ip4[ii+1].s_addr ||
845192895Sjamie				     ip4[ii].s_addr == ip4[ii+1].s_addr)) {
846192895Sjamie					error = EINVAL;
847192895Sjamie					goto done_free;
848192895Sjamie				}
849192895Sjamie			}
850185435Sbz		}
851191673Sjamie	}
852191673Sjamie#endif
853185435Sbz
854185435Sbz#ifdef INET6
855191673Sjamie	error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
856191673Sjamie	if (error == ENOENT)
857195870Sjamie		ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1;
858191673Sjamie	else if (error != 0)
859191673Sjamie		goto done_free;
860191673Sjamie	else if (ip6s & (sizeof(*ip6) - 1)) {
861191673Sjamie		error = EINVAL;
862191673Sjamie		goto done_free;
863192895Sjamie	} else {
864195870Sjamie		ch_flags |= PR_IP6_USER | PR_IP6_DISABLE;
865195870Sjamie		if (ip6s == 0)
866195870Sjamie			pr_flags |= PR_IP6_USER | PR_IP6_DISABLE;
867195870Sjamie		else {
868195870Sjamie			pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER;
869192895Sjamie			ip6s /= sizeof(*ip6);
870192895Sjamie			if (ip6s > jail_max_af_ips) {
871185435Sbz				error = EINVAL;
872192895Sjamie				vfs_opterror(opts, "too many IPv6 addresses");
873192895Sjamie				goto done_errmsg;
874185435Sbz			}
875195974Sjamie			ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
876192895Sjamie			bcopy(op, ip6, ip6s * sizeof(*ip6));
877192895Sjamie			if (ip6s > 1)
878192895Sjamie				qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6);
879192895Sjamie			for (ii = 0; ii < ip6s; ii++) {
880192895Sjamie				if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
881192895Sjamie					error = EINVAL;
882192895Sjamie					goto done_free;
883192895Sjamie				}
884192895Sjamie				if ((ii+1) < ip6s &&
885192895Sjamie				    (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
886192895Sjamie				     IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
887192895Sjamie				{
888192895Sjamie					error = EINVAL;
889192895Sjamie					goto done_free;
890192895Sjamie				}
891192895Sjamie			}
892185435Sbz		}
893191673Sjamie	}
894185435Sbz#endif
895185435Sbz
896195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6))
897195945Sjamie	if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
898195945Sjamie		error = EINVAL;
899195945Sjamie		vfs_opterror(opts,
900195945Sjamie		    "vnet jails cannot have IP address restrictions");
901195945Sjamie		goto done_errmsg;
902195945Sjamie	}
903195945Sjamie#endif
904195945Sjamie
905230143Smm	fullpath_disabled = 0;
906191673Sjamie	root = NULL;
907191673Sjamie	error = vfs_getopt(opts, "path", (void **)&path, &len);
908191673Sjamie	if (error == ENOENT)
909191673Sjamie		path = NULL;
910191673Sjamie	else if (error != 0)
911191673Sjamie		goto done_free;
912191673Sjamie	else {
913191673Sjamie		if (flags & JAIL_UPDATE) {
914191673Sjamie			error = EINVAL;
915191673Sjamie			vfs_opterror(opts,
916191673Sjamie			    "path cannot be changed after creation");
917191673Sjamie			goto done_errmsg;
918191673Sjamie		}
919191673Sjamie		if (len == 0 || path[len - 1] != '\0') {
920191673Sjamie			error = EINVAL;
921191673Sjamie			goto done_free;
922191673Sjamie		}
923230129Smm		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE, UIO_SYSSPACE,
924230129Smm		    path, td);
925230129Smm		error = namei(&nd);
926230129Smm		if (error)
927230129Smm			goto done_free;
928230129Smm		vfslocked = NDHASGIANT(&nd);
929230129Smm		root = nd.ni_vp;
930230129Smm		NDFREE(&nd, NDF_ONLY_PNBUF);
931230407Smm		g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
932230407Smm		strlcpy(g_path, path, MAXPATHLEN);
933230407Smm		error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
934230407Smm		if (error == 0)
935230407Smm			path = g_path;
936230407Smm		else if (error == ENODEV) {
937230129Smm			/* proceed if sysctl debug.disablefullpath == 1 */
938230129Smm			fullpath_disabled = 1;
939230129Smm			if (len < 2 || (len == 2 && path[0] == '/'))
940230129Smm				path = NULL;
941230407Smm		} else {
942230129Smm			/* exit on other errors */
943230129Smm			VFS_UNLOCK_GIANT(vfslocked);
944230129Smm			goto done_free;
945230129Smm		}
946230129Smm		if (root->v_type != VDIR) {
947230129Smm			error = ENOTDIR;
948230129Smm			vput(root);
949230129Smm			VFS_UNLOCK_GIANT(vfslocked);
950230129Smm			goto done_free;
951230129Smm		}
952230129Smm		VOP_UNLOCK(root, 0);
953230129Smm		VFS_UNLOCK_GIANT(vfslocked);
954230129Smm		if (fullpath_disabled) {
955192895Sjamie			/* Leave room for a real-root full pathname. */
956192895Sjamie			if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
957192895Sjamie			    ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
958192895Sjamie				error = ENAMETOOLONG;
959192895Sjamie				goto done_free;
960192895Sjamie			}
961191673Sjamie		}
962191673Sjamie	}
963185435Sbz
964191673Sjamie	/*
965191673Sjamie	 * Grab the allprison lock before letting modules check their
966191673Sjamie	 * parameters.  Once we have it, do not let go so we'll have a
967191673Sjamie	 * consistent view of the OSD list.
968191673Sjamie	 */
969191673Sjamie	sx_xlock(&allprison_lock);
970191673Sjamie	error = osd_jail_call(NULL, PR_METHOD_CHECK, opts);
971191673Sjamie	if (error)
972191673Sjamie		goto done_unlock_list;
973185435Sbz
974191673Sjamie	/* By now, all parameters should have been noted. */
975191673Sjamie	TAILQ_FOREACH(opt, opts, link) {
976191673Sjamie		if (!opt->seen && strcmp(opt->name, "errmsg")) {
977191673Sjamie			error = EINVAL;
978191673Sjamie			vfs_opterror(opts, "unknown parameter: %s", opt->name);
979191673Sjamie			goto done_unlock_list;
980191673Sjamie		}
981191673Sjamie	}
982191673Sjamie
983185435Sbz	/*
984191673Sjamie	 * See if we are creating a new record or updating an existing one.
985191673Sjamie	 * This abuses the file error codes ENOENT and EEXIST.
986185435Sbz	 */
987191673Sjamie	cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
988191673Sjamie	if (!cuflags) {
989191673Sjamie		error = EINVAL;
990191673Sjamie		vfs_opterror(opts, "no valid operation (create or update)");
991191673Sjamie		goto done_unlock_list;
992191673Sjamie	}
993191673Sjamie	pr = NULL;
994196835Sjamie	namelc = NULL;
995196835Sjamie	if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
996196835Sjamie		namelc = strrchr(name, '.');
997196835Sjamie		jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
998196835Sjamie		if (*p != '\0')
999196835Sjamie			jid = 0;
1000196835Sjamie	}
1001191673Sjamie	if (jid != 0) {
1002192895Sjamie		/*
1003192895Sjamie		 * See if a requested jid already exists.  There is an
1004192895Sjamie		 * information leak here if the jid exists but is not within
1005192895Sjamie		 * the caller's jail hierarchy.  Jail creators will get EEXIST
1006192895Sjamie		 * even though they cannot see the jail, and CREATE | UPDATE
1007192895Sjamie		 * will return ENOENT which is not normally a valid error.
1008192895Sjamie		 */
1009191673Sjamie		if (jid < 0) {
1010191673Sjamie			error = EINVAL;
1011191673Sjamie			vfs_opterror(opts, "negative jid");
1012191673Sjamie			goto done_unlock_list;
1013191673Sjamie		}
1014191673Sjamie		pr = prison_find(jid);
1015191673Sjamie		if (pr != NULL) {
1016192895Sjamie			ppr = pr->pr_parent;
1017191673Sjamie			/* Create: jid must not exist. */
1018191673Sjamie			if (cuflags == JAIL_CREATE) {
1019191673Sjamie				mtx_unlock(&pr->pr_mtx);
1020191673Sjamie				error = EEXIST;
1021191673Sjamie				vfs_opterror(opts, "jail %d already exists",
1022191673Sjamie				    jid);
1023191673Sjamie				goto done_unlock_list;
1024191673Sjamie			}
1025192895Sjamie			if (!prison_ischild(mypr, pr)) {
1026192895Sjamie				mtx_unlock(&pr->pr_mtx);
1027192895Sjamie				pr = NULL;
1028192895Sjamie			} else if (pr->pr_uref == 0) {
1029191673Sjamie				if (!(flags & JAIL_DYING)) {
1030191673Sjamie					mtx_unlock(&pr->pr_mtx);
1031191673Sjamie					error = ENOENT;
1032191673Sjamie					vfs_opterror(opts, "jail %d is dying",
1033191673Sjamie					    jid);
1034191673Sjamie					goto done_unlock_list;
1035191673Sjamie				} else if ((flags & JAIL_ATTACH) ||
1036191673Sjamie				    (pr_flags & PR_PERSIST)) {
1037191673Sjamie					/*
1038191673Sjamie					 * A dying jail might be resurrected
1039191673Sjamie					 * (via attach or persist), but first
1040191673Sjamie					 * it must determine if another jail
1041191673Sjamie					 * has claimed its name.  Accomplish
1042191673Sjamie					 * this by implicitly re-setting the
1043191673Sjamie					 * name.
1044191673Sjamie					 */
1045191673Sjamie					if (name == NULL)
1046192895Sjamie						name = prison_name(mypr, pr);
1047191673Sjamie				}
1048191673Sjamie			}
1049191673Sjamie		}
1050191673Sjamie		if (pr == NULL) {
1051191673Sjamie			/* Update: jid must exist. */
1052191673Sjamie			if (cuflags == JAIL_UPDATE) {
1053191673Sjamie				error = ENOENT;
1054191673Sjamie				vfs_opterror(opts, "jail %d not found", jid);
1055191673Sjamie				goto done_unlock_list;
1056191673Sjamie			}
1057191673Sjamie		}
1058191673Sjamie	}
1059191673Sjamie	/*
1060191673Sjamie	 * If the caller provided a name, look for a jail by that name.
1061191673Sjamie	 * This has different semantics for creates and updates keyed by jid
1062191673Sjamie	 * (where the name must not already exist in a different jail),
1063191673Sjamie	 * and updates keyed by the name itself (where the name must exist
1064191673Sjamie	 * because that is the jail being updated).
1065191673Sjamie	 */
1066191673Sjamie	if (name != NULL) {
1067196835Sjamie		namelc = strrchr(name, '.');
1068196835Sjamie		if (namelc == NULL)
1069196835Sjamie			namelc = name;
1070196835Sjamie		else {
1071192895Sjamie			/*
1072192895Sjamie			 * This is a hierarchical name.  Split it into the
1073192895Sjamie			 * parent and child names, and make sure the parent
1074192895Sjamie			 * exists or matches an already found jail.
1075192895Sjamie			 */
1076196835Sjamie			*namelc = '\0';
1077192895Sjamie			if (pr != NULL) {
1078196835Sjamie				if (strncmp(name, ppr->pr_name, namelc - name)
1079196835Sjamie				    || ppr->pr_name[namelc - name] != '\0') {
1080192895Sjamie					mtx_unlock(&pr->pr_mtx);
1081192895Sjamie					error = EINVAL;
1082192895Sjamie					vfs_opterror(opts,
1083192895Sjamie					    "cannot change jail's parent");
1084192895Sjamie					goto done_unlock_list;
1085192895Sjamie				}
1086192895Sjamie			} else {
1087192895Sjamie				ppr = prison_find_name(mypr, name);
1088192895Sjamie				if (ppr == NULL) {
1089192895Sjamie					error = ENOENT;
1090192895Sjamie					vfs_opterror(opts,
1091192895Sjamie					    "jail \"%s\" not found", name);
1092192895Sjamie					goto done_unlock_list;
1093192895Sjamie				}
1094192895Sjamie				mtx_unlock(&ppr->pr_mtx);
1095192895Sjamie			}
1096196835Sjamie			name = ++namelc;
1097192895Sjamie		}
1098191673Sjamie		if (name[0] != '\0') {
1099192895Sjamie			namelen =
1100192895Sjamie			    (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1101192895Sjamie name_again:
1102191673Sjamie			deadpr = NULL;
1103192895Sjamie			FOREACH_PRISON_CHILD(ppr, tpr) {
1104191673Sjamie				if (tpr != pr && tpr->pr_ref > 0 &&
1105192895Sjamie				    !strcmp(tpr->pr_name + namelen, name)) {
1106191673Sjamie					if (pr == NULL &&
1107191673Sjamie					    cuflags != JAIL_CREATE) {
1108191673Sjamie						mtx_lock(&tpr->pr_mtx);
1109191673Sjamie						if (tpr->pr_ref > 0) {
1110191673Sjamie							/*
1111191673Sjamie							 * Use this jail
1112191673Sjamie							 * for updates.
1113191673Sjamie							 */
1114191673Sjamie							if (tpr->pr_uref > 0) {
1115191673Sjamie								pr = tpr;
1116191673Sjamie								break;
1117191673Sjamie							}
1118191673Sjamie							deadpr = tpr;
1119191673Sjamie						}
1120191673Sjamie						mtx_unlock(&tpr->pr_mtx);
1121191673Sjamie					} else if (tpr->pr_uref > 0) {
1122191673Sjamie						/*
1123191673Sjamie						 * Create, or update(jid):
1124191673Sjamie						 * name must not exist in an
1125192895Sjamie						 * active sibling jail.
1126191673Sjamie						 */
1127191673Sjamie						error = EEXIST;
1128191673Sjamie						if (pr != NULL)
1129191673Sjamie							mtx_unlock(&pr->pr_mtx);
1130191673Sjamie						vfs_opterror(opts,
1131191673Sjamie						   "jail \"%s\" already exists",
1132191673Sjamie						   name);
1133191673Sjamie						goto done_unlock_list;
1134191673Sjamie					}
1135191673Sjamie				}
1136191673Sjamie			}
1137191673Sjamie			/* If no active jail is found, use a dying one. */
1138191673Sjamie			if (deadpr != NULL && pr == NULL) {
1139191673Sjamie				if (flags & JAIL_DYING) {
1140191673Sjamie					mtx_lock(&deadpr->pr_mtx);
1141191673Sjamie					if (deadpr->pr_ref == 0) {
1142191673Sjamie						mtx_unlock(&deadpr->pr_mtx);
1143191673Sjamie						goto name_again;
1144191673Sjamie					}
1145191673Sjamie					pr = deadpr;
1146191673Sjamie				} else if (cuflags == JAIL_UPDATE) {
1147191673Sjamie					error = ENOENT;
1148191673Sjamie					vfs_opterror(opts,
1149191673Sjamie					    "jail \"%s\" is dying", name);
1150191673Sjamie					goto done_unlock_list;
1151191673Sjamie				}
1152191673Sjamie			}
1153191673Sjamie			/* Update: name must exist if no jid. */
1154191673Sjamie			else if (cuflags == JAIL_UPDATE && pr == NULL) {
1155191673Sjamie				error = ENOENT;
1156191673Sjamie				vfs_opterror(opts, "jail \"%s\" not found",
1157191673Sjamie				    name);
1158191673Sjamie				goto done_unlock_list;
1159191673Sjamie			}
1160191673Sjamie		}
1161191673Sjamie	}
1162191673Sjamie	/* Update: must provide a jid or name. */
1163191673Sjamie	else if (cuflags == JAIL_UPDATE && pr == NULL) {
1164191673Sjamie		error = ENOENT;
1165191673Sjamie		vfs_opterror(opts, "update specified no jail");
1166191673Sjamie		goto done_unlock_list;
1167191673Sjamie	}
1168185435Sbz
1169191673Sjamie	/* If there's no prison to update, create a new one and link it in. */
1170191673Sjamie	if (pr == NULL) {
1171194762Sjamie		for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1172194762Sjamie			if (tpr->pr_childcount >= tpr->pr_childmax) {
1173194762Sjamie				error = EPERM;
1174194762Sjamie				vfs_opterror(opts, "prison limit exceeded");
1175194762Sjamie				goto done_unlock_list;
1176194762Sjamie			}
1177191673Sjamie		created = 1;
1178192895Sjamie		mtx_lock(&ppr->pr_mtx);
1179192895Sjamie		if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) {
1180192895Sjamie			mtx_unlock(&ppr->pr_mtx);
1181192895Sjamie			error = ENOENT;
1182192895Sjamie			vfs_opterror(opts, "parent jail went away!");
1183192895Sjamie			goto done_unlock_list;
1184192895Sjamie		}
1185192895Sjamie		ppr->pr_ref++;
1186192895Sjamie		ppr->pr_uref++;
1187192895Sjamie		mtx_unlock(&ppr->pr_mtx);
1188191673Sjamie		pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1189191673Sjamie		if (jid == 0) {
1190191673Sjamie			/* Find the next free jid. */
1191191673Sjamie			jid = lastprid + 1;
1192191673Sjamie findnext:
1193191673Sjamie			if (jid == JAIL_MAX)
1194191673Sjamie				jid = 1;
1195191673Sjamie			TAILQ_FOREACH(tpr, &allprison, pr_list) {
1196191673Sjamie				if (tpr->pr_id < jid)
1197191673Sjamie					continue;
1198191673Sjamie				if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1199191673Sjamie					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1200191673Sjamie					break;
1201191673Sjamie				}
1202191673Sjamie				if (jid == lastprid) {
1203191673Sjamie					error = EAGAIN;
1204191673Sjamie					vfs_opterror(opts,
1205191673Sjamie					    "no available jail IDs");
1206191673Sjamie					free(pr, M_PRISON);
1207192895Sjamie					prison_deref(ppr, PD_DEREF |
1208192895Sjamie					    PD_DEUREF | PD_LIST_XLOCKED);
1209192895Sjamie					goto done_releroot;
1210191673Sjamie				}
1211191673Sjamie				jid++;
1212191673Sjamie				goto findnext;
1213191673Sjamie			}
1214191673Sjamie			lastprid = jid;
1215191673Sjamie		} else {
1216191673Sjamie			/*
1217191673Sjamie			 * The jail already has a jid (that did not yet exist),
1218191673Sjamie			 * so just find where to insert it.
1219191673Sjamie			 */
1220191673Sjamie			TAILQ_FOREACH(tpr, &allprison, pr_list)
1221191673Sjamie				if (tpr->pr_id >= jid) {
1222191673Sjamie					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1223191673Sjamie					break;
1224191673Sjamie				}
1225191673Sjamie		}
1226191673Sjamie		if (tpr == NULL)
1227191673Sjamie			TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1228192895Sjamie		LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1229192895Sjamie		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1230194762Sjamie			tpr->pr_childcount++;
1231185435Sbz
1232192895Sjamie		pr->pr_parent = ppr;
1233191673Sjamie		pr->pr_id = jid;
1234192895Sjamie
1235192895Sjamie		/* Set some default values, and inherit some from the parent. */
1236191673Sjamie		if (name == NULL)
1237191673Sjamie			name = "";
1238191673Sjamie		if (path == NULL) {
1239191673Sjamie			path = "/";
1240192895Sjamie			root = mypr->pr_root;
1241191673Sjamie			vref(root);
1242191673Sjamie		}
1243195944Sjamie		strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1244195944Sjamie		pr->pr_flags |= PR_HOST;
1245195945Sjamie#if defined(INET) || defined(INET6)
1246195945Sjamie#ifdef VIMAGE
1247195945Sjamie		if (!(pr_flags & PR_VNET))
1248195945Sjamie#endif
1249195945Sjamie		{
1250192895Sjamie#ifdef INET
1251195974Sjamie			if (!(ch_flags & PR_IP4_USER))
1252195974Sjamie				pr->pr_flags |=
1253195974Sjamie				    PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE;
1254195974Sjamie			else if (!(pr_flags & PR_IP4_USER)) {
1255195974Sjamie				pr->pr_flags |= ppr->pr_flags & PR_IP4;
1256195974Sjamie				if (ppr->pr_ip4 != NULL) {
1257195974Sjamie					pr->pr_ip4s = ppr->pr_ip4s;
1258195974Sjamie					pr->pr_ip4 = malloc(pr->pr_ip4s *
1259195974Sjamie					    sizeof(struct in_addr), M_PRISON,
1260195974Sjamie					    M_WAITOK);
1261195974Sjamie					bcopy(ppr->pr_ip4, pr->pr_ip4,
1262195974Sjamie					    pr->pr_ip4s * sizeof(*pr->pr_ip4));
1263195974Sjamie				}
1264195974Sjamie			}
1265192895Sjamie#endif
1266192895Sjamie#ifdef INET6
1267195974Sjamie			if (!(ch_flags & PR_IP6_USER))
1268195974Sjamie				pr->pr_flags |=
1269195974Sjamie				    PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE;
1270195974Sjamie			else if (!(pr_flags & PR_IP6_USER)) {
1271195974Sjamie				pr->pr_flags |= ppr->pr_flags & PR_IP6;
1272195974Sjamie				if (ppr->pr_ip6 != NULL) {
1273195974Sjamie					pr->pr_ip6s = ppr->pr_ip6s;
1274195974Sjamie					pr->pr_ip6 = malloc(pr->pr_ip6s *
1275195974Sjamie					    sizeof(struct in6_addr), M_PRISON,
1276195974Sjamie					    M_WAITOK);
1277195974Sjamie					bcopy(ppr->pr_ip6, pr->pr_ip6,
1278195974Sjamie					    pr->pr_ip6s * sizeof(*pr->pr_ip6));
1279195974Sjamie				}
1280195974Sjamie			}
1281192895Sjamie#endif
1282195945Sjamie		}
1283195945Sjamie#endif
1284202468Sbz		/* Source address selection is always on by default. */
1285202468Sbz		pr->pr_flags |= _PR_IP_SADDRSEL;
1286202468Sbz
1287192895Sjamie		pr->pr_securelevel = ppr->pr_securelevel;
1288192895Sjamie		pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1289196002Sjamie		pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
1290232059Smm		pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1291191673Sjamie
1292192895Sjamie		LIST_INIT(&pr->pr_children);
1293192895Sjamie		mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1294191673Sjamie
1295194251Sjamie#ifdef VIMAGE
1296194251Sjamie		/* Allocate a new vnet if specified. */
1297194251Sjamie		pr->pr_vnet = (pr_flags & PR_VNET)
1298194251Sjamie		    ? vnet_alloc() : ppr->pr_vnet;
1299194251Sjamie#endif
1300185435Sbz		/*
1301191673Sjamie		 * Allocate a dedicated cpuset for each jail.
1302191673Sjamie		 * Unlike other initial settings, this may return an erorr.
1303185435Sbz		 */
1304192895Sjamie		error = cpuset_create_root(ppr, &pr->pr_cpuset);
1305191673Sjamie		if (error) {
1306191673Sjamie			prison_deref(pr, PD_LIST_XLOCKED);
1307191673Sjamie			goto done_releroot;
1308191673Sjamie		}
1309185435Sbz
1310191673Sjamie		mtx_lock(&pr->pr_mtx);
1311185435Sbz		/*
1312191673Sjamie		 * New prisons do not yet have a reference, because we do not
1313191673Sjamie		 * want other to see the incomplete prison once the
1314191673Sjamie		 * allprison_lock is downgraded.
1315185435Sbz		 */
1316191673Sjamie	} else {
1317191673Sjamie		created = 0;
1318195974Sjamie		/*
1319195974Sjamie		 * Grab a reference for existing prisons, to ensure they
1320195974Sjamie		 * continue to exist for the duration of the call.
1321195974Sjamie		 */
1322195974Sjamie		pr->pr_ref++;
1323195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6))
1324195945Sjamie		if ((pr->pr_flags & PR_VNET) &&
1325195945Sjamie		    (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1326195945Sjamie			error = EINVAL;
1327195945Sjamie			vfs_opterror(opts,
1328195945Sjamie			    "vnet jails cannot have IP address restrictions");
1329195945Sjamie			goto done_deref_locked;
1330195945Sjamie		}
1331195945Sjamie#endif
1332195974Sjamie#ifdef INET
1333195974Sjamie		if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1334195974Sjamie			error = EINVAL;
1335195974Sjamie			vfs_opterror(opts,
1336195974Sjamie			    "ip4 cannot be changed after creation");
1337195974Sjamie			goto done_deref_locked;
1338195974Sjamie		}
1339195974Sjamie#endif
1340195974Sjamie#ifdef INET6
1341195974Sjamie		if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1342195974Sjamie			error = EINVAL;
1343195974Sjamie			vfs_opterror(opts,
1344195974Sjamie			    "ip6 cannot be changed after creation");
1345195974Sjamie			goto done_deref_locked;
1346195974Sjamie		}
1347195974Sjamie#endif
1348191673Sjamie	}
1349185435Sbz
1350191673Sjamie	/* Do final error checking before setting anything. */
1351192895Sjamie	if (gotslevel) {
1352192895Sjamie		if (slevel < ppr->pr_securelevel) {
1353192895Sjamie			error = EPERM;
1354192895Sjamie			goto done_deref_locked;
1355192895Sjamie		}
1356192895Sjamie	}
1357194762Sjamie	if (gotchildmax) {
1358194762Sjamie		if (childmax >= ppr->pr_childmax) {
1359194762Sjamie			error = EPERM;
1360194762Sjamie			goto done_deref_locked;
1361194762Sjamie		}
1362194762Sjamie	}
1363192895Sjamie	if (gotenforce) {
1364192895Sjamie		if (enforce < ppr->pr_enforce_statfs) {
1365192895Sjamie			error = EPERM;
1366192895Sjamie			goto done_deref_locked;
1367192895Sjamie		}
1368192895Sjamie	}
1369231267Smm	if (gotrsnum) {
1370231267Smm		/*
1371231267Smm		 * devfs_rsnum is a uint16_t
1372231267Smm		 */
1373232059Smm		if (rsnum < 0 || rsnum > 65535) {
1374231267Smm			error = EINVAL;
1375231267Smm			goto done_deref_locked;
1376231267Smm		}
1377231267Smm		/*
1378232059Smm		 * Nested jails always inherit parent's devfs ruleset
1379231267Smm		 */
1380231267Smm		if (jailed(td->td_ucred)) {
1381231267Smm			if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
1382231267Smm				error = EPERM;
1383231267Smm				goto done_deref_locked;
1384232059Smm			} else
1385231267Smm				rsnum = ppr->pr_devfs_rsnum;
1386231267Smm		}
1387231267Smm	}
1388185435Sbz#ifdef INET
1389195974Sjamie	if (ip4s > 0) {
1390192895Sjamie		if (ppr->pr_flags & PR_IP4) {
1391195974Sjamie			/*
1392195974Sjamie			 * Make sure the new set of IP addresses is a
1393195974Sjamie			 * subset of the parent's list.  Don't worry
1394195974Sjamie			 * about the parent being unlocked, as any
1395195974Sjamie			 * setting is done with allprison_lock held.
1396195974Sjamie			 */
1397195974Sjamie			for (ij = 0; ij < ppr->pr_ip4s; ij++)
1398195974Sjamie				if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1399195974Sjamie					break;
1400195974Sjamie			if (ij == ppr->pr_ip4s) {
1401195974Sjamie				error = EPERM;
1402195974Sjamie				goto done_deref_locked;
1403195974Sjamie			}
1404195974Sjamie			if (ip4s > 1) {
1405195974Sjamie				for (ii = ij = 1; ii < ip4s; ii++) {
1406195974Sjamie					if (ip4[ii].s_addr ==
1407195974Sjamie					    ppr->pr_ip4[0].s_addr)
1408195974Sjamie						continue;
1409195974Sjamie					for (; ij < ppr->pr_ip4s; ij++)
1410195974Sjamie						if (ip4[ii].s_addr ==
1411195974Sjamie						    ppr->pr_ip4[ij].s_addr)
1412195974Sjamie							break;
1413195974Sjamie					if (ij == ppr->pr_ip4s)
1414195974Sjamie						break;
1415192895Sjamie				}
1416192895Sjamie				if (ij == ppr->pr_ip4s) {
1417192895Sjamie					error = EPERM;
1418192895Sjamie					goto done_deref_locked;
1419192895Sjamie				}
1420192895Sjamie			}
1421192895Sjamie		}
1422195974Sjamie		/*
1423195974Sjamie		 * Check for conflicting IP addresses.  We permit them
1424195974Sjamie		 * if there is no more than one IP on each jail.  If
1425195974Sjamie		 * there is a duplicate on a jail with more than one
1426195974Sjamie		 * IP stop checking and return error.
1427195974Sjamie		 */
1428195974Sjamie		tppr = ppr;
1429195945Sjamie#ifdef VIMAGE
1430195974Sjamie		for (; tppr != &prison0; tppr = tppr->pr_parent)
1431195974Sjamie			if (tppr->pr_flags & PR_VNET)
1432195974Sjamie				break;
1433195945Sjamie#endif
1434195974Sjamie		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1435195974Sjamie			if (tpr == pr ||
1436195945Sjamie#ifdef VIMAGE
1437195974Sjamie			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1438195945Sjamie#endif
1439195974Sjamie			    tpr->pr_uref == 0) {
1440192895Sjamie				descend = 0;
1441195974Sjamie				continue;
1442195974Sjamie			}
1443195974Sjamie			if (!(tpr->pr_flags & PR_IP4_USER))
1444195974Sjamie				continue;
1445195974Sjamie			descend = 0;
1446195974Sjamie			if (tpr->pr_ip4 == NULL ||
1447195974Sjamie			    (ip4s == 1 && tpr->pr_ip4s == 1))
1448195974Sjamie				continue;
1449195974Sjamie			for (ii = 0; ii < ip4s; ii++) {
1450195974Sjamie				if (_prison_check_ip4(tpr, &ip4[ii]) == 0) {
1451195974Sjamie					error = EADDRINUSE;
1452195974Sjamie					vfs_opterror(opts,
1453195974Sjamie					    "IPv4 addresses clash");
1454195974Sjamie					goto done_deref_locked;
1455192895Sjamie				}
1456192895Sjamie			}
1457192895Sjamie		}
1458192895Sjamie	}
1459185435Sbz#endif
1460191673Sjamie#ifdef INET6
1461195974Sjamie	if (ip6s > 0) {
1462192895Sjamie		if (ppr->pr_flags & PR_IP6) {
1463195974Sjamie			/*
1464195974Sjamie			 * Make sure the new set of IP addresses is a
1465195974Sjamie			 * subset of the parent's list.
1466195974Sjamie			 */
1467195974Sjamie			for (ij = 0; ij < ppr->pr_ip6s; ij++)
1468195974Sjamie				if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1469195974Sjamie				    &ppr->pr_ip6[ij]))
1470195974Sjamie					break;
1471195974Sjamie			if (ij == ppr->pr_ip6s) {
1472195974Sjamie				error = EPERM;
1473195974Sjamie				goto done_deref_locked;
1474195974Sjamie			}
1475195974Sjamie			if (ip6s > 1) {
1476195974Sjamie				for (ii = ij = 1; ii < ip6s; ii++) {
1477195974Sjamie					if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1478195974Sjamie					     &ppr->pr_ip6[0]))
1479195974Sjamie						continue;
1480195974Sjamie					for (; ij < ppr->pr_ip6s; ij++)
1481195974Sjamie						if (IN6_ARE_ADDR_EQUAL(
1482195974Sjamie						    &ip6[ii], &ppr->pr_ip6[ij]))
1483195974Sjamie							break;
1484195974Sjamie					if (ij == ppr->pr_ip6s)
1485195974Sjamie						break;
1486192895Sjamie				}
1487192895Sjamie				if (ij == ppr->pr_ip6s) {
1488192895Sjamie					error = EPERM;
1489192895Sjamie					goto done_deref_locked;
1490192895Sjamie				}
1491192895Sjamie			}
1492192895Sjamie		}
1493195974Sjamie		/* Check for conflicting IP addresses. */
1494195974Sjamie		tppr = ppr;
1495195945Sjamie#ifdef VIMAGE
1496195974Sjamie		for (; tppr != &prison0; tppr = tppr->pr_parent)
1497195974Sjamie			if (tppr->pr_flags & PR_VNET)
1498195974Sjamie				break;
1499195945Sjamie#endif
1500195974Sjamie		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1501195974Sjamie			if (tpr == pr ||
1502195945Sjamie#ifdef VIMAGE
1503195974Sjamie			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1504195945Sjamie#endif
1505195974Sjamie			    tpr->pr_uref == 0) {
1506192895Sjamie				descend = 0;
1507195974Sjamie				continue;
1508195974Sjamie			}
1509195974Sjamie			if (!(tpr->pr_flags & PR_IP6_USER))
1510195974Sjamie				continue;
1511195974Sjamie			descend = 0;
1512195974Sjamie			if (tpr->pr_ip6 == NULL ||
1513195974Sjamie			    (ip6s == 1 && tpr->pr_ip6s == 1))
1514195974Sjamie				continue;
1515195974Sjamie			for (ii = 0; ii < ip6s; ii++) {
1516195974Sjamie				if (_prison_check_ip6(tpr, &ip6[ii]) == 0) {
1517195974Sjamie					error = EADDRINUSE;
1518195974Sjamie					vfs_opterror(opts,
1519195974Sjamie					    "IPv6 addresses clash");
1520195974Sjamie					goto done_deref_locked;
1521192895Sjamie				}
1522192895Sjamie			}
1523191673Sjamie		}
1524192895Sjamie	}
1525191673Sjamie#endif
1526192895Sjamie	onamelen = namelen = 0;
1527192895Sjamie	if (name != NULL) {
1528191673Sjamie		/* Give a default name of the jid. */
1529191673Sjamie		if (name[0] == '\0')
1530191673Sjamie			snprintf(name = numbuf, sizeof(numbuf), "%d", jid);
1531196835Sjamie		else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid &&
1532196835Sjamie		    *p == '\0')) {
1533191673Sjamie			error = EINVAL;
1534196835Sjamie			vfs_opterror(opts,
1535196835Sjamie			    "name cannot be numeric (unless it is the jid)");
1536192895Sjamie			goto done_deref_locked;
1537191673Sjamie		}
1538191673Sjamie		/*
1539192895Sjamie		 * Make sure the name isn't too long for the prison or its
1540192895Sjamie		 * children.
1541191673Sjamie		 */
1542192895Sjamie		onamelen = strlen(pr->pr_name);
1543192895Sjamie		namelen = strlen(name);
1544192895Sjamie		if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) {
1545192895Sjamie			error = ENAMETOOLONG;
1546192895Sjamie			goto done_deref_locked;
1547192895Sjamie		}
1548192895Sjamie		FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1549192895Sjamie			if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1550192895Sjamie			    sizeof(pr->pr_name)) {
1551192895Sjamie				error = ENAMETOOLONG;
1552192895Sjamie				goto done_deref_locked;
1553192895Sjamie			}
1554192895Sjamie		}
1555191673Sjamie	}
1556192895Sjamie	if (pr_allow & ~ppr->pr_allow) {
1557192895Sjamie		error = EPERM;
1558192895Sjamie		goto done_deref_locked;
1559192895Sjamie	}
1560185435Sbz
1561191673Sjamie	/* Set the parameters of the prison. */
1562191673Sjamie#ifdef INET
1563192895Sjamie	redo_ip4 = 0;
1564195974Sjamie	if (pr_flags & PR_IP4_USER) {
1565195974Sjamie		pr->pr_flags |= PR_IP4;
1566195974Sjamie		free(pr->pr_ip4, M_PRISON);
1567195974Sjamie		pr->pr_ip4s = ip4s;
1568195974Sjamie		pr->pr_ip4 = ip4;
1569195974Sjamie		ip4 = NULL;
1570192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1571195945Sjamie#ifdef VIMAGE
1572195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1573195945Sjamie				descend = 0;
1574195945Sjamie				continue;
1575195945Sjamie			}
1576195945Sjamie#endif
1577192895Sjamie			if (prison_restrict_ip4(tpr, NULL)) {
1578192895Sjamie				redo_ip4 = 1;
1579192895Sjamie				descend = 0;
1580192895Sjamie			}
1581192895Sjamie		}
1582185435Sbz	}
1583191673Sjamie#endif
1584191673Sjamie#ifdef INET6
1585192895Sjamie	redo_ip6 = 0;
1586195974Sjamie	if (pr_flags & PR_IP6_USER) {
1587195974Sjamie		pr->pr_flags |= PR_IP6;
1588195974Sjamie		free(pr->pr_ip6, M_PRISON);
1589195974Sjamie		pr->pr_ip6s = ip6s;
1590195974Sjamie		pr->pr_ip6 = ip6;
1591195974Sjamie		ip6 = NULL;
1592192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1593195945Sjamie#ifdef VIMAGE
1594195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1595195945Sjamie				descend = 0;
1596195945Sjamie				continue;
1597195945Sjamie			}
1598195945Sjamie#endif
1599192895Sjamie			if (prison_restrict_ip6(tpr, NULL)) {
1600192895Sjamie				redo_ip6 = 1;
1601192895Sjamie				descend = 0;
1602192895Sjamie			}
1603192895Sjamie		}
1604191673Sjamie	}
1605191673Sjamie#endif
1606192895Sjamie	if (gotslevel) {
1607191673Sjamie		pr->pr_securelevel = slevel;
1608192895Sjamie		/* Set all child jails to be at least this level. */
1609192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1610192895Sjamie			if (tpr->pr_securelevel < slevel)
1611192895Sjamie				tpr->pr_securelevel = slevel;
1612192895Sjamie	}
1613194762Sjamie	if (gotchildmax) {
1614194762Sjamie		pr->pr_childmax = childmax;
1615194762Sjamie		/* Set all child jails to under this limit. */
1616194762Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1617194762Sjamie			if (tpr->pr_childmax > childmax - level)
1618194762Sjamie				tpr->pr_childmax = childmax > level
1619194762Sjamie				    ? childmax - level : 0;
1620194762Sjamie	}
1621192895Sjamie	if (gotenforce) {
1622192895Sjamie		pr->pr_enforce_statfs = enforce;
1623192895Sjamie		/* Pass this restriction on to the children. */
1624192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1625192895Sjamie			if (tpr->pr_enforce_statfs < enforce)
1626192895Sjamie				tpr->pr_enforce_statfs = enforce;
1627192895Sjamie	}
1628231267Smm	if (gotrsnum) {
1629231267Smm		pr->pr_devfs_rsnum = rsnum;
1630231267Smm		/* Pass this restriction on to the children. */
1631231267Smm		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1632232059Smm			tpr->pr_devfs_rsnum = rsnum;
1633231267Smm	}
1634192895Sjamie	if (name != NULL) {
1635192895Sjamie		if (ppr == &prison0)
1636192895Sjamie			strlcpy(pr->pr_name, name, sizeof(pr->pr_name));
1637192895Sjamie		else
1638192895Sjamie			snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1639192895Sjamie			    ppr->pr_name, name);
1640192895Sjamie		/* Change this component of child names. */
1641192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1642192895Sjamie			bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1643192895Sjamie			    strlen(tpr->pr_name + onamelen) + 1);
1644192895Sjamie			bcopy(pr->pr_name, tpr->pr_name, namelen);
1645192895Sjamie		}
1646192895Sjamie	}
1647191673Sjamie	if (path != NULL) {
1648192895Sjamie		/* Try to keep a real-rooted full pathname. */
1649230129Smm		if (fullpath_disabled && path[0] == '/' &&
1650230129Smm		    strcmp(mypr->pr_path, "/"))
1651192895Sjamie			snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
1652192895Sjamie			    mypr->pr_path, path);
1653192895Sjamie		else
1654192895Sjamie			strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1655191673Sjamie		pr->pr_root = root;
1656191673Sjamie	}
1657193066Sjamie	if (PR_HOST & ch_flags & ~pr_flags) {
1658193066Sjamie		if (pr->pr_flags & PR_HOST) {
1659193066Sjamie			/*
1660193066Sjamie			 * Copy the parent's host info.  As with pr_ip4 above,
1661193066Sjamie			 * the lack of a lock on the parent is not a problem;
1662193066Sjamie			 * it is always set with allprison_lock at least
1663193066Sjamie			 * shared, and is held exclusively here.
1664193066Sjamie			 */
1665194118Sjamie			strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1666194118Sjamie			    sizeof(pr->pr_hostname));
1667194118Sjamie			strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1668194118Sjamie			    sizeof(pr->pr_domainname));
1669194118Sjamie			strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1670194118Sjamie			    sizeof(pr->pr_hostuuid));
1671193066Sjamie			pr->pr_hostid = pr->pr_parent->pr_hostid;
1672193066Sjamie		}
1673193066Sjamie	} else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1674193066Sjamie		/* Set this prison, and any descendants without PR_HOST. */
1675193066Sjamie		if (host != NULL)
1676194118Sjamie			strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1677193066Sjamie		if (domain != NULL)
1678194118Sjamie			strlcpy(pr->pr_domainname, domain,
1679194118Sjamie			    sizeof(pr->pr_domainname));
1680193066Sjamie		if (uuid != NULL)
1681194118Sjamie			strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1682193066Sjamie		if (gothid)
1683193066Sjamie			pr->pr_hostid = hid;
1684193066Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1685193066Sjamie			if (tpr->pr_flags & PR_HOST)
1686193066Sjamie				descend = 0;
1687193066Sjamie			else {
1688193066Sjamie				if (host != NULL)
1689194118Sjamie					strlcpy(tpr->pr_hostname,
1690194118Sjamie					    pr->pr_hostname,
1691194118Sjamie					    sizeof(tpr->pr_hostname));
1692193066Sjamie				if (domain != NULL)
1693194118Sjamie					strlcpy(tpr->pr_domainname,
1694194118Sjamie					    pr->pr_domainname,
1695194118Sjamie					    sizeof(tpr->pr_domainname));
1696193066Sjamie				if (uuid != NULL)
1697194118Sjamie					strlcpy(tpr->pr_hostuuid,
1698194118Sjamie					    pr->pr_hostuuid,
1699194118Sjamie					    sizeof(tpr->pr_hostuuid));
1700193066Sjamie				if (gothid)
1701193066Sjamie					tpr->pr_hostid = hid;
1702193066Sjamie			}
1703193066Sjamie		}
1704193066Sjamie	}
1705192895Sjamie	if ((tallow = ch_allow & ~pr_allow)) {
1706192895Sjamie		/* Clear allow bits in all children. */
1707192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1708192895Sjamie			tpr->pr_allow &= ~tallow;
1709192895Sjamie	}
1710192895Sjamie	pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1711191673Sjamie	/*
1712191673Sjamie	 * Persistent prisons get an extra reference, and prisons losing their
1713191673Sjamie	 * persist flag lose that reference.  Only do this for existing prisons
1714191673Sjamie	 * for now, so new ones will remain unseen until after the module
1715191673Sjamie	 * handlers have completed.
1716191673Sjamie	 */
1717191673Sjamie	if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1718191673Sjamie		if (pr_flags & PR_PERSIST) {
1719191673Sjamie			pr->pr_ref++;
1720191673Sjamie			pr->pr_uref++;
1721191673Sjamie		} else {
1722191673Sjamie			pr->pr_ref--;
1723191673Sjamie			pr->pr_uref--;
1724191673Sjamie		}
1725191673Sjamie	}
1726191673Sjamie	pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1727191673Sjamie	mtx_unlock(&pr->pr_mtx);
1728185435Sbz
1729221362Strasz#ifdef RACCT
1730221362Strasz	if (created)
1731221362Strasz		prison_racct_attach(pr);
1732221362Strasz#endif
1733221362Strasz
1734192895Sjamie	/* Locks may have prevented a complete restriction of child IP
1735192895Sjamie	 * addresses.  If so, allocate some more memory and try again.
1736192895Sjamie	 */
1737192895Sjamie#ifdef INET
1738192895Sjamie	while (redo_ip4) {
1739192895Sjamie		ip4s = pr->pr_ip4s;
1740192895Sjamie		ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1741192895Sjamie		mtx_lock(&pr->pr_mtx);
1742192895Sjamie		redo_ip4 = 0;
1743192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1744195945Sjamie#ifdef VIMAGE
1745195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1746195945Sjamie				descend = 0;
1747195945Sjamie				continue;
1748195945Sjamie			}
1749195945Sjamie#endif
1750192895Sjamie			if (prison_restrict_ip4(tpr, ip4)) {
1751192895Sjamie				if (ip4 != NULL)
1752192895Sjamie					ip4 = NULL;
1753192895Sjamie				else
1754192895Sjamie					redo_ip4 = 1;
1755192895Sjamie			}
1756192895Sjamie		}
1757192895Sjamie		mtx_unlock(&pr->pr_mtx);
1758192895Sjamie	}
1759192895Sjamie#endif
1760192895Sjamie#ifdef INET6
1761192895Sjamie	while (redo_ip6) {
1762192895Sjamie		ip6s = pr->pr_ip6s;
1763192895Sjamie		ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1764192895Sjamie		mtx_lock(&pr->pr_mtx);
1765192895Sjamie		redo_ip6 = 0;
1766192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1767195945Sjamie#ifdef VIMAGE
1768195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1769195945Sjamie				descend = 0;
1770195945Sjamie				continue;
1771195945Sjamie			}
1772195945Sjamie#endif
1773192895Sjamie			if (prison_restrict_ip6(tpr, ip6)) {
1774192895Sjamie				if (ip6 != NULL)
1775192895Sjamie					ip6 = NULL;
1776192895Sjamie				else
1777192895Sjamie					redo_ip6 = 1;
1778192895Sjamie			}
1779192895Sjamie		}
1780192895Sjamie		mtx_unlock(&pr->pr_mtx);
1781192895Sjamie	}
1782192895Sjamie#endif
1783192895Sjamie
1784191673Sjamie	/* Let the modules do their work. */
1785191673Sjamie	sx_downgrade(&allprison_lock);
1786191673Sjamie	if (created) {
1787191673Sjamie		error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1788191673Sjamie		if (error) {
1789191673Sjamie			prison_deref(pr, PD_LIST_SLOCKED);
1790191673Sjamie			goto done_errmsg;
1791191673Sjamie		}
1792191673Sjamie	}
1793191673Sjamie	error = osd_jail_call(pr, PR_METHOD_SET, opts);
1794191673Sjamie	if (error) {
1795191673Sjamie		prison_deref(pr, created
1796191673Sjamie		    ? PD_LIST_SLOCKED
1797191673Sjamie		    : PD_DEREF | PD_LIST_SLOCKED);
1798191673Sjamie		goto done_errmsg;
1799191673Sjamie	}
1800191673Sjamie
1801191673Sjamie	/* Attach this process to the prison if requested. */
1802191673Sjamie	if (flags & JAIL_ATTACH) {
1803191673Sjamie		mtx_lock(&pr->pr_mtx);
1804191673Sjamie		error = do_jail_attach(td, pr);
1805191673Sjamie		if (error) {
1806191673Sjamie			vfs_opterror(opts, "attach failed");
1807191673Sjamie			if (!created)
1808191673Sjamie				prison_deref(pr, PD_DEREF);
1809191673Sjamie			goto done_errmsg;
1810191673Sjamie		}
1811191673Sjamie	}
1812191673Sjamie
1813191673Sjamie	/*
1814191673Sjamie	 * Now that it is all there, drop the temporary reference from existing
1815191673Sjamie	 * prisons.  Or add a reference to newly created persistent prisons
1816191673Sjamie	 * (which was not done earlier so that the prison would not be publicly
1817191673Sjamie	 * visible).
1818191673Sjamie	 */
1819191673Sjamie	if (!created) {
1820191673Sjamie		prison_deref(pr, (flags & JAIL_ATTACH)
1821191673Sjamie		    ? PD_DEREF
1822191673Sjamie		    : PD_DEREF | PD_LIST_SLOCKED);
1823191673Sjamie	} else {
1824191673Sjamie		if (pr_flags & PR_PERSIST) {
1825191673Sjamie			mtx_lock(&pr->pr_mtx);
1826191673Sjamie			pr->pr_ref++;
1827191673Sjamie			pr->pr_uref++;
1828191673Sjamie			mtx_unlock(&pr->pr_mtx);
1829191673Sjamie		}
1830191673Sjamie		if (!(flags & JAIL_ATTACH))
1831191673Sjamie			sx_sunlock(&allprison_lock);
1832191673Sjamie	}
1833191673Sjamie	td->td_retval[0] = pr->pr_id;
1834191673Sjamie	goto done_errmsg;
1835191673Sjamie
1836192895Sjamie done_deref_locked:
1837192895Sjamie	prison_deref(pr, created
1838192895Sjamie	    ? PD_LOCKED | PD_LIST_XLOCKED
1839192895Sjamie	    : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
1840192895Sjamie	goto done_releroot;
1841191673Sjamie done_unlock_list:
1842191673Sjamie	sx_xunlock(&allprison_lock);
1843191673Sjamie done_releroot:
1844191673Sjamie	if (root != NULL) {
1845191673Sjamie		vfslocked = VFS_LOCK_GIANT(root->v_mount);
1846191673Sjamie		vrele(root);
1847191673Sjamie		VFS_UNLOCK_GIANT(vfslocked);
1848191673Sjamie	}
1849191673Sjamie done_errmsg:
1850191673Sjamie	if (error) {
1851191673Sjamie		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
1852191673Sjamie		if (errmsg_len > 0) {
1853191673Sjamie			errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1854191673Sjamie			if (errmsg_pos > 0) {
1855191673Sjamie				if (optuio->uio_segflg == UIO_SYSSPACE)
1856191673Sjamie					bcopy(errmsg,
1857191673Sjamie					   optuio->uio_iov[errmsg_pos].iov_base,
1858191673Sjamie					   errmsg_len);
1859191673Sjamie				else
1860191673Sjamie					copyout(errmsg,
1861191673Sjamie					   optuio->uio_iov[errmsg_pos].iov_base,
1862191673Sjamie					   errmsg_len);
1863191673Sjamie			}
1864191673Sjamie		}
1865191673Sjamie	}
1866191673Sjamie done_free:
1867191673Sjamie#ifdef INET
1868191673Sjamie	free(ip4, M_PRISON);
1869191673Sjamie#endif
1870191673Sjamie#ifdef INET6
1871191673Sjamie	free(ip6, M_PRISON);
1872191673Sjamie#endif
1873230407Smm	if (g_path != NULL)
1874230407Smm		free(g_path, M_TEMP);
1875191673Sjamie	vfs_freeopts(opts);
1876191673Sjamie	return (error);
1877191673Sjamie}
1878191673Sjamie
1879191673Sjamie
188082710Sdillon/*
1881191673Sjamie * struct jail_get_args {
1882191673Sjamie *	struct iovec *iovp;
1883191673Sjamie *	unsigned int iovcnt;
1884191673Sjamie *	int flags;
1885114168Smike * };
188682710Sdillon */
188746155Sphkint
1888225617Skmacysys_jail_get(struct thread *td, struct jail_get_args *uap)
188946155Sphk{
1890191673Sjamie	struct uio *auio;
1891185435Sbz	int error;
1892185435Sbz
1893191673Sjamie	/* Check that we have an even number of iovecs. */
1894191673Sjamie	if (uap->iovcnt & 1)
1895191673Sjamie		return (EINVAL);
1896191673Sjamie
1897191673Sjamie	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1898185435Sbz	if (error)
1899185435Sbz		return (error);
1900191673Sjamie	error = kern_jail_get(td, auio, uap->flags);
1901191673Sjamie	if (error == 0)
1902191673Sjamie		error = copyout(auio->uio_iov, uap->iovp,
1903191673Sjamie		    uap->iovcnt * sizeof (struct iovec));
1904191673Sjamie	free(auio, M_IOV);
1905191673Sjamie	return (error);
1906191673Sjamie}
1907185435Sbz
1908191673Sjamieint
1909191673Sjamiekern_jail_get(struct thread *td, struct uio *optuio, int flags)
1910191673Sjamie{
1911192895Sjamie	struct prison *pr, *mypr;
1912191673Sjamie	struct vfsopt *opt;
1913191673Sjamie	struct vfsoptlist *opts;
1914191673Sjamie	char *errmsg, *name;
1915192895Sjamie	int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
1916185435Sbz
1917191673Sjamie	if (flags & ~JAIL_GET_MASK)
1918191673Sjamie		return (EINVAL);
1919185435Sbz
1920191673Sjamie	/* Get the parameter list. */
1921191673Sjamie	error = vfs_buildopts(optuio, &opts);
1922191673Sjamie	if (error)
1923191673Sjamie		return (error);
1924191673Sjamie	errmsg_pos = vfs_getopt_pos(opts, "errmsg");
1925192895Sjamie	mypr = td->td_ucred->cr_prison;
1926185435Sbz
1927191673Sjamie	/*
1928191673Sjamie	 * Find the prison specified by one of: lastjid, jid, name.
1929191673Sjamie	 */
1930191673Sjamie	sx_slock(&allprison_lock);
1931191673Sjamie	error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
1932191673Sjamie	if (error == 0) {
1933191673Sjamie		TAILQ_FOREACH(pr, &allprison, pr_list) {
1934192895Sjamie			if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
1935191673Sjamie				mtx_lock(&pr->pr_mtx);
1936191673Sjamie				if (pr->pr_ref > 0 &&
1937191673Sjamie				    (pr->pr_uref > 0 || (flags & JAIL_DYING)))
1938191673Sjamie					break;
1939191673Sjamie				mtx_unlock(&pr->pr_mtx);
1940191673Sjamie			}
1941191673Sjamie		}
1942191673Sjamie		if (pr != NULL)
1943191673Sjamie			goto found_prison;
1944191673Sjamie		error = ENOENT;
1945191673Sjamie		vfs_opterror(opts, "no jail after %d", jid);
1946191673Sjamie		goto done_unlock_list;
1947191673Sjamie	} else if (error != ENOENT)
1948191673Sjamie		goto done_unlock_list;
1949185435Sbz
1950191673Sjamie	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1951191673Sjamie	if (error == 0) {
1952191673Sjamie		if (jid != 0) {
1953192895Sjamie			pr = prison_find_child(mypr, jid);
1954191673Sjamie			if (pr != NULL) {
1955191673Sjamie				if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1956191673Sjamie					mtx_unlock(&pr->pr_mtx);
1957191673Sjamie					error = ENOENT;
1958191673Sjamie					vfs_opterror(opts, "jail %d is dying",
1959191673Sjamie					    jid);
1960191673Sjamie					goto done_unlock_list;
1961191673Sjamie				}
1962191673Sjamie				goto found_prison;
1963191673Sjamie			}
1964191673Sjamie			error = ENOENT;
1965191673Sjamie			vfs_opterror(opts, "jail %d not found", jid);
1966191673Sjamie			goto done_unlock_list;
1967191673Sjamie		}
1968191673Sjamie	} else if (error != ENOENT)
1969191673Sjamie		goto done_unlock_list;
197046155Sphk
1971191673Sjamie	error = vfs_getopt(opts, "name", (void **)&name, &len);
1972191673Sjamie	if (error == 0) {
1973191673Sjamie		if (len == 0 || name[len - 1] != '\0') {
1974191673Sjamie			error = EINVAL;
1975191673Sjamie			goto done_unlock_list;
1976191673Sjamie		}
1977192895Sjamie		pr = prison_find_name(mypr, name);
1978191673Sjamie		if (pr != NULL) {
1979191673Sjamie			if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1980191673Sjamie				mtx_unlock(&pr->pr_mtx);
1981191673Sjamie				error = ENOENT;
1982191673Sjamie				vfs_opterror(opts, "jail \"%s\" is dying",
1983191673Sjamie				    name);
1984191673Sjamie				goto done_unlock_list;
1985191673Sjamie			}
1986191673Sjamie			goto found_prison;
1987191673Sjamie		}
1988191673Sjamie		error = ENOENT;
1989191673Sjamie		vfs_opterror(opts, "jail \"%s\" not found", name);
1990191673Sjamie		goto done_unlock_list;
1991191673Sjamie	} else if (error != ENOENT)
1992191673Sjamie		goto done_unlock_list;
1993185435Sbz
1994191673Sjamie	vfs_opterror(opts, "no jail specified");
1995191673Sjamie	error = ENOENT;
1996191673Sjamie	goto done_unlock_list;
1997191673Sjamie
1998191673Sjamie found_prison:
1999191673Sjamie	/* Get the parameters of the prison. */
2000191673Sjamie	pr->pr_ref++;
2001191673Sjamie	locked = PD_LOCKED;
2002191673Sjamie	td->td_retval[0] = pr->pr_id;
2003191673Sjamie	error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2004191673Sjamie	if (error != 0 && error != ENOENT)
2005191673Sjamie		goto done_deref;
2006192895Sjamie	i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
2007192895Sjamie	error = vfs_setopt(opts, "parent", &i, sizeof(i));
2008191673Sjamie	if (error != 0 && error != ENOENT)
2009191673Sjamie		goto done_deref;
2010192895Sjamie	error = vfs_setopts(opts, "name", prison_name(mypr, pr));
2011192895Sjamie	if (error != 0 && error != ENOENT)
2012192895Sjamie		goto done_deref;
2013192895Sjamie	error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2014191673Sjamie	    sizeof(pr->pr_cpuset->cs_id));
2015191673Sjamie	if (error != 0 && error != ENOENT)
2016191673Sjamie		goto done_deref;
2017192895Sjamie	error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2018191673Sjamie	if (error != 0 && error != ENOENT)
2019191673Sjamie		goto done_deref;
2020191673Sjamie#ifdef INET
2021191673Sjamie	error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
2022191673Sjamie	    pr->pr_ip4s * sizeof(*pr->pr_ip4));
2023191673Sjamie	if (error != 0 && error != ENOENT)
2024191673Sjamie		goto done_deref;
2025191673Sjamie#endif
2026191673Sjamie#ifdef INET6
2027191673Sjamie	error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
2028191673Sjamie	    pr->pr_ip6s * sizeof(*pr->pr_ip6));
2029191673Sjamie	if (error != 0 && error != ENOENT)
2030191673Sjamie		goto done_deref;
2031191673Sjamie#endif
2032191673Sjamie	error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2033191673Sjamie	    sizeof(pr->pr_securelevel));
2034191673Sjamie	if (error != 0 && error != ENOENT)
2035191673Sjamie		goto done_deref;
2036194762Sjamie	error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2037194762Sjamie	    sizeof(pr->pr_childcount));
2038194762Sjamie	if (error != 0 && error != ENOENT)
2039194762Sjamie		goto done_deref;
2040194762Sjamie	error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2041194762Sjamie	    sizeof(pr->pr_childmax));
2042194762Sjamie	if (error != 0 && error != ENOENT)
2043194762Sjamie		goto done_deref;
2044194118Sjamie	error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2045191673Sjamie	if (error != 0 && error != ENOENT)
2046191673Sjamie		goto done_deref;
2047194118Sjamie	error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
2048193066Sjamie	if (error != 0 && error != ENOENT)
2049193066Sjamie		goto done_deref;
2050194118Sjamie	error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
2051193066Sjamie	if (error != 0 && error != ENOENT)
2052193066Sjamie		goto done_deref;
2053205014Snwhitehorn#ifdef COMPAT_FREEBSD32
2054217896Sdchagin	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
2055193066Sjamie		uint32_t hid32 = pr->pr_hostid;
2056193066Sjamie
2057193066Sjamie		error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
2058193066Sjamie	} else
2059193066Sjamie#endif
2060193066Sjamie	error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
2061193066Sjamie	    sizeof(pr->pr_hostid));
2062193066Sjamie	if (error != 0 && error != ENOENT)
2063193066Sjamie		goto done_deref;
2064192895Sjamie	error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
2065192895Sjamie	    sizeof(pr->pr_enforce_statfs));
2066191673Sjamie	if (error != 0 && error != ENOENT)
2067191673Sjamie		goto done_deref;
2068231267Smm	error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
2069231267Smm	    sizeof(pr->pr_devfs_rsnum));
2070231267Smm	if (error != 0 && error != ENOENT)
2071231267Smm		goto done_deref;
2072192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
2073192895Sjamie	    fi++) {
2074192895Sjamie		if (pr_flag_names[fi] == NULL)
2075192895Sjamie			continue;
2076192895Sjamie		i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
2077192895Sjamie		error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
2078192895Sjamie		if (error != 0 && error != ENOENT)
2079192895Sjamie			goto done_deref;
2080192895Sjamie		i = !i;
2081192895Sjamie		error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
2082192895Sjamie		if (error != 0 && error != ENOENT)
2083192895Sjamie			goto done_deref;
2084192895Sjamie	}
2085195870Sjamie	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
2086195870Sjamie	    fi++) {
2087195870Sjamie		i = pr->pr_flags &
2088195870Sjamie		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
2089195870Sjamie		i = pr_flag_jailsys[fi].disable &&
2090195870Sjamie		      (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE
2091195870Sjamie		    : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW
2092195870Sjamie		    : JAIL_SYS_INHERIT;
2093195870Sjamie		error =
2094195870Sjamie		    vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i));
2095195870Sjamie		if (error != 0 && error != ENOENT)
2096195870Sjamie			goto done_deref;
2097195870Sjamie	}
2098192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
2099192895Sjamie	    fi++) {
2100192895Sjamie		if (pr_allow_names[fi] == NULL)
2101192895Sjamie			continue;
2102192895Sjamie		i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
2103192895Sjamie		error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
2104192895Sjamie		if (error != 0 && error != ENOENT)
2105192895Sjamie			goto done_deref;
2106192895Sjamie		i = !i;
2107192895Sjamie		error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
2108192895Sjamie		if (error != 0 && error != ENOENT)
2109192895Sjamie			goto done_deref;
2110192895Sjamie	}
2111191673Sjamie	i = (pr->pr_uref == 0);
2112191673Sjamie	error = vfs_setopt(opts, "dying", &i, sizeof(i));
2113191673Sjamie	if (error != 0 && error != ENOENT)
2114191673Sjamie		goto done_deref;
2115191673Sjamie	i = !i;
2116191673Sjamie	error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2117191673Sjamie	if (error != 0 && error != ENOENT)
2118191673Sjamie		goto done_deref;
2119191673Sjamie
2120191673Sjamie	/* Get the module parameters. */
2121191673Sjamie	mtx_unlock(&pr->pr_mtx);
2122191673Sjamie	locked = 0;
2123191673Sjamie	error = osd_jail_call(pr, PR_METHOD_GET, opts);
212446155Sphk	if (error)
2125191673Sjamie		goto done_deref;
2126191673Sjamie	prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
212784828Sjhb
2128191673Sjamie	/* By now, all parameters should have been noted. */
2129191673Sjamie	TAILQ_FOREACH(opt, opts, link) {
2130191673Sjamie		if (!opt->seen && strcmp(opt->name, "errmsg")) {
2131191673Sjamie			error = EINVAL;
2132191673Sjamie			vfs_opterror(opts, "unknown parameter: %s", opt->name);
2133191673Sjamie			goto done_errmsg;
2134191673Sjamie		}
2135185435Sbz	}
2136191673Sjamie
2137191673Sjamie	/* Write the fetched parameters back to userspace. */
2138191673Sjamie	error = 0;
2139191673Sjamie	TAILQ_FOREACH(opt, opts, link) {
2140191673Sjamie		if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2141191673Sjamie			pos = 2 * opt->pos + 1;
2142191673Sjamie			optuio->uio_iov[pos].iov_len = opt->len;
2143191673Sjamie			if (opt->value != NULL) {
2144191673Sjamie				if (optuio->uio_segflg == UIO_SYSSPACE) {
2145191673Sjamie					bcopy(opt->value,
2146191673Sjamie					    optuio->uio_iov[pos].iov_base,
2147191673Sjamie					    opt->len);
2148191673Sjamie				} else {
2149191673Sjamie					error = copyout(opt->value,
2150191673Sjamie					    optuio->uio_iov[pos].iov_base,
2151191673Sjamie					    opt->len);
2152191673Sjamie					if (error)
2153191673Sjamie						break;
2154191673Sjamie				}
2155191673Sjamie			}
2156191673Sjamie		}
2157185435Sbz	}
2158191673Sjamie	goto done_errmsg;
2159191673Sjamie
2160191673Sjamie done_deref:
2161191673Sjamie	prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2162191673Sjamie	goto done_errmsg;
2163191673Sjamie
2164191673Sjamie done_unlock_list:
2165191673Sjamie	sx_sunlock(&allprison_lock);
2166191673Sjamie done_errmsg:
2167191673Sjamie	if (error && errmsg_pos >= 0) {
2168191673Sjamie		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2169191673Sjamie		errmsg_pos = 2 * errmsg_pos + 1;
2170191673Sjamie		if (errmsg_len > 0) {
2171191673Sjamie			if (optuio->uio_segflg == UIO_SYSSPACE)
2172191673Sjamie				bcopy(errmsg,
2173191673Sjamie				    optuio->uio_iov[errmsg_pos].iov_base,
2174191673Sjamie				    errmsg_len);
2175191673Sjamie			else
2176191673Sjamie				copyout(errmsg,
2177191673Sjamie				    optuio->uio_iov[errmsg_pos].iov_base,
2178191673Sjamie				    errmsg_len);
2179191673Sjamie		}
2180185435Sbz	}
2181191673Sjamie	vfs_freeopts(opts);
2182191673Sjamie	return (error);
2183191673Sjamie}
2184113275Smike
2185192895Sjamie
2186191673Sjamie/*
2187191673Sjamie * struct jail_remove_args {
2188191673Sjamie *	int jid;
2189191673Sjamie * };
2190191673Sjamie */
2191191673Sjamieint
2192225617Skmacysys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2193191673Sjamie{
2194192895Sjamie	struct prison *pr, *cpr, *lpr, *tpr;
2195192895Sjamie	int descend, error;
2196185435Sbz
2197191673Sjamie	error = priv_check(td, PRIV_JAIL_REMOVE);
2198185435Sbz	if (error)
2199191673Sjamie		return (error);
2200185435Sbz
2201185435Sbz	sx_xlock(&allprison_lock);
2202192895Sjamie	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2203191673Sjamie	if (pr == NULL) {
2204185435Sbz		sx_xunlock(&allprison_lock);
2205191673Sjamie		return (EINVAL);
2206185435Sbz	}
2207185435Sbz
2208192895Sjamie	/* Remove all descendants of this prison, then remove this prison. */
2209192895Sjamie	pr->pr_ref++;
2210192895Sjamie	pr->pr_flags |= PR_REMOVE;
2211192895Sjamie	if (!LIST_EMPTY(&pr->pr_children)) {
2212192895Sjamie		mtx_unlock(&pr->pr_mtx);
2213192895Sjamie		lpr = NULL;
2214192895Sjamie		FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
2215192895Sjamie			mtx_lock(&cpr->pr_mtx);
2216192895Sjamie			if (cpr->pr_ref > 0) {
2217192895Sjamie				tpr = cpr;
2218192895Sjamie				cpr->pr_ref++;
2219192895Sjamie				cpr->pr_flags |= PR_REMOVE;
2220192895Sjamie			} else {
2221192895Sjamie				/* Already removed - do not do it again. */
2222192895Sjamie				tpr = NULL;
2223192895Sjamie			}
2224192895Sjamie			mtx_unlock(&cpr->pr_mtx);
2225192895Sjamie			if (lpr != NULL) {
2226192895Sjamie				mtx_lock(&lpr->pr_mtx);
2227192895Sjamie				prison_remove_one(lpr);
2228192895Sjamie				sx_xlock(&allprison_lock);
2229192895Sjamie			}
2230192895Sjamie			lpr = tpr;
2231192895Sjamie		}
2232192895Sjamie		if (lpr != NULL) {
2233192895Sjamie			mtx_lock(&lpr->pr_mtx);
2234192895Sjamie			prison_remove_one(lpr);
2235192895Sjamie			sx_xlock(&allprison_lock);
2236192895Sjamie		}
2237192895Sjamie		mtx_lock(&pr->pr_mtx);
2238192895Sjamie	}
2239192895Sjamie	prison_remove_one(pr);
2240192895Sjamie	return (0);
2241192895Sjamie}
2242192895Sjamie
2243192895Sjamiestatic void
2244192895Sjamieprison_remove_one(struct prison *pr)
2245192895Sjamie{
2246192895Sjamie	struct proc *p;
2247192895Sjamie	int deuref;
2248192895Sjamie
2249191673Sjamie	/* If the prison was persistent, it is not anymore. */
2250191673Sjamie	deuref = 0;
2251191673Sjamie	if (pr->pr_flags & PR_PERSIST) {
2252191673Sjamie		pr->pr_ref--;
2253191673Sjamie		deuref = PD_DEUREF;
2254191673Sjamie		pr->pr_flags &= ~PR_PERSIST;
2255179881Sdelphij	}
2256113275Smike
2257192895Sjamie	/*
2258192895Sjamie	 * jail_remove added a reference.  If that's the only one, remove
2259192895Sjamie	 * the prison now.
2260192895Sjamie	 */
2261192895Sjamie	KASSERT(pr->pr_ref > 0,
2262192895Sjamie	    ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
2263192895Sjamie	if (pr->pr_ref == 1) {
2264191673Sjamie		prison_deref(pr,
2265191673Sjamie		    deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
2266192895Sjamie		return;
2267191673Sjamie	}
2268191673Sjamie
2269113275Smike	mtx_unlock(&pr->pr_mtx);
2270191673Sjamie	sx_xunlock(&allprison_lock);
2271191673Sjamie	/*
2272191673Sjamie	 * Kill all processes unfortunate enough to be attached to this prison.
2273191673Sjamie	 */
2274191673Sjamie	sx_slock(&allproc_lock);
2275191673Sjamie	LIST_FOREACH(p, &allproc, p_list) {
2276191673Sjamie		PROC_LOCK(p);
2277191673Sjamie		if (p->p_state != PRS_NEW && p->p_ucred &&
2278191673Sjamie		    p->p_ucred->cr_prison == pr)
2279225617Skmacy			kern_psignal(p, SIGKILL);
2280191673Sjamie		PROC_UNLOCK(p);
2281191673Sjamie	}
2282191673Sjamie	sx_sunlock(&allproc_lock);
2283192895Sjamie	/* Remove the temporary reference added by jail_remove. */
2284191673Sjamie	prison_deref(pr, deuref | PD_DEREF);
2285113275Smike}
2286113275Smike
2287190466Sjamie
2288113275Smike/*
2289114168Smike * struct jail_attach_args {
2290114168Smike *	int jid;
2291114168Smike * };
2292113275Smike */
2293113275Smikeint
2294225617Skmacysys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2295113275Smike{
2296113275Smike	struct prison *pr;
2297191673Sjamie	int error;
2298167309Spjd
2299164032Srwatson	error = priv_check(td, PRIV_JAIL_ATTACH);
2300126023Snectar	if (error)
2301126023Snectar		return (error);
2302126023Snectar
2303168401Spjd	sx_slock(&allprison_lock);
2304192895Sjamie	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2305113275Smike	if (pr == NULL) {
2306168401Spjd		sx_sunlock(&allprison_lock);
2307113275Smike		return (EINVAL);
2308113275Smike	}
2309185435Sbz
2310185435Sbz	/*
2311185435Sbz	 * Do not allow a process to attach to a prison that is not
2312191673Sjamie	 * considered to be "alive".
2313185435Sbz	 */
2314191673Sjamie	if (pr->pr_uref == 0) {
2315185435Sbz		mtx_unlock(&pr->pr_mtx);
2316185435Sbz		sx_sunlock(&allprison_lock);
2317185435Sbz		return (EINVAL);
2318185435Sbz	}
2319191673Sjamie
2320191673Sjamie	return (do_jail_attach(td, pr));
2321191673Sjamie}
2322191673Sjamie
2323191673Sjamiestatic int
2324191673Sjamiedo_jail_attach(struct thread *td, struct prison *pr)
2325191673Sjamie{
2326192895Sjamie	struct prison *ppr;
2327191673Sjamie	struct proc *p;
2328191673Sjamie	struct ucred *newcred, *oldcred;
2329191673Sjamie	int vfslocked, error;
2330191673Sjamie
2331191673Sjamie	/*
2332191673Sjamie	 * XXX: Note that there is a slight race here if two threads
2333191673Sjamie	 * in the same privileged process attempt to attach to two
2334191673Sjamie	 * different jails at the same time.  It is important for
2335191673Sjamie	 * user processes not to do this, or they might end up with
2336191673Sjamie	 * a process root from one prison, but attached to the jail
2337191673Sjamie	 * of another.
2338191673Sjamie	 */
2339113275Smike	pr->pr_ref++;
2340191673Sjamie	pr->pr_uref++;
2341113275Smike	mtx_unlock(&pr->pr_mtx);
2342191673Sjamie
2343191673Sjamie	/* Let modules do whatever they need to prepare for attaching. */
2344191673Sjamie	error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2345191673Sjamie	if (error) {
2346191673Sjamie		prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2347191673Sjamie		return (error);
2348191673Sjamie	}
2349168401Spjd	sx_sunlock(&allprison_lock);
2350113275Smike
2351185435Sbz	/*
2352185435Sbz	 * Reparent the newly attached process to this jail.
2353185435Sbz	 */
2354192895Sjamie	ppr = td->td_ucred->cr_prison;
2355191673Sjamie	p = td->td_proc;
2356185435Sbz	error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2357185435Sbz	if (error)
2358191673Sjamie		goto e_revert_osd;
2359185435Sbz
2360150652Scsjp	vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2361175202Sattilio	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2362113275Smike	if ((error = change_dir(pr->pr_root, td)) != 0)
2363113275Smike		goto e_unlock;
2364113275Smike#ifdef MAC
2365172930Srwatson	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2366113275Smike		goto e_unlock;
2367113275Smike#endif
2368175294Sattilio	VOP_UNLOCK(pr->pr_root, 0);
2369191673Sjamie	if ((error = change_root(pr->pr_root, td)))
2370191673Sjamie		goto e_unlock_giant;
2371150652Scsjp	VFS_UNLOCK_GIANT(vfslocked);
2372113275Smike
237384828Sjhb	newcred = crget();
237484828Sjhb	PROC_LOCK(p);
237584828Sjhb	oldcred = p->p_ucred;
2376113275Smike	setsugid(p);
237784828Sjhb	crcopy(newcred, oldcred);
2378113630Sjhb	newcred->cr_prison = pr;
237984828Sjhb	p->p_ucred = newcred;
238084828Sjhb	PROC_UNLOCK(p);
2381220137Strasz#ifdef RACCT
2382220137Strasz	racct_proc_ucred_changed(p, oldcred, newcred);
2383220137Strasz#endif
238484828Sjhb	crfree(oldcred);
2385192895Sjamie	prison_deref(ppr, PD_DEREF | PD_DEUREF);
238646155Sphk	return (0);
2387191673Sjamie e_unlock:
2388175294Sattilio	VOP_UNLOCK(pr->pr_root, 0);
2389191673Sjamie e_unlock_giant:
2390150652Scsjp	VFS_UNLOCK_GIANT(vfslocked);
2391191673Sjamie e_revert_osd:
2392191673Sjamie	/* Tell modules this thread is still in its old jail after all. */
2393192895Sjamie	(void)osd_jail_call(ppr, PR_METHOD_ATTACH, td);
2394191673Sjamie	prison_deref(pr, PD_DEREF | PD_DEUREF);
239546155Sphk	return (error);
239646155Sphk}
239746155Sphk
2398192895Sjamie
2399113275Smike/*
2400113275Smike * Returns a locked prison instance, or NULL on failure.
2401113275Smike */
2402168399Spjdstruct prison *
2403113275Smikeprison_find(int prid)
2404113275Smike{
2405113275Smike	struct prison *pr;
2406113275Smike
2407168401Spjd	sx_assert(&allprison_lock, SX_LOCKED);
2408191673Sjamie	TAILQ_FOREACH(pr, &allprison, pr_list) {
2409113275Smike		if (pr->pr_id == prid) {
2410113275Smike			mtx_lock(&pr->pr_mtx);
2411191673Sjamie			if (pr->pr_ref > 0)
2412191673Sjamie				return (pr);
2413191673Sjamie			mtx_unlock(&pr->pr_mtx);
2414113275Smike		}
2415113275Smike	}
2416113275Smike	return (NULL);
2417113275Smike}
2418113275Smike
2419191673Sjamie/*
2420192895Sjamie * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
2421191673Sjamie */
2422191673Sjamiestruct prison *
2423192895Sjamieprison_find_child(struct prison *mypr, int prid)
2424191673Sjamie{
2425192895Sjamie	struct prison *pr;
2426192895Sjamie	int descend;
2427192895Sjamie
2428192895Sjamie	sx_assert(&allprison_lock, SX_LOCKED);
2429192895Sjamie	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2430192895Sjamie		if (pr->pr_id == prid) {
2431192895Sjamie			mtx_lock(&pr->pr_mtx);
2432192895Sjamie			if (pr->pr_ref > 0)
2433192895Sjamie				return (pr);
2434192895Sjamie			mtx_unlock(&pr->pr_mtx);
2435192895Sjamie		}
2436192895Sjamie	}
2437192895Sjamie	return (NULL);
2438192895Sjamie}
2439192895Sjamie
2440192895Sjamie/*
2441192895Sjamie * Look for the name relative to mypr.  Returns a locked prison or NULL.
2442192895Sjamie */
2443192895Sjamiestruct prison *
2444192895Sjamieprison_find_name(struct prison *mypr, const char *name)
2445192895Sjamie{
2446191673Sjamie	struct prison *pr, *deadpr;
2447192895Sjamie	size_t mylen;
2448192895Sjamie	int descend;
2449191673Sjamie
2450191673Sjamie	sx_assert(&allprison_lock, SX_LOCKED);
2451192895Sjamie	mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2452191673Sjamie again:
2453191673Sjamie	deadpr = NULL;
2454192895Sjamie	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2455192895Sjamie		if (!strcmp(pr->pr_name + mylen, name)) {
2456191673Sjamie			mtx_lock(&pr->pr_mtx);
2457191673Sjamie			if (pr->pr_ref > 0) {
2458191673Sjamie				if (pr->pr_uref > 0)
2459191673Sjamie					return (pr);
2460191673Sjamie				deadpr = pr;
2461191673Sjamie			}
2462191673Sjamie			mtx_unlock(&pr->pr_mtx);
2463191673Sjamie		}
2464191673Sjamie	}
2465192895Sjamie	/* There was no valid prison - perhaps there was a dying one. */
2466191673Sjamie	if (deadpr != NULL) {
2467191673Sjamie		mtx_lock(&deadpr->pr_mtx);
2468191673Sjamie		if (deadpr->pr_ref == 0) {
2469191673Sjamie			mtx_unlock(&deadpr->pr_mtx);
2470191673Sjamie			goto again;
2471191673Sjamie		}
2472191673Sjamie	}
2473191673Sjamie	return (deadpr);
2474191673Sjamie}
2475191673Sjamie
2476191673Sjamie/*
2477192895Sjamie * See if a prison has the specific flag set.
2478192895Sjamie */
2479192895Sjamieint
2480192895Sjamieprison_flag(struct ucred *cred, unsigned flag)
2481192895Sjamie{
2482192895Sjamie
2483192895Sjamie	/* This is an atomic read, so no locking is necessary. */
2484192895Sjamie	return (cred->cr_prison->pr_flags & flag);
2485192895Sjamie}
2486192895Sjamie
2487192895Sjamieint
2488192895Sjamieprison_allow(struct ucred *cred, unsigned flag)
2489192895Sjamie{
2490192895Sjamie
2491192895Sjamie	/* This is an atomic read, so no locking is necessary. */
2492192895Sjamie	return (cred->cr_prison->pr_allow & flag);
2493192895Sjamie}
2494192895Sjamie
2495192895Sjamie/*
2496191673Sjamie * Remove a prison reference.  If that was the last reference, remove the
2497191673Sjamie * prison itself - but not in this context in case there are locks held.
2498191673Sjamie */
249972786Srwatsonvoid
2500185029Spjdprison_free_locked(struct prison *pr)
250172786Srwatson{
250272786Srwatson
2503185029Spjd	mtx_assert(&pr->pr_mtx, MA_OWNED);
250472786Srwatson	pr->pr_ref--;
250572786Srwatson	if (pr->pr_ref == 0) {
2506168483Spjd		mtx_unlock(&pr->pr_mtx);
2507124882Srwatson		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
2508144660Sjeff		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
250987275Srwatson		return;
251072786Srwatson	}
251187275Srwatson	mtx_unlock(&pr->pr_mtx);
251272786Srwatson}
251372786Srwatson
2514185029Spjdvoid
2515185029Spjdprison_free(struct prison *pr)
2516185029Spjd{
2517185029Spjd
2518185029Spjd	mtx_lock(&pr->pr_mtx);
2519185029Spjd	prison_free_locked(pr);
2520185029Spjd}
2521185029Spjd
2522124882Srwatsonstatic void
2523124882Srwatsonprison_complete(void *context, int pending)
2524124882Srwatson{
2525191673Sjamie
2526191673Sjamie	prison_deref((struct prison *)context, 0);
2527191673Sjamie}
2528191673Sjamie
2529191673Sjamie/*
2530191673Sjamie * Remove a prison reference (usually).  This internal version assumes no
2531191673Sjamie * mutexes are held, except perhaps the prison itself.  If there are no more
2532191673Sjamie * references, release and delist the prison.  On completion, the prison lock
2533191673Sjamie * and the allprison lock are both unlocked.
2534191673Sjamie */
2535191673Sjamiestatic void
2536191673Sjamieprison_deref(struct prison *pr, int flags)
2537191673Sjamie{
2538192895Sjamie	struct prison *ppr, *tpr;
2539150652Scsjp	int vfslocked;
2540124882Srwatson
2541191673Sjamie	if (!(flags & PD_LOCKED))
2542191673Sjamie		mtx_lock(&pr->pr_mtx);
2543225191Sjamie	for (;;) {
2544225191Sjamie		if (flags & PD_DEUREF) {
2545225191Sjamie			pr->pr_uref--;
2546225191Sjamie			KASSERT(prison0.pr_uref != 0, ("prison0 pr_uref=0"));
2547192895Sjamie		}
2548192895Sjamie		if (flags & PD_DEREF)
2549192895Sjamie			pr->pr_ref--;
2550192895Sjamie		/* If the prison still has references, nothing else to do. */
2551192895Sjamie		if (pr->pr_ref > 0) {
2552192895Sjamie			mtx_unlock(&pr->pr_mtx);
2553192895Sjamie			if (flags & PD_LIST_SLOCKED)
2554192895Sjamie				sx_sunlock(&allprison_lock);
2555192895Sjamie			else if (flags & PD_LIST_XLOCKED)
2556192895Sjamie				sx_xunlock(&allprison_lock);
2557192895Sjamie			return;
2558191673Sjamie		}
2559191673Sjamie
2560192895Sjamie		mtx_unlock(&pr->pr_mtx);
2561192895Sjamie		if (flags & PD_LIST_SLOCKED) {
2562192895Sjamie			if (!sx_try_upgrade(&allprison_lock)) {
2563192895Sjamie				sx_sunlock(&allprison_lock);
2564192895Sjamie				sx_xlock(&allprison_lock);
2565192895Sjamie			}
2566192895Sjamie		} else if (!(flags & PD_LIST_XLOCKED))
2567192895Sjamie			sx_xlock(&allprison_lock);
2568168489Spjd
2569192895Sjamie		TAILQ_REMOVE(&allprison, pr, pr_list);
2570192895Sjamie		LIST_REMOVE(pr, pr_sibling);
2571192895Sjamie		ppr = pr->pr_parent;
2572192895Sjamie		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2573194762Sjamie			tpr->pr_childcount--;
2574196592Sjamie		sx_xunlock(&allprison_lock);
2575192895Sjamie
2576194251Sjamie#ifdef VIMAGE
2577196505Szec		if (pr->pr_vnet != ppr->pr_vnet)
2578194251Sjamie			vnet_destroy(pr->pr_vnet);
2579194251Sjamie#endif
2580192895Sjamie		if (pr->pr_root != NULL) {
2581192895Sjamie			vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2582192895Sjamie			vrele(pr->pr_root);
2583192895Sjamie			VFS_UNLOCK_GIANT(vfslocked);
2584192895Sjamie		}
2585192895Sjamie		mtx_destroy(&pr->pr_mtx);
2586191673Sjamie#ifdef INET
2587192895Sjamie		free(pr->pr_ip4, M_PRISON);
2588191673Sjamie#endif
2589185435Sbz#ifdef INET6
2590192895Sjamie		free(pr->pr_ip6, M_PRISON);
2591185435Sbz#endif
2592192895Sjamie		if (pr->pr_cpuset != NULL)
2593192895Sjamie			cpuset_rel(pr->pr_cpuset);
2594192895Sjamie		osd_jail_exit(pr);
2595221362Strasz#ifdef RACCT
2596221362Strasz		prison_racct_detach(pr);
2597220163Strasz#endif
2598192895Sjamie		free(pr, M_PRISON);
2599192895Sjamie
2600192895Sjamie		/* Removing a prison frees a reference on its parent. */
2601192895Sjamie		pr = ppr;
2602192895Sjamie		mtx_lock(&pr->pr_mtx);
2603225191Sjamie		flags = PD_DEREF | PD_DEUREF;
2604192895Sjamie	}
2605124882Srwatson}
2606124882Srwatson
260772786Srwatsonvoid
2608185029Spjdprison_hold_locked(struct prison *pr)
260972786Srwatson{
261072786Srwatson
2611185029Spjd	mtx_assert(&pr->pr_mtx, MA_OWNED);
2612168489Spjd	KASSERT(pr->pr_ref > 0,
2613191671Sjamie	    ("Trying to hold dead prison (jid=%d).", pr->pr_id));
261472786Srwatson	pr->pr_ref++;
2615185029Spjd}
2616185029Spjd
2617185029Spjdvoid
2618185029Spjdprison_hold(struct prison *pr)
2619185029Spjd{
2620185029Spjd
2621185029Spjd	mtx_lock(&pr->pr_mtx);
2622185029Spjd	prison_hold_locked(pr);
262387275Srwatson	mtx_unlock(&pr->pr_mtx);
262472786Srwatson}
262572786Srwatson
2626185435Sbzvoid
2627185435Sbzprison_proc_hold(struct prison *pr)
262887275Srwatson{
262987275Srwatson
2630185435Sbz	mtx_lock(&pr->pr_mtx);
2631191673Sjamie	KASSERT(pr->pr_uref > 0,
2632191673Sjamie	    ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2633191673Sjamie	pr->pr_uref++;
2634185435Sbz	mtx_unlock(&pr->pr_mtx);
263587275Srwatson}
263687275Srwatson
2637185435Sbzvoid
2638185435Sbzprison_proc_free(struct prison *pr)
2639185435Sbz{
2640185435Sbz
2641185435Sbz	mtx_lock(&pr->pr_mtx);
2642191673Sjamie	KASSERT(pr->pr_uref > 0,
2643191673Sjamie	    ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2644191673Sjamie	prison_deref(pr, PD_DEUREF | PD_LOCKED);
2645185435Sbz}
2646185435Sbz
2647185435Sbz
2648185435Sbz#ifdef INET
2649185435Sbz/*
2650192895Sjamie * Restrict a prison's IP address list with its parent's, possibly replacing
2651192895Sjamie * it.  Return true if the replacement buffer was used (or would have been).
2652192895Sjamie */
2653192895Sjamiestatic int
2654192895Sjamieprison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
2655192895Sjamie{
2656192895Sjamie	int ii, ij, used;
2657192895Sjamie	struct prison *ppr;
2658192895Sjamie
2659192895Sjamie	ppr = pr->pr_parent;
2660192895Sjamie	if (!(pr->pr_flags & PR_IP4_USER)) {
2661192895Sjamie		/* This has no user settings, so just copy the parent's list. */
2662192895Sjamie		if (pr->pr_ip4s < ppr->pr_ip4s) {
2663192895Sjamie			/*
2664192895Sjamie			 * There's no room for the parent's list.  Use the
2665192895Sjamie			 * new list buffer, which is assumed to be big enough
2666192895Sjamie			 * (if it was passed).  If there's no buffer, try to
2667192895Sjamie			 * allocate one.
2668192895Sjamie			 */
2669192895Sjamie			used = 1;
2670192895Sjamie			if (newip4 == NULL) {
2671192895Sjamie				newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
2672192895Sjamie				    M_PRISON, M_NOWAIT);
2673192895Sjamie				if (newip4 != NULL)
2674192895Sjamie					used = 0;
2675192895Sjamie			}
2676192895Sjamie			if (newip4 != NULL) {
2677192895Sjamie				bcopy(ppr->pr_ip4, newip4,
2678192895Sjamie				    ppr->pr_ip4s * sizeof(*newip4));
2679192895Sjamie				free(pr->pr_ip4, M_PRISON);
2680192895Sjamie				pr->pr_ip4 = newip4;
2681192895Sjamie				pr->pr_ip4s = ppr->pr_ip4s;
2682192895Sjamie			}
2683192895Sjamie			return (used);
2684192895Sjamie		}
2685192895Sjamie		pr->pr_ip4s = ppr->pr_ip4s;
2686192895Sjamie		if (pr->pr_ip4s > 0)
2687192895Sjamie			bcopy(ppr->pr_ip4, pr->pr_ip4,
2688192895Sjamie			    pr->pr_ip4s * sizeof(*newip4));
2689192895Sjamie		else if (pr->pr_ip4 != NULL) {
2690192895Sjamie			free(pr->pr_ip4, M_PRISON);
2691192895Sjamie			pr->pr_ip4 = NULL;
2692192895Sjamie		}
2693195974Sjamie	} else if (pr->pr_ip4s > 0) {
2694192895Sjamie		/* Remove addresses that aren't in the parent. */
2695192895Sjamie		for (ij = 0; ij < ppr->pr_ip4s; ij++)
2696192895Sjamie			if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
2697192895Sjamie				break;
2698192895Sjamie		if (ij < ppr->pr_ip4s)
2699192895Sjamie			ii = 1;
2700192895Sjamie		else {
2701192895Sjamie			bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
2702192895Sjamie			    --pr->pr_ip4s * sizeof(*pr->pr_ip4));
2703192895Sjamie			ii = 0;
2704192895Sjamie		}
2705192895Sjamie		for (ij = 1; ii < pr->pr_ip4s; ) {
2706192895Sjamie			if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
2707192895Sjamie				ii++;
2708192895Sjamie				continue;
2709192895Sjamie			}
2710192895Sjamie			switch (ij >= ppr->pr_ip4s ? -1 :
2711192895Sjamie				qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
2712192895Sjamie			case -1:
2713192895Sjamie				bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
2714192895Sjamie				    (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
2715192895Sjamie				break;
2716192895Sjamie			case 0:
2717192895Sjamie				ii++;
2718192895Sjamie				ij++;
2719192895Sjamie				break;
2720192895Sjamie			case 1:
2721192895Sjamie				ij++;
2722192895Sjamie				break;
2723192895Sjamie			}
2724192895Sjamie		}
2725192895Sjamie		if (pr->pr_ip4s == 0) {
2726195870Sjamie			pr->pr_flags |= PR_IP4_DISABLE;
2727192895Sjamie			free(pr->pr_ip4, M_PRISON);
2728192895Sjamie			pr->pr_ip4 = NULL;
2729192895Sjamie		}
2730192895Sjamie	}
2731192895Sjamie	return (0);
2732192895Sjamie}
2733192895Sjamie
2734192895Sjamie/*
2735185435Sbz * Pass back primary IPv4 address of this jail.
2736185435Sbz *
2737192895Sjamie * If not restricted return success but do not alter the address.  Caller has
2738192895Sjamie * to make sure to initialize it correctly (e.g. INADDR_ANY).
2739185435Sbz *
2740188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2741188144Sjamie * Address returned in NBO.
2742185435Sbz */
274346155Sphkint
2744187684Sbzprison_get_ip4(struct ucred *cred, struct in_addr *ia)
274546155Sphk{
2746191673Sjamie	struct prison *pr;
274746155Sphk
2748185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2749185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2750185435Sbz
2751192895Sjamie	pr = cred->cr_prison;
2752192895Sjamie	if (!(pr->pr_flags & PR_IP4))
275346155Sphk		return (0);
2754191673Sjamie	mtx_lock(&pr->pr_mtx);
2755192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2756192895Sjamie		mtx_unlock(&pr->pr_mtx);
2757192895Sjamie		return (0);
2758192895Sjamie	}
2759191673Sjamie	if (pr->pr_ip4 == NULL) {
2760191673Sjamie		mtx_unlock(&pr->pr_mtx);
2761188144Sjamie		return (EAFNOSUPPORT);
2762191673Sjamie	}
2763185435Sbz
2764191673Sjamie	ia->s_addr = pr->pr_ip4[0].s_addr;
2765191673Sjamie	mtx_unlock(&pr->pr_mtx);
2766185435Sbz	return (0);
2767185435Sbz}
2768185435Sbz
2769185435Sbz/*
2770202468Sbz * Return 1 if we should do proper source address selection or are not jailed.
2771202468Sbz * We will return 0 if we should bypass source address selection in favour
2772202468Sbz * of the primary jail IPv4 address. Only in this case *ia will be updated and
2773202468Sbz * returned in NBO.
2774202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv4.
2775202468Sbz */
2776202468Sbzint
2777202468Sbzprison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia)
2778202468Sbz{
2779202468Sbz	struct prison *pr;
2780202468Sbz	struct in_addr lia;
2781202468Sbz	int error;
2782202468Sbz
2783202468Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2784202468Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2785202468Sbz
2786202468Sbz	if (!jailed(cred))
2787202468Sbz		return (1);
2788202468Sbz
2789202468Sbz	pr = cred->cr_prison;
2790202468Sbz	if (pr->pr_flags & PR_IP4_SADDRSEL)
2791202468Sbz		return (1);
2792202468Sbz
2793202468Sbz	lia.s_addr = INADDR_ANY;
2794202468Sbz	error = prison_get_ip4(cred, &lia);
2795202468Sbz	if (error)
2796202468Sbz		return (error);
2797202468Sbz	if (lia.s_addr == INADDR_ANY)
2798202468Sbz		return (1);
2799202468Sbz
2800202468Sbz	ia->s_addr = lia.s_addr;
2801202468Sbz	return (0);
2802202468Sbz}
2803202468Sbz
2804202468Sbz/*
2805192895Sjamie * Return true if pr1 and pr2 have the same IPv4 address restrictions.
2806192895Sjamie */
2807192895Sjamieint
2808192895Sjamieprison_equal_ip4(struct prison *pr1, struct prison *pr2)
2809192895Sjamie{
2810192895Sjamie
2811192895Sjamie	if (pr1 == pr2)
2812192895Sjamie		return (1);
2813192895Sjamie
2814192895Sjamie	/*
2815195974Sjamie	 * No need to lock since the PR_IP4_USER flag can't be altered for
2816195974Sjamie	 * existing prisons.
2817192895Sjamie	 */
2818195945Sjamie	while (pr1 != &prison0 &&
2819195945Sjamie#ifdef VIMAGE
2820195945Sjamie	       !(pr1->pr_flags & PR_VNET) &&
2821195945Sjamie#endif
2822195945Sjamie	       !(pr1->pr_flags & PR_IP4_USER))
2823192895Sjamie		pr1 = pr1->pr_parent;
2824195945Sjamie	while (pr2 != &prison0 &&
2825195945Sjamie#ifdef VIMAGE
2826195945Sjamie	       !(pr2->pr_flags & PR_VNET) &&
2827195945Sjamie#endif
2828195945Sjamie	       !(pr2->pr_flags & PR_IP4_USER))
2829192895Sjamie		pr2 = pr2->pr_parent;
2830192895Sjamie	return (pr1 == pr2);
2831192895Sjamie}
2832192895Sjamie
2833192895Sjamie/*
2834185435Sbz * Make sure our (source) address is set to something meaningful to this
2835185435Sbz * jail.
2836185435Sbz *
2837192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2838192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2839192895Sjamie * doesn't allow IPv4.  Address passed in in NBO and returned in NBO.
2840185435Sbz */
2841185435Sbzint
2842185435Sbzprison_local_ip4(struct ucred *cred, struct in_addr *ia)
2843185435Sbz{
2844191673Sjamie	struct prison *pr;
2845185435Sbz	struct in_addr ia0;
2846191673Sjamie	int error;
2847185435Sbz
2848185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2849185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2850185435Sbz
2851192895Sjamie	pr = cred->cr_prison;
2852192895Sjamie	if (!(pr->pr_flags & PR_IP4))
285346155Sphk		return (0);
2854191673Sjamie	mtx_lock(&pr->pr_mtx);
2855192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2856192895Sjamie		mtx_unlock(&pr->pr_mtx);
2857192895Sjamie		return (0);
2858192895Sjamie	}
2859191673Sjamie	if (pr->pr_ip4 == NULL) {
2860191673Sjamie		mtx_unlock(&pr->pr_mtx);
2861188144Sjamie		return (EAFNOSUPPORT);
2862191673Sjamie	}
2863185435Sbz
2864185435Sbz	ia0.s_addr = ntohl(ia->s_addr);
2865185435Sbz	if (ia0.s_addr == INADDR_LOOPBACK) {
2866191673Sjamie		ia->s_addr = pr->pr_ip4[0].s_addr;
2867191673Sjamie		mtx_unlock(&pr->pr_mtx);
2868185435Sbz		return (0);
286946155Sphk	}
2870185435Sbz
2871188144Sjamie	if (ia0.s_addr == INADDR_ANY) {
2872188144Sjamie		/*
2873188144Sjamie		 * In case there is only 1 IPv4 address, bind directly.
2874188144Sjamie		 */
2875191673Sjamie		if (pr->pr_ip4s == 1)
2876191673Sjamie			ia->s_addr = pr->pr_ip4[0].s_addr;
2877191673Sjamie		mtx_unlock(&pr->pr_mtx);
2878185435Sbz		return (0);
2879185435Sbz	}
2880185435Sbz
2881191673Sjamie	error = _prison_check_ip4(pr, ia);
2882191673Sjamie	mtx_unlock(&pr->pr_mtx);
2883191673Sjamie	return (error);
2884185435Sbz}
2885185435Sbz
2886185435Sbz/*
2887185435Sbz * Rewrite destination address in case we will connect to loopback address.
2888185435Sbz *
2889188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2890188144Sjamie * Address passed in in NBO and returned in NBO.
2891185435Sbz */
2892185435Sbzint
2893185435Sbzprison_remote_ip4(struct ucred *cred, struct in_addr *ia)
2894185435Sbz{
2895191673Sjamie	struct prison *pr;
2896185435Sbz
2897185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2898185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2899185435Sbz
2900192895Sjamie	pr = cred->cr_prison;
2901192895Sjamie	if (!(pr->pr_flags & PR_IP4))
2902185435Sbz		return (0);
2903191673Sjamie	mtx_lock(&pr->pr_mtx);
2904192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2905192895Sjamie		mtx_unlock(&pr->pr_mtx);
2906192895Sjamie		return (0);
2907192895Sjamie	}
2908191673Sjamie	if (pr->pr_ip4 == NULL) {
2909191673Sjamie		mtx_unlock(&pr->pr_mtx);
2910188144Sjamie		return (EAFNOSUPPORT);
2911191673Sjamie	}
2912188144Sjamie
2913185435Sbz	if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
2914191673Sjamie		ia->s_addr = pr->pr_ip4[0].s_addr;
2915191673Sjamie		mtx_unlock(&pr->pr_mtx);
2916185435Sbz		return (0);
2917185435Sbz	}
2918185435Sbz
2919185435Sbz	/*
2920185435Sbz	 * Return success because nothing had to be changed.
2921185435Sbz	 */
2922191673Sjamie	mtx_unlock(&pr->pr_mtx);
2923185435Sbz	return (0);
2924185435Sbz}
2925185435Sbz
2926185435Sbz/*
2927188144Sjamie * Check if given address belongs to the jail referenced by cred/prison.
2928185435Sbz *
2929192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2930192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2931192895Sjamie * doesn't allow IPv4.  Address passed in in NBO.
2932185435Sbz */
2933185435Sbzstatic int
2934185435Sbz_prison_check_ip4(struct prison *pr, struct in_addr *ia)
2935185435Sbz{
2936185435Sbz	int i, a, z, d;
2937185435Sbz
2938185435Sbz	/*
2939185435Sbz	 * Check the primary IP.
2940185435Sbz	 */
2941185435Sbz	if (pr->pr_ip4[0].s_addr == ia->s_addr)
2942188144Sjamie		return (0);
2943185435Sbz
2944185435Sbz	/*
2945185435Sbz	 * All the other IPs are sorted so we can do a binary search.
2946185435Sbz	 */
2947185435Sbz	a = 0;
2948185435Sbz	z = pr->pr_ip4s - 2;
2949185435Sbz	while (a <= z) {
2950185435Sbz		i = (a + z) / 2;
2951185435Sbz		d = qcmp_v4(&pr->pr_ip4[i+1], ia);
2952185435Sbz		if (d > 0)
2953185435Sbz			z = i - 1;
2954185435Sbz		else if (d < 0)
2955185435Sbz			a = i + 1;
295681114Srwatson		else
2957188144Sjamie			return (0);
2958185435Sbz	}
2959188144Sjamie
2960188144Sjamie	return (EADDRNOTAVAIL);
2961185435Sbz}
2962185435Sbz
2963185435Sbzint
2964185435Sbzprison_check_ip4(struct ucred *cred, struct in_addr *ia)
2965185435Sbz{
2966191673Sjamie	struct prison *pr;
2967191673Sjamie	int error;
2968185435Sbz
2969185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2970185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2971185435Sbz
2972192895Sjamie	pr = cred->cr_prison;
2973192895Sjamie	if (!(pr->pr_flags & PR_IP4))
2974188144Sjamie		return (0);
2975191673Sjamie	mtx_lock(&pr->pr_mtx);
2976192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2977192895Sjamie		mtx_unlock(&pr->pr_mtx);
2978192895Sjamie		return (0);
2979192895Sjamie	}
2980191673Sjamie	if (pr->pr_ip4 == NULL) {
2981191673Sjamie		mtx_unlock(&pr->pr_mtx);
2982188144Sjamie		return (EAFNOSUPPORT);
2983191673Sjamie	}
2984185435Sbz
2985191673Sjamie	error = _prison_check_ip4(pr, ia);
2986191673Sjamie	mtx_unlock(&pr->pr_mtx);
2987191673Sjamie	return (error);
2988185435Sbz}
2989185435Sbz#endif
2990185435Sbz
2991185435Sbz#ifdef INET6
2992192895Sjamiestatic int
2993192895Sjamieprison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
2994192895Sjamie{
2995192895Sjamie	int ii, ij, used;
2996192895Sjamie	struct prison *ppr;
2997192895Sjamie
2998192895Sjamie	ppr = pr->pr_parent;
2999192895Sjamie	if (!(pr->pr_flags & PR_IP6_USER)) {
3000192895Sjamie		/* This has no user settings, so just copy the parent's list. */
3001192895Sjamie		if (pr->pr_ip6s < ppr->pr_ip6s) {
3002192895Sjamie			/*
3003192895Sjamie			 * There's no room for the parent's list.  Use the
3004192895Sjamie			 * new list buffer, which is assumed to be big enough
3005192895Sjamie			 * (if it was passed).  If there's no buffer, try to
3006192895Sjamie			 * allocate one.
3007192895Sjamie			 */
3008192895Sjamie			used = 1;
3009192895Sjamie			if (newip6 == NULL) {
3010192895Sjamie				newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
3011192895Sjamie				    M_PRISON, M_NOWAIT);
3012192895Sjamie				if (newip6 != NULL)
3013192895Sjamie					used = 0;
3014192895Sjamie			}
3015192895Sjamie			if (newip6 != NULL) {
3016192895Sjamie				bcopy(ppr->pr_ip6, newip6,
3017192895Sjamie				    ppr->pr_ip6s * sizeof(*newip6));
3018192895Sjamie				free(pr->pr_ip6, M_PRISON);
3019192895Sjamie				pr->pr_ip6 = newip6;
3020192895Sjamie				pr->pr_ip6s = ppr->pr_ip6s;
3021192895Sjamie			}
3022192895Sjamie			return (used);
3023192895Sjamie		}
3024192895Sjamie		pr->pr_ip6s = ppr->pr_ip6s;
3025192895Sjamie		if (pr->pr_ip6s > 0)
3026192895Sjamie			bcopy(ppr->pr_ip6, pr->pr_ip6,
3027192895Sjamie			    pr->pr_ip6s * sizeof(*newip6));
3028192895Sjamie		else if (pr->pr_ip6 != NULL) {
3029192895Sjamie			free(pr->pr_ip6, M_PRISON);
3030192895Sjamie			pr->pr_ip6 = NULL;
3031192895Sjamie		}
3032195974Sjamie	} else if (pr->pr_ip6s > 0) {
3033192895Sjamie		/* Remove addresses that aren't in the parent. */
3034192895Sjamie		for (ij = 0; ij < ppr->pr_ip6s; ij++)
3035192895Sjamie			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
3036192895Sjamie			    &ppr->pr_ip6[ij]))
3037192895Sjamie				break;
3038192895Sjamie		if (ij < ppr->pr_ip6s)
3039192895Sjamie			ii = 1;
3040192895Sjamie		else {
3041192895Sjamie			bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
3042192895Sjamie			    --pr->pr_ip6s * sizeof(*pr->pr_ip6));
3043192895Sjamie			ii = 0;
3044192895Sjamie		}
3045192895Sjamie		for (ij = 1; ii < pr->pr_ip6s; ) {
3046192895Sjamie			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
3047192895Sjamie			    &ppr->pr_ip6[0])) {
3048192895Sjamie				ii++;
3049192895Sjamie				continue;
3050192895Sjamie			}
3051192895Sjamie			switch (ij >= ppr->pr_ip4s ? -1 :
3052192895Sjamie				qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
3053192895Sjamie			case -1:
3054192895Sjamie				bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
3055192895Sjamie				    (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
3056192895Sjamie				break;
3057192895Sjamie			case 0:
3058192895Sjamie				ii++;
3059192895Sjamie				ij++;
3060192895Sjamie				break;
3061192895Sjamie			case 1:
3062192895Sjamie				ij++;
3063192895Sjamie				break;
3064192895Sjamie			}
3065192895Sjamie		}
3066192895Sjamie		if (pr->pr_ip6s == 0) {
3067195870Sjamie			pr->pr_flags |= PR_IP6_DISABLE;
3068192895Sjamie			free(pr->pr_ip6, M_PRISON);
3069192895Sjamie			pr->pr_ip6 = NULL;
3070192895Sjamie		}
3071192895Sjamie	}
3072192895Sjamie	return 0;
3073192895Sjamie}
3074192895Sjamie
3075185435Sbz/*
3076185435Sbz * Pass back primary IPv6 address for this jail.
3077185435Sbz *
3078192895Sjamie * If not restricted return success but do not alter the address.  Caller has
3079192895Sjamie * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
3080185435Sbz *
3081188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3082185435Sbz */
3083185435Sbzint
3084187684Sbzprison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
3085185435Sbz{
3086191673Sjamie	struct prison *pr;
3087185435Sbz
3088185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3089185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3090185435Sbz
3091192895Sjamie	pr = cred->cr_prison;
3092192895Sjamie	if (!(pr->pr_flags & PR_IP6))
309381114Srwatson		return (0);
3094191673Sjamie	mtx_lock(&pr->pr_mtx);
3095192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3096192895Sjamie		mtx_unlock(&pr->pr_mtx);
3097192895Sjamie		return (0);
3098192895Sjamie	}
3099191673Sjamie	if (pr->pr_ip6 == NULL) {
3100191673Sjamie		mtx_unlock(&pr->pr_mtx);
3101188144Sjamie		return (EAFNOSUPPORT);
3102191673Sjamie	}
3103188144Sjamie
3104191673Sjamie	bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3105191673Sjamie	mtx_unlock(&pr->pr_mtx);
3106185435Sbz	return (0);
3107185435Sbz}
3108185435Sbz
3109185435Sbz/*
3110202468Sbz * Return 1 if we should do proper source address selection or are not jailed.
3111202468Sbz * We will return 0 if we should bypass source address selection in favour
3112202468Sbz * of the primary jail IPv6 address. Only in this case *ia will be updated and
3113202468Sbz * returned in NBO.
3114202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv6.
3115202468Sbz */
3116202468Sbzint
3117202468Sbzprison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6)
3118202468Sbz{
3119202468Sbz	struct prison *pr;
3120202468Sbz	struct in6_addr lia6;
3121202468Sbz	int error;
3122202468Sbz
3123202468Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3124202468Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3125202468Sbz
3126202468Sbz	if (!jailed(cred))
3127202468Sbz		return (1);
3128202468Sbz
3129202468Sbz	pr = cred->cr_prison;
3130202468Sbz	if (pr->pr_flags & PR_IP6_SADDRSEL)
3131202468Sbz		return (1);
3132202468Sbz
3133202468Sbz	lia6 = in6addr_any;
3134202468Sbz	error = prison_get_ip6(cred, &lia6);
3135202468Sbz	if (error)
3136202468Sbz		return (error);
3137202468Sbz	if (IN6_IS_ADDR_UNSPECIFIED(&lia6))
3138202468Sbz		return (1);
3139202468Sbz
3140202468Sbz	bcopy(&lia6, ia6, sizeof(struct in6_addr));
3141202468Sbz	return (0);
3142202468Sbz}
3143202468Sbz
3144202468Sbz/*
3145192895Sjamie * Return true if pr1 and pr2 have the same IPv6 address restrictions.
3146192895Sjamie */
3147192895Sjamieint
3148192895Sjamieprison_equal_ip6(struct prison *pr1, struct prison *pr2)
3149192895Sjamie{
3150192895Sjamie
3151192895Sjamie	if (pr1 == pr2)
3152192895Sjamie		return (1);
3153192895Sjamie
3154195945Sjamie	while (pr1 != &prison0 &&
3155195945Sjamie#ifdef VIMAGE
3156195945Sjamie	       !(pr1->pr_flags & PR_VNET) &&
3157195945Sjamie#endif
3158195945Sjamie	       !(pr1->pr_flags & PR_IP6_USER))
3159192895Sjamie		pr1 = pr1->pr_parent;
3160195945Sjamie	while (pr2 != &prison0 &&
3161195945Sjamie#ifdef VIMAGE
3162195945Sjamie	       !(pr2->pr_flags & PR_VNET) &&
3163195945Sjamie#endif
3164195945Sjamie	       !(pr2->pr_flags & PR_IP6_USER))
3165192895Sjamie		pr2 = pr2->pr_parent;
3166192895Sjamie	return (pr1 == pr2);
3167192895Sjamie}
3168192895Sjamie
3169192895Sjamie/*
3170185435Sbz * Make sure our (source) address is set to something meaningful to this jail.
3171185435Sbz *
3172185435Sbz * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
3173185435Sbz * when needed while binding.
3174185435Sbz *
3175192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3176192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3177192895Sjamie * doesn't allow IPv6.
3178185435Sbz */
3179185435Sbzint
3180185435Sbzprison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
3181185435Sbz{
3182191673Sjamie	struct prison *pr;
3183191673Sjamie	int error;
3184185435Sbz
3185185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3186185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3187185435Sbz
3188192895Sjamie	pr = cred->cr_prison;
3189192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3190185435Sbz		return (0);
3191191673Sjamie	mtx_lock(&pr->pr_mtx);
3192192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3193192895Sjamie		mtx_unlock(&pr->pr_mtx);
3194192895Sjamie		return (0);
3195192895Sjamie	}
3196191673Sjamie	if (pr->pr_ip6 == NULL) {
3197191673Sjamie		mtx_unlock(&pr->pr_mtx);
3198188144Sjamie		return (EAFNOSUPPORT);
3199191673Sjamie	}
3200188144Sjamie
3201185435Sbz	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3202191673Sjamie		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3203191673Sjamie		mtx_unlock(&pr->pr_mtx);
3204185435Sbz		return (0);
320581114Srwatson	}
3206185435Sbz
3207188144Sjamie	if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3208188144Sjamie		/*
3209188144Sjamie		 * In case there is only 1 IPv6 address, and v6only is true,
3210188144Sjamie		 * then bind directly.
3211188144Sjamie		 */
3212191673Sjamie		if (v6only != 0 && pr->pr_ip6s == 1)
3213191673Sjamie			bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3214191673Sjamie		mtx_unlock(&pr->pr_mtx);
3215185435Sbz		return (0);
3216185435Sbz	}
3217188144Sjamie
3218191673Sjamie	error = _prison_check_ip6(pr, ia6);
3219191673Sjamie	mtx_unlock(&pr->pr_mtx);
3220191673Sjamie	return (error);
3221185435Sbz}
3222185435Sbz
3223185435Sbz/*
3224185435Sbz * Rewrite destination address in case we will connect to loopback address.
3225185435Sbz *
3226188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3227185435Sbz */
3228185435Sbzint
3229185435Sbzprison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3230185435Sbz{
3231191673Sjamie	struct prison *pr;
3232185435Sbz
3233185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3234185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3235185435Sbz
3236192895Sjamie	pr = cred->cr_prison;
3237192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3238185435Sbz		return (0);
3239191673Sjamie	mtx_lock(&pr->pr_mtx);
3240192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3241192895Sjamie		mtx_unlock(&pr->pr_mtx);
3242192895Sjamie		return (0);
3243192895Sjamie	}
3244191673Sjamie	if (pr->pr_ip6 == NULL) {
3245191673Sjamie		mtx_unlock(&pr->pr_mtx);
3246188144Sjamie		return (EAFNOSUPPORT);
3247191673Sjamie	}
3248188144Sjamie
3249185435Sbz	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3250191673Sjamie		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3251191673Sjamie		mtx_unlock(&pr->pr_mtx);
3252185435Sbz		return (0);
3253185435Sbz	}
3254185435Sbz
3255185435Sbz	/*
3256185435Sbz	 * Return success because nothing had to be changed.
3257185435Sbz	 */
3258191673Sjamie	mtx_unlock(&pr->pr_mtx);
325946155Sphk	return (0);
326046155Sphk}
326146155Sphk
3262185435Sbz/*
3263188144Sjamie * Check if given address belongs to the jail referenced by cred/prison.
3264185435Sbz *
3265192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3266192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3267192895Sjamie * doesn't allow IPv6.
3268185435Sbz */
3269185435Sbzstatic int
3270185435Sbz_prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
327146155Sphk{
3272185435Sbz	int i, a, z, d;
327346155Sphk
3274185435Sbz	/*
3275185435Sbz	 * Check the primary IP.
3276185435Sbz	 */
3277185435Sbz	if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3278188144Sjamie		return (0);
3279185435Sbz
3280185435Sbz	/*
3281185435Sbz	 * All the other IPs are sorted so we can do a binary search.
3282185435Sbz	 */
3283185435Sbz	a = 0;
3284185435Sbz	z = pr->pr_ip6s - 2;
3285185435Sbz	while (a <= z) {
3286185435Sbz		i = (a + z) / 2;
3287185435Sbz		d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3288185435Sbz		if (d > 0)
3289185435Sbz			z = i - 1;
3290185435Sbz		else if (d < 0)
3291185435Sbz			a = i + 1;
329246155Sphk		else
3293188144Sjamie			return (0);
329446155Sphk	}
3295188144Sjamie
3296188144Sjamie	return (EADDRNOTAVAIL);
329746155Sphk}
329846155Sphk
329946155Sphkint
3300185435Sbzprison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3301185435Sbz{
3302191673Sjamie	struct prison *pr;
3303191673Sjamie	int error;
3304185435Sbz
3305185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3306185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3307185435Sbz
3308192895Sjamie	pr = cred->cr_prison;
3309192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3310188144Sjamie		return (0);
3311191673Sjamie	mtx_lock(&pr->pr_mtx);
3312192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3313192895Sjamie		mtx_unlock(&pr->pr_mtx);
3314192895Sjamie		return (0);
3315192895Sjamie	}
3316191673Sjamie	if (pr->pr_ip6 == NULL) {
3317191673Sjamie		mtx_unlock(&pr->pr_mtx);
3318188144Sjamie		return (EAFNOSUPPORT);
3319191673Sjamie	}
3320185435Sbz
3321191673Sjamie	error = _prison_check_ip6(pr, ia6);
3322191673Sjamie	mtx_unlock(&pr->pr_mtx);
3323191673Sjamie	return (error);
3324185435Sbz}
3325185435Sbz#endif
3326185435Sbz
3327185435Sbz/*
3328188146Sjamie * Check if a jail supports the given address family.
3329188146Sjamie *
3330188146Sjamie * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3331188146Sjamie * if not.
3332188146Sjamie */
3333188146Sjamieint
3334188146Sjamieprison_check_af(struct ucred *cred, int af)
3335188146Sjamie{
3336192895Sjamie	struct prison *pr;
3337188146Sjamie	int error;
3338188146Sjamie
3339188146Sjamie	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3340188146Sjamie
3341192895Sjamie	pr = cred->cr_prison;
3342194923Sjamie#ifdef VIMAGE
3343194915Sjamie	/* Prisons with their own network stack are not limited. */
3344200473Sbz	if (prison_owns_vnet(cred))
3345194915Sjamie		return (0);
3346194923Sjamie#endif
3347194915Sjamie
3348188146Sjamie	error = 0;
3349188146Sjamie	switch (af)
3350188146Sjamie	{
3351188146Sjamie#ifdef INET
3352188146Sjamie	case AF_INET:
3353192895Sjamie		if (pr->pr_flags & PR_IP4)
3354192895Sjamie		{
3355192895Sjamie			mtx_lock(&pr->pr_mtx);
3356192895Sjamie			if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3357192895Sjamie				error = EAFNOSUPPORT;
3358192895Sjamie			mtx_unlock(&pr->pr_mtx);
3359192895Sjamie		}
3360188146Sjamie		break;
3361188146Sjamie#endif
3362188146Sjamie#ifdef INET6
3363188146Sjamie	case AF_INET6:
3364192895Sjamie		if (pr->pr_flags & PR_IP6)
3365192895Sjamie		{
3366192895Sjamie			mtx_lock(&pr->pr_mtx);
3367192895Sjamie			if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3368192895Sjamie				error = EAFNOSUPPORT;
3369192895Sjamie			mtx_unlock(&pr->pr_mtx);
3370192895Sjamie		}
3371188146Sjamie		break;
3372188146Sjamie#endif
3373188146Sjamie	case AF_LOCAL:
3374188146Sjamie	case AF_ROUTE:
3375188146Sjamie		break;
3376188146Sjamie	default:
3377192895Sjamie		if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3378188146Sjamie			error = EAFNOSUPPORT;
3379188146Sjamie	}
3380188146Sjamie	return (error);
3381188146Sjamie}
3382188146Sjamie
3383188146Sjamie/*
3384185435Sbz * Check if given address belongs to the jail referenced by cred (wrapper to
3385185435Sbz * prison_check_ip[46]).
3386185435Sbz *
3387192895Sjamie * Returns 0 if jail doesn't restrict the address family or if address belongs
3388192895Sjamie * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3389192895Sjamie * the jail doesn't allow the address family.  IPv4 Address passed in in NBO.
3390185435Sbz */
3391185435Sbzint
339272786Srwatsonprison_if(struct ucred *cred, struct sockaddr *sa)
339346155Sphk{
3394185435Sbz#ifdef INET
3395114168Smike	struct sockaddr_in *sai;
3396185435Sbz#endif
3397185435Sbz#ifdef INET6
3398185435Sbz	struct sockaddr_in6 *sai6;
3399185435Sbz#endif
3400188144Sjamie	int error;
340146155Sphk
3402185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3403185435Sbz	KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3404185435Sbz
3405200473Sbz#ifdef VIMAGE
3406200473Sbz	if (prison_owns_vnet(cred))
3407200473Sbz		return (0);
3408200473Sbz#endif
3409200473Sbz
3410188144Sjamie	error = 0;
3411188144Sjamie	switch (sa->sa_family)
3412185435Sbz	{
3413185435Sbz#ifdef INET
3414185435Sbz	case AF_INET:
3415185435Sbz		sai = (struct sockaddr_in *)sa;
3416188144Sjamie		error = prison_check_ip4(cred, &sai->sin_addr);
3417185435Sbz		break;
3418185435Sbz#endif
3419185435Sbz#ifdef INET6
3420185435Sbz	case AF_INET6:
3421185435Sbz		sai6 = (struct sockaddr_in6 *)sa;
3422188144Sjamie		error = prison_check_ip6(cred, &sai6->sin6_addr);
3423185435Sbz		break;
3424185435Sbz#endif
3425185435Sbz	default:
3426192895Sjamie		if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3427188144Sjamie			error = EAFNOSUPPORT;
3428185435Sbz	}
3429188144Sjamie	return (error);
343046155Sphk}
343172786Srwatson
343272786Srwatson/*
343372786Srwatson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
343472786Srwatson */
343572786Srwatsonint
3436114168Smikeprison_check(struct ucred *cred1, struct ucred *cred2)
343772786Srwatson{
343872786Srwatson
3439192895Sjamie	return ((cred1->cr_prison == cred2->cr_prison ||
3440192895Sjamie	    prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3441192895Sjamie}
344272786Srwatson
3443192895Sjamie/*
3444192895Sjamie * Return 1 if p2 is a child of p1, otherwise 0.
3445192895Sjamie */
3446192895Sjamieint
3447192895Sjamieprison_ischild(struct prison *pr1, struct prison *pr2)
3448192895Sjamie{
3449192895Sjamie
3450192895Sjamie	for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3451192895Sjamie		if (pr1 == pr2)
3452192895Sjamie			return (1);
345372786Srwatson	return (0);
345472786Srwatson}
345572786Srwatson
345672786Srwatson/*
345772786Srwatson * Return 1 if the passed credential is in a jail, otherwise 0.
345872786Srwatson */
345972786Srwatsonint
3460114168Smikejailed(struct ucred *cred)
346172786Srwatson{
346272786Srwatson
3463192895Sjamie	return (cred->cr_prison != &prison0);
346472786Srwatson}
346591384Srobert
346691384Srobert/*
3467200473Sbz * Return 1 if the passed credential is in a jail and that jail does not
3468200473Sbz * have its own virtual network stack, otherwise 0.
3469200473Sbz */
3470200473Sbzint
3471200473Sbzjailed_without_vnet(struct ucred *cred)
3472200473Sbz{
3473200473Sbz
3474200473Sbz	if (!jailed(cred))
3475200473Sbz		return (0);
3476200473Sbz#ifdef VIMAGE
3477200473Sbz	if (prison_owns_vnet(cred))
3478200473Sbz		return (0);
3479200473Sbz#endif
3480200473Sbz
3481200473Sbz	return (1);
3482200473Sbz}
3483200473Sbz
3484200473Sbz/*
3485194090Sjamie * Return the correct hostname (domainname, et al) for the passed credential.
348691384Srobert */
348791391Srobertvoid
3488114168Smikegetcredhostname(struct ucred *cred, char *buf, size_t size)
348991384Srobert{
3490193066Sjamie	struct prison *pr;
349191384Srobert
3492194090Sjamie	/*
3493194090Sjamie	 * A NULL credential can be used to shortcut to the physical
3494194090Sjamie	 * system's hostname.
3495194090Sjamie	 */
3496193066Sjamie	pr = (cred != NULL) ? cred->cr_prison : &prison0;
3497193066Sjamie	mtx_lock(&pr->pr_mtx);
3498194118Sjamie	strlcpy(buf, pr->pr_hostname, size);
3499193066Sjamie	mtx_unlock(&pr->pr_mtx);
350091384Srobert}
3501113275Smike
3502194090Sjamievoid
3503194090Sjamiegetcreddomainname(struct ucred *cred, char *buf, size_t size)
3504194090Sjamie{
3505194090Sjamie
3506194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3507194118Sjamie	strlcpy(buf, cred->cr_prison->pr_domainname, size);
3508194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3509194090Sjamie}
3510194090Sjamie
3511194090Sjamievoid
3512194090Sjamiegetcredhostuuid(struct ucred *cred, char *buf, size_t size)
3513194090Sjamie{
3514194090Sjamie
3515194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3516194118Sjamie	strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3517194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3518194090Sjamie}
3519194090Sjamie
3520194090Sjamievoid
3521194090Sjamiegetcredhostid(struct ucred *cred, unsigned long *hostid)
3522194090Sjamie{
3523194090Sjamie
3524194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3525194090Sjamie	*hostid = cred->cr_prison->pr_hostid;
3526194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3527194090Sjamie}
3528194090Sjamie
3529196176Sbz#ifdef VIMAGE
3530125804Srwatson/*
3531196176Sbz * Determine whether the prison represented by cred owns
3532196176Sbz * its vnet rather than having it inherited.
3533196176Sbz *
3534196176Sbz * Returns 1 in case the prison owns the vnet, 0 otherwise.
3535196176Sbz */
3536196176Sbzint
3537196176Sbzprison_owns_vnet(struct ucred *cred)
3538196176Sbz{
3539196176Sbz
3540196176Sbz	/*
3541196176Sbz	 * vnets cannot be added/removed after jail creation,
3542196176Sbz	 * so no need to lock here.
3543196176Sbz	 */
3544196176Sbz	return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3545196176Sbz}
3546196176Sbz#endif
3547196176Sbz
3548196176Sbz/*
3549147185Spjd * Determine whether the subject represented by cred can "see"
3550147185Spjd * status of a mount point.
3551147185Spjd * Returns: 0 for permitted, ENOENT otherwise.
3552147185Spjd * XXX: This function should be called cr_canseemount() and should be
3553147185Spjd *      placed in kern_prot.c.
3554125804Srwatson */
3555125804Srwatsonint
3556147185Spjdprison_canseemount(struct ucred *cred, struct mount *mp)
3557125804Srwatson{
3558147185Spjd	struct prison *pr;
3559147185Spjd	struct statfs *sp;
3560147185Spjd	size_t len;
3561125804Srwatson
3562192895Sjamie	pr = cred->cr_prison;
3563192895Sjamie	if (pr->pr_enforce_statfs == 0)
3564147185Spjd		return (0);
3565147185Spjd	if (pr->pr_root->v_mount == mp)
3566147185Spjd		return (0);
3567192895Sjamie	if (pr->pr_enforce_statfs == 2)
3568147185Spjd		return (ENOENT);
3569147185Spjd	/*
3570147185Spjd	 * If jail's chroot directory is set to "/" we should be able to see
3571147185Spjd	 * all mount-points from inside a jail.
3572147185Spjd	 * This is ugly check, but this is the only situation when jail's
3573147185Spjd	 * directory ends with '/'.
3574147185Spjd	 */
3575147185Spjd	if (strcmp(pr->pr_path, "/") == 0)
3576147185Spjd		return (0);
3577147185Spjd	len = strlen(pr->pr_path);
3578147185Spjd	sp = &mp->mnt_stat;
3579147185Spjd	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3580147185Spjd		return (ENOENT);
3581147185Spjd	/*
3582147185Spjd	 * Be sure that we don't have situation where jail's root directory
3583147185Spjd	 * is "/some/path" and mount point is "/some/pathpath".
3584147185Spjd	 */
3585147185Spjd	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3586147185Spjd		return (ENOENT);
3587147185Spjd	return (0);
3588147185Spjd}
3589147185Spjd
3590147185Spjdvoid
3591147185Spjdprison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3592147185Spjd{
3593147185Spjd	char jpath[MAXPATHLEN];
3594147185Spjd	struct prison *pr;
3595147185Spjd	size_t len;
3596147185Spjd
3597192895Sjamie	pr = cred->cr_prison;
3598192895Sjamie	if (pr->pr_enforce_statfs == 0)
3599147185Spjd		return;
3600147185Spjd	if (prison_canseemount(cred, mp) != 0) {
3601147185Spjd		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3602147185Spjd		strlcpy(sp->f_mntonname, "[restricted]",
3603147185Spjd		    sizeof(sp->f_mntonname));
3604147185Spjd		return;
3605125804Srwatson	}
3606147185Spjd	if (pr->pr_root->v_mount == mp) {
3607147185Spjd		/*
3608147185Spjd		 * Clear current buffer data, so we are sure nothing from
3609147185Spjd		 * the valid path left there.
3610147185Spjd		 */
3611147185Spjd		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3612147185Spjd		*sp->f_mntonname = '/';
3613147185Spjd		return;
3614147185Spjd	}
3615147185Spjd	/*
3616147185Spjd	 * If jail's chroot directory is set to "/" we should be able to see
3617147185Spjd	 * all mount-points from inside a jail.
3618147185Spjd	 */
3619147185Spjd	if (strcmp(pr->pr_path, "/") == 0)
3620147185Spjd		return;
3621147185Spjd	len = strlen(pr->pr_path);
3622147185Spjd	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3623147185Spjd	/*
3624147185Spjd	 * Clear current buffer data, so we are sure nothing from
3625147185Spjd	 * the valid path left there.
3626147185Spjd	 */
3627147185Spjd	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3628147185Spjd	if (*jpath == '\0') {
3629147185Spjd		/* Should never happen. */
3630147185Spjd		*sp->f_mntonname = '/';
3631147185Spjd	} else {
3632147185Spjd		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3633147185Spjd	}
3634125804Srwatson}
3635125804Srwatson
3636164032Srwatson/*
3637164032Srwatson * Check with permission for a specific privilege is granted within jail.  We
3638164032Srwatson * have a specific list of accepted privileges; the rest are denied.
3639164032Srwatson */
3640164032Srwatsonint
3641164032Srwatsonprison_priv_check(struct ucred *cred, int priv)
3642164032Srwatson{
3643164032Srwatson
3644164032Srwatson	if (!jailed(cred))
3645164032Srwatson		return (0);
3646164032Srwatson
3647194915Sjamie#ifdef VIMAGE
3648194915Sjamie	/*
3649194915Sjamie	 * Privileges specific to prisons with a virtual network stack.
3650194915Sjamie	 * There might be a duplicate entry here in case the privilege
3651194915Sjamie	 * is only granted conditionally in the legacy jail case.
3652194915Sjamie	 */
3653164032Srwatson	switch (priv) {
3654194915Sjamie#ifdef notyet
3655194915Sjamie		/*
3656194915Sjamie		 * NFS-specific privileges.
3657194915Sjamie		 */
3658194915Sjamie	case PRIV_NFS_DAEMON:
3659194915Sjamie	case PRIV_NFS_LOCKD:
3660194915Sjamie#endif
3661194915Sjamie		/*
3662194915Sjamie		 * Network stack privileges.
3663194915Sjamie		 */
3664194915Sjamie	case PRIV_NET_BRIDGE:
3665194915Sjamie	case PRIV_NET_GRE:
3666194915Sjamie	case PRIV_NET_BPF:
3667194915Sjamie	case PRIV_NET_RAW:		/* Dup, cond. in legacy jail case. */
3668194915Sjamie	case PRIV_NET_ROUTE:
3669194915Sjamie	case PRIV_NET_TAP:
3670194915Sjamie	case PRIV_NET_SETIFMTU:
3671194915Sjamie	case PRIV_NET_SETIFFLAGS:
3672194915Sjamie	case PRIV_NET_SETIFCAP:
3673203052Sdelphij	case PRIV_NET_SETIFDESCR:
3674194915Sjamie	case PRIV_NET_SETIFNAME	:
3675194915Sjamie	case PRIV_NET_SETIFMETRIC:
3676194915Sjamie	case PRIV_NET_SETIFPHYS:
3677194915Sjamie	case PRIV_NET_SETIFMAC:
3678194915Sjamie	case PRIV_NET_ADDMULTI:
3679194915Sjamie	case PRIV_NET_DELMULTI:
3680194915Sjamie	case PRIV_NET_HWIOCTL:
3681194915Sjamie	case PRIV_NET_SETLLADDR:
3682194915Sjamie	case PRIV_NET_ADDIFGROUP:
3683194915Sjamie	case PRIV_NET_DELIFGROUP:
3684194915Sjamie	case PRIV_NET_IFCREATE:
3685194915Sjamie	case PRIV_NET_IFDESTROY:
3686194915Sjamie	case PRIV_NET_ADDIFADDR:
3687194915Sjamie	case PRIV_NET_DELIFADDR:
3688194915Sjamie	case PRIV_NET_LAGG:
3689194915Sjamie	case PRIV_NET_GIF:
3690194915Sjamie	case PRIV_NET_SETIFVNET:
3691223735Sbz	case PRIV_NET_SETIFFIB:
3692164032Srwatson
3693164032Srwatson		/*
3694194915Sjamie		 * 802.11-related privileges.
3695194915Sjamie		 */
3696194915Sjamie	case PRIV_NET80211_GETKEY:
3697194915Sjamie#ifdef notyet
3698194915Sjamie	case PRIV_NET80211_MANAGE:		/* XXX-BZ discuss with sam@ */
3699194915Sjamie#endif
3700194915Sjamie
3701194915Sjamie#ifdef notyet
3702194915Sjamie		/*
3703194915Sjamie		 * AppleTalk privileges.
3704194915Sjamie		 */
3705194915Sjamie	case PRIV_NETATALK_RESERVEDPORT:
3706194915Sjamie
3707194915Sjamie		/*
3708194915Sjamie		 * ATM privileges.
3709194915Sjamie		 */
3710194915Sjamie	case PRIV_NETATM_CFG:
3711194915Sjamie	case PRIV_NETATM_ADD:
3712194915Sjamie	case PRIV_NETATM_DEL:
3713194915Sjamie	case PRIV_NETATM_SET:
3714194915Sjamie
3715194915Sjamie		/*
3716194915Sjamie		 * Bluetooth privileges.
3717194915Sjamie		 */
3718194915Sjamie	case PRIV_NETBLUETOOTH_RAW:
3719194915Sjamie#endif
3720194915Sjamie
3721194915Sjamie		/*
3722194915Sjamie		 * Netgraph and netgraph module privileges.
3723194915Sjamie		 */
3724194915Sjamie	case PRIV_NETGRAPH_CONTROL:
3725194915Sjamie#ifdef notyet
3726194915Sjamie	case PRIV_NETGRAPH_TTY:
3727194915Sjamie#endif
3728194915Sjamie
3729194915Sjamie		/*
3730194915Sjamie		 * IPv4 and IPv6 privileges.
3731194915Sjamie		 */
3732194915Sjamie	case PRIV_NETINET_IPFW:
3733194915Sjamie	case PRIV_NETINET_DIVERT:
3734194915Sjamie	case PRIV_NETINET_PF:
3735194915Sjamie	case PRIV_NETINET_DUMMYNET:
3736194915Sjamie	case PRIV_NETINET_CARP:
3737194915Sjamie	case PRIV_NETINET_MROUTE:
3738194915Sjamie	case PRIV_NETINET_RAW:
3739194915Sjamie	case PRIV_NETINET_ADDRCTRL6:
3740194915Sjamie	case PRIV_NETINET_ND6:
3741194915Sjamie	case PRIV_NETINET_SCOPE6:
3742194915Sjamie	case PRIV_NETINET_ALIFETIME6:
3743194915Sjamie	case PRIV_NETINET_IPSEC:
3744194915Sjamie	case PRIV_NETINET_BINDANY:
3745194915Sjamie
3746194915Sjamie#ifdef notyet
3747194915Sjamie		/*
3748194915Sjamie		 * IPX/SPX privileges.
3749194915Sjamie		 */
3750194915Sjamie	case PRIV_NETIPX_RESERVEDPORT:
3751194915Sjamie	case PRIV_NETIPX_RAW:
3752194915Sjamie
3753194915Sjamie		/*
3754194915Sjamie		 * NCP privileges.
3755194915Sjamie		 */
3756194915Sjamie	case PRIV_NETNCP:
3757194915Sjamie
3758194915Sjamie		/*
3759194915Sjamie		 * SMB privileges.
3760194915Sjamie		 */
3761194915Sjamie	case PRIV_NETSMB:
3762194915Sjamie#endif
3763194915Sjamie
3764194915Sjamie	/*
3765194915Sjamie	 * No default: or deny here.
3766194915Sjamie	 * In case of no permit fall through to next switch().
3767194915Sjamie	 */
3768194915Sjamie		if (cred->cr_prison->pr_flags & PR_VNET)
3769194915Sjamie			return (0);
3770194915Sjamie	}
3771194915Sjamie#endif /* VIMAGE */
3772194915Sjamie
3773194915Sjamie	switch (priv) {
3774194915Sjamie
3775194915Sjamie		/*
3776164032Srwatson		 * Allow ktrace privileges for root in jail.
3777164032Srwatson		 */
3778164032Srwatson	case PRIV_KTRACE:
3779164032Srwatson
3780166827Srwatson#if 0
3781164032Srwatson		/*
3782164032Srwatson		 * Allow jailed processes to configure audit identity and
3783164032Srwatson		 * submit audit records (login, etc).  In the future we may
3784164032Srwatson		 * want to further refine the relationship between audit and
3785164032Srwatson		 * jail.
3786164032Srwatson		 */
3787164032Srwatson	case PRIV_AUDIT_GETAUDIT:
3788164032Srwatson	case PRIV_AUDIT_SETAUDIT:
3789164032Srwatson	case PRIV_AUDIT_SUBMIT:
3790166827Srwatson#endif
3791164032Srwatson
3792164032Srwatson		/*
3793164032Srwatson		 * Allow jailed processes to manipulate process UNIX
3794164032Srwatson		 * credentials in any way they see fit.
3795164032Srwatson		 */
3796164032Srwatson	case PRIV_CRED_SETUID:
3797164032Srwatson	case PRIV_CRED_SETEUID:
3798164032Srwatson	case PRIV_CRED_SETGID:
3799164032Srwatson	case PRIV_CRED_SETEGID:
3800164032Srwatson	case PRIV_CRED_SETGROUPS:
3801164032Srwatson	case PRIV_CRED_SETREUID:
3802164032Srwatson	case PRIV_CRED_SETREGID:
3803164032Srwatson	case PRIV_CRED_SETRESUID:
3804164032Srwatson	case PRIV_CRED_SETRESGID:
3805164032Srwatson
3806164032Srwatson		/*
3807164032Srwatson		 * Jail implements visibility constraints already, so allow
3808164032Srwatson		 * jailed root to override uid/gid-based constraints.
3809164032Srwatson		 */
3810164032Srwatson	case PRIV_SEEOTHERGIDS:
3811164032Srwatson	case PRIV_SEEOTHERUIDS:
3812164032Srwatson
3813164032Srwatson		/*
3814164032Srwatson		 * Jail implements inter-process debugging limits already, so
3815164032Srwatson		 * allow jailed root various debugging privileges.
3816164032Srwatson		 */
3817164032Srwatson	case PRIV_DEBUG_DIFFCRED:
3818164032Srwatson	case PRIV_DEBUG_SUGID:
3819164032Srwatson	case PRIV_DEBUG_UNPRIV:
3820164032Srwatson
3821164032Srwatson		/*
3822164032Srwatson		 * Allow jail to set various resource limits and login
3823164032Srwatson		 * properties, and for now, exceed process resource limits.
3824164032Srwatson		 */
3825164032Srwatson	case PRIV_PROC_LIMIT:
3826164032Srwatson	case PRIV_PROC_SETLOGIN:
3827164032Srwatson	case PRIV_PROC_SETRLIMIT:
3828164032Srwatson
3829164032Srwatson		/*
3830164032Srwatson		 * System V and POSIX IPC privileges are granted in jail.
3831164032Srwatson		 */
3832164032Srwatson	case PRIV_IPC_READ:
3833164032Srwatson	case PRIV_IPC_WRITE:
3834164032Srwatson	case PRIV_IPC_ADMIN:
3835164032Srwatson	case PRIV_IPC_MSGSIZE:
3836164032Srwatson	case PRIV_MQ_ADMIN:
3837164032Srwatson
3838164032Srwatson		/*
3839192895Sjamie		 * Jail operations within a jail work on child jails.
3840192895Sjamie		 */
3841192895Sjamie	case PRIV_JAIL_ATTACH:
3842192895Sjamie	case PRIV_JAIL_SET:
3843192895Sjamie	case PRIV_JAIL_REMOVE:
3844192895Sjamie
3845192895Sjamie		/*
3846164032Srwatson		 * Jail implements its own inter-process limits, so allow
3847164032Srwatson		 * root processes in jail to change scheduling on other
3848164032Srwatson		 * processes in the same jail.  Likewise for signalling.
3849164032Srwatson		 */
3850164032Srwatson	case PRIV_SCHED_DIFFCRED:
3851185435Sbz	case PRIV_SCHED_CPUSET:
3852164032Srwatson	case PRIV_SIGNAL_DIFFCRED:
3853164032Srwatson	case PRIV_SIGNAL_SUGID:
3854164032Srwatson
3855164032Srwatson		/*
3856164032Srwatson		 * Allow jailed processes to write to sysctls marked as jail
3857164032Srwatson		 * writable.
3858164032Srwatson		 */
3859164032Srwatson	case PRIV_SYSCTL_WRITEJAIL:
3860164032Srwatson
3861164032Srwatson		/*
3862164032Srwatson		 * Allow root in jail to manage a variety of quota
3863166831Srwatson		 * properties.  These should likely be conditional on a
3864166831Srwatson		 * configuration option.
3865164032Srwatson		 */
3866166832Srwatson	case PRIV_VFS_GETQUOTA:
3867166832Srwatson	case PRIV_VFS_SETQUOTA:
3868164032Srwatson
3869164032Srwatson		/*
3870164032Srwatson		 * Since Jail relies on chroot() to implement file system
3871164032Srwatson		 * protections, grant many VFS privileges to root in jail.
3872164032Srwatson		 * Be careful to exclude mount-related and NFS-related
3873164032Srwatson		 * privileges.
3874164032Srwatson		 */
3875164032Srwatson	case PRIV_VFS_READ:
3876164032Srwatson	case PRIV_VFS_WRITE:
3877164032Srwatson	case PRIV_VFS_ADMIN:
3878164032Srwatson	case PRIV_VFS_EXEC:
3879164032Srwatson	case PRIV_VFS_LOOKUP:
3880164032Srwatson	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
3881164032Srwatson	case PRIV_VFS_CHFLAGS_DEV:
3882164032Srwatson	case PRIV_VFS_CHOWN:
3883164032Srwatson	case PRIV_VFS_CHROOT:
3884167152Spjd	case PRIV_VFS_RETAINSUGID:
3885164032Srwatson	case PRIV_VFS_FCHROOT:
3886164032Srwatson	case PRIV_VFS_LINK:
3887164032Srwatson	case PRIV_VFS_SETGID:
3888172860Srwatson	case PRIV_VFS_STAT:
3889164032Srwatson	case PRIV_VFS_STICKYFILE:
3890164032Srwatson		return (0);
3891164032Srwatson
3892164032Srwatson		/*
3893164032Srwatson		 * Depending on the global setting, allow privilege of
3894164032Srwatson		 * setting system flags.
3895164032Srwatson		 */
3896164032Srwatson	case PRIV_VFS_SYSFLAGS:
3897192895Sjamie		if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3898164032Srwatson			return (0);
3899164032Srwatson		else
3900164032Srwatson			return (EPERM);
3901164032Srwatson
3902164032Srwatson		/*
3903168396Spjd		 * Depending on the global setting, allow privilege of
3904168396Spjd		 * mounting/unmounting file systems.
3905168396Spjd		 */
3906168396Spjd	case PRIV_VFS_MOUNT:
3907168396Spjd	case PRIV_VFS_UNMOUNT:
3908168396Spjd	case PRIV_VFS_MOUNT_NONUSER:
3909168699Spjd	case PRIV_VFS_MOUNT_OWNER:
3910224615Smm		if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT &&
3911224615Smm		    cred->cr_prison->pr_enforce_statfs < 2)
3912168396Spjd			return (0);
3913168396Spjd		else
3914168396Spjd			return (EPERM);
3915168396Spjd
3916168396Spjd		/*
3917168591Srwatson		 * Allow jailed root to bind reserved ports and reuse in-use
3918168591Srwatson		 * ports.
3919164032Srwatson		 */
3920164032Srwatson	case PRIV_NETINET_RESERVEDPORT:
3921168591Srwatson	case PRIV_NETINET_REUSEPORT:
3922164032Srwatson		return (0);
3923164032Srwatson
3924164032Srwatson		/*
3925175630Sbz		 * Allow jailed root to set certian IPv4/6 (option) headers.
3926175630Sbz		 */
3927175630Sbz	case PRIV_NETINET_SETHDROPTS:
3928175630Sbz		return (0);
3929175630Sbz
3930175630Sbz		/*
3931164032Srwatson		 * Conditionally allow creating raw sockets in jail.
3932164032Srwatson		 */
3933164032Srwatson	case PRIV_NETINET_RAW:
3934192895Sjamie		if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3935164032Srwatson			return (0);
3936164032Srwatson		else
3937164032Srwatson			return (EPERM);
3938164032Srwatson
3939164032Srwatson		/*
3940164032Srwatson		 * Since jail implements its own visibility limits on netstat
3941164032Srwatson		 * sysctls, allow getcred.  This allows identd to work in
3942164032Srwatson		 * jail.
3943164032Srwatson		 */
3944164032Srwatson	case PRIV_NETINET_GETCRED:
3945164032Srwatson		return (0);
3946164032Srwatson
3947219304Strasz		/*
3948219304Strasz		 * Allow jailed root to set loginclass.
3949219304Strasz		 */
3950219304Strasz	case PRIV_PROC_SETLOGINCLASS:
3951219304Strasz		return (0);
3952219304Strasz
3953164032Srwatson	default:
3954164032Srwatson		/*
3955164032Srwatson		 * In all remaining cases, deny the privilege request.  This
3956164032Srwatson		 * includes almost all network privileges, many system
3957164032Srwatson		 * configuration privileges.
3958164032Srwatson		 */
3959164032Srwatson		return (EPERM);
3960164032Srwatson	}
3961164032Srwatson}
3962164032Srwatson
3963192895Sjamie/*
3964192895Sjamie * Return the part of pr2's name that is relative to pr1, or the whole name
3965192895Sjamie * if it does not directly follow.
3966192895Sjamie */
3967192895Sjamie
3968192895Sjamiechar *
3969192895Sjamieprison_name(struct prison *pr1, struct prison *pr2)
3970192895Sjamie{
3971192895Sjamie	char *name;
3972192895Sjamie
3973192895Sjamie	/* Jails see themselves as "0" (if they see themselves at all). */
3974192895Sjamie	if (pr1 == pr2)
3975192895Sjamie		return "0";
3976192895Sjamie	name = pr2->pr_name;
3977192895Sjamie	if (prison_ischild(pr1, pr2)) {
3978192895Sjamie		/*
3979192895Sjamie		 * pr1 isn't locked (and allprison_lock may not be either)
3980192895Sjamie		 * so its length can't be counted on.  But the number of dots
3981192895Sjamie		 * can be counted on - and counted.
3982192895Sjamie		 */
3983192895Sjamie		for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3984192895Sjamie			name = strchr(name, '.') + 1;
3985192895Sjamie	}
3986192895Sjamie	return (name);
3987192895Sjamie}
3988192895Sjamie
3989192895Sjamie/*
3990192895Sjamie * Return the part of pr2's path that is relative to pr1, or the whole path
3991192895Sjamie * if it does not directly follow.
3992192895Sjamie */
3993192895Sjamiestatic char *
3994192895Sjamieprison_path(struct prison *pr1, struct prison *pr2)
3995192895Sjamie{
3996192895Sjamie	char *path1, *path2;
3997192895Sjamie	int len1;
3998192895Sjamie
3999192895Sjamie	path1 = pr1->pr_path;
4000192895Sjamie	path2 = pr2->pr_path;
4001192895Sjamie	if (!strcmp(path1, "/"))
4002192895Sjamie		return (path2);
4003192895Sjamie	len1 = strlen(path1);
4004192895Sjamie	if (strncmp(path1, path2, len1))
4005192895Sjamie		return (path2);
4006192895Sjamie	if (path2[len1] == '\0')
4007192895Sjamie		return "/";
4008192895Sjamie	if (path2[len1] == '/')
4009192895Sjamie		return (path2 + len1);
4010192895Sjamie	return (path2);
4011192895Sjamie}
4012192895Sjamie
4013192895Sjamie
4014192895Sjamie/*
4015192895Sjamie * Jail-related sysctls.
4016192895Sjamie */
4017227309Sedstatic SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
4018192895Sjamie    "Jails");
4019192895Sjamie
4020113275Smikestatic int
4021113275Smikesysctl_jail_list(SYSCTL_HANDLER_ARGS)
4022113275Smike{
4023191673Sjamie	struct xprison *xp;
4024192895Sjamie	struct prison *pr, *cpr;
4025191673Sjamie#ifdef INET
4026191673Sjamie	struct in_addr *ip4 = NULL;
4027191673Sjamie	int ip4s = 0;
4028191673Sjamie#endif
4029191673Sjamie#ifdef INET6
4030208803Scperciva	struct in6_addr *ip6 = NULL;
4031191673Sjamie	int ip6s = 0;
4032191673Sjamie#endif
4033192895Sjamie	int descend, error;
4034113275Smike
4035191673Sjamie	xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
4036192895Sjamie	pr = req->td->td_ucred->cr_prison;
4037191673Sjamie	error = 0;
4038168401Spjd	sx_slock(&allprison_lock);
4039192895Sjamie	FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
4040192895Sjamie#if defined(INET) || defined(INET6)
4041191673Sjamie again:
4042192895Sjamie#endif
4043192895Sjamie		mtx_lock(&cpr->pr_mtx);
4044185435Sbz#ifdef INET
4045192895Sjamie		if (cpr->pr_ip4s > 0) {
4046192895Sjamie			if (ip4s < cpr->pr_ip4s) {
4047192895Sjamie				ip4s = cpr->pr_ip4s;
4048192895Sjamie				mtx_unlock(&cpr->pr_mtx);
4049191673Sjamie				ip4 = realloc(ip4, ip4s *
4050191673Sjamie				    sizeof(struct in_addr), M_TEMP, M_WAITOK);
4051191673Sjamie				goto again;
4052191673Sjamie			}
4053192895Sjamie			bcopy(cpr->pr_ip4, ip4,
4054192895Sjamie			    cpr->pr_ip4s * sizeof(struct in_addr));
4055191673Sjamie		}
4056185435Sbz#endif
4057185435Sbz#ifdef INET6
4058192895Sjamie		if (cpr->pr_ip6s > 0) {
4059192895Sjamie			if (ip6s < cpr->pr_ip6s) {
4060192895Sjamie				ip6s = cpr->pr_ip6s;
4061192895Sjamie				mtx_unlock(&cpr->pr_mtx);
4062191673Sjamie				ip6 = realloc(ip6, ip6s *
4063191673Sjamie				    sizeof(struct in6_addr), M_TEMP, M_WAITOK);
4064191673Sjamie				goto again;
4065191673Sjamie			}
4066192895Sjamie			bcopy(cpr->pr_ip6, ip6,
4067192895Sjamie			    cpr->pr_ip6s * sizeof(struct in6_addr));
4068191673Sjamie		}
4069185435Sbz#endif
4070192895Sjamie		if (cpr->pr_ref == 0) {
4071192895Sjamie			mtx_unlock(&cpr->pr_mtx);
4072191673Sjamie			continue;
4073191673Sjamie		}
4074191673Sjamie		bzero(xp, sizeof(*xp));
4075113275Smike		xp->pr_version = XPRISON_VERSION;
4076192895Sjamie		xp->pr_id = cpr->pr_id;
4077192895Sjamie		xp->pr_state = cpr->pr_uref > 0
4078191673Sjamie		    ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
4079192895Sjamie		strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
4080194118Sjamie		strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
4081192895Sjamie		strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
4082185435Sbz#ifdef INET
4083192895Sjamie		xp->pr_ip4s = cpr->pr_ip4s;
4084185435Sbz#endif
4085185435Sbz#ifdef INET6
4086192895Sjamie		xp->pr_ip6s = cpr->pr_ip6s;
4087185435Sbz#endif
4088192895Sjamie		mtx_unlock(&cpr->pr_mtx);
4089191673Sjamie		error = SYSCTL_OUT(req, xp, sizeof(*xp));
4090191673Sjamie		if (error)
4091191673Sjamie			break;
4092185435Sbz#ifdef INET
4093191673Sjamie		if (xp->pr_ip4s > 0) {
4094191673Sjamie			error = SYSCTL_OUT(req, ip4,
4095191673Sjamie			    xp->pr_ip4s * sizeof(struct in_addr));
4096191673Sjamie			if (error)
4097191673Sjamie				break;
4098185435Sbz		}
4099185435Sbz#endif
4100185435Sbz#ifdef INET6
4101191673Sjamie		if (xp->pr_ip6s > 0) {
4102191673Sjamie			error = SYSCTL_OUT(req, ip6,
4103191673Sjamie			    xp->pr_ip6s * sizeof(struct in6_addr));
4104191673Sjamie			if (error)
4105191673Sjamie				break;
4106185435Sbz		}
4107185435Sbz#endif
4108113275Smike	}
4109168401Spjd	sx_sunlock(&allprison_lock);
4110191673Sjamie	free(xp, M_TEMP);
4111191673Sjamie#ifdef INET
4112191673Sjamie	free(ip4, M_TEMP);
4113191673Sjamie#endif
4114191673Sjamie#ifdef INET6
4115191673Sjamie	free(ip6, M_TEMP);
4116191673Sjamie#endif
4117167354Spjd	return (error);
4118113275Smike}
4119113275Smike
4120187864SedSYSCTL_OID(_security_jail, OID_AUTO, list,
4121187864Sed    CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4122187864Sed    sysctl_jail_list, "S", "List of active jails");
4123126004Spjd
4124126004Spjdstatic int
4125126004Spjdsysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4126126004Spjd{
4127126004Spjd	int error, injail;
4128126004Spjd
4129126004Spjd	injail = jailed(req->td->td_ucred);
4130126004Spjd	error = SYSCTL_OUT(req, &injail, sizeof(injail));
4131126004Spjd
4132126004Spjd	return (error);
4133126004Spjd}
4134192895Sjamie
4135187864SedSYSCTL_PROC(_security_jail, OID_AUTO, jailed,
4136187864Sed    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4137187864Sed    sysctl_jail_jailed, "I", "Process in jail?");
4138185435Sbz
4139192895Sjamie#if defined(INET) || defined(INET6)
4140193865SjamieSYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
4141192895Sjamie    &jail_max_af_ips, 0,
4142192895Sjamie    "Number of IP addresses a jail may have at most per address family");
4143192895Sjamie#endif
4144192895Sjamie
4145192895Sjamie/*
4146192895Sjamie * Default parameters for jail(2) compatability.  For historical reasons,
4147192895Sjamie * the sysctl names have varying similarity to the parameter names.  Prisons
4148192895Sjamie * just see their own parameters, and can't change them.
4149192895Sjamie */
4150192895Sjamiestatic int
4151192895Sjamiesysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
4152192895Sjamie{
4153192895Sjamie	struct prison *pr;
4154192895Sjamie	int allow, error, i;
4155192895Sjamie
4156192895Sjamie	pr = req->td->td_ucred->cr_prison;
4157192895Sjamie	allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
4158192895Sjamie
4159192895Sjamie	/* Get the current flag value, and convert it to a boolean. */
4160192895Sjamie	i = (allow & arg2) ? 1 : 0;
4161192895Sjamie	if (arg1 != NULL)
4162192895Sjamie		i = !i;
4163192895Sjamie	error = sysctl_handle_int(oidp, &i, 0, req);
4164192895Sjamie	if (error || !req->newptr)
4165192895Sjamie		return (error);
4166192895Sjamie	i = i ? arg2 : 0;
4167192895Sjamie	if (arg1 != NULL)
4168192895Sjamie		i ^= arg2;
4169192895Sjamie	/*
4170192895Sjamie	 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
4171192895Sjamie	 * for writing.
4172192895Sjamie	 */
4173192895Sjamie	mtx_lock(&prison0.pr_mtx);
4174192895Sjamie	jail_default_allow = (jail_default_allow & ~arg2) | i;
4175192895Sjamie	mtx_unlock(&prison0.pr_mtx);
4176192895Sjamie	return (0);
4177192895Sjamie}
4178192895Sjamie
4179192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
4180192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4181192895Sjamie    NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
4182192895Sjamie    "Processes in jail can set their hostnames");
4183192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
4184192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4185192895Sjamie    (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
4186192895Sjamie    "Processes in jail are limited to creating UNIX/IP/route sockets only");
4187192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4188192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4189192895Sjamie    NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4190192895Sjamie    "Processes in jail can use System V IPC primitives");
4191192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4192192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4193192895Sjamie    NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4194192895Sjamie    "Prison root can create raw sockets");
4195192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4196192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4197192895Sjamie    NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4198192895Sjamie    "Processes in jail can alter system file flags");
4199192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4200192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4201192895Sjamie    NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4202192895Sjamie    "Processes in jail can mount/unmount jail-friendly file systems");
4203232059SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_devfs_allowed,
4204232059Smm    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4205232059Smm    NULL, PR_ALLOW_MOUNT_DEVFS, sysctl_jail_default_allow, "I",
4206232186Smm    "Processes in jail can mount the devfs file system");
4207232059SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_nullfs_allowed,
4208232059Smm    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4209232059Smm    NULL, PR_ALLOW_MOUNT_NULLFS, sysctl_jail_default_allow, "I",
4210232186Smm    "Processes in jail can mount the nullfs file system");
4211232278SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_procfs_allowed,
4212232278Smm    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4213232278Smm    NULL, PR_ALLOW_MOUNT_PROCFS, sysctl_jail_default_allow, "I",
4214232278Smm    "Processes in jail can mount the procfs file system");
4215232186SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_zfs_allowed,
4216232186Smm    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4217232186Smm    NULL, PR_ALLOW_MOUNT_ZFS, sysctl_jail_default_allow, "I",
4218232186Smm    "Processes in jail can mount the zfs file system");
4219192895Sjamie
4220192895Sjamiestatic int
4221192895Sjamiesysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
4222192895Sjamie{
4223192895Sjamie	struct prison *pr;
4224192895Sjamie	int level, error;
4225192895Sjamie
4226192895Sjamie	pr = req->td->td_ucred->cr_prison;
4227192895Sjamie	level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4228192895Sjamie	error = sysctl_handle_int(oidp, &level, 0, req);
4229192895Sjamie	if (error || !req->newptr)
4230192895Sjamie		return (error);
4231192895Sjamie	*(int *)arg1 = level;
4232192895Sjamie	return (0);
4233192895Sjamie}
4234192895Sjamie
4235192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4236192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4237192895Sjamie    &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4238192895Sjamie    sysctl_jail_default_level, "I",
4239192895Sjamie    "Processes in jail cannot see all mounted file systems");
4240192895Sjamie
4241231267SmmSYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
4242231267Smm    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
4243231267Smm    &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
4244231267Smm    sysctl_jail_default_level, "I",
4245231267Smm    "Ruleset for the devfs filesystem in jail");
4246231267Smm
4247192895Sjamie/*
4248192895Sjamie * Nodes to describe jail parameters.  Maximum length of string parameters
4249192895Sjamie * is returned in the string itself, and the other parameters exist merely
4250192895Sjamie * to make themselves and their types known.
4251192895Sjamie */
4252192895SjamieSYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
4253192895Sjamie    "Jail parameters");
4254192895Sjamie
4255192895Sjamieint
4256192895Sjamiesysctl_jail_param(SYSCTL_HANDLER_ARGS)
4257192895Sjamie{
4258192895Sjamie	int i;
4259192895Sjamie	long l;
4260192895Sjamie	size_t s;
4261192895Sjamie	char numbuf[12];
4262192895Sjamie
4263192895Sjamie	switch (oidp->oid_kind & CTLTYPE)
4264192895Sjamie	{
4265192895Sjamie	case CTLTYPE_LONG:
4266192895Sjamie	case CTLTYPE_ULONG:
4267192895Sjamie		l = 0;
4268192895Sjamie#ifdef SCTL_MASK32
4269192895Sjamie		if (!(req->flags & SCTL_MASK32))
4270192895Sjamie#endif
4271192895Sjamie			return (SYSCTL_OUT(req, &l, sizeof(l)));
4272192895Sjamie	case CTLTYPE_INT:
4273192895Sjamie	case CTLTYPE_UINT:
4274192895Sjamie		i = 0;
4275192895Sjamie		return (SYSCTL_OUT(req, &i, sizeof(i)));
4276192895Sjamie	case CTLTYPE_STRING:
4277219819Sjeff		snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
4278192895Sjamie		return
4279192895Sjamie		    (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4280192895Sjamie	case CTLTYPE_STRUCT:
4281192895Sjamie		s = (size_t)arg2;
4282192895Sjamie		return (SYSCTL_OUT(req, &s, sizeof(s)));
4283192895Sjamie	}
4284192895Sjamie	return (0);
4285192895Sjamie}
4286192895Sjamie
4287192895SjamieSYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4288192895SjamieSYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4289192895SjamieSYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4290192895SjamieSYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4291192895SjamieSYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4292192895Sjamie    "I", "Jail secure level");
4293192895SjamieSYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4294192895Sjamie    "I", "Jail cannot see all mounted file systems");
4295231267SmmSYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
4296231267Smm    "I", "Ruleset for in-jail devfs mounts");
4297192895SjamieSYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4298192895Sjamie    "B", "Jail persistence");
4299194251Sjamie#ifdef VIMAGE
4300194251SjamieSYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4301195870Sjamie    "E,jailsys", "Virtual network stack");
4302194251Sjamie#endif
4303192895SjamieSYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4304192895Sjamie    "B", "Jail is in the process of shutting down");
4305192895Sjamie
4306194762SjamieSYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4307194762SjamieSYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4308194762Sjamie    "I", "Current number of child jails");
4309194762SjamieSYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4310194762Sjamie    "I", "Maximum number of child jails");
4311194762Sjamie
4312195870SjamieSYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4313192895SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4314192895Sjamie    "Jail hostname");
4315193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4316193066Sjamie    "Jail NIS domainname");
4317193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4318193066Sjamie    "Jail host UUID");
4319193066SjamieSYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4320193066Sjamie    "LU", "Jail host ID");
4321192895Sjamie
4322192895SjamieSYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4323192895SjamieSYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4324192895Sjamie
4325192895Sjamie#ifdef INET
4326195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4327195974Sjamie    "Jail IPv4 address virtualization");
4328192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4329192895Sjamie    "S,in_addr,a", "Jail IPv4 addresses");
4330202468SbzSYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4331202468Sbz    "B", "Do (not) use IPv4 source address selection rather than the "
4332202468Sbz    "primary jail IPv4 address.");
4333192895Sjamie#endif
4334192895Sjamie#ifdef INET6
4335195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4336195974Sjamie    "Jail IPv6 address virtualization");
4337192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4338192895Sjamie    "S,in6_addr,a", "Jail IPv6 addresses");
4339202468SbzSYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4340202468Sbz    "B", "Do (not) use IPv6 source address selection rather than the "
4341202468Sbz    "primary jail IPv6 address.");
4342192895Sjamie#endif
4343192895Sjamie
4344192895SjamieSYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4345192895SjamieSYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4346192895Sjamie    "B", "Jail may set hostname");
4347192895SjamieSYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4348192895Sjamie    "B", "Jail may use SYSV IPC");
4349192895SjamieSYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4350192895Sjamie    "B", "Jail may create raw sockets");
4351192895SjamieSYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4352192895Sjamie    "B", "Jail may alter system file flags");
4353192895SjamieSYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4354192895Sjamie    "B", "Jail may set file quotas");
4355192895SjamieSYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4356192895Sjamie    "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4357192895Sjamie
4358232059SmmSYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
4359232059SmmSYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
4360232059Smm    "B", "Jail may mount/unmount jail-friendly file systems in general");
4361232059SmmSYSCTL_JAIL_PARAM(_allow_mount, devfs, CTLTYPE_INT | CTLFLAG_RW,
4362232186Smm    "B", "Jail may mount the devfs file system");
4363232059SmmSYSCTL_JAIL_PARAM(_allow_mount, nullfs, CTLTYPE_INT | CTLFLAG_RW,
4364232186Smm    "B", "Jail may mount the nullfs file system");
4365232278SmmSYSCTL_JAIL_PARAM(_allow_mount, procfs, CTLTYPE_INT | CTLFLAG_RW,
4366232278Smm    "B", "Jail may mount the procfs file system");
4367232186SmmSYSCTL_JAIL_PARAM(_allow_mount, zfs, CTLTYPE_INT | CTLFLAG_RW,
4368232186Smm    "B", "Jail may mount the zfs file system");
4369232059Smm
4370220137Straszvoid
4371220137Straszprison_racct_foreach(void (*callback)(struct racct *racct,
4372220137Strasz    void *arg2, void *arg3), void *arg2, void *arg3)
4373220137Strasz{
4374221362Strasz	struct prison_racct *prr;
4375192895Sjamie
4376220137Strasz	sx_slock(&allprison_lock);
4377221362Strasz	LIST_FOREACH(prr, &allprison_racct, prr_next)
4378221362Strasz		(callback)(prr->prr_racct, arg2, arg3);
4379220137Strasz	sx_sunlock(&allprison_lock);
4380220137Strasz}
4381220137Strasz
4382221362Straszstatic struct prison_racct *
4383221362Straszprison_racct_find_locked(const char *name)
4384221362Strasz{
4385221362Strasz	struct prison_racct *prr;
4386221362Strasz
4387221362Strasz	sx_assert(&allprison_lock, SA_XLOCKED);
4388221362Strasz
4389221362Strasz	if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4390221362Strasz		return (NULL);
4391221362Strasz
4392221362Strasz	LIST_FOREACH(prr, &allprison_racct, prr_next) {
4393221362Strasz		if (strcmp(name, prr->prr_name) != 0)
4394221362Strasz			continue;
4395221362Strasz
4396221362Strasz		/* Found prison_racct with a matching name? */
4397221362Strasz		prison_racct_hold(prr);
4398221362Strasz		return (prr);
4399221362Strasz	}
4400221362Strasz
4401221362Strasz	/* Add new prison_racct. */
4402221362Strasz	prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4403221362Strasz	racct_create(&prr->prr_racct);
4404221362Strasz
4405221362Strasz	strcpy(prr->prr_name, name);
4406221362Strasz	refcount_init(&prr->prr_refcount, 1);
4407221362Strasz	LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4408221362Strasz
4409221362Strasz	return (prr);
4410221362Strasz}
4411221362Strasz
4412221362Straszstruct prison_racct *
4413221362Straszprison_racct_find(const char *name)
4414221362Strasz{
4415221362Strasz	struct prison_racct *prr;
4416221362Strasz
4417221362Strasz	sx_xlock(&allprison_lock);
4418221362Strasz	prr = prison_racct_find_locked(name);
4419221362Strasz	sx_xunlock(&allprison_lock);
4420221362Strasz	return (prr);
4421221362Strasz}
4422221362Strasz
4423221362Straszvoid
4424221362Straszprison_racct_hold(struct prison_racct *prr)
4425221362Strasz{
4426221362Strasz
4427221362Strasz	refcount_acquire(&prr->prr_refcount);
4428221362Strasz}
4429221362Strasz
4430221362Straszvoid
4431221362Straszprison_racct_free(struct prison_racct *prr)
4432221362Strasz{
4433221362Strasz	int old;
4434221362Strasz
4435221362Strasz	old = prr->prr_refcount;
4436221362Strasz	if (old > 1 && atomic_cmpset_int(&prr->prr_refcount, old, old - 1))
4437221362Strasz		return;
4438221362Strasz
4439221362Strasz	sx_xlock(&allprison_lock);
4440221362Strasz	if (refcount_release(&prr->prr_refcount)) {
4441221362Strasz		racct_destroy(&prr->prr_racct);
4442221362Strasz		LIST_REMOVE(prr, prr_next);
4443221362Strasz		sx_xunlock(&allprison_lock);
4444221362Strasz		free(prr, M_PRISON_RACCT);
4445221362Strasz
4446221362Strasz		return;
4447221362Strasz	}
4448221362Strasz	sx_xunlock(&allprison_lock);
4449221362Strasz}
4450221362Strasz
4451221362Strasz#ifdef RACCT
4452221362Straszstatic void
4453221362Straszprison_racct_attach(struct prison *pr)
4454221362Strasz{
4455221362Strasz	struct prison_racct *prr;
4456221362Strasz
4457221362Strasz	prr = prison_racct_find_locked(pr->pr_name);
4458221362Strasz	KASSERT(prr != NULL, ("cannot find prison_racct"));
4459221362Strasz
4460221362Strasz	pr->pr_prison_racct = prr;
4461221362Strasz}
4462221362Strasz
4463221362Straszstatic void
4464221362Straszprison_racct_detach(struct prison *pr)
4465221362Strasz{
4466221362Strasz	prison_racct_free(pr->pr_prison_racct);
4467221362Strasz	pr->pr_prison_racct = NULL;
4468221362Strasz}
4469221362Strasz#endif /* RACCT */
4470221362Strasz
4471185435Sbz#ifdef DDB
4472191673Sjamie
4473191673Sjamiestatic void
4474191673Sjamiedb_show_prison(struct prison *pr)
4475185435Sbz{
4476192895Sjamie	int fi;
4477191673Sjamie#if defined(INET) || defined(INET6)
4478191673Sjamie	int ii;
4479185435Sbz#endif
4480195870Sjamie	unsigned jsf;
4481185435Sbz#ifdef INET6
4482185435Sbz	char ip6buf[INET6_ADDRSTRLEN];
4483185435Sbz#endif
4484185435Sbz
4485191673Sjamie	db_printf("prison %p:\n", pr);
4486191673Sjamie	db_printf(" jid             = %d\n", pr->pr_id);
4487191673Sjamie	db_printf(" name            = %s\n", pr->pr_name);
4488192895Sjamie	db_printf(" parent          = %p\n", pr->pr_parent);
4489191673Sjamie	db_printf(" ref             = %d\n", pr->pr_ref);
4490191673Sjamie	db_printf(" uref            = %d\n", pr->pr_uref);
4491191673Sjamie	db_printf(" path            = %s\n", pr->pr_path);
4492191673Sjamie	db_printf(" cpuset          = %d\n", pr->pr_cpuset
4493191673Sjamie	    ? pr->pr_cpuset->cs_id : -1);
4494194251Sjamie#ifdef VIMAGE
4495194251Sjamie	db_printf(" vnet            = %p\n", pr->pr_vnet);
4496194251Sjamie#endif
4497191673Sjamie	db_printf(" root            = %p\n", pr->pr_root);
4498191673Sjamie	db_printf(" securelevel     = %d\n", pr->pr_securelevel);
4499231267Smm	db_printf(" devfs_rsnum     = %d\n", pr->pr_devfs_rsnum);
4500202123Sbz	db_printf(" children.max    = %d\n", pr->pr_childmax);
4501202123Sbz	db_printf(" children.cur    = %d\n", pr->pr_childcount);
4502192895Sjamie	db_printf(" child           = %p\n", LIST_FIRST(&pr->pr_children));
4503192895Sjamie	db_printf(" sibling         = %p\n", LIST_NEXT(pr, pr_sibling));
4504202123Sbz	db_printf(" flags           = 0x%x", pr->pr_flags);
4505192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
4506192895Sjamie	    fi++)
4507192895Sjamie		if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
4508192895Sjamie			db_printf(" %s", pr_flag_names[fi]);
4509195870Sjamie	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
4510195870Sjamie	    fi++) {
4511195870Sjamie		jsf = pr->pr_flags &
4512195870Sjamie		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
4513195870Sjamie		db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
4514195870Sjamie		    pr_flag_jailsys[fi].disable &&
4515195870Sjamie		      (jsf == pr_flag_jailsys[fi].disable) ? "disable"
4516195870Sjamie		    : (jsf == pr_flag_jailsys[fi].new) ? "new"
4517195870Sjamie		    : "inherit");
4518195870Sjamie	}
4519202123Sbz	db_printf(" allow           = 0x%x", pr->pr_allow);
4520192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
4521192895Sjamie	    fi++)
4522192895Sjamie		if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
4523192895Sjamie			db_printf(" %s", pr_allow_names[fi]);
4524191673Sjamie	db_printf("\n");
4525192895Sjamie	db_printf(" enforce_statfs  = %d\n", pr->pr_enforce_statfs);
4526194118Sjamie	db_printf(" host.hostname   = %s\n", pr->pr_hostname);
4527194118Sjamie	db_printf(" host.domainname = %s\n", pr->pr_domainname);
4528194118Sjamie	db_printf(" host.hostuuid   = %s\n", pr->pr_hostuuid);
4529193066Sjamie	db_printf(" host.hostid     = %lu\n", pr->pr_hostid);
4530185435Sbz#ifdef INET
4531191673Sjamie	db_printf(" ip4s            = %d\n", pr->pr_ip4s);
4532191673Sjamie	for (ii = 0; ii < pr->pr_ip4s; ii++)
4533191673Sjamie		db_printf(" %s %s\n",
4534202123Sbz		    ii == 0 ? "ip4.addr        =" : "                 ",
4535191673Sjamie		    inet_ntoa(pr->pr_ip4[ii]));
4536185435Sbz#endif
4537185435Sbz#ifdef INET6
4538191673Sjamie	db_printf(" ip6s            = %d\n", pr->pr_ip6s);
4539191673Sjamie	for (ii = 0; ii < pr->pr_ip6s; ii++)
4540191673Sjamie		db_printf(" %s %s\n",
4541202123Sbz		    ii == 0 ? "ip6.addr        =" : "                 ",
4542191673Sjamie		    ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4543191673Sjamie#endif
4544191673Sjamie}
4545191673Sjamie
4546191673SjamieDB_SHOW_COMMAND(prison, db_show_prison_command)
4547191673Sjamie{
4548191673Sjamie	struct prison *pr;
4549191673Sjamie
4550191673Sjamie	if (!have_addr) {
4551192895Sjamie		/*
4552192895Sjamie		 * Show all prisons in the list, and prison0 which is not
4553192895Sjamie		 * listed.
4554192895Sjamie		 */
4555192895Sjamie		db_show_prison(&prison0);
4556192895Sjamie		if (!db_pager_quit) {
4557192895Sjamie			TAILQ_FOREACH(pr, &allprison, pr_list) {
4558192895Sjamie				db_show_prison(pr);
4559192895Sjamie				if (db_pager_quit)
4560192895Sjamie					break;
4561192895Sjamie			}
4562191673Sjamie		}
4563191673Sjamie		return;
4564191673Sjamie	}
4565191673Sjamie
4566192895Sjamie	if (addr == 0)
4567192895Sjamie		pr = &prison0;
4568192895Sjamie	else {
4569192895Sjamie		/* Look for a prison with the ID and with references. */
4570191673Sjamie		TAILQ_FOREACH(pr, &allprison, pr_list)
4571192895Sjamie			if (pr->pr_id == addr && pr->pr_ref > 0)
4572191673Sjamie				break;
4573192895Sjamie		if (pr == NULL)
4574192895Sjamie			/* Look again, without requiring a reference. */
4575192895Sjamie			TAILQ_FOREACH(pr, &allprison, pr_list)
4576192895Sjamie				if (pr->pr_id == addr)
4577192895Sjamie					break;
4578192895Sjamie		if (pr == NULL)
4579192895Sjamie			/* Assume address points to a valid prison. */
4580192895Sjamie			pr = (struct prison *)addr;
4581192895Sjamie	}
4582191673Sjamie	db_show_prison(pr);
4583185435Sbz}
4584191673Sjamie
4585185435Sbz#endif /* DDB */
4586