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.
31 * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
34 #include <sys/cdefs.h>
37 * This module implements a "raw device" interface suitable for
38 * use by the stand-alone I/O library NFS code. This interface
39 * does not support any "block" access, and exists only for the
40 * purpose of initializing the network interface, getting boot
41 * parameters, and performing the NFS mount.
43 * At open time, this does:
45 * find interface - netif_open()
46 * RARP for IP address - rarp_getipaddress()
47 * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...)
48 * RPC/mountd - nfs_mount(sock, ip, path)
50 * the root file handle from mountd is saved in a global
51 * for use by the NFS open code (NFS/lookup).
54 #include <machine/stdarg.h>
55 #include <sys/param.h>
56 #include <sys/socket.h>
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
67 #include <bootparam.h>
70 #include "bootstrap.h"
76 static char *netdev_name
;
77 static int netdev_sock
= -1;
78 static int netdev_opens
;
80 static int net_init(void);
81 static int net_open(struct open_file
*, ...);
82 static int net_close(struct open_file
*);
83 static void net_cleanup(void);
84 static int net_strategy(void *, int, daddr_t
, size_t, char *, size_t *);
85 static int net_print(int);
87 static int net_getparams(int sock
);
89 struct devsw netdev
= {
101 static struct uri_scheme
{
105 { "tftp:/", NET_TFTP
},
106 { "nfs:/", NET_NFS
},
117 * Called by devopen after it sets f->f_dev to our devsw entry.
118 * This opens the low-level device and sets f->f_devdata.
119 * This is declared with variable arguments...
122 net_open(struct open_file
*f
, ...)
126 char *devname
; /* Device part of file name (or NULL). */
130 devname
= va_arg(args
, char *);
133 /* Before opening another interface, close the previous one first. */
134 if (netdev_sock
>= 0 && strcmp(devname
, netdev_name
) != 0)
137 /* On first open, do netif open, mount, etc. */
138 if (netdev_opens
== 0) {
139 /* Find network interface. */
140 if (netdev_sock
< 0) {
141 netdev_sock
= netif_open(devname
);
142 if (netdev_sock
< 0) {
143 printf("net_open: netif_open() failed\n");
146 netdev_name
= strdup(devname
);
149 printf("net_open: netif_open() succeeded\n");
153 * If network params were not set by netif_open(), try to get
154 * them via bootp, rarp, etc.
156 if (rootip
.s_addr
== 0) {
157 /* Get root IP address, and path, etc. */
158 error
= net_getparams(netdev_sock
);
160 /* getparams makes its own noise */
162 netif_close(netdev_sock
);
168 * Set the variables required by the kernel's nfs_diskless
169 * mechanism. This is the minimum set of variables required to
170 * mount a root filesystem without needing to obtain additional
171 * info from bootp or other sources.
173 d
= socktodesc(netdev_sock
);
174 setenv("boot.netif.hwaddr", ether_sprintf(d
->myea
), 1);
175 setenv("boot.netif.ip", inet_ntoa(myip
), 1);
176 setenv("boot.netif.netmask", intoa(netmask
), 1);
177 setenv("boot.netif.gateway", inet_ntoa(gateip
), 1);
178 setenv("boot.netif.server", inet_ntoa(rootip
), 1);
179 if (netproto
== NET_TFTP
) {
180 setenv("boot.tftproot.server", inet_ntoa(rootip
), 1);
181 setenv("boot.tftproot.path", rootpath
, 1);
183 setenv("boot.nfsroot.server", inet_ntoa(rootip
), 1);
184 setenv("boot.nfsroot.path", rootpath
, 1);
188 snprintf(mtu
, sizeof (mtu
), "%u", intf_mtu
);
189 setenv("boot.netif.mtu", mtu
, 1);
193 f
->f_devdata
= &netdev_sock
;
198 net_close(struct open_file
*f
)
203 printf("net_close: opens=%d\n", netdev_opens
);
215 if (netdev_sock
>= 0) {
218 printf("net_cleanup: calling netif_close()\n");
222 netif_close(netdev_sock
);
228 net_strategy(void *devdata
, int rw
, daddr_t blk
, size_t size
, char *buf
,
236 * Get info for NFS boot: our IP address, our hostname,
237 * server IP address, and our root path on the server.
238 * There are two ways to do this: The old, Sun way,
239 * and the more modern, BOOTP/DHCP way. (RFC951, RFC1048)
242 extern n_long
ip_convertaddr(char *p
);
245 net_getparams(int sock
)
247 char buf
[MAXHOSTNAMELEN
];
248 n_long rootaddr
, smask
;
251 * Try to get boot info using BOOTP/DHCP. If we succeed, then
252 * the server IP address, gateway, and root path will all
253 * be initialized. If any remain uninitialized, we will
254 * use RARP and RPC/bootparam (the Sun way) to get them.
257 if (myip
.s_addr
!= 0)
261 printf("net_open: BOOTP failed, trying RARP/RPC...\n");
265 * Use RARP to get our IP address. This also sets our
266 * netmask to the "natural" default for our address.
268 if (rarp_getipaddress(sock
)) {
269 printf("net_open: RARP failed\n");
272 printf("net_open: client addr: %s\n", inet_ntoa(myip
));
274 /* Get our hostname, server IP address, gateway. */
275 if (bp_whoami(sock
)) {
276 printf("net_open: bootparam/whoami RPC failed\n");
281 printf("net_open: client name: %s\n", hostname
);
285 * Ignore the gateway from whoami (unreliable).
286 * Use the "gateway" parameter instead.
290 if (bp_getfile(sock
, "gateway", &gateip
, buf
) == 0) {
291 /* Got it! Parse the netmask. */
292 smask
= ip_convertaddr(buf
);
298 printf("net_open: subnet mask: %s\n", intoa(netmask
));
302 if (gateip
.s_addr
&& debug
)
303 printf("net_open: net gateway: %s\n", inet_ntoa(gateip
));
306 /* Get the root server and pathname. */
307 if (bp_getfile(sock
, "root", &rootip
, rootpath
)) {
308 printf("net_open: bootparam/getfile RPC failed\n");
312 if ((rootaddr
= net_parse_rootpath()) != INADDR_NONE
)
313 rootip
.s_addr
= rootaddr
;
317 printf("net_open: server addr: %s\n", inet_ntoa(rootip
));
318 printf("net_open: server path: %s\n", rootpath
);
326 net_print(int verbose
)
328 struct netif_driver
*drv
;
332 if (netif_drivers
[0] == NULL
)
335 printf("%s devices:", netdev
.dv_name
);
336 if ((ret
= pager_output("\n")) != 0)
340 for (d
= 0; netif_drivers
[d
]; d
++) {
341 drv
= netif_drivers
[d
];
342 for (i
= 0; i
< drv
->netif_nifs
; i
++) {
343 printf("\t%s%d:", netdev
.dv_name
, cnt
++);
345 printf(" (%s%d)", drv
->netif_bname
,
346 drv
->netif_ifs
[i
].dif_unit
);
348 if ((ret
= pager_output("\n")) != 0)
356 * Parses the rootpath if present
358 * The rootpath format can be in the form
359 * <scheme>://IPv4/path
362 * For compatibility with previous behaviour it also accepts as an NFS scheme
366 * If an IPv4 address has been specified, it will be stripped out and passed
367 * out as the return value of this function in network byte order.
369 * If no rootpath is present then we will default to TFTP.
371 * If no global default scheme has been specified and no scheme has been
372 * specified, we will assume that this is an NFS URL.
374 * The pathname will be stored in the global variable rootpath.
377 net_parse_rootpath(void)
379 n_long addr
= htonl(INADDR_NONE
);
386 for (i
= 0; i
< nitems(uri_schemes
); i
++) {
387 if (strncmp(rootpath
, uri_schemes
[i
].scheme
,
388 strlen(uri_schemes
[i
].scheme
)) != 0)
391 netproto
= uri_schemes
[i
].proto
;
395 /* Fallback for compatibility mode */
396 if (netproto
== NET_NONE
) {
397 if (strcmp(rootpath
, "/") == 0) {
401 (void) strsep(&ptr
, ":");
403 addr
= inet_addr(rootpath
);
404 bcopy(ptr
, rootpath
, strlen(ptr
) + 1);
408 ptr
+= strlen(uri_schemes
[i
].scheme
);
410 /* we are in the form <scheme>://, we do expect an ip */
413 * XXX when http will be there we will need to check for
414 * a port, but right now we do not need it yet.
415 * Also will need rework for IPv6.
417 val
= strchr(ptr
, '/');
419 /* If no pathname component, default to / */
420 strlcat(rootpath
, "/", sizeof (rootpath
));
421 val
= strchr(ptr
, '/');
424 snprintf(ip
, sizeof (ip
), "%.*s",
425 (int)((uintptr_t)val
- (uintptr_t)ptr
),
427 addr
= inet_addr(ip
);
428 if (addr
== htonl(INADDR_NONE
)) {
429 printf("Bad IP address: %s\n", ip
);
431 bcopy(val
, rootpath
, strlen(val
) + 1);
435 bcopy(ptr
, rootpath
, strlen(ptr
) + 1);