Home | History | Annotate | Line # | Download | only in ofwboot
net.c revision 1.6
      1 /*	$NetBSD: net.c,v 1.6 2011/05/21 15:10:34 christos 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/bootparam.h>
     64 #include <lib/libsa/nfs.h>
     65 
     66 #include <lib/libkern/libkern.h>
     67 
     68 #include "ofdev.h"
     69 
     70 
     71 static int net_mountroot_bootparams(void);
     72 static int net_mountroot_bootp(void);
     73 
     74 char	rootpath[FNAME_SIZE];
     75 
     76 static	int netdev_sock = -1;
     77 static	int open_count;
     78 
     79 /*
     80  * Called by devopen after it sets f->f_dev to our devsw entry.
     81  * This opens the low-level device and sets f->f_devdata.
     82  */
     83 int
     84 net_open(struct of_dev *op)
     85 {
     86 	int error = 0;
     87 
     88 	/*
     89 	 * On first open, do netif open, mount, etc.
     90 	 */
     91 	if (open_count == 0) {
     92 		/* Find network interface. */
     93 		if ((netdev_sock = netif_open(op)) < 0) {
     94 			error = errno;
     95 			goto bad;
     96 		}
     97 	}
     98 	open_count++;
     99 bad:
    100 	if (netdev_sock >= 0 && open_count == 0) {
    101 		netif_close(netdev_sock);
    102 		netdev_sock = -1;
    103 	}
    104 	return error;
    105 }
    106 
    107 int
    108 net_close(struct of_dev *op)
    109 {
    110 
    111 	/*
    112 	 * On last close, do netif close, etc.
    113 	 */
    114 	if (open_count > 0)
    115 		if (--open_count == 0) {
    116 			netif_close(netdev_sock);
    117 			netdev_sock = -1;
    118 		}
    119 }
    120 
    121 static void
    122 net_clear_params(void)
    123 {
    124 
    125 	myip.s_addr = 0;
    126 	netmask = 0;
    127 	gateip.s_addr = 0;
    128 	*hostname = '\0';
    129 	rootip.s_addr = 0;
    130 	*rootpath = '\0';
    131 }
    132 
    133 int
    134 net_mountroot_bootparams(void)
    135 {
    136 
    137 	net_clear_params();
    138 
    139 	/* Get our IP address.  (rarp.c) */
    140 	if (rarp_getipaddress(netdev_sock) == -1)
    141 		return (errno);
    142 	printf("Using BOOTPARAMS protocol:\n  ip addr=%s\n", inet_ntoa(myip));
    143 	if (bp_whoami(netdev_sock))
    144 		return (errno);
    145 	printf("  hostname=%s\n", hostname);
    146 	if (bp_getfile(netdev_sock, "root", &rootip, rootpath))
    147 		return (errno);
    148 
    149 	return (0);
    150 }
    151 
    152 int
    153 net_mountroot_bootp(void)
    154 {
    155 	int attempts;
    156 
    157 	/* We need a few attempts here as some DHCP servers
    158 	 * require >1 packet and my wireless bridge is always
    159 	 * in learning mode until the 2nd attempt ... */
    160 	for (attempts = 0; attempts < 3; attempts++) {
    161 		net_clear_params();
    162 		bootp(netdev_sock);
    163 		if (myip.s_addr != 0)
    164 			break;
    165 	}
    166 	if (myip.s_addr == 0)
    167 		return(ENOENT);
    168 
    169 	printf("Using BOOTP protocol:\n ip addr=%s\n", inet_ntoa(myip));
    170 	if (hostname[0])
    171 		printf("  hostname=%s\n", hostname);
    172 	if (netmask)
    173 		printf("  netmask=%s\n", intoa(netmask));
    174 	if (gateip.s_addr)
    175 		printf("  gateway=%s\n", inet_ntoa(gateip));
    176 
    177 	return (0);
    178 }
    179 
    180 int
    181 net_tftp_bootp(int **sock)
    182 {
    183 
    184 	net_mountroot_bootp();
    185 	if (myip.s_addr == 0)
    186 		return(ENOENT);
    187 
    188 	*sock = &netdev_sock;
    189 	return (0);
    190 }
    191 
    192 int
    193 net_mountroot(void)
    194 {
    195 	int error;
    196 
    197 #ifdef DEBUG
    198 	printf("net_mountroot\n");
    199 #endif
    200 
    201 	/*
    202 	 * Get info for NFS boot: our IP address, our hostname,
    203 	 * server IP address, and our root path on the server.
    204 	 * There are two ways to do this:  The old, Sun way,
    205 	 * and the more modern, BOOTP way. (RFC951, RFC1048)
    206 	 */
    207 
    208 		/* Try BOOTP first */
    209 	error = net_mountroot_bootp();
    210 		/* Historically, we've used BOOTPARAMS, so try that next */
    211 	if (error != 0)
    212 		error = net_mountroot_bootparams();
    213 	if (error != 0)
    214 		return (error);
    215 
    216 	printf("  root addr=%s\n  path=%s\n", inet_ntoa(rootip), rootpath);
    217 
    218 	/* Get the NFS file handle (mount). */
    219 	if (nfs_mount(netdev_sock, rootip, rootpath) != 0)
    220 		return (errno);
    221 
    222 	return (0);
    223 }
    224