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