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