1*2f2c4c7aSAndroid Build Coastguard Worker#!/usr/bin/python3 2*2f2c4c7aSAndroid Build Coastguard Worker# 3*2f2c4c7aSAndroid Build Coastguard Worker# Copyright 2018 The Android Open Source Project 4*2f2c4c7aSAndroid Build Coastguard Worker# 5*2f2c4c7aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*2f2c4c7aSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*2f2c4c7aSAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*2f2c4c7aSAndroid Build Coastguard Worker# 9*2f2c4c7aSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*2f2c4c7aSAndroid Build Coastguard Worker# 11*2f2c4c7aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*2f2c4c7aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*2f2c4c7aSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*2f2c4c7aSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*2f2c4c7aSAndroid Build Coastguard Worker# limitations under the License. 16*2f2c4c7aSAndroid Build Coastguard Worker 17*2f2c4c7aSAndroid Build Coastguard Workerimport unittest 18*2f2c4c7aSAndroid Build Coastguard Worker 19*2f2c4c7aSAndroid Build Coastguard Workerimport errno 20*2f2c4c7aSAndroid Build Coastguard Workerfrom socket import * 21*2f2c4c7aSAndroid Build Coastguard Worker 22*2f2c4c7aSAndroid Build Coastguard Workerimport multinetwork_base 23*2f2c4c7aSAndroid Build Coastguard Workerimport net_test 24*2f2c4c7aSAndroid Build Coastguard Worker 25*2f2c4c7aSAndroid Build Coastguard Worker_TEST_IP4_ADDR = "192.0.2.1" 26*2f2c4c7aSAndroid Build Coastguard Worker_TEST_IP6_ADDR = "2001:db8::" 27*2f2c4c7aSAndroid Build Coastguard Worker 28*2f2c4c7aSAndroid Build Coastguard Worker 29*2f2c4c7aSAndroid Build Coastguard Worker# Regression tests for interactions between kernel networking and netfilter 30*2f2c4c7aSAndroid Build Coastguard Worker# 31*2f2c4c7aSAndroid Build Coastguard Worker# These tests were added to ensure that the lookup path for local-ICMP errors 32*2f2c4c7aSAndroid Build Coastguard Worker# do not cause failures. Specifically, local-ICMP packets do not have a 33*2f2c4c7aSAndroid Build Coastguard Worker# net_device in the skb, and has been known to trigger bugs in surrounding code. 34*2f2c4c7aSAndroid Build Coastguard Workerclass NetilterRejectTargetTest(multinetwork_base.MultiNetworkBaseTest): 35*2f2c4c7aSAndroid Build Coastguard Worker 36*2f2c4c7aSAndroid Build Coastguard Worker def setUp(self): 37*2f2c4c7aSAndroid Build Coastguard Worker multinetwork_base.MultiNetworkBaseTest.setUp(self) 38*2f2c4c7aSAndroid Build Coastguard Worker net_test.RunIptablesCommand(4, "-A OUTPUT -d " + _TEST_IP4_ADDR + " -j REJECT") 39*2f2c4c7aSAndroid Build Coastguard Worker net_test.RunIptablesCommand(6, "-A OUTPUT -d " + _TEST_IP6_ADDR + " -j REJECT") 40*2f2c4c7aSAndroid Build Coastguard Worker 41*2f2c4c7aSAndroid Build Coastguard Worker def tearDown(self): 42*2f2c4c7aSAndroid Build Coastguard Worker net_test.RunIptablesCommand(4, "-D OUTPUT -d " + _TEST_IP4_ADDR + " -j REJECT") 43*2f2c4c7aSAndroid Build Coastguard Worker net_test.RunIptablesCommand(6, "-D OUTPUT -d " + _TEST_IP6_ADDR + " -j REJECT") 44*2f2c4c7aSAndroid Build Coastguard Worker multinetwork_base.MultiNetworkBaseTest.tearDown(self) 45*2f2c4c7aSAndroid Build Coastguard Worker 46*2f2c4c7aSAndroid Build Coastguard Worker # Test a rejected TCP connect. The responding ICMP may not have skb->dev set. 47*2f2c4c7aSAndroid Build Coastguard Worker # This tests the local-ICMP output-input path. 48*2f2c4c7aSAndroid Build Coastguard Worker def CheckRejectedTcp(self, version, addr): 49*2f2c4c7aSAndroid Build Coastguard Worker sock = net_test.TCPSocket(net_test.GetAddressFamily(version)) 50*2f2c4c7aSAndroid Build Coastguard Worker netid = self.RandomNetid() 51*2f2c4c7aSAndroid Build Coastguard Worker self.SelectInterface(sock, netid, "mark") 52*2f2c4c7aSAndroid Build Coastguard Worker 53*2f2c4c7aSAndroid Build Coastguard Worker # Expect this to fail with ICMP unreachable 54*2f2c4c7aSAndroid Build Coastguard Worker try: 55*2f2c4c7aSAndroid Build Coastguard Worker sock.connect((addr, 53)) 56*2f2c4c7aSAndroid Build Coastguard Worker except IOError: 57*2f2c4c7aSAndroid Build Coastguard Worker pass 58*2f2c4c7aSAndroid Build Coastguard Worker sock.close() 59*2f2c4c7aSAndroid Build Coastguard Worker 60*2f2c4c7aSAndroid Build Coastguard Worker def testRejectTcp4(self): 61*2f2c4c7aSAndroid Build Coastguard Worker self.CheckRejectedTcp(4, _TEST_IP4_ADDR) 62*2f2c4c7aSAndroid Build Coastguard Worker 63*2f2c4c7aSAndroid Build Coastguard Worker def testRejectTcp6(self): 64*2f2c4c7aSAndroid Build Coastguard Worker self.CheckRejectedTcp(6, _TEST_IP6_ADDR) 65*2f2c4c7aSAndroid Build Coastguard Worker 66*2f2c4c7aSAndroid Build Coastguard Worker # Test a rejected UDP connect. The responding ICMP may not have skb->dev set. 67*2f2c4c7aSAndroid Build Coastguard Worker # This tests the local-ICMP output-input path. 68*2f2c4c7aSAndroid Build Coastguard Worker def CheckRejectedUdp(self, version, addr): 69*2f2c4c7aSAndroid Build Coastguard Worker sock = net_test.UDPSocket(net_test.GetAddressFamily(version)) 70*2f2c4c7aSAndroid Build Coastguard Worker netid = self.RandomNetid() 71*2f2c4c7aSAndroid Build Coastguard Worker self.SelectInterface(sock, netid, "mark") 72*2f2c4c7aSAndroid Build Coastguard Worker 73*2f2c4c7aSAndroid Build Coastguard Worker # Expect this to fail with ICMP unreachable 74*2f2c4c7aSAndroid Build Coastguard Worker try: 75*2f2c4c7aSAndroid Build Coastguard Worker sock.sendto(net_test.UDP_PAYLOAD, (addr, 53)) 76*2f2c4c7aSAndroid Build Coastguard Worker except IOError: 77*2f2c4c7aSAndroid Build Coastguard Worker pass 78*2f2c4c7aSAndroid Build Coastguard Worker sock.close() 79*2f2c4c7aSAndroid Build Coastguard Worker 80*2f2c4c7aSAndroid Build Coastguard Worker def testRejectUdp4(self): 81*2f2c4c7aSAndroid Build Coastguard Worker self.CheckRejectedUdp(4, _TEST_IP4_ADDR) 82*2f2c4c7aSAndroid Build Coastguard Worker 83*2f2c4c7aSAndroid Build Coastguard Worker def testRejectUdp6(self): 84*2f2c4c7aSAndroid Build Coastguard Worker self.CheckRejectedUdp(6, _TEST_IP6_ADDR) 85*2f2c4c7aSAndroid Build Coastguard Worker 86*2f2c4c7aSAndroid Build Coastguard Worker 87*2f2c4c7aSAndroid Build Coastguard Workerif __name__ == "__main__": 88*2f2c4c7aSAndroid Build Coastguard Worker unittest.main()