pf.conf.j2 3.72 KB
Newer Older
Pietsch, Martin's avatar
Pietsch, Martin committed
1
2
3

{% if network.nat is defined %}
{%  set natifs = [] %}
Pietsch, Martin's avatar
Pietsch, Martin committed
4
{%  for nif in network.nat if not ("%s:%s" | format(nif.extdevice, nif.intdevice)) in natifs %}
Pietsch, Martin's avatar
Pietsch, Martin committed
5
6
7
{%    if loop.index == 1 %}
scrub in all
{%    endif %}
Pietsch, Martin's avatar
Pietsch, Martin committed
8
{%    do natifs.append("%s:%s" | format(nif.extdevice, nif.intdevice)) %}
Pietsch, Martin's avatar
Pietsch, Martin committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
{%    if nif.ipv4 is defined %}
{{     'nat on %s inet from %s:network to any -> (%s)' | format(nif.extdevice, nif.intdevice, nif.extdevice) }}
{%    endif %}
{%    if nif.ipv6 is defined %}
{{     'nat on %s inet6 from %s:network to any -> (%s)' | format(nif.extdevice, nif.intdevice, nif.extdevice) }}
{%    endif %}
{%  endfor %}

{%  for nif in network.nat %}
{%    for fwd in (nif.forwards | default({})) %}
{%      if fwd.to is defined and nif.ipv4 is defined %}
{%        set proto = ('proto %s' | format(fwd.protocol | default('tcp'))) %}
{%        set srcaddr = 'any' %}
{%        set srcport = '' %}
{%        if fwd.from is defined %}
{%          set srcaddr = ('%s' | format(fwd.from.ipv4 | default('any') )) %}
{%          set srcport = ('port %s' | format(fwd.from.port)) if fwd.from.port is defined else '' %}
{%        endif %}
{%        set destaddr = ('%s' | format(network.devices[nif.extdevice].ipv4)) if network.devices[nif.extdevice].ipv4 is defined else '' %}
{%        set destport = ('port %s' | format(fwd.to.port)) if fwd.to.port is defined else '' %}
{%        set natport = ('port %s' | format(fwd.to.natport)) if fwd.to.natport is defined else '' %}
{%        set rule = 'rdr on %s inet %s from %s %s to %s %s -> %s %s' | format(nif.extdevice, proto, srcaddr, srcport, destaddr, destport, nif.ipv4 | regex_replace("/[0-9]*", ""), natport) %}
{{        rule | regex_replace('\\s+', ' ') | trim() }}
{%      endif %}
{%      if fwd.to is defined and nif.ipv6 is defined %}
{%        set proto = ('proto %s' | format(fwd.protocol | default('tcp'))) %}
{%        set srcaddr = 'any' %}
{%        set srcport = '' %}
{%        if fwd.from is defined %}
{%          set srcaddr = ('%s' | format(fwd.from.ipv6 | default('any') )) %}
{%          set srcport = ('port %s' | format(fwd.from.port)) if fwd.from.port is defined else '' %}
{%        endif %}
{%        set destaddr = ('%s' | format(network.devices[nif.extdevice].ipv6)) if network.devices[nif.extdevice].ipv6 is defined else '' %}
{%        set destport = ('port %s' | format(fwd.to.port)) if fwd.to.port is defined else '' %}
{%        set natport = ('port %s' | format(fwd.to.natport)) if fwd.to.natport is defined else '' %}
{%        set rule = 'rdr on %s inet6 %s from %s %s to %s %s -> %s %s' | format(nif.extdevice, proto, srcaddr, srcport, destaddr, destport, nif.ipv6 | regex_replace("/[0-9]*", ""), natport) %}
{{        rule | regex_replace('\\s+', ' ') | trim() }}
{%      endif %}
{%    endfor %}
{%  endfor %}
{% endif %}

{% if network.firewall is defined %}
{%   for rl in (firewall.rules | default({})) %}
{%     set dir = ('%s' | format(rl.direction)) if rl.direction is defined else '' %}
{%     set if = ('on %s' | format(rl.interface)) if rl.interface is defined else '' %}
{%     set af = ('%s' | format(rl.af)) if rl.af is defined else '' %}
{%     set proto = ('proto %s' | format(rl.protocol)) if rl.protocol is defined else '' %}
{%     set srcaddr = ('from %s' | format(rl.src_address)) if rl.src_address is defined else '' %}
{%     set srcport = ('port %s' | format(rl.src_port)) if rl.src_port is defined else '' %}
{%     set dstaddr = ('to %s' | format(rl.dst_address)) if rl.dst_address is defined else '' %}
{%     set dstport = ('port %s' | format(rl.dst_port)) if rl.dst_port is defined else '' %}
{%     set rule = '%s %s %s %s %s %s %s %s %s' | format(rl.action, dir, if, af, proto, srcaddr, srcport, dstaddr, dstport) %} 
{{     rule | regex_replace('\\s+', ' ') | trim() }}
{%   endfor %}
{% endif %}