Merge branch 'bugfix/fix_dhcp_server_address_pool_issue_5.1' into 'release/v5.1'

fix(dhcp server): Fix dhcp server address pool issue 5.1

See merge request espressif/esp-idf!25817
This commit is contained in:
Jiang Jiang Jian 2023-09-14 10:39:21 +08:00
commit 8f9c2c6891
3 changed files with 63 additions and 33 deletions

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -2228,34 +2228,43 @@ esp_err_t esp_netif_dhcps_option_api(esp_netif_api_msg_t *msg)
}
case REQUESTED_IP_ADDRESS: {
esp_netif_ip_info_t info;
uint32_t softap_ip = 0;
uint32_t server_ip = 0;
uint32_t start_ip = 0;
uint32_t end_ip = 0;
uint32_t range_start_ip = 0;
uint32_t range_end_ip = 0;
dhcps_lease_t *poll = opt->val;
if (poll->enable) {
memset(&info, 0x00, sizeof(esp_netif_ip_info_t));
esp_netif_get_ip_info(esp_netif, &info);
softap_ip = htonl(info.ip.addr);
server_ip = htonl(info.ip.addr);
range_start_ip = server_ip & htonl(info.netmask.addr);
range_end_ip = range_start_ip | ~htonl(info.netmask.addr);
if (server_ip == range_start_ip || server_ip == range_end_ip) {
return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
}
start_ip = htonl(poll->start_ip.addr);
end_ip = htonl(poll->end_ip.addr);
/*config ip information can't contain local ip*/
if ((start_ip <= softap_ip) && (softap_ip <= end_ip)) {
if ((server_ip >= start_ip) && (server_ip <= end_ip)) {
return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
}
/*config ip information must be in the same segment as the local ip*/
softap_ip >>= 8;
if ((start_ip >> 8 != softap_ip)
|| (end_ip >> 8 != softap_ip)) {
if (start_ip <= range_start_ip || start_ip >= range_end_ip) {
return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
}
if (end_ip - start_ip > DHCPS_MAX_LEASE) {
if (end_ip <= range_start_ip || end_ip >= range_end_ip) {
return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
}
/*The number of configured ip is less than DHCPS_MAX_LEASE*/
if ((end_ip - start_ip + 1 > DHCPS_MAX_LEASE) || (start_ip >= end_ip)) {
return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
}
} else {
return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
}
memcpy(opt_info, opt->val, opt->len);

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -76,6 +76,18 @@
} \
} while (0)
#define DHCP_CHECK_IP_MATCH_SUBNET_MASK(mask, ip) \
u32_t start_ip = 0; \
u32_t end_ip = 0; \
do { \
start_ip = ip & mask; \
end_ip = start_ip | ~mask; \
if (ip == end_ip || ip == start_ip) { \
DHCPS_LOG("dhcps: ip address and subnet mask do not match.\n"); \
return ERR_ARG; \
} \
} while (0)
#define MAX_STATION_NUM CONFIG_LWIP_DHCPS_MAX_STATION_NUM
#define DHCPS_STATE_OFFER 1
@ -1154,11 +1166,11 @@ static void handle_dhcp(void *arg,
*******************************************************************************/
static void dhcps_poll_set(dhcps_t *dhcps, u32_t ip)
{
u32_t server_ip = 0, local_ip = 0;
u32_t server_ip = 0;
u32_t start_ip = 0;
u32_t end_ip = 0;
u32_t temp_local_ip = 0;
u32_t host_num = 0;
u32_t range_start_ip = 0;
u32_t range_end_ip = 0;
dhcps_lease_t *dhcps_poll = &dhcps->dhcps_poll;
if (dhcps_poll->enable == true) {
server_ip = htonl(ip);
@ -1166,40 +1178,40 @@ static void dhcps_poll_set(dhcps_t *dhcps, u32_t ip)
end_ip = htonl(dhcps_poll->end_ip.addr);
/*config ip information can't contain local ip*/
if ((start_ip <= server_ip) && (server_ip <= end_ip)) {
if ((server_ip >= start_ip) && (server_ip <= end_ip)) {
dhcps_poll->enable = false;
} else {
/*config ip information must be in the same segment as the local ip*/
server_ip >>= 8;
if (((start_ip >> 8 != server_ip) || (end_ip >> 8 != server_ip))
|| (end_ip - start_ip > DHCPS_MAX_LEASE)) {
if (!ip4_addr_netcmp(&dhcps_poll->start_ip, &dhcps->server_address, &dhcps->dhcps_mask)
|| !ip4_addr_netcmp(&dhcps_poll->end_ip, &dhcps->server_address, &dhcps->dhcps_mask)
|| (end_ip - start_ip + 1 > DHCPS_MAX_LEASE)) {
dhcps_poll->enable = false;
}
}
}
if (dhcps_poll->enable == false) {
local_ip = server_ip = htonl(ip);
server_ip &= 0xFFFFFF00;
temp_local_ip = local_ip &= 0xFF;
server_ip = htonl(ip);
range_start_ip = server_ip & htonl(dhcps->dhcps_mask.addr);
range_end_ip = range_start_ip | ~htonl(dhcps->dhcps_mask.addr);
if (local_ip >= 0x80) {
local_ip -= DHCPS_MAX_LEASE;
temp_local_ip -= DHCPS_MAX_LEASE;
if (server_ip - range_start_ip > range_end_ip - server_ip) {
range_start_ip = range_start_ip + 1;
range_end_ip = server_ip - 1;
} else {
local_ip ++;
range_start_ip = server_ip + 1;
range_end_ip = range_end_ip - 1;
}
if (range_end_ip - range_start_ip + 1 > DHCPS_MAX_LEASE) {
range_end_ip = range_start_ip + DHCPS_MAX_LEASE - 1;
}
bzero(dhcps_poll, sizeof(*dhcps_poll));
host_num = IP_CLASS_HOST_NUM(htonl(dhcps->dhcps_mask.addr));
if (host_num > DHCPS_MAX_LEASE) {
host_num = DHCPS_MAX_LEASE;
}
dhcps_poll->start_ip.addr = server_ip | local_ip;
dhcps_poll->end_ip.addr = server_ip | (temp_local_ip + host_num - 1);
dhcps_poll->start_ip.addr = range_start_ip;
dhcps_poll->end_ip.addr = range_end_ip;
dhcps_poll->start_ip.addr = htonl(dhcps_poll->start_ip.addr);
dhcps_poll->end_ip.addr = htonl(dhcps_poll->end_ip.addr);
dhcps_poll->enable = true;
}
}
@ -1242,7 +1254,7 @@ err_t dhcps_start(dhcps_t *dhcps, struct netif *netif, ip4_addr_t ip)
dhcps->dhcps_pcb = udp_new();
if (dhcps->dhcps_pcb == NULL || ip4_addr_isany_val(ip)) {
printf("dhcps_start(): could not obtain pcb\n");
DHCPS_LOG("dhcps_start(): could not obtain pcb\n");
return ERR_ARG;
}
@ -1250,6 +1262,7 @@ err_t dhcps_start(dhcps_t *dhcps, struct netif *netif, ip4_addr_t ip)
dhcps->server_address.addr = ip.addr;
DHCP_CHECK_SUBNET_MASK_IP(htonl(dhcps->dhcps_mask.addr));
DHCP_CHECK_IP_MATCH_SUBNET_MASK(htonl(dhcps->dhcps_mask.addr), htonl(ip.addr));
dhcps_poll_set(dhcps, dhcps->server_address.addr);
dhcps->client_address_plus.addr = dhcps->dhcps_poll.start_ip.addr;

View File

@ -205,11 +205,19 @@ TEST(lwip, dhcp_server_start_stop_localhost)
// Class C: IP: 192.168.1.1, Mask: 255.255.255.0
dhcps_test_net_classes(0xC0A80101, 0xFFFFFF00, true);
// Class C: IP: 192.168.4.1, Mask: 255.255.0.0
dhcps_test_net_classes(0xC0A80401, 0xFFFF0000, true);
// Class C: IP: 192.168.4.1, Mask: 255.0.0.0
dhcps_test_net_classes(0xC0A80401, 0xFF000000, true);
// Class A: IP: 127.0.0.1, with inaccurate Mask: 255.248.255.0
// expect dhcps_start() to fail
dhcps_test_net_classes(0x7f000001, 0xFFF8FF00, false);
// Class C: IP: 192.168.200.8, with inaccurate Mask: 255.255.255.248
// expect dhcps_start() to fail
dhcps_test_net_classes(0xC0A8C808, 0xFFFFFFF8, false);
}