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 <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/sockio.h>
52 #include <sys/malloc.h>
53 #include <sys/mount.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/sysctl.h>
62 #include <net/route.h>
64 #include <netinet/in.h>
65 #include <net/if_types.h>
66 #include <net/if_dl.h>
68 #include <rpc/rpcclnt.h>
70 #include <nfs/rpcv2.h>
71 #include <nfs/nfsproto.h>
72 #include <nfsclient/nfs.h>
73 #include <nfsclient/nfsdiskless.h>
74 #include <nfsclient/krpc.h>
75 #include <nfs/xdr_subs.h>
78 #define BOOTP_MIN_LEN 300 /* Minimum size of bootp udp packet */
80 #ifndef BOOTP_SETTLE_DELAY
81 #define BOOTP_SETTLE_DELAY 3
85 * What is the longest we will wait before re-sending a request?
86 * Note this is also the frequency of "RPC timeout" messages.
87 * The re-send loop count sup linearly to this maximum, so the
88 * first complaint will happen after (1+2+3+4+5)=15 seconds.
90 #define MAX_RESEND_DELAY 5 /* seconds */
92 /* Definitions from RFC951 */
101 struct in_addr ciaddr;
102 struct in_addr yiaddr;
103 struct in_addr siaddr;
104 struct in_addr giaddr;
105 unsigned char chaddr[16];
108 unsigned char vend[1222];
111 struct bootpc_ifcontext {
112 struct bootpc_ifcontext *next;
113 struct bootp_packet call;
114 struct bootp_packet reply;
120 struct sockaddr_dl *sdl;
121 struct sockaddr_in myaddr;
122 struct sockaddr_in netmask;
123 struct sockaddr_in gw;
124 struct sockaddr_in broadcast; /* Different for each interface */
140 int dhcpquerytype; /* dhcp type sent */
141 struct in_addr dhcpserver;
145 #define TAG_MAXLEN 1024
146 struct bootpc_tagcontext {
147 char buf[TAG_MAXLEN + 1];
155 struct bootpc_globalcontext {
156 struct bootpc_ifcontext *interfaces;
157 struct bootpc_ifcontext *lastinterface;
164 struct bootp_packet reply;
166 struct bootpc_ifcontext *setrootfs;
167 struct bootpc_ifcontext *sethostname;
168 struct bootpc_tagcontext tmptag;
169 struct bootpc_tagcontext tag;
172 #define IPPORT_BOOTPC 68
173 #define IPPORT_BOOTPS 67
175 #define BOOTP_REQUEST 1
176 #define BOOTP_REPLY 2
179 #define TAG_PAD 0 /* Pad option, implicit length 1 */
180 #define TAG_SUBNETMASK 1 /* RFC 950 subnet mask */
181 #define TAG_ROUTERS 3 /* Routers (in order of preference) */
182 #define TAG_HOSTNAME 12 /* Client host name */
183 #define TAG_ROOT 17 /* Root path */
185 /* DHCP specific tags */
186 #define TAG_OVERLOAD 52 /* Option Overload */
187 #define TAG_MAXMSGSIZE 57 /* Maximum DHCP Message Size */
189 #define TAG_END 255 /* End Option (i.e. no more options) */
191 /* Overload values */
192 #define OVERLOAD_FILE 1
193 #define OVERLOAD_SNAME 2
195 /* Site specific tags: */
196 #define TAG_ROOTOPTS 130
197 #define TAG_COOKIE 134 /* ascii info for userland, via sysctl */
199 #define TAG_DHCP_MSGTYPE 53
200 #define TAG_DHCP_REQ_ADDR 50
201 #define TAG_DHCP_SERVERID 54
202 #define TAG_DHCP_LEASETIME 51
204 #define TAG_VENDOR_INDENTIFIER 60
207 #define DHCP_DISCOVER 1
209 #define DHCP_REQUEST 3
212 static char bootp_cookie[128];
213 SYSCTL_STRING(_kern, OID_AUTO, bootp_cookie, CTLFLAG_RD,
214 bootp_cookie, 0, "Cookie (T134) supplied by bootp server");
217 static int md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp,
218 int *fhsizep, struct nfs_args *args, struct thread *td);
219 static int setfs(struct sockaddr_in *addr, char *path, char *p,
220 const struct in_addr *siaddr);
221 static int getdec(char **ptr);
222 static int getip(char **ptr, struct in_addr *ip);
223 static void mountopts(struct nfs_args *args, char *p);
224 static int xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len);
225 static int xdr_int_decode(struct mbuf **ptr, int *iptr);
226 static void print_in_addr(struct in_addr addr);
227 static void print_sin_addr(struct sockaddr_in *addr);
228 static void clear_sinaddr(struct sockaddr_in *sin);
229 static void allocifctx(struct bootpc_globalcontext *gctx);
230 static void bootpc_compose_query(struct bootpc_ifcontext *ifctx,
231 struct bootpc_globalcontext *gctx, struct thread *td);
232 static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx,
233 struct bootp_packet *bp, int len, int tag);
234 static void bootpc_tag_helper(struct bootpc_tagcontext *tctx,
235 unsigned char *start, int len, int tag);
238 void bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma);
239 void bootpboot_p_ma(struct sockaddr *ma);
240 void bootpboot_p_rtentry(struct rtentry *rt);
241 void bootpboot_p_tree(struct radix_node *rn);
242 void bootpboot_p_rtlist(void);
243 void bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa);
244 void bootpboot_p_iflist(void);
247 static int bootpc_call(struct bootpc_globalcontext *gctx,
250 static int bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
251 struct bootpc_globalcontext *gctx, struct thread *td);
253 static int bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
254 struct bootpc_globalcontext *gctx, struct thread *td);
256 static void bootpc_decode_reply(struct nfsv3_diskless *nd,
257 struct bootpc_ifcontext *ifctx,
258 struct bootpc_globalcontext *gctx);
260 static int bootpc_received(struct bootpc_globalcontext *gctx,
261 struct bootpc_ifcontext *ifctx);
263 static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
264 static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
265 static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
268 * In order to have multiple active interfaces with address 0.0.0.0
269 * and be able to send data to a selected interface, we perform
272 * - The 'broadcast' address is different for each interface.
274 * - We temporarily add routing pointing 255.255.255.255 to the
275 * selected interface broadcast address, thus the packet sent
276 * goes to that interface.
281 bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma)
285 printf("(sockaddr *) <null>");
288 switch (sa->sa_family) {
291 struct sockaddr_in *sin;
293 sin = (struct sockaddr_in *) sa;
297 sin = (struct sockaddr_in *) ma;
305 struct sockaddr_dl *sli;
308 sli = (struct sockaddr_dl *) sa;
309 printf("link %.*s ", sli->sdl_nlen, sli->sdl_data);
310 for (i = 0; i < sli->sdl_alen; i++) {
313 printf("%x", ((unsigned char *) LLADDR(sli))[i]);
318 printf("af%d", sa->sa_family);
323 bootpboot_p_ma(struct sockaddr *ma)
330 printf("%x", *(int *)ma);
334 bootpboot_p_rtentry(struct rtentry *rt)
337 bootpboot_p_sa(rt_key(rt), rt_mask(rt));
339 bootpboot_p_ma(rt->rt_genmask);
341 bootpboot_p_sa(rt->rt_gateway, NULL);
343 printf("flags %x", (unsigned short) rt->rt_flags);
344 printf(" %d", (int) rt->rt_rmx.rmx_expire);
345 printf(" %s\n", rt->rt_ifp->if_xname);
349 bootpboot_p_tree(struct radix_node *rn)
353 if (rn->rn_bit < 0) {
354 if ((rn->rn_flags & RNF_ROOT) != 0) {
356 bootpboot_p_rtentry((struct rtentry *) rn);
358 rn = rn->rn_dupedkey;
360 bootpboot_p_tree(rn->rn_left);
361 bootpboot_p_tree(rn->rn_right);
368 bootpboot_p_rtlist(void)
371 printf("Routing table:\n");
372 RADIX_NODE_LOCK(rt_tables[AF_INET]); /* could sleep XXX */
373 bootpboot_p_tree(rt_tables[AF_INET]->rnh_treetop);
374 RADIX_NODE_UNLOCK(rt_tables[AF_INET]);
378 bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
381 printf("%s flags %x, addr ",
382 ifp->if_xname, ifp->if_flags);
383 print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
384 printf(", broadcast ");
385 print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
386 printf(", netmask ");
387 print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
392 bootpboot_p_iflist(void)
397 printf("Interface list:\n");
398 IFNET_RLOCK(); /* could sleep, but okay for debugging XXX */
399 for (ifp = TAILQ_FIRST(&ifnet);
401 ifp = TAILQ_NEXT(ifp, if_link)) {
402 for (ifa = TAILQ_FIRST(&ifp->if_addrhead);
404 ifa = TAILQ_NEXT(ifa, ifa_link))
405 if (ifa->ifa_addr->sa_family == AF_INET)
406 bootpboot_p_if(ifp, ifa);
410 #endif /* defined(BOOTP_DEBUG) */
413 clear_sinaddr(struct sockaddr_in *sin)
416 bzero(sin, sizeof(*sin));
417 sin->sin_len = sizeof(*sin);
418 sin->sin_family = AF_INET;
419 sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
424 allocifctx(struct bootpc_globalcontext *gctx)
426 struct bootpc_ifcontext *ifctx;
427 ifctx = (struct bootpc_ifcontext *) malloc(sizeof(*ifctx),
428 M_TEMP, M_WAITOK | M_ZERO);
430 panic("Failed to allocate bootp interface context structure");
432 ifctx->xid = gctx->xid;
434 ifctx->state = IF_BOOTP_UNRESOLVED;
436 ifctx->state = IF_DHCP_UNRESOLVED;
439 if (gctx->interfaces != NULL)
440 gctx->lastinterface->next = ifctx;
442 gctx->interfaces = ifctx;
443 gctx->lastinterface = ifctx;
447 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
450 if (ifctx->state == IF_BOOTP_RESOLVED ||
451 ifctx->state == IF_DHCP_RESOLVED)
457 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
460 if (ifctx->state == IF_BOOTP_UNRESOLVED ||
461 ifctx->state == IF_DHCP_UNRESOLVED)
467 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
470 if (ifctx->state == IF_BOOTP_FAILED ||
471 ifctx->state == IF_DHCP_FAILED)
477 bootpc_received(struct bootpc_globalcontext *gctx,
478 struct bootpc_ifcontext *ifctx)
480 unsigned char dhcpreplytype;
484 * Need timeout for fallback to less
485 * desirable alternative.
488 /* This call used for the side effect (badopt flag) */
489 (void) bootpc_tag(&gctx->tmptag, &gctx->reply,
493 /* If packet is invalid, ignore it */
494 if (gctx->tmptag.badopt != 0)
497 p = bootpc_tag(&gctx->tmptag, &gctx->reply,
498 gctx->replylen, TAG_DHCP_MSGTYPE);
502 dhcpreplytype = DHCP_NOMSG;
504 switch (ifctx->dhcpquerytype) {
506 if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */
507 #ifndef BOOTP_FORCE_DHCP
508 && dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */
514 if (dhcpreplytype != DHCP_ACK)
520 /* Ignore packet unless it gives us a root tag we didn't have */
522 if ((ifctx->state == IF_BOOTP_RESOLVED ||
523 (ifctx->dhcpquerytype == DHCP_DISCOVER &&
524 (ifctx->state == IF_DHCP_OFFERED ||
525 ifctx->state == IF_DHCP_RESOLVED))) &&
526 (bootpc_tag(&gctx->tmptag, &ifctx->reply,
529 bootpc_tag(&gctx->tmptag, &gctx->reply,
534 bcopy(&gctx->reply, &ifctx->reply, gctx->replylen);
535 ifctx->replylen = gctx->replylen;
537 /* XXX: Only reset if 'perfect' response */
538 if (ifctx->state == IF_BOOTP_UNRESOLVED)
539 ifctx->state = IF_BOOTP_RESOLVED;
540 else if (ifctx->state == IF_DHCP_UNRESOLVED &&
541 ifctx->dhcpquerytype == DHCP_DISCOVER) {
542 if (dhcpreplytype == DHCP_OFFER)
543 ifctx->state = IF_DHCP_OFFERED;
545 ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */
546 } else if (ifctx->state == IF_DHCP_OFFERED &&
547 ifctx->dhcpquerytype == DHCP_REQUEST)
548 ifctx->state = IF_DHCP_RESOLVED;
551 if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
552 ifctx->state != IF_BOOTP_RESOLVED) {
553 p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
554 ifctx->replylen, TAG_DHCP_SERVERID);
555 if (p != NULL && gctx->tmptag.taglen == 4) {
556 memcpy(&ifctx->dhcpserver, p, 4);
557 ifctx->gotdhcpserver = 1;
559 ifctx->gotdhcpserver = 0;
563 ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
566 ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
568 TAG_ROUTERS) != NULL);
569 ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
571 TAG_SUBNETMASK) != NULL);
576 bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
579 struct sockaddr_in *sin, dst;
583 int error, on, rcvflg, timo, len;
587 struct bootpc_ifcontext *ifctx;
594 * Create socket and set its recieve timeout.
596 error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td->td_ucred, td);
602 bzero(&sopt, sizeof(sopt));
603 sopt.sopt_dir = SOPT_SET;
604 sopt.sopt_level = SOL_SOCKET;
605 sopt.sopt_name = SO_RCVTIMEO;
607 sopt.sopt_valsize = sizeof tv;
609 error = sosetopt(so, &sopt);
617 sopt.sopt_name = SO_BROADCAST;
619 sopt.sopt_valsize = sizeof on;
621 error = sosetopt(so, &sopt);
630 sopt.sopt_name = SO_DONTROUTE;
632 sopt.sopt_valsize = sizeof on;
634 error = sosetopt(so, &sopt);
639 * Bind the local endpoint to a bootp client port.
643 sin->sin_port = htons(IPPORT_BOOTPC);
644 error = sobind(so, (struct sockaddr *)sin, td);
646 printf("bind failed\n");
651 * Setup socket address for the server.
655 sin->sin_addr.s_addr = INADDR_BROADCAST;
656 sin->sin_port = htons(IPPORT_BOOTPS);
659 * Send it, repeatedly, until a reply is received,
660 * but delay each re-send by an increasing amount.
661 * If the delay hits the maximum, start complaining.
670 for (ifctx = gctx->interfaces;
672 ifctx = ifctx->next) {
673 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
674 bootpc_tag(&gctx->tmptag, &ifctx->reply,
680 for (ifctx = gctx->interfaces;
682 ifctx = ifctx->next) {
683 ifctx->outstanding = 0;
684 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
688 if (bootpc_ifctx_isfailed(ifctx) != 0)
692 ifctx->outstanding = 1;
694 /* Proceed to next step in DHCP negotiation */
695 if ((ifctx->state == IF_DHCP_OFFERED &&
696 ifctx->dhcpquerytype != DHCP_REQUEST) ||
697 (ifctx->state == IF_DHCP_UNRESOLVED &&
698 ifctx->dhcpquerytype != DHCP_DISCOVER) ||
699 (ifctx->state == IF_BOOTP_UNRESOLVED &&
700 ifctx->dhcpquerytype != DHCP_NOMSG)) {
702 bootpc_compose_query(ifctx, gctx, td);
705 /* Send BOOTP request (or re-send). */
707 if (ifctx->sentmsg == 0) {
708 switch(ifctx->dhcpquerytype) {
720 printf("Sending %s packet from "
721 "interface %s (%*D)\n",
723 ifctx->ireq.ifr_name,
724 ifctx->sdl->sdl_alen,
725 (unsigned char *) LLADDR(ifctx->sdl),
730 aio.iov_base = (caddr_t) &ifctx->call;
731 aio.iov_len = sizeof(ifctx->call);
735 auio.uio_segflg = UIO_SYSSPACE;
736 auio.uio_rw = UIO_WRITE;
738 auio.uio_resid = sizeof(ifctx->call);
741 /* Set netmask to 0.0.0.0 */
743 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
745 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
746 (caddr_t) &ifctx->ireq, td);
749 "set if netmask, error=%d",
752 error = sosend(so, (struct sockaddr *) &dst,
753 &auio, NULL, NULL, 0, td);
755 printf("bootpc_call: sosend: %d state %08x\n",
756 error, (int) so->so_state);
759 /* XXX: Is this needed ? */
760 pause("bootpw", hz/10);
762 /* Set netmask to 255.0.0.0 */
764 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
766 sin->sin_addr.s_addr = htonl(0xff000000u);
767 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
768 (caddr_t) &ifctx->ireq, td);
771 "set if netmask, error=%d",
776 if (outstanding == 0 &&
777 (rtimo == 0 || time_second >= rtimo)) {
782 /* Determine new timeout. */
783 if (timo < MAX_RESEND_DELAY)
786 printf("DHCP/BOOTP timeout for server ");
787 print_sin_addr(&dst);
792 * Wait for up to timo seconds for a reply.
793 * The socket receive timeout was set to 1 second.
795 atimo = timo + time_second;
796 while (time_second < atimo) {
797 aio.iov_base = (caddr_t) &gctx->reply;
798 aio.iov_len = sizeof(gctx->reply);
802 auio.uio_segflg = UIO_SYSSPACE;
803 auio.uio_rw = UIO_READ;
805 auio.uio_resid = sizeof(gctx->reply);
809 error = soreceive(so, NULL, &auio,
810 NULL, NULL, &rcvflg);
811 gctx->secs = time_second - gctx->starttime;
812 for (ifctx = gctx->interfaces;
814 ifctx = ifctx->next) {
815 if (bootpc_ifctx_isresolved(ifctx) != 0 ||
816 bootpc_ifctx_isfailed(ifctx) != 0)
819 ifctx->call.secs = htons(gctx->secs);
821 if (error == EWOULDBLOCK)
825 len = sizeof(gctx->reply) - auio.uio_resid;
827 /* Do we have the required number of bytes ? */
828 if (len < BOOTP_MIN_LEN)
830 gctx->replylen = len;
833 if (gctx->reply.op != BOOTP_REPLY)
836 /* Is this an answer to our query */
837 for (ifctx = gctx->interfaces;
839 ifctx = ifctx->next) {
840 if (gctx->reply.xid != ifctx->call.xid)
843 /* Same HW address size ? */
844 if (gctx->reply.hlen != ifctx->call.hlen)
847 /* Correct HW address ? */
848 if (bcmp(gctx->reply.chaddr,
850 ifctx->call.hlen) != 0)
857 s = bootpc_tag(&gctx->tmptag,
870 s = "DHCP (unexpected)";
876 printf("Received %s packet"
879 ifctx->ireq.ifr_name);
880 print_in_addr(gctx->reply.siaddr);
881 if (gctx->reply.giaddr.s_addr !=
884 print_in_addr(gctx->reply.giaddr);
886 if (bootpc_received(gctx, ifctx) != 0) {
887 printf(" (accepted)");
888 if (ifctx->outstanding) {
889 ifctx->outstanding = 0;
892 /* Network settle delay */
893 if (outstanding == 0)
894 atimo = time_second +
897 printf(" (ignored)");
898 if (ifctx->gotrootpath) {
900 rtimo = time_second +
902 printf(" (got root path)");
904 printf(" (no root path)");
909 if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
912 /* Force a retry if halfway in DHCP negotiation */
914 for (ifctx = gctx->interfaces; ifctx != NULL;
915 ifctx = ifctx->next) {
916 if (ifctx->state == IF_DHCP_OFFERED) {
917 if (ifctx->dhcpquerytype == DHCP_DISCOVER)
920 ifctx->state = IF_DHCP_UNRESOLVED;
927 if (gotrootpath != 0) {
928 gctx->gotrootpath = gotrootpath;
929 if (rtimo != 0 && time_second >= rtimo)
932 } /* forever send/receive */
935 * XXX: These are errors of varying seriousness being silently
939 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
940 if (bootpc_ifctx_isresolved(ifctx) == 0) {
941 printf("%s timeout for interface %s\n",
942 ifctx->dhcpquerytype != DHCP_NOMSG ?
944 ifctx->ireq.ifr_name);
947 if (gctx->gotrootpath != 0) {
949 printf("Got a root path, ignoring remaining timeout\n");
954 #ifndef BOOTP_NFSROOT
955 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
956 if (bootpc_ifctx_isresolved(ifctx) != 0) {
973 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
974 struct bootpc_globalcontext *gctx, struct thread *td)
976 struct sockaddr_in *sin;
981 struct sockaddr_dl *sdl;
983 error = socreate(AF_INET, &ifctx->so, SOCK_DGRAM, 0, td->td_ucred, td);
985 panic("nfs_boot: socreate, error=%d", error);
991 * Bring up the interface.
993 * Get the old interface flags and or IFF_UP into them; if
994 * IFF_UP set blindly, interface selection can be clobbered.
996 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, td);
998 panic("bootpc_fakeup_interface: GIFFLAGS, error=%d", error);
999 ireq->ifr_flags |= IFF_UP;
1000 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, td);
1002 panic("bootpc_fakeup_interface: SIFFLAGS, error=%d", error);
1005 * Do enough of ifconfig(8) so that the chosen interface
1006 * can talk to the servers. (just set the address)
1009 /* addr is 0.0.0.0 */
1011 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1013 error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, td);
1014 if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1015 panic("bootpc_fakeup_interface: "
1016 "set if addr, error=%d", error);
1018 /* netmask is 255.0.0.0 */
1020 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1022 sin->sin_addr.s_addr = htonl(0xff000000u);
1023 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t)ireq, td);
1025 panic("bootpc_fakeup_interface: set if netmask, error=%d",
1028 /* Broadcast is 255.255.255.255 */
1030 sin = (struct sockaddr_in *)&ireq->ifr_addr;
1032 clear_sinaddr(&ifctx->broadcast);
1033 sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
1034 ifctx->broadcast.sin_addr.s_addr = sin->sin_addr.s_addr;
1036 error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t)ireq, td);
1038 panic("bootpc_fakeup_interface: "
1039 "set if broadcast addr, error=%d",
1042 /* Get HW address */
1045 TAILQ_FOREACH(ifa, &ifctx->ifp->if_addrhead, ifa_link)
1046 if (ifa->ifa_addr->sa_family == AF_LINK) {
1047 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1048 if (sdl->sdl_type == IFT_ETHER)
1053 panic("bootpc: Unable to find HW address for %s",
1054 ifctx->ireq.ifr_name);
1062 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
1063 struct bootpc_globalcontext *gctx, struct thread *td)
1066 struct sockaddr_in defdst;
1067 struct sockaddr_in defmask;
1068 struct sockaddr_in *sin;
1071 struct sockaddr_in *myaddr;
1072 struct sockaddr_in *netmask;
1073 struct sockaddr_in *gw;
1075 ireq = &ifctx->ireq;
1077 myaddr = &ifctx->myaddr;
1078 netmask = &ifctx->netmask;
1081 if (bootpc_ifctx_isresolved(ifctx) == 0) {
1083 /* Shutdown interfaces where BOOTP failed */
1085 printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
1086 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, td);
1088 panic("bootpc_adjust_interface: "
1089 "SIOCGIFFLAGS, error=%d", error);
1090 ireq->ifr_flags &= ~IFF_UP;
1091 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, td);
1093 panic("bootpc_adjust_interface: "
1094 "SIOCSIFFLAGS, error=%d", error);
1096 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1098 error = ifioctl(so, SIOCDIFADDR, (caddr_t) ireq, td);
1099 if (error != 0 && (error != EEXIST ||
1100 ifctx == gctx->interfaces))
1101 panic("bootpc_adjust_interface: "
1102 "SIOCDIFADDR, error=%d", error);
1107 printf("Adjusted interface %s\n", ifctx->ireq.ifr_name);
1109 * Do enough of ifconfig(8) so that the chosen interface
1110 * can talk to the servers. (just set the address)
1112 bcopy(netmask, &ireq->ifr_addr, sizeof(*netmask));
1113 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t) ireq, td);
1115 panic("bootpc_adjust_interface: "
1116 "set if netmask, error=%d", error);
1118 /* Broadcast is with host part of IP address all 1's */
1120 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1122 sin->sin_addr.s_addr = myaddr->sin_addr.s_addr |
1123 ~ netmask->sin_addr.s_addr;
1124 error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t) ireq, td);
1126 panic("bootpc_adjust_interface: "
1127 "set if broadcast addr, error=%d", error);
1129 bcopy(myaddr, &ireq->ifr_addr, sizeof(*myaddr));
1130 error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, td);
1131 if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1132 panic("bootpc_adjust_interface: "
1133 "set if addr, error=%d", error);
1135 /* Add new default route */
1137 if (ifctx->gotgw != 0 || gctx->gotgw == 0) {
1138 clear_sinaddr(&defdst);
1139 clear_sinaddr(&defmask);
1140 /* XXX MRT just table 0 */
1141 error = rtrequest_fib(RTM_ADD,
1142 (struct sockaddr *) &defdst,
1143 (struct sockaddr *) gw,
1144 (struct sockaddr *) &defmask,
1145 (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL, 0);
1147 printf("bootpc_adjust_interface: "
1148 "add net route, error=%d\n", error);
1157 setfs(struct sockaddr_in *addr, char *path, char *p,
1158 const struct in_addr *siaddr)
1161 if (getip(&p, &addr->sin_addr) == 0) {
1162 if (siaddr != NULL && *p == '/')
1163 bcopy(siaddr, &addr->sin_addr, sizeof(struct in_addr));
1172 addr->sin_len = sizeof(struct sockaddr_in);
1173 addr->sin_family = AF_INET;
1175 strlcpy(path, p, MNAMELEN);
1180 getip(char **ptr, struct in_addr *addr)
1188 if (((val = getdec(&p)) < 0) || (val > 255))
1194 if (((val = getdec(&p)) < 0) || (val > 255))
1200 if (((val = getdec(&p)) < 0) || (val > 255))
1206 if (((val = getdec(&p)) < 0) || (val > 255))
1210 addr->s_addr = htonl(ip);
1223 if ((*p < '0') || (*p > '9'))
1225 while ((*p >= '0') && (*p <= '9')) {
1226 ret = ret * 10 + (*p - '0');
1234 mountopts(struct nfs_args *args, char *p)
1236 args->version = NFS_ARGSVERSION;
1239 args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
1240 args->sotype = SOCK_DGRAM;
1242 nfs_parse_options(p, args);
1246 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
1252 alignedlen = ( len + 3 ) & ~3;
1254 if (m->m_len < alignedlen) {
1255 m = m_pullup(m, alignedlen);
1261 bcopy(mtod(m, u_char *), buf, len);
1262 m_adj(m, alignedlen);
1268 xdr_int_decode(struct mbuf **mptr, int *iptr)
1272 if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
1274 *iptr = fxdr_unsigned(u_int32_t, i);
1279 print_sin_addr(struct sockaddr_in *sin)
1282 print_in_addr(sin->sin_addr);
1286 print_in_addr(struct in_addr addr)
1290 ip = ntohl(addr.s_addr);
1291 printf("%d.%d.%d.%d",
1292 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1296 bootpc_compose_query(struct bootpc_ifcontext *ifctx,
1297 struct bootpc_globalcontext *gctx, struct thread *td)
1299 unsigned char *vendp;
1300 unsigned char vendor_client[64];
1302 uint8_t vendor_client_len;
1304 ifctx->gotrootpath = 0;
1306 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1309 ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */
1310 ifctx->call.htype = 1; /* 10mb ethernet */
1311 ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1312 ifctx->call.hops = 0;
1313 if (bootpc_ifctx_isunresolved(ifctx) != 0)
1315 ifctx->call.xid = txdr_unsigned(ifctx->xid);
1316 bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1318 vendp = ifctx->call.vend;
1319 *vendp++ = 99; /* RFC1048 cookie */
1323 *vendp++ = TAG_MAXMSGSIZE;
1325 *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1326 *vendp++ = sizeof(struct bootp_packet) & 255;
1328 snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1329 ostype, MACHINE, osrelease);
1330 vendor_client_len = strlen(vendor_client);
1331 *vendp++ = TAG_VENDOR_INDENTIFIER;
1332 *vendp++ = vendor_client_len;
1333 memcpy(vendp, vendor_client, vendor_client_len);
1334 vendp += vendor_client_len;;
1335 ifctx->dhcpquerytype = DHCP_NOMSG;
1336 switch (ifctx->state) {
1337 case IF_DHCP_UNRESOLVED:
1338 *vendp++ = TAG_DHCP_MSGTYPE;
1340 *vendp++ = DHCP_DISCOVER;
1341 ifctx->dhcpquerytype = DHCP_DISCOVER;
1342 ifctx->gotdhcpserver = 0;
1344 case IF_DHCP_OFFERED:
1345 *vendp++ = TAG_DHCP_MSGTYPE;
1347 *vendp++ = DHCP_REQUEST;
1348 ifctx->dhcpquerytype = DHCP_REQUEST;
1349 *vendp++ = TAG_DHCP_REQ_ADDR;
1351 memcpy(vendp, &ifctx->reply.yiaddr, 4);
1353 if (ifctx->gotdhcpserver != 0) {
1354 *vendp++ = TAG_DHCP_SERVERID;
1356 memcpy(vendp, &ifctx->dhcpserver, 4);
1359 *vendp++ = TAG_DHCP_LEASETIME;
1361 leasetime = htonl(300);
1362 memcpy(vendp, &leasetime, 4);
1370 ifctx->call.secs = 0;
1371 ifctx->call.flags = htons(0x8000); /* We need a broadcast answer */
1375 bootpc_hascookie(struct bootp_packet *bp)
1378 return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1379 bp->vend[2] == 83 && bp->vend[3] == 99);
1383 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1384 unsigned char *start, int len, int tag)
1390 if (tctx->badtag != 0 || tctx->badopt != 0)
1398 if (code == TAG_PAD)
1400 if (code == TAG_END)
1402 if (j >= ej || j + *j + 1 > ej) {
1408 if (tctx->taglen + len > TAG_MAXLEN) {
1414 memcpy(tctx->buf + tctx->taglen,
1416 tctx->taglen += len;
1418 if (code == TAG_OVERLOAD)
1419 tctx->overload = *j;
1425 static unsigned char *
1426 bootpc_tag(struct bootpc_tagcontext *tctx,
1427 struct bootp_packet *bp, int len, int tag)
1435 if (bootpc_hascookie(bp) == 0)
1438 bootpc_tag_helper(tctx, &bp->vend[4],
1439 (unsigned char *) bp + len - &bp->vend[4], tag);
1441 if ((tctx->overload & OVERLOAD_FILE) != 0)
1442 bootpc_tag_helper(tctx,
1443 (unsigned char *) bp->file,
1446 if ((tctx->overload & OVERLOAD_SNAME) != 0)
1447 bootpc_tag_helper(tctx,
1448 (unsigned char *) bp->sname,
1452 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1454 tctx->buf[tctx->taglen] = '\0';
1459 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1460 struct bootpc_globalcontext *gctx)
1466 ifctx->gotnetmask = 0;
1468 clear_sinaddr(&ifctx->myaddr);
1469 clear_sinaddr(&ifctx->netmask);
1470 clear_sinaddr(&ifctx->gw);
1472 ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1474 ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1476 printf("%s at ", ifctx->ireq.ifr_name);
1477 print_sin_addr(&ifctx->myaddr);
1479 print_in_addr(ifctx->reply.siaddr);
1481 ifctx->gw.sin_addr = ifctx->reply.giaddr;
1482 if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1483 printf(" via gateway ");
1484 print_in_addr(ifctx->reply.giaddr);
1487 /* This call used for the side effect (overload flag) */
1488 (void) bootpc_tag(&gctx->tmptag,
1489 &ifctx->reply, ifctx->replylen, TAG_END);
1491 if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1492 if (ifctx->reply.sname[0] != '\0')
1493 printf(" server name %s", ifctx->reply.sname);
1494 if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1495 if (ifctx->reply.file[0] != '\0')
1496 printf(" boot file %s", ifctx->reply.file);
1500 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1503 if (gctx->tag.taglen != 4)
1504 panic("bootpc: subnet mask len is %d",
1506 bcopy(p, &ifctx->netmask.sin_addr, 4);
1507 ifctx->gotnetmask = 1;
1508 printf("subnet mask ");
1509 print_sin_addr(&ifctx->netmask);
1513 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1517 if (gctx->tag.taglen % 4)
1518 panic("bootpc: Router Len is %d", gctx->tag.taglen);
1519 if (gctx->tag.taglen > 0) {
1520 bcopy(p, &ifctx->gw.sin_addr, 4);
1522 print_sin_addr(&ifctx->gw);
1529 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1532 if (gctx->setrootfs != NULL) {
1533 printf("rootfs %s (ignored) ", p);
1534 } else if (setfs(&nd->root_saddr,
1535 nd->root_hostnam, p, &ifctx->reply.siaddr)) {
1537 printf("root_server ");
1538 print_sin_addr(&nd->root_saddr);
1541 printf("rootfs %s ", p);
1542 gctx->gotrootpath = 1;
1543 ifctx->gotrootpath = 1;
1544 gctx->setrootfs = ifctx;
1546 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1550 mountopts(&nd->root_args, p);
1551 printf("rootopts %s ", p);
1554 panic("Failed to set rootfs to %s", p);
1557 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1560 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1561 panic("bootpc: hostname >= %d bytes",
1563 if (gctx->sethostname != NULL) {
1564 printf("hostname %s (ignored) ", p);
1566 strcpy(nd->my_hostnam, p);
1567 strcpy(hostname, p);
1568 printf("hostname %s ", hostname);
1569 gctx->sethostname = ifctx;
1572 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1574 if (p != NULL) { /* store in a sysctl variable */
1575 int i, l = sizeof(bootp_cookie) - 1;
1576 for (i = 0; i < l && p[i] != '\0'; i++)
1577 bootp_cookie[i] = p[i];
1584 if (ifctx->gotnetmask == 0) {
1585 if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1586 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1587 else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1588 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1590 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1592 if (ifctx->gotgw == 0) {
1594 ifctx->gw.sin_addr.s_addr = ifctx->myaddr.sin_addr.s_addr;
1601 struct bootpc_ifcontext *ifctx, *nctx; /* Interface BOOTP contexts */
1602 struct bootpc_globalcontext *gctx; /* Global BOOTP context */
1605 #ifndef BOOTP_WIRED_TO
1608 struct nfsv3_diskless *nd;
1611 nd = &nfsv3_diskless;
1615 * If already filled in, don't touch it here
1617 if (nfs_diskless_valid != 0)
1620 gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK | M_ZERO);
1622 panic("Failed to allocate bootp global context structure");
1624 gctx->xid = ~0xFFFF;
1625 gctx->starttime = time_second;
1628 * Find a network interface.
1630 #ifdef BOOTP_WIRED_TO
1631 printf("bootpc_init: wired to interface '%s'\n",
1632 __XSTRING(BOOTP_WIRED_TO));
1636 * Preallocate interface context storage, if another interface
1637 * attaches and wins the race, it won't be eligible for bootp.
1640 for (ifp = TAILQ_FIRST(&ifnet), ifcnt = 0;
1642 ifp = TAILQ_NEXT(ifp, if_link)) {
1643 if ((ifp->if_flags &
1644 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1651 panic("bootpc_init: no eligible interfaces");
1652 for (; ifcnt > 0; ifcnt--)
1657 for (ifp = TAILQ_FIRST(&ifnet), ifctx = gctx->interfaces;
1658 ifp != NULL && ifctx != NULL;
1659 ifp = TAILQ_NEXT(ifp, if_link)) {
1660 strlcpy(ifctx->ireq.ifr_name, ifp->if_xname,
1661 sizeof(ifctx->ireq.ifr_name));
1662 #ifdef BOOTP_WIRED_TO
1663 if (strcmp(ifctx->ireq.ifr_name,
1664 __XSTRING(BOOTP_WIRED_TO)) != 0)
1667 if ((ifp->if_flags &
1668 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1673 ifctx = ifctx->next;
1677 if (gctx->interfaces == NULL || gctx->interfaces->ifp == NULL) {
1678 #ifdef BOOTP_WIRED_TO
1679 panic("bootpc_init: Could not find interface specified "
1680 "by BOOTP_WIRED_TO: "
1681 __XSTRING(BOOTP_WIRED_TO));
1683 panic("bootpc_init: no suitable interface");
1687 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1688 bootpc_fakeup_interface(ifctx, gctx, td);
1690 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1691 bootpc_compose_query(ifctx, gctx, td);
1693 error = bootpc_call(gctx, td);
1696 #ifdef BOOTP_NFSROOT
1697 panic("BOOTP call failed");
1699 printf("BOOTP call failed\n");
1703 rootdevnames[0] = "nfs:";
1704 mountopts(&nd->root_args, NULL);
1706 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1707 if (bootpc_ifctx_isresolved(ifctx) != 0)
1708 bootpc_decode_reply(nd, ifctx, gctx);
1710 #ifdef BOOTP_NFSROOT
1711 if (gctx->gotrootpath == 0)
1712 panic("bootpc: No root path offered");
1715 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
1716 bootpc_adjust_interface(ifctx, gctx, td);
1721 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1722 if (ifctx->gotrootpath != 0)
1724 if (ifctx == NULL) {
1725 for (ifctx = gctx->interfaces;
1727 ifctx = ifctx->next)
1728 if (bootpc_ifctx_isresolved(ifctx) != 0)
1734 if (gctx->gotrootpath != 0) {
1736 setenv("boot.netif.name", ifctx->ifp->if_xname);
1738 error = md_mount(&nd->root_saddr, nd->root_hostnam,
1739 nd->root_fh, &nd->root_fhsize,
1740 &nd->root_args, td);
1742 panic("nfs_boot: mountd root, error=%d", error);
1744 nfs_diskless_valid = 3;
1747 strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1748 bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1749 bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1750 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1751 ifctx->myaddr.sin_addr.s_addr |
1752 ~ ifctx->netmask.sin_addr.s_addr;
1753 bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1756 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = nctx) {
1758 free(ifctx, M_TEMP);
1765 * Given a server pathname, get an NFS file handle.
1766 * Also, sets sin->sin_port to the NFS service port.
1769 md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp, int *fhsizep,
1770 struct nfs_args *args, struct thread *td)
1778 /* XXX honor v2/v3 flags in args->flags? */
1780 /* First try NFS v3 */
1781 /* Get port number for MOUNTD. */
1782 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1783 &mdsin->sin_port, td);
1785 m = xdr_string_encode(path, strlen(path));
1787 /* Do RPC to mountd. */
1788 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1789 RPCMNT_MOUNT, &m, NULL, td);
1792 args->flags |= NFSMNT_NFSV3;
1795 /* Fallback to NFS v2 */
1797 /* Get port number for MOUNTD. */
1798 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1799 &mdsin->sin_port, td);
1803 m = xdr_string_encode(path, strlen(path));
1805 /* Do RPC to mountd. */
1806 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1807 RPCMNT_MOUNT, &m, NULL, td);
1809 return error; /* message already freed */
1815 if (xdr_int_decode(&m, &error) != 0 || error != 0)
1818 if ((args->flags & NFSMNT_NFSV3) != 0) {
1819 if (xdr_int_decode(&m, fhsizep) != 0 ||
1820 *fhsizep > NFSX_V3FHMAX ||
1824 *fhsizep = NFSX_V2FH;
1826 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
1829 if (args->flags & NFSMNT_NFSV3) {
1830 if (xdr_int_decode(&m, &authcount) != 0)
1833 if (authcount < 0 || authcount > 100)
1835 while (authcount > 0) {
1836 if (xdr_int_decode(&m, &authver) != 0)
1838 if (authver == RPCAUTH_UNIX)
1842 if (authunixok == 0)
1846 /* Set port number for NFS use. */
1847 error = krpc_portmap(mdsin, NFS_PROG,
1849 NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
1850 &mdsin->sin_port, td);
1862 SYSINIT(bootp_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, bootpc_init, NULL);