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