1 /* $NetBSD: dev_net.c,v 1.12 1997/12/10 20:38:37 gwr Exp $ */
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
40 * This module implements a "raw device" interface suitable for
41 * use by the stand-alone I/O library NFS code. This interface
42 * does not support any "block" access, and exists only for the
43 * purpose of initializing the network interface, getting boot
44 * parameters, and performing the NFS mount.
46 * At open time, this does:
48 * find interface - netif_open()
49 * RARP for IP address - rarp_getipaddress()
50 * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...)
51 * RPC/mountd - nfs_mount(sock, ip, path)
53 * the root file handle from mountd is saved in a global
54 * for use by the NFS open code (NFS/lookup).
57 #include <machine/stdarg.h>
58 #include <sys/param.h>
59 #include <sys/socket.h>
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
68 #include "bootparam.h"
71 static int netdev_sock = -1;
72 static int netdev_opens;
74 static int net_getparams(int sock);
76 static void net_print(int verbose);
78 struct devsw netdev = {
90 * Print stuff about our net 'device'.
93 net_print(int verbose)
95 pager_output(" net: network interface");
96 /* XXX much more verbose stuff here */
100 * Called by devopen after it sets f->f_dev to our devsw entry.
101 * This opens the low-level device and sets f->f_devdata.
102 * This is declared with variable arguments...
105 net_open(struct open_file *f, void *vdev)
107 char *devname; /* Device part of file name (or NULL). */
114 printf("net_open: %s\n", devname);
117 /* On first open, do netif open, mount, etc. */
118 if (netdev_opens == 0) {
119 /* Find network interface. */
120 if (netdev_sock < 0) {
121 netdev_sock = netif_open(devname);
122 if (netdev_sock < 0) {
123 printf("net_open: netif_open() failed\n");
127 printf("net_open: netif_open() succeeded\n");
129 if (rootip.s_addr == 0) {
130 /* Get root IP address, and path, etc. */
131 error = net_getparams(netdev_sock);
133 /* getparams makes its own noise */
134 netif_close(netdev_sock);
151 printf("net_close: opens=%d\n", netdev_opens);
154 /* On last close, do netif close, etc. */
156 /* Extra close call? */
157 if (netdev_opens <= 0)
160 /* Not last close? */
161 if (netdev_opens > 0)
164 if (netdev_sock >= 0) {
166 printf("net_close: calling netif_close()\n");
167 netif_close(netdev_sock);
187 * Get info for NFS boot: our IP address, our hostname,
188 * server IP address, and our root path on the server.
189 * There are two ways to do this: The old, Sun way,
190 * and the more modern, BOOTP way. (RFC951, RFC1048)
192 * The default is to use the Sun bootparams RPC
193 * (because that is what the kernel will do).
194 * MD code can make try_bootp initialied data,
195 * which will override this common definition.
206 char buf[MAXHOSTNAMELEN];
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.
218 if (myip.s_addr != 0)
221 printf("net_open: BOOTP failed, trying RARP/RPC...\n");
225 * Use RARP to get our IP address. This also sets our
226 * netmask to the "natural" default for our address.
228 if (rarp_getipaddress(sock)) {
229 printf("net_open: RARP failed\n");
232 printf("net_open: client addr: %s\n", inet_ntoa(myip));
234 /* Get our hostname, server IP address, gateway. */
235 if (bp_whoami(sock)) {
236 printf("net_open: bootparam/whoami RPC failed\n");
239 printf("net_open: client name: %s\n", hostname);
242 * Ignore the gateway from whoami (unreliable).
243 * Use the "gateway" parameter instead.
247 if (bp_getfile(sock, "gateway", &gateip, buf) == 0) {
248 /* Got it! Parse the netmask. */
249 smask = ip_convertaddr(buf);
253 printf("net_open: subnet mask: %s\n", intoa(netmask));
256 printf("net_open: net gateway: %s\n", inet_ntoa(gateip));
258 /* Get the root server and pathname. */
259 if (bp_getfile(sock, "root", &rootip, rootpath)) {
260 printf("net_open: bootparam/getfile RPC failed\n");
264 printf("net_open: server addr: %s\n", inet_ntoa(rootip));
265 printf("net_open: server path: %s\n", rootpath);