tc_l2_redirect.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/bin/bash
  2. [[ -z $TC ]] && TC='tc'
  3. [[ -z $IP ]] && IP='ip'
  4. REDIRECT_USER='./tc_l2_redirect'
  5. REDIRECT_BPF='./tc_l2_redirect_kern.o'
  6. RP_FILTER=$(< /proc/sys/net/ipv4/conf/all/rp_filter)
  7. IPV6_FORWARDING=$(< /proc/sys/net/ipv6/conf/all/forwarding)
  8. function config_common {
  9. local tun_type=$1
  10. $IP netns add ns1
  11. $IP netns add ns2
  12. $IP link add ve1 type veth peer name vens1
  13. $IP link add ve2 type veth peer name vens2
  14. $IP link set dev ve1 up
  15. $IP link set dev ve2 up
  16. $IP link set dev ve1 mtu 1500
  17. $IP link set dev ve2 mtu 1500
  18. $IP link set dev vens1 netns ns1
  19. $IP link set dev vens2 netns ns2
  20. $IP -n ns1 link set dev lo up
  21. $IP -n ns1 link set dev vens1 up
  22. $IP -n ns1 addr add 10.1.1.101/24 dev vens1
  23. $IP -n ns1 addr add 2401:db01::65/64 dev vens1 nodad
  24. $IP -n ns1 route add default via 10.1.1.1 dev vens1
  25. $IP -n ns1 route add default via 2401:db01::1 dev vens1
  26. $IP -n ns2 link set dev lo up
  27. $IP -n ns2 link set dev vens2 up
  28. $IP -n ns2 addr add 10.2.1.102/24 dev vens2
  29. $IP -n ns2 addr add 2401:db02::66/64 dev vens2 nodad
  30. $IP -n ns2 addr add 10.10.1.102 dev lo
  31. $IP -n ns2 addr add 2401:face::66/64 dev lo nodad
  32. $IP -n ns2 link add ipt2 type ipip local 10.2.1.102 remote 10.2.1.1
  33. $IP -n ns2 link add ip6t2 type ip6tnl mode any local 2401:db02::66 remote 2401:db02::1
  34. $IP -n ns2 link set dev ipt2 up
  35. $IP -n ns2 link set dev ip6t2 up
  36. $IP netns exec ns2 $TC qdisc add dev vens2 clsact
  37. $IP netns exec ns2 $TC filter add dev vens2 ingress bpf da obj $REDIRECT_BPF sec drop_non_tun_vip
  38. if [[ $tun_type == "ipip" ]]; then
  39. $IP -n ns2 route add 10.1.1.0/24 dev ipt2
  40. $IP netns exec ns2 sysctl -q -w net.ipv4.conf.all.rp_filter=0
  41. $IP netns exec ns2 sysctl -q -w net.ipv4.conf.ipt2.rp_filter=0
  42. else
  43. $IP -n ns2 route add 10.1.1.0/24 dev ip6t2
  44. $IP -n ns2 route add 2401:db01::/64 dev ip6t2
  45. $IP netns exec ns2 sysctl -q -w net.ipv4.conf.all.rp_filter=0
  46. $IP netns exec ns2 sysctl -q -w net.ipv4.conf.ip6t2.rp_filter=0
  47. fi
  48. $IP addr add 10.1.1.1/24 dev ve1
  49. $IP addr add 2401:db01::1/64 dev ve1 nodad
  50. $IP addr add 10.2.1.1/24 dev ve2
  51. $IP addr add 2401:db02::1/64 dev ve2 nodad
  52. $TC qdisc add dev ve2 clsact
  53. $TC filter add dev ve2 ingress bpf da obj $REDIRECT_BPF sec l2_to_iptun_ingress_forward
  54. sysctl -q -w net.ipv4.conf.all.rp_filter=0
  55. sysctl -q -w net.ipv6.conf.all.forwarding=1
  56. }
  57. function cleanup {
  58. set +e
  59. [[ -z $DEBUG ]] || set +x
  60. $IP netns delete ns1 >& /dev/null
  61. $IP netns delete ns2 >& /dev/null
  62. $IP link del ve1 >& /dev/null
  63. $IP link del ve2 >& /dev/null
  64. $IP link del ipt >& /dev/null
  65. $IP link del ip6t >& /dev/null
  66. sysctl -q -w net.ipv4.conf.all.rp_filter=$RP_FILTER
  67. sysctl -q -w net.ipv6.conf.all.forwarding=$IPV6_FORWARDING
  68. rm -f /sys/fs/bpf/tc/globals/tun_iface
  69. [[ -z $DEBUG ]] || set -x
  70. set -e
  71. }
  72. function l2_to_ipip {
  73. echo -n "l2_to_ipip $1: "
  74. local dir=$1
  75. config_common ipip
  76. $IP link add ipt type ipip external
  77. $IP link set dev ipt up
  78. sysctl -q -w net.ipv4.conf.ipt.rp_filter=0
  79. sysctl -q -w net.ipv4.conf.ipt.forwarding=1
  80. if [[ $dir == "egress" ]]; then
  81. $IP route add 10.10.1.0/24 via 10.2.1.102 dev ve2
  82. $TC filter add dev ve2 egress bpf da obj $REDIRECT_BPF sec l2_to_iptun_ingress_redirect
  83. sysctl -q -w net.ipv4.conf.ve1.forwarding=1
  84. else
  85. $TC qdisc add dev ve1 clsact
  86. $TC filter add dev ve1 ingress bpf da obj $REDIRECT_BPF sec l2_to_iptun_ingress_redirect
  87. fi
  88. $REDIRECT_USER -U /sys/fs/bpf/tc/globals/tun_iface -i $(< /sys/class/net/ipt/ifindex)
  89. $IP netns exec ns1 ping -c1 10.10.1.102 >& /dev/null
  90. if [[ $dir == "egress" ]]; then
  91. # test direct egress to ve2 (i.e. not forwarding from
  92. # ve1 to ve2).
  93. ping -c1 10.10.1.102 >& /dev/null
  94. fi
  95. cleanup
  96. echo "OK"
  97. }
  98. function l2_to_ip6tnl {
  99. echo -n "l2_to_ip6tnl $1: "
  100. local dir=$1
  101. config_common ip6tnl
  102. $IP link add ip6t type ip6tnl mode any external
  103. $IP link set dev ip6t up
  104. sysctl -q -w net.ipv4.conf.ip6t.rp_filter=0
  105. sysctl -q -w net.ipv4.conf.ip6t.forwarding=1
  106. if [[ $dir == "egress" ]]; then
  107. $IP route add 10.10.1.0/24 via 10.2.1.102 dev ve2
  108. $IP route add 2401:face::/64 via 2401:db02::66 dev ve2
  109. $TC filter add dev ve2 egress bpf da obj $REDIRECT_BPF sec l2_to_ip6tun_ingress_redirect
  110. sysctl -q -w net.ipv4.conf.ve1.forwarding=1
  111. else
  112. $TC qdisc add dev ve1 clsact
  113. $TC filter add dev ve1 ingress bpf da obj $REDIRECT_BPF sec l2_to_ip6tun_ingress_redirect
  114. fi
  115. $REDIRECT_USER -U /sys/fs/bpf/tc/globals/tun_iface -i $(< /sys/class/net/ip6t/ifindex)
  116. $IP netns exec ns1 ping -c1 10.10.1.102 >& /dev/null
  117. $IP netns exec ns1 ping -6 -c1 2401:face::66 >& /dev/null
  118. if [[ $dir == "egress" ]]; then
  119. # test direct egress to ve2 (i.e. not forwarding from
  120. # ve1 to ve2).
  121. ping -c1 10.10.1.102 >& /dev/null
  122. ping -6 -c1 2401:face::66 >& /dev/null
  123. fi
  124. cleanup
  125. echo "OK"
  126. }
  127. cleanup
  128. test_names="l2_to_ipip l2_to_ip6tnl"
  129. test_dirs="ingress egress"
  130. if [[ $# -ge 2 ]]; then
  131. test_names=$1
  132. test_dirs=$2
  133. elif [[ $# -ge 1 ]]; then
  134. test_names=$1
  135. fi
  136. for t in $test_names; do
  137. for d in $test_dirs; do
  138. $t $d
  139. done
  140. done