net.c revision 1.7
1/* $NetBSD: net.c,v 1.7 2011/05/21 15:50:42 tsutsui Exp $ */ 2 3/* 4 * Copyright (C) 1995 Wolfgang Solfrank. 5 * Copyright (C) 1995 TooLs GmbH. 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. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by TooLs GmbH. 19 * 4. The name of TooLs GmbH may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``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 TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 31 * 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 * BOOTP - bootp() 45 * RPC/mountd - nfs_mount() 46 * 47 * The root file handle from mountd is saved in a global 48 * for use by the NFS open code (NFS/lookup). 49 * 50 * Note: this is based in part on sys/arch/sparc/stand/net.c 51 */ 52 53#include <sys/param.h> 54#include <sys/socket.h> 55 56#include <net/if.h> 57#include <netinet/in.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/bootp.h> 64#include <lib/libsa/bootparam.h> 65#include <lib/libsa/nfs.h> 66 67#include <lib/libkern/libkern.h> 68 69#include "ofdev.h" 70#include "net.h" 71 72 73static int net_mountroot_bootparams(void); 74static int net_mountroot_bootp(void); 75 76char rootpath[FNAME_SIZE]; 77 78static int netdev_sock = -1; 79static int open_count; 80 81/* 82 * Called by devopen after it sets f->f_dev to our devsw entry. 83 * This opens the low-level device and sets f->f_devdata. 84 */ 85int 86net_open(struct of_dev *op) 87{ 88 int error = 0; 89 90 /* 91 * On first open, do netif open, mount, etc. 92 */ 93 if (open_count == 0) { 94 /* Find network interface. */ 95 if ((netdev_sock = netif_open(op)) < 0) { 96 error = errno; 97 goto bad; 98 } 99 } 100 open_count++; 101bad: 102 if (netdev_sock >= 0 && open_count == 0) { 103 netif_close(netdev_sock); 104 netdev_sock = -1; 105 } 106 return error; 107} 108 109int 110net_close(struct of_dev *op) 111{ 112 113 /* 114 * On last close, do netif close, etc. 115 */ 116 if (open_count > 0) 117 if (--open_count == 0) { 118 netif_close(netdev_sock); 119 netdev_sock = -1; 120 } 121 return 0; 122} 123 124static void 125net_clear_params(void) 126{ 127 128 myip.s_addr = 0; 129 netmask = 0; 130 gateip.s_addr = 0; 131 *hostname = '\0'; 132 rootip.s_addr = 0; 133 *rootpath = '\0'; 134} 135 136int 137net_mountroot_bootparams(void) 138{ 139 140 net_clear_params(); 141 142 /* Get our IP address. (rarp.c) */ 143 if (rarp_getipaddress(netdev_sock) == -1) 144 return (errno); 145 printf("Using BOOTPARAMS protocol:\n ip addr=%s\n", inet_ntoa(myip)); 146 if (bp_whoami(netdev_sock)) 147 return (errno); 148 printf(" hostname=%s\n", hostname); 149 if (bp_getfile(netdev_sock, "root", &rootip, rootpath)) 150 return (errno); 151 152 return (0); 153} 154 155int 156net_mountroot_bootp(void) 157{ 158 int attempts; 159 160 /* We need a few attempts here as some DHCP servers 161 * require >1 packet and my wireless bridge is always 162 * in learning mode until the 2nd attempt ... */ 163 for (attempts = 0; attempts < 3; attempts++) { 164 net_clear_params(); 165 bootp(netdev_sock); 166 if (myip.s_addr != 0) 167 break; 168 } 169 if (myip.s_addr == 0) 170 return(ENOENT); 171 172 printf("Using BOOTP protocol:\n ip addr=%s\n", inet_ntoa(myip)); 173 if (hostname[0]) 174 printf(" hostname=%s\n", hostname); 175 if (netmask) 176 printf(" netmask=%s\n", intoa(netmask)); 177 if (gateip.s_addr) 178 printf(" gateway=%s\n", inet_ntoa(gateip)); 179 180 return (0); 181} 182 183int 184net_tftp_bootp(struct of_dev *op) 185{ 186 187 net_mountroot_bootp(); 188 if (myip.s_addr == 0) 189 return(ENOENT); 190 191 return (0); 192} 193 194int 195net_mountroot(void) 196{ 197 int error; 198 199#ifdef DEBUG 200 printf("net_mountroot\n"); 201#endif 202 203 /* 204 * Get info for NFS boot: our IP address, our hostname, 205 * server IP address, and our root path on the server. 206 * There are two ways to do this: The old, Sun way, 207 * and the more modern, BOOTP way. (RFC951, RFC1048) 208 */ 209 210 /* Try BOOTP first */ 211 error = net_mountroot_bootp(); 212 /* Historically, we've used BOOTPARAMS, so try that next */ 213 if (error != 0) 214 error = net_mountroot_bootparams(); 215 if (error != 0) 216 return (error); 217 218 printf(" root addr=%s\n path=%s\n", inet_ntoa(rootip), rootpath); 219 220 /* Get the NFS file handle (mount). */ 221 if (nfs_mount(netdev_sock, rootip, rootpath) != 0) 222 return (errno); 223 224 return (0); 225} 226