2 * Copyright (c) 1995 Gordon Ross, Adam Glass
3 * Copyright (c) 1992 Regents of the University of California.
6 * This software was developed by the Computer Systems Engineering group
7 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
8 * contributed to Berkeley.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Lawrence Berkeley Laboratory and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * $NetBSD: krpc_subr.c,v 1.10 1995/08/08 20:43:43 gwr Exp $
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include "opt_bootp.h"
48 #include "opt_rootdevname.h"
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/endian.h>
54 #include <sys/kernel.h>
55 #include <sys/sockio.h>
56 #include <sys/malloc.h>
57 #include <sys/mount.h>
60 #include <sys/reboot.h>
61 #include <sys/socket.h>
62 #include <sys/socketvar.h>
63 #include <sys/sysctl.h>
67 #include <net/route.h>
69 #include <netinet/in.h>
70 #include <netinet/in_var.h>
71 #include <net/if_types.h>
72 #include <net/if_dl.h>
75 #include <nfs/nfsproto.h>
76 #include <nfsclient/nfs.h>
77 #include <nfs/nfsdiskless.h>
79 #include <nfs/xdr_subs.h>
82 #define BOOTP_MIN_LEN 300 /* Minimum size of bootp udp packet */
84 #ifndef BOOTP_SETTLE_DELAY
85 #define BOOTP_SETTLE_DELAY 3
89 * Wait 10 seconds for interface appearance
90 * USB ethernet adapters might require some time to pop up
92 #ifndef BOOTP_IFACE_WAIT_TIMEOUT
93 #define BOOTP_IFACE_WAIT_TIMEOUT 10
97 * What is the longest we will wait before re-sending a request?
98 * Note this is also the frequency of "RPC timeout" messages.
99 * The re-send loop count sup linearly to this maximum, so the
100 * first complaint will happen after (1+2+3+4+5)=15 seconds.
102 #define MAX_RESEND_DELAY 5 /* seconds */
104 /* Definitions from RFC951 */
105 struct bootp_packet {
113 struct in_addr ciaddr;
114 struct in_addr yiaddr;
115 struct in_addr siaddr;
116 struct in_addr giaddr;
117 unsigned char chaddr[16];
120 unsigned char vend[1222];
123 struct bootpc_ifcontext {
124 STAILQ_ENTRY(bootpc_ifcontext) next;
125 struct bootp_packet call;
126 struct bootp_packet reply;
131 struct in_aliasreq _in_alias_req;
133 #define ireq _req._ifreq
134 #define iareq _req._in_alias_req
136 struct sockaddr_dl *sdl;
137 struct sockaddr_in myaddr;
138 struct sockaddr_in netmask;
139 struct sockaddr_in gw;
155 int dhcpquerytype; /* dhcp type sent */
156 struct in_addr dhcpserver;
161 #define TAG_MAXLEN 1024
162 struct bootpc_tagcontext {
163 char buf[TAG_MAXLEN + 1];
171 struct bootpc_globalcontext {
172 STAILQ_HEAD(, bootpc_ifcontext) interfaces;
174 int any_root_overrides;
180 struct bootp_packet reply;
182 struct bootpc_ifcontext *setrootfs;
183 struct bootpc_ifcontext *sethostname;
184 struct bootpc_tagcontext tmptag;
185 struct bootpc_tagcontext tag;
188 #define IPPORT_BOOTPC 68
189 #define IPPORT_BOOTPS 67
191 #define BOOTP_REQUEST 1
192 #define BOOTP_REPLY 2
195 #define TAG_PAD 0 /* Pad option, implicit length 1 */
196 #define TAG_SUBNETMASK 1 /* RFC 950 subnet mask */
197 #define TAG_ROUTERS 3 /* Routers (in order of preference) */
198 #define TAG_HOSTNAME 12 /* Client host name */
199 #define TAG_ROOT 17 /* Root path */
200 #define TAG_INTF_MTU 26 /* Interface MTU Size (RFC2132) */
202 /* DHCP specific tags */
203 #define TAG_OVERLOAD 52 /* Option Overload */
204 #define TAG_MAXMSGSIZE 57 /* Maximum DHCP Message Size */
206 #define TAG_END 255 /* End Option (i.e. no more options) */
208 /* Overload values */
209 #define OVERLOAD_FILE 1
210 #define OVERLOAD_SNAME 2
212 /* Site specific tags: */
213 #define TAG_ROOTOPTS 130
214 #define TAG_COOKIE 134 /* ascii info for userland, via sysctl */
216 #define TAG_DHCP_MSGTYPE 53
217 #define TAG_DHCP_REQ_ADDR 50
218 #define TAG_DHCP_SERVERID 54
219 #define TAG_DHCP_LEASETIME 51
221 #define TAG_VENDOR_INDENTIFIER 60
224 #define DHCP_DISCOVER 1
226 #define DHCP_REQUEST 3
229 /* NFS read/write block size */
230 #ifndef BOOTP_BLOCKSIZE
231 #define BOOTP_BLOCKSIZE 8192
234 static char bootp_cookie[128];
235 static struct socket *bootp_so;
236 SYSCTL_STRING(_kern, OID_AUTO, bootp_cookie, CTLFLAG_RD,
237 bootp_cookie, 0, "Cookie (T134) supplied by bootp server");
240 static int md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp,
241 int *fhsizep, struct nfs_args *args, struct thread *td);
242 static int setfs(struct sockaddr_in *addr, char *path, char *p,
243 const struct in_addr *siaddr);
244 static int getdec(char **ptr);
245 static int getip(char **ptr, struct in_addr *ip);
246 static void mountopts(struct nfs_args *args, char *p);
247 static int xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len);
248 static int xdr_int_decode(struct mbuf **ptr, int *iptr);
249 static void print_in_addr(struct in_addr addr);
250 static void print_sin_addr(struct sockaddr_in *addr);
251 static void clear_sinaddr(struct sockaddr_in *sin);
252 static void allocifctx(struct bootpc_globalcontext *gctx);
253 static void bootpc_compose_query(struct bootpc_ifcontext *ifctx,
255 static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx,
256 struct bootp_packet *bp, int len, int tag);
257 static void bootpc_tag_helper(struct bootpc_tagcontext *tctx,
258 unsigned char *start, int len, int tag);
261 void bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma);
262 void bootpboot_p_rtentry(struct rtentry *rt);
263 void bootpboot_p_tree(struct radix_node *rn);
264 void bootpboot_p_rtlist(void);
265 void bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa);
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_sa(struct sockaddr *sa, struct sockaddr *ma)
302 printf("(sockaddr *) <null>");
305 switch (sa->sa_family) {
308 struct sockaddr_in *sin;
310 sin = (struct sockaddr_in *) sa;
314 sin = (struct sockaddr_in *) ma;
322 struct sockaddr_dl *sli;
325 sli = (struct sockaddr_dl *) sa;
326 printf("link %.*s ", sli->sdl_nlen, sli->sdl_data);
327 for (i = 0; i < sli->sdl_alen; i++) {
330 printf("%x", ((unsigned char *) LLADDR(sli))[i]);
335 printf("af%d", sa->sa_family);
340 bootpboot_p_rtentry(struct rtentry *rt)
343 bootpboot_p_sa(rt_key(rt), rt_mask(rt));
345 bootpboot_p_sa(rt->rt_gateway, NULL);
347 printf("flags %x", (unsigned short) rt->rt_flags);
348 printf(" %d", (int) rt->rt_expire);
349 printf(" %s\n", rt->rt_ifp->if_xname);
353 bootpboot_p_tree(struct radix_node *rn)
357 if (rn->rn_bit < 0) {
358 if ((rn->rn_flags & RNF_ROOT) != 0) {
360 bootpboot_p_rtentry((struct rtentry *) rn);
362 rn = rn->rn_dupedkey;
364 bootpboot_p_tree(rn->rn_left);
365 bootpboot_p_tree(rn->rn_right);
372 bootpboot_p_rtlist(void)
374 struct radix_node_head *rnh;
376 printf("Routing table:\n");
377 rnh = rt_tables_get_rnh(0, AF_INET);
380 RADIX_NODE_HEAD_RLOCK(rnh); /* could sleep XXX */
381 bootpboot_p_tree(rnh->rnh_treetop);
382 RADIX_NODE_HEAD_RUNLOCK(rnh);
386 bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
389 printf("%s flags %x, addr ",
390 ifp->if_xname, ifp->if_flags);
391 print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
392 printf(", broadcast ");
393 print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
394 printf(", netmask ");
395 print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
400 bootpboot_p_iflist(void)
405 printf("Interface list:\n");
407 for (ifp = TAILQ_FIRST(&V_ifnet);
409 ifp = TAILQ_NEXT(ifp, if_link)) {
410 for (ifa = TAILQ_FIRST(&ifp->if_addrhead);
412 ifa = TAILQ_NEXT(ifa, ifa_link))
413 if (ifa->ifa_addr->sa_family == AF_INET)
414 bootpboot_p_if(ifp, ifa);
418 #endif /* defined(BOOTP_DEBUG) */
421 clear_sinaddr(struct sockaddr_in *sin)
424 bzero(sin, sizeof(*sin));
425 sin->sin_len = sizeof(*sin);
426 sin->sin_family = AF_INET;
427 sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
432 allocifctx(struct bootpc_globalcontext *gctx)
434 struct bootpc_ifcontext *ifctx;
436 ifctx = malloc(sizeof(*ifctx), M_TEMP, M_WAITOK | M_ZERO);
437 ifctx->xid = gctx->xid;
439 ifctx->state = IF_BOOTP_UNRESOLVED;
441 ifctx->state = IF_DHCP_UNRESOLVED;
444 STAILQ_INSERT_TAIL(&gctx->interfaces, ifctx, next);
448 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
451 if (ifctx->state == IF_BOOTP_RESOLVED ||
452 ifctx->state == IF_DHCP_RESOLVED)
458 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
461 if (ifctx->state == IF_BOOTP_UNRESOLVED ||
462 ifctx->state == IF_DHCP_UNRESOLVED)
468 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
471 if (ifctx->state == IF_BOOTP_FAILED ||
472 ifctx->state == IF_DHCP_FAILED)
478 bootpc_received(struct bootpc_globalcontext *gctx,
479 struct bootpc_ifcontext *ifctx)
481 unsigned char dhcpreplytype;
485 * Need timeout for fallback to less
486 * desirable alternative.
489 /* This call used for the side effect (badopt flag) */
490 (void) bootpc_tag(&gctx->tmptag, &gctx->reply,
494 /* If packet is invalid, ignore it */
495 if (gctx->tmptag.badopt != 0)
498 p = bootpc_tag(&gctx->tmptag, &gctx->reply,
499 gctx->replylen, TAG_DHCP_MSGTYPE);
503 dhcpreplytype = DHCP_NOMSG;
505 switch (ifctx->dhcpquerytype) {
507 if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */
508 #ifndef BOOTP_FORCE_DHCP
509 && dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */
515 if (dhcpreplytype != DHCP_ACK)
521 /* Ignore packet unless it gives us a root tag we didn't have */
523 if ((ifctx->state == IF_BOOTP_RESOLVED ||
524 (ifctx->dhcpquerytype == DHCP_DISCOVER &&
525 (ifctx->state == IF_DHCP_OFFERED ||
526 ifctx->state == IF_DHCP_RESOLVED))) &&
527 (bootpc_tag(&gctx->tmptag, &ifctx->reply,
530 bootpc_tag(&gctx->tmptag, &gctx->reply,
535 bcopy(&gctx->reply, &ifctx->reply, gctx->replylen);
536 ifctx->replylen = gctx->replylen;
538 /* XXX: Only reset if 'perfect' response */
539 if (ifctx->state == IF_BOOTP_UNRESOLVED)
540 ifctx->state = IF_BOOTP_RESOLVED;
541 else if (ifctx->state == IF_DHCP_UNRESOLVED &&
542 ifctx->dhcpquerytype == DHCP_DISCOVER) {
543 if (dhcpreplytype == DHCP_OFFER)
544 ifctx->state = IF_DHCP_OFFERED;
546 ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */
547 } else if (ifctx->state == IF_DHCP_OFFERED &&
548 ifctx->dhcpquerytype == DHCP_REQUEST)
549 ifctx->state = IF_DHCP_RESOLVED;
552 if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
553 ifctx->state != IF_BOOTP_RESOLVED) {
554 p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
555 ifctx->replylen, TAG_DHCP_SERVERID);
556 if (p != NULL && gctx->tmptag.taglen == 4) {
557 memcpy(&ifctx->dhcpserver, p, 4);
558 ifctx->gotdhcpserver = 1;
560 ifctx->gotdhcpserver = 0;
564 ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
567 ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
569 TAG_ROUTERS) != NULL);
570 ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
572 TAG_SUBNETMASK) != NULL);
577 bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
579 struct sockaddr_in *sin, dst;
583 int error, on, rcvflg, timo, len;
587 struct bootpc_ifcontext *ifctx;
595 bzero(&sopt, sizeof(sopt));
596 sopt.sopt_dir = SOPT_SET;
597 sopt.sopt_level = SOL_SOCKET;
598 sopt.sopt_name = SO_RCVTIMEO;
600 sopt.sopt_valsize = sizeof tv;
602 error = sosetopt(bootp_so, &sopt);
610 sopt.sopt_name = SO_BROADCAST;
612 sopt.sopt_valsize = sizeof on;
614 error = sosetopt(bootp_so, &sopt);
623 sopt.sopt_name = SO_DONTROUTE;
625 sopt.sopt_valsize = sizeof on;
627 error = sosetopt(bootp_so, &sopt);
632 * Bind the local endpoint to a bootp client port.
636 sin->sin_port = htons(IPPORT_BOOTPC);
637 error = sobind(bootp_so, (struct sockaddr *)sin, td);
639 printf("bind failed\n");
644 * Setup socket address for the server.
648 sin->sin_addr.s_addr = INADDR_BROADCAST;
649 sin->sin_port = htons(IPPORT_BOOTPS);
652 * Send it, repeatedly, until a reply is received,
653 * but delay each re-send by an increasing amount.
654 * If the delay hits the maximum, start complaining.
663 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
664 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
665 bootpc_tag(&gctx->tmptag, &ifctx->reply,
671 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
672 struct in_aliasreq *ifra = &ifctx->iareq;
673 sin = (struct sockaddr_in *)&ifra->ifra_mask;
675 ifctx->outstanding = 0;
676 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
680 if (bootpc_ifctx_isfailed(ifctx) != 0)
684 ifctx->outstanding = 1;
686 /* Proceed to next step in DHCP negotiation */
687 if ((ifctx->state == IF_DHCP_OFFERED &&
688 ifctx->dhcpquerytype != DHCP_REQUEST) ||
689 (ifctx->state == IF_DHCP_UNRESOLVED &&
690 ifctx->dhcpquerytype != DHCP_DISCOVER) ||
691 (ifctx->state == IF_BOOTP_UNRESOLVED &&
692 ifctx->dhcpquerytype != DHCP_NOMSG)) {
694 bootpc_compose_query(ifctx, td);
697 /* Send BOOTP request (or re-send). */
699 if (ifctx->sentmsg == 0) {
700 switch(ifctx->dhcpquerytype) {
712 printf("Sending %s packet from "
713 "interface %s (%*D)\n",
715 ifctx->ireq.ifr_name,
716 ifctx->sdl->sdl_alen,
717 (unsigned char *) LLADDR(ifctx->sdl),
722 aio.iov_base = (caddr_t) &ifctx->call;
723 aio.iov_len = sizeof(ifctx->call);
727 auio.uio_segflg = UIO_SYSSPACE;
728 auio.uio_rw = UIO_WRITE;
730 auio.uio_resid = sizeof(ifctx->call);
733 /* Set netmask to 0.0.0.0 */
735 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
738 panic("%s: SIOCAIFADDR, error=%d", __func__,
741 error = sosend(bootp_so, (struct sockaddr *) &dst,
742 &auio, NULL, NULL, 0, td);
744 printf("%s: sosend: %d state %08x\n", __func__,
745 error, (int )bootp_so->so_state);
747 /* Set netmask to 255.0.0.0 */
748 sin->sin_addr.s_addr = htonl(IN_CLASSA_NET);
749 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
752 panic("%s: SIOCAIFADDR, error=%d", __func__,
756 if (outstanding == 0 &&
757 (rtimo == 0 || time_second >= rtimo)) {
762 /* Determine new timeout. */
763 if (timo < MAX_RESEND_DELAY)
766 printf("DHCP/BOOTP timeout for server ");
767 print_sin_addr(&dst);
772 * Wait for up to timo seconds for a reply.
773 * The socket receive timeout was set to 1 second.
775 atimo = timo + time_second;
776 while (time_second < atimo) {
777 aio.iov_base = (caddr_t) &gctx->reply;
778 aio.iov_len = sizeof(gctx->reply);
782 auio.uio_segflg = UIO_SYSSPACE;
783 auio.uio_rw = UIO_READ;
785 auio.uio_resid = sizeof(gctx->reply);
789 error = soreceive(bootp_so, NULL, &auio,
790 NULL, NULL, &rcvflg);
791 gctx->secs = time_second - gctx->starttime;
792 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
793 if (bootpc_ifctx_isresolved(ifctx) != 0 ||
794 bootpc_ifctx_isfailed(ifctx) != 0)
797 ifctx->call.secs = htons(gctx->secs);
799 if (error == EWOULDBLOCK)
803 len = sizeof(gctx->reply) - auio.uio_resid;
805 /* Do we have the required number of bytes ? */
806 if (len < BOOTP_MIN_LEN)
808 gctx->replylen = len;
811 if (gctx->reply.op != BOOTP_REPLY)
814 /* Is this an answer to our query */
815 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
816 if (gctx->reply.xid != ifctx->call.xid)
819 /* Same HW address size ? */
820 if (gctx->reply.hlen != ifctx->call.hlen)
823 /* Correct HW address ? */
824 if (bcmp(gctx->reply.chaddr,
826 ifctx->call.hlen) != 0)
833 s = bootpc_tag(&gctx->tmptag,
846 s = "DHCP (unexpected)";
852 printf("Received %s packet"
855 ifctx->ireq.ifr_name);
856 print_in_addr(gctx->reply.siaddr);
857 if (gctx->reply.giaddr.s_addr !=
860 print_in_addr(gctx->reply.giaddr);
862 if (bootpc_received(gctx, ifctx) != 0) {
863 printf(" (accepted)");
864 if (ifctx->outstanding) {
865 ifctx->outstanding = 0;
868 /* Network settle delay */
869 if (outstanding == 0)
870 atimo = time_second +
873 printf(" (ignored)");
874 if (ifctx->gotrootpath ||
875 gctx->any_root_overrides) {
877 rtimo = time_second +
879 if (ifctx->gotrootpath)
880 printf(" (got root path)");
886 if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
889 /* Force a retry if halfway in DHCP negotiation */
891 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
892 if (ifctx->state == IF_DHCP_OFFERED) {
893 if (ifctx->dhcpquerytype == DHCP_DISCOVER)
896 ifctx->state = IF_DHCP_UNRESOLVED;
902 if (gotrootpath != 0) {
903 gctx->gotrootpath = gotrootpath;
904 if (rtimo != 0 && time_second >= rtimo)
907 } /* forever send/receive */
910 * XXX: These are errors of varying seriousness being silently
914 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
915 if (bootpc_ifctx_isresolved(ifctx) == 0) {
916 printf("%s timeout for interface %s\n",
917 ifctx->dhcpquerytype != DHCP_NOMSG ?
919 ifctx->ireq.ifr_name);
922 if (gctx->gotrootpath != 0) {
924 printf("Got a root path, ignoring remaining timeout\n");
929 #ifndef BOOTP_NFSROOT
930 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
931 if (bootpc_ifctx_isresolved(ifctx) != 0) {
943 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
946 struct in_aliasreq *ifra;
947 struct sockaddr_in *sin;
951 ifra = &ifctx->iareq;
954 * Bring up the interface.
956 * Get the old interface flags and or IFF_UP into them; if
957 * IFF_UP set blindly, interface selection can be clobbered.
959 error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
961 panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
962 ifr->ifr_flags |= IFF_UP;
963 error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
965 panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
968 * Do enough of ifconfig(8) so that the chosen interface
969 * can talk to the servers. Set address to 0.0.0.0/8 and
970 * broadcast address to local broadcast.
972 sin = (struct sockaddr_in *)&ifra->ifra_addr;
974 sin = (struct sockaddr_in *)&ifra->ifra_mask;
976 sin->sin_addr.s_addr = htonl(IN_CLASSA_NET);
977 sin = (struct sockaddr_in *)&ifra->ifra_broadaddr;
979 sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
980 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
982 panic("%s: SIOCAIFADDR, error=%d", __func__, error);
986 bootpc_shutdown_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
989 struct sockaddr_in *sin;
994 printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
995 error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
997 panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
998 ifr->ifr_flags &= ~IFF_UP;
999 error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
1001 panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
1003 sin = (struct sockaddr_in *) &ifr->ifr_addr;
1005 error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
1007 panic("%s: SIOCDIFADDR, error=%d", __func__, error);
1011 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
1012 struct bootpc_globalcontext *gctx, struct thread *td)
1015 struct sockaddr_in *sin;
1017 struct in_aliasreq *ifra;
1018 struct sockaddr_in *myaddr;
1019 struct sockaddr_in *netmask;
1022 ifra = &ifctx->iareq;
1023 myaddr = &ifctx->myaddr;
1024 netmask = &ifctx->netmask;
1026 if (bootpc_ifctx_isresolved(ifctx) == 0) {
1027 /* Shutdown interfaces where BOOTP failed */
1028 bootpc_shutdown_interface(ifctx, td);
1032 printf("Adjusted interface %s", ifctx->ireq.ifr_name);
1034 /* Do BOOTP interface options */
1035 if (ifctx->mtu != 0) {
1036 printf(" (MTU=%d%s)", ifctx->mtu,
1037 (ifctx->mtu > 1514) ? "/JUMBO" : "");
1038 ifr->ifr_mtu = ifctx->mtu;
1039 error = ifioctl(bootp_so, SIOCSIFMTU, (caddr_t) ifr, td);
1041 panic("%s: SIOCSIFMTU, error=%d", __func__, error);
1046 * Do enough of ifconfig(8) so that the chosen interface
1047 * can talk to the servers. (just set the address)
1049 sin = (struct sockaddr_in *) &ifr->ifr_addr;
1051 error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
1053 panic("%s: SIOCDIFADDR, error=%d", __func__, error);
1055 bcopy(myaddr, &ifra->ifra_addr, sizeof(*myaddr));
1056 bcopy(netmask, &ifra->ifra_mask, sizeof(*netmask));
1057 clear_sinaddr(&ifra->ifra_broadaddr);
1058 ifra->ifra_broadaddr.sin_addr.s_addr = myaddr->sin_addr.s_addr |
1059 ~netmask->sin_addr.s_addr;
1061 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
1063 panic("%s: SIOCAIFADDR, error=%d", __func__, error);
1067 bootpc_add_default_route(struct bootpc_ifcontext *ifctx)
1070 struct sockaddr_in defdst;
1071 struct sockaddr_in defmask;
1073 if (ifctx->gw.sin_addr.s_addr == htonl(INADDR_ANY))
1076 clear_sinaddr(&defdst);
1077 clear_sinaddr(&defmask);
1079 error = rtrequest_fib(RTM_ADD, (struct sockaddr *)&defdst,
1080 (struct sockaddr *) &ifctx->gw, (struct sockaddr *)&defmask,
1081 (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL, RT_DEFAULT_FIB);
1083 printf("%s: RTM_ADD, error=%d\n", __func__, error);
1088 bootpc_remove_default_route(struct bootpc_ifcontext *ifctx)
1091 struct sockaddr_in defdst;
1092 struct sockaddr_in defmask;
1094 if (ifctx->gw.sin_addr.s_addr == htonl(INADDR_ANY))
1097 clear_sinaddr(&defdst);
1098 clear_sinaddr(&defmask);
1100 error = rtrequest_fib(RTM_DELETE, (struct sockaddr *)&defdst,
1101 (struct sockaddr *) &ifctx->gw, (struct sockaddr *)&defmask,
1102 (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL, RT_DEFAULT_FIB);
1104 printf("%s: RTM_DELETE, error=%d\n", __func__, error);
1109 setfs(struct sockaddr_in *addr, char *path, char *p,
1110 const struct in_addr *siaddr)
1113 if (getip(&p, &addr->sin_addr) == 0) {
1114 if (siaddr != NULL && *p == '/')
1115 bcopy(siaddr, &addr->sin_addr, sizeof(struct in_addr));
1124 addr->sin_len = sizeof(struct sockaddr_in);
1125 addr->sin_family = AF_INET;
1127 strlcpy(path, p, MNAMELEN);
1132 getip(char **ptr, struct in_addr *addr)
1140 if (((val = getdec(&p)) < 0) || (val > 255))
1146 if (((val = getdec(&p)) < 0) || (val > 255))
1152 if (((val = getdec(&p)) < 0) || (val > 255))
1158 if (((val = getdec(&p)) < 0) || (val > 255))
1162 addr->s_addr = htonl(ip);
1175 if ((*p < '0') || (*p > '9'))
1177 while ((*p >= '0') && (*p <= '9')) {
1178 ret = ret * 10 + (*p - '0');
1186 mountopts(struct nfs_args *args, char *p)
1188 args->version = NFS_ARGSVERSION;
1189 args->rsize = BOOTP_BLOCKSIZE;
1190 args->wsize = BOOTP_BLOCKSIZE;
1191 args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
1192 args->sotype = SOCK_DGRAM;
1194 nfs_parse_options(p, args);
1198 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
1204 alignedlen = ( len + 3 ) & ~3;
1206 if (m->m_len < alignedlen) {
1207 m = m_pullup(m, alignedlen);
1213 bcopy(mtod(m, u_char *), buf, len);
1214 m_adj(m, alignedlen);
1220 xdr_int_decode(struct mbuf **mptr, int *iptr)
1224 if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
1226 *iptr = fxdr_unsigned(u_int32_t, i);
1231 print_sin_addr(struct sockaddr_in *sin)
1234 print_in_addr(sin->sin_addr);
1238 print_in_addr(struct in_addr addr)
1242 ip = ntohl(addr.s_addr);
1243 printf("%d.%d.%d.%d",
1244 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1248 bootpc_compose_query(struct bootpc_ifcontext *ifctx, struct thread *td)
1250 unsigned char *vendp;
1251 unsigned char vendor_client[64];
1253 uint8_t vendor_client_len;
1255 ifctx->gotrootpath = 0;
1257 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1260 ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */
1261 ifctx->call.htype = 1; /* 10mb ethernet */
1262 ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1263 ifctx->call.hops = 0;
1264 if (bootpc_ifctx_isunresolved(ifctx) != 0)
1266 ifctx->call.xid = txdr_unsigned(ifctx->xid);
1267 bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1269 vendp = ifctx->call.vend;
1270 *vendp++ = 99; /* RFC1048 cookie */
1274 *vendp++ = TAG_MAXMSGSIZE;
1276 *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1277 *vendp++ = sizeof(struct bootp_packet) & 255;
1279 snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1280 ostype, MACHINE, osrelease);
1281 vendor_client_len = strlen(vendor_client);
1282 *vendp++ = TAG_VENDOR_INDENTIFIER;
1283 *vendp++ = vendor_client_len;
1284 memcpy(vendp, vendor_client, vendor_client_len);
1285 vendp += vendor_client_len;
1286 ifctx->dhcpquerytype = DHCP_NOMSG;
1287 switch (ifctx->state) {
1288 case IF_DHCP_UNRESOLVED:
1289 *vendp++ = TAG_DHCP_MSGTYPE;
1291 *vendp++ = DHCP_DISCOVER;
1292 ifctx->dhcpquerytype = DHCP_DISCOVER;
1293 ifctx->gotdhcpserver = 0;
1295 case IF_DHCP_OFFERED:
1296 *vendp++ = TAG_DHCP_MSGTYPE;
1298 *vendp++ = DHCP_REQUEST;
1299 ifctx->dhcpquerytype = DHCP_REQUEST;
1300 *vendp++ = TAG_DHCP_REQ_ADDR;
1302 memcpy(vendp, &ifctx->reply.yiaddr, 4);
1304 if (ifctx->gotdhcpserver != 0) {
1305 *vendp++ = TAG_DHCP_SERVERID;
1307 memcpy(vendp, &ifctx->dhcpserver, 4);
1310 *vendp++ = TAG_DHCP_LEASETIME;
1312 leasetime = htonl(300);
1313 memcpy(vendp, &leasetime, 4);
1321 ifctx->call.secs = 0;
1322 ifctx->call.flags = htons(0x8000); /* We need a broadcast answer */
1326 bootpc_hascookie(struct bootp_packet *bp)
1329 return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1330 bp->vend[2] == 83 && bp->vend[3] == 99);
1334 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1335 unsigned char *start, int len, int tag)
1341 if (tctx->badtag != 0 || tctx->badopt != 0)
1349 if (code == TAG_PAD)
1351 if (code == TAG_END)
1353 if (j >= ej || j + *j + 1 > ej) {
1359 if (tctx->taglen + len > TAG_MAXLEN) {
1365 memcpy(tctx->buf + tctx->taglen,
1367 tctx->taglen += len;
1369 if (code == TAG_OVERLOAD)
1370 tctx->overload = *j;
1376 static unsigned char *
1377 bootpc_tag(struct bootpc_tagcontext *tctx,
1378 struct bootp_packet *bp, int len, int tag)
1386 if (bootpc_hascookie(bp) == 0)
1389 bootpc_tag_helper(tctx, &bp->vend[4],
1390 (unsigned char *) bp + len - &bp->vend[4], tag);
1392 if ((tctx->overload & OVERLOAD_FILE) != 0)
1393 bootpc_tag_helper(tctx,
1394 (unsigned char *) bp->file,
1397 if ((tctx->overload & OVERLOAD_SNAME) != 0)
1398 bootpc_tag_helper(tctx,
1399 (unsigned char *) bp->sname,
1403 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1405 tctx->buf[tctx->taglen] = '\0';
1410 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1411 struct bootpc_globalcontext *gctx)
1417 ifctx->gotnetmask = 0;
1419 clear_sinaddr(&ifctx->myaddr);
1420 clear_sinaddr(&ifctx->netmask);
1421 clear_sinaddr(&ifctx->gw);
1423 ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1425 ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1427 printf("%s at ", ifctx->ireq.ifr_name);
1428 print_sin_addr(&ifctx->myaddr);
1430 print_in_addr(ifctx->reply.siaddr);
1432 ifctx->gw.sin_addr = ifctx->reply.giaddr;
1433 if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1434 printf(" via gateway ");
1435 print_in_addr(ifctx->reply.giaddr);
1438 /* This call used for the side effect (overload flag) */
1439 (void) bootpc_tag(&gctx->tmptag,
1440 &ifctx->reply, ifctx->replylen, TAG_END);
1442 if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1443 if (ifctx->reply.sname[0] != '\0')
1444 printf(" server name %s", ifctx->reply.sname);
1445 if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1446 if (ifctx->reply.file[0] != '\0')
1447 printf(" boot file %s", ifctx->reply.file);
1451 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1454 if (gctx->tag.taglen != 4)
1455 panic("bootpc: subnet mask len is %d",
1457 bcopy(p, &ifctx->netmask.sin_addr, 4);
1458 ifctx->gotnetmask = 1;
1459 printf("subnet mask ");
1460 print_sin_addr(&ifctx->netmask);
1464 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1468 if (gctx->tag.taglen % 4)
1469 panic("bootpc: Router Len is %d", gctx->tag.taglen);
1470 if (gctx->tag.taglen > 0) {
1471 bcopy(p, &ifctx->gw.sin_addr, 4);
1473 print_sin_addr(&ifctx->gw);
1481 * Choose a root filesystem. If a value is forced in the environment
1482 * and it contains "nfs:", use it unconditionally. Otherwise, if the
1483 * kernel is compiled with the ROOTDEVNAME option, then use it if:
1484 * - The server doesn't provide a pathname.
1485 * - The boothowto flags include RB_DFLTROOT (user said to override
1486 * the server value).
1489 if ((s = getenv("vfs.root.mountfrom")) != NULL) {
1490 if ((p = strstr(s, "nfs:")) != NULL)
1491 p = strdup(p + 4, M_TEMP);
1495 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1498 ifctx->gotrootpath = 1;
1501 if ((p == NULL || (boothowto & RB_DFLTROOT) != 0) &&
1502 (p = strstr(ROOTDEVNAME, "nfs:")) != NULL) {
1507 if (gctx->setrootfs != NULL) {
1508 printf("rootfs %s (ignored) ", p);
1509 } else if (setfs(&nd->root_saddr,
1510 nd->root_hostnam, p, &ifctx->reply.siaddr)) {
1512 printf("root_server ");
1513 print_sin_addr(&nd->root_saddr);
1516 printf("rootfs %s ", p);
1517 gctx->gotrootpath = 1;
1518 gctx->setrootfs = ifctx;
1520 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1524 mountopts(&nd->root_args, p);
1525 printf("rootopts %s ", p);
1528 panic("Failed to set rootfs to %s", p);
1531 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1534 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1535 panic("bootpc: hostname >= %d bytes",
1537 if (gctx->sethostname != NULL) {
1538 printf("hostname %s (ignored) ", p);
1540 strcpy(nd->my_hostnam, p);
1541 mtx_lock(&prison0.pr_mtx);
1542 strcpy(prison0.pr_hostname, p);
1543 mtx_unlock(&prison0.pr_mtx);
1544 printf("hostname %s ", p);
1545 gctx->sethostname = ifctx;
1548 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1550 if (p != NULL) { /* store in a sysctl variable */
1551 int i, l = sizeof(bootp_cookie) - 1;
1552 for (i = 0; i < l && p[i] != '\0'; i++)
1553 bootp_cookie[i] = p[i];
1557 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1560 ifctx->mtu = be16dec(p);
1565 if (ifctx->gotnetmask == 0) {
1566 if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1567 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1568 else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1569 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1571 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1578 struct bootpc_ifcontext *ifctx; /* Interface BOOTP contexts */
1579 struct bootpc_globalcontext *gctx; /* Global BOOTP context */
1581 struct sockaddr_dl *sdl;
1584 #ifndef BOOTP_WIRED_TO
1587 struct nfsv3_diskless *nd;
1592 timeout = BOOTP_IFACE_WAIT_TIMEOUT * hz;
1595 nd = &nfsv3_diskless;
1599 * If already filled in, don't touch it here
1601 if (nfs_diskless_valid != 0)
1604 gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK | M_ZERO);
1605 STAILQ_INIT(&gctx->interfaces);
1606 gctx->xid = ~0xFFFF;
1607 gctx->starttime = time_second;
1610 * If ROOTDEVNAME is defined or vfs.root.mountfrom is set then we have
1611 * root-path overrides that can potentially let us boot even if we don't
1612 * get a root path from the server, so we can treat that as a non-error.
1615 gctx->any_root_overrides = 1;
1617 gctx->any_root_overrides = testenv("vfs.root.mountfrom");
1621 * Find a network interface.
1623 CURVNET_SET(TD_TO_VNET(td));
1624 #ifdef BOOTP_WIRED_TO
1625 printf("%s: wired to interface '%s'\n", __func__,
1626 __XSTRING(BOOTP_WIRED_TO));
1630 * Preallocate interface context storage, if another interface
1631 * attaches and wins the race, it won't be eligible for bootp.
1635 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1636 if ((ifp->if_flags &
1637 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1640 switch (ifp->if_alloctype) {
1652 panic("%s: no eligible interfaces", __func__);
1653 for (; ifcnt > 0; ifcnt--)
1658 ifctx = STAILQ_FIRST(&gctx->interfaces);
1660 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1663 #ifdef BOOTP_WIRED_TO
1664 if (strcmp(ifp->if_xname, __XSTRING(BOOTP_WIRED_TO)) != 0)
1667 if ((ifp->if_flags &
1668 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1671 switch (ifp->if_alloctype) {
1680 strlcpy(ifctx->ireq.ifr_name, ifp->if_xname,
1681 sizeof(ifctx->ireq.ifr_name));
1684 /* Get HW address */
1686 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1687 if (ifa->ifa_addr->sa_family == AF_LINK) {
1688 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1689 if (sdl->sdl_type == IFT_ETHER)
1693 panic("bootpc: Unable to find HW address for %s",
1694 ifctx->ireq.ifr_name);
1697 ifctx = STAILQ_NEXT(ifctx, next);
1702 if (STAILQ_EMPTY(&gctx->interfaces) ||
1703 STAILQ_FIRST(&gctx->interfaces)->ifp == NULL) {
1705 pause("bootpc", delay);
1709 #ifdef BOOTP_WIRED_TO
1710 panic("%s: Could not find interface specified "
1711 "by BOOTP_WIRED_TO: "
1712 __XSTRING(BOOTP_WIRED_TO), __func__);
1714 panic("%s: no suitable interface", __func__);
1718 error = socreate(AF_INET, &bootp_so, SOCK_DGRAM, 0, td->td_ucred, td);
1720 panic("%s: socreate, error=%d", __func__, error);
1722 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1723 bootpc_fakeup_interface(ifctx, td);
1725 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1726 bootpc_compose_query(ifctx, td);
1728 error = bootpc_call(gctx, td);
1730 printf("BOOTP call failed\n");
1733 mountopts(&nd->root_args, NULL);
1735 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1736 if (bootpc_ifctx_isresolved(ifctx) != 0)
1737 bootpc_decode_reply(nd, ifctx, gctx);
1739 #ifdef BOOTP_NFSROOT
1740 if (gctx->gotrootpath == 0 && gctx->any_root_overrides == 0)
1741 panic("bootpc: No root path offered");
1744 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1745 bootpc_adjust_interface(ifctx, gctx, td);
1749 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1750 if (ifctx->gotrootpath != 0)
1752 if (ifctx == NULL) {
1753 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1754 if (bootpc_ifctx_isresolved(ifctx) != 0)
1760 if (gctx->gotrootpath != 0) {
1762 setenv("boot.netif.name", ifctx->ifp->if_xname);
1764 bootpc_add_default_route(ifctx);
1765 error = md_mount(&nd->root_saddr, nd->root_hostnam,
1766 nd->root_fh, &nd->root_fhsize,
1767 &nd->root_args, td);
1768 bootpc_remove_default_route(ifctx);
1770 if (gctx->any_root_overrides == 0)
1771 panic("nfs_boot: mount root, error=%d", error);
1775 rootdevnames[0] = "nfs:";
1777 rootdevnames[1] = "oldnfs:";
1779 nfs_diskless_valid = 3;
1782 strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1783 bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1784 bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1785 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1786 ifctx->myaddr.sin_addr.s_addr |
1787 ~ ifctx->netmask.sin_addr.s_addr;
1788 bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1789 bcopy(&ifctx->gw, &nd->mygateway, sizeof(ifctx->gw));
1792 while((ifctx = STAILQ_FIRST(&gctx->interfaces)) != NULL) {
1793 STAILQ_REMOVE_HEAD(&gctx->interfaces, next);
1794 free(ifctx, M_TEMP);
1801 * Given a server pathname, get an NFS file handle.
1802 * Also, sets sin->sin_port to the NFS service port.
1805 md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp, int *fhsizep,
1806 struct nfs_args *args, struct thread *td)
1814 #define RPCPROG_MNT 100005
1815 #define RPCMNT_VER1 1
1816 #define RPCMNT_VER3 3
1817 #define RPCMNT_MOUNT 1
1818 #define AUTH_SYS 1 /* unix style (uid, gids) */
1819 #define AUTH_UNIX AUTH_SYS
1821 /* XXX honor v2/v3 flags in args->flags? */
1823 /* First try NFS v3 */
1824 /* Get port number for MOUNTD. */
1825 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1826 &mdsin->sin_port, td);
1828 m = xdr_string_encode(path, strlen(path));
1830 /* Do RPC to mountd. */
1831 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1832 RPCMNT_MOUNT, &m, NULL, td);
1835 args->flags |= NFSMNT_NFSV3;
1838 /* Fallback to NFS v2 */
1840 /* Get port number for MOUNTD. */
1841 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1842 &mdsin->sin_port, td);
1846 m = xdr_string_encode(path, strlen(path));
1848 /* Do RPC to mountd. */
1849 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1850 RPCMNT_MOUNT, &m, NULL, td);
1852 return error; /* message already freed */
1858 if (xdr_int_decode(&m, &error) != 0 || error != 0)
1861 if ((args->flags & NFSMNT_NFSV3) != 0) {
1862 if (xdr_int_decode(&m, fhsizep) != 0 ||
1863 *fhsizep > NFSX_V3FHMAX ||
1867 *fhsizep = NFSX_V2FH;
1869 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
1872 if (args->flags & NFSMNT_NFSV3) {
1873 if (xdr_int_decode(&m, &authcount) != 0)
1876 if (authcount < 0 || authcount > 100)
1878 while (authcount > 0) {
1879 if (xdr_int_decode(&m, &authver) != 0)
1881 if (authver == AUTH_UNIX)
1885 if (authunixok == 0)
1889 /* Set port number for NFS use. */
1890 error = krpc_portmap(mdsin, NFS_PROG,
1892 NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
1893 &mdsin->sin_port, td);
1905 SYSINIT(bootp_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, bootpc_init, NULL);