net.c revision 1.6
11.6Saymeric/*	$NetBSD: net.c,v 1.6 2003/06/26 20:46:31 aymeric 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.5Sdrochner * find interface	- netif_of_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.6Saymeric#include "net.h"
541.6Saymeric
551.1Sthorpej#include <sys/param.h>
561.1Sthorpej#include <sys/socket.h>
571.3Smycroft
581.1Sthorpej#include <net/if.h>
591.1Sthorpej#include <netinet/in.h>
601.1Sthorpej#include <netinet/in_systm.h>
611.1Sthorpej
621.1Sthorpej#include <lib/libsa/stand.h>
631.6Saymeric#include <lib/libsa/bootp.h>
641.1Sthorpej#include <lib/libsa/net.h>
651.6Saymeric#include <lib/libsa/nfs.h>
661.4Sdrochner
671.4Sdrochner#include <lib/libkern/libkern.h>
681.1Sthorpej
691.5Sdrochner#include "ofdev.h"
701.5Sdrochner#include "netif_of.h"
711.5Sdrochner
721.1Sthorpejchar	rootpath[FNAME_SIZE];
731.1Sthorpej
741.1Sthorpejstatic	int netdev_sock = -1;
751.1Sthorpejstatic	int open_count;
761.1Sthorpej
771.6Saymericstatic int net_mountroot(void);
781.6Saymeric
791.1Sthorpej/*
801.1Sthorpej * Called by devopen after it sets f->f_dev to our devsw entry.
811.1Sthorpej * This opens the low-level device and sets f->f_devdata.
821.1Sthorpej */
831.1Sthorpejint
841.6Saymericnet_open(struct of_dev *op)
851.1Sthorpej{
861.1Sthorpej	int error = 0;
871.5Sdrochner
881.1Sthorpej	/*
891.1Sthorpej	 * On first open, do netif open, mount, etc.
901.1Sthorpej	 */
911.1Sthorpej	if (open_count == 0) {
921.1Sthorpej		/* Find network interface. */
931.5Sdrochner		if ((netdev_sock = netif_of_open(op)) < 0) {
941.1Sthorpej			error = errno;
951.1Sthorpej			goto bad;
961.1Sthorpej		}
971.1Sthorpej		if ((error = net_mountroot()) != 0)
981.1Sthorpej			goto bad;
991.1Sthorpej	}
1001.1Sthorpej	open_count++;
1011.1Sthorpejbad:
1021.1Sthorpej	if (netdev_sock >= 0 && open_count == 0) {
1031.5Sdrochner		netif_of_close(netdev_sock);
1041.1Sthorpej		netdev_sock = -1;
1051.1Sthorpej	}
1061.1Sthorpej	return error;
1071.1Sthorpej}
1081.1Sthorpej
1091.1Sthorpejint
1101.6Saymericnet_close(struct of_dev *op)
1111.1Sthorpej{
1121.1Sthorpej	/*
1131.1Sthorpej	 * On last close, do netif close, etc.
1141.1Sthorpej	 */
1151.1Sthorpej	if (open_count > 0)
1161.1Sthorpej		if (--open_count == 0) {
1171.5Sdrochner			netif_of_close(netdev_sock);
1181.1Sthorpej			netdev_sock = -1;
1191.1Sthorpej		}
1201.6Saymeric
1211.6Saymeric	return 0;
1221.1Sthorpej}
1231.1Sthorpej
1241.6Saymericstatic int
1251.6Saymericnet_mountroot(void)
1261.1Sthorpej{
1271.1Sthorpej
1281.1Sthorpej#ifdef	DEBUG
1291.1Sthorpej	printf("net_mountroot\n");
1301.1Sthorpej#endif
1311.5Sdrochner
1321.1Sthorpej	/*
1331.1Sthorpej	 * Get info for NFS boot: our IP address, out hostname,
1341.1Sthorpej	 * server IP address, and our root path on the server.
1351.1Sthorpej	 * We use BOOTP (RFC951, RFC1532) exclusively as mandated
1361.1Sthorpej	 * by PowerPC Reference Platform Specification I.4.2
1371.1Sthorpej	 */
1381.5Sdrochner
1391.1Sthorpej	bootp(netdev_sock);
1401.5Sdrochner
1411.1Sthorpej	if (myip.s_addr == 0)
1421.1Sthorpej		return ETIMEDOUT;
1431.5Sdrochner
1441.1Sthorpej	printf("Using IP address: %s\n", inet_ntoa(myip));
1451.5Sdrochner
1461.1Sthorpej#ifdef	DEBUG
1471.1Sthorpej	printf("myip: %s (%s)", hostname, inet_ntoa(myip));
1481.1Sthorpej	if (gateip.s_addr)
1491.1Sthorpej		printf(", gateip: %s", inet_ntoa(gateip));
1501.1Sthorpej	if (netmask)
1511.1Sthorpej		printf(", netmask: %s", intoa(netmask));
1521.1Sthorpej	printf("\n");
1531.1Sthorpej#endif
1541.1Sthorpej	printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
1551.5Sdrochner
1561.1Sthorpej	/*
1571.1Sthorpej	 * Get the NFS file handle (mount).
1581.1Sthorpej	 */
1591.1Sthorpej	if (nfs_mount(netdev_sock, rootip, rootpath) < 0)
1601.1Sthorpej		return errno;
1611.1Sthorpej	return 0;
1621.1Sthorpej}
163