Home | History | Annotate | Line # | Download | only in common
dev_net.c revision 1.3.8.1
      1 /*
      2  * $NetBSD: dev_net.c,v 1.3.8.1 2009/04/28 07:34:15 skrll Exp $
      3  */
      4 
      5 /*-
      6  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      7  * All rights reserved.
      8  *
      9  * This code is derived from software contributed to The NetBSD Foundation
     10  * by Gordon W. Ross.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 /* __FBSDID("$FreeBSD: src/sys/boot/common/dev_net.c,v 1.15 2004/07/08 22:35:33 brian Exp $"); */
     36 
     37 /*-
     38  * This module implements a "raw device" interface suitable for
     39  * use by the stand-alone I/O library NFS code.  This interface
     40  * does not support any "block" access, and exists only for the
     41  * purpose of initializing the network interface, getting boot
     42  * parameters, and performing the NFS mount.
     43  *
     44  * At open time, this does:
     45  *
     46  * find interface      - netif_open()
     47  * RARP for IP address - rarp_getipaddress()
     48  * RPC/bootparams      - callrpc(d, RPC_BOOTPARAMS, ...)
     49  * RPC/mountd          - nfs_mount(sock, ip, path)
     50  *
     51  * the root file handle from mountd is saved in a global
     52  * for use by the NFS open code (NFS/lookup).
     53  */
     54 
     55 #include <machine/stdarg.h>
     56 #include <sys/param.h>
     57 #include <sys/socket.h>
     58 #include <net/if.h>
     59 #include <netinet/in.h>
     60 #include <netinet/in_systm.h>
     61 
     62 #include <stand.h>
     63 #include <string.h>
     64 #include <net.h>
     65 #include <netif.h>
     66 #include <bootp.h>
     67 #include <bootparam.h>
     68 
     69 #include "dev_net.h"
     70 #include "bootstrap.h"
     71 
     72 int debug = 0;
     73 
     74 static int netdev_sock = -1;
     75 static int netdev_opens;
     76 
     77 static int	net_init(void);
     78 static int	net_open(struct open_file *, ...);
     79 static int	net_close(struct open_file *);
     80 static int	net_strategy();
     81 static void	net_print(int);
     82 
     83 static int net_getparams(int sock);
     84 
     85 struct devsw netdev = {
     86     "net",
     87     DEVT_NET,
     88     net_init,
     89     net_strategy,
     90     net_open,
     91     net_close,
     92     noioctl,
     93     net_print
     94 };
     95 
     96 int
     97 net_init(void)
     98 {
     99     return 0;
    100 }
    101 
    102 /*
    103  * Called by devopen after it sets f->f_dev to our devsw entry.
    104  * This opens the low-level device and sets f->f_devdata.
    105  * This is declared with variable arguments...
    106  */
    107 int
    108 net_open(struct open_file *f, ...)
    109 {
    110     va_list args;
    111     char *devname;		/* Device part of file name (or NULL). */
    112     int error = 0;
    113 
    114     va_start(args, f);
    115     devname = va_arg(args, char*);
    116     va_end(args);
    117 
    118     /* On first open, do netif open, mount, etc. */
    119     if (netdev_opens == 0) {
    120 	/* Find network interface. */
    121 	if (netdev_sock < 0) {
    122 	    netdev_sock = netif_open(devname);
    123 	    if (netdev_sock < 0) {
    124 		printf("net_open: netif_open() failed\n");
    125 		return (ENXIO);
    126 	    }
    127 	    if (debug)
    128 		printf("net_open: netif_open() succeeded\n");
    129 	}
    130 	if (rootip.s_addr == 0) {
    131 	    /* Get root IP address, and path, etc. */
    132 	    error = net_getparams(netdev_sock);
    133 	    if (error) {
    134 				/* getparams makes its own noise */
    135 		netif_close(netdev_sock);
    136 		netdev_sock = -1;
    137 		return (error);
    138 	    }
    139 	}
    140 	netdev_opens++;
    141     }
    142     netdev_opens++;
    143     f->f_devdata = &netdev_sock;
    144     return (error);
    145 }
    146 
    147 int
    148 net_close(struct open_file *f)
    149 {
    150 
    151 #ifdef	NETIF_DEBUG
    152     if (debug)
    153 	printf("net_close: opens=%d\n", netdev_opens);
    154 #endif
    155 
    156     /* On last close, do netif close, etc. */
    157     f->f_devdata = NULL;
    158     /* Extra close call? */
    159     if (netdev_opens <= 0)
    160 	return (0);
    161     netdev_opens--;
    162     /* Not last close? */
    163     if (netdev_opens > 0)
    164 	return(0);
    165     rootip.s_addr = 0;
    166     if (netdev_sock >= 0) {
    167 	if (debug)
    168 	    printf("net_close: calling netif_close()\n");
    169 	netif_close(netdev_sock);
    170 	netdev_sock = -1;
    171     }
    172     return (0);
    173 }
    174 
    175 int
    176 net_strategy(void)
    177 {
    178     return EIO;
    179 }
    180 
    181 #define SUPPORT_BOOTP
    182 
    183 /*
    184  * Get info for NFS boot: our IP address, our hostname,
    185  * server IP address, and our root path on the server.
    186  * There are two ways to do this:  The old, Sun way,
    187  * and the more modern, BOOTP way. (RFC951, RFC1048)
    188  *
    189  * The default is to use the Sun bootparams RPC
    190  * (because that is what the kernel will do).
    191  * MD code can make try_bootp initialied data,
    192  * which will override this common definition.
    193  */
    194 #ifdef	SUPPORT_BOOTP
    195 int try_bootp = 1;
    196 #endif
    197 
    198 extern n_long ip_convertaddr(char *p);
    199 
    200 static int
    201 net_getparams(int sock)
    202 {
    203     char buf[MAXHOSTNAMELEN];
    204     char temp[FNAME_SIZE];
    205     struct iodesc *d;
    206     int i;
    207     n_long smask;
    208 
    209 #ifdef	SUPPORT_BOOTP
    210     /*
    211      * Try to get boot info using BOOTP.  If we succeed, then
    212      * the server IP address, gateway, and root path will all
    213      * be initialized.  If any remain uninitialized, we will
    214      * use RARP and RPC/bootparam (the Sun way) to get them.
    215      */
    216     if (try_bootp)
    217 	bootp(sock, BOOTP_NONE);
    218     if (myip.s_addr != 0)
    219 	goto exit;
    220     if (debug)
    221 	printf("net_open: BOOTP failed, trying RARP/RPC...\n");
    222 #endif
    223 
    224     /*
    225      * Use RARP to get our IP address.  This also sets our
    226      * netmask to the "natural" default for our address.
    227      */
    228     if (rarp_getipaddress(sock)) {
    229 	printf("net_open: RARP failed\n");
    230 	return (EIO);
    231     }
    232     printf("net_open: client addr: %s\n", inet_ntoa(myip));
    233 
    234     /* Get our hostname, server IP address, gateway. */
    235     if (bp_whoami(sock)) {
    236 	printf("net_open: bootparam/whoami RPC failed\n");
    237 	return (EIO);
    238     }
    239     printf("net_open: client name: %s\n", hostname);
    240 
    241     /*
    242      * Ignore the gateway from whoami (unreliable).
    243      * Use the "gateway" parameter instead.
    244      */
    245     smask = 0;
    246     gateip.s_addr = 0;
    247     if (bp_getfile(sock, "gateway", &gateip, buf) == 0) {
    248 	/* Got it!  Parse the netmask. */
    249 	smask = ip_convertaddr(buf);
    250     }
    251     if (smask) {
    252 	netmask = smask;
    253 	printf("net_open: subnet mask: %s\n", intoa(netmask));
    254     }
    255     if (gateip.s_addr)
    256 	printf("net_open: net gateway: %s\n", inet_ntoa(gateip));
    257 
    258     /* Get the root server and pathname. */
    259     if (bp_getfile(sock, "root", &rootip, rootpath)) {
    260 	printf("net_open: bootparam/getfile RPC failed\n");
    261 	return (EIO);
    262     }
    263  exit:
    264     /*
    265      * If present, strip the server's address off of the rootpath
    266      * before passing it along.  This allows us to be compatible with
    267      * the kernel's diskless (BOOTP_NFSROOT) booting conventions
    268      */
    269     for (i = 0; rootpath[i] != '\0' && i < FNAME_SIZE; i++)
    270 	    if (rootpath[i] == ':')
    271 		    break;
    272     if (i && i != FNAME_SIZE && rootpath[i] == ':') {
    273 	    rootpath[i++] = '\0';
    274 	    if (inet_addr(&rootpath[0]) != INADDR_NONE)
    275 		    rootip.s_addr = inet_addr(&rootpath[0]);
    276 	    memcpy( &temp[0], &rootpath[i], strlen(&rootpath[i])+1);
    277 	    memcpy( &rootpath[0], &temp[0], strlen(&rootpath[i])+1);
    278     }
    279     printf("net_open: server addr: %s\n", inet_ntoa(rootip));
    280     printf("net_open: server path: %s\n", rootpath);
    281 
    282     d = socktodesc(sock);
    283     sprintf(temp, "%6D", d->myea, ":");
    284     setenv("boot.netif.ip", inet_ntoa(myip), 1);
    285     setenv("boot.netif.netmask", intoa(netmask), 1);
    286     setenv("boot.netif.gateway", inet_ntoa(gateip), 1);
    287     setenv("boot.netif.hwaddr", temp, 1);
    288     setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
    289     setenv("boot.nfsroot.path", rootpath, 1);
    290 
    291     return (0);
    292 }
    293 
    294 static void
    295 net_print(int verbose)
    296 {
    297     return;
    298 }
    299