2 # SPDX-License-Identifier: GPL-2.0
4 # Tests sysctl options {arp,ndisc}_evict_nocarrier={0,1}
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:
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
15 readonly PEER_NS="ns-peer-$(mktemp -u XXXXXX)"
16 readonly V4_ADDR0=10.0.10.1
17 readonly V4_ADDR1=10.0.10.2
18 readonly V6_ADDR0=2001:db8:91::1
19 readonly V6_ADDR1=2001:db8:91::2
28 sysctl -w net.ipv6.conf.veth1.ndisc_evict_nocarrier=1 >/dev/null 2>&1
29 sysctl -w net.ipv6.conf.all.ndisc_evict_nocarrier=1 >/dev/null 2>&1
36 ip netns del ${n} 2>/dev/null
39 ip netns set ${n} $((nsid++))
40 ip -netns ${n} link set lo up
50 $IP li add veth1 type veth peer name veth2
52 $IP -6 addr add $V6_ADDR0/64 dev veth1 nodad
53 $IP li set veth2 netns peer up
54 ip -netns peer -6 addr add $V6_ADDR1/64 dev veth2 nodad
56 ip netns exec me sysctl -w $1 >/dev/null 2>&1
58 # Establish an ND cache entry
59 ip netns exec me ping -6 -c1 -Iveth1 $V6_ADDR1 >/dev/null 2>&1
60 # Should have the veth1 entry in ND table
61 ip netns exec me ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1
68 # Set veth2 down, which will put veth1 in NOCARRIER state
69 ip netns exec peer ip link set veth2 down
73 ip netns add "${PEER_NS}"
74 ip link add name veth0 type veth peer name veth1
75 ip link set dev veth0 up
76 ip link set dev veth1 netns "${PEER_NS}"
77 ip netns exec "${PEER_NS}" ip link set dev veth1 up
78 ip addr add $V4_ADDR0/24 dev veth0
79 ip netns exec "${PEER_NS}" ip addr add $V4_ADDR1/24 dev veth1
80 ip netns exec ${PEER_NS} ip route add default via $V4_ADDR1 dev veth1
81 ip route add default via $V4_ADDR0 dev veth0
83 sysctl -w "$1" >/dev/null 2>&1
85 # Establish an ARP cache entry
86 ping -c1 -I veth0 $V4_ADDR1 -q >/dev/null 2>&1
87 # Should have the veth1 entry in ARP table
88 ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1
95 # Set veth1 down, which will put veth0 in NOCARRIER state
96 ip netns exec "${PEER_NS}" ip link set veth1 down
100 ip neigh flush dev veth0
102 local -r ns="$(ip netns list|grep $PEER_NS)"
103 [ -n "$ns" ] && ip netns del $ns 2>/dev/null
105 sysctl -w net.ipv4.conf.veth0.arp_evict_nocarrier=1 >/dev/null 2>&1
106 sysctl -w net.ipv4.conf.all.arp_evict_nocarrier=1 >/dev/null 2>&1
109 # Run test when arp_evict_nocarrier = 1 (default).
110 run_arp_evict_nocarrier_enabled() {
111 echo "run arp_evict_nocarrier=1 test"
112 setup_v4 "net.ipv4.conf.veth0.arp_evict_nocarrier=1"
114 # ARP table should be empty
115 ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1
127 # Run test when arp_evict_nocarrier = 0
128 run_arp_evict_nocarrier_disabled() {
129 echo "run arp_evict_nocarrier=0 test"
130 setup_v4 "net.ipv4.conf.veth0.arp_evict_nocarrier=0"
132 # ARP table should still contain the entry
133 ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1
145 run_arp_evict_nocarrier_disabled_all() {
146 echo "run all.arp_evict_nocarrier=0 test"
147 setup_v4 "net.ipv4.conf.all.arp_evict_nocarrier=0"
149 # ARP table should still contain the entry
150 ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1
161 run_ndisc_evict_nocarrier_enabled() {
162 echo "run ndisc_evict_nocarrier=1 test"
164 setup_v6 "net.ipv6.conf.veth1.ndisc_evict_nocarrier=1"
166 ip netns exec me ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1
178 run_ndisc_evict_nocarrier_disabled() {
179 echo "run ndisc_evict_nocarrier=0 test"
181 setup_v6 "net.ipv6.conf.veth1.ndisc_evict_nocarrier=0"
183 ip netns exec me ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1
195 run_ndisc_evict_nocarrier_disabled_all() {
196 echo "run all.ndisc_evict_nocarrier=0 test"
198 setup_v6 "net.ipv6.conf.all.ndisc_evict_nocarrier=0"
200 ip netns exec me ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1
213 run_arp_evict_nocarrier_enabled
214 run_arp_evict_nocarrier_disabled
215 run_arp_evict_nocarrier_disabled_all
216 run_ndisc_evict_nocarrier_enabled
217 run_ndisc_evict_nocarrier_disabled
218 run_ndisc_evict_nocarrier_disabled_all
221 if [ "$(id -u)" -ne 0 ];then
222 echo "SKIP: Need root privileges"