net.c revision 1.4
11.4Sdrochner/* $NetBSD: net.c,v 1.4 1999/05/07 16:19:28 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.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.4Sdrochner 641.4Sdrochner#include <lib/libkern/libkern.h> 651.1Sthorpej 661.1Sthorpejchar rootpath[FNAME_SIZE]; 671.1Sthorpej 681.1Sthorpejstatic int netdev_sock = -1; 691.1Sthorpejstatic int open_count; 701.1Sthorpej 711.1Sthorpej/* 721.1Sthorpej * Called by devopen after it sets f->f_dev to our devsw entry. 731.1Sthorpej * This opens the low-level device and sets f->f_devdata. 741.1Sthorpej */ 751.1Sthorpejint 761.1Sthorpejnet_open(op) 771.1Sthorpej struct of_dev *op; 781.1Sthorpej{ 791.1Sthorpej int error = 0; 801.1Sthorpej 811.1Sthorpej /* 821.1Sthorpej * On first open, do netif open, mount, etc. 831.1Sthorpej */ 841.1Sthorpej if (open_count == 0) { 851.1Sthorpej /* Find network interface. */ 861.1Sthorpej if ((netdev_sock = netif_open(op)) < 0) { 871.1Sthorpej error = errno; 881.1Sthorpej goto bad; 891.1Sthorpej } 901.1Sthorpej if ((error = net_mountroot()) != 0) 911.1Sthorpej goto bad; 921.1Sthorpej } 931.1Sthorpej open_count++; 941.1Sthorpejbad: 951.1Sthorpej if (netdev_sock >= 0 && open_count == 0) { 961.1Sthorpej netif_close(netdev_sock); 971.1Sthorpej netdev_sock = -1; 981.1Sthorpej } 991.1Sthorpej return error; 1001.1Sthorpej} 1011.1Sthorpej 1021.1Sthorpejint 1031.1Sthorpejnet_close(op) 1041.1Sthorpej struct of_dev *op; 1051.1Sthorpej{ 1061.1Sthorpej /* 1071.1Sthorpej * On last close, do netif close, etc. 1081.1Sthorpej */ 1091.1Sthorpej if (open_count > 0) 1101.1Sthorpej if (--open_count == 0) { 1111.1Sthorpej netif_close(netdev_sock); 1121.1Sthorpej netdev_sock = -1; 1131.1Sthorpej } 1141.1Sthorpej} 1151.1Sthorpej 1161.1Sthorpejint 1171.1Sthorpejnet_mountroot() 1181.1Sthorpej{ 1191.1Sthorpej 1201.1Sthorpej#ifdef DEBUG 1211.1Sthorpej printf("net_mountroot\n"); 1221.1Sthorpej#endif 1231.1Sthorpej 1241.1Sthorpej /* 1251.1Sthorpej * Get info for NFS boot: our IP address, out hostname, 1261.1Sthorpej * server IP address, and our root path on the server. 1271.1Sthorpej * We use BOOTP (RFC951, RFC1532) exclusively as mandated 1281.1Sthorpej * by PowerPC Reference Platform Specification I.4.2 1291.1Sthorpej */ 1301.1Sthorpej 1311.1Sthorpej bootp(netdev_sock); 1321.1Sthorpej 1331.1Sthorpej if (myip.s_addr == 0) 1341.1Sthorpej return ETIMEDOUT; 1351.1Sthorpej 1361.1Sthorpej printf("Using IP address: %s\n", inet_ntoa(myip)); 1371.1Sthorpej 1381.1Sthorpej#ifdef DEBUG 1391.1Sthorpej printf("myip: %s (%s)", hostname, inet_ntoa(myip)); 1401.1Sthorpej if (gateip.s_addr) 1411.1Sthorpej printf(", gateip: %s", inet_ntoa(gateip)); 1421.1Sthorpej if (netmask) 1431.1Sthorpej printf(", netmask: %s", intoa(netmask)); 1441.1Sthorpej printf("\n"); 1451.1Sthorpej#endif 1461.1Sthorpej printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath); 1471.1Sthorpej 1481.1Sthorpej /* 1491.1Sthorpej * Get the NFS file handle (mount). 1501.1Sthorpej */ 1511.1Sthorpej if (nfs_mount(netdev_sock, rootip, rootpath) < 0) 1521.1Sthorpej return errno; 1531.1Sthorpej return 0; 1541.1Sthorpej} 155