xref: /netbsd-src/sys/fs/nfs/common/nfs_diskless.c (revision 88b1d6a671d2140f122be1da1cac398023579045)
1 /*	$NetBSD: nfs_diskless.c,v 1.2 2016/12/13 22:41:46 pgoyette Exp $	*/
2 /*-
3  * Copyright (c) 1990 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * William Jolitz.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	from: @(#)autoconf.c	7.1 (Berkeley) 5/9/91
34  */
35 
36 #include <sys/cdefs.h>
37 /* __FBSDID("FreeBSD: head/sys/nfs/nfs_diskless.c 297086 2016-03-20 21:48:26Z ian "); */
38 __RCSID("$NetBSD: nfs_diskless.c,v 1.2 2016/12/13 22:41:46 pgoyette Exp $");
39 
40 #ifdef _KERNEL_OPT
41 #include "opt_newnfs.h"
42 #endif
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/jail.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/socket.h>
51 
52 #include <net/if.h>
53 #include <net/if_dl.h>
54 #include <net/if_types.h>
55 
56 #include <net/if_var.h>
57 #include <net/ethernet.h>
58 #include <net/vnet.h>
59 
60 #include <netinet/in.h>
61 
62 #include <fs/nfs/common/nfsproto.h>
63 #include <fs/nfs/client/nfs.h>
64 #include <fs/nfs/common/nfsdiskless.h>
65 
66 #define	NFS_IFACE_TIMEOUT_SECS	10 /* Timeout for interface to appear. */
67 
68 static int inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa);
69 static int hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa);
70 static int decode_nfshandle(char *ev, u_char *fh, int maxfh);
71 
72 /*
73  * This structure must be filled in by a primary bootstrap or bootstrap
74  * server for a diskless/dataless machine. It is initialized below just
75  * to ensure that it is allocated to initialized data (.data not .bss).
76  */
77 struct nfs_diskless	nfs_diskless = { { { 0 } } };
78 struct nfsv3_diskless	nfsv3_diskless = { { { 0 } } };
79 int			nfs_diskless_valid = 0;
80 
81 /*
82  * Validate/sanity check a rsize/wsize parameter.
83  */
84 static int
checkrwsize(unsigned long v,const char * name)85 checkrwsize(unsigned long v, const char *name)
86 {
87 	/*
88 	 * 32K is used as an upper bound because most servers
89 	 * limit block size to satisfy IPv4's limit of
90 	 * 64K/reassembled packet.  The lower bound is pretty
91 	 * much arbitrary.
92 	 */
93 	if (!(4 <= v && v <= 32*1024)) {
94 		printf("nfs_parse_options: invalid %s %lu ignored\n", name, v);
95 		return 0;
96 	} else
97 		return 1;
98 }
99 
100 /*
101  * Parse mount options and apply them to the supplied
102  * nfs_diskless state.  Used also by bootp/dhcp support.
103  */
104 void
nfs_parse_options(const char * envopts,struct nfs_args * nd)105 nfs_parse_options(const char *envopts, struct nfs_args *nd)
106 {
107 	char *opts, *o, *otmp;
108 	unsigned long v;
109 
110 	opts = strdup(envopts, M_TEMP);
111 	otmp = opts;
112 	while ((o = strsep(&otmp, ":;, ")) != NULL) {
113 		if (*o == '\0')
114 			; /* Skip empty options. */
115 		else if (strcmp(o, "soft") == 0)
116 			nd->flags |= NFSMNT_SOFT;
117 		else if (strcmp(o, "intr") == 0)
118 			nd->flags |= NFSMNT_INT;
119 		else if (strcmp(o, "conn") == 0)
120 			nd->flags |= NFSMNT_NOCONN;
121 		else if (strcmp(o, "nolockd") == 0)
122 			nd->flags |= NFSMNT_NOLOCKD;
123 		else if (strcmp(o, "nocto") == 0)
124 			nd->flags |= NFSMNT_NOCTO;
125 		else if (strcmp(o, "nfsv2") == 0)
126 			nd->flags &= ~(NFSMNT_NFSV3 | NFSMNT_NFSV4);
127 		else if (strcmp(o, "nfsv3") == 0) {
128 			nd->flags &= ~NFSMNT_NFSV4;
129 			nd->flags |= NFSMNT_NFSV3;
130 		} else if (strcmp(o, "tcp") == 0)
131 			nd->sotype = SOCK_STREAM;
132 		else if (strcmp(o, "udp") == 0)
133 			nd->sotype = SOCK_DGRAM;
134 		else if (strncmp(o, "rsize=", 6) == 0) {
135 			v = strtoul(o+6, NULL, 10);
136 			if (checkrwsize(v, "rsize")) {
137 				nd->rsize = (int) v;
138 				nd->flags |= NFSMNT_RSIZE;
139 			}
140 		} else if (strncmp(o, "wsize=", 6) == 0) {
141 			v = strtoul(o+6, NULL, 10);
142 			if (checkrwsize(v, "wsize")) {
143 				nd->wsize = (int) v;
144 				nd->flags |= NFSMNT_WSIZE;
145 			}
146 		} else
147 			printf("%s: skipping unknown option \"%s\"\n",
148 			    __func__, o);
149 	}
150 	free(opts, M_TEMP);
151 }
152 
153 /*
154  * Populate the essential fields in the nfsv3_diskless structure.
155  *
156  * The loader is expected to export the following environment variables:
157  *
158  * boot.netif.name		name of boot interface
159  * boot.netif.ip		IP address on boot interface
160  * boot.netif.netmask		netmask on boot interface
161  * boot.netif.gateway		default gateway (optional)
162  * boot.netif.hwaddr		hardware address of boot interface
163  * boot.netif.mtu		interface mtu from bootp/dhcp (optional)
164  * boot.nfsroot.server		IP address of root filesystem server
165  * boot.nfsroot.path		path of the root filesystem on server
166  * boot.nfsroot.nfshandle	NFS handle for root filesystem on server
167  * boot.nfsroot.nfshandlelen	and length of this handle (for NFSv3 only)
168  * boot.nfsroot.options		NFS options for the root filesystem
169  */
170 void
nfs_setup_diskless(void)171 nfs_setup_diskless(void)
172 {
173 	struct nfs_diskless *nd = &nfs_diskless;
174 	struct nfsv3_diskless *nd3 = &nfsv3_diskless;
175 	struct ifnet *ifp;
176 	struct ifaddr *ifa;
177 	struct sockaddr_dl *sdl, ourdl;
178 	struct sockaddr_in myaddr, netmask;
179 	char *cp;
180 	int cnt, fhlen, is_nfsv3;
181 	uint32_t len;
182 	time_t timeout_at;
183 
184 	if (nfs_diskless_valid != 0)
185 		return;
186 
187 	/* get handle size. If this succeeds, it's an NFSv3 setup. */
188 	if ((cp = kern_getenv("boot.nfsroot.nfshandlelen")) != NULL) {
189 		cnt = sscanf(cp, "%d", &len);
190 		freeenv(cp);
191 		if (cnt != 1 || len == 0 || len > NFSX_V3FHMAX) {
192 			printf("nfs_diskless: bad NFS handle len\n");
193 			return;
194 		}
195 		nd3->root_fhsize = len;
196 		is_nfsv3 = 1;
197 	} else
198 		is_nfsv3 = 0;
199 	/* set up interface */
200 	if (inaddr_to_sockaddr("boot.netif.ip", &myaddr))
201 		return;
202 	if (inaddr_to_sockaddr("boot.netif.netmask", &netmask)) {
203 		printf("nfs_diskless: no netmask\n");
204 		return;
205 	}
206 	if (is_nfsv3 != 0) {
207 		bcopy(&myaddr, &nd3->myif.ifra_addr, sizeof(myaddr));
208 		bcopy(&myaddr, &nd3->myif.ifra_broadaddr, sizeof(myaddr));
209 		((struct sockaddr_in *)
210 		   &nd3->myif.ifra_broadaddr)->sin_addr.s_addr =
211 		    myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
212 		bcopy(&netmask, &nd3->myif.ifra_mask, sizeof(netmask));
213 	} else {
214 		bcopy(&myaddr, &nd->myif.ifra_addr, sizeof(myaddr));
215 		bcopy(&myaddr, &nd->myif.ifra_broadaddr, sizeof(myaddr));
216 		((struct sockaddr_in *)
217 		   &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
218 		    myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
219 		bcopy(&netmask, &nd->myif.ifra_mask, sizeof(netmask));
220 	}
221 
222 	if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl)) {
223 		printf("nfs_diskless: no hardware address\n");
224 		return;
225 	}
226 	ifa = NULL;
227 	timeout_at = time_uptime + NFS_IFACE_TIMEOUT_SECS;
228 retry:
229 	CURVNET_SET(TD_TO_VNET(curthread));
230 	IFNET_RLOCK();
231 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
232 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
233 			if (ifa->ifa_addr->sa_family == AF_LINK) {
234 				sdl = (struct sockaddr_dl *)ifa->ifa_addr;
235 				if ((sdl->sdl_type == ourdl.sdl_type) &&
236 				    (sdl->sdl_alen == ourdl.sdl_alen) &&
237 				    !bcmp(LLADDR(sdl),
238 					  LLADDR(&ourdl),
239 					  sdl->sdl_alen)) {
240 				    IFNET_RUNLOCK();
241 				    CURVNET_RESTORE();
242 				    goto match_done;
243 				}
244 			}
245 		}
246 	}
247 	IFNET_RUNLOCK();
248 	CURVNET_RESTORE();
249 	if (time_uptime < timeout_at) {
250 		pause("nfssdl", hz / 5);
251 		goto retry;
252 	}
253 	printf("nfs_diskless: no interface\n");
254 	return;	/* no matching interface */
255 match_done:
256 	kern_setenv("boot.netif.name", ifp->if_xname);
257 	if (is_nfsv3 != 0) {
258 		strlcpy(nd3->myif.ifra_name, ifp->if_xname,
259 		    sizeof(nd3->myif.ifra_name));
260 
261 		/* set up gateway */
262 		inaddr_to_sockaddr("boot.netif.gateway", &nd3->mygateway);
263 
264 		/* set up root mount */
265 		nd3->root_args.rsize = 32768;		/* XXX tunable? */
266 		nd3->root_args.wsize = 32768;
267 		nd3->root_args.sotype = SOCK_STREAM;
268 		nd3->root_args.flags = (NFSMNT_NFSV3 | NFSMNT_WSIZE |
269 		    NFSMNT_RSIZE | NFSMNT_RESVPORT);
270 		if (inaddr_to_sockaddr("boot.nfsroot.server",
271 		    &nd3->root_saddr)) {
272 			printf("nfs_diskless: no server\n");
273 			return;
274 		}
275 		nd3->root_saddr.sin_port = htons(NFS_PORT);
276 		fhlen = decode_nfshandle("boot.nfsroot.nfshandle",
277 		    &nd3->root_fh[0], NFSX_V3FHMAX);
278 		if (fhlen == 0) {
279 			printf("nfs_diskless: no NFS handle\n");
280 			return;
281 		}
282 		if (fhlen != nd3->root_fhsize) {
283 			printf("nfs_diskless: bad NFS handle len=%d\n", fhlen);
284 			return;
285 		}
286 		if ((cp = kern_getenv("boot.nfsroot.path")) != NULL) {
287 			strncpy(nd3->root_hostnam, cp, MNAMELEN - 1);
288 			freeenv(cp);
289 		}
290 		if ((cp = kern_getenv("boot.nfsroot.options")) != NULL) {
291 			nfs_parse_options(cp, &nd3->root_args);
292 			freeenv(cp);
293 		}
294 
295 		nfs_diskless_valid = 3;
296 	} else {
297 		strlcpy(nd->myif.ifra_name, ifp->if_xname,
298 		    sizeof(nd->myif.ifra_name));
299 
300 		/* set up gateway */
301 		inaddr_to_sockaddr("boot.netif.gateway", &nd->mygateway);
302 
303 		/* set up root mount */
304 		nd->root_args.rsize = 8192;		/* XXX tunable? */
305 		nd->root_args.wsize = 8192;
306 		nd->root_args.sotype = SOCK_STREAM;
307 		nd->root_args.flags = (NFSMNT_WSIZE |
308 		    NFSMNT_RSIZE | NFSMNT_RESVPORT);
309 		if (inaddr_to_sockaddr("boot.nfsroot.server",
310 		    &nd->root_saddr)) {
311 			printf("nfs_diskless: no server\n");
312 			return;
313 		}
314 		nd->root_saddr.sin_port = htons(NFS_PORT);
315 		if (decode_nfshandle("boot.nfsroot.nfshandle",
316 		    &nd->root_fh[0], NFSX_V2FH) == 0) {
317 			printf("nfs_diskless: no NFS handle\n");
318 			return;
319 		}
320 		if ((cp = kern_getenv("boot.nfsroot.path")) != NULL) {
321 			strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
322 			freeenv(cp);
323 		}
324 		if ((cp = kern_getenv("boot.nfsroot.options")) != NULL) {
325 			struct nfs_args args;
326 
327 			/*
328 			 * XXX yech, convert between old and current
329 			 * arg format
330 			 */
331 			args.flags = nd->root_args.flags;
332 			args.sotype = nd->root_args.sotype;
333 			args.rsize = nd->root_args.rsize;
334 			args.wsize = nd->root_args.wsize;
335 			nfs_parse_options(cp, &args);
336 			nd->root_args.flags = args.flags;
337 			nd->root_args.sotype = args.sotype;
338 			nd->root_args.rsize = args.rsize;
339 			nd->root_args.wsize = args.wsize;
340 			freeenv(cp);
341 		}
342 
343 		nfs_diskless_valid = 1;
344 	}
345 }
346 
347 static int
inaddr_to_sockaddr(char * ev,struct sockaddr_in * sa)348 inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
349 {
350 	u_int32_t a[4];
351 	char *cp;
352 	int count;
353 
354 	bzero(sa, sizeof(*sa));
355 	sa->sin_len = sizeof(*sa);
356 	sa->sin_family = AF_INET;
357 
358 	if ((cp = kern_getenv(ev)) == NULL)
359 		return (1);
360 	count = sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
361 	freeenv(cp);
362 	if (count != 4)
363 		return (1);
364 	sa->sin_addr.s_addr =
365 	    htonl((a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3]);
366 	return (0);
367 }
368 
369 static int
hwaddr_to_sockaddr(char * ev,struct sockaddr_dl * sa)370 hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
371 {
372 	char *cp;
373 	u_int32_t a[6];
374 	int count;
375 
376 	bzero(sa, sizeof(*sa));
377 	sa->sdl_len = sizeof(*sa);
378 	sa->sdl_family = AF_LINK;
379 	sa->sdl_type = IFT_ETHER;
380 	sa->sdl_alen = ETHER_ADDR_LEN;
381 	if ((cp = kern_getenv(ev)) == NULL)
382 		return (1);
383 	count = sscanf(cp, "%x:%x:%x:%x:%x:%x",
384 	    &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
385 	freeenv(cp);
386 	if (count != 6)
387 		return (1);
388 	sa->sdl_data[0] = a[0];
389 	sa->sdl_data[1] = a[1];
390 	sa->sdl_data[2] = a[2];
391 	sa->sdl_data[3] = a[3];
392 	sa->sdl_data[4] = a[4];
393 	sa->sdl_data[5] = a[5];
394 	return (0);
395 }
396 
397 static int
decode_nfshandle(char * ev,u_char * fh,int maxfh)398 decode_nfshandle(char *ev, u_char *fh, int maxfh)
399 {
400 	u_char *cp, *ep;
401 	int len, val;
402 
403 	ep = cp = kern_getenv(ev);
404 	if (cp == NULL)
405 		return (0);
406 	if ((strlen(cp) < 2) || (*cp != 'X')) {
407 		freeenv(ep);
408 		return (0);
409 	}
410 	len = 0;
411 	cp++;
412 	for (;;) {
413 		if (*cp == 'X') {
414 			freeenv(ep);
415 			return (len);
416 		}
417 		if ((sscanf(cp, "%2x", &val) != 1) || (val > 0xff)) {
418 			freeenv(ep);
419 			return (0);
420 		}
421 		*(fh++) = val;
422 		len++;
423 		cp += 2;
424 		if (len > maxfh) {
425 		    freeenv(ep);
426 		    return (0);
427 		}
428 	}
429 }
430 
431 #if !defined(NEW_NFS_BOOT_BOOTP)
432 static void
nfs_rootconf(void)433 nfs_rootconf(void)
434 {
435 
436 	nfs_setup_diskless();
437 	if (nfs_diskless_valid)
438 		rootdevnames[0] = "nfs:";
439 }
440 
441 SYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, nfs_rootconf, NULL);
442 #endif
443 
444