dev_net.c revision 1.19.84.3 1 /* $NetBSD: dev_net.c,v 1.19.84.3 2007/11/27 19:38:28 joerg Exp $ */
2
3 /*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Gordon W. Ross.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
25 *
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.
37 */
38
39 /*
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.
45 *
46 * At open time, this does:
47 *
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)
52 *
53 * the root file handle from mountd is saved in a global
54 * for use by the NFS open code (NFS/lookup).
55 */
56
57 #include <machine/stdarg.h>
58 #include <sys/param.h>
59 #include <sys/socket.h>
60 #include <net/if.h>
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63
64 #include <lib/libkern/libkern.h>
65
66 #include "stand.h"
67 #include "net.h"
68 #include "netif.h"
69 #include "nfs.h"
70 #include "bootparam.h"
71 #include "dev_net.h"
72 #ifdef SUPPORT_BOOTP
73 #include "bootp.h"
74 #endif
75
76 extern int nfs_root_node[]; /* XXX - get from nfs_mount() */
77
78 static int netdev_sock = -1;
79 static int netdev_opens;
80
81 static int net_getparams __P((int));
82
83 /*
84 * Called by devopen after it sets f->f_dev to our devsw entry.
85 * This opens the low-level device and sets f->f_devdata.
86 * This is declared with variable arguments...
87 */
88 int
89 net_open(struct open_file *f, ...)
90 {
91 va_list ap;
92 char *devname; /* Device part of file name (or NULL). */
93 int error = 0;
94
95 va_start(ap, f);
96 devname = va_arg(ap, char *);
97 va_end(ap);
98
99 #ifdef NETIF_DEBUG
100 if (debug)
101 printf("%s\n", devname);
102 #endif
103
104 /* On first open, do netif open, mount, etc. */
105 if (netdev_opens == 0) {
106 /* Find network interface. */
107 if (netdev_sock < 0) {
108 netdev_sock = netif_open(devname);
109 if (netdev_sock < 0) {
110 printf("netif_open() failed\n");
111 return ENXIO;
112 }
113 #ifdef NETIF_DEBUG
114 if (debug)
115 printf("netif_open() succeeded\n");
116 #endif
117 }
118 if (rootip.s_addr == 0) {
119 /* Get root IP address, and path, etc. */
120 error = net_getparams(netdev_sock);
121 if (error) {
122 /* getparams makes its own noise */
123 goto fail;
124 }
125 /* Get the NFS file handle (mountd). */
126 error = nfs_mount(netdev_sock, rootip, rootpath);
127 if (error) {
128 printf("NFS mount error=%d\n", error);
129 rootip.s_addr = 0;
130 fail:
131 netif_close(netdev_sock);
132 netdev_sock = -1;
133 return error;
134 }
135 #ifdef NETIF_DEBUG
136 if (debug)
137 printf("NFS mount succeeded\n");
138 #endif
139 }
140 }
141 netdev_opens++;
142 f->f_devdata = nfs_root_node;
143 return error;
144 }
145
146 int
147 net_close(struct open_file *f)
148 {
149
150 #ifdef NETIF_DEBUG
151 if (debug)
152 printf("net_close: opens=%d\n", netdev_opens);
153 #endif
154
155 /* On last close, do netif close, etc. */
156 f->f_devdata = NULL;
157 /* Extra close call? */
158 if (netdev_opens <= 0)
159 return 0;
160 netdev_opens--;
161 /* Not last close? */
162 if (netdev_opens > 0)
163 return 0;
164 rootip.s_addr = 0;
165 if (netdev_sock >= 0) {
166 #ifdef NETIF_DEBUG
167 if (debug)
168 printf("net_close: calling netif_close()\n");
169 #endif
170 netif_close(netdev_sock);
171 netdev_sock = -1;
172 }
173 return 0;
174 }
175
176 int
177 net_ioctl(struct open_file *f, u_long cmd, void *data)
178 {
179
180 return EIO;
181 }
182
183 int
184 net_strategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf,
185 size_t *rsize)
186 {
187
188 return EIO;
189 }
190
191
192 /*
193 * Get info for NFS boot: our IP address, our hostname,
194 * server IP address, and our root path on the server.
195 * There are two ways to do this: The old, Sun way,
196 * and the more modern, BOOTP way. (RFC951, RFC1048)
197 *
198 * The default is to use the Sun bootparams RPC
199 * (because that is what the kernel will do).
200 * MD code can make try_bootp initialied data,
201 * which will override this common definition.
202 */
203 #ifdef SUPPORT_BOOTP
204 int try_bootp;
205 #endif
206
207 static int
208 net_getparams(int sock)
209 {
210 char buf[MAXHOSTNAMELEN];
211 n_long smask;
212
213 #ifdef SUPPORT_BOOTP
214 /*
215 * Try to get boot info using BOOTP. If we succeed, then
216 * the server IP address, gateway, and root path will all
217 * be initialized. If any remain uninitialized, we will
218 * use RARP and RPC/bootparam (the Sun way) to get them.
219 */
220 if (try_bootp)
221 bootp(sock);
222 if (myip.s_addr != 0)
223 return 0;
224 #ifdef NETIF_DEBUG
225 if (debug)
226 printf("BOOTP failed, trying RARP/RPC...\n");
227 #endif
228 #endif
229
230 /*
231 * Use RARP to get our IP address. This also sets our
232 * netmask to the "natural" default for our address.
233 */
234 if (rarp_getipaddress(sock)) {
235 printf("RARP failed\n");
236 return EIO;
237 }
238 #ifdef NETIF_DEBUG
239 if (debug)
240 printf("client addr: %s\n", inet_ntoa(myip));
241 #endif
242
243 /* Get our hostname, server IP address, gateway. */
244 if (bp_whoami(sock)) {
245 printf("bootparam/whoami RPC failed\n");
246 return EIO;
247 }
248 #ifdef NETIF_DEBUG
249 if (debug)
250 printf("client name: %s\n", hostname);
251 #endif
252
253 /*
254 * Ignore the gateway from whoami (unreliable).
255 * Use the "gateway" parameter instead.
256 */
257 smask = 0;
258 gateip.s_addr = 0;
259 if (bp_getfile(sock, "gateway", &gateip, buf)) {
260 printf("nfs_open: gateway bootparam missing\n");
261 } else {
262 /* Got it! Parse the netmask. */
263 smask = inet_addr(buf);
264 }
265 if (smask) {
266 netmask = smask;
267 #ifdef NETIF_DEBUG
268 if (debug)
269 printf("subnet mask: %s\n", intoa(netmask));
270 #endif
271 }
272 #ifdef NETIF_DEBUG
273 if (debug)
274 if (gateip.s_addr)
275 printf("net gateway: %s\n", inet_ntoa(gateip));
276 #endif
277
278 /* Get the root server and pathname. */
279 if (bp_getfile(sock, "root", &rootip, rootpath)) {
280 printf("bootparam/getfile RPC failed\n");
281 return EIO;
282 }
283
284 #ifdef NETIF_DEBUG
285 if (debug) {
286 printf("server addr: %s\n", inet_ntoa(rootip));
287 printf("server path: %s\n", rootpath);
288 }
289 #endif
290
291 return 0;
292 }
293