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