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 1458 dev veth1 48 ip -netns "${ns1}" -6 route add "${ns2_v6}" mtu 1438 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 67server_listen() { 68 ip netns exec "${ns2}" nc "${netcat_opt}" -l -p "${port}" > "${outfile}" & 69 server_pid=$! 70 sleep 0.2 71} 72 73client_connect() { 74 ip netns exec "${ns1}" timeout 2 nc "${netcat_opt}" -w 1 "${addr2}" "${port}" < "${infile}" 75 echo $? 76} 77 78verify_data() { 79 wait "${server_pid}" 80 # sha1sum returns two fields [sha1] [filepath] 81 # convert to bash array and access first elem 82 insum=($(sha1sum ${infile})) 83 outsum=($(sha1sum ${outfile})) 84 if [[ "${insum[0]}" != "${outsum[0]}" ]]; then 85 echo "data mismatch" 86 exit 1 87 fi 88} 89 90set -e 91 92# no arguments: automated test, run all 93if [[ "$#" -eq "0" ]]; then 94 echo "ipip" 95 $0 ipv4 ipip none 100 96 97 echo "ip6ip6" 98 $0 ipv6 ip6tnl none 100 99 100 for mac in none mpls eth ; do 101 echo "ip gre $mac" 102 $0 ipv4 gre $mac 100 103 104 echo "ip6 gre $mac" 105 $0 ipv6 ip6gre $mac 100 106 107 echo "ip gre $mac gso" 108 $0 ipv4 gre $mac 2000 109 110 echo "ip6 gre $mac gso" 111 $0 ipv6 ip6gre $mac 2000 112 113 echo "ip udp $mac" 114 $0 ipv4 udp $mac 100 115 116 echo "ip6 udp $mac" 117 $0 ipv6 ip6udp $mac 100 118 119 echo "ip udp $mac gso" 120 $0 ipv4 udp $mac 2000 121 122 echo "ip6 udp $mac gso" 123 $0 ipv6 ip6udp $mac 2000 124 done 125 126 echo "OK. All tests passed" 127 exit 0 128fi 129 130if [[ "$#" -ne "4" ]]; then 131 echo "Usage: $0" 132 echo " or: $0 <ipv4|ipv6> <tuntype> <none|mpls|eth> <data_len>" 133 exit 1 134fi 135 136case "$1" in 137"ipv4") 138 readonly addr1="${ns1_v4}" 139 readonly addr2="${ns2_v4}" 140 readonly ipproto=4 141 readonly netcat_opt=-${ipproto} 142 readonly foumod=fou 143 readonly foutype=ipip 144 readonly fouproto=4 145 readonly fouproto_mpls=${mplsproto} 146 readonly gretaptype=gretap 147 ;; 148"ipv6") 149 readonly addr1="${ns1_v6}" 150 readonly addr2="${ns2_v6}" 151 readonly ipproto=6 152 readonly netcat_opt=-${ipproto} 153 readonly foumod=fou6 154 readonly foutype=ip6tnl 155 readonly fouproto="41 -6" 156 readonly fouproto_mpls="${mplsproto} -6" 157 readonly gretaptype=ip6gretap 158 ;; 159*) 160 echo "unknown arg: $1" 161 exit 1 162 ;; 163esac 164 165readonly tuntype=$2 166readonly mac=$3 167readonly datalen=$4 168 169echo "encap ${addr1} to ${addr2}, type ${tuntype}, mac ${mac} len ${datalen}" 170 171trap cleanup EXIT 172 173setup 174 175# basic communication works 176echo "test basic connectivity" 177server_listen 178client_connect 179verify_data 180 181# clientside, insert bpf program to encap all TCP to port ${port} 182# client can no longer connect 183ip netns exec "${ns1}" tc qdisc add dev veth1 clsact 184ip netns exec "${ns1}" tc filter add dev veth1 egress \ 185 bpf direct-action object-file ./test_tc_tunnel.o \ 186 section "encap_${tuntype}_${mac}" 187echo "test bpf encap without decap (expect failure)" 188server_listen 189! client_connect 190 191if [[ "$tuntype" =~ "udp" ]]; then 192 # Set up fou tunnel. 193 ttype="${foutype}" 194 targs="encap fou encap-sport auto encap-dport $udpport" 195 # fou may be a module; allow this to fail. 196 modprobe "${foumod}" ||true 197 if [[ "$mac" == "mpls" ]]; then 198 dport=${mplsudpport} 199 dproto=${fouproto_mpls} 200 tmode="mode any ttl 255" 201 else 202 dport=${udpport} 203 dproto=${fouproto} 204 fi 205 ip netns exec "${ns2}" ip fou add port $dport ipproto ${dproto} 206 targs="encap fou encap-sport auto encap-dport $dport" 207elif [[ "$tuntype" =~ "gre" && "$mac" == "eth" ]]; then 208 ttype=$gretaptype 209else 210 ttype=$tuntype 211 targs="" 212fi 213 214# serverside, insert decap module 215# server is still running 216# client can connect again 217ip netns exec "${ns2}" ip link add name testtun0 type "${ttype}" \ 218 ${tmode} remote "${addr1}" local "${addr2}" $targs 219 220expect_tun_fail=0 221 222if [[ "$tuntype" == "ip6udp" && "$mac" == "mpls" ]]; then 223 # No support for MPLS IPv6 fou tunnel; expect failure. 224 expect_tun_fail=1 225elif [[ "$tuntype" =~ "udp" && "$mac" == "eth" ]]; then 226 # No support for TEB fou tunnel; expect failure. 227 expect_tun_fail=1 228elif [[ "$tuntype" =~ "gre" && "$mac" == "eth" ]]; then 229 # Share ethernet address between tunnel/veth2 so L2 decap works. 230 ethaddr=$(ip netns exec "${ns2}" ip link show veth2 | \ 231 awk '/ether/ { print $2 }') 232 ip netns exec "${ns2}" ip link set testtun0 address $ethaddr 233elif [[ "$mac" == "mpls" ]]; then 234 modprobe mpls_iptunnel ||true 235 modprobe mpls_gso ||true 236 ip netns exec "${ns2}" sysctl -qw net.mpls.platform_labels=65536 237 ip netns exec "${ns2}" ip -f mpls route add 1000 dev lo 238 ip netns exec "${ns2}" ip link set lo up 239 ip netns exec "${ns2}" sysctl -qw net.mpls.conf.testtun0.input=1 240 ip netns exec "${ns2}" sysctl -qw net.ipv4.conf.lo.rp_filter=0 241fi 242 243# Because packets are decapped by the tunnel they arrive on testtun0 from 244# the IP stack perspective. Ensure reverse path filtering is disabled 245# otherwise we drop the TCP SYN as arriving on testtun0 instead of the 246# expected veth2 (veth2 is where 192.168.1.2 is configured). 247ip netns exec "${ns2}" sysctl -qw net.ipv4.conf.all.rp_filter=0 248# rp needs to be disabled for both all and testtun0 as the rp value is 249# selected as the max of the "all" and device-specific values. 250ip netns exec "${ns2}" sysctl -qw net.ipv4.conf.testtun0.rp_filter=0 251ip netns exec "${ns2}" ip link set dev testtun0 up 252if [[ "$expect_tun_fail" == 1 ]]; then 253 # This tunnel mode is not supported, so we expect failure. 254 echo "test bpf encap with tunnel device decap (expect failure)" 255 ! client_connect 256else 257 echo "test bpf encap with tunnel device decap" 258 client_connect 259 verify_data 260 server_listen 261fi 262 263# serverside, use BPF for decap 264ip netns exec "${ns2}" ip link del dev testtun0 265ip netns exec "${ns2}" tc qdisc add dev veth2 clsact 266ip netns exec "${ns2}" tc filter add dev veth2 ingress \ 267 bpf direct-action object-file ./test_tc_tunnel.o section decap 268echo "test bpf encap with bpf decap" 269client_connect 270verify_data 271 272echo OK 273