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.8 2004/04/19 16:33:49 cpressey 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)
528 /* Ignore packet unless it gives us a root tag we didn't have */
530 if ((ifctx->state == IF_BOOTP_RESOLVED ||
531 (ifctx->dhcpquerytype == DHCP_DISCOVER &&
532 (ifctx->state == IF_DHCP_OFFERED ||
533 ifctx->state == IF_DHCP_RESOLVED))) &&
534 (bootpc_tag(&gctx->tmptag, &ifctx->reply,
537 bootpc_tag(&gctx->tmptag, &gctx->reply,
545 ifctx->replylen = gctx->replylen;
547 /* XXX: Only reset if 'perfect' response */
548 if (ifctx->state == IF_BOOTP_UNRESOLVED)
549 ifctx->state = IF_BOOTP_RESOLVED;
550 else if (ifctx->state == IF_DHCP_UNRESOLVED &&
551 ifctx->dhcpquerytype == DHCP_DISCOVER) {
552 if (dhcpreplytype == DHCP_OFFER)
553 ifctx->state = IF_DHCP_OFFERED;
555 ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */
556 } else if (ifctx->state == IF_DHCP_OFFERED &&
557 ifctx->dhcpquerytype == DHCP_REQUEST)
558 ifctx->state = IF_DHCP_RESOLVED;
561 if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
562 ifctx->state != IF_BOOTP_RESOLVED) {
563 p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
564 ifctx->replylen, TAG_DHCP_SERVERID);
565 if (p != NULL && gctx->tmptag.taglen == 4) {
566 memcpy(&ifctx->dhcpserver, p, 4);
567 ifctx->gotdhcpserver = 1;
569 ifctx->gotdhcpserver = 0;
573 ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
576 ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
578 TAG_ROUTERS) != NULL);
579 ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
581 TAG_SUBNETMASK) != NULL);
586 bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
589 struct sockaddr_in *sin, dst;
593 int error, on, rcvflg, timo, len;
597 struct bootpc_ifcontext *ifctx;
604 * Create socket and set its recieve timeout.
606 error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td);
612 bzero(&sopt, sizeof(sopt));
613 sopt.sopt_level = SOL_SOCKET;
614 sopt.sopt_name = SO_RCVTIMEO;
616 sopt.sopt_valsize = sizeof tv;
618 error = sosetopt(so, &sopt);
626 sopt.sopt_name = SO_BROADCAST;
628 sopt.sopt_valsize = sizeof on;
630 error = sosetopt(so, &sopt);
639 sopt.sopt_name = SO_DONTROUTE;
641 sopt.sopt_valsize = sizeof on;
643 error = sosetopt(so, &sopt);
648 * Bind the local endpoint to a bootp client port.
652 sin->sin_port = htons(IPPORT_BOOTPC);
653 error = sobind(so, (struct sockaddr *)sin, td);
655 printf("bind failed\n");
660 * Setup socket address for the server.
664 sin->sin_addr.s_addr = INADDR_BROADCAST;
665 sin->sin_port = htons(IPPORT_BOOTPS);
668 * Send it, repeatedly, until a reply is received,
669 * but delay each re-send by an increasing amount.
670 * If the delay hits the maximum, start complaining.
679 for (ifctx = gctx->interfaces;
681 ifctx = ifctx->next) {
682 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
683 bootpc_tag(&gctx->tmptag, &ifctx->reply,
689 for (ifctx = gctx->interfaces;
691 ifctx = ifctx->next) {
692 ifctx->outstanding = 0;
693 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
697 if (bootpc_ifctx_isfailed(ifctx) != 0)
701 ifctx->outstanding = 1;
703 /* Proceed to next step in DHCP negotiation */
704 if ((ifctx->state == IF_DHCP_OFFERED &&
705 ifctx->dhcpquerytype != DHCP_REQUEST) ||
706 (ifctx->state == IF_DHCP_UNRESOLVED &&
707 ifctx->dhcpquerytype != DHCP_DISCOVER) ||
708 (ifctx->state == IF_BOOTP_UNRESOLVED &&
709 ifctx->dhcpquerytype != DHCP_NOMSG)) {
711 bootpc_compose_query(ifctx, gctx, td);
714 /* Send BOOTP request (or re-send). */
716 if (ifctx->sentmsg == 0) {
717 switch(ifctx->dhcpquerytype) {
729 printf("Sending %s packet from "
730 "interface %s (%*D)\n",
732 ifctx->ireq.ifr_name,
733 ifctx->sdl->sdl_alen,
734 (unsigned char *) LLADDR(ifctx->sdl),
739 aio.iov_base = (caddr_t) &ifctx->call;
740 aio.iov_len = sizeof(ifctx->call);
744 auio.uio_segflg = UIO_SYSSPACE;
745 auio.uio_rw = UIO_WRITE;
747 auio.uio_resid = sizeof(ifctx->call);
750 /* Set netmask to 0.0.0.0 */
752 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
754 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
755 (caddr_t) &ifctx->ireq, td);
758 "set if netmask, error=%d",
761 error = sosend(so, (struct sockaddr *) &dst,
762 &auio, NULL, NULL, 0, td);
764 printf("bootpc_call: sosend: %d state %08x\n",
765 error, (int) so->so_state);
768 /* XXX: Is this needed ? */
769 tsleep(&error, 0, "bootpw", 10);
771 /* Set netmask to 255.0.0.0 */
773 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
775 sin->sin_addr.s_addr = htonl(0xff000000u);
776 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
777 (caddr_t) &ifctx->ireq, td);
780 "set if netmask, error=%d",
785 if (outstanding == 0 &&
786 (rtimo == 0 || time_second >= rtimo)) {
791 /* Determine new timeout. */
792 if (timo < MAX_RESEND_DELAY)
795 printf("DHCP/BOOTP timeout for server ");
796 print_sin_addr(&dst);
801 * Wait for up to timo seconds for a reply.
802 * The socket receive timeout was set to 1 second.
804 atimo = timo + time_second;
805 while (time_second < atimo) {
806 aio.iov_base = (caddr_t) &gctx->reply;
807 aio.iov_len = sizeof(gctx->reply);
811 auio.uio_segflg = UIO_SYSSPACE;
812 auio.uio_rw = UIO_READ;
814 auio.uio_resid = sizeof(gctx->reply);
818 error = soreceive(so, NULL, &auio,
819 NULL, NULL, &rcvflg);
820 gctx->secs = time_second - gctx->starttime;
821 for (ifctx = gctx->interfaces;
823 ifctx = ifctx->next) {
824 if (bootpc_ifctx_isresolved(ifctx) != 0 ||
825 bootpc_ifctx_isfailed(ifctx) != 0)
828 ifctx->call.secs = htons(gctx->secs);
830 if (error == EWOULDBLOCK)
834 len = sizeof(gctx->reply) - auio.uio_resid;
836 /* Do we have the required number of bytes ? */
837 if (len < BOOTP_MIN_LEN)
839 gctx->replylen = len;
842 if (gctx->reply.op != BOOTP_REPLY)
845 /* Is this an answer to our query */
846 for (ifctx = gctx->interfaces;
848 ifctx = ifctx->next) {
849 if (gctx->reply.xid != ifctx->call.xid)
852 /* Same HW address size ? */
853 if (gctx->reply.hlen != ifctx->call.hlen)
856 /* Correct HW address ? */
857 if (bcmp(gctx->reply.chaddr,
859 ifctx->call.hlen) != 0)
866 s = bootpc_tag(&gctx->tmptag,
879 s = "DHCP (unexpected)";
885 printf("Received %s packet"
888 ifctx->ireq.ifr_name);
889 print_in_addr(gctx->reply.siaddr);
890 if (gctx->reply.giaddr.s_addr !=
893 print_in_addr(gctx->reply.giaddr);
895 if (bootpc_received(gctx, ifctx) != 0) {
896 printf(" (accepted)");
897 if (ifctx->outstanding) {
898 ifctx->outstanding = 0;
901 /* Network settle delay */
902 if (outstanding == 0)
903 atimo = time_second +
906 printf(" (ignored)");
907 if (ifctx->gotrootpath) {
909 rtimo = time_second +
911 printf(" (got root path)");
913 printf(" (no root path)");
918 if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
921 /* Force a retry if halfway in DHCP negotiation */
923 for (ifctx = gctx->interfaces; ifctx != NULL;
924 ifctx = ifctx->next) {
925 if (ifctx->state == IF_DHCP_OFFERED) {
926 if (ifctx->dhcpquerytype == DHCP_DISCOVER)
929 ifctx->state = IF_DHCP_UNRESOLVED;
936 if (gotrootpath != 0) {
937 gctx->gotrootpath = gotrootpath;
938 if (rtimo != 0 && time_second >= rtimo)
941 } /* forever send/receive */
944 * XXX: These are errors of varying seriousness being silently
948 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
949 if (bootpc_ifctx_isresolved(ifctx) == 0) {
950 printf("%s timeout for interface %s\n",
951 ifctx->dhcpquerytype != DHCP_NOMSG ?
953 ifctx->ireq.ifr_name);
956 if (gctx->gotrootpath != 0) {
958 printf("Got a root path, ignoring remaining timeout\n");
963 #ifndef BOOTP_NFSROOT
964 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
965 if (bootpc_ifctx_isresolved(ifctx) != 0) {
982 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
983 struct bootpc_globalcontext *gctx,
986 struct sockaddr_in *sin;
992 struct sockaddr_dl *sdl;
994 error = socreate(AF_INET, &ifctx->so, SOCK_DGRAM, 0, td);
996 panic("nfs_boot: socreate, error=%d", error);
1002 * Bring up the interface.
1004 * Get the old interface flags and or IFF_UP into them; if
1005 * IFF_UP set blindly, interface selection can be clobbered.
1007 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, td);
1009 panic("bootpc_fakeup_interface: GIFFLAGS, error=%d", error);
1010 ireq->ifr_flags |= IFF_UP;
1011 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, td);
1013 panic("bootpc_fakeup_interface: SIFFLAGS, error=%d", error);
1016 * Do enough of ifconfig(8) so that the chosen interface
1017 * can talk to the servers. (just set the address)
1020 /* addr is 0.0.0.0 */
1022 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1024 error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, td);
1025 if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1026 panic("bootpc_fakeup_interface: "
1027 "set if addr, error=%d", error);
1029 /* netmask is 255.0.0.0 */
1031 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1033 sin->sin_addr.s_addr = htonl(0xff000000u);
1034 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t)ireq, td);
1036 panic("bootpc_fakeup_interface: set if netmask, error=%d",
1039 /* Broadcast is 255.255.255.255 */
1041 sin = (struct sockaddr_in *)&ireq->ifr_addr;
1043 clear_sinaddr(&ifctx->broadcast);
1044 sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
1045 ifctx->broadcast.sin_addr.s_addr = sin->sin_addr.s_addr;
1047 error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t)ireq, td);
1048 if (error != 0 && error != EADDRNOTAVAIL)
1049 panic("bootpc_fakeup_interface: "
1050 "set if broadcast addr, error=%d",
1054 /* Get HW address */
1057 TAILQ_FOREACH(ifa, &ifctx->ifp->if_addrhead, ifa_link)
1058 if (ifa->ifa_addr->sa_family == AF_LINK &&
1059 (sdl = ((struct sockaddr_dl *) ifa->ifa_addr)) != NULL &&
1060 sdl->sdl_type == IFT_ETHER)
1064 panic("bootpc: Unable to find HW address for %s",
1065 ifctx->ireq.ifr_name);
1073 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
1074 struct bootpc_globalcontext *gctx,
1078 struct sockaddr_in defdst;
1079 struct sockaddr_in defmask;
1080 struct sockaddr_in *sin;
1084 struct sockaddr_in *myaddr;
1085 struct sockaddr_in *netmask;
1086 struct sockaddr_in *gw;
1088 ireq = &ifctx->ireq;
1090 myaddr = &ifctx->myaddr;
1091 netmask = &ifctx->netmask;
1094 if (bootpc_ifctx_isresolved(ifctx) == 0) {
1096 /* Shutdown interfaces where BOOTP failed */
1098 printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
1099 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, td);
1101 panic("bootpc_adjust_interface: "
1102 "SIOCGIFFLAGS, error=%d", error);
1103 ireq->ifr_flags &= ~IFF_UP;
1104 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, td);
1106 panic("bootpc_adjust_interface: "
1107 "SIOCSIFFLAGS, error=%d", error);
1109 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1111 error = ifioctl(so, SIOCDIFADDR, (caddr_t) ireq, td);
1112 if (error != 0 && (error != EADDRNOTAVAIL ||
1113 ifctx == gctx->interfaces))
1114 panic("bootpc_adjust_interface: "
1115 "SIOCDIFADDR, error=%d", error);
1120 printf("Adjusted interface %s\n", ifctx->ireq.ifr_name);
1122 * Do enough of ifconfig(8) so that the chosen interface
1123 * can talk to the servers. (just set the address)
1125 bcopy(netmask, &ireq->ifr_addr, sizeof(*netmask));
1126 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t) ireq, td);
1128 panic("bootpc_adjust_interface: "
1129 "set if netmask, error=%d", error);
1131 /* Broadcast is with host part of IP address all 1's */
1133 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1135 sin->sin_addr.s_addr = myaddr->sin_addr.s_addr |
1136 ~ netmask->sin_addr.s_addr;
1137 error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t) ireq, td);
1139 panic("bootpc_adjust_interface: "
1140 "set if broadcast addr, error=%d", error);
1142 bcopy(myaddr, &ireq->ifr_addr, sizeof(*myaddr));
1143 error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, td);
1144 if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1145 panic("bootpc_adjust_interface: "
1146 "set if addr, error=%d", error);
1148 /* Add new default route */
1150 if (ifctx->gotgw != 0 || gctx->gotgw == 0) {
1151 clear_sinaddr(&defdst);
1152 clear_sinaddr(&defmask);
1153 error = rtrequest(RTM_ADD,
1154 (struct sockaddr *) &defdst,
1155 (struct sockaddr *) gw,
1156 (struct sockaddr *) &defmask,
1157 (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
1159 printf("bootpc_adjust_interface: "
1160 "add net route, error=%d\n", error);
1170 setfs(struct sockaddr_in *addr, char *path, char *p)
1176 if (((val = getdec(&p)) < 0) || (val > 255))
1182 if (((val = getdec(&p)) < 0) || (val > 255))
1188 if (((val = getdec(&p)) < 0) || (val > 255))
1194 if (((val = getdec(&p)) < 0) || (val > 255))
1201 addr->sin_addr.s_addr = htonl(ip);
1202 addr->sin_len = sizeof(struct sockaddr_in);
1203 addr->sin_family = AF_INET;
1205 strncpy(path, p, MNAMELEN - 1);
1218 if ((*p < '0') || (*p > '9'))
1220 while ((*p >= '0') && (*p <= '9')) {
1221 ret = ret * 10 + (*p - '0');
1230 substr(char *a, char *b)
1235 while (*a != '\0') {
1238 while (*loc1 == *loc2++) {
1252 mountopts(struct nfs_args *args, char *p)
1256 args->version = NFS_ARGSVERSION;
1259 args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
1260 args->sotype = SOCK_DGRAM;
1263 if ((tmp = (char *)substr(p, "rsize=")))
1264 args->rsize = getdec(&tmp);
1265 if ((tmp = (char *)substr(p, "wsize=")))
1266 args->wsize = getdec(&tmp);
1267 if ((tmp = (char *)substr(p, "intr")))
1268 args->flags |= NFSMNT_INT;
1269 if ((tmp = (char *)substr(p, "soft")))
1270 args->flags |= NFSMNT_SOFT;
1271 if ((tmp = (char *)substr(p, "noconn")))
1272 args->flags |= NFSMNT_NOCONN;
1273 if ((tmp = (char *)substr(p, "tcp")))
1274 args->sotype = SOCK_STREAM;
1279 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
1285 alignedlen = ( len + 3 ) & ~3;
1287 if (m->m_len < alignedlen) {
1288 m = m_pullup(m, alignedlen);
1294 bcopy(mtod(m, u_char *), buf, len);
1295 m_adj(m, alignedlen);
1302 xdr_int_decode(struct mbuf **mptr, int *iptr)
1305 if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
1307 *iptr = fxdr_unsigned(u_int32_t, i);
1313 print_sin_addr(struct sockaddr_in *sin)
1315 print_in_addr(sin->sin_addr);
1320 print_in_addr(struct in_addr addr)
1324 ip = ntohl(addr.s_addr);
1325 printf("%d.%d.%d.%d",
1326 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1330 bootpc_compose_query(struct bootpc_ifcontext *ifctx,
1331 struct bootpc_globalcontext *gctx, struct thread *td)
1333 unsigned char *vendp;
1334 unsigned char vendor_client[64];
1336 uint8_t vendor_client_len;
1338 ifctx->gotrootpath = 0;
1340 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1343 ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */
1344 ifctx->call.htype = 1; /* 10mb ethernet */
1345 ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1346 ifctx->call.hops = 0;
1347 if (bootpc_ifctx_isunresolved(ifctx) != 0)
1349 ifctx->call.xid = txdr_unsigned(ifctx->xid);
1350 bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1352 vendp = ifctx->call.vend;
1353 *vendp++ = 99; /* RFC1048 cookie */
1357 *vendp++ = TAG_MAXMSGSIZE;
1359 *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1360 *vendp++ = sizeof(struct bootp_packet) & 255;
1362 snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1363 ostype, MACHINE, osrelease);
1364 vendor_client_len = strlen(vendor_client);
1365 *vendp++ = TAG_VENDOR_INDENTIFIER;
1366 *vendp++ = vendor_client_len;
1367 memcpy(vendp, vendor_client, vendor_client_len);
1368 vendp += vendor_client_len;;
1369 ifctx->dhcpquerytype = DHCP_NOMSG;
1370 switch (ifctx->state) {
1371 case IF_DHCP_UNRESOLVED:
1372 *vendp++ = TAG_DHCP_MSGTYPE;
1374 *vendp++ = DHCP_DISCOVER;
1375 ifctx->dhcpquerytype = DHCP_DISCOVER;
1376 ifctx->gotdhcpserver = 0;
1378 case IF_DHCP_OFFERED:
1379 *vendp++ = TAG_DHCP_MSGTYPE;
1381 *vendp++ = DHCP_REQUEST;
1382 ifctx->dhcpquerytype = DHCP_REQUEST;
1383 *vendp++ = TAG_DHCP_REQ_ADDR;
1385 memcpy(vendp, &ifctx->reply.yiaddr, 4);
1387 if (ifctx->gotdhcpserver != 0) {
1388 *vendp++ = TAG_DHCP_SERVERID;
1390 memcpy(vendp, &ifctx->dhcpserver, 4);
1393 *vendp++ = TAG_DHCP_LEASETIME;
1395 leasetime = htonl(300);
1396 memcpy(vendp, &leasetime, 4);
1403 ifctx->call.secs = 0;
1404 ifctx->call.flags = htons(0x8000); /* We need an broadcast answer */
1409 bootpc_hascookie(struct bootp_packet *bp)
1411 return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1412 bp->vend[2] == 83 && bp->vend[3] == 99);
1417 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1418 unsigned char *start, int len, int tag)
1424 if (tctx->badtag != 0 || tctx->badopt != 0)
1432 if (code == TAG_PAD)
1434 if (code == TAG_END)
1436 if (j >= ej || j + *j + 1 > ej) {
1442 if (tctx->taglen + len > TAG_MAXLEN) {
1448 memcpy(tctx->buf + tctx->taglen,
1450 tctx->taglen += len;
1452 if (code == TAG_OVERLOAD)
1453 tctx->overload = *j;
1460 static unsigned char *
1461 bootpc_tag(struct bootpc_tagcontext *tctx, struct bootp_packet *bp,
1473 if (bootpc_hascookie(bp) == 0)
1477 ej = (unsigned char *) bp + len;
1479 bootpc_tag_helper(tctx, &bp->vend[4],
1480 (unsigned char *) bp + len - &bp->vend[4], tag);
1482 if ((tctx->overload & OVERLOAD_FILE) != 0)
1483 bootpc_tag_helper(tctx,
1484 (unsigned char *) bp->file,
1487 if ((tctx->overload & OVERLOAD_SNAME) != 0)
1488 bootpc_tag_helper(tctx,
1489 (unsigned char *) bp->sname,
1493 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1495 tctx->buf[tctx->taglen] = '\0';
1501 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1502 struct bootpc_globalcontext *gctx)
1508 ifctx->gotnetmask = 0;
1510 clear_sinaddr(&ifctx->myaddr);
1511 clear_sinaddr(&ifctx->netmask);
1512 clear_sinaddr(&ifctx->gw);
1514 ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1516 ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1517 snprintf(gctx->lookup_path, sizeof(gctx->lookup_path),
1519 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1521 printf("%s at ", ifctx->ireq.ifr_name);
1522 print_sin_addr(&ifctx->myaddr);
1524 print_in_addr(ifctx->reply.siaddr);
1526 ifctx->gw.sin_addr = ifctx->reply.giaddr;
1527 if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1528 printf(" via gateway ");
1529 print_in_addr(ifctx->reply.giaddr);
1532 /* This call used for the side effect (overload flag) */
1533 (void) bootpc_tag(&gctx->tmptag,
1534 &ifctx->reply, ifctx->replylen, TAG_END);
1536 if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1537 if (ifctx->reply.sname[0] != '\0')
1538 printf(" server name %s", ifctx->reply.sname);
1539 if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1540 if (ifctx->reply.file[0] != '\0')
1541 printf(" boot file %s", ifctx->reply.file);
1545 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1548 if (gctx->tag.taglen != 4)
1549 panic("bootpc: subnet mask len is %d",
1551 bcopy(p, &ifctx->netmask.sin_addr, 4);
1552 ifctx->gotnetmask = 1;
1553 printf("subnet mask ");
1554 print_sin_addr(&ifctx->netmask);
1558 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1562 if (gctx->tag.taglen % 4)
1563 panic("bootpc: Router Len is %d", gctx->tag.taglen);
1564 if (gctx->tag.taglen > 0) {
1565 bcopy(p, &ifctx->gw.sin_addr, 4);
1567 print_sin_addr(&ifctx->gw);
1574 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1577 if (gctx->setrootfs != NULL) {
1578 printf("rootfs %s (ignored) ", p);
1579 } else if (setfs(&nd->root_saddr,
1580 nd->root_hostnam, p)) {
1581 printf("rootfs %s ",p);
1582 gctx->gotrootpath = 1;
1583 ifctx->gotrootpath = 1;
1584 gctx->setrootfs = ifctx;
1586 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1590 mountopts(&nd->root_args, p);
1591 printf("rootopts %s ", p);
1594 panic("Failed to set rootfs to %s",p);
1597 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1600 if (gctx->setswapfs != NULL) {
1601 printf("swapfs %s (ignored) ", p);
1602 } else if (setfs(&nd->swap_saddr,
1603 nd->swap_hostnam, p)) {
1604 gctx->gotswappath = 1;
1605 gctx->setswapfs = ifctx;
1606 printf("swapfs %s ", p);
1608 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1612 /* swap mount options */
1613 mountopts(&nd->swap_args, p);
1614 printf("swapopts %s ", p);
1617 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1622 if (gctx->tag.taglen != 4)
1624 "Expected 4 bytes for swaplen, "
1627 bcopy(p, &swaplen, 4);
1628 nd->swap_nblks = ntohl(swaplen);
1629 printf("swapsize %d KB ",
1633 panic("Failed to set swapfs to %s", p);
1636 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1639 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1640 panic("bootpc: hostname >= %d bytes",
1642 if (gctx->sethostname != NULL) {
1643 printf("hostname %s (ignored) ", p);
1645 strcpy(nd->my_hostnam, p);
1646 strcpy(hostname, p);
1647 printf("hostname %s ",hostname);
1648 gctx->sethostname = ifctx;
1651 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1653 if (p != NULL) { /* store in a sysctl variable */
1654 int i, l = sizeof(bootp_cookie) - 1;
1655 for (i = 0; i < l && p[i] != '\0'; i++)
1656 bootp_cookie[i] = p[i];
1662 if (ifctx->gotnetmask == 0) {
1663 if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1664 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1665 else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1666 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1668 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1670 if (ifctx->gotgw == 0) {
1672 ifctx->gw.sin_addr.s_addr = ifctx->myaddr.sin_addr.s_addr;
1679 struct bootpc_ifcontext *ifctx, *nctx; /* Interface BOOTP contexts */
1680 struct bootpc_globalcontext *gctx; /* Global BOOTP context */
1683 struct nfsv3_diskless *nd;
1686 nd = &nfsv3_diskless;
1690 * If already filled in, don't touch it here
1692 if (nfs_diskless_valid != 0)
1696 * Wait until arp entries can be handled.
1698 while (time_second == 0)
1699 tsleep(&time_second, 0, "arpkludge", 10);
1701 gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK);
1703 panic("Failed to allocate bootp global context structure");
1705 bzero(gctx, sizeof(*gctx));
1706 gctx->xid = ~0xFFFF;
1707 gctx->starttime = time_second;
1709 ifctx = allocifctx(gctx);
1712 * Find a network interface.
1714 #ifdef BOOTP_WIRED_TO
1715 printf("bootpc_init: wired to interface '%s'\n",
1716 __XSTRING(BOOTP_WIRED_TO));
1718 bzero(&ifctx->ireq, sizeof(ifctx->ireq));
1719 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1720 strlcpy(ifctx->ireq.ifr_name, ifp->if_xname,
1721 sizeof(ifctx->ireq.ifr_name));
1722 #ifdef BOOTP_WIRED_TO
1723 if (strcmp(ifctx->ireq.ifr_name,
1724 __XSTRING(BOOTP_WIRED_TO)) != 0)
1727 if ((ifp->if_flags &
1728 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1732 if (gctx->interfaces != NULL)
1733 gctx->lastinterface->next = ifctx;
1735 gctx->interfaces = ifctx;
1737 gctx->lastinterface = ifctx;
1738 ifctx = allocifctx(gctx);
1740 free(ifctx, M_TEMP);
1742 if (gctx->interfaces == NULL) {
1743 #ifdef BOOTP_WIRED_TO
1744 panic("bootpc_init: Could not find interface specified "
1745 "by BOOTP_WIRED_TO: "
1746 __XSTRING(BOOTP_WIRED_TO));
1748 panic("bootpc_init: no suitable interface");
1752 gctx->gotrootpath = 0;
1753 gctx->gotswappath = 0;
1756 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1757 bootpc_fakeup_interface(ifctx, gctx, td);
1759 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1760 bootpc_compose_query(ifctx, gctx, td);
1762 ifctx = gctx->interfaces;
1763 error = bootpc_call(gctx, td);
1766 #ifdef BOOTP_NFSROOT
1767 panic("BOOTP call failed");
1769 printf("BOOTP call failed\n");
1773 mountopts(&nd->root_args, NULL);
1775 mountopts(&nd->swap_args, NULL);
1777 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1778 if (bootpc_ifctx_isresolved(ifctx) != 0)
1779 bootpc_decode_reply(nd, ifctx, gctx);
1781 if (gctx->gotswappath == 0)
1783 #ifdef BOOTP_NFSROOT
1784 if (gctx->gotrootpath == 0)
1785 panic("bootpc: No root path offered");
1788 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
1789 bootpc_adjust_interface(ifctx, gctx, td);
1794 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1795 if (ifctx->gotrootpath != 0)
1797 if (ifctx == NULL) {
1798 for (ifctx = gctx->interfaces;
1800 ifctx = ifctx->next)
1801 if (bootpc_ifctx_isresolved(ifctx) != 0)
1807 if (gctx->gotrootpath != 0) {
1809 error = md_mount(&nd->root_saddr, nd->root_hostnam,
1810 nd->root_fh, &nd->root_fhsize,
1811 &nd->root_args, td);
1813 panic("nfs_boot: mountd root, error=%d", error);
1815 if (gctx->gotswappath != 0) {
1817 error = md_mount(&nd->swap_saddr,
1819 nd->swap_fh, &nd->swap_fhsize,
1820 &nd->swap_args, td);
1822 panic("nfs_boot: mountd swap, error=%d",
1825 error = md_lookup_swap(&nd->swap_saddr,
1827 nd->swap_fh, &nd->swap_fhsize,
1828 &nd->swap_args, td);
1830 panic("nfs_boot: lookup swap, error=%d",
1833 nfs_diskless_valid = 3;
1836 strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1837 bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1838 bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1839 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1840 ifctx->myaddr.sin_addr.s_addr |
1841 ~ ifctx->netmask.sin_addr.s_addr;
1842 bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1845 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = nctx) {
1847 free(ifctx, M_TEMP);
1855 * Given a server pathname, get an NFS file handle.
1856 * Also, sets sin->sin_port to the NFS service port.
1859 md_mount(struct sockaddr_in *mdsin, /* mountd server address */
1863 struct nfs_args *args,
1873 /* First try NFS v3 */
1874 /* Get port number for MOUNTD. */
1875 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1876 &mdsin->sin_port, td);
1878 m = xdr_string_encode(path, strlen(path));
1880 /* Do RPC to mountd. */
1881 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1882 RPCMNT_MOUNT, &m, NULL, td);
1885 args->flags |= NFSMNT_NFSV3;
1888 /* Fallback to NFS v2 */
1890 /* Get port number for MOUNTD. */
1891 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1892 &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_VER1,
1900 RPCMNT_MOUNT, &m, NULL, td);
1902 return error; /* message already freed */
1908 if (xdr_int_decode(&m, &error) != 0 || error != 0)
1911 if ((args->flags & NFSMNT_NFSV3) != 0) {
1912 if (xdr_int_decode(&m, fhsizep) != 0 ||
1913 *fhsizep > NFSX_V3FHMAX ||
1917 *fhsizep = NFSX_V2FH;
1919 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
1922 if (args->flags & NFSMNT_NFSV3) {
1923 if (xdr_int_decode(&m, &authcount) != 0)
1926 if (authcount < 0 || authcount > 100)
1928 while (authcount > 0) {
1929 if (xdr_int_decode(&m, &authver) != 0)
1931 if (authver == RPCAUTH_UNIX)
1935 if (authunixok == 0)
1939 /* Set port number for NFS use. */
1940 error = krpc_portmap(mdsin, NFS_PROG,
1942 NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
1943 &mdsin->sin_port, td);
1957 md_lookup_swap(struct sockaddr_in *mdsin, /* mountd server address */
1961 struct nfs_args *args,
1967 int attribs_present;
1974 m = m_get(M_WAIT,MT_DATA);
1978 if ((args->flags & NFSMNT_NFSV3) != 0) {
1979 *mtod(m, u_int32_t *) = txdr_unsigned(*fhsizep);
1980 bcopy(fhp, mtod(m, u_char *) + sizeof(u_int32_t), *fhsizep);
1981 m->m_len = *fhsizep + sizeof(u_int32_t);
1983 bcopy(fhp, mtod(m, u_char *), NFSX_V2FH);
1984 m->m_len = NFSX_V2FH;
1987 m->m_next = xdr_string_encode(path, strlen(path));
1988 if (m->m_next == NULL) {
1993 /* Do RPC to nfsd. */
1994 if ((args->flags & NFSMNT_NFSV3) != 0)
1995 error = krpc_call(mdsin, NFS_PROG, NFS_VER3,
1996 NFSPROC_LOOKUP, &m, NULL, td);
1998 error = krpc_call(mdsin, NFS_PROG, NFS_VER2,
1999 NFSV2PROC_LOOKUP, &m, NULL, td);
2001 return error; /* message already freed */
2003 if (xdr_int_decode(&m, &status) != 0)
2010 if ((args->flags & NFSMNT_NFSV3) != 0) {
2011 if (xdr_int_decode(&m, fhsizep) != 0 ||
2012 *fhsizep > NFSX_V3FHMAX ||
2016 *fhsizep = NFSX_V2FH;
2018 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
2021 if ((args->flags & NFSMNT_NFSV3) != 0) {
2022 if (xdr_int_decode(&m, &attribs_present) != 0)
2024 if (attribs_present != 0) {
2025 if (xdr_opaque_decode(&m, (u_char *) &fattribs.v3,
2026 sizeof(u_int32_t) * 21) != 0)
2028 size = fxdr_unsigned(u_int32_t, fattribs.v3[6]);
2031 if (xdr_opaque_decode(&m,(u_char *) &fattribs.v2,
2032 sizeof(u_int32_t) * 17) != 0)
2034 size = fxdr_unsigned(u_int32_t, fattribs.v2[5]);
2037 if (nfsv3_diskless.swap_nblks == 0 && size != -1) {
2038 nfsv3_diskless.swap_nblks = size / 1024;
2039 printf("md_lookup_swap: Swap size is %d KB\n",
2040 nfsv3_diskless.swap_nblks);