BOOTP/DHCP
Andrew Bythell
abythell at nortelnetworks.com
Mon Nov 27 14:48:09 UTC 2000
Hello -
I slapped together a quick DHCP client (attached) derived from the bootp
code that may be useful. It still needs work, as it doesn't yet handle
leases and always accepts the first DHCP offer it sees, but it is "pure
DCHP".
To use it, call rtems_bsdnet_do_dhcp() instead of rtems_bsdnet_do_bootp
in your network config.
It needs KERNEL and COMPILING_BSD_KERNEL defined, which are defined in
the source to make it build as an application instead of part of the
RTEMS tree *for testing only*. I understand one doesn't want these
things defined for normal applications. To build it properly, one
should probably put it in the libnetworking/nfs directory, remove the
#ifndef..#endif blocks, and modify Makefile.am.
Feedback is welcome.
Andrew.
Nick.SIMON at syntegra.bt.co.uk wrote:
>
> Is it possible that confusion has arisen from the fact that most (but not
> all!) DHCP servers will also keep BOOTP clients happy? If the RTEMS stack
> will talk "pure" DHCP, even with infinite leases, that would solve a problem
> for me.
>
> -- Nick Simon
>
-------------- next part --------------
/*
* DCHP client for RTEMS
* Andrew Bythell, <abythell at nortelnetworks.com>
* based on and uses subroutines from c/src/libnetworking/nfs/bootp_subr.c
*/
/*
* Copyright (c) 1995 Gordon Ross, Adam Glass
* Copyright (c) 1992 Regents of the University of California.
* All rights reserved.
*
* This software was developed by the Computer Systems Engineering group
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
* contributed to Berkeley.
*
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Lawrence Berkeley Laboratory and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
*
*/
/*
* WARNING:
* This file should be moved into c/src/libnetworking/nfs
* and the following two #ifndef...#endif blocks and the #undefs at
* the end of the file should be removed
*/
#ifndef _COMPILING_BSD_KERNEL_
#define _COMPILING_BSD_KERNEL_
#endif
#ifndef KERNEL
#define KERNEL
#endif
#include <sys/param.h> /* for MAXHOSTNAMELEN */
#include <sys/systm.h>
#include <sys/socketvar.h> /* for socreat() soclose() */
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h> /* for NBO-HBO conversions */
#include <net/if_types.h> /* for IFT_ETHER */
#include <net/if_dl.h> /* for LLADDR */
#ifndef EALEN
#define EALEN 6
#endif
/* DHCP flags */
#define DHCP_BROADCAST 0x8000
/* DHCP Op Codes */
#define DHCP_BOOTREQUEST 1
#define DHCP_BOOTREPLY 2
/* DHCP Messages */
#define DHCP_DISCOVER 1
#define DHCP_OFFER 2
#define DHCP_REQUEST 3
#define DHCP_DECLINE 4
#define DHCP_ACK 5
#define DHCP_NACK 6
#define DHCP_RELEASE 7
/* DHCP Options */
#define DHCP_MAGIC_COOKIE "99.130.83.99"
#define DHCP_OPTION_PAD 0
#define DHCP_SUBNET 1
#define DHCP_GATEWAY 3
#define DHCP_DNS 6
#define DHCP_HOST 12
#define DHCP_DOMAIN_NAME 15
#define DHCP_NETMASK 28
#define DHCP_REQUESTED_IP 50
#define DHCP_LEASE 51
#define DHCP_MESSAGE 53
#define DHCP_SERVER 54
#define DHCP_PARAMETERS 55
#define DHCP_OPTION_END 255
/* Definitions from RFC */
struct dhcp_packet
{
u_int8_t op;
u_int8_t htype;
u_int8_t hlen;
u_int8_t hops;
u_int32_t xid;
u_int16_t secs;
u_int16_t flags;
struct in_addr ciaddr;
struct in_addr yiaddr;
struct in_addr siaddr;
struct in_addr giaddr;
unsigned char chaddr[16];
char sname[64];
char file[128];
unsigned char vend[312];
};
/*
* External Declarations for Functions found in
* rtems/c/src/libnetworking/nfs/
*/
extern int bootpc_call (struct dhcp_packet *call,
struct dhcp_packet *reply, struct proc *procp);
extern int bootpc_fakeup_interface (struct ifreq *ireq, struct socket *so,
struct proc *procp);
extern int
bootpc_adjust_interface (struct ifreq *ireq, struct socket *so,
struct sockaddr_in *myaddr,
struct sockaddr_in *netmask,
struct sockaddr_in *gw, struct proc *procp);
extern int nfs_diskless_valid;
/* Variables */
static int dhcpOptionOverload = 0;
static char dhcp_gotgw = 0;
static char dhcp_gotnetmask = 0;
static char dhcp_gotserver = 0;
static char dhcp_gotlogserver = 0;
static struct sockaddr_in dhcp_netmask;
static struct sockaddr_in dhcp_gw;
static int dhcpMessageType = 0;
/*******************************************************************************
*
* Print formatted IP Addresses
* Same as bootpc but re-declared because of scope
*
*******************************************************************************/
static void
printip (char *prefix, struct in_addr addr)
{
unsigned int ip;
ip = ntohl (addr.s_addr);
printf ("%s is %d.%d.%d.%d\n", prefix,
ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
}
/*******************************************************************************
*
* Process DCHP Options
* Same as bootpc, with some extra handling for DHCP options
*
*******************************************************************************/
static void
processOptions (unsigned char *optbuf, int optbufSize)
{
int j = 0;
int len;
int code, ncode;
char *p;
ncode = optbuf[0];
while (j < optbufSize)
{
code = optbuf[j] = ncode;
if (code == 255)
return;
if (code == 0)
{
j++;
continue;
}
len = optbuf[j + 1];
j += 2;
if ((len + j) >= optbufSize)
{
printf ("Truncated field for code %d", code);
return;
}
ncode = optbuf[j + len];
optbuf[j + len] = '\0';
p = &optbuf[j];
j += len;
/*
* Process the option
*/
switch (code)
{
case 1:
/* Subnet mask */
if (len != 4)
panic ("dhcpc: subnet mask len is %d", len);
memcpy (&dhcp_netmask.sin_addr, p, 4);
dhcp_gotnetmask = 1;
break;
case 2:
/* Time offset */
if (len != 4)
panic ("dhcpc: time offset len is %d", len);
memcpy (&rtems_bsdnet_timeoffset, p, 4);
rtems_bsdnet_timeoffset = ntohl (rtems_bsdnet_timeoffset);
break;
case 3:
/* Routers */
if (len % 4)
panic ("dhcpc: Router Len is %d", len);
if (len > 0)
{
memcpy (&dhcp_gw.sin_addr, p, 4);
dhcp_gotgw = 1;
}
break;
case 42:
/* NTP servers */
if (len % 4)
panic ("dhcpc: time server Len is %d", len);
{
int tlen = 0;
while ((tlen < len) &&
(rtems_bsdnet_ntpserver_count <
sizeof rtems_bsdnet_config.ntp_server /
sizeof rtems_bsdnet_config.ntp_server[0]))
{
memcpy (&rtems_bsdnet_ntpserver[rtems_bsdnet_ntpserver_count],
p + tlen, 4);
printip ("Time Server",
rtems_bsdnet_ntpserver
[rtems_bsdnet_ntpserver_count]);
rtems_bsdnet_ntpserver_count++;
tlen += 4;
}
}
break;
case 6:
/* Domain Name servers */
if (len % 4)
panic ("dhcpc: DNS Len is %d", len);
{
int dlen = 0;
while ((dlen < len) &&
(rtems_bsdnet_nameserver_count <
sizeof rtems_bsdnet_config.name_server /
sizeof rtems_bsdnet_config.name_server[0]))
{
memcpy (&rtems_bsdnet_nameserver
[rtems_bsdnet_nameserver_count], p + dlen, 4);
printip ("Domain Name Server",
rtems_bsdnet_nameserver
[rtems_bsdnet_nameserver_count]);
rtems_bsdnet_nameserver_count++;
dlen += 4;
}
}
break;
case 12:
/* Host name */
if (len >= MAXHOSTNAMELEN)
panic ("dhcpc: hostname >=%d bytes", MAXHOSTNAMELEN);
if (sethostname (p, len) < 0)
panic ("Can't set host name");
printf ("Hostname is %s\n", p);
break;
case 7:
/* Log servers */
if (len % 4)
panic ("dhcpc: Log server Len is %d", len);
if (len > 0)
{
memcpy (&rtems_bsdnet_log_host_address, p, 4);
dhcp_gotlogserver = 1;
}
break;
case 15:
/* Domain name */
if (p[0])
{
rtems_bsdnet_domain_name = strdup (p);
printf ("Domain name is %s\n", rtems_bsdnet_domain_name);
}
break;
case 16: /* Swap server IP address. unused */
break;
case 50:
/* DHCP Requested IP Address */
if (len != 4)
panic ("dhcpc: DHCP option requested IP len is %d", len);
/*
* although nothing happens here, this case keeps the client
* from complaining about unknown options. The Requested IP
* is necessary to return to the server for a DHCP REQUEST
*/
break;
case 51:
/* DHCP Lease Length */
if (len != 4)
panic ("DHCP option Lease-Length len is %d", len);
/*
* Currently there is no support for renewing leases
* therefore if the lease is not infinite, do not accept it
*/
if (p[0] == p[1] == p[2] == p[3] == 0)
panic ("This DHCP client only supports infinite leases. Sorry.");
break;
case 52:
/* DHCP option override */
if (len != 1)
panic ("dhcpc: DHCP option overload len is %d", len);
dhcpOptionOverload = p[0];
break;
case 53:
/* DHCP message */
if (len != 1)
panic ("dhcpc: DHCP message len is %d", len);
dhcpMessageType = p[0];
break;
case 128: /* Site-specific option for DHCP servers that
* a) don't supply tag 54
* and
* b) don't supply the server address in siaddr
* For example, on Solaris 2.6 in.dhcpd, include in the dhcptab:
* Bootsrv s Site,128,IP,1,1
* and use that symbol in the macro that defines the client:
* Bootsrv=<tftp-server-ip-address>
*/
case 54:
/* DHCP server */
if (len != 4)
panic ("dhcpc: DHCP server len is %d", len);
memcpy (&rtems_bsdnet_bootp_server_address, p, 4);
dhcp_gotserver = 1;
break;
case 66:
/* DHCP server name option */
if (p[0])
rtems_bsdnet_bootp_server_name = strdup (p);
break;
case 67:
/* DHCP bootfile option */
if (p[0])
rtems_bsdnet_bootp_boot_file_name = strdup (p);
break;
default:
printf ("Ignoring BOOTP/DHCP option code %d\n", code);
break;
}
}
}
/*******************************************************************************
*
* DCHP Client Routine
* - This routine does not yet handle renewing or releasing leases.
* - The first DHCP offer is always accepted
* - No DHCP DECLINE message is sent if ARPing fails
*
*******************************************************************************/
void
dhcp_init (void)
{
struct dhcp_packet call;
struct dhcp_packet reply;
static u_int32_t xid = ~0xFF;
struct ifreq ireq;
struct ifnet *ifp;
struct socket *so;
int j;
int error;
struct sockaddr_in myaddr;
struct ifaddr *ifa;
struct sockaddr_dl *sdl = NULL;
char *delim;
struct proc *procp = NULL;
static const char dhcpRequestParameters[5] = { DHCP_SUBNET, DHCP_GATEWAY,
DHCP_DNS, DHCP_HOST, DHCP_DOMAIN_NAME
};
/*
* If already filled in, don't touch it here
*/
if (nfs_diskless_valid)
return;
/*
* Find a network interface.
*/
for (ifp = ifnet; ifp != 0; ifp = ifp->if_next)
if ((ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) == 0)
break;
if (ifp == NULL)
panic ("dhcpc_init: no suitable interface");
memset (&ireq, 0, sizeof (ireq));
sprintf (ireq.ifr_name, "%s%d", ifp->if_name, ifp->if_unit);
if ((error = socreate (AF_INET, &so, SOCK_DGRAM, 0, procp)) != 0)
panic ("nfs_boot: socreate, error=%d", error);
bootpc_fakeup_interface (&ireq, so, procp);
/* Get HW address */
for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
if (ifa->ifa_addr->sa_family == AF_LINK &&
(sdl = ((struct sockaddr_dl *) ifa->ifa_addr)) &&
sdl->sdl_type == IFT_ETHER)
break;
if (!sdl)
panic ("dhcpc: Unable to find HW address");
if (sdl->sdl_alen != EALEN)
panic ("dhcpc: HW address len is %d, expected value is %d",
sdl->sdl_alen, EALEN);
printf ("dhcpc hw address is ");
delim = "";
for (j = 0; j < sdl->sdl_alen; j++)
{
printf ("%s%x", delim, ((unsigned char *) LLADDR (sdl))[j]);
delim = ":";
}
printf ("\n");
memset ((caddr_t) & call, 0, sizeof (call));
/*
* Send a DHCP DISCOVER Message
*/
call.op = DHCP_BOOTREQUEST;
call.htype = 1; /* 10mb ethernet */
call.hlen = sdl->sdl_alen; /* Hardware address length */
call.hops = 0;
xid++;
call.xid = htonl ((long) xid);
memcpy (&call.chaddr, LLADDR (sdl), sdl->sdl_alen);
call.vend[0] = 99; /* MAGIC */
call.vend[1] = 130;
call.vend[2] = 83;
call.vend[3] = 99;
call.vend[4] = DHCP_MESSAGE;
call.vend[5] = 1;
call.vend[6] = DHCP_DISCOVER;
call.vend[7] = DHCP_PARAMETERS;
call.vend[8] = 5;
memcpy (&call.vend[9], &dhcpRequestParameters, 5);
call.vend[14] = DHCP_LEASE;
call.vend[15] = 4;
memset (&call.vend[16], 0xFF, 4); /* request infinite lease time */
call.vend[20] = DHCP_OPTION_END;
call.secs = 0;
call.flags = htons (DHCP_BROADCAST); /* We need a broadcast answer */
error = bootpc_call (&call, &reply, procp);
if (error)
panic ("BOOTP call failed -- error %d", error);
/*
* Check for DHCP OFFER
*/
if (!
(reply.vend[0] == 99 && reply.vend[1] == 130 && reply.vend[2] == 83
&& reply.vend[3] == 99))
panic ("DHCP server did not send Magic Cookie.\n");
processOptions (&reply.vend[4], sizeof (reply.vend) - 4);
if (dhcpMessageType != DHCP_OFFER)
panic ("DHCP server did not send a DHCP Offer.\n");
/*
* Send a DHCP REQUEST
*/
printf ("received dhcp offer. sending request.\n");
call.xid = reply.xid;
call.vend[6] = DHCP_REQUEST;
call.vend[7] = DHCP_SERVER;
call.vend[8] = 4;
memcpy (&call.vend[9], &rtems_bsdnet_bootp_server_address, 4);
call.vend[13] = DHCP_REQUESTED_IP;
call.vend[14] = 4;
memcpy (&call.vend[15], &reply.yiaddr, 4);
call.vend[19] = DHCP_PARAMETERS;
call.vend[20] = 5;
memcpy (&call.vend[21], &dhcpRequestParameters, 5);
call.vend[26] = DHCP_LEASE;
call.vend[27] = 4;
memset (&call.vend[28], 0xFF, 4);
call.vend[32] = DHCP_OPTION_END;
error = bootpc_call (&call, &reply, procp);
if (error)
panic ("BOOTP call failed -- error %d", error);
/*
* Check for DHCP ACK/NACK
*/
if (!
(reply.vend[0] == 99 && reply.vend[1] == 130 && reply.vend[2] == 83
&& reply.vend[3] == 99))
panic ("DHCP server did not send Magic Cookie.\n");
processOptions (&reply.vend[4], sizeof (reply.vend) - 4);
if (dhcpMessageType != DHCP_ACK)
panic ("DHCP server did not accept the DHCP request");
/*
* Initialize network address structures
*/
memset (&myaddr, 0, sizeof (myaddr));
memset (&dhcp_netmask, 0, sizeof (dhcp_netmask));
memset (&dhcp_gw, 0, sizeof (dhcp_gw));
myaddr.sin_len = sizeof (myaddr);
myaddr.sin_family = AF_INET;
dhcp_netmask.sin_len = sizeof (dhcp_netmask);
dhcp_netmask.sin_family = AF_INET;
dhcp_gw.sin_len = sizeof (dhcp_gw);
dhcp_gw.sin_family = AF_INET;
/*
* Set our address
*/
myaddr.sin_addr = reply.yiaddr;
printip ("My ip address", myaddr.sin_addr);
/*
* Process BOOTP/DHCP options
*/
if (reply.vend[0] == 99 && reply.vend[1] == 130 &&
reply.vend[2] == 83 && reply.vend[3] == 99)
{
processOptions (&reply.vend[4], sizeof (reply.vend) - 4);
}
if (dhcpOptionOverload & 1)
{
processOptions (reply.file, sizeof reply.file);
}
else
{
if (reply.file[0])
rtems_bsdnet_bootp_boot_file_name = strdup (reply.file);
}
if (dhcpOptionOverload & 2)
{
processOptions (reply.sname, sizeof reply.sname);
}
else
{
if (reply.sname[0])
rtems_bsdnet_bootp_server_name = strdup (reply.sname);
}
if (rtems_bsdnet_bootp_server_name)
printf ("Server name is %s\n", rtems_bsdnet_bootp_server_name);
if (rtems_bsdnet_bootp_boot_file_name)
printf ("Boot file is %s\n", rtems_bsdnet_bootp_boot_file_name);
/*
* Use defaults if values were not supplied by BOOTP/DHCP options
*/
if (!dhcp_gotnetmask)
{
if (IN_CLASSA (ntohl (myaddr.sin_addr.s_addr)))
dhcp_netmask.sin_addr.s_addr = htonl (IN_CLASSA_NET);
else if (IN_CLASSB (ntohl (myaddr.sin_addr.s_addr)))
dhcp_netmask.sin_addr.s_addr = htonl (IN_CLASSB_NET);
else
dhcp_netmask.sin_addr.s_addr = htonl (IN_CLASSC_NET);
}
printip ("Subnet mask", dhcp_netmask.sin_addr);
if (!dhcp_gotserver)
rtems_bsdnet_bootp_server_address = reply.siaddr;
printip ("Server ip address", rtems_bsdnet_bootp_server_address);
if (!dhcp_gotgw)
dhcp_gw.sin_addr = reply.giaddr;
printip ("Gateway ip address", dhcp_gw.sin_addr);
if (!dhcp_gotlogserver)
rtems_bsdnet_log_host_address = rtems_bsdnet_bootp_server_address;
printip ("Log server ip address", rtems_bsdnet_log_host_address);
/*
* Configure the interface with the new settings
*/
error = bootpc_adjust_interface (&ireq, so,
&myaddr, &dhcp_netmask, &dhcp_gw, procp);
soclose (so);
}
/*******************************************************************************
*
* RTEMS Entry point to DHCP client
*
*******************************************************************************/
void rtems_bsdnet_do_dhcp(void)
{
rtems_bsdnet_semaphore_obtain();
dhcp_init();
rtems_bsdnet_semaphore_release();
}
#undef D_COMPILING_BSD_KERNEL
#undef KERNEL
More information about the users
mailing list