From b396a7a302618ec7d4621d94d62eae1fd9e7f104 Mon Sep 17 00:00:00 2001 From: Our Air Quality Date: Tue, 6 Jun 2017 12:47:21 +1000 Subject: [PATCH] LwIP v2 support --- .gitmodules | 2 +- core/include/sdk_internal.h | 35 +- examples/http_get/http_get.c | 7 +- examples/tls_server/tls_server.c | 5 +- .../tls_server_bearssl/tls_server_bearssl.c | 5 +- extras/dhcpserver/dhcpserver.c | 109 ++++--- extras/dhcpserver/include/dhcpserver.h | 4 +- extras/httpd/httpd.c | 2 +- extras/sntp/sntp.c | 18 +- include/espressif/esp_wifi.h | 6 +- include/espressif/sdk_private.h | 4 +- lib/libnet80211.a | Bin 183796 -> 183796 bytes lib/libwpa.a | Bin 160156 -> 160156 bytes lwip/component.mk | 3 - lwip/esp_interface.c | 34 +- lwip/include/arch/cc.h | 13 +- lwip/include/lwipopts.h | 205 ++++++++++-- lwip/lwip | 2 +- open_esplibs/include/esplibs/libmain.h | 1 + open_esplibs/include/esplibs/libnet80211.h | 8 +- open_esplibs/include/esplibs/libpp.h | 1 + open_esplibs/include/esplibs/libwpa.h | 1 + open_esplibs/libmain/user_interface.c | 3 +- open_esplibs/libnet80211/ieee80211_hostap.c | 303 ++++++++++++++++++ open_esplibs/libnet80211/ieee80211_input.c | 5 +- open_esplibs/libnet80211/wl_cnx.c | 60 +++- open_esplibs/libwpa/wpa_main.c | 16 +- 27 files changed, 674 insertions(+), 178 deletions(-) diff --git a/.gitmodules b/.gitmodules index 19b37943..17f80032 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,6 @@ [submodule "lwip/lwip"] path = lwip/lwip - url = https://github.com/SuperHouse/esp-lwip.git + url = https://github.com/ourairquality/lwip.git [submodule "extras/mbedtls/mbedtls"] path = extras/mbedtls/mbedtls url = https://github.com/ARMmbed/mbedtls.git diff --git a/core/include/sdk_internal.h b/core/include/sdk_internal.h index 1847eefb..130586b9 100644 --- a/core/include/sdk_internal.h +++ b/core/include/sdk_internal.h @@ -83,7 +83,7 @@ struct _unknown_wpa1 { }; -struct sdk_netif_conninfo { +struct sdk_cnx_node { uint8_t mac_addr[6]; uint8_t _unknown07[2]; @@ -105,10 +105,12 @@ struct sdk_netif_conninfo { uint16_t _unknown9c; // ieee80211_hostap. increases by one one each timer func called. uint16_t _unknown9e; - uint32_t _unknowna0[18]; + uint32_t _unknowna0[17]; - int8_t _unknowne8; // - int8_t _unknowne9; // ppInstallKey + void *_unknowne4; + + uint8_t _unknowne8; // + uint8_t _unknowne9; // ppInstallKey int8_t _unknownea; int8_t _unknowneb; @@ -128,9 +130,9 @@ struct sdk_g_ic_netif_info { uint32_t _unknown48; // 0x48 uint8_t _unknown4c; // 0x4c uint8_t _unknown4d[59]; // 0x4d - 0x88 - struct sdk_netif_conninfo *_unknown88; // 0x88 + struct sdk_cnx_node *_unknown88; // 0x88 uint32_t _unknown8c; // 0x8c - struct sdk_netif_conninfo *conninfo[6]; // 0x90 - 0xa8 + struct sdk_cnx_node *cnx_nodes[6]; // 0x90 - 0xa8 uint8_t _unknowna8[12]; // 0xa8 - 0xb4 struct _unknown_softap1 *_unknownb4; uint8_t statusb8; // 0xb8 (arg of sta_status_set) @@ -260,7 +262,7 @@ struct sdk_g_ic_saved_st { uint8_t _unknown30d; // result of ieee80211_chan2ieee uint8_t _unknown30e; uint8_t _unknown30f; - uint8_t _unknown310; // count of entries in the softap conninfo array, less two. + uint8_t _unknown310; // count of entries in the softap cnx_node array, less two. uint8_t _unknown311[3]; @@ -331,8 +333,8 @@ _Static_assert(offsetof(struct _unknown_softap1, _unknown18) == 0x18, "bad struc _Static_assert(sizeof(struct _unknown_wpa1) == 0x4c, "_unknown_wpa1 is the wrong size!"); _Static_assert(offsetof(struct _unknown_wpa1, _unknown48) == 0x48, "bad struct"); -_Static_assert(offsetof(struct sdk_netif_conninfo, _unknown78) == 0x78, "bad struct"); -_Static_assert(offsetof(struct sdk_netif_conninfo, _unknown108) == 0x108, "bad struct"); +_Static_assert(offsetof(struct sdk_cnx_node, _unknown78) == 0x78, "bad struct"); +_Static_assert(offsetof(struct sdk_cnx_node, _unknown108) == 0x108, "bad struct"); _Static_assert(offsetof(struct sdk_g_ic_netif_info, started) == 0xbb, "bad struct"); @@ -361,17 +363,12 @@ _Static_assert(offsetof(struct netif, state) == 28, "netif->state offset wrong!" // Some sdk uses of netif->hwaddr have been converted to source code, but many // remain, but the content of this slot should not change in future versions of -// lwip, so just ensure it is at the expected offset. -_Static_assert(offsetof(struct netif, hwaddr) == 41, "netif->hwaddr offset wrong!"); - -// Most sdk uses of the netif->flags have been converted to source code. One -// known sdk binary read of the flags remains in wl_cnx.o:sdk_cnx_sta_leave -// which checks the NETIF_FLAG_DHCP flag. The NETIF_FLAG_DHCP has been removed -// in lwip v2, so some lwip hacks are needed to handle this for now until -// wl_cnx.o is converted so source code too. -_Static_assert(offsetof(struct netif, flags) == 47, "netif->flags offset wrong!"); +// lwip, so just ensure it is at the expected offset. Note the sdk binary +// libraries have been patched to move this offset from 41 to 42 to keep it +// 16-bit aligned to keep lwip v2 happy. +_Static_assert(offsetof(struct netif, hwaddr) == 42, "netif->hwaddr offset wrong!"); -_Static_assert(offsetof(struct pbuf, eb) == 16, "pbuf->eb offset wrong!"); +_Static_assert(offsetof(struct pbuf, esf_buf) == 16, "pbuf->esf_buf offset wrong!"); /// Misc. diff --git a/examples/http_get/http_get.c b/examples/http_get/http_get.c index 0fe0ae14..3a65f15e 100644 --- a/examples/http_get/http_get.c +++ b/examples/http_get/http_get.c @@ -22,7 +22,7 @@ #define WEB_SERVER "chainxor.org" #define WEB_PORT 80 -#define WEB_URL "http://chainxor.org/" +#define WEB_PATH "/" void http_get_task(void *pvParameters) { @@ -75,7 +75,8 @@ void http_get_task(void *pvParameters) freeaddrinfo(res); const char *req = - "GET "WEB_URL"\r\n" + "GET "WEB_PATH" HTTP/1.1\r\n" + "Host: "WEB_SERVER"\r\n" "User-Agent: esp-open-rtos/0.1 esp8266\r\n" "\r\n"; if (write(s, req, strlen(req)) < 0) { @@ -126,6 +127,6 @@ void user_init(void) sdk_wifi_set_opmode(STATION_MODE); sdk_wifi_station_set_config(&config); - xTaskCreate(&http_get_task, "get_task", 256, NULL, 2, NULL); + xTaskCreate(&http_get_task, "get_task", 384, NULL, 2, NULL); } diff --git a/examples/tls_server/tls_server.c b/examples/tls_server/tls_server.c index 79596095..e23e0041 100644 --- a/examples/tls_server/tls_server.c +++ b/examples/tls_server/tls_server.c @@ -202,9 +202,8 @@ void tls_server_task(void *pvParameters) socklen_t peer_addr_len = sizeof(struct sockaddr_in); getpeername(client_ctx.fd, (struct sockaddr *)&peer_addr, &peer_addr_len); unsigned char buf[256]; - int len = sprintf((char *) buf, "O hai, client %d.%d.%d.%d:%d\nFree heap size is %d bytes\n", - ip4_addr1(&peer_addr.sin_addr), ip4_addr2(&peer_addr.sin_addr), - ip4_addr3(&peer_addr.sin_addr), ip4_addr4(&peer_addr.sin_addr), + int len = sprintf((char *) buf, "O hai, client " IPSTR ":%d\nFree heap size is %d bytes\n", + IP2STR((ip4_addr_t *)&peer_addr.sin_addr.s_addr), peer_addr.sin_port, xPortGetFreeHeapSize()); while((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) { diff --git a/examples/tls_server_bearssl/tls_server_bearssl.c b/examples/tls_server_bearssl/tls_server_bearssl.c index 09632ae3..b1e48dc0 100644 --- a/examples/tls_server_bearssl/tls_server_bearssl.c +++ b/examples/tls_server_bearssl/tls_server_bearssl.c @@ -146,9 +146,8 @@ void tls_server_task(void *pvParameters) /* Prepare a message to the client */ unsigned char buf[100]; - int len = sprintf((char *) buf, "O hai, client %d.%d.%d.%d:%d\r\nFree heap size is %d bytes\r\n", - ip4_addr1(&sa.sin_addr), ip4_addr2(&sa.sin_addr), - ip4_addr3(&sa.sin_addr), ip4_addr4(&sa.sin_addr), + int len = sprintf((char *) buf, "O hai, client " IPSTR ":%d\r\nFree heap size is %d bytes\r\n", + IP2STR((ip4_addr_t *)&sa.sin_addr.s_addr), ntohs(sa.sin_port), xPortGetFreeHeapSize()); /* Send the message and close the connection */ diff --git a/extras/dhcpserver/dhcpserver.c b/extras/dhcpserver/dhcpserver.c index d87daab4..ad610cdd 100644 --- a/extras/dhcpserver/dhcpserver.c +++ b/extras/dhcpserver/dhcpserver.c @@ -14,18 +14,21 @@ * BSD Licensed as described in the file LICENSE */ #include +#include #include #include #include #include +#include "esplibs/libmain.h" /* Grow the size of the lwip dhcp_msg struct's options field, as LWIP defaults to a 68 octet options field for its DHCP client, and most full-sized clients send us more than this. */ #define DHCP_OPTIONS_LEN 312 -#include +#include +#include _Static_assert(sizeof(struct dhcp_msg) == offsetof(struct dhcp_msg, options) + 312, "dhcp_msg_t should have extended options size"); @@ -35,13 +38,14 @@ _Static_assert(sizeof(struct dhcp_msg) == offsetof(struct dhcp_msg, options) + 3 typedef struct { uint8_t hwaddr[NETIF_MAX_HWADDR_LEN]; + uint8_t active; uint32_t expires; } dhcp_lease_t; typedef struct { struct netconn *nc; uint8_t max_leases; - ip_addr_t first_client_addr; + ip4_addr_t first_client_addr; struct netif *server_if; dhcp_lease_t *leases; /* length max_leases */ } server_state_t; @@ -68,33 +72,34 @@ static uint8_t *add_dhcp_option_bytes(uint8_t *opt, uint8_t type, void *value, u static dhcp_lease_t *find_lease_slot(uint8_t *hwaddr); /* Copy IP address as dotted decimal to 'dest', must be at least 16 bytes long */ -inline static void sprintf_ipaddr(const ip_addr_t *addr, char *dest) +inline static void sprintf_ipaddr(const ip4_addr_t *addr, char *dest) { - if(addr == NULL) + if (addr == NULL) sprintf(dest, "NULL"); else sprintf(dest, "%d.%d.%d.%d", ip4_addr1(addr), ip4_addr2(addr), ip4_addr3(addr), ip4_addr4(addr)); } -void dhcpserver_start(const ip_addr_t *first_client_addr, uint8_t max_leases) +void dhcpserver_start(const ip4_addr_t *first_client_addr, uint8_t max_leases) { /* Stop any existing running dhcpserver */ - if(dhcpserver_task_handle) + if (dhcpserver_task_handle) dhcpserver_stop(); state = malloc(sizeof(server_state_t)); state->max_leases = max_leases; state->leases = calloc(max_leases, sizeof(dhcp_lease_t)); + bzero(state->leases, max_leases * sizeof(dhcp_lease_t)); // state->server_if is assigned once the task is running - see comment in dhcpserver_task() ip_addr_copy(state->first_client_addr, *first_client_addr); - xTaskCreate(dhcpserver_task, "DHCPServer", 768, NULL, 8, &dhcpserver_task_handle); + xTaskCreate(dhcpserver_task, "DHCP Server", 448, NULL, 2, &dhcpserver_task_handle); } void dhcpserver_stop(void) { - if(dhcpserver_task_handle) { + if (dhcpserver_task_handle) { vTaskDelete(dhcpserver_task_handle); free(state); dhcpserver_task_handle = NULL; @@ -107,12 +112,13 @@ static void dhcpserver_task(void *pxParameter) state->server_if = netif_list; /* TODO: Make this configurable */ state->nc = netconn_new (NETCONN_UDP); - if(!state->nc) { + if (!state->nc) { printf("DHCP Server Error: Failed to allocate socket.\r\n"); return; } netconn_bind(state->nc, IP_ADDR_ANY, DHCP_SERVER_PORT); + netconn_bind_if (state->nc, netif_get_index(state->server_if)); while(1) { @@ -121,29 +127,32 @@ static void dhcpserver_task(void *pxParameter) /* Receive a DHCP packet */ err_t err = netconn_recv(state->nc, &netbuf); - if(err != ERR_OK) { + if (err != ERR_OK) { printf("DHCP Server Error: Failed to receive DHCP packet. err=%d\r\n", err); continue; } /* expire any leases that have passed */ uint32_t now = xTaskGetTickCount(); - for(int i = 0; i < state->max_leases; i++) { - uint32_t expires = state->leases[i].expires; - if(expires && expires < now) - state->leases[i].expires = 0; + for (int i = 0; i < state->max_leases; i++) { + if (state->leases[i].active) { + uint32_t expires = state->leases[i].expires - now; + if (expires >= 0x80000000) { + state->leases[i].active = 0; + } + } } - ip_addr_t received_ip; + ip4_addr_t received_ip; u16_t port; netconn_addr(state->nc, &received_ip, &port); - if(netbuf_len(netbuf) < offsetof(struct dhcp_msg, options)) { + if (netbuf_len(netbuf) < offsetof(struct dhcp_msg, options)) { /* too short to be a valid DHCP client message */ netbuf_delete(netbuf); continue; } - if(netbuf_len(netbuf) >= sizeof(struct dhcp_msg)) { + if (netbuf_len(netbuf) >= sizeof(struct dhcp_msg)) { printf("DHCP Server Warning: Client sent more options than we know how to parse. len=%d\r\n", netbuf_len(netbuf)); } @@ -152,18 +161,18 @@ static void dhcpserver_task(void *pxParameter) uint8_t *message_type = find_dhcp_option(&received, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN, NULL); - if(!message_type) { + if (!message_type) { printf("DHCP Server Error: No message type field found"); continue; } - printf("State dump. Message type %d\n", *message_type); - for(int i = 0; i < state->max_leases; i++) { + for (int i = 0; i < state->max_leases; i++) { dhcp_lease_t *lease = &state->leases[i]; - printf("lease slot %d expiry %d hwaddr %02x:%02x:%02x:%02x:%02x:%02x\r\n", i, lease->expires, lease->hwaddr[0], - lease->hwaddr[1], lease->hwaddr[2], lease->hwaddr[3], lease->hwaddr[4], - lease->hwaddr[5]); + printf("lease slot %d active %d expiry %d hwaddr %02x:%02x:%02x:%02x:%02x:%02x\r\n", i, + lease->active, lease->expires - now, + lease->hwaddr[0], lease->hwaddr[1], lease->hwaddr[2], + lease->hwaddr[3], lease->hwaddr[4], lease->hwaddr[5]); } switch(*message_type) { @@ -184,13 +193,13 @@ static void dhcpserver_task(void *pxParameter) static void handle_dhcp_discover(struct dhcp_msg *dhcpmsg) { - if(dhcpmsg->htype != DHCP_HTYPE_ETH) + if (dhcpmsg->htype != DHCP_HTYPE_ETH) return; - if(dhcpmsg->hlen > NETIF_MAX_HWADDR_LEN) + if (dhcpmsg->hlen > NETIF_MAX_HWADDR_LEN) return; dhcp_lease_t *freelease = find_lease_slot(dhcpmsg->chaddr); - if(!freelease) { + if (!freelease) { printf("DHCP Server: All leases taken.\r\n"); return; /* Nothing available, so do nothing */ } @@ -199,8 +208,7 @@ static void handle_dhcp_discover(struct dhcp_msg *dhcpmsg) dhcpmsg->op = DHCP_BOOTREPLY; bzero(dhcpmsg->options, DHCP_OPTIONS_LEN); - ip_addr_copy(dhcpmsg->yiaddr, state->first_client_addr); - ip4_addr4(&(dhcpmsg->yiaddr)) += (freelease - state->leases); + dhcpmsg->yiaddr.addr = lwip_htonl(lwip_ntohl(state->first_client_addr.addr) + (freelease - state->leases)); uint8_t *opt = (uint8_t *)&dhcpmsg->options; opt = add_dhcp_option_byte(opt, DHCP_OPTION_MESSAGE_TYPE, DHCP_OFFER); @@ -218,16 +226,16 @@ static void handle_dhcp_discover(struct dhcp_msg *dhcpmsg) static void handle_dhcp_request(struct dhcp_msg *dhcpmsg) { static char ipbuf[16]; - if(dhcpmsg->htype != DHCP_HTYPE_ETH) + if (dhcpmsg->htype != DHCP_HTYPE_ETH) return; - if(dhcpmsg->hlen > NETIF_MAX_HWADDR_LEN) + if (dhcpmsg->hlen > NETIF_MAX_HWADDR_LEN) return; - ip_addr_t requested_ip; + ip4_addr_t requested_ip; uint8_t *requested_ip_opt = find_dhcp_option(dhcpmsg, DHCP_OPTION_REQUESTED_IP, 4, NULL); - if(requested_ip_opt) { - memcpy(&requested_ip.addr, requested_ip_opt, 4); - } else if(ip_addr_cmp(&requested_ip, IP_ADDR_ANY)) { + if (requested_ip_opt) { + memcpy(&requested_ip.addr, requested_ip_opt, 4); + } else if (ip_addr_cmp(&requested_ip, IP_ADDR_ANY)) { ip_addr_copy(requested_ip, dhcpmsg->ciaddr); } else { printf("DHCP Server Error: No requested IP\r\n"); @@ -236,7 +244,7 @@ static void handle_dhcp_request(struct dhcp_msg *dhcpmsg) } /* Test the first 4 octets match */ - if(ip4_addr1(&requested_ip) != ip4_addr1(&state->first_client_addr) + if (ip4_addr1(&requested_ip) != ip4_addr1(&state->first_client_addr) || ip4_addr2(&requested_ip) != ip4_addr2(&state->first_client_addr) || ip4_addr3(&requested_ip) != ip4_addr3(&state->first_client_addr)) { sprintf_ipaddr(&requested_ip, ipbuf); @@ -246,14 +254,14 @@ static void handle_dhcp_request(struct dhcp_msg *dhcpmsg) } /* Test the last octet is in the MAXCLIENTS range */ int16_t octet_offs = ip4_addr4(&requested_ip) - ip4_addr4(&state->first_client_addr); - if(octet_offs < 0 || octet_offs >= state->max_leases) { + if (octet_offs < 0 || octet_offs >= state->max_leases) { printf("DHCP Server Error: Address out of range\r\n"); send_dhcp_nak(dhcpmsg); return; } dhcp_lease_t *requested_lease = state->leases + octet_offs; - if(requested_lease->expires != 0 && memcmp(requested_lease->hwaddr, dhcpmsg->chaddr,dhcpmsg->hlen)) + if (requested_lease->active && memcmp(requested_lease->hwaddr, dhcpmsg->chaddr,dhcpmsg->hlen)) { printf("DHCP Server Error: Lease for address already taken\r\n"); send_dhcp_nak(dhcpmsg); @@ -265,7 +273,11 @@ static void handle_dhcp_request(struct dhcp_msg *dhcpmsg) printf("DHCP lease addr %s assigned to MAC %02x:%02x:%02x:%02x:%02x:%02x\r\n", ipbuf, requested_lease->hwaddr[0], requested_lease->hwaddr[1], requested_lease->hwaddr[2], requested_lease->hwaddr[3], requested_lease->hwaddr[4], requested_lease->hwaddr[5]); - requested_lease->expires = DHCPSERVER_LEASE_TIME * configTICK_RATE_HZ; + uint32_t now = xTaskGetTickCount(); + requested_lease->expires = now + DHCPSERVER_LEASE_TIME * configTICK_RATE_HZ; + requested_lease->active = 1; + + sdk_wifi_softap_set_station_info(requested_lease->hwaddr, &requested_ip); /* Reuse the REQUEST message as the ACK message */ dhcpmsg->op = DHCP_BOOTREPLY; @@ -291,7 +303,8 @@ static void handle_dhcp_request(struct dhcp_msg *dhcpmsg) static void handle_dhcp_release(struct dhcp_msg *dhcpmsg) { dhcp_lease_t *lease = find_lease_slot(dhcpmsg->chaddr); - if(lease) { + if (lease) { + lease->active = 0; lease->expires = 0; } } @@ -319,17 +332,17 @@ static uint8_t *find_dhcp_option(struct dhcp_msg *msg, uint8_t option_num, uint8 uint8_t *start = (uint8_t *)&msg->options; uint8_t *msg_end = (uint8_t *)msg + sizeof(struct dhcp_msg); - for(uint8_t *p = start; p < msg_end-2;) { + for (uint8_t *p = start; p < msg_end-2;) { uint8_t type = *p++; uint8_t len = *p++; - if(type == DHCP_OPTION_END) + if (type == DHCP_OPTION_END) return NULL; - if(p+len >= msg_end) + if (p+len >= msg_end) break; /* We've overrun our valid DHCP message size, or this isn't a valid option */ - if(type == option_num) { - if(len < min_length) + if (type == option_num) { + if (len < min_length) break; - if(length) + if (length) *length = len; return p; /* start of actual option data */ } @@ -349,7 +362,7 @@ static uint8_t *add_dhcp_option_byte(uint8_t *opt, uint8_t type, uint8_t value) static uint8_t *add_dhcp_option_bytes(uint8_t *opt, uint8_t type, void *value, uint8_t len) { *opt++ = type; - if(len) { + if (len) { *opt++ = len; memcpy(opt, value, len); } @@ -360,8 +373,8 @@ static uint8_t *add_dhcp_option_bytes(uint8_t *opt, uint8_t type, void *value, u static dhcp_lease_t *find_lease_slot(uint8_t *hwaddr) { dhcp_lease_t *empty_lease = NULL; - for(int i = 0; i < state->max_leases; i++) { - if(state->leases[i].expires == 0 && !empty_lease) + for (int i = 0; i < state->max_leases; i++) { + if (!state->leases[i].active && !empty_lease) empty_lease = &state->leases[i]; else if (memcmp(hwaddr, state->leases[i].hwaddr, 6) == 0) return &state->leases[i]; diff --git a/extras/dhcpserver/include/dhcpserver.h b/extras/dhcpserver/include/dhcpserver.h index 95a59b88..9dffa9b6 100644 --- a/extras/dhcpserver/include/dhcpserver.h +++ b/extras/dhcpserver/include/dhcpserver.h @@ -26,9 +26,9 @@ extern "C" { to a client. Subsequent lease addresses are calculated by incrementing the final octet of the IPv4 address, up to max_leases. */ -void dhcpserver_start(const ip_addr_t *first_client_addr, uint8_t max_leases); +void dhcpserver_start(const ip4_addr_t *first_client_addr, uint8_t max_leases); -void dhcpserver_get_lease(const ip_addr_t *first_client_addr, uint8_t max_leases); +void dhcpserver_get_lease(const ip4_addr_t *first_client_addr, uint8_t max_leases); /* Stop DHCP server. */ diff --git a/extras/httpd/httpd.c b/extras/httpd/httpd.c index f647ca69..253bc131 100644 --- a/extras/httpd/httpd.c +++ b/extras/httpd/httpd.c @@ -2675,7 +2675,7 @@ http_accept(void *arg, struct tcp_pcb *pcb, err_t err) * Initialize the httpd with the specified local address. */ static void -httpd_init_addr(ip_addr_t *local_addr) +httpd_init_addr(const ip_addr_t *local_addr) { struct tcp_pcb *pcb; err_t err; diff --git a/extras/sntp/sntp.c b/extras/sntp/sntp.c index d69b60e9..c034df25 100644 --- a/extras/sntp/sntp.c +++ b/extras/sntp/sntp.c @@ -38,7 +38,7 @@ #include "sntp.h" -#include "lwip/timers.h" +#include "lwip/timeouts.h" #include "lwip/udp.h" #include "lwip/dns.h" #include "lwip/ip_addr.h" @@ -136,12 +136,12 @@ #define SNTP_STARTUP_DELAY 0 #endif -/** SNTP receive timeout - in milliseconds +/** SNTP receive timeout - in seconds * Also used as retry timeout - this shouldn't be too low. * Default is 3 seconds. */ #ifndef SNTP_RECV_TIMEOUT -#define SNTP_RECV_TIMEOUT 3000 +#define SNTP_RECV_TIMEOUT 3 #endif /** SNTP update delay - in milliseconds @@ -384,8 +384,8 @@ sntp_request(void *arg) /* bind to local address */ if (lwip_bind(sock, (struct sockaddr *)&local, sizeof(local)) == 0) { /* set recv timeout */ - timeout = SNTP_RECV_TIMEOUT; - lwip_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)); + const struct timeval timeout = { SNTP_RECV_TIMEOUT, 0 }; + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); /* prepare SNTP request */ sntp_initialize_request(&sntpmsg); @@ -511,7 +511,7 @@ sntp_try_next_server(void* arg) /** UDP recv callback for the sntp pcb */ static void -sntp_recv(void *arg, struct udp_pcb* pcb, struct pbuf *p, ip_addr_t *addr, u16_t port) +sntp_recv(void *arg, struct udp_pcb* pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) { u8_t mode; u8_t stratum; @@ -597,7 +597,7 @@ sntp_recv(void *arg, struct udp_pcb* pcb, struct pbuf *p, ip_addr_t *addr, u16_t * @param server_addr resolved IP address of the SNTP server */ static void -sntp_send_request(ip_addr_t *server_addr) +sntp_send_request(const ip_addr_t *server_addr) { struct pbuf* p; p = pbuf_alloc(PBUF_TRANSPORT, SNTP_MSG_LEN, PBUF_RAM); @@ -611,7 +611,7 @@ sntp_send_request(ip_addr_t *server_addr) pbuf_free(p); /* set up receive timeout: try next server or retry on timeout */ - sys_timeout((u32_t)SNTP_RECV_TIMEOUT, sntp_try_next_server, NULL); + sys_timeout((u32_t)SNTP_RECV_TIMEOUT * 1000, sntp_try_next_server, NULL); #if SNTP_CHECK_RESPONSE >= 1 /* save server address to verify it in sntp_recv */ ip_addr_set(&sntp_last_server_address, server_addr); @@ -629,7 +629,7 @@ sntp_send_request(ip_addr_t *server_addr) * DNS found callback when using DNS names as server address. */ static void -sntp_dns_found(const char* hostname, ip_addr_t *ipaddr, void *arg) +sntp_dns_found(const char* hostname, const ip_addr_t *ipaddr, void *arg) { LWIP_UNUSED_ARG(hostname); LWIP_UNUSED_ARG(arg); diff --git a/include/espressif/esp_wifi.h b/include/espressif/esp_wifi.h index 5d9ba19f..89329747 100644 --- a/include/espressif/esp_wifi.h +++ b/include/espressif/esp_wifi.h @@ -44,9 +44,9 @@ enum { }; struct ip_info { - struct ip_addr ip; - struct ip_addr netmask; - struct ip_addr gw; + struct ip4_addr ip; + struct ip4_addr netmask; + struct ip4_addr gw; }; bool sdk_wifi_get_ip_info(uint8_t if_index, struct ip_info *info); diff --git a/include/espressif/sdk_private.h b/include/espressif/sdk_private.h index fa643a7e..3e5118f8 100644 --- a/include/espressif/sdk_private.h +++ b/include/espressif/sdk_private.h @@ -20,7 +20,7 @@ extern "C" { #endif -struct ip_addr; +struct ip4_addr; /********************************************* * Defined in libmain.a @@ -45,7 +45,7 @@ void sdk_os_putc(char c); /* Called when an IP gets set on the "station" (client) interface. */ -void sdk_system_station_got_ip_set(struct ip_addr *ip_addr, struct ip_addr *sn_mask, struct ip_addr *gw_addr); +void sdk_system_station_got_ip_set(struct ip4_addr *ip_addr, struct ip4_addr *sn_mask, struct ip4_addr *gw_addr); /* This is a no-op wrapper around ppRecycleRxPkt, which is defined in libpp.a diff --git a/lib/libnet80211.a b/lib/libnet80211.a index 4a36331b360d0318ab80208b2ae8ddd112629e75..dbefac8d898d4624463eec0882d13a22d02bcb8d 100644 GIT binary patch delta 197 zcmew|oBPXb?hWzt<8 diff --git a/lwip/component.mk b/lwip/component.mk index 1bbe21cf..b745a653 100644 --- a/lwip/component.mk +++ b/lwip/component.mk @@ -7,9 +7,6 @@ INC_DIRS += $(LWIP_DIR)include $(ROOT)lwip/include $(lwip_ROOT)include $(LWIP_DI lwip_INC_DIR = # all in INC_DIRS, needed for normal operation lwip_SRC_DIR = $(lwip_ROOT) $(LWIP_DIR)api $(LWIP_DIR)core $(LWIP_DIR)core/ipv4 $(LWIP_DIR)netif -# LWIP 1.4.1 generates a single warning so we need to disable -Werror when building it -lwip_CFLAGS = $(CFLAGS) -Wno-address - $(eval $(call component_compile_rules,lwip)) # Helpful error if git submodule not initialised diff --git a/lwip/esp_interface.c b/lwip/esp_interface.c index 6f37b5af..8e764a39 100644 --- a/lwip/esp_interface.c +++ b/lwip/esp_interface.c @@ -54,10 +54,16 @@ low_level_output(struct netif *netif, struct pbuf *p) { struct pbuf *q; - for(q = p; q != NULL; q = q->next) { - sdk_ieee80211_output_pbuf(netif, q); + if (p->next) { + q = pbuf_clone(PBUF_RAW, PBUF_RAM, p); + if (q == NULL) { + return ERR_MEM; + } + p = q; } + sdk_ieee80211_output_pbuf(netif, p); + LINK_STATS_INC(link.xmit); return ERR_OK; @@ -88,7 +94,7 @@ err_t ethernetif_init(struct netif *netif) /* low_level_init components */ netif->hwaddr_len = 6; - /* hwaddr seems to be set elsewhere, or (more likely) is set on tx by MAC layer */ + /* The hwaddr is set by sdk_wifi_station_start or sdk_wifi_softap_start. */ netif->mtu = 1500; netif->flags = NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP; @@ -122,3 +128,25 @@ void ethernetif_input(struct netif *netif, struct pbuf *p) break; } } + +/* Since the pbuf_type definition has changed in lwip v2 and it is used by the + * sdk when calling pbuf_alloc, the SDK libraries have been modified to rename + * their references to pbuf_alloc to _pbufalloc allowing the pbuf_type to be + * rewritten here. Doing this here keeps this hack out of the lwip code, and + * ensures that this re-writing is only applied to the sdk calls to pbuf_alloc. + * + * The only pbuf types used by the SDK are type 0 for PBUF_RAM when writing + * data, and type 2 for the received data. The receive data path references + * internal buffer objects that need to be freed with custom code so a custom + * pbuf allocation type is used for these. + */ +struct pbuf *_pbufalloc(pbuf_layer l, u16_t length, pbuf_type type) { + if (type == 0) { + return pbuf_alloc(l, length, PBUF_RAM); + } else if (type == 2) { + return pbuf_alloc_reference(NULL, length, PBUF_ALLOC_FLAG_RX | PBUF_TYPE_ALLOC_SRC_MASK_ESP_RX); + } else { + LWIP_ASSERT("Unexpected pbuf_alloc type", 0); + for (;;); + } +} diff --git a/lwip/include/arch/cc.h b/lwip/include/arch/cc.h index 2922a547..42a57d00 100644 --- a/lwip/include/arch/cc.h +++ b/lwip/include/arch/cc.h @@ -52,17 +52,6 @@ #define ERRNO -#define BYTE_ORDER LITTLE_ENDIAN - -/** @todo fix some warnings: don't use #pragma if compiling with cygwin gcc */ -#ifndef __GNUC__ - #include - #pragma warning (disable: 4244) /* disable conversion warning (implicit integer promotion!) */ - #pragma warning (disable: 4127) /* conditional expression is constant */ - #pragma warning (disable: 4996) /* 'strncpy' was declared deprecated */ - #pragma warning (disable: 4103) /* structure packing changed by including file */ -#endif - /* Define generic types used in lwIP */ typedef uint8_t u8_t; typedef int8_t s8_t; @@ -112,4 +101,6 @@ typedef int sys_prot_t; #define LWIP_PLATFORM_HTONS(_n) ((u16_t)((((_n) & 0xff) << 8) | (((_n) >> 8) & 0xff))) #define LWIP_PLATFORM_HTONL(_n) ((u32_t)( (((_n) & 0xff) << 24) | (((_n) & 0xff00) << 8) | (((_n) >> 8) & 0xff00) | (((_n) >> 24) & 0xff) )) +#define LWIP_RAND() rand() + #endif /* __ARCH_CC_H__ */ diff --git a/lwip/include/lwipopts.h b/lwip/include/lwipopts.h index 8f46c7d8..2f877f58 100644 --- a/lwip/include/lwipopts.h +++ b/lwip/include/lwipopts.h @@ -32,11 +32,13 @@ #ifndef __LWIPOPTS_H__ #define __LWIPOPTS_H__ -#define LWIP_ESP 1 -#define ESP_RTOS 1 -#define PBUF_RSV_FOR_WLAN 1 -#define EBUF_LWIP 1 +#define ESP_OPEN_RTOS 1 + +/* See tcp.c tcp_alloc(). */ #define ESP_TIMEWAIT_THRESHOLD 10000 + +/** LWIP_TIMEVAL_PRIVATE: if you want to use the struct timeval provided + * by your system, set this to 0 and include in cc.h */ #define LWIP_TIMEVAL_PRIVATE 0 /* @@ -76,16 +78,20 @@ #define MEM_LIBC_MALLOC 1 /** -* MEMP_MEM_MALLOC==1: Use mem_malloc/mem_free instead of the lwip pool allocator. -* Especially useful with MEM_LIBC_MALLOC but handle with care regarding execution -* speed and usage from interrupts! -*/ + * MEMP_MEM_MALLOC==1: Use mem_malloc/mem_free instead of the lwip pool allocator. + * Especially useful with MEM_LIBC_MALLOC but handle with care regarding execution + * speed (heap alloc can be much slower than pool alloc) and usage from interrupts + * (especially if your netif driver allocates PBUF_POOL pbufs for received frames + * from interrupt)! + * ATTENTION: Currently, this uses the heap for ALL pools (also for private pools, + * not only for internal pools defined in memp_std.h)! + */ #define MEMP_MEM_MALLOC 1 /** * MEM_ALIGNMENT: should be set to the alignment of the CPU - * 4 byte alignment -> #define MEM_ALIGNMENT 4 - * 2 byte alignment -> #define MEM_ALIGNMENT 2 + * 4 byte alignment -> \#define MEM_ALIGNMENT 4 + * 2 byte alignment -> \#define MEM_ALIGNMENT 2 */ #define MEM_ALIGNMENT 4 @@ -174,12 +180,12 @@ */ /* ---------------------------------- - ---------- SNMP options ---------- + ----- SNMP MIB2 support ----- ---------------------------------- */ /* ---------------------------------- - ---------- IGMP options ---------- + ----- Multicast/IGMP options ----- ---------------------------------- */ /* @@ -193,7 +199,10 @@ */ #define LWIP_DNS 1 +/** DNS maximum number of entries to maintain locally. */ #define DNS_TABLE_SIZE 1 + +/** DNS maximum host name length supported in the name table. */ #define DNS_MAX_NAME_LENGTH 128 /* @@ -206,31 +215,31 @@ ---------- TCP options ---------- --------------------------------- */ -/** - * TCP_QUEUE_OOSEQ==1: TCP will queue segments that arrive out of order. - * Define to 0 if your device is low on memory. - */ -#define TCP_QUEUE_OOSEQ 0 - -/* - * LWIP_EVENT_API==1: The user defines lwip_tcp_event() to receive all - * events (accept, sent, etc) that happen in the system. - * LWIP_CALLBACK_API==1: The PCB callback function is called directly - * for the event. This is the default. -*/ -#define TCP_MSS 1460 - /** * TCP_MAXRTX: Maximum number of retransmissions of data segments. */ #define TCP_MAXRTX 6 - /** * TCP_SYNMAXRTX: Maximum number of retransmissions of SYN segments. */ #define TCP_SYNMAXRTX 3 +/** + * TCP_QUEUE_OOSEQ==1: TCP will queue segments that arrive out of order. + * Define to 0 if your device is low on memory. + */ +#define TCP_QUEUE_OOSEQ 0 + +/** + * TCP_MSS: TCP Maximum segment size. (default is 536, a conservative default, + * you might want to increase this.) + * For the receive side, this MSS is advertised to the remote side + * when opening a connection. For the transmit size, this MSS sets + * an upper limit on the MSS advertised by the remote host. + */ +#define TCP_MSS 1460 + /* ---------------------------------- ---------- Pbuf options ---------- @@ -243,13 +252,17 @@ ------------------------------------------------ */ /** - * LWIP_NETIF_TX_SINGLE_PBUF: if this is set to 1, lwIP tries to put all data + * LWIP_NETIF_HOSTNAME==1: use DHCP_OPTION_HOSTNAME with netif's hostname + * field. + */ +#define LWIP_NETIF_HOSTNAME 1 + +/** + * LWIP_NETIF_TX_SINGLE_PBUF: if this is set to 1, lwIP *tries* to put all data * to be sent into one single pbuf. This is for compatibility with DMA-enabled * MACs that do not support scatter-gather. * Beware that this might involve CPU-memcpy before transmitting that would not * be needed without this flag! Use this only if you need to! - * - * @todo: TCP and IP-frag do not work with this, yet: */ #define LWIP_NETIF_TX_SINGLE_PBUF 1 @@ -358,6 +371,11 @@ ---------------------------------------- */ +/** + * LWIP_STATS_DISPLAY==1: Compile in the statistics output functions. + */ +#define LWIP_STATS_DISPLAY 1 + /* --------------------------------- ---------- PPP options ---------- @@ -391,11 +409,29 @@ // Uncomment this line, and set the individual debug options you want, for IP stack debug output //#define LWIP_DEBUG +/** + * LWIP_DBG_MIN_LEVEL: After masking, the value of the debug is + * compared against this value. If it is smaller, then debugging + * messages are written. + */ +//#define LWIP_DBG_MIN_LEVEL LWIP_DBG_LEVEL_WARNING + +/** + * LWIP_DBG_TYPES_ON: A mask that can be used to globally enable/disable + * debug messages of certain types. + */ +#define LWIP_DBG_TYPES_ON LWIP_DBG_ON + /** * ETHARP_DEBUG: Enable debugging in etharp.c. */ #define ETHARP_DEBUG LWIP_DBG_OFF +/** + * NETIF_DEBUG: Enable debugging in netif.c. + */ +#define NETIF_DEBUG LWIP_DBG_OFF + /** * PBUF_DEBUG: Enable debugging in pbuf.c. */ @@ -406,50 +442,151 @@ */ #define API_LIB_DEBUG LWIP_DBG_OFF +/** + * API_MSG_DEBUG: Enable debugging in api_msg.c. + */ +#define API_MSG_DEBUG LWIP_DBG_OFF + /** * SOCKETS_DEBUG: Enable debugging in sockets.c. */ #define SOCKETS_DEBUG LWIP_DBG_OFF +/** + * ICMP_DEBUG: Enable debugging in icmp.c. + */ +#define ICMP_DEBUG LWIP_DBG_OFF + +/** + * IGMP_DEBUG: Enable debugging in igmp.c. + */ +#define IGMP_DEBUG LWIP_DBG_OFF + +/** + * INET_DEBUG: Enable debugging in inet.c. + */ +#define INET_DEBUG LWIP_DBG_OFF + /** * IP_DEBUG: Enable debugging for IP. */ #define IP_DEBUG LWIP_DBG_OFF +/** + * IP_REASS_DEBUG: Enable debugging in ip_frag.c for both frag & reass. + */ +#define IP_REASS_DEBUG LWIP_DBG_OFF + +/** + * RAW_DEBUG: Enable debugging in raw.c. + */ +#define RAW_DEBUG LWIP_DBG_OFF + +/** + * MEM_DEBUG: Enable debugging in mem.c. + */ +#define MEM_DEBUG LWIP_DBG_OFF + /** * MEMP_DEBUG: Enable debugging in memp.c. */ #define MEMP_DEBUG LWIP_DBG_OFF +/** + * SYS_DEBUG: Enable debugging in sys.c. + */ +#define SYS_DEBUG LWIP_DBG_OFF + +/** + * TIMERS_DEBUG: Enable debugging in timers.c. + */ +#define TIMERS_DEBUG LWIP_DBG_OFF + +/** + * TCP_DEBUG: Enable debugging for TCP. + */ +#define TCP_DEBUG LWIP_DBG_OFF + /** * TCP_INPUT_DEBUG: Enable debugging in tcp_in.c for incoming debug. */ #define TCP_INPUT_DEBUG LWIP_DBG_OFF +/** + * TCP_FR_DEBUG: Enable debugging in tcp_in.c for fast retransmit. + */ +#define TCP_FR_DEBUG LWIP_DBG_OFF + +/** + * TCP_RTO_DEBUG: Enable debugging in TCP for retransmit + * timeout. + */ +#define TCP_RTO_DEBUG LWIP_DBG_OFF + +/** + * TCP_CWND_DEBUG: Enable debugging for TCP congestion window. + */ +#define TCP_CWND_DEBUG LWIP_DBG_OFF + +/** + * TCP_WND_DEBUG: Enable debugging in tcp_in.c for window updating. + */ +#define TCP_WND_DEBUG LWIP_DBG_OFF + /** * TCP_OUTPUT_DEBUG: Enable debugging in tcp_out.c output functions. */ #define TCP_OUTPUT_DEBUG LWIP_DBG_OFF /** - * UDP_DEBUG: Enable debugging in udp.c. + * TCP_RST_DEBUG: Enable debugging for TCP with the RST message. + */ +#define TCP_RST_DEBUG LWIP_DBG_OFF + +/** + * TCP_QLEN_DEBUG: Enable debugging for TCP queue lengths. */ -#define UDP_DEBUG LWIP_DBG_OFF +#define TCP_QLEN_DEBUG LWIP_DBG_OFF /** - * ICMP_DEBUG: Enable debugging in udp.c. + * UDP_DEBUG: Enable debugging in UDP. */ -#define ICMP_DEBUG LWIP_DBG_OFF +#define UDP_DEBUG LWIP_DBG_OFF /** * TCPIP_DEBUG: Enable debugging in tcpip.c. */ #define TCPIP_DEBUG LWIP_DBG_OFF +/** + * SLIP_DEBUG: Enable debugging in slipif.c. + */ +#define SLIP_DEBUG LWIP_DBG_OFF /** * DHCP_DEBUG: Enable debugging in dhcp.c. */ #define DHCP_DEBUG LWIP_DBG_OFF +/** + * AUTOIP_DEBUG: Enable debugging in autoip.c. + */ +#define AUTOIP_DEBUG LWIP_DBG_OFF + +/** + * DNS_DEBUG: Enable debugging for DNS. + */ +#define DNS_DEBUG LWIP_DBG_OFF + +/** + * IP6_DEBUG: Enable debugging for IPv6. + */ +#define IP6_DEBUG LWIP_DBG_OFF + +/* + -------------------------------------------------- + ---------- Performance tracking options ---------- + -------------------------------------------------- +*/ + #endif /* __LWIPOPTS_H__ */ diff --git a/lwip/lwip b/lwip/lwip index 3cf8d514..56384c26 160000 --- a/lwip/lwip +++ b/lwip/lwip @@ -1 +1 @@ -Subproject commit 3cf8d514bd76e6ef77e6fa514d0ec6d96da7fd9a +Subproject commit 56384c26d96757b8cdf5e6fd4d886197210741d8 diff --git a/open_esplibs/include/esplibs/libmain.h b/open_esplibs/include/esplibs/libmain.h index 9f4ad1ac..86e2dc95 100644 --- a/open_esplibs/include/esplibs/libmain.h +++ b/open_esplibs/include/esplibs/libmain.h @@ -56,6 +56,7 @@ uint32_t sdk_system_relative_time(uint32_t reltime); uint32_t sdk_system_get_checksum(uint8_t *, uint32_t); void sdk_wifi_softap_cacl_mac(uint8_t *, uint8_t *); void sdk_wifi_softap_set_default_ssid(void); +bool sdk_wifi_softap_set_station_info(const uint8_t *hwaddr, ip_addr_t *); // xtensa_context.o diff --git a/open_esplibs/include/esplibs/libnet80211.h b/open_esplibs/include/esplibs/libnet80211.h index b7a2db6f..d2c117b6 100644 --- a/open_esplibs/include/esplibs/libnet80211.h +++ b/open_esplibs/include/esplibs/libnet80211.h @@ -32,7 +32,7 @@ struct esf_buf *sdk_ieee80211_getmgtframe(void **arg0, uint32_t arg1, uint32_t a extern uint8_t sdk_TmpSTAAPCloseAP; extern uint8_t sdk_PendFreeBcnEb; void sdk_ieee80211_hostap_attach(struct sdk_g_ic_st *); -void sdk_hostap_handle_timer(struct sdk_netif_conninfo *cnx_node); +void sdk_hostap_handle_timer(struct sdk_cnx_node *cnx_node); bool sdk_wifi_softap_start(); bool sdk_wifi_softap_stop(); @@ -85,10 +85,10 @@ int sdk_chm_check_same_channel(); extern ETSTimer sdk_sta_con_timer; extern void *sdk_g_cnx_probe_rc_list_cb; void sdk_cnx_sta_leave(struct sdk_g_ic_netif_info *netif_info, void *); -void *sdk_cnx_node_search(uint8_t mac[6]); -void sdk_cnx_node_leave(struct sdk_g_ic_netif_info *netif, struct sdk_netif_conninfo *conn); +struct sdk_cnx_node *sdk_cnx_node_search(uint8_t mac[6]); +void sdk_cnx_node_leave(struct sdk_g_ic_netif_info *netif, struct sdk_cnx_node *conn); void sdk_cnx_rc_update_state_metric(void *, int, int); -void sdk_cnx_remove_rc(void *); +void sdk_cnx_node_remove(struct sdk_cnx_node *cnx_node); void sdk_cnx_attach(struct sdk_g_ic_st *); #endif /* _ESPLIBS_LIBNET80211_H */ diff --git a/open_esplibs/include/esplibs/libpp.h b/open_esplibs/include/esplibs/libpp.h index c6185b62..836a6896 100644 --- a/open_esplibs/include/esplibs/libpp.h +++ b/open_esplibs/include/esplibs/libpp.h @@ -23,6 +23,7 @@ extern uint8_t sdk_interface_mask; void sdk_ic_set_vif(int, int, uint8_t (*)[6], int, int); void sdk_ic_bss_info_update(int, uint8_t (*hwaddr)[], int, int); void sdk_ic_set_sta(int, int, void *, int, int, int, int, int); +void sdk_ic_remove_key(uint32_t); // lmac.o extern uint32_t sdk_lmacConfMib; diff --git a/open_esplibs/include/esplibs/libwpa.h b/open_esplibs/include/esplibs/libwpa.h index 36a2fc28..2839e56f 100644 --- a/open_esplibs/include/esplibs/libwpa.h +++ b/open_esplibs/include/esplibs/libwpa.h @@ -53,6 +53,7 @@ int sdk_os_get_random(uint8_t *dst, uint32_t size); // wpa_auth.o uint32_t *sdk_wpa_init(uint8_t (*hwaddr)[], struct _unknown_wpa1 *, int); +void sdk_wpa_auth_sta_deinit(void *); // wpabuf.o diff --git a/open_esplibs/libmain/user_interface.c b/open_esplibs/libmain/user_interface.c index eed34a10..e14d4fd5 100644 --- a/open_esplibs/libmain/user_interface.c +++ b/open_esplibs/libmain/user_interface.c @@ -471,8 +471,7 @@ uint32_t sdk_system_relative_time(uint32_t reltime) { return WDEV.SYS_TIME - reltime; } -// Change arg types to ip4_addr for lwip v2. -void sdk_system_station_got_ip_set(struct ip_addr *ip, struct ip_addr *mask, struct ip_addr *gw) { +void sdk_system_station_got_ip_set(struct ip4_addr *ip, struct ip4_addr *mask, struct ip4_addr *gw) { uint8_t *ip_bytes = (uint8_t *)&ip->addr; uint8_t *mask_bytes = (uint8_t *)&mask->addr; uint8_t *gw_bytes = (uint8_t *)&gw->addr; diff --git a/open_esplibs/libnet80211/ieee80211_hostap.c b/open_esplibs/libnet80211/ieee80211_hostap.c index fbe7f490..f3f36d7d 100644 --- a/open_esplibs/libnet80211/ieee80211_hostap.c +++ b/open_esplibs/libnet80211/ieee80211_hostap.c @@ -7,4 +7,307 @@ #if OPEN_LIBNET80211_HOSTAP // The contents of this file are only built if OPEN_LIBNET80211_HOSTAP is set to true +#include +#include "tcpip.h" +#include "espressif/esp_wifi.h" +#include "espressif/esp_misc.h" +#include "etstimer.h" +#include "esplibs/libmain.h" +#include "esplibs/libnet80211.h" +#include "esplibs/libpp.h" +#include "esplibs/libwpa.h" + +static uint8_t hostap_flags = 0; +static ETSTimer hostap_timer; +static struct esf_buf *hostap_timer_parg = NULL; + +void IRAM *zalloc(size_t nbytes); + +static void IRAM hostap_timer_func(struct esf_buf *esf_buf) { + struct sdk_cnx_node *cnx_node = sdk_g_ic.v.softap_netif_info->cnx_nodes[0]; + int32_t mode = sdk_wifi_get_phy_mode(); + uint8_t *frame = esf_buf->frame; + *(uint16_t *)(frame + 22) = (cnx_node->_unknown9c - 1) << 4; + + cnx_node->_unknown9c += 1; + + if (sdk_g_ic.s.wifi_led_enable) { + uint32_t gpio = sdk_g_ic.s.wifi_led_gpio; + uint32_t state = sdk_g_ic.s.wifi_led_state; + sdk_gpio_output_set(state << gpio, (((state & 1) == 0) ? 1 : 0) << gpio, + 1 << gpio, 0); + sdk_g_ic.s.wifi_led_state = (state & 1) ? 0 : 1; + } + + uint8_t *frame2 = frame + sdk_g_ic.s._unknown288 + (mode == 1 ? 23 : 27) ; + memcpy(frame2 + 29, &sdk_g_ic.v._unknown1d2, 1); + + uint32_t v1 = frame2[26]; + if (v1 == 0) { + v1 = frame2[27]; + } + frame2[26] = v1 - 1; + + uint32_t v2 = sdk_ieee80211_chan2ieee(sdk_g_ic.v._unknown14c); + frame2[23] = v2; + sdk_g_ic.s._unknown30d = v2; + + int32_t v3 = *((volatile int32_t *)0x3ff20c00); // mactime + *(uint32_t *)(esf_buf->extra + 16) = v3; + *(uint32_t *)(frame + 24) = v3; + *(uint32_t *)(frame + 28) = 0; + + if (sdk_chm_check_same_channel()) { + hostap_flags |= 1; + sdk_ppTxPkt(esf_buf); + return; + } + sdk_ets_timer_disarm(&hostap_timer); + sdk_ets_timer_arm(&hostap_timer, sdk_wDev_Get_Next_TBTT(), 0); +} + +static void IRAM hostap_tx_callback() { + uint32_t flags = hostap_flags & 0xfe; + + if (sdk_TmpSTAAPCloseAP == 0) { + hostap_flags = flags; + sdk_ets_timer_disarm(&hostap_timer); + sdk_ets_timer_arm(&hostap_timer, sdk_wDev_Get_Next_TBTT(), 0); + return; + } + + sdk_PendFreeBcnEb = 1; + + if (flags & 2) { + hostap_flags = flags & 0xfd; + sdk_wifi_softap_start(); + return; + } + + hostap_flags = flags; +} + +static void hostap_attach_misc() { + struct sdk_g_ic_netif_info *netif_info = sdk_g_ic.v.softap_netif_info; + + struct _unknown_softap1 *ptr1 = zalloc(28); // 0x1c + netif_info->_unknownb4 = ptr1; + + struct _unknown_softap2 *ptr2 = zalloc(204); // 0xcc + ptr1->_unknown04 = ptr2; + + struct _unknown_wpa1 *ptr3 = zalloc(76); // 0x4c + + uint32_t v1 = sdk_g_ic.s._unknown30e; + if (v1 == 2) { + ptr3->_unknown00 = 1; + } else if (v1 == 3) { + ptr3->_unknown00 = 2; + } else if (v1 == 4) { + ptr3->_unknown00 = 3; + } + + ptr3->_unknown28 = 2; + ptr3->_unknown04 = 2; + ptr3->_unknown0c = 2; + ptr3->_unknown08 = 10; + ptr3->_unknown20 = 10; + + int s1 = (sdk_g_ic.s._unknown28a << 16) | sdk_g_ic.s._unknown288; + memcpy(&ptr2->_unknown10, &sdk_g_ic.s._unknown28c, s1); + ptr2->_unknown30 = s1; + + uint8_t *ptr4 = zalloc(64); // 0x40 + ptr2->_unknown3c = ptr4; + + char *str1 = sdk_g_ic.s._unknown2ac; + memcpy(ptr4, str1, strlen(str1)); + + struct sdk_cnx_node *cnx_node = netif_info->cnx_nodes[0]; + + ptr2->_unknownb4 = 300; + + netif_info->_unknown4c = 3; + netif_info->_unknown48 |= 16; + + cnx_node->_unknown08 |= 1; + + sdk_hostapd_setup_wpa_psk(ptr2); + + struct netif *netif = netif_info->netif; + + ptr1->_unknown18 = sdk_wpa_init(&netif->hwaddr, ptr3, 0); + + free(ptr3); +} + +static void softap_stop_free() { + struct sdk_g_ic_netif_info *netif_info = sdk_g_ic.v.softap_netif_info; + netif_info->_unknown4c = 0; + netif_info->_unknown48 &= 0xffffffef; + netif_info->cnx_nodes[0]->_unknown08 = 0; + + struct _unknown_softap1 *unkb4 = netif_info->_unknownb4; + if (!unkb4) return; + + uint32_t *unk18 = unkb4->_unknown18; + if (unk18) { + uint32_t *ptr1 = ((uint32_t **)unk18)[20]; + if (ptr1) + free(ptr1); + + uint32_t *ptr2 = *(uint32_t **)unk18; + if (ptr2) + free(ptr2); + + free(unk18); + } + + struct _unknown_softap2 *unk04 = unkb4->_unknown04; + if (unk04) { + uint32_t *unk38 = unk04->_unknown38; + if (unk38) + free(unk38); + + uint8_t *unk3c = unk04->_unknown3c; + if (unk3c) + free(unk3c); + + free(unk04); + } + + free(unkb4); + netif_info->_unknownb4 = NULL; +} + +void sdk_ieee80211_hostap_attach(struct sdk_g_ic_st *ic) { + uint32_t scratch[12]; // ?? + struct sdk_g_ic_netif_info *netif_info = ic->v.softap_netif_info; + + uint32_t v1 = ic->s._unknown30e; + if (v1 >= 2 && v1 < 5) + hostap_attach_misc(); + + struct netif *netif = netif_info->netif; + sdk_ic_bss_info_update(1, &netif->hwaddr, 2, 100); + + ic->v._unknown1d0 = 0; + netif_info->_unknown3c = 5; + + sdk_ppRegisterTxCallback(hostap_tx_callback, 4); + + hostap_timer_parg = sdk_ieee80211_beacon_alloc(netif_info, scratch); + sdk_ets_timer_disarm(&hostap_timer); + sdk_ets_timer_setfn(&hostap_timer, (ETSTimerFunc *)hostap_timer_func, hostap_timer_parg); + sdk_wDev_Reset_TBTT(); + sdk_ets_timer_arm(&hostap_timer, sdk_wDev_Get_Next_TBTT(), 0); +} + +bool sdk_wifi_softap_start() { + struct sdk_g_ic_netif_info *netif_info = sdk_g_ic.v.softap_netif_info; + if (!netif_info) return 0; + if (netif_info->started) return 1; + + uint8_t flags = hostap_flags; + if (flags & 1) { + hostap_flags = flags | 2; + return 1; + } + + uint8_t (*mac_addr)[6] = &sdk_info.softap_mac_addr; + if (!netif_info->netif) { + struct netif *netif = (struct netif *)malloc(sizeof(struct netif)); + netif_info->netif = netif; + memcpy(&netif->hwaddr, mac_addr, 6); + netif_add(netif, &sdk_info.softap_ipaddr, &sdk_info.softap_netmask, + &sdk_info.softap_gw, netif_info, ethernetif_init, tcpip_input); + } + + sdk_ic_set_vif(1, 1, mac_addr, 1, 0); + + netif_set_up(netif_info->netif); + + if (sdk_wifi_get_opmode() != 3 || + !sdk_g_ic.v.station_netif_info || + sdk_g_ic.v.station_netif_info->_unknown3c < 2) { + + uint32_t i1 = (sdk_g_ic.s._unknown30d - 1) & 0xff; + + int nmi_on = sdk_NMIIrqIsOn; + if (!nmi_on) { + vPortEnterCritical(); + + do { + DPORT.DPORT0 = DPORT.DPORT0 & 0xffffffe0; + } while (DPORT.DPORT0 & 1); + } + + // current channel? + uint32_t *chan = &sdk_g_ic.v._unknown84[i1 * 3]; + sdk_g_ic.v._unknown14c = chan; + + if (!nmi_on) { + DPORT.DPORT0 = (DPORT.DPORT0 & 0xffffffe0) | 1; + vPortExitCritical(); + } + + sdk_chm_set_current_channel(chan); + } + + sdk_ieee80211_hostap_attach(&sdk_g_ic); + sdk_TmpSTAAPCloseAP = 0; + netif_info->started = 1; + return 1; +} + +bool sdk_wifi_softap_stop() { + struct sdk_g_ic_netif_info *netif_info = sdk_g_ic.v.softap_netif_info; + + if (!netif_info) + return 0; + + if (!netif_info->started) + return 1; + + uint32_t end = sdk_g_ic.s._unknown310 + 2; + uint32_t count = 1; + + // Note this defensive test seems dead code, the value is loaded + // as a uint8_t value so adding 2 ensures this test always passes. + if (end >= 2) { + do { + struct sdk_cnx_node *cnx_node = netif_info->cnx_nodes[count]; + if (cnx_node) { + struct sdk_cnx_node *cnx_node2 = netif_info->_unknown88; + netif_info->_unknown88 = cnx_node; + + sdk_ieee80211_send_mgmt(netif_info, 160, 4); + sdk_ieee80211_send_mgmt(netif_info, 192, 2); + + netif_info->_unknown88 = cnx_node2; + + sdk_cnx_node_leave(netif_info, netif_info->cnx_nodes[count]); + + // Number of entries might have changed, perhaps + // should have if one was removed above? + end = sdk_g_ic.s._unknown310 + 2; + } + count++; + } while (count < end); + } + + netif_set_down(netif_info->netif); + sdk_TmpSTAAPCloseAP = 1; + sdk_ets_timer_disarm(&hostap_timer); + sdk_ic_bss_info_update(1, &sdk_info.softap_mac_addr, 2, 0); + sdk_ic_set_vif(1, 0, NULL, 1, 0); + softap_stop_free(); + + if ((hostap_flags & 1) == 0) + sdk_esf_buf_recycle(hostap_timer_parg, 4); + + netif_info->started = 0; + return 1; +} + #endif /* OPEN_LIBNET80211_HOSTAP */ diff --git a/open_esplibs/libnet80211/ieee80211_input.c b/open_esplibs/libnet80211/ieee80211_input.c index be07a51a..d9fb4bb1 100644 --- a/open_esplibs/libnet80211/ieee80211_input.c +++ b/open_esplibs/libnet80211/ieee80211_input.c @@ -14,10 +14,9 @@ void IRAM sdk_ieee80211_deliver_data(struct sdk_g_ic_netif_info *netif_info, str if (netif->flags & NETIF_FLAG_LINK_UP) { uint16_t length = esf_buf->length; - struct pbuf *pbuf = pbuf_alloc(PBUF_RAW, length, PBUF_REF); - pbuf->payload = esf_buf->pbuf2->payload; + struct pbuf *pbuf = pbuf_alloc_reference(esf_buf->pbuf2->payload, length, PBUF_ALLOC_FLAG_RX | PBUF_TYPE_ALLOC_SRC_MASK_ESP_RX); esf_buf->pbuf1 = pbuf; - pbuf->eb = (void *)esf_buf; + pbuf->esf_buf = (void *)esf_buf; ethernetif_input(netif, pbuf); return; } diff --git a/open_esplibs/libnet80211/wl_cnx.c b/open_esplibs/libnet80211/wl_cnx.c index 3b7d7093..4fd5e188 100644 --- a/open_esplibs/libnet80211/wl_cnx.c +++ b/open_esplibs/libnet80211/wl_cnx.c @@ -9,12 +9,26 @@ #include "espressif/esp_misc.h" #include "esplibs/libnet80211.h" +#include "esplibs/libpp.h" +#include "esplibs/libwpa.h" #include #include "lwip/dhcp.h" ETSTimer sdk_sta_con_timer; void *sdk_g_cnx_probe_rc_list_cb; +/* + * Called from the ESP sdk_cnx_sta_leave function. Split out via a hack to the + * binary library to allow modification to track changes to lwip, for example + * changes to the offset of the netif->flags removal of the NETIF_FLAG_DHCP flag + * lwip v2 etc. + */ +void dhcp_if_down(struct netif *netif) +{ + dhcp_release_and_stop(netif); + netif_set_down(netif); +} + #if 0 // Most of the code in this file assesses static data so it will be all or none. @@ -24,7 +38,6 @@ static uint8_t Ldata004; static uint32_t Ldata006; static void *Ldate007; -// Use of the netif->flags and the NETIF_FLAG_DHCP flag removed in lwip v2. void sdk_cnx_sta_leave(struct sdk_g_ic_netif_info *netif_info, void *arg1) { struct netif *netif = netif_info->netif; @@ -32,13 +45,9 @@ void sdk_cnx_sta_leave(struct sdk_g_ic_netif_info *netif_info, void *arg1) { uint16_t v1 = *(uint16_t *)(arg1 + 0x1a) & 0xfff; sdk_ic_set_sta(0, 0, arg1, 0, v1, phy_type, 0, 0); - netif_set_down(netif); - - // The NETIF_FLAG_DHCP flags is removed in lwip v2? - if (netif->flags & 0x8) { - dhcp_release(netif); - dhcp_stop(netif); - } + // Note the SDK binary was modified here as it made use of the + // netif flags which changed in lwip v2. + dhcp_if_down(netif); uint32_t v2 = *(uint8_t *)(arg1 + 0xe8); free(sdk_g_ic.v._unknown190[v2]); @@ -77,7 +86,30 @@ void sdk_cnx_sta_leave(struct sdk_g_ic_netif_info *netif_info, void *arg1) { } #endif -void IRAM *sdk_cnx_node_search(uint8_t mac[6]) +void sdk_cnx_node_remove(struct sdk_cnx_node *cnx_node) { + const uint32_t num = sdk_g_ic.s._unknown310 + 2; + if ((int32_t)num < (int32_t)2) { + return; + } + + struct sdk_g_ic_netif_info *netif_info = sdk_g_ic.v.softap_netif_info; + uint32_t i = 1; + do { + if (netif_info->cnx_nodes[i] == cnx_node) { + uint32_t v2 = cnx_node->_unknowne8; + sdk_ic_remove_key(v2 + 2); + sdk_wpa_auth_sta_deinit(cnx_node->_unknowne4); + free(sdk_g_ic.v._unknown190[v2]); + sdk_g_ic.v._unknown190[v2] = NULL; + free(cnx_node); + netif_info->cnx_nodes[i] = NULL; + return; + } + i += 1; + } while (i < num); +} + +struct sdk_cnx_node *sdk_cnx_node_search(uint8_t mac[6]) { int end = sdk_g_ic.s._unknown310 + 2; @@ -86,15 +118,15 @@ void IRAM *sdk_cnx_node_search(uint8_t mac[6]) if (end < 1) return NULL; - struct sdk_netif_conninfo **conninfo = sdk_g_ic.v.softap_netif_info->conninfo; + struct sdk_cnx_node **cnx_nodes = sdk_g_ic.v.softap_netif_info->cnx_nodes; int i = 0; do { - struct sdk_netif_conninfo *info = conninfo[i]; + struct sdk_cnx_node *cnx_node = cnx_nodes[i]; - if (info) { - if (memcmp(mac, info->mac_addr, 6) == 0) { - return info; + if (cnx_node) { + if (memcmp(mac, cnx_node->mac_addr, 6) == 0) { + return cnx_node; } } i++; diff --git a/open_esplibs/libwpa/wpa_main.c b/open_esplibs/libwpa/wpa_main.c index 1c70516e..394eac80 100644 --- a/open_esplibs/libwpa/wpa_main.c +++ b/open_esplibs/libwpa/wpa_main.c @@ -72,12 +72,12 @@ void sdk_dhcp_bind_check() { void sdk_eagle_auth_done() { struct sdk_g_ic_netif_info *netif_info = sdk_g_ic.v.station_netif_info; struct netif *netif = netif_info->netif; - struct sdk_netif_conninfo *conninfo = netif_info->_unknown88; + struct sdk_cnx_node *cnx_node = netif_info->_unknown88; - if (conninfo->_unknown08 & 1) + if (cnx_node->_unknown08 & 1) return; - uint32_t channel = conninfo->_unknown78->channel; + uint32_t channel = cnx_node->_unknown78->channel; char *ssid = (char *)sdk_g_ic.s._unknown1e4.sta_ssid; printf("\nconnected with %s, channel %d\n", ssid, channel); @@ -89,15 +89,13 @@ void sdk_eagle_auth_done() { sdk_os_timer_arm(timer, 15000, 0); netif_info->statusb9 = 0; - conninfo->_unknown18 = 0; - conninfo->_unknown08 |= 1; + cnx_node->_unknown18 = 0; + cnx_node->_unknown08 |= 1; - // TODO lwip v2 removed the NETIF_FLAG_DHCP flag. - if (netif->flags & 0x08) // NETIF_FLAG_DHCP + if (dhcp_supplied_address(netif)) return; - // lwip v2: if (ip4_addr_isany_val(netif->ip_addr)) { - if (netif->ip_addr.addr == 0) { + if (ip4_addr_isany_val(netif->ip_addr)) { if (sdk_dhcpc_flag != DHCP_STOPPED) { printf("dhcp client start...\n"); dhcp_start(netif);