From a13f2b9768290a225c2de336521c0b8fd10b3938 Mon Sep 17 00:00:00 2001
From: zzz <zzz@i2pmail.org>
Date: Mon, 28 Feb 2022 12:32:36 -0500
Subject: [PATCH] SSU2 more fixes

Fix OES2 MTU
Fix Token Request header encryption
---
 .../udp/OutboundEstablishState2.java          | 27 +++++++++++++++++--
 .../router/transport/udp/PacketBuilder2.java  |  3 +--
 .../i2p/router/transport/udp/PeerState2.java  | 10 +++++++
 3 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/router/java/src/net/i2p/router/transport/udp/OutboundEstablishState2.java b/router/java/src/net/i2p/router/transport/udp/OutboundEstablishState2.java
index d8a2e3a1f3..2b11a019ee 100644
--- a/router/java/src/net/i2p/router/transport/udp/OutboundEstablishState2.java
+++ b/router/java/src/net/i2p/router/transport/udp/OutboundEstablishState2.java
@@ -72,7 +72,30 @@ class OutboundEstablishState2 extends OutboundEstablishState implements SSU2Payl
                 throw new IllegalArgumentException("bad IP", uhe);
             }
         }
-        _mtu = addr.getMTU();
+        // We need the MTU so the Session Confirmed can fit the RI in
+        int mtu = addr.getMTU();
+        if (mtu == 0) {
+            if (ra.getTransportStyle().equals("SSU2")) {
+                mtu = PeerState2.DEFAULT_MTU;
+            } else {
+                if (_bobIP.length == 16)
+                    mtu = PeerState2.DEFAULT_SSU_IPV6_MTU;
+                else
+                    mtu = PeerState2.DEFAULT_SSU_IPV4_MTU;
+            }
+        } else {
+            // TODO if too small, give up now
+            if (ra.getTransportStyle().equals("SSU2")) {
+                mtu = Math.min(Math.max(mtu, PeerState2.MIN_MTU), PeerState2.MAX_MTU);
+            } else {
+                if (_bobIP.length == 16)
+                    mtu = Math.min(Math.max(mtu, PeerState2.MIN_SSU_IPV6_MTU), PeerState2.MAX_SSU_IPV6_MTU);
+                else
+                    mtu = Math.min(Math.max(mtu, PeerState2.MIN_SSU_IPV4_MTU), PeerState2.MAX_SSU_IPV4_MTU);
+            }
+        }
+        _mtu = mtu;
+        // TODO if RI too big, give up now
         if (addr.getIntroducerCount() > 0) {
             if (_log.shouldLog(Log.DEBUG))
                 _log.debug("new outbound establish to " + remotePeer.calculateHash() + ", with address: " + addr);
@@ -87,13 +110,13 @@ class OutboundEstablishState2 extends OutboundEstablishState implements SSU2Payl
         do {
             rcid = ctx.random().nextLong();
         } while (_sendConnID == rcid);
+        _rcvConnID = rcid;
 
         _token = _transport.getEstablisher().getOutboundToken(_remotePeer.calculateHash());
         _routerAddress = ra;
         if (_token != 0)
             createNewState(ra);
 
-        _rcvConnID = rcid;
         byte[] ik = introKey.getData();
         _sendHeaderEncryptKey1 = ik;
         _rcvHeaderEncryptKey1 = ik;
diff --git a/router/java/src/net/i2p/router/transport/udp/PacketBuilder2.java b/router/java/src/net/i2p/router/transport/udp/PacketBuilder2.java
index 91cf5c8e0f..cf7c157a28 100644
--- a/router/java/src/net/i2p/router/transport/udp/PacketBuilder2.java
+++ b/router/java/src/net/i2p/router/transport/udp/PacketBuilder2.java
@@ -453,7 +453,6 @@ class PacketBuilder2 {
             len = info.length;
         }
 
-
         UDPPacket packets[] = new UDPPacket[numFragments];
         packets[0] = buildSessionConfirmedPacket(state, numFragments, info, len, gzip);
         if (numFragments > 1) {
@@ -909,7 +908,7 @@ class PacketBuilder2 {
                 _log.error("Bad token req msg out", gse);
             throw new RuntimeException("Bad token req msg out", gse);
         }
-        SSU2Header.encryptHandshakeHeader(packet, hdrKey1, hdrKey2);
+        SSU2Header.encryptLongHeader(packet, hdrKey1, hdrKey2);
     }
 
     /**
diff --git a/router/java/src/net/i2p/router/transport/udp/PeerState2.java b/router/java/src/net/i2p/router/transport/udp/PeerState2.java
index 86c590626f..f9c1e5d095 100644
--- a/router/java/src/net/i2p/router/transport/udp/PeerState2.java
+++ b/router/java/src/net/i2p/router/transport/udp/PeerState2.java
@@ -48,7 +48,17 @@ public class PeerState2 extends PeerState implements SSU2Payload.PayloadCallback
     private final SSU2Bitfield _ackedMessages;
     private byte[] _sessConfForReTX;
 
+    // As SSU
+    public static final int MIN_SSU_IPV4_MTU = 1292;
+    public static final int MAX_SSU_IPV4_MTU = 1484;
+    public static final int DEFAULT_SSU_IPV4_MTU = MAX_SSU_IPV4_MTU;
+    public static final int MIN_SSU_IPV6_MTU = 1280;
+    public static final int MAX_SSU_IPV6_MTU = 1488;
+    public static final int DEFAULT_SSU_IPV6_MTU = MIN_SSU_IPV6_MTU;  // should always be published
+    // As SSU2
     public static final int MIN_MTU = 1280;
+    public static final int MAX_MTU = 1500;
+    public static final int DEFAULT_MTU = MAX_MTU;
 
     /**
      *  @param rtt from the EstablishState, or 0 if not available
-- 
GitLab