Obviously, the identical function f(p)=p for all p∈P is a solution. We will show that this is the only one.
First we will show that f(2)=2. Taking q=2 and p any odd prime number, we have
f(p)f(2)+2p=f(2)f(p)+p2.
Assume that f(2)=2. It follows that f(2) is odd and so f(p)=2 for any odd prime number p.
Taking any two different odd prime numbers p,q we have
22+qp=22+pq⇒pq=qp⇒p=q,
contradiction. Hence, f(2)=2.
So for any odd prime number p we have
f(p)2+2p=2f(p)+p2.
Copy this relation as
2p−p2=2f(p)−f(p)2.(1)
Let T be the set of all positive integers greater than 2, i.e. T={3,4,5,…}. The function g:T→Z, g(n)=2n−n2, is strictly increasing, i.e.
g(n+1)−g(n)=2n−2n−1>0(2)
for all n∈T. We show this by induction. Indeed, for n=3 it is true, 23−2⋅3−1>0. Assume that 2k−2k−1>0. It follows that for n=k+1 we have
2k+1−2(k+1)−1=(2k−2k−1)+(2k−2)>0
for any k≥3. Therefore, (2) is true for all n∈T.
As consequence, (1) holds if and only if f(p)=p for all odd prime numbers p, as well as for p=2.
Therefore, the only function that satisfies the given relation is f(p)=p, for all p∈P.