net.c revision 1.7
11.7Smrg/* $NetBSD: net.c,v 1.7 2021/04/12 03:55:40 mrg 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.1Sthorpejstatic int netdev_sock = -1; 731.1Sthorpejstatic int open_count; 741.1Sthorpej 751.6Saymericstatic int net_mountroot(void); 761.6Saymeric 771.1Sthorpej/* 781.1Sthorpej * Called by devopen after it sets f->f_dev to our devsw entry. 791.1Sthorpej * This opens the low-level device and sets f->f_devdata. 801.1Sthorpej */ 811.1Sthorpejint 821.6Saymericnet_open(struct of_dev *op) 831.1Sthorpej{ 841.1Sthorpej int error = 0; 851.5Sdrochner 861.1Sthorpej /* 871.1Sthorpej * On first open, do netif open, mount, etc. 881.1Sthorpej */ 891.1Sthorpej if (open_count == 0) { 901.1Sthorpej /* Find network interface. */ 911.5Sdrochner if ((netdev_sock = netif_of_open(op)) < 0) { 921.1Sthorpej error = errno; 931.1Sthorpej goto bad; 941.1Sthorpej } 951.1Sthorpej if ((error = net_mountroot()) != 0) 961.1Sthorpej goto bad; 971.1Sthorpej } 981.1Sthorpej open_count++; 991.1Sthorpejbad: 1001.1Sthorpej if (netdev_sock >= 0 && open_count == 0) { 1011.5Sdrochner netif_of_close(netdev_sock); 1021.1Sthorpej netdev_sock = -1; 1031.1Sthorpej } 1041.1Sthorpej return error; 1051.1Sthorpej} 1061.1Sthorpej 1071.1Sthorpejint 1081.6Saymericnet_close(struct of_dev *op) 1091.1Sthorpej{ 1101.1Sthorpej /* 1111.1Sthorpej * On last close, do netif close, etc. 1121.1Sthorpej */ 1131.1Sthorpej if (open_count > 0) 1141.1Sthorpej if (--open_count == 0) { 1151.5Sdrochner netif_of_close(netdev_sock); 1161.1Sthorpej netdev_sock = -1; 1171.1Sthorpej } 1181.6Saymeric 1191.6Saymeric return 0; 1201.1Sthorpej} 1211.1Sthorpej 1221.6Saymericstatic int 1231.6Saymericnet_mountroot(void) 1241.1Sthorpej{ 1251.1Sthorpej 1261.1Sthorpej#ifdef DEBUG 1271.1Sthorpej printf("net_mountroot\n"); 1281.1Sthorpej#endif 1291.5Sdrochner 1301.1Sthorpej /* 1311.1Sthorpej * Get info for NFS boot: our IP address, out hostname, 1321.1Sthorpej * server IP address, and our root path on the server. 1331.1Sthorpej * We use BOOTP (RFC951, RFC1532) exclusively as mandated 1341.1Sthorpej * by PowerPC Reference Platform Specification I.4.2 1351.1Sthorpej */ 1361.5Sdrochner 1371.1Sthorpej bootp(netdev_sock); 1381.5Sdrochner 1391.1Sthorpej if (myip.s_addr == 0) 1401.1Sthorpej return ETIMEDOUT; 1411.5Sdrochner 1421.1Sthorpej printf("Using IP address: %s\n", inet_ntoa(myip)); 1431.5Sdrochner 1441.1Sthorpej#ifdef DEBUG 1451.1Sthorpej printf("myip: %s (%s)", hostname, inet_ntoa(myip)); 1461.1Sthorpej if (gateip.s_addr) 1471.1Sthorpej printf(", gateip: %s", inet_ntoa(gateip)); 1481.1Sthorpej if (netmask) 1491.1Sthorpej printf(", netmask: %s", intoa(netmask)); 1501.1Sthorpej printf("\n"); 1511.1Sthorpej#endif 1521.1Sthorpej printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath); 1531.5Sdrochner 1541.1Sthorpej /* 1551.1Sthorpej * Get the NFS file handle (mount). 1561.1Sthorpej */ 1571.1Sthorpej if (nfs_mount(netdev_sock, rootip, rootpath) < 0) 1581.1Sthorpej return errno; 1591.1Sthorpej return 0; 1601.1Sthorpej} 161