1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /* 30*0Sstevel@tonic-gate * This program does the following: 31*0Sstevel@tonic-gate * 32*0Sstevel@tonic-gate * a) Returns: 33*0Sstevel@tonic-gate * 0 - if the program successfully determined the net strategy. 34*0Sstevel@tonic-gate * !0 - if an error occurred. 35*0Sstevel@tonic-gate * 36*0Sstevel@tonic-gate * b) If the program is successful, it prints three tokens to 37*0Sstevel@tonic-gate * stdout: <root fs type> <interface name> <net config strategy>. 38*0Sstevel@tonic-gate * where: 39*0Sstevel@tonic-gate * <root fs type> - "nfs" or "ufs" 40*0Sstevel@tonic-gate * <interface name> - "hme0" or "none" 41*0Sstevel@tonic-gate * <net config strategy> - "dhcp", "rarp", or "none" 42*0Sstevel@tonic-gate * 43*0Sstevel@tonic-gate * Eg: 44*0Sstevel@tonic-gate * # /sbin/netstrategy 45*0Sstevel@tonic-gate * ufs hme0 dhcp 46*0Sstevel@tonic-gate * 47*0Sstevel@tonic-gate * <root fs type> identifies the system's root file system type. 48*0Sstevel@tonic-gate * 49*0Sstevel@tonic-gate * <interface name> is the 16 char name of the root interface, and is only 50*0Sstevel@tonic-gate * set if rarp/dhcp was used to configure the interface. 51*0Sstevel@tonic-gate * 52*0Sstevel@tonic-gate * <net config strategy> can be either "rarp", "dhcp", or "none" depending 53*0Sstevel@tonic-gate * on which strategy was used to configure the interface. Is "none" if 54*0Sstevel@tonic-gate * no interface was configured using a net-based strategy. 55*0Sstevel@tonic-gate * 56*0Sstevel@tonic-gate * CAVEATS: what about autoclient systems? XXX 57*0Sstevel@tonic-gate */ 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate #include <stdio.h> 60*0Sstevel@tonic-gate #include <stdlib.h> 61*0Sstevel@tonic-gate #include <unistd.h> 62*0Sstevel@tonic-gate #include <string.h> 63*0Sstevel@tonic-gate #include <sys/types.h> 64*0Sstevel@tonic-gate #include <errno.h> 65*0Sstevel@tonic-gate #include <alloca.h> 66*0Sstevel@tonic-gate #include <sys/systeminfo.h> 67*0Sstevel@tonic-gate #include <sys/socket.h> 68*0Sstevel@tonic-gate #include <sys/sockio.h> 69*0Sstevel@tonic-gate #include <net/if.h> 70*0Sstevel@tonic-gate #include <sys/statvfs.h> 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate /* ARGSUSED */ 73*0Sstevel@tonic-gate int 74*0Sstevel@tonic-gate main(int argc, char *argv[]) 75*0Sstevel@tonic-gate { 76*0Sstevel@tonic-gate struct statvfs vfs; 77*0Sstevel@tonic-gate char *root, *interface, *strategy, dummy; 78*0Sstevel@tonic-gate long len; 79*0Sstevel@tonic-gate int fd, numifs; 80*0Sstevel@tonic-gate struct ifreq *ifr; 81*0Sstevel@tonic-gate struct ifconf ifconf; 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate /* root location */ 84*0Sstevel@tonic-gate if (statvfs("/", &vfs) < 0) 85*0Sstevel@tonic-gate root = "none"; 86*0Sstevel@tonic-gate else { 87*0Sstevel@tonic-gate if (strncmp(vfs.f_basetype, "nfs", sizeof ("nfs") - 1) == 0) 88*0Sstevel@tonic-gate vfs.f_basetype[sizeof ("nfs") - 1] = '\0'; 89*0Sstevel@tonic-gate root = vfs.f_basetype; 90*0Sstevel@tonic-gate } 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate /* 93*0Sstevel@tonic-gate * Handle the simple case where diskless dhcp tells us everything 94*0Sstevel@tonic-gate * we need to know. 95*0Sstevel@tonic-gate */ 96*0Sstevel@tonic-gate if ((len = sysinfo(SI_DHCP_CACHE, &dummy, sizeof (dummy))) > 1) { 97*0Sstevel@tonic-gate /* interface is first thing in cache. */ 98*0Sstevel@tonic-gate strategy = "dhcp"; 99*0Sstevel@tonic-gate interface = alloca(len); 100*0Sstevel@tonic-gate (void) sysinfo(SI_DHCP_CACHE, interface, len); 101*0Sstevel@tonic-gate (void) printf("%s %s %s\n", root, interface, strategy); 102*0Sstevel@tonic-gate return (0); 103*0Sstevel@tonic-gate } 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gate /* 106*0Sstevel@tonic-gate * We're not "nfs dhcp", "nfs none" is impossible, and we don't handle 107*0Sstevel@tonic-gate * "ufs rarp" (consumers are coded to deal with this reality), so 108*0Sstevel@tonic-gate * there are three possible situations: 109*0Sstevel@tonic-gate * 110*0Sstevel@tonic-gate * 1. We're "ufs dhcp" if there are any interfaces which have 111*0Sstevel@tonic-gate * obtained their addresses through DHCP. That is, if there 112*0Sstevel@tonic-gate * are any IFF_UP and non-IFF_VIRTUAL interfaces also have 113*0Sstevel@tonic-gate * IFF_DHCPRUNNING set. 114*0Sstevel@tonic-gate * 115*0Sstevel@tonic-gate * 2. We're "ufs none" if our filesystem is local and there 116*0Sstevel@tonic-gate * are no interfaces which have obtained their addresses 117*0Sstevel@tonic-gate * through DHCP. 118*0Sstevel@tonic-gate * 119*0Sstevel@tonic-gate * 3. We're "nfs rarp" if our filesystem is remote and there's 120*0Sstevel@tonic-gate * at least IFF_UP non-IFF_VIRTUAL interface (which there 121*0Sstevel@tonic-gate * *must* be, since we're running over NFS somehow), then 122*0Sstevel@tonic-gate * it must be RARP since SI_DHCP_CACHE call above failed. 123*0Sstevel@tonic-gate * It's too bad there isn't an IFF_RARPRUNNING flag. 124*0Sstevel@tonic-gate */ 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { 127*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: socket: %s\n", argv[0], 128*0Sstevel@tonic-gate strerror(errno)); 129*0Sstevel@tonic-gate return (2); 130*0Sstevel@tonic-gate } 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate if (ioctl(fd, SIOCGIFNUM, &numifs) < 0) { 133*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: SIOCGIFNUM: %s\n", argv[0], 134*0Sstevel@tonic-gate strerror(errno)); 135*0Sstevel@tonic-gate (void) close(fd); 136*0Sstevel@tonic-gate return (2); 137*0Sstevel@tonic-gate } 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gate ifconf.ifc_len = numifs * sizeof (struct ifreq); 140*0Sstevel@tonic-gate ifconf.ifc_buf = alloca(ifconf.ifc_len); 141*0Sstevel@tonic-gate 142*0Sstevel@tonic-gate if (ioctl(fd, SIOCGIFCONF, &ifconf) < 0) { 143*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: SIOCGIFCONF: %s\n", argv[0], 144*0Sstevel@tonic-gate strerror(errno)); 145*0Sstevel@tonic-gate (void) close(fd); 146*0Sstevel@tonic-gate return (2); 147*0Sstevel@tonic-gate } 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate strategy = NULL; 150*0Sstevel@tonic-gate interface = NULL; 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate for (ifr = ifconf.ifc_req; ifr < &ifconf.ifc_req[ifconf.ifc_len / 153*0Sstevel@tonic-gate sizeof (struct ifreq)]; ifr++) { 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate if (strchr(ifr->ifr_name, ':') != NULL) 156*0Sstevel@tonic-gate continue; /* skip virtual interfaces */ 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate if (ioctl(fd, SIOCGIFFLAGS, ifr) < 0) { 159*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: SIOCGIFFLAGS: %s\n", 160*0Sstevel@tonic-gate argv[0], strerror(errno)); 161*0Sstevel@tonic-gate continue; 162*0Sstevel@tonic-gate } 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate if (ifr->ifr_flags & (IFF_VIRTUAL|IFF_POINTOPOINT)) 165*0Sstevel@tonic-gate continue; 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate if (ifr->ifr_flags & IFF_UP) { 168*0Sstevel@tonic-gate /* 169*0Sstevel@tonic-gate * For the "nfs rarp" case, we assume that the first 170*0Sstevel@tonic-gate * IFF_UP interface is the one using RARP, so stash 171*0Sstevel@tonic-gate * away the first interface in case we need it. 172*0Sstevel@tonic-gate * 173*0Sstevel@tonic-gate * Since the order of the interfaces retrieved via 174*0Sstevel@tonic-gate * SIOCGLIFCONF is not deterministic, this is largely 175*0Sstevel@tonic-gate * silliness, but (a) "it's always been this way", (b) 176*0Sstevel@tonic-gate * machines booted via diskless RARP typically only 177*0Sstevel@tonic-gate * have one interface, and (c) no one consumes the 178*0Sstevel@tonic-gate * interface name in the RARP case anyway. 179*0Sstevel@tonic-gate */ 180*0Sstevel@tonic-gate if (interface == NULL) 181*0Sstevel@tonic-gate interface = ifr->ifr_name; 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate if (ifr->ifr_flags & IFF_DHCPRUNNING) { 184*0Sstevel@tonic-gate interface = ifr->ifr_name; 185*0Sstevel@tonic-gate strategy = "dhcp"; 186*0Sstevel@tonic-gate break; 187*0Sstevel@tonic-gate } 188*0Sstevel@tonic-gate } 189*0Sstevel@tonic-gate } 190*0Sstevel@tonic-gate 191*0Sstevel@tonic-gate (void) close(fd); 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate if (strcmp(root, "nfs") == 0 || strcmp(root, "cachefs") == 0) { 194*0Sstevel@tonic-gate if (interface == NULL) { 195*0Sstevel@tonic-gate (void) fprintf(stderr, 196*0Sstevel@tonic-gate "%s: cannot identify root interface.\n", argv[0]); 197*0Sstevel@tonic-gate return (2); 198*0Sstevel@tonic-gate } 199*0Sstevel@tonic-gate if (strategy == NULL) 200*0Sstevel@tonic-gate strategy = "rarp"; /* must be rarp/bootparams */ 201*0Sstevel@tonic-gate } else { 202*0Sstevel@tonic-gate if (interface == NULL || strategy == NULL) 203*0Sstevel@tonic-gate interface = strategy = "none"; 204*0Sstevel@tonic-gate } 205*0Sstevel@tonic-gate 206*0Sstevel@tonic-gate (void) printf("%s %s %s\n", root, interface, strategy); 207*0Sstevel@tonic-gate return (0); 208*0Sstevel@tonic-gate } 209