blob: 852aef6de499c476167ede25172171d2224fa9d1 [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 -%}
Zack Williams81470de2016-04-11 12:44:38 -070030{% set vm_net = ( virt_nets | selectattr("head_vms", "defined") | first ) %}
Zack Williams4bd2dbc2016-03-10 12:50:02 -070031{% for port in vm.forwarded_ports -%}
Zack Williams81470de2016-04-11 12:44:38 -070032 add_port_fwd_rule {{ port.ext }} "{{ vm_net.ipv4_prefix }}.{{ vm.ipv4_last_octet }}" {{ port.int }}
Zack Williams4bd2dbc2016-03-10 12:50:02 -070033{% endfor -%}
34{% endif -%}
35{% endfor -%}
36
37 # Also flush the filter table before rules re-added
38 iptables -F
39fi
40