1#!/bin/bash 2# 4 mesh nodes in a diamond topology 3# paths must go through one of two intermediate nodes. 4 5num_nodes=4 6session=wmediumd 7subnet=10.10.10 8macfmt='02:00:00:00:%02x:00' 9 10. func 11 12if [[ $UID -ne 0 ]]; then 13 echo "Sorry, run me as root." 14 exit 1 15fi 16 17if [[ $# -eq 0 ]]; then 18 freq=2412 19else 20 freq=$1 21fi 22 23modprobe -r mac80211_hwsim 24modprobe mac80211_hwsim radios=$num_nodes 25iw reg set US 26 27for i in `seq 0 $((num_nodes-1))`; do 28 addrs[$i]=`printf $macfmt $i` 29done 30 31cat <<__EOM > diamond.cfg 32ifaces : 33{ 34 ids = [ 35 "02:00:00:00:00:00", 36 "02:00:00:00:01:00", 37 "02:00:00:00:02:00", 38 "02:00:00:00:03:00" 39 ]; 40 41 links = ( 42 (0, 1, 10), 43 (0, 2, 20), 44 (0, 3, 0), 45 (1, 2, 30), 46 (1, 3, 10), 47 (2, 3, 20) 48 ); 49}; 50__EOM 51 52tmux new -s $session -d 53# find out the index of the first window as we can't assume zero-indexing 54first_idx=`tmux list-windows -t $session | head -n1 | cut -d: -f1` 55 56rm /tmp/netns.pid.* 2>/dev/null 57i=0 58for addr in ${addrs[@]}; do 59 phy=`addr2phy $addr` 60 dev=`ls /sys/class/ieee80211/$phy/device/net` 61 phys[$i]=$phy 62 devs[$i]=$dev 63 64 ip=${subnet}.$((10 + i)) 65 66 # put this phy in own netns and tmux window, and start a mesh node 67 tmux new-window -t $session 68 69 # start netns 70 pidfile=/tmp/netns.pid.$i 71 win=$session:$((first_idx+i+1)) 72 tmux send-keys -t $win 'lxc-unshare -s NETWORK /bin/bash' C-m 73 tmux send-keys -t $win 'echo $$ > '$pidfile C-m 74 75 # wait for netns to exist 76 while [[ ! -e $pidfile ]]; do 77 echo "Waiting for netns $i -- $pidfile" 78 sleep 0.5 79 done 80 81 tmux send-keys -t $session:$first_idx \ 82 'iw phy '$phy' set netns `cat '$pidfile'`' C-m 83 84 # wait for phy to exist in netns 85 while [[ -e /sys/class/ieee80211/$phy ]]; do 86 echo "Waiting for $phy to move to netns..." 87 sleep 0.5 88 done 89 90 # start mesh node 91 tmux send-keys -t $win '. func' C-m 92 tmux send-keys -t $win 'meshup-iw '$dev' diamond '$freq' '$ip C-m 93 94 i=$((i+1)) 95done 96 97# start wmediumd 98tmux send-keys -t $session:$first_idx '../wmediumd/wmediumd -c diamond.cfg' C-m 99 100# start iperf server on 10.10.10.13 101node_idx=$((first_idx + 4)) 102tmux send-keys -t $session:$node_idx 'iperf -s' C-m 103 104# enable monitor 105tmux new-window -t $session 106cap_idx=$((first_idx + 5)) 107tmux send-keys -t $session:$cap_idx 'ip link set hwsim0 up' C-m 108# capture traffic as normal user (if possible) or root 109CAP_USER=${SUDO_USER:-root} 110tmux send-keys -t $session:$cap_idx "sudo -u $CAP_USER dumpcap -i hwsim0" C-m 111 112node_idx=$((first_idx + 1)) 113tmux select-window -t $session:$node_idx 114tmux send-keys -t $session:$node_idx 'ping -c 5 10.10.10.13' C-m 115tmux send-keys -t $session:$node_idx 'iperf -c 10.10.10.13 -i 5 -t 120' 116 117tmux attach 118