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