Home | History | Annotate | Line # | Download | only in nfs
nfs_bootdhcp.c revision 1.1
      1 /*	$NetBSD: nfs_bootdhcp.c,v 1.1 1997/08/29 16:10:31 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 with BOOTP (RFC951, RFC1048)
     41  *
     42  * History:
     43  *
     44  * Tor Egge developed the initial version of this code based on
     45  * the Sun RPC/bootparam sources nfs_boot.c and krpc_subr.c and
     46  * submitted that work to NetBSD as bugreport "kern/2351" on
     47  * 29 Apr 1996.
     48  *
     49  * Gordon Ross reorganized Tor's version into this form and
     50  * integrated it into the NetBSD sources during Aug 1997.
     51  */
     52 
     53 #include <sys/param.h>
     54 #include <sys/systm.h>
     55 #include <sys/kernel.h>
     56 #include <sys/conf.h>
     57 #include <sys/device.h>
     58 #include <sys/ioctl.h>
     59 #include <sys/proc.h>
     60 #include <sys/mount.h>
     61 #include <sys/mbuf.h>
     62 #include <sys/reboot.h>
     63 #include <sys/socket.h>
     64 #include <sys/socketvar.h>
     65 
     66 #include <net/if.h>
     67 #include <net/if_arp.h> 	/* ARPHRD_ETHER, etc. */
     68 #include <net/if_dl.h>
     69 #include <net/if_ether.h>
     70 #include <net/route.h>
     71 
     72 #include <netinet/in.h>
     73 #include <netinet/if_inarp.h>
     74 
     75 #include <nfs/nfsproto.h>
     76 /* #include <nfs/nfs.h> */
     77 #include <nfs/nfsdiskless.h>
     78 
     79 /*
     80  * There are two implementations of NFS diskless boot.
     81  * This implementation uses BOOTP (RFC951, RFC1048), and
     82  * the other uses Sun RPC/bootparams (nfs_bootparam.c).
     83  *
     84  * This method gets everything it needs with one BOOTP
     85  * request and reply.  Note that this actually uses only
     86  * the old BOOTP functionality subset of DHCP.  It is not
     87  * clear that DHCP provides any advantage over BOOTP for
     88  * diskless boot.  DHCP allows the server to assign an IP
     89  * address without any a-priori knowledge of the client,
     90  * but we require that the server has a-priori knowledge
     91  * of the client so it can export our (unique) NFS root.
     92  * Given that the server needs a-priori knowledge about
     93  * the client anyway, it might as well assign a fixed IP
     94  * address for the client and support BOOTP.
     95  *
     96  * On the other hand, disk-FULL clients may use DHCP, but
     97  * in that case the DHCP client should be user-mode code,
     98  * and has no bearing on the code below. -gwr
     99  */
    100 
    101 /* Begin stuff from bootp.h */
    102 /* Definitions from RFC951 */
    103 #define BP_CHADDR_LEN	 16
    104 #define BP_SNAME_LEN	 64
    105 #define BP_FILE_LEN	128
    106 #define BP_VEND_LEN	 64
    107 struct bootp {
    108 	u_int8_t	bp_op;		/* packet opcode type */
    109 	u_int8_t	bp_htype;	/* hardware addr type */
    110 	u_int8_t	bp_hlen;	/* hardware addr length */
    111 	u_int8_t	bp_hops;	/* gateway hops */
    112 	u_int32_t	bp_xid;		/* transaction ID */
    113 	u_int16_t	bp_secs;	/* seconds since boot began */
    114 	u_int16_t	bp_flags;	/* RFC1532 broadcast, etc. */
    115 	struct in_addr	bp_ciaddr;	/* client IP address */
    116 	struct in_addr	bp_yiaddr;	/* 'your' IP address */
    117 	struct in_addr	bp_siaddr;	/* server IP address */
    118 	struct in_addr	bp_giaddr;	/* gateway IP address */
    119 	u_int8_t bp_chaddr[BP_CHADDR_LEN]; /* client hardware address */
    120 	char	bp_sname[BP_SNAME_LEN]; /* server host name */
    121 	char	bp_file[BP_FILE_LEN];	/* boot file name */
    122 	u_int8_t bp_vend[BP_VEND_LEN];	/* RFC1048 options */
    123 	/*
    124 	 * Note that BOOTP packets are allowed to be longer
    125 	 * (see RFC 1532 sect. 2.1) and common practice is to
    126 	 * allow the option data in bp_vend to extend into the
    127 	 * additional space provided in longer packets.
    128 	 */
    129 };
    130 
    131 #define IPPORT_BOOTPS 67
    132 #define IPPORT_BOOTPC 68
    133 
    134 #define BOOTREQUEST		1
    135 #define BOOTREPLY		2
    136 
    137 /*
    138  * Is this available from the sockaddr_dl somehow?
    139  * Perhaps (struct arphdr)->ar_hrd = ARPHRD_ETHER?
    140  * The interface has ->if_type but not the ARP fmt.
    141  */
    142 #define HTYPE_ETHERNET		  1
    143 
    144 /*
    145  * Vendor magic cookie (v_magic) for RFC1048
    146  */
    147 static const u_int8_t vm_rfc1048[4] = { 99, 130, 83, 99 };
    148 
    149 /*
    150  * Tag values used to specify what information is being supplied in
    151  * the vendor (options) data area of the packet.
    152  */
    153 /* RFC 1048 */
    154 #define TAG_END			((unsigned char) 255)
    155 #define TAG_PAD			((unsigned char)   0)
    156 #define TAG_SUBNET_MASK		((unsigned char)   1)
    157 #define TAG_TIME_OFFSET		((unsigned char)   2)
    158 #define TAG_GATEWAY		((unsigned char)   3)
    159 #define TAG_TIME_SERVER		((unsigned char)   4)
    160 #define TAG_NAME_SERVER		((unsigned char)   5)
    161 #define TAG_DOMAIN_SERVER	((unsigned char)   6)
    162 #define TAG_LOG_SERVER		((unsigned char)   7)
    163 #define TAG_COOKIE_SERVER	((unsigned char)   8)
    164 #define TAG_LPR_SERVER		((unsigned char)   9)
    165 #define TAG_IMPRESS_SERVER	((unsigned char)  10)
    166 #define TAG_RLP_SERVER		((unsigned char)  11)
    167 #define TAG_HOST_NAME		((unsigned char)  12)
    168 #define TAG_BOOT_SIZE		((unsigned char)  13)
    169 /* RFC 1395 */
    170 #define TAG_DUMP_FILE		((unsigned char)  14)
    171 #define TAG_DOMAIN_NAME		((unsigned char)  15)
    172 #define TAG_SWAP_SERVER		((unsigned char)  16)
    173 #define TAG_ROOT_PATH		((unsigned char)  17)
    174 /* End of stuff from bootp.h */
    175 
    176 
    177 /*
    178  * The "extended" size is somewhat arbitrary, but is
    179  * constrained by the maximum message size specified
    180  * by RFC1533 (567 total).  This value increases the
    181  * space for options from 64 bytes to 256 bytes.
    182  */
    183 #define BOOTP_SIZE_MAX	(sizeof(struct bootp)+192)
    184 #define BOOTP_SIZE_MIN	(sizeof(struct bootp))
    185 
    186 /*
    187  * What is the longest we will wait before re-sending a request?
    188  * Note this is also the frequency of "BOOTP timeout" messages.
    189  * The re-send loop counts up linearly to this maximum, so the
    190  * first complaint will happen after (1+2+3+4+5)=15 seconds.
    191  */
    192 #define	MAX_RESEND_DELAY 5	/* seconds */
    193 #define TOTAL_TIMEOUT   30	/* seconds */
    194 
    195 /* Convenience macro */
    196 #define INTOHL(ina) ((u_int32_t)ntohl((ina).s_addr))
    197 
    198 static int bootpc_call __P((struct socket *, struct ifnet *,
    199 			struct nfs_diskless *, struct proc *));
    200 
    201 
    202 /* #define DEBUG	XXX */
    203 
    204 #ifdef	DEBUG
    205 #define DPRINT(s) printf("nfs_boot: %s\n", s)
    206 #else
    207 #define DPRINT(s) (void)0
    208 #endif
    209 
    210 
    211 /*
    212  * Get our boot parameters using BOOTP.
    213  */
    214 int
    215 nfs_bootdhcp(ifp, nd, procp)
    216 	struct ifnet *ifp;
    217 	struct nfs_diskless *nd;
    218 	struct proc *procp;
    219 {
    220 	struct ifaliasreq iareq;
    221 	struct socket *so;
    222 	struct sockaddr_in *sin;
    223 	int error;
    224 
    225 	/*
    226 	 * Get a socket to use for various things in here.
    227 	 * After this, use "goto out" to cleanup and return.
    228 	 */
    229 	error = socreate(AF_INET, &so, SOCK_DGRAM, 0);
    230 	if (error) {
    231 		printf("nfs_boot: socreate, error=%d\n", error);
    232 		return (error);
    233 	}
    234 
    235 	/*
    236 	 * Do enough of ifconfig(8) so that the chosen interface
    237 	 * can talk to the servers.  Use address zero for now.
    238 	 */
    239 	bzero(&iareq, sizeof(iareq));
    240 	bcopy(ifp->if_xname, iareq.ifra_name, IFNAMSIZ);
    241 	/* Set the I/F address */
    242 	sin = (struct sockaddr_in *)&iareq.ifra_addr;
    243 	sin->sin_len = sizeof(*sin);
    244 	sin->sin_family = AF_INET;
    245 	sin->sin_addr.s_addr = INADDR_ANY;
    246 	/* Leave subnetmask unspecified (len=0) */
    247 	/* Set the broadcast addr. */
    248 	sin = (struct sockaddr_in *)&iareq.ifra_broadaddr;
    249 	sin->sin_len = sizeof(*sin);
    250 	sin->sin_family = AF_INET;
    251 	sin->sin_addr.s_addr = INADDR_BROADCAST;
    252 	error = ifioctl(so, SIOCAIFADDR, (caddr_t)&iareq, procp);
    253 	if (error) {
    254 		printf("nfs_boot: set ifaddr zero, error=%d\n", error);
    255 		goto out;
    256 	}
    257 
    258 	/* This function call does the real send/recv work. */
    259 	error = bootpc_call(so, ifp, nd, procp);
    260 	/* Get rid of the temporary (zero) IP address. */
    261 	(void) ifioctl(so, SIOCDIFADDR, (caddr_t)&iareq, procp);
    262 	/* NOW we can test the error from bootpc_call. */
    263 	if (error)
    264 		goto out;
    265 
    266 	/*
    267 	 * Do ifconfig with our real IP address and mask.
    268 	 */
    269 	/* I/F address */
    270 	sin = (struct sockaddr_in *)&iareq.ifra_addr;
    271 	sin->sin_addr = nd->nd_myip;
    272 	/* subnetmask */
    273 	if (nd->nd_mask.s_addr) {
    274 		sin = (struct sockaddr_in *)&iareq.ifra_mask;
    275 		sin->sin_len = sizeof(*sin);
    276 		sin->sin_family = AF_INET;
    277 		sin->sin_addr = nd->nd_mask;
    278 	}
    279 	/* Let ifioctl() default the broadcast address. */
    280 	sin = (struct sockaddr_in *)&iareq.ifra_broadaddr;
    281 	sin->sin_len = 0;
    282 	sin->sin_family = 0;
    283 	sin->sin_addr.s_addr = 0;
    284 	error = ifioctl(so, SIOCAIFADDR, (caddr_t)&iareq, procp);
    285 	if (error) {
    286 		printf("nfs_boot: set ifaddr real, error=%d\n", error);
    287 		goto out;
    288 	}
    289 
    290 out:
    291 	soclose(so);
    292 	return (error);
    293 }
    294 
    295 static int
    296 bootpc_call(so, ifp, nd, procp)
    297 	struct socket *so;
    298 	struct ifnet *ifp;
    299 	struct nfs_diskless *nd;
    300 	struct proc *procp;
    301 {
    302 	static u_int32_t xid = ~0xFF;
    303 	struct uio uio;
    304 	struct iovec iov;
    305 	struct in_addr netmask;
    306 	struct in_addr gateway;
    307 	char *myname;	/* my hostname */
    308 	char *mydomain;	/* my domainname */
    309 	char *rootpath;
    310 	int mynamelen;
    311 	int mydomainlen;
    312 	int rootpathlen;
    313 	struct bootp *bootp;	/* request and reply */
    314 	struct mbuf *m, *nam;
    315 	struct sockaddr_in *sin;
    316 	int error, rcvflg, timo, secs, waited;
    317 	int sendlen, recvlen;
    318 	u_int tag, len;
    319 	u_char *p, *limit;
    320 	u_char *haddr;
    321 	u_char hafmt, halen;
    322 
    323 	/*
    324 	 * Initialize to NULL anything that will hold an allocation,
    325 	 * and free each at the end if not null.
    326 	 */
    327 	bootp = NULL;
    328 	m = nam = NULL;
    329 
    330 	/* Record our H/W (Ethernet) address. */
    331 	{	struct sockaddr_dl *sdl = ifp->if_sadl;
    332 		hafmt = HTYPE_ETHERNET; /* XXX: sdl->sdl_type? */
    333 		halen = sdl->sdl_alen;
    334 		haddr = (unsigned char *)LLADDR(sdl);
    335 	}
    336 
    337 	/*
    338 	 * Skip the route table when sending on this socket.
    339 	 * If this is not done, ip_output finds the loopback
    340 	 * interface (why?) and then fails because broadcast
    341 	 * is not supported on that interface...
    342 	 */
    343 	{	int32_t *opt;
    344 		m = m_get(M_WAIT, MT_SOOPTS);
    345 		opt = mtod(m, int32_t *);
    346 		m->m_len = sizeof(*opt);
    347 		*opt = 1;
    348 		error = sosetopt(so, SOL_SOCKET, SO_DONTROUTE, m);
    349 		m = NULL;	/* was consumed */
    350 	}
    351 	if (error) {
    352 		DPRINT("SO_DONTROUTE");
    353 		goto out;
    354 	}
    355 
    356 	/* Enable broadcast. */
    357 	{	int32_t *opt;
    358 		m = m_get(M_WAIT, MT_SOOPTS);
    359 		opt = mtod(m, int32_t *);
    360 		m->m_len = sizeof(*opt);
    361 		*opt = 1;
    362 		error = sosetopt(so, SOL_SOCKET, SO_BROADCAST, m);
    363 		m = NULL;	/* was consumed */
    364 	}
    365 	if (error) {
    366 		DPRINT("SO_BROADCAST");
    367 		goto out;
    368 	}
    369 
    370 	/* Set the receive timeout for the socket. */
    371 	{	struct timeval *tv;
    372 		m = m_get(M_WAIT, MT_SOOPTS);
    373 		tv = mtod(m, struct timeval *);
    374 		m->m_len = sizeof(*tv);
    375 		tv->tv_sec = 1;
    376 		tv->tv_usec = 0;
    377 		error = sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m);
    378 		m = NULL;	/* was consumed */
    379 	}
    380 	if (error) {
    381 		DPRINT("SO_RCVTIMEO");
    382 		goto out;
    383 	}
    384 
    385 	/*
    386 	 * Bind the local endpoint to a bootp client port.
    387 	 */
    388 	m = m_getclr(M_WAIT, MT_SONAME);
    389 	sin = mtod(m, struct sockaddr_in *);
    390 	sin->sin_len = m->m_len = sizeof(*sin);
    391 	sin->sin_family = AF_INET;
    392 	sin->sin_addr.s_addr = INADDR_ANY;
    393 	sin->sin_port = htons(IPPORT_BOOTPC);
    394 	error = sobind(so, m);
    395 	m_freem(m);
    396 	if (error) {
    397 		DPRINT("bind failed\n");
    398 		goto out;
    399 	}
    400 
    401 	/*
    402 	 * Setup socket address for the server.
    403 	 */
    404 	nam = m_get(M_WAIT, MT_SONAME);
    405 	sin = mtod(nam, struct sockaddr_in *);
    406 	sin->sin_len = nam->m_len = sizeof(*sin);
    407 	sin->sin_family = AF_INET;
    408 	sin->sin_addr.s_addr = INADDR_BROADCAST;
    409 	sin->sin_port = htons(IPPORT_BOOTPS);
    410 
    411 	/*
    412 	 * Allocate buffer used for request/reply
    413 	 */
    414 	bootp = malloc(BOOTP_SIZE_MAX, M_DEVBUF, M_WAITOK);
    415 	if (bootp == NULL)
    416 		panic("nfs_boot: malloc buf");
    417 
    418 	/*
    419 	 * Send it, repeatedly, until a reply is received,
    420 	 * but delay each re-send by an increasing amount.
    421 	 * If the delay hits the maximum, start complaining.
    422 	 * Try extended-length request first, and if that
    423 	 * results in total timeout, start over with the
    424 	 * standard-length BOOTP request.
    425 	 */
    426 	sendlen = BOOTP_SIZE_MAX;
    427 	waited = timo = 0;
    428 send_again:
    429 	waited += timo;
    430 	if (waited >= TOTAL_TIMEOUT) {
    431 		if (sendlen > BOOTP_SIZE_MIN) {
    432 			sendlen = BOOTP_SIZE_MIN;
    433 			waited = timo = 0;
    434 			printf("nfs_boot: trying short requests\n");
    435 			goto send_again;
    436 		}
    437 		error = ETIMEDOUT;
    438 		goto out;
    439 	}
    440 	/* Determine new timeout. */
    441 	if (timo < MAX_RESEND_DELAY)
    442 		timo++;
    443 	else
    444 		printf("nfs_boot: BOOTP timeout...\n");
    445 
    446 	/*
    447 	 * Build the BOOTP reqest message.  Do it every time
    448 	 * we send a new request because we reuse the buffer.
    449 	 * Also, use a new transaction ID for each request.
    450 	 * Note: xid is host order! (opaque to server)
    451 	 */
    452 	bzero((caddr_t)bootp, BOOTP_SIZE_MAX);
    453 	bootp->bp_op    = BOOTREQUEST;
    454 	bootp->bp_htype = hafmt;
    455 	bootp->bp_hlen  = halen;	/* Hardware address length */
    456 	bootp->bp_xid = ++xid;
    457 	bootp->bp_secs = htons(waited);
    458 	bcopy(haddr, bootp->bp_chaddr, halen);
    459 	/* Fill-in the vendor data. */
    460 	bcopy(vm_rfc1048, bootp->bp_vend, 4);
    461 	bootp->bp_vend[4] = TAG_END;
    462 
    463 	/*
    464 	 * The BOOTP request is complete.  Send it.
    465 	 */
    466 	iov.iov_base = (caddr_t) bootp;
    467 	iov.iov_len = sendlen;
    468 	uio.uio_iov = &iov;
    469 	uio.uio_iovcnt = 1;
    470 	uio.uio_segflg = UIO_SYSSPACE;
    471 	uio.uio_rw = UIO_WRITE;
    472 	uio.uio_offset = 0;
    473 	uio.uio_resid = sendlen;
    474 	uio.uio_procp = procp;
    475 	error = sosend(so, nam, &uio, NULL, NULL, 0);
    476 	if (error) {
    477 		printf("nfs_boot: sosend: %d\n", error);
    478 		goto out;
    479 	}
    480 
    481 	/*
    482 	 * Wait for up to timo seconds for a reply.
    483 	 * The socket receive timeout was set to 1 second.
    484 	 */
    485 	secs = timo;
    486 	for (;;) {
    487 		iov.iov_base = (caddr_t) bootp;
    488 		iov.iov_len = BOOTP_SIZE_MAX;
    489 		uio.uio_iov = &iov;
    490 		uio.uio_iovcnt = 1;
    491 		uio.uio_segflg = UIO_SYSSPACE;
    492 		uio.uio_rw = UIO_READ;
    493 		uio.uio_offset = 0;
    494 		uio.uio_resid = BOOTP_SIZE_MAX;
    495 		uio.uio_procp = procp;
    496 
    497 		rcvflg = 0;
    498 		error = soreceive(so, NULL, &uio, NULL, NULL, &rcvflg);
    499 		if (error == EWOULDBLOCK) {
    500 			if (--secs <= 0)
    501 				goto send_again;
    502 			continue;
    503 		}
    504 		if (error)
    505 			goto out;
    506 		recvlen = BOOTP_SIZE_MAX - uio.uio_resid;
    507 
    508 		/*
    509 		 * Is this a valid reply?
    510 		 */
    511 		if (recvlen < BOOTP_SIZE_MIN) {
    512 			DPRINT("short packet");
    513 			continue;
    514 		}
    515 		if (bootp->bp_op != BOOTREPLY) {
    516 			DPRINT("not reply");
    517 			continue;
    518 		}
    519 		if (bootp->bp_hlen != halen) {
    520 			DPRINT("bad hwa_len");
    521 			continue;
    522 		}
    523 		if (bcmp(bootp->bp_chaddr, haddr, halen)) {
    524 			DPRINT("wrong hwaddr");
    525 			continue;
    526 		}
    527 		if (bootp->bp_xid != xid) {
    528 			DPRINT("wrong xid");
    529 			continue;
    530 		}
    531 
    532 		/*
    533 		 * OK, we have a valid reply.
    534 		 * Decode the vendor data.
    535 		 */
    536 		if (bcmp(bootp->bp_vend, vm_rfc1048, 4)) {
    537 			printf("nfs_boot: reply missing options\n");
    538 			continue;
    539 		}
    540 		/* Default these to "unspecified". */
    541 		netmask.s_addr = 0;
    542 		gateway.s_addr = 0;
    543 		mydomain    = myname    = rootpath = NULL;
    544 		mydomainlen = mynamelen = rootpathlen = 0;
    545 		p = &bootp->bp_vend[4];
    546 		limit = ((char*)bootp) + recvlen;
    547 		while (p < limit) {
    548 			tag = *p++;
    549 			if (tag == TAG_END)
    550 				break;
    551 			if (tag == TAG_PAD)
    552 				continue;
    553 			len = *p++;
    554 			if (len == 0)
    555 				continue;
    556 			if ((p + len) > limit) {
    557 				printf("nfs_boot: option %d too long\n", tag);
    558 				break;
    559 			}
    560 			switch (tag) {
    561 			case TAG_SUBNET_MASK:
    562 				bcopy(p, &netmask, 4);
    563 				break;
    564 			case TAG_GATEWAY:
    565 				/* Routers */
    566 				bcopy(p, &gateway, 4);
    567 				break;
    568 			case TAG_HOST_NAME:
    569 				if (len >= sizeof(hostname)) {
    570 					printf("nfs_boot: host name >=%d bytes",
    571 					       sizeof(hostname));
    572 					break;
    573 				}
    574 				myname = p;
    575 				mynamelen = len;
    576 				break;
    577 			case TAG_DOMAIN_NAME:
    578 				if (len >= sizeof(domainname)) {
    579 					printf("nfs_boot: domain name >=%d bytes",
    580 					       sizeof(domainname));
    581 					break;
    582 				}
    583 				mydomain = p;
    584 				mydomainlen = len;
    585 				break;
    586 			case TAG_ROOT_PATH:
    587 				/* Leave some room for the server name. */
    588 				if (len >= (MNAMELEN-10)) {
    589 					printf("nfs_boot: rootpath >=%d bytes",
    590 					       (MNAMELEN-10));
    591 					break;
    592 				}
    593 				rootpath = p;
    594 				rootpathlen = len;
    595 				break;
    596 			default:
    597 				break;
    598 			}
    599 			p += len;
    600 		}
    601 
    602 		if (rootpath == NULL) {
    603 			printf("nfs_boot: No root path offered\n");
    604 			continue;
    605 		}
    606 
    607 		/* OK, the reply has everything we need. */
    608 		break;
    609 	}
    610 	rootpath[rootpathlen] = '\0';
    611 
    612 	/*
    613 	 * Store and print network config info.
    614 	 */
    615 	if (myname) {
    616 		myname[mynamelen] = '\0';
    617 		strncpy(hostname, myname, sizeof(hostname));
    618 		hostnamelen = mynamelen;
    619 		printf("nfs_boot: my_name=%s\n", hostname);
    620 	}
    621 	if (mydomain) {
    622 		mydomain[mydomainlen] = '\0';
    623 		strncpy(domainname, mydomain, sizeof(domainname));
    624 		domainnamelen = mydomainlen;
    625 		printf("nfs_boot: my_domain=%s\n", domainname);
    626 	}
    627 	nd->nd_myip = bootp->bp_yiaddr;
    628 	if (nd->nd_myip.s_addr)
    629 		printf("nfs_boot: my_addr=0x%x\n", INTOHL(nd->nd_myip));
    630 	nd->nd_mask = netmask;
    631 	if (nd->nd_mask.s_addr)
    632 		printf("nfs_boot: my_mask=0x%x\n", INTOHL(nd->nd_mask));
    633 	nd->nd_gwip = gateway;
    634 	if (nd->nd_gwip.s_addr)
    635 		printf("nfs_boot: gateway=0x%x\n", INTOHL(nd->nd_gwip));
    636 
    637 	/*
    638 	 * Store the information about our NFS root mount.
    639 	 * The caller will print it, so be silent here.
    640 	 */
    641 	{
    642 		struct nfs_dlmount *ndm = &nd->nd_root;
    643 
    644 		/* Server IP address. */
    645 		sin = (struct sockaddr_in *) &ndm->ndm_saddr;
    646 		bzero((caddr_t)sin, sizeof(*sin));
    647 		sin->sin_len = sizeof(*sin);
    648 		sin->sin_family = AF_INET;
    649 		sin->sin_addr = bootp->bp_siaddr;
    650 		/* Server name. */
    651 		strncpy(ndm->ndm_host, bootp->bp_sname, BP_SNAME_LEN-1);
    652 		len = strlen(ndm->ndm_host);
    653 		if ((len + 1 + rootpathlen + 1) > sizeof(ndm->ndm_host)) {
    654 			/* Show the server IP address numerically. */
    655 			sprintf(ndm->ndm_host, "0x%8x",
    656 			        INTOHL(bootp->bp_siaddr));
    657 			len = strlen(ndm->ndm_host);
    658 		}
    659 		ndm->ndm_host[len++] = ':';
    660 		strncpy(ndm->ndm_host + len,
    661 		    rootpath, rootpathlen + 1);
    662 	}
    663 	error = 0;
    664 
    665 out:
    666 	if (bootp)
    667 		free(bootp, M_DEVBUF);
    668 	if (nam)
    669 		m_freem(nam);
    670 	return (error);
    671 }
    672