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