1 /* $NetBSD: dev_net.c,v 1.1 2002/02/16 16:26:23 thorpej 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 <netinet/in_systm.h> 63 64 #include <lib/libkern/libkern.h> 65 66 #include "stand.h" 67 #include "net.h" 68 #include "netif.h" 69 #include "nfs.h" 70 #include "bootparam.h" 71 #include "dev_net.h" 72 73 extern int nfs_root_node[]; /* XXX - get from nfs_mount() */ 74 75 static int netdev_sock = -1; 76 static int netdev_opens; 77 78 static int net_getparams __P((int sock)); 79 80 /* 81 * Called by devopen after it sets f->f_dev to our devsw entry. 82 * This opens the low-level device and sets f->f_devdata. 83 * This is declared with variable arguments... 84 */ 85 int 86 net_open(struct open_file *f, ...) 87 { 88 va_list ap; 89 char *devname; /* Device part of file name (or NULL). */ 90 int error = 0; 91 92 va_start(ap, f); 93 devname = va_arg(ap, char*); 94 va_end(ap); 95 96 #ifdef NETIF_DEBUG 97 if (debug) 98 printf("net_open: %s\n", devname); 99 #endif 100 101 /* On first open, do netif open, mount, etc. */ 102 if (netdev_opens == 0) { 103 /* Find network interface. */ 104 if (netdev_sock < 0) { 105 netdev_sock = netif_open(devname); 106 if (netdev_sock < 0) { 107 printf("net_open: netif_open() failed\n"); 108 return (ENXIO); 109 } 110 if (debug) 111 printf("net_open: netif_open() succeeded\n"); 112 } 113 if (rootip.s_addr == 0) { 114 /* Get root IP address, and path, etc. */ 115 error = net_getparams(netdev_sock); 116 if (error) { 117 /* getparams makes its own noise */ 118 goto fail; 119 } 120 /* Get the NFS file handle (mountd). */ 121 error = nfs_mount(netdev_sock, rootip, rootpath); 122 if (error) { 123 printf("net_open: NFS mount error=%d\n", error); 124 rootip.s_addr = 0; 125 fail: 126 netif_close(netdev_sock); 127 netdev_sock = -1; 128 return (error); 129 } 130 if (debug) 131 printf("net_open: NFS mount succeeded\n"); 132 } 133 } 134 netdev_opens++; 135 f->f_devdata = nfs_root_node; 136 return (error); 137 } 138 139 int 140 net_close(f) 141 struct open_file *f; 142 { 143 144 #ifdef NETIF_DEBUG 145 if (debug) 146 printf("net_close: opens=%d\n", netdev_opens); 147 #endif 148 149 /* On last close, do netif close, etc. */ 150 f->f_devdata = NULL; 151 /* Extra close call? */ 152 if (netdev_opens <= 0) 153 return (0); 154 netdev_opens--; 155 /* Not last close? */ 156 if (netdev_opens > 0) 157 return(0); 158 rootip.s_addr = 0; 159 if (netdev_sock >= 0) { 160 if (debug) 161 printf("net_close: calling netif_close()\n"); 162 netif_close(netdev_sock); 163 netdev_sock = -1; 164 } 165 return (0); 166 } 167 168 int 169 net_ioctl(f, cmd, data) 170 struct open_file *f; 171 u_long cmd; 172 void *data; 173 { 174 return EIO; 175 } 176 177 int 178 net_strategy(devdata, rw, blk, size, buf, rsize) 179 void *devdata; 180 int rw; 181 daddr_t blk; 182 size_t size; 183 void *buf; 184 size_t *rsize; 185 { 186 return EIO; 187 } 188 189 190 /* 191 * Get info for NFS boot: our IP address, our hostname, 192 * server IP address, and our root path on the server. 193 * There are two ways to do this: The old, Sun way, 194 * and the more modern, BOOTP way. (RFC951, RFC1048) 195 * 196 * The default is to use the Sun bootparams RPC 197 * (because that is what the kernel will do). 198 * MD code can make try_bootp initialied data, 199 * which will override this common definition. 200 */ 201 #ifdef SUPPORT_BOOTP 202 int try_bootp; 203 int bootp __P((int sock)); 204 #endif 205 206 static int 207 net_getparams(sock) 208 int sock; 209 { 210 char buf[MAXHOSTNAMELEN]; 211 n_long smask; 212 #ifdef SUPPORT_BOOTP 213 /* 214 * Try to get boot info using BOOTP. If we succeed, then 215 * the server IP address, gateway, and root path will all 216 * be initialized. If any remain uninitialized, we will 217 * use RARP and RPC/bootparam (the Sun way) to get them. 218 */ 219 if (try_bootp) 220 bootp(sock); 221 if (myip.s_addr != 0) 222 return (0); 223 if (debug) 224 printf("net_open: BOOTP failed, trying RARP/RPC...\n"); 225 #endif 226 227 /* 228 * Use RARP to get our IP address. This also sets our 229 * netmask to the "natural" default for our address. 230 */ 231 if (rarp_getipaddress(sock)) { 232 printf("net_open: RARP failed\n"); 233 return (EIO); 234 } 235 printf("net_open: client addr: %s\n", inet_ntoa(myip)); 236 237 /* Get our hostname, server IP address, gateway. */ 238 if (bp_whoami(sock)) { 239 printf("net_open: bootparam/whoami RPC failed\n"); 240 return (EIO); 241 } 242 printf("net_open: client name: %s\n", hostname); 243 244 /* 245 * Ignore the gateway from whoami (unreliable). 246 * Use the "gateway" parameter instead. 247 */ 248 smask = 0; 249 gateip.s_addr = 0; 250 if (bp_getfile(sock, "gateway", &gateip, buf)) 251 printf("nfs_open: gateway bootparam missing\n"); 252 else { 253 /* Got it! Parse the netmask. */ 254 smask = inet_addr(buf); 255 } 256 if (smask) { 257 netmask = smask; 258 printf("net_open: subnet mask: %s\n", intoa(netmask)); 259 } 260 if (gateip.s_addr) 261 printf("net_open: net gateway: %s\n", inet_ntoa(gateip)); 262 263 /* Get the root server and pathname. */ 264 if (bp_getfile(sock, "root", &rootip, rootpath)) { 265 printf("net_open: bootparam/getfile RPC failed\n"); 266 return (EIO); 267 } 268 269 printf("net_open: server addr: %s\n", inet_ntoa(rootip)); 270 printf("net_open: server path: %s\n", rootpath); 271 272 return (0); 273 } 274