Home | History | Annotate | Line # | Download | only in nfs
nfs_bootparam.c revision 1.3
      1 /*	$NetBSD: nfs_bootparam.c,v 1.3 1997/12/10 20:22:37 gwr 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/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/conf.h>
     47 #include <sys/device.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/proc.h>
     50 #include <sys/mount.h>
     51 #include <sys/mbuf.h>
     52 #include <sys/reboot.h>
     53 #include <sys/socket.h>
     54 #include <sys/socketvar.h>
     55 
     56 #include <net/if.h>
     57 #include <net/route.h>
     58 #include <net/if_ether.h>
     59 
     60 #include <netinet/in.h>
     61 #include <netinet/if_inarp.h>
     62 
     63 #include <nfs/rpcv2.h>
     64 #include <nfs/krpc.h>
     65 #include <nfs/xdr_subs.h>
     66 
     67 #include <nfs/nfsproto.h>
     68 #include <nfs/nfsdiskless.h>
     69 
     70 /*
     71  * There are two implementations of NFS diskless boot.
     72  * This implementation uses Sun RPC/bootparams, and the
     73  * the other uses BOOTP (RFC951 - see nfs_bootdhcp.c).
     74  *
     75  * The Sun-style boot sequence goes as follows:
     76  * (1) Use RARP to get our interface address
     77  * (2) Use RPC/bootparam/whoami to get our hostname,
     78  *     our IP address, and the server's IP address.
     79  * (3) Use RPC/bootparam/getfile to get the root path
     80  * (4) Use RPC/mountd to get the root file handle
     81  * (5) Use RPC/bootparam/getfile to get the swap path
     82  * (6) Use RPC/mountd to get the swap file handle
     83  */
     84 
     85 /* bootparam RPC */
     86 static int bp_whoami __P((struct sockaddr_in *bpsin,
     87 	struct in_addr *my_ip, struct in_addr *gw_ip));
     88 static int bp_getfile __P((struct sockaddr_in *bpsin, char *key,
     89 	struct nfs_dlmount *ndm));
     90 
     91 /* helpers... */
     92 static char * number __P((char *s, int *n));
     93 static u_int32_t ip_convertaddr __P((char *));
     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 booptaram RPCs.
    105  */
    106 int
    107 nfs_bootparam(ifp, nd, procp)
    108 	struct ifnet *ifp;
    109 	struct nfs_diskless *nd;
    110 	struct proc *procp;
    111 {
    112 	struct ifreq ireq;
    113 	struct in_addr my_ip, gw_ip;
    114 	struct sockaddr_in bp_sin;
    115 	struct sockaddr_in *sin;
    116 	struct socket *so;
    117 	struct nfs_dlmount *gw_ndm;
    118 	char *p;
    119 	u_int32_t mask;
    120 	int error;
    121 
    122 	gw_ndm = 0;
    123 	bzero(&ireq, sizeof(ireq));
    124 	bcopy(ifp->if_xname, ireq.ifr_name, IFNAMSIZ);
    125 
    126 	/*
    127 	 * Get a socket to use for various things in here.
    128 	 * After this, use "goto out" to cleanup and return.
    129 	 */
    130 	error = socreate(AF_INET, &so, SOCK_DGRAM, 0);
    131 	if (error) {
    132 		printf("nfs_boot: socreate, error=%d\n", error);
    133 		return (error);
    134 	}
    135 
    136 	/*
    137 	 * Bring up the interface. (just set the "up" flag)
    138 	 * Get the old interface flags and or IFF_UP into them so
    139 	 * things like media selection flags are not clobbered.
    140 	 */
    141 	error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)&ireq, procp);
    142 	if (error) {
    143 		printf("nfs_boot: GIFFLAGS, error=%d\n", error);
    144 		goto out;
    145 	}
    146 	ireq.ifr_flags |= IFF_UP;
    147 	error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)&ireq, procp);
    148 	if (error) {
    149 		printf("nfs_boot: SIFFLAGS, error=%d\n", error);
    150 		goto out;
    151 	}
    152 
    153 	/*
    154 	 * Do RARP for the interface address.
    155 	 */
    156 	error = revarpwhoami(&my_ip, ifp);
    157 	if (error) {
    158 		printf("revarp failed, error=%d\n", error);
    159 		goto out;
    160 	}
    161 	nd->nd_myip.s_addr = my_ip.s_addr;
    162 	printf("nfs_boot: client_addr=0x%x\n",
    163 	       (u_int32_t)ntohl(my_ip.s_addr));
    164 
    165 	/*
    166 	 * Do enough of ifconfig(8) so that the chosen interface
    167 	 * can talk to the servers.  (just set the address)
    168 	 */
    169 	sin = (struct sockaddr_in *)&ireq.ifr_addr;
    170 	sin->sin_len = sizeof(*sin);
    171 	sin->sin_family = AF_INET;
    172 	sin->sin_addr = my_ip;
    173 	error = ifioctl(so, SIOCSIFADDR, (caddr_t)&ireq, procp);
    174 	if (error) {
    175 		printf("nfs_boot: set ifaddr, error=%d\n", error);
    176 		goto out;
    177 	}
    178 
    179 	/*
    180 	 * Get client name and gateway address.
    181 	 * RPC: bootparam/whoami
    182 	 * Use the old broadcast address for the WHOAMI
    183 	 * call because we do not yet know our netmask.
    184 	 * The server address returned by the WHOAMI call
    185 	 * is used for all subsequent booptaram RPCs.
    186 	 */
    187 	sin = &bp_sin;
    188 	bzero((caddr_t)sin, sizeof(*sin));
    189 	sin->sin_len = sizeof(*sin);
    190 	sin->sin_family = AF_INET;
    191 	sin->sin_addr.s_addr = INADDR_BROADCAST;
    192 
    193 	/* Do the RPC/bootparam/whoami. */
    194 	error = bp_whoami(sin, &my_ip, &gw_ip);
    195 	if (error) {
    196 		printf("nfs_boot: bootparam whoami, error=%d\n", error);
    197 		goto out;
    198 	}
    199 	printf("nfs_boot: server_addr=0x%x\n",
    200 		   (u_int32_t)ntohl(sin->sin_addr.s_addr));
    201 	printf("nfs_boot: hostname=%s\n", hostname);
    202 
    203 	/*
    204 	 * Now fetch the server:pathname strings and server IP
    205 	 * for root and swap.  Missing swap is not fatal.
    206 	 */
    207 	error = bp_getfile(sin, "root", &nd->nd_root);
    208 	if (error) {
    209 		printf("nfs_boot: bootparam get root: %d\n", error);
    210 		goto out;
    211 	}
    212 #if 0
    213 	error = bp_getfile(sin, "swap", &nd->nd_swap);
    214 	if (error) {
    215 		printf("nfs_boot: bootparam get swap: %d\n", error);
    216 		error = 0;
    217 	}
    218 #endif
    219 
    220 #ifdef	NFS_BOOT_GATEWAY
    221 	/*
    222 	 * Note: we normally ignore the gateway address returned
    223 	 * by the "bootparam/whoami" RPC above, because many old
    224 	 * bootparam servers supply a bogus gateway value.
    225 	 *
    226 	 * These deficiencies in the bootparam RPC interface are
    227 	 * circumvented by using the bootparam/getfile RPC.  The
    228 	 * parameter "gateway" is requested, and if its returned,
    229 	 * we use the "server" part of the reply as the gateway,
    230 	 * and use the "pathname" part of the reply as the mask.
    231 	 * (The mask comes to us as a string: "%d.%d.%d.%d")
    232 	 */
    233 	if (gw_ip.s_addr) {
    234 		/* Our caller will add the route. */
    235 		nd->nd_gwip = gw_ip;
    236 	}
    237 #endif
    238 	gw_ndm = malloc(sizeof(*gw_ndm), M_NFSMNT, M_WAITOK);
    239 	bzero((caddr_t)gw_ndm, sizeof(*gw_ndm));
    240 	error = bp_getfile(sin, "gateway", gw_ndm);
    241 	if (error) {
    242 		/* Ignore the error.  No gateway supplied. */
    243 		error = 0;
    244 		goto out;
    245 	}
    246 	printf("nfs_boot: gateway=%s\n", gw_ndm->ndm_host);
    247 	sin = (struct sockaddr_in *) &gw_ndm->ndm_saddr;
    248 	nd->nd_gwip = sin->sin_addr;
    249 	/* Find the pathname part of the "mounted-on" string. */
    250 	p = strchr(gw_ndm->ndm_host, ':');
    251 	if (p == 0)
    252 		goto out;
    253 	/* have pathname */
    254 	p++;	/* skip ':' */
    255 	mask = ip_convertaddr(p);
    256 	if (mask == 0)
    257 		goto out;
    258 
    259 	/* Save our netmask and update the network interface. */
    260 	nd->nd_mask.s_addr = mask;
    261 	sin = (struct sockaddr_in *)&ireq.ifr_addr;
    262 	sin->sin_len = sizeof(*sin);
    263 	sin->sin_family = AF_INET;
    264 	sin->sin_addr = nd->nd_mask;
    265 	error = ifioctl(so, SIOCSIFNETMASK, (caddr_t)&ireq, procp);
    266 	if (error) {
    267 		printf("nfs_boot: set ifmask, error=%d\n", error);
    268 		error = 0;	/* ignore it */
    269 	}
    270 
    271  out:
    272 	if (gw_ndm)
    273 		free(gw_ndm, M_NFSMNT);
    274 	soclose(so);
    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, msg_len;
    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 	msg_len = fxdr_unsigned(u_int32_t, reply->encap_len);
    353 	m_adj(m, sizeof(*reply));
    354 
    355 	/*
    356 	 * Save bootparam server address
    357 	 */
    358 	sin = mtod(from, struct sockaddr_in *);
    359 	bpsin->sin_port = htons(port);
    360 	bpsin->sin_addr.s_addr = sin->sin_addr.s_addr;
    361 
    362 	/* client name */
    363 	hostnamelen = MAXHOSTNAMELEN-1;
    364 	m = xdr_string_decode(m, hostname, &hostnamelen);
    365 	if (m == NULL)
    366 		goto bad;
    367 
    368 	/* domain name */
    369 	domainnamelen = MAXHOSTNAMELEN-1;
    370 	m = xdr_string_decode(m, domainname, &domainnamelen);
    371 	if (m == NULL)
    372 		goto bad;
    373 
    374 	/* gateway address */
    375 	m = xdr_inaddr_decode(m, gw_ip);
    376 	if (m == NULL)
    377 		goto bad;
    378 
    379 	/* success */
    380 	goto out;
    381 
    382 bad:
    383 	printf("nfs_boot: bootparam_whoami: bad reply\n");
    384 	error = EBADRPC;
    385 
    386 out:
    387 	if (from)
    388 		m_freem(from);
    389 	if (m)
    390 		m_freem(m);
    391 	return(error);
    392 }
    393 
    394 
    395 /*
    396  * RPC: bootparam/getfile
    397  * Given client name and file "key", get:
    398  *	server name
    399  *	server IP address
    400  *	server pathname
    401  */
    402 static int
    403 bp_getfile(bpsin, key, ndm)
    404 	struct sockaddr_in *bpsin;
    405 	char *key;
    406 	struct nfs_dlmount *ndm;
    407 {
    408 	char pathname[MNAMELEN];
    409 	struct in_addr inaddr;
    410 	struct sockaddr_in *sin;
    411 	struct mbuf *m;
    412 	char *serv_name;
    413 	int error, sn_len, path_len;
    414 
    415 	/*
    416 	 * Build request message.
    417 	 */
    418 
    419 	/* client name (hostname) */
    420 	m  = xdr_string_encode(hostname, hostnamelen);
    421 	if (m == NULL)
    422 		return (ENOMEM);
    423 
    424 	/* key name (root or swap) */
    425 	m->m_next = xdr_string_encode(key, strlen(key));
    426 	if (m->m_next == NULL)
    427 		return (ENOMEM);
    428 
    429 	/* RPC: bootparam/getfile */
    430 	error = krpc_call(bpsin, BOOTPARAM_PROG, BOOTPARAM_VERS,
    431 	                  BOOTPARAM_GETFILE, &m, NULL);
    432 	if (error)
    433 		return error;
    434 
    435 	/*
    436 	 * Parse result message.
    437 	 */
    438 
    439 	/* server name */
    440 	serv_name = &ndm->ndm_host[0];
    441 	sn_len = sizeof(ndm->ndm_host) - 1;
    442 	m = xdr_string_decode(m, serv_name, &sn_len);
    443 	if (m == NULL)
    444 		goto bad;
    445 
    446 	/* server IP address (mountd/NFS) */
    447 	m = xdr_inaddr_decode(m, &inaddr);
    448 	if (m == NULL)
    449 		goto bad;
    450 
    451 	/* server pathname */
    452 	path_len = sizeof(pathname) - 1;
    453 	m = xdr_string_decode(m, pathname, &path_len);
    454 	if (m == NULL)
    455 		goto bad;
    456 
    457 	/*
    458 	 * Store the results in the nfs_dlmount.
    459 	 * The strings become "server:pathname"
    460 	 */
    461 	sin = (struct sockaddr_in *) &ndm->ndm_saddr;
    462 	bzero((caddr_t)sin, sizeof(*sin));
    463 	sin->sin_len = sizeof(*sin);
    464 	sin->sin_family = AF_INET;
    465 	sin->sin_addr = inaddr;
    466 	if ((sn_len + 1 + path_len + 1) > sizeof(ndm->ndm_host)) {
    467 		printf("nfs_boot: getfile name too long\n");
    468 		error = EIO;
    469 		goto out;
    470 	}
    471 	ndm->ndm_host[sn_len] = ':';
    472 	bcopy(pathname, ndm->ndm_host + sn_len + 1, path_len + 1);
    473 
    474 	/* success */
    475 	goto out;
    476 
    477 bad:
    478 	printf("nfs_boot: bootparam_getfile: bad reply\n");
    479 	error = EBADRPC;
    480 
    481 out:
    482 	m_freem(m);
    483 	return(0);
    484 }
    485 
    486 
    487 /* helpers... */
    488 
    489 static char *
    490 number(s, n)
    491 	char *s;
    492 	int *n;
    493 {
    494 
    495 	for (*n = 0; ((*s) >= '0' && (*s) <= '9'); s++)
    496 		*n = (*n * 10) + *s - '0';
    497 	return s;
    498 }
    499 
    500 static u_int32_t
    501 ip_convertaddr(p)
    502 	char *p;
    503 {
    504 	u_int32_t addr = 0, n;
    505 
    506 	if (p == (char *)0 || *p == '\0')
    507 		return 0;
    508 	p = number(p, &n);
    509 	addr |= (n << 24) & 0xff000000;
    510 	if (*p == '\0' || *p++ != '.')
    511 		return 0;
    512 	p = number(p, &n);
    513 	addr |= (n << 16) & 0xff0000;
    514 	if (*p == '\0' || *p++ != '.')
    515 		return 0;
    516 	p = number(p, &n);
    517 	addr |= (n << 8) & 0xff00;
    518 	if (*p == '\0' || *p++ != '.')
    519 		return 0;
    520 	p = number(p, &n);
    521 	addr |= n & 0xff;
    522 	if (*p != '\0')
    523 		return 0;
    524 
    525 	return htonl(addr);
    526 }
    527