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