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.26 2008/03/08 07:50:49 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 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");
397 TAILQ_FOREACH(ifp, &ifnet, if_link) {
398 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
399 struct ifaddr *ifa = ifac->ifa;
401 if (ifa->ifa_addr->sa_family == AF_INET)
402 bootpboot_p_if(ifp, ifa);
406 #endif /* defined(BOOTP_DEBUG) */
410 clear_sinaddr(struct sockaddr_in *sin)
412 bzero(sin, sizeof(*sin));
413 sin->sin_len = sizeof(*sin);
414 sin->sin_family = AF_INET;
415 sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
420 static struct bootpc_ifcontext *
421 allocifctx(struct bootpc_globalcontext *gctx)
423 struct bootpc_ifcontext *ifctx;
424 ifctx = (struct bootpc_ifcontext *) kmalloc(sizeof(*ifctx),
426 bzero(ifctx, sizeof(*ifctx));
427 ifctx->xid = gctx->xid;
429 ifctx->state = IF_BOOTP_UNRESOLVED;
431 ifctx->state = IF_DHCP_UNRESOLVED;
439 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
441 if (ifctx->state == IF_BOOTP_RESOLVED ||
442 ifctx->state == IF_DHCP_RESOLVED)
449 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
451 if (ifctx->state == IF_BOOTP_UNRESOLVED ||
452 ifctx->state == IF_DHCP_UNRESOLVED)
459 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
461 if (ifctx->state == IF_BOOTP_FAILED ||
462 ifctx->state == IF_DHCP_FAILED)
469 bootpc_received(struct bootpc_globalcontext *gctx,
470 struct bootpc_ifcontext *ifctx)
472 unsigned char dhcpreplytype;
475 * Need timeout for fallback to less
476 * desirable alternative.
480 /* This call used for the side effect (badopt flag) */
481 (void) bootpc_tag(&gctx->tmptag, &gctx->reply,
485 /* If packet is invalid, ignore it */
486 if (gctx->tmptag.badopt != 0)
489 p = bootpc_tag(&gctx->tmptag, &gctx->reply,
490 gctx->replylen, TAG_DHCP_MSGTYPE);
494 dhcpreplytype = DHCP_NOMSG;
496 switch (ifctx->dhcpquerytype) {
498 if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */
499 #ifndef BOOTP_FORCE_DHCP
500 && dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */
506 if (dhcpreplytype != DHCP_ACK)
514 /* Ignore packet unless it gives us a root tag we didn't have */
516 if ((ifctx->state == IF_BOOTP_RESOLVED ||
517 (ifctx->dhcpquerytype == DHCP_DISCOVER &&
518 (ifctx->state == IF_DHCP_OFFERED ||
519 ifctx->state == IF_DHCP_RESOLVED))) &&
520 (bootpc_tag(&gctx->tmptag, &ifctx->reply,
523 bootpc_tag(&gctx->tmptag, &gctx->reply,
531 ifctx->replylen = gctx->replylen;
533 /* XXX: Only reset if 'perfect' response */
534 if (ifctx->state == IF_BOOTP_UNRESOLVED)
535 ifctx->state = IF_BOOTP_RESOLVED;
536 else if (ifctx->state == IF_DHCP_UNRESOLVED &&
537 ifctx->dhcpquerytype == DHCP_DISCOVER) {
538 if (dhcpreplytype == DHCP_OFFER)
539 ifctx->state = IF_DHCP_OFFERED;
541 ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */
542 } else if (ifctx->state == IF_DHCP_OFFERED &&
543 ifctx->dhcpquerytype == DHCP_REQUEST)
544 ifctx->state = IF_DHCP_RESOLVED;
547 if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
548 ifctx->state != IF_BOOTP_RESOLVED) {
549 p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
550 ifctx->replylen, TAG_DHCP_SERVERID);
551 if (p != NULL && gctx->tmptag.taglen == 4) {
552 memcpy(&ifctx->dhcpserver, p, 4);
553 ifctx->gotdhcpserver = 1;
555 ifctx->gotdhcpserver = 0;
559 ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
562 ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
564 TAG_ROUTERS) != NULL);
565 ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
567 TAG_SUBNETMASK) != NULL);
572 bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
575 struct sockaddr_in *sin, dst;
579 int error, on, rcvflg, timo, len;
583 struct bootpc_ifcontext *ifctx;
590 * Create socket and set its recieve timeout.
592 error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td);
598 bzero(&sopt, sizeof(sopt));
599 sopt.sopt_level = SOL_SOCKET;
600 sopt.sopt_name = SO_RCVTIMEO;
602 sopt.sopt_valsize = sizeof tv;
604 error = sosetopt(so, &sopt);
612 sopt.sopt_name = SO_BROADCAST;
614 sopt.sopt_valsize = sizeof on;
616 error = sosetopt(so, &sopt);
625 sopt.sopt_name = SO_DONTROUTE;
627 sopt.sopt_valsize = sizeof on;
629 error = sosetopt(so, &sopt);
634 * Bind the local endpoint to a bootp client port.
638 sin->sin_port = htons(IPPORT_BOOTPC);
639 error = sobind(so, (struct sockaddr *)sin, td);
641 kprintf("bind failed\n");
646 * Setup socket address for the server.
650 sin->sin_addr.s_addr = INADDR_BROADCAST;
651 sin->sin_port = htons(IPPORT_BOOTPS);
654 * Send it, repeatedly, until a reply is received,
655 * but delay each re-send by an increasing amount.
656 * If the delay hits the maximum, start complaining.
665 for (ifctx = gctx->interfaces;
667 ifctx = ifctx->next) {
668 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
669 bootpc_tag(&gctx->tmptag, &ifctx->reply,
675 for (ifctx = gctx->interfaces;
677 ifctx = ifctx->next) {
678 ifctx->outstanding = 0;
679 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
683 if (bootpc_ifctx_isfailed(ifctx) != 0)
687 ifctx->outstanding = 1;
689 /* Proceed to next step in DHCP negotiation */
690 if ((ifctx->state == IF_DHCP_OFFERED &&
691 ifctx->dhcpquerytype != DHCP_REQUEST) ||
692 (ifctx->state == IF_DHCP_UNRESOLVED &&
693 ifctx->dhcpquerytype != DHCP_DISCOVER) ||
694 (ifctx->state == IF_BOOTP_UNRESOLVED &&
695 ifctx->dhcpquerytype != DHCP_NOMSG)) {
697 bootpc_compose_query(ifctx, gctx, td);
700 /* Send BOOTP request (or re-send). */
702 if (ifctx->sentmsg == 0) {
703 switch(ifctx->dhcpquerytype) {
715 kprintf("Sending %s packet from "
716 "interface %s (%*D)\n",
718 ifctx->ireq.ifr_name,
719 ifctx->sdl->sdl_alen,
720 (unsigned char *) LLADDR(ifctx->sdl),
725 aio.iov_base = (caddr_t) &ifctx->call;
726 aio.iov_len = sizeof(ifctx->call);
730 auio.uio_segflg = UIO_SYSSPACE;
731 auio.uio_rw = UIO_WRITE;
733 auio.uio_resid = sizeof(ifctx->call);
736 /* Set netmask to 0.0.0.0 */
738 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
740 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
741 (caddr_t) &ifctx->ireq, proc0.p_ucred);
744 "set if netmask, error=%d",
747 error = sosend(so, (struct sockaddr *) &dst,
748 &auio, NULL, NULL, 0, td);
750 kprintf("bootpc_call: sosend: %d state %08x\n",
751 error, (int) so->so_state);
754 /* XXX: Is this needed ? */
755 tsleep(&error, 0, "bootpw", 10);
757 /* Set netmask to 255.0.0.0 */
759 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
761 sin->sin_addr.s_addr = htonl(0xff000000u);
762 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
763 (caddr_t) &ifctx->ireq, proc0.p_ucred);
766 "set if netmask, error=%d",
771 if (outstanding == 0 &&
772 (rtimo == 0 || time_second >= rtimo)) {
777 /* Determine new timeout. */
778 if (timo < MAX_RESEND_DELAY)
781 kprintf("DHCP/BOOTP timeout for server ");
782 print_sin_addr(&dst);
787 * Wait for up to timo seconds for a reply.
788 * The socket receive timeout was set to 1 second.
790 atimo = timo + time_second;
791 while (time_second < atimo) {
792 aio.iov_base = (caddr_t) &gctx->reply;
793 aio.iov_len = sizeof(gctx->reply);
797 auio.uio_segflg = UIO_SYSSPACE;
798 auio.uio_rw = UIO_READ;
800 auio.uio_resid = sizeof(gctx->reply);
804 error = soreceive(so, NULL, &auio,
805 NULL, NULL, &rcvflg);
806 gctx->secs = time_second - gctx->starttime;
807 for (ifctx = gctx->interfaces;
809 ifctx = ifctx->next) {
810 if (bootpc_ifctx_isresolved(ifctx) != 0 ||
811 bootpc_ifctx_isfailed(ifctx) != 0)
814 ifctx->call.secs = htons(gctx->secs);
816 if (error == EWOULDBLOCK)
820 len = sizeof(gctx->reply) - auio.uio_resid;
822 /* Do we have the required number of bytes ? */
823 if (len < BOOTP_MIN_LEN)
825 gctx->replylen = len;
828 if (gctx->reply.op != BOOTP_REPLY)
831 /* Is this an answer to our query */
832 for (ifctx = gctx->interfaces;
834 ifctx = ifctx->next) {
835 if (gctx->reply.xid != ifctx->call.xid)
838 /* Same HW address size ? */
839 if (gctx->reply.hlen != ifctx->call.hlen)
842 /* Correct HW address ? */
843 if (bcmp(gctx->reply.chaddr,
845 ifctx->call.hlen) != 0)
852 s = bootpc_tag(&gctx->tmptag,
865 s = "DHCP (unexpected)";
871 kprintf("Received %s packet"
874 ifctx->ireq.ifr_name);
875 print_in_addr(gctx->reply.siaddr);
876 if (gctx->reply.giaddr.s_addr !=
879 print_in_addr(gctx->reply.giaddr);
881 if (bootpc_received(gctx, ifctx) != 0) {
882 kprintf(" (accepted)");
883 if (ifctx->outstanding) {
884 ifctx->outstanding = 0;
887 /* Network settle delay */
888 if (outstanding == 0)
889 atimo = time_second +
892 kprintf(" (ignored)");
893 if (ifctx->gotrootpath) {
895 rtimo = time_second +
897 kprintf(" (got root path)");
899 kprintf(" (no root path)");
904 if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
907 /* Force a retry if halfway in DHCP negotiation */
909 for (ifctx = gctx->interfaces; ifctx != NULL;
910 ifctx = ifctx->next) {
911 if (ifctx->state == IF_DHCP_OFFERED) {
912 if (ifctx->dhcpquerytype == DHCP_DISCOVER)
915 ifctx->state = IF_DHCP_UNRESOLVED;
922 if (gotrootpath != 0) {
923 gctx->gotrootpath = gotrootpath;
924 if (rtimo != 0 && time_second >= rtimo)
927 } /* forever send/receive */
930 * XXX: These are errors of varying seriousness being silently
934 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
935 if (bootpc_ifctx_isresolved(ifctx) == 0) {
936 kprintf("%s timeout for interface %s\n",
937 ifctx->dhcpquerytype != DHCP_NOMSG ?
939 ifctx->ireq.ifr_name);
942 if (gctx->gotrootpath != 0) {
944 kprintf("Got a root path, ignoring remaining timeout\n");
949 #ifndef BOOTP_NFSROOT
950 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
951 if (bootpc_ifctx_isresolved(ifctx) != 0) {
962 soclose(so, FNONBLOCK);
968 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
969 struct bootpc_globalcontext *gctx,
972 struct ifaddr_container *ifac;
973 struct sockaddr_in *sin;
978 struct sockaddr_dl *sdl;
980 error = socreate(AF_INET, &ifctx->so, SOCK_DGRAM, 0, td);
982 panic("nfs_boot: socreate, error=%d", error);
988 * Bring up the interface.
990 * Get the old interface flags and or IFF_UP into them; if
991 * IFF_UP set blindly, interface selection can be clobbered.
993 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, proc0.p_ucred);
995 panic("bootpc_fakeup_interface: GIFFLAGS, error=%d", error);
996 ireq->ifr_flags |= IFF_UP;
997 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, proc0.p_ucred);
999 panic("bootpc_fakeup_interface: SIFFLAGS, error=%d", error);
1002 * Do enough of ifconfig(8) so that the chosen interface
1003 * can talk to the servers. (just set the address)
1006 /* addr is 0.0.0.0 */
1008 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1010 error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, proc0.p_ucred);
1011 if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1012 panic("bootpc_fakeup_interface: "
1013 "set if addr, error=%d", error);
1015 /* netmask is 255.0.0.0 */
1017 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1019 sin->sin_addr.s_addr = htonl(0xff000000u);
1020 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t)ireq, proc0.p_ucred);
1022 panic("bootpc_fakeup_interface: set if netmask, error=%d",
1025 /* Broadcast is 255.255.255.255 */
1027 sin = (struct sockaddr_in *)&ireq->ifr_addr;
1029 clear_sinaddr(&ifctx->broadcast);
1030 sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
1031 ifctx->broadcast.sin_addr.s_addr = sin->sin_addr.s_addr;
1033 error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t)ireq, proc0.p_ucred);
1034 if (error != 0 && error != EADDRNOTAVAIL)
1035 panic("bootpc_fakeup_interface: "
1036 "set if broadcast addr, error=%d",
1040 /* Get HW address */
1043 TAILQ_FOREACH(ifac, &ifctx->ifp->if_addrheads[mycpuid], ifa_link) {
1044 struct ifaddr *ifa = ifac->ifa;
1046 if (ifa->ifa_addr->sa_family == AF_LINK &&
1047 (sdl = ((struct sockaddr_dl *) ifa->ifa_addr)) != NULL &&
1048 sdl->sdl_type == IFT_ETHER)
1053 panic("bootpc: Unable to find HW address for %s",
1054 ifctx->ireq.ifr_name);
1062 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
1063 struct bootpc_globalcontext *gctx,
1067 struct sockaddr_in defdst;
1068 struct sockaddr_in defmask;
1069 struct sockaddr_in *sin;
1073 struct sockaddr_in *myaddr;
1074 struct sockaddr_in *netmask;
1075 struct sockaddr_in *gw;
1077 ireq = &ifctx->ireq;
1079 myaddr = &ifctx->myaddr;
1080 netmask = &ifctx->netmask;
1083 if (bootpc_ifctx_isresolved(ifctx) == 0) {
1085 /* Shutdown interfaces where BOOTP failed */
1087 kprintf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
1088 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, proc0.p_ucred);
1090 panic("bootpc_adjust_interface: "
1091 "SIOCGIFFLAGS, error=%d", error);
1092 ireq->ifr_flags &= ~IFF_UP;
1093 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, proc0.p_ucred);
1095 panic("bootpc_adjust_interface: "
1096 "SIOCSIFFLAGS, error=%d", error);
1098 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1100 error = ifioctl(so, SIOCDIFADDR, (caddr_t) ireq, proc0.p_ucred);
1101 if (error != 0 && (error != EADDRNOTAVAIL ||
1102 ifctx == gctx->interfaces))
1103 panic("bootpc_adjust_interface: "
1104 "SIOCDIFADDR, error=%d", error);
1109 kprintf("Adjusted interface %s\n", ifctx->ireq.ifr_name);
1111 * Do enough of ifconfig(8) so that the chosen interface
1112 * can talk to the servers. (just set the address)
1114 bcopy(netmask, &ireq->ifr_addr, sizeof(*netmask));
1115 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t) ireq, proc0.p_ucred);
1117 panic("bootpc_adjust_interface: "
1118 "set if netmask, error=%d", error);
1120 /* Broadcast is with host part of IP address all 1's */
1122 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1124 sin->sin_addr.s_addr = myaddr->sin_addr.s_addr |
1125 ~ netmask->sin_addr.s_addr;
1126 error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t) ireq, proc0.p_ucred);
1128 panic("bootpc_adjust_interface: "
1129 "set if broadcast addr, error=%d", error);
1131 bcopy(myaddr, &ireq->ifr_addr, sizeof(*myaddr));
1132 error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, proc0.p_ucred);
1133 if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1134 panic("bootpc_adjust_interface: "
1135 "set if addr, error=%d", error);
1137 /* Add new default route */
1139 if (ifctx->gotgw != 0 || gctx->gotgw == 0) {
1140 clear_sinaddr(&defdst);
1141 clear_sinaddr(&defmask);
1142 error = rtrequest_global(RTM_ADD, (struct sockaddr *) &defdst,
1143 (struct sockaddr *) gw,
1144 (struct sockaddr *) &defmask,
1145 (RTF_UP | RTF_GATEWAY | RTF_STATIC));
1147 kprintf("bootpc_adjust_interface: "
1148 "add net route, error=%d\n", error);
1157 print_sin_addr(struct sockaddr_in *sin)
1159 print_in_addr(sin->sin_addr);
1164 print_in_addr(struct in_addr addr)
1168 ip = ntohl(addr.s_addr);
1169 kprintf("%d.%d.%d.%d",
1170 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1174 bootpc_compose_query(struct bootpc_ifcontext *ifctx,
1175 struct bootpc_globalcontext *gctx, struct thread *td)
1177 unsigned char *vendp;
1178 unsigned char vendor_client[64];
1180 uint8_t vendor_client_len;
1182 ifctx->gotrootpath = 0;
1184 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1187 ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */
1188 ifctx->call.htype = 1; /* 10mb ethernet */
1189 ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1190 ifctx->call.hops = 0;
1191 if (bootpc_ifctx_isunresolved(ifctx) != 0)
1193 ifctx->call.xid = txdr_unsigned(ifctx->xid);
1194 bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1196 vendp = ifctx->call.vend;
1197 *vendp++ = 99; /* RFC1048 cookie */
1201 *vendp++ = TAG_MAXMSGSIZE;
1203 *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1204 *vendp++ = sizeof(struct bootp_packet) & 255;
1206 ksnprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1207 ostype, MACHINE, osrelease);
1208 vendor_client_len = strlen(vendor_client);
1209 *vendp++ = TAG_VENDOR_INDENTIFIER;
1210 *vendp++ = vendor_client_len;
1211 memcpy(vendp, vendor_client, vendor_client_len);
1212 vendp += vendor_client_len;
1213 ifctx->dhcpquerytype = DHCP_NOMSG;
1214 switch (ifctx->state) {
1215 case IF_DHCP_UNRESOLVED:
1216 *vendp++ = TAG_DHCP_MSGTYPE;
1218 *vendp++ = DHCP_DISCOVER;
1219 ifctx->dhcpquerytype = DHCP_DISCOVER;
1220 ifctx->gotdhcpserver = 0;
1222 case IF_DHCP_OFFERED:
1223 *vendp++ = TAG_DHCP_MSGTYPE;
1225 *vendp++ = DHCP_REQUEST;
1226 ifctx->dhcpquerytype = DHCP_REQUEST;
1227 *vendp++ = TAG_DHCP_REQ_ADDR;
1229 memcpy(vendp, &ifctx->reply.yiaddr, 4);
1231 if (ifctx->gotdhcpserver != 0) {
1232 *vendp++ = TAG_DHCP_SERVERID;
1234 memcpy(vendp, &ifctx->dhcpserver, 4);
1237 *vendp++ = TAG_DHCP_LEASETIME;
1239 leasetime = htonl(300);
1240 memcpy(vendp, &leasetime, 4);
1247 ifctx->call.secs = 0;
1248 ifctx->call.flags = htons(0x8000); /* We need an broadcast answer */
1253 bootpc_hascookie(struct bootp_packet *bp)
1255 return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1256 bp->vend[2] == 83 && bp->vend[3] == 99);
1261 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1262 unsigned char *start, int len, int tag)
1268 if (tctx->badtag != 0 || tctx->badopt != 0)
1276 if (code == TAG_PAD)
1278 if (code == TAG_END)
1280 if (j >= ej || j + *j + 1 > ej) {
1286 if (tctx->taglen + len > TAG_MAXLEN) {
1292 memcpy(tctx->buf + tctx->taglen,
1294 tctx->taglen += len;
1296 if (code == TAG_OVERLOAD)
1297 tctx->overload = *j;
1304 static unsigned char *
1305 bootpc_tag(struct bootpc_tagcontext *tctx, struct bootp_packet *bp,
1317 if (bootpc_hascookie(bp) == 0)
1321 ej = (unsigned char *) bp + len;
1323 bootpc_tag_helper(tctx, &bp->vend[4],
1324 (unsigned char *) bp + len - &bp->vend[4], tag);
1326 if ((tctx->overload & OVERLOAD_FILE) != 0)
1327 bootpc_tag_helper(tctx,
1328 (unsigned char *) bp->file,
1331 if ((tctx->overload & OVERLOAD_SNAME) != 0)
1332 bootpc_tag_helper(tctx,
1333 (unsigned char *) bp->sname,
1337 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1339 tctx->buf[tctx->taglen] = '\0';
1345 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1346 struct bootpc_globalcontext *gctx)
1352 ifctx->gotnetmask = 0;
1354 clear_sinaddr(&ifctx->myaddr);
1355 clear_sinaddr(&ifctx->netmask);
1356 clear_sinaddr(&ifctx->gw);
1358 ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1360 ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1361 ksnprintf(gctx->lookup_path, sizeof(gctx->lookup_path),
1363 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1365 kprintf("%s at ", ifctx->ireq.ifr_name);
1366 print_sin_addr(&ifctx->myaddr);
1367 kprintf(" server ");
1368 print_in_addr(ifctx->reply.siaddr);
1370 ifctx->gw.sin_addr = ifctx->reply.giaddr;
1371 if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1372 kprintf(" via gateway ");
1373 print_in_addr(ifctx->reply.giaddr);
1376 /* This call used for the side effect (overload flag) */
1377 (void) bootpc_tag(&gctx->tmptag,
1378 &ifctx->reply, ifctx->replylen, TAG_END);
1380 if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1381 if (ifctx->reply.sname[0] != '\0')
1382 kprintf(" server name %s", ifctx->reply.sname);
1383 if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1384 if (ifctx->reply.file[0] != '\0')
1385 kprintf(" boot file %s", ifctx->reply.file);
1389 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1392 if (gctx->tag.taglen != 4)
1393 panic("bootpc: subnet mask len is %d",
1395 bcopy(p, &ifctx->netmask.sin_addr, 4);
1396 ifctx->gotnetmask = 1;
1397 kprintf("subnet mask ");
1398 print_sin_addr(&ifctx->netmask);
1402 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1406 if (gctx->tag.taglen % 4)
1407 panic("bootpc: Router Len is %d", gctx->tag.taglen);
1408 if (gctx->tag.taglen > 0) {
1409 bcopy(p, &ifctx->gw.sin_addr, 4);
1411 print_sin_addr(&ifctx->gw);
1418 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1421 if (gctx->setrootfs != NULL) {
1422 kprintf("rootfs %s (ignored) ", p);
1423 } else if (setfs(&nd->root_saddr,
1424 nd->root_hostnam, p)) {
1425 kprintf("rootfs %s ",p);
1426 gctx->gotrootpath = 1;
1427 ifctx->gotrootpath = 1;
1428 gctx->setrootfs = ifctx;
1430 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1434 nfs_mountopts(&nd->root_args, p);
1435 kprintf("rootopts %s ", p);
1438 panic("Failed to set rootfs to %s",p);
1441 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1444 if (gctx->setswapfs != NULL) {
1445 kprintf("swapfs %s (ignored) ", p);
1446 } else if (setfs(&nd->swap_saddr,
1447 nd->swap_hostnam, p)) {
1448 gctx->gotswappath = 1;
1449 gctx->setswapfs = ifctx;
1450 kprintf("swapfs %s ", p);
1452 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1456 /* swap mount options */
1457 nfs_mountopts(&nd->swap_args, p);
1458 kprintf("swapopts %s ", p);
1461 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1466 if (gctx->tag.taglen != 4)
1468 "Expected 4 bytes for swaplen, "
1471 bcopy(p, &swaplen, 4);
1472 nd->swap_nblks = ntohl(swaplen);
1473 kprintf("swapsize %d KB ",
1477 panic("Failed to set swapfs to %s", p);
1480 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1483 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1484 panic("bootpc: hostname >= %d bytes",
1486 if (gctx->sethostname != NULL) {
1487 kprintf("hostname %s (ignored) ", p);
1489 strcpy(nd->my_hostnam, p);
1490 strcpy(hostname, p);
1491 kprintf("hostname %s ",hostname);
1492 gctx->sethostname = ifctx;
1495 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1497 if (p != NULL) { /* store in a sysctl variable */
1498 int i, l = sizeof(bootp_cookie) - 1;
1499 for (i = 0; i < l && p[i] != '\0'; i++)
1500 bootp_cookie[i] = p[i];
1506 if (ifctx->gotnetmask == 0) {
1507 if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1508 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1509 else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1510 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1512 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1514 if (ifctx->gotgw == 0) {
1516 ifctx->gw.sin_addr.s_addr = ifctx->myaddr.sin_addr.s_addr;
1523 struct bootpc_ifcontext *ifctx, *nctx; /* Interface BOOTP contexts */
1524 struct bootpc_globalcontext *gctx; /* Global BOOTP context */
1527 struct nfsv3_diskless *nd;
1530 nd = &nfsv3_diskless;
1534 * If already filled in, don't touch it here
1536 if (nfs_diskless_valid != 0)
1540 * Wait until arp entries can be handled.
1542 while (time_second == 0)
1543 tsleep(&time_second, 0, "arpkludge", 10);
1545 gctx = kmalloc(sizeof(*gctx), M_TEMP, M_WAITOK | M_ZERO);
1547 gctx->xid = ~0xFFFF;
1548 gctx->starttime = time_second;
1550 ifctx = allocifctx(gctx);
1553 * Find a network interface.
1555 #ifdef BOOTP_WIRED_TO
1556 kprintf("bootpc_init: wired to interface '%s'\n",
1557 __XSTRING(BOOTP_WIRED_TO));
1559 bzero(&ifctx->ireq, sizeof(ifctx->ireq));
1560 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1561 strlcpy(ifctx->ireq.ifr_name, ifp->if_xname,
1562 sizeof(ifctx->ireq.ifr_name));
1563 #ifdef BOOTP_WIRED_TO
1564 if (strcmp(ifctx->ireq.ifr_name,
1565 __XSTRING(BOOTP_WIRED_TO)) != 0)
1568 if ((ifp->if_flags &
1569 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1573 if (gctx->interfaces != NULL)
1574 gctx->lastinterface->next = ifctx;
1576 gctx->interfaces = ifctx;
1578 gctx->lastinterface = ifctx;
1579 ifctx = allocifctx(gctx);
1581 kfree(ifctx, M_TEMP);
1583 if (gctx->interfaces == NULL) {
1584 #ifdef BOOTP_WIRED_TO
1585 panic("bootpc_init: Could not find interface specified "
1586 "by BOOTP_WIRED_TO: "
1587 __XSTRING(BOOTP_WIRED_TO));
1589 panic("bootpc_init: no suitable interface");
1593 gctx->gotrootpath = 0;
1594 gctx->gotswappath = 0;
1597 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1598 bootpc_fakeup_interface(ifctx, gctx, td);
1600 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1601 bootpc_compose_query(ifctx, gctx, td);
1603 ifctx = gctx->interfaces;
1604 error = bootpc_call(gctx, td);
1607 #ifdef BOOTP_NFSROOT
1608 panic("BOOTP call failed");
1610 kprintf("BOOTP call failed\n");
1614 nfs_mountopts(&nd->root_args, NULL);
1616 nfs_mountopts(&nd->swap_args, NULL);
1618 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1619 if (bootpc_ifctx_isresolved(ifctx) != 0)
1620 bootpc_decode_reply(nd, ifctx, gctx);
1622 if (gctx->gotswappath == 0)
1624 #ifdef BOOTP_NFSROOT
1625 if (gctx->gotrootpath == 0)
1626 panic("bootpc: No root path offered");
1629 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
1630 bootpc_adjust_interface(ifctx, gctx, td);
1632 soclose(ifctx->so, FNONBLOCK);
1635 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1636 if (ifctx->gotrootpath != 0)
1638 if (ifctx == NULL) {
1639 for (ifctx = gctx->interfaces;
1641 ifctx = ifctx->next)
1642 if (bootpc_ifctx_isresolved(ifctx) != 0)
1648 if (gctx->gotrootpath != 0) {
1650 error = md_mount(&nd->root_saddr, nd->root_hostnam,
1651 nd->root_fh, &nd->root_fhsize,
1652 &nd->root_args, td);
1654 panic("nfs_boot: mountd root, error=%d", error);
1656 if (gctx->gotswappath != 0) {
1658 error = md_mount(&nd->swap_saddr,
1660 nd->swap_fh, &nd->swap_fhsize,
1661 &nd->swap_args, td);
1663 panic("nfs_boot: mountd swap, error=%d",
1666 error = md_lookup_swap(&nd->swap_saddr,
1668 nd->swap_fh, &nd->swap_fhsize,
1669 &nd->swap_args, td);
1671 panic("nfs_boot: lookup swap, error=%d",
1674 nfs_diskless_valid = 3;
1677 strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1678 bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1679 bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1680 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1681 ifctx->myaddr.sin_addr.s_addr |
1682 ~ ifctx->netmask.sin_addr.s_addr;
1683 bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1686 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = nctx) {
1688 kfree(ifctx, M_TEMP);
1690 kfree(gctx, M_TEMP);