From: "cron2 (Code Review)" Date: Sun, 16 Nov 2025 11:21:31 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., lstipakov@...277... Message-ID: <81d5840f473e97c885496e298496d194af1b7b3a-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="jMk3L6+p3gU="; charset=UTF-8 Subject: [Openvpn-devel] [S] Change in openvpn[master]: recursive routing: fixes and clean-ups List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: stipa , plaisthos , flichtenheld Cc: openvpn-devel --jMk3L6+p3gU= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#3) to the change originally created by= stipa. ( http://gerrit.openvpn.net/c/openvpn/+/1377?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by flichtenheld Change subject: recursive routing: fixes and clean-ups ...................................................................... recursive routing: fixes and clean-ups - get rid of atoi() for getting the remote transport port. It doesn't change, so no point to do in on every packet. In addition, atoi() breaks when we use service names as ports. - ensure we correctly handle IPv4 headers with options - directly use buf parameter in place of c->c2.buf GitHub: closes OpenVPN/openvpn#902 Change-Id: I8a0a8029da02fc63adc918e8d98e9f676ff4ea0d Signed-off-by: Lev Stipakov Acked-by: Frank Lichtenheld Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1377 Message-Id: <20251114115029.17432-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg34= 415.html Signed-off-by: Gert Doering --- M src/openvpn/forward.c 1 file changed, 22 insertions(+), 11 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/77/1377/3 diff --git a/src/openvpn/forward.c b/src/openvpn/forward.c index aa1f858..90e52d2 100644 --- a/src/openvpn/forward.c +++ b/src/openvpn/forward.c @@ -1382,15 +1382,24 @@ 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 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); + int tun_ip_ver =3D get_tun_ip_ver(TUNNEL_TYPE(c->c1.tuntap), buf, &ip_= hdr_offset); if (tun_ip_ver =3D=3D 4) { - /* make sure we got whole IP header and TCP/UDP src/dst ports */ - if (BLEN(buf) < ((int)sizeof(struct openvpn_iphdr) + ip_hdr_offset= + sizeof(uint16_t) * 2)) + /* Ensure we can safely read the IPv4 header */ + const int min_ip_header =3D ip_hdr_offset + sizeof(struct openvpn_= iphdr); + if (BLEN(buf) < min_ip_header) + { + return; + } + + struct openvpn_iphdr *pip =3D (struct openvpn_iphdr *)(BPTR(buf) += ip_hdr_offset); + const int ip_hlen =3D OPENVPN_IPH_GET_LEN(pip->version_len); + /* Reject malformed or truncated headers */ + if (ip_hlen < sizeof(struct openvpn_iphdr) + || BLEN(buf) < (int)(ip_hdr_offset + ip_hlen + sizeof(uint16_t= ) * 2)) { return; } @@ -1401,8 +1410,6 @@ return; } - struct openvpn_iphdr *pip =3D (struct openvpn_iphdr *)(BPTR(buf) += ip_hdr_offset); - /* 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)) @@ -1410,9 +1417,10 @@ return; } - /* drop packets with same dest addr and port as remote */ - uint8_t *l4_hdr =3D (uint8_t *)pip + sizeof(struct openvpn_iphdr); + uint8_t *l4_hdr =3D (uint8_t *)pip + ip_hlen; + + uint16_t link_port =3D ntohs(link_addr->addr.in4.sin_port); /* TCP and UDP ports are at the same place in the header, and othe= r protocols * can not happen here due to the lsi->proto check above */ @@ -1420,7 +1428,7 @@ 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; + buf->len =3D 0; struct gc_arena gc =3D gc_new(); msg(D_LOW, "Recursive routing detected, packet dropped %s:%" P= RIu16 " -> %s", @@ -1433,7 +1441,8 @@ else if (tun_ip_ver =3D=3D 6) { /* make sure we got whole IPv6 header and TCP/UDP src/dst ports */ - if (BLEN(buf) < ((int)sizeof(struct openvpn_ipv6hdr) + ip_hdr_offs= et + sizeof(uint16_t) * 2)) + const int min_ipv6 =3D ip_hdr_offset + sizeof(struct openvpn_ipv6h= dr) + sizeof(uint16_t) * 2; + if (BLEN(buf) < min_ipv6) { return; } @@ -1453,13 +1462,15 @@ return; } + uint16_t link_port =3D ntohs(link_addr->addr.in6.sin6_port); + /* drop packets with same dest addr and port as remote */ uint8_t *l4_hdr =3D (uint8_t *)pip6 + sizeof(struct openvpn_ipv6hd= r); uint16_t src_port =3D ntohs(*(uint16_t *)l4_hdr); uint16_t 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; + buf->len =3D 0; struct gc_arena gc =3D gc_new(); msg(D_LOW, "Recursive routing detected, packet dropped %s:%" P= RIu16 " -> %s", -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1377?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: newpatchset Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I8a0a8029da02fc63adc918e8d98e9f676ff4ea0d Gerrit-Change-Number: 1377 Gerrit-PatchSet: 3 Gerrit-Owner: stipa Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel --jMk3L6+p3gU= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 upl= oaded patch set #3 to the change originally created by stipa.

