1 /* $NetBSD: netio.c,v 1.4 1999/06/30 18:38:03 ragge Exp $ */ 2 3 /*- 4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe. 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 * Copyright (c) 1995 Gordon W. Ross 41 * All rights reserved. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. The name of the author may not be used to endorse or promote products 52 * derived from this software without specific prior written permission. 53 * 4. All advertising materials mentioning features or use of this software 54 * must display the following acknowledgement: 55 * This product includes software developed by Gordon W. Ross 56 * 57 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 60 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 61 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 62 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 63 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 64 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 65 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 66 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 67 */ 68 69 /* 70 * This module implements a "raw device" interface suitable for 71 * use by the stand-alone I/O library NFS code. This interface 72 * does not support any "block" access, and exists only for the 73 * purpose of initializing the network interface, getting boot 74 * parameters, and performing the NFS mount. 75 * 76 * At open time, this does: 77 * 78 * find interface - netif_open() 79 * RARP for IP address - rarp_getipaddress() 80 * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...) 81 * RPC/mountd - nfs_mount(sock, ip, path) 82 * 83 * the root file handle from mountd is saved in a global 84 * for use by the NFS open code (NFS/lookup). 85 */ 86 87 #include <sys/param.h> 88 #include <sys/socket.h> 89 #include <net/if.h> 90 #include <netinet/in.h> 91 #include <netinet/if_ether.h> 92 #include <netinet/in_systm.h> 93 94 #include "lib/libsa/stand.h" 95 #include "lib/libsa/net.h" 96 #include "lib/libsa/netif.h" 97 #include "lib/libsa/bootparam.h" 98 #include "lib/libsa/nfs.h" 99 100 #include <lib/libkern/libkern.h> 101 102 extern int nfs_root_node[]; /* XXX - get from nfs_mount() */ 103 104 struct in_addr myip, rootip, gateip; 105 n_long netmask; 106 char rootpath[FNAME_SIZE]; 107 108 int netdev_sock = -1; 109 static int open_count; 110 111 int netio_ask = 0; /* default to bootparam, can override */ 112 113 static char input_line[100]; 114 115 /* 116 * Called by devopen after it sets f->f_dev to our devsw entry. 117 * This opens the low-level device and sets f->f_devdata. 118 */ 119 int 120 netopen(f, devname) 121 struct open_file *f; 122 char *devname; /* Device part of file name (or NULL). */ 123 { 124 int error = 0; 125 126 /* On first open, do netif open, mount, etc. */ 127 if (open_count == 0) { 128 /* Find network interface. */ 129 if ((netdev_sock = netif_open(devname)) < 0) 130 return (error=ENXIO); 131 if ((error = netmountroot(f, devname)) != 0) 132 return (error); 133 } 134 open_count++; 135 f->f_devdata = nfs_root_node; 136 return (error); 137 } 138 139 int 140 netclose(f) 141 struct open_file *f; 142 { 143 netif_close(netdev_sock); 144 f->f_devdata = NULL; 145 } 146 147 int 148 netstrategy(devdata, func, dblk, size, v_buf, rsize) 149 void *devdata; 150 int func; 151 daddr_t dblk; 152 size_t size; 153 void *v_buf; 154 size_t *rsize; 155 { 156 157 *rsize = size; 158 return EIO; 159 } 160 161 int 162 netmountroot(f, devname) 163 struct open_file *f; 164 char *devname; /* Device part of file name (or NULL). */ 165 { 166 int error; 167 struct iodesc *d; 168 169 if (netio_ask) { 170 get_my_ip: 171 printf("My IP address? "); 172 bzero(input_line, sizeof(input_line)); 173 gets(input_line); 174 if ((myip.s_addr = inet_addr(input_line)) == 175 htonl(INADDR_NONE)) { 176 printf("invalid IP address: %s\n", input_line); 177 goto get_my_ip; 178 } 179 180 get_my_netmask: 181 printf("My netmask? "); 182 bzero(input_line, sizeof(input_line)); 183 gets(input_line); 184 if ((netmask = inet_addr(input_line)) == 185 htonl(INADDR_NONE)) { 186 printf("invalid netmask: %s\n", input_line); 187 goto get_my_netmask; 188 } 189 190 get_my_gateway: 191 printf("My gateway? "); 192 bzero(input_line, sizeof(input_line)); 193 gets(input_line); 194 if ((gateip.s_addr = inet_addr(input_line)) == 195 htonl(INADDR_NONE)) { 196 printf("invalid IP address: %s\n", input_line); 197 goto get_my_gateway; 198 } 199 200 get_server_ip: 201 printf("Server IP address? "); 202 bzero(input_line, sizeof(input_line)); 203 gets(input_line); 204 if ((rootip.s_addr = inet_addr(input_line)) == 205 htonl(INADDR_NONE)) { 206 printf("invalid IP address: %s\n", input_line); 207 goto get_server_ip; 208 } 209 210 get_server_path: 211 printf("Server path? "); 212 bzero(rootpath, sizeof(rootpath)); 213 gets(rootpath); 214 if (rootpath[0] == '\0' || rootpath[0] == '\n') 215 goto get_server_path; 216 217 if ((d = socktodesc(netdev_sock)) == NULL) 218 return (EMFILE); 219 220 d->myip = myip; 221 222 goto do_nfs_mount; 223 } 224 225 /* 226 * Get info for NFS boot: our IP address, our hostname, 227 * server IP address, and our root path on the server. 228 * There are two ways to do this: The old, Sun way, 229 * and the more modern, BOOTP way. (RFC951, RFC1048) 230 */ 231 232 #ifdef SUPPORT_BOOTP 233 234 /* Get boot info using BOOTP way. (RFC951, RFC1048) */ 235 printf("Trying BOOTP\n"); 236 bootp(netdev_sock); 237 238 if (myip.s_addr) { 239 printf("Using IP address: %s\n", inet_ntoa(myip)); 240 241 printf("myip: %s (%s)", hostname, inet_ntoa(myip)); 242 if (gateip.s_addr) 243 printf(", gateip: %s", inet_ntoa(gateip)); 244 if (netmask) 245 printf(", mask: %s", intoa(netmask)); 246 printf("\n"); 247 } else 248 249 #endif /* SUPPORT_BOOTP */ 250 { 251 #ifdef SUPPORT_BOOTPARAMS 252 /* Get boot info using RARP and Sun bootparams. */ 253 254 printf("Trying BOOTPARAMS\n"); 255 /* Get our IP address. (rarp.c) */ 256 if (rarp_getipaddress(netdev_sock) == -1) 257 return (errno); 258 259 printf("boot: client IP address: %s\n", inet_ntoa(myip)); 260 261 /* Get our hostname, server IP address. */ 262 if (bp_whoami(netdev_sock)) 263 return (errno); 264 265 printf("boot: client name: %s\n", hostname); 266 267 /* Get the root pathname. */ 268 if (bp_getfile(netdev_sock, "root", &rootip, rootpath)) 269 return (errno); 270 #endif 271 } 272 printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath); 273 274 do_nfs_mount: 275 /* Get the NFS file handle (mount). */ 276 error = nfs_mount(netdev_sock, rootip, rootpath); 277 278 return (error); 279 } 280