I patched OpenBSD 7.9-stable with the following on all of my OpenBSD
7.9-stable machines after running several tests that show the WireGuard
implementation is closer to Linux's:

diff --git a/sys/net/if_wg.c b/sys/net/if_wg.c
index 69f746e505b..9216d318f8e 100644
--- a/sys/net/if_wg.c
+++ b/sys/net/if_wg.c
@@ -1593,8 +1593,19 @@ wg_encap(struct wg_softc *sc, struct mbuf *m)
        t = wg_tag_get(m);
        peer = t->t_peer;

-       plaintext_len = WG_PKT_WITH_PADDING(m->m_pkthdr.len);
-       padding_len = plaintext_len - m->m_pkthdr.len;
+       if (m->m_pkthdr.len <= t->t_mtu) {
+               plaintext_len = min(WG_PKT_WITH_PADDING(m->m_pkthdr.len), 
t->t_mtu);
+               padding_len = plaintext_len - m->m_pkthdr.len;
+       } else {
+               /* We do this modulo business with the MTU, just in case the 
networking
+                * layer gives us a packet that's bigger than the MTU. In that 
case, we
+                * wouldn't want the final subtraction to overflow in the case 
of the
+                * padded_size being clamped.
+                */
+               padding_len = m->m_pkthdr.len % t->t_mtu;
+               padding_len = min(WG_PKT_WITH_PADDING(padding_len), t->t_mtu) - 
padding_len;
+               plaintext_len = m->m_pkthdr.len + padding_len;
+       }
        out_len = sizeof(struct wg_pkt_data) + plaintext_len +
            NOISE_AUTHTAG_LEN;

The if branch is exactly what Claudio suggested[^1], but the else branch
was written by me to also align the behavior with Linux when the
plaintext exceeds the interface's MTU. Note the else branch-more
accurately getting the IP layer to pass the WireGuard code a plaintext
that exceeds the MTU used by the WireGuard code-was rather tricky to
test if you're not a Linux or OpenBSD networking guru; as a result I had
to consult with an LLM-Claude Opus 5.5 if that's relevant-to design a
test using ping(8) as I tried for almost an hour without any success.
The if branch is rather trivial to test using ping6(8)/ping(8) though.

Using a 1500 MTU for the underlay, 1420 MTU for the WireGuard
interfaces, and IPv6 to establish the tunnel,
ping6 -mc1 -s 1372 fe80::1%wg0 where fe80::1 is the IP of the Linux peer
produces the following tcpdump(8) when listening on the physical
interface of the OpenBSD peer _without_ the above patch:

14:49:07.573565 2601:283:4e00:fdd1::2 > 2601:283:4e00:fdd4::2: frag 
(0x434a92af:1448@0+) 2601:283:4e00:fdd1::2.35011 > 2601:283:4e00:fdd4::2.35011:  
truncated-udp - 16 bytes missing![wg] data length 1408 to 0x6389640d nonce 2 (len 
1456, hlim 64)
14:49:07.573566 2601:283:4e00:fdd1::2 > 2601:283:4e00:fdd4::2: frag 
(0x434a92af:16@1448) (len 24, hlim 64)
14:49:07.575117 2601:283:4e00:fdd4::2.35011 > 2601:283:4e00:fdd1::2.35011: [wg] 
data length 1420 to 0x3e9b1de2 nonce 1 (len 1460, hlim 63)

We see OpenBSD sending fragmented packets while Linux is able to send a
single packet with length equal to the MTU of the WireGuard interface.

In contrast the same command produces the following tcpdump(8) when a
kernel is used that contains the proposed patch:

14:34:35.150068 2601:283:4e00:fdd1::2.35011 > 2601:283:4e00:fdd4::2.35011: [wg] 
data length 1420 to 0xfce12b6b nonce 2 (len 1460, hlim 64)
14:34:35.151167 2601:283:4e00:fdd4::2.35011 > 2601:283:4e00:fdd1::2.35011: [wg] 
data length 1420 to 0x04b51c88 nonce 1 (len 1460, hlim 63)

Both peers send a single 1420-byte payload in the WireGuard tunnel.

The else branch is trickier to trigger. The LLM suggested that the MTU
that is used by the WireGuard code is different for Linux than OpenBSD.
In particular it stated that the OpenBSD code always uses the MTU of
wg(4) when deciding if/how padding should be performed; while Linux uses
the route MTU which itself is based on the following in descending
order of priority:

* PMTUD exceptions
* Route MTU metric
* MTU of the interface

In addition to that, Linux offers a PROBE mode that can be used to
override the default behavior of the IP stack. Per ping(8) on my Linux
laptop:

-M pmtudisc_opt
   ...probe (set DF flag and bypass PMTU checks, useful for probing)...

The LLM claimed that OpenBSD does not have such a mode-ping6(8)/ping(8)
certainly doesn't report such an option. Last the LLM claimed that while
like Linux the route MTU is used by IPv6 and IPv4 in OpenBSD, the route
MTU is clamped down to the interface MTU in the event it exceeds it for
IPv6. A consequence of this is that the else branch can't be reached
when IPv6 is used in the tunnel at least for "normal" setups. Note the
IP version used to establish the tunnel itself is only relevant in
determining which payloads will cause fragmentation on the underlay
but otherwise does not matter.

With that info one can trigger the else branch by doing the following
assuming the underlays of both peers have an MTU of 1500:

Linux peer:

* Use normal WireGuard config including 1420 MTU.
* Create a route to the OpenBSD peer with an MTU of 1404.
* ping -4c1 -M probe -s 1377 10.0.4.2

OpenBSD peer:

* Use WireGuard config with 1404 MTU.
* Create a route to the Linux peer with an MTU of 1420.

Without the proposed patch, tcpdump(8) produces the following:

13:46:25.958808 2601:283:4e00:fdd4::2.35011 > 2601:283:4e00:fdd1::2.35011: [wg] 
data length 1420 to 0xc51a8057 nonce 1 (len 1460, hlim 63)
13:46:25.958904 2601:283:4e00:fdd1::2.35011 > 2601:283:4e00:fdd4::2.35011: [wg] 
data length 1408 to 0xc2e5172f nonce 2 (len 1448, hlim 64)

We see the Linux peer pad the 1377 + 28 = 1405 % 1404 = 1 byte payload
with fifteen 0-octets causing the whole payload to be 1405 + 15 = 1420
bytes; in contrast we see OpenBSD pad the 1405-byte payload to the
nearest multiple of 16 (i.e., 1408).

With the proposed patch, tcpdump(8) produces the following:

14:39:57.997901 2601:283:4e00:fdd4::2.35011 > 2601:283:4e00:fdd1::2.35011: [wg] 
data length 1420 to 0xa2890d82 nonce 2 (len 1460, hlim 63)
14:39:57.998269 2601:283:4e00:fdd1::2.35011 > 2601:283:4e00:fdd4::2.35011: [wg] 
data length 1420 to 0xb9b0c341 nonce 2 (len 1460, hlim 64)

We see both peers pad the plaintext based on the length of the residual
segment (i.e., 1) and not the entire payload producing consistent
behavior.

Personally I don't find the else branch _that_ complicated, so I kept
it especially since it produces more consistent behavior with Linux.
However due to the fact the else branch seems to rely on an admin
seemingly misconfiguring their system (i.e., creating a route with an
MTU that's greater than the interface's MTU), it doesn't seem _that_
crazy to ignore that case and simply pad to the nearest multiple of 16.

[^1]: https://marc.info/?l=openbsd-bugs&m=178937674771556&w=2

Reply via email to