net.c revision 1.1
11.1Smrg/*	$NetBSD: net.c,v 1.1 1997/06/01 03:39:34 mrg Exp $	*/
21.1Smrg
31.1Smrg/*
41.1Smrg * Copyright (c) 1995 Gordon W. Ross
51.1Smrg * All rights reserved.
61.1Smrg *
71.1Smrg * Redistribution and use in source and binary forms, with or without
81.1Smrg * modification, are permitted provided that the following conditions
91.1Smrg * are met:
101.1Smrg * 1. Redistributions of source code must retain the above copyright
111.1Smrg *    notice, this list of conditions and the following disclaimer.
121.1Smrg * 2. Redistributions in binary form must reproduce the above copyright
131.1Smrg *    notice, this list of conditions and the following disclaimer in the
141.1Smrg *    documentation and/or other materials provided with the distribution.
151.1Smrg * 3. The name of the author may not be used to endorse or promote products
161.1Smrg *    derived from this software without specific prior written permission.
171.1Smrg * 4. All advertising materials mentioning features or use of this software
181.1Smrg *    must display the following acknowledgement:
191.1Smrg *      This product includes software developed by Gordon W. Ross
201.1Smrg *
211.1Smrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
221.1Smrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
231.1Smrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
241.1Smrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
251.1Smrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
261.1Smrg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
271.1Smrg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
281.1Smrg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
291.1Smrg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
301.1Smrg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
311.1Smrg */
321.1Smrg
331.1Smrg/*
341.1Smrg * This module implements a "raw device" interface suitable for
351.1Smrg * use by the stand-alone I/O library NFS code.  This interface
361.1Smrg * does not support any "block" access, and exists only for the
371.1Smrg * purpose of initializing the network interface, getting boot
381.1Smrg * parameters, and performing the NFS mount.
391.1Smrg *
401.1Smrg * At open time, this does:
411.1Smrg *
421.1Smrg * find interface      - netif_open()
431.1Smrg * RARP for IP address - rarp_getipaddress()
441.1Smrg * RPC/bootparams      - callrpc(d, RPC_BOOTPARAMS, ...)
451.1Smrg * RPC/mountd          - nfs_mount(sock, ip, path)
461.1Smrg *
471.1Smrg * the root file handle from mountd is saved in a global
481.1Smrg * for use by the NFS open code (NFS/lookup).
491.1Smrg */
501.1Smrg
511.1Smrg#include <sys/param.h>
521.1Smrg#include <sys/socket.h>
531.1Smrg#include <net/if.h>
541.1Smrg#include <netinet/in.h>
551.1Smrg#include <lib/libsa/if_ether.h>
561.1Smrg#include <netinet/in_systm.h>
571.1Smrg
581.1Smrg#include <lib/libsa/stand.h>
591.1Smrg#include <lib/libsa/net.h>
601.1Smrg#include <lib/libsa/netif.h>
611.1Smrg#include <lib/libsa/bootparam.h>
621.1Smrg
631.1Smrgchar		rootpath[FNAME_SIZE];
641.1Smrg
651.1Smrgint	netdev_sock = -1;
661.1Smrgstatic	int open_count;
671.1Smrg
681.1Smrg/*
691.1Smrg * Called by devopen after it sets f->f_dev to our devsw entry.
701.1Smrg * This opens the low-level device and sets f->f_devdata.
711.1Smrg */
721.1Smrgint
731.1Smrgnet_open(pd)
741.1Smrg	struct promdata *pd;
751.1Smrg{
761.1Smrg	int error = 0;
771.1Smrg
781.1Smrg	/* On first open, do netif open, mount, etc. */
791.1Smrg	if (open_count == 0) {
801.1Smrg		/* Find network interface. */
811.1Smrg		if ((netdev_sock = netif_open(pd)) < 0) {
821.1Smrg			error = errno;
831.1Smrg			goto bad;
841.1Smrg		}
851.1Smrg		if ((error = net_mountroot()) != 0)
861.1Smrg			goto bad;
871.1Smrg	}
881.1Smrg	open_count++;
891.1Smrgbad:
901.1Smrg	return (error);
911.1Smrg}
921.1Smrg
931.1Smrgint
941.1Smrgnet_close(pd)
951.1Smrg	struct promdata *pd;
961.1Smrg{
971.1Smrg	/* On last close, do netif close, etc. */
981.1Smrg	if (open_count > 0)
991.1Smrg		if (--open_count == 0)
1001.1Smrg			netif_close(netdev_sock);
1011.1Smrg}
1021.1Smrg
1031.1Smrgint
1041.1Smrgnet_mountroot()
1051.1Smrg{
1061.1Smrg
1071.1Smrg#ifdef DEBUG
1081.1Smrg	printf("net_mountroot\n");
1091.1Smrg#endif
1101.1Smrg
1111.1Smrg	/*
1121.1Smrg	 * Get info for NFS boot: our IP address, our hostname,
1131.1Smrg	 * server IP address, and our root path on the server.
1141.1Smrg	 * There are two ways to do this:  The old, Sun way,
1151.1Smrg	 * and the more modern, BOOTP way. (RFC951, RFC1048)
1161.1Smrg	 */
1171.1Smrg
1181.1Smrg#ifdef	SUN_BOOTPARAMS
1191.1Smrg	/* Get boot info using RARP and Sun bootparams. */
1201.1Smrg
1211.1Smrg	/* Get our IP address.  (rarp.c) */
1221.1Smrg	if (rarp_getipaddress(netdev_sock) == -1)
1231.1Smrg		return errno;
1241.1Smrg
1251.1Smrg	printf("boot: client IP address: %s\n", inet_ntoa(myip));
1261.1Smrg
1271.1Smrg	/* Get our hostname, server IP address. */
1281.1Smrg	if (bp_whoami(netdev_sock))
1291.1Smrg		return (errno);
1301.1Smrg
1311.1Smrg	printf("boot: client name: %s\n", hostname);
1321.1Smrg
1331.1Smrg	/* Get the root pathname. */
1341.1Smrg	if (bp_getfile(netdev_sock, "root", &rootip, rootpath))
1351.1Smrg		return (errno);
1361.1Smrg
1371.1Smrg#else
1381.1Smrg
1391.1Smrg	/* Get boot info using BOOTP way. (RFC951, RFC1048) */
1401.1Smrg	bootp(netdev_sock);
1411.1Smrg
1421.1Smrg	printf("Using IP address: %s\n", inet_ntoa(myip));
1431.1Smrg
1441.1Smrg	printf("myip: %s (%s)", hostname, inet_ntoa(myip));
1451.1Smrg	if (gateip)
1461.1Smrg		printf(", gateip: %s", inet_ntoa(gateip));
1471.1Smrg	if (netmask)
1481.1Smrg		printf(", netmask: %s", intoa(netmask));
1491.1Smrg	printf("\n");
1501.1Smrg
1511.1Smrg#endif
1521.1Smrg
1531.1Smrg	printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
1541.1Smrg
1551.1Smrg	/* Get the NFS file handle (mount). */
1561.1Smrg	if (nfs_mount(netdev_sock, rootip, rootpath) != 0)
1571.1Smrg		return (errno);
1581.1Smrg	return 0;
1591.1Smrg}
160