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