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