Annotation of src/sys/nfs/nfs_bootdhcp.c, Revision 1.44
1.44 ! cegger 1: /* $NetBSD: nfs_bootdhcp.c,v 1.43 2008/10/24 17:17:12 cegger Exp $ */
1.1 gwr 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: */
1.18 lukem 45:
46: #include <sys/cdefs.h>
1.44 ! cegger 47: __KERNEL_RCSID(0, "$NetBSD: nfs_bootdhcp.c,v 1.43 2008/10/24 17:17:12 cegger Exp $");
1.1 gwr 48:
1.5 scottr 49: #include "opt_nfs_boot.h"
1.34 manu 50: #include "opt_tftproot.h"
1.5 scottr 51:
1.1 gwr 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>
1.3 drochner 65: #include <net/if_types.h>
1.1 gwr 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:
1.6 ross 74: #include <nfs/rpcv2.h>
75:
1.1 gwr 76: #include <nfs/nfsproto.h>
1.6 ross 77: #include <nfs/nfs.h>
78: #include <nfs/nfsmount.h>
1.1 gwr 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.
1.28 perry 97: *
1.1 gwr 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: */
1.15 gmcgarry 144: #define HTYPE_ETHERNET 1
145: #define HTYPE_IEEE802 6
1.1 gwr 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:
1.2 drochner 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
1.1 gwr 203:
1.2 drochner 204: #ifdef NFS_BOOT_DHCP
205: #define BOOTP_SIZE_MAX (sizeof(struct bootp)+312-64)
206: #else
1.1 gwr 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: */
1.2 drochner 213: #define BOOTP_SIZE_MAX (sizeof(struct bootp)+256-64)
214: #endif
1.1 gwr 215: #define BOOTP_SIZE_MIN (sizeof(struct bootp))
216:
217: /* Convenience macro */
218: #define INTOHL(ina) ((u_int32_t)ntohl((ina).s_addr))
219:
1.44 ! cegger 220: static int bootpc_call (struct nfs_diskless *, struct lwp *, int *);
! 221: static void bootp_extract (struct bootp *, int, struct nfs_diskless *, int *);
1.1 gwr 222:
1.30 christos 223: #ifdef DEBUG_NFS_BOOT_DHCP
224: #define DPRINTF(s) printf s
1.1 gwr 225: #else
1.30 christos 226: #define DPRINTF(s)
1.1 gwr 227: #endif
228:
229:
230: /*
231: * Get our boot parameters using BOOTP.
232: */
233: int
1.44 ! cegger 234: nfs_bootdhcp(struct nfs_diskless *nd, struct lwp *lwp, int *flags)
1.1 gwr 235: {
1.11 drochner 236: struct ifnet *ifp = nd->nd_ifp;
1.1 gwr 237: int error;
238:
239: /*
240: * Do enough of ifconfig(8) so that the chosen interface
241: * can talk to the servers. Use address zero for now.
242: */
1.44 ! cegger 243: error = nfs_boot_setaddress(ifp, lwp,
! 244: *flags & NFS_BOOT_HAS_MYIP ? nd->nd_myip.s_addr : INADDR_ANY,
! 245: *flags & NFS_BOOT_HAS_MASK ? nd->nd_mask.s_addr : INADDR_ANY,
! 246: INADDR_BROADCAST);
1.1 gwr 247: if (error) {
248: printf("nfs_boot: set ifaddr zero, error=%d\n", error);
1.11 drochner 249: return (error);
1.1 gwr 250: }
251:
252: /* This function call does the real send/recv work. */
1.44 ! cegger 253: error = bootpc_call(nd, lwp, flags);
1.11 drochner 254:
1.1 gwr 255: /* Get rid of the temporary (zero) IP address. */
1.29 christos 256: (void) nfs_boot_deladdress(ifp, lwp, INADDR_ANY);
1.11 drochner 257:
1.1 gwr 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: */
1.29 christos 265: error = nfs_boot_setaddress(ifp, lwp, nd->nd_myip.s_addr,
1.11 drochner 266: nd->nd_mask.s_addr, INADDR_ANY);
1.1 gwr 267: if (error) {
268: printf("nfs_boot: set ifaddr real, error=%d\n", error);
269: goto out;
270: }
271:
1.44 ! cegger 272: if ((*flags & NFS_BOOT_ALLINFO) != NFS_BOOT_ALLINFO)
! 273: return EADDRNOTAVAIL;
! 274:
1.1 gwr 275: out:
1.11 drochner 276: if (error) {
1.29 christos 277: (void) nfs_boot_ifupdown(ifp, lwp, 0);
1.11 drochner 278: nfs_boot_flushrt(ifp);
279: }
1.1 gwr 280: return (error);
281: }
282:
1.2 drochner 283: struct bootpcontext {
284: int xid;
1.36 dyoung 285: const u_char *haddr;
1.2 drochner 286: u_char halen;
287: struct bootp *replybuf;
288: int replylen;
289: #ifdef NFS_BOOT_DHCP
290: char expected_dhcpmsgtype, dhcp_ok;
291: struct in_addr dhcp_serverip;
292: #endif
293: };
294:
1.43 cegger 295: static int bootpset (struct mbuf*, void*, int);
296: static int bootpcheck (struct mbuf*, void*);
1.2 drochner 297:
1.7 drochner 298: static int
1.32 yamt 299: bootpset(struct mbuf *m, void *context, int waited)
1.2 drochner 300: {
301: struct bootp *bootp;
302:
303: /* we know it's contigous (in 1 mbuf cluster) */
304: bootp = mtod(m, struct bootp*);
305:
306: bootp->bp_secs = htons(waited);
307:
1.7 drochner 308: return (0);
1.2 drochner 309: }
310:
1.7 drochner 311: static int
1.43 cegger 312: bootpcheck(struct mbuf *m, void *context)
1.2 drochner 313: {
314: struct bootp *bootp;
315: struct bootpcontext *bpc = context;
316: u_int tag, len;
317: u_char *p, *limit;
318:
319: /*
320: * Is this a valid reply?
321: */
322: if (m->m_pkthdr.len < BOOTP_SIZE_MIN) {
1.30 christos 323: DPRINTF(("bootpcheck: short packet %d < %d\n", m->m_pkthdr.len,
324: BOOTP_SIZE_MIN));
1.7 drochner 325: return (-1);
1.2 drochner 326: }
327: if (m->m_pkthdr.len > BOOTP_SIZE_MAX) {
1.40 rumble 328: DPRINTF(("Bootpcheck: long packet %d > %d\n", m->m_pkthdr.len,
1.30 christos 329: BOOTP_SIZE_MAX));
1.7 drochner 330: return (-1);
1.2 drochner 331: }
332:
333: /*
334: * don't make first checks more expensive than necessary
335: */
1.20 simonb 336: if (m->m_len < offsetof(struct bootp, bp_sname)) {
337: m = m_pullup(m, offsetof(struct bootp, bp_sname));
1.30 christos 338: if (m == NULL) {
339: DPRINTF(("bootpcheck: m_pullup failed\n"));
1.7 drochner 340: return (-1);
1.30 christos 341: }
1.2 drochner 342: }
343: bootp = mtod(m, struct bootp*);
344:
345: if (bootp->bp_op != BOOTREPLY) {
1.30 christos 346: DPRINTF(("bootpcheck: op %d is not reply\n", bootp->bp_op));
1.7 drochner 347: return (-1);
1.2 drochner 348: }
349: if (bootp->bp_hlen != bpc->halen) {
1.30 christos 350: DPRINTF(("bootpcheck: hlen %d != %d\n", bootp->bp_hlen,
351: bpc->halen));
1.7 drochner 352: return (-1);
1.2 drochner 353: }
1.8 perry 354: if (memcmp(bootp->bp_chaddr, bpc->haddr, bpc->halen)) {
1.30 christos 355: #ifdef DEBUG_NFS_BOOT_DHCP
1.40 rumble 356: char *bp_chaddr, *haddr;
357:
358: bp_chaddr = malloc(3 * bpc->halen, M_TEMP, M_WAITOK);
359: haddr = malloc(3 * bpc->halen, M_TEMP, M_WAITOK);
360:
1.30 christos 361: DPRINTF(("bootpcheck: incorrect hwaddr %s != %s\n",
1.40 rumble 362: ether_snprintf(bp_chaddr, 3 * bpc->halen,
1.30 christos 363: bootp->bp_chaddr),
1.40 rumble 364: ether_snprintf(haddr, 3 * bpc->halen, bpc->haddr)));
365:
366: free(bp_chaddr, M_TEMP);
367: free(haddr, M_TEMP);
368: #endif
1.7 drochner 369: return (-1);
1.2 drochner 370: }
371: if (bootp->bp_xid != bpc->xid) {
1.30 christos 372: DPRINTF(("bootpcheck: xid %d != %d\n", bootp->bp_xid,
373: bpc->xid));
1.7 drochner 374: return (-1);
1.2 drochner 375: }
376:
377: /*
378: * OK, it's worth to look deeper.
379: * We copy the mbuf into a flat buffer here because
380: * m_pullup() is a bit limited for this purpose
381: * (doesn't allocate a cluster if necessary).
382: */
383: bpc->replylen = m->m_pkthdr.len;
1.33 christos 384: m_copydata(m, 0, bpc->replylen, (void *)bpc->replybuf);
1.2 drochner 385: bootp = bpc->replybuf;
386:
387: /*
1.7 drochner 388: * Check if the IP address we get looks correct.
389: * (DHCP servers can send junk to unknown clients.)
390: * XXX more checks might be needed
391: */
392: if (bootp->bp_yiaddr.s_addr == INADDR_ANY ||
393: bootp->bp_yiaddr.s_addr == INADDR_BROADCAST) {
1.12 drochner 394: printf("nfs_boot: wrong IP addr %s",
395: inet_ntoa(bootp->bp_yiaddr));
1.7 drochner 396: goto warn;
397: }
398:
399: /*
1.2 drochner 400: * Check the vendor data.
401: */
1.8 perry 402: if (memcmp(bootp->bp_vend, vm_rfc1048, 4)) {
1.7 drochner 403: printf("nfs_boot: reply missing options");
404: goto warn;
1.2 drochner 405: }
406: p = &bootp->bp_vend[4];
1.41 uwe 407: limit = ((u_char*)bootp) + bpc->replylen;
1.2 drochner 408: while (p < limit) {
409: tag = *p++;
410: if (tag == TAG_END)
411: break;
412: if (tag == TAG_PAD)
413: continue;
414: len = *p++;
415: if ((p + len) > limit) {
1.7 drochner 416: printf("nfs_boot: option %d too long", tag);
417: goto warn;
1.2 drochner 418: }
419: switch (tag) {
420: #ifdef NFS_BOOT_DHCP
1.19 thorpej 421: case TAG_DHCP_MSGTYPE:
1.2 drochner 422: if (*p != bpc->expected_dhcpmsgtype)
1.7 drochner 423: return (-1);
1.2 drochner 424: bpc->dhcp_ok = 1;
425: break;
1.19 thorpej 426: case TAG_SERVERID:
1.8 perry 427: memcpy(&bpc->dhcp_serverip.s_addr, p,
1.2 drochner 428: sizeof(bpc->dhcp_serverip.s_addr));
429: break;
430: #endif
1.19 thorpej 431: default:
1.2 drochner 432: break;
433: }
434: p += len;
435: }
1.7 drochner 436: return (0);
437:
438: warn:
1.12 drochner 439: printf(" (bad reply from %s)\n", inet_ntoa(bootp->bp_siaddr));
1.7 drochner 440: return (-1);
1.2 drochner 441: }
442:
1.1 gwr 443: static int
1.44 ! cegger 444: bootpc_call(struct nfs_diskless *nd, struct lwp *lwp, int *flags)
1.1 gwr 445: {
1.11 drochner 446: struct socket *so;
447: struct ifnet *ifp = nd->nd_ifp;
1.1 gwr 448: static u_int32_t xid = ~0xFF;
1.2 drochner 449: struct bootp *bootp; /* request */
1.1 gwr 450: struct mbuf *m, *nam;
451: struct sockaddr_in *sin;
1.2 drochner 452: int error;
1.36 dyoung 453: const u_char *haddr;
1.1 gwr 454: u_char hafmt, halen;
1.2 drochner 455: struct bootpcontext bpc;
1.19 thorpej 456: #ifdef NFS_BOOT_DHCP
457: char vci[64];
458: int vcilen;
459: #endif
1.1 gwr 460:
1.38 ad 461: error = socreate(AF_INET, &so, SOCK_DGRAM, 0, lwp, NULL);
1.11 drochner 462: if (error) {
463: printf("bootp: socreate, error=%d\n", error);
464: return (error);
465: }
466:
1.1 gwr 467: /*
468: * Initialize to NULL anything that will hold an allocation,
469: * and free each at the end if not null.
470: */
1.2 drochner 471: bpc.replybuf = NULL;
1.1 gwr 472: m = nam = NULL;
473:
474: /* Record our H/W (Ethernet) address. */
1.37 dyoung 475: { const struct sockaddr_dl *sdl = ifp->if_sadl;
1.3 drochner 476: switch (sdl->sdl_type) {
1.15 gmcgarry 477: case IFT_ISO88025:
478: hafmt = HTYPE_IEEE802;
479: break;
1.3 drochner 480: case IFT_ETHER:
481: case IFT_FDDI:
482: hafmt = HTYPE_ETHERNET;
483: break;
484: default:
485: printf("bootp: unsupported interface type %d\n",
486: sdl->sdl_type);
487: error = EINVAL;
488: goto out;
489: }
1.1 gwr 490: halen = sdl->sdl_alen;
1.36 dyoung 491: haddr = (const unsigned char *)CLLADDR(sdl);
1.1 gwr 492: }
493:
494: /*
495: * Skip the route table when sending on this socket.
496: * If this is not done, ip_output finds the loopback
497: * interface (why?) and then fails because broadcast
498: * is not supported on that interface...
499: */
1.42 plunky 500: { int32_t opt;
501:
502: opt = 1;
503: error = so_setsockopt(NULL, so, SOL_SOCKET, SO_DONTROUTE, &opt,
504: sizeof(opt));
1.1 gwr 505: }
506: if (error) {
1.30 christos 507: DPRINTF(("bootpc_call: SO_DONTROUTE failed %d\n", error));
1.1 gwr 508: goto out;
509: }
510:
511: /* Enable broadcast. */
1.2 drochner 512: if ((error = nfs_boot_enbroadcast(so))) {
1.30 christos 513: DPRINTF(("bootpc_call: SO_BROADCAST failed %d\n", error));
1.21 drochner 514: goto out;
515: }
516:
517: /*
518: * Set some TTL so we can boot through routers.
519: * Real BOOTP forwarding agents don't need this; they obey "bp_hops"
520: * and set "bp_giaddr", thus rewrite the packet anyway.
521: * The "helper-address" feature of some popular router vendor seems
522: * to do simple IP forwarding and drops packets with (ip_ttl == 1).
523: */
1.42 plunky 524: { u_char opt;
525:
526: opt = 7;
527: error = so_setsockopt(NULL, so, IPPROTO_IP, IP_MULTICAST_TTL,
528: &opt, sizeof(opt));
1.21 drochner 529: }
530: if (error) {
1.30 christos 531: DPRINTF(("bootpc_call: IP_MULTICAST_TTL failed %d\n", error));
1.1 gwr 532: goto out;
533: }
534:
535: /* Set the receive timeout for the socket. */
1.2 drochner 536: if ((error = nfs_boot_setrecvtimo(so))) {
1.30 christos 537: DPRINTF(("bootpc_call: SO_RCVTIMEO failed %d\n", error));
1.1 gwr 538: goto out;
539: }
540:
541: /*
542: * Bind the local endpoint to a bootp client port.
543: */
1.29 christos 544: if ((error = nfs_boot_sobind_ipport(so, IPPORT_BOOTPC, lwp))) {
1.30 christos 545: DPRINTF(("bootpc_call: bind failed %d\n", error));
1.1 gwr 546: goto out;
547: }
548:
549: /*
550: * Setup socket address for the server.
551: */
552: nam = m_get(M_WAIT, MT_SONAME);
553: sin = mtod(nam, struct sockaddr_in *);
554: sin->sin_len = nam->m_len = sizeof(*sin);
555: sin->sin_family = AF_INET;
556: sin->sin_addr.s_addr = INADDR_BROADCAST;
557: sin->sin_port = htons(IPPORT_BOOTPS);
558:
559: /*
1.2 drochner 560: * Allocate buffer used for request
1.1 gwr 561: */
1.2 drochner 562: m = m_gethdr(M_WAIT, MT_DATA);
1.22 matt 563: m_clget(m, M_WAIT);
1.2 drochner 564: bootp = mtod(m, struct bootp*);
565: m->m_pkthdr.len = m->m_len = BOOTP_SIZE_MAX;
566: m->m_pkthdr.rcvif = NULL;
1.1 gwr 567:
568: /*
1.2 drochner 569: * Build the BOOTP reqest message.
1.1 gwr 570: * Note: xid is host order! (opaque to server)
571: */
1.33 christos 572: memset((void *)bootp, 0, BOOTP_SIZE_MAX);
1.1 gwr 573: bootp->bp_op = BOOTREQUEST;
574: bootp->bp_htype = hafmt;
575: bootp->bp_hlen = halen; /* Hardware address length */
576: bootp->bp_xid = ++xid;
1.8 perry 577: memcpy(bootp->bp_chaddr, haddr, halen);
1.16 drochner 578: #ifdef NFS_BOOT_BOOTP_REQFILE
579: strncpy(bootp->bp_file, NFS_BOOT_BOOTP_REQFILE, sizeof(bootp->bp_file));
580: #endif
1.1 gwr 581: /* Fill-in the vendor data. */
1.8 perry 582: memcpy(bootp->bp_vend, vm_rfc1048, 4);
1.2 drochner 583: #ifdef NFS_BOOT_DHCP
584: bootp->bp_vend[4] = TAG_DHCP_MSGTYPE;
585: bootp->bp_vend[5] = 1;
586: bootp->bp_vend[6] = DHCPDISCOVER;
1.19 thorpej 587: /*
588: * Insert a NetBSD Vendor Class Identifier option.
589: */
1.25 itojun 590: snprintf(vci, sizeof(vci), "%s:%s:kernel:%s", ostype, MACHINE,
591: osrelease);
1.19 thorpej 592: vcilen = strlen(vci);
593: bootp->bp_vend[7] = TAG_CLASSID;
594: bootp->bp_vend[8] = vcilen;
595: memcpy(&bootp->bp_vend[9], vci, vcilen);
596: bootp->bp_vend[9 + vcilen] = TAG_END;
1.2 drochner 597: #else
1.1 gwr 598: bootp->bp_vend[4] = TAG_END;
1.2 drochner 599: #endif
600:
601: bpc.xid = xid;
602: bpc.haddr = haddr;
603: bpc.halen = halen;
604: bpc.replybuf = malloc(BOOTP_SIZE_MAX, M_DEVBUF, M_WAITOK);
605: if (bpc.replybuf == NULL)
606: panic("nfs_boot: malloc reply buf");
607: #ifdef NFS_BOOT_DHCP
608: bpc.expected_dhcpmsgtype = DHCPOFFER;
609: bpc.dhcp_ok = 0;
610: #endif
1.1 gwr 611:
1.2 drochner 612: error = nfs_boot_sendrecv(so, nam, bootpset, m,
1.29 christos 613: bootpcheck, 0, 0, &bpc, lwp);
1.2 drochner 614: if (error)
1.1 gwr 615: goto out;
1.2 drochner 616:
617: #ifdef NFS_BOOT_DHCP
618: if (bpc.dhcp_ok) {
619: u_int32_t leasetime;
620: bootp->bp_vend[6] = DHCPREQUEST;
621: bootp->bp_vend[7] = TAG_REQ_ADDR;
622: bootp->bp_vend[8] = 4;
1.8 perry 623: memcpy(&bootp->bp_vend[9], &bpc.replybuf->bp_yiaddr, 4);
1.2 drochner 624: bootp->bp_vend[13] = TAG_SERVERID;
625: bootp->bp_vend[14] = 4;
1.8 perry 626: memcpy(&bootp->bp_vend[15], &bpc.dhcp_serverip.s_addr, 4);
1.2 drochner 627: bootp->bp_vend[19] = TAG_LEASETIME;
628: bootp->bp_vend[20] = 4;
629: leasetime = htonl(300);
1.8 perry 630: memcpy(&bootp->bp_vend[21], &leasetime, 4);
1.19 thorpej 631: bootp->bp_vend[25] = TAG_CLASSID;
632: bootp->bp_vend[26] = vcilen;
633: memcpy(&bootp->bp_vend[27], vci, vcilen);
634: bootp->bp_vend[27 + vcilen] = TAG_END;
1.2 drochner 635:
636: bpc.expected_dhcpmsgtype = DHCPACK;
637:
638: error = nfs_boot_sendrecv(so, nam, bootpset, m,
1.29 christos 639: bootpcheck, 0, 0, &bpc, lwp);
1.2 drochner 640: if (error)
641: goto out;
1.1 gwr 642: }
1.2 drochner 643: #endif
1.1 gwr 644:
645: /*
1.2 drochner 646: * bootpcheck() has copied the receive mbuf into
647: * the buffer at bpc.replybuf.
1.1 gwr 648: */
1.7 drochner 649: #ifdef NFS_BOOT_DHCP
1.17 kim 650: printf("nfs_boot: %s next-server: %s\n",
1.7 drochner 651: (bpc.dhcp_ok ? "DHCP" : "BOOTP"),
652: #else
1.17 kim 653: printf("nfs_boot: BOOTP next-server: %s\n",
1.7 drochner 654: #endif
1.12 drochner 655: inet_ntoa(bpc.replybuf->bp_siaddr));
1.7 drochner 656:
1.44 ! cegger 657: bootp_extract(bpc.replybuf, bpc.replylen, nd, flags);
1.2 drochner 658:
659: out:
660: if (bpc.replybuf)
661: free(bpc.replybuf, M_DEVBUF);
662: if (m)
663: m_freem(m);
664: if (nam)
665: m_freem(nam);
1.11 drochner 666: soclose(so);
1.2 drochner 667: return (error);
668: }
1.1 gwr 669:
1.7 drochner 670: static void
1.44 ! cegger 671: bootp_extract(struct bootp *bootp, int replylen,
! 672: struct nfs_diskless *nd, int *flags)
1.2 drochner 673: {
674: struct sockaddr_in *sin;
675: struct in_addr netmask;
676: struct in_addr gateway;
677: struct in_addr rootserver;
678: char *myname; /* my hostname */
679: char *mydomain; /* my domainname */
680: char *rootpath;
681: int mynamelen;
682: int mydomainlen;
683: int rootpathlen;
1.13 enami 684: int overloaded;
1.2 drochner 685: u_int tag, len;
686: u_char *p, *limit;
1.1 gwr 687:
1.2 drochner 688: /* Default these to "unspecified". */
689: netmask.s_addr = 0;
690: gateway.s_addr = 0;
691: mydomain = myname = rootpath = NULL;
692: mydomainlen = mynamelen = rootpathlen = 0;
1.34 manu 693:
1.2 drochner 694: /* default root server to bootp next-server */
695: rootserver = bootp->bp_siaddr;
1.13 enami 696: /* assume that server name field is not overloaded by default */
697: overloaded = 0;
1.2 drochner 698:
699: p = &bootp->bp_vend[4];
1.41 uwe 700: limit = ((u_char*)bootp) + replylen;
1.2 drochner 701: while (p < limit) {
702: tag = *p++;
703: if (tag == TAG_END)
704: break;
705: if (tag == TAG_PAD)
1.1 gwr 706: continue;
1.2 drochner 707: len = *p++;
1.7 drochner 708: #if 0 /* already done in bootpcheck() */
1.2 drochner 709: if ((p + len) > limit) {
710: printf("nfs_boot: option %d too long\n", tag);
711: break;
712: }
1.7 drochner 713: #endif
1.2 drochner 714: switch (tag) {
715: case TAG_SUBNET_MASK:
1.8 perry 716: memcpy(&netmask, p, 4);
1.2 drochner 717: break;
718: case TAG_GATEWAY:
719: /* Routers */
1.8 perry 720: memcpy(&gateway, p, 4);
1.2 drochner 721: break;
722: case TAG_HOST_NAME:
723: if (len >= sizeof(hostname)) {
1.10 thorpej 724: printf("nfs_boot: host name >= %lu bytes",
725: (u_long)sizeof(hostname));
1.1 gwr 726: break;
1.2 drochner 727: }
728: myname = p;
729: mynamelen = len;
730: break;
731: case TAG_DOMAIN_NAME:
732: if (len >= sizeof(domainname)) {
1.10 thorpej 733: printf("nfs_boot: domain name >= %lu bytes",
734: (u_long)sizeof(domainname));
1.1 gwr 735: break;
736: }
1.2 drochner 737: mydomain = p;
738: mydomainlen = len;
739: break;
740: case TAG_ROOT_PATH:
741: /* Leave some room for the server name. */
742: if (len >= (MNAMELEN-10)) {
743: printf("nfs_boot: rootpath >=%d bytes",
744: (MNAMELEN-10));
1.1 gwr 745: break;
746: }
1.2 drochner 747: rootpath = p;
748: rootpathlen = len;
749: break;
750: case TAG_SWAP_SERVER:
751: /* override NFS server address */
1.8 perry 752: memcpy(&rootserver, p, 4);
1.2 drochner 753: break;
1.13 enami 754: #ifdef NFS_BOOT_DHCP
755: case TAG_OVERLOAD:
756: if (len > 0 && ((*p & 0x02) != 0))
757: /*
758: * The server name field in the dhcp packet
759: * is overloaded and we can't find server
760: * name there.
761: */
762: overloaded = 1;
763: break;
764: #endif
1.2 drochner 765: default:
766: break;
1.1 gwr 767: }
1.2 drochner 768: p += len;
1.1 gwr 769: }
770:
771: /*
772: * Store and print network config info.
773: */
774: if (myname) {
775: myname[mynamelen] = '\0';
776: strncpy(hostname, myname, sizeof(hostname));
777: hostnamelen = mynamelen;
778: printf("nfs_boot: my_name=%s\n", hostname);
779: }
780: if (mydomain) {
781: mydomain[mydomainlen] = '\0';
782: strncpy(domainname, mydomain, sizeof(domainname));
783: domainnamelen = mydomainlen;
784: printf("nfs_boot: my_domain=%s\n", domainname);
785: }
1.44 ! cegger 786: if (!(*flags & NFS_BOOT_HAS_MYIP)) {
! 787: nd->nd_myip = bootp->bp_yiaddr;
1.12 drochner 788: printf("nfs_boot: my_addr=%s\n", inet_ntoa(nd->nd_myip));
1.44 ! cegger 789: *flags |= NFS_BOOT_HAS_MYIP;
! 790: }
! 791: if (!(*flags & NFS_BOOT_HAS_MASK)) {
! 792: nd->nd_mask = netmask;
1.12 drochner 793: printf("nfs_boot: my_mask=%s\n", inet_ntoa(nd->nd_mask));
1.44 ! cegger 794: *flags |= NFS_BOOT_HAS_MASK;
! 795: }
! 796: if (!(*flags & NFS_BOOT_HAS_GWIP)) {
! 797: nd->nd_gwip = gateway;
1.12 drochner 798: printf("nfs_boot: gateway=%s\n", inet_ntoa(nd->nd_gwip));
1.44 ! cegger 799: *flags |= NFS_BOOT_HAS_GWIP;
! 800: }
1.1 gwr 801:
802: /*
803: * Store the information about our NFS root mount.
804: * The caller will print it, so be silent here.
805: */
1.44 ! cegger 806: do {
1.1 gwr 807: struct nfs_dlmount *ndm = &nd->nd_root;
808:
1.44 ! cegger 809:
! 810: if (!(*flags & NFS_BOOT_HAS_SERVADDR)) {
! 811: /* Server IP address. */
! 812: sin = (struct sockaddr_in *) &ndm->ndm_saddr;
! 813: memset((void *)sin, 0, sizeof(*sin));
! 814: sin->sin_len = sizeof(*sin);
! 815: sin->sin_family = AF_INET;
! 816: sin->sin_addr = rootserver;
! 817: *flags |= NFS_BOOT_HAS_SERVADDR;
1.1 gwr 818: }
1.44 ! cegger 819:
! 820: if (!(*flags & NFS_BOOT_HAS_SERVER)) {
! 821: /* Server name. */
! 822: if (!overloaded && bootp->bp_sname[0] != 0 &&
! 823: !memcmp(&rootserver, &bootp->bp_siaddr,
! 824: sizeof(struct in_addr)))
! 825: {
! 826: /* standard root server, we have the name */
! 827: strncpy(ndm->ndm_host, bootp->bp_sname,
! 828: BP_SNAME_LEN-1);
! 829: *flags |= NFS_BOOT_HAS_SERVER;
! 830: } else {
! 831: /* Show the server IP address numerically. */
! 832: strncpy(ndm->ndm_host, inet_ntoa(rootserver),
! 833: BP_SNAME_LEN-1);
! 834: *flags |= NFS_BOOT_HAS_SERVER;
! 835: }
! 836: }
! 837:
! 838: if (!(*flags & NFS_BOOT_HAS_ROOTPATH)) {
! 839: len = strlen(ndm->ndm_host);
! 840: if (rootpath &&
! 841: len + 1 + rootpathlen + 1 <= sizeof(ndm->ndm_host))
! 842: {
! 843: ndm->ndm_host[len++] = ':';
! 844: strncpy(ndm->ndm_host + len,
! 845: rootpath, rootpathlen);
! 846: ndm->ndm_host[len + rootpathlen] = '\0';
! 847: *flags |= NFS_BOOT_HAS_ROOTPATH;
! 848: } /* else: upper layer will handle error */
! 849: }
! 850: } while(0);
1.34 manu 851:
852: #ifdef TFTPROOT
853: #if BP_FILE_LEN > MNAMELEN
854: #define BOOTFILELEN MNAMELEN
855: #else
856: #define BOOTFILELEN BP_FILE_LEN
857: #endif
858: strncpy(nd->nd_bootfile, bootp->bp_file, BOOTFILELEN);
859: nd->nd_bootfile[BOOTFILELEN - 1] = '\0';
860: #undef BOOTFILELEN
861: #endif /* TFTPROOT */
1.1 gwr 862: }
CVSweb <webmaster@jp.NetBSD.org>