From: "stipa (Code Review)"
Date: Thu, 6 Mar 2025 14:37:29 +0000
References:
Reply-To: lstipakov@...277..., arne-openvpn@...1227...,
openvpn-devel@lists.sourceforge.net, frank@...2641...
Message-ID: <4e1b7d3ab73e978ecb22a56cec438fef03ad3146-HTML@...2715...>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Disposition: inline
Content-Type: multipart/alternative; boundary="2T89gShuNOo="; charset=UTF-8
Subject: [Openvpn-devel] [M] Change in openvpn[master]: Make recursive
routing check more fine-grained
List-Id:
List-Unsubscribe: ,
List-Archive:
List-Post:
List-Help:
List-Subscribe: ,
To: plaisthos , flichtenheld
Cc: openvpn-devel
--2T89gShuNOo=
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Attention is currently required from: flichtenheld, plaisthos.
Hello plaisthos, flichtenheld,
I'd like you to do a code review.
Please visit
http://gerrit.openvpn.net/c/openvpn/+/903?usp=3Demail
to review the following change.
Change subject: Make recursive routing check more fine-grained
......................................................................
Make recursive routing check more fine-grained
The existing recursive routing check drops TUN packets
if their address matches the remote. While this works in
most cases, a more fine-grained check is preferable for
complex routing rules.
Since we only need to drop traffic originating from OpenVPN,
all of the following values must match between the packet
and the link:
- IP protocol
- Transport protocol (TCP/UDP)
- Destination address
- Destination port
GitHub: #699
Change-Id: I6841e2f2a85275254a04e2d8ae3defe4420db8f6
Signed-off-by: Lev Stipakov
---
M src/openvpn/forward.c
1 file changed, 63 insertions(+), 38 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/03/903/1
diff --git a/src/openvpn/forward.c b/src/openvpn/forward.c
index b025344..7ec3ef2 100644
--- a/src/openvpn/forward.c
+++ b/src/openvpn/forward.c
@@ -1386,74 +1386,99 @@
static void
drop_if_recursive_routing(struct context *c, struct buffer *buf)
{
- bool drop =3D false;
- struct openvpn_sockaddr tun_sa;
- int ip_hdr_offset =3D 0;
+ struct gc_arena gc =3D gc_new();
if (c->c2.to_link_addr =3D=3D NULL) /* no remote addr known */
{
- return;
+ goto out;
}
- tun_sa =3D c->c2.to_link_addr->dest;
+ struct openvpn_sockaddr *link_addr =3D &c->c2.to_link_addr->dest;
+ struct link_socket_info *lsi =3D get_link_socket_info(c);
+ uint16_t link_port =3D atoi(c->c2.link_sockets[0]->remote_port);
- int proto_ver =3D get_tun_ip_ver(TUNNEL_TYPE(c->c1.tuntap), &c->c2.buf=
, &ip_hdr_offset);
+ uint16_t src_port =3D 0, dst_port =3D 0;
- if (proto_ver =3D=3D 4)
+ int ip_hdr_offset =3D 0;
+ int tun_ip_ver =3D get_tun_ip_ver(TUNNEL_TYPE(c->c1.tuntap), &c->c2.bu=
f, &ip_hdr_offset);
+
+ if (tun_ip_ver =3D=3D 4)
{
- /* make sure we got whole IP header */
- if (BLEN(buf) < ((int) sizeof(struct openvpn_iphdr) + ip_hdr_offse=
t))
+ /* make sure we got whole IP header and TCP/UDP src/dst ports */
+ if (BLEN(buf) < ((int) sizeof(struct openvpn_iphdr) + ip_hdr_offse=
t + sizeof(uint16_t) * 2))
{
- return;
- }
-
- /* skip ipv4 packets for ipv6 tun */
- if (tun_sa.addr.sa.sa_family !=3D AF_INET)
- {
- return;
+ goto out;
}
struct openvpn_iphdr *pip =3D (struct openvpn_iphdr *) (BPTR(buf) =
+ ip_hdr_offset);
- /* drop packets with same dest addr as gateway */
- if (memcmp(&tun_sa.addr.in4.sin_addr.s_addr, &pip->daddr, sizeof(p=
ip->daddr)) =3D=3D 0)
+ /* skip if tun protocol doesn't match link protocol */
+ if ((lsi->proto =3D=3D PROTO_TCP && pip->protocol !=3D OPENVPN_IPP=
ROTO_TCP)
+ || (lsi->proto =3D=3D PROTO_UDP && pip->protocol !=3D OPENVPN_=
IPPROTO_UDP))
{
- drop =3D true;
+ goto out;
+ }
+
+ /* skip ipv4 packets for ipv6 tun */
+ if (link_addr->addr.sa.sa_family !=3D AF_INET)
+ {
+ goto out;
+ }
+
+ /* drop packets with same dest addr and port as remote */
+ uint8_t *l4_hdr =3D (uint8_t *)pip + sizeof(struct openvpn_iphdr);
+ uint16_t src_port =3D ntohs(*(uint16_t *)l4_hdr);
+ uint16_t dst_port =3D ntohs(*(uint16_t *)(l4_hdr + sizeof(uint16_t=
)));
+ if ((memcmp(&link_addr->addr.in4.sin_addr.s_addr, &pip->daddr, siz=
eof(pip->daddr)) =3D=3D 0) && (link_port =3D=3D dst_port))
+ {
+ c->c2.buf.len =3D 0;
+
+ msg(D_LOW, "Recursive routing detected, packet dropped %s:%" P=
RIu16 " -> %s",
+ print_in_addr_t(pip->saddr, IA_NET_ORDER, &gc),
+ src_port,
+ print_link_socket_actual(c->c2.to_link_addr, &gc));
}
}
- else if (proto_ver =3D=3D 6)
+ else if (tun_ip_ver =3D=3D 6)
{
- /* make sure we got whole IPv6 header */
- if (BLEN(buf) < ((int) sizeof(struct openvpn_ipv6hdr) + ip_hdr_off=
set))
+ /* make sure we got whole IPv6 header and TCP/UDP src/dst ports */
+ if (BLEN(buf) < ((int) sizeof(struct openvpn_ipv6hdr) + ip_hdr_off=
set + sizeof(uint16_t) * 2))
{
- return;
+ goto out;
}
/* skip ipv6 packets for ipv4 tun */
- if (tun_sa.addr.sa.sa_family !=3D AF_INET6)
+ if (link_addr->addr.sa.sa_family !=3D AF_INET6)
{
- return;
+ goto out;
}
struct openvpn_ipv6hdr *pip6 =3D (struct openvpn_ipv6hdr *) (BPTR(=
buf) + ip_hdr_offset);
- /* drop packets with same dest addr as gateway */
- if (OPENVPN_IN6_ARE_ADDR_EQUAL(&tun_sa.addr.in6.sin6_addr, &pip6->=
daddr))
+ /* skip if tun protocol doesn't match link protocol */
+ if ((lsi->proto =3D=3D PROTO_TCP && pip6->nexthdr !=3D OPENVPN_IPP=
ROTO_TCP)
+ || (lsi->proto =3D=3D PROTO_UDP && pip6->nexthdr !=3D OPENVPN_=
IPPROTO_UDP))
{
- drop =3D true;
+ goto out;
+ }
+
+ /* drop packets with same dest addr and port as remote */
+ uint8_t *l4_hdr =3D (uint8_t *)pip6 + sizeof(struct openvpn_ipv6hd=
r);
+ src_port =3D ntohs(*(uint16_t *)l4_hdr);
+ dst_port =3D ntohs(*(uint16_t *)(l4_hdr + sizeof(uint16_t)));
+ if ((OPENVPN_IN6_ARE_ADDR_EQUAL(&link_addr->addr.in6.sin6_addr, &p=
ip6->daddr)) && (link_port =3D=3D dst_port))
+ {
+ c->c2.buf.len =3D 0;
+
+ msg(D_LOW, "Recursive routing detected, packet dropped %s:%" P=
RIu16 " -> %s",
+ print_in6_addr(pip6->saddr, IA_NET_ORDER, &gc),
+ src_port,
+ print_link_socket_actual(c->c2.to_link_addr, &gc));
}
}
- if (drop)
- {
- struct gc_arena gc =3D gc_new();
-
- c->c2.buf.len =3D 0;
-
- msg(D_LOW, "Recursive routing detected, drop tun packet to %s",
- print_link_socket_actual(c->c2.to_link_addr, &gc));
- gc_free(&gc);
- }
+out:
+ gc_free(&gc);
}
/*
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/903?usp=3Demail
To unsubscribe, or for help writing mail filters, visit http://gerrit.openv=
pn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6841e2f2a85275254a04e2d8ae3defe4420db8f6
Gerrit-Change-Number: 903
Gerrit-PatchSet: 1
Gerrit-Owner: stipa
Gerrit-Reviewer: flichtenheld
Gerrit-Reviewer: plaisthos
Gerrit-CC: openvpn-devel
Gerrit-Attention: plaisthos
Gerrit-Attention: flichtenheld
Gerrit-MessageType: newchange
--2T89gShuNOo=
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Attention is currently required from: flichtenheld, plaisthos.
stipa would like plaisthos and flichtenheld to review t=
his change.
Vi=
ew Change
Make recursive routing check more fine-grained
The exi=
sting recursive routing check drops TUN packets
if their address matches=
the remote. While this works in
most cases, a more fine-grained check i=
s preferable for
complex routing rules.
Since we only need to dro=
p traffic originating from OpenVPN,
all of the following values must mat=
ch between the packet
and the link:
- IP protocol
- Transpor=
t protocol (TCP/UDP)
- Destination address
- Destination port
GitHub: #699
Change-Id: I6841e2f2a85275254a04e2d8ae3defe4420db8f6<=
br>Signed-off-by: Lev Stipakov <lev@...515...>
---
M src/openvp=
n/forward.c
1 file changed, 63 insertions(+), 38 deletions(-)
=
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/03/903/1
=
diff --git a/src/openvpn/forward.c b/src/openvpn/forward.c
index b025344..7ec3ef2 100644
--- a/src/openvpn/forward.=
c
+++ b/src/openvpn/forward.c
@@ -1386,74 +=
1386,99 @@
static void
drop_if_recursive_=
routing(struct context *c, struct buffer *buf)
{
- bool drop =3D false;=
- struct openvpn_sockaddr =
tun_sa;
- int ip_hdr=
_offset =3D 0;
+ s=
truct gc_arena gc =3D gc_new();
if (=
c->c2.to_link_addr =3D=3D NULL) /* no remote addr known */
{
- retu=
rn;
+ goto out=
;
}
- tun_sa =3D c->c2.to_link_addr->dest;<=
br>+ struct openvpn_sockaddr=
*link_addr =3D &c->c2.to_link_addr->dest;
+ struct link_socket_info *lsi =3D get_=
link_socket_info(c);
=
+ uint16_t link_port =3D atoi(c->c2.link_sockets[0]->remote_port);=
- =
int proto_ver =3D get_tun_ip_ver(TUNNEL_TYPE(c->c1.tuntap), &c->=
c2.buf, &ip_hdr_offset);
+ uint16_t src_port =3D 0, dst_port =3D 0;
- if (proto_ver =3D=3D =
4)
+ int ip_hdr_of=
fset =3D 0;
+ int =
tun_ip_ver =3D get_tun_ip_ver(TUNNEL_TYPE(c->c1.tuntap), &c->c2.b=
uf, &ip_hdr_offset);
+
+ if (tun_ip=
_ver =3D=3D 4)
{
- /* make sure we got whole IP header */
- if (BLEN(buf) < ((int) =
sizeof(struct openvpn_iphdr) + ip_hdr_offset))
+ /* make sure we got whole IP header and =
TCP/UDP src/dst ports */
+ if (BLEN(buf) < ((int) sizeof(struct openvpn_iphdr) + ip_hd=
r_offset + sizeof(uint16_t) * 2))
{
- return;
- }
-
- /* skip ipv4 packets for ipv6 tun */
- if (tun_sa.addr.sa.sa_family !=3D AF_INE=
T)
- {
- return;
<=
span style=3D"color: hsl(120, 100%, 40%);">+ goto out;
}
struct openvpn=
_iphdr *pip =3D (struct openvpn_iphdr *) (BPTR(buf) + ip_hdr_offset);
- /=
* drop packets with same dest addr as gateway */
- if (memcmp(&tun_sa.addr.in4.sin_addr=
.s_addr, &pip->daddr, sizeof(pip->daddr)) =3D=3D 0)
+ /* skip if tun protocol d=
oesn't match link protocol */
+ if ((lsi->proto =3D=3D PROTO_TCP && pip-&g=
t;protocol !=3D OPENVPN_IPPROTO_TCP)
+ || (lsi->proto =3D=3D PROTO_UDP && =
pip->protocol !=3D OPENVPN_IPPROTO_UDP))
{
- drop =3D true;=
+ goto ou=
t;
+ }<=
br>+
+ /* skip ipv4 packets for ipv6 tun */=
span>
+ if (link_addr=
->addr.sa.sa_family !=3D AF_INET)
+ {
+ goto out;
+ }
+<=
/span>
+ /* drop pack=
ets with same dest addr and port as remote */
+ uint8_t *l4_hdr =3D (uint8_t *)pip + size=
of(struct openvpn_iphdr);
+ uint16_t src_port =3D ntohs(*(uint16_t *)l4_hdr);
<=
span style=3D"color: hsl(120, 100%, 40%);">+ uint16_t dst_port =3D n=
tohs(*(uint16_t *)(l4_hdr + sizeof(uint16_t)));
+ if ((memcmp(&link_addr->addr.in4=
.sin_addr.s_addr, &pip->daddr, sizeof(pip->daddr)) =3D=3D 0) &=
;& (link_port =3D=3D dst_port))
+ {
+ c->c2.buf.len =3D 0;
+
+ msg(D_LOW, "Recursive routing detected, packet dropped =
%s:%" PRIu16 " -> %s",
+ print_in_addr_t(pip->saddr, IA_NET_=
ORDER, &gc),
+ =
src_port,
+ print_link_socket_actual(c->c2.to_link_addr, &gc)=
);
}
}
- else if (proto_ver =3D=3D 6)
=
+ else if (tun_ip_ver =3D=3D=
6)
{
- /* make sure we got whole IPv6 header */
- if (BLEN(buf) < ((int) sizeof(st=
ruct openvpn_ipv6hdr) + ip_hdr_offset))
+ /* make sure we got whole IPv6 header and TCP/U=
DP src/dst ports */
+=
if (BLEN(buf) < ((int) sizeof(struct openvpn_ipv6hdr) + ip_hdr_o=
ffset + sizeof(uint16_t) * 2))
{
- return;
+ goto out;
=
}
/* skip ipv6 packets f=
or ipv4 tun */
- =
if (tun_sa.addr.sa.sa_family !=3D AF_INET6)
+ if (link_addr->addr.sa.sa_family !=3D A=
F_INET6)
{
- return;
+ goto out;
}
struct openvpn_ipv6hdr *pip6 =3D (struct ope=
nvpn_ipv6hdr *) (BPTR(buf) + ip_hdr_offset);
- /* drop packets with same =
dest addr as gateway */
- if (OPENVPN_IN6_ARE_ADDR_EQUAL(&tun_sa.addr.in6.sin6_addr, &a=
mp;pip6->daddr))
+=
/* skip if tun protocol doesn't match link protocol */+ if ((lsi->proto =
=3D=3D PROTO_TCP && pip6->nexthdr !=3D OPENVPN_IPPROTO_TCP)
+ || (lsi->=
proto =3D=3D PROTO_UDP && pip6->nexthdr !=3D OPENVPN_IPPROTO_UDP=
))
{
- drop =3D true;
+ goto out;
+ }
+
+ /* d=
rop packets with same dest addr and port as remote */
+ uint8_t *l4_hdr =3D (uint8_t *)pi=
p6 + sizeof(struct openvpn_ipv6hdr);
+ src_port =3D ntohs(*(uint16_t *)l4_hdr);
+ dst_port =3D ntohs(*(=
uint16_t *)(l4_hdr + sizeof(uint16_t)));
+ if ((OPENVPN_IN6_ARE_ADDR_EQUAL(&link_addr=
->addr.in6.sin6_addr, &pip6->daddr)) && (link_port =3D=3D=
dst_port))
+ =
{
+ c->=
c2.buf.len =3D 0;
+=
span>
+ msg(D_LOW=
, "Recursive routing detected, packet dropped %s:%" PRIu16 "=
-> %s",
+ =
print_in6_addr(pip6->saddr, IA_NET_ORDER, &gc),<=
br>+ src_port,=
span>
+ print=
_link_socket_actual(c->c2.to_link_addr, &gc));
=
}
}
- if (drop)
- {
=
- struct gc_arena gc =3D gc_new();
-
- =
c->c2.buf.len =3D 0;
-
- msg(=
D_LOW, "Recursive routing detected, drop tun packet to %s",
- print_link_sock=
et_actual(c->c2.to_link_addr, &gc));
- gc_free(&gc);
- }
+out:
+ =
gc_free(&gc);
}
/*=
To view, visit change 903. To unsubscribe, =
or for help writing mail filters, visit settings.
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6841e2f2a85275254a04e2d8ae3=
defe4420db8f6
Gerrit-Change-Number: 903
Gerrit-PatchSet: 1
Gerrit-Owner: stipa <lstipakov@...277...>=
;
Gerrit-Reviewer: flichtenheld <frank@...2716...=
1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...=
.1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...=
ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...=
..1227...>
Gerrit-Attention: flichtenheld <frank@...2375...=
41...>
Gerrit-MessageType: newchange
--2T89gShuNOo=--