Skip to content

Commit 9e3a1cb

Browse files
author
Rene Diepstraten
committed
Put secondary ipv6 addresses in right ipset
1 parent 6a40e90 commit 9e3a1cb

1 file changed

Lines changed: 47 additions & 19 deletions

File tree

scripts/vm/network/security_group.py

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,21 @@ def ipv6_link_local_addr(mac=None):
167167
return IPAddress('fe80::' + ':'.join(re.findall(r'.{4}', eui64)))
168168

169169

170+
def split_ips_by_family(ips):
171+
if type(ips) is str:
172+
ips = [ip for ip in ips.split(';') if ip != '']
173+
174+
ip4s = []
175+
ip6s = []
176+
for ip in ips:
177+
version = IPNetwork(ip).version
178+
if version == 4:
179+
ip4s.append(ip)
180+
elif version == 6:
181+
ip6s.append(ip)
182+
return ip4s, ip6s
183+
184+
170185
def destroy_network_rules_for_vm(vm_name, vif=None):
171186
vmchain = iptables_chain_name(vm_name)
172187
vmchain_egress = egress_chain_name(vm_name)
@@ -405,10 +420,17 @@ def network_rules_vmSecondaryIp(vm_name, ip_secondary, action):
405420
domid = getvmId(vm_name)
406421

407422
vmchain = vm_name
408-
add_to_ipset(vmchain, [ip_secondary], action)
423+
vmchain6 = vmchain + '-6'
424+
425+
ip4s, ip6s = split_ips_by_family(ip_secondary)
426+
427+
add_to_ipset(vmchain, ip4s, action)
409428

410-
#add ebtables rules for the secondary ip
411-
ebtables_rules_vmip(vm_name, [ip_secondary], action)
429+
#add ebtables rules for the secondary ips
430+
ebtables_rules_vmip(vm_name, ip4s, action)
431+
432+
#add ipv6 addresses to ipv6 ipset
433+
add_to_ipset(vmchain6, ip6s, action)
412434

413435
return True
414436

