Skip to content

Commit

Permalink
fix chksum
Browse files Browse the repository at this point in the history
  • Loading branch information
indexds committed Jan 19, 2025
1 parent 12595cc commit 00074a3
Show file tree
Hide file tree
Showing 27 changed files with 104 additions and 56 deletions.
20 changes: 10 additions & 10 deletions sdkconfig
Original file line number Diff line number Diff line change
Expand Up @@ -921,8 +921,8 @@ CONFIG_ESP_SYSTEM_PANIC_PRINT_HALT=y
# end of Memory protection

CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE=32
CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=3072
CONFIG_ESP_MAIN_TASK_STACK_SIZE=4096
CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=2048
CONFIG_ESP_MAIN_TASK_STACK_SIZE=3072
CONFIG_ESP_MAIN_TASK_AFFINITY_CPU0=y
# CONFIG_ESP_MAIN_TASK_AFFINITY_CPU1 is not set
# CONFIG_ESP_MAIN_TASK_AFFINITY_NO_AFFINITY is not set
Expand Down Expand Up @@ -1298,7 +1298,7 @@ CONFIG_LWIP_CHECKSUM_CHECK_UDP=y
CONFIG_LWIP_CHECKSUM_CHECK_ICMP=y
# end of Checksums

CONFIG_LWIP_TCPIP_TASK_STACK_SIZE=24576
CONFIG_LWIP_TCPIP_TASK_STACK_SIZE=32768
# CONFIG_LWIP_TCPIP_TASK_AFFINITY_NO_AFFINITY is not set
CONFIG_LWIP_TCPIP_TASK_AFFINITY_CPU0=y
# CONFIG_LWIP_TCPIP_TASK_AFFINITY_CPU1 is not set
Expand Down Expand Up @@ -1530,8 +1530,8 @@ CONFIG_ESP_PROTOCOMM_SUPPORT_SECURITY_VERSION_0=y
# PThreads
#
CONFIG_PTHREAD_TASK_PRIO_DEFAULT=5
CONFIG_PTHREAD_TASK_STACK_SIZE_DEFAULT=4096
CONFIG_PTHREAD_STACK_MIN=1024
CONFIG_PTHREAD_TASK_STACK_SIZE_DEFAULT=3072
CONFIG_PTHREAD_STACK_MIN=768
CONFIG_PTHREAD_DEFAULT_CORE_NO_AFFINITY=y
# CONFIG_PTHREAD_DEFAULT_CORE_0 is not set
# CONFIG_PTHREAD_DEFAULT_CORE_1 is not set
Expand Down Expand Up @@ -1780,8 +1780,8 @@ CONFIG_ESP32_PANIC_PRINT_HALT=y
# CONFIG_ESP32_PANIC_SILENT_REBOOT is not set
# CONFIG_ESP32_PANIC_GDBSTUB is not set
CONFIG_SYSTEM_EVENT_QUEUE_SIZE=32
CONFIG_SYSTEM_EVENT_TASK_STACK_SIZE=3072
CONFIG_MAIN_TASK_STACK_SIZE=4096
CONFIG_SYSTEM_EVENT_TASK_STACK_SIZE=2048
CONFIG_MAIN_TASK_STACK_SIZE=3072
CONFIG_CONSOLE_UART_DEFAULT=y
# CONFIG_CONSOLE_UART_CUSTOM is not set
# CONFIG_CONSOLE_UART_NONE is not set
Expand Down Expand Up @@ -1861,7 +1861,7 @@ CONFIG_TCP_OVERSIZE_MSS=y
# CONFIG_TCP_OVERSIZE_QUARTER_MSS is not set
# CONFIG_TCP_OVERSIZE_DISABLE is not set
CONFIG_UDP_RECVMBOX_SIZE=16
CONFIG_TCPIP_TASK_STACK_SIZE=24576
CONFIG_TCPIP_TASK_STACK_SIZE=32768
# CONFIG_TCPIP_TASK_AFFINITY_NO_AFFINITY is not set
CONFIG_TCPIP_TASK_AFFINITY_CPU0=y
# CONFIG_TCPIP_TASK_AFFINITY_CPU1 is not set
Expand All @@ -1880,8 +1880,8 @@ CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1=y
# CONFIG_ESP32_TIME_SYSCALL_USE_FRC1 is not set
# CONFIG_ESP32_TIME_SYSCALL_USE_NONE is not set
CONFIG_ESP32_PTHREAD_TASK_PRIO_DEFAULT=5
CONFIG_ESP32_PTHREAD_TASK_STACK_SIZE_DEFAULT=4096
CONFIG_ESP32_PTHREAD_STACK_MIN=1024
CONFIG_ESP32_PTHREAD_TASK_STACK_SIZE_DEFAULT=3072
CONFIG_ESP32_PTHREAD_STACK_MIN=768
CONFIG_ESP32_DEFAULT_PTHREAD_CORE_NO_AFFINITY=y
# CONFIG_ESP32_DEFAULT_PTHREAD_CORE_0 is not set
# CONFIG_ESP32_DEFAULT_PTHREAD_CORE_1 is not set
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ extern "C" {
#include <esp_err.h>
#include <lwip/netif.h>

#define ESP_WIREGUARD_CONFIG_DEFAULT() { \
.private_key = NULL, \
.listen_port = 0, \
.fw_mark = 0, \
.public_key = NULL, \
.preshared_key = NULL, \
.allowed_ip = NULL, \
.allowed_ip_mask = NULL, \
.endpoint = NULL, \
.port = 51820, \
.persistent_keepalive = 0, \
}

