net.c revision 1.8
11.8Suwe/* $NetBSD: net.c,v 1.8 2006/01/27 03:58:27 uwe 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.4Sdrochner * find interface - netif_of_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.5Saymeric#include "net.h" 541.5Saymeric 551.1Stsubai#include <sys/param.h> 561.1Stsubai#include <sys/socket.h> 571.1Stsubai 581.1Stsubai#include <net/if.h> 591.1Stsubai#include <netinet/in.h> 601.1Stsubai#include <netinet/in_systm.h> 611.1Stsubai 621.1Stsubai#include <lib/libsa/stand.h> 631.8Suwe#include <lib/libsa/bootp.h> 641.1Stsubai#include <lib/libsa/net.h> 651.8Suwe#include <lib/libsa/nfs.h> 661.2Sdrochner 671.2Sdrochner#include <lib/libkern/libkern.h> 681.1Stsubai 691.4Sdrochner#include "ofdev.h" 701.4Sdrochner#include "netif_of.h" 711.4Sdrochner 721.1Stsubaichar rootpath[FNAME_SIZE]; 731.1Stsubai 741.1Stsubaistatic int netdev_sock = -1; 751.1Stsubaistatic int open_count; 761.1Stsubai 771.5Saymericstatic int net_mountroot(void); 781.5Saymeric 791.1Stsubai/* 801.1Stsubai * Called by devopen after it sets f->f_dev to our devsw entry. 811.1Stsubai * This opens the low-level device and sets f->f_devdata. 821.1Stsubai */ 831.1Stsubaiint 841.5Saymericnet_open(struct of_dev *op) 851.1Stsubai{ 861.1Stsubai int error = 0; 871.3Stsutsui 881.1Stsubai /* 891.1Stsubai * On first open, do netif open, mount, etc. 901.1Stsubai */ 911.1Stsubai if (open_count == 0) { 921.1Stsubai /* Find network interface. */ 931.4Sdrochner if ((netdev_sock = netif_of_open(op)) < 0) { 941.1Stsubai error = errno; 951.1Stsubai goto bad; 961.1Stsubai } 971.1Stsubai if ((error = net_mountroot()) != 0) 981.1Stsubai goto bad; 991.1Stsubai } 1001.1Stsubai open_count++; 1011.1Stsubaibad: 1021.1Stsubai if (netdev_sock >= 0 && open_count == 0) { 1031.4Sdrochner netif_of_close(netdev_sock); 1041.1Stsubai netdev_sock = -1; 1051.1Stsubai } 1061.1Stsubai return error; 1071.1Stsubai} 1081.1Stsubai 1091.1Stsubaiint 1101.5Saymericnet_close(struct of_dev *op) 1111.1Stsubai{ 1121.1Stsubai /* 1131.1Stsubai * On last close, do netif close, etc. 1141.1Stsubai */ 1151.1Stsubai if (open_count > 0) 1161.1Stsubai if (--open_count == 0) { 1171.4Sdrochner netif_of_close(netdev_sock); 1181.1Stsubai netdev_sock = -1; 1191.1Stsubai } 1201.7Suwe return 0; 1211.1Stsubai} 1221.1Stsubai 1231.5Saymericstatic int 1241.5Saymericnet_mountroot(void) 1251.1Stsubai{ 1261.1Stsubai 1271.1Stsubai#ifdef DEBUG 1281.1Stsubai printf("net_mountroot\n"); 1291.1Stsubai#endif 1301.3Stsutsui 1311.1Stsubai /* 1321.1Stsubai * Get info for NFS boot: our IP address, out hostname, 1331.1Stsubai * server IP address, and our root path on the server. 1341.1Stsubai * We use BOOTP (RFC951, RFC1532) exclusively as mandated 1351.1Stsubai * by PowerPC Reference Platform Specification I.4.2 1361.1Stsubai */ 1371.3Stsutsui 1381.1Stsubai bootp(netdev_sock); 1391.3Stsutsui 1401.1Stsubai if (myip.s_addr == 0) 1411.1Stsubai return ETIMEDOUT; 1421.3Stsutsui 1431.1Stsubai printf("Using IP address: %s\n", inet_ntoa(myip)); 1441.3Stsutsui 1451.1Stsubai#ifdef DEBUG 1461.1Stsubai printf("myip: %s (%s)", hostname, inet_ntoa(myip)); 1471.1Stsubai if (gateip.s_addr) 1481.1Stsubai printf(", gateip: %s", inet_ntoa(gateip)); 1491.1Stsubai if (netmask) 1501.1Stsubai printf(", netmask: %s", intoa(netmask)); 1511.1Stsubai printf("\n"); 1521.1Stsubai#endif 1531.1Stsubai printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath); 1541.3Stsutsui 1551.1Stsubai /* 1561.1Stsubai * Get the NFS file handle (mount). 1571.1Stsubai */ 1581.1Stsubai if (nfs_mount(netdev_sock, rootip, rootpath) < 0) 1591.1Stsubai return errno; 1601.1Stsubai return 0; 1611.1Stsubai} 162