xref: /freebsd/sys/nfs/bootp_subr.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
1 /* $FreeBSD$	*/
2 
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 "opt_bootp.h"
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/sockio.h>
51 #include <sys/malloc.h>
52 #include <sys/mount.h>
53 #include <sys/mbuf.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/uio.h>
57 
58 #include <net/if.h>
59 #include <net/route.h>
60 
61 #include <netinet/in.h>
62 #include <net/if_types.h>
63 #include <net/if_dl.h>
64 
65 #include <nfs/rpcv2.h>
66 #include <nfs/nfsproto.h>
67 #include <nfs/nfs.h>
68 #include <nfs/nfsdiskless.h>
69 #include <nfs/krpc.h>
70 #include <nfs/xdr_subs.h>
71 
72 
73 #define BOOTP_MIN_LEN		300	/* Minimum size of bootp udp packet */
74 
75 #ifndef BOOTP_SETTLE_DELAY
76 #define BOOTP_SETTLE_DELAY 3
77 #endif
78 
79 /*
80  * What is the longest we will wait before re-sending a request?
81  * Note this is also the frequency of "RPC timeout" messages.
82  * The re-send loop count sup linearly to this maximum, so the
83  * first complaint will happen after (1+2+3+4+5)=15 seconds.
84  */
85 #define	MAX_RESEND_DELAY 5	/* seconds */
86 
87 /* Definitions from RFC951 */
88 struct bootp_packet {
89 	u_int8_t op;
90 	u_int8_t htype;
91 	u_int8_t hlen;
92 	u_int8_t hops;
93 	u_int32_t xid;
94 	u_int16_t secs;
95 	u_int16_t flags;
96 	struct in_addr ciaddr;
97 	struct in_addr yiaddr;
98 	struct in_addr siaddr;
99 	struct in_addr giaddr;
100 	unsigned char chaddr[16];
101 	char sname[64];
102 	char file[128];
103 	unsigned char vend[256];
104 };
105 
106 struct bootpc_ifcontext {
107 	struct bootpc_ifcontext *next;
108 	struct bootp_packet call;
109 	struct bootp_packet reply;
110 	int replylen;
111 	int overload;
112 	struct socket *so;
113 	struct ifreq ireq;
114 	struct ifnet *ifp;
115 	struct sockaddr_dl *sdl;
116 	struct sockaddr_in myaddr;
117 	struct sockaddr_in netmask;
118 	struct sockaddr_in gw;
119 	struct sockaddr_in broadcast;	/* Different for each interface */
120 	int gotgw;
121 	int gotnetmask;
122 	int gotrootpath;
123 	int outstanding;
124 	int sentmsg;
125 	u_int32_t xid;
126 	enum {
127 		IF_BOOTP_UNRESOLVED,
128 		IF_BOOTP_RESOLVED,
129 		IF_BOOTP_FAILED,
130 		IF_DHCP_UNRESOLVED,
131 		IF_DHCP_OFFERED,
132 		IF_DHCP_RESOLVED,
133 		IF_DHCP_FAILED,
134 	} state;
135 	int dhcpquerytype;		/* dhcp type sent */
136 	struct in_addr dhcpserver;
137 	int gotdhcpserver;
138 };
139 
140 #define TAG_MAXLEN 1024
141 struct bootpc_tagcontext {
142 	char buf[TAG_MAXLEN + 1];
143 	int overload;
144 	int badopt;
145 	int badtag;
146 	int foundopt;
147 	int taglen;
148 };
149 
150 struct bootpc_globalcontext {
151 	struct bootpc_ifcontext *interfaces;
152 	struct bootpc_ifcontext *lastinterface;
153 	u_int32_t xid;
154 	int gotrootpath;
155 	int gotswappath;
156 	int gotgw;
157 	int ifnum;
158 	int secs;
159 	int starttime;
160 	struct bootp_packet reply;
161 	int replylen;
162 	struct bootpc_ifcontext *setswapfs;
163 	struct bootpc_ifcontext *setrootfs;
164 	struct bootpc_ifcontext *sethostname;
165 	char lookup_path[24];
166 	struct bootpc_tagcontext tmptag;
167 	struct bootpc_tagcontext tag;
168 };
169 
170 #define IPPORT_BOOTPC 68
171 #define IPPORT_BOOTPS 67
172 
173 #define BOOTP_REQUEST 1
174 #define BOOTP_REPLY 2
175 
176 /* Common tags */
177 #define TAG_PAD		  0  /* Pad option, implicit length 1 */
178 #define TAG_SUBNETMASK	  1  /* RFC 950 subnet mask */
179 #define TAG_ROUTERS	  3  /* Routers (in order of preference) */
180 #define TAG_HOSTNAME	 12  /* Client host name */
181 #define TAG_ROOT	 17  /* Root path */
182 
183 /* DHCP specific tags */
184 #define TAG_OVERLOAD	 52  /* Option Overload */
185 #define TAG_MAXMSGSIZE   57  /* Maximum DHCP Message Size */
186 
187 #define TAG_END		255  /* End Option (i.e. no more options) */
188 
189 /* Overload values */
190 #define OVERLOAD_FILE     1
191 #define OVERLOAD_SNAME    2
192 
193 /* Site specific tags: */
194 #define TAG_SWAP	128
195 #define TAG_SWAPSIZE	129
196 #define TAG_ROOTOPTS	130
197 #define TAG_SWAPOPTS	131
198 
199 #define TAG_DHCP_MSGTYPE 53
200 #define TAG_DHCP_REQ_ADDR 50
201 #define TAG_DHCP_SERVERID 54
202 #define TAG_DHCP_LEASETIME 51
203 
204 #define DHCP_NOMSG    0
205 #define DHCP_DISCOVER 1
206 #define DHCP_OFFER    2
207 #define DHCP_REQUEST  3
208 #define DHCP_ACK      5
209 
210 extern int nfs_diskless_valid;
211 extern struct nfsv3_diskless nfsv3_diskless;
212 
213 /* mountd RPC */
214 static int md_mount(struct sockaddr_in *mdsin, char *path,
215 		    u_char *fhp, int *fhsizep,
216 		    struct nfs_args *args,struct proc *procp);
217 static int md_lookup_swap(struct sockaddr_in *mdsin,char *path,
218 			  u_char *fhp, int *fhsizep,
219 			  struct nfs_args *args,
220 			  struct proc *procp);
221 static int setfs(struct sockaddr_in *addr, char *path, char *p);
222 static int getdec(char **ptr);
223 static char *substr(char *a,char *b);
224 static void mountopts(struct nfs_args *args, char *p);
225 static int xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len);
226 static int xdr_int_decode(struct mbuf **ptr, int *iptr);
227 static void print_in_addr(struct in_addr addr);
228 static void print_sin_addr(struct sockaddr_in *addr);
229 static void clear_sinaddr(struct sockaddr_in *sin);
230 static
231 struct bootpc_ifcontext *allocifctx(struct bootpc_globalcontext *gctx);
232 static void bootpc_compose_query(struct bootpc_ifcontext *ifctx,
233 				 struct bootpc_globalcontext *gctx,
234 				 struct proc *procp);
235 static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx,
236 				 struct bootp_packet *bp, int len, int tag);
237 static void bootpc_tag_helper(struct bootpc_tagcontext *tctx,
238 			      unsigned char *start, int len, int tag);
239 
240 #ifdef BOOTP_DEBUG
241 void bootpboot_p_sa(struct sockaddr *sa,struct sockaddr *ma);
242 void bootpboot_p_ma(struct sockaddr *ma);
243 void bootpboot_p_rtentry(struct rtentry *rt);
244 void bootpboot_p_tree(struct radix_node *rn);
245 void bootpboot_p_rtlist(void);
246 void bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa);
247 void bootpboot_p_iflist(void);
248 #endif
249 
250 static int  bootpc_call(struct bootpc_globalcontext *gctx,
251 			struct proc *procp);
252 
253 static int bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
254 				   struct bootpc_globalcontext *gctx,
255 				   struct proc *procp);
256 
257 static int bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
258 				   struct bootpc_globalcontext *gctx,
259 				   struct proc *procp);
260 
261 static void bootpc_decode_reply(struct nfsv3_diskless *nd,
262 				struct bootpc_ifcontext *ifctx,
263 				struct bootpc_globalcontext *gctx);
264 
265 static int bootpc_received(struct bootpc_globalcontext *gctx,
266 			   struct bootpc_ifcontext *ifctx);
267 
268 static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
269 static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
270 static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
271 
272 void bootpc_init(void);
273 
274 /*
275  * In order to have multiple active interfaces with address 0.0.0.0
276  * and be able to send data to a selected interface, we perform
277  * some tricks:
278  *
279  *  - The 'broadcast' address is different for each interface.
280  *
281  *  - We temporarily add routing pointing 255.255.255.255 to the
282  *    selected interface broadcast address, thus the packet sent
283  *    goes to that interface.
284  */
285 
286 #ifdef BOOTP_DEBUG
287 void
288 bootpboot_p_sa(struct sockaddr *sa,
289 	       struct sockaddr *ma)
290 {
291 	if (sa == NULL) {
292 		printf("(sockaddr *) <null>");
293 		return;
294 	}
295 	switch (sa->sa_family) {
296 	case AF_INET:
297 	{
298 		struct sockaddr_in *sin;
299 
300 		sin = (struct sockaddr_in *) sa;
301 		printf("inet ");
302 		print_sin_addr(sin);
303 		if (ma != NULL) {
304 			sin = (struct sockaddr_in *) ma;
305 			printf(" mask ");
306 			print_sin_addr(sin);
307 		}
308 	}
309 	break;
310 	case AF_LINK:
311 	{
312 		struct sockaddr_dl *sli;
313 		int i;
314 
315 		sli = (struct sockaddr_dl *) sa;
316 		printf("link %.*s ", sli->sdl_nlen, sli->sdl_data);
317 		for (i = 0; i < sli->sdl_alen; i++) {
318 			if (i > 0)
319 				printf(":");
320 			printf("%x", ((unsigned char *) LLADDR(sli))[i]);
321 		}
322 	}
323 	break;
324 	default:
325 		printf("af%d", sa->sa_family);
326 	}
327 }
328 
329 
330 void
331 bootpboot_p_ma(struct sockaddr *ma)
332 {
333 	if (ma == NULL) {
334 		printf("<null>");
335 		return;
336 	}
337 	printf("%x", *(int *)ma);
338 }
339 
340 
341 void
342 bootpboot_p_rtentry(struct rtentry *rt)
343 {
344 	bootpboot_p_sa(rt_key(rt), rt_mask(rt));
345 	printf(" ");
346 	bootpboot_p_ma(rt->rt_genmask);
347 	printf(" ");
348 	bootpboot_p_sa(rt->rt_gateway, NULL);
349 	printf(" ");
350 	printf("flags %x", (unsigned short) rt->rt_flags);
351 	printf(" %d", (int) rt->rt_rmx.rmx_expire);
352 	printf(" %s%d\n", rt->rt_ifp->if_name, rt->rt_ifp->if_unit);
353 }
354 
355 
356 void
357 bootpboot_p_tree(struct radix_node *rn)
358 {
359 	while (rn != NULL) {
360 		if (rn->rn_bit < 0) {
361 			if ((rn->rn_flags & RNF_ROOT) != 0) {
362 			} else {
363 				bootpboot_p_rtentry((struct rtentry *) rn);
364 			}
365 			rn = rn->rn_dupedkey;
366 		} else {
367 			bootpboot_p_tree(rn->rn_left);
368 			bootpboot_p_tree(rn->rn_right);
369 			return;
370 		}
371 	}
372 }
373 
374 
375 void
376 bootpboot_p_rtlist(void)
377 {
378 	printf("Routing table:\n");
379 	bootpboot_p_tree(rt_tables[AF_INET]->rnh_treetop);
380 }
381 
382 
383 void
384 bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
385 {
386 	printf("%s%d flags %x, addr ",
387 	       ifp->if_name,
388 	       ifp->if_unit,
389 	       (unsigned short) ifp->if_flags);
390 	print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
391 	printf(", broadcast ");
392 	print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
393 	printf(", netmask ");
394 	print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
395 	printf("\n");
396 }
397 
398 
399 void
400 bootpboot_p_iflist(void)
401 {
402 	struct ifnet *ifp;
403 	struct ifaddr *ifa;
404 
405 	printf("Interface list:\n");
406 	for (ifp = TAILQ_FIRST(&ifnet);
407 	     ifp != NULL;
408 	     ifp = TAILQ_NEXT(ifp, if_link)) {
409 		for (ifa = TAILQ_FIRST(&ifp->if_addrhead);
410 		     ifa != NULL;
411 		     ifa = TAILQ_NEXT(ifa, ifa_link))
412 			if (ifa->ifa_addr->sa_family == AF_INET)
413 				bootpboot_p_if(ifp, ifa);
414 	}
415 }
416 #endif /* defined(BOOTP_DEBUG) */
417 
418 
419 static void
420 clear_sinaddr(struct sockaddr_in *sin)
421 {
422 	bzero(sin, sizeof(*sin));
423 	sin->sin_len = sizeof(*sin);
424 	sin->sin_family = AF_INET;
425 	sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
426 	sin->sin_port = 0;
427 }
428 
429 
430 static struct bootpc_ifcontext *
431 allocifctx(struct bootpc_globalcontext *gctx)
432 {
433 	struct bootpc_ifcontext *ifctx;
434 	ifctx = (struct bootpc_ifcontext *) malloc(sizeof(*ifctx),
435 						   M_TEMP, M_WAITOK);
436 	if (ifctx == NULL)
437 		panic("Failed to allocate bootp interface context structure");
438 
439 	bzero(ifctx, sizeof(*ifctx));
440 	ifctx->xid = gctx->xid;
441 #ifdef BOOTP_NO_DHCP
442 	ifctx->state = IF_BOOTP_UNRESOLVED;
443 #else
444 	ifctx->state = IF_DHCP_UNRESOLVED;
445 #endif
446 	gctx->xid += 0x100;
447 	return ifctx;
448 }
449 
450 
451 static __inline int
452 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
453 {
454 	if (ifctx->state == IF_BOOTP_RESOLVED ||
455 	    ifctx->state == IF_DHCP_RESOLVED)
456 		return 1;
457 	return 0;
458 }
459 
460 
461 static __inline int
462 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
463 {
464 	if (ifctx->state == IF_BOOTP_UNRESOLVED ||
465 	    ifctx->state == IF_DHCP_UNRESOLVED)
466 		return 1;
467 	return 0;
468 }
469 
470 
471 static __inline int
472 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
473 {
474 	if (ifctx->state == IF_BOOTP_FAILED ||
475 	    ifctx->state == IF_DHCP_FAILED)
476 		return 1;
477 	return 0;
478 }
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 	 * Need timeout for fallback to less
489 	 * desirable alternative.
490 	 */
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 	}
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,
540 	      &ifctx->reply,
541 	      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,
584 	    struct proc *procp)
585 {
586 	struct socket *so;
587 	struct sockaddr_in *sin, dst;
588 	struct uio auio;
589 	struct sockopt sopt;
590 	struct iovec aio;
591 	int error, on, rcvflg, timo, len;
592 	time_t atimo;
593 	time_t rtimo;
594 	struct timeval tv;
595 	struct bootpc_ifcontext *ifctx;
596 	int outstanding;
597 	int gotrootpath;
598 	int retry;
599 	const char *s;
600 
601 	/*
602 	 * Create socket and set its recieve timeout.
603 	 */
604 	error = socreate(AF_INET, &so, SOCK_DGRAM, 0, procp);
605 	if (error != 0)
606 		goto out;
607 
608 	tv.tv_sec = 1;
609 	tv.tv_usec = 0;
610 	bzero(&sopt, sizeof(sopt));
611 	sopt.sopt_level = SOL_SOCKET;
612 	sopt.sopt_name = SO_RCVTIMEO;
613 	sopt.sopt_val = &tv;
614 	sopt.sopt_valsize = sizeof tv;
615 
616 	error = sosetopt(so, &sopt);
617 	if (error != 0)
618 		goto out;
619 
620 	/*
621 	 * Enable broadcast.
622 	 */
623 	on = 1;
624 	sopt.sopt_name = SO_BROADCAST;
625 	sopt.sopt_val = &on;
626 	sopt.sopt_valsize = sizeof on;
627 
628 	error = sosetopt(so, &sopt);
629 	if (error != 0)
630 		goto out;
631 
632 	/*
633 	 * Disable routing.
634 	 */
635 
636 	on = 1;
637 	sopt.sopt_name = SO_DONTROUTE;
638 	sopt.sopt_val = &on;
639 	sopt.sopt_valsize = sizeof on;
640 
641 	error = sosetopt(so, &sopt);
642 	if (error != 0)
643 		goto out;
644 
645 	/*
646 	 * Bind the local endpoint to a bootp client port.
647 	 */
648 	sin = &dst;
649 	clear_sinaddr(sin);
650 	sin->sin_port = htons(IPPORT_BOOTPC);
651 	error = sobind(so, (struct sockaddr *)sin, procp);
652 	if (error != 0) {
653 		printf("bind failed\n");
654 		goto out;
655 	}
656 
657 	/*
658 	 * Setup socket address for the server.
659 	 */
660 	sin = &dst;
661 	clear_sinaddr(sin);
662 	sin->sin_addr.s_addr = INADDR_BROADCAST;
663 	sin->sin_port = htons(IPPORT_BOOTPS);
664 
665 	/*
666 	 * Send it, repeatedly, until a reply is received,
667 	 * but delay each re-send by an increasing amount.
668 	 * If the delay hits the maximum, start complaining.
669 	 */
670 	timo = 0;
671 	rtimo = 0;
672 	for (;;) {
673 
674 		outstanding = 0;
675 		gotrootpath = 0;
676 
677 		for (ifctx = gctx->interfaces;
678 		     ifctx != NULL;
679 		     ifctx = ifctx->next) {
680 			if (bootpc_ifctx_isresolved(ifctx) != 0 &&
681 			    bootpc_tag(&gctx->tmptag, &ifctx->reply,
682 				       ifctx->replylen,
683 				       TAG_ROOT) != NULL)
684 				gotrootpath = 1;
685 		}
686 
687 		for (ifctx = gctx->interfaces;
688 		     ifctx != NULL;
689 		     ifctx = ifctx->next) {
690 			ifctx->outstanding = 0;
691 			if (bootpc_ifctx_isresolved(ifctx)  != 0 &&
692 			    gotrootpath != 0) {
693 				continue;
694 			}
695 			if (bootpc_ifctx_isfailed(ifctx) != 0)
696 				continue;
697 
698 			outstanding++;
699 			ifctx->outstanding = 1;
700 
701 			/* Proceed to next step in DHCP negotiation */
702 			if ((ifctx->state == IF_DHCP_OFFERED &&
703 			     ifctx->dhcpquerytype != DHCP_REQUEST) ||
704 			    (ifctx->state == IF_DHCP_UNRESOLVED &&
705 			     ifctx->dhcpquerytype != DHCP_DISCOVER) ||
706 			    (ifctx->state == IF_BOOTP_UNRESOLVED &&
707 			     ifctx->dhcpquerytype != DHCP_NOMSG)) {
708 				ifctx->sentmsg = 0;
709 				bootpc_compose_query(ifctx, gctx, procp);
710 			}
711 
712 			/* Send BOOTP request (or re-send). */
713 
714 			if (ifctx->sentmsg == 0) {
715 				switch(ifctx->dhcpquerytype) {
716 				case DHCP_DISCOVER:
717 					s = "DHCP Discover";
718 					break;
719 				case DHCP_REQUEST:
720 					s = "DHCP Request";
721 					break;
722 				case DHCP_NOMSG:
723 				default:
724 					s = "BOOTP Query";
725 					break;
726 				}
727 				printf("Sending %s packet from "
728 				       "interface %s (%*D)\n",
729 				       s,
730 				       ifctx->ireq.ifr_name,
731 				       ifctx->sdl->sdl_alen,
732 				       (unsigned char *) LLADDR(ifctx->sdl),
733 				       ":");
734 				ifctx->sentmsg = 1;
735 			}
736 
737 			aio.iov_base = (caddr_t) &ifctx->call;
738 			aio.iov_len = sizeof(ifctx->call);
739 
740 			auio.uio_iov = &aio;
741 			auio.uio_iovcnt = 1;
742 			auio.uio_segflg = UIO_SYSSPACE;
743 			auio.uio_rw = UIO_WRITE;
744 			auio.uio_offset = 0;
745 			auio.uio_resid = sizeof(ifctx->call);
746 			auio.uio_procp = procp;
747 
748 			/* Set netmask to 0.0.0.0 */
749 
750 			sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
751 			clear_sinaddr(sin);
752 			error = ifioctl(ifctx->so, SIOCSIFNETMASK,
753 					(caddr_t) &ifctx->ireq, procp);
754 			if (error != 0)
755 				panic("bootpc_call:"
756 				      "set if netmask, error=%d",
757 				      error);
758 
759 			error = sosend(so, (struct sockaddr *) &dst,
760 				       &auio, NULL, NULL, 0, procp);
761 			if (error != 0) {
762 				printf("bootpc_call: sosend: %d state %08x\n",
763 				       error, (int) so->so_state);
764 			}
765 
766 			/* XXX: Is this needed ? */
767 			tsleep(&error, PZERO + 8, "bootpw", 10);
768 
769 			/* Set netmask to 255.0.0.0 */
770 
771 			sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
772 			clear_sinaddr(sin);
773 			sin->sin_addr.s_addr = htonl(0xff000000u);
774 			error = ifioctl(ifctx->so, SIOCSIFNETMASK,
775 					(caddr_t) &ifctx->ireq, procp);
776 			if (error != 0)
777 				panic("bootpc_call:"
778 				      "set if netmask, error=%d",
779 				      error);
780 
781 		}
782 
783 		if (outstanding == 0 &&
784 		    (rtimo == 0 || time_second >= rtimo)) {
785 			error = 0;
786 			goto gotreply;
787 		}
788 
789 		/* Determine new timeout. */
790 		if (timo < MAX_RESEND_DELAY)
791 			timo++;
792 		else {
793 			printf("DHCP/BOOTP timeout for server ");
794 			print_sin_addr(&dst);
795 			printf("\n");
796 		}
797 
798 		/*
799 		 * Wait for up to timo seconds for a reply.
800 		 * The socket receive timeout was set to 1 second.
801 		 */
802 		atimo = timo + time_second;
803 		while (time_second < atimo) {
804 			aio.iov_base = (caddr_t) &gctx->reply;
805 			aio.iov_len = sizeof(gctx->reply);
806 
807 			auio.uio_iov = &aio;
808 			auio.uio_iovcnt = 1;
809 			auio.uio_segflg = UIO_SYSSPACE;
810 			auio.uio_rw = UIO_READ;
811 			auio.uio_offset = 0;
812 			auio.uio_resid = sizeof(gctx->reply);
813 			auio.uio_procp = procp;
814 
815 			rcvflg = 0;
816 			error = soreceive(so, NULL, &auio,
817 					  NULL, NULL, &rcvflg);
818 			gctx->secs = time_second - gctx->starttime;
819 			for (ifctx = gctx->interfaces;
820 			     ifctx != NULL;
821 			     ifctx = ifctx->next) {
822 				if (bootpc_ifctx_isresolved(ifctx) != 0 ||
823 				    bootpc_ifctx_isfailed(ifctx) != 0)
824 					continue;
825 
826 				ifctx->call.secs = htons(gctx->secs);
827 			}
828 			if (error == EWOULDBLOCK)
829 				continue;
830 			if (error != 0)
831 				goto out;
832 			len = sizeof(gctx->reply) - auio.uio_resid;
833 
834 			/* Do we have the required number of bytes ? */
835 			if (len < BOOTP_MIN_LEN)
836 				continue;
837 			gctx->replylen = len;
838 
839 			/* Is it a reply? */
840 			if (gctx->reply.op != BOOTP_REPLY)
841 				continue;
842 
843 			/* Is this an answer to our query */
844 			for (ifctx = gctx->interfaces;
845 			     ifctx != NULL;
846 			     ifctx = ifctx->next) {
847 				if (gctx->reply.xid != ifctx->call.xid)
848 					continue;
849 
850 				/* Same HW address size ? */
851 				if (gctx->reply.hlen != ifctx->call.hlen)
852 					continue;
853 
854 				/* Correct HW address ? */
855 				if (bcmp(gctx->reply.chaddr,
856 					 ifctx->call.chaddr,
857 					 ifctx->call.hlen) != 0)
858 					continue;
859 
860 				break;
861 			}
862 
863 			if (ifctx != NULL) {
864 				s =  bootpc_tag(&gctx->tmptag,
865 						&gctx->reply,
866 						gctx->replylen,
867 						TAG_DHCP_MSGTYPE);
868 				if (s != NULL) {
869 					switch (*s) {
870 					case DHCP_OFFER:
871 						s = "DHCP Offer";
872 						break;
873 					case DHCP_ACK:
874 						s = "DHCP Ack";
875 						break;
876 					default:
877 						s = "DHCP (unexpected)";
878 						break;
879 					}
880 				} else
881 					s = "BOOTP Reply";
882 
883 				printf("Received %s packet"
884 				       " on %s from ",
885 				       s,
886 				       ifctx->ireq.ifr_name);
887 				print_in_addr(gctx->reply.siaddr);
888 				if (gctx->reply.giaddr.s_addr !=
889 				    htonl(INADDR_ANY)) {
890 					printf(" via ");
891 					print_in_addr(gctx->reply.giaddr);
892 				}
893 				if (bootpc_received(gctx, ifctx) != 0) {
894 					printf(" (accepted)");
895 					if (ifctx->outstanding) {
896 						ifctx->outstanding = 0;
897 						outstanding--;
898 					}
899 					/* Network settle delay */
900 					if (outstanding == 0)
901 						atimo = time_second +
902 							BOOTP_SETTLE_DELAY;
903 				} else
904 					printf(" (ignored)");
905 				if (ifctx->gotrootpath) {
906 					gotrootpath = 1;
907 					rtimo = time_second +
908 						BOOTP_SETTLE_DELAY;
909 					printf(" (got root path)");
910 				} else
911 					printf(" (no root path)");
912 				printf("\n");
913 			}
914 		} /* while secs */
915 #ifdef BOOTP_TIMEOUT
916 		if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
917 			break;
918 #endif
919 		/* Force a retry if halfway in DHCP negotiation */
920 		retry = 0;
921 		for (ifctx = gctx->interfaces; ifctx != NULL;
922 		     ifctx = ifctx->next) {
923 			if (ifctx->state == IF_DHCP_OFFERED) {
924 				if (ifctx->dhcpquerytype == DHCP_DISCOVER)
925 					retry = 1;
926 				else
927 					ifctx->state = IF_DHCP_UNRESOLVED;
928 			}
929 		}
930 
931 		if (retry != 0)
932 			continue;
933 
934 		if (gotrootpath != 0) {
935 			gctx->gotrootpath = gotrootpath;
936 			if (rtimo != 0 && time_second >= rtimo)
937 				break;
938 		}
939 	} /* forever send/receive */
940 
941 	/*
942 	 * XXX: These are errors of varying seriousness being silently
943 	 * ignored
944 	 */
945 
946 	for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
947 		if (bootpc_ifctx_isresolved(ifctx) == 0) {
948 			printf("%s timeout for interface %s\n",
949 			       ifctx->dhcpquerytype != DHCP_NOMSG ?
950 			       "DHCP" : "BOOTP",
951 			       ifctx->ireq.ifr_name);
952 		}
953 	}
954 	if (gctx->gotrootpath != 0) {
955 #if 0
956 		printf("Got a root path, ignoring remaining timeout\n");
957 #endif
958 		error = 0;
959 		goto out;
960 	}
961 #ifndef BOOTP_NFSROOT
962 	for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
963 		if (bootpc_ifctx_isresolved(ifctx) != 0) {
964 			error = 0;
965 			goto out;
966 		}
967 	}
968 #endif
969 	error = ETIMEDOUT;
970 	goto out;
971 
972 gotreply:
973 out:
974 	soclose(so);
975 	return error;
976 }
977 
978 
979 static int
980 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
981 			struct bootpc_globalcontext *gctx,
982 			struct proc *procp)
983 {
984 	struct sockaddr_in *sin;
985 	int error;
986 
987 	struct ifreq *ireq;
988 	struct socket *so;
989 	struct ifaddr *ifa;
990 	struct sockaddr_dl *sdl;
991 
992 	error = socreate(AF_INET, &ifctx->so, SOCK_DGRAM, 0, procp);
993 	if (error != 0)
994 		panic("nfs_boot: socreate, error=%d", error);
995 
996 	ireq = &ifctx->ireq;
997 	so = ifctx->so;
998 
999 	/*
1000 	 * Bring up the interface.
1001 	 *
1002 	 * Get the old interface flags and or IFF_UP into them; if
1003 	 * IFF_UP set blindly, interface selection can be clobbered.
1004 	 */
1005 	error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, procp);
1006 	if (error != 0)
1007 		panic("bootpc_fakeup_interface: GIFFLAGS, error=%d", error);
1008 	ireq->ifr_flags |= IFF_UP;
1009 	error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, procp);
1010 	if (error != 0)
1011 		panic("bootpc_fakeup_interface: SIFFLAGS, error=%d", error);
1012 
1013 	/*
1014 	 * Do enough of ifconfig(8) so that the chosen interface
1015 	 * can talk to the servers.  (just set the address)
1016 	 */
1017 
1018 	/* addr is 0.0.0.0 */
1019 
1020 	sin = (struct sockaddr_in *) &ireq->ifr_addr;
1021 	clear_sinaddr(sin);
1022 	error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, procp);
1023 	if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1024 		panic("bootpc_fakeup_interface: "
1025 		      "set if addr, error=%d", error);
1026 
1027 	/* netmask is 255.0.0.0 */
1028 
1029 	sin = (struct sockaddr_in *) &ireq->ifr_addr;
1030 	clear_sinaddr(sin);
1031 	sin->sin_addr.s_addr = htonl(0xff000000u);
1032 	error = ifioctl(so, SIOCSIFNETMASK, (caddr_t)ireq, procp);
1033 	if (error != 0)
1034 		panic("bootpc_fakeup_interface: set if netmask, error=%d",
1035 		      error);
1036 
1037 	/* Broadcast is 255.255.255.255 */
1038 
1039 	sin = (struct sockaddr_in *)&ireq->ifr_addr;
1040 	clear_sinaddr(sin);
1041 	clear_sinaddr(&ifctx->broadcast);
1042 	sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
1043 	ifctx->broadcast.sin_addr.s_addr = sin->sin_addr.s_addr;
1044 
1045 	error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t)ireq, procp);
1046 	if (error != 0)
1047 		panic("bootpc_fakeup_interface: "
1048 		      "set if broadcast addr, error=%d",
1049 		      error);
1050 
1051 	/* Get HW address */
1052 
1053 	sdl = NULL;
1054 	for (ifa = TAILQ_FIRST(&ifctx->ifp->if_addrhead);
1055 	     ifa != NULL;
1056 	     ifa = TAILQ_NEXT(ifa,ifa_link))
1057 		if (ifa->ifa_addr->sa_family == AF_LINK &&
1058 		    (sdl = ((struct sockaddr_dl *) ifa->ifa_addr)) != NULL &&
1059 		    sdl->sdl_type == IFT_ETHER)
1060 			break;
1061 
1062 	if (sdl == NULL)
1063 		panic("bootpc: Unable to find HW address for %s",
1064 		      ifctx->ireq.ifr_name);
1065 	ifctx->sdl = sdl;
1066 
1067 	return error;
1068 }
1069 
1070 
1071 static int
1072 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
1073 			struct bootpc_globalcontext *gctx,
1074 			struct proc *procp)
1075 {
1076 	int error;
1077 	struct sockaddr_in defdst;
1078 	struct sockaddr_in defmask;
1079 	struct sockaddr_in *sin;
1080 
1081 	struct ifreq *ireq;
1082 	struct socket *so;
1083 	struct sockaddr_in *myaddr;
1084 	struct sockaddr_in *netmask;
1085 	struct sockaddr_in *gw;
1086 
1087 	ireq = &ifctx->ireq;
1088 	so = ifctx->so;
1089 	myaddr = &ifctx->myaddr;
1090 	netmask = &ifctx->netmask;
1091 	gw = &ifctx->gw;
1092 
1093 	if (bootpc_ifctx_isresolved(ifctx) == 0) {
1094 
1095 		/* Shutdown interfaces where BOOTP failed */
1096 
1097 		printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
1098 		error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, procp);
1099 		if (error != 0)
1100 			panic("bootpc_adjust_interface: "
1101 			      "SIOCGIFFLAGS, error=%d", error);
1102 		ireq->ifr_flags &= ~IFF_UP;
1103 		error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, procp);
1104 		if (error != 0)
1105 			panic("bootpc_adjust_interface: "
1106 			      "SIOCSIFFLAGS, error=%d", error);
1107 
1108 		sin = (struct sockaddr_in *) &ireq->ifr_addr;
1109 		clear_sinaddr(sin);
1110 		error = ifioctl(so, SIOCDIFADDR, (caddr_t) ireq, procp);
1111 		if (error != 0 && (error != EEXIST ||
1112 				   ifctx == gctx->interfaces))
1113 			panic("bootpc_adjust_interface: "
1114 			      "SIOCDIFADDR, error=%d", error);
1115 
1116 		return 0;
1117 	}
1118 
1119 	printf("Adjusted interface %s\n", ifctx->ireq.ifr_name);
1120 	/*
1121 	 * Do enough of ifconfig(8) so that the chosen interface
1122 	 * can talk to the servers.  (just set the address)
1123 	 */
1124 	bcopy(netmask, &ireq->ifr_addr, sizeof(*netmask));
1125 	error = ifioctl(so, SIOCSIFNETMASK, (caddr_t) ireq, procp);
1126 	if (error != 0)
1127 		panic("bootpc_adjust_interface: "
1128 		      "set if netmask, error=%d", error);
1129 
1130 	/* Broadcast is with host part of IP address all 1's */
1131 
1132 	sin = (struct sockaddr_in *) &ireq->ifr_addr;
1133 	clear_sinaddr(sin);
1134 	sin->sin_addr.s_addr = myaddr->sin_addr.s_addr |
1135 		~ netmask->sin_addr.s_addr;
1136 	error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t) ireq, procp);
1137 	if (error != 0)
1138 		panic("bootpc_adjust_interface: "
1139 		      "set if broadcast addr, error=%d", error);
1140 
1141 	bcopy(myaddr, &ireq->ifr_addr, sizeof(*myaddr));
1142 	error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, procp);
1143 	if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1144 		panic("bootpc_adjust_interface: "
1145 		      "set if addr, error=%d", error);
1146 
1147 	/* Add new default route */
1148 
1149 	if (ifctx->gotgw != 0 || gctx->gotgw == 0) {
1150 		clear_sinaddr(&defdst);
1151 		clear_sinaddr(&defmask);
1152 		error = rtrequest(RTM_ADD,
1153 				  (struct sockaddr *) &defdst,
1154 				  (struct sockaddr *) gw,
1155 				  (struct sockaddr *) &defmask,
1156 				  (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
1157 		if (error != 0) {
1158 			printf("bootpc_adjust_interface: "
1159 			       "add net route, error=%d\n", error);
1160 			return error;
1161 		}
1162 	}
1163 
1164 	return 0;
1165 }
1166 
1167 
1168 static int
1169 setfs(struct sockaddr_in *addr, char *path, char *p)
1170 {
1171 	unsigned int ip;
1172 	int val;
1173 
1174 	ip = 0;
1175 	if (((val = getdec(&p)) < 0) || (val > 255))
1176 		return 0;
1177 	ip = val << 24;
1178 	if (*p != '.')
1179 		return 0;
1180 	p++;
1181 	if (((val = getdec(&p)) < 0) || (val > 255))
1182 		return 0;
1183 	ip |= (val << 16);
1184 	if (*p != '.')
1185 		return 0;
1186 	p++;
1187 	if (((val = getdec(&p)) < 0) || (val > 255))
1188 		return 0;
1189 	ip |= (val << 8);
1190 	if (*p != '.')
1191 		return 0;
1192 	p++;
1193 	if (((val = getdec(&p)) < 0) || (val > 255))
1194 		return 0;
1195 	ip |= val;
1196 	if (*p != ':')
1197 		return 0;
1198 	p++;
1199 
1200 	addr->sin_addr.s_addr = htonl(ip);
1201 	addr->sin_len = sizeof(struct sockaddr_in);
1202 	addr->sin_family = AF_INET;
1203 
1204 	strncpy(path, p, MNAMELEN - 1);
1205 	return 1;
1206 }
1207 
1208 
1209 static int
1210 getdec(char **ptr)
1211 {
1212 	char *p;
1213 	int ret;
1214 
1215 	p = *ptr;
1216 	ret = 0;
1217 	if ((*p < '0') || (*p > '9'))
1218 		return -1;
1219 	while ((*p >= '0') && (*p <= '9')) {
1220 		ret = ret * 10 + (*p - '0');
1221 		p++;
1222 	}
1223 	*ptr = p;
1224 	return ret;
1225 }
1226 
1227 
1228 static char *
1229 substr(char *a, char *b)
1230 {
1231 	char *loc1;
1232 	char *loc2;
1233 
1234         while (*a != '\0') {
1235                 loc1 = a;
1236                 loc2 = b;
1237                 while (*loc1 == *loc2++) {
1238                         if (*loc1 == '\0')
1239 				return 0;
1240                         loc1++;
1241                         if (*loc2 == '\0')
1242 				return loc1;
1243                 }
1244 		a++;
1245         }
1246         return 0;
1247 }
1248 
1249 
1250 static void
1251 mountopts(struct nfs_args *args, char *p)
1252 {
1253 	char *tmp;
1254 
1255 	args->version = NFS_ARGSVERSION;
1256 	args->rsize = 8192;
1257 	args->wsize = 8192;
1258 	args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
1259 	args->sotype = SOCK_DGRAM;
1260 	if (p == NULL)
1261 		return;
1262 	if ((tmp = (char *)substr(p, "rsize=")))
1263 		args->rsize = getdec(&tmp);
1264 	if ((tmp = (char *)substr(p, "wsize=")))
1265 		args->wsize = getdec(&tmp);
1266 	if ((tmp = (char *)substr(p, "intr")))
1267 		args->flags |= NFSMNT_INT;
1268 	if ((tmp = (char *)substr(p, "soft")))
1269 		args->flags |= NFSMNT_SOFT;
1270 	if ((tmp = (char *)substr(p, "noconn")))
1271 		args->flags |= NFSMNT_NOCONN;
1272 	if ((tmp = (char *)substr(p, "tcp")))
1273 		args->sotype = SOCK_STREAM;
1274 }
1275 
1276 
1277 static int
1278 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
1279 {
1280 	struct mbuf *m;
1281 	int alignedlen;
1282 
1283 	m = *mptr;
1284 	alignedlen = ( len + 3 ) & ~3;
1285 
1286 	if (m->m_len < alignedlen) {
1287 		m = m_pullup(m, alignedlen);
1288 		if (m == NULL) {
1289 			*mptr = NULL;
1290 			return EBADRPC;
1291 		}
1292 	}
1293 	bcopy(mtod(m, u_char *), buf, len);
1294 	m_adj(m, alignedlen);
1295 	*mptr = m;
1296 	return 0;
1297 }
1298 
1299 
1300 static int
1301 xdr_int_decode(struct mbuf **mptr, int *iptr)
1302 {
1303 	u_int32_t i;
1304 	if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
1305 		return EBADRPC;
1306 	*iptr = fxdr_unsigned(u_int32_t, i);
1307 	return 0;
1308 }
1309 
1310 
1311 static void
1312 print_sin_addr(struct sockaddr_in *sin)
1313 {
1314 	print_in_addr(sin->sin_addr);
1315 }
1316 
1317 
1318 static void
1319 print_in_addr(struct in_addr addr)
1320 {
1321 	unsigned int ip;
1322 
1323 	ip = ntohl(addr.s_addr);
1324 	printf("%d.%d.%d.%d",
1325 	       ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1326 }
1327 
1328 static void
1329 bootpc_compose_query(ifctx, gctx, procp)
1330 	struct bootpc_ifcontext *ifctx;
1331 	struct bootpc_globalcontext *gctx;
1332 	struct proc *procp;
1333 {
1334 	unsigned char *vendp;
1335 	uint32_t leasetime;
1336 
1337 	ifctx->gotrootpath = 0;
1338 
1339 	bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1340 
1341 	/* bootpc part */
1342 	ifctx->call.op = BOOTP_REQUEST; 	/* BOOTREQUEST */
1343 	ifctx->call.htype = 1;			/* 10mb ethernet */
1344 	ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1345 	ifctx->call.hops = 0;
1346 	if (bootpc_ifctx_isunresolved(ifctx) != 0)
1347 		ifctx->xid++;
1348 	ifctx->call.xid = txdr_unsigned(ifctx->xid);
1349 	bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1350 
1351 	vendp = ifctx->call.vend;
1352 	*vendp++ = 99;		/* RFC1048 cookie */
1353 	*vendp++ = 130;
1354 	*vendp++ = 83;
1355 	*vendp++ = 99;
1356 	*vendp++ = TAG_MAXMSGSIZE;
1357 	*vendp++ = 2;
1358 	*vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1359 	*vendp++ = sizeof(struct bootp_packet) & 255;
1360 	ifctx->dhcpquerytype = DHCP_NOMSG;
1361 	switch (ifctx->state) {
1362 	case IF_DHCP_UNRESOLVED:
1363 		*vendp++ = TAG_DHCP_MSGTYPE;
1364 		*vendp++ = 1;
1365 		*vendp++ = DHCP_DISCOVER;
1366 		ifctx->dhcpquerytype = DHCP_DISCOVER;
1367 		ifctx->gotdhcpserver = 0;
1368 		break;
1369 	case IF_DHCP_OFFERED:
1370 		*vendp++ = TAG_DHCP_MSGTYPE;
1371 		*vendp++ = 1;
1372 		*vendp++ = DHCP_REQUEST;
1373 		ifctx->dhcpquerytype = DHCP_REQUEST;
1374 		*vendp++ = TAG_DHCP_REQ_ADDR;
1375 		*vendp++ = 4;
1376 		memcpy(vendp, &ifctx->reply.yiaddr, 4);
1377 		vendp += 4;
1378 		if (ifctx->gotdhcpserver != 0) {
1379 			*vendp++ = TAG_DHCP_SERVERID;
1380 			*vendp++ = 4;
1381 			memcpy(vendp, &ifctx->dhcpserver, 4);
1382 			vendp += 4;
1383 		}
1384 		*vendp++ = TAG_DHCP_LEASETIME;
1385 		*vendp++ = 4;
1386 		leasetime = htonl(300);
1387 		memcpy(vendp, &leasetime, 4);
1388 		vendp += 4;
1389 	default:
1390 		;
1391 	}
1392 	*vendp = TAG_END;
1393 
1394 	ifctx->call.secs = 0;
1395 	ifctx->call.flags = htons(0x8000); /* We need an broadcast answer */
1396 }
1397 
1398 
1399 static int
1400 bootpc_hascookie(struct bootp_packet *bp)
1401 {
1402 	return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1403 		bp->vend[2] == 83 && bp->vend[3] == 99);
1404 }
1405 
1406 
1407 static void
1408 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1409 		  unsigned char *start,
1410 		  int len,
1411 		  int tag)
1412 {
1413 	unsigned char *j;
1414 	unsigned char *ej;
1415 	unsigned char code;
1416 
1417 	if (tctx->badtag != 0 || tctx->badopt != 0)
1418 		return;
1419 
1420 	j = start;
1421 	ej = j + len;
1422 
1423 	while (j < ej) {
1424 		code = *j++;
1425 		if (code == TAG_PAD)
1426 			continue;
1427 		if (code == TAG_END)
1428 			return;
1429 		if (j >= ej || j + *j + 1 > ej) {
1430 			tctx->badopt = 1;
1431 			return;
1432 		}
1433 		len = *j++;
1434 		if (code == tag) {
1435 			if (tctx->taglen + len > TAG_MAXLEN) {
1436 				tctx->badtag = 1;
1437 				return;
1438 			}
1439 			tctx->foundopt = 1;
1440 			if (len > 0)
1441 				memcpy(tctx->buf + tctx->taglen,
1442 				       j, len);
1443 			tctx->taglen += len;
1444 		}
1445 		if (code == TAG_OVERLOAD)
1446 			tctx->overload = *j;
1447 
1448 		j += len;
1449 	}
1450 }
1451 
1452 
1453 static unsigned char *
1454 bootpc_tag(struct bootpc_tagcontext *tctx,
1455 	   struct bootp_packet *bp,
1456 	   int len,
1457 	   int tag)
1458 {
1459 	unsigned char *j;
1460 	unsigned char *ej;
1461 
1462 	tctx->overload = 0;
1463 	tctx->badopt = 0;
1464 	tctx->badtag = 0;
1465 	tctx->foundopt = 0;
1466 	tctx->taglen = 0;
1467 
1468 	if (bootpc_hascookie(bp) == 0)
1469 		return NULL;
1470 
1471 	j = &bp->vend[4];
1472 	ej = (unsigned char *) bp + len;
1473 
1474 	bootpc_tag_helper(tctx, &bp->vend[4],
1475 			  (unsigned char *) bp + len - &bp->vend[4], tag);
1476 
1477 	if ((tctx->overload & OVERLOAD_FILE) != 0)
1478 		bootpc_tag_helper(tctx,
1479 				  (unsigned char *) bp->file,
1480 				  sizeof(bp->file),
1481 				  tag);
1482 	if ((tctx->overload & OVERLOAD_SNAME) != 0)
1483 		bootpc_tag_helper(tctx,
1484 				  (unsigned char *) bp->sname,
1485 				  sizeof(bp->sname),
1486 				  tag);
1487 
1488 	if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1489 		return NULL;
1490 	tctx->buf[tctx->taglen] = '\0';
1491 	return tctx->buf;
1492 }
1493 
1494 
1495 static void
1496 bootpc_decode_reply(nd, ifctx, gctx)
1497 	struct nfsv3_diskless *nd;
1498 	struct bootpc_ifcontext *ifctx;
1499 	struct bootpc_globalcontext *gctx;
1500 {
1501 	char *p;
1502 	unsigned int ip;
1503 
1504 	ifctx->gotgw = 0;
1505 	ifctx->gotnetmask = 0;
1506 
1507 	clear_sinaddr(&ifctx->myaddr);
1508 	clear_sinaddr(&ifctx->netmask);
1509 	clear_sinaddr(&ifctx->gw);
1510 
1511 	ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1512 
1513 	ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1514 	snprintf(gctx->lookup_path, sizeof(gctx->lookup_path),
1515 		 "swap.%d.%d.%d.%d",
1516 		 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1517 
1518 	printf("%s at ", ifctx->ireq.ifr_name);
1519 	print_sin_addr(&ifctx->myaddr);
1520 	printf(" server ");
1521 	print_in_addr(ifctx->reply.siaddr);
1522 
1523 	ifctx->gw.sin_addr = ifctx->reply.giaddr;
1524 	if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1525 		printf(" via gateway ");
1526 		print_in_addr(ifctx->reply.giaddr);
1527 	}
1528 
1529 	/* This call used for the side effect (overload flag) */
1530 	(void) bootpc_tag(&gctx->tmptag,
1531 			  &ifctx->reply, ifctx->replylen, TAG_END);
1532 
1533 	if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1534 		if (ifctx->reply.sname[0] != '\0')
1535 			printf(" server name %s", ifctx->reply.sname);
1536 	if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1537 		if (ifctx->reply.file[0] != '\0')
1538 			printf(" boot file %s", ifctx->reply.file);
1539 
1540 	printf("\n");
1541 
1542 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1543 		       TAG_SUBNETMASK);
1544 	if (p != NULL) {
1545 		if (gctx->tag.taglen != 4)
1546 			panic("bootpc: subnet mask len is %d",
1547 			      gctx->tag.taglen);
1548 		bcopy(p, &ifctx->netmask.sin_addr, 4);
1549 		ifctx->gotnetmask = 1;
1550 		printf("subnet mask ");
1551 		print_sin_addr(&ifctx->netmask);
1552 		printf(" ");
1553 	}
1554 
1555 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1556 		       TAG_ROUTERS);
1557 	if (p != NULL) {
1558 		/* Routers */
1559 		if (gctx->tag.taglen % 4)
1560 			panic("bootpc: Router Len is %d", gctx->tag.taglen);
1561 		if (gctx->tag.taglen > 0) {
1562 			bcopy(p, &ifctx->gw.sin_addr, 4);
1563 			printf("router ");
1564 			print_sin_addr(&ifctx->gw);
1565 			printf(" ");
1566 			ifctx->gotgw = 1;
1567 			gctx->gotgw = 1;
1568 		}
1569 	}
1570 
1571 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1572 		       TAG_ROOT);
1573 	if (p != NULL) {
1574 		if (gctx->setrootfs != NULL) {
1575 			printf("rootfs %s (ignored) ", p);
1576 		} else 	if (setfs(&nd->root_saddr,
1577 				  nd->root_hostnam, p)) {
1578 			printf("rootfs %s ",p);
1579 			gctx->gotrootpath = 1;
1580 			ifctx->gotrootpath = 1;
1581 			gctx->setrootfs = ifctx;
1582 
1583 			p = bootpc_tag(&gctx->tag, &ifctx->reply,
1584 				       ifctx->replylen,
1585 				       TAG_ROOTOPTS);
1586 			if (p != NULL) {
1587 				mountopts(&nd->root_args, p);
1588 				printf("rootopts %s ", p);
1589 			}
1590 		} else
1591 			panic("Failed to set rootfs to %s",p);
1592 	}
1593 
1594 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1595 		       TAG_SWAP);
1596 	if (p != NULL) {
1597 		if (gctx->setswapfs != NULL) {
1598 			printf("swapfs %s (ignored) ", p);
1599 		} else 	if (setfs(&nd->swap_saddr,
1600 				  nd->swap_hostnam, p)) {
1601 			gctx->gotswappath = 1;
1602 			gctx->setswapfs = ifctx;
1603 			printf("swapfs %s ", p);
1604 
1605 			p = bootpc_tag(&gctx->tag, &ifctx->reply,
1606 				       ifctx->replylen,
1607 				       TAG_SWAPOPTS);
1608 			if (p != NULL) {
1609 				/* swap mount options */
1610 				mountopts(&nd->swap_args, p);
1611 				printf("swapopts %s ", p);
1612 			}
1613 
1614 			p = bootpc_tag(&gctx->tag, &ifctx->reply,
1615 				       ifctx->replylen,
1616 				       TAG_SWAPSIZE);
1617 			if (p != NULL) {
1618 				int swaplen;
1619 				if (gctx->tag.taglen != 4)
1620 					panic("bootpc: "
1621 					      "Expected 4 bytes for swaplen, "
1622 					      "not %d bytes",
1623 					      gctx->tag.taglen);
1624 				bcopy(p, &swaplen, 4);
1625 				nd->swap_nblks = ntohl(swaplen);
1626 				printf("swapsize %d KB ",
1627 				       nd->swap_nblks);
1628 			}
1629 		} else
1630 			panic("Failed to set swapfs to %s", p);
1631 	}
1632 
1633 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1634 		       TAG_HOSTNAME);
1635 	if (p != NULL) {
1636 		if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1637 			panic("bootpc: hostname >= %d bytes",
1638 			      MAXHOSTNAMELEN);
1639 		if (gctx->sethostname != NULL) {
1640 			printf("hostname %s (ignored) ", p);
1641 		} else {
1642 			strcpy(nd->my_hostnam, p);
1643 			strcpy(hostname, p);
1644 			printf("hostname %s ",hostname);
1645 			gctx->sethostname = ifctx;
1646 		}
1647 	}
1648 
1649 	printf("\n");
1650 
1651 	if (ifctx->gotnetmask == 0) {
1652 		if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1653 			ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1654 		else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1655 			ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1656 		else
1657 			ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1658 	}
1659 	if (ifctx->gotgw == 0) {
1660 		/* Use proxyarp */
1661 		ifctx->gw.sin_addr.s_addr = ifctx->myaddr.sin_addr.s_addr;
1662 	}
1663 }
1664 
1665 void
1666 bootpc_init(void)
1667 {
1668 	struct bootpc_ifcontext *ifctx, *nctx;	/* Interface BOOTP contexts */
1669 	struct bootpc_globalcontext *gctx; 	/* Global BOOTP context */
1670 	struct ifnet *ifp;
1671 	int error;
1672 	struct nfsv3_diskless *nd;
1673 	struct proc *procp;
1674 
1675 	nd = &nfsv3_diskless;
1676 	procp = curproc;
1677 
1678 	/*
1679 	 * If already filled in, don't touch it here
1680 	 */
1681 	if (nfs_diskless_valid != 0)
1682 		return;
1683 
1684 	/*
1685 	 * Wait until arp entries can be handled.
1686 	 */
1687 	while (time_second == 0)
1688 		tsleep(&time_second, PZERO + 8, "arpkludge", 10);
1689 
1690 	gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK);
1691 	if (gctx == NULL)
1692 		panic("Failed to allocate bootp global context structure");
1693 
1694 	bzero(gctx, sizeof(*gctx));
1695 	gctx->xid = ~0xFFFF;
1696 	gctx->starttime = time_second;
1697 
1698 	ifctx = allocifctx(gctx);
1699 
1700 	/*
1701 	 * Find a network interface.
1702 	 */
1703 #ifdef BOOTP_WIRED_TO
1704 	printf("bootpc_init: wired to interface '%s'\n",
1705 	       __XSTRING(BOOTP_WIRED_TO));
1706 #endif
1707 	bzero(&ifctx->ireq, sizeof(ifctx->ireq));
1708 	for (ifp = TAILQ_FIRST(&ifnet);
1709 	     ifp != NULL;
1710 	     ifp = TAILQ_NEXT(ifp, if_link)) {
1711 		snprintf(ifctx->ireq.ifr_name, sizeof(ifctx->ireq.ifr_name),
1712 			 "%s%d", ifp->if_name, ifp->if_unit);
1713 #ifdef BOOTP_WIRED_TO
1714 		if (strcmp(ifctx->ireq.ifr_name,
1715 			   __XSTRING(BOOTP_WIRED_TO)) != 0)
1716 			continue;
1717 #else
1718 		if ((ifp->if_flags &
1719 		     (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1720 		    IFF_BROADCAST)
1721 			continue;
1722 #endif
1723 		if (gctx->interfaces != NULL)
1724 			gctx->lastinterface->next = ifctx;
1725 		else
1726 			gctx->interfaces = ifctx;
1727 		ifctx->ifp = ifp;
1728 		gctx->lastinterface = ifctx;
1729 		ifctx = allocifctx(gctx);
1730 	}
1731 	free(ifctx, M_TEMP);
1732 
1733 	if (gctx->interfaces == NULL) {
1734 #ifdef BOOTP_WIRED_TO
1735 		panic("bootpc_init: Could not find interface specified "
1736 		      "by BOOTP_WIRED_TO: "
1737 		      __XSTRING(BOOTP_WIRED_TO));
1738 #else
1739 		panic("bootpc_init: no suitable interface");
1740 #endif
1741 	}
1742 
1743 	gctx->gotrootpath = 0;
1744 	gctx->gotswappath = 0;
1745 	gctx->gotgw = 0;
1746 
1747 	for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1748 		bootpc_fakeup_interface(ifctx, gctx, procp);
1749 
1750 	for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1751 		bootpc_compose_query(ifctx, gctx, procp);
1752 
1753 	ifctx = gctx->interfaces;
1754 	error = bootpc_call(gctx, procp);
1755 
1756 	if (error != 0) {
1757 #ifdef BOOTP_NFSROOT
1758 		panic("BOOTP call failed");
1759 #else
1760 		printf("BOOTP call failed\n");
1761 #endif
1762 	}
1763 
1764 	mountopts(&nd->root_args, NULL);
1765 
1766 	mountopts(&nd->swap_args, NULL);
1767 
1768 	for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1769 		if (bootpc_ifctx_isresolved(ifctx) != 0)
1770 			bootpc_decode_reply(nd, ifctx, gctx);
1771 
1772 	if (gctx->gotswappath == 0)
1773 		nd->swap_nblks = 0;
1774 #ifdef BOOTP_NFSROOT
1775 	if (gctx->gotrootpath == 0)
1776 		panic("bootpc: No root path offered");
1777 #endif
1778 
1779 	for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
1780 		bootpc_adjust_interface(ifctx, gctx, procp);
1781 
1782 		soclose(ifctx->so);
1783 	}
1784 
1785 	for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1786 		if (ifctx->gotrootpath != 0)
1787 			break;
1788 	if (ifctx == NULL) {
1789 		for (ifctx = gctx->interfaces;
1790 		     ifctx != NULL;
1791 		     ifctx = ifctx->next)
1792 			if (bootpc_ifctx_isresolved(ifctx) != 0)
1793 				break;
1794 	}
1795 	if (ifctx == NULL)
1796 		goto out;
1797 
1798 	if (gctx->gotrootpath != 0) {
1799 
1800 		error = md_mount(&nd->root_saddr, nd->root_hostnam,
1801 				 nd->root_fh, &nd->root_fhsize,
1802 				 &nd->root_args, procp);
1803 		if (error != 0)
1804 			panic("nfs_boot: mountd root, error=%d", error);
1805 
1806 		if (gctx->gotswappath != 0) {
1807 
1808 			error = md_mount(&nd->swap_saddr,
1809 					 nd->swap_hostnam,
1810 					 nd->swap_fh, &nd->swap_fhsize,
1811 					 &nd->swap_args, procp);
1812 			if (error != 0)
1813 				panic("nfs_boot: mountd swap, error=%d",
1814 				      error);
1815 
1816 			error = md_lookup_swap(&nd->swap_saddr,
1817 					       gctx->lookup_path,
1818 					       nd->swap_fh, &nd->swap_fhsize,
1819 					       &nd->swap_args, procp);
1820 			if (error != 0)
1821 				panic("nfs_boot: lookup swap, error=%d",
1822 				      error);
1823 		}
1824 		nfs_diskless_valid = 3;
1825 	}
1826 
1827 	strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1828 	bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1829 	bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1830 	((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1831 		ifctx->myaddr.sin_addr.s_addr |
1832 		~ ifctx->netmask.sin_addr.s_addr;
1833 	bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1834 
1835 out:
1836 	for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = nctx) {
1837 		nctx = ifctx->next;
1838 		free(ifctx, M_TEMP);
1839 	}
1840 	free(gctx, M_TEMP);
1841 }
1842 
1843 
1844 /*
1845  * RPC: mountd/mount
1846  * Given a server pathname, get an NFS file handle.
1847  * Also, sets sin->sin_port to the NFS service port.
1848  */
1849 static int
1850 md_mount(struct sockaddr_in *mdsin,		/* mountd server address */
1851 	 char *path,
1852 	 u_char *fhp,
1853 	 int *fhsizep,
1854 	 struct nfs_args *args,
1855 	 struct proc *procp)
1856 {
1857 	struct mbuf *m;
1858 	int error;
1859 	int authunixok;
1860 	int authcount;
1861 	int authver;
1862 
1863 #ifdef BOOTP_NFSV3
1864 	/* First try NFS v3 */
1865 	/* Get port number for MOUNTD. */
1866 	error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1867 			     &mdsin->sin_port, procp);
1868 	if (error == 0) {
1869 		m = xdr_string_encode(path, strlen(path));
1870 
1871 		/* Do RPC to mountd. */
1872 		error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1873 				  RPCMNT_MOUNT, &m, NULL, procp);
1874 	}
1875 	if (error == 0) {
1876 		args->flags |= NFSMNT_NFSV3;
1877 	} else {
1878 #endif
1879 		/* Fallback to NFS v2 */
1880 
1881 		/* Get port number for MOUNTD. */
1882 		error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1883 				     &mdsin->sin_port, procp);
1884 		if (error != 0)
1885 			return error;
1886 
1887 		m = xdr_string_encode(path, strlen(path));
1888 
1889 		/* Do RPC to mountd. */
1890 		error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1891 				  RPCMNT_MOUNT, &m, NULL, procp);
1892 		if (error != 0)
1893 			return error;	/* message already freed */
1894 
1895 #ifdef BOOTP_NFSV3
1896 	}
1897 #endif
1898 
1899 	if (xdr_int_decode(&m, &error) != 0 || error != 0)
1900 		goto bad;
1901 
1902 	if ((args->flags & NFSMNT_NFSV3) != 0) {
1903 		if (xdr_int_decode(&m, fhsizep) != 0 ||
1904 		    *fhsizep > NFSX_V3FHMAX ||
1905 		    *fhsizep <= 0)
1906 			goto bad;
1907 	} else
1908 		*fhsizep = NFSX_V2FH;
1909 
1910 	if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
1911 		goto bad;
1912 
1913 	if (args->flags & NFSMNT_NFSV3) {
1914 		if (xdr_int_decode(&m, &authcount) != 0)
1915 			goto bad;
1916 		authunixok = 0;
1917 		if (authcount < 0 || authcount > 100)
1918 			goto bad;
1919 		while (authcount > 0) {
1920 			if (xdr_int_decode(&m, &authver) != 0)
1921 				goto bad;
1922 			if (authver == RPCAUTH_UNIX)
1923 				authunixok = 1;
1924 			authcount--;
1925 		}
1926 		if (authunixok == 0)
1927 			goto bad;
1928 	}
1929 
1930 	/* Set port number for NFS use. */
1931 	error = krpc_portmap(mdsin, NFS_PROG,
1932 			     (args->flags &
1933 			      NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
1934 			     &mdsin->sin_port, procp);
1935 
1936 	goto out;
1937 
1938 bad:
1939 	error = EBADRPC;
1940 
1941 out:
1942 	m_freem(m);
1943 	return error;
1944 }
1945 
1946 
1947 static int
1948 md_lookup_swap(struct sockaddr_in *mdsin,	/* mountd server address */
1949 	       char *path,
1950 	       u_char *fhp,
1951 	       int *fhsizep,
1952 	       struct nfs_args *args,
1953 	       struct proc *procp)
1954 {
1955 	struct mbuf *m;
1956 	int error;
1957 	int size = -1;
1958 	int attribs_present;
1959 	int status;
1960 	union {
1961 		u_int32_t v2[17];
1962 		u_int32_t v3[21];
1963 	} fattribs;
1964 
1965 	m = m_get(M_TRYWAIT,MT_DATA);
1966 	if (m == NULL)
1967 	  	return ENOBUFS;
1968 
1969 	if ((args->flags & NFSMNT_NFSV3) != 0) {
1970 		*mtod(m, u_int32_t *) = txdr_unsigned(*fhsizep);
1971 		bcopy(fhp, mtod(m, u_char *) + sizeof(u_int32_t), *fhsizep);
1972 		m->m_len = *fhsizep + sizeof(u_int32_t);
1973 	} else {
1974 		bcopy(fhp, mtod(m, u_char *), NFSX_V2FH);
1975 		m->m_len = NFSX_V2FH;
1976 	}
1977 
1978 	m->m_next = xdr_string_encode(path, strlen(path));
1979 	if (m->m_next == NULL) {
1980 		error = ENOBUFS;
1981 		goto out;
1982 	}
1983 
1984 	/* Do RPC to nfsd. */
1985 	if ((args->flags & NFSMNT_NFSV3) != 0)
1986 		error = krpc_call(mdsin, NFS_PROG, NFS_VER3,
1987 				  NFSPROC_LOOKUP, &m, NULL, procp);
1988 	else
1989 		error = krpc_call(mdsin, NFS_PROG, NFS_VER2,
1990 				  NFSV2PROC_LOOKUP, &m, NULL, procp);
1991 	if (error != 0)
1992 		return error;	/* message already freed */
1993 
1994 	if (xdr_int_decode(&m, &status) != 0)
1995 		goto bad;
1996 	if (status != 0) {
1997 		error = ENOENT;
1998 		goto out;
1999 	}
2000 
2001 	if ((args->flags & NFSMNT_NFSV3) != 0) {
2002 		if (xdr_int_decode(&m, fhsizep) != 0 ||
2003 		    *fhsizep > NFSX_V3FHMAX ||
2004 		    *fhsizep <= 0)
2005 			goto bad;
2006 	} else
2007 		*fhsizep = NFSX_V2FH;
2008 
2009 	if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
2010 		goto bad;
2011 
2012 	if ((args->flags & NFSMNT_NFSV3) != 0) {
2013 		if (xdr_int_decode(&m, &attribs_present) != 0)
2014 			goto bad;
2015 		if (attribs_present != 0) {
2016 			if (xdr_opaque_decode(&m, (u_char *) &fattribs.v3,
2017 					      sizeof(u_int32_t) * 21) != 0)
2018 				goto bad;
2019 			size = fxdr_unsigned(u_int32_t, fattribs.v3[6]);
2020 		}
2021 	} else {
2022 		if (xdr_opaque_decode(&m,(u_char *) &fattribs.v2,
2023 				      sizeof(u_int32_t) * 17) != 0)
2024 			goto bad;
2025 		size = fxdr_unsigned(u_int32_t, fattribs.v2[5]);
2026 	}
2027 
2028 	if (nfsv3_diskless.swap_nblks == 0 && size != -1) {
2029 		nfsv3_diskless.swap_nblks = size / 1024;
2030 		printf("md_lookup_swap: Swap size is %d KB\n",
2031 		       nfsv3_diskless.swap_nblks);
2032 	}
2033 
2034 	goto out;
2035 
2036 bad:
2037 	error = EBADRPC;
2038 
2039 out:
2040 	m_freem(m);
2041 	return error;
2042 }
2043