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 <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/sockio.h>
52 #include <sys/malloc.h>
53 #include <sys/mount.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/sysctl.h>
62 #include <net/route.h>
64 #include <netinet/in.h>
65 #include <net/if_types.h>
66 #include <net/if_dl.h>
68 #include <nfs/rpcv2.h>
69 #include <nfs/nfsproto.h>
70 #include <nfsclient/nfs.h>
71 #include <nfsclient/nfsdiskless.h>
72 #include <nfsclient/krpc.h>
73 #include <nfs/xdr_subs.h>
76 #define BOOTP_MIN_LEN 300 /* Minimum size of bootp udp packet */
78 #ifndef BOOTP_SETTLE_DELAY
79 #define BOOTP_SETTLE_DELAY 3
83 * What is the longest we will wait before re-sending a request?
84 * Note this is also the frequency of "RPC timeout" messages.
85 * The re-send loop count sup linearly to this maximum, so the
86 * first complaint will happen after (1+2+3+4+5)=15 seconds.
88 #define MAX_RESEND_DELAY 5 /* seconds */
90 /* Definitions from RFC951 */
99 struct in_addr ciaddr;
100 struct in_addr yiaddr;
101 struct in_addr siaddr;
102 struct in_addr giaddr;
103 unsigned char chaddr[16];
106 unsigned char vend[1222];
109 struct bootpc_ifcontext {
110 struct bootpc_ifcontext *next;
111 struct bootp_packet call;
112 struct bootp_packet reply;
118 struct sockaddr_dl *sdl;
119 struct sockaddr_in myaddr;
120 struct sockaddr_in netmask;
121 struct sockaddr_in gw;
122 struct sockaddr_in broadcast; /* Different for each interface */
138 int dhcpquerytype; /* dhcp type sent */
139 struct in_addr dhcpserver;
143 #define TAG_MAXLEN 1024
144 struct bootpc_tagcontext {
145 char buf[TAG_MAXLEN + 1];
153 struct bootpc_globalcontext {
154 struct bootpc_ifcontext *interfaces;
155 struct bootpc_ifcontext *lastinterface;
163 struct bootp_packet reply;
165 struct bootpc_ifcontext *setswapfs;
166 struct bootpc_ifcontext *setrootfs;
167 struct bootpc_ifcontext *sethostname;
168 char lookup_path[24];
169 struct bootpc_tagcontext tmptag;
170 struct bootpc_tagcontext tag;
173 #define IPPORT_BOOTPC 68
174 #define IPPORT_BOOTPS 67
176 #define BOOTP_REQUEST 1
177 #define BOOTP_REPLY 2
180 #define TAG_PAD 0 /* Pad option, implicit length 1 */
181 #define TAG_SUBNETMASK 1 /* RFC 950 subnet mask */
182 #define TAG_ROUTERS 3 /* Routers (in order of preference) */
183 #define TAG_HOSTNAME 12 /* Client host name */
184 #define TAG_ROOT 17 /* Root path */
186 /* DHCP specific tags */
187 #define TAG_OVERLOAD 52 /* Option Overload */
188 #define TAG_MAXMSGSIZE 57 /* Maximum DHCP Message Size */
190 #define TAG_END 255 /* End Option (i.e. no more options) */
192 /* Overload values */
193 #define OVERLOAD_FILE 1
194 #define OVERLOAD_SNAME 2
196 /* Site specific tags: */
198 #define TAG_SWAPSIZE 129
199 #define TAG_ROOTOPTS 130
200 #define TAG_SWAPOPTS 131
201 #define TAG_COOKIE 134 /* ascii info for userland, via sysctl */
203 #define TAG_DHCP_MSGTYPE 53
204 #define TAG_DHCP_REQ_ADDR 50
205 #define TAG_DHCP_SERVERID 54
206 #define TAG_DHCP_LEASETIME 51
208 #define TAG_VENDOR_INDENTIFIER 60
211 #define DHCP_DISCOVER 1
213 #define DHCP_REQUEST 3
216 static char bootp_cookie[128];
217 SYSCTL_STRING(_kern, OID_AUTO, bootp_cookie, CTLFLAG_RD,
218 bootp_cookie, 0, "Cookie (T134) supplied by bootp server");
221 static int md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp,
222 int *fhsizep, struct nfs_args *args, struct thread *td);
223 static int md_lookup_swap(struct sockaddr_in *mdsin, char *path,
224 u_char *fhp, int *fhsizep, struct nfs_args *args,
226 static int setfs(struct sockaddr_in *addr, char *path, char *p);
227 static int getdec(char **ptr);
228 static char *substr(char *a, char *b);
229 static void mountopts(struct nfs_args *args, char *p);
230 static int xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len);
231 static int xdr_int_decode(struct mbuf **ptr, int *iptr);
232 static void print_in_addr(struct in_addr addr);
233 static void print_sin_addr(struct sockaddr_in *addr);
234 static void clear_sinaddr(struct sockaddr_in *sin);
235 static struct bootpc_ifcontext *allocifctx(struct bootpc_globalcontext *gctx);
236 static void bootpc_compose_query(struct bootpc_ifcontext *ifctx,
237 struct bootpc_globalcontext *gctx, struct thread *td);
238 static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx,
239 struct bootp_packet *bp, int len, int tag);
240 static void bootpc_tag_helper(struct bootpc_tagcontext *tctx,
241 unsigned char *start, int len, int tag);
244 void bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma);
245 void bootpboot_p_ma(struct sockaddr *ma);
246 void bootpboot_p_rtentry(struct rtentry *rt);
247 void bootpboot_p_tree(struct radix_node *rn);
248 void bootpboot_p_rtlist(void);
249 void bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa);
250 void bootpboot_p_iflist(void);
253 static int bootpc_call(struct bootpc_globalcontext *gctx,
256 static int bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
257 struct bootpc_globalcontext *gctx, struct thread *td);
259 static int bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
260 struct bootpc_globalcontext *gctx, struct thread *td);
262 static void bootpc_decode_reply(struct nfsv3_diskless *nd,
263 struct bootpc_ifcontext *ifctx,
264 struct bootpc_globalcontext *gctx);
266 static int bootpc_received(struct bootpc_globalcontext *gctx,
267 struct bootpc_ifcontext *ifctx);
269 static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
270 static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
271 static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
274 * In order to have multiple active interfaces with address 0.0.0.0
275 * and be able to send data to a selected interface, we perform
278 * - The 'broadcast' address is different for each interface.
280 * - We temporarily add routing pointing 255.255.255.255 to the
281 * selected interface broadcast address, thus the packet sent
282 * goes to that interface.
287 bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma)
291 printf("(sockaddr *) <null>");
294 switch (sa->sa_family) {
297 struct sockaddr_in *sin;
299 sin = (struct sockaddr_in *) sa;
303 sin = (struct sockaddr_in *) ma;
311 struct sockaddr_dl *sli;
314 sli = (struct sockaddr_dl *) sa;
315 printf("link %.*s ", sli->sdl_nlen, sli->sdl_data);
316 for (i = 0; i < sli->sdl_alen; i++) {
319 printf("%x", ((unsigned char *) LLADDR(sli))[i]);
324 printf("af%d", sa->sa_family);
329 bootpboot_p_ma(struct sockaddr *ma)
336 printf("%x", *(int *)ma);
340 bootpboot_p_rtentry(struct rtentry *rt)
343 bootpboot_p_sa(rt_key(rt), rt_mask(rt));
345 bootpboot_p_ma(rt->rt_genmask);
347 bootpboot_p_sa(rt->rt_gateway, NULL);
349 printf("flags %x", (unsigned short) rt->rt_flags);
350 printf(" %d", (int) rt->rt_rmx.rmx_expire);
351 printf(" %s%d\n", rt->rt_ifp->if_name, rt->rt_ifp->if_unit);
355 bootpboot_p_tree(struct radix_node *rn)
359 if (rn->rn_bit < 0) {
360 if ((rn->rn_flags & RNF_ROOT) != 0) {
362 bootpboot_p_rtentry((struct rtentry *) rn);
364 rn = rn->rn_dupedkey;
366 bootpboot_p_tree(rn->rn_left);
367 bootpboot_p_tree(rn->rn_right);
374 bootpboot_p_rtlist(void)
377 printf("Routing table:\n");
378 bootpboot_p_tree(rt_tables[AF_INET]->rnh_treetop);
382 bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
385 printf("%s%d flags %x, addr ",
389 print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
390 printf(", broadcast ");
391 print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
392 printf(", netmask ");
393 print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
398 bootpboot_p_iflist(void)
403 printf("Interface list:\n");
404 for (ifp = TAILQ_FIRST(&ifnet);
406 ifp = TAILQ_NEXT(ifp, if_link)) {
407 for (ifa = TAILQ_FIRST(&ifp->if_addrhead);
409 ifa = TAILQ_NEXT(ifa, ifa_link))
410 if (ifa->ifa_addr->sa_family == AF_INET)
411 bootpboot_p_if(ifp, ifa);
414 #endif /* defined(BOOTP_DEBUG) */
417 clear_sinaddr(struct sockaddr_in *sin)
420 bzero(sin, sizeof(*sin));
421 sin->sin_len = sizeof(*sin);
422 sin->sin_family = AF_INET;
423 sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
427 static struct bootpc_ifcontext *
428 allocifctx(struct bootpc_globalcontext *gctx)
430 struct bootpc_ifcontext *ifctx;
431 ifctx = (struct bootpc_ifcontext *) malloc(sizeof(*ifctx),
434 panic("Failed to allocate bootp interface context structure");
436 bzero(ifctx, sizeof(*ifctx));
437 ifctx->xid = gctx->xid;
439 ifctx->state = IF_BOOTP_UNRESOLVED;
441 ifctx->state = IF_DHCP_UNRESOLVED;
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)
580 struct sockaddr_in *sin, dst;
584 int error, on, rcvflg, timo, len;
588 struct bootpc_ifcontext *ifctx;
595 * Create socket and set its recieve timeout.
597 error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td->td_ucred, td);
603 bzero(&sopt, sizeof(sopt));
604 sopt.sopt_level = SOL_SOCKET;
605 sopt.sopt_name = SO_RCVTIMEO;
607 sopt.sopt_valsize = sizeof tv;
609 error = sosetopt(so, &sopt);
617 sopt.sopt_name = SO_BROADCAST;
619 sopt.sopt_valsize = sizeof on;
621 error = sosetopt(so, &sopt);
630 sopt.sopt_name = SO_DONTROUTE;
632 sopt.sopt_valsize = sizeof on;
634 error = sosetopt(so, &sopt);
639 * Bind the local endpoint to a bootp client port.
643 sin->sin_port = htons(IPPORT_BOOTPC);
644 error = sobind(so, (struct sockaddr *)sin, td);
646 printf("bind failed\n");
651 * Setup socket address for the server.
655 sin->sin_addr.s_addr = INADDR_BROADCAST;
656 sin->sin_port = htons(IPPORT_BOOTPS);
659 * Send it, repeatedly, until a reply is received,
660 * but delay each re-send by an increasing amount.
661 * If the delay hits the maximum, start complaining.
670 for (ifctx = gctx->interfaces;
672 ifctx = ifctx->next) {
673 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
674 bootpc_tag(&gctx->tmptag, &ifctx->reply,
680 for (ifctx = gctx->interfaces;
682 ifctx = ifctx->next) {
683 ifctx->outstanding = 0;
684 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
688 if (bootpc_ifctx_isfailed(ifctx) != 0)
692 ifctx->outstanding = 1;
694 /* Proceed to next step in DHCP negotiation */
695 if ((ifctx->state == IF_DHCP_OFFERED &&
696 ifctx->dhcpquerytype != DHCP_REQUEST) ||
697 (ifctx->state == IF_DHCP_UNRESOLVED &&
698 ifctx->dhcpquerytype != DHCP_DISCOVER) ||
699 (ifctx->state == IF_BOOTP_UNRESOLVED &&
700 ifctx->dhcpquerytype != DHCP_NOMSG)) {
702 bootpc_compose_query(ifctx, gctx, td);
705 /* Send BOOTP request (or re-send). */
707 if (ifctx->sentmsg == 0) {
708 switch(ifctx->dhcpquerytype) {
720 printf("Sending %s packet from "
721 "interface %s (%*D)\n",
723 ifctx->ireq.ifr_name,
724 ifctx->sdl->sdl_alen,
725 (unsigned char *) LLADDR(ifctx->sdl),
730 aio.iov_base = (caddr_t) &ifctx->call;
731 aio.iov_len = sizeof(ifctx->call);
735 auio.uio_segflg = UIO_SYSSPACE;
736 auio.uio_rw = UIO_WRITE;
738 auio.uio_resid = sizeof(ifctx->call);
741 /* Set netmask to 0.0.0.0 */
743 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
745 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
746 (caddr_t) &ifctx->ireq, td);
749 "set if netmask, error=%d",
752 error = sosend(so, (struct sockaddr *) &dst,
753 &auio, NULL, NULL, 0, td);
755 printf("bootpc_call: sosend: %d state %08x\n",
756 error, (int) so->so_state);
759 /* XXX: Is this needed ? */
760 tsleep(&error, PZERO + 8, "bootpw", 10);
762 /* Set netmask to 255.0.0.0 */
764 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
766 sin->sin_addr.s_addr = htonl(0xff000000u);
767 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
768 (caddr_t) &ifctx->ireq, td);
771 "set if netmask, error=%d",
776 if (outstanding == 0 &&
777 (rtimo == 0 || time_second >= rtimo)) {
782 /* Determine new timeout. */
783 if (timo < MAX_RESEND_DELAY)
786 printf("DHCP/BOOTP timeout for server ");
787 print_sin_addr(&dst);
792 * Wait for up to timo seconds for a reply.
793 * The socket receive timeout was set to 1 second.
795 atimo = timo + time_second;
796 while (time_second < atimo) {
797 aio.iov_base = (caddr_t) &gctx->reply;
798 aio.iov_len = sizeof(gctx->reply);
802 auio.uio_segflg = UIO_SYSSPACE;
803 auio.uio_rw = UIO_READ;
805 auio.uio_resid = sizeof(gctx->reply);
809 error = soreceive(so, NULL, &auio,
810 NULL, NULL, &rcvflg);
811 gctx->secs = time_second - gctx->starttime;
812 for (ifctx = gctx->interfaces;
814 ifctx = ifctx->next) {
815 if (bootpc_ifctx_isresolved(ifctx) != 0 ||
816 bootpc_ifctx_isfailed(ifctx) != 0)
819 ifctx->call.secs = htons(gctx->secs);
821 if (error == EWOULDBLOCK)
825 len = sizeof(gctx->reply) - auio.uio_resid;
827 /* Do we have the required number of bytes ? */
828 if (len < BOOTP_MIN_LEN)
830 gctx->replylen = len;
833 if (gctx->reply.op != BOOTP_REPLY)
836 /* Is this an answer to our query */
837 for (ifctx = gctx->interfaces;
839 ifctx = ifctx->next) {
840 if (gctx->reply.xid != ifctx->call.xid)
843 /* Same HW address size ? */
844 if (gctx->reply.hlen != ifctx->call.hlen)
847 /* Correct HW address ? */
848 if (bcmp(gctx->reply.chaddr,
850 ifctx->call.hlen) != 0)
857 s = bootpc_tag(&gctx->tmptag,
870 s = "DHCP (unexpected)";
876 printf("Received %s packet"
879 ifctx->ireq.ifr_name);
880 print_in_addr(gctx->reply.siaddr);
881 if (gctx->reply.giaddr.s_addr !=
884 print_in_addr(gctx->reply.giaddr);
886 if (bootpc_received(gctx, ifctx) != 0) {
887 printf(" (accepted)");
888 if (ifctx->outstanding) {
889 ifctx->outstanding = 0;
892 /* Network settle delay */
893 if (outstanding == 0)
894 atimo = time_second +
897 printf(" (ignored)");
898 if (ifctx->gotrootpath) {
900 rtimo = time_second +
902 printf(" (got root path)");
904 printf(" (no root path)");
909 if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
912 /* Force a retry if halfway in DHCP negotiation */
914 for (ifctx = gctx->interfaces; ifctx != NULL;
915 ifctx = ifctx->next) {
916 if (ifctx->state == IF_DHCP_OFFERED) {
917 if (ifctx->dhcpquerytype == DHCP_DISCOVER)
920 ifctx->state = IF_DHCP_UNRESOLVED;
927 if (gotrootpath != 0) {
928 gctx->gotrootpath = gotrootpath;
929 if (rtimo != 0 && time_second >= rtimo)
932 } /* forever send/receive */
935 * XXX: These are errors of varying seriousness being silently
939 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
940 if (bootpc_ifctx_isresolved(ifctx) == 0) {
941 printf("%s timeout for interface %s\n",
942 ifctx->dhcpquerytype != DHCP_NOMSG ?
944 ifctx->ireq.ifr_name);
947 if (gctx->gotrootpath != 0) {
949 printf("Got a root path, ignoring remaining timeout\n");
954 #ifndef BOOTP_NFSROOT
955 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
956 if (bootpc_ifctx_isresolved(ifctx) != 0) {
972 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
973 struct bootpc_globalcontext *gctx, struct thread *td)
975 struct sockaddr_in *sin;
980 struct sockaddr_dl *sdl;
982 error = socreate(AF_INET, &ifctx->so, SOCK_DGRAM, 0, td->td_ucred, td);
984 panic("nfs_boot: socreate, error=%d", error);
990 * Bring up the interface.
992 * Get the old interface flags and or IFF_UP into them; if
993 * IFF_UP set blindly, interface selection can be clobbered.
995 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, td);
997 panic("bootpc_fakeup_interface: GIFFLAGS, error=%d", error);
998 ireq->ifr_flags |= IFF_UP;
999 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, td);
1001 panic("bootpc_fakeup_interface: SIFFLAGS, error=%d", error);
1004 * Do enough of ifconfig(8) so that the chosen interface
1005 * can talk to the servers. (just set the address)
1008 /* addr is 0.0.0.0 */
1010 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1012 error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, td);
1013 if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1014 panic("bootpc_fakeup_interface: "
1015 "set if addr, error=%d", error);
1017 /* netmask is 255.0.0.0 */
1019 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1021 sin->sin_addr.s_addr = htonl(0xff000000u);
1022 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t)ireq, td);
1024 panic("bootpc_fakeup_interface: set if netmask, error=%d",
1027 /* Broadcast is 255.255.255.255 */
1029 sin = (struct sockaddr_in *)&ireq->ifr_addr;
1031 clear_sinaddr(&ifctx->broadcast);
1032 sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
1033 ifctx->broadcast.sin_addr.s_addr = sin->sin_addr.s_addr;
1035 error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t)ireq, td);
1037 panic("bootpc_fakeup_interface: "
1038 "set if broadcast addr, error=%d",
1041 /* Get HW address */
1044 for (ifa = TAILQ_FIRST(&ifctx->ifp->if_addrhead);
1046 ifa = TAILQ_NEXT(ifa, ifa_link))
1047 if (ifa->ifa_addr->sa_family == AF_LINK &&
1048 (sdl = ((struct sockaddr_dl *) ifa->ifa_addr)) != NULL &&
1049 sdl->sdl_type == IFT_ETHER)
1053 panic("bootpc: Unable to find HW address for %s",
1054 ifctx->ireq.ifr_name);
1062 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
1063 struct bootpc_globalcontext *gctx, struct thread *td)
1066 struct sockaddr_in defdst;
1067 struct sockaddr_in defmask;
1068 struct sockaddr_in *sin;
1071 struct sockaddr_in *myaddr;
1072 struct sockaddr_in *netmask;
1073 struct sockaddr_in *gw;
1075 ireq = &ifctx->ireq;
1077 myaddr = &ifctx->myaddr;
1078 netmask = &ifctx->netmask;
1081 if (bootpc_ifctx_isresolved(ifctx) == 0) {
1083 /* Shutdown interfaces where BOOTP failed */
1085 printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
1086 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, td);
1088 panic("bootpc_adjust_interface: "
1089 "SIOCGIFFLAGS, error=%d", error);
1090 ireq->ifr_flags &= ~IFF_UP;
1091 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, td);
1093 panic("bootpc_adjust_interface: "
1094 "SIOCSIFFLAGS, error=%d", error);
1096 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1098 error = ifioctl(so, SIOCDIFADDR, (caddr_t) ireq, td);
1099 if (error != 0 && (error != EEXIST ||
1100 ifctx == gctx->interfaces))
1101 panic("bootpc_adjust_interface: "
1102 "SIOCDIFADDR, error=%d", error);
1107 printf("Adjusted interface %s\n", ifctx->ireq.ifr_name);
1109 * Do enough of ifconfig(8) so that the chosen interface
1110 * can talk to the servers. (just set the address)
1112 bcopy(netmask, &ireq->ifr_addr, sizeof(*netmask));
1113 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t) ireq, td);
1115 panic("bootpc_adjust_interface: "
1116 "set if netmask, error=%d", error);
1118 /* Broadcast is with host part of IP address all 1's */
1120 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1122 sin->sin_addr.s_addr = myaddr->sin_addr.s_addr |
1123 ~ netmask->sin_addr.s_addr;
1124 error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t) ireq, td);
1126 panic("bootpc_adjust_interface: "
1127 "set if broadcast addr, error=%d", error);
1129 bcopy(myaddr, &ireq->ifr_addr, sizeof(*myaddr));
1130 error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, td);
1131 if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1132 panic("bootpc_adjust_interface: "
1133 "set if addr, error=%d", error);
1135 /* Add new default route */
1137 if (ifctx->gotgw != 0 || gctx->gotgw == 0) {
1138 clear_sinaddr(&defdst);
1139 clear_sinaddr(&defmask);
1140 error = rtrequest(RTM_ADD,
1141 (struct sockaddr *) &defdst,
1142 (struct sockaddr *) gw,
1143 (struct sockaddr *) &defmask,
1144 (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
1146 printf("bootpc_adjust_interface: "
1147 "add net route, error=%d\n", error);
1156 setfs(struct sockaddr_in *addr, char *path, char *p)
1162 if (((val = getdec(&p)) < 0) || (val > 255))
1168 if (((val = getdec(&p)) < 0) || (val > 255))
1174 if (((val = getdec(&p)) < 0) || (val > 255))
1180 if (((val = getdec(&p)) < 0) || (val > 255))
1187 addr->sin_addr.s_addr = htonl(ip);
1188 addr->sin_len = sizeof(struct sockaddr_in);
1189 addr->sin_family = AF_INET;
1191 strncpy(path, p, MNAMELEN - 1);
1203 if ((*p < '0') || (*p > '9'))
1205 while ((*p >= '0') && (*p <= '9')) {
1206 ret = ret * 10 + (*p - '0');
1214 substr(char *a, char *b)
1219 while (*a != '\0') {
1222 while (*loc1 == *loc2++) {
1235 mountopts(struct nfs_args *args, char *p)
1239 args->version = NFS_ARGSVERSION;
1242 args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
1243 args->sotype = SOCK_DGRAM;
1246 if ((tmp = (char *)substr(p, "rsize=")))
1247 args->rsize = getdec(&tmp);
1248 if ((tmp = (char *)substr(p, "wsize=")))
1249 args->wsize = getdec(&tmp);
1250 if ((tmp = (char *)substr(p, "intr")))
1251 args->flags |= NFSMNT_INT;
1252 if ((tmp = (char *)substr(p, "soft")))
1253 args->flags |= NFSMNT_SOFT;
1254 if ((tmp = (char *)substr(p, "noconn")))
1255 args->flags |= NFSMNT_NOCONN;
1256 if ((tmp = (char *)substr(p, "tcp")))
1257 args->sotype = SOCK_STREAM;
1261 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
1267 alignedlen = ( len + 3 ) & ~3;
1269 if (m->m_len < alignedlen) {
1270 m = m_pullup(m, alignedlen);
1276 bcopy(mtod(m, u_char *), buf, len);
1277 m_adj(m, alignedlen);
1283 xdr_int_decode(struct mbuf **mptr, int *iptr)
1287 if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
1289 *iptr = fxdr_unsigned(u_int32_t, i);
1294 print_sin_addr(struct sockaddr_in *sin)
1297 print_in_addr(sin->sin_addr);
1301 print_in_addr(struct in_addr addr)
1305 ip = ntohl(addr.s_addr);
1306 printf("%d.%d.%d.%d",
1307 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1311 bootpc_compose_query(struct bootpc_ifcontext *ifctx,
1312 struct bootpc_globalcontext *gctx, struct thread *td)
1314 unsigned char *vendp;
1315 unsigned char vendor_client[64];
1317 uint8_t vendor_client_len;
1319 ifctx->gotrootpath = 0;
1321 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1324 ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */
1325 ifctx->call.htype = 1; /* 10mb ethernet */
1326 ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1327 ifctx->call.hops = 0;
1328 if (bootpc_ifctx_isunresolved(ifctx) != 0)
1330 ifctx->call.xid = txdr_unsigned(ifctx->xid);
1331 bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1333 vendp = ifctx->call.vend;
1334 *vendp++ = 99; /* RFC1048 cookie */
1338 *vendp++ = TAG_MAXMSGSIZE;
1340 *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1341 *vendp++ = sizeof(struct bootp_packet) & 255;
1343 snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1344 ostype, MACHINE, osrelease);
1345 vendor_client_len = strlen(vendor_client);
1346 *vendp++ = TAG_VENDOR_INDENTIFIER;
1347 *vendp++ = vendor_client_len;
1348 memcpy(vendp, vendor_client, vendor_client_len);
1349 vendp += vendor_client_len;;
1350 ifctx->dhcpquerytype = DHCP_NOMSG;
1351 switch (ifctx->state) {
1352 case IF_DHCP_UNRESOLVED:
1353 *vendp++ = TAG_DHCP_MSGTYPE;
1355 *vendp++ = DHCP_DISCOVER;
1356 ifctx->dhcpquerytype = DHCP_DISCOVER;
1357 ifctx->gotdhcpserver = 0;
1359 case IF_DHCP_OFFERED:
1360 *vendp++ = TAG_DHCP_MSGTYPE;
1362 *vendp++ = DHCP_REQUEST;
1363 ifctx->dhcpquerytype = DHCP_REQUEST;
1364 *vendp++ = TAG_DHCP_REQ_ADDR;
1366 memcpy(vendp, &ifctx->reply.yiaddr, 4);
1368 if (ifctx->gotdhcpserver != 0) {
1369 *vendp++ = TAG_DHCP_SERVERID;
1371 memcpy(vendp, &ifctx->dhcpserver, 4);
1374 *vendp++ = TAG_DHCP_LEASETIME;
1376 leasetime = htonl(300);
1377 memcpy(vendp, &leasetime, 4);
1384 ifctx->call.secs = 0;
1385 ifctx->call.flags = htons(0x8000); /* We need an broadcast answer */
1389 bootpc_hascookie(struct bootp_packet *bp)
1392 return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1393 bp->vend[2] == 83 && bp->vend[3] == 99);
1397 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1398 unsigned char *start, int len, int tag)
1404 if (tctx->badtag != 0 || tctx->badopt != 0)
1412 if (code == TAG_PAD)
1414 if (code == TAG_END)
1416 if (j >= ej || j + *j + 1 > ej) {
1422 if (tctx->taglen + len > TAG_MAXLEN) {
1428 memcpy(tctx->buf + tctx->taglen,
1430 tctx->taglen += len;
1432 if (code == TAG_OVERLOAD)
1433 tctx->overload = *j;
1439 static unsigned char *
1440 bootpc_tag(struct bootpc_tagcontext *tctx,
1441 struct bootp_packet *bp, int len, int tag)
1452 if (bootpc_hascookie(bp) == 0)
1456 ej = (unsigned char *) bp + len;
1458 bootpc_tag_helper(tctx, &bp->vend[4],
1459 (unsigned char *) bp + len - &bp->vend[4], tag);
1461 if ((tctx->overload & OVERLOAD_FILE) != 0)
1462 bootpc_tag_helper(tctx,
1463 (unsigned char *) bp->file,
1466 if ((tctx->overload & OVERLOAD_SNAME) != 0)
1467 bootpc_tag_helper(tctx,
1468 (unsigned char *) bp->sname,
1472 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1474 tctx->buf[tctx->taglen] = '\0';
1479 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1480 struct bootpc_globalcontext *gctx)
1486 ifctx->gotnetmask = 0;
1488 clear_sinaddr(&ifctx->myaddr);
1489 clear_sinaddr(&ifctx->netmask);
1490 clear_sinaddr(&ifctx->gw);
1492 ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1494 ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1495 snprintf(gctx->lookup_path, sizeof(gctx->lookup_path),
1497 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1499 printf("%s at ", ifctx->ireq.ifr_name);
1500 print_sin_addr(&ifctx->myaddr);
1502 print_in_addr(ifctx->reply.siaddr);
1504 ifctx->gw.sin_addr = ifctx->reply.giaddr;
1505 if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1506 printf(" via gateway ");
1507 print_in_addr(ifctx->reply.giaddr);
1510 /* This call used for the side effect (overload flag) */
1511 (void) bootpc_tag(&gctx->tmptag,
1512 &ifctx->reply, ifctx->replylen, TAG_END);
1514 if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1515 if (ifctx->reply.sname[0] != '\0')
1516 printf(" server name %s", ifctx->reply.sname);
1517 if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1518 if (ifctx->reply.file[0] != '\0')
1519 printf(" boot file %s", ifctx->reply.file);
1523 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1526 if (gctx->tag.taglen != 4)
1527 panic("bootpc: subnet mask len is %d",
1529 bcopy(p, &ifctx->netmask.sin_addr, 4);
1530 ifctx->gotnetmask = 1;
1531 printf("subnet mask ");
1532 print_sin_addr(&ifctx->netmask);
1536 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1540 if (gctx->tag.taglen % 4)
1541 panic("bootpc: Router Len is %d", gctx->tag.taglen);
1542 if (gctx->tag.taglen > 0) {
1543 bcopy(p, &ifctx->gw.sin_addr, 4);
1545 print_sin_addr(&ifctx->gw);
1552 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1555 if (gctx->setrootfs != NULL) {
1556 printf("rootfs %s (ignored) ", p);
1557 } else if (setfs(&nd->root_saddr,
1558 nd->root_hostnam, p)) {
1559 printf("rootfs %s ", p);
1560 gctx->gotrootpath = 1;
1561 ifctx->gotrootpath = 1;
1562 gctx->setrootfs = ifctx;
1564 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1568 mountopts(&nd->root_args, p);
1569 printf("rootopts %s ", p);
1572 panic("Failed to set rootfs to %s", p);
1575 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1578 if (gctx->setswapfs != NULL) {
1579 printf("swapfs %s (ignored) ", p);
1580 } else if (setfs(&nd->swap_saddr,
1581 nd->swap_hostnam, p)) {
1582 gctx->gotswappath = 1;
1583 gctx->setswapfs = ifctx;
1584 printf("swapfs %s ", p);
1586 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1590 /* swap mount options */
1591 mountopts(&nd->swap_args, p);
1592 printf("swapopts %s ", p);
1595 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1600 if (gctx->tag.taglen != 4)
1602 "Expected 4 bytes for swaplen, "
1605 bcopy(p, &swaplen, 4);
1606 nd->swap_nblks = ntohl(swaplen);
1607 printf("swapsize %d KB ",
1611 panic("Failed to set swapfs to %s", p);
1614 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1617 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1618 panic("bootpc: hostname >= %d bytes",
1620 if (gctx->sethostname != NULL) {
1621 printf("hostname %s (ignored) ", p);
1623 strcpy(nd->my_hostnam, p);
1624 strcpy(hostname, p);
1625 printf("hostname %s ", hostname);
1626 gctx->sethostname = ifctx;
1629 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1631 if (p != NULL) { /* store in a sysctl variable */
1632 int i, l = sizeof(bootp_cookie) - 1;
1633 for (i = 0; i < l && p[i] != '\0'; i++)
1634 bootp_cookie[i] = p[i];
1641 if (ifctx->gotnetmask == 0) {
1642 if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1643 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1644 else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1645 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1647 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1649 if (ifctx->gotgw == 0) {
1651 ifctx->gw.sin_addr.s_addr = ifctx->myaddr.sin_addr.s_addr;
1658 struct bootpc_ifcontext *ifctx, *nctx; /* Interface BOOTP contexts */
1659 struct bootpc_globalcontext *gctx; /* Global BOOTP context */
1662 struct nfsv3_diskless *nd;
1665 nd = &nfsv3_diskless;
1669 * If already filled in, don't touch it here
1671 if (nfs_diskless_valid != 0)
1674 gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK);
1676 panic("Failed to allocate bootp global context structure");
1678 bzero(gctx, sizeof(*gctx));
1679 gctx->xid = ~0xFFFF;
1680 gctx->starttime = time_second;
1682 ifctx = allocifctx(gctx);
1685 * Find a network interface.
1687 #ifdef BOOTP_WIRED_TO
1688 printf("bootpc_init: wired to interface '%s'\n",
1689 __XSTRING(BOOTP_WIRED_TO));
1691 bzero(&ifctx->ireq, sizeof(ifctx->ireq));
1692 for (ifp = TAILQ_FIRST(&ifnet);
1694 ifp = TAILQ_NEXT(ifp, if_link)) {
1695 snprintf(ifctx->ireq.ifr_name, sizeof(ifctx->ireq.ifr_name),
1696 "%s%d", ifp->if_name, ifp->if_unit);
1697 #ifdef BOOTP_WIRED_TO
1698 if (strcmp(ifctx->ireq.ifr_name,
1699 __XSTRING(BOOTP_WIRED_TO)) != 0)
1702 if ((ifp->if_flags &
1703 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1707 if (gctx->interfaces != NULL)
1708 gctx->lastinterface->next = ifctx;
1710 gctx->interfaces = ifctx;
1712 gctx->lastinterface = ifctx;
1713 ifctx = allocifctx(gctx);
1715 free(ifctx, M_TEMP);
1717 if (gctx->interfaces == NULL) {
1718 #ifdef BOOTP_WIRED_TO
1719 panic("bootpc_init: Could not find interface specified "
1720 "by BOOTP_WIRED_TO: "
1721 __XSTRING(BOOTP_WIRED_TO));
1723 panic("bootpc_init: no suitable interface");
1727 gctx->gotrootpath = 0;
1728 gctx->gotswappath = 0;
1731 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1732 bootpc_fakeup_interface(ifctx, gctx, td);
1734 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1735 bootpc_compose_query(ifctx, gctx, td);
1737 ifctx = gctx->interfaces;
1738 error = bootpc_call(gctx, td);
1741 #ifdef BOOTP_NFSROOT
1742 panic("BOOTP call failed");
1744 printf("BOOTP call failed\n");
1748 mountopts(&nd->root_args, NULL);
1750 mountopts(&nd->swap_args, NULL);
1752 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1753 if (bootpc_ifctx_isresolved(ifctx) != 0)
1754 bootpc_decode_reply(nd, ifctx, gctx);
1756 if (gctx->gotswappath == 0)
1758 #ifdef BOOTP_NFSROOT
1759 if (gctx->gotrootpath == 0)
1760 panic("bootpc: No root path offered");
1763 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
1764 bootpc_adjust_interface(ifctx, gctx, td);
1769 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1770 if (ifctx->gotrootpath != 0)
1772 if (ifctx == NULL) {
1773 for (ifctx = gctx->interfaces;
1775 ifctx = ifctx->next)
1776 if (bootpc_ifctx_isresolved(ifctx) != 0)
1782 if (gctx->gotrootpath != 0) {
1784 error = md_mount(&nd->root_saddr, nd->root_hostnam,
1785 nd->root_fh, &nd->root_fhsize,
1786 &nd->root_args, td);
1788 panic("nfs_boot: mountd root, error=%d", error);
1790 if (gctx->gotswappath != 0) {
1792 error = md_mount(&nd->swap_saddr,
1794 nd->swap_fh, &nd->swap_fhsize,
1795 &nd->swap_args, td);
1797 panic("nfs_boot: mountd swap, error=%d",
1800 error = md_lookup_swap(&nd->swap_saddr,
1802 nd->swap_fh, &nd->swap_fhsize,
1803 &nd->swap_args, td);
1805 panic("nfs_boot: lookup swap, error=%d",
1808 nfs_diskless_valid = 3;
1811 strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1812 bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1813 bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1814 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1815 ifctx->myaddr.sin_addr.s_addr |
1816 ~ ifctx->netmask.sin_addr.s_addr;
1817 bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1820 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = nctx) {
1822 free(ifctx, M_TEMP);
1829 * Given a server pathname, get an NFS file handle.
1830 * Also, sets sin->sin_port to the NFS service port.
1833 md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp, int *fhsizep,
1834 struct nfs_args *args, struct thread *td)
1843 /* First try NFS v3 */
1844 /* Get port number for MOUNTD. */
1845 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1846 &mdsin->sin_port, td);
1848 m = xdr_string_encode(path, strlen(path));
1850 /* Do RPC to mountd. */
1851 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1852 RPCMNT_MOUNT, &m, NULL, td);
1855 args->flags |= NFSMNT_NFSV3;
1858 /* Fallback to NFS v2 */
1860 /* Get port number for MOUNTD. */
1861 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1862 &mdsin->sin_port, td);
1866 m = xdr_string_encode(path, strlen(path));
1868 /* Do RPC to mountd. */
1869 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1870 RPCMNT_MOUNT, &m, NULL, td);
1872 return error; /* message already freed */
1878 if (xdr_int_decode(&m, &error) != 0 || error != 0)
1881 if ((args->flags & NFSMNT_NFSV3) != 0) {
1882 if (xdr_int_decode(&m, fhsizep) != 0 ||
1883 *fhsizep > NFSX_V3FHMAX ||
1887 *fhsizep = NFSX_V2FH;
1889 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
1892 if (args->flags & NFSMNT_NFSV3) {
1893 if (xdr_int_decode(&m, &authcount) != 0)
1896 if (authcount < 0 || authcount > 100)
1898 while (authcount > 0) {
1899 if (xdr_int_decode(&m, &authver) != 0)
1901 if (authver == RPCAUTH_UNIX)
1905 if (authunixok == 0)
1909 /* Set port number for NFS use. */
1910 error = krpc_portmap(mdsin, NFS_PROG,
1912 NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
1913 &mdsin->sin_port, td);
1926 md_lookup_swap(struct sockaddr_in *mdsin, char *path, u_char *fhp, int *fhsizep,
1927 struct nfs_args *args, struct thread *td)
1932 int attribs_present;
1939 m = m_get(M_TRYWAIT, MT_DATA);
1943 if ((args->flags & NFSMNT_NFSV3) != 0) {
1944 *mtod(m, u_int32_t *) = txdr_unsigned(*fhsizep);
1945 bcopy(fhp, mtod(m, u_char *) + sizeof(u_int32_t), *fhsizep);
1946 m->m_len = *fhsizep + sizeof(u_int32_t);
1948 bcopy(fhp, mtod(m, u_char *), NFSX_V2FH);
1949 m->m_len = NFSX_V2FH;
1952 m->m_next = xdr_string_encode(path, strlen(path));
1953 if (m->m_next == NULL) {
1958 /* Do RPC to nfsd. */
1959 if ((args->flags & NFSMNT_NFSV3) != 0)
1960 error = krpc_call(mdsin, NFS_PROG, NFS_VER3,
1961 NFSPROC_LOOKUP, &m, NULL, td);
1963 error = krpc_call(mdsin, NFS_PROG, NFS_VER2,
1964 NFSV2PROC_LOOKUP, &m, NULL, td);
1966 return error; /* message already freed */
1968 if (xdr_int_decode(&m, &status) != 0)
1975 if ((args->flags & NFSMNT_NFSV3) != 0) {
1976 if (xdr_int_decode(&m, fhsizep) != 0 ||
1977 *fhsizep > NFSX_V3FHMAX ||
1981 *fhsizep = NFSX_V2FH;
1983 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
1986 if ((args->flags & NFSMNT_NFSV3) != 0) {
1987 if (xdr_int_decode(&m, &attribs_present) != 0)
1989 if (attribs_present != 0) {
1990 if (xdr_opaque_decode(&m, (u_char *) &fattribs.v3,
1991 sizeof(u_int32_t) * 21) != 0)
1993 size = fxdr_unsigned(u_int32_t, fattribs.v3[6]);
1996 if (xdr_opaque_decode(&m,(u_char *) &fattribs.v2,
1997 sizeof(u_int32_t) * 17) != 0)
1999 size = fxdr_unsigned(u_int32_t, fattribs.v2[5]);
2002 if (nfsv3_diskless.swap_nblks == 0 && size != -1) {
2003 nfsv3_diskless.swap_nblks = size / 1024;
2004 printf("md_lookup_swap: Swap size is %d KB\n",
2005 nfsv3_diskless.swap_nblks);