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