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