Use MACVLAN instead of bridge
[nodemanager-topo.git] / setup-nat
1 #!/bin/sh +x
2
3 IP=/sbin/ip
4
5 SLICE=$1
6 SLICEID=`id -u $SLICE`
7 NODEID=$2
8 KEY=$3
9
10 modprobe etun
11
12 #
13 # OpenVPN uses addresses in 10.<nodeid>/16 block.  Avoid collisions with
14 # this block.  NAT interface is not advertised and so does not require
15 # unique address throughout the topology.  But the address of each slice's
16 # NAT interface must be unique on a single node.
17 #
18 BASE="10.0.$KEY"
19 EXTIP=$BASE.1
20 EXTNET=$EXTIP/24
21 INTIP=$BASE.2
22 INTNET=$INTIP/24
23 BASENET=$BASE.0/24
24
25 ### Setup etun
26 ETUN0=nat$KEY
27 ETUN1=natx$KEY
28 ip link add name $ETUN0 type veth peer name $ETUN1
29 ifconfig $ETUN1 $EXTNET up
30
31 /sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
32 /sbin/iptables -A FORWARD -i eth0 -o $ETUN1 -m state --state RELATED,ESTABLISHED -j ACCEPT
33 /sbin/iptables -A FORWARD -i $ETUN1 -o eth0 -j ACCEPT
34
35 ### Put a process in the vserver so we can move the interface there
36 su $SLICE -c "sleep 30 &"
37 PID=`su $SLICE -c "pgrep -n sleep"`
38 $IP link set $ETUN0 netns $PID
39 naddress --add --nid $SLICEID --ip $INTNET
40 sleep 1
41
42 su $SLICE -c "sudo /sbin/ifconfig $ETUN0 $INTNET up; \
43  sudo /sbin/route add default gw $EXTIP; \
44  sudo /sbin/ifconfig lo 127.0.0.1/8 up"
45