Home | History | Annotate | Line # | Download | only in boot
net.c revision 1.1
      1  1.1  tsubai /*	$NetBSD: net.c,v 1.1 1999/12/22 05:54:41 tsubai Exp $	*/
      2  1.1  tsubai 
      3  1.1  tsubai /*
      4  1.1  tsubai  * Copyright (c) 1995 Gordon W. Ross
      5  1.1  tsubai  * All rights reserved.
      6  1.1  tsubai  *
      7  1.1  tsubai  * Redistribution and use in source and binary forms, with or without
      8  1.1  tsubai  * modification, are permitted provided that the following conditions
      9  1.1  tsubai  * are met:
     10  1.1  tsubai  * 1. Redistributions of source code must retain the above copyright
     11  1.1  tsubai  *    notice, this list of conditions and the following disclaimer.
     12  1.1  tsubai  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  tsubai  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  tsubai  *    documentation and/or other materials provided with the distribution.
     15  1.1  tsubai  * 3. The name of the author may not be used to endorse or promote products
     16  1.1  tsubai  *    derived from this software without specific prior written permission.
     17  1.1  tsubai  * 4. All advertising materials mentioning features or use of this software
     18  1.1  tsubai  *    must display the following acknowledgement:
     19  1.1  tsubai  *      This product includes software developed by Gordon W. Ross
     20  1.1  tsubai  *
     21  1.1  tsubai  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.1  tsubai  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.1  tsubai  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.1  tsubai  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1  tsubai  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.1  tsubai  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.1  tsubai  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.1  tsubai  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.1  tsubai  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.1  tsubai  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1  tsubai  */
     32  1.1  tsubai 
     33  1.1  tsubai /*
     34  1.1  tsubai  * This module implements a "raw device" interface suitable for
     35  1.1  tsubai  * use by the stand-alone I/O library NFS code.  This interface
     36  1.1  tsubai  * does not support any "block" access, and exists only for the
     37  1.1  tsubai  * purpose of initializing the network interface, getting boot
     38  1.1  tsubai  * parameters, and performing the NFS mount.
     39  1.1  tsubai  *
     40  1.1  tsubai  * At open time, this does:
     41  1.1  tsubai  *
     42  1.1  tsubai  * find interface      - netif_open()
     43  1.1  tsubai  * RARP for IP address - rarp_getipaddress()
     44  1.1  tsubai  * RPC/bootparams      - callrpc(d, RPC_BOOTPARAMS, ...)
     45  1.1  tsubai  * RPC/mountd          - nfs_mount(sock, ip, path)
     46  1.1  tsubai  *
     47  1.1  tsubai  * the root file handle from mountd is saved in a global
     48  1.1  tsubai  * for use by the NFS open code (NFS/lookup).
     49  1.1  tsubai  */
     50  1.1  tsubai 
     51  1.1  tsubai #include <sys/param.h>
     52  1.1  tsubai #include <sys/socket.h>
     53  1.1  tsubai #include <net/if.h>
     54  1.1  tsubai #include <netinet/in.h>
     55  1.1  tsubai #include <netinet/in_systm.h>
     56  1.1  tsubai 
     57  1.1  tsubai #include <lib/libsa/stand.h>
     58  1.1  tsubai #include <lib/libsa/net.h>
     59  1.1  tsubai #include <lib/libsa/netif.h>
     60  1.1  tsubai #include <lib/libsa/bootparam.h>
     61  1.1  tsubai #include <lib/libsa/nfs.h>
     62  1.1  tsubai 
     63  1.1  tsubai #include <lib/libkern/libkern.h>
     64  1.1  tsubai 
     65  1.1  tsubai #include <promdev.h>
     66  1.1  tsubai 
     67  1.1  tsubai char rootpath[FNAME_SIZE];
     68  1.1  tsubai 
     69  1.1  tsubai int	netdev_sock = -1;
     70  1.1  tsubai static	int open_count;
     71  1.1  tsubai 
     72  1.1  tsubai /*
     73  1.1  tsubai  * Called by devopen after it sets f->f_dev to our devsw entry.
     74  1.1  tsubai  * This opens the low-level device and sets f->f_devdata.
     75  1.1  tsubai  */
     76  1.1  tsubai int
     77  1.1  tsubai net_open(pd)
     78  1.1  tsubai 	struct romdev *pd;
     79  1.1  tsubai {
     80  1.1  tsubai 	int error = 0;
     81  1.1  tsubai 
     82  1.1  tsubai 	/* On first open, do netif open, mount, etc. */
     83  1.1  tsubai 	if (open_count == 0) {
     84  1.1  tsubai 		/* Find network interface. */
     85  1.1  tsubai 		if ((netdev_sock = netif_open(pd)) < 0) {
     86  1.1  tsubai 			error = errno;
     87  1.1  tsubai 			goto bad;
     88  1.1  tsubai 		}
     89  1.1  tsubai 		if ((error = net_mountroot()) != 0)
     90  1.1  tsubai 			goto bad;
     91  1.1  tsubai 	}
     92  1.1  tsubai 	open_count++;
     93  1.1  tsubai bad:
     94  1.1  tsubai 	return (error);
     95  1.1  tsubai }
     96  1.1  tsubai 
     97  1.1  tsubai int
     98  1.1  tsubai net_close(pd)
     99  1.1  tsubai 	struct romdev *pd;
    100  1.1  tsubai {
    101  1.1  tsubai 	/* On last close, do netif close, etc. */
    102  1.1  tsubai 	if (open_count <= 0)
    103  1.1  tsubai 		return (0);
    104  1.1  tsubai 
    105  1.1  tsubai 	if (--open_count == 0)
    106  1.1  tsubai 		return (netif_close(netdev_sock));
    107  1.1  tsubai 
    108  1.1  tsubai 	return (0);
    109  1.1  tsubai }
    110  1.1  tsubai 
    111  1.1  tsubai int
    112  1.1  tsubai net_mountroot()
    113  1.1  tsubai {
    114  1.1  tsubai 
    115  1.1  tsubai #ifdef DEBUG
    116  1.1  tsubai 	printf("net_mountroot\n");
    117  1.1  tsubai #endif
    118  1.1  tsubai 
    119  1.1  tsubai 	/*
    120  1.1  tsubai 	 * Get info for NFS boot: our IP address, our hostname,
    121  1.1  tsubai 	 * server IP address, and our root path on the server.
    122  1.1  tsubai 	 * There are two ways to do this:  The old, Sun way,
    123  1.1  tsubai 	 * and the more modern, BOOTP way. (RFC951, RFC1048)
    124  1.1  tsubai 	 */
    125  1.1  tsubai 
    126  1.1  tsubai #ifdef	SUN_BOOTPARAMS
    127  1.1  tsubai 	/* Get boot info using RARP and Sun bootparams. */
    128  1.1  tsubai 
    129  1.1  tsubai 	/* Get our IP address.  (rarp.c) */
    130  1.1  tsubai 	if (rarp_getipaddress(netdev_sock) == -1)
    131  1.1  tsubai 		return (errno);
    132  1.1  tsubai 
    133  1.1  tsubai 	printf("boot: client IP address: %s\n", inet_ntoa(myip));
    134  1.1  tsubai 
    135  1.1  tsubai 	/* Get our hostname, server IP address. */
    136  1.1  tsubai 	if (bp_whoami(netdev_sock))
    137  1.1  tsubai 		return (errno);
    138  1.1  tsubai 
    139  1.1  tsubai 	printf("boot: client name: %s\n", hostname);
    140  1.1  tsubai 
    141  1.1  tsubai 	/* Get the root pathname. */
    142  1.1  tsubai 	if (bp_getfile(netdev_sock, "root", &rootip, rootpath))
    143  1.1  tsubai 		return (errno);
    144  1.1  tsubai 
    145  1.1  tsubai #else
    146  1.1  tsubai 
    147  1.1  tsubai 	/* Get boot info using BOOTP way. (RFC951, RFC1048) */
    148  1.1  tsubai 	bootp(netdev_sock);
    149  1.1  tsubai 
    150  1.1  tsubai 	printf("Using IP address: %s\n", inet_ntoa(myip));
    151  1.1  tsubai 
    152  1.1  tsubai 	printf("myip: %s (%s)", hostname, inet_ntoa(myip));
    153  1.1  tsubai 	if (gateip)
    154  1.1  tsubai 		printf(", gateip: %s", inet_ntoa(gateip));
    155  1.1  tsubai 	if (netmask)
    156  1.1  tsubai 		printf(", netmask: %s", intoa(netmask));
    157  1.1  tsubai 	printf("\n");
    158  1.1  tsubai 
    159  1.1  tsubai #endif
    160  1.1  tsubai 
    161  1.1  tsubai 	printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
    162  1.1  tsubai 
    163  1.1  tsubai 	/* Get the NFS file handle (mount). */
    164  1.1  tsubai 	if (nfs_mount(netdev_sock, rootip, rootpath) != 0)
    165  1.1  tsubai 		return (errno);
    166  1.1  tsubai 
    167  1.1  tsubai 	return (0);
    168  1.1  tsubai }
    169