I was confused if ICMP is a network layer protocol why does it send transport layer fields in its error reporting messages.
2 Answers
Because it is his very purpose.
ICMP was specifically created and designed to do so.
From RFC792
Introduction
[...]
Occasionally a gateway or destination host will communicate with a source host, for example, to report an error in datagram processing. For such purposes this protocol, the Internet Control Message Protocol (ICMP), is used.
[...]ICMP messages are sent in several situations: for example, when a datagram cannot reach its destination, when the gateway does not have the buffering capacity to forward a datagram, and when the gateway can direct the host to send traffic on a shorter route.
[...]
The ICMP messages typically report errors in the processing of
datagrams.
Also remember that the layered model is juts that, a model, there's many things that cross layer boundaries.
ICMP errors will return part of the packet that caused the error. This is to provide information about which connection caused the error. If I simply return "time exceeded" with no context, the originator will (a) not know who I am -- being a random transit router, and (b) not have any clue which connection is in error. If I include the IP header in the error, then the originator can tell which connection caused it. (i.e. "my packet to X failed at Y for reason Z")