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.6 2004/01/06 03:21:18 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,
301 printf("(sockaddr *) <null>");
304 switch (sa->sa_family) {
307 struct sockaddr_in *sin;
309 sin = (struct sockaddr_in *) sa;
313 sin = (struct sockaddr_in *) ma;
321 struct sockaddr_dl *sli;
324 sli = (struct sockaddr_dl *) sa;
325 printf("link %.*s ", sli->sdl_nlen, sli->sdl_data);
326 for (i = 0; i < sli->sdl_alen; i++) {
329 printf("%x", ((unsigned char *) LLADDR(sli))[i]);
334 printf("af%d", sa->sa_family);
340 bootpboot_p_ma(struct sockaddr *ma)
346 printf("%x", *(int *)ma);
351 bootpboot_p_rtentry(struct rtentry *rt)
353 bootpboot_p_sa(rt_key(rt), rt_mask(rt));
355 bootpboot_p_ma(rt->rt_genmask);
357 bootpboot_p_sa(rt->rt_gateway, NULL);
359 printf("flags %x", (unsigned short) rt->rt_flags);
360 printf(" %d", (int) rt->rt_rmx.rmx_expire);
361 printf(" %s\n", if_name(rt->rt_ifp));
366 bootpboot_p_tree(struct radix_node *rn)
369 if (rn->rn_bit < 0) {
370 if ((rn->rn_flags & RNF_ROOT) != 0) {
372 bootpboot_p_rtentry((struct rtentry *) rn);
374 rn = rn->rn_dupedkey;
376 bootpboot_p_tree(rn->rn_left);
377 bootpboot_p_tree(rn->rn_right);
385 bootpboot_p_rtlist(void)
387 printf("Routing table:\n");
388 bootpboot_p_tree(rt_tables[AF_INET]->rnh_treetop);
393 bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
395 printf("%s flags %x, addr ",
397 (unsigned short) ifp->if_flags);
398 print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
399 printf(", broadcast ");
400 print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
401 printf(", netmask ");
402 print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
408 bootpboot_p_iflist(void)
413 printf("Interface list:\n");
414 for (ifp = TAILQ_FIRST(&ifnet);
416 ifp = TAILQ_NEXT(ifp, if_link)) {
417 for (ifa = TAILQ_FIRST(&ifp->if_addrhead);
419 ifa = TAILQ_NEXT(ifa, ifa_link))
420 if (ifa->ifa_addr->sa_family == AF_INET)
421 bootpboot_p_if(ifp, ifa);
424 #endif /* defined(BOOTP_DEBUG) */
428 clear_sinaddr(struct sockaddr_in *sin)
430 bzero(sin, sizeof(*sin));
431 sin->sin_len = sizeof(*sin);
432 sin->sin_family = AF_INET;
433 sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
438 static struct bootpc_ifcontext *
439 allocifctx(struct bootpc_globalcontext *gctx)
441 struct bootpc_ifcontext *ifctx;
442 ifctx = (struct bootpc_ifcontext *) malloc(sizeof(*ifctx),
445 panic("Failed to allocate bootp interface context structure");
447 bzero(ifctx, sizeof(*ifctx));
448 ifctx->xid = gctx->xid;
450 ifctx->state = IF_BOOTP_UNRESOLVED;
452 ifctx->state = IF_DHCP_UNRESOLVED;
460 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
462 if (ifctx->state == IF_BOOTP_RESOLVED ||
463 ifctx->state == IF_DHCP_RESOLVED)
470 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
472 if (ifctx->state == IF_BOOTP_UNRESOLVED ||
473 ifctx->state == IF_DHCP_UNRESOLVED)
480 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
482 if (ifctx->state == IF_BOOTP_FAILED ||
483 ifctx->state == IF_DHCP_FAILED)
490 bootpc_received(struct bootpc_globalcontext *gctx,
491 struct bootpc_ifcontext *ifctx)
493 unsigned char dhcpreplytype;
496 * Need timeout for fallback to less
497 * desirable alternative.
501 /* This call used for the side effect (badopt flag) */
502 (void) bootpc_tag(&gctx->tmptag, &gctx->reply,
506 /* If packet is invalid, ignore it */
507 if (gctx->tmptag.badopt != 0)
510 p = bootpc_tag(&gctx->tmptag, &gctx->reply,
511 gctx->replylen, TAG_DHCP_MSGTYPE);
515 dhcpreplytype = DHCP_NOMSG;
517 switch (ifctx->dhcpquerytype) {
519 if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */
520 #ifndef BOOTP_FORCE_DHCP
521 && dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */
527 if (dhcpreplytype != DHCP_ACK)
533 /* Ignore packet unless it gives us a root tag we didn't have */
535 if ((ifctx->state == IF_BOOTP_RESOLVED ||
536 (ifctx->dhcpquerytype == DHCP_DISCOVER &&
537 (ifctx->state == IF_DHCP_OFFERED ||
538 ifctx->state == IF_DHCP_RESOLVED))) &&
539 (bootpc_tag(&gctx->tmptag, &ifctx->reply,
542 bootpc_tag(&gctx->tmptag, &gctx->reply,
550 ifctx->replylen = gctx->replylen;
552 /* XXX: Only reset if 'perfect' response */
553 if (ifctx->state == IF_BOOTP_UNRESOLVED)
554 ifctx->state = IF_BOOTP_RESOLVED;
555 else if (ifctx->state == IF_DHCP_UNRESOLVED &&
556 ifctx->dhcpquerytype == DHCP_DISCOVER) {
557 if (dhcpreplytype == DHCP_OFFER)
558 ifctx->state = IF_DHCP_OFFERED;
560 ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */
561 } else if (ifctx->state == IF_DHCP_OFFERED &&
562 ifctx->dhcpquerytype == DHCP_REQUEST)
563 ifctx->state = IF_DHCP_RESOLVED;
566 if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
567 ifctx->state != IF_BOOTP_RESOLVED) {
568 p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
569 ifctx->replylen, TAG_DHCP_SERVERID);
570 if (p != NULL && gctx->tmptag.taglen == 4) {
571 memcpy(&ifctx->dhcpserver, p, 4);
572 ifctx->gotdhcpserver = 1;
574 ifctx->gotdhcpserver = 0;
578 ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
581 ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
583 TAG_ROUTERS) != NULL);
584 ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
586 TAG_SUBNETMASK) != NULL);
591 bootpc_call(struct bootpc_globalcontext *gctx,
595 struct sockaddr_in *sin, dst;
599 int error, on, rcvflg, timo, len;
603 struct bootpc_ifcontext *ifctx;
610 * Create socket and set its recieve timeout.
612 error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td);
618 bzero(&sopt, sizeof(sopt));
619 sopt.sopt_level = SOL_SOCKET;
620 sopt.sopt_name = SO_RCVTIMEO;
622 sopt.sopt_valsize = sizeof tv;
624 error = sosetopt(so, &sopt);
632 sopt.sopt_name = SO_BROADCAST;
634 sopt.sopt_valsize = sizeof on;
636 error = sosetopt(so, &sopt);
645 sopt.sopt_name = SO_DONTROUTE;
647 sopt.sopt_valsize = sizeof on;
649 error = sosetopt(so, &sopt);
654 * Bind the local endpoint to a bootp client port.
658 sin->sin_port = htons(IPPORT_BOOTPC);
659 error = sobind(so, (struct sockaddr *)sin, td);
661 printf("bind failed\n");
666 * Setup socket address for the server.
670 sin->sin_addr.s_addr = INADDR_BROADCAST;
671 sin->sin_port = htons(IPPORT_BOOTPS);
674 * Send it, repeatedly, until a reply is received,
675 * but delay each re-send by an increasing amount.
676 * If the delay hits the maximum, start complaining.
685 for (ifctx = gctx->interfaces;
687 ifctx = ifctx->next) {
688 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
689 bootpc_tag(&gctx->tmptag, &ifctx->reply,
695 for (ifctx = gctx->interfaces;
697 ifctx = ifctx->next) {
698 ifctx->outstanding = 0;
699 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
703 if (bootpc_ifctx_isfailed(ifctx) != 0)
707 ifctx->outstanding = 1;
709 /* Proceed to next step in DHCP negotiation */
710 if ((ifctx->state == IF_DHCP_OFFERED &&
711 ifctx->dhcpquerytype != DHCP_REQUEST) ||
712 (ifctx->state == IF_DHCP_UNRESOLVED &&
713 ifctx->dhcpquerytype != DHCP_DISCOVER) ||
714 (ifctx->state == IF_BOOTP_UNRESOLVED &&
715 ifctx->dhcpquerytype != DHCP_NOMSG)) {
717 bootpc_compose_query(ifctx, gctx, td);
720 /* Send BOOTP request (or re-send). */
722 if (ifctx->sentmsg == 0) {
723 switch(ifctx->dhcpquerytype) {
735 printf("Sending %s packet from "
736 "interface %s (%*D)\n",
738 ifctx->ireq.ifr_name,
739 ifctx->sdl->sdl_alen,
740 (unsigned char *) LLADDR(ifctx->sdl),
745 aio.iov_base = (caddr_t) &ifctx->call;
746 aio.iov_len = sizeof(ifctx->call);
750 auio.uio_segflg = UIO_SYSSPACE;
751 auio.uio_rw = UIO_WRITE;
753 auio.uio_resid = sizeof(ifctx->call);
756 /* Set netmask to 0.0.0.0 */
758 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
760 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
761 (caddr_t) &ifctx->ireq, td);
764 "set if netmask, error=%d",
767 error = sosend(so, (struct sockaddr *) &dst,
768 &auio, NULL, NULL, 0, td);
770 printf("bootpc_call: sosend: %d state %08x\n",
771 error, (int) so->so_state);
774 /* XXX: Is this needed ? */
775 tsleep(&error, 0, "bootpw", 10);
777 /* Set netmask to 255.0.0.0 */
779 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
781 sin->sin_addr.s_addr = htonl(0xff000000u);
782 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
783 (caddr_t) &ifctx->ireq, td);
786 "set if netmask, error=%d",
791 if (outstanding == 0 &&
792 (rtimo == 0 || time_second >= rtimo)) {
797 /* Determine new timeout. */
798 if (timo < MAX_RESEND_DELAY)
801 printf("DHCP/BOOTP timeout for server ");
802 print_sin_addr(&dst);
807 * Wait for up to timo seconds for a reply.
808 * The socket receive timeout was set to 1 second.
810 atimo = timo + time_second;
811 while (time_second < atimo) {
812 aio.iov_base = (caddr_t) &gctx->reply;
813 aio.iov_len = sizeof(gctx->reply);
817 auio.uio_segflg = UIO_SYSSPACE;
818 auio.uio_rw = UIO_READ;
820 auio.uio_resid = sizeof(gctx->reply);
824 error = soreceive(so, NULL, &auio,
825 NULL, NULL, &rcvflg);
826 gctx->secs = time_second - gctx->starttime;
827 for (ifctx = gctx->interfaces;
829 ifctx = ifctx->next) {
830 if (bootpc_ifctx_isresolved(ifctx) != 0 ||
831 bootpc_ifctx_isfailed(ifctx) != 0)
834 ifctx->call.secs = htons(gctx->secs);
836 if (error == EWOULDBLOCK)
840 len = sizeof(gctx->reply) - auio.uio_resid;
842 /* Do we have the required number of bytes ? */
843 if (len < BOOTP_MIN_LEN)
845 gctx->replylen = len;
848 if (gctx->reply.op != BOOTP_REPLY)
851 /* Is this an answer to our query */
852 for (ifctx = gctx->interfaces;
854 ifctx = ifctx->next) {
855 if (gctx->reply.xid != ifctx->call.xid)
858 /* Same HW address size ? */
859 if (gctx->reply.hlen != ifctx->call.hlen)
862 /* Correct HW address ? */
863 if (bcmp(gctx->reply.chaddr,
865 ifctx->call.hlen) != 0)
872 s = bootpc_tag(&gctx->tmptag,
885 s = "DHCP (unexpected)";
891 printf("Received %s packet"
894 ifctx->ireq.ifr_name);
895 print_in_addr(gctx->reply.siaddr);
896 if (gctx->reply.giaddr.s_addr !=
899 print_in_addr(gctx->reply.giaddr);
901 if (bootpc_received(gctx, ifctx) != 0) {
902 printf(" (accepted)");
903 if (ifctx->outstanding) {
904 ifctx->outstanding = 0;
907 /* Network settle delay */
908 if (outstanding == 0)
909 atimo = time_second +
912 printf(" (ignored)");
913 if (ifctx->gotrootpath) {
915 rtimo = time_second +
917 printf(" (got root path)");
919 printf(" (no root path)");
924 if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
927 /* Force a retry if halfway in DHCP negotiation */
929 for (ifctx = gctx->interfaces; ifctx != NULL;
930 ifctx = ifctx->next) {
931 if (ifctx->state == IF_DHCP_OFFERED) {
932 if (ifctx->dhcpquerytype == DHCP_DISCOVER)
935 ifctx->state = IF_DHCP_UNRESOLVED;
942 if (gotrootpath != 0) {
943 gctx->gotrootpath = gotrootpath;
944 if (rtimo != 0 && time_second >= rtimo)
947 } /* forever send/receive */
950 * XXX: These are errors of varying seriousness being silently
954 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
955 if (bootpc_ifctx_isresolved(ifctx) == 0) {
956 printf("%s timeout for interface %s\n",
957 ifctx->dhcpquerytype != DHCP_NOMSG ?
959 ifctx->ireq.ifr_name);
962 if (gctx->gotrootpath != 0) {
964 printf("Got a root path, ignoring remaining timeout\n");
969 #ifndef BOOTP_NFSROOT
970 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
971 if (bootpc_ifctx_isresolved(ifctx) != 0) {
988 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
989 struct bootpc_globalcontext *gctx,
992 struct sockaddr_in *sin;
998 struct sockaddr_dl *sdl;
1000 error = socreate(AF_INET, &ifctx->so, SOCK_DGRAM, 0, td);
1002 panic("nfs_boot: socreate, error=%d", error);
1004 ireq = &ifctx->ireq;
1008 * Bring up the interface.
1010 * Get the old interface flags and or IFF_UP into them; if
1011 * IFF_UP set blindly, interface selection can be clobbered.
1013 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, td);
1015 panic("bootpc_fakeup_interface: GIFFLAGS, error=%d", error);
1016 ireq->ifr_flags |= IFF_UP;
1017 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, td);
1019 panic("bootpc_fakeup_interface: SIFFLAGS, error=%d", error);
1022 * Do enough of ifconfig(8) so that the chosen interface
1023 * can talk to the servers. (just set the address)
1026 /* addr is 0.0.0.0 */
1028 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1030 error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, td);
1031 if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1032 panic("bootpc_fakeup_interface: "
1033 "set if addr, error=%d", error);
1035 /* netmask is 255.0.0.0 */
1037 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1039 sin->sin_addr.s_addr = htonl(0xff000000u);
1040 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t)ireq, td);
1042 panic("bootpc_fakeup_interface: set if netmask, error=%d",
1045 /* Broadcast is 255.255.255.255 */
1047 sin = (struct sockaddr_in *)&ireq->ifr_addr;
1049 clear_sinaddr(&ifctx->broadcast);
1050 sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
1051 ifctx->broadcast.sin_addr.s_addr = sin->sin_addr.s_addr;
1053 error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t)ireq, td);
1054 if (error != 0 && error != EADDRNOTAVAIL)
1055 panic("bootpc_fakeup_interface: "
1056 "set if broadcast addr, error=%d",
1060 /* Get HW address */
1063 for (ifa = TAILQ_FIRST(&ifctx->ifp->if_addrhead);
1065 ifa = TAILQ_NEXT(ifa,ifa_link))
1066 if (ifa->ifa_addr->sa_family == AF_LINK &&
1067 (sdl = ((struct sockaddr_dl *) ifa->ifa_addr)) != NULL &&
1068 sdl->sdl_type == IFT_ETHER)
1072 panic("bootpc: Unable to find HW address for %s",
1073 ifctx->ireq.ifr_name);
1081 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
1082 struct bootpc_globalcontext *gctx,
1086 struct sockaddr_in defdst;
1087 struct sockaddr_in defmask;
1088 struct sockaddr_in *sin;
1092 struct sockaddr_in *myaddr;
1093 struct sockaddr_in *netmask;
1094 struct sockaddr_in *gw;
1096 ireq = &ifctx->ireq;
1098 myaddr = &ifctx->myaddr;
1099 netmask = &ifctx->netmask;
1102 if (bootpc_ifctx_isresolved(ifctx) == 0) {
1104 /* Shutdown interfaces where BOOTP failed */
1106 printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
1107 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, td);
1109 panic("bootpc_adjust_interface: "
1110 "SIOCGIFFLAGS, error=%d", error);
1111 ireq->ifr_flags &= ~IFF_UP;
1112 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, td);
1114 panic("bootpc_adjust_interface: "
1115 "SIOCSIFFLAGS, error=%d", error);
1117 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1119 error = ifioctl(so, SIOCDIFADDR, (caddr_t) ireq, td);
1120 if (error != 0 && (error != EADDRNOTAVAIL ||
1121 ifctx == gctx->interfaces))
1122 panic("bootpc_adjust_interface: "
1123 "SIOCDIFADDR, error=%d", error);
1128 printf("Adjusted interface %s\n", ifctx->ireq.ifr_name);
1130 * Do enough of ifconfig(8) so that the chosen interface
1131 * can talk to the servers. (just set the address)
1133 bcopy(netmask, &ireq->ifr_addr, sizeof(*netmask));
1134 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t) ireq, td);
1136 panic("bootpc_adjust_interface: "
1137 "set if netmask, error=%d", error);
1139 /* Broadcast is with host part of IP address all 1's */
1141 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1143 sin->sin_addr.s_addr = myaddr->sin_addr.s_addr |
1144 ~ netmask->sin_addr.s_addr;
1145 error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t) ireq, td);
1147 panic("bootpc_adjust_interface: "
1148 "set if broadcast addr, error=%d", error);
1150 bcopy(myaddr, &ireq->ifr_addr, sizeof(*myaddr));
1151 error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, td);
1152 if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1153 panic("bootpc_adjust_interface: "
1154 "set if addr, error=%d", error);
1156 /* Add new default route */
1158 if (ifctx->gotgw != 0 || gctx->gotgw == 0) {
1159 clear_sinaddr(&defdst);
1160 clear_sinaddr(&defmask);
1161 error = rtrequest(RTM_ADD,
1162 (struct sockaddr *) &defdst,
1163 (struct sockaddr *) gw,
1164 (struct sockaddr *) &defmask,
1165 (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
1167 printf("bootpc_adjust_interface: "
1168 "add net route, error=%d\n", error);
1178 setfs(struct sockaddr_in *addr, char *path, char *p)
1184 if (((val = getdec(&p)) < 0) || (val > 255))
1190 if (((val = getdec(&p)) < 0) || (val > 255))
1196 if (((val = getdec(&p)) < 0) || (val > 255))
1202 if (((val = getdec(&p)) < 0) || (val > 255))
1209 addr->sin_addr.s_addr = htonl(ip);
1210 addr->sin_len = sizeof(struct sockaddr_in);
1211 addr->sin_family = AF_INET;
1213 strncpy(path, p, MNAMELEN - 1);
1226 if ((*p < '0') || (*p > '9'))
1228 while ((*p >= '0') && (*p <= '9')) {
1229 ret = ret * 10 + (*p - '0');
1238 substr(char *a, char *b)
1243 while (*a != '\0') {
1246 while (*loc1 == *loc2++) {
1260 mountopts(struct nfs_args *args, char *p)
1264 args->version = NFS_ARGSVERSION;
1267 args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
1268 args->sotype = SOCK_DGRAM;
1271 if ((tmp = (char *)substr(p, "rsize=")))
1272 args->rsize = getdec(&tmp);
1273 if ((tmp = (char *)substr(p, "wsize=")))
1274 args->wsize = getdec(&tmp);
1275 if ((tmp = (char *)substr(p, "intr")))
1276 args->flags |= NFSMNT_INT;
1277 if ((tmp = (char *)substr(p, "soft")))
1278 args->flags |= NFSMNT_SOFT;
1279 if ((tmp = (char *)substr(p, "noconn")))
1280 args->flags |= NFSMNT_NOCONN;
1281 if ((tmp = (char *)substr(p, "tcp")))
1282 args->sotype = SOCK_STREAM;
1287 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
1293 alignedlen = ( len + 3 ) & ~3;
1295 if (m->m_len < alignedlen) {
1296 m = m_pullup(m, alignedlen);
1302 bcopy(mtod(m, u_char *), buf, len);
1303 m_adj(m, alignedlen);
1310 xdr_int_decode(struct mbuf **mptr, int *iptr)
1313 if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
1315 *iptr = fxdr_unsigned(u_int32_t, i);
1321 print_sin_addr(struct sockaddr_in *sin)
1323 print_in_addr(sin->sin_addr);
1328 print_in_addr(struct in_addr addr)
1332 ip = ntohl(addr.s_addr);
1333 printf("%d.%d.%d.%d",
1334 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1338 bootpc_compose_query(ifctx, gctx, td)
1339 struct bootpc_ifcontext *ifctx;
1340 struct bootpc_globalcontext *gctx;
1343 unsigned char *vendp;
1344 unsigned char vendor_client[64];
1346 uint8_t vendor_client_len;
1348 ifctx->gotrootpath = 0;
1350 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1353 ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */
1354 ifctx->call.htype = 1; /* 10mb ethernet */
1355 ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1356 ifctx->call.hops = 0;
1357 if (bootpc_ifctx_isunresolved(ifctx) != 0)
1359 ifctx->call.xid = txdr_unsigned(ifctx->xid);
1360 bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1362 vendp = ifctx->call.vend;
1363 *vendp++ = 99; /* RFC1048 cookie */
1367 *vendp++ = TAG_MAXMSGSIZE;
1369 *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1370 *vendp++ = sizeof(struct bootp_packet) & 255;
1372 snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1373 ostype, MACHINE, osrelease);
1374 vendor_client_len = strlen(vendor_client);
1375 *vendp++ = TAG_VENDOR_INDENTIFIER;
1376 *vendp++ = vendor_client_len;
1377 memcpy(vendp, vendor_client, vendor_client_len);
1378 vendp += vendor_client_len;;
1379 ifctx->dhcpquerytype = DHCP_NOMSG;
1380 switch (ifctx->state) {
1381 case IF_DHCP_UNRESOLVED:
1382 *vendp++ = TAG_DHCP_MSGTYPE;
1384 *vendp++ = DHCP_DISCOVER;
1385 ifctx->dhcpquerytype = DHCP_DISCOVER;
1386 ifctx->gotdhcpserver = 0;
1388 case IF_DHCP_OFFERED:
1389 *vendp++ = TAG_DHCP_MSGTYPE;
1391 *vendp++ = DHCP_REQUEST;
1392 ifctx->dhcpquerytype = DHCP_REQUEST;
1393 *vendp++ = TAG_DHCP_REQ_ADDR;
1395 memcpy(vendp, &ifctx->reply.yiaddr, 4);
1397 if (ifctx->gotdhcpserver != 0) {
1398 *vendp++ = TAG_DHCP_SERVERID;
1400 memcpy(vendp, &ifctx->dhcpserver, 4);
1403 *vendp++ = TAG_DHCP_LEASETIME;
1405 leasetime = htonl(300);
1406 memcpy(vendp, &leasetime, 4);
1413 ifctx->call.secs = 0;
1414 ifctx->call.flags = htons(0x8000); /* We need an broadcast answer */
1419 bootpc_hascookie(struct bootp_packet *bp)
1421 return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1422 bp->vend[2] == 83 && bp->vend[3] == 99);
1427 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1428 unsigned char *start,
1436 if (tctx->badtag != 0 || tctx->badopt != 0)
1444 if (code == TAG_PAD)
1446 if (code == TAG_END)
1448 if (j >= ej || j + *j + 1 > ej) {
1454 if (tctx->taglen + len > TAG_MAXLEN) {
1460 memcpy(tctx->buf + tctx->taglen,
1462 tctx->taglen += len;
1464 if (code == TAG_OVERLOAD)
1465 tctx->overload = *j;
1472 static unsigned char *
1473 bootpc_tag(struct bootpc_tagcontext *tctx,
1474 struct bootp_packet *bp,
1487 if (bootpc_hascookie(bp) == 0)
1491 ej = (unsigned char *) bp + len;
1493 bootpc_tag_helper(tctx, &bp->vend[4],
1494 (unsigned char *) bp + len - &bp->vend[4], tag);
1496 if ((tctx->overload & OVERLOAD_FILE) != 0)
1497 bootpc_tag_helper(tctx,
1498 (unsigned char *) bp->file,
1501 if ((tctx->overload & OVERLOAD_SNAME) != 0)
1502 bootpc_tag_helper(tctx,
1503 (unsigned char *) bp->sname,
1507 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1509 tctx->buf[tctx->taglen] = '\0';
1515 bootpc_decode_reply(nd, ifctx, gctx)
1516 struct nfsv3_diskless *nd;
1517 struct bootpc_ifcontext *ifctx;
1518 struct bootpc_globalcontext *gctx;
1524 ifctx->gotnetmask = 0;
1526 clear_sinaddr(&ifctx->myaddr);
1527 clear_sinaddr(&ifctx->netmask);
1528 clear_sinaddr(&ifctx->gw);
1530 ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1532 ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1533 snprintf(gctx->lookup_path, sizeof(gctx->lookup_path),
1535 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1537 printf("%s at ", ifctx->ireq.ifr_name);
1538 print_sin_addr(&ifctx->myaddr);
1540 print_in_addr(ifctx->reply.siaddr);
1542 ifctx->gw.sin_addr = ifctx->reply.giaddr;
1543 if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1544 printf(" via gateway ");
1545 print_in_addr(ifctx->reply.giaddr);
1548 /* This call used for the side effect (overload flag) */
1549 (void) bootpc_tag(&gctx->tmptag,
1550 &ifctx->reply, ifctx->replylen, TAG_END);
1552 if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1553 if (ifctx->reply.sname[0] != '\0')
1554 printf(" server name %s", ifctx->reply.sname);
1555 if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1556 if (ifctx->reply.file[0] != '\0')
1557 printf(" boot file %s", ifctx->reply.file);
1561 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1564 if (gctx->tag.taglen != 4)
1565 panic("bootpc: subnet mask len is %d",
1567 bcopy(p, &ifctx->netmask.sin_addr, 4);
1568 ifctx->gotnetmask = 1;
1569 printf("subnet mask ");
1570 print_sin_addr(&ifctx->netmask);
1574 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1578 if (gctx->tag.taglen % 4)
1579 panic("bootpc: Router Len is %d", gctx->tag.taglen);
1580 if (gctx->tag.taglen > 0) {
1581 bcopy(p, &ifctx->gw.sin_addr, 4);
1583 print_sin_addr(&ifctx->gw);
1590 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1593 if (gctx->setrootfs != NULL) {
1594 printf("rootfs %s (ignored) ", p);
1595 } else if (setfs(&nd->root_saddr,
1596 nd->root_hostnam, p)) {
1597 printf("rootfs %s ",p);
1598 gctx->gotrootpath = 1;
1599 ifctx->gotrootpath = 1;
1600 gctx->setrootfs = ifctx;
1602 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1606 mountopts(&nd->root_args, p);
1607 printf("rootopts %s ", p);
1610 panic("Failed to set rootfs to %s",p);
1613 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1616 if (gctx->setswapfs != NULL) {
1617 printf("swapfs %s (ignored) ", p);
1618 } else if (setfs(&nd->swap_saddr,
1619 nd->swap_hostnam, p)) {
1620 gctx->gotswappath = 1;
1621 gctx->setswapfs = ifctx;
1622 printf("swapfs %s ", p);
1624 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1628 /* swap mount options */
1629 mountopts(&nd->swap_args, p);
1630 printf("swapopts %s ", p);
1633 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1638 if (gctx->tag.taglen != 4)
1640 "Expected 4 bytes for swaplen, "
1643 bcopy(p, &swaplen, 4);
1644 nd->swap_nblks = ntohl(swaplen);
1645 printf("swapsize %d KB ",
1649 panic("Failed to set swapfs to %s", p);
1652 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1655 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1656 panic("bootpc: hostname >= %d bytes",
1658 if (gctx->sethostname != NULL) {
1659 printf("hostname %s (ignored) ", p);
1661 strcpy(nd->my_hostnam, p);
1662 strcpy(hostname, p);
1663 printf("hostname %s ",hostname);
1664 gctx->sethostname = ifctx;
1667 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1669 if (p != NULL) { /* store in a sysctl variable */
1670 int i, l = sizeof(bootp_cookie) - 1;
1671 for (i = 0; i < l && p[i] != '\0'; i++)
1672 bootp_cookie[i] = p[i];
1678 if (ifctx->gotnetmask == 0) {
1679 if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1680 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1681 else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1682 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1684 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1686 if (ifctx->gotgw == 0) {
1688 ifctx->gw.sin_addr.s_addr = ifctx->myaddr.sin_addr.s_addr;
1695 struct bootpc_ifcontext *ifctx, *nctx; /* Interface BOOTP contexts */
1696 struct bootpc_globalcontext *gctx; /* Global BOOTP context */
1699 struct nfsv3_diskless *nd;
1702 nd = &nfsv3_diskless;
1706 * If already filled in, don't touch it here
1708 if (nfs_diskless_valid != 0)
1712 * Wait until arp entries can be handled.
1714 while (time_second == 0)
1715 tsleep(&time_second, 0, "arpkludge", 10);
1717 gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK);
1719 panic("Failed to allocate bootp global context structure");
1721 bzero(gctx, sizeof(*gctx));
1722 gctx->xid = ~0xFFFF;
1723 gctx->starttime = time_second;
1725 ifctx = allocifctx(gctx);
1728 * Find a network interface.
1730 #ifdef BOOTP_WIRED_TO
1731 printf("bootpc_init: wired to interface '%s'\n",
1732 __XSTRING(BOOTP_WIRED_TO));
1734 bzero(&ifctx->ireq, sizeof(ifctx->ireq));
1735 for (ifp = TAILQ_FIRST(&ifnet);
1737 ifp = TAILQ_NEXT(ifp, if_link)) {
1738 strlcpy(ifctx->ireq.ifr_name, ifp->if_xname,
1739 sizeof(ifctx->ireq.ifr_name));
1740 #ifdef BOOTP_WIRED_TO
1741 if (strcmp(ifctx->ireq.ifr_name,
1742 __XSTRING(BOOTP_WIRED_TO)) != 0)
1745 if ((ifp->if_flags &
1746 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1750 if (gctx->interfaces != NULL)
1751 gctx->lastinterface->next = ifctx;
1753 gctx->interfaces = ifctx;
1755 gctx->lastinterface = ifctx;
1756 ifctx = allocifctx(gctx);
1758 free(ifctx, M_TEMP);
1760 if (gctx->interfaces == NULL) {
1761 #ifdef BOOTP_WIRED_TO
1762 panic("bootpc_init: Could not find interface specified "
1763 "by BOOTP_WIRED_TO: "
1764 __XSTRING(BOOTP_WIRED_TO));
1766 panic("bootpc_init: no suitable interface");
1770 gctx->gotrootpath = 0;
1771 gctx->gotswappath = 0;
1774 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1775 bootpc_fakeup_interface(ifctx, gctx, td);
1777 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1778 bootpc_compose_query(ifctx, gctx, td);
1780 ifctx = gctx->interfaces;
1781 error = bootpc_call(gctx, td);
1784 #ifdef BOOTP_NFSROOT
1785 panic("BOOTP call failed");
1787 printf("BOOTP call failed\n");
1791 mountopts(&nd->root_args, NULL);
1793 mountopts(&nd->swap_args, NULL);
1795 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1796 if (bootpc_ifctx_isresolved(ifctx) != 0)
1797 bootpc_decode_reply(nd, ifctx, gctx);
1799 if (gctx->gotswappath == 0)
1801 #ifdef BOOTP_NFSROOT
1802 if (gctx->gotrootpath == 0)
1803 panic("bootpc: No root path offered");
1806 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
1807 bootpc_adjust_interface(ifctx, gctx, td);
1812 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1813 if (ifctx->gotrootpath != 0)
1815 if (ifctx == NULL) {
1816 for (ifctx = gctx->interfaces;
1818 ifctx = ifctx->next)
1819 if (bootpc_ifctx_isresolved(ifctx) != 0)
1825 if (gctx->gotrootpath != 0) {
1827 error = md_mount(&nd->root_saddr, nd->root_hostnam,
1828 nd->root_fh, &nd->root_fhsize,
1829 &nd->root_args, td);
1831 panic("nfs_boot: mountd root, error=%d", error);
1833 if (gctx->gotswappath != 0) {
1835 error = md_mount(&nd->swap_saddr,
1837 nd->swap_fh, &nd->swap_fhsize,
1838 &nd->swap_args, td);
1840 panic("nfs_boot: mountd swap, error=%d",
1843 error = md_lookup_swap(&nd->swap_saddr,
1845 nd->swap_fh, &nd->swap_fhsize,
1846 &nd->swap_args, td);
1848 panic("nfs_boot: lookup swap, error=%d",
1851 nfs_diskless_valid = 3;
1854 strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1855 bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1856 bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1857 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1858 ifctx->myaddr.sin_addr.s_addr |
1859 ~ ifctx->netmask.sin_addr.s_addr;
1860 bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1863 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = nctx) {
1865 free(ifctx, M_TEMP);
1873 * Given a server pathname, get an NFS file handle.
1874 * Also, sets sin->sin_port to the NFS service port.
1877 md_mount(struct sockaddr_in *mdsin, /* mountd server address */
1881 struct nfs_args *args,
1891 /* First try NFS v3 */
1892 /* Get port number for MOUNTD. */
1893 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1894 &mdsin->sin_port, td);
1896 m = xdr_string_encode(path, strlen(path));
1898 /* Do RPC to mountd. */
1899 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1900 RPCMNT_MOUNT, &m, NULL, td);
1903 args->flags |= NFSMNT_NFSV3;
1906 /* Fallback to NFS v2 */
1908 /* Get port number for MOUNTD. */
1909 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1910 &mdsin->sin_port, td);
1914 m = xdr_string_encode(path, strlen(path));
1916 /* Do RPC to mountd. */
1917 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1918 RPCMNT_MOUNT, &m, NULL, td);
1920 return error; /* message already freed */
1926 if (xdr_int_decode(&m, &error) != 0 || error != 0)
1929 if ((args->flags & NFSMNT_NFSV3) != 0) {
1930 if (xdr_int_decode(&m, fhsizep) != 0 ||
1931 *fhsizep > NFSX_V3FHMAX ||
1935 *fhsizep = NFSX_V2FH;
1937 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
1940 if (args->flags & NFSMNT_NFSV3) {
1941 if (xdr_int_decode(&m, &authcount) != 0)
1944 if (authcount < 0 || authcount > 100)
1946 while (authcount > 0) {
1947 if (xdr_int_decode(&m, &authver) != 0)
1949 if (authver == RPCAUTH_UNIX)
1953 if (authunixok == 0)
1957 /* Set port number for NFS use. */
1958 error = krpc_portmap(mdsin, NFS_PROG,
1960 NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
1961 &mdsin->sin_port, td);
1975 md_lookup_swap(struct sockaddr_in *mdsin, /* mountd server address */
1979 struct nfs_args *args,
1985 int attribs_present;
1992 m = m_get(M_WAIT,MT_DATA);
1996 if ((args->flags & NFSMNT_NFSV3) != 0) {
1997 *mtod(m, u_int32_t *) = txdr_unsigned(*fhsizep);
1998 bcopy(fhp, mtod(m, u_char *) + sizeof(u_int32_t), *fhsizep);
1999 m->m_len = *fhsizep + sizeof(u_int32_t);
2001 bcopy(fhp, mtod(m, u_char *), NFSX_V2FH);
2002 m->m_len = NFSX_V2FH;
2005 m->m_next = xdr_string_encode(path, strlen(path));
2006 if (m->m_next == NULL) {
2011 /* Do RPC to nfsd. */
2012 if ((args->flags & NFSMNT_NFSV3) != 0)
2013 error = krpc_call(mdsin, NFS_PROG, NFS_VER3,
2014 NFSPROC_LOOKUP, &m, NULL, td);
2016 error = krpc_call(mdsin, NFS_PROG, NFS_VER2,
2017 NFSV2PROC_LOOKUP, &m, NULL, td);
2019 return error; /* message already freed */
2021 if (xdr_int_decode(&m, &status) != 0)
2028 if ((args->flags & NFSMNT_NFSV3) != 0) {
2029 if (xdr_int_decode(&m, fhsizep) != 0 ||
2030 *fhsizep > NFSX_V3FHMAX ||
2034 *fhsizep = NFSX_V2FH;
2036 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
2039 if ((args->flags & NFSMNT_NFSV3) != 0) {
2040 if (xdr_int_decode(&m, &attribs_present) != 0)
2042 if (attribs_present != 0) {
2043 if (xdr_opaque_decode(&m, (u_char *) &fattribs.v3,
2044 sizeof(u_int32_t) * 21) != 0)
2046 size = fxdr_unsigned(u_int32_t, fattribs.v3[6]);
2049 if (xdr_opaque_decode(&m,(u_char *) &fattribs.v2,
2050 sizeof(u_int32_t) * 17) != 0)
2052 size = fxdr_unsigned(u_int32_t, fattribs.v2[5]);
2055 if (nfsv3_diskless.swap_nblks == 0 && size != -1) {
2056 nfsv3_diskless.swap_nblks = size / 1024;
2057 printf("md_lookup_swap: Swap size is %d KB\n",
2058 nfsv3_diskless.swap_nblks);