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*a9ddc71cSsetje * Copyright 2007 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 #pragma ident "%Z%%M% %I% %E% SMI" 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #include <sys/param.h> 327c478bd9Sstevel@tonic-gate #include <sys/types.h> 337c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 347c478bd9Sstevel@tonic-gate #include <sys/systm.h> 357c478bd9Sstevel@tonic-gate #include <sys/tuneable.h> 367c478bd9Sstevel@tonic-gate #include <sys/errno.h> 377c478bd9Sstevel@tonic-gate #include <sys/cred.h> 387c478bd9Sstevel@tonic-gate #include <sys/utsname.h> 397c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 407c478bd9Sstevel@tonic-gate #include <sys/unistd.h> 417c478bd9Sstevel@tonic-gate #include <sys/debug.h> 427c478bd9Sstevel@tonic-gate #include <sys/bootconf.h> 437c478bd9Sstevel@tonic-gate #include <sys/socket.h> 447c478bd9Sstevel@tonic-gate #include <sys/policy.h> 457c478bd9Sstevel@tonic-gate #include <net/if.h> 467c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 477c478bd9Sstevel@tonic-gate #include <sys/promif.h> 487c478bd9Sstevel@tonic-gate #include <sys/zone.h> 497c478bd9Sstevel@tonic-gate #include <sys/model.h> 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate static void get_netif_name(char *, char *); 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate long 547c478bd9Sstevel@tonic-gate systeminfo(int command, char *buf, long count) 557c478bd9Sstevel@tonic-gate { 567c478bd9Sstevel@tonic-gate int error = 0; 577c478bd9Sstevel@tonic-gate long strcnt, getcnt; 587c478bd9Sstevel@tonic-gate char *kstr; 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: 1057c478bd9Sstevel@tonic-gate kstr = hw_serial; 1067c478bd9Sstevel@tonic-gate break; 1077c478bd9Sstevel@tonic-gate case SI_HW_PROVIDER: 1087c478bd9Sstevel@tonic-gate kstr = hw_provider; 1097c478bd9Sstevel@tonic-gate break; 1107c478bd9Sstevel@tonic-gate case SI_SRPC_DOMAIN: 1117c478bd9Sstevel@tonic-gate kstr = curproc->p_zone->zone_domain; 1127c478bd9Sstevel@tonic-gate break; 1137c478bd9Sstevel@tonic-gate case SI_PLATFORM: 1147c478bd9Sstevel@tonic-gate kstr = platform; 1157c478bd9Sstevel@tonic-gate break; 1167c478bd9Sstevel@tonic-gate case SI_ISALIST: 1177c478bd9Sstevel@tonic-gate kstr = isa_list; 1187c478bd9Sstevel@tonic-gate break; 1197c478bd9Sstevel@tonic-gate default: 1207c478bd9Sstevel@tonic-gate kstr = NULL; 1217c478bd9Sstevel@tonic-gate break; 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate if (kstr != NULL) { 125abddd56eScb117521 strcnt = strlen(kstr); 126abddd56eScb117521 if (count > 0) { 127abddd56eScb117521 if (count <= strcnt) { 1287c478bd9Sstevel@tonic-gate getcnt = count - 1; 129abddd56eScb117521 if (subyte(buf + getcnt, 0) < 0) 1307c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 131abddd56eScb117521 } else { 1327c478bd9Sstevel@tonic-gate getcnt = strcnt + 1; 133abddd56eScb117521 } 1347c478bd9Sstevel@tonic-gate if (copyout(kstr, buf, getcnt)) 1357c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 136abddd56eScb117521 } 1377c478bd9Sstevel@tonic-gate return (strcnt + 1); 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate switch (command) { 1417c478bd9Sstevel@tonic-gate case SI_DHCP_CACHE: 1427c478bd9Sstevel@tonic-gate { 1437c478bd9Sstevel@tonic-gate char *tmp; 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate if (dhcack == NULL) { 1467c478bd9Sstevel@tonic-gate tmp = ""; 1477c478bd9Sstevel@tonic-gate strcnt = 0; 1487c478bd9Sstevel@tonic-gate } else { 1497c478bd9Sstevel@tonic-gate /* 150*a9ddc71cSsetje * If the interface didn't have a name (bindable 151*a9ddc71cSsetje * driver) to begin with, it might have one now. 152*a9ddc71cSsetje * So, re-run strplumb_get_netdev_path() to see 153*a9ddc71cSsetje * if one can be established at this time. 154*a9ddc71cSsetje */ 155*a9ddc71cSsetje if (netdev_path == NULL || netdev_path[0] == '\0') { 156*a9ddc71cSsetje netdev_path = strplumb_get_netdev_path(); 157*a9ddc71cSsetje } 158*a9ddc71cSsetje /* 1597c478bd9Sstevel@tonic-gate * If the interface name has not yet been resolved 1607c478bd9Sstevel@tonic-gate * (first IFNAMSIZ bytes of dhcack[]) and a valid 1617c478bd9Sstevel@tonic-gate * netdev_path[] was stashed by loadrootmodules in 162*a9ddc71cSsetje * swapgeneric.c, or established above, resolve 163*a9ddc71cSsetje * the interface name now. 1647c478bd9Sstevel@tonic-gate */ 1657c478bd9Sstevel@tonic-gate if (dhcack[0] == '\0' && 1667c478bd9Sstevel@tonic-gate netdev_path != NULL && netdev_path[0] != '\0') { 1677c478bd9Sstevel@tonic-gate get_netif_name(netdev_path, dhcack); 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate tmp = dhcack; 1717c478bd9Sstevel@tonic-gate strcnt = IFNAMSIZ + strlen(&tmp[IFNAMSIZ]); 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate 174abddd56eScb117521 if (count > 0) { 175abddd56eScb117521 if (count <= strcnt) { 176abddd56eScb117521 getcnt = count - 1; 177abddd56eScb117521 if (subyte((buf + getcnt), 0) < 0) { 178abddd56eScb117521 error = EFAULT; 179abddd56eScb117521 break; 180abddd56eScb117521 } 181abddd56eScb117521 } else { 182abddd56eScb117521 getcnt = strcnt + 1; 183abddd56eScb117521 } 1847c478bd9Sstevel@tonic-gate if (copyout(tmp, buf, getcnt)) { 1857c478bd9Sstevel@tonic-gate error = EFAULT; 1867c478bd9Sstevel@tonic-gate break; 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate return (strcnt + 1); 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate case SI_SET_HOSTNAME: 1947c478bd9Sstevel@tonic-gate { 1957c478bd9Sstevel@tonic-gate size_t len; 1967c478bd9Sstevel@tonic-gate char name[SYS_NMLN]; 1977c478bd9Sstevel@tonic-gate char *name_to_use; 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate if ((error = secpolicy_systeminfo(CRED())) != 0) 2007c478bd9Sstevel@tonic-gate break; 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate name_to_use = uts_nodename(); 2037c478bd9Sstevel@tonic-gate if ((error = copyinstr(buf, name, SYS_NMLN, &len)) != 0) 2047c478bd9Sstevel@tonic-gate break; 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate /* 2077c478bd9Sstevel@tonic-gate * Must be non-NULL string and string 2087c478bd9Sstevel@tonic-gate * must be less than SYS_NMLN chars. 2097c478bd9Sstevel@tonic-gate */ 2107c478bd9Sstevel@tonic-gate if (len < 2 || (len == SYS_NMLN && name[SYS_NMLN-1] != '\0')) { 2117c478bd9Sstevel@tonic-gate error = EINVAL; 2127c478bd9Sstevel@tonic-gate break; 2137c478bd9Sstevel@tonic-gate } 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate /* 2167c478bd9Sstevel@tonic-gate * Copy the name into the relevant zone's nodename. 2177c478bd9Sstevel@tonic-gate */ 2187c478bd9Sstevel@tonic-gate (void) strcpy(name_to_use, name); 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate /* 2217c478bd9Sstevel@tonic-gate * Notify other interested parties that the nodename was set 2227c478bd9Sstevel@tonic-gate */ 2237c478bd9Sstevel@tonic-gate if (name_to_use == utsname.nodename) /* global zone nodename */ 2247c478bd9Sstevel@tonic-gate nodename_set(); 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate return (len); 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate case SI_SET_SRPC_DOMAIN: 2307c478bd9Sstevel@tonic-gate { 2317c478bd9Sstevel@tonic-gate char name[SYS_NMLN]; 2327c478bd9Sstevel@tonic-gate size_t len; 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate if ((error = secpolicy_systeminfo(CRED())) != 0) 2357c478bd9Sstevel@tonic-gate break; 2367c478bd9Sstevel@tonic-gate if ((error = copyinstr(buf, name, SYS_NMLN, &len)) != 0) 2377c478bd9Sstevel@tonic-gate break; 2387c478bd9Sstevel@tonic-gate /* 2397c478bd9Sstevel@tonic-gate * If string passed in is longer than length 2407c478bd9Sstevel@tonic-gate * allowed for domain name, fail. 2417c478bd9Sstevel@tonic-gate */ 2427c478bd9Sstevel@tonic-gate if (len == SYS_NMLN && name[SYS_NMLN-1] != '\0') { 2437c478bd9Sstevel@tonic-gate error = EINVAL; 2447c478bd9Sstevel@tonic-gate break; 2457c478bd9Sstevel@tonic-gate } 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate (void) strcpy(curproc->p_zone->zone_domain, name); 2487c478bd9Sstevel@tonic-gate return (len); 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate default: 2527c478bd9Sstevel@tonic-gate error = EINVAL; 2537c478bd9Sstevel@tonic-gate break; 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate return (set_errno(error)); 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate /* 2607c478bd9Sstevel@tonic-gate * i_path_find_node: Internal routine used by path_to_devinfo 2617c478bd9Sstevel@tonic-gate * to locate a given nodeid in the device tree. 2627c478bd9Sstevel@tonic-gate */ 2637c478bd9Sstevel@tonic-gate struct i_path_findnode { 264fa9e4066Sahrens pnode_t nodeid; 2657c478bd9Sstevel@tonic-gate dev_info_t *dip; 2667c478bd9Sstevel@tonic-gate }; 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate static int 2697c478bd9Sstevel@tonic-gate i_path_find_node(dev_info_t *dev, void *arg) 2707c478bd9Sstevel@tonic-gate { 2717c478bd9Sstevel@tonic-gate struct i_path_findnode *f = (struct i_path_findnode *)arg; 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate if (ddi_get_nodeid(dev) == (int)f->nodeid) { 2757c478bd9Sstevel@tonic-gate f->dip = dev; 2767c478bd9Sstevel@tonic-gate return (DDI_WALK_TERMINATE); 2777c478bd9Sstevel@tonic-gate } 2787c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE); 2797c478bd9Sstevel@tonic-gate } 2807c478bd9Sstevel@tonic-gate 2817c478bd9Sstevel@tonic-gate /* 2827c478bd9Sstevel@tonic-gate * Return the devinfo node to a boot device 2837c478bd9Sstevel@tonic-gate */ 2847c478bd9Sstevel@tonic-gate static dev_info_t * 2857c478bd9Sstevel@tonic-gate path_to_devinfo(char *path) 2867c478bd9Sstevel@tonic-gate { 2877c478bd9Sstevel@tonic-gate struct i_path_findnode fn; 2887c478bd9Sstevel@tonic-gate extern dev_info_t *top_devinfo; 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate /* 2917c478bd9Sstevel@tonic-gate * Get the nodeid of the given pathname, if such a mapping exists. 2927c478bd9Sstevel@tonic-gate */ 2937c478bd9Sstevel@tonic-gate fn.dip = NULL; 2947c478bd9Sstevel@tonic-gate fn.nodeid = prom_finddevice(path); 2957c478bd9Sstevel@tonic-gate if (fn.nodeid != OBP_BADNODE) { 2967c478bd9Sstevel@tonic-gate /* 2977c478bd9Sstevel@tonic-gate * Find the nodeid in our copy of the device tree and return 2987c478bd9Sstevel@tonic-gate * whatever name we used to bind this node to a driver. 2997c478bd9Sstevel@tonic-gate */ 3007c478bd9Sstevel@tonic-gate ddi_walk_devs(top_devinfo, i_path_find_node, (void *)(&fn)); 3017c478bd9Sstevel@tonic-gate } 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate return (fn.dip); 3047c478bd9Sstevel@tonic-gate } 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate /* 3077c478bd9Sstevel@tonic-gate * Determine the network interface name from the device path argument. 3087c478bd9Sstevel@tonic-gate */ 3097c478bd9Sstevel@tonic-gate static void 3107c478bd9Sstevel@tonic-gate get_netif_name(char *devname, char *ifname) 3117c478bd9Sstevel@tonic-gate { 3127c478bd9Sstevel@tonic-gate dev_info_t *dip; 3137c478bd9Sstevel@tonic-gate major_t ndev; 3147c478bd9Sstevel@tonic-gate char *name; 3157c478bd9Sstevel@tonic-gate int unit; 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate dip = path_to_devinfo(devname); 3187c478bd9Sstevel@tonic-gate if (dip == NULL) { 3197c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "get_netif_name: " 3207c478bd9Sstevel@tonic-gate "can't bind driver for '%s'\n", devname); 3217c478bd9Sstevel@tonic-gate return; 3227c478bd9Sstevel@tonic-gate } 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate ndev = ddi_driver_major(dip); 3257c478bd9Sstevel@tonic-gate if (ndev == -1) { 3267c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "get_netif_name: " 3277c478bd9Sstevel@tonic-gate "no driver bound to '%s'\n", devname); 3287c478bd9Sstevel@tonic-gate return; 3297c478bd9Sstevel@tonic-gate } 3307c478bd9Sstevel@tonic-gate 3317c478bd9Sstevel@tonic-gate name = ddi_major_to_name(ndev); 3327c478bd9Sstevel@tonic-gate if (name == NULL) { 3337c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "get_netif_name: " 3347c478bd9Sstevel@tonic-gate "no name for major number %d\n", ndev); 3357c478bd9Sstevel@tonic-gate return; 3367c478bd9Sstevel@tonic-gate } 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate unit = i_ddi_devi_get_ppa(dip); 3397c478bd9Sstevel@tonic-gate if (unit < 0) { 3407c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "get_netif_name: " 3417c478bd9Sstevel@tonic-gate "illegal unit number %d\n", unit); 3427c478bd9Sstevel@tonic-gate return; 3437c478bd9Sstevel@tonic-gate } 3447c478bd9Sstevel@tonic-gate 3457c478bd9Sstevel@tonic-gate (void) snprintf(ifname, IFNAMSIZ, "%s%d", name, unit); 3467c478bd9Sstevel@tonic-gate } 347