1 /* $NetBSD: dev_net.c,v 1.23 2008/04/28 20:24:06 martin 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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * This module implements a "raw device" interface suitable for
34 * use by the stand-alone I/O library NFS code. This interface
35 * does not support any "block" access, and exists only for the
36 * purpose of initializing the network interface, getting boot
37 * parameters, and performing the NFS mount.
39 * At open time, this does:
41 * find interface - netif_open()
42 * RARP for IP address - rarp_getipaddress()
43 * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...)
44 * RPC/mountd - nfs_mount(sock, ip, path)
46 * the root file handle from mountd is saved in a global
47 * for use by the NFS open code (NFS/lookup).
50 #include <machine/stdarg.h>
51 #include <sys/param.h>
52 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
57 #include <lib/libkern/libkern.h>
63 #include "bootparam.h"
69 extern int nfs_root_node
[]; /* XXX - get from nfs_mount() */
71 static int netdev_sock
= -1;
72 static int netdev_opens
;
74 static int net_getparams(int);
77 * Called by devopen after it sets f->f_dev to our devsw entry.
78 * This opens the low-level device and sets f->f_devdata.
79 * This is declared with variable arguments...
82 net_open(struct open_file
*f
, ...)
85 char *devname
; /* Device part of file name (or NULL). */
89 devname
= va_arg(ap
, char *);
94 printf("%s\n", devname
);
97 /* On first open, do netif open, mount, etc. */
98 if (netdev_opens
== 0) {
99 /* Find network interface. */
100 if (netdev_sock
< 0) {
101 netdev_sock
= netif_open(devname
);
102 if (netdev_sock
< 0) {
103 printf("netif_open() failed\n");
108 printf("netif_open() succeeded\n");
111 if (rootip
.s_addr
== 0) {
112 /* Get root IP address, and path, etc. */
113 error
= net_getparams(netdev_sock
);
115 /* getparams makes its own noise */
118 /* Get the NFS file handle (mountd). */
119 error
= nfs_mount(netdev_sock
, rootip
, rootpath
);
121 printf("NFS mount error=%d\n", error
);
124 netif_close(netdev_sock
);
130 printf("NFS mount succeeded\n");
135 f
->f_devdata
= nfs_root_node
;
140 net_close(struct open_file
*f
)
145 printf("net_close: opens=%d\n", netdev_opens
);
148 /* On last close, do netif close, etc. */
150 /* Extra close call? */
151 if (netdev_opens
<= 0)
154 /* Not last close? */
155 if (netdev_opens
> 0)
158 if (netdev_sock
>= 0) {
161 printf("net_close: calling netif_close()\n");
163 netif_close(netdev_sock
);
170 net_ioctl(struct open_file
*f
, u_long cmd
, void *data
)
177 net_strategy(void *devdata
, int rw
, daddr_t blk
, size_t size
, void *buf
,
186 * Get info for NFS boot: our IP address, our hostname,
187 * server IP address, and our root path on the server.
188 * There are two ways to do this: The old, Sun way,
189 * and the more modern, BOOTP way. (RFC951, RFC1048)
191 * The default is to use the Sun bootparams RPC
192 * (because that is what the kernel will do).
193 * MD code can make try_bootp initialied data,
194 * which will override this common definition.
201 net_getparams(int sock
)
203 char buf
[MAXHOSTNAMELEN
];
208 * Try to get boot info using BOOTP. If we succeed, then
209 * the server IP address, gateway, and root path will all
210 * be initialized. If any remain uninitialized, we will
211 * use RARP and RPC/bootparam (the Sun way) to get them.
215 if (myip
.s_addr
!= 0)
219 printf("BOOTP failed, trying RARP/RPC...\n");
224 * Use RARP to get our IP address. This also sets our
225 * netmask to the "natural" default for our address.
227 if (rarp_getipaddress(sock
)) {
228 printf("RARP failed\n");
233 printf("client addr: %s\n", inet_ntoa(myip
));
236 /* Get our hostname, server IP address, gateway. */
237 if (bp_whoami(sock
)) {
238 printf("bootparam/whoami RPC failed\n");
243 printf("client name: %s\n", hostname
);
247 * Ignore the gateway from whoami (unreliable).
248 * Use the "gateway" parameter instead.
252 if (bp_getfile(sock
, "gateway", &gateip
, buf
)) {
253 printf("nfs_open: gateway bootparam missing\n");
255 /* Got it! Parse the netmask. */
256 smask
= inet_addr(buf
);
262 printf("subnet mask: %s\n", intoa(netmask
));
268 printf("net gateway: %s\n", inet_ntoa(gateip
));
271 /* Get the root server and pathname. */
272 if (bp_getfile(sock
, "root", &rootip
, rootpath
)) {
273 printf("bootparam/getfile RPC failed\n");
279 printf("server addr: %s\n", inet_ntoa(rootip
));
280 printf("server path: %s\n", rootpath
);