1 /* $NetBSD: net.c,v 1.7 2003/02/25 08:06:29 pk Exp $ */ 2 3 /* 4 * Copyright (c) 1995 Gordon W. Ross 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 4. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by Gordon W. Ross 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * This module implements a "raw device" interface suitable for 35 * use by the stand-alone I/O library NFS code. This interface 36 * does not support any "block" access, and exists only for the 37 * purpose of initializing the network interface, getting boot 38 * parameters, and performing the NFS mount. 39 * 40 * At open time, this does: 41 * 42 * find interface - netif_open() 43 * RARP for IP address - rarp_getipaddress() 44 * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...) 45 * RPC/mountd - nfs_mount(sock, ip, path) 46 * 47 * the root file handle from mountd is saved in a global 48 * for use by the NFS open code (NFS/lookup). 49 */ 50 51 #include <sys/param.h> 52 #include <sys/socket.h> 53 #include <net/if.h> 54 #include <netinet/in.h> 55 #include <netinet/in_systm.h> 56 57 #include <lib/libsa/stand.h> 58 #include <lib/libsa/net.h> 59 #include <lib/libsa/netif.h> 60 #include <lib/libsa/bootparam.h> 61 #include <lib/libsa/bootp.h> 62 #include <lib/libsa/nfs.h> 63 64 #include <lib/libkern/libkern.h> 65 66 #include <sparc/stand/common/promdev.h> 67 68 char rootpath[FNAME_SIZE]; 69 70 int netdev_sock = -1; 71 static int open_count; 72 73 static int net_mountroot_bootparams __P((void)); 74 static int net_mountroot_bootp __P((void)); 75 76 /* 77 * Called by devopen after it sets f->f_dev to our devsw entry. 78 * This opens the low-level device and sets f->f_devdata. 79 */ 80 int 81 net_open(pd) 82 struct promdata *pd; 83 { 84 int error = 0; 85 86 /* On first open, do netif open, mount, etc. */ 87 if (open_count == 0) { 88 /* Find network interface. */ 89 if ((netdev_sock = netif_open(pd)) < 0) { 90 error = errno; 91 goto bad; 92 } 93 if ((error = net_mountroot()) != 0) 94 goto bad; 95 } 96 open_count++; 97 bad: 98 return (error); 99 } 100 101 int 102 net_close(pd) 103 struct promdata *pd; 104 { 105 /* On last close, do netif close, etc. */ 106 if (open_count <= 0) 107 return (0); 108 109 if (--open_count == 0) 110 return (netif_close(netdev_sock)); 111 112 return (0); 113 } 114 115 int 116 net_mountroot_bootparams() 117 { 118 printf("Trying BOOTPARAMS protocol... "); 119 120 /* Get our IP address. (rarp.c) */ 121 if (rarp_getipaddress(netdev_sock) == -1) 122 return (errno); 123 124 printf("ip address: %s", inet_ntoa(myip)); 125 126 /* Get our hostname, server IP address. */ 127 if (bp_whoami(netdev_sock)) 128 return (errno); 129 130 printf(", hostname: %s\n", hostname); 131 132 /* Get the root pathname. */ 133 if (bp_getfile(netdev_sock, "root", &rootip, rootpath)) 134 return (errno); 135 136 return (0); 137 } 138 139 int 140 net_mountroot_bootp() 141 { 142 printf("Trying BOOTP protocol... "); 143 144 bootp(netdev_sock); 145 146 if (myip.s_addr == 0) 147 return(ENOENT); 148 149 printf("ip address: %s", inet_ntoa(myip)); 150 151 if (hostname[0]) 152 printf(", hostname: %s", hostname); 153 if (netmask) 154 printf(", netmask: %s", intoa(netmask)); 155 if (gateip.s_addr) 156 printf(", gateway: %s", inet_ntoa(gateip)); 157 printf("\n"); 158 159 return (0); 160 } 161 162 int 163 net_mountroot() 164 { 165 int error; 166 167 #ifdef DEBUG 168 printf("net_mountroot\n"); 169 #endif 170 171 /* 172 * Get info for NFS boot: our IP address, our hostname, 173 * server IP address, and our root path on the server. 174 * There are two ways to do this: The old, Sun way, 175 * and the more modern, BOOTP way. (RFC951, RFC1048) 176 */ 177 178 /* Try BOOTP first */ 179 error = net_mountroot_bootp(); 180 /* Historically, we've used BOOTPARAMS, so try that next */ 181 if (error != 0) 182 error = net_mountroot_bootparams(); 183 if (error != 0) 184 return (error); 185 186 printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath); 187 188 /* Get the NFS file handle (mount). */ 189 if (nfs_mount(netdev_sock, rootip, rootpath) != 0) 190 return (errno); 191 192 return (0); 193 } 194