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