2 * Copyright (c) 1995 Gordon Ross, Adam Glass
3 * Copyright (c) 1992 Regents of the University of California.
6 * This software was developed by the Computer Systems Engineering group
7 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
8 * contributed to Berkeley.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Lawrence Berkeley Laboratory and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * $NetBSD: krpc_subr.c,v 1.10 1995/08/08 20:43:43 gwr Exp $
40 * $FreeBSD: src/sys/nfs/bootp_subr.c,v 1.20.2.9 2003/04/24 16:51:08 ambrisko Exp $
41 * $DragonFly: src/sys/vfs/nfs/bootp_subr.c,v 1.12 2005/10/27 14:03:56 sephe Exp $
44 #include "opt_bootp.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/sockio.h>
51 #include <sys/malloc.h>
52 #include <sys/mount.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/sysctl.h>
60 #include <net/route.h>
62 #include <netinet/in.h>
63 #include <net/if_types.h>
64 #include <net/if_dl.h>
69 #include "nfsdiskless.h"
72 #include "nfsmountrpc.h"
74 #define BOOTP_MIN_LEN 300 /* Minimum size of bootp udp packet */
76 #ifndef BOOTP_SETTLE_DELAY
77 #define BOOTP_SETTLE_DELAY 3
81 * What is the longest we will wait before re-sending a request?
82 * Note this is also the frequency of "RPC timeout" messages.
83 * The re-send loop count sup linearly to this maximum, so the
84 * first complaint will happen after (1+2+3+4+5)=15 seconds.
86 #define MAX_RESEND_DELAY 5 /* seconds */
88 /* Definitions from RFC951 */
97 struct in_addr ciaddr;
98 struct in_addr yiaddr;
99 struct in_addr siaddr;
100 struct in_addr giaddr;
101 unsigned char chaddr[16];
104 unsigned char vend[1222];
107 struct bootpc_ifcontext {
108 struct bootpc_ifcontext *next;
109 struct bootp_packet call;
110 struct bootp_packet reply;
116 struct sockaddr_dl *sdl;
117 struct sockaddr_in myaddr;
118 struct sockaddr_in netmask;
119 struct sockaddr_in gw;
120 struct sockaddr_in broadcast; /* Different for each interface */
136 int dhcpquerytype; /* dhcp type sent */
137 struct in_addr dhcpserver;
141 #define TAG_MAXLEN 1024
142 struct bootpc_tagcontext {
143 char buf[TAG_MAXLEN + 1];
151 struct bootpc_globalcontext {
152 struct bootpc_ifcontext *interfaces;
153 struct bootpc_ifcontext *lastinterface;
161 struct bootp_packet reply;
163 struct bootpc_ifcontext *setswapfs;
164 struct bootpc_ifcontext *setrootfs;
165 struct bootpc_ifcontext *sethostname;
166 char lookup_path[24];
167 struct bootpc_tagcontext tmptag;
168 struct bootpc_tagcontext tag;
171 #define IPPORT_BOOTPC 68
172 #define IPPORT_BOOTPS 67
174 #define BOOTP_REQUEST 1
175 #define BOOTP_REPLY 2
178 #define TAG_PAD 0 /* Pad option, implicit length 1 */
179 #define TAG_SUBNETMASK 1 /* RFC 950 subnet mask */
180 #define TAG_ROUTERS 3 /* Routers (in order of preference) */
181 #define TAG_HOSTNAME 12 /* Client host name */
182 #define TAG_ROOT 17 /* Root path */
184 /* DHCP specific tags */
185 #define TAG_OVERLOAD 52 /* Option Overload */
186 #define TAG_MAXMSGSIZE 57 /* Maximum DHCP Message Size */
188 #define TAG_END 255 /* End Option (i.e. no more options) */
190 /* Overload values */
191 #define OVERLOAD_FILE 1
192 #define OVERLOAD_SNAME 2
194 /* Site specific tags: */
196 #define TAG_SWAPSIZE 129
197 #define TAG_ROOTOPTS 130
198 #define TAG_SWAPOPTS 131
199 #define TAG_COOKIE 134 /* ascii info for userland, exported via sysctl */
201 #define TAG_DHCP_MSGTYPE 53
202 #define TAG_DHCP_REQ_ADDR 50
203 #define TAG_DHCP_SERVERID 54
204 #define TAG_DHCP_LEASETIME 51
206 #define TAG_VENDOR_INDENTIFIER 60
209 #define DHCP_DISCOVER 1
211 #define DHCP_REQUEST 3
214 extern int nfs_diskless_valid;
215 extern struct nfsv3_diskless nfsv3_diskless;
216 static char bootp_cookie[128];
217 SYSCTL_STRING(_kern, OID_AUTO, bootp_cookie, CTLFLAG_RD,
218 bootp_cookie, 0, "Cookie (T134) supplied by bootp server");
221 static void print_in_addr(struct in_addr addr);
222 static void print_sin_addr(struct sockaddr_in *addr);
223 static void clear_sinaddr(struct sockaddr_in *sin);
225 struct bootpc_ifcontext *allocifctx(struct bootpc_globalcontext *gctx);
226 static void bootpc_compose_query(struct bootpc_ifcontext *ifctx,
227 struct bootpc_globalcontext *gctx,
229 static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx,
230 struct bootp_packet *bp, int len, int tag);
231 static void bootpc_tag_helper(struct bootpc_tagcontext *tctx,
232 unsigned char *start, int len, int tag);
235 void bootpboot_p_sa(struct sockaddr *sa,struct sockaddr *ma);
236 void bootpboot_p_ma(struct sockaddr *ma);
237 void bootpboot_p_rtentry(struct rtentry *rt);
238 void bootpboot_p_tree(struct radix_node *rn);
239 void bootpboot_p_rtlist(void);
240 void bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa);
241 void bootpboot_p_iflist(void);
244 static int bootpc_call(struct bootpc_globalcontext *gctx,
247 static int bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
248 struct bootpc_globalcontext *gctx,
251 static int bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
252 struct bootpc_globalcontext *gctx,
255 static void bootpc_decode_reply(struct nfsv3_diskless *nd,
256 struct bootpc_ifcontext *ifctx,
257 struct bootpc_globalcontext *gctx);
259 static int bootpc_received(struct bootpc_globalcontext *gctx,
260 struct bootpc_ifcontext *ifctx);
262 static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
263 static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
264 static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
266 void bootpc_init(void);
269 * In order to have multiple active interfaces with address 0.0.0.0
270 * and be able to send data to a selected interface, we perform
273 * - The 'broadcast' address is different for each interface.
275 * - We temporarily add routing pointing 255.255.255.255 to the
276 * selected interface broadcast address, thus the packet sent
277 * goes to that interface.
282 bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma)
285 printf("(sockaddr *) <null>");
288 switch (sa->sa_family) {
291 struct sockaddr_in *sin;
293 sin = (struct sockaddr_in *) sa;
297 sin = (struct sockaddr_in *) ma;
305 struct sockaddr_dl *sli;
308 sli = (struct sockaddr_dl *) sa;
309 printf("link %.*s ", sli->sdl_nlen, sli->sdl_data);
310 for (i = 0; i < sli->sdl_alen; i++) {
313 printf("%x", ((unsigned char *) LLADDR(sli))[i]);
318 printf("af%d", sa->sa_family);
324 bootpboot_p_ma(struct sockaddr *ma)
330 printf("%x", *(int *)ma);
335 bootpboot_p_rtentry(struct rtentry *rt)
337 bootpboot_p_sa(rt_key(rt), rt_mask(rt));
339 bootpboot_p_ma(rt->rt_genmask);
341 bootpboot_p_sa(rt->rt_gateway, NULL);
343 printf("flags %x", (unsigned short) rt->rt_flags);
344 printf(" %d", (int) rt->rt_rmx.rmx_expire);
345 printf(" %s\n", if_name(rt->rt_ifp));
350 bootpboot_p_tree(struct radix_node *rn)
353 if (rn->rn_bit < 0) {
354 if ((rn->rn_flags & RNF_ROOT) != 0) {
356 bootpboot_p_rtentry((struct rtentry *) rn);
358 rn = rn->rn_dupedkey;
360 bootpboot_p_tree(rn->rn_left);
361 bootpboot_p_tree(rn->rn_right);
369 bootpboot_p_rtlist(void)
371 printf("Routing table:\n");
372 bootpboot_p_tree(rt_tables[AF_INET]->rnh_treetop);
377 bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
379 printf("%s flags %x, addr ",
381 (unsigned short) ifp->if_flags);
382 print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
383 printf(", broadcast ");
384 print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
385 printf(", netmask ");
386 print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
392 bootpboot_p_iflist(void)
397 printf("Interface list:\n");
398 TAILQ_FOREACH(ifp, &ifnet, if_link) {
399 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
400 if (ifa->ifa_addr->sa_family == AF_INET)
401 bootpboot_p_if(ifp, ifa);
404 #endif /* defined(BOOTP_DEBUG) */
408 clear_sinaddr(struct sockaddr_in *sin)
410 bzero(sin, sizeof(*sin));
411 sin->sin_len = sizeof(*sin);
412 sin->sin_family = AF_INET;
413 sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
418 static struct bootpc_ifcontext *
419 allocifctx(struct bootpc_globalcontext *gctx)
421 struct bootpc_ifcontext *ifctx;
422 ifctx = (struct bootpc_ifcontext *) malloc(sizeof(*ifctx),
425 panic("Failed to allocate bootp interface context structure");
427 bzero(ifctx, sizeof(*ifctx));
428 ifctx->xid = gctx->xid;
430 ifctx->state = IF_BOOTP_UNRESOLVED;
432 ifctx->state = IF_DHCP_UNRESOLVED;
440 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
442 if (ifctx->state == IF_BOOTP_RESOLVED ||
443 ifctx->state == IF_DHCP_RESOLVED)
450 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
452 if (ifctx->state == IF_BOOTP_UNRESOLVED ||
453 ifctx->state == IF_DHCP_UNRESOLVED)
460 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
462 if (ifctx->state == IF_BOOTP_FAILED ||
463 ifctx->state == IF_DHCP_FAILED)
470 bootpc_received(struct bootpc_globalcontext *gctx,
471 struct bootpc_ifcontext *ifctx)
473 unsigned char dhcpreplytype;
476 * Need timeout for fallback to less
477 * desirable alternative.
481 /* This call used for the side effect (badopt flag) */
482 (void) bootpc_tag(&gctx->tmptag, &gctx->reply,
486 /* If packet is invalid, ignore it */
487 if (gctx->tmptag.badopt != 0)
490 p = bootpc_tag(&gctx->tmptag, &gctx->reply,
491 gctx->replylen, TAG_DHCP_MSGTYPE);
495 dhcpreplytype = DHCP_NOMSG;
497 switch (ifctx->dhcpquerytype) {
499 if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */
500 #ifndef BOOTP_FORCE_DHCP
501 && dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */
507 if (dhcpreplytype != DHCP_ACK)
515 /* Ignore packet unless it gives us a root tag we didn't have */
517 if ((ifctx->state == IF_BOOTP_RESOLVED ||
518 (ifctx->dhcpquerytype == DHCP_DISCOVER &&
519 (ifctx->state == IF_DHCP_OFFERED ||
520 ifctx->state == IF_DHCP_RESOLVED))) &&
521 (bootpc_tag(&gctx->tmptag, &ifctx->reply,
524 bootpc_tag(&gctx->tmptag, &gctx->reply,
532 ifctx->replylen = gctx->replylen;
534 /* XXX: Only reset if 'perfect' response */
535 if (ifctx->state == IF_BOOTP_UNRESOLVED)
536 ifctx->state = IF_BOOTP_RESOLVED;
537 else if (ifctx->state == IF_DHCP_UNRESOLVED &&
538 ifctx->dhcpquerytype == DHCP_DISCOVER) {
539 if (dhcpreplytype == DHCP_OFFER)
540 ifctx->state = IF_DHCP_OFFERED;
542 ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */
543 } else if (ifctx->state == IF_DHCP_OFFERED &&
544 ifctx->dhcpquerytype == DHCP_REQUEST)
545 ifctx->state = IF_DHCP_RESOLVED;
548 if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
549 ifctx->state != IF_BOOTP_RESOLVED) {
550 p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
551 ifctx->replylen, TAG_DHCP_SERVERID);
552 if (p != NULL && gctx->tmptag.taglen == 4) {
553 memcpy(&ifctx->dhcpserver, p, 4);
554 ifctx->gotdhcpserver = 1;
556 ifctx->gotdhcpserver = 0;
560 ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
563 ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
565 TAG_ROUTERS) != NULL);
566 ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
568 TAG_SUBNETMASK) != NULL);
573 bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
576 struct sockaddr_in *sin, dst;
580 int error, on, rcvflg, timo, len;
584 struct bootpc_ifcontext *ifctx;
591 * Create socket and set its recieve timeout.
593 error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td);
599 bzero(&sopt, sizeof(sopt));
600 sopt.sopt_level = SOL_SOCKET;
601 sopt.sopt_name = SO_RCVTIMEO;
603 sopt.sopt_valsize = sizeof tv;
605 error = sosetopt(so, &sopt);
613 sopt.sopt_name = SO_BROADCAST;
615 sopt.sopt_valsize = sizeof on;
617 error = sosetopt(so, &sopt);
626 sopt.sopt_name = SO_DONTROUTE;
628 sopt.sopt_valsize = sizeof on;
630 error = sosetopt(so, &sopt);
635 * Bind the local endpoint to a bootp client port.
639 sin->sin_port = htons(IPPORT_BOOTPC);
640 error = sobind(so, (struct sockaddr *)sin, td);
642 printf("bind failed\n");
647 * Setup socket address for the server.
651 sin->sin_addr.s_addr = INADDR_BROADCAST;
652 sin->sin_port = htons(IPPORT_BOOTPS);
655 * Send it, repeatedly, until a reply is received,
656 * but delay each re-send by an increasing amount.
657 * If the delay hits the maximum, start complaining.
666 for (ifctx = gctx->interfaces;
668 ifctx = ifctx->next) {
669 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
670 bootpc_tag(&gctx->tmptag, &ifctx->reply,
676 for (ifctx = gctx->interfaces;
678 ifctx = ifctx->next) {
679 ifctx->outstanding = 0;
680 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
684 if (bootpc_ifctx_isfailed(ifctx) != 0)
688 ifctx->outstanding = 1;
690 /* Proceed to next step in DHCP negotiation */
691 if ((ifctx->state == IF_DHCP_OFFERED &&
692 ifctx->dhcpquerytype != DHCP_REQUEST) ||
693 (ifctx->state == IF_DHCP_UNRESOLVED &&
694 ifctx->dhcpquerytype != DHCP_DISCOVER) ||
695 (ifctx->state == IF_BOOTP_UNRESOLVED &&
696 ifctx->dhcpquerytype != DHCP_NOMSG)) {
698 bootpc_compose_query(ifctx, gctx, td);
701 /* Send BOOTP request (or re-send). */
703 if (ifctx->sentmsg == 0) {
704 switch(ifctx->dhcpquerytype) {
716 printf("Sending %s packet from "
717 "interface %s (%*D)\n",
719 ifctx->ireq.ifr_name,
720 ifctx->sdl->sdl_alen,
721 (unsigned char *) LLADDR(ifctx->sdl),
726 aio.iov_base = (caddr_t) &ifctx->call;
727 aio.iov_len = sizeof(ifctx->call);
731 auio.uio_segflg = UIO_SYSSPACE;
732 auio.uio_rw = UIO_WRITE;
734 auio.uio_resid = sizeof(ifctx->call);
737 /* Set netmask to 0.0.0.0 */
739 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
741 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
742 (caddr_t) &ifctx->ireq, td);
745 "set if netmask, error=%d",
748 error = sosend(so, (struct sockaddr *) &dst,
749 &auio, NULL, NULL, 0, td);
751 printf("bootpc_call: sosend: %d state %08x\n",
752 error, (int) so->so_state);
755 /* XXX: Is this needed ? */
756 tsleep(&error, 0, "bootpw", 10);
758 /* Set netmask to 255.0.0.0 */
760 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
762 sin->sin_addr.s_addr = htonl(0xff000000u);
763 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
764 (caddr_t) &ifctx->ireq, td);
767 "set if netmask, error=%d",
772 if (outstanding == 0 &&
773 (rtimo == 0 || time_second >= rtimo)) {
778 /* Determine new timeout. */
779 if (timo < MAX_RESEND_DELAY)
782 printf("DHCP/BOOTP timeout for server ");
783 print_sin_addr(&dst);
788 * Wait for up to timo seconds for a reply.
789 * The socket receive timeout was set to 1 second.
791 atimo = timo + time_second;
792 while (time_second < atimo) {
793 aio.iov_base = (caddr_t) &gctx->reply;
794 aio.iov_len = sizeof(gctx->reply);
798 auio.uio_segflg = UIO_SYSSPACE;
799 auio.uio_rw = UIO_READ;
801 auio.uio_resid = sizeof(gctx->reply);
805 error = soreceive(so, NULL, &auio,
806 NULL, NULL, &rcvflg);
807 gctx->secs = time_second - gctx->starttime;
808 for (ifctx = gctx->interfaces;
810 ifctx = ifctx->next) {
811 if (bootpc_ifctx_isresolved(ifctx) != 0 ||
812 bootpc_ifctx_isfailed(ifctx) != 0)
815 ifctx->call.secs = htons(gctx->secs);
817 if (error == EWOULDBLOCK)
821 len = sizeof(gctx->reply) - auio.uio_resid;
823 /* Do we have the required number of bytes ? */
824 if (len < BOOTP_MIN_LEN)
826 gctx->replylen = len;
829 if (gctx->reply.op != BOOTP_REPLY)
832 /* Is this an answer to our query */
833 for (ifctx = gctx->interfaces;
835 ifctx = ifctx->next) {
836 if (gctx->reply.xid != ifctx->call.xid)
839 /* Same HW address size ? */
840 if (gctx->reply.hlen != ifctx->call.hlen)
843 /* Correct HW address ? */
844 if (bcmp(gctx->reply.chaddr,
846 ifctx->call.hlen) != 0)
853 s = bootpc_tag(&gctx->tmptag,
866 s = "DHCP (unexpected)";
872 printf("Received %s packet"
875 ifctx->ireq.ifr_name);
876 print_in_addr(gctx->reply.siaddr);
877 if (gctx->reply.giaddr.s_addr !=
880 print_in_addr(gctx->reply.giaddr);
882 if (bootpc_received(gctx, ifctx) != 0) {
883 printf(" (accepted)");
884 if (ifctx->outstanding) {
885 ifctx->outstanding = 0;
888 /* Network settle delay */
889 if (outstanding == 0)
890 atimo = time_second +
893 printf(" (ignored)");
894 if (ifctx->gotrootpath) {
896 rtimo = time_second +
898 printf(" (got root path)");
900 printf(" (no root path)");
905 if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
908 /* Force a retry if halfway in DHCP negotiation */
910 for (ifctx = gctx->interfaces; ifctx != NULL;
911 ifctx = ifctx->next) {
912 if (ifctx->state == IF_DHCP_OFFERED) {
913 if (ifctx->dhcpquerytype == DHCP_DISCOVER)
916 ifctx->state = IF_DHCP_UNRESOLVED;
923 if (gotrootpath != 0) {
924 gctx->gotrootpath = gotrootpath;
925 if (rtimo != 0 && time_second >= rtimo)
928 } /* forever send/receive */
931 * XXX: These are errors of varying seriousness being silently
935 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
936 if (bootpc_ifctx_isresolved(ifctx) == 0) {
937 printf("%s timeout for interface %s\n",
938 ifctx->dhcpquerytype != DHCP_NOMSG ?
940 ifctx->ireq.ifr_name);
943 if (gctx->gotrootpath != 0) {
945 printf("Got a root path, ignoring remaining timeout\n");
950 #ifndef BOOTP_NFSROOT
951 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
952 if (bootpc_ifctx_isresolved(ifctx) != 0) {
969 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
970 struct bootpc_globalcontext *gctx,
973 struct sockaddr_in *sin;
979 struct sockaddr_dl *sdl;
981 error = socreate(AF_INET, &ifctx->so, SOCK_DGRAM, 0, td);
983 panic("nfs_boot: socreate, error=%d", error);
989 * Bring up the interface.
991 * Get the old interface flags and or IFF_UP into them; if
992 * IFF_UP set blindly, interface selection can be clobbered.
994 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, td);
996 panic("bootpc_fakeup_interface: GIFFLAGS, error=%d", error);
997 ireq->ifr_flags |= IFF_UP;
998 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, td);
1000 panic("bootpc_fakeup_interface: SIFFLAGS, error=%d", error);
1003 * Do enough of ifconfig(8) so that the chosen interface
1004 * can talk to the servers. (just set the address)
1007 /* addr is 0.0.0.0 */
1009 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1011 error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, td);
1012 if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1013 panic("bootpc_fakeup_interface: "
1014 "set if addr, error=%d", error);
1016 /* netmask is 255.0.0.0 */
1018 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1020 sin->sin_addr.s_addr = htonl(0xff000000u);
1021 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t)ireq, td);
1023 panic("bootpc_fakeup_interface: set if netmask, error=%d",
1026 /* Broadcast is 255.255.255.255 */
1028 sin = (struct sockaddr_in *)&ireq->ifr_addr;
1030 clear_sinaddr(&ifctx->broadcast);
1031 sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
1032 ifctx->broadcast.sin_addr.s_addr = sin->sin_addr.s_addr;
1034 error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t)ireq, td);
1035 if (error != 0 && error != EADDRNOTAVAIL)
1036 panic("bootpc_fakeup_interface: "
1037 "set if broadcast addr, error=%d",
1041 /* Get HW address */
1044 TAILQ_FOREACH(ifa, &ifctx->ifp->if_addrhead, ifa_link)
1045 if (ifa->ifa_addr->sa_family == AF_LINK &&
1046 (sdl = ((struct sockaddr_dl *) ifa->ifa_addr)) != NULL &&
1047 sdl->sdl_type == IFT_ETHER)
1051 panic("bootpc: Unable to find HW address for %s",
1052 ifctx->ireq.ifr_name);
1060 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
1061 struct bootpc_globalcontext *gctx,
1065 struct sockaddr_in defdst;
1066 struct sockaddr_in defmask;
1067 struct sockaddr_in *sin;
1071 struct sockaddr_in *myaddr;
1072 struct sockaddr_in *netmask;
1073 struct sockaddr_in *gw;
1075 ireq = &ifctx->ireq;
1077 myaddr = &ifctx->myaddr;
1078 netmask = &ifctx->netmask;
1081 if (bootpc_ifctx_isresolved(ifctx) == 0) {
1083 /* Shutdown interfaces where BOOTP failed */
1085 printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
1086 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, td);
1088 panic("bootpc_adjust_interface: "
1089 "SIOCGIFFLAGS, error=%d", error);
1090 ireq->ifr_flags &= ~IFF_UP;
1091 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, td);
1093 panic("bootpc_adjust_interface: "
1094 "SIOCSIFFLAGS, error=%d", error);
1096 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1098 error = ifioctl(so, SIOCDIFADDR, (caddr_t) ireq, td);
1099 if (error != 0 && (error != EADDRNOTAVAIL ||
1100 ifctx == gctx->interfaces))
1101 panic("bootpc_adjust_interface: "
1102 "SIOCDIFADDR, error=%d", error);
1107 printf("Adjusted interface %s\n", ifctx->ireq.ifr_name);
1109 * Do enough of ifconfig(8) so that the chosen interface
1110 * can talk to the servers. (just set the address)
1112 bcopy(netmask, &ireq->ifr_addr, sizeof(*netmask));
1113 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t) ireq, td);
1115 panic("bootpc_adjust_interface: "
1116 "set if netmask, error=%d", error);
1118 /* Broadcast is with host part of IP address all 1's */
1120 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1122 sin->sin_addr.s_addr = myaddr->sin_addr.s_addr |
1123 ~ netmask->sin_addr.s_addr;
1124 error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t) ireq, td);
1126 panic("bootpc_adjust_interface: "
1127 "set if broadcast addr, error=%d", error);
1129 bcopy(myaddr, &ireq->ifr_addr, sizeof(*myaddr));
1130 error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, td);
1131 if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1132 panic("bootpc_adjust_interface: "
1133 "set if addr, error=%d", error);
1135 /* Add new default route */
1137 if (ifctx->gotgw != 0 || gctx->gotgw == 0) {
1138 clear_sinaddr(&defdst);
1139 clear_sinaddr(&defmask);
1140 error = rtrequest(RTM_ADD,
1141 (struct sockaddr *) &defdst,
1142 (struct sockaddr *) gw,
1143 (struct sockaddr *) &defmask,
1144 (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
1146 printf("bootpc_adjust_interface: "
1147 "add net route, error=%d\n", error);
1156 print_sin_addr(struct sockaddr_in *sin)
1158 print_in_addr(sin->sin_addr);
1163 print_in_addr(struct in_addr addr)
1167 ip = ntohl(addr.s_addr);
1168 printf("%d.%d.%d.%d",
1169 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1173 bootpc_compose_query(struct bootpc_ifcontext *ifctx,
1174 struct bootpc_globalcontext *gctx, struct thread *td)
1176 unsigned char *vendp;
1177 unsigned char vendor_client[64];
1179 uint8_t vendor_client_len;
1181 ifctx->gotrootpath = 0;
1183 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1186 ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */
1187 ifctx->call.htype = 1; /* 10mb ethernet */
1188 ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1189 ifctx->call.hops = 0;
1190 if (bootpc_ifctx_isunresolved(ifctx) != 0)
1192 ifctx->call.xid = txdr_unsigned(ifctx->xid);
1193 bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1195 vendp = ifctx->call.vend;
1196 *vendp++ = 99; /* RFC1048 cookie */
1200 *vendp++ = TAG_MAXMSGSIZE;
1202 *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1203 *vendp++ = sizeof(struct bootp_packet) & 255;
1205 snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1206 ostype, MACHINE, osrelease);
1207 vendor_client_len = strlen(vendor_client);
1208 *vendp++ = TAG_VENDOR_INDENTIFIER;
1209 *vendp++ = vendor_client_len;
1210 memcpy(vendp, vendor_client, vendor_client_len);
1211 vendp += vendor_client_len;;
1212 ifctx->dhcpquerytype = DHCP_NOMSG;
1213 switch (ifctx->state) {
1214 case IF_DHCP_UNRESOLVED:
1215 *vendp++ = TAG_DHCP_MSGTYPE;
1217 *vendp++ = DHCP_DISCOVER;
1218 ifctx->dhcpquerytype = DHCP_DISCOVER;
1219 ifctx->gotdhcpserver = 0;
1221 case IF_DHCP_OFFERED:
1222 *vendp++ = TAG_DHCP_MSGTYPE;
1224 *vendp++ = DHCP_REQUEST;
1225 ifctx->dhcpquerytype = DHCP_REQUEST;
1226 *vendp++ = TAG_DHCP_REQ_ADDR;
1228 memcpy(vendp, &ifctx->reply.yiaddr, 4);
1230 if (ifctx->gotdhcpserver != 0) {
1231 *vendp++ = TAG_DHCP_SERVERID;
1233 memcpy(vendp, &ifctx->dhcpserver, 4);
1236 *vendp++ = TAG_DHCP_LEASETIME;
1238 leasetime = htonl(300);
1239 memcpy(vendp, &leasetime, 4);
1246 ifctx->call.secs = 0;
1247 ifctx->call.flags = htons(0x8000); /* We need an broadcast answer */
1252 bootpc_hascookie(struct bootp_packet *bp)
1254 return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1255 bp->vend[2] == 83 && bp->vend[3] == 99);
1260 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1261 unsigned char *start, int len, int tag)
1267 if (tctx->badtag != 0 || tctx->badopt != 0)
1275 if (code == TAG_PAD)
1277 if (code == TAG_END)
1279 if (j >= ej || j + *j + 1 > ej) {
1285 if (tctx->taglen + len > TAG_MAXLEN) {
1291 memcpy(tctx->buf + tctx->taglen,
1293 tctx->taglen += len;
1295 if (code == TAG_OVERLOAD)
1296 tctx->overload = *j;
1303 static unsigned char *
1304 bootpc_tag(struct bootpc_tagcontext *tctx, struct bootp_packet *bp,
1316 if (bootpc_hascookie(bp) == 0)
1320 ej = (unsigned char *) bp + len;
1322 bootpc_tag_helper(tctx, &bp->vend[4],
1323 (unsigned char *) bp + len - &bp->vend[4], tag);
1325 if ((tctx->overload & OVERLOAD_FILE) != 0)
1326 bootpc_tag_helper(tctx,
1327 (unsigned char *) bp->file,
1330 if ((tctx->overload & OVERLOAD_SNAME) != 0)
1331 bootpc_tag_helper(tctx,
1332 (unsigned char *) bp->sname,
1336 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1338 tctx->buf[tctx->taglen] = '\0';
1344 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1345 struct bootpc_globalcontext *gctx)
1351 ifctx->gotnetmask = 0;
1353 clear_sinaddr(&ifctx->myaddr);
1354 clear_sinaddr(&ifctx->netmask);
1355 clear_sinaddr(&ifctx->gw);
1357 ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1359 ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1360 snprintf(gctx->lookup_path, sizeof(gctx->lookup_path),
1362 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1364 printf("%s at ", ifctx->ireq.ifr_name);
1365 print_sin_addr(&ifctx->myaddr);
1367 print_in_addr(ifctx->reply.siaddr);
1369 ifctx->gw.sin_addr = ifctx->reply.giaddr;
1370 if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1371 printf(" via gateway ");
1372 print_in_addr(ifctx->reply.giaddr);
1375 /* This call used for the side effect (overload flag) */
1376 (void) bootpc_tag(&gctx->tmptag,
1377 &ifctx->reply, ifctx->replylen, TAG_END);
1379 if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1380 if (ifctx->reply.sname[0] != '\0')
1381 printf(" server name %s", ifctx->reply.sname);
1382 if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1383 if (ifctx->reply.file[0] != '\0')
1384 printf(" boot file %s", ifctx->reply.file);
1388 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1391 if (gctx->tag.taglen != 4)
1392 panic("bootpc: subnet mask len is %d",
1394 bcopy(p, &ifctx->netmask.sin_addr, 4);
1395 ifctx->gotnetmask = 1;
1396 printf("subnet mask ");
1397 print_sin_addr(&ifctx->netmask);
1401 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1405 if (gctx->tag.taglen % 4)
1406 panic("bootpc: Router Len is %d", gctx->tag.taglen);
1407 if (gctx->tag.taglen > 0) {
1408 bcopy(p, &ifctx->gw.sin_addr, 4);
1410 print_sin_addr(&ifctx->gw);
1417 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1420 if (gctx->setrootfs != NULL) {
1421 printf("rootfs %s (ignored) ", p);
1422 } else if (setfs(&nd->root_saddr,
1423 nd->root_hostnam, p)) {
1424 printf("rootfs %s ",p);
1425 gctx->gotrootpath = 1;
1426 ifctx->gotrootpath = 1;
1427 gctx->setrootfs = ifctx;
1429 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1433 mountopts(&nd->root_args, p);
1434 printf("rootopts %s ", p);
1437 panic("Failed to set rootfs to %s",p);
1440 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1443 if (gctx->setswapfs != NULL) {
1444 printf("swapfs %s (ignored) ", p);
1445 } else if (setfs(&nd->swap_saddr,
1446 nd->swap_hostnam, p)) {
1447 gctx->gotswappath = 1;
1448 gctx->setswapfs = ifctx;
1449 printf("swapfs %s ", p);
1451 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1455 /* swap mount options */
1456 mountopts(&nd->swap_args, p);
1457 printf("swapopts %s ", p);
1460 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1465 if (gctx->tag.taglen != 4)
1467 "Expected 4 bytes for swaplen, "
1470 bcopy(p, &swaplen, 4);
1471 nd->swap_nblks = ntohl(swaplen);
1472 printf("swapsize %d KB ",
1476 panic("Failed to set swapfs to %s", p);
1479 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1482 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1483 panic("bootpc: hostname >= %d bytes",
1485 if (gctx->sethostname != NULL) {
1486 printf("hostname %s (ignored) ", p);
1488 strcpy(nd->my_hostnam, p);
1489 strcpy(hostname, p);
1490 printf("hostname %s ",hostname);
1491 gctx->sethostname = ifctx;
1494 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1496 if (p != NULL) { /* store in a sysctl variable */
1497 int i, l = sizeof(bootp_cookie) - 1;
1498 for (i = 0; i < l && p[i] != '\0'; i++)
1499 bootp_cookie[i] = p[i];
1505 if (ifctx->gotnetmask == 0) {
1506 if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1507 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1508 else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1509 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1511 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1513 if (ifctx->gotgw == 0) {
1515 ifctx->gw.sin_addr.s_addr = ifctx->myaddr.sin_addr.s_addr;
1522 struct bootpc_ifcontext *ifctx, *nctx; /* Interface BOOTP contexts */
1523 struct bootpc_globalcontext *gctx; /* Global BOOTP context */
1526 struct nfsv3_diskless *nd;
1529 nd = &nfsv3_diskless;
1533 * If already filled in, don't touch it here
1535 if (nfs_diskless_valid != 0)
1539 * Wait until arp entries can be handled.
1541 while (time_second == 0)
1542 tsleep(&time_second, 0, "arpkludge", 10);
1544 gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK);
1546 panic("Failed to allocate bootp global context structure");
1548 bzero(gctx, sizeof(*gctx));
1549 gctx->xid = ~0xFFFF;
1550 gctx->starttime = time_second;
1552 ifctx = allocifctx(gctx);
1555 * Find a network interface.
1557 #ifdef BOOTP_WIRED_TO
1558 printf("bootpc_init: wired to interface '%s'\n",
1559 __XSTRING(BOOTP_WIRED_TO));
1561 bzero(&ifctx->ireq, sizeof(ifctx->ireq));
1562 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1563 strlcpy(ifctx->ireq.ifr_name, ifp->if_xname,
1564 sizeof(ifctx->ireq.ifr_name));
1565 #ifdef BOOTP_WIRED_TO
1566 if (strcmp(ifctx->ireq.ifr_name,
1567 __XSTRING(BOOTP_WIRED_TO)) != 0)
1570 if ((ifp->if_flags &
1571 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1575 if (gctx->interfaces != NULL)
1576 gctx->lastinterface->next = ifctx;
1578 gctx->interfaces = ifctx;
1580 gctx->lastinterface = ifctx;
1581 ifctx = allocifctx(gctx);
1583 free(ifctx, M_TEMP);
1585 if (gctx->interfaces == NULL) {
1586 #ifdef BOOTP_WIRED_TO
1587 panic("bootpc_init: Could not find interface specified "
1588 "by BOOTP_WIRED_TO: "
1589 __XSTRING(BOOTP_WIRED_TO));
1591 panic("bootpc_init: no suitable interface");
1595 gctx->gotrootpath = 0;
1596 gctx->gotswappath = 0;
1599 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1600 bootpc_fakeup_interface(ifctx, gctx, td);
1602 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1603 bootpc_compose_query(ifctx, gctx, td);
1605 ifctx = gctx->interfaces;
1606 error = bootpc_call(gctx, td);
1609 #ifdef BOOTP_NFSROOT
1610 panic("BOOTP call failed");
1612 printf("BOOTP call failed\n");
1616 mountopts(&nd->root_args, NULL);
1618 mountopts(&nd->swap_args, NULL);
1620 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1621 if (bootpc_ifctx_isresolved(ifctx) != 0)
1622 bootpc_decode_reply(nd, ifctx, gctx);
1624 if (gctx->gotswappath == 0)
1626 #ifdef BOOTP_NFSROOT
1627 if (gctx->gotrootpath == 0)
1628 panic("bootpc: No root path offered");
1631 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
1632 bootpc_adjust_interface(ifctx, gctx, td);
1637 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1638 if (ifctx->gotrootpath != 0)
1640 if (ifctx == NULL) {
1641 for (ifctx = gctx->interfaces;
1643 ifctx = ifctx->next)
1644 if (bootpc_ifctx_isresolved(ifctx) != 0)
1650 if (gctx->gotrootpath != 0) {
1652 error = md_mount(&nd->root_saddr, nd->root_hostnam,
1653 nd->root_fh, &nd->root_fhsize,
1654 &nd->root_args, td);
1656 panic("nfs_boot: mountd root, error=%d", error);
1658 if (gctx->gotswappath != 0) {
1660 error = md_mount(&nd->swap_saddr,
1662 nd->swap_fh, &nd->swap_fhsize,
1663 &nd->swap_args, td);
1665 panic("nfs_boot: mountd swap, error=%d",
1668 error = md_lookup_swap(&nd->swap_saddr,
1670 nd->swap_fh, &nd->swap_fhsize,
1671 &nd->swap_args, td);
1673 panic("nfs_boot: lookup swap, error=%d",
1676 nfs_diskless_valid = 3;
1679 strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1680 bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1681 bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1682 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1683 ifctx->myaddr.sin_addr.s_addr |
1684 ~ ifctx->netmask.sin_addr.s_addr;
1685 bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1688 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = nctx) {
1690 free(ifctx, M_TEMP);