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.25 2008/03/07 11:34:21 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>
58 #include <sys/fcntl.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 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 kprintf("(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 kprintf("link %.*s ", sli->sdl_nlen, sli->sdl_data);
310 for (i = 0; i < sli->sdl_alen; i++) {
313 kprintf("%x", ((unsigned char *) LLADDR(sli))[i]);
318 kprintf("af%d", sa->sa_family);
324 bootpboot_p_ma(struct sockaddr *ma)
330 kprintf("%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 kprintf("flags %x", (unsigned short) rt->rt_flags);
344 kprintf(" %d", (int) rt->rt_rmx.rmx_expire);
345 kprintf(" %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 kprintf("Routing table:\n");
372 bootpboot_p_tree(rt_tables[AF_INET]->rnh_treetop);
377 bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
379 kprintf("%s flags %x, addr ",
381 (unsigned short) ifp->if_flags);
382 print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
383 kprintf(", broadcast ");
384 print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
385 kprintf(", netmask ");
386 print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
392 bootpboot_p_iflist(void)
397 kprintf("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 *) kmalloc(sizeof(*ifctx),
424 bzero(ifctx, sizeof(*ifctx));
425 ifctx->xid = gctx->xid;
427 ifctx->state = IF_BOOTP_UNRESOLVED;
429 ifctx->state = IF_DHCP_UNRESOLVED;
437 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
439 if (ifctx->state == IF_BOOTP_RESOLVED ||
440 ifctx->state == IF_DHCP_RESOLVED)
447 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
449 if (ifctx->state == IF_BOOTP_UNRESOLVED ||
450 ifctx->state == IF_DHCP_UNRESOLVED)
457 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
459 if (ifctx->state == IF_BOOTP_FAILED ||
460 ifctx->state == IF_DHCP_FAILED)
467 bootpc_received(struct bootpc_globalcontext *gctx,
468 struct bootpc_ifcontext *ifctx)
470 unsigned char dhcpreplytype;
473 * Need timeout for fallback to less
474 * desirable alternative.
478 /* This call used for the side effect (badopt flag) */
479 (void) bootpc_tag(&gctx->tmptag, &gctx->reply,
483 /* If packet is invalid, ignore it */
484 if (gctx->tmptag.badopt != 0)
487 p = bootpc_tag(&gctx->tmptag, &gctx->reply,
488 gctx->replylen, TAG_DHCP_MSGTYPE);
492 dhcpreplytype = DHCP_NOMSG;
494 switch (ifctx->dhcpquerytype) {
496 if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */
497 #ifndef BOOTP_FORCE_DHCP
498 && dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */
504 if (dhcpreplytype != DHCP_ACK)
512 /* Ignore packet unless it gives us a root tag we didn't have */
514 if ((ifctx->state == IF_BOOTP_RESOLVED ||
515 (ifctx->dhcpquerytype == DHCP_DISCOVER &&
516 (ifctx->state == IF_DHCP_OFFERED ||
517 ifctx->state == IF_DHCP_RESOLVED))) &&
518 (bootpc_tag(&gctx->tmptag, &ifctx->reply,
521 bootpc_tag(&gctx->tmptag, &gctx->reply,
529 ifctx->replylen = gctx->replylen;
531 /* XXX: Only reset if 'perfect' response */
532 if (ifctx->state == IF_BOOTP_UNRESOLVED)
533 ifctx->state = IF_BOOTP_RESOLVED;
534 else if (ifctx->state == IF_DHCP_UNRESOLVED &&
535 ifctx->dhcpquerytype == DHCP_DISCOVER) {
536 if (dhcpreplytype == DHCP_OFFER)
537 ifctx->state = IF_DHCP_OFFERED;
539 ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */
540 } else if (ifctx->state == IF_DHCP_OFFERED &&
541 ifctx->dhcpquerytype == DHCP_REQUEST)
542 ifctx->state = IF_DHCP_RESOLVED;
545 if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
546 ifctx->state != IF_BOOTP_RESOLVED) {
547 p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
548 ifctx->replylen, TAG_DHCP_SERVERID);
549 if (p != NULL && gctx->tmptag.taglen == 4) {
550 memcpy(&ifctx->dhcpserver, p, 4);
551 ifctx->gotdhcpserver = 1;
553 ifctx->gotdhcpserver = 0;
557 ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
560 ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
562 TAG_ROUTERS) != NULL);
563 ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
565 TAG_SUBNETMASK) != NULL);
570 bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
573 struct sockaddr_in *sin, dst;
577 int error, on, rcvflg, timo, len;
581 struct bootpc_ifcontext *ifctx;
588 * Create socket and set its recieve timeout.
590 error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td);
596 bzero(&sopt, sizeof(sopt));
597 sopt.sopt_level = SOL_SOCKET;
598 sopt.sopt_name = SO_RCVTIMEO;
600 sopt.sopt_valsize = sizeof tv;
602 error = sosetopt(so, &sopt);
610 sopt.sopt_name = SO_BROADCAST;
612 sopt.sopt_valsize = sizeof on;
614 error = sosetopt(so, &sopt);
623 sopt.sopt_name = SO_DONTROUTE;
625 sopt.sopt_valsize = sizeof on;
627 error = sosetopt(so, &sopt);
632 * Bind the local endpoint to a bootp client port.
636 sin->sin_port = htons(IPPORT_BOOTPC);
637 error = sobind(so, (struct sockaddr *)sin, td);
639 kprintf("bind failed\n");
644 * Setup socket address for the server.
648 sin->sin_addr.s_addr = INADDR_BROADCAST;
649 sin->sin_port = htons(IPPORT_BOOTPS);
652 * Send it, repeatedly, until a reply is received,
653 * but delay each re-send by an increasing amount.
654 * If the delay hits the maximum, start complaining.
663 for (ifctx = gctx->interfaces;
665 ifctx = ifctx->next) {
666 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
667 bootpc_tag(&gctx->tmptag, &ifctx->reply,
673 for (ifctx = gctx->interfaces;
675 ifctx = ifctx->next) {
676 ifctx->outstanding = 0;
677 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
681 if (bootpc_ifctx_isfailed(ifctx) != 0)
685 ifctx->outstanding = 1;
687 /* Proceed to next step in DHCP negotiation */
688 if ((ifctx->state == IF_DHCP_OFFERED &&
689 ifctx->dhcpquerytype != DHCP_REQUEST) ||
690 (ifctx->state == IF_DHCP_UNRESOLVED &&
691 ifctx->dhcpquerytype != DHCP_DISCOVER) ||
692 (ifctx->state == IF_BOOTP_UNRESOLVED &&
693 ifctx->dhcpquerytype != DHCP_NOMSG)) {
695 bootpc_compose_query(ifctx, gctx, td);
698 /* Send BOOTP request (or re-send). */
700 if (ifctx->sentmsg == 0) {
701 switch(ifctx->dhcpquerytype) {
713 kprintf("Sending %s packet from "
714 "interface %s (%*D)\n",
716 ifctx->ireq.ifr_name,
717 ifctx->sdl->sdl_alen,
718 (unsigned char *) LLADDR(ifctx->sdl),
723 aio.iov_base = (caddr_t) &ifctx->call;
724 aio.iov_len = sizeof(ifctx->call);
728 auio.uio_segflg = UIO_SYSSPACE;
729 auio.uio_rw = UIO_WRITE;
731 auio.uio_resid = sizeof(ifctx->call);
734 /* Set netmask to 0.0.0.0 */
736 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
738 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
739 (caddr_t) &ifctx->ireq, proc0.p_ucred);
742 "set if netmask, error=%d",
745 error = sosend(so, (struct sockaddr *) &dst,
746 &auio, NULL, NULL, 0, td);
748 kprintf("bootpc_call: sosend: %d state %08x\n",
749 error, (int) so->so_state);
752 /* XXX: Is this needed ? */
753 tsleep(&error, 0, "bootpw", 10);
755 /* Set netmask to 255.0.0.0 */
757 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
759 sin->sin_addr.s_addr = htonl(0xff000000u);
760 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
761 (caddr_t) &ifctx->ireq, proc0.p_ucred);
764 "set if netmask, error=%d",
769 if (outstanding == 0 &&
770 (rtimo == 0 || time_second >= rtimo)) {
775 /* Determine new timeout. */
776 if (timo < MAX_RESEND_DELAY)
779 kprintf("DHCP/BOOTP timeout for server ");
780 print_sin_addr(&dst);
785 * Wait for up to timo seconds for a reply.
786 * The socket receive timeout was set to 1 second.
788 atimo = timo + time_second;
789 while (time_second < atimo) {
790 aio.iov_base = (caddr_t) &gctx->reply;
791 aio.iov_len = sizeof(gctx->reply);
795 auio.uio_segflg = UIO_SYSSPACE;
796 auio.uio_rw = UIO_READ;
798 auio.uio_resid = sizeof(gctx->reply);
802 error = soreceive(so, NULL, &auio,
803 NULL, NULL, &rcvflg);
804 gctx->secs = time_second - gctx->starttime;
805 for (ifctx = gctx->interfaces;
807 ifctx = ifctx->next) {
808 if (bootpc_ifctx_isresolved(ifctx) != 0 ||
809 bootpc_ifctx_isfailed(ifctx) != 0)
812 ifctx->call.secs = htons(gctx->secs);
814 if (error == EWOULDBLOCK)
818 len = sizeof(gctx->reply) - auio.uio_resid;
820 /* Do we have the required number of bytes ? */
821 if (len < BOOTP_MIN_LEN)
823 gctx->replylen = len;
826 if (gctx->reply.op != BOOTP_REPLY)
829 /* Is this an answer to our query */
830 for (ifctx = gctx->interfaces;
832 ifctx = ifctx->next) {
833 if (gctx->reply.xid != ifctx->call.xid)
836 /* Same HW address size ? */
837 if (gctx->reply.hlen != ifctx->call.hlen)
840 /* Correct HW address ? */
841 if (bcmp(gctx->reply.chaddr,
843 ifctx->call.hlen) != 0)
850 s = bootpc_tag(&gctx->tmptag,
863 s = "DHCP (unexpected)";
869 kprintf("Received %s packet"
872 ifctx->ireq.ifr_name);
873 print_in_addr(gctx->reply.siaddr);
874 if (gctx->reply.giaddr.s_addr !=
877 print_in_addr(gctx->reply.giaddr);
879 if (bootpc_received(gctx, ifctx) != 0) {
880 kprintf(" (accepted)");
881 if (ifctx->outstanding) {
882 ifctx->outstanding = 0;
885 /* Network settle delay */
886 if (outstanding == 0)
887 atimo = time_second +
890 kprintf(" (ignored)");
891 if (ifctx->gotrootpath) {
893 rtimo = time_second +
895 kprintf(" (got root path)");
897 kprintf(" (no root path)");
902 if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
905 /* Force a retry if halfway in DHCP negotiation */
907 for (ifctx = gctx->interfaces; ifctx != NULL;
908 ifctx = ifctx->next) {
909 if (ifctx->state == IF_DHCP_OFFERED) {
910 if (ifctx->dhcpquerytype == DHCP_DISCOVER)
913 ifctx->state = IF_DHCP_UNRESOLVED;
920 if (gotrootpath != 0) {
921 gctx->gotrootpath = gotrootpath;
922 if (rtimo != 0 && time_second >= rtimo)
925 } /* forever send/receive */
928 * XXX: These are errors of varying seriousness being silently
932 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
933 if (bootpc_ifctx_isresolved(ifctx) == 0) {
934 kprintf("%s timeout for interface %s\n",
935 ifctx->dhcpquerytype != DHCP_NOMSG ?
937 ifctx->ireq.ifr_name);
940 if (gctx->gotrootpath != 0) {
942 kprintf("Got a root path, ignoring remaining timeout\n");
947 #ifndef BOOTP_NFSROOT
948 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
949 if (bootpc_ifctx_isresolved(ifctx) != 0) {
960 soclose(so, FNONBLOCK);
966 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
967 struct bootpc_globalcontext *gctx,
970 struct ifaddr_container *ifac;
971 struct sockaddr_in *sin;
976 struct sockaddr_dl *sdl;
978 error = socreate(AF_INET, &ifctx->so, SOCK_DGRAM, 0, td);
980 panic("nfs_boot: socreate, error=%d", error);
986 * Bring up the interface.
988 * Get the old interface flags and or IFF_UP into them; if
989 * IFF_UP set blindly, interface selection can be clobbered.
991 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, proc0.p_ucred);
993 panic("bootpc_fakeup_interface: GIFFLAGS, error=%d", error);
994 ireq->ifr_flags |= IFF_UP;
995 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, proc0.p_ucred);
997 panic("bootpc_fakeup_interface: SIFFLAGS, error=%d", error);
1000 * Do enough of ifconfig(8) so that the chosen interface
1001 * can talk to the servers. (just set the address)
1004 /* addr is 0.0.0.0 */
1006 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1008 error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, proc0.p_ucred);
1009 if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1010 panic("bootpc_fakeup_interface: "
1011 "set if addr, error=%d", error);
1013 /* netmask is 255.0.0.0 */
1015 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1017 sin->sin_addr.s_addr = htonl(0xff000000u);
1018 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t)ireq, proc0.p_ucred);
1020 panic("bootpc_fakeup_interface: set if netmask, error=%d",
1023 /* Broadcast is 255.255.255.255 */
1025 sin = (struct sockaddr_in *)&ireq->ifr_addr;
1027 clear_sinaddr(&ifctx->broadcast);
1028 sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
1029 ifctx->broadcast.sin_addr.s_addr = sin->sin_addr.s_addr;
1031 error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t)ireq, proc0.p_ucred);
1032 if (error != 0 && error != EADDRNOTAVAIL)
1033 panic("bootpc_fakeup_interface: "
1034 "set if broadcast addr, error=%d",
1038 /* Get HW address */
1041 TAILQ_FOREACH(ifac, &ifctx->ifp->if_addrheads[mycpuid], ifa_link) {
1042 struct ifaddr *ifa = ifac->ifa;
1044 if (ifa->ifa_addr->sa_family == AF_LINK &&
1045 (sdl = ((struct sockaddr_dl *) ifa->ifa_addr)) != NULL &&
1046 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 kprintf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
1086 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, proc0.p_ucred);
1088 panic("bootpc_adjust_interface: "
1089 "SIOCGIFFLAGS, error=%d", error);
1090 ireq->ifr_flags &= ~IFF_UP;
1091 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, proc0.p_ucred);
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, proc0.p_ucred);
1099 if (error != 0 && (error != EADDRNOTAVAIL ||
1100 ifctx == gctx->interfaces))
1101 panic("bootpc_adjust_interface: "
1102 "SIOCDIFADDR, error=%d", error);
1107 kprintf("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, proc0.p_ucred);
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, proc0.p_ucred);
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, proc0.p_ucred);
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_global(RTM_ADD, (struct sockaddr *) &defdst,
1141 (struct sockaddr *) gw,
1142 (struct sockaddr *) &defmask,
1143 (RTF_UP | RTF_GATEWAY | RTF_STATIC));
1145 kprintf("bootpc_adjust_interface: "
1146 "add net route, error=%d\n", error);
1155 print_sin_addr(struct sockaddr_in *sin)
1157 print_in_addr(sin->sin_addr);
1162 print_in_addr(struct in_addr addr)
1166 ip = ntohl(addr.s_addr);
1167 kprintf("%d.%d.%d.%d",
1168 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1172 bootpc_compose_query(struct bootpc_ifcontext *ifctx,
1173 struct bootpc_globalcontext *gctx, struct thread *td)
1175 unsigned char *vendp;
1176 unsigned char vendor_client[64];
1178 uint8_t vendor_client_len;
1180 ifctx->gotrootpath = 0;
1182 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1185 ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */
1186 ifctx->call.htype = 1; /* 10mb ethernet */
1187 ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1188 ifctx->call.hops = 0;
1189 if (bootpc_ifctx_isunresolved(ifctx) != 0)
1191 ifctx->call.xid = txdr_unsigned(ifctx->xid);
1192 bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1194 vendp = ifctx->call.vend;
1195 *vendp++ = 99; /* RFC1048 cookie */
1199 *vendp++ = TAG_MAXMSGSIZE;
1201 *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1202 *vendp++ = sizeof(struct bootp_packet) & 255;
1204 ksnprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1205 ostype, MACHINE, osrelease);
1206 vendor_client_len = strlen(vendor_client);
1207 *vendp++ = TAG_VENDOR_INDENTIFIER;
1208 *vendp++ = vendor_client_len;
1209 memcpy(vendp, vendor_client, vendor_client_len);
1210 vendp += vendor_client_len;
1211 ifctx->dhcpquerytype = DHCP_NOMSG;
1212 switch (ifctx->state) {
1213 case IF_DHCP_UNRESOLVED:
1214 *vendp++ = TAG_DHCP_MSGTYPE;
1216 *vendp++ = DHCP_DISCOVER;
1217 ifctx->dhcpquerytype = DHCP_DISCOVER;
1218 ifctx->gotdhcpserver = 0;
1220 case IF_DHCP_OFFERED:
1221 *vendp++ = TAG_DHCP_MSGTYPE;
1223 *vendp++ = DHCP_REQUEST;
1224 ifctx->dhcpquerytype = DHCP_REQUEST;
1225 *vendp++ = TAG_DHCP_REQ_ADDR;
1227 memcpy(vendp, &ifctx->reply.yiaddr, 4);
1229 if (ifctx->gotdhcpserver != 0) {
1230 *vendp++ = TAG_DHCP_SERVERID;
1232 memcpy(vendp, &ifctx->dhcpserver, 4);
1235 *vendp++ = TAG_DHCP_LEASETIME;
1237 leasetime = htonl(300);
1238 memcpy(vendp, &leasetime, 4);
1245 ifctx->call.secs = 0;
1246 ifctx->call.flags = htons(0x8000); /* We need an broadcast answer */
1251 bootpc_hascookie(struct bootp_packet *bp)
1253 return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1254 bp->vend[2] == 83 && bp->vend[3] == 99);
1259 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1260 unsigned char *start, int len, int tag)
1266 if (tctx->badtag != 0 || tctx->badopt != 0)
1274 if (code == TAG_PAD)
1276 if (code == TAG_END)
1278 if (j >= ej || j + *j + 1 > ej) {
1284 if (tctx->taglen + len > TAG_MAXLEN) {
1290 memcpy(tctx->buf + tctx->taglen,
1292 tctx->taglen += len;
1294 if (code == TAG_OVERLOAD)
1295 tctx->overload = *j;
1302 static unsigned char *
1303 bootpc_tag(struct bootpc_tagcontext *tctx, struct bootp_packet *bp,
1315 if (bootpc_hascookie(bp) == 0)
1319 ej = (unsigned char *) bp + len;
1321 bootpc_tag_helper(tctx, &bp->vend[4],
1322 (unsigned char *) bp + len - &bp->vend[4], tag);
1324 if ((tctx->overload & OVERLOAD_FILE) != 0)
1325 bootpc_tag_helper(tctx,
1326 (unsigned char *) bp->file,
1329 if ((tctx->overload & OVERLOAD_SNAME) != 0)
1330 bootpc_tag_helper(tctx,
1331 (unsigned char *) bp->sname,
1335 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1337 tctx->buf[tctx->taglen] = '\0';
1343 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1344 struct bootpc_globalcontext *gctx)
1350 ifctx->gotnetmask = 0;
1352 clear_sinaddr(&ifctx->myaddr);
1353 clear_sinaddr(&ifctx->netmask);
1354 clear_sinaddr(&ifctx->gw);
1356 ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1358 ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1359 ksnprintf(gctx->lookup_path, sizeof(gctx->lookup_path),
1361 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1363 kprintf("%s at ", ifctx->ireq.ifr_name);
1364 print_sin_addr(&ifctx->myaddr);
1365 kprintf(" server ");
1366 print_in_addr(ifctx->reply.siaddr);
1368 ifctx->gw.sin_addr = ifctx->reply.giaddr;
1369 if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1370 kprintf(" via gateway ");
1371 print_in_addr(ifctx->reply.giaddr);
1374 /* This call used for the side effect (overload flag) */
1375 (void) bootpc_tag(&gctx->tmptag,
1376 &ifctx->reply, ifctx->replylen, TAG_END);
1378 if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1379 if (ifctx->reply.sname[0] != '\0')
1380 kprintf(" server name %s", ifctx->reply.sname);
1381 if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1382 if (ifctx->reply.file[0] != '\0')
1383 kprintf(" boot file %s", ifctx->reply.file);
1387 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1390 if (gctx->tag.taglen != 4)
1391 panic("bootpc: subnet mask len is %d",
1393 bcopy(p, &ifctx->netmask.sin_addr, 4);
1394 ifctx->gotnetmask = 1;
1395 kprintf("subnet mask ");
1396 print_sin_addr(&ifctx->netmask);
1400 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1404 if (gctx->tag.taglen % 4)
1405 panic("bootpc: Router Len is %d", gctx->tag.taglen);
1406 if (gctx->tag.taglen > 0) {
1407 bcopy(p, &ifctx->gw.sin_addr, 4);
1409 print_sin_addr(&ifctx->gw);
1416 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1419 if (gctx->setrootfs != NULL) {
1420 kprintf("rootfs %s (ignored) ", p);
1421 } else if (setfs(&nd->root_saddr,
1422 nd->root_hostnam, p)) {
1423 kprintf("rootfs %s ",p);
1424 gctx->gotrootpath = 1;
1425 ifctx->gotrootpath = 1;
1426 gctx->setrootfs = ifctx;
1428 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1432 mountopts(&nd->root_args, p);
1433 kprintf("rootopts %s ", p);
1436 panic("Failed to set rootfs to %s",p);
1439 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1442 if (gctx->setswapfs != NULL) {
1443 kprintf("swapfs %s (ignored) ", p);
1444 } else if (setfs(&nd->swap_saddr,
1445 nd->swap_hostnam, p)) {
1446 gctx->gotswappath = 1;
1447 gctx->setswapfs = ifctx;
1448 kprintf("swapfs %s ", p);
1450 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1454 /* swap mount options */
1455 mountopts(&nd->swap_args, p);
1456 kprintf("swapopts %s ", p);
1459 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1464 if (gctx->tag.taglen != 4)
1466 "Expected 4 bytes for swaplen, "
1469 bcopy(p, &swaplen, 4);
1470 nd->swap_nblks = ntohl(swaplen);
1471 kprintf("swapsize %d KB ",
1475 panic("Failed to set swapfs to %s", p);
1478 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1481 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1482 panic("bootpc: hostname >= %d bytes",
1484 if (gctx->sethostname != NULL) {
1485 kprintf("hostname %s (ignored) ", p);
1487 strcpy(nd->my_hostnam, p);
1488 strcpy(hostname, p);
1489 kprintf("hostname %s ",hostname);
1490 gctx->sethostname = ifctx;
1493 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1495 if (p != NULL) { /* store in a sysctl variable */
1496 int i, l = sizeof(bootp_cookie) - 1;
1497 for (i = 0; i < l && p[i] != '\0'; i++)
1498 bootp_cookie[i] = p[i];
1504 if (ifctx->gotnetmask == 0) {
1505 if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1506 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1507 else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1508 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1510 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1512 if (ifctx->gotgw == 0) {
1514 ifctx->gw.sin_addr.s_addr = ifctx->myaddr.sin_addr.s_addr;
1521 struct bootpc_ifcontext *ifctx, *nctx; /* Interface BOOTP contexts */
1522 struct bootpc_globalcontext *gctx; /* Global BOOTP context */
1525 struct nfsv3_diskless *nd;
1528 nd = &nfsv3_diskless;
1532 * If already filled in, don't touch it here
1534 if (nfs_diskless_valid != 0)
1538 * Wait until arp entries can be handled.
1540 while (time_second == 0)
1541 tsleep(&time_second, 0, "arpkludge", 10);
1543 gctx = kmalloc(sizeof(*gctx), M_TEMP, M_WAITOK | M_ZERO);
1545 gctx->xid = ~0xFFFF;
1546 gctx->starttime = time_second;
1548 ifctx = allocifctx(gctx);
1551 * Find a network interface.
1553 #ifdef BOOTP_WIRED_TO
1554 kprintf("bootpc_init: wired to interface '%s'\n",
1555 __XSTRING(BOOTP_WIRED_TO));
1557 bzero(&ifctx->ireq, sizeof(ifctx->ireq));
1558 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1559 strlcpy(ifctx->ireq.ifr_name, ifp->if_xname,
1560 sizeof(ifctx->ireq.ifr_name));
1561 #ifdef BOOTP_WIRED_TO
1562 if (strcmp(ifctx->ireq.ifr_name,
1563 __XSTRING(BOOTP_WIRED_TO)) != 0)
1566 if ((ifp->if_flags &
1567 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1571 if (gctx->interfaces != NULL)
1572 gctx->lastinterface->next = ifctx;
1574 gctx->interfaces = ifctx;
1576 gctx->lastinterface = ifctx;
1577 ifctx = allocifctx(gctx);
1579 kfree(ifctx, M_TEMP);
1581 if (gctx->interfaces == NULL) {
1582 #ifdef BOOTP_WIRED_TO
1583 panic("bootpc_init: Could not find interface specified "
1584 "by BOOTP_WIRED_TO: "
1585 __XSTRING(BOOTP_WIRED_TO));
1587 panic("bootpc_init: no suitable interface");
1591 gctx->gotrootpath = 0;
1592 gctx->gotswappath = 0;
1595 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1596 bootpc_fakeup_interface(ifctx, gctx, td);
1598 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1599 bootpc_compose_query(ifctx, gctx, td);
1601 ifctx = gctx->interfaces;
1602 error = bootpc_call(gctx, td);
1605 #ifdef BOOTP_NFSROOT
1606 panic("BOOTP call failed");
1608 kprintf("BOOTP call failed\n");
1612 mountopts(&nd->root_args, NULL);
1614 mountopts(&nd->swap_args, NULL);
1616 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1617 if (bootpc_ifctx_isresolved(ifctx) != 0)
1618 bootpc_decode_reply(nd, ifctx, gctx);
1620 if (gctx->gotswappath == 0)
1622 #ifdef BOOTP_NFSROOT
1623 if (gctx->gotrootpath == 0)
1624 panic("bootpc: No root path offered");
1627 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
1628 bootpc_adjust_interface(ifctx, gctx, td);
1630 soclose(ifctx->so, FNONBLOCK);
1633 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1634 if (ifctx->gotrootpath != 0)
1636 if (ifctx == NULL) {
1637 for (ifctx = gctx->interfaces;
1639 ifctx = ifctx->next)
1640 if (bootpc_ifctx_isresolved(ifctx) != 0)
1646 if (gctx->gotrootpath != 0) {
1648 error = md_mount(&nd->root_saddr, nd->root_hostnam,
1649 nd->root_fh, &nd->root_fhsize,
1650 &nd->root_args, td);
1652 panic("nfs_boot: mountd root, error=%d", error);
1654 if (gctx->gotswappath != 0) {
1656 error = md_mount(&nd->swap_saddr,
1658 nd->swap_fh, &nd->swap_fhsize,
1659 &nd->swap_args, td);
1661 panic("nfs_boot: mountd swap, error=%d",
1664 error = md_lookup_swap(&nd->swap_saddr,
1666 nd->swap_fh, &nd->swap_fhsize,
1667 &nd->swap_args, td);
1669 panic("nfs_boot: lookup swap, error=%d",
1672 nfs_diskless_valid = 3;
1675 strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1676 bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1677 bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1678 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1679 ifctx->myaddr.sin_addr.s_addr |
1680 ~ ifctx->netmask.sin_addr.s_addr;
1681 bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1684 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = nctx) {
1686 kfree(ifctx, M_TEMP);
1688 kfree(gctx, M_TEMP);