2010-10-05 03:48:22 +04:00
|
|
|
/* $NetBSD: nfs_bootdhcp.c,v 1.52 2010/10/04 23:48:22 cyber Exp $ */
|
1997-08-29 20:10:31 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2001-11-10 13:59:08 +03:00
|
|
|
#include <sys/cdefs.h>
|
2010-10-05 03:48:22 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: nfs_bootdhcp.c,v 1.52 2010/10/04 23:48:22 cyber Exp $");
|
2001-11-10 13:59:08 +03:00
|
|
|
|
2008-11-19 21:35:57 +03:00
|
|
|
#ifdef _KERNEL_OPT
|
1998-01-13 00:27:09 +03:00
|
|
|
#include "opt_nfs_boot.h"
|
2007-05-08 10:10:27 +04:00
|
|
|
#include "opt_tftproot.h"
|
2008-11-19 21:35:57 +03:00
|
|
|
#endif
|
1998-01-13 00:27:09 +03:00
|
|
|
|
1997-08-29 20:10:31 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/device.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/reboot.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
1998-01-09 18:13:44 +03:00
|
|
|
#include <net/if_types.h>
|
1997-08-29 20:10:31 +04:00
|
|
|
#include <net/if_arp.h> /* ARPHRD_ETHER, etc. */
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
#include <net/if_ether.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/if_inarp.h>
|
|
|
|
|
1998-03-01 10:15:39 +03:00
|
|
|
#include <nfs/rpcv2.h>
|
|
|
|
|
1997-08-29 20:10:31 +04:00
|
|
|
#include <nfs/nfsproto.h>
|
1998-03-01 10:15:39 +03:00
|
|
|
#include <nfs/nfs.h>
|
|
|
|
#include <nfs/nfsmount.h>
|
1997-08-29 20:10:31 +04:00
|
|
|
#include <nfs/nfsdiskless.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
2005-02-27 01:31:44 +03:00
|
|
|
*
|
1997-08-29 20:10:31 +04:00
|
|
|
* 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.
|
|
|
|
*/
|
2000-05-28 11:01:09 +04:00
|
|
|
#define HTYPE_ETHERNET 1
|
|
|
|
#define HTYPE_IEEE802 6
|
1997-08-29 20:10:31 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Vendor magic cookie (v_magic) for RFC1048
|
|
|
|
*/
|
|
|
|
static const u_int8_t vm_rfc1048[4] = { 99, 130, 83, 99 };
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tag values used to specify what information is being supplied in
|
|
|
|
* the vendor (options) data area of the packet.
|
|
|
|
*/
|
|
|
|
/* RFC 1048 */
|
|
|
|
#define TAG_END ((unsigned char) 255)
|
|
|
|
#define TAG_PAD ((unsigned char) 0)
|
|
|
|
#define TAG_SUBNET_MASK ((unsigned char) 1)
|
|
|
|
#define TAG_TIME_OFFSET ((unsigned char) 2)
|
|
|
|
#define TAG_GATEWAY ((unsigned char) 3)
|
|
|
|
#define TAG_TIME_SERVER ((unsigned char) 4)
|
|
|
|
#define TAG_NAME_SERVER ((unsigned char) 5)
|
|
|
|
#define TAG_DOMAIN_SERVER ((unsigned char) 6)
|
|
|
|
#define TAG_LOG_SERVER ((unsigned char) 7)
|
|
|
|
#define TAG_COOKIE_SERVER ((unsigned char) 8)
|
|
|
|
#define TAG_LPR_SERVER ((unsigned char) 9)
|
|
|
|
#define TAG_IMPRESS_SERVER ((unsigned char) 10)
|
|
|
|
#define TAG_RLP_SERVER ((unsigned char) 11)
|
|
|
|
#define TAG_HOST_NAME ((unsigned char) 12)
|
|
|
|
#define TAG_BOOT_SIZE ((unsigned char) 13)
|
|
|
|
/* RFC 1395 */
|
|
|
|
#define TAG_DUMP_FILE ((unsigned char) 14)
|
|
|
|
#define TAG_DOMAIN_NAME ((unsigned char) 15)
|
|
|
|
#define TAG_SWAP_SERVER ((unsigned char) 16)
|
|
|
|
#define TAG_ROOT_PATH ((unsigned char) 17)
|
2010-10-05 03:48:22 +04:00
|
|
|
/* RFC 2132 */
|
|
|
|
#define TAG_INTERFACE_MTU ((unsigned char) 26)
|
1997-08-29 20:10:31 +04:00
|
|
|
/* End of stuff from bootp.h */
|
|
|
|
|
1997-10-01 00:51:03 +04:00
|
|
|
#ifdef NFS_BOOT_DHCP
|
|
|
|
#define TAG_REQ_ADDR ((unsigned char) 50)
|
|
|
|
#define TAG_LEASETIME ((unsigned char) 51)
|
|
|
|
#define TAG_OVERLOAD ((unsigned char) 52)
|
|
|
|
#define TAG_DHCP_MSGTYPE ((unsigned char) 53)
|
|
|
|
#define TAG_SERVERID ((unsigned char) 54)
|
|
|
|
#define TAG_PARAM_REQ ((unsigned char) 55)
|
|
|
|
#define TAG_MSG ((unsigned char) 56)
|
|
|
|
#define TAG_MAXSIZE ((unsigned char) 57)
|
|
|
|
#define TAG_T1 ((unsigned char) 58)
|
|
|
|
#define TAG_T2 ((unsigned char) 59)
|
|
|
|
#define TAG_CLASSID ((unsigned char) 60)
|
|
|
|
#define TAG_CLIENTID ((unsigned char) 61)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef NFS_BOOT_DHCP
|
|
|
|
#define DHCPDISCOVER 1
|
|
|
|
#define DHCPOFFER 2
|
|
|
|
#define DHCPREQUEST 3
|
|
|
|
#define DHCPDECLINE 4
|
|
|
|
#define DHCPACK 5
|
|
|
|
#define DHCPNAK 6
|
|
|
|
#define DHCPRELEASE 7
|
|
|
|
#endif
|
1997-08-29 20:10:31 +04:00
|
|
|
|
2010-10-05 03:48:22 +04:00
|
|
|
#define IP_MIN_MTU 576
|
|
|
|
|
1997-10-01 00:51:03 +04:00
|
|
|
#ifdef NFS_BOOT_DHCP
|
|
|
|
#define BOOTP_SIZE_MAX (sizeof(struct bootp)+312-64)
|
|
|
|
#else
|
1997-08-29 20:10:31 +04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
1997-10-01 00:51:03 +04:00
|
|
|
#define BOOTP_SIZE_MAX (sizeof(struct bootp)+256-64)
|
|
|
|
#endif
|
1997-08-29 20:10:31 +04:00
|
|
|
#define BOOTP_SIZE_MIN (sizeof(struct bootp))
|
|
|
|
|
|
|
|
/* Convenience macro */
|
|
|
|
#define INTOHL(ina) ((u_int32_t)ntohl((ina).s_addr))
|
|
|
|
|
2008-10-27 13:58:22 +03:00
|
|
|
static int bootpc_call (struct nfs_diskless *, struct lwp *, int *);
|
|
|
|
static void bootp_extract (struct bootp *, int, struct nfs_diskless *, int *);
|
1997-08-29 20:10:31 +04:00
|
|
|
|
2006-03-16 18:55:20 +03:00
|
|
|
#ifdef DEBUG_NFS_BOOT_DHCP
|
|
|
|
#define DPRINTF(s) printf s
|
1997-08-29 20:10:31 +04:00
|
|
|
#else
|
2006-03-16 18:55:20 +03:00
|
|
|
#define DPRINTF(s)
|
1997-08-29 20:10:31 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get our boot parameters using BOOTP.
|
|
|
|
*/
|
|
|
|
int
|
2008-10-27 13:58:22 +03:00
|
|
|
nfs_bootdhcp(struct nfs_diskless *nd, struct lwp *lwp, int *flags)
|
1997-08-29 20:10:31 +04:00
|
|
|
{
|
1999-02-21 18:07:49 +03:00
|
|
|
struct ifnet *ifp = nd->nd_ifp;
|
1997-08-29 20:10:31 +04:00
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do enough of ifconfig(8) so that the chosen interface
|
|
|
|
* can talk to the servers. Use address zero for now.
|
|
|
|
*/
|
2008-10-27 13:58:22 +03:00
|
|
|
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);
|
1997-08-29 20:10:31 +04:00
|
|
|
if (error) {
|
|
|
|
printf("nfs_boot: set ifaddr zero, error=%d\n", error);
|
1999-02-21 18:07:49 +03:00
|
|
|
return (error);
|
1997-08-29 20:10:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This function call does the real send/recv work. */
|
2008-10-27 13:58:22 +03:00
|
|
|
error = bootpc_call(nd, lwp, flags);
|
1999-02-21 18:07:49 +03:00
|
|
|
|
1997-08-29 20:10:31 +04:00
|
|
|
/* Get rid of the temporary (zero) IP address. */
|
2005-12-11 15:16:03 +03:00
|
|
|
(void) nfs_boot_deladdress(ifp, lwp, INADDR_ANY);
|
1999-02-21 18:07:49 +03:00
|
|
|
|
1997-08-29 20:10:31 +04:00
|
|
|
/* NOW we can test the error from bootpc_call. */
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do ifconfig with our real IP address and mask.
|
|
|
|
*/
|
2005-12-11 15:16:03 +03:00
|
|
|
error = nfs_boot_setaddress(ifp, lwp, nd->nd_myip.s_addr,
|
1999-02-21 18:07:49 +03:00
|
|
|
nd->nd_mask.s_addr, INADDR_ANY);
|
1997-08-29 20:10:31 +04:00
|
|
|
if (error) {
|
|
|
|
printf("nfs_boot: set ifaddr real, error=%d\n", error);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2009-05-03 01:06:51 +04:00
|
|
|
if ((*flags & NFS_BOOT_ALLINFO) != NFS_BOOT_ALLINFO) {
|
|
|
|
printf("nfs_boot: missing options (need IP, netmask, "
|
|
|
|
"gateway, next-server, root-path)\n");
|
2008-10-27 13:58:22 +03:00
|
|
|
return EADDRNOTAVAIL;
|
2009-05-03 01:06:51 +04:00
|
|
|
}
|
2008-10-27 13:58:22 +03:00
|
|
|
|
1997-08-29 20:10:31 +04:00
|
|
|
out:
|
1999-02-21 18:07:49 +03:00
|
|
|
if (error) {
|
2005-12-11 15:16:03 +03:00
|
|
|
(void) nfs_boot_ifupdown(ifp, lwp, 0);
|
1999-02-21 18:07:49 +03:00
|
|
|
nfs_boot_flushrt(ifp);
|
|
|
|
}
|
1997-08-29 20:10:31 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
1997-10-01 00:51:03 +04:00
|
|
|
struct bootpcontext {
|
|
|
|
int xid;
|
2007-08-30 02:53:35 +04:00
|
|
|
const u_char *haddr;
|
1997-10-01 00:51:03 +04:00
|
|
|
u_char halen;
|
|
|
|
struct bootp *replybuf;
|
|
|
|
int replylen;
|
|
|
|
#ifdef NFS_BOOT_DHCP
|
|
|
|
char expected_dhcpmsgtype, dhcp_ok;
|
|
|
|
struct in_addr dhcp_serverip;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2008-10-24 21:17:12 +04:00
|
|
|
static int bootpset (struct mbuf*, void*, int);
|
|
|
|
static int bootpcheck (struct mbuf*, void*);
|
1997-10-01 00:51:03 +04:00
|
|
|
|
1998-04-24 22:38:30 +04:00
|
|
|
static int
|
2006-11-09 12:53:57 +03:00
|
|
|
bootpset(struct mbuf *m, void *context, int waited)
|
1997-10-01 00:51:03 +04:00
|
|
|
{
|
|
|
|
struct bootp *bootp;
|
|
|
|
|
|
|
|
/* we know it's contigous (in 1 mbuf cluster) */
|
|
|
|
bootp = mtod(m, struct bootp*);
|
|
|
|
|
|
|
|
bootp->bp_secs = htons(waited);
|
|
|
|
|
1998-04-24 22:38:30 +04:00
|
|
|
return (0);
|
1997-10-01 00:51:03 +04:00
|
|
|
}
|
|
|
|
|
1998-04-24 22:38:30 +04:00
|
|
|
static int
|
2008-10-24 21:17:12 +04:00
|
|
|
bootpcheck(struct mbuf *m, void *context)
|
1997-10-01 00:51:03 +04:00
|
|
|
{
|
|
|
|
struct bootp *bootp;
|
|
|
|
struct bootpcontext *bpc = context;
|
|
|
|
u_int tag, len;
|
|
|
|
u_char *p, *limit;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Is this a valid reply?
|
|
|
|
*/
|
|
|
|
if (m->m_pkthdr.len < BOOTP_SIZE_MIN) {
|
2009-05-06 09:34:12 +04:00
|
|
|
DPRINTF(("bootpcheck: short packet %d < %zu\n",
|
2009-05-03 01:06:51 +04:00
|
|
|
m->m_pkthdr.len, BOOTP_SIZE_MIN));
|
1998-04-24 22:38:30 +04:00
|
|
|
return (-1);
|
1997-10-01 00:51:03 +04:00
|
|
|
}
|
|
|
|
if (m->m_pkthdr.len > BOOTP_SIZE_MAX) {
|
2009-05-06 09:34:12 +04:00
|
|
|
DPRINTF(("Bootpcheck: long packet %d > %zu\n",
|
2009-05-03 01:06:51 +04:00
|
|
|
m->m_pkthdr.len, BOOTP_SIZE_MAX));
|
1998-04-24 22:38:30 +04:00
|
|
|
return (-1);
|
1997-10-01 00:51:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* don't make first checks more expensive than necessary
|
|
|
|
*/
|
2002-05-12 16:52:58 +04:00
|
|
|
if (m->m_len < offsetof(struct bootp, bp_sname)) {
|
|
|
|
m = m_pullup(m, offsetof(struct bootp, bp_sname));
|
2006-03-16 18:55:20 +03:00
|
|
|
if (m == NULL) {
|
|
|
|
DPRINTF(("bootpcheck: m_pullup failed\n"));
|
1998-04-24 22:38:30 +04:00
|
|
|
return (-1);
|
2006-03-16 18:55:20 +03:00
|
|
|
}
|
1997-10-01 00:51:03 +04:00
|
|
|
}
|
|
|
|
bootp = mtod(m, struct bootp*);
|
|
|
|
|
|
|
|
if (bootp->bp_op != BOOTREPLY) {
|
2006-03-16 18:55:20 +03:00
|
|
|
DPRINTF(("bootpcheck: op %d is not reply\n", bootp->bp_op));
|
1998-04-24 22:38:30 +04:00
|
|
|
return (-1);
|
1997-10-01 00:51:03 +04:00
|
|
|
}
|
|
|
|
if (bootp->bp_hlen != bpc->halen) {
|
2006-03-16 18:55:20 +03:00
|
|
|
DPRINTF(("bootpcheck: hlen %d != %d\n", bootp->bp_hlen,
|
|
|
|
bpc->halen));
|
1998-04-24 22:38:30 +04:00
|
|
|
return (-1);
|
1997-10-01 00:51:03 +04:00
|
|
|
}
|
1998-08-10 00:51:08 +04:00
|
|
|
if (memcmp(bootp->bp_chaddr, bpc->haddr, bpc->halen)) {
|
2006-03-16 18:55:20 +03:00
|
|
|
#ifdef DEBUG_NFS_BOOT_DHCP
|
2008-05-09 10:20:39 +04:00
|
|
|
char *bp_chaddr, *haddr;
|
|
|
|
|
|
|
|
bp_chaddr = malloc(3 * bpc->halen, M_TEMP, M_WAITOK);
|
|
|
|
haddr = malloc(3 * bpc->halen, M_TEMP, M_WAITOK);
|
|
|
|
|
2006-03-16 18:55:20 +03:00
|
|
|
DPRINTF(("bootpcheck: incorrect hwaddr %s != %s\n",
|
2008-05-09 10:20:39 +04:00
|
|
|
ether_snprintf(bp_chaddr, 3 * bpc->halen,
|
2006-03-16 18:55:20 +03:00
|
|
|
bootp->bp_chaddr),
|
2008-05-09 10:20:39 +04:00
|
|
|
ether_snprintf(haddr, 3 * bpc->halen, bpc->haddr)));
|
|
|
|
|
|
|
|
free(bp_chaddr, M_TEMP);
|
|
|
|
free(haddr, M_TEMP);
|
|
|
|
#endif
|
1998-04-24 22:38:30 +04:00
|
|
|
return (-1);
|
1997-10-01 00:51:03 +04:00
|
|
|
}
|
|
|
|
if (bootp->bp_xid != bpc->xid) {
|
2006-03-16 18:55:20 +03:00
|
|
|
DPRINTF(("bootpcheck: xid %d != %d\n", bootp->bp_xid,
|
|
|
|
bpc->xid));
|
1998-04-24 22:38:30 +04:00
|
|
|
return (-1);
|
1997-10-01 00:51:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2007-03-04 08:59:00 +03:00
|
|
|
m_copydata(m, 0, bpc->replylen, (void *)bpc->replybuf);
|
1997-10-01 00:51:03 +04:00
|
|
|
bootp = bpc->replybuf;
|
|
|
|
|
1998-04-24 22:38:30 +04:00
|
|
|
/*
|
|
|
|
* 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) {
|
1999-05-07 19:10:03 +04:00
|
|
|
printf("nfs_boot: wrong IP addr %s",
|
|
|
|
inet_ntoa(bootp->bp_yiaddr));
|
1998-04-24 22:38:30 +04:00
|
|
|
goto warn;
|
|
|
|
}
|
|
|
|
|
1997-10-01 00:51:03 +04:00
|
|
|
/*
|
|
|
|
* Check the vendor data.
|
|
|
|
*/
|
1998-08-10 00:51:08 +04:00
|
|
|
if (memcmp(bootp->bp_vend, vm_rfc1048, 4)) {
|
1998-04-24 22:38:30 +04:00
|
|
|
printf("nfs_boot: reply missing options");
|
|
|
|
goto warn;
|
1997-10-01 00:51:03 +04:00
|
|
|
}
|
|
|
|
p = &bootp->bp_vend[4];
|
2008-07-20 06:06:37 +04:00
|
|
|
limit = ((u_char*)bootp) + bpc->replylen;
|
1997-10-01 00:51:03 +04:00
|
|
|
while (p < limit) {
|
|
|
|
tag = *p++;
|
|
|
|
if (tag == TAG_END)
|
|
|
|
break;
|
|
|
|
if (tag == TAG_PAD)
|
|
|
|
continue;
|
|
|
|
len = *p++;
|
|
|
|
if ((p + len) > limit) {
|
1998-04-24 22:38:30 +04:00
|
|
|
printf("nfs_boot: option %d too long", tag);
|
|
|
|
goto warn;
|
1997-10-01 00:51:03 +04:00
|
|
|
}
|
|
|
|
switch (tag) {
|
|
|
|
#ifdef NFS_BOOT_DHCP
|
2002-03-21 02:07:37 +03:00
|
|
|
case TAG_DHCP_MSGTYPE:
|
1997-10-01 00:51:03 +04:00
|
|
|
if (*p != bpc->expected_dhcpmsgtype)
|
1998-04-24 22:38:30 +04:00
|
|
|
return (-1);
|
1997-10-01 00:51:03 +04:00
|
|
|
bpc->dhcp_ok = 1;
|
|
|
|
break;
|
2002-03-21 02:07:37 +03:00
|
|
|
case TAG_SERVERID:
|
1998-08-10 00:51:08 +04:00
|
|
|
memcpy(&bpc->dhcp_serverip.s_addr, p,
|
1997-10-01 00:51:03 +04:00
|
|
|
sizeof(bpc->dhcp_serverip.s_addr));
|
|
|
|
break;
|
|
|
|
#endif
|
2002-03-21 02:07:37 +03:00
|
|
|
default:
|
1997-10-01 00:51:03 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
p += len;
|
|
|
|
}
|
1998-04-24 22:38:30 +04:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
warn:
|
1999-05-07 19:10:03 +04:00
|
|
|
printf(" (bad reply from %s)\n", inet_ntoa(bootp->bp_siaddr));
|
1998-04-24 22:38:30 +04:00
|
|
|
return (-1);
|
1997-10-01 00:51:03 +04:00
|
|
|
}
|
|
|
|
|
2009-07-10 16:16:31 +04:00
|
|
|
static void
|
|
|
|
bootp_addvend(u_char *area)
|
|
|
|
{
|
|
|
|
#ifdef NFS_BOOT_DHCP
|
|
|
|
char vci[64];
|
|
|
|
int vcilen;
|
|
|
|
|
|
|
|
*area++ = TAG_PARAM_REQ;
|
2010-10-05 03:48:22 +04:00
|
|
|
*area++ = 7;
|
2009-07-10 16:16:31 +04:00
|
|
|
*area++ = TAG_SUBNET_MASK;
|
|
|
|
*area++ = TAG_GATEWAY;
|
|
|
|
*area++ = TAG_HOST_NAME;
|
|
|
|
*area++ = TAG_DOMAIN_NAME;
|
|
|
|
*area++ = TAG_ROOT_PATH;
|
|
|
|
*area++ = TAG_SWAP_SERVER;
|
2010-10-05 03:48:22 +04:00
|
|
|
*area++ = TAG_INTERFACE_MTU;
|
2009-07-10 16:16:31 +04:00
|
|
|
|
|
|
|
/* Insert a NetBSD Vendor Class Identifier option. */
|
|
|
|
snprintf(vci, sizeof(vci), "%s:%s:kernel:%s", ostype, MACHINE,
|
|
|
|
osrelease);
|
|
|
|
vcilen = strlen(vci);
|
|
|
|
*area++ = TAG_CLASSID;
|
|
|
|
*area++ = vcilen;
|
|
|
|
(void)memcpy(area, vci, vcilen);
|
|
|
|
area += vcilen;
|
|
|
|
#endif
|
|
|
|
*area = TAG_END;
|
|
|
|
}
|
|
|
|
|
1997-08-29 20:10:31 +04:00
|
|
|
static int
|
2008-10-27 13:58:22 +03:00
|
|
|
bootpc_call(struct nfs_diskless *nd, struct lwp *lwp, int *flags)
|
1997-08-29 20:10:31 +04:00
|
|
|
{
|
1999-02-21 18:07:49 +03:00
|
|
|
struct socket *so;
|
|
|
|
struct ifnet *ifp = nd->nd_ifp;
|
1997-08-29 20:10:31 +04:00
|
|
|
static u_int32_t xid = ~0xFF;
|
1997-10-01 00:51:03 +04:00
|
|
|
struct bootp *bootp; /* request */
|
1997-08-29 20:10:31 +04:00
|
|
|
struct mbuf *m, *nam;
|
|
|
|
struct sockaddr_in *sin;
|
1997-10-01 00:51:03 +04:00
|
|
|
int error;
|
2007-08-30 02:53:35 +04:00
|
|
|
const u_char *haddr;
|
1997-08-29 20:10:31 +04:00
|
|
|
u_char hafmt, halen;
|
1997-10-01 00:51:03 +04:00
|
|
|
struct bootpcontext bpc;
|
2009-07-10 05:29:54 +04:00
|
|
|
unsigned int index;
|
1997-08-29 20:10:31 +04:00
|
|
|
|
2008-04-24 15:38:36 +04:00
|
|
|
error = socreate(AF_INET, &so, SOCK_DGRAM, 0, lwp, NULL);
|
1999-02-21 18:07:49 +03:00
|
|
|
if (error) {
|
|
|
|
printf("bootp: socreate, error=%d\n", error);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
1997-08-29 20:10:31 +04:00
|
|
|
/*
|
|
|
|
* Initialize to NULL anything that will hold an allocation,
|
|
|
|
* and free each at the end if not null.
|
|
|
|
*/
|
1997-10-01 00:51:03 +04:00
|
|
|
bpc.replybuf = NULL;
|
1997-08-29 20:10:31 +04:00
|
|
|
m = nam = NULL;
|
|
|
|
|
|
|
|
/* Record our H/W (Ethernet) address. */
|
2007-12-20 19:19:38 +03:00
|
|
|
{ const struct sockaddr_dl *sdl = ifp->if_sadl;
|
1998-01-09 18:13:44 +03:00
|
|
|
switch (sdl->sdl_type) {
|
2000-05-28 11:01:09 +04:00
|
|
|
case IFT_ISO88025:
|
|
|
|
hafmt = HTYPE_IEEE802;
|
|
|
|
break;
|
1998-01-09 18:13:44 +03:00
|
|
|
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;
|
|
|
|
}
|
1997-08-29 20:10:31 +04:00
|
|
|
halen = sdl->sdl_alen;
|
2007-08-30 02:53:35 +04:00
|
|
|
haddr = (const unsigned char *)CLLADDR(sdl);
|
1997-08-29 20:10:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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...
|
|
|
|
*/
|
2008-08-06 19:01:23 +04:00
|
|
|
{ int32_t opt;
|
|
|
|
|
|
|
|
opt = 1;
|
|
|
|
error = so_setsockopt(NULL, so, SOL_SOCKET, SO_DONTROUTE, &opt,
|
|
|
|
sizeof(opt));
|
1997-08-29 20:10:31 +04:00
|
|
|
}
|
|
|
|
if (error) {
|
2006-03-16 18:55:20 +03:00
|
|
|
DPRINTF(("bootpc_call: SO_DONTROUTE failed %d\n", error));
|
1997-08-29 20:10:31 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable broadcast. */
|
1997-10-01 00:51:03 +04:00
|
|
|
if ((error = nfs_boot_enbroadcast(so))) {
|
2006-03-16 18:55:20 +03:00
|
|
|
DPRINTF(("bootpc_call: SO_BROADCAST failed %d\n", error));
|
1997-08-29 20:10:31 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2002-06-10 21:45:54 +04:00
|
|
|
/*
|
|
|
|
* 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).
|
|
|
|
*/
|
2008-08-06 19:01:23 +04:00
|
|
|
{ u_char opt;
|
|
|
|
|
|
|
|
opt = 7;
|
|
|
|
error = so_setsockopt(NULL, so, IPPROTO_IP, IP_MULTICAST_TTL,
|
|
|
|
&opt, sizeof(opt));
|
2002-06-10 21:45:54 +04:00
|
|
|
}
|
|
|
|
if (error) {
|
2006-03-16 18:55:20 +03:00
|
|
|
DPRINTF(("bootpc_call: IP_MULTICAST_TTL failed %d\n", error));
|
2002-06-10 21:45:54 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
1997-08-29 20:10:31 +04:00
|
|
|
/* Set the receive timeout for the socket. */
|
1997-10-01 00:51:03 +04:00
|
|
|
if ((error = nfs_boot_setrecvtimo(so))) {
|
2006-03-16 18:55:20 +03:00
|
|
|
DPRINTF(("bootpc_call: SO_RCVTIMEO failed %d\n", error));
|
1997-08-29 20:10:31 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bind the local endpoint to a bootp client port.
|
|
|
|
*/
|
2005-12-11 15:16:03 +03:00
|
|
|
if ((error = nfs_boot_sobind_ipport(so, IPPORT_BOOTPC, lwp))) {
|
2006-03-16 18:55:20 +03:00
|
|
|
DPRINTF(("bootpc_call: bind failed %d\n", error));
|
1997-08-29 20:10:31 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Setup socket address for the server.
|
|
|
|
*/
|
|
|
|
nam = m_get(M_WAIT, MT_SONAME);
|
|
|
|
sin = mtod(nam, struct sockaddr_in *);
|
|
|
|
sin->sin_len = nam->m_len = sizeof(*sin);
|
|
|
|
sin->sin_family = AF_INET;
|
|
|
|
sin->sin_addr.s_addr = INADDR_BROADCAST;
|
|
|
|
sin->sin_port = htons(IPPORT_BOOTPS);
|
|
|
|
|
|
|
|
/*
|
1997-10-01 00:51:03 +04:00
|
|
|
* Allocate buffer used for request
|
1997-08-29 20:10:31 +04:00
|
|
|
*/
|
1997-10-01 00:51:03 +04:00
|
|
|
m = m_gethdr(M_WAIT, MT_DATA);
|
2003-02-26 09:31:08 +03:00
|
|
|
m_clget(m, M_WAIT);
|
1997-10-01 00:51:03 +04:00
|
|
|
bootp = mtod(m, struct bootp*);
|
|
|
|
m->m_pkthdr.len = m->m_len = BOOTP_SIZE_MAX;
|
|
|
|
m->m_pkthdr.rcvif = NULL;
|
1997-08-29 20:10:31 +04:00
|
|
|
|
|
|
|
/*
|
1997-10-01 00:51:03 +04:00
|
|
|
* Build the BOOTP reqest message.
|
1997-08-29 20:10:31 +04:00
|
|
|
* Note: xid is host order! (opaque to server)
|
|
|
|
*/
|
2007-03-04 08:59:00 +03:00
|
|
|
memset((void *)bootp, 0, BOOTP_SIZE_MAX);
|
1997-08-29 20:10:31 +04:00
|
|
|
bootp->bp_op = BOOTREQUEST;
|
|
|
|
bootp->bp_htype = hafmt;
|
|
|
|
bootp->bp_hlen = halen; /* Hardware address length */
|
|
|
|
bootp->bp_xid = ++xid;
|
1998-08-10 00:51:08 +04:00
|
|
|
memcpy(bootp->bp_chaddr, haddr, halen);
|
2000-12-05 20:59:43 +03:00
|
|
|
#ifdef NFS_BOOT_BOOTP_REQFILE
|
|
|
|
strncpy(bootp->bp_file, NFS_BOOT_BOOTP_REQFILE, sizeof(bootp->bp_file));
|
|
|
|
#endif
|
1997-08-29 20:10:31 +04:00
|
|
|
/* Fill-in the vendor data. */
|
1998-08-10 00:51:08 +04:00
|
|
|
memcpy(bootp->bp_vend, vm_rfc1048, 4);
|
2009-07-10 05:29:54 +04:00
|
|
|
index = 4;
|
2009-07-10 16:16:31 +04:00
|
|
|
#ifdef NFS_BOOT_DHCP
|
2009-07-10 05:29:54 +04:00
|
|
|
bootp->bp_vend[index++] = TAG_DHCP_MSGTYPE;
|
|
|
|
bootp->bp_vend[index++] = 1;
|
|
|
|
bootp->bp_vend[index++] = DHCPDISCOVER;
|
1997-10-01 00:51:03 +04:00
|
|
|
#endif
|
2009-07-10 16:16:31 +04:00
|
|
|
bootp_addvend(&bootp->bp_vend[index]);
|
1997-08-29 20:10:31 +04:00
|
|
|
|
1997-10-01 00:51:03 +04:00
|
|
|
bpc.xid = xid;
|
|
|
|
bpc.haddr = haddr;
|
|
|
|
bpc.halen = halen;
|
|
|
|
bpc.replybuf = malloc(BOOTP_SIZE_MAX, M_DEVBUF, M_WAITOK);
|
|
|
|
if (bpc.replybuf == NULL)
|
|
|
|
panic("nfs_boot: malloc reply buf");
|
|
|
|
#ifdef NFS_BOOT_DHCP
|
|
|
|
bpc.expected_dhcpmsgtype = DHCPOFFER;
|
|
|
|
bpc.dhcp_ok = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
error = nfs_boot_sendrecv(so, nam, bootpset, m,
|
2005-12-11 15:16:03 +03:00
|
|
|
bootpcheck, 0, 0, &bpc, lwp);
|
1997-10-01 00:51:03 +04:00
|
|
|
if (error)
|
1997-08-29 20:10:31 +04:00
|
|
|
goto out;
|
1997-10-01 00:51:03 +04:00
|
|
|
|
|
|
|
#ifdef NFS_BOOT_DHCP
|
|
|
|
if (bpc.dhcp_ok) {
|
|
|
|
u_int32_t leasetime;
|
2009-07-10 05:29:54 +04:00
|
|
|
index = 6;
|
|
|
|
bootp->bp_vend[index++] = DHCPREQUEST;
|
|
|
|
bootp->bp_vend[index++] = TAG_REQ_ADDR;
|
|
|
|
bootp->bp_vend[index++] = 4;
|
|
|
|
memcpy(&bootp->bp_vend[index], &bpc.replybuf->bp_yiaddr, 4);
|
|
|
|
index += 4;
|
|
|
|
bootp->bp_vend[index++] = TAG_SERVERID;
|
|
|
|
bootp->bp_vend[index++] = 4;
|
|
|
|
memcpy(&bootp->bp_vend[index], &bpc.dhcp_serverip.s_addr, 4);
|
|
|
|
index += 4;
|
|
|
|
bootp->bp_vend[index++] = TAG_LEASETIME;
|
|
|
|
bootp->bp_vend[index++] = 4;
|
1997-10-01 00:51:03 +04:00
|
|
|
leasetime = htonl(300);
|
2009-07-10 05:29:54 +04:00
|
|
|
memcpy(&bootp->bp_vend[index], &leasetime, 4);
|
|
|
|
index += 4;
|
2009-07-10 16:16:31 +04:00
|
|
|
bootp_addvend(&bootp->bp_vend[index]);
|
1997-10-01 00:51:03 +04:00
|
|
|
|
|
|
|
bpc.expected_dhcpmsgtype = DHCPACK;
|
|
|
|
|
|
|
|
error = nfs_boot_sendrecv(so, nam, bootpset, m,
|
2005-12-11 15:16:03 +03:00
|
|
|
bootpcheck, 0, 0, &bpc, lwp);
|
1997-10-01 00:51:03 +04:00
|
|
|
if (error)
|
|
|
|
goto out;
|
1997-08-29 20:10:31 +04:00
|
|
|
}
|
1997-10-01 00:51:03 +04:00
|
|
|
#endif
|
1997-08-29 20:10:31 +04:00
|
|
|
|
|
|
|
/*
|
1997-10-01 00:51:03 +04:00
|
|
|
* bootpcheck() has copied the receive mbuf into
|
|
|
|
* the buffer at bpc.replybuf.
|
1997-08-29 20:10:31 +04:00
|
|
|
*/
|
1998-04-24 22:38:30 +04:00
|
|
|
#ifdef NFS_BOOT_DHCP
|
2001-06-02 05:52:10 +04:00
|
|
|
printf("nfs_boot: %s next-server: %s\n",
|
1998-04-24 22:38:30 +04:00
|
|
|
(bpc.dhcp_ok ? "DHCP" : "BOOTP"),
|
|
|
|
#else
|
2001-06-02 05:52:10 +04:00
|
|
|
printf("nfs_boot: BOOTP next-server: %s\n",
|
1998-04-24 22:38:30 +04:00
|
|
|
#endif
|
1999-05-07 19:10:03 +04:00
|
|
|
inet_ntoa(bpc.replybuf->bp_siaddr));
|
1998-04-24 22:38:30 +04:00
|
|
|
|
2008-10-27 13:58:22 +03:00
|
|
|
bootp_extract(bpc.replybuf, bpc.replylen, nd, flags);
|
1997-08-29 20:10:31 +04:00
|
|
|
|
1997-10-01 00:51:03 +04:00
|
|
|
out:
|
|
|
|
if (bpc.replybuf)
|
|
|
|
free(bpc.replybuf, M_DEVBUF);
|
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
if (nam)
|
|
|
|
m_freem(nam);
|
1999-02-21 18:07:49 +03:00
|
|
|
soclose(so);
|
1997-10-01 00:51:03 +04:00
|
|
|
return (error);
|
|
|
|
}
|
1997-08-29 20:10:31 +04:00
|
|
|
|
1998-04-24 22:38:30 +04:00
|
|
|
static void
|
2008-10-27 13:58:22 +03:00
|
|
|
bootp_extract(struct bootp *bootp, int replylen,
|
|
|
|
struct nfs_diskless *nd, int *flags)
|
1997-10-01 00:51:03 +04:00
|
|
|
{
|
|
|
|
struct sockaddr_in *sin;
|
|
|
|
struct in_addr netmask;
|
|
|
|
struct in_addr gateway;
|
|
|
|
struct in_addr rootserver;
|
|
|
|
char *myname; /* my hostname */
|
|
|
|
char *mydomain; /* my domainname */
|
|
|
|
char *rootpath;
|
2010-10-05 03:48:22 +04:00
|
|
|
uint16_t myinterfacemtu;
|
1997-10-01 00:51:03 +04:00
|
|
|
int mynamelen;
|
|
|
|
int mydomainlen;
|
|
|
|
int rootpathlen;
|
2000-01-20 11:34:52 +03:00
|
|
|
int overloaded;
|
1997-10-01 00:51:03 +04:00
|
|
|
u_int tag, len;
|
|
|
|
u_char *p, *limit;
|
|
|
|
|
|
|
|
/* Default these to "unspecified". */
|
|
|
|
netmask.s_addr = 0;
|
|
|
|
gateway.s_addr = 0;
|
|
|
|
mydomain = myname = rootpath = NULL;
|
|
|
|
mydomainlen = mynamelen = rootpathlen = 0;
|
2007-05-08 10:10:27 +04:00
|
|
|
|
1997-10-01 00:51:03 +04:00
|
|
|
/* default root server to bootp next-server */
|
|
|
|
rootserver = bootp->bp_siaddr;
|
2000-01-20 11:34:52 +03:00
|
|
|
/* assume that server name field is not overloaded by default */
|
|
|
|
overloaded = 0;
|
2010-10-05 03:48:22 +04:00
|
|
|
/* MTU can't be less than IP_MIN_MTU, set to 0 to indicate unset */
|
|
|
|
myinterfacemtu = 0;
|
1997-10-01 00:51:03 +04:00
|
|
|
|
|
|
|
p = &bootp->bp_vend[4];
|
2008-07-20 06:06:37 +04:00
|
|
|
limit = ((u_char*)bootp) + replylen;
|
1997-10-01 00:51:03 +04:00
|
|
|
while (p < limit) {
|
|
|
|
tag = *p++;
|
|
|
|
if (tag == TAG_END)
|
|
|
|
break;
|
|
|
|
if (tag == TAG_PAD)
|
1997-08-29 20:10:31 +04:00
|
|
|
continue;
|
1997-10-01 00:51:03 +04:00
|
|
|
len = *p++;
|
1998-04-24 22:38:30 +04:00
|
|
|
#if 0 /* already done in bootpcheck() */
|
1997-10-01 00:51:03 +04:00
|
|
|
if ((p + len) > limit) {
|
|
|
|
printf("nfs_boot: option %d too long\n", tag);
|
|
|
|
break;
|
1997-08-29 20:10:31 +04:00
|
|
|
}
|
1998-04-24 22:38:30 +04:00
|
|
|
#endif
|
1997-10-01 00:51:03 +04:00
|
|
|
switch (tag) {
|
|
|
|
case TAG_SUBNET_MASK:
|
2009-07-10 06:41:39 +04:00
|
|
|
if (len < 4) {
|
|
|
|
printf("nfs_boot: subnet mask < 4 bytes\n");
|
|
|
|
break;
|
|
|
|
}
|
1998-08-10 00:51:08 +04:00
|
|
|
memcpy(&netmask, p, 4);
|
1997-10-01 00:51:03 +04:00
|
|
|
break;
|
|
|
|
case TAG_GATEWAY:
|
|
|
|
/* Routers */
|
2009-07-10 06:41:39 +04:00
|
|
|
if (len < 4) {
|
|
|
|
printf("nfs_boot: routers < 4 bytes\n");
|
|
|
|
break;
|
|
|
|
}
|
1998-08-10 00:51:08 +04:00
|
|
|
memcpy(&gateway, p, 4);
|
1997-10-01 00:51:03 +04:00
|
|
|
break;
|
|
|
|
case TAG_HOST_NAME:
|
|
|
|
if (len >= sizeof(hostname)) {
|
2009-07-10 06:41:39 +04:00
|
|
|
printf("nfs_boot: host name >= %lu bytes\n",
|
1999-02-12 04:38:38 +03:00
|
|
|
(u_long)sizeof(hostname));
|
1997-08-29 20:10:31 +04:00
|
|
|
break;
|
|
|
|
}
|
1997-10-01 00:51:03 +04:00
|
|
|
myname = p;
|
|
|
|
mynamelen = len;
|
|
|
|
break;
|
|
|
|
case TAG_DOMAIN_NAME:
|
|
|
|
if (len >= sizeof(domainname)) {
|
2009-07-10 06:41:39 +04:00
|
|
|
printf("nfs_boot: domain name >= %lu bytes\n",
|
1999-02-12 04:38:38 +03:00
|
|
|
(u_long)sizeof(domainname));
|
1997-08-29 20:10:31 +04:00
|
|
|
break;
|
1997-10-01 00:51:03 +04:00
|
|
|
}
|
|
|
|
mydomain = p;
|
|
|
|
mydomainlen = len;
|
|
|
|
break;
|
|
|
|
case TAG_ROOT_PATH:
|
|
|
|
/* Leave some room for the server name. */
|
|
|
|
if (len >= (MNAMELEN-10)) {
|
2009-07-10 06:41:39 +04:00
|
|
|
printf("nfs_boot: rootpath >= %d bytes\n",
|
1997-10-01 00:51:03 +04:00
|
|
|
(MNAMELEN-10));
|
1997-08-29 20:10:31 +04:00
|
|
|
break;
|
|
|
|
}
|
1997-10-01 00:51:03 +04:00
|
|
|
rootpath = p;
|
|
|
|
rootpathlen = len;
|
|
|
|
break;
|
2010-10-05 03:48:22 +04:00
|
|
|
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;
|
1997-10-01 00:51:03 +04:00
|
|
|
case TAG_SWAP_SERVER:
|
|
|
|
/* override NFS server address */
|
2009-07-10 06:41:39 +04:00
|
|
|
if (len < 4) {
|
|
|
|
printf("nfs_boot: swap server < 4 bytes\n");
|
|
|
|
break;
|
|
|
|
}
|
1998-08-10 00:51:08 +04:00
|
|
|
memcpy(&rootserver, p, 4);
|
1997-10-01 00:51:03 +04:00
|
|
|
break;
|
2000-01-20 11:34:52 +03:00
|
|
|
#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
|
1997-10-01 00:51:03 +04:00
|
|
|
default:
|
|
|
|
break;
|
1997-08-29 20:10:31 +04:00
|
|
|
}
|
1997-10-01 00:51:03 +04:00
|
|
|
p += len;
|
1997-08-29 20:10:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Store and print network config info.
|
|
|
|
*/
|
|
|
|
if (myname) {
|
|
|
|
myname[mynamelen] = '\0';
|
|
|
|
strncpy(hostname, myname, sizeof(hostname));
|
|
|
|
hostnamelen = mynamelen;
|
|
|
|
printf("nfs_boot: my_name=%s\n", hostname);
|
|
|
|
}
|
|
|
|
if (mydomain) {
|
|
|
|
mydomain[mydomainlen] = '\0';
|
|
|
|
strncpy(domainname, mydomain, sizeof(domainname));
|
|
|
|
domainnamelen = mydomainlen;
|
|
|
|
printf("nfs_boot: my_domain=%s\n", domainname);
|
|
|
|
}
|
2008-10-27 13:58:22 +03:00
|
|
|
if (!(*flags & NFS_BOOT_HAS_MYIP)) {
|
|
|
|
nd->nd_myip = bootp->bp_yiaddr;
|
1999-05-07 19:10:03 +04:00
|
|
|
printf("nfs_boot: my_addr=%s\n", inet_ntoa(nd->nd_myip));
|
2008-10-27 13:58:22 +03:00
|
|
|
*flags |= NFS_BOOT_HAS_MYIP;
|
|
|
|
}
|
|
|
|
if (!(*flags & NFS_BOOT_HAS_MASK)) {
|
|
|
|
nd->nd_mask = netmask;
|
1999-05-07 19:10:03 +04:00
|
|
|
printf("nfs_boot: my_mask=%s\n", inet_ntoa(nd->nd_mask));
|
2008-10-27 13:58:22 +03:00
|
|
|
*flags |= NFS_BOOT_HAS_MASK;
|
|
|
|
}
|
|
|
|
if (!(*flags & NFS_BOOT_HAS_GWIP)) {
|
|
|
|
nd->nd_gwip = gateway;
|
1999-05-07 19:10:03 +04:00
|
|
|
printf("nfs_boot: gateway=%s\n", inet_ntoa(nd->nd_gwip));
|
2008-10-27 13:58:22 +03:00
|
|
|
*flags |= NFS_BOOT_HAS_GWIP;
|
|
|
|
}
|
2010-10-05 03:48:22 +04:00
|
|
|
if (myinterfacemtu >= IP_MIN_MTU) {
|
|
|
|
nd->nd_mtu = myinterfacemtu;
|
|
|
|
printf("nfs_boot: mtu=%d\n", nd->nd_mtu);
|
|
|
|
}
|
1997-08-29 20:10:31 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Store the information about our NFS root mount.
|
|
|
|
* The caller will print it, so be silent here.
|
|
|
|
*/
|
2008-10-27 13:58:22 +03:00
|
|
|
do {
|
1997-08-29 20:10:31 +04:00
|
|
|
struct nfs_dlmount *ndm = &nd->nd_root;
|
|
|
|
|
2008-10-27 13:58:22 +03:00
|
|
|
|
|
|
|
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;
|
1997-08-29 20:10:31 +04:00
|
|
|
}
|
2008-10-27 13:58:22 +03:00
|
|
|
|
|
|
|
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);
|
2007-05-08 10:10:27 +04:00
|
|
|
|
|
|
|
#ifdef TFTPROOT
|
|
|
|
#if BP_FILE_LEN > MNAMELEN
|
|
|
|
#define BOOTFILELEN MNAMELEN
|
|
|
|
#else
|
|
|
|
#define BOOTFILELEN BP_FILE_LEN
|
|
|
|
#endif
|
|
|
|
strncpy(nd->nd_bootfile, bootp->bp_file, BOOTFILELEN);
|
|
|
|
nd->nd_bootfile[BOOTFILELEN - 1] = '\0';
|
|
|
|
#undef BOOTFILELEN
|
|
|
|
#endif /* TFTPROOT */
|
1997-08-29 20:10:31 +04:00
|
|
|
}
|