Skip to content

Commit 2740395

Browse files
committed
only attach the ftp helper on static nat and port forwarded public ips
1 parent f706f0a commit 2740395

5 files changed

Lines changed: 21 additions & 26 deletions

File tree

‎systemvm/debian/opt/cloud/bin/configure.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,8 +1415,7 @@ def processForwardRule(self, rule):
14151415

14161416
def forward_ftp_helper(self, rule):
14171417
# Linux 6.0 no longer attaches the ftp helper after DNAT, so attach it
1418-
# to the public port that is forwarded to port 21. Public port 21 is
1419-
# already covered by the rule from CsAddress.py
1418+
# to the public port that is forwarded to port 21
14201419
if rule["protocol"] != "tcp" or "any" in [rule["public_ports"], rule["internal_ports"]]:
14211420
return
14221421
public_start = int(rule["public_ports"].split(":")[0])
@@ -1426,8 +1425,6 @@ def forward_ftp_helper(self, rule):
14261425
if not internal_start <= 21 <= internal_end:
14271426
return
14281427
public_port = public_start + 21 - internal_start
1429-
if public_port == 21:
1430-
return
14311428
self.fw.append(["raw", "", "-A PREROUTING -d %s/32 -p tcp -m tcp --dport %s -j CT --helper ftp" %
14321429
(rule["public_ip"], public_port)])
14331430

@@ -1574,6 +1571,7 @@ def processStaticNatRule(self, rule):
15741571
self.fw.append(["mangle", "front", "-A %s -d %s -j RETURN" %
15751572
(chain_name, static_route['network'])])
15761573

1574+
self.fw.append(["raw", "", "-A PREROUTING -d %s/32 -p tcp -m tcp --dport 21 -j CT --helper ftp" % rule["public_ip"]])
15771575
self.fw.append(["nat", "front",
15781576
"-A PREROUTING -d %s/32 -j DNAT --to-destination %s" % (rule["public_ip"], rule["internal_ip"])])
15791577
self.fw.append(["nat", "front",

‎systemvm/debian/opt/cloud/bin/cs/CsAddress.py‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,6 @@ def fw_router(self):
485485
"-A POSTROUTING " +
486486
"-p udp -m udp --dport 68 -j CHECKSUM --checksum-fill"])
487487

488-
self.fw.append(["raw", "", "-A PREROUTING -p tcp -m tcp --dport 21 -j CT --helper ftp"])
489-
490488
if self.get_type() in ["public"]:
491489
self.fw.append(["mangle", "front",
492490
"-A PREROUTING " +
@@ -568,7 +566,6 @@ def fw_vpcrouter(self):
568566
return
569567

570568
self.fw.append(["filter", "", "-A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT"])
571-
self.fw.append(["raw", "", "-A PREROUTING -p tcp -m tcp --dport 21 -j CT --helper ftp"])
572569

573570
if self.get_type() in ["guest"]:
574571
self.fw.append(["mangle", "front", "-A PREROUTING " +

‎systemvm/debian/opt/cloud/bin/setup/common.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ enable_fwding() {
165165
enable_passive_ftp() {
166166
log_it "cloud: enabling passive FTP for guest VMs"
167167
# Linux 6.0 removed the nf_conntrack_helper sysctl, the ftp helper is
168-
# attached to port 21 traffic by a raw table rule from CsAddress.py
168+
# attached by raw table rules for static nat and port forwarding
169169
modprobe nf_nat_ftp
170170
}
171171

‎systemvm/test/TestCsAddress.py‎

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
# under the License.
1717

1818
import unittest
19-
import mock
20-
from cs.CsAddress import CsAddress, CsIP
19+
from cs.CsAddress import CsAddress
2120
import merge
2221

2322

@@ -39,21 +38,6 @@ def test_get_guest_ip(self):
3938
def test_get_guest_netmask(self):
4039
self.assertTrue(self.csaddress.get_guest_netmask() == "255.255.255.0")
4140

42-
@mock.patch.object(CsIP, 'list')
43-
def test_ftp_helper_rule(self, _):
44-
ftp_rule = ["raw", "", "-A PREROUTING -p tcp -m tcp --dport 21 -j CT --helper ftp"]
45-
for is_vpc in [False, True]:
46-
config = mock.MagicMock()
47-
config.get_fw.return_value = []
48-
config.is_vpc.return_value = is_vpc
49-
config.is_routed.return_value = False
50-
config.is_dhcp.return_value = False
51-
ip = CsIP("eth2", config)
52-
ip.setAddress({"nw_type": "public", "public_ip": "10.0.0.2"})
53-
ip.fw_router()
54-
ip.fw_vpcrouter()
55-
self.assertIn(ftp_rule, ip.fw)
56-
5741

5842
if __name__ == '__main__':
5943
unittest.main()

‎systemvm/test/TestCsForwardingRules.py‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# under the License.
1717

1818
import unittest
19+
import mock
1920
from configure import CsForwardingRules
2021

2122

@@ -36,12 +37,27 @@ def test_ftp_helper_on_port_range(self):
3637
self.assertEqual(self.ftp_helper_rules("tcp", "1020:1030", "20:30"),
3738
[["raw", "", "-A PREROUTING -d 10.0.0.2/32 -p tcp -m tcp --dport 1021 -j CT --helper ftp"]])
3839

40+
def test_ftp_helper_on_port_21(self):
41+
self.assertEqual(self.ftp_helper_rules("tcp", "21:21", "21:21"),
42+
[["raw", "", "-A PREROUTING -d 10.0.0.2/32 -p tcp -m tcp --dport 21 -j CT --helper ftp"]])
43+
3944
def test_no_ftp_helper(self):
40-
self.assertEqual(self.ftp_helper_rules("tcp", "21:21", "21:21"), [])
4145
self.assertEqual(self.ftp_helper_rules("tcp", "2121:2121", "2121:2121"), [])
4246
self.assertEqual(self.ftp_helper_rules("udp", "2121:2121", "21:21"), [])
4347
self.assertEqual(self.ftp_helper_rules("tcp", "any", "any"), [])
4448

49+
@mock.patch.object(CsForwardingRules, 'getStaticRoutes', return_value=[])
50+
@mock.patch.object(CsForwardingRules, 'getPrivateGatewayNetworks', return_value=[])
51+
@mock.patch.object(CsForwardingRules, 'getGuestIpByIp', return_value="10.1.1.1")
52+
@mock.patch.object(CsForwardingRules, 'getNetworkByIp', return_value="10.1.1.0/24")
53+
@mock.patch.object(CsForwardingRules, 'getDeviceByIp', return_value="eth2")
54+
def test_ftp_helper_on_static_nat(self, *_):
55+
forwarding = CsForwardingRules.__new__(CsForwardingRules)
56+
forwarding.fw = []
57+
forwarding.processStaticNatRule({"public_ip": "10.0.0.2", "internal_ip": "10.1.1.10"})
58+
self.assertIn(["raw", "", "-A PREROUTING -d 10.0.0.2/32 -p tcp -m tcp --dport 21 -j CT --helper ftp"],
59+
forwarding.fw)
60+
4561

4662
if __name__ == '__main__':
4763
unittest.main()

0 commit comments

Comments
 (0)