def setUpClass(cls): # This is per-class setup instead of per-testcase setup because shelling out # to ip and iptables is slow, and because routing configuration doesn't # change during the test. cls.iproute = iproute.IPRoute() cls.tuns = {} cls.ifindices = {} if HAVE_AUTOCONF_TABLE: cls.SetSysctl(AUTOCONF_TABLE_SYSCTL, -1000) cls.AUTOCONF_TABLE_OFFSET = -1000 else: cls.AUTOCONF_TABLE_OFFSET = None # Disable ICMP rate limits. These will be restored by _RestoreSysctls. for version in [4, 6]: cls._SetICMPRatelimit(version, 0) for netid in cls.NETIDS: cls.tuns[netid] = cls.CreateTunInterface(netid) iface = cls.GetInterfaceName(netid) cls.ifindices[netid] = net_test.GetInterfaceIndex(iface) cls.SendRA(netid) cls._RunSetupCommands(netid, True) for version in [4, 6]: cls.iproute.UnreachableRule(version, True, 1000)
def testAnycastNetdeviceUnregister(self): netid = self._TEST_NETID self.assertNotIn(netid, self.tuns) self.tuns[netid] = self.CreateTunInterface(netid) self.SendRA(netid) iface = self.GetInterfaceName(netid) self.ifindices[netid] = net_test.GetInterfaceIndex(iface) s = socket(AF_INET6, SOCK_DGRAM, 0) addr = self.MyAddress(6, netid) self.assertIsNotNone(addr) addr = inet_pton(AF_INET6, addr) addr = addr[:8] + os.urandom(8) self.AnycastSetsockopt(s, True, netid, addr) # Close the tun fd in the background. # This will hang if the kernel has the bug. thread = CloseFileDescriptorThread(self.tuns[netid]) thread.start() time.sleep(0.1) # Make teardown work. del self.tuns[netid] # Check that the interface is gone. try: self.assertIsNone(self.MyAddress(6, netid)) finally: # This doesn't seem to help, but still. self.AnycastSetsockopt(s, False, netid, addr) self.assertTrue(thread.finished)
def setUpClass(cls): # This is per-class setup instead of per-testcase setup because shelling out # to ip and iptables is slow, and because routing configuration doesn't # change during the test. cls.iproute = iproute.IPRoute() cls.tuns = {} cls.ifindices = {} if HAVE_AUTOCONF_TABLE: cls.SetSysctl(AUTOCONF_TABLE_SYSCTL, -1000) cls.AUTOCONF_TABLE_OFFSET = -1000 else: cls.AUTOCONF_TABLE_OFFSET = None # Disable ICMP rate limits. These will be restored by _RestoreSysctls. for version in [4, 6]: cls._SetICMPRatelimit(version, 0) for version in [4, 6]: cls.iproute.UnreachableRule(version, True, cls.PRIORITY_UNREACHABLE) for netid in cls.NETIDS: cls.tuns[netid] = cls.CreateTunInterface(netid) iface = cls.GetInterfaceName(netid) cls.ifindices[netid] = net_test.GetInterfaceIndex(iface) cls.SendRA(netid) cls._RunSetupCommands(netid, True) # Don't print lots of "device foo entered promiscuous mode" warnings. cls.loglevel = cls.GetConsoleLogLevel() cls.SetConsoleLogLevel(net_test.KERN_INFO) # When running on device, don't send connections through FwmarkServer. os.environ["ANDROID_NO_USE_FWMARK_CLIENT"] = "1"
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)
def testAddXfrmInterface(self): self.iproute.CreateXfrmInterface(_TEST_XFRM_IFNAME, _TEST_XFRM_IF_ID, _LOOPBACK_IFINDEX) if_index = self.iproute.GetIfIndex(_TEST_XFRM_IFNAME) net_test.SetInterfaceUp(_TEST_XFRM_IFNAME) # Validate that the netlink interface matches the ioctl interface. self.assertEquals(net_test.GetInterfaceIndex(_TEST_XFRM_IFNAME), if_index) self.iproute.DeleteLink(_TEST_XFRM_IFNAME) with self.assertRaises(IOError): self.iproute.GetIfIndex(_TEST_XFRM_IFNAME)
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 testAddVti(self): """Test the creation of a Virtual Tunnel Interface.""" for version in [4, 6]: netid = self.RandomNetid() local_addr = self.MyAddress(version, netid) self.iproute.CreateVirtualTunnelInterface( dev_name=_VTI_IFNAME, local_addr=local_addr, remote_addr=_GetRemoteOuterAddress(version), o_key=_TEST_OKEY, i_key=_TEST_IKEY) self.verifyVtiInfoData(self.iproute.GetVtiInfoData(_VTI_IFNAME), version, local_addr, _GetRemoteOuterAddress(version), _TEST_IKEY, _TEST_OKEY) new_remote_addr = {4: net_test.IPV4_ADDR2, 6: net_test.IPV6_ADDR2} new_okey = _TEST_OKEY + _VTI_NETID new_ikey = _TEST_IKEY + _VTI_NETID self.iproute.CreateVirtualTunnelInterface( dev_name=_VTI_IFNAME, local_addr=local_addr, remote_addr=new_remote_addr[version], o_key=new_okey, i_key=new_ikey, is_update=True) self.verifyVtiInfoData(self.iproute.GetVtiInfoData(_VTI_IFNAME), version, local_addr, new_remote_addr[version], new_ikey, new_okey) if_index = self.iproute.GetIfIndex(_VTI_IFNAME) # Validate that the netlink interface matches the ioctl interface. self.assertEquals(net_test.GetInterfaceIndex(_VTI_IFNAME), if_index) self.iproute.DeleteLink(_VTI_IFNAME) with self.assertRaises(IOError): self.iproute.GetIfIndex(_VTI_IFNAME)
def testAnycastNetdeviceUnregister(self): netid = self._TEST_NETID self.assertNotIn(netid, self.tuns) self.tuns[netid] = self.CreateTunInterface(netid) self.SendRA(netid) iface = self.GetInterfaceName(netid) self.ifindices[netid] = net_test.GetInterfaceIndex(iface) s = socket(AF_INET6, SOCK_DGRAM, 0) addr = self.MyAddress(6, netid) self.assertIsNotNone(addr) addr = inet_pton(AF_INET6, addr) addr = addr[:8] + os.urandom(8) self.AnycastSetsockopt(s, True, netid, addr) # Close the tun fd in the background. # This will hang if the kernel has the bug. thread = CloseFileDescriptorThread(self.tuns[netid]) thread.start() # Wait up to 3 seconds for the thread to finish, but # continue and fail the test if the thread hangs. # For kernels with MPTCP ported, closing tun interface need more # than 0.5 sec. DAD procedure within MPTCP fullmesh module takes # more time, because duplicate address-timer takes a refcount # on the IPv6-address, preventing it from getting closed. thread.join(3) # Make teardown work. del self.tuns[netid] # Check that the interface is gone. try: self.assertIsNone(self.MyAddress(6, netid)) finally: # This doesn't seem to help, but still. self.AnycastSetsockopt(s, False, netid, addr) self.assertTrue(thread.finished)