dev_net.c revision 200945
1/*
2 * $NetBSD: dev_net.c,v 1.12 1997/12/10 20:38:37 gwr Exp $
3 */
4
5/*-
6 * Copyright (c) 1997 The NetBSD Foundation, Inc.
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Gordon W. Ross.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 *    must display the following acknowledgement:
22 *        This product includes software developed by the NetBSD
23 *        Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 *    contributors may be used to endorse or promote products derived
26 *    from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41#include <sys/cdefs.h>
42__FBSDID("$FreeBSD: head/sys/boot/common/dev_net.c 200945 2009-12-24 15:16:16Z marius $");
43
44/*-
45 * This module implements a "raw device" interface suitable for
46 * use by the stand-alone I/O library NFS code.  This interface
47 * does not support any "block" access, and exists only for the
48 * purpose of initializing the network interface, getting boot
49 * parameters, and performing the NFS mount.
50 *
51 * At open time, this does:
52 *
53 * find interface      - netif_open()
54 * RARP for IP address - rarp_getipaddress()
55 * RPC/bootparams      - callrpc(d, RPC_BOOTPARAMS, ...)
56 * RPC/mountd          - nfs_mount(sock, ip, path)
57 *
58 * the root file handle from mountd is saved in a global
59 * for use by the NFS open code (NFS/lookup).
60 */
61
62#include <machine/stdarg.h>
63#include <sys/param.h>
64#include <sys/socket.h>
65#include <net/if.h>
66#include <netinet/in.h>
67#include <netinet/in_systm.h>
68
69#include <stand.h>
70#include <string.h>
71#include <net.h>
72#include <netif.h>
73#include <bootp.h>
74#include <bootparam.h>
75
76#include "dev_net.h"
77#include "bootstrap.h"
78
79#ifdef	NETIF_DEBUG
80int debug = 0;
81#endif
82
83static int netdev_sock = -1;
84static int netdev_opens;
85
86static int	net_init(void);
87static int	net_open(struct open_file *, ...);
88static int	net_close(struct open_file *);
89static int	net_strategy();
90static void	net_print(int);
91
92static int net_getparams(int sock);
93
94struct devsw netdev = {
95	"net",
96	DEVT_NET,
97	net_init,
98	net_strategy,
99	net_open,
100	net_close,
101	noioctl,
102	net_print
103};
104
105static int
106net_init(void)
107{
108
109	return (0);
110}
111
112/*
113 * Called by devopen after it sets f->f_dev to our devsw entry.
114 * This opens the low-level device and sets f->f_devdata.
115 * This is declared with variable arguments...
116 */
117static int
118net_open(struct open_file *f, ...)
119{
120	va_list args;
121	char *devname;		/* Device part of file name (or NULL). */
122	int error = 0;
123
124	va_start(args, f);
125	devname = va_arg(args, char*);
126	va_end(args);
127
128	/* On first open, do netif open, mount, etc. */
129	if (netdev_opens == 0) {
130		/* Find network interface. */
131		if (netdev_sock < 0) {
132			netdev_sock = netif_open(devname);
133			if (netdev_sock < 0) {
134				printf("net_open: netif_open() failed\n");
135				return (ENXIO);
136			}
137#ifdef	NETIF_DEBUG
138			if (debug)
139				printf("net_open: netif_open() succeeded\n");
140#endif
141		}
142		if (rootip.s_addr == 0) {
143			/* Get root IP address, and path, etc. */
144			error = net_getparams(netdev_sock);
145			if (error) {
146				/* getparams makes its own noise */
147				netif_close(netdev_sock);
148				netdev_sock = -1;
149				return (error);
150			}
151		}
152#if defined(__sparc64__)
153		netdev_opens++;
154#endif
155	}
156	netdev_opens++;
157	f->f_devdata = &netdev_sock;
158	return (error);
159}
160
161static int
162net_close(struct open_file *f)
163{
164#ifdef	NETIF_DEBUG
165	if (debug)
166		printf("net_close: opens=%d\n", netdev_opens);
167#endif
168
169	/* On last close, do netif close, etc. */
170	f->f_devdata = NULL;
171	/* Extra close call? */
172	if (netdev_opens <= 0)
173		return (0);
174	netdev_opens--;
175	/* Not last close? */
176	if (netdev_opens > 0)
177		return(0);
178	rootip.s_addr = 0;
179	if (netdev_sock >= 0) {
180#ifdef	NETIF_DEBUG
181		if (debug)
182			printf("net_close: calling netif_close()\n");
183#endif
184		netif_close(netdev_sock);
185		netdev_sock = -1;
186	}
187	return (0);
188}
189
190static int
191net_strategy()
192{
193
194	return (EIO);
195}
196
197#define SUPPORT_BOOTP
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
211int try_bootp = 1;
212#endif
213
214extern n_long ip_convertaddr(char *p);
215
216static int
217net_getparams(int sock)
218{
219	char buf[MAXHOSTNAMELEN];
220	char temp[FNAME_SIZE];
221	struct iodesc *d;
222	int i;
223	n_long smask;
224
225#ifdef	SUPPORT_BOOTP
226	/*
227	 * Try to get boot info using BOOTP.  If we succeed, then
228	 * the server IP address, gateway, and root path will all
229	 * be initialized.  If any remain uninitialized, we will
230	 * use RARP and RPC/bootparam (the Sun way) to get them.
231	 */
232	if (try_bootp)
233		bootp(sock, BOOTP_NONE);
234	if (myip.s_addr != 0)
235		goto exit;
236#ifdef	NETIF_DEBUG
237	if (debug)
238		printf("net_open: BOOTP failed, trying RARP/RPC...\n");
239#endif
240#endif
241
242	/*
243	 * Use RARP to get our IP address.  This also sets our
244	 * netmask to the "natural" default for our address.
245	 */
246	if (rarp_getipaddress(sock)) {
247		printf("net_open: RARP failed\n");
248		return (EIO);
249	}
250	printf("net_open: client addr: %s\n", inet_ntoa(myip));
251
252	/* Get our hostname, server IP address, gateway. */
253	if (bp_whoami(sock)) {
254		printf("net_open: bootparam/whoami RPC failed\n");
255		return (EIO);
256	}
257#ifdef	NETIF_DEBUG
258	if (debug)
259		printf("net_open: client name: %s\n", hostname);
260#endif
261
262	/*
263	 * Ignore the gateway from whoami (unreliable).
264	 * Use the "gateway" parameter instead.
265	 */
266	smask = 0;
267	gateip.s_addr = 0;
268	if (bp_getfile(sock, "gateway", &gateip, buf) == 0) {
269		/* Got it!  Parse the netmask. */
270		smask = ip_convertaddr(buf);
271	}
272	if (smask) {
273		netmask = smask;
274#ifdef	NETIF_DEBUG
275		if (debug)
276			printf("net_open: subnet mask: %s\n", intoa(netmask));
277#endif
278	}
279#ifdef	NETIF_DEBUG
280	if (gateip.s_addr && debug)
281		printf("net_open: net gateway: %s\n", inet_ntoa(gateip));
282#endif
283
284	/* Get the root server and pathname. */
285	if (bp_getfile(sock, "root", &rootip, rootpath)) {
286		printf("net_open: bootparam/getfile RPC failed\n");
287		return (EIO);
288	}
289exit:
290	/*
291	 * If present, strip the server's address off of the rootpath
292	 * before passing it along.  This allows us to be compatible with
293	 * the kernel's diskless (BOOTP_NFSROOT) booting conventions
294	 */
295	for (i = 0; rootpath[i] != '\0' && i < FNAME_SIZE; i++)
296		if (rootpath[i] == ':')
297			break;
298	if (i && i != FNAME_SIZE && rootpath[i] == ':') {
299		rootpath[i++] = '\0';
300		if (inet_addr(&rootpath[0]) != INADDR_NONE)
301			rootip.s_addr = inet_addr(&rootpath[0]);
302		bcopy(&rootpath[i], &temp[0], strlen(&rootpath[i])+1);
303		bcopy(&temp[0], &rootpath[0], strlen(&rootpath[i])+1);
304	}
305#ifdef	NETIF_DEBUG
306	if (debug) {
307		printf("net_open: server addr: %s\n", inet_ntoa(rootip));
308		printf("net_open: server path: %s\n", rootpath);
309	}
310#endif
311
312	d = socktodesc(sock);
313	sprintf(temp, "%6D", d->myea, ":");
314	setenv("boot.netif.ip", inet_ntoa(myip), 1);
315	setenv("boot.netif.netmask", intoa(netmask), 1);
316	setenv("boot.netif.gateway", inet_ntoa(gateip), 1);
317	setenv("boot.netif.hwaddr", temp, 1);
318	setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
319	setenv("boot.nfsroot.path", rootpath, 1);
320
321	return (0);
322}
323
324static void
325net_print(int verbose)
326{
327	struct netif_driver *drv;
328	int i, d, cnt;
329
330	cnt = 0;
331	for (d = 0; netif_drivers[d]; d++) {
332		drv = netif_drivers[d];
333		for (i = 0; i < drv->netif_nifs; i++) {
334			printf("\t%s%d:", "net", cnt++);
335			if (verbose)
336				printf(" (%s%d)", drv->netif_bname,
337				    drv->netif_ifs[i].dif_unit);
338		}
339	}
340	printf("\n");
341}
342