xref: /freebsd/sys/compat/linux/linux_sysctl.c (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2001 Marcel Moolenaar
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_compat.h"
33 
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/proc.h>
40 #include <sys/sdt.h>
41 #include <sys/sysctl.h>
42 #include <sys/systm.h>
43 #include <sys/sbuf.h>
44 
45 #ifdef COMPAT_LINUX32
46 #include <machine/../linux32/linux.h>
47 #include <machine/../linux32/linux32_proto.h>
48 #else
49 #include <machine/../linux/linux.h>
50 #include <machine/../linux/linux_proto.h>
51 #endif
52 
53 #include <compat/linux/linux_dtrace.h>
54 #include <compat/linux/linux_misc.h>
55 #include <compat/linux/linux_util.h>
56 
57 #define	LINUX_CTL_KERN		1
58 #define	LINUX_CTL_VM		2
59 #define	LINUX_CTL_NET		3
60 #define	LINUX_CTL_PROC		4
61 #define	LINUX_CTL_FS		5
62 #define	LINUX_CTL_DEBUG		6
63 #define	LINUX_CTL_DEV		7
64 #define	LINUX_CTL_BUS		8
65 
66 /* CTL_KERN names */
67 #define	LINUX_KERN_OSTYPE	1
68 #define	LINUX_KERN_OSRELEASE	2
69 #define	LINUX_KERN_OSREV	3
70 #define	LINUX_KERN_VERSION	4
71 
72 /* DTrace init */
73 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
74 
75 /**
76  * DTrace probes in this module.
77  */
78 LIN_SDT_PROBE_DEFINE1(sysctl, handle_string, copyout_error, "int");
79 LIN_SDT_PROBE_DEFINE1(sysctl, linux_sysctl, copyin_error, "int");
80 LIN_SDT_PROBE_DEFINE2(sysctl, linux_sysctl, wrong_length, "int", "int");
81 LIN_SDT_PROBE_DEFINE1(sysctl, linux_sysctl, unsupported_sysctl, "char *");
82 
83 #ifdef LINUX_LEGACY_SYSCALLS
84 static int
85 handle_string(struct l___sysctl_args *la, char *value)
86 {
87 	int error;
88 
89 	if (la->oldval != 0) {
90 		l_int len = strlen(value);
91 		error = copyout(value, PTRIN(la->oldval), len + 1);
92 		if (!error && la->oldlenp != 0)
93 			error = copyout(&len, PTRIN(la->oldlenp), sizeof(len));
94 		if (error) {
95 			LIN_SDT_PROBE1(sysctl, handle_string, copyout_error,
96 			    error);
97 			return (error);
98 		}
99 	}
100 
101 	if (la->newval != 0)
102 		return (ENOTDIR);
103 
104 	return (0);
105 }
106 
107 int
108 linux_sysctl(struct thread *td, struct linux_sysctl_args *args)
109 {
110 	struct l___sysctl_args la;
111 	struct sbuf *sb;
112 	l_int *mib;
113 	char *sysctl_string;
114 	int error, i;
115 
116 	error = copyin(args->args, &la, sizeof(la));
117 	if (error) {
118 		LIN_SDT_PROBE1(sysctl, linux_sysctl, copyin_error, error);
119 		return (error);
120 	}
121 
122 	if (la.nlen <= 0 || la.nlen > LINUX_CTL_MAXNAME) {
123 		LIN_SDT_PROBE2(sysctl, linux_sysctl, wrong_length, la.nlen,
124 		    LINUX_CTL_MAXNAME);
125 		return (ENOTDIR);
126 	}
127 
128 	mib = malloc(la.nlen * sizeof(l_int), M_LINUX, M_WAITOK);
129 	error = copyin(PTRIN(la.name), mib, la.nlen * sizeof(l_int));
130 	if (error) {
131 		LIN_SDT_PROBE1(sysctl, linux_sysctl, copyin_error, error);
132 		free(mib, M_LINUX);
133 		return (error);
134 	}
135 
136 	switch (mib[0]) {
137 	case LINUX_CTL_KERN:
138 		if (la.nlen < 2)
139 			break;
140 
141 		switch (mib[1]) {
142 		case LINUX_KERN_VERSION:
143 			error = handle_string(&la, version);
144 			free(mib, M_LINUX);
145 			return (error);
146 		default:
147 			break;
148 		}
149 		break;
150 	default:
151 		break;
152 	}
153 
154 	sb = sbuf_new(NULL, NULL, 20 + la.nlen * 5, SBUF_AUTOEXTEND);
155 	if (sb == NULL) {
156 		linux_msg(td, "sysctl is not implemented");
157 		LIN_SDT_PROBE1(sysctl, linux_sysctl, unsupported_sysctl,
158 		    "unknown sysctl, ENOMEM during lookup");
159 	} else {
160 		sbuf_printf(sb, "sysctl ");
161 		for (i = 0; i < la.nlen; i++)
162 			sbuf_printf(sb, "%c%d", (i) ? ',' : '{', mib[i]);
163 		sbuf_printf(sb, "} is not implemented");
164 		sbuf_finish(sb);
165 		sysctl_string = sbuf_data(sb);
166 		linux_msg(td, "%s", sbuf_data(sb));
167 		LIN_SDT_PROBE1(sysctl, linux_sysctl, unsupported_sysctl,
168 		    sysctl_string);
169 		sbuf_delete(sb);
170 	}
171 
172 	free(mib, M_LINUX);
173 
174 	return (ENOTDIR);
175 }
176 #endif
177