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>
72 #include <net/route_var.h>
75 #include <netinet/in.h>
76 #include <netinet/in_var.h>
77 #include <net/if_types.h>
78 #include <net/if_dl.h>
81 #include <nfs/nfsproto.h>
82 #include <nfsclient/nfs.h>
83 #include <nfs/nfsdiskless.h>
85 #include <nfs/xdr_subs.h>
87 #define BOOTP_MIN_LEN 300 /* Minimum size of bootp udp packet */
89 #ifndef BOOTP_SETTLE_DELAY
90 #define BOOTP_SETTLE_DELAY 3
94 * Wait 10 seconds for interface appearance
95 * USB ethernet adapters might require some time to pop up
97 #ifndef BOOTP_IFACE_WAIT_TIMEOUT
98 #define BOOTP_IFACE_WAIT_TIMEOUT 10
102 * What is the longest we will wait before re-sending a request?
103 * Note this is also the frequency of "RPC timeout" messages.
104 * The re-send loop count sup linearly to this maximum, so the
105 * first complaint will happen after (1+2+3+4+5)=15 seconds.
107 #define MAX_RESEND_DELAY 5 /* seconds */
109 /* Definitions from RFC951 */
110 struct bootp_packet {
118 struct in_addr ciaddr;
119 struct in_addr yiaddr;
120 struct in_addr siaddr;
121 struct in_addr giaddr;
122 unsigned char chaddr[16];
125 unsigned char vend[1222];
128 struct bootpc_ifcontext {
129 STAILQ_ENTRY(bootpc_ifcontext) next;
130 struct bootp_packet call;
131 struct bootp_packet reply;
136 struct in_aliasreq _in_alias_req;
138 #define ireq _req._ifreq
139 #define iareq _req._in_alias_req
141 struct sockaddr_dl *sdl;
142 struct sockaddr_in myaddr;
143 struct sockaddr_in netmask;
144 struct sockaddr_in gw;
160 int dhcpquerytype; /* dhcp type sent */
161 struct in_addr dhcpserver;
166 #define TAG_MAXLEN 1024
167 struct bootpc_tagcontext {
168 char buf[TAG_MAXLEN + 1];
176 struct bootpc_globalcontext {
177 STAILQ_HEAD(, bootpc_ifcontext) interfaces;
179 int any_root_overrides;
185 struct bootp_packet reply;
187 struct bootpc_ifcontext *setrootfs;
188 struct bootpc_ifcontext *sethostname;
189 struct bootpc_tagcontext tmptag;
190 struct bootpc_tagcontext tag;
193 #define IPPORT_BOOTPC 68
194 #define IPPORT_BOOTPS 67
196 #define BOOTP_REQUEST 1
197 #define BOOTP_REPLY 2
200 #define TAG_PAD 0 /* Pad option, implicit length 1 */
201 #define TAG_SUBNETMASK 1 /* RFC 950 subnet mask */
202 #define TAG_ROUTERS 3 /* Routers (in order of preference) */
203 #define TAG_HOSTNAME 12 /* Client host name */
204 #define TAG_ROOT 17 /* Root path */
205 #define TAG_INTF_MTU 26 /* Interface MTU Size (RFC2132) */
207 /* DHCP specific tags */
208 #define TAG_OVERLOAD 52 /* Option Overload */
209 #define TAG_MAXMSGSIZE 57 /* Maximum DHCP Message Size */
211 #define TAG_END 255 /* End Option (i.e. no more options) */
213 /* Overload values */
214 #define OVERLOAD_FILE 1
215 #define OVERLOAD_SNAME 2
217 /* Site specific tags: */
218 #define TAG_ROOTOPTS 130
219 #define TAG_COOKIE 134 /* ascii info for userland, via sysctl */
221 #define TAG_DHCP_MSGTYPE 53
222 #define TAG_DHCP_REQ_ADDR 50
223 #define TAG_DHCP_SERVERID 54
224 #define TAG_DHCP_LEASETIME 51
226 #define TAG_VENDOR_INDENTIFIER 60
229 #define DHCP_DISCOVER 1
231 #define DHCP_REQUEST 3
234 /* NFS read/write block size */
235 #ifndef BOOTP_BLOCKSIZE
236 #define BOOTP_BLOCKSIZE 8192
239 static char bootp_cookie[128];
240 static struct socket *bootp_so;
241 SYSCTL_STRING(_kern, OID_AUTO, bootp_cookie, CTLFLAG_RD,
242 bootp_cookie, 0, "Cookie (T134) supplied by bootp server");
245 static int md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp,
246 int *fhsizep, struct nfs_args *args, struct thread *td);
247 static int setfs(struct sockaddr_in *addr, char *path, char *p,
248 const struct in_addr *siaddr);
249 static int getdec(char **ptr);
250 static int getip(char **ptr, struct in_addr *ip);
251 static void mountopts(struct nfs_args *args, char *p);
252 static int xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len);
253 static int xdr_int_decode(struct mbuf **ptr, int *iptr);
254 static void print_in_addr(struct in_addr addr);
255 static void print_sin_addr(struct sockaddr_in *addr);
256 static void clear_sinaddr(struct sockaddr_in *sin);
257 static void allocifctx(struct bootpc_globalcontext *gctx);
258 static void bootpc_compose_query(struct bootpc_ifcontext *ifctx,
260 static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx,
261 struct bootp_packet *bp, int len, int tag);
262 static void bootpc_tag_helper(struct bootpc_tagcontext *tctx,
263 unsigned char *start, int len, int tag);
266 void bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma);
267 void bootpboot_p_rtentry(struct rtentry *rt);
268 void bootpboot_p_tree(struct radix_node *rn);
269 void bootpboot_p_rtlist(void);
270 void bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa);
271 void bootpboot_p_iflist(void);
274 static int bootpc_call(struct bootpc_globalcontext *gctx,
277 static void bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
280 static void bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
281 struct bootpc_globalcontext *gctx, struct thread *td);
283 static void bootpc_decode_reply(struct nfsv3_diskless *nd,
284 struct bootpc_ifcontext *ifctx,
285 struct bootpc_globalcontext *gctx);
287 static int bootpc_received(struct bootpc_globalcontext *gctx,
288 struct bootpc_ifcontext *ifctx);
290 static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
291 static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
292 static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
295 * In order to have multiple active interfaces with address 0.0.0.0
296 * and be able to send data to a selected interface, we first set
297 * mask to /8 on all interfaces, and temporarily set it to /0 when
303 bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma)
307 printf("(sockaddr *) <null>");
310 switch (sa->sa_family) {
313 struct sockaddr_in *sin;
315 sin = (struct sockaddr_in *) sa;
319 sin = (struct sockaddr_in *) ma;
327 struct sockaddr_dl *sli;
330 sli = (struct sockaddr_dl *) sa;
331 printf("link %.*s ", sli->sdl_nlen, sli->sdl_data);
332 for (i = 0; i < sli->sdl_alen; i++) {
335 printf("%x", ((unsigned char *) LLADDR(sli))[i]);
340 printf("af%d", sa->sa_family);
345 bootpboot_p_rtentry(struct rtentry *rt)
348 bootpboot_p_sa(rt_key(rt), rt_mask(rt));
350 bootpboot_p_sa(rt->rt_gateway, NULL);
352 printf("flags %x", (unsigned short) rt->rt_flags);
353 printf(" %d", (int) rt->rt_expire);
354 printf(" %s\n", rt->rt_ifp->if_xname);
358 bootpboot_p_tree(struct radix_node *rn)
362 if (rn->rn_bit < 0) {
363 if ((rn->rn_flags & RNF_ROOT) != 0) {
365 bootpboot_p_rtentry((struct rtentry *) rn);
367 rn = rn->rn_dupedkey;
369 bootpboot_p_tree(rn->rn_left);
370 bootpboot_p_tree(rn->rn_right);
377 bootpboot_p_rtlist(void)
380 struct rib_head *rnh;
382 printf("Routing table:\n");
383 rnh = rt_tables_get_rnh(0, AF_INET);
386 RIB_RLOCK(rnh); /* could sleep XXX */
387 bootpboot_p_tree(rnh->rnh_treetop);
392 bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
395 printf("%s flags %x, addr ",
396 ifp->if_xname, ifp->if_flags);
397 print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
398 printf(", broadcast ");
399 print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
400 printf(", netmask ");
401 print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
406 bootpboot_p_iflist(void)
411 printf("Interface list:\n");
413 for (ifp = CK_STAILQ_FIRST(&V_ifnet);
415 ifp = CK_STAILQ_NEXT(ifp, if_link)) {
416 for (ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
418 ifa = CK_STAILQ_NEXT(ifa, ifa_link))
419 if (ifa->ifa_addr->sa_family == AF_INET)
420 bootpboot_p_if(ifp, ifa);
424 #endif /* defined(BOOTP_DEBUG) */
427 clear_sinaddr(struct sockaddr_in *sin)
430 bzero(sin, sizeof(*sin));
431 sin->sin_len = sizeof(*sin);
432 sin->sin_family = AF_INET;
433 sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
438 allocifctx(struct bootpc_globalcontext *gctx)
440 struct bootpc_ifcontext *ifctx;
442 ifctx = malloc(sizeof(*ifctx), M_TEMP, M_WAITOK | M_ZERO);
443 ifctx->xid = gctx->xid;
445 ifctx->state = IF_BOOTP_UNRESOLVED;
447 ifctx->state = IF_DHCP_UNRESOLVED;
450 STAILQ_INSERT_TAIL(&gctx->interfaces, ifctx, next);
454 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
457 if (ifctx->state == IF_BOOTP_RESOLVED ||
458 ifctx->state == IF_DHCP_RESOLVED)
464 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
467 if (ifctx->state == IF_BOOTP_UNRESOLVED ||
468 ifctx->state == IF_DHCP_UNRESOLVED)
474 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
477 if (ifctx->state == IF_BOOTP_FAILED ||
478 ifctx->state == IF_DHCP_FAILED)
484 bootpc_received(struct bootpc_globalcontext *gctx,
485 struct bootpc_ifcontext *ifctx)
487 unsigned char dhcpreplytype;
491 * Need timeout for fallback to less
492 * desirable alternative.
495 /* This call used for the side effect (badopt flag) */
496 (void) bootpc_tag(&gctx->tmptag, &gctx->reply,
500 /* If packet is invalid, ignore it */
501 if (gctx->tmptag.badopt != 0)
504 p = bootpc_tag(&gctx->tmptag, &gctx->reply,
505 gctx->replylen, TAG_DHCP_MSGTYPE);
509 dhcpreplytype = DHCP_NOMSG;
511 switch (ifctx->dhcpquerytype) {
513 if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */
514 #ifndef BOOTP_FORCE_DHCP
515 && dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */
521 if (dhcpreplytype != DHCP_ACK)
527 /* Ignore packet unless it gives us a root tag we didn't have */
529 if ((ifctx->state == IF_BOOTP_RESOLVED ||
530 (ifctx->dhcpquerytype == DHCP_DISCOVER &&
531 (ifctx->state == IF_DHCP_OFFERED ||
532 ifctx->state == IF_DHCP_RESOLVED))) &&
533 (bootpc_tag(&gctx->tmptag, &ifctx->reply,
536 bootpc_tag(&gctx->tmptag, &gctx->reply,
541 bcopy(&gctx->reply, &ifctx->reply, gctx->replylen);
542 ifctx->replylen = gctx->replylen;
544 /* XXX: Only reset if 'perfect' response */
545 if (ifctx->state == IF_BOOTP_UNRESOLVED)
546 ifctx->state = IF_BOOTP_RESOLVED;
547 else if (ifctx->state == IF_DHCP_UNRESOLVED &&
548 ifctx->dhcpquerytype == DHCP_DISCOVER) {
549 if (dhcpreplytype == DHCP_OFFER)
550 ifctx->state = IF_DHCP_OFFERED;
552 ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */
553 } else if (ifctx->state == IF_DHCP_OFFERED &&
554 ifctx->dhcpquerytype == DHCP_REQUEST)
555 ifctx->state = IF_DHCP_RESOLVED;
558 if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
559 ifctx->state != IF_BOOTP_RESOLVED) {
560 p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
561 ifctx->replylen, TAG_DHCP_SERVERID);
562 if (p != NULL && gctx->tmptag.taglen == 4) {
563 memcpy(&ifctx->dhcpserver, p, 4);
564 ifctx->gotdhcpserver = 1;
566 ifctx->gotdhcpserver = 0;
570 ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
573 ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
575 TAG_ROUTERS) != NULL);
576 ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
578 TAG_SUBNETMASK) != NULL);
583 bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
585 struct sockaddr_in *sin, dst;
589 int error, on, rcvflg, timo, len;
593 struct bootpc_ifcontext *ifctx;
601 bzero(&sopt, sizeof(sopt));
602 sopt.sopt_dir = SOPT_SET;
603 sopt.sopt_level = SOL_SOCKET;
604 sopt.sopt_name = SO_RCVTIMEO;
606 sopt.sopt_valsize = sizeof tv;
608 error = sosetopt(bootp_so, &sopt);
616 sopt.sopt_name = SO_BROADCAST;
618 sopt.sopt_valsize = sizeof on;
620 error = sosetopt(bootp_so, &sopt);
629 sopt.sopt_name = SO_DONTROUTE;
631 sopt.sopt_valsize = sizeof on;
633 error = sosetopt(bootp_so, &sopt);
638 * Bind the local endpoint to a bootp client port.
642 sin->sin_port = htons(IPPORT_BOOTPC);
643 error = sobind(bootp_so, (struct sockaddr *)sin, td);
645 printf("bind failed\n");
650 * Setup socket address for the server.
654 sin->sin_addr.s_addr = INADDR_BROADCAST;
655 sin->sin_port = htons(IPPORT_BOOTPS);
658 * Send it, repeatedly, until a reply is received,
659 * but delay each re-send by an increasing amount.
660 * If the delay hits the maximum, start complaining.
669 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
670 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
671 bootpc_tag(&gctx->tmptag, &ifctx->reply,
677 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
678 struct in_aliasreq *ifra = &ifctx->iareq;
679 sin = (struct sockaddr_in *)&ifra->ifra_mask;
681 ifctx->outstanding = 0;
682 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
686 if (bootpc_ifctx_isfailed(ifctx) != 0)
690 ifctx->outstanding = 1;
692 /* Proceed to next step in DHCP negotiation */
693 if ((ifctx->state == IF_DHCP_OFFERED &&
694 ifctx->dhcpquerytype != DHCP_REQUEST) ||
695 (ifctx->state == IF_DHCP_UNRESOLVED &&
696 ifctx->dhcpquerytype != DHCP_DISCOVER) ||
697 (ifctx->state == IF_BOOTP_UNRESOLVED &&
698 ifctx->dhcpquerytype != DHCP_NOMSG)) {
700 bootpc_compose_query(ifctx, td);
703 /* Send BOOTP request (or re-send). */
705 if (ifctx->sentmsg == 0) {
706 switch(ifctx->dhcpquerytype) {
718 printf("Sending %s packet from "
719 "interface %s (%*D)\n",
721 ifctx->ireq.ifr_name,
722 ifctx->sdl->sdl_alen,
723 (unsigned char *) LLADDR(ifctx->sdl),
728 aio.iov_base = (caddr_t) &ifctx->call;
729 aio.iov_len = sizeof(ifctx->call);
733 auio.uio_segflg = UIO_SYSSPACE;
734 auio.uio_rw = UIO_WRITE;
736 auio.uio_resid = sizeof(ifctx->call);
739 /* Set netmask to 0.0.0.0 */
741 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
744 panic("%s: SIOCAIFADDR, error=%d", __func__,
747 error = sosend(bootp_so, (struct sockaddr *) &dst,
748 &auio, NULL, NULL, 0, td);
750 printf("%s: sosend: %d state %08x\n", __func__,
751 error, (int )bootp_so->so_state);
753 /* Set netmask to 255.0.0.0 */
754 sin->sin_addr.s_addr = htonl(IN_CLASSA_NET);
755 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
758 panic("%s: SIOCAIFADDR, error=%d", __func__,
762 if (outstanding == 0 &&
763 (rtimo == 0 || time_second >= rtimo)) {
768 /* Determine new timeout. */
769 if (timo < MAX_RESEND_DELAY)
772 printf("DHCP/BOOTP timeout for server ");
773 print_sin_addr(&dst);
778 * Wait for up to timo seconds for a reply.
779 * The socket receive timeout was set to 1 second.
781 atimo = timo + time_second;
782 while (time_second < atimo) {
783 aio.iov_base = (caddr_t) &gctx->reply;
784 aio.iov_len = sizeof(gctx->reply);
788 auio.uio_segflg = UIO_SYSSPACE;
789 auio.uio_rw = UIO_READ;
791 auio.uio_resid = sizeof(gctx->reply);
795 error = soreceive(bootp_so, NULL, &auio,
796 NULL, NULL, &rcvflg);
797 gctx->secs = time_second - gctx->starttime;
798 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
799 if (bootpc_ifctx_isresolved(ifctx) != 0 ||
800 bootpc_ifctx_isfailed(ifctx) != 0)
803 ifctx->call.secs = htons(gctx->secs);
805 if (error == EWOULDBLOCK)
809 len = sizeof(gctx->reply) - auio.uio_resid;
811 /* Do we have the required number of bytes ? */
812 if (len < BOOTP_MIN_LEN)
814 gctx->replylen = len;
817 if (gctx->reply.op != BOOTP_REPLY)
820 /* Is this an answer to our query */
821 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
822 if (gctx->reply.xid != ifctx->call.xid)
825 /* Same HW address size ? */
826 if (gctx->reply.hlen != ifctx->call.hlen)
829 /* Correct HW address ? */
830 if (bcmp(gctx->reply.chaddr,
832 ifctx->call.hlen) != 0)
839 s = bootpc_tag(&gctx->tmptag,
852 s = "DHCP (unexpected)";
858 printf("Received %s packet"
861 ifctx->ireq.ifr_name);
862 print_in_addr(gctx->reply.siaddr);
863 if (gctx->reply.giaddr.s_addr !=
866 print_in_addr(gctx->reply.giaddr);
868 if (bootpc_received(gctx, ifctx) != 0) {
869 printf(" (accepted)");
870 if (ifctx->outstanding) {
871 ifctx->outstanding = 0;
874 /* Network settle delay */
875 if (outstanding == 0)
876 atimo = time_second +
879 printf(" (ignored)");
880 if (ifctx->gotrootpath ||
881 gctx->any_root_overrides) {
883 rtimo = time_second +
885 if (ifctx->gotrootpath)
886 printf(" (got root path)");
892 if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
895 /* Force a retry if halfway in DHCP negotiation */
897 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
898 if (ifctx->state == IF_DHCP_OFFERED) {
899 if (ifctx->dhcpquerytype == DHCP_DISCOVER)
902 ifctx->state = IF_DHCP_UNRESOLVED;
908 if (gotrootpath != 0) {
909 gctx->gotrootpath = gotrootpath;
910 if (rtimo != 0 && time_second >= rtimo)
913 } /* forever send/receive */
916 * XXX: These are errors of varying seriousness being silently
920 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
921 if (bootpc_ifctx_isresolved(ifctx) == 0) {
922 printf("%s timeout for interface %s\n",
923 ifctx->dhcpquerytype != DHCP_NOMSG ?
925 ifctx->ireq.ifr_name);
928 if (gctx->gotrootpath != 0) {
930 printf("Got a root path, ignoring remaining timeout\n");
935 #ifndef BOOTP_NFSROOT
936 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
937 if (bootpc_ifctx_isresolved(ifctx) != 0) {
949 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
952 struct in_aliasreq *ifra;
953 struct sockaddr_in *sin;
957 ifra = &ifctx->iareq;
960 * Bring up the interface.
962 * Get the old interface flags and or IFF_UP into them; if
963 * IFF_UP set blindly, interface selection can be clobbered.
965 error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
967 panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
968 ifr->ifr_flags |= IFF_UP;
969 error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
971 panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
974 * Do enough of ifconfig(8) so that the chosen interface
975 * can talk to the servers. Set address to 0.0.0.0/8 and
976 * broadcast address to local broadcast.
978 sin = (struct sockaddr_in *)&ifra->ifra_addr;
980 sin = (struct sockaddr_in *)&ifra->ifra_mask;
982 sin->sin_addr.s_addr = htonl(IN_CLASSA_NET);
983 sin = (struct sockaddr_in *)&ifra->ifra_broadaddr;
985 sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
986 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
988 panic("%s: SIOCAIFADDR, error=%d", __func__, error);
992 bootpc_shutdown_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
995 struct sockaddr_in *sin;
1000 printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
1001 error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
1003 panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
1004 ifr->ifr_flags &= ~IFF_UP;
1005 error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
1007 panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
1009 sin = (struct sockaddr_in *) &ifr->ifr_addr;
1011 error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
1013 panic("%s: SIOCDIFADDR, error=%d", __func__, error);
1017 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
1018 struct bootpc_globalcontext *gctx, struct thread *td)
1021 struct sockaddr_in *sin;
1023 struct in_aliasreq *ifra;
1024 struct sockaddr_in *myaddr;
1025 struct sockaddr_in *netmask;
1028 ifra = &ifctx->iareq;
1029 myaddr = &ifctx->myaddr;
1030 netmask = &ifctx->netmask;
1032 if (bootpc_ifctx_isresolved(ifctx) == 0) {
1033 /* Shutdown interfaces where BOOTP failed */
1034 bootpc_shutdown_interface(ifctx, td);
1038 printf("Adjusted interface %s", ifctx->ireq.ifr_name);
1040 /* Do BOOTP interface options */
1041 if (ifctx->mtu != 0) {
1042 printf(" (MTU=%d%s)", ifctx->mtu,
1043 (ifctx->mtu > 1514) ? "/JUMBO" : "");
1044 ifr->ifr_mtu = ifctx->mtu;
1045 error = ifioctl(bootp_so, SIOCSIFMTU, (caddr_t) ifr, td);
1047 panic("%s: SIOCSIFMTU, error=%d", __func__, error);
1052 * Do enough of ifconfig(8) so that the chosen interface
1053 * can talk to the servers. (just set the address)
1055 sin = (struct sockaddr_in *) &ifr->ifr_addr;
1057 error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
1059 panic("%s: SIOCDIFADDR, error=%d", __func__, error);
1061 bcopy(myaddr, &ifra->ifra_addr, sizeof(*myaddr));
1062 bcopy(netmask, &ifra->ifra_mask, sizeof(*netmask));
1063 clear_sinaddr(&ifra->ifra_broadaddr);
1064 ifra->ifra_broadaddr.sin_addr.s_addr = myaddr->sin_addr.s_addr |
1065 ~netmask->sin_addr.s_addr;
1067 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
1069 panic("%s: SIOCAIFADDR, error=%d", __func__, error);
1073 bootpc_add_default_route(struct bootpc_ifcontext *ifctx)
1076 struct sockaddr_in defdst;
1077 struct sockaddr_in defmask;
1079 if (ifctx->gw.sin_addr.s_addr == htonl(INADDR_ANY))
1082 clear_sinaddr(&defdst);
1083 clear_sinaddr(&defmask);
1085 error = rtrequest_fib(RTM_ADD, (struct sockaddr *)&defdst,
1086 (struct sockaddr *) &ifctx->gw, (struct sockaddr *)&defmask,
1087 (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL, RT_DEFAULT_FIB);
1089 printf("%s: RTM_ADD, error=%d\n", __func__, error);
1094 bootpc_remove_default_route(struct bootpc_ifcontext *ifctx)
1097 struct sockaddr_in defdst;
1098 struct sockaddr_in defmask;
1100 if (ifctx->gw.sin_addr.s_addr == htonl(INADDR_ANY))
1103 clear_sinaddr(&defdst);
1104 clear_sinaddr(&defmask);
1106 error = rtrequest_fib(RTM_DELETE, (struct sockaddr *)&defdst,
1107 (struct sockaddr *) &ifctx->gw, (struct sockaddr *)&defmask,
1108 (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL, RT_DEFAULT_FIB);
1110 printf("%s: RTM_DELETE, error=%d\n", __func__, error);
1115 setfs(struct sockaddr_in *addr, char *path, char *p,
1116 const struct in_addr *siaddr)
1119 if (getip(&p, &addr->sin_addr) == 0) {
1120 if (siaddr != NULL && *p == '/')
1121 bcopy(siaddr, &addr->sin_addr, sizeof(struct in_addr));
1130 addr->sin_len = sizeof(struct sockaddr_in);
1131 addr->sin_family = AF_INET;
1133 strlcpy(path, p, MNAMELEN);
1138 getip(char **ptr, struct in_addr *addr)
1146 if (((val = getdec(&p)) < 0) || (val > 255))
1152 if (((val = getdec(&p)) < 0) || (val > 255))
1158 if (((val = getdec(&p)) < 0) || (val > 255))
1164 if (((val = getdec(&p)) < 0) || (val > 255))
1168 addr->s_addr = htonl(ip);
1181 if ((*p < '0') || (*p > '9'))
1183 while ((*p >= '0') && (*p <= '9')) {
1184 ret = ret * 10 + (*p - '0');
1192 mountopts(struct nfs_args *args, char *p)
1194 args->version = NFS_ARGSVERSION;
1195 args->rsize = BOOTP_BLOCKSIZE;
1196 args->wsize = BOOTP_BLOCKSIZE;
1197 args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
1198 args->sotype = SOCK_DGRAM;
1200 nfs_parse_options(p, args);
1204 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
1210 alignedlen = ( len + 3 ) & ~3;
1212 if (m->m_len < alignedlen) {
1213 m = m_pullup(m, alignedlen);
1219 bcopy(mtod(m, u_char *), buf, len);
1220 m_adj(m, alignedlen);
1226 xdr_int_decode(struct mbuf **mptr, int *iptr)
1230 if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
1232 *iptr = fxdr_unsigned(u_int32_t, i);
1237 print_sin_addr(struct sockaddr_in *sin)
1240 print_in_addr(sin->sin_addr);
1244 print_in_addr(struct in_addr addr)
1248 ip = ntohl(addr.s_addr);
1249 printf("%d.%d.%d.%d",
1250 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1254 bootpc_compose_query(struct bootpc_ifcontext *ifctx, struct thread *td)
1256 unsigned char *vendp;
1257 unsigned char vendor_client[64];
1259 uint8_t vendor_client_len;
1261 ifctx->gotrootpath = 0;
1263 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1266 ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */
1267 ifctx->call.htype = 1; /* 10mb ethernet */
1268 ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1269 ifctx->call.hops = 0;
1270 if (bootpc_ifctx_isunresolved(ifctx) != 0)
1272 ifctx->call.xid = txdr_unsigned(ifctx->xid);
1273 bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1275 vendp = ifctx->call.vend;
1276 *vendp++ = 99; /* RFC1048 cookie */
1280 *vendp++ = TAG_MAXMSGSIZE;
1282 *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1283 *vendp++ = sizeof(struct bootp_packet) & 255;
1285 snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1286 ostype, MACHINE, osrelease);
1287 vendor_client_len = strlen(vendor_client);
1288 *vendp++ = TAG_VENDOR_INDENTIFIER;
1289 *vendp++ = vendor_client_len;
1290 memcpy(vendp, vendor_client, vendor_client_len);
1291 vendp += vendor_client_len;
1292 ifctx->dhcpquerytype = DHCP_NOMSG;
1293 switch (ifctx->state) {
1294 case IF_DHCP_UNRESOLVED:
1295 *vendp++ = TAG_DHCP_MSGTYPE;
1297 *vendp++ = DHCP_DISCOVER;
1298 ifctx->dhcpquerytype = DHCP_DISCOVER;
1299 ifctx->gotdhcpserver = 0;
1301 case IF_DHCP_OFFERED:
1302 *vendp++ = TAG_DHCP_MSGTYPE;
1304 *vendp++ = DHCP_REQUEST;
1305 ifctx->dhcpquerytype = DHCP_REQUEST;
1306 *vendp++ = TAG_DHCP_REQ_ADDR;
1308 memcpy(vendp, &ifctx->reply.yiaddr, 4);
1310 if (ifctx->gotdhcpserver != 0) {
1311 *vendp++ = TAG_DHCP_SERVERID;
1313 memcpy(vendp, &ifctx->dhcpserver, 4);
1316 *vendp++ = TAG_DHCP_LEASETIME;
1318 leasetime = htonl(300);
1319 memcpy(vendp, &leasetime, 4);
1327 ifctx->call.secs = 0;
1328 ifctx->call.flags = htons(0x8000); /* We need a broadcast answer */
1332 bootpc_hascookie(struct bootp_packet *bp)
1335 return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1336 bp->vend[2] == 83 && bp->vend[3] == 99);
1340 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1341 unsigned char *start, int len, int tag)
1347 if (tctx->badtag != 0 || tctx->badopt != 0)
1355 if (code == TAG_PAD)
1357 if (code == TAG_END)
1359 if (j >= ej || j + *j + 1 > ej) {
1365 if (tctx->taglen + len > TAG_MAXLEN) {
1371 memcpy(tctx->buf + tctx->taglen,
1373 tctx->taglen += len;
1375 if (code == TAG_OVERLOAD)
1376 tctx->overload = *j;
1382 static unsigned char *
1383 bootpc_tag(struct bootpc_tagcontext *tctx,
1384 struct bootp_packet *bp, int len, int tag)
1392 if (bootpc_hascookie(bp) == 0)
1395 bootpc_tag_helper(tctx, &bp->vend[4],
1396 (unsigned char *) bp + len - &bp->vend[4], tag);
1398 if ((tctx->overload & OVERLOAD_FILE) != 0)
1399 bootpc_tag_helper(tctx,
1400 (unsigned char *) bp->file,
1403 if ((tctx->overload & OVERLOAD_SNAME) != 0)
1404 bootpc_tag_helper(tctx,
1405 (unsigned char *) bp->sname,
1409 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1411 tctx->buf[tctx->taglen] = '\0';
1416 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1417 struct bootpc_globalcontext *gctx)
1423 ifctx->gotnetmask = 0;
1425 clear_sinaddr(&ifctx->myaddr);
1426 clear_sinaddr(&ifctx->netmask);
1427 clear_sinaddr(&ifctx->gw);
1429 ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1431 ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1433 printf("%s at ", ifctx->ireq.ifr_name);
1434 print_sin_addr(&ifctx->myaddr);
1436 print_in_addr(ifctx->reply.siaddr);
1438 ifctx->gw.sin_addr = ifctx->reply.giaddr;
1439 if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1440 printf(" via gateway ");
1441 print_in_addr(ifctx->reply.giaddr);
1444 /* This call used for the side effect (overload flag) */
1445 (void) bootpc_tag(&gctx->tmptag,
1446 &ifctx->reply, ifctx->replylen, TAG_END);
1448 if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1449 if (ifctx->reply.sname[0] != '\0')
1450 printf(" server name %s", ifctx->reply.sname);
1451 if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1452 if (ifctx->reply.file[0] != '\0')
1453 printf(" boot file %s", ifctx->reply.file);
1457 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1460 if (gctx->tag.taglen != 4)
1461 panic("bootpc: subnet mask len is %d",
1463 bcopy(p, &ifctx->netmask.sin_addr, 4);
1464 ifctx->gotnetmask = 1;
1465 printf("subnet mask ");
1466 print_sin_addr(&ifctx->netmask);
1470 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1474 if (gctx->tag.taglen % 4)
1475 panic("bootpc: Router Len is %d", gctx->tag.taglen);
1476 if (gctx->tag.taglen > 0) {
1477 bcopy(p, &ifctx->gw.sin_addr, 4);
1479 print_sin_addr(&ifctx->gw);
1487 * Choose a root filesystem. If a value is forced in the environment
1488 * and it contains "nfs:", use it unconditionally. Otherwise, if the
1489 * kernel is compiled with the ROOTDEVNAME option, then use it if:
1490 * - The server doesn't provide a pathname.
1491 * - The boothowto flags include RB_DFLTROOT (user said to override
1492 * the server value).
1495 if ((s = kern_getenv("vfs.root.mountfrom")) != NULL) {
1496 if ((p = strstr(s, "nfs:")) != NULL)
1497 p = strdup(p + 4, M_TEMP);
1501 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1504 ifctx->gotrootpath = 1;
1507 if ((p == NULL || (boothowto & RB_DFLTROOT) != 0) &&
1508 (p = strstr(ROOTDEVNAME, "nfs:")) != NULL) {
1513 if (gctx->setrootfs != NULL) {
1514 printf("rootfs %s (ignored) ", p);
1515 } else if (setfs(&nd->root_saddr,
1516 nd->root_hostnam, p, &ifctx->reply.siaddr)) {
1518 printf("root_server ");
1519 print_sin_addr(&nd->root_saddr);
1522 printf("rootfs %s ", p);
1523 gctx->gotrootpath = 1;
1524 gctx->setrootfs = ifctx;
1526 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1530 mountopts(&nd->root_args, p);
1531 printf("rootopts %s ", p);
1534 panic("Failed to set rootfs to %s", p);
1537 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1540 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1541 panic("bootpc: hostname >= %d bytes",
1543 if (gctx->sethostname != NULL) {
1544 printf("hostname %s (ignored) ", p);
1546 strcpy(nd->my_hostnam, p);
1547 mtx_lock(&prison0.pr_mtx);
1548 strcpy(prison0.pr_hostname, p);
1549 mtx_unlock(&prison0.pr_mtx);
1550 printf("hostname %s ", p);
1551 gctx->sethostname = ifctx;
1554 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1556 if (p != NULL) { /* store in a sysctl variable */
1557 int i, l = sizeof(bootp_cookie) - 1;
1558 for (i = 0; i < l && p[i] != '\0'; i++)
1559 bootp_cookie[i] = p[i];
1563 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1566 ifctx->mtu = be16dec(p);
1571 if (ifctx->gotnetmask == 0) {
1572 if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1573 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1574 else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1575 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1577 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1584 struct bootpc_ifcontext *ifctx; /* Interface BOOTP contexts */
1585 struct bootpc_globalcontext *gctx; /* Global BOOTP context */
1587 struct sockaddr_dl *sdl;
1590 #ifndef BOOTP_WIRED_TO
1593 struct nfsv3_diskless *nd;
1598 timeout = BOOTP_IFACE_WAIT_TIMEOUT * hz;
1601 nd = &nfsv3_diskless;
1605 * If already filled in, don't touch it here
1607 if (nfs_diskless_valid != 0)
1610 gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK | M_ZERO);
1611 STAILQ_INIT(&gctx->interfaces);
1612 gctx->xid = ~0xFFFF;
1613 gctx->starttime = time_second;
1616 * If ROOTDEVNAME is defined or vfs.root.mountfrom is set then we have
1617 * root-path overrides that can potentially let us boot even if we don't
1618 * get a root path from the server, so we can treat that as a non-error.
1621 gctx->any_root_overrides = 1;
1623 gctx->any_root_overrides = testenv("vfs.root.mountfrom");
1627 * Find a network interface.
1629 CURVNET_SET(TD_TO_VNET(td));
1630 #ifdef BOOTP_WIRED_TO
1631 printf("%s: wired to interface '%s'\n", __func__,
1632 __XSTRING(BOOTP_WIRED_TO));
1636 * Preallocate interface context storage, if another interface
1637 * attaches and wins the race, it won't be eligible for bootp.
1641 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1642 if ((ifp->if_flags &
1643 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1646 switch (ifp->if_alloctype) {
1656 panic("%s: no eligible interfaces", __func__);
1657 for (; ifcnt > 0; ifcnt--)
1662 ifctx = STAILQ_FIRST(&gctx->interfaces);
1664 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1667 #ifdef BOOTP_WIRED_TO
1668 if (strcmp(ifp->if_xname, __XSTRING(BOOTP_WIRED_TO)) != 0)
1671 if ((ifp->if_flags &
1672 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1675 switch (ifp->if_alloctype) {
1682 strlcpy(ifctx->ireq.ifr_name, ifp->if_xname,
1683 sizeof(ifctx->ireq.ifr_name));
1686 /* Get HW address */
1688 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1689 if (ifa->ifa_addr->sa_family == AF_LINK) {
1690 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1691 if (sdl->sdl_type == IFT_ETHER)
1695 panic("bootpc: Unable to find HW address for %s",
1696 ifctx->ireq.ifr_name);
1699 ifctx = STAILQ_NEXT(ifctx, next);
1704 if (STAILQ_EMPTY(&gctx->interfaces) ||
1705 STAILQ_FIRST(&gctx->interfaces)->ifp == NULL) {
1707 pause("bootpc", delay);
1711 #ifdef BOOTP_WIRED_TO
1712 panic("%s: Could not find interface specified "
1713 "by BOOTP_WIRED_TO: "
1714 __XSTRING(BOOTP_WIRED_TO), __func__);
1716 panic("%s: no suitable interface", __func__);
1720 error = socreate(AF_INET, &bootp_so, SOCK_DGRAM, 0, td->td_ucred, td);
1722 panic("%s: socreate, error=%d", __func__, error);
1724 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1725 bootpc_fakeup_interface(ifctx, td);
1727 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1728 bootpc_compose_query(ifctx, td);
1730 error = bootpc_call(gctx, td);
1732 printf("BOOTP call failed\n");
1735 mountopts(&nd->root_args, NULL);
1737 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1738 if (bootpc_ifctx_isresolved(ifctx) != 0)
1739 bootpc_decode_reply(nd, ifctx, gctx);
1741 #ifdef BOOTP_NFSROOT
1742 if (gctx->gotrootpath == 0 && gctx->any_root_overrides == 0)
1743 panic("bootpc: No root path offered");
1746 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1747 bootpc_adjust_interface(ifctx, gctx, td);
1751 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1752 if (ifctx->gotrootpath != 0)
1754 if (ifctx == NULL) {
1755 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1756 if (bootpc_ifctx_isresolved(ifctx) != 0)
1762 if (gctx->gotrootpath != 0) {
1764 kern_setenv("boot.netif.name", ifctx->ifp->if_xname);
1766 bootpc_add_default_route(ifctx);
1767 error = md_mount(&nd->root_saddr, nd->root_hostnam,
1768 nd->root_fh, &nd->root_fhsize,
1769 &nd->root_args, td);
1770 bootpc_remove_default_route(ifctx);
1772 if (gctx->any_root_overrides == 0)
1773 panic("nfs_boot: mount root, error=%d", error);
1777 rootdevnames[0] = "nfs:";
1778 nfs_diskless_valid = 3;
1781 strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1782 bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1783 bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1784 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1785 ifctx->myaddr.sin_addr.s_addr |
1786 ~ ifctx->netmask.sin_addr.s_addr;
1787 bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1788 bcopy(&ifctx->gw, &nd->mygateway, sizeof(ifctx->gw));
1791 while((ifctx = STAILQ_FIRST(&gctx->interfaces)) != NULL) {
1792 STAILQ_REMOVE_HEAD(&gctx->interfaces, next);
1793 free(ifctx, M_TEMP);
1800 * Given a server pathname, get an NFS file handle.
1801 * Also, sets sin->sin_port to the NFS service port.
1804 md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp, int *fhsizep,
1805 struct nfs_args *args, struct thread *td)
1813 #define RPCPROG_MNT 100005
1814 #define RPCMNT_VER1 1
1815 #define RPCMNT_VER3 3
1816 #define RPCMNT_MOUNT 1
1817 #define AUTH_SYS 1 /* unix style (uid, gids) */
1818 #define AUTH_UNIX AUTH_SYS
1820 /* XXX honor v2/v3 flags in args->flags? */
1822 /* First try NFS v3 */
1823 /* Get port number for MOUNTD. */
1824 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1825 &mdsin->sin_port, td);
1827 m = xdr_string_encode(path, strlen(path));
1829 /* Do RPC to mountd. */
1830 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1831 RPCMNT_MOUNT, &m, NULL, td);
1834 args->flags |= NFSMNT_NFSV3;
1837 /* Fallback to NFS v2 */
1839 /* Get port number for MOUNTD. */
1840 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1841 &mdsin->sin_port, td);
1845 m = xdr_string_encode(path, strlen(path));
1847 /* Do RPC to mountd. */
1848 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1849 RPCMNT_MOUNT, &m, NULL, td);
1851 return error; /* message already freed */
1857 if (xdr_int_decode(&m, &error) != 0 || error != 0)
1860 if ((args->flags & NFSMNT_NFSV3) != 0) {
1861 if (xdr_int_decode(&m, fhsizep) != 0 ||
1862 *fhsizep > NFSX_V3FHMAX ||
1866 *fhsizep = NFSX_V2FH;
1868 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
1871 if (args->flags & NFSMNT_NFSV3) {
1872 if (xdr_int_decode(&m, &authcount) != 0)
1875 if (authcount < 0 || authcount > 100)
1877 while (authcount > 0) {
1878 if (xdr_int_decode(&m, &authver) != 0)
1880 if (authver == AUTH_UNIX)
1884 if (authunixok == 0)
1888 /* Set port number for NFS use. */
1889 error = krpc_portmap(mdsin, NFS_PROG,
1891 NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
1892 &mdsin->sin_port, td);
1904 SYSINIT(bootp_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, bootpc_init, NULL);