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 #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_iflist(void);
267 static int bootpc_call(struct bootpc_globalcontext *gctx,
270 static void bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
273 static void bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
274 struct bootpc_globalcontext *gctx, struct thread *td);
276 static void bootpc_decode_reply(struct nfsv3_diskless *nd,
277 struct bootpc_ifcontext *ifctx,
278 struct bootpc_globalcontext *gctx);
280 static int bootpc_received(struct bootpc_globalcontext *gctx,
281 struct bootpc_ifcontext *ifctx);
283 static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
284 static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
285 static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
288 * In order to have multiple active interfaces with address 0.0.0.0
289 * and be able to send data to a selected interface, we first set
290 * mask to /8 on all interfaces, and temporarily set it to /0 when
296 bootpboot_p_ifa(void *ifp, struct ifaddr *ifa, u_int count __unused)
299 printf("%s flags %x, addr ",
300 if_name(ifp), if_getflags(ifp));
301 print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
302 printf(", broadcast ");
303 print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
304 printf(", netmask ");
305 print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
312 bootpboot_p_iflist(void)
314 struct epoch_tracker et;
318 printf("Interface list:\n");
320 for (ifp = if_iter_start(&iter); ifp != NULL; ifp = if_iter_next(&iter))
321 if_foreach_addr_type(ifp, AF_INET, bootpboot_p_ifa, ifp);
322 if_iter_finish(&iter);
325 #endif /* defined(BOOTP_DEBUG) */
328 clear_sinaddr(struct sockaddr_in *sin)
331 bzero(sin, sizeof(*sin));
332 sin->sin_len = sizeof(*sin);
333 sin->sin_family = AF_INET;
334 sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
339 allocifctx(struct bootpc_globalcontext *gctx)
341 struct bootpc_ifcontext *ifctx;
343 ifctx = malloc(sizeof(*ifctx), M_TEMP, M_WAITOK | M_ZERO);
344 ifctx->xid = gctx->xid;
346 ifctx->state = IF_BOOTP_UNRESOLVED;
348 ifctx->state = IF_DHCP_UNRESOLVED;
351 STAILQ_INSERT_TAIL(&gctx->interfaces, ifctx, next);
355 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
358 if (ifctx->state == IF_BOOTP_RESOLVED ||
359 ifctx->state == IF_DHCP_RESOLVED)
365 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
368 if (ifctx->state == IF_BOOTP_UNRESOLVED ||
369 ifctx->state == IF_DHCP_UNRESOLVED)
375 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
378 if (ifctx->state == IF_BOOTP_FAILED ||
379 ifctx->state == IF_DHCP_FAILED)
385 bootpc_received(struct bootpc_globalcontext *gctx,
386 struct bootpc_ifcontext *ifctx)
388 unsigned char dhcpreplytype;
392 * Need timeout for fallback to less
393 * desirable alternative.
396 /* This call used for the side effect (badopt flag) */
397 (void) bootpc_tag(&gctx->tmptag, &gctx->reply,
401 /* If packet is invalid, ignore it */
402 if (gctx->tmptag.badopt != 0)
405 p = bootpc_tag(&gctx->tmptag, &gctx->reply,
406 gctx->replylen, TAG_DHCP_MSGTYPE);
410 dhcpreplytype = DHCP_NOMSG;
412 switch (ifctx->dhcpquerytype) {
414 if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */
415 #ifndef BOOTP_FORCE_DHCP
416 && dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */
422 if (dhcpreplytype != DHCP_ACK)
428 /* Ignore packet unless it gives us a root tag we didn't have */
430 if ((ifctx->state == IF_BOOTP_RESOLVED ||
431 (ifctx->dhcpquerytype == DHCP_DISCOVER &&
432 (ifctx->state == IF_DHCP_OFFERED ||
433 ifctx->state == IF_DHCP_RESOLVED))) &&
434 (bootpc_tag(&gctx->tmptag, &ifctx->reply,
437 bootpc_tag(&gctx->tmptag, &gctx->reply,
442 bcopy(&gctx->reply, &ifctx->reply, gctx->replylen);
443 ifctx->replylen = gctx->replylen;
445 /* XXX: Only reset if 'perfect' response */
446 if (ifctx->state == IF_BOOTP_UNRESOLVED)
447 ifctx->state = IF_BOOTP_RESOLVED;
448 else if (ifctx->state == IF_DHCP_UNRESOLVED &&
449 ifctx->dhcpquerytype == DHCP_DISCOVER) {
450 if (dhcpreplytype == DHCP_OFFER)
451 ifctx->state = IF_DHCP_OFFERED;
453 ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */
454 } else if (ifctx->state == IF_DHCP_OFFERED &&
455 ifctx->dhcpquerytype == DHCP_REQUEST)
456 ifctx->state = IF_DHCP_RESOLVED;
458 if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
459 ifctx->state != IF_BOOTP_RESOLVED) {
460 p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
461 ifctx->replylen, TAG_DHCP_SERVERID);
462 if (p != NULL && gctx->tmptag.taglen == 4) {
463 memcpy(&ifctx->dhcpserver, p, 4);
464 ifctx->gotdhcpserver = 1;
466 ifctx->gotdhcpserver = 0;
470 ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
473 ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
475 TAG_ROUTERS) != NULL);
476 ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
478 TAG_SUBNETMASK) != NULL);
483 bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
485 struct sockaddr_in *sin, dst;
489 int error, on, rcvflg, timo, len;
493 struct bootpc_ifcontext *ifctx;
501 bzero(&sopt, sizeof(sopt));
502 sopt.sopt_dir = SOPT_SET;
503 sopt.sopt_level = SOL_SOCKET;
504 sopt.sopt_name = SO_RCVTIMEO;
506 sopt.sopt_valsize = sizeof tv;
508 error = sosetopt(bootp_so, &sopt);
516 sopt.sopt_name = SO_BROADCAST;
518 sopt.sopt_valsize = sizeof on;
520 error = sosetopt(bootp_so, &sopt);
529 sopt.sopt_name = SO_DONTROUTE;
531 sopt.sopt_valsize = sizeof on;
533 error = sosetopt(bootp_so, &sopt);
538 * Bind the local endpoint to a bootp client port.
542 sin->sin_port = htons(IPPORT_BOOTPC);
543 error = sobind(bootp_so, (struct sockaddr *)sin, td);
545 printf("bind failed\n");
550 * Setup socket address for the server.
554 sin->sin_addr.s_addr = INADDR_BROADCAST;
555 sin->sin_port = htons(IPPORT_BOOTPS);
558 * Send it, repeatedly, until a reply is received,
559 * but delay each re-send by an increasing amount.
560 * If the delay hits the maximum, start complaining.
568 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
569 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
570 bootpc_tag(&gctx->tmptag, &ifctx->reply,
576 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
577 struct in_aliasreq *ifra = &ifctx->iareq;
578 sin = (struct sockaddr_in *)&ifra->ifra_mask;
580 ifctx->outstanding = 0;
581 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
585 if (bootpc_ifctx_isfailed(ifctx) != 0)
589 ifctx->outstanding = 1;
591 /* Proceed to next step in DHCP negotiation */
592 if ((ifctx->state == IF_DHCP_OFFERED &&
593 ifctx->dhcpquerytype != DHCP_REQUEST) ||
594 (ifctx->state == IF_DHCP_UNRESOLVED &&
595 ifctx->dhcpquerytype != DHCP_DISCOVER) ||
596 (ifctx->state == IF_BOOTP_UNRESOLVED &&
597 ifctx->dhcpquerytype != DHCP_NOMSG)) {
599 bootpc_compose_query(ifctx, td);
602 /* Send BOOTP request (or re-send). */
604 if (ifctx->sentmsg == 0) {
605 switch(ifctx->dhcpquerytype) {
617 printf("Sending %s packet from "
618 "interface %s (%*D)\n",
620 ifctx->ireq.ifr_name,
621 ifctx->sdl->sdl_alen,
622 (unsigned char *) LLADDR(ifctx->sdl),
627 aio.iov_base = (caddr_t) &ifctx->call;
628 aio.iov_len = sizeof(ifctx->call);
632 auio.uio_segflg = UIO_SYSSPACE;
633 auio.uio_rw = UIO_WRITE;
635 auio.uio_resid = sizeof(ifctx->call);
638 /* Set netmask to 0.0.0.0 */
640 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
643 panic("%s: SIOCAIFADDR, error=%d", __func__,
646 error = sosend(bootp_so, (struct sockaddr *) &dst,
647 &auio, NULL, NULL, 0, td);
649 printf("%s: sosend: %d state %08x\n", __func__,
650 error, (int )bootp_so->so_state);
652 /* Set netmask to 255.0.0.0 */
653 sin->sin_addr.s_addr = htonl(0xff000000);
654 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
657 panic("%s: SIOCAIFADDR, error=%d", __func__,
661 if (outstanding == 0 &&
662 (rtimo == 0 || time_second >= rtimo)) {
667 /* Determine new timeout. */
668 if (timo < MAX_RESEND_DELAY)
671 printf("DHCP/BOOTP timeout for server ");
672 print_sin_addr(&dst);
677 * Wait for up to timo seconds for a reply.
678 * The socket receive timeout was set to 1 second.
680 atimo = timo + time_second;
681 while (time_second < atimo) {
682 aio.iov_base = (caddr_t) &gctx->reply;
683 aio.iov_len = sizeof(gctx->reply);
687 auio.uio_segflg = UIO_SYSSPACE;
688 auio.uio_rw = UIO_READ;
690 auio.uio_resid = sizeof(gctx->reply);
694 error = soreceive(bootp_so, NULL, &auio,
695 NULL, NULL, &rcvflg);
696 gctx->secs = time_second - gctx->starttime;
697 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
698 if (bootpc_ifctx_isresolved(ifctx) != 0 ||
699 bootpc_ifctx_isfailed(ifctx) != 0)
702 ifctx->call.secs = htons(gctx->secs);
704 if (error == EWOULDBLOCK)
708 len = sizeof(gctx->reply) - auio.uio_resid;
710 /* Do we have the required number of bytes ? */
711 if (len < BOOTP_MIN_LEN)
713 gctx->replylen = len;
716 if (gctx->reply.op != BOOTP_REPLY)
719 /* Is this an answer to our query */
720 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
721 if (gctx->reply.xid != ifctx->call.xid)
724 /* Same HW address size ? */
725 if (gctx->reply.hlen != ifctx->call.hlen)
728 /* Correct HW address ? */
729 if (bcmp(gctx->reply.chaddr,
731 ifctx->call.hlen) != 0)
738 s = bootpc_tag(&gctx->tmptag,
751 s = "DHCP (unexpected)";
757 printf("Received %s packet"
760 ifctx->ireq.ifr_name);
761 print_in_addr(gctx->reply.siaddr);
762 if (gctx->reply.giaddr.s_addr !=
765 print_in_addr(gctx->reply.giaddr);
767 if (bootpc_received(gctx, ifctx) != 0) {
768 printf(" (accepted)");
769 if (ifctx->outstanding) {
770 ifctx->outstanding = 0;
773 /* Network settle delay */
774 if (outstanding == 0)
775 atimo = time_second +
778 printf(" (ignored)");
779 if (ifctx->gotrootpath ||
780 gctx->any_root_overrides) {
782 rtimo = time_second +
784 if (ifctx->gotrootpath)
785 printf(" (got root path)");
791 if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
794 /* Force a retry if halfway in DHCP negotiation */
796 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
797 if (ifctx->state == IF_DHCP_OFFERED) {
798 if (ifctx->dhcpquerytype == DHCP_DISCOVER)
801 ifctx->state = IF_DHCP_UNRESOLVED;
807 if (gotrootpath != 0) {
808 gctx->gotrootpath = gotrootpath;
809 if (rtimo != 0 && time_second >= rtimo)
812 } /* forever send/receive */
815 * XXX: These are errors of varying seriousness being silently
819 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
820 if (bootpc_ifctx_isresolved(ifctx) == 0) {
821 printf("%s timeout for interface %s\n",
822 ifctx->dhcpquerytype != DHCP_NOMSG ?
824 ifctx->ireq.ifr_name);
827 if (gctx->gotrootpath != 0) {
829 printf("Got a root path, ignoring remaining timeout\n");
834 #ifndef BOOTP_NFSROOT
835 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
836 if (bootpc_ifctx_isresolved(ifctx) != 0) {
848 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
851 struct in_aliasreq *ifra;
852 struct sockaddr_in *sin;
856 ifra = &ifctx->iareq;
859 * Bring up the interface.
861 * Get the old interface flags and or IFF_UP into them; if
862 * IFF_UP set blindly, interface selection can be clobbered.
864 error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
866 panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
867 ifr->ifr_flags |= IFF_UP;
868 error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
870 panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
873 * Do enough of ifconfig(8) so that the chosen interface
874 * can talk to the servers. Set address to 0.0.0.0/8 and
875 * broadcast address to local broadcast.
877 sin = (struct sockaddr_in *)&ifra->ifra_addr;
879 sin = (struct sockaddr_in *)&ifra->ifra_mask;
881 sin->sin_addr.s_addr = htonl(0xff000000);
882 sin = (struct sockaddr_in *)&ifra->ifra_broadaddr;
884 sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
885 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
887 panic("%s: SIOCAIFADDR, error=%d", __func__, error);
891 bootpc_shutdown_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
894 struct sockaddr_in *sin;
899 printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
900 error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
902 panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
903 ifr->ifr_flags &= ~IFF_UP;
904 error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
906 panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
908 sin = (struct sockaddr_in *) &ifr->ifr_addr;
910 error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
912 panic("%s: SIOCDIFADDR, error=%d", __func__, error);
916 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
917 struct bootpc_globalcontext *gctx, struct thread *td)
920 struct sockaddr_in *sin;
922 struct in_aliasreq *ifra;
923 struct sockaddr_in *myaddr;
924 struct sockaddr_in *netmask;
927 ifra = &ifctx->iareq;
928 myaddr = &ifctx->myaddr;
929 netmask = &ifctx->netmask;
931 if (bootpc_ifctx_isresolved(ifctx) == 0) {
932 /* Shutdown interfaces where BOOTP failed */
933 bootpc_shutdown_interface(ifctx, td);
937 printf("Adjusted interface %s", ifctx->ireq.ifr_name);
939 /* Do BOOTP interface options */
940 if (ifctx->mtu != 0) {
941 printf(" (MTU=%d%s)", ifctx->mtu,
942 (ifctx->mtu > 1514) ? "/JUMBO" : "");
943 ifr->ifr_mtu = ifctx->mtu;
944 error = ifioctl(bootp_so, SIOCSIFMTU, (caddr_t) ifr, td);
946 panic("%s: SIOCSIFMTU, error=%d", __func__, error);
951 * Do enough of ifconfig(8) so that the chosen interface
952 * can talk to the servers. (just set the address)
954 sin = (struct sockaddr_in *) &ifr->ifr_addr;
956 error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
958 panic("%s: SIOCDIFADDR, error=%d", __func__, error);
960 bcopy(myaddr, &ifra->ifra_addr, sizeof(*myaddr));
961 bcopy(netmask, &ifra->ifra_mask, sizeof(*netmask));
962 clear_sinaddr(&ifra->ifra_broadaddr);
963 ifra->ifra_broadaddr.sin_addr.s_addr = myaddr->sin_addr.s_addr |
964 ~netmask->sin_addr.s_addr;
966 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
968 panic("%s: SIOCAIFADDR, error=%d", __func__, error);
972 bootpc_add_default_route(struct bootpc_ifcontext *ifctx)
975 struct sockaddr_in defdst;
976 struct sockaddr_in defmask;
977 struct rt_addrinfo info;
978 struct rib_cmd_info rc;
980 if (ifctx->gw.sin_addr.s_addr == htonl(INADDR_ANY))
983 clear_sinaddr(&defdst);
984 clear_sinaddr(&defmask);
986 bzero((caddr_t)&info, sizeof(info));
987 info.rti_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
988 info.rti_info[RTAX_DST] = (struct sockaddr *)&defdst;
989 info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&defmask;
990 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ifctx->gw;
992 error = rib_action(RT_DEFAULT_FIB, RTM_ADD, &info, &rc);
995 printf("%s: RTM_ADD, error=%d\n", __func__, error);
1000 bootpc_remove_default_route(struct bootpc_ifcontext *ifctx)
1003 struct sockaddr_in defdst;
1004 struct sockaddr_in defmask;
1005 struct rt_addrinfo info;
1006 struct rib_cmd_info rc;
1008 if (ifctx->gw.sin_addr.s_addr == htonl(INADDR_ANY))
1011 clear_sinaddr(&defdst);
1012 clear_sinaddr(&defmask);
1014 bzero((caddr_t)&info, sizeof(info));
1015 info.rti_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
1016 info.rti_info[RTAX_DST] = (struct sockaddr *)&defdst;
1017 info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&defmask;
1018 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ifctx->gw;
1020 error = rib_action(RT_DEFAULT_FIB, RTM_DELETE, &info, &rc);
1022 printf("%s: RTM_DELETE, error=%d\n", __func__, error);
1027 setfs(struct sockaddr_in *addr, char *path, char *p,
1028 const struct in_addr *siaddr)
1031 if (getip(&p, &addr->sin_addr) == 0) {
1032 if (siaddr != NULL && *p == '/')
1033 bcopy(siaddr, &addr->sin_addr, sizeof(struct in_addr));
1042 addr->sin_len = sizeof(struct sockaddr_in);
1043 addr->sin_family = AF_INET;
1045 strlcpy(path, p, MNAMELEN);
1050 getip(char **ptr, struct in_addr *addr)
1058 if (((val = getdec(&p)) < 0) || (val > 255))
1064 if (((val = getdec(&p)) < 0) || (val > 255))
1070 if (((val = getdec(&p)) < 0) || (val > 255))
1076 if (((val = getdec(&p)) < 0) || (val > 255))
1080 addr->s_addr = htonl(ip);
1093 if ((*p < '0') || (*p > '9'))
1095 while ((*p >= '0') && (*p <= '9')) {
1096 ret = ret * 10 + (*p - '0');
1104 mountopts(struct nfs_args *args, char *p)
1106 args->version = NFS_ARGSVERSION;
1107 args->rsize = BOOTP_BLOCKSIZE;
1108 args->wsize = BOOTP_BLOCKSIZE;
1109 args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
1110 args->sotype = SOCK_DGRAM;
1112 nfs_parse_options(p, args);
1116 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
1122 alignedlen = ( len + 3 ) & ~3;
1124 if (m->m_len < alignedlen) {
1125 m = m_pullup(m, alignedlen);
1131 bcopy(mtod(m, u_char *), buf, len);
1132 m_adj(m, alignedlen);
1138 xdr_int_decode(struct mbuf **mptr, int *iptr)
1142 if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
1144 *iptr = fxdr_unsigned(u_int32_t, i);
1149 print_sin_addr(struct sockaddr_in *sin)
1152 print_in_addr(sin->sin_addr);
1156 print_in_addr(struct in_addr addr)
1160 ip = ntohl(addr.s_addr);
1161 printf("%d.%d.%d.%d",
1162 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1166 bootpc_compose_query(struct bootpc_ifcontext *ifctx, struct thread *td)
1168 unsigned char *vendp;
1169 unsigned char vendor_client[64];
1171 uint8_t vendor_client_len;
1173 ifctx->gotrootpath = 0;
1175 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1178 ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */
1179 ifctx->call.htype = 1; /* 10mb ethernet */
1180 ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1181 ifctx->call.hops = 0;
1182 if (bootpc_ifctx_isunresolved(ifctx) != 0)
1184 ifctx->call.xid = txdr_unsigned(ifctx->xid);
1185 bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1187 vendp = ifctx->call.vend;
1188 *vendp++ = 99; /* RFC1048 cookie */
1192 *vendp++ = TAG_MAXMSGSIZE;
1194 *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1195 *vendp++ = sizeof(struct bootp_packet) & 255;
1197 snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1198 ostype, MACHINE, osrelease);
1199 vendor_client_len = strlen(vendor_client);
1200 *vendp++ = TAG_VENDOR_INDENTIFIER;
1201 *vendp++ = vendor_client_len;
1202 memcpy(vendp, vendor_client, vendor_client_len);
1203 vendp += vendor_client_len;
1204 ifctx->dhcpquerytype = DHCP_NOMSG;
1205 switch (ifctx->state) {
1206 case IF_DHCP_UNRESOLVED:
1207 *vendp++ = TAG_DHCP_MSGTYPE;
1209 *vendp++ = DHCP_DISCOVER;
1210 ifctx->dhcpquerytype = DHCP_DISCOVER;
1211 ifctx->gotdhcpserver = 0;
1213 case IF_DHCP_OFFERED:
1214 *vendp++ = TAG_DHCP_MSGTYPE;
1216 *vendp++ = DHCP_REQUEST;
1217 ifctx->dhcpquerytype = DHCP_REQUEST;
1218 *vendp++ = TAG_DHCP_REQ_ADDR;
1220 memcpy(vendp, &ifctx->reply.yiaddr, 4);
1222 if (ifctx->gotdhcpserver != 0) {
1223 *vendp++ = TAG_DHCP_SERVERID;
1225 memcpy(vendp, &ifctx->dhcpserver, 4);
1228 *vendp++ = TAG_DHCP_LEASETIME;
1230 leasetime = htonl(300);
1231 memcpy(vendp, &leasetime, 4);
1239 ifctx->call.secs = 0;
1240 ifctx->call.flags = htons(0x8000); /* We need a broadcast answer */
1244 bootpc_hascookie(struct bootp_packet *bp)
1247 return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1248 bp->vend[2] == 83 && bp->vend[3] == 99);
1252 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1253 unsigned char *start, int len, int tag)
1259 if (tctx->badtag != 0 || tctx->badopt != 0)
1267 if (code == TAG_PAD)
1269 if (code == TAG_END)
1271 if (j >= ej || j + *j + 1 > ej) {
1277 if (tctx->taglen + len > TAG_MAXLEN) {
1283 memcpy(tctx->buf + tctx->taglen,
1285 tctx->taglen += len;
1287 if (code == TAG_OVERLOAD)
1288 tctx->overload = *j;
1294 static unsigned char *
1295 bootpc_tag(struct bootpc_tagcontext *tctx,
1296 struct bootp_packet *bp, int len, int tag)
1304 if (bootpc_hascookie(bp) == 0)
1307 bootpc_tag_helper(tctx, &bp->vend[4],
1308 (unsigned char *) bp + len - &bp->vend[4], tag);
1310 if ((tctx->overload & OVERLOAD_FILE) != 0)
1311 bootpc_tag_helper(tctx,
1312 (unsigned char *) bp->file,
1315 if ((tctx->overload & OVERLOAD_SNAME) != 0)
1316 bootpc_tag_helper(tctx,
1317 (unsigned char *) bp->sname,
1321 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1323 tctx->buf[tctx->taglen] = '\0';
1328 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1329 struct bootpc_globalcontext *gctx)
1334 ifctx->gotnetmask = 0;
1336 clear_sinaddr(&ifctx->myaddr);
1337 clear_sinaddr(&ifctx->netmask);
1338 clear_sinaddr(&ifctx->gw);
1340 ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1342 printf("%s at ", ifctx->ireq.ifr_name);
1343 print_sin_addr(&ifctx->myaddr);
1345 print_in_addr(ifctx->reply.siaddr);
1347 ifctx->gw.sin_addr = ifctx->reply.giaddr;
1348 if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1349 printf(" via gateway ");
1350 print_in_addr(ifctx->reply.giaddr);
1353 /* This call used for the side effect (overload flag) */
1354 (void) bootpc_tag(&gctx->tmptag,
1355 &ifctx->reply, ifctx->replylen, TAG_END);
1357 if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1358 if (ifctx->reply.sname[0] != '\0')
1359 printf(" server name %s", ifctx->reply.sname);
1360 if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1361 if (ifctx->reply.file[0] != '\0')
1362 printf(" boot file %s", ifctx->reply.file);
1366 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1369 if (gctx->tag.taglen != 4)
1370 panic("bootpc: subnet mask len is %d",
1372 bcopy(p, &ifctx->netmask.sin_addr, 4);
1373 ifctx->gotnetmask = 1;
1374 printf("subnet mask ");
1375 print_sin_addr(&ifctx->netmask);
1379 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1383 if (gctx->tag.taglen % 4)
1384 panic("bootpc: Router Len is %d", gctx->tag.taglen);
1385 if (gctx->tag.taglen > 0) {
1386 bcopy(p, &ifctx->gw.sin_addr, 4);
1388 print_sin_addr(&ifctx->gw);
1396 * Choose a root filesystem. If a value is forced in the environment
1397 * and it contains "nfs:", use it unconditionally. Otherwise, if the
1398 * kernel is compiled with the ROOTDEVNAME option, then use it if:
1399 * - The server doesn't provide a pathname.
1400 * - The boothowto flags include RB_DFLTROOT (user said to override
1401 * the server value).
1404 if ((s = kern_getenv("vfs.root.mountfrom")) != NULL) {
1405 if ((p = strstr(s, "nfs:")) != NULL)
1406 p = strdup(p + 4, M_TEMP);
1410 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1413 ifctx->gotrootpath = 1;
1416 if ((p == NULL || (boothowto & RB_DFLTROOT) != 0) &&
1417 (p = strstr(ROOTDEVNAME, "nfs:")) != NULL) {
1422 if (gctx->setrootfs != NULL) {
1423 printf("rootfs %s (ignored) ", p);
1424 } else if (setfs(&nd->root_saddr,
1425 nd->root_hostnam, p, &ifctx->reply.siaddr)) {
1427 printf("root_server ");
1428 print_sin_addr(&nd->root_saddr);
1431 printf("rootfs %s ", p);
1432 gctx->gotrootpath = 1;
1433 gctx->setrootfs = ifctx;
1435 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1439 mountopts(&nd->root_args, p);
1440 printf("rootopts %s ", p);
1443 panic("Failed to set rootfs to %s", p);
1446 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1449 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1450 panic("bootpc: hostname >= %d bytes",
1452 if (gctx->sethostname != NULL) {
1453 printf("hostname %s (ignored) ", p);
1455 strcpy(nd->my_hostnam, p);
1456 mtx_lock(&prison0.pr_mtx);
1457 strcpy(prison0.pr_hostname, p);
1458 mtx_unlock(&prison0.pr_mtx);
1459 printf("hostname %s ", p);
1460 gctx->sethostname = ifctx;
1463 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1465 if (p != NULL) { /* store in a sysctl variable */
1466 int i, l = sizeof(bootp_cookie) - 1;
1467 for (i = 0; i < l && p[i] != '\0'; i++)
1468 bootp_cookie[i] = p[i];
1472 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1475 ifctx->mtu = be16dec(p);
1480 if (ifctx->gotnetmask == 0) {
1482 * If there is no netmask, use historical default,
1483 * but we really need the right mask from the server.
1485 printf("%s: no netmask received!\n", ifctx->ireq.ifr_name);
1486 if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1487 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1488 else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1489 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1491 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1496 bootpc_init_ifa_cb(void *arg, struct ifaddr *ifa, u_int count)
1498 struct sockaddr_dl *sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1503 if (sdl->sdl_type != IFT_ETHER)
1506 *(struct sockaddr_dl **)arg = sdl;
1514 struct epoch_tracker et;
1515 struct bootpc_ifcontext *ifctx = NULL; /* Interface BOOTP contexts */
1516 struct bootpc_globalcontext *gctx; /* Global BOOTP context */
1517 struct sockaddr_dl *sdl;
1518 struct if_iter iter;
1521 #ifndef BOOTP_WIRED_TO
1524 struct nfsv3_diskless *nd;
1530 timeout = BOOTP_IFACE_WAIT_TIMEOUT * hz;
1533 nd = &nfsv3_diskless;
1537 * If already filled in, don't touch it here
1539 if (nfs_diskless_valid != 0)
1543 * If "vfs.root.mountfrom" is set and the value is something other
1544 * than "nfs:", it means the user doesn't want to mount root via nfs,
1545 * there's no reason to continue with bootpc
1547 if ((s = kern_getenv("vfs.root.mountfrom")) != NULL) {
1548 if ((strncmp(s, "nfs:", 4)) != 0) {
1549 printf("%s: vfs.root.mountfrom set to %s. "
1550 "BOOTP aborted.\n", __func__, s);
1557 gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK | M_ZERO);
1558 STAILQ_INIT(&gctx->interfaces);
1559 gctx->xid = ~0xFFFF;
1560 gctx->starttime = time_second;
1563 * If ROOTDEVNAME is defined or vfs.root.mountfrom is set then we have
1564 * root-path overrides that can potentially let us boot even if we don't
1565 * get a root path from the server, so we can treat that as a non-error.
1568 gctx->any_root_overrides = 1;
1570 gctx->any_root_overrides = testenv("vfs.root.mountfrom");
1574 * Find a network interface.
1576 CURVNET_SET(TD_TO_VNET(td));
1577 #ifdef BOOTP_WIRED_TO
1578 printf("%s: wired to interface '%s'\n", __func__,
1579 __XSTRING(BOOTP_WIRED_TO));
1583 * Preallocate interface context storage, if another interface
1584 * attaches and wins the race, it won't be eligible for bootp.
1587 NET_EPOCH_ENTER(et);
1588 for (if_t ifp = if_iter_start(&iter); ifp != NULL; ifp = if_iter_next(&iter)) {
1589 if ((if_getflags(ifp) &
1590 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) ==
1594 if_iter_finish(&iter);
1597 printf("WARNING: BOOTP found no eligible network interfaces, skipping!\n");
1601 for (; ifcnt > 0; ifcnt--)
1606 ifctx = STAILQ_FIRST(&gctx->interfaces);
1607 NET_EPOCH_ENTER(et);
1608 for (ifp = if_iter_start(&iter); ifp != NULL; ifp = if_iter_next(&iter)) {
1611 #ifdef BOOTP_WIRED_TO
1612 if (strcmp(if_name(ifp), __XSTRING(BOOTP_WIRED_TO)) != 0)
1615 if ((if_getflags(ifp) &
1616 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1619 switch (if_getalloctype(ifp)) {
1626 strlcpy(ifctx->ireq.ifr_name, if_name(ifp),
1627 sizeof(ifctx->ireq.ifr_name));
1630 /* Get HW address */
1632 if_foreach_addr_type(ifp, AF_LINK, bootpc_init_ifa_cb, &sdl);
1634 panic("bootpc: Unable to find HW address for %s",
1635 ifctx->ireq.ifr_name);
1638 ifctx = STAILQ_NEXT(ifctx, next);
1640 if_iter_finish(&iter);
1644 if (STAILQ_EMPTY(&gctx->interfaces) ||
1645 STAILQ_FIRST(&gctx->interfaces)->ifp == NULL) {
1647 pause("bootpc", delay);
1651 #ifdef BOOTP_WIRED_TO
1652 panic("%s: Could not find interface specified "
1653 "by BOOTP_WIRED_TO: "
1654 __XSTRING(BOOTP_WIRED_TO), __func__);
1656 panic("%s: no suitable interface", __func__);
1660 error = socreate(AF_INET, &bootp_so, SOCK_DGRAM, 0, td->td_ucred, td);
1662 panic("%s: socreate, error=%d", __func__, error);
1664 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1665 bootpc_fakeup_interface(ifctx, td);
1667 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1668 bootpc_compose_query(ifctx, td);
1670 error = bootpc_call(gctx, td);
1672 printf("BOOTP call failed\n");
1675 mountopts(&nd->root_args, NULL);
1677 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1678 if (bootpc_ifctx_isresolved(ifctx) != 0)
1679 bootpc_decode_reply(nd, ifctx, gctx);
1681 #ifdef BOOTP_NFSROOT
1682 if (gctx->gotrootpath == 0 && gctx->any_root_overrides == 0)
1683 panic("bootpc: No root path offered");
1686 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1687 bootpc_adjust_interface(ifctx, gctx, td);
1691 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1692 if (ifctx->gotrootpath != 0)
1694 if (ifctx == NULL) {
1695 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1696 if (bootpc_ifctx_isresolved(ifctx) != 0)
1702 if (gctx->gotrootpath != 0) {
1703 struct epoch_tracker et;
1705 kern_setenv("boot.netif.name", if_name(ifctx->ifp));
1707 NET_EPOCH_ENTER(et);
1708 bootpc_add_default_route(ifctx);
1710 error = md_mount(&nd->root_saddr, nd->root_hostnam,
1711 nd->root_fh, &nd->root_fhsize,
1712 &nd->root_args, td);
1713 NET_EPOCH_ENTER(et);
1714 bootpc_remove_default_route(ifctx);
1717 if (gctx->any_root_overrides == 0)
1718 panic("nfs_boot: mount root, error=%d", error);
1722 rootdevnames[0] = "nfs:";
1723 nfs_diskless_valid = 3;
1726 strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1727 bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1728 bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1729 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1730 ifctx->myaddr.sin_addr.s_addr |
1731 ~ ifctx->netmask.sin_addr.s_addr;
1732 bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1733 bcopy(&ifctx->gw, &nd->mygateway, sizeof(ifctx->gw));
1736 while((ifctx = STAILQ_FIRST(&gctx->interfaces)) != NULL) {
1737 STAILQ_REMOVE_HEAD(&gctx->interfaces, next);
1738 free(ifctx, M_TEMP);
1745 * Given a server pathname, get an NFS file handle.
1746 * Also, sets sin->sin_port to the NFS service port.
1749 md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp, int *fhsizep,
1750 struct nfs_args *args, struct thread *td)
1758 #define RPCPROG_MNT 100005
1759 #define RPCMNT_VER1 1
1760 #define RPCMNT_VER3 3
1761 #define RPCMNT_MOUNT 1
1762 #define AUTH_SYS 1 /* unix style (uid, gids) */
1763 #define AUTH_UNIX AUTH_SYS
1765 /* XXX honor v2/v3 flags in args->flags? */
1767 /* First try NFS v3 */
1768 /* Get port number for MOUNTD. */
1769 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1770 &mdsin->sin_port, td);
1772 m = xdr_string_encode(path, strlen(path));
1774 /* Do RPC to mountd. */
1775 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1776 RPCMNT_MOUNT, &m, NULL, td);
1779 args->flags |= NFSMNT_NFSV3;
1782 /* Fallback to NFS v2 */
1784 /* Get port number for MOUNTD. */
1785 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1786 &mdsin->sin_port, td);
1790 m = xdr_string_encode(path, strlen(path));
1792 /* Do RPC to mountd. */
1793 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1794 RPCMNT_MOUNT, &m, NULL, td);
1796 return error; /* message already freed */
1802 if (xdr_int_decode(&m, &error) != 0 || error != 0)
1805 if ((args->flags & NFSMNT_NFSV3) != 0) {
1806 if (xdr_int_decode(&m, fhsizep) != 0 ||
1807 *fhsizep > NFSX_V3FHMAX ||
1811 *fhsizep = NFSX_V2FH;
1813 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
1816 if (args->flags & NFSMNT_NFSV3) {
1817 if (xdr_int_decode(&m, &authcount) != 0)
1820 if (authcount < 0 || authcount > 100)
1822 while (authcount > 0) {
1823 if (xdr_int_decode(&m, &authver) != 0)
1825 if (authver == AUTH_UNIX)
1829 if (authunixok == 0)
1833 /* Set port number for NFS use. */
1834 error = krpc_portmap(mdsin, NFS_PROG,
1836 NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
1837 &mdsin->sin_port, td);
1849 SYSINIT(bootp_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, bootpc_init, NULL);