bootp_subr.c revision 301056
11541Srgrimes/*-
21541Srgrimes * Copyright (c) 1995 Gordon Ross, Adam Glass
31541Srgrimes * Copyright (c) 1992 Regents of the University of California.
41541Srgrimes * All rights reserved.
51541Srgrimes *
61541Srgrimes * This software was developed by the Computer Systems Engineering group
71541Srgrimes * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
81541Srgrimes * contributed to Berkeley.
91541Srgrimes *
101541Srgrimes * Redistribution and use in source and binary forms, with or without
111541Srgrimes * modification, are permitted provided that the following conditions
121541Srgrimes * are met:
131541Srgrimes * 1. Redistributions of source code must retain the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer.
151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161541Srgrimes *    notice, this list of conditions and the following disclaimer in the
171541Srgrimes *    documentation and/or other materials provided with the distribution.
181541Srgrimes * 3. All advertising materials mentioning features or use of this software
191541Srgrimes *    must display the following acknowledgement:
201541Srgrimes *	This product includes software developed by the University of
211541Srgrimes *	California, Lawrence Berkeley Laboratory and its contributors.
221541Srgrimes * 4. Neither the name of the University nor the names of its contributors
231541Srgrimes *    may be used to endorse or promote products derived from this software
241541Srgrimes *    without specific prior written permission.
251541Srgrimes *
261541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
271541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
281541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
291541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
301541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
311541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
321541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
331541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3414508Shsu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3550477Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
361541Srgrimes * SUCH DAMAGE.
371541Srgrimes *
382165Spaul * based on:
392865Sbde *      nfs/krpc_subr.c
402165Spaul *	$NetBSD: krpc_subr.c,v 1.10 1995/08/08 20:43:43 gwr Exp $
4149043Salc */
421549Srgrimes
4329683Sgibbs#include <sys/cdefs.h>
44120610Smux__FBSDID("$FreeBSD: stable/10/sys/nfs/bootp_subr.c 301056 2016-05-31 17:01:54Z ian $");
4594936Smux
46112367Sphk#include "opt_bootp.h"
471549Srgrimes#include "opt_nfs.h"
487090Sbde#include "opt_rootdevname.h"
49289032Scperciva
50214004Smarcel#include <sys/param.h>
511541Srgrimes#include <sys/systm.h>
521541Srgrimes#include <sys/endian.h>
53246254Savg#include <sys/jail.h>
541541Srgrimes#include <sys/kernel.h>
55103083Speter#include <sys/sockio.h>
561541Srgrimes#include <sys/malloc.h>
57197316Salc#include <sys/mount.h>
58102600Speter#include <sys/mbuf.h>
59142834Swes#include <sys/proc.h>
601541Srgrimes#include <sys/reboot.h>
6136809Sbde#include <sys/socket.h>
621541Srgrimes#include <sys/socketvar.h>
631541Srgrimes#include <sys/sysctl.h>
6415113Sbde#include <sys/uio.h>
651541Srgrimes
6680418Speter#include <net/if.h>
67202143Sbrooks#include <net/route.h>
68204420Salc
6980418Speter#include <netinet/in.h>
70204633Sivoras#include <netinet/in_var.h>
71204633Sivoras#include <net/if_types.h>
72204611Sivoras#include <net/if_dl.h>
73204633Sivoras#include <net/vnet.h>
74258127Spluknet
75204633Sivoras#include <nfs/nfsproto.h>
76258127Spluknet#include <nfsclient/nfs.h>
77278522Sjhb#include <nfs/nfsdiskless.h>
78204420Salc#include <nfs/krpc.h>
79244105Salfred#include <nfs/xdr_subs.h>
80253007Salfred
81244105Salfred
82243980Salfred#define BOOTP_MIN_LEN		300	/* Minimum size of bootp udp packet */
83244105Salfred
84120610Smux#ifndef BOOTP_SETTLE_DELAY
85120610Smux#define BOOTP_SETTLE_DELAY 3
86243980Salfred#endif
87120610Smux
88141458Sphk/*
89141458Sphk * Wait 10 seconds for interface appearance
90182909Sjhb * USB ethernet adapters might require some time to pop up
91243980Salfred */
92141458Sphk#ifndef	BOOTP_IFACE_WAIT_TIMEOUT
93141458Sphk#define	BOOTP_IFACE_WAIT_TIMEOUT	10
9442408Seivind#endif
95165547Skmacy
96165547Skmacy/*
97165547Skmacy * What is the longest we will wait before re-sending a request?
98165547Skmacy * Note this is also the frequency of "RPC timeout" messages.
99165547Skmacy * The re-send loop count sup linearly to this maximum, so the
10042408Seivind * first complaint will happen after (1+2+3+4+5)=15 seconds.
10142408Seivind */
102228478Sed#define	MAX_RESEND_DELAY 5	/* seconds */
103228478Sed
104109316Sphk/* Definitions from RFC951 */
10593600Sjakestruct bootp_packet {
106196334Sattilio	u_int8_t op;
107196334Sattilio	u_int8_t htype;
108196334Sattilio	u_int8_t hlen;
109196334Sattilio	u_int8_t hops;
110196334Sattilio	u_int32_t xid;
111196334Sattilio	u_int16_t secs;
112196334Sattilio	u_int16_t flags;
113196334Sattilio	struct in_addr ciaddr;
114196334Sattilio	struct in_addr yiaddr;
115196334Sattilio	struct in_addr siaddr;
116196226Sbz	struct in_addr giaddr;
1171541Srgrimes	unsigned char chaddr[16];
118249189Sglebius	char sname[64];
119249189Sglebius	char file[128];
120249189Sglebius	unsigned char vend[1222];
121249189Sglebius};
122249189Sglebius
123249189Sglebiusstruct bootpc_ifcontext {
124228424Savg	STAILQ_ENTRY(bootpc_ifcontext) next;
125228424Savg	struct bootp_packet call;
126228424Savg	struct bootp_packet reply;
127228424Savg	int replylen;
128228424Savg	int overload;
129230643Sattilio	union {
130228424Savg		struct ifreq _ifreq;
131228424Savg		struct in_aliasreq _in_alias_req;
13283595Speter	} _req;
13383595Speter#define	ireq	_req._ifreq
13483595Speter#define	iareq	_req._in_alias_req
13583595Speter	struct ifnet *ifp;
13683595Speter	struct sockaddr_dl *sdl;
137174253Skib	struct sockaddr_in myaddr;
13883595Speter	struct sockaddr_in netmask;
13983595Speter	struct sockaddr_in gw;
14094936Smux	int gotgw;
141160217Sscottl	int gotnetmask;
14283595Speter	int gotrootpath;
14383595Speter	int outstanding;
14483595Speter	int sentmsg;
14583595Speter	u_int32_t xid;
14694936Smux	enum {
14794936Smux		IF_BOOTP_UNRESOLVED,
148221853Smdf		IF_BOOTP_RESOLVED,
149221853Smdf		IF_BOOTP_FAILED,
150248508Skib		IF_DHCP_UNRESOLVED,
151231949Skib		IF_DHCP_OFFERED,
152257122Skib		IF_DHCP_RESOLVED,
153231949Skib		IF_DHCP_FAILED,
154257122Skib	} state;
155231949Skib	int dhcpquerytype;		/* dhcp type sent */
15683595Speter	struct in_addr dhcpserver;
1571541Srgrimes	int gotdhcpserver;
1581541Srgrimes	uint16_t mtu;
15930282Sphk};
160183982Sbz
161167387Sjhb#define TAG_MAXLEN 1024
16230282Sphkstruct bootpc_tagcontext {
16365708Sjake	char buf[TAG_MAXLEN + 1];
16433777Sbde	int overload;
16592976Srwatson	int badopt;
16683366Sjulian	int badtag;
16733777Sbde	int foundopt;
16867893Sphk	int taglen;
16933777Sbde};
17079418Sjulian
171210131Smavstruct bootpc_globalcontext {
172266347Sian	STAILQ_HEAD(, bootpc_ifcontext) interfaces;
17330282Sphk	u_int32_t xid;
174234785Sdim	int any_root_overrides;
17592719Salfred	int gotrootpath;
17692719Salfred	int gotgw;
17792719Salfred	int ifnum;
17892719Salfred	int secs;
17992719Salfred	int starttime;
18099098Siedowse	struct bootp_packet reply;
181220987Skib	int replylen;
182166022Srrs	struct bootpc_ifcontext *setrootfs;
183166022Srrs	struct bootpc_ifcontext *sethostname;
184166022Srrs	struct bootpc_tagcontext tmptag;
185166022Srrs	struct bootpc_tagcontext tag;
186166022Srrs};
18792719Salfred
188136836Sphk#define IPPORT_BOOTPC 68
1891541Srgrimes#define IPPORT_BOOTPS 67
190130164Sphk
191283676Smarkj#define BOOTP_REQUEST 1
19290503Sbde#define BOOTP_REPLY 2
19392719Salfred
194192323Smarcel/* Common tags */
19592719Salfred#define TAG_PAD		  0  /* Pad option, implicit length 1 */
19692719Salfred#define TAG_SUBNETMASK	  1  /* RFC 950 subnet mask */
19792719Salfred#define TAG_ROUTERS	  3  /* Routers (in order of preference) */
19892719Salfred#define TAG_HOSTNAME	 12  /* Client host name */
199102600Speter#define TAG_ROOT	 17  /* Root path */
200202050Simp#define TAG_INTF_MTU	 26  /* Interface MTU Size (RFC2132) */
20192719Salfred
202266094Sian/* DHCP specific tags */
203266197Sian#define TAG_OVERLOAD	 52  /* Option Overload */
204266197Sian#define TAG_MAXMSGSIZE   57  /* Maximum DHCP Message Size */
205266094Sian
20692719Salfred#define TAG_END		255  /* End Option (i.e. no more options) */
207102227Smike
20892719Salfred/* Overload values */
20992719Salfred#define OVERLOAD_FILE     1
210288766Smav#define OVERLOAD_SNAME    2
211288766Smav
21292719Salfred/* Site specific tags: */
21392719Salfred#define TAG_ROOTOPTS	130
21492719Salfred#define TAG_COOKIE	134	/* ascii info for userland, via sysctl */
21592719Salfred
216102227Smike#define TAG_DHCP_MSGTYPE 53
217288766Smav#define TAG_DHCP_REQ_ADDR 50
218288766Smav#define TAG_DHCP_SERVERID 54
219102227Smike#define TAG_DHCP_LEASETIME 51
220137098Sdes
221102227Smike#define TAG_VENDOR_INDENTIFIER 60
22292719Salfred
223117837Sphk#define DHCP_NOMSG    0
224117837Sphk#define DHCP_DISCOVER 1
225117860Sphk#define DHCP_OFFER    2
226117860Sphk#define DHCP_REQUEST  3
227117837Sphk#define DHCP_ACK      5
228117837Sphk
22992719Salfred/* NFS read/write block size */
230255351Snp#ifndef BOOTP_BLOCKSIZE
231144706Sphk#define	BOOTP_BLOCKSIZE	8192
232123215Sscottl#endif
233123215Sscottl
234123215Sscottlstatic char bootp_cookie[128];
235123215Sscottlstatic struct socket *bootp_so;
236123215SscottlSYSCTL_STRING(_kern, OID_AUTO, bootp_cookie, CTLFLAG_RD,
2371541Srgrimes	bootp_cookie, 0, "Cookie (T134) supplied by bootp server");
238113090Sdes
239117837Sphk/* mountd RPC */
240117837Sphkstatic int	md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp,
2411541Srgrimes		    int *fhsizep, struct nfs_args *args, struct thread *td);
242117837Sphkstatic int	setfs(struct sockaddr_in *addr, char *path, char *p,
243189170Sed		    const struct in_addr *siaddr);
2448215Sdgstatic int	getdec(char **ptr);
245123852Salfredstatic int	getip(char **ptr, struct in_addr *ip);
246123852Salfredstatic void	mountopts(struct nfs_args *args, char *p);
247123852Salfredstatic int	xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len);
248123852Salfredstatic int	xdr_int_decode(struct mbuf **ptr, int *iptr);
249123852Salfredstatic void	print_in_addr(struct in_addr addr);
250123852Salfredstatic void	print_sin_addr(struct sockaddr_in *addr);
251123852Salfredstatic void	clear_sinaddr(struct sockaddr_in *sin);
252123852Salfredstatic void	allocifctx(struct bootpc_globalcontext *gctx);
253223889Skibstatic void	bootpc_compose_query(struct bootpc_ifcontext *ifctx,
254223889Skib		    struct thread *td);
255123852Salfredstatic unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx,
256123852Salfred		    struct bootp_packet *bp, int len, int tag);
257223889Skibstatic void bootpc_tag_helper(struct bootpc_tagcontext *tctx,
258223889Skib		    unsigned char *start, int len, int tag);
2591541Srgrimes
260274648Skib#ifdef BOOTP_DEBUG
261274648Skibvoid bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma);
262274648Skibvoid bootpboot_p_rtentry(struct rtentry *rt);
263274648Skibvoid bootpboot_p_tree(struct radix_node *rn);
264274648Skibvoid bootpboot_p_rtlist(void);
265274648Skibvoid bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa);
266274648Skibvoid bootpboot_p_iflist(void);
267274648Skib#endif
268274648Skib
269274648Skibstatic int	bootpc_call(struct bootpc_globalcontext *gctx,
270274648Skib		    struct thread *td);
271274648Skib
272274648Skibstatic void	bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
273163449Sdavidxu		    struct thread *td);
274274648Skib
275274648Skibstatic int	bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
276274648Skib		    struct bootpc_globalcontext *gctx, struct thread *td);
277274648Skib
278274648Skibstatic void	bootpc_decode_reply(struct nfsv3_diskless *nd,
2791541Srgrimes		    struct bootpc_ifcontext *ifctx,
28092719Salfred		    struct bootpc_globalcontext *gctx);
2811541Srgrimes
282177642Sphkstatic int	bootpc_received(struct bootpc_globalcontext *gctx,
283177642Sphk		    struct bootpc_ifcontext *ifctx);
284153666Sjhb
285232783Smavstatic __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
286153666Sjhbstatic __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
287212541Smavstatic __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
28892719Salfred
289153666Sjhb/*
290232783Smav * In order to have multiple active interfaces with address 0.0.0.0
291153666Sjhb * and be able to send data to a selected interface, we first set
292232783Smav * mask to /8 on all interfaces, and temporarily set it to /0 when
2931541Srgrimes * doing sosend().
294212541Smav */
295210131Smav
29692719Salfred#ifdef BOOTP_DEBUG
29792719Salfredvoid
298110296Sjakebootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma)
299110296Sjake{
300247454Sdavide
301212541Smav	if (sa == NULL) {
302247777Sdavide		printf("(sockaddr *) <null>");
303266347Sian		return;
304280973Sjhb	}
305280973Sjhb	switch (sa->sa_family) {
306280973Sjhb	case AF_INET:
3071541Srgrimes	{
30892719Salfred		struct sockaddr_in *sin;
30992976Srwatson
310183982Sbz		sin = (struct sockaddr_in *) sa;
31167893Sphk		printf("inet ");
31292719Salfred		print_sin_addr(sin);
31394936Smux		if (ma != NULL) {
31492719Salfred			sin = (struct sockaddr_in *) ma;
315172612Sdes			printf(" mask ");
316172612Sdes			print_sin_addr(sin);
317172612Sdes		}
31892719Salfred	}
31992719Salfred	break;
32094959Smux	case AF_LINK:
32194936Smux	{
32294936Smux		struct sockaddr_dl *sli;
32340096Smsmith		int i;
324155534Sphk
325155534Sphk		sli = (struct sockaddr_dl *) sa;
326155534Sphk		printf("link %.*s ", sli->sdl_nlen, sli->sdl_data);
327155534Sphk		for (i = 0; i < sli->sdl_alen; i++) {
328155534Sphk			if (i > 0)
329155444Sphk				printf(":");
330137098Sdes			printf("%x", ((unsigned char *) LLADDR(sli))[i]);
33183595Speter		}
332137098Sdes	}
333137098Sdes	break;
33431960Snate	default:
3357109Sphk		printf("af%d", sa->sa_family);
3362112Swollman	}
3372112Swollman}
33892719Salfred
33992719Salfredvoid
340209371Smavbootpboot_p_rtentry(struct rtentry *rt)
341209371Smav{
34292719Salfred
3432112Swollman	bootpboot_p_sa(rt_key(rt), rt_mask(rt));
344167111Sthomas	printf(" ");
345214004Smarcel	bootpboot_p_sa(rt->rt_gateway, NULL);
34692719Salfred	printf(" ");
3477090Sbde	printf("flags %x", (unsigned short) rt->rt_flags);
3482112Swollman	printf(" %d", (int) rt->rt_expire);
34992719Salfred	printf(" %s\n", rt->rt_ifp->if_xname);
35029683Sgibbs}
35129683Sgibbs
3522112Swollmanvoid
35392719Salfredbootpboot_p_tree(struct radix_node *rn)
35492719Salfred{
35592719Salfred
3562165Spaul	while (rn != NULL) {
357167111Sthomas		if (rn->rn_bit < 0) {
35871240Speter			if ((rn->rn_flags & RNF_ROOT) != 0) {
35971240Speter			} else {
36071240Speter				bootpboot_p_rtentry((struct rtentry *) rn);
36171240Speter			}
36271240Speter			rn = rn->rn_dupedkey;
36371240Speter		} else {
36471240Speter			bootpboot_p_tree(rn->rn_left);
36571240Speter			bootpboot_p_tree(rn->rn_right);
366100073Smarkm			return;
36726312Speter		}
368137098Sdes	}
36918884Sbde}
37018884Sbde
37118884Sbdevoid
372167387Sjhbbootpboot_p_rtlist(void)
373247787Sdavide{
374167387Sjhb	struct radix_node_head *rnh;
375247787Sdavide
376247787Sdavide	printf("Routing table:\n");
377247787Sdavide	rnh = rt_tables_get_rnh(0, AF_INET);
378247787Sdavide	if (rnh == NULL)
379247787Sdavide		return;
380247787Sdavide	RADIX_NODE_HEAD_RLOCK(rnh);	/* could sleep XXX */
381247787Sdavide	bootpboot_p_tree(rnh->rnh_treetop);
382247787Sdavide	RADIX_NODE_HEAD_RUNLOCK(rnh);
383247787Sdavide}
384247787Sdavide
385247787Sdavidevoid
386247787Sdavidebootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
387247787Sdavide{
388247787Sdavide
389337034Shselasky	printf("%s flags %x, addr ",
390337034Shselasky	       ifp->if_xname, ifp->if_flags);
391167387Sjhb	print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
392247787Sdavide	printf(", broadcast ");
393247787Sdavide	print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
394247787Sdavide	printf(", netmask ");
395247787Sdavide	print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
396117837Sphk	printf("\n");
397117837Sphk}
39818884Sbde
39947028Sphkvoid
400130585Sphkbootpboot_p_iflist(void)
40147028Sphk{
40247028Sphk	struct ifnet *ifp;
403138509Sphk	struct ifaddr *ifa;
404130640Sphk
405143622Sphk	printf("Interface list:\n");
40667158Sphk	IFNET_RLOCK();
407189450Skib	for (ifp = TAILQ_FIRST(&V_ifnet);
408189450Skib	     ifp != NULL;
40967158Sphk	     ifp = TAILQ_NEXT(ifp, if_link)) {
41092719Salfred		for (ifa = TAILQ_FIRST(&ifp->if_addrhead);
41167158Sphk		     ifa != NULL;
412145250Sphk		     ifa = TAILQ_NEXT(ifa, ifa_link))
413145250Sphk			if (ifa->ifa_addr->sa_family == AF_INET)
414145250Sphk				bootpboot_p_if(ifp, ifa);
415190878Sthompsa	}
416145250Sphk	IFNET_RUNLOCK();
417168300Spjd}
418168506Spjd#endif /* defined(BOOTP_DEBUG) */
419145250Sphk
420145250Sphkstatic void
421135956Sphkclear_sinaddr(struct sockaddr_in *sin)
422135956Sphk{
423135956Sphk
424135956Sphk	bzero(sin, sizeof(*sin));
425143283Sphk	sin->sin_len = sizeof(*sin);
426255057Skib	sin->sin_family = AF_INET;
427136945Sphk	sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
428171202Skib	sin->sin_port = 0;
429171202Skib}
430143283Sphk
431209710Sjhstatic void
432143283Sphkallocifctx(struct bootpc_globalcontext *gctx)
433135956Sphk{
434135956Sphk	struct bootpc_ifcontext *ifctx;
435272946Skib
436272946Skib	ifctx = malloc(sizeof(*ifctx), M_TEMP, M_WAITOK | M_ZERO);
437284199Skib	ifctx->xid = gctx->xid;
438284199Skib#ifdef BOOTP_NO_DHCP
439303433Skib	ifctx->state = IF_BOOTP_UNRESOLVED;
440303433Skib#else
4412865Sbde	ifctx->state = IF_DHCP_UNRESOLVED;
442#endif
443	gctx->xid += 0x100;
444	STAILQ_INSERT_TAIL(&gctx->interfaces, ifctx, next);
445}
446
447static __inline int
448bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
449{
450
451	if (ifctx->state == IF_BOOTP_RESOLVED ||
452	    ifctx->state == IF_DHCP_RESOLVED)
453		return 1;
454	return 0;
455}
456
457static __inline int
458bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
459{
460
461	if (ifctx->state == IF_BOOTP_UNRESOLVED ||
462	    ifctx->state == IF_DHCP_UNRESOLVED)
463		return 1;
464	return 0;
465}
466
467static __inline int
468bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
469{
470
471	if (ifctx->state == IF_BOOTP_FAILED ||
472	    ifctx->state == IF_DHCP_FAILED)
473		return 1;
474	return 0;
475}
476
477static int
478bootpc_received(struct bootpc_globalcontext *gctx,
479    struct bootpc_ifcontext *ifctx)
480{
481	unsigned char dhcpreplytype;
482	char *p;
483
484	/*
485	 * Need timeout for fallback to less
486	 * desirable alternative.
487	 */
488
489	/* This call used for the side effect (badopt flag) */
490	(void) bootpc_tag(&gctx->tmptag, &gctx->reply,
491			  gctx->replylen,
492			  TAG_END);
493
494	/* If packet is invalid, ignore it */
495	if (gctx->tmptag.badopt != 0)
496		return 0;
497
498	p = bootpc_tag(&gctx->tmptag, &gctx->reply,
499		       gctx->replylen, TAG_DHCP_MSGTYPE);
500	if (p != NULL)
501		dhcpreplytype = *p;
502	else
503		dhcpreplytype = DHCP_NOMSG;
504
505	switch (ifctx->dhcpquerytype) {
506	case DHCP_DISCOVER:
507		if (dhcpreplytype != DHCP_OFFER 	/* Normal DHCP offer */
508#ifndef BOOTP_FORCE_DHCP
509		    && dhcpreplytype != DHCP_NOMSG	/* Fallback to BOOTP */
510#endif
511			)
512			return 0;
513		break;
514	case DHCP_REQUEST:
515		if (dhcpreplytype != DHCP_ACK)
516			return 0;
517	case DHCP_NOMSG:
518		break;
519	}
520
521	/* Ignore packet unless it gives us a root tag we didn't have */
522
523	if ((ifctx->state == IF_BOOTP_RESOLVED ||
524	     (ifctx->dhcpquerytype == DHCP_DISCOVER &&
525	      (ifctx->state == IF_DHCP_OFFERED ||
526	       ifctx->state == IF_DHCP_RESOLVED))) &&
527	    (bootpc_tag(&gctx->tmptag, &ifctx->reply,
528			ifctx->replylen,
529			TAG_ROOT) != NULL ||
530	     bootpc_tag(&gctx->tmptag, &gctx->reply,
531			gctx->replylen,
532			TAG_ROOT) == NULL))
533		return 0;
534
535	bcopy(&gctx->reply, &ifctx->reply, gctx->replylen);
536	ifctx->replylen = gctx->replylen;
537
538	/* XXX: Only reset if 'perfect' response */
539	if (ifctx->state == IF_BOOTP_UNRESOLVED)
540		ifctx->state = IF_BOOTP_RESOLVED;
541	else if (ifctx->state == IF_DHCP_UNRESOLVED &&
542		 ifctx->dhcpquerytype == DHCP_DISCOVER) {
543		if (dhcpreplytype == DHCP_OFFER)
544			ifctx->state = IF_DHCP_OFFERED;
545		else
546			ifctx->state = IF_BOOTP_RESOLVED;	/* Fallback */
547	} else if (ifctx->state == IF_DHCP_OFFERED &&
548		   ifctx->dhcpquerytype == DHCP_REQUEST)
549		ifctx->state = IF_DHCP_RESOLVED;
550
551
552	if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
553	    ifctx->state != IF_BOOTP_RESOLVED) {
554		p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
555			       ifctx->replylen, TAG_DHCP_SERVERID);
556		if (p != NULL && gctx->tmptag.taglen == 4) {
557			memcpy(&ifctx->dhcpserver, p, 4);
558			ifctx->gotdhcpserver = 1;
559		} else
560			ifctx->gotdhcpserver = 0;
561		return 1;
562	}
563
564	ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
565					 ifctx->replylen,
566					 TAG_ROOT) != NULL);
567	ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
568				   ifctx->replylen,
569				   TAG_ROUTERS) != NULL);
570	ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
571					ifctx->replylen,
572					TAG_SUBNETMASK) != NULL);
573	return 1;
574}
575
576static int
577bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
578{
579	struct sockaddr_in *sin, dst;
580	struct uio auio;
581	struct sockopt sopt;
582	struct iovec aio;
583	int error, on, rcvflg, timo, len;
584	time_t atimo;
585	time_t rtimo;
586	struct timeval tv;
587	struct bootpc_ifcontext *ifctx;
588	int outstanding;
589	int gotrootpath;
590	int retry;
591	const char *s;
592
593	tv.tv_sec = 1;
594	tv.tv_usec = 0;
595	bzero(&sopt, sizeof(sopt));
596	sopt.sopt_dir = SOPT_SET;
597	sopt.sopt_level = SOL_SOCKET;
598	sopt.sopt_name = SO_RCVTIMEO;
599	sopt.sopt_val = &tv;
600	sopt.sopt_valsize = sizeof tv;
601
602	error = sosetopt(bootp_so, &sopt);
603	if (error != 0)
604		goto out;
605
606	/*
607	 * Enable broadcast.
608	 */
609	on = 1;
610	sopt.sopt_name = SO_BROADCAST;
611	sopt.sopt_val = &on;
612	sopt.sopt_valsize = sizeof on;
613
614	error = sosetopt(bootp_so, &sopt);
615	if (error != 0)
616		goto out;
617
618	/*
619	 * Disable routing.
620	 */
621
622	on = 1;
623	sopt.sopt_name = SO_DONTROUTE;
624	sopt.sopt_val = &on;
625	sopt.sopt_valsize = sizeof on;
626
627	error = sosetopt(bootp_so, &sopt);
628	if (error != 0)
629		goto out;
630
631	/*
632	 * Bind the local endpoint to a bootp client port.
633	 */
634	sin = &dst;
635	clear_sinaddr(sin);
636	sin->sin_port = htons(IPPORT_BOOTPC);
637	error = sobind(bootp_so, (struct sockaddr *)sin, td);
638	if (error != 0) {
639		printf("bind failed\n");
640		goto out;
641	}
642
643	/*
644	 * Setup socket address for the server.
645	 */
646	sin = &dst;
647	clear_sinaddr(sin);
648	sin->sin_addr.s_addr = INADDR_BROADCAST;
649	sin->sin_port = htons(IPPORT_BOOTPS);
650
651	/*
652	 * Send it, repeatedly, until a reply is received,
653	 * but delay each re-send by an increasing amount.
654	 * If the delay hits the maximum, start complaining.
655	 */
656	timo = 0;
657	rtimo = 0;
658	for (;;) {
659
660		outstanding = 0;
661		gotrootpath = 0;
662
663		STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
664			if (bootpc_ifctx_isresolved(ifctx) != 0 &&
665			    bootpc_tag(&gctx->tmptag, &ifctx->reply,
666				       ifctx->replylen,
667				       TAG_ROOT) != NULL)
668				gotrootpath = 1;
669		}
670
671		STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
672			struct in_aliasreq *ifra = &ifctx->iareq;
673			sin = (struct sockaddr_in *)&ifra->ifra_mask;
674
675			ifctx->outstanding = 0;
676			if (bootpc_ifctx_isresolved(ifctx)  != 0 &&
677			    gotrootpath != 0) {
678				continue;
679			}
680			if (bootpc_ifctx_isfailed(ifctx) != 0)
681				continue;
682
683			outstanding++;
684			ifctx->outstanding = 1;
685
686			/* Proceed to next step in DHCP negotiation */
687			if ((ifctx->state == IF_DHCP_OFFERED &&
688			     ifctx->dhcpquerytype != DHCP_REQUEST) ||
689			    (ifctx->state == IF_DHCP_UNRESOLVED &&
690			     ifctx->dhcpquerytype != DHCP_DISCOVER) ||
691			    (ifctx->state == IF_BOOTP_UNRESOLVED &&
692			     ifctx->dhcpquerytype != DHCP_NOMSG)) {
693				ifctx->sentmsg = 0;
694				bootpc_compose_query(ifctx, td);
695			}
696
697			/* Send BOOTP request (or re-send). */
698
699			if (ifctx->sentmsg == 0) {
700				switch(ifctx->dhcpquerytype) {
701				case DHCP_DISCOVER:
702					s = "DHCP Discover";
703					break;
704				case DHCP_REQUEST:
705					s = "DHCP Request";
706					break;
707				case DHCP_NOMSG:
708				default:
709					s = "BOOTP Query";
710					break;
711				}
712				printf("Sending %s packet from "
713				       "interface %s (%*D)\n",
714				       s,
715				       ifctx->ireq.ifr_name,
716				       ifctx->sdl->sdl_alen,
717				       (unsigned char *) LLADDR(ifctx->sdl),
718				       ":");
719				ifctx->sentmsg = 1;
720			}
721
722			aio.iov_base = (caddr_t) &ifctx->call;
723			aio.iov_len = sizeof(ifctx->call);
724
725			auio.uio_iov = &aio;
726			auio.uio_iovcnt = 1;
727			auio.uio_segflg = UIO_SYSSPACE;
728			auio.uio_rw = UIO_WRITE;
729			auio.uio_offset = 0;
730			auio.uio_resid = sizeof(ifctx->call);
731			auio.uio_td = td;
732
733			/* Set netmask to 0.0.0.0 */
734			clear_sinaddr(sin);
735			error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
736			    td);
737			if (error != 0)
738				panic("%s: SIOCAIFADDR, error=%d", __func__,
739				    error);
740
741			error = sosend(bootp_so, (struct sockaddr *) &dst,
742				       &auio, NULL, NULL, 0, td);
743			if (error != 0)
744				printf("%s: sosend: %d state %08x\n", __func__,
745				    error, (int )bootp_so->so_state);
746
747			/* Set netmask to 255.0.0.0 */
748			sin->sin_addr.s_addr = htonl(IN_CLASSA_NET);
749			error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
750			    td);
751			if (error != 0)
752				panic("%s: SIOCAIFADDR, error=%d", __func__,
753				    error);
754		}
755
756		if (outstanding == 0 &&
757		    (rtimo == 0 || time_second >= rtimo)) {
758			error = 0;
759			goto out;
760		}
761
762		/* Determine new timeout. */
763		if (timo < MAX_RESEND_DELAY)
764			timo++;
765		else {
766			printf("DHCP/BOOTP timeout for server ");
767			print_sin_addr(&dst);
768			printf("\n");
769		}
770
771		/*
772		 * Wait for up to timo seconds for a reply.
773		 * The socket receive timeout was set to 1 second.
774		 */
775		atimo = timo + time_second;
776		while (time_second < atimo) {
777			aio.iov_base = (caddr_t) &gctx->reply;
778			aio.iov_len = sizeof(gctx->reply);
779
780			auio.uio_iov = &aio;
781			auio.uio_iovcnt = 1;
782			auio.uio_segflg = UIO_SYSSPACE;
783			auio.uio_rw = UIO_READ;
784			auio.uio_offset = 0;
785			auio.uio_resid = sizeof(gctx->reply);
786			auio.uio_td = td;
787
788			rcvflg = 0;
789			error = soreceive(bootp_so, NULL, &auio,
790					  NULL, NULL, &rcvflg);
791			gctx->secs = time_second - gctx->starttime;
792			STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
793				if (bootpc_ifctx_isresolved(ifctx) != 0 ||
794				    bootpc_ifctx_isfailed(ifctx) != 0)
795					continue;
796
797				ifctx->call.secs = htons(gctx->secs);
798			}
799			if (error == EWOULDBLOCK)
800				continue;
801			if (error != 0)
802				goto out;
803			len = sizeof(gctx->reply) - auio.uio_resid;
804
805			/* Do we have the required number of bytes ? */
806			if (len < BOOTP_MIN_LEN)
807				continue;
808			gctx->replylen = len;
809
810			/* Is it a reply? */
811			if (gctx->reply.op != BOOTP_REPLY)
812				continue;
813
814			/* Is this an answer to our query */
815			STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
816				if (gctx->reply.xid != ifctx->call.xid)
817					continue;
818
819				/* Same HW address size ? */
820				if (gctx->reply.hlen != ifctx->call.hlen)
821					continue;
822
823				/* Correct HW address ? */
824				if (bcmp(gctx->reply.chaddr,
825					 ifctx->call.chaddr,
826					 ifctx->call.hlen) != 0)
827					continue;
828
829				break;
830			}
831
832			if (ifctx != NULL) {
833				s =  bootpc_tag(&gctx->tmptag,
834						&gctx->reply,
835						gctx->replylen,
836						TAG_DHCP_MSGTYPE);
837				if (s != NULL) {
838					switch (*s) {
839					case DHCP_OFFER:
840						s = "DHCP Offer";
841						break;
842					case DHCP_ACK:
843						s = "DHCP Ack";
844						break;
845					default:
846						s = "DHCP (unexpected)";
847						break;
848					}
849				} else
850					s = "BOOTP Reply";
851
852				printf("Received %s packet"
853				       " on %s from ",
854				       s,
855				       ifctx->ireq.ifr_name);
856				print_in_addr(gctx->reply.siaddr);
857				if (gctx->reply.giaddr.s_addr !=
858				    htonl(INADDR_ANY)) {
859					printf(" via ");
860					print_in_addr(gctx->reply.giaddr);
861				}
862				if (bootpc_received(gctx, ifctx) != 0) {
863					printf(" (accepted)");
864					if (ifctx->outstanding) {
865						ifctx->outstanding = 0;
866						outstanding--;
867					}
868					/* Network settle delay */
869					if (outstanding == 0)
870						atimo = time_second +
871							BOOTP_SETTLE_DELAY;
872				} else
873					printf(" (ignored)");
874				if (ifctx->gotrootpath ||
875				    gctx->any_root_overrides) {
876					gotrootpath = 1;
877					rtimo = time_second +
878						BOOTP_SETTLE_DELAY;
879					if (ifctx->gotrootpath)
880						printf(" (got root path)");
881				}
882				printf("\n");
883			}
884		} /* while secs */
885#ifdef BOOTP_TIMEOUT
886		if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
887			break;
888#endif
889		/* Force a retry if halfway in DHCP negotiation */
890		retry = 0;
891		STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
892			if (ifctx->state == IF_DHCP_OFFERED) {
893				if (ifctx->dhcpquerytype == DHCP_DISCOVER)
894					retry = 1;
895				else
896					ifctx->state = IF_DHCP_UNRESOLVED;
897			}
898
899		if (retry != 0)
900			continue;
901
902		if (gotrootpath != 0) {
903			gctx->gotrootpath = gotrootpath;
904			if (rtimo != 0 && time_second >= rtimo)
905				break;
906		}
907	} /* forever send/receive */
908
909	/*
910	 * XXX: These are errors of varying seriousness being silently
911	 * ignored
912	 */
913
914	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
915		if (bootpc_ifctx_isresolved(ifctx) == 0) {
916			printf("%s timeout for interface %s\n",
917			       ifctx->dhcpquerytype != DHCP_NOMSG ?
918			       "DHCP" : "BOOTP",
919			       ifctx->ireq.ifr_name);
920		}
921
922	if (gctx->gotrootpath != 0) {
923#if 0
924		printf("Got a root path, ignoring remaining timeout\n");
925#endif
926		error = 0;
927		goto out;
928	}
929#ifndef BOOTP_NFSROOT
930	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
931		if (bootpc_ifctx_isresolved(ifctx) != 0) {
932			error = 0;
933			goto out;
934		}
935#endif
936	error = ETIMEDOUT;
937
938out:
939	return (error);
940}
941
942static void
943bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
944{
945	struct ifreq *ifr;
946	struct in_aliasreq *ifra;
947	struct sockaddr_in *sin;
948	int error;
949
950	ifr = &ifctx->ireq;
951	ifra = &ifctx->iareq;
952
953	/*
954	 * Bring up the interface.
955	 *
956	 * Get the old interface flags and or IFF_UP into them; if
957	 * IFF_UP set blindly, interface selection can be clobbered.
958	 */
959	error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
960	if (error != 0)
961		panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
962	ifr->ifr_flags |= IFF_UP;
963	error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
964	if (error != 0)
965		panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
966
967	/*
968	 * Do enough of ifconfig(8) so that the chosen interface
969	 * can talk to the servers. Set address to 0.0.0.0/8 and
970	 * broadcast address to local broadcast.
971	 */
972	sin = (struct sockaddr_in *)&ifra->ifra_addr;
973	clear_sinaddr(sin);
974	sin = (struct sockaddr_in *)&ifra->ifra_mask;
975	clear_sinaddr(sin);
976	sin->sin_addr.s_addr = htonl(IN_CLASSA_NET);
977	sin = (struct sockaddr_in *)&ifra->ifra_broadaddr;
978	clear_sinaddr(sin);
979	sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
980	error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
981	if (error != 0)
982		panic("%s: SIOCAIFADDR, error=%d", __func__, error);
983}
984
985static void
986bootpc_shutdown_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
987{
988	struct ifreq *ifr;
989	struct sockaddr_in *sin;
990	int error;
991
992	ifr = &ifctx->ireq;
993
994	printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
995	error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
996	if (error != 0)
997		panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
998	ifr->ifr_flags &= ~IFF_UP;
999	error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
1000	if (error != 0)
1001		panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
1002
1003	sin = (struct sockaddr_in *) &ifr->ifr_addr;
1004	clear_sinaddr(sin);
1005	error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
1006	if (error != 0)
1007		panic("%s: SIOCDIFADDR, error=%d", __func__, error);
1008}
1009
1010static int
1011bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
1012    struct bootpc_globalcontext *gctx, struct thread *td)
1013{
1014	int error;
1015	struct sockaddr_in defdst;
1016	struct sockaddr_in defmask;
1017	struct sockaddr_in *sin;
1018	struct ifreq *ifr;
1019	struct in_aliasreq *ifra;
1020	struct sockaddr_in *myaddr;
1021	struct sockaddr_in *netmask;
1022	struct sockaddr_in *gw;
1023
1024	ifr = &ifctx->ireq;
1025	ifra = &ifctx->iareq;
1026	myaddr = &ifctx->myaddr;
1027	netmask = &ifctx->netmask;
1028	gw = &ifctx->gw;
1029
1030	if (bootpc_ifctx_isresolved(ifctx) == 0) {
1031		/* Shutdown interfaces where BOOTP failed */
1032		bootpc_shutdown_interface(ifctx, td);
1033		return (0);
1034	}
1035
1036	printf("Adjusted interface %s", ifctx->ireq.ifr_name);
1037
1038	/* Do BOOTP interface options */
1039	if (ifctx->mtu != 0) {
1040		printf(" (MTU=%d%s)", ifctx->mtu,
1041		    (ifctx->mtu > 1514) ? "/JUMBO" : "");
1042		ifr->ifr_mtu = ifctx->mtu;
1043		error = ifioctl(bootp_so, SIOCSIFMTU, (caddr_t) ifr, td);
1044		if (error != 0)
1045			panic("%s: SIOCSIFMTU, error=%d", __func__, error);
1046	}
1047	printf("\n");
1048
1049	/*
1050	 * Do enough of ifconfig(8) so that the chosen interface
1051	 * can talk to the servers.  (just set the address)
1052	 */
1053	sin = (struct sockaddr_in *) &ifr->ifr_addr;
1054	clear_sinaddr(sin);
1055	error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
1056	if (error != 0)
1057		panic("%s: SIOCDIFADDR, error=%d", __func__, error);
1058
1059	bcopy(myaddr, &ifra->ifra_addr, sizeof(*myaddr));
1060	bcopy(netmask, &ifra->ifra_mask, sizeof(*netmask));
1061	clear_sinaddr(&ifra->ifra_broadaddr);
1062	ifra->ifra_broadaddr.sin_addr.s_addr = myaddr->sin_addr.s_addr |
1063	    ~netmask->sin_addr.s_addr;
1064
1065	error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
1066	if (error != 0)
1067		panic("%s: SIOCAIFADDR, error=%d", __func__, error);
1068
1069	/* Add new default route */
1070
1071	if (ifctx->gotgw != 0 || gctx->gotgw == 0) {
1072		clear_sinaddr(&defdst);
1073		clear_sinaddr(&defmask);
1074		/* XXX MRT just table 0 */
1075		error = rtrequest_fib(RTM_ADD,
1076		    (struct sockaddr *) &defdst, (struct sockaddr *) gw,
1077		    (struct sockaddr *) &defmask,
1078		    (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL, RT_DEFAULT_FIB);
1079		if (error != 0) {
1080			printf("%s: RTM_ADD, error=%d\n", __func__, error);
1081			return (error);
1082		}
1083	}
1084
1085	return (0);
1086}
1087
1088static int
1089setfs(struct sockaddr_in *addr, char *path, char *p,
1090    const struct in_addr *siaddr)
1091{
1092
1093	if (getip(&p, &addr->sin_addr) == 0) {
1094		if (siaddr != NULL && *p == '/')
1095			bcopy(siaddr, &addr->sin_addr, sizeof(struct in_addr));
1096		else
1097			return 0;
1098	} else {
1099		if (*p != ':')
1100			return 0;
1101		p++;
1102	}
1103
1104	addr->sin_len = sizeof(struct sockaddr_in);
1105	addr->sin_family = AF_INET;
1106
1107	strlcpy(path, p, MNAMELEN);
1108	return 1;
1109}
1110
1111static int
1112getip(char **ptr, struct in_addr *addr)
1113{
1114	char *p;
1115	unsigned int ip;
1116	int val;
1117
1118	p = *ptr;
1119	ip = 0;
1120	if (((val = getdec(&p)) < 0) || (val > 255))
1121		return 0;
1122	ip = val << 24;
1123	if (*p != '.')
1124		return 0;
1125	p++;
1126	if (((val = getdec(&p)) < 0) || (val > 255))
1127		return 0;
1128	ip |= (val << 16);
1129	if (*p != '.')
1130		return 0;
1131	p++;
1132	if (((val = getdec(&p)) < 0) || (val > 255))
1133		return 0;
1134	ip |= (val << 8);
1135	if (*p != '.')
1136		return 0;
1137	p++;
1138	if (((val = getdec(&p)) < 0) || (val > 255))
1139		return 0;
1140	ip |= val;
1141
1142	addr->s_addr = htonl(ip);
1143	*ptr = p;
1144	return 1;
1145}
1146
1147static int
1148getdec(char **ptr)
1149{
1150	char *p;
1151	int ret;
1152
1153	p = *ptr;
1154	ret = 0;
1155	if ((*p < '0') || (*p > '9'))
1156		return -1;
1157	while ((*p >= '0') && (*p <= '9')) {
1158		ret = ret * 10 + (*p - '0');
1159		p++;
1160	}
1161	*ptr = p;
1162	return ret;
1163}
1164
1165static void
1166mountopts(struct nfs_args *args, char *p)
1167{
1168	args->version = NFS_ARGSVERSION;
1169	args->rsize = BOOTP_BLOCKSIZE;
1170	args->wsize = BOOTP_BLOCKSIZE;
1171	args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
1172	args->sotype = SOCK_DGRAM;
1173	if (p != NULL)
1174		nfs_parse_options(p, args);
1175}
1176
1177static int
1178xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
1179{
1180	struct mbuf *m;
1181	int alignedlen;
1182
1183	m = *mptr;
1184	alignedlen = ( len + 3 ) & ~3;
1185
1186	if (m->m_len < alignedlen) {
1187		m = m_pullup(m, alignedlen);
1188		if (m == NULL) {
1189			*mptr = NULL;
1190			return EBADRPC;
1191		}
1192	}
1193	bcopy(mtod(m, u_char *), buf, len);
1194	m_adj(m, alignedlen);
1195	*mptr = m;
1196	return 0;
1197}
1198
1199static int
1200xdr_int_decode(struct mbuf **mptr, int *iptr)
1201{
1202	u_int32_t i;
1203
1204	if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
1205		return EBADRPC;
1206	*iptr = fxdr_unsigned(u_int32_t, i);
1207	return 0;
1208}
1209
1210static void
1211print_sin_addr(struct sockaddr_in *sin)
1212{
1213
1214	print_in_addr(sin->sin_addr);
1215}
1216
1217static void
1218print_in_addr(struct in_addr addr)
1219{
1220	unsigned int ip;
1221
1222	ip = ntohl(addr.s_addr);
1223	printf("%d.%d.%d.%d",
1224	       ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1225}
1226
1227static void
1228bootpc_compose_query(struct bootpc_ifcontext *ifctx, struct thread *td)
1229{
1230	unsigned char *vendp;
1231	unsigned char vendor_client[64];
1232	uint32_t leasetime;
1233	uint8_t vendor_client_len;
1234
1235	ifctx->gotrootpath = 0;
1236
1237	bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1238
1239	/* bootpc part */
1240	ifctx->call.op = BOOTP_REQUEST; 	/* BOOTREQUEST */
1241	ifctx->call.htype = 1;			/* 10mb ethernet */
1242	ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1243	ifctx->call.hops = 0;
1244	if (bootpc_ifctx_isunresolved(ifctx) != 0)
1245		ifctx->xid++;
1246	ifctx->call.xid = txdr_unsigned(ifctx->xid);
1247	bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1248
1249	vendp = ifctx->call.vend;
1250	*vendp++ = 99;		/* RFC1048 cookie */
1251	*vendp++ = 130;
1252	*vendp++ = 83;
1253	*vendp++ = 99;
1254	*vendp++ = TAG_MAXMSGSIZE;
1255	*vendp++ = 2;
1256	*vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1257	*vendp++ = sizeof(struct bootp_packet) & 255;
1258
1259	snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1260		ostype, MACHINE, osrelease);
1261	vendor_client_len = strlen(vendor_client);
1262	*vendp++ = TAG_VENDOR_INDENTIFIER;
1263	*vendp++ = vendor_client_len;
1264	memcpy(vendp, vendor_client, vendor_client_len);
1265	vendp += vendor_client_len;
1266	ifctx->dhcpquerytype = DHCP_NOMSG;
1267	switch (ifctx->state) {
1268	case IF_DHCP_UNRESOLVED:
1269		*vendp++ = TAG_DHCP_MSGTYPE;
1270		*vendp++ = 1;
1271		*vendp++ = DHCP_DISCOVER;
1272		ifctx->dhcpquerytype = DHCP_DISCOVER;
1273		ifctx->gotdhcpserver = 0;
1274		break;
1275	case IF_DHCP_OFFERED:
1276		*vendp++ = TAG_DHCP_MSGTYPE;
1277		*vendp++ = 1;
1278		*vendp++ = DHCP_REQUEST;
1279		ifctx->dhcpquerytype = DHCP_REQUEST;
1280		*vendp++ = TAG_DHCP_REQ_ADDR;
1281		*vendp++ = 4;
1282		memcpy(vendp, &ifctx->reply.yiaddr, 4);
1283		vendp += 4;
1284		if (ifctx->gotdhcpserver != 0) {
1285			*vendp++ = TAG_DHCP_SERVERID;
1286			*vendp++ = 4;
1287			memcpy(vendp, &ifctx->dhcpserver, 4);
1288			vendp += 4;
1289		}
1290		*vendp++ = TAG_DHCP_LEASETIME;
1291		*vendp++ = 4;
1292		leasetime = htonl(300);
1293		memcpy(vendp, &leasetime, 4);
1294		vendp += 4;
1295		break;
1296	default:
1297		break;
1298	}
1299	*vendp = TAG_END;
1300
1301	ifctx->call.secs = 0;
1302	ifctx->call.flags = htons(0x8000); /* We need a broadcast answer */
1303}
1304
1305static int
1306bootpc_hascookie(struct bootp_packet *bp)
1307{
1308
1309	return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1310		bp->vend[2] == 83 && bp->vend[3] == 99);
1311}
1312
1313static void
1314bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1315    unsigned char *start, int len, int tag)
1316{
1317	unsigned char *j;
1318	unsigned char *ej;
1319	unsigned char code;
1320
1321	if (tctx->badtag != 0 || tctx->badopt != 0)
1322		return;
1323
1324	j = start;
1325	ej = j + len;
1326
1327	while (j < ej) {
1328		code = *j++;
1329		if (code == TAG_PAD)
1330			continue;
1331		if (code == TAG_END)
1332			return;
1333		if (j >= ej || j + *j + 1 > ej) {
1334			tctx->badopt = 1;
1335			return;
1336		}
1337		len = *j++;
1338		if (code == tag) {
1339			if (tctx->taglen + len > TAG_MAXLEN) {
1340				tctx->badtag = 1;
1341				return;
1342			}
1343			tctx->foundopt = 1;
1344			if (len > 0)
1345				memcpy(tctx->buf + tctx->taglen,
1346				       j, len);
1347			tctx->taglen += len;
1348		}
1349		if (code == TAG_OVERLOAD)
1350			tctx->overload = *j;
1351
1352		j += len;
1353	}
1354}
1355
1356static unsigned char *
1357bootpc_tag(struct bootpc_tagcontext *tctx,
1358    struct bootp_packet *bp, int len, int tag)
1359{
1360	tctx->overload = 0;
1361	tctx->badopt = 0;
1362	tctx->badtag = 0;
1363	tctx->foundopt = 0;
1364	tctx->taglen = 0;
1365
1366	if (bootpc_hascookie(bp) == 0)
1367		return NULL;
1368
1369	bootpc_tag_helper(tctx, &bp->vend[4],
1370			  (unsigned char *) bp + len - &bp->vend[4], tag);
1371
1372	if ((tctx->overload & OVERLOAD_FILE) != 0)
1373		bootpc_tag_helper(tctx,
1374				  (unsigned char *) bp->file,
1375				  sizeof(bp->file),
1376				  tag);
1377	if ((tctx->overload & OVERLOAD_SNAME) != 0)
1378		bootpc_tag_helper(tctx,
1379				  (unsigned char *) bp->sname,
1380				  sizeof(bp->sname),
1381				  tag);
1382
1383	if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1384		return NULL;
1385	tctx->buf[tctx->taglen] = '\0';
1386	return tctx->buf;
1387}
1388
1389static void
1390bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1391    struct bootpc_globalcontext *gctx)
1392{
1393	char *p, *s;
1394	unsigned int ip;
1395
1396	ifctx->gotgw = 0;
1397	ifctx->gotnetmask = 0;
1398
1399	clear_sinaddr(&ifctx->myaddr);
1400	clear_sinaddr(&ifctx->netmask);
1401	clear_sinaddr(&ifctx->gw);
1402
1403	ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1404
1405	ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1406
1407	printf("%s at ", ifctx->ireq.ifr_name);
1408	print_sin_addr(&ifctx->myaddr);
1409	printf(" server ");
1410	print_in_addr(ifctx->reply.siaddr);
1411
1412	ifctx->gw.sin_addr = ifctx->reply.giaddr;
1413	if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1414		printf(" via gateway ");
1415		print_in_addr(ifctx->reply.giaddr);
1416	}
1417
1418	/* This call used for the side effect (overload flag) */
1419	(void) bootpc_tag(&gctx->tmptag,
1420			  &ifctx->reply, ifctx->replylen, TAG_END);
1421
1422	if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1423		if (ifctx->reply.sname[0] != '\0')
1424			printf(" server name %s", ifctx->reply.sname);
1425	if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1426		if (ifctx->reply.file[0] != '\0')
1427			printf(" boot file %s", ifctx->reply.file);
1428
1429	printf("\n");
1430
1431	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1432		       TAG_SUBNETMASK);
1433	if (p != NULL) {
1434		if (gctx->tag.taglen != 4)
1435			panic("bootpc: subnet mask len is %d",
1436			      gctx->tag.taglen);
1437		bcopy(p, &ifctx->netmask.sin_addr, 4);
1438		ifctx->gotnetmask = 1;
1439		printf("subnet mask ");
1440		print_sin_addr(&ifctx->netmask);
1441		printf(" ");
1442	}
1443
1444	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1445		       TAG_ROUTERS);
1446	if (p != NULL) {
1447		/* Routers */
1448		if (gctx->tag.taglen % 4)
1449			panic("bootpc: Router Len is %d", gctx->tag.taglen);
1450		if (gctx->tag.taglen > 0) {
1451			bcopy(p, &ifctx->gw.sin_addr, 4);
1452			printf("router ");
1453			print_sin_addr(&ifctx->gw);
1454			printf(" ");
1455			ifctx->gotgw = 1;
1456			gctx->gotgw = 1;
1457		}
1458	}
1459
1460	/*
1461	 * Choose a root filesystem.  If a value is forced in the environment
1462	 * and it contains "nfs:", use it unconditionally.  Otherwise, if the
1463	 * kernel is compiled with the ROOTDEVNAME option, then use it if:
1464	 *  - The server doesn't provide a pathname.
1465	 *  - The boothowto flags include RB_DFLTROOT (user said to override
1466	 *    the server value).
1467	 */
1468	p = NULL;
1469	if ((s = getenv("vfs.root.mountfrom")) != NULL) {
1470		if ((p = strstr(s, "nfs:")) != NULL)
1471			p = strdup(p + 4, M_TEMP);
1472		freeenv(s);
1473	}
1474	if (p == NULL) {
1475		p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1476		       TAG_ROOT);
1477	}
1478#ifdef ROOTDEVNAME
1479	if ((p == NULL || (boothowto & RB_DFLTROOT) != 0) &&
1480	    (p = strstr(ROOTDEVNAME, "nfs:")) != NULL) {
1481		p += 4;
1482	}
1483#endif
1484	if (p != NULL) {
1485		if (gctx->setrootfs != NULL) {
1486			printf("rootfs %s (ignored) ", p);
1487		} else 	if (setfs(&nd->root_saddr,
1488				  nd->root_hostnam, p, &ifctx->reply.siaddr)) {
1489			if (*p == '/') {
1490				printf("root_server ");
1491				print_sin_addr(&nd->root_saddr);
1492				printf(" ");
1493			}
1494			printf("rootfs %s ", p);
1495			gctx->gotrootpath = 1;
1496			ifctx->gotrootpath = 1;
1497			gctx->setrootfs = ifctx;
1498
1499			p = bootpc_tag(&gctx->tag, &ifctx->reply,
1500				       ifctx->replylen,
1501				       TAG_ROOTOPTS);
1502			if (p != NULL) {
1503				mountopts(&nd->root_args, p);
1504				printf("rootopts %s ", p);
1505			}
1506		} else
1507			panic("Failed to set rootfs to %s", p);
1508	}
1509
1510	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1511		       TAG_HOSTNAME);
1512	if (p != NULL) {
1513		if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1514			panic("bootpc: hostname >= %d bytes",
1515			      MAXHOSTNAMELEN);
1516		if (gctx->sethostname != NULL) {
1517			printf("hostname %s (ignored) ", p);
1518		} else {
1519			strcpy(nd->my_hostnam, p);
1520			mtx_lock(&prison0.pr_mtx);
1521			strcpy(prison0.pr_hostname, p);
1522			mtx_unlock(&prison0.pr_mtx);
1523			printf("hostname %s ", p);
1524			gctx->sethostname = ifctx;
1525		}
1526	}
1527	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1528			TAG_COOKIE);
1529	if (p != NULL) {        /* store in a sysctl variable */
1530		int i, l = sizeof(bootp_cookie) - 1;
1531		for (i = 0; i < l && p[i] != '\0'; i++)
1532			bootp_cookie[i] = p[i];
1533		p[i] = '\0';
1534	}
1535
1536	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1537		       TAG_INTF_MTU);
1538	if (p != NULL) {
1539		ifctx->mtu = be16dec(p);
1540	}
1541
1542	printf("\n");
1543
1544	if (ifctx->gotnetmask == 0) {
1545		if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1546			ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1547		else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1548			ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1549		else
1550			ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1551	}
1552	if (ifctx->gotgw == 0) {
1553		/* Use proxyarp */
1554		ifctx->gw.sin_addr.s_addr = ifctx->myaddr.sin_addr.s_addr;
1555	}
1556}
1557
1558void
1559bootpc_init(void)
1560{
1561	struct bootpc_ifcontext *ifctx;		/* Interface BOOTP contexts */
1562	struct bootpc_globalcontext *gctx; 	/* Global BOOTP context */
1563	struct ifnet *ifp;
1564	struct sockaddr_dl *sdl;
1565	struct ifaddr *ifa;
1566	int error;
1567#ifndef BOOTP_WIRED_TO
1568	int ifcnt;
1569#endif
1570	struct nfsv3_diskless *nd;
1571	struct thread *td;
1572	int timeout;
1573	int delay;
1574
1575	timeout = BOOTP_IFACE_WAIT_TIMEOUT * hz;
1576	delay = hz / 10;
1577
1578	nd = &nfsv3_diskless;
1579	td = curthread;
1580
1581	/*
1582	 * If already filled in, don't touch it here
1583	 */
1584	if (nfs_diskless_valid != 0)
1585		return;
1586
1587	gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK | M_ZERO);
1588	STAILQ_INIT(&gctx->interfaces);
1589	gctx->xid = ~0xFFFF;
1590	gctx->starttime = time_second;
1591
1592	/*
1593	 * If ROOTDEVNAME is defined or vfs.root.mountfrom is set then we have
1594	 * root-path overrides that can potentially let us boot even if we don't
1595	 * get a root path from the server, so we can treat that as a non-error.
1596	 */
1597#ifdef ROOTDEVNAME
1598	gctx->any_root_overrides = 1;
1599#else
1600	gctx->any_root_overrides = testenv("vfs.root.mountfrom");
1601#endif
1602
1603	/*
1604	 * Find a network interface.
1605	 */
1606	CURVNET_SET(TD_TO_VNET(td));
1607#ifdef BOOTP_WIRED_TO
1608	printf("%s: wired to interface '%s'\n", __func__,
1609	       __XSTRING(BOOTP_WIRED_TO));
1610	allocifctx(gctx);
1611#else
1612	/*
1613	 * Preallocate interface context storage, if another interface
1614	 * attaches and wins the race, it won't be eligible for bootp.
1615	 */
1616	ifcnt = 0;
1617	IFNET_RLOCK();
1618	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1619		if ((ifp->if_flags &
1620		     (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1621		    IFF_BROADCAST)
1622			continue;
1623		switch (ifp->if_alloctype) {
1624			case IFT_ETHER:
1625			case IFT_FDDI:
1626			case IFT_ISO88025:
1627				break;
1628			default:
1629				continue;
1630		}
1631		ifcnt++;
1632	}
1633	IFNET_RUNLOCK();
1634	if (ifcnt == 0)
1635		panic("%s: no eligible interfaces", __func__);
1636	for (; ifcnt > 0; ifcnt--)
1637		allocifctx(gctx);
1638#endif
1639
1640retry:
1641	ifctx = STAILQ_FIRST(&gctx->interfaces);
1642	IFNET_RLOCK();
1643	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1644		if (ifctx == NULL)
1645			break;
1646#ifdef BOOTP_WIRED_TO
1647		if (strcmp(ifp->if_xname, __XSTRING(BOOTP_WIRED_TO)) != 0)
1648			continue;
1649#else
1650		if ((ifp->if_flags &
1651		     (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1652		    IFF_BROADCAST)
1653			continue;
1654		switch (ifp->if_alloctype) {
1655			case IFT_ETHER:
1656			case IFT_FDDI:
1657			case IFT_ISO88025:
1658				break;
1659			default:
1660				continue;
1661		}
1662#endif
1663		strlcpy(ifctx->ireq.ifr_name, ifp->if_xname,
1664		    sizeof(ifctx->ireq.ifr_name));
1665		ifctx->ifp = ifp;
1666
1667		/* Get HW address */
1668		sdl = NULL;
1669		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1670			if (ifa->ifa_addr->sa_family == AF_LINK) {
1671				sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1672				if (sdl->sdl_type == IFT_ETHER)
1673					break;
1674			}
1675		if (sdl == NULL)
1676			panic("bootpc: Unable to find HW address for %s",
1677			    ifctx->ireq.ifr_name);
1678		ifctx->sdl = sdl;
1679
1680		ifctx = STAILQ_NEXT(ifctx, next);
1681	}
1682	IFNET_RUNLOCK();
1683	CURVNET_RESTORE();
1684
1685	if (STAILQ_EMPTY(&gctx->interfaces) ||
1686	    STAILQ_FIRST(&gctx->interfaces)->ifp == NULL) {
1687		if (timeout > 0) {
1688			pause("bootpc", delay);
1689			timeout -= delay;
1690			goto retry;
1691		}
1692#ifdef BOOTP_WIRED_TO
1693		panic("%s: Could not find interface specified "
1694		      "by BOOTP_WIRED_TO: "
1695		      __XSTRING(BOOTP_WIRED_TO), __func__);
1696#else
1697		panic("%s: no suitable interface", __func__);
1698#endif
1699	}
1700
1701	error = socreate(AF_INET, &bootp_so, SOCK_DGRAM, 0, td->td_ucred, td);
1702	if (error != 0)
1703		panic("%s: socreate, error=%d", __func__, error);
1704
1705	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1706		bootpc_fakeup_interface(ifctx, td);
1707
1708	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1709		bootpc_compose_query(ifctx, td);
1710
1711	error = bootpc_call(gctx, td);
1712	if (error != 0) {
1713		printf("BOOTP call failed\n");
1714	}
1715
1716	mountopts(&nd->root_args, NULL);
1717
1718	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1719		if (bootpc_ifctx_isresolved(ifctx) != 0)
1720			bootpc_decode_reply(nd, ifctx, gctx);
1721
1722#ifdef BOOTP_NFSROOT
1723	if (gctx->gotrootpath == 0 && gctx->any_root_overrides == 0)
1724		panic("bootpc: No root path offered");
1725#endif
1726
1727	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1728		bootpc_adjust_interface(ifctx, gctx, td);
1729
1730	soclose(bootp_so);
1731
1732	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1733		if (ifctx->gotrootpath != 0)
1734			break;
1735	if (ifctx == NULL) {
1736		STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1737			if (bootpc_ifctx_isresolved(ifctx) != 0)
1738				break;
1739	}
1740	if (ifctx == NULL)
1741		goto out;
1742
1743	if (gctx->gotrootpath != 0) {
1744
1745		setenv("boot.netif.name", ifctx->ifp->if_xname);
1746
1747		error = md_mount(&nd->root_saddr, nd->root_hostnam,
1748				 nd->root_fh, &nd->root_fhsize,
1749				 &nd->root_args, td);
1750		if (error != 0) {
1751			if (gctx->any_root_overrides == 0)
1752				panic("nfs_boot: mount root, error=%d", error);
1753			else
1754				goto out;
1755		}
1756		rootdevnames[0] = "nfs:";
1757#ifdef NFSCLIENT
1758		rootdevnames[1] = "oldnfs:";
1759#endif
1760		nfs_diskless_valid = 3;
1761	}
1762
1763	strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1764	bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1765	bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1766	((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1767		ifctx->myaddr.sin_addr.s_addr |
1768		~ ifctx->netmask.sin_addr.s_addr;
1769	bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1770
1771out:
1772	while((ifctx = STAILQ_FIRST(&gctx->interfaces)) != NULL) {
1773		STAILQ_REMOVE_HEAD(&gctx->interfaces, next);
1774		free(ifctx, M_TEMP);
1775	}
1776	free(gctx, M_TEMP);
1777}
1778
1779/*
1780 * RPC: mountd/mount
1781 * Given a server pathname, get an NFS file handle.
1782 * Also, sets sin->sin_port to the NFS service port.
1783 */
1784static int
1785md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp, int *fhsizep,
1786    struct nfs_args *args, struct thread *td)
1787{
1788	struct mbuf *m;
1789	int error;
1790	int authunixok;
1791	int authcount;
1792	int authver;
1793
1794#define	RPCPROG_MNT	100005
1795#define	RPCMNT_VER1	1
1796#define RPCMNT_VER3	3
1797#define	RPCMNT_MOUNT	1
1798#define	AUTH_SYS	1		/* unix style (uid, gids) */
1799#define AUTH_UNIX	AUTH_SYS
1800
1801	/* XXX honor v2/v3 flags in args->flags? */
1802#ifdef BOOTP_NFSV3
1803	/* First try NFS v3 */
1804	/* Get port number for MOUNTD. */
1805	error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1806			     &mdsin->sin_port, td);
1807	if (error == 0) {
1808		m = xdr_string_encode(path, strlen(path));
1809
1810		/* Do RPC to mountd. */
1811		error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1812				  RPCMNT_MOUNT, &m, NULL, td);
1813	}
1814	if (error == 0) {
1815		args->flags |= NFSMNT_NFSV3;
1816	} else {
1817#endif
1818		/* Fallback to NFS v2 */
1819
1820		/* Get port number for MOUNTD. */
1821		error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1822				     &mdsin->sin_port, td);
1823		if (error != 0)
1824			return error;
1825
1826		m = xdr_string_encode(path, strlen(path));
1827
1828		/* Do RPC to mountd. */
1829		error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1830				  RPCMNT_MOUNT, &m, NULL, td);
1831		if (error != 0)
1832			return error;	/* message already freed */
1833
1834#ifdef BOOTP_NFSV3
1835	}
1836#endif
1837
1838	if (xdr_int_decode(&m, &error) != 0 || error != 0)
1839		goto bad;
1840
1841	if ((args->flags & NFSMNT_NFSV3) != 0) {
1842		if (xdr_int_decode(&m, fhsizep) != 0 ||
1843		    *fhsizep > NFSX_V3FHMAX ||
1844		    *fhsizep <= 0)
1845			goto bad;
1846	} else
1847		*fhsizep = NFSX_V2FH;
1848
1849	if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
1850		goto bad;
1851
1852	if (args->flags & NFSMNT_NFSV3) {
1853		if (xdr_int_decode(&m, &authcount) != 0)
1854			goto bad;
1855		authunixok = 0;
1856		if (authcount < 0 || authcount > 100)
1857			goto bad;
1858		while (authcount > 0) {
1859			if (xdr_int_decode(&m, &authver) != 0)
1860				goto bad;
1861			if (authver == AUTH_UNIX)
1862				authunixok = 1;
1863			authcount--;
1864		}
1865		if (authunixok == 0)
1866			goto bad;
1867	}
1868
1869	/* Set port number for NFS use. */
1870	error = krpc_portmap(mdsin, NFS_PROG,
1871			     (args->flags &
1872			      NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
1873			     &mdsin->sin_port, td);
1874
1875	goto out;
1876
1877bad:
1878	error = EBADRPC;
1879
1880out:
1881	m_freem(m);
1882	return error;
1883}
1884
1885SYSINIT(bootp_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, bootpc_init, NULL);
1886