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