xref: /dragonfly/sys/vfs/nfs/bootp_subr.c (revision e65bc1c3)
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  * nfs/krpc_subr.c
39  * $NetBSD: krpc_subr.c,v 1.10 1995/08/08 20:43:43 gwr Exp $
40  * $FreeBSD: src/sys/nfs/bootp_subr.c,v 1.20.2.9 2003/04/24 16:51:08 ambrisko Exp $
41  */
42 
43 #include "opt_bootp.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/sockio.h>
49 #include <sys/proc.h>
50 #include <sys/malloc.h>
51 #include <sys/mount.h>
52 #include <sys/mbuf.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/sysctl.h>
56 #include <sys/uio.h>
57 #include <sys/fcntl.h>
58 
59 #include <net/if.h>
60 #include <net/route.h>
61 
62 #include <netinet/in.h>
63 #include <net/if_types.h>
64 #include <net/if_dl.h>
65 
66 #include "rpcv2.h"
67 #include "nfsproto.h"
68 #include "nfs.h"
69 #include "nfsdiskless.h"
70 #include "krpc.h"
71 #include "xdr_subs.h"
72 #include "nfsmountrpc.h"
73 
74 #define BOOTP_MIN_LEN		300	/* Minimum size of bootp udp packet */
75 
76 #ifndef BOOTP_SETTLE_DELAY
77 #define BOOTP_SETTLE_DELAY 3
78 #endif
79 
80 /*
81  * What is the longest we will wait before re-sending a request?
82  * Note this is also the frequency of "RPC timeout" messages.
83  * The re-send loop count sup linearly to this maximum, so the
84  * first complaint will happen after (1+2+3+4+5)=15 seconds.
85  */
86 #define	MAX_RESEND_DELAY 5	/* seconds */
87 
88 /* Definitions from RFC951 */
89 struct bootp_packet {
90 	u_int8_t op;
91 	u_int8_t htype;
92 	u_int8_t hlen;
93 	u_int8_t hops;
94 	u_int32_t xid;
95 	u_int16_t secs;
96 	u_int16_t flags;
97 	struct in_addr ciaddr;
98 	struct in_addr yiaddr;
99 	struct in_addr siaddr;
100 	struct in_addr giaddr;
101 	unsigned char chaddr[16];
102 	char sname[64];
103 	char file[128];
104 	unsigned char vend[1222];
105 };
106 
107 struct bootpc_ifcontext {
108 	struct bootpc_ifcontext *next;
109 	struct bootp_packet call;
110 	struct bootp_packet reply;
111 	int replylen;
112 	int overload;
113 	struct socket *so;
114 	struct ifreq ireq;
115 	struct ifnet *ifp;
116 	struct sockaddr_dl *sdl;
117 	struct sockaddr_in myaddr;
118 	struct sockaddr_in netmask;
119 	struct sockaddr_in gw;
120 	struct sockaddr_in broadcast;	/* Different for each interface */
121 	int gotgw;
122 	int gotnetmask;
123 	int gotrootpath;
124 	int outstanding;
125 	int sentmsg;
126 	u_int32_t xid;
127 	enum {
128 		IF_BOOTP_UNRESOLVED,
129 		IF_BOOTP_RESOLVED,
130 		IF_BOOTP_FAILED,
131 		IF_DHCP_UNRESOLVED,
132 		IF_DHCP_OFFERED,
133 		IF_DHCP_RESOLVED,
134 		IF_DHCP_FAILED,
135 	} state;
136 	int dhcpquerytype;		/* dhcp type sent */
137 	struct in_addr dhcpserver;
138 	int gotdhcpserver;
139 };
140 
141 #define TAG_MAXLEN 1024
142 struct bootpc_tagcontext {
143 	char buf[TAG_MAXLEN + 1];
144 	int overload;
145 	int badopt;
146 	int badtag;
147 	int foundopt;
148 	int taglen;
149 };
150 
151 struct bootpc_globalcontext {
152 	struct bootpc_ifcontext *interfaces;
153 	struct bootpc_ifcontext *lastinterface;
154 	u_int32_t xid;
155 	int gotrootpath;
156 	int gotswappath;
157 	int gotgw;
158 	int ifnum;
159 	int secs;
160 	int starttime;
161 	struct bootp_packet reply;
162 	int replylen;
163 	struct bootpc_ifcontext *setswapfs;
164 	struct bootpc_ifcontext *setrootfs;
165 	struct bootpc_ifcontext *sethostname;
166 	char lookup_path[24];
167 	struct bootpc_tagcontext tmptag;
168 	struct bootpc_tagcontext tag;
169 };
170 
171 #define IPPORT_BOOTPC 68
172 #define IPPORT_BOOTPS 67
173 
174 #define BOOTP_REQUEST 1
175 #define BOOTP_REPLY 2
176 
177 /* Common tags */
178 #define TAG_PAD		  0  /* Pad option, implicit length 1 */
179 #define TAG_SUBNETMASK	  1  /* RFC 950 subnet mask */
180 #define TAG_ROUTERS	  3  /* Routers (in order of preference) */
181 #define TAG_HOSTNAME	 12  /* Client host name */
182 #define TAG_ROOT	 17  /* Root path */
183 
184 /* DHCP specific tags */
185 #define TAG_OVERLOAD	 52  /* Option Overload */
186 #define TAG_MAXMSGSIZE   57  /* Maximum DHCP Message Size */
187 
188 #define TAG_END		255  /* End Option (i.e. no more options) */
189 
190 /* Overload values */
191 #define OVERLOAD_FILE     1
192 #define OVERLOAD_SNAME    2
193 
194 /* Site specific tags: */
195 #define TAG_SWAP	128
196 #define TAG_SWAPSIZE	129
197 #define TAG_ROOTOPTS	130
198 #define TAG_SWAPOPTS	131
199 #define TAG_COOKIE	134	/* ascii info for userland, exported via sysctl */
200 
201 #define TAG_DHCP_MSGTYPE 53
202 #define TAG_DHCP_REQ_ADDR 50
203 #define TAG_DHCP_SERVERID 54
204 #define TAG_DHCP_LEASETIME 51
205 
206 #define TAG_VENDOR_INDENTIFIER 60
207 
208 #define DHCP_NOMSG    0
209 #define DHCP_DISCOVER 1
210 #define DHCP_OFFER    2
211 #define DHCP_REQUEST  3
212 #define DHCP_ACK      5
213 
214 static char bootp_cookie[128];
215 SYSCTL_STRING(_kern, OID_AUTO, bootp_cookie, CTLFLAG_RD,
216 	bootp_cookie, 0, "Cookie (T134) supplied by bootp server");
217 
218 /* mountd RPC */
219 static void print_in_addr(struct in_addr addr);
220 static void print_sin_addr(struct sockaddr_in *addr);
221 static void clear_sinaddr(struct sockaddr_in *sin);
222 static
223 struct bootpc_ifcontext *allocifctx(struct bootpc_globalcontext *gctx);
224 static void bootpc_compose_query(struct bootpc_ifcontext *ifctx,
225 				 struct bootpc_globalcontext *gctx,
226 				 struct thread *td);
227 static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx,
228 				 struct bootp_packet *bp, int len, int tag);
229 static void bootpc_tag_helper(struct bootpc_tagcontext *tctx,
230 			      unsigned char *start, int len, int tag);
231 
232 #ifdef BOOTP_DEBUG
233 void bootpboot_p_sa(struct sockaddr *sa,struct sockaddr *ma);
234 void bootpboot_p_ma(struct sockaddr *ma);
235 void bootpboot_p_rtentry(struct rtentry *rt);
236 void bootpboot_p_tree(struct radix_node *rn);
237 void bootpboot_p_rtlist(void);
238 void bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa);
239 void bootpboot_p_iflist(void);
240 #endif
241 
242 static int  bootpc_call(struct bootpc_globalcontext *gctx,
243 			struct thread *td);
244 
245 static int bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
246 				   struct bootpc_globalcontext *gctx,
247 				   struct thread *td);
248 
249 static int bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
250 				   struct bootpc_globalcontext *gctx,
251 				   struct thread *td);
252 
253 static void bootpc_decode_reply(struct nfsv3_diskless *nd,
254 				struct bootpc_ifcontext *ifctx,
255 				struct bootpc_globalcontext *gctx);
256 
257 static int bootpc_received(struct bootpc_globalcontext *gctx,
258 			   struct bootpc_ifcontext *ifctx);
259 
260 static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
261 static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
262 static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
263 
264 void bootpc_init(void);
265 
266 /*
267  * In order to have multiple active interfaces with address 0.0.0.0
268  * and be able to send data to a selected interface, we perform
269  * some tricks:
270  *
271  *  - The 'broadcast' address is different for each interface.
272  *
273  *  - We temporarily add routing pointing 255.255.255.255 to the
274  *    selected interface broadcast address, thus the packet sent
275  *    goes to that interface.
276  */
277 
278 #ifdef BOOTP_DEBUG
279 void
280 bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma)
281 {
282 	if (sa == NULL) {
283 		kprintf("(sockaddr *) <null>");
284 		return;
285 	}
286 	switch (sa->sa_family) {
287 	case AF_INET:
288 	{
289 		struct sockaddr_in *sin;
290 
291 		sin = (struct sockaddr_in *) sa;
292 		kprintf("inet ");
293 		print_sin_addr(sin);
294 		if (ma != NULL) {
295 			sin = (struct sockaddr_in *) ma;
296 			kprintf(" mask ");
297 			print_sin_addr(sin);
298 		}
299 	}
300 	break;
301 	case AF_LINK:
302 	{
303 		struct sockaddr_dl *sli;
304 		int i;
305 
306 		sli = (struct sockaddr_dl *) sa;
307 		kprintf("link %.*s ", sli->sdl_nlen, sli->sdl_data);
308 		for (i = 0; i < sli->sdl_alen; i++) {
309 			if (i > 0)
310 				kprintf(":");
311 			kprintf("%x", ((unsigned char *) LLADDR(sli))[i]);
312 		}
313 	}
314 	break;
315 	default:
316 		kprintf("af%d", sa->sa_family);
317 	}
318 }
319 
320 
321 void
322 bootpboot_p_ma(struct sockaddr *ma)
323 {
324 	if (ma == NULL) {
325 		kprintf("<null>");
326 		return;
327 	}
328 	kprintf("%x", *(int *)ma);
329 }
330 
331 
332 void
333 bootpboot_p_rtentry(struct rtentry *rt)
334 {
335 	bootpboot_p_sa(rt_key(rt), rt_mask(rt));
336 	kprintf(" ");
337 	bootpboot_p_ma(rt->rt_genmask);
338 	kprintf(" ");
339 	bootpboot_p_sa(rt->rt_gateway, NULL);
340 	kprintf(" ");
341 	kprintf("flags %x", (unsigned short) rt->rt_flags);
342 	kprintf(" %d", (int) rt->rt_rmx.rmx_expire);
343 	kprintf(" %s\n", if_name(rt->rt_ifp));
344 }
345 
346 
347 void
348 bootpboot_p_tree(struct radix_node *rn)
349 {
350 	while (rn != NULL) {
351 		if (rn->rn_bit < 0) {
352 			if ((rn->rn_flags & RNF_ROOT) != 0) {
353 			} else {
354 				bootpboot_p_rtentry((struct rtentry *) rn);
355 			}
356 			rn = rn->rn_dupedkey;
357 		} else {
358 			bootpboot_p_tree(rn->rn_left);
359 			bootpboot_p_tree(rn->rn_right);
360 			return;
361 		}
362 	}
363 }
364 
365 
366 void
367 bootpboot_p_rtlist(void)
368 {
369 	kprintf("Routing table:\n");
370 	bootpboot_p_tree(rt_tables[AF_INET]->rnh_treetop);
371 }
372 
373 
374 void
375 bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
376 {
377 	kprintf("%s flags %x, addr ",
378 	       if_name(ifp),
379 	       (unsigned short) ifp->if_flags);
380 	print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
381 	kprintf(", broadcast ");
382 	print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
383 	kprintf(", netmask ");
384 	print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
385 	kprintf("\n");
386 }
387 
388 
389 void
390 bootpboot_p_iflist(void)
391 {
392 	struct ifnet *ifp;
393 	struct ifaddr_container *ifac;
394 
395 	kprintf("Interface list:\n");
396 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
397 		TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
398 			struct ifaddr *ifa = ifac->ifa;
399 
400 			if (ifa->ifa_addr->sa_family == AF_INET)
401 				bootpboot_p_if(ifp, ifa);
402 		}
403 	}
404 }
405 #endif /* defined(BOOTP_DEBUG) */
406 
407 
408 static void
409 clear_sinaddr(struct sockaddr_in *sin)
410 {
411 	bzero(sin, sizeof(*sin));
412 	sin->sin_len = sizeof(*sin);
413 	sin->sin_family = AF_INET;
414 	sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
415 	sin->sin_port = 0;
416 }
417 
418 
419 static struct bootpc_ifcontext *
420 allocifctx(struct bootpc_globalcontext *gctx)
421 {
422 	struct bootpc_ifcontext *ifctx;
423 	ifctx = (struct bootpc_ifcontext *) kmalloc(sizeof(*ifctx),
424 						   M_TEMP, M_WAITOK);
425 	bzero(ifctx, sizeof(*ifctx));
426 	ifctx->xid = gctx->xid;
427 #ifdef BOOTP_NO_DHCP
428 	ifctx->state = IF_BOOTP_UNRESOLVED;
429 #else
430 	ifctx->state = IF_DHCP_UNRESOLVED;
431 #endif
432 	gctx->xid += 0x100;
433 	return ifctx;
434 }
435 
436 
437 static __inline int
438 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
439 {
440 	if (ifctx->state == IF_BOOTP_RESOLVED ||
441 	    ifctx->state == IF_DHCP_RESOLVED)
442 		return 1;
443 	return 0;
444 }
445 
446 
447 static __inline int
448 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
449 {
450 	if (ifctx->state == IF_BOOTP_UNRESOLVED ||
451 	    ifctx->state == IF_DHCP_UNRESOLVED)
452 		return 1;
453 	return 0;
454 }
455 
456 
457 static __inline int
458 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
459 {
460 	if (ifctx->state == IF_BOOTP_FAILED ||
461 	    ifctx->state == IF_DHCP_FAILED)
462 		return 1;
463 	return 0;
464 }
465 
466 
467 static int
468 bootpc_received(struct bootpc_globalcontext *gctx,
469 		struct bootpc_ifcontext *ifctx)
470 {
471 	unsigned char dhcpreplytype;
472 	char *p;
473 	/*
474 	 * Need timeout for fallback to less
475 	 * desirable alternative.
476 	 */
477 
478 
479 	/* This call used for the side effect (badopt flag) */
480 	(void) bootpc_tag(&gctx->tmptag, &gctx->reply,
481 			  gctx->replylen,
482 			  TAG_END);
483 
484 	/* If packet is invalid, ignore it */
485 	if (gctx->tmptag.badopt != 0)
486 		return 0;
487 
488 	p = bootpc_tag(&gctx->tmptag, &gctx->reply,
489 		       gctx->replylen, TAG_DHCP_MSGTYPE);
490 	if (p != NULL)
491 		dhcpreplytype = *p;
492 	else
493 		dhcpreplytype = DHCP_NOMSG;
494 
495 	switch (ifctx->dhcpquerytype) {
496 	case DHCP_DISCOVER:
497 		if (dhcpreplytype != DHCP_OFFER 	/* Normal DHCP offer */
498 #ifndef BOOTP_FORCE_DHCP
499 		    && dhcpreplytype != DHCP_NOMSG	/* Fallback to BOOTP */
500 #endif
501 			)
502 			return 0;
503 		break;
504 	case DHCP_REQUEST:
505 		if (dhcpreplytype != DHCP_ACK)
506 			return 0;
507 		/* fall through */
508 	case DHCP_NOMSG:
509 		break;
510 	}
511 
512 
513 	/* Ignore packet unless it gives us a root tag we didn't have */
514 
515 	if ((ifctx->state == IF_BOOTP_RESOLVED ||
516 	     (ifctx->dhcpquerytype == DHCP_DISCOVER &&
517 	      (ifctx->state == IF_DHCP_OFFERED ||
518 	       ifctx->state == IF_DHCP_RESOLVED))) &&
519 	    (bootpc_tag(&gctx->tmptag, &ifctx->reply,
520 			ifctx->replylen,
521 			TAG_ROOT) != NULL ||
522 	     bootpc_tag(&gctx->tmptag, &gctx->reply,
523 			gctx->replylen,
524 			TAG_ROOT) == NULL))
525 		return 0;
526 
527 	bcopy(&gctx->reply,
528 	      &ifctx->reply,
529 	      gctx->replylen);
530 	ifctx->replylen = gctx->replylen;
531 
532 	/* XXX: Only reset if 'perfect' response */
533 	if (ifctx->state == IF_BOOTP_UNRESOLVED)
534 		ifctx->state = IF_BOOTP_RESOLVED;
535 	else if (ifctx->state == IF_DHCP_UNRESOLVED &&
536 		 ifctx->dhcpquerytype == DHCP_DISCOVER) {
537 		if (dhcpreplytype == DHCP_OFFER)
538 			ifctx->state = IF_DHCP_OFFERED;
539 		else
540 			ifctx->state = IF_BOOTP_RESOLVED;	/* Fallback */
541 	} else if (ifctx->state == IF_DHCP_OFFERED &&
542 		   ifctx->dhcpquerytype == DHCP_REQUEST)
543 		ifctx->state = IF_DHCP_RESOLVED;
544 
545 
546 	if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
547 	    ifctx->state != IF_BOOTP_RESOLVED) {
548 		p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
549 			       ifctx->replylen, TAG_DHCP_SERVERID);
550 		if (p != NULL && gctx->tmptag.taglen == 4) {
551 			memcpy(&ifctx->dhcpserver, p, 4);
552 			ifctx->gotdhcpserver = 1;
553 		} else
554 			ifctx->gotdhcpserver = 0;
555 		return 1;
556 	}
557 
558 	ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
559 					 ifctx->replylen,
560 					 TAG_ROOT) != NULL);
561 	ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
562 				   ifctx->replylen,
563 				   TAG_ROUTERS) != NULL);
564 	ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
565 					ifctx->replylen,
566 					TAG_SUBNETMASK) != NULL);
567 	return 1;
568 }
569 
570 static int
571 bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
572 {
573 	struct socket *so;
574 	struct sockaddr_in *sin, dst;
575 	struct uio auio;
576 	struct sockopt sopt;
577 	struct iovec aio;
578 	int error, on, rcvflg, timo, len;
579 	time_t atimo;
580 	time_t rtimo;
581 	struct timeval tv;
582 	struct bootpc_ifcontext *ifctx;
583 	int outstanding;
584 	int gotrootpath;
585 	int retry;
586 	const char *s;
587 	char hexstr[64];
588 
589 	/*
590 	 * Create socket and set its recieve timeout.
591 	 */
592 	error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td);
593 	if (error != 0)
594 		goto out;
595 
596 	tv.tv_sec = 1;
597 	tv.tv_usec = 0;
598 	bzero(&sopt, sizeof(sopt));
599 	sopt.sopt_level = SOL_SOCKET;
600 	sopt.sopt_name = SO_RCVTIMEO;
601 	sopt.sopt_val = &tv;
602 	sopt.sopt_valsize = sizeof tv;
603 
604 	error = sosetopt(so, &sopt);
605 	if (error != 0)
606 		goto out;
607 
608 	/*
609 	 * Enable broadcast.
610 	 */
611 	on = 1;
612 	sopt.sopt_name = SO_BROADCAST;
613 	sopt.sopt_val = &on;
614 	sopt.sopt_valsize = sizeof on;
615 
616 	error = sosetopt(so, &sopt);
617 	if (error != 0)
618 		goto out;
619 
620 	/*
621 	 * Disable routing.
622 	 */
623 
624 	on = 1;
625 	sopt.sopt_name = SO_DONTROUTE;
626 	sopt.sopt_val = &on;
627 	sopt.sopt_valsize = sizeof on;
628 
629 	error = sosetopt(so, &sopt);
630 	if (error != 0)
631 		goto out;
632 
633 	/*
634 	 * Bind the local endpoint to a bootp client port.
635 	 */
636 	sin = &dst;
637 	clear_sinaddr(sin);
638 	sin->sin_port = htons(IPPORT_BOOTPC);
639 	error = sobind(so, (struct sockaddr *)sin, td);
640 	if (error != 0) {
641 		kprintf("bind failed\n");
642 		goto out;
643 	}
644 
645 	/*
646 	 * Setup socket address for the server.
647 	 */
648 	sin = &dst;
649 	clear_sinaddr(sin);
650 	sin->sin_addr.s_addr = INADDR_BROADCAST;
651 	sin->sin_port = htons(IPPORT_BOOTPS);
652 
653 	/*
654 	 * Send it, repeatedly, until a reply is received,
655 	 * but delay each re-send by an increasing amount.
656 	 * If the delay hits the maximum, start complaining.
657 	 */
658 	timo = 0;
659 	rtimo = 0;
660 	for (;;) {
661 
662 		outstanding = 0;
663 		gotrootpath = 0;
664 
665 		for (ifctx = gctx->interfaces;
666 		     ifctx != NULL;
667 		     ifctx = ifctx->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 		for (ifctx = gctx->interfaces;
676 		     ifctx != NULL;
677 		     ifctx = ifctx->next) {
678 			ifctx->outstanding = 0;
679 			if (bootpc_ifctx_isresolved(ifctx)  != 0 &&
680 			    gotrootpath != 0) {
681 				continue;
682 			}
683 			if (bootpc_ifctx_isfailed(ifctx) != 0)
684 				continue;
685 
686 			outstanding++;
687 			ifctx->outstanding = 1;
688 
689 			/* Proceed to next step in DHCP negotiation */
690 			if ((ifctx->state == IF_DHCP_OFFERED &&
691 			     ifctx->dhcpquerytype != DHCP_REQUEST) ||
692 			    (ifctx->state == IF_DHCP_UNRESOLVED &&
693 			     ifctx->dhcpquerytype != DHCP_DISCOVER) ||
694 			    (ifctx->state == IF_BOOTP_UNRESOLVED &&
695 			     ifctx->dhcpquerytype != DHCP_NOMSG)) {
696 				ifctx->sentmsg = 0;
697 				bootpc_compose_query(ifctx, gctx, td);
698 			}
699 
700 			/* Send BOOTP request (or re-send). */
701 
702 			if (ifctx->sentmsg == 0) {
703 				switch(ifctx->dhcpquerytype) {
704 				case DHCP_DISCOVER:
705 					s = "DHCP Discover";
706 					break;
707 				case DHCP_REQUEST:
708 					s = "DHCP Request";
709 					break;
710 				case DHCP_NOMSG:
711 				default:
712 					s = "BOOTP Query";
713 					break;
714 				}
715 				hexncpy((u_char *)LLADDR(ifctx->sdl),
716 				    ifctx->sdl->sdl_alen, hexstr,
717 				    HEX_NCPYLEN(ifctx->sdl->sdl_alen), ":");
718 				kprintf("Sending %s packet from "
719 				       "interface %s (%s)\n",
720 				       s, ifctx->ireq.ifr_name,
721 				       hexstr);
722 				ifctx->sentmsg = 1;
723 			}
724 
725 			aio.iov_base = (caddr_t) &ifctx->call;
726 			aio.iov_len = sizeof(ifctx->call);
727 
728 			auio.uio_iov = &aio;
729 			auio.uio_iovcnt = 1;
730 			auio.uio_segflg = UIO_SYSSPACE;
731 			auio.uio_rw = UIO_WRITE;
732 			auio.uio_offset = 0;
733 			auio.uio_resid = sizeof(ifctx->call);
734 			auio.uio_td = td;
735 
736 			/* Set netmask to 0.0.0.0 */
737 
738 			sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
739 			clear_sinaddr(sin);
740 			error = ifioctl(ifctx->so, SIOCSIFNETMASK,
741 					(caddr_t) &ifctx->ireq, proc0.p_ucred);
742 			if (error != 0)
743 				panic("bootpc_call:"
744 				      "set if netmask, error=%d",
745 				      error);
746 
747 			error = sosend(so, (struct sockaddr *) &dst,
748 				       &auio, NULL, NULL, 0, td);
749 			if (error != 0) {
750 				kprintf("bootpc_call: sosend: %d state %08x\n",
751 				       error, (int) so->so_state);
752 			}
753 
754 			/* XXX: Is this needed ? */
755 			tsleep(&error, 0, "bootpw", 10);
756 
757 			/* Set netmask to 255.0.0.0 */
758 
759 			sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
760 			clear_sinaddr(sin);
761 			sin->sin_addr.s_addr = htonl(0xff000000u);
762 			error = ifioctl(ifctx->so, SIOCSIFNETMASK,
763 					(caddr_t) &ifctx->ireq, proc0.p_ucred);
764 			if (error != 0)
765 				panic("bootpc_call:"
766 				      "set if netmask, error=%d",
767 				      error);
768 
769 		}
770 
771 		if (outstanding == 0 &&
772 		    (rtimo == 0 || time_second >= rtimo)) {
773 			error = 0;
774 			goto gotreply;
775 		}
776 
777 		/* Determine new timeout. */
778 		if (timo < MAX_RESEND_DELAY)
779 			timo++;
780 		else {
781 			kprintf("DHCP/BOOTP timeout for server ");
782 			print_sin_addr(&dst);
783 			kprintf("\n");
784 		}
785 
786 		/*
787 		 * Wait for up to timo seconds for a reply.
788 		 * The socket receive timeout was set to 1 second.
789 		 */
790 		atimo = timo + time_second;
791 		while (time_second < atimo) {
792 			aio.iov_base = (caddr_t) &gctx->reply;
793 			aio.iov_len = sizeof(gctx->reply);
794 
795 			auio.uio_iov = &aio;
796 			auio.uio_iovcnt = 1;
797 			auio.uio_segflg = UIO_SYSSPACE;
798 			auio.uio_rw = UIO_READ;
799 			auio.uio_offset = 0;
800 			auio.uio_resid = sizeof(gctx->reply);
801 			auio.uio_td = td;
802 
803 			rcvflg = 0;
804 			error = soreceive(so, NULL, &auio,
805 					  NULL, NULL, &rcvflg);
806 			gctx->secs = time_second - gctx->starttime;
807 			for (ifctx = gctx->interfaces;
808 			     ifctx != NULL;
809 			     ifctx = ifctx->next) {
810 				if (bootpc_ifctx_isresolved(ifctx) != 0 ||
811 				    bootpc_ifctx_isfailed(ifctx) != 0)
812 					continue;
813 
814 				ifctx->call.secs = htons(gctx->secs);
815 			}
816 			if (error == EWOULDBLOCK)
817 				continue;
818 			if (error != 0)
819 				goto out;
820 			len = sizeof(gctx->reply) - auio.uio_resid;
821 
822 			/* Do we have the required number of bytes ? */
823 			if (len < BOOTP_MIN_LEN)
824 				continue;
825 			gctx->replylen = len;
826 
827 			/* Is it a reply? */
828 			if (gctx->reply.op != BOOTP_REPLY)
829 				continue;
830 
831 			/* Is this an answer to our query */
832 			for (ifctx = gctx->interfaces;
833 			     ifctx != NULL;
834 			     ifctx = ifctx->next) {
835 				if (gctx->reply.xid != ifctx->call.xid)
836 					continue;
837 
838 				/* Same HW address size ? */
839 				if (gctx->reply.hlen != ifctx->call.hlen)
840 					continue;
841 
842 				/* Correct HW address ? */
843 				if (bcmp(gctx->reply.chaddr,
844 					 ifctx->call.chaddr,
845 					 ifctx->call.hlen) != 0)
846 					continue;
847 
848 				break;
849 			}
850 
851 			if (ifctx != NULL) {
852 				s =  bootpc_tag(&gctx->tmptag,
853 						&gctx->reply,
854 						gctx->replylen,
855 						TAG_DHCP_MSGTYPE);
856 				if (s != NULL) {
857 					switch (*s) {
858 					case DHCP_OFFER:
859 						s = "DHCP Offer";
860 						break;
861 					case DHCP_ACK:
862 						s = "DHCP Ack";
863 						break;
864 					default:
865 						s = "DHCP (unexpected)";
866 						break;
867 					}
868 				} else
869 					s = "BOOTP Reply";
870 
871 				kprintf("Received %s packet"
872 				       " on %s from ",
873 				       s,
874 				       ifctx->ireq.ifr_name);
875 				print_in_addr(gctx->reply.siaddr);
876 				if (gctx->reply.giaddr.s_addr !=
877 				    htonl(INADDR_ANY)) {
878 					kprintf(" via ");
879 					print_in_addr(gctx->reply.giaddr);
880 				}
881 				if (bootpc_received(gctx, ifctx) != 0) {
882 					kprintf(" (accepted)");
883 					if (ifctx->outstanding) {
884 						ifctx->outstanding = 0;
885 						outstanding--;
886 					}
887 					/* Network settle delay */
888 					if (outstanding == 0)
889 						atimo = time_second +
890 							BOOTP_SETTLE_DELAY;
891 				} else
892 					kprintf(" (ignored)");
893 				if (ifctx->gotrootpath) {
894 					gotrootpath = 1;
895 					rtimo = time_second +
896 						BOOTP_SETTLE_DELAY;
897 					kprintf(" (got root path)");
898 				} else
899 					kprintf(" (no root path)");
900 				kprintf("\n");
901 			}
902 		} /* while secs */
903 #ifdef BOOTP_TIMEOUT
904 		if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
905 			break;
906 #endif
907 		/* Force a retry if halfway in DHCP negotiation */
908 		retry = 0;
909 		for (ifctx = gctx->interfaces; ifctx != NULL;
910 		     ifctx = ifctx->next) {
911 			if (ifctx->state == IF_DHCP_OFFERED) {
912 				if (ifctx->dhcpquerytype == DHCP_DISCOVER)
913 					retry = 1;
914 				else
915 					ifctx->state = IF_DHCP_UNRESOLVED;
916 			}
917 		}
918 
919 		if (retry != 0)
920 			continue;
921 
922 		if (gotrootpath != 0) {
923 			gctx->gotrootpath = gotrootpath;
924 			if (rtimo != 0 && time_second >= rtimo)
925 				break;
926 		}
927 	} /* forever send/receive */
928 
929 	/*
930 	 * XXX: These are errors of varying seriousness being silently
931 	 * ignored
932 	 */
933 
934 	for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
935 		if (bootpc_ifctx_isresolved(ifctx) == 0) {
936 			kprintf("%s timeout for interface %s\n",
937 			       ifctx->dhcpquerytype != DHCP_NOMSG ?
938 			       "DHCP" : "BOOTP",
939 			       ifctx->ireq.ifr_name);
940 		}
941 	}
942 	if (gctx->gotrootpath != 0) {
943 #if 0
944 		kprintf("Got a root path, ignoring remaining timeout\n");
945 #endif
946 		error = 0;
947 		goto out;
948 	}
949 #ifndef BOOTP_NFSROOT
950 	for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
951 		if (bootpc_ifctx_isresolved(ifctx) != 0) {
952 			error = 0;
953 			goto out;
954 		}
955 	}
956 #endif
957 	error = ETIMEDOUT;
958 	goto out;
959 
960 gotreply:
961 out:
962 	soclose(so, FNONBLOCK);
963 	return error;
964 }
965 
966 
967 static int
968 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
969 			struct bootpc_globalcontext *gctx,
970 			struct thread *td)
971 {
972 	struct ifaddr_container *ifac;
973 	struct sockaddr_in *sin;
974 	int error;
975 
976 	struct ifreq *ireq;
977 	struct socket *so;
978 	struct sockaddr_dl *sdl;
979 
980 	error = socreate(AF_INET, &ifctx->so, SOCK_DGRAM, 0, td);
981 	if (error != 0)
982 		panic("nfs_boot: socreate, error=%d", error);
983 
984 	ireq = &ifctx->ireq;
985 	so = ifctx->so;
986 
987 	/*
988 	 * Bring up the interface.
989 	 *
990 	 * Get the old interface flags and or IFF_UP into them; if
991 	 * IFF_UP set blindly, interface selection can be clobbered.
992 	 */
993 	error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, proc0.p_ucred);
994 	if (error != 0)
995 		panic("bootpc_fakeup_interface: GIFFLAGS, error=%d", error);
996 	ireq->ifr_flags |= IFF_UP;
997 	error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, proc0.p_ucred);
998 	if (error != 0)
999 		panic("bootpc_fakeup_interface: SIFFLAGS, error=%d", error);
1000 
1001 	/*
1002 	 * Do enough of ifconfig(8) so that the chosen interface
1003 	 * can talk to the servers.  (just set the address)
1004 	 */
1005 
1006 	/* addr is 0.0.0.0 */
1007 
1008 	sin = (struct sockaddr_in *) &ireq->ifr_addr;
1009 	clear_sinaddr(sin);
1010 	error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, proc0.p_ucred);
1011 	if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1012 		panic("bootpc_fakeup_interface: "
1013 		      "set if addr, error=%d", error);
1014 
1015 	/* netmask is 255.0.0.0 */
1016 
1017 	sin = (struct sockaddr_in *) &ireq->ifr_addr;
1018 	clear_sinaddr(sin);
1019 	sin->sin_addr.s_addr = htonl(0xff000000u);
1020 	error = ifioctl(so, SIOCSIFNETMASK, (caddr_t)ireq, proc0.p_ucred);
1021 	if (error != 0)
1022 		panic("bootpc_fakeup_interface: set if netmask, error=%d",
1023 		      error);
1024 
1025 	/* Broadcast is 255.255.255.255 */
1026 
1027 	sin = (struct sockaddr_in *)&ireq->ifr_addr;
1028 	clear_sinaddr(sin);
1029 	clear_sinaddr(&ifctx->broadcast);
1030 	sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
1031 	ifctx->broadcast.sin_addr.s_addr = sin->sin_addr.s_addr;
1032 
1033 	error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t)ireq, proc0.p_ucred);
1034 	if (error != 0 && error != EADDRNOTAVAIL)
1035 		panic("bootpc_fakeup_interface: "
1036 		      "set if broadcast addr, error=%d",
1037 		      error);
1038 	error = 0;
1039 
1040 	/* Get HW address */
1041 
1042 	sdl = NULL;
1043 	TAILQ_FOREACH(ifac, &ifctx->ifp->if_addrheads[mycpuid], ifa_link) {
1044 		struct ifaddr *ifa = ifac->ifa;
1045 
1046 		if (ifa->ifa_addr->sa_family == AF_LINK &&
1047 		    (sdl = ((struct sockaddr_dl *) ifa->ifa_addr)) != NULL &&
1048 		    sdl->sdl_type == IFT_ETHER)
1049 			break;
1050 	}
1051 
1052 	if (sdl == NULL)
1053 		panic("bootpc: Unable to find HW address for %s",
1054 		      ifctx->ireq.ifr_name);
1055 	ifctx->sdl = sdl;
1056 
1057 	return error;
1058 }
1059 
1060 
1061 static int
1062 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
1063 			struct bootpc_globalcontext *gctx,
1064 			struct thread *td)
1065 {
1066 	int error;
1067 	struct sockaddr_in defdst;
1068 	struct sockaddr_in defmask;
1069 	struct sockaddr_in *sin;
1070 
1071 	struct ifreq *ireq;
1072 	struct socket *so;
1073 	struct sockaddr_in *myaddr;
1074 	struct sockaddr_in *netmask;
1075 	struct sockaddr_in *gw;
1076 
1077 	ireq = &ifctx->ireq;
1078 	so = ifctx->so;
1079 	myaddr = &ifctx->myaddr;
1080 	netmask = &ifctx->netmask;
1081 	gw = &ifctx->gw;
1082 
1083 	if (bootpc_ifctx_isresolved(ifctx) == 0) {
1084 
1085 		/* Shutdown interfaces where BOOTP failed */
1086 
1087 		kprintf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
1088 		error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, proc0.p_ucred);
1089 		if (error != 0)
1090 			panic("bootpc_adjust_interface: "
1091 			      "SIOCGIFFLAGS, error=%d", error);
1092 		ireq->ifr_flags &= ~IFF_UP;
1093 		error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, proc0.p_ucred);
1094 		if (error != 0)
1095 			panic("bootpc_adjust_interface: "
1096 			      "SIOCSIFFLAGS, error=%d", error);
1097 
1098 		sin = (struct sockaddr_in *) &ireq->ifr_addr;
1099 		clear_sinaddr(sin);
1100 		error = ifioctl(so, SIOCDIFADDR, (caddr_t) ireq, proc0.p_ucred);
1101 		if (error != 0 && (error != EADDRNOTAVAIL ||
1102 				   ifctx == gctx->interfaces))
1103 			panic("bootpc_adjust_interface: "
1104 			      "SIOCDIFADDR, error=%d", error);
1105 
1106 		return 0;
1107 	}
1108 
1109 	kprintf("Adjusted interface %s\n", ifctx->ireq.ifr_name);
1110 	/*
1111 	 * Do enough of ifconfig(8) so that the chosen interface
1112 	 * can talk to the servers.  (just set the address)
1113 	 */
1114 	bcopy(netmask, &ireq->ifr_addr, sizeof(*netmask));
1115 	error = ifioctl(so, SIOCSIFNETMASK, (caddr_t) ireq, proc0.p_ucred);
1116 	if (error != 0)
1117 		panic("bootpc_adjust_interface: "
1118 		      "set if netmask, error=%d", error);
1119 
1120 	/* Broadcast is with host part of IP address all 1's */
1121 
1122 	sin = (struct sockaddr_in *) &ireq->ifr_addr;
1123 	clear_sinaddr(sin);
1124 	sin->sin_addr.s_addr = myaddr->sin_addr.s_addr |
1125 		~ netmask->sin_addr.s_addr;
1126 	error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t) ireq, proc0.p_ucred);
1127 	if (error != 0)
1128 		panic("bootpc_adjust_interface: "
1129 		      "set if broadcast addr, error=%d", error);
1130 
1131 	bcopy(myaddr, &ireq->ifr_addr, sizeof(*myaddr));
1132 	error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, proc0.p_ucred);
1133 	if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1134 		panic("bootpc_adjust_interface: "
1135 		      "set if addr, error=%d", error);
1136 
1137 	/* Add new default route */
1138 
1139 	if (ifctx->gotgw != 0 || gctx->gotgw == 0) {
1140 		clear_sinaddr(&defdst);
1141 		clear_sinaddr(&defmask);
1142 		error = rtrequest_global(RTM_ADD, (struct sockaddr *) &defdst,
1143 					 (struct sockaddr *) gw,
1144 					 (struct sockaddr *) &defmask,
1145 					 (RTF_UP | RTF_GATEWAY | RTF_STATIC));
1146 		if (error != 0) {
1147 			kprintf("bootpc_adjust_interface: "
1148 			       "add net route, error=%d\n", error);
1149 			return error;
1150 		}
1151 	}
1152 
1153 	return 0;
1154 }
1155 
1156 static void
1157 print_sin_addr(struct sockaddr_in *sin)
1158 {
1159 	print_in_addr(sin->sin_addr);
1160 }
1161 
1162 
1163 static void
1164 print_in_addr(struct in_addr addr)
1165 {
1166 	unsigned int ip;
1167 
1168 	ip = ntohl(addr.s_addr);
1169 	kprintf("%d.%d.%d.%d",
1170 	       ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1171 }
1172 
1173 static void
1174 bootpc_compose_query(struct bootpc_ifcontext *ifctx,
1175 		     struct bootpc_globalcontext *gctx, struct thread *td)
1176 {
1177 	unsigned char *vendp;
1178 	unsigned char vendor_client[64];
1179 	uint32_t leasetime;
1180 	uint8_t vendor_client_len;
1181 
1182 	ifctx->gotrootpath = 0;
1183 
1184 	bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1185 
1186 	/* bootpc part */
1187 	ifctx->call.op = BOOTP_REQUEST; 	/* BOOTREQUEST */
1188 	ifctx->call.htype = 1;			/* 10mb ethernet */
1189 	ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1190 	ifctx->call.hops = 0;
1191 	if (bootpc_ifctx_isunresolved(ifctx) != 0)
1192 		ifctx->xid++;
1193 	ifctx->call.xid = txdr_unsigned(ifctx->xid);
1194 	bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1195 
1196 	vendp = ifctx->call.vend;
1197 	*vendp++ = 99;		/* RFC1048 cookie */
1198 	*vendp++ = 130;
1199 	*vendp++ = 83;
1200 	*vendp++ = 99;
1201 	*vendp++ = TAG_MAXMSGSIZE;
1202 	*vendp++ = 2;
1203 	*vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1204 	*vendp++ = sizeof(struct bootp_packet) & 255;
1205 
1206 	ksnprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1207 		ostype, MACHINE, osrelease);
1208 	vendor_client_len = strlen(vendor_client);
1209 	*vendp++ = TAG_VENDOR_INDENTIFIER;
1210 	*vendp++ = vendor_client_len;
1211 	memcpy(vendp, vendor_client, vendor_client_len);
1212 	vendp += vendor_client_len;
1213 	ifctx->dhcpquerytype = DHCP_NOMSG;
1214 	switch (ifctx->state) {
1215 	case IF_DHCP_UNRESOLVED:
1216 		*vendp++ = TAG_DHCP_MSGTYPE;
1217 		*vendp++ = 1;
1218 		*vendp++ = DHCP_DISCOVER;
1219 		ifctx->dhcpquerytype = DHCP_DISCOVER;
1220 		ifctx->gotdhcpserver = 0;
1221 		break;
1222 	case IF_DHCP_OFFERED:
1223 		*vendp++ = TAG_DHCP_MSGTYPE;
1224 		*vendp++ = 1;
1225 		*vendp++ = DHCP_REQUEST;
1226 		ifctx->dhcpquerytype = DHCP_REQUEST;
1227 		*vendp++ = TAG_DHCP_REQ_ADDR;
1228 		*vendp++ = 4;
1229 		memcpy(vendp, &ifctx->reply.yiaddr, 4);
1230 		vendp += 4;
1231 		if (ifctx->gotdhcpserver != 0) {
1232 			*vendp++ = TAG_DHCP_SERVERID;
1233 			*vendp++ = 4;
1234 			memcpy(vendp, &ifctx->dhcpserver, 4);
1235 			vendp += 4;
1236 		}
1237 		*vendp++ = TAG_DHCP_LEASETIME;
1238 		*vendp++ = 4;
1239 		leasetime = htonl(300);
1240 		memcpy(vendp, &leasetime, 4);
1241 		vendp += 4;
1242 	default:
1243 		;
1244 	}
1245 	*vendp = TAG_END;
1246 
1247 	ifctx->call.secs = 0;
1248 	ifctx->call.flags = htons(0x8000); /* We need an broadcast answer */
1249 }
1250 
1251 
1252 static int
1253 bootpc_hascookie(struct bootp_packet *bp)
1254 {
1255 	return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1256 		bp->vend[2] == 83 && bp->vend[3] == 99);
1257 }
1258 
1259 
1260 static void
1261 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1262 		  unsigned char *start, int len, int tag)
1263 {
1264 	unsigned char *j;
1265 	unsigned char *ej;
1266 	unsigned char code;
1267 
1268 	if (tctx->badtag != 0 || tctx->badopt != 0)
1269 		return;
1270 
1271 	j = start;
1272 	ej = j + len;
1273 
1274 	while (j < ej) {
1275 		code = *j++;
1276 		if (code == TAG_PAD)
1277 			continue;
1278 		if (code == TAG_END)
1279 			return;
1280 		if (j >= ej || j + *j + 1 > ej) {
1281 			tctx->badopt = 1;
1282 			return;
1283 		}
1284 		len = *j++;
1285 		if (code == tag) {
1286 			if (tctx->taglen + len > TAG_MAXLEN) {
1287 				tctx->badtag = 1;
1288 				return;
1289 			}
1290 			tctx->foundopt = 1;
1291 			if (len > 0)
1292 				memcpy(tctx->buf + tctx->taglen,
1293 				       j, len);
1294 			tctx->taglen += len;
1295 		}
1296 		if (code == TAG_OVERLOAD)
1297 			tctx->overload = *j;
1298 
1299 		j += len;
1300 	}
1301 }
1302 
1303 
1304 static unsigned char *
1305 bootpc_tag(struct bootpc_tagcontext *tctx, struct bootp_packet *bp,
1306 	   int len, int tag)
1307 {
1308 	tctx->overload = 0;
1309 	tctx->badopt = 0;
1310 	tctx->badtag = 0;
1311 	tctx->foundopt = 0;
1312 	tctx->taglen = 0;
1313 
1314 	if (bootpc_hascookie(bp) == 0)
1315 		return NULL;
1316 
1317 	bootpc_tag_helper(tctx, &bp->vend[4],
1318 			  (unsigned char *) bp + len - &bp->vend[4], tag);
1319 
1320 	if ((tctx->overload & OVERLOAD_FILE) != 0)
1321 		bootpc_tag_helper(tctx,
1322 				  (unsigned char *) bp->file,
1323 				  sizeof(bp->file),
1324 				  tag);
1325 	if ((tctx->overload & OVERLOAD_SNAME) != 0)
1326 		bootpc_tag_helper(tctx,
1327 				  (unsigned char *) bp->sname,
1328 				  sizeof(bp->sname),
1329 				  tag);
1330 
1331 	if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1332 		return NULL;
1333 	tctx->buf[tctx->taglen] = '\0';
1334 	return tctx->buf;
1335 }
1336 
1337 
1338 static void
1339 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1340 		    struct bootpc_globalcontext *gctx)
1341 {
1342 	char *p;
1343 	unsigned int ip;
1344 
1345 	ifctx->gotgw = 0;
1346 	ifctx->gotnetmask = 0;
1347 
1348 	clear_sinaddr(&ifctx->myaddr);
1349 	clear_sinaddr(&ifctx->netmask);
1350 	clear_sinaddr(&ifctx->gw);
1351 
1352 	ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1353 
1354 	ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1355 	ksnprintf(gctx->lookup_path, sizeof(gctx->lookup_path),
1356 		 "swap.%d.%d.%d.%d",
1357 		 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1358 
1359 	kprintf("%s at ", ifctx->ireq.ifr_name);
1360 	print_sin_addr(&ifctx->myaddr);
1361 	kprintf(" server ");
1362 	print_in_addr(ifctx->reply.siaddr);
1363 
1364 	ifctx->gw.sin_addr = ifctx->reply.giaddr;
1365 	if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1366 		kprintf(" via gateway ");
1367 		print_in_addr(ifctx->reply.giaddr);
1368 	}
1369 
1370 	/* This call used for the side effect (overload flag) */
1371 	(void) bootpc_tag(&gctx->tmptag,
1372 			  &ifctx->reply, ifctx->replylen, TAG_END);
1373 
1374 	if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1375 		if (ifctx->reply.sname[0] != '\0')
1376 			kprintf(" server name %s", ifctx->reply.sname);
1377 	if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1378 		if (ifctx->reply.file[0] != '\0')
1379 			kprintf(" boot file %s", ifctx->reply.file);
1380 
1381 	kprintf("\n");
1382 
1383 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1384 		       TAG_SUBNETMASK);
1385 	if (p != NULL) {
1386 		if (gctx->tag.taglen != 4)
1387 			panic("bootpc: subnet mask len is %d",
1388 			      gctx->tag.taglen);
1389 		bcopy(p, &ifctx->netmask.sin_addr, 4);
1390 		ifctx->gotnetmask = 1;
1391 		kprintf("subnet mask ");
1392 		print_sin_addr(&ifctx->netmask);
1393 		kprintf(" ");
1394 	}
1395 
1396 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1397 		       TAG_ROUTERS);
1398 	if (p != NULL) {
1399 		/* Routers */
1400 		if (gctx->tag.taglen % 4)
1401 			panic("bootpc: Router Len is %d", gctx->tag.taglen);
1402 		if (gctx->tag.taglen > 0) {
1403 			bcopy(p, &ifctx->gw.sin_addr, 4);
1404 			kprintf("router ");
1405 			print_sin_addr(&ifctx->gw);
1406 			kprintf(" ");
1407 			ifctx->gotgw = 1;
1408 			gctx->gotgw = 1;
1409 		}
1410 	}
1411 
1412 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1413 		       TAG_ROOT);
1414 	if (p != NULL) {
1415 		if (gctx->setrootfs != NULL) {
1416 			kprintf("rootfs %s (ignored) ", p);
1417 		} else 	if (setfs(&nd->root_saddr,
1418 				  nd->root_hostnam, p)) {
1419 			kprintf("rootfs %s ",p);
1420 			gctx->gotrootpath = 1;
1421 			ifctx->gotrootpath = 1;
1422 			gctx->setrootfs = ifctx;
1423 
1424 			p = bootpc_tag(&gctx->tag, &ifctx->reply,
1425 				       ifctx->replylen,
1426 				       TAG_ROOTOPTS);
1427 			if (p != NULL) {
1428 				nfs_mountopts(&nd->root_args, p);
1429 				kprintf("rootopts %s ", p);
1430 			}
1431 		} else
1432 			panic("Failed to set rootfs to %s",p);
1433 	}
1434 
1435 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1436 		       TAG_SWAP);
1437 	if (p != NULL) {
1438 		if (gctx->setswapfs != NULL) {
1439 			kprintf("swapfs %s (ignored) ", p);
1440 		} else 	if (setfs(&nd->swap_saddr,
1441 				  nd->swap_hostnam, p)) {
1442 			gctx->gotswappath = 1;
1443 			gctx->setswapfs = ifctx;
1444 			kprintf("swapfs %s ", p);
1445 
1446 			p = bootpc_tag(&gctx->tag, &ifctx->reply,
1447 				       ifctx->replylen,
1448 				       TAG_SWAPOPTS);
1449 			if (p != NULL) {
1450 				/* swap mount options */
1451 				nfs_mountopts(&nd->swap_args, p);
1452 				kprintf("swapopts %s ", p);
1453 			}
1454 
1455 			p = bootpc_tag(&gctx->tag, &ifctx->reply,
1456 				       ifctx->replylen,
1457 				       TAG_SWAPSIZE);
1458 			if (p != NULL) {
1459 				int swaplen;
1460 				if (gctx->tag.taglen != 4)
1461 					panic("bootpc: "
1462 					      "Expected 4 bytes for swaplen, "
1463 					      "not %d bytes",
1464 					      gctx->tag.taglen);
1465 				bcopy(p, &swaplen, 4);
1466 				nd->swap_nblks = ntohl(swaplen);
1467 				kprintf("swapsize %d KB ",
1468 				       nd->swap_nblks);
1469 			}
1470 		} else
1471 			panic("Failed to set swapfs to %s", p);
1472 	}
1473 
1474 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1475 		       TAG_HOSTNAME);
1476 	if (p != NULL) {
1477 		if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1478 			panic("bootpc: hostname >= %d bytes",
1479 			      MAXHOSTNAMELEN);
1480 		if (gctx->sethostname != NULL) {
1481 			kprintf("hostname %s (ignored) ", p);
1482 		} else {
1483 			strcpy(nd->my_hostnam, p);
1484 			strcpy(hostname, p);
1485 			kprintf("hostname %s ",hostname);
1486 			gctx->sethostname = ifctx;
1487 		}
1488 	}
1489 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1490 		       TAG_COOKIE);
1491 	if (p != NULL) {	/* store in a sysctl variable */
1492 		int i, l = sizeof(bootp_cookie) - 1;
1493 		for (i = 0; i < l && p[i] != '\0'; i++)
1494 			bootp_cookie[i] = p[i];
1495 		p[i] = '\0';
1496 	}
1497 
1498 	kprintf("\n");
1499 
1500 	if (ifctx->gotnetmask == 0) {
1501 		if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1502 			ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1503 		else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1504 			ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1505 		else
1506 			ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1507 	}
1508 	if (ifctx->gotgw == 0) {
1509 		/* Use proxyarp */
1510 		ifctx->gw.sin_addr.s_addr = ifctx->myaddr.sin_addr.s_addr;
1511 	}
1512 }
1513 
1514 void
1515 bootpc_init(void)
1516 {
1517 	struct bootpc_ifcontext *ifctx, *nctx;	/* Interface BOOTP contexts */
1518 	struct bootpc_globalcontext *gctx; 	/* Global BOOTP context */
1519 	struct ifnet *ifp;
1520 	int error;
1521 	struct nfsv3_diskless *nd;
1522 	struct thread *td;
1523 
1524 	nd = &nfsv3_diskless;
1525 	td = curthread;
1526 
1527 	/*
1528 	 * If already filled in, don't touch it here
1529 	 */
1530 	if (nfs_diskless_valid != 0)
1531 		return;
1532 
1533 	/*
1534 	 * Wait until arp entries can be handled.
1535 	 */
1536 	while (time_second == 0)
1537 		tsleep(&time_second, 0, "arpkludge", 10);
1538 
1539 	gctx = kmalloc(sizeof(*gctx), M_TEMP, M_WAITOK | M_ZERO);
1540 
1541 	gctx->xid = ~0xFFFF;
1542 	gctx->starttime = time_second;
1543 
1544 	ifctx = allocifctx(gctx);
1545 
1546 	/*
1547 	 * Find a network interface.
1548 	 */
1549 #ifdef BOOTP_WIRED_TO
1550 	kprintf("bootpc_init: wired to interface '%s'\n",
1551 	       __XSTRING(BOOTP_WIRED_TO));
1552 #endif
1553 	bzero(&ifctx->ireq, sizeof(ifctx->ireq));
1554 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
1555 		strlcpy(ifctx->ireq.ifr_name, ifp->if_xname,
1556 			 sizeof(ifctx->ireq.ifr_name));
1557 #ifdef BOOTP_WIRED_TO
1558 		if (strcmp(ifctx->ireq.ifr_name,
1559 			   __XSTRING(BOOTP_WIRED_TO)) != 0)
1560 			continue;
1561 #else
1562 		if ((ifp->if_flags &
1563 		     (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1564 		    IFF_BROADCAST)
1565 			continue;
1566 #endif
1567 		if (gctx->interfaces != NULL)
1568 			gctx->lastinterface->next = ifctx;
1569 		else
1570 			gctx->interfaces = ifctx;
1571 		ifctx->ifp = ifp;
1572 		gctx->lastinterface = ifctx;
1573 		ifctx = allocifctx(gctx);
1574 	}
1575 	kfree(ifctx, M_TEMP);
1576 
1577 	if (gctx->interfaces == NULL) {
1578 #ifdef BOOTP_WIRED_TO
1579 		panic("bootpc_init: Could not find interface specified "
1580 		      "by BOOTP_WIRED_TO: "
1581 		      __XSTRING(BOOTP_WIRED_TO));
1582 #else
1583 		panic("bootpc_init: no suitable interface");
1584 #endif
1585 	}
1586 
1587 	gctx->gotrootpath = 0;
1588 	gctx->gotswappath = 0;
1589 	gctx->gotgw = 0;
1590 
1591 	for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1592 		bootpc_fakeup_interface(ifctx, gctx, td);
1593 
1594 	for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1595 		bootpc_compose_query(ifctx, gctx, td);
1596 
1597 	ifctx = gctx->interfaces;
1598 	error = bootpc_call(gctx, td);
1599 
1600 	if (error != 0) {
1601 #ifdef BOOTP_NFSROOT
1602 		panic("BOOTP call failed");
1603 #else
1604 		kprintf("BOOTP call failed\n");
1605 #endif
1606 	}
1607 
1608 	nfs_mountopts(&nd->root_args, NULL);
1609 
1610 	nfs_mountopts(&nd->swap_args, NULL);
1611 
1612 	for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1613 		if (bootpc_ifctx_isresolved(ifctx) != 0)
1614 			bootpc_decode_reply(nd, ifctx, gctx);
1615 
1616 	if (gctx->gotswappath == 0)
1617 		nd->swap_nblks = 0;
1618 #ifdef BOOTP_NFSROOT
1619 	if (gctx->gotrootpath == 0)
1620 		panic("bootpc: No root path offered");
1621 #endif
1622 
1623 	for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
1624 		bootpc_adjust_interface(ifctx, gctx, td);
1625 
1626 		soclose(ifctx->so, FNONBLOCK);
1627 	}
1628 
1629 	for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1630 		if (ifctx->gotrootpath != 0)
1631 			break;
1632 	if (ifctx == NULL) {
1633 		for (ifctx = gctx->interfaces;
1634 		     ifctx != NULL;
1635 		     ifctx = ifctx->next)
1636 			if (bootpc_ifctx_isresolved(ifctx) != 0)
1637 				break;
1638 	}
1639 	if (ifctx == NULL)
1640 		goto out;
1641 
1642 	if (gctx->gotrootpath != 0) {
1643 
1644 		error = md_mount(&nd->root_saddr, nd->root_hostnam,
1645 				 nd->root_fh, &nd->root_fhsize,
1646 				 &nd->root_args, td);
1647 		if (error != 0)
1648 			panic("nfs_boot: mountd root, error=%d", error);
1649 
1650 		if (gctx->gotswappath != 0) {
1651 
1652 			error = md_mount(&nd->swap_saddr,
1653 					 nd->swap_hostnam,
1654 					 nd->swap_fh, &nd->swap_fhsize,
1655 					 &nd->swap_args, td);
1656 			if (error != 0)
1657 				panic("nfs_boot: mountd swap, error=%d",
1658 				      error);
1659 
1660 			error = md_lookup_swap(&nd->swap_saddr,
1661 					       gctx->lookup_path,
1662 					       nd->swap_fh, &nd->swap_fhsize,
1663 					       &nd->swap_args, td);
1664 			if (error != 0)
1665 				panic("nfs_boot: lookup swap, error=%d",
1666 				      error);
1667 		}
1668 		nfs_diskless_valid = 3;
1669 	}
1670 
1671 	strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1672 	bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1673 	bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1674 	((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1675 		ifctx->myaddr.sin_addr.s_addr |
1676 		~ ifctx->netmask.sin_addr.s_addr;
1677 	bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1678 
1679 out:
1680 	for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = nctx) {
1681 		nctx = ifctx->next;
1682 		kfree(ifctx, M_TEMP);
1683 	}
1684 	kfree(gctx, M_TEMP);
1685 }
1686 
1687