Change NAT address block
[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
20 ### Setup etun
21 ETUN0=nat$KEY
22 ETUN1=natx$KEY
23 echo $ETUN0,$ETUN1 > /sys/module/etun/parameters/newif
24 ifconfig $ETUN1 $BASE.1/24 up
25
26 /sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
27 /sbin/iptables -A FORWARD -i eth0 -o $ETUN1 -m state --state RELATED,ESTABLISHED -j ACCEPT
28 /sbin/iptables -A FORWARD -i $ETUN1 -o eth0 -j ACCEPT
29
30 ### Put a process in the vserver so we can move the interface there
31 su $SLICE -c "sleep 30" &
32 sleep 1
33 PID=`su $SLICE -c "pgrep sleep"`
34 chcontext --ctx 1 -- echo $PID > /sys/class/net/$ETUN0/new_ns_pid 
35 sleep 1
36
37 su $SLICE -c "sudo /sbin/ifconfig $ETUN0 $BASE.2/24 up; \
38  sudo /sbin/route add -net $BASE.0/24 $ETUN0; \
39  sudo /sbin/route add default gw $BASE.1"
40