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 5*abddd56eScb117521 * Common Development and Distribution License (the "License"). 6*abddd56eScb117521 * 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*abddd56eScb117521 * Copyright 2006 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) { 125*abddd56eScb117521 strcnt = strlen(kstr); 126*abddd56eScb117521 if (count > 0) { 127*abddd56eScb117521 if (count <= strcnt) { 1287c478bd9Sstevel@tonic-gate getcnt = count - 1; 129*abddd56eScb117521 if (subyte(buf + getcnt, 0) < 0) 1307c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 131*abddd56eScb117521 } else { 1327c478bd9Sstevel@tonic-gate getcnt = strcnt + 1; 133*abddd56eScb117521 } 1347c478bd9Sstevel@tonic-gate if (copyout(kstr, buf, getcnt)) 1357c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 136*abddd56eScb117521 } 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 /* 1507c478bd9Sstevel@tonic-gate * If the interface name has not yet been resolved 1517c478bd9Sstevel@tonic-gate * (first IFNAMSIZ bytes of dhcack[]) and a valid 1527c478bd9Sstevel@tonic-gate * netdev_path[] was stashed by loadrootmodules in 1537c478bd9Sstevel@tonic-gate * swapgeneric.c, resolve the interface name now. 1547c478bd9Sstevel@tonic-gate */ 1557c478bd9Sstevel@tonic-gate if (dhcack[0] == '\0' && 1567c478bd9Sstevel@tonic-gate netdev_path != NULL && netdev_path[0] != '\0') { 1577c478bd9Sstevel@tonic-gate get_netif_name(netdev_path, dhcack); 1587c478bd9Sstevel@tonic-gate } 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate tmp = dhcack; 1617c478bd9Sstevel@tonic-gate strcnt = IFNAMSIZ + strlen(&tmp[IFNAMSIZ]); 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate 164*abddd56eScb117521 if (count > 0) { 165*abddd56eScb117521 if (count <= strcnt) { 166*abddd56eScb117521 getcnt = count - 1; 167*abddd56eScb117521 if (subyte((buf + getcnt), 0) < 0) { 168*abddd56eScb117521 error = EFAULT; 169*abddd56eScb117521 break; 170*abddd56eScb117521 } 171*abddd56eScb117521 } else { 172*abddd56eScb117521 getcnt = strcnt + 1; 173*abddd56eScb117521 } 1747c478bd9Sstevel@tonic-gate if (copyout(tmp, buf, getcnt)) { 1757c478bd9Sstevel@tonic-gate error = EFAULT; 1767c478bd9Sstevel@tonic-gate break; 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate } 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate return (strcnt + 1); 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate case SI_SET_HOSTNAME: 1847c478bd9Sstevel@tonic-gate { 1857c478bd9Sstevel@tonic-gate size_t len; 1867c478bd9Sstevel@tonic-gate char name[SYS_NMLN]; 1877c478bd9Sstevel@tonic-gate char *name_to_use; 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate if ((error = secpolicy_systeminfo(CRED())) != 0) 1907c478bd9Sstevel@tonic-gate break; 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate name_to_use = uts_nodename(); 1937c478bd9Sstevel@tonic-gate if ((error = copyinstr(buf, name, SYS_NMLN, &len)) != 0) 1947c478bd9Sstevel@tonic-gate break; 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate /* 1977c478bd9Sstevel@tonic-gate * Must be non-NULL string and string 1987c478bd9Sstevel@tonic-gate * must be less than SYS_NMLN chars. 1997c478bd9Sstevel@tonic-gate */ 2007c478bd9Sstevel@tonic-gate if (len < 2 || (len == SYS_NMLN && name[SYS_NMLN-1] != '\0')) { 2017c478bd9Sstevel@tonic-gate error = EINVAL; 2027c478bd9Sstevel@tonic-gate break; 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate /* 2067c478bd9Sstevel@tonic-gate * Copy the name into the relevant zone's nodename. 2077c478bd9Sstevel@tonic-gate */ 2087c478bd9Sstevel@tonic-gate (void) strcpy(name_to_use, name); 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate /* 2117c478bd9Sstevel@tonic-gate * Notify other interested parties that the nodename was set 2127c478bd9Sstevel@tonic-gate */ 2137c478bd9Sstevel@tonic-gate if (name_to_use == utsname.nodename) /* global zone nodename */ 2147c478bd9Sstevel@tonic-gate nodename_set(); 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate return (len); 2177c478bd9Sstevel@tonic-gate } 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate case SI_SET_SRPC_DOMAIN: 2207c478bd9Sstevel@tonic-gate { 2217c478bd9Sstevel@tonic-gate char name[SYS_NMLN]; 2227c478bd9Sstevel@tonic-gate size_t len; 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate if ((error = secpolicy_systeminfo(CRED())) != 0) 2257c478bd9Sstevel@tonic-gate break; 2267c478bd9Sstevel@tonic-gate if ((error = copyinstr(buf, name, SYS_NMLN, &len)) != 0) 2277c478bd9Sstevel@tonic-gate break; 2287c478bd9Sstevel@tonic-gate /* 2297c478bd9Sstevel@tonic-gate * If string passed in is longer than length 2307c478bd9Sstevel@tonic-gate * allowed for domain name, fail. 2317c478bd9Sstevel@tonic-gate */ 2327c478bd9Sstevel@tonic-gate if (len == SYS_NMLN && name[SYS_NMLN-1] != '\0') { 2337c478bd9Sstevel@tonic-gate error = EINVAL; 2347c478bd9Sstevel@tonic-gate break; 2357c478bd9Sstevel@tonic-gate } 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate (void) strcpy(curproc->p_zone->zone_domain, name); 2387c478bd9Sstevel@tonic-gate return (len); 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate default: 2427c478bd9Sstevel@tonic-gate error = EINVAL; 2437c478bd9Sstevel@tonic-gate break; 2447c478bd9Sstevel@tonic-gate } 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate return (set_errno(error)); 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate /* 2507c478bd9Sstevel@tonic-gate * i_path_find_node: Internal routine used by path_to_devinfo 2517c478bd9Sstevel@tonic-gate * to locate a given nodeid in the device tree. 2527c478bd9Sstevel@tonic-gate */ 2537c478bd9Sstevel@tonic-gate struct i_path_findnode { 254fa9e4066Sahrens pnode_t nodeid; 2557c478bd9Sstevel@tonic-gate dev_info_t *dip; 2567c478bd9Sstevel@tonic-gate }; 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate static int 2597c478bd9Sstevel@tonic-gate i_path_find_node(dev_info_t *dev, void *arg) 2607c478bd9Sstevel@tonic-gate { 2617c478bd9Sstevel@tonic-gate struct i_path_findnode *f = (struct i_path_findnode *)arg; 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate if (ddi_get_nodeid(dev) == (int)f->nodeid) { 2657c478bd9Sstevel@tonic-gate f->dip = dev; 2667c478bd9Sstevel@tonic-gate return (DDI_WALK_TERMINATE); 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE); 2697c478bd9Sstevel@tonic-gate } 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate /* 2727c478bd9Sstevel@tonic-gate * Return the devinfo node to a boot device 2737c478bd9Sstevel@tonic-gate */ 2747c478bd9Sstevel@tonic-gate static dev_info_t * 2757c478bd9Sstevel@tonic-gate path_to_devinfo(char *path) 2767c478bd9Sstevel@tonic-gate { 2777c478bd9Sstevel@tonic-gate struct i_path_findnode fn; 2787c478bd9Sstevel@tonic-gate extern dev_info_t *top_devinfo; 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate /* 2817c478bd9Sstevel@tonic-gate * Get the nodeid of the given pathname, if such a mapping exists. 2827c478bd9Sstevel@tonic-gate */ 2837c478bd9Sstevel@tonic-gate fn.dip = NULL; 2847c478bd9Sstevel@tonic-gate fn.nodeid = prom_finddevice(path); 2857c478bd9Sstevel@tonic-gate if (fn.nodeid != OBP_BADNODE) { 2867c478bd9Sstevel@tonic-gate /* 2877c478bd9Sstevel@tonic-gate * Find the nodeid in our copy of the device tree and return 2887c478bd9Sstevel@tonic-gate * whatever name we used to bind this node to a driver. 2897c478bd9Sstevel@tonic-gate */ 2907c478bd9Sstevel@tonic-gate ddi_walk_devs(top_devinfo, i_path_find_node, (void *)(&fn)); 2917c478bd9Sstevel@tonic-gate } 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate return (fn.dip); 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate /* 2977c478bd9Sstevel@tonic-gate * Determine the network interface name from the device path argument. 2987c478bd9Sstevel@tonic-gate */ 2997c478bd9Sstevel@tonic-gate static void 3007c478bd9Sstevel@tonic-gate get_netif_name(char *devname, char *ifname) 3017c478bd9Sstevel@tonic-gate { 3027c478bd9Sstevel@tonic-gate dev_info_t *dip; 3037c478bd9Sstevel@tonic-gate major_t ndev; 3047c478bd9Sstevel@tonic-gate char *name; 3057c478bd9Sstevel@tonic-gate int unit; 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate dip = path_to_devinfo(devname); 3087c478bd9Sstevel@tonic-gate if (dip == NULL) { 3097c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "get_netif_name: " 3107c478bd9Sstevel@tonic-gate "can't bind driver for '%s'\n", devname); 3117c478bd9Sstevel@tonic-gate return; 3127c478bd9Sstevel@tonic-gate } 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate ndev = ddi_driver_major(dip); 3157c478bd9Sstevel@tonic-gate if (ndev == -1) { 3167c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "get_netif_name: " 3177c478bd9Sstevel@tonic-gate "no driver bound to '%s'\n", devname); 3187c478bd9Sstevel@tonic-gate return; 3197c478bd9Sstevel@tonic-gate } 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate name = ddi_major_to_name(ndev); 3227c478bd9Sstevel@tonic-gate if (name == NULL) { 3237c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "get_netif_name: " 3247c478bd9Sstevel@tonic-gate "no name for major number %d\n", ndev); 3257c478bd9Sstevel@tonic-gate return; 3267c478bd9Sstevel@tonic-gate } 3277c478bd9Sstevel@tonic-gate 3287c478bd9Sstevel@tonic-gate unit = i_ddi_devi_get_ppa(dip); 3297c478bd9Sstevel@tonic-gate if (unit < 0) { 3307c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "get_netif_name: " 3317c478bd9Sstevel@tonic-gate "illegal unit number %d\n", unit); 3327c478bd9Sstevel@tonic-gate return; 3337c478bd9Sstevel@tonic-gate } 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate (void) snprintf(ifname, IFNAMSIZ, "%s%d", name, unit); 3367c478bd9Sstevel@tonic-gate } 337