xref: /illumos-gate/usr/src/uts/common/syscall/systeminfo.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2020 Oxide Computer Company
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All rights reserved.	*/
29 
30 
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/sysmacros.h>
34 #include <sys/systm.h>
35 #include <sys/tuneable.h>
36 #include <sys/errno.h>
37 #include <sys/cred.h>
38 #include <sys/utsname.h>
39 #include <sys/systeminfo.h>
40 #include <sys/unistd.h>
41 #include <sys/debug.h>
42 #include <sys/bootconf.h>
43 #include <sys/socket.h>
44 #include <sys/policy.h>
45 #include <net/if.h>
46 #include <sys/sunddi.h>
47 #include <sys/promif.h>
48 #include <sys/zone.h>
49 #include <sys/model.h>
50 #include <netinet/inetutil.h>
51 
52 static void get_netif_name(char *, char *);
53 
54 long
55 systeminfo(int command, char *buf, long count)
56 {
57 	int error = 0;
58 	long strcnt, getcnt;
59 	char *kstr;
60 	char hostidp[HW_HOSTID_LEN];
61 
62 	if (count < 0 && command != SI_SET_HOSTNAME &&
63 	    command != SI_SET_SRPC_DOMAIN)
64 		return (set_errno(EINVAL));
65 
66 	/*
67 	 * Deal with the common "get a string" case first.
68 	 */
69 	switch (command) {
70 	case SI_SYSNAME:
71 		kstr = utsname.sysname;
72 		break;
73 	case SI_HOSTNAME:
74 		kstr = uts_nodename();
75 		break;
76 	case SI_RELEASE:
77 		kstr = utsname.release;
78 		break;
79 	case SI_VERSION:
80 		kstr = utsname.version;
81 		break;
82 	case SI_MACHINE:
83 		kstr = utsname.machine;
84 		break;
85 #ifdef _LP64
86 	case SI_ADDRESS_WIDTH:
87 		kstr = "64";
88 		break;
89 	case SI_ARCHITECTURE_64:
90 	case SI_ARCHITECTURE_K:
91 		kstr = architecture;
92 		break;
93 	case SI_ARCHITECTURE_32:
94 	case SI_ARCHITECTURE:
95 		kstr = architecture_32;
96 		break;
97 	case SI_ARCHITECTURE_NATIVE:
98 		kstr = get_udatamodel() == DATAMODEL_NATIVE ?
99 		    architecture : architecture_32;
100 		break;
101 #else
102 	case SI_ADDRESS_WIDTH:
103 		kstr = "32";
104 		break;
105 	case SI_ARCHITECTURE_K:
106 	case SI_ARCHITECTURE_32:
107 	case SI_ARCHITECTURE:
108 	case SI_ARCHITECTURE_NATIVE:
109 		kstr = architecture;
110 		break;
111 #endif
112 	case SI_HW_SERIAL:
113 		(void) snprintf(hostidp, sizeof (hostidp), "%u",
114 		    zone_get_hostid(curzone));
115 		kstr = hostidp;
116 		break;
117 	case SI_HW_PROVIDER:
118 		kstr = hw_provider;
119 		break;
120 	case SI_SRPC_DOMAIN:
121 		kstr = curproc->p_zone->zone_domain;
122 		break;
123 	case SI_PLATFORM:
124 		kstr = platform;
125 		break;
126 	case SI_ISALIST:
127 		kstr = isa_list;
128 		break;
129 	default:
130 		kstr = NULL;
131 		break;
132 	}
133 
134 	if (kstr != NULL) {
135 		strcnt = strlen(kstr);
136 		if (count > 0) {
137 			if (count <= strcnt) {
138 				getcnt = count - 1;
139 				if (subyte(buf + getcnt, 0) < 0)
140 					return (set_errno(EFAULT));
141 			} else {
142 				getcnt = strcnt + 1;
143 			}
144 			if (copyout(kstr, buf, getcnt))
145 				return (set_errno(EFAULT));
146 		}
147 		return (strcnt + 1);
148 	}
149 
150 	switch (command) {
151 	case SI_DHCP_CACHE:
152 	{
153 		char	*tmp;
154 		unsigned int tlen, octlen;
155 
156 		if (dhcack == NULL) {
157 			tmp = "";
158 			strcnt = 0;
159 		} else {
160 			/*
161 			 * If the interface didn't have a name (bindable
162 			 * driver) to begin with, it might have one now.
163 			 * So, re-run strplumb_get_netdev_path() to see
164 			 * if one can be established at this time.
165 			 */
166 			if (netdev_path == NULL || netdev_path[0] == '\0') {
167 				netdev_path = strplumb_get_netdev_path();
168 			}
169 			/*
170 			 * If the interface name has not yet been resolved
171 			 * and a validnetdev_path[] was stashed by
172 			 * loadrootmodules in swapgeneric.c, or established
173 			 * above, resolve the interface name now.
174 			 */
175 			if (dhcifname[0] == '\0' &&
176 			    netdev_path != NULL && netdev_path[0] != '\0') {
177 				get_netif_name(netdev_path, dhcifname);
178 			}
179 
180 			/*
181 			 * Form reply:
182 			 *  IFNAMESIZ array of dhcp i/f
183 			 *  hexascii representation of dhcp reply
184 			 */
185 			octlen = dhcacklen * 2 + 1;
186 			tlen = octlen + IFNAMSIZ;
187 			tmp = kmem_alloc(tlen, KM_SLEEP);
188 			(void) strncpy(tmp, dhcifname, IFNAMSIZ);
189 			if (octet_to_hexascii(dhcack, dhcacklen,
190 			    &tmp[IFNAMSIZ], &octlen) != 0) {
191 				kmem_free(tmp, tlen);
192 				error = EINVAL;
193 				break;
194 			} else {
195 				strcnt = IFNAMSIZ + octlen;
196 			}
197 		}
198 
199 		if (count > 0) {
200 			if (count <= strcnt) {
201 				getcnt = count - 1;
202 				if (subyte((buf + getcnt), 0) < 0)
203 					goto fail;
204 			} else {
205 				getcnt = strcnt + 1;
206 			}
207 			if (copyout(tmp, buf, getcnt))
208 				goto fail;
209 		}
210 		if (strcnt != 0)
211 			kmem_free(tmp, tlen);
212 		return (strcnt + 1);
213 fail:
214 		if (strcnt != 0)
215 			kmem_free(tmp, tlen);
216 		error = EFAULT;
217 		break;
218 	}
219 
220 	case SI_SET_HOSTNAME:
221 	{
222 		size_t		len;
223 		char		name[SYS_NMLN];
224 		char		*name_to_use;
225 
226 		if ((error = secpolicy_systeminfo(CRED())) != 0)
227 			break;
228 
229 		name_to_use = uts_nodename();
230 		if ((error = copyinstr(buf, name, SYS_NMLN, &len)) != 0)
231 			break;
232 
233 		/*
234 		 * Must be non-NULL string and string
235 		 * must be less than SYS_NMLN chars.
236 		 */
237 		if (len < 2 || (len == SYS_NMLN && name[SYS_NMLN-1] != '\0')) {
238 			error = EINVAL;
239 			break;
240 		}
241 
242 		/*
243 		 * Copy the name into the relevant zone's nodename.
244 		 */
245 		(void) strcpy(name_to_use, name);
246 
247 		/*
248 		 * Notify other interested parties that the nodename was set
249 		 */
250 		if (name_to_use == utsname.nodename) /* global zone nodename */
251 			nodename_set();
252 
253 		return (len);
254 	}
255 
256 	case SI_SET_SRPC_DOMAIN:
257 	{
258 		char name[SYS_NMLN];
259 		size_t len;
260 
261 		if ((error = secpolicy_systeminfo(CRED())) != 0)
262 			break;
263 		if ((error = copyinstr(buf, name, SYS_NMLN, &len)) != 0)
264 			break;
265 		/*
266 		 * If string passed in is longer than length
267 		 * allowed for domain name, fail.
268 		 */
269 		if (len == SYS_NMLN && name[SYS_NMLN-1] != '\0') {
270 			error = EINVAL;
271 			break;
272 		}
273 
274 		(void) strcpy(curproc->p_zone->zone_domain, name);
275 		return (len);
276 	}
277 
278 	default:
279 		error = EINVAL;
280 		break;
281 	}
282 
283 	return (set_errno(error));
284 }
285 
286 /*
287  * i_path_find_node: Internal routine used by path_to_devinfo
288  * to locate a given nodeid in the device tree.
289  */
290 struct i_path_findnode {
291 	pnode_t nodeid;
292 	dev_info_t *dip;
293 };
294 
295 static int
296 i_path_find_node(dev_info_t *dev, void *arg)
297 {
298 	struct i_path_findnode *f = (struct i_path_findnode *)arg;
299 
300 
301 	if (ddi_get_nodeid(dev) == (int)f->nodeid) {
302 		f->dip = dev;
303 		return (DDI_WALK_TERMINATE);
304 	}
305 	return (DDI_WALK_CONTINUE);
306 }
307 
308 /*
309  * Return the devinfo node to a boot device
310  */
311 static dev_info_t *
312 path_to_devinfo(char *path)
313 {
314 	struct i_path_findnode fn;
315 	extern dev_info_t *top_devinfo;
316 
317 	/*
318 	 * Get the nodeid of the given pathname, if such a mapping exists.
319 	 */
320 	fn.dip = NULL;
321 	fn.nodeid = prom_finddevice(path);
322 	if (fn.nodeid != OBP_BADNODE) {
323 		/*
324 		 * Find the nodeid in our copy of the device tree and return
325 		 * whatever name we used to bind this node to a driver.
326 		 */
327 		ddi_walk_devs(top_devinfo, i_path_find_node, (void *)(&fn));
328 	}
329 
330 	return (fn.dip);
331 }
332 
333 /*
334  * Determine the network interface name from the device path argument.
335  */
336 static void
337 get_netif_name(char *devname, char *ifname)
338 {
339 	dev_info_t	*dip;
340 	major_t		ndev;
341 	char		*name;
342 	int		unit;
343 
344 	dip = path_to_devinfo(devname);
345 	if (dip == NULL) {
346 		cmn_err(CE_WARN, "get_netif_name: "
347 		    "can't bind driver for '%s'\n", devname);
348 		return;
349 	}
350 
351 	ndev = ddi_driver_major(dip);
352 	if (ndev == -1) {
353 		cmn_err(CE_WARN, "get_netif_name: "
354 		    "no driver bound to '%s'\n", devname);
355 		return;
356 	}
357 
358 	name = ddi_major_to_name(ndev);
359 	if (name == NULL) {
360 		cmn_err(CE_WARN, "get_netif_name: "
361 		    "no name for major number %d\n", ndev);
362 		return;
363 	}
364 
365 	unit = i_ddi_devi_get_ppa(dip);
366 	if (unit < 0) {
367 		cmn_err(CE_WARN, "get_netif_name: "
368 		    "illegal unit number %d\n", unit);
369 		return;
370 	}
371 
372 	(void) snprintf(ifname, IFNAMSIZ, "%s%d", name, unit);
373 }
374