1 /* $NetBSD: dev_net.c,v 1.23 2008/04/28 20:24:06 martin Exp $ */
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 * This module implements a "raw device" interface suitable for
37 * use by the stand-alone I/O library NFS code. This interface
38 * does not support any "block" access, and exists only for the
39 * purpose of initializing the network interface, getting boot
40 * parameters, and performing the NFS mount.
42 * At open time, this does:
44 * find interface - netif_open()
45 * RARP for IP address - rarp_getipaddress()
46 * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...)
47 * RPC/mountd - nfs_mount(sock, ip, path)
49 * the root file handle from mountd is saved in a global
50 * for use by the NFS open code (NFS/lookup).
53 #include <machine/stdarg.h>
54 #include <sys/param.h>
55 #include <sys/socket.h>
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
66 #include <bootparam.h>
69 #include "bootstrap.h"
75 static char *netdev_name;
76 static int netdev_sock = -1;
77 static int netdev_opens;
79 static int net_init(void);
80 static int net_open(struct open_file *, ...);
81 static int net_close(struct open_file *);
82 static void net_cleanup(void);
83 static int net_strategy(void *, int, daddr_t, size_t, char *, size_t *);
84 static int net_print(int);
86 static int net_getparams(int sock);
88 struct devsw netdev = {
100 static struct uri_scheme {
104 { "tftp:/", NET_TFTP },
105 { "nfs:/", NET_NFS },
116 * Called by devopen after it sets f->f_dev to our devsw entry.
117 * This opens the low-level device and sets f->f_devdata.
118 * This is declared with variable arguments...
121 net_open(struct open_file *f, ...)
126 const char *devname; /* Device part of file name (or NULL). */
130 dev = va_arg(args, struct devdesc *);
133 devname = dev->d_dev->dv_name;
134 /* Before opening another interface, close the previous one first. */
135 if (netdev_sock >= 0 && strcmp(devname, netdev_name) != 0)
138 /* On first open, do netif open, mount, etc. */
139 if (netdev_opens == 0) {
140 /* Find network interface. */
141 if (netdev_sock < 0) {
142 netdev_sock = netif_open(dev);
143 if (netdev_sock < 0) {
144 printf("net_open: netif_open() failed\n");
147 netdev_name = strdup(devname);
150 printf("net_open: netif_open() succeeded\n");
154 * If network params were not set by netif_open(), try to get
155 * them via bootp, rarp, etc.
157 if (rootip.s_addr == 0) {
158 /* Get root IP address, and path, etc. */
159 error = net_getparams(netdev_sock);
161 /* getparams makes its own noise */
163 netif_close(netdev_sock);
169 * Set the variables required by the kernel's nfs_diskless
170 * mechanism. This is the minimum set of variables required to
171 * mount a root filesystem without needing to obtain additional
172 * info from bootp or other sources.
174 d = socktodesc(netdev_sock);
175 setenv("boot.netif.hwaddr", ether_sprintf(d->myea), 1);
176 setenv("boot.netif.ip", inet_ntoa(myip), 1);
177 setenv("boot.netif.netmask", intoa(netmask), 1);
178 setenv("boot.netif.gateway", inet_ntoa(gateip), 1);
179 setenv("boot.netif.server", inet_ntoa(rootip), 1);
180 if (netproto == NET_TFTP) {
181 setenv("boot.tftproot.server", inet_ntoa(rootip), 1);
182 setenv("boot.tftproot.path", rootpath, 1);
183 } else if (netproto == NET_NFS) {
184 setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
185 setenv("boot.nfsroot.path", rootpath, 1);
189 snprintf(mtu, sizeof(mtu), "%u", intf_mtu);
190 setenv("boot.netif.mtu", mtu, 1);
195 f->f_devdata = &netdev_sock;
200 net_close(struct open_file *f)
205 printf("net_close: opens=%d\n", netdev_opens);
217 if (netdev_sock >= 0) {
220 printf("net_cleanup: calling netif_close()\n");
224 netif_close(netdev_sock);
230 net_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf,
237 #define SUPPORT_BOOTP
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)
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.
254 extern n_long ip_convertaddr(char *p);
257 net_getparams(int sock)
259 char buf[MAXHOSTNAMELEN];
260 n_long rootaddr, smask;
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.
271 if (myip.s_addr != 0)
275 printf("net_open: BOOTP failed, trying RARP/RPC...\n");
280 * Use RARP to get our IP address. This also sets our
281 * netmask to the "natural" default for our address.
283 if (rarp_getipaddress(sock)) {
284 printf("net_open: RARP failed\n");
287 printf("net_open: client addr: %s\n", inet_ntoa(myip));
289 /* Get our hostname, server IP address, gateway. */
290 if (bp_whoami(sock)) {
291 printf("net_open: bootparam/whoami RPC failed\n");
296 printf("net_open: client name: %s\n", hostname);
300 * Ignore the gateway from whoami (unreliable).
301 * Use the "gateway" parameter instead.
305 if (bp_getfile(sock, "gateway", &gateip, buf) == 0) {
306 /* Got it! Parse the netmask. */
307 smask = ip_convertaddr(buf);
313 printf("net_open: subnet mask: %s\n", intoa(netmask));
317 if (gateip.s_addr && debug)
318 printf("net_open: net gateway: %s\n", inet_ntoa(gateip));
321 /* Get the root server and pathname. */
322 if (bp_getfile(sock, "root", &rootip, rootpath)) {
323 printf("net_open: bootparam/getfile RPC failed\n");
327 if ((rootaddr = net_parse_rootpath()) != INADDR_NONE)
328 rootip.s_addr = rootaddr;
332 printf("net_open: server addr: %s\n", inet_ntoa(rootip));
333 printf("net_open: server path: %s\n", rootpath);
341 net_print(int verbose)
343 struct netif_driver *drv;
347 if (netif_drivers[0] == NULL)
350 printf("%s devices:", netdev.dv_name);
351 if ((ret = pager_output("\n")) != 0)
355 for (d = 0; netif_drivers[d]; d++) {
356 drv = netif_drivers[d];
357 for (i = 0; i < drv->netif_nifs; i++) {
358 printf("\t%s%d:", netdev.dv_name, cnt++);
360 printf(" (%s%d)", drv->netif_bname,
361 drv->netif_ifs[i].dif_unit);
363 if ((ret = pager_output("\n")) != 0)
371 * Parses the rootpath if present
373 * The rootpath format can be in the form
377 * For compatibility with previous behaviour it also accepts as an NFS scheme
381 * If an ip is set it returns it in network byte order.
382 * The default scheme defined in the global netproto, if not set it defaults to
384 * It leaves just the pathname in the global rootpath.
387 net_parse_rootpath(void)
389 n_long addr = htonl(INADDR_NONE);
396 for (i = 0; i < nitems(uri_schemes); i++) {
397 if (strncmp(rootpath, uri_schemes[i].scheme,
398 strlen(uri_schemes[i].scheme)) != 0)
401 netproto = uri_schemes[i].proto;
405 /* Fallback for compatibility mode */
406 if (netproto == NET_NONE) {
408 (void)strsep(&ptr, ":");
410 addr = inet_addr(rootpath);
411 bcopy(ptr, rootpath, strlen(ptr) + 1);
414 ptr += strlen(uri_schemes[i].scheme);
416 /* we are in the form <scheme>://, we do expect an ip */
419 * XXX when http will be there we will need to check for
420 * a port, but right now we do not need it yet
422 val = strchr(ptr, '/');
424 snprintf(ip, sizeof(ip), "%.*s",
425 (int)((uintptr_t)val - (uintptr_t)ptr),
427 addr = inet_addr(ip);
428 bcopy(val, rootpath, strlen(val) + 1);
432 bcopy(ptr, rootpath, strlen(ptr) + 1);