1 /* $NetBSD: nfs_diskless.c,v 1.1.1.1 2013/09/30 07:19:32 dholland 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 221436 2011-05-04 13:27:45Z ru "); */ 38 __RCSID("$NetBSD: nfs_diskless.c,v 1.1.1.1 2013/09/30 07:19:32 dholland Exp $"); 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 static int inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa); 63 static int hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa); 64 static int decode_nfshandle(char *ev, u_char *fh, int maxfh); 65 66 /* 67 * This structure must be filled in by a primary bootstrap or bootstrap 68 * server for a diskless/dataless machine. It is initialized below just 69 * to ensure that it is allocated to initialized data (.data not .bss). 70 */ 71 struct nfs_diskless nfs_diskless = { { { 0 } } }; 72 struct nfsv3_diskless nfsv3_diskless = { { { 0 } } }; 73 int nfs_diskless_valid = 0; 74 75 /* 76 * Validate/sanity check a rsize/wsize parameter. 77 */ 78 static int 79 checkrwsize(unsigned long v, const char *name) 80 { 81 /* 82 * 32K is used as an upper bound because most servers 83 * limit block size to satisfy IPv4's limit of 84 * 64K/reassembled packet. The lower bound is pretty 85 * much arbitrary. 86 */ 87 if (!(4 <= v && v <= 32*1024)) { 88 printf("nfs_parse_options: invalid %s %lu ignored\n", name, v); 89 return 0; 90 } else 91 return 1; 92 } 93 94 /* 95 * Parse mount options and apply them to the supplied 96 * nfs_diskless state. Used also by bootp/dhcp support. 97 */ 98 void 99 nfs_parse_options(const char *envopts, struct nfs_args *nd) 100 { 101 char *opts, *o, *otmp; 102 unsigned long v; 103 104 opts = strdup(envopts, M_TEMP); 105 otmp = opts; 106 while ((o = strsep(&otmp, ":;, ")) != NULL) { 107 if (*o == '\0') 108 ; /* Skip empty options. */ 109 else if (strcmp(o, "soft") == 0) 110 nd->flags |= NFSMNT_SOFT; 111 else if (strcmp(o, "intr") == 0) 112 nd->flags |= NFSMNT_INT; 113 else if (strcmp(o, "conn") == 0) 114 nd->flags |= NFSMNT_NOCONN; 115 else if (strcmp(o, "nolockd") == 0) 116 nd->flags |= NFSMNT_NOLOCKD; 117 else if (strcmp(o, "nocto") == 0) 118 nd->flags |= NFSMNT_NOCTO; 119 else if (strcmp(o, "nfsv2") == 0) 120 nd->flags &= ~(NFSMNT_NFSV3 | NFSMNT_NFSV4); 121 else if (strcmp(o, "nfsv3") == 0) { 122 nd->flags &= ~NFSMNT_NFSV4; 123 nd->flags |= NFSMNT_NFSV3; 124 } else if (strcmp(o, "tcp") == 0) 125 nd->sotype = SOCK_STREAM; 126 else if (strcmp(o, "udp") == 0) 127 nd->sotype = SOCK_DGRAM; 128 else if (strncmp(o, "rsize=", 6) == 0) { 129 v = strtoul(o+6, NULL, 10); 130 if (checkrwsize(v, "rsize")) { 131 nd->rsize = (int) v; 132 nd->flags |= NFSMNT_RSIZE; 133 } 134 } else if (strncmp(o, "wsize=", 6) == 0) { 135 v = strtoul(o+6, NULL, 10); 136 if (checkrwsize(v, "wsize")) { 137 nd->wsize = (int) v; 138 nd->flags |= NFSMNT_WSIZE; 139 } 140 } else 141 printf("%s: skipping unknown option \"%s\"\n", 142 __func__, o); 143 } 144 free(opts, M_TEMP); 145 } 146 147 /* 148 * Populate the essential fields in the nfsv3_diskless structure. 149 * 150 * The loader is expected to export the following environment variables: 151 * 152 * boot.netif.name name of boot interface 153 * boot.netif.ip IP address on boot interface 154 * boot.netif.netmask netmask on boot interface 155 * boot.netif.gateway default gateway (optional) 156 * boot.netif.hwaddr hardware address of boot interface 157 * boot.nfsroot.server IP address of root filesystem server 158 * boot.nfsroot.path path of the root filesystem on server 159 * boot.nfsroot.nfshandle NFS handle for root filesystem on server 160 * boot.nfsroot.nfshandlelen and length of this handle (for NFSv3 only) 161 * boot.nfsroot.options NFS options for the root filesystem 162 */ 163 void 164 nfs_setup_diskless(void) 165 { 166 struct nfs_diskless *nd = &nfs_diskless; 167 struct nfsv3_diskless *nd3 = &nfsv3_diskless; 168 struct ifnet *ifp; 169 struct ifaddr *ifa; 170 struct sockaddr_dl *sdl, ourdl; 171 struct sockaddr_in myaddr, netmask; 172 char *cp; 173 int cnt, fhlen, is_nfsv3; 174 uint32_t len; 175 176 if (nfs_diskless_valid != 0) 177 return; 178 179 /* get handle size. If this succeeds, it's an NFSv3 setup. */ 180 if ((cp = getenv("boot.nfsroot.nfshandlelen")) != NULL) { 181 cnt = sscanf(cp, "%d", &len); 182 freeenv(cp); 183 if (cnt != 1 || len == 0 || len > NFSX_V3FHMAX) { 184 printf("nfs_diskless: bad NFS handle len\n"); 185 return; 186 } 187 nd3->root_fhsize = len; 188 is_nfsv3 = 1; 189 } else 190 is_nfsv3 = 0; 191 /* set up interface */ 192 if (inaddr_to_sockaddr("boot.netif.ip", &myaddr)) 193 return; 194 if (inaddr_to_sockaddr("boot.netif.netmask", &netmask)) { 195 printf("nfs_diskless: no netmask\n"); 196 return; 197 } 198 if (is_nfsv3 != 0) { 199 bcopy(&myaddr, &nd3->myif.ifra_addr, sizeof(myaddr)); 200 bcopy(&myaddr, &nd3->myif.ifra_broadaddr, sizeof(myaddr)); 201 ((struct sockaddr_in *) 202 &nd3->myif.ifra_broadaddr)->sin_addr.s_addr = 203 myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr; 204 bcopy(&netmask, &nd3->myif.ifra_mask, sizeof(netmask)); 205 } else { 206 bcopy(&myaddr, &nd->myif.ifra_addr, sizeof(myaddr)); 207 bcopy(&myaddr, &nd->myif.ifra_broadaddr, sizeof(myaddr)); 208 ((struct sockaddr_in *) 209 &nd->myif.ifra_broadaddr)->sin_addr.s_addr = 210 myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr; 211 bcopy(&netmask, &nd->myif.ifra_mask, sizeof(netmask)); 212 } 213 214 if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl)) { 215 printf("nfs_diskless: no hardware address\n"); 216 return; 217 } 218 ifa = NULL; 219 CURVNET_SET(TD_TO_VNET(curthread)); 220 IFNET_RLOCK(); 221 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 222 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 223 if (ifa->ifa_addr->sa_family == AF_LINK) { 224 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 225 if ((sdl->sdl_type == ourdl.sdl_type) && 226 (sdl->sdl_alen == ourdl.sdl_alen) && 227 !bcmp(LLADDR(sdl), 228 LLADDR(&ourdl), 229 sdl->sdl_alen)) { 230 IFNET_RUNLOCK(); 231 CURVNET_RESTORE(); 232 goto match_done; 233 } 234 } 235 } 236 } 237 IFNET_RUNLOCK(); 238 CURVNET_RESTORE(); 239 printf("nfs_diskless: no interface\n"); 240 return; /* no matching interface */ 241 match_done: 242 setenv("boot.netif.name", ifp->if_xname); 243 if (is_nfsv3 != 0) { 244 strlcpy(nd3->myif.ifra_name, ifp->if_xname, 245 sizeof(nd3->myif.ifra_name)); 246 247 /* set up gateway */ 248 inaddr_to_sockaddr("boot.netif.gateway", &nd3->mygateway); 249 250 /* set up root mount */ 251 nd3->root_args.rsize = 32768; /* XXX tunable? */ 252 nd3->root_args.wsize = 32768; 253 nd3->root_args.sotype = SOCK_STREAM; 254 nd3->root_args.flags = (NFSMNT_NFSV3 | NFSMNT_WSIZE | 255 NFSMNT_RSIZE | NFSMNT_RESVPORT); 256 if (inaddr_to_sockaddr("boot.nfsroot.server", 257 &nd3->root_saddr)) { 258 printf("nfs_diskless: no server\n"); 259 return; 260 } 261 nd3->root_saddr.sin_port = htons(NFS_PORT); 262 fhlen = decode_nfshandle("boot.nfsroot.nfshandle", 263 &nd3->root_fh[0], NFSX_V3FHMAX); 264 if (fhlen == 0) { 265 printf("nfs_diskless: no NFS handle\n"); 266 return; 267 } 268 if (fhlen != nd3->root_fhsize) { 269 printf("nfs_diskless: bad NFS handle len=%d\n", fhlen); 270 return; 271 } 272 if ((cp = getenv("boot.nfsroot.path")) != NULL) { 273 strncpy(nd3->root_hostnam, cp, MNAMELEN - 1); 274 freeenv(cp); 275 } 276 if ((cp = getenv("boot.nfsroot.options")) != NULL) { 277 nfs_parse_options(cp, &nd3->root_args); 278 freeenv(cp); 279 } 280 281 nfs_diskless_valid = 3; 282 } else { 283 strlcpy(nd->myif.ifra_name, ifp->if_xname, 284 sizeof(nd->myif.ifra_name)); 285 286 /* set up gateway */ 287 inaddr_to_sockaddr("boot.netif.gateway", &nd->mygateway); 288 289 /* set up root mount */ 290 nd->root_args.rsize = 8192; /* XXX tunable? */ 291 nd->root_args.wsize = 8192; 292 nd->root_args.sotype = SOCK_STREAM; 293 nd->root_args.flags = (NFSMNT_WSIZE | 294 NFSMNT_RSIZE | NFSMNT_RESVPORT); 295 if (inaddr_to_sockaddr("boot.nfsroot.server", 296 &nd->root_saddr)) { 297 printf("nfs_diskless: no server\n"); 298 return; 299 } 300 nd->root_saddr.sin_port = htons(NFS_PORT); 301 if (decode_nfshandle("boot.nfsroot.nfshandle", 302 &nd->root_fh[0], NFSX_V2FH) == 0) { 303 printf("nfs_diskless: no NFS handle\n"); 304 return; 305 } 306 if ((cp = getenv("boot.nfsroot.path")) != NULL) { 307 strncpy(nd->root_hostnam, cp, MNAMELEN - 1); 308 freeenv(cp); 309 } 310 if ((cp = getenv("boot.nfsroot.options")) != NULL) { 311 struct nfs_args args; 312 313 /* 314 * XXX yech, convert between old and current 315 * arg format 316 */ 317 args.flags = nd->root_args.flags; 318 args.sotype = nd->root_args.sotype; 319 args.rsize = nd->root_args.rsize; 320 args.wsize = nd->root_args.wsize; 321 nfs_parse_options(cp, &args); 322 nd->root_args.flags = args.flags; 323 nd->root_args.sotype = args.sotype; 324 nd->root_args.rsize = args.rsize; 325 nd->root_args.wsize = args.wsize; 326 freeenv(cp); 327 } 328 329 nfs_diskless_valid = 1; 330 } 331 } 332 333 static int 334 inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa) 335 { 336 u_int32_t a[4]; 337 char *cp; 338 int count; 339 340 bzero(sa, sizeof(*sa)); 341 sa->sin_len = sizeof(*sa); 342 sa->sin_family = AF_INET; 343 344 if ((cp = getenv(ev)) == NULL) 345 return (1); 346 count = sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]); 347 freeenv(cp); 348 if (count != 4) 349 return (1); 350 sa->sin_addr.s_addr = 351 htonl((a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3]); 352 return (0); 353 } 354 355 static int 356 hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa) 357 { 358 char *cp; 359 u_int32_t a[6]; 360 int count; 361 362 bzero(sa, sizeof(*sa)); 363 sa->sdl_len = sizeof(*sa); 364 sa->sdl_family = AF_LINK; 365 sa->sdl_type = IFT_ETHER; 366 sa->sdl_alen = ETHER_ADDR_LEN; 367 if ((cp = getenv(ev)) == NULL) 368 return (1); 369 count = sscanf(cp, "%x:%x:%x:%x:%x:%x", 370 &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]); 371 freeenv(cp); 372 if (count != 6) 373 return (1); 374 sa->sdl_data[0] = a[0]; 375 sa->sdl_data[1] = a[1]; 376 sa->sdl_data[2] = a[2]; 377 sa->sdl_data[3] = a[3]; 378 sa->sdl_data[4] = a[4]; 379 sa->sdl_data[5] = a[5]; 380 return (0); 381 } 382 383 static int 384 decode_nfshandle(char *ev, u_char *fh, int maxfh) 385 { 386 u_char *cp, *ep; 387 int len, val; 388 389 ep = cp = getenv(ev); 390 if (cp == NULL) 391 return (0); 392 if ((strlen(cp) < 2) || (*cp != 'X')) { 393 freeenv(ep); 394 return (0); 395 } 396 len = 0; 397 cp++; 398 for (;;) { 399 if (*cp == 'X') { 400 freeenv(ep); 401 return (len); 402 } 403 if ((sscanf(cp, "%2x", &val) != 1) || (val > 0xff)) { 404 freeenv(ep); 405 return (0); 406 } 407 *(fh++) = val; 408 len++; 409 cp += 2; 410 if (len > maxfh) { 411 freeenv(ep); 412 return (0); 413 } 414 } 415 } 416 417 #if !defined(BOOTP_NFSROOT) 418 static void 419 nfs_rootconf(void) 420 { 421 422 nfs_setup_diskless(); 423 if (nfs_diskless_valid) 424 rootdevnames[0] = "nfs:"; 425 } 426 427 SYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, nfs_rootconf, NULL); 428 #endif 429 430