I have a few dozen servers behind OpenBSD firewall with port forwarding. Most rules are very similar and differ only in IPs or sometimes in ports forwarded, so I want to compact them to remove excessive repetition but I've found that it is impossible to use tables with rdr-to
rules. Is there any way to improve this configuration? May be there is option to use pf
macros to generate multiple rules at once? I can't use external preprocessor at the moment.
Example set of rules:
pass in on $extif proto tcp from any to 10.0.0.213 port {25,80,443} rdr-to 172.16.1.193
pass in on $intif proto tcp from $intnet to 10.0.0.213 port {25,80,443} rdr-to 172.16.1.193
pass out on $intif proto tcp from any to 172.16.1.193 port {25,80,443} received-on $intif nat-to $intif
pass in on $extif proto tcp from any to 10.0.0.214 port {25,80,443} rdr-to 172.16.1.194
pass in on $intif proto tcp from $intnet to 10.0.0.214 port {25,80,443} rdr-to 172.16.1.194
pass out on $intif proto tcp from any to 172.16.1.194 port {25,80,443} received-on $intif nat-to $intif
pass in on $extif proto tcp from any to 10.0.0.215 port {25,80,443,3389} rdr-to 172.16.1.195
pass in on $intif proto tcp from $intnet to 10.0.0.215 port {25,80,443,3389} rdr-to 172.16.1.195
pass out on $intif proto tcp from any to 172.16.1.195 port {25,80,443,3389} received-on $intif nat-to $intif
From the pf.conf(5) manpage:
You might be able to condense some of your rules by not specifying which interface you want the re-direction to take place on and allow pf to evaluate it for you.
For example:
Could be re-written as:
$extif
will be redirected to 172.16.1.193.$intif
will be redirected to 172.16.1.193.$intnet
to happily be redirected as well. This may or may not desirable.Without seeing your entire ruleset or really knowing what you're trying to accomplish I can only offer a proof-of-concept example.
One final note: I would really avoid doing this. There is a tendency (at least I have it) to want to write the fewest rules possible by having one rule do more than "one thing". This is bad. Firewalls are already frightfully complicated and seemly always misconfigured; why make it harder on yourself by crafting a byzantine ruleset? A longer ruleset with simpler rules will be easier to understand, maintenance and debug. Avoid the temptation to be overly clever.