1 /* $NetBSD: netio.c,v 1.13 2009/03/18 17:06:47 cegger Exp $ */ 2 3 /*- 4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1995 Gordon W. Ross 34 * All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. The name of the author may not be used to endorse or promote products 45 * derived from this software without specific prior written permission. 46 * 4. All advertising materials mentioning features or use of this software 47 * must display the following acknowledgement: 48 * This product includes software developed by Gordon W. Ross 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 53 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 59 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 60 */ 61 62 /* 63 * This module implements a "raw device" interface suitable for 64 * use by the stand-alone I/O library NFS code. This interface 65 * does not support any "block" access, and exists only for the 66 * purpose of initializing the network interface, getting boot 67 * parameters, and performing the NFS mount. 68 * 69 * At open time, this does: 70 * 71 * find interface - netif_open() 72 * RARP for IP address - rarp_getipaddress() 73 * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...) 74 * RPC/mountd - nfs_mount(sock, ip, path) 75 * 76 * the root file handle from mountd is saved in a global 77 * for use by the NFS open code (NFS/lookup). 78 */ 79 80 #include <sys/param.h> 81 #include <sys/socket.h> 82 #include <net/if.h> 83 #include <netinet/in.h> 84 #include <netinet/if_ether.h> 85 #include <netinet/in_systm.h> 86 87 #include <lib/libsa/stand.h> 88 #include <lib/libsa/net.h> 89 #include <lib/libsa/netif.h> 90 #include <lib/libsa/bootparam.h> 91 #include <lib/libsa/nfs.h> 92 #include <lib/libsa/bootp.h> 93 94 #include <lib/libkern/libkern.h> 95 96 #include "vaxstand.h" 97 98 static struct iodesc desc; 99 static int inited = 0; 100 101 struct iodesc * 102 socktodesc(sock) 103 { 104 return &desc; 105 } 106 107 int 108 net_devinit(struct open_file *f, struct netif_driver *drv, u_char *eaddr) { 109 static struct netif best_if; 110 struct iodesc *s; 111 int r; 112 113 if (inited) 114 return 0; 115 /* find a free socket */ 116 s = &desc; 117 118 memset(s, 0, sizeof(*s)); 119 best_if.nif_driver = drv; 120 s->io_netif = &best_if; 121 memcpy( s->myea, eaddr, 6); 122 123 /* 124 * Get info for NFS boot: our IP address, our hostname, 125 * server IP address, and our root path on the server. 126 * There are two ways to do this: The old, Sun way, 127 * and the more modern, BOOTP way. (RFC951, RFC1048) 128 */ 129 130 #ifdef SUPPORT_BOOTP 131 132 /* Get boot info using BOOTP way. (RFC951, RFC1048) */ 133 printf("Trying BOOTP\n"); 134 bootp(0); 135 136 if (myip.s_addr) { 137 printf("Using IP address: %s\n", inet_ntoa(myip)); 138 139 printf("myip: %s (%s)\n", hostname, inet_ntoa(myip)); 140 } else 141 142 #endif /* SUPPORT_BOOTP */ 143 { 144 #ifdef SUPPORT_BOOTPARAMS 145 /* Get boot info using RARP and Sun bootparams. */ 146 147 printf("Trying BOOTPARAMS\n"); 148 /* Get our IP address. (rarp.c) */ 149 if (rarp_getipaddress(0) == -1) 150 return (errno); 151 152 printf("boot: client IP address: %s\n", inet_ntoa(myip)); 153 154 /* Get our hostname, server IP address. */ 155 if (bp_whoami(0)) 156 return (errno); 157 158 printf("boot: client name: %s\n", hostname); 159 160 /* Get the root pathname. */ 161 if (bp_getfile(0, "root", &rootip, rootpath)) 162 return (errno); 163 #endif 164 } 165 printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath); 166 f->f_devdata = s; 167 168 /* Get the NFS file handle (mount). */ 169 r = nfs_mount(0, rootip, rootpath); 170 if (r) 171 return r; 172 173 inited = 1; 174 return 0; 175 } 176 177 ssize_t 178 netif_put(struct iodesc *desc, void *pkt, size_t len) 179 { 180 return (*((struct netif*)desc->io_netif)->nif_driver->netif_put) 181 (desc, pkt, len); 182 } 183 184 ssize_t 185 netif_get(struct iodesc *desc, void *pkt, size_t len, saseconds_t timo) 186 { 187 return (*((struct netif*)desc->io_netif)->nif_driver->netif_get) 188 (desc, pkt, len, timo); 189 } 190