pxeboot should default to TFTP in absence of root-path
[unleashed.git] / usr / src / boot / sys / boot / common / dev_net.c
blob1814d3e682aee28e8e6031f2984cd98c979355f3
1 /*
2 * Copyright (c) 1997 The NetBSD Foundation, Inc.
3 * All rights reserved.
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Gordon W. Ross.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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>
57 #include <net/if.h>
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
61 #include <stand.h>
62 #include <stddef.h>
63 #include <string.h>
64 #include <net.h>
65 #include <netif.h>
66 #include <bootp.h>
67 #include <bootparam.h>
69 #include "dev_net.h"
70 #include "bootstrap.h"
72 #ifdef NETIF_DEBUG
73 int debug = 0;
74 #endif
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 = {
90 "net",
91 DEVT_NET,
92 net_init,
93 net_strategy,
94 net_open,
95 net_close,
96 noioctl,
97 net_print,
98 net_cleanup
101 static struct uri_scheme {
102 const char *scheme;
103 int proto;
104 } uri_schemes[] = {
105 { "tftp:/", NET_TFTP },
106 { "nfs:/", NET_NFS },
109 static int
110 net_init(void)
113 return (0);
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...
121 static int
122 net_open(struct open_file *f, ...)
124 struct iodesc *d;
125 va_list args;
126 char *devname; /* Device part of file name (or NULL). */
127 int error = 0;
129 va_start(args, f);
130 devname = va_arg(args, char *);
131 va_end(args);
133 /* Before opening another interface, close the previous one first. */
134 if (netdev_sock >= 0 && strcmp(devname, netdev_name) != 0)
135 net_cleanup();
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");
144 return (ENXIO);
146 netdev_name = strdup(devname);
147 #ifdef NETIF_DEBUG
148 if (debug)
149 printf("net_open: netif_open() succeeded\n");
150 #endif
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);
159 if (error) {
160 /* getparams makes its own noise */
161 free(netdev_name);
162 netif_close(netdev_sock);
163 netdev_sock = -1;
164 return (error);
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);
182 } else {
183 setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
184 setenv("boot.nfsroot.path", rootpath, 1);
186 if (intf_mtu != 0) {
187 char mtu[16];
188 snprintf(mtu, sizeof (mtu), "%u", intf_mtu);
189 setenv("boot.netif.mtu", mtu, 1);
192 netdev_opens++;
193 f->f_devdata = &netdev_sock;
194 return (error);
197 static int
198 net_close(struct open_file *f)
201 #ifdef NETIF_DEBUG
202 if (debug)
203 printf("net_close: opens=%d\n", netdev_opens);
204 #endif
206 f->f_devdata = NULL;
208 return (0);
211 static void
212 net_cleanup(void)
215 if (netdev_sock >= 0) {
216 #ifdef NETIF_DEBUG
217 if (debug)
218 printf("net_cleanup: calling netif_close()\n");
219 #endif
220 rootip.s_addr = 0;
221 free(netdev_name);
222 netif_close(netdev_sock);
223 netdev_sock = -1;
227 static int
228 net_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf,
229 size_t *rsize)
232 return (EIO);
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);
244 static int
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.
256 bootp(sock);
257 if (myip.s_addr != 0)
258 goto exit;
259 #ifdef NETIF_DEBUG
260 if (debug)
261 printf("net_open: BOOTP failed, trying RARP/RPC...\n");
262 #endif
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");
270 return (EIO);
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");
277 return (EIO);
279 #ifdef NETIF_DEBUG
280 if (debug)
281 printf("net_open: client name: %s\n", hostname);
282 #endif
285 * Ignore the gateway from whoami (unreliable).
286 * Use the "gateway" parameter instead.
288 smask = 0;
289 gateip.s_addr = 0;
290 if (bp_getfile(sock, "gateway", &gateip, buf) == 0) {
291 /* Got it! Parse the netmask. */
292 smask = ip_convertaddr(buf);
294 if (smask) {
295 netmask = smask;
296 #ifdef NETIF_DEBUG
297 if (debug)
298 printf("net_open: subnet mask: %s\n", intoa(netmask));
299 #endif
301 #ifdef NETIF_DEBUG
302 if (gateip.s_addr && debug)
303 printf("net_open: net gateway: %s\n", inet_ntoa(gateip));
304 #endif
306 /* Get the root server and pathname. */
307 if (bp_getfile(sock, "root", &rootip, rootpath)) {
308 printf("net_open: bootparam/getfile RPC failed\n");
309 return (EIO);
311 exit:
312 if ((rootaddr = net_parse_rootpath()) != INADDR_NONE)
313 rootip.s_addr = rootaddr;
315 #ifdef NETIF_DEBUG
316 if (debug) {
317 printf("net_open: server addr: %s\n", inet_ntoa(rootip));
318 printf("net_open: server path: %s\n", rootpath);
320 #endif
322 return (0);
325 static int
326 net_print(int verbose)
328 struct netif_driver *drv;
329 int i, d, cnt;
330 int ret = 0;
332 if (netif_drivers[0] == NULL)
333 return (ret);
335 printf("%s devices:", netdev.dv_name);
336 if ((ret = pager_output("\n")) != 0)
337 return (ret);
339 cnt = 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++);
344 if (verbose) {
345 printf(" (%s%d)", drv->netif_bname,
346 drv->netif_ifs[i].dif_unit);
348 if ((ret = pager_output("\n")) != 0)
349 return (ret);
352 return (ret);
356 * Parses the rootpath if present
358 * The rootpath format can be in the form
359 * <scheme>://IPv4/path
360 * <scheme>:/path
362 * For compatibility with previous behaviour it also accepts as an NFS scheme
363 * IPv4:/path
364 * /path
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.
376 uint32_t
377 net_parse_rootpath(void)
379 n_long addr = htonl(INADDR_NONE);
380 size_t i;
381 char ip[FNAME_SIZE];
382 char *ptr, *val;
384 netproto = NET_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)
389 continue;
391 netproto = uri_schemes[i].proto;
392 break;
394 ptr = rootpath;
395 /* Fallback for compatibility mode */
396 if (netproto == NET_NONE) {
397 if (strcmp(rootpath, "/") == 0) {
398 netproto = NET_TFTP;
399 } else {
400 netproto = NET_NFS;
401 (void) strsep(&ptr, ":");
402 if (ptr != NULL) {
403 addr = inet_addr(rootpath);
404 bcopy(ptr, rootpath, strlen(ptr) + 1);
407 } else {
408 ptr += strlen(uri_schemes[i].scheme);
409 if (*ptr == '/') {
410 /* we are in the form <scheme>://, we do expect an ip */
411 ptr++;
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, '/');
418 if (val == NULL) {
419 /* If no pathname component, default to / */
420 strlcat(rootpath, "/", sizeof (rootpath));
421 val = strchr(ptr, '/');
423 if (val != NULL) {
424 snprintf(ip, sizeof (ip), "%.*s",
425 (int)((uintptr_t)val - (uintptr_t)ptr),
426 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);
433 } else {
434 ptr--;
435 bcopy(ptr, rootpath, strlen(ptr) + 1);
439 return (addr);