diff --git a/client/firewall/iptables/router_linux.go b/client/firewall/iptables/router_linux.go index b59c88580..bb799b99b 100644 --- a/client/firewall/iptables/router_linux.go +++ b/client/firewall/iptables/router_linux.go @@ -631,7 +631,9 @@ func (r *router) addNatRule(pair firewall.RouterPair) error { "-j", "MARK", "--set-mark", fmt.Sprintf("%#x", markValue), ) - if err := r.iptablesClient.Append(tableMangle, chainRTPRE, rule...); err != nil { + // Ensure nat rules come first, so the mark can be overwritten. + // Currently overwritten by the dst-type LOCAL rules for redirected traffic. + if err := r.iptablesClient.Insert(tableMangle, chainRTPRE, 1, rule...); err != nil { // TODO: rollback ipset counter return fmt.Errorf("error while adding marking rule for %s: %v", pair.Destination, err) } diff --git a/client/firewall/nftables/router_linux.go b/client/firewall/nftables/router_linux.go index c2ba2a072..0f6c5bdf6 100644 --- a/client/firewall/nftables/router_linux.go +++ b/client/firewall/nftables/router_linux.go @@ -666,7 +666,9 @@ func (r *router) addNatRule(pair firewall.RouterPair) error { } } - r.rules[ruleKey] = r.conn.AddRule(&nftables.Rule{ + // Ensure nat rules come first, so the mark can be overwritten. + // Currently overwritten by the dst-type LOCAL rules for redirected traffic. + r.rules[ruleKey] = r.conn.InsertRule(&nftables.Rule{ Table: r.workTable, Chain: r.chains[chainNameManglePrerouting], Exprs: exprs,