2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-05-08 08:50:50 +05:30
|
|
|
/* clientpacket.c
|
|
|
|
*
|
|
|
|
* Packet generation and dispatching functions for the DHCP client.
|
|
|
|
*
|
|
|
|
* Russ Dill <Russ.Dill@asu.edu> July 2001
|
|
|
|
*
|
2006-05-28 06:36:36 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2006-05-08 08:50:50 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <features.h>
|
2006-12-19 03:19:06 +05:30
|
|
|
#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
|
2006-05-08 08:50:50 +05:30
|
|
|
#include <netpacket/packet.h>
|
|
|
|
#include <net/ethernet.h>
|
|
|
|
#else
|
|
|
|
#include <asm/types.h>
|
|
|
|
#include <linux/if_packet.h>
|
|
|
|
#include <linux/if_ether.h>
|
|
|
|
#endif
|
|
|
|
|
2006-11-19 01:21:32 +05:30
|
|
|
#include "common.h"
|
2006-05-08 08:50:50 +05:30
|
|
|
#include "dhcpd.h"
|
|
|
|
#include "dhcpc.h"
|
2006-11-19 01:21:32 +05:30
|
|
|
#include "options.h"
|
2006-05-08 08:50:50 +05:30
|
|
|
|
|
|
|
|
|
|
|
/* Create a random xid */
|
2007-07-03 21:17:50 +05:30
|
|
|
uint32_t random_xid(void)
|
2006-05-08 08:50:50 +05:30
|
|
|
{
|
2007-06-18 05:10:26 +05:30
|
|
|
static smallint initialized;
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2007-06-18 05:10:26 +05:30
|
|
|
if (!initialized) {
|
|
|
|
srand(monotonic_us());
|
|
|
|
initialized = 1;
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
return rand();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* initialize a packet with the proper defaults */
|
|
|
|
static void init_packet(struct dhcpMessage *packet, char type)
|
|
|
|
{
|
2006-05-28 06:36:36 +05:30
|
|
|
udhcp_init_header(packet, type);
|
2006-05-08 08:50:50 +05:30
|
|
|
memcpy(packet->chaddr, client_config.arp, 6);
|
|
|
|
if (client_config.clientid)
|
2006-11-19 01:21:32 +05:30
|
|
|
add_option_string(packet->options, client_config.clientid);
|
2007-07-03 21:17:50 +05:30
|
|
|
if (client_config.hostname)
|
|
|
|
add_option_string(packet->options, client_config.hostname);
|
|
|
|
if (client_config.fqdn)
|
|
|
|
add_option_string(packet->options, client_config.fqdn);
|
2006-05-08 08:50:50 +05:30
|
|
|
add_option_string(packet->options, client_config.vendorclass);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Add a parameter request list for stubborn DHCP servers. Pull the data
|
|
|
|
* from the struct in options.c. Don't do bounds checking here because it
|
|
|
|
* goes towards the head of the packet. */
|
|
|
|
static void add_requests(struct dhcpMessage *packet)
|
|
|
|
{
|
|
|
|
int end = end_option(packet->options);
|
|
|
|
int i, len = 0;
|
|
|
|
|
|
|
|
packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
|
|
|
|
for (i = 0; dhcp_options[i].code; i++)
|
|
|
|
if (dhcp_options[i].flags & OPTION_REQ)
|
|
|
|
packet->options[end + OPT_DATA + len++] = dhcp_options[i].code;
|
|
|
|
packet->options[end + OPT_LEN] = len;
|
|
|
|
packet->options[end + OPT_DATA + len] = DHCP_END;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-11-22 06:28:49 +05:30
|
|
|
#if ENABLE_FEATURE_UDHCPC_ARPING
|
|
|
|
/* Unicast a DHCP decline message */
|
|
|
|
int send_decline(uint32_t xid, uint32_t server)
|
|
|
|
{
|
|
|
|
struct dhcpMessage packet;
|
|
|
|
|
|
|
|
init_packet(&packet, DHCPDECLINE);
|
|
|
|
packet.xid = xid;
|
|
|
|
add_requests(&packet);
|
|
|
|
|
|
|
|
bb_info_msg("Sending decline...");
|
|
|
|
|
|
|
|
return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
|
|
|
|
SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
|
|
|
|
}
|
|
|
|
#endif
|
2006-05-08 08:50:50 +05:30
|
|
|
|
|
|
|
/* Broadcast a DHCP discover packet to the network, with an optionally requested IP */
|
2007-07-03 21:17:50 +05:30
|
|
|
int send_discover(uint32_t xid, uint32_t requested)
|
2006-05-08 08:50:50 +05:30
|
|
|
{
|
|
|
|
struct dhcpMessage packet;
|
|
|
|
|
|
|
|
init_packet(&packet, DHCPDISCOVER);
|
|
|
|
packet.xid = xid;
|
|
|
|
if (requested)
|
|
|
|
add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
|
|
|
|
|
|
|
|
add_requests(&packet);
|
2006-09-07 00:06:50 +05:30
|
|
|
bb_info_msg("Sending discover...");
|
2006-05-28 06:36:36 +05:30
|
|
|
return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
|
2006-11-19 01:21:32 +05:30
|
|
|
SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Broadcasts a DHCP request message */
|
2007-07-03 21:17:50 +05:30
|
|
|
int send_selecting(uint32_t xid, uint32_t server, uint32_t requested)
|
2006-05-08 08:50:50 +05:30
|
|
|
{
|
|
|
|
struct dhcpMessage packet;
|
|
|
|
struct in_addr addr;
|
|
|
|
|
|
|
|
init_packet(&packet, DHCPREQUEST);
|
|
|
|
packet.xid = xid;
|
|
|
|
|
|
|
|
add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
|
|
|
|
add_simple_option(packet.options, DHCP_SERVER_ID, server);
|
|
|
|
|
|
|
|
add_requests(&packet);
|
|
|
|
addr.s_addr = requested;
|
2006-09-07 00:06:50 +05:30
|
|
|
bb_info_msg("Sending select for %s...", inet_ntoa(addr));
|
2006-05-28 06:36:36 +05:30
|
|
|
return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
|
2006-05-08 08:50:50 +05:30
|
|
|
SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Unicasts or broadcasts a DHCP renew message */
|
2007-07-03 21:17:50 +05:30
|
|
|
int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr)
|
2006-05-08 08:50:50 +05:30
|
|
|
{
|
|
|
|
struct dhcpMessage packet;
|
|
|
|
|
|
|
|
init_packet(&packet, DHCPREQUEST);
|
|
|
|
packet.xid = xid;
|
|
|
|
packet.ciaddr = ciaddr;
|
|
|
|
|
|
|
|
add_requests(&packet);
|
2006-09-07 00:06:50 +05:30
|
|
|
bb_info_msg("Sending renew...");
|
2006-05-08 08:50:50 +05:30
|
|
|
if (server)
|
2007-07-03 21:17:50 +05:30
|
|
|
return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
|
|
|
|
|
|
|
|
return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
|
2006-05-08 08:50:50 +05:30
|
|
|
SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Unicasts a DHCP release message */
|
2007-07-03 21:17:50 +05:30
|
|
|
int send_release(uint32_t server, uint32_t ciaddr)
|
2006-05-08 08:50:50 +05:30
|
|
|
{
|
|
|
|
struct dhcpMessage packet;
|
|
|
|
|
|
|
|
init_packet(&packet, DHCPRELEASE);
|
|
|
|
packet.xid = random_xid();
|
|
|
|
packet.ciaddr = ciaddr;
|
|
|
|
|
|
|
|
add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr);
|
|
|
|
add_simple_option(packet.options, DHCP_SERVER_ID, server);
|
|
|
|
|
2006-09-07 00:06:50 +05:30
|
|
|
bb_info_msg("Sending release...");
|
2006-05-28 06:36:36 +05:30
|
|
|
return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* return -1 on errors that are fatal for the socket, -2 for those that aren't */
|
|
|
|
int get_raw_packet(struct dhcpMessage *payload, int fd)
|
|
|
|
{
|
|
|
|
int bytes;
|
|
|
|
struct udp_dhcp_packet packet;
|
|
|
|
uint32_t source, dest;
|
|
|
|
uint16_t check;
|
|
|
|
|
|
|
|
memset(&packet, 0, sizeof(struct udp_dhcp_packet));
|
|
|
|
bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
|
|
|
|
if (bytes < 0) {
|
2006-09-30 03:00:43 +05:30
|
|
|
DEBUG("Cannot read on raw listening socket - ignoring");
|
2007-11-23 05:38:54 +05:30
|
|
|
sleep(1); /* possible down interface, looping condition */
|
2006-05-08 08:50:50 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
|
2006-09-07 00:06:50 +05:30
|
|
|
DEBUG("Message too short, ignoring");
|
2006-05-08 08:50:50 +05:30
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytes < ntohs(packet.ip.tot_len)) {
|
2006-09-07 00:06:50 +05:30
|
|
|
DEBUG("Truncated packet");
|
2006-05-08 08:50:50 +05:30
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ignore any extra garbage bytes */
|
|
|
|
bytes = ntohs(packet.ip.tot_len);
|
|
|
|
|
|
|
|
/* Make sure its the right packet for us, and that it passes sanity checks */
|
2006-11-19 01:21:32 +05:30
|
|
|
if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION
|
2007-11-23 05:38:54 +05:30
|
|
|
|| packet.ip.ihl != (sizeof(packet.ip) >> 2)
|
2006-11-19 01:21:32 +05:30
|
|
|
|| packet.udp.dest != htons(CLIENT_PORT)
|
|
|
|
|| bytes > (int) sizeof(struct udp_dhcp_packet)
|
|
|
|
|| ntohs(packet.udp.len) != (uint16_t)(bytes - sizeof(packet.ip))
|
|
|
|
) {
|
2006-09-07 00:06:50 +05:30
|
|
|
DEBUG("Unrelated/bogus packet");
|
2006-05-08 08:50:50 +05:30
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check IP checksum */
|
|
|
|
check = packet.ip.check;
|
|
|
|
packet.ip.check = 0;
|
2006-05-28 06:36:36 +05:30
|
|
|
if (check != udhcp_checksum(&(packet.ip), sizeof(packet.ip))) {
|
2006-09-07 00:06:50 +05:30
|
|
|
DEBUG("bad IP header checksum, ignoring");
|
2006-05-08 08:50:50 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-11-23 05:38:54 +05:30
|
|
|
/* verify the UDP checksum by replacing the header with a pseudo header */
|
2006-05-08 08:50:50 +05:30
|
|
|
source = packet.ip.saddr;
|
|
|
|
dest = packet.ip.daddr;
|
|
|
|
check = packet.udp.check;
|
|
|
|
packet.udp.check = 0;
|
|
|
|
memset(&packet.ip, 0, sizeof(packet.ip));
|
|
|
|
|
|
|
|
packet.ip.protocol = IPPROTO_UDP;
|
|
|
|
packet.ip.saddr = source;
|
|
|
|
packet.ip.daddr = dest;
|
|
|
|
packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
|
2006-05-28 06:36:36 +05:30
|
|
|
if (check && check != udhcp_checksum(&packet, bytes)) {
|
2006-10-20 18:58:22 +05:30
|
|
|
bb_error_msg("packet with bad UDP checksum received, ignoring");
|
2006-05-08 08:50:50 +05:30
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
|
|
|
|
|
2007-11-23 05:38:54 +05:30
|
|
|
if (payload->cookie != htonl(DHCP_MAGIC)) {
|
2006-10-20 18:58:22 +05:30
|
|
|
bb_error_msg("received bogus message (bad magic) - ignoring");
|
2006-05-08 08:50:50 +05:30
|
|
|
return -2;
|
|
|
|
}
|
2006-09-07 00:06:50 +05:30
|
|
|
DEBUG("oooooh!!! got some!");
|
2006-05-08 08:50:50 +05:30
|
|
|
return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
|
|
|
|
}
|