dev_net.c revision 64527
1/* 2 * $FreeBSD: head/sys/boot/common/dev_net.c 64527 2000-08-11 08:36:17Z ps $ 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 <string.h> 69#include <net.h> 70#include <netif.h> 71#include <bootp.h> 72#include <bootparam.h> 73 74#include "dev_net.h" 75#include "bootstrap.h" 76 77int debug = 0; 78 79static int netdev_sock = -1; 80static int netdev_opens; 81 82static int net_init(void); 83static int net_open(struct open_file *, ...); 84static int net_close(struct open_file *); 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, ...) 112{ 113 va_list args; 114 char *devname; /* Device part of file name (or NULL). */ 115 int error = 0; 116 117 va_start(args, f); 118 devname = va_arg(args, char*); 119 va_end(args); 120 121 /* On first open, do netif open, mount, etc. */ 122 if (netdev_opens == 0) { 123 /* Find network interface. */ 124 if (netdev_sock < 0) { 125 netdev_sock = netif_open(devname); 126 if (netdev_sock < 0) { 127 printf("net_open: netif_open() failed\n"); 128 return (ENXIO); 129 } 130 if (debug) 131 printf("net_open: netif_open() succeeded\n"); 132 } 133 if (rootip.s_addr == 0) { 134 /* Get root IP address, and path, etc. */ 135 error = net_getparams(netdev_sock); 136 if (error) { 137 /* getparams makes its own noise */ 138 netif_close(netdev_sock); 139 netdev_sock = -1; 140 return (error); 141 } 142 } 143 netdev_opens++; 144 } 145 netdev_opens++; 146 f->f_devdata = &netdev_sock; 147 return (error); 148} 149 150int 151net_close(f) 152 struct open_file *f; 153{ 154 155#ifdef NETIF_DEBUG 156 if (debug) 157 printf("net_close: opens=%d\n", netdev_opens); 158#endif 159 160 /* On last close, do netif close, etc. */ 161 f->f_devdata = NULL; 162 /* Extra close call? */ 163 if (netdev_opens <= 0) 164 return (0); 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_strategy() 181{ 182 return EIO; 183} 184 185#define SUPPORT_BOOTP 186 187/* 188 * Get info for NFS boot: our IP address, our hostname, 189 * server IP address, and our root path on the server. 190 * There are two ways to do this: The old, Sun way, 191 * and the more modern, BOOTP way. (RFC951, RFC1048) 192 * 193 * The default is to use the Sun bootparams RPC 194 * (because that is what the kernel will do). 195 * MD code can make try_bootp initialied data, 196 * which will override this common definition. 197 */ 198#ifdef SUPPORT_BOOTP 199int try_bootp = 1; 200#endif 201 202extern n_long ip_convertaddr(char *p); 203 204static int 205net_getparams(sock) 206 int sock; 207{ 208 char buf[MAXHOSTNAMELEN]; 209 char temp[FNAME_SIZE]; 210 int i; 211 n_long smask; 212 213#ifdef SUPPORT_BOOTP 214 /* 215 * Try to get boot info using BOOTP. If we succeed, then 216 * the server IP address, gateway, and root path will all 217 * be initialized. If any remain uninitialized, we will 218 * use RARP and RPC/bootparam (the Sun way) to get them. 219 */ 220 if (try_bootp) 221 bootp(sock, BOOTP_NONE); 222 if (myip.s_addr != 0) 223 goto exit; 224 if (debug) 225 printf("net_open: BOOTP failed, trying RARP/RPC...\n"); 226#endif 227 228 /* 229 * Use RARP to get our IP address. This also sets our 230 * netmask to the "natural" default for our address. 231 */ 232 if (rarp_getipaddress(sock)) { 233 printf("net_open: RARP failed\n"); 234 return (EIO); 235 } 236 printf("net_open: client addr: %s\n", inet_ntoa(myip)); 237 238 /* Get our hostname, server IP address, gateway. */ 239 if (bp_whoami(sock)) { 240 printf("net_open: bootparam/whoami RPC failed\n"); 241 return (EIO); 242 } 243 printf("net_open: client name: %s\n", hostname); 244 245 /* 246 * Ignore the gateway from whoami (unreliable). 247 * Use the "gateway" parameter instead. 248 */ 249 smask = 0; 250 gateip.s_addr = 0; 251 if (bp_getfile(sock, "gateway", &gateip, buf) == 0) { 252 /* Got it! Parse the netmask. */ 253 smask = ip_convertaddr(buf); 254 } 255 if (smask) { 256 netmask = smask; 257 printf("net_open: subnet mask: %s\n", intoa(netmask)); 258 } 259 if (gateip.s_addr) 260 printf("net_open: net gateway: %s\n", inet_ntoa(gateip)); 261 262 /* Get the root server and pathname. */ 263 if (bp_getfile(sock, "root", &rootip, rootpath)) { 264 printf("net_open: bootparam/getfile RPC failed\n"); 265 return (EIO); 266 } 267 exit: 268 printf("net_open: server addr: %s\n", inet_ntoa(rootip)); 269 270 /* 271 * If present, strip the server's address off of the rootpath 272 * before passing it along. This allows us to be compatible with 273 * the kernel's diskless (BOOTP_NFSROOT) booting conventions 274 */ 275 276 for(i=0; i<FNAME_SIZE; i++) 277 if(rootpath[i] == ':') 278 break; 279 if(i && i != FNAME_SIZE) { 280 i++; 281 bcopy(&rootpath[i], &temp[0], strlen(&rootpath[i])+1); 282 bcopy(&temp[0], &rootpath[0], strlen(&rootpath[i])+1); 283 } 284 printf("net_open: server path: %s\n", rootpath); 285 return (0); 286} 287