Difference between revisions of "Mangle packet header fields"

From nftables wiki
Jump to: navigation, search
(add example of using notrack, along with a reference to the article.)
(use raw/prerouting rather than mangle/forward)
Line 6: Line 6:
  
 
<source lang="bash">
 
<source lang="bash">
% nft add table mangle
+
% nft add table raw
% nft add chain mangle forward {type filter hook forward priority 0\;}
+
% nft add chain raw prerouting {type filter hook prerouting priority -300\;}
% nft add rule mangle forward tcp dport 8080 tcp dport set 80
+
% nft add rule raw prerouting tcp dport 8080 tcp dport set 80
 
</source>
 
</source>
  
The commands above create a table named ''mangle'', a chain named ''forward'', see [[Netfilter hooks]], and a rule to mangle the destination port of packets over TCP from 8080 to 80.
+
The commands above create a table named ''raw'', a chain named ''prerouting'', see [[Netfilter hooks]], and a rule to mangle the destination port of packets over TCP from 8080 to 80.
  
 
Keep in mind the interactions with conntrack, flows with mangled traffic must be [[Setting packet connection tracking metainformation | untracked]]. You can do this in a single rule:
 
Keep in mind the interactions with conntrack, flows with mangled traffic must be [[Setting packet connection tracking metainformation | untracked]]. You can do this in a single rule:
  
 
<source>
 
<source>
% nft add rule ip6 mangle forward ip6 daddr fd00::1 ip6 daddr set fd00::2 notrack
+
% nft add rule ip6 raw prerouting ip6 daddr fd00::1 ip6 daddr set fd00::2 notrack
 
</source>
 
</source>
  
 
For more information about packet headers to mangle check manpage nft(8), [[Matching packet header fields]] and [[Quick reference-nftables in 10 minutes]].
 
For more information about packet headers to mangle check manpage nft(8), [[Matching packet header fields]] and [[Quick reference-nftables in 10 minutes]].

Revision as of 12:08, 11 January 2018

Mangle packet header fields

Since nft v0.6 nftables supports stateless payload mangling. Note that if you mangle packet fields that are included in the layer 4 checksum pseudoheader, then you require a Linux kernel version >= 4.10.

To mangle packet header fields you should create a rule to match the packet, match the desired header field and set a new value to it:

% nft add table raw
% nft add chain raw prerouting {type filter hook prerouting priority -300\;}
% nft add rule raw prerouting tcp dport 8080 tcp dport set 80

The commands above create a table named raw, a chain named prerouting, see Netfilter hooks, and a rule to mangle the destination port of packets over TCP from 8080 to 80.

Keep in mind the interactions with conntrack, flows with mangled traffic must be untracked. You can do this in a single rule:

% nft add rule ip6 raw prerouting ip6 daddr fd00::1 ip6 daddr set fd00::2 notrack

For more information about packet headers to mangle check manpage nft(8), Matching packet header fields and Quick reference-nftables in 10 minutes.