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