From: "stipa (Code Review)" Attention is currently required from: flichtenheld, plaisthos. stipa would like plaisthos and flichtenheld to review t=
his change.
dco-win: multipeer support
This is the main commit for=
dco-win multipeer
implementation.
This adds concept of "mod=
e" to DCO implementation,
which is peer-to-peer or multipeer. Depen=
ds on mode,
some functions use MP-specific IOCTL commands, which
incl=
ude peer-id as a part of input.
The driver initialization accomodate=
s server mode,
in which tun device is created before transport.
S=
ince on Windows the socket is visible to the kernel only,
control channe=
l packets have to be prepended with remote
sockaddr of the peer - this a=
llows userspace to distinguish
among peers. Sadly there is no reliable w=
ay to get peer local
address, such as on Linux/FreeBSD, so we have to do=
a bit of
guesswork figure out IP address based on remote IP and localrouting table, which may backfire if there are multiple IPs
assigned t=
o the same network adapter. However, as for now
peer-specific local IP i=
s not used by the driver. We use
instead the result of bind() to the lis=
tening address.
Existing sockethandle_finalize() function has been r=
efactored
to accomodate packets with possibly prepended sockaddr.
Change-Id: Ia267276d61fa1425ba205f54ba6eb89021f32dba
Signed-off-by: Lev=
Stipakov <lev@...515...>
---
M src/openvpn/dco.h
M src/open=
vpn/dco_freebsd.c
M src/openvpn/dco_linux.c
M src/openvpn/dco_win.cM src/openvpn/dco_win.h
M src/openvpn/forward.c
M src/openvpn/init.=
c
M src/openvpn/ovpn_dco_win.h
M src/openvpn/socket.c
M src/openvp=
n/socket.h
10 files changed, 518 insertions(+), 103 deletions(-)
=
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/15/815/1
= diff --git a/src/openvpn/dco.h b/src/openvpn/dco.h
in= dex 035474f..7f2021d 100644
--- a/src/openvpn/dco.h<= br>+++ b/src/openvpn/dco.h
@@ -106,9 +106,10 @@
*
* @param mode the instance operating = mode (P2P or multi-peer)
* @param dco the context t= o initialize
+ * @par= am dev_node device node, used on Windows to specify certain DCO adapter
* @return true on success, false otherwise<= br> */
-bool ovp= n_dco_init(int mode, dco_context_t *dco);
+bool ovpn_dco_init(int mode, dco_context_t *dco, cons= t char *dev_node);
/**
= * Open/create a DCO interface
@@ -284,7 +285,7 @@ }
static inline bool
= -ovpn_dco_init(int mode, dco_cont= ext_t *dco)
+ovpn_dco= _init(int mode, dco_context_t *dco, const char *dev_node)
= {
return true;
}
diff= --git a/src/openvpn/dco_freebsd.c b/src/openvpn/dco_freebsd.cindex f4c3b02..0e536de 100644
--- a/src/openvpn/dco_fre= ebsd.c
+++ b/src/openvpn/dco_freebsd.c
@@ -= 165,7 +165,7 @@
}
bool<= /span>
-ovpn_dco_init(int mode= , dco_context_t *dco)
+ovpn_dco_init(int mode, dco_context_t *dco, const char *dev_node)<= br> {
if (open_fd(dco) < 0)
= {
diff --git a/src/openvpn/dco_linux.c b/src/openvpn/dc= o_linux.c
index b038382..0a38a0e 100644
---= a/src/openvpn/dco_linux.c
+++ b/src/openvpn/dco_linux.c
@@ -391,7 +391,7 @@
}
bool
-ovpn= _dco_init(int mode, dco_context_t *dco)
+ovpn_dco_init(int mode, dco_context_t *dco, const char = *dev_node)
{
switch (mode)
= {
diff --git a/src/openvpn/dco_win.c b/src/openv= pn/dco_win.c
index 3bc123b..7be6fdb 100644
= --- a/src/openvpn/dco_win.c
+++ b/src/openvpn/dco_win.c
@@ -28,6 +28,7 @@
#include "syshead.h&qu= ot;
#include "dco.h"
+#include "forward.h"= ;
#include "tun.h"
#include &qu= ot;crypto.h"
#include "ssl_common.h"=
@@ -42,16 +43,37 @@
#endif
struct tuntap
-create_dco_handle(const char *devname, struct gc_arena *gc)
+create_dco_handle(const char = *devname, struct gc_arena *gc, int mode)
{
struct tuntap tt =3D { .backend_driver =3D DRIVER_DCO };const char *device_guid;
tu= n_open_device(&tt, devname, &device_guid, gc);
+ /* for mp we set m= ode in ovpn_dco_init(), however for p2p it is too late */
+ tt.dco.mode =3D (mode =3D=3D MODE= _SERVER) ? dco_mode_mp : dco_mode_p2p;
+
return tt;
}
+/* Som= etimes IP Helper API, which we use for setting IP address etc,+ * complains that interface is n= ot found. Give it some time to settle
+ */
+static void
+dco_wa= it_ready(DWORD idx)
+= {
+ for (int i =3D= 0; i < 20; ++i)
+= {
+ MIB_IP= INTERFACE_ROW row =3D { .InterfaceIndex =3D idx, .Family =3D AF_INET };
+ if (GetIpInterf= aceEntry(&row) !=3D ERROR_NOT_FOUND)
+ {
+ break;
+ }
+= msg(D_DCO_DEBUG, "interface %ld not yet ready, retrying",= idx);
+ Sleep= (50);
+ }+}
+
/**
* Gets= version of dco-win driver
*
@@ -104,8 +1= 26,32 @@
}
bool<= br>-ovpn_dco_init(int mode, dco_c= ontext_t *dco)
+ovpn_= dco_init(int mode, dco_context_t *dco, const char *dev_node){
+ /* for p2p = dco->mode is already set */
+ dco->mode =3D (mode =3D=3D MODE_POINT_TO_POINT) ? dco_mod= e_p2p : dco_mode_mp;
= +
+ if (dco->mo= de =3D=3D dco_mode_p2p)
+ {
+ re= turn true;
+ }
+
+ /* open DCO device */
+ struct gc_arena gc =3D gc_new()= ;
+ const char *de= vice_guid;
+ tun_o= pen_device(dco->tt, dev_node, &device_guid, &gc);+ gc_free(&gc); +
+ /* set mp mode */
+ OVPN_MODE m =3D OVPN_MODE_MP;
+ DWORD bytes_returned =3D 0;
+ if (!DeviceIoContro= l(dco->tt->hand, OVPN_IOCTL_SET_MODE, &m, sizeof(m), NULL, 0, &am= p;bytes_returned, NULL))
+ {
+ m= sg(M_ERR, "DeviceIoControl(OVPN_IOCTL_SET_MODE) failed");<= br>+ }
+
+ dco_wait_ready(dco->tt->adapter_index);
= +
return t= rue;
}
@@ -116,19 +162,1= 9 @@
return 0;
}
-static void-dco_wait_ready(DWORD idx)+void
+dco_p2p_start_vpn(struct tuntap *tt)
{
- for= (int i =3D 0; i < 20; ++i)
+ DWORD bytes_returned =3D 0;
+ if (!DeviceIoControl(tt->hand, OVPN_IOCTL_STA= RT_VPN, NULL, 0, NULL, 0, &bytes_returned, NULL))
= {
- MIB_IPINTERF= ACE_ROW row =3D {.InterfaceIndex =3D idx, .Family =3D AF_INET};
<= span style=3D"color: hsl(0, 100%, 40%);">- if (GetIpInterfaceEntry(&= amp;row) !=3D ERROR_NOT_FOUND)
- {
- = break;
- = }
- msg(D_DCO_= DEBUG, "interface %ld not yet ready, retrying", idx);
<= span style=3D"color: hsl(0, 100%, 40%);">- Sleep(50);+ msg(M_ERR, "DeviceIoC= ontrol(OVPN_IOCTL_START_VPN) failed");
}+
+ /* Sometimes IP Helper API, which we use fo= r setting IP address etc,
+ * complains that interface is not found. Give it some time to se= ttle
+ */<= br>+ dco_wait_ready(tt->a= dapter_index);
}
void= span>
@@ -141,17 +187,10 @@
*/tt->dco.tt =3D tt;
- DWORD bytes_returned =3D 0;
- if (!DeviceIoControl(tt->hand, O= VPN_IOCTL_START_VPN, NULL, 0, NULL, 0,
- &bytes_returned, NULL))
+ if (tt->dco.mode = =3D=3D dco_mode_p2p)
{
- msg(M_ERR, "DeviceIoControl(OVPN_IOCTL_S= TART_VPN) failed");
+ dco_p2p_start_vpn(tt);
}
-
- /* Sometimes IP Helper API, which we use for setting IP= address etc,
- * c= omplains that interface is not found. Give it some time to settle
- */
- dco_wait_ready(tt->adapter_index);= span>
}
static void
= @@ -207,14 +246,67 @@
}
= void
-dco_create= _socket(HANDLE handle, struct addrinfo *remoteaddr, bool bind_local,=
- struct add= rinfo *bind, int timeout,
- struct signal_info *sig_info)
+dco_mp_start_vpn(HANDLE handle, struct li= nk_socket *sock)
+{= span>
+ msg(D_DCO_DEBUG, = "%s", __func__);
+
+ int ai_f= amily =3D sock->info.lsa->bind_local->ai_family;
+ struct addrinfo *local =3D sock-&= gt;info.lsa->bind_local;
+ struct addrinfo *cur =3D NULL;
+
+ for (cur =3D local; cur; cur =3D cur->ai_next)
+ {
+ if (cur->ai_family =3D=3D ai_family)
+ {+ break; + }
+ }
+ if (!cur)
+ {
+ = msg(M_FATAL, "%s: Socket bind failed: Addr to bind has no %s reco= rd",
+ = __func__, addr_family_name(ai_family));
+ }
+
+ OVPN_MP= _START_VPN in, out;
+= in.IPv6Only =3D sock->info.bind_ipv6_only ? 1 : 0;
+ if (ai_family =3D=3D AF_INET)
+ {
+ memcpy(&in.ListenAddress.= Addr4, cur->ai_addr, sizeof(struct sockaddr_in));
+ }
+ else
+ {
+ = memcpy(&in.ListenAddress.Addr6, cur->ai_addr, sizeof(struct socka= ddr_in6));
+ }
+
+ /* in multipeer mode control channel = packets are prepended with remote peer's sockaddr */
+ sock->sockflags |=3D SF_PREPEND= _SA;
++ DWORD bytes_returned =3D 0;
+ if (!DeviceIoContr= ol(handle, OVPN_IOCTL_MP_START_VPN, &in, sizeof(in), &out, sizeof(o= ut),
+ = &bytes_returned, NULL))
+ {
+ msg(M_ERR, "DeviceIoControl(OVPN_IOCTL_MP_START_VPN) faile= d");
+ }
+
+ if (out.ListenAddress.Addr4.sin_famil= y =3D=3D AF_INET)
+ = {
+ memcpy(&= amp;sock->info.lsa->actual.dest.addr.in4, &out.ListenAddress.Addr= 4, sizeof(struct sockaddr_in));
+ }
+ = else
+ {<= br>+ memcpy(&sock-&g= t;info.lsa->actual.dest.addr.in6, &out.ListenAddress.Addr6, sizeof(s= truct sockaddr_in6));
+ }
+}
= +
+void
+dco_p2p_new_peer(HANDLE handle, struct link_socket *sock, struct= signal_info *sig_info)
{
msg(D_DCO_D= EBUG, "%s", __func__);
OVP= N_NEW_PEER peer =3D { 0 };
+ struct addrinfo *remoteaddr =3D sock->info.= lsa->current_remote;
+
struct sockaddr *local =3D NULL;
= struct sockaddr *remote =3D remoteaddr->ai_addr;
<= /span>
@@ -228,9 +320,10 @@
peer.Proto = =3D OVPN_PROTO_UDP;
}
- if (bind_local)
+ if (sock->bind_local)<= br> {
/* Use first local address with co= rrect address family */
+ struct addrinfo *bind =3D sock->info.lsa->bind_local;
while (bind && !local)
= {
if (bind->ai_family =3D=3D remote->= sa_family)
@@ -241,7 +334,7 @@
}= span>
}
- if (bind_local && !local)
+ if (sock->bind_local && = !local)
{
msg(M_FATAL, "= DCO: Socket bind failed: Address to bind lacks %s record",
<= span> addr_family_name(remote->sa_family));
= @@ -290,17 +383,131 @@
}
= else
{
- dco_connect_wait(handle, &ov, timeout, sig_info);=
+ dco_con= nect_wait(handle, &ov, get_server_poll_remaining_time(sock->server_p= oll_timeout), sig_info);
}
}<= /span>
}
+static struct sockaddr *
+dco_get_source_ip_for_dst(const struct sockaddr *dst)=
+{
+ SOCKADDR_INET dst_in =3D { 0 };= span>
+
+ /* Cast the destination address to S= OCKADDR_INET */
+ = if (dst->sa_family =3D=3D AF_INET)
+ {
+ dst_in.Ipv4 =3D *((struct sockaddr_in *)dst);
+ dst_in.si_family =3D AF_INET;=
+ }+ else if (dst->sa_family = =3D=3D AF_INET6)
+ = {
+ dst_in.Ip= v6 =3D *((struct sockaddr_in6 *)dst);
+ dst_in.si_family =3D AF_INET6;
+ }
+ else
+ {
+ = msg(M_FATAL, "%s: unknown dst address family %d", __func__,= dst->sa_family);
= + }
++ /* Get the best source addre= ss */
+ MIB_IPFORW= ARD_ROW2 row;
+ SO= CKADDR_INET best_src =3D { 0 };
+ DWORD ret =3D GetBestRoute2(NULL, 0, NULL, &dst_in, 0, = &row, &best_src);
+ if (ret !=3D NO_ERROR)
+ {
+ = msg(M_WARN, "%s: GetBestRoute2() failed with error: %ld\n"= , __func__, ret);
+ = return NULL;
+ = }
++ /* Allocate and copy the best = source address to return as sockaddr */
+ struct sockaddr *src =3D NULL;
+ if (best_src.si_family =3D=3D AF_INE= T6)
+ {
= + struct sockaddr_in6 *s= rc_in6 =3D (struct sockaddr_in6 *)malloc(sizeof(struct sockaddr_in6));
+ if (src_in6 =3D= =3D NULL)
+ {<= /span>
+ msg(M_FA= TAL, "%s: malloc failed", __func__);
+ }
+ *src_in6 =3D best_src.Ipv6;
+ src =3D (struct sockaddr *)src_in= 6;
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+ else if (best_src.si_family= =3D=3D AF_INET)
+ = {
+ struct so= ckaddr_in *src_in =3D (struct sockaddr_in *)malloc(sizeof(struct sockaddr_i= n));
+ if (src= _in =3D=3D NULL)
+ = {
+ m= sg(M_FATAL, "%s: malloc failed", __func__);
+ }
+ *src_in =3D best_src.Ipv4;
+ src =3D (struct sockaddr *)s= rc_in;
+ }<= br>+ else
+ {
+ msg(M_FATAL, "%s: unknown src family %d= ", __func__, best_src.si_family);
+ }
+
+ return src= ;
+}
+
intdco_new_peer(dco_context_t *dco, unsigned int peerid, int sd,
= struct sockaddr *localaddr, struct sockaddr *remoteaddr= ,
struct in_addr *vpn_ipv4, struct in6_addr *= vpn_ipv6)
{
msg(D_DCO_DEBUG, "%s= : peer-id %d, fd %d", __func__, peerid, sd);
+
+ if (dco->mode =3D=3D dco_mode_p2p)
+ {
+ /* no-op for p2p */
+ return 0;
+ }
+
+ OVP= N_MP_NEW_PEER newPeer =3D {0};
+
+ if (= remoteaddr)
+ {
+ struct sockadd= r *local =3D dco_get_source_ip_for_dst(remoteaddr);
+ if (local)
+ {
+ if (local->sa_family =3D=3D AF_INET)=
+ {
+ memcpy(&a= mp;newPeer.Local.Addr4, local, sizeof(struct sockaddr_in));
+ }
+ else
+ {
+ memcpy(&newPeer.Local.Addr6, l= ocal, sizeof(struct sockaddr_in6));
+ }
+ free(local);
+ }
+
+ if (re= moteaddr->sa_family =3D=3D AF_INET)
+ {
+ memcpy(&newPeer.Remote.Addr4, remoteaddr, sizeof(s= truct sockaddr_in));
= + }
+ e= lse
+ {=
+ memcpy(&ne= wPeer.Remote.Addr6, remoteaddr, sizeof(struct sockaddr_in6));+ }
+ }
+
= + if (vpn_ipv4)
+ = {
+ newPeer= .VpnAddr4 =3D *vpn_ipv4;
+ }
++ if (vpn_ipv6)
= + {
+ newPeer.VpnAddr6 =3D *vpn_ipv6;
+ }
+
+ newPeer.PeerId =3D peerid;
+
+ DWORD bytesReturned;
+ if (!DeviceIoControl(dco->tt->hand, OVPN_IOCTL_MP_= NEW_PEER, &newPeer, sizeof(newPeer), NULL, 0, &bytesReturned, NULL)= )
+ {+ msg(M_ERR, "DeviceI= oControl(OVPN_IOCTL_MP_NEW_PEER) failed");
+ }
+
return 0;
}
=
@@ -309,9 +516,20 @@
{
<= span> msg(D_DCO_DEBUG, "%s: peer-id %d", __func__, peerid);= span>
+ = OVPN_MP_DEL_PEER del_peer =3D { peerid };
+ VOID *buf =3D NULL;
+ DWORD len =3D 0;
+ DWORD ioctl =3D OVPN_IOCTL_DEL_PEER;=
+
+ if (dco->mode =3D=3D dco_mode_mp)
+ {
+ ioctl =3D OVPN_IOCTL_MP_DEL_PE= ER;
+ buf =3D = &del_peer;
+ = len =3D sizeof(del_peer);
+ }
+
DWORD bytes_returned =3D 0;
- if (!DeviceIoControl(dco->tt->hand, OVPN_I= OCTL_DEL_PEER, NULL,
- = 0, NULL, 0, &bytes_returned, NULL))
<= span style=3D"color: hsl(120, 100%, 40%);">+ if (!DeviceIoControl(dco-&g= t;tt->hand, ioctl, buf, len, NULL, 0, &bytes_returned, NULL))=
{
msg(M_WARN | M_ERRNO, "Devic= eIoControl(OVPN_IOCTL_DEL_PEER) failed");
ret= urn -1;
@@ -326,19 +544,30 @@
msg(D_DC= O_DEBUG, "%s: peer-id %d, keepalive %d/%d, mss %d", __func__,
peerid, keepalive_interval, keepalive_timeout, mss);<= /span>
- = OVPN_SET_PEER peer;
+= OVPN_MP_SET_PEER mp_peer =3D { peerid, keepalive_interval, keepalive_ti= meout, mss };
+ OV= PN_SET_PEER peer =3D { keepalive_interval, keepalive_timeout, mss };=
+ VOID *buf =3D NULL;
+ DWORD len =3D 0;= span>
+ DWORD ioctl =3D (= dco->mode =3D=3D dco_mode_mp) ? OVPN_IOCTL_MP_SET_PEER : OVPN_IOCTL_SET_= PEER;
- peer.KeepaliveInterval =3D keepalive_interval;
- peer.KeepaliveTimeout =3D keepalive_tim= eout;
- peer.MSS =3D= mss;
+ if (dco-&g= t;mode =3D=3D dco_mode_mp)
+ {
+ = buf =3D &mp_peer;
+ len =3D sizeof(OVPN_MP_SET_PEER);
+ }
+ else
+= {
+ buf = =3D &peer;
+ = len =3D sizeof(OVPN_SET_PEER);
+ }
DWORD bytes_retu= rned =3D 0;
- if (!D= eviceIoControl(dco->tt->hand, OVPN_IOCTL_SET_PEER, &peer,<= br>- size= of(peer), NULL, 0, &bytes_returned, NULL))
+ if (!DeviceIoControl(dco->tt->hand, io= ctl, buf, len, NULL, 0, &bytes_returned, NULL))
{<= /span>
- msg(M_WARN | M= _ERRNO, "DeviceIoControl(OVPN_IOCTL_SET_PEER) failed");
+ msg(M_WARN | M_ERRNO,= "DeviceIoControl(OVPN_IOCTL_MP_SET_PEER) failed");return -1;
}
+
return 0;
= }
@@ -397,9 +626,20 @@
{=
msg(D_DCO_DEBUG, "%s: peer-id %d", __func__= , peer_id);
+ OVPN_MP_SWAP_KEYS swap =3D {peer_id};
+ DWORD ioctl =3D OVPN_IOCTL_SWAP_KEYS;=
+ VOID *buf =3D N= ULL;
+ DWORD len = =3D 0;
++ if (dco->mode =3D=3D dco_= mode_mp)
+ {
+ ioctl =3D OVPN_IO= CTL_MP_SWAP_KEYS;
+ = buf =3D &swap;
+ len =3D sizeof(swap);
+ }
= +
DWORD bytes_returned =3D 0;
- if (!DeviceIoControl(dco->tt->hand, = OVPN_IOCTL_SWAP_KEYS, NULL, 0, NULL, 0,
- &bytes_returned, NULL))
+ if (!DeviceIoControl= (dco->tt->hand, ioctl, buf, len, NULL, 0, &bytes_returned, NULL))=
{
msg(M_ERR, "DeviceIoC= ontrol(OVPN_IOCTL_SWAP_KEYS) failed");
return= -1;
diff --git a/src/openvpn/dco_win.h b/src/openvpn/dco_w= in.h
index a28436c..3e1c2f3 100644
--- a/sr= c/openvpn/dco_win.h
+++ b/src/openvpn/dco_win.h
<= span>@@ -31,19 +31,28 @@
typedef OVPN_KEY_SLOT dco_key_slo= t_t;
typedef OVPN_CIPHER_ALG dco_cipher_t;
+typedef enum {
+ dco_mode_uninit,= span>
+ dco_mode_p2p,
+ dco_mode_mp<= br>+} dco_mode_type;
= +
struct dco_c= ontext {
struct tuntap *tt;
+ dco_mode_type mode;
+
};
typedef struct dco_context dco_context_t;
<= /span>
struct tuntap
-create_dco_handle(const char *devname, struct gc_arena *gc);
+create_dco_handle(const c= har *devname, struct gc_arena *gc, int mode);
<= span> void
-dco_create_= socket(HANDLE handle, struct addrinfo *remoteaddr, bool bind_local,<= br>- struct addr= info *bind, int timeout,
- struct signal_info *sig_info);
+dco_mp_start_vpn(HANDLE handle, struct li= nk_socket *sock);
+= span>
+void
+dco_p2p_new_peer(HANDLE handle, stru= ct link_socket *sock, struct signal_info *sig_info);
void
dco_start_tun(struct tuntap *tt);=
diff --git a/src/openvpn/forward.c b/src/openvpn/forward.c=
index d50b24c..0a3936a 100644
--- a/src/openvpn/f= orward.c
+++ b/src/openvpn/forward.c
@@ -13= 15,7 +1315,8 @@
}
else
= {
- s= ockethandle_t sh =3D { .is_handle =3D true, .h =3D c->c1.tuntap->hand= };
+ bool pre= pend_sa =3D c->options.mode =3D=3D MODE_SERVER && dco_enabled(&a= mp;c->options);
+ = sockethandle_t sh =3D { .is_handle =3D true, .h =3D c->c1.tuntap-= >hand, .prepend_sa =3D prepend_sa };
sockethand= le_finalize(sh, &c->c1.tuntap->reads, &c->c2.buf, NULL);= span>
}
#else /* ifdef _WIN32 */
= diff --git a/src/openvpn/init.c b/src/openvpn/init.c
= index 656fedf..b02e4f1 100644
--- a/src/openvpn/init.c
+++ b/src/openvpn/init.c
@@ -1987,7 +1987,7 @@<= /span>
#endif
if (dco_enabled(&c->= ;options))
{
- ovpn_dco_init(c->mode, &c->c1.tuntap-= >dco);
+ = ovpn_dco_init(c->mode, &c->c1.tuntap->dco, c->options.dev= _node);
}
= /* open the tun device */
diff --git a/src/openvpn/ovpn_= dco_win.h b/src/openvpn/ovpn_dco_win.h
index ea2a733..8003a= ce 100644
--- a/src/openvpn/ovpn_dco_win.h
= +++ b/src/openvpn/ovpn_dco_win.h
@@ -47,6 +47,23 @@<= br> OVPN_PROTO Proto;
} OVPN_NEW_PEER, * POVPN_NEW_= PEER;
+typedef struct _OVPN_MP_NEW_PEER {
+ union {
+ SOCKADDR_IN Addr4;
+ SOCKADDR_IN6 Addr6;
+ } Local;
+
+= union {
+ = SOCKADDR_IN Addr4;
+ = SOCKADDR_IN6 Addr6;
+ } Remote;
= +
+ IN_ADDR VpnAdd= r4;
+ IN6_ADDR Vpn= Addr6;
++ int PeerId;
+} OVPN_MP_NEW_PEER, * POVPN_MP_NEW_P= EER;
+typedef struct _OVPN_STATS {
LONG LostInControlPackets= ;
LONG LostOutControlPackets;
@@ -94,10 += 111,25 @@
int PeerId;
} OVPN_CRYPTO_DATA= , * POVPN_CRYPTO_DATA;
+#define CRYPTO_OPTIONS_AEAD_TAG_END (1<<1)
+#define CRYPTO_OPTIONS_64= BIT_PKTID (1<<2)
+
+typedef struct = _OVPN_CRYPTO_DATA_V2 {
+ OVPN_CRYPTO_DATA V1;
+ UINT32 CryptoOptions;
+} OVPN_CRYPTO_DATA_V2, * POVPN_CRYPTO_DATA_V2;+
+typedef struct _OVPN_MP_SET_PEER {
+ int PeerId;
+ LONG KeepaliveInterval;+ LONG KeepaliveTimeout; =
+ LONG MSS;+} OVPN_MP_SET_PEER, * POVPN_MP_S= ET_PEER;
+
= typedef struct _OVPN_SET_PEER {
- LONG KeepaliveInterval;
- LONG KeepaliveTimeout;
- LONG MSS;
+ LONG KeepaliveInterval;
+ LONG KeepaliveTimeout;
+ LONG MSS;
} OVPN_SET_PEER,= * POVPN_SET_PEER;
typedef struct _OVPN_= VERSION {
@@ -106,6 +138,50 @@
LONG Pa= tch;
} OVPN_VERSION, * POVPN_VERSION;
+typedef enum {<= br>+ OVPN_MODE_P2P,+ OVPN_MODE_MP
<= span style=3D"color: hsl(120, 100%, 40%);">+} OVPN_MODE;
+
+typedef struct _OVPN_SET_MODE {
+ OVPN_MODE Mode;
+} OVPN_SET_MODE, * POVPN_SET_MODE;
= +
+typedef struct _OVPN_MP_START_VPN {+ union {
+ SOCKADDR_IN Addr4;+ SOCKADDR_IN6 Addr6; =
+ } ListenAddress;
+ int IPv6Only;<= br>+} OVPN_MP_START_VPN, * POVP= N_MP_START_VPN;
+
+typedef enum {+ OVPN_CMD_DEL_PEER,
+ OVPN_CMD_SWAP_KEYS
+} OVPN_NOTIFY_CMD;
+
+typedef enum {
+ OVPN_DEL_PEER_REASON_TEARDOWN,
<= span style=3D"color: hsl(120, 100%, 40%);">+ OVPN_DEL_PEER_REASON_USERSP= ACE,
+ OVPN_DEL_PE= ER_REASON_EXPIRED,
+ = OVPN_DEL_PEER_REASON_TRANSPORT_ERROR,
+ OVPN_DEL_PEER_REASON_TRANSPORT_DISCONNECT+} OVPN_DEL_PEER_REASON;
+
+typedef struct _OVPN_NOTIFY_EVENT {
+ OVPN_NOTIFY_CMD Cmd;<= /span>
+ int PeerId;
+ OVPN_DEL_PEER_REASON= DelPeerReason;
+} OV= PN_NOTIFY_EVENT, * POVPN_NOTIFY_EVENT;
+
+= typedef struct _OVPN_MP_DEL_PEER {
+ int PeerId;
+} OVPN_MP_DEL_PEER, * POVPN_MP_DEL_PEER;
+
+typedef struct _OVPN_MP_SWAP_KEYS {
+ int PeerId;
+} OVPN_MP_SWAP_KEYS, * POVPN_MP_SWAP_KEYS;
+
#define= OVPN_IOCTL_NEW_PEER CTL_CODE(FILE_DEVICE_UNKNOWN, 1, METHOD_BUFFERED, = FILE_ANY_ACCESS)
#define OVPN_IOCTL_GET_STATS CTL_CODE(= FILE_DEVICE_UNKNOWN, 2, METHOD_BUFFERED, FILE_ANY_ACCESS)
= #define OVPN_IOCTL_NEW_KEY CTL_CODE(FILE_DEVICE_UNKNOWN, 3, METHOD_BUF= FERED, FILE_ANY_ACCESS)
@@ -114,3 +190,14 @@#define OVPN_IOCTL_START_VPN CTL_CODE(FILE_DEVICE_UNKNOWN, 6, METHOD_= BUFFERED, FILE_ANY_ACCESS)
#define OVPN_IOCTL_DEL_PEER = CTL_CODE(FILE_DEVICE_UNKNOWN, 7, METHOD_BUFFERED, FILE_ANY_ACCESS)<= br> #define OVPN_IOCTL_GET_VERSION CTL_CODE(FILE_DEVICE_UNKNOWN, 8, = METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define OVPN_IOCTL_NEW_KEY_V2 CTL_CODE(FILE_DEVICE_UNKNOWN,= 9, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define OVPN_IOCTL_SET_MODE CTL_CODE(FILE_DEVICE_UNKN= OWN, 10, METHOD_BUFFERED, FILE_ANY_ACCESS)
+
+#define OVPN_IOCTL_MP_START_VPN CTL_CODE(FILE_DEVICE_UNKNOWN, 11, MET= HOD_BUFFERED, FILE_ANY_ACCESS)
+#define OVPN_IOCTL_MP_NEW_PEER CTL_CODE(FILE_DEVICE_UNKNOWN, = 12, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define OVPN_IOCTL_MP_SET_PEER CTL_CODE(FILE_DEVICE_UN= KNOWN, 13, METHOD_BUFFERED, FILE_ANY_ACCESS)
+
+#define OVPN_IOCTL_NOTIFY_EVENT CTL_CODE(FILE_DEVICE_UNKNOWN, 14, M= ETHOD_BUFFERED, FILE_ANY_ACCESS)
+
+#defin= e OVPN_IOCTL_MP_DEL_PEER CTL_CODE(FILE_DEVICE_UNKNOWN, 15, METHOD_BUFFER= ED, FILE_ANY_ACCESS)
= +#define OVPN_IOCTL_MP_SWAP_KEYS CTL_CODE(FILE_DEVICE_UNKNOWN, 16, METHOD= _BUFFERED, FILE_ANY_ACCESS)
diff --git a/src/openvpn/socket= .c b/src/openvpn/socket.c
index efd742c..6909e7a 100644
--- a/src/openvpn/socket.c
+++ b/src/openvpn/s= ocket.c
@@ -2144,7 +2144,7 @@
stru= ct tuntap *tt;
ALLOC_OBJ(tt, struct tuntap);
- *= tt =3D create_dco_handle(c->options.dev_node, &c->gc);
= + *tt =3D create_dco_han= dle(c->options.dev_node, &c->gc, c->options.mode);
<= span>
/* Ensure we can "safely" cast th= e handle to a socket */
static_assert(sizeof(sock-= >sd) =3D=3D sizeof(tt->hand), "HANDLE and SOCKET size differs&qu= ot;);
@@ -2152,12 +2152,14 @@
c-&g= t;c1.tuntap =3D tt;
}
- dco_create_socket(c->c1.tunta= p->hand,
- = sock->info.lsa->current_remote,
- sock->bind_local, sock= ->info.lsa->bind_local,
- get_server_poll_remaining_time(sock->serve= r_poll_timeout),
- = sig_info);
-
+ if (c->= ;options.mode =3D=3D MODE_SERVER)
+ {
+= dco_mp_start_vpn(c->c1.tuntap->hand, sock);
+ }
+ else
+ {
+ = dco_p2p_new_peer(c->c1.tuntap->hand, sock, sig_info);+ }
s= ock->sockflags |=3D SF_DCO_WIN;
i= f (sig_info->signal_received)
@@ -2207,16 +2209,16 @@
resolve_remote(sock, 2, &remote_dynamic, sig_info);=
/* If a valid remote has been found= , create the socket with its addrinfo */
- if (sock->info.lsa->current_remote)
= - {
#if defin= ed(_WIN32)
- if = (dco_enabled(&c->options))
- {
-= create_socket_dco_win(c, sock, sig_info);
- goto done;
- }
- else
+ if (dco_enabled(&c->options))
+ {
+ create_socket_dco_win(c, sock, sig_info);
+ goto done;
+ }
+ else
#endif=
+ {
+ if (sock->info.lsa->curren= t_remote)
{
create_so= cket(sock, sock->info.lsa->current_remote);
= }
@@ -3753,6 +3755,91 @@
return sock-&= gt;writes.iostate;
}
+void
+read_sockaddr_from_overlapped(struct overlapped_io *io= , struct sockaddr *dst, int overlapped_ret)
+{
+ if (overlapped_ret >=3D 0 && io->addr_defined)
+ {
+ /* TODO(jjo): streamline this = mess */
+ /* i= n this func we don't have relevant info about the PF_ of this
+ * endpoint, as link_= socket_actual will be zero for the 1st received packet
+ *
+ * Test for inets PF_ possible sizes
+ */+ switch (io->addrlen)<= /span>
+ {
= + case sizeof(struct= sockaddr_in):
+ = case sizeof(struct sockaddr_in6):
+ /* TODO(jjo): for some reason (?) I'm = getting 24,28 for AF_INET6
+ * under _WIN32*/
+ case sizeof(struct sockaddr_in6) - 4:+ break;
+
+ default:
+ bad_address_length(io->= ;addrlen, af_addr_size(io->addr.sin_family));
+ }
+
+= switch (io->addr.sin_family)
+ {
+ case AF_INET:
+ memcpy(dst, &io->addr, sizeof(struct= sockaddr_in));
+ = break;
+<= /span>
+ case AF_= INET6:
+ = memcpy(dst, &io->addr, sizeof(struct sockaddr_in));+ break;
= + }
+ }
+ else
+ {
+ = CLEAR(*dst);
+ = }
+}
+
+/**
+ * @brief Extracts a sockaddr from a packet payload.
+ *
+ * Reads a sockaddr structure from the start of the pa= cket buffer and writes it to `dst`.
+ *
+ = * @param[in] buf Packet buffer containing the payload.
+ * @param[out] dst Destination buffer fo= r the extracted sockaddr.
+ * @return Length of the extracted sockaddr
+ */
+static int
+read_sockaddr_from_packet(struct buffer *buf, struct sockaddr *dst)
+{
+ int sa_len =3D 0;
+
+ const struct sockaddr *sa =3D (const struct sockaddr = *)BPTR(buf);
+ swi= tch (sa->sa_family)
+ {
+ cas= e AF_INET:
+ = if (buf_len(buf) < sizeof(struct sockaddr_in))
+ {
+ msg(M_FATAL, "ERROR: recei= ved incoming packet with too short length of %d -- must be at least %zu.&qu= ot;, buf_len(buf), sizeof(struct sockaddr_in));
+ }
+ memcpy(dst, sa, sizeof(struct sockaddr_in= ));
+ buf_= advance(buf, sizeof(struct sockaddr_in));
+ sa_len =3D sizeof(struct sockaddr_in);
+ break;
+
+ case AF_INET6:
+ if (buf_len(buf) < size= of(struct sockaddr_in6))
+ {
+ = msg(M_FATAL, "ERROR: received incoming packet with too = short length of %d -- must be at least %zu.", buf_len(buf), sizeof(str= uct sockaddr_in6));
+= }
+ = memcpy(dst, sa, sizeof(struct sockaddr_in6));
+ buf_advance(buf, sizeof(struc= t sockaddr_in6));
+ = sa_len =3D sizeof(struct sockaddr_in6);
+ break;
+
+ default:
+ msg(M_FATAL, "ERROR: received incoming packet with = invalid address family %d.", sa->sa_family);
+ }
+
= + return sa_len;
+= }
+
= /* Returns the number of bytes successfully read */
int
sockethandle_finalize(sockethandle_t sh,
@@ = -3826,45 +3913,14 @@
ASSERT(0);}
- /* return from address if requested */
+ if (from && ret > 0 && s= h.is_handle && sh.prepend_sa)
+ {
+ ret -=3D read_sockaddr_from_packet(buf, &from->dest.addr= .sa);
+ }+
if (!s= h.is_handle && from)
{
- if (ret >=3D 0 && io->= ;addr_defined)
- = {
- /* TODO= (jjo): streamline this mess */
- /* in this func we don't have relevant info about = the PF_ of this
- = * endpoint, as link_socket_actual will be zero for the 1st received = packet
- *<= /span>
- * Test fo= r inets PF_ possible sizes
- */
- = switch (io->addrlen)
- {
- case sizeof(struct sockaddr_in):
- case sizeof(struct sockaddr= _in6):
- = /* TODO(jjo): for some reason (?) I'm getting 24,28 for AF_INET6
- * under _W= IN32*/
- = case sizeof(struct sockaddr_in6)-4:
- break;
-
- = default:
- bad_address_length(io->addrlen, af_addr_size(io-&g= t;addr.sin_family));
- = }
-- switch (io->add= r.sin_family)
- = {
- = case AF_INET:
- = from->dest.addr.in4 =3D io->addr;
- break;
-
- case AF_INET6:
- from->dest.addr.in6 =3D io-= >addr6;
- = break;
- = }
- }
- else- {
- CLEAR(from->dest.addr);- }
+ read_sockaddr_from_overlapped(io= , &from->dest.addr.sa, ret);
}
=
if (buf)
diff --git a/src/openvpn/so= cket.h b/src/openvpn/socket.h
index 465d92b..36c683c 100644=
--- a/src/openvpn/socket.h
+++ b/src/openv= pn/socket.h
@@ -224,6 +224,7 @@
#define SF= _HOST_RANDOMIZE (1<<3)
#define SF_GETADDRINFO_DGRAM = (1<<4)
#define SF_DCO_WIN (1<<5)+#define SF_PREPEND_SA (1<<6= )
unsigned int sockflags;
int mar= k;
const char *bind_dev;
@@ -287,6 +28= 8,7 @@
HANDLE h;
};
bool is_handle;
+ bool prepend_sa; /* are incoming packets prepended with sockadd= r? */
} sockethandle_t;
= int sockethandle_finalize(sockethandle_t sh,
@@ -1042,6 +1= 044,7 @@
{
*from =3D sock->= ;info.lsa->actual;
sh.is_handle =3D true;
+ sh.prepend_sa =3D= sock->sockflags & SF_PREPEND_SA;
}
<= span> return sockethandle_finalize(sh, &sock->reads, buf, from);=
}
@@ -1112,6 +1115,24 @@
= err =3D SocketHandleGetLastError(sh);
= }
}
+
+ /* dco-wi= n mp requires control packets to be prepended with sockaddr */+ if (sock->sockflags &= SF_PREPEND_SA)
+ = {
+ if (to->= ;dest.addr.sa.sa_family =3D=3D AF_INET)
+ {
+ struct sockaddr_in sa;
+ memcpy(&sa, &to->dest.addr.= in4, sizeof(sa));
+ = buf_write_prepend(buf, &sa, sizeof(sa));
+ }
+ else
+ {
+ struct sockaddr_in6 sa;
+ memcpy(&sa, &to->dest.addr.i= n6, sizeof(sa));
+ = buf_write_prepend(buf, &sa, sizeof(sa));
+ }
+ }
+
socket_send_queue(sock, buf, to);
if (status < 0)
{
To view, visit change 815. To unsubscribe, or for help writing ma= il filters, visit settings= a>.