typedef struct {
/* interface config */
char* private_key; /**< a base64 private key generated by wg genkey. Required. */
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@
#define TAG "esp_wireguard"
#define WG_KEY_LEN (32)
#define WG_B64_KEY_LEN (4 * ((WG_KEY_LEN + 2) / 3))
#if defined(CONFIG_LWIP_IPV6)
#define WG_ADDRSTRLEN INET6_ADDRSTRLEN
#else
#define WG_ADDRSTRLEN INET_ADDRSTRLEN
#endif

static struct netif wg_netif_struct = {0};
static struct netif *wg_netif = NULL;
Expand Down Expand Up @@ -107,7 +111,6 @@ static esp_err_t esp_wireguard_peer_init(const wireguard_config_t *config, struc
}

/* resolve peer name or IP address */
ESP_LOGI(TAG, "resolving ip address..");
{
ip_addr_t endpoint_ip;
memset(&endpoint_ip, 0, sizeof(endpoint_ip));
Expand All @@ -121,14 +124,15 @@ static esp_err_t esp_wireguard_peer_init(const wireguard_config_t *config, struc
goto fail;
}

ESP_LOGI(TAG, "resolved ip address successfully!");

if (res->ai_family == AF_INET) {
struct in_addr addr4 = ((struct sockaddr_in *) (res->ai_addr))->sin_addr;
inet_addr_to_ip4addr(ip_2_ip4(&endpoint_ip), &addr4);
} else {
#if defined(CONFIG_LWIP_IPV6)
struct in6_addr addr6 = ((struct sockaddr_in6 *) (res->ai_addr))->sin6_addr;
inet6_addr_to_ip6addr(ip_2_ip6(&endpoint_ip), &addr6);
#endif
}

ESP_LOGI(TAG, "setting endpoint..");
peer->endpoint_ip = endpoint_ip;

if (inet_ntop(res->ai_family, &(peer->endpoint_ip), addr_str, WG_ADDRSTRLEN) == NULL) {
Expand All @@ -140,7 +144,6 @@ static esp_err_t esp_wireguard_peer_init(const wireguard_config_t *config, struc
config->port);
}
}
ESP_LOGI(TAG, "Setting port, keepalive..");
peer->endport_port = config->port;
peer->keep_alive = config->persistent_keepalive;
err = ESP_OK;
Expand Down Expand Up @@ -180,7 +183,6 @@ static esp_err_t esp_wireguard_netif_create(const wireguard_config_t *config)
goto fail;
}

ESP_LOGI(TAG, "attempting netif_add..");
/* Register the new WireGuard network interface with lwIP */
wg_netif = netif_add(
&wg_netif_struct,
Expand All @@ -196,9 +198,8 @@ static esp_err_t esp_wireguard_netif_create(const wireguard_config_t *config)
}

