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