1#!/bin/bash 2# SPDX-License-Identifier: GPL-2.0 3# 4# Tests sysctl options {arp,ndisc}_evict_nocarrier={0,1} 5# 6# Create a veth pair and set IPs/routes on both. Then ping to establish 7# an entry in the ARP/ND table. Depending on the test set sysctl option to 8# 1 or 0. Set remote veth down which will cause local veth to go into a no 9# carrier state. Depending on the test check the ARP/ND table: 10# 11# {arp,ndisc}_evict_nocarrier=1 should contain no ARP/ND after no carrier 12# {arp,ndisc}_evict_nocarrer=0 should still contain the single ARP/ND entry 13# 14 15readonly PEER_NS="ns-peer-$(mktemp -u XXXXXX)" 16readonly V4_ADDR0=10.0.10.1 17readonly V4_ADDR1=10.0.10.2 18readonly V6_ADDR0=2001:db8:91::1 19readonly V6_ADDR1=2001:db8:91::2 20nsid=100 21 22cleanup_v6() 23{ 24 ip netns del me 25 ip netns del peer 26 27 sysctl -w net.ipv4.conf.veth0.ndisc_evict_nocarrier=1 >/dev/null 2>&1 28 sysctl -w net.ipv4.conf.all.ndisc_evict_nocarrier=1 >/dev/null 2>&1 29} 30 31create_ns() 32{ 33 local n=${1} 34 35 ip netns del ${n} 2>/dev/null 36 37 ip netns add ${n} 38 ip netns set ${n} $((nsid++)) 39 ip -netns ${n} link set lo up 40} 41 42 43setup_v6() { 44 create_ns me 45 create_ns peer 46 47 IP="ip -netns me" 48 49 $IP li add veth1 type veth peer name veth2 50 $IP li set veth1 up 51 $IP -6 addr add $V6_ADDR0/64 dev veth1 nodad 52 $IP li set veth2 netns peer up 53 ip -netns peer -6 addr add $V6_ADDR1/64 dev veth2 nodad 54 55 ip netns exec me sysctl -w $1 >/dev/null 2>&1 56 57 # Establish an ND cache entry 58 ip netns exec me ping -6 -c1 -Iveth1 $V6_ADDR1 >/dev/null 2>&1 59 # Should have the veth1 entry in ND table 60 ip netns exec me ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1 61 if [ $? -ne 0 ]; then 62 cleanup_v6 63 echo "failed" 64 exit 65 fi 66 67 # Set veth2 down, which will put veth1 in NOCARRIER state 68 ip netns exec peer ip link set veth2 down 69} 70 71setup_v4() { 72 ip netns add "${PEER_NS}" 73 ip link add name veth0 type veth peer name veth1 74 ip link set dev veth0 up 75 ip link set dev veth1 netns "${PEER_NS}" 76 ip netns exec "${PEER_NS}" ip link set dev veth1 up 77 ip addr add $V4_ADDR0/24 dev veth0 78 ip netns exec "${PEER_NS}" ip addr add $V4_ADDR1/24 dev veth1 79 ip netns exec ${PEER_NS} ip route add default via $V4_ADDR1 dev veth1 80 ip route add default via $V4_ADDR0 dev veth0 81 82 sysctl -w "$1" >/dev/null 2>&1 83 84 # Establish an ARP cache entry 85 ping -c1 -I veth0 $V4_ADDR1 -q >/dev/null 2>&1 86 # Should have the veth1 entry in ARP table 87 ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1 88 if [ $? -ne 0 ]; then 89 cleanup_v4 90 echo "failed" 91 exit 92 fi 93 94 # Set veth1 down, which will put veth0 in NOCARRIER state 95 ip netns exec "${PEER_NS}" ip link set veth1 down 96} 97 98cleanup_v4() { 99 ip neigh flush dev veth0 100 ip link del veth0 101 local -r ns="$(ip netns list|grep $PEER_NS)" 102 [ -n "$ns" ] && ip netns del $ns 2>/dev/null 103 104 sysctl -w net.ipv4.conf.veth0.arp_evict_nocarrier=1 >/dev/null 2>&1 105 sysctl -w net.ipv4.conf.all.arp_evict_nocarrier=1 >/dev/null 2>&1 106} 107 108# Run test when arp_evict_nocarrier = 1 (default). 109run_arp_evict_nocarrier_enabled() { 110 echo "run arp_evict_nocarrier=1 test" 111 setup_v4 "net.ipv4.conf.veth0.arp_evict_nocarrier=1" 112 113 # ARP table should be empty 114 ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1 115 116 if [ $? -eq 0 ];then 117 echo "failed" 118 else 119 echo "ok" 120 fi 121 122 cleanup_v4 123} 124 125# Run test when arp_evict_nocarrier = 0 126run_arp_evict_nocarrier_disabled() { 127 echo "run arp_evict_nocarrier=0 test" 128 setup_v4 "net.ipv4.conf.veth0.arp_evict_nocarrier=0" 129 130 # ARP table should still contain the entry 131 ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1 132 133 if [ $? -eq 0 ];then 134 echo "ok" 135 else 136 echo "failed" 137 fi 138 139 cleanup_v4 140} 141 142run_arp_evict_nocarrier_disabled_all() { 143 echo "run all.arp_evict_nocarrier=0 test" 144 setup_v4 "net.ipv4.conf.all.arp_evict_nocarrier=0" 145 146 # ARP table should still contain the entry 147 ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1 148 149 if [ $? -eq 0 ];then 150 echo "ok" 151 else 152 echo "failed" 153 fi 154 155 cleanup_v4 156} 157 158run_ndisc_evict_nocarrier_enabled() { 159 echo "run ndisc_evict_nocarrier=1 test" 160 161 setup_v6 "net.ipv6.conf.veth1.ndisc_evict_nocarrier=1" 162 163 ip netns exec me ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1 164 165 if [ $? -eq 0 ];then 166 echo "failed" 167 else 168 echo "ok" 169 fi 170 171 cleanup_v6 172} 173 174run_ndisc_evict_nocarrier_disabled() { 175 echo "run ndisc_evict_nocarrier=0 test" 176 177 setup_v6 "net.ipv6.conf.veth1.ndisc_evict_nocarrier=0" 178 179 ip netns exec me ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1 180 181 if [ $? -eq 0 ];then 182 echo "ok" 183 else 184 echo "failed" 185 fi 186 187 cleanup_v6 188} 189 190run_ndisc_evict_nocarrier_disabled_all() { 191 echo "run all.ndisc_evict_nocarrier=0 test" 192 193 setup_v6 "net.ipv6.conf.all.ndisc_evict_nocarrier=0" 194 195 ip netns exec me ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1 196 197 if [ $? -eq 0 ];then 198 echo "ok" 199 else 200 echo "failed" 201 fi 202 203 cleanup_v6 204} 205 206run_all_tests() { 207 run_arp_evict_nocarrier_enabled 208 run_arp_evict_nocarrier_disabled 209 run_arp_evict_nocarrier_disabled_all 210 run_ndisc_evict_nocarrier_enabled 211 run_ndisc_evict_nocarrier_disabled 212 run_ndisc_evict_nocarrier_disabled_all 213} 214 215if [ "$(id -u)" -ne 0 ];then 216 echo "SKIP: Need root privileges" 217 exit $ksft_skip; 218fi 219 220run_all_tests 221