blob: c79bf4a633a8dad1dc3c2ab233b97a41250ee426 [file] [log] [blame]
Zack Williams4bd2dbc2016-03-10 12:50:02 -07001#!/bin/sh
2
3SHELL="/bin/bash"
4
5NIC=$( route|grep default|awk '{print $NF}' )
6
7NAME="${1}"
8OP="${2}"
9SUBOP="${3}"
10ARGS="${4}"
11
12add_port_fwd_rule() {
13 DPORT=$1
Zack Williams709f11b2016-03-17 14:29:51 -070014 VMIP=$2
Zack Williams4bd2dbc2016-03-10 12:50:02 -070015 TOPORT=$3
16
Zack Williams4bd2dbc2016-03-10 12:50:02 -070017 iptables -t nat -C PREROUTING -p tcp -i $NIC --dport $DPORT -j DNAT --to-destination $VMIP:$TOPORT
18 if [ "$?" -ne 0 ]
19 then
20 iptables -t nat -A PREROUTING -p tcp -i $NIC --dport $DPORT -j DNAT --to-destination $VMIP:$TOPORT
21 fi
22}
23
24if [ "$OP" = "start" ] || [ "$OP" = "reload" ]
25then
26 iptables -t nat -F
27
28{% for vm in head_vm_list -%}
29{% if vm.forwarded_ports is defined -%}
30{% for port in vm.forwarded_ports -%}
Zack Williams709f11b2016-03-17 14:29:51 -070031 add_port_fwd_rule {{ port.ext }} "{{ mgmtbr_prefix }}.{{ vm.ipv4_last_octet }}" {{ port.int }}
Zack Williams4bd2dbc2016-03-10 12:50:02 -070032{% endfor -%}
33{% endif -%}
34{% endfor -%}
35
36 # Also flush the filter table before rules re-added
37 iptables -F
38fi
39