1 /* $NetBSD: nfs_bootparam.c,v 1.7 1998/03/01 17:37:32 veego 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 "opt_nfs_boot.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <sys/conf.h> 49 #include <sys/device.h> 50 #include <sys/ioctl.h> 51 #include <sys/proc.h> 52 #include <sys/mount.h> 53 #include <sys/mbuf.h> 54 #include <sys/reboot.h> 55 #include <sys/socket.h> 56 #include <sys/socketvar.h> 57 58 #include <net/if.h> 59 #include <net/if_types.h> 60 #include <net/route.h> 61 #include <net/if_ether.h> 62 63 #include <netinet/in.h> 64 #include <netinet/if_inarp.h> 65 66 #include <nfs/rpcv2.h> 67 #include <nfs/krpc.h> 68 #include <nfs/xdr_subs.h> 69 70 #include <nfs/nfsproto.h> 71 #include <nfs/nfs.h> 72 #include <nfs/nfsmount.h> 73 #include <nfs/nfsdiskless.h> 74 75 #include "arp.h" 76 77 /* 78 * There are two implementations of NFS diskless boot. 79 * This implementation uses Sun RPC/bootparams, and the 80 * the other uses BOOTP (RFC951 - see nfs_bootdhcp.c). 81 * 82 * The Sun-style boot sequence goes as follows: 83 * (1) Use RARP to get our interface address 84 * (2) Use RPC/bootparam/whoami to get our hostname, 85 * our IP address, and the server's IP address. 86 * (3) Use RPC/bootparam/getfile to get the root path 87 * (4) Use RPC/mountd to get the root file handle 88 * (5) Use RPC/bootparam/getfile to get the swap path 89 * (6) Use RPC/mountd to get the swap file handle 90 */ 91 92 /* bootparam RPC */ 93 static int bp_whoami __P((struct sockaddr_in *bpsin, 94 struct in_addr *my_ip, struct in_addr *gw_ip)); 95 static int bp_getfile __P((struct sockaddr_in *bpsin, char *key, 96 struct nfs_dlmount *ndm)); 97 98 99 /* 100 * Get client name, gateway address, then 101 * get root and swap server:pathname info. 102 * RPCs: bootparam/whoami, bootparam/getfile 103 * 104 * Use the old broadcast address for the WHOAMI 105 * call because we do not yet know our netmask. 106 * The server address returned by the WHOAMI call 107 * is used for all subsequent booptaram RPCs. 108 */ 109 int 110 nfs_bootparam(ifp, nd, procp) 111 struct ifnet *ifp; 112 struct nfs_diskless *nd; 113 struct proc *procp; 114 { 115 struct ifreq ireq; 116 struct in_addr my_ip, gw_ip; 117 struct sockaddr_in bp_sin; 118 struct sockaddr_in *sin; 119 struct socket *so; 120 struct nfs_dlmount *gw_ndm; 121 #if 0 /* XXX - not yet */ 122 char *p; 123 u_int32_t mask; 124 #endif /* XXX */ 125 int error; 126 127 gw_ndm = 0; 128 bzero(&ireq, sizeof(ireq)); 129 bcopy(ifp->if_xname, ireq.ifr_name, IFNAMSIZ); 130 131 /* 132 * Get a socket to use for various things in here. 133 * After this, use "goto out" to cleanup and return. 134 */ 135 error = socreate(AF_INET, &so, SOCK_DGRAM, 0); 136 if (error) { 137 printf("nfs_boot: socreate, error=%d\n", error); 138 return (error); 139 } 140 141 /* 142 * Bring up the interface. (just set the "up" flag) 143 * Get the old interface flags and or IFF_UP into them so 144 * things like media selection flags are not clobbered. 145 */ 146 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)&ireq, procp); 147 if (error) { 148 printf("nfs_boot: GIFFLAGS, error=%d\n", error); 149 goto out; 150 } 151 ireq.ifr_flags |= IFF_UP; 152 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)&ireq, procp); 153 if (error) { 154 printf("nfs_boot: SIFFLAGS, error=%d\n", error); 155 goto out; 156 } 157 158 error = EADDRNOTAVAIL; /* ??? */ 159 #if NARP > 0 160 if (ifp->if_type == IFT_ETHER || ifp->if_type == IFT_FDDI) { 161 /* 162 * Do RARP for the interface address. 163 */ 164 error = revarpwhoami(&my_ip, ifp); 165 if (!error) 166 goto ok; 167 printf("revarp failed, error=%d\n", error); 168 } 169 #endif 170 if (0) goto ok; /* XXX stupid gcc */ 171 goto out; 172 173 ok: 174 nd->nd_myip.s_addr = my_ip.s_addr; 175 printf("nfs_boot: client_addr=0x%x\n", 176 (u_int32_t)ntohl(my_ip.s_addr)); 177 178 /* 179 * Do enough of ifconfig(8) so that the chosen interface 180 * can talk to the servers. (just set the address) 181 */ 182 sin = (struct sockaddr_in *)&ireq.ifr_addr; 183 sin->sin_len = sizeof(*sin); 184 sin->sin_family = AF_INET; 185 sin->sin_addr = my_ip; 186 error = ifioctl(so, SIOCSIFADDR, (caddr_t)&ireq, procp); 187 if (error) { 188 printf("nfs_boot: set ifaddr, error=%d\n", error); 189 goto out; 190 } 191 192 /* 193 * Get client name and gateway address. 194 * RPC: bootparam/whoami 195 * Use the old broadcast address for the WHOAMI 196 * call because we do not yet know our netmask. 197 * The server address returned by the WHOAMI call 198 * is used for all subsequent booptaram RPCs. 199 */ 200 sin = &bp_sin; 201 bzero((caddr_t)sin, sizeof(*sin)); 202 sin->sin_len = sizeof(*sin); 203 sin->sin_family = AF_INET; 204 sin->sin_addr.s_addr = INADDR_BROADCAST; 205 206 /* Do the RPC/bootparam/whoami. */ 207 error = bp_whoami(sin, &my_ip, &gw_ip); 208 if (error) { 209 printf("nfs_boot: bootparam whoami, error=%d\n", error); 210 goto out; 211 } 212 printf("nfs_boot: server_addr=0x%x\n", 213 (u_int32_t)ntohl(sin->sin_addr.s_addr)); 214 printf("nfs_boot: hostname=%s\n", hostname); 215 216 /* 217 * Now fetch the server:pathname strings and server IP 218 * for root and swap. Missing swap is not fatal. 219 */ 220 error = bp_getfile(sin, "root", &nd->nd_root); 221 if (error) { 222 printf("nfs_boot: bootparam get root: %d\n", error); 223 goto out; 224 } 225 #if 0 226 error = bp_getfile(sin, "swap", &nd->nd_swap); 227 if (error) { 228 printf("nfs_boot: bootparam get swap: %d\n", error); 229 error = 0; 230 } 231 #endif 232 233 #ifdef NFS_BOOT_GATEWAY 234 /* 235 * Note: we normally ignore the gateway address returned 236 * by the "bootparam/whoami" RPC above, because many old 237 * bootparam servers supply a bogus gateway value. 238 * 239 * These deficiencies in the bootparam RPC interface are 240 * circumvented by using the bootparam/getfile RPC. The 241 * parameter "gateway" is requested, and if its returned, 242 * we use the "server" part of the reply as the gateway, 243 * and use the "pathname" part of the reply as the mask. 244 * (The mask comes to us as a string.) 245 */ 246 if (gw_ip.s_addr) { 247 /* Our caller will add the route. */ 248 nd->nd_gwip = gw_ip; 249 } 250 #endif 251 #if 0 /* XXX - not yet */ 252 gw_ndm = malloc(sizeof(*gw_ndm), M_NFSMNT, M_WAITOK); 253 bzero((caddr_t)gw_ndm, sizeof(*gw_ndm)); 254 error = bp_getfile(sin, "gateway", gw_ndm); 255 if (error) { 256 /* Ignore the error. No gateway supplied. */ 257 error = 0; 258 goto out; 259 } 260 printf("nfs_boot: gateway=%s\n", gw_ndm->ndm_host); 261 sin = (struct sockaddr_in *) &gw_ndm->ndm_saddr; 262 nd->nd_gwip = sin->sin_addr; 263 /* Find the pathname part of the "mounted-on" string. */ 264 p = strchr(gw_ndm->ndm_host, ':'); 265 if (p == 0) 266 goto out; 267 /* have pathname */ 268 p++; /* skip ':' */ 269 mask = inet_addr(p); /* libkern */ 270 if (mask == 0) { 271 printf("nfs_boot: gateway netmask missing\n"); 272 goto out; 273 } 274 275 /* Save our netmask and update the network interface. */ 276 nd->nd_mask.s_addr = mask; 277 sin = (struct sockaddr_in *)&ireq.ifr_addr; 278 sin->sin_len = sizeof(*sin); 279 sin->sin_family = AF_INET; 280 sin->sin_addr = nd->nd_mask; 281 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t)&ireq, procp); 282 if (error) { 283 printf("nfs_boot: set ifmask, error=%d\n", error); 284 error = 0; /* ignore it */ 285 } 286 #endif /* XXX */ 287 288 out: 289 if (gw_ndm) 290 free(gw_ndm, M_NFSMNT); 291 soclose(so); 292 return (error); 293 } 294 295 296 /* 297 * RPC: bootparam/whoami 298 * Given client IP address, get: 299 * client name (hostname) 300 * domain name (domainname) 301 * gateway address 302 * 303 * The hostname and domainname are set here for convenience. 304 * 305 * Note - bpsin is initialized to the broadcast address, 306 * and will be replaced with the bootparam server address 307 * after this call is complete. Have to use PMAP_PROC_CALL 308 * to make sure we get responses only from a servers that 309 * know about us (don't want to broadcast a getport call). 310 */ 311 static int 312 bp_whoami(bpsin, my_ip, gw_ip) 313 struct sockaddr_in *bpsin; 314 struct in_addr *my_ip; 315 struct in_addr *gw_ip; 316 { 317 /* RPC structures for PMAPPROC_CALLIT */ 318 struct whoami_call { 319 u_int32_t call_prog; 320 u_int32_t call_vers; 321 u_int32_t call_proc; 322 u_int32_t call_arglen; 323 } *call; 324 struct callit_reply { 325 u_int32_t port; 326 u_int32_t encap_len; 327 /* encapsulated data here */ 328 } *reply; 329 330 struct mbuf *m, *from; 331 struct sockaddr_in *sin; 332 int error, msg_len; 333 int16_t port; 334 335 /* 336 * Build request message for PMAPPROC_CALLIT. 337 */ 338 m = m_get(M_WAIT, MT_DATA); 339 call = mtod(m, struct whoami_call *); 340 m->m_len = sizeof(*call); 341 call->call_prog = txdr_unsigned(BOOTPARAM_PROG); 342 call->call_vers = txdr_unsigned(BOOTPARAM_VERS); 343 call->call_proc = txdr_unsigned(BOOTPARAM_WHOAMI); 344 345 /* 346 * append encapsulated data (client IP address) 347 */ 348 m->m_next = xdr_inaddr_encode(my_ip); 349 call->call_arglen = txdr_unsigned(m->m_next->m_len); 350 351 /* RPC: portmap/callit */ 352 bpsin->sin_port = htons(PMAPPORT); 353 from = NULL; 354 error = krpc_call(bpsin, PMAPPROG, PMAPVERS, 355 PMAPPROC_CALLIT, &m, &from); 356 if (error) 357 return error; 358 359 /* 360 * Parse result message. 361 */ 362 if (m->m_len < sizeof(*reply)) { 363 m = m_pullup(m, sizeof(*reply)); 364 if (m == NULL) 365 goto bad; 366 } 367 reply = mtod(m, struct callit_reply *); 368 port = fxdr_unsigned(u_int32_t, reply->port); 369 msg_len = fxdr_unsigned(u_int32_t, reply->encap_len); 370 m_adj(m, sizeof(*reply)); 371 372 /* 373 * Save bootparam server address 374 */ 375 sin = mtod(from, struct sockaddr_in *); 376 bpsin->sin_port = htons(port); 377 bpsin->sin_addr.s_addr = sin->sin_addr.s_addr; 378 379 /* client name */ 380 hostnamelen = MAXHOSTNAMELEN-1; 381 m = xdr_string_decode(m, hostname, &hostnamelen); 382 if (m == NULL) 383 goto bad; 384 385 /* domain name */ 386 domainnamelen = MAXHOSTNAMELEN-1; 387 m = xdr_string_decode(m, domainname, &domainnamelen); 388 if (m == NULL) 389 goto bad; 390 391 /* gateway address */ 392 m = xdr_inaddr_decode(m, gw_ip); 393 if (m == NULL) 394 goto bad; 395 396 /* success */ 397 goto out; 398 399 bad: 400 printf("nfs_boot: bootparam_whoami: bad reply\n"); 401 error = EBADRPC; 402 403 out: 404 if (from) 405 m_freem(from); 406 if (m) 407 m_freem(m); 408 return(error); 409 } 410 411 412 /* 413 * RPC: bootparam/getfile 414 * Given client name and file "key", get: 415 * server name 416 * server IP address 417 * server pathname 418 */ 419 static int 420 bp_getfile(bpsin, key, ndm) 421 struct sockaddr_in *bpsin; 422 char *key; 423 struct nfs_dlmount *ndm; 424 { 425 char pathname[MNAMELEN]; 426 struct in_addr inaddr; 427 struct sockaddr_in *sin; 428 struct mbuf *m; 429 char *serv_name; 430 int error, sn_len, path_len; 431 432 /* 433 * Build request message. 434 */ 435 436 /* client name (hostname) */ 437 m = xdr_string_encode(hostname, hostnamelen); 438 if (m == NULL) 439 return (ENOMEM); 440 441 /* key name (root or swap) */ 442 m->m_next = xdr_string_encode(key, strlen(key)); 443 if (m->m_next == NULL) 444 return (ENOMEM); 445 446 /* RPC: bootparam/getfile */ 447 error = krpc_call(bpsin, BOOTPARAM_PROG, BOOTPARAM_VERS, 448 BOOTPARAM_GETFILE, &m, NULL); 449 if (error) 450 return error; 451 452 /* 453 * Parse result message. 454 */ 455 456 /* server name */ 457 serv_name = &ndm->ndm_host[0]; 458 sn_len = sizeof(ndm->ndm_host) - 1; 459 m = xdr_string_decode(m, serv_name, &sn_len); 460 if (m == NULL) 461 goto bad; 462 463 /* server IP address (mountd/NFS) */ 464 m = xdr_inaddr_decode(m, &inaddr); 465 if (m == NULL) 466 goto bad; 467 468 /* server pathname */ 469 path_len = sizeof(pathname) - 1; 470 m = xdr_string_decode(m, pathname, &path_len); 471 if (m == NULL) 472 goto bad; 473 474 /* 475 * Store the results in the nfs_dlmount. 476 * The strings become "server:pathname" 477 */ 478 sin = (struct sockaddr_in *) &ndm->ndm_saddr; 479 bzero((caddr_t)sin, sizeof(*sin)); 480 sin->sin_len = sizeof(*sin); 481 sin->sin_family = AF_INET; 482 sin->sin_addr = inaddr; 483 if ((sn_len + 1 + path_len + 1) > sizeof(ndm->ndm_host)) { 484 printf("nfs_boot: getfile name too long\n"); 485 error = EIO; 486 goto out; 487 } 488 ndm->ndm_host[sn_len] = ':'; 489 bcopy(pathname, ndm->ndm_host + sn_len + 1, path_len + 1); 490 491 /* success */ 492 goto out; 493 494 bad: 495 printf("nfs_boot: bootparam_getfile: bad reply\n"); 496 error = EBADRPC; 497 498 out: 499 m_freem(m); 500 return(0); 501 } 502 503 504