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