xref: /freebsd/sys/nfs/bootp_subr.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1995 Gordon Ross, Adam Glass
5  * Copyright (c) 1992 Regents of the University of California.
6  * All rights reserved.
7  *
8  * This software was developed by the Computer Systems Engineering group
9  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10  * contributed to Berkeley.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Lawrence Berkeley Laboratory and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  * based on:
41  *      nfs/krpc_subr.c
42  *	$NetBSD: krpc_subr.c,v 1.10 1995/08/08 20:43:43 gwr Exp $
43  */
44 
45 #define IN_HISTORICAL_NETS		/* include class masks */
46 
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
49 
50 #include "opt_bootp.h"
51 #include "opt_nfs.h"
52 #include "opt_rootdevname.h"
53 
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/endian.h>
57 #include <sys/jail.h>
58 #include <sys/kernel.h>
59 #include <sys/sockio.h>
60 #include <sys/malloc.h>
61 #include <sys/mount.h>
62 #include <sys/mbuf.h>
63 #include <sys/proc.h>
64 #include <sys/reboot.h>
65 #include <sys/socket.h>
66 #include <sys/socketvar.h>
67 #include <sys/sysctl.h>
68 #include <sys/uio.h>
69 
70 #include <net/if.h>
71 #include <net/if_var.h>
72 #include <net/route.h>
73 #include <net/route/route_ctl.h>
74 
75 #include <netinet/in.h>
76 #include <netinet/in_var.h>
77 #include <net/if_types.h>
78 #include <net/if_dl.h>
79 #include <net/vnet.h>
80 
81 #include <nfs/nfsproto.h>
82 #include <nfsclient/nfs.h>
83 #include <nfs/nfsdiskless.h>
84 #include <nfs/krpc.h>
85 #include <nfs/xdr_subs.h>
86 
87 #define BOOTP_MIN_LEN		300	/* Minimum size of bootp udp packet */
88 
89 #ifndef BOOTP_SETTLE_DELAY
90 #define BOOTP_SETTLE_DELAY 3
91 #endif
92 
93 /*
94  * Wait 10 seconds for interface appearance
95  * USB ethernet adapters might require some time to pop up
96  */
97 #ifndef	BOOTP_IFACE_WAIT_TIMEOUT
98 #define	BOOTP_IFACE_WAIT_TIMEOUT	10
99 #endif
100 
101 /*
102  * What is the longest we will wait before re-sending a request?
103  * Note this is also the frequency of "RPC timeout" messages.
104  * The re-send loop count sup linearly to this maximum, so the
105  * first complaint will happen after (1+2+3+4+5)=15 seconds.
106  */
107 #define	MAX_RESEND_DELAY 5	/* seconds */
108 
109 /* Definitions from RFC951 */
110 struct bootp_packet {
111 	u_int8_t op;
112 	u_int8_t htype;
113 	u_int8_t hlen;
114 	u_int8_t hops;
115 	u_int32_t xid;
116 	u_int16_t secs;
117 	u_int16_t flags;
118 	struct in_addr ciaddr;
119 	struct in_addr yiaddr;
120 	struct in_addr siaddr;
121 	struct in_addr giaddr;
122 	unsigned char chaddr[16];
123 	char sname[64];
124 	char file[128];
125 	unsigned char vend[1222];
126 };
127 
128 struct bootpc_ifcontext {
129 	STAILQ_ENTRY(bootpc_ifcontext) next;
130 	struct bootp_packet call;
131 	struct bootp_packet reply;
132 	int replylen;
133 	int overload;
134 	union {
135 		struct ifreq _ifreq;
136 		struct in_aliasreq _in_alias_req;
137 	} _req;
138 #define	ireq	_req._ifreq
139 #define	iareq	_req._in_alias_req
140 	if_t ifp;
141 	struct sockaddr_dl *sdl;
142 	struct sockaddr_in myaddr;
143 	struct sockaddr_in netmask;
144 	struct sockaddr_in gw;
145 	int gotgw;
146 	int gotnetmask;
147 	int gotrootpath;
148 	int outstanding;
149 	int sentmsg;
150 	u_int32_t xid;
151 	enum {
152 		IF_BOOTP_UNRESOLVED,
153 		IF_BOOTP_RESOLVED,
154 		IF_BOOTP_FAILED,
155 		IF_DHCP_UNRESOLVED,
156 		IF_DHCP_OFFERED,
157 		IF_DHCP_RESOLVED,
158 		IF_DHCP_FAILED,
159 	} state;
160 	int dhcpquerytype;		/* dhcp type sent */
161 	struct in_addr dhcpserver;
162 	int gotdhcpserver;
163 	uint16_t mtu;
164 };
165 
166 #define TAG_MAXLEN 1024
167 struct bootpc_tagcontext {
168 	char buf[TAG_MAXLEN + 1];
169 	int overload;
170 	int badopt;
171 	int badtag;
172 	int foundopt;
173 	int taglen;
174 };
175 
176 struct bootpc_globalcontext {
177 	STAILQ_HEAD(, bootpc_ifcontext) interfaces;
178 	u_int32_t xid;
179 	int any_root_overrides;
180 	int gotrootpath;
181 	int gotgw;
182 	int ifnum;
183 	int secs;
184 	int starttime;
185 	struct bootp_packet reply;
186 	int replylen;
187 	struct bootpc_ifcontext *setrootfs;
188 	struct bootpc_ifcontext *sethostname;
189 	struct bootpc_tagcontext tmptag;
190 	struct bootpc_tagcontext tag;
191 };
192 
193 #define IPPORT_BOOTPC 68
194 #define IPPORT_BOOTPS 67
195 
196 #define BOOTP_REQUEST 1
197 #define BOOTP_REPLY 2
198 
199 /* Common tags */
200 #define TAG_PAD		  0  /* Pad option, implicit length 1 */
201 #define TAG_SUBNETMASK	  1  /* RFC 950 subnet mask */
202 #define TAG_ROUTERS	  3  /* Routers (in order of preference) */
203 #define TAG_HOSTNAME	 12  /* Client host name */
204 #define TAG_ROOT	 17  /* Root path */
205 #define TAG_INTF_MTU	 26  /* Interface MTU Size (RFC2132) */
206 
207 /* DHCP specific tags */
208 #define TAG_OVERLOAD	 52  /* Option Overload */
209 #define TAG_MAXMSGSIZE   57  /* Maximum DHCP Message Size */
210 
211 #define TAG_END		255  /* End Option (i.e. no more options) */
212 
213 /* Overload values */
214 #define OVERLOAD_FILE     1
215 #define OVERLOAD_SNAME    2
216 
217 /* Site specific tags: */
218 #define TAG_ROOTOPTS	130
219 #define TAG_COOKIE	134	/* ascii info for userland, via sysctl */
220 
221 #define TAG_DHCP_MSGTYPE 53
222 #define TAG_DHCP_REQ_ADDR 50
223 #define TAG_DHCP_SERVERID 54
224 #define TAG_DHCP_LEASETIME 51
225 
226 #define TAG_VENDOR_INDENTIFIER 60
227 
228 #define DHCP_NOMSG    0
229 #define DHCP_DISCOVER 1
230 #define DHCP_OFFER    2
231 #define DHCP_REQUEST  3
232 #define DHCP_ACK      5
233 
234 /* NFS read/write block size */
235 #ifndef BOOTP_BLOCKSIZE
236 #define	BOOTP_BLOCKSIZE	8192
237 #endif
238 
239 static char bootp_cookie[128];
240 static struct socket *bootp_so;
241 SYSCTL_STRING(_kern, OID_AUTO, bootp_cookie, CTLFLAG_RD,
242 	bootp_cookie, 0, "Cookie (T134) supplied by bootp server");
243 
244 /* mountd RPC */
245 static int	md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp,
246 		    int *fhsizep, struct nfs_args *args, struct thread *td);
247 static int	setfs(struct sockaddr_in *addr, char *path, char *p,
248 		    const struct in_addr *siaddr);
249 static int	getdec(char **ptr);
250 static int	getip(char **ptr, struct in_addr *ip);
251 static void	mountopts(struct nfs_args *args, char *p);
252 static int	xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len);
253 static int	xdr_int_decode(struct mbuf **ptr, int *iptr);
254 static void	print_in_addr(struct in_addr addr);
255 static void	print_sin_addr(struct sockaddr_in *addr);
256 static void	clear_sinaddr(struct sockaddr_in *sin);
257 static void	allocifctx(struct bootpc_globalcontext *gctx);
258 static void	bootpc_compose_query(struct bootpc_ifcontext *ifctx,
259 		    struct thread *td);
260 static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx,
261 		    struct bootp_packet *bp, int len, int tag);
262 static void bootpc_tag_helper(struct bootpc_tagcontext *tctx,
263 		    unsigned char *start, int len, int tag);
264 
265 #ifdef BOOTP_DEBUG
266 void bootpboot_p_iflist(void);
267 #endif
268 
269 static int	bootpc_call(struct bootpc_globalcontext *gctx,
270 		    struct thread *td);
271 
272 static void	bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
273 		    struct thread *td);
274 
275 static void	bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
276 		    struct bootpc_globalcontext *gctx, struct thread *td);
277 
278 static void	bootpc_decode_reply(struct nfsv3_diskless *nd,
279 		    struct bootpc_ifcontext *ifctx,
280 		    struct bootpc_globalcontext *gctx);
281 
282 static int	bootpc_received(struct bootpc_globalcontext *gctx,
283 		    struct bootpc_ifcontext *ifctx);
284 
285 static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
286 static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
287 static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
288 
289 /*
290  * In order to have multiple active interfaces with address 0.0.0.0
291  * and be able to send data to a selected interface, we first set
292  * mask to /8 on all interfaces, and temporarily set it to /0 when
293  * doing sosend().
294  */
295 
296 #ifdef BOOTP_DEBUG
297 static u_int
298 bootpboot_p_ifa(void *ifp, struct ifaddr *ifa, u_int count __unused)
299 {
300 
301 	printf("%s flags %x, addr ",
302 	       if_name(ifp), if_getflags(ifp));
303 	print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
304 	printf(", broadcast ");
305 	print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
306 	printf(", netmask ");
307 	print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
308 	printf("\n");
309 
310 	return (0);
311 }
312 
313 void
314 bootpboot_p_iflist(void)
315 {
316 	struct epoch_tracker et;
317 	struct if_iter iter;
318 	if_t ifp;
319 
320 	printf("Interface list:\n");
321 	NET_EPOCH_ENTER(et);
322 	for (ifp = if_iter_start(&iter); ifp != NULL; ifp = if_iter_next(&iter))
323 		if_foreach_addr_type(ifp, AF_INET, bootpboot_p_ifa, ifp);
324 	if_iter_finish(&iter);
325 	NET_EPOCH_EXIT(et);
326 }
327 #endif /* defined(BOOTP_DEBUG) */
328 
329 static void
330 clear_sinaddr(struct sockaddr_in *sin)
331 {
332 
333 	bzero(sin, sizeof(*sin));
334 	sin->sin_len = sizeof(*sin);
335 	sin->sin_family = AF_INET;
336 	sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
337 	sin->sin_port = 0;
338 }
339 
340 static void
341 allocifctx(struct bootpc_globalcontext *gctx)
342 {
343 	struct bootpc_ifcontext *ifctx;
344 
345 	ifctx = malloc(sizeof(*ifctx), M_TEMP, M_WAITOK | M_ZERO);
346 	ifctx->xid = gctx->xid;
347 #ifdef BOOTP_NO_DHCP
348 	ifctx->state = IF_BOOTP_UNRESOLVED;
349 #else
350 	ifctx->state = IF_DHCP_UNRESOLVED;
351 #endif
352 	gctx->xid += 0x100;
353 	STAILQ_INSERT_TAIL(&gctx->interfaces, ifctx, next);
354 }
355 
356 static __inline int
357 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
358 {
359 
360 	if (ifctx->state == IF_BOOTP_RESOLVED ||
361 	    ifctx->state == IF_DHCP_RESOLVED)
362 		return 1;
363 	return 0;
364 }
365 
366 static __inline int
367 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
368 {
369 
370 	if (ifctx->state == IF_BOOTP_UNRESOLVED ||
371 	    ifctx->state == IF_DHCP_UNRESOLVED)
372 		return 1;
373 	return 0;
374 }
375 
376 static __inline int
377 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
378 {
379 
380 	if (ifctx->state == IF_BOOTP_FAILED ||
381 	    ifctx->state == IF_DHCP_FAILED)
382 		return 1;
383 	return 0;
384 }
385 
386 static int
387 bootpc_received(struct bootpc_globalcontext *gctx,
388     struct bootpc_ifcontext *ifctx)
389 {
390 	unsigned char dhcpreplytype;
391 	char *p;
392 
393 	/*
394 	 * Need timeout for fallback to less
395 	 * desirable alternative.
396 	 */
397 
398 	/* This call used for the side effect (badopt flag) */
399 	(void) bootpc_tag(&gctx->tmptag, &gctx->reply,
400 			  gctx->replylen,
401 			  TAG_END);
402 
403 	/* If packet is invalid, ignore it */
404 	if (gctx->tmptag.badopt != 0)
405 		return 0;
406 
407 	p = bootpc_tag(&gctx->tmptag, &gctx->reply,
408 		       gctx->replylen, TAG_DHCP_MSGTYPE);
409 	if (p != NULL)
410 		dhcpreplytype = *p;
411 	else
412 		dhcpreplytype = DHCP_NOMSG;
413 
414 	switch (ifctx->dhcpquerytype) {
415 	case DHCP_DISCOVER:
416 		if (dhcpreplytype != DHCP_OFFER 	/* Normal DHCP offer */
417 #ifndef BOOTP_FORCE_DHCP
418 		    && dhcpreplytype != DHCP_NOMSG	/* Fallback to BOOTP */
419 #endif
420 			)
421 			return 0;
422 		break;
423 	case DHCP_REQUEST:
424 		if (dhcpreplytype != DHCP_ACK)
425 			return 0;
426 	case DHCP_NOMSG:
427 		break;
428 	}
429 
430 	/* Ignore packet unless it gives us a root tag we didn't have */
431 
432 	if ((ifctx->state == IF_BOOTP_RESOLVED ||
433 	     (ifctx->dhcpquerytype == DHCP_DISCOVER &&
434 	      (ifctx->state == IF_DHCP_OFFERED ||
435 	       ifctx->state == IF_DHCP_RESOLVED))) &&
436 	    (bootpc_tag(&gctx->tmptag, &ifctx->reply,
437 			ifctx->replylen,
438 			TAG_ROOT) != NULL ||
439 	     bootpc_tag(&gctx->tmptag, &gctx->reply,
440 			gctx->replylen,
441 			TAG_ROOT) == NULL))
442 		return 0;
443 
444 	bcopy(&gctx->reply, &ifctx->reply, gctx->replylen);
445 	ifctx->replylen = gctx->replylen;
446 
447 	/* XXX: Only reset if 'perfect' response */
448 	if (ifctx->state == IF_BOOTP_UNRESOLVED)
449 		ifctx->state = IF_BOOTP_RESOLVED;
450 	else if (ifctx->state == IF_DHCP_UNRESOLVED &&
451 		 ifctx->dhcpquerytype == DHCP_DISCOVER) {
452 		if (dhcpreplytype == DHCP_OFFER)
453 			ifctx->state = IF_DHCP_OFFERED;
454 		else
455 			ifctx->state = IF_BOOTP_RESOLVED;	/* Fallback */
456 	} else if (ifctx->state == IF_DHCP_OFFERED &&
457 		   ifctx->dhcpquerytype == DHCP_REQUEST)
458 		ifctx->state = IF_DHCP_RESOLVED;
459 
460 	if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
461 	    ifctx->state != IF_BOOTP_RESOLVED) {
462 		p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
463 			       ifctx->replylen, TAG_DHCP_SERVERID);
464 		if (p != NULL && gctx->tmptag.taglen == 4) {
465 			memcpy(&ifctx->dhcpserver, p, 4);
466 			ifctx->gotdhcpserver = 1;
467 		} else
468 			ifctx->gotdhcpserver = 0;
469 		return 1;
470 	}
471 
472 	ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
473 					 ifctx->replylen,
474 					 TAG_ROOT) != NULL);
475 	ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
476 				   ifctx->replylen,
477 				   TAG_ROUTERS) != NULL);
478 	ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
479 					ifctx->replylen,
480 					TAG_SUBNETMASK) != NULL);
481 	return 1;
482 }
483 
484 static int
485 bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
486 {
487 	struct sockaddr_in *sin, dst;
488 	struct uio auio;
489 	struct sockopt sopt;
490 	struct iovec aio;
491 	int error, on, rcvflg, timo, len;
492 	time_t atimo;
493 	time_t rtimo;
494 	struct timeval tv;
495 	struct bootpc_ifcontext *ifctx;
496 	int outstanding;
497 	int gotrootpath;
498 	int retry;
499 	const char *s;
500 
501 	tv.tv_sec = 1;
502 	tv.tv_usec = 0;
503 	bzero(&sopt, sizeof(sopt));
504 	sopt.sopt_dir = SOPT_SET;
505 	sopt.sopt_level = SOL_SOCKET;
506 	sopt.sopt_name = SO_RCVTIMEO;
507 	sopt.sopt_val = &tv;
508 	sopt.sopt_valsize = sizeof tv;
509 
510 	error = sosetopt(bootp_so, &sopt);
511 	if (error != 0)
512 		goto out;
513 
514 	/*
515 	 * Enable broadcast.
516 	 */
517 	on = 1;
518 	sopt.sopt_name = SO_BROADCAST;
519 	sopt.sopt_val = &on;
520 	sopt.sopt_valsize = sizeof on;
521 
522 	error = sosetopt(bootp_so, &sopt);
523 	if (error != 0)
524 		goto out;
525 
526 	/*
527 	 * Disable routing.
528 	 */
529 
530 	on = 1;
531 	sopt.sopt_name = SO_DONTROUTE;
532 	sopt.sopt_val = &on;
533 	sopt.sopt_valsize = sizeof on;
534 
535 	error = sosetopt(bootp_so, &sopt);
536 	if (error != 0)
537 		goto out;
538 
539 	/*
540 	 * Bind the local endpoint to a bootp client port.
541 	 */
542 	sin = &dst;
543 	clear_sinaddr(sin);
544 	sin->sin_port = htons(IPPORT_BOOTPC);
545 	error = sobind(bootp_so, (struct sockaddr *)sin, td);
546 	if (error != 0) {
547 		printf("bind failed\n");
548 		goto out;
549 	}
550 
551 	/*
552 	 * Setup socket address for the server.
553 	 */
554 	sin = &dst;
555 	clear_sinaddr(sin);
556 	sin->sin_addr.s_addr = INADDR_BROADCAST;
557 	sin->sin_port = htons(IPPORT_BOOTPS);
558 
559 	/*
560 	 * Send it, repeatedly, until a reply is received,
561 	 * but delay each re-send by an increasing amount.
562 	 * If the delay hits the maximum, start complaining.
563 	 */
564 	timo = 0;
565 	rtimo = 0;
566 	for (;;) {
567 		outstanding = 0;
568 		gotrootpath = 0;
569 
570 		STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
571 			if (bootpc_ifctx_isresolved(ifctx) != 0 &&
572 			    bootpc_tag(&gctx->tmptag, &ifctx->reply,
573 				       ifctx->replylen,
574 				       TAG_ROOT) != NULL)
575 				gotrootpath = 1;
576 		}
577 
578 		STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
579 			struct in_aliasreq *ifra = &ifctx->iareq;
580 			sin = (struct sockaddr_in *)&ifra->ifra_mask;
581 
582 			ifctx->outstanding = 0;
583 			if (bootpc_ifctx_isresolved(ifctx)  != 0 &&
584 			    gotrootpath != 0) {
585 				continue;
586 			}
587 			if (bootpc_ifctx_isfailed(ifctx) != 0)
588 				continue;
589 
590 			outstanding++;
591 			ifctx->outstanding = 1;
592 
593 			/* Proceed to next step in DHCP negotiation */
594 			if ((ifctx->state == IF_DHCP_OFFERED &&
595 			     ifctx->dhcpquerytype != DHCP_REQUEST) ||
596 			    (ifctx->state == IF_DHCP_UNRESOLVED &&
597 			     ifctx->dhcpquerytype != DHCP_DISCOVER) ||
598 			    (ifctx->state == IF_BOOTP_UNRESOLVED &&
599 			     ifctx->dhcpquerytype != DHCP_NOMSG)) {
600 				ifctx->sentmsg = 0;
601 				bootpc_compose_query(ifctx, td);
602 			}
603 
604 			/* Send BOOTP request (or re-send). */
605 
606 			if (ifctx->sentmsg == 0) {
607 				switch(ifctx->dhcpquerytype) {
608 				case DHCP_DISCOVER:
609 					s = "DHCP Discover";
610 					break;
611 				case DHCP_REQUEST:
612 					s = "DHCP Request";
613 					break;
614 				case DHCP_NOMSG:
615 				default:
616 					s = "BOOTP Query";
617 					break;
618 				}
619 				printf("Sending %s packet from "
620 				       "interface %s (%*D)\n",
621 				       s,
622 				       ifctx->ireq.ifr_name,
623 				       ifctx->sdl->sdl_alen,
624 				       (unsigned char *) LLADDR(ifctx->sdl),
625 				       ":");
626 				ifctx->sentmsg = 1;
627 			}
628 
629 			aio.iov_base = (caddr_t) &ifctx->call;
630 			aio.iov_len = sizeof(ifctx->call);
631 
632 			auio.uio_iov = &aio;
633 			auio.uio_iovcnt = 1;
634 			auio.uio_segflg = UIO_SYSSPACE;
635 			auio.uio_rw = UIO_WRITE;
636 			auio.uio_offset = 0;
637 			auio.uio_resid = sizeof(ifctx->call);
638 			auio.uio_td = td;
639 
640 			/* Set netmask to 0.0.0.0 */
641 			clear_sinaddr(sin);
642 			error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
643 			    td);
644 			if (error != 0)
645 				panic("%s: SIOCAIFADDR, error=%d", __func__,
646 				    error);
647 
648 			error = sosend(bootp_so, (struct sockaddr *) &dst,
649 				       &auio, NULL, NULL, 0, td);
650 			if (error != 0)
651 				printf("%s: sosend: %d state %08x\n", __func__,
652 				    error, (int )bootp_so->so_state);
653 
654 			/* Set netmask to 255.0.0.0 */
655 			sin->sin_addr.s_addr = htonl(0xff000000);
656 			error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
657 			    td);
658 			if (error != 0)
659 				panic("%s: SIOCAIFADDR, error=%d", __func__,
660 				    error);
661 		}
662 
663 		if (outstanding == 0 &&
664 		    (rtimo == 0 || time_second >= rtimo)) {
665 			error = 0;
666 			goto out;
667 		}
668 
669 		/* Determine new timeout. */
670 		if (timo < MAX_RESEND_DELAY)
671 			timo++;
672 		else {
673 			printf("DHCP/BOOTP timeout for server ");
674 			print_sin_addr(&dst);
675 			printf("\n");
676 		}
677 
678 		/*
679 		 * Wait for up to timo seconds for a reply.
680 		 * The socket receive timeout was set to 1 second.
681 		 */
682 		atimo = timo + time_second;
683 		while (time_second < atimo) {
684 			aio.iov_base = (caddr_t) &gctx->reply;
685 			aio.iov_len = sizeof(gctx->reply);
686 
687 			auio.uio_iov = &aio;
688 			auio.uio_iovcnt = 1;
689 			auio.uio_segflg = UIO_SYSSPACE;
690 			auio.uio_rw = UIO_READ;
691 			auio.uio_offset = 0;
692 			auio.uio_resid = sizeof(gctx->reply);
693 			auio.uio_td = td;
694 
695 			rcvflg = 0;
696 			error = soreceive(bootp_so, NULL, &auio,
697 					  NULL, NULL, &rcvflg);
698 			gctx->secs = time_second - gctx->starttime;
699 			STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
700 				if (bootpc_ifctx_isresolved(ifctx) != 0 ||
701 				    bootpc_ifctx_isfailed(ifctx) != 0)
702 					continue;
703 
704 				ifctx->call.secs = htons(gctx->secs);
705 			}
706 			if (error == EWOULDBLOCK)
707 				continue;
708 			if (error != 0)
709 				goto out;
710 			len = sizeof(gctx->reply) - auio.uio_resid;
711 
712 			/* Do we have the required number of bytes ? */
713 			if (len < BOOTP_MIN_LEN)
714 				continue;
715 			gctx->replylen = len;
716 
717 			/* Is it a reply? */
718 			if (gctx->reply.op != BOOTP_REPLY)
719 				continue;
720 
721 			/* Is this an answer to our query */
722 			STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
723 				if (gctx->reply.xid != ifctx->call.xid)
724 					continue;
725 
726 				/* Same HW address size ? */
727 				if (gctx->reply.hlen != ifctx->call.hlen)
728 					continue;
729 
730 				/* Correct HW address ? */
731 				if (bcmp(gctx->reply.chaddr,
732 					 ifctx->call.chaddr,
733 					 ifctx->call.hlen) != 0)
734 					continue;
735 
736 				break;
737 			}
738 
739 			if (ifctx != NULL) {
740 				s =  bootpc_tag(&gctx->tmptag,
741 						&gctx->reply,
742 						gctx->replylen,
743 						TAG_DHCP_MSGTYPE);
744 				if (s != NULL) {
745 					switch (*s) {
746 					case DHCP_OFFER:
747 						s = "DHCP Offer";
748 						break;
749 					case DHCP_ACK:
750 						s = "DHCP Ack";
751 						break;
752 					default:
753 						s = "DHCP (unexpected)";
754 						break;
755 					}
756 				} else
757 					s = "BOOTP Reply";
758 
759 				printf("Received %s packet"
760 				       " on %s from ",
761 				       s,
762 				       ifctx->ireq.ifr_name);
763 				print_in_addr(gctx->reply.siaddr);
764 				if (gctx->reply.giaddr.s_addr !=
765 				    htonl(INADDR_ANY)) {
766 					printf(" via ");
767 					print_in_addr(gctx->reply.giaddr);
768 				}
769 				if (bootpc_received(gctx, ifctx) != 0) {
770 					printf(" (accepted)");
771 					if (ifctx->outstanding) {
772 						ifctx->outstanding = 0;
773 						outstanding--;
774 					}
775 					/* Network settle delay */
776 					if (outstanding == 0)
777 						atimo = time_second +
778 							BOOTP_SETTLE_DELAY;
779 				} else
780 					printf(" (ignored)");
781 				if (ifctx->gotrootpath ||
782 				    gctx->any_root_overrides) {
783 					gotrootpath = 1;
784 					rtimo = time_second +
785 						BOOTP_SETTLE_DELAY;
786 					if (ifctx->gotrootpath)
787 						printf(" (got root path)");
788 				}
789 				printf("\n");
790 			}
791 		} /* while secs */
792 #ifdef BOOTP_TIMEOUT
793 		if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
794 			break;
795 #endif
796 		/* Force a retry if halfway in DHCP negotiation */
797 		retry = 0;
798 		STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
799 			if (ifctx->state == IF_DHCP_OFFERED) {
800 				if (ifctx->dhcpquerytype == DHCP_DISCOVER)
801 					retry = 1;
802 				else
803 					ifctx->state = IF_DHCP_UNRESOLVED;
804 			}
805 
806 		if (retry != 0)
807 			continue;
808 
809 		if (gotrootpath != 0) {
810 			gctx->gotrootpath = gotrootpath;
811 			if (rtimo != 0 && time_second >= rtimo)
812 				break;
813 		}
814 	} /* forever send/receive */
815 
816 	/*
817 	 * XXX: These are errors of varying seriousness being silently
818 	 * ignored
819 	 */
820 
821 	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
822 		if (bootpc_ifctx_isresolved(ifctx) == 0) {
823 			printf("%s timeout for interface %s\n",
824 			       ifctx->dhcpquerytype != DHCP_NOMSG ?
825 			       "DHCP" : "BOOTP",
826 			       ifctx->ireq.ifr_name);
827 		}
828 
829 	if (gctx->gotrootpath != 0) {
830 #if 0
831 		printf("Got a root path, ignoring remaining timeout\n");
832 #endif
833 		error = 0;
834 		goto out;
835 	}
836 #ifndef BOOTP_NFSROOT
837 	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
838 		if (bootpc_ifctx_isresolved(ifctx) != 0) {
839 			error = 0;
840 			goto out;
841 		}
842 #endif
843 	error = ETIMEDOUT;
844 
845 out:
846 	return (error);
847 }
848 
849 static void
850 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
851 {
852 	struct ifreq *ifr;
853 	struct in_aliasreq *ifra;
854 	struct sockaddr_in *sin;
855 	int error;
856 
857 	ifr = &ifctx->ireq;
858 	ifra = &ifctx->iareq;
859 
860 	/*
861 	 * Bring up the interface.
862 	 *
863 	 * Get the old interface flags and or IFF_UP into them; if
864 	 * IFF_UP set blindly, interface selection can be clobbered.
865 	 */
866 	error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
867 	if (error != 0)
868 		panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
869 	ifr->ifr_flags |= IFF_UP;
870 	error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
871 	if (error != 0)
872 		panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
873 
874 	/*
875 	 * Do enough of ifconfig(8) so that the chosen interface
876 	 * can talk to the servers. Set address to 0.0.0.0/8 and
877 	 * broadcast address to local broadcast.
878 	 */
879 	sin = (struct sockaddr_in *)&ifra->ifra_addr;
880 	clear_sinaddr(sin);
881 	sin = (struct sockaddr_in *)&ifra->ifra_mask;
882 	clear_sinaddr(sin);
883 	sin->sin_addr.s_addr = htonl(0xff000000);
884 	sin = (struct sockaddr_in *)&ifra->ifra_broadaddr;
885 	clear_sinaddr(sin);
886 	sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
887 	error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
888 	if (error != 0)
889 		panic("%s: SIOCAIFADDR, error=%d", __func__, error);
890 }
891 
892 static void
893 bootpc_shutdown_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
894 {
895 	struct ifreq *ifr;
896 	struct sockaddr_in *sin;
897 	int error;
898 
899 	ifr = &ifctx->ireq;
900 
901 	printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
902 	error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
903 	if (error != 0)
904 		panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
905 	ifr->ifr_flags &= ~IFF_UP;
906 	error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
907 	if (error != 0)
908 		panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
909 
910 	sin = (struct sockaddr_in *) &ifr->ifr_addr;
911 	clear_sinaddr(sin);
912 	error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
913 	if (error != 0)
914 		panic("%s: SIOCDIFADDR, error=%d", __func__, error);
915 }
916 
917 static void
918 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
919     struct bootpc_globalcontext *gctx, struct thread *td)
920 {
921 	int error;
922 	struct sockaddr_in *sin;
923 	struct ifreq *ifr;
924 	struct in_aliasreq *ifra;
925 	struct sockaddr_in *myaddr;
926 	struct sockaddr_in *netmask;
927 
928 	ifr = &ifctx->ireq;
929 	ifra = &ifctx->iareq;
930 	myaddr = &ifctx->myaddr;
931 	netmask = &ifctx->netmask;
932 
933 	if (bootpc_ifctx_isresolved(ifctx) == 0) {
934 		/* Shutdown interfaces where BOOTP failed */
935 		bootpc_shutdown_interface(ifctx, td);
936 		return;
937 	}
938 
939 	printf("Adjusted interface %s", ifctx->ireq.ifr_name);
940 
941 	/* Do BOOTP interface options */
942 	if (ifctx->mtu != 0) {
943 		printf(" (MTU=%d%s)", ifctx->mtu,
944 		    (ifctx->mtu > 1514) ? "/JUMBO" : "");
945 		ifr->ifr_mtu = ifctx->mtu;
946 		error = ifioctl(bootp_so, SIOCSIFMTU, (caddr_t) ifr, td);
947 		if (error != 0)
948 			panic("%s: SIOCSIFMTU, error=%d", __func__, error);
949 	}
950 	printf("\n");
951 
952 	/*
953 	 * Do enough of ifconfig(8) so that the chosen interface
954 	 * can talk to the servers.  (just set the address)
955 	 */
956 	sin = (struct sockaddr_in *) &ifr->ifr_addr;
957 	clear_sinaddr(sin);
958 	error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
959 	if (error != 0)
960 		panic("%s: SIOCDIFADDR, error=%d", __func__, error);
961 
962 	bcopy(myaddr, &ifra->ifra_addr, sizeof(*myaddr));
963 	bcopy(netmask, &ifra->ifra_mask, sizeof(*netmask));
964 	clear_sinaddr(&ifra->ifra_broadaddr);
965 	ifra->ifra_broadaddr.sin_addr.s_addr = myaddr->sin_addr.s_addr |
966 	    ~netmask->sin_addr.s_addr;
967 
968 	error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
969 	if (error != 0)
970 		panic("%s: SIOCAIFADDR, error=%d", __func__, error);
971 }
972 
973 static void
974 bootpc_add_default_route(struct bootpc_ifcontext *ifctx)
975 {
976 	int error;
977 	struct sockaddr_in defdst;
978 	struct sockaddr_in defmask;
979 	struct rt_addrinfo info;
980 	struct rib_cmd_info rc;
981 
982 	if (ifctx->gw.sin_addr.s_addr == htonl(INADDR_ANY))
983 		return;
984 
985 	clear_sinaddr(&defdst);
986 	clear_sinaddr(&defmask);
987 
988 	bzero((caddr_t)&info, sizeof(info));
989 	info.rti_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
990 	info.rti_info[RTAX_DST] = (struct sockaddr *)&defdst;
991 	info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&defmask;
992 	info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ifctx->gw;
993 
994 	error = rib_action(RT_DEFAULT_FIB, RTM_ADD, &info, &rc);
995 
996 	if (error != 0) {
997 		printf("%s: RTM_ADD, error=%d\n", __func__, error);
998 	}
999 }
1000 
1001 static void
1002 bootpc_remove_default_route(struct bootpc_ifcontext *ifctx)
1003 {
1004 	int error;
1005 	struct sockaddr_in defdst;
1006 	struct sockaddr_in defmask;
1007 	struct rt_addrinfo info;
1008 	struct rib_cmd_info rc;
1009 
1010 	if (ifctx->gw.sin_addr.s_addr == htonl(INADDR_ANY))
1011 		return;
1012 
1013 	clear_sinaddr(&defdst);
1014 	clear_sinaddr(&defmask);
1015 
1016 	bzero((caddr_t)&info, sizeof(info));
1017 	info.rti_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
1018 	info.rti_info[RTAX_DST] = (struct sockaddr *)&defdst;
1019 	info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&defmask;
1020 	info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ifctx->gw;
1021 
1022 	error = rib_action(RT_DEFAULT_FIB, RTM_DELETE, &info, &rc);
1023 	if (error != 0) {
1024 		printf("%s: RTM_DELETE, error=%d\n", __func__, error);
1025 	}
1026 }
1027 
1028 static int
1029 setfs(struct sockaddr_in *addr, char *path, char *p,
1030     const struct in_addr *siaddr)
1031 {
1032 
1033 	if (getip(&p, &addr->sin_addr) == 0) {
1034 		if (siaddr != NULL && *p == '/')
1035 			bcopy(siaddr, &addr->sin_addr, sizeof(struct in_addr));
1036 		else
1037 			return 0;
1038 	} else {
1039 		if (*p != ':')
1040 			return 0;
1041 		p++;
1042 	}
1043 
1044 	addr->sin_len = sizeof(struct sockaddr_in);
1045 	addr->sin_family = AF_INET;
1046 
1047 	strlcpy(path, p, MNAMELEN);
1048 	return 1;
1049 }
1050 
1051 static int
1052 getip(char **ptr, struct in_addr *addr)
1053 {
1054 	char *p;
1055 	unsigned int ip;
1056 	int val;
1057 
1058 	p = *ptr;
1059 	ip = 0;
1060 	if (((val = getdec(&p)) < 0) || (val > 255))
1061 		return 0;
1062 	ip = val << 24;
1063 	if (*p != '.')
1064 		return 0;
1065 	p++;
1066 	if (((val = getdec(&p)) < 0) || (val > 255))
1067 		return 0;
1068 	ip |= (val << 16);
1069 	if (*p != '.')
1070 		return 0;
1071 	p++;
1072 	if (((val = getdec(&p)) < 0) || (val > 255))
1073 		return 0;
1074 	ip |= (val << 8);
1075 	if (*p != '.')
1076 		return 0;
1077 	p++;
1078 	if (((val = getdec(&p)) < 0) || (val > 255))
1079 		return 0;
1080 	ip |= val;
1081 
1082 	addr->s_addr = htonl(ip);
1083 	*ptr = p;
1084 	return 1;
1085 }
1086 
1087 static int
1088 getdec(char **ptr)
1089 {
1090 	char *p;
1091 	int ret;
1092 
1093 	p = *ptr;
1094 	ret = 0;
1095 	if ((*p < '0') || (*p > '9'))
1096 		return -1;
1097 	while ((*p >= '0') && (*p <= '9')) {
1098 		ret = ret * 10 + (*p - '0');
1099 		p++;
1100 	}
1101 	*ptr = p;
1102 	return ret;
1103 }
1104 
1105 static void
1106 mountopts(struct nfs_args *args, char *p)
1107 {
1108 	args->version = NFS_ARGSVERSION;
1109 	args->rsize = BOOTP_BLOCKSIZE;
1110 	args->wsize = BOOTP_BLOCKSIZE;
1111 	args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
1112 	args->sotype = SOCK_DGRAM;
1113 	if (p != NULL)
1114 		nfs_parse_options(p, args);
1115 }
1116 
1117 static int
1118 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
1119 {
1120 	struct mbuf *m;
1121 	int alignedlen;
1122 
1123 	m = *mptr;
1124 	alignedlen = ( len + 3 ) & ~3;
1125 
1126 	if (m->m_len < alignedlen) {
1127 		m = m_pullup(m, alignedlen);
1128 		if (m == NULL) {
1129 			*mptr = NULL;
1130 			return EBADRPC;
1131 		}
1132 	}
1133 	bcopy(mtod(m, u_char *), buf, len);
1134 	m_adj(m, alignedlen);
1135 	*mptr = m;
1136 	return 0;
1137 }
1138 
1139 static int
1140 xdr_int_decode(struct mbuf **mptr, int *iptr)
1141 {
1142 	u_int32_t i;
1143 
1144 	if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
1145 		return EBADRPC;
1146 	*iptr = fxdr_unsigned(u_int32_t, i);
1147 	return 0;
1148 }
1149 
1150 static void
1151 print_sin_addr(struct sockaddr_in *sin)
1152 {
1153 
1154 	print_in_addr(sin->sin_addr);
1155 }
1156 
1157 static void
1158 print_in_addr(struct in_addr addr)
1159 {
1160 	unsigned int ip;
1161 
1162 	ip = ntohl(addr.s_addr);
1163 	printf("%d.%d.%d.%d",
1164 	       ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1165 }
1166 
1167 static void
1168 bootpc_compose_query(struct bootpc_ifcontext *ifctx, struct thread *td)
1169 {
1170 	unsigned char *vendp;
1171 	unsigned char vendor_client[64];
1172 	uint32_t leasetime;
1173 	uint8_t vendor_client_len;
1174 
1175 	ifctx->gotrootpath = 0;
1176 
1177 	bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1178 
1179 	/* bootpc part */
1180 	ifctx->call.op = BOOTP_REQUEST; 	/* BOOTREQUEST */
1181 	ifctx->call.htype = 1;			/* 10mb ethernet */
1182 	ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1183 	ifctx->call.hops = 0;
1184 	if (bootpc_ifctx_isunresolved(ifctx) != 0)
1185 		ifctx->xid++;
1186 	ifctx->call.xid = txdr_unsigned(ifctx->xid);
1187 	bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1188 
1189 	vendp = ifctx->call.vend;
1190 	*vendp++ = 99;		/* RFC1048 cookie */
1191 	*vendp++ = 130;
1192 	*vendp++ = 83;
1193 	*vendp++ = 99;
1194 	*vendp++ = TAG_MAXMSGSIZE;
1195 	*vendp++ = 2;
1196 	*vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1197 	*vendp++ = sizeof(struct bootp_packet) & 255;
1198 
1199 	snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1200 		ostype, MACHINE, osrelease);
1201 	vendor_client_len = strlen(vendor_client);
1202 	*vendp++ = TAG_VENDOR_INDENTIFIER;
1203 	*vendp++ = vendor_client_len;
1204 	memcpy(vendp, vendor_client, vendor_client_len);
1205 	vendp += vendor_client_len;
1206 	ifctx->dhcpquerytype = DHCP_NOMSG;
1207 	switch (ifctx->state) {
1208 	case IF_DHCP_UNRESOLVED:
1209 		*vendp++ = TAG_DHCP_MSGTYPE;
1210 		*vendp++ = 1;
1211 		*vendp++ = DHCP_DISCOVER;
1212 		ifctx->dhcpquerytype = DHCP_DISCOVER;
1213 		ifctx->gotdhcpserver = 0;
1214 		break;
1215 	case IF_DHCP_OFFERED:
1216 		*vendp++ = TAG_DHCP_MSGTYPE;
1217 		*vendp++ = 1;
1218 		*vendp++ = DHCP_REQUEST;
1219 		ifctx->dhcpquerytype = DHCP_REQUEST;
1220 		*vendp++ = TAG_DHCP_REQ_ADDR;
1221 		*vendp++ = 4;
1222 		memcpy(vendp, &ifctx->reply.yiaddr, 4);
1223 		vendp += 4;
1224 		if (ifctx->gotdhcpserver != 0) {
1225 			*vendp++ = TAG_DHCP_SERVERID;
1226 			*vendp++ = 4;
1227 			memcpy(vendp, &ifctx->dhcpserver, 4);
1228 			vendp += 4;
1229 		}
1230 		*vendp++ = TAG_DHCP_LEASETIME;
1231 		*vendp++ = 4;
1232 		leasetime = htonl(300);
1233 		memcpy(vendp, &leasetime, 4);
1234 		vendp += 4;
1235 		break;
1236 	default:
1237 		break;
1238 	}
1239 	*vendp = TAG_END;
1240 
1241 	ifctx->call.secs = 0;
1242 	ifctx->call.flags = htons(0x8000); /* We need a broadcast answer */
1243 }
1244 
1245 static int
1246 bootpc_hascookie(struct bootp_packet *bp)
1247 {
1248 
1249 	return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1250 		bp->vend[2] == 83 && bp->vend[3] == 99);
1251 }
1252 
1253 static void
1254 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1255     unsigned char *start, int len, int tag)
1256 {
1257 	unsigned char *j;
1258 	unsigned char *ej;
1259 	unsigned char code;
1260 
1261 	if (tctx->badtag != 0 || tctx->badopt != 0)
1262 		return;
1263 
1264 	j = start;
1265 	ej = j + len;
1266 
1267 	while (j < ej) {
1268 		code = *j++;
1269 		if (code == TAG_PAD)
1270 			continue;
1271 		if (code == TAG_END)
1272 			return;
1273 		if (j >= ej || j + *j + 1 > ej) {
1274 			tctx->badopt = 1;
1275 			return;
1276 		}
1277 		len = *j++;
1278 		if (code == tag) {
1279 			if (tctx->taglen + len > TAG_MAXLEN) {
1280 				tctx->badtag = 1;
1281 				return;
1282 			}
1283 			tctx->foundopt = 1;
1284 			if (len > 0)
1285 				memcpy(tctx->buf + tctx->taglen,
1286 				       j, len);
1287 			tctx->taglen += len;
1288 		}
1289 		if (code == TAG_OVERLOAD)
1290 			tctx->overload = *j;
1291 
1292 		j += len;
1293 	}
1294 }
1295 
1296 static unsigned char *
1297 bootpc_tag(struct bootpc_tagcontext *tctx,
1298     struct bootp_packet *bp, int len, int tag)
1299 {
1300 	tctx->overload = 0;
1301 	tctx->badopt = 0;
1302 	tctx->badtag = 0;
1303 	tctx->foundopt = 0;
1304 	tctx->taglen = 0;
1305 
1306 	if (bootpc_hascookie(bp) == 0)
1307 		return NULL;
1308 
1309 	bootpc_tag_helper(tctx, &bp->vend[4],
1310 			  (unsigned char *) bp + len - &bp->vend[4], tag);
1311 
1312 	if ((tctx->overload & OVERLOAD_FILE) != 0)
1313 		bootpc_tag_helper(tctx,
1314 				  (unsigned char *) bp->file,
1315 				  sizeof(bp->file),
1316 				  tag);
1317 	if ((tctx->overload & OVERLOAD_SNAME) != 0)
1318 		bootpc_tag_helper(tctx,
1319 				  (unsigned char *) bp->sname,
1320 				  sizeof(bp->sname),
1321 				  tag);
1322 
1323 	if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1324 		return NULL;
1325 	tctx->buf[tctx->taglen] = '\0';
1326 	return tctx->buf;
1327 }
1328 
1329 static void
1330 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1331     struct bootpc_globalcontext *gctx)
1332 {
1333 	char *p, *s;
1334 
1335 	ifctx->gotgw = 0;
1336 	ifctx->gotnetmask = 0;
1337 
1338 	clear_sinaddr(&ifctx->myaddr);
1339 	clear_sinaddr(&ifctx->netmask);
1340 	clear_sinaddr(&ifctx->gw);
1341 
1342 	ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1343 
1344 	printf("%s at ", ifctx->ireq.ifr_name);
1345 	print_sin_addr(&ifctx->myaddr);
1346 	printf(" server ");
1347 	print_in_addr(ifctx->reply.siaddr);
1348 
1349 	ifctx->gw.sin_addr = ifctx->reply.giaddr;
1350 	if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1351 		printf(" via gateway ");
1352 		print_in_addr(ifctx->reply.giaddr);
1353 	}
1354 
1355 	/* This call used for the side effect (overload flag) */
1356 	(void) bootpc_tag(&gctx->tmptag,
1357 			  &ifctx->reply, ifctx->replylen, TAG_END);
1358 
1359 	if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1360 		if (ifctx->reply.sname[0] != '\0')
1361 			printf(" server name %s", ifctx->reply.sname);
1362 	if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1363 		if (ifctx->reply.file[0] != '\0')
1364 			printf(" boot file %s", ifctx->reply.file);
1365 
1366 	printf("\n");
1367 
1368 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1369 		       TAG_SUBNETMASK);
1370 	if (p != NULL) {
1371 		if (gctx->tag.taglen != 4)
1372 			panic("bootpc: subnet mask len is %d",
1373 			      gctx->tag.taglen);
1374 		bcopy(p, &ifctx->netmask.sin_addr, 4);
1375 		ifctx->gotnetmask = 1;
1376 		printf("subnet mask ");
1377 		print_sin_addr(&ifctx->netmask);
1378 		printf(" ");
1379 	}
1380 
1381 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1382 		       TAG_ROUTERS);
1383 	if (p != NULL) {
1384 		/* Routers */
1385 		if (gctx->tag.taglen % 4)
1386 			panic("bootpc: Router Len is %d", gctx->tag.taglen);
1387 		if (gctx->tag.taglen > 0) {
1388 			bcopy(p, &ifctx->gw.sin_addr, 4);
1389 			printf("router ");
1390 			print_sin_addr(&ifctx->gw);
1391 			printf(" ");
1392 			ifctx->gotgw = 1;
1393 			gctx->gotgw = 1;
1394 		}
1395 	}
1396 
1397 	/*
1398 	 * Choose a root filesystem.  If a value is forced in the environment
1399 	 * and it contains "nfs:", use it unconditionally.  Otherwise, if the
1400 	 * kernel is compiled with the ROOTDEVNAME option, then use it if:
1401 	 *  - The server doesn't provide a pathname.
1402 	 *  - The boothowto flags include RB_DFLTROOT (user said to override
1403 	 *    the server value).
1404 	 */
1405 	p = NULL;
1406 	if ((s = kern_getenv("vfs.root.mountfrom")) != NULL) {
1407 		if ((p = strstr(s, "nfs:")) != NULL)
1408 			p = strdup(p + 4, M_TEMP);
1409 		freeenv(s);
1410 	}
1411 	if (p == NULL) {
1412 		p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1413 		       TAG_ROOT);
1414 		if (p != NULL)
1415 			ifctx->gotrootpath = 1;
1416 	}
1417 #ifdef ROOTDEVNAME
1418 	if ((p == NULL || (boothowto & RB_DFLTROOT) != 0) &&
1419 	    (p = strstr(ROOTDEVNAME, "nfs:")) != NULL) {
1420 		p += 4;
1421 	}
1422 #endif
1423 	if (p != NULL) {
1424 		if (gctx->setrootfs != NULL) {
1425 			printf("rootfs %s (ignored) ", p);
1426 		} else 	if (setfs(&nd->root_saddr,
1427 				  nd->root_hostnam, p, &ifctx->reply.siaddr)) {
1428 			if (*p == '/') {
1429 				printf("root_server ");
1430 				print_sin_addr(&nd->root_saddr);
1431 				printf(" ");
1432 			}
1433 			printf("rootfs %s ", p);
1434 			gctx->gotrootpath = 1;
1435 			gctx->setrootfs = ifctx;
1436 
1437 			p = bootpc_tag(&gctx->tag, &ifctx->reply,
1438 				       ifctx->replylen,
1439 				       TAG_ROOTOPTS);
1440 			if (p != NULL) {
1441 				mountopts(&nd->root_args, p);
1442 				printf("rootopts %s ", p);
1443 			}
1444 		} else
1445 			panic("Failed to set rootfs to %s", p);
1446 	}
1447 
1448 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1449 		       TAG_HOSTNAME);
1450 	if (p != NULL) {
1451 		if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1452 			panic("bootpc: hostname >= %d bytes",
1453 			      MAXHOSTNAMELEN);
1454 		if (gctx->sethostname != NULL) {
1455 			printf("hostname %s (ignored) ", p);
1456 		} else {
1457 			strcpy(nd->my_hostnam, p);
1458 			mtx_lock(&prison0.pr_mtx);
1459 			strcpy(prison0.pr_hostname, p);
1460 			mtx_unlock(&prison0.pr_mtx);
1461 			printf("hostname %s ", p);
1462 			gctx->sethostname = ifctx;
1463 		}
1464 	}
1465 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1466 			TAG_COOKIE);
1467 	if (p != NULL) {        /* store in a sysctl variable */
1468 		int i, l = sizeof(bootp_cookie) - 1;
1469 		for (i = 0; i < l && p[i] != '\0'; i++)
1470 			bootp_cookie[i] = p[i];
1471 		p[i] = '\0';
1472 	}
1473 
1474 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1475 		       TAG_INTF_MTU);
1476 	if (p != NULL) {
1477 		ifctx->mtu = be16dec(p);
1478 	}
1479 
1480 	printf("\n");
1481 
1482 	if (ifctx->gotnetmask == 0) {
1483 		/*
1484 		 * If there is no netmask, use historical default,
1485 		 * but we really need the right mask from the server.
1486 		 */
1487 		printf("%s: no netmask received!\n", ifctx->ireq.ifr_name);
1488 		if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1489 			ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1490 		else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1491 			ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1492 		else
1493 			ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1494 	}
1495 }
1496 
1497 static u_int
1498 bootpc_init_ifa_cb(void *arg, struct ifaddr *ifa, u_int count)
1499 {
1500 	struct sockaddr_dl *sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1501 
1502 	if (count != 0)
1503 		return (0);
1504 
1505 	if (sdl->sdl_type != IFT_ETHER)
1506 		return (0);
1507 
1508 	*(struct sockaddr_dl **)arg = sdl;
1509 
1510 	return (1);
1511 }
1512 
1513 void
1514 bootpc_init(void)
1515 {
1516 	struct epoch_tracker et;
1517 	struct bootpc_ifcontext *ifctx = NULL;	/* Interface BOOTP contexts */
1518 	struct bootpc_globalcontext *gctx; 	/* Global BOOTP context */
1519 	struct sockaddr_dl *sdl;
1520 	struct if_iter iter;
1521 	if_t ifp;
1522 	int error;
1523 #ifndef BOOTP_WIRED_TO
1524 	int ifcnt;
1525 #endif
1526 	struct nfsv3_diskless *nd;
1527 	struct thread *td;
1528 	int timeout;
1529 	int delay;
1530 	char *s;
1531 
1532 	timeout = BOOTP_IFACE_WAIT_TIMEOUT * hz;
1533 	delay = hz / 10;
1534 
1535 	nd = &nfsv3_diskless;
1536 	td = curthread;
1537 
1538 	/*
1539 	 * If already filled in, don't touch it here
1540 	 */
1541 	if (nfs_diskless_valid != 0)
1542 		return;
1543 
1544 	/*
1545 	 * If "vfs.root.mountfrom" is set and the value is something other
1546 	 * than "nfs:", it means the user doesn't want to mount root via nfs,
1547 	 * there's no reason to continue with bootpc
1548 	 */
1549 	if ((s = kern_getenv("vfs.root.mountfrom")) != NULL) {
1550 		if ((strncmp(s, "nfs:", 4)) != 0) {
1551 			printf("%s: vfs.root.mountfrom set to %s. "
1552 			       "BOOTP aborted.\n", __func__, s);
1553 			freeenv(s);
1554 			return;
1555 		}
1556 		freeenv(s);
1557 	}
1558 
1559 	gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK | M_ZERO);
1560 	STAILQ_INIT(&gctx->interfaces);
1561 	gctx->xid = ~0xFFFF;
1562 	gctx->starttime = time_second;
1563 
1564 	/*
1565 	 * If ROOTDEVNAME is defined or vfs.root.mountfrom is set then we have
1566 	 * root-path overrides that can potentially let us boot even if we don't
1567 	 * get a root path from the server, so we can treat that as a non-error.
1568 	 */
1569 #ifdef ROOTDEVNAME
1570 	gctx->any_root_overrides = 1;
1571 #else
1572 	gctx->any_root_overrides = testenv("vfs.root.mountfrom");
1573 #endif
1574 
1575 	/*
1576 	 * Find a network interface.
1577 	 */
1578 	CURVNET_SET(TD_TO_VNET(td));
1579 #ifdef BOOTP_WIRED_TO
1580 	printf("%s: wired to interface '%s'\n", __func__,
1581 	       __XSTRING(BOOTP_WIRED_TO));
1582 	allocifctx(gctx);
1583 #else
1584 	/*
1585 	 * Preallocate interface context storage, if another interface
1586 	 * attaches and wins the race, it won't be eligible for bootp.
1587 	 */
1588 	ifcnt = 0;
1589 	NET_EPOCH_ENTER(et);
1590 	for (if_t ifp = if_iter_start(&iter); ifp != NULL; ifp = if_iter_next(&iter)) {
1591 		if ((if_getflags(ifp) &
1592 		    (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) ==
1593 		    IFF_BROADCAST)
1594 			ifcnt++;
1595 	}
1596 	if_iter_finish(&iter);
1597 	NET_EPOCH_EXIT(et);
1598 	if (ifcnt == 0) {
1599 		printf("WARNING: BOOTP found no eligible network interfaces, skipping!\n");
1600 		goto out;
1601 	}
1602 
1603 	for (; ifcnt > 0; ifcnt--)
1604 		allocifctx(gctx);
1605 #endif
1606 
1607 retry:
1608 	ifctx = STAILQ_FIRST(&gctx->interfaces);
1609 	NET_EPOCH_ENTER(et);
1610 	for (ifp = if_iter_start(&iter); ifp != NULL; ifp = if_iter_next(&iter)) {
1611 		if (ifctx == NULL)
1612 			break;
1613 #ifdef BOOTP_WIRED_TO
1614 		if (strcmp(if_name(ifp), __XSTRING(BOOTP_WIRED_TO)) != 0)
1615 			continue;
1616 #else
1617 		if ((if_getflags(ifp) &
1618 		     (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1619 		    IFF_BROADCAST)
1620 			break;
1621 		switch (if_getalloctype(ifp)) {
1622 			case IFT_ETHER:
1623 				break;
1624 			default:
1625 				continue;
1626 		}
1627 #endif
1628 		strlcpy(ifctx->ireq.ifr_name, if_name(ifp),
1629 		    sizeof(ifctx->ireq.ifr_name));
1630 		ifctx->ifp = ifp;
1631 
1632 		/* Get HW address */
1633 		sdl = NULL;
1634 		if_foreach_addr_type(ifp, AF_LINK, bootpc_init_ifa_cb, &sdl);
1635 		if (sdl == NULL)
1636 			panic("bootpc: Unable to find HW address for %s",
1637 			    ifctx->ireq.ifr_name);
1638 		ifctx->sdl = sdl;
1639 
1640 		ifctx = STAILQ_NEXT(ifctx, next);
1641 	}
1642 	if_iter_finish(&iter);
1643 	NET_EPOCH_EXIT(et);
1644 	CURVNET_RESTORE();
1645 
1646 	if (STAILQ_EMPTY(&gctx->interfaces) ||
1647 	    STAILQ_FIRST(&gctx->interfaces)->ifp == NULL) {
1648 		if (timeout > 0) {
1649 			pause("bootpc", delay);
1650 			timeout -= delay;
1651 			goto retry;
1652 		}
1653 #ifdef BOOTP_WIRED_TO
1654 		panic("%s: Could not find interface specified "
1655 		      "by BOOTP_WIRED_TO: "
1656 		      __XSTRING(BOOTP_WIRED_TO), __func__);
1657 #else
1658 		panic("%s: no suitable interface", __func__);
1659 #endif
1660 	}
1661 
1662 	error = socreate(AF_INET, &bootp_so, SOCK_DGRAM, 0, td->td_ucred, td);
1663 	if (error != 0)
1664 		panic("%s: socreate, error=%d", __func__, error);
1665 
1666 	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1667 		bootpc_fakeup_interface(ifctx, td);
1668 
1669 	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1670 		bootpc_compose_query(ifctx, td);
1671 
1672 	error = bootpc_call(gctx, td);
1673 	if (error != 0) {
1674 		printf("BOOTP call failed\n");
1675 	}
1676 
1677 	mountopts(&nd->root_args, NULL);
1678 
1679 	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1680 		if (bootpc_ifctx_isresolved(ifctx) != 0)
1681 			bootpc_decode_reply(nd, ifctx, gctx);
1682 
1683 #ifdef BOOTP_NFSROOT
1684 	if (gctx->gotrootpath == 0 && gctx->any_root_overrides == 0)
1685 		panic("bootpc: No root path offered");
1686 #endif
1687 
1688 	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1689 		bootpc_adjust_interface(ifctx, gctx, td);
1690 
1691 	soclose(bootp_so);
1692 
1693 	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1694 		if (ifctx->gotrootpath != 0)
1695 			break;
1696 	if (ifctx == NULL) {
1697 		STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1698 			if (bootpc_ifctx_isresolved(ifctx) != 0)
1699 				break;
1700 	}
1701 	if (ifctx == NULL)
1702 		goto out;
1703 
1704 	if (gctx->gotrootpath != 0) {
1705 		struct epoch_tracker et;
1706 
1707 		kern_setenv("boot.netif.name", if_name(ifctx->ifp));
1708 
1709 		NET_EPOCH_ENTER(et);
1710 		bootpc_add_default_route(ifctx);
1711 		NET_EPOCH_EXIT(et);
1712 		error = md_mount(&nd->root_saddr, nd->root_hostnam,
1713 				 nd->root_fh, &nd->root_fhsize,
1714 				 &nd->root_args, td);
1715 		NET_EPOCH_ENTER(et);
1716 		bootpc_remove_default_route(ifctx);
1717 		NET_EPOCH_EXIT(et);
1718 		if (error != 0) {
1719 			if (gctx->any_root_overrides == 0)
1720 				panic("nfs_boot: mount root, error=%d", error);
1721 			else
1722 				goto out;
1723 		}
1724 		rootdevnames[0] = "nfs:";
1725 		nfs_diskless_valid = 3;
1726 	}
1727 
1728 	strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1729 	bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1730 	bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1731 	((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1732 		ifctx->myaddr.sin_addr.s_addr |
1733 		~ ifctx->netmask.sin_addr.s_addr;
1734 	bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1735 	bcopy(&ifctx->gw, &nd->mygateway, sizeof(ifctx->gw));
1736 
1737 out:
1738 	while((ifctx = STAILQ_FIRST(&gctx->interfaces)) != NULL) {
1739 		STAILQ_REMOVE_HEAD(&gctx->interfaces, next);
1740 		free(ifctx, M_TEMP);
1741 	}
1742 	free(gctx, M_TEMP);
1743 }
1744 
1745 /*
1746  * RPC: mountd/mount
1747  * Given a server pathname, get an NFS file handle.
1748  * Also, sets sin->sin_port to the NFS service port.
1749  */
1750 static int
1751 md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp, int *fhsizep,
1752     struct nfs_args *args, struct thread *td)
1753 {
1754 	struct mbuf *m;
1755 	int error;
1756 	int authunixok;
1757 	int authcount;
1758 	int authver;
1759 
1760 #define	RPCPROG_MNT	100005
1761 #define	RPCMNT_VER1	1
1762 #define RPCMNT_VER3	3
1763 #define	RPCMNT_MOUNT	1
1764 #define	AUTH_SYS	1		/* unix style (uid, gids) */
1765 #define AUTH_UNIX	AUTH_SYS
1766 
1767 	/* XXX honor v2/v3 flags in args->flags? */
1768 #ifdef BOOTP_NFSV3
1769 	/* First try NFS v3 */
1770 	/* Get port number for MOUNTD. */
1771 	error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1772 			     &mdsin->sin_port, td);
1773 	if (error == 0) {
1774 		m = xdr_string_encode(path, strlen(path));
1775 
1776 		/* Do RPC to mountd. */
1777 		error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1778 				  RPCMNT_MOUNT, &m, NULL, td);
1779 	}
1780 	if (error == 0) {
1781 		args->flags |= NFSMNT_NFSV3;
1782 	} else {
1783 #endif
1784 		/* Fallback to NFS v2 */
1785 
1786 		/* Get port number for MOUNTD. */
1787 		error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1788 				     &mdsin->sin_port, td);
1789 		if (error != 0)
1790 			return error;
1791 
1792 		m = xdr_string_encode(path, strlen(path));
1793 
1794 		/* Do RPC to mountd. */
1795 		error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1796 				  RPCMNT_MOUNT, &m, NULL, td);
1797 		if (error != 0)
1798 			return error;	/* message already freed */
1799 
1800 #ifdef BOOTP_NFSV3
1801 	}
1802 #endif
1803 
1804 	if (xdr_int_decode(&m, &error) != 0 || error != 0)
1805 		goto bad;
1806 
1807 	if ((args->flags & NFSMNT_NFSV3) != 0) {
1808 		if (xdr_int_decode(&m, fhsizep) != 0 ||
1809 		    *fhsizep > NFSX_V3FHMAX ||
1810 		    *fhsizep <= 0)
1811 			goto bad;
1812 	} else
1813 		*fhsizep = NFSX_V2FH;
1814 
1815 	if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
1816 		goto bad;
1817 
1818 	if (args->flags & NFSMNT_NFSV3) {
1819 		if (xdr_int_decode(&m, &authcount) != 0)
1820 			goto bad;
1821 		authunixok = 0;
1822 		if (authcount < 0 || authcount > 100)
1823 			goto bad;
1824 		while (authcount > 0) {
1825 			if (xdr_int_decode(&m, &authver) != 0)
1826 				goto bad;
1827 			if (authver == AUTH_UNIX)
1828 				authunixok = 1;
1829 			authcount--;
1830 		}
1831 		if (authunixok == 0)
1832 			goto bad;
1833 	}
1834 
1835 	/* Set port number for NFS use. */
1836 	error = krpc_portmap(mdsin, NFS_PROG,
1837 			     (args->flags &
1838 			      NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
1839 			     &mdsin->sin_port, td);
1840 
1841 	goto out;
1842 
1843 bad:
1844 	error = EBADRPC;
1845 
1846 out:
1847 	m_freem(m);
1848 	return error;
1849 }
1850 
1851 SYSINIT(bootp_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, bootpc_init, NULL);
1852