1/* $NetBSD: dev_net.c,v 1.5 2009/10/21 23:12:09 snj Exp $ */ 2 3/* 4 * Copyright (c) 1995 Gordon W. Ross 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28/* 29 * This module implements a "raw device" interface suitable for 30 * use by the stand-alone I/O library NFS code. This interface 31 * does not support any "block" access, and exists only for the 32 * purpose of initializing the network interface, getting boot 33 * parameters, and performing the NFS mount. 34 * 35 * At open time, this does: 36 * 37 * find interface - netif_open() 38 * RARP for IP address - rarp_getipaddress() 39 * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...) 40 * RPC/mountd - nfs_mount(sock, ip, path) 41 * 42 * the root file handle from mountd is saved in a global 43 * for use by the NFS open code (NFS/lookup). 44 */ 45 46#include <sys/param.h> 47#include <sys/socket.h> 48#include <net/if.h> 49#include <netinet/in.h> 50#include <netinet/in_systm.h> 51 52#include <lib/libsa/stand.h> 53#include <lib/libsa/net.h> 54#include <lib/libsa/netif.h> 55#include <lib/libsa/bootparam.h> 56#include <lib/libsa/nfs.h> 57 58#include <lib/libkern/libkern.h> 59 60#include <lib/libsa/dev_net.h> 61 62#ifndef SUN_BOOTPARAMS 63void bootp(int); 64#endif 65 66extern int debug; 67extern int nfs_root_node[]; /* XXX - get from nfs_mount() */ 68 69/* 70 * Various globals needed by the network code: 71 */ 72 73#if 0 74/* for arp.c, rarp.c */ 75u_char bcea[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 76#endif 77 78struct in_addr myip; /* my ip address */ 79struct in_addr rootip; /* root ip address */ 80struct in_addr gateip; /* swap ip address */ 81n_long netmask; /* subnet or net mask */ 82 83char rootpath[FNAME_SIZE]; 84char hostname[FNAME_SIZE]; 85 86/* 87 * Local things... 88 */ 89static int netdev_sock = -1; 90static int netdev_opens; 91 92int net_getparams(int); 93 94/* 95 * Called by devopen after it sets f->f_dev to our devsw entry. 96 * This opens the low-level device and sets f->f_devdata. 97 * This is declared with variable arguments... 98 */ 99int 100net_open(struct open_file *f, ...) 101{ 102 va_list ap; 103 char *devname; /* Device part of file name (or NULL). */ 104 int error = 0; 105 106 va_start(ap, f); 107 devname = va_arg(ap, char*); 108 va_end(ap); 109 110#ifdef NETIF_DEBUG 111 if (debug) 112 printf("net_open: %s\n", devname); 113#endif 114 115 /* On first open, do netif open, mount, etc. */ 116 if (netdev_opens == 0) { 117 /* Find network interface. */ 118 if (netdev_sock < 0) { 119 netdev_sock = netif_open(devname); 120 if (netdev_sock < 0) { 121 printf("net_open: netif_open() failed\n"); 122 return (ENXIO); 123 } 124 if (debug) 125 printf("net_open: netif_open() succeeded\n"); 126 } 127 if (rootip.s_addr == 0) { 128 /* Get root IP address, and path, etc. */ 129 error = net_getparams(netdev_sock); 130 if (error) { 131 /* getparams makes its own noise */ 132 goto fail; 133 } 134 /* Get the NFS file handle (mountd). */ 135 error = nfs_mount(netdev_sock, rootip, rootpath); 136 if (error) { 137 error = errno; 138 printf("net_open: NFS mount error=%d\n", error); 139 rootip.s_addr = 0; 140 fail: 141 netif_close(netdev_sock); 142 netdev_sock = -1; 143 return (error); 144 } 145 if (debug) 146 printf("net_open: NFS mount succeeded\n"); 147 } 148 } 149 netdev_opens++; 150 f->f_devdata = nfs_root_node; 151 return (error); 152} 153 154int 155net_close(struct open_file *f) 156{ 157 158#ifdef NETIF_DEBUG 159 if (debug) 160 printf("net_close: opens=%d\n", netdev_opens); 161#endif 162 163 /* On last close, do netif close, etc. */ 164 f->f_devdata = NULL; 165 /* Extra close call? */ 166 if (netdev_opens <= 0) 167 return (0); 168 netdev_opens--; 169 /* Not last close? */ 170 if (netdev_opens > 0) 171 return(0); 172 rootip.s_addr = 0; 173 if (netdev_sock >= 0) { 174 if (debug) 175 printf("net_close: calling netif_close()\n"); 176 netif_close(netdev_sock); 177 netdev_sock = -1; 178 } 179 return (0); 180} 181 182int 183net_ioctl(struct open_file *a, u_long b, void *c) 184{ 185 return EIO; 186} 187 188int 189net_strategy(void *a, int b, daddr_t c, size_t d, void *e, size_t *f) 190{ 191 return EIO; 192} 193 194int 195net_getparams(int sock) 196{ 197 /* 198 * Get info for NFS boot: our IP address, our hostname, 199 * server IP address, and our root path on the server. 200 * There are two ways to do this: The old, Sun way, 201 * and the more modern, BOOTP way. (RFC951, RFC1048) 202 */ 203 204#ifdef SUN_BOOTPARAMS 205 /* Get our IP address. (rarp.c) */ 206 if (rarp_getipaddress(sock)) { 207 printf("net_open: RARP failed\n"); 208 return (EIO); 209 } 210#else /* BOOTPARAMS */ 211 /* 212 * Get boot info using BOOTP. (RFC951, RFC1048) 213 * This also gets the server IP address, gateway, 214 * root path, etc. 215 */ 216 bootp(sock); 217 if (myip.s_addr == 0) { 218 printf("net_open: BOOTP failed\n"); 219 return (EIO); 220 } 221#endif /* BOOTPARAMS */ 222 223 printf("boot: client addr: %s\n", inet_ntoa(myip)); 224 225#ifdef SUN_BOOTPARAMS 226 /* Get our hostname, server IP address, gateway. */ 227 if (bp_whoami(sock)) { 228 printf("net_open: bootparam/whoami RPC failed\n"); 229 return (EIO); 230 } 231#endif /* BOOTPARAMS */ 232 233 printf("boot: client name: %s\n", hostname); 234 if (gateip.s_addr) { 235 printf("boot: subnet mask: %s\n", intoa(netmask)); 236 printf("boot: net gateway: %s\n", inet_ntoa(gateip)); 237 } 238 239#ifdef SUN_BOOTPARAMS 240 /* Get the root pathname. */ 241 if (bp_getfile(sock, "root", &rootip, rootpath)) { 242 printf("net_open: bootparam/getfile RPC failed\n"); 243 return (EIO); 244 } 245#endif /* BOOTPARAMS */ 246 247 printf("boot: server addr: %s\n", inet_ntoa(rootip)); 248 printf("boot: server path: %s\n", rootpath); 249 250 return (0); 251} 252