11.11Smrg/* $NetBSD: net.c,v 1.11 2021/04/12 03:55:40 mrg Exp $ */ 21.1Smrg 31.1Smrg/* 41.1Smrg * Copyright (c) 1995 Gordon W. Ross 51.1Smrg * All rights reserved. 61.1Smrg * 71.1Smrg * Redistribution and use in source and binary forms, with or without 81.1Smrg * modification, are permitted provided that the following conditions 91.1Smrg * are met: 101.1Smrg * 1. Redistributions of source code must retain the above copyright 111.1Smrg * notice, this list of conditions and the following disclaimer. 121.1Smrg * 2. Redistributions in binary form must reproduce the above copyright 131.1Smrg * notice, this list of conditions and the following disclaimer in the 141.1Smrg * documentation and/or other materials provided with the distribution. 151.1Smrg * 161.1Smrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 171.1Smrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 181.1Smrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 191.1Smrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 201.1Smrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 211.1Smrg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 221.1Smrg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 231.1Smrg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 241.1Smrg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 251.1Smrg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 261.1Smrg */ 271.1Smrg 281.1Smrg/* 291.1Smrg * This module implements a "raw device" interface suitable for 301.1Smrg * use by the stand-alone I/O library NFS code. This interface 311.1Smrg * does not support any "block" access, and exists only for the 321.1Smrg * purpose of initializing the network interface, getting boot 331.1Smrg * parameters, and performing the NFS mount. 341.1Smrg * 351.1Smrg * At open time, this does: 361.1Smrg * 371.1Smrg * find interface - netif_open() 381.1Smrg * RARP for IP address - rarp_getipaddress() 391.1Smrg * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...) 401.1Smrg * RPC/mountd - nfs_mount(sock, ip, path) 411.1Smrg * 421.1Smrg * the root file handle from mountd is saved in a global 431.1Smrg * for use by the NFS open code (NFS/lookup). 441.1Smrg */ 451.1Smrg 461.1Smrg#include <sys/param.h> 471.1Smrg#include <sys/socket.h> 481.1Smrg#include <net/if.h> 491.1Smrg#include <netinet/in.h> 501.1Smrg#include <netinet/in_systm.h> 511.1Smrg 521.1Smrg#include <lib/libsa/stand.h> 531.1Smrg#include <lib/libsa/net.h> 541.1Smrg#include <lib/libsa/netif.h> 551.1Smrg#include <lib/libsa/bootparam.h> 561.5Spk#include <lib/libsa/bootp.h> 571.3Spk#include <lib/libsa/nfs.h> 581.4Sdrochner 591.4Sdrochner#include <lib/libkern/libkern.h> 601.3Spk 611.3Spk#include <sparc/stand/common/promdev.h> 621.1Smrg 631.1Smrgint netdev_sock = -1; 641.1Smrgstatic int open_count; 651.1Smrg 661.8Suwestatic int net_mountroot_bootparams(void); 671.8Suwestatic int net_mountroot_bootp(void); 681.5Spk 691.1Smrg/* 701.1Smrg * Called by devopen after it sets f->f_dev to our devsw entry. 711.1Smrg * This opens the low-level device and sets f->f_devdata. 721.1Smrg */ 731.1Smrgint 741.8Suwenet_open(struct promdata *pd) 751.1Smrg{ 761.1Smrg int error = 0; 771.1Smrg 781.1Smrg /* On first open, do netif open, mount, etc. */ 791.1Smrg if (open_count == 0) { 801.1Smrg /* Find network interface. */ 811.1Smrg if ((netdev_sock = netif_open(pd)) < 0) { 821.1Smrg error = errno; 831.1Smrg goto bad; 841.1Smrg } 851.1Smrg if ((error = net_mountroot()) != 0) 861.1Smrg goto bad; 871.1Smrg } 881.1Smrg open_count++; 891.1Smrgbad: 901.1Smrg return (error); 911.1Smrg} 921.1Smrg 931.1Smrgint 941.8Suwenet_close(struct promdata *pd) 951.1Smrg{ 961.1Smrg /* On last close, do netif close, etc. */ 971.3Spk if (open_count <= 0) 981.3Spk return (0); 991.3Spk 1001.3Spk if (--open_count == 0) 1011.3Spk return (netif_close(netdev_sock)); 1021.3Spk 1031.3Spk return (0); 1041.1Smrg} 1051.1Smrg 1061.1Smrgint 1071.8Suwenet_mountroot_bootparams(void) 1081.1Smrg{ 1091.7Spk printf("Trying BOOTPARAMS protocol... "); 1101.7Spk 1111.1Smrg /* Get our IP address. (rarp.c) */ 1121.1Smrg if (rarp_getipaddress(netdev_sock) == -1) 1131.3Spk return (errno); 1141.1Smrg 1151.5Spk printf("ip address: %s", inet_ntoa(myip)); 1161.1Smrg 1171.1Smrg /* Get our hostname, server IP address. */ 1181.1Smrg if (bp_whoami(netdev_sock)) 1191.1Smrg return (errno); 1201.1Smrg 1211.5Spk printf(", hostname: %s\n", hostname); 1221.1Smrg 1231.1Smrg /* Get the root pathname. */ 1241.1Smrg if (bp_getfile(netdev_sock, "root", &rootip, rootpath)) 1251.1Smrg return (errno); 1261.1Smrg 1271.5Spk return (0); 1281.5Spk} 1291.1Smrg 1301.5Spkint 1311.8Suwenet_mountroot_bootp(void) 1321.5Spk{ 1331.7Spk printf("Trying BOOTP protocol... "); 1341.7Spk 1351.1Smrg bootp(netdev_sock); 1361.1Smrg 1371.5Spk if (myip.s_addr == 0) 1381.5Spk return(ENOENT); 1391.1Smrg 1401.5Spk printf("ip address: %s", inet_ntoa(myip)); 1411.5Spk 1421.5Spk if (hostname[0]) 1431.5Spk printf(", hostname: %s", hostname); 1441.1Smrg if (netmask) 1451.1Smrg printf(", netmask: %s", intoa(netmask)); 1461.5Spk if (gateip.s_addr) 1471.5Spk printf(", gateway: %s", inet_ntoa(gateip)); 1481.1Smrg printf("\n"); 1491.1Smrg 1501.5Spk return (0); 1511.5Spk} 1521.5Spk 1531.5Spkint 1541.8Suwenet_mountroot(void) 1551.5Spk{ 1561.5Spk int error; 1571.5Spk 1581.5Spk#ifdef DEBUG 1591.5Spk printf("net_mountroot\n"); 1601.1Smrg#endif 1611.5Spk 1621.5Spk /* 1631.5Spk * Get info for NFS boot: our IP address, our hostname, 1641.5Spk * server IP address, and our root path on the server. 1651.5Spk * There are two ways to do this: The old, Sun way, 1661.5Spk * and the more modern, BOOTP way. (RFC951, RFC1048) 1671.5Spk */ 1681.5Spk 1691.7Spk /* Try BOOTP first */ 1701.6Slukem error = net_mountroot_bootp(); 1711.7Spk /* Historically, we've used BOOTPARAMS, so try that next */ 1721.5Spk if (error != 0) 1731.6Slukem error = net_mountroot_bootparams(); 1741.5Spk if (error != 0) 1751.5Spk return (error); 1761.1Smrg 1771.1Smrg printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath); 1781.1Smrg 1791.1Smrg /* Get the NFS file handle (mount). */ 1801.1Smrg if (nfs_mount(netdev_sock, rootip, rootpath) != 0) 1811.1Smrg return (errno); 1821.3Spk 1831.3Spk return (0); 1841.1Smrg} 185