diff --git a/subsys/net/lib/dhcpv4/dhcpv4.c b/subsys/net/lib/dhcpv4/dhcpv4.c index b12f24bbb8bf3..7b34177fdb868 100644 --- a/subsys/net/lib/dhcpv4/dhcpv4.c +++ b/subsys/net/lib/dhcpv4/dhcpv4.c @@ -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: @@ -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; @@ -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) diff --git a/subsys/net/lib/dhcpv4/dhcpv4_internal.h b/subsys/net/lib/dhcpv4/dhcpv4_internal.h index 350e16946bebe..baf8a1aa7772e 100644 --- a/subsys/net/lib/dhcpv4/dhcpv4_internal.h +++ b/subsys/net/lib/dhcpv4/dhcpv4_internal.h @@ -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 diff --git a/tests/net/dhcpv4/client/src/main.c b/tests/net/dhcpv4/client/src/main.c index ceddbbe9e6982..1c9cfda3bb283 100644 --- a/tests/net/dhcpv4/client/src/main.c +++ b/tests/net/dhcpv4/client/src/main.c @@ -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, @@ -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 */ @@ -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, @@ -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 */ @@ -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) @@ -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: @@ -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; } @@ -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 */