1/*	$NetBSD: dev_net.c,v 1.23 2008/04/28 20:24:06 martin 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*-
33 * This module implements a "raw device" interface suitable for
34 * use by the stand-alone I/O library NFS code.  This interface
35 * does not support any "block" access, and exists only for the
36 * purpose of initializing the network interface, getting boot
37 * parameters, and performing the NFS mount.
38 *
39 * At open time, this does:
40 *
41 * find interface      - netif_open()
42 * RARP for IP address - rarp_getipaddress()
43 * RPC/bootparams      - callrpc(d, RPC_BOOTPARAMS, ...)
44 * RPC/mountd          - nfs_mount(sock, ip, path)
45 *
46 * the root file handle from mountd is saved in a global
47 * for use by the NFS open code (NFS/lookup).
48 */
49
50#include <machine/stdarg.h>
51#include <sys/param.h>
52#include <sys/socket.h>
53#include <net/if.h>
54#include <netinet/in.h>
55#include <netinet/in_systm.h>
56
57#include <stand.h>
58#include <stddef.h>
59#include <string.h>
60#include <net.h>
61#include <netif.h>
62#include <bootp.h>
63#include <bootparam.h>
64
65#include "dev_net.h"
66#include "bootstrap.h"
67
68#ifdef	NETIF_DEBUG
69int debug = 0;
70#endif
71
72static char *netdev_name;
73static int netdev_sock = -1;
74static int netdev_opens;
75
76static int	net_init(void);
77static int	net_open(struct open_file *, ...);
78static int	net_close(struct open_file *);
79static void	net_cleanup(void);
80static int	net_strategy(void *, int, daddr_t, size_t, char *, size_t *);
81static int	net_print(int);
82
83static int net_getparams(int sock);
84
85struct devsw netdev = {
86	.dv_name = "net",
87	.dv_type = DEVT_NET,
88	.dv_init = net_init,
89	.dv_strategy = net_strategy,
90	.dv_open = net_open,
91	.dv_close = net_close,
92	.dv_ioctl = noioctl,
93	.dv_print = net_print,
94	.dv_cleanup = net_cleanup,
95};
96
97static struct uri_scheme {
98	const char *scheme;
99	int proto;
100} uri_schemes[] = {
101	{ "tftp:/", NET_TFTP },
102	{ "nfs:/", NET_NFS },
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 dev->d_opendata.
115 * This is declared with variable arguments...
116 */
117static int
118net_open(struct open_file *f, ...)
119{
120	struct iodesc *d;
121	va_list args;
122	struct devdesc *dev;
123	const char *devname;	/* Device part of file name (or NULL). */
124	int error = 0;
125
126	va_start(args, f);
127	dev = va_arg(args, struct devdesc *);
128	va_end(args);
129
130	devname = dev->d_dev->dv_name;
131	/* Before opening another interface, close the previous one first. */
132	if (netdev_sock >= 0 && strcmp(devname, netdev_name) != 0)
133		net_cleanup();
134
135	/* On first open, do netif open, mount, etc. */
136	if (netdev_opens == 0) {
137		/* Find network interface. */
138		if (netdev_sock < 0) {
139			netdev_sock = netif_open(dev);
140			if (netdev_sock < 0) {
141				printf("%s: netif_open() failed\n", __func__);
142				return (ENXIO);
143			}
144			netdev_name = strdup(devname);
145#ifdef	NETIF_DEBUG
146			if (debug)
147				printf("%s: netif_open() succeeded\n",
148				    __func__);
149#endif
150		}
151		/*
152		 * If network params were not set by netif_open(), try to get
153		 * them via bootp, rarp, etc.
154		 */
155		if (rootip.s_addr == 0) {
156			/* Get root IP address, and path, etc. */
157			error = net_getparams(netdev_sock);
158			if (error) {
159				/* getparams makes its own noise */
160				free(netdev_name);
161				netif_close(netdev_sock);
162				netdev_sock = -1;
163				return (error);
164			}
165		}
166		/*
167		 * Set the variables required by the kernel's nfs_diskless
168		 * mechanism.  This is the minimum set of variables required to
169		 * mount a root filesystem without needing to obtain additional
170		 * info from bootp or other sources.
171		 */
172		d = socktodesc(netdev_sock);
173		setenv("boot.netif.hwaddr", ether_sprintf(d->myea), 1);
174		setenv("boot.netif.ip", inet_ntoa(myip), 1);
175		setenv("boot.netif.netmask", intoa(netmask), 1);
176		setenv("boot.netif.gateway", inet_ntoa(gateip), 1);
177		setenv("boot.netif.server", inet_ntoa(rootip), 1);
178		if (netproto == NET_TFTP) {
179			setenv("boot.tftproot.server", inet_ntoa(rootip), 1);
180			setenv("boot.tftproot.path", rootpath, 1);
181		} else if (netproto == NET_NFS) {
182			setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
183			setenv("boot.nfsroot.path", rootpath, 1);
184		}
185		if (intf_mtu != 0) {
186			char mtu[16];
187			snprintf(mtu, sizeof(mtu), "%u", intf_mtu);
188			setenv("boot.netif.mtu", mtu, 1);
189		}
190
191	}
192	netdev_opens++;
193	dev->d_opendata = &netdev_sock;
194	return (error);
195}
196
197static int
198net_close(struct open_file *f)
199{
200	struct devdesc *dev;
201
202#ifdef	NETIF_DEBUG
203	if (debug)
204		printf("%s: opens=%d\n", __func__, netdev_opens);
205#endif
206
207	dev = f->f_devdata;
208	dev->d_opendata = NULL;
209
210	return (0);
211}
212
213static void
214net_cleanup(void)
215{
216
217	if (netdev_sock >= 0) {
218#ifdef	NETIF_DEBUG
219		if (debug)
220			printf("%s: calling netif_close()\n", __func__);
221#endif
222		rootip.s_addr = 0;
223		free(netdev_name);
224		netif_close(netdev_sock);
225		netdev_sock = -1;
226	}
227}
228
229static int
230net_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf,
231    size_t *rsize)
232{
233
234	return (EIO);
235}
236
237#define SUPPORT_BOOTP
238
239/*
240 * Get info for NFS boot: our IP address, our hostname,
241 * server IP address, and our root path on the server.
242 * There are two ways to do this:  The old, Sun way,
243 * and the more modern, BOOTP way. (RFC951, RFC1048)
244 *
245 * The default is to use the Sun bootparams RPC
246 * (because that is what the kernel will do).
247 * MD code can make try_bootp initialied data,
248 * which will override this common definition.
249 */
250#ifdef	SUPPORT_BOOTP
251int try_bootp = 1;
252#endif
253
254extern n_long ip_convertaddr(char *p);
255
256static int
257net_getparams(int sock)
258{
259	char buf[MAXHOSTNAMELEN];
260	n_long rootaddr, smask;
261
262#ifdef	SUPPORT_BOOTP
263	/*
264	 * Try to get boot info using BOOTP.  If we succeed, then
265	 * the server IP address, gateway, and root path will all
266	 * be initialized.  If any remain uninitialized, we will
267	 * use RARP and RPC/bootparam (the Sun way) to get them.
268	 */
269	if (try_bootp)
270		bootp(sock);
271	if (myip.s_addr != 0)
272		goto exit;
273#ifdef	NETIF_DEBUG
274	if (debug)
275		printf("%s: BOOTP failed, trying RARP/RPC...\n", __func__);
276#endif
277#endif
278
279	/*
280	 * Use RARP to get our IP address.  This also sets our
281	 * netmask to the "natural" default for our address.
282	 */
283	if (rarp_getipaddress(sock)) {
284		printf("%s: RARP failed\n", __func__);
285		return (EIO);
286	}
287	printf("%s: client addr: %s\n", __func__, inet_ntoa(myip));
288
289	/* Get our hostname, server IP address, gateway. */
290	if (bp_whoami(sock)) {
291		printf("%s: bootparam/whoami RPC failed\n", __func__);
292		return (EIO);
293	}
294#ifdef	NETIF_DEBUG
295	if (debug)
296		printf("%s: client name: %s\n", __func__, hostname);
297#endif
298
299	/*
300	 * Ignore the gateway from whoami (unreliable).
301	 * Use the "gateway" parameter instead.
302	 */
303	smask = 0;
304	gateip.s_addr = 0;
305	if (bp_getfile(sock, "gateway", &gateip, buf) == 0) {
306		/* Got it!  Parse the netmask. */
307		smask = ip_convertaddr(buf);
308	}
309	if (smask) {
310		netmask = smask;
311#ifdef	NETIF_DEBUG
312		if (debug)
313			printf("%s: subnet mask: %s\n", __func__,
314			    intoa(netmask));
315#endif
316	}
317#ifdef	NETIF_DEBUG
318	if (gateip.s_addr && debug)
319		printf("%s: net gateway: %s\n", __func__, inet_ntoa(gateip));
320#endif
321
322	/* Get the root server and pathname. */
323	if (bp_getfile(sock, "root", &rootip, rootpath)) {
324		printf("%s: bootparam/getfile RPC failed\n", __func__);
325		return (EIO);
326	}
327exit:
328	if ((rootaddr = net_parse_rootpath()) != INADDR_NONE)
329		rootip.s_addr = rootaddr;
330
331#ifdef	NETIF_DEBUG
332	if (debug) {
333		printf("%s: server addr: %s\n", __func__, inet_ntoa(rootip));
334		printf("%s: server path: %s\n", __func__, rootpath);
335	}
336#endif
337
338	return (0);
339}
340
341static int
342net_print(int verbose)
343{
344	struct netif_driver *drv;
345	int i, d, cnt;
346	int ret = 0;
347
348	if (netif_drivers[0] == NULL)
349		return (ret);
350
351	printf("%s devices:", netdev.dv_name);
352	if ((ret = pager_output("\n")) != 0)
353		return (ret);
354
355	cnt = 0;
356	for (d = 0; netif_drivers[d]; d++) {
357		drv = netif_drivers[d];
358		for (i = 0; i < drv->netif_nifs; i++) {
359			printf("\t%s%d:", netdev.dv_name, cnt++);
360			if (verbose) {
361				printf(" (%s%d)", drv->netif_bname,
362				    drv->netif_ifs[i].dif_unit);
363			}
364			if ((ret = pager_output("\n")) != 0)
365				return (ret);
366		}
367	}
368	return (ret);
369}
370
371/*
372 * Parses the rootpath if present
373 *
374 * The rootpath format can be in the form
375 * <scheme>://ip/path
376 * <scheme>:/path
377 *
378 * For compatibility with previous behaviour it also accepts as an NFS scheme
379 * ip:/path
380 * /path
381 *
382 * If an ip is set it returns it in network byte order.
383 * The default scheme defined in the global netproto, if not set it defaults to
384 * NFS.
385 * It leaves just the pathname in the global rootpath.
386 */
387uint32_t
388net_parse_rootpath(void)
389{
390	n_long addr = htonl(INADDR_NONE);
391	size_t i;
392	char ip[FNAME_SIZE];
393	char *ptr, *val;
394
395	netproto = NET_NONE;
396
397	for (i = 0; i < nitems(uri_schemes); i++) {
398		if (strncmp(rootpath, uri_schemes[i].scheme,
399		    strlen(uri_schemes[i].scheme)) != 0)
400			continue;
401
402		netproto = uri_schemes[i].proto;
403		break;
404	}
405	ptr = rootpath;
406	/* Fallback for compatibility mode */
407	if (netproto == NET_NONE) {
408		netproto = NET_NFS;
409		(void)strsep(&ptr, ":");
410		if (ptr != NULL) {
411			addr = inet_addr(rootpath);
412			bcopy(ptr, rootpath, strlen(ptr) + 1);
413		}
414	} else {
415		ptr += strlen(uri_schemes[i].scheme);
416		if (*ptr == '/') {
417			/* we are in the form <scheme>://, we do expect an ip */
418			ptr++;
419			/*
420			 * XXX when http will be there we will need to check for
421			 * a port, but right now we do not need it yet
422			 */
423			val = strchr(ptr, '/');
424			if (val != NULL) {
425				snprintf(ip, sizeof(ip), "%.*s",
426				    (int)((uintptr_t)val - (uintptr_t)ptr),
427				    ptr);
428				addr = inet_addr(ip);
429				bcopy(val, rootpath, strlen(val) + 1);
430			}
431		} else {
432			ptr--;
433			bcopy(ptr, rootpath, strlen(ptr) + 1);
434		}
435	}
436
437	return (addr);
438}
439