Beispiel #1
0
    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)
Beispiel #2
0
    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)
Beispiel #3
0
 def CreateTunInterface(cls, netid):
     iface = cls.GetInterfaceName(netid)
     f = open("/dev/net/tun", "r+b")
     ifr = struct.pack("16sH", iface, IFF_TAP | IFF_NO_PI)
     ifr += "\x00" * (40 - len(ifr))
     fcntl.ioctl(f, TUNSETIFF, ifr)
     # Give ourselves a predictable MAC address.
     net_test.SetInterfaceHWAddr(iface, cls.MyMacAddress(netid))
     # Disable DAD so we don't have to wait for it.
     cls.SetSysctl("/proc/sys/net/ipv6/conf/%s/accept_dad" % iface, 0)
     net_test.SetInterfaceUp(iface)
     net_test.SetNonBlocking(f)
     return f