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 #define IN_HISTORICAL_NETS /* include class masks */
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
50 #include "opt_bootp.h"
52 #include "opt_rootdevname.h"
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/endian.h>
58 #include <sys/kernel.h>
59 #include <sys/sockio.h>
60 #include <sys/malloc.h>
61 #include <sys/mount.h>
64 #include <sys/reboot.h>
65 #include <sys/socket.h>
66 #include <sys/socketvar.h>
67 #include <sys/sysctl.h>
71 #include <net/if_var.h>
72 #include <net/route.h>
73 #include <net/route/route_ctl.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_iflist(void);
269 static int bootpc_call(struct bootpc_globalcontext *gctx,
272 static void bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
275 static void bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
276 struct bootpc_globalcontext *gctx, struct thread *td);
278 static void bootpc_decode_reply(struct nfsv3_diskless *nd,
279 struct bootpc_ifcontext *ifctx,
280 struct bootpc_globalcontext *gctx);
282 static int bootpc_received(struct bootpc_globalcontext *gctx,
283 struct bootpc_ifcontext *ifctx);
285 static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
286 static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
287 static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
290 * In order to have multiple active interfaces with address 0.0.0.0
291 * and be able to send data to a selected interface, we first set
292 * mask to /8 on all interfaces, and temporarily set it to /0 when
298 bootpboot_p_ifa(void *ifp, struct ifaddr *ifa, u_int count __unused)
301 printf("%s flags %x, addr ",
302 if_name(ifp), if_getflags(ifp));
303 print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
304 printf(", broadcast ");
305 print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
306 printf(", netmask ");
307 print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
314 bootpboot_p_iflist(void)
316 struct epoch_tracker et;
320 printf("Interface list:\n");
322 for (ifp = if_iter_start(&iter); ifp != NULL; ifp = if_iter_next(&iter))
323 if_foreach_addr_type(ifp, AF_INET, bootpboot_p_ifa, ifp);
324 if_iter_finish(&iter);
327 #endif /* defined(BOOTP_DEBUG) */
330 clear_sinaddr(struct sockaddr_in *sin)
333 bzero(sin, sizeof(*sin));
334 sin->sin_len = sizeof(*sin);
335 sin->sin_family = AF_INET;
336 sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
341 allocifctx(struct bootpc_globalcontext *gctx)
343 struct bootpc_ifcontext *ifctx;
345 ifctx = malloc(sizeof(*ifctx), M_TEMP, M_WAITOK | M_ZERO);
346 ifctx->xid = gctx->xid;
348 ifctx->state = IF_BOOTP_UNRESOLVED;
350 ifctx->state = IF_DHCP_UNRESOLVED;
353 STAILQ_INSERT_TAIL(&gctx->interfaces, ifctx, next);
357 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
360 if (ifctx->state == IF_BOOTP_RESOLVED ||
361 ifctx->state == IF_DHCP_RESOLVED)
367 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
370 if (ifctx->state == IF_BOOTP_UNRESOLVED ||
371 ifctx->state == IF_DHCP_UNRESOLVED)
377 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
380 if (ifctx->state == IF_BOOTP_FAILED ||
381 ifctx->state == IF_DHCP_FAILED)
387 bootpc_received(struct bootpc_globalcontext *gctx,
388 struct bootpc_ifcontext *ifctx)
390 unsigned char dhcpreplytype;
394 * Need timeout for fallback to less
395 * desirable alternative.
398 /* This call used for the side effect (badopt flag) */
399 (void) bootpc_tag(&gctx->tmptag, &gctx->reply,
403 /* If packet is invalid, ignore it */
404 if (gctx->tmptag.badopt != 0)
407 p = bootpc_tag(&gctx->tmptag, &gctx->reply,
408 gctx->replylen, TAG_DHCP_MSGTYPE);
412 dhcpreplytype = DHCP_NOMSG;
414 switch (ifctx->dhcpquerytype) {
416 if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */
417 #ifndef BOOTP_FORCE_DHCP
418 && dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */
424 if (dhcpreplytype != DHCP_ACK)
430 /* Ignore packet unless it gives us a root tag we didn't have */
432 if ((ifctx->state == IF_BOOTP_RESOLVED ||
433 (ifctx->dhcpquerytype == DHCP_DISCOVER &&
434 (ifctx->state == IF_DHCP_OFFERED ||
435 ifctx->state == IF_DHCP_RESOLVED))) &&
436 (bootpc_tag(&gctx->tmptag, &ifctx->reply,
439 bootpc_tag(&gctx->tmptag, &gctx->reply,
444 bcopy(&gctx->reply, &ifctx->reply, gctx->replylen);
445 ifctx->replylen = gctx->replylen;
447 /* XXX: Only reset if 'perfect' response */
448 if (ifctx->state == IF_BOOTP_UNRESOLVED)
449 ifctx->state = IF_BOOTP_RESOLVED;
450 else if (ifctx->state == IF_DHCP_UNRESOLVED &&
451 ifctx->dhcpquerytype == DHCP_DISCOVER) {
452 if (dhcpreplytype == DHCP_OFFER)
453 ifctx->state = IF_DHCP_OFFERED;
455 ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */
456 } else if (ifctx->state == IF_DHCP_OFFERED &&
457 ifctx->dhcpquerytype == DHCP_REQUEST)
458 ifctx->state = IF_DHCP_RESOLVED;
460 if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
461 ifctx->state != IF_BOOTP_RESOLVED) {
462 p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
463 ifctx->replylen, TAG_DHCP_SERVERID);
464 if (p != NULL && gctx->tmptag.taglen == 4) {
465 memcpy(&ifctx->dhcpserver, p, 4);
466 ifctx->gotdhcpserver = 1;
468 ifctx->gotdhcpserver = 0;
472 ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
475 ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
477 TAG_ROUTERS) != NULL);
478 ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
480 TAG_SUBNETMASK) != NULL);
485 bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
487 struct sockaddr_in *sin, dst;
491 int error, on, rcvflg, timo, len;
495 struct bootpc_ifcontext *ifctx;
503 bzero(&sopt, sizeof(sopt));
504 sopt.sopt_dir = SOPT_SET;
505 sopt.sopt_level = SOL_SOCKET;
506 sopt.sopt_name = SO_RCVTIMEO;
508 sopt.sopt_valsize = sizeof tv;
510 error = sosetopt(bootp_so, &sopt);
518 sopt.sopt_name = SO_BROADCAST;
520 sopt.sopt_valsize = sizeof on;
522 error = sosetopt(bootp_so, &sopt);
531 sopt.sopt_name = SO_DONTROUTE;
533 sopt.sopt_valsize = sizeof on;
535 error = sosetopt(bootp_so, &sopt);
540 * Bind the local endpoint to a bootp client port.
544 sin->sin_port = htons(IPPORT_BOOTPC);
545 error = sobind(bootp_so, (struct sockaddr *)sin, td);
547 printf("bind failed\n");
552 * Setup socket address for the server.
556 sin->sin_addr.s_addr = INADDR_BROADCAST;
557 sin->sin_port = htons(IPPORT_BOOTPS);
560 * Send it, repeatedly, until a reply is received,
561 * but delay each re-send by an increasing amount.
562 * If the delay hits the maximum, start complaining.
570 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
571 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
572 bootpc_tag(&gctx->tmptag, &ifctx->reply,
578 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
579 struct in_aliasreq *ifra = &ifctx->iareq;
580 sin = (struct sockaddr_in *)&ifra->ifra_mask;
582 ifctx->outstanding = 0;
583 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
587 if (bootpc_ifctx_isfailed(ifctx) != 0)
591 ifctx->outstanding = 1;
593 /* Proceed to next step in DHCP negotiation */
594 if ((ifctx->state == IF_DHCP_OFFERED &&
595 ifctx->dhcpquerytype != DHCP_REQUEST) ||
596 (ifctx->state == IF_DHCP_UNRESOLVED &&
597 ifctx->dhcpquerytype != DHCP_DISCOVER) ||
598 (ifctx->state == IF_BOOTP_UNRESOLVED &&
599 ifctx->dhcpquerytype != DHCP_NOMSG)) {
601 bootpc_compose_query(ifctx, td);
604 /* Send BOOTP request (or re-send). */
606 if (ifctx->sentmsg == 0) {
607 switch(ifctx->dhcpquerytype) {
619 printf("Sending %s packet from "
620 "interface %s (%*D)\n",
622 ifctx->ireq.ifr_name,
623 ifctx->sdl->sdl_alen,
624 (unsigned char *) LLADDR(ifctx->sdl),
629 aio.iov_base = (caddr_t) &ifctx->call;
630 aio.iov_len = sizeof(ifctx->call);
634 auio.uio_segflg = UIO_SYSSPACE;
635 auio.uio_rw = UIO_WRITE;
637 auio.uio_resid = sizeof(ifctx->call);
640 /* Set netmask to 0.0.0.0 */
642 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
645 panic("%s: SIOCAIFADDR, error=%d", __func__,
648 error = sosend(bootp_so, (struct sockaddr *) &dst,
649 &auio, NULL, NULL, 0, td);
651 printf("%s: sosend: %d state %08x\n", __func__,
652 error, (int )bootp_so->so_state);
654 /* Set netmask to 255.0.0.0 */
655 sin->sin_addr.s_addr = htonl(0xff000000);
656 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
659 panic("%s: SIOCAIFADDR, error=%d", __func__,
663 if (outstanding == 0 &&
664 (rtimo == 0 || time_second >= rtimo)) {
669 /* Determine new timeout. */
670 if (timo < MAX_RESEND_DELAY)
673 printf("DHCP/BOOTP timeout for server ");
674 print_sin_addr(&dst);
679 * Wait for up to timo seconds for a reply.
680 * The socket receive timeout was set to 1 second.
682 atimo = timo + time_second;
683 while (time_second < atimo) {
684 aio.iov_base = (caddr_t) &gctx->reply;
685 aio.iov_len = sizeof(gctx->reply);
689 auio.uio_segflg = UIO_SYSSPACE;
690 auio.uio_rw = UIO_READ;
692 auio.uio_resid = sizeof(gctx->reply);
696 error = soreceive(bootp_so, NULL, &auio,
697 NULL, NULL, &rcvflg);
698 gctx->secs = time_second - gctx->starttime;
699 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
700 if (bootpc_ifctx_isresolved(ifctx) != 0 ||
701 bootpc_ifctx_isfailed(ifctx) != 0)
704 ifctx->call.secs = htons(gctx->secs);
706 if (error == EWOULDBLOCK)
710 len = sizeof(gctx->reply) - auio.uio_resid;
712 /* Do we have the required number of bytes ? */
713 if (len < BOOTP_MIN_LEN)
715 gctx->replylen = len;
718 if (gctx->reply.op != BOOTP_REPLY)
721 /* Is this an answer to our query */
722 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
723 if (gctx->reply.xid != ifctx->call.xid)
726 /* Same HW address size ? */
727 if (gctx->reply.hlen != ifctx->call.hlen)
730 /* Correct HW address ? */
731 if (bcmp(gctx->reply.chaddr,
733 ifctx->call.hlen) != 0)
740 s = bootpc_tag(&gctx->tmptag,
753 s = "DHCP (unexpected)";
759 printf("Received %s packet"
762 ifctx->ireq.ifr_name);
763 print_in_addr(gctx->reply.siaddr);
764 if (gctx->reply.giaddr.s_addr !=
767 print_in_addr(gctx->reply.giaddr);
769 if (bootpc_received(gctx, ifctx) != 0) {
770 printf(" (accepted)");
771 if (ifctx->outstanding) {
772 ifctx->outstanding = 0;
775 /* Network settle delay */
776 if (outstanding == 0)
777 atimo = time_second +
780 printf(" (ignored)");
781 if (ifctx->gotrootpath ||
782 gctx->any_root_overrides) {
784 rtimo = time_second +
786 if (ifctx->gotrootpath)
787 printf(" (got root path)");
793 if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
796 /* Force a retry if halfway in DHCP negotiation */
798 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
799 if (ifctx->state == IF_DHCP_OFFERED) {
800 if (ifctx->dhcpquerytype == DHCP_DISCOVER)
803 ifctx->state = IF_DHCP_UNRESOLVED;
809 if (gotrootpath != 0) {
810 gctx->gotrootpath = gotrootpath;
811 if (rtimo != 0 && time_second >= rtimo)
814 } /* forever send/receive */
817 * XXX: These are errors of varying seriousness being silently
821 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
822 if (bootpc_ifctx_isresolved(ifctx) == 0) {
823 printf("%s timeout for interface %s\n",
824 ifctx->dhcpquerytype != DHCP_NOMSG ?
826 ifctx->ireq.ifr_name);
829 if (gctx->gotrootpath != 0) {
831 printf("Got a root path, ignoring remaining timeout\n");
836 #ifndef BOOTP_NFSROOT
837 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
838 if (bootpc_ifctx_isresolved(ifctx) != 0) {
850 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
853 struct in_aliasreq *ifra;
854 struct sockaddr_in *sin;
858 ifra = &ifctx->iareq;
861 * Bring up the interface.
863 * Get the old interface flags and or IFF_UP into them; if
864 * IFF_UP set blindly, interface selection can be clobbered.
866 error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
868 panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
869 ifr->ifr_flags |= IFF_UP;
870 error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
872 panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
875 * Do enough of ifconfig(8) so that the chosen interface
876 * can talk to the servers. Set address to 0.0.0.0/8 and
877 * broadcast address to local broadcast.
879 sin = (struct sockaddr_in *)&ifra->ifra_addr;
881 sin = (struct sockaddr_in *)&ifra->ifra_mask;
883 sin->sin_addr.s_addr = htonl(0xff000000);
884 sin = (struct sockaddr_in *)&ifra->ifra_broadaddr;
886 sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
887 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
889 panic("%s: SIOCAIFADDR, error=%d", __func__, error);
893 bootpc_shutdown_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
896 struct sockaddr_in *sin;
901 printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
902 error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
904 panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
905 ifr->ifr_flags &= ~IFF_UP;
906 error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
908 panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
910 sin = (struct sockaddr_in *) &ifr->ifr_addr;
912 error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
914 panic("%s: SIOCDIFADDR, error=%d", __func__, error);
918 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
919 struct bootpc_globalcontext *gctx, struct thread *td)
922 struct sockaddr_in *sin;
924 struct in_aliasreq *ifra;
925 struct sockaddr_in *myaddr;
926 struct sockaddr_in *netmask;
929 ifra = &ifctx->iareq;
930 myaddr = &ifctx->myaddr;
931 netmask = &ifctx->netmask;
933 if (bootpc_ifctx_isresolved(ifctx) == 0) {
934 /* Shutdown interfaces where BOOTP failed */
935 bootpc_shutdown_interface(ifctx, td);
939 printf("Adjusted interface %s", ifctx->ireq.ifr_name);
941 /* Do BOOTP interface options */
942 if (ifctx->mtu != 0) {
943 printf(" (MTU=%d%s)", ifctx->mtu,
944 (ifctx->mtu > 1514) ? "/JUMBO" : "");
945 ifr->ifr_mtu = ifctx->mtu;
946 error = ifioctl(bootp_so, SIOCSIFMTU, (caddr_t) ifr, td);
948 panic("%s: SIOCSIFMTU, error=%d", __func__, error);
953 * Do enough of ifconfig(8) so that the chosen interface
954 * can talk to the servers. (just set the address)
956 sin = (struct sockaddr_in *) &ifr->ifr_addr;
958 error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
960 panic("%s: SIOCDIFADDR, error=%d", __func__, error);
962 bcopy(myaddr, &ifra->ifra_addr, sizeof(*myaddr));
963 bcopy(netmask, &ifra->ifra_mask, sizeof(*netmask));
964 clear_sinaddr(&ifra->ifra_broadaddr);
965 ifra->ifra_broadaddr.sin_addr.s_addr = myaddr->sin_addr.s_addr |
966 ~netmask->sin_addr.s_addr;
968 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
970 panic("%s: SIOCAIFADDR, error=%d", __func__, error);
974 bootpc_add_default_route(struct bootpc_ifcontext *ifctx)
977 struct sockaddr_in defdst;
978 struct sockaddr_in defmask;
979 struct rt_addrinfo info;
980 struct rib_cmd_info rc;
982 if (ifctx->gw.sin_addr.s_addr == htonl(INADDR_ANY))
985 clear_sinaddr(&defdst);
986 clear_sinaddr(&defmask);
988 bzero((caddr_t)&info, sizeof(info));
989 info.rti_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
990 info.rti_info[RTAX_DST] = (struct sockaddr *)&defdst;
991 info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&defmask;
992 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ifctx->gw;
994 error = rib_action(RT_DEFAULT_FIB, RTM_ADD, &info, &rc);
997 printf("%s: RTM_ADD, error=%d\n", __func__, error);
1002 bootpc_remove_default_route(struct bootpc_ifcontext *ifctx)
1005 struct sockaddr_in defdst;
1006 struct sockaddr_in defmask;
1007 struct rt_addrinfo info;
1008 struct rib_cmd_info rc;
1010 if (ifctx->gw.sin_addr.s_addr == htonl(INADDR_ANY))
1013 clear_sinaddr(&defdst);
1014 clear_sinaddr(&defmask);
1016 bzero((caddr_t)&info, sizeof(info));
1017 info.rti_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
1018 info.rti_info[RTAX_DST] = (struct sockaddr *)&defdst;
1019 info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&defmask;
1020 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ifctx->gw;
1022 error = rib_action(RT_DEFAULT_FIB, RTM_DELETE, &info, &rc);
1024 printf("%s: RTM_DELETE, error=%d\n", __func__, error);
1029 setfs(struct sockaddr_in *addr, char *path, char *p,
1030 const struct in_addr *siaddr)
1033 if (getip(&p, &addr->sin_addr) == 0) {
1034 if (siaddr != NULL && *p == '/')
1035 bcopy(siaddr, &addr->sin_addr, sizeof(struct in_addr));
1044 addr->sin_len = sizeof(struct sockaddr_in);
1045 addr->sin_family = AF_INET;
1047 strlcpy(path, p, MNAMELEN);
1052 getip(char **ptr, struct in_addr *addr)
1060 if (((val = getdec(&p)) < 0) || (val > 255))
1066 if (((val = getdec(&p)) < 0) || (val > 255))
1072 if (((val = getdec(&p)) < 0) || (val > 255))
1078 if (((val = getdec(&p)) < 0) || (val > 255))
1082 addr->s_addr = htonl(ip);
1095 if ((*p < '0') || (*p > '9'))
1097 while ((*p >= '0') && (*p <= '9')) {
1098 ret = ret * 10 + (*p - '0');
1106 mountopts(struct nfs_args *args, char *p)
1108 args->version = NFS_ARGSVERSION;
1109 args->rsize = BOOTP_BLOCKSIZE;
1110 args->wsize = BOOTP_BLOCKSIZE;
1111 args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
1112 args->sotype = SOCK_DGRAM;
1114 nfs_parse_options(p, args);
1118 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
1124 alignedlen = ( len + 3 ) & ~3;
1126 if (m->m_len < alignedlen) {
1127 m = m_pullup(m, alignedlen);
1133 bcopy(mtod(m, u_char *), buf, len);
1134 m_adj(m, alignedlen);
1140 xdr_int_decode(struct mbuf **mptr, int *iptr)
1144 if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
1146 *iptr = fxdr_unsigned(u_int32_t, i);
1151 print_sin_addr(struct sockaddr_in *sin)
1154 print_in_addr(sin->sin_addr);
1158 print_in_addr(struct in_addr addr)
1162 ip = ntohl(addr.s_addr);
1163 printf("%d.%d.%d.%d",
1164 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1168 bootpc_compose_query(struct bootpc_ifcontext *ifctx, struct thread *td)
1170 unsigned char *vendp;
1171 unsigned char vendor_client[64];
1173 uint8_t vendor_client_len;
1175 ifctx->gotrootpath = 0;
1177 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1180 ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */
1181 ifctx->call.htype = 1; /* 10mb ethernet */
1182 ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1183 ifctx->call.hops = 0;
1184 if (bootpc_ifctx_isunresolved(ifctx) != 0)
1186 ifctx->call.xid = txdr_unsigned(ifctx->xid);
1187 bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1189 vendp = ifctx->call.vend;
1190 *vendp++ = 99; /* RFC1048 cookie */
1194 *vendp++ = TAG_MAXMSGSIZE;
1196 *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1197 *vendp++ = sizeof(struct bootp_packet) & 255;
1199 snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1200 ostype, MACHINE, osrelease);
1201 vendor_client_len = strlen(vendor_client);
1202 *vendp++ = TAG_VENDOR_INDENTIFIER;
1203 *vendp++ = vendor_client_len;
1204 memcpy(vendp, vendor_client, vendor_client_len);
1205 vendp += vendor_client_len;
1206 ifctx->dhcpquerytype = DHCP_NOMSG;
1207 switch (ifctx->state) {
1208 case IF_DHCP_UNRESOLVED:
1209 *vendp++ = TAG_DHCP_MSGTYPE;
1211 *vendp++ = DHCP_DISCOVER;
1212 ifctx->dhcpquerytype = DHCP_DISCOVER;
1213 ifctx->gotdhcpserver = 0;
1215 case IF_DHCP_OFFERED:
1216 *vendp++ = TAG_DHCP_MSGTYPE;
1218 *vendp++ = DHCP_REQUEST;
1219 ifctx->dhcpquerytype = DHCP_REQUEST;
1220 *vendp++ = TAG_DHCP_REQ_ADDR;
1222 memcpy(vendp, &ifctx->reply.yiaddr, 4);
1224 if (ifctx->gotdhcpserver != 0) {
1225 *vendp++ = TAG_DHCP_SERVERID;
1227 memcpy(vendp, &ifctx->dhcpserver, 4);
1230 *vendp++ = TAG_DHCP_LEASETIME;
1232 leasetime = htonl(300);
1233 memcpy(vendp, &leasetime, 4);
1241 ifctx->call.secs = 0;
1242 ifctx->call.flags = htons(0x8000); /* We need a broadcast answer */
1246 bootpc_hascookie(struct bootp_packet *bp)
1249 return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1250 bp->vend[2] == 83 && bp->vend[3] == 99);
1254 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1255 unsigned char *start, int len, int tag)
1261 if (tctx->badtag != 0 || tctx->badopt != 0)
1269 if (code == TAG_PAD)
1271 if (code == TAG_END)
1273 if (j >= ej || j + *j + 1 > ej) {
1279 if (tctx->taglen + len > TAG_MAXLEN) {
1285 memcpy(tctx->buf + tctx->taglen,
1287 tctx->taglen += len;
1289 if (code == TAG_OVERLOAD)
1290 tctx->overload = *j;
1296 static unsigned char *
1297 bootpc_tag(struct bootpc_tagcontext *tctx,
1298 struct bootp_packet *bp, int len, int tag)
1306 if (bootpc_hascookie(bp) == 0)
1309 bootpc_tag_helper(tctx, &bp->vend[4],
1310 (unsigned char *) bp + len - &bp->vend[4], tag);
1312 if ((tctx->overload & OVERLOAD_FILE) != 0)
1313 bootpc_tag_helper(tctx,
1314 (unsigned char *) bp->file,
1317 if ((tctx->overload & OVERLOAD_SNAME) != 0)
1318 bootpc_tag_helper(tctx,
1319 (unsigned char *) bp->sname,
1323 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1325 tctx->buf[tctx->taglen] = '\0';
1330 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1331 struct bootpc_globalcontext *gctx)
1336 ifctx->gotnetmask = 0;
1338 clear_sinaddr(&ifctx->myaddr);
1339 clear_sinaddr(&ifctx->netmask);
1340 clear_sinaddr(&ifctx->gw);
1342 ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1344 printf("%s at ", ifctx->ireq.ifr_name);
1345 print_sin_addr(&ifctx->myaddr);
1347 print_in_addr(ifctx->reply.siaddr);
1349 ifctx->gw.sin_addr = ifctx->reply.giaddr;
1350 if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1351 printf(" via gateway ");
1352 print_in_addr(ifctx->reply.giaddr);
1355 /* This call used for the side effect (overload flag) */
1356 (void) bootpc_tag(&gctx->tmptag,
1357 &ifctx->reply, ifctx->replylen, TAG_END);
1359 if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1360 if (ifctx->reply.sname[0] != '\0')
1361 printf(" server name %s", ifctx->reply.sname);
1362 if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1363 if (ifctx->reply.file[0] != '\0')
1364 printf(" boot file %s", ifctx->reply.file);
1368 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1371 if (gctx->tag.taglen != 4)
1372 panic("bootpc: subnet mask len is %d",
1374 bcopy(p, &ifctx->netmask.sin_addr, 4);
1375 ifctx->gotnetmask = 1;
1376 printf("subnet mask ");
1377 print_sin_addr(&ifctx->netmask);
1381 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1385 if (gctx->tag.taglen % 4)
1386 panic("bootpc: Router Len is %d", gctx->tag.taglen);
1387 if (gctx->tag.taglen > 0) {
1388 bcopy(p, &ifctx->gw.sin_addr, 4);
1390 print_sin_addr(&ifctx->gw);
1398 * Choose a root filesystem. If a value is forced in the environment
1399 * and it contains "nfs:", use it unconditionally. Otherwise, if the
1400 * kernel is compiled with the ROOTDEVNAME option, then use it if:
1401 * - The server doesn't provide a pathname.
1402 * - The boothowto flags include RB_DFLTROOT (user said to override
1403 * the server value).
1406 if ((s = kern_getenv("vfs.root.mountfrom")) != NULL) {
1407 if ((p = strstr(s, "nfs:")) != NULL)
1408 p = strdup(p + 4, M_TEMP);
1412 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1415 ifctx->gotrootpath = 1;
1418 if ((p == NULL || (boothowto & RB_DFLTROOT) != 0) &&
1419 (p = strstr(ROOTDEVNAME, "nfs:")) != NULL) {
1424 if (gctx->setrootfs != NULL) {
1425 printf("rootfs %s (ignored) ", p);
1426 } else if (setfs(&nd->root_saddr,
1427 nd->root_hostnam, p, &ifctx->reply.siaddr)) {
1429 printf("root_server ");
1430 print_sin_addr(&nd->root_saddr);
1433 printf("rootfs %s ", p);
1434 gctx->gotrootpath = 1;
1435 gctx->setrootfs = ifctx;
1437 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1441 mountopts(&nd->root_args, p);
1442 printf("rootopts %s ", p);
1445 panic("Failed to set rootfs to %s", p);
1448 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1451 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1452 panic("bootpc: hostname >= %d bytes",
1454 if (gctx->sethostname != NULL) {
1455 printf("hostname %s (ignored) ", p);
1457 strcpy(nd->my_hostnam, p);
1458 mtx_lock(&prison0.pr_mtx);
1459 strcpy(prison0.pr_hostname, p);
1460 mtx_unlock(&prison0.pr_mtx);
1461 printf("hostname %s ", p);
1462 gctx->sethostname = ifctx;
1465 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1467 if (p != NULL) { /* store in a sysctl variable */
1468 int i, l = sizeof(bootp_cookie) - 1;
1469 for (i = 0; i < l && p[i] != '\0'; i++)
1470 bootp_cookie[i] = p[i];
1474 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1477 ifctx->mtu = be16dec(p);
1482 if (ifctx->gotnetmask == 0) {
1484 * If there is no netmask, use historical default,
1485 * but we really need the right mask from the server.
1487 printf("%s: no netmask received!\n", ifctx->ireq.ifr_name);
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);
1498 bootpc_init_ifa_cb(void *arg, struct ifaddr *ifa, u_int count)
1500 struct sockaddr_dl *sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1505 if (sdl->sdl_type != IFT_ETHER)
1508 *(struct sockaddr_dl **)arg = sdl;
1516 struct epoch_tracker et;
1517 struct bootpc_ifcontext *ifctx = NULL; /* Interface BOOTP contexts */
1518 struct bootpc_globalcontext *gctx; /* Global BOOTP context */
1519 struct sockaddr_dl *sdl;
1520 struct if_iter iter;
1523 #ifndef BOOTP_WIRED_TO
1526 struct nfsv3_diskless *nd;
1532 timeout = BOOTP_IFACE_WAIT_TIMEOUT * hz;
1535 nd = &nfsv3_diskless;
1539 * If already filled in, don't touch it here
1541 if (nfs_diskless_valid != 0)
1545 * If "vfs.root.mountfrom" is set and the value is something other
1546 * than "nfs:", it means the user doesn't want to mount root via nfs,
1547 * there's no reason to continue with bootpc
1549 if ((s = kern_getenv("vfs.root.mountfrom")) != NULL) {
1550 if ((strncmp(s, "nfs:", 4)) != 0) {
1551 printf("%s: vfs.root.mountfrom set to %s. "
1552 "BOOTP aborted.\n", __func__, s);
1559 gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK | M_ZERO);
1560 STAILQ_INIT(&gctx->interfaces);
1561 gctx->xid = ~0xFFFF;
1562 gctx->starttime = time_second;
1565 * If ROOTDEVNAME is defined or vfs.root.mountfrom is set then we have
1566 * root-path overrides that can potentially let us boot even if we don't
1567 * get a root path from the server, so we can treat that as a non-error.
1570 gctx->any_root_overrides = 1;
1572 gctx->any_root_overrides = testenv("vfs.root.mountfrom");
1576 * Find a network interface.
1578 CURVNET_SET(TD_TO_VNET(td));
1579 #ifdef BOOTP_WIRED_TO
1580 printf("%s: wired to interface '%s'\n", __func__,
1581 __XSTRING(BOOTP_WIRED_TO));
1585 * Preallocate interface context storage, if another interface
1586 * attaches and wins the race, it won't be eligible for bootp.
1589 NET_EPOCH_ENTER(et);
1590 for (if_t ifp = if_iter_start(&iter); ifp != NULL; ifp = if_iter_next(&iter)) {
1591 if ((if_getflags(ifp) &
1592 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) ==
1596 if_iter_finish(&iter);
1599 printf("WARNING: BOOTP found no eligible network interfaces, skipping!\n");
1603 for (; ifcnt > 0; ifcnt--)
1608 ifctx = STAILQ_FIRST(&gctx->interfaces);
1609 NET_EPOCH_ENTER(et);
1610 for (ifp = if_iter_start(&iter); ifp != NULL; ifp = if_iter_next(&iter)) {
1613 #ifdef BOOTP_WIRED_TO
1614 if (strcmp(if_name(ifp), __XSTRING(BOOTP_WIRED_TO)) != 0)
1617 if ((if_getflags(ifp) &
1618 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1621 switch (if_getalloctype(ifp)) {
1628 strlcpy(ifctx->ireq.ifr_name, if_name(ifp),
1629 sizeof(ifctx->ireq.ifr_name));
1632 /* Get HW address */
1634 if_foreach_addr_type(ifp, AF_LINK, bootpc_init_ifa_cb, &sdl);
1636 panic("bootpc: Unable to find HW address for %s",
1637 ifctx->ireq.ifr_name);
1640 ifctx = STAILQ_NEXT(ifctx, next);
1642 if_iter_finish(&iter);
1646 if (STAILQ_EMPTY(&gctx->interfaces) ||
1647 STAILQ_FIRST(&gctx->interfaces)->ifp == NULL) {
1649 pause("bootpc", delay);
1653 #ifdef BOOTP_WIRED_TO
1654 panic("%s: Could not find interface specified "
1655 "by BOOTP_WIRED_TO: "
1656 __XSTRING(BOOTP_WIRED_TO), __func__);
1658 panic("%s: no suitable interface", __func__);
1662 error = socreate(AF_INET, &bootp_so, SOCK_DGRAM, 0, td->td_ucred, td);
1664 panic("%s: socreate, error=%d", __func__, error);
1666 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1667 bootpc_fakeup_interface(ifctx, td);
1669 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1670 bootpc_compose_query(ifctx, td);
1672 error = bootpc_call(gctx, td);
1674 printf("BOOTP call failed\n");
1677 mountopts(&nd->root_args, NULL);
1679 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1680 if (bootpc_ifctx_isresolved(ifctx) != 0)
1681 bootpc_decode_reply(nd, ifctx, gctx);
1683 #ifdef BOOTP_NFSROOT
1684 if (gctx->gotrootpath == 0 && gctx->any_root_overrides == 0)
1685 panic("bootpc: No root path offered");
1688 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1689 bootpc_adjust_interface(ifctx, gctx, td);
1693 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1694 if (ifctx->gotrootpath != 0)
1696 if (ifctx == NULL) {
1697 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1698 if (bootpc_ifctx_isresolved(ifctx) != 0)
1704 if (gctx->gotrootpath != 0) {
1705 struct epoch_tracker et;
1707 kern_setenv("boot.netif.name", if_name(ifctx->ifp));
1709 NET_EPOCH_ENTER(et);
1710 bootpc_add_default_route(ifctx);
1712 error = md_mount(&nd->root_saddr, nd->root_hostnam,
1713 nd->root_fh, &nd->root_fhsize,
1714 &nd->root_args, td);
1715 NET_EPOCH_ENTER(et);
1716 bootpc_remove_default_route(ifctx);
1719 if (gctx->any_root_overrides == 0)
1720 panic("nfs_boot: mount root, error=%d", error);
1724 rootdevnames[0] = "nfs:";
1725 nfs_diskless_valid = 3;
1728 strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1729 bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1730 bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1731 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1732 ifctx->myaddr.sin_addr.s_addr |
1733 ~ ifctx->netmask.sin_addr.s_addr;
1734 bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1735 bcopy(&ifctx->gw, &nd->mygateway, sizeof(ifctx->gw));
1738 while((ifctx = STAILQ_FIRST(&gctx->interfaces)) != NULL) {
1739 STAILQ_REMOVE_HEAD(&gctx->interfaces, next);
1740 free(ifctx, M_TEMP);
1747 * Given a server pathname, get an NFS file handle.
1748 * Also, sets sin->sin_port to the NFS service port.
1751 md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp, int *fhsizep,
1752 struct nfs_args *args, struct thread *td)
1760 #define RPCPROG_MNT 100005
1761 #define RPCMNT_VER1 1
1762 #define RPCMNT_VER3 3
1763 #define RPCMNT_MOUNT 1
1764 #define AUTH_SYS 1 /* unix style (uid, gids) */
1765 #define AUTH_UNIX AUTH_SYS
1767 /* XXX honor v2/v3 flags in args->flags? */
1769 /* First try NFS v3 */
1770 /* Get port number for MOUNTD. */
1771 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1772 &mdsin->sin_port, td);
1774 m = xdr_string_encode(path, strlen(path));
1776 /* Do RPC to mountd. */
1777 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1778 RPCMNT_MOUNT, &m, NULL, td);
1781 args->flags |= NFSMNT_NFSV3;
1784 /* Fallback to NFS v2 */
1786 /* Get port number for MOUNTD. */
1787 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1788 &mdsin->sin_port, td);
1792 m = xdr_string_encode(path, strlen(path));
1794 /* Do RPC to mountd. */
1795 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1796 RPCMNT_MOUNT, &m, NULL, td);
1798 return error; /* message already freed */
1804 if (xdr_int_decode(&m, &error) != 0 || error != 0)
1807 if ((args->flags & NFSMNT_NFSV3) != 0) {
1808 if (xdr_int_decode(&m, fhsizep) != 0 ||
1809 *fhsizep > NFSX_V3FHMAX ||
1813 *fhsizep = NFSX_V2FH;
1815 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
1818 if (args->flags & NFSMNT_NFSV3) {
1819 if (xdr_int_decode(&m, &authcount) != 0)
1822 if (authcount < 0 || authcount > 100)
1824 while (authcount > 0) {
1825 if (xdr_int_decode(&m, &authver) != 0)
1827 if (authver == AUTH_UNIX)
1831 if (authunixok == 0)
1835 /* Set port number for NFS use. */
1836 error = krpc_portmap(mdsin, NFS_PROG,
1838 NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
1839 &mdsin->sin_port, td);
1851 SYSINIT(bootp_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, bootpc_init, NULL);