Home | History | Annotate | Line # | Download | only in boot
      1  1.7       mrg /*	$NetBSD: net.c,v 1.7 2021/04/12 03:55:40 mrg Exp $	*/
      2  1.1    tsubai 
      3  1.1    tsubai /*
      4  1.1    tsubai  * Copyright (c) 1995 Gordon W. Ross
      5  1.1    tsubai  * All rights reserved.
      6  1.1    tsubai  *
      7  1.1    tsubai  * Redistribution and use in source and binary forms, with or without
      8  1.1    tsubai  * modification, are permitted provided that the following conditions
      9  1.1    tsubai  * are met:
     10  1.1    tsubai  * 1. Redistributions of source code must retain the above copyright
     11  1.1    tsubai  *    notice, this list of conditions and the following disclaimer.
     12  1.1    tsubai  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1    tsubai  *    notice, this list of conditions and the following disclaimer in the
     14  1.1    tsubai  *    documentation and/or other materials provided with the distribution.
     15  1.1    tsubai  *
     16  1.1    tsubai  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1    tsubai  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1    tsubai  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1    tsubai  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1    tsubai  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  1.1    tsubai  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  1.1    tsubai  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  1.1    tsubai  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  1.1    tsubai  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  1.1    tsubai  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  1.1    tsubai  */
     27  1.1    tsubai 
     28  1.1    tsubai /*
     29  1.1    tsubai  * This module implements a "raw device" interface suitable for
     30  1.1    tsubai  * use by the stand-alone I/O library NFS code.  This interface
     31  1.1    tsubai  * does not support any "block" access, and exists only for the
     32  1.1    tsubai  * purpose of initializing the network interface, getting boot
     33  1.1    tsubai  * parameters, and performing the NFS mount.
     34  1.1    tsubai  *
     35  1.1    tsubai  * At open time, this does:
     36  1.1    tsubai  *
     37  1.1    tsubai  * find interface      - netif_open()
     38  1.1    tsubai  * RARP for IP address - rarp_getipaddress()
     39  1.1    tsubai  * RPC/bootparams      - callrpc(d, RPC_BOOTPARAMS, ...)
     40  1.1    tsubai  * RPC/mountd          - nfs_mount(sock, ip, path)
     41  1.1    tsubai  *
     42  1.1    tsubai  * the root file handle from mountd is saved in a global
     43  1.1    tsubai  * for use by the NFS open code (NFS/lookup).
     44  1.1    tsubai  */
     45  1.1    tsubai 
     46  1.1    tsubai #include <sys/param.h>
     47  1.1    tsubai #include <sys/socket.h>
     48  1.1    tsubai #include <net/if.h>
     49  1.1    tsubai #include <netinet/in.h>
     50  1.1    tsubai #include <netinet/in_systm.h>
     51  1.1    tsubai 
     52  1.1    tsubai #include <lib/libsa/stand.h>
     53  1.1    tsubai #include <lib/libsa/net.h>
     54  1.1    tsubai #include <lib/libsa/bootparam.h>
     55  1.6   tsutsui #include <lib/libsa/bootp.h>
     56  1.1    tsubai #include <lib/libsa/nfs.h>
     57  1.1    tsubai 
     58  1.1    tsubai #include <lib/libkern/libkern.h>
     59  1.1    tsubai 
     60  1.1    tsubai #include <promdev.h>
     61  1.1    tsubai 
     62  1.2  drochner #include "netif_news.h"
     63  1.2  drochner 
     64  1.1    tsubai int	netdev_sock = -1;
     65  1.1    tsubai static	int open_count;
     66  1.1    tsubai 
     67  1.1    tsubai /*
     68  1.1    tsubai  * Called by devopen after it sets f->f_dev to our devsw entry.
     69  1.1    tsubai  * This opens the low-level device and sets f->f_devdata.
     70  1.1    tsubai  */
     71  1.1    tsubai int
     72  1.3   tsutsui net_open(struct romdev *pd)
     73  1.1    tsubai {
     74  1.1    tsubai 	int error = 0;
     75  1.1    tsubai 
     76  1.1    tsubai 	/* On first open, do netif open, mount, etc. */
     77  1.1    tsubai 	if (open_count == 0) {
     78  1.1    tsubai 		/* Find network interface. */
     79  1.2  drochner 		if ((netdev_sock = netif_news_open(pd)) < 0) {
     80  1.1    tsubai 			error = errno;
     81  1.1    tsubai 			goto bad;
     82  1.1    tsubai 		}
     83  1.1    tsubai 		if ((error = net_mountroot()) != 0)
     84  1.1    tsubai 			goto bad;
     85  1.1    tsubai 	}
     86  1.1    tsubai 	open_count++;
     87  1.1    tsubai bad:
     88  1.3   tsutsui 	return error;
     89  1.1    tsubai }
     90  1.1    tsubai 
     91  1.1    tsubai int
     92  1.3   tsutsui net_close(struct romdev *pd)
     93  1.1    tsubai {
     94  1.1    tsubai 	/* On last close, do netif close, etc. */
     95  1.1    tsubai 	if (open_count <= 0)
     96  1.3   tsutsui 		return 0;
     97  1.1    tsubai 
     98  1.1    tsubai 	if (--open_count == 0)
     99  1.2  drochner 		netif_news_close(netdev_sock);
    100  1.1    tsubai 
    101  1.3   tsutsui 	return 0;
    102  1.1    tsubai }
    103  1.1    tsubai 
    104  1.1    tsubai int
    105  1.3   tsutsui net_mountroot(void)
    106  1.1    tsubai {
    107  1.1    tsubai 
    108  1.1    tsubai #ifdef DEBUG
    109  1.1    tsubai 	printf("net_mountroot\n");
    110  1.1    tsubai #endif
    111  1.1    tsubai 
    112  1.1    tsubai 	/*
    113  1.1    tsubai 	 * Get info for NFS boot: our IP address, our hostname,
    114  1.1    tsubai 	 * server IP address, and our root path on the server.
    115  1.1    tsubai 	 * There are two ways to do this:  The old, Sun way,
    116  1.1    tsubai 	 * and the more modern, BOOTP way. (RFC951, RFC1048)
    117  1.1    tsubai 	 */
    118  1.1    tsubai 
    119  1.1    tsubai #ifdef	SUN_BOOTPARAMS
    120  1.1    tsubai 	/* Get boot info using RARP and Sun bootparams. */
    121  1.1    tsubai 
    122  1.1    tsubai 	/* Get our IP address.  (rarp.c) */
    123  1.1    tsubai 	if (rarp_getipaddress(netdev_sock) == -1)
    124  1.1    tsubai 		return (errno);
    125  1.1    tsubai 
    126  1.1    tsubai 	printf("boot: client IP address: %s\n", inet_ntoa(myip));
    127  1.1    tsubai 
    128  1.1    tsubai 	/* Get our hostname, server IP address. */
    129  1.1    tsubai 	if (bp_whoami(netdev_sock))
    130  1.1    tsubai 		return (errno);
    131  1.1    tsubai 
    132  1.1    tsubai 	printf("boot: client name: %s\n", hostname);
    133  1.1    tsubai 
    134  1.1    tsubai 	/* Get the root pathname. */
    135  1.1    tsubai 	if (bp_getfile(netdev_sock, "root", &rootip, rootpath))
    136  1.1    tsubai 		return (errno);
    137  1.1    tsubai 
    138  1.1    tsubai #else
    139  1.1    tsubai 
    140  1.1    tsubai 	/* Get boot info using BOOTP way. (RFC951, RFC1048) */
    141  1.1    tsubai 	bootp(netdev_sock);
    142  1.1    tsubai 
    143  1.1    tsubai 	printf("Using IP address: %s\n", inet_ntoa(myip));
    144  1.1    tsubai 
    145  1.1    tsubai 	printf("myip: %s (%s)", hostname, inet_ntoa(myip));
    146  1.6   tsutsui 	if (gateip.s_addr)
    147  1.1    tsubai 		printf(", gateip: %s", inet_ntoa(gateip));
    148  1.1    tsubai 	if (netmask)
    149  1.1    tsubai 		printf(", netmask: %s", intoa(netmask));
    150  1.1    tsubai 	printf("\n");
    151  1.1    tsubai 
    152  1.1    tsubai #endif
    153  1.1    tsubai 
    154  1.1    tsubai 	printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
    155  1.1    tsubai 
    156  1.1    tsubai 	/* Get the NFS file handle (mount). */
    157  1.1    tsubai 	if (nfs_mount(netdev_sock, rootip, rootpath) != 0)
    158  1.1    tsubai 		return (errno);
    159  1.1    tsubai 
    160  1.3   tsutsui 	return 0;
    161  1.1    tsubai }
    162