Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/bash
0002 # SPDX-License-Identifier: GPL-2.0
0003 
0004 # Test for checking ICMP response with dummy address instead of 0.0.0.0.
0005 # Sets up two namespaces like:
0006 # +----------------------+                          +--------------------+
0007 # | ns1                  |    v4-via-v6 routes:     | ns2                |
0008 # |                      |                  '       |                    |
0009 # |             +--------+   -> 172.16.1.0/24 ->    +--------+           |
0010 # |             | veth0  +--------------------------+  veth0 |           |
0011 # |             +--------+   <- 172.16.0.0/24 <-    +--------+           |
0012 # |           172.16.0.1 |                          | 2001:db8:1::2/64   |
0013 # |     2001:db8:1::2/64 |                          |                    |
0014 # +----------------------+                          +--------------------+
0015 #
0016 # And then tries to ping 172.16.1.1 from ns1. This results in a "net
0017 # unreachable" message being sent from ns2, but there is no IPv4 address set in
0018 # that address space, so the kernel should substitute the dummy address
0019 # 192.0.0.8 defined in RFC7600.
0020 
0021 NS1=ns1
0022 NS2=ns2
0023 H1_IP=172.16.0.1/32
0024 H1_IP6=2001:db8:1::1
0025 RT1=172.16.1.0/24
0026 PINGADDR=172.16.1.1
0027 RT2=172.16.0.0/24
0028 H2_IP6=2001:db8:1::2
0029 
0030 TMPFILE=$(mktemp)
0031 
0032 cleanup()
0033 {
0034     rm -f "$TMPFILE"
0035     ip netns del $NS1
0036     ip netns del $NS2
0037 }
0038 
0039 trap cleanup EXIT
0040 
0041 # Namespaces
0042 ip netns add $NS1
0043 ip netns add $NS2
0044 
0045 # Connectivity
0046 ip -netns $NS1 link add veth0 type veth peer name veth0 netns $NS2
0047 ip -netns $NS1 link set dev veth0 up
0048 ip -netns $NS2 link set dev veth0 up
0049 ip -netns $NS1 addr add $H1_IP dev veth0
0050 ip -netns $NS1 addr add $H1_IP6/64 dev veth0 nodad
0051 ip -netns $NS2 addr add $H2_IP6/64 dev veth0 nodad
0052 ip -netns $NS1 route add $RT1 via inet6 $H2_IP6
0053 ip -netns $NS2 route add $RT2 via inet6 $H1_IP6
0054 
0055 # Make sure ns2 will respond with ICMP unreachable
0056 ip netns exec $NS2 sysctl -qw net.ipv4.icmp_ratelimit=0 net.ipv4.ip_forward=1
0057 
0058 # Run the test - a ping runs in the background, and we capture ICMP responses
0059 # with tcpdump; -c 1 means it should exit on the first ping, but add a timeout
0060 # in case something goes wrong
0061 ip netns exec $NS1 ping -w 3 -i 0.5 $PINGADDR >/dev/null &
0062 ip netns exec $NS1 timeout 10 tcpdump -tpni veth0 -c 1 'icmp and icmp[icmptype] != icmp-echo' > $TMPFILE 2>/dev/null
0063 
0064 # Parse response and check for dummy address
0065 # tcpdump output looks like:
0066 # IP 192.0.0.8 > 172.16.0.1: ICMP net 172.16.1.1 unreachable, length 92
0067 RESP_IP=$(awk '{print $2}' < $TMPFILE)
0068 if [[ "$RESP_IP" != "192.0.0.8" ]]; then
0069     echo "FAIL - got ICMP response from $RESP_IP, should be 192.0.0.8"
0070     exit 1
0071 else
0072     echo "OK"
0073     exit 0
0074 fi