xref: /freebsd/sys/nfs/nfs_diskless.c (revision 266b0663c598b7e50c2998974c16f89b7ac23e3a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * William Jolitz.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
21  *
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
32  * SUCH DAMAGE.
33  *
34  *	from: @(#)autoconf.c	7.1 (Berkeley) 5/9/91
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include "opt_bootp.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/jail.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/socket.h>
49 
50 #include <net/if.h>
51 #include <net/if_dl.h>
52 #include <net/if_types.h>
53 #include <net/if_var.h>
54 #include <net/ethernet.h>
55 #include <net/vnet.h>
56 
57 #include <netinet/in.h>
58 #include <nfs/nfsproto.h>
59 #include <nfsclient/nfs.h>
60 #include <nfs/nfsdiskless.h>
61 
62 #define	NFS_IFACE_TIMEOUT_SECS	10 /* Timeout for interface to appear. */
63 
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);
67 
68 /*
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).
72  */
73 struct nfs_diskless	nfs_diskless = { { { 0 } } };
74 struct nfsv3_diskless	nfsv3_diskless = { { { 0 } } };
75 int			nfs_diskless_valid = 0;
76 
77 /*
78  * Validate/sanity check a rsize/wsize parameter.
79  */
80 static int
81 checkrwsize(unsigned long v, const char *name)
82 {
83 	/*
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
87 	 * much arbitrary.
88 	 */
89 	if (!(4 <= v && v <= 32*1024)) {
90 		printf("nfs_parse_options: invalid %s %lu ignored\n", name, v);
91 		return 0;
92 	} else
93 		return 1;
94 }
95 
96 /*
97  * Parse mount options and apply them to the supplied
98  * nfs_diskless state.  Used also by bootp/dhcp support.
99  */
100 void
101 nfs_parse_options(const char *envopts, struct nfs_args *nd)
102 {
103 	char *opts, *o, *otmp;
104 	unsigned long v;
105 
106 	opts = strdup(envopts, M_TEMP);
107 	otmp = opts;
108 	while ((o = strsep(&otmp, ":;, ")) != NULL) {
109 		if (*o == '\0')
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")) {
133 				nd->rsize = (int) v;
134 				nd->flags |= NFSMNT_RSIZE;
135 			}
136 		} else if (strncmp(o, "wsize=", 6) == 0) {
137 			v = strtoul(o+6, NULL, 10);
138 			if (checkrwsize(v, "wsize")) {
139 				nd->wsize = (int) v;
140 				nd->flags |= NFSMNT_WSIZE;
141 			}
142 		} else
143 			printf("%s: skipping unknown option \"%s\"\n",
144 			    __func__, o);
145 	}
146 	free(opts, M_TEMP);
147 }
148 
149 static u_int
150 nfs_setup_diskless_ifa_cb(void *arg, struct sockaddr_dl *sdl, u_int count)
151 {
152 	struct sockaddr_dl *ourdl = arg;
153 
154 	if ((sdl->sdl_type == ourdl->sdl_type) &&
155 	    (sdl->sdl_alen == ourdl->sdl_alen) &&
156 	    !bcmp(LLADDR(sdl), LLADDR(ourdl), sdl->sdl_alen))
157 		return (1);
158 
159 	return (0);
160 }
161 
162 /*
163  * Populate the essential fields in the nfsv3_diskless structure.
164  *
165  * The loader is expected to export the following environment variables:
166  *
167  * boot.netif.name		name of boot interface
168  * boot.netif.ip		IP address on boot interface
169  * boot.netif.netmask		netmask on boot interface
170  * boot.netif.gateway		default gateway (optional)
171  * boot.netif.hwaddr		hardware address of boot interface
172  * boot.netif.mtu		interface mtu from bootp/dhcp (optional)
173  * boot.nfsroot.server		IP address of root filesystem server
174  * boot.nfsroot.path		path of the root filesystem on server
175  * boot.nfsroot.nfshandle	NFS handle for root filesystem on server
176  * boot.nfsroot.nfshandlelen	and length of this handle (for NFSv3 only)
177  * boot.nfsroot.options		NFS options for the root filesystem
178  */
179 void
180 nfs_setup_diskless(void)
181 {
182 	struct epoch_tracker et;
183 	struct if_iter iter;
184 	struct nfs_diskless *nd = &nfs_diskless;
185 	struct nfsv3_diskless *nd3 = &nfsv3_diskless;
186 	if_t ifp;
187 	struct sockaddr_dl ourdl;
188 	struct sockaddr_in myaddr, netmask;
189 	char *cp;
190 	int cnt, fhlen, is_nfsv3;
191 	uint32_t len;
192 	time_t timeout_at;
193 
194 	if (nfs_diskless_valid != 0)
195 		return;
196 
197 	/* get handle size. If this succeeds, it's an NFSv3 setup. */
198 	if ((cp = kern_getenv("boot.nfsroot.nfshandlelen")) != NULL) {
199 		cnt = sscanf(cp, "%d", &len);
200 		freeenv(cp);
201 		if (cnt != 1 || len == 0 || len > NFSX_V3FHMAX) {
202 			printf("nfs_diskless: bad NFS handle len\n");
203 			return;
204 		}
205 		nd3->root_fhsize = len;
206 		is_nfsv3 = 1;
207 	} else
208 		is_nfsv3 = 0;
209 	/* set up interface */
210 	if (inaddr_to_sockaddr("boot.netif.ip", &myaddr))
211 		return;
212 	if (inaddr_to_sockaddr("boot.netif.netmask", &netmask)) {
213 		printf("nfs_diskless: no netmask\n");
214 		return;
215 	}
216 	if (is_nfsv3 != 0) {
217 		bcopy(&myaddr, &nd3->myif.ifra_addr, sizeof(myaddr));
218 		bcopy(&myaddr, &nd3->myif.ifra_broadaddr, sizeof(myaddr));
219 		((struct sockaddr_in *)
220 		   &nd3->myif.ifra_broadaddr)->sin_addr.s_addr =
221 		    myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
222 		bcopy(&netmask, &nd3->myif.ifra_mask, sizeof(netmask));
223 	} else {
224 		bcopy(&myaddr, &nd->myif.ifra_addr, sizeof(myaddr));
225 		bcopy(&myaddr, &nd->myif.ifra_broadaddr, sizeof(myaddr));
226 		((struct sockaddr_in *)
227 		   &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
228 		    myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
229 		bcopy(&netmask, &nd->myif.ifra_mask, sizeof(netmask));
230 	}
231 
232 	if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl)) {
233 		printf("nfs_diskless: no hardware address\n");
234 		return;
235 	}
236 	timeout_at = time_uptime + NFS_IFACE_TIMEOUT_SECS;
237 retry:
238 	CURVNET_SET(TD_TO_VNET(curthread));
239 	NET_EPOCH_ENTER(et);
240 	for (ifp = if_iter_start(&iter); ifp != NULL; ifp = if_iter_next(&iter)) {
241 		cnt = if_foreach_lladdr(ifp, nfs_setup_diskless_ifa_cb, &ourdl);
242 		if (cnt > 0)
243 			break;
244 	}
245 	if_iter_finish(&iter);
246 	NET_EPOCH_EXIT(et);
247 	CURVNET_RESTORE();
248 	if (ifp != NULL)
249 		goto match_done;
250 
251 	if (time_uptime < timeout_at) {
252 		pause("nfssdl", hz / 5);
253 		goto retry;
254 	}
255 	printf("nfs_diskless: no interface\n");
256 	return;	/* no matching interface */
257 match_done:
258 	kern_setenv("boot.netif.name", if_name(ifp));
259 	if (is_nfsv3 != 0) {
260 		strlcpy(nd3->myif.ifra_name, if_name(ifp),
261 		    sizeof(nd3->myif.ifra_name));
262 
263 		/* set up gateway */
264 		inaddr_to_sockaddr("boot.netif.gateway", &nd3->mygateway);
265 
266 		/* set up root mount */
267 		nd3->root_args.rsize = 32768;		/* XXX tunable? */
268 		nd3->root_args.wsize = 32768;
269 		nd3->root_args.sotype = SOCK_STREAM;
270 		nd3->root_args.flags = (NFSMNT_NFSV3 | NFSMNT_WSIZE |
271 		    NFSMNT_RSIZE | NFSMNT_RESVPORT);
272 		if (inaddr_to_sockaddr("boot.nfsroot.server",
273 		    &nd3->root_saddr)) {
274 			printf("nfs_diskless: no server\n");
275 			return;
276 		}
277 		nd3->root_saddr.sin_port = htons(NFS_PORT);
278 		fhlen = decode_nfshandle("boot.nfsroot.nfshandle",
279 		    &nd3->root_fh[0], NFSX_V3FHMAX);
280 		if (fhlen == 0) {
281 			printf("nfs_diskless: no NFS handle\n");
282 			return;
283 		}
284 		if (fhlen != nd3->root_fhsize) {
285 			printf("nfs_diskless: bad NFS handle len=%d\n", fhlen);
286 			return;
287 		}
288 		if ((cp = kern_getenv("boot.nfsroot.path")) != NULL) {
289 			strncpy(nd3->root_hostnam, cp, MNAMELEN - 1);
290 			freeenv(cp);
291 		}
292 		if ((cp = kern_getenv("boot.nfsroot.options")) != NULL) {
293 			nfs_parse_options(cp, &nd3->root_args);
294 			freeenv(cp);
295 		}
296 
297 		nfs_diskless_valid = 3;
298 	} else {
299 		strlcpy(nd->myif.ifra_name, if_name(ifp),
300 		    sizeof(nd->myif.ifra_name));
301 
302 		/* set up gateway */
303 		inaddr_to_sockaddr("boot.netif.gateway", &nd->mygateway);
304 
305 		/* set up root mount */
306 		nd->root_args.rsize = 8192;		/* XXX tunable? */
307 		nd->root_args.wsize = 8192;
308 		nd->root_args.sotype = SOCK_STREAM;
309 		nd->root_args.flags = (NFSMNT_WSIZE |
310 		    NFSMNT_RSIZE | NFSMNT_RESVPORT);
311 		if (inaddr_to_sockaddr("boot.nfsroot.server",
312 		    &nd->root_saddr)) {
313 			printf("nfs_diskless: no server\n");
314 			return;
315 		}
316 		nd->root_saddr.sin_port = htons(NFS_PORT);
317 		if (decode_nfshandle("boot.nfsroot.nfshandle",
318 		    &nd->root_fh[0], NFSX_V2FH) == 0) {
319 			printf("nfs_diskless: no NFS handle\n");
320 			return;
321 		}
322 		if ((cp = kern_getenv("boot.nfsroot.path")) != NULL) {
323 			strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
324 			freeenv(cp);
325 		}
326 		if ((cp = kern_getenv("boot.nfsroot.options")) != NULL) {
327 			struct nfs_args args;
328 
329 			/*
330 			 * XXX yech, convert between old and current
331 			 * arg format
332 			 */
333 			args.flags = nd->root_args.flags;
334 			args.sotype = nd->root_args.sotype;
335 			args.rsize = nd->root_args.rsize;
336 			args.wsize = nd->root_args.wsize;
337 			nfs_parse_options(cp, &args);
338 			nd->root_args.flags = args.flags;
339 			nd->root_args.sotype = args.sotype;
340 			nd->root_args.rsize = args.rsize;
341 			nd->root_args.wsize = args.wsize;
342 			freeenv(cp);
343 		}
344 
345 		nfs_diskless_valid = 1;
346 	}
347 }
348 
349 static int
350 inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
351 {
352 	u_int32_t a[4];
353 	char *cp;
354 	int count;
355 
356 	bzero(sa, sizeof(*sa));
357 	sa->sin_len = sizeof(*sa);
358 	sa->sin_family = AF_INET;
359 
360 	if ((cp = kern_getenv(ev)) == NULL)
361 		return (1);
362 	count = sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
363 	freeenv(cp);
364 	if (count != 4)
365 		return (1);
366 	sa->sin_addr.s_addr =
367 	    htonl((a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3]);
368 	return (0);
369 }
370 
371 static int
372 hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
373 {
374 	char *cp;
375 	u_int32_t a[6];
376 	int count;
377 
378 	bzero(sa, sizeof(*sa));
379 	sa->sdl_len = sizeof(*sa);
380 	sa->sdl_family = AF_LINK;
381 	sa->sdl_type = IFT_ETHER;
382 	sa->sdl_alen = ETHER_ADDR_LEN;
383 	if ((cp = kern_getenv(ev)) == NULL)
384 		return (1);
385 	count = sscanf(cp, "%x:%x:%x:%x:%x:%x",
386 	    &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
387 	freeenv(cp);
388 	if (count != 6)
389 		return (1);
390 	sa->sdl_data[0] = a[0];
391 	sa->sdl_data[1] = a[1];
392 	sa->sdl_data[2] = a[2];
393 	sa->sdl_data[3] = a[3];
394 	sa->sdl_data[4] = a[4];
395 	sa->sdl_data[5] = a[5];
396 	return (0);
397 }
398 
399 static int
400 decode_nfshandle(char *ev, u_char *fh, int maxfh)
401 {
402 	u_char *cp, *ep;
403 	int len, val;
404 
405 	ep = cp = kern_getenv(ev);
406 	if (cp == NULL)
407 		return (0);
408 	if ((strlen(cp) < 2) || (*cp != 'X')) {
409 		freeenv(ep);
410 		return (0);
411 	}
412 	len = 0;
413 	cp++;
414 	for (;;) {
415 		if (*cp == 'X') {
416 			freeenv(ep);
417 			return (len);
418 		}
419 		if ((sscanf(cp, "%2x", &val) != 1) || (val > 0xff)) {
420 			freeenv(ep);
421 			return (0);
422 		}
423 		*(fh++) = val;
424 		len++;
425 		cp += 2;
426 		if (len > maxfh) {
427 		    freeenv(ep);
428 		    return (0);
429 		}
430 	}
431 }
432 
433 #if !defined(BOOTP_NFSROOT)
434 static void
435 nfs_rootconf(void)
436 {
437 
438 	nfs_setup_diskless();
439 	if (nfs_diskless_valid)
440 		rootdevnames[0] = "nfs:";
441 }
442 
443 SYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, nfs_rootconf, NULL);
444 #endif
445