/*-
* Copyright (c) 1995, 1997 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Adam Glass and Gordon W. Ross.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Support for NFS diskless booting with BOOTP (RFC951, RFC1048)
*
* History:
*
* Tor Egge developed the initial version of this code based on
* the Sun RPC/bootparam sources nfs_boot.c and krpc_subr.c and
* submitted that work to NetBSD as bugreport "kern/2351" on
* 29 Apr 1996.
*
* Gordon Ross reorganized Tor's version into this form and
* integrated it into the NetBSD sources during Aug 1997.
*/
/*
* There are two implementations of NFS diskless boot.
* This implementation uses BOOTP (RFC951, RFC1048), and
* the other uses Sun RPC/bootparams (nfs_bootparam.c).
*
* This method gets everything it needs with one BOOTP
* request and reply. Note that this actually uses only
* the old BOOTP functionality subset of DHCP. It is not
* clear that DHCP provides any advantage over BOOTP for
* diskless boot. DHCP allows the server to assign an IP
* address without any a-priori knowledge of the client,
* but we require that the server has a-priori knowledge
* of the client so it can export our (unique) NFS root.
* Given that the server needs a-priori knowledge about
* the client anyway, it might as well assign a fixed IP
* address for the client and support BOOTP.
*
* On the other hand, disk-FULL clients may use DHCP, but
* in that case the DHCP client should be user-mode code,
* and has no bearing on the code below. -gwr
*/
/* Begin stuff from bootp.h */
/* Definitions from RFC951 */
#define BP_CHADDR_LEN 16
#define BP_SNAME_LEN 64
#define BP_FILE_LEN 128
#define BP_VEND_LEN 64
struct bootp {
u_int8_t bp_op; /* packet opcode type */
u_int8_t bp_htype; /* hardware addr type */
u_int8_t bp_hlen; /* hardware addr length */
u_int8_t bp_hops; /* gateway hops */
u_int32_t bp_xid; /* transaction ID */
u_int16_t bp_secs; /* seconds since boot began */
u_int16_t bp_flags; /* RFC1532 broadcast, etc. */
struct in_addr bp_ciaddr; /* client IP address */
struct in_addr bp_yiaddr; /* 'your' IP address */
struct in_addr bp_siaddr; /* server IP address */
struct in_addr bp_giaddr; /* gateway IP address */
u_int8_t bp_chaddr[BP_CHADDR_LEN]; /* client hardware address */
char bp_sname[BP_SNAME_LEN]; /* server host name */
char bp_file[BP_FILE_LEN]; /* boot file name */
u_int8_t bp_vend[BP_VEND_LEN]; /* RFC1048 options */
/*
* Note that BOOTP packets are allowed to be longer
* (see RFC 1532 sect. 2.1) and common practice is to
* allow the option data in bp_vend to extend into the
* additional space provided in longer packets.
*/
};
#define IPPORT_BOOTPS 67
#define IPPORT_BOOTPC 68
#define BOOTREQUEST 1
#define BOOTREPLY 2
/*
* Is this available from the sockaddr_dl somehow?
* Perhaps (struct arphdr)->ar_hrd = ARPHRD_ETHER?
* The interface has ->if_type but not the ARP fmt.
*/
#define HTYPE_ETHERNET 1
#define HTYPE_IEEE802 6
#ifdef NFS_BOOT_DHCP
#define BOOTP_SIZE_MAX (sizeof(struct bootp)+312-64)
#else
/*
* The "extended" size is somewhat arbitrary, but is
* constrained by the maximum message size specified
* by RFC1533 (567 total). This value increases the
* space for options from 64 bytes to 256 bytes.
*/
#define BOOTP_SIZE_MAX (sizeof(struct bootp)+256-64)
#endif
#define BOOTP_SIZE_MIN (sizeof(struct bootp))
static int bootpc_call (struct nfs_diskless *, struct lwp *, int *);
static void bootp_extract (struct bootp *, int, struct nfs_diskless *, int *);
#ifdef DEBUG_NFS_BOOT_DHCP
#define DPRINTF(s) printf s
#else
#define DPRINTF(s)
#endif
/*
* Get our boot parameters using BOOTP.
*/
int
nfs_bootdhcp(struct nfs_diskless *nd, struct lwp *lwp, int *flags)
{
struct ifnet *ifp = nd->nd_ifp;
int error;
/*
* Do enough of ifconfig(8) so that the chosen interface
* can talk to the servers. Use address zero for now.
*/
error = nfs_boot_setaddress(ifp, lwp,
*flags & NFS_BOOT_HAS_MYIP ? nd->nd_myip.s_addr : INADDR_ANY,
*flags & NFS_BOOT_HAS_MASK ? nd->nd_mask.s_addr : INADDR_ANY,
INADDR_BROADCAST);
if (error) {
printf("nfs_boot: set ifaddr zero, error=%d\n", error);
return (error);
}
/* This function call does the real send/recv work. */
error = bootpc_call(nd, lwp, flags);
/* Get rid of the temporary (zero) IP address. */
(void) nfs_boot_deladdress(ifp, lwp, INADDR_ANY);
/* NOW we can test the error from bootpc_call. */
if (error)
goto out;
/*
* Do ifconfig with our real IP address and mask.
*/
error = nfs_boot_setaddress(ifp, lwp, nd->nd_myip.s_addr,
nd->nd_mask.s_addr, INADDR_ANY);
if (error) {
printf("nfs_boot: set ifaddr real, error=%d\n", error);
goto out;
}
/*
* OK, it's worth to look deeper.
* We copy the mbuf into a flat buffer here because
* m_pullup() is a bit limited for this purpose
* (doesn't allocate a cluster if necessary).
*/
bpc->replylen = m->m_pkthdr.len;
m_copydata(m, 0, bpc->replylen, (void *)bpc->replybuf);
bootp = bpc->replybuf;
/*
* Check if the IP address we get looks correct.
* (DHCP servers can send junk to unknown clients.)
* XXX more checks might be needed
*/
if (bootp->bp_yiaddr.s_addr == INADDR_ANY ||
bootp->bp_yiaddr.s_addr == INADDR_BROADCAST) {
printf("nfs_boot: wrong IP addr %s",
inet_ntoa(bootp->bp_yiaddr));
goto warn;
}
/*
* Check the vendor data.
*/
if (memcmp(bootp->bp_vend, vm_rfc1048, 4)) {
printf("nfs_boot: reply missing options");
goto warn;
}
p = &bootp->bp_vend[4];
limit = ((u_char*)bootp) + bpc->replylen;
while (p < limit) {
tag = *p++;
if (tag == TAG_END)
break;
if (tag == TAG_PAD)
continue;
len = *p++;
if ((p + len) > limit) {
printf("nfs_boot: option %d too long", tag);
goto warn;
}
switch (tag) {
#ifdef NFS_BOOT_DHCP
case TAG_DHCP_MSGTYPE:
if (*p != bpc->expected_dhcpmsgtype)
return (-1);
bpc->dhcp_ok = 1;
break;
case TAG_SERVERID:
memcpy(&bpc->dhcp_serverip.s_addr, p,
sizeof(bpc->dhcp_serverip.s_addr));
break;
#endif
default:
break;
}
p += len;
}
return (0);
warn:
printf(" (bad reply from %s)\n", inet_ntoa(bootp->bp_siaddr));
return (-1);
}
/*
* Initialize to NULL anything that will hold an allocation,
* and free each at the end if not null.
*/
bpc.replybuf = NULL;
m = NULL;
/* Record our H/W (Ethernet) address. */
{ const struct sockaddr_dl *sdl = ifp->if_sadl;
switch (sdl->sdl_type) {
case IFT_ISO88025:
hafmt = HTYPE_IEEE802;
break;
case IFT_ETHER:
case IFT_FDDI:
hafmt = HTYPE_ETHERNET;
break;
default:
printf("bootp: unsupported interface type %d\n",
sdl->sdl_type);
error = EINVAL;
goto out;
}
halen = sdl->sdl_alen;
haddr = (const unsigned char *)CLLADDR(sdl);
}
/*
* Skip the route table when sending on this socket.
* If this is not done, ip_output finds the loopback
* interface (why?) and then fails because broadcast
* is not supported on that interface...
*/
{ int32_t opt;
/*
* Set some TTL so we can boot through routers.
* Real BOOTP forwarding agents don't need this; they obey "bp_hops"
* and set "bp_giaddr", thus rewrite the packet anyway.
* The "helper-address" feature of some popular router vendor seems
* to do simple IP forwarding and drops packets with (ip_ttl == 1).
*/
{ u_char opt;
/* Set the receive timeout for the socket. */
if ((error = nfs_boot_setrecvtimo(so))) {
DPRINTF(("bootpc_call: SO_RCVTIMEO failed %d\n", error));
goto out;
}
/*
* Bind the local endpoint to a bootp client port.
*/
if ((error = nfs_boot_sobind_ipport(so, IPPORT_BOOTPC, lwp))) {
DPRINTF(("bootpc_call: bind failed %d\n", error));
goto out;
}
/* default root server to bootp next-server */
rootserver = bootp->bp_siaddr;
/* assume that server name field is not overloaded by default */
overloaded = 0;
/* MTU can't be less than IP_MIN_MTU, set to 0 to indicate unset */
myinterfacemtu = 0;
p = &bootp->bp_vend[4];
limit = ((u_char*)bootp) + replylen;
while (p < limit) {
tag = *p++;
if (tag == TAG_END)
break;
if (tag == TAG_PAD)
continue;
len = *p++;
#if 0 /* already done in bootpcheck() */
if ((p + len) > limit) {
printf("nfs_boot: option %d too long\n", tag);
break;
}
#endif
switch (tag) {
case TAG_SUBNET_MASK:
if (len < 4) {
printf("nfs_boot: subnet mask < 4 bytes\n");
break;
}
memcpy(&netmask, p, 4);
break;
case TAG_GATEWAY:
/* Routers */
if (len < 4) {
printf("nfs_boot: routers < 4 bytes\n");
break;
}
memcpy(&gateway, p, 4);
break;
case TAG_HOST_NAME:
if (len >= sizeof(hostname)) {
printf("nfs_boot: host name >= %lu bytes\n",
(u_long)sizeof(hostname));
break;
}
myname = p;
mynamelen = len;
break;
case TAG_DOMAIN_NAME:
if (len >= sizeof(domainname)) {
printf("nfs_boot: domain name >= %lu bytes\n",
(u_long)sizeof(domainname));
break;
}
mydomain = p;
mydomainlen = len;
break;
case TAG_ROOT_PATH:
/* Leave some room for the server name. */
if (len >= (MNAMELEN-10)) {
printf("nfs_boot: rootpath >= %d bytes\n",
(MNAMELEN-10));
break;
}
rootpath = p;
rootpathlen = len;
break;
case TAG_INTERFACE_MTU:
if (len != 2) {
printf("nfs_boot: interface-mtu len != 2 (%d)",
len);
break;
}
memcpy(&myinterfacemtu, p, 2);
myinterfacemtu = ntohs(myinterfacemtu);
break;
case TAG_SWAP_SERVER:
/* override NFS server address */
if (len < 4) {
printf("nfs_boot: swap server < 4 bytes\n");
break;
}
memcpy(&rootserver, p, 4);
break;
#ifdef NFS_BOOT_DHCP
case TAG_OVERLOAD:
if (len > 0 && ((*p & 0x02) != 0))
/*
* The server name field in the dhcp packet
* is overloaded and we can't find server
* name there.
*/
overloaded = 1;
break;
#endif
default:
break;
}
p += len;
}
/*
* Store the information about our NFS root mount.
* The caller will print it, so be silent here.
*/
do {
struct nfs_dlmount *ndm = &nd->nd_root;
if (!(*flags & NFS_BOOT_HAS_SERVADDR)) {
/* Server IP address. */
sin = (struct sockaddr_in *) &ndm->ndm_saddr;
memset((void *)sin, 0, sizeof(*sin));
sin->sin_len = sizeof(*sin);
sin->sin_family = AF_INET;
sin->sin_addr = rootserver;
*flags |= NFS_BOOT_HAS_SERVADDR;
}
if (!(*flags & NFS_BOOT_HAS_SERVER)) {
/* Server name. */
if (!overloaded && bootp->bp_sname[0] != 0 &&
!memcmp(&rootserver, &bootp->bp_siaddr,
sizeof(struct in_addr)))
{
/* standard root server, we have the name */
strncpy(ndm->ndm_host, bootp->bp_sname,
BP_SNAME_LEN-1);
*flags |= NFS_BOOT_HAS_SERVER;
} else {
/* Show the server IP address numerically. */
strncpy(ndm->ndm_host, inet_ntoa(rootserver),
BP_SNAME_LEN-1);
*flags |= NFS_BOOT_HAS_SERVER;
}
}
if (!(*flags & NFS_BOOT_HAS_ROOTPATH)) {
len = strlen(ndm->ndm_host);
if (rootpath &&
len + 1 + rootpathlen + 1 <= sizeof(ndm->ndm_host))
{
ndm->ndm_host[len++] = ':';
strncpy(ndm->ndm_host + len,
rootpath, rootpathlen);
ndm->ndm_host[len + rootpathlen] = '\0';
*flags |= NFS_BOOT_HAS_ROOTPATH;
} /* else: upper layer will handle error */
}
} while(0);