Home | History | Annotate | Download | only in test
      1 #!/usr/bin/python
      2 #
      3 # Copyright 2015 The Android Open Source Project
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 # http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 
     17 import itertools
     18 import random
     19 import unittest
     20 
     21 from socket import *
     22 
     23 import multinetwork_base
     24 import net_test
     25 import packets
     26 
     27 
     28 class ForwardingTest(multinetwork_base.MultiNetworkBaseTest):
     29   """Checks that IPv6 forwarding doesn't crash the system.
     30 
     31   Relevant kernel commits:
     32     upstream net-next:
     33       e7eadb4 ipv6: inet6_sk() should use sk_fullsock()
     34     android-3.10:
     35       feee3c1 ipv6: inet6_sk() should use sk_fullsock()
     36       cdab04e net: add sk_fullsock() helper
     37     android-3.18:
     38       8246f18 ipv6: inet6_sk() should use sk_fullsock()
     39       bea19db net: add sk_fullsock() helper
     40   """
     41 
     42   TCP_TIME_WAIT = 6
     43 
     44   def ForwardBetweenInterfaces(self, enabled, iface1, iface2):
     45     for iif, oif in itertools.permutations([iface1, iface2]):
     46       self.iproute.IifRule(6, enabled, self.GetInterfaceName(iif),
     47                            self._TableForNetid(oif), self.PRIORITY_IIF)
     48 
     49   def setUp(self):
     50     self.SetSysctl("/proc/sys/net/ipv6/conf/all/forwarding", 1)
     51 
     52   def tearDown(self):
     53     self.SetSysctl("/proc/sys/net/ipv6/conf/all/forwarding", 0)
     54 
     55   def CheckForwardingCrash(self, netid, iface1, iface2):
     56     version = 6
     57     listensocket = net_test.IPv6TCPSocket()
     58     self.SetSocketMark(listensocket, netid)
     59     listenport = net_test.BindRandomPort(version, listensocket)
     60 
     61     remoteaddr = self.GetRemoteAddress(version)
     62     myaddr = self.MyAddress(version, netid)
     63 
     64     desc, syn = packets.SYN(listenport, version, remoteaddr, myaddr)
     65     synack_desc, synack = packets.SYNACK(version, myaddr, remoteaddr, syn)
     66     msg = "Sent %s, expected %s" % (desc, synack_desc)
     67     reply = self._ReceiveAndExpectResponse(netid, syn, synack, msg)
     68 
     69     establishing_ack = packets.ACK(version, remoteaddr, myaddr, reply)[1]
     70     self.ReceivePacketOn(netid, establishing_ack)
     71     accepted, peer = listensocket.accept()
     72     remoteport = accepted.getpeername()[1]
     73 
     74     accepted.close()
     75     desc, fin = packets.FIN(version, myaddr, remoteaddr, establishing_ack)
     76     self.ExpectPacketOn(netid, msg + ": expecting %s after close" % desc, fin)
     77 
     78     desc, finack = packets.FIN(version, remoteaddr, myaddr, fin)
     79     self.ReceivePacketOn(netid, finack)
     80 
     81     # Check our socket is now in TIME_WAIT.
     82     sockets = self.ReadProcNetSocket("tcp6")
     83     mysrc = "%s:%04X" % (net_test.FormatSockStatAddress(myaddr), listenport)
     84     mydst = "%s:%04X" % (net_test.FormatSockStatAddress(remoteaddr), remoteport)
     85     state = None
     86     sockets = [s for s in sockets if s[0] == mysrc and s[1] == mydst]
     87     self.assertEquals(1, len(sockets))
     88     self.assertEquals("%02X" % self.TCP_TIME_WAIT, sockets[0][2])
     89 
     90     # Remove our IP address.
     91     try:
     92       self.iproute.DelAddress(myaddr, 64, self.ifindices[netid])
     93 
     94       self.ReceivePacketOn(iface1, finack)
     95       self.ReceivePacketOn(iface1, establishing_ack)
     96       self.ReceivePacketOn(iface1, establishing_ack)
     97       # No crashes? Good.
     98 
     99     finally:
    100       # Put back our IP address.
    101       self.SendRA(netid)
    102       listensocket.close()
    103 
    104   def testCrash(self):
    105     # Run the test a few times as it doesn't crash/hang the first time.
    106     for netids in itertools.permutations(self.tuns):
    107       # Pick an interface to send traffic on and two to forward traffic between.
    108       netid, iface1, iface2 = random.sample(netids, 3)
    109       self.ForwardBetweenInterfaces(True, iface1, iface2)
    110       try:
    111         self.CheckForwardingCrash(netid, iface1, iface2)
    112       finally:
    113         self.ForwardBetweenInterfaces(False, iface1, iface2)
    114 
    115 
    116 if __name__ == "__main__":
    117   unittest.main()
    118