Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions subsys/net/lib/dhcpv4/dhcpv4.c
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,6 @@ static uint32_t dhcpv4_send_request(struct net_if *iface)
struct net_pkt *pkt = NULL;
uint32_t timeout = UINT32_MAX;

iface->config.dhcpv4.xid++;

switch (iface->config.dhcpv4.state) {
case NET_DHCPV4_DISABLED:
case NET_DHCPV4_INIT:
Expand Down Expand Up @@ -978,6 +976,13 @@ static bool dhcpv4_parse_options(struct net_pkt *pkt,
goto end;
}

if (type == DHCPV4_OPTIONS_PAD) {
/* Pad option has a fixed 1-byte length and should be
* ignored.
*/
continue;
}

if (net_pkt_read_u8(pkt, &length)) {
NET_ERR("option parsing, bad length");
return false;
Expand Down Expand Up @@ -1652,8 +1657,11 @@ const char *net_dhcpv4_msg_type_name(enum net_dhcpv4_msg_type msg_type)
"inform"
};

__ASSERT_NO_MSG(msg_type >= 1 && msg_type <= sizeof(name));
return name[msg_type - 1];
if (msg_type >= 1 && msg_type <= sizeof(name)) {
return name[msg_type - 1];
}

return "invalid";
}

static void dhcpv4_start_internal(struct net_if *iface, bool first_start)
Expand Down
1 change: 1 addition & 0 deletions subsys/net/lib/dhcpv4/dhcpv4_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ struct dhcp_msg {
#define DHCPV4_SERVER_PORT 67
#define DHCPV4_CLIENT_PORT 68

#define DHCPV4_OPTIONS_PAD 0
#define DHCPV4_OPTIONS_SUBNET_MASK 1
#define DHCPV4_OPTIONS_ROUTER 3
#define DHCPV4_OPTIONS_DNS_SERVER 6
Expand Down
21 changes: 19 additions & 2 deletions tests/net/dhcpv4/client/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ LOG_MODULE_REGISTER(net_test, CONFIG_NET_DHCPV4_LOG_LEVEL);
#include "net_private.h"

/* Sample DHCP offer (420 bytes) */
static const unsigned char offer[420] = {
static const unsigned char offer[] = {
0x02, 0x01, 0x06, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x0a, 0xed, 0x48, 0x9e, 0x0a, 0xb8,
Expand Down Expand Up @@ -70,6 +70,8 @@ static const unsigned char offer[420] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* Magic cookie: DHCP */
0x63, 0x82, 0x53, 0x63,
/* [0] Pad option */
0x00,
/* [53] DHCP Message Type: OFFER */
0x35, 0x01, 0x02,
/* [1] Subnet Mask: 255.255.255.0 */
Expand Down Expand Up @@ -124,7 +126,7 @@ static const unsigned char offer[420] = {
};

/* Sample DHCPv4 ACK */
static const unsigned char ack[420] = {
static const unsigned char ack[] = {
0x02, 0x01, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0a, 0xed, 0x48, 0x9e, 0x00, 0x00, 0x00, 0x00,
Expand Down Expand Up @@ -157,6 +159,8 @@ static const unsigned char ack[420] = {
0x00, 0x00, 0x00, 0x00,
/* Magic cookie: DHCP */
0x63, 0x82, 0x53, 0x63,
/* [0] Pad option */
0x00,
/* [53] DHCP Message Type: ACK */
0x35, 0x01, 0x05,
/* [58] Renewal Time Value: (21600s) 6 hours */
Expand Down Expand Up @@ -230,6 +234,9 @@ struct dhcp_msg {
uint8_t type;
};

static uint32_t offer_xid;
static uint32_t request_xid;

static struct k_sem test_lock;

#define WAIT_TIME K_SECONDS(CONFIG_NET_DHCPV4_INITIAL_DELAY_MAX + 1)
Expand Down Expand Up @@ -306,6 +313,8 @@ struct net_pkt *prepare_dhcp_offer(struct net_if *iface, uint32_t xid)

net_ipv4_finalize(pkt, IPPROTO_UDP);

offer_xid = xid;

return pkt;

fail:
Expand Down Expand Up @@ -392,6 +401,10 @@ static int parse_dhcp_message(struct net_pkt *pkt, struct dhcp_msg *msg)
return 0;
}

if (msg->type == NET_DHCPV4_MSG_TYPE_REQUEST) {
request_xid = msg->xid;
}

return 1;
}

Expand Down Expand Up @@ -687,6 +700,10 @@ ZTEST(dhcpv4_tests, test_dhcp)
zassert_true(false, "Timeout while waiting");
}
}

/* Verify that Request xid matched Offer xid. */
zassert_equal(offer_xid, request_xid, "Offer/Request xid mismatch, "
"Offer 0x%08x, Request 0x%08x", offer_xid, request_xid);
}

/**test case main entry */
Expand Down
Loading