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