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