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