11.10Smrg/* $NetBSD: net.c,v 1.10 2021/04/12 03:55:40 mrg 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.1Stsubaistatic int netdev_sock = -1; 731.1Stsubaistatic int open_count; 741.1Stsubai 751.5Saymericstatic int net_mountroot(void); 761.5Saymeric 771.1Stsubai/* 781.1Stsubai * Called by devopen after it sets f->f_dev to our devsw entry. 791.1Stsubai * This opens the low-level device and sets f->f_devdata. 801.1Stsubai */ 811.1Stsubaiint 821.5Saymericnet_open(struct of_dev *op) 831.1Stsubai{ 841.1Stsubai int error = 0; 851.3Stsutsui 861.1Stsubai /* 871.1Stsubai * On first open, do netif open, mount, etc. 881.1Stsubai */ 891.1Stsubai if (open_count == 0) { 901.1Stsubai /* Find network interface. */ 911.4Sdrochner if ((netdev_sock = netif_of_open(op)) < 0) { 921.1Stsubai error = errno; 931.1Stsubai goto bad; 941.1Stsubai } 951.1Stsubai if ((error = net_mountroot()) != 0) 961.1Stsubai goto bad; 971.1Stsubai } 981.1Stsubai open_count++; 991.1Stsubaibad: 1001.1Stsubai if (netdev_sock >= 0 && open_count == 0) { 1011.4Sdrochner netif_of_close(netdev_sock); 1021.1Stsubai netdev_sock = -1; 1031.1Stsubai } 1041.1Stsubai return error; 1051.1Stsubai} 1061.1Stsubai 1071.1Stsubaiint 1081.5Saymericnet_close(struct of_dev *op) 1091.1Stsubai{ 1101.1Stsubai /* 1111.1Stsubai * On last close, do netif close, etc. 1121.1Stsubai */ 1131.1Stsubai if (open_count > 0) 1141.1Stsubai if (--open_count == 0) { 1151.4Sdrochner netif_of_close(netdev_sock); 1161.1Stsubai netdev_sock = -1; 1171.1Stsubai } 1181.7Suwe return 0; 1191.1Stsubai} 1201.1Stsubai 1211.5Saymericstatic int 1221.5Saymericnet_mountroot(void) 1231.1Stsubai{ 1241.1Stsubai 1251.1Stsubai#ifdef DEBUG 1261.1Stsubai printf("net_mountroot\n"); 1271.1Stsubai#endif 1281.3Stsutsui 1291.1Stsubai /* 1301.1Stsubai * Get info for NFS boot: our IP address, out hostname, 1311.1Stsubai * server IP address, and our root path on the server. 1321.1Stsubai * We use BOOTP (RFC951, RFC1532) exclusively as mandated 1331.1Stsubai * by PowerPC Reference Platform Specification I.4.2 1341.1Stsubai */ 1351.3Stsutsui 1361.1Stsubai bootp(netdev_sock); 1371.3Stsutsui 1381.1Stsubai if (myip.s_addr == 0) 1391.1Stsubai return ETIMEDOUT; 1401.3Stsutsui 1411.1Stsubai printf("Using IP address: %s\n", inet_ntoa(myip)); 1421.3Stsutsui 1431.1Stsubai#ifdef DEBUG 1441.1Stsubai printf("myip: %s (%s)", hostname, inet_ntoa(myip)); 1451.1Stsubai if (gateip.s_addr) 1461.1Stsubai printf(", gateip: %s", inet_ntoa(gateip)); 1471.1Stsubai if (netmask) 1481.1Stsubai printf(", netmask: %s", intoa(netmask)); 1491.1Stsubai printf("\n"); 1501.1Stsubai#endif 1511.1Stsubai printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath); 1521.3Stsutsui 1531.1Stsubai /* 1541.1Stsubai * Get the NFS file handle (mount). 1551.1Stsubai */ 1561.1Stsubai if (nfs_mount(netdev_sock, rootip, rootpath) < 0) 1571.1Stsubai return errno; 1581.1Stsubai return 0; 1591.1Stsubai} 160