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