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