xref: /netbsd-src/sys/nfs/nfs_bootdhcp.c (revision ba65fde2d7fefa7d39838fa5fa855e62bd606b5e)
1 /*	$NetBSD: nfs_bootdhcp.c,v 1.52 2010/10/04 23:48:22 cyber 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 with BOOTP (RFC951, RFC1048)
34  *
35  * History:
36  *
37  * Tor Egge developed the initial version of this code based on
38  * the Sun RPC/bootparam sources nfs_boot.c and krpc_subr.c and
39  * submitted that work to NetBSD as bugreport "kern/2351" on
40  * 29 Apr 1996.
41  *
42  * Gordon Ross reorganized Tor's version into this form and
43  * integrated it into the NetBSD sources during Aug 1997.
44  */
45 
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: nfs_bootdhcp.c,v 1.52 2010/10/04 23:48:22 cyber Exp $");
48 
49 #ifdef _KERNEL_OPT
50 #include "opt_nfs_boot.h"
51 #include "opt_tftproot.h"
52 #endif
53 
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.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_types.h>
68 #include <net/if_arp.h> 	/* ARPHRD_ETHER, etc. */
69 #include <net/if_dl.h>
70 #include <net/if_ether.h>
71 #include <net/route.h>
72 
73 #include <netinet/in.h>
74 #include <netinet/if_inarp.h>
75 
76 #include <nfs/rpcv2.h>
77 
78 #include <nfs/nfsproto.h>
79 #include <nfs/nfs.h>
80 #include <nfs/nfsmount.h>
81 #include <nfs/nfsdiskless.h>
82 
83 /*
84  * There are two implementations of NFS diskless boot.
85  * This implementation uses BOOTP (RFC951, RFC1048), and
86  * the other uses Sun RPC/bootparams (nfs_bootparam.c).
87  *
88  * This method gets everything it needs with one BOOTP
89  * request and reply.  Note that this actually uses only
90  * the old BOOTP functionality subset of DHCP.  It is not
91  * clear that DHCP provides any advantage over BOOTP for
92  * diskless boot.  DHCP allows the server to assign an IP
93  * address without any a-priori knowledge of the client,
94  * but we require that the server has a-priori knowledge
95  * of the client so it can export our (unique) NFS root.
96  * Given that the server needs a-priori knowledge about
97  * the client anyway, it might as well assign a fixed IP
98  * address for the client and support BOOTP.
99  *
100  * On the other hand, disk-FULL clients may use DHCP, but
101  * in that case the DHCP client should be user-mode code,
102  * and has no bearing on the code below. -gwr
103  */
104 
105 /* Begin stuff from bootp.h */
106 /* Definitions from RFC951 */
107 #define BP_CHADDR_LEN	 16
108 #define BP_SNAME_LEN	 64
109 #define BP_FILE_LEN	128
110 #define BP_VEND_LEN	 64
111 struct bootp {
112 	u_int8_t	bp_op;		/* packet opcode type */
113 	u_int8_t	bp_htype;	/* hardware addr type */
114 	u_int8_t	bp_hlen;	/* hardware addr length */
115 	u_int8_t	bp_hops;	/* gateway hops */
116 	u_int32_t	bp_xid;		/* transaction ID */
117 	u_int16_t	bp_secs;	/* seconds since boot began */
118 	u_int16_t	bp_flags;	/* RFC1532 broadcast, etc. */
119 	struct in_addr	bp_ciaddr;	/* client IP address */
120 	struct in_addr	bp_yiaddr;	/* 'your' IP address */
121 	struct in_addr	bp_siaddr;	/* server IP address */
122 	struct in_addr	bp_giaddr;	/* gateway IP address */
123 	u_int8_t bp_chaddr[BP_CHADDR_LEN]; /* client hardware address */
124 	char	bp_sname[BP_SNAME_LEN]; /* server host name */
125 	char	bp_file[BP_FILE_LEN];	/* boot file name */
126 	u_int8_t bp_vend[BP_VEND_LEN];	/* RFC1048 options */
127 	/*
128 	 * Note that BOOTP packets are allowed to be longer
129 	 * (see RFC 1532 sect. 2.1) and common practice is to
130 	 * allow the option data in bp_vend to extend into the
131 	 * additional space provided in longer packets.
132 	 */
133 };
134 
135 #define IPPORT_BOOTPS 67
136 #define IPPORT_BOOTPC 68
137 
138 #define BOOTREQUEST		1
139 #define BOOTREPLY		2
140 
141 /*
142  * Is this available from the sockaddr_dl somehow?
143  * Perhaps (struct arphdr)->ar_hrd = ARPHRD_ETHER?
144  * The interface has ->if_type but not the ARP fmt.
145  */
146 #define HTYPE_ETHERNET		1
147 #define HTYPE_IEEE802		6
148 
149 /*
150  * Vendor magic cookie (v_magic) for RFC1048
151  */
152 static const u_int8_t vm_rfc1048[4] = { 99, 130, 83, 99 };
153 
154 /*
155  * Tag values used to specify what information is being supplied in
156  * the vendor (options) data area of the packet.
157  */
158 /* RFC 1048 */
159 #define TAG_END			((unsigned char) 255)
160 #define TAG_PAD			((unsigned char)   0)
161 #define TAG_SUBNET_MASK		((unsigned char)   1)
162 #define TAG_TIME_OFFSET		((unsigned char)   2)
163 #define TAG_GATEWAY		((unsigned char)   3)
164 #define TAG_TIME_SERVER		((unsigned char)   4)
165 #define TAG_NAME_SERVER		((unsigned char)   5)
166 #define TAG_DOMAIN_SERVER	((unsigned char)   6)
167 #define TAG_LOG_SERVER		((unsigned char)   7)
168 #define TAG_COOKIE_SERVER	((unsigned char)   8)
169 #define TAG_LPR_SERVER		((unsigned char)   9)
170 #define TAG_IMPRESS_SERVER	((unsigned char)  10)
171 #define TAG_RLP_SERVER		((unsigned char)  11)
172 #define TAG_HOST_NAME		((unsigned char)  12)
173 #define TAG_BOOT_SIZE		((unsigned char)  13)
174 /* RFC 1395 */
175 #define TAG_DUMP_FILE		((unsigned char)  14)
176 #define TAG_DOMAIN_NAME		((unsigned char)  15)
177 #define TAG_SWAP_SERVER		((unsigned char)  16)
178 #define TAG_ROOT_PATH		((unsigned char)  17)
179 /* RFC 2132 */
180 #define TAG_INTERFACE_MTU	((unsigned char)  26)
181 /* End of stuff from bootp.h */
182 
183 #ifdef NFS_BOOT_DHCP
184 #define TAG_REQ_ADDR		((unsigned char)  50)
185 #define TAG_LEASETIME		((unsigned char)  51)
186 #define TAG_OVERLOAD		((unsigned char)  52)
187 #define TAG_DHCP_MSGTYPE	((unsigned char)  53)
188 #define TAG_SERVERID		((unsigned char)  54)
189 #define TAG_PARAM_REQ		((unsigned char)  55)
190 #define TAG_MSG			((unsigned char)  56)
191 #define TAG_MAXSIZE		((unsigned char)  57)
192 #define TAG_T1			((unsigned char)  58)
193 #define TAG_T2			((unsigned char)  59)
194 #define TAG_CLASSID		((unsigned char)  60)
195 #define TAG_CLIENTID		((unsigned char)  61)
196 #endif
197 
198 #ifdef NFS_BOOT_DHCP
199 #define DHCPDISCOVER 1
200 #define DHCPOFFER 2
201 #define DHCPREQUEST 3
202 #define DHCPDECLINE 4
203 #define DHCPACK 5
204 #define DHCPNAK 6
205 #define DHCPRELEASE 7
206 #endif
207 
208 #define IP_MIN_MTU 576
209 
210 #ifdef NFS_BOOT_DHCP
211 #define BOOTP_SIZE_MAX	(sizeof(struct bootp)+312-64)
212 #else
213 /*
214  * The "extended" size is somewhat arbitrary, but is
215  * constrained by the maximum message size specified
216  * by RFC1533 (567 total).  This value increases the
217  * space for options from 64 bytes to 256 bytes.
218  */
219 #define BOOTP_SIZE_MAX	(sizeof(struct bootp)+256-64)
220 #endif
221 #define BOOTP_SIZE_MIN	(sizeof(struct bootp))
222 
223 /* Convenience macro */
224 #define INTOHL(ina) ((u_int32_t)ntohl((ina).s_addr))
225 
226 static int bootpc_call (struct nfs_diskless *, struct lwp *, int *);
227 static void bootp_extract (struct bootp *, int, struct nfs_diskless *, int *);
228 
229 #ifdef	DEBUG_NFS_BOOT_DHCP
230 #define DPRINTF(s)  printf s
231 #else
232 #define DPRINTF(s)
233 #endif
234 
235 
236 /*
237  * Get our boot parameters using BOOTP.
238  */
239 int
240 nfs_bootdhcp(struct nfs_diskless *nd, struct lwp *lwp, int *flags)
241 {
242 	struct ifnet *ifp = nd->nd_ifp;
243 	int error;
244 
245 	/*
246 	 * Do enough of ifconfig(8) so that the chosen interface
247 	 * can talk to the servers.  Use address zero for now.
248 	 */
249 	error = nfs_boot_setaddress(ifp, lwp,
250 		*flags & NFS_BOOT_HAS_MYIP ? nd->nd_myip.s_addr : INADDR_ANY,
251 		*flags & NFS_BOOT_HAS_MASK ? nd->nd_mask.s_addr : INADDR_ANY,
252 		    INADDR_BROADCAST);
253 	if (error) {
254 		printf("nfs_boot: set ifaddr zero, error=%d\n", error);
255 		return (error);
256 	}
257 
258 	/* This function call does the real send/recv work. */
259 	error = bootpc_call(nd, lwp, flags);
260 
261 	/* Get rid of the temporary (zero) IP address. */
262 	(void) nfs_boot_deladdress(ifp, lwp, INADDR_ANY);
263 
264 	/* NOW we can test the error from bootpc_call. */
265 	if (error)
266 		goto out;
267 
268 	/*
269 	 * Do ifconfig with our real IP address and mask.
270 	 */
271 	error = nfs_boot_setaddress(ifp, lwp, nd->nd_myip.s_addr,
272 				    nd->nd_mask.s_addr, INADDR_ANY);
273 	if (error) {
274 		printf("nfs_boot: set ifaddr real, error=%d\n", error);
275 		goto out;
276 	}
277 
278 	if ((*flags & NFS_BOOT_ALLINFO) != NFS_BOOT_ALLINFO) {
279 		printf("nfs_boot: missing options (need IP, netmask, "
280 		       "gateway, next-server, root-path)\n");
281 		return EADDRNOTAVAIL;
282 	}
283 
284 out:
285 	if (error) {
286 		(void) nfs_boot_ifupdown(ifp, lwp, 0);
287 		nfs_boot_flushrt(ifp);
288 	}
289 	return (error);
290 }
291 
292 struct bootpcontext {
293 	int xid;
294 	const u_char *haddr;
295 	u_char halen;
296 	struct bootp *replybuf;
297 	int replylen;
298 #ifdef NFS_BOOT_DHCP
299 	char expected_dhcpmsgtype, dhcp_ok;
300 	struct in_addr dhcp_serverip;
301 #endif
302 };
303 
304 static int bootpset (struct mbuf*, void*, int);
305 static int bootpcheck (struct mbuf*, void*);
306 
307 static int
308 bootpset(struct mbuf *m, void *context, int waited)
309 {
310 	struct bootp *bootp;
311 
312 	/* we know it's contigous (in 1 mbuf cluster) */
313 	bootp = mtod(m, struct bootp*);
314 
315 	bootp->bp_secs = htons(waited);
316 
317 	return (0);
318 }
319 
320 static int
321 bootpcheck(struct mbuf *m, void *context)
322 {
323 	struct bootp *bootp;
324 	struct bootpcontext *bpc = context;
325 	u_int tag, len;
326 	u_char *p, *limit;
327 
328 	/*
329 	 * Is this a valid reply?
330 	 */
331 	if (m->m_pkthdr.len < BOOTP_SIZE_MIN) {
332 		DPRINTF(("bootpcheck: short packet %d < %zu\n",
333 		    m->m_pkthdr.len, BOOTP_SIZE_MIN));
334 		return (-1);
335 	}
336 	if (m->m_pkthdr.len > BOOTP_SIZE_MAX) {
337 		DPRINTF(("Bootpcheck: long packet %d > %zu\n",
338 		   m->m_pkthdr.len, BOOTP_SIZE_MAX));
339 		return (-1);
340 	}
341 
342 	/*
343 	 * don't make first checks more expensive than necessary
344 	 */
345 	if (m->m_len < offsetof(struct bootp, bp_sname)) {
346 		m = m_pullup(m, offsetof(struct bootp, bp_sname));
347 		if (m == NULL) {
348 			DPRINTF(("bootpcheck: m_pullup failed\n"));
349 			return (-1);
350 		}
351 	}
352 	bootp = mtod(m, struct bootp*);
353 
354 	if (bootp->bp_op != BOOTREPLY) {
355 		DPRINTF(("bootpcheck: op %d is not reply\n", bootp->bp_op));
356 		return (-1);
357 	}
358 	if (bootp->bp_hlen != bpc->halen) {
359 		DPRINTF(("bootpcheck: hlen %d != %d\n", bootp->bp_hlen,
360 		    bpc->halen));
361 		return (-1);
362 	}
363 	if (memcmp(bootp->bp_chaddr, bpc->haddr, bpc->halen)) {
364 #ifdef DEBUG_NFS_BOOT_DHCP
365 		char *bp_chaddr, *haddr;
366 
367 		bp_chaddr = malloc(3 * bpc->halen, M_TEMP, M_WAITOK);
368 		haddr     = malloc(3 * bpc->halen, M_TEMP, M_WAITOK);
369 
370 		DPRINTF(("bootpcheck: incorrect hwaddr %s != %s\n",
371 		    ether_snprintf(bp_chaddr, 3 * bpc->halen,
372 		    bootp->bp_chaddr),
373 		    ether_snprintf(haddr, 3 * bpc->halen, bpc->haddr)));
374 
375 		free(bp_chaddr, M_TEMP);
376 		free(haddr, M_TEMP);
377 #endif
378 		return (-1);
379 	}
380 	if (bootp->bp_xid != bpc->xid) {
381 		DPRINTF(("bootpcheck: xid %d != %d\n", bootp->bp_xid,
382 		    bpc->xid));
383 		return (-1);
384 	}
385 
386 	/*
387 	 * OK, it's worth to look deeper.
388 	 * We copy the mbuf into a flat buffer here because
389 	 * m_pullup() is a bit limited for this purpose
390 	 * (doesn't allocate a cluster if necessary).
391 	 */
392 	bpc->replylen = m->m_pkthdr.len;
393 	m_copydata(m, 0, bpc->replylen, (void *)bpc->replybuf);
394 	bootp = bpc->replybuf;
395 
396 	/*
397 	 * Check if the IP address we get looks correct.
398 	 * (DHCP servers can send junk to unknown clients.)
399 	 * XXX more checks might be needed
400 	 */
401 	if (bootp->bp_yiaddr.s_addr == INADDR_ANY ||
402 	    bootp->bp_yiaddr.s_addr == INADDR_BROADCAST) {
403 		printf("nfs_boot: wrong IP addr %s",
404 		       inet_ntoa(bootp->bp_yiaddr));
405 		goto warn;
406 	}
407 
408 	/*
409 	 * Check the vendor data.
410 	 */
411 	if (memcmp(bootp->bp_vend, vm_rfc1048, 4)) {
412 		printf("nfs_boot: reply missing options");
413 		goto warn;
414 	}
415 	p = &bootp->bp_vend[4];
416 	limit = ((u_char*)bootp) + bpc->replylen;
417 	while (p < limit) {
418 		tag = *p++;
419 		if (tag == TAG_END)
420 			break;
421 		if (tag == TAG_PAD)
422 			continue;
423 		len = *p++;
424 		if ((p + len) > limit) {
425 			printf("nfs_boot: option %d too long", tag);
426 			goto warn;
427 		}
428 		switch (tag) {
429 #ifdef NFS_BOOT_DHCP
430 		case TAG_DHCP_MSGTYPE:
431 			if (*p != bpc->expected_dhcpmsgtype)
432 				return (-1);
433 			bpc->dhcp_ok = 1;
434 			break;
435 		case TAG_SERVERID:
436 			memcpy(&bpc->dhcp_serverip.s_addr, p,
437 			      sizeof(bpc->dhcp_serverip.s_addr));
438 			break;
439 #endif
440 		default:
441 			break;
442 		}
443 		p += len;
444 	}
445 	return (0);
446 
447 warn:
448 	printf(" (bad reply from %s)\n", inet_ntoa(bootp->bp_siaddr));
449 	return (-1);
450 }
451 
452 static void
453 bootp_addvend(u_char *area)
454 {
455 #ifdef NFS_BOOT_DHCP
456 	char vci[64];
457 	int vcilen;
458 
459 	*area++ = TAG_PARAM_REQ;
460 	*area++ = 7;
461 	*area++ = TAG_SUBNET_MASK;
462 	*area++ = TAG_GATEWAY;
463 	*area++ = TAG_HOST_NAME;
464 	*area++ = TAG_DOMAIN_NAME;
465 	*area++ = TAG_ROOT_PATH;
466 	*area++ = TAG_SWAP_SERVER;
467 	*area++ = TAG_INTERFACE_MTU;
468 
469 	/* Insert a NetBSD Vendor Class Identifier option. */
470 	snprintf(vci, sizeof(vci), "%s:%s:kernel:%s", ostype, MACHINE,
471 	    osrelease);
472 	vcilen = strlen(vci);
473 	*area++ = TAG_CLASSID;
474 	*area++ = vcilen;
475 	(void)memcpy(area, vci, vcilen);
476 	area += vcilen;
477 #endif
478 	*area = TAG_END;
479 }
480 
481 static int
482 bootpc_call(struct nfs_diskless *nd, struct lwp *lwp, int *flags)
483 {
484 	struct socket *so;
485 	struct ifnet *ifp = nd->nd_ifp;
486 	static u_int32_t xid = ~0xFF;
487 	struct bootp *bootp;	/* request */
488 	struct mbuf *m, *nam;
489 	struct sockaddr_in *sin;
490 	int error;
491 	const u_char *haddr;
492 	u_char hafmt, halen;
493 	struct bootpcontext bpc;
494 	unsigned int index;
495 
496 	error = socreate(AF_INET, &so, SOCK_DGRAM, 0, lwp, NULL);
497 	if (error) {
498 		printf("bootp: socreate, error=%d\n", error);
499 		return (error);
500 	}
501 
502 	/*
503 	 * Initialize to NULL anything that will hold an allocation,
504 	 * and free each at the end if not null.
505 	 */
506 	bpc.replybuf = NULL;
507 	m = nam = NULL;
508 
509 	/* Record our H/W (Ethernet) address. */
510 	{	const struct sockaddr_dl *sdl = ifp->if_sadl;
511 		switch (sdl->sdl_type) {
512 		    case IFT_ISO88025:
513 			hafmt = HTYPE_IEEE802;
514 			break;
515 		    case IFT_ETHER:
516 		    case IFT_FDDI:
517 			hafmt = HTYPE_ETHERNET;
518 			break;
519 		    default:
520 			printf("bootp: unsupported interface type %d\n",
521 			       sdl->sdl_type);
522 			error = EINVAL;
523 			goto out;
524 		}
525 		halen = sdl->sdl_alen;
526 		haddr = (const unsigned char *)CLLADDR(sdl);
527 	}
528 
529 	/*
530 	 * Skip the route table when sending on this socket.
531 	 * If this is not done, ip_output finds the loopback
532 	 * interface (why?) and then fails because broadcast
533 	 * is not supported on that interface...
534 	 */
535 	{	int32_t opt;
536 
537 		opt = 1;
538 		error = so_setsockopt(NULL, so, SOL_SOCKET, SO_DONTROUTE, &opt,
539 		    sizeof(opt));
540 	}
541 	if (error) {
542 		DPRINTF(("bootpc_call: SO_DONTROUTE failed %d\n", error));
543 		goto out;
544 	}
545 
546 	/* Enable broadcast. */
547 	if ((error = nfs_boot_enbroadcast(so))) {
548 		DPRINTF(("bootpc_call: SO_BROADCAST failed %d\n", error));
549 		goto out;
550 	}
551 
552 	/*
553 	 * Set some TTL so we can boot through routers.
554 	 * Real BOOTP forwarding agents don't need this; they obey "bp_hops"
555 	 * and set "bp_giaddr", thus rewrite the packet anyway.
556 	 * The "helper-address" feature of some popular router vendor seems
557 	 * to do simple IP forwarding and drops packets with (ip_ttl == 1).
558 	 */
559 	{	u_char opt;
560 
561 		opt = 7;
562 		error = so_setsockopt(NULL, so, IPPROTO_IP, IP_MULTICAST_TTL,
563 		    &opt, sizeof(opt));
564 	}
565 	if (error) {
566 		DPRINTF(("bootpc_call: IP_MULTICAST_TTL failed %d\n", error));
567 		goto out;
568 	}
569 
570 	/* Set the receive timeout for the socket. */
571 	if ((error = nfs_boot_setrecvtimo(so))) {
572 		DPRINTF(("bootpc_call: SO_RCVTIMEO failed %d\n", error));
573 		goto out;
574 	}
575 
576 	/*
577 	 * Bind the local endpoint to a bootp client port.
578 	 */
579 	if ((error = nfs_boot_sobind_ipport(so, IPPORT_BOOTPC, lwp))) {
580 		DPRINTF(("bootpc_call: bind failed %d\n", error));
581 		goto out;
582 	}
583 
584 	/*
585 	 * Setup socket address for the server.
586 	 */
587 	nam = m_get(M_WAIT, MT_SONAME);
588 	sin = mtod(nam, struct sockaddr_in *);
589 	sin->sin_len = nam->m_len = sizeof(*sin);
590 	sin->sin_family = AF_INET;
591 	sin->sin_addr.s_addr = INADDR_BROADCAST;
592 	sin->sin_port = htons(IPPORT_BOOTPS);
593 
594 	/*
595 	 * Allocate buffer used for request
596 	 */
597 	m = m_gethdr(M_WAIT, MT_DATA);
598 	m_clget(m, M_WAIT);
599 	bootp = mtod(m, struct bootp*);
600 	m->m_pkthdr.len = m->m_len = BOOTP_SIZE_MAX;
601 	m->m_pkthdr.rcvif = NULL;
602 
603 	/*
604 	 * Build the BOOTP reqest message.
605 	 * Note: xid is host order! (opaque to server)
606 	 */
607 	memset((void *)bootp, 0, BOOTP_SIZE_MAX);
608 	bootp->bp_op    = BOOTREQUEST;
609 	bootp->bp_htype = hafmt;
610 	bootp->bp_hlen  = halen;	/* Hardware address length */
611 	bootp->bp_xid = ++xid;
612 	memcpy(bootp->bp_chaddr, haddr, halen);
613 #ifdef NFS_BOOT_BOOTP_REQFILE
614 	strncpy(bootp->bp_file, NFS_BOOT_BOOTP_REQFILE, sizeof(bootp->bp_file));
615 #endif
616 	/* Fill-in the vendor data. */
617 	memcpy(bootp->bp_vend, vm_rfc1048, 4);
618 	index = 4;
619 #ifdef NFS_BOOT_DHCP
620 	bootp->bp_vend[index++] = TAG_DHCP_MSGTYPE;
621 	bootp->bp_vend[index++] = 1;
622 	bootp->bp_vend[index++] = DHCPDISCOVER;
623 #endif
624 	bootp_addvend(&bootp->bp_vend[index]);
625 
626 	bpc.xid = xid;
627 	bpc.haddr = haddr;
628 	bpc.halen = halen;
629 	bpc.replybuf = malloc(BOOTP_SIZE_MAX, M_DEVBUF, M_WAITOK);
630 	if (bpc.replybuf == NULL)
631 		panic("nfs_boot: malloc reply buf");
632 #ifdef NFS_BOOT_DHCP
633 	bpc.expected_dhcpmsgtype = DHCPOFFER;
634 	bpc.dhcp_ok = 0;
635 #endif
636 
637 	error = nfs_boot_sendrecv(so, nam, bootpset, m,
638 				  bootpcheck, 0, 0, &bpc, lwp);
639 	if (error)
640 		goto out;
641 
642 #ifdef NFS_BOOT_DHCP
643 	if (bpc.dhcp_ok) {
644 		u_int32_t leasetime;
645 		index = 6;
646 		bootp->bp_vend[index++] = DHCPREQUEST;
647 		bootp->bp_vend[index++] = TAG_REQ_ADDR;
648 		bootp->bp_vend[index++] = 4;
649 		memcpy(&bootp->bp_vend[index], &bpc.replybuf->bp_yiaddr, 4);
650 		index += 4;
651 		bootp->bp_vend[index++] = TAG_SERVERID;
652 		bootp->bp_vend[index++] = 4;
653 		memcpy(&bootp->bp_vend[index], &bpc.dhcp_serverip.s_addr, 4);
654 		index += 4;
655 		bootp->bp_vend[index++] = TAG_LEASETIME;
656 		bootp->bp_vend[index++] = 4;
657 		leasetime = htonl(300);
658 		memcpy(&bootp->bp_vend[index], &leasetime, 4);
659 		index += 4;
660 		bootp_addvend(&bootp->bp_vend[index]);
661 
662 		bpc.expected_dhcpmsgtype = DHCPACK;
663 
664 		error = nfs_boot_sendrecv(so, nam, bootpset, m,
665 					  bootpcheck, 0, 0, &bpc, lwp);
666 		if (error)
667 			goto out;
668 	}
669 #endif
670 
671 	/*
672 	 * bootpcheck() has copied the receive mbuf into
673 	 * the buffer at bpc.replybuf.
674 	 */
675 #ifdef NFS_BOOT_DHCP
676 	printf("nfs_boot: %s next-server: %s\n",
677 	       (bpc.dhcp_ok ? "DHCP" : "BOOTP"),
678 #else
679 	printf("nfs_boot: BOOTP next-server: %s\n",
680 #endif
681 	       inet_ntoa(bpc.replybuf->bp_siaddr));
682 
683 	bootp_extract(bpc.replybuf, bpc.replylen, nd, flags);
684 
685 out:
686 	if (bpc.replybuf)
687 		free(bpc.replybuf, M_DEVBUF);
688 	if (m)
689 		m_freem(m);
690 	if (nam)
691 		m_freem(nam);
692 	soclose(so);
693 	return (error);
694 }
695 
696 static void
697 bootp_extract(struct bootp *bootp, int replylen,
698 		struct nfs_diskless *nd, int *flags)
699 {
700 	struct sockaddr_in *sin;
701 	struct in_addr netmask;
702 	struct in_addr gateway;
703 	struct in_addr rootserver;
704 	char *myname;	/* my hostname */
705 	char *mydomain;	/* my domainname */
706 	char *rootpath;
707 	uint16_t myinterfacemtu;
708 	int mynamelen;
709 	int mydomainlen;
710 	int rootpathlen;
711 	int overloaded;
712 	u_int tag, len;
713 	u_char *p, *limit;
714 
715 	/* Default these to "unspecified". */
716 	netmask.s_addr = 0;
717 	gateway.s_addr = 0;
718 	mydomain    = myname    = rootpath = NULL;
719 	mydomainlen = mynamelen = rootpathlen = 0;
720 
721 	/* default root server to bootp next-server */
722 	rootserver = bootp->bp_siaddr;
723 	/* assume that server name field is not overloaded by default */
724 	overloaded = 0;
725 	/* MTU can't be less than IP_MIN_MTU, set to 0 to indicate unset */
726 	myinterfacemtu = 0;
727 
728 	p = &bootp->bp_vend[4];
729 	limit = ((u_char*)bootp) + replylen;
730 	while (p < limit) {
731 		tag = *p++;
732 		if (tag == TAG_END)
733 			break;
734 		if (tag == TAG_PAD)
735 			continue;
736 		len = *p++;
737 #if 0 /* already done in bootpcheck() */
738 		if ((p + len) > limit) {
739 			printf("nfs_boot: option %d too long\n", tag);
740 			break;
741 		}
742 #endif
743 		switch (tag) {
744 		    case TAG_SUBNET_MASK:
745 			if (len < 4) {
746 				printf("nfs_boot: subnet mask < 4 bytes\n");
747 				break;
748 			}
749 			memcpy(&netmask, p, 4);
750 			break;
751 		    case TAG_GATEWAY:
752 			/* Routers */
753 			if (len < 4) {
754 				printf("nfs_boot: routers < 4 bytes\n");
755 				break;
756 			}
757 			memcpy(&gateway, p, 4);
758 			break;
759 		    case TAG_HOST_NAME:
760 			if (len >= sizeof(hostname)) {
761 				printf("nfs_boot: host name >= %lu bytes\n",
762 				       (u_long)sizeof(hostname));
763 				break;
764 			}
765 			myname = p;
766 			mynamelen = len;
767 			break;
768 		    case TAG_DOMAIN_NAME:
769 			if (len >= sizeof(domainname)) {
770 				printf("nfs_boot: domain name >= %lu bytes\n",
771 				       (u_long)sizeof(domainname));
772 				break;
773 			}
774 			mydomain = p;
775 			mydomainlen = len;
776 			break;
777 		    case TAG_ROOT_PATH:
778 			/* Leave some room for the server name. */
779 			if (len >= (MNAMELEN-10)) {
780 				printf("nfs_boot: rootpath >= %d bytes\n",
781 				       (MNAMELEN-10));
782 				break;
783 			}
784 			rootpath = p;
785 			rootpathlen = len;
786 			break;
787 		    case TAG_INTERFACE_MTU:
788 			if (len != 2) {
789 				printf("nfs_boot: interface-mtu len != 2 (%d)",
790 					len);
791 				break;
792 			}
793 			memcpy(&myinterfacemtu, p, 2);
794 			myinterfacemtu = ntohs(myinterfacemtu);
795 			break;
796 		    case TAG_SWAP_SERVER:
797 			/* override NFS server address */
798 			if (len < 4) {
799 				printf("nfs_boot: swap server < 4 bytes\n");
800 				break;
801 			}
802 			memcpy(&rootserver, p, 4);
803 			break;
804 #ifdef NFS_BOOT_DHCP
805 		    case TAG_OVERLOAD:
806 			if (len > 0 && ((*p & 0x02) != 0))
807 				/*
808 				 * The server name field in the dhcp packet
809 				 * is overloaded and we can't find server
810 				 * name there.
811 				 */
812 				overloaded = 1;
813 			break;
814 #endif
815 		    default:
816 			break;
817 		}
818 		p += len;
819 	}
820 
821 	/*
822 	 * Store and print network config info.
823 	 */
824 	if (myname) {
825 		myname[mynamelen] = '\0';
826 		strncpy(hostname, myname, sizeof(hostname));
827 		hostnamelen = mynamelen;
828 		printf("nfs_boot: my_name=%s\n", hostname);
829 	}
830 	if (mydomain) {
831 		mydomain[mydomainlen] = '\0';
832 		strncpy(domainname, mydomain, sizeof(domainname));
833 		domainnamelen = mydomainlen;
834 		printf("nfs_boot: my_domain=%s\n", domainname);
835 	}
836 	if (!(*flags & NFS_BOOT_HAS_MYIP)) {
837 		nd->nd_myip = bootp->bp_yiaddr;
838 		printf("nfs_boot: my_addr=%s\n", inet_ntoa(nd->nd_myip));
839 		*flags |= NFS_BOOT_HAS_MYIP;
840 	}
841 	if (!(*flags & NFS_BOOT_HAS_MASK)) {
842 		nd->nd_mask = netmask;
843 		printf("nfs_boot: my_mask=%s\n", inet_ntoa(nd->nd_mask));
844 		*flags |= NFS_BOOT_HAS_MASK;
845 	}
846 	if (!(*flags & NFS_BOOT_HAS_GWIP)) {
847 		nd->nd_gwip = gateway;
848 		printf("nfs_boot: gateway=%s\n", inet_ntoa(nd->nd_gwip));
849 		*flags |= NFS_BOOT_HAS_GWIP;
850 	}
851 	if (myinterfacemtu >= IP_MIN_MTU) {
852 		nd->nd_mtu = myinterfacemtu;
853 		printf("nfs_boot: mtu=%d\n", nd->nd_mtu);
854 	}
855 
856 	/*
857 	 * Store the information about our NFS root mount.
858 	 * The caller will print it, so be silent here.
859 	 */
860 	do {
861 		struct nfs_dlmount *ndm = &nd->nd_root;
862 
863 
864 		if (!(*flags & NFS_BOOT_HAS_SERVADDR)) {
865 			/* Server IP address. */
866 			sin = (struct sockaddr_in *) &ndm->ndm_saddr;
867 			memset((void *)sin, 0, sizeof(*sin));
868 			sin->sin_len = sizeof(*sin);
869 			sin->sin_family = AF_INET;
870 			sin->sin_addr = rootserver;
871 			*flags |= NFS_BOOT_HAS_SERVADDR;
872 		}
873 
874 		if (!(*flags & NFS_BOOT_HAS_SERVER)) {
875 			/* Server name. */
876 			if (!overloaded && bootp->bp_sname[0] != 0 &&
877 			    !memcmp(&rootserver, &bootp->bp_siaddr,
878 				  sizeof(struct in_addr)))
879 			{
880 				/* standard root server, we have the name */
881 				strncpy(ndm->ndm_host, bootp->bp_sname,
882 					BP_SNAME_LEN-1);
883 				*flags |= NFS_BOOT_HAS_SERVER;
884 			} else {
885 				/* Show the server IP address numerically. */
886 				strncpy(ndm->ndm_host, inet_ntoa(rootserver),
887 					BP_SNAME_LEN-1);
888 				*flags |= NFS_BOOT_HAS_SERVER;
889 			}
890 		}
891 
892 		if (!(*flags & NFS_BOOT_HAS_ROOTPATH)) {
893 			len = strlen(ndm->ndm_host);
894 			if (rootpath &&
895 			    len + 1 + rootpathlen + 1 <= sizeof(ndm->ndm_host))
896 			{
897 				ndm->ndm_host[len++] = ':';
898 				strncpy(ndm->ndm_host + len,
899 					rootpath, rootpathlen);
900 				ndm->ndm_host[len + rootpathlen] = '\0';
901 				*flags |= NFS_BOOT_HAS_ROOTPATH;
902 			} /* else: upper layer will handle error */
903 		}
904 	} while(0);
905 
906 #ifdef TFTPROOT
907 #if BP_FILE_LEN > MNAMELEN
908 #define BOOTFILELEN MNAMELEN
909 #else
910 #define BOOTFILELEN BP_FILE_LEN
911 #endif
912 	strncpy(nd->nd_bootfile, bootp->bp_file, BOOTFILELEN);
913 	nd->nd_bootfile[BOOTFILELEN - 1] = '\0';
914 #undef BOOTFILELEN
915 #endif /* TFTPROOT */
916 }
917