1227522Sdas/* $NetBSD: netio.c,v 1.19 2023/01/15 06:19:46 tsutsui Exp $ */ 2227522Sdas 3227522Sdas/*- 4227522Sdas * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc. 5227522Sdas * All rights reserved. 6227522Sdas * 7227522Sdas * This code is derived from software contributed to The NetBSD Foundation 8227522Sdas * by Jason R. Thorpe. 9227522Sdas * 10227522Sdas * Redistribution and use in source and binary forms, with or without 11227522Sdas * modification, are permitted provided that the following conditions 12227522Sdas * are met: 13227522Sdas * 1. Redistributions of source code must retain the above copyright 14227522Sdas * notice, this list of conditions and the following disclaimer. 15227522Sdas * 2. Redistributions in binary form must reproduce the above copyright 16227522Sdas * notice, this list of conditions and the following disclaimer in the 17227522Sdas * documentation and/or other materials provided with the distribution. 18227522Sdas * 19227522Sdas * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20227522Sdas * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21227522Sdas * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22227522Sdas * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23227522Sdas * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24227522Sdas * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25227522Sdas * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26227522Sdas * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27227522Sdas * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28227522Sdas * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29227522Sdas * POSSIBILITY OF SUCH DAMAGE. 30274581Sngie */ 31227522Sdas 32227522Sdas/* 33290881Sngie * Copyright (c) 1995 Gordon W. Ross 34227522Sdas * All rights reserved. 35227522Sdas * 36227522Sdas * Redistribution and use in source and binary forms, with or without 37227522Sdas * modification, are permitted provided that the following conditions 38290881Sngie * are met: 39274581Sngie * 1. Redistributions of source code must retain the above copyright 40227522Sdas * notice, this list of conditions and the following disclaimer. 41227522Sdas * 2. Redistributions in binary form must reproduce the above copyright 42227522Sdas * notice, this list of conditions and the following disclaimer in the 43227522Sdas * documentation and/or other materials provided with the distribution. 44227522Sdas * 45227522Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 46227522Sdas * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 47227522Sdas * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 48227522Sdas * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 49227522Sdas * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 50227522Sdas * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 51274581Sngie * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 52274581Sngie * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 53274581Sngie * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 54227522Sdas * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 55227522Sdas */ 56227522Sdas 57227522Sdas/* 58227522Sdas * This module implements a "raw device" interface suitable for 59227522Sdas * use by the stand-alone I/O library NFS code. This interface 60227522Sdas * does not support any "block" access, and exists only for the 61227522Sdas * purpose of initializing the network interface, getting boot 62227522Sdas * parameters, and performing the NFS mount. 63290881Sngie * 64227522Sdas * At open time, this does: 65227522Sdas * 66227522Sdas * find interface - netif_open() 67227522Sdas * RARP for IP address - rarp_getipaddress() 68274581Sngie * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...) 69227522Sdas * RPC/mountd - nfs_mount(sock, ip, path) 70227522Sdas * 71227522Sdas * the root file handle from mountd is saved in a global 72227522Sdas * for use by the NFS open code (NFS/lookup). 73227522Sdas */ 74227522Sdas 75227522Sdas#include <sys/param.h> 76227522Sdas#include <sys/socket.h> 77227522Sdas 78227522Sdas#include <net/if.h> 79274581Sngie#include <netinet/in.h> 80274581Sngie#include <netinet/in_systm.h> 81274581Sngie 82227522Sdas#include <lib/libsa/stand.h> 83274581Sngie#include <lib/libsa/net.h> 84274581Sngie#include <lib/libsa/netif.h> 85274581Sngie#include <lib/libsa/bootp.h> 86274581Sngie#include <lib/libsa/bootparam.h> 87274581Sngie#include <lib/libsa/nfs.h> 88274581Sngie 89227522Sdas#include <lib/libkern/libkern.h> 90 91#include <hp300/stand/common/conf.h> 92#include <hp300/stand/common/samachdep.h> 93 94extern int nfs_root_node[]; /* XXX - get from nfs_mount() */ 95 96static int netdev_sock = -1; 97static int open_count; 98 99int netio_ask = 0; /* default to bootparam, can override */ 100 101static char input_line[100]; 102 103int netmountroot(struct open_file *, char *); 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 */ 109int 110netopen(struct open_file *f, ...) 111{ 112 va_list ap; 113 char *devname; 114 int error = 0; 115 116 va_start(ap, f); 117 devname = va_arg(ap, char *); 118 va_end(ap); 119 120 /* On first open, do netif open, mount, etc. */ 121 if (open_count == 0) { 122 /* Find network interface. */ 123 if ((netdev_sock = netif_open(devname)) < 0) 124 return ENXIO; 125 if ((error = netmountroot(f, devname)) != 0) 126 return error; 127 } 128 open_count++; 129 f->f_devdata = nfs_root_node; 130 return error; 131} 132 133int 134netclose(struct open_file *f) 135{ 136 /* On last close, do netif close, etc. */ 137 if (open_count > 0) 138 if (--open_count == 0) 139 netif_close(netdev_sock); 140 f->f_devdata = NULL; 141 142 return 0; 143} 144 145int 146netstrategy(void *devdata, int func, daddr_t dblk, size_t size, void *v_buf, 147 size_t *rsize) 148{ 149 150 *rsize = size; 151 return EIO; 152} 153 154int 155netmountroot(struct open_file *f, char *devname) 156{ 157 struct iodesc *d; 158 159#ifdef DEBUG 160 printf("netmountroot: %s\n", devname); 161#endif 162 163 if (netio_ask) { 164 get_my_ip: 165 printf("My IP address? "); 166 memset(input_line, 0, sizeof(input_line)); 167 kgets(input_line, sizeof(input_line)); 168 if ((myip.s_addr = inet_addr(input_line)) == 169 htonl(INADDR_NONE)) { 170 printf("invalid IP address: %s\n", input_line); 171 goto get_my_ip; 172 } 173 174 get_my_netmask: 175 printf("My netmask? "); 176 memset(input_line, 0, sizeof(input_line)); 177 kgets(input_line, sizeof(input_line)); 178 if ((netmask = inet_addr(input_line)) == 179 htonl(INADDR_NONE)) { 180 printf("invalid netmask: %s\n", input_line); 181 goto get_my_netmask; 182 } 183 184 get_my_gateway: 185 printf("My gateway? "); 186 memset(input_line, 0, sizeof(input_line)); 187 kgets(input_line, sizeof(input_line)); 188 if ((gateip.s_addr = inet_addr(input_line)) == 189 htonl(INADDR_NONE)) { 190 printf("invalid IP address: %s\n", input_line); 191 goto get_my_gateway; 192 } 193 194 get_server_ip: 195 printf("Server IP address? "); 196 memset(input_line, 0, sizeof(input_line)); 197 kgets(input_line, sizeof(input_line)); 198 if ((rootip.s_addr = inet_addr(input_line)) == 199 htonl(INADDR_NONE)) { 200 printf("invalid IP address: %s\n", input_line); 201 goto get_server_ip; 202 } 203 204 get_server_path: 205 printf("Server path? "); 206 memset(rootpath, 0, sizeof(rootpath)); 207 kgets(rootpath, sizeof(rootpath)); 208 if (rootpath[0] == '\0' || rootpath[0] == '\n') 209 goto get_server_path; 210 211 if ((d = socktodesc(netdev_sock)) == NULL) 212 return EMFILE; 213 214 d->myip = myip; 215 216 goto do_nfs_mount; 217 } 218 219 /* 220 * Get info for NFS boot: our IP address, our hostname, 221 * server IP address, and our root path on the server. 222 * There are two ways to do this: The old, Sun way, 223 * and the more modern, BOOTP way. (RFC951, RFC1048) 224 */ 225 226#ifdef SUN_BOOTPARAMS 227 /* Get boot info using RARP and Sun bootparams. */ 228 229 /* Get our IP address. (rarp.c) */ 230 if (rarp_getipaddress(netdev_sock) == -1) 231 return errno; 232 233 printf("boot: client IP address: %s\n", inet_ntoa(myip)); 234 235 /* Get our hostname, server IP address. */ 236 if (bp_whoami(netdev_sock)) 237 return errno; 238 239 printf("boot: client name: %s\n", hostname); 240 241 /* Get the root pathname. */ 242 if (bp_getfile(netdev_sock, "root", &rootip, rootpath)) 243 return errno; 244 245#else 246 247 /* Get boot info using BOOTP way. (RFC951, RFC1048) */ 248 bootp(netdev_sock); 249 250 printf("boot: client IP address: %s \n", inet_ntoa(myip)); 251 252#endif /* SUN_BOOTPARAMS */ 253 254 printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath); 255 256 do_nfs_mount: 257 /* Get the NFS file handle (mount). */ 258 if (nfs_mount(netdev_sock, rootip, rootpath)) 259 return errno; 260 261 return 0; 262} 263