1 /* 2 * $NetBSD: dev_net.c,v 1.3 2008/04/28 20:23:25 martin Exp $ 3 */ 4 5 /*- 6 * Copyright (c) 1997 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Gordon W. Ross. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 /* __FBSDID("$FreeBSD: src/sys/boot/common/dev_net.c,v 1.15 2004/07/08 22:35:33 brian Exp $"); */ 36 37 /*- 38 * This module implements a "raw device" interface suitable for 39 * use by the stand-alone I/O library NFS code. This interface 40 * does not support any "block" access, and exists only for the 41 * purpose of initializing the network interface, getting boot 42 * parameters, and performing the NFS mount. 43 * 44 * At open time, this does: 45 * 46 * find interface - netif_open() 47 * RARP for IP address - rarp_getipaddress() 48 * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...) 49 * RPC/mountd - nfs_mount(sock, ip, path) 50 * 51 * the root file handle from mountd is saved in a global 52 * for use by the NFS open code (NFS/lookup). 53 */ 54 55 #include <machine/stdarg.h> 56 #include <sys/param.h> 57 #include <sys/socket.h> 58 #include <net/if.h> 59 #include <netinet/in.h> 60 #include <netinet/in_systm.h> 61 62 #include <stand.h> 63 #include <string.h> 64 #include <net.h> 65 #include <netif.h> 66 #include <bootp.h> 67 #include <bootparam.h> 68 69 #include "dev_net.h" 70 #include "bootstrap.h" 71 72 int debug = 0; 73 74 static int netdev_sock = -1; 75 static int netdev_opens; 76 77 static int net_init(void); 78 static int net_open(struct open_file *, ...); 79 static int net_close(struct open_file *); 80 static int net_strategy(); 81 static void net_print(int); 82 83 static int net_getparams(int sock); 84 85 struct devsw netdev = { 86 "net", 87 DEVT_NET, 88 net_init, 89 net_strategy, 90 net_open, 91 net_close, 92 noioctl, 93 net_print 94 }; 95 96 int 97 net_init(void) 98 { 99 return 0; 100 } 101 102 /* 103 * Called by devopen after it sets f->f_dev to our devsw entry. 104 * This opens the low-level device and sets f->f_devdata. 105 * This is declared with variable arguments... 106 */ 107 int 108 net_open(struct open_file *f, ...) 109 { 110 va_list args; 111 char *devname; /* Device part of file name (or NULL). */ 112 int error = 0; 113 114 va_start(args, f); 115 devname = va_arg(args, char*); 116 va_end(args); 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 netdev_opens++; 141 } 142 netdev_opens++; 143 f->f_devdata = &netdev_sock; 144 return (error); 145 } 146 147 int 148 net_close(f) 149 struct open_file *f; 150 { 151 152 #ifdef NETIF_DEBUG 153 if (debug) 154 printf("net_close: opens=%d\n", netdev_opens); 155 #endif 156 157 /* On last close, do netif close, etc. */ 158 f->f_devdata = NULL; 159 /* Extra close call? */ 160 if (netdev_opens <= 0) 161 return (0); 162 netdev_opens--; 163 /* Not last close? */ 164 if (netdev_opens > 0) 165 return(0); 166 rootip.s_addr = 0; 167 if (netdev_sock >= 0) { 168 if (debug) 169 printf("net_close: calling netif_close()\n"); 170 netif_close(netdev_sock); 171 netdev_sock = -1; 172 } 173 return (0); 174 } 175 176 int 177 net_strategy() 178 { 179 return EIO; 180 } 181 182 #define SUPPORT_BOOTP 183 184 /* 185 * Get info for NFS boot: our IP address, our hostname, 186 * server IP address, and our root path on the server. 187 * There are two ways to do this: The old, Sun way, 188 * and the more modern, BOOTP way. (RFC951, RFC1048) 189 * 190 * The default is to use the Sun bootparams RPC 191 * (because that is what the kernel will do). 192 * MD code can make try_bootp initialied data, 193 * which will override this common definition. 194 */ 195 #ifdef SUPPORT_BOOTP 196 int try_bootp = 1; 197 #endif 198 199 extern n_long ip_convertaddr(char *p); 200 201 static int 202 net_getparams(sock) 203 int sock; 204 { 205 char buf[MAXHOSTNAMELEN]; 206 char temp[FNAME_SIZE]; 207 struct iodesc *d; 208 int i; 209 n_long smask; 210 211 #ifdef SUPPORT_BOOTP 212 /* 213 * Try to get boot info using BOOTP. If we succeed, then 214 * the server IP address, gateway, and root path will all 215 * be initialized. If any remain uninitialized, we will 216 * use RARP and RPC/bootparam (the Sun way) to get them. 217 */ 218 if (try_bootp) 219 bootp(sock, BOOTP_NONE); 220 if (myip.s_addr != 0) 221 goto exit; 222 if (debug) 223 printf("net_open: BOOTP failed, trying RARP/RPC...\n"); 224 #endif 225 226 /* 227 * Use RARP to get our IP address. This also sets our 228 * netmask to the "natural" default for our address. 229 */ 230 if (rarp_getipaddress(sock)) { 231 printf("net_open: RARP failed\n"); 232 return (EIO); 233 } 234 printf("net_open: client addr: %s\n", inet_ntoa(myip)); 235 236 /* Get our hostname, server IP address, gateway. */ 237 if (bp_whoami(sock)) { 238 printf("net_open: bootparam/whoami RPC failed\n"); 239 return (EIO); 240 } 241 printf("net_open: client name: %s\n", hostname); 242 243 /* 244 * Ignore the gateway from whoami (unreliable). 245 * Use the "gateway" parameter instead. 246 */ 247 smask = 0; 248 gateip.s_addr = 0; 249 if (bp_getfile(sock, "gateway", &gateip, buf) == 0) { 250 /* Got it! Parse the netmask. */ 251 smask = ip_convertaddr(buf); 252 } 253 if (smask) { 254 netmask = smask; 255 printf("net_open: subnet mask: %s\n", intoa(netmask)); 256 } 257 if (gateip.s_addr) 258 printf("net_open: net gateway: %s\n", inet_ntoa(gateip)); 259 260 /* Get the root server and pathname. */ 261 if (bp_getfile(sock, "root", &rootip, rootpath)) { 262 printf("net_open: bootparam/getfile RPC failed\n"); 263 return (EIO); 264 } 265 exit: 266 /* 267 * If present, strip the server's address off of the rootpath 268 * before passing it along. This allows us to be compatible with 269 * the kernel's diskless (BOOTP_NFSROOT) booting conventions 270 */ 271 for (i = 0; rootpath[i] != '\0' && i < FNAME_SIZE; i++) 272 if (rootpath[i] == ':') 273 break; 274 if (i && i != FNAME_SIZE && rootpath[i] == ':') { 275 rootpath[i++] = '\0'; 276 if (inet_addr(&rootpath[0]) != INADDR_NONE) 277 rootip.s_addr = inet_addr(&rootpath[0]); 278 bcopy(&rootpath[i], &temp[0], strlen(&rootpath[i])+1); 279 bcopy(&temp[0], &rootpath[0], strlen(&rootpath[i])+1); 280 } 281 printf("net_open: server addr: %s\n", inet_ntoa(rootip)); 282 printf("net_open: server path: %s\n", rootpath); 283 284 d = socktodesc(sock); 285 sprintf(temp, "%6D", d->myea, ":"); 286 setenv("boot.netif.ip", inet_ntoa(myip), 1); 287 setenv("boot.netif.netmask", intoa(netmask), 1); 288 setenv("boot.netif.gateway", inet_ntoa(gateip), 1); 289 setenv("boot.netif.hwaddr", temp, 1); 290 setenv("boot.nfsroot.server", inet_ntoa(rootip), 1); 291 setenv("boot.nfsroot.path", rootpath, 1); 292 293 return (0); 294 } 295 296 static void 297 net_print(int verbose) 298 { 299 return; 300 } 301