/* Mark the interface as administratively up, link up flag is set
* automatically when peer connects
*/
netif_set_up(wg_netif);
* automatically when peer connects */
netif_set_up(wg_netif);
err = ESP_OK;
fail:
return err;
Expand Down Expand Up @@ -302,25 +303,20 @@ esp_err_t esp_wireguard_disconnect(wireguard_ctx_t *ctx)

// Clear the IP address to gracefully disconnect any clients while the
// peers are still valid
ESP_LOGI(TAG, "clearing ip addr..");
netif_set_ipaddr(ctx->netif, IP4_ADDR_ANY4);

ESP_LOGI(TAG, "running wireguardif_disconnect..");
lwip_err = wireguardif_disconnect(ctx->netif, wireguard_peer_index);
if (lwip_err != ERR_OK) {
ESP_LOGW(TAG, "wireguardif_disconnect: peer_index: %" PRIu8 " err: %i", wireguard_peer_index, lwip_err);
}

ESP_LOGI(TAG, "running wireguardif_remove_peer..");
lwip_err = wireguardif_remove_peer(ctx->netif, wireguard_peer_index);
if (lwip_err != ERR_OK) {
ESP_LOGW(TAG, "wireguardif_remove_peer: peer_index: %" PRIu8 " err: %i", wireguard_peer_index, lwip_err);
}

ESP_LOGI(TAG, "shutting down if");
wireguard_peer_index = WIREGUARDIF_INVALID_INDEX;
wireguardif_shutdown(ctx->netif);
ESP_LOGI(TAG, "removing netif");
netif_remove(ctx->netif);
wireguardif_fini(ctx->netif);
netif_set_default(ctx->netif_default);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ static void mainloop(unsigned int work[64],const unsigned char e[32])
unsigned int s[32];
unsigned int t[32];
unsigned int u[32];
unsigned int i;
unsigned int j;
unsigned int b;
int pos;
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
/*
* Copyright (c) 2021 Daniel Hope (www.floorsense.nz)
* Copyright (c) 2021 Kenta Ida ([email protected])
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* 3. Neither the name of "Floorsense Ltd", "Agile Workspace Ltd" nor the names of
* its contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Author: Daniel Hope <[email protected]>
*/

#include "wireguardif.h"

#include <string.h>
Expand All @@ -12,10 +45,19 @@
#include <sys/socket.h>
#include <esp_log.h>
#include <esp_err.h>
#if defined(CONFIG_WIREGUARD_ESP_NETIF)
#include <esp_netif.h>
#endif
#if defined(CONFIG_WIREGUARD_ESP_TCPIP_ADAPTER)
#include <tcpip_adapter.h>
#endif

#include "wireguard.h"
#include "crypto.h"

#include <lwip/inet_chksum.h>
#include <esp_netif_net_stack.h>

#define WIREGUARDIF_TIMER_MSECS 400

#define TAG "wireguardif"
Expand Down Expand Up @@ -86,8 +128,12 @@ static err_t wireguardif_output_to_peer(struct netif *netif, struct pbuf *q, con
// Calculate the outgoing packet size - round up to next 16 bytes, add 16 bytes for header
if (q) {
// This is actual transport data
struct ip_hdr *unpadded_header = (struct ip_hdr *)q->payload;
unpadded_header->src.addr = 0x02c8a8c0;
struct ip_hdr *unpadded_header = (struct ip_hdr *)p->payload;
unpadded_header->dest.addr = 0x02c8a8c0;

IPH_CHKSUM_SET(unpadded_header, 0);
IPH_CHKSUM_SET(unpadded_header, inet_chksum(unpadded_header, IPH_HL_BYTES(unpadded_header)));

unpadded_len = q->tot_len;
} else {
// This is a keep-alive
Expand Down Expand Up @@ -231,40 +277,25 @@ err_t output_to_eth(struct pbuf *p) {
struct netif *eth_netif = esp_netif_get_netif_impl(esp_netif_get_handle_from_ifkey("ETH_DEF"));

if (eth_netif == NULL) {
ESP_LOGI(TAG, "couldnt find eth interface!");
ESP_LOGE(TAG, "ethif not found!");
return ERR_IF;
}

struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;

u16_t iphdr_hlen = IPH_HL_BYTES(iphdr);
u16_t iphdr_len = lwip_ntohs(IPH_LEN(iphdr));
iphdr->dest.addr = 0x0264a8c0; //2.100.168.192

if (iphdr_len < p->tot_len) {
pbuf_realloc(p, iphdr_len);
}

if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len) || (iphdr_hlen < IP_HLEN)) {
ESP_LOGW(TAG, "FUCKED LENGTH");
pbuf_free(p);
return ERR_OK;
}
if (inet_chksum(iphdr, iphdr_hlen) != 0) {
ESP_LOGW(TAG, "FAILED CHECKSUM!");
pbuf_free(p);
return ERR_OK;
}

ip4_addr_t src;
ip4_addr_copy(src, iphdr->src);
IPH_CHKSUM_SET(iphdr, 0);
IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IPH_HL_BYTES(iphdr)));

