Skip to content

Commit 698614a

Browse files
committed
kvm: fix security group conntrack NOTRACK optimization triggered by single-direction allow-all rule
The raw-table NOTRACK rules for a VM IP in the cs_notrack/cs_notrack6 ipsets apply to that IP as both source and destination, disabling connection tracking for it in both directions at once. The check that populates these ipsets did not look at rule['ruletype'], so an allow-all rule in only one direction (e.g. egress all to 0.0.0.0/0) was enough to disable conntrack for the VM, breaking return traffic when the other direction (e.g. ingress) is restricted. Only skip conntrack for a family now when both an ingress allow-all and an egress allow-all rule exist for that family, matching the actual bidirectional effect of the NOTRACK rules.
1 parent c77e16a commit 698614a

1 file changed

Lines changed: 18 additions & 16 deletions

File tree

scripts/vm/network/security_group.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,23 +1136,25 @@ def add_network_rules(vm_name, vm_id, vm_ip, vm_ip6, signature, seqno, vmMac, ru
11361136
ip4s, ip6s = split_ips_by_family(vm_ip, vm_ip6, sec_ips, str(ipv6_link_local_addr(vmMac)))
11371137

11381138
rules = parse_network_rules(rules)
1139-
conntrack4_not_needed = False
1140-
conntrack6_not_needed = False
1139+
ingress4_allow_all = False
1140+
egress4_allow_all = False
1141+
ingress6_allow_all = False
1142+
egress6_allow_all = False
11411143
for rule in rules:
1142-
"""
1143-
If any of the rules has an explicit allow all protocols from 0.0.0.0/0 (ipv4)
1144-
or ::/0 (ipv6), then that IP family doesn't need its connection tracked
1145-
Example contents of the rules list:
1146-
[
1147-
{'ipv4': ['1.0.0.0/24', '0.0.0.0/0'], 'ipv6': ['::/0'], 'ruletype': 'I', 'start': 0, 'end': 0, 'protocol': 'all'},
1148-
{'ipv4': ['1.1.1.1/32'], 'ipv6': [], 'ruletype': 'I', 'start': 1, 'end': 65535, 'protocol': 'tcp'},
1149-
{'ipv4': [], 'ipv6': ['2001:db8::/32'], 'ruletype': 'I', 'start': 2000, 'end': 3000, 'protocol': 'tcp'}
1150-
]
1151-
"""
1152-
if '0.0.0.0/0' in rule['ipv4'] and rule['protocol'].lower() == 'all':
1153-
conntrack4_not_needed = True
1154-
if '::/0' in rule['ipv6'] and rule['protocol'].lower() == 'all':
1155-
conntrack6_not_needed = True
1144+
if rule['protocol'].lower() == 'all':
1145+
if '0.0.0.0/0' in rule['ipv4']:
1146+
if rule['ruletype'] == 'E':
1147+
egress4_allow_all = True
1148+
else:
1149+
ingress4_allow_all = True
1150+
if '::/0' in rule['ipv6']:
1151+
if rule['ruletype'] == 'E':
1152+
egress6_allow_all = True
1153+
else:
1154+
ingress6_allow_all = True
1155+
1156+
conntrack4_not_needed = ingress4_allow_all and egress4_allow_all
1157+
conntrack6_not_needed = ingress6_allow_all and egress6_allow_all
11561158

11571159
if conntrack4_not_needed:
11581160
add_to_ipset(NOTRACK_IPV4_IPSET, ip4s, "add")

0 commit comments

Comments
 (0)