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