Home | History | Annotate | Line # | Download | only in netboot
dev_net.c revision 1.12.108.2
      1 /* $NetBSD: dev_net.c,v 1.12.108.2 2010/03/11 15:01:58 yamt Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1995 Gordon W. Ross
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * This module implements a "raw device" interface suitable for
     30  * use by the stand-alone I/O library NFS code.  This interface
     31  * does not support any "block" access, and exists only for the
     32  * purpose of initializing the network interface, getting boot
     33  * parameters, and performing the NFS mount.
     34  *
     35  * At open time, this does:
     36  *
     37  * find interface      - netif_open()
     38  * RARP for IP address - rarp_getipaddress()
     39  * RPC/bootparams      - callrpc(d, RPC_BOOTPARAMS, ...)
     40  * RPC/mountd          - nfs_mount(sock, ip, path)
     41  *
     42  * the root file handle from mountd is saved in a global
     43  * for use by the NFS open code (NFS/lookup).
     44  */
     45 
     46 #include <machine/stdarg.h>
     47 #include <sys/param.h>
     48 #include <sys/socket.h>
     49 #include <net/if.h>
     50 #include <netinet/in.h>
     51 #include <netinet/in_systm.h>
     52 
     53 #include <lib/libsa/stand.h>
     54 #include <lib/libsa/net.h>
     55 #include <lib/libsa/netif.h>
     56 #include <lib/libsa/bootparam.h>
     57 #include <lib/libsa/nfs.h>
     58 
     59 #include <lib/libkern/libkern.h>
     60 
     61 #include "dev_net.h"
     62 
     63 #ifndef SUN_BOOTPARAMS
     64 void bootp(int);
     65 #endif
     66 
     67 extern int debug;
     68 extern int nfs_root_node[];	/* XXX - get from nfs_mount() */
     69 
     70 /*
     71  * Various globals needed by the network code:
     72  */
     73 
     74 #if 0
     75 /* for arp.c, rarp.c */
     76 u_char bcea[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
     77 #endif
     78 
     79 struct	in_addr myip;		/* my ip address */
     80 struct	in_addr rootip;		/* root ip address */
     81 struct	in_addr gateip;		/* swap ip address */
     82 n_long	netmask;		/* subnet or net mask */
     83 
     84 char rootpath[FNAME_SIZE];
     85 char hostname[FNAME_SIZE];
     86 
     87 /*
     88  * Local things...
     89  */
     90 static int netdev_sock = -1;
     91 static int netdev_opens;
     92 
     93 int net_getparams(int);
     94 
     95 /*
     96  * Called by devopen after it sets f->f_dev to our devsw entry.
     97  * This opens the low-level device and sets f->f_devdata.
     98  * This is declared with variable arguments...
     99  */
    100 int
    101 net_open(struct open_file *f, ...)
    102 {
    103 	va_list ap;
    104 	char *devname;		/* Device part of file name (or NULL). */
    105 	int error = 0;
    106 
    107 	va_start(ap, f);
    108 	devname = va_arg(ap, char*);
    109 	va_end(ap);
    110 
    111 #ifdef	NETIF_DEBUG
    112 	if (debug)
    113 		printf("net_open: %s\n", devname);
    114 #endif
    115 
    116 	/* On first open, do netif open, mount, etc. */
    117 	if (netdev_opens == 0) {
    118 		/* Find network interface. */
    119 		if (netdev_sock < 0) {
    120 			netdev_sock = netif_open(devname);
    121 			if (netdev_sock < 0) {
    122 				printf("net_open: netif_open() failed\n");
    123 				return (ENXIO);
    124 			}
    125 			if (debug)
    126 				printf("net_open: netif_open() succeeded\n");
    127 		}
    128 		if (rootip.s_addr == 0) {
    129 			/* Get root IP address, and path, etc. */
    130 			error = net_getparams(netdev_sock);
    131 			if (error) {
    132 				/* getparams makes its own noise */
    133 				goto fail;
    134 			}
    135 			/* Get the NFS file handle (mountd). */
    136 			error = nfs_mount(netdev_sock, rootip, rootpath);
    137 			if (error) {
    138 				error = errno;
    139 				printf("net_open: NFS mount error=%d\n", error);
    140 				rootip.s_addr = 0;
    141 			fail:
    142 				netif_close(netdev_sock);
    143 				netdev_sock = -1;
    144 				return (error);
    145 			}
    146 			if (debug)
    147 				printf("net_open: NFS mount succeeded\n");
    148 		}
    149 	}
    150 	netdev_opens++;
    151 	f->f_devdata = nfs_root_node;
    152 	return (error);
    153 }
    154 
    155 int
    156 net_close(struct open_file *f)
    157 {
    158 
    159 #ifdef	NETIF_DEBUG
    160 	if (debug)
    161 		printf("net_close: opens=%d\n", netdev_opens);
    162 #endif
    163 
    164 	/* On last close, do netif close, etc. */
    165 	f->f_devdata = NULL;
    166 	/* Extra close call? */
    167 	if (netdev_opens <= 0)
    168 		return (0);
    169 	netdev_opens--;
    170 	/* Not last close? */
    171 	if (netdev_opens > 0)
    172 		return(0);
    173 	rootip.s_addr = 0;
    174 	if (netdev_sock >= 0) {
    175 		if (debug)
    176 			printf("net_close: calling netif_close()\n");
    177 		netif_close(netdev_sock);
    178 		netdev_sock = -1;
    179 	}
    180 	return (0);
    181 }
    182 
    183 int
    184 net_ioctl(struct open_file *f, u_long cmd, void *data)
    185 {
    186 	return EIO;
    187 }
    188 
    189 int
    190 net_strategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf, size_t *rsize)
    191 {
    192 	return EIO;
    193 }
    194 
    195 int
    196 net_getparams(int sock)
    197 {
    198 	/*
    199 	 * Get info for NFS boot: our IP address, our hostname,
    200 	 * server IP address, and our root path on the server.
    201 	 * There are two ways to do this:  The old, Sun way,
    202 	 * and the more modern, BOOTP way. (RFC951, RFC1048)
    203 	 */
    204 
    205 #ifdef	SUN_BOOTPARAMS
    206 	/* Get our IP address.  (rarp.c) */
    207 	if (rarp_getipaddress(sock)) {
    208 		printf("net_open: RARP failed\n");
    209 		return (EIO);
    210 	}
    211 #else	/* BOOTPARAMS */
    212 	/*
    213 	 * Get boot info using BOOTP. (RFC951, RFC1048)
    214 	 * This also gets the server IP address, gateway,
    215 	 * root path, etc.
    216 	 */
    217 	bootp(sock);
    218 	if (myip.s_addr == 0) {
    219 		printf("net_open: BOOTP failed\n");
    220 		return (EIO);
    221 	}
    222 #endif	/* BOOTPARAMS */
    223 
    224 	printf("boot: client addr: %s\n", inet_ntoa(myip));
    225 
    226 #ifdef	SUN_BOOTPARAMS
    227 	/* Get our hostname, server IP address, gateway. */
    228 	if (bp_whoami(sock)) {
    229 		printf("net_open: bootparam/whoami RPC failed\n");
    230 		return (EIO);
    231 	}
    232 #endif	/* BOOTPARAMS */
    233 
    234 	printf("boot: client name: %s\n", hostname);
    235 	if (gateip.s_addr) {
    236 		printf("boot: subnet mask: %s\n", intoa(netmask));
    237 		printf("boot: net gateway: %s\n", inet_ntoa(gateip));
    238 	}
    239 
    240 #ifdef	SUN_BOOTPARAMS
    241 	/* Get the root pathname. */
    242 	if (bp_getfile(sock, "root", &rootip, rootpath)) {
    243 		printf("net_open: bootparam/getfile RPC failed\n");
    244 		return (EIO);
    245 	}
    246 #endif	/* BOOTPARAMS */
    247 
    248 	printf("boot: server addr: %s\n", inet_ntoa(rootip));
    249 	printf("boot: server path: %s\n", rootpath);
    250 
    251 	return (0);
    252 }
    253