Scripts for creating and deleting NAT interface inside a sliver
[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 ### Avoid IP address collisions between NAT and virtual links.
14 ### Virtual links have addresses 10.A.B.[23], where A < B always.
15 ### So make sure that A > B for NAT.
16 ###
17 if [ $KEY > $NODEID ]; then
18     BASE="10.$KEY.$NODEID"
19 else
20     BASE="10.$NODEID.$KEY"
21 fi
22
23 ### Setup etun
24 ETUN0=nat$KEY
25 ETUN1=natx$KEY
26 echo $ETUN0,$ETUN1 > /sys/module/etun/parameters/newif
27 ifconfig $ETUN1 $BASE.1 up
28
29 /sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
30 /sbin/iptables -A FORWARD -i eth0 -o $ETUN1 -m state --state RELATED,ESTABLISHED -j ACCEPT
31 /sbin/iptables -A FORWARD -i $ETUN1 -o eth0 -j ACCEPT
32
33 ### Put a process in the vserver so we can move the interface there
34 su $SLICE -c "sleep 30" &
35 sleep 1
36 PID=`su $SLICE -c "pgrep sleep"`
37 chcontext --ctx 1 -- echo $PID > /sys/class/net/$ETUN0/new_ns_pid 
38 sleep 1
39
40 su $SLICE -c "sudo /sbin/ifconfig $ETUN0 $BASE.2/24 up; \
41  sudo /sbin/route add -net $BASE.0/24 $ETUN0; \
42  sudo /sbin/route add default gw $BASE.1"
43