2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Marcel Moolenaar
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/kernel.h>
35 #include <sys/systm.h>
36 #include <sys/sysctl.h>
38 #include <sys/malloc.h>
39 #include <sys/mount.h>
44 #include <compat/linux/linux_mib.h>
45 #include <compat/linux/linux_misc.h>
48 char pr_osname[LINUX_MAX_UTSNAME];
49 char pr_osrelease[LINUX_MAX_UTSNAME];
54 static struct linux_prison lprison0 = {
56 .pr_osrelease = LINUX_VERSION_STR,
57 .pr_oss_version = 0x030600,
58 .pr_osrel = LINUX_VERSION_CODE
61 static unsigned linux_osd_jail_slot;
63 SYSCTL_NODE(_compat, OID_AUTO, linux, CTLFLAG_RW, 0, "Linux mode");
65 int linux_ignore_ip_recverr = 1;
66 SYSCTL_INT(_compat_linux, OID_AUTO, ignore_ip_recverr, CTLFLAG_RWTUN,
67 &linux_ignore_ip_recverr, 0, "Ignore enabling IP_RECVERR");
69 int linux_preserve_vstatus = 0;
70 SYSCTL_INT(_compat_linux, OID_AUTO, preserve_vstatus, CTLFLAG_RWTUN,
71 &linux_preserve_vstatus, 0, "Preserve VSTATUS termios(4) flag");
73 static int linux_set_osname(struct thread *td, char *osname);
74 static int linux_set_osrelease(struct thread *td, char *osrelease);
75 static int linux_set_oss_version(struct thread *td, int oss_version);
78 linux_sysctl_osname(SYSCTL_HANDLER_ARGS)
80 char osname[LINUX_MAX_UTSNAME];
83 linux_get_osname(req->td, osname);
84 error = sysctl_handle_string(oidp, osname, LINUX_MAX_UTSNAME, req);
85 if (error != 0 || req->newptr == NULL)
87 error = linux_set_osname(req->td, osname);
92 SYSCTL_PROC(_compat_linux, OID_AUTO, osname,
93 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
94 0, 0, linux_sysctl_osname, "A",
95 "Linux kernel OS name");
98 linux_sysctl_osrelease(SYSCTL_HANDLER_ARGS)
100 char osrelease[LINUX_MAX_UTSNAME];
103 linux_get_osrelease(req->td, osrelease);
104 error = sysctl_handle_string(oidp, osrelease, LINUX_MAX_UTSNAME, req);
105 if (error != 0 || req->newptr == NULL)
107 error = linux_set_osrelease(req->td, osrelease);
112 SYSCTL_PROC(_compat_linux, OID_AUTO, osrelease,
113 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
114 0, 0, linux_sysctl_osrelease, "A",
115 "Linux kernel OS release");
118 linux_sysctl_oss_version(SYSCTL_HANDLER_ARGS)
123 oss_version = linux_get_oss_version(req->td);
124 error = sysctl_handle_int(oidp, &oss_version, 0, req);
125 if (error != 0 || req->newptr == NULL)
127 error = linux_set_oss_version(req->td, oss_version);
132 SYSCTL_PROC(_compat_linux, OID_AUTO, oss_version,
133 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
134 0, 0, linux_sysctl_oss_version, "I",
135 "Linux OSS version");
138 * Map the osrelease into integer
141 linux_map_osrel(char *osrelease, int *osrel)
143 char *sep, *eosrelease;
144 int len, v0, v1, v2, v;
146 len = strlen(osrelease);
147 eosrelease = osrelease + len;
148 v0 = strtol(osrelease, &sep, 10);
149 if (osrelease == sep || sep + 1 >= eosrelease || *sep != '.')
152 v1 = strtol(osrelease, &sep, 10);
153 if (osrelease == sep || sep + 1 >= eosrelease || *sep != '.')
156 v2 = strtol(osrelease, &sep, 10);
157 if (osrelease == sep ||
158 (sep != eosrelease && (sep + 1 >= eosrelease || *sep != '-')))
161 v = LINUX_KERNVER(v0, v1, v2);
162 if (v < LINUX_KERNVER(1, 0, 0))
172 * Find a prison with Linux info.
173 * Return the Linux info and the (locked) prison.
175 static struct linux_prison *
176 linux_find_prison(struct prison *spr, struct prison **prp)
179 struct linux_prison *lpr;
181 for (pr = spr;; pr = pr->pr_parent) {
182 mtx_lock(&pr->pr_mtx);
183 lpr = (pr == &prison0)
185 : osd_jail_get(pr, linux_osd_jail_slot);
188 mtx_unlock(&pr->pr_mtx);
196 * Ensure a prison has its own Linux info. If lprp is non-null, point it to
197 * the Linux info and lock the prison.
200 linux_alloc_prison(struct prison *pr, struct linux_prison **lprp)
203 struct linux_prison *lpr, *nlpr;
206 /* If this prison already has Linux info, return that. */
207 lpr = linux_find_prison(pr, &ppr);
211 * Allocate a new info record. Then check again, in case something
212 * changed during the allocation.
214 mtx_unlock(&ppr->pr_mtx);
215 nlpr = malloc(sizeof(struct linux_prison), M_PRISON, M_WAITOK);
216 rsv = osd_reserve(linux_osd_jail_slot);
217 lpr = linux_find_prison(pr, &ppr);
219 free(nlpr, M_PRISON);
220 osd_free_reserved(rsv);
223 /* Inherit the initial values from the ancestor. */
224 mtx_lock(&pr->pr_mtx);
225 (void)osd_jail_set_reserved(pr, linux_osd_jail_slot, rsv, nlpr);
226 bcopy(lpr, nlpr, sizeof(*lpr));
228 mtx_unlock(&ppr->pr_mtx);
233 mtx_unlock(&pr->pr_mtx);
237 * Jail OSD methods for Linux prison data.
240 linux_prison_create(void *obj, void *data)
242 struct prison *pr = obj;
243 struct vfsoptlist *opts = data;
246 if (vfs_copyopt(opts, "linux", &jsys, sizeof(jsys)) == 0 &&
247 jsys == JAIL_SYS_INHERIT)
250 * Inherit a prison's initial values from its parent
251 * (different from JAIL_SYS_INHERIT which also inherits changes).
253 linux_alloc_prison(pr, NULL);
258 linux_prison_check(void *obj __unused, void *data)
260 struct vfsoptlist *opts = data;
261 char *osname, *osrelease;
262 int error, jsys, len, oss_version;
264 /* Check that the parameters are correct. */
265 error = vfs_copyopt(opts, "linux", &jsys, sizeof(jsys));
266 if (error != ENOENT) {
269 if (jsys != JAIL_SYS_NEW && jsys != JAIL_SYS_INHERIT)
272 error = vfs_getopt(opts, "linux.osname", (void **)&osname, &len);
273 if (error != ENOENT) {
276 if (len == 0 || osname[len - 1] != '\0')
278 if (len > LINUX_MAX_UTSNAME) {
279 vfs_opterror(opts, "linux.osname too long");
280 return (ENAMETOOLONG);
283 error = vfs_getopt(opts, "linux.osrelease", (void **)&osrelease, &len);
284 if (error != ENOENT) {
287 if (len == 0 || osrelease[len - 1] != '\0')
289 if (len > LINUX_MAX_UTSNAME) {
290 vfs_opterror(opts, "linux.osrelease too long");
291 return (ENAMETOOLONG);
293 error = linux_map_osrel(osrelease, NULL);
295 vfs_opterror(opts, "linux.osrelease format error");
299 error = vfs_copyopt(opts, "linux.oss_version", &oss_version,
300 sizeof(oss_version));
308 linux_prison_set(void *obj, void *data)
310 struct linux_prison *lpr;
311 struct prison *pr = obj;
312 struct vfsoptlist *opts = data;
313 char *osname, *osrelease;
314 int error, gotversion, jsys, len, oss_version;
316 /* Set the parameters, which should be correct. */
317 error = vfs_copyopt(opts, "linux", &jsys, sizeof(jsys));
320 error = vfs_getopt(opts, "linux.osname", (void **)&osname, &len);
325 error = vfs_getopt(opts, "linux.osrelease", (void **)&osrelease, &len);
330 error = vfs_copyopt(opts, "linux.oss_version", &oss_version,
331 sizeof(oss_version));
339 case JAIL_SYS_INHERIT:
340 /* "linux=inherit": inherit the parent's Linux info. */
341 mtx_lock(&pr->pr_mtx);
342 osd_jail_del(pr, linux_osd_jail_slot);
343 mtx_unlock(&pr->pr_mtx);
347 * "linux=new" or "linux.*":
348 * the prison gets its own Linux info.
350 linux_alloc_prison(pr, &lpr);
352 (void)linux_map_osrel(osrelease, &lpr->pr_osrel);
353 strlcpy(lpr->pr_osrelease, osrelease,
357 strlcpy(lpr->pr_osname, osname, LINUX_MAX_UTSNAME);
359 lpr->pr_oss_version = oss_version;
360 mtx_unlock(&pr->pr_mtx);
366 SYSCTL_JAIL_PARAM_SYS_NODE(linux, CTLFLAG_RW, "Jail Linux parameters");
367 SYSCTL_JAIL_PARAM_STRING(_linux, osname, CTLFLAG_RW, LINUX_MAX_UTSNAME,
368 "Jail Linux kernel OS name");
369 SYSCTL_JAIL_PARAM_STRING(_linux, osrelease, CTLFLAG_RW, LINUX_MAX_UTSNAME,
370 "Jail Linux kernel OS release");
371 SYSCTL_JAIL_PARAM(_linux, oss_version, CTLTYPE_INT | CTLFLAG_RW,
372 "I", "Jail Linux OSS version");
375 linux_prison_get(void *obj, void *data)
377 struct linux_prison *lpr;
379 struct prison *pr = obj;
380 struct vfsoptlist *opts = data;
385 /* See if this prison is the one with the Linux info. */
386 lpr = linux_find_prison(pr, &ppr);
387 i = (ppr == pr) ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
388 error = vfs_setopt(opts, "linux", &i, sizeof(i));
389 if (error != 0 && error != ENOENT)
392 error = vfs_setopts(opts, "linux.osname", lpr->pr_osname);
393 if (error != 0 && error != ENOENT)
395 error = vfs_setopts(opts, "linux.osrelease", lpr->pr_osrelease);
396 if (error != 0 && error != ENOENT)
398 error = vfs_setopt(opts, "linux.oss_version",
399 &lpr->pr_oss_version, sizeof(lpr->pr_oss_version));
400 if (error != 0 && error != ENOENT)
404 * If this prison is inheriting its Linux info, report
405 * empty/zero parameters.
407 error = vfs_setopts(opts, "linux.osname", "");
408 if (error != 0 && error != ENOENT)
410 error = vfs_setopts(opts, "linux.osrelease", "");
411 if (error != 0 && error != ENOENT)
413 error = vfs_setopt(opts, "linux.oss_version", &version0,
414 sizeof(lpr->pr_oss_version));
415 if (error != 0 && error != ENOENT)
421 mtx_unlock(&ppr->pr_mtx);
427 linux_prison_destructor(void *data)
430 free(data, M_PRISON);
434 linux_osd_jail_register(void)
437 osd_method_t methods[PR_MAXMETHOD] = {
438 [PR_METHOD_CREATE] = linux_prison_create,
439 [PR_METHOD_GET] = linux_prison_get,
440 [PR_METHOD_SET] = linux_prison_set,
441 [PR_METHOD_CHECK] = linux_prison_check
444 linux_osd_jail_slot =
445 osd_jail_register(linux_prison_destructor, methods);
446 /* Copy the system Linux info to any current prisons. */
447 sx_slock(&allprison_lock);
448 TAILQ_FOREACH(pr, &allprison, pr_list)
449 linux_alloc_prison(pr, NULL);
450 sx_sunlock(&allprison_lock);
454 linux_osd_jail_deregister(void)
457 osd_jail_deregister(linux_osd_jail_slot);
461 linux_get_osname(struct thread *td, char *dst)
464 struct linux_prison *lpr;
466 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
467 bcopy(lpr->pr_osname, dst, LINUX_MAX_UTSNAME);
468 mtx_unlock(&pr->pr_mtx);
472 linux_set_osname(struct thread *td, char *osname)
475 struct linux_prison *lpr;
477 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
478 strlcpy(lpr->pr_osname, osname, LINUX_MAX_UTSNAME);
479 mtx_unlock(&pr->pr_mtx);
485 linux_get_osrelease(struct thread *td, char *dst)
488 struct linux_prison *lpr;
490 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
491 bcopy(lpr->pr_osrelease, dst, LINUX_MAX_UTSNAME);
492 mtx_unlock(&pr->pr_mtx);
496 linux_kernver(struct thread *td)
499 struct linux_prison *lpr;
502 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
503 osrel = lpr->pr_osrel;
504 mtx_unlock(&pr->pr_mtx);
510 linux_set_osrelease(struct thread *td, char *osrelease)
513 struct linux_prison *lpr;
516 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
517 error = linux_map_osrel(osrelease, &lpr->pr_osrel);
519 strlcpy(lpr->pr_osrelease, osrelease, LINUX_MAX_UTSNAME);
520 mtx_unlock(&pr->pr_mtx);
526 linux_get_oss_version(struct thread *td)
529 struct linux_prison *lpr;
532 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
533 version = lpr->pr_oss_version;
534 mtx_unlock(&pr->pr_mtx);
540 linux_set_oss_version(struct thread *td, int oss_version)
543 struct linux_prison *lpr;
545 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
546 lpr->pr_oss_version = oss_version;
547 mtx_unlock(&pr->pr_mtx);