17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5abddd56eScb117521 * Common Development and Distribution License (the "License").
6abddd56eScb117521 * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*5679c89fSjv227347 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All rights reserved. */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #include <sys/tuneable.h>
347c478bd9Sstevel@tonic-gate #include <sys/errno.h>
357c478bd9Sstevel@tonic-gate #include <sys/cred.h>
367c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
377c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
387c478bd9Sstevel@tonic-gate #include <sys/unistd.h>
397c478bd9Sstevel@tonic-gate #include <sys/debug.h>
407c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
417c478bd9Sstevel@tonic-gate #include <sys/socket.h>
427c478bd9Sstevel@tonic-gate #include <sys/policy.h>
437c478bd9Sstevel@tonic-gate #include <net/if.h>
447c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
457c478bd9Sstevel@tonic-gate #include <sys/promif.h>
467c478bd9Sstevel@tonic-gate #include <sys/zone.h>
477c478bd9Sstevel@tonic-gate #include <sys/model.h>
48986fd29aSsetje #include <netinet/inetutil.h>
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate static void get_netif_name(char *, char *);
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate long
systeminfo(int command,char * buf,long count)537c478bd9Sstevel@tonic-gate systeminfo(int command, char *buf, long count)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate int error = 0;
567c478bd9Sstevel@tonic-gate long strcnt, getcnt;
577c478bd9Sstevel@tonic-gate char *kstr;
58*5679c89fSjv227347 char hostidp[HW_HOSTID_LEN];
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate if (count < 0 && command != SI_SET_HOSTNAME &&
617c478bd9Sstevel@tonic-gate command != SI_SET_SRPC_DOMAIN)
627c478bd9Sstevel@tonic-gate return (set_errno(EINVAL));
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate * Deal with the common "get a string" case first.
667c478bd9Sstevel@tonic-gate */
677c478bd9Sstevel@tonic-gate switch (command) {
687c478bd9Sstevel@tonic-gate case SI_SYSNAME:
697c478bd9Sstevel@tonic-gate kstr = utsname.sysname;
707c478bd9Sstevel@tonic-gate break;
717c478bd9Sstevel@tonic-gate case SI_HOSTNAME:
727c478bd9Sstevel@tonic-gate kstr = uts_nodename();
737c478bd9Sstevel@tonic-gate break;
747c478bd9Sstevel@tonic-gate case SI_RELEASE:
757c478bd9Sstevel@tonic-gate kstr = utsname.release;
767c478bd9Sstevel@tonic-gate break;
777c478bd9Sstevel@tonic-gate case SI_VERSION:
787c478bd9Sstevel@tonic-gate kstr = utsname.version;
797c478bd9Sstevel@tonic-gate break;
807c478bd9Sstevel@tonic-gate case SI_MACHINE:
817c478bd9Sstevel@tonic-gate kstr = utsname.machine;
827c478bd9Sstevel@tonic-gate break;
837c478bd9Sstevel@tonic-gate #ifdef _LP64
847c478bd9Sstevel@tonic-gate case SI_ARCHITECTURE_64:
857c478bd9Sstevel@tonic-gate case SI_ARCHITECTURE_K:
867c478bd9Sstevel@tonic-gate kstr = architecture;
877c478bd9Sstevel@tonic-gate break;
887c478bd9Sstevel@tonic-gate case SI_ARCHITECTURE_32:
897c478bd9Sstevel@tonic-gate case SI_ARCHITECTURE:
907c478bd9Sstevel@tonic-gate kstr = architecture_32;
917c478bd9Sstevel@tonic-gate break;
927c478bd9Sstevel@tonic-gate case SI_ARCHITECTURE_NATIVE:
937c478bd9Sstevel@tonic-gate kstr = get_udatamodel() == DATAMODEL_NATIVE ?
947c478bd9Sstevel@tonic-gate architecture : architecture_32;
957c478bd9Sstevel@tonic-gate break;
967c478bd9Sstevel@tonic-gate #else
977c478bd9Sstevel@tonic-gate case SI_ARCHITECTURE_K:
987c478bd9Sstevel@tonic-gate case SI_ARCHITECTURE_32:
997c478bd9Sstevel@tonic-gate case SI_ARCHITECTURE:
1007c478bd9Sstevel@tonic-gate case SI_ARCHITECTURE_NATIVE:
1017c478bd9Sstevel@tonic-gate kstr = architecture;
1027c478bd9Sstevel@tonic-gate break;
1037c478bd9Sstevel@tonic-gate #endif
1047c478bd9Sstevel@tonic-gate case SI_HW_SERIAL:
105*5679c89fSjv227347 (void) snprintf(hostidp, sizeof (hostidp), "%u",
106*5679c89fSjv227347 zone_get_hostid(curzone));
107*5679c89fSjv227347 kstr = hostidp;
1087c478bd9Sstevel@tonic-gate break;
1097c478bd9Sstevel@tonic-gate case SI_HW_PROVIDER:
1107c478bd9Sstevel@tonic-gate kstr = hw_provider;
1117c478bd9Sstevel@tonic-gate break;
1127c478bd9Sstevel@tonic-gate case SI_SRPC_DOMAIN:
1137c478bd9Sstevel@tonic-gate kstr = curproc->p_zone->zone_domain;
1147c478bd9Sstevel@tonic-gate break;
1157c478bd9Sstevel@tonic-gate case SI_PLATFORM:
1167c478bd9Sstevel@tonic-gate kstr = platform;
1177c478bd9Sstevel@tonic-gate break;
1187c478bd9Sstevel@tonic-gate case SI_ISALIST:
1197c478bd9Sstevel@tonic-gate kstr = isa_list;
1207c478bd9Sstevel@tonic-gate break;
1217c478bd9Sstevel@tonic-gate default:
1227c478bd9Sstevel@tonic-gate kstr = NULL;
1237c478bd9Sstevel@tonic-gate break;
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate if (kstr != NULL) {
127abddd56eScb117521 strcnt = strlen(kstr);
128abddd56eScb117521 if (count > 0) {
129abddd56eScb117521 if (count <= strcnt) {
1307c478bd9Sstevel@tonic-gate getcnt = count - 1;
131abddd56eScb117521 if (subyte(buf + getcnt, 0) < 0)
1327c478bd9Sstevel@tonic-gate return (set_errno(EFAULT));
133abddd56eScb117521 } else {
1347c478bd9Sstevel@tonic-gate getcnt = strcnt + 1;
135abddd56eScb117521 }
1367c478bd9Sstevel@tonic-gate if (copyout(kstr, buf, getcnt))
1377c478bd9Sstevel@tonic-gate return (set_errno(EFAULT));
138abddd56eScb117521 }
1397c478bd9Sstevel@tonic-gate return (strcnt + 1);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate switch (command) {
1437c478bd9Sstevel@tonic-gate case SI_DHCP_CACHE:
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate char *tmp;
146986fd29aSsetje unsigned int tlen, octlen;
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate if (dhcack == NULL) {
1497c478bd9Sstevel@tonic-gate tmp = "";
1507c478bd9Sstevel@tonic-gate strcnt = 0;
1517c478bd9Sstevel@tonic-gate } else {
1527c478bd9Sstevel@tonic-gate /*
153a9ddc71cSsetje * If the interface didn't have a name (bindable
154a9ddc71cSsetje * driver) to begin with, it might have one now.
155a9ddc71cSsetje * So, re-run strplumb_get_netdev_path() to see
156a9ddc71cSsetje * if one can be established at this time.
157a9ddc71cSsetje */
158a9ddc71cSsetje if (netdev_path == NULL || netdev_path[0] == '\0') {
159a9ddc71cSsetje netdev_path = strplumb_get_netdev_path();
160a9ddc71cSsetje }
161a9ddc71cSsetje /*
1627c478bd9Sstevel@tonic-gate * If the interface name has not yet been resolved
163986fd29aSsetje * and a validnetdev_path[] was stashed by
164986fd29aSsetje * loadrootmodules in swapgeneric.c, or established
165986fd29aSsetje * above, resolve the interface name now.
1667c478bd9Sstevel@tonic-gate */
167986fd29aSsetje if (dhcifname[0] == '\0' &&
1687c478bd9Sstevel@tonic-gate netdev_path != NULL && netdev_path[0] != '\0') {
169986fd29aSsetje get_netif_name(netdev_path, dhcifname);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate
172986fd29aSsetje /*
173986fd29aSsetje * Form reply:
174986fd29aSsetje * IFNAMESIZ array of dhcp i/f
175986fd29aSsetje * hexascii representation of dhcp reply
176986fd29aSsetje */
177986fd29aSsetje octlen = dhcacklen * 2 + 1;
178986fd29aSsetje tlen = octlen + IFNAMSIZ;
179986fd29aSsetje tmp = kmem_alloc(tlen, KM_SLEEP);
180986fd29aSsetje (void) strncpy(tmp, dhcifname, IFNAMSIZ);
181986fd29aSsetje if (octet_to_hexascii(dhcack, dhcacklen,
182986fd29aSsetje &tmp[IFNAMSIZ], &octlen) != 0) {
183986fd29aSsetje kmem_free(tmp, tlen);
184986fd29aSsetje error = EINVAL;
185986fd29aSsetje break;
186986fd29aSsetje } else {
187986fd29aSsetje strcnt = IFNAMSIZ + octlen;
188986fd29aSsetje }
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate
191abddd56eScb117521 if (count > 0) {
192abddd56eScb117521 if (count <= strcnt) {
193abddd56eScb117521 getcnt = count - 1;
194986fd29aSsetje if (subyte((buf + getcnt), 0) < 0)
195986fd29aSsetje goto fail;
196abddd56eScb117521 } else {
197abddd56eScb117521 getcnt = strcnt + 1;
198abddd56eScb117521 }
199986fd29aSsetje if (copyout(tmp, buf, getcnt))
200986fd29aSsetje goto fail;
201986fd29aSsetje }
202986fd29aSsetje if (strcnt != 0)
203986fd29aSsetje kmem_free(tmp, tlen);
204986fd29aSsetje return (strcnt + 1);
205986fd29aSsetje fail:
206986fd29aSsetje if (strcnt != 0)
207986fd29aSsetje kmem_free(tmp, tlen);
2087c478bd9Sstevel@tonic-gate error = EFAULT;
2097c478bd9Sstevel@tonic-gate break;
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate case SI_SET_HOSTNAME:
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate size_t len;
2157c478bd9Sstevel@tonic-gate char name[SYS_NMLN];
2167c478bd9Sstevel@tonic-gate char *name_to_use;
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate if ((error = secpolicy_systeminfo(CRED())) != 0)
2197c478bd9Sstevel@tonic-gate break;
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate name_to_use = uts_nodename();
2227c478bd9Sstevel@tonic-gate if ((error = copyinstr(buf, name, SYS_NMLN, &len)) != 0)
2237c478bd9Sstevel@tonic-gate break;
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate * Must be non-NULL string and string
2277c478bd9Sstevel@tonic-gate * must be less than SYS_NMLN chars.
2287c478bd9Sstevel@tonic-gate */
2297c478bd9Sstevel@tonic-gate if (len < 2 || (len == SYS_NMLN && name[SYS_NMLN-1] != '\0')) {
2307c478bd9Sstevel@tonic-gate error = EINVAL;
2317c478bd9Sstevel@tonic-gate break;
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate /*
2357c478bd9Sstevel@tonic-gate * Copy the name into the relevant zone's nodename.
2367c478bd9Sstevel@tonic-gate */
2377c478bd9Sstevel@tonic-gate (void) strcpy(name_to_use, name);
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate /*
2407c478bd9Sstevel@tonic-gate * Notify other interested parties that the nodename was set
2417c478bd9Sstevel@tonic-gate */
2427c478bd9Sstevel@tonic-gate if (name_to_use == utsname.nodename) /* global zone nodename */
2437c478bd9Sstevel@tonic-gate nodename_set();
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate return (len);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate case SI_SET_SRPC_DOMAIN:
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate char name[SYS_NMLN];
2517c478bd9Sstevel@tonic-gate size_t len;
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate if ((error = secpolicy_systeminfo(CRED())) != 0)
2547c478bd9Sstevel@tonic-gate break;
2557c478bd9Sstevel@tonic-gate if ((error = copyinstr(buf, name, SYS_NMLN, &len)) != 0)
2567c478bd9Sstevel@tonic-gate break;
2577c478bd9Sstevel@tonic-gate /*
2587c478bd9Sstevel@tonic-gate * If string passed in is longer than length
2597c478bd9Sstevel@tonic-gate * allowed for domain name, fail.
2607c478bd9Sstevel@tonic-gate */
2617c478bd9Sstevel@tonic-gate if (len == SYS_NMLN && name[SYS_NMLN-1] != '\0') {
2627c478bd9Sstevel@tonic-gate error = EINVAL;
2637c478bd9Sstevel@tonic-gate break;
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate (void) strcpy(curproc->p_zone->zone_domain, name);
2677c478bd9Sstevel@tonic-gate return (len);
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate default:
2717c478bd9Sstevel@tonic-gate error = EINVAL;
2727c478bd9Sstevel@tonic-gate break;
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate return (set_errno(error));
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate
2787c478bd9Sstevel@tonic-gate /*
2797c478bd9Sstevel@tonic-gate * i_path_find_node: Internal routine used by path_to_devinfo
2807c478bd9Sstevel@tonic-gate * to locate a given nodeid in the device tree.
2817c478bd9Sstevel@tonic-gate */
2827c478bd9Sstevel@tonic-gate struct i_path_findnode {
283fa9e4066Sahrens pnode_t nodeid;
2847c478bd9Sstevel@tonic-gate dev_info_t *dip;
2857c478bd9Sstevel@tonic-gate };
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate static int
i_path_find_node(dev_info_t * dev,void * arg)2887c478bd9Sstevel@tonic-gate i_path_find_node(dev_info_t *dev, void *arg)
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate struct i_path_findnode *f = (struct i_path_findnode *)arg;
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate if (ddi_get_nodeid(dev) == (int)f->nodeid) {
2947c478bd9Sstevel@tonic-gate f->dip = dev;
2957c478bd9Sstevel@tonic-gate return (DDI_WALK_TERMINATE);
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate
3007c478bd9Sstevel@tonic-gate /*
3017c478bd9Sstevel@tonic-gate * Return the devinfo node to a boot device
3027c478bd9Sstevel@tonic-gate */
3037c478bd9Sstevel@tonic-gate static dev_info_t *
path_to_devinfo(char * path)3047c478bd9Sstevel@tonic-gate path_to_devinfo(char *path)
3057c478bd9Sstevel@tonic-gate {
3067c478bd9Sstevel@tonic-gate struct i_path_findnode fn;
3077c478bd9Sstevel@tonic-gate extern dev_info_t *top_devinfo;
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate /*
3107c478bd9Sstevel@tonic-gate * Get the nodeid of the given pathname, if such a mapping exists.
3117c478bd9Sstevel@tonic-gate */
3127c478bd9Sstevel@tonic-gate fn.dip = NULL;
3137c478bd9Sstevel@tonic-gate fn.nodeid = prom_finddevice(path);
3147c478bd9Sstevel@tonic-gate if (fn.nodeid != OBP_BADNODE) {
3157c478bd9Sstevel@tonic-gate /*
3167c478bd9Sstevel@tonic-gate * Find the nodeid in our copy of the device tree and return
3177c478bd9Sstevel@tonic-gate * whatever name we used to bind this node to a driver.
3187c478bd9Sstevel@tonic-gate */
3197c478bd9Sstevel@tonic-gate ddi_walk_devs(top_devinfo, i_path_find_node, (void *)(&fn));
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate
3227c478bd9Sstevel@tonic-gate return (fn.dip);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate
3257c478bd9Sstevel@tonic-gate /*
3267c478bd9Sstevel@tonic-gate * Determine the network interface name from the device path argument.
3277c478bd9Sstevel@tonic-gate */
3287c478bd9Sstevel@tonic-gate static void
get_netif_name(char * devname,char * ifname)3297c478bd9Sstevel@tonic-gate get_netif_name(char *devname, char *ifname)
3307c478bd9Sstevel@tonic-gate {
3317c478bd9Sstevel@tonic-gate dev_info_t *dip;
3327c478bd9Sstevel@tonic-gate major_t ndev;
3337c478bd9Sstevel@tonic-gate char *name;
3347c478bd9Sstevel@tonic-gate int unit;
3357c478bd9Sstevel@tonic-gate
3367c478bd9Sstevel@tonic-gate dip = path_to_devinfo(devname);
3377c478bd9Sstevel@tonic-gate if (dip == NULL) {
3387c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "get_netif_name: "
3397c478bd9Sstevel@tonic-gate "can't bind driver for '%s'\n", devname);
3407c478bd9Sstevel@tonic-gate return;
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate
3437c478bd9Sstevel@tonic-gate ndev = ddi_driver_major(dip);
3447c478bd9Sstevel@tonic-gate if (ndev == -1) {
3457c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "get_netif_name: "
3467c478bd9Sstevel@tonic-gate "no driver bound to '%s'\n", devname);
3477c478bd9Sstevel@tonic-gate return;
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate
3507c478bd9Sstevel@tonic-gate name = ddi_major_to_name(ndev);
3517c478bd9Sstevel@tonic-gate if (name == NULL) {
3527c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "get_netif_name: "
3537c478bd9Sstevel@tonic-gate "no name for major number %d\n", ndev);
3547c478bd9Sstevel@tonic-gate return;
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate unit = i_ddi_devi_get_ppa(dip);
3587c478bd9Sstevel@tonic-gate if (unit < 0) {
3597c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "get_netif_name: "
3607c478bd9Sstevel@tonic-gate "illegal unit number %d\n", unit);
3617c478bd9Sstevel@tonic-gate return;
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate
3647c478bd9Sstevel@tonic-gate (void) snprintf(ifname, IFNAMSIZ, "%s%d", name, unit);
3657c478bd9Sstevel@tonic-gate }
366