How do I generate a session key using the Diffie Hellman algorithm? How to generate a session key between two nodes in two different subnets when the nodes don't know each other directly, using diffie hellman algorithm?
If the nodes can send messages to each other ( via any number of hops/ intermediate nodes) then you can do straight standard diffie-hellmen even if they don't have a prior relationship. Indeed, thats the point of diffie-hellmen.

However, any one of the intermediate nodes could launch a man in the middle attack and could then impersonate either node to the other. There is no way to stop this if the nodes actually don't know anything about eachother. If they do know each other, you can used authenticated diffie-helmen key exchange or a public key crypto system. Agian, these only work if the nodes know each other's public key before hand.