1 /* $NetBSD: dev_net.c,v 1.25 2010/08/08 18:38:31 chs 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * This module implements a "raw device" interface suitable for 34 * use by the stand-alone I/O library NFS code. This interface 35 * does not support any "block" access, and exists only for the 36 * purpose of initializing the network interface, getting boot 37 * parameters, and performing the NFS mount. 38 * 39 * At open time, this does: 40 * 41 * find interface - netif_open() 42 * RARP for IP address - rarp_getipaddress() 43 * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...) 44 * RPC/mountd - nfs_mount(sock, ip, path) 45 * 46 * the root file handle from mountd is saved in a global 47 * for use by the NFS open code (NFS/lookup). 48 */ 49 50 #include <machine/stdarg.h> 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/libkern/libkern.h> 58 59 #include "stand.h" 60 #include "net.h" 61 #include "netif.h" 62 #include "nfs.h" 63 #include "bootparam.h" 64 #include "dev_net.h" 65 #ifdef SUPPORT_BOOTP 66 #include "bootp.h" 67 #endif 68 69 extern int nfs_root_node[]; /* XXX - get from nfs_mount() */ 70 71 static int netdev_sock = -1; 72 static int netdev_opens; 73 74 static int net_getparams(int); 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 * This is declared with variable arguments... 80 */ 81 int 82 net_open(struct open_file *f, ...) 83 { 84 va_list ap; 85 char *devname; /* Device part of file name (or NULL). */ 86 int error = 0; 87 88 va_start(ap, f); 89 devname = va_arg(ap, char *); 90 va_end(ap); 91 92 #ifdef NETIF_DEBUG 93 if (debug) 94 printf("%s\n", devname); 95 #endif 96 97 /* On first open, do netif open, mount, etc. */ 98 if (netdev_opens == 0) { 99 /* Find network interface. */ 100 if (netdev_sock < 0) { 101 netdev_sock = netif_open(devname); 102 if (netdev_sock < 0) { 103 printf("netif_open() failed\n"); 104 return ENXIO; 105 } 106 #ifdef NETIF_DEBUG 107 if (debug) 108 printf("netif_open() succeeded\n"); 109 #endif 110 } 111 if (rootip.s_addr == 0) { 112 /* Get root IP address, and path, etc. */ 113 error = net_getparams(netdev_sock); 114 if (error) { 115 /* getparams makes its own noise */ 116 goto fail; 117 } 118 /* Get the NFS file handle (mountd). */ 119 error = nfs_mount(netdev_sock, rootip, rootpath); 120 if (error) { 121 printf("NFS mount error=%d\n", errno); 122 rootip.s_addr = 0; 123 fail: 124 netif_close(netdev_sock); 125 netdev_sock = -1; 126 return error; 127 } 128 #ifdef NETIF_DEBUG 129 if (debug) 130 printf("NFS mount succeeded\n"); 131 #endif 132 } 133 } 134 netdev_opens++; 135 f->f_devdata = nfs_root_node; 136 return error; 137 } 138 139 int 140 net_close(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 #ifdef NETIF_DEBUG 160 if (debug) 161 printf("net_close: calling netif_close()\n"); 162 #endif 163 netif_close(netdev_sock); 164 netdev_sock = -1; 165 } 166 return 0; 167 } 168 169 int 170 net_ioctl(struct open_file *f, u_long cmd, void *data) 171 { 172 173 return EIO; 174 } 175 176 int 177 net_strategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf, 178 size_t *rsize) 179 { 180 181 return EIO; 182 } 183 184 185 /* 186 * Get info for NFS boot: our IP address, our hostname, 187 * server IP address, and our root path on the server. 188 * There are two ways to do this: The old, Sun way, 189 * and the more modern, BOOTP way. (RFC951, RFC1048) 190 * 191 * The default is to use the Sun bootparams RPC 192 * (because that is what the kernel will do). 193 * MD code can make try_bootp initialied data, 194 * which will override this common definition. 195 */ 196 #ifdef SUPPORT_BOOTP 197 int try_bootp; 198 #endif 199 200 static int 201 net_getparams(int sock) 202 { 203 char buf[MAXHOSTNAMELEN]; 204 n_long smask; 205 206 #ifdef SUPPORT_BOOTP 207 /* 208 * Try to get boot info using BOOTP. If we succeed, then 209 * the server IP address, gateway, and root path will all 210 * be initialized. If any remain uninitialized, we will 211 * use RARP and RPC/bootparam (the Sun way) to get them. 212 */ 213 if (try_bootp) 214 bootp(sock); 215 if (myip.s_addr != 0) 216 return 0; 217 #ifdef NETIF_DEBUG 218 if (debug) 219 printf("BOOTP failed, trying RARP/RPC...\n"); 220 #endif 221 #endif 222 223 /* 224 * Use RARP to get our IP address. This also sets our 225 * netmask to the "natural" default for our address. 226 */ 227 if (rarp_getipaddress(sock)) { 228 printf("RARP failed\n"); 229 return EIO; 230 } 231 #ifdef NETIF_DEBUG 232 if (debug) 233 printf("client addr: %s\n", inet_ntoa(myip)); 234 #endif 235 236 /* Get our hostname, server IP address, gateway. */ 237 if (bp_whoami(sock)) { 238 printf("bootparam/whoami RPC failed\n"); 239 return EIO; 240 } 241 #ifdef NETIF_DEBUG 242 if (debug) 243 printf("client name: %s\n", hostname); 244 #endif 245 246 /* 247 * Ignore the gateway from whoami (unreliable). 248 * Use the "gateway" parameter instead. 249 */ 250 smask = 0; 251 gateip.s_addr = 0; 252 if (bp_getfile(sock, "gateway", &gateip, buf)) { 253 printf("nfs_open: gateway bootparam missing\n"); 254 } else { 255 /* Got it! Parse the netmask. */ 256 smask = inet_addr(buf); 257 } 258 if (smask) { 259 netmask = smask; 260 #ifdef NETIF_DEBUG 261 if (debug) 262 printf("subnet mask: %s\n", intoa(netmask)); 263 #endif 264 } 265 #ifdef NETIF_DEBUG 266 if (debug) 267 if (gateip.s_addr) 268 printf("net gateway: %s\n", inet_ntoa(gateip)); 269 #endif 270 271 /* Get the root server and pathname. */ 272 if (bp_getfile(sock, "root", &rootip, rootpath)) { 273 printf("bootparam/getfile RPC failed\n"); 274 return EIO; 275 } 276 277 #ifdef NETIF_DEBUG 278 if (debug) { 279 printf("server addr: %s\n", inet_ntoa(rootip)); 280 printf("server path: %s\n", rootpath); 281 } 282 #endif 283 284 return 0; 285 } 286