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