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 */
|
2008-09-26 15:04:59 +05:30
|
|
|
uint32_t FAST_FUNC 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-16 15:34:23 +05:30
|
|
|
/* Initialize the packet with the proper defaults */
|
2006-05-08 08:50:50 +05:30
|
|
|
static void init_packet(struct dhcpMessage *packet, char type)
|
|
|
|
{
|
2006-05-28 06:36:36 +05:30
|
|
|
udhcp_init_header(packet, type);
|
2009-06-16 15:34:23 +05:30
|
|
|
memcpy(packet->chaddr, client_config.client_mac, 6);
|
2006-05-08 08:50:50 +05:30
|
|
|
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);
|
2008-01-26 00:57:08 +05:30
|
|
|
if ((type != DHCPDECLINE) && (type != DHCPRELEASE))
|
|
|
|
add_option_string(packet->options, client_config.vendorclass);
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 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. */
|
2008-01-26 00:57:08 +05:30
|
|
|
static void add_param_req_option(struct dhcpMessage *packet)
|
2006-05-08 08:50:50 +05:30
|
|
|
{
|
2007-12-10 12:33:38 +05:30
|
|
|
uint8_t c;
|
2006-05-08 08:50:50 +05:30
|
|
|
int end = end_option(packet->options);
|
|
|
|
int i, len = 0;
|
|
|
|
|
2007-12-10 12:33:38 +05:30
|
|
|
for (i = 0; (c = dhcp_options[i].code) != 0; i++) {
|
2008-04-02 18:34:19 +05:30
|
|
|
if (((dhcp_options[i].flags & OPTION_REQ)
|
2008-06-25 15:23:17 +05:30
|
|
|
&& !client_config.no_default_options)
|
2007-12-10 12:33:38 +05:30
|
|
|
|| (client_config.opt_mask[c >> 3] & (1 << (c & 7)))
|
|
|
|
) {
|
2008-01-26 00:57:08 +05:30
|
|
|
packet->options[end + OPT_DATA + len] = c;
|
|
|
|
len++;
|
2007-12-10 12:33:38 +05:30
|
|
|
}
|
|
|
|
}
|
2008-04-02 18:34:19 +05:30
|
|
|
if (len) {
|
|
|
|
packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
|
|
|
|
packet->options[end + OPT_LEN] = len;
|
|
|
|
packet->options[end + OPT_DATA + len] = DHCP_END;
|
|
|
|
}
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
2008-09-27 05:15:20 +05:30
|
|
|
/* RFC 2131
|
|
|
|
* 4.4.4 Use of broadcast and unicast
|
|
|
|
*
|
|
|
|
* The DHCP client broadcasts DHCPDISCOVER, DHCPREQUEST and DHCPINFORM
|
|
|
|
* messages, unless the client knows the address of a DHCP server.
|
|
|
|
* The client unicasts DHCPRELEASE messages to the server. Because
|
|
|
|
* the client is declining the use of the IP address supplied by the server,
|
|
|
|
* the client broadcasts DHCPDECLINE messages.
|
|
|
|
*
|
|
|
|
* When the DHCP client knows the address of a DHCP server, in either
|
|
|
|
* INIT or REBOOTING state, the client may use that address
|
|
|
|
* in the DHCPDISCOVER or DHCPREQUEST rather than the IP broadcast address.
|
|
|
|
* The client may also use unicast to send DHCPINFORM messages
|
|
|
|
* to a known DHCP server. If the client receives no response to DHCP
|
|
|
|
* messages sent to the IP address of a known DHCP server, the DHCP
|
|
|
|
* client reverts to using the IP broadcast address.
|
|
|
|
*/
|
2008-01-26 00:57:08 +05:30
|
|
|
|
2008-09-26 21:59:12 +05:30
|
|
|
static int raw_bcast_from_client_config_ifindex(struct dhcpMessage *packet)
|
|
|
|
{
|
|
|
|
return udhcp_send_raw_packet(packet,
|
|
|
|
/*src*/ INADDR_ANY, CLIENT_PORT,
|
|
|
|
/*dst*/ INADDR_BROADCAST, SERVER_PORT, MAC_BCAST_ADDR,
|
|
|
|
client_config.ifindex);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-22 06:28:49 +05:30
|
|
|
#if ENABLE_FEATURE_UDHCPC_ARPING
|
2008-09-26 21:59:12 +05:30
|
|
|
/* Broadcast a DHCP decline message */
|
2008-09-26 15:04:59 +05:30
|
|
|
int FAST_FUNC send_decline(uint32_t xid, uint32_t server, uint32_t requested)
|
2007-11-22 06:28:49 +05:30
|
|
|
{
|
|
|
|
struct dhcpMessage packet;
|
|
|
|
|
|
|
|
init_packet(&packet, DHCPDECLINE);
|
|
|
|
packet.xid = xid;
|
2008-01-26 00:57:08 +05:30
|
|
|
add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
|
|
|
|
add_simple_option(packet.options, DHCP_SERVER_ID, server);
|
2007-11-22 06:28:49 +05:30
|
|
|
|
|
|
|
bb_info_msg("Sending decline...");
|
|
|
|
|
2008-09-26 21:59:12 +05:30
|
|
|
return raw_bcast_from_client_config_ifindex(&packet);
|
2007-11-22 06:28:49 +05:30
|
|
|
}
|
|
|
|
#endif
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2009-06-16 15:34:23 +05:30
|
|
|
|
2006-05-08 08:50:50 +05:30
|
|
|
/* Broadcast a DHCP discover packet to the network, with an optionally requested IP */
|
2008-09-26 15:04:59 +05:30
|
|
|
int FAST_FUNC 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);
|
|
|
|
|
2007-11-29 00:53:12 +05:30
|
|
|
/* Explicitly saying that we want RFC-compliant packets helps
|
|
|
|
* some buggy DHCP servers to NOT send bigger packets */
|
|
|
|
add_simple_option(packet.options, DHCP_MAX_SIZE, htons(576));
|
2008-04-02 18:34:19 +05:30
|
|
|
|
2008-01-26 00:57:08 +05:30
|
|
|
add_param_req_option(&packet);
|
2008-04-02 18:34:19 +05:30
|
|
|
|
2006-09-07 00:06:50 +05:30
|
|
|
bb_info_msg("Sending discover...");
|
2008-09-26 21:59:12 +05:30
|
|
|
return raw_bcast_from_client_config_ifindex(&packet);
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-16 15:34:23 +05:30
|
|
|
/* Broadcast a DHCP request message */
|
2008-09-27 05:15:20 +05:30
|
|
|
/* RFC 2131 3.1 paragraph 3:
|
|
|
|
* "The client _broadcasts_ a DHCPREQUEST message..."
|
|
|
|
*/
|
2008-09-26 21:59:12 +05:30
|
|
|
int FAST_FUNC send_select(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);
|
2008-01-26 00:57:08 +05:30
|
|
|
add_param_req_option(&packet);
|
2008-04-02 18:34:19 +05:30
|
|
|
|
2006-05-08 08:50:50 +05:30
|
|
|
addr.s_addr = requested;
|
2006-09-07 00:06:50 +05:30
|
|
|
bb_info_msg("Sending select for %s...", inet_ntoa(addr));
|
2008-09-26 21:59:12 +05:30
|
|
|
return raw_bcast_from_client_config_ifindex(&packet);
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Unicasts or broadcasts a DHCP renew message */
|
2008-09-26 15:04:59 +05:30
|
|
|
int FAST_FUNC 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;
|
|
|
|
|
2008-01-26 00:57:08 +05:30
|
|
|
add_param_req_option(&packet);
|
2006-09-07 00:06:50 +05:30
|
|
|
bb_info_msg("Sending renew...");
|
2006-05-08 08:50:50 +05:30
|
|
|
if (server)
|
2008-09-26 21:59:12 +05:30
|
|
|
return udhcp_send_kernel_packet(&packet,
|
|
|
|
ciaddr, CLIENT_PORT,
|
|
|
|
server, SERVER_PORT);
|
2007-07-03 21:17:50 +05:30
|
|
|
|
2008-09-26 21:59:12 +05:30
|
|
|
return raw_bcast_from_client_config_ifindex(&packet);
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Unicasts a DHCP release message */
|
2008-09-26 15:04:59 +05:30
|
|
|
int FAST_FUNC 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_SERVER_ID, server);
|
|
|
|
|
2006-09-07 00:06:50 +05:30
|
|
|
bb_info_msg("Sending release...");
|
2007-12-21 02:41:38 +05:30
|
|
|
return udhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-24 23:02:22 +05:30
|
|
|
/* Returns -1 on errors that are fatal for the socket, -2 for those that aren't */
|
2009-06-17 15:24:52 +05:30
|
|
|
int FAST_FUNC udhcp_recv_raw_packet(struct dhcpMessage *dhcp_pkt, int fd)
|
2006-05-08 08:50:50 +05:30
|
|
|
{
|
|
|
|
int bytes;
|
|
|
|
struct udp_dhcp_packet packet;
|
|
|
|
uint16_t check;
|
|
|
|
|
2007-12-24 23:02:22 +05:30
|
|
|
memset(&packet, 0, sizeof(packet));
|
|
|
|
bytes = safe_read(fd, &packet, sizeof(packet));
|
2006-05-08 08:50:50 +05:30
|
|
|
if (bytes < 0) {
|
2009-06-17 15:24:52 +05:30
|
|
|
log1("Packet read error, ignoring");
|
2008-05-21 12:35:06 +05:30
|
|
|
/* NB: possible down interface, etc. Caller should pause. */
|
2007-12-24 23:02:22 +05:30
|
|
|
return bytes; /* returns -1 */
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
2007-12-24 23:02:22 +05:30
|
|
|
if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp))) {
|
2009-06-17 15:24:52 +05:30
|
|
|
log1("Packet is too short, ignoring");
|
2006-05-08 08:50:50 +05:30
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytes < ntohs(packet.ip.tot_len)) {
|
2007-12-24 23:02:22 +05:30
|
|
|
/* packet is bigger than sizeof(packet), we did partial read */
|
2009-06-17 15:24:52 +05:30
|
|
|
log1("Oversized packet, ignoring");
|
2006-05-08 08:50:50 +05:30
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ignore any extra garbage bytes */
|
|
|
|
bytes = ntohs(packet.ip.tot_len);
|
|
|
|
|
2007-12-24 23:02:22 +05:30
|
|
|
/* 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)
|
2007-12-24 23:02:22 +05:30
|
|
|
/* || bytes > (int) sizeof(packet) - can't happen */
|
2006-11-19 01:21:32 +05:30
|
|
|
|| ntohs(packet.udp.len) != (uint16_t)(bytes - sizeof(packet.ip))
|
|
|
|
) {
|
2009-06-17 15:24:52 +05:30
|
|
|
log1("Unrelated/bogus packet, ignoring");
|
2006-05-08 08:50:50 +05:30
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2007-12-21 02:41:38 +05:30
|
|
|
/* verify IP checksum */
|
2006-05-08 08:50:50 +05:30
|
|
|
check = packet.ip.check;
|
|
|
|
packet.ip.check = 0;
|
2007-12-21 02:41:38 +05:30
|
|
|
if (check != udhcp_checksum(&packet.ip, sizeof(packet.ip))) {
|
2009-06-17 15:24:52 +05:30
|
|
|
log1("Bad IP header checksum, ignoring");
|
2007-12-24 23:02:22 +05:30
|
|
|
return -2;
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
2007-12-21 02:41:38 +05:30
|
|
|
/* verify UDP checksum. IP header has to be modified for this */
|
|
|
|
memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
|
2007-12-24 23:02:22 +05:30
|
|
|
/* ip.xx fields which are not memset: protocol, check, saddr, daddr */
|
2007-12-21 02:41:38 +05:30
|
|
|
packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
|
2006-05-08 08:50:50 +05:30
|
|
|
check = packet.udp.check;
|
|
|
|
packet.udp.check = 0;
|
2006-05-28 06:36:36 +05:30
|
|
|
if (check && check != udhcp_checksum(&packet, bytes)) {
|
2009-06-17 15:24:52 +05:30
|
|
|
log1("Packet with bad UDP checksum received, ignoring");
|
2006-05-08 08:50:50 +05:30
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2009-06-17 15:24:52 +05:30
|
|
|
memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2009-06-17 15:24:52 +05:30
|
|
|
if (dhcp_pkt->cookie != htonl(DHCP_MAGIC)) {
|
|
|
|
bb_info_msg("Packet with bad magic, ignoring");
|
2006-05-08 08:50:50 +05:30
|
|
|
return -2;
|
|
|
|
}
|
2009-06-17 15:24:52 +05:30
|
|
|
log1("Got valid DHCP packet");
|
|
|
|
udhcp_dump_packet(dhcp_pkt);
|
2006-05-08 08:50:50 +05:30
|
|
|
return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
|
|
|
|
}
|