def _SwapInterfaceAddress(self, ifname, old_addr, new_addr): """Exchange two addresses on a given interface. Args: ifname: Name of the interface old_addr: An address to be removed from the interface new_addr: An address to be added to an interface """ version = 6 if ":" in new_addr else 4 ifindex = net_test.GetInterfaceIndex(ifname) self.iproute.AddAddress(new_addr, net_test.AddressLengthBits(version), ifindex) self.iproute.DelAddress(old_addr, net_test.AddressLengthBits(version), ifindex)
def SrcDstSelector(src, dst): """A selector that matches packets between the specified IP addresses.""" srcver = csocket.AddressVersion(src) dstver = csocket.AddressVersion(dst) if srcver != dstver: raise ValueError("Cross-address family selector specified: %s -> %s" % (src, dst)) prefixlen = net_test.AddressLengthBits(srcver) family = net_test.GetAddressFamily(srcver) return XfrmSelector(saddr=PaddedAddress(src), daddr=PaddedAddress(dst), prefixlen_s=prefixlen, prefixlen_d=prefixlen, family=family)
def _SetupVtiNetwork(cls, vti, is_add): """Setup rules and routes for a VTI Network. Takes an interface and depending on the boolean value of is_add, either adds or removes the rules and routes for a VTI to behave like an Android Network for purposes of testing. Args: vti: A VtiInterface, the VTI to set up. is_add: Boolean that causes this method to perform setup if True or teardown if False """ if is_add: # Disable router solicitations to avoid occasional spurious packets # arriving on the underlying network; there are two possible behaviors # when that occurred: either only the RA packet is read, and when it # is echoed back to the VTI, it causes the test to fail by not receiving # the UDP_PAYLOAD; or, two packets may arrive on the underlying # network which fails the assertion that only one ESP packet is received. cls.SetSysctl( "/proc/sys/net/ipv6/conf/%s/router_solicitations" % vti.iface, 0) net_test.SetInterfaceUp(vti.iface) for version in [4, 6]: ifindex = net_test.GetInterfaceIndex(vti.iface) table = vti.netid # Set up routing rules. start, end = cls.UidRangeForNetid(vti.netid) cls.iproute.UidRangeRule(version, is_add, start, end, table, cls.PRIORITY_UID) cls.iproute.OifRule(version, is_add, vti.iface, table, cls.PRIORITY_OIF) cls.iproute.FwmarkRule(version, is_add, vti.netid, cls.NETID_FWMASK, table, cls.PRIORITY_FWMARK) # Configure IP addresses. if version == 4: addr = cls._MyIPv4Address(vti.netid) else: addr = cls.OnlinkPrefix(6, vti.netid) + "1" prefixlen = net_test.AddressLengthBits(version) vti.addrs[version] = addr if is_add: cls.iproute.AddAddress(addr, prefixlen, ifindex) cls.iproute.AddRoute(version, table, "default", 0, None, ifindex) else: cls.iproute.DelRoute(version, table, "default", 0, None, ifindex) cls.iproute.DelAddress(addr, prefixlen, ifindex)