net.c revision 1.4
11.4Sdrochner/* $NetBSD: net.c,v 1.4 1999/05/07 16:19:27 drochner 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.3Spk#include <lib/libsa/nfs.h> 621.4Sdrochner 631.4Sdrochner#include <lib/libkern/libkern.h> 641.3Spk 651.3Spk#include <sparc/stand/common/promdev.h> 661.1Smrg 671.1Smrgchar rootpath[FNAME_SIZE]; 681.1Smrg 691.1Smrgint netdev_sock = -1; 701.1Smrgstatic int open_count; 711.1Smrg 721.1Smrg/* 731.1Smrg * Called by devopen after it sets f->f_dev to our devsw entry. 741.1Smrg * This opens the low-level device and sets f->f_devdata. 751.1Smrg */ 761.1Smrgint 771.1Smrgnet_open(pd) 781.1Smrg struct promdata *pd; 791.1Smrg{ 801.1Smrg int error = 0; 811.1Smrg 821.1Smrg /* On first open, do netif open, mount, etc. */ 831.1Smrg if (open_count == 0) { 841.1Smrg /* Find network interface. */ 851.1Smrg if ((netdev_sock = netif_open(pd)) < 0) { 861.1Smrg error = errno; 871.1Smrg goto bad; 881.1Smrg } 891.1Smrg if ((error = net_mountroot()) != 0) 901.1Smrg goto bad; 911.1Smrg } 921.1Smrg open_count++; 931.1Smrgbad: 941.1Smrg return (error); 951.1Smrg} 961.1Smrg 971.1Smrgint 981.1Smrgnet_close(pd) 991.1Smrg struct promdata *pd; 1001.1Smrg{ 1011.1Smrg /* On last close, do netif close, etc. */ 1021.3Spk if (open_count <= 0) 1031.3Spk return (0); 1041.3Spk 1051.3Spk if (--open_count == 0) 1061.3Spk return (netif_close(netdev_sock)); 1071.3Spk 1081.3Spk return (0); 1091.1Smrg} 1101.1Smrg 1111.1Smrgint 1121.1Smrgnet_mountroot() 1131.1Smrg{ 1141.1Smrg 1151.1Smrg#ifdef DEBUG 1161.1Smrg printf("net_mountroot\n"); 1171.1Smrg#endif 1181.1Smrg 1191.1Smrg /* 1201.1Smrg * Get info for NFS boot: our IP address, our hostname, 1211.1Smrg * server IP address, and our root path on the server. 1221.1Smrg * There are two ways to do this: The old, Sun way, 1231.1Smrg * and the more modern, BOOTP way. (RFC951, RFC1048) 1241.1Smrg */ 1251.1Smrg 1261.1Smrg#ifdef SUN_BOOTPARAMS 1271.1Smrg /* Get boot info using RARP and Sun bootparams. */ 1281.1Smrg 1291.1Smrg /* Get our IP address. (rarp.c) */ 1301.1Smrg if (rarp_getipaddress(netdev_sock) == -1) 1311.3Spk return (errno); 1321.1Smrg 1331.1Smrg printf("boot: client IP address: %s\n", inet_ntoa(myip)); 1341.1Smrg 1351.1Smrg /* Get our hostname, server IP address. */ 1361.1Smrg if (bp_whoami(netdev_sock)) 1371.1Smrg return (errno); 1381.1Smrg 1391.1Smrg printf("boot: client name: %s\n", hostname); 1401.1Smrg 1411.1Smrg /* Get the root pathname. */ 1421.1Smrg if (bp_getfile(netdev_sock, "root", &rootip, rootpath)) 1431.1Smrg return (errno); 1441.1Smrg 1451.1Smrg#else 1461.1Smrg 1471.1Smrg /* Get boot info using BOOTP way. (RFC951, RFC1048) */ 1481.1Smrg bootp(netdev_sock); 1491.1Smrg 1501.1Smrg printf("Using IP address: %s\n", inet_ntoa(myip)); 1511.1Smrg 1521.1Smrg printf("myip: %s (%s)", hostname, inet_ntoa(myip)); 1531.1Smrg if (gateip) 1541.1Smrg printf(", gateip: %s", inet_ntoa(gateip)); 1551.1Smrg if (netmask) 1561.1Smrg printf(", netmask: %s", intoa(netmask)); 1571.1Smrg printf("\n"); 1581.1Smrg 1591.1Smrg#endif 1601.1Smrg 1611.1Smrg printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath); 1621.1Smrg 1631.1Smrg /* Get the NFS file handle (mount). */ 1641.1Smrg if (nfs_mount(netdev_sock, rootip, rootpath) != 0) 1651.1Smrg return (errno); 1661.3Spk 1671.3Spk return (0); 1681.1Smrg} 169