2 * Copyright (c) 1995 Gordon Ross, Adam Glass
3 * Copyright (c) 1992 Regents of the University of California.
6 * This software was developed by the Computer Systems Engineering group
7 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
8 * contributed to Berkeley.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Lawrence Berkeley Laboratory and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * $NetBSD: krpc_subr.c,v 1.10 1995/08/08 20:43:43 gwr Exp $
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include "opt_bootp.h"
48 #include "opt_rootdevname.h"
50 #include <sys/param.h>
51 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/sockio.h>
55 #include <sys/malloc.h>
56 #include <sys/mount.h>
59 #include <sys/reboot.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/sysctl.h>
66 #include <net/if_var.h>
67 #include <net/route.h>
69 #include <net/route_var.h>
72 #include <netinet/in.h>
73 #include <netinet/in_var.h>
74 #include <net/if_types.h>
75 #include <net/if_dl.h>
78 #include <nfs/nfsproto.h>
79 #include <nfsclient/nfs.h>
80 #include <nfs/nfsdiskless.h>
82 #include <nfs/xdr_subs.h>
85 #define BOOTP_MIN_LEN 300 /* Minimum size of bootp udp packet */
87 #ifndef BOOTP_SETTLE_DELAY
88 #define BOOTP_SETTLE_DELAY 3
92 * Wait 10 seconds for interface appearance
93 * USB ethernet adapters might require some time to pop up
95 #ifndef BOOTP_IFACE_WAIT_TIMEOUT
96 #define BOOTP_IFACE_WAIT_TIMEOUT 10
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.
105 #define MAX_RESEND_DELAY 5 /* seconds */
107 /* Definitions from RFC951 */
108 struct bootp_packet {
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];
123 unsigned char vend[1222];
126 struct bootpc_ifcontext {
127 STAILQ_ENTRY(bootpc_ifcontext) next;
128 struct bootp_packet call;
129 struct bootp_packet reply;
134 struct in_aliasreq _in_alias_req;
136 #define ireq _req._ifreq
137 #define iareq _req._in_alias_req
139 struct sockaddr_dl *sdl;
140 struct sockaddr_in myaddr;
141 struct sockaddr_in netmask;
142 struct sockaddr_in gw;
158 int dhcpquerytype; /* dhcp type sent */
159 struct in_addr dhcpserver;
163 #define TAG_MAXLEN 1024
164 struct bootpc_tagcontext {
165 char buf[TAG_MAXLEN + 1];
173 struct bootpc_globalcontext {
174 STAILQ_HEAD(, bootpc_ifcontext) interfaces;
176 int any_root_overrides;
182 struct bootp_packet reply;
184 struct bootpc_ifcontext *setrootfs;
185 struct bootpc_ifcontext *sethostname;
186 struct bootpc_tagcontext tmptag;
187 struct bootpc_tagcontext tag;
190 #define IPPORT_BOOTPC 68
191 #define IPPORT_BOOTPS 67
193 #define BOOTP_REQUEST 1
194 #define BOOTP_REPLY 2
197 #define TAG_PAD 0 /* Pad option, implicit length 1 */
198 #define TAG_SUBNETMASK 1 /* RFC 950 subnet mask */
199 #define TAG_ROUTERS 3 /* Routers (in order of preference) */
200 #define TAG_HOSTNAME 12 /* Client host name */
201 #define TAG_ROOT 17 /* Root path */
203 /* DHCP specific tags */
204 #define TAG_OVERLOAD 52 /* Option Overload */
205 #define TAG_MAXMSGSIZE 57 /* Maximum DHCP Message Size */
207 #define TAG_END 255 /* End Option (i.e. no more options) */
209 /* Overload values */
210 #define OVERLOAD_FILE 1
211 #define OVERLOAD_SNAME 2
213 /* Site specific tags: */
214 #define TAG_ROOTOPTS 130
215 #define TAG_COOKIE 134 /* ascii info for userland, via sysctl */
217 #define TAG_DHCP_MSGTYPE 53
218 #define TAG_DHCP_REQ_ADDR 50
219 #define TAG_DHCP_SERVERID 54
220 #define TAG_DHCP_LEASETIME 51
222 #define TAG_VENDOR_INDENTIFIER 60
225 #define DHCP_DISCOVER 1
227 #define DHCP_REQUEST 3
230 /* NFS read/write block size */
231 #ifndef BOOTP_BLOCKSIZE
232 #define BOOTP_BLOCKSIZE 8192
235 static char bootp_cookie[128];
236 static struct socket *bootp_so;
237 SYSCTL_STRING(_kern, OID_AUTO, bootp_cookie, CTLFLAG_RD,
238 bootp_cookie, 0, "Cookie (T134) supplied by bootp server");
241 static int md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp,
242 int *fhsizep, struct nfs_args *args, struct thread *td);
243 static int setfs(struct sockaddr_in *addr, char *path, char *p,
244 const struct in_addr *siaddr);
245 static int getdec(char **ptr);
246 static int getip(char **ptr, struct in_addr *ip);
247 static void mountopts(struct nfs_args *args, char *p);
248 static int xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len);
249 static int xdr_int_decode(struct mbuf **ptr, int *iptr);
250 static void print_in_addr(struct in_addr addr);
251 static void print_sin_addr(struct sockaddr_in *addr);
252 static void clear_sinaddr(struct sockaddr_in *sin);
253 static void allocifctx(struct bootpc_globalcontext *gctx);
254 static void bootpc_compose_query(struct bootpc_ifcontext *ifctx,
256 static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx,
257 struct bootp_packet *bp, int len, int tag);
258 static void bootpc_tag_helper(struct bootpc_tagcontext *tctx,
259 unsigned char *start, int len, int tag);
262 void bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma);
263 void bootpboot_p_rtentry(struct rtentry *rt);
264 void bootpboot_p_tree(struct radix_node *rn);
265 void bootpboot_p_rtlist(void);
266 void bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa);
267 void bootpboot_p_iflist(void);
270 static int bootpc_call(struct bootpc_globalcontext *gctx,
273 static void bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
276 static int bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
277 struct bootpc_globalcontext *gctx, struct thread *td);
279 static void bootpc_decode_reply(struct nfsv3_diskless *nd,
280 struct bootpc_ifcontext *ifctx,
281 struct bootpc_globalcontext *gctx);
283 static int bootpc_received(struct bootpc_globalcontext *gctx,
284 struct bootpc_ifcontext *ifctx);
286 static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
287 static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
288 static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
291 * In order to have multiple active interfaces with address 0.0.0.0
292 * and be able to send data to a selected interface, we first set
293 * mask to /8 on all interfaces, and temporarily set it to /0 when
299 bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma)
303 printf("(sockaddr *) <null>");
306 switch (sa->sa_family) {
309 struct sockaddr_in *sin;
311 sin = (struct sockaddr_in *) sa;
315 sin = (struct sockaddr_in *) ma;
323 struct sockaddr_dl *sli;
326 sli = (struct sockaddr_dl *) sa;
327 printf("link %.*s ", sli->sdl_nlen, sli->sdl_data);
328 for (i = 0; i < sli->sdl_alen; i++) {
331 printf("%x", ((unsigned char *) LLADDR(sli))[i]);
336 printf("af%d", sa->sa_family);
341 bootpboot_p_rtentry(struct rtentry *rt)
344 bootpboot_p_sa(rt_key(rt), rt_mask(rt));
346 bootpboot_p_sa(rt->rt_gateway, NULL);
348 printf("flags %x", (unsigned short) rt->rt_flags);
349 printf(" %d", (int) rt->rt_expire);
350 printf(" %s\n", rt->rt_ifp->if_xname);
354 bootpboot_p_tree(struct radix_node *rn)
358 if (rn->rn_bit < 0) {
359 if ((rn->rn_flags & RNF_ROOT) != 0) {
361 bootpboot_p_rtentry((struct rtentry *) rn);
363 rn = rn->rn_dupedkey;
365 bootpboot_p_tree(rn->rn_left);
366 bootpboot_p_tree(rn->rn_right);
373 bootpboot_p_rtlist(void)
375 struct rib_head *rnh;
377 printf("Routing table:\n");
378 rnh = rt_tables_get_rnh(0, AF_INET);
381 RIB_RLOCK(rnh); /* could sleep XXX */
382 bootpboot_p_tree(rnh->rnh_treetop);
387 bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
390 printf("%s flags %x, addr ",
391 ifp->if_xname, ifp->if_flags);
392 print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
393 printf(", broadcast ");
394 print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
395 printf(", netmask ");
396 print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
401 bootpboot_p_iflist(void)
406 printf("Interface list:\n");
408 for (ifp = TAILQ_FIRST(&V_ifnet);
410 ifp = TAILQ_NEXT(ifp, if_link)) {
411 for (ifa = TAILQ_FIRST(&ifp->if_addrhead);
413 ifa = TAILQ_NEXT(ifa, ifa_link))
414 if (ifa->ifa_addr->sa_family == AF_INET)
415 bootpboot_p_if(ifp, ifa);
419 #endif /* defined(BOOTP_DEBUG) */
422 clear_sinaddr(struct sockaddr_in *sin)
425 bzero(sin, sizeof(*sin));
426 sin->sin_len = sizeof(*sin);
427 sin->sin_family = AF_INET;
428 sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
433 allocifctx(struct bootpc_globalcontext *gctx)
435 struct bootpc_ifcontext *ifctx;
437 ifctx = malloc(sizeof(*ifctx), M_TEMP, M_WAITOK | M_ZERO);
438 ifctx->xid = gctx->xid;
440 ifctx->state = IF_BOOTP_UNRESOLVED;
442 ifctx->state = IF_DHCP_UNRESOLVED;
445 STAILQ_INSERT_TAIL(&gctx->interfaces, ifctx, next);
449 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
452 if (ifctx->state == IF_BOOTP_RESOLVED ||
453 ifctx->state == IF_DHCP_RESOLVED)
459 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
462 if (ifctx->state == IF_BOOTP_UNRESOLVED ||
463 ifctx->state == IF_DHCP_UNRESOLVED)
469 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
472 if (ifctx->state == IF_BOOTP_FAILED ||
473 ifctx->state == IF_DHCP_FAILED)
479 bootpc_received(struct bootpc_globalcontext *gctx,
480 struct bootpc_ifcontext *ifctx)
482 unsigned char dhcpreplytype;
486 * Need timeout for fallback to less
487 * desirable alternative.
490 /* This call used for the side effect (badopt flag) */
491 (void) bootpc_tag(&gctx->tmptag, &gctx->reply,
495 /* If packet is invalid, ignore it */
496 if (gctx->tmptag.badopt != 0)
499 p = bootpc_tag(&gctx->tmptag, &gctx->reply,
500 gctx->replylen, TAG_DHCP_MSGTYPE);
504 dhcpreplytype = DHCP_NOMSG;
506 switch (ifctx->dhcpquerytype) {
508 if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */
509 #ifndef BOOTP_FORCE_DHCP
510 && dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */
516 if (dhcpreplytype != DHCP_ACK)
522 /* Ignore packet unless it gives us a root tag we didn't have */
524 if ((ifctx->state == IF_BOOTP_RESOLVED ||
525 (ifctx->dhcpquerytype == DHCP_DISCOVER &&
526 (ifctx->state == IF_DHCP_OFFERED ||
527 ifctx->state == IF_DHCP_RESOLVED))) &&
528 (bootpc_tag(&gctx->tmptag, &ifctx->reply,
531 bootpc_tag(&gctx->tmptag, &gctx->reply,
536 bcopy(&gctx->reply, &ifctx->reply, gctx->replylen);
537 ifctx->replylen = gctx->replylen;
539 /* XXX: Only reset if 'perfect' response */
540 if (ifctx->state == IF_BOOTP_UNRESOLVED)
541 ifctx->state = IF_BOOTP_RESOLVED;
542 else if (ifctx->state == IF_DHCP_UNRESOLVED &&
543 ifctx->dhcpquerytype == DHCP_DISCOVER) {
544 if (dhcpreplytype == DHCP_OFFER)
545 ifctx->state = IF_DHCP_OFFERED;
547 ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */
548 } else if (ifctx->state == IF_DHCP_OFFERED &&
549 ifctx->dhcpquerytype == DHCP_REQUEST)
550 ifctx->state = IF_DHCP_RESOLVED;
553 if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
554 ifctx->state != IF_BOOTP_RESOLVED) {
555 p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
556 ifctx->replylen, TAG_DHCP_SERVERID);
557 if (p != NULL && gctx->tmptag.taglen == 4) {
558 memcpy(&ifctx->dhcpserver, p, 4);
559 ifctx->gotdhcpserver = 1;
561 ifctx->gotdhcpserver = 0;
565 ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
568 ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
570 TAG_ROUTERS) != NULL);
571 ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
573 TAG_SUBNETMASK) != NULL);
578 bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
580 struct sockaddr_in *sin, dst;
584 int error, on, rcvflg, timo, len;
588 struct bootpc_ifcontext *ifctx;
596 bzero(&sopt, sizeof(sopt));
597 sopt.sopt_dir = SOPT_SET;
598 sopt.sopt_level = SOL_SOCKET;
599 sopt.sopt_name = SO_RCVTIMEO;
601 sopt.sopt_valsize = sizeof tv;
603 error = sosetopt(bootp_so, &sopt);
611 sopt.sopt_name = SO_BROADCAST;
613 sopt.sopt_valsize = sizeof on;
615 error = sosetopt(bootp_so, &sopt);
624 sopt.sopt_name = SO_DONTROUTE;
626 sopt.sopt_valsize = sizeof on;
628 error = sosetopt(bootp_so, &sopt);
633 * Bind the local endpoint to a bootp client port.
637 sin->sin_port = htons(IPPORT_BOOTPC);
638 error = sobind(bootp_so, (struct sockaddr *)sin, td);
640 printf("bind failed\n");
645 * Setup socket address for the server.
649 sin->sin_addr.s_addr = INADDR_BROADCAST;
650 sin->sin_port = htons(IPPORT_BOOTPS);
653 * Send it, repeatedly, until a reply is received,
654 * but delay each re-send by an increasing amount.
655 * If the delay hits the maximum, start complaining.
664 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
665 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
666 bootpc_tag(&gctx->tmptag, &ifctx->reply,
672 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
673 struct in_aliasreq *ifra = &ifctx->iareq;
674 sin = (struct sockaddr_in *)&ifra->ifra_mask;
676 ifctx->outstanding = 0;
677 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
681 if (bootpc_ifctx_isfailed(ifctx) != 0)
685 ifctx->outstanding = 1;
687 /* Proceed to next step in DHCP negotiation */
688 if ((ifctx->state == IF_DHCP_OFFERED &&
689 ifctx->dhcpquerytype != DHCP_REQUEST) ||
690 (ifctx->state == IF_DHCP_UNRESOLVED &&
691 ifctx->dhcpquerytype != DHCP_DISCOVER) ||
692 (ifctx->state == IF_BOOTP_UNRESOLVED &&
693 ifctx->dhcpquerytype != DHCP_NOMSG)) {
695 bootpc_compose_query(ifctx, td);
698 /* Send BOOTP request (or re-send). */
700 if (ifctx->sentmsg == 0) {
701 switch(ifctx->dhcpquerytype) {
713 printf("Sending %s packet from "
714 "interface %s (%*D)\n",
716 ifctx->ireq.ifr_name,
717 ifctx->sdl->sdl_alen,
718 (unsigned char *) LLADDR(ifctx->sdl),
723 aio.iov_base = (caddr_t) &ifctx->call;
724 aio.iov_len = sizeof(ifctx->call);
728 auio.uio_segflg = UIO_SYSSPACE;
729 auio.uio_rw = UIO_WRITE;
731 auio.uio_resid = sizeof(ifctx->call);
734 /* Set netmask to 0.0.0.0 */
736 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
739 panic("%s: SIOCAIFADDR, error=%d", __func__,
742 error = sosend(bootp_so, (struct sockaddr *) &dst,
743 &auio, NULL, NULL, 0, td);
745 printf("%s: sosend: %d state %08x\n", __func__,
746 error, (int )bootp_so->so_state);
748 /* Set netmask to 255.0.0.0 */
749 sin->sin_addr.s_addr = htonl(IN_CLASSA_NET);
750 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
753 panic("%s: SIOCAIFADDR, error=%d", __func__,
757 if (outstanding == 0 &&
758 (rtimo == 0 || time_second >= rtimo)) {
763 /* Determine new timeout. */
764 if (timo < MAX_RESEND_DELAY)
767 printf("DHCP/BOOTP timeout for server ");
768 print_sin_addr(&dst);
773 * Wait for up to timo seconds for a reply.
774 * The socket receive timeout was set to 1 second.
776 atimo = timo + time_second;
777 while (time_second < atimo) {
778 aio.iov_base = (caddr_t) &gctx->reply;
779 aio.iov_len = sizeof(gctx->reply);
783 auio.uio_segflg = UIO_SYSSPACE;
784 auio.uio_rw = UIO_READ;
786 auio.uio_resid = sizeof(gctx->reply);
790 error = soreceive(bootp_so, NULL, &auio,
791 NULL, NULL, &rcvflg);
792 gctx->secs = time_second - gctx->starttime;
793 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
794 if (bootpc_ifctx_isresolved(ifctx) != 0 ||
795 bootpc_ifctx_isfailed(ifctx) != 0)
798 ifctx->call.secs = htons(gctx->secs);
800 if (error == EWOULDBLOCK)
804 len = sizeof(gctx->reply) - auio.uio_resid;
806 /* Do we have the required number of bytes ? */
807 if (len < BOOTP_MIN_LEN)
809 gctx->replylen = len;
812 if (gctx->reply.op != BOOTP_REPLY)
815 /* Is this an answer to our query */
816 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
817 if (gctx->reply.xid != ifctx->call.xid)
820 /* Same HW address size ? */
821 if (gctx->reply.hlen != ifctx->call.hlen)
824 /* Correct HW address ? */
825 if (bcmp(gctx->reply.chaddr,
827 ifctx->call.hlen) != 0)
834 s = bootpc_tag(&gctx->tmptag,
847 s = "DHCP (unexpected)";
853 printf("Received %s packet"
856 ifctx->ireq.ifr_name);
857 print_in_addr(gctx->reply.siaddr);
858 if (gctx->reply.giaddr.s_addr !=
861 print_in_addr(gctx->reply.giaddr);
863 if (bootpc_received(gctx, ifctx) != 0) {
864 printf(" (accepted)");
865 if (ifctx->outstanding) {
866 ifctx->outstanding = 0;
869 /* Network settle delay */
870 if (outstanding == 0)
871 atimo = time_second +
874 printf(" (ignored)");
875 if (ifctx->gotrootpath ||
876 gctx->any_root_overrides) {
878 rtimo = time_second +
880 if (ifctx->gotrootpath)
881 printf(" (got root path)");
887 if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
890 /* Force a retry if halfway in DHCP negotiation */
892 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
893 if (ifctx->state == IF_DHCP_OFFERED) {
894 if (ifctx->dhcpquerytype == DHCP_DISCOVER)
897 ifctx->state = IF_DHCP_UNRESOLVED;
903 if (gotrootpath != 0) {
904 gctx->gotrootpath = gotrootpath;
905 if (rtimo != 0 && time_second >= rtimo)
908 } /* forever send/receive */
911 * XXX: These are errors of varying seriousness being silently
915 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
916 if (bootpc_ifctx_isresolved(ifctx) == 0) {
917 printf("%s timeout for interface %s\n",
918 ifctx->dhcpquerytype != DHCP_NOMSG ?
920 ifctx->ireq.ifr_name);
923 if (gctx->gotrootpath != 0) {
925 printf("Got a root path, ignoring remaining timeout\n");
930 #ifndef BOOTP_NFSROOT
931 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
932 if (bootpc_ifctx_isresolved(ifctx) != 0) {
944 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
947 struct in_aliasreq *ifra;
948 struct sockaddr_in *sin;
952 ifra = &ifctx->iareq;
955 * Bring up the interface.
957 * Get the old interface flags and or IFF_UP into them; if
958 * IFF_UP set blindly, interface selection can be clobbered.
960 error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
962 panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
963 ifr->ifr_flags |= IFF_UP;
964 error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
966 panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
969 * Do enough of ifconfig(8) so that the chosen interface
970 * can talk to the servers. Set address to 0.0.0.0/8 and
971 * broadcast address to local broadcast.
973 sin = (struct sockaddr_in *)&ifra->ifra_addr;
975 sin = (struct sockaddr_in *)&ifra->ifra_mask;
977 sin->sin_addr.s_addr = htonl(IN_CLASSA_NET);
978 sin = (struct sockaddr_in *)&ifra->ifra_broadaddr;
980 sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
981 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
983 panic("%s: SIOCAIFADDR, error=%d", __func__, error);
987 bootpc_shutdown_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
990 struct sockaddr_in *sin;
995 printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
996 error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
998 panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
999 ifr->ifr_flags &= ~IFF_UP;
1000 error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
1002 panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
1004 sin = (struct sockaddr_in *) &ifr->ifr_addr;
1006 error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
1008 panic("%s: SIOCDIFADDR, error=%d", __func__, error);
1012 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
1013 struct bootpc_globalcontext *gctx, struct thread *td)
1016 struct sockaddr_in defdst;
1017 struct sockaddr_in defmask;
1018 struct sockaddr_in *sin;
1020 struct in_aliasreq *ifra;
1021 struct sockaddr_in *myaddr;
1022 struct sockaddr_in *netmask;
1023 struct sockaddr_in *gw;
1026 ifra = &ifctx->iareq;
1027 myaddr = &ifctx->myaddr;
1028 netmask = &ifctx->netmask;
1031 if (bootpc_ifctx_isresolved(ifctx) == 0) {
1032 /* Shutdown interfaces where BOOTP failed */
1033 bootpc_shutdown_interface(ifctx, td);
1037 printf("Adjusted interface %s\n", ifctx->ireq.ifr_name);
1039 * Do enough of ifconfig(8) so that the chosen interface
1040 * can talk to the servers. (just set the address)
1042 sin = (struct sockaddr_in *) &ifr->ifr_addr;
1044 error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
1046 panic("%s: SIOCDIFADDR, error=%d", __func__, error);
1048 bcopy(myaddr, &ifra->ifra_addr, sizeof(*myaddr));
1049 bcopy(netmask, &ifra->ifra_mask, sizeof(*netmask));
1050 clear_sinaddr(&ifra->ifra_broadaddr);
1051 ifra->ifra_broadaddr.sin_addr.s_addr = myaddr->sin_addr.s_addr |
1052 ~netmask->sin_addr.s_addr;
1054 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
1056 panic("%s: SIOCAIFADDR, error=%d", __func__, error);
1058 /* Add new default route */
1060 if (ifctx->gotgw != 0 || gctx->gotgw == 0) {
1061 clear_sinaddr(&defdst);
1062 clear_sinaddr(&defmask);
1063 /* XXX MRT just table 0 */
1064 error = rtrequest_fib(RTM_ADD,
1065 (struct sockaddr *) &defdst, (struct sockaddr *) gw,
1066 (struct sockaddr *) &defmask,
1067 (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL, RT_DEFAULT_FIB);
1069 printf("%s: RTM_ADD, error=%d\n", __func__, error);
1078 setfs(struct sockaddr_in *addr, char *path, char *p,
1079 const struct in_addr *siaddr)
1082 if (getip(&p, &addr->sin_addr) == 0) {
1083 if (siaddr != NULL && *p == '/')
1084 bcopy(siaddr, &addr->sin_addr, sizeof(struct in_addr));
1093 addr->sin_len = sizeof(struct sockaddr_in);
1094 addr->sin_family = AF_INET;
1096 strlcpy(path, p, MNAMELEN);
1101 getip(char **ptr, struct in_addr *addr)
1109 if (((val = getdec(&p)) < 0) || (val > 255))
1115 if (((val = getdec(&p)) < 0) || (val > 255))
1121 if (((val = getdec(&p)) < 0) || (val > 255))
1127 if (((val = getdec(&p)) < 0) || (val > 255))
1131 addr->s_addr = htonl(ip);
1144 if ((*p < '0') || (*p > '9'))
1146 while ((*p >= '0') && (*p <= '9')) {
1147 ret = ret * 10 + (*p - '0');
1155 mountopts(struct nfs_args *args, char *p)
1157 args->version = NFS_ARGSVERSION;
1158 args->rsize = BOOTP_BLOCKSIZE;
1159 args->wsize = BOOTP_BLOCKSIZE;
1160 args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
1161 args->sotype = SOCK_DGRAM;
1163 nfs_parse_options(p, args);
1167 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
1173 alignedlen = ( len + 3 ) & ~3;
1175 if (m->m_len < alignedlen) {
1176 m = m_pullup(m, alignedlen);
1182 bcopy(mtod(m, u_char *), buf, len);
1183 m_adj(m, alignedlen);
1189 xdr_int_decode(struct mbuf **mptr, int *iptr)
1193 if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
1195 *iptr = fxdr_unsigned(u_int32_t, i);
1200 print_sin_addr(struct sockaddr_in *sin)
1203 print_in_addr(sin->sin_addr);
1207 print_in_addr(struct in_addr addr)
1211 ip = ntohl(addr.s_addr);
1212 printf("%d.%d.%d.%d",
1213 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1217 bootpc_compose_query(struct bootpc_ifcontext *ifctx, struct thread *td)
1219 unsigned char *vendp;
1220 unsigned char vendor_client[64];
1222 uint8_t vendor_client_len;
1224 ifctx->gotrootpath = 0;
1226 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1229 ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */
1230 ifctx->call.htype = 1; /* 10mb ethernet */
1231 ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1232 ifctx->call.hops = 0;
1233 if (bootpc_ifctx_isunresolved(ifctx) != 0)
1235 ifctx->call.xid = txdr_unsigned(ifctx->xid);
1236 bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1238 vendp = ifctx->call.vend;
1239 *vendp++ = 99; /* RFC1048 cookie */
1243 *vendp++ = TAG_MAXMSGSIZE;
1245 *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1246 *vendp++ = sizeof(struct bootp_packet) & 255;
1248 snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1249 ostype, MACHINE, osrelease);
1250 vendor_client_len = strlen(vendor_client);
1251 *vendp++ = TAG_VENDOR_INDENTIFIER;
1252 *vendp++ = vendor_client_len;
1253 memcpy(vendp, vendor_client, vendor_client_len);
1254 vendp += vendor_client_len;
1255 ifctx->dhcpquerytype = DHCP_NOMSG;
1256 switch (ifctx->state) {
1257 case IF_DHCP_UNRESOLVED:
1258 *vendp++ = TAG_DHCP_MSGTYPE;
1260 *vendp++ = DHCP_DISCOVER;
1261 ifctx->dhcpquerytype = DHCP_DISCOVER;
1262 ifctx->gotdhcpserver = 0;
1264 case IF_DHCP_OFFERED:
1265 *vendp++ = TAG_DHCP_MSGTYPE;
1267 *vendp++ = DHCP_REQUEST;
1268 ifctx->dhcpquerytype = DHCP_REQUEST;
1269 *vendp++ = TAG_DHCP_REQ_ADDR;
1271 memcpy(vendp, &ifctx->reply.yiaddr, 4);
1273 if (ifctx->gotdhcpserver != 0) {
1274 *vendp++ = TAG_DHCP_SERVERID;
1276 memcpy(vendp, &ifctx->dhcpserver, 4);
1279 *vendp++ = TAG_DHCP_LEASETIME;
1281 leasetime = htonl(300);
1282 memcpy(vendp, &leasetime, 4);
1290 ifctx->call.secs = 0;
1291 ifctx->call.flags = htons(0x8000); /* We need a broadcast answer */
1295 bootpc_hascookie(struct bootp_packet *bp)
1298 return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1299 bp->vend[2] == 83 && bp->vend[3] == 99);
1303 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1304 unsigned char *start, int len, int tag)
1310 if (tctx->badtag != 0 || tctx->badopt != 0)
1318 if (code == TAG_PAD)
1320 if (code == TAG_END)
1322 if (j >= ej || j + *j + 1 > ej) {
1328 if (tctx->taglen + len > TAG_MAXLEN) {
1334 memcpy(tctx->buf + tctx->taglen,
1336 tctx->taglen += len;
1338 if (code == TAG_OVERLOAD)
1339 tctx->overload = *j;
1345 static unsigned char *
1346 bootpc_tag(struct bootpc_tagcontext *tctx,
1347 struct bootp_packet *bp, int len, int tag)
1355 if (bootpc_hascookie(bp) == 0)
1358 bootpc_tag_helper(tctx, &bp->vend[4],
1359 (unsigned char *) bp + len - &bp->vend[4], tag);
1361 if ((tctx->overload & OVERLOAD_FILE) != 0)
1362 bootpc_tag_helper(tctx,
1363 (unsigned char *) bp->file,
1366 if ((tctx->overload & OVERLOAD_SNAME) != 0)
1367 bootpc_tag_helper(tctx,
1368 (unsigned char *) bp->sname,
1372 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1374 tctx->buf[tctx->taglen] = '\0';
1379 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1380 struct bootpc_globalcontext *gctx)
1386 ifctx->gotnetmask = 0;
1388 clear_sinaddr(&ifctx->myaddr);
1389 clear_sinaddr(&ifctx->netmask);
1390 clear_sinaddr(&ifctx->gw);
1392 ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1394 ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1396 printf("%s at ", ifctx->ireq.ifr_name);
1397 print_sin_addr(&ifctx->myaddr);
1399 print_in_addr(ifctx->reply.siaddr);
1401 ifctx->gw.sin_addr = ifctx->reply.giaddr;
1402 if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1403 printf(" via gateway ");
1404 print_in_addr(ifctx->reply.giaddr);
1407 /* This call used for the side effect (overload flag) */
1408 (void) bootpc_tag(&gctx->tmptag,
1409 &ifctx->reply, ifctx->replylen, TAG_END);
1411 if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1412 if (ifctx->reply.sname[0] != '\0')
1413 printf(" server name %s", ifctx->reply.sname);
1414 if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1415 if (ifctx->reply.file[0] != '\0')
1416 printf(" boot file %s", ifctx->reply.file);
1420 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1423 if (gctx->tag.taglen != 4)
1424 panic("bootpc: subnet mask len is %d",
1426 bcopy(p, &ifctx->netmask.sin_addr, 4);
1427 ifctx->gotnetmask = 1;
1428 printf("subnet mask ");
1429 print_sin_addr(&ifctx->netmask);
1433 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1437 if (gctx->tag.taglen % 4)
1438 panic("bootpc: Router Len is %d", gctx->tag.taglen);
1439 if (gctx->tag.taglen > 0) {
1440 bcopy(p, &ifctx->gw.sin_addr, 4);
1442 print_sin_addr(&ifctx->gw);
1450 * Choose a root filesystem. If a value is forced in the environment
1451 * and it contains "nfs:", use it unconditionally. Otherwise, if the
1452 * kernel is compiled with the ROOTDEVNAME option, then use it if:
1453 * - The server doesn't provide a pathname.
1454 * - The boothowto flags include RB_DFLTROOT (user said to override
1455 * the server value).
1458 if ((s = kern_getenv("vfs.root.mountfrom")) != NULL) {
1459 if ((p = strstr(s, "nfs:")) != NULL)
1460 p = strdup(p + 4, M_TEMP);
1464 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1468 if ((p == NULL || (boothowto & RB_DFLTROOT) != 0) &&
1469 (p = strstr(ROOTDEVNAME, "nfs:")) != NULL) {
1474 if (gctx->setrootfs != NULL) {
1475 printf("rootfs %s (ignored) ", p);
1476 } else if (setfs(&nd->root_saddr,
1477 nd->root_hostnam, p, &ifctx->reply.siaddr)) {
1479 printf("root_server ");
1480 print_sin_addr(&nd->root_saddr);
1483 printf("rootfs %s ", p);
1484 gctx->gotrootpath = 1;
1485 ifctx->gotrootpath = 1;
1486 gctx->setrootfs = ifctx;
1488 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1492 mountopts(&nd->root_args, p);
1493 printf("rootopts %s ", p);
1496 panic("Failed to set rootfs to %s", p);
1499 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1502 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1503 panic("bootpc: hostname >= %d bytes",
1505 if (gctx->sethostname != NULL) {
1506 printf("hostname %s (ignored) ", p);
1508 strcpy(nd->my_hostnam, p);
1509 mtx_lock(&prison0.pr_mtx);
1510 strcpy(prison0.pr_hostname, p);
1511 mtx_unlock(&prison0.pr_mtx);
1512 printf("hostname %s ", p);
1513 gctx->sethostname = ifctx;
1516 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1518 if (p != NULL) { /* store in a sysctl variable */
1519 int i, l = sizeof(bootp_cookie) - 1;
1520 for (i = 0; i < l && p[i] != '\0'; i++)
1521 bootp_cookie[i] = p[i];
1528 if (ifctx->gotnetmask == 0) {
1529 if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1530 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1531 else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1532 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1534 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1536 if (ifctx->gotgw == 0) {
1538 ifctx->gw.sin_addr.s_addr = ifctx->myaddr.sin_addr.s_addr;
1545 struct bootpc_ifcontext *ifctx; /* Interface BOOTP contexts */
1546 struct bootpc_globalcontext *gctx; /* Global BOOTP context */
1548 struct sockaddr_dl *sdl;
1551 #ifndef BOOTP_WIRED_TO
1554 struct nfsv3_diskless *nd;
1559 timeout = BOOTP_IFACE_WAIT_TIMEOUT * hz;
1562 nd = &nfsv3_diskless;
1566 * If already filled in, don't touch it here
1568 if (nfs_diskless_valid != 0)
1571 gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK | M_ZERO);
1572 STAILQ_INIT(&gctx->interfaces);
1573 gctx->xid = ~0xFFFF;
1574 gctx->starttime = time_second;
1577 * If ROOTDEVNAME is defined or vfs.root.mountfrom is set then we have
1578 * root-path overrides that can potentially let us boot even if we don't
1579 * get a root path from the server, so we can treat that as a non-error.
1582 gctx->any_root_overrides = 1;
1584 gctx->any_root_overrides = testenv("vfs.root.mountfrom");
1588 * Find a network interface.
1590 CURVNET_SET(TD_TO_VNET(td));
1591 #ifdef BOOTP_WIRED_TO
1592 printf("%s: wired to interface '%s'\n", __func__,
1593 __XSTRING(BOOTP_WIRED_TO));
1597 * Preallocate interface context storage, if another interface
1598 * attaches and wins the race, it won't be eligible for bootp.
1602 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1603 if ((ifp->if_flags &
1604 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1607 switch (ifp->if_alloctype) {
1619 panic("%s: no eligible interfaces", __func__);
1620 for (; ifcnt > 0; ifcnt--)
1625 ifctx = STAILQ_FIRST(&gctx->interfaces);
1627 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1630 #ifdef BOOTP_WIRED_TO
1631 if (strcmp(ifp->if_xname, __XSTRING(BOOTP_WIRED_TO)) != 0)
1634 if ((ifp->if_flags &
1635 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1638 switch (ifp->if_alloctype) {
1647 strlcpy(ifctx->ireq.ifr_name, ifp->if_xname,
1648 sizeof(ifctx->ireq.ifr_name));
1651 /* Get HW address */
1653 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1654 if (ifa->ifa_addr->sa_family == AF_LINK) {
1655 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1656 if (sdl->sdl_type == IFT_ETHER)
1660 panic("bootpc: Unable to find HW address for %s",
1661 ifctx->ireq.ifr_name);
1664 ifctx = STAILQ_NEXT(ifctx, next);
1669 if (STAILQ_EMPTY(&gctx->interfaces) ||
1670 STAILQ_FIRST(&gctx->interfaces)->ifp == NULL) {
1672 pause("bootpc", delay);
1676 #ifdef BOOTP_WIRED_TO
1677 panic("%s: Could not find interface specified "
1678 "by BOOTP_WIRED_TO: "
1679 __XSTRING(BOOTP_WIRED_TO), __func__);
1681 panic("%s: no suitable interface", __func__);
1685 error = socreate(AF_INET, &bootp_so, SOCK_DGRAM, 0, td->td_ucred, td);
1687 panic("%s: socreate, error=%d", __func__, error);
1689 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1690 bootpc_fakeup_interface(ifctx, td);
1692 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1693 bootpc_compose_query(ifctx, td);
1695 error = bootpc_call(gctx, td);
1697 printf("BOOTP call failed\n");
1700 mountopts(&nd->root_args, NULL);
1702 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1703 if (bootpc_ifctx_isresolved(ifctx) != 0)
1704 bootpc_decode_reply(nd, ifctx, gctx);
1706 #ifdef BOOTP_NFSROOT
1707 if (gctx->gotrootpath == 0 && gctx->any_root_overrides == 0)
1708 panic("bootpc: No root path offered");
1711 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1712 bootpc_adjust_interface(ifctx, gctx, td);
1716 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1717 if (ifctx->gotrootpath != 0)
1719 if (ifctx == NULL) {
1720 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1721 if (bootpc_ifctx_isresolved(ifctx) != 0)
1727 if (gctx->gotrootpath != 0) {
1729 kern_setenv("boot.netif.name", ifctx->ifp->if_xname);
1731 error = md_mount(&nd->root_saddr, nd->root_hostnam,
1732 nd->root_fh, &nd->root_fhsize,
1733 &nd->root_args, td);
1735 if (gctx->any_root_overrides == 0)
1736 panic("nfs_boot: mount root, error=%d", error);
1740 rootdevnames[0] = "nfs:";
1741 nfs_diskless_valid = 3;
1744 strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1745 bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1746 bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1747 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1748 ifctx->myaddr.sin_addr.s_addr |
1749 ~ ifctx->netmask.sin_addr.s_addr;
1750 bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1753 while((ifctx = STAILQ_FIRST(&gctx->interfaces)) != NULL) {
1754 STAILQ_REMOVE_HEAD(&gctx->interfaces, next);
1755 free(ifctx, M_TEMP);
1762 * Given a server pathname, get an NFS file handle.
1763 * Also, sets sin->sin_port to the NFS service port.
1766 md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp, int *fhsizep,
1767 struct nfs_args *args, struct thread *td)
1775 #define RPCPROG_MNT 100005
1776 #define RPCMNT_VER1 1
1777 #define RPCMNT_VER3 3
1778 #define RPCMNT_MOUNT 1
1779 #define AUTH_SYS 1 /* unix style (uid, gids) */
1780 #define AUTH_UNIX AUTH_SYS
1782 /* XXX honor v2/v3 flags in args->flags? */
1784 /* First try NFS v3 */
1785 /* Get port number for MOUNTD. */
1786 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1787 &mdsin->sin_port, td);
1789 m = xdr_string_encode(path, strlen(path));
1791 /* Do RPC to mountd. */
1792 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1793 RPCMNT_MOUNT, &m, NULL, td);
1796 args->flags |= NFSMNT_NFSV3;
1799 /* Fallback to NFS v2 */
1801 /* Get port number for MOUNTD. */
1802 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1803 &mdsin->sin_port, td);
1807 m = xdr_string_encode(path, strlen(path));
1809 /* Do RPC to mountd. */
1810 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1811 RPCMNT_MOUNT, &m, NULL, td);
1813 return error; /* message already freed */
1819 if (xdr_int_decode(&m, &error) != 0 || error != 0)
1822 if ((args->flags & NFSMNT_NFSV3) != 0) {
1823 if (xdr_int_decode(&m, fhsizep) != 0 ||
1824 *fhsizep > NFSX_V3FHMAX ||
1828 *fhsizep = NFSX_V2FH;
1830 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
1833 if (args->flags & NFSMNT_NFSV3) {
1834 if (xdr_int_decode(&m, &authcount) != 0)
1837 if (authcount < 0 || authcount > 100)
1839 while (authcount > 0) {
1840 if (xdr_int_decode(&m, &authver) != 0)
1842 if (authver == AUTH_UNIX)
1846 if (authunixok == 0)
1850 /* Set port number for NFS use. */
1851 error = krpc_portmap(mdsin, NFS_PROG,
1853 NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
1854 &mdsin->sin_port, td);
1866 SYSINIT(bootp_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, bootpc_init, NULL);