Undo change to tag, commit to trunk instead
[vsys.git] / factory / setup-nat
1 #!/bin/sh +x
2
3 IP=/sbin/ip
4
5 SLICE=$1
6 SLICEID=`id -u $SLICE`
7 read KEY
8
9 modprobe etun
10
11 ### Setup etun
12 ETUN0=nat$KEY
13 ETUN1=natx$KEY
14 echo $ETUN0,$ETUN1 > /sys/module/etun/parameters/newif
15 ifconfig $ETUN1 10.0.$KEY.1 up
16
17 /sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
18 /sbin/iptables -A FORWARD -i eth0 -o $ETUN1 -m state --state RELATED,ESTABLISHED -j ACCEPT
19 /sbin/iptables -A FORWARD -i $ETUN1 -o eth0 -j ACCEPT
20
21 ### Create "grab link" script
22 GRAB=/vsys/local_grab-$ETUN0
23 echo $SLICE > $GRAB.acl 
24 rm -f $GRAB
25 cat > $GRAB <<EOF
26 #!/bin/sh
27
28 read PID
29
30 chcontext --ctx 1 -- echo \$PID > /sys/class/net/$ETUN0/new_ns_pid 
31 EOF
32 chmod +x $GRAB
33
34 ### Create "delete link" script
35 DELETE=/vsys/local_delete-$ETUN0
36 echo $SLICE > $DELETE.acl 
37 rm -f $DELETE
38 cat > $DELETE <<EOF
39 #!/bin/sh
40
41 read NULL
42
43 # Remove iptables rules
44 /sbin/iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
45 /sbin/iptables -D FORWARD -i eth0 -o $ETUN1 -m state --state RELATED,ESTABLISHED -j ACCEPT
46 /sbin/iptables -D FORWARD -i $ETUN1 -o eth0 -j ACCEPT
47
48 # Get rid of etun devices, only need name of one of them
49 echo $ETUN1 > /sys/module/etun/parameters/delif
50
51 # Clean up files
52 rm -f $GRAB $GRAB.acl
53 rm -f $DELETE $DELETE.acl
54
55 EOF
56 chmod +x $DELETE
57