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