Merge branch 'Bugfix/softap_excedes_the_range_of_subnet_4.3' into 'release/v4.3'

dhcp server:bugfix softap excedes the range of subnet

See merge request espressif/esp-idf!21995
This commit is contained in:
Jiang Jiang Jian
2023-01-12 10:53:38 +08:00
5 changed files with 69 additions and 7 deletions

View File

@ -537,6 +537,9 @@ static const esp_err_msg_t esp_err_msg_table[] = {
# endif # endif
# ifdef ESP_ERR_ESP_NETIF_DNS_NOT_CONFIGURED # ifdef ESP_ERR_ESP_NETIF_DNS_NOT_CONFIGURED
ERR_TBL_IT(ESP_ERR_ESP_NETIF_DNS_NOT_CONFIGURED), /* 20490 0x500a */ ERR_TBL_IT(ESP_ERR_ESP_NETIF_DNS_NOT_CONFIGURED), /* 20490 0x500a */
# endif
# ifdef ESP_ERR_ESP_NETIF_DHCPS_START_FAILED
ERR_TBL_IT(ESP_ERR_ESP_NETIF_DHCPS_START_FAILED), /* 20491 0x500b */
# endif # endif
// components/esp_common/include/esp_err.h // components/esp_common/include/esp_err.h
# ifdef ESP_ERR_FLASH_BASE # ifdef ESP_ERR_FLASH_BASE

View File

@ -33,6 +33,7 @@ extern "C" {
#define ESP_ERR_ESP_NETIF_DRIVER_ATTACH_FAILED ESP_ERR_ESP_NETIF_BASE + 0x08 #define ESP_ERR_ESP_NETIF_DRIVER_ATTACH_FAILED ESP_ERR_ESP_NETIF_BASE + 0x08
#define ESP_ERR_ESP_NETIF_INIT_FAILED ESP_ERR_ESP_NETIF_BASE + 0x09 #define ESP_ERR_ESP_NETIF_INIT_FAILED ESP_ERR_ESP_NETIF_BASE + 0x09
#define ESP_ERR_ESP_NETIF_DNS_NOT_CONFIGURED ESP_ERR_ESP_NETIF_BASE + 0x0A #define ESP_ERR_ESP_NETIF_DNS_NOT_CONFIGURED ESP_ERR_ESP_NETIF_BASE + 0x0A
#define ESP_ERR_ESP_NETIF_DHCPS_START_FAILED ESP_ERR_ESP_NETIF_BASE + 0x0B
/** @brief Type of esp_netif_object server */ /** @brief Type of esp_netif_object server */

View File

@ -733,7 +733,11 @@ static esp_err_t esp_netif_start_api(esp_netif_api_msg_t *msg)
memcpy(&lwip_netmask, &default_ip->netmask, sizeof(struct ip4_addr)); memcpy(&lwip_netmask, &default_ip->netmask, sizeof(struct ip4_addr));
dhcps_set_new_lease_cb(esp_netif_dhcps_cb); dhcps_set_new_lease_cb(esp_netif_dhcps_cb);
dhcps_set_option_info(SUBNET_MASK, (void*)&lwip_netmask, sizeof(lwip_netmask)); dhcps_set_option_info(SUBNET_MASK, (void*)&lwip_netmask, sizeof(lwip_netmask));
dhcps_start(p_netif, lwip_ip); if (dhcps_start(p_netif, lwip_ip) != ERR_OK) {
ESP_LOGE(TAG, "DHCP server cannot be started");
esp_netif->dhcps_status = ESP_NETIF_DHCP_INIT;
return ESP_ERR_ESP_NETIF_DHCPS_START_FAILED;
}
esp_netif->dhcps_status = ESP_NETIF_DHCP_STARTED; esp_netif->dhcps_status = ESP_NETIF_DHCP_STARTED;
ESP_LOGD(TAG, "DHCP server started successfully"); ESP_LOGD(TAG, "DHCP server started successfully");
esp_netif_update_default_netif(esp_netif, ESP_NETIF_STARTED); esp_netif_update_default_netif(esp_netif, ESP_NETIF_STARTED);
@ -1145,7 +1149,11 @@ static esp_err_t esp_netif_dhcps_start_api(esp_netif_api_msg_t *msg)
memcpy(&lwip_netmask, &default_ip->netmask, sizeof(struct ip4_addr)); memcpy(&lwip_netmask, &default_ip->netmask, sizeof(struct ip4_addr));
dhcps_set_new_lease_cb(esp_netif_dhcps_cb); dhcps_set_new_lease_cb(esp_netif_dhcps_cb);
dhcps_set_option_info(SUBNET_MASK, (void*)&lwip_netmask, sizeof(lwip_netmask)); dhcps_set_option_info(SUBNET_MASK, (void*)&lwip_netmask, sizeof(lwip_netmask));
dhcps_start(p_netif, lwip_ip); if (dhcps_start(p_netif, lwip_ip) != ERR_OK) {
ESP_LOGE(TAG, "DHCP server cannot be started");
esp_netif->dhcps_status = ESP_NETIF_DHCP_INIT;
return ESP_ERR_ESP_NETIF_DHCPS_START_FAILED;
}
esp_netif->dhcps_status = ESP_NETIF_DHCP_STARTED; esp_netif->dhcps_status = ESP_NETIF_DHCP_STARTED;
ESP_LOGD(TAG, "DHCP server started successfully"); ESP_LOGD(TAG, "DHCP server started successfully");
return ESP_OK; return ESP_OK;

View File

@ -63,6 +63,43 @@
#define DHCPS_DEBUG 0 #define DHCPS_DEBUG 0
#define DHCPS_LOG printf #define DHCPS_LOG printf
#define IS_INVALID_SUBNET_MASK(x) (((x-1) | x) != 0xFFFFFFFF)
/* Notes:
* 1. Class a address range 0.0.0.0~127.255.255.255.
* 2. Class b address range 128.0.0.0~191.255.255.255.
* 3. Class c address range 192.0.0.0~223.255.255.255.
*/
#define IS_VALID_CLASSA_SUBNET_MASK(mask) (mask >= 0xFF000000 && mask <= 0xFFFE0000)
#define IS_VALID_CLASSB_SUBNET_MASK(mask) (mask >= 0xFFFF0000 && mask <= 0xFFFFFE00)
#define IS_VALID_CLASSC_SUBNET_MASK(mask) (mask >= 0xFFFFFF00 && mask <= 0xFFFFFFFC)
#define IP_CLASS_HOST_NUM(mask) (0xffffffff & ~mask)
#define DHCP_CHECK_SUBNET_MASK_IP(mask, ip) \
do { \
if (IS_INVALID_SUBNET_MASK(mask)) { \
DHCPS_LOG("dhcps: Illegal subnet mask.\n"); \
return ERR_ARG; \
} else { \
if (IP_CLASSA(ip)) { \
if(!IS_VALID_CLASSA_SUBNET_MASK(mask)) { \
DHCPS_LOG("dhcps: The subnet mask does not match the A address.\n"); \
return ERR_ARG; \
} \
} else if (IP_CLASSB(ip)) { \
if(!IS_VALID_CLASSB_SUBNET_MASK(mask)) { \
DHCPS_LOG("dhcps: The subnet mask does not match the B address.\n"); \
return ERR_ARG; \
} \
} else if (IP_CLASSC(ip)) { \
if(!IS_VALID_CLASSC_SUBNET_MASK(mask)) { \
DHCPS_LOG("dhcps: The subnet mask does not match the C address.\n"); \
return ERR_ARG; \
} \
} \
} \
} while (0)
#define MAX_STATION_NUM CONFIG_LWIP_DHCPS_MAX_STATION_NUM #define MAX_STATION_NUM CONFIG_LWIP_DHCPS_MAX_STATION_NUM
#define DHCPS_STATE_OFFER 1 #define DHCPS_STATE_OFFER 1
@ -1079,6 +1116,8 @@ static void dhcps_poll_set(u32_t ip)
u32_t softap_ip = 0, local_ip = 0; u32_t softap_ip = 0, local_ip = 0;
u32_t start_ip = 0; u32_t start_ip = 0;
u32_t end_ip = 0; u32_t end_ip = 0;
u32_t temp_local_ip = 0;
u32_t host_num = 0;
if (dhcps_poll.enable == true) { if (dhcps_poll.enable == true) {
softap_ip = htonl(ip); softap_ip = htonl(ip);
@ -1102,17 +1141,22 @@ static void dhcps_poll_set(u32_t ip)
if (dhcps_poll.enable == false) { if (dhcps_poll.enable == false) {
local_ip = softap_ip = htonl(ip); local_ip = softap_ip = htonl(ip);
softap_ip &= 0xFFFFFF00; softap_ip &= 0xFFFFFF00;
local_ip &= 0xFF; temp_local_ip = local_ip &= 0xFF;
if (local_ip >= 0x80) { if (local_ip >= 0x80) {
local_ip -= DHCPS_MAX_LEASE; local_ip -= DHCPS_MAX_LEASE;
temp_local_ip -= DHCPS_MAX_LEASE;
} else { } else {
local_ip ++; local_ip ++;
} }
bzero(&dhcps_poll, sizeof(dhcps_poll)); bzero(&dhcps_poll, sizeof(dhcps_poll));
host_num = IP_CLASS_HOST_NUM(htonl(s_dhcps_mask.addr));
if (host_num > DHCPS_MAX_LEASE) {
host_num = DHCPS_MAX_LEASE;
}
dhcps_poll.start_ip.addr = softap_ip | local_ip; dhcps_poll.start_ip.addr = softap_ip | local_ip;
dhcps_poll.end_ip.addr = softap_ip | (local_ip + DHCPS_MAX_LEASE - 1); dhcps_poll.end_ip.addr = softap_ip | (temp_local_ip + host_num - 1);
dhcps_poll.start_ip.addr = htonl(dhcps_poll.start_ip.addr); dhcps_poll.start_ip.addr = htonl(dhcps_poll.start_ip.addr);
dhcps_poll.end_ip.addr = htonl(dhcps_poll.end_ip.addr); dhcps_poll.end_ip.addr = htonl(dhcps_poll.end_ip.addr);
} }
@ -1139,8 +1183,11 @@ void dhcps_set_new_lease_cb(dhcps_cb_t cb)
* : info -- The current ip info * : info -- The current ip info
* Returns : none * Returns : none
*******************************************************************************/ *******************************************************************************/
void dhcps_start(struct netif *netif, ip4_addr_t ip) err_t dhcps_start(struct netif *netif, ip4_addr_t ip)
{ {
if (netif == NULL) {
return ERR_ARG;
}
dhcps_netif = netif; dhcps_netif = netif;
if (dhcps_netif->dhcps_pcb != NULL) { if (dhcps_netif->dhcps_pcb != NULL) {
@ -1152,6 +1199,7 @@ void dhcps_start(struct netif *netif, ip4_addr_t ip)
if (pcb_dhcps == NULL || ip4_addr_isany_val(ip)) { if (pcb_dhcps == NULL || ip4_addr_isany_val(ip)) {
printf("dhcps_start(): could not obtain pcb\n"); printf("dhcps_start(): could not obtain pcb\n");
return ERR_ARG;
} }
dhcps_netif->dhcps_pcb = pcb_dhcps; dhcps_netif->dhcps_pcb = pcb_dhcps;
@ -1159,6 +1207,7 @@ void dhcps_start(struct netif *netif, ip4_addr_t ip)
IP4_ADDR(&broadcast_dhcps, 255, 255, 255, 255); IP4_ADDR(&broadcast_dhcps, 255, 255, 255, 255);
server_address.addr = ip.addr; server_address.addr = ip.addr;
DHCP_CHECK_SUBNET_MASK_IP(htonl(s_dhcps_mask.addr), htonl(server_address.addr));
dhcps_poll_set(server_address.addr); dhcps_poll_set(server_address.addr);
client_address_plus.addr = dhcps_poll.start_ip.addr; client_address_plus.addr = dhcps_poll.start_ip.addr;
@ -1168,7 +1217,7 @@ void dhcps_start(struct netif *netif, ip4_addr_t ip)
#if DHCPS_DEBUG #if DHCPS_DEBUG
DHCPS_LOG("dhcps:dhcps_start->udp_recv function Set a receive callback handle_dhcp for UDP_PCB pcb_dhcps\n"); DHCPS_LOG("dhcps:dhcps_start->udp_recv function Set a receive callback handle_dhcp for UDP_PCB pcb_dhcps\n");
#endif #endif
return ERR_OK;
} }
/****************************************************************************** /******************************************************************************

View File

@ -16,6 +16,7 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#include "lwip/ip_addr.h" #include "lwip/ip_addr.h"
#include "lwip/err.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -86,7 +87,7 @@ static inline bool dhcps_dns_enabled (dhcps_offer_t offer)
return (offer & OFFER_DNS) != 0; return (offer & OFFER_DNS) != 0;
} }
void dhcps_start(struct netif *netif, ip4_addr_t ip); err_t dhcps_start(struct netif *netif, ip4_addr_t ip);
void dhcps_stop(struct netif *netif); void dhcps_stop(struct netif *netif);
void *dhcps_option_info(u8_t op_id, u32_t opt_len); void *dhcps_option_info(u8_t op_id, u32_t opt_len);
void dhcps_set_option_info(u8_t op_id, void *opt_info, u32_t opt_len); void dhcps_set_option_info(u8_t op_id, void *opt_info, u32_t opt_len);