Firewall Rule Definitions
Firewall configuration in debops.ferm is done through a flexible
definition of rules. There are a number of variables which are used to
reference a set of default rules and can be extended by user defined
rules. Here a description of the involved configurations should be given
so that everyone can customize the ruleset according to individual
requirements.
Default rules
By default debops.ferm configures a number of rules as soon as a
host is part of the [debops_all_hosts] Ansible host group. The rules
created by default are defined in defaults/main.yml and activated by
being listed in ferm__default_rules. They consist of basic rules for
setting the iptables default policies, restricting extensive connection
attempts, logging and more.
In case a firewall is not required or preferred this behaviour can be
disabled by setting ferm__enabled to False in the inventory.
Custom rules
A custom rule can be enabled by adding a rule definition to one of the
predefined rule lists (ferm__rules, ferm__group_rules,
ferm__host_rules or ferm__dependent_rules) in the Ansible
inventory. Each rule has to be defined as a YAML dict using some of
the following keys:
domainOptional. iptables domain used for the firewall rule. Possible values:
ip,ip6. Defaults toferm__domains.tableOptional. iptables table to which the rule is added or from which it is removed. Defaults to
filter.chainOptional. iptables chain to which the rule is added or from which it is removed. Defaults to
INPUT.by_roleOptional. Name of the Ansible role in the format
ROLE_OWNER.ROLE_NAMEwhich is responsible for the rule. This will be included as a comment in the generated rule file.
Depending on the chosen type, many additional variables are supported. Please check the individual rule type description below.
Rule types
There exist a number of predefined rule types for generating firewall rules through ferm. Following a list of the available rule types which can be used to create custom rules.
The 'accept' type
This rule type can be used to create rules that match interfaces, ports, remote IP addresses/subnets and can accept the packets, reject, or redirect to a different chain. The following type-specific YAML keys are supported:
accept_anyOptional. Match all source addresses by default. Possible values:
TrueorFalse. Defaults toTrue. If this option is set toFalseanditem.targetis set toREJECTall traffic is blocked by default. As soon asitem.saddris not empty, this configuration doesn't matter anymore.daddrOptional. List of destination IP addresses or networks to which the rule is applied.
dportOptional. List of destination ports to which the rule is applied.
includeOptional. Custom ferm configuration file to include. See ferm include for more details.
interfaceOptional. List of network interfaces for incoming packets to which the rule is applied.
interface_presentOptional. Same as
item.interfacebut first check if specified network interfaces exists before adding the firewall rules.multiportOptional. Use
iptables multiportextension. Possible values:TrueorFalse. Defaults toFalse.outerfaceOptional. List of network interfaces for outgoing packets to which the rule is applied.
outerface_presentOptional. Same as
item.outerfacebut first check if specified network interface exists before adding the firewall rule.protocolOptional. Network protocol to which the rule is applied.
protocol_synOptional. Match TCP packet with only the SYN flag set. Possible values
TrueorFalse. If set toFalseit will match all other packets except the ones with only the SYN flag set. Defaults to unset.realgotoOptional. After packet match jump to custom chain. See ferm realgoto for more details.
reject_withOptional. Define reject message being sent when the rule
item.targetis set toREJECT. Defaults toicmp-admin-prohibited.saddrOptional. List of source IP addresses or networks to which this rule is applied.
sportOptional. List of source ports to which the rule is applied.
stateOptional. Connection state which should be matched. Possible values:
INVALID,ESTABLISHED,NEW,RELATED,UNTRACKEDor comma-separated combination thereof.subchainOptional. Subchain name. If more than 3 addresses are listed in
target.saddrmove resulting iptables rules into a separate subchain with the given name. See ferm subchain for more details.targetOptional. iptables jump target. Possible values:
ACCEPT,DROP,REJECT,RETURN,NOPor a custom target. Defaults toACCEPT.
The 'ansible_controller' type
Similar to the accept type but defaults to the SSH target port and sets the
source address to the host running Ansible if not overwritten through the
item.ansible_controllers key. The following type-specific YAML keys are
supported:
ansible_controllersOptional. List of source IP address which are added to
item.saddr. Overwrites auto-detection of the Ansible controller address.daddrOptional. List of destination IP addresses or networks to which the rule is applied.
dportOptional. List of destination ports to which the rule is applied. Defaults to ssh.
includeOptional. Custom ferm configuration file to include. See ferm include for more details.
interfaceOptional. List of network interfaces for incoming packets to which the rule is applied.
multiportOptional. Use iptables multiport extension. Possible values:
TrueorFalse. Defaults toFalse.outerfaceOptional. List of network interfaces for outgoing packets to which the rule is applied.
protocolOptional. Network protocol to which the rule is applied. Defaults to
tcp.protocol_synOptional. Match TCP packet with only the SYN flag set. Possible values
TrueorFalse. If set toFalseit will match all other packets except the ones with only the SYN flag set. Defaults to unset.realgotoOptional. After packet match jump to custom chain. See ferm realgoto for more details.
reject_withOptional. Define reject message being sent when the rule
item.targetis set toREJECT. Defaults toicmp-admin-prohibited.saddrOptional. List of source IP addresses or networks to which this rule is applied.
sportOptional. List of source ports to which the rule is applied.
stateOptional. Connection state which should be matched. Possible values:
INVALID,ESTABLISHED,NEW,RELATED,UNTRACKEDor comma-separated combination thereof.subchainOptional. Subchain name. If more than 3 addresses are listed in
target.saddrmove resulting iptables rules into a separate subchain with the given name. See ferm subchain for more details.targetOptional. iptables jump target. Possible values:
ACCEPT,DROP,REJECT,RETURN,NOPor a custom target. Defaults toACCEPT.
The 'connection_tracking' type
This type is used to enable connection tracking using the iptables conntrack or iptables state extension. The following type-specific YAML keys are supported:
tracking_active_targetOptional. iptables jump target for valid connections. Defaults to
ACCEPT.tracking_invalid_targetOptional. iptables jump target for invalid connections. Defaults to
DROP.tracking_moduleOptional. iptables module used for connection tracking. Possible values:
stateorconntrack. Defaults toconntrack.interfaceOptional. List of network interfaces for incoming packets to which the rule is applied.
outerfaceOptional. List of network interfaces for outgoing packets to which the rule is applied.
interface_notOptional. List of network interfaces for incoming packets which are excluded from the rule.
outerface_notOptional. List of network interfaces for outgoing packets which are excluded from the rule.
The 'custom' type
The type used to define custom ferm rules. The following additional YAML keys are supported:
rulesferm rule definition, required.
by_roleOptional. Add comment to generated ferm rule definition file that rule is defined in the given Ansible role.
This template is used among others in a debops.libvirtd custom ferm rule.
The 'default_policy' type
This type is used to define iptables default policies. The following type-specific YAML keys are supported:
policyiptables chain policy, required.
The 'dmz' type
This type can be used to enable connection forwarding to another host. If
item.port is not specified, all traffic is forwarded. The following
type-specific YAML keys are supported:
multiportOptional. Use iptables multiport extension. Possible values:
TrueorFalse. Defaults toFalse.interfaceOptional. List of public network interfaces which accept connections. At least one of
interfaceorpublic_ipis required.public_ipOptional. IPv4 address on the public network which accepts connections. At least one of
interfaceorpublic_ipis required. Only 1 IP address should be used at a time.private_ipIPv4 address of the host on the internal network, required. Only 1 IP address should be used at a time.
protocol(s)Optional. List of protocols to forward. Defaults to
tcp.port(s)Optional. List of ports to forward.
dportOptional. Destination port to forward to. Only needs to be specified if internal destination port is different from the original destination port.
snat_ipOptional. IP address for source address translation. If set the source IP is rewritten to this address. This is typically the IP address of the firewall host on the internal network. This is useful if traffic from the
private_ipwould otherwise not be routed back to the firewall host.
The 'hashlimit' type
This type is used to define rate limit rules using the iptables hashlimit extension. The following type-specific YAML keys are supported:
daddrOptional. List of destination IP addresses or networks to which the rule is applied.
dportOptional. List of destination ports to which the rule is applied.
hashlimit_burstOptional. Number of packets to match within the expiration time. Defaults to
5.hashlimit_expireOptional. Expiration time of hash entries in seconds. Defaults to
1.8.hashlimit_targetOptional. Jump target used when packet matches the
hashlimitrule which means that the rate limit is not reached yet. Defaults toRETURN.hashlimit_modeOptional. Options to take into consideration when associating packet streams. Possible values:
srcip,srcport,dstip,dstportor a comma-separated list thereof. Defaults tosrcip.interfaceOptional. List of network interfaces for incoming packets to which the rule is applied.
interface_presentOptional. Same as
item.interfacebut first check if specified network interfaces exists before adding the firewall rules.outerfaceOptional. List of network interfaces for outgoing packets to which the rule is applied.
outerface_presentOptional. Same as
item.outerfacebut first check if specified network interface exists before adding the firewall rule.includeOptional. Custom ferm configuration file to include. See ferm include for more details.
logOptional. Write rate limit hits to syslog. Possible values:
TrueandFalse. Defaults toTrue.protocolOptional. Network protocol to which the rule is applied.
protocol_synOptional. Match TCP packet with only the SYN flag set. Possible values
TrueorFalse. If set toFalseit will match all other packets except the ones with only the SYN flag set. Defaults to unset.reject_withOptional. Define reject message being sent when the rule
item.targetis set toREJECT. Defaults toicmp-admin-prohibited.stateOptional. Connection state which should be matched. Possible values:
INVALID,ESTABLISHED,NEW,RELATED,UNTRACKEDor comma-separated combination thereof.subchainOptional. Subchain name. Move resulting iptables rules into a separate subchain with the given name. See ferm subchain for more details.
targetOptional. iptables jump target in case the rate limit is reached. Defaults to
REJECT.
The 'include' type
This type can be used to include custom ferm configuration files. The following type-specific YAML keys are supported:
includeOptional. Custom ferm configuration file to include. See ferm include for more details.
The 'log' type
This type can be used to specify logging rules using the iptables log extension. The following type-specific YAML keys are supported:
includeOptional. Custom ferm configuration file to include. See ferm include for more details.
log_burstOptional. Burst limit of packets being logged. Defaults to
ferm__log_burst.log_ip_optionsOptional. Log IP options of packet. Possible values:
TrueorFalse. Defaults toTrue.log_levelOptional. Log level for firewall messages. Possible values are:
emerg,alert,crit,error,warning,notice,infoordebug. Defaults towarning.log_limitOptional. Rate limit of packets being logged. Defaults to
ferm__log_limit.log_prefixOptional. Prefix (up to 29 characters) for firewall log messages. Defaults to iptables-log:
log_targetOptional. Select how iptables performs logging. Possible values:
LOG,ULOG,NFLOG. Defaults toLOG.log_tcp_optionsOptional. Log TCP options of packet. Possible values:
TrueorFalse. Defaults toFalse.log_tcp_sequenceOptional. Log TCP sequence of packet. Possible values:
TrueorFalse. Defaults toFalse.realgotoOptional. After packet match jump to custom chain. See ferm realgoto for more details.
reject_withOptional. Define reject message being sent when the rule
item.targetis set toREJECT. Defaults toicmp-admin-prohibited.targetOptional. iptables jump target for logged packets.
The 'recent' type
This type can be used to track connections and respond accordingly by using the iptables recent extension. The following type-specific YAML keys are supported:
dportOptional. List of destination ports to which the rule is applied.
includeOptional. Custom ferm configuration file to include. See ferm include for more details.
multiportOptional. Use iptables multiport extension. Possible values:
TrueorFalse. Defaults toFalse.protocolOptional. Network protocol to which the rule is applied.
protocol_synOptional. Match TCP packet with only the SYN flag set. Possible values
TrueorFalse. If set toFalseit will match all other packets except the ones with only the SYN flag set. Defaults to unset.recent_hitcountOptional. Must be used in combination with
item.recent_update. Match if address is in the list and at least the given number of packets were received so far.recent_logOptional. Log packets matching the rule. Possible values:
TrueorFalse. Defaults toferm__log. If this is set toTrueferm__logmust be enabled too for the packet to be logged.recent_nameOptional. Name of the list. Defaults to
DEFAULT.recent_removeOptional. Remove address from the list. Possible values:
TrueorFalse. Defaults toFalse. Mutually exclusive withitem.recent_update.recent_secondsOptional. Must be used in combination with
item.recent_update. Match if address is in the list and was last seen within the given number of seconds.recent_set_nameOptional. Add the source address of a matching packet to the given list. This must correspond with
item.recent_nameof a second rule which would potentially act on the packet, e. g. reject it.recent_targetOptional. iptables jump target when packet has hit the recent list. Possible values:
ACCEPT,DROP,REJECT,RETURN,NOPor a custom target. Defaults toNOP.recent_updateOptional. Update "last-seen" timestamp. Possible values:
TrueorFalse. Defaults toFalse. Mutually exclusive withitem.recent_remove.reject_withOptional. Define reject message being sent when the rule
item.targetis set toREJECT. Defaults toicmp-admin-prohibited.stateOptional. Connection state which should be matched. Possible values:
INVALID,ESTABLISHED,NEW,RELATED,UNTRACKEDor comma-separated combination thereof.subchainOptional. Subchain name. Move resulting iptables rules into a separate subchain with the name given. See ferm subchain for more details.
When using the recent type make sure to always define two
rules:
One for matching the packet against the address list using the
item.recent_updatefeature. If this filter matches you likely want to set theitem.recent_targettoDROPorREJECT.To clear the source address from the list again in case the connection restrictions are not met, add a second role using
item.recent_remove.
The 'reject' type
This type is used to reject all traffic. It can be added for example as a final rule in a custom chain.
Legacy rules
Legacy rules are the (old) deprecated way to configure firewall rules using a simpler less flexible syntax than described above. As support for these is likely going to be removed in the future, they shouldn't be used anymore.
Support for legacy rules is still enabled by default. However, they are
stored in a separate iptables INPUT chain called
debops-legacy-input-rules. In case you haven't defined any legacy
rules and none of the DebOps roles you are using are still depending
on it, disable support completely by setting ferm__include_legacy
to False which will avoid the additional chain from being created.
If you're not sure if you still have legacy rules defined, look for
variable names with only one '_' after the ferm prefix (e. g.
ferm_input_list and ferm_input_dependent_list).