route_localnet.sh 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Run a couple of tests when route_localnet = 1.
  5. readonly PEER_NS="ns-peer-$(mktemp -u XXXXXX)"
  6. setup() {
  7. ip netns add "${PEER_NS}"
  8. ip -netns "${PEER_NS}" link set dev lo up
  9. ip link add name veth0 type veth peer name veth1
  10. ip link set dev veth0 up
  11. ip link set dev veth1 netns "${PEER_NS}"
  12. # Enable route_localnet and delete useless route 127.0.0.0/8.
  13. sysctl -w net.ipv4.conf.veth0.route_localnet=1
  14. ip netns exec "${PEER_NS}" sysctl -w net.ipv4.conf.veth1.route_localnet=1
  15. ip route del 127.0.0.0/8 dev lo table local
  16. ip netns exec "${PEER_NS}" ip route del 127.0.0.0/8 dev lo table local
  17. ifconfig veth0 127.25.3.4/24 up
  18. ip netns exec "${PEER_NS}" ifconfig veth1 127.25.3.14/24 up
  19. ip route flush cache
  20. ip netns exec "${PEER_NS}" ip route flush cache
  21. }
  22. cleanup() {
  23. ip link del veth0
  24. ip route add local 127.0.0.0/8 dev lo proto kernel scope host src 127.0.0.1
  25. local -r ns="$(ip netns list|grep $PEER_NS)"
  26. [ -n "$ns" ] && ip netns del $ns 2>/dev/null
  27. }
  28. # Run test when arp_announce = 2.
  29. run_arp_announce_test() {
  30. echo "run arp_announce test"
  31. setup
  32. sysctl -w net.ipv4.conf.veth0.arp_announce=2
  33. ip netns exec "${PEER_NS}" sysctl -w net.ipv4.conf.veth1.arp_announce=2
  34. ping -c5 -I veth0 127.25.3.14
  35. if [ $? -ne 0 ];then
  36. echo "failed"
  37. else
  38. echo "ok"
  39. fi
  40. cleanup
  41. }
  42. # Run test when arp_ignore = 3.
  43. run_arp_ignore_test() {
  44. echo "run arp_ignore test"
  45. setup
  46. sysctl -w net.ipv4.conf.veth0.arp_ignore=3
  47. ip netns exec "${PEER_NS}" sysctl -w net.ipv4.conf.veth1.arp_ignore=3
  48. ping -c5 -I veth0 127.25.3.14
  49. if [ $? -ne 0 ];then
  50. echo "failed"
  51. else
  52. echo "ok"
  53. fi
  54. cleanup
  55. }
  56. run_all_tests() {
  57. run_arp_announce_test
  58. run_arp_ignore_test
  59. }
  60. run_all_tests