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.2 2003/06/17 04:28:54 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>
68 #include <nfs/rpcv2.h>
69 #include <nfs/nfsproto.h>
71 #include <nfs/nfsdiskless.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, 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];
220 SYSCTL_STRING(_kern, OID_AUTO, bootp_cookie, CTLFLAG_RD,
221 bootp_cookie, 0, "Cookie (T134) supplied by bootp server");
224 static int md_mount(struct sockaddr_in *mdsin, char *path,
225 u_char *fhp, int *fhsizep,
226 struct nfs_args *args,struct proc *procp);
227 static int md_lookup_swap(struct sockaddr_in *mdsin,char *path,
228 u_char *fhp, int *fhsizep,
229 struct nfs_args *args,
231 static int setfs(struct sockaddr_in *addr, char *path, char *p);
232 static int getdec(char **ptr);
233 static char *substr(char *a,char *b);
234 static void mountopts(struct nfs_args *args, char *p);
235 static int xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len);
236 static int xdr_int_decode(struct mbuf **ptr, int *iptr);
237 static void print_in_addr(struct in_addr addr);
238 static void print_sin_addr(struct sockaddr_in *addr);
239 static void clear_sinaddr(struct sockaddr_in *sin);
241 struct bootpc_ifcontext *allocifctx(struct bootpc_globalcontext *gctx);
242 static void bootpc_compose_query(struct bootpc_ifcontext *ifctx,
243 struct bootpc_globalcontext *gctx,
245 static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx,
246 struct bootp_packet *bp, int len, int tag);
247 static void bootpc_tag_helper(struct bootpc_tagcontext *tctx,
248 unsigned char *start, int len, int tag);
251 void bootpboot_p_sa(struct sockaddr *sa,struct sockaddr *ma);
252 void bootpboot_p_ma(struct sockaddr *ma);
253 void bootpboot_p_rtentry(struct rtentry *rt);
254 void bootpboot_p_tree(struct radix_node *rn);
255 void bootpboot_p_rtlist(void);
256 void bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa);
257 void bootpboot_p_iflist(void);
260 static int bootpc_call(struct bootpc_globalcontext *gctx,
263 static int bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
264 struct bootpc_globalcontext *gctx,
267 static int bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
268 struct bootpc_globalcontext *gctx,
271 static void bootpc_decode_reply(struct nfsv3_diskless *nd,
272 struct bootpc_ifcontext *ifctx,
273 struct bootpc_globalcontext *gctx);
275 static int bootpc_received(struct bootpc_globalcontext *gctx,
276 struct bootpc_ifcontext *ifctx);
278 static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
279 static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
280 static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
282 void bootpc_init(void);
285 * In order to have multiple active interfaces with address 0.0.0.0
286 * and be able to send data to a selected interface, we perform
289 * - The 'broadcast' address is different for each interface.
291 * - We temporarily add routing pointing 255.255.255.255 to the
292 * selected interface broadcast address, thus the packet sent
293 * goes to that interface.
298 bootpboot_p_sa(struct sockaddr *sa,
302 printf("(sockaddr *) <null>");
305 switch (sa->sa_family) {
308 struct sockaddr_in *sin;
310 sin = (struct sockaddr_in *) sa;
314 sin = (struct sockaddr_in *) ma;
322 struct sockaddr_dl *sli;
325 sli = (struct sockaddr_dl *) sa;
326 printf("link %.*s ", sli->sdl_nlen, sli->sdl_data);
327 for (i = 0; i < sli->sdl_alen; i++) {
330 printf("%x", ((unsigned char *) LLADDR(sli))[i]);
335 printf("af%d", sa->sa_family);
341 bootpboot_p_ma(struct sockaddr *ma)
347 printf("%x", *(int *)ma);
352 bootpboot_p_rtentry(struct rtentry *rt)
354 bootpboot_p_sa(rt_key(rt), rt_mask(rt));
356 bootpboot_p_ma(rt->rt_genmask);
358 bootpboot_p_sa(rt->rt_gateway, NULL);
360 printf("flags %x", (unsigned short) rt->rt_flags);
361 printf(" %d", (int) rt->rt_rmx.rmx_expire);
362 printf(" %s%d\n", rt->rt_ifp->if_name, rt->rt_ifp->if_unit);
367 bootpboot_p_tree(struct radix_node *rn)
370 if (rn->rn_bit < 0) {
371 if ((rn->rn_flags & RNF_ROOT) != 0) {
373 bootpboot_p_rtentry((struct rtentry *) rn);
375 rn = rn->rn_dupedkey;
377 bootpboot_p_tree(rn->rn_left);
378 bootpboot_p_tree(rn->rn_right);
386 bootpboot_p_rtlist(void)
388 printf("Routing table:\n");
389 bootpboot_p_tree(rt_tables[AF_INET]->rnh_treetop);
394 bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
396 printf("%s%d flags %x, addr ",
399 (unsigned short) ifp->if_flags);
400 print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
401 printf(", broadcast ");
402 print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
403 printf(", netmask ");
404 print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
410 bootpboot_p_iflist(void)
415 printf("Interface list:\n");
416 for (ifp = TAILQ_FIRST(&ifnet);
418 ifp = TAILQ_NEXT(ifp, if_link)) {
419 for (ifa = TAILQ_FIRST(&ifp->if_addrhead);
421 ifa = TAILQ_NEXT(ifa, ifa_link))
422 if (ifa->ifa_addr->sa_family == AF_INET)
423 bootpboot_p_if(ifp, ifa);
426 #endif /* defined(BOOTP_DEBUG) */
430 clear_sinaddr(struct sockaddr_in *sin)
432 bzero(sin, sizeof(*sin));
433 sin->sin_len = sizeof(*sin);
434 sin->sin_family = AF_INET;
435 sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
440 static struct bootpc_ifcontext *
441 allocifctx(struct bootpc_globalcontext *gctx)
443 struct bootpc_ifcontext *ifctx;
444 ifctx = (struct bootpc_ifcontext *) malloc(sizeof(*ifctx),
447 panic("Failed to allocate bootp interface context structure");
449 bzero(ifctx, sizeof(*ifctx));
450 ifctx->xid = gctx->xid;
452 ifctx->state = IF_BOOTP_UNRESOLVED;
454 ifctx->state = IF_DHCP_UNRESOLVED;
462 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
464 if (ifctx->state == IF_BOOTP_RESOLVED ||
465 ifctx->state == IF_DHCP_RESOLVED)
472 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
474 if (ifctx->state == IF_BOOTP_UNRESOLVED ||
475 ifctx->state == IF_DHCP_UNRESOLVED)
482 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
484 if (ifctx->state == IF_BOOTP_FAILED ||
485 ifctx->state == IF_DHCP_FAILED)
492 bootpc_received(struct bootpc_globalcontext *gctx,
493 struct bootpc_ifcontext *ifctx)
495 unsigned char dhcpreplytype;
498 * Need timeout for fallback to less
499 * desirable alternative.
503 /* This call used for the side effect (badopt flag) */
504 (void) bootpc_tag(&gctx->tmptag, &gctx->reply,
508 /* If packet is invalid, ignore it */
509 if (gctx->tmptag.badopt != 0)
512 p = bootpc_tag(&gctx->tmptag, &gctx->reply,
513 gctx->replylen, TAG_DHCP_MSGTYPE);
517 dhcpreplytype = DHCP_NOMSG;
519 switch (ifctx->dhcpquerytype) {
521 if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */
522 #ifndef BOOTP_FORCE_DHCP
523 && dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */
529 if (dhcpreplytype != DHCP_ACK)
535 /* Ignore packet unless it gives us a root tag we didn't have */
537 if ((ifctx->state == IF_BOOTP_RESOLVED ||
538 (ifctx->dhcpquerytype == DHCP_DISCOVER &&
539 (ifctx->state == IF_DHCP_OFFERED ||
540 ifctx->state == IF_DHCP_RESOLVED))) &&
541 (bootpc_tag(&gctx->tmptag, &ifctx->reply,
544 bootpc_tag(&gctx->tmptag, &gctx->reply,
552 ifctx->replylen = gctx->replylen;
554 /* XXX: Only reset if 'perfect' response */
555 if (ifctx->state == IF_BOOTP_UNRESOLVED)
556 ifctx->state = IF_BOOTP_RESOLVED;
557 else if (ifctx->state == IF_DHCP_UNRESOLVED &&
558 ifctx->dhcpquerytype == DHCP_DISCOVER) {
559 if (dhcpreplytype == DHCP_OFFER)
560 ifctx->state = IF_DHCP_OFFERED;
562 ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */
563 } else if (ifctx->state == IF_DHCP_OFFERED &&
564 ifctx->dhcpquerytype == DHCP_REQUEST)
565 ifctx->state = IF_DHCP_RESOLVED;
568 if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
569 ifctx->state != IF_BOOTP_RESOLVED) {
570 p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
571 ifctx->replylen, TAG_DHCP_SERVERID);
572 if (p != NULL && gctx->tmptag.taglen == 4) {
573 memcpy(&ifctx->dhcpserver, p, 4);
574 ifctx->gotdhcpserver = 1;
576 ifctx->gotdhcpserver = 0;
580 ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
583 ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
585 TAG_ROUTERS) != NULL);
586 ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
588 TAG_SUBNETMASK) != NULL);
593 bootpc_call(struct bootpc_globalcontext *gctx,
597 struct sockaddr_in *sin, dst;
601 int error, on, rcvflg, timo, len;
605 struct bootpc_ifcontext *ifctx;
612 * Create socket and set its recieve timeout.
614 error = socreate(AF_INET, &so, SOCK_DGRAM, 0, procp);
620 bzero(&sopt, sizeof(sopt));
621 sopt.sopt_level = SOL_SOCKET;
622 sopt.sopt_name = SO_RCVTIMEO;
624 sopt.sopt_valsize = sizeof tv;
626 error = sosetopt(so, &sopt);
634 sopt.sopt_name = SO_BROADCAST;
636 sopt.sopt_valsize = sizeof on;
638 error = sosetopt(so, &sopt);
647 sopt.sopt_name = SO_DONTROUTE;
649 sopt.sopt_valsize = sizeof on;
651 error = sosetopt(so, &sopt);
656 * Bind the local endpoint to a bootp client port.
660 sin->sin_port = htons(IPPORT_BOOTPC);
661 error = sobind(so, (struct sockaddr *)sin, procp);
663 printf("bind failed\n");
668 * Setup socket address for the server.
672 sin->sin_addr.s_addr = INADDR_BROADCAST;
673 sin->sin_port = htons(IPPORT_BOOTPS);
676 * Send it, repeatedly, until a reply is received,
677 * but delay each re-send by an increasing amount.
678 * If the delay hits the maximum, start complaining.
687 for (ifctx = gctx->interfaces;
689 ifctx = ifctx->next) {
690 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
691 bootpc_tag(&gctx->tmptag, &ifctx->reply,
697 for (ifctx = gctx->interfaces;
699 ifctx = ifctx->next) {
700 ifctx->outstanding = 0;
701 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
705 if (bootpc_ifctx_isfailed(ifctx) != 0)
709 ifctx->outstanding = 1;
711 /* Proceed to next step in DHCP negotiation */
712 if ((ifctx->state == IF_DHCP_OFFERED &&
713 ifctx->dhcpquerytype != DHCP_REQUEST) ||
714 (ifctx->state == IF_DHCP_UNRESOLVED &&
715 ifctx->dhcpquerytype != DHCP_DISCOVER) ||
716 (ifctx->state == IF_BOOTP_UNRESOLVED &&
717 ifctx->dhcpquerytype != DHCP_NOMSG)) {
719 bootpc_compose_query(ifctx, gctx, procp);
722 /* Send BOOTP request (or re-send). */
724 if (ifctx->sentmsg == 0) {
725 switch(ifctx->dhcpquerytype) {
737 printf("Sending %s packet from "
738 "interface %s (%*D)\n",
740 ifctx->ireq.ifr_name,
741 ifctx->sdl->sdl_alen,
742 (unsigned char *) LLADDR(ifctx->sdl),
747 aio.iov_base = (caddr_t) &ifctx->call;
748 aio.iov_len = sizeof(ifctx->call);
752 auio.uio_segflg = UIO_SYSSPACE;
753 auio.uio_rw = UIO_WRITE;
755 auio.uio_resid = sizeof(ifctx->call);
756 auio.uio_procp = procp;
758 /* Set netmask to 0.0.0.0 */
760 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
762 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
763 (caddr_t) &ifctx->ireq, procp);
766 "set if netmask, error=%d",
769 error = sosend(so, (struct sockaddr *) &dst,
770 &auio, NULL, NULL, 0, procp);
772 printf("bootpc_call: sosend: %d state %08x\n",
773 error, (int) so->so_state);
776 /* XXX: Is this needed ? */
777 tsleep(&error, PZERO + 8, "bootpw", 10);
779 /* Set netmask to 255.0.0.0 */
781 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
783 sin->sin_addr.s_addr = htonl(0xff000000u);
784 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
785 (caddr_t) &ifctx->ireq, procp);
788 "set if netmask, error=%d",
793 if (outstanding == 0 &&
794 (rtimo == 0 || time_second >= rtimo)) {
799 /* Determine new timeout. */
800 if (timo < MAX_RESEND_DELAY)
803 printf("DHCP/BOOTP timeout for server ");
804 print_sin_addr(&dst);
809 * Wait for up to timo seconds for a reply.
810 * The socket receive timeout was set to 1 second.
812 atimo = timo + time_second;
813 while (time_second < atimo) {
814 aio.iov_base = (caddr_t) &gctx->reply;
815 aio.iov_len = sizeof(gctx->reply);
819 auio.uio_segflg = UIO_SYSSPACE;
820 auio.uio_rw = UIO_READ;
822 auio.uio_resid = sizeof(gctx->reply);
823 auio.uio_procp = procp;
826 error = soreceive(so, NULL, &auio,
827 NULL, NULL, &rcvflg);
828 gctx->secs = time_second - gctx->starttime;
829 for (ifctx = gctx->interfaces;
831 ifctx = ifctx->next) {
832 if (bootpc_ifctx_isresolved(ifctx) != 0 ||
833 bootpc_ifctx_isfailed(ifctx) != 0)
836 ifctx->call.secs = htons(gctx->secs);
838 if (error == EWOULDBLOCK)
842 len = sizeof(gctx->reply) - auio.uio_resid;
844 /* Do we have the required number of bytes ? */
845 if (len < BOOTP_MIN_LEN)
847 gctx->replylen = len;
850 if (gctx->reply.op != BOOTP_REPLY)
853 /* Is this an answer to our query */
854 for (ifctx = gctx->interfaces;
856 ifctx = ifctx->next) {
857 if (gctx->reply.xid != ifctx->call.xid)
860 /* Same HW address size ? */
861 if (gctx->reply.hlen != ifctx->call.hlen)
864 /* Correct HW address ? */
865 if (bcmp(gctx->reply.chaddr,
867 ifctx->call.hlen) != 0)
874 s = bootpc_tag(&gctx->tmptag,
887 s = "DHCP (unexpected)";
893 printf("Received %s packet"
896 ifctx->ireq.ifr_name);
897 print_in_addr(gctx->reply.siaddr);
898 if (gctx->reply.giaddr.s_addr !=
901 print_in_addr(gctx->reply.giaddr);
903 if (bootpc_received(gctx, ifctx) != 0) {
904 printf(" (accepted)");
905 if (ifctx->outstanding) {
906 ifctx->outstanding = 0;
909 /* Network settle delay */
910 if (outstanding == 0)
911 atimo = time_second +
914 printf(" (ignored)");
915 if (ifctx->gotrootpath) {
917 rtimo = time_second +
919 printf(" (got root path)");
921 printf(" (no root path)");
926 if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
929 /* Force a retry if halfway in DHCP negotiation */
931 for (ifctx = gctx->interfaces; ifctx != NULL;
932 ifctx = ifctx->next) {
933 if (ifctx->state == IF_DHCP_OFFERED) {
934 if (ifctx->dhcpquerytype == DHCP_DISCOVER)
937 ifctx->state = IF_DHCP_UNRESOLVED;
944 if (gotrootpath != 0) {
945 gctx->gotrootpath = gotrootpath;
946 if (rtimo != 0 && time_second >= rtimo)
949 } /* forever send/receive */
952 * XXX: These are errors of varying seriousness being silently
956 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
957 if (bootpc_ifctx_isresolved(ifctx) == 0) {
958 printf("%s timeout for interface %s\n",
959 ifctx->dhcpquerytype != DHCP_NOMSG ?
961 ifctx->ireq.ifr_name);
964 if (gctx->gotrootpath != 0) {
966 printf("Got a root path, ignoring remaining timeout\n");
971 #ifndef BOOTP_NFSROOT
972 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
973 if (bootpc_ifctx_isresolved(ifctx) != 0) {
990 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
991 struct bootpc_globalcontext *gctx,
994 struct sockaddr_in *sin;
1000 struct sockaddr_dl *sdl;
1002 error = socreate(AF_INET, &ifctx->so, SOCK_DGRAM, 0, procp);
1004 panic("nfs_boot: socreate, error=%d", error);
1006 ireq = &ifctx->ireq;
1010 * Bring up the interface.
1012 * Get the old interface flags and or IFF_UP into them; if
1013 * IFF_UP set blindly, interface selection can be clobbered.
1015 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, procp);
1017 panic("bootpc_fakeup_interface: GIFFLAGS, error=%d", error);
1018 ireq->ifr_flags |= IFF_UP;
1019 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, procp);
1021 panic("bootpc_fakeup_interface: SIFFLAGS, error=%d", error);
1024 * Do enough of ifconfig(8) so that the chosen interface
1025 * can talk to the servers. (just set the address)
1028 /* addr is 0.0.0.0 */
1030 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1032 error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, procp);
1033 if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1034 panic("bootpc_fakeup_interface: "
1035 "set if addr, error=%d", error);
1037 /* netmask is 255.0.0.0 */
1039 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1041 sin->sin_addr.s_addr = htonl(0xff000000u);
1042 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t)ireq, procp);
1044 panic("bootpc_fakeup_interface: set if netmask, error=%d",
1047 /* Broadcast is 255.255.255.255 */
1049 sin = (struct sockaddr_in *)&ireq->ifr_addr;
1051 clear_sinaddr(&ifctx->broadcast);
1052 sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
1053 ifctx->broadcast.sin_addr.s_addr = sin->sin_addr.s_addr;
1055 error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t)ireq, procp);
1056 if (error != 0 && error != EADDRNOTAVAIL)
1057 panic("bootpc_fakeup_interface: "
1058 "set if broadcast addr, error=%d",
1062 /* Get HW address */
1065 for (ifa = TAILQ_FIRST(&ifctx->ifp->if_addrhead);
1067 ifa = TAILQ_NEXT(ifa,ifa_link))
1068 if (ifa->ifa_addr->sa_family == AF_LINK &&
1069 (sdl = ((struct sockaddr_dl *) ifa->ifa_addr)) != NULL &&
1070 sdl->sdl_type == IFT_ETHER)
1074 panic("bootpc: Unable to find HW address for %s",
1075 ifctx->ireq.ifr_name);
1083 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
1084 struct bootpc_globalcontext *gctx,
1088 struct sockaddr_in defdst;
1089 struct sockaddr_in defmask;
1090 struct sockaddr_in *sin;
1094 struct sockaddr_in *myaddr;
1095 struct sockaddr_in *netmask;
1096 struct sockaddr_in *gw;
1098 ireq = &ifctx->ireq;
1100 myaddr = &ifctx->myaddr;
1101 netmask = &ifctx->netmask;
1104 if (bootpc_ifctx_isresolved(ifctx) == 0) {
1106 /* Shutdown interfaces where BOOTP failed */
1108 printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
1109 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, procp);
1111 panic("bootpc_adjust_interface: "
1112 "SIOCGIFFLAGS, error=%d", error);
1113 ireq->ifr_flags &= ~IFF_UP;
1114 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, procp);
1116 panic("bootpc_adjust_interface: "
1117 "SIOCSIFFLAGS, error=%d", error);
1119 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1121 error = ifioctl(so, SIOCDIFADDR, (caddr_t) ireq, procp);
1122 if (error != 0 && (error != EADDRNOTAVAIL ||
1123 ifctx == gctx->interfaces))
1124 panic("bootpc_adjust_interface: "
1125 "SIOCDIFADDR, error=%d", error);
1130 printf("Adjusted interface %s\n", ifctx->ireq.ifr_name);
1132 * Do enough of ifconfig(8) so that the chosen interface
1133 * can talk to the servers. (just set the address)
1135 bcopy(netmask, &ireq->ifr_addr, sizeof(*netmask));
1136 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t) ireq, procp);
1138 panic("bootpc_adjust_interface: "
1139 "set if netmask, error=%d", error);
1141 /* Broadcast is with host part of IP address all 1's */
1143 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1145 sin->sin_addr.s_addr = myaddr->sin_addr.s_addr |
1146 ~ netmask->sin_addr.s_addr;
1147 error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t) ireq, procp);
1149 panic("bootpc_adjust_interface: "
1150 "set if broadcast addr, error=%d", error);
1152 bcopy(myaddr, &ireq->ifr_addr, sizeof(*myaddr));
1153 error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, procp);
1154 if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1155 panic("bootpc_adjust_interface: "
1156 "set if addr, error=%d", error);
1158 /* Add new default route */
1160 if (ifctx->gotgw != 0 || gctx->gotgw == 0) {
1161 clear_sinaddr(&defdst);
1162 clear_sinaddr(&defmask);
1163 error = rtrequest(RTM_ADD,
1164 (struct sockaddr *) &defdst,
1165 (struct sockaddr *) gw,
1166 (struct sockaddr *) &defmask,
1167 (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
1169 printf("bootpc_adjust_interface: "
1170 "add net route, error=%d\n", error);
1180 setfs(struct sockaddr_in *addr, char *path, char *p)
1186 if (((val = getdec(&p)) < 0) || (val > 255))
1192 if (((val = getdec(&p)) < 0) || (val > 255))
1198 if (((val = getdec(&p)) < 0) || (val > 255))
1204 if (((val = getdec(&p)) < 0) || (val > 255))
1211 addr->sin_addr.s_addr = htonl(ip);
1212 addr->sin_len = sizeof(struct sockaddr_in);
1213 addr->sin_family = AF_INET;
1215 strncpy(path, p, MNAMELEN - 1);
1228 if ((*p < '0') || (*p > '9'))
1230 while ((*p >= '0') && (*p <= '9')) {
1231 ret = ret * 10 + (*p - '0');
1240 substr(char *a, char *b)
1245 while (*a != '\0') {
1248 while (*loc1 == *loc2++) {
1262 mountopts(struct nfs_args *args, char *p)
1266 args->version = NFS_ARGSVERSION;
1269 args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
1270 args->sotype = SOCK_DGRAM;
1273 if ((tmp = (char *)substr(p, "rsize=")))
1274 args->rsize = getdec(&tmp);
1275 if ((tmp = (char *)substr(p, "wsize=")))
1276 args->wsize = getdec(&tmp);
1277 if ((tmp = (char *)substr(p, "intr")))
1278 args->flags |= NFSMNT_INT;
1279 if ((tmp = (char *)substr(p, "soft")))
1280 args->flags |= NFSMNT_SOFT;
1281 if ((tmp = (char *)substr(p, "noconn")))
1282 args->flags |= NFSMNT_NOCONN;
1283 if ((tmp = (char *)substr(p, "tcp")))
1284 args->sotype = SOCK_STREAM;
1289 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
1295 alignedlen = ( len + 3 ) & ~3;
1297 if (m->m_len < alignedlen) {
1298 m = m_pullup(m, alignedlen);
1304 bcopy(mtod(m, u_char *), buf, len);
1305 m_adj(m, alignedlen);
1312 xdr_int_decode(struct mbuf **mptr, int *iptr)
1315 if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
1317 *iptr = fxdr_unsigned(u_int32_t, i);
1323 print_sin_addr(struct sockaddr_in *sin)
1325 print_in_addr(sin->sin_addr);
1330 print_in_addr(struct in_addr addr)
1334 ip = ntohl(addr.s_addr);
1335 printf("%d.%d.%d.%d",
1336 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1340 bootpc_compose_query(ifctx, gctx, procp)
1341 struct bootpc_ifcontext *ifctx;
1342 struct bootpc_globalcontext *gctx;
1345 unsigned char *vendp;
1346 unsigned char vendor_client[64];
1348 uint8_t vendor_client_len;
1350 ifctx->gotrootpath = 0;
1352 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1355 ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */
1356 ifctx->call.htype = 1; /* 10mb ethernet */
1357 ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1358 ifctx->call.hops = 0;
1359 if (bootpc_ifctx_isunresolved(ifctx) != 0)
1361 ifctx->call.xid = txdr_unsigned(ifctx->xid);
1362 bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1364 vendp = ifctx->call.vend;
1365 *vendp++ = 99; /* RFC1048 cookie */
1369 *vendp++ = TAG_MAXMSGSIZE;
1371 *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1372 *vendp++ = sizeof(struct bootp_packet) & 255;
1374 snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1375 ostype, MACHINE, osrelease);
1376 vendor_client_len = strlen(vendor_client);
1377 *vendp++ = TAG_VENDOR_INDENTIFIER;
1378 *vendp++ = vendor_client_len;
1379 memcpy(vendp, vendor_client, vendor_client_len);
1380 vendp += vendor_client_len;;
1381 ifctx->dhcpquerytype = DHCP_NOMSG;
1382 switch (ifctx->state) {
1383 case IF_DHCP_UNRESOLVED:
1384 *vendp++ = TAG_DHCP_MSGTYPE;
1386 *vendp++ = DHCP_DISCOVER;
1387 ifctx->dhcpquerytype = DHCP_DISCOVER;
1388 ifctx->gotdhcpserver = 0;
1390 case IF_DHCP_OFFERED:
1391 *vendp++ = TAG_DHCP_MSGTYPE;
1393 *vendp++ = DHCP_REQUEST;
1394 ifctx->dhcpquerytype = DHCP_REQUEST;
1395 *vendp++ = TAG_DHCP_REQ_ADDR;
1397 memcpy(vendp, &ifctx->reply.yiaddr, 4);
1399 if (ifctx->gotdhcpserver != 0) {
1400 *vendp++ = TAG_DHCP_SERVERID;
1402 memcpy(vendp, &ifctx->dhcpserver, 4);
1405 *vendp++ = TAG_DHCP_LEASETIME;
1407 leasetime = htonl(300);
1408 memcpy(vendp, &leasetime, 4);
1415 ifctx->call.secs = 0;
1416 ifctx->call.flags = htons(0x8000); /* We need an broadcast answer */
1421 bootpc_hascookie(struct bootp_packet *bp)
1423 return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1424 bp->vend[2] == 83 && bp->vend[3] == 99);
1429 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1430 unsigned char *start,
1438 if (tctx->badtag != 0 || tctx->badopt != 0)
1446 if (code == TAG_PAD)
1448 if (code == TAG_END)
1450 if (j >= ej || j + *j + 1 > ej) {
1456 if (tctx->taglen + len > TAG_MAXLEN) {
1462 memcpy(tctx->buf + tctx->taglen,
1464 tctx->taglen += len;
1466 if (code == TAG_OVERLOAD)
1467 tctx->overload = *j;
1474 static unsigned char *
1475 bootpc_tag(struct bootpc_tagcontext *tctx,
1476 struct bootp_packet *bp,
1489 if (bootpc_hascookie(bp) == 0)
1493 ej = (unsigned char *) bp + len;
1495 bootpc_tag_helper(tctx, &bp->vend[4],
1496 (unsigned char *) bp + len - &bp->vend[4], tag);
1498 if ((tctx->overload & OVERLOAD_FILE) != 0)
1499 bootpc_tag_helper(tctx,
1500 (unsigned char *) bp->file,
1503 if ((tctx->overload & OVERLOAD_SNAME) != 0)
1504 bootpc_tag_helper(tctx,
1505 (unsigned char *) bp->sname,
1509 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1511 tctx->buf[tctx->taglen] = '\0';
1517 bootpc_decode_reply(nd, ifctx, gctx)
1518 struct nfsv3_diskless *nd;
1519 struct bootpc_ifcontext *ifctx;
1520 struct bootpc_globalcontext *gctx;
1526 ifctx->gotnetmask = 0;
1528 clear_sinaddr(&ifctx->myaddr);
1529 clear_sinaddr(&ifctx->netmask);
1530 clear_sinaddr(&ifctx->gw);
1532 ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1534 ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1535 snprintf(gctx->lookup_path, sizeof(gctx->lookup_path),
1537 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1539 printf("%s at ", ifctx->ireq.ifr_name);
1540 print_sin_addr(&ifctx->myaddr);
1542 print_in_addr(ifctx->reply.siaddr);
1544 ifctx->gw.sin_addr = ifctx->reply.giaddr;
1545 if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1546 printf(" via gateway ");
1547 print_in_addr(ifctx->reply.giaddr);
1550 /* This call used for the side effect (overload flag) */
1551 (void) bootpc_tag(&gctx->tmptag,
1552 &ifctx->reply, ifctx->replylen, TAG_END);
1554 if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1555 if (ifctx->reply.sname[0] != '\0')
1556 printf(" server name %s", ifctx->reply.sname);
1557 if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1558 if (ifctx->reply.file[0] != '\0')
1559 printf(" boot file %s", ifctx->reply.file);
1563 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1566 if (gctx->tag.taglen != 4)
1567 panic("bootpc: subnet mask len is %d",
1569 bcopy(p, &ifctx->netmask.sin_addr, 4);
1570 ifctx->gotnetmask = 1;
1571 printf("subnet mask ");
1572 print_sin_addr(&ifctx->netmask);
1576 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1580 if (gctx->tag.taglen % 4)
1581 panic("bootpc: Router Len is %d", gctx->tag.taglen);
1582 if (gctx->tag.taglen > 0) {
1583 bcopy(p, &ifctx->gw.sin_addr, 4);
1585 print_sin_addr(&ifctx->gw);
1592 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1595 if (gctx->setrootfs != NULL) {
1596 printf("rootfs %s (ignored) ", p);
1597 } else if (setfs(&nd->root_saddr,
1598 nd->root_hostnam, p)) {
1599 printf("rootfs %s ",p);
1600 gctx->gotrootpath = 1;
1601 ifctx->gotrootpath = 1;
1602 gctx->setrootfs = ifctx;
1604 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1608 mountopts(&nd->root_args, p);
1609 printf("rootopts %s ", p);
1612 panic("Failed to set rootfs to %s",p);
1615 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1618 if (gctx->setswapfs != NULL) {
1619 printf("swapfs %s (ignored) ", p);
1620 } else if (setfs(&nd->swap_saddr,
1621 nd->swap_hostnam, p)) {
1622 gctx->gotswappath = 1;
1623 gctx->setswapfs = ifctx;
1624 printf("swapfs %s ", p);
1626 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1630 /* swap mount options */
1631 mountopts(&nd->swap_args, p);
1632 printf("swapopts %s ", p);
1635 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1640 if (gctx->tag.taglen != 4)
1642 "Expected 4 bytes for swaplen, "
1645 bcopy(p, &swaplen, 4);
1646 nd->swap_nblks = ntohl(swaplen);
1647 printf("swapsize %d KB ",
1651 panic("Failed to set swapfs to %s", p);
1654 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1657 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1658 panic("bootpc: hostname >= %d bytes",
1660 if (gctx->sethostname != NULL) {
1661 printf("hostname %s (ignored) ", p);
1663 strcpy(nd->my_hostnam, p);
1664 strcpy(hostname, p);
1665 printf("hostname %s ",hostname);
1666 gctx->sethostname = ifctx;
1669 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1671 if (p != NULL) { /* store in a sysctl variable */
1672 int i, l = sizeof(bootp_cookie) - 1;
1673 for (i = 0; i < l && p[i] != '\0'; i++)
1674 bootp_cookie[i] = p[i];
1680 if (ifctx->gotnetmask == 0) {
1681 if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1682 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1683 else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1684 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1686 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1688 if (ifctx->gotgw == 0) {
1690 ifctx->gw.sin_addr.s_addr = ifctx->myaddr.sin_addr.s_addr;
1697 struct bootpc_ifcontext *ifctx, *nctx; /* Interface BOOTP contexts */
1698 struct bootpc_globalcontext *gctx; /* Global BOOTP context */
1701 struct nfsv3_diskless *nd;
1704 nd = &nfsv3_diskless;
1708 * If already filled in, don't touch it here
1710 if (nfs_diskless_valid != 0)
1714 * Wait until arp entries can be handled.
1716 while (time_second == 0)
1717 tsleep(&time_second, PZERO + 8, "arpkludge", 10);
1719 gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK);
1721 panic("Failed to allocate bootp global context structure");
1723 bzero(gctx, sizeof(*gctx));
1724 gctx->xid = ~0xFFFF;
1725 gctx->starttime = time_second;
1727 ifctx = allocifctx(gctx);
1730 * Find a network interface.
1732 #ifdef BOOTP_WIRED_TO
1733 printf("bootpc_init: wired to interface '%s'\n",
1734 __XSTRING(BOOTP_WIRED_TO));
1736 bzero(&ifctx->ireq, sizeof(ifctx->ireq));
1737 for (ifp = TAILQ_FIRST(&ifnet);
1739 ifp = TAILQ_NEXT(ifp, if_link)) {
1740 snprintf(ifctx->ireq.ifr_name, sizeof(ifctx->ireq.ifr_name),
1741 "%s%d", ifp->if_name, ifp->if_unit);
1742 #ifdef BOOTP_WIRED_TO
1743 if (strcmp(ifctx->ireq.ifr_name,
1744 __XSTRING(BOOTP_WIRED_TO)) != 0)
1747 if ((ifp->if_flags &
1748 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1752 if (gctx->interfaces != NULL)
1753 gctx->lastinterface->next = ifctx;
1755 gctx->interfaces = ifctx;
1757 gctx->lastinterface = ifctx;
1758 ifctx = allocifctx(gctx);
1760 free(ifctx, M_TEMP);
1762 if (gctx->interfaces == NULL) {
1763 #ifdef BOOTP_WIRED_TO
1764 panic("bootpc_init: Could not find interface specified "
1765 "by BOOTP_WIRED_TO: "
1766 __XSTRING(BOOTP_WIRED_TO));
1768 panic("bootpc_init: no suitable interface");
1772 gctx->gotrootpath = 0;
1773 gctx->gotswappath = 0;
1776 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1777 bootpc_fakeup_interface(ifctx, gctx, procp);
1779 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1780 bootpc_compose_query(ifctx, gctx, procp);
1782 ifctx = gctx->interfaces;
1783 error = bootpc_call(gctx, procp);
1786 #ifdef BOOTP_NFSROOT
1787 panic("BOOTP call failed");
1789 printf("BOOTP call failed\n");
1793 mountopts(&nd->root_args, NULL);
1795 mountopts(&nd->swap_args, NULL);
1797 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1798 if (bootpc_ifctx_isresolved(ifctx) != 0)
1799 bootpc_decode_reply(nd, ifctx, gctx);
1801 if (gctx->gotswappath == 0)
1803 #ifdef BOOTP_NFSROOT
1804 if (gctx->gotrootpath == 0)
1805 panic("bootpc: No root path offered");
1808 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
1809 bootpc_adjust_interface(ifctx, gctx, procp);
1814 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1815 if (ifctx->gotrootpath != 0)
1817 if (ifctx == NULL) {
1818 for (ifctx = gctx->interfaces;
1820 ifctx = ifctx->next)
1821 if (bootpc_ifctx_isresolved(ifctx) != 0)
1827 if (gctx->gotrootpath != 0) {
1829 error = md_mount(&nd->root_saddr, nd->root_hostnam,
1830 nd->root_fh, &nd->root_fhsize,
1831 &nd->root_args, procp);
1833 panic("nfs_boot: mountd root, error=%d", error);
1835 if (gctx->gotswappath != 0) {
1837 error = md_mount(&nd->swap_saddr,
1839 nd->swap_fh, &nd->swap_fhsize,
1840 &nd->swap_args, procp);
1842 panic("nfs_boot: mountd swap, error=%d",
1845 error = md_lookup_swap(&nd->swap_saddr,
1847 nd->swap_fh, &nd->swap_fhsize,
1848 &nd->swap_args, procp);
1850 panic("nfs_boot: lookup swap, error=%d",
1853 nfs_diskless_valid = 3;
1856 strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1857 bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1858 bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1859 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1860 ifctx->myaddr.sin_addr.s_addr |
1861 ~ ifctx->netmask.sin_addr.s_addr;
1862 bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1865 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = nctx) {
1867 free(ifctx, M_TEMP);
1875 * Given a server pathname, get an NFS file handle.
1876 * Also, sets sin->sin_port to the NFS service port.
1879 md_mount(struct sockaddr_in *mdsin, /* mountd server address */
1883 struct nfs_args *args,
1893 /* First try NFS v3 */
1894 /* Get port number for MOUNTD. */
1895 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1896 &mdsin->sin_port, procp);
1898 m = xdr_string_encode(path, strlen(path));
1900 /* Do RPC to mountd. */
1901 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1902 RPCMNT_MOUNT, &m, NULL, procp);
1905 args->flags |= NFSMNT_NFSV3;
1908 /* Fallback to NFS v2 */
1910 /* Get port number for MOUNTD. */
1911 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1912 &mdsin->sin_port, procp);
1916 m = xdr_string_encode(path, strlen(path));
1918 /* Do RPC to mountd. */
1919 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1920 RPCMNT_MOUNT, &m, NULL, procp);
1922 return error; /* message already freed */
1928 if (xdr_int_decode(&m, &error) != 0 || error != 0)
1931 if ((args->flags & NFSMNT_NFSV3) != 0) {
1932 if (xdr_int_decode(&m, fhsizep) != 0 ||
1933 *fhsizep > NFSX_V3FHMAX ||
1937 *fhsizep = NFSX_V2FH;
1939 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
1942 if (args->flags & NFSMNT_NFSV3) {
1943 if (xdr_int_decode(&m, &authcount) != 0)
1946 if (authcount < 0 || authcount > 100)
1948 while (authcount > 0) {
1949 if (xdr_int_decode(&m, &authver) != 0)
1951 if (authver == RPCAUTH_UNIX)
1955 if (authunixok == 0)
1959 /* Set port number for NFS use. */
1960 error = krpc_portmap(mdsin, NFS_PROG,
1962 NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
1963 &mdsin->sin_port, procp);
1977 md_lookup_swap(struct sockaddr_in *mdsin, /* mountd server address */
1981 struct nfs_args *args,
1987 int attribs_present;
1994 m = m_get(M_WAIT,MT_DATA);
1998 if ((args->flags & NFSMNT_NFSV3) != 0) {
1999 *mtod(m, u_int32_t *) = txdr_unsigned(*fhsizep);
2000 bcopy(fhp, mtod(m, u_char *) + sizeof(u_int32_t), *fhsizep);
2001 m->m_len = *fhsizep + sizeof(u_int32_t);
2003 bcopy(fhp, mtod(m, u_char *), NFSX_V2FH);
2004 m->m_len = NFSX_V2FH;
2007 m->m_next = xdr_string_encode(path, strlen(path));
2008 if (m->m_next == NULL) {
2013 /* Do RPC to nfsd. */
2014 if ((args->flags & NFSMNT_NFSV3) != 0)
2015 error = krpc_call(mdsin, NFS_PROG, NFS_VER3,
2016 NFSPROC_LOOKUP, &m, NULL, procp);
2018 error = krpc_call(mdsin, NFS_PROG, NFS_VER2,
2019 NFSV2PROC_LOOKUP, &m, NULL, procp);
2021 return error; /* message already freed */
2023 if (xdr_int_decode(&m, &status) != 0)
2030 if ((args->flags & NFSMNT_NFSV3) != 0) {
2031 if (xdr_int_decode(&m, fhsizep) != 0 ||
2032 *fhsizep > NFSX_V3FHMAX ||
2036 *fhsizep = NFSX_V2FH;
2038 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
2041 if ((args->flags & NFSMNT_NFSV3) != 0) {
2042 if (xdr_int_decode(&m, &attribs_present) != 0)
2044 if (attribs_present != 0) {
2045 if (xdr_opaque_decode(&m, (u_char *) &fattribs.v3,
2046 sizeof(u_int32_t) * 21) != 0)
2048 size = fxdr_unsigned(u_int32_t, fattribs.v3[6]);
2051 if (xdr_opaque_decode(&m,(u_char *) &fattribs.v2,
2052 sizeof(u_int32_t) * 17) != 0)
2054 size = fxdr_unsigned(u_int32_t, fattribs.v2[5]);
2057 if (nfsv3_diskless.swap_nblks == 0 && size != -1) {
2058 nfsv3_diskless.swap_nblks = size / 1024;
2059 printf("md_lookup_swap: Swap size is %d KB\n",
2060 nfsv3_diskless.swap_nblks);