SHOREWALL-MANGLE(5) | Configuration Files | SHOREWALL-MANGLE(5) |
mangle - Shorewall Packet marking/mangling rules file
/etc/shorewall[6]/mangle
This file was introduced in Shorewall 4.6.0 and replaces shorewall-tcrules(5)[1]. This file is only processed by the compiler if:
Entries in this file cause packets to be marked as a means of classifying them for traffic control or policy routing.
Unlike rules in the shorewall-rules[2](5) file, evaluation of rules in this file will continue after a match. So the final mark for each packet will be the one assigned by the LAST tcrule that matches.
If you use multiple internet providers with the 'track' option, in /etc/shorewall/providers be sure to read the restrictions at https://shorewall.org/MultiISP.html[3].
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 - command[(parameters)][:chain-designator]
P
F
T
I
NP
NI
NO
NT
The nat table designators were added in Shorewall 5.2.1. When a nat table designator is given, only the CONNMARK, MARK, SAVE and RESTORE commands may be used.
Unless otherwise specified for the particular command, the default chain is PREROUTING when MARK_IN_FORWARD_CHAIN=No in shorewall.conf(5)[4], and FORWARD when MARK_IN_FORWARD_CHAIN=Yes.
A chain-designator may not be specified if the SOURCE or DEST columns begin with '$FW'. When the SOURCE is $FW, the generated rule is always placed in the OUTPUT chain. If DEST is '$FW', then the rule is placed in the INPUT chain. Additionally, a chain-designator may not be specified in an action body.
Where a command takes parameters, those parameters are enclosed in parentheses ("(....)") and separated by commas.
The command may be one of the following.
action[([param[,...])]
ADD(ipset:flags)
ADD is non-terminating. Even if a packet matches the rule, it is passed on to the next rule.
CHECKSUM
Requires 'Checksum Target' support in your kernel and iptables.
CLASSIFY(classid)
atm
cbq
dsmark
pfifo_fast
htb
prio
Classification occurs in the POSTROUTING chain except when the SOURCE is $FW[:address] in which case classification occurs in the OUTPUT chain.
When using Shorewall's built-in traffic shaping tool, the major class is the device number (the first device in shorewall-tcdevices[6](5) is major class 1, the second device is major class 2, and so on) and the minor class is the class's MARK value in shorewall-tcclasses[7](5) preceded by the number 1 (MARK 1 corresponds to minor class 11, MARK 5 corresponds to minor class 15, MARK 22 corresponds to minor class 122, etc.).
?COMMENT
To stop the comment from being attached to further rules, simply include ?COMMENT on a line by itself.
CONMARK({mark|range})
CONTINUE
Currently, CONTINUE may not be used with exclusion (see the SOURCE and DEST columns below); that restriction will be removed when iptables/Netfilter provides the necessary support.
DEL(ipset:flags)
DEL is non-terminating. Even if a packet matches the rule, it is passed on to the next rule.
DIVERT
DIVERTHA
#NAME NUMBER MARK DUPLICATE INTERFACE GATEWAY OPTIONS COPY TProxy 1 - - lo - tproxy
and use this DIVERTHA entry:
#ACTION SOURCE DEST PROTO DPORT SPORT USER TEST LENGTH TOS CONNBYTES HELPER PROBABILITY DSCP DIVERTHA - - tcp
DROP
DSCP(dscp)
CS0 => 0x00
CS1 => 0x08
CS2 => 0x10
CS3 => 0x18
CS4 => 0x20
CS5 => 0x28
CS6 => 0x30
CS7 => 0x38
BE => 0x00
AF11 => 0x0a
AF12 => 0x0c
AF13 => 0x0e
AF21 => 0x12
AF22 => 0x14
AF23 => 0x16
AF31 => 0x1a
AF32 => 0x1c
AF33 => 0x1e
AF41 => 0x22
AF42 => 0x24
AF43 => 0x26
EF => 0x2e
To indicate more than one class, add their hex values together and specify the result. By default, DSCP rules are placed in the POSTROUTING chain.
ECN
IMQ(number)
INLINE[(action)]
The following rules are equivalent:
IPMARK
In a sense, the IPMARK target is more like an IPCLASSIFY target in that the mark value is later interpreted as a class ID. A packet mark is 32 bits wide; so is a class ID. The <major> class occupies the high-order 16 bits and the <minor> class occupies the low-order 16 bits. So the class ID 1:4ff (remember that class IDs are always in hex) is equivalent to a mark value of 0x104ff. Remember that Shorewall uses the interface number as the <major> number where the first interface in tcdevices has <major> number 1, the second has <major> number 2, and so on.
The IPMARK target assigns a mark to each matching packet based on the either the source or destination IP address. By default, it assigns a mark value equal to the low-order 8 bits of the source address. The syntax is as follows: IPMARK[([{src|dst}][,[mask1][,[mask2][,[shift]]]])] Default values are:
Example: IPMARK(src,0xff,0x10100)
For example, suppose that your internal network is 192.168.1.0/29 (host IP addresses 192.168.1.1 - 192.168.1.6). Your first notion might be to use IPMARK(src,0xFF,0x10000) so as to produce class IDs 1:1 through 1:6. But 1:1 is an invalid class ID since the major and minor classes are equal. So you might choose instead to use IPMARK(src,0xFF,0x10100) as in the example above so that all of your minor classes will have a value > 256.
IP6TABLES({target [option ...])
This action allows you to specify an iptables target with options (e.g., 'IP6TABLES(MARK --set-xmark 0x01/0xff)'. If the target is not one recognized by Shorewall, the following error message will be issued:
IPTABLES({target [option ...])
This action allows you to specify an iptables target with options (e.g., 'IPTABLES(MARK --set-xmark 0x01/0xff)'. If the target is not one recognized by Shorewall, the following error message will be issued:
MARK({mark|range})
Normally will set the mark value. If preceded by a vertical bar ("|"), the mark value will be logically ORed with the current mark value to produce a new mark value. If preceded by an ampersand ("&"), will be logically ANDed with the current mark value to produce a new mark value.
Both "|" and "&" require Extended MARK Target support in your kernel and iptables.
The mark value may be optionally followed by "/" and a mask value (used to determine those bits of the connection mark to actually be set). When a mask is specified, the result of logically ANDing the mark value with the mask must be the same as the mark value.
A mark range is a pair of integers separated by a dash ("-").
May be optionally followed by a slash ("/") and a mask and requires the Statistics Match capability in iptables and kernel. Marks in the specified range are assigned to packets on a round-robin fashion.
When a mask is specified, the result of logically ANDing each mark value with the mask must be the same as the mark value. The least significant bit in the mask is used as an increment. For example, if '0x200-0x400/0xff00' is specified, then the assigned mark values are 0x200, 0x300 and 0x400 in equal proportions. If no mask is specified, then ( 2 ** MASK_BITS ) - 1 is assumed (MASK_BITS is set in shorewall.conf[4](5)).
NFLOG[(nflog-parameters)]
RESTORE[(mask)]
SAME[(timeout)]
#ACTION SOURCE DEST PROTO DPORT SAME:P 192.168.1.0/24 0.0.0.0/0 tcp 80,443
If a host in 192.168.1.0/24 attempts a connection on TCP port 80 or 443 and it has sent a packet on either of those ports in the last five minutes then the new connection will use the same provider as the connection over which that last packet was sent.
When used in the OUTPUT chain, it causes all matching connections to an individual remote system to all use the same provider. For example:
#ACTION SOURCE DEST PROTO DPORT SAME $FW 0.0.0.0/0 tcp 80,443
The optional timeout parameter was added in Shorewall 4.6.7 and specifies a number of seconds . When not specified, a value of 300 seconds (5 minutes) is assumed. If the firewall attempts a connection on TCP port 80 or 443 and it has sent a packet on either of those ports in the last timeout seconds to the same remote system then the new connection will use the same provider as the connection over which that last packet was sent.
SAVE[(mask)]
TCPMSS([mss[,ipsec]])
The mss parameter may be either pmtu or an integer in the range 500:65533. The value pmtu automatically clamps the MSS value to (path_MTU - 40 for IPv4; -60 for IPv6). This may not function as desired where asymmetric routes with differing path MTU exist — the kernel uses the path MTU which it would use to send packets from itself to the source and destination IP addresses. Prior to Linux 2.6.25, only the path MTU to the destination IP address was considered by this option; subsequent kernels also consider the path MTU to the source IP address. If an integer is given, the MSS option is set to the specified value. If the MSS of the packet is already lower than mss, it will not be increased (from Linux 2.6.25 onwards) to avoid more problems with hosts relying on a proper MSS. If mss is omitted, pmtu is assumed.
The ipsec parameter determines whether the rule applies to IPSEC traffic (ipsec is passed), non-IPSEC traffic (none is passed) or both (all is passed). If omitted, all is assumed.
TOS(tos[/mask])
Minimize-Delay => 0x10, Maximize-Throughput => 0x08, Maximize-Reliability => 0x04, Minimize-Cost => 0x02, Normal-Service => 0x00
To indicate more than one class, add their hex values together and specify the result.
When tos is given as a number, it may be optionally followed by '/' and a mask. When no mask is given, the value 0xff is assumed. When tos is given as a type name, the mask 0x3f is assumed.
The action performed is to zero out the bits specified by the mask, then set the bits specified by tos.
TPROXY([port[,address]])
There are three parameters to TPROXY - neither is required:
TTL([-|+]number)
SOURCE - {-|source-spec[,...]}
[!]interface
Beginning with Shorweall 5.2.1, the interface may be preceded with '!' which matches all interfaces except the one specified.
address[,...][exclusion]
The name of an ipset preceded by a plus sign ("+").
A MAC address in Shorewall format (preceded by a tilde ("~") and using dash ("-") as a separator (e.g., ~00-A0-C9-15-39-78). Matches traffic whose source IP address matches one of the listed addresses and that does not match an address listed in the exclusion (see shorewall-exclusion[12](5)).
This form will not match traffic that originates on the firewall itself unless either <major><minor> or the :T chain qualifier is used in the ACTION column.
[!]interface:address,[...][exclusion]
Beginning with Shorweall 5.2.1, the interface may be preceded with '!' which matches all interfaces except the one specified.
[!]interface:exclusion
Beginning with Shorweall 5.2.1, the interface may be preceded with '!' which matches all interfaces except the one specified.
$FW
$FW:address[,...][exclusion]
$FW:exclusion
Beginning with Shorewall 5.1.0, multiple source_specs, separated by commas, may be given provided that the following alternative forms are used: (address[,...][exclusion])
interface:(address[,...][exclusion])
interface:(exclusion)
$FW:(address[,...][exclusion])
$FW:(exclusion)
DEST - {-|dest-spec[,...]}
interface
address[,...][exclusion]
The name of an ipset preceded by a plus sign ("+").
A MAC address in Shorewall format (preceded by a tilde ("~") and using dash ("-") as a separator (e.g., ~00-A0-C9-15-39-78). Matches traffic whose destination IP address matches one of the listed addresses and that does not match an address listed in the exclusion (see shorewall-exclusion[12](5)).
interface:address,[...][exclusion]
interface:exclusion
$FW
$FW:address[,...][exclusion]
$FW:exclusion
Beginning with Shorewall 5.1.0, multiple dest_specs, separated by commas, may be given provided that the following alternative forms are used: (address[,...][exclusion])
interface:(address[,...][exclusion])
interface:(exclusion)
$FW:(address[,...][exclusion])
$FW:(exclusion)
PROTO - {-|{tcp:[!]syn|ipp2p|ipp2p:udp|ipp2p:all|protocol-number|protocol-name|all}[,...]}
Beginning with Shorewall 4.5.12, this column can accept a comma-separated list of protocols.
DPORT- {-|port-name-number-or-range[,port-name-number-or-range]...|+ipset}
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.
An entry in this field requires that the PROTO column specify icmp (1), tcp (6), udp (17), sctp (132) or udplite (136). Use '-' if any of the following field is supplied.
Beginning with Shorewall 4.6.0, an ipset name can be specified in this column. This is intended to be used with bitmap:port ipsets.
This column was formerly named DEST PORT(S).
SPORT - {-|port-name-number-or-range[,port-name-number-or-range]...|+ipset}
An entry in this field requires that the PROTO column specify tcp (6), udp (17), sctp (132) or udplite (136). Use '-' if any of the following fields is supplied.
Beginning with Shorewall 4.5.15, you may place '=' in this column, provided that the DPORT 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.
Beginning with Shorewall 4.6.0, an ipset name can be specified in this column. This is intended to be used with bitmap:port ipsets.
This column was formerly labelled SOURCE PORT(S).
USER - [!][user-name-or-number][:group-name-or-number][+program-name]
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).
Examples:
joe
:kids
!:kids
+upnpd
TEST - [!]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
LENGTH - [length|[min]:[max]]
TOS - tos
Minimize-Delay (16)
Maximize-Throughput (8)
Maximize-Reliability (4)
Minimize-Cost (2)
Normal-Service (0)
CONNBYTES - [!]min:[max[:{O|R|B}[:{B|P|A}]]]
A packet matches if the the packet/byte count is within the range defined by min and max (unless ! is given in which case, a packet matches if the packet/byte count is not within the range). min is an integer which defines the beginning of the byte/packet range. max is an integer which defines the end of the byte/packet range; if omitted, only the beginning of the range is checked. The first letter gives the direction which the range refers to:O - The original direction of the connection. .sp - The opposite direction from the original connection. .sp B - The total of both directions.
If omitted, B is assumed.
The second letter determines what the range refers to.B - Bytes .sp P - Packets .sp A - Average packet size.If omitted, B is assumed.
HELPER - helper
Example: Mark all FTP data connections with mark 4:
#ACTION SOURCE DEST PROTO DPORT SPORT USER TEST LENGTH TOS CONNBYTES HELPER 4:T 0.0.0.0/0 0.0.0.0/0 TCP - - - - - - - ftp
PROBABILITY - [probability]
DSCP - [[!]dscp]
CS0 => 0x00
CS1 => 0x08
CS2 => 0x10
CS3 => 0x18
CS4 => 0x20
CS5 => 0x28
CS6 => 0x30
CS7 => 0x38
BE => 0x00
AF11 => 0x0a
AF12 => 0x0c
AF13 => 0x0e
AF21 => 0x12
AF22 => 0x14
AF23 => 0x16
AF31 => 0x1a
AF32 => 0x1c
AF33 => 0x1e
AF41 => 0x22
AF42 => 0x24
AF43 => 0x26
EF => 0x2e
STATE -- {NEW|RELATED|ESTABLISHED|INVALID} [,...]
TIME - timeelement[&timeelement...]
May be used to limit the rule to a particular time period each day, to particular days of the week or month, or to a range defined by dates and times. Requires time match support in your kernel and ip6tables.
timeelement may be:
timestart=hh:mm[:ss]
timestop=hh:mm[:ss]
contiguous
utc
localtz
kerneltz
weekdays=ddd[,ddd]...
monthdays=dd[,dd],...
datestart=yyyy[-mm[-dd[Thh[:mm[:ss]]]]]
datestop=yyyy[-mm[-dd[Thh[:mm[:ss]]]]]
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:
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.
IPv4 Example 1:
This is a little more complex than otherwise expected. Since the ipp2p module is unable to determine all packets in a connection are P2P packets, we mark the entire connection as P2P if any of the packets are determined to match.
We assume packet/connection mark 0 means unclassified.
#ACTION SOURCE DEST PROTO DPORT SPORT USER TEST
MARK(1):T 0.0.0.0/0 0.0.0.0/0 icmp echo-request
MARK(1):T 0.0.0.0/0 0.0.0.0/0 icmp echo-reply
RESTORE:T 0.0.0.0/0 0.0.0.0/0 all - - - 0
CONTINUE:T 0.0.0.0/0 0.0.0.0/0 all - - - !0
MARK(4):T 0.0.0.0/0 0.0.0.0/0 ipp2p:all
SAVE:T 0.0.0.0/0 0.0.0.0/0 all - - - !0
If a packet hasn't been classified (packet mark is 0), copy the connection mark to the packet mark. If the packet mark is set, we're done. If the packet is P2P, set the packet mark to 4. If the packet mark has been set, save it to the connection mark.
IPv4 Example 2:
/etc/shorewall/mangle:
#ACTION SOURCE DEST PROTO DPORT SPORT USER TEST
CONNMARK(1-3):F 192.168.1.0/24 eth0 ; state=NEW /etc/shorewall/snat:
#ACTION SOURCE DEST ...
SNAT(1.1.1.1) eth0:192.168.1.0/24 - { mark=1:C }
SNAT(1.1.1.3) eth0:192.168.1.0/24 - { mark=2:C }
SNAT(1.1.1.4) eth0:192.168.1.0/24 - { mark=3:C }
IPv6 Example 1:
This is a little more complex than otherwise expected. Since the ipp2p module is unable to determine all packets in a connection are P2P packets, we mark the entire connection as P2P if any of the packets are determined to match.
We assume packet/connection mark 0 means unclassified.
#ACTION SOURCE DEST PROTO DPORT SPORT USER TEST
MARK(1):T ::/0 ::/0 icmp echo-request
MARK(1):T ::/0 ::/0 icmp echo-reply
RESTORE:T ::/0 ::/0 all - - - 0
CONTINUE:T ::/0 ::/0 all - - - !0
MARK(4):T ::/0 ::/0 ipp2p:all
SAVE:T ::/0 ::/0 all - - - !0
If a packet hasn't been classified (packet mark is 0), copy the connection mark to the packet mark. If the packet mark is set, we're done. If the packet is P2P, set the packet mark to 4. If the packet mark has been set, save it to the connection mark.
/etc/shorewall/mangle
/etc/shorewall6/mangle
https://shorewall.org/traffic_shaping.htm[14]
https://shorewall.org/MultiISP.html[3]
https://shorewall.org/PacketMarking.html[15]
https://shorewall.org/configuration_file_basics.htm#Pairs[16]
09/24/2020 | Configuration Files |