2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990 The Regents of the University of California.
7 * This code is derived from software contributed to Berkeley by
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.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * from: @(#)autoconf.c 7.1 (Berkeley) 5/9/91
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_bootp.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/socket.h>
51 #include <net/if_dl.h>
52 #include <net/if_types.h>
53 #include <net/if_var.h>
54 #include <net/ethernet.h>
57 #include <netinet/in.h>
58 #include <nfs/nfsproto.h>
59 #include <nfsclient/nfs.h>
60 #include <nfs/nfsdiskless.h>
62 #define NFS_IFACE_TIMEOUT_SECS 10 /* Timeout for interface to appear. */
64 static int inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa);
65 static int hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa);
66 static int decode_nfshandle(char *ev, u_char *fh, int maxfh);
69 * This structure must be filled in by a primary bootstrap or bootstrap
70 * server for a diskless/dataless machine. It is initialized below just
71 * to ensure that it is allocated to initialized data (.data not .bss).
73 struct nfs_diskless nfs_diskless = { { { 0 } } };
74 struct nfsv3_diskless nfsv3_diskless = { { { 0 } } };
75 int nfs_diskless_valid = 0;
78 * Validate/sanity check a rsize/wsize parameter.
81 checkrwsize(unsigned long v, const char *name)
84 * 32K is used as an upper bound because most servers
85 * limit block size to satisfy IPv4's limit of
86 * 64K/reassembled packet. The lower bound is pretty
89 if (!(4 <= v && v <= 32*1024)) {
90 printf("nfs_parse_options: invalid %s %lu ignored\n", name, v);
97 * Parse mount options and apply them to the supplied
98 * nfs_diskless state. Used also by bootp/dhcp support.
101 nfs_parse_options(const char *envopts, struct nfs_args *nd)
103 char *opts, *o, *otmp;
106 opts = strdup(envopts, M_TEMP);
108 while ((o = strsep(&otmp, ":;, ")) != NULL) {
110 ; /* Skip empty options. */
111 else if (strcmp(o, "soft") == 0)
112 nd->flags |= NFSMNT_SOFT;
113 else if (strcmp(o, "intr") == 0)
114 nd->flags |= NFSMNT_INT;
115 else if (strcmp(o, "conn") == 0)
116 nd->flags |= NFSMNT_NOCONN;
117 else if (strcmp(o, "nolockd") == 0)
118 nd->flags |= NFSMNT_NOLOCKD;
119 else if (strcmp(o, "nocto") == 0)
120 nd->flags |= NFSMNT_NOCTO;
121 else if (strcmp(o, "nfsv2") == 0)
122 nd->flags &= ~(NFSMNT_NFSV3 | NFSMNT_NFSV4);
123 else if (strcmp(o, "nfsv3") == 0) {
124 nd->flags &= ~NFSMNT_NFSV4;
125 nd->flags |= NFSMNT_NFSV3;
126 } else if (strcmp(o, "tcp") == 0)
127 nd->sotype = SOCK_STREAM;
128 else if (strcmp(o, "udp") == 0)
129 nd->sotype = SOCK_DGRAM;
130 else if (strncmp(o, "rsize=", 6) == 0) {
131 v = strtoul(o+6, NULL, 10);
132 if (checkrwsize(v, "rsize")) {
134 nd->flags |= NFSMNT_RSIZE;
136 } else if (strncmp(o, "wsize=", 6) == 0) {
137 v = strtoul(o+6, NULL, 10);
138 if (checkrwsize(v, "wsize")) {
140 nd->flags |= NFSMNT_WSIZE;
143 printf("%s: skipping unknown option \"%s\"\n",
150 * Populate the essential fields in the nfsv3_diskless structure.
152 * The loader is expected to export the following environment variables:
154 * boot.netif.name name of boot interface
155 * boot.netif.ip IP address on boot interface
156 * boot.netif.netmask netmask on boot interface
157 * boot.netif.gateway default gateway (optional)
158 * boot.netif.hwaddr hardware address of boot interface
159 * boot.netif.mtu interface mtu from bootp/dhcp (optional)
160 * boot.nfsroot.server IP address of root filesystem server
161 * boot.nfsroot.path path of the root filesystem on server
162 * boot.nfsroot.nfshandle NFS handle for root filesystem on server
163 * boot.nfsroot.nfshandlelen and length of this handle (for NFSv3 only)
164 * boot.nfsroot.options NFS options for the root filesystem
167 nfs_setup_diskless(void)
169 struct nfs_diskless *nd = &nfs_diskless;
170 struct nfsv3_diskless *nd3 = &nfsv3_diskless;
173 struct sockaddr_dl *sdl, ourdl;
174 struct sockaddr_in myaddr, netmask;
176 int cnt, fhlen, is_nfsv3;
180 if (nfs_diskless_valid != 0)
183 /* get handle size. If this succeeds, it's an NFSv3 setup. */
184 if ((cp = kern_getenv("boot.nfsroot.nfshandlelen")) != NULL) {
185 cnt = sscanf(cp, "%d", &len);
187 if (cnt != 1 || len == 0 || len > NFSX_V3FHMAX) {
188 printf("nfs_diskless: bad NFS handle len\n");
191 nd3->root_fhsize = len;
195 /* set up interface */
196 if (inaddr_to_sockaddr("boot.netif.ip", &myaddr))
198 if (inaddr_to_sockaddr("boot.netif.netmask", &netmask)) {
199 printf("nfs_diskless: no netmask\n");
203 bcopy(&myaddr, &nd3->myif.ifra_addr, sizeof(myaddr));
204 bcopy(&myaddr, &nd3->myif.ifra_broadaddr, sizeof(myaddr));
205 ((struct sockaddr_in *)
206 &nd3->myif.ifra_broadaddr)->sin_addr.s_addr =
207 myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
208 bcopy(&netmask, &nd3->myif.ifra_mask, sizeof(netmask));
210 bcopy(&myaddr, &nd->myif.ifra_addr, sizeof(myaddr));
211 bcopy(&myaddr, &nd->myif.ifra_broadaddr, sizeof(myaddr));
212 ((struct sockaddr_in *)
213 &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
214 myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
215 bcopy(&netmask, &nd->myif.ifra_mask, sizeof(netmask));
218 if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl)) {
219 printf("nfs_diskless: no hardware address\n");
223 timeout_at = time_uptime + NFS_IFACE_TIMEOUT_SECS;
225 CURVNET_SET(TD_TO_VNET(curthread));
227 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
228 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
229 if (ifa->ifa_addr->sa_family == AF_LINK) {
230 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
231 if ((sdl->sdl_type == ourdl.sdl_type) &&
232 (sdl->sdl_alen == ourdl.sdl_alen) &&
245 if (time_uptime < timeout_at) {
246 pause("nfssdl", hz / 5);
249 printf("nfs_diskless: no interface\n");
250 return; /* no matching interface */
252 kern_setenv("boot.netif.name", ifp->if_xname);
254 strlcpy(nd3->myif.ifra_name, ifp->if_xname,
255 sizeof(nd3->myif.ifra_name));
258 inaddr_to_sockaddr("boot.netif.gateway", &nd3->mygateway);
260 /* set up root mount */
261 nd3->root_args.rsize = 32768; /* XXX tunable? */
262 nd3->root_args.wsize = 32768;
263 nd3->root_args.sotype = SOCK_STREAM;
264 nd3->root_args.flags = (NFSMNT_NFSV3 | NFSMNT_WSIZE |
265 NFSMNT_RSIZE | NFSMNT_RESVPORT);
266 if (inaddr_to_sockaddr("boot.nfsroot.server",
268 printf("nfs_diskless: no server\n");
271 nd3->root_saddr.sin_port = htons(NFS_PORT);
272 fhlen = decode_nfshandle("boot.nfsroot.nfshandle",
273 &nd3->root_fh[0], NFSX_V3FHMAX);
275 printf("nfs_diskless: no NFS handle\n");
278 if (fhlen != nd3->root_fhsize) {
279 printf("nfs_diskless: bad NFS handle len=%d\n", fhlen);
282 if ((cp = kern_getenv("boot.nfsroot.path")) != NULL) {
283 strncpy(nd3->root_hostnam, cp, MNAMELEN - 1);
286 if ((cp = kern_getenv("boot.nfsroot.options")) != NULL) {
287 nfs_parse_options(cp, &nd3->root_args);
291 nfs_diskless_valid = 3;
293 strlcpy(nd->myif.ifra_name, ifp->if_xname,
294 sizeof(nd->myif.ifra_name));
297 inaddr_to_sockaddr("boot.netif.gateway", &nd->mygateway);
299 /* set up root mount */
300 nd->root_args.rsize = 8192; /* XXX tunable? */
301 nd->root_args.wsize = 8192;
302 nd->root_args.sotype = SOCK_STREAM;
303 nd->root_args.flags = (NFSMNT_WSIZE |
304 NFSMNT_RSIZE | NFSMNT_RESVPORT);
305 if (inaddr_to_sockaddr("boot.nfsroot.server",
307 printf("nfs_diskless: no server\n");
310 nd->root_saddr.sin_port = htons(NFS_PORT);
311 if (decode_nfshandle("boot.nfsroot.nfshandle",
312 &nd->root_fh[0], NFSX_V2FH) == 0) {
313 printf("nfs_diskless: no NFS handle\n");
316 if ((cp = kern_getenv("boot.nfsroot.path")) != NULL) {
317 strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
320 if ((cp = kern_getenv("boot.nfsroot.options")) != NULL) {
321 struct nfs_args args;
324 * XXX yech, convert between old and current
327 args.flags = nd->root_args.flags;
328 args.sotype = nd->root_args.sotype;
329 args.rsize = nd->root_args.rsize;
330 args.wsize = nd->root_args.wsize;
331 nfs_parse_options(cp, &args);
332 nd->root_args.flags = args.flags;
333 nd->root_args.sotype = args.sotype;
334 nd->root_args.rsize = args.rsize;
335 nd->root_args.wsize = args.wsize;
339 nfs_diskless_valid = 1;
344 inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
350 bzero(sa, sizeof(*sa));
351 sa->sin_len = sizeof(*sa);
352 sa->sin_family = AF_INET;
354 if ((cp = kern_getenv(ev)) == NULL)
356 count = sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
360 sa->sin_addr.s_addr =
361 htonl((a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3]);
366 hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
372 bzero(sa, sizeof(*sa));
373 sa->sdl_len = sizeof(*sa);
374 sa->sdl_family = AF_LINK;
375 sa->sdl_type = IFT_ETHER;
376 sa->sdl_alen = ETHER_ADDR_LEN;
377 if ((cp = kern_getenv(ev)) == NULL)
379 count = sscanf(cp, "%x:%x:%x:%x:%x:%x",
380 &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
384 sa->sdl_data[0] = a[0];
385 sa->sdl_data[1] = a[1];
386 sa->sdl_data[2] = a[2];
387 sa->sdl_data[3] = a[3];
388 sa->sdl_data[4] = a[4];
389 sa->sdl_data[5] = a[5];
394 decode_nfshandle(char *ev, u_char *fh, int maxfh)
399 ep = cp = kern_getenv(ev);
402 if ((strlen(cp) < 2) || (*cp != 'X')) {
413 if ((sscanf(cp, "%2x", &val) != 1) || (val > 0xff)) {
427 #if !defined(BOOTP_NFSROOT)
432 nfs_setup_diskless();
433 if (nfs_diskless_valid)
434 rootdevnames[0] = "nfs:";
437 SYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, nfs_rootconf, NULL);