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