ip4_addr_t dest;
dest.addr = 0x0264a8c0; //2.100.168.192
ip4_addr_t src, dest;
src.addr = iphdr->src.addr;
dest.addr = iphdr->dest.addr;

if (ip4_output_if(p, &src, &dest,
IPH_TTL(iphdr), IPH_TOS(iphdr),
IPH_PROTO(iphdr), eth_netif) != ERR_OK) {
ESP_LOGI(TAG, "FAILED TO OUTPUT!");
ESP_LOGW(TAG, "failed to output!");
return ERR_IF;
}

Expand Down Expand Up @@ -299,6 +330,8 @@ static void wireguardif_process_data_message(struct wireguard_device *device, st
src = &data_hdr->enc_packet[0];
src_len = data_len;

ESP_LOGI(TAG, "Encrypted data size: %zu", src_len);

// We don't know the unpadded size until we have decrypted the packet and validated/inspected the IP header
pbuf = pbuf_alloc(PBUF_TRANSPORT, src_len - WIREGUARD_AUTHTAG_LEN, PBUF_RAM);
if (pbuf) {
Expand Down Expand Up @@ -358,23 +391,24 @@ static void wireguardif_process_data_message(struct wireguard_device *device, st

// 5. If the plaintext packet has not been dropped, it is inserted into the receive queue of the wg0 interface.
if (dest_ok) {
// Send packet to PC
ESP_LOGI(TAG, "Sending To: 192.168.100.2");
// Send packet to be process by LWIP
ESP_LOGI(TAG, "Outputting..");
output_to_eth(pbuf);
// ip_input(pbuf, device->netif);
// pbuf is owned by IP layer now
pbuf = NULL;
}
} else {
// IP header is corrupt or lied about packet size
ESP_LOGI(TAG, "Ip header is corrupt or lied about packet size.");
ESP_LOGW(TAG, "Corrupt packet!");
}
} else {
// This is a duplicate packet / replayed / too far out of order
ESP_LOGI(TAG, "Duplicate/Replayed packet.");
ESP_LOGW(TAG, "Duplicate packet!");
}
} else {
// This was a keep-alive packet
ESP_LOGI(TAG, "Keepalive.");
ESP_LOGI(TAG, "Keepalive packet!");
}
}

Expand Down Expand Up @@ -567,6 +601,8 @@ void wireguardif_network_rx(void *arg, struct udp_pcb *pcb, struct pbuf *p, cons
uint8_t *data = p->payload;
size_t len = p->len; // This buf, not chained ones

ESP_LOGI(TAG, "Found pbuf on udp socket! Length: %zu", len);

struct message_handshake_initiation *msg_initiation;
struct message_handshake_response *msg_response;
struct message_cookie_reply *msg_cookie;
Expand Down Expand Up @@ -632,6 +668,7 @@ void wireguardif_network_rx(void *arg, struct udp_pcb *pcb, struct pbuf *p, cons
break;

default:
ESP_LOGI(TAG, "Received: UNKNOWN OR BAD PACKET HEADER");
// Unknown or bad packet header
break;
}
Expand Down Expand Up @@ -1069,4 +1106,5 @@ void wireguardif_fini(struct netif *netif) {
// remove device context.
free(device);
netif->state = NULL;
}
}
// vim: noexpandtab

0 comments on commit 00074a3

Please sign in to comment.