@@ -460,6 +482,8 @@ def default_network_rules(vm_name, vm_id, vm_ip, vm_ip6, vm_mac, vif, brname, se
460482

461483
action = "-A"
462484
vmipsetName = ipset_chain_name(vm_name)
485+
vmipsetName6 = vmipsetName + '-6'
486+
463487
#create ipset and add vm ips to that ip set
464488
if not create_ipset_forvm(vmipsetName):
465489
logging.debug(" failed to create ipset for rule " + str(tokens))
@@ -478,8 +502,11 @@ def default_network_rules(vm_name, vm_id, vm_ip, vm_ip6, vm_mac, vif, brname, se
478502
secIpSet = "0"
479503

480504
if secIpSet == "1":
481-
logging.debug("Adding ipset for secondary ips")
482-
add_to_ipset(vmipsetName, ips, action)
505+
logging.debug("Adding ipset for secondary ipv4 addresses")
506+
ip4s, ip6s = split_ips_by_family(ips)
507+
508+
add_to_ipset(vmipsetName, ip4s, action)
509+
483510
if not write_secip_log_for_vm(vm_name, sec_ips, vm_id):
484511
logging.debug("Failed to log default network rules, ignoring")
485512

@@ -505,15 +532,13 @@ def default_network_rules(vm_name, vm_id, vm_ip, vm_ip6, vm_mac, vif, brname, se
505532

506533
default_ebtables_rules(vm_name, vm_ip, vm_mac, vif)
507534
#default ebtables rules for vm secondary ips
508-
ebtables_rules_vmip(vm_name, ips, "-I")
535+
ebtables_rules_vmip(vm_name, ip4s, "-I")
509536

510537
if vm_ip:
511538
if not write_rule_log_for_vm(vmName, vm_id, vm_ip, domID, '_initial_', '-1'):
512539
logging.debug("Failed to log default network rules, ignoring")
513540

514-
vm_ip6_set_name = vm_name + '-6'
515-
516-
if not create_ipset_forvm(vm_ip6_set_name, family='inet6', type='hash:net'):
541+
if not create_ipset_forvm(vmipsetName6, family='inet6', type='hash:net'):
517542
logging.debug(" failed to create ivp6 ipset for rule " + str(tokens))
518543
return False
519544

@@ -525,7 +550,10 @@ def default_network_rules(vm_name, vm_id, vm_ip, vm_ip6, vm_mac, vif, brname, se
525550
except AddrFormatError:
526551
pass
527552

528-
add_to_ipset(vm_ip6_set_name, vm_ip6_addr, action)
553+
add_to_ipset(vmipsetName6, vm_ip6_addr, action)
554+
if secIpSet == "1":
555+
logging.debug("Adding ipset for secondary ipv6 addresses")
556+
add_to_ipset(vmipsetName6, ip6s, action)
529557

530558
try:
531559
execute('ip6tables -A ' + brfw + '-OUT' + ' -m physdev --physdev-is-bridged --physdev-out ' + vif + ' -j ' + vmchain_default)
@@ -540,20 +568,20 @@ def default_network_rules(vm_name, vm_id, vm_ip, vm_ip6, vm_mac, vif, brname, se
540568
# Allow neighbor solicitations and advertisements
541569
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-in ' + vif + ' -p icmpv6 --icmpv6-type neighbor-solicitation -m hl --hl-eq 255 -j RETURN')
542570
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-out ' + vif + ' -p icmpv6 --icmpv6-type neighbor-solicitation -m hl --hl-eq 255 -j ACCEPT')
543-
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-in ' + vif + ' -p icmpv6 --icmpv6-type neighbor-advertisement -m set --match-set ' + vm_ip6_set_name + ' src -m hl --hl-eq 255 -j RETURN')
571+
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-in ' + vif + ' -p icmpv6 --icmpv6-type neighbor-advertisement -m set --match-set ' + vmipsetName6 + ' src -m hl --hl-eq 255 -j RETURN')
544572
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-out ' + vif + ' -p icmpv6 --icmpv6-type neighbor-advertisement -m hl --hl-eq 255 -j ACCEPT')
545573

546574
# Packets to allow as per RFC4890
547-
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-in ' + vif + ' -p icmpv6 --icmpv6-type packet-too-big -m set --match-set ' + vm_ip6_set_name + ' src -j RETURN')
575+
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-in ' + vif + ' -p icmpv6 --icmpv6-type packet-too-big -m set --match-set ' + vmipsetName6 + ' src -j RETURN')
548576
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-out ' + vif + ' -p icmpv6 --icmpv6-type packet-too-big -j ACCEPT')
549577

550-
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-in ' + vif + ' -p icmpv6 --icmpv6-type destination-unreachable -m set --match-set ' + vm_ip6_set_name + ' src -j RETURN')
578+
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-in ' + vif + ' -p icmpv6 --icmpv6-type destination-unreachable -m set --match-set ' + vmipsetName6 + ' src -j RETURN')
551579
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-out ' + vif + ' -p icmpv6 --icmpv6-type destination-unreachable -j ACCEPT')
552580

553-
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-in ' + vif + ' -p icmpv6 --icmpv6-type time-exceeded -m set --match-set ' + vm_ip6_set_name + ' src -j RETURN')
581+
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-in ' + vif + ' -p icmpv6 --icmpv6-type time-exceeded -m set --match-set ' + vmipsetName6 + ' src -j RETURN')
554582
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-out ' + vif + ' -p icmpv6 --icmpv6-type time-exceeded -j ACCEPT')
555583

556-
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-in ' + vif + ' -p icmpv6 --icmpv6-type parameter-problem -m set --match-set ' + vm_ip6_set_name + ' src -j RETURN')
584+
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-in ' + vif + ' -p icmpv6 --icmpv6-type parameter-problem -m set --match-set ' + vmipsetName6 + ' src -j RETURN')
557585
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-out ' + vif + ' -p icmpv6 --icmpv6-type parameter-problem -j ACCEPT')
558586

559587
# MLDv2 discovery packets
@@ -565,14 +593,14 @@ def default_network_rules(vm_name, vm_id, vm_ip, vm_ip6, vm_mac, vif, brname, se
565593
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-in ' + vif + ' -p udp --sport 547 ! --dst fe80::/64 -j DROP')
566594

567595
# Always allow outbound DNS over UDP and TCP
568-
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-in ' + vif + ' -p udp --dport 53 -m set --match-set ' + vm_ip6_set_name + ' src -j RETURN')
569-
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-in ' + vif + ' -p tcp --dport 53 -m set --match-set ' + vm_ip6_set_name + ' src -j RETURN')
596+
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-in ' + vif + ' -p udp --dport 53 -m set --match-set ' + vmipsetName6 + ' src -j RETURN')
597+
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-in ' + vif + ' -p tcp --dport 53 -m set --match-set ' + vmipsetName6 + ' src -j RETURN')
570598

571599
# Prevent source address spoofing
572-
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-in ' + vif + ' -m set ! --match-set ' + vm_ip6_set_name + ' src -j DROP')
600+
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-in ' + vif + ' -m set ! --match-set ' + vmipsetName6 + ' src -j DROP')
573601

574602
# Send proper traffic to the egress chain of the Instance
575-
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-in ' + vif + ' -m set --match-set ' + vm_ip6_set_name + ' src -j ' + vmchain_egress)
603+
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-in ' + vif + ' -m set --match-set ' + vmipsetName6 + ' src -j ' + vmchain_egress)
576604

577605
execute('ip6tables -A ' + vmchain_default + ' -m physdev --physdev-is-bridged --physdev-out ' + vif + ' -j ' + vmchain)
578606

0 commit comments

Comments
 (0)