2 * Copyright (c) 1995 Gordon Ross, Adam Glass
3 * Copyright (c) 1992 Regents of the University of California.
6 * This software was developed by the Computer Systems Engineering group
7 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
8 * contributed to Berkeley.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Lawrence Berkeley Laboratory and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * $NetBSD: krpc_subr.c,v 1.10 1995/08/08 20:43:43 gwr Exp $
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include "opt_bootp.h"
48 #include "opt_rootdevname.h"
50 #include <sys/param.h>
51 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/sockio.h>
55 #include <sys/malloc.h>
56 #include <sys/mount.h>
59 #include <sys/reboot.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/sysctl.h>
66 #include <net/if_var.h>
67 #include <net/route.h>
69 #include <netinet/in.h>
70 #include <netinet/in_var.h>
71 #include <net/if_types.h>
72 #include <net/if_dl.h>
75 #include <nfs/nfsproto.h>
76 #include <nfsclient/nfs.h>
77 #include <nfs/nfsdiskless.h>
79 #include <nfs/xdr_subs.h>
82 #define BOOTP_MIN_LEN 300 /* Minimum size of bootp udp packet */
84 #ifndef BOOTP_SETTLE_DELAY
85 #define BOOTP_SETTLE_DELAY 3
89 * Wait 10 seconds for interface appearance
90 * USB ethernet adapters might require some time to pop up
92 #ifndef BOOTP_IFACE_WAIT_TIMEOUT
93 #define BOOTP_IFACE_WAIT_TIMEOUT 10
97 * What is the longest we will wait before re-sending a request?
98 * Note this is also the frequency of "RPC timeout" messages.
99 * The re-send loop count sup linearly to this maximum, so the
100 * first complaint will happen after (1+2+3+4+5)=15 seconds.
102 #define MAX_RESEND_DELAY 5 /* seconds */
104 /* Definitions from RFC951 */
105 struct bootp_packet {
113 struct in_addr ciaddr;
114 struct in_addr yiaddr;
115 struct in_addr siaddr;
116 struct in_addr giaddr;
117 unsigned char chaddr[16];
120 unsigned char vend[1222];
123 struct bootpc_ifcontext {
124 STAILQ_ENTRY(bootpc_ifcontext) next;
125 struct bootp_packet call;
126 struct bootp_packet reply;
131 struct in_aliasreq _in_alias_req;
133 #define ireq _req._ifreq
134 #define iareq _req._in_alias_req
136 struct sockaddr_dl *sdl;
137 struct sockaddr_in myaddr;
138 struct sockaddr_in netmask;
139 struct sockaddr_in gw;
155 int dhcpquerytype; /* dhcp type sent */
156 struct in_addr dhcpserver;
160 #define TAG_MAXLEN 1024
161 struct bootpc_tagcontext {
162 char buf[TAG_MAXLEN + 1];
170 struct bootpc_globalcontext {
171 STAILQ_HEAD(, bootpc_ifcontext) interfaces;
173 int any_root_overrides;
179 struct bootp_packet reply;
181 struct bootpc_ifcontext *setrootfs;
182 struct bootpc_ifcontext *sethostname;
183 struct bootpc_tagcontext tmptag;
184 struct bootpc_tagcontext tag;
187 #define IPPORT_BOOTPC 68
188 #define IPPORT_BOOTPS 67
190 #define BOOTP_REQUEST 1
191 #define BOOTP_REPLY 2
194 #define TAG_PAD 0 /* Pad option, implicit length 1 */
195 #define TAG_SUBNETMASK 1 /* RFC 950 subnet mask */
196 #define TAG_ROUTERS 3 /* Routers (in order of preference) */
197 #define TAG_HOSTNAME 12 /* Client host name */
198 #define TAG_ROOT 17 /* Root path */
200 /* DHCP specific tags */
201 #define TAG_OVERLOAD 52 /* Option Overload */
202 #define TAG_MAXMSGSIZE 57 /* Maximum DHCP Message Size */
204 #define TAG_END 255 /* End Option (i.e. no more options) */
206 /* Overload values */
207 #define OVERLOAD_FILE 1
208 #define OVERLOAD_SNAME 2
210 /* Site specific tags: */
211 #define TAG_ROOTOPTS 130
212 #define TAG_COOKIE 134 /* ascii info for userland, via sysctl */
214 #define TAG_DHCP_MSGTYPE 53
215 #define TAG_DHCP_REQ_ADDR 50
216 #define TAG_DHCP_SERVERID 54
217 #define TAG_DHCP_LEASETIME 51
219 #define TAG_VENDOR_INDENTIFIER 60
222 #define DHCP_DISCOVER 1
224 #define DHCP_REQUEST 3
227 /* NFS read/write block size */
228 #ifndef BOOTP_BLOCKSIZE
229 #define BOOTP_BLOCKSIZE 8192
232 static char bootp_cookie[128];
233 static struct socket *bootp_so;
234 SYSCTL_STRING(_kern, OID_AUTO, bootp_cookie, CTLFLAG_RD,
235 bootp_cookie, 0, "Cookie (T134) supplied by bootp server");
238 static int md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp,
239 int *fhsizep, struct nfs_args *args, struct thread *td);
240 static int setfs(struct sockaddr_in *addr, char *path, char *p,
241 const struct in_addr *siaddr);
242 static int getdec(char **ptr);
243 static int getip(char **ptr, struct in_addr *ip);
244 static void mountopts(struct nfs_args *args, char *p);
245 static int xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len);
246 static int xdr_int_decode(struct mbuf **ptr, int *iptr);
247 static void print_in_addr(struct in_addr addr);
248 static void print_sin_addr(struct sockaddr_in *addr);
249 static void clear_sinaddr(struct sockaddr_in *sin);
250 static void allocifctx(struct bootpc_globalcontext *gctx);
251 static void bootpc_compose_query(struct bootpc_ifcontext *ifctx,
253 static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx,
254 struct bootp_packet *bp, int len, int tag);
255 static void bootpc_tag_helper(struct bootpc_tagcontext *tctx,
256 unsigned char *start, int len, int tag);
259 void bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma);
260 void bootpboot_p_rtentry(struct rtentry *rt);
261 void bootpboot_p_tree(struct radix_node *rn);
262 void bootpboot_p_rtlist(void);
263 void bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa);
264 void bootpboot_p_iflist(void);
267 static int bootpc_call(struct bootpc_globalcontext *gctx,
270 static void bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
273 static int bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
274 struct bootpc_globalcontext *gctx, struct thread *td);
276 static void bootpc_decode_reply(struct nfsv3_diskless *nd,
277 struct bootpc_ifcontext *ifctx,
278 struct bootpc_globalcontext *gctx);
280 static int bootpc_received(struct bootpc_globalcontext *gctx,
281 struct bootpc_ifcontext *ifctx);
283 static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
284 static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
285 static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
288 * In order to have multiple active interfaces with address 0.0.0.0
289 * and be able to send data to a selected interface, we first set
290 * mask to /8 on all interfaces, and temporarily set it to /0 when
296 bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma)
300 printf("(sockaddr *) <null>");
303 switch (sa->sa_family) {
306 struct sockaddr_in *sin;
308 sin = (struct sockaddr_in *) sa;
312 sin = (struct sockaddr_in *) ma;
320 struct sockaddr_dl *sli;
323 sli = (struct sockaddr_dl *) sa;
324 printf("link %.*s ", sli->sdl_nlen, sli->sdl_data);
325 for (i = 0; i < sli->sdl_alen; i++) {
328 printf("%x", ((unsigned char *) LLADDR(sli))[i]);
333 printf("af%d", sa->sa_family);
338 bootpboot_p_rtentry(struct rtentry *rt)
341 bootpboot_p_sa(rt_key(rt), rt_mask(rt));
343 bootpboot_p_sa(rt->rt_gateway, NULL);
345 printf("flags %x", (unsigned short) rt->rt_flags);
346 printf(" %d", (int) rt->rt_rmx.rmx_expire);
347 printf(" %s\n", rt->rt_ifp->if_xname);
351 bootpboot_p_tree(struct radix_node *rn)
355 if (rn->rn_bit < 0) {
356 if ((rn->rn_flags & RNF_ROOT) != 0) {
358 bootpboot_p_rtentry((struct rtentry *) rn);
360 rn = rn->rn_dupedkey;
362 bootpboot_p_tree(rn->rn_left);
363 bootpboot_p_tree(rn->rn_right);
370 bootpboot_p_rtlist(void)
372 struct radix_node_head *rnh;
374 printf("Routing table:\n");
375 rnh = rt_tables_get_rnh(0, AF_INET);
378 RADIX_NODE_HEAD_RLOCK(rnh); /* could sleep XXX */
379 bootpboot_p_tree(rnh->rnh_treetop);
380 RADIX_NODE_HEAD_RUNLOCK(rnh);
384 bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
387 printf("%s flags %x, addr ",
388 ifp->if_xname, ifp->if_flags);
389 print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
390 printf(", broadcast ");
391 print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
392 printf(", netmask ");
393 print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
398 bootpboot_p_iflist(void)
403 printf("Interface list:\n");
405 for (ifp = TAILQ_FIRST(&V_ifnet);
407 ifp = TAILQ_NEXT(ifp, if_link)) {
408 for (ifa = TAILQ_FIRST(&ifp->if_addrhead);
410 ifa = TAILQ_NEXT(ifa, ifa_link))
411 if (ifa->ifa_addr->sa_family == AF_INET)
412 bootpboot_p_if(ifp, ifa);
416 #endif /* defined(BOOTP_DEBUG) */
419 clear_sinaddr(struct sockaddr_in *sin)
422 bzero(sin, sizeof(*sin));
423 sin->sin_len = sizeof(*sin);
424 sin->sin_family = AF_INET;
425 sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
430 allocifctx(struct bootpc_globalcontext *gctx)
432 struct bootpc_ifcontext *ifctx;
434 ifctx = malloc(sizeof(*ifctx), M_TEMP, M_WAITOK | M_ZERO);
435 ifctx->xid = gctx->xid;
437 ifctx->state = IF_BOOTP_UNRESOLVED;
439 ifctx->state = IF_DHCP_UNRESOLVED;
442 STAILQ_INSERT_TAIL(&gctx->interfaces, ifctx, next);
446 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
449 if (ifctx->state == IF_BOOTP_RESOLVED ||
450 ifctx->state == IF_DHCP_RESOLVED)
456 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
459 if (ifctx->state == IF_BOOTP_UNRESOLVED ||
460 ifctx->state == IF_DHCP_UNRESOLVED)
466 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
469 if (ifctx->state == IF_BOOTP_FAILED ||
470 ifctx->state == IF_DHCP_FAILED)
476 bootpc_received(struct bootpc_globalcontext *gctx,
477 struct bootpc_ifcontext *ifctx)
479 unsigned char dhcpreplytype;
483 * Need timeout for fallback to less
484 * desirable alternative.
487 /* This call used for the side effect (badopt flag) */
488 (void) bootpc_tag(&gctx->tmptag, &gctx->reply,
492 /* If packet is invalid, ignore it */
493 if (gctx->tmptag.badopt != 0)
496 p = bootpc_tag(&gctx->tmptag, &gctx->reply,
497 gctx->replylen, TAG_DHCP_MSGTYPE);
501 dhcpreplytype = DHCP_NOMSG;
503 switch (ifctx->dhcpquerytype) {
505 if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */
506 #ifndef BOOTP_FORCE_DHCP
507 && dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */
513 if (dhcpreplytype != DHCP_ACK)
519 /* Ignore packet unless it gives us a root tag we didn't have */
521 if ((ifctx->state == IF_BOOTP_RESOLVED ||
522 (ifctx->dhcpquerytype == DHCP_DISCOVER &&
523 (ifctx->state == IF_DHCP_OFFERED ||
524 ifctx->state == IF_DHCP_RESOLVED))) &&
525 (bootpc_tag(&gctx->tmptag, &ifctx->reply,
528 bootpc_tag(&gctx->tmptag, &gctx->reply,
533 bcopy(&gctx->reply, &ifctx->reply, gctx->replylen);
534 ifctx->replylen = gctx->replylen;
536 /* XXX: Only reset if 'perfect' response */
537 if (ifctx->state == IF_BOOTP_UNRESOLVED)
538 ifctx->state = IF_BOOTP_RESOLVED;
539 else if (ifctx->state == IF_DHCP_UNRESOLVED &&
540 ifctx->dhcpquerytype == DHCP_DISCOVER) {
541 if (dhcpreplytype == DHCP_OFFER)
542 ifctx->state = IF_DHCP_OFFERED;
544 ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */
545 } else if (ifctx->state == IF_DHCP_OFFERED &&
546 ifctx->dhcpquerytype == DHCP_REQUEST)
547 ifctx->state = IF_DHCP_RESOLVED;
550 if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
551 ifctx->state != IF_BOOTP_RESOLVED) {
552 p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
553 ifctx->replylen, TAG_DHCP_SERVERID);
554 if (p != NULL && gctx->tmptag.taglen == 4) {
555 memcpy(&ifctx->dhcpserver, p, 4);
556 ifctx->gotdhcpserver = 1;
558 ifctx->gotdhcpserver = 0;
562 ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
565 ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
567 TAG_ROUTERS) != NULL);
568 ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
570 TAG_SUBNETMASK) != NULL);
575 bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
577 struct sockaddr_in *sin, dst;
581 int error, on, rcvflg, timo, len;
585 struct bootpc_ifcontext *ifctx;
593 bzero(&sopt, sizeof(sopt));
594 sopt.sopt_dir = SOPT_SET;
595 sopt.sopt_level = SOL_SOCKET;
596 sopt.sopt_name = SO_RCVTIMEO;
598 sopt.sopt_valsize = sizeof tv;
600 error = sosetopt(bootp_so, &sopt);
608 sopt.sopt_name = SO_BROADCAST;
610 sopt.sopt_valsize = sizeof on;
612 error = sosetopt(bootp_so, &sopt);
621 sopt.sopt_name = SO_DONTROUTE;
623 sopt.sopt_valsize = sizeof on;
625 error = sosetopt(bootp_so, &sopt);
630 * Bind the local endpoint to a bootp client port.
634 sin->sin_port = htons(IPPORT_BOOTPC);
635 error = sobind(bootp_so, (struct sockaddr *)sin, td);
637 printf("bind failed\n");
642 * Setup socket address for the server.
646 sin->sin_addr.s_addr = INADDR_BROADCAST;
647 sin->sin_port = htons(IPPORT_BOOTPS);
650 * Send it, repeatedly, until a reply is received,
651 * but delay each re-send by an increasing amount.
652 * If the delay hits the maximum, start complaining.
661 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
662 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
663 bootpc_tag(&gctx->tmptag, &ifctx->reply,
669 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
670 struct in_aliasreq *ifra = &ifctx->iareq;
671 sin = (struct sockaddr_in *)&ifra->ifra_mask;
673 ifctx->outstanding = 0;
674 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
678 if (bootpc_ifctx_isfailed(ifctx) != 0)
682 ifctx->outstanding = 1;
684 /* Proceed to next step in DHCP negotiation */
685 if ((ifctx->state == IF_DHCP_OFFERED &&
686 ifctx->dhcpquerytype != DHCP_REQUEST) ||
687 (ifctx->state == IF_DHCP_UNRESOLVED &&
688 ifctx->dhcpquerytype != DHCP_DISCOVER) ||
689 (ifctx->state == IF_BOOTP_UNRESOLVED &&
690 ifctx->dhcpquerytype != DHCP_NOMSG)) {
692 bootpc_compose_query(ifctx, td);
695 /* Send BOOTP request (or re-send). */
697 if (ifctx->sentmsg == 0) {
698 switch(ifctx->dhcpquerytype) {
710 printf("Sending %s packet from "
711 "interface %s (%*D)\n",
713 ifctx->ireq.ifr_name,
714 ifctx->sdl->sdl_alen,
715 (unsigned char *) LLADDR(ifctx->sdl),
720 aio.iov_base = (caddr_t) &ifctx->call;
721 aio.iov_len = sizeof(ifctx->call);
725 auio.uio_segflg = UIO_SYSSPACE;
726 auio.uio_rw = UIO_WRITE;
728 auio.uio_resid = sizeof(ifctx->call);
731 /* Set netmask to 0.0.0.0 */
733 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
736 panic("%s: SIOCAIFADDR, error=%d", __func__,
739 error = sosend(bootp_so, (struct sockaddr *) &dst,
740 &auio, NULL, NULL, 0, td);
742 printf("%s: sosend: %d state %08x\n", __func__,
743 error, (int )bootp_so->so_state);
745 /* Set netmask to 255.0.0.0 */
746 sin->sin_addr.s_addr = htonl(IN_CLASSA_NET);
747 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
750 panic("%s: SIOCAIFADDR, error=%d", __func__,
754 if (outstanding == 0 &&
755 (rtimo == 0 || time_second >= rtimo)) {
760 /* Determine new timeout. */
761 if (timo < MAX_RESEND_DELAY)
764 printf("DHCP/BOOTP timeout for server ");
765 print_sin_addr(&dst);
770 * Wait for up to timo seconds for a reply.
771 * The socket receive timeout was set to 1 second.
773 atimo = timo + time_second;
774 while (time_second < atimo) {
775 aio.iov_base = (caddr_t) &gctx->reply;
776 aio.iov_len = sizeof(gctx->reply);
780 auio.uio_segflg = UIO_SYSSPACE;
781 auio.uio_rw = UIO_READ;
783 auio.uio_resid = sizeof(gctx->reply);
787 error = soreceive(bootp_so, NULL, &auio,
788 NULL, NULL, &rcvflg);
789 gctx->secs = time_second - gctx->starttime;
790 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
791 if (bootpc_ifctx_isresolved(ifctx) != 0 ||
792 bootpc_ifctx_isfailed(ifctx) != 0)
795 ifctx->call.secs = htons(gctx->secs);
797 if (error == EWOULDBLOCK)
801 len = sizeof(gctx->reply) - auio.uio_resid;
803 /* Do we have the required number of bytes ? */
804 if (len < BOOTP_MIN_LEN)
806 gctx->replylen = len;
809 if (gctx->reply.op != BOOTP_REPLY)
812 /* Is this an answer to our query */
813 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
814 if (gctx->reply.xid != ifctx->call.xid)
817 /* Same HW address size ? */
818 if (gctx->reply.hlen != ifctx->call.hlen)
821 /* Correct HW address ? */
822 if (bcmp(gctx->reply.chaddr,
824 ifctx->call.hlen) != 0)
831 s = bootpc_tag(&gctx->tmptag,
844 s = "DHCP (unexpected)";
850 printf("Received %s packet"
853 ifctx->ireq.ifr_name);
854 print_in_addr(gctx->reply.siaddr);
855 if (gctx->reply.giaddr.s_addr !=
858 print_in_addr(gctx->reply.giaddr);
860 if (bootpc_received(gctx, ifctx) != 0) {
861 printf(" (accepted)");
862 if (ifctx->outstanding) {
863 ifctx->outstanding = 0;
866 /* Network settle delay */
867 if (outstanding == 0)
868 atimo = time_second +
871 printf(" (ignored)");
872 if (ifctx->gotrootpath ||
873 gctx->any_root_overrides) {
875 rtimo = time_second +
877 if (ifctx->gotrootpath)
878 printf(" (got root path)");
884 if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
887 /* Force a retry if halfway in DHCP negotiation */
889 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
890 if (ifctx->state == IF_DHCP_OFFERED) {
891 if (ifctx->dhcpquerytype == DHCP_DISCOVER)
894 ifctx->state = IF_DHCP_UNRESOLVED;
900 if (gotrootpath != 0) {
901 gctx->gotrootpath = gotrootpath;
902 if (rtimo != 0 && time_second >= rtimo)
905 } /* forever send/receive */
908 * XXX: These are errors of varying seriousness being silently
912 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
913 if (bootpc_ifctx_isresolved(ifctx) == 0) {
914 printf("%s timeout for interface %s\n",
915 ifctx->dhcpquerytype != DHCP_NOMSG ?
917 ifctx->ireq.ifr_name);
920 if (gctx->gotrootpath != 0) {
922 printf("Got a root path, ignoring remaining timeout\n");
927 #ifndef BOOTP_NFSROOT
928 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
929 if (bootpc_ifctx_isresolved(ifctx) != 0) {
941 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
944 struct in_aliasreq *ifra;
945 struct sockaddr_in *sin;
949 ifra = &ifctx->iareq;
952 * Bring up the interface.
954 * Get the old interface flags and or IFF_UP into them; if
955 * IFF_UP set blindly, interface selection can be clobbered.
957 error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
959 panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
960 ifr->ifr_flags |= IFF_UP;
961 error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
963 panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
966 * Do enough of ifconfig(8) so that the chosen interface
967 * can talk to the servers. Set address to 0.0.0.0/8 and
968 * broadcast address to local broadcast.
970 sin = (struct sockaddr_in *)&ifra->ifra_addr;
972 sin = (struct sockaddr_in *)&ifra->ifra_mask;
974 sin->sin_addr.s_addr = htonl(IN_CLASSA_NET);
975 sin = (struct sockaddr_in *)&ifra->ifra_broadaddr;
977 sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
978 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
980 panic("%s: SIOCAIFADDR, error=%d", __func__, error);
984 bootpc_shutdown_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
987 struct sockaddr_in *sin;
992 printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
993 error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
995 panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
996 ifr->ifr_flags &= ~IFF_UP;
997 error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
999 panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
1001 sin = (struct sockaddr_in *) &ifr->ifr_addr;
1003 error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
1005 panic("%s: SIOCDIFADDR, error=%d", __func__, error);
1009 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
1010 struct bootpc_globalcontext *gctx, struct thread *td)
1013 struct sockaddr_in defdst;
1014 struct sockaddr_in defmask;
1015 struct sockaddr_in *sin;
1017 struct in_aliasreq *ifra;
1018 struct sockaddr_in *myaddr;
1019 struct sockaddr_in *netmask;
1020 struct sockaddr_in *gw;
1023 ifra = &ifctx->iareq;
1024 myaddr = &ifctx->myaddr;
1025 netmask = &ifctx->netmask;
1028 if (bootpc_ifctx_isresolved(ifctx) == 0) {
1029 /* Shutdown interfaces where BOOTP failed */
1030 bootpc_shutdown_interface(ifctx, td);
1034 printf("Adjusted interface %s\n", ifctx->ireq.ifr_name);
1036 * Do enough of ifconfig(8) so that the chosen interface
1037 * can talk to the servers. (just set the address)
1039 sin = (struct sockaddr_in *) &ifr->ifr_addr;
1041 error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
1043 panic("%s: SIOCDIFADDR, error=%d", __func__, error);
1045 bcopy(myaddr, &ifra->ifra_addr, sizeof(*myaddr));
1046 bcopy(netmask, &ifra->ifra_mask, sizeof(*netmask));
1047 clear_sinaddr(&ifra->ifra_broadaddr);
1048 ifra->ifra_broadaddr.sin_addr.s_addr = myaddr->sin_addr.s_addr |
1049 ~netmask->sin_addr.s_addr;
1051 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
1053 panic("%s: SIOCAIFADDR, error=%d", __func__, error);
1055 /* Add new default route */
1057 if (ifctx->gotgw != 0 || gctx->gotgw == 0) {
1058 clear_sinaddr(&defdst);
1059 clear_sinaddr(&defmask);
1060 /* XXX MRT just table 0 */
1061 error = rtrequest_fib(RTM_ADD,
1062 (struct sockaddr *) &defdst, (struct sockaddr *) gw,
1063 (struct sockaddr *) &defmask,
1064 (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL, RT_DEFAULT_FIB);
1066 printf("%s: RTM_ADD, error=%d\n", __func__, error);
1075 setfs(struct sockaddr_in *addr, char *path, char *p,
1076 const struct in_addr *siaddr)
1079 if (getip(&p, &addr->sin_addr) == 0) {
1080 if (siaddr != NULL && *p == '/')
1081 bcopy(siaddr, &addr->sin_addr, sizeof(struct in_addr));
1090 addr->sin_len = sizeof(struct sockaddr_in);
1091 addr->sin_family = AF_INET;
1093 strlcpy(path, p, MNAMELEN);
1098 getip(char **ptr, struct in_addr *addr)
1106 if (((val = getdec(&p)) < 0) || (val > 255))
1112 if (((val = getdec(&p)) < 0) || (val > 255))
1118 if (((val = getdec(&p)) < 0) || (val > 255))
1124 if (((val = getdec(&p)) < 0) || (val > 255))
1128 addr->s_addr = htonl(ip);
1141 if ((*p < '0') || (*p > '9'))
1143 while ((*p >= '0') && (*p <= '9')) {
1144 ret = ret * 10 + (*p - '0');
1152 mountopts(struct nfs_args *args, char *p)
1154 args->version = NFS_ARGSVERSION;
1155 args->rsize = BOOTP_BLOCKSIZE;
1156 args->wsize = BOOTP_BLOCKSIZE;
1157 args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
1158 args->sotype = SOCK_DGRAM;
1160 nfs_parse_options(p, args);
1164 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
1170 alignedlen = ( len + 3 ) & ~3;
1172 if (m->m_len < alignedlen) {
1173 m = m_pullup(m, alignedlen);
1179 bcopy(mtod(m, u_char *), buf, len);
1180 m_adj(m, alignedlen);
1186 xdr_int_decode(struct mbuf **mptr, int *iptr)
1190 if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
1192 *iptr = fxdr_unsigned(u_int32_t, i);
1197 print_sin_addr(struct sockaddr_in *sin)
1200 print_in_addr(sin->sin_addr);
1204 print_in_addr(struct in_addr addr)
1208 ip = ntohl(addr.s_addr);
1209 printf("%d.%d.%d.%d",
1210 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1214 bootpc_compose_query(struct bootpc_ifcontext *ifctx, struct thread *td)
1216 unsigned char *vendp;
1217 unsigned char vendor_client[64];
1219 uint8_t vendor_client_len;
1221 ifctx->gotrootpath = 0;
1223 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1226 ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */
1227 ifctx->call.htype = 1; /* 10mb ethernet */
1228 ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1229 ifctx->call.hops = 0;
1230 if (bootpc_ifctx_isunresolved(ifctx) != 0)
1232 ifctx->call.xid = txdr_unsigned(ifctx->xid);
1233 bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1235 vendp = ifctx->call.vend;
1236 *vendp++ = 99; /* RFC1048 cookie */
1240 *vendp++ = TAG_MAXMSGSIZE;
1242 *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1243 *vendp++ = sizeof(struct bootp_packet) & 255;
1245 snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1246 ostype, MACHINE, osrelease);
1247 vendor_client_len = strlen(vendor_client);
1248 *vendp++ = TAG_VENDOR_INDENTIFIER;
1249 *vendp++ = vendor_client_len;
1250 memcpy(vendp, vendor_client, vendor_client_len);
1251 vendp += vendor_client_len;
1252 ifctx->dhcpquerytype = DHCP_NOMSG;
1253 switch (ifctx->state) {
1254 case IF_DHCP_UNRESOLVED:
1255 *vendp++ = TAG_DHCP_MSGTYPE;
1257 *vendp++ = DHCP_DISCOVER;
1258 ifctx->dhcpquerytype = DHCP_DISCOVER;
1259 ifctx->gotdhcpserver = 0;
1261 case IF_DHCP_OFFERED:
1262 *vendp++ = TAG_DHCP_MSGTYPE;
1264 *vendp++ = DHCP_REQUEST;
1265 ifctx->dhcpquerytype = DHCP_REQUEST;
1266 *vendp++ = TAG_DHCP_REQ_ADDR;
1268 memcpy(vendp, &ifctx->reply.yiaddr, 4);
1270 if (ifctx->gotdhcpserver != 0) {
1271 *vendp++ = TAG_DHCP_SERVERID;
1273 memcpy(vendp, &ifctx->dhcpserver, 4);
1276 *vendp++ = TAG_DHCP_LEASETIME;
1278 leasetime = htonl(300);
1279 memcpy(vendp, &leasetime, 4);
1287 ifctx->call.secs = 0;
1288 ifctx->call.flags = htons(0x8000); /* We need a broadcast answer */
1292 bootpc_hascookie(struct bootp_packet *bp)
1295 return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1296 bp->vend[2] == 83 && bp->vend[3] == 99);
1300 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1301 unsigned char *start, int len, int tag)
1307 if (tctx->badtag != 0 || tctx->badopt != 0)
1315 if (code == TAG_PAD)
1317 if (code == TAG_END)
1319 if (j >= ej || j + *j + 1 > ej) {
1325 if (tctx->taglen + len > TAG_MAXLEN) {
1331 memcpy(tctx->buf + tctx->taglen,
1333 tctx->taglen += len;
1335 if (code == TAG_OVERLOAD)
1336 tctx->overload = *j;
1342 static unsigned char *
1343 bootpc_tag(struct bootpc_tagcontext *tctx,
1344 struct bootp_packet *bp, int len, int tag)
1352 if (bootpc_hascookie(bp) == 0)
1355 bootpc_tag_helper(tctx, &bp->vend[4],
1356 (unsigned char *) bp + len - &bp->vend[4], tag);
1358 if ((tctx->overload & OVERLOAD_FILE) != 0)
1359 bootpc_tag_helper(tctx,
1360 (unsigned char *) bp->file,
1363 if ((tctx->overload & OVERLOAD_SNAME) != 0)
1364 bootpc_tag_helper(tctx,
1365 (unsigned char *) bp->sname,
1369 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1371 tctx->buf[tctx->taglen] = '\0';
1376 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1377 struct bootpc_globalcontext *gctx)
1383 ifctx->gotnetmask = 0;
1385 clear_sinaddr(&ifctx->myaddr);
1386 clear_sinaddr(&ifctx->netmask);
1387 clear_sinaddr(&ifctx->gw);
1389 ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1391 ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1393 printf("%s at ", ifctx->ireq.ifr_name);
1394 print_sin_addr(&ifctx->myaddr);
1396 print_in_addr(ifctx->reply.siaddr);
1398 ifctx->gw.sin_addr = ifctx->reply.giaddr;
1399 if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1400 printf(" via gateway ");
1401 print_in_addr(ifctx->reply.giaddr);
1404 /* This call used for the side effect (overload flag) */
1405 (void) bootpc_tag(&gctx->tmptag,
1406 &ifctx->reply, ifctx->replylen, TAG_END);
1408 if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1409 if (ifctx->reply.sname[0] != '\0')
1410 printf(" server name %s", ifctx->reply.sname);
1411 if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1412 if (ifctx->reply.file[0] != '\0')
1413 printf(" boot file %s", ifctx->reply.file);
1417 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1420 if (gctx->tag.taglen != 4)
1421 panic("bootpc: subnet mask len is %d",
1423 bcopy(p, &ifctx->netmask.sin_addr, 4);
1424 ifctx->gotnetmask = 1;
1425 printf("subnet mask ");
1426 print_sin_addr(&ifctx->netmask);
1430 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1434 if (gctx->tag.taglen % 4)
1435 panic("bootpc: Router Len is %d", gctx->tag.taglen);
1436 if (gctx->tag.taglen > 0) {
1437 bcopy(p, &ifctx->gw.sin_addr, 4);
1439 print_sin_addr(&ifctx->gw);
1447 * Choose a root filesystem. If a value is forced in the environment
1448 * and it contains "nfs:", use it unconditionally. Otherwise, if the
1449 * kernel is compiled with the ROOTDEVNAME option, then use it if:
1450 * - The server doesn't provide a pathname.
1451 * - The boothowto flags include RB_DFLTROOT (user said to override
1452 * the server value).
1455 if ((s = getenv("vfs.root.mountfrom")) != NULL) {
1456 if ((p = strstr(s, "nfs:")) != NULL)
1457 p = strdup(p + 4, M_TEMP);
1461 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1465 if ((p == NULL || (boothowto & RB_DFLTROOT) != 0) &&
1466 (p = strstr(ROOTDEVNAME, "nfs:")) != NULL) {
1471 if (gctx->setrootfs != NULL) {
1472 printf("rootfs %s (ignored) ", p);
1473 } else if (setfs(&nd->root_saddr,
1474 nd->root_hostnam, p, &ifctx->reply.siaddr)) {
1476 printf("root_server ");
1477 print_sin_addr(&nd->root_saddr);
1480 printf("rootfs %s ", p);
1481 gctx->gotrootpath = 1;
1482 ifctx->gotrootpath = 1;
1483 gctx->setrootfs = ifctx;
1485 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1489 mountopts(&nd->root_args, p);
1490 printf("rootopts %s ", p);
1493 panic("Failed to set rootfs to %s", p);
1496 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1499 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1500 panic("bootpc: hostname >= %d bytes",
1502 if (gctx->sethostname != NULL) {
1503 printf("hostname %s (ignored) ", p);
1505 strcpy(nd->my_hostnam, p);
1506 mtx_lock(&prison0.pr_mtx);
1507 strcpy(prison0.pr_hostname, p);
1508 mtx_unlock(&prison0.pr_mtx);
1509 printf("hostname %s ", p);
1510 gctx->sethostname = ifctx;
1513 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1515 if (p != NULL) { /* store in a sysctl variable */
1516 int i, l = sizeof(bootp_cookie) - 1;
1517 for (i = 0; i < l && p[i] != '\0'; i++)
1518 bootp_cookie[i] = p[i];
1525 if (ifctx->gotnetmask == 0) {
1526 if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1527 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1528 else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1529 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1531 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1533 if (ifctx->gotgw == 0) {
1535 ifctx->gw.sin_addr.s_addr = ifctx->myaddr.sin_addr.s_addr;
1542 struct bootpc_ifcontext *ifctx; /* Interface BOOTP contexts */
1543 struct bootpc_globalcontext *gctx; /* Global BOOTP context */
1545 struct sockaddr_dl *sdl;
1548 #ifndef BOOTP_WIRED_TO
1551 struct nfsv3_diskless *nd;
1556 timeout = BOOTP_IFACE_WAIT_TIMEOUT * hz;
1559 nd = &nfsv3_diskless;
1563 * If already filled in, don't touch it here
1565 if (nfs_diskless_valid != 0)
1568 gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK | M_ZERO);
1569 STAILQ_INIT(&gctx->interfaces);
1570 gctx->xid = ~0xFFFF;
1571 gctx->starttime = time_second;
1574 * If ROOTDEVNAME is defined or vfs.root.mountfrom is set then we have
1575 * root-path overrides that can potentially let us boot even if we don't
1576 * get a root path from the server, so we can treat that as a non-error.
1579 gctx->any_root_overrides = 1;
1581 gctx->any_root_overrides = testenv("vfs.root.mountfrom");
1585 * Find a network interface.
1587 CURVNET_SET(TD_TO_VNET(td));
1588 #ifdef BOOTP_WIRED_TO
1589 printf("%s: wired to interface '%s'\n", __func__,
1590 __XSTRING(BOOTP_WIRED_TO));
1594 * Preallocate interface context storage, if another interface
1595 * attaches and wins the race, it won't be eligible for bootp.
1599 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1600 if ((ifp->if_flags &
1601 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1604 switch (ifp->if_alloctype) {
1616 panic("%s: no eligible interfaces", __func__);
1617 for (; ifcnt > 0; ifcnt--)
1622 ifctx = STAILQ_FIRST(&gctx->interfaces);
1624 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1627 #ifdef BOOTP_WIRED_TO
1628 if (strcmp(ifp->if_xname, __XSTRING(BOOTP_WIRED_TO)) != 0)
1631 if ((ifp->if_flags &
1632 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1635 switch (ifp->if_alloctype) {
1644 strlcpy(ifctx->ireq.ifr_name, ifp->if_xname,
1645 sizeof(ifctx->ireq.ifr_name));
1648 /* Get HW address */
1650 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1651 if (ifa->ifa_addr->sa_family == AF_LINK) {
1652 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1653 if (sdl->sdl_type == IFT_ETHER)
1657 panic("bootpc: Unable to find HW address for %s",
1658 ifctx->ireq.ifr_name);
1661 ifctx = STAILQ_NEXT(ifctx, next);
1666 if (STAILQ_EMPTY(&gctx->interfaces) ||
1667 STAILQ_FIRST(&gctx->interfaces)->ifp == NULL) {
1669 pause("bootpc", delay);
1673 #ifdef BOOTP_WIRED_TO
1674 panic("%s: Could not find interface specified "
1675 "by BOOTP_WIRED_TO: "
1676 __XSTRING(BOOTP_WIRED_TO), __func__);
1678 panic("%s: no suitable interface", __func__);
1682 error = socreate(AF_INET, &bootp_so, SOCK_DGRAM, 0, td->td_ucred, td);
1684 panic("%s: socreate, error=%d", __func__, error);
1686 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1687 bootpc_fakeup_interface(ifctx, td);
1689 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1690 bootpc_compose_query(ifctx, td);
1692 error = bootpc_call(gctx, td);
1694 printf("BOOTP call failed\n");
1697 mountopts(&nd->root_args, NULL);
1699 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1700 if (bootpc_ifctx_isresolved(ifctx) != 0)
1701 bootpc_decode_reply(nd, ifctx, gctx);
1703 #ifdef BOOTP_NFSROOT
1704 if (gctx->gotrootpath == 0 && gctx->any_root_overrides == 0)
1705 panic("bootpc: No root path offered");
1708 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1709 bootpc_adjust_interface(ifctx, gctx, td);
1713 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1714 if (ifctx->gotrootpath != 0)
1716 if (ifctx == NULL) {
1717 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1718 if (bootpc_ifctx_isresolved(ifctx) != 0)
1724 if (gctx->gotrootpath != 0) {
1726 setenv("boot.netif.name", ifctx->ifp->if_xname);
1728 error = md_mount(&nd->root_saddr, nd->root_hostnam,
1729 nd->root_fh, &nd->root_fhsize,
1730 &nd->root_args, td);
1732 if (gctx->any_root_overrides == 0)
1733 panic("nfs_boot: mount root, error=%d", error);
1737 rootdevnames[0] = "nfs:";
1739 rootdevnames[1] = "oldnfs:";
1741 nfs_diskless_valid = 3;
1744 strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1745 bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1746 bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1747 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1748 ifctx->myaddr.sin_addr.s_addr |
1749 ~ ifctx->netmask.sin_addr.s_addr;
1750 bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1753 while((ifctx = STAILQ_FIRST(&gctx->interfaces)) != NULL) {
1754 STAILQ_REMOVE_HEAD(&gctx->interfaces, next);
1755 free(ifctx, M_TEMP);
1762 * Given a server pathname, get an NFS file handle.
1763 * Also, sets sin->sin_port to the NFS service port.
1766 md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp, int *fhsizep,
1767 struct nfs_args *args, struct thread *td)
1775 #define RPCPROG_MNT 100005
1776 #define RPCMNT_VER1 1
1777 #define RPCMNT_VER3 3
1778 #define RPCMNT_MOUNT 1
1779 #define AUTH_SYS 1 /* unix style (uid, gids) */
1780 #define AUTH_UNIX AUTH_SYS
1782 /* XXX honor v2/v3 flags in args->flags? */
1784 /* First try NFS v3 */
1785 /* Get port number for MOUNTD. */
1786 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1787 &mdsin->sin_port, td);
1789 m = xdr_string_encode(path, strlen(path));
1791 /* Do RPC to mountd. */
1792 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1793 RPCMNT_MOUNT, &m, NULL, td);
1796 args->flags |= NFSMNT_NFSV3;
1799 /* Fallback to NFS v2 */
1801 /* Get port number for MOUNTD. */
1802 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1803 &mdsin->sin_port, td);
1807 m = xdr_string_encode(path, strlen(path));
1809 /* Do RPC to mountd. */
1810 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1811 RPCMNT_MOUNT, &m, NULL, td);
1813 return error; /* message already freed */
1819 if (xdr_int_decode(&m, &error) != 0 || error != 0)
1822 if ((args->flags & NFSMNT_NFSV3) != 0) {
1823 if (xdr_int_decode(&m, fhsizep) != 0 ||
1824 *fhsizep > NFSX_V3FHMAX ||
1828 *fhsizep = NFSX_V2FH;
1830 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
1833 if (args->flags & NFSMNT_NFSV3) {
1834 if (xdr_int_decode(&m, &authcount) != 0)
1837 if (authcount < 0 || authcount > 100)
1839 while (authcount > 0) {
1840 if (xdr_int_decode(&m, &authver) != 0)
1842 if (authver == AUTH_UNIX)
1846 if (authunixok == 0)
1850 /* Set port number for NFS use. */
1851 error = krpc_portmap(mdsin, NFS_PROG,
1853 NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
1854 &mdsin->sin_port, td);
1866 SYSINIT(bootp_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, bootpc_init, NULL);