1 /* $NetBSD: nfs_bootparam.c,v 1.40 2024/07/05 04:31:54 rin Exp $ */ 2 3 /*- 4 * Copyright (c) 1995, 1997 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Adam Glass and Gordon W. Ross. 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Support for NFS diskless booting, Sun-style (RPC/bootparams) 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: nfs_bootparam.c,v 1.40 2024/07/05 04:31:54 rin Exp $"); 38 39 #ifdef _KERNEL_OPT 40 #include "opt_nfs_boot.h" 41 #include "arp.h" 42 #endif 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/kernel.h> 47 #include <sys/ioctl.h> 48 #include <sys/proc.h> 49 #include <sys/mount.h> 50 #include <sys/mbuf.h> 51 #include <sys/reboot.h> 52 #include <sys/socket.h> 53 #include <sys/socketvar.h> 54 #include <sys/vnode.h> 55 56 #include <net/if.h> 57 #include <net/if_types.h> 58 #include <net/route.h> 59 #include <net/if_ether.h> 60 61 #include <netinet/in.h> 62 #include <netinet/if_inarp.h> 63 64 #include <nfs/rpcv2.h> 65 #include <nfs/krpc.h> 66 #include <nfs/xdr_subs.h> 67 68 #include <nfs/nfsproto.h> 69 #include <nfs/nfs.h> 70 #include <nfs/nfsmount.h> 71 #include <nfs/nfsdiskless.h> 72 #include <nfs/nfs_var.h> 73 74 /* 75 * There are two implementations of NFS diskless boot. 76 * This implementation uses Sun RPC/bootparams, and the 77 * the other uses BOOTP (RFC951 - see nfs_bootdhcp.c). 78 * 79 * The Sun-style boot sequence goes as follows: 80 * (1) Use RARP to get our interface address 81 * (2) Use RPC/bootparam/whoami to get our hostname, 82 * our IP address, and the server's IP address. 83 * (3) Use RPC/bootparam/getfile to get the root path 84 * (4) Use RPC/mountd to get the root file handle 85 * (5) Use RPC/bootparam/getfile to get the swap path 86 * (6) Use RPC/mountd to get the swap file handle 87 */ 88 89 /* bootparam RPC */ 90 static int bp_whoami (struct sockaddr_in *bpsin, 91 struct in_addr *my_ip, struct in_addr *gw_ip, struct lwp *l); 92 static int bp_getfile (struct sockaddr_in *bpsin, const char *key, 93 struct nfs_dlmount *ndm, struct lwp *l); 94 95 96 /* 97 * Get client name, gateway address, then 98 * get root and swap server:pathname info. 99 * RPCs: bootparam/whoami, bootparam/getfile 100 * 101 * Use the old broadcast address for the WHOAMI 102 * call because we do not yet know our netmask. 103 * The server address returned by the WHOAMI call 104 * is used for all subsequent bootparam RPCs. 105 */ 106 int 107 nfs_bootparam(struct nfs_diskless *nd, struct lwp *lwp, int *flags) 108 { 109 struct ifnet *ifp = nd->nd_ifp; 110 struct in_addr my_ip, arps_ip, gw_ip; 111 struct sockaddr_in bp_sin; 112 struct sockaddr_in *sin; 113 #ifndef NFS_BOOTPARAM_NOGATEWAY 114 struct nfs_dlmount *gw_ndm = 0; 115 char *p; 116 u_int32_t mask; 117 #endif 118 int error; 119 120 /* 121 * Bring up the interface. (just set the "up" flag) 122 */ 123 error = nfs_boot_ifupdown(ifp, lwp, 1); 124 if (error) { 125 printf("nfs_boot: SIFFLAGS, error=%d\n", error); 126 return (error); 127 } 128 129 error = EADDRNOTAVAIL; 130 memset(&arps_ip, 0, sizeof(arps_ip)); /* GCC */ 131 #if NARP > 0 132 if (ifp->if_type == IFT_ETHER || ifp->if_type == IFT_FDDI) { 133 /* 134 * Do RARP for the interface address. 135 */ 136 error = revarpwhoarewe(ifp, &arps_ip, &my_ip); 137 } 138 #endif 139 if (error) { 140 printf("revarp failed, error=%d\n", error); 141 goto out; 142 } 143 144 if (!(*flags & NFS_BOOT_HAS_MYIP)) { 145 nd->nd_myip.s_addr = my_ip.s_addr; 146 printf("nfs_boot: client_addr=%s", inet_ntoa(my_ip)); 147 printf(" (RARP from %s)\n", inet_ntoa(arps_ip)); 148 *flags |= NFS_BOOT_HAS_MYIP; 149 } 150 151 /* 152 * Do enough of ifconfig(8) so that the chosen interface 153 * can talk to the servers. (just set the address) 154 */ 155 error = nfs_boot_setaddress(ifp, lwp, my_ip.s_addr, 156 INADDR_ANY, INADDR_ANY); 157 if (error) { 158 printf("nfs_boot: set ifaddr, error=%d\n", error); 159 goto out; 160 } 161 162 /* 163 * Get client name and gateway address. 164 * RPC: bootparam/whoami 165 * Use the old broadcast address for the WHOAMI 166 * call because we do not yet know our netmask. 167 * The server address returned by the WHOAMI call 168 * is used for all subsequent booptaram RPCs. 169 */ 170 sin = &bp_sin; 171 memset((void *)sin, 0, sizeof(*sin)); 172 sin->sin_len = sizeof(*sin); 173 sin->sin_family = AF_INET; 174 sin->sin_addr.s_addr = INADDR_BROADCAST; 175 176 /* Do the RPC/bootparam/whoami. */ 177 error = bp_whoami(sin, &my_ip, &gw_ip, lwp); 178 if (error) { 179 printf("nfs_boot: bootparam whoami, error=%d\n", error); 180 goto delout; 181 } 182 *flags |= NFS_BOOT_HAS_SERVADDR | NFS_BOOT_HAS_SERVER; 183 printf("nfs_boot: server_addr=%s\n", inet_ntoa(sin->sin_addr)); 184 printf("nfs_boot: hostname=%s\n", hostname); 185 186 /* 187 * Now fetch the server:pathname strings and server IP 188 * for root and swap. Missing swap is not fatal. 189 */ 190 error = bp_getfile(sin, "root", &nd->nd_root, lwp); 191 if (error) { 192 printf("nfs_boot: bootparam get root: %d\n", error); 193 goto delout; 194 } 195 *flags |= NFS_BOOT_HAS_ROOTPATH; 196 197 #ifndef NFS_BOOTPARAM_NOGATEWAY 198 gw_ndm = kmem_alloc(sizeof(*gw_ndm), KM_SLEEP); 199 memset((void *)gw_ndm, 0, sizeof(*gw_ndm)); 200 error = bp_getfile(sin, "gateway", gw_ndm, lwp); 201 if (error) { 202 /* No gateway supplied. No error, but try fallback. */ 203 error = 0; 204 goto nogwrepl; 205 } 206 sin = (struct sockaddr_in *) &gw_ndm->ndm_saddr; 207 if (sin->sin_addr.s_addr == 0) 208 goto out; /* no gateway */ 209 210 /* OK, we have a gateway! */ 211 printf("nfs_boot: gateway=%s\n", inet_ntoa(sin->sin_addr)); 212 /* Just save it. Caller adds the route. */ 213 nd->nd_gwip = sin->sin_addr; 214 *flags |= NFS_BOOT_HAS_GWIP; 215 216 /* Look for a mask string after the colon. */ 217 p = strchr(gw_ndm->ndm_host, ':'); 218 if (p == 0) 219 goto out; /* no netmask */ 220 /* have pathname */ 221 p++; /* skip ':' */ 222 mask = inet_addr(p); /* libkern */ 223 if (mask == 0) 224 goto out; /* no netmask */ 225 226 /* Have a netmask too! Save it; update the I/F. */ 227 nd->nd_mask.s_addr = mask; 228 *flags |= NFS_BOOT_HAS_MASK; 229 printf("nfs_boot: my_mask=%s\n", inet_ntoa(nd->nd_mask)); 230 (void) nfs_boot_deladdress(ifp, lwp, my_ip.s_addr); 231 error = nfs_boot_setaddress(ifp, lwp, my_ip.s_addr, 232 mask, INADDR_ANY); 233 if (error) { 234 printf("nfs_boot: set ifmask, error=%d\n", error); 235 goto out; 236 } 237 goto gwok; 238 nogwrepl: 239 #endif 240 #ifdef NFS_BOOT_GATEWAY 241 /* 242 * Note: we normally ignore the gateway address returned 243 * by the "bootparam/whoami" RPC above, because many old 244 * bootparam servers supply a bogus gateway value. 245 * 246 * These deficiencies in the bootparam RPC interface are 247 * circumvented by using the bootparam/getfile RPC. The 248 * parameter "gateway" is requested, and if its returned, 249 * we use the "server" part of the reply as the gateway, 250 * and use the "pathname" part of the reply as the mask. 251 * (The mask comes to us as a string.) 252 */ 253 if (gw_ip.s_addr) { 254 /* Our caller will add the route. */ 255 nd->nd_gwip = gw_ip; 256 *flags |= NFS_BOOT_HAS_GWIP; 257 } 258 #endif 259 260 delout: 261 if (error) 262 (void) nfs_boot_deladdress(ifp, lwp, my_ip.s_addr); 263 out: 264 if (error) { 265 (void) nfs_boot_ifupdown(ifp, lwp, 0); 266 nfs_boot_flushrt(ifp); 267 } 268 #ifndef NFS_BOOTPARAM_NOGATEWAY 269 gwok: 270 if (gw_ndm) 271 kmem_free(gw_ndm, sizeof(*gw_ndm)); 272 #endif 273 if ((*flags & NFS_BOOT_ALLINFO) != NFS_BOOT_ALLINFO) 274 return error ? error : EADDRNOTAVAIL; 275 276 return (error); 277 } 278 279 280 /* 281 * RPC: bootparam/whoami 282 * Given client IP address, get: 283 * client name (hostname) 284 * domain name (domainname) 285 * gateway address 286 * 287 * The hostname and domainname are set here for convenience. 288 * 289 * Note - bpsin is initialized to the broadcast address, 290 * and will be replaced with the bootparam server address 291 * after this call is complete. Have to use PMAP_PROC_CALL 292 * to make sure we get responses only from a servers that 293 * know about us (don't want to broadcast a getport call). 294 */ 295 static int 296 bp_whoami(struct sockaddr_in *bpsin, struct in_addr *my_ip, 297 struct in_addr *gw_ip, struct lwp *l) 298 { 299 /* RPC structures for PMAPPROC_CALLIT */ 300 struct whoami_call { 301 u_int32_t call_prog; 302 u_int32_t call_vers; 303 u_int32_t call_proc; 304 u_int32_t call_arglen; 305 } *call; 306 struct callit_reply { 307 u_int32_t port; 308 u_int32_t encap_len; 309 /* encapsulated data here */ 310 } *reply; 311 312 struct mbuf *m, *from; 313 struct sockaddr_in *sin; 314 int error; 315 int16_t port; 316 317 /* 318 * Build request message for PMAPPROC_CALLIT. 319 */ 320 m = m_get(M_WAIT, MT_DATA); 321 call = mtod(m, struct whoami_call *); 322 m->m_len = sizeof(*call); 323 call->call_prog = txdr_unsigned(BOOTPARAM_PROG); 324 call->call_vers = txdr_unsigned(BOOTPARAM_VERS); 325 call->call_proc = txdr_unsigned(BOOTPARAM_WHOAMI); 326 327 /* 328 * append encapsulated data (client IP address) 329 */ 330 m->m_next = xdr_inaddr_encode(my_ip); 331 call->call_arglen = txdr_unsigned(m->m_next->m_len); 332 333 /* RPC: portmap/callit */ 334 bpsin->sin_port = htons(PMAPPORT); 335 error = krpc_call(bpsin, PMAPPROG, PMAPVERS, 336 PMAPPROC_CALLIT, &m, &from, l); 337 if (error) { 338 m_freem(m); 339 return error; 340 } 341 342 /* 343 * Parse result message. 344 */ 345 if (m->m_len < sizeof(*reply)) { 346 m = m_pullup(m, sizeof(*reply)); 347 if (m == NULL) 348 goto bad; 349 } 350 reply = mtod(m, struct callit_reply *); 351 port = fxdr_unsigned(u_int32_t, reply->port); 352 m_adj(m, sizeof(*reply)); 353 354 /* 355 * Save bootparam server address 356 */ 357 sin = mtod(from, struct sockaddr_in *); 358 bpsin->sin_port = htons(port); 359 bpsin->sin_addr.s_addr = sin->sin_addr.s_addr; 360 361 /* client name */ 362 hostnamelen = MAXHOSTNAMELEN-1; 363 m = xdr_string_decode(m, hostname, &hostnamelen); 364 if (m == NULL) 365 goto bad; 366 367 /* domain name */ 368 domainnamelen = MAXHOSTNAMELEN-1; 369 m = xdr_string_decode(m, domainname, &domainnamelen); 370 if (m == NULL) 371 goto bad; 372 373 /* gateway address */ 374 m = xdr_inaddr_decode(m, gw_ip); 375 if (m == NULL) 376 goto bad; 377 378 /* success */ 379 goto out; 380 381 bad: 382 printf("nfs_boot: bootparam_whoami: bad reply\n"); 383 error = EBADRPC; 384 385 out: 386 m_freem(from); 387 m_freem(m); 388 return(error); 389 } 390 391 392 /* 393 * RPC: bootparam/getfile 394 * Given client name and file "key", get: 395 * server name 396 * server IP address 397 * server pathname 398 */ 399 static int 400 bp_getfile(struct sockaddr_in *bpsin, const char *key, 401 struct nfs_dlmount *ndm, struct lwp *l) 402 { 403 char pathname[MNAMELEN]; 404 struct in_addr inaddr; 405 struct sockaddr_in *sin; 406 struct mbuf *m; 407 char *serv_name; 408 int error, sn_len, path_len; 409 410 /* 411 * Build request message. 412 */ 413 414 /* client name (hostname) */ 415 m = xdr_string_encode(hostname, hostnamelen); 416 if (m == NULL) 417 return (ENOMEM); 418 419 /* key name (root or swap) */ 420 /*XXXUNCONST*/ 421 m->m_next = xdr_string_encode(__UNCONST(key), strlen(key)); 422 if (m->m_next == NULL) 423 return (ENOMEM); 424 425 /* RPC: bootparam/getfile */ 426 error = krpc_call(bpsin, BOOTPARAM_PROG, BOOTPARAM_VERS, 427 BOOTPARAM_GETFILE, &m, NULL, l); 428 if (error) 429 return error; 430 431 /* 432 * Parse result message. 433 */ 434 435 /* server name */ 436 serv_name = &ndm->ndm_host[0]; 437 sn_len = sizeof(ndm->ndm_host) - 1; 438 m = xdr_string_decode(m, serv_name, &sn_len); 439 if (m == NULL) 440 goto bad; 441 442 /* server IP address (mountd/NFS) */ 443 m = xdr_inaddr_decode(m, &inaddr); 444 if (m == NULL) 445 goto bad; 446 447 /* server pathname */ 448 path_len = sizeof(pathname) - 1; 449 m = xdr_string_decode(m, pathname, &path_len); 450 if (m == NULL) 451 goto bad; 452 453 /* 454 * Store the results in the nfs_dlmount. 455 * The strings become "server:pathname" 456 */ 457 sin = (struct sockaddr_in *) &ndm->ndm_saddr; 458 memset((void *)sin, 0, sizeof(*sin)); 459 sin->sin_len = sizeof(*sin); 460 sin->sin_family = AF_INET; 461 sin->sin_addr = inaddr; 462 if ((sn_len + 1 + path_len + 1) > sizeof(ndm->ndm_host)) { 463 printf("nfs_boot: getfile name too long\n"); 464 error = EIO; 465 goto out; 466 } 467 ndm->ndm_host[sn_len] = ':'; 468 memcpy(ndm->ndm_host + sn_len + 1, pathname, path_len + 1); 469 470 /* success */ 471 goto out; 472 473 bad: 474 printf("nfs_boot: bootparam_getfile: bad reply\n"); 475 error = EBADRPC; 476 477 out: 478 m_freem(m); 479 return(0); 480 } 481