xref: /titanic_50/usr/src/uts/common/syscall/systeminfo.c (revision e29eff43297ff00f74f3111c6d2b040e444d7aea)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All rights reserved.	*/
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
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 
61 	if (count < 0 && command != SI_SET_HOSTNAME &&
62 	    command != SI_SET_SRPC_DOMAIN)
63 		return (set_errno(EINVAL));
64 
65 	/*
66 	 * Deal with the common "get a string" case first.
67 	 */
68 	switch (command) {
69 	case SI_SYSNAME:
70 		kstr = utsname.sysname;
71 		break;
72 	case SI_HOSTNAME:
73 		kstr = uts_nodename();
74 		break;
75 	case SI_RELEASE:
76 		kstr = utsname.release;
77 		break;
78 	case SI_VERSION:
79 		kstr = utsname.version;
80 		break;
81 	case SI_MACHINE:
82 		kstr = utsname.machine;
83 		break;
84 #ifdef _LP64
85 	case SI_ARCHITECTURE_64:
86 	case SI_ARCHITECTURE_K:
87 		kstr = architecture;
88 		break;
89 	case SI_ARCHITECTURE_32:
90 	case SI_ARCHITECTURE:
91 		kstr = architecture_32;
92 		break;
93 	case SI_ARCHITECTURE_NATIVE:
94 		kstr = get_udatamodel() == DATAMODEL_NATIVE ?
95 		    architecture : architecture_32;
96 		break;
97 #else
98 	case SI_ARCHITECTURE_K:
99 	case SI_ARCHITECTURE_32:
100 	case SI_ARCHITECTURE:
101 	case SI_ARCHITECTURE_NATIVE:
102 		kstr = architecture;
103 		break;
104 #endif
105 	case SI_HW_SERIAL:
106 		kstr = hw_serial;
107 		break;
108 	case SI_HW_PROVIDER:
109 		kstr = hw_provider;
110 		break;
111 	case SI_SRPC_DOMAIN:
112 		kstr = curproc->p_zone->zone_domain;
113 		break;
114 	case SI_PLATFORM:
115 		kstr = platform;
116 		break;
117 	case SI_ISALIST:
118 		kstr = isa_list;
119 		break;
120 	default:
121 		kstr = NULL;
122 		break;
123 	}
124 
125 	if (kstr != NULL) {
126 		strcnt = strlen(kstr);
127 		if (count > 0) {
128 			if (count <= strcnt) {
129 				getcnt = count - 1;
130 				if (subyte(buf + getcnt, 0) < 0)
131 					return (set_errno(EFAULT));
132 			} else {
133 				getcnt = strcnt + 1;
134 			}
135 			if (copyout(kstr, buf, getcnt))
136 				return (set_errno(EFAULT));
137 		}
138 		return (strcnt + 1);
139 	}
140 
141 	switch (command) {
142 	case SI_DHCP_CACHE:
143 	{
144 		char	*tmp;
145 		unsigned int tlen, octlen;
146 
147 		if (dhcack == NULL) {
148 			tmp = "";
149 			strcnt = 0;
150 		} else {
151 			/*
152 			 * If the interface didn't have a name (bindable
153 			 * driver) to begin with, it might have one now.
154 			 * So, re-run strplumb_get_netdev_path() to see
155 			 * if one can be established at this time.
156 			 */
157 			if (netdev_path == NULL || netdev_path[0] == '\0') {
158 				netdev_path = strplumb_get_netdev_path();
159 			}
160 			/*
161 			 * If the interface name has not yet been resolved
162 			 * and a validnetdev_path[] was stashed by
163 			 * loadrootmodules in swapgeneric.c, or established
164 			 * above, resolve the interface name now.
165 			 */
166 			if (dhcifname[0] == '\0' &&
167 			    netdev_path != NULL && netdev_path[0] != '\0') {
168 				get_netif_name(netdev_path, dhcifname);
169 			}
170 
171 			/*
172 			 * Form reply:
173 			 *  IFNAMESIZ array of dhcp i/f
174 			 *  hexascii representation of dhcp reply
175 			 */
176 			octlen = dhcacklen * 2 + 1;
177 			tlen = octlen + IFNAMSIZ;
178 			tmp = kmem_alloc(tlen, KM_SLEEP);
179 			(void) strncpy(tmp, dhcifname, IFNAMSIZ);
180 			if (octet_to_hexascii(dhcack, dhcacklen,
181 			    &tmp[IFNAMSIZ], &octlen) != 0) {
182 				kmem_free(tmp, tlen);
183 				error = EINVAL;
184 				break;
185 			} else {
186 				strcnt = IFNAMSIZ + octlen;
187 			}
188 		}
189 
190 		if (count > 0) {
191 			if (count <= strcnt) {
192 				getcnt = count - 1;
193 				if (subyte((buf + getcnt), 0) < 0)
194 					goto fail;
195 			} else {
196 				getcnt = strcnt + 1;
197 			}
198 			if (copyout(tmp, buf, getcnt))
199 				goto fail;
200 		}
201 		if (strcnt != 0)
202 			kmem_free(tmp, tlen);
203 		return (strcnt + 1);
204 fail:
205 		if (strcnt != 0)
206 			kmem_free(tmp, tlen);
207 		error = EFAULT;
208 		break;
209 	}
210 
211 	case SI_SET_HOSTNAME:
212 	{
213 		size_t		len;
214 		char		name[SYS_NMLN];
215 		char		*name_to_use;
216 
217 		if ((error = secpolicy_systeminfo(CRED())) != 0)
218 			break;
219 
220 		name_to_use = uts_nodename();
221 		if ((error = copyinstr(buf, name, SYS_NMLN, &len)) != 0)
222 			break;
223 
224 		/*
225 		 * Must be non-NULL string and string
226 		 * must be less than SYS_NMLN chars.
227 		 */
228 		if (len < 2 || (len == SYS_NMLN && name[SYS_NMLN-1] != '\0')) {
229 			error = EINVAL;
230 			break;
231 		}
232 
233 		/*
234 		 * Copy the name into the relevant zone's nodename.
235 		 */
236 		(void) strcpy(name_to_use, name);
237 
238 		/*
239 		 * Notify other interested parties that the nodename was set
240 		 */
241 		if (name_to_use == utsname.nodename) /* global zone nodename */
242 			nodename_set();
243 
244 		return (len);
245 	}
246 
247 	case SI_SET_SRPC_DOMAIN:
248 	{
249 		char name[SYS_NMLN];
250 		size_t len;
251 
252 		if ((error = secpolicy_systeminfo(CRED())) != 0)
253 			break;
254 		if ((error = copyinstr(buf, name, SYS_NMLN, &len)) != 0)
255 			break;
256 		/*
257 		 * If string passed in is longer than length
258 		 * allowed for domain name, fail.
259 		 */
260 		if (len == SYS_NMLN && name[SYS_NMLN-1] != '\0') {
261 			error = EINVAL;
262 			break;
263 		}
264 
265 		(void) strcpy(curproc->p_zone->zone_domain, name);
266 		return (len);
267 	}
268 
269 	default:
270 		error = EINVAL;
271 		break;
272 	}
273 
274 	return (set_errno(error));
275 }
276 
277 /*
278  * i_path_find_node: Internal routine used by path_to_devinfo
279  * to locate a given nodeid in the device tree.
280  */
281 struct i_path_findnode {
282 	pnode_t nodeid;
283 	dev_info_t *dip;
284 };
285 
286 static int
287 i_path_find_node(dev_info_t *dev, void *arg)
288 {
289 	struct i_path_findnode *f = (struct i_path_findnode *)arg;
290 
291 
292 	if (ddi_get_nodeid(dev) == (int)f->nodeid) {
293 		f->dip = dev;
294 		return (DDI_WALK_TERMINATE);
295 	}
296 	return (DDI_WALK_CONTINUE);
297 }
298 
299 /*
300  * Return the devinfo node to a boot device
301  */
302 static dev_info_t *
303 path_to_devinfo(char *path)
304 {
305 	struct i_path_findnode fn;
306 	extern dev_info_t *top_devinfo;
307 
308 	/*
309 	 * Get the nodeid of the given pathname, if such a mapping exists.
310 	 */
311 	fn.dip = NULL;
312 	fn.nodeid = prom_finddevice(path);
313 	if (fn.nodeid != OBP_BADNODE) {
314 		/*
315 		 * Find the nodeid in our copy of the device tree and return
316 		 * whatever name we used to bind this node to a driver.
317 		 */
318 		ddi_walk_devs(top_devinfo, i_path_find_node, (void *)(&fn));
319 	}
320 
321 	return (fn.dip);
322 }
323 
324 /*
325  * Determine the network interface name from the device path argument.
326  */
327 static void
328 get_netif_name(char *devname, char *ifname)
329 {
330 	dev_info_t	*dip;
331 	major_t		ndev;
332 	char		*name;
333 	int		unit;
334 
335 	dip = path_to_devinfo(devname);
336 	if (dip == NULL) {
337 		cmn_err(CE_WARN, "get_netif_name: "
338 		    "can't bind driver for '%s'\n", devname);
339 		return;
340 	}
341 
342 	ndev = ddi_driver_major(dip);
343 	if (ndev == -1) {
344 		cmn_err(CE_WARN, "get_netif_name: "
345 		    "no driver bound to '%s'\n", devname);
346 		return;
347 	}
348 
349 	name = ddi_major_to_name(ndev);
350 	if (name == NULL) {
351 		cmn_err(CE_WARN, "get_netif_name: "
352 		    "no name for major number %d\n", ndev);
353 		return;
354 	}
355 
356 	unit = i_ddi_devi_get_ppa(dip);
357 	if (unit < 0) {
358 		cmn_err(CE_WARN, "get_netif_name: "
359 		    "illegal unit number %d\n", unit);
360 		return;
361 	}
362 
363 	(void) snprintf(ifname, IFNAMSIZ, "%s%d", name, unit);
364 }
365