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