199767f8919635c4928607450d9e0abb932109ceToomas Soome/* $NetBSD: dev_net.c,v 1.23 2008/04/28 20:24:06 martin Exp $ */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1997 The NetBSD Foundation, Inc.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This code is derived from software contributed to The NetBSD Foundation
199767f8919635c4928607450d9e0abb932109ceToomas Soome * by Gordon W. Ross.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 * are met:
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 *
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
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 *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * At open time, this does:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 *
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 Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <machine/stdarg.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/param.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/socket.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <net/if.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <netinet/in.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <netinet/in_systm.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stand.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <string.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <net.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <netif.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <bootp.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <bootparam.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "dev_net.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "bootstrap.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NETIF_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint debug = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char *netdev_name;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int netdev_sock = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int netdev_opens;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int net_init(void);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int net_open(struct open_file *, ...);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int net_close(struct open_file *);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void net_cleanup(void);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int net_strategy();
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int net_print(int);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int net_getparams(int sock);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct devsw netdev = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome "net",
199767f8919635c4928607450d9e0abb932109ceToomas Soome DEVT_NET,
199767f8919635c4928607450d9e0abb932109ceToomas Soome net_init,
199767f8919635c4928607450d9e0abb932109ceToomas Soome net_strategy,
199767f8919635c4928607450d9e0abb932109ceToomas Soome net_open,
199767f8919635c4928607450d9e0abb932109ceToomas Soome net_close,
199767f8919635c4928607450d9e0abb932109ceToomas Soome noioctl,
199767f8919635c4928607450d9e0abb932109ceToomas Soome net_print,
199767f8919635c4928607450d9e0abb932109ceToomas Soome net_cleanup
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomenet_init(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomenet_open(struct open_file *f, ...)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct iodesc *d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_list args;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *devname; /* Device part of file name (or NULL). */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int error = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_start(args, f);
199767f8919635c4928607450d9e0abb932109ceToomas Soome devname = va_arg(args, char*);
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_end(args);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NETIF_OPEN_CLOSE_ONCE
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Before opening another interface, close the previous one first. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (netdev_sock >= 0 && strcmp(devname, netdev_name) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome net_cleanup();
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* On first open, do netif open, mount, etc. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (netdev_opens == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Find network interface. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (netdev_sock < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome netdev_sock = netif_open(devname);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (netdev_sock < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_open: netif_open() failed\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENXIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome netdev_name = strdup(devname);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NETIF_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_open: netif_open() succeeded\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If network params were not set by netif_open(), try to get
199767f8919635c4928607450d9e0abb932109ceToomas Soome * them via bootp, rarp, etc.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rootip.s_addr == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Get root IP address, and path, etc. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = net_getparams(netdev_sock);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* getparams makes its own noise */
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(netdev_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome netif_close(netdev_sock);
199767f8919635c4928607450d9e0abb932109ceToomas Soome netdev_sock = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome d = socktodesc(netdev_sock);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.netif.hwaddr", ether_sprintf(d->myea), 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.netif.ip", inet_ntoa(myip), 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 setenv("boot.nfsroot.path", rootpath, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome netdev_opens++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_devdata = &netdev_sock;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomenet_close(struct open_file *f)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NETIF_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_close: opens=%d\n", netdev_opens);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_devdata = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef NETIF_OPEN_CLOSE_ONCE
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Extra close call? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (netdev_opens <= 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome netdev_opens--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Not last close? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (netdev_opens > 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* On last close, do netif close, etc. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NETIF_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_close: calling net_cleanup()\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome net_cleanup();
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomenet_cleanup(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (netdev_sock >= 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NETIF_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_cleanup: calling netif_close()\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome rootip.s_addr = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(netdev_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome netif_close(netdev_sock);
199767f8919635c4928607450d9e0abb932109ceToomas Soome netdev_sock = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomenet_strategy()
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define SUPPORT_BOOTP
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
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 *
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef SUPPORT_BOOTP
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint try_bootp = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern n_long ip_convertaddr(char *p);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomenet_getparams(int sock)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char buf[MAXHOSTNAMELEN];
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long rootaddr, smask;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef SUPPORT_BOOTP
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (try_bootp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome bootp(sock, BOOTP_NONE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (myip.s_addr != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto exit;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NETIF_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_open: BOOTP failed, trying RARP/RPC...\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rarp_getipaddress(sock)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_open: RARP failed\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_open: client addr: %s\n", inet_ntoa(myip));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Get our hostname, server IP address, gateway. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bp_whoami(sock)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_open: bootparam/whoami RPC failed\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NETIF_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_open: client name: %s\n", hostname);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Ignore the gateway from whoami (unreliable).
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Use the "gateway" parameter instead.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome smask = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome gateip.s_addr = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bp_getfile(sock, "gateway", &gateip, buf) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Got it! Parse the netmask. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome smask = ip_convertaddr(buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (smask) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome netmask = smask;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NETIF_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_open: subnet mask: %s\n", intoa(netmask));
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NETIF_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (gateip.s_addr && debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_open: net gateway: %s\n", inet_ntoa(gateip));
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soomeexit:
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((rootaddr = net_parse_rootpath()) != INADDR_NONE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome rootip.s_addr = rootaddr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NETIF_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_open: server addr: %s\n", inet_ntoa(rootip));
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_open: server path: %s\n", rootpath);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomenet_print(int verbose)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct netif_driver *drv;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char line[80];
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i, d, cnt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int ret = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome cnt = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (d = 0; netif_drivers[d]; d++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome drv = netif_drivers[d];
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < drv->netif_nifs; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(line, 80, "\t%s%d:", "net", cnt++);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret = pager_output(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ret != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (verbose) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(line, 80, " (%s%d)", drv->netif_bname,
199767f8919635c4928607450d9e0abb932109ceToomas Soome drv->netif_ifs[i].dif_unit);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret = pager_output(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ret != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ret |= pager_output("\n"));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeuint32_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomenet_parse_rootpath()
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long addr = INADDR_NONE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; rootpath[i] != '\0' && i < FNAME_SIZE; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rootpath[i] == ':')
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (i && i != FNAME_SIZE && rootpath[i] == ':') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome rootpath[i++] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr = inet_addr(&rootpath[0]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(&rootpath[i], rootpath, strlen(&rootpath[i])+1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (addr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}