net.c revision 1.8
11.8Suwe/* $NetBSD: net.c,v 1.8 2006/07/13 20:03:34 uwe 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.8Suwestatic int net_mountroot_bootparams(void); 741.8Suwestatic int net_mountroot_bootp(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.8Suwenet_open(struct promdata *pd) 821.1Smrg{ 831.1Smrg int error = 0; 841.1Smrg 851.1Smrg /* On first open, do netif open, mount, etc. */ 861.1Smrg if (open_count == 0) { 871.1Smrg /* Find network interface. */ 881.1Smrg if ((netdev_sock = netif_open(pd)) < 0) { 891.1Smrg error = errno; 901.1Smrg goto bad; 911.1Smrg } 921.1Smrg if ((error = net_mountroot()) != 0) 931.1Smrg goto bad; 941.1Smrg } 951.1Smrg open_count++; 961.1Smrgbad: 971.1Smrg return (error); 981.1Smrg} 991.1Smrg 1001.1Smrgint 1011.8Suwenet_close(struct promdata *pd) 1021.1Smrg{ 1031.1Smrg /* On last close, do netif close, etc. */ 1041.3Spk if (open_count <= 0) 1051.3Spk return (0); 1061.3Spk 1071.3Spk if (--open_count == 0) 1081.3Spk return (netif_close(netdev_sock)); 1091.3Spk 1101.3Spk return (0); 1111.1Smrg} 1121.1Smrg 1131.1Smrgint 1141.8Suwenet_mountroot_bootparams(void) 1151.1Smrg{ 1161.7Spk printf("Trying BOOTPARAMS protocol... "); 1171.7Spk 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("ip address: %s", inet_ntoa(myip)); 1231.1Smrg 1241.1Smrg /* Get our hostname, server IP address. */ 1251.1Smrg if (bp_whoami(netdev_sock)) 1261.1Smrg return (errno); 1271.1Smrg 1281.5Spk printf(", hostname: %s\n", hostname); 1291.1Smrg 1301.1Smrg /* Get the root pathname. */ 1311.1Smrg if (bp_getfile(netdev_sock, "root", &rootip, rootpath)) 1321.1Smrg return (errno); 1331.1Smrg 1341.5Spk return (0); 1351.5Spk} 1361.1Smrg 1371.5Spkint 1381.8Suwenet_mountroot_bootp(void) 1391.5Spk{ 1401.7Spk printf("Trying BOOTP protocol... "); 1411.7Spk 1421.1Smrg bootp(netdev_sock); 1431.1Smrg 1441.5Spk if (myip.s_addr == 0) 1451.5Spk return(ENOENT); 1461.1Smrg 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.8Suwenet_mountroot(void) 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.7Spk /* Try BOOTP first */ 1771.6Slukem error = net_mountroot_bootp(); 1781.7Spk /* Historically, we've used BOOTPARAMS, so try that next */ 1791.5Spk if (error != 0) 1801.6Slukem error = net_mountroot_bootparams(); 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