1 /* $NetBSD: netio.c,v 1.15 2009/10/26 19:16:58 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 * 45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 55 */ 56 57 /* 58 * This module implements a "raw device" interface suitable for 59 * use by the stand-alone I/O library NFS code. This interface 60 * does not support any "block" access, and exists only for the 61 * purpose of initializing the network interface, getting boot 62 * parameters, and performing the NFS mount. 63 * 64 * At open time, this does: 65 * 66 * find interface - netif_open() 67 * RARP for IP address - rarp_getipaddress() 68 * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...) 69 * RPC/mountd - nfs_mount(sock, ip, path) 70 * 71 * the root file handle from mountd is saved in a global 72 * for use by the NFS open code (NFS/lookup). 73 */ 74 75 #include <sys/param.h> 76 #include <sys/socket.h> 77 #include <net/if.h> 78 #include <netinet/in.h> 79 #include <netinet/if_ether.h> 80 #include <netinet/in_systm.h> 81 82 #include <lib/libsa/stand.h> 83 #include <lib/libsa/net.h> 84 #include <lib/libsa/netif.h> 85 #include <lib/libsa/bootparam.h> 86 #include <lib/libsa/nfs.h> 87 #include <lib/libsa/bootp.h> 88 89 #include <lib/libkern/libkern.h> 90 91 #include "vaxstand.h" 92 93 static struct iodesc desc; 94 static int inited = 0; 95 96 struct iodesc * 97 socktodesc(sock) 98 { 99 return &desc; 100 } 101 102 int 103 net_devinit(struct open_file *f, struct netif_driver *drv, u_char *eaddr) { 104 static struct netif best_if; 105 struct iodesc *s; 106 int r; 107 108 if (inited) 109 return 0; 110 /* find a free socket */ 111 s = &desc; 112 113 memset(s, 0, sizeof(*s)); 114 best_if.nif_driver = drv; 115 s->io_netif = &best_if; 116 memcpy(s->myea, eaddr, 6); 117 118 /* 119 * Get info for NFS boot: our IP address, our hostname, 120 * server IP address, and our root path on the server. 121 * There are two ways to do this: The old, Sun way, 122 * and the more modern, BOOTP way. (RFC951, RFC1048) 123 */ 124 125 #ifdef SUPPORT_BOOTP 126 127 /* Get boot info using BOOTP way. (RFC951, RFC1048) */ 128 printf("Trying BOOTP\n"); 129 bootp(0); 130 131 if (myip.s_addr) { 132 printf("Using IP address: %s\n", inet_ntoa(myip)); 133 134 printf("myip: %s (%s)\n", hostname, inet_ntoa(myip)); 135 } else 136 137 #endif /* SUPPORT_BOOTP */ 138 { 139 #ifdef SUPPORT_BOOTPARAMS 140 /* Get boot info using RARP and Sun bootparams. */ 141 142 printf("Trying BOOTPARAMS\n"); 143 /* Get our IP address. (rarp.c) */ 144 if (rarp_getipaddress(0) == -1) 145 return (errno); 146 147 printf("boot: client IP address: %s\n", inet_ntoa(myip)); 148 149 /* Get our hostname, server IP address. */ 150 if (bp_whoami(0)) 151 return (errno); 152 153 printf("boot: client name: %s\n", hostname); 154 155 /* Get the root pathname. */ 156 if (bp_getfile(0, "root", &rootip, rootpath)) 157 return (errno); 158 #endif 159 } 160 printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath); 161 f->f_devdata = s; 162 163 /* Get the NFS file handle (mount). */ 164 r = nfs_mount(0, rootip, rootpath); 165 if (r) 166 return r; 167 168 inited = 1; 169 return 0; 170 } 171 172 ssize_t 173 netif_put(struct iodesc *desc, void *pkt, size_t len) 174 { 175 return (*((struct netif*)desc->io_netif)->nif_driver->netif_put) 176 (desc, pkt, len); 177 } 178 179 ssize_t 180 netif_get(struct iodesc *desc, void *pkt, size_t len, saseconds_t timo) 181 { 182 return (*((struct netif*)desc->io_netif)->nif_driver->netif_get) 183 (desc, pkt, len, timo); 184 } 185