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