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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 23*fa9e4066Sahrens * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All rights reserved. */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #include <sys/param.h> 337c478bd9Sstevel@tonic-gate #include <sys/types.h> 347c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 357c478bd9Sstevel@tonic-gate #include <sys/systm.h> 367c478bd9Sstevel@tonic-gate #include <sys/tuneable.h> 377c478bd9Sstevel@tonic-gate #include <sys/errno.h> 387c478bd9Sstevel@tonic-gate #include <sys/cred.h> 397c478bd9Sstevel@tonic-gate #include <sys/utsname.h> 407c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 417c478bd9Sstevel@tonic-gate #include <sys/unistd.h> 427c478bd9Sstevel@tonic-gate #include <sys/debug.h> 437c478bd9Sstevel@tonic-gate #include <sys/bootconf.h> 447c478bd9Sstevel@tonic-gate #include <sys/socket.h> 457c478bd9Sstevel@tonic-gate #include <sys/policy.h> 467c478bd9Sstevel@tonic-gate #include <net/if.h> 477c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 487c478bd9Sstevel@tonic-gate #include <sys/promif.h> 497c478bd9Sstevel@tonic-gate #include <sys/zone.h> 507c478bd9Sstevel@tonic-gate #include <sys/model.h> 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate static void get_netif_name(char *, char *); 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate long 557c478bd9Sstevel@tonic-gate systeminfo(int command, char *buf, long count) 567c478bd9Sstevel@tonic-gate { 577c478bd9Sstevel@tonic-gate int error = 0; 587c478bd9Sstevel@tonic-gate long strcnt, getcnt; 597c478bd9Sstevel@tonic-gate char *kstr; 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate if (count < 0 && command != SI_SET_HOSTNAME && 627c478bd9Sstevel@tonic-gate command != SI_SET_SRPC_DOMAIN) 637c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate /* 667c478bd9Sstevel@tonic-gate * Deal with the common "get a string" case first. 677c478bd9Sstevel@tonic-gate */ 687c478bd9Sstevel@tonic-gate switch (command) { 697c478bd9Sstevel@tonic-gate case SI_SYSNAME: 707c478bd9Sstevel@tonic-gate kstr = utsname.sysname; 717c478bd9Sstevel@tonic-gate break; 727c478bd9Sstevel@tonic-gate case SI_HOSTNAME: 737c478bd9Sstevel@tonic-gate kstr = uts_nodename(); 747c478bd9Sstevel@tonic-gate break; 757c478bd9Sstevel@tonic-gate case SI_RELEASE: 767c478bd9Sstevel@tonic-gate kstr = utsname.release; 777c478bd9Sstevel@tonic-gate break; 787c478bd9Sstevel@tonic-gate case SI_VERSION: 797c478bd9Sstevel@tonic-gate kstr = utsname.version; 807c478bd9Sstevel@tonic-gate break; 817c478bd9Sstevel@tonic-gate case SI_MACHINE: 827c478bd9Sstevel@tonic-gate kstr = utsname.machine; 837c478bd9Sstevel@tonic-gate break; 847c478bd9Sstevel@tonic-gate #ifdef _LP64 857c478bd9Sstevel@tonic-gate case SI_ARCHITECTURE_64: 867c478bd9Sstevel@tonic-gate case SI_ARCHITECTURE_K: 877c478bd9Sstevel@tonic-gate kstr = architecture; 887c478bd9Sstevel@tonic-gate break; 897c478bd9Sstevel@tonic-gate case SI_ARCHITECTURE_32: 907c478bd9Sstevel@tonic-gate case SI_ARCHITECTURE: 917c478bd9Sstevel@tonic-gate kstr = architecture_32; 927c478bd9Sstevel@tonic-gate break; 937c478bd9Sstevel@tonic-gate case SI_ARCHITECTURE_NATIVE: 947c478bd9Sstevel@tonic-gate kstr = get_udatamodel() == DATAMODEL_NATIVE ? 957c478bd9Sstevel@tonic-gate architecture : architecture_32; 967c478bd9Sstevel@tonic-gate break; 977c478bd9Sstevel@tonic-gate #else 987c478bd9Sstevel@tonic-gate case SI_ARCHITECTURE_K: 997c478bd9Sstevel@tonic-gate case SI_ARCHITECTURE_32: 1007c478bd9Sstevel@tonic-gate case SI_ARCHITECTURE: 1017c478bd9Sstevel@tonic-gate case SI_ARCHITECTURE_NATIVE: 1027c478bd9Sstevel@tonic-gate kstr = architecture; 1037c478bd9Sstevel@tonic-gate break; 1047c478bd9Sstevel@tonic-gate #endif 1057c478bd9Sstevel@tonic-gate case SI_HW_SERIAL: 1067c478bd9Sstevel@tonic-gate kstr = hw_serial; 1077c478bd9Sstevel@tonic-gate break; 1087c478bd9Sstevel@tonic-gate case SI_HW_PROVIDER: 1097c478bd9Sstevel@tonic-gate kstr = hw_provider; 1107c478bd9Sstevel@tonic-gate break; 1117c478bd9Sstevel@tonic-gate case SI_SRPC_DOMAIN: 1127c478bd9Sstevel@tonic-gate kstr = curproc->p_zone->zone_domain; 1137c478bd9Sstevel@tonic-gate break; 1147c478bd9Sstevel@tonic-gate case SI_PLATFORM: 1157c478bd9Sstevel@tonic-gate kstr = platform; 1167c478bd9Sstevel@tonic-gate break; 1177c478bd9Sstevel@tonic-gate case SI_ISALIST: 1187c478bd9Sstevel@tonic-gate kstr = isa_list; 1197c478bd9Sstevel@tonic-gate break; 1207c478bd9Sstevel@tonic-gate default: 1217c478bd9Sstevel@tonic-gate kstr = NULL; 1227c478bd9Sstevel@tonic-gate break; 1237c478bd9Sstevel@tonic-gate } 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate if (kstr != NULL) { 1267c478bd9Sstevel@tonic-gate if ((strcnt = strlen(kstr)) >= count) { 1277c478bd9Sstevel@tonic-gate getcnt = count - 1; 1287c478bd9Sstevel@tonic-gate if (subyte(buf + count - 1, 0) < 0) 1297c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 1307c478bd9Sstevel@tonic-gate } else 1317c478bd9Sstevel@tonic-gate getcnt = strcnt + 1; 1327c478bd9Sstevel@tonic-gate if (copyout(kstr, buf, getcnt)) 1337c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 1347c478bd9Sstevel@tonic-gate return (strcnt + 1); 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate switch (command) { 1387c478bd9Sstevel@tonic-gate case SI_DHCP_CACHE: 1397c478bd9Sstevel@tonic-gate { 1407c478bd9Sstevel@tonic-gate char *tmp; 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate if (dhcack == NULL) { 1437c478bd9Sstevel@tonic-gate tmp = ""; 1447c478bd9Sstevel@tonic-gate strcnt = 0; 1457c478bd9Sstevel@tonic-gate } else { 1467c478bd9Sstevel@tonic-gate /* 1477c478bd9Sstevel@tonic-gate * If the interface name has not yet been resolved 1487c478bd9Sstevel@tonic-gate * (first IFNAMSIZ bytes of dhcack[]) and a valid 1497c478bd9Sstevel@tonic-gate * netdev_path[] was stashed by loadrootmodules in 1507c478bd9Sstevel@tonic-gate * swapgeneric.c, resolve the interface name now. 1517c478bd9Sstevel@tonic-gate */ 1527c478bd9Sstevel@tonic-gate if (dhcack[0] == '\0' && 1537c478bd9Sstevel@tonic-gate netdev_path != NULL && netdev_path[0] != '\0') { 1547c478bd9Sstevel@tonic-gate get_netif_name(netdev_path, dhcack); 1557c478bd9Sstevel@tonic-gate } 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate tmp = dhcack; 1587c478bd9Sstevel@tonic-gate strcnt = IFNAMSIZ + strlen(&tmp[IFNAMSIZ]); 1597c478bd9Sstevel@tonic-gate } 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate getcnt = (strcnt >= count) ? count : strcnt + 1; 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate if (copyout(tmp, buf, getcnt)) { 1647c478bd9Sstevel@tonic-gate error = EFAULT; 1657c478bd9Sstevel@tonic-gate break; 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate if (strcnt >= count && subyte((buf + count - 1), 0) < 0) { 1697c478bd9Sstevel@tonic-gate error = EFAULT; 1707c478bd9Sstevel@tonic-gate break; 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate return (strcnt + 1); 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate case SI_SET_HOSTNAME: 1777c478bd9Sstevel@tonic-gate { 1787c478bd9Sstevel@tonic-gate size_t len; 1797c478bd9Sstevel@tonic-gate char name[SYS_NMLN]; 1807c478bd9Sstevel@tonic-gate char *name_to_use; 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate if ((error = secpolicy_systeminfo(CRED())) != 0) 1837c478bd9Sstevel@tonic-gate break; 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate name_to_use = uts_nodename(); 1867c478bd9Sstevel@tonic-gate if ((error = copyinstr(buf, name, SYS_NMLN, &len)) != 0) 1877c478bd9Sstevel@tonic-gate break; 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate /* 1907c478bd9Sstevel@tonic-gate * Must be non-NULL string and string 1917c478bd9Sstevel@tonic-gate * must be less than SYS_NMLN chars. 1927c478bd9Sstevel@tonic-gate */ 1937c478bd9Sstevel@tonic-gate if (len < 2 || (len == SYS_NMLN && name[SYS_NMLN-1] != '\0')) { 1947c478bd9Sstevel@tonic-gate error = EINVAL; 1957c478bd9Sstevel@tonic-gate break; 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate /* 1997c478bd9Sstevel@tonic-gate * Copy the name into the relevant zone's nodename. 2007c478bd9Sstevel@tonic-gate */ 2017c478bd9Sstevel@tonic-gate (void) strcpy(name_to_use, name); 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate /* 2047c478bd9Sstevel@tonic-gate * Notify other interested parties that the nodename was set 2057c478bd9Sstevel@tonic-gate */ 2067c478bd9Sstevel@tonic-gate if (name_to_use == utsname.nodename) /* global zone nodename */ 2077c478bd9Sstevel@tonic-gate nodename_set(); 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate return (len); 2107c478bd9Sstevel@tonic-gate } 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate case SI_SET_SRPC_DOMAIN: 2137c478bd9Sstevel@tonic-gate { 2147c478bd9Sstevel@tonic-gate char name[SYS_NMLN]; 2157c478bd9Sstevel@tonic-gate size_t len; 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate if ((error = secpolicy_systeminfo(CRED())) != 0) 2187c478bd9Sstevel@tonic-gate break; 2197c478bd9Sstevel@tonic-gate if ((error = copyinstr(buf, name, SYS_NMLN, &len)) != 0) 2207c478bd9Sstevel@tonic-gate break; 2217c478bd9Sstevel@tonic-gate /* 2227c478bd9Sstevel@tonic-gate * If string passed in is longer than length 2237c478bd9Sstevel@tonic-gate * allowed for domain name, fail. 2247c478bd9Sstevel@tonic-gate */ 2257c478bd9Sstevel@tonic-gate if (len == SYS_NMLN && name[SYS_NMLN-1] != '\0') { 2267c478bd9Sstevel@tonic-gate error = EINVAL; 2277c478bd9Sstevel@tonic-gate break; 2287c478bd9Sstevel@tonic-gate } 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate (void) strcpy(curproc->p_zone->zone_domain, name); 2317c478bd9Sstevel@tonic-gate return (len); 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate default: 2357c478bd9Sstevel@tonic-gate error = EINVAL; 2367c478bd9Sstevel@tonic-gate break; 2377c478bd9Sstevel@tonic-gate } 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate return (set_errno(error)); 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate /* 2437c478bd9Sstevel@tonic-gate * i_path_find_node: Internal routine used by path_to_devinfo 2447c478bd9Sstevel@tonic-gate * to locate a given nodeid in the device tree. 2457c478bd9Sstevel@tonic-gate */ 2467c478bd9Sstevel@tonic-gate struct i_path_findnode { 247*fa9e4066Sahrens pnode_t nodeid; 2487c478bd9Sstevel@tonic-gate dev_info_t *dip; 2497c478bd9Sstevel@tonic-gate }; 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate static int 2527c478bd9Sstevel@tonic-gate i_path_find_node(dev_info_t *dev, void *arg) 2537c478bd9Sstevel@tonic-gate { 2547c478bd9Sstevel@tonic-gate struct i_path_findnode *f = (struct i_path_findnode *)arg; 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate if (ddi_get_nodeid(dev) == (int)f->nodeid) { 2587c478bd9Sstevel@tonic-gate f->dip = dev; 2597c478bd9Sstevel@tonic-gate return (DDI_WALK_TERMINATE); 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE); 2627c478bd9Sstevel@tonic-gate } 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate /* 2657c478bd9Sstevel@tonic-gate * Return the devinfo node to a boot device 2667c478bd9Sstevel@tonic-gate */ 2677c478bd9Sstevel@tonic-gate static dev_info_t * 2687c478bd9Sstevel@tonic-gate path_to_devinfo(char *path) 2697c478bd9Sstevel@tonic-gate { 2707c478bd9Sstevel@tonic-gate struct i_path_findnode fn; 2717c478bd9Sstevel@tonic-gate extern dev_info_t *top_devinfo; 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate /* 2747c478bd9Sstevel@tonic-gate * Get the nodeid of the given pathname, if such a mapping exists. 2757c478bd9Sstevel@tonic-gate */ 2767c478bd9Sstevel@tonic-gate fn.dip = NULL; 2777c478bd9Sstevel@tonic-gate fn.nodeid = prom_finddevice(path); 2787c478bd9Sstevel@tonic-gate if (fn.nodeid != OBP_BADNODE) { 2797c478bd9Sstevel@tonic-gate /* 2807c478bd9Sstevel@tonic-gate * Find the nodeid in our copy of the device tree and return 2817c478bd9Sstevel@tonic-gate * whatever name we used to bind this node to a driver. 2827c478bd9Sstevel@tonic-gate */ 2837c478bd9Sstevel@tonic-gate ddi_walk_devs(top_devinfo, i_path_find_node, (void *)(&fn)); 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate return (fn.dip); 2877c478bd9Sstevel@tonic-gate } 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate /* 2907c478bd9Sstevel@tonic-gate * Determine the network interface name from the device path argument. 2917c478bd9Sstevel@tonic-gate */ 2927c478bd9Sstevel@tonic-gate static void 2937c478bd9Sstevel@tonic-gate get_netif_name(char *devname, char *ifname) 2947c478bd9Sstevel@tonic-gate { 2957c478bd9Sstevel@tonic-gate dev_info_t *dip; 2967c478bd9Sstevel@tonic-gate major_t ndev; 2977c478bd9Sstevel@tonic-gate char *name; 2987c478bd9Sstevel@tonic-gate int unit; 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate dip = path_to_devinfo(devname); 3017c478bd9Sstevel@tonic-gate if (dip == NULL) { 3027c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "get_netif_name: " 3037c478bd9Sstevel@tonic-gate "can't bind driver for '%s'\n", devname); 3047c478bd9Sstevel@tonic-gate return; 3057c478bd9Sstevel@tonic-gate } 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate ndev = ddi_driver_major(dip); 3087c478bd9Sstevel@tonic-gate if (ndev == -1) { 3097c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "get_netif_name: " 3107c478bd9Sstevel@tonic-gate "no driver bound to '%s'\n", devname); 3117c478bd9Sstevel@tonic-gate return; 3127c478bd9Sstevel@tonic-gate } 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate name = ddi_major_to_name(ndev); 3157c478bd9Sstevel@tonic-gate if (name == NULL) { 3167c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "get_netif_name: " 3177c478bd9Sstevel@tonic-gate "no name for major number %d\n", ndev); 3187c478bd9Sstevel@tonic-gate return; 3197c478bd9Sstevel@tonic-gate } 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate unit = i_ddi_devi_get_ppa(dip); 3227c478bd9Sstevel@tonic-gate if (unit < 0) { 3237c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "get_netif_name: " 3247c478bd9Sstevel@tonic-gate "illegal unit number %d\n", unit); 3257c478bd9Sstevel@tonic-gate return; 3267c478bd9Sstevel@tonic-gate } 3277c478bd9Sstevel@tonic-gate 3287c478bd9Sstevel@tonic-gate (void) snprintf(ifname, IFNAMSIZ, "%s%d", name, unit); 3297c478bd9Sstevel@tonic-gate } 330