2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1995 Gordon Ross, Adam Glass
5 * Copyright (c) 1992 Regents of the University of California.
8 * This software was developed by the Computer Systems Engineering group
9 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10 * contributed to Berkeley.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Lawrence Berkeley Laboratory and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * $NetBSD: krpc_subr.c,v 1.10 1995/08/08 20:43:43 gwr Exp $
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include "opt_bootp.h"
50 #include "opt_rootdevname.h"
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/endian.h>
56 #include <sys/kernel.h>
57 #include <sys/sockio.h>
58 #include <sys/malloc.h>
59 #include <sys/mount.h>
62 #include <sys/reboot.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/sysctl.h>
69 #include <net/if_var.h>
70 #include <net/route.h>
71 #include <net/route/route_ctl.h>
73 #include <netinet/in.h>
74 #include <netinet/in_var.h>
75 #include <net/if_types.h>
76 #include <net/if_dl.h>
79 #include <nfs/nfsproto.h>
80 #include <nfsclient/nfs.h>
81 #include <nfs/nfsdiskless.h>
83 #include <nfs/xdr_subs.h>
85 #define BOOTP_MIN_LEN 300 /* Minimum size of bootp udp packet */
87 #ifndef BOOTP_SETTLE_DELAY
88 #define BOOTP_SETTLE_DELAY 3
92 * Wait 10 seconds for interface appearance
93 * USB ethernet adapters might require some time to pop up
95 #ifndef BOOTP_IFACE_WAIT_TIMEOUT
96 #define BOOTP_IFACE_WAIT_TIMEOUT 10
100 * What is the longest we will wait before re-sending a request?
101 * Note this is also the frequency of "RPC timeout" messages.
102 * The re-send loop count sup linearly to this maximum, so the
103 * first complaint will happen after (1+2+3+4+5)=15 seconds.
105 #define MAX_RESEND_DELAY 5 /* seconds */
107 /* Definitions from RFC951 */
108 struct bootp_packet {
116 struct in_addr ciaddr;
117 struct in_addr yiaddr;
118 struct in_addr siaddr;
119 struct in_addr giaddr;
120 unsigned char chaddr[16];
123 unsigned char vend[1222];
126 struct bootpc_ifcontext {
127 STAILQ_ENTRY(bootpc_ifcontext) next;
128 struct bootp_packet call;
129 struct bootp_packet reply;
134 struct in_aliasreq _in_alias_req;
136 #define ireq _req._ifreq
137 #define iareq _req._in_alias_req
139 struct sockaddr_dl *sdl;
140 struct sockaddr_in myaddr;
141 struct sockaddr_in netmask;
142 struct sockaddr_in gw;
158 int dhcpquerytype; /* dhcp type sent */
159 struct in_addr dhcpserver;
164 #define TAG_MAXLEN 1024
165 struct bootpc_tagcontext {
166 char buf[TAG_MAXLEN + 1];
174 struct bootpc_globalcontext {
175 STAILQ_HEAD(, bootpc_ifcontext) interfaces;
177 int any_root_overrides;
183 struct bootp_packet reply;
185 struct bootpc_ifcontext *setrootfs;
186 struct bootpc_ifcontext *sethostname;
187 struct bootpc_tagcontext tmptag;
188 struct bootpc_tagcontext tag;
191 #define IPPORT_BOOTPC 68
192 #define IPPORT_BOOTPS 67
194 #define BOOTP_REQUEST 1
195 #define BOOTP_REPLY 2
198 #define TAG_PAD 0 /* Pad option, implicit length 1 */
199 #define TAG_SUBNETMASK 1 /* RFC 950 subnet mask */
200 #define TAG_ROUTERS 3 /* Routers (in order of preference) */
201 #define TAG_HOSTNAME 12 /* Client host name */
202 #define TAG_ROOT 17 /* Root path */
203 #define TAG_INTF_MTU 26 /* Interface MTU Size (RFC2132) */
205 /* DHCP specific tags */
206 #define TAG_OVERLOAD 52 /* Option Overload */
207 #define TAG_MAXMSGSIZE 57 /* Maximum DHCP Message Size */
209 #define TAG_END 255 /* End Option (i.e. no more options) */
211 /* Overload values */
212 #define OVERLOAD_FILE 1
213 #define OVERLOAD_SNAME 2
215 /* Site specific tags: */
216 #define TAG_ROOTOPTS 130
217 #define TAG_COOKIE 134 /* ascii info for userland, via sysctl */
219 #define TAG_DHCP_MSGTYPE 53
220 #define TAG_DHCP_REQ_ADDR 50
221 #define TAG_DHCP_SERVERID 54
222 #define TAG_DHCP_LEASETIME 51
224 #define TAG_VENDOR_INDENTIFIER 60
227 #define DHCP_DISCOVER 1
229 #define DHCP_REQUEST 3
232 /* NFS read/write block size */
233 #ifndef BOOTP_BLOCKSIZE
234 #define BOOTP_BLOCKSIZE 8192
237 static char bootp_cookie[128];
238 static struct socket *bootp_so;
239 SYSCTL_STRING(_kern, OID_AUTO, bootp_cookie, CTLFLAG_RD,
240 bootp_cookie, 0, "Cookie (T134) supplied by bootp server");
243 static int md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp,
244 int *fhsizep, struct nfs_args *args, struct thread *td);
245 static int setfs(struct sockaddr_in *addr, char *path, char *p,
246 const struct in_addr *siaddr);
247 static int getdec(char **ptr);
248 static int getip(char **ptr, struct in_addr *ip);
249 static void mountopts(struct nfs_args *args, char *p);
250 static int xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len);
251 static int xdr_int_decode(struct mbuf **ptr, int *iptr);
252 static void print_in_addr(struct in_addr addr);
253 static void print_sin_addr(struct sockaddr_in *addr);
254 static void clear_sinaddr(struct sockaddr_in *sin);
255 static void allocifctx(struct bootpc_globalcontext *gctx);
256 static void bootpc_compose_query(struct bootpc_ifcontext *ifctx,
258 static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx,
259 struct bootp_packet *bp, int len, int tag);
260 static void bootpc_tag_helper(struct bootpc_tagcontext *tctx,
261 unsigned char *start, int len, int tag);
264 void bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa);
265 void bootpboot_p_iflist(void);
268 static int bootpc_call(struct bootpc_globalcontext *gctx,
271 static void bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
274 static void bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
275 struct bootpc_globalcontext *gctx, struct thread *td);
277 static void bootpc_decode_reply(struct nfsv3_diskless *nd,
278 struct bootpc_ifcontext *ifctx,
279 struct bootpc_globalcontext *gctx);
281 static int bootpc_received(struct bootpc_globalcontext *gctx,
282 struct bootpc_ifcontext *ifctx);
284 static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
285 static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
286 static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
289 * In order to have multiple active interfaces with address 0.0.0.0
290 * and be able to send data to a selected interface, we first set
291 * mask to /8 on all interfaces, and temporarily set it to /0 when
297 bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
300 printf("%s flags %x, addr ",
301 ifp->if_xname, ifp->if_flags);
302 print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
303 printf(", broadcast ");
304 print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
305 printf(", netmask ");
306 print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
311 bootpboot_p_iflist(void)
316 printf("Interface list:\n");
318 for (ifp = CK_STAILQ_FIRST(&V_ifnet);
320 ifp = CK_STAILQ_NEXT(ifp, if_link)) {
321 for (ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
323 ifa = CK_STAILQ_NEXT(ifa, ifa_link))
324 if (ifa->ifa_addr->sa_family == AF_INET)
325 bootpboot_p_if(ifp, ifa);
329 #endif /* defined(BOOTP_DEBUG) */
332 clear_sinaddr(struct sockaddr_in *sin)
335 bzero(sin, sizeof(*sin));
336 sin->sin_len = sizeof(*sin);
337 sin->sin_family = AF_INET;
338 sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
343 allocifctx(struct bootpc_globalcontext *gctx)
345 struct bootpc_ifcontext *ifctx;
347 ifctx = malloc(sizeof(*ifctx), M_TEMP, M_WAITOK | M_ZERO);
348 ifctx->xid = gctx->xid;
350 ifctx->state = IF_BOOTP_UNRESOLVED;
352 ifctx->state = IF_DHCP_UNRESOLVED;
355 STAILQ_INSERT_TAIL(&gctx->interfaces, ifctx, next);
359 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
362 if (ifctx->state == IF_BOOTP_RESOLVED ||
363 ifctx->state == IF_DHCP_RESOLVED)
369 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
372 if (ifctx->state == IF_BOOTP_UNRESOLVED ||
373 ifctx->state == IF_DHCP_UNRESOLVED)
379 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
382 if (ifctx->state == IF_BOOTP_FAILED ||
383 ifctx->state == IF_DHCP_FAILED)
389 bootpc_received(struct bootpc_globalcontext *gctx,
390 struct bootpc_ifcontext *ifctx)
392 unsigned char dhcpreplytype;
396 * Need timeout for fallback to less
397 * desirable alternative.
400 /* This call used for the side effect (badopt flag) */
401 (void) bootpc_tag(&gctx->tmptag, &gctx->reply,
405 /* If packet is invalid, ignore it */
406 if (gctx->tmptag.badopt != 0)
409 p = bootpc_tag(&gctx->tmptag, &gctx->reply,
410 gctx->replylen, TAG_DHCP_MSGTYPE);
414 dhcpreplytype = DHCP_NOMSG;
416 switch (ifctx->dhcpquerytype) {
418 if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */
419 #ifndef BOOTP_FORCE_DHCP
420 && dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */
426 if (dhcpreplytype != DHCP_ACK)
432 /* Ignore packet unless it gives us a root tag we didn't have */
434 if ((ifctx->state == IF_BOOTP_RESOLVED ||
435 (ifctx->dhcpquerytype == DHCP_DISCOVER &&
436 (ifctx->state == IF_DHCP_OFFERED ||
437 ifctx->state == IF_DHCP_RESOLVED))) &&
438 (bootpc_tag(&gctx->tmptag, &ifctx->reply,
441 bootpc_tag(&gctx->tmptag, &gctx->reply,
446 bcopy(&gctx->reply, &ifctx->reply, gctx->replylen);
447 ifctx->replylen = gctx->replylen;
449 /* XXX: Only reset if 'perfect' response */
450 if (ifctx->state == IF_BOOTP_UNRESOLVED)
451 ifctx->state = IF_BOOTP_RESOLVED;
452 else if (ifctx->state == IF_DHCP_UNRESOLVED &&
453 ifctx->dhcpquerytype == DHCP_DISCOVER) {
454 if (dhcpreplytype == DHCP_OFFER)
455 ifctx->state = IF_DHCP_OFFERED;
457 ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */
458 } else if (ifctx->state == IF_DHCP_OFFERED &&
459 ifctx->dhcpquerytype == DHCP_REQUEST)
460 ifctx->state = IF_DHCP_RESOLVED;
462 if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
463 ifctx->state != IF_BOOTP_RESOLVED) {
464 p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
465 ifctx->replylen, TAG_DHCP_SERVERID);
466 if (p != NULL && gctx->tmptag.taglen == 4) {
467 memcpy(&ifctx->dhcpserver, p, 4);
468 ifctx->gotdhcpserver = 1;
470 ifctx->gotdhcpserver = 0;
474 ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
477 ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
479 TAG_ROUTERS) != NULL);
480 ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
482 TAG_SUBNETMASK) != NULL);
487 bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
489 struct sockaddr_in *sin, dst;
493 int error, on, rcvflg, timo, len;
497 struct bootpc_ifcontext *ifctx;
505 bzero(&sopt, sizeof(sopt));
506 sopt.sopt_dir = SOPT_SET;
507 sopt.sopt_level = SOL_SOCKET;
508 sopt.sopt_name = SO_RCVTIMEO;
510 sopt.sopt_valsize = sizeof tv;
512 error = sosetopt(bootp_so, &sopt);
520 sopt.sopt_name = SO_BROADCAST;
522 sopt.sopt_valsize = sizeof on;
524 error = sosetopt(bootp_so, &sopt);
533 sopt.sopt_name = SO_DONTROUTE;
535 sopt.sopt_valsize = sizeof on;
537 error = sosetopt(bootp_so, &sopt);
542 * Bind the local endpoint to a bootp client port.
546 sin->sin_port = htons(IPPORT_BOOTPC);
547 error = sobind(bootp_so, (struct sockaddr *)sin, td);
549 printf("bind failed\n");
554 * Setup socket address for the server.
558 sin->sin_addr.s_addr = INADDR_BROADCAST;
559 sin->sin_port = htons(IPPORT_BOOTPS);
562 * Send it, repeatedly, until a reply is received,
563 * but delay each re-send by an increasing amount.
564 * If the delay hits the maximum, start complaining.
572 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
573 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
574 bootpc_tag(&gctx->tmptag, &ifctx->reply,
580 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
581 struct in_aliasreq *ifra = &ifctx->iareq;
582 sin = (struct sockaddr_in *)&ifra->ifra_mask;
584 ifctx->outstanding = 0;
585 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
589 if (bootpc_ifctx_isfailed(ifctx) != 0)
593 ifctx->outstanding = 1;
595 /* Proceed to next step in DHCP negotiation */
596 if ((ifctx->state == IF_DHCP_OFFERED &&
597 ifctx->dhcpquerytype != DHCP_REQUEST) ||
598 (ifctx->state == IF_DHCP_UNRESOLVED &&
599 ifctx->dhcpquerytype != DHCP_DISCOVER) ||
600 (ifctx->state == IF_BOOTP_UNRESOLVED &&
601 ifctx->dhcpquerytype != DHCP_NOMSG)) {
603 bootpc_compose_query(ifctx, td);
606 /* Send BOOTP request (or re-send). */
608 if (ifctx->sentmsg == 0) {
609 switch(ifctx->dhcpquerytype) {
621 printf("Sending %s packet from "
622 "interface %s (%*D)\n",
624 ifctx->ireq.ifr_name,
625 ifctx->sdl->sdl_alen,
626 (unsigned char *) LLADDR(ifctx->sdl),
631 aio.iov_base = (caddr_t) &ifctx->call;
632 aio.iov_len = sizeof(ifctx->call);
636 auio.uio_segflg = UIO_SYSSPACE;
637 auio.uio_rw = UIO_WRITE;
639 auio.uio_resid = sizeof(ifctx->call);
642 /* Set netmask to 0.0.0.0 */
644 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
647 panic("%s: SIOCAIFADDR, error=%d", __func__,
650 error = sosend(bootp_so, (struct sockaddr *) &dst,
651 &auio, NULL, NULL, 0, td);
653 printf("%s: sosend: %d state %08x\n", __func__,
654 error, (int )bootp_so->so_state);
656 /* Set netmask to 255.0.0.0 */
657 sin->sin_addr.s_addr = htonl(IN_CLASSA_NET);
658 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
661 panic("%s: SIOCAIFADDR, error=%d", __func__,
665 if (outstanding == 0 &&
666 (rtimo == 0 || time_second >= rtimo)) {
671 /* Determine new timeout. */
672 if (timo < MAX_RESEND_DELAY)
675 printf("DHCP/BOOTP timeout for server ");
676 print_sin_addr(&dst);
681 * Wait for up to timo seconds for a reply.
682 * The socket receive timeout was set to 1 second.
684 atimo = timo + time_second;
685 while (time_second < atimo) {
686 aio.iov_base = (caddr_t) &gctx->reply;
687 aio.iov_len = sizeof(gctx->reply);
691 auio.uio_segflg = UIO_SYSSPACE;
692 auio.uio_rw = UIO_READ;
694 auio.uio_resid = sizeof(gctx->reply);
698 error = soreceive(bootp_so, NULL, &auio,
699 NULL, NULL, &rcvflg);
700 gctx->secs = time_second - gctx->starttime;
701 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
702 if (bootpc_ifctx_isresolved(ifctx) != 0 ||
703 bootpc_ifctx_isfailed(ifctx) != 0)
706 ifctx->call.secs = htons(gctx->secs);
708 if (error == EWOULDBLOCK)
712 len = sizeof(gctx->reply) - auio.uio_resid;
714 /* Do we have the required number of bytes ? */
715 if (len < BOOTP_MIN_LEN)
717 gctx->replylen = len;
720 if (gctx->reply.op != BOOTP_REPLY)
723 /* Is this an answer to our query */
724 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
725 if (gctx->reply.xid != ifctx->call.xid)
728 /* Same HW address size ? */
729 if (gctx->reply.hlen != ifctx->call.hlen)
732 /* Correct HW address ? */
733 if (bcmp(gctx->reply.chaddr,
735 ifctx->call.hlen) != 0)
742 s = bootpc_tag(&gctx->tmptag,
755 s = "DHCP (unexpected)";
761 printf("Received %s packet"
764 ifctx->ireq.ifr_name);
765 print_in_addr(gctx->reply.siaddr);
766 if (gctx->reply.giaddr.s_addr !=
769 print_in_addr(gctx->reply.giaddr);
771 if (bootpc_received(gctx, ifctx) != 0) {
772 printf(" (accepted)");
773 if (ifctx->outstanding) {
774 ifctx->outstanding = 0;
777 /* Network settle delay */
778 if (outstanding == 0)
779 atimo = time_second +
782 printf(" (ignored)");
783 if (ifctx->gotrootpath ||
784 gctx->any_root_overrides) {
786 rtimo = time_second +
788 if (ifctx->gotrootpath)
789 printf(" (got root path)");
795 if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
798 /* Force a retry if halfway in DHCP negotiation */
800 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
801 if (ifctx->state == IF_DHCP_OFFERED) {
802 if (ifctx->dhcpquerytype == DHCP_DISCOVER)
805 ifctx->state = IF_DHCP_UNRESOLVED;
811 if (gotrootpath != 0) {
812 gctx->gotrootpath = gotrootpath;
813 if (rtimo != 0 && time_second >= rtimo)
816 } /* forever send/receive */
819 * XXX: These are errors of varying seriousness being silently
823 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
824 if (bootpc_ifctx_isresolved(ifctx) == 0) {
825 printf("%s timeout for interface %s\n",
826 ifctx->dhcpquerytype != DHCP_NOMSG ?
828 ifctx->ireq.ifr_name);
831 if (gctx->gotrootpath != 0) {
833 printf("Got a root path, ignoring remaining timeout\n");
838 #ifndef BOOTP_NFSROOT
839 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
840 if (bootpc_ifctx_isresolved(ifctx) != 0) {
852 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
855 struct in_aliasreq *ifra;
856 struct sockaddr_in *sin;
860 ifra = &ifctx->iareq;
863 * Bring up the interface.
865 * Get the old interface flags and or IFF_UP into them; if
866 * IFF_UP set blindly, interface selection can be clobbered.
868 error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
870 panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
871 ifr->ifr_flags |= IFF_UP;
872 error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
874 panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
877 * Do enough of ifconfig(8) so that the chosen interface
878 * can talk to the servers. Set address to 0.0.0.0/8 and
879 * broadcast address to local broadcast.
881 sin = (struct sockaddr_in *)&ifra->ifra_addr;
883 sin = (struct sockaddr_in *)&ifra->ifra_mask;
885 sin->sin_addr.s_addr = htonl(IN_CLASSA_NET);
886 sin = (struct sockaddr_in *)&ifra->ifra_broadaddr;
888 sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
889 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
891 panic("%s: SIOCAIFADDR, error=%d", __func__, error);
895 bootpc_shutdown_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
898 struct sockaddr_in *sin;
903 printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
904 error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
906 panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
907 ifr->ifr_flags &= ~IFF_UP;
908 error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
910 panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
912 sin = (struct sockaddr_in *) &ifr->ifr_addr;
914 error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
916 panic("%s: SIOCDIFADDR, error=%d", __func__, error);
920 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
921 struct bootpc_globalcontext *gctx, struct thread *td)
924 struct sockaddr_in *sin;
926 struct in_aliasreq *ifra;
927 struct sockaddr_in *myaddr;
928 struct sockaddr_in *netmask;
931 ifra = &ifctx->iareq;
932 myaddr = &ifctx->myaddr;
933 netmask = &ifctx->netmask;
935 if (bootpc_ifctx_isresolved(ifctx) == 0) {
936 /* Shutdown interfaces where BOOTP failed */
937 bootpc_shutdown_interface(ifctx, td);
941 printf("Adjusted interface %s", ifctx->ireq.ifr_name);
943 /* Do BOOTP interface options */
944 if (ifctx->mtu != 0) {
945 printf(" (MTU=%d%s)", ifctx->mtu,
946 (ifctx->mtu > 1514) ? "/JUMBO" : "");
947 ifr->ifr_mtu = ifctx->mtu;
948 error = ifioctl(bootp_so, SIOCSIFMTU, (caddr_t) ifr, td);
950 panic("%s: SIOCSIFMTU, error=%d", __func__, error);
955 * Do enough of ifconfig(8) so that the chosen interface
956 * can talk to the servers. (just set the address)
958 sin = (struct sockaddr_in *) &ifr->ifr_addr;
960 error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
962 panic("%s: SIOCDIFADDR, error=%d", __func__, error);
964 bcopy(myaddr, &ifra->ifra_addr, sizeof(*myaddr));
965 bcopy(netmask, &ifra->ifra_mask, sizeof(*netmask));
966 clear_sinaddr(&ifra->ifra_broadaddr);
967 ifra->ifra_broadaddr.sin_addr.s_addr = myaddr->sin_addr.s_addr |
968 ~netmask->sin_addr.s_addr;
970 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
972 panic("%s: SIOCAIFADDR, error=%d", __func__, error);
976 bootpc_add_default_route(struct bootpc_ifcontext *ifctx)
979 struct sockaddr_in defdst;
980 struct sockaddr_in defmask;
981 struct rt_addrinfo info;
982 struct rib_cmd_info rc;
984 if (ifctx->gw.sin_addr.s_addr == htonl(INADDR_ANY))
987 clear_sinaddr(&defdst);
988 clear_sinaddr(&defmask);
990 bzero((caddr_t)&info, sizeof(info));
991 info.rti_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
992 info.rti_info[RTAX_DST] = (struct sockaddr *)&defdst;
993 info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&defmask;
994 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ifctx->gw;
996 error = rib_action(RT_DEFAULT_FIB, RTM_ADD, &info, &rc);
999 printf("%s: RTM_ADD, error=%d\n", __func__, error);
1004 bootpc_remove_default_route(struct bootpc_ifcontext *ifctx)
1007 struct sockaddr_in defdst;
1008 struct sockaddr_in defmask;
1009 struct rt_addrinfo info;
1010 struct rib_cmd_info rc;
1012 if (ifctx->gw.sin_addr.s_addr == htonl(INADDR_ANY))
1015 clear_sinaddr(&defdst);
1016 clear_sinaddr(&defmask);
1018 bzero((caddr_t)&info, sizeof(info));
1019 info.rti_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
1020 info.rti_info[RTAX_DST] = (struct sockaddr *)&defdst;
1021 info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&defmask;
1022 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ifctx->gw;
1024 error = rib_action(RT_DEFAULT_FIB, RTM_DELETE, &info, &rc);
1026 printf("%s: RTM_DELETE, error=%d\n", __func__, error);
1031 setfs(struct sockaddr_in *addr, char *path, char *p,
1032 const struct in_addr *siaddr)
1035 if (getip(&p, &addr->sin_addr) == 0) {
1036 if (siaddr != NULL && *p == '/')
1037 bcopy(siaddr, &addr->sin_addr, sizeof(struct in_addr));
1046 addr->sin_len = sizeof(struct sockaddr_in);
1047 addr->sin_family = AF_INET;
1049 strlcpy(path, p, MNAMELEN);
1054 getip(char **ptr, struct in_addr *addr)
1062 if (((val = getdec(&p)) < 0) || (val > 255))
1068 if (((val = getdec(&p)) < 0) || (val > 255))
1074 if (((val = getdec(&p)) < 0) || (val > 255))
1080 if (((val = getdec(&p)) < 0) || (val > 255))
1084 addr->s_addr = htonl(ip);
1097 if ((*p < '0') || (*p > '9'))
1099 while ((*p >= '0') && (*p <= '9')) {
1100 ret = ret * 10 + (*p - '0');
1108 mountopts(struct nfs_args *args, char *p)
1110 args->version = NFS_ARGSVERSION;
1111 args->rsize = BOOTP_BLOCKSIZE;
1112 args->wsize = BOOTP_BLOCKSIZE;
1113 args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
1114 args->sotype = SOCK_DGRAM;
1116 nfs_parse_options(p, args);
1120 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
1126 alignedlen = ( len + 3 ) & ~3;
1128 if (m->m_len < alignedlen) {
1129 m = m_pullup(m, alignedlen);
1135 bcopy(mtod(m, u_char *), buf, len);
1136 m_adj(m, alignedlen);
1142 xdr_int_decode(struct mbuf **mptr, int *iptr)
1146 if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
1148 *iptr = fxdr_unsigned(u_int32_t, i);
1153 print_sin_addr(struct sockaddr_in *sin)
1156 print_in_addr(sin->sin_addr);
1160 print_in_addr(struct in_addr addr)
1164 ip = ntohl(addr.s_addr);
1165 printf("%d.%d.%d.%d",
1166 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1170 bootpc_compose_query(struct bootpc_ifcontext *ifctx, struct thread *td)
1172 unsigned char *vendp;
1173 unsigned char vendor_client[64];
1175 uint8_t vendor_client_len;
1177 ifctx->gotrootpath = 0;
1179 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1182 ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */
1183 ifctx->call.htype = 1; /* 10mb ethernet */
1184 ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1185 ifctx->call.hops = 0;
1186 if (bootpc_ifctx_isunresolved(ifctx) != 0)
1188 ifctx->call.xid = txdr_unsigned(ifctx->xid);
1189 bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1191 vendp = ifctx->call.vend;
1192 *vendp++ = 99; /* RFC1048 cookie */
1196 *vendp++ = TAG_MAXMSGSIZE;
1198 *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1199 *vendp++ = sizeof(struct bootp_packet) & 255;
1201 snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1202 ostype, MACHINE, osrelease);
1203 vendor_client_len = strlen(vendor_client);
1204 *vendp++ = TAG_VENDOR_INDENTIFIER;
1205 *vendp++ = vendor_client_len;
1206 memcpy(vendp, vendor_client, vendor_client_len);
1207 vendp += vendor_client_len;
1208 ifctx->dhcpquerytype = DHCP_NOMSG;
1209 switch (ifctx->state) {
1210 case IF_DHCP_UNRESOLVED:
1211 *vendp++ = TAG_DHCP_MSGTYPE;
1213 *vendp++ = DHCP_DISCOVER;
1214 ifctx->dhcpquerytype = DHCP_DISCOVER;
1215 ifctx->gotdhcpserver = 0;
1217 case IF_DHCP_OFFERED:
1218 *vendp++ = TAG_DHCP_MSGTYPE;
1220 *vendp++ = DHCP_REQUEST;
1221 ifctx->dhcpquerytype = DHCP_REQUEST;
1222 *vendp++ = TAG_DHCP_REQ_ADDR;
1224 memcpy(vendp, &ifctx->reply.yiaddr, 4);
1226 if (ifctx->gotdhcpserver != 0) {
1227 *vendp++ = TAG_DHCP_SERVERID;
1229 memcpy(vendp, &ifctx->dhcpserver, 4);
1232 *vendp++ = TAG_DHCP_LEASETIME;
1234 leasetime = htonl(300);
1235 memcpy(vendp, &leasetime, 4);
1243 ifctx->call.secs = 0;
1244 ifctx->call.flags = htons(0x8000); /* We need a broadcast answer */
1248 bootpc_hascookie(struct bootp_packet *bp)
1251 return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1252 bp->vend[2] == 83 && bp->vend[3] == 99);
1256 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1257 unsigned char *start, int len, int tag)
1263 if (tctx->badtag != 0 || tctx->badopt != 0)
1271 if (code == TAG_PAD)
1273 if (code == TAG_END)
1275 if (j >= ej || j + *j + 1 > ej) {
1281 if (tctx->taglen + len > TAG_MAXLEN) {
1287 memcpy(tctx->buf + tctx->taglen,
1289 tctx->taglen += len;
1291 if (code == TAG_OVERLOAD)
1292 tctx->overload = *j;
1298 static unsigned char *
1299 bootpc_tag(struct bootpc_tagcontext *tctx,
1300 struct bootp_packet *bp, int len, int tag)
1308 if (bootpc_hascookie(bp) == 0)
1311 bootpc_tag_helper(tctx, &bp->vend[4],
1312 (unsigned char *) bp + len - &bp->vend[4], tag);
1314 if ((tctx->overload & OVERLOAD_FILE) != 0)
1315 bootpc_tag_helper(tctx,
1316 (unsigned char *) bp->file,
1319 if ((tctx->overload & OVERLOAD_SNAME) != 0)
1320 bootpc_tag_helper(tctx,
1321 (unsigned char *) bp->sname,
1325 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1327 tctx->buf[tctx->taglen] = '\0';
1332 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1333 struct bootpc_globalcontext *gctx)
1339 ifctx->gotnetmask = 0;
1341 clear_sinaddr(&ifctx->myaddr);
1342 clear_sinaddr(&ifctx->netmask);
1343 clear_sinaddr(&ifctx->gw);
1345 ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1347 ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1349 printf("%s at ", ifctx->ireq.ifr_name);
1350 print_sin_addr(&ifctx->myaddr);
1352 print_in_addr(ifctx->reply.siaddr);
1354 ifctx->gw.sin_addr = ifctx->reply.giaddr;
1355 if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1356 printf(" via gateway ");
1357 print_in_addr(ifctx->reply.giaddr);
1360 /* This call used for the side effect (overload flag) */
1361 (void) bootpc_tag(&gctx->tmptag,
1362 &ifctx->reply, ifctx->replylen, TAG_END);
1364 if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1365 if (ifctx->reply.sname[0] != '\0')
1366 printf(" server name %s", ifctx->reply.sname);
1367 if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1368 if (ifctx->reply.file[0] != '\0')
1369 printf(" boot file %s", ifctx->reply.file);
1373 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1376 if (gctx->tag.taglen != 4)
1377 panic("bootpc: subnet mask len is %d",
1379 bcopy(p, &ifctx->netmask.sin_addr, 4);
1380 ifctx->gotnetmask = 1;
1381 printf("subnet mask ");
1382 print_sin_addr(&ifctx->netmask);
1386 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1390 if (gctx->tag.taglen % 4)
1391 panic("bootpc: Router Len is %d", gctx->tag.taglen);
1392 if (gctx->tag.taglen > 0) {
1393 bcopy(p, &ifctx->gw.sin_addr, 4);
1395 print_sin_addr(&ifctx->gw);
1403 * Choose a root filesystem. If a value is forced in the environment
1404 * and it contains "nfs:", use it unconditionally. Otherwise, if the
1405 * kernel is compiled with the ROOTDEVNAME option, then use it if:
1406 * - The server doesn't provide a pathname.
1407 * - The boothowto flags include RB_DFLTROOT (user said to override
1408 * the server value).
1411 if ((s = kern_getenv("vfs.root.mountfrom")) != NULL) {
1412 if ((p = strstr(s, "nfs:")) != NULL)
1413 p = strdup(p + 4, M_TEMP);
1417 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1420 ifctx->gotrootpath = 1;
1423 if ((p == NULL || (boothowto & RB_DFLTROOT) != 0) &&
1424 (p = strstr(ROOTDEVNAME, "nfs:")) != NULL) {
1429 if (gctx->setrootfs != NULL) {
1430 printf("rootfs %s (ignored) ", p);
1431 } else if (setfs(&nd->root_saddr,
1432 nd->root_hostnam, p, &ifctx->reply.siaddr)) {
1434 printf("root_server ");
1435 print_sin_addr(&nd->root_saddr);
1438 printf("rootfs %s ", p);
1439 gctx->gotrootpath = 1;
1440 gctx->setrootfs = ifctx;
1442 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1446 mountopts(&nd->root_args, p);
1447 printf("rootopts %s ", p);
1450 panic("Failed to set rootfs to %s", p);
1453 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1456 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1457 panic("bootpc: hostname >= %d bytes",
1459 if (gctx->sethostname != NULL) {
1460 printf("hostname %s (ignored) ", p);
1462 strcpy(nd->my_hostnam, p);
1463 mtx_lock(&prison0.pr_mtx);
1464 strcpy(prison0.pr_hostname, p);
1465 mtx_unlock(&prison0.pr_mtx);
1466 printf("hostname %s ", p);
1467 gctx->sethostname = ifctx;
1470 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1472 if (p != NULL) { /* store in a sysctl variable */
1473 int i, l = sizeof(bootp_cookie) - 1;
1474 for (i = 0; i < l && p[i] != '\0'; i++)
1475 bootp_cookie[i] = p[i];
1479 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1482 ifctx->mtu = be16dec(p);
1487 if (ifctx->gotnetmask == 0) {
1488 if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1489 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1490 else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1491 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1493 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1500 struct bootpc_ifcontext *ifctx; /* Interface BOOTP contexts */
1501 struct bootpc_globalcontext *gctx; /* Global BOOTP context */
1503 struct sockaddr_dl *sdl;
1506 #ifndef BOOTP_WIRED_TO
1509 struct nfsv3_diskless *nd;
1514 timeout = BOOTP_IFACE_WAIT_TIMEOUT * hz;
1517 nd = &nfsv3_diskless;
1521 * If already filled in, don't touch it here
1523 if (nfs_diskless_valid != 0)
1526 gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK | M_ZERO);
1527 STAILQ_INIT(&gctx->interfaces);
1528 gctx->xid = ~0xFFFF;
1529 gctx->starttime = time_second;
1532 * If ROOTDEVNAME is defined or vfs.root.mountfrom is set then we have
1533 * root-path overrides that can potentially let us boot even if we don't
1534 * get a root path from the server, so we can treat that as a non-error.
1537 gctx->any_root_overrides = 1;
1539 gctx->any_root_overrides = testenv("vfs.root.mountfrom");
1543 * Find a network interface.
1545 CURVNET_SET(TD_TO_VNET(td));
1546 #ifdef BOOTP_WIRED_TO
1547 printf("%s: wired to interface '%s'\n", __func__,
1548 __XSTRING(BOOTP_WIRED_TO));
1552 * Preallocate interface context storage, if another interface
1553 * attaches and wins the race, it won't be eligible for bootp.
1557 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1558 if ((ifp->if_flags &
1559 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1562 switch (ifp->if_alloctype) {
1572 panic("%s: no eligible interfaces", __func__);
1573 for (; ifcnt > 0; ifcnt--)
1578 ifctx = STAILQ_FIRST(&gctx->interfaces);
1580 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1583 #ifdef BOOTP_WIRED_TO
1584 if (strcmp(ifp->if_xname, __XSTRING(BOOTP_WIRED_TO)) != 0)
1587 if ((ifp->if_flags &
1588 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1591 switch (ifp->if_alloctype) {
1598 strlcpy(ifctx->ireq.ifr_name, ifp->if_xname,
1599 sizeof(ifctx->ireq.ifr_name));
1602 /* Get HW address */
1604 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1605 if (ifa->ifa_addr->sa_family == AF_LINK) {
1606 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1607 if (sdl->sdl_type == IFT_ETHER)
1611 panic("bootpc: Unable to find HW address for %s",
1612 ifctx->ireq.ifr_name);
1615 ifctx = STAILQ_NEXT(ifctx, next);
1620 if (STAILQ_EMPTY(&gctx->interfaces) ||
1621 STAILQ_FIRST(&gctx->interfaces)->ifp == NULL) {
1623 pause("bootpc", delay);
1627 #ifdef BOOTP_WIRED_TO
1628 panic("%s: Could not find interface specified "
1629 "by BOOTP_WIRED_TO: "
1630 __XSTRING(BOOTP_WIRED_TO), __func__);
1632 panic("%s: no suitable interface", __func__);
1636 error = socreate(AF_INET, &bootp_so, SOCK_DGRAM, 0, td->td_ucred, td);
1638 panic("%s: socreate, error=%d", __func__, error);
1640 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1641 bootpc_fakeup_interface(ifctx, td);
1643 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1644 bootpc_compose_query(ifctx, td);
1646 error = bootpc_call(gctx, td);
1648 printf("BOOTP call failed\n");
1651 mountopts(&nd->root_args, NULL);
1653 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1654 if (bootpc_ifctx_isresolved(ifctx) != 0)
1655 bootpc_decode_reply(nd, ifctx, gctx);
1657 #ifdef BOOTP_NFSROOT
1658 if (gctx->gotrootpath == 0 && gctx->any_root_overrides == 0)
1659 panic("bootpc: No root path offered");
1662 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1663 bootpc_adjust_interface(ifctx, gctx, td);
1667 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1668 if (ifctx->gotrootpath != 0)
1670 if (ifctx == NULL) {
1671 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1672 if (bootpc_ifctx_isresolved(ifctx) != 0)
1678 if (gctx->gotrootpath != 0) {
1679 struct epoch_tracker et;
1681 kern_setenv("boot.netif.name", ifctx->ifp->if_xname);
1683 NET_EPOCH_ENTER(et);
1684 bootpc_add_default_route(ifctx);
1685 error = md_mount(&nd->root_saddr, nd->root_hostnam,
1686 nd->root_fh, &nd->root_fhsize,
1687 &nd->root_args, td);
1688 bootpc_remove_default_route(ifctx);
1691 if (gctx->any_root_overrides == 0)
1692 panic("nfs_boot: mount root, error=%d", error);
1696 rootdevnames[0] = "nfs:";
1697 nfs_diskless_valid = 3;
1700 strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1701 bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1702 bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1703 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1704 ifctx->myaddr.sin_addr.s_addr |
1705 ~ ifctx->netmask.sin_addr.s_addr;
1706 bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1707 bcopy(&ifctx->gw, &nd->mygateway, sizeof(ifctx->gw));
1710 while((ifctx = STAILQ_FIRST(&gctx->interfaces)) != NULL) {
1711 STAILQ_REMOVE_HEAD(&gctx->interfaces, next);
1712 free(ifctx, M_TEMP);
1719 * Given a server pathname, get an NFS file handle.
1720 * Also, sets sin->sin_port to the NFS service port.
1723 md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp, int *fhsizep,
1724 struct nfs_args *args, struct thread *td)
1732 #define RPCPROG_MNT 100005
1733 #define RPCMNT_VER1 1
1734 #define RPCMNT_VER3 3
1735 #define RPCMNT_MOUNT 1
1736 #define AUTH_SYS 1 /* unix style (uid, gids) */
1737 #define AUTH_UNIX AUTH_SYS
1739 /* XXX honor v2/v3 flags in args->flags? */
1741 /* First try NFS v3 */
1742 /* Get port number for MOUNTD. */
1743 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1744 &mdsin->sin_port, td);
1746 m = xdr_string_encode(path, strlen(path));
1748 /* Do RPC to mountd. */
1749 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1750 RPCMNT_MOUNT, &m, NULL, td);
1753 args->flags |= NFSMNT_NFSV3;
1756 /* Fallback to NFS v2 */
1758 /* Get port number for MOUNTD. */
1759 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1760 &mdsin->sin_port, td);
1764 m = xdr_string_encode(path, strlen(path));
1766 /* Do RPC to mountd. */
1767 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1768 RPCMNT_MOUNT, &m, NULL, td);
1770 return error; /* message already freed */
1776 if (xdr_int_decode(&m, &error) != 0 || error != 0)
1779 if ((args->flags & NFSMNT_NFSV3) != 0) {
1780 if (xdr_int_decode(&m, fhsizep) != 0 ||
1781 *fhsizep > NFSX_V3FHMAX ||
1785 *fhsizep = NFSX_V2FH;
1787 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
1790 if (args->flags & NFSMNT_NFSV3) {
1791 if (xdr_int_decode(&m, &authcount) != 0)
1794 if (authcount < 0 || authcount > 100)
1796 while (authcount > 0) {
1797 if (xdr_int_decode(&m, &authver) != 0)
1799 if (authver == AUTH_UNIX)
1803 if (authunixok == 0)
1807 /* Set port number for NFS use. */
1808 error = krpc_portmap(mdsin, NFS_PROG,
1810 NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
1811 &mdsin->sin_port, td);
1823 SYSINIT(bootp_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, bootpc_init, NULL);