#!/bin/bash # TOPO: # host1 172.16.1.1/24 <-> .254/24 RTR .254/24 <-> 192.168.1.1/24 host2 # <-> .253/24 RTR2 .254/24 <-> 192.168.2.1/24 host3 netns_list="host1 host2 host3 RTR RTR2" for i in $netns_list; do ip netns del $i > /dev/null 2>&1 done for i in $netns_list; do ip netns add $i done ip link add host1_eth1 type veth peer name RTR_eth1 ip link add host1_eth2 type veth peer name RTR2_eth1 ip link add host2_eth1 type veth peer name RTR_eth2 ip link add host3_eth1 type veth peer name RTR2_eth2 ip link set RTR_eth1 netns RTR ip link set RTR2_eth1 netns RTR2 ip link set RTR_eth2 netns RTR ip link set RTR2_eth2 netns RTR2 ip link set host1_eth1 netns host1 ip link set host1_eth2 netns host1 ip link set host2_eth1 netns host2 ip link set host3_eth1 netns host3 ip netns exec host1 brctl addbr host1_br0 ip netns exec host1 brctl addif host1_br0 host1_eth1 ip netns exec host1 brctl addif host1_br0 host1_eth2 ip netns exec host1 ifconfig host1_br0 172.16.1.1/24 up ip netns exec host1 ip link set host1_eth1 up ip netns exec host1 ip link set host1_eth2 up ip netns exec RTR ifconfig RTR_eth1 172.16.1.254/24 up ip netns exec RTR2 ifconfig RTR2_eth1 172.16.1.253/24 up ip netns exec RTR ifconfig RTR_eth2 192.168.1.254/24 up ip netns exec RTR2 ifconfig RTR2_eth2 192.168.2.254/24 up ip netns exec RTR sysctl -w net.ipv4.conf.all.forwarding=1 ip netns exec RTR2 sysctl -w net.ipv4.conf.all.forwarding=1 ip netns exec RTR sysctl -w net.ipv4.conf.RTR_eth1.bc_forwarding=1 ip netns exec RTR2 sysctl -w net.ipv4.conf.RTR2_eth1.bc_forwarding=1 ip netns exec RTR sysctl -w net.ipv4.conf.all.bc_forwarding=1 ip netns exec RTR2 sysctl -w net.ipv4.conf.all.bc_forwarding=1 ip netns exec host2 ifconfig host2_eth1 192.168.1.1/24 up ip netns exec host3 ifconfig host3_eth1 192.168.2.1/24 up ip netns exec host1 ip route add default nexthop via 172.16.1.254 nexthop via 172.16.1.253 ip netns exec host2 ip route add default via 192.168.1.254 ip netns exec host3 ip route add default via 192.168.2.254 ip netns exec host2 sysctl -w net.ipv4.icmp_echo_ignore_broadcasts=0 ip netns exec host3 sysctl -w net.ipv4.icmp_echo_ignore_broadcasts=0 ip netns exec RTR sysctl -w net.ipv4.icmp_echo_ignore_broadcasts=0 ip netns exec RTR2 sysctl -w net.ipv4.icmp_echo_ignore_broadcasts=0 ip netns exec host1 ping 192.168.2.255 -c 1 # ip netns exec host3 tcpdump -i host3_eth1 -p icmp -nn # ip netns exec host1 ping 192.168.1.255 -c 1 # ip netns exec host2 tcpdump -i host2_eth1 -p icmp -nn # ip netns exec host1 ping 255.255.255.255 -c 1