Skip to content

Commit 0486807

Browse files
committed
fix(NSC): cleanup historical bad IPv6 TCPMSS vals
1 parent 8aaba65 commit 0486807

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

pkg/controllers/proxy/dsr_tcpmss_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,6 @@ func TestSetupMangleTableRule_LegacyCleanup_PREROUTING(t *testing.T) {
446446
// TestSetupMangleTableRule_UpgradeCleanup_IncorrectIPv6MSS tests that when upgrading
447447
// from a version that had the bug (using MTU-60 for IPv6 instead of MTU-100),
448448
// the old incorrect rules are cleaned up.
449-
//
450-
// EXPECTED TO FAIL: The current implementation does not clean up old IPv6 rules
451-
// that were created with the incorrect MSS value.
452449
func TestSetupMangleTableRule_UpgradeCleanup_IncorrectIPv6MSS(t *testing.T) {
453450
t.Parallel()
454451
existsRules := make(map[string]bool)
@@ -478,9 +475,7 @@ func TestSetupMangleTableRule_UpgradeCleanup_IncorrectIPv6MSS(t *testing.T) {
478475
}
479476
}
480477
assert.Contains(t, deletedMSSValues, oldIncorrectIPv6MSS,
481-
"Expected old incorrect IPv6 TCPMSS rule with MSS %s to be deleted, "+
482-
"but deleted MSS values were: %v. "+
483-
"This test is expected to fail until the cleanup gap is fixed.",
478+
"Expected old incorrect IPv6 TCPMSS rule with MSS %s to be deleted, but deleted MSS values were: %v",
484479
oldIncorrectIPv6MSS, deletedMSSValues)
485480
}
486481

pkg/controllers/proxy/network_services_controller.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,57 @@ func (nsc *NetworkServicesController) setupMangleTableRule(ip string, protocol s
15951595
}
15961596
}
15971597

1598+
// Clean up old IPv6 rules that were created with the incorrect MSS value (MTU-60 instead of MTU-100).
1599+
// Prior to commit ee0940b8, IPv6 rules incorrectly used the same MSS calculation as IPv4 (MTU-60),
1600+
// resulting in IPv6 rules with MSS=1440 instead of the correct MSS=1400 (for MTU=1500).
1601+
// This cleanup ensures these old incorrect rules are removed during upgrades.
1602+
// TODO: remove after v2.4.X or above
1603+
if parsedIP.To4() == nil && protocol == tcpProtocol {
1604+
// Calculate what the old incorrect MSS would have been (using IPv4 calculation for IPv6)
1605+
oldIncorrectIPv6MSS := nsc.mtu - 2*ipv4.HeaderLen - tcpHeaderMinLen
1606+
1607+
// Only proceed if the old incorrect value differs from the current correct value
1608+
if oldIncorrectIPv6MSS != tcpMSS {
1609+
klog.V(2).Infof("checking for old incorrect IPv6 TCPMSS rules with MSS %d (correct is %d)",
1610+
oldIncorrectIPv6MSS, tcpMSS)
1611+
1612+
// Check for old-style rules (POSTROUTING with -s, PREROUTING with -d)
1613+
for firstArg, chain := range map[string]string{"-s": "POSTROUTING", "-d": "PREROUTING"} {
1614+
oldIncorrectArgs := []string{firstArg, ip, "-m", tcpProtocol, "-p", tcpProtocol,
1615+
"--tcp-flags", "SYN,RST", "SYN", "-j", "TCPMSS", "--set-mss", strconv.Itoa(oldIncorrectIPv6MSS)}
1616+
exists, err := iptablesCmdHandler.Exists("mangle", chain, oldIncorrectArgs...)
1617+
if err != nil {
1618+
return fmt.Errorf("failed to check for old incorrect IPv6 TCPMSS rule in %s due to %v", chain, err)
1619+
}
1620+
if exists {
1621+
klog.V(2).Infof("removing old incorrect IPv6 TCPMSS rule with MSS %d: ip6tables -D %s -t mangle %v",
1622+
oldIncorrectIPv6MSS, chain, oldIncorrectArgs)
1623+
err = iptablesCmdHandler.Delete("mangle", chain, oldIncorrectArgs...)
1624+
if err != nil {
1625+
return fmt.Errorf("failed to delete old incorrect IPv6 TCPMSS rule from %s due to %v", chain, err)
1626+
}
1627+
}
1628+
}
1629+
1630+
// Check for current-style rule format with incorrect MSS
1631+
currentFormatOldMSS := []string{"-s", ip, "-m", tcpProtocol, "-p", tcpProtocol, "--sport", port,
1632+
"-i", "kube-bridge", "--tcp-flags", "SYN,RST", "SYN", "-j", "TCPMSS",
1633+
"--set-mss", strconv.Itoa(oldIncorrectIPv6MSS)}
1634+
exists, err := iptablesCmdHandler.Exists("mangle", "PREROUTING", currentFormatOldMSS...)
1635+
if err != nil {
1636+
return fmt.Errorf("failed to check for old incorrect IPv6 TCPMSS rule (current format) due to %v", err)
1637+
}
1638+
if exists {
1639+
klog.V(2).Infof("removing old incorrect IPv6 TCPMSS rule (current format) with MSS %d",
1640+
oldIncorrectIPv6MSS)
1641+
err = iptablesCmdHandler.Delete("mangle", "PREROUTING", currentFormatOldMSS...)
1642+
if err != nil {
1643+
return fmt.Errorf("failed to delete old incorrect IPv6 TCPMSS rule (current format) due to %v", err)
1644+
}
1645+
}
1646+
}
1647+
}
1648+
15981649
return nil
15991650
}
16001651

0 commit comments

Comments
 (0)