Skip to content

Commit

Permalink
[WIP] 35% wireguard
Browse files Browse the repository at this point in the history
  • Loading branch information
radkesvat committed Jul 1, 2024
1 parent f3d4591 commit 0cb6560
Show file tree
Hide file tree
Showing 5 changed files with 1,352 additions and 27 deletions.
54 changes: 27 additions & 27 deletions tunnels/shared/wireguard/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@
their license files are placed next to this file
*/

#include <stdbool.h>
#include <stdint.h>

struct ip4_addr {
uint32_t addr;
};
typedef struct ip4_addr ip4_addr_t;
#include "lwip_types.h"

enum wg_general_limits
{
Expand Down Expand Up @@ -54,10 +48,10 @@ enum wg_timing_consts
enum wg_message_consts
{
kWgMsgInvalid = 0,
kWgMsgInitHandshake = 0,
kWgMsgReplyHandshake = 0,
kWgMsgReplyCookie = 0,
kWgMsgTransportData = 0
kWgMsgInitHandshake = 1,
kWgMsgReplyHandshake = 2,
kWgMsgReplyCookie = 3,
kWgMsgTransportData = 4
};

typedef struct wireguard_keypair_s
Expand Down Expand Up @@ -92,13 +86,15 @@ typedef struct wireguard_handshake_s

} wireguard_handshake_t;

typedef struct wireguard_allowed_ip_s {
bool valid;
ip_addr_t ip;
ip_addr_t mask;
typedef struct wireguard_allowed_ip_s
{
bool valid;
ip_addr_t ip;
ip_addr_t mask;

} wireguard_allowed_ip_t;

struct wireguard_peer
struct wireguard_peer_s
{
bool valid; // Is this peer initialised?
bool active; // Should we be actively trying to connect?
Expand All @@ -112,7 +108,7 @@ struct wireguard_peer
// keep-alive interval in seconds, 0 is disable
uint16_t keepalive_interval;

struct wireguard_allowed_ip allowed_source_ips[kWgMaxSrcIPs];
struct wireguard_allowed_ip_s allowed_source_ips[kWgMaxSrcIPs];

uint8_t public_key[kWgPublicKeyLen];
uint8_t preshared_key[kWgSessionKeyLen];
Expand All @@ -121,15 +117,15 @@ struct wireguard_peer
uint8_t public_key_dh[kWgPublicKeyLen];

// Session keypairs
struct wireguard_keypair curr_keypair;
struct wireguard_keypair prev_keypair;
struct wireguard_keypair next_keypair;
struct wireguard_keypair_s curr_keypair;
struct wireguard_keypair_s prev_keypair;
struct wireguard_keypair_s next_keypair;

// 5.1 Silence is a Virtue: The responder keeps track of the greatest timestamp received per peer
uint8_t greatest_timestamp[kWgTai64Len];

// The active handshake that is happening
struct wireguard_handshake handshake;
struct wireguard_handshake_s handshake;

// Decrypted cookie from the responder
uint32_t cookie_millis;
Expand Down Expand Up @@ -173,16 +169,19 @@ struct wireguard_device
uint8_t label_mac1_key[kWgSessionKeyLen];

// List of peers associated with this device
struct wireguard_peer peers[kWgMaxPeers];
struct wireguard_peer_s peers[kWgMaxPeers];

bool valid;
};

#define MESSAGE_INVALID 0
#define MESSAGE_HANDSHAKE_INITIATION 1
#define MESSAGE_HANDSHAKE_RESPONSE 2
#define MESSAGE_COOKIE_REPLY 3
#define MESSAGE_TRANSPORT_DATA 4
enum wireguard_message_constants
{
kMessageInvalid = 0,
kMessageHandshakeInitiation = 1,
kMessageHandshakeResponse = 2,
kMessageCookieReply = 3,
kMessageTransportData = 4
};

// 5.4.2 First Message: Initiator to Responder
struct message_handshake_initiation
Expand Down Expand Up @@ -223,6 +222,7 @@ struct message_cookie_reply
// 5.4.6 Subsequent Messages: Transport Data Messages
struct message_transport_data
{

uint8_t type;
uint8_t reserved[3];
uint32_t receiver;
Expand Down
64 changes: 64 additions & 0 deletions tunnels/shared/wireguard/lwip_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>

/*
Most of the code is taken and renamed, from the awesome projects wireguard-lwip and lwip
Author of lwip: Adam Dunkels https://github.com/smartalock/wireguard-lwip
Author of wireguard-lwip: Daniel Hope https://github.com/lwip-tcpip/lwip
their license files are placed next to this file
*/

struct ip4_addr
{
uint32_t addr;
};

typedef struct ip4_addr ip4_addr_t;

/** This is the aligned version of ip6_addr_t,
used as local variable, on the stack, etc. */
struct ip6_addr
{
uint32_t addr[4];
#if LWIP_IPV6_SCOPES
uint8_t zone;
#endif /* LWIP_IPV6_SCOPES */
};

/** IPv6 address */
typedef struct ip6_addr ip6_addr_t;

/** @ingroup ipaddr
* IP address types for use in ip_addr_t.type member.
* @see tcp_new_ip_type(), udp_new_ip_type(), raw_new_ip_type().
*/

enum lwip_ip_addr_type
{
/** IPv4 */
kIpaddrTypeV4 = 0U,
/** IPv6 */
kIpaddrTypeV6 = 6U,
/** IPv4+IPv6 ("dual-stack") */
kIpaddrTypeAny = 46U
};

/**
* @ingroup ipaddr
* A union struct for both IP version's addresses.
* ATTENTION: watch out for its size when adding IPv6 address scope!
*/
typedef struct ip_addr
{
union {
ip6_addr_t ip6;
ip4_addr_t ip4;
} u_addr;
/** @ref lwip_ip_addr_type */
uint8_t type;
} ip_addr_t;

typedef unsigned char err_t;
Loading

0 comments on commit 0cb6560

Please sign in to comment.