<= p>View C= hange

The following approvals got outdated and were removed: Code-Review+2 by flichtenheld

recursive routing: fixes and=
 clean-ups

- get rid of atoi() for getting the remote transport por= t.
It doesn't change, so no point to do in on every packet.
In = addition, atoi() breaks when we use service names as ports.

- ensur= e we correctly handle IPv4 headers with options

- directly use buf = parameter in place of c->c2.buf

GitHub: closes OpenVPN/openvpn#90= 2

Change-Id: I8a0a8029da02fc63adc918e8d98e9f676ff4ea0d
Signed-off= -by: Lev Stipakov <lev@...515...>
Acked-by: Frank Lichtenheld <= frank@...2641...>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/= 1377
Message-Id: <20251114115029.17432-1-gert@...1296...>
URL: = https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg34415.h= tml
Signed-off-by: Gert Doering <gert@...1296...>
---
M src/= openvpn/forward.c
1 file changed, 22 insertions(+), 11 deletions(-)
<= br>
git pull ssh://gerrit.openvpn.net:29418/openvpn refs=
/changes/77/1377/3
diff --git a/src/openvpn/forward.c b/src/openvpn=
/forward.c
index aa1f858..90e52d2 100644
--= - a/src/openvpn/forward.c
+++ b/src/openvpn/forward.c
@@ -1382,15 +1382,24 @@

s= truct openvpn_sockaddr *link_addr =3D &c->c2.to_link_addr->dest;<= /span>
struct link_socket_info *lsi =3D get_link_socket_info(= c);
- uint16_t link_= port =3D atoi(c->c2.link_sockets[0]->remote_port);
<= /span>
int ip_hdr_offset =3D 0;
- int tun_ip_ver =3D get_tun_ip_ver(TUNNEL_TYPE(c= ->c1.tuntap), &c->c2.buf, &ip_hdr_offset);
+ int tun_ip_ver =3D get_tun_ip_ver(= TUNNEL_TYPE(c->c1.tuntap), buf, &ip_hdr_offset);

if (tun_ip_ver =3D=3D 4)
{<= br>- /* make sure we got w= hole IP header and TCP/UDP src/dst ports */
- if (BLEN(buf) < ((int)sizeof(struct openvp= n_iphdr) + ip_hdr_offset + sizeof(uint16_t) * 2))
+ /* Ensure we can safely read the IPv4= header */
+ c= onst int min_ip_header =3D ip_hdr_offset + sizeof(struct openvpn_iphdr);
+ if (BLEN(buf) = < min_ip_header)
+= {
+ = return;
+ }<= /span>
+
+ struct openvpn_iphdr *pip =3D (= struct openvpn_iphdr *)(BPTR(buf) + ip_hdr_offset);
+ const int ip_hlen =3D OPENVPN_IPH= _GET_LEN(pip->version_len);
+ /* Reject malformed or truncated headers */
+ if (ip_hlen < sizeof(= struct openvpn_iphdr)

+ || BLEN(buf) < (int)(ip_hdr_offset + ip_hlen + sizeof(uint= 16_t) * 2))
{
return;=
}
@@ -1401,8 +1410,6 @@ return;
}

