Home | History | Annotate | Line # | Download | only in ofwboot
net.c revision 1.7.10.1
      1  1.7.10.1       riz /*	$NetBSD: net.c,v 1.7.10.1 2012/07/21 00:04:56 riz Exp $	*/
      2       1.1       mrg 
      3       1.1       mrg /*
      4       1.1       mrg  * Copyright (C) 1995 Wolfgang Solfrank.
      5       1.1       mrg  * Copyright (C) 1995 TooLs GmbH.
      6       1.1       mrg  * All rights reserved.
      7       1.1       mrg  *
      8       1.1       mrg  * Redistribution and use in source and binary forms, with or without
      9       1.1       mrg  * modification, are permitted provided that the following conditions
     10       1.1       mrg  * are met:
     11       1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     12       1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     13       1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1       mrg  *    notice, this list of conditions and the following disclaimer in the
     15       1.1       mrg  *    documentation and/or other materials provided with the distribution.
     16       1.1       mrg  * 3. All advertising materials mentioning features or use of this software
     17       1.1       mrg  *    must display the following acknowledgement:
     18       1.1       mrg  *	This product includes software developed by TooLs GmbH.
     19       1.1       mrg  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     20       1.1       mrg  *    derived from this software without specific prior written permission.
     21       1.1       mrg  *
     22       1.1       mrg  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     23       1.1       mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24       1.1       mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25       1.1       mrg  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26       1.1       mrg  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     27       1.1       mrg  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     28       1.1       mrg  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29       1.1       mrg  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     30       1.1       mrg  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     31       1.1       mrg  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32       1.1       mrg  */
     33       1.1       mrg 
     34       1.1       mrg /*
     35       1.1       mrg  * This module implements a "raw device" interface suitable for
     36       1.1       mrg  * use by the stand-alone I/O library NFS code.  This interface
     37       1.1       mrg  * does not support any "block" access, and exists only for the
     38       1.1       mrg  * purpose of initializing the network interface, getting boot
     39       1.1       mrg  * parameters, and performing the NFS mount.
     40       1.1       mrg  *
     41       1.1       mrg  * At open time, this does:
     42       1.1       mrg  *
     43       1.1       mrg  * find interface	- netif_open()
     44       1.1       mrg  * BOOTP		- bootp()
     45       1.1       mrg  * RPC/mountd		- nfs_mount()
     46       1.1       mrg  *
     47       1.1       mrg  * The root file handle from mountd is saved in a global
     48       1.1       mrg  * for use by the NFS open code (NFS/lookup).
     49       1.1       mrg  *
     50       1.1       mrg  * Note: this is based in part on sys/arch/sparc/stand/net.c
     51       1.1       mrg  */
     52       1.1       mrg 
     53       1.1       mrg #include <sys/param.h>
     54       1.1       mrg #include <sys/socket.h>
     55       1.1       mrg 
     56       1.1       mrg #include <net/if.h>
     57       1.1       mrg #include <netinet/in.h>
     58       1.1       mrg #include <netinet/in_systm.h>
     59       1.1       mrg 
     60       1.1       mrg #include <lib/libsa/stand.h>
     61       1.1       mrg #include <lib/libsa/net.h>
     62       1.1       mrg #include <lib/libsa/netif.h>
     63       1.7   tsutsui #include <lib/libsa/bootp.h>
     64       1.6  christos #include <lib/libsa/bootparam.h>
     65       1.6  christos #include <lib/libsa/nfs.h>
     66       1.1       mrg 
     67       1.1       mrg #include <lib/libkern/libkern.h>
     68       1.1       mrg 
     69       1.3       uwe #include "ofdev.h"
     70       1.7   tsutsui #include "net.h"
     71       1.3       uwe 
     72       1.3       uwe 
     73       1.3       uwe static int net_mountroot_bootparams(void);
     74       1.3       uwe static int net_mountroot_bootp(void);
     75       1.1       mrg 
     76       1.1       mrg char	rootpath[FNAME_SIZE];
     77       1.1       mrg 
     78       1.1       mrg static	int netdev_sock = -1;
     79       1.1       mrg static	int open_count;
     80       1.1       mrg 
     81       1.1       mrg /*
     82       1.1       mrg  * Called by devopen after it sets f->f_dev to our devsw entry.
     83       1.1       mrg  * This opens the low-level device and sets f->f_devdata.
     84       1.1       mrg  */
     85       1.1       mrg int
     86       1.3       uwe net_open(struct of_dev *op)
     87       1.1       mrg {
     88       1.1       mrg 	int error = 0;
     89       1.1       mrg 
     90       1.1       mrg 	/*
     91       1.1       mrg 	 * On first open, do netif open, mount, etc.
     92       1.1       mrg 	 */
     93       1.1       mrg 	if (open_count == 0) {
     94       1.1       mrg 		/* Find network interface. */
     95       1.1       mrg 		if ((netdev_sock = netif_open(op)) < 0) {
     96       1.1       mrg 			error = errno;
     97       1.1       mrg 			goto bad;
     98       1.1       mrg 		}
     99       1.1       mrg 	}
    100       1.1       mrg 	open_count++;
    101       1.1       mrg bad:
    102       1.1       mrg 	if (netdev_sock >= 0 && open_count == 0) {
    103       1.1       mrg 		netif_close(netdev_sock);
    104       1.1       mrg 		netdev_sock = -1;
    105       1.1       mrg 	}
    106       1.1       mrg 	return error;
    107       1.1       mrg }
    108       1.1       mrg 
    109       1.1       mrg int
    110       1.3       uwe net_close(struct of_dev *op)
    111       1.1       mrg {
    112       1.3       uwe 
    113       1.1       mrg 	/*
    114       1.1       mrg 	 * On last close, do netif close, etc.
    115       1.1       mrg 	 */
    116       1.1       mrg 	if (open_count > 0)
    117       1.1       mrg 		if (--open_count == 0) {
    118       1.1       mrg 			netif_close(netdev_sock);
    119       1.1       mrg 			netdev_sock = -1;
    120       1.1       mrg 		}
    121       1.7   tsutsui 	return 0;
    122       1.1       mrg }
    123       1.1       mrg 
    124       1.5       roy static void
    125       1.5       roy net_clear_params(void)
    126       1.5       roy {
    127       1.5       roy 
    128       1.5       roy 	myip.s_addr = 0;
    129       1.5       roy 	netmask = 0;
    130       1.5       roy 	gateip.s_addr = 0;
    131       1.5       roy 	*hostname = '\0';
    132       1.5       roy 	rootip.s_addr = 0;
    133       1.5       roy 	*rootpath = '\0';
    134       1.5       roy }
    135       1.5       roy 
    136       1.1       mrg int
    137       1.3       uwe net_mountroot_bootparams(void)
    138       1.1       mrg {
    139       1.3       uwe 
    140       1.5       roy 	net_clear_params();
    141       1.5       roy 
    142       1.1       mrg 	/* Get our IP address.  (rarp.c) */
    143       1.1       mrg 	if (rarp_getipaddress(netdev_sock) == -1)
    144       1.1       mrg 		return (errno);
    145       1.5       roy 	printf("Using BOOTPARAMS protocol:\n  ip addr=%s\n", inet_ntoa(myip));
    146       1.1       mrg 	if (bp_whoami(netdev_sock))
    147       1.1       mrg 		return (errno);
    148       1.5       roy 	printf("  hostname=%s\n", hostname);
    149       1.1       mrg 	if (bp_getfile(netdev_sock, "root", &rootip, rootpath))
    150       1.1       mrg 		return (errno);
    151       1.1       mrg 
    152       1.1       mrg 	return (0);
    153       1.1       mrg }
    154       1.1       mrg 
    155       1.1       mrg int
    156       1.3       uwe net_mountroot_bootp(void)
    157       1.1       mrg {
    158       1.5       roy 	int attempts;
    159       1.3       uwe 
    160       1.5       roy 	/* We need a few attempts here as some DHCP servers
    161       1.5       roy 	 * require >1 packet and my wireless bridge is always
    162       1.5       roy 	 * in learning mode until the 2nd attempt ... */
    163       1.5       roy 	for (attempts = 0; attempts < 3; attempts++) {
    164       1.5       roy 		net_clear_params();
    165       1.5       roy 		bootp(netdev_sock);
    166       1.5       roy 		if (myip.s_addr != 0)
    167       1.5       roy 			break;
    168       1.5       roy 	}
    169       1.1       mrg 	if (myip.s_addr == 0)
    170       1.1       mrg 		return(ENOENT);
    171       1.1       mrg 
    172       1.5       roy 	printf("Using BOOTP protocol:\n ip addr=%s\n", inet_ntoa(myip));
    173       1.1       mrg 	if (hostname[0])
    174       1.5       roy 		printf("  hostname=%s\n", hostname);
    175       1.1       mrg 	if (netmask)
    176       1.5       roy 		printf("  netmask=%s\n", intoa(netmask));
    177       1.1       mrg 	if (gateip.s_addr)
    178       1.5       roy 		printf("  gateway=%s\n", inet_ntoa(gateip));
    179       1.1       mrg 
    180       1.1       mrg 	return (0);
    181       1.1       mrg }
    182       1.1       mrg 
    183  1.7.10.1       riz /*
    184  1.7.10.1       riz  * libsa's tftp_open expects a pointer to netdev_sock, i.e. an (int *),
    185  1.7.10.1       riz  * in f_devdata, a pointer to which gets handed down from devopen().
    186  1.7.10.1       riz  *
    187  1.7.10.1       riz  * Do not expect booting via different methods to have the same
    188  1.7.10.1       riz  * requirements or semantics.
    189  1.7.10.1       riz  *
    190  1.7.10.1       riz  * net_tftp_bootp uses net_mountroot_bootp because that incidentially does
    191  1.7.10.1       riz  * most of what it needs to do. It of course in no manner actually mounts
    192  1.7.10.1       riz  * anything, all that routine actually does is prepare the socket for the
    193  1.7.10.1       riz  * necessary net access, and print info for the user.
    194  1.7.10.1       riz  */
    195  1.7.10.1       riz 
    196       1.1       mrg int
    197  1.7.10.1       riz net_tftp_bootp(int **sock)
    198       1.4   mlelstv {
    199       1.4   mlelstv 
    200       1.5       roy 	net_mountroot_bootp();
    201       1.4   mlelstv 	if (myip.s_addr == 0)
    202       1.4   mlelstv 		return(ENOENT);
    203       1.4   mlelstv 
    204  1.7.10.1       riz 	*sock = &netdev_sock;
    205       1.4   mlelstv 	return (0);
    206       1.4   mlelstv }
    207       1.4   mlelstv 
    208       1.4   mlelstv int
    209       1.3       uwe net_mountroot(void)
    210       1.1       mrg {
    211       1.1       mrg 	int error;
    212       1.1       mrg 
    213       1.1       mrg #ifdef DEBUG
    214       1.1       mrg 	printf("net_mountroot\n");
    215       1.1       mrg #endif
    216       1.1       mrg 
    217       1.1       mrg 	/*
    218       1.1       mrg 	 * Get info for NFS boot: our IP address, our hostname,
    219       1.1       mrg 	 * server IP address, and our root path on the server.
    220       1.1       mrg 	 * There are two ways to do this:  The old, Sun way,
    221       1.1       mrg 	 * and the more modern, BOOTP way. (RFC951, RFC1048)
    222       1.1       mrg 	 */
    223       1.1       mrg 
    224       1.2     lukem 		/* Try BOOTP first */
    225       1.2     lukem 	error = net_mountroot_bootp();
    226       1.2     lukem 		/* Historically, we've used BOOTPARAMS, so try that next */
    227       1.1       mrg 	if (error != 0)
    228       1.2     lukem 		error = net_mountroot_bootparams();
    229       1.1       mrg 	if (error != 0)
    230       1.1       mrg 		return (error);
    231       1.1       mrg 
    232       1.5       roy 	printf("  root addr=%s\n  path=%s\n", inet_ntoa(rootip), rootpath);
    233       1.1       mrg 
    234       1.1       mrg 	/* Get the NFS file handle (mount). */
    235       1.1       mrg 	if (nfs_mount(netdev_sock, rootip, rootpath) != 0)
    236       1.1       mrg 		return (errno);
    237       1.1       mrg 
    238       1.1       mrg 	return (0);
    239       1.1       mrg }
    240