8797 loader: Support URI scheme for root-path in netbooting
[unleashed.git] / usr / src / boot / sys / boot / common / dev_net.c
blob24e3d9291bfe80a8d2bab707f5de36f51c7b2da3
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.
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>
53 #include <net/if.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
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>
65 #include "dev_net.h"
66 #include "bootstrap.h"
68 #ifdef NETIF_DEBUG
69 int debug = 0;
70 #endif
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 = {
86 "net",
87 DEVT_NET,
88 net_init,
89 net_strategy,
90 net_open,
91 net_close,
92 noioctl,
93 net_print,
94 net_cleanup
97 static struct uri_scheme {
98 const char *scheme;
99 int proto;
100 } uri_schemes[] = {
101 { "tftp:/", NET_TFTP },
102 { "nfs:/", NET_NFS },
105 static int
106 net_init(void)
109 return (0);
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...
117 static int
118 net_open(struct open_file *f, ...)
120 struct iodesc *d;
121 va_list args;
122 char *devname; /* Device part of file name (or NULL). */
123 int error = 0;
125 va_start(args, f);
126 devname = va_arg(args, char *);
127 va_end(args);
129 /* Before opening another interface, close the previous one first. */
130 if (netdev_sock >= 0 && strcmp(devname, netdev_name) != 0)
131 net_cleanup();
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");
140 return (ENXIO);
142 netdev_name = strdup(devname);
143 #ifdef NETIF_DEBUG
144 if (debug)
145 printf("net_open: netif_open() succeeded\n");
146 #endif
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);
155 if (error) {
156 /* getparams makes its own noise */
157 free(netdev_name);
158 netif_close(netdev_sock);
159 netdev_sock = -1;
160 return (error);
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);
178 } else {
179 setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
180 setenv("boot.nfsroot.path", rootpath, 1);
182 if (intf_mtu != 0) {
183 char mtu[16];
184 snprintf(mtu, sizeof (mtu), "%u", intf_mtu);
185 setenv("boot.netif.mtu", mtu, 1);
188 netdev_opens++;
189 f->f_devdata = &netdev_sock;
190 return (error);
193 static int
194 net_close(struct open_file *f)
197 #ifdef NETIF_DEBUG
198 if (debug)
199 printf("net_close: opens=%d\n", netdev_opens);
200 #endif
202 f->f_devdata = NULL;
204 return (0);
207 static void
208 net_cleanup(void)
211 if (netdev_sock >= 0) {
212 #ifdef NETIF_DEBUG
213 if (debug)
214 printf("net_cleanup: calling netif_close()\n");
215 #endif
216 rootip.s_addr = 0;
217 free(netdev_name);
218 netif_close(netdev_sock);
219 netdev_sock = -1;
223 static int
224 net_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf,
225 size_t *rsize)
228 return (EIO);
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.
244 #ifdef SUPPORT_BOOTP
245 int try_bootp = 1;
246 #endif
248 extern n_long ip_convertaddr(char *p);
250 static int
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;
258 #ifdef SUPPORT_BOOTP
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.
265 if (try_bootp) {
266 int rc = -1;
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;
280 d->myip = myip;
282 if (rc < 0)
283 bootp(sock, BOOTP_NONE);
285 if (myip.s_addr != 0)
286 goto exit;
287 #ifdef NETIF_DEBUG
288 if (debug)
289 printf("net_open: BOOTP failed, trying RARP/RPC...\n");
290 #endif
291 #endif
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");
299 return (EIO);
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");
306 return (EIO);
308 #ifdef NETIF_DEBUG
309 if (debug)
310 printf("net_open: client name: %s\n", hostname);
311 #endif
314 * Ignore the gateway from whoami (unreliable).
315 * Use the "gateway" parameter instead.
317 smask = 0;
318 gateip.s_addr = 0;
319 if (bp_getfile(sock, "gateway", &gateip, buf) == 0) {
320 /* Got it! Parse the netmask. */
321 smask = ip_convertaddr(buf);
323 if (smask) {
324 netmask = smask;
325 #ifdef NETIF_DEBUG
326 if (debug)
327 printf("net_open: subnet mask: %s\n", intoa(netmask));
328 #endif
330 #ifdef NETIF_DEBUG
331 if (gateip.s_addr && debug)
332 printf("net_open: net gateway: %s\n", inet_ntoa(gateip));
333 #endif
335 /* Get the root server and pathname. */
336 if (bp_getfile(sock, "root", &rootip, rootpath)) {
337 printf("net_open: bootparam/getfile RPC failed\n");
338 return (EIO);
340 exit:
341 if ((rootaddr = net_parse_rootpath()) != INADDR_NONE)
342 rootip.s_addr = rootaddr;
344 #ifdef NETIF_DEBUG
345 if (debug) {
346 printf("net_open: server addr: %s\n", inet_ntoa(rootip));
347 printf("net_open: server path: %s\n", rootpath);
349 #endif
351 return (0);
354 static int
355 net_print(int verbose)
357 struct netif_driver *drv;
358 int i, d, cnt;
359 int ret = 0;
361 if (netif_drivers[0] == NULL)
362 return (ret);
364 printf("%s devices:", netdev.dv_name);
365 if ((ret = pager_output("\n")) != 0)
366 return (ret);
368 cnt = 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++);
373 if (verbose) {
374 printf(" (%s%d)", drv->netif_bname,
375 drv->netif_ifs[i].dif_unit);
377 if ((ret = pager_output("\n")) != 0)
378 return (ret);
381 return (ret);
385 * Parses the rootpath if present
387 * The rootpath format can be in the form
388 * <scheme>://IPv4/path
389 * <scheme>:/path
391 * For compatibility with previous behaviour it also accepts as an NFS scheme
392 * IPv4:/path
393 * /path
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.
403 uint32_t
404 net_parse_rootpath(void)
406 n_long addr = htonl(INADDR_NONE);
407 size_t i;
408 char ip[FNAME_SIZE];
409 char *ptr, *val;
411 netproto = NET_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)
416 continue;
418 netproto = uri_schemes[i].proto;
419 break;
421 ptr = rootpath;
422 /* Fallback for compatibility mode */
423 if (netproto == NET_NONE) {
424 netproto = NET_NFS;
425 (void) strsep(&ptr, ":");
426 if (ptr != NULL) {
427 addr = inet_addr(rootpath);
428 bcopy(ptr, rootpath, strlen(ptr) + 1);
430 } else {
431 ptr += strlen(uri_schemes[i].scheme);
432 if (*ptr == '/') {
433 /* we are in the form <scheme>://, we do expect an ip */
434 ptr++;
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, '/');
441 if (val != NULL) {
442 snprintf(ip, sizeof (ip), "%.*s",
443 (int)((uintptr_t)val - (uintptr_t)ptr),
444 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);
451 } else {
452 ptr--;
453 bcopy(ptr, rootpath, strlen(ptr) + 1);
457 return (addr);