net.c revision 1.5
11.5Spk/* $NetBSD: net.c,v 1.5 2000/01/22 12:34:57 pk 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 * 3. The name of the author may not be used to endorse or promote products 161.1Smrg * derived from this software without specific prior written permission. 171.1Smrg * 4. All advertising materials mentioning features or use of this software 181.1Smrg * must display the following acknowledgement: 191.1Smrg * This product includes software developed by Gordon W. Ross 201.1Smrg * 211.1Smrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 221.1Smrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 231.1Smrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 241.1Smrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 251.1Smrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 261.1Smrg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 271.1Smrg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 281.1Smrg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 291.1Smrg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 301.1Smrg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 311.1Smrg */ 321.1Smrg 331.1Smrg/* 341.1Smrg * This module implements a "raw device" interface suitable for 351.1Smrg * use by the stand-alone I/O library NFS code. This interface 361.1Smrg * does not support any "block" access, and exists only for the 371.1Smrg * purpose of initializing the network interface, getting boot 381.1Smrg * parameters, and performing the NFS mount. 391.1Smrg * 401.1Smrg * At open time, this does: 411.1Smrg * 421.1Smrg * find interface - netif_open() 431.1Smrg * RARP for IP address - rarp_getipaddress() 441.1Smrg * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...) 451.1Smrg * RPC/mountd - nfs_mount(sock, ip, path) 461.1Smrg * 471.1Smrg * the root file handle from mountd is saved in a global 481.1Smrg * for use by the NFS open code (NFS/lookup). 491.1Smrg */ 501.1Smrg 511.1Smrg#include <sys/param.h> 521.1Smrg#include <sys/socket.h> 531.1Smrg#include <net/if.h> 541.1Smrg#include <netinet/in.h> 551.1Smrg#include <netinet/in_systm.h> 561.1Smrg 571.1Smrg#include <lib/libsa/stand.h> 581.1Smrg#include <lib/libsa/net.h> 591.1Smrg#include <lib/libsa/netif.h> 601.1Smrg#include <lib/libsa/bootparam.h> 611.5Spk#include <lib/libsa/bootp.h> 621.3Spk#include <lib/libsa/nfs.h> 631.4Sdrochner 641.4Sdrochner#include <lib/libkern/libkern.h> 651.3Spk 661.3Spk#include <sparc/stand/common/promdev.h> 671.1Smrg 681.1Smrgchar rootpath[FNAME_SIZE]; 691.1Smrg 701.1Smrgint netdev_sock = -1; 711.1Smrgstatic int open_count; 721.1Smrg 731.5Spkstatic int net_mountroot_bootparams __P((void)); 741.5Spkstatic int net_mountroot_bootp __P((void)); 751.5Spk 761.1Smrg/* 771.1Smrg * Called by devopen after it sets f->f_dev to our devsw entry. 781.1Smrg * This opens the low-level device and sets f->f_devdata. 791.1Smrg */ 801.1Smrgint 811.1Smrgnet_open(pd) 821.1Smrg struct promdata *pd; 831.1Smrg{ 841.1Smrg int error = 0; 851.1Smrg 861.1Smrg /* On first open, do netif open, mount, etc. */ 871.1Smrg if (open_count == 0) { 881.1Smrg /* Find network interface. */ 891.1Smrg if ((netdev_sock = netif_open(pd)) < 0) { 901.1Smrg error = errno; 911.1Smrg goto bad; 921.1Smrg } 931.1Smrg if ((error = net_mountroot()) != 0) 941.1Smrg goto bad; 951.1Smrg } 961.1Smrg open_count++; 971.1Smrgbad: 981.1Smrg return (error); 991.1Smrg} 1001.1Smrg 1011.1Smrgint 1021.1Smrgnet_close(pd) 1031.1Smrg struct promdata *pd; 1041.1Smrg{ 1051.1Smrg /* On last close, do netif close, etc. */ 1061.3Spk if (open_count <= 0) 1071.3Spk return (0); 1081.3Spk 1091.3Spk if (--open_count == 0) 1101.3Spk return (netif_close(netdev_sock)); 1111.3Spk 1121.3Spk return (0); 1131.1Smrg} 1141.1Smrg 1151.1Smrgint 1161.5Spknet_mountroot_bootparams() 1171.1Smrg{ 1181.1Smrg /* Get our IP address. (rarp.c) */ 1191.1Smrg if (rarp_getipaddress(netdev_sock) == -1) 1201.3Spk return (errno); 1211.1Smrg 1221.5Spk printf("Using BOOTPARAMS protocol: "); 1231.5Spk printf("ip address: %s", inet_ntoa(myip)); 1241.1Smrg 1251.1Smrg /* Get our hostname, server IP address. */ 1261.1Smrg if (bp_whoami(netdev_sock)) 1271.1Smrg return (errno); 1281.1Smrg 1291.5Spk printf(", hostname: %s\n", hostname); 1301.1Smrg 1311.1Smrg /* Get the root pathname. */ 1321.1Smrg if (bp_getfile(netdev_sock, "root", &rootip, rootpath)) 1331.1Smrg return (errno); 1341.1Smrg 1351.5Spk return (0); 1361.5Spk} 1371.1Smrg 1381.5Spkint 1391.5Spknet_mountroot_bootp() 1401.5Spk{ 1411.1Smrg bootp(netdev_sock); 1421.1Smrg 1431.5Spk if (myip.s_addr == 0) 1441.5Spk return(ENOENT); 1451.1Smrg 1461.5Spk printf("Using BOOTP protocol: "); 1471.5Spk printf("ip address: %s", inet_ntoa(myip)); 1481.5Spk 1491.5Spk if (hostname[0]) 1501.5Spk printf(", hostname: %s", hostname); 1511.1Smrg if (netmask) 1521.1Smrg printf(", netmask: %s", intoa(netmask)); 1531.5Spk if (gateip.s_addr) 1541.5Spk printf(", gateway: %s", inet_ntoa(gateip)); 1551.1Smrg printf("\n"); 1561.1Smrg 1571.5Spk return (0); 1581.5Spk} 1591.5Spk 1601.5Spkint 1611.5Spknet_mountroot() 1621.5Spk{ 1631.5Spk int error; 1641.5Spk 1651.5Spk#ifdef DEBUG 1661.5Spk printf("net_mountroot\n"); 1671.1Smrg#endif 1681.5Spk 1691.5Spk /* 1701.5Spk * Get info for NFS boot: our IP address, our hostname, 1711.5Spk * server IP address, and our root path on the server. 1721.5Spk * There are two ways to do this: The old, Sun way, 1731.5Spk * and the more modern, BOOTP way. (RFC951, RFC1048) 1741.5Spk */ 1751.5Spk 1761.5Spk /* Historically, we've used BOOTPARAMS, so try that first */ 1771.5Spk error = net_mountroot_bootparams(); 1781.5Spk if (error != 0) 1791.5Spk /* Next, try BOOTP */ 1801.5Spk error = net_mountroot_bootp(); 1811.5Spk if (error != 0) 1821.5Spk return (error); 1831.1Smrg 1841.1Smrg printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath); 1851.1Smrg 1861.1Smrg /* Get the NFS file handle (mount). */ 1871.1Smrg if (nfs_mount(netdev_sock, rootip, rootpath) != 0) 1881.1Smrg return (errno); 1891.3Spk 1901.3Spk return (0); 1911.1Smrg} 192