/etc/shorewall/rules
Entries in this file govern connection establishment by defining exceptions to the policies laid out in shorewall-policy[1](5). By default, subsequent requests and responses are automatically allowed using connection tracking. For any particular (source,dest) pair of zones, the rules are evaluated in the order in which they appear in this file and the first terminating match is the one that determines the disposition of the request. All rules are terminating except LOG and COUNT rules.
If you masquerade or use SNAT from a local system to the internet, you cannot use an ACCEPT rule to allow traffic from the internet to that system. You must use a DNAT rule instead.
The rules file is divided into sections. Each section is introduced by a "Section Header" which is a line beginning with SECTION and followed by the section name.
Sections are as follows and must appear in the order listed:
ALL
ESTABLISHED
The only ACTIONs allowed in this section are ACCEPT, DROP, REJECT, LOG and QUEUE
There is an implicit ACCEPT rule inserted at the end of this section.
RELATED
The only ACTIONs allowed in this section are ACCEPT, DROP, REJECT, LOG and QUEUE
There is an implicit rule added at the end of this section that invokes the RELATED_DISPOSITION (shorewall.conf[2](5)).
INVALID
The only Actions allowed in this section are ACCEPT, DROP, REJECT, LOG and QUEUE.
There is an implicit rule added at the end of this section that invokes the INVALID_DISPOSITION (shorewall.conf[2](5)).
UNTRACKED
The only Actions allowed in this section are ACCEPT, DROP, REJECT, LOG and QUEUE.
There is an implicit rule added at the end of this section that invokes the UNTRACKED_DISPOSITION (shorewall.conf[2](5)).
NEW
If you are not familiar with Netfilter to the point where you are comfortable with the differences between the various connection tracking states, then it is suggested that you omit the ESTABLISHED and RELATED sections and place all of your non-blacklisting rules in the NEW section (That's after the line that reads SECTION NEW').
If you specify FASTACCEPT=Yes in shorewall.conf[2](5) then the ALL, ESTABLISHED and RELATED sections must be empty.
An except is made if you are running Shorewall 4.4.27 or later and you have specified a non-default value for RELATED_DISPOSITION or RELATED_LOG_LEVEL. In that case, you may have rules in the RELATED section of this file.
You may omit any section that you don't need. If no Section Headers appear in the file then all rules are assumed to be in the NEW section.
When defining rules that rewrite the destination IP address and/or port number (namely DNAT and REDIRECT rules), it is important to keep straight which columns in the file specify the packet before rewriting and which specify how the packet will look after rewriting.
The columns in the file are as follows (where the column name is followed by a different name in parentheses, the different name is used in the alternate specification syntax).
ACTION - target[:{log-level|none}[!][:tag]]
ACCEPT
ACCEPT+
ACCEPT!
action
ADD(ipset:flags)
ADD is non-terminating. Even if a packet matches the rule, it is passed on to the next rule.
AUDIT[(accept|drop|reject)]
A_ACCEPT, A_ACCEPT+ and A_ACCEPT!
A_DROP and A_DROP!
A_REJECT AND A_REJECT!
[?]COMMENT
CONTINUE
Do not process any of the following rules for this (source zone,destination zone). If the source and/or destination IP address falls into a zone defined later in shorewall-zones[4](5) or in a parent zone of the source or destination zones, then this connection request will be passed to the rules defined for that (those) zone(s). See shorewall-nesting[5](5) for additional information.
CONTINUE!
COUNT
DEL(ipset:flags)
DEL is non-terminating. Even if a packet matches the rule, it is passed on to the next rule.
DNAT
DNAT-
Like DNAT but only generates the DNAT iptables rule and not the companion ACCEPT rule.
DROP
DROP!
HELPER
INLINE[(action)]
Some considerations when using INLINE:
LOG:level
macro[(macrotarget)]
Example: FTP(ACCEPT).
The older syntax where the macro name and the target are separated by a slash (e.g. FTP/ACCEPT) is still allowed but is deprecated.
NFLOG[(nflog-parameters)]
Similar to LOG:NFLOG[(nflog-parameters)], except that the log level is not changed when this ACTION is used in an action or macro body and the invocation of that action or macro specifies a log level.
NFQUEUE[(queuenumber)]
NFQUEUE![(queuenumber)]
NONAT
QUEUE
QUEUE!
REJECT
REJECT!
REDIRECT
REDIRECT-
Like REDIRECT but only generates the REDIRECT iptables rule and not the companion ACCEPT rule.
ULOG[(ulog-parameters)]
Similar to LOG:ULOG[(ulog-parameters)], except that the log level is not changed when this ACTION is used in an action or macro body and the invocation of that action or macro specifies a log level.
The target may optionally be followed by ":" and a syslog log level (e.g, REJECT:info or Web(ACCEPT):debug). This causes the packet to be logged at the specified level. Note that if the ACTION involves destination network address translation (DNAT, REDIRECT, etc.) then the packet is logged before the destination address is rewritten.
If the ACTION names an action declared in shorewall-actions[3](5) or in /usr/share/shorewall/actions.std then:
You may also specify ULOG or NFLOG (must be in upper case) as a log level.This will log to the ULOG or NFLOG target for routing to a separate log through use of ulogd (http://www.netfilter.org/projects/ulogd/index.html).
Actions specifying logging may be followed by a log tag (a string of alphanumeric characters) which is appended to the string generated by the LOGPREFIX (in shorewall.conf[2](5)).
Example: ACCEPT:info:ftp would include 'ftp ' at the end of the log prefix generated by the LOGPREFIX setting.
SOURCE - {zone|zone-list[+]|{all|any}[+][-]}[:interface][:{address-or-range[,address-or-range]...[exclusion]|exclusion|+ipset|^countrycode-list}
Beginning with Shorewall 4.4.13, you may use a zone-list which consists of a comma-separated list of zones declared in shorewall-zones[4] (5). This zone-list may be optionally followed by "+" to indicate that the rule is to apply to intra-zone traffic as well as inter-zone traffic.
When none is used either in the SOURCE or DEST column, the rule is ignored.
all means "All Zones", including the firewall itself. all- means "All Zones, except the firewall itself". When all[-] is used either in the SOURCE or DEST column intra-zone traffic is not affected. When all+[-] is "used, intra-zone traffic is affected. Beginning with Shorewall 4.4.13, exclusion is supported -- see see shorewall-exclusion[7](5).
Except when all[+][-] or any[+][-] is specified, clients may be further restricted to a list of networks and/or hosts by appending ":" and a comma-separated list of network and/or host addresses. Hosts may be specified by IP or MAC address; mac addresses must begin with "~" and must use "-" as a separator.
The above restriction on all[+][-] and any[+][-] is removed in Shorewall-4.4.13.
any is equivalent to all when there are no nested zones. When there are nested zones, any only refers to top-level zones (those with no parent zones). Note that any excludes all vserver zones, since those zones are nested within the firewall zone.
Hosts may also be specified as an IP address range using the syntax lowaddress-highaddress. This requires that your kernel and iptables contain iprange match support. If your kernel and iptables have ipset match support then you may give the name of an ipset prefaced by "+". The ipset name may be optionally followed by a number from 1 to 6 enclosed in square brackets ([]) to indicate the number of levels of source bindings to be matched.
Beginning with Shorewall 4.4.17, the primary IP address of a firewall interface can be specified by an ampersand ('&') followed by the logical name of the interface as found in the INTERFACE column of shorewall-interfaces[8] (5).
Beginning with Shorewall 4.5.4, A countrycode-list may be specified. A countrycode-list is a comma-separated list of up to 15 two-character ISO-3661 country codes enclosed in square brackets ('[...]') and preceded by a caret ('^'). When a single country code is given, the square brackets may be omitted. A list of country codes supported by Shorewall may be found at http://www.shorewall.net/ISO-3661.html. Specifying a countrycode-list requires GeoIP Match support in your iptables and Kernel.
You may exclude certain hosts from the set already defined through use of an exclusion (see shorewall-exclusion[7](5)).
Examples:
dmz:192.168.2.2
net:155.186.235.0/24
loc:192.168.1.1,192.168.1.2
loc:~00-A0-C9-15-39-78
net:192.0.2.11-192.0.2.17
net:!192.0.2.11-192.0.2.17
net:155.186.235.0/24!155.186.235.16/28
$FW:ð0
DEST - {zone|zone-list[+]|{all|any}[+][-]}[:{interface|address-or-range[,address-or-range]...[exclusion]|exclusion|+ipset|^countrycode-list}][:port[:random]]
Beginning with Shorewall 4.4.13, you may use a zone-list which consists of a comma-separated list of zones declared in shorewall-zones[4] (5). This zone-list may be optionally followed by "+" to indicate that the rule is to apply to intra-zone traffic as well as inter-zone traffic.
Beginning with Shorewall 4.5.4, A countrycode-list may be specified. A countrycode-list is a comma-separated list of up to 15 two-character ISO-3661 country codes enclosed in square brackets ('[...]') and preceded by a caret ('^'). When a single country code is given, the square brackets may be omitted. A list of country codes supported by Shorewall may be found at http://www.shorewall.net/ISO-3661.html. Specifying a countrycode-list requires GeoIP Match support in your iptables and Kernel.
When none is used either in the SOURCE or DEST column, the rule is ignored.
When all is used either in the SOURCE or DEST column intra-zone traffic is not affected. When all+ is used, intra-zone traffic is affected. Beginning with Shorewall 4.4.13, exclusion is supported -- see see shorewall-exclusion[7](5).
any is equivalent to all when there are no nested zones. When there are nested zones, any only refers to top-level zones (those with no parent zones).
The zone should be omitted in DNAT-, REDIRECT- and NONAT rules.
If the DEST zone is a bport zone, then either:
Except when all[+]|[-] is specified, the server may be further restricted to a particular network, host or interface by appending ":" and the network, host or interface. See SOURCE above.
You may exclude certain hosts from the set already defined through use of an exclusion (see shorewall-exclusion[7](5)).
Restriction: MAC addresses are not allowed (this is a Netfilter restriction).
Like in the SOURCE column, you may specify a range of IP addresses using the syntax lowaddress-highaddress. When the ACTION is DNAT or DNAT-, the connections will be assigned to addresses in the range in a round-robin fashion.
If you kernel and iptables have ipset match support then you may give the name of an ipset prefaced by "+". The ipset name may be optionally followed by a number from 1 to 6 enclosed in square brackets ([]) to indicate the number of levels of destination bindings to be matched. Only one of the SOURCE and DEST columns may specify an ipset name.
Beginning with Shorewall 4.4.17, the primary IP address of a firewall interface can be specified by an ampersand ('&') followed by the logical name of the interface as found in the INTERFACE column of shorewall-interfaces[8] (5).
The port that the server is listening on may be included and separated from the server's IP address by ":". If omitted, the firewall will not modify the destination port. A destination port may only be included if the ACTION is DNAT or REDIRECT.
Example:
The port may be specified as a service name. You may specify a port range in the form lowport-highport to cause connections to be assigned to ports in the range in round-robin fashion. When a port range is specified, lowport and highport must be given as integers; service names are not permitted. Additionally, the port range may be optionally followed by :random which causes assignment to ports in the list to be random.
If the ACTION is REDIRECT or REDIRECT-, this column needs only to contain the port number on the firewall that the request should be redirected to. That is equivalent to specifying $FW::port.
PROTO- {-|tcp:syn|ipp2p|ipp2p:udp|ipp2p:all|protocol-number|protocol-name|all}
Beginning with Shorewall 4.4.19, this column can contain a comma-separated list of protocol-numbers and/or protocol names.
DEST PORT(S) (dport) - {-|port-name-number-or-range[,port-name-number-or-range]...}
If the protocol is ipp2p, this column is interpreted as an ipp2p option without the leading "--" (example bit for bit-torrent). If no port is given, ipp2p is assumed.
A port range is expressed as lowport:highport.
This column is ignored if PROTO = all but must be entered if any of the following columns are supplied. In that case, it is suggested that this field contain a dash (-).
If your kernel contains multi-port match support, then only a single Netfilter rule will be generated if in this list and the CLIENT PORT(S) list below:
1. There are 15 or less ports listed.
2. No port ranges are included or your kernel and iptables contain extended multi-port match support.
SOURCE PORT(S) (sport) - {-|port-name-number-or-range[,port-name-number-or-range]...}
Beginning with Shorewall 4.5.15, you may place '=' in this column, provided that the DEST PORT(S) column is non-empty. This causes the rule to match when either the source port or the destination port in a packet matches one of the ports specified in DEST PORTS(S). Use of '=' requires multi-port match in your iptables and kernel.
If your kernel contains multi-port match support, then only a single Netfilter rule will be generated if in this list and the DEST PORT(S) list above:
1. There are 15 or less ports listed.
2. No port ranges are included or your kernel and iptables contain extended multi-port match support.
ORIGINAL DEST (origdest) - [-|address[,address]...[exclusion]|exclusion]
A comma-separated list of addresses may also be used. This is most useful with the REDIRECT target where you want to redirect traffic destined for particular set of hosts. Finally, if the list of addresses begins with "!" (exclusion) then the rule will be followed only if the original destination address in the connection request does not match any of the addresses listed.
Beginning with Shorewall 4.4.17, the primary IP address of a firewall interface can be specified by an ampersand ('&') followed by the logical name of the interface as found in the INTERFACE column of shorewall-interfaces[8] (5).
For other actions, this column may be included and may contain one or more addresses (host or network) separated by commas. Address ranges are not allowed. When this column is supplied, rules are generated that require that the original destination address matches one of the listed addresses. This feature is most useful when you want to generate a filter rule that corresponds to a DNAT- or REDIRECT- rule. In this usage, the list of addresses should not begin with "!".
It is also possible to specify a set of addresses then exclude part of those addresses. For example, 192.168.1.0/24!192.168.1.16/28 specifies the addresses 192.168.1.0-182.168.1.15 and 192.168.1.32-192.168.1.255. See shorewall-exclusion[7](5).
See http://shorewall.net/PortKnocking.html[9] for an example of using an entry in this column with a user-defined action rule.
RATE LIMIT (rate) - [-|[{s|d}:[[name]:]]]rate/{sec|min|hour|day}[:burst]
rate is the number of connections per interval (sec or min) and burst is the largest burst permitted. If no burst is given, a value of 5 is assumed. There may be no no white-space embedded in the specification.
Example: 10/sec:20
When s: or d: is specified, the rate applies per source IP address or per destination IP address respectively. The name may be chosen by the user and specifies a hash table to be used to count matching connections. If not given, the name shorewallN (where N is a unique integer) is assumed. Where more than one rule specifies the same name, the connections counts for the rules are aggregated and the individual rates apply to the aggregated count.
Example: s:ssh:3/min:5
USER/GROUP (user) - [!][user-name-or-number][:group-name-or-number][,...]
When this column is non-empty, the rule applies only if the program generating the output is running under the effective user and/or group specified (or is NOT running under that id if "!" is given).
Beginning with Shorewall 4.5.8, multiple user or group names/ids separated by commas may be specified.
Examples:
joe
:kids
!:kids
2001-2099
MARK - [!]value[/mask][:C]
If you don't want to define a test but need to specify anything in the following columns, place a "-" in this field.
!
value
mask
:C
CONNLIMIT - [!]limit[:mask]
TIME - timeelement[&timeelement...]
timeelement may be:
timestart=hh:mm[:ss]
timestop=hh:mm[:ss]
utc
localtz
kerneltz
weekdays=ddd[,ddd]...
monthdays=dd[,dd],...
datestart=yyyy[-mm[-dd[Thh[:mm[:ss]]]]]
datestop=yyyy[-mm[-dd[Thh[:mm[:ss]]]]]
HEADERS
SWITCH - [!]switch-name[={0|1}]
The rule is enabled if the value stored in /proc/net/nf_condition/switch-name is 1. The rule is disabled if that file contains 0 (the default). If '!' is supplied, the test is inverted such that the rule is enabled if the file contains 0.
Within the switch-name, '@0' and '@{0}' are replaced by the name of the chain to which the rule is a added. The switch-name (after '@...' expansion) must begin with a letter and be composed of letters, decimal digits, underscores or hyphens. Switch names must be 30 characters or less in length.
Switches are normally off. To turn a switch on:
Beginning with Shorewall 4.5.10, when the switch-name is followed by =0 or =1, then the switch is initialized to off or on respectively by the start command. Other commands do not affect the switch setting.
HELPER - [helper]
In the NEW section, causes the named conntrack helper to be associated with this connection; the contents of this column are ignored unless ACTION is ACCEPT*, DNAT* or REDIRECT*.
In the RELATED section, will only match if the related connection has the named helper associated with it.
The helper may be one of:
Example 1:
#ACTION SOURCE DEST PROTO DEST SOURCE ORIGINAL
# PORT PORT(S) DEST
ACCEPT dmz net tcp smtp
Example 2:
#ACTION SOURCE DEST PROTO DEST SOURCE ORIGINAL
# PORT PORT(S) DEST
DNAT net loc:192.168.1.3 tcp ssh,http
Example 3:
#ACTION SOURCE DEST PROTO DEST SOURCE ORIGINAL RATE
# PORT PORT(S) DEST LIMIT
DNAT net loc:192.168.1.3 tcp http - - 3/sec:10
Example 4:
#ACTION SOURCE DEST PROTO DEST SOURCE ORIGINAL
# PORT PORT(S) DEST
REDIRECT loc 3128 tcp www - !192.168.2.2
Example 5:
#ACTION SOURCE DEST PROTO DEST SOURCE ORIGINAL
# PORT PORT(S) DEST
DNAT net loc:192.168.1.3 tcp 80 - 130.252.100.69
Example 6:
#ACTION SOURCE DEST PROTO DEST SOURCE ORIGINAL
# PORT PORT(S) DEST
ACCEPT net:130.252.100.69,130.252.100.70 $FW \
tcp 22
Example 7:
#ACTION SOURCE DEST PROTO DEST SOURCE ORIGINAL
# PORT PORT(S) DEST
DNAT net loc:192.168.1.3:22 tcp 2222
Example 8:
#ACTION SOURCE DEST PROTO DEST SOURCE ORIGINAL
# PORT PORT(S) DEST
REDIRECT net $FW::81-90:random tcp www
Example 9:
shorewall-zones[4](8):
#ZONE TYPE OPTIONS
fw firewall
net ipv4
dmz ipv4
loc ipv4
shorewall-interfaces[8](8):
#ZONE INTERFACE BROADCAST OPTIONS
net ppp0
loc eth1 detect
dmz eth2 detect
- ppp+ # Addresses are assigned from 192.168.3.0/24
shorewall-host[10](8):
#ZONE HOST(S) OPTIONS
loc ppp+:192.168.3.0/24
rules:
#ACTION SOURCE DEST PROTO DEST
# PORT(S)
REDIRECT loc 3128 tcp 80
Note that it would have been tempting to simply define the loc zone entirely in shorewall-interfaces(8):
#******************* INCORRECT *****************
#ZONE INTERFACE BROADCAST OPTIONS
net ppp0
loc eth1 detect
loc ppp+
dmz eth2
This would have made it impossible to run a internet-accessible web server in the DMZ because all traffic entering ppp+ interfaces would have been redirected to port 3128 on the firewall and there would have been no net->fw ACCEPT rule for that traffic.
Example 10:
#ACTION SOURCE DEST PROTO DEST
# PORT(S)
ADD(+S:dst,src,dst) net fw tcp 22
Example 11:
#ACTION SOURCE DEST PROTO DEST SOURCE ORIGINAL RATE
# PORT(S) PORT(S) DEST LIMIT
SSH(ACCEPT) net all - - - - s:1/min:3
Example 12:
#ACTION SOURCE DEST PROTO DEST SOURCE ORIGINAL RATE USER/ MARK CONNLIMIT TIME HEADERS SWITCH
# PORT(S) PORT(S) DEST LIMIT GROUP
DNAT net dmz:$BACKUP tcp 80 - - - - - - - - primary_down
Example 13:
#ACTION SOURCE DEST PROTO DEST
# PORT(S)
DROP net:^A1,A2 fw tcp 25
Example 14:
#ACTION SOURCE DEST PROTO DEST
# PORT(S)
INLINE $FW net ; -p 6 -m mickey-mouse --name test -m set --match-set set1 src -m mickey-mouse --name test2 -j SECCTX --name test3
The above will generate the following iptables-restore input:
-A fw2net -p 6 -m mickey-mouse --name test -m set --match-set set1 src -m mickey-mouse --name test2 -j SECCTX --name test3
Note that SECCTX must be defined as a builtin action in shorewall-actions[3](5):
#ACTION OPTIONS
SECCTX builtin
/etc/shorewall/rules
http://www.shorewall.net/ipsets.html
http://www.shorewall.net/configuration_file_basics.htm#Pairs[11]
http://www.shorewall.net/shorewall_logging.html
shorewall(8), shorewall-accounting(5), shorewall-actions(5), shorewall-blacklist(5), shorewall-blrules(5), shorewall-hosts(5), shorewall_interfaces(5), shorewall-ipsets(5), shorewall-maclist(5), shorewall-masq(5), shorewall-nat(5), shorewall-netmap(5), shorewall-params(5), shorewall-policy(5), shorewall-providers(5), shorewall-proxyarp(5), shorewall-rtrules(5), shorewall-routestopped(5), shorewall.conf(5), shorewall-secmarks(5), shorewall-tcclasses(5), shorewall-tcdevices(5), shorewall-tcrules(5), shorewall-tos(5), shorewall-tunnels(5), shorewall-zones(5)