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