1 /* $NetBSD: dev_net.c,v 1.19 2002/03/17 05:46:37 gmcgarry 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
73 extern int nfs_root_node[]; /* XXX - get from nfs_mount() */
74
75 static int netdev_sock = -1;
76 static int netdev_opens;
77
78 static int net_getparams __P((int sock));
79
80 /*
81 * Called by devopen after it sets f->f_dev to our devsw entry.
82 * This opens the low-level device and sets f->f_devdata.
83 * This is declared with variable arguments...
84 */
85 int
86 net_open(struct open_file *f, ...)
87 {
88 va_list ap;
89 char *devname; /* Device part of file name (or NULL). */
90 int error = 0;
91
92 va_start(ap, f);
93 devname = va_arg(ap, char*);
94 va_end(ap);
95
96 #ifdef NETIF_DEBUG
97 if (debug)
98 printf("net_open: %s\n", devname);
99 #endif
100
101 /* On first open, do netif open, mount, etc. */
102 if (netdev_opens == 0) {
103 /* Find network interface. */
104 if (netdev_sock < 0) {
105 netdev_sock = netif_open(devname);
106 if (netdev_sock < 0) {
107 printf("net_open: netif_open() failed\n");
108 return (ENXIO);
109 }
110 #ifdef NETIF_DEBUG
111 if (debug)
112 printf("net_open: netif_open() succeeded\n");
113 #endif
114 }
115 if (rootip.s_addr == 0) {
116 /* Get root IP address, and path, etc. */
117 error = net_getparams(netdev_sock);
118 if (error) {
119 /* getparams makes its own noise */
120 goto fail;
121 }
122 /* Get the NFS file handle (mountd). */
123 error = nfs_mount(netdev_sock, rootip, rootpath);
124 if (error) {
125 printf("net_open: NFS mount error=%d\n", error);
126 rootip.s_addr = 0;
127 fail:
128 netif_close(netdev_sock);
129 netdev_sock = -1;
130 return (error);
131 }
132 #ifdef NETIF_DEBUG
133 if (debug)
134 printf("net_open: NFS mount succeeded\n");
135 #endif
136 }
137 }
138 netdev_opens++;
139 f->f_devdata = nfs_root_node;
140 return (error);
141 }
142
143 int
144 net_close(f)
145 struct open_file *f;
146 {
147
148 #ifdef NETIF_DEBUG
149 if (debug)
150 printf("net_close: opens=%d\n", netdev_opens);
151 #endif
152
153 /* On last close, do netif close, etc. */
154 f->f_devdata = NULL;
155 /* Extra close call? */
156 if (netdev_opens <= 0)
157 return (0);
158 netdev_opens--;
159 /* Not last close? */
160 if (netdev_opens > 0)
161 return(0);
162 rootip.s_addr = 0;
163 if (netdev_sock >= 0) {
164 #ifdef NETIF_DEBUG
165 if (debug)
166 printf("net_close: calling netif_close()\n");
167 #endif
168 netif_close(netdev_sock);
169 netdev_sock = -1;
170 }
171 return (0);
172 }
173
174 int
175 net_ioctl(f, cmd, data)
176 struct open_file *f;
177 u_long cmd;
178 void *data;
179 {
180 return EIO;
181 }
182
183 int
184 net_strategy(devdata, rw, blk, size, buf, rsize)
185 void *devdata;
186 int rw;
187 daddr_t blk;
188 size_t size;
189 void *buf;
190 size_t *rsize;
191 {
192 return EIO;
193 }
194
195
196 /*
197 * Get info for NFS boot: our IP address, our hostname,
198 * server IP address, and our root path on the server.
199 * There are two ways to do this: The old, Sun way,
200 * and the more modern, BOOTP way. (RFC951, RFC1048)
201 *
202 * The default is to use the Sun bootparams RPC
203 * (because that is what the kernel will do).
204 * MD code can make try_bootp initialied data,
205 * which will override this common definition.
206 */
207 #ifdef SUPPORT_BOOTP
208 int try_bootp;
209 int bootp __P((int sock));
210 #endif
211
212 static int
213 net_getparams(sock)
214 int sock;
215 {
216 char buf[MAXHOSTNAMELEN];
217 n_long smask;
218
219 #ifdef SUPPORT_BOOTP
220 /*
221 * Try to get boot info using BOOTP. If we succeed, then
222 * the server IP address, gateway, and root path will all
223 * be initialized. If any remain uninitialized, we will
224 * use RARP and RPC/bootparam (the Sun way) to get them.
225 */
226 if (try_bootp)
227 bootp(sock);
228 if (myip.s_addr != 0)
229 return (0);
230 #ifdef NETIF_DEBUG
231 if (debug)
232 printf("net_open: BOOTP failed, trying RARP/RPC...\n");
233 #endif
234 #endif
235
236 /*
237 * Use RARP to get our IP address. This also sets our
238 * netmask to the "natural" default for our address.
239 */
240 if (rarp_getipaddress(sock)) {
241 printf("net_open: RARP failed\n");
242 return (EIO);
243 }
244 printf("net_open: client addr: %s\n", inet_ntoa(myip));
245
246 /* Get our hostname, server IP address, gateway. */
247 if (bp_whoami(sock)) {
248 printf("net_open: bootparam/whoami RPC failed\n");
249 return (EIO);
250 }
251 printf("net_open: client name: %s\n", hostname);
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 printf("net_open: subnet mask: %s\n", intoa(netmask));
268 }
269 if (gateip.s_addr)
270 printf("net_open: net gateway: %s\n", inet_ntoa(gateip));
271
272 /* Get the root server and pathname. */
273 if (bp_getfile(sock, "root", &rootip, rootpath)) {
274 printf("net_open: bootparam/getfile RPC failed\n");
275 return (EIO);
276 }
277
278 printf("net_open: server addr: %s\n", inet_ntoa(rootip));
279 printf("net_open: server path: %s\n", rootpath);
280
281 return (0);
282 }
Cache object: b12f810b5f2a1d506e78650cbfc43f78
|