1#!/bin/bash 2# SPDX-License-Identifier: GPL-2.0 3# 4# In-place tunneling 5 6# must match the port that the bpf program filters on 7readonly port=8000 8 9readonly ns_prefix="ns-$$-" 10readonly ns1="${ns_prefix}1" 11readonly ns2="${ns_prefix}2" 12 13readonly ns1_v4=192.168.1.1 14readonly ns2_v4=192.168.1.2 15readonly ns1_v6=fd::1 16readonly ns2_v6=fd::2 17 18# Must match port used by bpf program 19readonly udpport=5555 20# MPLSoverUDP 21readonly mplsudpport=6635 22readonly mplsproto=137 23 24readonly infile="$(mktemp)" 25readonly outfile="$(mktemp)" 26 27setup() { 28 ip netns add "${ns1}" 29 ip netns add "${ns2}" 30 31 ip link add dev veth1 mtu 1500 netns "${ns1}" type veth \ 32 peer name veth2 mtu 1500 netns "${ns2}" 33 34 ip netns exec "${ns1}" ethtool -K veth1 tso off 35 36 ip -netns "${ns1}" link set veth1 up 37 ip -netns "${ns2}" link set veth2 up 38 39 ip -netns "${ns1}" -4 addr add "${ns1_v4}/24" dev veth1 40 ip -netns "${ns2}" -4 addr add "${ns2_v4}/24" dev veth2 41 ip -netns "${ns1}" -6 addr add "${ns1_v6}/64" dev veth1 nodad 42 ip -netns "${ns2}" -6 addr add "${ns2_v6}/64" dev veth2 nodad 43 44 # clamp route to reserve room for tunnel headers 45 ip -netns "${ns1}" -4 route flush table main 46 ip -netns "${ns1}" -6 route flush table main 47 ip -netns "${ns1}" -4 route add "${ns2_v4}" mtu 1450 dev veth1 48 ip -netns "${ns1}" -6 route add "${ns2_v6}" mtu 1430 dev veth1 49 50 sleep 1 51 52 dd if=/dev/urandom of="${infile}" bs="${datalen}" count=1 status=none 53} 54 55cleanup() { 56 ip netns del "${ns2}" 57 ip netns del "${ns1}" 58 59 if [[ -f "${outfile}" ]]; then 60 rm "${outfile}" 61 fi 62 if [[ -f "${infile}" ]]; then 63 rm "${infile}" 64 fi 65 66 if [[ -n $server_pid ]]; then 67 kill $server_pid 2> /dev/null 68 fi 69} 70 71server_listen() { 72 ip netns exec "${ns2}" nc "${netcat_opt}" -l "${port}" > "${outfile}" & 73 server_pid=$! 74 sleep 0.2 75} 76 77client_connect() { 78 ip netns exec "${ns1}" timeout 2 nc "${netcat_opt}" -w 1 "${addr2}" "${port}" < "${infile}" 79 echo $? 80} 81 82verify_data() { 83 wait "${server_pid}" 84 server_pid= 85 # sha1sum returns two fields [sha1] [filepath] 86 # convert to bash array and access first elem 87 insum=($(sha1sum ${infile})) 88 outsum=($(sha1sum ${outfile})) 89 if [[ "${insum[0]}" != "${outsum[0]}" ]]; then 90 echo "data mismatch" 91 exit 1 92 fi 93} 94 95set -e 96 97# no arguments: automated test, run all 98if [[ "$#" -eq "0" ]]; then 99 echo "ipip" 100 $0 ipv4 ipip none 100 101 102 echo "ip6ip6" 103 $0 ipv6 ip6tnl none 100 104 105 echo "sit" 106 $0 ipv6 sit none 100 107 108 echo "ip4 vxlan" 109 $0 ipv4 vxlan eth 2000 110 111 echo "ip6 vxlan" 112 $0 ipv6 ip6vxlan eth 2000 113 114 for mac in none mpls eth ; do 115 echo "ip gre $mac" 116 $0 ipv4 gre $mac 100 117 118 echo "ip6 gre $mac" 119 $0 ipv6 ip6gre $mac 100 120 121 echo "ip gre $mac gso" 122 $0 ipv4 gre $mac 2000 123 124 echo "ip6 gre $mac gso" 125 $0 ipv6 ip6gre $mac 2000 126 127 echo "ip udp $mac" 128 $0 ipv4 udp $mac 100 129 130 echo "ip6 udp $mac" 131 $0 ipv6 ip6udp $mac 100 132 133 echo "ip udp $mac gso" 134 $0 ipv4 udp $mac 2000 135 136 echo "ip6 udp $mac gso" 137 $0 ipv6 ip6udp $mac 2000 138 done 139 140 echo "OK. All tests passed" 141 exit 0 142fi 143 144if [[ "$#" -ne "4" ]]; then 145 echo "Usage: $0" 146 echo " or: $0 <ipv4|ipv6> <tuntype> <none|mpls|eth> <data_len>" 147 exit 1 148fi 149 150case "$1" in 151"ipv4") 152 readonly addr1="${ns1_v4}" 153 readonly addr2="${ns2_v4}" 154 readonly ipproto=4 155 readonly netcat_opt=-${ipproto} 156 readonly foumod=fou 157 readonly foutype=ipip 158 readonly fouproto=4 159 readonly fouproto_mpls=${mplsproto} 160 readonly gretaptype=gretap 161 ;; 162"ipv6") 163 readonly addr1="${ns1_v6}" 164 readonly addr2="${ns2_v6}" 165 readonly ipproto=6 166 readonly netcat_opt=-${ipproto} 167 readonly foumod=fou6 168 readonly foutype=ip6tnl 169 readonly fouproto="41 -6" 170 readonly fouproto_mpls="${mplsproto} -6" 171 readonly gretaptype=ip6gretap 172 ;; 173*) 174 echo "unknown arg: $1" 175 exit 1 176 ;; 177esac 178 179readonly tuntype=$2 180readonly mac=$3 181readonly datalen=$4 182 183echo "encap ${addr1} to ${addr2}, type ${tuntype}, mac ${mac} len ${datalen}" 184 185trap cleanup EXIT 186 187setup 188 189# basic communication works 190echo "test basic connectivity" 191server_listen 192client_connect 193verify_data 194 195# clientside, insert bpf program to encap all TCP to port ${port} 196# client can no longer connect 197ip netns exec "${ns1}" tc qdisc add dev veth1 clsact 198ip netns exec "${ns1}" tc filter add dev veth1 egress \ 199 bpf direct-action object-file ./test_tc_tunnel.o \ 200 section "encap_${tuntype}_${mac}" 201echo "test bpf encap without decap (expect failure)" 202server_listen 203! client_connect 204 205if [[ "$tuntype" =~ "udp" ]]; then 206 # Set up fou tunnel. 207 ttype="${foutype}" 208 targs="encap fou encap-sport auto encap-dport $udpport" 209 # fou may be a module; allow this to fail. 210 modprobe "${foumod}" ||true 211 if [[ "$mac" == "mpls" ]]; then 212 dport=${mplsudpport} 213 dproto=${fouproto_mpls} 214 tmode="mode any ttl 255" 215 else 216 dport=${udpport} 217 dproto=${fouproto} 218 fi 219 ip netns exec "${ns2}" ip fou add port $dport ipproto ${dproto} 220 targs="encap fou encap-sport auto encap-dport $dport" 221elif [[ "$tuntype" =~ "gre" && "$mac" == "eth" ]]; then 222 ttype=$gretaptype 223elif [[ "$tuntype" =~ "vxlan" && "$mac" == "eth" ]]; then 224 ttype="vxlan" 225 targs="id 1 dstport 8472 udp6zerocsumrx" 226else 227 ttype=$tuntype 228 targs="" 229fi 230 231# tunnel address family differs from inner for SIT 232if [[ "${tuntype}" == "sit" ]]; then 233 link_addr1="${ns1_v4}" 234 link_addr2="${ns2_v4}" 235else 236 link_addr1="${addr1}" 237 link_addr2="${addr2}" 238fi 239 240# serverside, insert decap module 241# server is still running 242# client can connect again 243ip netns exec "${ns2}" ip link add name testtun0 type "${ttype}" \ 244 ${tmode} remote "${link_addr1}" local "${link_addr2}" $targs 245 246expect_tun_fail=0 247 248if [[ "$tuntype" == "ip6udp" && "$mac" == "mpls" ]]; then 249 # No support for MPLS IPv6 fou tunnel; expect failure. 250 expect_tun_fail=1 251elif [[ "$tuntype" =~ "udp" && "$mac" == "eth" ]]; then 252 # No support for TEB fou tunnel; expect failure. 253 expect_tun_fail=1 254elif [[ "$tuntype" =~ (gre|vxlan) && "$mac" == "eth" ]]; then 255 # Share ethernet address between tunnel/veth2 so L2 decap works. 256 ethaddr=$(ip netns exec "${ns2}" ip link show veth2 | \ 257 awk '/ether/ { print $2 }') 258 ip netns exec "${ns2}" ip link set testtun0 address $ethaddr 259elif [[ "$mac" == "mpls" ]]; then 260 modprobe mpls_iptunnel ||true 261 modprobe mpls_gso ||true 262 ip netns exec "${ns2}" sysctl -qw net.mpls.platform_labels=65536 263 ip netns exec "${ns2}" ip -f mpls route add 1000 dev lo 264 ip netns exec "${ns2}" ip link set lo up 265 ip netns exec "${ns2}" sysctl -qw net.mpls.conf.testtun0.input=1 266 ip netns exec "${ns2}" sysctl -qw net.ipv4.conf.lo.rp_filter=0 267fi 268 269# Because packets are decapped by the tunnel they arrive on testtun0 from 270# the IP stack perspective. Ensure reverse path filtering is disabled 271# otherwise we drop the TCP SYN as arriving on testtun0 instead of the 272# expected veth2 (veth2 is where 192.168.1.2 is configured). 273ip netns exec "${ns2}" sysctl -qw net.ipv4.conf.all.rp_filter=0 274# rp needs to be disabled for both all and testtun0 as the rp value is 275# selected as the max of the "all" and device-specific values. 276ip netns exec "${ns2}" sysctl -qw net.ipv4.conf.testtun0.rp_filter=0 277ip netns exec "${ns2}" ip link set dev testtun0 up 278if [[ "$expect_tun_fail" == 1 ]]; then 279 # This tunnel mode is not supported, so we expect failure. 280 echo "test bpf encap with tunnel device decap (expect failure)" 281 ! client_connect 282else 283 echo "test bpf encap with tunnel device decap" 284 client_connect 285 verify_data 286 server_listen 287fi 288 289# bpf_skb_net_shrink does not take tunnel flags yet, cannot update L3. 290if [[ "${tuntype}" == "sit" ]]; then 291 echo OK 292 exit 0 293fi 294 295# serverside, use BPF for decap 296ip netns exec "${ns2}" ip link del dev testtun0 297ip netns exec "${ns2}" tc qdisc add dev veth2 clsact 298ip netns exec "${ns2}" tc filter add dev veth2 ingress \ 299 bpf direct-action object-file ./test_tc_tunnel.o section decap 300echo "test bpf encap with bpf decap" 301client_connect 302verify_data 303 304echo OK 305