| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- #!/bin/bash
- # SPDX-License-Identifier: GPL-2.0
- #
- # Tests sysctl options {arp,ndisc}_evict_nocarrier={0,1}
- #
- # Create a veth pair and set IPs/routes on both. Then ping to establish
- # an entry in the ARP/ND table. Depending on the test set sysctl option to
- # 1 or 0. Set remote veth down which will cause local veth to go into a no
- # carrier state. Depending on the test check the ARP/ND table:
- #
- # {arp,ndisc}_evict_nocarrier=1 should contain no ARP/ND after no carrier
- # {arp,ndisc}_evict_nocarrer=0 should still contain the single ARP/ND entry
- #
- source lib.sh
- readonly V4_ADDR0=10.0.10.1
- readonly V4_ADDR1=10.0.10.2
- readonly V6_ADDR0=2001:db8:91::1
- readonly V6_ADDR1=2001:db8:91::2
- nsid=100
- ret=0
- cleanup_v6()
- {
- cleanup_ns ${me} ${peer}
- sysctl -w net.ipv6.conf.veth1.ndisc_evict_nocarrier=1 >/dev/null 2>&1
- sysctl -w net.ipv6.conf.all.ndisc_evict_nocarrier=1 >/dev/null 2>&1
- }
- setup_v6() {
- setup_ns me peer
- IP="ip -netns ${me}"
- $IP li add veth1 type veth peer name veth2
- $IP li set veth1 up
- $IP -6 addr add $V6_ADDR0/64 dev veth1 nodad
- $IP li set veth2 netns ${peer} up
- ip -netns ${peer} -6 addr add $V6_ADDR1/64 dev veth2 nodad
- ip netns exec ${me} sysctl -w $1 >/dev/null 2>&1
- # Establish an ND cache entry
- ip netns exec ${me} ping -6 -c1 -Iveth1 $V6_ADDR1 >/dev/null 2>&1
- # Should have the veth1 entry in ND table
- ip netns exec ${me} ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1
- if [ $? -ne 0 ]; then
- cleanup_v6
- echo "failed"
- exit 1
- fi
- # Set veth2 down, which will put veth1 in NOCARRIER state
- ip netns exec ${peer} ip link set veth2 down
- }
- setup_v4() {
- setup_ns PEER_NS
- ip link add name veth0 type veth peer name veth1
- ip link set dev veth0 up
- ip link set dev veth1 netns "${PEER_NS}"
- ip netns exec "${PEER_NS}" ip link set dev veth1 up
- ip addr add $V4_ADDR0/24 dev veth0
- ip netns exec "${PEER_NS}" ip addr add $V4_ADDR1/24 dev veth1
- ip netns exec ${PEER_NS} ip route add default via $V4_ADDR1 dev veth1
- ip route add default via $V4_ADDR0 dev veth0
- sysctl -w "$1" >/dev/null 2>&1
- # Establish an ARP cache entry
- ping -c1 -I veth0 $V4_ADDR1 -q >/dev/null 2>&1
- # Should have the veth1 entry in ARP table
- ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1
- if [ $? -ne 0 ]; then
- cleanup_v4
- echo "failed; is the system using MACAddressPolicy=persistent ?"
- exit 1
- fi
- # Set veth1 down, which will put veth0 in NOCARRIER state
- ip netns exec "${PEER_NS}" ip link set veth1 down
- }
- cleanup_v4() {
- ip neigh flush dev veth0
- ip link del veth0
- cleanup_ns $PEER_NS
- sysctl -w net.ipv4.conf.veth0.arp_evict_nocarrier=1 >/dev/null 2>&1
- sysctl -w net.ipv4.conf.all.arp_evict_nocarrier=1 >/dev/null 2>&1
- }
- # Run test when arp_evict_nocarrier = 1 (default).
- run_arp_evict_nocarrier_enabled() {
- echo "run arp_evict_nocarrier=1 test"
- setup_v4 "net.ipv4.conf.veth0.arp_evict_nocarrier=1"
- # ARP table should be empty
- ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1
- if [ $? -eq 0 ];then
- echo "failed"
- ret=1
- else
- echo "ok"
- fi
- cleanup_v4
- }
- # Run test when arp_evict_nocarrier = 0
- run_arp_evict_nocarrier_disabled() {
- echo "run arp_evict_nocarrier=0 test"
- setup_v4 "net.ipv4.conf.veth0.arp_evict_nocarrier=0"
- # ARP table should still contain the entry
- ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1
- if [ $? -eq 0 ];then
- echo "ok"
- else
- echo "failed"
- ret=1
- fi
- cleanup_v4
- }
- run_arp_evict_nocarrier_disabled_all() {
- echo "run all.arp_evict_nocarrier=0 test"
- setup_v4 "net.ipv4.conf.all.arp_evict_nocarrier=0"
- # ARP table should still contain the entry
- ip neigh get $V4_ADDR1 dev veth0 >/dev/null 2>&1
- if [ $? -eq 0 ];then
- echo "ok"
- else
- echo "failed"
- fi
- cleanup_v4
- }
- run_ndisc_evict_nocarrier_enabled() {
- echo "run ndisc_evict_nocarrier=1 test"
- setup_v6 "net.ipv6.conf.veth1.ndisc_evict_nocarrier=1"
- ip netns exec ${me} ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1
- if [ $? -eq 0 ];then
- echo "failed"
- ret=1
- else
- echo "ok"
- fi
- cleanup_v6
- }
- run_ndisc_evict_nocarrier_disabled() {
- echo "run ndisc_evict_nocarrier=0 test"
- setup_v6 "net.ipv6.conf.veth1.ndisc_evict_nocarrier=0"
- ip netns exec ${me} ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1
- if [ $? -eq 0 ];then
- echo "ok"
- else
- echo "failed"
- ret=1
- fi
- cleanup_v6
- }
- run_ndisc_evict_nocarrier_disabled_all() {
- echo "run all.ndisc_evict_nocarrier=0 test"
- setup_v6 "net.ipv6.conf.all.ndisc_evict_nocarrier=0"
- ip netns exec ${me} ip -6 neigh get $V6_ADDR1 dev veth1 >/dev/null 2>&1
- if [ $? -eq 0 ];then
- echo "ok"
- else
- echo "failed"
- ret=1
- fi
- cleanup_v6
- }
- run_all_tests() {
- run_arp_evict_nocarrier_enabled
- run_arp_evict_nocarrier_disabled
- run_arp_evict_nocarrier_disabled_all
- run_ndisc_evict_nocarrier_enabled
- run_ndisc_evict_nocarrier_disabled
- run_ndisc_evict_nocarrier_disabled_all
- }
- if [ "$(id -u)" -ne 0 ];then
- echo "SKIP: Need root privileges"
- exit $ksft_skip;
- fi
- run_all_tests
- exit $ret
|