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