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