1 /* $NetBSD: dev_net.c,v 1.9 1997/06/26 19:11:35 drochner Exp $ */ 2 3 /*- 4 * Copyright (c) 1997 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Gordon W. Ross. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * This module implements a "raw device" interface suitable for 41 * use by the stand-alone I/O library NFS code. This interface 42 * does not support any "block" access, and exists only for the 43 * purpose of initializing the network interface, getting boot 44 * parameters, and performing the NFS mount. 45 * 46 * At open time, this does: 47 * 48 * find interface - netif_open() 49 * RARP for IP address - rarp_getipaddress() 50 * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...) 51 * RPC/mountd - nfs_mount(sock, ip, path) 52 * 53 * the root file handle from mountd is saved in a global 54 * for use by the NFS open code (NFS/lookup). 55 */ 56 57 #include <machine/stdarg.h> 58 #include <sys/param.h> 59 #include <sys/socket.h> 60 #include <net/if.h> 61 #include <netinet/in.h> 62 #include "if_ether.h" 63 #include <netinet/in_systm.h> 64 65 #include "stand.h" 66 #include "net.h" 67 #include "netif.h" 68 #include "nfs.h" 69 #include "bootparam.h" 70 #include "dev_net.h" 71 72 extern int nfs_root_node[]; /* XXX - get from nfs_mount() */ 73 74 static int netdev_sock = -1; 75 static int netdev_opens; 76 77 static int net_getparams __P((int sock)); 78 79 /* 80 * Called by devopen after it sets f->f_dev to our devsw entry. 81 * This opens the low-level device and sets f->f_devdata. 82 * This is declared with variable arguments... 83 */ 84 int 85 net_open(struct open_file *f, ...) 86 { 87 va_list ap; 88 char *devname; /* Device part of file name (or NULL). */ 89 int error = 0; 90 91 va_start(ap, f); 92 devname = va_arg(ap, char*); 93 va_end(ap); 94 95 #ifdef NETIF_DEBUG 96 if (debug) 97 printf("net_open: %s\n", devname); 98 #endif 99 100 /* On first open, do netif open, mount, etc. */ 101 if (netdev_opens == 0) { 102 /* Find network interface. */ 103 if (netdev_sock < 0) { 104 netdev_sock = netif_open(devname); 105 if (netdev_sock < 0) { 106 printf("net_open: netif_open() failed\n"); 107 return (ENXIO); 108 } 109 if (debug) 110 printf("net_open: netif_open() succeeded\n"); 111 } 112 if (rootip.s_addr == 0) { 113 /* Get root IP address, and path, etc. */ 114 error = net_getparams(netdev_sock); 115 if (error) { 116 /* getparams makes its own noise */ 117 goto fail; 118 } 119 /* Get the NFS file handle (mountd). */ 120 error = nfs_mount(netdev_sock, rootip, rootpath); 121 if (error) { 122 printf("net_open: NFS mount error=%d\n", error); 123 rootip.s_addr = 0; 124 fail: 125 netif_close(netdev_sock); 126 netdev_sock = -1; 127 return (error); 128 } 129 if (debug) 130 printf("net_open: NFS mount succeeded\n"); 131 } 132 } 133 netdev_opens++; 134 f->f_devdata = nfs_root_node; 135 return (error); 136 } 137 138 int 139 net_close(f) 140 struct open_file *f; 141 { 142 143 #ifdef NETIF_DEBUG 144 if (debug) 145 printf("net_close: opens=%d\n", netdev_opens); 146 #endif 147 148 /* On last close, do netif close, etc. */ 149 f->f_devdata = NULL; 150 /* Extra close call? */ 151 if (netdev_opens <= 0) 152 return (0); 153 netdev_opens--; 154 /* Not last close? */ 155 if (netdev_opens > 0) 156 return(0); 157 rootip.s_addr = 0; 158 if (netdev_sock >= 0) { 159 if (debug) 160 printf("net_close: calling netif_close()\n"); 161 netif_close(netdev_sock); 162 netdev_sock = -1; 163 } 164 return (0); 165 } 166 167 int 168 net_ioctl() 169 { 170 return EIO; 171 } 172 173 int 174 net_strategy() 175 { 176 return EIO; 177 } 178 179 180 /* 181 * Get info for NFS boot: our IP address, our hostname, 182 * server IP address, and our root path on the server. 183 * There are two ways to do this: The old, Sun way, 184 * and the more modern, BOOTP way. (RFC951, RFC1048) 185 * 186 * The default is to use the Sun bootparams RPC 187 * (because that is what the kernel will do). 188 * MD code can make try_bootp initialied data, 189 * which will override this common definition. 190 */ 191 #ifdef SUPPORT_BOOTP 192 int try_bootp; 193 int bootp __P((int sock)); 194 #endif 195 196 static int 197 net_getparams(sock) 198 int sock; 199 { 200 201 #ifdef SUPPORT_BOOTP 202 /* 203 * Try to get boot info using BOOTP. If we succeed, then 204 * the server IP address, gateway, and root path will all 205 * be initialized. If any remain uninitialized, we will 206 * use RARP and RPC/bootparam (the Sun way) to get them. 207 */ 208 if (try_bootp) { 209 bootp(sock); 210 if ((myip.s_addr == 0) && debug) 211 printf("net_open: BOOTP failed, trying RARP/RPC...\n"); 212 } 213 #endif 214 215 /* Use RARP to get our IP address. (See rarp.c) */ 216 if (myip.s_addr == 0) { 217 if (rarp_getipaddress(sock)) { 218 printf("net_open: RARP failed\n"); 219 return (EIO); 220 } 221 } 222 printf("net_open: client addr: %s\n", inet_ntoa(myip)); 223 224 /* Get our hostname, server IP address, gateway. */ 225 if (hostname[0] == '\0') { 226 if (bp_whoami(sock)) { 227 printf("net_open: bootparam/whoami RPC failed\n"); 228 return (EIO); 229 } 230 } 231 printf("net_open: client name: %s\n", hostname); 232 if (gateip.s_addr) { 233 printf("net_open: subnet mask: %s\n", intoa(netmask)); 234 printf("net_open: net gateway: %s\n", inet_ntoa(gateip)); 235 } 236 237 /* Get the root server and pathname. */ 238 if (rootip.s_addr == 0) { 239 if (bp_getfile(sock, "root", &rootip, rootpath)) { 240 printf("net_open: bootparam/getfile RPC failed\n"); 241 return (EIO); 242 } 243 } 244 245 printf("net_open: server addr: %s\n", inet_ntoa(rootip)); 246 printf("net_open: server path: %s\n", rootpath); 247 248 return (0); 249 } 250