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