nfs_bootdhcp.c revision 1.37.6.2 1 /* $NetBSD: nfs_bootdhcp.c,v 1.37.6.2 2008/09/28 10:41:00 mjf Exp $ */
2
3 /*-
4 * Copyright (c) 1995, 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Adam Glass and Gordon W. Ross.
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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Support for NFS diskless booting with BOOTP (RFC951, RFC1048)
34 *
35 * History:
36 *
37 * Tor Egge developed the initial version of this code based on
38 * the Sun RPC/bootparam sources nfs_boot.c and krpc_subr.c and
39 * submitted that work to NetBSD as bugreport "kern/2351" on
40 * 29 Apr 1996.
41 *
42 * Gordon Ross reorganized Tor's version into this form and
43 * integrated it into the NetBSD sources during Aug 1997.
44 */
45
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: nfs_bootdhcp.c,v 1.37.6.2 2008/09/28 10:41:00 mjf Exp $");
48
49 #include "opt_nfs_boot.h"
50 #include "opt_tftproot.h"
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/device.h>
56 #include <sys/ioctl.h>
57 #include <sys/proc.h>
58 #include <sys/mount.h>
59 #include <sys/mbuf.h>
60 #include <sys/reboot.h>
61 #include <sys/socket.h>
62 #include <sys/socketvar.h>
63
64 #include <net/if.h>
65 #include <net/if_types.h>
66 #include <net/if_arp.h> /* ARPHRD_ETHER, etc. */
67 #include <net/if_dl.h>
68 #include <net/if_ether.h>
69 #include <net/route.h>
70
71 #include <netinet/in.h>
72 #include <netinet/if_inarp.h>
73
74 #include <nfs/rpcv2.h>
75
76 #include <nfs/nfsproto.h>
77 #include <nfs/nfs.h>
78 #include <nfs/nfsmount.h>
79 #include <nfs/nfsdiskless.h>
80
81 /*
82 * There are two implementations of NFS diskless boot.
83 * This implementation uses BOOTP (RFC951, RFC1048), and
84 * the other uses Sun RPC/bootparams (nfs_bootparam.c).
85 *
86 * This method gets everything it needs with one BOOTP
87 * request and reply. Note that this actually uses only
88 * the old BOOTP functionality subset of DHCP. It is not
89 * clear that DHCP provides any advantage over BOOTP for
90 * diskless boot. DHCP allows the server to assign an IP
91 * address without any a-priori knowledge of the client,
92 * but we require that the server has a-priori knowledge
93 * of the client so it can export our (unique) NFS root.
94 * Given that the server needs a-priori knowledge about
95 * the client anyway, it might as well assign a fixed IP
96 * address for the client and support BOOTP.
97 *
98 * On the other hand, disk-FULL clients may use DHCP, but
99 * in that case the DHCP client should be user-mode code,
100 * and has no bearing on the code below. -gwr
101 */
102
103 /* Begin stuff from bootp.h */
104 /* Definitions from RFC951 */
105 #define BP_CHADDR_LEN 16
106 #define BP_SNAME_LEN 64
107 #define BP_FILE_LEN 128
108 #define BP_VEND_LEN 64
109 struct bootp {
110 u_int8_t bp_op; /* packet opcode type */
111 u_int8_t bp_htype; /* hardware addr type */
112 u_int8_t bp_hlen; /* hardware addr length */
113 u_int8_t bp_hops; /* gateway hops */
114 u_int32_t bp_xid; /* transaction ID */
115 u_int16_t bp_secs; /* seconds since boot began */
116 u_int16_t bp_flags; /* RFC1532 broadcast, etc. */
117 struct in_addr bp_ciaddr; /* client IP address */
118 struct in_addr bp_yiaddr; /* 'your' IP address */
119 struct in_addr bp_siaddr; /* server IP address */
120 struct in_addr bp_giaddr; /* gateway IP address */
121 u_int8_t bp_chaddr[BP_CHADDR_LEN]; /* client hardware address */
122 char bp_sname[BP_SNAME_LEN]; /* server host name */
123 char bp_file[BP_FILE_LEN]; /* boot file name */
124 u_int8_t bp_vend[BP_VEND_LEN]; /* RFC1048 options */
125 /*
126 * Note that BOOTP packets are allowed to be longer
127 * (see RFC 1532 sect. 2.1) and common practice is to
128 * allow the option data in bp_vend to extend into the
129 * additional space provided in longer packets.
130 */
131 };
132
133 #define IPPORT_BOOTPS 67
134 #define IPPORT_BOOTPC 68
135
136 #define BOOTREQUEST 1
137 #define BOOTREPLY 2
138
139 /*
140 * Is this available from the sockaddr_dl somehow?
141 * Perhaps (struct arphdr)->ar_hrd = ARPHRD_ETHER?
142 * The interface has ->if_type but not the ARP fmt.
143 */
144 #define HTYPE_ETHERNET 1
145 #define HTYPE_IEEE802 6
146
147 /*
148 * Vendor magic cookie (v_magic) for RFC1048
149 */
150 static const u_int8_t vm_rfc1048[4] = { 99, 130, 83, 99 };
151
152 /*
153 * Tag values used to specify what information is being supplied in
154 * the vendor (options) data area of the packet.
155 */
156 /* RFC 1048 */
157 #define TAG_END ((unsigned char) 255)
158 #define TAG_PAD ((unsigned char) 0)
159 #define TAG_SUBNET_MASK ((unsigned char) 1)
160 #define TAG_TIME_OFFSET ((unsigned char) 2)
161 #define TAG_GATEWAY ((unsigned char) 3)
162 #define TAG_TIME_SERVER ((unsigned char) 4)
163 #define TAG_NAME_SERVER ((unsigned char) 5)
164 #define TAG_DOMAIN_SERVER ((unsigned char) 6)
165 #define TAG_LOG_SERVER ((unsigned char) 7)
166 #define TAG_COOKIE_SERVER ((unsigned char) 8)
167 #define TAG_LPR_SERVER ((unsigned char) 9)
168 #define TAG_IMPRESS_SERVER ((unsigned char) 10)
169 #define TAG_RLP_SERVER ((unsigned char) 11)
170 #define TAG_HOST_NAME ((unsigned char) 12)
171 #define TAG_BOOT_SIZE ((unsigned char) 13)
172 /* RFC 1395 */
173 #define TAG_DUMP_FILE ((unsigned char) 14)
174 #define TAG_DOMAIN_NAME ((unsigned char) 15)
175 #define TAG_SWAP_SERVER ((unsigned char) 16)
176 #define TAG_ROOT_PATH ((unsigned char) 17)
177 /* End of stuff from bootp.h */
178
179 #ifdef NFS_BOOT_DHCP
180 #define TAG_REQ_ADDR ((unsigned char) 50)
181 #define TAG_LEASETIME ((unsigned char) 51)
182 #define TAG_OVERLOAD ((unsigned char) 52)
183 #define TAG_DHCP_MSGTYPE ((unsigned char) 53)
184 #define TAG_SERVERID ((unsigned char) 54)
185 #define TAG_PARAM_REQ ((unsigned char) 55)
186 #define TAG_MSG ((unsigned char) 56)
187 #define TAG_MAXSIZE ((unsigned char) 57)
188 #define TAG_T1 ((unsigned char) 58)
189 #define TAG_T2 ((unsigned char) 59)
190 #define TAG_CLASSID ((unsigned char) 60)
191 #define TAG_CLIENTID ((unsigned char) 61)
192 #endif
193
194 #ifdef NFS_BOOT_DHCP
195 #define DHCPDISCOVER 1
196 #define DHCPOFFER 2
197 #define DHCPREQUEST 3
198 #define DHCPDECLINE 4
199 #define DHCPACK 5
200 #define DHCPNAK 6
201 #define DHCPRELEASE 7
202 #endif
203
204 #ifdef NFS_BOOT_DHCP
205 #define BOOTP_SIZE_MAX (sizeof(struct bootp)+312-64)
206 #else
207 /*
208 * The "extended" size is somewhat arbitrary, but is
209 * constrained by the maximum message size specified
210 * by RFC1533 (567 total). This value increases the
211 * space for options from 64 bytes to 256 bytes.
212 */
213 #define BOOTP_SIZE_MAX (sizeof(struct bootp)+256-64)
214 #endif
215 #define BOOTP_SIZE_MIN (sizeof(struct bootp))
216
217 /* Convenience macro */
218 #define INTOHL(ina) ((u_int32_t)ntohl((ina).s_addr))
219
220 static int bootpc_call __P((struct nfs_diskless *, struct lwp *));
221 static void bootp_extract __P((struct bootp *, int, struct nfs_diskless *));
222
223 #ifdef DEBUG_NFS_BOOT_DHCP
224 #define DPRINTF(s) printf s
225 #else
226 #define DPRINTF(s)
227 #endif
228
229
230 /*
231 * Get our boot parameters using BOOTP.
232 */
233 int
234 nfs_bootdhcp(nd, lwp)
235 struct nfs_diskless *nd;
236 struct lwp *lwp;
237 {
238 struct ifnet *ifp = nd->nd_ifp;
239 int error;
240
241 /*
242 * Do enough of ifconfig(8) so that the chosen interface
243 * can talk to the servers. Use address zero for now.
244 */
245 error = nfs_boot_setaddress(ifp, lwp, INADDR_ANY, INADDR_ANY,
246 INADDR_BROADCAST);
247 if (error) {
248 printf("nfs_boot: set ifaddr zero, error=%d\n", error);
249 return (error);
250 }
251
252 /* This function call does the real send/recv work. */
253 error = bootpc_call(nd, lwp);
254
255 /* Get rid of the temporary (zero) IP address. */
256 (void) nfs_boot_deladdress(ifp, lwp, INADDR_ANY);
257
258 /* NOW we can test the error from bootpc_call. */
259 if (error)
260 goto out;
261
262 /*
263 * Do ifconfig with our real IP address and mask.
264 */
265 error = nfs_boot_setaddress(ifp, lwp, nd->nd_myip.s_addr,
266 nd->nd_mask.s_addr, INADDR_ANY);
267 if (error) {
268 printf("nfs_boot: set ifaddr real, error=%d\n", error);
269 goto out;
270 }
271
272 out:
273 if (error) {
274 (void) nfs_boot_ifupdown(ifp, lwp, 0);
275 nfs_boot_flushrt(ifp);
276 }
277 return (error);
278 }
279
280 struct bootpcontext {
281 int xid;
282 const u_char *haddr;
283 u_char halen;
284 struct bootp *replybuf;
285 int replylen;
286 #ifdef NFS_BOOT_DHCP
287 char expected_dhcpmsgtype, dhcp_ok;
288 struct in_addr dhcp_serverip;
289 #endif
290 };
291
292 static int bootpset __P((struct mbuf*, void*, int));
293 static int bootpcheck __P((struct mbuf*, void*));
294
295 static int
296 bootpset(struct mbuf *m, void *context, int waited)
297 {
298 struct bootp *bootp;
299
300 /* we know it's contigous (in 1 mbuf cluster) */
301 bootp = mtod(m, struct bootp*);
302
303 bootp->bp_secs = htons(waited);
304
305 return (0);
306 }
307
308 static int
309 bootpcheck(m, context)
310 struct mbuf *m;
311 void *context;
312 {
313 struct bootp *bootp;
314 struct bootpcontext *bpc = context;
315 u_int tag, len;
316 u_char *p, *limit;
317
318 /*
319 * Is this a valid reply?
320 */
321 if (m->m_pkthdr.len < BOOTP_SIZE_MIN) {
322 DPRINTF(("bootpcheck: short packet %d < %d\n", m->m_pkthdr.len,
323 BOOTP_SIZE_MIN));
324 return (-1);
325 }
326 if (m->m_pkthdr.len > BOOTP_SIZE_MAX) {
327 DPRINTF(("Bootpcheck: long packet %d > %d\n", m->m_pkthdr.len,
328 BOOTP_SIZE_MAX));
329 return (-1);
330 }
331
332 /*
333 * don't make first checks more expensive than necessary
334 */
335 if (m->m_len < offsetof(struct bootp, bp_sname)) {
336 m = m_pullup(m, offsetof(struct bootp, bp_sname));
337 if (m == NULL) {
338 DPRINTF(("bootpcheck: m_pullup failed\n"));
339 return (-1);
340 }
341 }
342 bootp = mtod(m, struct bootp*);
343
344 if (bootp->bp_op != BOOTREPLY) {
345 DPRINTF(("bootpcheck: op %d is not reply\n", bootp->bp_op));
346 return (-1);
347 }
348 if (bootp->bp_hlen != bpc->halen) {
349 DPRINTF(("bootpcheck: hlen %d != %d\n", bootp->bp_hlen,
350 bpc->halen));
351 return (-1);
352 }
353 if (memcmp(bootp->bp_chaddr, bpc->haddr, bpc->halen)) {
354 #ifdef DEBUG_NFS_BOOT_DHCP
355 char *bp_chaddr, *haddr;
356
357 bp_chaddr = malloc(3 * bpc->halen, M_TEMP, M_WAITOK);
358 haddr = malloc(3 * bpc->halen, M_TEMP, M_WAITOK);
359
360 DPRINTF(("bootpcheck: incorrect hwaddr %s != %s\n",
361 ether_snprintf(bp_chaddr, 3 * bpc->halen,
362 bootp->bp_chaddr),
363 ether_snprintf(haddr, 3 * bpc->halen, bpc->haddr)));
364
365 free(bp_chaddr, M_TEMP);
366 free(haddr, M_TEMP);
367 #endif
368 return (-1);
369 }
370 if (bootp->bp_xid != bpc->xid) {
371 DPRINTF(("bootpcheck: xid %d != %d\n", bootp->bp_xid,
372 bpc->xid));
373 return (-1);
374 }
375
376 /*
377 * OK, it's worth to look deeper.
378 * We copy the mbuf into a flat buffer here because
379 * m_pullup() is a bit limited for this purpose
380 * (doesn't allocate a cluster if necessary).
381 */
382 bpc->replylen = m->m_pkthdr.len;
383 m_copydata(m, 0, bpc->replylen, (void *)bpc->replybuf);
384 bootp = bpc->replybuf;
385
386 /*
387 * Check if the IP address we get looks correct.
388 * (DHCP servers can send junk to unknown clients.)
389 * XXX more checks might be needed
390 */
391 if (bootp->bp_yiaddr.s_addr == INADDR_ANY ||
392 bootp->bp_yiaddr.s_addr == INADDR_BROADCAST) {
393 printf("nfs_boot: wrong IP addr %s",
394 inet_ntoa(bootp->bp_yiaddr));
395 goto warn;
396 }
397
398 /*
399 * Check the vendor data.
400 */
401 if (memcmp(bootp->bp_vend, vm_rfc1048, 4)) {
402 printf("nfs_boot: reply missing options");
403 goto warn;
404 }
405 p = &bootp->bp_vend[4];
406 limit = ((u_char*)bootp) + bpc->replylen;
407 while (p < limit) {
408 tag = *p++;
409 if (tag == TAG_END)
410 break;
411 if (tag == TAG_PAD)
412 continue;
413 len = *p++;
414 if ((p + len) > limit) {
415 printf("nfs_boot: option %d too long", tag);
416 goto warn;
417 }
418 switch (tag) {
419 #ifdef NFS_BOOT_DHCP
420 case TAG_DHCP_MSGTYPE:
421 if (*p != bpc->expected_dhcpmsgtype)
422 return (-1);
423 bpc->dhcp_ok = 1;
424 break;
425 case TAG_SERVERID:
426 memcpy(&bpc->dhcp_serverip.s_addr, p,
427 sizeof(bpc->dhcp_serverip.s_addr));
428 break;
429 #endif
430 default:
431 break;
432 }
433 p += len;
434 }
435 return (0);
436
437 warn:
438 printf(" (bad reply from %s)\n", inet_ntoa(bootp->bp_siaddr));
439 return (-1);
440 }
441
442 static int
443 bootpc_call(nd, lwp)
444 struct nfs_diskless *nd;
445 struct lwp *lwp;
446 {
447 struct socket *so;
448 struct ifnet *ifp = nd->nd_ifp;
449 static u_int32_t xid = ~0xFF;
450 struct bootp *bootp; /* request */
451 struct mbuf *m, *nam;
452 struct sockaddr_in *sin;
453 int error;
454 const u_char *haddr;
455 u_char hafmt, halen;
456 struct bootpcontext bpc;
457 #ifdef NFS_BOOT_DHCP
458 char vci[64];
459 int vcilen;
460 #endif
461
462 error = socreate(AF_INET, &so, SOCK_DGRAM, 0, lwp, NULL);
463 if (error) {
464 printf("bootp: socreate, error=%d\n", error);
465 return (error);
466 }
467
468 /*
469 * Initialize to NULL anything that will hold an allocation,
470 * and free each at the end if not null.
471 */
472 bpc.replybuf = NULL;
473 m = nam = NULL;
474
475 /* Record our H/W (Ethernet) address. */
476 { const struct sockaddr_dl *sdl = ifp->if_sadl;
477 switch (sdl->sdl_type) {
478 case IFT_ISO88025:
479 hafmt = HTYPE_IEEE802;
480 break;
481 case IFT_ETHER:
482 case IFT_FDDI:
483 hafmt = HTYPE_ETHERNET;
484 break;
485 default:
486 printf("bootp: unsupported interface type %d\n",
487 sdl->sdl_type);
488 error = EINVAL;
489 goto out;
490 }
491 halen = sdl->sdl_alen;
492 haddr = (const unsigned char *)CLLADDR(sdl);
493 }
494
495 /*
496 * Skip the route table when sending on this socket.
497 * If this is not done, ip_output finds the loopback
498 * interface (why?) and then fails because broadcast
499 * is not supported on that interface...
500 */
501 { int32_t opt;
502
503 opt = 1;
504 error = so_setsockopt(NULL, so, SOL_SOCKET, SO_DONTROUTE, &opt,
505 sizeof(opt));
506 }
507 if (error) {
508 DPRINTF(("bootpc_call: SO_DONTROUTE failed %d\n", error));
509 goto out;
510 }
511
512 /* Enable broadcast. */
513 if ((error = nfs_boot_enbroadcast(so))) {
514 DPRINTF(("bootpc_call: SO_BROADCAST failed %d\n", error));
515 goto out;
516 }
517
518 /*
519 * Set some TTL so we can boot through routers.
520 * Real BOOTP forwarding agents don't need this; they obey "bp_hops"
521 * and set "bp_giaddr", thus rewrite the packet anyway.
522 * The "helper-address" feature of some popular router vendor seems
523 * to do simple IP forwarding and drops packets with (ip_ttl == 1).
524 */
525 { u_char opt;
526
527 opt = 7;
528 error = so_setsockopt(NULL, so, IPPROTO_IP, IP_MULTICAST_TTL,
529 &opt, sizeof(opt));
530 }
531 if (error) {
532 DPRINTF(("bootpc_call: IP_MULTICAST_TTL failed %d\n", error));
533 goto out;
534 }
535
536 /* Set the receive timeout for the socket. */
537 if ((error = nfs_boot_setrecvtimo(so))) {
538 DPRINTF(("bootpc_call: SO_RCVTIMEO failed %d\n", error));
539 goto out;
540 }
541
542 /*
543 * Bind the local endpoint to a bootp client port.
544 */
545 if ((error = nfs_boot_sobind_ipport(so, IPPORT_BOOTPC, lwp))) {
546 DPRINTF(("bootpc_call: bind failed %d\n", error));
547 goto out;
548 }
549
550 /*
551 * Setup socket address for the server.
552 */
553 nam = m_get(M_WAIT, MT_SONAME);
554 sin = mtod(nam, struct sockaddr_in *);
555 sin->sin_len = nam->m_len = sizeof(*sin);
556 sin->sin_family = AF_INET;
557 sin->sin_addr.s_addr = INADDR_BROADCAST;
558 sin->sin_port = htons(IPPORT_BOOTPS);
559
560 /*
561 * Allocate buffer used for request
562 */
563 m = m_gethdr(M_WAIT, MT_DATA);
564 m_clget(m, M_WAIT);
565 bootp = mtod(m, struct bootp*);
566 m->m_pkthdr.len = m->m_len = BOOTP_SIZE_MAX;
567 m->m_pkthdr.rcvif = NULL;
568
569 /*
570 * Build the BOOTP reqest message.
571 * Note: xid is host order! (opaque to server)
572 */
573 memset((void *)bootp, 0, BOOTP_SIZE_MAX);
574 bootp->bp_op = BOOTREQUEST;
575 bootp->bp_htype = hafmt;
576 bootp->bp_hlen = halen; /* Hardware address length */
577 bootp->bp_xid = ++xid;
578 memcpy(bootp->bp_chaddr, haddr, halen);
579 #ifdef NFS_BOOT_BOOTP_REQFILE
580 strncpy(bootp->bp_file, NFS_BOOT_BOOTP_REQFILE, sizeof(bootp->bp_file));
581 #endif
582 /* Fill-in the vendor data. */
583 memcpy(bootp->bp_vend, vm_rfc1048, 4);
584 #ifdef NFS_BOOT_DHCP
585 bootp->bp_vend[4] = TAG_DHCP_MSGTYPE;
586 bootp->bp_vend[5] = 1;
587 bootp->bp_vend[6] = DHCPDISCOVER;
588 /*
589 * Insert a NetBSD Vendor Class Identifier option.
590 */
591 snprintf(vci, sizeof(vci), "%s:%s:kernel:%s", ostype, MACHINE,
592 osrelease);
593 vcilen = strlen(vci);
594 bootp->bp_vend[7] = TAG_CLASSID;
595 bootp->bp_vend[8] = vcilen;
596 memcpy(&bootp->bp_vend[9], vci, vcilen);
597 bootp->bp_vend[9 + vcilen] = TAG_END;
598 #else
599 bootp->bp_vend[4] = TAG_END;
600 #endif
601
602 bpc.xid = xid;
603 bpc.haddr = haddr;
604 bpc.halen = halen;
605 bpc.replybuf = malloc(BOOTP_SIZE_MAX, M_DEVBUF, M_WAITOK);
606 if (bpc.replybuf == NULL)
607 panic("nfs_boot: malloc reply buf");
608 #ifdef NFS_BOOT_DHCP
609 bpc.expected_dhcpmsgtype = DHCPOFFER;
610 bpc.dhcp_ok = 0;
611 #endif
612
613 error = nfs_boot_sendrecv(so, nam, bootpset, m,
614 bootpcheck, 0, 0, &bpc, lwp);
615 if (error)
616 goto out;
617
618 #ifdef NFS_BOOT_DHCP
619 if (bpc.dhcp_ok) {
620 u_int32_t leasetime;
621 bootp->bp_vend[6] = DHCPREQUEST;
622 bootp->bp_vend[7] = TAG_REQ_ADDR;
623 bootp->bp_vend[8] = 4;
624 memcpy(&bootp->bp_vend[9], &bpc.replybuf->bp_yiaddr, 4);
625 bootp->bp_vend[13] = TAG_SERVERID;
626 bootp->bp_vend[14] = 4;
627 memcpy(&bootp->bp_vend[15], &bpc.dhcp_serverip.s_addr, 4);
628 bootp->bp_vend[19] = TAG_LEASETIME;
629 bootp->bp_vend[20] = 4;
630 leasetime = htonl(300);
631 memcpy(&bootp->bp_vend[21], &leasetime, 4);
632 bootp->bp_vend[25] = TAG_CLASSID;
633 bootp->bp_vend[26] = vcilen;
634 memcpy(&bootp->bp_vend[27], vci, vcilen);
635 bootp->bp_vend[27 + vcilen] = TAG_END;
636
637 bpc.expected_dhcpmsgtype = DHCPACK;
638
639 error = nfs_boot_sendrecv(so, nam, bootpset, m,
640 bootpcheck, 0, 0, &bpc, lwp);
641 if (error)
642 goto out;
643 }
644 #endif
645
646 /*
647 * bootpcheck() has copied the receive mbuf into
648 * the buffer at bpc.replybuf.
649 */
650 #ifdef NFS_BOOT_DHCP
651 printf("nfs_boot: %s next-server: %s\n",
652 (bpc.dhcp_ok ? "DHCP" : "BOOTP"),
653 #else
654 printf("nfs_boot: BOOTP next-server: %s\n",
655 #endif
656 inet_ntoa(bpc.replybuf->bp_siaddr));
657
658 bootp_extract(bpc.replybuf, bpc.replylen, nd);
659
660 out:
661 if (bpc.replybuf)
662 free(bpc.replybuf, M_DEVBUF);
663 if (m)
664 m_freem(m);
665 if (nam)
666 m_freem(nam);
667 soclose(so);
668 return (error);
669 }
670
671 static void
672 bootp_extract(bootp, replylen, nd)
673 struct bootp *bootp;
674 int replylen;
675 struct nfs_diskless *nd;
676 {
677 struct sockaddr_in *sin;
678 struct in_addr netmask;
679 struct in_addr gateway;
680 struct in_addr rootserver;
681 char *myname; /* my hostname */
682 char *mydomain; /* my domainname */
683 char *rootpath;
684 int mynamelen;
685 int mydomainlen;
686 int rootpathlen;
687 int overloaded;
688 u_int tag, len;
689 u_char *p, *limit;
690
691 /* Default these to "unspecified". */
692 netmask.s_addr = 0;
693 gateway.s_addr = 0;
694 mydomain = myname = rootpath = NULL;
695 mydomainlen = mynamelen = rootpathlen = 0;
696
697 /* default root server to bootp next-server */
698 rootserver = bootp->bp_siaddr;
699 /* assume that server name field is not overloaded by default */
700 overloaded = 0;
701
702 p = &bootp->bp_vend[4];
703 limit = ((u_char*)bootp) + replylen;
704 while (p < limit) {
705 tag = *p++;
706 if (tag == TAG_END)
707 break;
708 if (tag == TAG_PAD)
709 continue;
710 len = *p++;
711 #if 0 /* already done in bootpcheck() */
712 if ((p + len) > limit) {
713 printf("nfs_boot: option %d too long\n", tag);
714 break;
715 }
716 #endif
717 switch (tag) {
718 case TAG_SUBNET_MASK:
719 memcpy(&netmask, p, 4);
720 break;
721 case TAG_GATEWAY:
722 /* Routers */
723 memcpy(&gateway, p, 4);
724 break;
725 case TAG_HOST_NAME:
726 if (len >= sizeof(hostname)) {
727 printf("nfs_boot: host name >= %lu bytes",
728 (u_long)sizeof(hostname));
729 break;
730 }
731 myname = p;
732 mynamelen = len;
733 break;
734 case TAG_DOMAIN_NAME:
735 if (len >= sizeof(domainname)) {
736 printf("nfs_boot: domain name >= %lu bytes",
737 (u_long)sizeof(domainname));
738 break;
739 }
740 mydomain = p;
741 mydomainlen = len;
742 break;
743 case TAG_ROOT_PATH:
744 /* Leave some room for the server name. */
745 if (len >= (MNAMELEN-10)) {
746 printf("nfs_boot: rootpath >=%d bytes",
747 (MNAMELEN-10));
748 break;
749 }
750 rootpath = p;
751 rootpathlen = len;
752 break;
753 case TAG_SWAP_SERVER:
754 /* override NFS server address */
755 memcpy(&rootserver, p, 4);
756 break;
757 #ifdef NFS_BOOT_DHCP
758 case TAG_OVERLOAD:
759 if (len > 0 && ((*p & 0x02) != 0))
760 /*
761 * The server name field in the dhcp packet
762 * is overloaded and we can't find server
763 * name there.
764 */
765 overloaded = 1;
766 break;
767 #endif
768 default:
769 break;
770 }
771 p += len;
772 }
773
774 /*
775 * Store and print network config info.
776 */
777 if (myname) {
778 myname[mynamelen] = '\0';
779 strncpy(hostname, myname, sizeof(hostname));
780 hostnamelen = mynamelen;
781 printf("nfs_boot: my_name=%s\n", hostname);
782 }
783 if (mydomain) {
784 mydomain[mydomainlen] = '\0';
785 strncpy(domainname, mydomain, sizeof(domainname));
786 domainnamelen = mydomainlen;
787 printf("nfs_boot: my_domain=%s\n", domainname);
788 }
789 nd->nd_myip = bootp->bp_yiaddr;
790 if (nd->nd_myip.s_addr)
791 printf("nfs_boot: my_addr=%s\n", inet_ntoa(nd->nd_myip));
792 nd->nd_mask = netmask;
793 if (nd->nd_mask.s_addr)
794 printf("nfs_boot: my_mask=%s\n", inet_ntoa(nd->nd_mask));
795 nd->nd_gwip = gateway;
796 if (nd->nd_gwip.s_addr)
797 printf("nfs_boot: gateway=%s\n", inet_ntoa(nd->nd_gwip));
798
799 /*
800 * Store the information about our NFS root mount.
801 * The caller will print it, so be silent here.
802 */
803 {
804 struct nfs_dlmount *ndm = &nd->nd_root;
805
806 /* Server IP address. */
807 sin = (struct sockaddr_in *) &ndm->ndm_saddr;
808 memset((void *)sin, 0, sizeof(*sin));
809 sin->sin_len = sizeof(*sin);
810 sin->sin_family = AF_INET;
811 sin->sin_addr = rootserver;
812 /* Server name. */
813 if (!overloaded && bootp->bp_sname[0] != 0 &&
814 !memcmp(&rootserver, &bootp->bp_siaddr,
815 sizeof(struct in_addr))) {
816 /* standard root server, we have the name */
817 strncpy(ndm->ndm_host, bootp->bp_sname, BP_SNAME_LEN-1);
818 } else {
819 /* Show the server IP address numerically. */
820 strncpy(ndm->ndm_host, inet_ntoa(rootserver),
821 BP_SNAME_LEN-1);
822 }
823 len = strlen(ndm->ndm_host);
824 if (rootpath &&
825 len + 1 + rootpathlen + 1 <= sizeof(ndm->ndm_host)) {
826 ndm->ndm_host[len++] = ':';
827 strncpy(ndm->ndm_host + len,
828 rootpath, rootpathlen);
829 ndm->ndm_host[len + rootpathlen] = '\0';
830 } /* else: upper layer will handle error */
831 }
832
833 #ifdef TFTPROOT
834 #if BP_FILE_LEN > MNAMELEN
835 #define BOOTFILELEN MNAMELEN
836 #else
837 #define BOOTFILELEN BP_FILE_LEN
838 #endif
839 strncpy(nd->nd_bootfile, bootp->bp_file, BOOTFILELEN);
840 nd->nd_bootfile[BOOTFILELEN - 1] = '\0';
841 #undef BOOTFILELEN
842 #endif /* TFTPROOT */
843 }
844