Solution by OronSH.
We claim f(x)=x and f(x)=0 are the only solutions, which work.
Denote the assertion by P(x,y). Note P(0,0) gives f(0)2=0, hence f(0)=0. Next P(x,0) gives f(xf(x))=f(x2).
Note that if f is injective, this immediately implies f(x)=x for all x. Hence suppose that for some a,b we have f(a)=f(b)=c. First P(a,b) and P(b,a) give f(ac+bc)=f(a2)+c2=f(b2)+c2, so f(a2)=f(b2).
Next take P(a,x−a) and P(b,x−b) to get f(xf(a))=f(a2)+cf(x−a) and likewise for b, but this implies cf(x−a)=cf(x−b). Hence c is zero, or f(x−a)=f(x−b), hence f is periodic with period a−b.
Assume the latter is never true, so f is injective except for its roots. Hence from f(xf(x))=f(x2) we get f(x2)=0 or f(x)=x.
Assume for some nonzero x that f(x2)=0, so f(x)=x. If no y=0 goes to zero, then f is injective. Hence pick y=0 with f(0)=0. Then P(x,y) gives f(x2+xy)=f(x2). Since xy=0, we have that x2 is a root, contradiction. Hence f(x2)=0, so all nonnegatives map to zero.
Suppose there are x,y with f(x),f(y)=0. Then P(x,y) and P(y,x) give f(xf(x)+yf(x))=f(x)f(y)=f(xf(y)+yf(y)). Since f(x)f(y)=0, injectivity gives xf(x)+yf(x)=xf(y)+yf(y). Since x,y must both be negative, this implies f(x)=f(y). Since they are nonzero again, we get x=y. Hence there exists at most one value of x with f(x) nonzero.
Suppose such x exists. For this value of x, take P(x,x) giving f(2xf(x))=f(x)2. Since f(2xf(x)) is nonzero, we get 2xf(x)=x, so f(x)=21. However, plugging in gives 21=41, contradiction. Hence f is constantly zero.
Otherwise, f is periodic. Let p be a period of f and take P(x+p,y) and P(x,y+p) to get f(xf(x)+yf(x)+pf(x))=f((x+p)2)+f(x)f(y)=f(x2)+f(x)f(y). Hence f(x2)=f((x+p)2), and similarly these equal f((x+2p)2). Assume f(x2)=0, so that from our injectivity argument it follows that (x+p)2−x2=2xp+p2 is a period. Similarly (x+2p)2−(x+p)2=2xp+3p2 is a period. Taking thrice the former minus the latter gives 4xp is a period.
Taking the period p here to be 41 gives that x is a period, but f(x)=0 implies f(xf(x))=f(x2)=0, contradiction, so 41 is not a period. Taking the period p here to itself be 4xp gives that 16x2p is a period. Hence if x is positive we have that f(x)=0 implies 16xp is a period.
In fact we can remove the positivity constraint, since if q is a period then we can pick a positive integer n such that x+nq is positive, then from f(x+nq)=f(x)=0 we have 16p(x+nq) is a period. We also get 16p(x+(n+1)q) is a period, so their difference 16pq is a period, hence 16px is a period as required.
Let y be a root of f and suppose x has f(x2)=0. Then f(xf(x)+yf(x))=f(x2)=f(xf(x))=0 gives yf(x) is a period by our injectivity argument. Hence if f is not constantly zero, by periodicity we can choose such an x2, which implies f(x)=c=0 from f(xf(x))=f(x2).
Hence we have that for each x, if it goes to zero, then cx is a period, otherwise 16px is a period. Since 41 is not a period, taking x=64p1 must give that 64pc is a period. Hence taking p to be this in 16px, we get that for each x at least one of cx or 4pcx is a period. Changing variables, we get that at least one of x or 4px is a period.
Hence if x is not a period then 4px is, and so is 16px. However we have shown that if q is a period, then 16pq is a period. Hence 16px is always a period. Since this takes every real value, we get that every real is a period of f, hence f is constantly zero.
This resolves all cases, hence f(x)=x and f(x)=0 are the only solutions.