- struct openvpn_i= phdr *pip =3D (struct openvpn_iphdr *)(BPTR(buf) + ip_hdr_offset);-
/* s= kip if tun protocol doesn't match link protocol */
= if ((lsi->proto =3D=3D PROTO_TCP && pip->protocol !=3D O= PENVPN_IPPROTO_TCP)
|| (lsi->proto =3D=3D P= ROTO_UDP && pip->protocol !=3D OPENVPN_IPPROTO_UDP))
<= span>@@ -1410,9 +1417,10 @@

return;
= }

-
/* drop packets with same dest addr = and port as remote */
-= uint8_t *l4_hdr =3D (uint8_t *)pip + sizeof(struct openvpn_iphdr);<= /span>
+ uint8_t *l4_= hdr =3D (uint8_t *)pip + ip_hlen;
+
+ = uint16_t link_port =3D ntohs(link_addr->addr.in4.sin_port);
/* TCP and UDP ports are at the same plac= e in the header, and other protocols
* can not ha= ppen here due to the lsi->proto check above */
@@ -1420,= 7 +1428,7 @@
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, sizeof(pip->= ;daddr)) =3D=3D 0) && (link_port =3D=3D dst_port))
= {
- = c->c2.buf.len =3D 0;
+ buf->len =3D 0;

= struct gc_arena gc =3D gc_new();
msg(D= _LOW, "Recursive routing detected, packet dropped %s:%" PRIu16 &q= uot; -> %s",
@@ -1433,7 +1441,8 @@
= else if (tun_ip_ver =3D=3D 6)
{
= /* make sure we got whole IPv6 header and TCP/UDP src/dst ports */<= /span>
- if (BLEN(buf) = < ((int)sizeof(struct openvpn_ipv6hdr) + ip_hdr_offset + sizeof(uint16_t= ) * 2))
+ cons= t int min_ipv6 =3D ip_hdr_offset + sizeof(struct openvpn_ipv6hdr) + sizeof(= uint16_t) * 2;
+ = if (BLEN(buf) < min_ipv6)
{
= return;
}
@@ -1453,13= +1462,15 @@
return;
= }

+= uint16_t link_port =3D ntohs(link_addr->addr.in6.sin6_port);
+
= /* drop packets with same dest addr and port as remote */
uint8_t *l4_hdr =3D (uint8_t *)pip6 + sizeof(struct openvpn_ipv6= hdr);

uint16_t src_port =3D ntohs(*(uint16_t *)l4_= hdr);
uint16_t dst_port =3D ntohs(*(uint16_t *)(l4= _hdr + sizeof(uint16_t)));
if ((OPENVPN_IN6_ARE_AD= DR_EQUAL(&link_addr->addr.in6.sin6_addr, &pip6->daddr)) &= & (link_port =3D=3D dst_port))
{
- c->c2.buf.len =3D 0;=

+ buf->= ;len =3D 0;

struct gc_arena = gc =3D gc_new();
msg(D_LOW, "Recursive ro= uting detected, packet dropped %s:%" PRIu16 " -> %s",

To view, visit change 1377. To unsubscribe, or= for help writing mail filters, visit settings.

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I8a0a8029da02fc63adc918e8d98= e9f676ff4ea0d
Gerrit-Change-Number: 1377
Gerrit-PatchSet: 3
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>
= --jMk3L6+p3gU=--