The Sin of Wasting Sperm

1) It is forbidden to emit sperm to waste [i.e. to fornicate], and this sin is greater than any other sin of the Torah. Thereby a man may not perform the spilling of the sperm outside the womb [or anywhere else], nor may he may he marry a girl too young to have children.

3) It is forbidden for a man to cause self-erection [masturbation] or to think erotic thoughts [fantasy]. If such thoughts do come to him he should discard them for thoughts of the Torah, since the Torah is liken to a “beloved hind, inspiring favour.” Thus it is forbidden for a man to sleep flat on his back, but he should turn slightly to one side to prevent him having an erection. He must not look at male and female animals [pornography] or birds [thoughts] that are copulating.

Quoted from Even Haezer Siman 23.