Skip to content

Commit

Permalink
multipeer: proper support for IPv6
Browse files Browse the repository at this point in the history
SOCKADDR is not big enough to fit SOCKADDR_IN6, so use
SOCKADDR_STORAGE to store peer's remote transport address.

Signed-off-by: Lev Stipakov <[email protected]>
  • Loading branch information
lstipakov committed Nov 14, 2024
1 parent e333765 commit cf7a29e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
13 changes: 13 additions & 0 deletions socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,16 @@ OvpnSocketSend(_In_ OvpnSocket* ovpnSocket, _In_ OVPN_TX_BUFFER* buffer, _In_opt
_Must_inspect_result_
NTSTATUS
OvpnSocketTcpConnect(_In_ PWSK_SOCKET socket, _In_ PVOID context, _In_ PSOCKADDR remote);

template<typename T>
static
VOID
OvpnSocketCopyRemoteToSockaddr(T& remote, SOCKADDR_STORAGE* sockaddr) {
// Copy the appropriate address based on the family
if (remote.IPv4.sin_family == AF_INET) {
RtlCopyMemory(sockaddr, &remote.IPv4, sizeof(SOCKADDR_IN));
}
else if (remote.IPv6.sin6_family == AF_INET6) {
RtlCopyMemory(sockaddr, &remote.IPv6, sizeof(SOCKADDR_IN6));
}
}
6 changes: 3 additions & 3 deletions timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ static VOID OvpnTimerXmit(WDFTIMER timer)
ExAcquireSpinLockSharedAtDpcLevel(&peer->SpinLock);

auto peerId = peer->PeerId;
auto addr = peer->TransportAddrs.Remote;
SOCKADDR_STORAGE sa;
OvpnSocketCopyRemoteToSockaddr(peer->TransportAddrs.Remote, &sa);

OvpnCryptoContext* cryptoContext = &peer->CryptoContext;
if (cryptoContext->Encrypt) {
Expand All @@ -97,8 +98,7 @@ static VOID OvpnTimerXmit(WDFTIMER timer)

if (NT_SUCCESS(status)) {
// start async send, completion handler will return ciphertext buffer to the pool
SOCKADDR* sa = (SOCKADDR*)&(addr);
LOG_IF_NOT_NT_SUCCESS(status = OvpnSocketSend(&device->Socket, buffer, sa));
LOG_IF_NOT_NT_SUCCESS(status = OvpnSocketSend(&device->Socket, buffer, (SOCKADDR*)&sa));
if (NT_SUCCESS(status)) {
LOG_INFO("Ping sent", TraceLoggingValue(peerId, "peer-id"));
}
Expand Down
27 changes: 7 additions & 20 deletions txqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,6 @@
#include "socket.h"
#include "peer.h"

template<typename T>
static
VOID
OvpnTxCopyRemoteToSockaddr(T& remote, SOCKADDR* sockaddr) {
// Copy the appropriate address based on the family
if (remote.IPv4.sin_family == AF_INET) {
RtlCopyMemory(sockaddr, &remote.IPv4, sizeof(SOCKADDR_IN));
}
else if (remote.IPv6.sin6_family == AF_INET6) {
RtlCopyMemory(sockaddr, &remote.IPv6, sizeof(SOCKADDR_IN6));
}
}

static
BOOLEAN
OvpnTxAreSockaddrEqual(const SOCKADDR* addr1, const SOCKADDR* addr2) {
Expand Down Expand Up @@ -81,7 +68,7 @@ _Must_inspect_result_
static
NTSTATUS
OvpnTxProcessPacket(_In_ POVPN_DEVICE device, _In_ POVPN_TXQUEUE queue, _In_ NET_RING_PACKET_ITERATOR *pi,
_Inout_ OVPN_TX_BUFFER **head, _Inout_ OVPN_TX_BUFFER** tail, _Inout_ SOCKADDR *headSockaddr)
_Inout_ OVPN_TX_BUFFER **head, _Inout_ OVPN_TX_BUFFER** tail, _Inout_ SOCKADDR_STORAGE *headSockaddr)
{
NET_RING_FRAGMENT_ITERATOR fi = NetPacketIteratorGetFragments(pi);

Expand Down Expand Up @@ -189,16 +176,16 @@ OvpnTxProcessPacket(_In_ POVPN_DEVICE device, _In_ POVPN_TXQUEUE queue, _In_ NET
// If this peer is different (head sockaddr != peer sockaddr) to the previous buffer chain peers,
// then flush those and restart with a new buffer list.

if ((*head != NULL) && !(OvpnTxAreSockaddrEqual(headSockaddr, (const SOCKADDR*)&remoteAddr)))
if ((*head != NULL) && !(OvpnTxAreSockaddrEqual((const SOCKADDR*)headSockaddr, (const SOCKADDR*)&remoteAddr)))
{
LOG_IF_NOT_NT_SUCCESS(OvpnSocketSend(&device->Socket, *head, headSockaddr));
LOG_IF_NOT_NT_SUCCESS(OvpnSocketSend(&device->Socket, *head, (SOCKADDR*)headSockaddr));
*head = buffer;
*tail = buffer;
OvpnTxCopyRemoteToSockaddr(remoteAddr, headSockaddr);
OvpnSocketCopyRemoteToSockaddr(remoteAddr, headSockaddr);
} else {
if (*head == NULL) {
*head = buffer;
OvpnTxCopyRemoteToSockaddr(remoteAddr, headSockaddr);
OvpnSocketCopyRemoteToSockaddr(remoteAddr, headSockaddr);
}
else {
(*tail)->WskBufList.Next = &buffer->WskBufList;
Expand Down Expand Up @@ -236,7 +223,7 @@ OvpnEvtTxQueueAdvance(NETPACKETQUEUE netPacketQueue)

OVPN_TX_BUFFER* txBufferHead = NULL;
OVPN_TX_BUFFER* txBufferTail = NULL;
SOCKADDR headSockaddr = {0};
SOCKADDR_STORAGE headSockaddr = {0};

while (NetPacketIteratorHasAny(&pi)) {
NET_PACKET* packet = NetPacketIteratorGetPacket(&pi);
Expand All @@ -261,7 +248,7 @@ OvpnEvtTxQueueAdvance(NETPACKETQUEUE netPacketQueue)
if (packetSent) {
if (!device->Socket.Tcp) {
// this will use WskSendMessages to send buffers list which we constructed before
LOG_IF_NOT_NT_SUCCESS(OvpnSocketSend(&device->Socket, txBufferHead, &headSockaddr));
LOG_IF_NOT_NT_SUCCESS(OvpnSocketSend(&device->Socket, txBufferHead, (SOCKADDR*)&headSockaddr));
}
}
}
Expand Down

0 comments on commit cf7a29e

Please sign in to comment.