xref: /freebsd/sys/nfs/bootp_subr.c (revision ccf5e68e5bb2cdabb4686d81c2d63c2f6304d0a4)
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 void	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 void
1015 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
1016     struct bootpc_globalcontext *gctx, struct thread *td)
1017 {
1018 	int error;
1019 	struct sockaddr_in *sin;
1020 	struct ifreq *ifr;
1021 	struct in_aliasreq *ifra;
1022 	struct sockaddr_in *myaddr;
1023 	struct sockaddr_in *netmask;
1024 
1025 	ifr = &ifctx->ireq;
1026 	ifra = &ifctx->iareq;
1027 	myaddr = &ifctx->myaddr;
1028 	netmask = &ifctx->netmask;
1029 
1030 	if (bootpc_ifctx_isresolved(ifctx) == 0) {
1031 		/* Shutdown interfaces where BOOTP failed */
1032 		bootpc_shutdown_interface(ifctx, td);
1033 		return;
1034 	}
1035 
1036 	printf("Adjusted interface %s", ifctx->ireq.ifr_name);
1037 
1038 	/* Do BOOTP interface options */
1039 	if (ifctx->mtu != 0) {
1040 		printf(" (MTU=%d%s)", ifctx->mtu,
1041 		    (ifctx->mtu > 1514) ? "/JUMBO" : "");
1042 		ifr->ifr_mtu = ifctx->mtu;
1043 		error = ifioctl(bootp_so, SIOCSIFMTU, (caddr_t) ifr, td);
1044 		if (error != 0)
1045 			panic("%s: SIOCSIFMTU, error=%d", __func__, error);
1046 	}
1047 	printf("\n");
1048 
1049 	/*
1050 	 * Do enough of ifconfig(8) so that the chosen interface
1051 	 * can talk to the servers.  (just set the address)
1052 	 */
1053 	sin = (struct sockaddr_in *) &ifr->ifr_addr;
1054 	clear_sinaddr(sin);
1055 	error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
1056 	if (error != 0)
1057 		panic("%s: SIOCDIFADDR, error=%d", __func__, error);
1058 
1059 	bcopy(myaddr, &ifra->ifra_addr, sizeof(*myaddr));
1060 	bcopy(netmask, &ifra->ifra_mask, sizeof(*netmask));
1061 	clear_sinaddr(&ifra->ifra_broadaddr);
1062 	ifra->ifra_broadaddr.sin_addr.s_addr = myaddr->sin_addr.s_addr |
1063 	    ~netmask->sin_addr.s_addr;
1064 
1065 	error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
1066 	if (error != 0)
1067 		panic("%s: SIOCAIFADDR, error=%d", __func__, error);
1068 }
1069 
1070 static void
1071 bootpc_add_default_route(struct bootpc_ifcontext *ifctx)
1072 {
1073 	int error;
1074 	struct sockaddr_in defdst;
1075 	struct sockaddr_in defmask;
1076 
1077 	if (ifctx->gw.sin_addr.s_addr == htonl(INADDR_ANY))
1078 		return;
1079 
1080 	clear_sinaddr(&defdst);
1081 	clear_sinaddr(&defmask);
1082 
1083 	error = rtrequest_fib(RTM_ADD, (struct sockaddr *)&defdst,
1084 	    (struct sockaddr *) &ifctx->gw, (struct sockaddr *)&defmask,
1085 	    (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL, RT_DEFAULT_FIB);
1086 	if (error != 0) {
1087 		printf("%s: RTM_ADD, error=%d\n", __func__, error);
1088 	}
1089 }
1090 
1091 static void
1092 bootpc_remove_default_route(struct bootpc_ifcontext *ifctx)
1093 {
1094 	int error;
1095 	struct sockaddr_in defdst;
1096 	struct sockaddr_in defmask;
1097 
1098 	if (ifctx->gw.sin_addr.s_addr == htonl(INADDR_ANY))
1099 		return;
1100 
1101 	clear_sinaddr(&defdst);
1102 	clear_sinaddr(&defmask);
1103 
1104 	error = rtrequest_fib(RTM_DELETE, (struct sockaddr *)&defdst,
1105 	    (struct sockaddr *) &ifctx->gw, (struct sockaddr *)&defmask,
1106 	    (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL, RT_DEFAULT_FIB);
1107 	if (error != 0) {
1108 		printf("%s: RTM_DELETE, error=%d\n", __func__, error);
1109 	}
1110 }
1111 
1112 static int
1113 setfs(struct sockaddr_in *addr, char *path, char *p,
1114     const struct in_addr *siaddr)
1115 {
1116 
1117 	if (getip(&p, &addr->sin_addr) == 0) {
1118 		if (siaddr != NULL && *p == '/')
1119 			bcopy(siaddr, &addr->sin_addr, sizeof(struct in_addr));
1120 		else
1121 			return 0;
1122 	} else {
1123 		if (*p != ':')
1124 			return 0;
1125 		p++;
1126 	}
1127 
1128 	addr->sin_len = sizeof(struct sockaddr_in);
1129 	addr->sin_family = AF_INET;
1130 
1131 	strlcpy(path, p, MNAMELEN);
1132 	return 1;
1133 }
1134 
1135 static int
1136 getip(char **ptr, struct in_addr *addr)
1137 {
1138 	char *p;
1139 	unsigned int ip;
1140 	int val;
1141 
1142 	p = *ptr;
1143 	ip = 0;
1144 	if (((val = getdec(&p)) < 0) || (val > 255))
1145 		return 0;
1146 	ip = val << 24;
1147 	if (*p != '.')
1148 		return 0;
1149 	p++;
1150 	if (((val = getdec(&p)) < 0) || (val > 255))
1151 		return 0;
1152 	ip |= (val << 16);
1153 	if (*p != '.')
1154 		return 0;
1155 	p++;
1156 	if (((val = getdec(&p)) < 0) || (val > 255))
1157 		return 0;
1158 	ip |= (val << 8);
1159 	if (*p != '.')
1160 		return 0;
1161 	p++;
1162 	if (((val = getdec(&p)) < 0) || (val > 255))
1163 		return 0;
1164 	ip |= val;
1165 
1166 	addr->s_addr = htonl(ip);
1167 	*ptr = p;
1168 	return 1;
1169 }
1170 
1171 static int
1172 getdec(char **ptr)
1173 {
1174 	char *p;
1175 	int ret;
1176 
1177 	p = *ptr;
1178 	ret = 0;
1179 	if ((*p < '0') || (*p > '9'))
1180 		return -1;
1181 	while ((*p >= '0') && (*p <= '9')) {
1182 		ret = ret * 10 + (*p - '0');
1183 		p++;
1184 	}
1185 	*ptr = p;
1186 	return ret;
1187 }
1188 
1189 static void
1190 mountopts(struct nfs_args *args, char *p)
1191 {
1192 	args->version = NFS_ARGSVERSION;
1193 	args->rsize = BOOTP_BLOCKSIZE;
1194 	args->wsize = BOOTP_BLOCKSIZE;
1195 	args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
1196 	args->sotype = SOCK_DGRAM;
1197 	if (p != NULL)
1198 		nfs_parse_options(p, args);
1199 }
1200 
1201 static int
1202 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
1203 {
1204 	struct mbuf *m;
1205 	int alignedlen;
1206 
1207 	m = *mptr;
1208 	alignedlen = ( len + 3 ) & ~3;
1209 
1210 	if (m->m_len < alignedlen) {
1211 		m = m_pullup(m, alignedlen);
1212 		if (m == NULL) {
1213 			*mptr = NULL;
1214 			return EBADRPC;
1215 		}
1216 	}
1217 	bcopy(mtod(m, u_char *), buf, len);
1218 	m_adj(m, alignedlen);
1219 	*mptr = m;
1220 	return 0;
1221 }
1222 
1223 static int
1224 xdr_int_decode(struct mbuf **mptr, int *iptr)
1225 {
1226 	u_int32_t i;
1227 
1228 	if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
1229 		return EBADRPC;
1230 	*iptr = fxdr_unsigned(u_int32_t, i);
1231 	return 0;
1232 }
1233 
1234 static void
1235 print_sin_addr(struct sockaddr_in *sin)
1236 {
1237 
1238 	print_in_addr(sin->sin_addr);
1239 }
1240 
1241 static void
1242 print_in_addr(struct in_addr addr)
1243 {
1244 	unsigned int ip;
1245 
1246 	ip = ntohl(addr.s_addr);
1247 	printf("%d.%d.%d.%d",
1248 	       ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1249 }
1250 
1251 static void
1252 bootpc_compose_query(struct bootpc_ifcontext *ifctx, struct thread *td)
1253 {
1254 	unsigned char *vendp;
1255 	unsigned char vendor_client[64];
1256 	uint32_t leasetime;
1257 	uint8_t vendor_client_len;
1258 
1259 	ifctx->gotrootpath = 0;
1260 
1261 	bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1262 
1263 	/* bootpc part */
1264 	ifctx->call.op = BOOTP_REQUEST; 	/* BOOTREQUEST */
1265 	ifctx->call.htype = 1;			/* 10mb ethernet */
1266 	ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1267 	ifctx->call.hops = 0;
1268 	if (bootpc_ifctx_isunresolved(ifctx) != 0)
1269 		ifctx->xid++;
1270 	ifctx->call.xid = txdr_unsigned(ifctx->xid);
1271 	bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1272 
1273 	vendp = ifctx->call.vend;
1274 	*vendp++ = 99;		/* RFC1048 cookie */
1275 	*vendp++ = 130;
1276 	*vendp++ = 83;
1277 	*vendp++ = 99;
1278 	*vendp++ = TAG_MAXMSGSIZE;
1279 	*vendp++ = 2;
1280 	*vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1281 	*vendp++ = sizeof(struct bootp_packet) & 255;
1282 
1283 	snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1284 		ostype, MACHINE, osrelease);
1285 	vendor_client_len = strlen(vendor_client);
1286 	*vendp++ = TAG_VENDOR_INDENTIFIER;
1287 	*vendp++ = vendor_client_len;
1288 	memcpy(vendp, vendor_client, vendor_client_len);
1289 	vendp += vendor_client_len;
1290 	ifctx->dhcpquerytype = DHCP_NOMSG;
1291 	switch (ifctx->state) {
1292 	case IF_DHCP_UNRESOLVED:
1293 		*vendp++ = TAG_DHCP_MSGTYPE;
1294 		*vendp++ = 1;
1295 		*vendp++ = DHCP_DISCOVER;
1296 		ifctx->dhcpquerytype = DHCP_DISCOVER;
1297 		ifctx->gotdhcpserver = 0;
1298 		break;
1299 	case IF_DHCP_OFFERED:
1300 		*vendp++ = TAG_DHCP_MSGTYPE;
1301 		*vendp++ = 1;
1302 		*vendp++ = DHCP_REQUEST;
1303 		ifctx->dhcpquerytype = DHCP_REQUEST;
1304 		*vendp++ = TAG_DHCP_REQ_ADDR;
1305 		*vendp++ = 4;
1306 		memcpy(vendp, &ifctx->reply.yiaddr, 4);
1307 		vendp += 4;
1308 		if (ifctx->gotdhcpserver != 0) {
1309 			*vendp++ = TAG_DHCP_SERVERID;
1310 			*vendp++ = 4;
1311 			memcpy(vendp, &ifctx->dhcpserver, 4);
1312 			vendp += 4;
1313 		}
1314 		*vendp++ = TAG_DHCP_LEASETIME;
1315 		*vendp++ = 4;
1316 		leasetime = htonl(300);
1317 		memcpy(vendp, &leasetime, 4);
1318 		vendp += 4;
1319 		break;
1320 	default:
1321 		break;
1322 	}
1323 	*vendp = TAG_END;
1324 
1325 	ifctx->call.secs = 0;
1326 	ifctx->call.flags = htons(0x8000); /* We need a broadcast answer */
1327 }
1328 
1329 static int
1330 bootpc_hascookie(struct bootp_packet *bp)
1331 {
1332 
1333 	return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1334 		bp->vend[2] == 83 && bp->vend[3] == 99);
1335 }
1336 
1337 static void
1338 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1339     unsigned char *start, int len, int tag)
1340 {
1341 	unsigned char *j;
1342 	unsigned char *ej;
1343 	unsigned char code;
1344 
1345 	if (tctx->badtag != 0 || tctx->badopt != 0)
1346 		return;
1347 
1348 	j = start;
1349 	ej = j + len;
1350 
1351 	while (j < ej) {
1352 		code = *j++;
1353 		if (code == TAG_PAD)
1354 			continue;
1355 		if (code == TAG_END)
1356 			return;
1357 		if (j >= ej || j + *j + 1 > ej) {
1358 			tctx->badopt = 1;
1359 			return;
1360 		}
1361 		len = *j++;
1362 		if (code == tag) {
1363 			if (tctx->taglen + len > TAG_MAXLEN) {
1364 				tctx->badtag = 1;
1365 				return;
1366 			}
1367 			tctx->foundopt = 1;
1368 			if (len > 0)
1369 				memcpy(tctx->buf + tctx->taglen,
1370 				       j, len);
1371 			tctx->taglen += len;
1372 		}
1373 		if (code == TAG_OVERLOAD)
1374 			tctx->overload = *j;
1375 
1376 		j += len;
1377 	}
1378 }
1379 
1380 static unsigned char *
1381 bootpc_tag(struct bootpc_tagcontext *tctx,
1382     struct bootp_packet *bp, int len, int tag)
1383 {
1384 	tctx->overload = 0;
1385 	tctx->badopt = 0;
1386 	tctx->badtag = 0;
1387 	tctx->foundopt = 0;
1388 	tctx->taglen = 0;
1389 
1390 	if (bootpc_hascookie(bp) == 0)
1391 		return NULL;
1392 
1393 	bootpc_tag_helper(tctx, &bp->vend[4],
1394 			  (unsigned char *) bp + len - &bp->vend[4], tag);
1395 
1396 	if ((tctx->overload & OVERLOAD_FILE) != 0)
1397 		bootpc_tag_helper(tctx,
1398 				  (unsigned char *) bp->file,
1399 				  sizeof(bp->file),
1400 				  tag);
1401 	if ((tctx->overload & OVERLOAD_SNAME) != 0)
1402 		bootpc_tag_helper(tctx,
1403 				  (unsigned char *) bp->sname,
1404 				  sizeof(bp->sname),
1405 				  tag);
1406 
1407 	if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1408 		return NULL;
1409 	tctx->buf[tctx->taglen] = '\0';
1410 	return tctx->buf;
1411 }
1412 
1413 static void
1414 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1415     struct bootpc_globalcontext *gctx)
1416 {
1417 	char *p, *s;
1418 	unsigned int ip;
1419 
1420 	ifctx->gotgw = 0;
1421 	ifctx->gotnetmask = 0;
1422 
1423 	clear_sinaddr(&ifctx->myaddr);
1424 	clear_sinaddr(&ifctx->netmask);
1425 	clear_sinaddr(&ifctx->gw);
1426 
1427 	ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1428 
1429 	ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1430 
1431 	printf("%s at ", ifctx->ireq.ifr_name);
1432 	print_sin_addr(&ifctx->myaddr);
1433 	printf(" server ");
1434 	print_in_addr(ifctx->reply.siaddr);
1435 
1436 	ifctx->gw.sin_addr = ifctx->reply.giaddr;
1437 	if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1438 		printf(" via gateway ");
1439 		print_in_addr(ifctx->reply.giaddr);
1440 	}
1441 
1442 	/* This call used for the side effect (overload flag) */
1443 	(void) bootpc_tag(&gctx->tmptag,
1444 			  &ifctx->reply, ifctx->replylen, TAG_END);
1445 
1446 	if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1447 		if (ifctx->reply.sname[0] != '\0')
1448 			printf(" server name %s", ifctx->reply.sname);
1449 	if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1450 		if (ifctx->reply.file[0] != '\0')
1451 			printf(" boot file %s", ifctx->reply.file);
1452 
1453 	printf("\n");
1454 
1455 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1456 		       TAG_SUBNETMASK);
1457 	if (p != NULL) {
1458 		if (gctx->tag.taglen != 4)
1459 			panic("bootpc: subnet mask len is %d",
1460 			      gctx->tag.taglen);
1461 		bcopy(p, &ifctx->netmask.sin_addr, 4);
1462 		ifctx->gotnetmask = 1;
1463 		printf("subnet mask ");
1464 		print_sin_addr(&ifctx->netmask);
1465 		printf(" ");
1466 	}
1467 
1468 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1469 		       TAG_ROUTERS);
1470 	if (p != NULL) {
1471 		/* Routers */
1472 		if (gctx->tag.taglen % 4)
1473 			panic("bootpc: Router Len is %d", gctx->tag.taglen);
1474 		if (gctx->tag.taglen > 0) {
1475 			bcopy(p, &ifctx->gw.sin_addr, 4);
1476 			printf("router ");
1477 			print_sin_addr(&ifctx->gw);
1478 			printf(" ");
1479 			ifctx->gotgw = 1;
1480 			gctx->gotgw = 1;
1481 		}
1482 	}
1483 
1484 	/*
1485 	 * Choose a root filesystem.  If a value is forced in the environment
1486 	 * and it contains "nfs:", use it unconditionally.  Otherwise, if the
1487 	 * kernel is compiled with the ROOTDEVNAME option, then use it if:
1488 	 *  - The server doesn't provide a pathname.
1489 	 *  - The boothowto flags include RB_DFLTROOT (user said to override
1490 	 *    the server value).
1491 	 */
1492 	p = NULL;
1493 	if ((s = kern_getenv("vfs.root.mountfrom")) != NULL) {
1494 		if ((p = strstr(s, "nfs:")) != NULL)
1495 			p = strdup(p + 4, M_TEMP);
1496 		freeenv(s);
1497 	}
1498 	if (p == NULL) {
1499 		p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1500 		       TAG_ROOT);
1501 		if (p != NULL)
1502 			ifctx->gotrootpath = 1;
1503 	}
1504 #ifdef ROOTDEVNAME
1505 	if ((p == NULL || (boothowto & RB_DFLTROOT) != 0) &&
1506 	    (p = strstr(ROOTDEVNAME, "nfs:")) != NULL) {
1507 		p += 4;
1508 	}
1509 #endif
1510 	if (p != NULL) {
1511 		if (gctx->setrootfs != NULL) {
1512 			printf("rootfs %s (ignored) ", p);
1513 		} else 	if (setfs(&nd->root_saddr,
1514 				  nd->root_hostnam, p, &ifctx->reply.siaddr)) {
1515 			if (*p == '/') {
1516 				printf("root_server ");
1517 				print_sin_addr(&nd->root_saddr);
1518 				printf(" ");
1519 			}
1520 			printf("rootfs %s ", p);
1521 			gctx->gotrootpath = 1;
1522 			gctx->setrootfs = ifctx;
1523 
1524 			p = bootpc_tag(&gctx->tag, &ifctx->reply,
1525 				       ifctx->replylen,
1526 				       TAG_ROOTOPTS);
1527 			if (p != NULL) {
1528 				mountopts(&nd->root_args, p);
1529 				printf("rootopts %s ", p);
1530 			}
1531 		} else
1532 			panic("Failed to set rootfs to %s", p);
1533 	}
1534 
1535 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1536 		       TAG_HOSTNAME);
1537 	if (p != NULL) {
1538 		if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1539 			panic("bootpc: hostname >= %d bytes",
1540 			      MAXHOSTNAMELEN);
1541 		if (gctx->sethostname != NULL) {
1542 			printf("hostname %s (ignored) ", p);
1543 		} else {
1544 			strcpy(nd->my_hostnam, p);
1545 			mtx_lock(&prison0.pr_mtx);
1546 			strcpy(prison0.pr_hostname, p);
1547 			mtx_unlock(&prison0.pr_mtx);
1548 			printf("hostname %s ", p);
1549 			gctx->sethostname = ifctx;
1550 		}
1551 	}
1552 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1553 			TAG_COOKIE);
1554 	if (p != NULL) {        /* store in a sysctl variable */
1555 		int i, l = sizeof(bootp_cookie) - 1;
1556 		for (i = 0; i < l && p[i] != '\0'; i++)
1557 			bootp_cookie[i] = p[i];
1558 		p[i] = '\0';
1559 	}
1560 
1561 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1562 		       TAG_INTF_MTU);
1563 	if (p != NULL) {
1564 		ifctx->mtu = be16dec(p);
1565 	}
1566 
1567 	printf("\n");
1568 
1569 	if (ifctx->gotnetmask == 0) {
1570 		if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1571 			ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1572 		else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1573 			ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1574 		else
1575 			ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1576 	}
1577 }
1578 
1579 void
1580 bootpc_init(void)
1581 {
1582 	struct bootpc_ifcontext *ifctx;		/* Interface BOOTP contexts */
1583 	struct bootpc_globalcontext *gctx; 	/* Global BOOTP context */
1584 	struct ifnet *ifp;
1585 	struct sockaddr_dl *sdl;
1586 	struct ifaddr *ifa;
1587 	int error;
1588 #ifndef BOOTP_WIRED_TO
1589 	int ifcnt;
1590 #endif
1591 	struct nfsv3_diskless *nd;
1592 	struct thread *td;
1593 	int timeout;
1594 	int delay;
1595 
1596 	timeout = BOOTP_IFACE_WAIT_TIMEOUT * hz;
1597 	delay = hz / 10;
1598 
1599 	nd = &nfsv3_diskless;
1600 	td = curthread;
1601 
1602 	/*
1603 	 * If already filled in, don't touch it here
1604 	 */
1605 	if (nfs_diskless_valid != 0)
1606 		return;
1607 
1608 	gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK | M_ZERO);
1609 	STAILQ_INIT(&gctx->interfaces);
1610 	gctx->xid = ~0xFFFF;
1611 	gctx->starttime = time_second;
1612 
1613 	/*
1614 	 * If ROOTDEVNAME is defined or vfs.root.mountfrom is set then we have
1615 	 * root-path overrides that can potentially let us boot even if we don't
1616 	 * get a root path from the server, so we can treat that as a non-error.
1617 	 */
1618 #ifdef ROOTDEVNAME
1619 	gctx->any_root_overrides = 1;
1620 #else
1621 	gctx->any_root_overrides = testenv("vfs.root.mountfrom");
1622 #endif
1623 
1624 	/*
1625 	 * Find a network interface.
1626 	 */
1627 	CURVNET_SET(TD_TO_VNET(td));
1628 #ifdef BOOTP_WIRED_TO
1629 	printf("%s: wired to interface '%s'\n", __func__,
1630 	       __XSTRING(BOOTP_WIRED_TO));
1631 	allocifctx(gctx);
1632 #else
1633 	/*
1634 	 * Preallocate interface context storage, if another interface
1635 	 * attaches and wins the race, it won't be eligible for bootp.
1636 	 */
1637 	ifcnt = 0;
1638 	IFNET_RLOCK();
1639 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1640 		if ((ifp->if_flags &
1641 		     (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1642 		    IFF_BROADCAST)
1643 			continue;
1644 		switch (ifp->if_alloctype) {
1645 			case IFT_ETHER:
1646 			case IFT_FDDI:
1647 			case IFT_ISO88025:
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 	TAILQ_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 			case IFT_FDDI:
1678 			case IFT_ISO88025:
1679 				break;
1680 			default:
1681 				continue;
1682 		}
1683 #endif
1684 		strlcpy(ifctx->ireq.ifr_name, ifp->if_xname,
1685 		    sizeof(ifctx->ireq.ifr_name));
1686 		ifctx->ifp = ifp;
1687 
1688 		/* Get HW address */
1689 		sdl = NULL;
1690 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1691 			if (ifa->ifa_addr->sa_family == AF_LINK) {
1692 				sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1693 				if (sdl->sdl_type == IFT_ETHER)
1694 					break;
1695 			}
1696 		if (sdl == NULL)
1697 			panic("bootpc: Unable to find HW address for %s",
1698 			    ifctx->ireq.ifr_name);
1699 		ifctx->sdl = sdl;
1700 
1701 		ifctx = STAILQ_NEXT(ifctx, next);
1702 	}
1703 	IFNET_RUNLOCK();
1704 	CURVNET_RESTORE();
1705 
1706 	if (STAILQ_EMPTY(&gctx->interfaces) ||
1707 	    STAILQ_FIRST(&gctx->interfaces)->ifp == NULL) {
1708 		if (timeout > 0) {
1709 			pause("bootpc", delay);
1710 			timeout -= delay;
1711 			goto retry;
1712 		}
1713 #ifdef BOOTP_WIRED_TO
1714 		panic("%s: Could not find interface specified "
1715 		      "by BOOTP_WIRED_TO: "
1716 		      __XSTRING(BOOTP_WIRED_TO), __func__);
1717 #else
1718 		panic("%s: no suitable interface", __func__);
1719 #endif
1720 	}
1721 
1722 	error = socreate(AF_INET, &bootp_so, SOCK_DGRAM, 0, td->td_ucred, td);
1723 	if (error != 0)
1724 		panic("%s: socreate, error=%d", __func__, error);
1725 
1726 	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1727 		bootpc_fakeup_interface(ifctx, td);
1728 
1729 	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1730 		bootpc_compose_query(ifctx, td);
1731 
1732 	error = bootpc_call(gctx, td);
1733 	if (error != 0) {
1734 		printf("BOOTP call failed\n");
1735 	}
1736 
1737 	mountopts(&nd->root_args, NULL);
1738 
1739 	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1740 		if (bootpc_ifctx_isresolved(ifctx) != 0)
1741 			bootpc_decode_reply(nd, ifctx, gctx);
1742 
1743 #ifdef BOOTP_NFSROOT
1744 	if (gctx->gotrootpath == 0 && gctx->any_root_overrides == 0)
1745 		panic("bootpc: No root path offered");
1746 #endif
1747 
1748 	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1749 		bootpc_adjust_interface(ifctx, gctx, td);
1750 
1751 	soclose(bootp_so);
1752 
1753 	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1754 		if (ifctx->gotrootpath != 0)
1755 			break;
1756 	if (ifctx == NULL) {
1757 		STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1758 			if (bootpc_ifctx_isresolved(ifctx) != 0)
1759 				break;
1760 	}
1761 	if (ifctx == NULL)
1762 		goto out;
1763 
1764 	if (gctx->gotrootpath != 0) {
1765 
1766 		kern_setenv("boot.netif.name", ifctx->ifp->if_xname);
1767 
1768 		bootpc_add_default_route(ifctx);
1769 		error = md_mount(&nd->root_saddr, nd->root_hostnam,
1770 				 nd->root_fh, &nd->root_fhsize,
1771 				 &nd->root_args, td);
1772 		bootpc_remove_default_route(ifctx);
1773 		if (error != 0) {
1774 			if (gctx->any_root_overrides == 0)
1775 				panic("nfs_boot: mount root, error=%d", error);
1776 			else
1777 				goto out;
1778 		}
1779 		rootdevnames[0] = "nfs:";
1780 		nfs_diskless_valid = 3;
1781 	}
1782 
1783 	strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1784 	bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1785 	bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1786 	((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1787 		ifctx->myaddr.sin_addr.s_addr |
1788 		~ ifctx->netmask.sin_addr.s_addr;
1789 	bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1790 	bcopy(&ifctx->gw, &nd->mygateway, sizeof(ifctx->gw));
1791 
1792 out:
1793 	while((ifctx = STAILQ_FIRST(&gctx->interfaces)) != NULL) {
1794 		STAILQ_REMOVE_HEAD(&gctx->interfaces, next);
1795 		free(ifctx, M_TEMP);
1796 	}
1797 	free(gctx, M_TEMP);
1798 }
1799 
1800 /*
1801  * RPC: mountd/mount
1802  * Given a server pathname, get an NFS file handle.
1803  * Also, sets sin->sin_port to the NFS service port.
1804  */
1805 static int
1806 md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp, int *fhsizep,
1807     struct nfs_args *args, struct thread *td)
1808 {
1809 	struct mbuf *m;
1810 	int error;
1811 	int authunixok;
1812 	int authcount;
1813 	int authver;
1814 
1815 #define	RPCPROG_MNT	100005
1816 #define	RPCMNT_VER1	1
1817 #define RPCMNT_VER3	3
1818 #define	RPCMNT_MOUNT	1
1819 #define	AUTH_SYS	1		/* unix style (uid, gids) */
1820 #define AUTH_UNIX	AUTH_SYS
1821 
1822 	/* XXX honor v2/v3 flags in args->flags? */
1823 #ifdef BOOTP_NFSV3
1824 	/* First try NFS v3 */
1825 	/* Get port number for MOUNTD. */
1826 	error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1827 			     &mdsin->sin_port, td);
1828 	if (error == 0) {
1829 		m = xdr_string_encode(path, strlen(path));
1830 
1831 		/* Do RPC to mountd. */
1832 		error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1833 				  RPCMNT_MOUNT, &m, NULL, td);
1834 	}
1835 	if (error == 0) {
1836 		args->flags |= NFSMNT_NFSV3;
1837 	} else {
1838 #endif
1839 		/* Fallback to NFS v2 */
1840 
1841 		/* Get port number for MOUNTD. */
1842 		error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1843 				     &mdsin->sin_port, td);
1844 		if (error != 0)
1845 			return error;
1846 
1847 		m = xdr_string_encode(path, strlen(path));
1848 
1849 		/* Do RPC to mountd. */
1850 		error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1851 				  RPCMNT_MOUNT, &m, NULL, td);
1852 		if (error != 0)
1853 			return error;	/* message already freed */
1854 
1855 #ifdef BOOTP_NFSV3
1856 	}
1857 #endif
1858 
1859 	if (xdr_int_decode(&m, &error) != 0 || error != 0)
1860 		goto bad;
1861 
1862 	if ((args->flags & NFSMNT_NFSV3) != 0) {
1863 		if (xdr_int_decode(&m, fhsizep) != 0 ||
1864 		    *fhsizep > NFSX_V3FHMAX ||
1865 		    *fhsizep <= 0)
1866 			goto bad;
1867 	} else
1868 		*fhsizep = NFSX_V2FH;
1869 
1870 	if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
1871 		goto bad;
1872 
1873 	if (args->flags & NFSMNT_NFSV3) {
1874 		if (xdr_int_decode(&m, &authcount) != 0)
1875 			goto bad;
1876 		authunixok = 0;
1877 		if (authcount < 0 || authcount > 100)
1878 			goto bad;
1879 		while (authcount > 0) {
1880 			if (xdr_int_decode(&m, &authver) != 0)
1881 				goto bad;
1882 			if (authver == AUTH_UNIX)
1883 				authunixok = 1;
1884 			authcount--;
1885 		}
1886 		if (authunixok == 0)
1887 			goto bad;
1888 	}
1889 
1890 	/* Set port number for NFS use. */
1891 	error = krpc_portmap(mdsin, NFS_PROG,
1892 			     (args->flags &
1893 			      NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
1894 			     &mdsin->sin_port, td);
1895 
1896 	goto out;
1897 
1898 bad:
1899 	error = EBADRPC;
1900 
1901 out:
1902 	m_freem(m);
1903 	return error;
1904 }
1905 
1906 SYSINIT(bootp_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, bootpc_init, NULL);
1907