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