Fix up more files for the <net/if.h> / <net/if_var.h> split (fixes LINT64).
[dragonfly.git] / sys / vfs / nfs / bootp_subr.c
blob9d405440a14cba1ed51e29c69e517a57cf66775d
1 /*
2 * Copyright (c) 1995 Gordon Ross, Adam Glass
3 * Copyright (c) 1992 Regents of the University of California.
4 * All rights reserved.
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
12 * are met:
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
36 * SUCH DAMAGE.
38 * nfs/krpc_subr.c
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>
49 #include <sys/proc.h>
50 #include <sys/malloc.h>
51 #include <sys/mount.h>
52 #include <sys/mbuf.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/sysctl.h>
56 #include <sys/uio.h>
57 #include <sys/fcntl.h>
59 #include <net/if.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>
67 #include "rpcv2.h"
68 #include "nfsproto.h"
69 #include "nfs.h"
70 #include "nfsdiskless.h"
71 #include "krpc.h"
72 #include "xdr_subs.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
79 #endif
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 */
90 struct bootp_packet {
91 u_int8_t op;
92 u_int8_t htype;
93 u_int8_t hlen;
94 u_int8_t hops;
95 u_int32_t xid;
96 u_int16_t secs;
97 u_int16_t flags;
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];
103 char sname[64];
104 char file[128];
105 unsigned char vend[1222];
108 struct bootpc_ifcontext {
109 struct bootpc_ifcontext *next;
110 struct bootp_packet call;
111 struct bootp_packet reply;
112 int replylen;
113 int overload;
114 struct socket *so;
115 struct ifreq ireq;
116 struct ifnet *ifp;
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 */
122 int gotgw;
123 int gotnetmask;
124 int gotrootpath;
125 int outstanding;
126 int sentmsg;
127 u_int32_t xid;
128 enum {
129 IF_BOOTP_UNRESOLVED,
130 IF_BOOTP_RESOLVED,
131 IF_BOOTP_FAILED,
132 IF_DHCP_UNRESOLVED,
133 IF_DHCP_OFFERED,
134 IF_DHCP_RESOLVED,
135 IF_DHCP_FAILED,
136 } state;
137 int dhcpquerytype; /* dhcp type sent */
138 struct in_addr dhcpserver;
139 int gotdhcpserver;
142 #define TAG_MAXLEN 1024
143 struct bootpc_tagcontext {
144 char buf[TAG_MAXLEN + 1];
145 int overload;
146 int badopt;
147 int badtag;
148 int foundopt;
149 int taglen;
152 struct bootpc_globalcontext {
153 struct bootpc_ifcontext *interfaces;
154 struct bootpc_ifcontext *lastinterface;
155 u_int32_t xid;
156 int gotrootpath;
157 int gotswappath;
158 int gotgw;
159 int ifnum;
160 int secs;
161 int starttime;
162 struct bootp_packet reply;
163 int replylen;
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
178 /* Common tags */
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: */
196 #define TAG_SWAP 128
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
209 #define DHCP_NOMSG 0
210 #define DHCP_DISCOVER 1
211 #define DHCP_OFFER 2
212 #define DHCP_REQUEST 3
213 #define DHCP_ACK 5
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");
219 /* mountd RPC */
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);
223 static
224 struct bootpc_ifcontext *allocifctx(struct bootpc_globalcontext *gctx);
225 static void bootpc_compose_query(struct bootpc_ifcontext *ifctx,
226 struct bootpc_globalcontext *gctx,
227 struct thread *td);
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);
233 #ifdef BOOTP_DEBUG
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);
241 #endif
243 static int bootpc_call(struct bootpc_globalcontext *gctx,
244 struct thread *td);
246 static int bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
247 struct bootpc_globalcontext *gctx,
248 struct thread *td);
250 static int bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
251 struct bootpc_globalcontext *gctx,
252 struct thread *td);
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
270 * some tricks:
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.
279 #ifdef BOOTP_DEBUG
280 void
281 bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma)
283 if (sa == NULL) {
284 kprintf("(sockaddr *) <null>");
285 return;
287 switch (sa->sa_family) {
288 case AF_INET:
290 struct sockaddr_in *sin;
292 sin = (struct sockaddr_in *) sa;
293 kprintf("inet ");
294 print_sin_addr(sin);
295 if (ma != NULL) {
296 sin = (struct sockaddr_in *) ma;
297 kprintf(" mask ");
298 print_sin_addr(sin);
301 break;
302 case AF_LINK:
304 struct sockaddr_dl *sli;
305 int i;
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++) {
310 if (i > 0)
311 kprintf(":");
312 kprintf("%x", ((unsigned char *) LLADDR(sli))[i]);
315 break;
316 default:
317 kprintf("af%d", sa->sa_family);
322 void
323 bootpboot_p_ma(struct sockaddr *ma)
325 if (ma == NULL) {
326 kprintf("<null>");
327 return;
329 kprintf("%x", *(int *)ma);
333 void
334 bootpboot_p_rtentry(struct rtentry *rt)
336 bootpboot_p_sa(rt_key(rt), rt_mask(rt));
337 kprintf(" ");
338 bootpboot_p_ma(rt->rt_genmask);
339 kprintf(" ");
340 bootpboot_p_sa(rt->rt_gateway, NULL);
341 kprintf(" ");
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));
348 void
349 bootpboot_p_tree(struct radix_node *rn)
351 while (rn != NULL) {
352 if (rn->rn_bit < 0) {
353 if ((rn->rn_flags & RNF_ROOT) != 0) {
354 } else {
355 bootpboot_p_rtentry((struct rtentry *) rn);
357 rn = rn->rn_dupedkey;
358 } else {
359 bootpboot_p_tree(rn->rn_left);
360 bootpboot_p_tree(rn->rn_right);
361 return;
367 void
368 bootpboot_p_rtlist(void)
370 kprintf("Routing table:\n");
371 bootpboot_p_tree(rt_tables[AF_INET]->rnh_treetop);
375 void
376 bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
378 kprintf("%s flags %x, addr ",
379 if_name(ifp),
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);
386 kprintf("\n");
390 void
391 bootpboot_p_iflist(void)
393 struct ifnet *ifp;
394 struct ifaddr_container *ifac;
396 kprintf("Interface list:\n");
397 ifnet_lock();
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);
406 ifnet_unlock();
408 #endif /* defined(BOOTP_DEBUG) */
411 static void
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) ? */
418 sin->sin_port = 0;
422 static struct bootpc_ifcontext *
423 allocifctx(struct bootpc_globalcontext *gctx)
425 struct bootpc_ifcontext *ifctx;
426 ifctx = (struct bootpc_ifcontext *) kmalloc(sizeof(*ifctx),
427 M_TEMP, M_WAITOK);
428 bzero(ifctx, sizeof(*ifctx));
429 ifctx->xid = gctx->xid;
430 #ifdef BOOTP_NO_DHCP
431 ifctx->state = IF_BOOTP_UNRESOLVED;
432 #else
433 ifctx->state = IF_DHCP_UNRESOLVED;
434 #endif
435 gctx->xid += 0x100;
436 return ifctx;
440 static __inline int
441 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
443 if (ifctx->state == IF_BOOTP_RESOLVED ||
444 ifctx->state == IF_DHCP_RESOLVED)
445 return 1;
446 return 0;
450 static __inline int
451 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
453 if (ifctx->state == IF_BOOTP_UNRESOLVED ||
454 ifctx->state == IF_DHCP_UNRESOLVED)
455 return 1;
456 return 0;
460 static __inline int
461 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
463 if (ifctx->state == IF_BOOTP_FAILED ||
464 ifctx->state == IF_DHCP_FAILED)
465 return 1;
466 return 0;
470 static int
471 bootpc_received(struct bootpc_globalcontext *gctx,
472 struct bootpc_ifcontext *ifctx)
474 unsigned char dhcpreplytype;
475 char *p;
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,
484 gctx->replylen,
485 TAG_END);
487 /* If packet is invalid, ignore it */
488 if (gctx->tmptag.badopt != 0)
489 return 0;
491 p = bootpc_tag(&gctx->tmptag, &gctx->reply,
492 gctx->replylen, TAG_DHCP_MSGTYPE);
493 if (p != NULL)
494 dhcpreplytype = *p;
495 else
496 dhcpreplytype = DHCP_NOMSG;
498 switch (ifctx->dhcpquerytype) {
499 case DHCP_DISCOVER:
500 if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */
501 #ifndef BOOTP_FORCE_DHCP
502 && dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */
503 #endif
505 return 0;
506 break;
507 case DHCP_REQUEST:
508 if (dhcpreplytype != DHCP_ACK)
509 return 0;
510 /* fall through */
511 case DHCP_NOMSG:
512 break;
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,
523 ifctx->replylen,
524 TAG_ROOT) != NULL ||
525 bootpc_tag(&gctx->tmptag, &gctx->reply,
526 gctx->replylen,
527 TAG_ROOT) == NULL))
528 return 0;
530 bcopy(&gctx->reply,
531 &ifctx->reply,
532 gctx->replylen);
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;
542 else
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;
556 } else
557 ifctx->gotdhcpserver = 0;
558 return 1;
561 ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
562 ifctx->replylen,
563 TAG_ROOT) != NULL);
564 ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
565 ifctx->replylen,
566 TAG_ROUTERS) != NULL);
567 ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
568 ifctx->replylen,
569 TAG_SUBNETMASK) != NULL);
570 return 1;
573 static int
574 bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
576 struct socket *so;
577 struct sockaddr_in *sin, dst;
578 struct uio auio;
579 struct sockopt sopt;
580 struct iovec aio;
581 int error, on, rcvflg, timo, len;
582 time_t atimo;
583 time_t rtimo;
584 struct timeval tv;
585 struct bootpc_ifcontext *ifctx;
586 int outstanding;
587 int gotrootpath;
588 int retry;
589 const char *s;
590 char hexstr[64];
593 * Create socket and set its recieve timeout.
595 error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td);
596 if (error != 0)
597 goto out;
599 tv.tv_sec = 1;
600 tv.tv_usec = 0;
601 bzero(&sopt, sizeof(sopt));
602 sopt.sopt_level = SOL_SOCKET;
603 sopt.sopt_name = SO_RCVTIMEO;
604 sopt.sopt_val = &tv;
605 sopt.sopt_valsize = sizeof tv;
607 error = sosetopt(so, &sopt);
608 if (error != 0)
609 goto out;
612 * Enable broadcast.
614 on = 1;
615 sopt.sopt_name = SO_BROADCAST;
616 sopt.sopt_val = &on;
617 sopt.sopt_valsize = sizeof on;
619 error = sosetopt(so, &sopt);
620 if (error != 0)
621 goto out;
624 * Disable routing.
627 on = 1;
628 sopt.sopt_name = SO_DONTROUTE;
629 sopt.sopt_val = &on;
630 sopt.sopt_valsize = sizeof on;
632 error = sosetopt(so, &sopt);
633 if (error != 0)
634 goto out;
637 * Bind the local endpoint to a bootp client port.
639 sin = &dst;
640 clear_sinaddr(sin);
641 sin->sin_port = htons(IPPORT_BOOTPC);
642 error = sobind(so, (struct sockaddr *)sin, td);
643 if (error != 0) {
644 kprintf("bind failed\n");
645 goto out;
649 * Setup socket address for the server.
651 sin = &dst;
652 clear_sinaddr(sin);
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.
661 timo = 0;
662 rtimo = 0;
663 for (;;) {
665 outstanding = 0;
666 gotrootpath = 0;
668 for (ifctx = gctx->interfaces;
669 ifctx != NULL;
670 ifctx = ifctx->next) {
671 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
672 bootpc_tag(&gctx->tmptag, &ifctx->reply,
673 ifctx->replylen,
674 TAG_ROOT) != NULL)
675 gotrootpath = 1;
678 for (ifctx = gctx->interfaces;
679 ifctx != NULL;
680 ifctx = ifctx->next) {
681 ifctx->outstanding = 0;
682 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
683 gotrootpath != 0) {
684 continue;
686 if (bootpc_ifctx_isfailed(ifctx) != 0)
687 continue;
689 outstanding++;
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)) {
699 ifctx->sentmsg = 0;
700 bootpc_compose_query(ifctx, gctx, td);
703 /* Send BOOTP request (or re-send). */
705 if (ifctx->sentmsg == 0) {
706 switch(ifctx->dhcpquerytype) {
707 case DHCP_DISCOVER:
708 s = "DHCP Discover";
709 break;
710 case DHCP_REQUEST:
711 s = "DHCP Request";
712 break;
713 case DHCP_NOMSG:
714 default:
715 s = "BOOTP Query";
716 break;
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,
724 hexstr);
725 ifctx->sentmsg = 1;
728 aio.iov_base = (caddr_t) &ifctx->call;
729 aio.iov_len = sizeof(ifctx->call);
731 auio.uio_iov = &aio;
732 auio.uio_iovcnt = 1;
733 auio.uio_segflg = UIO_SYSSPACE;
734 auio.uio_rw = UIO_WRITE;
735 auio.uio_offset = 0;
736 auio.uio_resid = sizeof(ifctx->call);
737 auio.uio_td = td;
739 /* Set netmask to 0.0.0.0 */
741 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
742 clear_sinaddr(sin);
743 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
744 (caddr_t) &ifctx->ireq, proc0.p_ucred);
745 if (error != 0)
746 panic("bootpc_call:"
747 "set if netmask, error=%d",
748 error);
750 error = sosend(so, (struct sockaddr *) &dst,
751 &auio, NULL, NULL, 0, td);
752 if (error != 0) {
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;
763 clear_sinaddr(sin);
764 sin->sin_addr.s_addr = htonl(0xff000000u);
765 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
766 (caddr_t) &ifctx->ireq, proc0.p_ucred);
767 if (error != 0)
768 panic("bootpc_call:"
769 "set if netmask, error=%d",
770 error);
774 if (outstanding == 0 &&
775 (rtimo == 0 || time_uptime >= rtimo)) {
776 error = 0;
777 goto gotreply;
780 /* Determine new timeout. */
781 if (timo < MAX_RESEND_DELAY)
782 timo++;
783 else {
784 kprintf("DHCP/BOOTP timeout for server ");
785 print_sin_addr(&dst);
786 kprintf("\n");
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);
798 auio.uio_iov = &aio;
799 auio.uio_iovcnt = 1;
800 auio.uio_segflg = UIO_SYSSPACE;
801 auio.uio_rw = UIO_READ;
802 auio.uio_offset = 0;
803 auio.uio_resid = sizeof(gctx->reply);
804 auio.uio_td = td;
806 rcvflg = 0;
807 error = soreceive(so, NULL, &auio,
808 NULL, NULL, &rcvflg);
809 gctx->secs = time_uptime - gctx->starttime;
810 for (ifctx = gctx->interfaces;
811 ifctx != NULL;
812 ifctx = ifctx->next) {
813 if (bootpc_ifctx_isresolved(ifctx) != 0 ||
814 bootpc_ifctx_isfailed(ifctx) != 0)
815 continue;
817 ifctx->call.secs = htons(gctx->secs);
819 if (error == EWOULDBLOCK)
820 continue;
821 if (error != 0)
822 goto out;
823 len = sizeof(gctx->reply) - auio.uio_resid;
825 /* Do we have the required number of bytes ? */
826 if (len < BOOTP_MIN_LEN)
827 continue;
828 gctx->replylen = len;
830 /* Is it a reply? */
831 if (gctx->reply.op != BOOTP_REPLY)
832 continue;
834 /* Is this an answer to our query */
835 for (ifctx = gctx->interfaces;
836 ifctx != NULL;
837 ifctx = ifctx->next) {
838 if (gctx->reply.xid != ifctx->call.xid)
839 continue;
841 /* Same HW address size ? */
842 if (gctx->reply.hlen != ifctx->call.hlen)
843 continue;
845 /* Correct HW address ? */
846 if (bcmp(gctx->reply.chaddr,
847 ifctx->call.chaddr,
848 ifctx->call.hlen) != 0)
849 continue;
851 break;
854 if (ifctx != NULL) {
855 s = bootpc_tag(&gctx->tmptag,
856 &gctx->reply,
857 gctx->replylen,
858 TAG_DHCP_MSGTYPE);
859 if (s != NULL) {
860 switch (*s) {
861 case DHCP_OFFER:
862 s = "DHCP Offer";
863 break;
864 case DHCP_ACK:
865 s = "DHCP Ack";
866 break;
867 default:
868 s = "DHCP (unexpected)";
869 break;
871 } else
872 s = "BOOTP Reply";
874 kprintf("Received %s packet"
875 " on %s from ",
877 ifctx->ireq.ifr_name);
878 print_in_addr(gctx->reply.siaddr);
879 if (gctx->reply.giaddr.s_addr !=
880 htonl(INADDR_ANY)) {
881 kprintf(" via ");
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;
888 outstanding--;
890 /* Network settle delay */
891 if (outstanding == 0)
892 atimo = time_uptime +
893 BOOTP_SETTLE_DELAY;
894 } else
895 kprintf(" (ignored)");
896 if (ifctx->gotrootpath) {
897 gotrootpath = 1;
898 rtimo = time_uptime +
899 BOOTP_SETTLE_DELAY;
900 kprintf(" (got root path)");
901 } else
902 kprintf(" (no root path)");
903 kprintf("\n");
905 } /* while secs */
906 #ifdef BOOTP_TIMEOUT
907 if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
908 break;
909 #endif
910 /* Force a retry if halfway in DHCP negotiation */
911 retry = 0;
912 for (ifctx = gctx->interfaces; ifctx != NULL;
913 ifctx = ifctx->next) {
914 if (ifctx->state == IF_DHCP_OFFERED) {
915 if (ifctx->dhcpquerytype == DHCP_DISCOVER)
916 retry = 1;
917 else
918 ifctx->state = IF_DHCP_UNRESOLVED;
922 if (retry != 0)
923 continue;
925 if (gotrootpath != 0) {
926 gctx->gotrootpath = gotrootpath;
927 if (rtimo != 0 && time_uptime >= rtimo)
928 break;
930 } /* forever send/receive */
933 * XXX: These are errors of varying seriousness being silently
934 * ignored
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 ?
941 "DHCP" : "BOOTP",
942 ifctx->ireq.ifr_name);
945 if (gctx->gotrootpath != 0) {
946 #if 0
947 kprintf("Got a root path, ignoring remaining timeout\n");
948 #endif
949 error = 0;
950 goto out;
952 #ifndef BOOTP_NFSROOT
953 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
954 if (bootpc_ifctx_isresolved(ifctx) != 0) {
955 error = 0;
956 goto out;
959 #endif
960 error = ETIMEDOUT;
961 goto out;
963 gotreply:
964 out:
965 soclose(so, FNONBLOCK);
966 return error;
970 static int
971 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
972 struct bootpc_globalcontext *gctx,
973 struct thread *td)
975 struct ifaddr_container *ifac;
976 struct sockaddr_in *sin;
977 int error;
979 struct ifreq *ireq;
980 struct socket *so;
981 struct sockaddr_dl *sdl;
983 error = socreate(AF_INET, &ifctx->so, SOCK_DGRAM, 0, td);
984 if (error != 0)
985 panic("nfs_boot: socreate, error=%d", error);
987 ireq = &ifctx->ireq;
988 so = ifctx->so;
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);
997 if (error != 0)
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);
1001 if (error != 0)
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;
1012 clear_sinaddr(sin);
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;
1021 clear_sinaddr(sin);
1022 sin->sin_addr.s_addr = htonl(0xff000000u);
1023 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t)ireq, proc0.p_ucred);
1024 if (error != 0)
1025 panic("bootpc_fakeup_interface: set if netmask, error=%d",
1026 error);
1028 /* Broadcast is 255.255.255.255 */
1030 sin = (struct sockaddr_in *)&ireq->ifr_addr;
1031 clear_sinaddr(sin);
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",
1040 error);
1041 error = 0;
1043 /* Get HW address */
1045 sdl = NULL;
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)
1052 break;
1055 if (sdl == NULL)
1056 panic("bootpc: Unable to find HW address for %s",
1057 ifctx->ireq.ifr_name);
1058 ifctx->sdl = sdl;
1060 return error;
1064 static int
1065 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
1066 struct bootpc_globalcontext *gctx,
1067 struct thread *td)
1069 int error;
1070 struct sockaddr_in defdst;
1071 struct sockaddr_in defmask;
1072 struct sockaddr_in *sin;
1074 struct ifreq *ireq;
1075 struct socket *so;
1076 struct sockaddr_in *myaddr;
1077 struct sockaddr_in *netmask;
1078 struct sockaddr_in *gw;
1080 ireq = &ifctx->ireq;
1081 so = ifctx->so;
1082 myaddr = &ifctx->myaddr;
1083 netmask = &ifctx->netmask;
1084 gw = &ifctx->gw;
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);
1092 if (error != 0)
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);
1097 if (error != 0)
1098 panic("bootpc_adjust_interface: "
1099 "SIOCSIFFLAGS, error=%d", error);
1101 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1102 clear_sinaddr(sin);
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);
1109 return 0;
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);
1119 if (error != 0)
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;
1126 clear_sinaddr(sin);
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);
1130 if (error != 0)
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));
1149 if (error != 0) {
1150 kprintf("bootpc_adjust_interface: "
1151 "add net route, error=%d\n", error);
1152 return error;
1156 return 0;
1159 static void
1160 print_sin_addr(struct sockaddr_in *sin)
1162 print_in_addr(sin->sin_addr);
1166 static void
1167 print_in_addr(struct in_addr addr)
1169 unsigned int ip;
1171 ip = ntohl(addr.s_addr);
1172 kprintf("%d.%d.%d.%d",
1173 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1176 static void
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];
1182 uint32_t leasetime;
1183 uint8_t vendor_client_len;
1185 ifctx->gotrootpath = 0;
1187 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1189 /* bootpc part */
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)
1195 ifctx->xid++;
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 */
1201 *vendp++ = 130;
1202 *vendp++ = 83;
1203 *vendp++ = 99;
1204 *vendp++ = TAG_MAXMSGSIZE;
1205 *vendp++ = 2;
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;
1220 *vendp++ = 1;
1221 *vendp++ = DHCP_DISCOVER;
1222 ifctx->dhcpquerytype = DHCP_DISCOVER;
1223 ifctx->gotdhcpserver = 0;
1224 break;
1225 case IF_DHCP_OFFERED:
1226 *vendp++ = TAG_DHCP_MSGTYPE;
1227 *vendp++ = 1;
1228 *vendp++ = DHCP_REQUEST;
1229 ifctx->dhcpquerytype = DHCP_REQUEST;
1230 *vendp++ = TAG_DHCP_REQ_ADDR;
1231 *vendp++ = 4;
1232 memcpy(vendp, &ifctx->reply.yiaddr, 4);
1233 vendp += 4;
1234 if (ifctx->gotdhcpserver != 0) {
1235 *vendp++ = TAG_DHCP_SERVERID;
1236 *vendp++ = 4;
1237 memcpy(vendp, &ifctx->dhcpserver, 4);
1238 vendp += 4;
1240 *vendp++ = TAG_DHCP_LEASETIME;
1241 *vendp++ = 4;
1242 leasetime = htonl(300);
1243 memcpy(vendp, &leasetime, 4);
1244 vendp += 4;
1245 default:
1248 *vendp = TAG_END;
1250 ifctx->call.secs = 0;
1251 ifctx->call.flags = htons(0x8000); /* We need an broadcast answer */
1255 static int
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);
1263 static void
1264 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1265 unsigned char *start, int len, int tag)
1267 unsigned char *j;
1268 unsigned char *ej;
1269 unsigned char code;
1271 if (tctx->badtag != 0 || tctx->badopt != 0)
1272 return;
1274 j = start;
1275 ej = j + len;
1277 while (j < ej) {
1278 code = *j++;
1279 if (code == TAG_PAD)
1280 continue;
1281 if (code == TAG_END)
1282 return;
1283 if (j >= ej || j + *j + 1 > ej) {
1284 tctx->badopt = 1;
1285 return;
1287 len = *j++;
1288 if (code == tag) {
1289 if (tctx->taglen + len > TAG_MAXLEN) {
1290 tctx->badtag = 1;
1291 return;
1293 tctx->foundopt = 1;
1294 if (len > 0)
1295 memcpy(tctx->buf + tctx->taglen,
1296 j, len);
1297 tctx->taglen += len;
1299 if (code == TAG_OVERLOAD)
1300 tctx->overload = *j;
1302 j += len;
1307 static unsigned char *
1308 bootpc_tag(struct bootpc_tagcontext *tctx, struct bootp_packet *bp,
1309 int len, int tag)
1311 tctx->overload = 0;
1312 tctx->badopt = 0;
1313 tctx->badtag = 0;
1314 tctx->foundopt = 0;
1315 tctx->taglen = 0;
1317 if (bootpc_hascookie(bp) == 0)
1318 return NULL;
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,
1326 sizeof(bp->file),
1327 tag);
1328 if ((tctx->overload & OVERLOAD_SNAME) != 0)
1329 bootpc_tag_helper(tctx,
1330 (unsigned char *) bp->sname,
1331 sizeof(bp->sname),
1332 tag);
1334 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1335 return NULL;
1336 tctx->buf[tctx->taglen] = '\0';
1337 return tctx->buf;
1341 static void
1342 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1343 struct bootpc_globalcontext *gctx)
1345 char *p;
1346 unsigned int ip;
1348 ifctx->gotgw = 0;
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),
1359 "swap.%d.%d.%d.%d",
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);
1384 kprintf("\n");
1386 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1387 TAG_SUBNETMASK);
1388 if (p != NULL) {
1389 if (gctx->tag.taglen != 4)
1390 panic("bootpc: subnet mask len is %d",
1391 gctx->tag.taglen);
1392 bcopy(p, &ifctx->netmask.sin_addr, 4);
1393 ifctx->gotnetmask = 1;
1394 kprintf("subnet mask ");
1395 print_sin_addr(&ifctx->netmask);
1396 kprintf(" ");
1399 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1400 TAG_ROUTERS);
1401 if (p != NULL) {
1402 /* Routers */
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);
1407 kprintf("router ");
1408 print_sin_addr(&ifctx->gw);
1409 kprintf(" ");
1410 ifctx->gotgw = 1;
1411 gctx->gotgw = 1;
1415 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1416 TAG_ROOT);
1417 if (p != NULL) {
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,
1428 ifctx->replylen,
1429 TAG_ROOTOPTS);
1430 if (p != NULL) {
1431 nfs_mountopts(&nd->root_args, p);
1432 kprintf("rootopts %s ", p);
1434 } else
1435 panic("Failed to set rootfs to %s",p);
1438 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1439 TAG_SWAP);
1440 if (p != NULL) {
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,
1450 ifctx->replylen,
1451 TAG_SWAPOPTS);
1452 if (p != NULL) {
1453 /* swap mount options */
1454 nfs_mountopts(&nd->swap_args, p);
1455 kprintf("swapopts %s ", p);
1458 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1459 ifctx->replylen,
1460 TAG_SWAPSIZE);
1461 if (p != NULL) {
1462 int swaplen;
1463 if (gctx->tag.taglen != 4)
1464 panic("bootpc: "
1465 "Expected 4 bytes for swaplen, "
1466 "not %d bytes",
1467 gctx->tag.taglen);
1468 bcopy(p, &swaplen, 4);
1469 nd->swap_nblks = ntohl(swaplen);
1470 kprintf("swapsize %d KB ",
1471 nd->swap_nblks);
1473 } else
1474 panic("Failed to set swapfs to %s", p);
1477 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1478 TAG_HOSTNAME);
1479 if (p != NULL) {
1480 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1481 panic("bootpc: hostname >= %d bytes",
1482 MAXHOSTNAMELEN);
1483 if (gctx->sethostname != NULL) {
1484 kprintf("hostname %s (ignored) ", p);
1485 } else {
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,
1493 TAG_COOKIE);
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];
1498 p[i] = '\0';
1501 kprintf("\n");
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);
1508 else
1509 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1511 if (ifctx->gotgw == 0) {
1512 /* Use proxyarp */
1513 ifctx->gw.sin_addr.s_addr = ifctx->myaddr.sin_addr.s_addr;
1517 void
1518 bootpc_init(void)
1520 struct bootpc_ifcontext *ifctx, *nctx; /* Interface BOOTP contexts */
1521 struct bootpc_globalcontext *gctx; /* Global BOOTP context */
1522 struct ifnet *ifp;
1523 int error;
1524 struct nfsv3_diskless *nd;
1525 struct thread *td;
1527 nd = &nfsv3_diskless;
1528 td = curthread;
1531 * If already filled in, don't touch it here
1533 if (nfs_diskless_valid != 0)
1534 return;
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));
1549 #endif
1550 bzero(&ifctx->ireq, sizeof(ifctx->ireq));
1551 /* XXX ALMOST MPSAFE */
1552 ifnet_lock();
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)
1559 continue;
1560 #else
1561 if ((ifp->if_flags &
1562 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1563 IFF_BROADCAST)
1564 continue;
1565 #endif
1566 if (gctx->interfaces != NULL)
1567 gctx->lastinterface->next = ifctx;
1568 else
1569 gctx->interfaces = ifctx;
1570 ifctx->ifp = ifp;
1571 gctx->lastinterface = ifctx;
1572 ifctx = allocifctx(gctx);
1574 ifnet_unlock();
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));
1582 #else
1583 panic("bootpc_init: no suitable interface");
1584 #endif
1587 gctx->gotrootpath = 0;
1588 gctx->gotswappath = 0;
1589 gctx->gotgw = 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);
1600 if (error != 0) {
1601 #ifdef BOOTP_NFSROOT
1602 panic("BOOTP call failed");
1603 #else
1604 kprintf("BOOTP call failed\n");
1605 #endif
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)
1617 nd->swap_nblks = 0;
1618 #ifdef BOOTP_NFSROOT
1619 if (gctx->gotrootpath == 0)
1620 panic("bootpc: No root path offered");
1621 #endif
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)
1631 break;
1632 if (ifctx == NULL) {
1633 for (ifctx = gctx->interfaces;
1634 ifctx != NULL;
1635 ifctx = ifctx->next)
1636 if (bootpc_ifctx_isresolved(ifctx) != 0)
1637 break;
1639 if (ifctx == NULL)
1640 goto out;
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);
1647 if (error != 0)
1648 panic("nfs_boot: mountd root, error=%d", error);
1650 if (gctx->gotswappath != 0) {
1652 error = md_mount(&nd->swap_saddr,
1653 nd->swap_hostnam,
1654 nd->swap_fh, &nd->swap_fhsize,
1655 &nd->swap_args, td);
1656 if (error != 0)
1657 panic("nfs_boot: mountd swap, error=%d",
1658 error);
1660 error = md_lookup_swap(&nd->swap_saddr,
1661 gctx->lookup_path,
1662 nd->swap_fh, &nd->swap_fhsize,
1663 &nd->swap_args, td);
1664 if (error != 0)
1665 panic("nfs_boot: lookup swap, error=%d",
1666 error);
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));
1679 out:
1680 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = nctx) {
1681 nctx = ifctx->next;
1682 kfree(ifctx, M_TEMP);
1684 kfree(gctx, M_TEMP);