Solution by benjaminchew13.
Let P(x,y) denote the assertion
f(xf(x)+f(xy))=x2+f(x)f(y).
First, P(0,0) gives
f(f(0))=f(0)2,
while P(0,y) gives
f(f(0))=f(0)f(y).
If f(0)=0, then f is constant, which is impossible because the term x2 appears on the right-hand side. Hence
f(0)=0.
Then P(x,0) gives
f(xf(x))=x2.(1)
In particular, if f(u)=0, then 0=f(uf(u))=u2, so
f(u)=0⟺u=0.(2)
Next,
P(−1,1):f(−f(−1)+f(−1))=1+f(−1)f(1),
so, using f(0)=0,
f(−1)f(1)=−1.(3)
Also,
P(1,−1):f(f(1)+f(−1))=1+f(1)f(−1)=0.
By (2),
f(1)+f(−1)=0.
Together with (3), this gives
f(1)2=1.
We split into two cases.
If f(1)=1, then f(−1)=−1. From P(1,y) we get
f(f(y)+1)=f(y)+1.(4)
Also, comparing P(−1,y) with P(1,−y) gives
1−f(y)=f(f(−y)+1)=f(−y)+1,
so
f(−y)=−f(y).
Thus f is odd. From (1), every nonnegative real number lies in the range of f, and by oddness every nonpositive real number also lies in the range. Hence f is surjective. In (4), let f(y)+1 range over all real numbers. We get
f(x)=x
for all real x.
If f(1)=−1, then f(−1)=1. From P(1,y) we get
f(f(y)−1)=1−f(y).(5)
Comparing P(−1,y) with P(1,−y) gives
1+f(y)=f(f(−y)−1)=1−f(−y),
so again f is odd. As before, (1) and oddness imply that f is surjective. In (5), let f(y)−1 range over all real numbers. We get
f(x)=−x
for all real x.
Both functions are directly checked, so the solutions are
f(x)=xorf(x)=−x.