199767f8919635c4928607450d9e0abb932109ceToomas Soome/* $NetBSD: dev_net.c,v 1.23 2008/04/28 20:24:06 martin Exp $ */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1997 The NetBSD Foundation, Inc.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This code is derived from software contributed to The NetBSD Foundation
199767f8919635c4928607450d9e0abb932109ceToomas Soome * by Gordon W. Ross.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
199767f8919635c4928607450d9e0abb932109ceToomas Soome * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
199767f8919635c4928607450d9e0abb932109ceToomas Soome * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
199767f8919635c4928607450d9e0abb932109ceToomas Soome * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
199767f8919635c4928607450d9e0abb932109ceToomas Soome * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * POSSIBILITY OF SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This module implements a "raw device" interface suitable for
199767f8919635c4928607450d9e0abb932109ceToomas Soome * use by the stand-alone I/O library NFS code. This interface
199767f8919635c4928607450d9e0abb932109ceToomas Soome * does not support any "block" access, and exists only for the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * purpose of initializing the network interface, getting boot
199767f8919635c4928607450d9e0abb932109ceToomas Soome * parameters, and performing the NFS mount.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * At open time, this does:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * find interface - netif_open()
199767f8919635c4928607450d9e0abb932109ceToomas Soome * RARP for IP address - rarp_getipaddress()
199767f8919635c4928607450d9e0abb932109ceToomas Soome * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * RPC/mountd - nfs_mount(sock, ip, path)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the root file handle from mountd is saved in a global
199767f8919635c4928607450d9e0abb932109ceToomas Soome * for use by the NFS open code (NFS/lookup).
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int net_init(void);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void net_cleanup(void);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int net_print(int);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Called by devopen after it sets f->f_dev to our devsw entry.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This opens the low-level device and sets f->f_devdata.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This is declared with variable arguments...
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *devname; /* Device part of file name (or NULL). */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Before opening another interface, close the previous one first. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (netdev_sock >= 0 && strcmp(devname, netdev_name) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* On first open, do netif open, mount, etc. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Find network interface. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If network params were not set by netif_open(), try to get
199767f8919635c4928607450d9e0abb932109ceToomas Soome * them via bootp, rarp, etc.
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Get root IP address, and path, etc. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* getparams makes its own noise */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Set the variables required by the kernel's nfs_diskless
199767f8919635c4928607450d9e0abb932109ceToomas Soome * mechanism. This is the minimum set of variables required to
199767f8919635c4928607450d9e0abb932109ceToomas Soome * mount a root filesystem without needing to obtain additional
199767f8919635c4928607450d9e0abb932109ceToomas Soome * info from bootp or other sources.
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.netif.hwaddr", ether_sprintf(d->myea), 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.netif.netmask", intoa(netmask), 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.netif.gateway", inet_ntoa(gateip), 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Extra close call? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Not last close? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* On last close, do netif close, etc. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_cleanup: calling netif_close()\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Get info for NFS boot: our IP address, our hostname,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * server IP address, and our root path on the server.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * There are two ways to do this: The old, Sun way,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * and the more modern, BOOTP way. (RFC951, RFC1048)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The default is to use the Sun bootparams RPC
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (because that is what the kernel will do).
199767f8919635c4928607450d9e0abb932109ceToomas Soome * MD code can make try_bootp initialied data,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * which will override this common definition.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Try to get boot info using BOOTP. If we succeed, then
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the server IP address, gateway, and root path will all
199767f8919635c4928607450d9e0abb932109ceToomas Soome * be initialized. If any remain uninitialized, we will
199767f8919635c4928607450d9e0abb932109ceToomas Soome * use RARP and RPC/bootparam (the Sun way) to get them.
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_open: BOOTP failed, trying RARP/RPC...\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Use RARP to get our IP address. This also sets our
199767f8919635c4928607450d9e0abb932109ceToomas Soome * netmask to the "natural" default for our address.
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_open: client addr: %s\n", inet_ntoa(myip));
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Get our hostname, server IP address, gateway. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_open: bootparam/whoami RPC failed\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_open: client name: %s\n", hostname);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Ignore the gateway from whoami (unreliable).
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Use the "gateway" parameter instead.
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bp_getfile(sock, "gateway", &gateip, buf) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Got it! Parse the netmask. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_open: subnet mask: %s\n", intoa(netmask));
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_open: net gateway: %s\n", inet_ntoa(gateip));
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Get the root server and pathname. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bp_getfile(sock, "root", &rootip, rootpath)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_open: bootparam/getfile RPC failed\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((rootaddr = net_parse_rootpath()) != INADDR_NONE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_open: server addr: %s\n", inet_ntoa(rootip));
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_open: server path: %s\n", rootpath);
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (d = 0; netif_drivers[d]; d++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(line, 80, " (%s%d)", drv->netif_bname,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Strip the server's address off of the rootpath if present and return it in
199767f8919635c4928607450d9e0abb932109ceToomas Soome * network byte order, leaving just the pathname part in the global rootpath.
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; rootpath[i] != '\0' && i < FNAME_SIZE; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (i && i != FNAME_SIZE && rootpath[i] == ':') {