Send-to-self on Linux

Just copy-paste of script and link to mailing-list archives. This will allow sending traffic to self on linux over physical or virtual wire. This is seriously cool trick and required patches are now included in kernel so it works out of the box on Ubuntu 12.04.

---- 8< (mk-tap-loop.sh) ----
#!/bin/sh -e

# reset interfaces
ip link del tap0 2>/dev/null || :
ip link del tap1 2>/dev/null || :

# create interfaces
vde_tunctl -t tap0
vde_tunctl -t tap1

# assign addresses
ip addr add 192.168.23.10/24 dev tap0
ip addr add 192.168.23.11/24 dev tap1

# put ifs up
ip link set tap0 up
ip link set tap1 up

# lower priority of kernel local table to 500
ip rule del pref   0 lookup local 2>/dev/null || :
ip rule del pref 500 lookup local 2>/dev/null || :
ip rule add pref 500 lookup local

# on rx side handle packets by local table, so we can receive them
echo 1 >/proc/sys/net/ipv4/conf/tap0/accept_local
echo 1 >/proc/sys/net/ipv4/conf/tap1/accept_local
ip rule del pref 10 2>/dev/null || :
ip rule del pref 11 2>/dev/null || :
ip rule add pref 10 iif tap0 lookup local
ip rule add pref 11 iif tap1 lookup local

# tx
ip rule del pref 100 2>/dev/null || :
ip rule del pref 101 2>/dev/null || :
ip rule add pref 100 to 192.168.23.10 lookup 100 # tap0 <- tap1
ip rule add pref 101 to 192.168.23.11 lookup 101 # tap1 <- tap0

ip route flush table 100
ip route flush table 101
ip route add default dev tap1 table 100
ip route add default dev tap0 table 101

# ensure (visually) we've set up it ok

echo
echo " >>> rules:"
ip rule

echo
echo " >>> tap(0|1) routing table:"
ip route show table all | grep '\<tap\(0\|1\)\>'

# tx path
echo
echo " >>> checking routing for tx path:"
ip route get 192.168.23.10 connected
ip route get 192.168.23.11 connected

# rx path
echo
echo " >>> checking routing for rx path:"
ip route get from 192.168.23.10 to 192.168.23.11 iif tap1
ip route get from 192.168.23.11 to 192.168.23.10 iif tap0

# start switch and connect switch-tap0 and switch-tap1
echo
echo " >>> ready to start vde_switch and connect wires..."
#read ## had to remote this line
screen sh -c 'screen sh -cx "sleep 4; vde_plug2tap tap0"; screen sh -cx "sleep 4; vde_plug2tap tap1"; sh -cx vde_switch'

# now e.g. ping 192.168.23.11 sends packets to tap0 which are received
# on tap1 and ICMP-ECHO'ed by kernel on tap1 and received on tap0.

Comments