1 /* $NetBSD: dev_net.c,v 1.26 2011/07/17 20:54:52 joerg 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 <sys/param.h>
51 #include <sys/socket.h>
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
56 #include <lib/libkern/libkern.h>
62 #include "bootparam.h"
68 extern int nfs_root_node
[]; /* XXX - get from nfs_mount() */
70 static int netdev_sock
= -1;
71 static int netdev_opens
;
73 static int net_getparams(int);
76 * Called by devopen after it sets f->f_dev to our devsw entry.
77 * This opens the low-level device and sets f->f_devdata.
78 * This is declared with variable arguments...
81 net_open(struct open_file
*f
, ...)
84 char *devname
; /* Device part of file name (or NULL). */
88 devname
= va_arg(ap
, char *);
93 printf("%s\n", devname
);
96 /* On first open, do netif open, mount, etc. */
97 if (netdev_opens
== 0) {
98 /* Find network interface. */
99 if (netdev_sock
< 0) {
100 netdev_sock
= netif_open(devname
);
101 if (netdev_sock
< 0) {
102 printf("netif_open() failed\n");
107 printf("netif_open() succeeded\n");
110 if (rootip
.s_addr
== 0) {
111 /* Get root IP address, and path, etc. */
112 error
= net_getparams(netdev_sock
);
114 /* getparams makes its own noise */
117 /* Get the NFS file handle (mountd). */
118 error
= nfs_mount(netdev_sock
, rootip
, rootpath
);
120 printf("NFS mount error=%d\n", errno
);
123 netif_close(netdev_sock
);
129 printf("NFS mount succeeded\n");
134 f
->f_devdata
= nfs_root_node
;
139 net_close(struct open_file
*f
)
144 printf("net_close: opens=%d\n", netdev_opens
);
147 /* On last close, do netif close, etc. */
149 /* Extra close call? */
150 if (netdev_opens
<= 0)
153 /* Not last close? */
154 if (netdev_opens
> 0)
157 if (netdev_sock
>= 0) {
160 printf("net_close: calling netif_close()\n");
162 netif_close(netdev_sock
);
169 net_ioctl(struct open_file
*f
, u_long cmd
, void *data
)
176 net_strategy(void *devdata
, int rw
, daddr_t blk
, size_t size
, void *buf
,
185 * Get info for NFS boot: our IP address, our hostname,
186 * server IP address, and our root path on the server.
187 * There are two ways to do this: The old, Sun way,
188 * and the more modern, BOOTP way. (RFC951, RFC1048)
190 * The default is to use the Sun bootparams RPC
191 * (because that is what the kernel will do).
192 * MD code can make try_bootp initialied data,
193 * which will override this common definition.
200 net_getparams(int sock
)
202 char buf
[MAXHOSTNAMELEN
];
207 * Try to get boot info using BOOTP. If we succeed, then
208 * the server IP address, gateway, and root path will all
209 * be initialized. If any remain uninitialized, we will
210 * use RARP and RPC/bootparam (the Sun way) to get them.
214 if (myip
.s_addr
!= 0)
218 printf("BOOTP failed, trying RARP/RPC...\n");
223 * Use RARP to get our IP address. This also sets our
224 * netmask to the "natural" default for our address.
226 if (rarp_getipaddress(sock
)) {
227 printf("RARP failed\n");
232 printf("client addr: %s\n", inet_ntoa(myip
));
235 /* Get our hostname, server IP address, gateway. */
236 if (bp_whoami(sock
)) {
237 printf("bootparam/whoami RPC failed\n");
242 printf("client name: %s\n", hostname
);
246 * Ignore the gateway from whoami (unreliable).
247 * Use the "gateway" parameter instead.
251 if (bp_getfile(sock
, "gateway", &gateip
, buf
)) {
252 printf("nfs_open: gateway bootparam missing\n");
254 /* Got it! Parse the netmask. */
255 smask
= inet_addr(buf
);
261 printf("subnet mask: %s\n", intoa(netmask
));
267 printf("net gateway: %s\n", inet_ntoa(gateip
));
270 /* Get the root server and pathname. */
271 if (bp_getfile(sock
, "root", &rootip
, rootpath
)) {
272 printf("bootparam/getfile RPC failed\n");
278 printf("server addr: %s\n", inet_ntoa(rootip
));
279 printf("server path: %s\n", rootpath
);