2 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
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.
39 * At open time, this does:
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)
46 * the root file handle from mountd is saved in a global
47 * for use by the NFS open code (NFS/lookup).
50 #include <machine/stdarg.h>
51 #include <sys/param.h>
52 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
63 #include <bootparam.h>
66 #include "bootstrap.h"
72 static char *netdev_name
;
73 static int netdev_sock
= -1;
74 static int netdev_opens
;
76 static int net_init(void);
77 static int net_open(struct open_file
*, ...);
78 static int net_close(struct open_file
*);
79 static void net_cleanup(void);
80 static int net_strategy(void *, int, daddr_t
, size_t, char *, size_t *);
81 static int net_print(int);
83 static int net_getparams(int sock
);
85 struct devsw netdev
= {
97 static struct uri_scheme
{
101 { "tftp:/", NET_TFTP
},
102 { "nfs:/", NET_NFS
},
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...
118 net_open(struct open_file
*f
, ...)
122 char *devname
; /* Device part of file name (or NULL). */
126 devname
= va_arg(args
, char *);
129 /* Before opening another interface, close the previous one first. */
130 if (netdev_sock
>= 0 && strcmp(devname
, netdev_name
) != 0)
133 /* On first open, do netif open, mount, etc. */
134 if (netdev_opens
== 0) {
135 /* Find network interface. */
136 if (netdev_sock
< 0) {
137 netdev_sock
= netif_open(devname
);
138 if (netdev_sock
< 0) {
139 printf("net_open: netif_open() failed\n");
142 netdev_name
= strdup(devname
);
145 printf("net_open: netif_open() succeeded\n");
149 * If network params were not set by netif_open(), try to get
150 * them via bootp, rarp, etc.
152 if (rootip
.s_addr
== 0) {
153 /* Get root IP address, and path, etc. */
154 error
= net_getparams(netdev_sock
);
156 /* getparams makes its own noise */
158 netif_close(netdev_sock
);
164 * Set the variables required by the kernel's nfs_diskless
165 * mechanism. This is the minimum set of variables required to
166 * mount a root filesystem without needing to obtain additional
167 * info from bootp or other sources.
169 d
= socktodesc(netdev_sock
);
170 setenv("boot.netif.hwaddr", ether_sprintf(d
->myea
), 1);
171 setenv("boot.netif.ip", inet_ntoa(myip
), 1);
172 setenv("boot.netif.netmask", intoa(netmask
), 1);
173 setenv("boot.netif.gateway", inet_ntoa(gateip
), 1);
174 setenv("boot.netif.server", inet_ntoa(rootip
), 1);
175 if (netproto
== NET_TFTP
) {
176 setenv("boot.tftproot.server", inet_ntoa(rootip
), 1);
177 setenv("boot.tftproot.path", rootpath
, 1);
179 setenv("boot.nfsroot.server", inet_ntoa(rootip
), 1);
180 setenv("boot.nfsroot.path", rootpath
, 1);
184 snprintf(mtu
, sizeof (mtu
), "%u", intf_mtu
);
185 setenv("boot.netif.mtu", mtu
, 1);
189 f
->f_devdata
= &netdev_sock
;
194 net_close(struct open_file
*f
)
199 printf("net_close: opens=%d\n", netdev_opens
);
211 if (netdev_sock
>= 0) {
214 printf("net_cleanup: calling netif_close()\n");
218 netif_close(netdev_sock
);
224 net_strategy(void *devdata
, int rw
, daddr_t blk
, size_t size
, char *buf
,
231 #define SUPPORT_BOOTP
234 * Get info for NFS boot: our IP address, our hostname,
235 * server IP address, and our root path on the server.
236 * There are two ways to do this: The old, Sun way,
237 * and the more modern, BOOTP way. (RFC951, RFC1048)
239 * The default is to use the Sun bootparams RPC
240 * (because that is what the kernel will do).
241 * MD code can make try_bootp initialied data,
242 * which will override this common definition.
248 extern n_long
ip_convertaddr(char *p
);
251 net_getparams(int sock
)
253 char buf
[MAXHOSTNAMELEN
];
254 n_long rootaddr
, smask
;
255 struct iodesc
*d
= socktodesc(sock
);
256 extern struct in_addr servip
;
260 * Try to get boot info using BOOTP. If we succeed, then
261 * the server IP address, gateway, and root path will all
262 * be initialized. If any remain uninitialized, we will
263 * use RARP and RPC/bootparam (the Sun way) to get them.
267 if (bootp_response
!= NULL
) {
268 rc
= dhcp_try_rfc1048(bootp_response
->bp_vend
,
269 bootp_response_size
-
270 offsetof(struct bootp
, bp_vend
));
272 if (servip
.s_addr
== 0)
273 servip
= bootp_response
->bp_siaddr
;
274 if (rootip
.s_addr
== 0)
275 rootip
= bootp_response
->bp_siaddr
;
276 if (gateip
.s_addr
== 0)
277 gateip
= bootp_response
->bp_giaddr
;
278 if (myip
.s_addr
== 0)
279 myip
= bootp_response
->bp_yiaddr
;
283 bootp(sock
, BOOTP_NONE
);
285 if (myip
.s_addr
!= 0)
289 printf("net_open: BOOTP failed, trying RARP/RPC...\n");
294 * Use RARP to get our IP address. This also sets our
295 * netmask to the "natural" default for our address.
297 if (rarp_getipaddress(sock
)) {
298 printf("net_open: RARP failed\n");
301 printf("net_open: client addr: %s\n", inet_ntoa(myip
));
303 /* Get our hostname, server IP address, gateway. */
304 if (bp_whoami(sock
)) {
305 printf("net_open: bootparam/whoami RPC failed\n");
310 printf("net_open: client name: %s\n", hostname
);
314 * Ignore the gateway from whoami (unreliable).
315 * Use the "gateway" parameter instead.
319 if (bp_getfile(sock
, "gateway", &gateip
, buf
) == 0) {
320 /* Got it! Parse the netmask. */
321 smask
= ip_convertaddr(buf
);
327 printf("net_open: subnet mask: %s\n", intoa(netmask
));
331 if (gateip
.s_addr
&& debug
)
332 printf("net_open: net gateway: %s\n", inet_ntoa(gateip
));
335 /* Get the root server and pathname. */
336 if (bp_getfile(sock
, "root", &rootip
, rootpath
)) {
337 printf("net_open: bootparam/getfile RPC failed\n");
341 if ((rootaddr
= net_parse_rootpath()) != INADDR_NONE
)
342 rootip
.s_addr
= rootaddr
;
346 printf("net_open: server addr: %s\n", inet_ntoa(rootip
));
347 printf("net_open: server path: %s\n", rootpath
);
355 net_print(int verbose
)
357 struct netif_driver
*drv
;
361 if (netif_drivers
[0] == NULL
)
364 printf("%s devices:", netdev
.dv_name
);
365 if ((ret
= pager_output("\n")) != 0)
369 for (d
= 0; netif_drivers
[d
]; d
++) {
370 drv
= netif_drivers
[d
];
371 for (i
= 0; i
< drv
->netif_nifs
; i
++) {
372 printf("\t%s%d:", netdev
.dv_name
, cnt
++);
374 printf(" (%s%d)", drv
->netif_bname
,
375 drv
->netif_ifs
[i
].dif_unit
);
377 if ((ret
= pager_output("\n")) != 0)
385 * Parses the rootpath if present
387 * The rootpath format can be in the form
388 * <scheme>://IPv4/path
391 * For compatibility with previous behaviour it also accepts as an NFS scheme
395 * If an IPv4 address has been specified, it will be stripped out and passed
396 * out as the return value of this function in network byte order.
398 * If no global default scheme has been specified and no scheme has been
399 * specified, we will assume that this is an NFS URL.
401 * The pathname will be stored in the global variable rootpath.
404 net_parse_rootpath(void)
406 n_long addr
= htonl(INADDR_NONE
);
413 for (i
= 0; i
< nitems(uri_schemes
); i
++) {
414 if (strncmp(rootpath
, uri_schemes
[i
].scheme
,
415 strlen(uri_schemes
[i
].scheme
)) != 0)
418 netproto
= uri_schemes
[i
].proto
;
422 /* Fallback for compatibility mode */
423 if (netproto
== NET_NONE
) {
425 (void) strsep(&ptr
, ":");
427 addr
= inet_addr(rootpath
);
428 bcopy(ptr
, rootpath
, strlen(ptr
) + 1);
431 ptr
+= strlen(uri_schemes
[i
].scheme
);
433 /* we are in the form <scheme>://, we do expect an ip */
436 * XXX when http will be there we will need to check for
437 * a port, but right now we do not need it yet.
438 * Also will need rework for IPv6.
440 val
= strchr(ptr
, '/');
442 snprintf(ip
, sizeof (ip
), "%.*s",
443 (int)((uintptr_t)val
- (uintptr_t)ptr
),
445 addr
= inet_addr(ip
);
446 if (addr
== htonl(INADDR_NONE
)) {
447 printf("Bad IP address: %s\n", ip
);
449 bcopy(val
, rootpath
, strlen(val
) + 1);
453 bcopy(ptr
, rootpath
, strlen(ptr
) + 1);