1 /* $OpenBSD: dev_net.c,v 1.4 2014/07/13 15:31:20 mpi 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 59 #include <lib/libsa/stand.h> 60 #include <lib/libsa/net.h> 61 #include <lib/libsa/netif.h> 62 #include <lib/libsa/bootparam.h> 63 #include "dev_net.h" 64 65 extern int debug; 66 extern int nfs_root_node[]; /* XXX - get from nfs_mount() */ 67 68 /* 69 * Various globals needed by the network code: 70 */ 71 72 #if 0 73 /* for arp.c, rarp.c */ 74 u_char bcea[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 75 #endif 76 77 struct in_addr myip; /* my ip address */ 78 struct in_addr rootip; /* root ip address */ 79 struct in_addr gateip; /* swap ip address */ 80 u_int32_t netmask; /* subnet or net mask */ 81 82 char rootpath[FNAME_SIZE]; 83 84 int hostnamelen; 85 char hostname[FNAME_SIZE]; 86 87 int domainnamelen; 88 char domainname[FNAME_SIZE]; 89 90 /* 91 * Local things... 92 */ 93 static int netdev_sock = -1; 94 static int netdev_opens; 95 96 /* 97 * Called by devopen after it sets f->f_dev to our devsw entry. 98 * This opens the low-level device and sets f->f_devdata. 99 * This is declared with variable arguments... 100 */ 101 int 102 net_open(struct open_file *f, ...) 103 { 104 va_list ap; 105 char *devname; /* Device part of file name (or NULL). */ 106 int error = 0; 107 108 va_start(ap, f); 109 devname = va_arg(ap, char *); 110 va_end(ap); 111 112 #ifdef NETIF_DEBUG 113 if (debug) 114 printf("net_open: %s\n", devname); 115 #endif 116 117 /* On first open, do netif open, mount, etc. */ 118 if (netdev_opens == 0) { 119 /* Find network interface. */ 120 if (netdev_sock < 0) { 121 netdev_sock = netif_open(devname); 122 if (netdev_sock < 0) { 123 printf("net_open: netif_open() failed\n"); 124 return (ENXIO); 125 } 126 if (debug) 127 printf("net_open: netif_open() succeeded\n"); 128 } 129 if (rootip.s_addr == 0) { 130 /* Get root IP address, and path, etc. */ 131 error = net_getparams(netdev_sock); 132 if (error) { 133 /* getparams makes its own noise */ 134 goto fail; 135 } 136 /* Get the NFS file handle (mountd). */ 137 error = nfs_mount(netdev_sock, rootip, rootpath); 138 if (error) { 139 printf("net_open: NFS mount error=%d\n", error); 140 rootip.s_addr = 0; 141 fail: 142 netif_close(netdev_sock); 143 netdev_sock = -1; 144 return (error); 145 } 146 if (debug) 147 printf("net_open: NFS mount succeeded\n"); 148 } 149 } 150 netdev_opens++; 151 f->f_devdata = nfs_root_node; 152 return (error); 153 } 154 155 int 156 net_close(f) 157 struct open_file *f; 158 { 159 160 #ifdef NETIF_DEBUG 161 if (debug) 162 printf("net_close: opens=%d\n", netdev_opens); 163 #endif 164 165 /* On last close, do netif close, etc. */ 166 f->f_devdata = NULL; 167 /* Extra close call? */ 168 if (netdev_opens <= 0) 169 return (0); 170 netdev_opens--; 171 /* Not last close? */ 172 if (netdev_opens > 0) 173 return(0); 174 rootip.s_addr = 0; 175 if (netdev_sock >= 0) { 176 if (debug) 177 printf("net_close: calling netif_close()\n"); 178 netif_close(netdev_sock); 179 netdev_sock = -1; 180 } 181 return (0); 182 } 183 184 int 185 net_ioctl() 186 { 187 return EIO; 188 } 189 190 int 191 net_strategy() 192 { 193 return EIO; 194 } 195 196 int 197 net_getparams(sock) 198 int sock; 199 { 200 /* 201 * Get info for NFS boot: our IP address, our hostname, 202 * server IP address, and our root path on the server. 203 * There are two ways to do this: The old, Sun way, 204 * and the more modern, BOOTP way. (RFC951, RFC1048) 205 */ 206 207 #ifdef SUN_BOOTPARAMS 208 /* Get our IP address. (rarp.c) */ 209 if (rarp_getipaddress(sock)) { 210 printf("net_open: RARP failed\n"); 211 return (EIO); 212 } 213 #else /* BOOTPARAMS */ 214 /* 215 * Get boot info using BOOTP. (RFC951, RFC1048) 216 * This also gets the server IP address, gateway, 217 * root path, etc. 218 */ 219 bootp(sock); 220 if (myip.s_addr == 0) { 221 printf("net_open: BOOTP failed\n"); 222 return (EIO); 223 } 224 #endif /* BOOTPARAMS */ 225 226 printf("boot: client addr: %s\n", inet_ntoa(myip)); 227 228 #ifdef SUN_BOOTPARAMS 229 /* Get our hostname, server IP address, gateway. */ 230 if (bp_whoami(sock)) { 231 printf("net_open: bootparam/whoami RPC failed\n"); 232 return (EIO); 233 } 234 #endif /* BOOTPARAMS */ 235 236 printf("boot: client name: %s\n", hostname); 237 if (gateip.s_addr) { 238 printf("boot: subnet mask: %s\n", intoa(netmask)); 239 printf("boot: net gateway: %s\n", inet_ntoa(gateip)); 240 } 241 242 #ifdef SUN_BOOTPARAMS 243 /* Get the root pathname. */ 244 if (bp_getfile(sock, "root", &rootip, rootpath)) { 245 printf("net_open: bootparam/getfile RPC failed\n"); 246 return (EIO); 247 } 248 #endif /* BOOTPARAMS */ 249 250 printf("boot: server addr: %s\n", inet_ntoa(rootip)); 251 printf("boot: server path: %s\n", rootpath); 252 253 return (0); 254 } 255