xref: /netbsd-src/sys/lib/libsa/dev_net.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /*	$NetBSD: dev_net.c,v 1.21 2007/10/27 12:19:41 tsutsui 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 sock));
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(f)
148 	struct open_file *f;
149 {
150 
151 #ifdef	NETIF_DEBUG
152 	if (debug)
153 		printf("net_close: opens=%d\n", netdev_opens);
154 #endif
155 
156 	/* On last close, do netif close, etc. */
157 	f->f_devdata = NULL;
158 	/* Extra close call? */
159 	if (netdev_opens <= 0)
160 		return (0);
161 	netdev_opens--;
162 	/* Not last close? */
163 	if (netdev_opens > 0)
164 		return(0);
165 	rootip.s_addr = 0;
166 	if (netdev_sock >= 0) {
167 #ifdef NETIF_DEBUG
168 		if (debug)
169 			printf("net_close: calling netif_close()\n");
170 #endif
171 		netif_close(netdev_sock);
172 		netdev_sock = -1;
173 	}
174 	return (0);
175 }
176 
177 int
178 net_ioctl(f, cmd, data)
179 	struct open_file *f;
180 	u_long cmd;
181 	void *data;
182 {
183 	return EIO;
184 }
185 
186 int
187 net_strategy(devdata, rw, blk, size, buf, rsize)
188 	void *devdata;
189 	int rw;
190 	daddr_t blk;
191 	size_t size;
192 	void *buf;
193 	size_t *rsize;
194 {
195 	return EIO;
196 }
197 
198 
199 /*
200  * Get info for NFS boot: our IP address, our hostname,
201  * server IP address, and our root path on the server.
202  * There are two ways to do this:  The old, Sun way,
203  * and the more modern, BOOTP way. (RFC951, RFC1048)
204  *
205  * The default is to use the Sun bootparams RPC
206  * (because that is what the kernel will do).
207  * MD code can make try_bootp initialied data,
208  * which will override this common definition.
209  */
210 #ifdef	SUPPORT_BOOTP
211 int try_bootp;
212 #endif
213 
214 static int
215 net_getparams(sock)
216 	int sock;
217 {
218 	char buf[MAXHOSTNAMELEN];
219 	n_long smask;
220 
221 #ifdef	SUPPORT_BOOTP
222 	/*
223 	 * Try to get boot info using BOOTP.  If we succeed, then
224 	 * the server IP address, gateway, and root path will all
225 	 * be initialized.  If any remain uninitialized, we will
226 	 * use RARP and RPC/bootparam (the Sun way) to get them.
227 	 */
228 	if (try_bootp)
229 		bootp(sock);
230 	if (myip.s_addr != 0)
231 		return (0);
232 #ifdef NETIF_DEBUG
233 	if (debug)
234 		printf("BOOTP failed, trying RARP/RPC...\n");
235 #endif
236 #endif
237 
238 	/*
239 	 * Use RARP to get our IP address.  This also sets our
240 	 * netmask to the "natural" default for our address.
241 	 */
242 	if (rarp_getipaddress(sock)) {
243 		printf("RARP failed\n");
244 		return (EIO);
245 	}
246 #ifdef NETIF_DEBUG
247 	if (debug)
248 		printf("client addr: %s\n", inet_ntoa(myip));
249 #endif
250 
251 	/* Get our hostname, server IP address, gateway. */
252 	if (bp_whoami(sock)) {
253 		printf("bootparam/whoami RPC failed\n");
254 		return (EIO);
255 	}
256 #ifdef NETIF_DEBUG
257 	if (debug)
258 		printf("client name: %s\n", hostname);
259 #endif
260 
261 	/*
262 	 * Ignore the gateway from whoami (unreliable).
263 	 * Use the "gateway" parameter instead.
264 	 */
265 	smask = 0;
266 	gateip.s_addr = 0;
267 	if (bp_getfile(sock, "gateway", &gateip, buf))
268 		printf("nfs_open: gateway bootparam missing\n");
269 	else {
270 		/* Got it!  Parse the netmask. */
271 		smask = inet_addr(buf);
272 	}
273 	if (smask) {
274 		netmask = smask;
275 #ifdef NETIF_DEBUG
276 		if (debug)
277 			printf("subnet mask: %s\n", intoa(netmask));
278 #endif
279 	}
280 #ifdef NETIF_DEBUG
281 	if (debug)
282 		if (gateip.s_addr)
283 			printf("net gateway: %s\n", inet_ntoa(gateip));
284 #endif
285 
286 	/* Get the root server and pathname. */
287 	if (bp_getfile(sock, "root", &rootip, rootpath)) {
288 		printf("bootparam/getfile RPC failed\n");
289 		return (EIO);
290 	}
291 
292 #ifdef NETIF_DEBUG
293 	if (debug) {
294 		printf("server addr: %s\n", inet_ntoa(rootip));
295 		printf("server path: %s\n", rootpath);
296 	}
297 #endif
298 
299 	return (0);
300 }
301