Home | History | Annotate | Line # | Download | only in netboot
dev_net.c revision 1.9
      1 /* $NetBSD: dev_net.c,v 1.9 1999/11/12 13:11:41 simonb 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 
     64 #include "dev_net.h"
     65 
     66 #ifndef SUN_BOOTPARAMS
     67 void bootp      __P((int));
     68 #endif
     69 
     70 extern int debug;
     71 extern int nfs_root_node[];	/* XXX - get from nfs_mount() */
     72 
     73 /*
     74  * Various globals needed by the network code:
     75  */
     76 
     77 #if 0
     78 /* for arp.c, rarp.c */
     79 u_char bcea[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
     80 #endif
     81 
     82 struct	in_addr myip;		/* my ip address */
     83 struct	in_addr rootip;		/* root ip address */
     84 struct	in_addr gateip;		/* swap ip address */
     85 n_long	netmask;		/* subnet or net mask */
     86 
     87 char rootpath[FNAME_SIZE];
     88 
     89 int hostnamelen;
     90 char hostname[FNAME_SIZE];
     91 
     92 int domainnamelen;
     93 char domainname[FNAME_SIZE];
     94 
     95 /*
     96  * Local things...
     97  */
     98 static int netdev_sock = -1;
     99 static int netdev_opens;
    100 
    101 int net_getparams __P((int));
    102 
    103 /*
    104  * Called by devopen after it sets f->f_dev to our devsw entry.
    105  * This opens the low-level device and sets f->f_devdata.
    106  * This is declared with variable arguments...
    107  */
    108 int
    109 net_open(struct open_file *f, ...)
    110 {
    111 	va_list ap;
    112 	char *devname;		/* Device part of file name (or NULL). */
    113 	int error = 0;
    114 
    115 	va_start(ap, f);
    116 	devname = va_arg(ap, char*);
    117 	va_end(ap);
    118 
    119 #ifdef	NETIF_DEBUG
    120 	if (debug)
    121 		printf("net_open: %s\n", devname);
    122 #endif
    123 
    124 	/* On first open, do netif open, mount, etc. */
    125 	if (netdev_opens == 0) {
    126 		/* Find network interface. */
    127 		if (netdev_sock < 0) {
    128 			netdev_sock = netif_open(devname);
    129 			if (netdev_sock < 0) {
    130 				printf("net_open: netif_open() failed\n");
    131 				return (ENXIO);
    132 			}
    133 			if (debug)
    134 				printf("net_open: netif_open() succeeded\n");
    135 		}
    136 		if (rootip.s_addr == 0) {
    137 			/* Get root IP address, and path, etc. */
    138 			error = net_getparams(netdev_sock);
    139 			if (error) {
    140 				/* getparams makes its own noise */
    141 				goto fail;
    142 			}
    143 			/* Get the NFS file handle (mountd). */
    144 			error = nfs_mount(netdev_sock, rootip, rootpath);
    145 			if (error) {
    146 				error = errno;
    147 				printf("net_open: NFS mount error=%d\n", error);
    148 				rootip.s_addr = 0;
    149 			fail:
    150 				netif_close(netdev_sock);
    151 				netdev_sock = -1;
    152 				return (error);
    153 			}
    154 			if (debug)
    155 				printf("net_open: NFS mount succeeded\n");
    156 		}
    157 	}
    158 	netdev_opens++;
    159 	f->f_devdata = nfs_root_node;
    160 	return (error);
    161 }
    162 
    163 int
    164 net_close(f)
    165 	struct open_file *f;
    166 {
    167 
    168 #ifdef	NETIF_DEBUG
    169 	if (debug)
    170 		printf("net_close: opens=%d\n", netdev_opens);
    171 #endif
    172 
    173 	/* On last close, do netif close, etc. */
    174 	f->f_devdata = NULL;
    175 	/* Extra close call? */
    176 	if (netdev_opens <= 0)
    177 		return (0);
    178 	netdev_opens--;
    179 	/* Not last close? */
    180 	if (netdev_opens > 0)
    181 		return(0);
    182 	rootip.s_addr = 0;
    183 	if (netdev_sock >= 0) {
    184 		if (debug)
    185 			printf("net_close: calling netif_close()\n");
    186 		netif_close(netdev_sock);
    187 		netdev_sock = -1;
    188 	}
    189 	return (0);
    190 }
    191 
    192 int
    193 net_ioctl()
    194 {
    195 	return EIO;
    196 }
    197 
    198 int
    199 net_strategy()
    200 {
    201 	return EIO;
    202 }
    203 
    204 int
    205 net_getparams(sock)
    206 	int sock;
    207 {
    208 	/*
    209 	 * Get info for NFS boot: our IP address, our hostname,
    210 	 * server IP address, and our root path on the server.
    211 	 * There are two ways to do this:  The old, Sun way,
    212 	 * and the more modern, BOOTP way. (RFC951, RFC1048)
    213 	 */
    214 
    215 #ifdef	SUN_BOOTPARAMS
    216 	/* Get our IP address.  (rarp.c) */
    217 	if (rarp_getipaddress(sock)) {
    218 		printf("net_open: RARP failed\n");
    219 		return (EIO);
    220 	}
    221 #else	/* BOOTPARAMS */
    222 	/*
    223 	 * Get boot info using BOOTP. (RFC951, RFC1048)
    224 	 * This also gets the server IP address, gateway,
    225 	 * root path, etc.
    226 	 */
    227 	bootp(sock);
    228 	if (myip.s_addr == 0) {
    229 		printf("net_open: BOOTP failed\n");
    230 		return (EIO);
    231 	}
    232 #endif	/* BOOTPARAMS */
    233 
    234 	printf("boot: client addr: %s\n", inet_ntoa(myip));
    235 
    236 #ifdef	SUN_BOOTPARAMS
    237 	/* Get our hostname, server IP address, gateway. */
    238 	if (bp_whoami(sock)) {
    239 		printf("net_open: bootparam/whoami RPC failed\n");
    240 		return (EIO);
    241 	}
    242 #endif	/* BOOTPARAMS */
    243 
    244 	printf("boot: client name: %s\n", hostname);
    245 	if (gateip.s_addr) {
    246 		printf("boot: subnet mask: %s\n", intoa(netmask));
    247 		printf("boot: net gateway: %s\n", inet_ntoa(gateip));
    248 	}
    249 
    250 #ifdef	SUN_BOOTPARAMS
    251 	/* Get the root pathname. */
    252 	if (bp_getfile(sock, "root", &rootip, rootpath)) {
    253 		printf("net_open: bootparam/getfile RPC failed\n");
    254 		return (EIO);
    255 	}
    256 #endif	/* BOOTPARAMS */
    257 
    258 	printf("boot: server addr: %s\n", inet_ntoa(rootip));
    259 	printf("boot: server path: %s\n", rootpath);
    260 
    261 	return (0);
    262 }
    263