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 error = rtrequest(RTM_ADD,
1141 (struct sockaddr *) &defdst,
1142 (struct sockaddr *) gw,
1143 (struct sockaddr *) &defmask,
1144 (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
1146 printf("bootpc_adjust_interface: "
1147 "add net route, error=%d\n", error);
1156 setfs(struct sockaddr_in *addr, char *path, char *p,
1157 const struct in_addr *siaddr)
1160 if (getip(&p, &addr->sin_addr) == 0) {
1161 if (siaddr != NULL && *p == '/')
1162 bcopy(siaddr, &addr->sin_addr, sizeof(struct in_addr));
1171 addr->sin_len = sizeof(struct sockaddr_in);
1172 addr->sin_family = AF_INET;
1174 strlcpy(path, p, MNAMELEN);
1179 getip(char **ptr, struct in_addr *addr)
1187 if (((val = getdec(&p)) < 0) || (val > 255))
1193 if (((val = getdec(&p)) < 0) || (val > 255))
1199 if (((val = getdec(&p)) < 0) || (val > 255))
1205 if (((val = getdec(&p)) < 0) || (val > 255))
1209 addr->s_addr = htonl(ip);
1222 if ((*p < '0') || (*p > '9'))
1224 while ((*p >= '0') && (*p <= '9')) {
1225 ret = ret * 10 + (*p - '0');
1233 mountopts(struct nfs_args *args, char *p)
1235 args->version = NFS_ARGSVERSION;
1238 args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
1239 args->sotype = SOCK_DGRAM;
1241 nfs_parse_options(p, args);
1245 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
1251 alignedlen = ( len + 3 ) & ~3;
1253 if (m->m_len < alignedlen) {
1254 m = m_pullup(m, alignedlen);
1260 bcopy(mtod(m, u_char *), buf, len);
1261 m_adj(m, alignedlen);
1267 xdr_int_decode(struct mbuf **mptr, int *iptr)
1271 if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
1273 *iptr = fxdr_unsigned(u_int32_t, i);
1278 print_sin_addr(struct sockaddr_in *sin)
1281 print_in_addr(sin->sin_addr);
1285 print_in_addr(struct in_addr addr)
1289 ip = ntohl(addr.s_addr);
1290 printf("%d.%d.%d.%d",
1291 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1295 bootpc_compose_query(struct bootpc_ifcontext *ifctx,
1296 struct bootpc_globalcontext *gctx, struct thread *td)
1298 unsigned char *vendp;
1299 unsigned char vendor_client[64];
1301 uint8_t vendor_client_len;
1303 ifctx->gotrootpath = 0;
1305 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1308 ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */
1309 ifctx->call.htype = 1; /* 10mb ethernet */
1310 ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1311 ifctx->call.hops = 0;
1312 if (bootpc_ifctx_isunresolved(ifctx) != 0)
1314 ifctx->call.xid = txdr_unsigned(ifctx->xid);
1315 bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1317 vendp = ifctx->call.vend;
1318 *vendp++ = 99; /* RFC1048 cookie */
1322 *vendp++ = TAG_MAXMSGSIZE;
1324 *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1325 *vendp++ = sizeof(struct bootp_packet) & 255;
1327 snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1328 ostype, MACHINE, osrelease);
1329 vendor_client_len = strlen(vendor_client);
1330 *vendp++ = TAG_VENDOR_INDENTIFIER;
1331 *vendp++ = vendor_client_len;
1332 memcpy(vendp, vendor_client, vendor_client_len);
1333 vendp += vendor_client_len;;
1334 ifctx->dhcpquerytype = DHCP_NOMSG;
1335 switch (ifctx->state) {
1336 case IF_DHCP_UNRESOLVED:
1337 *vendp++ = TAG_DHCP_MSGTYPE;
1339 *vendp++ = DHCP_DISCOVER;
1340 ifctx->dhcpquerytype = DHCP_DISCOVER;
1341 ifctx->gotdhcpserver = 0;
1343 case IF_DHCP_OFFERED:
1344 *vendp++ = TAG_DHCP_MSGTYPE;
1346 *vendp++ = DHCP_REQUEST;
1347 ifctx->dhcpquerytype = DHCP_REQUEST;
1348 *vendp++ = TAG_DHCP_REQ_ADDR;
1350 memcpy(vendp, &ifctx->reply.yiaddr, 4);
1352 if (ifctx->gotdhcpserver != 0) {
1353 *vendp++ = TAG_DHCP_SERVERID;
1355 memcpy(vendp, &ifctx->dhcpserver, 4);
1358 *vendp++ = TAG_DHCP_LEASETIME;
1360 leasetime = htonl(300);
1361 memcpy(vendp, &leasetime, 4);
1369 ifctx->call.secs = 0;
1370 ifctx->call.flags = htons(0x8000); /* We need a broadcast answer */
1374 bootpc_hascookie(struct bootp_packet *bp)
1377 return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1378 bp->vend[2] == 83 && bp->vend[3] == 99);
1382 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1383 unsigned char *start, int len, int tag)
1389 if (tctx->badtag != 0 || tctx->badopt != 0)
1397 if (code == TAG_PAD)
1399 if (code == TAG_END)
1401 if (j >= ej || j + *j + 1 > ej) {
1407 if (tctx->taglen + len > TAG_MAXLEN) {
1413 memcpy(tctx->buf + tctx->taglen,
1415 tctx->taglen += len;
1417 if (code == TAG_OVERLOAD)
1418 tctx->overload = *j;
1424 static unsigned char *
1425 bootpc_tag(struct bootpc_tagcontext *tctx,
1426 struct bootp_packet *bp, int len, int tag)
1434 if (bootpc_hascookie(bp) == 0)
1437 bootpc_tag_helper(tctx, &bp->vend[4],
1438 (unsigned char *) bp + len - &bp->vend[4], tag);
1440 if ((tctx->overload & OVERLOAD_FILE) != 0)
1441 bootpc_tag_helper(tctx,
1442 (unsigned char *) bp->file,
1445 if ((tctx->overload & OVERLOAD_SNAME) != 0)
1446 bootpc_tag_helper(tctx,
1447 (unsigned char *) bp->sname,
1451 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1453 tctx->buf[tctx->taglen] = '\0';
1458 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1459 struct bootpc_globalcontext *gctx)
1465 ifctx->gotnetmask = 0;
1467 clear_sinaddr(&ifctx->myaddr);
1468 clear_sinaddr(&ifctx->netmask);
1469 clear_sinaddr(&ifctx->gw);
1471 ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1473 ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1475 printf("%s at ", ifctx->ireq.ifr_name);
1476 print_sin_addr(&ifctx->myaddr);
1478 print_in_addr(ifctx->reply.siaddr);
1480 ifctx->gw.sin_addr = ifctx->reply.giaddr;
1481 if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1482 printf(" via gateway ");
1483 print_in_addr(ifctx->reply.giaddr);
1486 /* This call used for the side effect (overload flag) */
1487 (void) bootpc_tag(&gctx->tmptag,
1488 &ifctx->reply, ifctx->replylen, TAG_END);
1490 if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1491 if (ifctx->reply.sname[0] != '\0')
1492 printf(" server name %s", ifctx->reply.sname);
1493 if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1494 if (ifctx->reply.file[0] != '\0')
1495 printf(" boot file %s", ifctx->reply.file);
1499 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1502 if (gctx->tag.taglen != 4)
1503 panic("bootpc: subnet mask len is %d",
1505 bcopy(p, &ifctx->netmask.sin_addr, 4);
1506 ifctx->gotnetmask = 1;
1507 printf("subnet mask ");
1508 print_sin_addr(&ifctx->netmask);
1512 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1516 if (gctx->tag.taglen % 4)
1517 panic("bootpc: Router Len is %d", gctx->tag.taglen);
1518 if (gctx->tag.taglen > 0) {
1519 bcopy(p, &ifctx->gw.sin_addr, 4);
1521 print_sin_addr(&ifctx->gw);
1528 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1531 if (gctx->setrootfs != NULL) {
1532 printf("rootfs %s (ignored) ", p);
1533 } else if (setfs(&nd->root_saddr,
1534 nd->root_hostnam, p, &ifctx->reply.siaddr)) {
1536 printf("root_server ");
1537 print_sin_addr(&nd->root_saddr);
1540 printf("rootfs %s ", p);
1541 gctx->gotrootpath = 1;
1542 ifctx->gotrootpath = 1;
1543 gctx->setrootfs = ifctx;
1545 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1549 mountopts(&nd->root_args, p);
1550 printf("rootopts %s ", p);
1553 panic("Failed to set rootfs to %s", p);
1556 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1559 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1560 panic("bootpc: hostname >= %d bytes",
1562 if (gctx->sethostname != NULL) {
1563 printf("hostname %s (ignored) ", p);
1565 strcpy(nd->my_hostnam, p);
1566 strcpy(hostname, p);
1567 printf("hostname %s ", hostname);
1568 gctx->sethostname = ifctx;
1571 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1573 if (p != NULL) { /* store in a sysctl variable */
1574 int i, l = sizeof(bootp_cookie) - 1;
1575 for (i = 0; i < l && p[i] != '\0'; i++)
1576 bootp_cookie[i] = p[i];
1583 if (ifctx->gotnetmask == 0) {
1584 if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1585 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1586 else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1587 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1589 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1591 if (ifctx->gotgw == 0) {
1593 ifctx->gw.sin_addr.s_addr = ifctx->myaddr.sin_addr.s_addr;
1600 struct bootpc_ifcontext *ifctx, *nctx; /* Interface BOOTP contexts */
1601 struct bootpc_globalcontext *gctx; /* Global BOOTP context */
1604 #ifndef BOOTP_WIRED_TO
1607 struct nfsv3_diskless *nd;
1610 nd = &nfsv3_diskless;
1614 * If already filled in, don't touch it here
1616 if (nfs_diskless_valid != 0)
1619 gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK | M_ZERO);
1621 panic("Failed to allocate bootp global context structure");
1623 gctx->xid = ~0xFFFF;
1624 gctx->starttime = time_second;
1627 * Find a network interface.
1629 #ifdef BOOTP_WIRED_TO
1630 printf("bootpc_init: wired to interface '%s'\n",
1631 __XSTRING(BOOTP_WIRED_TO));
1635 * Preallocate interface context storage, if another interface
1636 * attaches and wins the race, it won't be eligible for bootp.
1639 for (ifp = TAILQ_FIRST(&ifnet), ifcnt = 0;
1641 ifp = TAILQ_NEXT(ifp, if_link)) {
1642 if ((ifp->if_flags &
1643 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1650 panic("bootpc_init: no eligible interfaces");
1651 for (; ifcnt > 0; ifcnt--)
1656 for (ifp = TAILQ_FIRST(&ifnet), ifctx = gctx->interfaces;
1657 ifp != NULL && ifctx != NULL;
1658 ifp = TAILQ_NEXT(ifp, if_link)) {
1659 strlcpy(ifctx->ireq.ifr_name, ifp->if_xname,
1660 sizeof(ifctx->ireq.ifr_name));
1661 #ifdef BOOTP_WIRED_TO
1662 if (strcmp(ifctx->ireq.ifr_name,
1663 __XSTRING(BOOTP_WIRED_TO)) != 0)
1666 if ((ifp->if_flags &
1667 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1672 ifctx = ifctx->next;
1676 if (gctx->interfaces == NULL || gctx->interfaces->ifp == NULL) {
1677 #ifdef BOOTP_WIRED_TO
1678 panic("bootpc_init: Could not find interface specified "
1679 "by BOOTP_WIRED_TO: "
1680 __XSTRING(BOOTP_WIRED_TO));
1682 panic("bootpc_init: no suitable interface");
1686 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1687 bootpc_fakeup_interface(ifctx, gctx, td);
1689 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1690 bootpc_compose_query(ifctx, gctx, td);
1692 error = bootpc_call(gctx, td);
1695 #ifdef BOOTP_NFSROOT
1696 panic("BOOTP call failed");
1698 printf("BOOTP call failed\n");
1702 rootdevnames[0] = "nfs:";
1703 mountopts(&nd->root_args, NULL);
1705 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1706 if (bootpc_ifctx_isresolved(ifctx) != 0)
1707 bootpc_decode_reply(nd, ifctx, gctx);
1709 #ifdef BOOTP_NFSROOT
1710 if (gctx->gotrootpath == 0)
1711 panic("bootpc: No root path offered");
1714 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
1715 bootpc_adjust_interface(ifctx, gctx, td);
1720 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1721 if (ifctx->gotrootpath != 0)
1723 if (ifctx == NULL) {
1724 for (ifctx = gctx->interfaces;
1726 ifctx = ifctx->next)
1727 if (bootpc_ifctx_isresolved(ifctx) != 0)
1733 if (gctx->gotrootpath != 0) {
1735 setenv("boot.netif.name", ifctx->ifp->if_xname);
1737 error = md_mount(&nd->root_saddr, nd->root_hostnam,
1738 nd->root_fh, &nd->root_fhsize,
1739 &nd->root_args, td);
1741 panic("nfs_boot: mountd root, error=%d", error);
1743 nfs_diskless_valid = 3;
1746 strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1747 bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1748 bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1749 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1750 ifctx->myaddr.sin_addr.s_addr |
1751 ~ ifctx->netmask.sin_addr.s_addr;
1752 bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1755 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = nctx) {
1757 free(ifctx, M_TEMP);
1764 * Given a server pathname, get an NFS file handle.
1765 * Also, sets sin->sin_port to the NFS service port.
1768 md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp, int *fhsizep,
1769 struct nfs_args *args, struct thread *td)
1777 /* XXX honor v2/v3 flags in args->flags? */
1779 /* First try NFS v3 */
1780 /* Get port number for MOUNTD. */
1781 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1782 &mdsin->sin_port, td);
1784 m = xdr_string_encode(path, strlen(path));
1786 /* Do RPC to mountd. */
1787 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1788 RPCMNT_MOUNT, &m, NULL, td);
1791 args->flags |= NFSMNT_NFSV3;
1794 /* Fallback to NFS v2 */
1796 /* Get port number for MOUNTD. */
1797 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1798 &mdsin->sin_port, td);
1802 m = xdr_string_encode(path, strlen(path));
1804 /* Do RPC to mountd. */
1805 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1806 RPCMNT_MOUNT, &m, NULL, td);
1808 return error; /* message already freed */
1814 if (xdr_int_decode(&m, &error) != 0 || error != 0)
1817 if ((args->flags & NFSMNT_NFSV3) != 0) {
1818 if (xdr_int_decode(&m, fhsizep) != 0 ||
1819 *fhsizep > NFSX_V3FHMAX ||
1823 *fhsizep = NFSX_V2FH;
1825 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
1828 if (args->flags & NFSMNT_NFSV3) {
1829 if (xdr_int_decode(&m, &authcount) != 0)
1832 if (authcount < 0 || authcount > 100)
1834 while (authcount > 0) {
1835 if (xdr_int_decode(&m, &authver) != 0)
1837 if (authver == RPCAUTH_UNIX)
1841 if (authunixok == 0)
1845 /* Set port number for NFS use. */
1846 error = krpc_portmap(mdsin, NFS_PROG,
1848 NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
1849 &mdsin->sin_port, td);
1861 SYSINIT(bootp_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, bootpc_init, NULL);