net.c revision 1.5
11.5Sdrochner/* $NetBSD: net.c,v 1.5 2003/03/13 15:36:06 drochner 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.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.4Sdrochner 631.4Sdrochner#include <lib/libkern/libkern.h> 641.1Sthorpej 651.5Sdrochner#include "ofdev.h" 661.5Sdrochner#include "netif_of.h" 671.5Sdrochner 681.1Sthorpejchar rootpath[FNAME_SIZE]; 691.1Sthorpej 701.1Sthorpejstatic int netdev_sock = -1; 711.1Sthorpejstatic int open_count; 721.1Sthorpej 731.1Sthorpej/* 741.1Sthorpej * Called by devopen after it sets f->f_dev to our devsw entry. 751.1Sthorpej * This opens the low-level device and sets f->f_devdata. 761.1Sthorpej */ 771.1Sthorpejint 781.1Sthorpejnet_open(op) 791.1Sthorpej struct of_dev *op; 801.1Sthorpej{ 811.1Sthorpej int error = 0; 821.5Sdrochner 831.1Sthorpej /* 841.1Sthorpej * On first open, do netif open, mount, etc. 851.1Sthorpej */ 861.1Sthorpej if (open_count == 0) { 871.1Sthorpej /* Find network interface. */ 881.5Sdrochner if ((netdev_sock = netif_of_open(op)) < 0) { 891.1Sthorpej error = errno; 901.1Sthorpej goto bad; 911.1Sthorpej } 921.1Sthorpej if ((error = net_mountroot()) != 0) 931.1Sthorpej goto bad; 941.1Sthorpej } 951.1Sthorpej open_count++; 961.1Sthorpejbad: 971.1Sthorpej if (netdev_sock >= 0 && open_count == 0) { 981.5Sdrochner netif_of_close(netdev_sock); 991.1Sthorpej netdev_sock = -1; 1001.1Sthorpej } 1011.1Sthorpej return error; 1021.1Sthorpej} 1031.1Sthorpej 1041.1Sthorpejint 1051.1Sthorpejnet_close(op) 1061.1Sthorpej struct of_dev *op; 1071.1Sthorpej{ 1081.1Sthorpej /* 1091.1Sthorpej * On last close, do netif close, etc. 1101.1Sthorpej */ 1111.1Sthorpej if (open_count > 0) 1121.1Sthorpej if (--open_count == 0) { 1131.5Sdrochner netif_of_close(netdev_sock); 1141.1Sthorpej netdev_sock = -1; 1151.1Sthorpej } 1161.1Sthorpej} 1171.1Sthorpej 1181.1Sthorpejint 1191.1Sthorpejnet_mountroot() 1201.1Sthorpej{ 1211.1Sthorpej 1221.1Sthorpej#ifdef DEBUG 1231.1Sthorpej printf("net_mountroot\n"); 1241.1Sthorpej#endif 1251.5Sdrochner 1261.1Sthorpej /* 1271.1Sthorpej * Get info for NFS boot: our IP address, out hostname, 1281.1Sthorpej * server IP address, and our root path on the server. 1291.1Sthorpej * We use BOOTP (RFC951, RFC1532) exclusively as mandated 1301.1Sthorpej * by PowerPC Reference Platform Specification I.4.2 1311.1Sthorpej */ 1321.5Sdrochner 1331.1Sthorpej bootp(netdev_sock); 1341.5Sdrochner 1351.1Sthorpej if (myip.s_addr == 0) 1361.1Sthorpej return ETIMEDOUT; 1371.5Sdrochner 1381.1Sthorpej printf("Using IP address: %s\n", inet_ntoa(myip)); 1391.5Sdrochner 1401.1Sthorpej#ifdef DEBUG 1411.1Sthorpej printf("myip: %s (%s)", hostname, inet_ntoa(myip)); 1421.1Sthorpej if (gateip.s_addr) 1431.1Sthorpej printf(", gateip: %s", inet_ntoa(gateip)); 1441.1Sthorpej if (netmask) 1451.1Sthorpej printf(", netmask: %s", intoa(netmask)); 1461.1Sthorpej printf("\n"); 1471.1Sthorpej#endif 1481.1Sthorpej printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath); 1491.5Sdrochner 1501.1Sthorpej /* 1511.1Sthorpej * Get the NFS file handle (mount). 1521.1Sthorpej */ 1531.1Sthorpej if (nfs_mount(netdev_sock, rootip, rootpath) < 0) 1541.1Sthorpej return errno; 1551.1Sthorpej return 0; 1561.1Sthorpej} 157