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 $
43 #include "opt_bootp.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/sockio.h>
50 #include <sys/malloc.h>
51 #include <sys/mount.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/sysctl.h>
57 #include <sys/fcntl.h>
60 #include <net/if_var.h>
61 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <net/if_types.h>
65 #include <net/if_dl.h>
70 #include "nfsdiskless.h"
73 #include "nfsmountrpc.h"
75 #define BOOTP_MIN_LEN 300 /* Minimum size of bootp udp packet */
77 #ifndef BOOTP_SETTLE_DELAY
78 #define BOOTP_SETTLE_DELAY 3
82 * What is the longest we will wait before re-sending a request?
83 * Note this is also the frequency of "RPC timeout" messages.
84 * The re-send loop count sup linearly to this maximum, so the
85 * first complaint will happen after (1+2+3+4+5)=15 seconds.
87 #define MAX_RESEND_DELAY 5 /* seconds */
89 /* Definitions from RFC951 */
98 struct in_addr ciaddr;
99 struct in_addr yiaddr;
100 struct in_addr siaddr;
101 struct in_addr giaddr;
102 unsigned char chaddr[16];
105 unsigned char vend[1222];
108 struct bootpc_ifcontext {
109 struct bootpc_ifcontext *next;
110 struct bootp_packet call;
111 struct bootp_packet reply;
117 struct sockaddr_dl *sdl;
118 struct sockaddr_in myaddr;
119 struct sockaddr_in netmask;
120 struct sockaddr_in gw;
121 struct sockaddr_in broadcast; /* Different for each interface */
137 int dhcpquerytype; /* dhcp type sent */
138 struct in_addr dhcpserver;
142 #define TAG_MAXLEN 1024
143 struct bootpc_tagcontext {
144 char buf[TAG_MAXLEN + 1];
152 struct bootpc_globalcontext {
153 struct bootpc_ifcontext *interfaces;
154 struct bootpc_ifcontext *lastinterface;
162 struct bootp_packet reply;
164 struct bootpc_ifcontext *setswapfs;
165 struct bootpc_ifcontext *setrootfs;
166 struct bootpc_ifcontext *sethostname;
167 char lookup_path[24];
168 struct bootpc_tagcontext tmptag;
169 struct bootpc_tagcontext tag;
172 #define IPPORT_BOOTPC 68
173 #define IPPORT_BOOTPS 67
175 #define BOOTP_REQUEST 1
176 #define BOOTP_REPLY 2
179 #define TAG_PAD 0 /* Pad option, implicit length 1 */
180 #define TAG_SUBNETMASK 1 /* RFC 950 subnet mask */
181 #define TAG_ROUTERS 3 /* Routers (in order of preference) */
182 #define TAG_HOSTNAME 12 /* Client host name */
183 #define TAG_ROOT 17 /* Root path */
185 /* DHCP specific tags */
186 #define TAG_OVERLOAD 52 /* Option Overload */
187 #define TAG_MAXMSGSIZE 57 /* Maximum DHCP Message Size */
189 #define TAG_END 255 /* End Option (i.e. no more options) */
191 /* Overload values */
192 #define OVERLOAD_FILE 1
193 #define OVERLOAD_SNAME 2
195 /* Site specific tags: */
197 #define TAG_SWAPSIZE 129
198 #define TAG_ROOTOPTS 130
199 #define TAG_SWAPOPTS 131
200 #define TAG_COOKIE 134 /* ascii info for userland, exported via sysctl */
202 #define TAG_DHCP_MSGTYPE 53
203 #define TAG_DHCP_REQ_ADDR 50
204 #define TAG_DHCP_SERVERID 54
205 #define TAG_DHCP_LEASETIME 51
207 #define TAG_VENDOR_INDENTIFIER 60
210 #define DHCP_DISCOVER 1
212 #define DHCP_REQUEST 3
215 static char bootp_cookie[128];
216 SYSCTL_STRING(_kern, OID_AUTO, bootp_cookie, CTLFLAG_RD,
217 bootp_cookie, 0, "Cookie (T134) supplied by bootp server");
220 static void print_in_addr(struct in_addr addr);
221 static void print_sin_addr(struct sockaddr_in *addr);
222 static void clear_sinaddr(struct sockaddr_in *sin);
224 struct bootpc_ifcontext *allocifctx(struct bootpc_globalcontext *gctx);
225 static void bootpc_compose_query(struct bootpc_ifcontext *ifctx,
226 struct bootpc_globalcontext *gctx,
228 static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx,
229 struct bootp_packet *bp, int len, int tag);
230 static void bootpc_tag_helper(struct bootpc_tagcontext *tctx,
231 unsigned char *start, int len, int tag);
234 void bootpboot_p_sa(struct sockaddr *sa,struct sockaddr *ma);
235 void bootpboot_p_ma(struct sockaddr *ma);
236 void bootpboot_p_rtentry(struct rtentry *rt);
237 void bootpboot_p_tree(struct radix_node *rn);
238 void bootpboot_p_rtlist(void);
239 void bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa);
240 void bootpboot_p_iflist(void);
243 static int bootpc_call(struct bootpc_globalcontext *gctx,
246 static int bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
247 struct bootpc_globalcontext *gctx,
250 static int bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
251 struct bootpc_globalcontext *gctx,
254 static void bootpc_decode_reply(struct nfsv3_diskless *nd,
255 struct bootpc_ifcontext *ifctx,
256 struct bootpc_globalcontext *gctx);
258 static int bootpc_received(struct bootpc_globalcontext *gctx,
259 struct bootpc_ifcontext *ifctx);
261 static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
262 static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
263 static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
265 void bootpc_init(void);
268 * In order to have multiple active interfaces with address 0.0.0.0
269 * and be able to send data to a selected interface, we perform
272 * - The 'broadcast' address is different for each interface.
274 * - We temporarily add routing pointing 255.255.255.255 to the
275 * selected interface broadcast address, thus the packet sent
276 * goes to that interface.
281 bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma)
284 kprintf("(sockaddr *) <null>");
287 switch (sa->sa_family) {
290 struct sockaddr_in *sin;
292 sin = (struct sockaddr_in *) sa;
296 sin = (struct sockaddr_in *) ma;
304 struct sockaddr_dl *sli;
307 sli = (struct sockaddr_dl *) sa;
308 kprintf("link %.*s ", sli->sdl_nlen, sli->sdl_data);
309 for (i = 0; i < sli->sdl_alen; i++) {
312 kprintf("%x", ((unsigned char *) LLADDR(sli))[i]);
317 kprintf("af%d", sa->sa_family);
323 bootpboot_p_ma(struct sockaddr *ma)
329 kprintf("%x", *(int *)ma);
334 bootpboot_p_rtentry(struct rtentry *rt)
336 bootpboot_p_sa(rt_key(rt), rt_mask(rt));
338 bootpboot_p_ma(rt->rt_genmask);
340 bootpboot_p_sa(rt->rt_gateway, NULL);
342 kprintf("flags %x", (unsigned short) rt->rt_flags);
343 kprintf(" %d", (int) rt->rt_rmx.rmx_expire);
344 kprintf(" %s\n", if_name(rt->rt_ifp));
349 bootpboot_p_tree(struct radix_node *rn)
352 if (rn->rn_bit < 0) {
353 if ((rn->rn_flags & RNF_ROOT) != 0) {
355 bootpboot_p_rtentry((struct rtentry *) rn);
357 rn = rn->rn_dupedkey;
359 bootpboot_p_tree(rn->rn_left);
360 bootpboot_p_tree(rn->rn_right);
368 bootpboot_p_rtlist(void)
370 kprintf("Routing table:\n");
371 bootpboot_p_tree(rt_tables[AF_INET]->rnh_treetop);
376 bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
378 kprintf("%s flags %x, addr ",
380 (unsigned short) ifp->if_flags);
381 print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
382 kprintf(", broadcast ");
383 print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
384 kprintf(", netmask ");
385 print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
391 bootpboot_p_iflist(void)
394 struct ifaddr_container *ifac;
396 kprintf("Interface list:\n");
398 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
399 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
400 struct ifaddr *ifa = ifac->ifa;
402 if (ifa->ifa_addr->sa_family == AF_INET)
403 bootpboot_p_if(ifp, ifa);
408 #endif /* defined(BOOTP_DEBUG) */
412 clear_sinaddr(struct sockaddr_in *sin)
414 bzero(sin, sizeof(*sin));
415 sin->sin_len = sizeof(*sin);
416 sin->sin_family = AF_INET;
417 sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
422 static struct bootpc_ifcontext *
423 allocifctx(struct bootpc_globalcontext *gctx)
425 struct bootpc_ifcontext *ifctx;
426 ifctx = (struct bootpc_ifcontext *) kmalloc(sizeof(*ifctx),
428 bzero(ifctx, sizeof(*ifctx));
429 ifctx->xid = gctx->xid;
431 ifctx->state = IF_BOOTP_UNRESOLVED;
433 ifctx->state = IF_DHCP_UNRESOLVED;
441 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
443 if (ifctx->state == IF_BOOTP_RESOLVED ||
444 ifctx->state == IF_DHCP_RESOLVED)
451 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
453 if (ifctx->state == IF_BOOTP_UNRESOLVED ||
454 ifctx->state == IF_DHCP_UNRESOLVED)
461 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
463 if (ifctx->state == IF_BOOTP_FAILED ||
464 ifctx->state == IF_DHCP_FAILED)
471 bootpc_received(struct bootpc_globalcontext *gctx,
472 struct bootpc_ifcontext *ifctx)
474 unsigned char dhcpreplytype;
477 * Need timeout for fallback to less
478 * desirable alternative.
482 /* This call used for the side effect (badopt flag) */
483 (void) bootpc_tag(&gctx->tmptag, &gctx->reply,
487 /* If packet is invalid, ignore it */
488 if (gctx->tmptag.badopt != 0)
491 p = bootpc_tag(&gctx->tmptag, &gctx->reply,
492 gctx->replylen, TAG_DHCP_MSGTYPE);
496 dhcpreplytype = DHCP_NOMSG;
498 switch (ifctx->dhcpquerytype) {
500 if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */
501 #ifndef BOOTP_FORCE_DHCP
502 && dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */
508 if (dhcpreplytype != DHCP_ACK)
516 /* Ignore packet unless it gives us a root tag we didn't have */
518 if ((ifctx->state == IF_BOOTP_RESOLVED ||
519 (ifctx->dhcpquerytype == DHCP_DISCOVER &&
520 (ifctx->state == IF_DHCP_OFFERED ||
521 ifctx->state == IF_DHCP_RESOLVED))) &&
522 (bootpc_tag(&gctx->tmptag, &ifctx->reply,
525 bootpc_tag(&gctx->tmptag, &gctx->reply,
533 ifctx->replylen = gctx->replylen;
535 /* XXX: Only reset if 'perfect' response */
536 if (ifctx->state == IF_BOOTP_UNRESOLVED)
537 ifctx->state = IF_BOOTP_RESOLVED;
538 else if (ifctx->state == IF_DHCP_UNRESOLVED &&
539 ifctx->dhcpquerytype == DHCP_DISCOVER) {
540 if (dhcpreplytype == DHCP_OFFER)
541 ifctx->state = IF_DHCP_OFFERED;
543 ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */
544 } else if (ifctx->state == IF_DHCP_OFFERED &&
545 ifctx->dhcpquerytype == DHCP_REQUEST)
546 ifctx->state = IF_DHCP_RESOLVED;
549 if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
550 ifctx->state != IF_BOOTP_RESOLVED) {
551 p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
552 ifctx->replylen, TAG_DHCP_SERVERID);
553 if (p != NULL && gctx->tmptag.taglen == 4) {
554 memcpy(&ifctx->dhcpserver, p, 4);
555 ifctx->gotdhcpserver = 1;
557 ifctx->gotdhcpserver = 0;
561 ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
564 ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
566 TAG_ROUTERS) != NULL);
567 ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
569 TAG_SUBNETMASK) != NULL);
574 bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
577 struct sockaddr_in *sin, dst;
581 int error, on, rcvflg, timo, len;
585 struct bootpc_ifcontext *ifctx;
593 * Create socket and set its recieve timeout.
595 error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td);
601 bzero(&sopt, sizeof(sopt));
602 sopt.sopt_level = SOL_SOCKET;
603 sopt.sopt_name = SO_RCVTIMEO;
605 sopt.sopt_valsize = sizeof tv;
607 error = sosetopt(so, &sopt);
615 sopt.sopt_name = SO_BROADCAST;
617 sopt.sopt_valsize = sizeof on;
619 error = sosetopt(so, &sopt);
628 sopt.sopt_name = SO_DONTROUTE;
630 sopt.sopt_valsize = sizeof on;
632 error = sosetopt(so, &sopt);
637 * Bind the local endpoint to a bootp client port.
641 sin->sin_port = htons(IPPORT_BOOTPC);
642 error = sobind(so, (struct sockaddr *)sin, td);
644 kprintf("bind failed\n");
649 * Setup socket address for the server.
653 sin->sin_addr.s_addr = INADDR_BROADCAST;
654 sin->sin_port = htons(IPPORT_BOOTPS);
657 * Send it, repeatedly, until a reply is received,
658 * but delay each re-send by an increasing amount.
659 * If the delay hits the maximum, start complaining.
668 for (ifctx = gctx->interfaces;
670 ifctx = ifctx->next) {
671 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
672 bootpc_tag(&gctx->tmptag, &ifctx->reply,
678 for (ifctx = gctx->interfaces;
680 ifctx = ifctx->next) {
681 ifctx->outstanding = 0;
682 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
686 if (bootpc_ifctx_isfailed(ifctx) != 0)
690 ifctx->outstanding = 1;
692 /* Proceed to next step in DHCP negotiation */
693 if ((ifctx->state == IF_DHCP_OFFERED &&
694 ifctx->dhcpquerytype != DHCP_REQUEST) ||
695 (ifctx->state == IF_DHCP_UNRESOLVED &&
696 ifctx->dhcpquerytype != DHCP_DISCOVER) ||
697 (ifctx->state == IF_BOOTP_UNRESOLVED &&
698 ifctx->dhcpquerytype != DHCP_NOMSG)) {
700 bootpc_compose_query(ifctx, gctx, td);
703 /* Send BOOTP request (or re-send). */
705 if (ifctx->sentmsg == 0) {
706 switch(ifctx->dhcpquerytype) {
718 hexncpy((u_char *)LLADDR(ifctx->sdl),
719 ifctx->sdl->sdl_alen, hexstr,
720 HEX_NCPYLEN(ifctx->sdl->sdl_alen), ":");
721 kprintf("Sending %s packet from "
722 "interface %s (%s)\n",
723 s, ifctx->ireq.ifr_name,
728 aio.iov_base = (caddr_t) &ifctx->call;
729 aio.iov_len = sizeof(ifctx->call);
733 auio.uio_segflg = UIO_SYSSPACE;
734 auio.uio_rw = UIO_WRITE;
736 auio.uio_resid = sizeof(ifctx->call);
739 /* Set netmask to 0.0.0.0 */
741 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
743 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
744 (caddr_t) &ifctx->ireq, proc0.p_ucred);
747 "set if netmask, error=%d",
750 error = sosend(so, (struct sockaddr *) &dst,
751 &auio, NULL, NULL, 0, td);
753 kprintf("bootpc_call: sosend: %d state %08x\n",
754 error, (int) so->so_state);
757 /* XXX: Is this needed ? */
758 tsleep(&error, 0, "bootpw", 10);
760 /* Set netmask to 255.0.0.0 */
762 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
764 sin->sin_addr.s_addr = htonl(0xff000000u);
765 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
766 (caddr_t) &ifctx->ireq, proc0.p_ucred);
769 "set if netmask, error=%d",
774 if (outstanding == 0 &&
775 (rtimo == 0 || time_uptime >= rtimo)) {
780 /* Determine new timeout. */
781 if (timo < MAX_RESEND_DELAY)
784 kprintf("DHCP/BOOTP timeout for server ");
785 print_sin_addr(&dst);
790 * Wait for up to timo seconds for a reply.
791 * The socket receive timeout was set to 1 second.
793 atimo = timo + time_uptime;
794 while (time_uptime < atimo) {
795 aio.iov_base = (caddr_t) &gctx->reply;
796 aio.iov_len = sizeof(gctx->reply);
800 auio.uio_segflg = UIO_SYSSPACE;
801 auio.uio_rw = UIO_READ;
803 auio.uio_resid = sizeof(gctx->reply);
807 error = soreceive(so, NULL, &auio,
808 NULL, NULL, &rcvflg);
809 gctx->secs = time_uptime - gctx->starttime;
810 for (ifctx = gctx->interfaces;
812 ifctx = ifctx->next) {
813 if (bootpc_ifctx_isresolved(ifctx) != 0 ||
814 bootpc_ifctx_isfailed(ifctx) != 0)
817 ifctx->call.secs = htons(gctx->secs);
819 if (error == EWOULDBLOCK)
823 len = sizeof(gctx->reply) - auio.uio_resid;
825 /* Do we have the required number of bytes ? */
826 if (len < BOOTP_MIN_LEN)
828 gctx->replylen = len;
831 if (gctx->reply.op != BOOTP_REPLY)
834 /* Is this an answer to our query */
835 for (ifctx = gctx->interfaces;
837 ifctx = ifctx->next) {
838 if (gctx->reply.xid != ifctx->call.xid)
841 /* Same HW address size ? */
842 if (gctx->reply.hlen != ifctx->call.hlen)
845 /* Correct HW address ? */
846 if (bcmp(gctx->reply.chaddr,
848 ifctx->call.hlen) != 0)
855 s = bootpc_tag(&gctx->tmptag,
868 s = "DHCP (unexpected)";
874 kprintf("Received %s packet"
877 ifctx->ireq.ifr_name);
878 print_in_addr(gctx->reply.siaddr);
879 if (gctx->reply.giaddr.s_addr !=
882 print_in_addr(gctx->reply.giaddr);
884 if (bootpc_received(gctx, ifctx) != 0) {
885 kprintf(" (accepted)");
886 if (ifctx->outstanding) {
887 ifctx->outstanding = 0;
890 /* Network settle delay */
891 if (outstanding == 0)
892 atimo = time_uptime +
895 kprintf(" (ignored)");
896 if (ifctx->gotrootpath) {
898 rtimo = time_uptime +
900 kprintf(" (got root path)");
902 kprintf(" (no root path)");
907 if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
910 /* Force a retry if halfway in DHCP negotiation */
912 for (ifctx = gctx->interfaces; ifctx != NULL;
913 ifctx = ifctx->next) {
914 if (ifctx->state == IF_DHCP_OFFERED) {
915 if (ifctx->dhcpquerytype == DHCP_DISCOVER)
918 ifctx->state = IF_DHCP_UNRESOLVED;
925 if (gotrootpath != 0) {
926 gctx->gotrootpath = gotrootpath;
927 if (rtimo != 0 && time_uptime >= rtimo)
930 } /* forever send/receive */
933 * XXX: These are errors of varying seriousness being silently
937 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
938 if (bootpc_ifctx_isresolved(ifctx) == 0) {
939 kprintf("%s timeout for interface %s\n",
940 ifctx->dhcpquerytype != DHCP_NOMSG ?
942 ifctx->ireq.ifr_name);
945 if (gctx->gotrootpath != 0) {
947 kprintf("Got a root path, ignoring remaining timeout\n");
952 #ifndef BOOTP_NFSROOT
953 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
954 if (bootpc_ifctx_isresolved(ifctx) != 0) {
965 soclose(so, FNONBLOCK);
971 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
972 struct bootpc_globalcontext *gctx,
975 struct ifaddr_container *ifac;
976 struct sockaddr_in *sin;
981 struct sockaddr_dl *sdl;
983 error = socreate(AF_INET, &ifctx->so, SOCK_DGRAM, 0, td);
985 panic("nfs_boot: socreate, error=%d", error);
991 * Bring up the interface.
993 * Get the old interface flags and or IFF_UP into them; if
994 * IFF_UP set blindly, interface selection can be clobbered.
996 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, proc0.p_ucred);
998 panic("bootpc_fakeup_interface: GIFFLAGS, error=%d", error);
999 ireq->ifr_flags |= IFF_UP;
1000 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, proc0.p_ucred);
1002 panic("bootpc_fakeup_interface: SIFFLAGS, error=%d", error);
1005 * Do enough of ifconfig(8) so that the chosen interface
1006 * can talk to the servers. (just set the address)
1009 /* addr is 0.0.0.0 */
1011 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1013 error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, proc0.p_ucred);
1014 if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1015 panic("bootpc_fakeup_interface: "
1016 "set if addr, error=%d", error);
1018 /* netmask is 255.0.0.0 */
1020 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1022 sin->sin_addr.s_addr = htonl(0xff000000u);
1023 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t)ireq, proc0.p_ucred);
1025 panic("bootpc_fakeup_interface: set if netmask, error=%d",
1028 /* Broadcast is 255.255.255.255 */
1030 sin = (struct sockaddr_in *)&ireq->ifr_addr;
1032 clear_sinaddr(&ifctx->broadcast);
1033 sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
1034 ifctx->broadcast.sin_addr.s_addr = sin->sin_addr.s_addr;
1036 error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t)ireq, proc0.p_ucred);
1037 if (error != 0 && error != EADDRNOTAVAIL)
1038 panic("bootpc_fakeup_interface: "
1039 "set if broadcast addr, error=%d",
1043 /* Get HW address */
1046 TAILQ_FOREACH(ifac, &ifctx->ifp->if_addrheads[mycpuid], ifa_link) {
1047 struct ifaddr *ifa = ifac->ifa;
1049 if (ifa->ifa_addr->sa_family == AF_LINK &&
1050 (sdl = ((struct sockaddr_dl *) ifa->ifa_addr)) != NULL &&
1051 sdl->sdl_type == IFT_ETHER)
1056 panic("bootpc: Unable to find HW address for %s",
1057 ifctx->ireq.ifr_name);
1065 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
1066 struct bootpc_globalcontext *gctx,
1070 struct sockaddr_in defdst;
1071 struct sockaddr_in defmask;
1072 struct sockaddr_in *sin;
1076 struct sockaddr_in *myaddr;
1077 struct sockaddr_in *netmask;
1078 struct sockaddr_in *gw;
1080 ireq = &ifctx->ireq;
1082 myaddr = &ifctx->myaddr;
1083 netmask = &ifctx->netmask;
1086 if (bootpc_ifctx_isresolved(ifctx) == 0) {
1088 /* Shutdown interfaces where BOOTP failed */
1090 kprintf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
1091 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, proc0.p_ucred);
1093 panic("bootpc_adjust_interface: "
1094 "SIOCGIFFLAGS, error=%d", error);
1095 ireq->ifr_flags &= ~IFF_UP;
1096 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, proc0.p_ucred);
1098 panic("bootpc_adjust_interface: "
1099 "SIOCSIFFLAGS, error=%d", error);
1101 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1103 error = ifioctl(so, SIOCDIFADDR, (caddr_t) ireq, proc0.p_ucred);
1104 if (error != 0 && (error != EADDRNOTAVAIL ||
1105 ifctx == gctx->interfaces))
1106 panic("bootpc_adjust_interface: "
1107 "SIOCDIFADDR, error=%d", error);
1112 kprintf("Adjusted interface %s\n", ifctx->ireq.ifr_name);
1114 * Do enough of ifconfig(8) so that the chosen interface
1115 * can talk to the servers. (just set the address)
1117 bcopy(netmask, &ireq->ifr_addr, sizeof(*netmask));
1118 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t) ireq, proc0.p_ucred);
1120 panic("bootpc_adjust_interface: "
1121 "set if netmask, error=%d", error);
1123 /* Broadcast is with host part of IP address all 1's */
1125 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1127 sin->sin_addr.s_addr = myaddr->sin_addr.s_addr |
1128 ~ netmask->sin_addr.s_addr;
1129 error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t) ireq, proc0.p_ucred);
1131 panic("bootpc_adjust_interface: "
1132 "set if broadcast addr, error=%d", error);
1134 bcopy(myaddr, &ireq->ifr_addr, sizeof(*myaddr));
1135 error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, proc0.p_ucred);
1136 if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1137 panic("bootpc_adjust_interface: "
1138 "set if addr, error=%d", error);
1140 /* Add new default route */
1142 if (ifctx->gotgw != 0 || gctx->gotgw == 0) {
1143 clear_sinaddr(&defdst);
1144 clear_sinaddr(&defmask);
1145 error = rtrequest_global(RTM_ADD, (struct sockaddr *) &defdst,
1146 (struct sockaddr *) gw,
1147 (struct sockaddr *) &defmask,
1148 (RTF_UP | RTF_GATEWAY | RTF_STATIC));
1150 kprintf("bootpc_adjust_interface: "
1151 "add net route, error=%d\n", error);
1160 print_sin_addr(struct sockaddr_in *sin)
1162 print_in_addr(sin->sin_addr);
1167 print_in_addr(struct in_addr addr)
1171 ip = ntohl(addr.s_addr);
1172 kprintf("%d.%d.%d.%d",
1173 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1177 bootpc_compose_query(struct bootpc_ifcontext *ifctx,
1178 struct bootpc_globalcontext *gctx, struct thread *td)
1180 unsigned char *vendp;
1181 unsigned char vendor_client[64];
1183 uint8_t vendor_client_len;
1185 ifctx->gotrootpath = 0;
1187 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1190 ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */
1191 ifctx->call.htype = 1; /* 10mb ethernet */
1192 ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1193 ifctx->call.hops = 0;
1194 if (bootpc_ifctx_isunresolved(ifctx) != 0)
1196 ifctx->call.xid = txdr_unsigned(ifctx->xid);
1197 bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1199 vendp = ifctx->call.vend;
1200 *vendp++ = 99; /* RFC1048 cookie */
1204 *vendp++ = TAG_MAXMSGSIZE;
1206 *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1207 *vendp++ = sizeof(struct bootp_packet) & 255;
1209 ksnprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1210 ostype, MACHINE, osrelease);
1211 vendor_client_len = strlen(vendor_client);
1212 *vendp++ = TAG_VENDOR_INDENTIFIER;
1213 *vendp++ = vendor_client_len;
1214 memcpy(vendp, vendor_client, vendor_client_len);
1215 vendp += vendor_client_len;
1216 ifctx->dhcpquerytype = DHCP_NOMSG;
1217 switch (ifctx->state) {
1218 case IF_DHCP_UNRESOLVED:
1219 *vendp++ = TAG_DHCP_MSGTYPE;
1221 *vendp++ = DHCP_DISCOVER;
1222 ifctx->dhcpquerytype = DHCP_DISCOVER;
1223 ifctx->gotdhcpserver = 0;
1225 case IF_DHCP_OFFERED:
1226 *vendp++ = TAG_DHCP_MSGTYPE;
1228 *vendp++ = DHCP_REQUEST;
1229 ifctx->dhcpquerytype = DHCP_REQUEST;
1230 *vendp++ = TAG_DHCP_REQ_ADDR;
1232 memcpy(vendp, &ifctx->reply.yiaddr, 4);
1234 if (ifctx->gotdhcpserver != 0) {
1235 *vendp++ = TAG_DHCP_SERVERID;
1237 memcpy(vendp, &ifctx->dhcpserver, 4);
1240 *vendp++ = TAG_DHCP_LEASETIME;
1242 leasetime = htonl(300);
1243 memcpy(vendp, &leasetime, 4);
1250 ifctx->call.secs = 0;
1251 ifctx->call.flags = htons(0x8000); /* We need an broadcast answer */
1256 bootpc_hascookie(struct bootp_packet *bp)
1258 return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1259 bp->vend[2] == 83 && bp->vend[3] == 99);
1264 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1265 unsigned char *start, int len, int tag)
1271 if (tctx->badtag != 0 || tctx->badopt != 0)
1279 if (code == TAG_PAD)
1281 if (code == TAG_END)
1283 if (j >= ej || j + *j + 1 > ej) {
1289 if (tctx->taglen + len > TAG_MAXLEN) {
1295 memcpy(tctx->buf + tctx->taglen,
1297 tctx->taglen += len;
1299 if (code == TAG_OVERLOAD)
1300 tctx->overload = *j;
1307 static unsigned char *
1308 bootpc_tag(struct bootpc_tagcontext *tctx, struct bootp_packet *bp,
1317 if (bootpc_hascookie(bp) == 0)
1320 bootpc_tag_helper(tctx, &bp->vend[4],
1321 (unsigned char *) bp + len - &bp->vend[4], tag);
1323 if ((tctx->overload & OVERLOAD_FILE) != 0)
1324 bootpc_tag_helper(tctx,
1325 (unsigned char *) bp->file,
1328 if ((tctx->overload & OVERLOAD_SNAME) != 0)
1329 bootpc_tag_helper(tctx,
1330 (unsigned char *) bp->sname,
1334 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1336 tctx->buf[tctx->taglen] = '\0';
1342 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1343 struct bootpc_globalcontext *gctx)
1349 ifctx->gotnetmask = 0;
1351 clear_sinaddr(&ifctx->myaddr);
1352 clear_sinaddr(&ifctx->netmask);
1353 clear_sinaddr(&ifctx->gw);
1355 ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1357 ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1358 ksnprintf(gctx->lookup_path, sizeof(gctx->lookup_path),
1360 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1362 kprintf("%s at ", ifctx->ireq.ifr_name);
1363 print_sin_addr(&ifctx->myaddr);
1364 kprintf(" server ");
1365 print_in_addr(ifctx->reply.siaddr);
1367 ifctx->gw.sin_addr = ifctx->reply.giaddr;
1368 if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1369 kprintf(" via gateway ");
1370 print_in_addr(ifctx->reply.giaddr);
1373 /* This call used for the side effect (overload flag) */
1374 (void) bootpc_tag(&gctx->tmptag,
1375 &ifctx->reply, ifctx->replylen, TAG_END);
1377 if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1378 if (ifctx->reply.sname[0] != '\0')
1379 kprintf(" server name %s", ifctx->reply.sname);
1380 if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1381 if (ifctx->reply.file[0] != '\0')
1382 kprintf(" boot file %s", ifctx->reply.file);
1386 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1389 if (gctx->tag.taglen != 4)
1390 panic("bootpc: subnet mask len is %d",
1392 bcopy(p, &ifctx->netmask.sin_addr, 4);
1393 ifctx->gotnetmask = 1;
1394 kprintf("subnet mask ");
1395 print_sin_addr(&ifctx->netmask);
1399 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1403 if (gctx->tag.taglen % 4)
1404 panic("bootpc: Router Len is %d", gctx->tag.taglen);
1405 if (gctx->tag.taglen > 0) {
1406 bcopy(p, &ifctx->gw.sin_addr, 4);
1408 print_sin_addr(&ifctx->gw);
1415 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1418 if (gctx->setrootfs != NULL) {
1419 kprintf("rootfs %s (ignored) ", p);
1420 } else if (setfs(&nd->root_saddr,
1421 nd->root_hostnam, p)) {
1422 kprintf("rootfs %s ",p);
1423 gctx->gotrootpath = 1;
1424 ifctx->gotrootpath = 1;
1425 gctx->setrootfs = ifctx;
1427 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1431 nfs_mountopts(&nd->root_args, p);
1432 kprintf("rootopts %s ", p);
1435 panic("Failed to set rootfs to %s",p);
1438 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1441 if (gctx->setswapfs != NULL) {
1442 kprintf("swapfs %s (ignored) ", p);
1443 } else if (setfs(&nd->swap_saddr,
1444 nd->swap_hostnam, p)) {
1445 gctx->gotswappath = 1;
1446 gctx->setswapfs = ifctx;
1447 kprintf("swapfs %s ", p);
1449 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1453 /* swap mount options */
1454 nfs_mountopts(&nd->swap_args, p);
1455 kprintf("swapopts %s ", p);
1458 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1463 if (gctx->tag.taglen != 4)
1465 "Expected 4 bytes for swaplen, "
1468 bcopy(p, &swaplen, 4);
1469 nd->swap_nblks = ntohl(swaplen);
1470 kprintf("swapsize %d KB ",
1474 panic("Failed to set swapfs to %s", p);
1477 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1480 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1481 panic("bootpc: hostname >= %d bytes",
1483 if (gctx->sethostname != NULL) {
1484 kprintf("hostname %s (ignored) ", p);
1486 strcpy(nd->my_hostnam, p);
1487 strcpy(hostname, p);
1488 kprintf("hostname %s ",hostname);
1489 gctx->sethostname = ifctx;
1492 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1494 if (p != NULL) { /* store in a sysctl variable */
1495 int i, l = sizeof(bootp_cookie) - 1;
1496 for (i = 0; i < l && p[i] != '\0'; i++)
1497 bootp_cookie[i] = p[i];
1503 if (ifctx->gotnetmask == 0) {
1504 if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1505 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1506 else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1507 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1509 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1511 if (ifctx->gotgw == 0) {
1513 ifctx->gw.sin_addr.s_addr = ifctx->myaddr.sin_addr.s_addr;
1520 struct bootpc_ifcontext *ifctx, *nctx; /* Interface BOOTP contexts */
1521 struct bootpc_globalcontext *gctx; /* Global BOOTP context */
1524 struct nfsv3_diskless *nd;
1527 nd = &nfsv3_diskless;
1531 * If already filled in, don't touch it here
1533 if (nfs_diskless_valid != 0)
1536 gctx = kmalloc(sizeof(*gctx), M_TEMP, M_WAITOK | M_ZERO);
1538 gctx->xid = ~0xFFFF;
1539 gctx->starttime = time_uptime;
1541 ifctx = allocifctx(gctx);
1544 * Find a network interface.
1546 #ifdef BOOTP_WIRED_TO
1547 kprintf("bootpc_init: wired to interface '%s'\n",
1548 __XSTRING(BOOTP_WIRED_TO));
1550 bzero(&ifctx->ireq, sizeof(ifctx->ireq));
1551 /* XXX ALMOST MPSAFE */
1553 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
1554 strlcpy(ifctx->ireq.ifr_name, ifp->if_xname,
1555 sizeof(ifctx->ireq.ifr_name));
1556 #ifdef BOOTP_WIRED_TO
1557 if (strcmp(ifctx->ireq.ifr_name,
1558 __XSTRING(BOOTP_WIRED_TO)) != 0)
1561 if ((ifp->if_flags &
1562 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1566 if (gctx->interfaces != NULL)
1567 gctx->lastinterface->next = ifctx;
1569 gctx->interfaces = ifctx;
1571 gctx->lastinterface = ifctx;
1572 ifctx = allocifctx(gctx);
1575 kfree(ifctx, M_TEMP);
1577 if (gctx->interfaces == NULL) {
1578 #ifdef BOOTP_WIRED_TO
1579 panic("bootpc_init: Could not find interface specified "
1580 "by BOOTP_WIRED_TO: "
1581 __XSTRING(BOOTP_WIRED_TO));
1583 panic("bootpc_init: no suitable interface");
1587 gctx->gotrootpath = 0;
1588 gctx->gotswappath = 0;
1591 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1592 bootpc_fakeup_interface(ifctx, gctx, td);
1594 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1595 bootpc_compose_query(ifctx, gctx, td);
1597 ifctx = gctx->interfaces;
1598 error = bootpc_call(gctx, td);
1601 #ifdef BOOTP_NFSROOT
1602 panic("BOOTP call failed");
1604 kprintf("BOOTP call failed\n");
1608 nfs_mountopts(&nd->root_args, NULL);
1610 nfs_mountopts(&nd->swap_args, NULL);
1612 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1613 if (bootpc_ifctx_isresolved(ifctx) != 0)
1614 bootpc_decode_reply(nd, ifctx, gctx);
1616 if (gctx->gotswappath == 0)
1618 #ifdef BOOTP_NFSROOT
1619 if (gctx->gotrootpath == 0)
1620 panic("bootpc: No root path offered");
1623 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
1624 bootpc_adjust_interface(ifctx, gctx, td);
1626 soclose(ifctx->so, FNONBLOCK);
1629 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1630 if (ifctx->gotrootpath != 0)
1632 if (ifctx == NULL) {
1633 for (ifctx = gctx->interfaces;
1635 ifctx = ifctx->next)
1636 if (bootpc_ifctx_isresolved(ifctx) != 0)
1642 if (gctx->gotrootpath != 0) {
1644 error = md_mount(&nd->root_saddr, nd->root_hostnam,
1645 nd->root_fh, &nd->root_fhsize,
1646 &nd->root_args, td);
1648 panic("nfs_boot: mountd root, error=%d", error);
1650 if (gctx->gotswappath != 0) {
1652 error = md_mount(&nd->swap_saddr,
1654 nd->swap_fh, &nd->swap_fhsize,
1655 &nd->swap_args, td);
1657 panic("nfs_boot: mountd swap, error=%d",
1660 error = md_lookup_swap(&nd->swap_saddr,
1662 nd->swap_fh, &nd->swap_fhsize,
1663 &nd->swap_args, td);
1665 panic("nfs_boot: lookup swap, error=%d",
1668 nfs_diskless_valid = 3;
1671 strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1672 bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1673 bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1674 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1675 ifctx->myaddr.sin_addr.s_addr |
1676 ~ ifctx->netmask.sin_addr.s_addr;
1677 bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1680 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = nctx) {
1682 kfree(ifctx, M_TEMP);
1684 kfree(gctx, M_TEMP);