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