1 /* $NetBSD: netio.c,v 1.8 2001/12/03 05:44:37 gmcgarry 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 90 #include <net/if.h> 91 #include <netinet/in.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 #include <hp300/stand/common/samachdep.h> 103 104 extern int nfs_root_node[]; /* XXX - get from nfs_mount() */ 105 106 struct in_addr myip, rootip, gateip; 107 n_long netmask; 108 char rootpath[FNAME_SIZE]; 109 110 int netdev_sock = -1; 111 static int open_count; 112 113 int netio_ask = 0; /* default to bootparam, can override */ 114 115 static char input_line[100]; 116 117 /* 118 * Called by devopen after it sets f->f_dev to our devsw entry. 119 * This opens the low-level device and sets f->f_devdata. 120 */ 121 int 122 netopen(f, devname) 123 struct open_file *f; 124 char *devname; /* Device part of file name (or NULL). */ 125 { 126 int error = 0; 127 128 /* On first open, do netif open, mount, etc. */ 129 if (open_count == 0) { 130 /* Find network interface. */ 131 if ((netdev_sock = netif_open(devname)) < 0) 132 return (error=ENXIO); 133 if ((error = netmountroot(f, devname)) != 0) 134 return (error); 135 } 136 open_count++; 137 f->f_devdata = nfs_root_node; 138 return (error); 139 } 140 141 int 142 netclose(f) 143 struct open_file *f; 144 { 145 /* On last close, do netif close, etc. */ 146 if (open_count > 0) 147 if (--open_count == 0) 148 netif_close(netdev_sock); 149 f->f_devdata = NULL; 150 } 151 152 int 153 netstrategy(devdata, func, dblk, size, v_buf, rsize) 154 void *devdata; 155 int func; 156 daddr_t dblk; 157 size_t size; 158 void *v_buf; 159 size_t *rsize; 160 { 161 162 *rsize = size; 163 return EIO; 164 } 165 166 int 167 netmountroot(f, devname) 168 struct open_file *f; 169 char *devname; /* Device part of file name (or NULL). */ 170 { 171 int error; 172 struct iodesc *d; 173 174 #ifdef DEBUG 175 printf("netmountroot: %s\n", devname); 176 #endif 177 178 if (netio_ask) { 179 get_my_ip: 180 printf("My IP address? "); 181 bzero(input_line, sizeof(input_line)); 182 gets(input_line); 183 if ((myip.s_addr = inet_addr(input_line)) == 184 htonl(INADDR_NONE)) { 185 printf("invalid IP address: %s\n", input_line); 186 goto get_my_ip; 187 } 188 189 get_my_netmask: 190 printf("My netmask? "); 191 bzero(input_line, sizeof(input_line)); 192 gets(input_line); 193 if ((netmask = inet_addr(input_line)) == 194 htonl(INADDR_NONE)) { 195 printf("invalid netmask: %s\n", input_line); 196 goto get_my_netmask; 197 } 198 199 get_my_gateway: 200 printf("My gateway? "); 201 bzero(input_line, sizeof(input_line)); 202 gets(input_line); 203 if ((gateip.s_addr = inet_addr(input_line)) == 204 htonl(INADDR_NONE)) { 205 printf("invalid IP address: %s\n", input_line); 206 goto get_my_gateway; 207 } 208 209 get_server_ip: 210 printf("Server IP address? "); 211 bzero(input_line, sizeof(input_line)); 212 gets(input_line); 213 if ((rootip.s_addr = inet_addr(input_line)) == 214 htonl(INADDR_NONE)) { 215 printf("invalid IP address: %s\n", input_line); 216 goto get_server_ip; 217 } 218 219 get_server_path: 220 printf("Server path? "); 221 bzero(rootpath, sizeof(rootpath)); 222 gets(rootpath); 223 if (rootpath[0] == '\0' || rootpath[0] == '\n') 224 goto get_server_path; 225 226 if ((d = socktodesc(netdev_sock)) == NULL) 227 return (EMFILE); 228 229 d->myip = myip; 230 231 goto do_nfs_mount; 232 } 233 234 /* 235 * Get info for NFS boot: our IP address, our hostname, 236 * server IP address, and our root path on the server. 237 * There are two ways to do this: The old, Sun way, 238 * and the more modern, BOOTP way. (RFC951, RFC1048) 239 */ 240 241 #ifdef SUN_BOOTPARAMS 242 /* Get boot info using RARP and Sun bootparams. */ 243 244 /* Get our IP address. (rarp.c) */ 245 if (rarp_getipaddress(netdev_sock) == -1) 246 return (errno); 247 248 printf("boot: client IP address: %s\n", inet_ntoa(myip)); 249 250 /* Get our hostname, server IP address. */ 251 if (bp_whoami(netdev_sock)) 252 return (errno); 253 254 printf("boot: client name: %s\n", hostname); 255 256 /* Get the root pathname. */ 257 if (bp_getfile(netdev_sock, "root", &rootip, rootpath)) 258 return (errno); 259 260 #else 261 262 /* Get boot info using BOOTP way. (RFC951, RFC1048) */ 263 bootp(netdev_sock); 264 265 printf("boot: client IP address: %s \n", inet_ntoa(myip)); 266 267 #endif /* SUN_BOOTPARAMS */ 268 269 printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath); 270 271 do_nfs_mount: 272 /* Get the NFS file handle (mount). */ 273 error = nfs_mount(netdev_sock, rootip, rootpath); 274 275 return (error); 276 } 277