6. Let $k$ and $m$ be two real constants, and the function $f: \mathbf{R} \rightarrow \mathbf{R}$ satisfies for any $x, y \in \mathbf{R}$,
$$
f(x f(y))=k x y+x+m \text{. }
$$

Then the necessary and sufficient conditions for $k$ and $m$, and all such expressions for $f(x)$ are $\qquad$
6. $m \neq 0$, and $k=\frac{1}{m^{2}}, f(x)=\frac{x}{m}+m$.

From the problem, for any $x, y \in \mathbf{R}$, we have
$$
f(x f(y))=k x y+x+m \text{. }
$$

Clearly, $f(x) \neq 0$ (if $f(x) \equiv 0$, it does not satisfy equation (1), so there exists at least one $y_{1} \in \mathbf{R}$ such that $f\left(y_{1}\right) \neq 0$.
Substituting $y=y_{1}$ into equation (1), we get
$$
f\left(x f\left(y_{1}\right)\right)=\left(k y_{1}+1\right) x+m \text{. }
$$

In equation (2), substituting $\frac{x}{f\left(y_{1}\right)}$ for $x$, we get
$$
f(x)=\frac{k y_{1}+1}{f\left(y_{1}\right)} x+m,
$$

i.e., $f(x)=a x+m (\forall x \in \mathbf{R})$.
Here, $a=\frac{k y_{1}+1}{f\left(y_{1}\right)}$ is a constant.
Substituting equation (3) into equation (1), we get
$$
a[x(a y+m)]+m=k x y+x+m \text{, }
$$

i.e., $a^{2} x y+a m x=k x y+x$.
From equation (4), we have $a^{2}=k, a m=1$.
Thus, $a=\frac{1}{m} (m \neq 0), k=a^{2}=\frac{1}{m^{2}}$.
Therefore, $f(x)=\frac{x}{m}+m$.
In summary, the necessary and sufficient condition is “$m \neq 0$, and $k=\frac{1}{m^{2}}$, while $f(x)=\frac{x}{m}+m$.”