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/route.h>
68 #include <netinet/in.h>
69 #include <netinet/in_var.h>
70 #include <net/if_types.h>
71 #include <net/if_dl.h>
74 #include <nfs/nfsproto.h>
75 #include <nfsclient/nfs.h>
76 #include <nfs/nfsdiskless.h>
78 #include <nfs/xdr_subs.h>
81 #define BOOTP_MIN_LEN 300 /* Minimum size of bootp udp packet */
83 #ifndef BOOTP_SETTLE_DELAY
84 #define BOOTP_SETTLE_DELAY 3
88 * Wait 10 seconds for interface appearance
89 * USB ethernet adapters might require some time to pop up
91 #ifndef BOOTP_IFACE_WAIT_TIMEOUT
92 #define BOOTP_IFACE_WAIT_TIMEOUT 10
96 * What is the longest we will wait before re-sending a request?
97 * Note this is also the frequency of "RPC timeout" messages.
98 * The re-send loop count sup linearly to this maximum, so the
99 * first complaint will happen after (1+2+3+4+5)=15 seconds.
101 #define MAX_RESEND_DELAY 5 /* seconds */
103 /* Definitions from RFC951 */
104 struct bootp_packet {
112 struct in_addr ciaddr;
113 struct in_addr yiaddr;
114 struct in_addr siaddr;
115 struct in_addr giaddr;
116 unsigned char chaddr[16];
119 unsigned char vend[1222];
122 struct bootpc_ifcontext {
123 STAILQ_ENTRY(bootpc_ifcontext) next;
124 struct bootp_packet call;
125 struct bootp_packet reply;
130 struct in_aliasreq _in_alias_req;
132 #define ireq _req._ifreq
133 #define iareq _req._in_alias_req
135 struct sockaddr_dl *sdl;
136 struct sockaddr_in myaddr;
137 struct sockaddr_in netmask;
138 struct sockaddr_in gw;
154 int dhcpquerytype; /* dhcp type sent */
155 struct in_addr dhcpserver;
159 #define TAG_MAXLEN 1024
160 struct bootpc_tagcontext {
161 char buf[TAG_MAXLEN + 1];
169 struct bootpc_globalcontext {
170 STAILQ_HEAD(, bootpc_ifcontext) interfaces;
172 int any_root_overrides;
178 struct bootp_packet reply;
180 struct bootpc_ifcontext *setrootfs;
181 struct bootpc_ifcontext *sethostname;
182 struct bootpc_tagcontext tmptag;
183 struct bootpc_tagcontext tag;
186 #define IPPORT_BOOTPC 68
187 #define IPPORT_BOOTPS 67
189 #define BOOTP_REQUEST 1
190 #define BOOTP_REPLY 2
193 #define TAG_PAD 0 /* Pad option, implicit length 1 */
194 #define TAG_SUBNETMASK 1 /* RFC 950 subnet mask */
195 #define TAG_ROUTERS 3 /* Routers (in order of preference) */
196 #define TAG_HOSTNAME 12 /* Client host name */
197 #define TAG_ROOT 17 /* Root path */
199 /* DHCP specific tags */
200 #define TAG_OVERLOAD 52 /* Option Overload */
201 #define TAG_MAXMSGSIZE 57 /* Maximum DHCP Message Size */
203 #define TAG_END 255 /* End Option (i.e. no more options) */
205 /* Overload values */
206 #define OVERLOAD_FILE 1
207 #define OVERLOAD_SNAME 2
209 /* Site specific tags: */
210 #define TAG_ROOTOPTS 130
211 #define TAG_COOKIE 134 /* ascii info for userland, via sysctl */
213 #define TAG_DHCP_MSGTYPE 53
214 #define TAG_DHCP_REQ_ADDR 50
215 #define TAG_DHCP_SERVERID 54
216 #define TAG_DHCP_LEASETIME 51
218 #define TAG_VENDOR_INDENTIFIER 60
221 #define DHCP_DISCOVER 1
223 #define DHCP_REQUEST 3
226 /* NFS read/write block size */
227 #ifndef BOOTP_BLOCKSIZE
228 #define BOOTP_BLOCKSIZE 8192
231 static char bootp_cookie[128];
232 static struct socket *bootp_so;
233 SYSCTL_STRING(_kern, OID_AUTO, bootp_cookie, CTLFLAG_RD,
234 bootp_cookie, 0, "Cookie (T134) supplied by bootp server");
237 static int md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp,
238 int *fhsizep, struct nfs_args *args, struct thread *td);
239 static int setfs(struct sockaddr_in *addr, char *path, char *p,
240 const struct in_addr *siaddr);
241 static int getdec(char **ptr);
242 static int getip(char **ptr, struct in_addr *ip);
243 static void mountopts(struct nfs_args *args, char *p);
244 static int xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len);
245 static int xdr_int_decode(struct mbuf **ptr, int *iptr);
246 static void print_in_addr(struct in_addr addr);
247 static void print_sin_addr(struct sockaddr_in *addr);
248 static void clear_sinaddr(struct sockaddr_in *sin);
249 static void allocifctx(struct bootpc_globalcontext *gctx);
250 static void bootpc_compose_query(struct bootpc_ifcontext *ifctx,
252 static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx,
253 struct bootp_packet *bp, int len, int tag);
254 static void bootpc_tag_helper(struct bootpc_tagcontext *tctx,
255 unsigned char *start, int len, int tag);
258 void bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma);
259 void bootpboot_p_rtentry(struct rtentry *rt);
260 void bootpboot_p_tree(struct radix_node *rn);
261 void bootpboot_p_rtlist(void);
262 void bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa);
263 void bootpboot_p_iflist(void);
266 static int bootpc_call(struct bootpc_globalcontext *gctx,
269 static void bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
272 static int bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
273 struct bootpc_globalcontext *gctx, struct thread *td);
275 static void bootpc_decode_reply(struct nfsv3_diskless *nd,
276 struct bootpc_ifcontext *ifctx,
277 struct bootpc_globalcontext *gctx);
279 static int bootpc_received(struct bootpc_globalcontext *gctx,
280 struct bootpc_ifcontext *ifctx);
282 static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
283 static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
284 static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
287 * In order to have multiple active interfaces with address 0.0.0.0
288 * and be able to send data to a selected interface, we first set
289 * mask to /8 on all interfaces, and temporarily set it to /0 when
295 bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma)
299 printf("(sockaddr *) <null>");
302 switch (sa->sa_family) {
305 struct sockaddr_in *sin;
307 sin = (struct sockaddr_in *) sa;
311 sin = (struct sockaddr_in *) ma;
319 struct sockaddr_dl *sli;
322 sli = (struct sockaddr_dl *) sa;
323 printf("link %.*s ", sli->sdl_nlen, sli->sdl_data);
324 for (i = 0; i < sli->sdl_alen; i++) {
327 printf("%x", ((unsigned char *) LLADDR(sli))[i]);
332 printf("af%d", sa->sa_family);
337 bootpboot_p_rtentry(struct rtentry *rt)
340 bootpboot_p_sa(rt_key(rt), rt_mask(rt));
342 bootpboot_p_sa(rt->rt_gateway, NULL);
344 printf("flags %x", (unsigned short) rt->rt_flags);
345 printf(" %d", (int) rt->rt_rmx.rmx_expire);
346 printf(" %s\n", rt->rt_ifp->if_xname);
350 bootpboot_p_tree(struct radix_node *rn)
354 if (rn->rn_bit < 0) {
355 if ((rn->rn_flags & RNF_ROOT) != 0) {
357 bootpboot_p_rtentry((struct rtentry *) rn);
359 rn = rn->rn_dupedkey;
361 bootpboot_p_tree(rn->rn_left);
362 bootpboot_p_tree(rn->rn_right);
369 bootpboot_p_rtlist(void)
371 struct radix_node_head *rnh;
373 printf("Routing table:\n");
374 rnh = rt_tables_get_rnh(0, AF_INET);
377 RADIX_NODE_HEAD_RLOCK(rnh); /* could sleep XXX */
378 bootpboot_p_tree(rnh->rnh_treetop);
379 RADIX_NODE_HEAD_RUNLOCK(rnh);
383 bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
386 printf("%s flags %x, addr ",
387 ifp->if_xname, ifp->if_flags);
388 print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
389 printf(", broadcast ");
390 print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
391 printf(", netmask ");
392 print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
397 bootpboot_p_iflist(void)
402 printf("Interface list:\n");
404 for (ifp = TAILQ_FIRST(&V_ifnet);
406 ifp = TAILQ_NEXT(ifp, if_link)) {
407 for (ifa = TAILQ_FIRST(&ifp->if_addrhead);
409 ifa = TAILQ_NEXT(ifa, ifa_link))
410 if (ifa->ifa_addr->sa_family == AF_INET)
411 bootpboot_p_if(ifp, ifa);
415 #endif /* defined(BOOTP_DEBUG) */
418 clear_sinaddr(struct sockaddr_in *sin)
421 bzero(sin, sizeof(*sin));
422 sin->sin_len = sizeof(*sin);
423 sin->sin_family = AF_INET;
424 sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
429 allocifctx(struct bootpc_globalcontext *gctx)
431 struct bootpc_ifcontext *ifctx;
433 ifctx = malloc(sizeof(*ifctx), M_TEMP, M_WAITOK | M_ZERO);
434 ifctx->xid = gctx->xid;
436 ifctx->state = IF_BOOTP_UNRESOLVED;
438 ifctx->state = IF_DHCP_UNRESOLVED;
441 STAILQ_INSERT_TAIL(&gctx->interfaces, ifctx, next);
445 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
448 if (ifctx->state == IF_BOOTP_RESOLVED ||
449 ifctx->state == IF_DHCP_RESOLVED)
455 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
458 if (ifctx->state == IF_BOOTP_UNRESOLVED ||
459 ifctx->state == IF_DHCP_UNRESOLVED)
465 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
468 if (ifctx->state == IF_BOOTP_FAILED ||
469 ifctx->state == IF_DHCP_FAILED)
475 bootpc_received(struct bootpc_globalcontext *gctx,
476 struct bootpc_ifcontext *ifctx)
478 unsigned char dhcpreplytype;
482 * Need timeout for fallback to less
483 * desirable alternative.
486 /* This call used for the side effect (badopt flag) */
487 (void) bootpc_tag(&gctx->tmptag, &gctx->reply,
491 /* If packet is invalid, ignore it */
492 if (gctx->tmptag.badopt != 0)
495 p = bootpc_tag(&gctx->tmptag, &gctx->reply,
496 gctx->replylen, TAG_DHCP_MSGTYPE);
500 dhcpreplytype = DHCP_NOMSG;
502 switch (ifctx->dhcpquerytype) {
504 if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */
505 #ifndef BOOTP_FORCE_DHCP
506 && dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */
512 if (dhcpreplytype != DHCP_ACK)
518 /* Ignore packet unless it gives us a root tag we didn't have */
520 if ((ifctx->state == IF_BOOTP_RESOLVED ||
521 (ifctx->dhcpquerytype == DHCP_DISCOVER &&
522 (ifctx->state == IF_DHCP_OFFERED ||
523 ifctx->state == IF_DHCP_RESOLVED))) &&
524 (bootpc_tag(&gctx->tmptag, &ifctx->reply,
527 bootpc_tag(&gctx->tmptag, &gctx->reply,
532 bcopy(&gctx->reply, &ifctx->reply, gctx->replylen);
533 ifctx->replylen = gctx->replylen;
535 /* XXX: Only reset if 'perfect' response */
536 if (ifctx->state == IF_BOOTP_UNRESOLVED)
537 ifctx->state = IF_BOOTP_RESOLVED;
538 else if (ifctx->state == IF_DHCP_UNRESOLVED &&
539 ifctx->dhcpquerytype == DHCP_DISCOVER) {
540 if (dhcpreplytype == DHCP_OFFER)
541 ifctx->state = IF_DHCP_OFFERED;
543 ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */
544 } else if (ifctx->state == IF_DHCP_OFFERED &&
545 ifctx->dhcpquerytype == DHCP_REQUEST)
546 ifctx->state = IF_DHCP_RESOLVED;
549 if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
550 ifctx->state != IF_BOOTP_RESOLVED) {
551 p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
552 ifctx->replylen, TAG_DHCP_SERVERID);
553 if (p != NULL && gctx->tmptag.taglen == 4) {
554 memcpy(&ifctx->dhcpserver, p, 4);
555 ifctx->gotdhcpserver = 1;
557 ifctx->gotdhcpserver = 0;
561 ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
564 ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
566 TAG_ROUTERS) != NULL);
567 ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
569 TAG_SUBNETMASK) != NULL);
574 bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
576 struct sockaddr_in *sin, dst;
580 int error, on, rcvflg, timo, len;
584 struct bootpc_ifcontext *ifctx;
592 bzero(&sopt, sizeof(sopt));
593 sopt.sopt_dir = SOPT_SET;
594 sopt.sopt_level = SOL_SOCKET;
595 sopt.sopt_name = SO_RCVTIMEO;
597 sopt.sopt_valsize = sizeof tv;
599 error = sosetopt(bootp_so, &sopt);
607 sopt.sopt_name = SO_BROADCAST;
609 sopt.sopt_valsize = sizeof on;
611 error = sosetopt(bootp_so, &sopt);
620 sopt.sopt_name = SO_DONTROUTE;
622 sopt.sopt_valsize = sizeof on;
624 error = sosetopt(bootp_so, &sopt);
629 * Bind the local endpoint to a bootp client port.
633 sin->sin_port = htons(IPPORT_BOOTPC);
634 error = sobind(bootp_so, (struct sockaddr *)sin, td);
636 printf("bind failed\n");
641 * Setup socket address for the server.
645 sin->sin_addr.s_addr = INADDR_BROADCAST;
646 sin->sin_port = htons(IPPORT_BOOTPS);
649 * Send it, repeatedly, until a reply is received,
650 * but delay each re-send by an increasing amount.
651 * If the delay hits the maximum, start complaining.
660 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
661 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
662 bootpc_tag(&gctx->tmptag, &ifctx->reply,
668 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
669 struct in_aliasreq *ifra = &ifctx->iareq;
670 sin = (struct sockaddr_in *)&ifra->ifra_mask;
672 ifctx->outstanding = 0;
673 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
677 if (bootpc_ifctx_isfailed(ifctx) != 0)
681 ifctx->outstanding = 1;
683 /* Proceed to next step in DHCP negotiation */
684 if ((ifctx->state == IF_DHCP_OFFERED &&
685 ifctx->dhcpquerytype != DHCP_REQUEST) ||
686 (ifctx->state == IF_DHCP_UNRESOLVED &&
687 ifctx->dhcpquerytype != DHCP_DISCOVER) ||
688 (ifctx->state == IF_BOOTP_UNRESOLVED &&
689 ifctx->dhcpquerytype != DHCP_NOMSG)) {
691 bootpc_compose_query(ifctx, td);
694 /* Send BOOTP request (or re-send). */
696 if (ifctx->sentmsg == 0) {
697 switch(ifctx->dhcpquerytype) {
709 printf("Sending %s packet from "
710 "interface %s (%*D)\n",
712 ifctx->ireq.ifr_name,
713 ifctx->sdl->sdl_alen,
714 (unsigned char *) LLADDR(ifctx->sdl),
719 aio.iov_base = (caddr_t) &ifctx->call;
720 aio.iov_len = sizeof(ifctx->call);
724 auio.uio_segflg = UIO_SYSSPACE;
725 auio.uio_rw = UIO_WRITE;
727 auio.uio_resid = sizeof(ifctx->call);
730 /* Set netmask to 0.0.0.0 */
732 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
735 panic("%s: SIOCAIFADDR, error=%d", __func__,
738 error = sosend(bootp_so, (struct sockaddr *) &dst,
739 &auio, NULL, NULL, 0, td);
741 printf("%s: sosend: %d state %08x\n", __func__,
742 error, (int )bootp_so->so_state);
744 /* Set netmask to 255.0.0.0 */
745 sin->sin_addr.s_addr = htonl(IN_CLASSA_NET);
746 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
749 panic("%s: SIOCAIFADDR, error=%d", __func__,
753 if (outstanding == 0 &&
754 (rtimo == 0 || time_second >= rtimo)) {
759 /* Determine new timeout. */
760 if (timo < MAX_RESEND_DELAY)
763 printf("DHCP/BOOTP timeout for server ");
764 print_sin_addr(&dst);
769 * Wait for up to timo seconds for a reply.
770 * The socket receive timeout was set to 1 second.
772 atimo = timo + time_second;
773 while (time_second < atimo) {
774 aio.iov_base = (caddr_t) &gctx->reply;
775 aio.iov_len = sizeof(gctx->reply);
779 auio.uio_segflg = UIO_SYSSPACE;
780 auio.uio_rw = UIO_READ;
782 auio.uio_resid = sizeof(gctx->reply);
786 error = soreceive(bootp_so, NULL, &auio,
787 NULL, NULL, &rcvflg);
788 gctx->secs = time_second - gctx->starttime;
789 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
790 if (bootpc_ifctx_isresolved(ifctx) != 0 ||
791 bootpc_ifctx_isfailed(ifctx) != 0)
794 ifctx->call.secs = htons(gctx->secs);
796 if (error == EWOULDBLOCK)
800 len = sizeof(gctx->reply) - auio.uio_resid;
802 /* Do we have the required number of bytes ? */
803 if (len < BOOTP_MIN_LEN)
805 gctx->replylen = len;
808 if (gctx->reply.op != BOOTP_REPLY)
811 /* Is this an answer to our query */
812 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
813 if (gctx->reply.xid != ifctx->call.xid)
816 /* Same HW address size ? */
817 if (gctx->reply.hlen != ifctx->call.hlen)
820 /* Correct HW address ? */
821 if (bcmp(gctx->reply.chaddr,
823 ifctx->call.hlen) != 0)
830 s = bootpc_tag(&gctx->tmptag,
843 s = "DHCP (unexpected)";
849 printf("Received %s packet"
852 ifctx->ireq.ifr_name);
853 print_in_addr(gctx->reply.siaddr);
854 if (gctx->reply.giaddr.s_addr !=
857 print_in_addr(gctx->reply.giaddr);
859 if (bootpc_received(gctx, ifctx) != 0) {
860 printf(" (accepted)");
861 if (ifctx->outstanding) {
862 ifctx->outstanding = 0;
865 /* Network settle delay */
866 if (outstanding == 0)
867 atimo = time_second +
870 printf(" (ignored)");
871 if (ifctx->gotrootpath ||
872 gctx->any_root_overrides) {
874 rtimo = time_second +
876 if (ifctx->gotrootpath)
877 printf(" (got root path)");
883 if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
886 /* Force a retry if halfway in DHCP negotiation */
888 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
889 if (ifctx->state == IF_DHCP_OFFERED) {
890 if (ifctx->dhcpquerytype == DHCP_DISCOVER)
893 ifctx->state = IF_DHCP_UNRESOLVED;
899 if (gotrootpath != 0) {
900 gctx->gotrootpath = gotrootpath;
901 if (rtimo != 0 && time_second >= rtimo)
904 } /* forever send/receive */
907 * XXX: These are errors of varying seriousness being silently
911 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
912 if (bootpc_ifctx_isresolved(ifctx) == 0) {
913 printf("%s timeout for interface %s\n",
914 ifctx->dhcpquerytype != DHCP_NOMSG ?
916 ifctx->ireq.ifr_name);
919 if (gctx->gotrootpath != 0) {
921 printf("Got a root path, ignoring remaining timeout\n");
926 #ifndef BOOTP_NFSROOT
927 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
928 if (bootpc_ifctx_isresolved(ifctx) != 0) {
940 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
943 struct in_aliasreq *ifra;
944 struct sockaddr_in *sin;
948 ifra = &ifctx->iareq;
951 * Bring up the interface.
953 * Get the old interface flags and or IFF_UP into them; if
954 * IFF_UP set blindly, interface selection can be clobbered.
956 error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
958 panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
959 ifr->ifr_flags |= IFF_UP;
960 error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
962 panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
965 * Do enough of ifconfig(8) so that the chosen interface
966 * can talk to the servers. Set address to 0.0.0.0/8 and
967 * broadcast address to local broadcast.
969 sin = (struct sockaddr_in *)&ifra->ifra_addr;
971 sin = (struct sockaddr_in *)&ifra->ifra_mask;
973 sin->sin_addr.s_addr = htonl(IN_CLASSA_NET);
974 sin = (struct sockaddr_in *)&ifra->ifra_broadaddr;
976 sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
977 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
979 panic("%s: SIOCAIFADDR, error=%d", __func__, error);
983 bootpc_shutdown_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
986 struct sockaddr_in *sin;
991 printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
992 error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
994 panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
995 ifr->ifr_flags &= ~IFF_UP;
996 error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
998 panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
1000 sin = (struct sockaddr_in *) &ifr->ifr_addr;
1002 error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
1004 panic("%s: SIOCDIFADDR, error=%d", __func__, error);
1008 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
1009 struct bootpc_globalcontext *gctx, struct thread *td)
1012 struct sockaddr_in defdst;
1013 struct sockaddr_in defmask;
1014 struct sockaddr_in *sin;
1016 struct in_aliasreq *ifra;
1017 struct sockaddr_in *myaddr;
1018 struct sockaddr_in *netmask;
1019 struct sockaddr_in *gw;
1022 ifra = &ifctx->iareq;
1023 myaddr = &ifctx->myaddr;
1024 netmask = &ifctx->netmask;
1027 if (bootpc_ifctx_isresolved(ifctx) == 0) {
1028 /* Shutdown interfaces where BOOTP failed */
1029 bootpc_shutdown_interface(ifctx, td);
1033 printf("Adjusted interface %s\n", ifctx->ireq.ifr_name);
1035 * Do enough of ifconfig(8) so that the chosen interface
1036 * can talk to the servers. (just set the address)
1038 sin = (struct sockaddr_in *) &ifr->ifr_addr;
1040 error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
1042 panic("%s: SIOCDIFADDR, error=%d", __func__, error);
1044 bcopy(myaddr, &ifra->ifra_addr, sizeof(*myaddr));
1045 bcopy(netmask, &ifra->ifra_mask, sizeof(*netmask));
1046 clear_sinaddr(&ifra->ifra_broadaddr);
1047 ifra->ifra_broadaddr.sin_addr.s_addr = myaddr->sin_addr.s_addr |
1048 ~netmask->sin_addr.s_addr;
1050 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
1052 panic("%s: SIOCAIFADDR, error=%d", __func__, error);
1054 /* Add new default route */
1056 if (ifctx->gotgw != 0 || gctx->gotgw == 0) {
1057 clear_sinaddr(&defdst);
1058 clear_sinaddr(&defmask);
1059 /* XXX MRT just table 0 */
1060 error = rtrequest_fib(RTM_ADD,
1061 (struct sockaddr *) &defdst, (struct sockaddr *) gw,
1062 (struct sockaddr *) &defmask,
1063 (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL, RT_DEFAULT_FIB);
1065 printf("%s: RTM_ADD, error=%d\n", __func__, error);
1074 setfs(struct sockaddr_in *addr, char *path, char *p,
1075 const struct in_addr *siaddr)
1078 if (getip(&p, &addr->sin_addr) == 0) {
1079 if (siaddr != NULL && *p == '/')
1080 bcopy(siaddr, &addr->sin_addr, sizeof(struct in_addr));
1089 addr->sin_len = sizeof(struct sockaddr_in);
1090 addr->sin_family = AF_INET;
1092 strlcpy(path, p, MNAMELEN);
1097 getip(char **ptr, struct in_addr *addr)
1105 if (((val = getdec(&p)) < 0) || (val > 255))
1111 if (((val = getdec(&p)) < 0) || (val > 255))
1117 if (((val = getdec(&p)) < 0) || (val > 255))
1123 if (((val = getdec(&p)) < 0) || (val > 255))
1127 addr->s_addr = htonl(ip);
1140 if ((*p < '0') || (*p > '9'))
1142 while ((*p >= '0') && (*p <= '9')) {
1143 ret = ret * 10 + (*p - '0');
1151 mountopts(struct nfs_args *args, char *p)
1153 args->version = NFS_ARGSVERSION;
1154 args->rsize = BOOTP_BLOCKSIZE;
1155 args->wsize = BOOTP_BLOCKSIZE;
1156 args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
1157 args->sotype = SOCK_DGRAM;
1159 nfs_parse_options(p, args);
1163 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
1169 alignedlen = ( len + 3 ) & ~3;
1171 if (m->m_len < alignedlen) {
1172 m = m_pullup(m, alignedlen);
1178 bcopy(mtod(m, u_char *), buf, len);
1179 m_adj(m, alignedlen);
1185 xdr_int_decode(struct mbuf **mptr, int *iptr)
1189 if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
1191 *iptr = fxdr_unsigned(u_int32_t, i);
1196 print_sin_addr(struct sockaddr_in *sin)
1199 print_in_addr(sin->sin_addr);
1203 print_in_addr(struct in_addr addr)
1207 ip = ntohl(addr.s_addr);
1208 printf("%d.%d.%d.%d",
1209 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1213 bootpc_compose_query(struct bootpc_ifcontext *ifctx, struct thread *td)
1215 unsigned char *vendp;
1216 unsigned char vendor_client[64];
1218 uint8_t vendor_client_len;
1220 ifctx->gotrootpath = 0;
1222 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1225 ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */
1226 ifctx->call.htype = 1; /* 10mb ethernet */
1227 ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1228 ifctx->call.hops = 0;
1229 if (bootpc_ifctx_isunresolved(ifctx) != 0)
1231 ifctx->call.xid = txdr_unsigned(ifctx->xid);
1232 bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1234 vendp = ifctx->call.vend;
1235 *vendp++ = 99; /* RFC1048 cookie */
1239 *vendp++ = TAG_MAXMSGSIZE;
1241 *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1242 *vendp++ = sizeof(struct bootp_packet) & 255;
1244 snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1245 ostype, MACHINE, osrelease);
1246 vendor_client_len = strlen(vendor_client);
1247 *vendp++ = TAG_VENDOR_INDENTIFIER;
1248 *vendp++ = vendor_client_len;
1249 memcpy(vendp, vendor_client, vendor_client_len);
1250 vendp += vendor_client_len;
1251 ifctx->dhcpquerytype = DHCP_NOMSG;
1252 switch (ifctx->state) {
1253 case IF_DHCP_UNRESOLVED:
1254 *vendp++ = TAG_DHCP_MSGTYPE;
1256 *vendp++ = DHCP_DISCOVER;
1257 ifctx->dhcpquerytype = DHCP_DISCOVER;
1258 ifctx->gotdhcpserver = 0;
1260 case IF_DHCP_OFFERED:
1261 *vendp++ = TAG_DHCP_MSGTYPE;
1263 *vendp++ = DHCP_REQUEST;
1264 ifctx->dhcpquerytype = DHCP_REQUEST;
1265 *vendp++ = TAG_DHCP_REQ_ADDR;
1267 memcpy(vendp, &ifctx->reply.yiaddr, 4);
1269 if (ifctx->gotdhcpserver != 0) {
1270 *vendp++ = TAG_DHCP_SERVERID;
1272 memcpy(vendp, &ifctx->dhcpserver, 4);
1275 *vendp++ = TAG_DHCP_LEASETIME;
1277 leasetime = htonl(300);
1278 memcpy(vendp, &leasetime, 4);
1286 ifctx->call.secs = 0;
1287 ifctx->call.flags = htons(0x8000); /* We need a broadcast answer */
1291 bootpc_hascookie(struct bootp_packet *bp)
1294 return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1295 bp->vend[2] == 83 && bp->vend[3] == 99);
1299 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1300 unsigned char *start, int len, int tag)
1306 if (tctx->badtag != 0 || tctx->badopt != 0)
1314 if (code == TAG_PAD)
1316 if (code == TAG_END)
1318 if (j >= ej || j + *j + 1 > ej) {
1324 if (tctx->taglen + len > TAG_MAXLEN) {
1330 memcpy(tctx->buf + tctx->taglen,
1332 tctx->taglen += len;
1334 if (code == TAG_OVERLOAD)
1335 tctx->overload = *j;
1341 static unsigned char *
1342 bootpc_tag(struct bootpc_tagcontext *tctx,
1343 struct bootp_packet *bp, int len, int tag)
1351 if (bootpc_hascookie(bp) == 0)
1354 bootpc_tag_helper(tctx, &bp->vend[4],
1355 (unsigned char *) bp + len - &bp->vend[4], tag);
1357 if ((tctx->overload & OVERLOAD_FILE) != 0)
1358 bootpc_tag_helper(tctx,
1359 (unsigned char *) bp->file,
1362 if ((tctx->overload & OVERLOAD_SNAME) != 0)
1363 bootpc_tag_helper(tctx,
1364 (unsigned char *) bp->sname,
1368 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1370 tctx->buf[tctx->taglen] = '\0';
1375 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1376 struct bootpc_globalcontext *gctx)
1382 ifctx->gotnetmask = 0;
1384 clear_sinaddr(&ifctx->myaddr);
1385 clear_sinaddr(&ifctx->netmask);
1386 clear_sinaddr(&ifctx->gw);
1388 ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1390 ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1392 printf("%s at ", ifctx->ireq.ifr_name);
1393 print_sin_addr(&ifctx->myaddr);
1395 print_in_addr(ifctx->reply.siaddr);
1397 ifctx->gw.sin_addr = ifctx->reply.giaddr;
1398 if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1399 printf(" via gateway ");
1400 print_in_addr(ifctx->reply.giaddr);
1403 /* This call used for the side effect (overload flag) */
1404 (void) bootpc_tag(&gctx->tmptag,
1405 &ifctx->reply, ifctx->replylen, TAG_END);
1407 if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1408 if (ifctx->reply.sname[0] != '\0')
1409 printf(" server name %s", ifctx->reply.sname);
1410 if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1411 if (ifctx->reply.file[0] != '\0')
1412 printf(" boot file %s", ifctx->reply.file);
1416 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1419 if (gctx->tag.taglen != 4)
1420 panic("bootpc: subnet mask len is %d",
1422 bcopy(p, &ifctx->netmask.sin_addr, 4);
1423 ifctx->gotnetmask = 1;
1424 printf("subnet mask ");
1425 print_sin_addr(&ifctx->netmask);
1429 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1433 if (gctx->tag.taglen % 4)
1434 panic("bootpc: Router Len is %d", gctx->tag.taglen);
1435 if (gctx->tag.taglen > 0) {
1436 bcopy(p, &ifctx->gw.sin_addr, 4);
1438 print_sin_addr(&ifctx->gw);
1446 * Choose a root filesystem. If a value is forced in the environment
1447 * and it contains "nfs:", use it unconditionally. Otherwise, if the
1448 * kernel is compiled with the ROOTDEVNAME option, then use it if:
1449 * - The server doesn't provide a pathname.
1450 * - The boothowto flags include RB_DFLTROOT (user said to override
1451 * the server value).
1454 if ((s = getenv("vfs.root.mountfrom")) != NULL) {
1455 if ((p = strstr(s, "nfs:")) != NULL)
1456 p = strdup(p + 4, M_TEMP);
1460 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1464 if ((p == NULL || (boothowto & RB_DFLTROOT) != 0) &&
1465 (p = strstr(ROOTDEVNAME, "nfs:")) != NULL) {
1470 if (gctx->setrootfs != NULL) {
1471 printf("rootfs %s (ignored) ", p);
1472 } else if (setfs(&nd->root_saddr,
1473 nd->root_hostnam, p, &ifctx->reply.siaddr)) {
1475 printf("root_server ");
1476 print_sin_addr(&nd->root_saddr);
1479 printf("rootfs %s ", p);
1480 gctx->gotrootpath = 1;
1481 ifctx->gotrootpath = 1;
1482 gctx->setrootfs = ifctx;
1484 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1488 mountopts(&nd->root_args, p);
1489 printf("rootopts %s ", p);
1492 panic("Failed to set rootfs to %s", p);
1495 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1498 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1499 panic("bootpc: hostname >= %d bytes",
1501 if (gctx->sethostname != NULL) {
1502 printf("hostname %s (ignored) ", p);
1504 strcpy(nd->my_hostnam, p);
1505 mtx_lock(&prison0.pr_mtx);
1506 strcpy(prison0.pr_hostname, p);
1507 mtx_unlock(&prison0.pr_mtx);
1508 printf("hostname %s ", p);
1509 gctx->sethostname = ifctx;
1512 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1514 if (p != NULL) { /* store in a sysctl variable */
1515 int i, l = sizeof(bootp_cookie) - 1;
1516 for (i = 0; i < l && p[i] != '\0'; i++)
1517 bootp_cookie[i] = p[i];
1524 if (ifctx->gotnetmask == 0) {
1525 if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1526 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1527 else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1528 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1530 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1532 if (ifctx->gotgw == 0) {
1534 ifctx->gw.sin_addr.s_addr = ifctx->myaddr.sin_addr.s_addr;
1541 struct bootpc_ifcontext *ifctx; /* Interface BOOTP contexts */
1542 struct bootpc_globalcontext *gctx; /* Global BOOTP context */
1544 struct sockaddr_dl *sdl;
1547 #ifndef BOOTP_WIRED_TO
1550 struct nfsv3_diskless *nd;
1555 timeout = BOOTP_IFACE_WAIT_TIMEOUT * hz;
1558 nd = &nfsv3_diskless;
1562 * If already filled in, don't touch it here
1564 if (nfs_diskless_valid != 0)
1567 gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK | M_ZERO);
1568 STAILQ_INIT(&gctx->interfaces);
1569 gctx->xid = ~0xFFFF;
1570 gctx->starttime = time_second;
1573 * If ROOTDEVNAME is defined or vfs.root.mountfrom is set then we have
1574 * root-path overrides that can potentially let us boot even if we don't
1575 * get a root path from the server, so we can treat that as a non-error.
1578 gctx->any_root_overrides = 1;
1580 gctx->any_root_overrides = testenv("vfs.root.mountfrom");
1584 * Find a network interface.
1586 CURVNET_SET(TD_TO_VNET(td));
1587 #ifdef BOOTP_WIRED_TO
1588 printf("%s: wired to interface '%s'\n", __func__,
1589 __XSTRING(BOOTP_WIRED_TO));
1593 * Preallocate interface context storage, if another interface
1594 * attaches and wins the race, it won't be eligible for bootp.
1598 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1599 if ((ifp->if_flags &
1600 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1603 switch (ifp->if_alloctype) {
1615 panic("%s: no eligible interfaces", __func__);
1616 for (; ifcnt > 0; ifcnt--)
1621 ifctx = STAILQ_FIRST(&gctx->interfaces);
1623 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1626 #ifdef BOOTP_WIRED_TO
1627 if (strcmp(ifp->if_xname, __XSTRING(BOOTP_WIRED_TO)) != 0)
1630 if ((ifp->if_flags &
1631 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1634 switch (ifp->if_alloctype) {
1643 strlcpy(ifctx->ireq.ifr_name, ifp->if_xname,
1644 sizeof(ifctx->ireq.ifr_name));
1647 /* Get HW address */
1649 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1650 if (ifa->ifa_addr->sa_family == AF_LINK) {
1651 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1652 if (sdl->sdl_type == IFT_ETHER)
1656 panic("bootpc: Unable to find HW address for %s",
1657 ifctx->ireq.ifr_name);
1660 ifctx = STAILQ_NEXT(ifctx, next);
1665 if (STAILQ_EMPTY(&gctx->interfaces) ||
1666 STAILQ_FIRST(&gctx->interfaces)->ifp == NULL) {
1668 pause("bootpc", delay);
1672 #ifdef BOOTP_WIRED_TO
1673 panic("%s: Could not find interface specified "
1674 "by BOOTP_WIRED_TO: "
1675 __XSTRING(BOOTP_WIRED_TO), __func__);
1677 panic("%s: no suitable interface", __func__);
1681 error = socreate(AF_INET, &bootp_so, SOCK_DGRAM, 0, td->td_ucred, td);
1683 panic("%s: socreate, error=%d", __func__, error);
1685 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1686 bootpc_fakeup_interface(ifctx, td);
1688 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1689 bootpc_compose_query(ifctx, td);
1691 error = bootpc_call(gctx, td);
1693 printf("BOOTP call failed\n");
1696 mountopts(&nd->root_args, NULL);
1698 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1699 if (bootpc_ifctx_isresolved(ifctx) != 0)
1700 bootpc_decode_reply(nd, ifctx, gctx);
1702 #ifdef BOOTP_NFSROOT
1703 if (gctx->gotrootpath == 0 && gctx->any_root_overrides == 0)
1704 panic("bootpc: No root path offered");
1707 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1708 bootpc_adjust_interface(ifctx, gctx, td);
1712 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1713 if (ifctx->gotrootpath != 0)
1715 if (ifctx == NULL) {
1716 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1717 if (bootpc_ifctx_isresolved(ifctx) != 0)
1723 if (gctx->gotrootpath != 0) {
1725 setenv("boot.netif.name", ifctx->ifp->if_xname);
1727 error = md_mount(&nd->root_saddr, nd->root_hostnam,
1728 nd->root_fh, &nd->root_fhsize,
1729 &nd->root_args, td);
1731 if (gctx->any_root_overrides == 0)
1732 panic("nfs_boot: mount root, error=%d", error);
1736 rootdevnames[0] = "nfs:";
1738 rootdevnames[1] = "oldnfs:";
1740 nfs_diskless_valid = 3;
1743 strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1744 bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1745 bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1746 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1747 ifctx->myaddr.sin_addr.s_addr |
1748 ~ ifctx->netmask.sin_addr.s_addr;
1749 bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1752 while((ifctx = STAILQ_FIRST(&gctx->interfaces)) != NULL) {
1753 STAILQ_REMOVE_HEAD(&gctx->interfaces, next);
1754 free(ifctx, M_TEMP);
1761 * Given a server pathname, get an NFS file handle.
1762 * Also, sets sin->sin_port to the NFS service port.
1765 md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp, int *fhsizep,
1766 struct nfs_args *args, struct thread *td)
1774 #define RPCPROG_MNT 100005
1775 #define RPCMNT_VER1 1
1776 #define RPCMNT_VER3 3
1777 #define RPCMNT_MOUNT 1
1778 #define AUTH_SYS 1 /* unix style (uid, gids) */
1779 #define AUTH_UNIX AUTH_SYS
1781 /* XXX honor v2/v3 flags in args->flags? */
1783 /* First try NFS v3 */
1784 /* Get port number for MOUNTD. */
1785 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1786 &mdsin->sin_port, td);
1788 m = xdr_string_encode(path, strlen(path));
1790 /* Do RPC to mountd. */
1791 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1792 RPCMNT_MOUNT, &m, NULL, td);
1795 args->flags |= NFSMNT_NFSV3;
1798 /* Fallback to NFS v2 */
1800 /* Get port number for MOUNTD. */
1801 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1802 &mdsin->sin_port, td);
1806 m = xdr_string_encode(path, strlen(path));
1808 /* Do RPC to mountd. */
1809 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1810 RPCMNT_MOUNT, &m, NULL, td);
1812 return error; /* message already freed */
1818 if (xdr_int_decode(&m, &error) != 0 || error != 0)
1821 if ((args->flags & NFSMNT_NFSV3) != 0) {
1822 if (xdr_int_decode(&m, fhsizep) != 0 ||
1823 *fhsizep > NFSX_V3FHMAX ||
1827 *fhsizep = NFSX_V2FH;
1829 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
1832 if (args->flags & NFSMNT_NFSV3) {
1833 if (xdr_int_decode(&m, &authcount) != 0)
1836 if (authcount < 0 || authcount > 100)
1838 while (authcount > 0) {
1839 if (xdr_int_decode(&m, &authver) != 0)
1841 if (authver == AUTH_UNIX)
1845 if (authunixok == 0)
1849 /* Set port number for NFS use. */
1850 error = krpc_portmap(mdsin, NFS_PROG,
1852 NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
1853 &mdsin->sin_port, td);
1865 SYSINIT(bootp_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, bootpc_init, NULL);