diff --git a/components/lwip/apps/dhcpserver/dhcpserver.c b/components/lwip/apps/dhcpserver/dhcpserver.c index 6d8cb6e70f54..43d518621edd 100644 --- a/components/lwip/apps/dhcpserver/dhcpserver.c +++ b/components/lwip/apps/dhcpserver/dhcpserver.c @@ -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); @@ -1170,34 +1182,33 @@ static void dhcps_poll_set(dhcps_t *dhcps, u32_t 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; } - - 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; + if (range_end_ip - range_start_ip + 1 > DHCPS_MAX_LEASE) { + range_end_ip = range_start_ip + DHCPS_MAX_LEASE - 1; } - dhcps_poll->start_ip.addr = server_ip | local_ip; - dhcps_poll->end_ip.addr = server_ip | (temp_local_ip + host_num - 1); + bzero(dhcps_poll, sizeof(*dhcps_poll)); + 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); } @@ -1242,7 +1253,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 +1261,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; diff --git a/components/lwip/test_apps/main/lwip_test.c b/components/lwip/test_apps/main/lwip_test.c index 70471462638d..6a34edeb43bf 100644 --- a/components/lwip/test_apps/main/lwip_test.c +++ b/components/lwip/test_apps/main/lwip_test.c @@ -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); }