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