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