1 /* $FreeBSD: src/sys/nfs/bootp_subr.c,v 1.20.2.9 2003/04/24 16:51:08 ambrisko Exp $ */
2 /* $DragonFly: src/sys/vfs/nfs/bootp_subr.c,v 1.10 2004/06/21 05:58:01 dillon Exp $ */
5 * Copyright (c) 1995 Gordon Ross, Adam Glass
6 * Copyright (c) 1992 Regents of the University of California.
9 * This software was developed by the Computer Systems Engineering group
10 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
11 * contributed to Berkeley.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Lawrence Berkeley Laboratory and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * $NetBSD: krpc_subr.c,v 1.10 1995/08/08 20:43:43 gwr Exp $
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>
53 #include <sys/malloc.h>
54 #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>
71 #include "nfsdiskless.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, exported 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 extern int nfs_diskless_valid;
217 extern struct nfsv3_diskless nfsv3_diskless;
218 static char bootp_cookie[128];
219 SYSCTL_STRING(_kern, OID_AUTO, bootp_cookie, CTLFLAG_RD,
220 bootp_cookie, 0, "Cookie (T134) supplied by bootp server");
223 static int md_mount(struct sockaddr_in *mdsin, char *path,
224 u_char *fhp, int *fhsizep,
225 struct nfs_args *args,struct thread *td);
226 static int md_lookup_swap(struct sockaddr_in *mdsin,char *path,
227 u_char *fhp, int *fhsizep,
228 struct nfs_args *args,
230 static int setfs(struct sockaddr_in *addr, char *path, char *p);
231 static int getdec(char **ptr);
232 static char *substr(char *a,char *b);
233 static void mountopts(struct nfs_args *args, char *p);
234 static int xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len);
235 static int xdr_int_decode(struct mbuf **ptr, int *iptr);
236 static void print_in_addr(struct in_addr addr);
237 static void print_sin_addr(struct sockaddr_in *addr);
238 static void clear_sinaddr(struct sockaddr_in *sin);
240 struct bootpc_ifcontext *allocifctx(struct bootpc_globalcontext *gctx);
241 static void bootpc_compose_query(struct bootpc_ifcontext *ifctx,
242 struct bootpc_globalcontext *gctx,
244 static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx,
245 struct bootp_packet *bp, int len, int tag);
246 static void bootpc_tag_helper(struct bootpc_tagcontext *tctx,
247 unsigned char *start, int len, int tag);
250 void bootpboot_p_sa(struct sockaddr *sa,struct sockaddr *ma);
251 void bootpboot_p_ma(struct sockaddr *ma);
252 void bootpboot_p_rtentry(struct rtentry *rt);
253 void bootpboot_p_tree(struct radix_node *rn);
254 void bootpboot_p_rtlist(void);
255 void bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa);
256 void bootpboot_p_iflist(void);
259 static int bootpc_call(struct bootpc_globalcontext *gctx,
262 static int bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
263 struct bootpc_globalcontext *gctx,
266 static int bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
267 struct bootpc_globalcontext *gctx,
270 static void bootpc_decode_reply(struct nfsv3_diskless *nd,
271 struct bootpc_ifcontext *ifctx,
272 struct bootpc_globalcontext *gctx);
274 static int bootpc_received(struct bootpc_globalcontext *gctx,
275 struct bootpc_ifcontext *ifctx);
277 static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
278 static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
279 static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
281 void bootpc_init(void);
284 * In order to have multiple active interfaces with address 0.0.0.0
285 * and be able to send data to a selected interface, we perform
288 * - The 'broadcast' address is different for each interface.
290 * - We temporarily add routing pointing 255.255.255.255 to the
291 * selected interface broadcast address, thus the packet sent
292 * goes to that interface.
297 bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma)
300 printf("(sockaddr *) <null>");
303 switch (sa->sa_family) {
306 struct sockaddr_in *sin;
308 sin = (struct sockaddr_in *) sa;
312 sin = (struct sockaddr_in *) ma;
320 struct sockaddr_dl *sli;
323 sli = (struct sockaddr_dl *) sa;
324 printf("link %.*s ", sli->sdl_nlen, sli->sdl_data);
325 for (i = 0; i < sli->sdl_alen; i++) {
328 printf("%x", ((unsigned char *) LLADDR(sli))[i]);
333 printf("af%d", sa->sa_family);
339 bootpboot_p_ma(struct sockaddr *ma)
345 printf("%x", *(int *)ma);
350 bootpboot_p_rtentry(struct rtentry *rt)
352 bootpboot_p_sa(rt_key(rt), rt_mask(rt));
354 bootpboot_p_ma(rt->rt_genmask);
356 bootpboot_p_sa(rt->rt_gateway, NULL);
358 printf("flags %x", (unsigned short) rt->rt_flags);
359 printf(" %d", (int) rt->rt_rmx.rmx_expire);
360 printf(" %s\n", if_name(rt->rt_ifp));
365 bootpboot_p_tree(struct radix_node *rn)
368 if (rn->rn_bit < 0) {
369 if ((rn->rn_flags & RNF_ROOT) != 0) {
371 bootpboot_p_rtentry((struct rtentry *) rn);
373 rn = rn->rn_dupedkey;
375 bootpboot_p_tree(rn->rn_left);
376 bootpboot_p_tree(rn->rn_right);
384 bootpboot_p_rtlist(void)
386 printf("Routing table:\n");
387 bootpboot_p_tree(rt_tables[AF_INET]->rnh_treetop);
392 bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
394 printf("%s flags %x, addr ",
396 (unsigned short) ifp->if_flags);
397 print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
398 printf(", broadcast ");
399 print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
400 printf(", netmask ");
401 print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
407 bootpboot_p_iflist(void)
412 printf("Interface list:\n");
413 TAILQ_FOREACH(ifp, &ifnet, if_link) {
414 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
415 if (ifa->ifa_addr->sa_family == AF_INET)
416 bootpboot_p_if(ifp, ifa);
419 #endif /* defined(BOOTP_DEBUG) */
423 clear_sinaddr(struct sockaddr_in *sin)
425 bzero(sin, sizeof(*sin));
426 sin->sin_len = sizeof(*sin);
427 sin->sin_family = AF_INET;
428 sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
433 static struct bootpc_ifcontext *
434 allocifctx(struct bootpc_globalcontext *gctx)
436 struct bootpc_ifcontext *ifctx;
437 ifctx = (struct bootpc_ifcontext *) malloc(sizeof(*ifctx),
440 panic("Failed to allocate bootp interface context structure");
442 bzero(ifctx, sizeof(*ifctx));
443 ifctx->xid = gctx->xid;
445 ifctx->state = IF_BOOTP_UNRESOLVED;
447 ifctx->state = IF_DHCP_UNRESOLVED;
455 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
457 if (ifctx->state == IF_BOOTP_RESOLVED ||
458 ifctx->state == IF_DHCP_RESOLVED)
465 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
467 if (ifctx->state == IF_BOOTP_UNRESOLVED ||
468 ifctx->state == IF_DHCP_UNRESOLVED)
475 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
477 if (ifctx->state == IF_BOOTP_FAILED ||
478 ifctx->state == IF_DHCP_FAILED)
485 bootpc_received(struct bootpc_globalcontext *gctx,
486 struct bootpc_ifcontext *ifctx)
488 unsigned char dhcpreplytype;
491 * Need timeout for fallback to less
492 * desirable alternative.
496 /* This call used for the side effect (badopt flag) */
497 (void) bootpc_tag(&gctx->tmptag, &gctx->reply,
501 /* If packet is invalid, ignore it */
502 if (gctx->tmptag.badopt != 0)
505 p = bootpc_tag(&gctx->tmptag, &gctx->reply,
506 gctx->replylen, TAG_DHCP_MSGTYPE);
510 dhcpreplytype = DHCP_NOMSG;
512 switch (ifctx->dhcpquerytype) {
514 if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */
515 #ifndef BOOTP_FORCE_DHCP
516 && dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */
522 if (dhcpreplytype != DHCP_ACK)
530 /* Ignore packet unless it gives us a root tag we didn't have */
532 if ((ifctx->state == IF_BOOTP_RESOLVED ||
533 (ifctx->dhcpquerytype == DHCP_DISCOVER &&
534 (ifctx->state == IF_DHCP_OFFERED ||
535 ifctx->state == IF_DHCP_RESOLVED))) &&
536 (bootpc_tag(&gctx->tmptag, &ifctx->reply,
539 bootpc_tag(&gctx->tmptag, &gctx->reply,
547 ifctx->replylen = gctx->replylen;
549 /* XXX: Only reset if 'perfect' response */
550 if (ifctx->state == IF_BOOTP_UNRESOLVED)
551 ifctx->state = IF_BOOTP_RESOLVED;
552 else if (ifctx->state == IF_DHCP_UNRESOLVED &&
553 ifctx->dhcpquerytype == DHCP_DISCOVER) {
554 if (dhcpreplytype == DHCP_OFFER)
555 ifctx->state = IF_DHCP_OFFERED;
557 ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */
558 } else if (ifctx->state == IF_DHCP_OFFERED &&
559 ifctx->dhcpquerytype == DHCP_REQUEST)
560 ifctx->state = IF_DHCP_RESOLVED;
563 if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
564 ifctx->state != IF_BOOTP_RESOLVED) {
565 p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
566 ifctx->replylen, TAG_DHCP_SERVERID);
567 if (p != NULL && gctx->tmptag.taglen == 4) {
568 memcpy(&ifctx->dhcpserver, p, 4);
569 ifctx->gotdhcpserver = 1;
571 ifctx->gotdhcpserver = 0;
575 ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
578 ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
580 TAG_ROUTERS) != NULL);
581 ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
583 TAG_SUBNETMASK) != NULL);
588 bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
591 struct sockaddr_in *sin, dst;
595 int error, on, rcvflg, timo, len;
599 struct bootpc_ifcontext *ifctx;
606 * Create socket and set its recieve timeout.
608 error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td);
614 bzero(&sopt, sizeof(sopt));
615 sopt.sopt_level = SOL_SOCKET;
616 sopt.sopt_name = SO_RCVTIMEO;
618 sopt.sopt_valsize = sizeof tv;
620 error = sosetopt(so, &sopt);
628 sopt.sopt_name = SO_BROADCAST;
630 sopt.sopt_valsize = sizeof on;
632 error = sosetopt(so, &sopt);
641 sopt.sopt_name = SO_DONTROUTE;
643 sopt.sopt_valsize = sizeof on;
645 error = sosetopt(so, &sopt);
650 * Bind the local endpoint to a bootp client port.
654 sin->sin_port = htons(IPPORT_BOOTPC);
655 error = sobind(so, (struct sockaddr *)sin, td);
657 printf("bind failed\n");
662 * Setup socket address for the server.
666 sin->sin_addr.s_addr = INADDR_BROADCAST;
667 sin->sin_port = htons(IPPORT_BOOTPS);
670 * Send it, repeatedly, until a reply is received,
671 * but delay each re-send by an increasing amount.
672 * If the delay hits the maximum, start complaining.
681 for (ifctx = gctx->interfaces;
683 ifctx = ifctx->next) {
684 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
685 bootpc_tag(&gctx->tmptag, &ifctx->reply,
691 for (ifctx = gctx->interfaces;
693 ifctx = ifctx->next) {
694 ifctx->outstanding = 0;
695 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
699 if (bootpc_ifctx_isfailed(ifctx) != 0)
703 ifctx->outstanding = 1;
705 /* Proceed to next step in DHCP negotiation */
706 if ((ifctx->state == IF_DHCP_OFFERED &&
707 ifctx->dhcpquerytype != DHCP_REQUEST) ||
708 (ifctx->state == IF_DHCP_UNRESOLVED &&
709 ifctx->dhcpquerytype != DHCP_DISCOVER) ||
710 (ifctx->state == IF_BOOTP_UNRESOLVED &&
711 ifctx->dhcpquerytype != DHCP_NOMSG)) {
713 bootpc_compose_query(ifctx, gctx, td);
716 /* Send BOOTP request (or re-send). */
718 if (ifctx->sentmsg == 0) {
719 switch(ifctx->dhcpquerytype) {
731 printf("Sending %s packet from "
732 "interface %s (%*D)\n",
734 ifctx->ireq.ifr_name,
735 ifctx->sdl->sdl_alen,
736 (unsigned char *) LLADDR(ifctx->sdl),
741 aio.iov_base = (caddr_t) &ifctx->call;
742 aio.iov_len = sizeof(ifctx->call);
746 auio.uio_segflg = UIO_SYSSPACE;
747 auio.uio_rw = UIO_WRITE;
749 auio.uio_resid = sizeof(ifctx->call);
752 /* Set netmask to 0.0.0.0 */
754 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
756 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
757 (caddr_t) &ifctx->ireq, td);
760 "set if netmask, error=%d",
763 error = sosend(so, (struct sockaddr *) &dst,
764 &auio, NULL, NULL, 0, td);
766 printf("bootpc_call: sosend: %d state %08x\n",
767 error, (int) so->so_state);
770 /* XXX: Is this needed ? */
771 tsleep(&error, 0, "bootpw", 10);
773 /* Set netmask to 255.0.0.0 */
775 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
777 sin->sin_addr.s_addr = htonl(0xff000000u);
778 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
779 (caddr_t) &ifctx->ireq, td);
782 "set if netmask, error=%d",
787 if (outstanding == 0 &&
788 (rtimo == 0 || time_second >= rtimo)) {
793 /* Determine new timeout. */
794 if (timo < MAX_RESEND_DELAY)
797 printf("DHCP/BOOTP timeout for server ");
798 print_sin_addr(&dst);
803 * Wait for up to timo seconds for a reply.
804 * The socket receive timeout was set to 1 second.
806 atimo = timo + time_second;
807 while (time_second < atimo) {
808 aio.iov_base = (caddr_t) &gctx->reply;
809 aio.iov_len = sizeof(gctx->reply);
813 auio.uio_segflg = UIO_SYSSPACE;
814 auio.uio_rw = UIO_READ;
816 auio.uio_resid = sizeof(gctx->reply);
820 error = soreceive(so, NULL, &auio,
821 NULL, NULL, &rcvflg);
822 gctx->secs = time_second - gctx->starttime;
823 for (ifctx = gctx->interfaces;
825 ifctx = ifctx->next) {
826 if (bootpc_ifctx_isresolved(ifctx) != 0 ||
827 bootpc_ifctx_isfailed(ifctx) != 0)
830 ifctx->call.secs = htons(gctx->secs);
832 if (error == EWOULDBLOCK)
836 len = sizeof(gctx->reply) - auio.uio_resid;
838 /* Do we have the required number of bytes ? */
839 if (len < BOOTP_MIN_LEN)
841 gctx->replylen = len;
844 if (gctx->reply.op != BOOTP_REPLY)
847 /* Is this an answer to our query */
848 for (ifctx = gctx->interfaces;
850 ifctx = ifctx->next) {
851 if (gctx->reply.xid != ifctx->call.xid)
854 /* Same HW address size ? */
855 if (gctx->reply.hlen != ifctx->call.hlen)
858 /* Correct HW address ? */
859 if (bcmp(gctx->reply.chaddr,
861 ifctx->call.hlen) != 0)
868 s = bootpc_tag(&gctx->tmptag,
881 s = "DHCP (unexpected)";
887 printf("Received %s packet"
890 ifctx->ireq.ifr_name);
891 print_in_addr(gctx->reply.siaddr);
892 if (gctx->reply.giaddr.s_addr !=
895 print_in_addr(gctx->reply.giaddr);
897 if (bootpc_received(gctx, ifctx) != 0) {
898 printf(" (accepted)");
899 if (ifctx->outstanding) {
900 ifctx->outstanding = 0;
903 /* Network settle delay */
904 if (outstanding == 0)
905 atimo = time_second +
908 printf(" (ignored)");
909 if (ifctx->gotrootpath) {
911 rtimo = time_second +
913 printf(" (got root path)");
915 printf(" (no root path)");
920 if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
923 /* Force a retry if halfway in DHCP negotiation */
925 for (ifctx = gctx->interfaces; ifctx != NULL;
926 ifctx = ifctx->next) {
927 if (ifctx->state == IF_DHCP_OFFERED) {
928 if (ifctx->dhcpquerytype == DHCP_DISCOVER)
931 ifctx->state = IF_DHCP_UNRESOLVED;
938 if (gotrootpath != 0) {
939 gctx->gotrootpath = gotrootpath;
940 if (rtimo != 0 && time_second >= rtimo)
943 } /* forever send/receive */
946 * XXX: These are errors of varying seriousness being silently
950 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
951 if (bootpc_ifctx_isresolved(ifctx) == 0) {
952 printf("%s timeout for interface %s\n",
953 ifctx->dhcpquerytype != DHCP_NOMSG ?
955 ifctx->ireq.ifr_name);
958 if (gctx->gotrootpath != 0) {
960 printf("Got a root path, ignoring remaining timeout\n");
965 #ifndef BOOTP_NFSROOT
966 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
967 if (bootpc_ifctx_isresolved(ifctx) != 0) {
984 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
985 struct bootpc_globalcontext *gctx,
988 struct sockaddr_in *sin;
994 struct sockaddr_dl *sdl;
996 error = socreate(AF_INET, &ifctx->so, SOCK_DGRAM, 0, td);
998 panic("nfs_boot: socreate, error=%d", error);
1000 ireq = &ifctx->ireq;
1004 * Bring up the interface.
1006 * Get the old interface flags and or IFF_UP into them; if
1007 * IFF_UP set blindly, interface selection can be clobbered.
1009 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, td);
1011 panic("bootpc_fakeup_interface: GIFFLAGS, error=%d", error);
1012 ireq->ifr_flags |= IFF_UP;
1013 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, td);
1015 panic("bootpc_fakeup_interface: SIFFLAGS, error=%d", error);
1018 * Do enough of ifconfig(8) so that the chosen interface
1019 * can talk to the servers. (just set the address)
1022 /* addr is 0.0.0.0 */
1024 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1026 error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, td);
1027 if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1028 panic("bootpc_fakeup_interface: "
1029 "set if addr, error=%d", error);
1031 /* netmask is 255.0.0.0 */
1033 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1035 sin->sin_addr.s_addr = htonl(0xff000000u);
1036 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t)ireq, td);
1038 panic("bootpc_fakeup_interface: set if netmask, error=%d",
1041 /* Broadcast is 255.255.255.255 */
1043 sin = (struct sockaddr_in *)&ireq->ifr_addr;
1045 clear_sinaddr(&ifctx->broadcast);
1046 sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
1047 ifctx->broadcast.sin_addr.s_addr = sin->sin_addr.s_addr;
1049 error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t)ireq, td);
1050 if (error != 0 && error != EADDRNOTAVAIL)
1051 panic("bootpc_fakeup_interface: "
1052 "set if broadcast addr, error=%d",
1056 /* Get HW address */
1059 TAILQ_FOREACH(ifa, &ifctx->ifp->if_addrhead, ifa_link)
1060 if (ifa->ifa_addr->sa_family == AF_LINK &&
1061 (sdl = ((struct sockaddr_dl *) ifa->ifa_addr)) != NULL &&
1062 sdl->sdl_type == IFT_ETHER)
1066 panic("bootpc: Unable to find HW address for %s",
1067 ifctx->ireq.ifr_name);
1075 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
1076 struct bootpc_globalcontext *gctx,
1080 struct sockaddr_in defdst;
1081 struct sockaddr_in defmask;
1082 struct sockaddr_in *sin;
1086 struct sockaddr_in *myaddr;
1087 struct sockaddr_in *netmask;
1088 struct sockaddr_in *gw;
1090 ireq = &ifctx->ireq;
1092 myaddr = &ifctx->myaddr;
1093 netmask = &ifctx->netmask;
1096 if (bootpc_ifctx_isresolved(ifctx) == 0) {
1098 /* Shutdown interfaces where BOOTP failed */
1100 printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
1101 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, td);
1103 panic("bootpc_adjust_interface: "
1104 "SIOCGIFFLAGS, error=%d", error);
1105 ireq->ifr_flags &= ~IFF_UP;
1106 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, td);
1108 panic("bootpc_adjust_interface: "
1109 "SIOCSIFFLAGS, error=%d", error);
1111 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1113 error = ifioctl(so, SIOCDIFADDR, (caddr_t) ireq, td);
1114 if (error != 0 && (error != EADDRNOTAVAIL ||
1115 ifctx == gctx->interfaces))
1116 panic("bootpc_adjust_interface: "
1117 "SIOCDIFADDR, error=%d", error);
1122 printf("Adjusted interface %s\n", ifctx->ireq.ifr_name);
1124 * Do enough of ifconfig(8) so that the chosen interface
1125 * can talk to the servers. (just set the address)
1127 bcopy(netmask, &ireq->ifr_addr, sizeof(*netmask));
1128 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t) ireq, td);
1130 panic("bootpc_adjust_interface: "
1131 "set if netmask, error=%d", error);
1133 /* Broadcast is with host part of IP address all 1's */
1135 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1137 sin->sin_addr.s_addr = myaddr->sin_addr.s_addr |
1138 ~ netmask->sin_addr.s_addr;
1139 error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t) ireq, td);
1141 panic("bootpc_adjust_interface: "
1142 "set if broadcast addr, error=%d", error);
1144 bcopy(myaddr, &ireq->ifr_addr, sizeof(*myaddr));
1145 error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, td);
1146 if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1147 panic("bootpc_adjust_interface: "
1148 "set if addr, error=%d", error);
1150 /* Add new default route */
1152 if (ifctx->gotgw != 0 || gctx->gotgw == 0) {
1153 clear_sinaddr(&defdst);
1154 clear_sinaddr(&defmask);
1155 error = rtrequest(RTM_ADD,
1156 (struct sockaddr *) &defdst,
1157 (struct sockaddr *) gw,
1158 (struct sockaddr *) &defmask,
1159 (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
1161 printf("bootpc_adjust_interface: "
1162 "add net route, error=%d\n", error);
1172 setfs(struct sockaddr_in *addr, char *path, char *p)
1178 if (((val = getdec(&p)) < 0) || (val > 255))
1184 if (((val = getdec(&p)) < 0) || (val > 255))
1190 if (((val = getdec(&p)) < 0) || (val > 255))
1196 if (((val = getdec(&p)) < 0) || (val > 255))
1203 addr->sin_addr.s_addr = htonl(ip);
1204 addr->sin_len = sizeof(struct sockaddr_in);
1205 addr->sin_family = AF_INET;
1207 strncpy(path, p, MNAMELEN - 1);
1220 if ((*p < '0') || (*p > '9'))
1222 while ((*p >= '0') && (*p <= '9')) {
1223 ret = ret * 10 + (*p - '0');
1232 substr(char *a, char *b)
1237 while (*a != '\0') {
1240 while (*loc1 == *loc2++) {
1254 mountopts(struct nfs_args *args, char *p)
1258 args->version = NFS_ARGSVERSION;
1261 args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
1262 args->sotype = SOCK_DGRAM;
1265 if ((tmp = (char *)substr(p, "rsize=")))
1266 args->rsize = getdec(&tmp);
1267 if ((tmp = (char *)substr(p, "wsize=")))
1268 args->wsize = getdec(&tmp);
1269 if ((tmp = (char *)substr(p, "intr")))
1270 args->flags |= NFSMNT_INT;
1271 if ((tmp = (char *)substr(p, "soft")))
1272 args->flags |= NFSMNT_SOFT;
1273 if ((tmp = (char *)substr(p, "noconn")))
1274 args->flags |= NFSMNT_NOCONN;
1275 if ((tmp = (char *)substr(p, "tcp")))
1276 args->sotype = SOCK_STREAM;
1281 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
1287 alignedlen = ( len + 3 ) & ~3;
1289 if (m->m_len < alignedlen) {
1290 m = m_pullup(m, alignedlen);
1296 bcopy(mtod(m, u_char *), buf, len);
1297 m_adj(m, alignedlen);
1304 xdr_int_decode(struct mbuf **mptr, int *iptr)
1307 if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
1309 *iptr = fxdr_unsigned(u_int32_t, i);
1315 print_sin_addr(struct sockaddr_in *sin)
1317 print_in_addr(sin->sin_addr);
1322 print_in_addr(struct in_addr addr)
1326 ip = ntohl(addr.s_addr);
1327 printf("%d.%d.%d.%d",
1328 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1332 bootpc_compose_query(struct bootpc_ifcontext *ifctx,
1333 struct bootpc_globalcontext *gctx, struct thread *td)
1335 unsigned char *vendp;
1336 unsigned char vendor_client[64];
1338 uint8_t vendor_client_len;
1340 ifctx->gotrootpath = 0;
1342 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1345 ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */
1346 ifctx->call.htype = 1; /* 10mb ethernet */
1347 ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1348 ifctx->call.hops = 0;
1349 if (bootpc_ifctx_isunresolved(ifctx) != 0)
1351 ifctx->call.xid = txdr_unsigned(ifctx->xid);
1352 bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1354 vendp = ifctx->call.vend;
1355 *vendp++ = 99; /* RFC1048 cookie */
1359 *vendp++ = TAG_MAXMSGSIZE;
1361 *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1362 *vendp++ = sizeof(struct bootp_packet) & 255;
1364 snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1365 ostype, MACHINE, osrelease);
1366 vendor_client_len = strlen(vendor_client);
1367 *vendp++ = TAG_VENDOR_INDENTIFIER;
1368 *vendp++ = vendor_client_len;
1369 memcpy(vendp, vendor_client, vendor_client_len);
1370 vendp += vendor_client_len;;
1371 ifctx->dhcpquerytype = DHCP_NOMSG;
1372 switch (ifctx->state) {
1373 case IF_DHCP_UNRESOLVED:
1374 *vendp++ = TAG_DHCP_MSGTYPE;
1376 *vendp++ = DHCP_DISCOVER;
1377 ifctx->dhcpquerytype = DHCP_DISCOVER;
1378 ifctx->gotdhcpserver = 0;
1380 case IF_DHCP_OFFERED:
1381 *vendp++ = TAG_DHCP_MSGTYPE;
1383 *vendp++ = DHCP_REQUEST;
1384 ifctx->dhcpquerytype = DHCP_REQUEST;
1385 *vendp++ = TAG_DHCP_REQ_ADDR;
1387 memcpy(vendp, &ifctx->reply.yiaddr, 4);
1389 if (ifctx->gotdhcpserver != 0) {
1390 *vendp++ = TAG_DHCP_SERVERID;
1392 memcpy(vendp, &ifctx->dhcpserver, 4);
1395 *vendp++ = TAG_DHCP_LEASETIME;
1397 leasetime = htonl(300);
1398 memcpy(vendp, &leasetime, 4);
1405 ifctx->call.secs = 0;
1406 ifctx->call.flags = htons(0x8000); /* We need an broadcast answer */
1411 bootpc_hascookie(struct bootp_packet *bp)
1413 return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1414 bp->vend[2] == 83 && bp->vend[3] == 99);
1419 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1420 unsigned char *start, int len, int tag)
1426 if (tctx->badtag != 0 || tctx->badopt != 0)
1434 if (code == TAG_PAD)
1436 if (code == TAG_END)
1438 if (j >= ej || j + *j + 1 > ej) {
1444 if (tctx->taglen + len > TAG_MAXLEN) {
1450 memcpy(tctx->buf + tctx->taglen,
1452 tctx->taglen += len;
1454 if (code == TAG_OVERLOAD)
1455 tctx->overload = *j;
1462 static unsigned char *
1463 bootpc_tag(struct bootpc_tagcontext *tctx, struct bootp_packet *bp,
1475 if (bootpc_hascookie(bp) == 0)
1479 ej = (unsigned char *) bp + len;
1481 bootpc_tag_helper(tctx, &bp->vend[4],
1482 (unsigned char *) bp + len - &bp->vend[4], tag);
1484 if ((tctx->overload & OVERLOAD_FILE) != 0)
1485 bootpc_tag_helper(tctx,
1486 (unsigned char *) bp->file,
1489 if ((tctx->overload & OVERLOAD_SNAME) != 0)
1490 bootpc_tag_helper(tctx,
1491 (unsigned char *) bp->sname,
1495 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1497 tctx->buf[tctx->taglen] = '\0';
1503 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1504 struct bootpc_globalcontext *gctx)
1510 ifctx->gotnetmask = 0;
1512 clear_sinaddr(&ifctx->myaddr);
1513 clear_sinaddr(&ifctx->netmask);
1514 clear_sinaddr(&ifctx->gw);
1516 ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1518 ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1519 snprintf(gctx->lookup_path, sizeof(gctx->lookup_path),
1521 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1523 printf("%s at ", ifctx->ireq.ifr_name);
1524 print_sin_addr(&ifctx->myaddr);
1526 print_in_addr(ifctx->reply.siaddr);
1528 ifctx->gw.sin_addr = ifctx->reply.giaddr;
1529 if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1530 printf(" via gateway ");
1531 print_in_addr(ifctx->reply.giaddr);
1534 /* This call used for the side effect (overload flag) */
1535 (void) bootpc_tag(&gctx->tmptag,
1536 &ifctx->reply, ifctx->replylen, TAG_END);
1538 if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1539 if (ifctx->reply.sname[0] != '\0')
1540 printf(" server name %s", ifctx->reply.sname);
1541 if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1542 if (ifctx->reply.file[0] != '\0')
1543 printf(" boot file %s", ifctx->reply.file);
1547 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1550 if (gctx->tag.taglen != 4)
1551 panic("bootpc: subnet mask len is %d",
1553 bcopy(p, &ifctx->netmask.sin_addr, 4);
1554 ifctx->gotnetmask = 1;
1555 printf("subnet mask ");
1556 print_sin_addr(&ifctx->netmask);
1560 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1564 if (gctx->tag.taglen % 4)
1565 panic("bootpc: Router Len is %d", gctx->tag.taglen);
1566 if (gctx->tag.taglen > 0) {
1567 bcopy(p, &ifctx->gw.sin_addr, 4);
1569 print_sin_addr(&ifctx->gw);
1576 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1579 if (gctx->setrootfs != NULL) {
1580 printf("rootfs %s (ignored) ", p);
1581 } else if (setfs(&nd->root_saddr,
1582 nd->root_hostnam, p)) {
1583 printf("rootfs %s ",p);
1584 gctx->gotrootpath = 1;
1585 ifctx->gotrootpath = 1;
1586 gctx->setrootfs = ifctx;
1588 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1592 mountopts(&nd->root_args, p);
1593 printf("rootopts %s ", p);
1596 panic("Failed to set rootfs to %s",p);
1599 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1602 if (gctx->setswapfs != NULL) {
1603 printf("swapfs %s (ignored) ", p);
1604 } else if (setfs(&nd->swap_saddr,
1605 nd->swap_hostnam, p)) {
1606 gctx->gotswappath = 1;
1607 gctx->setswapfs = ifctx;
1608 printf("swapfs %s ", p);
1610 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1614 /* swap mount options */
1615 mountopts(&nd->swap_args, p);
1616 printf("swapopts %s ", p);
1619 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1624 if (gctx->tag.taglen != 4)
1626 "Expected 4 bytes for swaplen, "
1629 bcopy(p, &swaplen, 4);
1630 nd->swap_nblks = ntohl(swaplen);
1631 printf("swapsize %d KB ",
1635 panic("Failed to set swapfs to %s", p);
1638 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1641 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1642 panic("bootpc: hostname >= %d bytes",
1644 if (gctx->sethostname != NULL) {
1645 printf("hostname %s (ignored) ", p);
1647 strcpy(nd->my_hostnam, p);
1648 strcpy(hostname, p);
1649 printf("hostname %s ",hostname);
1650 gctx->sethostname = ifctx;
1653 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1655 if (p != NULL) { /* store in a sysctl variable */
1656 int i, l = sizeof(bootp_cookie) - 1;
1657 for (i = 0; i < l && p[i] != '\0'; i++)
1658 bootp_cookie[i] = p[i];
1664 if (ifctx->gotnetmask == 0) {
1665 if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1666 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1667 else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1668 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1670 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1672 if (ifctx->gotgw == 0) {
1674 ifctx->gw.sin_addr.s_addr = ifctx->myaddr.sin_addr.s_addr;
1681 struct bootpc_ifcontext *ifctx, *nctx; /* Interface BOOTP contexts */
1682 struct bootpc_globalcontext *gctx; /* Global BOOTP context */
1685 struct nfsv3_diskless *nd;
1688 nd = &nfsv3_diskless;
1692 * If already filled in, don't touch it here
1694 if (nfs_diskless_valid != 0)
1698 * Wait until arp entries can be handled.
1700 while (time_second == 0)
1701 tsleep(&time_second, 0, "arpkludge", 10);
1703 gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK);
1705 panic("Failed to allocate bootp global context structure");
1707 bzero(gctx, sizeof(*gctx));
1708 gctx->xid = ~0xFFFF;
1709 gctx->starttime = time_second;
1711 ifctx = allocifctx(gctx);
1714 * Find a network interface.
1716 #ifdef BOOTP_WIRED_TO
1717 printf("bootpc_init: wired to interface '%s'\n",
1718 __XSTRING(BOOTP_WIRED_TO));
1720 bzero(&ifctx->ireq, sizeof(ifctx->ireq));
1721 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1722 strlcpy(ifctx->ireq.ifr_name, ifp->if_xname,
1723 sizeof(ifctx->ireq.ifr_name));
1724 #ifdef BOOTP_WIRED_TO
1725 if (strcmp(ifctx->ireq.ifr_name,
1726 __XSTRING(BOOTP_WIRED_TO)) != 0)
1729 if ((ifp->if_flags &
1730 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1734 if (gctx->interfaces != NULL)
1735 gctx->lastinterface->next = ifctx;
1737 gctx->interfaces = ifctx;
1739 gctx->lastinterface = ifctx;
1740 ifctx = allocifctx(gctx);
1742 free(ifctx, M_TEMP);
1744 if (gctx->interfaces == NULL) {
1745 #ifdef BOOTP_WIRED_TO
1746 panic("bootpc_init: Could not find interface specified "
1747 "by BOOTP_WIRED_TO: "
1748 __XSTRING(BOOTP_WIRED_TO));
1750 panic("bootpc_init: no suitable interface");
1754 gctx->gotrootpath = 0;
1755 gctx->gotswappath = 0;
1758 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1759 bootpc_fakeup_interface(ifctx, gctx, td);
1761 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1762 bootpc_compose_query(ifctx, gctx, td);
1764 ifctx = gctx->interfaces;
1765 error = bootpc_call(gctx, td);
1768 #ifdef BOOTP_NFSROOT
1769 panic("BOOTP call failed");
1771 printf("BOOTP call failed\n");
1775 mountopts(&nd->root_args, NULL);
1777 mountopts(&nd->swap_args, NULL);
1779 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1780 if (bootpc_ifctx_isresolved(ifctx) != 0)
1781 bootpc_decode_reply(nd, ifctx, gctx);
1783 if (gctx->gotswappath == 0)
1785 #ifdef BOOTP_NFSROOT
1786 if (gctx->gotrootpath == 0)
1787 panic("bootpc: No root path offered");
1790 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
1791 bootpc_adjust_interface(ifctx, gctx, td);
1796 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1797 if (ifctx->gotrootpath != 0)
1799 if (ifctx == NULL) {
1800 for (ifctx = gctx->interfaces;
1802 ifctx = ifctx->next)
1803 if (bootpc_ifctx_isresolved(ifctx) != 0)
1809 if (gctx->gotrootpath != 0) {
1811 error = md_mount(&nd->root_saddr, nd->root_hostnam,
1812 nd->root_fh, &nd->root_fhsize,
1813 &nd->root_args, td);
1815 panic("nfs_boot: mountd root, error=%d", error);
1817 if (gctx->gotswappath != 0) {
1819 error = md_mount(&nd->swap_saddr,
1821 nd->swap_fh, &nd->swap_fhsize,
1822 &nd->swap_args, td);
1824 panic("nfs_boot: mountd swap, error=%d",
1827 error = md_lookup_swap(&nd->swap_saddr,
1829 nd->swap_fh, &nd->swap_fhsize,
1830 &nd->swap_args, td);
1832 panic("nfs_boot: lookup swap, error=%d",
1835 nfs_diskless_valid = 3;
1838 strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1839 bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1840 bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1841 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1842 ifctx->myaddr.sin_addr.s_addr |
1843 ~ ifctx->netmask.sin_addr.s_addr;
1844 bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1847 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = nctx) {
1849 free(ifctx, M_TEMP);
1857 * Given a server pathname, get an NFS file handle.
1858 * Also, sets sin->sin_port to the NFS service port.
1861 md_mount(struct sockaddr_in *mdsin, /* mountd server address */
1865 struct nfs_args *args,
1875 /* First try NFS v3 */
1876 /* Get port number for MOUNTD. */
1877 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1878 &mdsin->sin_port, td);
1880 m = xdr_string_encode(path, strlen(path));
1882 /* Do RPC to mountd. */
1883 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1884 RPCMNT_MOUNT, &m, NULL, td);
1887 args->flags |= NFSMNT_NFSV3;
1890 /* Fallback to NFS v2 */
1892 /* Get port number for MOUNTD. */
1893 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1894 &mdsin->sin_port, td);
1898 m = xdr_string_encode(path, strlen(path));
1900 /* Do RPC to mountd. */
1901 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1902 RPCMNT_MOUNT, &m, NULL, td);
1904 return error; /* message already freed */
1910 if (xdr_int_decode(&m, &error) != 0 || error != 0)
1913 if ((args->flags & NFSMNT_NFSV3) != 0) {
1914 if (xdr_int_decode(&m, fhsizep) != 0 ||
1915 *fhsizep > NFSX_V3FHMAX ||
1919 *fhsizep = NFSX_V2FH;
1921 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
1924 if (args->flags & NFSMNT_NFSV3) {
1925 if (xdr_int_decode(&m, &authcount) != 0)
1928 if (authcount < 0 || authcount > 100)
1930 while (authcount > 0) {
1931 if (xdr_int_decode(&m, &authver) != 0)
1933 if (authver == RPCAUTH_UNIX)
1937 if (authunixok == 0)
1941 /* Set port number for NFS use. */
1942 error = krpc_portmap(mdsin, NFS_PROG,
1944 NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
1945 &mdsin->sin_port, td);
1959 md_lookup_swap(struct sockaddr_in *mdsin, /* mountd server address */
1963 struct nfs_args *args,
1969 int attribs_present;
1976 m = m_get(MB_WAIT,MT_DATA);
1980 if ((args->flags & NFSMNT_NFSV3) != 0) {
1981 *mtod(m, u_int32_t *) = txdr_unsigned(*fhsizep);
1982 bcopy(fhp, mtod(m, u_char *) + sizeof(u_int32_t), *fhsizep);
1983 m->m_len = *fhsizep + sizeof(u_int32_t);
1985 bcopy(fhp, mtod(m, u_char *), NFSX_V2FH);
1986 m->m_len = NFSX_V2FH;
1989 m->m_next = xdr_string_encode(path, strlen(path));
1990 if (m->m_next == NULL) {
1995 /* Do RPC to nfsd. */
1996 if ((args->flags & NFSMNT_NFSV3) != 0)
1997 error = krpc_call(mdsin, NFS_PROG, NFS_VER3,
1998 NFSPROC_LOOKUP, &m, NULL, td);
2000 error = krpc_call(mdsin, NFS_PROG, NFS_VER2,
2001 NFSV2PROC_LOOKUP, &m, NULL, td);
2003 return error; /* message already freed */
2005 if (xdr_int_decode(&m, &status) != 0)
2012 if ((args->flags & NFSMNT_NFSV3) != 0) {
2013 if (xdr_int_decode(&m, fhsizep) != 0 ||
2014 *fhsizep > NFSX_V3FHMAX ||
2018 *fhsizep = NFSX_V2FH;
2020 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
2023 if ((args->flags & NFSMNT_NFSV3) != 0) {
2024 if (xdr_int_decode(&m, &attribs_present) != 0)
2026 if (attribs_present != 0) {
2027 if (xdr_opaque_decode(&m, (u_char *) &fattribs.v3,
2028 sizeof(u_int32_t) * 21) != 0)
2030 size = fxdr_unsigned(u_int32_t, fattribs.v3[6]);
2033 if (xdr_opaque_decode(&m,(u_char *) &fattribs.v2,
2034 sizeof(u_int32_t) * 17) != 0)
2036 size = fxdr_unsigned(u_int32_t, fattribs.v2[5]);
2039 if (nfsv3_diskless.swap_nblks == 0 && size != -1) {
2040 nfsv3_diskless.swap_nblks = size / 1024;
2041 printf("md_lookup_swap: Swap size is %d KB\n",
2042 nfsv3_diskless.swap_nblks);