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