1 /*-
2 * Copyright (c) 1995 Gordon Ross, Adam Glass
3 * Copyright (c) 1992 Regents of the University of California.
4 * All rights reserved.
5 *
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.
9 *
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.
25 *
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.
37 *
38 * based on:
39 * nfs/krpc_subr.c
40 * $NetBSD: krpc_subr.c,v 1.10 1995/08/08 20:43:43 gwr Exp $
41 */
42
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD: releng/6.3/sys/nfsclient/bootp_subr.c 173165 2007-10-30 00:49:41Z sam $");
45
46 #include "opt_bootp.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/sockio.h>
52 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/mbuf.h>
55 #include <sys/proc.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/sysctl.h>
59 #include <sys/uio.h>
60
61 #include <net/if.h>
62 #include <net/route.h>
63
64 #include <netinet/in.h>
65 #include <net/if_types.h>
66 #include <net/if_dl.h>
67
68 #include <rpc/rpcclnt.h>
69
70 #include <nfs/rpcv2.h>
71 #include <nfs/nfsproto.h>
72 #include <nfsclient/nfs.h>
73 #include <nfsclient/nfsdiskless.h>
74 #include <nfsclient/krpc.h>
75 #include <nfs/xdr_subs.h>
76
77
78 #define BOOTP_MIN_LEN 300 /* Minimum size of bootp udp packet */
79
80 #ifndef BOOTP_SETTLE_DELAY
81 #define BOOTP_SETTLE_DELAY 3
82 #endif
83
84 /*
85 * What is the longest we will wait before re-sending a request?
86 * Note this is also the frequency of "RPC timeout" messages.
87 * The re-send loop count sup linearly to this maximum, so the
88 * first complaint will happen after (1+2+3+4+5)=15 seconds.
89 */
90 #define MAX_RESEND_DELAY 5 /* seconds */
91
92 /* Definitions from RFC951 */
93 struct bootp_packet {
94 u_int8_t op;
95 u_int8_t htype;
96 u_int8_t hlen;
97 u_int8_t hops;
98 u_int32_t xid;
99 u_int16_t secs;
100 u_int16_t flags;
101 struct in_addr ciaddr;
102 struct in_addr yiaddr;
103 struct in_addr siaddr;
104 struct in_addr giaddr;
105 unsigned char chaddr[16];
106 char sname[64];
107 char file[128];
108 unsigned char vend[1222];
109 };
110
111 struct bootpc_ifcontext {
112 struct bootpc_ifcontext *next;
113 struct bootp_packet call;
114 struct bootp_packet reply;
115 int replylen;
116 int overload;
117 struct socket *so;
118 struct ifreq ireq;
119 struct ifnet *ifp;
120 struct sockaddr_dl *sdl;
121 struct sockaddr_in myaddr;
122 struct sockaddr_in netmask;
123 struct sockaddr_in gw;
124 struct sockaddr_in broadcast; /* Different for each interface */
125 int gotgw;
126 int gotnetmask;
127 int gotrootpath;
128 int outstanding;
129 int sentmsg;
130 u_int32_t xid;
131 enum {
132 IF_BOOTP_UNRESOLVED,
133 IF_BOOTP_RESOLVED,
134 IF_BOOTP_FAILED,
135 IF_DHCP_UNRESOLVED,
136 IF_DHCP_OFFERED,
137 IF_DHCP_RESOLVED,
138 IF_DHCP_FAILED,
139 } state;
140 int dhcpquerytype; /* dhcp type sent */
141 struct in_addr dhcpserver;
142 int gotdhcpserver;
143 };
144
145 #define TAG_MAXLEN 1024
146 struct bootpc_tagcontext {
147 char buf[TAG_MAXLEN + 1];
148 int overload;
149 int badopt;
150 int badtag;
151 int foundopt;
152 int taglen;
153 };
154
155 struct bootpc_globalcontext {
156 struct bootpc_ifcontext *interfaces;
157 struct bootpc_ifcontext *lastinterface;
158 u_int32_t xid;
159 int gotrootpath;
160 int gotgw;
161 int ifnum;
162 int secs;
163 int starttime;
164 struct bootp_packet reply;
165 int replylen;
166 struct bootpc_ifcontext *setrootfs;
167 struct bootpc_ifcontext *sethostname;
168 struct bootpc_tagcontext tmptag;
169 struct bootpc_tagcontext tag;
170 };
171
172 #define IPPORT_BOOTPC 68
173 #define IPPORT_BOOTPS 67
174
175 #define BOOTP_REQUEST 1
176 #define BOOTP_REPLY 2
177
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 */
184
185 /* DHCP specific tags */
186 #define TAG_OVERLOAD 52 /* Option Overload */
187 #define TAG_MAXMSGSIZE 57 /* Maximum DHCP Message Size */
188
189 #define TAG_END 255 /* End Option (i.e. no more options) */
190
191 /* Overload values */
192 #define OVERLOAD_FILE 1
193 #define OVERLOAD_SNAME 2
194
195 /* Site specific tags: */
196 #define TAG_ROOTOPTS 130
197 #define TAG_COOKIE 134 /* ascii info for userland, via sysctl */
198
199 #define TAG_DHCP_MSGTYPE 53
200 #define TAG_DHCP_REQ_ADDR 50
201 #define TAG_DHCP_SERVERID 54
202 #define TAG_DHCP_LEASETIME 51
203
204 #define TAG_VENDOR_INDENTIFIER 60
205
206 #define DHCP_NOMSG 0
207 #define DHCP_DISCOVER 1
208 #define DHCP_OFFER 2
209 #define DHCP_REQUEST 3
210 #define DHCP_ACK 5
211
212 static char bootp_cookie[128];
213 SYSCTL_STRING(_kern, OID_AUTO, bootp_cookie, CTLFLAG_RD,
214 bootp_cookie, 0, "Cookie (T134) supplied by bootp server");
215
216 /* mountd RPC */
217 static int md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp,
218 int *fhsizep, struct nfs_args *args, struct thread *td);
219 static int setfs(struct sockaddr_in *addr, char *path, char *p,
220 const struct in_addr *siaddr);
221 static int getdec(char **ptr);
222 static int getip(char **ptr, struct in_addr *ip);
223 static void mountopts(struct nfs_args *args, char *p);
224 static int xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len);
225 static int xdr_int_decode(struct mbuf **ptr, int *iptr);
226 static void print_in_addr(struct in_addr addr);
227 static void print_sin_addr(struct sockaddr_in *addr);
228 static void clear_sinaddr(struct sockaddr_in *sin);
229 static void allocifctx(struct bootpc_globalcontext *gctx);
230 static void bootpc_compose_query(struct bootpc_ifcontext *ifctx,
231 struct bootpc_globalcontext *gctx, struct thread *td);
232 static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx,
233 struct bootp_packet *bp, int len, int tag);
234 static void bootpc_tag_helper(struct bootpc_tagcontext *tctx,
235 unsigned char *start, int len, int tag);
236
237 #ifdef BOOTP_DEBUG
238 void bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma);
239 void bootpboot_p_ma(struct sockaddr *ma);
240 void bootpboot_p_rtentry(struct rtentry *rt);
241 void bootpboot_p_tree(struct radix_node *rn);
242 void bootpboot_p_rtlist(void);
243 void bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa);
244 void bootpboot_p_iflist(void);
245 #endif
246
247 static int bootpc_call(struct bootpc_globalcontext *gctx,
248 struct thread *td);
249
250 static int bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
251 struct bootpc_globalcontext *gctx, struct thread *td);
252
253 static int bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
254 struct bootpc_globalcontext *gctx, struct thread *td);
255
256 static void bootpc_decode_reply(struct nfsv3_diskless *nd,
257 struct bootpc_ifcontext *ifctx,
258 struct bootpc_globalcontext *gctx);
259
260 static int bootpc_received(struct bootpc_globalcontext *gctx,
261 struct bootpc_ifcontext *ifctx);
262
263 static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
264 static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
265 static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
266
267 /*
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:
271 *
272 * - The 'broadcast' address is different for each interface.
273 *
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.
277 */
278
279 #ifdef BOOTP_DEBUG
280 void
281 bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma)
282 {
283
284 if (sa == NULL) {
285 printf("(sockaddr *) <null>");
286 return;
287 }
288 switch (sa->sa_family) {
289 case AF_INET:
290 {
291 struct sockaddr_in *sin;
292
293 sin = (struct sockaddr_in *) sa;
294 printf("inet ");
295 print_sin_addr(sin);
296 if (ma != NULL) {
297 sin = (struct sockaddr_in *) ma;
298 printf(" mask ");
299 print_sin_addr(sin);
300 }
301 }
302 break;
303 case AF_LINK:
304 {
305 struct sockaddr_dl *sli;
306 int i;
307
308 sli = (struct sockaddr_dl *) sa;
309 printf("link %.*s ", sli->sdl_nlen, sli->sdl_data);
310 for (i = 0; i < sli->sdl_alen; i++) {
311 if (i > 0)
312 printf(":");
313 printf("%x", ((unsigned char *) LLADDR(sli))[i]);
314 }
315 }
316 break;
317 default:
318 printf("af%d", sa->sa_family);
319 }
320 }
321
322 void
323 bootpboot_p_ma(struct sockaddr *ma)
324 {
325
326 if (ma == NULL) {
327 printf("<null>");
328 return;
329 }
330 printf("%x", *(int *)ma);
331 }
332
333 void
334 bootpboot_p_rtentry(struct rtentry *rt)
335 {
336
337 bootpboot_p_sa(rt_key(rt), rt_mask(rt));
338 printf(" ");
339 bootpboot_p_ma(rt->rt_genmask);
340 printf(" ");
341 bootpboot_p_sa(rt->rt_gateway, NULL);
342 printf(" ");
343 printf("flags %x", (unsigned short) rt->rt_flags);
344 printf(" %d", (int) rt->rt_rmx.rmx_expire);
345 printf(" %s\n", rt->rt_ifp->if_xname);
346 }
347
348 void
349 bootpboot_p_tree(struct radix_node *rn)
350 {
351
352 while (rn != NULL) {
353 if (rn->rn_bit < 0) {
354 if ((rn->rn_flags & RNF_ROOT) != 0) {
355 } else {
356 bootpboot_p_rtentry((struct rtentry *) rn);
357 }
358 rn = rn->rn_dupedkey;
359 } else {
360 bootpboot_p_tree(rn->rn_left);
361 bootpboot_p_tree(rn->rn_right);
362 return;
363 }
364 }
365 }
366
367 void
368 bootpboot_p_rtlist(void)
369 {
370
371 printf("Routing table:\n");
372 RADIX_NODE_LOCK(rt_tables[AF_INET]); /* could sleep XXX */
373 bootpboot_p_tree(rt_tables[AF_INET]->rnh_treetop);
374 RADIX_NODE_UNLOCK(rt_tables[AF_INET]);
375 }
376
377 void
378 bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
379 {
380
381 printf("%s flags %x, addr ",
382 ifp->if_xname, ifp->if_flags);
383 print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
384 printf(", broadcast ");
385 print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
386 printf(", netmask ");
387 print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
388 printf("\n");
389 }
390
391 void
392 bootpboot_p_iflist(void)
393 {
394 struct ifnet *ifp;
395 struct ifaddr *ifa;
396
397 printf("Interface list:\n");
398 IFNET_RLOCK(); /* could sleep, but okay for debugging XXX */
399 for (ifp = TAILQ_FIRST(&ifnet);
400 ifp != NULL;
401 ifp = TAILQ_NEXT(ifp, if_link)) {
402 for (ifa = TAILQ_FIRST(&ifp->if_addrhead);
403 ifa != NULL;
404 ifa = TAILQ_NEXT(ifa, ifa_link))
405 if (ifa->ifa_addr->sa_family == AF_INET)
406 bootpboot_p_if(ifp, ifa);
407 }
408 IFNET_RUNLOCK();
409 }
410 #endif /* defined(BOOTP_DEBUG) */
411
412 static void
413 clear_sinaddr(struct sockaddr_in *sin)
414 {
415
416 bzero(sin, sizeof(*sin));
417 sin->sin_len = sizeof(*sin);
418 sin->sin_family = AF_INET;
419 sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
420 sin->sin_port = 0;
421 }
422
423 static void
424 allocifctx(struct bootpc_globalcontext *gctx)
425 {
426 struct bootpc_ifcontext *ifctx;
427 ifctx = (struct bootpc_ifcontext *) malloc(sizeof(*ifctx),
428 M_TEMP, M_WAITOK | M_ZERO);
429 if (ifctx == NULL)
430 panic("Failed to allocate bootp interface context structure");
431
432 ifctx->xid = gctx->xid;
433 #ifdef BOOTP_NO_DHCP
434 ifctx->state = IF_BOOTP_UNRESOLVED;
435 #else
436 ifctx->state = IF_DHCP_UNRESOLVED;
437 #endif
438 gctx->xid += 0x100;
439 if (gctx->interfaces != NULL)
440 gctx->lastinterface->next = ifctx;
441 else
442 gctx->interfaces = ifctx;
443 gctx->lastinterface = ifctx;
444 }
445
446 static __inline int
447 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
448 {
449
450 if (ifctx->state == IF_BOOTP_RESOLVED ||
451 ifctx->state == IF_DHCP_RESOLVED)
452 return 1;
453 return 0;
454 }
455
456 static __inline int
457 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
458 {
459
460 if (ifctx->state == IF_BOOTP_UNRESOLVED ||
461 ifctx->state == IF_DHCP_UNRESOLVED)
462 return 1;
463 return 0;
464 }
465
466 static __inline int
467 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
468 {
469
470 if (ifctx->state == IF_BOOTP_FAILED ||
471 ifctx->state == IF_DHCP_FAILED)
472 return 1;
473 return 0;
474 }
475
476 static int
477 bootpc_received(struct bootpc_globalcontext *gctx,
478 struct bootpc_ifcontext *ifctx)
479 {
480 unsigned char dhcpreplytype;
481 char *p;
482
483 /*
484 * Need timeout for fallback to less
485 * desirable alternative.
486 */
487
488 /* This call used for the side effect (badopt flag) */
489 (void) bootpc_tag(&gctx->tmptag, &gctx->reply,
490 gctx->replylen,
491 TAG_END);
492
493 /* If packet is invalid, ignore it */
494 if (gctx->tmptag.badopt != 0)
495 return 0;
496
497 p = bootpc_tag(&gctx->tmptag, &gctx->reply,
498 gctx->replylen, TAG_DHCP_MSGTYPE);
499 if (p != NULL)
500 dhcpreplytype = *p;
501 else
502 dhcpreplytype = DHCP_NOMSG;
503
504 switch (ifctx->dhcpquerytype) {
505 case DHCP_DISCOVER:
506 if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */
507 #ifndef BOOTP_FORCE_DHCP
508 && dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */
509 #endif
510 )
511 return 0;
512 break;
513 case DHCP_REQUEST:
514 if (dhcpreplytype != DHCP_ACK)
515 return 0;
516 case DHCP_NOMSG:
517 break;
518 }
519
520 /* Ignore packet unless it gives us a root tag we didn't have */
521
522 if ((ifctx->state == IF_BOOTP_RESOLVED ||
523 (ifctx->dhcpquerytype == DHCP_DISCOVER &&
524 (ifctx->state == IF_DHCP_OFFERED ||
525 ifctx->state == IF_DHCP_RESOLVED))) &&
526 (bootpc_tag(&gctx->tmptag, &ifctx->reply,
527 ifctx->replylen,
528 TAG_ROOT) != NULL ||
529 bootpc_tag(&gctx->tmptag, &gctx->reply,
530 gctx->replylen,
531 TAG_ROOT) == NULL))
532 return 0;
533
534 bcopy(&gctx->reply, &ifctx->reply, gctx->replylen);
535 ifctx->replylen = gctx->replylen;
536
537 /* XXX: Only reset if 'perfect' response */
538 if (ifctx->state == IF_BOOTP_UNRESOLVED)
539 ifctx->state = IF_BOOTP_RESOLVED;
540 else if (ifctx->state == IF_DHCP_UNRESOLVED &&
541 ifctx->dhcpquerytype == DHCP_DISCOVER) {
542 if (dhcpreplytype == DHCP_OFFER)
543 ifctx->state = IF_DHCP_OFFERED;
544 else
545 ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */
546 } else if (ifctx->state == IF_DHCP_OFFERED &&
547 ifctx->dhcpquerytype == DHCP_REQUEST)
548 ifctx->state = IF_DHCP_RESOLVED;
549
550
551 if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
552 ifctx->state != IF_BOOTP_RESOLVED) {
553 p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
554 ifctx->replylen, TAG_DHCP_SERVERID);
555 if (p != NULL && gctx->tmptag.taglen == 4) {
556 memcpy(&ifctx->dhcpserver, p, 4);
557 ifctx->gotdhcpserver = 1;
558 } else
559 ifctx->gotdhcpserver = 0;
560 return 1;
561 }
562
563 ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
564 ifctx->replylen,
565 TAG_ROOT) != NULL);
566 ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
567 ifctx->replylen,
568 TAG_ROUTERS) != NULL);
569 ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
570 ifctx->replylen,
571 TAG_SUBNETMASK) != NULL);
572 return 1;
573 }
574
575 static int
576 bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
577 {
578 struct socket *so;
579 struct sockaddr_in *sin, dst;
580 struct uio auio;
581 struct sockopt sopt;
582 struct iovec aio;
583 int error, on, rcvflg, timo, len;
584 time_t atimo;
585 time_t rtimo;
586 struct timeval tv;
587 struct bootpc_ifcontext *ifctx;
588 int outstanding;
589 int gotrootpath;
590 int retry;
591 const char *s;
592
593 NET_ASSERT_GIANT();
594
595 /*
596 * Create socket and set its recieve timeout.
597 */
598 error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td->td_ucred, td);
599 if (error != 0)
600 goto out0;
601
602 tv.tv_sec = 1;
603 tv.tv_usec = 0;
604 bzero(&sopt, sizeof(sopt));
605 sopt.sopt_dir = SOPT_SET;
606 sopt.sopt_level = SOL_SOCKET;
607 sopt.sopt_name = SO_RCVTIMEO;
608 sopt.sopt_val = &tv;
609 sopt.sopt_valsize = sizeof tv;
610
611 error = sosetopt(so, &sopt);
612 if (error != 0)
613 goto out;
614
615 /*
616 * Enable broadcast.
617 */
618 on = 1;
619 sopt.sopt_name = SO_BROADCAST;
620 sopt.sopt_val = &on;
621 sopt.sopt_valsize = sizeof on;
622
623 error = sosetopt(so, &sopt);
624 if (error != 0)
625 goto out;
626
627 /*
628 * Disable routing.
629 */
630
631 on = 1;
632 sopt.sopt_name = SO_DONTROUTE;
633 sopt.sopt_val = &on;
634 sopt.sopt_valsize = sizeof on;
635
636 error = sosetopt(so, &sopt);
637 if (error != 0)
638 goto out;
639
640 /*
641 * Bind the local endpoint to a bootp client port.
642 */
643 sin = &dst;
644 clear_sinaddr(sin);
645 sin->sin_port = htons(IPPORT_BOOTPC);
646 error = sobind(so, (struct sockaddr *)sin, td);
647 if (error != 0) {
648 printf("bind failed\n");
649 goto out;
650 }
651
652 /*
653 * Setup socket address for the server.
654 */
655 sin = &dst;
656 clear_sinaddr(sin);
657 sin->sin_addr.s_addr = INADDR_BROADCAST;
658 sin->sin_port = htons(IPPORT_BOOTPS);
659
660 /*
661 * Send it, repeatedly, until a reply is received,
662 * but delay each re-send by an increasing amount.
663 * If the delay hits the maximum, start complaining.
664 */
665 timo = 0;
666 rtimo = 0;
667 for (;;) {
668
669 outstanding = 0;
670 gotrootpath = 0;
671
672 for (ifctx = gctx->interfaces;
673 ifctx != NULL;
674 ifctx = ifctx->next) {
675 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
676 bootpc_tag(&gctx->tmptag, &ifctx->reply,
677 ifctx->replylen,
678 TAG_ROOT) != NULL)
679 gotrootpath = 1;
680 }
681
682 for (ifctx = gctx->interfaces;
683 ifctx != NULL;
684 ifctx = ifctx->next) {
685 ifctx->outstanding = 0;
686 if (bootpc_ifctx_isresolved(ifctx) != 0 &&
687 gotrootpath != 0) {
688 continue;
689 }
690 if (bootpc_ifctx_isfailed(ifctx) != 0)
691 continue;
692
693 outstanding++;
694 ifctx->outstanding = 1;
695
696 /* Proceed to next step in DHCP negotiation */
697 if ((ifctx->state == IF_DHCP_OFFERED &&
698 ifctx->dhcpquerytype != DHCP_REQUEST) ||
699 (ifctx->state == IF_DHCP_UNRESOLVED &&
700 ifctx->dhcpquerytype != DHCP_DISCOVER) ||
701 (ifctx->state == IF_BOOTP_UNRESOLVED &&
702 ifctx->dhcpquerytype != DHCP_NOMSG)) {
703 ifctx->sentmsg = 0;
704 bootpc_compose_query(ifctx, gctx, td);
705 }
706
707 /* Send BOOTP request (or re-send). */
708
709 if (ifctx->sentmsg == 0) {
710 switch(ifctx->dhcpquerytype) {
711 case DHCP_DISCOVER:
712 s = "DHCP Discover";
713 break;
714 case DHCP_REQUEST:
715 s = "DHCP Request";
716 break;
717 case DHCP_NOMSG:
718 default:
719 s = "BOOTP Query";
720 break;
721 }
722 printf("Sending %s packet from "
723 "interface %s (%*D)\n",
724 s,
725 ifctx->ireq.ifr_name,
726 ifctx->sdl->sdl_alen,
727 (unsigned char *) LLADDR(ifctx->sdl),
728 ":");
729 ifctx->sentmsg = 1;
730 }
731
732 aio.iov_base = (caddr_t) &ifctx->call;
733 aio.iov_len = sizeof(ifctx->call);
734
735 auio.uio_iov = &aio;
736 auio.uio_iovcnt = 1;
737 auio.uio_segflg = UIO_SYSSPACE;
738 auio.uio_rw = UIO_WRITE;
739 auio.uio_offset = 0;
740 auio.uio_resid = sizeof(ifctx->call);
741 auio.uio_td = td;
742
743 /* Set netmask to 0.0.0.0 */
744
745 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
746 clear_sinaddr(sin);
747 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
748 (caddr_t) &ifctx->ireq, td);
749 if (error != 0)
750 panic("bootpc_call:"
751 "set if netmask, error=%d",
752 error);
753
754 error = sosend(so, (struct sockaddr *) &dst,
755 &auio, NULL, NULL, 0, td);
756 if (error != 0) {
757 printf("bootpc_call: sosend: %d state %08x\n",
758 error, (int) so->so_state);
759 }
760
761 /* XXX: Is this needed ? */
762 tsleep(&error, PZERO + 8, "bootpw", 10);
763
764 /* Set netmask to 255.0.0.0 */
765
766 sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
767 clear_sinaddr(sin);
768 sin->sin_addr.s_addr = htonl(0xff000000u);
769 error = ifioctl(ifctx->so, SIOCSIFNETMASK,
770 (caddr_t) &ifctx->ireq, td);
771 if (error != 0)
772 panic("bootpc_call:"
773 "set if netmask, error=%d",
774 error);
775
776 }
777
778 if (outstanding == 0 &&
779 (rtimo == 0 || time_second >= rtimo)) {
780 error = 0;
781 goto gotreply;
782 }
783
784 /* Determine new timeout. */
785 if (timo < MAX_RESEND_DELAY)
786 timo++;
787 else {
788 printf("DHCP/BOOTP timeout for server ");
789 print_sin_addr(&dst);
790 printf("\n");
791 }
792
793 /*
794 * Wait for up to timo seconds for a reply.
795 * The socket receive timeout was set to 1 second.
796 */
797 atimo = timo + time_second;
798 while (time_second < atimo) {
799 aio.iov_base = (caddr_t) &gctx->reply;
800 aio.iov_len = sizeof(gctx->reply);
801
802 auio.uio_iov = &aio;
803 auio.uio_iovcnt = 1;
804 auio.uio_segflg = UIO_SYSSPACE;
805 auio.uio_rw = UIO_READ;
806 auio.uio_offset = 0;
807 auio.uio_resid = sizeof(gctx->reply);
808 auio.uio_td = td;
809
810 rcvflg = 0;
811 error = soreceive(so, NULL, &auio,
812 NULL, NULL, &rcvflg);
813 gctx->secs = time_second - gctx->starttime;
814 for (ifctx = gctx->interfaces;
815 ifctx != NULL;
816 ifctx = ifctx->next) {
817 if (bootpc_ifctx_isresolved(ifctx) != 0 ||
818 bootpc_ifctx_isfailed(ifctx) != 0)
819 continue;
820
821 ifctx->call.secs = htons(gctx->secs);
822 }
823 if (error == EWOULDBLOCK)
824 continue;
825 if (error != 0)
826 goto out;
827 len = sizeof(gctx->reply) - auio.uio_resid;
828
829 /* Do we have the required number of bytes ? */
830 if (len < BOOTP_MIN_LEN)
831 continue;
832 gctx->replylen = len;
833
834 /* Is it a reply? */
835 if (gctx->reply.op != BOOTP_REPLY)
836 continue;
837
838 /* Is this an answer to our query */
839 for (ifctx = gctx->interfaces;
840 ifctx != NULL;
841 ifctx = ifctx->next) {
842 if (gctx->reply.xid != ifctx->call.xid)
843 continue;
844
845 /* Same HW address size ? */
846 if (gctx->reply.hlen != ifctx->call.hlen)
847 continue;
848
849 /* Correct HW address ? */
850 if (bcmp(gctx->reply.chaddr,
851 ifctx->call.chaddr,
852 ifctx->call.hlen) != 0)
853 continue;
854
855 break;
856 }
857
858 if (ifctx != NULL) {
859 s = bootpc_tag(&gctx->tmptag,
860 &gctx->reply,
861 gctx->replylen,
862 TAG_DHCP_MSGTYPE);
863 if (s != NULL) {
864 switch (*s) {
865 case DHCP_OFFER:
866 s = "DHCP Offer";
867 break;
868 case DHCP_ACK:
869 s = "DHCP Ack";
870 break;
871 default:
872 s = "DHCP (unexpected)";
873 break;
874 }
875 } else
876 s = "BOOTP Reply";
877
878 printf("Received %s packet"
879 " on %s from ",
880 s,
881 ifctx->ireq.ifr_name);
882 print_in_addr(gctx->reply.siaddr);
883 if (gctx->reply.giaddr.s_addr !=
884 htonl(INADDR_ANY)) {
885 printf(" via ");
886 print_in_addr(gctx->reply.giaddr);
887 }
888 if (bootpc_received(gctx, ifctx) != 0) {
889 printf(" (accepted)");
890 if (ifctx->outstanding) {
891 ifctx->outstanding = 0;
892 outstanding--;
893 }
894 /* Network settle delay */
895 if (outstanding == 0)
896 atimo = time_second +
897 BOOTP_SETTLE_DELAY;
898 } else
899 printf(" (ignored)");
900 if (ifctx->gotrootpath) {
901 gotrootpath = 1;
902 rtimo = time_second +
903 BOOTP_SETTLE_DELAY;
904 printf(" (got root path)");
905 } else
906 printf(" (no root path)");
907 printf("\n");
908 }
909 } /* while secs */
910 #ifdef BOOTP_TIMEOUT
911 if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
912 break;
913 #endif
914 /* Force a retry if halfway in DHCP negotiation */
915 retry = 0;
916 for (ifctx = gctx->interfaces; ifctx != NULL;
917 ifctx = ifctx->next) {
918 if (ifctx->state == IF_DHCP_OFFERED) {
919 if (ifctx->dhcpquerytype == DHCP_DISCOVER)
920 retry = 1;
921 else
922 ifctx->state = IF_DHCP_UNRESOLVED;
923 }
924 }
925
926 if (retry != 0)
927 continue;
928
929 if (gotrootpath != 0) {
930 gctx->gotrootpath = gotrootpath;
931 if (rtimo != 0 && time_second >= rtimo)
932 break;
933 }
934 } /* forever send/receive */
935
936 /*
937 * XXX: These are errors of varying seriousness being silently
938 * ignored
939 */
940
941 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
942 if (bootpc_ifctx_isresolved(ifctx) == 0) {
943 printf("%s timeout for interface %s\n",
944 ifctx->dhcpquerytype != DHCP_NOMSG ?
945 "DHCP" : "BOOTP",
946 ifctx->ireq.ifr_name);
947 }
948 }
949 if (gctx->gotrootpath != 0) {
950 #if 0
951 printf("Got a root path, ignoring remaining timeout\n");
952 #endif
953 error = 0;
954 goto out;
955 }
956 #ifndef BOOTP_NFSROOT
957 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
958 if (bootpc_ifctx_isresolved(ifctx) != 0) {
959 error = 0;
960 goto out;
961 }
962 }
963 #endif
964 error = ETIMEDOUT;
965 goto out;
966
967 gotreply:
968 out:
969 soclose(so);
970 out0:
971 return error;
972 }
973
974 static int
975 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
976 struct bootpc_globalcontext *gctx, struct thread *td)
977 {
978 struct sockaddr_in *sin;
979 int error;
980 struct ifreq *ireq;
981 struct socket *so;
982 struct ifaddr *ifa;
983 struct sockaddr_dl *sdl;
984
985 NET_ASSERT_GIANT();
986
987 error = socreate(AF_INET, &ifctx->so, SOCK_DGRAM, 0, td->td_ucred, td);
988 if (error != 0)
989 panic("nfs_boot: socreate, error=%d", error);
990
991 ireq = &ifctx->ireq;
992 so = ifctx->so;
993
994 /*
995 * Bring up the interface.
996 *
997 * Get the old interface flags and or IFF_UP into them; if
998 * IFF_UP set blindly, interface selection can be clobbered.
999 */
1000 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, td);
1001 if (error != 0)
1002 panic("bootpc_fakeup_interface: GIFFLAGS, error=%d", error);
1003 ireq->ifr_flags |= IFF_UP;
1004 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, td);
1005 if (error != 0)
1006 panic("bootpc_fakeup_interface: SIFFLAGS, error=%d", error);
1007
1008 /*
1009 * Do enough of ifconfig(8) so that the chosen interface
1010 * can talk to the servers. (just set the address)
1011 */
1012
1013 /* addr is 0.0.0.0 */
1014
1015 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1016 clear_sinaddr(sin);
1017 error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, td);
1018 if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1019 panic("bootpc_fakeup_interface: "
1020 "set if addr, error=%d", error);
1021
1022 /* netmask is 255.0.0.0 */
1023
1024 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1025 clear_sinaddr(sin);
1026 sin->sin_addr.s_addr = htonl(0xff000000u);
1027 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t)ireq, td);
1028 if (error != 0)
1029 panic("bootpc_fakeup_interface: set if netmask, error=%d",
1030 error);
1031
1032 /* Broadcast is 255.255.255.255 */
1033
1034 sin = (struct sockaddr_in *)&ireq->ifr_addr;
1035 clear_sinaddr(sin);
1036 clear_sinaddr(&ifctx->broadcast);
1037 sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
1038 ifctx->broadcast.sin_addr.s_addr = sin->sin_addr.s_addr;
1039
1040 error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t)ireq, td);
1041 if (error != 0)
1042 panic("bootpc_fakeup_interface: "
1043 "set if broadcast addr, error=%d",
1044 error);
1045
1046 /* Get HW address */
1047
1048 sdl = NULL;
1049 for (ifa = TAILQ_FIRST(&ifctx->ifp->if_addrhead);
1050 ifa != NULL;
1051 ifa = TAILQ_NEXT(ifa, ifa_link))
1052 if (ifa->ifa_addr->sa_family == AF_LINK &&
1053 (sdl = ((struct sockaddr_dl *) ifa->ifa_addr)) != NULL &&
1054 sdl->sdl_type == IFT_ETHER)
1055 break;
1056
1057 if (sdl == NULL)
1058 panic("bootpc: Unable to find HW address for %s",
1059 ifctx->ireq.ifr_name);
1060 ifctx->sdl = sdl;
1061
1062 return error;
1063 }
1064
1065
1066 static int
1067 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
1068 struct bootpc_globalcontext *gctx, struct thread *td)
1069 {
1070 int error;
1071 struct sockaddr_in defdst;
1072 struct sockaddr_in defmask;
1073 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;
1079
1080 ireq = &ifctx->ireq;
1081 so = ifctx->so;
1082 myaddr = &ifctx->myaddr;
1083 netmask = &ifctx->netmask;
1084 gw = &ifctx->gw;
1085
1086 if (bootpc_ifctx_isresolved(ifctx) == 0) {
1087
1088 /* Shutdown interfaces where BOOTP failed */
1089
1090 printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
1091 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, td);
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, td);
1097 if (error != 0)
1098 panic("bootpc_adjust_interface: "
1099 "SIOCSIFFLAGS, error=%d", error);
1100
1101 sin = (struct sockaddr_in *) &ireq->ifr_addr;
1102 clear_sinaddr(sin);
1103 error = ifioctl(so, SIOCDIFADDR, (caddr_t) ireq, td);
1104 if (error != 0 && (error != EEXIST ||
1105 ifctx == gctx->interfaces))
1106 panic("bootpc_adjust_interface: "
1107 "SIOCDIFADDR, error=%d", error);
1108
1109 return 0;
1110 }
1111
1112 printf("Adjusted interface %s\n", ifctx->ireq.ifr_name);
1113 /*
1114 * Do enough of ifconfig(8) so that the chosen interface
1115 * can talk to the servers. (just set the address)
1116 */
1117 bcopy(netmask, &ireq->ifr_addr, sizeof(*netmask));
1118 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t) ireq, td);
1119 if (error != 0)
1120 panic("bootpc_adjust_interface: "
1121 "set if netmask, error=%d", error);
1122
1123 /* Broadcast is with host part of IP address all 1's */
1124
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, td);
1130 if (error != 0)
1131 panic("bootpc_adjust_interface: "
1132 "set if broadcast addr, error=%d", error);
1133
1134 bcopy(myaddr, &ireq->ifr_addr, sizeof(*myaddr));
1135 error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, td);
1136 if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
1137 panic("bootpc_adjust_interface: "
1138 "set if addr, error=%d", error);
1139
1140 /* Add new default route */
1141
1142 if (ifctx->gotgw != 0 || gctx->gotgw == 0) {
1143 clear_sinaddr(&defdst);
1144 clear_sinaddr(&defmask);
1145 error = rtrequest(RTM_ADD,
1146 (struct sockaddr *) &defdst,
1147 (struct sockaddr *) gw,
1148 (struct sockaddr *) &defmask,
1149 (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
1150 if (error != 0) {
1151 printf("bootpc_adjust_interface: "
1152 "add net route, error=%d\n", error);
1153 return error;
1154 }
1155 }
1156
1157 return 0;
1158 }
1159
1160 static int
1161 setfs(struct sockaddr_in *addr, char *path, char *p,
1162 const struct in_addr *siaddr)
1163 {
1164
1165 if (getip(&p, &addr->sin_addr) == 0) {
1166 if (siaddr != NULL && *p == '/')
1167 bcopy(siaddr, &addr->sin_addr, sizeof(struct in_addr));
1168 else
1169 return 0;
1170 } else {
1171 if (*p != ':')
1172 return 0;
1173 p++;
1174 }
1175
1176 addr->sin_len = sizeof(struct sockaddr_in);
1177 addr->sin_family = AF_INET;
1178
1179 strlcpy(path, p, MNAMELEN);
1180 return 1;
1181 }
1182
1183 static int
1184 getip(char **ptr, struct in_addr *addr)
1185 {
1186 char *p;
1187 unsigned int ip;
1188 int val;
1189
1190 p = *ptr;
1191 ip = 0;
1192 if (((val = getdec(&p)) < 0) || (val > 255))
1193 return 0;
1194 ip = val << 24;
1195 if (*p != '.')
1196 return 0;
1197 p++;
1198 if (((val = getdec(&p)) < 0) || (val > 255))
1199 return 0;
1200 ip |= (val << 16);
1201 if (*p != '.')
1202 return 0;
1203 p++;
1204 if (((val = getdec(&p)) < 0) || (val > 255))
1205 return 0;
1206 ip |= (val << 8);
1207 if (*p != '.')
1208 return 0;
1209 p++;
1210 if (((val = getdec(&p)) < 0) || (val > 255))
1211 return 0;
1212 ip |= val;
1213
1214 addr->s_addr = htonl(ip);
1215 *ptr = p;
1216 return 1;
1217 }
1218
1219 static int
1220 getdec(char **ptr)
1221 {
1222 char *p;
1223 int ret;
1224
1225 p = *ptr;
1226 ret = 0;
1227 if ((*p < '') || (*p > '9'))
1228 return -1;
1229 while ((*p >= '') && (*p <= '9')) {
1230 ret = ret * 10 + (*p - '');
1231 p++;
1232 }
1233 *ptr = p;
1234 return ret;
1235 }
1236
1237 static void
1238 mountopts(struct nfs_args *args, char *p)
1239 {
1240 args->version = NFS_ARGSVERSION;
1241 args->rsize = 8192;
1242 args->wsize = 8192;
1243 args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
1244 args->sotype = SOCK_DGRAM;
1245 if (p != NULL)
1246 nfs_parse_options(p, args);
1247 }
1248
1249 static int
1250 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
1251 {
1252 struct mbuf *m;
1253 int alignedlen;
1254
1255 m = *mptr;
1256 alignedlen = ( len + 3 ) & ~3;
1257
1258 if (m->m_len < alignedlen) {
1259 m = m_pullup(m, alignedlen);
1260 if (m == NULL) {
1261 *mptr = NULL;
1262 return EBADRPC;
1263 }
1264 }
1265 bcopy(mtod(m, u_char *), buf, len);
1266 m_adj(m, alignedlen);
1267 *mptr = m;
1268 return 0;
1269 }
1270
1271 static int
1272 xdr_int_decode(struct mbuf **mptr, int *iptr)
1273 {
1274 u_int32_t i;
1275
1276 if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
1277 return EBADRPC;
1278 *iptr = fxdr_unsigned(u_int32_t, i);
1279 return 0;
1280 }
1281
1282 static void
1283 print_sin_addr(struct sockaddr_in *sin)
1284 {
1285
1286 print_in_addr(sin->sin_addr);
1287 }
1288
1289 static void
1290 print_in_addr(struct in_addr addr)
1291 {
1292 unsigned int ip;
1293
1294 ip = ntohl(addr.s_addr);
1295 printf("%d.%d.%d.%d",
1296 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
1297 }
1298
1299 static void
1300 bootpc_compose_query(struct bootpc_ifcontext *ifctx,
1301 struct bootpc_globalcontext *gctx, struct thread *td)
1302 {
1303 unsigned char *vendp;
1304 unsigned char vendor_client[64];
1305 uint32_t leasetime;
1306 uint8_t vendor_client_len;
1307
1308 ifctx->gotrootpath = 0;
1309
1310 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
1311
1312 /* bootpc part */
1313 ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */
1314 ifctx->call.htype = 1; /* 10mb ethernet */
1315 ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
1316 ifctx->call.hops = 0;
1317 if (bootpc_ifctx_isunresolved(ifctx) != 0)
1318 ifctx->xid++;
1319 ifctx->call.xid = txdr_unsigned(ifctx->xid);
1320 bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
1321
1322 vendp = ifctx->call.vend;
1323 *vendp++ = 99; /* RFC1048 cookie */
1324 *vendp++ = 130;
1325 *vendp++ = 83;
1326 *vendp++ = 99;
1327 *vendp++ = TAG_MAXMSGSIZE;
1328 *vendp++ = 2;
1329 *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
1330 *vendp++ = sizeof(struct bootp_packet) & 255;
1331
1332 snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
1333 ostype, MACHINE, osrelease);
1334 vendor_client_len = strlen(vendor_client);
1335 *vendp++ = TAG_VENDOR_INDENTIFIER;
1336 *vendp++ = vendor_client_len;
1337 memcpy(vendp, vendor_client, vendor_client_len);
1338 vendp += vendor_client_len;;
1339 ifctx->dhcpquerytype = DHCP_NOMSG;
1340 switch (ifctx->state) {
1341 case IF_DHCP_UNRESOLVED:
1342 *vendp++ = TAG_DHCP_MSGTYPE;
1343 *vendp++ = 1;
1344 *vendp++ = DHCP_DISCOVER;
1345 ifctx->dhcpquerytype = DHCP_DISCOVER;
1346 ifctx->gotdhcpserver = 0;
1347 break;
1348 case IF_DHCP_OFFERED:
1349 *vendp++ = TAG_DHCP_MSGTYPE;
1350 *vendp++ = 1;
1351 *vendp++ = DHCP_REQUEST;
1352 ifctx->dhcpquerytype = DHCP_REQUEST;
1353 *vendp++ = TAG_DHCP_REQ_ADDR;
1354 *vendp++ = 4;
1355 memcpy(vendp, &ifctx->reply.yiaddr, 4);
1356 vendp += 4;
1357 if (ifctx->gotdhcpserver != 0) {
1358 *vendp++ = TAG_DHCP_SERVERID;
1359 *vendp++ = 4;
1360 memcpy(vendp, &ifctx->dhcpserver, 4);
1361 vendp += 4;
1362 }
1363 *vendp++ = TAG_DHCP_LEASETIME;
1364 *vendp++ = 4;
1365 leasetime = htonl(300);
1366 memcpy(vendp, &leasetime, 4);
1367 vendp += 4;
1368 break;
1369 default:
1370 break;
1371 }
1372 *vendp = TAG_END;
1373
1374 ifctx->call.secs = 0;
1375 ifctx->call.flags = htons(0x8000); /* We need a broadcast answer */
1376 }
1377
1378 static int
1379 bootpc_hascookie(struct bootp_packet *bp)
1380 {
1381
1382 return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
1383 bp->vend[2] == 83 && bp->vend[3] == 99);
1384 }
1385
1386 static void
1387 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
1388 unsigned char *start, int len, int tag)
1389 {
1390 unsigned char *j;
1391 unsigned char *ej;
1392 unsigned char code;
1393
1394 if (tctx->badtag != 0 || tctx->badopt != 0)
1395 return;
1396
1397 j = start;
1398 ej = j + len;
1399
1400 while (j < ej) {
1401 code = *j++;
1402 if (code == TAG_PAD)
1403 continue;
1404 if (code == TAG_END)
1405 return;
1406 if (j >= ej || j + *j + 1 > ej) {
1407 tctx->badopt = 1;
1408 return;
1409 }
1410 len = *j++;
1411 if (code == tag) {
1412 if (tctx->taglen + len > TAG_MAXLEN) {
1413 tctx->badtag = 1;
1414 return;
1415 }
1416 tctx->foundopt = 1;
1417 if (len > 0)
1418 memcpy(tctx->buf + tctx->taglen,
1419 j, len);
1420 tctx->taglen += len;
1421 }
1422 if (code == TAG_OVERLOAD)
1423 tctx->overload = *j;
1424
1425 j += len;
1426 }
1427 }
1428
1429 static unsigned char *
1430 bootpc_tag(struct bootpc_tagcontext *tctx,
1431 struct bootp_packet *bp, int len, int tag)
1432 {
1433 tctx->overload = 0;
1434 tctx->badopt = 0;
1435 tctx->badtag = 0;
1436 tctx->foundopt = 0;
1437 tctx->taglen = 0;
1438
1439 if (bootpc_hascookie(bp) == 0)
1440 return NULL;
1441
1442 bootpc_tag_helper(tctx, &bp->vend[4],
1443 (unsigned char *) bp + len - &bp->vend[4], tag);
1444
1445 if ((tctx->overload & OVERLOAD_FILE) != 0)
1446 bootpc_tag_helper(tctx,
1447 (unsigned char *) bp->file,
1448 sizeof(bp->file),
1449 tag);
1450 if ((tctx->overload & OVERLOAD_SNAME) != 0)
1451 bootpc_tag_helper(tctx,
1452 (unsigned char *) bp->sname,
1453 sizeof(bp->sname),
1454 tag);
1455
1456 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
1457 return NULL;
1458 tctx->buf[tctx->taglen] = '\0';
1459 return tctx->buf;
1460 }
1461
1462 static void
1463 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
1464 struct bootpc_globalcontext *gctx)
1465 {
1466 char *p;
1467 unsigned int ip;
1468
1469 ifctx->gotgw = 0;
1470 ifctx->gotnetmask = 0;
1471
1472 clear_sinaddr(&ifctx->myaddr);
1473 clear_sinaddr(&ifctx->netmask);
1474 clear_sinaddr(&ifctx->gw);
1475
1476 ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
1477
1478 ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
1479
1480 printf("%s at ", ifctx->ireq.ifr_name);
1481 print_sin_addr(&ifctx->myaddr);
1482 printf(" server ");
1483 print_in_addr(ifctx->reply.siaddr);
1484
1485 ifctx->gw.sin_addr = ifctx->reply.giaddr;
1486 if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
1487 printf(" via gateway ");
1488 print_in_addr(ifctx->reply.giaddr);
1489 }
1490
1491 /* This call used for the side effect (overload flag) */
1492 (void) bootpc_tag(&gctx->tmptag,
1493 &ifctx->reply, ifctx->replylen, TAG_END);
1494
1495 if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
1496 if (ifctx->reply.sname[0] != '\0')
1497 printf(" server name %s", ifctx->reply.sname);
1498 if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
1499 if (ifctx->reply.file[0] != '\0')
1500 printf(" boot file %s", ifctx->reply.file);
1501
1502 printf("\n");
1503
1504 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1505 TAG_SUBNETMASK);
1506 if (p != NULL) {
1507 if (gctx->tag.taglen != 4)
1508 panic("bootpc: subnet mask len is %d",
1509 gctx->tag.taglen);
1510 bcopy(p, &ifctx->netmask.sin_addr, 4);
1511 ifctx->gotnetmask = 1;
1512 printf("subnet mask ");
1513 print_sin_addr(&ifctx->netmask);
1514 printf(" ");
1515 }
1516
1517 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1518 TAG_ROUTERS);
1519 if (p != NULL) {
1520 /* Routers */
1521 if (gctx->tag.taglen % 4)
1522 panic("bootpc: Router Len is %d", gctx->tag.taglen);
1523 if (gctx->tag.taglen > 0) {
1524 bcopy(p, &ifctx->gw.sin_addr, 4);
1525 printf("router ");
1526 print_sin_addr(&ifctx->gw);
1527 printf(" ");
1528 ifctx->gotgw = 1;
1529 gctx->gotgw = 1;
1530 }
1531 }
1532
1533 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1534 TAG_ROOT);
1535 if (p != NULL) {
1536 if (gctx->setrootfs != NULL) {
1537 printf("rootfs %s (ignored) ", p);
1538 } else if (setfs(&nd->root_saddr,
1539 nd->root_hostnam, p, &ifctx->reply.siaddr)) {
1540 if (*p == '/') {
1541 printf("root_server ");
1542 print_sin_addr(&nd->root_saddr);
1543 printf(" ");
1544 }
1545 printf("rootfs %s ", p);
1546 gctx->gotrootpath = 1;
1547 ifctx->gotrootpath = 1;
1548 gctx->setrootfs = ifctx;
1549
1550 p = bootpc_tag(&gctx->tag, &ifctx->reply,
1551 ifctx->replylen,
1552 TAG_ROOTOPTS);
1553 if (p != NULL) {
1554 mountopts(&nd->root_args, p);
1555 printf("rootopts %s ", p);
1556 }
1557 } else
1558 panic("Failed to set rootfs to %s", p);
1559 }
1560
1561 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1562 TAG_HOSTNAME);
1563 if (p != NULL) {
1564 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
1565 panic("bootpc: hostname >= %d bytes",
1566 MAXHOSTNAMELEN);
1567 if (gctx->sethostname != NULL) {
1568 printf("hostname %s (ignored) ", p);
1569 } else {
1570 strcpy(nd->my_hostnam, p);
1571 strcpy(hostname, p);
1572 printf("hostname %s ", hostname);
1573 gctx->sethostname = ifctx;
1574 }
1575 }
1576 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
1577 TAG_COOKIE);
1578 if (p != NULL) { /* store in a sysctl variable */
1579 int i, l = sizeof(bootp_cookie) - 1;
1580 for (i = 0; i < l && p[i] != '\0'; i++)
1581 bootp_cookie[i] = p[i];
1582 p[i] = '\0';
1583 }
1584
1585
1586 printf("\n");
1587
1588 if (ifctx->gotnetmask == 0) {
1589 if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1590 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
1591 else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
1592 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
1593 else
1594 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
1595 }
1596 if (ifctx->gotgw == 0) {
1597 /* Use proxyarp */
1598 ifctx->gw.sin_addr.s_addr = ifctx->myaddr.sin_addr.s_addr;
1599 }
1600 }
1601
1602 void
1603 bootpc_init(void)
1604 {
1605 struct bootpc_ifcontext *ifctx, *nctx; /* Interface BOOTP contexts */
1606 struct bootpc_globalcontext *gctx; /* Global BOOTP context */
1607 struct ifnet *ifp;
1608 int error;
1609 #ifndef BOOTP_WIRED_TO
1610 int ifcnt;
1611 #endif
1612 struct nfsv3_diskless *nd;
1613 struct thread *td;
1614
1615 nd = &nfsv3_diskless;
1616 td = curthread;
1617
1618 /*
1619 * If already filled in, don't touch it here
1620 */
1621 if (nfs_diskless_valid != 0)
1622 return;
1623
1624 gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK | M_ZERO);
1625 if (gctx == NULL)
1626 panic("Failed to allocate bootp global context structure");
1627
1628 gctx->xid = ~0xFFFF;
1629 gctx->starttime = time_second;
1630
1631 /*
1632 * Find a network interface.
1633 */
1634 #ifdef BOOTP_WIRED_TO
1635 printf("bootpc_init: wired to interface '%s'\n",
1636 __XSTRING(BOOTP_WIRED_TO));
1637 allocifctx(gctx);
1638 #else
1639 /*
1640 * Preallocate interface context storage, if another interface
1641 * attaches and wins the race, it won't be eligible for bootp.
1642 */
1643 IFNET_RLOCK();
1644 for (ifp = TAILQ_FIRST(&ifnet), ifcnt = 0;
1645 ifp != NULL;
1646 ifp = TAILQ_NEXT(ifp, if_link)) {
1647 if ((ifp->if_flags &
1648 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1649 IFF_BROADCAST)
1650 continue;
1651 ifcnt++;
1652 }
1653 IFNET_RUNLOCK();
1654 if (ifcnt == 0)
1655 panic("bootpc_init: no eligible interfaces");
1656 for (; ifcnt > 0; ifcnt--)
1657 allocifctx(gctx);
1658 #endif
1659
1660 IFNET_RLOCK();
1661 for (ifp = TAILQ_FIRST(&ifnet), ifctx = gctx->interfaces;
1662 ifp != NULL && ifctx != NULL;
1663 ifp = TAILQ_NEXT(ifp, if_link)) {
1664 strlcpy(ifctx->ireq.ifr_name, ifp->if_xname,
1665 sizeof(ifctx->ireq.ifr_name));
1666 #ifdef BOOTP_WIRED_TO
1667 if (strcmp(ifctx->ireq.ifr_name,
1668 __XSTRING(BOOTP_WIRED_TO)) != 0)
1669 continue;
1670 #else
1671 if ((ifp->if_flags &
1672 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
1673 IFF_BROADCAST)
1674 continue;
1675 #endif
1676 ifctx->ifp = ifp;
1677 ifctx = ifctx->next;
1678 }
1679 IFNET_RUNLOCK();
1680
1681 if (gctx->interfaces == NULL || gctx->interfaces->ifp == NULL) {
1682 #ifdef BOOTP_WIRED_TO
1683 panic("bootpc_init: Could not find interface specified "
1684 "by BOOTP_WIRED_TO: "
1685 __XSTRING(BOOTP_WIRED_TO));
1686 #else
1687 panic("bootpc_init: no suitable interface");
1688 #endif
1689 }
1690
1691 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1692 bootpc_fakeup_interface(ifctx, gctx, td);
1693
1694 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1695 bootpc_compose_query(ifctx, gctx, td);
1696
1697 error = bootpc_call(gctx, td);
1698
1699 if (error != 0) {
1700 #ifdef BOOTP_NFSROOT
1701 panic("BOOTP call failed");
1702 #else
1703 printf("BOOTP call failed\n");
1704 #endif
1705 }
1706
1707 rootdevnames[0] = "nfs:";
1708 mountopts(&nd->root_args, NULL);
1709
1710 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1711 if (bootpc_ifctx_isresolved(ifctx) != 0)
1712 bootpc_decode_reply(nd, ifctx, gctx);
1713
1714 #ifdef BOOTP_NFSROOT
1715 if (gctx->gotrootpath == 0)
1716 panic("bootpc: No root path offered");
1717 #endif
1718
1719 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
1720 bootpc_adjust_interface(ifctx, gctx, td);
1721
1722 soclose(ifctx->so);
1723 }
1724
1725 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
1726 if (ifctx->gotrootpath != 0)
1727 break;
1728 if (ifctx == NULL) {
1729 for (ifctx = gctx->interfaces;
1730 ifctx != NULL;
1731 ifctx = ifctx->next)
1732 if (bootpc_ifctx_isresolved(ifctx) != 0)
1733 break;
1734 }
1735 if (ifctx == NULL)
1736 goto out;
1737
1738 if (gctx->gotrootpath != 0) {
1739
1740 setenv("boot.netif.name", ifctx->ifp->if_xname);
1741
1742 error = md_mount(&nd->root_saddr, nd->root_hostnam,
1743 nd->root_fh, &nd->root_fhsize,
1744 &nd->root_args, td);
1745 if (error != 0)
1746 panic("nfs_boot: mountd root, error=%d", error);
1747
1748 nfs_diskless_valid = 3;
1749 }
1750
1751 strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
1752 bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
1753 bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
1754 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
1755 ifctx->myaddr.sin_addr.s_addr |
1756 ~ ifctx->netmask.sin_addr.s_addr;
1757 bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
1758
1759 out:
1760 for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = nctx) {
1761 nctx = ifctx->next;
1762 free(ifctx, M_TEMP);
1763 }
1764 free(gctx, M_TEMP);
1765 }
1766
1767 /*
1768 * RPC: mountd/mount
1769 * Given a server pathname, get an NFS file handle.
1770 * Also, sets sin->sin_port to the NFS service port.
1771 */
1772 static int
1773 md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp, int *fhsizep,
1774 struct nfs_args *args, struct thread *td)
1775 {
1776 struct mbuf *m;
1777 int error;
1778 int authunixok;
1779 int authcount;
1780 int authver;
1781
1782 /* XXX honor v2/v3 flags in args->flags? */
1783 #ifdef BOOTP_NFSV3
1784 /* First try NFS v3 */
1785 /* Get port number for MOUNTD. */
1786 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1787 &mdsin->sin_port, td);
1788 if (error == 0) {
1789 m = xdr_string_encode(path, strlen(path));
1790
1791 /* Do RPC to mountd. */
1792 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
1793 RPCMNT_MOUNT, &m, NULL, td);
1794 }
1795 if (error == 0) {
1796 args->flags |= NFSMNT_NFSV3;
1797 } else {
1798 #endif
1799 /* Fallback to NFS v2 */
1800
1801 /* Get port number for MOUNTD. */
1802 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1803 &mdsin->sin_port, td);
1804 if (error != 0)
1805 return error;
1806
1807 m = xdr_string_encode(path, strlen(path));
1808
1809 /* Do RPC to mountd. */
1810 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
1811 RPCMNT_MOUNT, &m, NULL, td);
1812 if (error != 0)
1813 return error; /* message already freed */
1814
1815 #ifdef BOOTP_NFSV3
1816 }
1817 #endif
1818
1819 if (xdr_int_decode(&m, &error) != 0 || error != 0)
1820 goto bad;
1821
1822 if ((args->flags & NFSMNT_NFSV3) != 0) {
1823 if (xdr_int_decode(&m, fhsizep) != 0 ||
1824 *fhsizep > NFSX_V3FHMAX ||
1825 *fhsizep <= 0)
1826 goto bad;
1827 } else
1828 *fhsizep = NFSX_V2FH;
1829
1830 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
1831 goto bad;
1832
1833 if (args->flags & NFSMNT_NFSV3) {
1834 if (xdr_int_decode(&m, &authcount) != 0)
1835 goto bad;
1836 authunixok = 0;
1837 if (authcount < 0 || authcount > 100)
1838 goto bad;
1839 while (authcount > 0) {
1840 if (xdr_int_decode(&m, &authver) != 0)
1841 goto bad;
1842 if (authver == RPCAUTH_UNIX)
1843 authunixok = 1;
1844 authcount--;
1845 }
1846 if (authunixok == 0)
1847 goto bad;
1848 }
1849
1850 /* Set port number for NFS use. */
1851 error = krpc_portmap(mdsin, NFS_PROG,
1852 (args->flags &
1853 NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
1854 &mdsin->sin_port, td);
1855
1856 goto out;
1857
1858 bad:
1859 error = EBADRPC;
1860
1861 out:
1862 m_freem(m);
1863 return error;
1864 }
1865
1866 SYSINIT(bootp_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, bootpc_init, NULL);
Cache object: 1a165dc3f3f5d361d0a586d1fc94357f
|