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/if_var.h>
68 #include <net/route.h>
70 #include <net/route_var.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>
86 #define BOOTP_MIN_LEN 300 /* Minimum size of bootp udp packet */
88 #ifndef BOOTP_SETTLE_DELAY
89 #define BOOTP_SETTLE_DELAY 3
93 * Wait 10 seconds for interface appearance
94 * USB ethernet adapters might require some time to pop up
96 #ifndef BOOTP_IFACE_WAIT_TIMEOUT
97 #define BOOTP_IFACE_WAIT_TIMEOUT 10
101 * What is the longest we will wait before re-sending a request?
102 * Note this is also the frequency of "RPC timeout" messages.
103 * The re-send loop count sup linearly to this maximum, so the
104 * first complaint will happen after (1+2+3+4+5)=15 seconds.
106 #define MAX_RESEND_DELAY 5 /* seconds */
108 /* Definitions from RFC951 */
109 struct bootp_packet {
117 struct in_addr ciaddr;
118 struct in_addr yiaddr;
119 struct in_addr siaddr;
120 struct in_addr giaddr;
121 unsigned char chaddr[16];
124 unsigned char vend[1222];
127 struct bootpc_ifcontext {
128 STAILQ_ENTRY(bootpc_ifcontext) next;
129 struct bootp_packet call;
130 struct bootp_packet reply;
135 struct in_aliasreq _in_alias_req;
137 #define ireq _req._ifreq
138 #define iareq _req._in_alias_req
140 struct sockaddr_dl *sdl;
141 struct sockaddr_in myaddr;
142 struct sockaddr_in netmask;
143 struct sockaddr_in gw;
159 int dhcpquerytype; /* dhcp type sent */
160 struct in_addr dhcpserver;
165 #define TAG_MAXLEN 1024
166 struct bootpc_tagcontext {
167 char buf[TAG_MAXLEN + 1];
175 struct bootpc_globalcontext {
176 STAILQ_HEAD(, bootpc_ifcontext) interfaces;
178 int any_root_overrides;
184 struct bootp_packet reply;
186 struct bootpc_ifcontext *setrootfs;
187 struct bootpc_ifcontext *sethostname;
188 struct bootpc_tagcontext tmptag;
189 struct bootpc_tagcontext tag;
192 #define IPPORT_BOOTPC 68
193 #define IPPORT_BOOTPS 67
195 #define BOOTP_REQUEST 1
196 #define BOOTP_REPLY 2
199 #define TAG_PAD 0 /* Pad option, implicit length 1 */
200 #define TAG_SUBNETMASK 1 /* RFC 950 subnet mask */
201 #define TAG_ROUTERS 3 /* Routers (in order of preference) */
202 #define TAG_HOSTNAME 12 /* Client host name */
203 #define TAG_ROOT 17 /* Root path */
204 #define TAG_INTF_MTU 26 /* Interface MTU Size (RFC2132) */
206 /* DHCP specific tags */
207 #define TAG_OVERLOAD 52 /* Option Overload */
208 #define TAG_MAXMSGSIZE 57 /* Maximum DHCP Message Size */
210 #define TAG_END 255 /* End Option (i.e. no more options) */
212 /* Overload values */
213 #define OVERLOAD_FILE 1
214 #define OVERLOAD_SNAME 2
216 /* Site specific tags: */
217 #define TAG_ROOTOPTS 130
218 #define TAG_COOKIE 134 /* ascii info for userland, via sysctl */
220 #define TAG_DHCP_MSGTYPE 53
221 #define TAG_DHCP_REQ_ADDR 50
222 #define TAG_DHCP_SERVERID 54
223 #define TAG_DHCP_LEASETIME 51
225 #define TAG_VENDOR_INDENTIFIER 60
228 #define DHCP_DISCOVER 1
230 #define DHCP_REQUEST 3
233 /* NFS read/write block size */
234 #ifndef BOOTP_BLOCKSIZE
235 #define BOOTP_BLOCKSIZE 8192
238 static char bootp_cookie[128];
239 static struct socket *bootp_so;
240 SYSCTL_STRING(_kern, OID_AUTO, bootp_cookie, CTLFLAG_RD,
241 bootp_cookie, 0, "Cookie (T134) supplied by bootp server");
244 static int md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp,
245 int *fhsizep, struct nfs_args *args, struct thread *td);
246 static int setfs(struct sockaddr_in *addr, char *path, char *p,
247 const struct in_addr *siaddr);
248 static int getdec(char **ptr);
249 static int getip(char **ptr, struct in_addr *ip);
250 static void mountopts(struct nfs_args *args, char *p);
251 static int xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len);
252 static int xdr_int_decode(struct mbuf **ptr, int *iptr);
253 static void print_in_addr(struct in_addr addr);
254 static void print_sin_addr(struct sockaddr_in *addr);
255 static void clear_sinaddr(struct sockaddr_in *sin);
256 static void allocifctx(struct bootpc_globalcontext *gctx);
257 static void bootpc_compose_query(struct bootpc_ifcontext *ifctx,
259 static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx,
260 struct bootp_packet *bp, int len, int tag);
261 static void bootpc_tag_helper(struct bootpc_tagcontext *tctx,
262 unsigned char *start, int len, int tag);
265 void bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma);
266 void bootpboot_p_rtentry(struct rtentry *rt);
267 void bootpboot_p_tree(struct radix_node *rn);
268 void bootpboot_p_rtlist(void);
269 void bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa);
270 void bootpboot_p_iflist(void);
273 static int bootpc_call(struct bootpc_globalcontext *gctx,
276 static void bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
279 static void bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
280 struct bootpc_globalcontext *gctx, struct thread *td);
282 static void bootpc_decode_reply(struct nfsv3_diskless *nd,
283 struct bootpc_ifcontext *ifctx,
284 struct bootpc_globalcontext *gctx);
286 static int bootpc_received(struct bootpc_globalcontext *gctx,
287 struct bootpc_ifcontext *ifctx);
289 static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
290 static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
291 static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
294 * In order to have multiple active interfaces with address 0.0.0.0
295 * and be able to send data to a selected interface, we first set
296 * mask to /8 on all interfaces, and temporarily set it to /0 when
302 bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma)
306 printf("(sockaddr *) <null>");
309 switch (sa->sa_family) {
312 struct sockaddr_in *sin;
314 sin = (struct sockaddr_in *) sa;
318 sin = (struct sockaddr_in *) ma;
326 struct sockaddr_dl *sli;
329 sli = (struct sockaddr_dl *) sa;
330 printf("link %.*s ", sli->sdl_nlen, sli->sdl_data);
331 for (i = 0; i < sli->sdl_alen; i++) {
334 printf("%x", ((unsigned char *) LLADDR(sli))[i]);
339 printf("af%d", sa->sa_family);
344 bootpboot_p_rtentry(struct rtentry *rt)
347 bootpboot_p_sa(rt_key(rt), rt_mask(rt));
349 bootpboot_p_sa(rt->rt_gateway, NULL);
351 printf("flags %x", (unsigned short) rt->rt_flags);
352 printf(" %d", (int) rt->rt_expire);
353 printf(" %s\n", rt->rt_ifp->if_xname);
357 bootpboot_p_tree(struct radix_node *rn)
361 if (rn->rn_bit < 0) {
362 if ((rn->rn_flags & RNF_ROOT) != 0) {
364 bootpboot_p_rtentry((struct rtentry *) rn);
366 rn = rn->rn_dupedkey;
368 bootpboot_p_tree(rn->rn_left);
369 bootpboot_p_tree(rn->rn_right);
376 bootpboot_p_rtlist(void)
378 struct rib_head *rnh;
380 printf("Routing table:\n");
381 rnh = rt_tables_get_rnh(0, AF_INET);
384 RIB_RLOCK(rnh); /* could sleep XXX */
385 bootpboot_p_tree(rnh->rnh_treetop);
390 bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
393 printf("%s flags %x, addr ",
394 ifp->if_xname, ifp->if_flags);
395 print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
396 printf(", broadcast ");
397 print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
398 printf(", netmask ");
399 print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
404 bootpboot_p_iflist(void)
409 printf("Interface list:\n");
411 for (ifp = TAILQ_FIRST(&V_ifnet);
413 ifp = TAILQ_NEXT(ifp, if_link)) {
414 for (ifa = TAILQ_FIRST(&ifp->if_addrhead);
416 ifa = TAILQ_NEXT(ifa, ifa_link))
417 if (ifa->ifa_addr->sa_family == AF_INET)
418 bootpboot_p_if(ifp, ifa);
422 #endif /* defined(BOOTP_DEBUG) */
425 clear_sinaddr(struct sockaddr_in *sin)
428 bzero(sin, sizeof(*sin));
429 sin->sin_len = sizeof(*sin);
430 sin->sin_family = AF_INET;
431 sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
436 allocifctx(struct bootpc_globalcontext *gctx)
438 struct bootpc_ifcontext *ifctx;
440 ifctx = malloc(sizeof(*ifctx), M_TEMP, M_WAITOK | M_ZERO);
441 ifctx->xid = gctx->xid;
443 ifctx->state = IF_BOOTP_UNRESOLVED;
445 ifctx->state = IF_DHCP_UNRESOLVED;
448 STAILQ_INSERT_TAIL(&gctx->interfaces, ifctx, next);
452 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
455 if (ifctx->state == IF_BOOTP_RESOLVED ||
456 ifctx->state == IF_DHCP_RESOLVED)
462 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
465 if (ifctx->state == IF_BOOTP_UNRESOLVED ||
466 ifctx->state == IF_DHCP_UNRESOLVED)
472 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
475 if (ifctx->state == IF_BOOTP_FAILED ||
476 ifctx->state == IF_DHCP_FAILED)
482 bootpc_received(struct bootpc_globalcontext *gctx,
483 struct bootpc_ifcontext *ifctx)
485 unsigned char dhcpreplytype;
489 * Need timeout for fallback to less
490 * desirable alternative.
493 /* This call used for the side effect (badopt flag) */
494 (void) bootpc_tag(&gctx->tmptag, &gctx->reply,
498 /* If packet is invalid, ignore it */
499 if (gctx->tmptag.badopt != 0)
502 p = bootpc_tag(&gctx->tmptag, &gctx->reply,
503 gctx->replylen, TAG_DHCP_MSGTYPE);
507 dhcpreplytype = DHCP_NOMSG;
509 switch (ifctx->dhcpquerytype) {
511 if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */
512 #ifndef BOOTP_FORCE_DHCP
513 && dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */
519 if (dhcpreplytype != DHCP_ACK)
525 /* Ignore packet unless it gives us a root tag we didn't have */
527 if ((ifctx->state == IF_BOOTP_RESOLVED ||
528 (ifctx->dhcpquerytype == DHCP_DISCOVER &&
529 (ifctx->state == IF_DHCP_OFFERED ||
530 ifctx->state == IF_DHCP_RESOLVED))) &&
531 (bootpc_tag(&gctx->tmptag, &ifctx->reply,
534 bootpc_tag(&gctx->tmptag, &gctx->reply,
539 bcopy(&gctx->reply, &ifctx->reply, gctx->replylen);
540 ifctx->replylen = gctx->replylen;
542 /* XXX: Only reset if 'perfect' response */
543 if (ifctx->state == IF_BOOTP_UNRESOLVED)
544 ifctx->state = IF_BOOTP_RESOLVED;
545 else if (ifctx->state == IF_DHCP_UNRESOLVED &&
546 ifctx->dhcpquerytype == DHCP_DISCOVER) {
547 if (dhcpreplytype == DHCP_OFFER)
548 ifctx->state = IF_DHCP_OFFERED;
550 ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */
551 } else if (ifctx->state == IF_DHCP_OFFERED &&
552 ifctx->dhcpquerytype == DHCP_REQUEST)
553 ifctx->state = IF_DHCP_RESOLVED;
556 if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
557 ifctx->state != IF_BOOTP_RESOLVED) {
558 p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
559 ifctx->replylen, TAG_DHCP_SERVERID);
560 if (p != NULL && gctx->tmptag.taglen == 4) {
561 memcpy(&ifctx->dhcpserver, p, 4);
562 ifctx->gotdhcpserver = 1;
564 ifctx->gotdhcpserver = 0;
568 ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
571 ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
573 TAG_ROUTERS) != NULL);
574 ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
576 TAG_SUBNETMASK) != NULL);
581 bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
583 struct sockaddr_in *sin, dst;
587 int error, on, rcvflg, timo, len;
591 struct bootpc_ifcontext *ifctx;
599 bzero(&sopt, sizeof(sopt));
600 sopt.sopt_dir = SOPT_SET;
601 sopt.sopt_level = SOL_SOCKET;
602 sopt.sopt_name = SO_RCVTIMEO;
604 sopt.sopt_valsize = sizeof tv;
606 error = sosetopt(bootp_so, &sopt);
614 sopt.sopt_name = SO_BROADCAST;
616 sopt.sopt_valsize = sizeof on;
618 error = sosetopt(bootp_so, &sopt);
627 sopt.sopt_name = SO_DONTROUTE;
629 sopt.sopt_valsize = sizeof on;
631 error = sosetopt(bootp_so, &sopt);
636 * Bind the local endpoint to a bootp client port.
640 sin->sin_port = htons(IPPORT_BOOTPC);
641 error = sobind(bootp_so, (struct sockaddr *)sin, td);
643 printf("bind failed\n");
648 * Setup socket address for the server.
652 sin->sin_addr.s_addr = INADDR_BROADCAST;
653 sin->sin_port = htons(IPPORT_BOOTPS);
656 * Send it, repeatedly, until a reply is received,
657 * but delay each re-send by an increasing amount.
658 * If the delay hits the maximum, start complaining.
667 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
668 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
669 bootpc_tag(&gctx->tmptag, &ifctx->reply,
675 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
676 struct in_aliasreq *ifra = &ifctx->iareq;
677 sin = (struct sockaddr_in *)&ifra->ifra_mask;
679 ifctx->outstanding = 0;
680 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
684 if (bootpc_ifctx_isfailed(ifctx) != 0)
688 ifctx->outstanding = 1;
690 /* Proceed to next step in DHCP negotiation */
691 if ((ifctx->state == IF_DHCP_OFFERED &&
692 ifctx->dhcpquerytype != DHCP_REQUEST) ||
693 (ifctx->state == IF_DHCP_UNRESOLVED &&
694 ifctx->dhcpquerytype != DHCP_DISCOVER) ||
695 (ifctx->state == IF_BOOTP_UNRESOLVED &&
696 ifctx->dhcpquerytype != DHCP_NOMSG)) {
698 bootpc_compose_query(ifctx, td);
701 /* Send BOOTP request (or re-send). */
703 if (ifctx->sentmsg == 0) {
704 switch(ifctx->dhcpquerytype) {
716 printf("Sending %s packet from "
717 "interface %s (%*D)\n",
719 ifctx->ireq.ifr_name,
720 ifctx->sdl->sdl_alen,
721 (unsigned char *) LLADDR(ifctx->sdl),
726 aio.iov_base = (caddr_t) &ifctx->call;
727 aio.iov_len = sizeof(ifctx->call);
731 auio.uio_segflg = UIO_SYSSPACE;
732 auio.uio_rw = UIO_WRITE;
734 auio.uio_resid = sizeof(ifctx->call);
737 /* Set netmask to 0.0.0.0 */
739 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
742 panic("%s: SIOCAIFADDR, error=%d", __func__,
745 error = sosend(bootp_so, (struct sockaddr *) &dst,
746 &auio, NULL, NULL, 0, td);
748 printf("%s: sosend: %d state %08x\n", __func__,
749 error, (int )bootp_so->so_state);
751 /* Set netmask to 255.0.0.0 */
752 sin->sin_addr.s_addr = htonl(IN_CLASSA_NET);
753 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
756 panic("%s: SIOCAIFADDR, error=%d", __func__,
760 if (outstanding == 0 &&
761 (rtimo == 0 || time_second >= rtimo)) {
766 /* Determine new timeout. */
767 if (timo < MAX_RESEND_DELAY)
770 printf("DHCP/BOOTP timeout for server ");
771 print_sin_addr(&dst);
776 * Wait for up to timo seconds for a reply.
777 * The socket receive timeout was set to 1 second.
779 atimo = timo + time_second;
780 while (time_second < atimo) {
781 aio.iov_base = (caddr_t) &gctx->reply;
782 aio.iov_len = sizeof(gctx->reply);
786 auio.uio_segflg = UIO_SYSSPACE;
787 auio.uio_rw = UIO_READ;
789 auio.uio_resid = sizeof(gctx->reply);
793 error = soreceive(bootp_so, NULL, &auio,
794 NULL, NULL, &rcvflg);
795 gctx->secs = time_second - gctx->starttime;
796 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
797 if (bootpc_ifctx_isresolved(ifctx) != 0 ||
798 bootpc_ifctx_isfailed(ifctx) != 0)
801 ifctx->call.secs = htons(gctx->secs);
803 if (error == EWOULDBLOCK)
807 len = sizeof(gctx->reply) - auio.uio_resid;
809 /* Do we have the required number of bytes ? */
810 if (len < BOOTP_MIN_LEN)
812 gctx->replylen = len;
815 if (gctx->reply.op != BOOTP_REPLY)
818 /* Is this an answer to our query */
819 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
820 if (gctx->reply.xid != ifctx->call.xid)
823 /* Same HW address size ? */
824 if (gctx->reply.hlen != ifctx->call.hlen)
827 /* Correct HW address ? */
828 if (bcmp(gctx->reply.chaddr,
830 ifctx->call.hlen) != 0)
837 s = bootpc_tag(&gctx->tmptag,
850 s = "DHCP (unexpected)";
856 printf("Received %s packet"
859 ifctx->ireq.ifr_name);
860 print_in_addr(gctx->reply.siaddr);
861 if (gctx->reply.giaddr.s_addr !=
864 print_in_addr(gctx->reply.giaddr);
866 if (bootpc_received(gctx, ifctx) != 0) {
867 printf(" (accepted)");
868 if (ifctx->outstanding) {
869 ifctx->outstanding = 0;
872 /* Network settle delay */
873 if (outstanding == 0)
874 atimo = time_second +
877 printf(" (ignored)");
878 if (ifctx->gotrootpath ||
879 gctx->any_root_overrides) {
881 rtimo = time_second +
883 if (ifctx->gotrootpath)
884 printf(" (got root path)");
890 if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
893 /* Force a retry if halfway in DHCP negotiation */
895 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
896 if (ifctx->state == IF_DHCP_OFFERED) {
897 if (ifctx->dhcpquerytype == DHCP_DISCOVER)
900 ifctx->state = IF_DHCP_UNRESOLVED;
906 if (gotrootpath != 0) {
907 gctx->gotrootpath = gotrootpath;
908 if (rtimo != 0 && time_second >= rtimo)
911 } /* forever send/receive */
914 * XXX: These are errors of varying seriousness being silently
918 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
919 if (bootpc_ifctx_isresolved(ifctx) == 0) {
920 printf("%s timeout for interface %s\n",
921 ifctx->dhcpquerytype != DHCP_NOMSG ?
923 ifctx->ireq.ifr_name);
926 if (gctx->gotrootpath != 0) {
928 printf("Got a root path, ignoring remaining timeout\n");
933 #ifndef BOOTP_NFSROOT
934 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
935 if (bootpc_ifctx_isresolved(ifctx) != 0) {
947 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
950 struct in_aliasreq *ifra;
951 struct sockaddr_in *sin;
955 ifra = &ifctx->iareq;
958 * Bring up the interface.
960 * Get the old interface flags and or IFF_UP into them; if
961 * IFF_UP set blindly, interface selection can be clobbered.
963 error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
965 panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
966 ifr->ifr_flags |= IFF_UP;
967 error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
969 panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
972 * Do enough of ifconfig(8) so that the chosen interface
973 * can talk to the servers. Set address to 0.0.0.0/8 and
974 * broadcast address to local broadcast.
976 sin = (struct sockaddr_in *)&ifra->ifra_addr;
978 sin = (struct sockaddr_in *)&ifra->ifra_mask;
980 sin->sin_addr.s_addr = htonl(IN_CLASSA_NET);
981 sin = (struct sockaddr_in *)&ifra->ifra_broadaddr;
983 sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
984 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
986 panic("%s: SIOCAIFADDR, error=%d", __func__, error);
990 bootpc_shutdown_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
993 struct sockaddr_in *sin;
998 printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
999 error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
1001 panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
1002 ifr->ifr_flags &= ~IFF_UP;
1003 error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
1005 panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
1007 sin = (struct sockaddr_in *) &ifr->ifr_addr;
1009 error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
1011 panic("%s: SIOCDIFADDR, error=%d", __func__, error);
1015 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
1016 struct bootpc_globalcontext *gctx, struct thread *td)
1019 struct sockaddr_in *sin;
1021 struct in_aliasreq *ifra;
1022 struct sockaddr_in *myaddr;
1023 struct sockaddr_in *netmask;
1026 ifra = &ifctx->iareq;
1027 myaddr = &ifctx->myaddr;
1028 netmask = &ifctx->netmask;
1030 if (bootpc_ifctx_isresolved(ifctx) == 0) {
1031 /* Shutdown interfaces where BOOTP failed */
1032 bootpc_shutdown_interface(ifctx, td);
1036 printf("Adjusted interface %s", ifctx->ireq.ifr_name);
1038 /* Do BOOTP interface options */
1039 if (ifctx->mtu != 0) {
1040 printf(" (MTU=%d%s)", ifctx->mtu,
1041 (ifctx->mtu > 1514) ? "/JUMBO" : "");
1042 ifr->ifr_mtu = ifctx->mtu;
1043 error = ifioctl(bootp_so, SIOCSIFMTU, (caddr_t) ifr, td);
1045 panic("%s: SIOCSIFMTU, error=%d", __func__, error);
1050 * Do enough of ifconfig(8) so that the chosen interface
1051 * can talk to the servers. (just set the address)
1053 sin = (struct sockaddr_in *) &ifr->ifr_addr;
1055 error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
1057 panic("%s: SIOCDIFADDR, error=%d", __func__, error);
1059 bcopy(myaddr, &ifra->ifra_addr, sizeof(*myaddr));
1060 bcopy(netmask, &ifra->ifra_mask, sizeof(*netmask));
1061 clear_sinaddr(&ifra->ifra_broadaddr);
1062 ifra->ifra_broadaddr.sin_addr.s_addr = myaddr->sin_addr.s_addr |
1063 ~netmask->sin_addr.s_addr;
1065 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
1067 panic("%s: SIOCAIFADDR, error=%d", __func__, error);
1071 bootpc_add_default_route(struct bootpc_ifcontext *ifctx)
1074 struct sockaddr_in defdst;
1075 struct sockaddr_in defmask;
1077 if (ifctx->gw.sin_addr.s_addr == htonl(INADDR_ANY))
1080 clear_sinaddr(&defdst);
1081 clear_sinaddr(&defmask);
1083 error = rtrequest_fib(RTM_ADD, (struct sockaddr *)&defdst,
1084 (struct sockaddr *) &ifctx->gw, (struct sockaddr *)&defmask,
1085 (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL, RT_DEFAULT_FIB);
1087 printf("%s: RTM_ADD, error=%d\n", __func__, error);
1092 bootpc_remove_default_route(struct bootpc_ifcontext *ifctx)
1095 struct sockaddr_in defdst;
1096 struct sockaddr_in defmask;
1098 if (ifctx->gw.sin_addr.s_addr == htonl(INADDR_ANY))
1101 clear_sinaddr(&defdst);
1102 clear_sinaddr(&defmask);
1104 error = rtrequest_fib(RTM_DELETE, (struct sockaddr *)&defdst,
1105 (struct sockaddr *) &ifctx->gw, (struct sockaddr *)&defmask,
1106 (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL, RT_DEFAULT_FIB);
1108 printf("%s: RTM_DELETE, error=%d\n", __func__, error);
1113 setfs(struct sockaddr_in *addr, char *path, char *p,
1114 const struct in_addr *siaddr)
1117 if (getip(&p, &addr->sin_addr) == 0) {
1118 if (siaddr != NULL && *p == '/')
1119 bcopy(siaddr, &addr->sin_addr, sizeof(struct in_addr));
1128 addr->sin_len = sizeof(struct sockaddr_in);
1129 addr->sin_family = AF_INET;
1131 strlcpy(path, p, MNAMELEN);
1136 getip(char **ptr, struct in_addr *addr)
1144 if (((val = getdec(&p)) < 0) || (val > 255))
1150 if (((val = getdec(&p)) < 0) || (val > 255))
1156 if (((val = getdec(&p)) < 0) || (val > 255))
1162 if (((val = getdec(&p)) < 0) || (val > 255))
1166 addr->s_addr = htonl(ip);
1179 if ((*p < '0') || (*p > '9'))
1181 while ((*p >= '0') && (*p <= '9')) {
1182 ret = ret * 10 + (*p - '0');
1190 mountopts(struct nfs_args *args, char *p)
1192 args->version = NFS_ARGSVERSION;
1193 args->rsize = BOOTP_BLOCKSIZE;
1194 args->wsize = BOOTP_BLOCKSIZE;
1195 args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
1196 args->sotype = SOCK_DGRAM;
1198 nfs_parse_options(p, args);
1202 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
1208 alignedlen = ( len + 3 ) & ~3;
1210 if (m->m_len < alignedlen) {
1211 m = m_pullup(m, alignedlen);
1217 bcopy(mtod(m, u_char *), buf, len);
1218 m_adj(m, alignedlen);
1224 xdr_int_decode(struct mbuf **mptr, int *iptr)
1228 if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
1230 *iptr = fxdr_unsigned(u_int32_t, i);
1235 print_sin_addr(struct sockaddr_in *sin)
1238 print_in_addr(sin->sin_addr);
1242 print_in_addr(struct in_addr addr)
1246 ip = ntohl(addr.s_addr);
1247 printf("%d.%d.%d.%d",
1248 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1252 bootpc_compose_query(struct bootpc_ifcontext *ifctx, struct thread *td)
1254 unsigned char *vendp;
1255 unsigned char vendor_client[64];
1257 uint8_t vendor_client_len;
1259 ifctx->gotrootpath = 0;
1261 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1264 ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */
1265 ifctx->call.htype = 1; /* 10mb ethernet */
1266 ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1267 ifctx->call.hops = 0;
1268 if (bootpc_ifctx_isunresolved(ifctx) != 0)
1270 ifctx->call.xid = txdr_unsigned(ifctx->xid);
1271 bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1273 vendp = ifctx->call.vend;
1274 *vendp++ = 99; /* RFC1048 cookie */
1278 *vendp++ = TAG_MAXMSGSIZE;
1280 *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1281 *vendp++ = sizeof(struct bootp_packet) & 255;
1283 snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1284 ostype, MACHINE, osrelease);
1285 vendor_client_len = strlen(vendor_client);
1286 *vendp++ = TAG_VENDOR_INDENTIFIER;
1287 *vendp++ = vendor_client_len;
1288 memcpy(vendp, vendor_client, vendor_client_len);
1289 vendp += vendor_client_len;
1290 ifctx->dhcpquerytype = DHCP_NOMSG;
1291 switch (ifctx->state) {
1292 case IF_DHCP_UNRESOLVED:
1293 *vendp++ = TAG_DHCP_MSGTYPE;
1295 *vendp++ = DHCP_DISCOVER;
1296 ifctx->dhcpquerytype = DHCP_DISCOVER;
1297 ifctx->gotdhcpserver = 0;
1299 case IF_DHCP_OFFERED:
1300 *vendp++ = TAG_DHCP_MSGTYPE;
1302 *vendp++ = DHCP_REQUEST;
1303 ifctx->dhcpquerytype = DHCP_REQUEST;
1304 *vendp++ = TAG_DHCP_REQ_ADDR;
1306 memcpy(vendp, &ifctx->reply.yiaddr, 4);
1308 if (ifctx->gotdhcpserver != 0) {
1309 *vendp++ = TAG_DHCP_SERVERID;
1311 memcpy(vendp, &ifctx->dhcpserver, 4);
1314 *vendp++ = TAG_DHCP_LEASETIME;
1316 leasetime = htonl(300);
1317 memcpy(vendp, &leasetime, 4);
1325 ifctx->call.secs = 0;
1326 ifctx->call.flags = htons(0x8000); /* We need a broadcast answer */
1330 bootpc_hascookie(struct bootp_packet *bp)
1333 return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1334 bp->vend[2] == 83 && bp->vend[3] == 99);
1338 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1339 unsigned char *start, int len, int tag)
1345 if (tctx->badtag != 0 || tctx->badopt != 0)
1353 if (code == TAG_PAD)
1355 if (code == TAG_END)
1357 if (j >= ej || j + *j + 1 > ej) {
1363 if (tctx->taglen + len > TAG_MAXLEN) {
1369 memcpy(tctx->buf + tctx->taglen,
1371 tctx->taglen += len;
1373 if (code == TAG_OVERLOAD)
1374 tctx->overload = *j;
1380 static unsigned char *
1381 bootpc_tag(struct bootpc_tagcontext *tctx,
1382 struct bootp_packet *bp, int len, int tag)
1390 if (bootpc_hascookie(bp) == 0)
1393 bootpc_tag_helper(tctx, &bp->vend[4],
1394 (unsigned char *) bp + len - &bp->vend[4], tag);
1396 if ((tctx->overload & OVERLOAD_FILE) != 0)
1397 bootpc_tag_helper(tctx,
1398 (unsigned char *) bp->file,
1401 if ((tctx->overload & OVERLOAD_SNAME) != 0)
1402 bootpc_tag_helper(tctx,
1403 (unsigned char *) bp->sname,
1407 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1409 tctx->buf[tctx->taglen] = '\0';
1414 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1415 struct bootpc_globalcontext *gctx)
1421 ifctx->gotnetmask = 0;
1423 clear_sinaddr(&ifctx->myaddr);
1424 clear_sinaddr(&ifctx->netmask);
1425 clear_sinaddr(&ifctx->gw);
1427 ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1429 ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1431 printf("%s at ", ifctx->ireq.ifr_name);
1432 print_sin_addr(&ifctx->myaddr);
1434 print_in_addr(ifctx->reply.siaddr);
1436 ifctx->gw.sin_addr = ifctx->reply.giaddr;
1437 if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1438 printf(" via gateway ");
1439 print_in_addr(ifctx->reply.giaddr);
1442 /* This call used for the side effect (overload flag) */
1443 (void) bootpc_tag(&gctx->tmptag,
1444 &ifctx->reply, ifctx->replylen, TAG_END);
1446 if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1447 if (ifctx->reply.sname[0] != '\0')
1448 printf(" server name %s", ifctx->reply.sname);
1449 if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1450 if (ifctx->reply.file[0] != '\0')
1451 printf(" boot file %s", ifctx->reply.file);
1455 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1458 if (gctx->tag.taglen != 4)
1459 panic("bootpc: subnet mask len is %d",
1461 bcopy(p, &ifctx->netmask.sin_addr, 4);
1462 ifctx->gotnetmask = 1;
1463 printf("subnet mask ");
1464 print_sin_addr(&ifctx->netmask);
1468 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1472 if (gctx->tag.taglen % 4)
1473 panic("bootpc: Router Len is %d", gctx->tag.taglen);
1474 if (gctx->tag.taglen > 0) {
1475 bcopy(p, &ifctx->gw.sin_addr, 4);
1477 print_sin_addr(&ifctx->gw);
1485 * Choose a root filesystem. If a value is forced in the environment
1486 * and it contains "nfs:", use it unconditionally. Otherwise, if the
1487 * kernel is compiled with the ROOTDEVNAME option, then use it if:
1488 * - The server doesn't provide a pathname.
1489 * - The boothowto flags include RB_DFLTROOT (user said to override
1490 * the server value).
1493 if ((s = kern_getenv("vfs.root.mountfrom")) != NULL) {
1494 if ((p = strstr(s, "nfs:")) != NULL)
1495 p = strdup(p + 4, M_TEMP);
1499 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1502 ifctx->gotrootpath = 1;
1505 if ((p == NULL || (boothowto & RB_DFLTROOT) != 0) &&
1506 (p = strstr(ROOTDEVNAME, "nfs:")) != NULL) {
1511 if (gctx->setrootfs != NULL) {
1512 printf("rootfs %s (ignored) ", p);
1513 } else if (setfs(&nd->root_saddr,
1514 nd->root_hostnam, p, &ifctx->reply.siaddr)) {
1516 printf("root_server ");
1517 print_sin_addr(&nd->root_saddr);
1520 printf("rootfs %s ", p);
1521 gctx->gotrootpath = 1;
1522 gctx->setrootfs = ifctx;
1524 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1528 mountopts(&nd->root_args, p);
1529 printf("rootopts %s ", p);
1532 panic("Failed to set rootfs to %s", p);
1535 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1538 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1539 panic("bootpc: hostname >= %d bytes",
1541 if (gctx->sethostname != NULL) {
1542 printf("hostname %s (ignored) ", p);
1544 strcpy(nd->my_hostnam, p);
1545 mtx_lock(&prison0.pr_mtx);
1546 strcpy(prison0.pr_hostname, p);
1547 mtx_unlock(&prison0.pr_mtx);
1548 printf("hostname %s ", p);
1549 gctx->sethostname = ifctx;
1552 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1554 if (p != NULL) { /* store in a sysctl variable */
1555 int i, l = sizeof(bootp_cookie) - 1;
1556 for (i = 0; i < l && p[i] != '\0'; i++)
1557 bootp_cookie[i] = p[i];
1561 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1564 ifctx->mtu = be16dec(p);
1569 if (ifctx->gotnetmask == 0) {
1570 if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1571 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1572 else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1573 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1575 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1582 struct bootpc_ifcontext *ifctx; /* Interface BOOTP contexts */
1583 struct bootpc_globalcontext *gctx; /* Global BOOTP context */
1585 struct sockaddr_dl *sdl;
1588 #ifndef BOOTP_WIRED_TO
1591 struct nfsv3_diskless *nd;
1596 timeout = BOOTP_IFACE_WAIT_TIMEOUT * hz;
1599 nd = &nfsv3_diskless;
1603 * If already filled in, don't touch it here
1605 if (nfs_diskless_valid != 0)
1608 gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK | M_ZERO);
1609 STAILQ_INIT(&gctx->interfaces);
1610 gctx->xid = ~0xFFFF;
1611 gctx->starttime = time_second;
1614 * If ROOTDEVNAME is defined or vfs.root.mountfrom is set then we have
1615 * root-path overrides that can potentially let us boot even if we don't
1616 * get a root path from the server, so we can treat that as a non-error.
1619 gctx->any_root_overrides = 1;
1621 gctx->any_root_overrides = testenv("vfs.root.mountfrom");
1625 * Find a network interface.
1627 CURVNET_SET(TD_TO_VNET(td));
1628 #ifdef BOOTP_WIRED_TO
1629 printf("%s: wired to interface '%s'\n", __func__,
1630 __XSTRING(BOOTP_WIRED_TO));
1634 * Preallocate interface context storage, if another interface
1635 * attaches and wins the race, it won't be eligible for bootp.
1639 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1640 if ((ifp->if_flags &
1641 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1644 switch (ifp->if_alloctype) {
1656 panic("%s: no eligible interfaces", __func__);
1657 for (; ifcnt > 0; ifcnt--)
1662 ifctx = STAILQ_FIRST(&gctx->interfaces);
1664 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1667 #ifdef BOOTP_WIRED_TO
1668 if (strcmp(ifp->if_xname, __XSTRING(BOOTP_WIRED_TO)) != 0)
1671 if ((ifp->if_flags &
1672 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1675 switch (ifp->if_alloctype) {
1684 strlcpy(ifctx->ireq.ifr_name, ifp->if_xname,
1685 sizeof(ifctx->ireq.ifr_name));
1688 /* Get HW address */
1690 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1691 if (ifa->ifa_addr->sa_family == AF_LINK) {
1692 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1693 if (sdl->sdl_type == IFT_ETHER)
1697 panic("bootpc: Unable to find HW address for %s",
1698 ifctx->ireq.ifr_name);
1701 ifctx = STAILQ_NEXT(ifctx, next);
1706 if (STAILQ_EMPTY(&gctx->interfaces) ||
1707 STAILQ_FIRST(&gctx->interfaces)->ifp == NULL) {
1709 pause("bootpc", delay);
1713 #ifdef BOOTP_WIRED_TO
1714 panic("%s: Could not find interface specified "
1715 "by BOOTP_WIRED_TO: "
1716 __XSTRING(BOOTP_WIRED_TO), __func__);
1718 panic("%s: no suitable interface", __func__);
1722 error = socreate(AF_INET, &bootp_so, SOCK_DGRAM, 0, td->td_ucred, td);
1724 panic("%s: socreate, error=%d", __func__, error);
1726 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1727 bootpc_fakeup_interface(ifctx, td);
1729 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1730 bootpc_compose_query(ifctx, td);
1732 error = bootpc_call(gctx, td);
1734 printf("BOOTP call failed\n");
1737 mountopts(&nd->root_args, NULL);
1739 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1740 if (bootpc_ifctx_isresolved(ifctx) != 0)
1741 bootpc_decode_reply(nd, ifctx, gctx);
1743 #ifdef BOOTP_NFSROOT
1744 if (gctx->gotrootpath == 0 && gctx->any_root_overrides == 0)
1745 panic("bootpc: No root path offered");
1748 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1749 bootpc_adjust_interface(ifctx, gctx, td);
1753 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1754 if (ifctx->gotrootpath != 0)
1756 if (ifctx == NULL) {
1757 STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
1758 if (bootpc_ifctx_isresolved(ifctx) != 0)
1764 if (gctx->gotrootpath != 0) {
1766 kern_setenv("boot.netif.name", ifctx->ifp->if_xname);
1768 bootpc_add_default_route(ifctx);
1769 error = md_mount(&nd->root_saddr, nd->root_hostnam,
1770 nd->root_fh, &nd->root_fhsize,
1771 &nd->root_args, td);
1772 bootpc_remove_default_route(ifctx);
1774 if (gctx->any_root_overrides == 0)
1775 panic("nfs_boot: mount root, error=%d", error);
1779 rootdevnames[0] = "nfs:";
1780 nfs_diskless_valid = 3;
1783 strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1784 bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1785 bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1786 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1787 ifctx->myaddr.sin_addr.s_addr |
1788 ~ ifctx->netmask.sin_addr.s_addr;
1789 bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1790 bcopy(&ifctx->gw, &nd->mygateway, sizeof(ifctx->gw));
1793 while((ifctx = STAILQ_FIRST(&gctx->interfaces)) != NULL) {
1794 STAILQ_REMOVE_HEAD(&gctx->interfaces, next);
1795 free(ifctx, M_TEMP);
1802 * Given a server pathname, get an NFS file handle.
1803 * Also, sets sin->sin_port to the NFS service port.
1806 md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp, int *fhsizep,
1807 struct nfs_args *args, struct thread *td)
1815 #define RPCPROG_MNT 100005
1816 #define RPCMNT_VER1 1
1817 #define RPCMNT_VER3 3
1818 #define RPCMNT_MOUNT 1
1819 #define AUTH_SYS 1 /* unix style (uid, gids) */
1820 #define AUTH_UNIX AUTH_SYS
1822 /* XXX honor v2/v3 flags in args->flags? */
1824 /* First try NFS v3 */
1825 /* Get port number for MOUNTD. */
1826 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1827 &mdsin->sin_port, td);
1829 m = xdr_string_encode(path, strlen(path));
1831 /* Do RPC to mountd. */
1832 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1833 RPCMNT_MOUNT, &m, NULL, td);
1836 args->flags |= NFSMNT_NFSV3;
1839 /* Fallback to NFS v2 */
1841 /* Get port number for MOUNTD. */
1842 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1843 &mdsin->sin_port, td);
1847 m = xdr_string_encode(path, strlen(path));
1849 /* Do RPC to mountd. */
1850 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1851 RPCMNT_MOUNT, &m, NULL, td);
1853 return error; /* message already freed */
1859 if (xdr_int_decode(&m, &error) != 0 || error != 0)
1862 if ((args->flags & NFSMNT_NFSV3) != 0) {
1863 if (xdr_int_decode(&m, fhsizep) != 0 ||
1864 *fhsizep > NFSX_V3FHMAX ||
1868 *fhsizep = NFSX_V2FH;
1870 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
1873 if (args->flags & NFSMNT_NFSV3) {
1874 if (xdr_int_decode(&m, &authcount) != 0)
1877 if (authcount < 0 || authcount > 100)
1879 while (authcount > 0) {
1880 if (xdr_int_decode(&m, &authver) != 0)
1882 if (authver == AUTH_UNIX)
1886 if (authunixok == 0)
1890 /* Set port number for NFS use. */
1891 error = krpc_portmap(mdsin, NFS_PROG,
1893 NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
1894 &mdsin->sin_port, td);
1906 SYSINIT(bootp_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, bootpc_init, NULL);