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